isl_map_simplify.c: remove unused set_swap_inequality
[isl.git] / isl_map.c
blob9d0ffbf9cb2593233c22da0c7be830bf14a19167
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 struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
1283 int i = 0;
1284 unsigned total;
1285 if (!bmap)
1286 goto error;
1287 total = isl_basic_map_total_dim(bmap);
1288 isl_basic_map_free_div(bmap, bmap->n_div);
1289 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1290 if (bmap->n_eq > 0)
1291 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
1292 else {
1293 i = isl_basic_map_alloc_equality(bmap);
1294 if (i < 0)
1295 goto error;
1297 isl_int_set_si(bmap->eq[i][0], 1);
1298 isl_seq_clr(bmap->eq[i]+1, total);
1299 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
1300 isl_vec_free(bmap->sample);
1301 bmap->sample = NULL;
1302 return isl_basic_map_finalize(bmap);
1303 error:
1304 isl_basic_map_free(bmap);
1305 return NULL;
1308 struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
1310 return (struct isl_basic_set *)
1311 isl_basic_map_set_to_empty((struct isl_basic_map *)bset);
1314 void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
1316 int i;
1317 unsigned off = isl_dim_total(bmap->dim);
1318 isl_int *t = bmap->div[a];
1319 bmap->div[a] = bmap->div[b];
1320 bmap->div[b] = t;
1322 for (i = 0; i < bmap->n_eq; ++i)
1323 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
1325 for (i = 0; i < bmap->n_ineq; ++i)
1326 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
1328 for (i = 0; i < bmap->n_div; ++i)
1329 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
1330 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1333 /* Eliminate the specified n dimensions starting at first from the
1334 * constraints using Fourier-Motzkin. The dimensions themselves
1335 * are not removed.
1337 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
1338 enum isl_dim_type type, unsigned first, unsigned n)
1340 int i;
1342 if (!map)
1343 return NULL;
1344 if (n == 0)
1345 return map;
1347 map = isl_map_cow(map);
1348 if (!map)
1349 return NULL;
1350 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
1351 first += pos(map->dim, type) - 1;
1353 for (i = 0; i < map->n; ++i) {
1354 map->p[i] = isl_basic_map_eliminate_vars(map->p[i], first, n);
1355 if (!map->p[i])
1356 goto error;
1358 return map;
1359 error:
1360 isl_map_free(map);
1361 return NULL;
1364 /* Eliminate the specified n dimensions starting at first from the
1365 * constraints using Fourier-Motzkin. The dimensions themselves
1366 * are not removed.
1368 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
1369 enum isl_dim_type type, unsigned first, unsigned n)
1371 return (isl_set *)isl_map_eliminate((isl_map *)set, type, first, n);
1374 /* Eliminate the specified n dimensions starting at first from the
1375 * constraints using Fourier-Motzkin. The dimensions themselves
1376 * are not removed.
1378 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
1379 unsigned first, unsigned n)
1381 return isl_set_eliminate(set, isl_dim_set, first, n);
1384 __isl_give isl_basic_map *isl_basic_map_remove_divs(
1385 __isl_take isl_basic_map *bmap)
1387 bmap = isl_basic_map_eliminate_vars(bmap, isl_dim_total(bmap->dim),
1388 bmap->n_div);
1389 if (!bmap)
1390 return NULL;
1391 bmap->n_div = 0;
1392 return isl_basic_map_finalize(bmap);
1395 __isl_give isl_basic_set *isl_basic_set_remove_divs(
1396 __isl_take isl_basic_set *bset)
1398 return (struct isl_basic_set *)isl_basic_map_remove_divs(
1399 (struct isl_basic_map *)bset);
1402 __isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
1404 int i;
1406 if (!map)
1407 return NULL;
1408 if (map->n == 0)
1409 return map;
1411 map = isl_map_cow(map);
1412 if (!map)
1413 return NULL;
1415 for (i = 0; i < map->n; ++i) {
1416 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
1417 if (!map->p[i])
1418 goto error;
1420 return map;
1421 error:
1422 isl_map_free(map);
1423 return NULL;
1426 __isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
1428 return isl_map_remove_divs(set);
1431 struct isl_basic_map *isl_basic_map_remove_dims(struct isl_basic_map *bmap,
1432 enum isl_dim_type type, unsigned first, unsigned n)
1434 if (!bmap)
1435 return NULL;
1436 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1437 goto error);
1438 if (n == 0)
1439 return bmap;
1440 bmap = isl_basic_map_eliminate_vars(bmap,
1441 isl_basic_map_offset(bmap, type) - 1 + first, n);
1442 if (!bmap)
1443 return bmap;
1444 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
1445 return bmap;
1446 bmap = isl_basic_map_drop(bmap, type, first, n);
1447 return bmap;
1448 error:
1449 isl_basic_map_free(bmap);
1450 return NULL;
1453 /* Return true if the definition of the given div (recursively) involves
1454 * any of the given variables.
1456 static int div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
1457 unsigned first, unsigned n)
1459 int i;
1460 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
1462 if (isl_int_is_zero(bmap->div[div][0]))
1463 return 0;
1464 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
1465 return 1;
1467 for (i = bmap->n_div - 1; i >= 0; --i) {
1468 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
1469 continue;
1470 if (div_involves_vars(bmap, i, first, n))
1471 return 1;
1474 return 0;
1477 /* Remove all divs (recursively) involving any of the given dimensions
1478 * in their definitions.
1480 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
1481 __isl_take isl_basic_map *bmap,
1482 enum isl_dim_type type, unsigned first, unsigned n)
1484 int i;
1486 if (!bmap)
1487 return NULL;
1488 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1489 goto error);
1490 first += isl_basic_map_offset(bmap, type);
1492 for (i = bmap->n_div - 1; i >= 0; --i) {
1493 if (!div_involves_vars(bmap, i, first, n))
1494 continue;
1495 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
1498 return bmap;
1499 error:
1500 isl_basic_map_free(bmap);
1501 return NULL;
1504 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
1505 enum isl_dim_type type, unsigned first, unsigned n)
1507 int i;
1509 if (!map)
1510 return NULL;
1511 if (map->n == 0)
1512 return map;
1514 map = isl_map_cow(map);
1515 if (!map)
1516 return NULL;
1518 for (i = 0; i < map->n; ++i) {
1519 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
1520 type, first, n);
1521 if (!map->p[i])
1522 goto error;
1524 return map;
1525 error:
1526 isl_map_free(map);
1527 return NULL;
1530 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
1531 enum isl_dim_type type, unsigned first, unsigned n)
1533 return (isl_set *)isl_map_remove_divs_involving_dims((isl_map *)set,
1534 type, first, n);
1537 int isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
1538 enum isl_dim_type type, unsigned first, unsigned n)
1540 int i;
1542 if (!bset)
1543 return -1;
1545 if (first + n > isl_basic_set_dim(bset, type))
1546 isl_die(bset->ctx, isl_error_invalid,
1547 "index out of bounds", return -1);
1549 first += isl_basic_set_offset(bset, type);
1550 for (i = 0; i < bset->n_eq; ++i)
1551 if (isl_seq_first_non_zero(bset->eq[i] + first, n) >= 0)
1552 return 1;
1553 for (i = 0; i < bset->n_ineq; ++i)
1554 if (isl_seq_first_non_zero(bset->ineq[i] + first, n) >= 0)
1555 return 1;
1557 return 0;
1560 int isl_set_involves_dims(__isl_keep isl_set *set,
1561 enum isl_dim_type type, unsigned first, unsigned n)
1563 int i;
1565 if (!set)
1566 return -1;
1568 if (first + n > isl_set_dim(set, type))
1569 isl_die(set->ctx, isl_error_invalid,
1570 "index out of bounds", return -1);
1572 for (i = 0; i < set->n; ++i) {
1573 int involves = isl_basic_set_involves_dims(set->p[i],
1574 type, first, n);
1575 if (involves < 0 || !involves)
1576 return involves;
1579 return 1;
1582 /* Return true if the definition of the given div is unknown or depends
1583 * on unknown divs.
1585 static int div_is_unknown(__isl_keep isl_basic_map *bmap, int div)
1587 int i;
1588 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
1590 if (isl_int_is_zero(bmap->div[div][0]))
1591 return 1;
1593 for (i = bmap->n_div - 1; i >= 0; --i) {
1594 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
1595 continue;
1596 if (div_is_unknown(bmap, i))
1597 return 1;
1600 return 0;
1603 /* Remove all divs that are unknown or defined in terms of unknown divs.
1605 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
1606 __isl_take isl_basic_map *bmap)
1608 int i;
1610 if (!bmap)
1611 return NULL;
1613 for (i = bmap->n_div - 1; i >= 0; --i) {
1614 if (!div_is_unknown(bmap, i))
1615 continue;
1616 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
1619 return bmap;
1622 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
1624 int i;
1626 if (!map)
1627 return NULL;
1628 if (map->n == 0)
1629 return map;
1631 map = isl_map_cow(map);
1632 if (!map)
1633 return NULL;
1635 for (i = 0; i < map->n; ++i) {
1636 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
1637 if (!map->p[i])
1638 goto error;
1640 return map;
1641 error:
1642 isl_map_free(map);
1643 return NULL;
1646 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
1648 return (isl_set *)isl_map_remove_unknown_divs((isl_map *)set);
1651 __isl_give isl_basic_set *isl_basic_set_remove_dims(
1652 __isl_take isl_basic_set *bset,
1653 enum isl_dim_type type, unsigned first, unsigned n)
1655 return (isl_basic_set *)
1656 isl_basic_map_remove_dims((isl_basic_map *)bset, type, first, n);
1659 struct isl_map *isl_map_remove_dims(struct isl_map *map,
1660 enum isl_dim_type type, unsigned first, unsigned n)
1662 int i;
1664 if (n == 0)
1665 return map;
1667 map = isl_map_cow(map);
1668 if (!map)
1669 return NULL;
1670 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
1672 for (i = 0; i < map->n; ++i) {
1673 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
1674 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
1675 if (!map->p[i])
1676 goto error;
1678 map = isl_map_drop(map, type, first, n);
1679 return map;
1680 error:
1681 isl_map_free(map);
1682 return NULL;
1685 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
1686 enum isl_dim_type type, unsigned first, unsigned n)
1688 return (isl_set *)isl_map_remove_dims((isl_map *)bset, type, first, n);
1691 /* Project out n inputs starting at first using Fourier-Motzkin */
1692 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
1693 unsigned first, unsigned n)
1695 return isl_map_remove_dims(map, isl_dim_in, first, n);
1698 static void dump_term(struct isl_basic_map *bmap,
1699 isl_int c, int pos, FILE *out)
1701 const char *name;
1702 unsigned in = isl_basic_map_n_in(bmap);
1703 unsigned dim = in + isl_basic_map_n_out(bmap);
1704 unsigned nparam = isl_basic_map_n_param(bmap);
1705 if (!pos)
1706 isl_int_print(out, c, 0);
1707 else {
1708 if (!isl_int_is_one(c))
1709 isl_int_print(out, c, 0);
1710 if (pos < 1 + nparam) {
1711 name = isl_dim_get_name(bmap->dim,
1712 isl_dim_param, pos - 1);
1713 if (name)
1714 fprintf(out, "%s", name);
1715 else
1716 fprintf(out, "p%d", pos - 1);
1717 } else if (pos < 1 + nparam + in)
1718 fprintf(out, "i%d", pos - 1 - nparam);
1719 else if (pos < 1 + nparam + dim)
1720 fprintf(out, "o%d", pos - 1 - nparam - in);
1721 else
1722 fprintf(out, "e%d", pos - 1 - nparam - dim);
1726 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
1727 int sign, FILE *out)
1729 int i;
1730 int first;
1731 unsigned len = 1 + isl_basic_map_total_dim(bmap);
1732 isl_int v;
1734 isl_int_init(v);
1735 for (i = 0, first = 1; i < len; ++i) {
1736 if (isl_int_sgn(c[i]) * sign <= 0)
1737 continue;
1738 if (!first)
1739 fprintf(out, " + ");
1740 first = 0;
1741 isl_int_abs(v, c[i]);
1742 dump_term(bmap, v, i, out);
1744 isl_int_clear(v);
1745 if (first)
1746 fprintf(out, "0");
1749 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
1750 const char *op, FILE *out, int indent)
1752 int i;
1754 fprintf(out, "%*s", indent, "");
1756 dump_constraint_sign(bmap, c, 1, out);
1757 fprintf(out, " %s ", op);
1758 dump_constraint_sign(bmap, c, -1, out);
1760 fprintf(out, "\n");
1762 for (i = bmap->n_div; i < bmap->extra; ++i) {
1763 if (isl_int_is_zero(c[1+isl_dim_total(bmap->dim)+i]))
1764 continue;
1765 fprintf(out, "%*s", indent, "");
1766 fprintf(out, "ERROR: unused div coefficient not zero\n");
1767 abort();
1771 static void dump_constraints(struct isl_basic_map *bmap,
1772 isl_int **c, unsigned n,
1773 const char *op, FILE *out, int indent)
1775 int i;
1777 for (i = 0; i < n; ++i)
1778 dump_constraint(bmap, c[i], op, out, indent);
1781 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
1783 int j;
1784 int first = 1;
1785 unsigned total = isl_basic_map_total_dim(bmap);
1787 for (j = 0; j < 1 + total; ++j) {
1788 if (isl_int_is_zero(exp[j]))
1789 continue;
1790 if (!first && isl_int_is_pos(exp[j]))
1791 fprintf(out, "+");
1792 dump_term(bmap, exp[j], j, out);
1793 first = 0;
1797 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
1799 int i;
1801 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
1802 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
1804 for (i = 0; i < bmap->n_div; ++i) {
1805 fprintf(out, "%*s", indent, "");
1806 fprintf(out, "e%d = [(", i);
1807 dump_affine(bmap, bmap->div[i]+1, out);
1808 fprintf(out, ")/");
1809 isl_int_print(out, bmap->div[i][0], 0);
1810 fprintf(out, "]\n");
1814 void isl_basic_set_print_internal(struct isl_basic_set *bset,
1815 FILE *out, int indent)
1817 if (!bset) {
1818 fprintf(out, "null basic set\n");
1819 return;
1822 fprintf(out, "%*s", indent, "");
1823 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
1824 bset->ref, bset->dim->nparam, bset->dim->n_out,
1825 bset->extra, bset->flags);
1826 dump((struct isl_basic_map *)bset, out, indent);
1829 void isl_basic_map_print_internal(struct isl_basic_map *bmap,
1830 FILE *out, int indent)
1832 if (!bmap) {
1833 fprintf(out, "null basic map\n");
1834 return;
1837 fprintf(out, "%*s", indent, "");
1838 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
1839 "flags: %x, n_name: %d\n",
1840 bmap->ref,
1841 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
1842 bmap->extra, bmap->flags, bmap->dim->n_name);
1843 dump(bmap, out, indent);
1846 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
1848 unsigned total;
1849 if (!bmap)
1850 return -1;
1851 total = isl_basic_map_total_dim(bmap);
1852 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1853 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
1854 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
1855 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1856 return 0;
1859 struct isl_set *isl_set_alloc_dim(struct isl_dim *dim, int n, unsigned flags)
1861 struct isl_set *set;
1863 if (!dim)
1864 return NULL;
1865 isl_assert(dim->ctx, dim->n_in == 0, goto error);
1866 isl_assert(dim->ctx, n >= 0, goto error);
1867 set = isl_alloc(dim->ctx, struct isl_set,
1868 sizeof(struct isl_set) +
1869 (n - 1) * sizeof(struct isl_basic_set *));
1870 if (!set)
1871 goto error;
1873 set->ctx = dim->ctx;
1874 isl_ctx_ref(set->ctx);
1875 set->ref = 1;
1876 set->size = n;
1877 set->n = 0;
1878 set->dim = dim;
1879 set->flags = flags;
1880 return set;
1881 error:
1882 isl_dim_free(dim);
1883 return NULL;
1886 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
1887 unsigned nparam, unsigned dim, int n, unsigned flags)
1889 struct isl_set *set;
1890 struct isl_dim *dims;
1892 dims = isl_dim_alloc(ctx, nparam, 0, dim);
1893 if (!dims)
1894 return NULL;
1896 set = isl_set_alloc_dim(dims, n, flags);
1897 return set;
1900 /* Make sure "map" has room for at least "n" more basic maps.
1902 struct isl_map *isl_map_grow(struct isl_map *map, int n)
1904 int i;
1905 struct isl_map *grown = NULL;
1907 if (!map)
1908 return NULL;
1909 isl_assert(map->ctx, n >= 0, goto error);
1910 if (map->n + n <= map->size)
1911 return map;
1912 grown = isl_map_alloc_dim(isl_map_get_dim(map), map->n + n, map->flags);
1913 if (!grown)
1914 goto error;
1915 for (i = 0; i < map->n; ++i) {
1916 grown->p[i] = isl_basic_map_copy(map->p[i]);
1917 if (!grown->p[i])
1918 goto error;
1919 grown->n++;
1921 isl_map_free(map);
1922 return grown;
1923 error:
1924 isl_map_free(grown);
1925 isl_map_free(map);
1926 return NULL;
1929 /* Make sure "set" has room for at least "n" more basic sets.
1931 struct isl_set *isl_set_grow(struct isl_set *set, int n)
1933 return (struct isl_set *)isl_map_grow((struct isl_map *)set, n);
1936 struct isl_set *isl_set_dup(struct isl_set *set)
1938 int i;
1939 struct isl_set *dup;
1941 if (!set)
1942 return NULL;
1944 dup = isl_set_alloc_dim(isl_dim_copy(set->dim), set->n, set->flags);
1945 if (!dup)
1946 return NULL;
1947 for (i = 0; i < set->n; ++i)
1948 dup = isl_set_add_basic_set(dup, isl_basic_set_copy(set->p[i]));
1949 return dup;
1952 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
1954 return isl_map_from_basic_map(bset);
1957 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
1959 struct isl_map *map;
1961 if (!bmap)
1962 return NULL;
1964 map = isl_map_alloc_dim(isl_dim_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
1965 return isl_map_add_basic_map(map, bmap);
1968 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
1969 __isl_take isl_basic_set *bset)
1971 return (struct isl_set *)isl_map_add_basic_map((struct isl_map *)set,
1972 (struct isl_basic_map *)bset);
1975 void isl_set_free(struct isl_set *set)
1977 int i;
1979 if (!set)
1980 return;
1982 if (--set->ref > 0)
1983 return;
1985 isl_ctx_deref(set->ctx);
1986 for (i = 0; i < set->n; ++i)
1987 isl_basic_set_free(set->p[i]);
1988 isl_dim_free(set->dim);
1989 free(set);
1992 void isl_set_print_internal(struct isl_set *set, FILE *out, int indent)
1994 int i;
1996 if (!set) {
1997 fprintf(out, "null set\n");
1998 return;
2001 fprintf(out, "%*s", indent, "");
2002 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
2003 set->ref, set->n, set->dim->nparam, set->dim->n_out,
2004 set->flags);
2005 for (i = 0; i < set->n; ++i) {
2006 fprintf(out, "%*s", indent, "");
2007 fprintf(out, "basic set %d:\n", i);
2008 isl_basic_set_print_internal(set->p[i], out, indent+4);
2012 void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
2014 int i;
2016 if (!map) {
2017 fprintf(out, "null map\n");
2018 return;
2021 fprintf(out, "%*s", indent, "");
2022 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2023 "flags: %x, n_name: %d\n",
2024 map->ref, map->n, map->dim->nparam, map->dim->n_in,
2025 map->dim->n_out, map->flags, map->dim->n_name);
2026 for (i = 0; i < map->n; ++i) {
2027 fprintf(out, "%*s", indent, "");
2028 fprintf(out, "basic map %d:\n", i);
2029 isl_basic_map_print_internal(map->p[i], out, indent+4);
2033 struct isl_basic_map *isl_basic_map_intersect_domain(
2034 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2036 struct isl_basic_map *bmap_domain;
2038 if (!bmap || !bset)
2039 goto error;
2041 isl_assert(bset->ctx, isl_dim_match(bmap->dim, isl_dim_param,
2042 bset->dim, isl_dim_param), goto error);
2044 if (isl_dim_size(bset->dim, isl_dim_set) != 0)
2045 isl_assert(bset->ctx,
2046 isl_basic_map_compatible_domain(bmap, bset), goto error);
2048 bmap = isl_basic_map_cow(bmap);
2049 if (!bmap)
2050 goto error;
2051 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
2052 bset->n_div, bset->n_eq, bset->n_ineq);
2053 bmap_domain = isl_basic_map_from_domain(bset);
2054 bmap = add_constraints(bmap, bmap_domain, 0, 0);
2056 bmap = isl_basic_map_simplify(bmap);
2057 return isl_basic_map_finalize(bmap);
2058 error:
2059 isl_basic_map_free(bmap);
2060 isl_basic_set_free(bset);
2061 return NULL;
2064 struct isl_basic_map *isl_basic_map_intersect_range(
2065 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2067 struct isl_basic_map *bmap_range;
2069 if (!bmap || !bset)
2070 goto error;
2072 isl_assert(bset->ctx, isl_dim_match(bmap->dim, isl_dim_param,
2073 bset->dim, isl_dim_param), goto error);
2075 if (isl_dim_size(bset->dim, isl_dim_set) != 0)
2076 isl_assert(bset->ctx,
2077 isl_basic_map_compatible_range(bmap, bset), goto error);
2079 if (isl_basic_set_is_universe(bset)) {
2080 isl_basic_set_free(bset);
2081 return bmap;
2084 bmap = isl_basic_map_cow(bmap);
2085 if (!bmap)
2086 goto error;
2087 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
2088 bset->n_div, bset->n_eq, bset->n_ineq);
2089 bmap_range = isl_basic_map_from_basic_set(bset, isl_dim_copy(bset->dim));
2090 bmap = add_constraints(bmap, bmap_range, 0, 0);
2092 bmap = isl_basic_map_simplify(bmap);
2093 return isl_basic_map_finalize(bmap);
2094 error:
2095 isl_basic_map_free(bmap);
2096 isl_basic_set_free(bset);
2097 return NULL;
2100 int isl_basic_map_contains(struct isl_basic_map *bmap, struct isl_vec *vec)
2102 int i;
2103 unsigned total;
2104 isl_int s;
2106 total = 1 + isl_basic_map_total_dim(bmap);
2107 if (total != vec->size)
2108 return -1;
2110 isl_int_init(s);
2112 for (i = 0; i < bmap->n_eq; ++i) {
2113 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
2114 if (!isl_int_is_zero(s)) {
2115 isl_int_clear(s);
2116 return 0;
2120 for (i = 0; i < bmap->n_ineq; ++i) {
2121 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
2122 if (isl_int_is_neg(s)) {
2123 isl_int_clear(s);
2124 return 0;
2128 isl_int_clear(s);
2130 return 1;
2133 int isl_basic_set_contains(struct isl_basic_set *bset, struct isl_vec *vec)
2135 return isl_basic_map_contains((struct isl_basic_map *)bset, vec);
2138 struct isl_basic_map *isl_basic_map_intersect(
2139 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2141 struct isl_vec *sample = NULL;
2143 if (!bmap1 || !bmap2)
2144 goto error;
2146 isl_assert(bmap1->ctx, isl_dim_match(bmap1->dim, isl_dim_param,
2147 bmap2->dim, isl_dim_param), goto error);
2148 if (isl_dim_total(bmap1->dim) ==
2149 isl_dim_size(bmap1->dim, isl_dim_param) &&
2150 isl_dim_total(bmap2->dim) !=
2151 isl_dim_size(bmap2->dim, isl_dim_param))
2152 return isl_basic_map_intersect(bmap2, bmap1);
2154 if (isl_dim_total(bmap2->dim) !=
2155 isl_dim_size(bmap2->dim, isl_dim_param))
2156 isl_assert(bmap1->ctx,
2157 isl_dim_equal(bmap1->dim, bmap2->dim), goto error);
2159 if (bmap1->sample &&
2160 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
2161 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
2162 sample = isl_vec_copy(bmap1->sample);
2163 else if (bmap2->sample &&
2164 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
2165 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
2166 sample = isl_vec_copy(bmap2->sample);
2168 bmap1 = isl_basic_map_cow(bmap1);
2169 if (!bmap1)
2170 goto error;
2171 bmap1 = isl_basic_map_extend_dim(bmap1, isl_dim_copy(bmap1->dim),
2172 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
2173 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
2175 if (!bmap1)
2176 isl_vec_free(sample);
2177 else if (sample) {
2178 isl_vec_free(bmap1->sample);
2179 bmap1->sample = sample;
2182 bmap1 = isl_basic_map_simplify(bmap1);
2183 return isl_basic_map_finalize(bmap1);
2184 error:
2185 if (sample)
2186 isl_vec_free(sample);
2187 isl_basic_map_free(bmap1);
2188 isl_basic_map_free(bmap2);
2189 return NULL;
2192 struct isl_basic_set *isl_basic_set_intersect(
2193 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
2195 return (struct isl_basic_set *)
2196 isl_basic_map_intersect(
2197 (struct isl_basic_map *)bset1,
2198 (struct isl_basic_map *)bset2);
2201 /* Special case of isl_map_intersect, where both map1 and map2
2202 * are convex, without any divs and such that either map1 or map2
2203 * contains a single constraint. This constraint is then simply
2204 * added to the other map.
2206 static __isl_give isl_map *map_intersect_add_constraint(
2207 __isl_take isl_map *map1, __isl_take isl_map *map2)
2209 isl_assert(map1->ctx, map1->n == 1, goto error);
2210 isl_assert(map2->ctx, map1->n == 1, goto error);
2211 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
2212 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
2214 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
2215 return isl_map_intersect(map2, map1);
2217 isl_assert(map2->ctx,
2218 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1, goto error);
2220 map1 = isl_map_cow(map1);
2221 if (!map1)
2222 goto error;
2223 if (isl_map_plain_is_empty(map1)) {
2224 isl_map_free(map2);
2225 return map1;
2227 map1->p[0] = isl_basic_map_cow(map1->p[0]);
2228 if (map2->p[0]->n_eq == 1)
2229 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
2230 else
2231 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
2232 map2->p[0]->ineq[0]);
2234 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
2235 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
2236 if (!map1->p[0])
2237 goto error;
2239 if (isl_basic_map_plain_is_empty(map1->p[0])) {
2240 isl_basic_map_free(map1->p[0]);
2241 map1->n = 0;
2244 isl_map_free(map2);
2246 return map1;
2247 error:
2248 isl_map_free(map1);
2249 isl_map_free(map2);
2250 return NULL;
2253 struct isl_map *isl_map_intersect(struct isl_map *map1, struct isl_map *map2)
2255 unsigned flags = 0;
2256 struct isl_map *result;
2257 int i, j;
2259 if (!map1 || !map2)
2260 goto error;
2262 if (isl_map_plain_is_empty(map1)) {
2263 isl_map_free(map2);
2264 return map1;
2266 if (isl_map_plain_is_empty(map2)) {
2267 isl_map_free(map1);
2268 return map2;
2271 if (map1->n == 1 && map2->n == 1 &&
2272 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
2273 isl_dim_equal(map1->dim, map2->dim) &&
2274 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
2275 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
2276 return map_intersect_add_constraint(map1, map2);
2277 isl_assert(map1->ctx, isl_dim_match(map1->dim, isl_dim_param,
2278 map2->dim, isl_dim_param), goto error);
2279 if (isl_dim_total(map1->dim) ==
2280 isl_dim_size(map1->dim, isl_dim_param) &&
2281 isl_dim_total(map2->dim) != isl_dim_size(map2->dim, isl_dim_param))
2282 return isl_map_intersect(map2, map1);
2284 if (isl_dim_total(map2->dim) != isl_dim_size(map2->dim, isl_dim_param))
2285 isl_assert(map1->ctx,
2286 isl_dim_equal(map1->dim, map2->dim), goto error);
2288 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
2289 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
2290 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
2292 result = isl_map_alloc_dim(isl_dim_copy(map1->dim),
2293 map1->n * map2->n, flags);
2294 if (!result)
2295 goto error;
2296 for (i = 0; i < map1->n; ++i)
2297 for (j = 0; j < map2->n; ++j) {
2298 struct isl_basic_map *part;
2299 part = isl_basic_map_intersect(
2300 isl_basic_map_copy(map1->p[i]),
2301 isl_basic_map_copy(map2->p[j]));
2302 if (isl_basic_map_is_empty(part))
2303 isl_basic_map_free(part);
2304 else
2305 result = isl_map_add_basic_map(result, part);
2306 if (!result)
2307 goto error;
2309 isl_map_free(map1);
2310 isl_map_free(map2);
2311 return result;
2312 error:
2313 isl_map_free(map1);
2314 isl_map_free(map2);
2315 return NULL;
2318 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
2320 return (struct isl_set *)
2321 isl_map_intersect((struct isl_map *)set1,
2322 (struct isl_map *)set2);
2325 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
2327 struct isl_dim *dim;
2328 struct isl_basic_set *bset;
2329 unsigned in;
2331 if (!bmap)
2332 return NULL;
2333 bmap = isl_basic_map_cow(bmap);
2334 if (!bmap)
2335 return NULL;
2336 dim = isl_dim_reverse(isl_dim_copy(bmap->dim));
2337 in = isl_basic_map_n_in(bmap);
2338 bset = isl_basic_set_from_basic_map(bmap);
2339 bset = isl_basic_set_swap_vars(bset, in);
2340 return isl_basic_map_from_basic_set(bset, dim);
2343 __isl_give isl_basic_map *isl_basic_map_insert(__isl_take isl_basic_map *bmap,
2344 enum isl_dim_type type, unsigned pos, unsigned n)
2346 struct isl_dim *res_dim;
2347 struct isl_basic_map *res;
2348 struct isl_dim_map *dim_map;
2349 unsigned total, off;
2350 enum isl_dim_type t;
2352 if (n == 0)
2353 return bmap;
2355 if (!bmap)
2356 return NULL;
2358 res_dim = isl_dim_insert(isl_basic_map_get_dim(bmap), type, pos, n);
2360 total = isl_basic_map_total_dim(bmap) + n;
2361 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2362 off = 0;
2363 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2364 if (t != type) {
2365 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2366 } else {
2367 unsigned size = isl_basic_map_dim(bmap, t);
2368 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2369 0, pos, off);
2370 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2371 pos, size - pos, off + pos + n);
2373 off += isl_dim_size(res_dim, t);
2375 isl_dim_map_div(dim_map, bmap, off);
2377 res = isl_basic_map_alloc_dim(res_dim,
2378 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2379 if (isl_basic_map_is_rational(bmap))
2380 res = isl_basic_map_set_rational(res);
2381 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
2382 return isl_basic_map_finalize(res);
2385 __isl_give isl_basic_map *isl_basic_map_add(__isl_take isl_basic_map *bmap,
2386 enum isl_dim_type type, unsigned n)
2388 if (!bmap)
2389 return NULL;
2390 return isl_basic_map_insert(bmap, type,
2391 isl_basic_map_dim(bmap, type), n);
2394 __isl_give isl_basic_set *isl_basic_set_add(__isl_take isl_basic_set *bset,
2395 enum isl_dim_type type, unsigned n)
2397 if (!bset)
2398 return NULL;
2399 isl_assert(bset->ctx, type != isl_dim_in, goto error);
2400 return (isl_basic_set *)isl_basic_map_add((isl_basic_map *)bset, type, n);
2401 error:
2402 isl_basic_set_free(bset);
2403 return NULL;
2406 __isl_give isl_map *isl_map_insert(__isl_take isl_map *map,
2407 enum isl_dim_type type, unsigned pos, unsigned n)
2409 int i;
2411 if (n == 0)
2412 return map;
2414 map = isl_map_cow(map);
2415 if (!map)
2416 return NULL;
2418 map->dim = isl_dim_insert(map->dim, type, pos, n);
2419 if (!map->dim)
2420 goto error;
2422 for (i = 0; i < map->n; ++i) {
2423 map->p[i] = isl_basic_map_insert(map->p[i], type, pos, n);
2424 if (!map->p[i])
2425 goto error;
2428 return map;
2429 error:
2430 isl_map_free(map);
2431 return NULL;
2434 __isl_give isl_set *isl_set_insert(__isl_take isl_set *set,
2435 enum isl_dim_type type, unsigned pos, unsigned n)
2437 return (isl_set *)isl_map_insert((isl_map *)set, type, pos, n);
2440 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
2441 enum isl_dim_type type, unsigned n)
2443 if (!map)
2444 return NULL;
2445 return isl_map_insert(map, type, isl_map_dim(map, type), n);
2448 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
2449 enum isl_dim_type type, unsigned n)
2451 if (!set)
2452 return NULL;
2453 isl_assert(set->ctx, type != isl_dim_in, goto error);
2454 return (isl_set *)isl_map_add_dims((isl_map *)set, type, n);
2455 error:
2456 isl_set_free(set);
2457 return NULL;
2460 __isl_give isl_basic_map *isl_basic_map_move_dims(
2461 __isl_take isl_basic_map *bmap,
2462 enum isl_dim_type dst_type, unsigned dst_pos,
2463 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2465 struct isl_dim_map *dim_map;
2466 struct isl_basic_map *res;
2467 enum isl_dim_type t;
2468 unsigned total, off;
2470 if (!bmap)
2471 return NULL;
2472 if (n == 0)
2473 return bmap;
2475 isl_assert(bmap->ctx, src_pos + n <= isl_basic_map_dim(bmap, src_type),
2476 goto error);
2478 if (dst_type == src_type && dst_pos == src_pos)
2479 return bmap;
2481 isl_assert(bmap->ctx, dst_type != src_type, goto error);
2483 if (pos(bmap->dim, dst_type) + dst_pos ==
2484 pos(bmap->dim, src_type) + src_pos +
2485 ((src_type < dst_type) ? n : 0)) {
2486 bmap = isl_basic_map_cow(bmap);
2487 if (!bmap)
2488 return NULL;
2490 bmap->dim = isl_dim_move(bmap->dim, dst_type, dst_pos,
2491 src_type, src_pos, n);
2492 if (!bmap->dim)
2493 goto error;
2495 bmap = isl_basic_map_finalize(bmap);
2497 return bmap;
2500 total = isl_basic_map_total_dim(bmap);
2501 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2503 off = 0;
2504 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2505 unsigned size = isl_dim_size(bmap->dim, t);
2506 if (t == dst_type) {
2507 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2508 0, dst_pos, off);
2509 off += dst_pos;
2510 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
2511 src_pos, n, off);
2512 off += n;
2513 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2514 dst_pos, size - dst_pos, off);
2515 off += size - dst_pos;
2516 } else if (t == src_type) {
2517 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2518 0, src_pos, off);
2519 off += src_pos;
2520 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2521 src_pos + n, size - src_pos - n, off);
2522 off += size - src_pos - n;
2523 } else {
2524 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2525 off += size;
2528 isl_dim_map_div(dim_map, bmap, off);
2530 res = isl_basic_map_alloc_dim(isl_basic_map_get_dim(bmap),
2531 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2532 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
2534 bmap->dim = isl_dim_move(bmap->dim, dst_type, dst_pos,
2535 src_type, src_pos, n);
2536 if (!bmap->dim)
2537 goto error;
2539 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2540 bmap = isl_basic_map_gauss(bmap, NULL);
2541 bmap = isl_basic_map_finalize(bmap);
2543 return bmap;
2544 error:
2545 isl_basic_map_free(bmap);
2546 return NULL;
2549 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
2550 enum isl_dim_type dst_type, unsigned dst_pos,
2551 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2553 return (isl_basic_set *)isl_basic_map_move_dims(
2554 (isl_basic_map *)bset, dst_type, dst_pos, src_type, src_pos, n);
2557 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
2558 enum isl_dim_type dst_type, unsigned dst_pos,
2559 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2561 if (!set)
2562 return NULL;
2563 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
2564 return (isl_set *)isl_map_move_dims((isl_map *)set, dst_type, dst_pos,
2565 src_type, src_pos, n);
2566 error:
2567 isl_set_free(set);
2568 return NULL;
2571 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
2572 enum isl_dim_type dst_type, unsigned dst_pos,
2573 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2575 int i;
2577 if (!map)
2578 return NULL;
2579 if (n == 0)
2580 return map;
2582 isl_assert(map->ctx, src_pos + n <= isl_map_dim(map, src_type),
2583 goto error);
2585 if (dst_type == src_type && dst_pos == src_pos)
2586 return map;
2588 isl_assert(map->ctx, dst_type != src_type, goto error);
2590 map = isl_map_cow(map);
2591 if (!map)
2592 return NULL;
2594 map->dim = isl_dim_move(map->dim, dst_type, dst_pos, src_type, src_pos, n);
2595 if (!map->dim)
2596 goto error;
2598 for (i = 0; i < map->n; ++i) {
2599 map->p[i] = isl_basic_map_move_dims(map->p[i],
2600 dst_type, dst_pos,
2601 src_type, src_pos, n);
2602 if (!map->p[i])
2603 goto error;
2606 return map;
2607 error:
2608 isl_map_free(map);
2609 return NULL;
2612 /* Move the specified dimensions to the last columns right before
2613 * the divs. Don't change the dimension specification of bmap.
2614 * That's the responsibility of the caller.
2616 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
2617 enum isl_dim_type type, unsigned first, unsigned n)
2619 struct isl_dim_map *dim_map;
2620 struct isl_basic_map *res;
2621 enum isl_dim_type t;
2622 unsigned total, off;
2624 if (!bmap)
2625 return NULL;
2626 if (pos(bmap->dim, type) + first + n == 1 + isl_dim_total(bmap->dim))
2627 return bmap;
2629 total = isl_basic_map_total_dim(bmap);
2630 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2632 off = 0;
2633 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2634 unsigned size = isl_dim_size(bmap->dim, t);
2635 if (t == type) {
2636 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2637 0, first, off);
2638 off += first;
2639 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2640 first, n, total - bmap->n_div - n);
2641 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2642 first + n, size - (first + n), off);
2643 off += size - (first + n);
2644 } else {
2645 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2646 off += size;
2649 isl_dim_map_div(dim_map, bmap, off + n);
2651 res = isl_basic_map_alloc_dim(isl_basic_map_get_dim(bmap),
2652 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2653 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
2654 return res;
2657 /* Turn the n dimensions of type type, starting at first
2658 * into existentially quantified variables.
2660 __isl_give isl_basic_map *isl_basic_map_project_out(
2661 __isl_take isl_basic_map *bmap,
2662 enum isl_dim_type type, unsigned first, unsigned n)
2664 int i;
2665 size_t row_size;
2666 isl_int **new_div;
2667 isl_int *old;
2669 if (n == 0)
2670 return bmap;
2672 if (!bmap)
2673 return NULL;
2675 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
2676 return isl_basic_map_remove_dims(bmap, type, first, n);
2678 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
2679 goto error);
2681 bmap = move_last(bmap, type, first, n);
2682 bmap = isl_basic_map_cow(bmap);
2683 if (!bmap)
2684 return NULL;
2686 row_size = 1 + isl_dim_total(bmap->dim) + bmap->extra;
2687 old = bmap->block2.data;
2688 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
2689 (bmap->extra + n) * (1 + row_size));
2690 if (!bmap->block2.data)
2691 goto error;
2692 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
2693 if (!new_div)
2694 goto error;
2695 for (i = 0; i < n; ++i) {
2696 new_div[i] = bmap->block2.data +
2697 (bmap->extra + i) * (1 + row_size);
2698 isl_seq_clr(new_div[i], 1 + row_size);
2700 for (i = 0; i < bmap->extra; ++i)
2701 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
2702 free(bmap->div);
2703 bmap->div = new_div;
2704 bmap->n_div += n;
2705 bmap->extra += n;
2707 bmap->dim = isl_dim_drop(bmap->dim, type, first, n);
2708 if (!bmap->dim)
2709 goto error;
2710 bmap = isl_basic_map_simplify(bmap);
2711 bmap = isl_basic_map_drop_redundant_divs(bmap);
2712 return isl_basic_map_finalize(bmap);
2713 error:
2714 isl_basic_map_free(bmap);
2715 return NULL;
2718 /* Turn the n dimensions of type type, starting at first
2719 * into existentially quantified variables.
2721 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
2722 enum isl_dim_type type, unsigned first, unsigned n)
2724 return (isl_basic_set *)isl_basic_map_project_out(
2725 (isl_basic_map *)bset, type, first, n);
2728 /* Turn the n dimensions of type type, starting at first
2729 * into existentially quantified variables.
2731 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
2732 enum isl_dim_type type, unsigned first, unsigned n)
2734 int i;
2736 if (!map)
2737 return NULL;
2739 if (n == 0)
2740 return map;
2742 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
2744 map = isl_map_cow(map);
2745 if (!map)
2746 return NULL;
2748 map->dim = isl_dim_drop(map->dim, type, first, n);
2749 if (!map->dim)
2750 goto error;
2752 for (i = 0; i < map->n; ++i) {
2753 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
2754 if (!map->p[i])
2755 goto error;
2758 return map;
2759 error:
2760 isl_map_free(map);
2761 return NULL;
2764 /* Turn the n dimensions of type type, starting at first
2765 * into existentially quantified variables.
2767 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
2768 enum isl_dim_type type, unsigned first, unsigned n)
2770 return (isl_set *)isl_map_project_out((isl_map *)set, type, first, n);
2773 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
2775 int i, j;
2777 for (i = 0; i < n; ++i) {
2778 j = isl_basic_map_alloc_div(bmap);
2779 if (j < 0)
2780 goto error;
2781 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
2783 return bmap;
2784 error:
2785 isl_basic_map_free(bmap);
2786 return NULL;
2789 struct isl_basic_map *isl_basic_map_apply_range(
2790 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2792 struct isl_dim *dim_result = NULL;
2793 struct isl_basic_map *bmap;
2794 unsigned n_in, n_out, n, nparam, total, pos;
2795 struct isl_dim_map *dim_map1, *dim_map2;
2797 if (!bmap1 || !bmap2)
2798 goto error;
2800 dim_result = isl_dim_join(isl_dim_copy(bmap1->dim),
2801 isl_dim_copy(bmap2->dim));
2803 n_in = isl_basic_map_n_in(bmap1);
2804 n_out = isl_basic_map_n_out(bmap2);
2805 n = isl_basic_map_n_out(bmap1);
2806 nparam = isl_basic_map_n_param(bmap1);
2808 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
2809 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
2810 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
2811 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
2812 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
2813 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
2814 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
2815 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
2816 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
2817 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
2818 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
2820 bmap = isl_basic_map_alloc_dim(dim_result,
2821 bmap1->n_div + bmap2->n_div + n,
2822 bmap1->n_eq + bmap2->n_eq,
2823 bmap1->n_ineq + bmap2->n_ineq);
2824 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
2825 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
2826 bmap = add_divs(bmap, n);
2827 bmap = isl_basic_map_simplify(bmap);
2828 bmap = isl_basic_map_drop_redundant_divs(bmap);
2829 return isl_basic_map_finalize(bmap);
2830 error:
2831 isl_basic_map_free(bmap1);
2832 isl_basic_map_free(bmap2);
2833 return NULL;
2836 struct isl_basic_set *isl_basic_set_apply(
2837 struct isl_basic_set *bset, struct isl_basic_map *bmap)
2839 if (!bset || !bmap)
2840 goto error;
2842 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
2843 goto error);
2845 return (struct isl_basic_set *)
2846 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
2847 error:
2848 isl_basic_set_free(bset);
2849 isl_basic_map_free(bmap);
2850 return NULL;
2853 struct isl_basic_map *isl_basic_map_apply_domain(
2854 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2856 if (!bmap1 || !bmap2)
2857 goto error;
2859 isl_assert(bmap1->ctx,
2860 isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
2861 isl_assert(bmap1->ctx,
2862 isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
2863 goto error);
2865 bmap1 = isl_basic_map_reverse(bmap1);
2866 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
2867 return isl_basic_map_reverse(bmap1);
2868 error:
2869 isl_basic_map_free(bmap1);
2870 isl_basic_map_free(bmap2);
2871 return NULL;
2874 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
2875 * A \cap B -> f(A) + f(B)
2877 struct isl_basic_map *isl_basic_map_sum(
2878 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2880 unsigned n_in, n_out, nparam, total, pos;
2881 struct isl_basic_map *bmap = NULL;
2882 struct isl_dim_map *dim_map1, *dim_map2;
2883 int i;
2885 if (!bmap1 || !bmap2)
2886 goto error;
2888 isl_assert(bmap1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim),
2889 goto error);
2891 nparam = isl_basic_map_n_param(bmap1);
2892 n_in = isl_basic_map_n_in(bmap1);
2893 n_out = isl_basic_map_n_out(bmap1);
2895 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
2896 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
2897 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
2898 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
2899 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
2900 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
2901 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
2902 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
2903 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
2904 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
2905 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
2907 bmap = isl_basic_map_alloc_dim(isl_dim_copy(bmap1->dim),
2908 bmap1->n_div + bmap2->n_div + 2 * n_out,
2909 bmap1->n_eq + bmap2->n_eq + n_out,
2910 bmap1->n_ineq + bmap2->n_ineq);
2911 for (i = 0; i < n_out; ++i) {
2912 int j = isl_basic_map_alloc_equality(bmap);
2913 if (j < 0)
2914 goto error;
2915 isl_seq_clr(bmap->eq[j], 1+total);
2916 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
2917 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
2918 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
2920 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
2921 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
2922 bmap = add_divs(bmap, 2 * n_out);
2924 bmap = isl_basic_map_simplify(bmap);
2925 return isl_basic_map_finalize(bmap);
2926 error:
2927 isl_basic_map_free(bmap);
2928 isl_basic_map_free(bmap1);
2929 isl_basic_map_free(bmap2);
2930 return NULL;
2933 /* Given two maps A -> f(A) and B -> g(B), construct a map
2934 * A \cap B -> f(A) + f(B)
2936 struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
2938 struct isl_map *result;
2939 int i, j;
2941 if (!map1 || !map2)
2942 goto error;
2944 isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
2946 result = isl_map_alloc_dim(isl_dim_copy(map1->dim),
2947 map1->n * map2->n, 0);
2948 if (!result)
2949 goto error;
2950 for (i = 0; i < map1->n; ++i)
2951 for (j = 0; j < map2->n; ++j) {
2952 struct isl_basic_map *part;
2953 part = isl_basic_map_sum(
2954 isl_basic_map_copy(map1->p[i]),
2955 isl_basic_map_copy(map2->p[j]));
2956 if (isl_basic_map_is_empty(part))
2957 isl_basic_map_free(part);
2958 else
2959 result = isl_map_add_basic_map(result, part);
2960 if (!result)
2961 goto error;
2963 isl_map_free(map1);
2964 isl_map_free(map2);
2965 return result;
2966 error:
2967 isl_map_free(map1);
2968 isl_map_free(map2);
2969 return NULL;
2972 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
2973 __isl_take isl_set *set2)
2975 return (isl_set *)isl_map_sum((isl_map *)set1, (isl_map *)set2);
2978 /* Given a basic map A -> f(A), construct A -> -f(A).
2980 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
2982 int i, j;
2983 unsigned off, n;
2985 bmap = isl_basic_map_cow(bmap);
2986 if (!bmap)
2987 return NULL;
2989 n = isl_basic_map_dim(bmap, isl_dim_out);
2990 off = isl_basic_map_offset(bmap, isl_dim_out);
2991 for (i = 0; i < bmap->n_eq; ++i)
2992 for (j = 0; j < n; ++j)
2993 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
2994 for (i = 0; i < bmap->n_ineq; ++i)
2995 for (j = 0; j < n; ++j)
2996 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
2997 for (i = 0; i < bmap->n_div; ++i)
2998 for (j = 0; j < n; ++j)
2999 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
3000 return isl_basic_map_finalize(bmap);
3003 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
3005 return isl_basic_map_neg(bset);
3008 /* Given a map A -> f(A), construct A -> -f(A).
3010 struct isl_map *isl_map_neg(struct isl_map *map)
3012 int i;
3014 map = isl_map_cow(map);
3015 if (!map)
3016 return NULL;
3018 for (i = 0; i < map->n; ++i) {
3019 map->p[i] = isl_basic_map_neg(map->p[i]);
3020 if (!map->p[i])
3021 goto error;
3024 return map;
3025 error:
3026 isl_map_free(map);
3027 return NULL;
3030 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
3032 return (isl_set *)isl_map_neg((isl_map *)set);
3035 /* Given a basic map A -> f(A) and an integer d, construct a basic map
3036 * A -> floor(f(A)/d).
3038 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
3039 isl_int d)
3041 unsigned n_in, n_out, nparam, total, pos;
3042 struct isl_basic_map *result = NULL;
3043 struct isl_dim_map *dim_map;
3044 int i;
3046 if (!bmap)
3047 return NULL;
3049 nparam = isl_basic_map_n_param(bmap);
3050 n_in = isl_basic_map_n_in(bmap);
3051 n_out = isl_basic_map_n_out(bmap);
3053 total = nparam + n_in + n_out + bmap->n_div + n_out;
3054 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3055 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
3056 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
3057 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
3058 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
3060 result = isl_basic_map_alloc_dim(isl_dim_copy(bmap->dim),
3061 bmap->n_div + n_out,
3062 bmap->n_eq, bmap->n_ineq + 2 * n_out);
3063 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
3064 result = add_divs(result, n_out);
3065 for (i = 0; i < n_out; ++i) {
3066 int j;
3067 j = isl_basic_map_alloc_inequality(result);
3068 if (j < 0)
3069 goto error;
3070 isl_seq_clr(result->ineq[j], 1+total);
3071 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
3072 isl_int_set_si(result->ineq[j][1+pos+i], 1);
3073 j = isl_basic_map_alloc_inequality(result);
3074 if (j < 0)
3075 goto error;
3076 isl_seq_clr(result->ineq[j], 1+total);
3077 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
3078 isl_int_set_si(result->ineq[j][1+pos+i], -1);
3079 isl_int_sub_ui(result->ineq[j][0], d, 1);
3082 result = isl_basic_map_simplify(result);
3083 return isl_basic_map_finalize(result);
3084 error:
3085 isl_basic_map_free(result);
3086 return NULL;
3089 /* Given a map A -> f(A) and an integer d, construct a map
3090 * A -> floor(f(A)/d).
3092 struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
3094 int i;
3096 map = isl_map_cow(map);
3097 if (!map)
3098 return NULL;
3100 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3101 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3102 for (i = 0; i < map->n; ++i) {
3103 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
3104 if (!map->p[i])
3105 goto error;
3108 return map;
3109 error:
3110 isl_map_free(map);
3111 return NULL;
3114 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
3116 int i;
3117 unsigned nparam;
3118 unsigned n_in;
3120 i = isl_basic_map_alloc_equality(bmap);
3121 if (i < 0)
3122 goto error;
3123 nparam = isl_basic_map_n_param(bmap);
3124 n_in = isl_basic_map_n_in(bmap);
3125 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
3126 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
3127 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
3128 return isl_basic_map_finalize(bmap);
3129 error:
3130 isl_basic_map_free(bmap);
3131 return NULL;
3134 /* Add a constraints to "bmap" expressing i_pos < o_pos
3136 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
3138 int i;
3139 unsigned nparam;
3140 unsigned n_in;
3142 i = isl_basic_map_alloc_inequality(bmap);
3143 if (i < 0)
3144 goto error;
3145 nparam = isl_basic_map_n_param(bmap);
3146 n_in = isl_basic_map_n_in(bmap);
3147 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3148 isl_int_set_si(bmap->ineq[i][0], -1);
3149 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
3150 isl_int_set_si(bmap->ineq[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 constraint to "bmap" expressing i_pos <= o_pos
3159 static __isl_give isl_basic_map *var_less_or_equal(
3160 __isl_take isl_basic_map *bmap, unsigned pos)
3162 int i;
3163 unsigned nparam;
3164 unsigned n_in;
3166 i = isl_basic_map_alloc_inequality(bmap);
3167 if (i < 0)
3168 goto error;
3169 nparam = isl_basic_map_n_param(bmap);
3170 n_in = isl_basic_map_n_in(bmap);
3171 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
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 constraints to "bmap" expressing i_pos > o_pos
3182 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
3184 int i;
3185 unsigned nparam;
3186 unsigned n_in;
3188 i = isl_basic_map_alloc_inequality(bmap);
3189 if (i < 0)
3190 goto error;
3191 nparam = isl_basic_map_n_param(bmap);
3192 n_in = isl_basic_map_n_in(bmap);
3193 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3194 isl_int_set_si(bmap->ineq[i][0], -1);
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 constraint to "bmap" expressing i_pos >= o_pos
3205 static __isl_give isl_basic_map *var_more_or_equal(
3206 __isl_take isl_basic_map *bmap, unsigned pos)
3208 int i;
3209 unsigned nparam;
3210 unsigned n_in;
3212 i = isl_basic_map_alloc_inequality(bmap);
3213 if (i < 0)
3214 goto error;
3215 nparam = isl_basic_map_n_param(bmap);
3216 n_in = isl_basic_map_n_in(bmap);
3217 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
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 struct isl_basic_map *isl_basic_map_equal(struct isl_dim *dim, unsigned n_equal)
3228 int i;
3229 struct isl_basic_map *bmap;
3230 bmap = isl_basic_map_alloc_dim(dim, 0, n_equal, 0);
3231 if (!bmap)
3232 return NULL;
3233 for (i = 0; i < n_equal && bmap; ++i)
3234 bmap = var_equal(bmap, i);
3235 return isl_basic_map_finalize(bmap);
3238 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
3240 struct isl_basic_map *isl_basic_map_less_at(struct isl_dim *dim, unsigned pos)
3242 int i;
3243 struct isl_basic_map *bmap;
3244 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
3245 if (!bmap)
3246 return NULL;
3247 for (i = 0; i < pos && bmap; ++i)
3248 bmap = var_equal(bmap, i);
3249 if (bmap)
3250 bmap = var_less(bmap, pos);
3251 return isl_basic_map_finalize(bmap);
3254 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
3256 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
3257 __isl_take isl_dim *dim, unsigned pos)
3259 int i;
3260 isl_basic_map *bmap;
3262 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
3263 for (i = 0; i < pos; ++i)
3264 bmap = var_equal(bmap, i);
3265 bmap = var_less_or_equal(bmap, pos);
3266 return isl_basic_map_finalize(bmap);
3269 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
3271 struct isl_basic_map *isl_basic_map_more_at(struct isl_dim *dim, unsigned pos)
3273 int i;
3274 struct isl_basic_map *bmap;
3275 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
3276 if (!bmap)
3277 return NULL;
3278 for (i = 0; i < pos && bmap; ++i)
3279 bmap = var_equal(bmap, i);
3280 if (bmap)
3281 bmap = var_more(bmap, pos);
3282 return isl_basic_map_finalize(bmap);
3285 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
3287 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
3288 __isl_take isl_dim *dim, unsigned pos)
3290 int i;
3291 isl_basic_map *bmap;
3293 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
3294 for (i = 0; i < pos; ++i)
3295 bmap = var_equal(bmap, i);
3296 bmap = var_more_or_equal(bmap, pos);
3297 return isl_basic_map_finalize(bmap);
3300 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_dim *dims,
3301 unsigned n, int equal)
3303 struct isl_map *map;
3304 int i;
3306 if (n == 0 && equal)
3307 return isl_map_universe(dims);
3309 map = isl_map_alloc_dim(isl_dim_copy(dims), n, ISL_MAP_DISJOINT);
3311 for (i = 0; i + 1 < n; ++i)
3312 map = isl_map_add_basic_map(map,
3313 isl_basic_map_less_at(isl_dim_copy(dims), i));
3314 if (n > 0) {
3315 if (equal)
3316 map = isl_map_add_basic_map(map,
3317 isl_basic_map_less_or_equal_at(dims, n - 1));
3318 else
3319 map = isl_map_add_basic_map(map,
3320 isl_basic_map_less_at(dims, n - 1));
3321 } else
3322 isl_dim_free(dims);
3324 return map;
3327 static __isl_give isl_map *map_lex_lte(__isl_take isl_dim *dims, int equal)
3329 if (!dims)
3330 return NULL;
3331 return map_lex_lte_first(dims, dims->n_out, equal);
3334 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_dim *dim, unsigned n)
3336 return map_lex_lte_first(dim, n, 0);
3339 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_dim *dim, unsigned n)
3341 return map_lex_lte_first(dim, n, 1);
3344 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_dim *set_dim)
3346 return map_lex_lte(isl_dim_map_from_set(set_dim), 0);
3349 __isl_give isl_map *isl_map_lex_le(__isl_take isl_dim *set_dim)
3351 return map_lex_lte(isl_dim_map_from_set(set_dim), 1);
3354 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_dim *dims,
3355 unsigned n, int equal)
3357 struct isl_map *map;
3358 int i;
3360 if (n == 0 && equal)
3361 return isl_map_universe(dims);
3363 map = isl_map_alloc_dim(isl_dim_copy(dims), n, ISL_MAP_DISJOINT);
3365 for (i = 0; i + 1 < n; ++i)
3366 map = isl_map_add_basic_map(map,
3367 isl_basic_map_more_at(isl_dim_copy(dims), i));
3368 if (n > 0) {
3369 if (equal)
3370 map = isl_map_add_basic_map(map,
3371 isl_basic_map_more_or_equal_at(dims, n - 1));
3372 else
3373 map = isl_map_add_basic_map(map,
3374 isl_basic_map_more_at(dims, n - 1));
3375 } else
3376 isl_dim_free(dims);
3378 return map;
3381 static __isl_give isl_map *map_lex_gte(__isl_take isl_dim *dims, int equal)
3383 if (!dims)
3384 return NULL;
3385 return map_lex_gte_first(dims, dims->n_out, equal);
3388 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_dim *dim, unsigned n)
3390 return map_lex_gte_first(dim, n, 0);
3393 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_dim *dim, unsigned n)
3395 return map_lex_gte_first(dim, n, 1);
3398 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_dim *set_dim)
3400 return map_lex_gte(isl_dim_map_from_set(set_dim), 0);
3403 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_dim *set_dim)
3405 return map_lex_gte(isl_dim_map_from_set(set_dim), 1);
3408 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
3409 __isl_take isl_set *set2)
3411 isl_map *map;
3412 map = isl_map_lex_le(isl_set_get_dim(set1));
3413 map = isl_map_intersect_domain(map, set1);
3414 map = isl_map_intersect_range(map, set2);
3415 return map;
3418 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
3419 __isl_take isl_set *set2)
3421 isl_map *map;
3422 map = isl_map_lex_lt(isl_set_get_dim(set1));
3423 map = isl_map_intersect_domain(map, set1);
3424 map = isl_map_intersect_range(map, set2);
3425 return map;
3428 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
3429 __isl_take isl_set *set2)
3431 isl_map *map;
3432 map = isl_map_lex_ge(isl_set_get_dim(set1));
3433 map = isl_map_intersect_domain(map, set1);
3434 map = isl_map_intersect_range(map, set2);
3435 return map;
3438 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
3439 __isl_take isl_set *set2)
3441 isl_map *map;
3442 map = isl_map_lex_gt(isl_set_get_dim(set1));
3443 map = isl_map_intersect_domain(map, set1);
3444 map = isl_map_intersect_range(map, set2);
3445 return map;
3448 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
3449 __isl_take isl_map *map2)
3451 isl_map *map;
3452 map = isl_map_lex_le(isl_dim_range(isl_map_get_dim(map1)));
3453 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3454 map = isl_map_apply_range(map, isl_map_reverse(map2));
3455 return map;
3458 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
3459 __isl_take isl_map *map2)
3461 isl_map *map;
3462 map = isl_map_lex_lt(isl_dim_range(isl_map_get_dim(map1)));
3463 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3464 map = isl_map_apply_range(map, isl_map_reverse(map2));
3465 return map;
3468 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
3469 __isl_take isl_map *map2)
3471 isl_map *map;
3472 map = isl_map_lex_ge(isl_dim_range(isl_map_get_dim(map1)));
3473 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3474 map = isl_map_apply_range(map, isl_map_reverse(map2));
3475 return map;
3478 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
3479 __isl_take isl_map *map2)
3481 isl_map *map;
3482 map = isl_map_lex_gt(isl_dim_range(isl_map_get_dim(map1)));
3483 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3484 map = isl_map_apply_range(map, isl_map_reverse(map2));
3485 return map;
3488 struct isl_basic_map *isl_basic_map_from_basic_set(
3489 struct isl_basic_set *bset, struct isl_dim *dim)
3491 struct isl_basic_map *bmap;
3493 bset = isl_basic_set_cow(bset);
3494 if (!bset || !dim)
3495 goto error;
3497 isl_assert(bset->ctx, isl_dim_compatible(bset->dim, dim), goto error);
3498 isl_dim_free(bset->dim);
3499 bmap = (struct isl_basic_map *) bset;
3500 bmap->dim = dim;
3501 return isl_basic_map_finalize(bmap);
3502 error:
3503 isl_basic_set_free(bset);
3504 isl_dim_free(dim);
3505 return NULL;
3508 struct isl_basic_set *isl_basic_set_from_basic_map(struct isl_basic_map *bmap)
3510 if (!bmap)
3511 goto error;
3512 if (bmap->dim->n_in == 0)
3513 return (struct isl_basic_set *)bmap;
3514 bmap = isl_basic_map_cow(bmap);
3515 if (!bmap)
3516 goto error;
3517 bmap->dim = isl_dim_as_set_dim(bmap->dim);
3518 if (!bmap->dim)
3519 goto error;
3520 bmap = isl_basic_map_finalize(bmap);
3521 return (struct isl_basic_set *)bmap;
3522 error:
3523 isl_basic_map_free(bmap);
3524 return NULL;
3527 /* For a div d = floor(f/m), add the constraints
3529 * f - m d >= 0
3530 * -(f-(n-1)) + m d >= 0
3532 * Note that the second constraint is the negation of
3534 * f - m d >= n
3536 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
3537 unsigned pos, isl_int *div)
3539 int i, j;
3540 unsigned total = isl_basic_map_total_dim(bmap);
3542 i = isl_basic_map_alloc_inequality(bmap);
3543 if (i < 0)
3544 return -1;
3545 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
3546 isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
3548 j = isl_basic_map_alloc_inequality(bmap);
3549 if (j < 0)
3550 return -1;
3551 isl_seq_neg(bmap->ineq[j], bmap->ineq[i], 1 + total);
3552 isl_int_add(bmap->ineq[j][0], bmap->ineq[j][0], bmap->ineq[j][1 + pos]);
3553 isl_int_sub_ui(bmap->ineq[j][0], bmap->ineq[j][0], 1);
3554 return j;
3557 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
3558 unsigned pos, isl_int *div)
3560 return isl_basic_map_add_div_constraints_var((isl_basic_map *)bset,
3561 pos, div);
3564 int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
3566 unsigned total = isl_basic_map_total_dim(bmap);
3567 unsigned div_pos = total - bmap->n_div + div;
3569 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
3570 bmap->div[div]);
3573 struct isl_basic_set *isl_basic_map_underlying_set(
3574 struct isl_basic_map *bmap)
3576 if (!bmap)
3577 goto error;
3578 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
3579 bmap->n_div == 0 &&
3580 !isl_dim_is_named_or_nested(bmap->dim, isl_dim_in) &&
3581 !isl_dim_is_named_or_nested(bmap->dim, isl_dim_out))
3582 return (struct isl_basic_set *)bmap;
3583 bmap = isl_basic_map_cow(bmap);
3584 if (!bmap)
3585 goto error;
3586 bmap->dim = isl_dim_underlying(bmap->dim, bmap->n_div);
3587 if (!bmap->dim)
3588 goto error;
3589 bmap->extra -= bmap->n_div;
3590 bmap->n_div = 0;
3591 bmap = isl_basic_map_finalize(bmap);
3592 return (struct isl_basic_set *)bmap;
3593 error:
3594 return NULL;
3597 __isl_give isl_basic_set *isl_basic_set_underlying_set(
3598 __isl_take isl_basic_set *bset)
3600 return isl_basic_map_underlying_set((isl_basic_map *)bset);
3603 struct isl_basic_map *isl_basic_map_overlying_set(
3604 struct isl_basic_set *bset, struct isl_basic_map *like)
3606 struct isl_basic_map *bmap;
3607 struct isl_ctx *ctx;
3608 unsigned total;
3609 int i;
3611 if (!bset || !like)
3612 goto error;
3613 ctx = bset->ctx;
3614 isl_assert(ctx, bset->n_div == 0, goto error);
3615 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
3616 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
3617 goto error);
3618 if (isl_dim_equal(bset->dim, like->dim) && like->n_div == 0) {
3619 isl_basic_map_free(like);
3620 return (struct isl_basic_map *)bset;
3622 bset = isl_basic_set_cow(bset);
3623 if (!bset)
3624 goto error;
3625 total = bset->dim->n_out + bset->extra;
3626 bmap = (struct isl_basic_map *)bset;
3627 isl_dim_free(bmap->dim);
3628 bmap->dim = isl_dim_copy(like->dim);
3629 if (!bmap->dim)
3630 goto error;
3631 bmap->n_div = like->n_div;
3632 bmap->extra += like->n_div;
3633 if (bmap->extra) {
3634 unsigned ltotal;
3635 isl_int **div;
3636 ltotal = total - bmap->extra + like->extra;
3637 if (ltotal > total)
3638 ltotal = total;
3639 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
3640 bmap->extra * (1 + 1 + total));
3641 if (isl_blk_is_error(bmap->block2))
3642 goto error;
3643 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
3644 if (!div)
3645 goto error;
3646 bmap->div = div;
3647 for (i = 0; i < bmap->extra; ++i)
3648 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
3649 for (i = 0; i < like->n_div; ++i) {
3650 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
3651 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
3653 bmap = isl_basic_map_extend_constraints(bmap,
3654 0, 2 * like->n_div);
3655 for (i = 0; i < like->n_div; ++i) {
3656 if (isl_int_is_zero(bmap->div[i][0]))
3657 continue;
3658 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
3659 goto error;
3662 isl_basic_map_free(like);
3663 bmap = isl_basic_map_simplify(bmap);
3664 bmap = isl_basic_map_finalize(bmap);
3665 return bmap;
3666 error:
3667 isl_basic_map_free(like);
3668 isl_basic_set_free(bset);
3669 return NULL;
3672 struct isl_basic_set *isl_basic_set_from_underlying_set(
3673 struct isl_basic_set *bset, struct isl_basic_set *like)
3675 return (struct isl_basic_set *)
3676 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
3679 struct isl_set *isl_set_from_underlying_set(
3680 struct isl_set *set, struct isl_basic_set *like)
3682 int i;
3684 if (!set || !like)
3685 goto error;
3686 isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
3687 goto error);
3688 if (isl_dim_equal(set->dim, like->dim) && like->n_div == 0) {
3689 isl_basic_set_free(like);
3690 return set;
3692 set = isl_set_cow(set);
3693 if (!set)
3694 goto error;
3695 for (i = 0; i < set->n; ++i) {
3696 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
3697 isl_basic_set_copy(like));
3698 if (!set->p[i])
3699 goto error;
3701 isl_dim_free(set->dim);
3702 set->dim = isl_dim_copy(like->dim);
3703 if (!set->dim)
3704 goto error;
3705 isl_basic_set_free(like);
3706 return set;
3707 error:
3708 isl_basic_set_free(like);
3709 isl_set_free(set);
3710 return NULL;
3713 struct isl_set *isl_map_underlying_set(struct isl_map *map)
3715 int i;
3717 map = isl_map_cow(map);
3718 if (!map)
3719 return NULL;
3720 map->dim = isl_dim_cow(map->dim);
3721 if (!map->dim)
3722 goto error;
3724 for (i = 1; i < map->n; ++i)
3725 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
3726 goto error);
3727 for (i = 0; i < map->n; ++i) {
3728 map->p[i] = (struct isl_basic_map *)
3729 isl_basic_map_underlying_set(map->p[i]);
3730 if (!map->p[i])
3731 goto error;
3733 if (map->n == 0)
3734 map->dim = isl_dim_underlying(map->dim, 0);
3735 else {
3736 isl_dim_free(map->dim);
3737 map->dim = isl_dim_copy(map->p[0]->dim);
3739 if (!map->dim)
3740 goto error;
3741 return (struct isl_set *)map;
3742 error:
3743 isl_map_free(map);
3744 return NULL;
3747 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
3749 return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
3752 __isl_give isl_basic_map *isl_basic_map_reset_dim(
3753 __isl_take isl_basic_map *bmap, __isl_take isl_dim *dim)
3755 bmap = isl_basic_map_cow(bmap);
3756 if (!bmap || !dim)
3757 goto error;
3759 isl_dim_free(bmap->dim);
3760 bmap->dim = dim;
3762 bmap = isl_basic_map_finalize(bmap);
3764 return bmap;
3765 error:
3766 isl_basic_map_free(bmap);
3767 isl_dim_free(dim);
3768 return NULL;
3771 __isl_give isl_basic_set *isl_basic_set_reset_dim(
3772 __isl_take isl_basic_set *bset, __isl_take isl_dim *dim)
3774 return (isl_basic_set *)isl_basic_map_reset_dim((isl_basic_map *)bset,
3775 dim);
3778 __isl_give isl_map *isl_map_reset_dim(__isl_take isl_map *map,
3779 __isl_take isl_dim *dim)
3781 int i;
3783 map = isl_map_cow(map);
3784 if (!map || !dim)
3785 goto error;
3787 for (i = 0; i < map->n; ++i) {
3788 map->p[i] = isl_basic_map_reset_dim(map->p[i],
3789 isl_dim_copy(dim));
3790 if (!map->p[i])
3791 goto error;
3793 isl_dim_free(map->dim);
3794 map->dim = dim;
3796 return map;
3797 error:
3798 isl_map_free(map);
3799 isl_dim_free(dim);
3800 return NULL;
3803 __isl_give isl_set *isl_set_reset_dim(__isl_take isl_set *set,
3804 __isl_take isl_dim *dim)
3806 return (struct isl_set *) isl_map_reset_dim((struct isl_map *)set, dim);
3809 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
3811 isl_dim *dim;
3812 struct isl_basic_set *domain;
3813 unsigned n_in;
3814 unsigned n_out;
3816 if (!bmap)
3817 return NULL;
3818 dim = isl_dim_domain(isl_basic_map_get_dim(bmap));
3820 n_in = isl_basic_map_n_in(bmap);
3821 n_out = isl_basic_map_n_out(bmap);
3822 domain = isl_basic_set_from_basic_map(bmap);
3823 domain = isl_basic_set_project_out(domain, isl_dim_set, n_in, n_out);
3825 domain = isl_basic_set_reset_dim(domain, dim);
3827 return domain;
3830 int isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
3832 if (!bmap)
3833 return -1;
3834 return isl_dim_may_be_set(bmap->dim);
3837 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
3839 if (!bmap)
3840 return NULL;
3841 if (isl_basic_map_may_be_set(bmap))
3842 return bmap;
3843 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
3846 __isl_give isl_basic_map *isl_basic_map_domain_map(
3847 __isl_take isl_basic_map *bmap)
3849 int i, k;
3850 isl_dim *dim;
3851 isl_basic_map *domain;
3852 int nparam, n_in, n_out;
3853 unsigned total;
3855 nparam = isl_basic_map_dim(bmap, isl_dim_param);
3856 n_in = isl_basic_map_dim(bmap, isl_dim_in);
3857 n_out = isl_basic_map_dim(bmap, isl_dim_out);
3859 dim = isl_dim_from_range(isl_dim_domain(isl_basic_map_get_dim(bmap)));
3860 domain = isl_basic_map_universe(dim);
3862 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
3863 bmap = isl_basic_map_apply_range(bmap, domain);
3864 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
3866 total = isl_basic_map_total_dim(bmap);
3868 for (i = 0; i < n_in; ++i) {
3869 k = isl_basic_map_alloc_equality(bmap);
3870 if (k < 0)
3871 goto error;
3872 isl_seq_clr(bmap->eq[k], 1 + total);
3873 isl_int_set_si(bmap->eq[k][1 + nparam + i], -1);
3874 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
3877 bmap = isl_basic_map_gauss(bmap, NULL);
3878 return isl_basic_map_finalize(bmap);
3879 error:
3880 isl_basic_map_free(bmap);
3881 return NULL;
3884 __isl_give isl_basic_map *isl_basic_map_range_map(
3885 __isl_take isl_basic_map *bmap)
3887 int i, k;
3888 isl_dim *dim;
3889 isl_basic_map *range;
3890 int nparam, n_in, n_out;
3891 unsigned total;
3893 nparam = isl_basic_map_dim(bmap, isl_dim_param);
3894 n_in = isl_basic_map_dim(bmap, isl_dim_in);
3895 n_out = isl_basic_map_dim(bmap, isl_dim_out);
3897 dim = isl_dim_from_range(isl_dim_range(isl_basic_map_get_dim(bmap)));
3898 range = isl_basic_map_universe(dim);
3900 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
3901 bmap = isl_basic_map_apply_range(bmap, range);
3902 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
3904 total = isl_basic_map_total_dim(bmap);
3906 for (i = 0; i < n_out; ++i) {
3907 k = isl_basic_map_alloc_equality(bmap);
3908 if (k < 0)
3909 goto error;
3910 isl_seq_clr(bmap->eq[k], 1 + total);
3911 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + i], -1);
3912 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
3915 bmap = isl_basic_map_gauss(bmap, NULL);
3916 return isl_basic_map_finalize(bmap);
3917 error:
3918 isl_basic_map_free(bmap);
3919 return NULL;
3922 int isl_map_may_be_set(__isl_keep isl_map *map)
3924 if (!map)
3925 return -1;
3926 return isl_dim_may_be_set(map->dim);
3929 struct isl_set *isl_map_range(struct isl_map *map)
3931 int i;
3932 struct isl_set *set;
3934 if (!map)
3935 goto error;
3936 if (isl_map_may_be_set(map))
3937 return (isl_set *)map;
3939 map = isl_map_cow(map);
3940 if (!map)
3941 goto error;
3943 set = (struct isl_set *) map;
3944 set->dim = isl_dim_drop_inputs(set->dim, 0, set->dim->n_in);
3945 if (!set->dim)
3946 goto error;
3947 for (i = 0; i < map->n; ++i) {
3948 set->p[i] = isl_basic_map_range(map->p[i]);
3949 if (!set->p[i])
3950 goto error;
3952 ISL_F_CLR(set, ISL_MAP_DISJOINT);
3953 ISL_F_CLR(set, ISL_SET_NORMALIZED);
3954 return set;
3955 error:
3956 isl_map_free(map);
3957 return NULL;
3960 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
3962 int i;
3963 isl_dim *domain_dim;
3965 map = isl_map_cow(map);
3966 if (!map)
3967 return NULL;
3969 domain_dim = isl_dim_from_range(isl_dim_domain(isl_map_get_dim(map)));
3970 map->dim = isl_dim_from_domain(isl_dim_wrap(map->dim));
3971 map->dim = isl_dim_join(map->dim, domain_dim);
3972 if (!map->dim)
3973 goto error;
3974 for (i = 0; i < map->n; ++i) {
3975 map->p[i] = isl_basic_map_domain_map(map->p[i]);
3976 if (!map->p[i])
3977 goto error;
3979 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3980 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3981 return map;
3982 error:
3983 isl_map_free(map);
3984 return NULL;
3987 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
3989 int i;
3990 isl_dim *range_dim;
3992 map = isl_map_cow(map);
3993 if (!map)
3994 return NULL;
3996 range_dim = isl_dim_range(isl_map_get_dim(map));
3997 map->dim = isl_dim_from_domain(isl_dim_wrap(map->dim));
3998 map->dim = isl_dim_join(map->dim, range_dim);
3999 if (!map->dim)
4000 goto error;
4001 for (i = 0; i < map->n; ++i) {
4002 map->p[i] = isl_basic_map_range_map(map->p[i]);
4003 if (!map->p[i])
4004 goto error;
4006 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4007 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4008 return map;
4009 error:
4010 isl_map_free(map);
4011 return NULL;
4014 struct isl_map *isl_map_from_set(struct isl_set *set, struct isl_dim *dim)
4016 int i;
4017 struct isl_map *map = NULL;
4019 set = isl_set_cow(set);
4020 if (!set || !dim)
4021 goto error;
4022 isl_assert(set->ctx, isl_dim_compatible(set->dim, dim), goto error);
4023 map = (struct isl_map *)set;
4024 for (i = 0; i < set->n; ++i) {
4025 map->p[i] = isl_basic_map_from_basic_set(
4026 set->p[i], isl_dim_copy(dim));
4027 if (!map->p[i])
4028 goto error;
4030 isl_dim_free(map->dim);
4031 map->dim = dim;
4032 return map;
4033 error:
4034 isl_dim_free(dim);
4035 isl_set_free(set);
4036 return NULL;
4039 __isl_give isl_basic_map *isl_basic_map_from_domain(
4040 __isl_take isl_basic_set *bset)
4042 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
4045 __isl_give isl_basic_map *isl_basic_map_from_range(
4046 __isl_take isl_basic_set *bset)
4048 return (isl_basic_map *)bset;
4051 struct isl_map *isl_map_from_range(struct isl_set *set)
4053 return (struct isl_map *)set;
4056 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
4058 return isl_map_reverse(isl_map_from_range(set));
4061 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
4062 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
4064 return isl_basic_map_apply_range(isl_basic_map_from_domain(domain),
4065 isl_basic_map_from_range(range));
4068 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
4069 __isl_take isl_set *range)
4071 return isl_map_apply_range(isl_map_from_domain(domain),
4072 isl_map_from_range(range));
4075 struct isl_set *isl_set_from_map(struct isl_map *map)
4077 int i;
4078 struct isl_set *set = NULL;
4080 if (!map)
4081 return NULL;
4082 map = isl_map_cow(map);
4083 if (!map)
4084 return NULL;
4085 map->dim = isl_dim_as_set_dim(map->dim);
4086 if (!map->dim)
4087 goto error;
4088 set = (struct isl_set *)map;
4089 for (i = 0; i < map->n; ++i) {
4090 set->p[i] = isl_basic_set_from_basic_map(map->p[i]);
4091 if (!set->p[i])
4092 goto error;
4094 return set;
4095 error:
4096 isl_map_free(map);
4097 return NULL;
4100 struct isl_map *isl_map_alloc_dim(struct isl_dim *dim, int n, unsigned flags)
4102 struct isl_map *map;
4104 if (!dim)
4105 return NULL;
4106 isl_assert(dim->ctx, n >= 0, return NULL);
4107 map = isl_alloc(dim->ctx, struct isl_map,
4108 sizeof(struct isl_map) +
4109 (n - 1) * sizeof(struct isl_basic_map *));
4110 if (!map)
4111 goto error;
4113 map->ctx = dim->ctx;
4114 isl_ctx_ref(map->ctx);
4115 map->ref = 1;
4116 map->size = n;
4117 map->n = 0;
4118 map->dim = dim;
4119 map->flags = flags;
4120 return map;
4121 error:
4122 isl_dim_free(dim);
4123 return NULL;
4126 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
4127 unsigned nparam, unsigned in, unsigned out, int n,
4128 unsigned flags)
4130 struct isl_map *map;
4131 struct isl_dim *dims;
4133 dims = isl_dim_alloc(ctx, nparam, in, out);
4134 if (!dims)
4135 return NULL;
4137 map = isl_map_alloc_dim(dims, n, flags);
4138 return map;
4141 struct isl_basic_map *isl_basic_map_empty(struct isl_dim *dim)
4143 struct isl_basic_map *bmap;
4144 bmap = isl_basic_map_alloc_dim(dim, 0, 1, 0);
4145 bmap = isl_basic_map_set_to_empty(bmap);
4146 return bmap;
4149 struct isl_basic_set *isl_basic_set_empty(struct isl_dim *dim)
4151 struct isl_basic_set *bset;
4152 bset = isl_basic_set_alloc_dim(dim, 0, 1, 0);
4153 bset = isl_basic_set_set_to_empty(bset);
4154 return bset;
4157 struct isl_basic_map *isl_basic_map_empty_like(struct isl_basic_map *model)
4159 struct isl_basic_map *bmap;
4160 if (!model)
4161 return NULL;
4162 bmap = isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
4163 bmap = isl_basic_map_set_to_empty(bmap);
4164 return bmap;
4167 struct isl_basic_map *isl_basic_map_empty_like_map(struct isl_map *model)
4169 struct isl_basic_map *bmap;
4170 if (!model)
4171 return NULL;
4172 bmap = isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
4173 bmap = isl_basic_map_set_to_empty(bmap);
4174 return bmap;
4177 struct isl_basic_set *isl_basic_set_empty_like(struct isl_basic_set *model)
4179 struct isl_basic_set *bset;
4180 if (!model)
4181 return NULL;
4182 bset = isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
4183 bset = isl_basic_set_set_to_empty(bset);
4184 return bset;
4187 struct isl_basic_map *isl_basic_map_universe(struct isl_dim *dim)
4189 struct isl_basic_map *bmap;
4190 bmap = isl_basic_map_alloc_dim(dim, 0, 0, 0);
4191 bmap = isl_basic_map_finalize(bmap);
4192 return bmap;
4195 struct isl_basic_set *isl_basic_set_universe(struct isl_dim *dim)
4197 struct isl_basic_set *bset;
4198 bset = isl_basic_set_alloc_dim(dim, 0, 0, 0);
4199 bset = isl_basic_set_finalize(bset);
4200 return bset;
4203 __isl_give isl_basic_map *isl_basic_map_nat_universe(__isl_take isl_dim *dim)
4205 int i;
4206 unsigned total = isl_dim_total(dim);
4207 isl_basic_map *bmap;
4209 bmap= isl_basic_map_alloc_dim(dim, 0, 0, total);
4210 for (i = 0; i < total; ++i) {
4211 int k = isl_basic_map_alloc_inequality(bmap);
4212 if (k < 0)
4213 goto error;
4214 isl_seq_clr(bmap->ineq[k], 1 + total);
4215 isl_int_set_si(bmap->ineq[k][1 + i], 1);
4217 return bmap;
4218 error:
4219 isl_basic_map_free(bmap);
4220 return NULL;
4223 __isl_give isl_basic_set *isl_basic_set_nat_universe(__isl_take isl_dim *dim)
4225 return isl_basic_map_nat_universe(dim);
4228 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_dim *dim)
4230 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
4233 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_dim *dim)
4235 return isl_map_nat_universe(dim);
4238 __isl_give isl_basic_map *isl_basic_map_universe_like(
4239 __isl_keep isl_basic_map *model)
4241 if (!model)
4242 return NULL;
4243 return isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
4246 struct isl_basic_set *isl_basic_set_universe_like(struct isl_basic_set *model)
4248 if (!model)
4249 return NULL;
4250 return isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
4253 __isl_give isl_basic_set *isl_basic_set_universe_like_set(
4254 __isl_keep isl_set *model)
4256 if (!model)
4257 return NULL;
4258 return isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
4261 struct isl_map *isl_map_empty(struct isl_dim *dim)
4263 return isl_map_alloc_dim(dim, 0, ISL_MAP_DISJOINT);
4266 struct isl_map *isl_map_empty_like(struct isl_map *model)
4268 if (!model)
4269 return NULL;
4270 return isl_map_alloc_dim(isl_dim_copy(model->dim), 0, ISL_MAP_DISJOINT);
4273 struct isl_map *isl_map_empty_like_basic_map(struct isl_basic_map *model)
4275 if (!model)
4276 return NULL;
4277 return isl_map_alloc_dim(isl_dim_copy(model->dim), 0, ISL_MAP_DISJOINT);
4280 struct isl_set *isl_set_empty(struct isl_dim *dim)
4282 return isl_set_alloc_dim(dim, 0, ISL_MAP_DISJOINT);
4285 struct isl_set *isl_set_empty_like(struct isl_set *model)
4287 if (!model)
4288 return NULL;
4289 return isl_set_empty(isl_dim_copy(model->dim));
4292 struct isl_map *isl_map_universe(struct isl_dim *dim)
4294 struct isl_map *map;
4295 if (!dim)
4296 return NULL;
4297 map = isl_map_alloc_dim(isl_dim_copy(dim), 1, ISL_MAP_DISJOINT);
4298 map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
4299 return map;
4302 struct isl_set *isl_set_universe(struct isl_dim *dim)
4304 struct isl_set *set;
4305 if (!dim)
4306 return NULL;
4307 set = isl_set_alloc_dim(isl_dim_copy(dim), 1, ISL_MAP_DISJOINT);
4308 set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
4309 return set;
4312 __isl_give isl_set *isl_set_universe_like(__isl_keep isl_set *model)
4314 if (!model)
4315 return NULL;
4316 return isl_set_universe(isl_dim_copy(model->dim));
4319 struct isl_map *isl_map_dup(struct isl_map *map)
4321 int i;
4322 struct isl_map *dup;
4324 if (!map)
4325 return NULL;
4326 dup = isl_map_alloc_dim(isl_dim_copy(map->dim), map->n, map->flags);
4327 for (i = 0; i < map->n; ++i)
4328 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
4329 return dup;
4332 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
4333 __isl_take isl_basic_map *bmap)
4335 if (!bmap || !map)
4336 goto error;
4337 if (isl_basic_map_plain_is_empty(bmap)) {
4338 isl_basic_map_free(bmap);
4339 return map;
4341 isl_assert(map->ctx, isl_dim_equal(map->dim, bmap->dim), goto error);
4342 isl_assert(map->ctx, map->n < map->size, goto error);
4343 map->p[map->n] = bmap;
4344 map->n++;
4345 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4346 return map;
4347 error:
4348 if (map)
4349 isl_map_free(map);
4350 if (bmap)
4351 isl_basic_map_free(bmap);
4352 return NULL;
4355 void isl_map_free(struct isl_map *map)
4357 int i;
4359 if (!map)
4360 return;
4362 if (--map->ref > 0)
4363 return;
4365 isl_ctx_deref(map->ctx);
4366 for (i = 0; i < map->n; ++i)
4367 isl_basic_map_free(map->p[i]);
4368 isl_dim_free(map->dim);
4369 free(map);
4372 struct isl_map *isl_map_extend(struct isl_map *base,
4373 unsigned nparam, unsigned n_in, unsigned n_out)
4375 int i;
4377 base = isl_map_cow(base);
4378 if (!base)
4379 return NULL;
4381 base->dim = isl_dim_extend(base->dim, nparam, n_in, n_out);
4382 if (!base->dim)
4383 goto error;
4384 for (i = 0; i < base->n; ++i) {
4385 base->p[i] = isl_basic_map_extend_dim(base->p[i],
4386 isl_dim_copy(base->dim), 0, 0, 0);
4387 if (!base->p[i])
4388 goto error;
4390 return base;
4391 error:
4392 isl_map_free(base);
4393 return NULL;
4396 struct isl_set *isl_set_extend(struct isl_set *base,
4397 unsigned nparam, unsigned dim)
4399 return (struct isl_set *)isl_map_extend((struct isl_map *)base,
4400 nparam, 0, dim);
4403 static struct isl_basic_map *isl_basic_map_fix_pos_si(
4404 struct isl_basic_map *bmap, unsigned pos, int value)
4406 int j;
4408 bmap = isl_basic_map_cow(bmap);
4409 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
4410 j = isl_basic_map_alloc_equality(bmap);
4411 if (j < 0)
4412 goto error;
4413 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
4414 isl_int_set_si(bmap->eq[j][pos], -1);
4415 isl_int_set_si(bmap->eq[j][0], value);
4416 bmap = isl_basic_map_simplify(bmap);
4417 return isl_basic_map_finalize(bmap);
4418 error:
4419 isl_basic_map_free(bmap);
4420 return NULL;
4423 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
4424 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
4426 int j;
4428 bmap = isl_basic_map_cow(bmap);
4429 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
4430 j = isl_basic_map_alloc_equality(bmap);
4431 if (j < 0)
4432 goto error;
4433 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
4434 isl_int_set_si(bmap->eq[j][pos], -1);
4435 isl_int_set(bmap->eq[j][0], value);
4436 bmap = isl_basic_map_simplify(bmap);
4437 return isl_basic_map_finalize(bmap);
4438 error:
4439 isl_basic_map_free(bmap);
4440 return NULL;
4443 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
4444 enum isl_dim_type type, unsigned pos, int value)
4446 if (!bmap)
4447 return NULL;
4448 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
4449 return isl_basic_map_fix_pos_si(bmap,
4450 isl_basic_map_offset(bmap, type) + pos, value);
4451 error:
4452 isl_basic_map_free(bmap);
4453 return NULL;
4456 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
4457 enum isl_dim_type type, unsigned pos, isl_int value)
4459 if (!bmap)
4460 return NULL;
4461 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
4462 return isl_basic_map_fix_pos(bmap,
4463 isl_basic_map_offset(bmap, type) + pos, value);
4464 error:
4465 isl_basic_map_free(bmap);
4466 return NULL;
4469 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
4470 enum isl_dim_type type, unsigned pos, int value)
4472 return (struct isl_basic_set *)
4473 isl_basic_map_fix_si((struct isl_basic_map *)bset,
4474 type, pos, value);
4477 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
4478 enum isl_dim_type type, unsigned pos, isl_int value)
4480 return (struct isl_basic_set *)
4481 isl_basic_map_fix((struct isl_basic_map *)bset,
4482 type, pos, value);
4485 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
4486 unsigned input, int value)
4488 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
4491 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
4492 unsigned dim, int value)
4494 return (struct isl_basic_set *)
4495 isl_basic_map_fix_si((struct isl_basic_map *)bset,
4496 isl_dim_set, dim, value);
4499 struct isl_map *isl_map_fix_si(struct isl_map *map,
4500 enum isl_dim_type type, unsigned pos, int value)
4502 int i;
4504 map = isl_map_cow(map);
4505 if (!map)
4506 return NULL;
4508 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
4509 for (i = 0; i < map->n; ++i) {
4510 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
4511 if (!map->p[i])
4512 goto error;
4514 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4515 return map;
4516 error:
4517 isl_map_free(map);
4518 return NULL;
4521 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
4522 enum isl_dim_type type, unsigned pos, int value)
4524 return (struct isl_set *)
4525 isl_map_fix_si((struct isl_map *)set, type, pos, value);
4528 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
4529 enum isl_dim_type type, unsigned pos, isl_int value)
4531 int i;
4533 map = isl_map_cow(map);
4534 if (!map)
4535 return NULL;
4537 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
4538 for (i = 0; i < map->n; ++i) {
4539 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
4540 if (!map->p[i])
4541 goto error;
4543 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4544 return map;
4545 error:
4546 isl_map_free(map);
4547 return NULL;
4550 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
4551 enum isl_dim_type type, unsigned pos, isl_int value)
4553 return (struct isl_set *)isl_map_fix((isl_map *)set, type, pos, value);
4556 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
4557 unsigned input, int value)
4559 return isl_map_fix_si(map, isl_dim_in, input, value);
4562 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
4564 return (struct isl_set *)
4565 isl_map_fix_si((struct isl_map *)set, isl_dim_set, dim, value);
4568 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
4569 __isl_take isl_basic_map *bmap,
4570 enum isl_dim_type type, unsigned pos, int value)
4572 int j;
4574 if (!bmap)
4575 return NULL;
4576 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
4577 pos += isl_basic_map_offset(bmap, type);
4578 bmap = isl_basic_map_cow(bmap);
4579 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
4580 j = isl_basic_map_alloc_inequality(bmap);
4581 if (j < 0)
4582 goto error;
4583 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
4584 isl_int_set_si(bmap->ineq[j][pos], 1);
4585 isl_int_set_si(bmap->ineq[j][0], -value);
4586 bmap = isl_basic_map_simplify(bmap);
4587 return isl_basic_map_finalize(bmap);
4588 error:
4589 isl_basic_map_free(bmap);
4590 return NULL;
4593 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
4594 unsigned dim, isl_int value)
4596 int j;
4598 bset = isl_basic_set_cow(bset);
4599 bset = isl_basic_set_extend_constraints(bset, 0, 1);
4600 j = isl_basic_set_alloc_inequality(bset);
4601 if (j < 0)
4602 goto error;
4603 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
4604 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
4605 isl_int_neg(bset->ineq[j][0], value);
4606 bset = isl_basic_set_simplify(bset);
4607 return isl_basic_set_finalize(bset);
4608 error:
4609 isl_basic_set_free(bset);
4610 return NULL;
4613 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
4614 enum isl_dim_type type, unsigned pos, int value)
4616 int i;
4618 map = isl_map_cow(map);
4619 if (!map)
4620 return NULL;
4622 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
4623 for (i = 0; i < map->n; ++i) {
4624 map->p[i] = isl_basic_map_lower_bound_si(map->p[i],
4625 type, pos, value);
4626 if (!map->p[i])
4627 goto error;
4629 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4630 return map;
4631 error:
4632 isl_map_free(map);
4633 return NULL;
4636 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
4637 enum isl_dim_type type, unsigned pos, int value)
4639 return (struct isl_set *)
4640 isl_map_lower_bound_si((struct isl_map *)set, type, pos, value);
4643 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
4644 isl_int value)
4646 int i;
4648 set = isl_set_cow(set);
4649 if (!set)
4650 return NULL;
4652 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
4653 for (i = 0; i < set->n; ++i) {
4654 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
4655 if (!set->p[i])
4656 goto error;
4658 return set;
4659 error:
4660 isl_set_free(set);
4661 return NULL;
4664 struct isl_map *isl_map_reverse(struct isl_map *map)
4666 int i;
4668 map = isl_map_cow(map);
4669 if (!map)
4670 return NULL;
4672 map->dim = isl_dim_reverse(map->dim);
4673 if (!map->dim)
4674 goto error;
4675 for (i = 0; i < map->n; ++i) {
4676 map->p[i] = isl_basic_map_reverse(map->p[i]);
4677 if (!map->p[i])
4678 goto error;
4680 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4681 return map;
4682 error:
4683 isl_map_free(map);
4684 return NULL;
4687 static struct isl_map *isl_basic_map_partial_lexopt(
4688 struct isl_basic_map *bmap, struct isl_basic_set *dom,
4689 struct isl_set **empty, int max)
4691 if (!bmap)
4692 goto error;
4693 if (bmap->ctx->opt->pip == ISL_PIP_PIP)
4694 return isl_pip_basic_map_lexopt(bmap, dom, empty, max);
4695 else
4696 return isl_tab_basic_map_partial_lexopt(bmap, dom, empty, max);
4697 error:
4698 isl_basic_map_free(bmap);
4699 isl_basic_set_free(dom);
4700 if (empty)
4701 *empty = NULL;
4702 return NULL;
4705 struct isl_map *isl_basic_map_partial_lexmax(
4706 struct isl_basic_map *bmap, struct isl_basic_set *dom,
4707 struct isl_set **empty)
4709 return isl_basic_map_partial_lexopt(bmap, dom, empty, 1);
4712 struct isl_map *isl_basic_map_partial_lexmin(
4713 struct isl_basic_map *bmap, struct isl_basic_set *dom,
4714 struct isl_set **empty)
4716 return isl_basic_map_partial_lexopt(bmap, dom, empty, 0);
4719 struct isl_set *isl_basic_set_partial_lexmin(
4720 struct isl_basic_set *bset, struct isl_basic_set *dom,
4721 struct isl_set **empty)
4723 return (struct isl_set *)
4724 isl_basic_map_partial_lexmin((struct isl_basic_map *)bset,
4725 dom, empty);
4728 struct isl_set *isl_basic_set_partial_lexmax(
4729 struct isl_basic_set *bset, struct isl_basic_set *dom,
4730 struct isl_set **empty)
4732 return (struct isl_set *)
4733 isl_basic_map_partial_lexmax((struct isl_basic_map *)bset,
4734 dom, empty);
4737 /* Given a basic map "bmap", compute the lexicographically minimal
4738 * (or maximal) image element for each domain element in dom.
4739 * Set *empty to those elements in dom that do not have an image element.
4741 * We first make sure the basic sets in dom are disjoint and then
4742 * simply collect the results over each of the basic sets separately.
4743 * We could probably improve the efficiency a bit by moving the union
4744 * domain down into the parametric integer programming.
4746 static __isl_give isl_map *basic_map_partial_lexopt(
4747 __isl_take isl_basic_map *bmap, __isl_take isl_set *dom,
4748 __isl_give isl_set **empty, int max)
4750 int i;
4751 struct isl_map *res;
4753 dom = isl_set_make_disjoint(dom);
4754 if (!dom)
4755 goto error;
4757 if (isl_set_plain_is_empty(dom)) {
4758 res = isl_map_empty_like_basic_map(bmap);
4759 *empty = isl_set_empty_like(dom);
4760 isl_set_free(dom);
4761 isl_basic_map_free(bmap);
4762 return res;
4765 res = isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap),
4766 isl_basic_set_copy(dom->p[0]), empty, max);
4768 for (i = 1; i < dom->n; ++i) {
4769 struct isl_map *res_i;
4770 struct isl_set *empty_i;
4772 res_i = isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap),
4773 isl_basic_set_copy(dom->p[i]), &empty_i, max);
4775 res = isl_map_union_disjoint(res, res_i);
4776 *empty = isl_set_union_disjoint(*empty, empty_i);
4779 isl_set_free(dom);
4780 isl_basic_map_free(bmap);
4781 return res;
4782 error:
4783 *empty = NULL;
4784 isl_set_free(dom);
4785 isl_basic_map_free(bmap);
4786 return NULL;
4789 /* Given a map "map", compute the lexicographically minimal
4790 * (or maximal) image element for each domain element in dom.
4791 * Set *empty to those elements in dom that do not have an image element.
4793 * We first compute the lexicographically minimal or maximal element
4794 * in the first basic map. This results in a partial solution "res"
4795 * and a subset "todo" of dom that still need to be handled.
4796 * We then consider each of the remaining maps in "map" and successively
4797 * improve both "res" and "todo".
4799 * Let res^k and todo^k be the results after k steps and let i = k + 1.
4800 * Assume we are computing the lexicographical maximum.
4801 * We first compute the lexicographically maximal element in basic map i.
4802 * This results in a partial solution res_i and a subset todo_i.
4803 * Then we combine these results with those obtain for the first k basic maps
4804 * to obtain a result that is valid for the first k+1 basic maps.
4805 * In particular, the set where there is no solution is the set where
4806 * there is no solution for the first k basic maps and also no solution
4807 * for the ith basic map, i.e.,
4809 * todo^i = todo^k * todo_i
4811 * On dom(res^k) * dom(res_i), we need to pick the larger of the two
4812 * solutions, arbitrarily breaking ties in favor of res^k.
4813 * That is, when res^k(a) >= res_i(a), we pick res^k and
4814 * when res^k(a) < res_i(a), we pick res_i. (Here, ">=" and "<" denote
4815 * the lexicographic order.)
4816 * In practice, we compute
4818 * res^k * (res_i . "<=")
4820 * and
4822 * res_i * (res^k . "<")
4824 * Finally, we consider the symmetric difference of dom(res^k) and dom(res_i),
4825 * where only one of res^k and res_i provides a solution and we simply pick
4826 * that one, i.e.,
4828 * res^k * todo_i
4829 * and
4830 * res_i * todo^k
4832 * Note that we only compute these intersections when dom(res^k) intersects
4833 * dom(res_i). Otherwise, the only effect of these intersections is to
4834 * potentially break up res^k and res_i into smaller pieces.
4835 * We want to avoid such splintering as much as possible.
4836 * In fact, an earlier implementation of this function would look for
4837 * better results in the domain of res^k and for extra results in todo^k,
4838 * but this would always result in a splintering according to todo^k,
4839 * even when the domain of basic map i is disjoint from the domains of
4840 * the previous basic maps.
4842 static __isl_give isl_map *isl_map_partial_lexopt(
4843 __isl_take isl_map *map, __isl_take isl_set *dom,
4844 __isl_give isl_set **empty, int max)
4846 int i;
4847 struct isl_map *res;
4848 struct isl_set *todo;
4850 if (!map || !dom)
4851 goto error;
4853 if (isl_map_plain_is_empty(map)) {
4854 if (empty)
4855 *empty = dom;
4856 else
4857 isl_set_free(dom);
4858 return map;
4861 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
4862 isl_set_copy(dom), &todo, max);
4864 for (i = 1; i < map->n; ++i) {
4865 isl_map *lt, *le;
4866 isl_map *res_i;
4867 isl_set *todo_i;
4868 isl_dim *dim = isl_dim_range(isl_map_get_dim(res));
4870 res_i = basic_map_partial_lexopt(isl_basic_map_copy(map->p[i]),
4871 isl_set_copy(dom), &todo_i, max);
4873 if (max) {
4874 lt = isl_map_lex_lt(isl_dim_copy(dim));
4875 le = isl_map_lex_le(dim);
4876 } else {
4877 lt = isl_map_lex_gt(isl_dim_copy(dim));
4878 le = isl_map_lex_ge(dim);
4880 lt = isl_map_apply_range(isl_map_copy(res), lt);
4881 lt = isl_map_intersect(lt, isl_map_copy(res_i));
4882 le = isl_map_apply_range(isl_map_copy(res_i), le);
4883 le = isl_map_intersect(le, isl_map_copy(res));
4885 if (!isl_map_is_empty(lt) || !isl_map_is_empty(le)) {
4886 res = isl_map_intersect_domain(res,
4887 isl_set_copy(todo_i));
4888 res_i = isl_map_intersect_domain(res_i,
4889 isl_set_copy(todo));
4892 res = isl_map_union_disjoint(res, res_i);
4893 res = isl_map_union_disjoint(res, lt);
4894 res = isl_map_union_disjoint(res, le);
4896 todo = isl_set_intersect(todo, todo_i);
4899 isl_set_free(dom);
4900 isl_map_free(map);
4902 if (empty)
4903 *empty = todo;
4904 else
4905 isl_set_free(todo);
4907 return res;
4908 error:
4909 if (empty)
4910 *empty = NULL;
4911 isl_set_free(dom);
4912 isl_map_free(map);
4913 return NULL;
4916 __isl_give isl_map *isl_map_partial_lexmax(
4917 __isl_take isl_map *map, __isl_take isl_set *dom,
4918 __isl_give isl_set **empty)
4920 return isl_map_partial_lexopt(map, dom, empty, 1);
4923 __isl_give isl_map *isl_map_partial_lexmin(
4924 __isl_take isl_map *map, __isl_take isl_set *dom,
4925 __isl_give isl_set **empty)
4927 return isl_map_partial_lexopt(map, dom, empty, 0);
4930 __isl_give isl_set *isl_set_partial_lexmin(
4931 __isl_take isl_set *set, __isl_take isl_set *dom,
4932 __isl_give isl_set **empty)
4934 return (struct isl_set *)
4935 isl_map_partial_lexmin((struct isl_map *)set,
4936 dom, empty);
4939 __isl_give isl_set *isl_set_partial_lexmax(
4940 __isl_take isl_set *set, __isl_take isl_set *dom,
4941 __isl_give isl_set **empty)
4943 return (struct isl_set *)
4944 isl_map_partial_lexmax((struct isl_map *)set,
4945 dom, empty);
4948 __isl_give isl_map *isl_basic_map_lexopt(__isl_take isl_basic_map *bmap, int max)
4950 struct isl_basic_set *dom = NULL;
4951 struct isl_dim *dom_dim;
4953 if (!bmap)
4954 goto error;
4955 dom_dim = isl_dim_domain(isl_dim_copy(bmap->dim));
4956 dom = isl_basic_set_universe(dom_dim);
4957 return isl_basic_map_partial_lexopt(bmap, dom, NULL, max);
4958 error:
4959 isl_basic_map_free(bmap);
4960 return NULL;
4963 __isl_give isl_map *isl_basic_map_lexmin(__isl_take isl_basic_map *bmap)
4965 return isl_basic_map_lexopt(bmap, 0);
4968 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
4970 return isl_basic_map_lexopt(bmap, 1);
4973 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
4975 return (isl_set *)isl_basic_map_lexmin((isl_basic_map *)bset);
4978 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
4980 return (isl_set *)isl_basic_map_lexmax((isl_basic_map *)bset);
4983 __isl_give isl_map *isl_map_lexopt(__isl_take isl_map *map, int max)
4985 struct isl_set *dom = NULL;
4986 struct isl_dim *dom_dim;
4988 if (!map)
4989 goto error;
4990 dom_dim = isl_dim_domain(isl_dim_copy(map->dim));
4991 dom = isl_set_universe(dom_dim);
4992 return isl_map_partial_lexopt(map, dom, NULL, max);
4993 error:
4994 isl_map_free(map);
4995 return NULL;
4998 __isl_give isl_map *isl_map_lexmin(__isl_take isl_map *map)
5000 return isl_map_lexopt(map, 0);
5003 __isl_give isl_map *isl_map_lexmax(__isl_take isl_map *map)
5005 return isl_map_lexopt(map, 1);
5008 __isl_give isl_set *isl_set_lexmin(__isl_take isl_set *set)
5010 return (isl_set *)isl_map_lexmin((isl_map *)set);
5013 __isl_give isl_set *isl_set_lexmax(__isl_take isl_set *set)
5015 return (isl_set *)isl_map_lexmax((isl_map *)set);
5018 /* Apply a preimage specified by "mat" on the parameters of "bset".
5019 * bset is assumed to have only parameters and divs.
5021 static struct isl_basic_set *basic_set_parameter_preimage(
5022 struct isl_basic_set *bset, struct isl_mat *mat)
5024 unsigned nparam;
5026 if (!bset || !mat)
5027 goto error;
5029 bset->dim = isl_dim_cow(bset->dim);
5030 if (!bset->dim)
5031 goto error;
5033 nparam = isl_basic_set_dim(bset, isl_dim_param);
5035 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
5037 bset->dim->nparam = 0;
5038 bset->dim->n_out = nparam;
5039 bset = isl_basic_set_preimage(bset, mat);
5040 if (bset) {
5041 bset->dim->nparam = bset->dim->n_out;
5042 bset->dim->n_out = 0;
5044 return bset;
5045 error:
5046 isl_mat_free(mat);
5047 isl_basic_set_free(bset);
5048 return NULL;
5051 /* Apply a preimage specified by "mat" on the parameters of "set".
5052 * set is assumed to have only parameters and divs.
5054 static struct isl_set *set_parameter_preimage(
5055 struct isl_set *set, struct isl_mat *mat)
5057 struct isl_dim *dim = NULL;
5058 unsigned nparam;
5060 if (!set || !mat)
5061 goto error;
5063 dim = isl_dim_copy(set->dim);
5064 dim = isl_dim_cow(dim);
5065 if (!dim)
5066 goto error;
5068 nparam = isl_set_dim(set, isl_dim_param);
5070 isl_assert(set->ctx, mat->n_row == 1 + nparam, goto error);
5072 dim->nparam = 0;
5073 dim->n_out = nparam;
5074 isl_set_reset_dim(set, dim);
5075 set = isl_set_preimage(set, mat);
5076 if (!set)
5077 goto error2;
5078 dim = isl_dim_copy(set->dim);
5079 dim = isl_dim_cow(dim);
5080 if (!dim)
5081 goto error2;
5082 dim->nparam = dim->n_out;
5083 dim->n_out = 0;
5084 isl_set_reset_dim(set, dim);
5085 return set;
5086 error:
5087 isl_dim_free(dim);
5088 isl_mat_free(mat);
5089 error2:
5090 isl_set_free(set);
5091 return NULL;
5094 /* Intersect the basic set "bset" with the affine space specified by the
5095 * equalities in "eq".
5097 static struct isl_basic_set *basic_set_append_equalities(
5098 struct isl_basic_set *bset, struct isl_mat *eq)
5100 int i, k;
5101 unsigned len;
5103 if (!bset || !eq)
5104 goto error;
5106 bset = isl_basic_set_extend_dim(bset, isl_dim_copy(bset->dim), 0,
5107 eq->n_row, 0);
5108 if (!bset)
5109 goto error;
5111 len = 1 + isl_dim_total(bset->dim) + bset->extra;
5112 for (i = 0; i < eq->n_row; ++i) {
5113 k = isl_basic_set_alloc_equality(bset);
5114 if (k < 0)
5115 goto error;
5116 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
5117 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
5119 isl_mat_free(eq);
5121 bset = isl_basic_set_gauss(bset, NULL);
5122 bset = isl_basic_set_finalize(bset);
5124 return bset;
5125 error:
5126 isl_mat_free(eq);
5127 isl_basic_set_free(bset);
5128 return NULL;
5131 /* Intersect the set "set" with the affine space specified by the
5132 * equalities in "eq".
5134 static struct isl_set *set_append_equalities(struct isl_set *set,
5135 struct isl_mat *eq)
5137 int i;
5139 if (!set || !eq)
5140 goto error;
5142 for (i = 0; i < set->n; ++i) {
5143 set->p[i] = basic_set_append_equalities(set->p[i],
5144 isl_mat_copy(eq));
5145 if (!set->p[i])
5146 goto error;
5148 isl_mat_free(eq);
5149 return set;
5150 error:
5151 isl_mat_free(eq);
5152 isl_set_free(set);
5153 return NULL;
5156 /* Project the given basic set onto its parameter domain, possibly introducing
5157 * new, explicit, existential variables in the constraints.
5158 * The input has parameters and (possibly implicit) existential variables.
5159 * The output has the same parameters, but only
5160 * explicit existentially quantified variables.
5162 * The actual projection is performed by pip, but pip doesn't seem
5163 * to like equalities very much, so we first remove the equalities
5164 * among the parameters by performing a variable compression on
5165 * the parameters. Afterward, an inverse transformation is performed
5166 * and the equalities among the parameters are inserted back in.
5168 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
5170 int i, j;
5171 struct isl_mat *eq;
5172 struct isl_mat *T, *T2;
5173 struct isl_set *set;
5174 unsigned nparam, n_div;
5176 bset = isl_basic_set_cow(bset);
5177 if (!bset)
5178 return NULL;
5180 if (bset->n_eq == 0)
5181 return isl_basic_set_lexmin(bset);
5183 isl_basic_set_gauss(bset, NULL);
5185 nparam = isl_basic_set_dim(bset, isl_dim_param);
5186 n_div = isl_basic_set_dim(bset, isl_dim_div);
5188 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
5189 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
5190 ++i;
5192 if (i == bset->n_eq)
5193 return isl_basic_set_lexmin(bset);
5195 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
5196 0, 1 + nparam);
5197 eq = isl_mat_cow(eq);
5198 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
5199 if (T && T->n_col == 0) {
5200 isl_mat_free(T);
5201 isl_mat_free(T2);
5202 isl_mat_free(eq);
5203 bset = isl_basic_set_set_to_empty(bset);
5204 return isl_set_from_basic_set(bset);
5206 bset = basic_set_parameter_preimage(bset, T);
5208 set = isl_basic_set_lexmin(bset);
5209 set = set_parameter_preimage(set, T2);
5210 set = set_append_equalities(set, eq);
5211 return set;
5214 /* Compute an explicit representation for all the existentially
5215 * quantified variables.
5216 * The input and output dimensions are first turned into parameters.
5217 * compute_divs then returns a map with the same parameters and
5218 * no input or output dimensions and the dimension specification
5219 * is reset to that of the input.
5221 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
5223 struct isl_basic_set *bset;
5224 struct isl_set *set;
5225 struct isl_map *map;
5226 struct isl_dim *dim, *orig_dim = NULL;
5227 unsigned nparam;
5228 unsigned n_in;
5229 unsigned n_out;
5231 bmap = isl_basic_map_cow(bmap);
5232 if (!bmap)
5233 return NULL;
5235 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5236 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5237 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5238 dim = isl_dim_set_alloc(bmap->ctx, nparam + n_in + n_out, 0);
5239 if (!dim)
5240 goto error;
5242 orig_dim = bmap->dim;
5243 bmap->dim = dim;
5244 bset = (struct isl_basic_set *)bmap;
5246 set = parameter_compute_divs(bset);
5247 map = (struct isl_map *)set;
5248 map = isl_map_reset_dim(map, orig_dim);
5250 return map;
5251 error:
5252 isl_basic_map_free(bmap);
5253 return NULL;
5256 static int basic_map_divs_known(__isl_keep isl_basic_map *bmap)
5258 int i;
5259 unsigned off;
5261 if (!bmap)
5262 return -1;
5264 off = isl_dim_total(bmap->dim);
5265 for (i = 0; i < bmap->n_div; ++i) {
5266 if (isl_int_is_zero(bmap->div[i][0]))
5267 return 0;
5268 isl_assert(bmap->ctx, isl_int_is_zero(bmap->div[i][1+1+off+i]),
5269 return -1);
5271 return 1;
5274 static int map_divs_known(__isl_keep isl_map *map)
5276 int i;
5278 if (!map)
5279 return -1;
5281 for (i = 0; i < map->n; ++i) {
5282 int known = basic_map_divs_known(map->p[i]);
5283 if (known <= 0)
5284 return known;
5287 return 1;
5290 /* If bmap contains any unknown divs, then compute explicit
5291 * expressions for them. However, this computation may be
5292 * quite expensive, so first try to remove divs that aren't
5293 * strictly needed.
5295 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
5297 int known;
5298 struct isl_map *map;
5300 known = basic_map_divs_known(bmap);
5301 if (known < 0)
5302 goto error;
5303 if (known)
5304 return isl_map_from_basic_map(bmap);
5306 bmap = isl_basic_map_drop_redundant_divs(bmap);
5308 known = basic_map_divs_known(bmap);
5309 if (known < 0)
5310 goto error;
5311 if (known)
5312 return isl_map_from_basic_map(bmap);
5314 map = compute_divs(bmap);
5315 return map;
5316 error:
5317 isl_basic_map_free(bmap);
5318 return NULL;
5321 struct isl_map *isl_map_compute_divs(struct isl_map *map)
5323 int i;
5324 int known;
5325 struct isl_map *res;
5327 if (!map)
5328 return NULL;
5329 if (map->n == 0)
5330 return map;
5332 known = map_divs_known(map);
5333 if (known < 0) {
5334 isl_map_free(map);
5335 return NULL;
5337 if (known)
5338 return map;
5340 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
5341 for (i = 1 ; i < map->n; ++i) {
5342 struct isl_map *r2;
5343 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
5344 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
5345 res = isl_map_union_disjoint(res, r2);
5346 else
5347 res = isl_map_union(res, r2);
5349 isl_map_free(map);
5351 return res;
5354 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
5356 return (struct isl_set *)
5357 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
5360 struct isl_set *isl_set_compute_divs(struct isl_set *set)
5362 return (struct isl_set *)
5363 isl_map_compute_divs((struct isl_map *)set);
5366 struct isl_set *isl_map_domain(struct isl_map *map)
5368 int i;
5369 struct isl_set *set;
5371 if (!map)
5372 goto error;
5374 map = isl_map_cow(map);
5375 if (!map)
5376 return NULL;
5378 set = (struct isl_set *)map;
5379 set->dim = isl_dim_domain(set->dim);
5380 if (!set->dim)
5381 goto error;
5382 for (i = 0; i < map->n; ++i) {
5383 set->p[i] = isl_basic_map_domain(map->p[i]);
5384 if (!set->p[i])
5385 goto error;
5387 ISL_F_CLR(set, ISL_MAP_DISJOINT);
5388 ISL_F_CLR(set, ISL_SET_NORMALIZED);
5389 return set;
5390 error:
5391 isl_map_free(map);
5392 return NULL;
5395 struct isl_map *isl_map_union_disjoint(
5396 struct isl_map *map1, struct isl_map *map2)
5398 int i;
5399 unsigned flags = 0;
5400 struct isl_map *map = NULL;
5402 if (!map1 || !map2)
5403 goto error;
5405 if (map1->n == 0) {
5406 isl_map_free(map1);
5407 return map2;
5409 if (map2->n == 0) {
5410 isl_map_free(map2);
5411 return map1;
5414 isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
5416 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
5417 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
5418 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
5420 map = isl_map_alloc_dim(isl_dim_copy(map1->dim),
5421 map1->n + map2->n, flags);
5422 if (!map)
5423 goto error;
5424 for (i = 0; i < map1->n; ++i) {
5425 map = isl_map_add_basic_map(map,
5426 isl_basic_map_copy(map1->p[i]));
5427 if (!map)
5428 goto error;
5430 for (i = 0; i < map2->n; ++i) {
5431 map = isl_map_add_basic_map(map,
5432 isl_basic_map_copy(map2->p[i]));
5433 if (!map)
5434 goto error;
5436 isl_map_free(map1);
5437 isl_map_free(map2);
5438 return map;
5439 error:
5440 isl_map_free(map);
5441 isl_map_free(map1);
5442 isl_map_free(map2);
5443 return NULL;
5446 struct isl_map *isl_map_union(struct isl_map *map1, struct isl_map *map2)
5448 map1 = isl_map_union_disjoint(map1, map2);
5449 if (!map1)
5450 return NULL;
5451 if (map1->n > 1)
5452 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
5453 return map1;
5456 struct isl_set *isl_set_union_disjoint(
5457 struct isl_set *set1, struct isl_set *set2)
5459 return (struct isl_set *)
5460 isl_map_union_disjoint(
5461 (struct isl_map *)set1, (struct isl_map *)set2);
5464 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
5466 return (struct isl_set *)
5467 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
5470 struct isl_map *isl_map_intersect_range(
5471 struct isl_map *map, struct isl_set *set)
5473 unsigned flags = 0;
5474 struct isl_map *result;
5475 int i, j;
5477 if (!map || !set)
5478 goto error;
5480 if (!isl_dim_match(map->dim, isl_dim_param, set->dim, isl_dim_param))
5481 isl_die(set->ctx, isl_error_invalid,
5482 "parameters don't match", goto error);
5484 if (isl_dim_size(set->dim, isl_dim_set) != 0 &&
5485 !isl_map_compatible_range(map, set))
5486 isl_die(set->ctx, isl_error_invalid,
5487 "incompatible spaces", goto error);
5489 if (isl_set_plain_is_universe(set)) {
5490 isl_set_free(set);
5491 return map;
5494 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
5495 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
5496 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
5498 result = isl_map_alloc_dim(isl_dim_copy(map->dim),
5499 map->n * set->n, flags);
5500 if (!result)
5501 goto error;
5502 for (i = 0; i < map->n; ++i)
5503 for (j = 0; j < set->n; ++j) {
5504 result = isl_map_add_basic_map(result,
5505 isl_basic_map_intersect_range(
5506 isl_basic_map_copy(map->p[i]),
5507 isl_basic_set_copy(set->p[j])));
5508 if (!result)
5509 goto error;
5511 isl_map_free(map);
5512 isl_set_free(set);
5513 return result;
5514 error:
5515 isl_map_free(map);
5516 isl_set_free(set);
5517 return NULL;
5520 struct isl_map *isl_map_intersect_domain(
5521 struct isl_map *map, struct isl_set *set)
5523 return isl_map_reverse(
5524 isl_map_intersect_range(isl_map_reverse(map), set));
5527 struct isl_map *isl_map_apply_domain(
5528 struct isl_map *map1, struct isl_map *map2)
5530 if (!map1 || !map2)
5531 goto error;
5532 map1 = isl_map_reverse(map1);
5533 map1 = isl_map_apply_range(map1, map2);
5534 return isl_map_reverse(map1);
5535 error:
5536 isl_map_free(map1);
5537 isl_map_free(map2);
5538 return NULL;
5541 struct isl_map *isl_map_apply_range(
5542 struct isl_map *map1, struct isl_map *map2)
5544 struct isl_dim *dim_result;
5545 struct isl_map *result;
5546 int i, j;
5548 if (!map1 || !map2)
5549 goto error;
5551 dim_result = isl_dim_join(isl_dim_copy(map1->dim),
5552 isl_dim_copy(map2->dim));
5554 result = isl_map_alloc_dim(dim_result, map1->n * map2->n, 0);
5555 if (!result)
5556 goto error;
5557 for (i = 0; i < map1->n; ++i)
5558 for (j = 0; j < map2->n; ++j) {
5559 result = isl_map_add_basic_map(result,
5560 isl_basic_map_apply_range(
5561 isl_basic_map_copy(map1->p[i]),
5562 isl_basic_map_copy(map2->p[j])));
5563 if (!result)
5564 goto error;
5566 isl_map_free(map1);
5567 isl_map_free(map2);
5568 if (result && result->n <= 1)
5569 ISL_F_SET(result, ISL_MAP_DISJOINT);
5570 return result;
5571 error:
5572 isl_map_free(map1);
5573 isl_map_free(map2);
5574 return NULL;
5578 * returns range - domain
5580 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
5582 isl_dim *dims, *target_dim;
5583 struct isl_basic_set *bset;
5584 unsigned dim;
5585 unsigned nparam;
5586 int i;
5588 if (!bmap)
5589 goto error;
5590 isl_assert(bmap->ctx, isl_dim_tuple_match(bmap->dim, isl_dim_in,
5591 bmap->dim, isl_dim_out),
5592 goto error);
5593 target_dim = isl_dim_domain(isl_basic_map_get_dim(bmap));
5594 dim = isl_basic_map_n_in(bmap);
5595 nparam = isl_basic_map_n_param(bmap);
5596 bset = isl_basic_set_from_basic_map(bmap);
5597 bset = isl_basic_set_cow(bset);
5598 dims = isl_basic_set_get_dim(bset);
5599 dims = isl_dim_add(dims, isl_dim_set, dim);
5600 bset = isl_basic_set_extend_dim(bset, dims, 0, dim, 0);
5601 bset = isl_basic_set_swap_vars(bset, 2*dim);
5602 for (i = 0; i < dim; ++i) {
5603 int j = isl_basic_map_alloc_equality(
5604 (struct isl_basic_map *)bset);
5605 if (j < 0)
5606 goto error;
5607 isl_seq_clr(bset->eq[j], 1 + isl_basic_set_total_dim(bset));
5608 isl_int_set_si(bset->eq[j][1+nparam+i], 1);
5609 isl_int_set_si(bset->eq[j][1+nparam+dim+i], 1);
5610 isl_int_set_si(bset->eq[j][1+nparam+2*dim+i], -1);
5612 bset = isl_basic_set_project_out(bset, isl_dim_set, dim, 2*dim);
5613 bset = isl_basic_set_reset_dim(bset, target_dim);
5614 return bset;
5615 error:
5616 isl_basic_map_free(bmap);
5617 return NULL;
5621 * returns range - domain
5623 struct isl_set *isl_map_deltas(struct isl_map *map)
5625 int i;
5626 isl_dim *dim;
5627 struct isl_set *result;
5629 if (!map)
5630 return NULL;
5632 isl_assert(map->ctx, isl_dim_tuple_match(map->dim, isl_dim_in,
5633 map->dim, isl_dim_out),
5634 goto error);
5635 dim = isl_map_get_dim(map);
5636 dim = isl_dim_domain(dim);
5637 result = isl_set_alloc_dim(dim, map->n, 0);
5638 if (!result)
5639 goto error;
5640 for (i = 0; i < map->n; ++i)
5641 result = isl_set_add_basic_set(result,
5642 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
5643 isl_map_free(map);
5644 return result;
5645 error:
5646 isl_map_free(map);
5647 return NULL;
5651 * returns [domain -> range] -> range - domain
5653 __isl_give isl_basic_map *isl_basic_map_deltas_map(
5654 __isl_take isl_basic_map *bmap)
5656 int i, k;
5657 isl_dim *dim;
5658 isl_basic_map *domain;
5659 int nparam, n;
5660 unsigned total;
5662 if (!isl_dim_tuple_match(bmap->dim, isl_dim_in, bmap->dim, isl_dim_out))
5663 isl_die(bmap->ctx, isl_error_invalid,
5664 "domain and range don't match", goto error);
5666 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5667 n = isl_basic_map_dim(bmap, isl_dim_in);
5669 dim = isl_dim_from_range(isl_dim_domain(isl_basic_map_get_dim(bmap)));
5670 domain = isl_basic_map_universe(dim);
5672 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5673 bmap = isl_basic_map_apply_range(bmap, domain);
5674 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
5676 total = isl_basic_map_total_dim(bmap);
5678 for (i = 0; i < n; ++i) {
5679 k = isl_basic_map_alloc_equality(bmap);
5680 if (k < 0)
5681 goto error;
5682 isl_seq_clr(bmap->eq[k], 1 + total);
5683 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
5684 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
5685 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
5688 bmap = isl_basic_map_gauss(bmap, NULL);
5689 return isl_basic_map_finalize(bmap);
5690 error:
5691 isl_basic_map_free(bmap);
5692 return NULL;
5696 * returns [domain -> range] -> range - domain
5698 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
5700 int i;
5701 isl_dim *domain_dim;
5703 if (!map)
5704 return NULL;
5706 if (!isl_dim_tuple_match(map->dim, isl_dim_in, map->dim, isl_dim_out))
5707 isl_die(map->ctx, isl_error_invalid,
5708 "domain and range don't match", goto error);
5710 map = isl_map_cow(map);
5711 if (!map)
5712 return NULL;
5714 domain_dim = isl_dim_from_range(isl_dim_domain(isl_map_get_dim(map)));
5715 map->dim = isl_dim_from_domain(isl_dim_wrap(map->dim));
5716 map->dim = isl_dim_join(map->dim, domain_dim);
5717 if (!map->dim)
5718 goto error;
5719 for (i = 0; i < map->n; ++i) {
5720 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
5721 if (!map->p[i])
5722 goto error;
5724 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5725 return map;
5726 error:
5727 isl_map_free(map);
5728 return NULL;
5731 static struct isl_basic_map *basic_map_identity(struct isl_dim *dims)
5733 struct isl_basic_map *bmap;
5734 unsigned nparam;
5735 unsigned dim;
5736 int i;
5738 if (!dims)
5739 return NULL;
5741 nparam = dims->nparam;
5742 dim = dims->n_out;
5743 bmap = isl_basic_map_alloc_dim(dims, 0, dim, 0);
5744 if (!bmap)
5745 goto error;
5747 for (i = 0; i < dim; ++i) {
5748 int j = isl_basic_map_alloc_equality(bmap);
5749 if (j < 0)
5750 goto error;
5751 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
5752 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
5753 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
5755 return isl_basic_map_finalize(bmap);
5756 error:
5757 isl_basic_map_free(bmap);
5758 return NULL;
5761 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_dim *dim)
5763 if (!dim)
5764 return NULL;
5765 if (dim->n_in != dim->n_out)
5766 isl_die(dim->ctx, isl_error_invalid,
5767 "number of input and output dimensions needs to be "
5768 "the same", goto error);
5769 return basic_map_identity(dim);
5770 error:
5771 isl_dim_free(dim);
5772 return NULL;
5775 struct isl_basic_map *isl_basic_map_identity_like(struct isl_basic_map *model)
5777 if (!model || !model->dim)
5778 return NULL;
5779 return isl_basic_map_identity(isl_dim_copy(model->dim));
5782 __isl_give isl_map *isl_map_identity(__isl_take isl_dim *dim)
5784 return isl_map_from_basic_map(isl_basic_map_identity(dim));
5787 struct isl_map *isl_map_identity_like(struct isl_map *model)
5789 if (!model || !model->dim)
5790 return NULL;
5791 return isl_map_identity(isl_dim_copy(model->dim));
5794 struct isl_map *isl_map_identity_like_basic_map(struct isl_basic_map *model)
5796 if (!model || !model->dim)
5797 return NULL;
5798 return isl_map_identity(isl_dim_copy(model->dim));
5801 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
5803 isl_dim *dim = isl_set_get_dim(set);
5804 isl_map *id;
5805 id = isl_map_identity(isl_dim_map_from_set(dim));
5806 return isl_map_intersect_range(id, set);
5809 /* Construct a basic set with all set dimensions having only non-negative
5810 * values.
5812 struct isl_basic_set *isl_basic_set_positive_orthant(struct isl_dim *dims)
5814 int i;
5815 unsigned nparam;
5816 unsigned dim;
5817 struct isl_basic_set *bset;
5819 if (!dims)
5820 return NULL;
5821 nparam = dims->nparam;
5822 dim = dims->n_out;
5823 bset = isl_basic_set_alloc_dim(dims, 0, 0, dim);
5824 if (!bset)
5825 return NULL;
5826 for (i = 0; i < dim; ++i) {
5827 int k = isl_basic_set_alloc_inequality(bset);
5828 if (k < 0)
5829 goto error;
5830 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
5831 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
5833 return bset;
5834 error:
5835 isl_basic_set_free(bset);
5836 return NULL;
5839 /* Construct the half-space x_pos >= 0.
5841 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_dim *dim,
5842 int pos)
5844 int k;
5845 isl_basic_set *nonneg;
5847 nonneg = isl_basic_set_alloc_dim(dim, 0, 0, 1);
5848 k = isl_basic_set_alloc_inequality(nonneg);
5849 if (k < 0)
5850 goto error;
5851 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
5852 isl_int_set_si(nonneg->ineq[k][pos], 1);
5854 return isl_basic_set_finalize(nonneg);
5855 error:
5856 isl_basic_set_free(nonneg);
5857 return NULL;
5860 /* Construct the half-space x_pos <= -1.
5862 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_dim *dim, int pos)
5864 int k;
5865 isl_basic_set *neg;
5867 neg = isl_basic_set_alloc_dim(dim, 0, 0, 1);
5868 k = isl_basic_set_alloc_inequality(neg);
5869 if (k < 0)
5870 goto error;
5871 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
5872 isl_int_set_si(neg->ineq[k][0], -1);
5873 isl_int_set_si(neg->ineq[k][pos], -1);
5875 return isl_basic_set_finalize(neg);
5876 error:
5877 isl_basic_set_free(neg);
5878 return NULL;
5881 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
5882 enum isl_dim_type type, unsigned first, unsigned n)
5884 int i;
5885 isl_basic_set *nonneg;
5886 isl_basic_set *neg;
5888 if (!set)
5889 return NULL;
5890 if (n == 0)
5891 return set;
5893 isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
5895 for (i = 0; i < n; ++i) {
5896 nonneg = nonneg_halfspace(isl_set_get_dim(set),
5897 pos(set->dim, type) + first + i);
5898 neg = neg_halfspace(isl_set_get_dim(set),
5899 pos(set->dim, type) + first + i);
5901 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
5904 return set;
5905 error:
5906 isl_set_free(set);
5907 return NULL;
5910 static int foreach_orthant(__isl_take isl_set *set, int *signs, int first,
5911 int len, int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
5912 void *user)
5914 isl_set *half;
5916 if (!set)
5917 return -1;
5918 if (isl_set_plain_is_empty(set)) {
5919 isl_set_free(set);
5920 return 0;
5922 if (first == len)
5923 return fn(set, signs, user);
5925 signs[first] = 1;
5926 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_dim(set),
5927 1 + first));
5928 half = isl_set_intersect(half, isl_set_copy(set));
5929 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
5930 goto error;
5932 signs[first] = -1;
5933 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_dim(set),
5934 1 + first));
5935 half = isl_set_intersect(half, set);
5936 return foreach_orthant(half, signs, first + 1, len, fn, user);
5937 error:
5938 isl_set_free(set);
5939 return -1;
5942 /* Call "fn" on the intersections of "set" with each of the orthants
5943 * (except for obviously empty intersections). The orthant is identified
5944 * by the signs array, with each entry having value 1 or -1 according
5945 * to the sign of the corresponding variable.
5947 int isl_set_foreach_orthant(__isl_keep isl_set *set,
5948 int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
5949 void *user)
5951 unsigned nparam;
5952 unsigned nvar;
5953 int *signs;
5954 int r;
5956 if (!set)
5957 return -1;
5958 if (isl_set_plain_is_empty(set))
5959 return 0;
5961 nparam = isl_set_dim(set, isl_dim_param);
5962 nvar = isl_set_dim(set, isl_dim_set);
5964 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
5966 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
5967 fn, user);
5969 free(signs);
5971 return r;
5974 int isl_set_is_equal(struct isl_set *set1, struct isl_set *set2)
5976 return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
5979 int isl_basic_map_is_subset(
5980 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
5982 int is_subset;
5983 struct isl_map *map1;
5984 struct isl_map *map2;
5986 if (!bmap1 || !bmap2)
5987 return -1;
5989 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
5990 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
5992 is_subset = isl_map_is_subset(map1, map2);
5994 isl_map_free(map1);
5995 isl_map_free(map2);
5997 return is_subset;
6000 int isl_basic_map_is_equal(
6001 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6003 int is_subset;
6005 if (!bmap1 || !bmap2)
6006 return -1;
6007 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
6008 if (is_subset != 1)
6009 return is_subset;
6010 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
6011 return is_subset;
6014 int isl_basic_set_is_equal(
6015 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
6017 return isl_basic_map_is_equal(
6018 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
6021 int isl_map_is_empty(struct isl_map *map)
6023 int i;
6024 int is_empty;
6026 if (!map)
6027 return -1;
6028 for (i = 0; i < map->n; ++i) {
6029 is_empty = isl_basic_map_is_empty(map->p[i]);
6030 if (is_empty < 0)
6031 return -1;
6032 if (!is_empty)
6033 return 0;
6035 return 1;
6038 int isl_map_plain_is_empty(__isl_keep isl_map *map)
6040 return map ? map->n == 0 : -1;
6043 int isl_map_fast_is_empty(__isl_keep isl_map *map)
6045 return isl_map_plain_is_empty(map);
6048 int isl_set_plain_is_empty(struct isl_set *set)
6050 return set ? set->n == 0 : -1;
6053 int isl_set_fast_is_empty(__isl_keep isl_set *set)
6055 return isl_set_plain_is_empty(set);
6058 int isl_set_is_empty(struct isl_set *set)
6060 return isl_map_is_empty((struct isl_map *)set);
6063 int isl_map_has_equal_dim(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
6065 if (!map1 || !map2)
6066 return -1;
6068 return isl_dim_equal(map1->dim, map2->dim);
6071 int isl_set_has_equal_dim(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
6073 if (!set1 || !set2)
6074 return -1;
6076 return isl_dim_equal(set1->dim, set2->dim);
6079 int isl_map_is_equal(struct isl_map *map1, struct isl_map *map2)
6081 int is_subset;
6083 if (!map1 || !map2)
6084 return -1;
6085 is_subset = isl_map_is_subset(map1, map2);
6086 if (is_subset != 1)
6087 return is_subset;
6088 is_subset = isl_map_is_subset(map2, map1);
6089 return is_subset;
6092 int isl_basic_map_is_strict_subset(
6093 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6095 int is_subset;
6097 if (!bmap1 || !bmap2)
6098 return -1;
6099 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
6100 if (is_subset != 1)
6101 return is_subset;
6102 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
6103 if (is_subset == -1)
6104 return is_subset;
6105 return !is_subset;
6108 int isl_map_is_strict_subset(struct isl_map *map1, struct isl_map *map2)
6110 int is_subset;
6112 if (!map1 || !map2)
6113 return -1;
6114 is_subset = isl_map_is_subset(map1, map2);
6115 if (is_subset != 1)
6116 return is_subset;
6117 is_subset = isl_map_is_subset(map2, map1);
6118 if (is_subset == -1)
6119 return is_subset;
6120 return !is_subset;
6123 int isl_set_is_strict_subset(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
6125 return isl_map_is_strict_subset((isl_map *)set1, (isl_map *)set2);
6128 int isl_basic_map_is_universe(struct isl_basic_map *bmap)
6130 if (!bmap)
6131 return -1;
6132 return bmap->n_eq == 0 && bmap->n_ineq == 0;
6135 int isl_basic_set_is_universe(struct isl_basic_set *bset)
6137 if (!bset)
6138 return -1;
6139 return bset->n_eq == 0 && bset->n_ineq == 0;
6142 int isl_map_plain_is_universe(__isl_keep isl_map *map)
6144 int i;
6146 if (!map)
6147 return -1;
6149 for (i = 0; i < map->n; ++i) {
6150 int r = isl_basic_map_is_universe(map->p[i]);
6151 if (r < 0 || r)
6152 return r;
6155 return 0;
6158 int isl_set_plain_is_universe(__isl_keep isl_set *set)
6160 return isl_map_plain_is_universe((isl_map *) set);
6163 int isl_set_fast_is_universe(__isl_keep isl_set *set)
6165 return isl_set_plain_is_universe(set);
6168 int isl_basic_map_is_empty(struct isl_basic_map *bmap)
6170 struct isl_basic_set *bset = NULL;
6171 struct isl_vec *sample = NULL;
6172 int empty;
6173 unsigned total;
6175 if (!bmap)
6176 return -1;
6178 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
6179 return 1;
6181 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
6182 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
6183 copy = isl_basic_map_remove_redundancies(copy);
6184 empty = ISL_F_ISSET(copy, ISL_BASIC_MAP_EMPTY);
6185 isl_basic_map_free(copy);
6186 return empty;
6189 total = 1 + isl_basic_map_total_dim(bmap);
6190 if (bmap->sample && bmap->sample->size == total) {
6191 int contains = isl_basic_map_contains(bmap, bmap->sample);
6192 if (contains < 0)
6193 return -1;
6194 if (contains)
6195 return 0;
6197 isl_vec_free(bmap->sample);
6198 bmap->sample = NULL;
6199 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
6200 if (!bset)
6201 return -1;
6202 sample = isl_basic_set_sample_vec(bset);
6203 if (!sample)
6204 return -1;
6205 empty = sample->size == 0;
6206 isl_vec_free(bmap->sample);
6207 bmap->sample = sample;
6208 if (empty)
6209 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
6211 return empty;
6214 int isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
6216 if (!bmap)
6217 return -1;
6218 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
6221 int isl_basic_map_fast_is_empty(__isl_keep isl_basic_map *bmap)
6223 return isl_basic_map_plain_is_empty(bmap);
6226 int isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
6228 if (!bset)
6229 return -1;
6230 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
6233 int isl_basic_set_fast_is_empty(__isl_keep isl_basic_set *bset)
6235 return isl_basic_set_plain_is_empty(bset);
6238 int isl_basic_set_is_empty(struct isl_basic_set *bset)
6240 return isl_basic_map_is_empty((struct isl_basic_map *)bset);
6243 struct isl_map *isl_basic_map_union(
6244 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6246 struct isl_map *map;
6247 if (!bmap1 || !bmap2)
6248 return NULL;
6250 isl_assert(bmap1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim), goto error);
6252 map = isl_map_alloc_dim(isl_dim_copy(bmap1->dim), 2, 0);
6253 if (!map)
6254 goto error;
6255 map = isl_map_add_basic_map(map, bmap1);
6256 map = isl_map_add_basic_map(map, bmap2);
6257 return map;
6258 error:
6259 isl_basic_map_free(bmap1);
6260 isl_basic_map_free(bmap2);
6261 return NULL;
6264 struct isl_set *isl_basic_set_union(
6265 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
6267 return (struct isl_set *)isl_basic_map_union(
6268 (struct isl_basic_map *)bset1,
6269 (struct isl_basic_map *)bset2);
6272 /* Order divs such that any div only depends on previous divs */
6273 struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
6275 int i;
6276 unsigned off;
6278 if (!bmap)
6279 return NULL;
6281 off = isl_dim_total(bmap->dim);
6283 for (i = 0; i < bmap->n_div; ++i) {
6284 int pos;
6285 if (isl_int_is_zero(bmap->div[i][0]))
6286 continue;
6287 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
6288 bmap->n_div-i);
6289 if (pos == -1)
6290 continue;
6291 isl_basic_map_swap_div(bmap, i, i + pos);
6292 --i;
6294 return bmap;
6297 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
6299 return (struct isl_basic_set *)
6300 isl_basic_map_order_divs((struct isl_basic_map *)bset);
6303 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
6305 int i;
6307 if (!map)
6308 return 0;
6310 for (i = 0; i < map->n; ++i) {
6311 map->p[i] = isl_basic_map_order_divs(map->p[i]);
6312 if (!map->p[i])
6313 goto error;
6316 return map;
6317 error:
6318 isl_map_free(map);
6319 return NULL;
6322 /* Apply the expansion computed by isl_merge_divs.
6323 * The expansion itself is given by "exp" while the resulting
6324 * list of divs is given by "div".
6326 __isl_give isl_basic_set *isl_basic_set_expand_divs(
6327 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
6329 int i, j;
6330 int n_div;
6332 bset = isl_basic_set_cow(bset);
6333 if (!bset || !div)
6334 goto error;
6336 if (div->n_row < bset->n_div)
6337 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
6338 "not an expansion", goto error);
6340 bset = isl_basic_map_extend_dim(bset, isl_dim_copy(bset->dim),
6341 div->n_row - bset->n_div, 0,
6342 2 * (div->n_row - bset->n_div));
6344 n_div = bset->n_div;
6345 for (i = n_div; i < div->n_row; ++i)
6346 if (isl_basic_set_alloc_div(bset) < 0)
6347 goto error;
6349 j = n_div - 1;
6350 for (i = div->n_row - 1; i >= 0; --i) {
6351 if (j >= 0 && exp[j] == i) {
6352 if (i != j)
6353 isl_basic_map_swap_div(bset, i, j);
6354 j--;
6355 } else {
6356 isl_seq_cpy(bset->div[i], div->row[i], div->n_col);
6357 if (isl_basic_map_add_div_constraints(bset, i) < 0)
6358 goto error;
6362 isl_mat_free(div);
6363 return bset;
6364 error:
6365 isl_basic_set_free(bset);
6366 isl_mat_free(div);
6367 return NULL;
6370 /* Look for a div in dst that corresponds to the div "div" in src.
6371 * The divs before "div" in src and dst are assumed to be the same.
6373 * Returns -1 if no corresponding div was found and the position
6374 * of the corresponding div in dst otherwise.
6376 static int find_div(struct isl_basic_map *dst,
6377 struct isl_basic_map *src, unsigned div)
6379 int i;
6381 unsigned total = isl_dim_total(src->dim);
6383 isl_assert(dst->ctx, div <= dst->n_div, return -1);
6384 for (i = div; i < dst->n_div; ++i)
6385 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
6386 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
6387 dst->n_div - div) == -1)
6388 return i;
6389 return -1;
6392 struct isl_basic_map *isl_basic_map_align_divs(
6393 struct isl_basic_map *dst, struct isl_basic_map *src)
6395 int i;
6396 unsigned total = isl_dim_total(src->dim);
6398 if (!dst || !src)
6399 goto error;
6401 if (src->n_div == 0)
6402 return dst;
6404 for (i = 0; i < src->n_div; ++i)
6405 isl_assert(src->ctx, !isl_int_is_zero(src->div[i][0]), goto error);
6407 src = isl_basic_map_order_divs(src);
6408 dst = isl_basic_map_cow(dst);
6409 dst = isl_basic_map_extend_dim(dst, isl_dim_copy(dst->dim),
6410 src->n_div, 0, 2 * src->n_div);
6411 if (!dst)
6412 return NULL;
6413 for (i = 0; i < src->n_div; ++i) {
6414 int j = find_div(dst, src, i);
6415 if (j < 0) {
6416 j = isl_basic_map_alloc_div(dst);
6417 if (j < 0)
6418 goto error;
6419 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
6420 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
6421 if (isl_basic_map_add_div_constraints(dst, j) < 0)
6422 goto error;
6424 if (j != i)
6425 isl_basic_map_swap_div(dst, i, j);
6427 return dst;
6428 error:
6429 isl_basic_map_free(dst);
6430 return NULL;
6433 struct isl_basic_set *isl_basic_set_align_divs(
6434 struct isl_basic_set *dst, struct isl_basic_set *src)
6436 return (struct isl_basic_set *)isl_basic_map_align_divs(
6437 (struct isl_basic_map *)dst, (struct isl_basic_map *)src);
6440 struct isl_map *isl_map_align_divs(struct isl_map *map)
6442 int i;
6444 if (!map)
6445 return NULL;
6446 if (map->n == 0)
6447 return map;
6448 map = isl_map_compute_divs(map);
6449 map = isl_map_cow(map);
6450 if (!map)
6451 return NULL;
6453 for (i = 1; i < map->n; ++i)
6454 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
6455 for (i = 1; i < map->n; ++i)
6456 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
6458 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6459 return map;
6462 struct isl_set *isl_set_align_divs(struct isl_set *set)
6464 return (struct isl_set *)isl_map_align_divs((struct isl_map *)set);
6467 struct isl_set *isl_set_apply(struct isl_set *set, struct isl_map *map)
6469 if (!set || !map)
6470 goto error;
6471 isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
6472 map = isl_map_intersect_domain(map, set);
6473 set = isl_map_range(map);
6474 return set;
6475 error:
6476 isl_set_free(set);
6477 isl_map_free(map);
6478 return NULL;
6481 /* There is no need to cow as removing empty parts doesn't change
6482 * the meaning of the set.
6484 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
6486 int i;
6488 if (!map)
6489 return NULL;
6491 for (i = map->n-1; i >= 0; --i) {
6492 if (!ISL_F_ISSET(map->p[i], ISL_BASIC_MAP_EMPTY))
6493 continue;
6494 isl_basic_map_free(map->p[i]);
6495 if (i != map->n-1) {
6496 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6497 map->p[i] = map->p[map->n-1];
6499 map->n--;
6502 return map;
6505 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
6507 return (struct isl_set *)
6508 isl_map_remove_empty_parts((struct isl_map *)set);
6511 struct isl_basic_map *isl_map_copy_basic_map(struct isl_map *map)
6513 struct isl_basic_map *bmap;
6514 if (!map || map->n == 0)
6515 return NULL;
6516 bmap = map->p[map->n-1];
6517 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
6518 return isl_basic_map_copy(bmap);
6521 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
6523 return (struct isl_basic_set *)
6524 isl_map_copy_basic_map((struct isl_map *)set);
6527 __isl_give isl_map *isl_map_drop_basic_map(__isl_take isl_map *map,
6528 __isl_keep isl_basic_map *bmap)
6530 int i;
6532 if (!map || !bmap)
6533 goto error;
6534 for (i = map->n-1; i >= 0; --i) {
6535 if (map->p[i] != bmap)
6536 continue;
6537 map = isl_map_cow(map);
6538 if (!map)
6539 goto error;
6540 isl_basic_map_free(map->p[i]);
6541 if (i != map->n-1) {
6542 ISL_F_CLR(map, ISL_SET_NORMALIZED);
6543 map->p[i] = map->p[map->n-1];
6545 map->n--;
6546 return map;
6548 return map;
6549 error:
6550 isl_map_free(map);
6551 return NULL;
6554 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
6555 struct isl_basic_set *bset)
6557 return (struct isl_set *)isl_map_drop_basic_map((struct isl_map *)set,
6558 (struct isl_basic_map *)bset);
6561 /* Given two basic sets bset1 and bset2, compute the maximal difference
6562 * between the values of dimension pos in bset1 and those in bset2
6563 * for any common value of the parameters and dimensions preceding pos.
6565 static enum isl_lp_result basic_set_maximal_difference_at(
6566 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
6567 int pos, isl_int *opt)
6569 struct isl_dim *dims;
6570 struct isl_basic_map *bmap1 = NULL;
6571 struct isl_basic_map *bmap2 = NULL;
6572 struct isl_ctx *ctx;
6573 struct isl_vec *obj;
6574 unsigned total;
6575 unsigned nparam;
6576 unsigned dim1, dim2;
6577 enum isl_lp_result res;
6579 if (!bset1 || !bset2)
6580 return isl_lp_error;
6582 nparam = isl_basic_set_n_param(bset1);
6583 dim1 = isl_basic_set_n_dim(bset1);
6584 dim2 = isl_basic_set_n_dim(bset2);
6585 dims = isl_dim_alloc(bset1->ctx, nparam, pos, dim1 - pos);
6586 bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1), dims);
6587 dims = isl_dim_alloc(bset2->ctx, nparam, pos, dim2 - pos);
6588 bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims);
6589 if (!bmap1 || !bmap2)
6590 goto error;
6591 bmap1 = isl_basic_map_cow(bmap1);
6592 bmap1 = isl_basic_map_extend(bmap1, nparam,
6593 pos, (dim1 - pos) + (dim2 - pos),
6594 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
6595 bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
6596 if (!bmap1)
6597 goto error;
6598 total = isl_basic_map_total_dim(bmap1);
6599 ctx = bmap1->ctx;
6600 obj = isl_vec_alloc(ctx, 1 + total);
6601 isl_seq_clr(obj->block.data, 1 + total);
6602 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
6603 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
6604 if (!obj)
6605 goto error;
6606 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
6607 opt, NULL, NULL);
6608 isl_basic_map_free(bmap1);
6609 isl_vec_free(obj);
6610 return res;
6611 error:
6612 isl_basic_map_free(bmap1);
6613 isl_basic_map_free(bmap2);
6614 return isl_lp_error;
6617 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
6618 * for any common value of the parameters and dimensions preceding pos
6619 * in both basic sets, the values of dimension pos in bset1 are
6620 * smaller or larger than those in bset2.
6622 * Returns
6623 * 1 if bset1 follows bset2
6624 * -1 if bset1 precedes bset2
6625 * 0 if bset1 and bset2 are incomparable
6626 * -2 if some error occurred.
6628 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
6629 struct isl_basic_set *bset2, int pos)
6631 isl_int opt;
6632 enum isl_lp_result res;
6633 int cmp;
6635 isl_int_init(opt);
6637 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
6639 if (res == isl_lp_empty)
6640 cmp = 0;
6641 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
6642 res == isl_lp_unbounded)
6643 cmp = 1;
6644 else if (res == isl_lp_ok && isl_int_is_neg(opt))
6645 cmp = -1;
6646 else
6647 cmp = -2;
6649 isl_int_clear(opt);
6650 return cmp;
6653 /* Given two basic sets bset1 and bset2, check whether
6654 * for any common value of the parameters and dimensions preceding pos
6655 * there is a value of dimension pos in bset1 that is larger
6656 * than a value of the same dimension in bset2.
6658 * Return
6659 * 1 if there exists such a pair
6660 * 0 if there is no such pair, but there is a pair of equal values
6661 * -1 otherwise
6662 * -2 if some error occurred.
6664 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
6665 __isl_keep isl_basic_set *bset2, int pos)
6667 isl_int opt;
6668 enum isl_lp_result res;
6669 int cmp;
6671 isl_int_init(opt);
6673 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
6675 if (res == isl_lp_empty)
6676 cmp = -1;
6677 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
6678 res == isl_lp_unbounded)
6679 cmp = 1;
6680 else if (res == isl_lp_ok && isl_int_is_neg(opt))
6681 cmp = -1;
6682 else if (res == isl_lp_ok)
6683 cmp = 0;
6684 else
6685 cmp = -2;
6687 isl_int_clear(opt);
6688 return cmp;
6691 /* Given two sets set1 and set2, check whether
6692 * for any common value of the parameters and dimensions preceding pos
6693 * there is a value of dimension pos in set1 that is larger
6694 * than a value of the same dimension in set2.
6696 * Return
6697 * 1 if there exists such a pair
6698 * 0 if there is no such pair, but there is a pair of equal values
6699 * -1 otherwise
6700 * -2 if some error occurred.
6702 int isl_set_follows_at(__isl_keep isl_set *set1,
6703 __isl_keep isl_set *set2, int pos)
6705 int i, j;
6706 int follows = -1;
6708 if (!set1 || !set2)
6709 return -2;
6711 for (i = 0; i < set1->n; ++i)
6712 for (j = 0; j < set2->n; ++j) {
6713 int f;
6714 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
6715 if (f == 1 || f == -2)
6716 return f;
6717 if (f > follows)
6718 follows = f;
6721 return follows;
6724 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map *bmap,
6725 unsigned pos, isl_int *val)
6727 int i;
6728 int d;
6729 unsigned total;
6731 if (!bmap)
6732 return -1;
6733 total = isl_basic_map_total_dim(bmap);
6734 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
6735 for (; d+1 > pos; --d)
6736 if (!isl_int_is_zero(bmap->eq[i][1+d]))
6737 break;
6738 if (d != pos)
6739 continue;
6740 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
6741 return 0;
6742 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
6743 return 0;
6744 if (!isl_int_is_one(bmap->eq[i][1+d]))
6745 return 0;
6746 if (val)
6747 isl_int_neg(*val, bmap->eq[i][0]);
6748 return 1;
6750 return 0;
6753 static int isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
6754 unsigned pos, isl_int *val)
6756 int i;
6757 isl_int v;
6758 isl_int tmp;
6759 int fixed;
6761 if (!map)
6762 return -1;
6763 if (map->n == 0)
6764 return 0;
6765 if (map->n == 1)
6766 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
6767 isl_int_init(v);
6768 isl_int_init(tmp);
6769 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
6770 for (i = 1; fixed == 1 && i < map->n; ++i) {
6771 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
6772 if (fixed == 1 && isl_int_ne(tmp, v))
6773 fixed = 0;
6775 if (val)
6776 isl_int_set(*val, v);
6777 isl_int_clear(tmp);
6778 isl_int_clear(v);
6779 return fixed;
6782 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set *bset,
6783 unsigned pos, isl_int *val)
6785 return isl_basic_map_plain_has_fixed_var((struct isl_basic_map *)bset,
6786 pos, val);
6789 static int isl_set_plain_has_fixed_var(__isl_keep isl_set *set, unsigned pos,
6790 isl_int *val)
6792 return isl_map_plain_has_fixed_var((struct isl_map *)set, pos, val);
6795 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
6796 enum isl_dim_type type, unsigned pos, isl_int *val)
6798 if (pos >= isl_basic_map_dim(bmap, type))
6799 return -1;
6800 return isl_basic_map_plain_has_fixed_var(bmap,
6801 isl_basic_map_offset(bmap, type) - 1 + pos, val);
6804 int isl_map_plain_is_fixed(__isl_keep isl_map *map,
6805 enum isl_dim_type type, unsigned pos, isl_int *val)
6807 if (pos >= isl_map_dim(map, type))
6808 return -1;
6809 return isl_map_plain_has_fixed_var(map,
6810 map_offset(map, type) - 1 + pos, val);
6813 int isl_map_fast_is_fixed(__isl_keep isl_map *map,
6814 enum isl_dim_type type, unsigned pos, isl_int *val)
6816 return isl_map_plain_is_fixed(map, type, pos, val);
6819 /* Check if dimension dim has fixed value and if so and if val is not NULL,
6820 * then return this fixed value in *val.
6822 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
6823 unsigned dim, isl_int *val)
6825 return isl_basic_set_plain_has_fixed_var(bset,
6826 isl_basic_set_n_param(bset) + dim, val);
6829 /* Check if dimension dim has fixed value and if so and if val is not NULL,
6830 * then return this fixed value in *val.
6832 int isl_set_plain_dim_is_fixed(__isl_keep isl_set *set,
6833 unsigned dim, isl_int *val)
6835 return isl_set_plain_has_fixed_var(set, isl_set_n_param(set) + dim, val);
6838 int isl_set_fast_dim_is_fixed(__isl_keep isl_set *set,
6839 unsigned dim, isl_int *val)
6841 return isl_set_plain_dim_is_fixed(set, dim, val);
6844 /* Check if input variable in has fixed value and if so and if val is not NULL,
6845 * then return this fixed value in *val.
6847 int isl_map_plain_input_is_fixed(__isl_keep isl_map *map,
6848 unsigned in, isl_int *val)
6850 return isl_map_plain_has_fixed_var(map, isl_map_n_param(map) + in, val);
6853 /* Check if dimension dim has an (obvious) fixed lower bound and if so
6854 * and if val is not NULL, then return this lower bound in *val.
6856 int isl_basic_set_plain_dim_has_fixed_lower_bound(
6857 __isl_keep isl_basic_set *bset, unsigned dim, isl_int *val)
6859 int i, i_eq = -1, i_ineq = -1;
6860 isl_int *c;
6861 unsigned total;
6862 unsigned nparam;
6864 if (!bset)
6865 return -1;
6866 total = isl_basic_set_total_dim(bset);
6867 nparam = isl_basic_set_n_param(bset);
6868 for (i = 0; i < bset->n_eq; ++i) {
6869 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
6870 continue;
6871 if (i_eq != -1)
6872 return 0;
6873 i_eq = i;
6875 for (i = 0; i < bset->n_ineq; ++i) {
6876 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
6877 continue;
6878 if (i_eq != -1 || i_ineq != -1)
6879 return 0;
6880 i_ineq = i;
6882 if (i_eq == -1 && i_ineq == -1)
6883 return 0;
6884 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
6885 /* The coefficient should always be one due to normalization. */
6886 if (!isl_int_is_one(c[1+nparam+dim]))
6887 return 0;
6888 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
6889 return 0;
6890 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
6891 total - nparam - dim - 1) != -1)
6892 return 0;
6893 if (val)
6894 isl_int_neg(*val, c[0]);
6895 return 1;
6898 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set *set,
6899 unsigned dim, isl_int *val)
6901 int i;
6902 isl_int v;
6903 isl_int tmp;
6904 int fixed;
6906 if (!set)
6907 return -1;
6908 if (set->n == 0)
6909 return 0;
6910 if (set->n == 1)
6911 return isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
6912 dim, val);
6913 isl_int_init(v);
6914 isl_int_init(tmp);
6915 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
6916 dim, &v);
6917 for (i = 1; fixed == 1 && i < set->n; ++i) {
6918 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[i],
6919 dim, &tmp);
6920 if (fixed == 1 && isl_int_ne(tmp, v))
6921 fixed = 0;
6923 if (val)
6924 isl_int_set(*val, v);
6925 isl_int_clear(tmp);
6926 isl_int_clear(v);
6927 return fixed;
6930 struct constraint {
6931 unsigned size;
6932 isl_int *c;
6935 /* uset_gist depends on constraints without existentially quantified
6936 * variables sorting first.
6938 static int qsort_constraint_cmp(const void *p1, const void *p2)
6940 const struct constraint *c1 = (const struct constraint *)p1;
6941 const struct constraint *c2 = (const struct constraint *)p2;
6942 int l1, l2;
6943 unsigned size = isl_min(c1->size, c2->size);
6945 l1 = isl_seq_last_non_zero(c1->c, size);
6946 l2 = isl_seq_last_non_zero(c2->c, size);
6948 if (l1 != l2)
6949 return l1 - l2;
6951 return isl_seq_cmp(c1->c, c2->c, size);
6954 static struct isl_basic_map *isl_basic_map_sort_constraints(
6955 struct isl_basic_map *bmap)
6957 int i;
6958 struct constraint *c;
6959 unsigned total;
6961 if (!bmap)
6962 return NULL;
6963 total = isl_basic_map_total_dim(bmap);
6964 c = isl_alloc_array(bmap->ctx, struct constraint, bmap->n_ineq);
6965 if (!c)
6966 goto error;
6967 for (i = 0; i < bmap->n_ineq; ++i) {
6968 c[i].size = total;
6969 c[i].c = bmap->ineq[i];
6971 qsort(c, bmap->n_ineq, sizeof(struct constraint), qsort_constraint_cmp);
6972 for (i = 0; i < bmap->n_ineq; ++i)
6973 bmap->ineq[i] = c[i].c;
6974 free(c);
6975 return bmap;
6976 error:
6977 isl_basic_map_free(bmap);
6978 return NULL;
6981 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
6982 __isl_take isl_basic_set *bset)
6984 return (struct isl_basic_set *)isl_basic_map_sort_constraints(
6985 (struct isl_basic_map *)bset);
6988 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
6990 if (!bmap)
6991 return NULL;
6992 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
6993 return bmap;
6994 bmap = isl_basic_map_remove_redundancies(bmap);
6995 bmap = isl_basic_map_sort_constraints(bmap);
6996 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
6997 return bmap;
7000 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
7002 return (struct isl_basic_set *)isl_basic_map_normalize(
7003 (struct isl_basic_map *)bset);
7006 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map *bmap1,
7007 const __isl_keep isl_basic_map *bmap2)
7009 int i, cmp;
7010 unsigned total;
7012 if (bmap1 == bmap2)
7013 return 0;
7014 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
7015 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
7016 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
7017 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
7018 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
7019 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
7020 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
7021 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
7022 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
7023 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
7024 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
7025 return 0;
7026 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
7027 return 1;
7028 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
7029 return -1;
7030 if (bmap1->n_eq != bmap2->n_eq)
7031 return bmap1->n_eq - bmap2->n_eq;
7032 if (bmap1->n_ineq != bmap2->n_ineq)
7033 return bmap1->n_ineq - bmap2->n_ineq;
7034 if (bmap1->n_div != bmap2->n_div)
7035 return bmap1->n_div - bmap2->n_div;
7036 total = isl_basic_map_total_dim(bmap1);
7037 for (i = 0; i < bmap1->n_eq; ++i) {
7038 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
7039 if (cmp)
7040 return cmp;
7042 for (i = 0; i < bmap1->n_ineq; ++i) {
7043 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
7044 if (cmp)
7045 return cmp;
7047 for (i = 0; i < bmap1->n_div; ++i) {
7048 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
7049 if (cmp)
7050 return cmp;
7052 return 0;
7055 int isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
7056 __isl_keep isl_basic_map *bmap2)
7058 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
7061 int isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
7062 __isl_keep isl_basic_set *bset2)
7064 return isl_basic_map_plain_is_equal((isl_basic_map *)bset1,
7065 (isl_basic_map *)bset2);
7068 static int qsort_bmap_cmp(const void *p1, const void *p2)
7070 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
7071 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
7073 return isl_basic_map_plain_cmp(bmap1, bmap2);
7076 /* We normalize in place, but if anything goes wrong we need
7077 * to return NULL, so we need to make sure we don't change the
7078 * meaning of any possible other copies of map.
7080 struct isl_map *isl_map_normalize(struct isl_map *map)
7082 int i, j;
7083 struct isl_basic_map *bmap;
7085 if (!map)
7086 return NULL;
7087 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
7088 return map;
7089 for (i = 0; i < map->n; ++i) {
7090 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
7091 if (!bmap)
7092 goto error;
7093 isl_basic_map_free(map->p[i]);
7094 map->p[i] = bmap;
7096 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
7097 ISL_F_SET(map, ISL_MAP_NORMALIZED);
7098 map = isl_map_remove_empty_parts(map);
7099 if (!map)
7100 return NULL;
7101 for (i = map->n - 1; i >= 1; --i) {
7102 if (!isl_basic_map_plain_is_equal(map->p[i-1], map->p[i]))
7103 continue;
7104 isl_basic_map_free(map->p[i-1]);
7105 for (j = i; j < map->n; ++j)
7106 map->p[j-1] = map->p[j];
7107 map->n--;
7109 return map;
7110 error:
7111 isl_map_free(map);
7112 return NULL;
7116 struct isl_set *isl_set_normalize(struct isl_set *set)
7118 return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
7121 int isl_map_plain_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7123 int i;
7124 int equal;
7126 if (!map1 || !map2)
7127 return -1;
7129 if (map1 == map2)
7130 return 1;
7131 if (!isl_dim_equal(map1->dim, map2->dim))
7132 return 0;
7134 map1 = isl_map_copy(map1);
7135 map2 = isl_map_copy(map2);
7136 map1 = isl_map_normalize(map1);
7137 map2 = isl_map_normalize(map2);
7138 if (!map1 || !map2)
7139 goto error;
7140 equal = map1->n == map2->n;
7141 for (i = 0; equal && i < map1->n; ++i) {
7142 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
7143 if (equal < 0)
7144 goto error;
7146 isl_map_free(map1);
7147 isl_map_free(map2);
7148 return equal;
7149 error:
7150 isl_map_free(map1);
7151 isl_map_free(map2);
7152 return -1;
7155 int isl_map_fast_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7157 return isl_map_plain_is_equal(map1, map2);
7160 int isl_set_plain_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7162 return isl_map_plain_is_equal((struct isl_map *)set1,
7163 (struct isl_map *)set2);
7166 int isl_set_fast_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7168 return isl_set_plain_is_equal(set1, set2);
7171 /* Return an interval that ranges from min to max (inclusive)
7173 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
7174 isl_int min, isl_int max)
7176 int k;
7177 struct isl_basic_set *bset = NULL;
7179 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
7180 if (!bset)
7181 goto error;
7183 k = isl_basic_set_alloc_inequality(bset);
7184 if (k < 0)
7185 goto error;
7186 isl_int_set_si(bset->ineq[k][1], 1);
7187 isl_int_neg(bset->ineq[k][0], min);
7189 k = isl_basic_set_alloc_inequality(bset);
7190 if (k < 0)
7191 goto error;
7192 isl_int_set_si(bset->ineq[k][1], -1);
7193 isl_int_set(bset->ineq[k][0], max);
7195 return bset;
7196 error:
7197 isl_basic_set_free(bset);
7198 return NULL;
7201 /* Return the Cartesian product of the basic sets in list (in the given order).
7203 __isl_give isl_basic_set *isl_basic_set_list_product(
7204 __isl_take struct isl_basic_set_list *list)
7206 int i;
7207 unsigned dim;
7208 unsigned nparam;
7209 unsigned extra;
7210 unsigned n_eq;
7211 unsigned n_ineq;
7212 struct isl_basic_set *product = NULL;
7214 if (!list)
7215 goto error;
7216 isl_assert(list->ctx, list->n > 0, goto error);
7217 isl_assert(list->ctx, list->p[0], goto error);
7218 nparam = isl_basic_set_n_param(list->p[0]);
7219 dim = isl_basic_set_n_dim(list->p[0]);
7220 extra = list->p[0]->n_div;
7221 n_eq = list->p[0]->n_eq;
7222 n_ineq = list->p[0]->n_ineq;
7223 for (i = 1; i < list->n; ++i) {
7224 isl_assert(list->ctx, list->p[i], goto error);
7225 isl_assert(list->ctx,
7226 nparam == isl_basic_set_n_param(list->p[i]), goto error);
7227 dim += isl_basic_set_n_dim(list->p[i]);
7228 extra += list->p[i]->n_div;
7229 n_eq += list->p[i]->n_eq;
7230 n_ineq += list->p[i]->n_ineq;
7232 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
7233 n_eq, n_ineq);
7234 if (!product)
7235 goto error;
7236 dim = 0;
7237 for (i = 0; i < list->n; ++i) {
7238 isl_basic_set_add_constraints(product,
7239 isl_basic_set_copy(list->p[i]), dim);
7240 dim += isl_basic_set_n_dim(list->p[i]);
7242 isl_basic_set_list_free(list);
7243 return product;
7244 error:
7245 isl_basic_set_free(product);
7246 isl_basic_set_list_free(list);
7247 return NULL;
7250 struct isl_basic_map *isl_basic_map_product(
7251 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7253 struct isl_dim *dim_result = NULL;
7254 struct isl_basic_map *bmap;
7255 unsigned in1, in2, out1, out2, nparam, total, pos;
7256 struct isl_dim_map *dim_map1, *dim_map2;
7258 if (!bmap1 || !bmap2)
7259 goto error;
7261 isl_assert(bmap1->ctx, isl_dim_match(bmap1->dim, isl_dim_param,
7262 bmap2->dim, isl_dim_param), goto error);
7263 dim_result = isl_dim_product(isl_dim_copy(bmap1->dim),
7264 isl_dim_copy(bmap2->dim));
7266 in1 = isl_basic_map_n_in(bmap1);
7267 in2 = isl_basic_map_n_in(bmap2);
7268 out1 = isl_basic_map_n_out(bmap1);
7269 out2 = isl_basic_map_n_out(bmap2);
7270 nparam = isl_basic_map_n_param(bmap1);
7272 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
7273 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
7274 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
7275 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
7276 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
7277 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
7278 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
7279 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
7280 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
7281 isl_dim_map_div(dim_map1, bmap1, pos += out2);
7282 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
7284 bmap = isl_basic_map_alloc_dim(dim_result,
7285 bmap1->n_div + bmap2->n_div,
7286 bmap1->n_eq + bmap2->n_eq,
7287 bmap1->n_ineq + bmap2->n_ineq);
7288 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
7289 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
7290 bmap = isl_basic_map_simplify(bmap);
7291 return isl_basic_map_finalize(bmap);
7292 error:
7293 isl_basic_map_free(bmap1);
7294 isl_basic_map_free(bmap2);
7295 return NULL;
7298 __isl_give isl_basic_map *isl_basic_map_flat_product(
7299 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
7301 isl_basic_map *prod;
7303 prod = isl_basic_map_product(bmap1, bmap2);
7304 prod = isl_basic_map_flatten(prod);
7305 return prod;
7308 __isl_give isl_basic_set *isl_basic_set_flat_product(
7309 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
7311 return isl_basic_map_flat_product(bset1, bset2);
7314 __isl_give isl_basic_map *isl_basic_map_range_product(
7315 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
7317 isl_dim *dim_result = NULL;
7318 isl_basic_map *bmap;
7319 unsigned in, out1, out2, nparam, total, pos;
7320 struct isl_dim_map *dim_map1, *dim_map2;
7322 if (!bmap1 || !bmap2)
7323 goto error;
7325 dim_result = isl_dim_range_product(isl_dim_copy(bmap1->dim),
7326 isl_dim_copy(bmap2->dim));
7328 in = isl_basic_map_dim(bmap1, isl_dim_in);
7329 out1 = isl_basic_map_n_out(bmap1);
7330 out2 = isl_basic_map_n_out(bmap2);
7331 nparam = isl_basic_map_n_param(bmap1);
7333 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
7334 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
7335 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
7336 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
7337 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
7338 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
7339 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
7340 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
7341 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
7342 isl_dim_map_div(dim_map1, bmap1, pos += out2);
7343 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
7345 bmap = isl_basic_map_alloc_dim(dim_result,
7346 bmap1->n_div + bmap2->n_div,
7347 bmap1->n_eq + bmap2->n_eq,
7348 bmap1->n_ineq + bmap2->n_ineq);
7349 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
7350 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
7351 bmap = isl_basic_map_simplify(bmap);
7352 return isl_basic_map_finalize(bmap);
7353 error:
7354 isl_basic_map_free(bmap1);
7355 isl_basic_map_free(bmap2);
7356 return NULL;
7359 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
7360 __isl_take isl_map *map2,
7361 __isl_give isl_dim *(*dim_product)(__isl_take isl_dim *left,
7362 __isl_take isl_dim *right),
7363 __isl_give isl_basic_map *(*basic_map_product)(
7364 __isl_take isl_basic_map *left, __isl_take isl_basic_map *right))
7366 unsigned flags = 0;
7367 struct isl_map *result;
7368 int i, j;
7370 if (!map1 || !map2)
7371 goto error;
7373 isl_assert(map1->ctx, isl_dim_match(map1->dim, isl_dim_param,
7374 map2->dim, isl_dim_param), goto error);
7376 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
7377 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
7378 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7380 result = isl_map_alloc_dim(dim_product(isl_dim_copy(map1->dim),
7381 isl_dim_copy(map2->dim)),
7382 map1->n * map2->n, flags);
7383 if (!result)
7384 goto error;
7385 for (i = 0; i < map1->n; ++i)
7386 for (j = 0; j < map2->n; ++j) {
7387 struct isl_basic_map *part;
7388 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
7389 isl_basic_map_copy(map2->p[j]));
7390 if (isl_basic_map_is_empty(part))
7391 isl_basic_map_free(part);
7392 else
7393 result = isl_map_add_basic_map(result, part);
7394 if (!result)
7395 goto error;
7397 isl_map_free(map1);
7398 isl_map_free(map2);
7399 return result;
7400 error:
7401 isl_map_free(map1);
7402 isl_map_free(map2);
7403 return NULL;
7406 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
7408 struct isl_map *isl_map_product(struct isl_map *map1, struct isl_map *map2)
7410 return map_product(map1, map2, &isl_dim_product, &isl_basic_map_product);
7413 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
7415 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
7416 __isl_take isl_map *map2)
7418 isl_map *prod;
7420 prod = isl_map_product(map1, map2);
7421 prod = isl_map_flatten(prod);
7422 return prod;
7425 /* Given two set A and B, construct its Cartesian product A x B.
7427 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
7429 return (struct isl_set *)isl_map_product((struct isl_map *)set1,
7430 (struct isl_map *)set2);
7433 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
7434 __isl_take isl_set *set2)
7436 return (isl_set *)isl_map_flat_product((isl_map *)set1, (isl_map *)set2);
7439 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
7441 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
7442 __isl_take isl_map *map2)
7444 return map_product(map1, map2, &isl_dim_range_product,
7445 &isl_basic_map_range_product);
7448 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
7450 int i;
7451 uint32_t hash = isl_hash_init();
7452 unsigned total;
7454 if (!bmap)
7455 return 0;
7456 bmap = isl_basic_map_copy(bmap);
7457 bmap = isl_basic_map_normalize(bmap);
7458 if (!bmap)
7459 return 0;
7460 total = isl_basic_map_total_dim(bmap);
7461 isl_hash_byte(hash, bmap->n_eq & 0xFF);
7462 for (i = 0; i < bmap->n_eq; ++i) {
7463 uint32_t c_hash;
7464 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
7465 isl_hash_hash(hash, c_hash);
7467 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
7468 for (i = 0; i < bmap->n_ineq; ++i) {
7469 uint32_t c_hash;
7470 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
7471 isl_hash_hash(hash, c_hash);
7473 isl_hash_byte(hash, bmap->n_div & 0xFF);
7474 for (i = 0; i < bmap->n_div; ++i) {
7475 uint32_t c_hash;
7476 if (isl_int_is_zero(bmap->div[i][0]))
7477 continue;
7478 isl_hash_byte(hash, i & 0xFF);
7479 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
7480 isl_hash_hash(hash, c_hash);
7482 isl_basic_map_free(bmap);
7483 return hash;
7486 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
7488 return isl_basic_map_get_hash((isl_basic_map *)bset);
7491 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
7493 int i;
7494 uint32_t hash;
7496 if (!map)
7497 return 0;
7498 map = isl_map_copy(map);
7499 map = isl_map_normalize(map);
7500 if (!map)
7501 return 0;
7503 hash = isl_hash_init();
7504 for (i = 0; i < map->n; ++i) {
7505 uint32_t bmap_hash;
7506 bmap_hash = isl_basic_map_get_hash(map->p[i]);
7507 isl_hash_hash(hash, bmap_hash);
7510 isl_map_free(map);
7512 return hash;
7515 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
7517 return isl_map_get_hash((isl_map *)set);
7520 /* Check if the value for dimension dim is completely determined
7521 * by the values of the other parameters and variables.
7522 * That is, check if dimension dim is involved in an equality.
7524 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
7526 int i;
7527 unsigned nparam;
7529 if (!bset)
7530 return -1;
7531 nparam = isl_basic_set_n_param(bset);
7532 for (i = 0; i < bset->n_eq; ++i)
7533 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
7534 return 1;
7535 return 0;
7538 /* Check if the value for dimension dim is completely determined
7539 * by the values of the other parameters and variables.
7540 * That is, check if dimension dim is involved in an equality
7541 * for each of the subsets.
7543 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
7545 int i;
7547 if (!set)
7548 return -1;
7549 for (i = 0; i < set->n; ++i) {
7550 int unique;
7551 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
7552 if (unique != 1)
7553 return unique;
7555 return 1;
7558 int isl_set_n_basic_set(__isl_keep isl_set *set)
7560 return set ? set->n : 0;
7563 int isl_map_foreach_basic_map(__isl_keep isl_map *map,
7564 int (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
7566 int i;
7568 if (!map)
7569 return -1;
7571 for (i = 0; i < map->n; ++i)
7572 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
7573 return -1;
7575 return 0;
7578 int isl_set_foreach_basic_set(__isl_keep isl_set *set,
7579 int (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
7581 int i;
7583 if (!set)
7584 return -1;
7586 for (i = 0; i < set->n; ++i)
7587 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
7588 return -1;
7590 return 0;
7593 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
7595 struct isl_dim *dim;
7597 if (!bset)
7598 return NULL;
7600 bset = isl_basic_set_cow(bset);
7601 if (!bset)
7602 return NULL;
7604 dim = isl_basic_set_get_dim(bset);
7605 dim = isl_dim_lift(dim, bset->n_div);
7606 if (!dim)
7607 goto error;
7608 isl_dim_free(bset->dim);
7609 bset->dim = dim;
7610 bset->extra -= bset->n_div;
7611 bset->n_div = 0;
7613 bset = isl_basic_set_finalize(bset);
7615 return bset;
7616 error:
7617 isl_basic_set_free(bset);
7618 return NULL;
7621 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
7623 int i;
7624 struct isl_dim *dim;
7625 unsigned n_div;
7627 set = isl_set_align_divs(set);
7629 if (!set)
7630 return NULL;
7632 set = isl_set_cow(set);
7633 if (!set)
7634 return NULL;
7636 n_div = set->p[0]->n_div;
7637 dim = isl_set_get_dim(set);
7638 dim = isl_dim_lift(dim, n_div);
7639 if (!dim)
7640 goto error;
7641 isl_dim_free(set->dim);
7642 set->dim = dim;
7644 for (i = 0; i < set->n; ++i) {
7645 set->p[i] = isl_basic_set_lift(set->p[i]);
7646 if (!set->p[i])
7647 goto error;
7650 return set;
7651 error:
7652 isl_set_free(set);
7653 return NULL;
7656 __isl_give isl_map *isl_set_lifting(__isl_take isl_set *set)
7658 struct isl_dim *dim;
7659 struct isl_basic_map *bmap;
7660 unsigned n_set;
7661 unsigned n_div;
7662 unsigned n_param;
7663 unsigned total;
7664 int i, k, l;
7666 set = isl_set_align_divs(set);
7668 if (!set)
7669 return NULL;
7671 dim = isl_set_get_dim(set);
7672 if (set->n == 0 || set->p[0]->n_div == 0) {
7673 isl_set_free(set);
7674 return isl_map_identity(isl_dim_map_from_set(dim));
7677 n_div = set->p[0]->n_div;
7678 dim = isl_dim_map_from_set(dim);
7679 n_param = isl_dim_size(dim, isl_dim_param);
7680 n_set = isl_dim_size(dim, isl_dim_in);
7681 dim = isl_dim_extend(dim, n_param, n_set, n_set + n_div);
7682 bmap = isl_basic_map_alloc_dim(dim, 0, n_set, 2 * n_div);
7683 for (i = 0; i < n_set; ++i)
7684 bmap = var_equal(bmap, i);
7686 total = n_param + n_set + n_set + n_div;
7687 for (i = 0; i < n_div; ++i) {
7688 k = isl_basic_map_alloc_inequality(bmap);
7689 if (k < 0)
7690 goto error;
7691 isl_seq_cpy(bmap->ineq[k], set->p[0]->div[i]+1, 1+n_param);
7692 isl_seq_clr(bmap->ineq[k]+1+n_param, n_set);
7693 isl_seq_cpy(bmap->ineq[k]+1+n_param+n_set,
7694 set->p[0]->div[i]+1+1+n_param, n_set + n_div);
7695 isl_int_neg(bmap->ineq[k][1+n_param+n_set+n_set+i],
7696 set->p[0]->div[i][0]);
7698 l = isl_basic_map_alloc_inequality(bmap);
7699 if (l < 0)
7700 goto error;
7701 isl_seq_neg(bmap->ineq[l], bmap->ineq[k], 1 + total);
7702 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0],
7703 set->p[0]->div[i][0]);
7704 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
7707 isl_set_free(set);
7708 bmap = isl_basic_map_simplify(bmap);
7709 bmap = isl_basic_map_finalize(bmap);
7710 return isl_map_from_basic_map(bmap);
7711 error:
7712 isl_set_free(set);
7713 isl_basic_map_free(bmap);
7714 return NULL;
7717 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
7719 unsigned dim;
7720 int size = 0;
7722 if (!bset)
7723 return -1;
7725 dim = isl_basic_set_total_dim(bset);
7726 size += bset->n_eq * (1 + dim);
7727 size += bset->n_ineq * (1 + dim);
7728 size += bset->n_div * (2 + dim);
7730 return size;
7733 int isl_set_size(__isl_keep isl_set *set)
7735 int i;
7736 int size = 0;
7738 if (!set)
7739 return -1;
7741 for (i = 0; i < set->n; ++i)
7742 size += isl_basic_set_size(set->p[i]);
7744 return size;
7747 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
7748 enum isl_dim_type type, unsigned pos)
7750 int i;
7751 int lower, upper;
7753 if (!bmap)
7754 return -1;
7756 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), return -1);
7758 pos += isl_basic_map_offset(bmap, type);
7760 for (i = 0; i < bmap->n_eq; ++i)
7761 if (!isl_int_is_zero(bmap->eq[i][pos]))
7762 return 1;
7764 lower = upper = 0;
7765 for (i = 0; i < bmap->n_ineq; ++i) {
7766 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
7767 if (sgn > 0)
7768 lower = 1;
7769 if (sgn < 0)
7770 upper = 1;
7773 return lower && upper;
7776 int isl_map_dim_is_bounded(__isl_keep isl_map *map,
7777 enum isl_dim_type type, unsigned pos)
7779 int i;
7781 if (!map)
7782 return -1;
7784 for (i = 0; i < map->n; ++i) {
7785 int bounded;
7786 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
7787 if (bounded < 0 || !bounded)
7788 return bounded;
7791 return 1;
7794 /* Return 1 if the specified dim is involved in both an upper bound
7795 * and a lower bound.
7797 int isl_set_dim_is_bounded(__isl_keep isl_set *set,
7798 enum isl_dim_type type, unsigned pos)
7800 return isl_map_dim_is_bounded((isl_map *)set, type, pos);
7803 /* For each of the "n" variables starting at "first", determine
7804 * the sign of the variable and put the results in the first "n"
7805 * elements of the array "signs".
7806 * Sign
7807 * 1 means that the variable is non-negative
7808 * -1 means that the variable is non-positive
7809 * 0 means the variable attains both positive and negative values.
7811 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
7812 unsigned first, unsigned n, int *signs)
7814 isl_vec *bound = NULL;
7815 struct isl_tab *tab = NULL;
7816 struct isl_tab_undo *snap;
7817 int i;
7819 if (!bset || !signs)
7820 return -1;
7822 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
7823 tab = isl_tab_from_basic_set(bset);
7824 if (!bound || !tab)
7825 goto error;
7827 isl_seq_clr(bound->el, bound->size);
7828 isl_int_set_si(bound->el[0], -1);
7830 snap = isl_tab_snap(tab);
7831 for (i = 0; i < n; ++i) {
7832 int empty;
7834 isl_int_set_si(bound->el[1 + first + i], -1);
7835 if (isl_tab_add_ineq(tab, bound->el) < 0)
7836 goto error;
7837 empty = tab->empty;
7838 isl_int_set_si(bound->el[1 + first + i], 0);
7839 if (isl_tab_rollback(tab, snap) < 0)
7840 goto error;
7842 if (empty) {
7843 signs[i] = 1;
7844 continue;
7847 isl_int_set_si(bound->el[1 + first + i], 1);
7848 if (isl_tab_add_ineq(tab, bound->el) < 0)
7849 goto error;
7850 empty = tab->empty;
7851 isl_int_set_si(bound->el[1 + first + i], 0);
7852 if (isl_tab_rollback(tab, snap) < 0)
7853 goto error;
7855 signs[i] = empty ? -1 : 0;
7858 isl_tab_free(tab);
7859 isl_vec_free(bound);
7860 return 0;
7861 error:
7862 isl_tab_free(tab);
7863 isl_vec_free(bound);
7864 return -1;
7867 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
7868 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
7870 if (!bset || !signs)
7871 return -1;
7872 isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
7873 return -1);
7875 first += pos(bset->dim, type) - 1;
7876 return isl_basic_set_vars_get_sign(bset, first, n, signs);
7879 /* Check if the given basic map is obviously single-valued.
7880 * In particular, for each output dimension, check that there is
7881 * an equality that defines the output dimension in terms of
7882 * earlier dimensions.
7884 int isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
7886 int i, j;
7887 unsigned total;
7888 unsigned n_out;
7889 unsigned o_out;
7891 if (!bmap)
7892 return -1;
7894 total = 1 + isl_basic_map_total_dim(bmap);
7895 n_out = isl_basic_map_dim(bmap, isl_dim_out);
7896 o_out = isl_basic_map_offset(bmap, isl_dim_out);
7898 for (i = 0; i < n_out; ++i) {
7899 for (j = 0; j < bmap->n_eq; ++j) {
7900 if (isl_int_is_zero(bmap->eq[j][o_out + i]))
7901 continue;
7902 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + i + 1,
7903 total - (o_out + i + 1)) == -1)
7904 break;
7906 if (j >= bmap->n_eq)
7907 return 0;
7910 return 1;
7913 /* Check if the given map is obviously single-valued.
7915 int isl_map_plain_is_single_valued(__isl_keep isl_map *map)
7917 if (!map)
7918 return -1;
7919 if (map->n == 0)
7920 return 1;
7921 if (map->n >= 2)
7922 return 0;
7924 return isl_basic_map_plain_is_single_valued(map->p[0]);
7927 /* Check if the given map is single-valued.
7928 * We simply compute
7930 * M \circ M^-1
7932 * and check if the result is a subset of the identity mapping.
7934 int isl_map_is_single_valued(__isl_keep isl_map *map)
7936 isl_dim *dim;
7937 isl_map *test;
7938 isl_map *id;
7939 int sv;
7941 sv = isl_map_plain_is_single_valued(map);
7942 if (sv < 0 || sv)
7943 return sv;
7945 test = isl_map_reverse(isl_map_copy(map));
7946 test = isl_map_apply_range(test, isl_map_copy(map));
7948 dim = isl_dim_map_from_set(isl_dim_range(isl_map_get_dim(map)));
7949 id = isl_map_identity(dim);
7951 sv = isl_map_is_subset(test, id);
7953 isl_map_free(test);
7954 isl_map_free(id);
7956 return sv;
7959 int isl_map_is_injective(__isl_keep isl_map *map)
7961 int in;
7963 map = isl_map_copy(map);
7964 map = isl_map_reverse(map);
7965 in = isl_map_is_single_valued(map);
7966 isl_map_free(map);
7968 return in;
7971 /* Check if the given map is obviously injective.
7973 int isl_map_plain_is_injective(__isl_keep isl_map *map)
7975 int in;
7977 map = isl_map_copy(map);
7978 map = isl_map_reverse(map);
7979 in = isl_map_plain_is_single_valued(map);
7980 isl_map_free(map);
7982 return in;
7985 int isl_map_is_bijective(__isl_keep isl_map *map)
7987 int sv;
7989 sv = isl_map_is_single_valued(map);
7990 if (sv < 0 || !sv)
7991 return sv;
7993 return isl_map_is_injective(map);
7996 int isl_set_is_singleton(__isl_keep isl_set *set)
7998 return isl_map_is_single_valued((isl_map *)set);
8001 int isl_map_is_translation(__isl_keep isl_map *map)
8003 int ok;
8004 isl_set *delta;
8006 delta = isl_map_deltas(isl_map_copy(map));
8007 ok = isl_set_is_singleton(delta);
8008 isl_set_free(delta);
8010 return ok;
8013 static int unique(isl_int *p, unsigned pos, unsigned len)
8015 if (isl_seq_first_non_zero(p, pos) != -1)
8016 return 0;
8017 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
8018 return 0;
8019 return 1;
8022 int isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
8024 int i, j;
8025 unsigned nvar;
8026 unsigned ovar;
8028 if (!bset)
8029 return -1;
8031 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
8032 return 0;
8034 nvar = isl_basic_set_dim(bset, isl_dim_set);
8035 ovar = isl_dim_offset(bset->dim, isl_dim_set);
8036 for (j = 0; j < nvar; ++j) {
8037 int lower = 0, upper = 0;
8038 for (i = 0; i < bset->n_eq; ++i) {
8039 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
8040 continue;
8041 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
8042 return 0;
8043 break;
8045 if (i < bset->n_eq)
8046 continue;
8047 for (i = 0; i < bset->n_ineq; ++i) {
8048 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
8049 continue;
8050 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
8051 return 0;
8052 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
8053 lower = 1;
8054 else
8055 upper = 1;
8057 if (!lower || !upper)
8058 return 0;
8061 return 1;
8064 int isl_set_is_box(__isl_keep isl_set *set)
8066 if (!set)
8067 return -1;
8068 if (set->n != 1)
8069 return 0;
8071 return isl_basic_set_is_box(set->p[0]);
8074 int isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
8076 if (!bset)
8077 return -1;
8079 return isl_dim_is_wrapping(bset->dim);
8082 int isl_set_is_wrapping(__isl_keep isl_set *set)
8084 if (!set)
8085 return -1;
8087 return isl_dim_is_wrapping(set->dim);
8090 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
8092 bmap = isl_basic_map_cow(bmap);
8093 if (!bmap)
8094 return NULL;
8096 bmap->dim = isl_dim_wrap(bmap->dim);
8097 if (!bmap->dim)
8098 goto error;
8100 bmap = isl_basic_map_finalize(bmap);
8102 return (isl_basic_set *)bmap;
8103 error:
8104 isl_basic_map_free(bmap);
8105 return NULL;
8108 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
8110 int i;
8112 map = isl_map_cow(map);
8113 if (!map)
8114 return NULL;
8116 for (i = 0; i < map->n; ++i) {
8117 map->p[i] = (isl_basic_map *)isl_basic_map_wrap(map->p[i]);
8118 if (!map->p[i])
8119 goto error;
8121 map->dim = isl_dim_wrap(map->dim);
8122 if (!map->dim)
8123 goto error;
8125 return (isl_set *)map;
8126 error:
8127 isl_map_free(map);
8128 return NULL;
8131 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
8133 bset = isl_basic_set_cow(bset);
8134 if (!bset)
8135 return NULL;
8137 bset->dim = isl_dim_unwrap(bset->dim);
8138 if (!bset->dim)
8139 goto error;
8141 bset = isl_basic_set_finalize(bset);
8143 return (isl_basic_map *)bset;
8144 error:
8145 isl_basic_set_free(bset);
8146 return NULL;
8149 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
8151 int i;
8153 if (!set)
8154 return NULL;
8156 if (!isl_set_is_wrapping(set))
8157 isl_die(set->ctx, isl_error_invalid, "not a wrapping set",
8158 goto error);
8160 set = isl_set_cow(set);
8161 if (!set)
8162 return NULL;
8164 for (i = 0; i < set->n; ++i) {
8165 set->p[i] = (isl_basic_set *)isl_basic_set_unwrap(set->p[i]);
8166 if (!set->p[i])
8167 goto error;
8170 set->dim = isl_dim_unwrap(set->dim);
8171 if (!set->dim)
8172 goto error;
8174 return (isl_map *)set;
8175 error:
8176 isl_set_free(set);
8177 return NULL;
8180 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
8181 enum isl_dim_type type)
8183 if (!bmap)
8184 return NULL;
8186 if (!isl_dim_is_named_or_nested(bmap->dim, type))
8187 return bmap;
8189 bmap = isl_basic_map_cow(bmap);
8190 if (!bmap)
8191 return NULL;
8193 bmap->dim = isl_dim_reset(bmap->dim, type);
8194 if (!bmap->dim)
8195 goto error;
8197 bmap = isl_basic_map_finalize(bmap);
8199 return bmap;
8200 error:
8201 isl_basic_map_free(bmap);
8202 return NULL;
8205 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
8206 enum isl_dim_type type)
8208 int i;
8210 if (!map)
8211 return NULL;
8213 if (!isl_dim_is_named_or_nested(map->dim, type))
8214 return map;
8216 map = isl_map_cow(map);
8217 if (!map)
8218 return NULL;
8220 for (i = 0; i < map->n; ++i) {
8221 map->p[i] = isl_basic_map_reset(map->p[i], type);
8222 if (!map->p[i])
8223 goto error;
8225 map->dim = isl_dim_reset(map->dim, type);
8226 if (!map->dim)
8227 goto error;
8229 return map;
8230 error:
8231 isl_map_free(map);
8232 return NULL;
8235 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
8237 if (!bmap)
8238 return NULL;
8240 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
8241 return bmap;
8243 bmap = isl_basic_map_cow(bmap);
8244 if (!bmap)
8245 return NULL;
8247 bmap->dim = isl_dim_flatten(bmap->dim);
8248 if (!bmap->dim)
8249 goto error;
8251 bmap = isl_basic_map_finalize(bmap);
8253 return bmap;
8254 error:
8255 isl_basic_map_free(bmap);
8256 return NULL;
8259 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
8261 return (isl_basic_set *)isl_basic_map_flatten((isl_basic_map *)bset);
8264 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
8266 int i;
8268 if (!map)
8269 return NULL;
8271 if (!map->dim->nested[0] && !map->dim->nested[1])
8272 return map;
8274 map = isl_map_cow(map);
8275 if (!map)
8276 return NULL;
8278 for (i = 0; i < map->n; ++i) {
8279 map->p[i] = isl_basic_map_flatten(map->p[i]);
8280 if (!map->p[i])
8281 goto error;
8283 map->dim = isl_dim_flatten(map->dim);
8284 if (!map->dim)
8285 goto error;
8287 return map;
8288 error:
8289 isl_map_free(map);
8290 return NULL;
8293 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
8295 return (isl_set *)isl_map_flatten((isl_map *)set);
8298 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
8300 isl_dim *dim, *flat_dim;
8301 isl_map *map;
8303 dim = isl_set_get_dim(set);
8304 flat_dim = isl_dim_flatten(isl_dim_copy(dim));
8305 map = isl_map_identity(isl_dim_join(isl_dim_reverse(dim), flat_dim));
8306 map = isl_map_intersect_domain(map, set);
8308 return map;
8311 /* Reorder the dimensions of "bmap" according to the given dim_map
8312 * and set the dimension specification to "dim".
8314 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
8315 __isl_take isl_dim *dim, __isl_take struct isl_dim_map *dim_map)
8317 isl_basic_map *res;
8319 bmap = isl_basic_map_cow(bmap);
8320 if (!bmap || !dim || !dim_map)
8321 goto error;
8323 res = isl_basic_map_alloc_dim(dim,
8324 bmap->n_div, bmap->n_eq, bmap->n_ineq);
8325 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
8326 res = isl_basic_map_finalize(res);
8327 return res;
8328 error:
8329 free(dim_map);
8330 isl_basic_map_free(bmap);
8331 isl_dim_free(dim);
8332 return NULL;
8335 /* Reorder the dimensions of "map" according to given reordering.
8337 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
8338 __isl_take isl_reordering *r)
8340 int i;
8341 struct isl_dim_map *dim_map;
8343 map = isl_map_cow(map);
8344 dim_map = isl_dim_map_from_reordering(r);
8345 if (!map || !r || !dim_map)
8346 goto error;
8348 for (i = 0; i < map->n; ++i) {
8349 struct isl_dim_map *dim_map_i;
8351 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
8353 map->p[i] = isl_basic_map_realign(map->p[i],
8354 isl_dim_copy(r->dim), dim_map_i);
8356 if (!map->p[i])
8357 goto error;
8360 map = isl_map_reset_dim(map, isl_dim_copy(r->dim));
8362 isl_reordering_free(r);
8363 free(dim_map);
8364 return map;
8365 error:
8366 free(dim_map);
8367 isl_map_free(map);
8368 isl_reordering_free(r);
8369 return NULL;
8372 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
8373 __isl_take isl_reordering *r)
8375 return (isl_set *)isl_map_realign((isl_map *)set, r);
8378 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
8379 __isl_take isl_dim *model)
8381 isl_ctx *ctx;
8383 if (!map || !model)
8384 goto error;
8386 ctx = isl_dim_get_ctx(model);
8387 if (!isl_dim_has_named_params(model))
8388 isl_die(ctx, isl_error_invalid,
8389 "model has unnamed parameters", goto error);
8390 if (!isl_dim_has_named_params(map->dim))
8391 isl_die(ctx, isl_error_invalid,
8392 "relation has unnamed parameters", goto error);
8393 if (!isl_dim_match(map->dim, isl_dim_param, model, isl_dim_param)) {
8394 isl_reordering *exp;
8396 model = isl_dim_drop(model, isl_dim_in,
8397 0, isl_dim_size(model, isl_dim_in));
8398 model = isl_dim_drop(model, isl_dim_out,
8399 0, isl_dim_size(model, isl_dim_out));
8400 exp = isl_parameter_alignment_reordering(map->dim, model);
8401 exp = isl_reordering_extend_dim(exp, isl_map_get_dim(map));
8402 map = isl_map_realign(map, exp);
8405 isl_dim_free(model);
8406 return map;
8407 error:
8408 isl_dim_free(model);
8409 isl_map_free(map);
8410 return NULL;
8413 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
8414 __isl_take isl_dim *model)
8416 return isl_map_align_params(set, model);
8419 __isl_give isl_mat *isl_basic_map_equalities_matrix(
8420 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
8421 enum isl_dim_type c2, enum isl_dim_type c3,
8422 enum isl_dim_type c4, enum isl_dim_type c5)
8424 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
8425 struct isl_mat *mat;
8426 int i, j, k;
8427 int pos;
8429 if (!bmap)
8430 return NULL;
8431 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
8432 isl_basic_map_total_dim(bmap) + 1);
8433 if (!mat)
8434 return NULL;
8435 for (i = 0; i < bmap->n_eq; ++i)
8436 for (j = 0, pos = 0; j < 5; ++j) {
8437 int off = isl_basic_map_offset(bmap, c[j]);
8438 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
8439 isl_int_set(mat->row[i][pos],
8440 bmap->eq[i][off + k]);
8441 ++pos;
8445 return mat;
8448 __isl_give isl_mat *isl_basic_map_inequalities_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_ineq,
8461 isl_basic_map_total_dim(bmap) + 1);
8462 if (!mat)
8463 return NULL;
8464 for (i = 0; i < bmap->n_ineq; ++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->ineq[i][off + k]);
8470 ++pos;
8474 return mat;
8477 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
8478 __isl_take isl_dim *dim,
8479 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
8480 enum isl_dim_type c2, enum isl_dim_type c3,
8481 enum isl_dim_type c4, enum isl_dim_type c5)
8483 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
8484 isl_basic_map *bmap;
8485 unsigned total;
8486 unsigned extra;
8487 int i, j, k, l;
8488 int pos;
8490 if (!dim || !eq || !ineq)
8491 goto error;
8493 if (eq->n_col != ineq->n_col)
8494 isl_die(dim->ctx, isl_error_invalid,
8495 "equalities and inequalities matrices should have "
8496 "same number of columns", goto error);
8498 total = 1 + isl_dim_total(dim);
8500 if (eq->n_col < total)
8501 isl_die(dim->ctx, isl_error_invalid,
8502 "number of columns too small", goto error);
8504 extra = eq->n_col - total;
8506 bmap = isl_basic_map_alloc_dim(isl_dim_copy(dim), extra,
8507 eq->n_row, ineq->n_row);
8508 if (!bmap)
8509 goto error;
8510 for (i = 0; i < extra; ++i) {
8511 k = isl_basic_map_alloc_div(bmap);
8512 if (k < 0)
8513 goto error;
8514 isl_int_set_si(bmap->div[k][0], 0);
8516 for (i = 0; i < eq->n_row; ++i) {
8517 l = isl_basic_map_alloc_equality(bmap);
8518 if (l < 0)
8519 goto error;
8520 for (j = 0, pos = 0; j < 5; ++j) {
8521 int off = isl_basic_map_offset(bmap, c[j]);
8522 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
8523 isl_int_set(bmap->eq[l][off + k],
8524 eq->row[i][pos]);
8525 ++pos;
8529 for (i = 0; i < ineq->n_row; ++i) {
8530 l = isl_basic_map_alloc_inequality(bmap);
8531 if (l < 0)
8532 goto error;
8533 for (j = 0, pos = 0; j < 5; ++j) {
8534 int off = isl_basic_map_offset(bmap, c[j]);
8535 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
8536 isl_int_set(bmap->ineq[l][off + k],
8537 ineq->row[i][pos]);
8538 ++pos;
8543 isl_dim_free(dim);
8544 isl_mat_free(eq);
8545 isl_mat_free(ineq);
8547 return bmap;
8548 error:
8549 isl_dim_free(dim);
8550 isl_mat_free(eq);
8551 isl_mat_free(ineq);
8552 return NULL;
8555 __isl_give isl_mat *isl_basic_set_equalities_matrix(
8556 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
8557 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
8559 return isl_basic_map_equalities_matrix((isl_basic_map *)bset,
8560 c1, c2, c3, c4, isl_dim_in);
8563 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
8564 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
8565 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
8567 return isl_basic_map_inequalities_matrix((isl_basic_map *)bset,
8568 c1, c2, c3, c4, isl_dim_in);
8571 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
8572 __isl_take isl_dim *dim,
8573 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
8574 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
8576 return (isl_basic_set*)
8577 isl_basic_map_from_constraint_matrices(dim, eq, ineq,
8578 c1, c2, c3, c4, isl_dim_in);
8581 int isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
8583 if (!bmap)
8584 return -1;
8586 return isl_dim_can_zip(bmap->dim);
8589 int isl_map_can_zip(__isl_keep isl_map *map)
8591 if (!map)
8592 return -1;
8594 return isl_dim_can_zip(map->dim);
8597 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
8598 * (A -> C) -> (B -> D).
8600 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
8602 unsigned pos;
8603 unsigned n1;
8604 unsigned n2;
8606 if (!bmap)
8607 return NULL;
8609 if (!isl_basic_map_can_zip(bmap))
8610 isl_die(bmap->ctx, isl_error_invalid,
8611 "basic map cannot be zipped", goto error);
8612 pos = isl_basic_map_offset(bmap, isl_dim_in) +
8613 isl_dim_size(bmap->dim->nested[0], isl_dim_in);
8614 n1 = isl_dim_size(bmap->dim->nested[0], isl_dim_out);
8615 n2 = isl_dim_size(bmap->dim->nested[1], isl_dim_in);
8616 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
8617 if (!bmap)
8618 return NULL;
8619 bmap->dim = isl_dim_zip(bmap->dim);
8620 if (!bmap->dim)
8621 goto error;
8622 return bmap;
8623 error:
8624 isl_basic_map_free(bmap);
8625 return NULL;
8628 /* Given a map (A -> B) -> (C -> D), return the corresponding map
8629 * (A -> C) -> (B -> D).
8631 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
8633 int i;
8635 if (!map)
8636 return NULL;
8638 if (!isl_map_can_zip(map))
8639 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
8640 goto error);
8642 map = isl_map_cow(map);
8643 if (!map)
8644 return NULL;
8646 for (i = 0; i < map->n; ++i) {
8647 map->p[i] = isl_basic_map_zip(map->p[i]);
8648 if (!map->p[i])
8649 goto error;
8652 map->dim = isl_dim_zip(map->dim);
8653 if (!map->dim)
8654 goto error;
8656 return map;
8657 error:
8658 isl_map_free(map);
8659 return NULL;