Merge branch 'maint'
[isl.git] / isl_map.c
blobfc2be4a161c1e88cf46120464944c7732efb7475
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
5 * Use of this software is governed by the GNU LGPLv2.1 license
7 * Written by Sven Verdoolaege, K.U.Leuven, Departement
8 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
9 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
10 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
13 #include <string.h>
14 #include <isl_ctx_private.h>
15 #include <isl_map_private.h>
16 #include <isl/blk.h>
17 #include "isl_dim_private.h"
18 #include "isl_equalities.h"
19 #include <isl_list_private.h>
20 #include <isl/lp.h>
21 #include <isl/seq.h>
22 #include <isl/set.h>
23 #include <isl/map.h>
24 #include "isl_map_piplib.h"
25 #include <isl_reordering.h>
26 #include "isl_sample.h"
27 #include "isl_tab.h"
28 #include <isl/vec.h>
29 #include <isl_mat_private.h>
30 #include <isl_dim_map.h>
31 #include <isl_local_space_private.h>
32 #include <isl_aff_private.h>
34 static unsigned n(struct isl_dim *dim, enum isl_dim_type type)
36 switch (type) {
37 case isl_dim_param: return dim->nparam;
38 case isl_dim_in: return dim->n_in;
39 case isl_dim_out: return dim->n_out;
40 case isl_dim_all: return dim->nparam + dim->n_in + dim->n_out;
41 default: return 0;
45 static unsigned pos(struct isl_dim *dim, enum isl_dim_type type)
47 switch (type) {
48 case isl_dim_param: return 1;
49 case isl_dim_in: return 1 + dim->nparam;
50 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
51 default: return 0;
55 unsigned isl_basic_map_dim(const struct isl_basic_map *bmap,
56 enum isl_dim_type type)
58 if (!bmap)
59 return 0;
60 switch (type) {
61 case isl_dim_cst: return 1;
62 case isl_dim_param:
63 case isl_dim_in:
64 case isl_dim_out: return isl_dim_size(bmap->dim, type);
65 case isl_dim_div: return bmap->n_div;
66 case isl_dim_all: return isl_basic_map_total_dim(bmap);
67 default: return 0;
71 unsigned isl_map_dim(const struct isl_map *map, enum isl_dim_type type)
73 return map ? n(map->dim, type) : 0;
76 unsigned isl_set_dim(const struct isl_set *set, enum isl_dim_type type)
78 return set ? n(set->dim, type) : 0;
81 unsigned isl_basic_map_offset(struct isl_basic_map *bmap,
82 enum isl_dim_type type)
84 struct isl_dim *dim = bmap->dim;
85 switch (type) {
86 case isl_dim_cst: return 0;
87 case isl_dim_param: return 1;
88 case isl_dim_in: return 1 + dim->nparam;
89 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
90 case isl_dim_div: return 1 + dim->nparam + dim->n_in + dim->n_out;
91 default: return 0;
95 unsigned isl_basic_set_offset(struct isl_basic_set *bset,
96 enum isl_dim_type type)
98 return isl_basic_map_offset(bset, type);
101 static unsigned map_offset(struct isl_map *map, enum isl_dim_type type)
103 return pos(map->dim, type);
106 unsigned isl_basic_set_dim(const struct isl_basic_set *bset,
107 enum isl_dim_type type)
109 return isl_basic_map_dim((const struct isl_basic_map*)bset, type);
112 unsigned isl_basic_set_n_dim(const struct isl_basic_set *bset)
114 return isl_basic_set_dim(bset, isl_dim_set);
117 unsigned isl_basic_set_n_param(const struct isl_basic_set *bset)
119 return isl_basic_set_dim(bset, isl_dim_param);
122 unsigned isl_basic_set_total_dim(const struct isl_basic_set *bset)
124 return isl_dim_total(bset->dim) + bset->n_div;
127 unsigned isl_set_n_dim(const struct isl_set *set)
129 return isl_set_dim(set, isl_dim_set);
132 unsigned isl_set_n_param(const struct isl_set *set)
134 return isl_set_dim(set, isl_dim_param);
137 unsigned isl_basic_map_n_in(const struct isl_basic_map *bmap)
139 return bmap ? bmap->dim->n_in : 0;
142 unsigned isl_basic_map_n_out(const struct isl_basic_map *bmap)
144 return bmap ? bmap->dim->n_out : 0;
147 unsigned isl_basic_map_n_param(const struct isl_basic_map *bmap)
149 return bmap ? bmap->dim->nparam : 0;
152 unsigned isl_basic_map_n_div(const struct isl_basic_map *bmap)
154 return bmap ? bmap->n_div : 0;
157 unsigned isl_basic_map_total_dim(const struct isl_basic_map *bmap)
159 return bmap ? isl_dim_total(bmap->dim) + bmap->n_div : 0;
162 unsigned isl_map_n_in(const struct isl_map *map)
164 return map->dim->n_in;
167 unsigned isl_map_n_out(const struct isl_map *map)
169 return map->dim->n_out;
172 unsigned isl_map_n_param(const struct isl_map *map)
174 return map->dim->nparam;
177 int isl_map_compatible_domain(struct isl_map *map, struct isl_set *set)
179 int m;
180 if (!map || !set)
181 return -1;
182 m = isl_dim_match(map->dim, isl_dim_param, set->dim, isl_dim_param);
183 if (m < 0 || !m)
184 return m;
185 return isl_dim_tuple_match(map->dim, isl_dim_in, set->dim, isl_dim_set);
188 int isl_basic_map_compatible_domain(struct isl_basic_map *bmap,
189 struct isl_basic_set *bset)
191 int m;
192 if (!bmap || !bset)
193 return -1;
194 m = isl_dim_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
195 if (m < 0 || !m)
196 return m;
197 return isl_dim_tuple_match(bmap->dim, isl_dim_in, bset->dim, isl_dim_set);
200 int isl_map_compatible_range(__isl_keep isl_map *map, __isl_keep isl_set *set)
202 int m;
203 if (!map || !set)
204 return -1;
205 m = isl_dim_match(map->dim, isl_dim_param, set->dim, isl_dim_param);
206 if (m < 0 || !m)
207 return m;
208 return isl_dim_tuple_match(map->dim, isl_dim_out, set->dim, isl_dim_set);
211 int isl_basic_map_compatible_range(struct isl_basic_map *bmap,
212 struct isl_basic_set *bset)
214 int m;
215 if (!bmap || !bset)
216 return -1;
217 m = isl_dim_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
218 if (m < 0 || !m)
219 return m;
220 return isl_dim_tuple_match(bmap->dim, isl_dim_out, bset->dim, isl_dim_set);
223 isl_ctx *isl_basic_map_get_ctx(__isl_keep isl_basic_map *bmap)
225 return bmap ? bmap->ctx : NULL;
228 isl_ctx *isl_basic_set_get_ctx(__isl_keep isl_basic_set *bset)
230 return bset ? bset->ctx : NULL;
233 isl_ctx *isl_map_get_ctx(__isl_keep isl_map *map)
235 return map ? map->ctx : NULL;
238 isl_ctx *isl_set_get_ctx(__isl_keep isl_set *set)
240 return set ? set->ctx : NULL;
243 struct isl_dim *isl_basic_map_get_dim(struct isl_basic_map *bmap)
245 if (!bmap)
246 return NULL;
247 return isl_dim_copy(bmap->dim);
250 struct isl_dim *isl_basic_set_get_dim(struct isl_basic_set *bset)
252 if (!bset)
253 return NULL;
254 return isl_dim_copy(bset->dim);
257 __isl_give isl_local_space *isl_basic_map_get_local_space(
258 __isl_keep isl_basic_map *bmap)
260 int i;
261 isl_local_space *ls;
262 unsigned total;
264 if (!bmap)
265 return NULL;
267 total = isl_basic_map_total_dim(bmap);
268 ls = isl_local_space_alloc(isl_dim_copy(bmap->dim), bmap->n_div);
269 if (!ls)
270 return NULL;
272 for (i = 0; i < bmap->n_div; ++i)
273 isl_seq_cpy(ls->div->row[i], bmap->div[i], 2 + total);
275 return ls;
278 __isl_give isl_local_space *isl_basic_set_get_local_space(
279 __isl_keep isl_basic_set *bset)
281 return isl_basic_map_get_local_space(bset);
284 __isl_give isl_basic_map *isl_basic_map_from_local_space(
285 __isl_take isl_local_space *ls)
287 int i;
288 int n_div;
289 isl_basic_map *bmap;
291 if (!ls)
292 return NULL;
294 n_div = isl_local_space_dim(ls, isl_dim_div);
295 bmap = isl_basic_map_alloc_dim(isl_local_space_get_dim(ls),
296 n_div, 0, 2 * n_div);
298 for (i = 0; i < n_div; ++i)
299 if (isl_basic_map_alloc_div(bmap) < 0)
300 goto error;
302 for (i = 0; i < n_div; ++i) {
303 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
304 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
305 goto error;
308 isl_local_space_free(ls);
309 return bmap;
310 error:
311 isl_local_space_free(ls);
312 isl_basic_map_free(bmap);
313 return NULL;
316 __isl_give isl_basic_set *isl_basic_set_from_local_space(
317 __isl_take isl_local_space *ls)
319 return isl_basic_map_from_local_space(ls);
322 struct isl_dim *isl_map_get_dim(struct isl_map *map)
324 if (!map)
325 return NULL;
326 return isl_dim_copy(map->dim);
329 struct isl_dim *isl_set_get_dim(struct isl_set *set)
331 if (!set)
332 return NULL;
333 return isl_dim_copy(set->dim);
336 __isl_give isl_basic_map *isl_basic_map_set_tuple_name(
337 __isl_take isl_basic_map *bmap, enum isl_dim_type type, const char *s)
339 bmap = isl_basic_map_cow(bmap);
340 if (!bmap)
341 return NULL;
342 bmap->dim = isl_dim_set_tuple_name(bmap->dim, type, s);
343 if (!bmap->dim)
344 goto error;
345 bmap = isl_basic_map_finalize(bmap);
346 return bmap;
347 error:
348 isl_basic_map_free(bmap);
349 return NULL;
352 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);
1496 if (!bmap)
1497 return NULL;
1498 i = bmap->n_div;
1501 return bmap;
1502 error:
1503 isl_basic_map_free(bmap);
1504 return NULL;
1507 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
1508 enum isl_dim_type type, unsigned first, unsigned n)
1510 int i;
1512 if (!map)
1513 return NULL;
1514 if (map->n == 0)
1515 return map;
1517 map = isl_map_cow(map);
1518 if (!map)
1519 return NULL;
1521 for (i = 0; i < map->n; ++i) {
1522 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
1523 type, first, n);
1524 if (!map->p[i])
1525 goto error;
1527 return map;
1528 error:
1529 isl_map_free(map);
1530 return NULL;
1533 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
1534 enum isl_dim_type type, unsigned first, unsigned n)
1536 return (isl_set *)isl_map_remove_divs_involving_dims((isl_map *)set,
1537 type, first, n);
1540 int isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
1541 enum isl_dim_type type, unsigned first, unsigned n)
1543 int i;
1545 if (!bset)
1546 return -1;
1548 if (first + n > isl_basic_set_dim(bset, type))
1549 isl_die(bset->ctx, isl_error_invalid,
1550 "index out of bounds", return -1);
1552 first += isl_basic_set_offset(bset, type);
1553 for (i = 0; i < bset->n_eq; ++i)
1554 if (isl_seq_first_non_zero(bset->eq[i] + first, n) >= 0)
1555 return 1;
1556 for (i = 0; i < bset->n_ineq; ++i)
1557 if (isl_seq_first_non_zero(bset->ineq[i] + first, n) >= 0)
1558 return 1;
1560 return 0;
1563 int isl_set_involves_dims(__isl_keep isl_set *set,
1564 enum isl_dim_type type, unsigned first, unsigned n)
1566 int i;
1568 if (!set)
1569 return -1;
1571 if (first + n > isl_set_dim(set, type))
1572 isl_die(set->ctx, isl_error_invalid,
1573 "index out of bounds", return -1);
1575 for (i = 0; i < set->n; ++i) {
1576 int involves = isl_basic_set_involves_dims(set->p[i],
1577 type, first, n);
1578 if (involves < 0 || !involves)
1579 return involves;
1582 return 1;
1585 /* Return true if the definition of the given div is unknown or depends
1586 * on unknown divs.
1588 static int div_is_unknown(__isl_keep isl_basic_map *bmap, int div)
1590 int i;
1591 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
1593 if (isl_int_is_zero(bmap->div[div][0]))
1594 return 1;
1596 for (i = bmap->n_div - 1; i >= 0; --i) {
1597 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
1598 continue;
1599 if (div_is_unknown(bmap, i))
1600 return 1;
1603 return 0;
1606 /* Remove all divs that are unknown or defined in terms of unknown divs.
1608 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
1609 __isl_take isl_basic_map *bmap)
1611 int i;
1613 if (!bmap)
1614 return NULL;
1616 for (i = bmap->n_div - 1; i >= 0; --i) {
1617 if (!div_is_unknown(bmap, i))
1618 continue;
1619 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
1622 return bmap;
1625 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
1627 int i;
1629 if (!map)
1630 return NULL;
1631 if (map->n == 0)
1632 return map;
1634 map = isl_map_cow(map);
1635 if (!map)
1636 return NULL;
1638 for (i = 0; i < map->n; ++i) {
1639 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
1640 if (!map->p[i])
1641 goto error;
1643 return map;
1644 error:
1645 isl_map_free(map);
1646 return NULL;
1649 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
1651 return (isl_set *)isl_map_remove_unknown_divs((isl_map *)set);
1654 __isl_give isl_basic_set *isl_basic_set_remove_dims(
1655 __isl_take isl_basic_set *bset,
1656 enum isl_dim_type type, unsigned first, unsigned n)
1658 return (isl_basic_set *)
1659 isl_basic_map_remove_dims((isl_basic_map *)bset, type, first, n);
1662 struct isl_map *isl_map_remove_dims(struct isl_map *map,
1663 enum isl_dim_type type, unsigned first, unsigned n)
1665 int i;
1667 if (n == 0)
1668 return map;
1670 map = isl_map_cow(map);
1671 if (!map)
1672 return NULL;
1673 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
1675 for (i = 0; i < map->n; ++i) {
1676 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
1677 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
1678 if (!map->p[i])
1679 goto error;
1681 map = isl_map_drop(map, type, first, n);
1682 return map;
1683 error:
1684 isl_map_free(map);
1685 return NULL;
1688 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
1689 enum isl_dim_type type, unsigned first, unsigned n)
1691 return (isl_set *)isl_map_remove_dims((isl_map *)bset, type, first, n);
1694 /* Project out n inputs starting at first using Fourier-Motzkin */
1695 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
1696 unsigned first, unsigned n)
1698 return isl_map_remove_dims(map, isl_dim_in, first, n);
1701 static void dump_term(struct isl_basic_map *bmap,
1702 isl_int c, int pos, FILE *out)
1704 const char *name;
1705 unsigned in = isl_basic_map_n_in(bmap);
1706 unsigned dim = in + isl_basic_map_n_out(bmap);
1707 unsigned nparam = isl_basic_map_n_param(bmap);
1708 if (!pos)
1709 isl_int_print(out, c, 0);
1710 else {
1711 if (!isl_int_is_one(c))
1712 isl_int_print(out, c, 0);
1713 if (pos < 1 + nparam) {
1714 name = isl_dim_get_name(bmap->dim,
1715 isl_dim_param, pos - 1);
1716 if (name)
1717 fprintf(out, "%s", name);
1718 else
1719 fprintf(out, "p%d", pos - 1);
1720 } else if (pos < 1 + nparam + in)
1721 fprintf(out, "i%d", pos - 1 - nparam);
1722 else if (pos < 1 + nparam + dim)
1723 fprintf(out, "o%d", pos - 1 - nparam - in);
1724 else
1725 fprintf(out, "e%d", pos - 1 - nparam - dim);
1729 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
1730 int sign, FILE *out)
1732 int i;
1733 int first;
1734 unsigned len = 1 + isl_basic_map_total_dim(bmap);
1735 isl_int v;
1737 isl_int_init(v);
1738 for (i = 0, first = 1; i < len; ++i) {
1739 if (isl_int_sgn(c[i]) * sign <= 0)
1740 continue;
1741 if (!first)
1742 fprintf(out, " + ");
1743 first = 0;
1744 isl_int_abs(v, c[i]);
1745 dump_term(bmap, v, i, out);
1747 isl_int_clear(v);
1748 if (first)
1749 fprintf(out, "0");
1752 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
1753 const char *op, FILE *out, int indent)
1755 int i;
1757 fprintf(out, "%*s", indent, "");
1759 dump_constraint_sign(bmap, c, 1, out);
1760 fprintf(out, " %s ", op);
1761 dump_constraint_sign(bmap, c, -1, out);
1763 fprintf(out, "\n");
1765 for (i = bmap->n_div; i < bmap->extra; ++i) {
1766 if (isl_int_is_zero(c[1+isl_dim_total(bmap->dim)+i]))
1767 continue;
1768 fprintf(out, "%*s", indent, "");
1769 fprintf(out, "ERROR: unused div coefficient not zero\n");
1770 abort();
1774 static void dump_constraints(struct isl_basic_map *bmap,
1775 isl_int **c, unsigned n,
1776 const char *op, FILE *out, int indent)
1778 int i;
1780 for (i = 0; i < n; ++i)
1781 dump_constraint(bmap, c[i], op, out, indent);
1784 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
1786 int j;
1787 int first = 1;
1788 unsigned total = isl_basic_map_total_dim(bmap);
1790 for (j = 0; j < 1 + total; ++j) {
1791 if (isl_int_is_zero(exp[j]))
1792 continue;
1793 if (!first && isl_int_is_pos(exp[j]))
1794 fprintf(out, "+");
1795 dump_term(bmap, exp[j], j, out);
1796 first = 0;
1800 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
1802 int i;
1804 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
1805 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
1807 for (i = 0; i < bmap->n_div; ++i) {
1808 fprintf(out, "%*s", indent, "");
1809 fprintf(out, "e%d = [(", i);
1810 dump_affine(bmap, bmap->div[i]+1, out);
1811 fprintf(out, ")/");
1812 isl_int_print(out, bmap->div[i][0], 0);
1813 fprintf(out, "]\n");
1817 void isl_basic_set_print_internal(struct isl_basic_set *bset,
1818 FILE *out, int indent)
1820 if (!bset) {
1821 fprintf(out, "null basic set\n");
1822 return;
1825 fprintf(out, "%*s", indent, "");
1826 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
1827 bset->ref, bset->dim->nparam, bset->dim->n_out,
1828 bset->extra, bset->flags);
1829 dump((struct isl_basic_map *)bset, out, indent);
1832 void isl_basic_map_print_internal(struct isl_basic_map *bmap,
1833 FILE *out, int indent)
1835 if (!bmap) {
1836 fprintf(out, "null basic map\n");
1837 return;
1840 fprintf(out, "%*s", indent, "");
1841 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
1842 "flags: %x, n_name: %d\n",
1843 bmap->ref,
1844 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
1845 bmap->extra, bmap->flags, bmap->dim->n_name);
1846 dump(bmap, out, indent);
1849 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
1851 unsigned total;
1852 if (!bmap)
1853 return -1;
1854 total = isl_basic_map_total_dim(bmap);
1855 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1856 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
1857 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
1858 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1859 return 0;
1862 struct isl_set *isl_set_alloc_dim(struct isl_dim *dim, int n, unsigned flags)
1864 struct isl_set *set;
1866 if (!dim)
1867 return NULL;
1868 isl_assert(dim->ctx, dim->n_in == 0, goto error);
1869 isl_assert(dim->ctx, n >= 0, goto error);
1870 set = isl_alloc(dim->ctx, struct isl_set,
1871 sizeof(struct isl_set) +
1872 (n - 1) * sizeof(struct isl_basic_set *));
1873 if (!set)
1874 goto error;
1876 set->ctx = dim->ctx;
1877 isl_ctx_ref(set->ctx);
1878 set->ref = 1;
1879 set->size = n;
1880 set->n = 0;
1881 set->dim = dim;
1882 set->flags = flags;
1883 return set;
1884 error:
1885 isl_dim_free(dim);
1886 return NULL;
1889 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
1890 unsigned nparam, unsigned dim, int n, unsigned flags)
1892 struct isl_set *set;
1893 struct isl_dim *dims;
1895 dims = isl_dim_alloc(ctx, nparam, 0, dim);
1896 if (!dims)
1897 return NULL;
1899 set = isl_set_alloc_dim(dims, n, flags);
1900 return set;
1903 /* Make sure "map" has room for at least "n" more basic maps.
1905 struct isl_map *isl_map_grow(struct isl_map *map, int n)
1907 int i;
1908 struct isl_map *grown = NULL;
1910 if (!map)
1911 return NULL;
1912 isl_assert(map->ctx, n >= 0, goto error);
1913 if (map->n + n <= map->size)
1914 return map;
1915 grown = isl_map_alloc_dim(isl_map_get_dim(map), map->n + n, map->flags);
1916 if (!grown)
1917 goto error;
1918 for (i = 0; i < map->n; ++i) {
1919 grown->p[i] = isl_basic_map_copy(map->p[i]);
1920 if (!grown->p[i])
1921 goto error;
1922 grown->n++;
1924 isl_map_free(map);
1925 return grown;
1926 error:
1927 isl_map_free(grown);
1928 isl_map_free(map);
1929 return NULL;
1932 /* Make sure "set" has room for at least "n" more basic sets.
1934 struct isl_set *isl_set_grow(struct isl_set *set, int n)
1936 return (struct isl_set *)isl_map_grow((struct isl_map *)set, n);
1939 struct isl_set *isl_set_dup(struct isl_set *set)
1941 int i;
1942 struct isl_set *dup;
1944 if (!set)
1945 return NULL;
1947 dup = isl_set_alloc_dim(isl_dim_copy(set->dim), set->n, set->flags);
1948 if (!dup)
1949 return NULL;
1950 for (i = 0; i < set->n; ++i)
1951 dup = isl_set_add_basic_set(dup, isl_basic_set_copy(set->p[i]));
1952 return dup;
1955 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
1957 return isl_map_from_basic_map(bset);
1960 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
1962 struct isl_map *map;
1964 if (!bmap)
1965 return NULL;
1967 map = isl_map_alloc_dim(isl_dim_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
1968 return isl_map_add_basic_map(map, bmap);
1971 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
1972 __isl_take isl_basic_set *bset)
1974 return (struct isl_set *)isl_map_add_basic_map((struct isl_map *)set,
1975 (struct isl_basic_map *)bset);
1978 void isl_set_free(struct isl_set *set)
1980 int i;
1982 if (!set)
1983 return;
1985 if (--set->ref > 0)
1986 return;
1988 isl_ctx_deref(set->ctx);
1989 for (i = 0; i < set->n; ++i)
1990 isl_basic_set_free(set->p[i]);
1991 isl_dim_free(set->dim);
1992 free(set);
1995 void isl_set_print_internal(struct isl_set *set, FILE *out, int indent)
1997 int i;
1999 if (!set) {
2000 fprintf(out, "null set\n");
2001 return;
2004 fprintf(out, "%*s", indent, "");
2005 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
2006 set->ref, set->n, set->dim->nparam, set->dim->n_out,
2007 set->flags);
2008 for (i = 0; i < set->n; ++i) {
2009 fprintf(out, "%*s", indent, "");
2010 fprintf(out, "basic set %d:\n", i);
2011 isl_basic_set_print_internal(set->p[i], out, indent+4);
2015 void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
2017 int i;
2019 if (!map) {
2020 fprintf(out, "null map\n");
2021 return;
2024 fprintf(out, "%*s", indent, "");
2025 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2026 "flags: %x, n_name: %d\n",
2027 map->ref, map->n, map->dim->nparam, map->dim->n_in,
2028 map->dim->n_out, map->flags, map->dim->n_name);
2029 for (i = 0; i < map->n; ++i) {
2030 fprintf(out, "%*s", indent, "");
2031 fprintf(out, "basic map %d:\n", i);
2032 isl_basic_map_print_internal(map->p[i], out, indent+4);
2036 struct isl_basic_map *isl_basic_map_intersect_domain(
2037 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2039 struct isl_basic_map *bmap_domain;
2041 if (!bmap || !bset)
2042 goto error;
2044 isl_assert(bset->ctx, isl_dim_match(bmap->dim, isl_dim_param,
2045 bset->dim, isl_dim_param), goto error);
2047 if (isl_dim_size(bset->dim, isl_dim_set) != 0)
2048 isl_assert(bset->ctx,
2049 isl_basic_map_compatible_domain(bmap, bset), goto error);
2051 bmap = isl_basic_map_cow(bmap);
2052 if (!bmap)
2053 goto error;
2054 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
2055 bset->n_div, bset->n_eq, bset->n_ineq);
2056 bmap_domain = isl_basic_map_from_domain(bset);
2057 bmap = add_constraints(bmap, bmap_domain, 0, 0);
2059 bmap = isl_basic_map_simplify(bmap);
2060 return isl_basic_map_finalize(bmap);
2061 error:
2062 isl_basic_map_free(bmap);
2063 isl_basic_set_free(bset);
2064 return NULL;
2067 struct isl_basic_map *isl_basic_map_intersect_range(
2068 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2070 struct isl_basic_map *bmap_range;
2072 if (!bmap || !bset)
2073 goto error;
2075 isl_assert(bset->ctx, isl_dim_match(bmap->dim, isl_dim_param,
2076 bset->dim, isl_dim_param), goto error);
2078 if (isl_dim_size(bset->dim, isl_dim_set) != 0)
2079 isl_assert(bset->ctx,
2080 isl_basic_map_compatible_range(bmap, bset), goto error);
2082 if (isl_basic_set_is_universe(bset)) {
2083 isl_basic_set_free(bset);
2084 return bmap;
2087 bmap = isl_basic_map_cow(bmap);
2088 if (!bmap)
2089 goto error;
2090 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
2091 bset->n_div, bset->n_eq, bset->n_ineq);
2092 bmap_range = isl_basic_map_from_basic_set(bset, isl_dim_copy(bset->dim));
2093 bmap = add_constraints(bmap, bmap_range, 0, 0);
2095 bmap = isl_basic_map_simplify(bmap);
2096 return isl_basic_map_finalize(bmap);
2097 error:
2098 isl_basic_map_free(bmap);
2099 isl_basic_set_free(bset);
2100 return NULL;
2103 int isl_basic_map_contains(struct isl_basic_map *bmap, struct isl_vec *vec)
2105 int i;
2106 unsigned total;
2107 isl_int s;
2109 total = 1 + isl_basic_map_total_dim(bmap);
2110 if (total != vec->size)
2111 return -1;
2113 isl_int_init(s);
2115 for (i = 0; i < bmap->n_eq; ++i) {
2116 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
2117 if (!isl_int_is_zero(s)) {
2118 isl_int_clear(s);
2119 return 0;
2123 for (i = 0; i < bmap->n_ineq; ++i) {
2124 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
2125 if (isl_int_is_neg(s)) {
2126 isl_int_clear(s);
2127 return 0;
2131 isl_int_clear(s);
2133 return 1;
2136 int isl_basic_set_contains(struct isl_basic_set *bset, struct isl_vec *vec)
2138 return isl_basic_map_contains((struct isl_basic_map *)bset, vec);
2141 struct isl_basic_map *isl_basic_map_intersect(
2142 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2144 struct isl_vec *sample = NULL;
2146 if (!bmap1 || !bmap2)
2147 goto error;
2149 isl_assert(bmap1->ctx, isl_dim_match(bmap1->dim, isl_dim_param,
2150 bmap2->dim, isl_dim_param), goto error);
2151 if (isl_dim_total(bmap1->dim) ==
2152 isl_dim_size(bmap1->dim, isl_dim_param) &&
2153 isl_dim_total(bmap2->dim) !=
2154 isl_dim_size(bmap2->dim, isl_dim_param))
2155 return isl_basic_map_intersect(bmap2, bmap1);
2157 if (isl_dim_total(bmap2->dim) !=
2158 isl_dim_size(bmap2->dim, isl_dim_param))
2159 isl_assert(bmap1->ctx,
2160 isl_dim_equal(bmap1->dim, bmap2->dim), goto error);
2162 if (bmap1->sample &&
2163 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
2164 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
2165 sample = isl_vec_copy(bmap1->sample);
2166 else if (bmap2->sample &&
2167 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
2168 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
2169 sample = isl_vec_copy(bmap2->sample);
2171 bmap1 = isl_basic_map_cow(bmap1);
2172 if (!bmap1)
2173 goto error;
2174 bmap1 = isl_basic_map_extend_dim(bmap1, isl_dim_copy(bmap1->dim),
2175 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
2176 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
2178 if (!bmap1)
2179 isl_vec_free(sample);
2180 else if (sample) {
2181 isl_vec_free(bmap1->sample);
2182 bmap1->sample = sample;
2185 bmap1 = isl_basic_map_simplify(bmap1);
2186 return isl_basic_map_finalize(bmap1);
2187 error:
2188 if (sample)
2189 isl_vec_free(sample);
2190 isl_basic_map_free(bmap1);
2191 isl_basic_map_free(bmap2);
2192 return NULL;
2195 struct isl_basic_set *isl_basic_set_intersect(
2196 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
2198 return (struct isl_basic_set *)
2199 isl_basic_map_intersect(
2200 (struct isl_basic_map *)bset1,
2201 (struct isl_basic_map *)bset2);
2204 /* Special case of isl_map_intersect, where both map1 and map2
2205 * are convex, without any divs and such that either map1 or map2
2206 * contains a single constraint. This constraint is then simply
2207 * added to the other map.
2209 static __isl_give isl_map *map_intersect_add_constraint(
2210 __isl_take isl_map *map1, __isl_take isl_map *map2)
2212 isl_assert(map1->ctx, map1->n == 1, goto error);
2213 isl_assert(map2->ctx, map1->n == 1, goto error);
2214 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
2215 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
2217 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
2218 return isl_map_intersect(map2, map1);
2220 isl_assert(map2->ctx,
2221 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1, goto error);
2223 map1 = isl_map_cow(map1);
2224 if (!map1)
2225 goto error;
2226 if (isl_map_plain_is_empty(map1)) {
2227 isl_map_free(map2);
2228 return map1;
2230 map1->p[0] = isl_basic_map_cow(map1->p[0]);
2231 if (map2->p[0]->n_eq == 1)
2232 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
2233 else
2234 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
2235 map2->p[0]->ineq[0]);
2237 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
2238 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
2239 if (!map1->p[0])
2240 goto error;
2242 if (isl_basic_map_plain_is_empty(map1->p[0])) {
2243 isl_basic_map_free(map1->p[0]);
2244 map1->n = 0;
2247 isl_map_free(map2);
2249 return map1;
2250 error:
2251 isl_map_free(map1);
2252 isl_map_free(map2);
2253 return NULL;
2256 struct isl_map *isl_map_intersect(struct isl_map *map1, struct isl_map *map2)
2258 unsigned flags = 0;
2259 struct isl_map *result;
2260 int i, j;
2262 if (!map1 || !map2)
2263 goto error;
2265 if (isl_map_plain_is_empty(map1)) {
2266 isl_map_free(map2);
2267 return map1;
2269 if (isl_map_plain_is_empty(map2)) {
2270 isl_map_free(map1);
2271 return map2;
2274 if (map1->n == 1 && map2->n == 1 &&
2275 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
2276 isl_dim_equal(map1->dim, map2->dim) &&
2277 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
2278 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
2279 return map_intersect_add_constraint(map1, map2);
2280 isl_assert(map1->ctx, isl_dim_match(map1->dim, isl_dim_param,
2281 map2->dim, isl_dim_param), goto error);
2282 if (isl_dim_total(map1->dim) ==
2283 isl_dim_size(map1->dim, isl_dim_param) &&
2284 isl_dim_total(map2->dim) != isl_dim_size(map2->dim, isl_dim_param))
2285 return isl_map_intersect(map2, map1);
2287 if (isl_dim_total(map2->dim) != isl_dim_size(map2->dim, isl_dim_param))
2288 isl_assert(map1->ctx,
2289 isl_dim_equal(map1->dim, map2->dim), goto error);
2291 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
2292 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
2293 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
2295 result = isl_map_alloc_dim(isl_dim_copy(map1->dim),
2296 map1->n * map2->n, flags);
2297 if (!result)
2298 goto error;
2299 for (i = 0; i < map1->n; ++i)
2300 for (j = 0; j < map2->n; ++j) {
2301 struct isl_basic_map *part;
2302 part = isl_basic_map_intersect(
2303 isl_basic_map_copy(map1->p[i]),
2304 isl_basic_map_copy(map2->p[j]));
2305 if (isl_basic_map_is_empty(part))
2306 isl_basic_map_free(part);
2307 else
2308 result = isl_map_add_basic_map(result, part);
2309 if (!result)
2310 goto error;
2312 isl_map_free(map1);
2313 isl_map_free(map2);
2314 return result;
2315 error:
2316 isl_map_free(map1);
2317 isl_map_free(map2);
2318 return NULL;
2321 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
2323 return (struct isl_set *)
2324 isl_map_intersect((struct isl_map *)set1,
2325 (struct isl_map *)set2);
2328 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
2330 struct isl_dim *dim;
2331 struct isl_basic_set *bset;
2332 unsigned in;
2334 if (!bmap)
2335 return NULL;
2336 bmap = isl_basic_map_cow(bmap);
2337 if (!bmap)
2338 return NULL;
2339 dim = isl_dim_reverse(isl_dim_copy(bmap->dim));
2340 in = isl_basic_map_n_in(bmap);
2341 bset = isl_basic_set_from_basic_map(bmap);
2342 bset = isl_basic_set_swap_vars(bset, in);
2343 return isl_basic_map_from_basic_set(bset, dim);
2346 __isl_give isl_basic_map *isl_basic_map_insert(__isl_take isl_basic_map *bmap,
2347 enum isl_dim_type type, unsigned pos, unsigned n)
2349 struct isl_dim *res_dim;
2350 struct isl_basic_map *res;
2351 struct isl_dim_map *dim_map;
2352 unsigned total, off;
2353 enum isl_dim_type t;
2355 if (n == 0)
2356 return bmap;
2358 if (!bmap)
2359 return NULL;
2361 res_dim = isl_dim_insert(isl_basic_map_get_dim(bmap), type, pos, n);
2363 total = isl_basic_map_total_dim(bmap) + n;
2364 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2365 off = 0;
2366 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2367 if (t != type) {
2368 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2369 } else {
2370 unsigned size = isl_basic_map_dim(bmap, t);
2371 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2372 0, pos, off);
2373 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2374 pos, size - pos, off + pos + n);
2376 off += isl_dim_size(res_dim, t);
2378 isl_dim_map_div(dim_map, bmap, off);
2380 res = isl_basic_map_alloc_dim(res_dim,
2381 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2382 if (isl_basic_map_is_rational(bmap))
2383 res = isl_basic_map_set_rational(res);
2384 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
2385 return isl_basic_map_finalize(res);
2388 __isl_give isl_basic_map *isl_basic_map_add(__isl_take isl_basic_map *bmap,
2389 enum isl_dim_type type, unsigned n)
2391 if (!bmap)
2392 return NULL;
2393 return isl_basic_map_insert(bmap, type,
2394 isl_basic_map_dim(bmap, type), n);
2397 __isl_give isl_basic_set *isl_basic_set_add(__isl_take isl_basic_set *bset,
2398 enum isl_dim_type type, unsigned n)
2400 if (!bset)
2401 return NULL;
2402 isl_assert(bset->ctx, type != isl_dim_in, goto error);
2403 return (isl_basic_set *)isl_basic_map_add((isl_basic_map *)bset, type, n);
2404 error:
2405 isl_basic_set_free(bset);
2406 return NULL;
2409 __isl_give isl_map *isl_map_insert(__isl_take isl_map *map,
2410 enum isl_dim_type type, unsigned pos, unsigned n)
2412 int i;
2414 if (n == 0)
2415 return map;
2417 map = isl_map_cow(map);
2418 if (!map)
2419 return NULL;
2421 map->dim = isl_dim_insert(map->dim, type, pos, n);
2422 if (!map->dim)
2423 goto error;
2425 for (i = 0; i < map->n; ++i) {
2426 map->p[i] = isl_basic_map_insert(map->p[i], type, pos, n);
2427 if (!map->p[i])
2428 goto error;
2431 return map;
2432 error:
2433 isl_map_free(map);
2434 return NULL;
2437 __isl_give isl_set *isl_set_insert(__isl_take isl_set *set,
2438 enum isl_dim_type type, unsigned pos, unsigned n)
2440 return (isl_set *)isl_map_insert((isl_map *)set, type, pos, n);
2443 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
2444 enum isl_dim_type type, unsigned n)
2446 if (!map)
2447 return NULL;
2448 return isl_map_insert(map, type, isl_map_dim(map, type), n);
2451 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
2452 enum isl_dim_type type, unsigned n)
2454 if (!set)
2455 return NULL;
2456 isl_assert(set->ctx, type != isl_dim_in, goto error);
2457 return (isl_set *)isl_map_add_dims((isl_map *)set, type, n);
2458 error:
2459 isl_set_free(set);
2460 return NULL;
2463 __isl_give isl_basic_map *isl_basic_map_move_dims(
2464 __isl_take isl_basic_map *bmap,
2465 enum isl_dim_type dst_type, unsigned dst_pos,
2466 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2468 struct isl_dim_map *dim_map;
2469 struct isl_basic_map *res;
2470 enum isl_dim_type t;
2471 unsigned total, off;
2473 if (!bmap)
2474 return NULL;
2475 if (n == 0)
2476 return bmap;
2478 isl_assert(bmap->ctx, src_pos + n <= isl_basic_map_dim(bmap, src_type),
2479 goto error);
2481 if (dst_type == src_type && dst_pos == src_pos)
2482 return bmap;
2484 isl_assert(bmap->ctx, dst_type != src_type, goto error);
2486 if (pos(bmap->dim, dst_type) + dst_pos ==
2487 pos(bmap->dim, src_type) + src_pos +
2488 ((src_type < dst_type) ? n : 0)) {
2489 bmap = isl_basic_map_cow(bmap);
2490 if (!bmap)
2491 return NULL;
2493 bmap->dim = isl_dim_move(bmap->dim, dst_type, dst_pos,
2494 src_type, src_pos, n);
2495 if (!bmap->dim)
2496 goto error;
2498 bmap = isl_basic_map_finalize(bmap);
2500 return bmap;
2503 total = isl_basic_map_total_dim(bmap);
2504 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2506 off = 0;
2507 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2508 unsigned size = isl_dim_size(bmap->dim, t);
2509 if (t == dst_type) {
2510 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2511 0, dst_pos, off);
2512 off += dst_pos;
2513 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
2514 src_pos, n, off);
2515 off += n;
2516 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2517 dst_pos, size - dst_pos, off);
2518 off += size - dst_pos;
2519 } else if (t == src_type) {
2520 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2521 0, src_pos, off);
2522 off += src_pos;
2523 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2524 src_pos + n, size - src_pos - n, off);
2525 off += size - src_pos - n;
2526 } else {
2527 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2528 off += size;
2531 isl_dim_map_div(dim_map, bmap, off);
2533 res = isl_basic_map_alloc_dim(isl_basic_map_get_dim(bmap),
2534 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2535 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
2537 bmap->dim = isl_dim_move(bmap->dim, dst_type, dst_pos,
2538 src_type, src_pos, n);
2539 if (!bmap->dim)
2540 goto error;
2542 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2543 bmap = isl_basic_map_gauss(bmap, NULL);
2544 bmap = isl_basic_map_finalize(bmap);
2546 return bmap;
2547 error:
2548 isl_basic_map_free(bmap);
2549 return NULL;
2552 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
2553 enum isl_dim_type dst_type, unsigned dst_pos,
2554 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2556 return (isl_basic_set *)isl_basic_map_move_dims(
2557 (isl_basic_map *)bset, dst_type, dst_pos, src_type, src_pos, n);
2560 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
2561 enum isl_dim_type dst_type, unsigned dst_pos,
2562 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2564 if (!set)
2565 return NULL;
2566 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
2567 return (isl_set *)isl_map_move_dims((isl_map *)set, dst_type, dst_pos,
2568 src_type, src_pos, n);
2569 error:
2570 isl_set_free(set);
2571 return NULL;
2574 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
2575 enum isl_dim_type dst_type, unsigned dst_pos,
2576 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2578 int i;
2580 if (!map)
2581 return NULL;
2582 if (n == 0)
2583 return map;
2585 isl_assert(map->ctx, src_pos + n <= isl_map_dim(map, src_type),
2586 goto error);
2588 if (dst_type == src_type && dst_pos == src_pos)
2589 return map;
2591 isl_assert(map->ctx, dst_type != src_type, goto error);
2593 map = isl_map_cow(map);
2594 if (!map)
2595 return NULL;
2597 map->dim = isl_dim_move(map->dim, dst_type, dst_pos, src_type, src_pos, n);
2598 if (!map->dim)
2599 goto error;
2601 for (i = 0; i < map->n; ++i) {
2602 map->p[i] = isl_basic_map_move_dims(map->p[i],
2603 dst_type, dst_pos,
2604 src_type, src_pos, n);
2605 if (!map->p[i])
2606 goto error;
2609 return map;
2610 error:
2611 isl_map_free(map);
2612 return NULL;
2615 /* Move the specified dimensions to the last columns right before
2616 * the divs. Don't change the dimension specification of bmap.
2617 * That's the responsibility of the caller.
2619 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
2620 enum isl_dim_type type, unsigned first, unsigned n)
2622 struct isl_dim_map *dim_map;
2623 struct isl_basic_map *res;
2624 enum isl_dim_type t;
2625 unsigned total, off;
2627 if (!bmap)
2628 return NULL;
2629 if (pos(bmap->dim, type) + first + n == 1 + isl_dim_total(bmap->dim))
2630 return bmap;
2632 total = isl_basic_map_total_dim(bmap);
2633 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2635 off = 0;
2636 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2637 unsigned size = isl_dim_size(bmap->dim, t);
2638 if (t == type) {
2639 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2640 0, first, off);
2641 off += first;
2642 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2643 first, n, total - bmap->n_div - n);
2644 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2645 first + n, size - (first + n), off);
2646 off += size - (first + n);
2647 } else {
2648 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2649 off += size;
2652 isl_dim_map_div(dim_map, bmap, off + n);
2654 res = isl_basic_map_alloc_dim(isl_basic_map_get_dim(bmap),
2655 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2656 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
2657 return res;
2660 /* Turn the n dimensions of type type, starting at first
2661 * into existentially quantified variables.
2663 __isl_give isl_basic_map *isl_basic_map_project_out(
2664 __isl_take isl_basic_map *bmap,
2665 enum isl_dim_type type, unsigned first, unsigned n)
2667 int i;
2668 size_t row_size;
2669 isl_int **new_div;
2670 isl_int *old;
2672 if (n == 0)
2673 return bmap;
2675 if (!bmap)
2676 return NULL;
2678 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
2679 return isl_basic_map_remove_dims(bmap, type, first, n);
2681 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
2682 goto error);
2684 bmap = move_last(bmap, type, first, n);
2685 bmap = isl_basic_map_cow(bmap);
2686 if (!bmap)
2687 return NULL;
2689 row_size = 1 + isl_dim_total(bmap->dim) + bmap->extra;
2690 old = bmap->block2.data;
2691 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
2692 (bmap->extra + n) * (1 + row_size));
2693 if (!bmap->block2.data)
2694 goto error;
2695 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
2696 if (!new_div)
2697 goto error;
2698 for (i = 0; i < n; ++i) {
2699 new_div[i] = bmap->block2.data +
2700 (bmap->extra + i) * (1 + row_size);
2701 isl_seq_clr(new_div[i], 1 + row_size);
2703 for (i = 0; i < bmap->extra; ++i)
2704 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
2705 free(bmap->div);
2706 bmap->div = new_div;
2707 bmap->n_div += n;
2708 bmap->extra += n;
2710 bmap->dim = isl_dim_drop(bmap->dim, type, first, n);
2711 if (!bmap->dim)
2712 goto error;
2713 bmap = isl_basic_map_simplify(bmap);
2714 bmap = isl_basic_map_drop_redundant_divs(bmap);
2715 return isl_basic_map_finalize(bmap);
2716 error:
2717 isl_basic_map_free(bmap);
2718 return NULL;
2721 /* Turn the n dimensions of type type, starting at first
2722 * into existentially quantified variables.
2724 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
2725 enum isl_dim_type type, unsigned first, unsigned n)
2727 return (isl_basic_set *)isl_basic_map_project_out(
2728 (isl_basic_map *)bset, type, first, n);
2731 /* Turn the n dimensions of type type, starting at first
2732 * into existentially quantified variables.
2734 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
2735 enum isl_dim_type type, unsigned first, unsigned n)
2737 int i;
2739 if (!map)
2740 return NULL;
2742 if (n == 0)
2743 return map;
2745 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
2747 map = isl_map_cow(map);
2748 if (!map)
2749 return NULL;
2751 map->dim = isl_dim_drop(map->dim, type, first, n);
2752 if (!map->dim)
2753 goto error;
2755 for (i = 0; i < map->n; ++i) {
2756 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
2757 if (!map->p[i])
2758 goto error;
2761 return map;
2762 error:
2763 isl_map_free(map);
2764 return NULL;
2767 /* Turn the n dimensions of type type, starting at first
2768 * into existentially quantified variables.
2770 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
2771 enum isl_dim_type type, unsigned first, unsigned n)
2773 return (isl_set *)isl_map_project_out((isl_map *)set, type, first, n);
2776 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
2778 int i, j;
2780 for (i = 0; i < n; ++i) {
2781 j = isl_basic_map_alloc_div(bmap);
2782 if (j < 0)
2783 goto error;
2784 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
2786 return bmap;
2787 error:
2788 isl_basic_map_free(bmap);
2789 return NULL;
2792 struct isl_basic_map *isl_basic_map_apply_range(
2793 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2795 struct isl_dim *dim_result = NULL;
2796 struct isl_basic_map *bmap;
2797 unsigned n_in, n_out, n, nparam, total, pos;
2798 struct isl_dim_map *dim_map1, *dim_map2;
2800 if (!bmap1 || !bmap2)
2801 goto error;
2803 dim_result = isl_dim_join(isl_dim_copy(bmap1->dim),
2804 isl_dim_copy(bmap2->dim));
2806 n_in = isl_basic_map_n_in(bmap1);
2807 n_out = isl_basic_map_n_out(bmap2);
2808 n = isl_basic_map_n_out(bmap1);
2809 nparam = isl_basic_map_n_param(bmap1);
2811 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
2812 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
2813 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
2814 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
2815 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
2816 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
2817 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
2818 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
2819 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
2820 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
2821 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
2823 bmap = isl_basic_map_alloc_dim(dim_result,
2824 bmap1->n_div + bmap2->n_div + n,
2825 bmap1->n_eq + bmap2->n_eq,
2826 bmap1->n_ineq + bmap2->n_ineq);
2827 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
2828 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
2829 bmap = add_divs(bmap, n);
2830 bmap = isl_basic_map_simplify(bmap);
2831 bmap = isl_basic_map_drop_redundant_divs(bmap);
2832 return isl_basic_map_finalize(bmap);
2833 error:
2834 isl_basic_map_free(bmap1);
2835 isl_basic_map_free(bmap2);
2836 return NULL;
2839 struct isl_basic_set *isl_basic_set_apply(
2840 struct isl_basic_set *bset, struct isl_basic_map *bmap)
2842 if (!bset || !bmap)
2843 goto error;
2845 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
2846 goto error);
2848 return (struct isl_basic_set *)
2849 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
2850 error:
2851 isl_basic_set_free(bset);
2852 isl_basic_map_free(bmap);
2853 return NULL;
2856 struct isl_basic_map *isl_basic_map_apply_domain(
2857 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2859 if (!bmap1 || !bmap2)
2860 goto error;
2862 isl_assert(bmap1->ctx,
2863 isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
2864 isl_assert(bmap1->ctx,
2865 isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
2866 goto error);
2868 bmap1 = isl_basic_map_reverse(bmap1);
2869 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
2870 return isl_basic_map_reverse(bmap1);
2871 error:
2872 isl_basic_map_free(bmap1);
2873 isl_basic_map_free(bmap2);
2874 return NULL;
2877 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
2878 * A \cap B -> f(A) + f(B)
2880 struct isl_basic_map *isl_basic_map_sum(
2881 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2883 unsigned n_in, n_out, nparam, total, pos;
2884 struct isl_basic_map *bmap = NULL;
2885 struct isl_dim_map *dim_map1, *dim_map2;
2886 int i;
2888 if (!bmap1 || !bmap2)
2889 goto error;
2891 isl_assert(bmap1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim),
2892 goto error);
2894 nparam = isl_basic_map_n_param(bmap1);
2895 n_in = isl_basic_map_n_in(bmap1);
2896 n_out = isl_basic_map_n_out(bmap1);
2898 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
2899 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
2900 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
2901 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
2902 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
2903 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
2904 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
2905 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
2906 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
2907 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
2908 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
2910 bmap = isl_basic_map_alloc_dim(isl_dim_copy(bmap1->dim),
2911 bmap1->n_div + bmap2->n_div + 2 * n_out,
2912 bmap1->n_eq + bmap2->n_eq + n_out,
2913 bmap1->n_ineq + bmap2->n_ineq);
2914 for (i = 0; i < n_out; ++i) {
2915 int j = isl_basic_map_alloc_equality(bmap);
2916 if (j < 0)
2917 goto error;
2918 isl_seq_clr(bmap->eq[j], 1+total);
2919 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
2920 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
2921 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
2923 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
2924 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
2925 bmap = add_divs(bmap, 2 * n_out);
2927 bmap = isl_basic_map_simplify(bmap);
2928 return isl_basic_map_finalize(bmap);
2929 error:
2930 isl_basic_map_free(bmap);
2931 isl_basic_map_free(bmap1);
2932 isl_basic_map_free(bmap2);
2933 return NULL;
2936 /* Given two maps A -> f(A) and B -> g(B), construct a map
2937 * A \cap B -> f(A) + f(B)
2939 struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
2941 struct isl_map *result;
2942 int i, j;
2944 if (!map1 || !map2)
2945 goto error;
2947 isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
2949 result = isl_map_alloc_dim(isl_dim_copy(map1->dim),
2950 map1->n * map2->n, 0);
2951 if (!result)
2952 goto error;
2953 for (i = 0; i < map1->n; ++i)
2954 for (j = 0; j < map2->n; ++j) {
2955 struct isl_basic_map *part;
2956 part = isl_basic_map_sum(
2957 isl_basic_map_copy(map1->p[i]),
2958 isl_basic_map_copy(map2->p[j]));
2959 if (isl_basic_map_is_empty(part))
2960 isl_basic_map_free(part);
2961 else
2962 result = isl_map_add_basic_map(result, part);
2963 if (!result)
2964 goto error;
2966 isl_map_free(map1);
2967 isl_map_free(map2);
2968 return result;
2969 error:
2970 isl_map_free(map1);
2971 isl_map_free(map2);
2972 return NULL;
2975 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
2976 __isl_take isl_set *set2)
2978 return (isl_set *)isl_map_sum((isl_map *)set1, (isl_map *)set2);
2981 /* Given a basic map A -> f(A), construct A -> -f(A).
2983 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
2985 int i, j;
2986 unsigned off, n;
2988 bmap = isl_basic_map_cow(bmap);
2989 if (!bmap)
2990 return NULL;
2992 n = isl_basic_map_dim(bmap, isl_dim_out);
2993 off = isl_basic_map_offset(bmap, isl_dim_out);
2994 for (i = 0; i < bmap->n_eq; ++i)
2995 for (j = 0; j < n; ++j)
2996 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
2997 for (i = 0; i < bmap->n_ineq; ++i)
2998 for (j = 0; j < n; ++j)
2999 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
3000 for (i = 0; i < bmap->n_div; ++i)
3001 for (j = 0; j < n; ++j)
3002 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
3003 return isl_basic_map_finalize(bmap);
3006 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
3008 return isl_basic_map_neg(bset);
3011 /* Given a map A -> f(A), construct A -> -f(A).
3013 struct isl_map *isl_map_neg(struct isl_map *map)
3015 int i;
3017 map = isl_map_cow(map);
3018 if (!map)
3019 return NULL;
3021 for (i = 0; i < map->n; ++i) {
3022 map->p[i] = isl_basic_map_neg(map->p[i]);
3023 if (!map->p[i])
3024 goto error;
3027 return map;
3028 error:
3029 isl_map_free(map);
3030 return NULL;
3033 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
3035 return (isl_set *)isl_map_neg((isl_map *)set);
3038 /* Given a basic map A -> f(A) and an integer d, construct a basic map
3039 * A -> floor(f(A)/d).
3041 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
3042 isl_int d)
3044 unsigned n_in, n_out, nparam, total, pos;
3045 struct isl_basic_map *result = NULL;
3046 struct isl_dim_map *dim_map;
3047 int i;
3049 if (!bmap)
3050 return NULL;
3052 nparam = isl_basic_map_n_param(bmap);
3053 n_in = isl_basic_map_n_in(bmap);
3054 n_out = isl_basic_map_n_out(bmap);
3056 total = nparam + n_in + n_out + bmap->n_div + n_out;
3057 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3058 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
3059 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
3060 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
3061 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
3063 result = isl_basic_map_alloc_dim(isl_dim_copy(bmap->dim),
3064 bmap->n_div + n_out,
3065 bmap->n_eq, bmap->n_ineq + 2 * n_out);
3066 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
3067 result = add_divs(result, n_out);
3068 for (i = 0; i < n_out; ++i) {
3069 int j;
3070 j = isl_basic_map_alloc_inequality(result);
3071 if (j < 0)
3072 goto error;
3073 isl_seq_clr(result->ineq[j], 1+total);
3074 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
3075 isl_int_set_si(result->ineq[j][1+pos+i], 1);
3076 j = isl_basic_map_alloc_inequality(result);
3077 if (j < 0)
3078 goto error;
3079 isl_seq_clr(result->ineq[j], 1+total);
3080 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
3081 isl_int_set_si(result->ineq[j][1+pos+i], -1);
3082 isl_int_sub_ui(result->ineq[j][0], d, 1);
3085 result = isl_basic_map_simplify(result);
3086 return isl_basic_map_finalize(result);
3087 error:
3088 isl_basic_map_free(result);
3089 return NULL;
3092 /* Given a map A -> f(A) and an integer d, construct a map
3093 * A -> floor(f(A)/d).
3095 struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
3097 int i;
3099 map = isl_map_cow(map);
3100 if (!map)
3101 return NULL;
3103 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3104 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3105 for (i = 0; i < map->n; ++i) {
3106 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
3107 if (!map->p[i])
3108 goto error;
3111 return map;
3112 error:
3113 isl_map_free(map);
3114 return NULL;
3117 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
3119 int i;
3120 unsigned nparam;
3121 unsigned n_in;
3123 i = isl_basic_map_alloc_equality(bmap);
3124 if (i < 0)
3125 goto error;
3126 nparam = isl_basic_map_n_param(bmap);
3127 n_in = isl_basic_map_n_in(bmap);
3128 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
3129 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
3130 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
3131 return isl_basic_map_finalize(bmap);
3132 error:
3133 isl_basic_map_free(bmap);
3134 return NULL;
3137 /* Add a constraints to "bmap" expressing i_pos < o_pos
3139 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
3141 int i;
3142 unsigned nparam;
3143 unsigned n_in;
3145 i = isl_basic_map_alloc_inequality(bmap);
3146 if (i < 0)
3147 goto error;
3148 nparam = isl_basic_map_n_param(bmap);
3149 n_in = isl_basic_map_n_in(bmap);
3150 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3151 isl_int_set_si(bmap->ineq[i][0], -1);
3152 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
3153 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
3154 return isl_basic_map_finalize(bmap);
3155 error:
3156 isl_basic_map_free(bmap);
3157 return NULL;
3160 /* Add a constraint to "bmap" expressing i_pos <= o_pos
3162 static __isl_give isl_basic_map *var_less_or_equal(
3163 __isl_take isl_basic_map *bmap, unsigned pos)
3165 int i;
3166 unsigned nparam;
3167 unsigned n_in;
3169 i = isl_basic_map_alloc_inequality(bmap);
3170 if (i < 0)
3171 goto error;
3172 nparam = isl_basic_map_n_param(bmap);
3173 n_in = isl_basic_map_n_in(bmap);
3174 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3175 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
3176 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
3177 return isl_basic_map_finalize(bmap);
3178 error:
3179 isl_basic_map_free(bmap);
3180 return NULL;
3183 /* Add a constraints to "bmap" expressing i_pos > o_pos
3185 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
3187 int i;
3188 unsigned nparam;
3189 unsigned n_in;
3191 i = isl_basic_map_alloc_inequality(bmap);
3192 if (i < 0)
3193 goto error;
3194 nparam = isl_basic_map_n_param(bmap);
3195 n_in = isl_basic_map_n_in(bmap);
3196 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3197 isl_int_set_si(bmap->ineq[i][0], -1);
3198 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
3199 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
3200 return isl_basic_map_finalize(bmap);
3201 error:
3202 isl_basic_map_free(bmap);
3203 return NULL;
3206 /* Add a constraint to "bmap" expressing i_pos >= o_pos
3208 static __isl_give isl_basic_map *var_more_or_equal(
3209 __isl_take isl_basic_map *bmap, unsigned pos)
3211 int i;
3212 unsigned nparam;
3213 unsigned n_in;
3215 i = isl_basic_map_alloc_inequality(bmap);
3216 if (i < 0)
3217 goto error;
3218 nparam = isl_basic_map_n_param(bmap);
3219 n_in = isl_basic_map_n_in(bmap);
3220 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3221 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
3222 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
3223 return isl_basic_map_finalize(bmap);
3224 error:
3225 isl_basic_map_free(bmap);
3226 return NULL;
3229 struct isl_basic_map *isl_basic_map_equal(struct isl_dim *dim, unsigned n_equal)
3231 int i;
3232 struct isl_basic_map *bmap;
3233 bmap = isl_basic_map_alloc_dim(dim, 0, n_equal, 0);
3234 if (!bmap)
3235 return NULL;
3236 for (i = 0; i < n_equal && bmap; ++i)
3237 bmap = var_equal(bmap, i);
3238 return isl_basic_map_finalize(bmap);
3241 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
3243 struct isl_basic_map *isl_basic_map_less_at(struct isl_dim *dim, unsigned pos)
3245 int i;
3246 struct isl_basic_map *bmap;
3247 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
3248 if (!bmap)
3249 return NULL;
3250 for (i = 0; i < pos && bmap; ++i)
3251 bmap = var_equal(bmap, i);
3252 if (bmap)
3253 bmap = var_less(bmap, pos);
3254 return isl_basic_map_finalize(bmap);
3257 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
3259 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
3260 __isl_take isl_dim *dim, unsigned pos)
3262 int i;
3263 isl_basic_map *bmap;
3265 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
3266 for (i = 0; i < pos; ++i)
3267 bmap = var_equal(bmap, i);
3268 bmap = var_less_or_equal(bmap, pos);
3269 return isl_basic_map_finalize(bmap);
3272 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
3274 struct isl_basic_map *isl_basic_map_more_at(struct isl_dim *dim, unsigned pos)
3276 int i;
3277 struct isl_basic_map *bmap;
3278 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
3279 if (!bmap)
3280 return NULL;
3281 for (i = 0; i < pos && bmap; ++i)
3282 bmap = var_equal(bmap, i);
3283 if (bmap)
3284 bmap = var_more(bmap, pos);
3285 return isl_basic_map_finalize(bmap);
3288 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
3290 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
3291 __isl_take isl_dim *dim, unsigned pos)
3293 int i;
3294 isl_basic_map *bmap;
3296 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
3297 for (i = 0; i < pos; ++i)
3298 bmap = var_equal(bmap, i);
3299 bmap = var_more_or_equal(bmap, pos);
3300 return isl_basic_map_finalize(bmap);
3303 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_dim *dims,
3304 unsigned n, int equal)
3306 struct isl_map *map;
3307 int i;
3309 if (n == 0 && equal)
3310 return isl_map_universe(dims);
3312 map = isl_map_alloc_dim(isl_dim_copy(dims), n, ISL_MAP_DISJOINT);
3314 for (i = 0; i + 1 < n; ++i)
3315 map = isl_map_add_basic_map(map,
3316 isl_basic_map_less_at(isl_dim_copy(dims), i));
3317 if (n > 0) {
3318 if (equal)
3319 map = isl_map_add_basic_map(map,
3320 isl_basic_map_less_or_equal_at(dims, n - 1));
3321 else
3322 map = isl_map_add_basic_map(map,
3323 isl_basic_map_less_at(dims, n - 1));
3324 } else
3325 isl_dim_free(dims);
3327 return map;
3330 static __isl_give isl_map *map_lex_lte(__isl_take isl_dim *dims, int equal)
3332 if (!dims)
3333 return NULL;
3334 return map_lex_lte_first(dims, dims->n_out, equal);
3337 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_dim *dim, unsigned n)
3339 return map_lex_lte_first(dim, n, 0);
3342 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_dim *dim, unsigned n)
3344 return map_lex_lte_first(dim, n, 1);
3347 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_dim *set_dim)
3349 return map_lex_lte(isl_dim_map_from_set(set_dim), 0);
3352 __isl_give isl_map *isl_map_lex_le(__isl_take isl_dim *set_dim)
3354 return map_lex_lte(isl_dim_map_from_set(set_dim), 1);
3357 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_dim *dims,
3358 unsigned n, int equal)
3360 struct isl_map *map;
3361 int i;
3363 if (n == 0 && equal)
3364 return isl_map_universe(dims);
3366 map = isl_map_alloc_dim(isl_dim_copy(dims), n, ISL_MAP_DISJOINT);
3368 for (i = 0; i + 1 < n; ++i)
3369 map = isl_map_add_basic_map(map,
3370 isl_basic_map_more_at(isl_dim_copy(dims), i));
3371 if (n > 0) {
3372 if (equal)
3373 map = isl_map_add_basic_map(map,
3374 isl_basic_map_more_or_equal_at(dims, n - 1));
3375 else
3376 map = isl_map_add_basic_map(map,
3377 isl_basic_map_more_at(dims, n - 1));
3378 } else
3379 isl_dim_free(dims);
3381 return map;
3384 static __isl_give isl_map *map_lex_gte(__isl_take isl_dim *dims, int equal)
3386 if (!dims)
3387 return NULL;
3388 return map_lex_gte_first(dims, dims->n_out, equal);
3391 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_dim *dim, unsigned n)
3393 return map_lex_gte_first(dim, n, 0);
3396 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_dim *dim, unsigned n)
3398 return map_lex_gte_first(dim, n, 1);
3401 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_dim *set_dim)
3403 return map_lex_gte(isl_dim_map_from_set(set_dim), 0);
3406 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_dim *set_dim)
3408 return map_lex_gte(isl_dim_map_from_set(set_dim), 1);
3411 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
3412 __isl_take isl_set *set2)
3414 isl_map *map;
3415 map = isl_map_lex_le(isl_set_get_dim(set1));
3416 map = isl_map_intersect_domain(map, set1);
3417 map = isl_map_intersect_range(map, set2);
3418 return map;
3421 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
3422 __isl_take isl_set *set2)
3424 isl_map *map;
3425 map = isl_map_lex_lt(isl_set_get_dim(set1));
3426 map = isl_map_intersect_domain(map, set1);
3427 map = isl_map_intersect_range(map, set2);
3428 return map;
3431 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
3432 __isl_take isl_set *set2)
3434 isl_map *map;
3435 map = isl_map_lex_ge(isl_set_get_dim(set1));
3436 map = isl_map_intersect_domain(map, set1);
3437 map = isl_map_intersect_range(map, set2);
3438 return map;
3441 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
3442 __isl_take isl_set *set2)
3444 isl_map *map;
3445 map = isl_map_lex_gt(isl_set_get_dim(set1));
3446 map = isl_map_intersect_domain(map, set1);
3447 map = isl_map_intersect_range(map, set2);
3448 return map;
3451 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
3452 __isl_take isl_map *map2)
3454 isl_map *map;
3455 map = isl_map_lex_le(isl_dim_range(isl_map_get_dim(map1)));
3456 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3457 map = isl_map_apply_range(map, isl_map_reverse(map2));
3458 return map;
3461 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
3462 __isl_take isl_map *map2)
3464 isl_map *map;
3465 map = isl_map_lex_lt(isl_dim_range(isl_map_get_dim(map1)));
3466 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3467 map = isl_map_apply_range(map, isl_map_reverse(map2));
3468 return map;
3471 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
3472 __isl_take isl_map *map2)
3474 isl_map *map;
3475 map = isl_map_lex_ge(isl_dim_range(isl_map_get_dim(map1)));
3476 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3477 map = isl_map_apply_range(map, isl_map_reverse(map2));
3478 return map;
3481 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
3482 __isl_take isl_map *map2)
3484 isl_map *map;
3485 map = isl_map_lex_gt(isl_dim_range(isl_map_get_dim(map1)));
3486 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3487 map = isl_map_apply_range(map, isl_map_reverse(map2));
3488 return map;
3491 struct isl_basic_map *isl_basic_map_from_basic_set(
3492 struct isl_basic_set *bset, struct isl_dim *dim)
3494 struct isl_basic_map *bmap;
3496 bset = isl_basic_set_cow(bset);
3497 if (!bset || !dim)
3498 goto error;
3500 isl_assert(bset->ctx, isl_dim_compatible(bset->dim, dim), goto error);
3501 isl_dim_free(bset->dim);
3502 bmap = (struct isl_basic_map *) bset;
3503 bmap->dim = dim;
3504 return isl_basic_map_finalize(bmap);
3505 error:
3506 isl_basic_set_free(bset);
3507 isl_dim_free(dim);
3508 return NULL;
3511 struct isl_basic_set *isl_basic_set_from_basic_map(struct isl_basic_map *bmap)
3513 if (!bmap)
3514 goto error;
3515 if (bmap->dim->n_in == 0)
3516 return (struct isl_basic_set *)bmap;
3517 bmap = isl_basic_map_cow(bmap);
3518 if (!bmap)
3519 goto error;
3520 bmap->dim = isl_dim_as_set_dim(bmap->dim);
3521 if (!bmap->dim)
3522 goto error;
3523 bmap = isl_basic_map_finalize(bmap);
3524 return (struct isl_basic_set *)bmap;
3525 error:
3526 isl_basic_map_free(bmap);
3527 return NULL;
3530 /* For a div d = floor(f/m), add the constraints
3532 * f - m d >= 0
3533 * -(f-(n-1)) + m d >= 0
3535 * Note that the second constraint is the negation of
3537 * f - m d >= n
3539 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
3540 unsigned pos, isl_int *div)
3542 int i, j;
3543 unsigned total = isl_basic_map_total_dim(bmap);
3545 i = isl_basic_map_alloc_inequality(bmap);
3546 if (i < 0)
3547 return -1;
3548 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
3549 isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
3551 j = isl_basic_map_alloc_inequality(bmap);
3552 if (j < 0)
3553 return -1;
3554 isl_seq_neg(bmap->ineq[j], bmap->ineq[i], 1 + total);
3555 isl_int_add(bmap->ineq[j][0], bmap->ineq[j][0], bmap->ineq[j][1 + pos]);
3556 isl_int_sub_ui(bmap->ineq[j][0], bmap->ineq[j][0], 1);
3557 return j;
3560 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
3561 unsigned pos, isl_int *div)
3563 return isl_basic_map_add_div_constraints_var((isl_basic_map *)bset,
3564 pos, div);
3567 int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
3569 unsigned total = isl_basic_map_total_dim(bmap);
3570 unsigned div_pos = total - bmap->n_div + div;
3572 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
3573 bmap->div[div]);
3576 struct isl_basic_set *isl_basic_map_underlying_set(
3577 struct isl_basic_map *bmap)
3579 if (!bmap)
3580 goto error;
3581 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
3582 bmap->n_div == 0 &&
3583 !isl_dim_is_named_or_nested(bmap->dim, isl_dim_in) &&
3584 !isl_dim_is_named_or_nested(bmap->dim, isl_dim_out))
3585 return (struct isl_basic_set *)bmap;
3586 bmap = isl_basic_map_cow(bmap);
3587 if (!bmap)
3588 goto error;
3589 bmap->dim = isl_dim_underlying(bmap->dim, bmap->n_div);
3590 if (!bmap->dim)
3591 goto error;
3592 bmap->extra -= bmap->n_div;
3593 bmap->n_div = 0;
3594 bmap = isl_basic_map_finalize(bmap);
3595 return (struct isl_basic_set *)bmap;
3596 error:
3597 return NULL;
3600 __isl_give isl_basic_set *isl_basic_set_underlying_set(
3601 __isl_take isl_basic_set *bset)
3603 return isl_basic_map_underlying_set((isl_basic_map *)bset);
3606 struct isl_basic_map *isl_basic_map_overlying_set(
3607 struct isl_basic_set *bset, struct isl_basic_map *like)
3609 struct isl_basic_map *bmap;
3610 struct isl_ctx *ctx;
3611 unsigned total;
3612 int i;
3614 if (!bset || !like)
3615 goto error;
3616 ctx = bset->ctx;
3617 isl_assert(ctx, bset->n_div == 0, goto error);
3618 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
3619 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
3620 goto error);
3621 if (isl_dim_equal(bset->dim, like->dim) && like->n_div == 0) {
3622 isl_basic_map_free(like);
3623 return (struct isl_basic_map *)bset;
3625 bset = isl_basic_set_cow(bset);
3626 if (!bset)
3627 goto error;
3628 total = bset->dim->n_out + bset->extra;
3629 bmap = (struct isl_basic_map *)bset;
3630 isl_dim_free(bmap->dim);
3631 bmap->dim = isl_dim_copy(like->dim);
3632 if (!bmap->dim)
3633 goto error;
3634 bmap->n_div = like->n_div;
3635 bmap->extra += like->n_div;
3636 if (bmap->extra) {
3637 unsigned ltotal;
3638 isl_int **div;
3639 ltotal = total - bmap->extra + like->extra;
3640 if (ltotal > total)
3641 ltotal = total;
3642 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
3643 bmap->extra * (1 + 1 + total));
3644 if (isl_blk_is_error(bmap->block2))
3645 goto error;
3646 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
3647 if (!div)
3648 goto error;
3649 bmap->div = div;
3650 for (i = 0; i < bmap->extra; ++i)
3651 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
3652 for (i = 0; i < like->n_div; ++i) {
3653 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
3654 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
3656 bmap = isl_basic_map_extend_constraints(bmap,
3657 0, 2 * like->n_div);
3658 for (i = 0; i < like->n_div; ++i) {
3659 if (isl_int_is_zero(bmap->div[i][0]))
3660 continue;
3661 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
3662 goto error;
3665 isl_basic_map_free(like);
3666 bmap = isl_basic_map_simplify(bmap);
3667 bmap = isl_basic_map_finalize(bmap);
3668 return bmap;
3669 error:
3670 isl_basic_map_free(like);
3671 isl_basic_set_free(bset);
3672 return NULL;
3675 struct isl_basic_set *isl_basic_set_from_underlying_set(
3676 struct isl_basic_set *bset, struct isl_basic_set *like)
3678 return (struct isl_basic_set *)
3679 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
3682 struct isl_set *isl_set_from_underlying_set(
3683 struct isl_set *set, struct isl_basic_set *like)
3685 int i;
3687 if (!set || !like)
3688 goto error;
3689 isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
3690 goto error);
3691 if (isl_dim_equal(set->dim, like->dim) && like->n_div == 0) {
3692 isl_basic_set_free(like);
3693 return set;
3695 set = isl_set_cow(set);
3696 if (!set)
3697 goto error;
3698 for (i = 0; i < set->n; ++i) {
3699 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
3700 isl_basic_set_copy(like));
3701 if (!set->p[i])
3702 goto error;
3704 isl_dim_free(set->dim);
3705 set->dim = isl_dim_copy(like->dim);
3706 if (!set->dim)
3707 goto error;
3708 isl_basic_set_free(like);
3709 return set;
3710 error:
3711 isl_basic_set_free(like);
3712 isl_set_free(set);
3713 return NULL;
3716 struct isl_set *isl_map_underlying_set(struct isl_map *map)
3718 int i;
3720 map = isl_map_cow(map);
3721 if (!map)
3722 return NULL;
3723 map->dim = isl_dim_cow(map->dim);
3724 if (!map->dim)
3725 goto error;
3727 for (i = 1; i < map->n; ++i)
3728 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
3729 goto error);
3730 for (i = 0; i < map->n; ++i) {
3731 map->p[i] = (struct isl_basic_map *)
3732 isl_basic_map_underlying_set(map->p[i]);
3733 if (!map->p[i])
3734 goto error;
3736 if (map->n == 0)
3737 map->dim = isl_dim_underlying(map->dim, 0);
3738 else {
3739 isl_dim_free(map->dim);
3740 map->dim = isl_dim_copy(map->p[0]->dim);
3742 if (!map->dim)
3743 goto error;
3744 return (struct isl_set *)map;
3745 error:
3746 isl_map_free(map);
3747 return NULL;
3750 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
3752 return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
3755 __isl_give isl_basic_map *isl_basic_map_reset_dim(
3756 __isl_take isl_basic_map *bmap, __isl_take isl_dim *dim)
3758 bmap = isl_basic_map_cow(bmap);
3759 if (!bmap || !dim)
3760 goto error;
3762 isl_dim_free(bmap->dim);
3763 bmap->dim = dim;
3765 bmap = isl_basic_map_finalize(bmap);
3767 return bmap;
3768 error:
3769 isl_basic_map_free(bmap);
3770 isl_dim_free(dim);
3771 return NULL;
3774 __isl_give isl_basic_set *isl_basic_set_reset_dim(
3775 __isl_take isl_basic_set *bset, __isl_take isl_dim *dim)
3777 return (isl_basic_set *)isl_basic_map_reset_dim((isl_basic_map *)bset,
3778 dim);
3781 __isl_give isl_map *isl_map_reset_dim(__isl_take isl_map *map,
3782 __isl_take isl_dim *dim)
3784 int i;
3786 map = isl_map_cow(map);
3787 if (!map || !dim)
3788 goto error;
3790 for (i = 0; i < map->n; ++i) {
3791 map->p[i] = isl_basic_map_reset_dim(map->p[i],
3792 isl_dim_copy(dim));
3793 if (!map->p[i])
3794 goto error;
3796 isl_dim_free(map->dim);
3797 map->dim = dim;
3799 return map;
3800 error:
3801 isl_map_free(map);
3802 isl_dim_free(dim);
3803 return NULL;
3806 __isl_give isl_set *isl_set_reset_dim(__isl_take isl_set *set,
3807 __isl_take isl_dim *dim)
3809 return (struct isl_set *) isl_map_reset_dim((struct isl_map *)set, dim);
3812 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
3814 isl_dim *dim;
3815 struct isl_basic_set *domain;
3816 unsigned n_in;
3817 unsigned n_out;
3819 if (!bmap)
3820 return NULL;
3821 dim = isl_dim_domain(isl_basic_map_get_dim(bmap));
3823 n_in = isl_basic_map_n_in(bmap);
3824 n_out = isl_basic_map_n_out(bmap);
3825 domain = isl_basic_set_from_basic_map(bmap);
3826 domain = isl_basic_set_project_out(domain, isl_dim_set, n_in, n_out);
3828 domain = isl_basic_set_reset_dim(domain, dim);
3830 return domain;
3833 int isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
3835 if (!bmap)
3836 return -1;
3837 return isl_dim_may_be_set(bmap->dim);
3840 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
3842 if (!bmap)
3843 return NULL;
3844 if (isl_basic_map_may_be_set(bmap))
3845 return bmap;
3846 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
3849 __isl_give isl_basic_map *isl_basic_map_domain_map(
3850 __isl_take isl_basic_map *bmap)
3852 int i, k;
3853 isl_dim *dim;
3854 isl_basic_map *domain;
3855 int nparam, n_in, n_out;
3856 unsigned total;
3858 nparam = isl_basic_map_dim(bmap, isl_dim_param);
3859 n_in = isl_basic_map_dim(bmap, isl_dim_in);
3860 n_out = isl_basic_map_dim(bmap, isl_dim_out);
3862 dim = isl_dim_from_range(isl_dim_domain(isl_basic_map_get_dim(bmap)));
3863 domain = isl_basic_map_universe(dim);
3865 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
3866 bmap = isl_basic_map_apply_range(bmap, domain);
3867 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
3869 total = isl_basic_map_total_dim(bmap);
3871 for (i = 0; i < n_in; ++i) {
3872 k = isl_basic_map_alloc_equality(bmap);
3873 if (k < 0)
3874 goto error;
3875 isl_seq_clr(bmap->eq[k], 1 + total);
3876 isl_int_set_si(bmap->eq[k][1 + nparam + i], -1);
3877 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
3880 bmap = isl_basic_map_gauss(bmap, NULL);
3881 return isl_basic_map_finalize(bmap);
3882 error:
3883 isl_basic_map_free(bmap);
3884 return NULL;
3887 __isl_give isl_basic_map *isl_basic_map_range_map(
3888 __isl_take isl_basic_map *bmap)
3890 int i, k;
3891 isl_dim *dim;
3892 isl_basic_map *range;
3893 int nparam, n_in, n_out;
3894 unsigned total;
3896 nparam = isl_basic_map_dim(bmap, isl_dim_param);
3897 n_in = isl_basic_map_dim(bmap, isl_dim_in);
3898 n_out = isl_basic_map_dim(bmap, isl_dim_out);
3900 dim = isl_dim_from_range(isl_dim_range(isl_basic_map_get_dim(bmap)));
3901 range = isl_basic_map_universe(dim);
3903 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
3904 bmap = isl_basic_map_apply_range(bmap, range);
3905 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
3907 total = isl_basic_map_total_dim(bmap);
3909 for (i = 0; i < n_out; ++i) {
3910 k = isl_basic_map_alloc_equality(bmap);
3911 if (k < 0)
3912 goto error;
3913 isl_seq_clr(bmap->eq[k], 1 + total);
3914 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + i], -1);
3915 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
3918 bmap = isl_basic_map_gauss(bmap, NULL);
3919 return isl_basic_map_finalize(bmap);
3920 error:
3921 isl_basic_map_free(bmap);
3922 return NULL;
3925 int isl_map_may_be_set(__isl_keep isl_map *map)
3927 if (!map)
3928 return -1;
3929 return isl_dim_may_be_set(map->dim);
3932 struct isl_set *isl_map_range(struct isl_map *map)
3934 int i;
3935 struct isl_set *set;
3937 if (!map)
3938 goto error;
3939 if (isl_map_may_be_set(map))
3940 return (isl_set *)map;
3942 map = isl_map_cow(map);
3943 if (!map)
3944 goto error;
3946 set = (struct isl_set *) map;
3947 set->dim = isl_dim_drop_inputs(set->dim, 0, set->dim->n_in);
3948 if (!set->dim)
3949 goto error;
3950 for (i = 0; i < map->n; ++i) {
3951 set->p[i] = isl_basic_map_range(map->p[i]);
3952 if (!set->p[i])
3953 goto error;
3955 ISL_F_CLR(set, ISL_MAP_DISJOINT);
3956 ISL_F_CLR(set, ISL_SET_NORMALIZED);
3957 return set;
3958 error:
3959 isl_map_free(map);
3960 return NULL;
3963 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
3965 int i;
3966 isl_dim *domain_dim;
3968 map = isl_map_cow(map);
3969 if (!map)
3970 return NULL;
3972 domain_dim = isl_dim_from_range(isl_dim_domain(isl_map_get_dim(map)));
3973 map->dim = isl_dim_from_domain(isl_dim_wrap(map->dim));
3974 map->dim = isl_dim_join(map->dim, domain_dim);
3975 if (!map->dim)
3976 goto error;
3977 for (i = 0; i < map->n; ++i) {
3978 map->p[i] = isl_basic_map_domain_map(map->p[i]);
3979 if (!map->p[i])
3980 goto error;
3982 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3983 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3984 return map;
3985 error:
3986 isl_map_free(map);
3987 return NULL;
3990 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
3992 int i;
3993 isl_dim *range_dim;
3995 map = isl_map_cow(map);
3996 if (!map)
3997 return NULL;
3999 range_dim = isl_dim_range(isl_map_get_dim(map));
4000 map->dim = isl_dim_from_domain(isl_dim_wrap(map->dim));
4001 map->dim = isl_dim_join(map->dim, range_dim);
4002 if (!map->dim)
4003 goto error;
4004 for (i = 0; i < map->n; ++i) {
4005 map->p[i] = isl_basic_map_range_map(map->p[i]);
4006 if (!map->p[i])
4007 goto error;
4009 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4010 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4011 return map;
4012 error:
4013 isl_map_free(map);
4014 return NULL;
4017 struct isl_map *isl_map_from_set(struct isl_set *set, struct isl_dim *dim)
4019 int i;
4020 struct isl_map *map = NULL;
4022 set = isl_set_cow(set);
4023 if (!set || !dim)
4024 goto error;
4025 isl_assert(set->ctx, isl_dim_compatible(set->dim, dim), goto error);
4026 map = (struct isl_map *)set;
4027 for (i = 0; i < set->n; ++i) {
4028 map->p[i] = isl_basic_map_from_basic_set(
4029 set->p[i], isl_dim_copy(dim));
4030 if (!map->p[i])
4031 goto error;
4033 isl_dim_free(map->dim);
4034 map->dim = dim;
4035 return map;
4036 error:
4037 isl_dim_free(dim);
4038 isl_set_free(set);
4039 return NULL;
4042 __isl_give isl_basic_map *isl_basic_map_from_domain(
4043 __isl_take isl_basic_set *bset)
4045 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
4048 __isl_give isl_basic_map *isl_basic_map_from_range(
4049 __isl_take isl_basic_set *bset)
4051 return (isl_basic_map *)bset;
4054 struct isl_map *isl_map_from_range(struct isl_set *set)
4056 return (struct isl_map *)set;
4059 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
4061 return isl_map_reverse(isl_map_from_range(set));
4064 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
4065 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
4067 return isl_basic_map_apply_range(isl_basic_map_from_domain(domain),
4068 isl_basic_map_from_range(range));
4071 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
4072 __isl_take isl_set *range)
4074 return isl_map_apply_range(isl_map_from_domain(domain),
4075 isl_map_from_range(range));
4078 struct isl_set *isl_set_from_map(struct isl_map *map)
4080 int i;
4081 struct isl_set *set = NULL;
4083 if (!map)
4084 return NULL;
4085 map = isl_map_cow(map);
4086 if (!map)
4087 return NULL;
4088 map->dim = isl_dim_as_set_dim(map->dim);
4089 if (!map->dim)
4090 goto error;
4091 set = (struct isl_set *)map;
4092 for (i = 0; i < map->n; ++i) {
4093 set->p[i] = isl_basic_set_from_basic_map(map->p[i]);
4094 if (!set->p[i])
4095 goto error;
4097 return set;
4098 error:
4099 isl_map_free(map);
4100 return NULL;
4103 struct isl_map *isl_map_alloc_dim(struct isl_dim *dim, int n, unsigned flags)
4105 struct isl_map *map;
4107 if (!dim)
4108 return NULL;
4109 isl_assert(dim->ctx, n >= 0, return NULL);
4110 map = isl_alloc(dim->ctx, struct isl_map,
4111 sizeof(struct isl_map) +
4112 (n - 1) * sizeof(struct isl_basic_map *));
4113 if (!map)
4114 goto error;
4116 map->ctx = dim->ctx;
4117 isl_ctx_ref(map->ctx);
4118 map->ref = 1;
4119 map->size = n;
4120 map->n = 0;
4121 map->dim = dim;
4122 map->flags = flags;
4123 return map;
4124 error:
4125 isl_dim_free(dim);
4126 return NULL;
4129 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
4130 unsigned nparam, unsigned in, unsigned out, int n,
4131 unsigned flags)
4133 struct isl_map *map;
4134 struct isl_dim *dims;
4136 dims = isl_dim_alloc(ctx, nparam, in, out);
4137 if (!dims)
4138 return NULL;
4140 map = isl_map_alloc_dim(dims, n, flags);
4141 return map;
4144 struct isl_basic_map *isl_basic_map_empty(struct isl_dim *dim)
4146 struct isl_basic_map *bmap;
4147 bmap = isl_basic_map_alloc_dim(dim, 0, 1, 0);
4148 bmap = isl_basic_map_set_to_empty(bmap);
4149 return bmap;
4152 struct isl_basic_set *isl_basic_set_empty(struct isl_dim *dim)
4154 struct isl_basic_set *bset;
4155 bset = isl_basic_set_alloc_dim(dim, 0, 1, 0);
4156 bset = isl_basic_set_set_to_empty(bset);
4157 return bset;
4160 struct isl_basic_map *isl_basic_map_empty_like(struct isl_basic_map *model)
4162 struct isl_basic_map *bmap;
4163 if (!model)
4164 return NULL;
4165 bmap = isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
4166 bmap = isl_basic_map_set_to_empty(bmap);
4167 return bmap;
4170 struct isl_basic_map *isl_basic_map_empty_like_map(struct isl_map *model)
4172 struct isl_basic_map *bmap;
4173 if (!model)
4174 return NULL;
4175 bmap = isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
4176 bmap = isl_basic_map_set_to_empty(bmap);
4177 return bmap;
4180 struct isl_basic_set *isl_basic_set_empty_like(struct isl_basic_set *model)
4182 struct isl_basic_set *bset;
4183 if (!model)
4184 return NULL;
4185 bset = isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
4186 bset = isl_basic_set_set_to_empty(bset);
4187 return bset;
4190 struct isl_basic_map *isl_basic_map_universe(struct isl_dim *dim)
4192 struct isl_basic_map *bmap;
4193 bmap = isl_basic_map_alloc_dim(dim, 0, 0, 0);
4194 bmap = isl_basic_map_finalize(bmap);
4195 return bmap;
4198 struct isl_basic_set *isl_basic_set_universe(struct isl_dim *dim)
4200 struct isl_basic_set *bset;
4201 bset = isl_basic_set_alloc_dim(dim, 0, 0, 0);
4202 bset = isl_basic_set_finalize(bset);
4203 return bset;
4206 __isl_give isl_basic_map *isl_basic_map_nat_universe(__isl_take isl_dim *dim)
4208 int i;
4209 unsigned total = isl_dim_total(dim);
4210 isl_basic_map *bmap;
4212 bmap= isl_basic_map_alloc_dim(dim, 0, 0, total);
4213 for (i = 0; i < total; ++i) {
4214 int k = isl_basic_map_alloc_inequality(bmap);
4215 if (k < 0)
4216 goto error;
4217 isl_seq_clr(bmap->ineq[k], 1 + total);
4218 isl_int_set_si(bmap->ineq[k][1 + i], 1);
4220 return bmap;
4221 error:
4222 isl_basic_map_free(bmap);
4223 return NULL;
4226 __isl_give isl_basic_set *isl_basic_set_nat_universe(__isl_take isl_dim *dim)
4228 return isl_basic_map_nat_universe(dim);
4231 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_dim *dim)
4233 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
4236 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_dim *dim)
4238 return isl_map_nat_universe(dim);
4241 __isl_give isl_basic_map *isl_basic_map_universe_like(
4242 __isl_keep isl_basic_map *model)
4244 if (!model)
4245 return NULL;
4246 return isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
4249 struct isl_basic_set *isl_basic_set_universe_like(struct isl_basic_set *model)
4251 if (!model)
4252 return NULL;
4253 return isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
4256 __isl_give isl_basic_set *isl_basic_set_universe_like_set(
4257 __isl_keep isl_set *model)
4259 if (!model)
4260 return NULL;
4261 return isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
4264 struct isl_map *isl_map_empty(struct isl_dim *dim)
4266 return isl_map_alloc_dim(dim, 0, ISL_MAP_DISJOINT);
4269 struct isl_map *isl_map_empty_like(struct isl_map *model)
4271 if (!model)
4272 return NULL;
4273 return isl_map_alloc_dim(isl_dim_copy(model->dim), 0, ISL_MAP_DISJOINT);
4276 struct isl_map *isl_map_empty_like_basic_map(struct isl_basic_map *model)
4278 if (!model)
4279 return NULL;
4280 return isl_map_alloc_dim(isl_dim_copy(model->dim), 0, ISL_MAP_DISJOINT);
4283 struct isl_set *isl_set_empty(struct isl_dim *dim)
4285 return isl_set_alloc_dim(dim, 0, ISL_MAP_DISJOINT);
4288 struct isl_set *isl_set_empty_like(struct isl_set *model)
4290 if (!model)
4291 return NULL;
4292 return isl_set_empty(isl_dim_copy(model->dim));
4295 struct isl_map *isl_map_universe(struct isl_dim *dim)
4297 struct isl_map *map;
4298 if (!dim)
4299 return NULL;
4300 map = isl_map_alloc_dim(isl_dim_copy(dim), 1, ISL_MAP_DISJOINT);
4301 map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
4302 return map;
4305 struct isl_set *isl_set_universe(struct isl_dim *dim)
4307 struct isl_set *set;
4308 if (!dim)
4309 return NULL;
4310 set = isl_set_alloc_dim(isl_dim_copy(dim), 1, ISL_MAP_DISJOINT);
4311 set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
4312 return set;
4315 __isl_give isl_set *isl_set_universe_like(__isl_keep isl_set *model)
4317 if (!model)
4318 return NULL;
4319 return isl_set_universe(isl_dim_copy(model->dim));
4322 struct isl_map *isl_map_dup(struct isl_map *map)
4324 int i;
4325 struct isl_map *dup;
4327 if (!map)
4328 return NULL;
4329 dup = isl_map_alloc_dim(isl_dim_copy(map->dim), map->n, map->flags);
4330 for (i = 0; i < map->n; ++i)
4331 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
4332 return dup;
4335 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
4336 __isl_take isl_basic_map *bmap)
4338 if (!bmap || !map)
4339 goto error;
4340 if (isl_basic_map_plain_is_empty(bmap)) {
4341 isl_basic_map_free(bmap);
4342 return map;
4344 isl_assert(map->ctx, isl_dim_equal(map->dim, bmap->dim), goto error);
4345 isl_assert(map->ctx, map->n < map->size, goto error);
4346 map->p[map->n] = bmap;
4347 map->n++;
4348 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4349 return map;
4350 error:
4351 if (map)
4352 isl_map_free(map);
4353 if (bmap)
4354 isl_basic_map_free(bmap);
4355 return NULL;
4358 void isl_map_free(struct isl_map *map)
4360 int i;
4362 if (!map)
4363 return;
4365 if (--map->ref > 0)
4366 return;
4368 isl_ctx_deref(map->ctx);
4369 for (i = 0; i < map->n; ++i)
4370 isl_basic_map_free(map->p[i]);
4371 isl_dim_free(map->dim);
4372 free(map);
4375 struct isl_map *isl_map_extend(struct isl_map *base,
4376 unsigned nparam, unsigned n_in, unsigned n_out)
4378 int i;
4380 base = isl_map_cow(base);
4381 if (!base)
4382 return NULL;
4384 base->dim = isl_dim_extend(base->dim, nparam, n_in, n_out);
4385 if (!base->dim)
4386 goto error;
4387 for (i = 0; i < base->n; ++i) {
4388 base->p[i] = isl_basic_map_extend_dim(base->p[i],
4389 isl_dim_copy(base->dim), 0, 0, 0);
4390 if (!base->p[i])
4391 goto error;
4393 return base;
4394 error:
4395 isl_map_free(base);
4396 return NULL;
4399 struct isl_set *isl_set_extend(struct isl_set *base,
4400 unsigned nparam, unsigned dim)
4402 return (struct isl_set *)isl_map_extend((struct isl_map *)base,
4403 nparam, 0, dim);
4406 static struct isl_basic_map *isl_basic_map_fix_pos_si(
4407 struct isl_basic_map *bmap, unsigned pos, int value)
4409 int j;
4411 bmap = isl_basic_map_cow(bmap);
4412 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
4413 j = isl_basic_map_alloc_equality(bmap);
4414 if (j < 0)
4415 goto error;
4416 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
4417 isl_int_set_si(bmap->eq[j][pos], -1);
4418 isl_int_set_si(bmap->eq[j][0], value);
4419 bmap = isl_basic_map_simplify(bmap);
4420 return isl_basic_map_finalize(bmap);
4421 error:
4422 isl_basic_map_free(bmap);
4423 return NULL;
4426 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
4427 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
4429 int j;
4431 bmap = isl_basic_map_cow(bmap);
4432 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
4433 j = isl_basic_map_alloc_equality(bmap);
4434 if (j < 0)
4435 goto error;
4436 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
4437 isl_int_set_si(bmap->eq[j][pos], -1);
4438 isl_int_set(bmap->eq[j][0], value);
4439 bmap = isl_basic_map_simplify(bmap);
4440 return isl_basic_map_finalize(bmap);
4441 error:
4442 isl_basic_map_free(bmap);
4443 return NULL;
4446 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
4447 enum isl_dim_type type, unsigned pos, int value)
4449 if (!bmap)
4450 return NULL;
4451 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
4452 return isl_basic_map_fix_pos_si(bmap,
4453 isl_basic_map_offset(bmap, type) + pos, value);
4454 error:
4455 isl_basic_map_free(bmap);
4456 return NULL;
4459 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
4460 enum isl_dim_type type, unsigned pos, isl_int value)
4462 if (!bmap)
4463 return NULL;
4464 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
4465 return isl_basic_map_fix_pos(bmap,
4466 isl_basic_map_offset(bmap, type) + pos, value);
4467 error:
4468 isl_basic_map_free(bmap);
4469 return NULL;
4472 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
4473 enum isl_dim_type type, unsigned pos, int value)
4475 return (struct isl_basic_set *)
4476 isl_basic_map_fix_si((struct isl_basic_map *)bset,
4477 type, pos, value);
4480 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
4481 enum isl_dim_type type, unsigned pos, isl_int value)
4483 return (struct isl_basic_set *)
4484 isl_basic_map_fix((struct isl_basic_map *)bset,
4485 type, pos, value);
4488 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
4489 unsigned input, int value)
4491 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
4494 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
4495 unsigned dim, int value)
4497 return (struct isl_basic_set *)
4498 isl_basic_map_fix_si((struct isl_basic_map *)bset,
4499 isl_dim_set, dim, value);
4502 struct isl_map *isl_map_fix_si(struct isl_map *map,
4503 enum isl_dim_type type, unsigned pos, int value)
4505 int i;
4507 map = isl_map_cow(map);
4508 if (!map)
4509 return NULL;
4511 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
4512 for (i = 0; i < map->n; ++i) {
4513 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
4514 if (!map->p[i])
4515 goto error;
4517 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4518 return map;
4519 error:
4520 isl_map_free(map);
4521 return NULL;
4524 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
4525 enum isl_dim_type type, unsigned pos, int value)
4527 return (struct isl_set *)
4528 isl_map_fix_si((struct isl_map *)set, type, pos, value);
4531 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
4532 enum isl_dim_type type, unsigned pos, isl_int value)
4534 int i;
4536 map = isl_map_cow(map);
4537 if (!map)
4538 return NULL;
4540 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
4541 for (i = 0; i < map->n; ++i) {
4542 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
4543 if (!map->p[i])
4544 goto error;
4546 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4547 return map;
4548 error:
4549 isl_map_free(map);
4550 return NULL;
4553 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
4554 enum isl_dim_type type, unsigned pos, isl_int value)
4556 return (struct isl_set *)isl_map_fix((isl_map *)set, type, pos, value);
4559 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
4560 unsigned input, int value)
4562 return isl_map_fix_si(map, isl_dim_in, input, value);
4565 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
4567 return (struct isl_set *)
4568 isl_map_fix_si((struct isl_map *)set, isl_dim_set, dim, value);
4571 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
4572 __isl_take isl_basic_map *bmap,
4573 enum isl_dim_type type, unsigned pos, int value)
4575 int j;
4577 if (!bmap)
4578 return NULL;
4579 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
4580 pos += isl_basic_map_offset(bmap, type);
4581 bmap = isl_basic_map_cow(bmap);
4582 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
4583 j = isl_basic_map_alloc_inequality(bmap);
4584 if (j < 0)
4585 goto error;
4586 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
4587 isl_int_set_si(bmap->ineq[j][pos], 1);
4588 isl_int_set_si(bmap->ineq[j][0], -value);
4589 bmap = isl_basic_map_simplify(bmap);
4590 return isl_basic_map_finalize(bmap);
4591 error:
4592 isl_basic_map_free(bmap);
4593 return NULL;
4596 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
4597 unsigned dim, isl_int value)
4599 int j;
4601 bset = isl_basic_set_cow(bset);
4602 bset = isl_basic_set_extend_constraints(bset, 0, 1);
4603 j = isl_basic_set_alloc_inequality(bset);
4604 if (j < 0)
4605 goto error;
4606 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
4607 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
4608 isl_int_neg(bset->ineq[j][0], value);
4609 bset = isl_basic_set_simplify(bset);
4610 return isl_basic_set_finalize(bset);
4611 error:
4612 isl_basic_set_free(bset);
4613 return NULL;
4616 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
4617 enum isl_dim_type type, unsigned pos, int value)
4619 int i;
4621 map = isl_map_cow(map);
4622 if (!map)
4623 return NULL;
4625 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
4626 for (i = 0; i < map->n; ++i) {
4627 map->p[i] = isl_basic_map_lower_bound_si(map->p[i],
4628 type, pos, value);
4629 if (!map->p[i])
4630 goto error;
4632 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4633 return map;
4634 error:
4635 isl_map_free(map);
4636 return NULL;
4639 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
4640 enum isl_dim_type type, unsigned pos, int value)
4642 return (struct isl_set *)
4643 isl_map_lower_bound_si((struct isl_map *)set, type, pos, value);
4646 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
4647 isl_int value)
4649 int i;
4651 set = isl_set_cow(set);
4652 if (!set)
4653 return NULL;
4655 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
4656 for (i = 0; i < set->n; ++i) {
4657 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
4658 if (!set->p[i])
4659 goto error;
4661 return set;
4662 error:
4663 isl_set_free(set);
4664 return NULL;
4667 struct isl_map *isl_map_reverse(struct isl_map *map)
4669 int i;
4671 map = isl_map_cow(map);
4672 if (!map)
4673 return NULL;
4675 map->dim = isl_dim_reverse(map->dim);
4676 if (!map->dim)
4677 goto error;
4678 for (i = 0; i < map->n; ++i) {
4679 map->p[i] = isl_basic_map_reverse(map->p[i]);
4680 if (!map->p[i])
4681 goto error;
4683 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4684 return map;
4685 error:
4686 isl_map_free(map);
4687 return NULL;
4690 static struct isl_map *isl_basic_map_partial_lexopt(
4691 struct isl_basic_map *bmap, struct isl_basic_set *dom,
4692 struct isl_set **empty, int max)
4694 if (!bmap)
4695 goto error;
4696 if (bmap->ctx->opt->pip == ISL_PIP_PIP)
4697 return isl_pip_basic_map_lexopt(bmap, dom, empty, max);
4698 else
4699 return isl_tab_basic_map_partial_lexopt(bmap, dom, empty, max);
4700 error:
4701 isl_basic_map_free(bmap);
4702 isl_basic_set_free(dom);
4703 if (empty)
4704 *empty = NULL;
4705 return NULL;
4708 struct isl_map *isl_basic_map_partial_lexmax(
4709 struct isl_basic_map *bmap, struct isl_basic_set *dom,
4710 struct isl_set **empty)
4712 return isl_basic_map_partial_lexopt(bmap, dom, empty, 1);
4715 struct isl_map *isl_basic_map_partial_lexmin(
4716 struct isl_basic_map *bmap, struct isl_basic_set *dom,
4717 struct isl_set **empty)
4719 return isl_basic_map_partial_lexopt(bmap, dom, empty, 0);
4722 struct isl_set *isl_basic_set_partial_lexmin(
4723 struct isl_basic_set *bset, struct isl_basic_set *dom,
4724 struct isl_set **empty)
4726 return (struct isl_set *)
4727 isl_basic_map_partial_lexmin((struct isl_basic_map *)bset,
4728 dom, empty);
4731 struct isl_set *isl_basic_set_partial_lexmax(
4732 struct isl_basic_set *bset, struct isl_basic_set *dom,
4733 struct isl_set **empty)
4735 return (struct isl_set *)
4736 isl_basic_map_partial_lexmax((struct isl_basic_map *)bset,
4737 dom, empty);
4740 /* Given a basic map "bmap", compute the lexicographically minimal
4741 * (or maximal) image element for each domain element in dom.
4742 * Set *empty to those elements in dom that do not have an image element.
4744 * We first make sure the basic sets in dom are disjoint and then
4745 * simply collect the results over each of the basic sets separately.
4746 * We could probably improve the efficiency a bit by moving the union
4747 * domain down into the parametric integer programming.
4749 static __isl_give isl_map *basic_map_partial_lexopt(
4750 __isl_take isl_basic_map *bmap, __isl_take isl_set *dom,
4751 __isl_give isl_set **empty, int max)
4753 int i;
4754 struct isl_map *res;
4756 dom = isl_set_make_disjoint(dom);
4757 if (!dom)
4758 goto error;
4760 if (isl_set_plain_is_empty(dom)) {
4761 res = isl_map_empty_like_basic_map(bmap);
4762 *empty = isl_set_empty_like(dom);
4763 isl_set_free(dom);
4764 isl_basic_map_free(bmap);
4765 return res;
4768 res = isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap),
4769 isl_basic_set_copy(dom->p[0]), empty, max);
4771 for (i = 1; i < dom->n; ++i) {
4772 struct isl_map *res_i;
4773 struct isl_set *empty_i;
4775 res_i = isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap),
4776 isl_basic_set_copy(dom->p[i]), &empty_i, max);
4778 res = isl_map_union_disjoint(res, res_i);
4779 *empty = isl_set_union_disjoint(*empty, empty_i);
4782 isl_set_free(dom);
4783 isl_basic_map_free(bmap);
4784 return res;
4785 error:
4786 *empty = NULL;
4787 isl_set_free(dom);
4788 isl_basic_map_free(bmap);
4789 return NULL;
4792 /* Given a map "map", compute the lexicographically minimal
4793 * (or maximal) image element for each domain element in dom.
4794 * Set *empty to those elements in dom that do not have an image element.
4796 * We first compute the lexicographically minimal or maximal element
4797 * in the first basic map. This results in a partial solution "res"
4798 * and a subset "todo" of dom that still need to be handled.
4799 * We then consider each of the remaining maps in "map" and successively
4800 * improve both "res" and "todo".
4802 * Let res^k and todo^k be the results after k steps and let i = k + 1.
4803 * Assume we are computing the lexicographical maximum.
4804 * We first compute the lexicographically maximal element in basic map i.
4805 * This results in a partial solution res_i and a subset todo_i.
4806 * Then we combine these results with those obtain for the first k basic maps
4807 * to obtain a result that is valid for the first k+1 basic maps.
4808 * In particular, the set where there is no solution is the set where
4809 * there is no solution for the first k basic maps and also no solution
4810 * for the ith basic map, i.e.,
4812 * todo^i = todo^k * todo_i
4814 * On dom(res^k) * dom(res_i), we need to pick the larger of the two
4815 * solutions, arbitrarily breaking ties in favor of res^k.
4816 * That is, when res^k(a) >= res_i(a), we pick res^k and
4817 * when res^k(a) < res_i(a), we pick res_i. (Here, ">=" and "<" denote
4818 * the lexicographic order.)
4819 * In practice, we compute
4821 * res^k * (res_i . "<=")
4823 * and
4825 * res_i * (res^k . "<")
4827 * Finally, we consider the symmetric difference of dom(res^k) and dom(res_i),
4828 * where only one of res^k and res_i provides a solution and we simply pick
4829 * that one, i.e.,
4831 * res^k * todo_i
4832 * and
4833 * res_i * todo^k
4835 * Note that we only compute these intersections when dom(res^k) intersects
4836 * dom(res_i). Otherwise, the only effect of these intersections is to
4837 * potentially break up res^k and res_i into smaller pieces.
4838 * We want to avoid such splintering as much as possible.
4839 * In fact, an earlier implementation of this function would look for
4840 * better results in the domain of res^k and for extra results in todo^k,
4841 * but this would always result in a splintering according to todo^k,
4842 * even when the domain of basic map i is disjoint from the domains of
4843 * the previous basic maps.
4845 static __isl_give isl_map *isl_map_partial_lexopt(
4846 __isl_take isl_map *map, __isl_take isl_set *dom,
4847 __isl_give isl_set **empty, int max)
4849 int i;
4850 struct isl_map *res;
4851 struct isl_set *todo;
4853 if (!map || !dom)
4854 goto error;
4856 if (isl_map_plain_is_empty(map)) {
4857 if (empty)
4858 *empty = dom;
4859 else
4860 isl_set_free(dom);
4861 return map;
4864 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
4865 isl_set_copy(dom), &todo, max);
4867 for (i = 1; i < map->n; ++i) {
4868 isl_map *lt, *le;
4869 isl_map *res_i;
4870 isl_set *todo_i;
4871 isl_dim *dim = isl_dim_range(isl_map_get_dim(res));
4873 res_i = basic_map_partial_lexopt(isl_basic_map_copy(map->p[i]),
4874 isl_set_copy(dom), &todo_i, max);
4876 if (max) {
4877 lt = isl_map_lex_lt(isl_dim_copy(dim));
4878 le = isl_map_lex_le(dim);
4879 } else {
4880 lt = isl_map_lex_gt(isl_dim_copy(dim));
4881 le = isl_map_lex_ge(dim);
4883 lt = isl_map_apply_range(isl_map_copy(res), lt);
4884 lt = isl_map_intersect(lt, isl_map_copy(res_i));
4885 le = isl_map_apply_range(isl_map_copy(res_i), le);
4886 le = isl_map_intersect(le, isl_map_copy(res));
4888 if (!isl_map_is_empty(lt) || !isl_map_is_empty(le)) {
4889 res = isl_map_intersect_domain(res,
4890 isl_set_copy(todo_i));
4891 res_i = isl_map_intersect_domain(res_i,
4892 isl_set_copy(todo));
4895 res = isl_map_union_disjoint(res, res_i);
4896 res = isl_map_union_disjoint(res, lt);
4897 res = isl_map_union_disjoint(res, le);
4899 todo = isl_set_intersect(todo, todo_i);
4902 isl_set_free(dom);
4903 isl_map_free(map);
4905 if (empty)
4906 *empty = todo;
4907 else
4908 isl_set_free(todo);
4910 return res;
4911 error:
4912 if (empty)
4913 *empty = NULL;
4914 isl_set_free(dom);
4915 isl_map_free(map);
4916 return NULL;
4919 __isl_give isl_map *isl_map_partial_lexmax(
4920 __isl_take isl_map *map, __isl_take isl_set *dom,
4921 __isl_give isl_set **empty)
4923 return isl_map_partial_lexopt(map, dom, empty, 1);
4926 __isl_give isl_map *isl_map_partial_lexmin(
4927 __isl_take isl_map *map, __isl_take isl_set *dom,
4928 __isl_give isl_set **empty)
4930 return isl_map_partial_lexopt(map, dom, empty, 0);
4933 __isl_give isl_set *isl_set_partial_lexmin(
4934 __isl_take isl_set *set, __isl_take isl_set *dom,
4935 __isl_give isl_set **empty)
4937 return (struct isl_set *)
4938 isl_map_partial_lexmin((struct isl_map *)set,
4939 dom, empty);
4942 __isl_give isl_set *isl_set_partial_lexmax(
4943 __isl_take isl_set *set, __isl_take isl_set *dom,
4944 __isl_give isl_set **empty)
4946 return (struct isl_set *)
4947 isl_map_partial_lexmax((struct isl_map *)set,
4948 dom, empty);
4951 __isl_give isl_map *isl_basic_map_lexopt(__isl_take isl_basic_map *bmap, int max)
4953 struct isl_basic_set *dom = NULL;
4954 struct isl_dim *dom_dim;
4956 if (!bmap)
4957 goto error;
4958 dom_dim = isl_dim_domain(isl_dim_copy(bmap->dim));
4959 dom = isl_basic_set_universe(dom_dim);
4960 return isl_basic_map_partial_lexopt(bmap, dom, NULL, max);
4961 error:
4962 isl_basic_map_free(bmap);
4963 return NULL;
4966 __isl_give isl_map *isl_basic_map_lexmin(__isl_take isl_basic_map *bmap)
4968 return isl_basic_map_lexopt(bmap, 0);
4971 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
4973 return isl_basic_map_lexopt(bmap, 1);
4976 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
4978 return (isl_set *)isl_basic_map_lexmin((isl_basic_map *)bset);
4981 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
4983 return (isl_set *)isl_basic_map_lexmax((isl_basic_map *)bset);
4986 __isl_give isl_map *isl_map_lexopt(__isl_take isl_map *map, int max)
4988 struct isl_set *dom = NULL;
4989 struct isl_dim *dom_dim;
4991 if (!map)
4992 goto error;
4993 dom_dim = isl_dim_domain(isl_dim_copy(map->dim));
4994 dom = isl_set_universe(dom_dim);
4995 return isl_map_partial_lexopt(map, dom, NULL, max);
4996 error:
4997 isl_map_free(map);
4998 return NULL;
5001 __isl_give isl_map *isl_map_lexmin(__isl_take isl_map *map)
5003 return isl_map_lexopt(map, 0);
5006 __isl_give isl_map *isl_map_lexmax(__isl_take isl_map *map)
5008 return isl_map_lexopt(map, 1);
5011 __isl_give isl_set *isl_set_lexmin(__isl_take isl_set *set)
5013 return (isl_set *)isl_map_lexmin((isl_map *)set);
5016 __isl_give isl_set *isl_set_lexmax(__isl_take isl_set *set)
5018 return (isl_set *)isl_map_lexmax((isl_map *)set);
5021 /* Construct a map that equates the two given dimensions in the given space.
5023 static __isl_give isl_map *equate(__isl_take isl_dim *dim,
5024 enum isl_dim_type src_type, int src_pos,
5025 enum isl_dim_type dst_type, int dst_pos)
5027 isl_basic_map *bmap;
5028 int k;
5030 bmap = isl_basic_map_alloc_dim(dim, 0, 1, 0);
5031 k = isl_basic_map_alloc_equality(bmap);
5032 if (k < 0)
5033 goto error;
5034 isl_seq_clr(bmap->eq[k], 1 + isl_basic_map_total_dim(bmap));
5035 src_pos += isl_basic_map_offset(bmap, src_type);
5036 dst_pos += isl_basic_map_offset(bmap, dst_type);
5037 isl_int_set_si(bmap->eq[k][src_pos], 1);
5038 isl_int_set_si(bmap->eq[k][dst_pos], -1);
5040 return isl_map_from_basic_map(bmap);
5041 error:
5042 isl_basic_map_free(bmap);
5043 return NULL;
5046 /* Extract the first and only affine expression from list
5047 * and then add it to *pwaff with the given dom.
5048 * This domain is known to be disjoint from other domains
5049 * because of the way isl_basic_set_foreach_lexmax works.
5051 static int update_dim_max(__isl_take isl_basic_set *dom,
5052 __isl_take isl_aff_list *list, void *user)
5054 isl_ctx *ctx = isl_basic_set_get_ctx(dom);
5055 isl_aff *aff;
5056 isl_pw_aff **pwaff = user;
5057 isl_pw_aff *pwaff_i;
5059 if (isl_aff_list_n_aff(list) != 1)
5060 isl_die(ctx, isl_error_internal,
5061 "expecting single element list", goto error);
5063 aff = isl_aff_list_get_aff(list, 0);
5064 pwaff_i = isl_pw_aff_alloc(isl_set_from_basic_set(dom), aff);
5066 *pwaff = isl_pw_aff_add_disjoint(*pwaff, pwaff_i);
5068 isl_aff_list_free(list);
5070 return 0;
5071 error:
5072 isl_basic_set_free(dom);
5073 isl_aff_list_free(list);
5074 return -1;
5077 /* Given a one-dimensional basic set, compute the maximum of that
5078 * dimension as an isl_pw_aff.
5080 * The isl_pw_aff is constructed by having isl_basic_set_foreach_lexmax
5081 * call update_dim_max on each leaf of the result.
5083 static __isl_give isl_pw_aff *basic_set_dim_max(__isl_keep isl_basic_set *bset)
5085 isl_dim *dim = isl_basic_set_get_dim(bset);
5086 isl_pw_aff *pwaff;
5087 int r;
5089 dim = isl_dim_domain(isl_dim_from_range(dim));
5090 pwaff = isl_pw_aff_empty(dim);
5092 r = isl_basic_set_foreach_lexmax(bset, &update_dim_max, &pwaff);
5093 if (r < 0)
5094 return isl_pw_aff_free(pwaff);
5096 return pwaff;
5099 /* Compute the maximum of the given set dimension as a function of the
5100 * parameters, but independently of the other set dimensions.
5102 * We first project the set onto the given dimension and then compute
5103 * the "lexicographic" maximum in each basic set, combining the results
5104 * using isl_pw_aff_max.
5106 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
5108 int i;
5109 isl_map *map;
5110 isl_pw_aff *pwaff;
5112 map = isl_map_from_domain(set);
5113 map = isl_map_add_dims(map, isl_dim_out, 1);
5114 map = isl_map_intersect(map,
5115 equate(isl_map_get_dim(map), isl_dim_in, pos,
5116 isl_dim_out, 0));
5117 set = isl_map_range(map);
5118 if (!set)
5119 return NULL;
5121 if (set->n == 0) {
5122 isl_dim *dim = isl_set_get_dim(set);
5123 dim = isl_dim_domain(isl_dim_from_range(dim));
5124 isl_set_free(set);
5125 return isl_pw_aff_empty(dim);
5128 pwaff = basic_set_dim_max(set->p[0]);
5129 for (i = 1; i < set->n; ++i) {
5130 isl_pw_aff *pwaff_i;
5132 pwaff_i = basic_set_dim_max(set->p[i]);
5133 pwaff = isl_pw_aff_max(pwaff, pwaff_i);
5136 isl_set_free(set);
5138 return pwaff;
5141 /* Apply a preimage specified by "mat" on the parameters of "bset".
5142 * bset is assumed to have only parameters and divs.
5144 static struct isl_basic_set *basic_set_parameter_preimage(
5145 struct isl_basic_set *bset, struct isl_mat *mat)
5147 unsigned nparam;
5149 if (!bset || !mat)
5150 goto error;
5152 bset->dim = isl_dim_cow(bset->dim);
5153 if (!bset->dim)
5154 goto error;
5156 nparam = isl_basic_set_dim(bset, isl_dim_param);
5158 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
5160 bset->dim->nparam = 0;
5161 bset->dim->n_out = nparam;
5162 bset = isl_basic_set_preimage(bset, mat);
5163 if (bset) {
5164 bset->dim->nparam = bset->dim->n_out;
5165 bset->dim->n_out = 0;
5167 return bset;
5168 error:
5169 isl_mat_free(mat);
5170 isl_basic_set_free(bset);
5171 return NULL;
5174 /* Apply a preimage specified by "mat" on the parameters of "set".
5175 * set is assumed to have only parameters and divs.
5177 static struct isl_set *set_parameter_preimage(
5178 struct isl_set *set, struct isl_mat *mat)
5180 struct isl_dim *dim = NULL;
5181 unsigned nparam;
5183 if (!set || !mat)
5184 goto error;
5186 dim = isl_dim_copy(set->dim);
5187 dim = isl_dim_cow(dim);
5188 if (!dim)
5189 goto error;
5191 nparam = isl_set_dim(set, isl_dim_param);
5193 isl_assert(set->ctx, mat->n_row == 1 + nparam, goto error);
5195 dim->nparam = 0;
5196 dim->n_out = nparam;
5197 isl_set_reset_dim(set, dim);
5198 set = isl_set_preimage(set, mat);
5199 if (!set)
5200 goto error2;
5201 dim = isl_dim_copy(set->dim);
5202 dim = isl_dim_cow(dim);
5203 if (!dim)
5204 goto error2;
5205 dim->nparam = dim->n_out;
5206 dim->n_out = 0;
5207 isl_set_reset_dim(set, dim);
5208 return set;
5209 error:
5210 isl_dim_free(dim);
5211 isl_mat_free(mat);
5212 error2:
5213 isl_set_free(set);
5214 return NULL;
5217 /* Intersect the basic set "bset" with the affine space specified by the
5218 * equalities in "eq".
5220 static struct isl_basic_set *basic_set_append_equalities(
5221 struct isl_basic_set *bset, struct isl_mat *eq)
5223 int i, k;
5224 unsigned len;
5226 if (!bset || !eq)
5227 goto error;
5229 bset = isl_basic_set_extend_dim(bset, isl_dim_copy(bset->dim), 0,
5230 eq->n_row, 0);
5231 if (!bset)
5232 goto error;
5234 len = 1 + isl_dim_total(bset->dim) + bset->extra;
5235 for (i = 0; i < eq->n_row; ++i) {
5236 k = isl_basic_set_alloc_equality(bset);
5237 if (k < 0)
5238 goto error;
5239 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
5240 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
5242 isl_mat_free(eq);
5244 bset = isl_basic_set_gauss(bset, NULL);
5245 bset = isl_basic_set_finalize(bset);
5247 return bset;
5248 error:
5249 isl_mat_free(eq);
5250 isl_basic_set_free(bset);
5251 return NULL;
5254 /* Intersect the set "set" with the affine space specified by the
5255 * equalities in "eq".
5257 static struct isl_set *set_append_equalities(struct isl_set *set,
5258 struct isl_mat *eq)
5260 int i;
5262 if (!set || !eq)
5263 goto error;
5265 for (i = 0; i < set->n; ++i) {
5266 set->p[i] = basic_set_append_equalities(set->p[i],
5267 isl_mat_copy(eq));
5268 if (!set->p[i])
5269 goto error;
5271 isl_mat_free(eq);
5272 return set;
5273 error:
5274 isl_mat_free(eq);
5275 isl_set_free(set);
5276 return NULL;
5279 /* Project the given basic set onto its parameter domain, possibly introducing
5280 * new, explicit, existential variables in the constraints.
5281 * The input has parameters and (possibly implicit) existential variables.
5282 * The output has the same parameters, but only
5283 * explicit existentially quantified variables.
5285 * The actual projection is performed by pip, but pip doesn't seem
5286 * to like equalities very much, so we first remove the equalities
5287 * among the parameters by performing a variable compression on
5288 * the parameters. Afterward, an inverse transformation is performed
5289 * and the equalities among the parameters are inserted back in.
5291 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
5293 int i, j;
5294 struct isl_mat *eq;
5295 struct isl_mat *T, *T2;
5296 struct isl_set *set;
5297 unsigned nparam, n_div;
5299 bset = isl_basic_set_cow(bset);
5300 if (!bset)
5301 return NULL;
5303 if (bset->n_eq == 0)
5304 return isl_basic_set_lexmin(bset);
5306 isl_basic_set_gauss(bset, NULL);
5308 nparam = isl_basic_set_dim(bset, isl_dim_param);
5309 n_div = isl_basic_set_dim(bset, isl_dim_div);
5311 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
5312 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
5313 ++i;
5315 if (i == bset->n_eq)
5316 return isl_basic_set_lexmin(bset);
5318 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
5319 0, 1 + nparam);
5320 eq = isl_mat_cow(eq);
5321 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
5322 if (T && T->n_col == 0) {
5323 isl_mat_free(T);
5324 isl_mat_free(T2);
5325 isl_mat_free(eq);
5326 bset = isl_basic_set_set_to_empty(bset);
5327 return isl_set_from_basic_set(bset);
5329 bset = basic_set_parameter_preimage(bset, T);
5331 set = isl_basic_set_lexmin(bset);
5332 set = set_parameter_preimage(set, T2);
5333 set = set_append_equalities(set, eq);
5334 return set;
5337 /* Compute an explicit representation for all the existentially
5338 * quantified variables.
5339 * The input and output dimensions are first turned into parameters.
5340 * compute_divs then returns a map with the same parameters and
5341 * no input or output dimensions and the dimension specification
5342 * is reset to that of the input.
5344 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
5346 struct isl_basic_set *bset;
5347 struct isl_set *set;
5348 struct isl_map *map;
5349 struct isl_dim *dim, *orig_dim = NULL;
5350 unsigned nparam;
5351 unsigned n_in;
5352 unsigned n_out;
5354 bmap = isl_basic_map_cow(bmap);
5355 if (!bmap)
5356 return NULL;
5358 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5359 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5360 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5361 dim = isl_dim_set_alloc(bmap->ctx, nparam + n_in + n_out, 0);
5362 if (!dim)
5363 goto error;
5365 orig_dim = bmap->dim;
5366 bmap->dim = dim;
5367 bset = (struct isl_basic_set *)bmap;
5369 set = parameter_compute_divs(bset);
5370 map = (struct isl_map *)set;
5371 map = isl_map_reset_dim(map, orig_dim);
5373 return map;
5374 error:
5375 isl_basic_map_free(bmap);
5376 return NULL;
5379 static int basic_map_divs_known(__isl_keep isl_basic_map *bmap)
5381 int i;
5382 unsigned off;
5384 if (!bmap)
5385 return -1;
5387 off = isl_dim_total(bmap->dim);
5388 for (i = 0; i < bmap->n_div; ++i) {
5389 if (isl_int_is_zero(bmap->div[i][0]))
5390 return 0;
5391 isl_assert(bmap->ctx, isl_int_is_zero(bmap->div[i][1+1+off+i]),
5392 return -1);
5394 return 1;
5397 static int map_divs_known(__isl_keep isl_map *map)
5399 int i;
5401 if (!map)
5402 return -1;
5404 for (i = 0; i < map->n; ++i) {
5405 int known = basic_map_divs_known(map->p[i]);
5406 if (known <= 0)
5407 return known;
5410 return 1;
5413 /* If bmap contains any unknown divs, then compute explicit
5414 * expressions for them. However, this computation may be
5415 * quite expensive, so first try to remove divs that aren't
5416 * strictly needed.
5418 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
5420 int known;
5421 struct isl_map *map;
5423 known = basic_map_divs_known(bmap);
5424 if (known < 0)
5425 goto error;
5426 if (known)
5427 return isl_map_from_basic_map(bmap);
5429 bmap = isl_basic_map_drop_redundant_divs(bmap);
5431 known = basic_map_divs_known(bmap);
5432 if (known < 0)
5433 goto error;
5434 if (known)
5435 return isl_map_from_basic_map(bmap);
5437 map = compute_divs(bmap);
5438 return map;
5439 error:
5440 isl_basic_map_free(bmap);
5441 return NULL;
5444 struct isl_map *isl_map_compute_divs(struct isl_map *map)
5446 int i;
5447 int known;
5448 struct isl_map *res;
5450 if (!map)
5451 return NULL;
5452 if (map->n == 0)
5453 return map;
5455 known = map_divs_known(map);
5456 if (known < 0) {
5457 isl_map_free(map);
5458 return NULL;
5460 if (known)
5461 return map;
5463 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
5464 for (i = 1 ; i < map->n; ++i) {
5465 struct isl_map *r2;
5466 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
5467 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
5468 res = isl_map_union_disjoint(res, r2);
5469 else
5470 res = isl_map_union(res, r2);
5472 isl_map_free(map);
5474 return res;
5477 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
5479 return (struct isl_set *)
5480 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
5483 struct isl_set *isl_set_compute_divs(struct isl_set *set)
5485 return (struct isl_set *)
5486 isl_map_compute_divs((struct isl_map *)set);
5489 struct isl_set *isl_map_domain(struct isl_map *map)
5491 int i;
5492 struct isl_set *set;
5494 if (!map)
5495 goto error;
5497 map = isl_map_cow(map);
5498 if (!map)
5499 return NULL;
5501 set = (struct isl_set *)map;
5502 set->dim = isl_dim_domain(set->dim);
5503 if (!set->dim)
5504 goto error;
5505 for (i = 0; i < map->n; ++i) {
5506 set->p[i] = isl_basic_map_domain(map->p[i]);
5507 if (!set->p[i])
5508 goto error;
5510 ISL_F_CLR(set, ISL_MAP_DISJOINT);
5511 ISL_F_CLR(set, ISL_SET_NORMALIZED);
5512 return set;
5513 error:
5514 isl_map_free(map);
5515 return NULL;
5518 struct isl_map *isl_map_union_disjoint(
5519 struct isl_map *map1, struct isl_map *map2)
5521 int i;
5522 unsigned flags = 0;
5523 struct isl_map *map = NULL;
5525 if (!map1 || !map2)
5526 goto error;
5528 if (map1->n == 0) {
5529 isl_map_free(map1);
5530 return map2;
5532 if (map2->n == 0) {
5533 isl_map_free(map2);
5534 return map1;
5537 isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
5539 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
5540 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
5541 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
5543 map = isl_map_alloc_dim(isl_dim_copy(map1->dim),
5544 map1->n + map2->n, flags);
5545 if (!map)
5546 goto error;
5547 for (i = 0; i < map1->n; ++i) {
5548 map = isl_map_add_basic_map(map,
5549 isl_basic_map_copy(map1->p[i]));
5550 if (!map)
5551 goto error;
5553 for (i = 0; i < map2->n; ++i) {
5554 map = isl_map_add_basic_map(map,
5555 isl_basic_map_copy(map2->p[i]));
5556 if (!map)
5557 goto error;
5559 isl_map_free(map1);
5560 isl_map_free(map2);
5561 return map;
5562 error:
5563 isl_map_free(map);
5564 isl_map_free(map1);
5565 isl_map_free(map2);
5566 return NULL;
5569 struct isl_map *isl_map_union(struct isl_map *map1, struct isl_map *map2)
5571 map1 = isl_map_union_disjoint(map1, map2);
5572 if (!map1)
5573 return NULL;
5574 if (map1->n > 1)
5575 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
5576 return map1;
5579 struct isl_set *isl_set_union_disjoint(
5580 struct isl_set *set1, struct isl_set *set2)
5582 return (struct isl_set *)
5583 isl_map_union_disjoint(
5584 (struct isl_map *)set1, (struct isl_map *)set2);
5587 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
5589 return (struct isl_set *)
5590 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
5593 struct isl_map *isl_map_intersect_range(
5594 struct isl_map *map, struct isl_set *set)
5596 unsigned flags = 0;
5597 struct isl_map *result;
5598 int i, j;
5600 if (!map || !set)
5601 goto error;
5603 if (!isl_dim_match(map->dim, isl_dim_param, set->dim, isl_dim_param))
5604 isl_die(set->ctx, isl_error_invalid,
5605 "parameters don't match", goto error);
5607 if (isl_dim_size(set->dim, isl_dim_set) != 0 &&
5608 !isl_map_compatible_range(map, set))
5609 isl_die(set->ctx, isl_error_invalid,
5610 "incompatible spaces", goto error);
5612 if (isl_set_plain_is_universe(set)) {
5613 isl_set_free(set);
5614 return map;
5617 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
5618 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
5619 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
5621 result = isl_map_alloc_dim(isl_dim_copy(map->dim),
5622 map->n * set->n, flags);
5623 if (!result)
5624 goto error;
5625 for (i = 0; i < map->n; ++i)
5626 for (j = 0; j < set->n; ++j) {
5627 result = isl_map_add_basic_map(result,
5628 isl_basic_map_intersect_range(
5629 isl_basic_map_copy(map->p[i]),
5630 isl_basic_set_copy(set->p[j])));
5631 if (!result)
5632 goto error;
5634 isl_map_free(map);
5635 isl_set_free(set);
5636 return result;
5637 error:
5638 isl_map_free(map);
5639 isl_set_free(set);
5640 return NULL;
5643 struct isl_map *isl_map_intersect_domain(
5644 struct isl_map *map, struct isl_set *set)
5646 return isl_map_reverse(
5647 isl_map_intersect_range(isl_map_reverse(map), set));
5650 struct isl_map *isl_map_apply_domain(
5651 struct isl_map *map1, struct isl_map *map2)
5653 if (!map1 || !map2)
5654 goto error;
5655 map1 = isl_map_reverse(map1);
5656 map1 = isl_map_apply_range(map1, map2);
5657 return isl_map_reverse(map1);
5658 error:
5659 isl_map_free(map1);
5660 isl_map_free(map2);
5661 return NULL;
5664 struct isl_map *isl_map_apply_range(
5665 struct isl_map *map1, struct isl_map *map2)
5667 struct isl_dim *dim_result;
5668 struct isl_map *result;
5669 int i, j;
5671 if (!map1 || !map2)
5672 goto error;
5674 dim_result = isl_dim_join(isl_dim_copy(map1->dim),
5675 isl_dim_copy(map2->dim));
5677 result = isl_map_alloc_dim(dim_result, map1->n * map2->n, 0);
5678 if (!result)
5679 goto error;
5680 for (i = 0; i < map1->n; ++i)
5681 for (j = 0; j < map2->n; ++j) {
5682 result = isl_map_add_basic_map(result,
5683 isl_basic_map_apply_range(
5684 isl_basic_map_copy(map1->p[i]),
5685 isl_basic_map_copy(map2->p[j])));
5686 if (!result)
5687 goto error;
5689 isl_map_free(map1);
5690 isl_map_free(map2);
5691 if (result && result->n <= 1)
5692 ISL_F_SET(result, ISL_MAP_DISJOINT);
5693 return result;
5694 error:
5695 isl_map_free(map1);
5696 isl_map_free(map2);
5697 return NULL;
5701 * returns range - domain
5703 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
5705 isl_dim *dims, *target_dim;
5706 struct isl_basic_set *bset;
5707 unsigned dim;
5708 unsigned nparam;
5709 int i;
5711 if (!bmap)
5712 goto error;
5713 isl_assert(bmap->ctx, isl_dim_tuple_match(bmap->dim, isl_dim_in,
5714 bmap->dim, isl_dim_out),
5715 goto error);
5716 target_dim = isl_dim_domain(isl_basic_map_get_dim(bmap));
5717 dim = isl_basic_map_n_in(bmap);
5718 nparam = isl_basic_map_n_param(bmap);
5719 bset = isl_basic_set_from_basic_map(bmap);
5720 bset = isl_basic_set_cow(bset);
5721 dims = isl_basic_set_get_dim(bset);
5722 dims = isl_dim_add(dims, isl_dim_set, dim);
5723 bset = isl_basic_set_extend_dim(bset, dims, 0, dim, 0);
5724 bset = isl_basic_set_swap_vars(bset, 2*dim);
5725 for (i = 0; i < dim; ++i) {
5726 int j = isl_basic_map_alloc_equality(
5727 (struct isl_basic_map *)bset);
5728 if (j < 0)
5729 goto error;
5730 isl_seq_clr(bset->eq[j], 1 + isl_basic_set_total_dim(bset));
5731 isl_int_set_si(bset->eq[j][1+nparam+i], 1);
5732 isl_int_set_si(bset->eq[j][1+nparam+dim+i], 1);
5733 isl_int_set_si(bset->eq[j][1+nparam+2*dim+i], -1);
5735 bset = isl_basic_set_project_out(bset, isl_dim_set, dim, 2*dim);
5736 bset = isl_basic_set_reset_dim(bset, target_dim);
5737 return bset;
5738 error:
5739 isl_basic_map_free(bmap);
5740 return NULL;
5744 * returns range - domain
5746 struct isl_set *isl_map_deltas(struct isl_map *map)
5748 int i;
5749 isl_dim *dim;
5750 struct isl_set *result;
5752 if (!map)
5753 return NULL;
5755 isl_assert(map->ctx, isl_dim_tuple_match(map->dim, isl_dim_in,
5756 map->dim, isl_dim_out),
5757 goto error);
5758 dim = isl_map_get_dim(map);
5759 dim = isl_dim_domain(dim);
5760 result = isl_set_alloc_dim(dim, map->n, 0);
5761 if (!result)
5762 goto error;
5763 for (i = 0; i < map->n; ++i)
5764 result = isl_set_add_basic_set(result,
5765 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
5766 isl_map_free(map);
5767 return result;
5768 error:
5769 isl_map_free(map);
5770 return NULL;
5774 * returns [domain -> range] -> range - domain
5776 __isl_give isl_basic_map *isl_basic_map_deltas_map(
5777 __isl_take isl_basic_map *bmap)
5779 int i, k;
5780 isl_dim *dim;
5781 isl_basic_map *domain;
5782 int nparam, n;
5783 unsigned total;
5785 if (!isl_dim_tuple_match(bmap->dim, isl_dim_in, bmap->dim, isl_dim_out))
5786 isl_die(bmap->ctx, isl_error_invalid,
5787 "domain and range don't match", goto error);
5789 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5790 n = isl_basic_map_dim(bmap, isl_dim_in);
5792 dim = isl_dim_from_range(isl_dim_domain(isl_basic_map_get_dim(bmap)));
5793 domain = isl_basic_map_universe(dim);
5795 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5796 bmap = isl_basic_map_apply_range(bmap, domain);
5797 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
5799 total = isl_basic_map_total_dim(bmap);
5801 for (i = 0; i < n; ++i) {
5802 k = isl_basic_map_alloc_equality(bmap);
5803 if (k < 0)
5804 goto error;
5805 isl_seq_clr(bmap->eq[k], 1 + total);
5806 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
5807 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
5808 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
5811 bmap = isl_basic_map_gauss(bmap, NULL);
5812 return isl_basic_map_finalize(bmap);
5813 error:
5814 isl_basic_map_free(bmap);
5815 return NULL;
5819 * returns [domain -> range] -> range - domain
5821 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
5823 int i;
5824 isl_dim *domain_dim;
5826 if (!map)
5827 return NULL;
5829 if (!isl_dim_tuple_match(map->dim, isl_dim_in, map->dim, isl_dim_out))
5830 isl_die(map->ctx, isl_error_invalid,
5831 "domain and range don't match", goto error);
5833 map = isl_map_cow(map);
5834 if (!map)
5835 return NULL;
5837 domain_dim = isl_dim_from_range(isl_dim_domain(isl_map_get_dim(map)));
5838 map->dim = isl_dim_from_domain(isl_dim_wrap(map->dim));
5839 map->dim = isl_dim_join(map->dim, domain_dim);
5840 if (!map->dim)
5841 goto error;
5842 for (i = 0; i < map->n; ++i) {
5843 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
5844 if (!map->p[i])
5845 goto error;
5847 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5848 return map;
5849 error:
5850 isl_map_free(map);
5851 return NULL;
5854 static struct isl_basic_map *basic_map_identity(struct isl_dim *dims)
5856 struct isl_basic_map *bmap;
5857 unsigned nparam;
5858 unsigned dim;
5859 int i;
5861 if (!dims)
5862 return NULL;
5864 nparam = dims->nparam;
5865 dim = dims->n_out;
5866 bmap = isl_basic_map_alloc_dim(dims, 0, dim, 0);
5867 if (!bmap)
5868 goto error;
5870 for (i = 0; i < dim; ++i) {
5871 int j = isl_basic_map_alloc_equality(bmap);
5872 if (j < 0)
5873 goto error;
5874 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
5875 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
5876 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
5878 return isl_basic_map_finalize(bmap);
5879 error:
5880 isl_basic_map_free(bmap);
5881 return NULL;
5884 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_dim *dim)
5886 if (!dim)
5887 return NULL;
5888 if (dim->n_in != dim->n_out)
5889 isl_die(dim->ctx, isl_error_invalid,
5890 "number of input and output dimensions needs to be "
5891 "the same", goto error);
5892 return basic_map_identity(dim);
5893 error:
5894 isl_dim_free(dim);
5895 return NULL;
5898 struct isl_basic_map *isl_basic_map_identity_like(struct isl_basic_map *model)
5900 if (!model || !model->dim)
5901 return NULL;
5902 return isl_basic_map_identity(isl_dim_copy(model->dim));
5905 __isl_give isl_map *isl_map_identity(__isl_take isl_dim *dim)
5907 return isl_map_from_basic_map(isl_basic_map_identity(dim));
5910 struct isl_map *isl_map_identity_like(struct isl_map *model)
5912 if (!model || !model->dim)
5913 return NULL;
5914 return isl_map_identity(isl_dim_copy(model->dim));
5917 struct isl_map *isl_map_identity_like_basic_map(struct isl_basic_map *model)
5919 if (!model || !model->dim)
5920 return NULL;
5921 return isl_map_identity(isl_dim_copy(model->dim));
5924 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
5926 isl_dim *dim = isl_set_get_dim(set);
5927 isl_map *id;
5928 id = isl_map_identity(isl_dim_map_from_set(dim));
5929 return isl_map_intersect_range(id, set);
5932 /* Construct a basic set with all set dimensions having only non-negative
5933 * values.
5935 struct isl_basic_set *isl_basic_set_positive_orthant(struct isl_dim *dims)
5937 int i;
5938 unsigned nparam;
5939 unsigned dim;
5940 struct isl_basic_set *bset;
5942 if (!dims)
5943 return NULL;
5944 nparam = dims->nparam;
5945 dim = dims->n_out;
5946 bset = isl_basic_set_alloc_dim(dims, 0, 0, dim);
5947 if (!bset)
5948 return NULL;
5949 for (i = 0; i < dim; ++i) {
5950 int k = isl_basic_set_alloc_inequality(bset);
5951 if (k < 0)
5952 goto error;
5953 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
5954 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
5956 return bset;
5957 error:
5958 isl_basic_set_free(bset);
5959 return NULL;
5962 /* Construct the half-space x_pos >= 0.
5964 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_dim *dim,
5965 int pos)
5967 int k;
5968 isl_basic_set *nonneg;
5970 nonneg = isl_basic_set_alloc_dim(dim, 0, 0, 1);
5971 k = isl_basic_set_alloc_inequality(nonneg);
5972 if (k < 0)
5973 goto error;
5974 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
5975 isl_int_set_si(nonneg->ineq[k][pos], 1);
5977 return isl_basic_set_finalize(nonneg);
5978 error:
5979 isl_basic_set_free(nonneg);
5980 return NULL;
5983 /* Construct the half-space x_pos <= -1.
5985 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_dim *dim, int pos)
5987 int k;
5988 isl_basic_set *neg;
5990 neg = isl_basic_set_alloc_dim(dim, 0, 0, 1);
5991 k = isl_basic_set_alloc_inequality(neg);
5992 if (k < 0)
5993 goto error;
5994 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
5995 isl_int_set_si(neg->ineq[k][0], -1);
5996 isl_int_set_si(neg->ineq[k][pos], -1);
5998 return isl_basic_set_finalize(neg);
5999 error:
6000 isl_basic_set_free(neg);
6001 return NULL;
6004 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
6005 enum isl_dim_type type, unsigned first, unsigned n)
6007 int i;
6008 isl_basic_set *nonneg;
6009 isl_basic_set *neg;
6011 if (!set)
6012 return NULL;
6013 if (n == 0)
6014 return set;
6016 isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
6018 for (i = 0; i < n; ++i) {
6019 nonneg = nonneg_halfspace(isl_set_get_dim(set),
6020 pos(set->dim, type) + first + i);
6021 neg = neg_halfspace(isl_set_get_dim(set),
6022 pos(set->dim, type) + first + i);
6024 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
6027 return set;
6028 error:
6029 isl_set_free(set);
6030 return NULL;
6033 static int foreach_orthant(__isl_take isl_set *set, int *signs, int first,
6034 int len, int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
6035 void *user)
6037 isl_set *half;
6039 if (!set)
6040 return -1;
6041 if (isl_set_plain_is_empty(set)) {
6042 isl_set_free(set);
6043 return 0;
6045 if (first == len)
6046 return fn(set, signs, user);
6048 signs[first] = 1;
6049 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_dim(set),
6050 1 + first));
6051 half = isl_set_intersect(half, isl_set_copy(set));
6052 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
6053 goto error;
6055 signs[first] = -1;
6056 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_dim(set),
6057 1 + first));
6058 half = isl_set_intersect(half, set);
6059 return foreach_orthant(half, signs, first + 1, len, fn, user);
6060 error:
6061 isl_set_free(set);
6062 return -1;
6065 /* Call "fn" on the intersections of "set" with each of the orthants
6066 * (except for obviously empty intersections). The orthant is identified
6067 * by the signs array, with each entry having value 1 or -1 according
6068 * to the sign of the corresponding variable.
6070 int isl_set_foreach_orthant(__isl_keep isl_set *set,
6071 int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
6072 void *user)
6074 unsigned nparam;
6075 unsigned nvar;
6076 int *signs;
6077 int r;
6079 if (!set)
6080 return -1;
6081 if (isl_set_plain_is_empty(set))
6082 return 0;
6084 nparam = isl_set_dim(set, isl_dim_param);
6085 nvar = isl_set_dim(set, isl_dim_set);
6087 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
6089 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
6090 fn, user);
6092 free(signs);
6094 return r;
6097 int isl_set_is_equal(struct isl_set *set1, struct isl_set *set2)
6099 return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
6102 int isl_basic_map_is_subset(
6103 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6105 int is_subset;
6106 struct isl_map *map1;
6107 struct isl_map *map2;
6109 if (!bmap1 || !bmap2)
6110 return -1;
6112 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
6113 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
6115 is_subset = isl_map_is_subset(map1, map2);
6117 isl_map_free(map1);
6118 isl_map_free(map2);
6120 return is_subset;
6123 int isl_basic_map_is_equal(
6124 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6126 int is_subset;
6128 if (!bmap1 || !bmap2)
6129 return -1;
6130 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
6131 if (is_subset != 1)
6132 return is_subset;
6133 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
6134 return is_subset;
6137 int isl_basic_set_is_equal(
6138 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
6140 return isl_basic_map_is_equal(
6141 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
6144 int isl_map_is_empty(struct isl_map *map)
6146 int i;
6147 int is_empty;
6149 if (!map)
6150 return -1;
6151 for (i = 0; i < map->n; ++i) {
6152 is_empty = isl_basic_map_is_empty(map->p[i]);
6153 if (is_empty < 0)
6154 return -1;
6155 if (!is_empty)
6156 return 0;
6158 return 1;
6161 int isl_map_plain_is_empty(__isl_keep isl_map *map)
6163 return map ? map->n == 0 : -1;
6166 int isl_map_fast_is_empty(__isl_keep isl_map *map)
6168 return isl_map_plain_is_empty(map);
6171 int isl_set_plain_is_empty(struct isl_set *set)
6173 return set ? set->n == 0 : -1;
6176 int isl_set_fast_is_empty(__isl_keep isl_set *set)
6178 return isl_set_plain_is_empty(set);
6181 int isl_set_is_empty(struct isl_set *set)
6183 return isl_map_is_empty((struct isl_map *)set);
6186 int isl_map_has_equal_dim(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
6188 if (!map1 || !map2)
6189 return -1;
6191 return isl_dim_equal(map1->dim, map2->dim);
6194 int isl_set_has_equal_dim(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
6196 if (!set1 || !set2)
6197 return -1;
6199 return isl_dim_equal(set1->dim, set2->dim);
6202 int isl_map_is_equal(struct isl_map *map1, struct isl_map *map2)
6204 int is_subset;
6206 if (!map1 || !map2)
6207 return -1;
6208 is_subset = isl_map_is_subset(map1, map2);
6209 if (is_subset != 1)
6210 return is_subset;
6211 is_subset = isl_map_is_subset(map2, map1);
6212 return is_subset;
6215 int isl_basic_map_is_strict_subset(
6216 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6218 int is_subset;
6220 if (!bmap1 || !bmap2)
6221 return -1;
6222 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
6223 if (is_subset != 1)
6224 return is_subset;
6225 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
6226 if (is_subset == -1)
6227 return is_subset;
6228 return !is_subset;
6231 int isl_map_is_strict_subset(struct isl_map *map1, struct isl_map *map2)
6233 int is_subset;
6235 if (!map1 || !map2)
6236 return -1;
6237 is_subset = isl_map_is_subset(map1, map2);
6238 if (is_subset != 1)
6239 return is_subset;
6240 is_subset = isl_map_is_subset(map2, map1);
6241 if (is_subset == -1)
6242 return is_subset;
6243 return !is_subset;
6246 int isl_set_is_strict_subset(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
6248 return isl_map_is_strict_subset((isl_map *)set1, (isl_map *)set2);
6251 int isl_basic_map_is_universe(struct isl_basic_map *bmap)
6253 if (!bmap)
6254 return -1;
6255 return bmap->n_eq == 0 && bmap->n_ineq == 0;
6258 int isl_basic_set_is_universe(struct isl_basic_set *bset)
6260 if (!bset)
6261 return -1;
6262 return bset->n_eq == 0 && bset->n_ineq == 0;
6265 int isl_map_plain_is_universe(__isl_keep isl_map *map)
6267 int i;
6269 if (!map)
6270 return -1;
6272 for (i = 0; i < map->n; ++i) {
6273 int r = isl_basic_map_is_universe(map->p[i]);
6274 if (r < 0 || r)
6275 return r;
6278 return 0;
6281 int isl_set_plain_is_universe(__isl_keep isl_set *set)
6283 return isl_map_plain_is_universe((isl_map *) set);
6286 int isl_set_fast_is_universe(__isl_keep isl_set *set)
6288 return isl_set_plain_is_universe(set);
6291 int isl_basic_map_is_empty(struct isl_basic_map *bmap)
6293 struct isl_basic_set *bset = NULL;
6294 struct isl_vec *sample = NULL;
6295 int empty;
6296 unsigned total;
6298 if (!bmap)
6299 return -1;
6301 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
6302 return 1;
6304 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
6305 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
6306 copy = isl_basic_map_remove_redundancies(copy);
6307 empty = ISL_F_ISSET(copy, ISL_BASIC_MAP_EMPTY);
6308 isl_basic_map_free(copy);
6309 return empty;
6312 total = 1 + isl_basic_map_total_dim(bmap);
6313 if (bmap->sample && bmap->sample->size == total) {
6314 int contains = isl_basic_map_contains(bmap, bmap->sample);
6315 if (contains < 0)
6316 return -1;
6317 if (contains)
6318 return 0;
6320 isl_vec_free(bmap->sample);
6321 bmap->sample = NULL;
6322 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
6323 if (!bset)
6324 return -1;
6325 sample = isl_basic_set_sample_vec(bset);
6326 if (!sample)
6327 return -1;
6328 empty = sample->size == 0;
6329 isl_vec_free(bmap->sample);
6330 bmap->sample = sample;
6331 if (empty)
6332 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
6334 return empty;
6337 int isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
6339 if (!bmap)
6340 return -1;
6341 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
6344 int isl_basic_map_fast_is_empty(__isl_keep isl_basic_map *bmap)
6346 return isl_basic_map_plain_is_empty(bmap);
6349 int isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
6351 if (!bset)
6352 return -1;
6353 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
6356 int isl_basic_set_fast_is_empty(__isl_keep isl_basic_set *bset)
6358 return isl_basic_set_plain_is_empty(bset);
6361 int isl_basic_set_is_empty(struct isl_basic_set *bset)
6363 return isl_basic_map_is_empty((struct isl_basic_map *)bset);
6366 struct isl_map *isl_basic_map_union(
6367 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6369 struct isl_map *map;
6370 if (!bmap1 || !bmap2)
6371 return NULL;
6373 isl_assert(bmap1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim), goto error);
6375 map = isl_map_alloc_dim(isl_dim_copy(bmap1->dim), 2, 0);
6376 if (!map)
6377 goto error;
6378 map = isl_map_add_basic_map(map, bmap1);
6379 map = isl_map_add_basic_map(map, bmap2);
6380 return map;
6381 error:
6382 isl_basic_map_free(bmap1);
6383 isl_basic_map_free(bmap2);
6384 return NULL;
6387 struct isl_set *isl_basic_set_union(
6388 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
6390 return (struct isl_set *)isl_basic_map_union(
6391 (struct isl_basic_map *)bset1,
6392 (struct isl_basic_map *)bset2);
6395 /* Order divs such that any div only depends on previous divs */
6396 struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
6398 int i;
6399 unsigned off;
6401 if (!bmap)
6402 return NULL;
6404 off = isl_dim_total(bmap->dim);
6406 for (i = 0; i < bmap->n_div; ++i) {
6407 int pos;
6408 if (isl_int_is_zero(bmap->div[i][0]))
6409 continue;
6410 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
6411 bmap->n_div-i);
6412 if (pos == -1)
6413 continue;
6414 isl_basic_map_swap_div(bmap, i, i + pos);
6415 --i;
6417 return bmap;
6420 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
6422 return (struct isl_basic_set *)
6423 isl_basic_map_order_divs((struct isl_basic_map *)bset);
6426 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
6428 int i;
6430 if (!map)
6431 return 0;
6433 for (i = 0; i < map->n; ++i) {
6434 map->p[i] = isl_basic_map_order_divs(map->p[i]);
6435 if (!map->p[i])
6436 goto error;
6439 return map;
6440 error:
6441 isl_map_free(map);
6442 return NULL;
6445 /* Apply the expansion computed by isl_merge_divs.
6446 * The expansion itself is given by "exp" while the resulting
6447 * list of divs is given by "div".
6449 __isl_give isl_basic_set *isl_basic_set_expand_divs(
6450 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
6452 int i, j;
6453 int n_div;
6455 bset = isl_basic_set_cow(bset);
6456 if (!bset || !div)
6457 goto error;
6459 if (div->n_row < bset->n_div)
6460 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
6461 "not an expansion", goto error);
6463 bset = isl_basic_map_extend_dim(bset, isl_dim_copy(bset->dim),
6464 div->n_row - bset->n_div, 0,
6465 2 * (div->n_row - bset->n_div));
6467 n_div = bset->n_div;
6468 for (i = n_div; i < div->n_row; ++i)
6469 if (isl_basic_set_alloc_div(bset) < 0)
6470 goto error;
6472 j = n_div - 1;
6473 for (i = div->n_row - 1; i >= 0; --i) {
6474 if (j >= 0 && exp[j] == i) {
6475 if (i != j)
6476 isl_basic_map_swap_div(bset, i, j);
6477 j--;
6478 } else {
6479 isl_seq_cpy(bset->div[i], div->row[i], div->n_col);
6480 if (isl_basic_map_add_div_constraints(bset, i) < 0)
6481 goto error;
6485 isl_mat_free(div);
6486 return bset;
6487 error:
6488 isl_basic_set_free(bset);
6489 isl_mat_free(div);
6490 return NULL;
6493 /* Look for a div in dst that corresponds to the div "div" in src.
6494 * The divs before "div" in src and dst are assumed to be the same.
6496 * Returns -1 if no corresponding div was found and the position
6497 * of the corresponding div in dst otherwise.
6499 static int find_div(struct isl_basic_map *dst,
6500 struct isl_basic_map *src, unsigned div)
6502 int i;
6504 unsigned total = isl_dim_total(src->dim);
6506 isl_assert(dst->ctx, div <= dst->n_div, return -1);
6507 for (i = div; i < dst->n_div; ++i)
6508 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
6509 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
6510 dst->n_div - div) == -1)
6511 return i;
6512 return -1;
6515 struct isl_basic_map *isl_basic_map_align_divs(
6516 struct isl_basic_map *dst, struct isl_basic_map *src)
6518 int i;
6519 unsigned total = isl_dim_total(src->dim);
6521 if (!dst || !src)
6522 goto error;
6524 if (src->n_div == 0)
6525 return dst;
6527 for (i = 0; i < src->n_div; ++i)
6528 isl_assert(src->ctx, !isl_int_is_zero(src->div[i][0]), goto error);
6530 src = isl_basic_map_order_divs(src);
6531 dst = isl_basic_map_cow(dst);
6532 dst = isl_basic_map_extend_dim(dst, isl_dim_copy(dst->dim),
6533 src->n_div, 0, 2 * src->n_div);
6534 if (!dst)
6535 return NULL;
6536 for (i = 0; i < src->n_div; ++i) {
6537 int j = find_div(dst, src, i);
6538 if (j < 0) {
6539 j = isl_basic_map_alloc_div(dst);
6540 if (j < 0)
6541 goto error;
6542 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
6543 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
6544 if (isl_basic_map_add_div_constraints(dst, j) < 0)
6545 goto error;
6547 if (j != i)
6548 isl_basic_map_swap_div(dst, i, j);
6550 return dst;
6551 error:
6552 isl_basic_map_free(dst);
6553 return NULL;
6556 struct isl_basic_set *isl_basic_set_align_divs(
6557 struct isl_basic_set *dst, struct isl_basic_set *src)
6559 return (struct isl_basic_set *)isl_basic_map_align_divs(
6560 (struct isl_basic_map *)dst, (struct isl_basic_map *)src);
6563 struct isl_map *isl_map_align_divs(struct isl_map *map)
6565 int i;
6567 if (!map)
6568 return NULL;
6569 if (map->n == 0)
6570 return map;
6571 map = isl_map_compute_divs(map);
6572 map = isl_map_cow(map);
6573 if (!map)
6574 return NULL;
6576 for (i = 1; i < map->n; ++i)
6577 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
6578 for (i = 1; i < map->n; ++i)
6579 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
6581 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6582 return map;
6585 struct isl_set *isl_set_align_divs(struct isl_set *set)
6587 return (struct isl_set *)isl_map_align_divs((struct isl_map *)set);
6590 struct isl_set *isl_set_apply(struct isl_set *set, struct isl_map *map)
6592 if (!set || !map)
6593 goto error;
6594 isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
6595 map = isl_map_intersect_domain(map, set);
6596 set = isl_map_range(map);
6597 return set;
6598 error:
6599 isl_set_free(set);
6600 isl_map_free(map);
6601 return NULL;
6604 /* There is no need to cow as removing empty parts doesn't change
6605 * the meaning of the set.
6607 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
6609 int i;
6611 if (!map)
6612 return NULL;
6614 for (i = map->n-1; i >= 0; --i) {
6615 if (!ISL_F_ISSET(map->p[i], ISL_BASIC_MAP_EMPTY))
6616 continue;
6617 isl_basic_map_free(map->p[i]);
6618 if (i != map->n-1) {
6619 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6620 map->p[i] = map->p[map->n-1];
6622 map->n--;
6625 return map;
6628 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
6630 return (struct isl_set *)
6631 isl_map_remove_empty_parts((struct isl_map *)set);
6634 struct isl_basic_map *isl_map_copy_basic_map(struct isl_map *map)
6636 struct isl_basic_map *bmap;
6637 if (!map || map->n == 0)
6638 return NULL;
6639 bmap = map->p[map->n-1];
6640 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
6641 return isl_basic_map_copy(bmap);
6644 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
6646 return (struct isl_basic_set *)
6647 isl_map_copy_basic_map((struct isl_map *)set);
6650 __isl_give isl_map *isl_map_drop_basic_map(__isl_take isl_map *map,
6651 __isl_keep isl_basic_map *bmap)
6653 int i;
6655 if (!map || !bmap)
6656 goto error;
6657 for (i = map->n-1; i >= 0; --i) {
6658 if (map->p[i] != bmap)
6659 continue;
6660 map = isl_map_cow(map);
6661 if (!map)
6662 goto error;
6663 isl_basic_map_free(map->p[i]);
6664 if (i != map->n-1) {
6665 ISL_F_CLR(map, ISL_SET_NORMALIZED);
6666 map->p[i] = map->p[map->n-1];
6668 map->n--;
6669 return map;
6671 return map;
6672 error:
6673 isl_map_free(map);
6674 return NULL;
6677 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
6678 struct isl_basic_set *bset)
6680 return (struct isl_set *)isl_map_drop_basic_map((struct isl_map *)set,
6681 (struct isl_basic_map *)bset);
6684 /* Given two basic sets bset1 and bset2, compute the maximal difference
6685 * between the values of dimension pos in bset1 and those in bset2
6686 * for any common value of the parameters and dimensions preceding pos.
6688 static enum isl_lp_result basic_set_maximal_difference_at(
6689 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
6690 int pos, isl_int *opt)
6692 struct isl_dim *dims;
6693 struct isl_basic_map *bmap1 = NULL;
6694 struct isl_basic_map *bmap2 = NULL;
6695 struct isl_ctx *ctx;
6696 struct isl_vec *obj;
6697 unsigned total;
6698 unsigned nparam;
6699 unsigned dim1, dim2;
6700 enum isl_lp_result res;
6702 if (!bset1 || !bset2)
6703 return isl_lp_error;
6705 nparam = isl_basic_set_n_param(bset1);
6706 dim1 = isl_basic_set_n_dim(bset1);
6707 dim2 = isl_basic_set_n_dim(bset2);
6708 dims = isl_dim_alloc(bset1->ctx, nparam, pos, dim1 - pos);
6709 bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1), dims);
6710 dims = isl_dim_alloc(bset2->ctx, nparam, pos, dim2 - pos);
6711 bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims);
6712 if (!bmap1 || !bmap2)
6713 goto error;
6714 bmap1 = isl_basic_map_cow(bmap1);
6715 bmap1 = isl_basic_map_extend(bmap1, nparam,
6716 pos, (dim1 - pos) + (dim2 - pos),
6717 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
6718 bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
6719 if (!bmap1)
6720 goto error;
6721 total = isl_basic_map_total_dim(bmap1);
6722 ctx = bmap1->ctx;
6723 obj = isl_vec_alloc(ctx, 1 + total);
6724 isl_seq_clr(obj->block.data, 1 + total);
6725 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
6726 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
6727 if (!obj)
6728 goto error;
6729 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
6730 opt, NULL, NULL);
6731 isl_basic_map_free(bmap1);
6732 isl_vec_free(obj);
6733 return res;
6734 error:
6735 isl_basic_map_free(bmap1);
6736 isl_basic_map_free(bmap2);
6737 return isl_lp_error;
6740 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
6741 * for any common value of the parameters and dimensions preceding pos
6742 * in both basic sets, the values of dimension pos in bset1 are
6743 * smaller or larger than those in bset2.
6745 * Returns
6746 * 1 if bset1 follows bset2
6747 * -1 if bset1 precedes bset2
6748 * 0 if bset1 and bset2 are incomparable
6749 * -2 if some error occurred.
6751 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
6752 struct isl_basic_set *bset2, int pos)
6754 isl_int opt;
6755 enum isl_lp_result res;
6756 int cmp;
6758 isl_int_init(opt);
6760 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
6762 if (res == isl_lp_empty)
6763 cmp = 0;
6764 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
6765 res == isl_lp_unbounded)
6766 cmp = 1;
6767 else if (res == isl_lp_ok && isl_int_is_neg(opt))
6768 cmp = -1;
6769 else
6770 cmp = -2;
6772 isl_int_clear(opt);
6773 return cmp;
6776 /* Given two basic sets bset1 and bset2, check whether
6777 * for any common value of the parameters and dimensions preceding pos
6778 * there is a value of dimension pos in bset1 that is larger
6779 * than a value of the same dimension in bset2.
6781 * Return
6782 * 1 if there exists such a pair
6783 * 0 if there is no such pair, but there is a pair of equal values
6784 * -1 otherwise
6785 * -2 if some error occurred.
6787 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
6788 __isl_keep isl_basic_set *bset2, int pos)
6790 isl_int opt;
6791 enum isl_lp_result res;
6792 int cmp;
6794 isl_int_init(opt);
6796 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
6798 if (res == isl_lp_empty)
6799 cmp = -1;
6800 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
6801 res == isl_lp_unbounded)
6802 cmp = 1;
6803 else if (res == isl_lp_ok && isl_int_is_neg(opt))
6804 cmp = -1;
6805 else if (res == isl_lp_ok)
6806 cmp = 0;
6807 else
6808 cmp = -2;
6810 isl_int_clear(opt);
6811 return cmp;
6814 /* Given two sets set1 and set2, check whether
6815 * for any common value of the parameters and dimensions preceding pos
6816 * there is a value of dimension pos in set1 that is larger
6817 * than a value of the same dimension in set2.
6819 * Return
6820 * 1 if there exists such a pair
6821 * 0 if there is no such pair, but there is a pair of equal values
6822 * -1 otherwise
6823 * -2 if some error occurred.
6825 int isl_set_follows_at(__isl_keep isl_set *set1,
6826 __isl_keep isl_set *set2, int pos)
6828 int i, j;
6829 int follows = -1;
6831 if (!set1 || !set2)
6832 return -2;
6834 for (i = 0; i < set1->n; ++i)
6835 for (j = 0; j < set2->n; ++j) {
6836 int f;
6837 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
6838 if (f == 1 || f == -2)
6839 return f;
6840 if (f > follows)
6841 follows = f;
6844 return follows;
6847 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map *bmap,
6848 unsigned pos, isl_int *val)
6850 int i;
6851 int d;
6852 unsigned total;
6854 if (!bmap)
6855 return -1;
6856 total = isl_basic_map_total_dim(bmap);
6857 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
6858 for (; d+1 > pos; --d)
6859 if (!isl_int_is_zero(bmap->eq[i][1+d]))
6860 break;
6861 if (d != pos)
6862 continue;
6863 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
6864 return 0;
6865 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
6866 return 0;
6867 if (!isl_int_is_one(bmap->eq[i][1+d]))
6868 return 0;
6869 if (val)
6870 isl_int_neg(*val, bmap->eq[i][0]);
6871 return 1;
6873 return 0;
6876 static int isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
6877 unsigned pos, isl_int *val)
6879 int i;
6880 isl_int v;
6881 isl_int tmp;
6882 int fixed;
6884 if (!map)
6885 return -1;
6886 if (map->n == 0)
6887 return 0;
6888 if (map->n == 1)
6889 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
6890 isl_int_init(v);
6891 isl_int_init(tmp);
6892 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
6893 for (i = 1; fixed == 1 && i < map->n; ++i) {
6894 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
6895 if (fixed == 1 && isl_int_ne(tmp, v))
6896 fixed = 0;
6898 if (val)
6899 isl_int_set(*val, v);
6900 isl_int_clear(tmp);
6901 isl_int_clear(v);
6902 return fixed;
6905 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set *bset,
6906 unsigned pos, isl_int *val)
6908 return isl_basic_map_plain_has_fixed_var((struct isl_basic_map *)bset,
6909 pos, val);
6912 static int isl_set_plain_has_fixed_var(__isl_keep isl_set *set, unsigned pos,
6913 isl_int *val)
6915 return isl_map_plain_has_fixed_var((struct isl_map *)set, pos, val);
6918 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
6919 enum isl_dim_type type, unsigned pos, isl_int *val)
6921 if (pos >= isl_basic_map_dim(bmap, type))
6922 return -1;
6923 return isl_basic_map_plain_has_fixed_var(bmap,
6924 isl_basic_map_offset(bmap, type) - 1 + pos, val);
6927 int isl_map_plain_is_fixed(__isl_keep isl_map *map,
6928 enum isl_dim_type type, unsigned pos, isl_int *val)
6930 if (pos >= isl_map_dim(map, type))
6931 return -1;
6932 return isl_map_plain_has_fixed_var(map,
6933 map_offset(map, type) - 1 + pos, val);
6936 int isl_map_fast_is_fixed(__isl_keep isl_map *map,
6937 enum isl_dim_type type, unsigned pos, isl_int *val)
6939 return isl_map_plain_is_fixed(map, type, pos, val);
6942 /* Check if dimension dim has fixed value and if so and if val is not NULL,
6943 * then return this fixed value in *val.
6945 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
6946 unsigned dim, isl_int *val)
6948 return isl_basic_set_plain_has_fixed_var(bset,
6949 isl_basic_set_n_param(bset) + dim, val);
6952 /* Check if dimension dim has fixed value and if so and if val is not NULL,
6953 * then return this fixed value in *val.
6955 int isl_set_plain_dim_is_fixed(__isl_keep isl_set *set,
6956 unsigned dim, isl_int *val)
6958 return isl_set_plain_has_fixed_var(set, isl_set_n_param(set) + dim, val);
6961 int isl_set_fast_dim_is_fixed(__isl_keep isl_set *set,
6962 unsigned dim, isl_int *val)
6964 return isl_set_plain_dim_is_fixed(set, dim, val);
6967 /* Check if input variable in has fixed value and if so and if val is not NULL,
6968 * then return this fixed value in *val.
6970 int isl_map_plain_input_is_fixed(__isl_keep isl_map *map,
6971 unsigned in, isl_int *val)
6973 return isl_map_plain_has_fixed_var(map, isl_map_n_param(map) + in, val);
6976 /* Check if dimension dim has an (obvious) fixed lower bound and if so
6977 * and if val is not NULL, then return this lower bound in *val.
6979 int isl_basic_set_plain_dim_has_fixed_lower_bound(
6980 __isl_keep isl_basic_set *bset, unsigned dim, isl_int *val)
6982 int i, i_eq = -1, i_ineq = -1;
6983 isl_int *c;
6984 unsigned total;
6985 unsigned nparam;
6987 if (!bset)
6988 return -1;
6989 total = isl_basic_set_total_dim(bset);
6990 nparam = isl_basic_set_n_param(bset);
6991 for (i = 0; i < bset->n_eq; ++i) {
6992 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
6993 continue;
6994 if (i_eq != -1)
6995 return 0;
6996 i_eq = i;
6998 for (i = 0; i < bset->n_ineq; ++i) {
6999 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
7000 continue;
7001 if (i_eq != -1 || i_ineq != -1)
7002 return 0;
7003 i_ineq = i;
7005 if (i_eq == -1 && i_ineq == -1)
7006 return 0;
7007 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
7008 /* The coefficient should always be one due to normalization. */
7009 if (!isl_int_is_one(c[1+nparam+dim]))
7010 return 0;
7011 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
7012 return 0;
7013 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
7014 total - nparam - dim - 1) != -1)
7015 return 0;
7016 if (val)
7017 isl_int_neg(*val, c[0]);
7018 return 1;
7021 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set *set,
7022 unsigned dim, isl_int *val)
7024 int i;
7025 isl_int v;
7026 isl_int tmp;
7027 int fixed;
7029 if (!set)
7030 return -1;
7031 if (set->n == 0)
7032 return 0;
7033 if (set->n == 1)
7034 return isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
7035 dim, val);
7036 isl_int_init(v);
7037 isl_int_init(tmp);
7038 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
7039 dim, &v);
7040 for (i = 1; fixed == 1 && i < set->n; ++i) {
7041 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[i],
7042 dim, &tmp);
7043 if (fixed == 1 && isl_int_ne(tmp, v))
7044 fixed = 0;
7046 if (val)
7047 isl_int_set(*val, v);
7048 isl_int_clear(tmp);
7049 isl_int_clear(v);
7050 return fixed;
7053 struct constraint {
7054 unsigned size;
7055 isl_int *c;
7058 /* uset_gist depends on constraints without existentially quantified
7059 * variables sorting first.
7061 static int qsort_constraint_cmp(const void *p1, const void *p2)
7063 const struct constraint *c1 = (const struct constraint *)p1;
7064 const struct constraint *c2 = (const struct constraint *)p2;
7065 int l1, l2;
7066 unsigned size = isl_min(c1->size, c2->size);
7068 l1 = isl_seq_last_non_zero(c1->c, size);
7069 l2 = isl_seq_last_non_zero(c2->c, size);
7071 if (l1 != l2)
7072 return l1 - l2;
7074 return isl_seq_cmp(c1->c, c2->c, size);
7077 static struct isl_basic_map *isl_basic_map_sort_constraints(
7078 struct isl_basic_map *bmap)
7080 int i;
7081 struct constraint *c;
7082 unsigned total;
7084 if (!bmap)
7085 return NULL;
7086 total = isl_basic_map_total_dim(bmap);
7087 c = isl_alloc_array(bmap->ctx, struct constraint, bmap->n_ineq);
7088 if (!c)
7089 goto error;
7090 for (i = 0; i < bmap->n_ineq; ++i) {
7091 c[i].size = total;
7092 c[i].c = bmap->ineq[i];
7094 qsort(c, bmap->n_ineq, sizeof(struct constraint), qsort_constraint_cmp);
7095 for (i = 0; i < bmap->n_ineq; ++i)
7096 bmap->ineq[i] = c[i].c;
7097 free(c);
7098 return bmap;
7099 error:
7100 isl_basic_map_free(bmap);
7101 return NULL;
7104 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
7105 __isl_take isl_basic_set *bset)
7107 return (struct isl_basic_set *)isl_basic_map_sort_constraints(
7108 (struct isl_basic_map *)bset);
7111 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
7113 if (!bmap)
7114 return NULL;
7115 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
7116 return bmap;
7117 bmap = isl_basic_map_remove_redundancies(bmap);
7118 bmap = isl_basic_map_sort_constraints(bmap);
7119 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
7120 return bmap;
7123 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
7125 return (struct isl_basic_set *)isl_basic_map_normalize(
7126 (struct isl_basic_map *)bset);
7129 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map *bmap1,
7130 const __isl_keep isl_basic_map *bmap2)
7132 int i, cmp;
7133 unsigned total;
7135 if (bmap1 == bmap2)
7136 return 0;
7137 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
7138 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
7139 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
7140 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
7141 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
7142 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
7143 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
7144 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
7145 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
7146 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
7147 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
7148 return 0;
7149 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
7150 return 1;
7151 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
7152 return -1;
7153 if (bmap1->n_eq != bmap2->n_eq)
7154 return bmap1->n_eq - bmap2->n_eq;
7155 if (bmap1->n_ineq != bmap2->n_ineq)
7156 return bmap1->n_ineq - bmap2->n_ineq;
7157 if (bmap1->n_div != bmap2->n_div)
7158 return bmap1->n_div - bmap2->n_div;
7159 total = isl_basic_map_total_dim(bmap1);
7160 for (i = 0; i < bmap1->n_eq; ++i) {
7161 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
7162 if (cmp)
7163 return cmp;
7165 for (i = 0; i < bmap1->n_ineq; ++i) {
7166 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
7167 if (cmp)
7168 return cmp;
7170 for (i = 0; i < bmap1->n_div; ++i) {
7171 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
7172 if (cmp)
7173 return cmp;
7175 return 0;
7178 int isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
7179 __isl_keep isl_basic_map *bmap2)
7181 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
7184 int isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
7185 __isl_keep isl_basic_set *bset2)
7187 return isl_basic_map_plain_is_equal((isl_basic_map *)bset1,
7188 (isl_basic_map *)bset2);
7191 static int qsort_bmap_cmp(const void *p1, const void *p2)
7193 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
7194 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
7196 return isl_basic_map_plain_cmp(bmap1, bmap2);
7199 /* We normalize in place, but if anything goes wrong we need
7200 * to return NULL, so we need to make sure we don't change the
7201 * meaning of any possible other copies of map.
7203 struct isl_map *isl_map_normalize(struct isl_map *map)
7205 int i, j;
7206 struct isl_basic_map *bmap;
7208 if (!map)
7209 return NULL;
7210 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
7211 return map;
7212 for (i = 0; i < map->n; ++i) {
7213 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
7214 if (!bmap)
7215 goto error;
7216 isl_basic_map_free(map->p[i]);
7217 map->p[i] = bmap;
7219 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
7220 ISL_F_SET(map, ISL_MAP_NORMALIZED);
7221 map = isl_map_remove_empty_parts(map);
7222 if (!map)
7223 return NULL;
7224 for (i = map->n - 1; i >= 1; --i) {
7225 if (!isl_basic_map_plain_is_equal(map->p[i-1], map->p[i]))
7226 continue;
7227 isl_basic_map_free(map->p[i-1]);
7228 for (j = i; j < map->n; ++j)
7229 map->p[j-1] = map->p[j];
7230 map->n--;
7232 return map;
7233 error:
7234 isl_map_free(map);
7235 return NULL;
7239 struct isl_set *isl_set_normalize(struct isl_set *set)
7241 return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
7244 int isl_map_plain_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7246 int i;
7247 int equal;
7249 if (!map1 || !map2)
7250 return -1;
7252 if (map1 == map2)
7253 return 1;
7254 if (!isl_dim_equal(map1->dim, map2->dim))
7255 return 0;
7257 map1 = isl_map_copy(map1);
7258 map2 = isl_map_copy(map2);
7259 map1 = isl_map_normalize(map1);
7260 map2 = isl_map_normalize(map2);
7261 if (!map1 || !map2)
7262 goto error;
7263 equal = map1->n == map2->n;
7264 for (i = 0; equal && i < map1->n; ++i) {
7265 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
7266 if (equal < 0)
7267 goto error;
7269 isl_map_free(map1);
7270 isl_map_free(map2);
7271 return equal;
7272 error:
7273 isl_map_free(map1);
7274 isl_map_free(map2);
7275 return -1;
7278 int isl_map_fast_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7280 return isl_map_plain_is_equal(map1, map2);
7283 int isl_set_plain_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7285 return isl_map_plain_is_equal((struct isl_map *)set1,
7286 (struct isl_map *)set2);
7289 int isl_set_fast_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7291 return isl_set_plain_is_equal(set1, set2);
7294 /* Return an interval that ranges from min to max (inclusive)
7296 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
7297 isl_int min, isl_int max)
7299 int k;
7300 struct isl_basic_set *bset = NULL;
7302 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
7303 if (!bset)
7304 goto error;
7306 k = isl_basic_set_alloc_inequality(bset);
7307 if (k < 0)
7308 goto error;
7309 isl_int_set_si(bset->ineq[k][1], 1);
7310 isl_int_neg(bset->ineq[k][0], min);
7312 k = isl_basic_set_alloc_inequality(bset);
7313 if (k < 0)
7314 goto error;
7315 isl_int_set_si(bset->ineq[k][1], -1);
7316 isl_int_set(bset->ineq[k][0], max);
7318 return bset;
7319 error:
7320 isl_basic_set_free(bset);
7321 return NULL;
7324 /* Return the Cartesian product of the basic sets in list (in the given order).
7326 __isl_give isl_basic_set *isl_basic_set_list_product(
7327 __isl_take struct isl_basic_set_list *list)
7329 int i;
7330 unsigned dim;
7331 unsigned nparam;
7332 unsigned extra;
7333 unsigned n_eq;
7334 unsigned n_ineq;
7335 struct isl_basic_set *product = NULL;
7337 if (!list)
7338 goto error;
7339 isl_assert(list->ctx, list->n > 0, goto error);
7340 isl_assert(list->ctx, list->p[0], goto error);
7341 nparam = isl_basic_set_n_param(list->p[0]);
7342 dim = isl_basic_set_n_dim(list->p[0]);
7343 extra = list->p[0]->n_div;
7344 n_eq = list->p[0]->n_eq;
7345 n_ineq = list->p[0]->n_ineq;
7346 for (i = 1; i < list->n; ++i) {
7347 isl_assert(list->ctx, list->p[i], goto error);
7348 isl_assert(list->ctx,
7349 nparam == isl_basic_set_n_param(list->p[i]), goto error);
7350 dim += isl_basic_set_n_dim(list->p[i]);
7351 extra += list->p[i]->n_div;
7352 n_eq += list->p[i]->n_eq;
7353 n_ineq += list->p[i]->n_ineq;
7355 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
7356 n_eq, n_ineq);
7357 if (!product)
7358 goto error;
7359 dim = 0;
7360 for (i = 0; i < list->n; ++i) {
7361 isl_basic_set_add_constraints(product,
7362 isl_basic_set_copy(list->p[i]), dim);
7363 dim += isl_basic_set_n_dim(list->p[i]);
7365 isl_basic_set_list_free(list);
7366 return product;
7367 error:
7368 isl_basic_set_free(product);
7369 isl_basic_set_list_free(list);
7370 return NULL;
7373 struct isl_basic_map *isl_basic_map_product(
7374 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7376 struct isl_dim *dim_result = NULL;
7377 struct isl_basic_map *bmap;
7378 unsigned in1, in2, out1, out2, nparam, total, pos;
7379 struct isl_dim_map *dim_map1, *dim_map2;
7381 if (!bmap1 || !bmap2)
7382 goto error;
7384 isl_assert(bmap1->ctx, isl_dim_match(bmap1->dim, isl_dim_param,
7385 bmap2->dim, isl_dim_param), goto error);
7386 dim_result = isl_dim_product(isl_dim_copy(bmap1->dim),
7387 isl_dim_copy(bmap2->dim));
7389 in1 = isl_basic_map_n_in(bmap1);
7390 in2 = isl_basic_map_n_in(bmap2);
7391 out1 = isl_basic_map_n_out(bmap1);
7392 out2 = isl_basic_map_n_out(bmap2);
7393 nparam = isl_basic_map_n_param(bmap1);
7395 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
7396 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
7397 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
7398 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
7399 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
7400 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
7401 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
7402 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
7403 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
7404 isl_dim_map_div(dim_map1, bmap1, pos += out2);
7405 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
7407 bmap = isl_basic_map_alloc_dim(dim_result,
7408 bmap1->n_div + bmap2->n_div,
7409 bmap1->n_eq + bmap2->n_eq,
7410 bmap1->n_ineq + bmap2->n_ineq);
7411 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
7412 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
7413 bmap = isl_basic_map_simplify(bmap);
7414 return isl_basic_map_finalize(bmap);
7415 error:
7416 isl_basic_map_free(bmap1);
7417 isl_basic_map_free(bmap2);
7418 return NULL;
7421 __isl_give isl_basic_map *isl_basic_map_flat_product(
7422 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
7424 isl_basic_map *prod;
7426 prod = isl_basic_map_product(bmap1, bmap2);
7427 prod = isl_basic_map_flatten(prod);
7428 return prod;
7431 __isl_give isl_basic_set *isl_basic_set_flat_product(
7432 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
7434 return isl_basic_map_flat_product(bset1, bset2);
7437 __isl_give isl_basic_map *isl_basic_map_range_product(
7438 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
7440 isl_dim *dim_result = NULL;
7441 isl_basic_map *bmap;
7442 unsigned in, out1, out2, nparam, total, pos;
7443 struct isl_dim_map *dim_map1, *dim_map2;
7445 if (!bmap1 || !bmap2)
7446 goto error;
7448 dim_result = isl_dim_range_product(isl_dim_copy(bmap1->dim),
7449 isl_dim_copy(bmap2->dim));
7451 in = isl_basic_map_dim(bmap1, isl_dim_in);
7452 out1 = isl_basic_map_n_out(bmap1);
7453 out2 = isl_basic_map_n_out(bmap2);
7454 nparam = isl_basic_map_n_param(bmap1);
7456 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
7457 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
7458 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
7459 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
7460 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
7461 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
7462 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
7463 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
7464 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
7465 isl_dim_map_div(dim_map1, bmap1, pos += out2);
7466 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
7468 bmap = isl_basic_map_alloc_dim(dim_result,
7469 bmap1->n_div + bmap2->n_div,
7470 bmap1->n_eq + bmap2->n_eq,
7471 bmap1->n_ineq + bmap2->n_ineq);
7472 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
7473 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
7474 bmap = isl_basic_map_simplify(bmap);
7475 return isl_basic_map_finalize(bmap);
7476 error:
7477 isl_basic_map_free(bmap1);
7478 isl_basic_map_free(bmap2);
7479 return NULL;
7482 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
7483 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
7485 isl_basic_map *prod;
7487 prod = isl_basic_map_range_product(bmap1, bmap2);
7488 prod = isl_basic_map_flatten_range(prod);
7489 return prod;
7492 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
7493 __isl_take isl_map *map2,
7494 __isl_give isl_dim *(*dim_product)(__isl_take isl_dim *left,
7495 __isl_take isl_dim *right),
7496 __isl_give isl_basic_map *(*basic_map_product)(
7497 __isl_take isl_basic_map *left, __isl_take isl_basic_map *right))
7499 unsigned flags = 0;
7500 struct isl_map *result;
7501 int i, j;
7503 if (!map1 || !map2)
7504 goto error;
7506 isl_assert(map1->ctx, isl_dim_match(map1->dim, isl_dim_param,
7507 map2->dim, isl_dim_param), goto error);
7509 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
7510 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
7511 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7513 result = isl_map_alloc_dim(dim_product(isl_dim_copy(map1->dim),
7514 isl_dim_copy(map2->dim)),
7515 map1->n * map2->n, flags);
7516 if (!result)
7517 goto error;
7518 for (i = 0; i < map1->n; ++i)
7519 for (j = 0; j < map2->n; ++j) {
7520 struct isl_basic_map *part;
7521 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
7522 isl_basic_map_copy(map2->p[j]));
7523 if (isl_basic_map_is_empty(part))
7524 isl_basic_map_free(part);
7525 else
7526 result = isl_map_add_basic_map(result, part);
7527 if (!result)
7528 goto error;
7530 isl_map_free(map1);
7531 isl_map_free(map2);
7532 return result;
7533 error:
7534 isl_map_free(map1);
7535 isl_map_free(map2);
7536 return NULL;
7539 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
7541 struct isl_map *isl_map_product(struct isl_map *map1, struct isl_map *map2)
7543 return map_product(map1, map2, &isl_dim_product, &isl_basic_map_product);
7546 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
7548 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
7549 __isl_take isl_map *map2)
7551 isl_map *prod;
7553 prod = isl_map_product(map1, map2);
7554 prod = isl_map_flatten(prod);
7555 return prod;
7558 /* Given two set A and B, construct its Cartesian product A x B.
7560 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
7562 return (struct isl_set *)isl_map_product((struct isl_map *)set1,
7563 (struct isl_map *)set2);
7566 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
7567 __isl_take isl_set *set2)
7569 return (isl_set *)isl_map_flat_product((isl_map *)set1, (isl_map *)set2);
7572 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
7574 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
7575 __isl_take isl_map *map2)
7577 return map_product(map1, map2, &isl_dim_range_product,
7578 &isl_basic_map_range_product);
7581 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
7583 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
7584 __isl_take isl_map *map2)
7586 isl_map *prod;
7588 prod = isl_map_range_product(map1, map2);
7589 prod = isl_map_flatten_range(prod);
7590 return prod;
7593 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
7595 int i;
7596 uint32_t hash = isl_hash_init();
7597 unsigned total;
7599 if (!bmap)
7600 return 0;
7601 bmap = isl_basic_map_copy(bmap);
7602 bmap = isl_basic_map_normalize(bmap);
7603 if (!bmap)
7604 return 0;
7605 total = isl_basic_map_total_dim(bmap);
7606 isl_hash_byte(hash, bmap->n_eq & 0xFF);
7607 for (i = 0; i < bmap->n_eq; ++i) {
7608 uint32_t c_hash;
7609 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
7610 isl_hash_hash(hash, c_hash);
7612 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
7613 for (i = 0; i < bmap->n_ineq; ++i) {
7614 uint32_t c_hash;
7615 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
7616 isl_hash_hash(hash, c_hash);
7618 isl_hash_byte(hash, bmap->n_div & 0xFF);
7619 for (i = 0; i < bmap->n_div; ++i) {
7620 uint32_t c_hash;
7621 if (isl_int_is_zero(bmap->div[i][0]))
7622 continue;
7623 isl_hash_byte(hash, i & 0xFF);
7624 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
7625 isl_hash_hash(hash, c_hash);
7627 isl_basic_map_free(bmap);
7628 return hash;
7631 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
7633 return isl_basic_map_get_hash((isl_basic_map *)bset);
7636 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
7638 int i;
7639 uint32_t hash;
7641 if (!map)
7642 return 0;
7643 map = isl_map_copy(map);
7644 map = isl_map_normalize(map);
7645 if (!map)
7646 return 0;
7648 hash = isl_hash_init();
7649 for (i = 0; i < map->n; ++i) {
7650 uint32_t bmap_hash;
7651 bmap_hash = isl_basic_map_get_hash(map->p[i]);
7652 isl_hash_hash(hash, bmap_hash);
7655 isl_map_free(map);
7657 return hash;
7660 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
7662 return isl_map_get_hash((isl_map *)set);
7665 /* Check if the value for dimension dim is completely determined
7666 * by the values of the other parameters and variables.
7667 * That is, check if dimension dim is involved in an equality.
7669 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
7671 int i;
7672 unsigned nparam;
7674 if (!bset)
7675 return -1;
7676 nparam = isl_basic_set_n_param(bset);
7677 for (i = 0; i < bset->n_eq; ++i)
7678 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
7679 return 1;
7680 return 0;
7683 /* Check if the value for dimension dim is completely determined
7684 * by the values of the other parameters and variables.
7685 * That is, check if dimension dim is involved in an equality
7686 * for each of the subsets.
7688 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
7690 int i;
7692 if (!set)
7693 return -1;
7694 for (i = 0; i < set->n; ++i) {
7695 int unique;
7696 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
7697 if (unique != 1)
7698 return unique;
7700 return 1;
7703 int isl_set_n_basic_set(__isl_keep isl_set *set)
7705 return set ? set->n : 0;
7708 int isl_map_foreach_basic_map(__isl_keep isl_map *map,
7709 int (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
7711 int i;
7713 if (!map)
7714 return -1;
7716 for (i = 0; i < map->n; ++i)
7717 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
7718 return -1;
7720 return 0;
7723 int isl_set_foreach_basic_set(__isl_keep isl_set *set,
7724 int (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
7726 int i;
7728 if (!set)
7729 return -1;
7731 for (i = 0; i < set->n; ++i)
7732 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
7733 return -1;
7735 return 0;
7738 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
7740 struct isl_dim *dim;
7742 if (!bset)
7743 return NULL;
7745 bset = isl_basic_set_cow(bset);
7746 if (!bset)
7747 return NULL;
7749 dim = isl_basic_set_get_dim(bset);
7750 dim = isl_dim_lift(dim, bset->n_div);
7751 if (!dim)
7752 goto error;
7753 isl_dim_free(bset->dim);
7754 bset->dim = dim;
7755 bset->extra -= bset->n_div;
7756 bset->n_div = 0;
7758 bset = isl_basic_set_finalize(bset);
7760 return bset;
7761 error:
7762 isl_basic_set_free(bset);
7763 return NULL;
7766 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
7768 int i;
7769 struct isl_dim *dim;
7770 unsigned n_div;
7772 set = isl_set_align_divs(set);
7774 if (!set)
7775 return NULL;
7777 set = isl_set_cow(set);
7778 if (!set)
7779 return NULL;
7781 n_div = set->p[0]->n_div;
7782 dim = isl_set_get_dim(set);
7783 dim = isl_dim_lift(dim, n_div);
7784 if (!dim)
7785 goto error;
7786 isl_dim_free(set->dim);
7787 set->dim = dim;
7789 for (i = 0; i < set->n; ++i) {
7790 set->p[i] = isl_basic_set_lift(set->p[i]);
7791 if (!set->p[i])
7792 goto error;
7795 return set;
7796 error:
7797 isl_set_free(set);
7798 return NULL;
7801 __isl_give isl_map *isl_set_lifting(__isl_take isl_set *set)
7803 struct isl_dim *dim;
7804 struct isl_basic_map *bmap;
7805 unsigned n_set;
7806 unsigned n_div;
7807 unsigned n_param;
7808 unsigned total;
7809 int i, k, l;
7811 set = isl_set_align_divs(set);
7813 if (!set)
7814 return NULL;
7816 dim = isl_set_get_dim(set);
7817 if (set->n == 0 || set->p[0]->n_div == 0) {
7818 isl_set_free(set);
7819 return isl_map_identity(isl_dim_map_from_set(dim));
7822 n_div = set->p[0]->n_div;
7823 dim = isl_dim_map_from_set(dim);
7824 n_param = isl_dim_size(dim, isl_dim_param);
7825 n_set = isl_dim_size(dim, isl_dim_in);
7826 dim = isl_dim_extend(dim, n_param, n_set, n_set + n_div);
7827 bmap = isl_basic_map_alloc_dim(dim, 0, n_set, 2 * n_div);
7828 for (i = 0; i < n_set; ++i)
7829 bmap = var_equal(bmap, i);
7831 total = n_param + n_set + n_set + n_div;
7832 for (i = 0; i < n_div; ++i) {
7833 k = isl_basic_map_alloc_inequality(bmap);
7834 if (k < 0)
7835 goto error;
7836 isl_seq_cpy(bmap->ineq[k], set->p[0]->div[i]+1, 1+n_param);
7837 isl_seq_clr(bmap->ineq[k]+1+n_param, n_set);
7838 isl_seq_cpy(bmap->ineq[k]+1+n_param+n_set,
7839 set->p[0]->div[i]+1+1+n_param, n_set + n_div);
7840 isl_int_neg(bmap->ineq[k][1+n_param+n_set+n_set+i],
7841 set->p[0]->div[i][0]);
7843 l = isl_basic_map_alloc_inequality(bmap);
7844 if (l < 0)
7845 goto error;
7846 isl_seq_neg(bmap->ineq[l], bmap->ineq[k], 1 + total);
7847 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0],
7848 set->p[0]->div[i][0]);
7849 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
7852 isl_set_free(set);
7853 bmap = isl_basic_map_simplify(bmap);
7854 bmap = isl_basic_map_finalize(bmap);
7855 return isl_map_from_basic_map(bmap);
7856 error:
7857 isl_set_free(set);
7858 isl_basic_map_free(bmap);
7859 return NULL;
7862 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
7864 unsigned dim;
7865 int size = 0;
7867 if (!bset)
7868 return -1;
7870 dim = isl_basic_set_total_dim(bset);
7871 size += bset->n_eq * (1 + dim);
7872 size += bset->n_ineq * (1 + dim);
7873 size += bset->n_div * (2 + dim);
7875 return size;
7878 int isl_set_size(__isl_keep isl_set *set)
7880 int i;
7881 int size = 0;
7883 if (!set)
7884 return -1;
7886 for (i = 0; i < set->n; ++i)
7887 size += isl_basic_set_size(set->p[i]);
7889 return size;
7892 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
7893 enum isl_dim_type type, unsigned pos)
7895 int i;
7896 int lower, upper;
7898 if (!bmap)
7899 return -1;
7901 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), return -1);
7903 pos += isl_basic_map_offset(bmap, type);
7905 for (i = 0; i < bmap->n_eq; ++i)
7906 if (!isl_int_is_zero(bmap->eq[i][pos]))
7907 return 1;
7909 lower = upper = 0;
7910 for (i = 0; i < bmap->n_ineq; ++i) {
7911 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
7912 if (sgn > 0)
7913 lower = 1;
7914 if (sgn < 0)
7915 upper = 1;
7918 return lower && upper;
7921 int isl_map_dim_is_bounded(__isl_keep isl_map *map,
7922 enum isl_dim_type type, unsigned pos)
7924 int i;
7926 if (!map)
7927 return -1;
7929 for (i = 0; i < map->n; ++i) {
7930 int bounded;
7931 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
7932 if (bounded < 0 || !bounded)
7933 return bounded;
7936 return 1;
7939 /* Return 1 if the specified dim is involved in both an upper bound
7940 * and a lower bound.
7942 int isl_set_dim_is_bounded(__isl_keep isl_set *set,
7943 enum isl_dim_type type, unsigned pos)
7945 return isl_map_dim_is_bounded((isl_map *)set, type, pos);
7948 /* For each of the "n" variables starting at "first", determine
7949 * the sign of the variable and put the results in the first "n"
7950 * elements of the array "signs".
7951 * Sign
7952 * 1 means that the variable is non-negative
7953 * -1 means that the variable is non-positive
7954 * 0 means the variable attains both positive and negative values.
7956 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
7957 unsigned first, unsigned n, int *signs)
7959 isl_vec *bound = NULL;
7960 struct isl_tab *tab = NULL;
7961 struct isl_tab_undo *snap;
7962 int i;
7964 if (!bset || !signs)
7965 return -1;
7967 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
7968 tab = isl_tab_from_basic_set(bset);
7969 if (!bound || !tab)
7970 goto error;
7972 isl_seq_clr(bound->el, bound->size);
7973 isl_int_set_si(bound->el[0], -1);
7975 snap = isl_tab_snap(tab);
7976 for (i = 0; i < n; ++i) {
7977 int empty;
7979 isl_int_set_si(bound->el[1 + first + i], -1);
7980 if (isl_tab_add_ineq(tab, bound->el) < 0)
7981 goto error;
7982 empty = tab->empty;
7983 isl_int_set_si(bound->el[1 + first + i], 0);
7984 if (isl_tab_rollback(tab, snap) < 0)
7985 goto error;
7987 if (empty) {
7988 signs[i] = 1;
7989 continue;
7992 isl_int_set_si(bound->el[1 + first + i], 1);
7993 if (isl_tab_add_ineq(tab, bound->el) < 0)
7994 goto error;
7995 empty = tab->empty;
7996 isl_int_set_si(bound->el[1 + first + i], 0);
7997 if (isl_tab_rollback(tab, snap) < 0)
7998 goto error;
8000 signs[i] = empty ? -1 : 0;
8003 isl_tab_free(tab);
8004 isl_vec_free(bound);
8005 return 0;
8006 error:
8007 isl_tab_free(tab);
8008 isl_vec_free(bound);
8009 return -1;
8012 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
8013 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
8015 if (!bset || !signs)
8016 return -1;
8017 isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
8018 return -1);
8020 first += pos(bset->dim, type) - 1;
8021 return isl_basic_set_vars_get_sign(bset, first, n, signs);
8024 /* Check if the given basic map is obviously single-valued.
8025 * In particular, for each output dimension, check that there is
8026 * an equality that defines the output dimension in terms of
8027 * earlier dimensions.
8029 int isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
8031 int i, j;
8032 unsigned total;
8033 unsigned n_out;
8034 unsigned o_out;
8036 if (!bmap)
8037 return -1;
8039 total = 1 + isl_basic_map_total_dim(bmap);
8040 n_out = isl_basic_map_dim(bmap, isl_dim_out);
8041 o_out = isl_basic_map_offset(bmap, isl_dim_out);
8043 for (i = 0; i < n_out; ++i) {
8044 for (j = 0; j < bmap->n_eq; ++j) {
8045 if (isl_int_is_zero(bmap->eq[j][o_out + i]))
8046 continue;
8047 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + i + 1,
8048 total - (o_out + i + 1)) == -1)
8049 break;
8051 if (j >= bmap->n_eq)
8052 return 0;
8055 return 1;
8058 /* Check if the given map is obviously single-valued.
8060 int isl_map_plain_is_single_valued(__isl_keep isl_map *map)
8062 if (!map)
8063 return -1;
8064 if (map->n == 0)
8065 return 1;
8066 if (map->n >= 2)
8067 return 0;
8069 return isl_basic_map_plain_is_single_valued(map->p[0]);
8072 /* Check if the given map is single-valued.
8073 * We simply compute
8075 * M \circ M^-1
8077 * and check if the result is a subset of the identity mapping.
8079 int isl_map_is_single_valued(__isl_keep isl_map *map)
8081 isl_dim *dim;
8082 isl_map *test;
8083 isl_map *id;
8084 int sv;
8086 sv = isl_map_plain_is_single_valued(map);
8087 if (sv < 0 || sv)
8088 return sv;
8090 test = isl_map_reverse(isl_map_copy(map));
8091 test = isl_map_apply_range(test, isl_map_copy(map));
8093 dim = isl_dim_map_from_set(isl_dim_range(isl_map_get_dim(map)));
8094 id = isl_map_identity(dim);
8096 sv = isl_map_is_subset(test, id);
8098 isl_map_free(test);
8099 isl_map_free(id);
8101 return sv;
8104 int isl_map_is_injective(__isl_keep isl_map *map)
8106 int in;
8108 map = isl_map_copy(map);
8109 map = isl_map_reverse(map);
8110 in = isl_map_is_single_valued(map);
8111 isl_map_free(map);
8113 return in;
8116 /* Check if the given map is obviously injective.
8118 int isl_map_plain_is_injective(__isl_keep isl_map *map)
8120 int in;
8122 map = isl_map_copy(map);
8123 map = isl_map_reverse(map);
8124 in = isl_map_plain_is_single_valued(map);
8125 isl_map_free(map);
8127 return in;
8130 int isl_map_is_bijective(__isl_keep isl_map *map)
8132 int sv;
8134 sv = isl_map_is_single_valued(map);
8135 if (sv < 0 || !sv)
8136 return sv;
8138 return isl_map_is_injective(map);
8141 int isl_set_is_singleton(__isl_keep isl_set *set)
8143 return isl_map_is_single_valued((isl_map *)set);
8146 int isl_map_is_translation(__isl_keep isl_map *map)
8148 int ok;
8149 isl_set *delta;
8151 delta = isl_map_deltas(isl_map_copy(map));
8152 ok = isl_set_is_singleton(delta);
8153 isl_set_free(delta);
8155 return ok;
8158 static int unique(isl_int *p, unsigned pos, unsigned len)
8160 if (isl_seq_first_non_zero(p, pos) != -1)
8161 return 0;
8162 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
8163 return 0;
8164 return 1;
8167 int isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
8169 int i, j;
8170 unsigned nvar;
8171 unsigned ovar;
8173 if (!bset)
8174 return -1;
8176 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
8177 return 0;
8179 nvar = isl_basic_set_dim(bset, isl_dim_set);
8180 ovar = isl_dim_offset(bset->dim, isl_dim_set);
8181 for (j = 0; j < nvar; ++j) {
8182 int lower = 0, upper = 0;
8183 for (i = 0; i < bset->n_eq; ++i) {
8184 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
8185 continue;
8186 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
8187 return 0;
8188 break;
8190 if (i < bset->n_eq)
8191 continue;
8192 for (i = 0; i < bset->n_ineq; ++i) {
8193 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
8194 continue;
8195 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
8196 return 0;
8197 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
8198 lower = 1;
8199 else
8200 upper = 1;
8202 if (!lower || !upper)
8203 return 0;
8206 return 1;
8209 int isl_set_is_box(__isl_keep isl_set *set)
8211 if (!set)
8212 return -1;
8213 if (set->n != 1)
8214 return 0;
8216 return isl_basic_set_is_box(set->p[0]);
8219 int isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
8221 if (!bset)
8222 return -1;
8224 return isl_dim_is_wrapping(bset->dim);
8227 int isl_set_is_wrapping(__isl_keep isl_set *set)
8229 if (!set)
8230 return -1;
8232 return isl_dim_is_wrapping(set->dim);
8235 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
8237 bmap = isl_basic_map_cow(bmap);
8238 if (!bmap)
8239 return NULL;
8241 bmap->dim = isl_dim_wrap(bmap->dim);
8242 if (!bmap->dim)
8243 goto error;
8245 bmap = isl_basic_map_finalize(bmap);
8247 return (isl_basic_set *)bmap;
8248 error:
8249 isl_basic_map_free(bmap);
8250 return NULL;
8253 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
8255 int i;
8257 map = isl_map_cow(map);
8258 if (!map)
8259 return NULL;
8261 for (i = 0; i < map->n; ++i) {
8262 map->p[i] = (isl_basic_map *)isl_basic_map_wrap(map->p[i]);
8263 if (!map->p[i])
8264 goto error;
8266 map->dim = isl_dim_wrap(map->dim);
8267 if (!map->dim)
8268 goto error;
8270 return (isl_set *)map;
8271 error:
8272 isl_map_free(map);
8273 return NULL;
8276 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
8278 bset = isl_basic_set_cow(bset);
8279 if (!bset)
8280 return NULL;
8282 bset->dim = isl_dim_unwrap(bset->dim);
8283 if (!bset->dim)
8284 goto error;
8286 bset = isl_basic_set_finalize(bset);
8288 return (isl_basic_map *)bset;
8289 error:
8290 isl_basic_set_free(bset);
8291 return NULL;
8294 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
8296 int i;
8298 if (!set)
8299 return NULL;
8301 if (!isl_set_is_wrapping(set))
8302 isl_die(set->ctx, isl_error_invalid, "not a wrapping set",
8303 goto error);
8305 set = isl_set_cow(set);
8306 if (!set)
8307 return NULL;
8309 for (i = 0; i < set->n; ++i) {
8310 set->p[i] = (isl_basic_set *)isl_basic_set_unwrap(set->p[i]);
8311 if (!set->p[i])
8312 goto error;
8315 set->dim = isl_dim_unwrap(set->dim);
8316 if (!set->dim)
8317 goto error;
8319 return (isl_map *)set;
8320 error:
8321 isl_set_free(set);
8322 return NULL;
8325 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
8326 enum isl_dim_type type)
8328 if (!bmap)
8329 return NULL;
8331 if (!isl_dim_is_named_or_nested(bmap->dim, type))
8332 return bmap;
8334 bmap = isl_basic_map_cow(bmap);
8335 if (!bmap)
8336 return NULL;
8338 bmap->dim = isl_dim_reset(bmap->dim, type);
8339 if (!bmap->dim)
8340 goto error;
8342 bmap = isl_basic_map_finalize(bmap);
8344 return bmap;
8345 error:
8346 isl_basic_map_free(bmap);
8347 return NULL;
8350 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
8351 enum isl_dim_type type)
8353 int i;
8355 if (!map)
8356 return NULL;
8358 if (!isl_dim_is_named_or_nested(map->dim, type))
8359 return map;
8361 map = isl_map_cow(map);
8362 if (!map)
8363 return NULL;
8365 for (i = 0; i < map->n; ++i) {
8366 map->p[i] = isl_basic_map_reset(map->p[i], type);
8367 if (!map->p[i])
8368 goto error;
8370 map->dim = isl_dim_reset(map->dim, type);
8371 if (!map->dim)
8372 goto error;
8374 return map;
8375 error:
8376 isl_map_free(map);
8377 return NULL;
8380 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
8382 if (!bmap)
8383 return NULL;
8385 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
8386 return bmap;
8388 bmap = isl_basic_map_cow(bmap);
8389 if (!bmap)
8390 return NULL;
8392 bmap->dim = isl_dim_flatten(bmap->dim);
8393 if (!bmap->dim)
8394 goto error;
8396 bmap = isl_basic_map_finalize(bmap);
8398 return bmap;
8399 error:
8400 isl_basic_map_free(bmap);
8401 return NULL;
8404 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
8406 return (isl_basic_set *)isl_basic_map_flatten((isl_basic_map *)bset);
8409 __isl_give isl_basic_map *isl_basic_map_flatten_range(
8410 __isl_take isl_basic_map *bmap)
8412 if (!bmap)
8413 return NULL;
8415 if (!bmap->dim->nested[1])
8416 return bmap;
8418 bmap = isl_basic_map_cow(bmap);
8419 if (!bmap)
8420 return NULL;
8422 bmap->dim = isl_dim_flatten_range(bmap->dim);
8423 if (!bmap->dim)
8424 goto error;
8426 bmap = isl_basic_map_finalize(bmap);
8428 return bmap;
8429 error:
8430 isl_basic_map_free(bmap);
8431 return NULL;
8434 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
8436 int i;
8438 if (!map)
8439 return NULL;
8441 if (!map->dim->nested[0] && !map->dim->nested[1])
8442 return map;
8444 map = isl_map_cow(map);
8445 if (!map)
8446 return NULL;
8448 for (i = 0; i < map->n; ++i) {
8449 map->p[i] = isl_basic_map_flatten(map->p[i]);
8450 if (!map->p[i])
8451 goto error;
8453 map->dim = isl_dim_flatten(map->dim);
8454 if (!map->dim)
8455 goto error;
8457 return map;
8458 error:
8459 isl_map_free(map);
8460 return NULL;
8463 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
8465 return (isl_set *)isl_map_flatten((isl_map *)set);
8468 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
8470 isl_dim *dim, *flat_dim;
8471 isl_map *map;
8473 dim = isl_set_get_dim(set);
8474 flat_dim = isl_dim_flatten(isl_dim_copy(dim));
8475 map = isl_map_identity(isl_dim_join(isl_dim_reverse(dim), flat_dim));
8476 map = isl_map_intersect_domain(map, set);
8478 return map;
8481 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
8483 int i;
8485 if (!map)
8486 return NULL;
8488 if (!map->dim->nested[1])
8489 return map;
8491 map = isl_map_cow(map);
8492 if (!map)
8493 return NULL;
8495 for (i = 0; i < map->n; ++i) {
8496 map->p[i] = isl_basic_map_flatten_range(map->p[i]);
8497 if (!map->p[i])
8498 goto error;
8500 map->dim = isl_dim_flatten_range(map->dim);
8501 if (!map->dim)
8502 goto error;
8504 return map;
8505 error:
8506 isl_map_free(map);
8507 return NULL;
8510 /* Reorder the dimensions of "bmap" according to the given dim_map
8511 * and set the dimension specification to "dim".
8513 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
8514 __isl_take isl_dim *dim, __isl_take struct isl_dim_map *dim_map)
8516 isl_basic_map *res;
8518 bmap = isl_basic_map_cow(bmap);
8519 if (!bmap || !dim || !dim_map)
8520 goto error;
8522 res = isl_basic_map_alloc_dim(dim,
8523 bmap->n_div, bmap->n_eq, bmap->n_ineq);
8524 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
8525 res = isl_basic_map_finalize(res);
8526 return res;
8527 error:
8528 free(dim_map);
8529 isl_basic_map_free(bmap);
8530 isl_dim_free(dim);
8531 return NULL;
8534 /* Reorder the dimensions of "map" according to given reordering.
8536 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
8537 __isl_take isl_reordering *r)
8539 int i;
8540 struct isl_dim_map *dim_map;
8542 map = isl_map_cow(map);
8543 dim_map = isl_dim_map_from_reordering(r);
8544 if (!map || !r || !dim_map)
8545 goto error;
8547 for (i = 0; i < map->n; ++i) {
8548 struct isl_dim_map *dim_map_i;
8550 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
8552 map->p[i] = isl_basic_map_realign(map->p[i],
8553 isl_dim_copy(r->dim), dim_map_i);
8555 if (!map->p[i])
8556 goto error;
8559 map = isl_map_reset_dim(map, isl_dim_copy(r->dim));
8561 isl_reordering_free(r);
8562 free(dim_map);
8563 return map;
8564 error:
8565 free(dim_map);
8566 isl_map_free(map);
8567 isl_reordering_free(r);
8568 return NULL;
8571 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
8572 __isl_take isl_reordering *r)
8574 return (isl_set *)isl_map_realign((isl_map *)set, r);
8577 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
8578 __isl_take isl_dim *model)
8580 isl_ctx *ctx;
8582 if (!map || !model)
8583 goto error;
8585 ctx = isl_dim_get_ctx(model);
8586 if (!isl_dim_has_named_params(model))
8587 isl_die(ctx, isl_error_invalid,
8588 "model has unnamed parameters", goto error);
8589 if (!isl_dim_has_named_params(map->dim))
8590 isl_die(ctx, isl_error_invalid,
8591 "relation has unnamed parameters", goto error);
8592 if (!isl_dim_match(map->dim, isl_dim_param, model, isl_dim_param)) {
8593 isl_reordering *exp;
8595 model = isl_dim_drop(model, isl_dim_in,
8596 0, isl_dim_size(model, isl_dim_in));
8597 model = isl_dim_drop(model, isl_dim_out,
8598 0, isl_dim_size(model, isl_dim_out));
8599 exp = isl_parameter_alignment_reordering(map->dim, model);
8600 exp = isl_reordering_extend_dim(exp, isl_map_get_dim(map));
8601 map = isl_map_realign(map, exp);
8604 isl_dim_free(model);
8605 return map;
8606 error:
8607 isl_dim_free(model);
8608 isl_map_free(map);
8609 return NULL;
8612 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
8613 __isl_take isl_dim *model)
8615 return isl_map_align_params(set, model);
8618 __isl_give isl_mat *isl_basic_map_equalities_matrix(
8619 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
8620 enum isl_dim_type c2, enum isl_dim_type c3,
8621 enum isl_dim_type c4, enum isl_dim_type c5)
8623 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
8624 struct isl_mat *mat;
8625 int i, j, k;
8626 int pos;
8628 if (!bmap)
8629 return NULL;
8630 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
8631 isl_basic_map_total_dim(bmap) + 1);
8632 if (!mat)
8633 return NULL;
8634 for (i = 0; i < bmap->n_eq; ++i)
8635 for (j = 0, pos = 0; j < 5; ++j) {
8636 int off = isl_basic_map_offset(bmap, c[j]);
8637 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
8638 isl_int_set(mat->row[i][pos],
8639 bmap->eq[i][off + k]);
8640 ++pos;
8644 return mat;
8647 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
8648 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
8649 enum isl_dim_type c2, enum isl_dim_type c3,
8650 enum isl_dim_type c4, enum isl_dim_type c5)
8652 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
8653 struct isl_mat *mat;
8654 int i, j, k;
8655 int pos;
8657 if (!bmap)
8658 return NULL;
8659 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
8660 isl_basic_map_total_dim(bmap) + 1);
8661 if (!mat)
8662 return NULL;
8663 for (i = 0; i < bmap->n_ineq; ++i)
8664 for (j = 0, pos = 0; j < 5; ++j) {
8665 int off = isl_basic_map_offset(bmap, c[j]);
8666 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
8667 isl_int_set(mat->row[i][pos],
8668 bmap->ineq[i][off + k]);
8669 ++pos;
8673 return mat;
8676 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
8677 __isl_take isl_dim *dim,
8678 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
8679 enum isl_dim_type c2, enum isl_dim_type c3,
8680 enum isl_dim_type c4, enum isl_dim_type c5)
8682 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
8683 isl_basic_map *bmap;
8684 unsigned total;
8685 unsigned extra;
8686 int i, j, k, l;
8687 int pos;
8689 if (!dim || !eq || !ineq)
8690 goto error;
8692 if (eq->n_col != ineq->n_col)
8693 isl_die(dim->ctx, isl_error_invalid,
8694 "equalities and inequalities matrices should have "
8695 "same number of columns", goto error);
8697 total = 1 + isl_dim_total(dim);
8699 if (eq->n_col < total)
8700 isl_die(dim->ctx, isl_error_invalid,
8701 "number of columns too small", goto error);
8703 extra = eq->n_col - total;
8705 bmap = isl_basic_map_alloc_dim(isl_dim_copy(dim), extra,
8706 eq->n_row, ineq->n_row);
8707 if (!bmap)
8708 goto error;
8709 for (i = 0; i < extra; ++i) {
8710 k = isl_basic_map_alloc_div(bmap);
8711 if (k < 0)
8712 goto error;
8713 isl_int_set_si(bmap->div[k][0], 0);
8715 for (i = 0; i < eq->n_row; ++i) {
8716 l = isl_basic_map_alloc_equality(bmap);
8717 if (l < 0)
8718 goto error;
8719 for (j = 0, pos = 0; j < 5; ++j) {
8720 int off = isl_basic_map_offset(bmap, c[j]);
8721 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
8722 isl_int_set(bmap->eq[l][off + k],
8723 eq->row[i][pos]);
8724 ++pos;
8728 for (i = 0; i < ineq->n_row; ++i) {
8729 l = isl_basic_map_alloc_inequality(bmap);
8730 if (l < 0)
8731 goto error;
8732 for (j = 0, pos = 0; j < 5; ++j) {
8733 int off = isl_basic_map_offset(bmap, c[j]);
8734 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
8735 isl_int_set(bmap->ineq[l][off + k],
8736 ineq->row[i][pos]);
8737 ++pos;
8742 isl_dim_free(dim);
8743 isl_mat_free(eq);
8744 isl_mat_free(ineq);
8746 return bmap;
8747 error:
8748 isl_dim_free(dim);
8749 isl_mat_free(eq);
8750 isl_mat_free(ineq);
8751 return NULL;
8754 __isl_give isl_mat *isl_basic_set_equalities_matrix(
8755 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
8756 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
8758 return isl_basic_map_equalities_matrix((isl_basic_map *)bset,
8759 c1, c2, c3, c4, isl_dim_in);
8762 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
8763 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
8764 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
8766 return isl_basic_map_inequalities_matrix((isl_basic_map *)bset,
8767 c1, c2, c3, c4, isl_dim_in);
8770 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
8771 __isl_take isl_dim *dim,
8772 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
8773 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
8775 return (isl_basic_set*)
8776 isl_basic_map_from_constraint_matrices(dim, eq, ineq,
8777 c1, c2, c3, c4, isl_dim_in);
8780 int isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
8782 if (!bmap)
8783 return -1;
8785 return isl_dim_can_zip(bmap->dim);
8788 int isl_map_can_zip(__isl_keep isl_map *map)
8790 if (!map)
8791 return -1;
8793 return isl_dim_can_zip(map->dim);
8796 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
8797 * (A -> C) -> (B -> D).
8799 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
8801 unsigned pos;
8802 unsigned n1;
8803 unsigned n2;
8805 if (!bmap)
8806 return NULL;
8808 if (!isl_basic_map_can_zip(bmap))
8809 isl_die(bmap->ctx, isl_error_invalid,
8810 "basic map cannot be zipped", goto error);
8811 pos = isl_basic_map_offset(bmap, isl_dim_in) +
8812 isl_dim_size(bmap->dim->nested[0], isl_dim_in);
8813 n1 = isl_dim_size(bmap->dim->nested[0], isl_dim_out);
8814 n2 = isl_dim_size(bmap->dim->nested[1], isl_dim_in);
8815 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
8816 if (!bmap)
8817 return NULL;
8818 bmap->dim = isl_dim_zip(bmap->dim);
8819 if (!bmap->dim)
8820 goto error;
8821 return bmap;
8822 error:
8823 isl_basic_map_free(bmap);
8824 return NULL;
8827 /* Given a map (A -> B) -> (C -> D), return the corresponding map
8828 * (A -> C) -> (B -> D).
8830 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
8832 int i;
8834 if (!map)
8835 return NULL;
8837 if (!isl_map_can_zip(map))
8838 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
8839 goto error);
8841 map = isl_map_cow(map);
8842 if (!map)
8843 return NULL;
8845 for (i = 0; i < map->n; ++i) {
8846 map->p[i] = isl_basic_map_zip(map->p[i]);
8847 if (!map->p[i])
8848 goto error;
8851 map->dim = isl_dim_zip(map->dim);
8852 if (!map->dim)
8853 goto error;
8855 return map;
8856 error:
8857 isl_map_free(map);
8858 return NULL;
8861 /* Construct a basic map mapping the domain of the affine expression
8862 * to a one-dimensional range prescribed by the affine expression.
8864 __isl_give isl_basic_map *isl_basic_map_from_aff(__isl_take isl_aff *aff)
8866 int k;
8867 int pos;
8868 isl_local_space *ls;
8869 isl_basic_map *bmap;
8871 if (!aff)
8872 return NULL;
8874 ls = isl_aff_get_local_space(aff);
8875 ls = isl_local_space_from_domain(ls);
8876 ls = isl_local_space_add_dim(ls, isl_dim_out, 1);
8877 bmap = isl_basic_map_from_local_space(ls);
8878 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
8879 k = isl_basic_map_alloc_equality(bmap);
8880 if (k < 0)
8881 goto error;
8883 pos = isl_basic_map_offset(bmap, isl_dim_out);
8884 isl_seq_cpy(bmap->eq[k], aff->v->el + 1, pos);
8885 isl_int_neg(bmap->eq[k][pos], aff->v->el[0]);
8886 isl_seq_cpy(bmap->eq[k] + pos + 1, aff->v->el + 1 + pos,
8887 aff->v->size - (pos + 1));
8889 isl_aff_free(aff);
8890 bmap = isl_basic_map_finalize(bmap);
8891 return bmap;
8892 error:
8893 isl_aff_free(aff);
8894 isl_basic_map_free(bmap);
8895 return NULL;
8898 /* Construct a basic map mapping a domain in the given space to
8899 * to an n-dimensional range, with n the number of elements in the list,
8900 * where each coordinate in the range is prescribed by the
8901 * corresponding affine expression.
8902 * The domains of all affine expressions in the list are assumed to match
8903 * domain_dim.
8905 __isl_give isl_basic_map *isl_basic_map_from_aff_list(
8906 __isl_take isl_dim *domain_dim, __isl_take isl_aff_list *list)
8908 int i;
8909 isl_dim *dim;
8910 isl_basic_map *bmap;
8912 if (!list)
8913 return NULL;
8915 dim = isl_dim_from_domain(domain_dim);
8916 bmap = isl_basic_map_universe(dim);
8918 for (i = 0; i < list->n; ++i) {
8919 isl_aff *aff;
8920 isl_basic_map *bmap_i;
8922 aff = isl_aff_copy(list->p[i]);
8923 bmap_i = isl_basic_map_from_aff(aff);
8925 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
8928 isl_aff_list_free(list);
8929 return bmap;