isl_stream: accept "$" token
[isl.git] / isl_map.c
blob03e29cc6e9a797957303caa28639557f3f3ac0fe
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
5 * Use of this software is governed by the GNU LGPLv2.1 license
7 * Written by Sven Verdoolaege, K.U.Leuven, Departement
8 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
9 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
10 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
13 #include <string.h>
14 #include <strings.h>
15 #include <isl_map_private.h>
16 #include <isl/ctx.h>
17 #include <isl/blk.h>
18 #include "isl_dim_private.h"
19 #include "isl_equalities.h"
20 #include <isl/list.h>
21 #include <isl/lp.h>
22 #include <isl/seq.h>
23 #include <isl/set.h>
24 #include <isl/map.h>
25 #include "isl_map_piplib.h"
26 #include <isl_reordering.h>
27 #include "isl_sample.h"
28 #include "isl_tab.h"
29 #include <isl/vec.h>
30 #include <isl_mat_private.h>
32 /* Maps dst positions to src positions */
33 struct isl_dim_map {
34 unsigned len;
35 int pos[1];
38 static struct isl_dim_map *isl_dim_map_alloc(struct isl_ctx *ctx, unsigned len)
40 int i;
41 struct isl_dim_map *dim_map;
42 dim_map = isl_alloc(ctx, struct isl_dim_map,
43 sizeof(struct isl_dim_map) + len * sizeof(int));
44 if (!dim_map)
45 return NULL;
46 dim_map->len = 1 + len;
47 dim_map->pos[0] = 0;
48 for (i = 0; i < len; ++i)
49 dim_map->pos[1 + i] = -1;
50 return dim_map;
53 static unsigned n(struct isl_dim *dim, enum isl_dim_type type)
55 switch (type) {
56 case isl_dim_param: return dim->nparam;
57 case isl_dim_in: return dim->n_in;
58 case isl_dim_out: return dim->n_out;
59 case isl_dim_all: return dim->nparam + dim->n_in + dim->n_out;
60 default: return 0;
64 static unsigned pos(struct isl_dim *dim, enum isl_dim_type type)
66 switch (type) {
67 case isl_dim_param: return 1;
68 case isl_dim_in: return 1 + dim->nparam;
69 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
70 default: return 0;
74 static void isl_dim_map_dim_range(struct isl_dim_map *dim_map,
75 struct isl_dim *dim, enum isl_dim_type type,
76 unsigned first, unsigned n, unsigned dst_pos)
78 int i;
79 unsigned src_pos;
81 if (!dim_map || !dim)
82 return;
84 src_pos = pos(dim, type);
85 for (i = 0; i < n; ++i)
86 dim_map->pos[1 + dst_pos + i] = src_pos + first + i;
89 static void isl_dim_map_dim(struct isl_dim_map *dim_map, struct isl_dim *dim,
90 enum isl_dim_type type, unsigned dst_pos)
92 isl_dim_map_dim_range(dim_map, dim, type, 0, n(dim, type), dst_pos);
95 static void isl_dim_map_div(struct isl_dim_map *dim_map,
96 struct isl_basic_map *bmap, unsigned dst_pos)
98 int i;
99 unsigned src_pos;
101 if (!dim_map || !bmap)
102 return;
104 src_pos = 1 + isl_dim_total(bmap->dim);
105 for (i = 0; i < bmap->n_div; ++i)
106 dim_map->pos[1 + dst_pos + i] = src_pos + i;
109 static void isl_dim_map_dump(struct isl_dim_map *dim_map)
111 int i;
113 for (i = 0; i < dim_map->len; ++i)
114 fprintf(stderr, "%d -> %d; ", i, dim_map->pos[i]);
115 fprintf(stderr, "\n");
118 unsigned isl_basic_map_dim(const struct isl_basic_map *bmap,
119 enum isl_dim_type type)
121 if (!bmap)
122 return 0;
123 switch (type) {
124 case isl_dim_cst: return 1;
125 case isl_dim_param:
126 case isl_dim_in:
127 case isl_dim_out: return isl_dim_size(bmap->dim, type);
128 case isl_dim_div: return bmap->n_div;
129 case isl_dim_all: return isl_basic_map_total_dim(bmap);
130 default: return 0;
134 unsigned isl_map_dim(const struct isl_map *map, enum isl_dim_type type)
136 return map ? n(map->dim, type) : 0;
139 unsigned isl_set_dim(const struct isl_set *set, enum isl_dim_type type)
141 return set ? n(set->dim, type) : 0;
144 unsigned isl_basic_map_offset(struct isl_basic_map *bmap,
145 enum isl_dim_type type)
147 struct isl_dim *dim = bmap->dim;
148 switch (type) {
149 case isl_dim_cst: return 0;
150 case isl_dim_param: return 1;
151 case isl_dim_in: return 1 + dim->nparam;
152 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
153 case isl_dim_div: return 1 + dim->nparam + dim->n_in + dim->n_out;
154 default: return 0;
158 static unsigned map_offset(struct isl_map *map, enum isl_dim_type type)
160 return pos(map->dim, type);
163 unsigned isl_basic_set_dim(const struct isl_basic_set *bset,
164 enum isl_dim_type type)
166 return isl_basic_map_dim((const struct isl_basic_map*)bset, type);
169 unsigned isl_basic_set_n_dim(const struct isl_basic_set *bset)
171 return isl_basic_set_dim(bset, isl_dim_set);
174 unsigned isl_basic_set_n_param(const struct isl_basic_set *bset)
176 return isl_basic_set_dim(bset, isl_dim_param);
179 unsigned isl_basic_set_total_dim(const struct isl_basic_set *bset)
181 return isl_dim_total(bset->dim) + bset->n_div;
184 unsigned isl_set_n_dim(const struct isl_set *set)
186 return isl_set_dim(set, isl_dim_set);
189 unsigned isl_set_n_param(const struct isl_set *set)
191 return isl_set_dim(set, isl_dim_param);
194 unsigned isl_basic_map_n_in(const struct isl_basic_map *bmap)
196 return bmap ? bmap->dim->n_in : 0;
199 unsigned isl_basic_map_n_out(const struct isl_basic_map *bmap)
201 return bmap ? bmap->dim->n_out : 0;
204 unsigned isl_basic_map_n_param(const struct isl_basic_map *bmap)
206 return bmap ? bmap->dim->nparam : 0;
209 unsigned isl_basic_map_n_div(const struct isl_basic_map *bmap)
211 return bmap ? bmap->n_div : 0;
214 unsigned isl_basic_map_total_dim(const struct isl_basic_map *bmap)
216 return bmap ? isl_dim_total(bmap->dim) + bmap->n_div : 0;
219 unsigned isl_map_n_in(const struct isl_map *map)
221 return map->dim->n_in;
224 unsigned isl_map_n_out(const struct isl_map *map)
226 return map->dim->n_out;
229 unsigned isl_map_n_param(const struct isl_map *map)
231 return map->dim->nparam;
234 int isl_map_compatible_domain(struct isl_map *map, struct isl_set *set)
236 int m;
237 if (!map || !set)
238 return -1;
239 m = isl_dim_match(map->dim, isl_dim_param, set->dim, isl_dim_param);
240 if (m < 0 || !m)
241 return m;
242 return isl_dim_tuple_match(map->dim, isl_dim_in, set->dim, isl_dim_set);
245 int isl_basic_map_compatible_domain(struct isl_basic_map *bmap,
246 struct isl_basic_set *bset)
248 int m;
249 if (!bmap || !bset)
250 return -1;
251 m = isl_dim_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
252 if (m < 0 || !m)
253 return m;
254 return isl_dim_tuple_match(bmap->dim, isl_dim_in, bset->dim, isl_dim_set);
257 int isl_basic_map_compatible_range(struct isl_basic_map *bmap,
258 struct isl_basic_set *bset)
260 int m;
261 if (!bmap || !bset)
262 return -1;
263 m = isl_dim_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
264 if (m < 0 || !m)
265 return m;
266 return isl_dim_tuple_match(bmap->dim, isl_dim_out, bset->dim, isl_dim_set);
269 isl_ctx *isl_basic_map_get_ctx(__isl_keep isl_basic_map *bmap)
271 return bmap ? bmap->ctx : NULL;
274 isl_ctx *isl_basic_set_get_ctx(__isl_keep isl_basic_set *bset)
276 return bset ? bset->ctx : NULL;
279 isl_ctx *isl_map_get_ctx(__isl_keep isl_map *map)
281 return map ? map->ctx : NULL;
284 isl_ctx *isl_set_get_ctx(__isl_keep isl_set *set)
286 return set ? set->ctx : NULL;
289 struct isl_dim *isl_basic_map_get_dim(struct isl_basic_map *bmap)
291 if (!bmap)
292 return NULL;
293 return isl_dim_copy(bmap->dim);
296 struct isl_dim *isl_basic_set_get_dim(struct isl_basic_set *bset)
298 if (!bset)
299 return NULL;
300 return isl_dim_copy(bset->dim);
303 struct isl_dim *isl_map_get_dim(struct isl_map *map)
305 if (!map)
306 return NULL;
307 return isl_dim_copy(map->dim);
310 struct isl_dim *isl_set_get_dim(struct isl_set *set)
312 if (!set)
313 return NULL;
314 return isl_dim_copy(set->dim);
317 __isl_give isl_basic_map *isl_basic_map_set_tuple_name(
318 __isl_take isl_basic_map *bmap, enum isl_dim_type type, const char *s)
320 bmap = isl_basic_map_cow(bmap);
321 if (!bmap)
322 return NULL;
323 bmap->dim = isl_dim_set_tuple_name(bmap->dim, type, s);
324 if (!bmap->dim)
325 goto error;
326 bmap = isl_basic_map_finalize(bmap);
327 return bmap;
328 error:
329 isl_basic_map_free(bmap);
330 return NULL;
333 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map *bmap,
334 enum isl_dim_type type)
336 return bmap ? isl_dim_get_tuple_name(bmap->dim, type) : NULL;
339 __isl_give isl_map *isl_map_set_tuple_name(__isl_take isl_map *map,
340 enum isl_dim_type type, const char *s)
342 int i;
344 map = isl_map_cow(map);
345 if (!map)
346 return NULL;
348 map->dim = isl_dim_set_tuple_name(map->dim, type, s);
349 if (!map->dim)
350 goto error;
352 for (i = 0; i < map->n; ++i) {
353 map->p[i] = isl_basic_map_set_tuple_name(map->p[i], type, s);
354 if (!map->p[i])
355 goto error;
358 return map;
359 error:
360 isl_map_free(map);
361 return NULL;
364 const char *isl_map_get_tuple_name(__isl_keep isl_map *map,
365 enum isl_dim_type type)
367 return map ? isl_dim_get_tuple_name(map->dim, type) : NULL;
370 __isl_give isl_set *isl_set_set_tuple_name(__isl_take isl_set *set,
371 const char *s)
373 return (isl_set *)isl_map_set_tuple_name((isl_map *)set, isl_dim_set, s);
376 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set *bset)
378 return bset ? isl_dim_get_tuple_name(bset->dim, isl_dim_set) : NULL;
381 const char *isl_set_get_tuple_name(__isl_keep isl_set *set)
383 return set ? isl_dim_get_tuple_name(set->dim, isl_dim_set) : NULL;
386 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map *bmap,
387 enum isl_dim_type type, unsigned pos)
389 return bmap ? isl_dim_get_name(bmap->dim, type, pos) : NULL;
392 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set *bset,
393 enum isl_dim_type type, unsigned pos)
395 return bset ? isl_dim_get_name(bset->dim, type, pos) : NULL;
398 const char *isl_map_get_dim_name(__isl_keep isl_map *map,
399 enum isl_dim_type type, unsigned pos)
401 return map ? isl_dim_get_name(map->dim, type, pos) : NULL;
404 const char *isl_set_get_dim_name(__isl_keep isl_set *set,
405 enum isl_dim_type type, unsigned pos)
407 return set ? isl_dim_get_name(set->dim, type, pos) : NULL;
410 __isl_give isl_basic_map *isl_basic_map_set_dim_name(
411 __isl_take isl_basic_map *bmap,
412 enum isl_dim_type type, unsigned pos, const char *s)
414 if (!bmap)
415 return NULL;
416 bmap->dim = isl_dim_set_name(bmap->dim, type, pos, s);
417 if (!bmap->dim)
418 goto error;
419 return bmap;
420 error:
421 isl_basic_map_free(bmap);
422 return NULL;
425 __isl_give isl_map *isl_map_set_dim_name(__isl_take isl_map *map,
426 enum isl_dim_type type, unsigned pos, const char *s)
428 int i;
430 if (!map)
431 return NULL;
433 map->dim = isl_dim_set_name(map->dim, type, pos, s);
434 if (!map->dim)
435 goto error;
437 for (i = 0; i < map->n; ++i) {
438 map->p[i] = isl_basic_map_set_dim_name(map->p[i], type, pos, s);
439 if (!map->p[i])
440 goto error;
443 return map;
444 error:
445 isl_map_free(map);
446 return NULL;
449 __isl_give isl_basic_set *isl_basic_set_set_dim_name(
450 __isl_take isl_basic_set *bset,
451 enum isl_dim_type type, unsigned pos, const char *s)
453 return (isl_basic_set *)isl_basic_map_set_dim_name(
454 (isl_basic_map *)bset, type, pos, s);
457 __isl_give isl_set *isl_set_set_dim_name(__isl_take isl_set *set,
458 enum isl_dim_type type, unsigned pos, const char *s)
460 return (isl_set *)isl_map_set_dim_name((isl_map *)set, type, pos, s);
463 int isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
465 if (!bmap)
466 return -1;
467 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
470 int isl_basic_set_is_rational(__isl_keep isl_basic_set *bset)
472 return isl_basic_map_is_rational(bset);
475 static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
476 struct isl_basic_map *bmap, unsigned extra,
477 unsigned n_eq, unsigned n_ineq)
479 int i;
480 size_t row_size = 1 + isl_dim_total(bmap->dim) + extra;
482 bmap->ctx = ctx;
483 isl_ctx_ref(ctx);
485 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
486 if (isl_blk_is_error(bmap->block))
487 goto error;
489 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
490 if (!bmap->ineq)
491 goto error;
493 if (extra == 0) {
494 bmap->block2 = isl_blk_empty();
495 bmap->div = NULL;
496 } else {
497 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
498 if (isl_blk_is_error(bmap->block2))
499 goto error;
501 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
502 if (!bmap->div)
503 goto error;
506 for (i = 0; i < n_ineq + n_eq; ++i)
507 bmap->ineq[i] = bmap->block.data + i * row_size;
509 for (i = 0; i < extra; ++i)
510 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
512 bmap->ref = 1;
513 bmap->flags = 0;
514 bmap->c_size = n_eq + n_ineq;
515 bmap->eq = bmap->ineq + n_ineq;
516 bmap->extra = extra;
517 bmap->n_eq = 0;
518 bmap->n_ineq = 0;
519 bmap->n_div = 0;
520 bmap->sample = NULL;
522 return bmap;
523 error:
524 isl_basic_map_free(bmap);
525 return NULL;
528 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
529 unsigned nparam, unsigned dim, unsigned extra,
530 unsigned n_eq, unsigned n_ineq)
532 struct isl_basic_map *bmap;
533 bmap = isl_basic_map_alloc(ctx, nparam, 0, dim, extra, n_eq, n_ineq);
534 return (struct isl_basic_set *)bmap;
537 struct isl_basic_set *isl_basic_set_alloc_dim(struct isl_dim *dim,
538 unsigned extra, unsigned n_eq, unsigned n_ineq)
540 struct isl_basic_map *bmap;
541 if (!dim)
542 return NULL;
543 isl_assert(dim->ctx, dim->n_in == 0, goto error);
544 bmap = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
545 return (struct isl_basic_set *)bmap;
546 error:
547 isl_dim_free(dim);
548 return NULL;
551 struct isl_basic_map *isl_basic_map_alloc_dim(struct isl_dim *dim,
552 unsigned extra, unsigned n_eq, unsigned n_ineq)
554 struct isl_basic_map *bmap;
556 if (!dim)
557 return NULL;
558 bmap = isl_calloc_type(dim->ctx, struct isl_basic_map);
559 if (!bmap)
560 goto error;
561 bmap->dim = dim;
563 return basic_map_init(dim->ctx, bmap, extra, n_eq, n_ineq);
564 error:
565 isl_dim_free(dim);
566 return NULL;
569 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
570 unsigned nparam, unsigned in, unsigned out, unsigned extra,
571 unsigned n_eq, unsigned n_ineq)
573 struct isl_basic_map *bmap;
574 struct isl_dim *dim;
576 dim = isl_dim_alloc(ctx, nparam, in, out);
577 if (!dim)
578 return NULL;
580 bmap = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
581 return bmap;
584 static void dup_constraints(
585 struct isl_basic_map *dst, struct isl_basic_map *src)
587 int i;
588 unsigned total = isl_basic_map_total_dim(src);
590 for (i = 0; i < src->n_eq; ++i) {
591 int j = isl_basic_map_alloc_equality(dst);
592 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
595 for (i = 0; i < src->n_ineq; ++i) {
596 int j = isl_basic_map_alloc_inequality(dst);
597 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
600 for (i = 0; i < src->n_div; ++i) {
601 int j = isl_basic_map_alloc_div(dst);
602 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
604 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
607 struct isl_basic_map *isl_basic_map_dup(struct isl_basic_map *bmap)
609 struct isl_basic_map *dup;
611 if (!bmap)
612 return NULL;
613 dup = isl_basic_map_alloc_dim(isl_dim_copy(bmap->dim),
614 bmap->n_div, bmap->n_eq, bmap->n_ineq);
615 if (!dup)
616 return NULL;
617 dup_constraints(dup, bmap);
618 dup->flags = bmap->flags;
619 dup->sample = isl_vec_copy(bmap->sample);
620 return dup;
623 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
625 struct isl_basic_map *dup;
627 dup = isl_basic_map_dup((struct isl_basic_map *)bset);
628 return (struct isl_basic_set *)dup;
631 struct isl_basic_set *isl_basic_set_copy(struct isl_basic_set *bset)
633 if (!bset)
634 return NULL;
636 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
637 bset->ref++;
638 return bset;
640 return isl_basic_set_dup(bset);
643 struct isl_set *isl_set_copy(struct isl_set *set)
645 if (!set)
646 return NULL;
648 set->ref++;
649 return set;
652 struct isl_basic_map *isl_basic_map_copy(struct isl_basic_map *bmap)
654 if (!bmap)
655 return NULL;
657 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
658 bmap->ref++;
659 return bmap;
661 bmap = isl_basic_map_dup(bmap);
662 if (bmap)
663 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
664 return bmap;
667 struct isl_map *isl_map_copy(struct isl_map *map)
669 if (!map)
670 return NULL;
672 map->ref++;
673 return map;
676 void isl_basic_map_free(struct isl_basic_map *bmap)
678 if (!bmap)
679 return;
681 if (--bmap->ref > 0)
682 return;
684 isl_ctx_deref(bmap->ctx);
685 free(bmap->div);
686 isl_blk_free(bmap->ctx, bmap->block2);
687 free(bmap->ineq);
688 isl_blk_free(bmap->ctx, bmap->block);
689 isl_vec_free(bmap->sample);
690 isl_dim_free(bmap->dim);
691 free(bmap);
694 void isl_basic_set_free(struct isl_basic_set *bset)
696 isl_basic_map_free((struct isl_basic_map *)bset);
699 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
701 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
704 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
706 struct isl_ctx *ctx;
707 if (!bmap)
708 return -1;
709 ctx = bmap->ctx;
710 isl_assert(ctx, room_for_con(bmap, 1), return -1);
711 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
712 return -1);
713 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
714 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
715 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
716 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
717 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
718 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
719 isl_int *t;
720 int j = isl_basic_map_alloc_inequality(bmap);
721 if (j < 0)
722 return -1;
723 t = bmap->ineq[j];
724 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
725 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
726 bmap->eq[-1] = t;
727 bmap->n_eq++;
728 bmap->n_ineq--;
729 bmap->eq--;
730 return 0;
732 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
733 bmap->extra - bmap->n_div);
734 return bmap->n_eq++;
737 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
739 return isl_basic_map_alloc_equality((struct isl_basic_map *)bset);
742 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
744 if (!bmap)
745 return -1;
746 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
747 bmap->n_eq -= n;
748 return 0;
751 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
753 return isl_basic_map_free_equality((struct isl_basic_map *)bset, n);
756 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
758 isl_int *t;
759 if (!bmap)
760 return -1;
761 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
763 if (pos != bmap->n_eq - 1) {
764 t = bmap->eq[pos];
765 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
766 bmap->eq[bmap->n_eq - 1] = t;
768 bmap->n_eq--;
769 return 0;
772 int isl_basic_set_drop_equality(struct isl_basic_set *bset, unsigned pos)
774 return isl_basic_map_drop_equality((struct isl_basic_map *)bset, pos);
777 void isl_basic_map_inequality_to_equality(
778 struct isl_basic_map *bmap, unsigned pos)
780 isl_int *t;
782 t = bmap->ineq[pos];
783 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
784 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
785 bmap->eq[-1] = t;
786 bmap->n_eq++;
787 bmap->n_ineq--;
788 bmap->eq--;
789 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
790 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
791 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
792 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
795 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
797 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
800 int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap)
802 struct isl_ctx *ctx;
803 if (!bmap)
804 return -1;
805 ctx = bmap->ctx;
806 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
807 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
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_ALL_EQUALITIES);
811 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
812 1 + isl_basic_map_total_dim(bmap),
813 bmap->extra - bmap->n_div);
814 return bmap->n_ineq++;
817 int isl_basic_set_alloc_inequality(struct isl_basic_set *bset)
819 return isl_basic_map_alloc_inequality((struct isl_basic_map *)bset);
822 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
824 if (!bmap)
825 return -1;
826 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
827 bmap->n_ineq -= n;
828 return 0;
831 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
833 return isl_basic_map_free_inequality((struct isl_basic_map *)bset, n);
836 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
838 isl_int *t;
839 if (!bmap)
840 return -1;
841 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
843 if (pos != bmap->n_ineq - 1) {
844 t = bmap->ineq[pos];
845 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
846 bmap->ineq[bmap->n_ineq - 1] = t;
847 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
849 bmap->n_ineq--;
850 return 0;
853 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
855 return isl_basic_map_drop_inequality((struct isl_basic_map *)bset, pos);
858 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
859 isl_int *eq)
861 int k;
863 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
864 if (!bmap)
865 return NULL;
866 k = isl_basic_map_alloc_equality(bmap);
867 if (k < 0)
868 goto error;
869 isl_seq_cpy(bmap->eq[k], eq, 1 + isl_basic_map_total_dim(bmap));
870 return bmap;
871 error:
872 isl_basic_map_free(bmap);
873 return NULL;
876 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
877 isl_int *eq)
879 return (isl_basic_set *)
880 isl_basic_map_add_eq((isl_basic_map *)bset, eq);
883 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
884 isl_int *ineq)
886 int k;
888 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
889 if (!bmap)
890 return NULL;
891 k = isl_basic_map_alloc_inequality(bmap);
892 if (k < 0)
893 goto error;
894 isl_seq_cpy(bmap->ineq[k], ineq, 1 + isl_basic_map_total_dim(bmap));
895 return bmap;
896 error:
897 isl_basic_map_free(bmap);
898 return NULL;
901 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
902 isl_int *ineq)
904 return (isl_basic_set *)
905 isl_basic_map_add_ineq((isl_basic_map *)bset, ineq);
908 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
910 if (!bmap)
911 return -1;
912 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
913 isl_seq_clr(bmap->div[bmap->n_div] +
914 1 + 1 + isl_basic_map_total_dim(bmap),
915 bmap->extra - bmap->n_div);
916 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
917 return bmap->n_div++;
920 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
922 return isl_basic_map_alloc_div((struct isl_basic_map *)bset);
925 int isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
927 if (!bmap)
928 return -1;
929 isl_assert(bmap->ctx, n <= bmap->n_div, return -1);
930 bmap->n_div -= n;
931 return 0;
934 int isl_basic_set_free_div(struct isl_basic_set *bset, unsigned n)
936 return isl_basic_map_free_div((struct isl_basic_map *)bset, n);
939 /* Copy constraint from src to dst, putting the vars of src at offset
940 * dim_off in dst and the divs of src at offset div_off in dst.
941 * If both sets are actually map, then dim_off applies to the input
942 * variables.
944 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
945 struct isl_basic_map *src_map, isl_int *src,
946 unsigned in_off, unsigned out_off, unsigned div_off)
948 unsigned src_nparam = isl_basic_map_n_param(src_map);
949 unsigned dst_nparam = isl_basic_map_n_param(dst_map);
950 unsigned src_in = isl_basic_map_n_in(src_map);
951 unsigned dst_in = isl_basic_map_n_in(dst_map);
952 unsigned src_out = isl_basic_map_n_out(src_map);
953 unsigned dst_out = isl_basic_map_n_out(dst_map);
954 isl_int_set(dst[0], src[0]);
955 isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
956 if (dst_nparam > src_nparam)
957 isl_seq_clr(dst+1+src_nparam,
958 dst_nparam - src_nparam);
959 isl_seq_clr(dst+1+dst_nparam, in_off);
960 isl_seq_cpy(dst+1+dst_nparam+in_off,
961 src+1+src_nparam,
962 isl_min(dst_in-in_off, src_in));
963 if (dst_in-in_off > src_in)
964 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
965 dst_in - in_off - src_in);
966 isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
967 isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
968 src+1+src_nparam+src_in,
969 isl_min(dst_out-out_off, src_out));
970 if (dst_out-out_off > src_out)
971 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
972 dst_out - out_off - src_out);
973 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
974 isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
975 src+1+src_nparam+src_in+src_out,
976 isl_min(dst_map->extra-div_off, src_map->n_div));
977 if (dst_map->n_div-div_off > src_map->n_div)
978 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
979 div_off+src_map->n_div,
980 dst_map->n_div - div_off - src_map->n_div);
983 static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
984 struct isl_basic_map *src_map, isl_int *src,
985 unsigned in_off, unsigned out_off, unsigned div_off)
987 isl_int_set(dst[0], src[0]);
988 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
991 static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
992 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
994 int i;
995 unsigned div_off;
997 if (!bmap1 || !bmap2)
998 goto error;
1000 div_off = bmap1->n_div;
1002 for (i = 0; i < bmap2->n_eq; ++i) {
1003 int i1 = isl_basic_map_alloc_equality(bmap1);
1004 if (i1 < 0)
1005 goto error;
1006 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
1007 i_pos, o_pos, div_off);
1010 for (i = 0; i < bmap2->n_ineq; ++i) {
1011 int i1 = isl_basic_map_alloc_inequality(bmap1);
1012 if (i1 < 0)
1013 goto error;
1014 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
1015 i_pos, o_pos, div_off);
1018 for (i = 0; i < bmap2->n_div; ++i) {
1019 int i1 = isl_basic_map_alloc_div(bmap1);
1020 if (i1 < 0)
1021 goto error;
1022 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
1023 i_pos, o_pos, div_off);
1026 isl_basic_map_free(bmap2);
1028 return bmap1;
1030 error:
1031 isl_basic_map_free(bmap1);
1032 isl_basic_map_free(bmap2);
1033 return NULL;
1036 static void copy_constraint_dim_map(isl_int *dst, isl_int *src,
1037 struct isl_dim_map *dim_map)
1039 int i;
1041 for (i = 0; i < dim_map->len; ++i) {
1042 if (dim_map->pos[i] < 0)
1043 isl_int_set_si(dst[i], 0);
1044 else
1045 isl_int_set(dst[i], src[dim_map->pos[i]]);
1049 static void copy_div_dim_map(isl_int *dst, isl_int *src,
1050 struct isl_dim_map *dim_map)
1052 isl_int_set(dst[0], src[0]);
1053 copy_constraint_dim_map(dst+1, src+1, dim_map);
1056 static struct isl_basic_map *add_constraints_dim_map(struct isl_basic_map *dst,
1057 struct isl_basic_map *src, struct isl_dim_map *dim_map)
1059 int i;
1061 if (!src || !dst || !dim_map)
1062 goto error;
1064 for (i = 0; i < src->n_eq; ++i) {
1065 int i1 = isl_basic_map_alloc_equality(dst);
1066 if (i1 < 0)
1067 goto error;
1068 copy_constraint_dim_map(dst->eq[i1], src->eq[i], dim_map);
1071 for (i = 0; i < src->n_ineq; ++i) {
1072 int i1 = isl_basic_map_alloc_inequality(dst);
1073 if (i1 < 0)
1074 goto error;
1075 copy_constraint_dim_map(dst->ineq[i1], src->ineq[i], dim_map);
1078 for (i = 0; i < src->n_div; ++i) {
1079 int i1 = isl_basic_map_alloc_div(dst);
1080 if (i1 < 0)
1081 goto error;
1082 copy_div_dim_map(dst->div[i1], src->div[i], dim_map);
1085 free(dim_map);
1086 isl_basic_map_free(src);
1088 return dst;
1089 error:
1090 free(dim_map);
1091 isl_basic_map_free(src);
1092 isl_basic_map_free(dst);
1093 return NULL;
1096 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
1097 struct isl_basic_set *bset2, unsigned pos)
1099 return (struct isl_basic_set *)
1100 add_constraints((struct isl_basic_map *)bset1,
1101 (struct isl_basic_map *)bset2, 0, pos);
1104 struct isl_basic_map *isl_basic_map_extend_dim(struct isl_basic_map *base,
1105 struct isl_dim *dim, unsigned extra,
1106 unsigned n_eq, unsigned n_ineq)
1108 struct isl_basic_map *ext;
1109 unsigned flags;
1110 int dims_ok;
1112 if (!dim)
1113 goto error;
1115 if (!base)
1116 goto error;
1118 dims_ok = isl_dim_equal(base->dim, dim) &&
1119 base->extra >= base->n_div + extra;
1121 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1122 room_for_ineq(base, n_ineq)) {
1123 isl_dim_free(dim);
1124 return base;
1127 isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error);
1128 isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error);
1129 isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error);
1130 extra += base->extra;
1131 n_eq += base->n_eq;
1132 n_ineq += base->n_ineq;
1134 ext = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
1135 dim = NULL;
1136 if (!ext)
1137 goto error;
1139 if (dims_ok)
1140 ext->sample = isl_vec_copy(base->sample);
1141 flags = base->flags;
1142 ext = add_constraints(ext, base, 0, 0);
1143 if (ext) {
1144 ext->flags = flags;
1145 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
1148 return ext;
1150 error:
1151 isl_dim_free(dim);
1152 isl_basic_map_free(base);
1153 return NULL;
1156 struct isl_basic_set *isl_basic_set_extend_dim(struct isl_basic_set *base,
1157 struct isl_dim *dim, unsigned extra,
1158 unsigned n_eq, unsigned n_ineq)
1160 return (struct isl_basic_set *)
1161 isl_basic_map_extend_dim((struct isl_basic_map *)base, dim,
1162 extra, n_eq, n_ineq);
1165 struct isl_basic_map *isl_basic_map_extend_constraints(
1166 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1168 if (!base)
1169 return NULL;
1170 return isl_basic_map_extend_dim(base, isl_dim_copy(base->dim),
1171 0, n_eq, n_ineq);
1174 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
1175 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
1176 unsigned n_eq, unsigned n_ineq)
1178 struct isl_basic_map *bmap;
1179 struct isl_dim *dim;
1181 if (!base)
1182 return NULL;
1183 dim = isl_dim_alloc(base->ctx, nparam, n_in, n_out);
1184 if (!dim)
1185 goto error;
1187 bmap = isl_basic_map_extend_dim(base, dim, extra, n_eq, n_ineq);
1188 return bmap;
1189 error:
1190 isl_basic_map_free(base);
1191 return NULL;
1194 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
1195 unsigned nparam, unsigned dim, unsigned extra,
1196 unsigned n_eq, unsigned n_ineq)
1198 return (struct isl_basic_set *)
1199 isl_basic_map_extend((struct isl_basic_map *)base,
1200 nparam, 0, dim, extra, n_eq, n_ineq);
1203 struct isl_basic_set *isl_basic_set_extend_constraints(
1204 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1206 return (struct isl_basic_set *)
1207 isl_basic_map_extend_constraints((struct isl_basic_map *)base,
1208 n_eq, n_ineq);
1211 struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset)
1213 return (struct isl_basic_set *)
1214 isl_basic_map_cow((struct isl_basic_map *)bset);
1217 struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
1219 if (!bmap)
1220 return NULL;
1222 if (bmap->ref > 1) {
1223 bmap->ref--;
1224 bmap = isl_basic_map_dup(bmap);
1226 if (bmap)
1227 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
1228 return bmap;
1231 struct isl_set *isl_set_cow(struct isl_set *set)
1233 if (!set)
1234 return NULL;
1236 if (set->ref == 1)
1237 return set;
1238 set->ref--;
1239 return isl_set_dup(set);
1242 struct isl_map *isl_map_cow(struct isl_map *map)
1244 if (!map)
1245 return NULL;
1247 if (map->ref == 1)
1248 return map;
1249 map->ref--;
1250 return isl_map_dup(map);
1253 static void swap_vars(struct isl_blk blk, isl_int *a,
1254 unsigned a_len, unsigned b_len)
1256 isl_seq_cpy(blk.data, a+a_len, b_len);
1257 isl_seq_cpy(blk.data+b_len, a, a_len);
1258 isl_seq_cpy(a, blk.data, b_len+a_len);
1261 static __isl_give isl_basic_map *isl_basic_map_swap_vars(
1262 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
1264 int i;
1265 struct isl_blk blk;
1267 if (!bmap)
1268 goto error;
1270 isl_assert(bmap->ctx,
1271 pos + n1 + n2 <= 1 + isl_basic_map_total_dim(bmap), goto error);
1273 if (n1 == 0 || n2 == 0)
1274 return bmap;
1276 bmap = isl_basic_map_cow(bmap);
1277 if (!bmap)
1278 return NULL;
1280 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
1281 if (isl_blk_is_error(blk))
1282 goto error;
1284 for (i = 0; i < bmap->n_eq; ++i)
1285 swap_vars(blk,
1286 bmap->eq[i] + pos, n1, n2);
1288 for (i = 0; i < bmap->n_ineq; ++i)
1289 swap_vars(blk,
1290 bmap->ineq[i] + pos, n1, n2);
1292 for (i = 0; i < bmap->n_div; ++i)
1293 swap_vars(blk,
1294 bmap->div[i]+1 + pos, n1, n2);
1296 isl_blk_free(bmap->ctx, blk);
1298 ISL_F_CLR(bmap, ISL_BASIC_SET_NORMALIZED);
1299 bmap = isl_basic_map_gauss(bmap, NULL);
1300 return isl_basic_map_finalize(bmap);
1301 error:
1302 isl_basic_map_free(bmap);
1303 return NULL;
1306 static __isl_give isl_basic_set *isl_basic_set_swap_vars(
1307 __isl_take isl_basic_set *bset, unsigned n)
1309 unsigned dim;
1310 unsigned nparam;
1312 nparam = isl_basic_set_n_param(bset);
1313 dim = isl_basic_set_n_dim(bset);
1314 isl_assert(bset->ctx, n <= dim, goto error);
1316 return isl_basic_map_swap_vars(bset, 1 + nparam, n, dim - n);
1317 error:
1318 isl_basic_set_free(bset);
1319 return NULL;
1322 static __isl_give isl_set *isl_set_swap_vars(__isl_take isl_set *set,
1323 unsigned n)
1325 int i;
1326 set = isl_set_cow(set);
1327 if (!set)
1328 return NULL;
1330 for (i = 0; i < set->n; ++i) {
1331 set->p[i] = isl_basic_set_swap_vars(set->p[i], n);
1332 if (!set->p[i]) {
1333 isl_set_free(set);
1334 return NULL;
1337 ISL_F_CLR(set, ISL_SET_NORMALIZED);
1338 return set;
1341 struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
1343 int i = 0;
1344 unsigned total;
1345 if (!bmap)
1346 goto error;
1347 total = isl_basic_map_total_dim(bmap);
1348 isl_basic_map_free_div(bmap, bmap->n_div);
1349 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1350 if (bmap->n_eq > 0)
1351 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
1352 else {
1353 i = isl_basic_map_alloc_equality(bmap);
1354 if (i < 0)
1355 goto error;
1357 isl_int_set_si(bmap->eq[i][0], 1);
1358 isl_seq_clr(bmap->eq[i]+1, total);
1359 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
1360 isl_vec_free(bmap->sample);
1361 bmap->sample = NULL;
1362 return isl_basic_map_finalize(bmap);
1363 error:
1364 isl_basic_map_free(bmap);
1365 return NULL;
1368 struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
1370 return (struct isl_basic_set *)
1371 isl_basic_map_set_to_empty((struct isl_basic_map *)bset);
1374 void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
1376 int i;
1377 unsigned off = isl_dim_total(bmap->dim);
1378 isl_int *t = bmap->div[a];
1379 bmap->div[a] = bmap->div[b];
1380 bmap->div[b] = t;
1382 for (i = 0; i < bmap->n_eq; ++i)
1383 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
1385 for (i = 0; i < bmap->n_ineq; ++i)
1386 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
1388 for (i = 0; i < bmap->n_div; ++i)
1389 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
1390 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1393 /* Eliminate the specified n dimensions starting at first from the
1394 * constraints using Fourier-Motzkin. The dimensions themselves
1395 * are not removed.
1397 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
1398 enum isl_dim_type type, unsigned first, unsigned n)
1400 int i;
1402 if (!map)
1403 return NULL;
1404 if (n == 0)
1405 return map;
1407 map = isl_map_cow(map);
1408 if (!map)
1409 return NULL;
1410 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
1411 first += pos(map->dim, type) - 1;
1413 for (i = 0; i < map->n; ++i) {
1414 map->p[i] = isl_basic_map_eliminate_vars(map->p[i], first, n);
1415 if (!map->p[i])
1416 goto error;
1418 return map;
1419 error:
1420 isl_map_free(map);
1421 return NULL;
1424 /* Eliminate the specified n dimensions starting at first from the
1425 * constraints using Fourier-Motzkin. The dimensions themselves
1426 * are not removed.
1428 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
1429 enum isl_dim_type type, unsigned first, unsigned n)
1431 return (isl_set *)isl_map_eliminate((isl_map *)set, type, first, n);
1434 /* Eliminate the specified n dimensions starting at first from the
1435 * constraints using Fourier-Motzkin. The dimensions themselves
1436 * are not removed.
1438 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
1439 unsigned first, unsigned n)
1441 return isl_set_eliminate(set, isl_dim_set, first, n);
1444 __isl_give isl_basic_map *isl_basic_map_remove_divs(
1445 __isl_take isl_basic_map *bmap)
1447 bmap = isl_basic_map_eliminate_vars(bmap, isl_dim_total(bmap->dim),
1448 bmap->n_div);
1449 if (!bmap)
1450 return NULL;
1451 bmap->n_div = 0;
1452 return isl_basic_map_finalize(bmap);
1455 __isl_give isl_basic_set *isl_basic_set_remove_divs(
1456 __isl_take isl_basic_set *bset)
1458 return (struct isl_basic_set *)isl_basic_map_remove_divs(
1459 (struct isl_basic_map *)bset);
1462 struct isl_set *isl_set_remove_divs(struct isl_set *set)
1464 int i;
1466 if (!set)
1467 return NULL;
1468 if (set->n == 0)
1469 return set;
1471 set = isl_set_cow(set);
1472 if (!set)
1473 return NULL;
1475 for (i = 0; i < set->n; ++i) {
1476 set->p[i] = isl_basic_set_remove_divs(set->p[i]);
1477 if (!set->p[i])
1478 goto error;
1480 return set;
1481 error:
1482 isl_set_free(set);
1483 return NULL;
1486 struct isl_basic_map *isl_basic_map_remove_dims(struct isl_basic_map *bmap,
1487 enum isl_dim_type type, unsigned first, unsigned n)
1489 if (!bmap)
1490 return NULL;
1491 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1492 goto error);
1493 if (n == 0)
1494 return bmap;
1495 bmap = isl_basic_map_eliminate_vars(bmap,
1496 isl_basic_map_offset(bmap, type) - 1 + first, n);
1497 if (!bmap)
1498 return bmap;
1499 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
1500 return bmap;
1501 bmap = isl_basic_map_drop(bmap, type, first, n);
1502 return bmap;
1503 error:
1504 isl_basic_map_free(bmap);
1505 return NULL;
1508 /* Return true if the definition of the given div (recursively) involves
1509 * any of the given variables.
1511 static int div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
1512 unsigned first, unsigned n)
1514 int i;
1515 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
1517 if (isl_int_is_zero(bmap->div[div][0]))
1518 return 0;
1519 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
1520 return 1;
1522 for (i = bmap->n_div - 1; i >= 0; --i) {
1523 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
1524 continue;
1525 if (div_involves_vars(bmap, i, first, n))
1526 return 1;
1529 return 0;
1532 /* Remove all divs (recursively) involving any of the given dimensions
1533 * in their definitions.
1535 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
1536 __isl_take isl_basic_map *bmap,
1537 enum isl_dim_type type, unsigned first, unsigned n)
1539 int i;
1541 if (!bmap)
1542 return NULL;
1543 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1544 goto error);
1545 first += isl_basic_map_offset(bmap, type);
1547 for (i = bmap->n_div - 1; i >= 0; --i) {
1548 if (!div_involves_vars(bmap, i, first, n))
1549 continue;
1550 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
1553 return bmap;
1554 error:
1555 isl_basic_map_free(bmap);
1556 return NULL;
1559 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
1560 enum isl_dim_type type, unsigned first, unsigned n)
1562 int i;
1564 if (!map)
1565 return NULL;
1566 if (map->n == 0)
1567 return map;
1569 map = isl_map_cow(map);
1570 if (!map)
1571 return NULL;
1573 for (i = 0; i < map->n; ++i) {
1574 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
1575 type, first, n);
1576 if (!map->p[i])
1577 goto error;
1579 return map;
1580 error:
1581 isl_map_free(map);
1582 return NULL;
1585 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
1586 enum isl_dim_type type, unsigned first, unsigned n)
1588 return (isl_set *)isl_map_remove_divs_involving_dims((isl_map *)set,
1589 type, first, n);
1592 /* Return true if the definition of the given div is unknown or depends
1593 * on unknown divs.
1595 static int div_is_unknown(__isl_keep isl_basic_map *bmap, int div)
1597 int i;
1598 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
1600 if (isl_int_is_zero(bmap->div[div][0]))
1601 return 1;
1603 for (i = bmap->n_div - 1; i >= 0; --i) {
1604 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
1605 continue;
1606 if (div_is_unknown(bmap, i))
1607 return 1;
1610 return 0;
1613 /* Remove all divs that are unknown or defined in terms of unknown divs.
1615 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
1616 __isl_take isl_basic_map *bmap)
1618 int i;
1620 if (!bmap)
1621 return NULL;
1623 for (i = bmap->n_div - 1; i >= 0; --i) {
1624 if (!div_is_unknown(bmap, i))
1625 continue;
1626 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
1629 return bmap;
1630 error:
1631 isl_basic_map_free(bmap);
1632 return NULL;
1635 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
1637 int i;
1639 if (!map)
1640 return NULL;
1641 if (map->n == 0)
1642 return map;
1644 map = isl_map_cow(map);
1645 if (!map)
1646 return NULL;
1648 for (i = 0; i < map->n; ++i) {
1649 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
1650 if (!map->p[i])
1651 goto error;
1653 return map;
1654 error:
1655 isl_map_free(map);
1656 return NULL;
1659 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
1661 return (isl_set *)isl_map_remove_unknown_divs((isl_map *)set);
1664 __isl_give isl_basic_set *isl_basic_set_remove_dims(
1665 __isl_take isl_basic_set *bset,
1666 enum isl_dim_type type, unsigned first, unsigned n)
1668 return (isl_basic_set *)
1669 isl_basic_map_remove_dims((isl_basic_map *)bset, type, first, n);
1672 struct isl_map *isl_map_remove_dims(struct isl_map *map,
1673 enum isl_dim_type type, unsigned first, unsigned n)
1675 int i;
1677 if (n == 0)
1678 return map;
1680 map = isl_map_cow(map);
1681 if (!map)
1682 return NULL;
1683 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
1685 for (i = 0; i < map->n; ++i) {
1686 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
1687 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
1688 if (!map->p[i])
1689 goto error;
1691 map = isl_map_drop(map, type, first, n);
1692 return map;
1693 error:
1694 isl_map_free(map);
1695 return NULL;
1698 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
1699 enum isl_dim_type type, unsigned first, unsigned n)
1701 return (isl_set *)isl_map_remove_dims((isl_map *)bset, type, first, n);
1704 /* Project out n inputs starting at first using Fourier-Motzkin */
1705 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
1706 unsigned first, unsigned n)
1708 return isl_map_remove_dims(map, isl_dim_in, first, n);
1711 static void dump_term(struct isl_basic_map *bmap,
1712 isl_int c, int pos, FILE *out)
1714 const char *name;
1715 unsigned in = isl_basic_map_n_in(bmap);
1716 unsigned dim = in + isl_basic_map_n_out(bmap);
1717 unsigned nparam = isl_basic_map_n_param(bmap);
1718 if (!pos)
1719 isl_int_print(out, c, 0);
1720 else {
1721 if (!isl_int_is_one(c))
1722 isl_int_print(out, c, 0);
1723 if (pos < 1 + nparam) {
1724 name = isl_dim_get_name(bmap->dim,
1725 isl_dim_param, pos - 1);
1726 if (name)
1727 fprintf(out, "%s", name);
1728 else
1729 fprintf(out, "p%d", pos - 1);
1730 } else if (pos < 1 + nparam + in)
1731 fprintf(out, "i%d", pos - 1 - nparam);
1732 else if (pos < 1 + nparam + dim)
1733 fprintf(out, "o%d", pos - 1 - nparam - in);
1734 else
1735 fprintf(out, "e%d", pos - 1 - nparam - dim);
1739 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
1740 int sign, FILE *out)
1742 int i;
1743 int first;
1744 unsigned len = 1 + isl_basic_map_total_dim(bmap);
1745 isl_int v;
1747 isl_int_init(v);
1748 for (i = 0, first = 1; i < len; ++i) {
1749 if (isl_int_sgn(c[i]) * sign <= 0)
1750 continue;
1751 if (!first)
1752 fprintf(out, " + ");
1753 first = 0;
1754 isl_int_abs(v, c[i]);
1755 dump_term(bmap, v, i, out);
1757 isl_int_clear(v);
1758 if (first)
1759 fprintf(out, "0");
1762 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
1763 const char *op, FILE *out, int indent)
1765 int i;
1767 fprintf(out, "%*s", indent, "");
1769 dump_constraint_sign(bmap, c, 1, out);
1770 fprintf(out, " %s ", op);
1771 dump_constraint_sign(bmap, c, -1, out);
1773 fprintf(out, "\n");
1775 for (i = bmap->n_div; i < bmap->extra; ++i) {
1776 if (isl_int_is_zero(c[1+isl_dim_total(bmap->dim)+i]))
1777 continue;
1778 fprintf(out, "%*s", indent, "");
1779 fprintf(out, "ERROR: unused div coefficient not zero\n");
1780 abort();
1784 static void dump_constraints(struct isl_basic_map *bmap,
1785 isl_int **c, unsigned n,
1786 const char *op, FILE *out, int indent)
1788 int i;
1790 for (i = 0; i < n; ++i)
1791 dump_constraint(bmap, c[i], op, out, indent);
1794 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
1796 int j;
1797 int first = 1;
1798 unsigned total = isl_basic_map_total_dim(bmap);
1800 for (j = 0; j < 1 + total; ++j) {
1801 if (isl_int_is_zero(exp[j]))
1802 continue;
1803 if (!first && isl_int_is_pos(exp[j]))
1804 fprintf(out, "+");
1805 dump_term(bmap, exp[j], j, out);
1806 first = 0;
1810 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
1812 int i;
1814 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
1815 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
1817 for (i = 0; i < bmap->n_div; ++i) {
1818 fprintf(out, "%*s", indent, "");
1819 fprintf(out, "e%d = [(", i);
1820 dump_affine(bmap, bmap->div[i]+1, out);
1821 fprintf(out, ")/");
1822 isl_int_print(out, bmap->div[i][0], 0);
1823 fprintf(out, "]\n");
1827 void isl_basic_set_print_internal(struct isl_basic_set *bset,
1828 FILE *out, int indent)
1830 if (!bset) {
1831 fprintf(out, "null basic set\n");
1832 return;
1835 fprintf(out, "%*s", indent, "");
1836 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
1837 bset->ref, bset->dim->nparam, bset->dim->n_out,
1838 bset->extra, bset->flags);
1839 dump((struct isl_basic_map *)bset, out, indent);
1842 void isl_basic_map_print_internal(struct isl_basic_map *bmap,
1843 FILE *out, int indent)
1845 if (!bmap) {
1846 fprintf(out, "null basic map\n");
1847 return;
1850 fprintf(out, "%*s", indent, "");
1851 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
1852 "flags: %x, n_name: %d\n",
1853 bmap->ref,
1854 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
1855 bmap->extra, bmap->flags, bmap->dim->n_name);
1856 dump(bmap, out, indent);
1859 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
1861 unsigned total;
1862 if (!bmap)
1863 return -1;
1864 total = isl_basic_map_total_dim(bmap);
1865 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1866 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
1867 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
1868 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1869 return 0;
1872 struct isl_set *isl_set_alloc_dim(struct isl_dim *dim, int n, unsigned flags)
1874 struct isl_set *set;
1876 if (!dim)
1877 return NULL;
1878 isl_assert(dim->ctx, dim->n_in == 0, goto error);
1879 isl_assert(dim->ctx, n >= 0, goto error);
1880 set = isl_alloc(dim->ctx, struct isl_set,
1881 sizeof(struct isl_set) +
1882 (n - 1) * sizeof(struct isl_basic_set *));
1883 if (!set)
1884 goto error;
1886 set->ctx = dim->ctx;
1887 isl_ctx_ref(set->ctx);
1888 set->ref = 1;
1889 set->size = n;
1890 set->n = 0;
1891 set->dim = dim;
1892 set->flags = flags;
1893 return set;
1894 error:
1895 isl_dim_free(dim);
1896 return NULL;
1899 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
1900 unsigned nparam, unsigned dim, int n, unsigned flags)
1902 struct isl_set *set;
1903 struct isl_dim *dims;
1905 dims = isl_dim_alloc(ctx, nparam, 0, dim);
1906 if (!dims)
1907 return NULL;
1909 set = isl_set_alloc_dim(dims, n, flags);
1910 return set;
1913 /* Make sure "map" has room for at least "n" more basic maps.
1915 struct isl_map *isl_map_grow(struct isl_map *map, int n)
1917 int i;
1918 struct isl_map *grown = NULL;
1920 if (!map)
1921 return NULL;
1922 isl_assert(map->ctx, n >= 0, goto error);
1923 if (map->n + n <= map->size)
1924 return map;
1925 grown = isl_map_alloc_dim(isl_map_get_dim(map), map->n + n, map->flags);
1926 if (!grown)
1927 goto error;
1928 for (i = 0; i < map->n; ++i) {
1929 grown->p[i] = isl_basic_map_copy(map->p[i]);
1930 if (!grown->p[i])
1931 goto error;
1932 grown->n++;
1934 isl_map_free(map);
1935 return grown;
1936 error:
1937 isl_map_free(grown);
1938 isl_map_free(map);
1939 return NULL;
1942 /* Make sure "set" has room for at least "n" more basic sets.
1944 struct isl_set *isl_set_grow(struct isl_set *set, int n)
1946 return (struct isl_set *)isl_map_grow((struct isl_map *)set, n);
1949 struct isl_set *isl_set_dup(struct isl_set *set)
1951 int i;
1952 struct isl_set *dup;
1954 if (!set)
1955 return NULL;
1957 dup = isl_set_alloc_dim(isl_dim_copy(set->dim), set->n, set->flags);
1958 if (!dup)
1959 return NULL;
1960 for (i = 0; i < set->n; ++i)
1961 dup = isl_set_add_basic_set(dup, isl_basic_set_copy(set->p[i]));
1962 return dup;
1965 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
1967 return isl_map_from_basic_map(bset);
1970 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
1972 struct isl_map *map;
1974 if (!bmap)
1975 return NULL;
1977 map = isl_map_alloc_dim(isl_dim_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
1978 return isl_map_add_basic_map(map, bmap);
1981 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
1982 __isl_take isl_basic_set *bset)
1984 return (struct isl_set *)isl_map_add_basic_map((struct isl_map *)set,
1985 (struct isl_basic_map *)bset);
1988 void isl_set_free(struct isl_set *set)
1990 int i;
1992 if (!set)
1993 return;
1995 if (--set->ref > 0)
1996 return;
1998 isl_ctx_deref(set->ctx);
1999 for (i = 0; i < set->n; ++i)
2000 isl_basic_set_free(set->p[i]);
2001 isl_dim_free(set->dim);
2002 free(set);
2005 void isl_set_print_internal(struct isl_set *set, FILE *out, int indent)
2007 int i;
2009 if (!set) {
2010 fprintf(out, "null set\n");
2011 return;
2014 fprintf(out, "%*s", indent, "");
2015 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
2016 set->ref, set->n, set->dim->nparam, set->dim->n_out,
2017 set->flags);
2018 for (i = 0; i < set->n; ++i) {
2019 fprintf(out, "%*s", indent, "");
2020 fprintf(out, "basic set %d:\n", i);
2021 isl_basic_set_print_internal(set->p[i], out, indent+4);
2025 void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
2027 int i;
2029 if (!map) {
2030 fprintf(out, "null map\n");
2031 return;
2034 fprintf(out, "%*s", indent, "");
2035 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2036 "flags: %x, n_name: %d\n",
2037 map->ref, map->n, map->dim->nparam, map->dim->n_in,
2038 map->dim->n_out, map->flags, map->dim->n_name);
2039 for (i = 0; i < map->n; ++i) {
2040 fprintf(out, "%*s", indent, "");
2041 fprintf(out, "basic map %d:\n", i);
2042 isl_basic_map_print_internal(map->p[i], out, indent+4);
2046 struct isl_basic_map *isl_basic_map_intersect_domain(
2047 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2049 struct isl_basic_map *bmap_domain;
2051 if (!bmap || !bset)
2052 goto error;
2054 isl_assert(bset->ctx, isl_dim_match(bmap->dim, isl_dim_param,
2055 bset->dim, isl_dim_param), goto error);
2057 if (isl_dim_size(bset->dim, isl_dim_set) != 0)
2058 isl_assert(bset->ctx,
2059 isl_basic_map_compatible_domain(bmap, bset), goto error);
2061 bmap = isl_basic_map_cow(bmap);
2062 if (!bmap)
2063 goto error;
2064 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
2065 bset->n_div, bset->n_eq, bset->n_ineq);
2066 bmap_domain = isl_basic_map_from_domain(bset);
2067 bmap = add_constraints(bmap, bmap_domain, 0, 0);
2069 bmap = isl_basic_map_simplify(bmap);
2070 return isl_basic_map_finalize(bmap);
2071 error:
2072 isl_basic_map_free(bmap);
2073 isl_basic_set_free(bset);
2074 return NULL;
2077 struct isl_basic_map *isl_basic_map_intersect_range(
2078 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2080 struct isl_basic_map *bmap_range;
2082 if (!bmap || !bset)
2083 goto error;
2085 isl_assert(bset->ctx, isl_dim_match(bmap->dim, isl_dim_param,
2086 bset->dim, isl_dim_param), goto error);
2088 if (isl_dim_size(bset->dim, isl_dim_set) != 0)
2089 isl_assert(bset->ctx,
2090 isl_basic_map_compatible_range(bmap, bset), goto error);
2092 bmap = isl_basic_map_cow(bmap);
2093 if (!bmap)
2094 goto error;
2095 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
2096 bset->n_div, bset->n_eq, bset->n_ineq);
2097 bmap_range = isl_basic_map_from_basic_set(bset, isl_dim_copy(bset->dim));
2098 bmap = add_constraints(bmap, bmap_range, 0, 0);
2100 bmap = isl_basic_map_simplify(bmap);
2101 return isl_basic_map_finalize(bmap);
2102 error:
2103 isl_basic_map_free(bmap);
2104 isl_basic_set_free(bset);
2105 return NULL;
2108 int isl_basic_map_contains(struct isl_basic_map *bmap, struct isl_vec *vec)
2110 int i;
2111 unsigned total;
2112 isl_int s;
2114 total = 1 + isl_basic_map_total_dim(bmap);
2115 if (total != vec->size)
2116 return -1;
2118 isl_int_init(s);
2120 for (i = 0; i < bmap->n_eq; ++i) {
2121 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
2122 if (!isl_int_is_zero(s)) {
2123 isl_int_clear(s);
2124 return 0;
2128 for (i = 0; i < bmap->n_ineq; ++i) {
2129 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
2130 if (isl_int_is_neg(s)) {
2131 isl_int_clear(s);
2132 return 0;
2136 isl_int_clear(s);
2138 return 1;
2141 int isl_basic_set_contains(struct isl_basic_set *bset, struct isl_vec *vec)
2143 return isl_basic_map_contains((struct isl_basic_map *)bset, vec);
2146 struct isl_basic_map *isl_basic_map_intersect(
2147 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2149 struct isl_vec *sample = NULL;
2151 if (!bmap1 || !bmap2)
2152 goto error;
2154 isl_assert(bmap1->ctx, isl_dim_match(bmap1->dim, isl_dim_param,
2155 bmap2->dim, isl_dim_param), goto error);
2156 if (isl_dim_total(bmap1->dim) ==
2157 isl_dim_size(bmap1->dim, isl_dim_param) &&
2158 isl_dim_total(bmap2->dim) !=
2159 isl_dim_size(bmap2->dim, isl_dim_param))
2160 return isl_basic_map_intersect(bmap2, bmap1);
2162 if (isl_dim_total(bmap2->dim) !=
2163 isl_dim_size(bmap2->dim, isl_dim_param))
2164 isl_assert(bmap1->ctx,
2165 isl_dim_equal(bmap1->dim, bmap2->dim), goto error);
2167 if (bmap1->sample &&
2168 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
2169 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
2170 sample = isl_vec_copy(bmap1->sample);
2171 else if (bmap2->sample &&
2172 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
2173 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
2174 sample = isl_vec_copy(bmap2->sample);
2176 bmap1 = isl_basic_map_cow(bmap1);
2177 if (!bmap1)
2178 goto error;
2179 bmap1 = isl_basic_map_extend_dim(bmap1, isl_dim_copy(bmap1->dim),
2180 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
2181 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
2183 if (!bmap1)
2184 isl_vec_free(sample);
2185 else if (sample) {
2186 isl_vec_free(bmap1->sample);
2187 bmap1->sample = sample;
2190 bmap1 = isl_basic_map_simplify(bmap1);
2191 return isl_basic_map_finalize(bmap1);
2192 error:
2193 if (sample)
2194 isl_vec_free(sample);
2195 isl_basic_map_free(bmap1);
2196 isl_basic_map_free(bmap2);
2197 return NULL;
2200 struct isl_basic_set *isl_basic_set_intersect(
2201 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
2203 return (struct isl_basic_set *)
2204 isl_basic_map_intersect(
2205 (struct isl_basic_map *)bset1,
2206 (struct isl_basic_map *)bset2);
2209 /* Special case of isl_map_intersect, where both map1 and map2
2210 * are convex, without any divs and such that either map1 or map2
2211 * contains a single constraint. This constraint is then simply
2212 * added to the other map.
2214 static __isl_give isl_map *map_intersect_add_constraint(
2215 __isl_take isl_map *map1, __isl_take isl_map *map2)
2217 struct isl_basic_map *bmap1;
2218 struct isl_basic_map *bmap2;
2220 isl_assert(map1->ctx, map1->n == 1, goto error);
2221 isl_assert(map2->ctx, map1->n == 1, goto error);
2222 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
2223 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
2225 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
2226 return isl_map_intersect(map2, map1);
2228 isl_assert(map2->ctx,
2229 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1, goto error);
2231 map1 = isl_map_cow(map1);
2232 if (!map1)
2233 goto error;
2234 if (isl_map_fast_is_empty(map1)) {
2235 isl_map_free(map2);
2236 return map1;
2238 map1->p[0] = isl_basic_map_cow(map1->p[0]);
2239 if (map2->p[0]->n_eq == 1)
2240 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
2241 else
2242 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
2243 map2->p[0]->ineq[0]);
2245 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
2246 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
2247 if (!map1->p[0])
2248 goto error;
2250 if (isl_basic_map_fast_is_empty(map1->p[0])) {
2251 isl_basic_map_free(map1->p[0]);
2252 map1->n = 0;
2255 isl_map_free(map2);
2257 return map1;
2258 error:
2259 isl_map_free(map1);
2260 isl_map_free(map2);
2261 return NULL;
2264 struct isl_map *isl_map_intersect(struct isl_map *map1, struct isl_map *map2)
2266 unsigned flags = 0;
2267 struct isl_map *result;
2268 int i, j;
2270 if (!map1 || !map2)
2271 goto error;
2273 if (isl_map_fast_is_empty(map1)) {
2274 isl_map_free(map2);
2275 return map1;
2277 if (isl_map_fast_is_empty(map2)) {
2278 isl_map_free(map1);
2279 return map2;
2282 if (map1->n == 1 && map2->n == 1 &&
2283 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
2284 isl_dim_equal(map1->dim, map2->dim) &&
2285 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
2286 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
2287 return map_intersect_add_constraint(map1, map2);
2288 isl_assert(map1->ctx, isl_dim_match(map1->dim, isl_dim_param,
2289 map2->dim, isl_dim_param), goto error);
2290 if (isl_dim_total(map1->dim) ==
2291 isl_dim_size(map1->dim, isl_dim_param) &&
2292 isl_dim_total(map2->dim) != isl_dim_size(map2->dim, isl_dim_param))
2293 return isl_map_intersect(map2, map1);
2295 if (isl_dim_total(map2->dim) != isl_dim_size(map2->dim, isl_dim_param))
2296 isl_assert(map1->ctx,
2297 isl_dim_equal(map1->dim, map2->dim), goto error);
2299 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
2300 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
2301 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
2303 result = isl_map_alloc_dim(isl_dim_copy(map1->dim),
2304 map1->n * map2->n, flags);
2305 if (!result)
2306 goto error;
2307 for (i = 0; i < map1->n; ++i)
2308 for (j = 0; j < map2->n; ++j) {
2309 struct isl_basic_map *part;
2310 part = isl_basic_map_intersect(
2311 isl_basic_map_copy(map1->p[i]),
2312 isl_basic_map_copy(map2->p[j]));
2313 if (isl_basic_map_is_empty(part))
2314 isl_basic_map_free(part);
2315 else
2316 result = isl_map_add_basic_map(result, part);
2317 if (!result)
2318 goto error;
2320 isl_map_free(map1);
2321 isl_map_free(map2);
2322 return result;
2323 error:
2324 isl_map_free(map1);
2325 isl_map_free(map2);
2326 return NULL;
2329 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
2331 return (struct isl_set *)
2332 isl_map_intersect((struct isl_map *)set1,
2333 (struct isl_map *)set2);
2336 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
2338 struct isl_dim *dim;
2339 struct isl_basic_set *bset;
2340 unsigned in;
2342 if (!bmap)
2343 return NULL;
2344 bmap = isl_basic_map_cow(bmap);
2345 if (!bmap)
2346 return NULL;
2347 dim = isl_dim_reverse(isl_dim_copy(bmap->dim));
2348 in = isl_basic_map_n_in(bmap);
2349 bset = isl_basic_set_from_basic_map(bmap);
2350 bset = isl_basic_set_swap_vars(bset, in);
2351 return isl_basic_map_from_basic_set(bset, dim);
2354 __isl_give isl_basic_map *isl_basic_map_insert(__isl_take isl_basic_map *bmap,
2355 enum isl_dim_type type, unsigned pos, unsigned n)
2357 struct isl_dim *res_dim;
2358 struct isl_basic_map *res;
2359 struct isl_dim_map *dim_map;
2360 unsigned total, off;
2361 enum isl_dim_type t;
2363 if (n == 0)
2364 return bmap;
2366 if (!bmap)
2367 return NULL;
2369 res_dim = isl_dim_insert(isl_basic_map_get_dim(bmap), type, pos, n);
2371 total = isl_basic_map_total_dim(bmap) + n;
2372 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2373 off = 0;
2374 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2375 if (t != type) {
2376 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2377 } else {
2378 unsigned size = isl_basic_map_dim(bmap, t);
2379 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2380 0, pos, off);
2381 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2382 pos, size - pos, off + pos + n);
2384 off += isl_dim_size(res_dim, t);
2386 isl_dim_map_div(dim_map, bmap, off);
2388 res = isl_basic_map_alloc_dim(res_dim,
2389 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2390 if (isl_basic_map_is_rational(bmap))
2391 res = isl_basic_map_set_rational(res);
2392 res = add_constraints_dim_map(res, bmap, dim_map);
2393 return isl_basic_map_finalize(res);
2396 __isl_give isl_basic_map *isl_basic_map_add(__isl_take isl_basic_map *bmap,
2397 enum isl_dim_type type, unsigned n)
2399 if (!bmap)
2400 return NULL;
2401 return isl_basic_map_insert(bmap, type,
2402 isl_basic_map_dim(bmap, type), n);
2405 __isl_give isl_basic_set *isl_basic_set_add(__isl_take isl_basic_set *bset,
2406 enum isl_dim_type type, unsigned n)
2408 if (!bset)
2409 return NULL;
2410 isl_assert(bset->ctx, type != isl_dim_in, goto error);
2411 return (isl_basic_set *)isl_basic_map_add((isl_basic_map *)bset, type, n);
2412 error:
2413 isl_basic_set_free(bset);
2414 return NULL;
2417 __isl_give isl_map *isl_map_insert(__isl_take isl_map *map,
2418 enum isl_dim_type type, unsigned pos, unsigned n)
2420 int i;
2422 if (n == 0)
2423 return map;
2425 map = isl_map_cow(map);
2426 if (!map)
2427 return NULL;
2429 map->dim = isl_dim_insert(map->dim, type, pos, n);
2430 if (!map->dim)
2431 goto error;
2433 for (i = 0; i < map->n; ++i) {
2434 map->p[i] = isl_basic_map_insert(map->p[i], type, pos, n);
2435 if (!map->p[i])
2436 goto error;
2439 return map;
2440 error:
2441 isl_map_free(map);
2442 return NULL;
2445 __isl_give isl_set *isl_set_insert(__isl_take isl_set *set,
2446 enum isl_dim_type type, unsigned pos, unsigned n)
2448 return (isl_set *)isl_map_insert((isl_map *)set, type, pos, n);
2451 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
2452 enum isl_dim_type type, unsigned n)
2454 if (!map)
2455 return NULL;
2456 return isl_map_insert(map, type, isl_map_dim(map, type), n);
2459 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
2460 enum isl_dim_type type, unsigned n)
2462 if (!set)
2463 return NULL;
2464 isl_assert(set->ctx, type != isl_dim_in, goto error);
2465 return (isl_set *)isl_map_add_dims((isl_map *)set, type, n);
2466 error:
2467 isl_set_free(set);
2468 return NULL;
2471 __isl_give isl_basic_map *isl_basic_map_move_dims(
2472 __isl_take isl_basic_map *bmap,
2473 enum isl_dim_type dst_type, unsigned dst_pos,
2474 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2476 int i;
2477 struct isl_dim_map *dim_map;
2478 struct isl_basic_map *res;
2479 enum isl_dim_type t;
2480 unsigned total, off;
2482 if (!bmap)
2483 return NULL;
2484 if (n == 0)
2485 return bmap;
2487 isl_assert(bmap->ctx, src_pos + n <= isl_basic_map_dim(bmap, src_type),
2488 goto error);
2490 if (dst_type == src_type && dst_pos == src_pos)
2491 return bmap;
2493 isl_assert(bmap->ctx, dst_type != src_type, goto error);
2495 if (pos(bmap->dim, dst_type) + dst_pos ==
2496 pos(bmap->dim, src_type) + src_pos +
2497 ((src_type < dst_type) ? n : 0)) {
2498 bmap = isl_basic_map_cow(bmap);
2499 if (!bmap)
2500 return NULL;
2502 bmap->dim = isl_dim_move(bmap->dim, dst_type, dst_pos,
2503 src_type, src_pos, n);
2504 if (!bmap->dim)
2505 goto error;
2507 bmap = isl_basic_map_finalize(bmap);
2509 return bmap;
2512 total = isl_basic_map_total_dim(bmap);
2513 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2515 off = 0;
2516 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2517 unsigned size = isl_dim_size(bmap->dim, t);
2518 if (t == dst_type) {
2519 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2520 0, dst_pos, off);
2521 off += dst_pos;
2522 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
2523 src_pos, n, off);
2524 off += n;
2525 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2526 dst_pos, size - dst_pos, off);
2527 off += size - dst_pos;
2528 } else if (t == src_type) {
2529 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2530 0, src_pos, off);
2531 off += src_pos;
2532 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2533 src_pos + n, size - src_pos - n, off);
2534 off += size - src_pos - n;
2535 } else {
2536 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2537 off += size;
2540 isl_dim_map_div(dim_map, bmap, off);
2542 res = isl_basic_map_alloc_dim(isl_basic_map_get_dim(bmap),
2543 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2544 bmap = add_constraints_dim_map(res, bmap, dim_map);
2546 bmap->dim = isl_dim_move(bmap->dim, dst_type, dst_pos,
2547 src_type, src_pos, n);
2548 if (!bmap->dim)
2549 goto error;
2551 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2552 bmap = isl_basic_map_gauss(bmap, NULL);
2553 bmap = isl_basic_map_finalize(bmap);
2555 return bmap;
2556 error:
2557 isl_basic_map_free(bmap);
2558 return NULL;
2561 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
2562 enum isl_dim_type dst_type, unsigned dst_pos,
2563 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2565 return (isl_basic_set *)isl_basic_map_move_dims(
2566 (isl_basic_map *)bset, dst_type, dst_pos, src_type, src_pos, n);
2569 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
2570 enum isl_dim_type dst_type, unsigned dst_pos,
2571 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2573 if (!set)
2574 return NULL;
2575 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
2576 return (isl_set *)isl_map_move_dims((isl_map *)set, dst_type, dst_pos,
2577 src_type, src_pos, n);
2578 error:
2579 isl_set_free(set);
2580 return NULL;
2583 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
2584 enum isl_dim_type dst_type, unsigned dst_pos,
2585 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2587 int i;
2589 if (!map)
2590 return NULL;
2591 if (n == 0)
2592 return map;
2594 isl_assert(map->ctx, src_pos + n <= isl_map_dim(map, src_type),
2595 goto error);
2597 if (dst_type == src_type && dst_pos == src_pos)
2598 return map;
2600 isl_assert(map->ctx, dst_type != src_type, goto error);
2602 map = isl_map_cow(map);
2603 if (!map)
2604 return NULL;
2606 map->dim = isl_dim_move(map->dim, dst_type, dst_pos, src_type, src_pos, n);
2607 if (!map->dim)
2608 goto error;
2610 for (i = 0; i < map->n; ++i) {
2611 map->p[i] = isl_basic_map_move_dims(map->p[i],
2612 dst_type, dst_pos,
2613 src_type, src_pos, n);
2614 if (!map->p[i])
2615 goto error;
2618 return map;
2619 error:
2620 isl_map_free(map);
2621 return NULL;
2624 /* Move the specified dimensions to the last columns right before
2625 * the divs. Don't change the dimension specification of bmap.
2626 * That's the responsibility of the caller.
2628 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
2629 enum isl_dim_type type, unsigned first, unsigned n)
2631 struct isl_dim_map *dim_map;
2632 struct isl_basic_map *res;
2633 enum isl_dim_type t;
2634 unsigned total, off;
2636 if (!bmap)
2637 return NULL;
2638 if (pos(bmap->dim, type) + first + n == 1 + isl_dim_total(bmap->dim))
2639 return bmap;
2641 total = isl_basic_map_total_dim(bmap);
2642 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2644 off = 0;
2645 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2646 unsigned size = isl_dim_size(bmap->dim, t);
2647 if (t == type) {
2648 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2649 0, first, off);
2650 off += first;
2651 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2652 first, n, total - bmap->n_div - n);
2653 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2654 first + n, size - (first + n), off);
2655 off += size - (first + n);
2656 } else {
2657 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2658 off += size;
2661 isl_dim_map_div(dim_map, bmap, off + n);
2663 res = isl_basic_map_alloc_dim(isl_basic_map_get_dim(bmap),
2664 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2665 res = add_constraints_dim_map(res, bmap, dim_map);
2666 return res;
2669 /* Turn the n dimensions of type type, starting at first
2670 * into existentially quantified variables.
2672 __isl_give isl_basic_map *isl_basic_map_project_out(
2673 __isl_take isl_basic_map *bmap,
2674 enum isl_dim_type type, unsigned first, unsigned n)
2676 int i;
2677 size_t row_size;
2678 isl_int **new_div;
2679 isl_int *old;
2681 if (n == 0)
2682 return bmap;
2684 if (!bmap)
2685 return NULL;
2687 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
2688 return isl_basic_map_remove_dims(bmap, type, first, n);
2690 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
2691 goto error);
2693 bmap = move_last(bmap, type, first, n);
2694 bmap = isl_basic_map_cow(bmap);
2695 if (!bmap)
2696 return NULL;
2698 row_size = 1 + isl_dim_total(bmap->dim) + bmap->extra;
2699 old = bmap->block2.data;
2700 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
2701 (bmap->extra + n) * (1 + row_size));
2702 if (!bmap->block2.data)
2703 goto error;
2704 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
2705 if (!new_div)
2706 goto error;
2707 for (i = 0; i < n; ++i) {
2708 new_div[i] = bmap->block2.data +
2709 (bmap->extra + i) * (1 + row_size);
2710 isl_seq_clr(new_div[i], 1 + row_size);
2712 for (i = 0; i < bmap->extra; ++i)
2713 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
2714 free(bmap->div);
2715 bmap->div = new_div;
2716 bmap->n_div += n;
2717 bmap->extra += n;
2719 bmap->dim = isl_dim_drop(bmap->dim, type, first, n);
2720 if (!bmap->dim)
2721 goto error;
2722 bmap = isl_basic_map_simplify(bmap);
2723 bmap = isl_basic_map_drop_redundant_divs(bmap);
2724 return isl_basic_map_finalize(bmap);
2725 error:
2726 isl_basic_map_free(bmap);
2727 return NULL;
2730 /* Turn the n dimensions of type type, starting at first
2731 * into existentially quantified variables.
2733 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
2734 enum isl_dim_type type, unsigned first, unsigned n)
2736 return (isl_basic_set *)isl_basic_map_project_out(
2737 (isl_basic_map *)bset, type, first, n);
2740 /* Turn the n dimensions of type type, starting at first
2741 * into existentially quantified variables.
2743 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
2744 enum isl_dim_type type, unsigned first, unsigned n)
2746 int i;
2748 if (!map)
2749 return NULL;
2751 if (n == 0)
2752 return map;
2754 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
2756 map = isl_map_cow(map);
2757 if (!map)
2758 return NULL;
2760 map->dim = isl_dim_drop(map->dim, type, first, n);
2761 if (!map->dim)
2762 goto error;
2764 for (i = 0; i < map->n; ++i) {
2765 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
2766 if (!map->p[i])
2767 goto error;
2770 return map;
2771 error:
2772 isl_map_free(map);
2773 return NULL;
2776 /* Turn the n dimensions of type type, starting at first
2777 * into existentially quantified variables.
2779 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
2780 enum isl_dim_type type, unsigned first, unsigned n)
2782 return (isl_set *)isl_map_project_out((isl_map *)set, type, first, n);
2785 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
2787 int i, j;
2789 for (i = 0; i < n; ++i) {
2790 j = isl_basic_map_alloc_div(bmap);
2791 if (j < 0)
2792 goto error;
2793 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
2795 return bmap;
2796 error:
2797 isl_basic_map_free(bmap);
2798 return NULL;
2801 struct isl_basic_map *isl_basic_map_apply_range(
2802 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2804 struct isl_dim *dim_result = NULL;
2805 struct isl_basic_map *bmap;
2806 unsigned n_in, n_out, n, nparam, total, pos;
2807 struct isl_dim_map *dim_map1, *dim_map2;
2809 if (!bmap1 || !bmap2)
2810 goto error;
2812 dim_result = isl_dim_join(isl_dim_copy(bmap1->dim),
2813 isl_dim_copy(bmap2->dim));
2815 n_in = isl_basic_map_n_in(bmap1);
2816 n_out = isl_basic_map_n_out(bmap2);
2817 n = isl_basic_map_n_out(bmap1);
2818 nparam = isl_basic_map_n_param(bmap1);
2820 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
2821 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
2822 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
2823 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
2824 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
2825 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
2826 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
2827 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
2828 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
2829 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
2830 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
2832 bmap = isl_basic_map_alloc_dim(dim_result,
2833 bmap1->n_div + bmap2->n_div + n,
2834 bmap1->n_eq + bmap2->n_eq,
2835 bmap1->n_ineq + bmap2->n_ineq);
2836 bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
2837 bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
2838 bmap = add_divs(bmap, n);
2839 bmap = isl_basic_map_simplify(bmap);
2840 bmap = isl_basic_map_drop_redundant_divs(bmap);
2841 return isl_basic_map_finalize(bmap);
2842 error:
2843 isl_basic_map_free(bmap1);
2844 isl_basic_map_free(bmap2);
2845 return NULL;
2848 struct isl_basic_set *isl_basic_set_apply(
2849 struct isl_basic_set *bset, struct isl_basic_map *bmap)
2851 if (!bset || !bmap)
2852 goto error;
2854 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
2855 goto error);
2857 return (struct isl_basic_set *)
2858 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
2859 error:
2860 isl_basic_set_free(bset);
2861 isl_basic_map_free(bmap);
2862 return NULL;
2865 struct isl_basic_map *isl_basic_map_apply_domain(
2866 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2868 if (!bmap1 || !bmap2)
2869 goto error;
2871 isl_assert(bmap1->ctx,
2872 isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
2873 isl_assert(bmap1->ctx,
2874 isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
2875 goto error);
2877 bmap1 = isl_basic_map_reverse(bmap1);
2878 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
2879 return isl_basic_map_reverse(bmap1);
2880 error:
2881 isl_basic_map_free(bmap1);
2882 isl_basic_map_free(bmap2);
2883 return NULL;
2886 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
2887 * A \cap B -> f(A) + f(B)
2889 struct isl_basic_map *isl_basic_map_sum(
2890 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2892 unsigned n_in, n_out, nparam, total, pos;
2893 struct isl_basic_map *bmap = NULL;
2894 struct isl_dim_map *dim_map1, *dim_map2;
2895 int i;
2897 if (!bmap1 || !bmap2)
2898 goto error;
2900 isl_assert(bmap1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim),
2901 goto error);
2903 nparam = isl_basic_map_n_param(bmap1);
2904 n_in = isl_basic_map_n_in(bmap1);
2905 n_out = isl_basic_map_n_out(bmap1);
2907 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
2908 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
2909 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
2910 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
2911 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
2912 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
2913 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
2914 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
2915 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
2916 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
2917 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
2919 bmap = isl_basic_map_alloc_dim(isl_dim_copy(bmap1->dim),
2920 bmap1->n_div + bmap2->n_div + 2 * n_out,
2921 bmap1->n_eq + bmap2->n_eq + n_out,
2922 bmap1->n_ineq + bmap2->n_ineq);
2923 for (i = 0; i < n_out; ++i) {
2924 int j = isl_basic_map_alloc_equality(bmap);
2925 if (j < 0)
2926 goto error;
2927 isl_seq_clr(bmap->eq[j], 1+total);
2928 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
2929 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
2930 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
2932 bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
2933 bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
2934 bmap = add_divs(bmap, 2 * n_out);
2936 bmap = isl_basic_map_simplify(bmap);
2937 return isl_basic_map_finalize(bmap);
2938 error:
2939 isl_basic_map_free(bmap);
2940 isl_basic_map_free(bmap1);
2941 isl_basic_map_free(bmap2);
2942 return NULL;
2945 /* Given two maps A -> f(A) and B -> g(B), construct a map
2946 * A \cap B -> f(A) + f(B)
2948 struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
2950 struct isl_map *result;
2951 int i, j;
2953 if (!map1 || !map2)
2954 goto error;
2956 isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
2958 result = isl_map_alloc_dim(isl_dim_copy(map1->dim),
2959 map1->n * map2->n, 0);
2960 if (!result)
2961 goto error;
2962 for (i = 0; i < map1->n; ++i)
2963 for (j = 0; j < map2->n; ++j) {
2964 struct isl_basic_map *part;
2965 part = isl_basic_map_sum(
2966 isl_basic_map_copy(map1->p[i]),
2967 isl_basic_map_copy(map2->p[j]));
2968 if (isl_basic_map_is_empty(part))
2969 isl_basic_map_free(part);
2970 else
2971 result = isl_map_add_basic_map(result, part);
2972 if (!result)
2973 goto error;
2975 isl_map_free(map1);
2976 isl_map_free(map2);
2977 return result;
2978 error:
2979 isl_map_free(map1);
2980 isl_map_free(map2);
2981 return NULL;
2984 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
2985 __isl_take isl_set *set2)
2987 return (isl_set *)isl_map_sum((isl_map *)set1, (isl_map *)set2);
2990 /* Given a basic map A -> f(A), construct A -> -f(A).
2992 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
2994 int i, j;
2995 unsigned off, n;
2997 bmap = isl_basic_map_cow(bmap);
2998 if (!bmap)
2999 return NULL;
3001 n = isl_basic_map_dim(bmap, isl_dim_out);
3002 off = isl_basic_map_offset(bmap, isl_dim_out);
3003 for (i = 0; i < bmap->n_eq; ++i)
3004 for (j = 0; j < n; ++j)
3005 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
3006 for (i = 0; i < bmap->n_ineq; ++i)
3007 for (j = 0; j < n; ++j)
3008 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
3009 for (i = 0; i < bmap->n_div; ++i)
3010 for (j = 0; j < n; ++j)
3011 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
3012 return isl_basic_map_finalize(bmap);
3015 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
3017 return isl_basic_map_neg(bset);
3020 /* Given a map A -> f(A), construct A -> -f(A).
3022 struct isl_map *isl_map_neg(struct isl_map *map)
3024 int i;
3026 map = isl_map_cow(map);
3027 if (!map)
3028 return NULL;
3030 for (i = 0; i < map->n; ++i) {
3031 map->p[i] = isl_basic_map_neg(map->p[i]);
3032 if (!map->p[i])
3033 goto error;
3036 return map;
3037 error:
3038 isl_map_free(map);
3039 return NULL;
3042 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
3044 return (isl_set *)isl_map_neg((isl_map *)set);
3047 /* Given a basic map A -> f(A) and an integer d, construct a basic map
3048 * A -> floor(f(A)/d).
3050 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
3051 isl_int d)
3053 unsigned n_in, n_out, nparam, total, pos;
3054 struct isl_basic_map *result = NULL;
3055 struct isl_dim_map *dim_map;
3056 int i;
3058 if (!bmap)
3059 return NULL;
3061 nparam = isl_basic_map_n_param(bmap);
3062 n_in = isl_basic_map_n_in(bmap);
3063 n_out = isl_basic_map_n_out(bmap);
3065 total = nparam + n_in + n_out + bmap->n_div + n_out;
3066 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3067 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
3068 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
3069 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
3070 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
3072 result = isl_basic_map_alloc_dim(isl_dim_copy(bmap->dim),
3073 bmap->n_div + n_out,
3074 bmap->n_eq, bmap->n_ineq + 2 * n_out);
3075 result = add_constraints_dim_map(result, bmap, dim_map);
3076 result = add_divs(result, n_out);
3077 for (i = 0; i < n_out; ++i) {
3078 int j;
3079 j = isl_basic_map_alloc_inequality(result);
3080 if (j < 0)
3081 goto error;
3082 isl_seq_clr(result->ineq[j], 1+total);
3083 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
3084 isl_int_set_si(result->ineq[j][1+pos+i], 1);
3085 j = isl_basic_map_alloc_inequality(result);
3086 if (j < 0)
3087 goto error;
3088 isl_seq_clr(result->ineq[j], 1+total);
3089 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
3090 isl_int_set_si(result->ineq[j][1+pos+i], -1);
3091 isl_int_sub_ui(result->ineq[j][0], d, 1);
3094 result = isl_basic_map_simplify(result);
3095 return isl_basic_map_finalize(result);
3096 error:
3097 isl_basic_map_free(result);
3098 return NULL;
3101 /* Given a map A -> f(A) and an integer d, construct a map
3102 * A -> floor(f(A)/d).
3104 struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
3106 int i;
3108 map = isl_map_cow(map);
3109 if (!map)
3110 return NULL;
3112 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3113 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3114 for (i = 0; i < map->n; ++i) {
3115 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
3116 if (!map->p[i])
3117 goto error;
3120 return map;
3121 error:
3122 isl_map_free(map);
3123 return NULL;
3126 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
3128 int i;
3129 unsigned nparam;
3130 unsigned n_in;
3132 i = isl_basic_map_alloc_equality(bmap);
3133 if (i < 0)
3134 goto error;
3135 nparam = isl_basic_map_n_param(bmap);
3136 n_in = isl_basic_map_n_in(bmap);
3137 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
3138 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
3139 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
3140 return isl_basic_map_finalize(bmap);
3141 error:
3142 isl_basic_map_free(bmap);
3143 return NULL;
3146 /* Add a constraints to "bmap" expressing i_pos < o_pos
3148 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
3150 int i;
3151 unsigned nparam;
3152 unsigned n_in;
3154 i = isl_basic_map_alloc_inequality(bmap);
3155 if (i < 0)
3156 goto error;
3157 nparam = isl_basic_map_n_param(bmap);
3158 n_in = isl_basic_map_n_in(bmap);
3159 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3160 isl_int_set_si(bmap->ineq[i][0], -1);
3161 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
3162 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
3163 return isl_basic_map_finalize(bmap);
3164 error:
3165 isl_basic_map_free(bmap);
3166 return NULL;
3169 /* Add a constraint to "bmap" expressing i_pos <= o_pos
3171 static __isl_give isl_basic_map *var_less_or_equal(
3172 __isl_take isl_basic_map *bmap, unsigned pos)
3174 int i;
3175 unsigned nparam;
3176 unsigned n_in;
3178 i = isl_basic_map_alloc_inequality(bmap);
3179 if (i < 0)
3180 goto error;
3181 nparam = isl_basic_map_n_param(bmap);
3182 n_in = isl_basic_map_n_in(bmap);
3183 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3184 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
3185 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
3186 return isl_basic_map_finalize(bmap);
3187 error:
3188 isl_basic_map_free(bmap);
3189 return NULL;
3192 /* Add a constraints to "bmap" expressing i_pos > o_pos
3194 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
3196 int i;
3197 unsigned nparam;
3198 unsigned n_in;
3200 i = isl_basic_map_alloc_inequality(bmap);
3201 if (i < 0)
3202 goto error;
3203 nparam = isl_basic_map_n_param(bmap);
3204 n_in = isl_basic_map_n_in(bmap);
3205 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3206 isl_int_set_si(bmap->ineq[i][0], -1);
3207 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
3208 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
3209 return isl_basic_map_finalize(bmap);
3210 error:
3211 isl_basic_map_free(bmap);
3212 return NULL;
3215 /* Add a constraint to "bmap" expressing i_pos >= o_pos
3217 static __isl_give isl_basic_map *var_more_or_equal(
3218 __isl_take isl_basic_map *bmap, unsigned pos)
3220 int i;
3221 unsigned nparam;
3222 unsigned n_in;
3224 i = isl_basic_map_alloc_inequality(bmap);
3225 if (i < 0)
3226 goto error;
3227 nparam = isl_basic_map_n_param(bmap);
3228 n_in = isl_basic_map_n_in(bmap);
3229 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3230 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
3231 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
3232 return isl_basic_map_finalize(bmap);
3233 error:
3234 isl_basic_map_free(bmap);
3235 return NULL;
3238 struct isl_basic_map *isl_basic_map_equal(struct isl_dim *dim, unsigned n_equal)
3240 int i;
3241 struct isl_basic_map *bmap;
3242 bmap = isl_basic_map_alloc_dim(dim, 0, n_equal, 0);
3243 if (!bmap)
3244 return NULL;
3245 for (i = 0; i < n_equal && bmap; ++i)
3246 bmap = var_equal(bmap, i);
3247 return isl_basic_map_finalize(bmap);
3250 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
3252 struct isl_basic_map *isl_basic_map_less_at(struct isl_dim *dim, unsigned pos)
3254 int i;
3255 struct isl_basic_map *bmap;
3256 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
3257 if (!bmap)
3258 return NULL;
3259 for (i = 0; i < pos && bmap; ++i)
3260 bmap = var_equal(bmap, i);
3261 if (bmap)
3262 bmap = var_less(bmap, pos);
3263 return isl_basic_map_finalize(bmap);
3266 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
3268 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
3269 __isl_take isl_dim *dim, unsigned pos)
3271 int i;
3272 isl_basic_map *bmap;
3274 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
3275 for (i = 0; i < pos; ++i)
3276 bmap = var_equal(bmap, i);
3277 bmap = var_less_or_equal(bmap, pos);
3278 return isl_basic_map_finalize(bmap);
3281 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
3283 struct isl_basic_map *isl_basic_map_more_at(struct isl_dim *dim, unsigned pos)
3285 int i;
3286 struct isl_basic_map *bmap;
3287 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
3288 if (!bmap)
3289 return NULL;
3290 for (i = 0; i < pos && bmap; ++i)
3291 bmap = var_equal(bmap, i);
3292 if (bmap)
3293 bmap = var_more(bmap, pos);
3294 return isl_basic_map_finalize(bmap);
3297 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
3299 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
3300 __isl_take isl_dim *dim, unsigned pos)
3302 int i;
3303 isl_basic_map *bmap;
3305 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
3306 for (i = 0; i < pos; ++i)
3307 bmap = var_equal(bmap, i);
3308 bmap = var_more_or_equal(bmap, pos);
3309 return isl_basic_map_finalize(bmap);
3312 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_dim *dims,
3313 unsigned n, int equal)
3315 struct isl_map *map;
3316 int i;
3318 if (n == 0 && equal)
3319 return isl_map_universe(dims);
3321 map = isl_map_alloc_dim(isl_dim_copy(dims), n, ISL_MAP_DISJOINT);
3323 for (i = 0; i + 1 < n; ++i)
3324 map = isl_map_add_basic_map(map,
3325 isl_basic_map_less_at(isl_dim_copy(dims), i));
3326 if (n > 0) {
3327 if (equal)
3328 map = isl_map_add_basic_map(map,
3329 isl_basic_map_less_or_equal_at(dims, n - 1));
3330 else
3331 map = isl_map_add_basic_map(map,
3332 isl_basic_map_less_at(dims, n - 1));
3333 } else
3334 isl_dim_free(dims);
3336 return map;
3339 static __isl_give isl_map *map_lex_lte(__isl_take isl_dim *dims, int equal)
3341 if (!dims)
3342 return NULL;
3343 return map_lex_lte_first(dims, dims->n_out, equal);
3346 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_dim *dim, unsigned n)
3348 return map_lex_lte_first(dim, n, 0);
3351 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_dim *dim, unsigned n)
3353 return map_lex_lte_first(dim, n, 1);
3356 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_dim *set_dim)
3358 return map_lex_lte(isl_dim_map_from_set(set_dim), 0);
3361 __isl_give isl_map *isl_map_lex_le(__isl_take isl_dim *set_dim)
3363 return map_lex_lte(isl_dim_map_from_set(set_dim), 1);
3366 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_dim *dims,
3367 unsigned n, int equal)
3369 struct isl_map *map;
3370 int i;
3372 if (n == 0 && equal)
3373 return isl_map_universe(dims);
3375 map = isl_map_alloc_dim(isl_dim_copy(dims), n, ISL_MAP_DISJOINT);
3377 for (i = 0; i + 1 < n; ++i)
3378 map = isl_map_add_basic_map(map,
3379 isl_basic_map_more_at(isl_dim_copy(dims), i));
3380 if (n > 0) {
3381 if (equal)
3382 map = isl_map_add_basic_map(map,
3383 isl_basic_map_more_or_equal_at(dims, n - 1));
3384 else
3385 map = isl_map_add_basic_map(map,
3386 isl_basic_map_more_at(dims, n - 1));
3387 } else
3388 isl_dim_free(dims);
3390 return map;
3393 static __isl_give isl_map *map_lex_gte(__isl_take isl_dim *dims, int equal)
3395 if (!dims)
3396 return NULL;
3397 return map_lex_gte_first(dims, dims->n_out, equal);
3400 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_dim *dim, unsigned n)
3402 return map_lex_gte_first(dim, n, 0);
3405 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_dim *dim, unsigned n)
3407 return map_lex_gte_first(dim, n, 1);
3410 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_dim *set_dim)
3412 return map_lex_gte(isl_dim_map_from_set(set_dim), 0);
3415 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_dim *set_dim)
3417 return map_lex_gte(isl_dim_map_from_set(set_dim), 1);
3420 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
3421 __isl_take isl_set *set2)
3423 isl_map *map;
3424 map = isl_map_lex_le(isl_set_get_dim(set1));
3425 map = isl_map_intersect_domain(map, set1);
3426 map = isl_map_intersect_range(map, set2);
3427 return map;
3430 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
3431 __isl_take isl_set *set2)
3433 isl_map *map;
3434 map = isl_map_lex_lt(isl_set_get_dim(set1));
3435 map = isl_map_intersect_domain(map, set1);
3436 map = isl_map_intersect_range(map, set2);
3437 return map;
3440 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
3441 __isl_take isl_set *set2)
3443 isl_map *map;
3444 map = isl_map_lex_ge(isl_set_get_dim(set1));
3445 map = isl_map_intersect_domain(map, set1);
3446 map = isl_map_intersect_range(map, set2);
3447 return map;
3450 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
3451 __isl_take isl_set *set2)
3453 isl_map *map;
3454 map = isl_map_lex_gt(isl_set_get_dim(set1));
3455 map = isl_map_intersect_domain(map, set1);
3456 map = isl_map_intersect_range(map, set2);
3457 return map;
3460 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
3461 __isl_take isl_map *map2)
3463 isl_map *map;
3464 map = isl_map_lex_le(isl_dim_range(isl_map_get_dim(map1)));
3465 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3466 map = isl_map_apply_range(map, isl_map_reverse(map2));
3467 return map;
3470 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
3471 __isl_take isl_map *map2)
3473 isl_map *map;
3474 map = isl_map_lex_lt(isl_dim_range(isl_map_get_dim(map1)));
3475 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3476 map = isl_map_apply_range(map, isl_map_reverse(map2));
3477 return map;
3480 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
3481 __isl_take isl_map *map2)
3483 isl_map *map;
3484 map = isl_map_lex_ge(isl_dim_range(isl_map_get_dim(map1)));
3485 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3486 map = isl_map_apply_range(map, isl_map_reverse(map2));
3487 return map;
3490 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
3491 __isl_take isl_map *map2)
3493 isl_map *map;
3494 map = isl_map_lex_gt(isl_dim_range(isl_map_get_dim(map1)));
3495 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3496 map = isl_map_apply_range(map, isl_map_reverse(map2));
3497 return map;
3500 struct isl_basic_map *isl_basic_map_from_basic_set(
3501 struct isl_basic_set *bset, struct isl_dim *dim)
3503 struct isl_basic_map *bmap;
3505 bset = isl_basic_set_cow(bset);
3506 if (!bset || !dim)
3507 goto error;
3509 isl_assert(bset->ctx, isl_dim_compatible(bset->dim, dim), goto error);
3510 isl_dim_free(bset->dim);
3511 bmap = (struct isl_basic_map *) bset;
3512 bmap->dim = dim;
3513 return isl_basic_map_finalize(bmap);
3514 error:
3515 isl_basic_set_free(bset);
3516 isl_dim_free(dim);
3517 return NULL;
3520 struct isl_basic_set *isl_basic_set_from_basic_map(struct isl_basic_map *bmap)
3522 if (!bmap)
3523 goto error;
3524 if (bmap->dim->n_in == 0)
3525 return (struct isl_basic_set *)bmap;
3526 bmap = isl_basic_map_cow(bmap);
3527 if (!bmap)
3528 goto error;
3529 bmap->dim = isl_dim_as_set_dim(bmap->dim);
3530 if (!bmap->dim)
3531 goto error;
3532 bmap = isl_basic_map_finalize(bmap);
3533 return (struct isl_basic_set *)bmap;
3534 error:
3535 isl_basic_map_free(bmap);
3536 return NULL;
3539 /* For a div d = floor(f/m), add the constraints
3541 * f - m d >= 0
3542 * -(f-(n-1)) + m d >= 0
3544 * Note that the second constraint is the negation of
3546 * f - m d >= n
3548 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
3549 unsigned pos, isl_int *div)
3551 int i, j;
3552 unsigned total = isl_basic_map_total_dim(bmap);
3554 i = isl_basic_map_alloc_inequality(bmap);
3555 if (i < 0)
3556 return -1;
3557 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
3558 isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
3560 j = isl_basic_map_alloc_inequality(bmap);
3561 if (j < 0)
3562 return -1;
3563 isl_seq_neg(bmap->ineq[j], bmap->ineq[i], 1 + total);
3564 isl_int_add(bmap->ineq[j][0], bmap->ineq[j][0], bmap->ineq[j][1 + pos]);
3565 isl_int_sub_ui(bmap->ineq[j][0], bmap->ineq[j][0], 1);
3566 return j;
3569 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
3570 unsigned pos, isl_int *div)
3572 return isl_basic_map_add_div_constraints_var((isl_basic_map *)bset,
3573 pos, div);
3576 int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
3578 unsigned total = isl_basic_map_total_dim(bmap);
3579 unsigned div_pos = total - bmap->n_div + div;
3581 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
3582 bmap->div[div]);
3585 struct isl_basic_set *isl_basic_map_underlying_set(
3586 struct isl_basic_map *bmap)
3588 if (!bmap)
3589 goto error;
3590 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
3591 bmap->n_div == 0 &&
3592 !isl_dim_is_named_or_nested(bmap->dim, isl_dim_in) &&
3593 !isl_dim_is_named_or_nested(bmap->dim, isl_dim_out))
3594 return (struct isl_basic_set *)bmap;
3595 bmap = isl_basic_map_cow(bmap);
3596 if (!bmap)
3597 goto error;
3598 bmap->dim = isl_dim_underlying(bmap->dim, bmap->n_div);
3599 if (!bmap->dim)
3600 goto error;
3601 bmap->extra -= bmap->n_div;
3602 bmap->n_div = 0;
3603 bmap = isl_basic_map_finalize(bmap);
3604 return (struct isl_basic_set *)bmap;
3605 error:
3606 return NULL;
3609 __isl_give isl_basic_set *isl_basic_set_underlying_set(
3610 __isl_take isl_basic_set *bset)
3612 return isl_basic_map_underlying_set((isl_basic_map *)bset);
3615 struct isl_basic_map *isl_basic_map_overlying_set(
3616 struct isl_basic_set *bset, struct isl_basic_map *like)
3618 struct isl_basic_map *bmap;
3619 struct isl_ctx *ctx;
3620 unsigned total;
3621 int i;
3623 if (!bset || !like)
3624 goto error;
3625 ctx = bset->ctx;
3626 isl_assert(ctx, bset->n_div == 0, goto error);
3627 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
3628 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
3629 goto error);
3630 if (isl_dim_equal(bset->dim, like->dim) && like->n_div == 0) {
3631 isl_basic_map_free(like);
3632 return (struct isl_basic_map *)bset;
3634 bset = isl_basic_set_cow(bset);
3635 if (!bset)
3636 goto error;
3637 total = bset->dim->n_out + bset->extra;
3638 bmap = (struct isl_basic_map *)bset;
3639 isl_dim_free(bmap->dim);
3640 bmap->dim = isl_dim_copy(like->dim);
3641 if (!bmap->dim)
3642 goto error;
3643 bmap->n_div = like->n_div;
3644 bmap->extra += like->n_div;
3645 if (bmap->extra) {
3646 unsigned ltotal;
3647 ltotal = total - bmap->extra + like->extra;
3648 if (ltotal > total)
3649 ltotal = total;
3650 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
3651 bmap->extra * (1 + 1 + total));
3652 if (isl_blk_is_error(bmap->block2))
3653 goto error;
3654 bmap->div = isl_realloc_array(ctx, bmap->div, isl_int *,
3655 bmap->extra);
3656 if (!bmap->div)
3657 goto error;
3658 for (i = 0; i < bmap->extra; ++i)
3659 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
3660 for (i = 0; i < like->n_div; ++i) {
3661 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
3662 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
3664 bmap = isl_basic_map_extend_constraints(bmap,
3665 0, 2 * like->n_div);
3666 for (i = 0; i < like->n_div; ++i) {
3667 if (isl_int_is_zero(bmap->div[i][0]))
3668 continue;
3669 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
3670 goto error;
3673 isl_basic_map_free(like);
3674 bmap = isl_basic_map_simplify(bmap);
3675 bmap = isl_basic_map_finalize(bmap);
3676 return bmap;
3677 error:
3678 isl_basic_map_free(like);
3679 isl_basic_set_free(bset);
3680 return NULL;
3683 struct isl_basic_set *isl_basic_set_from_underlying_set(
3684 struct isl_basic_set *bset, struct isl_basic_set *like)
3686 return (struct isl_basic_set *)
3687 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
3690 struct isl_set *isl_set_from_underlying_set(
3691 struct isl_set *set, struct isl_basic_set *like)
3693 int i;
3695 if (!set || !like)
3696 goto error;
3697 isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
3698 goto error);
3699 if (isl_dim_equal(set->dim, like->dim) && like->n_div == 0) {
3700 isl_basic_set_free(like);
3701 return set;
3703 set = isl_set_cow(set);
3704 if (!set)
3705 goto error;
3706 for (i = 0; i < set->n; ++i) {
3707 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
3708 isl_basic_set_copy(like));
3709 if (!set->p[i])
3710 goto error;
3712 isl_dim_free(set->dim);
3713 set->dim = isl_dim_copy(like->dim);
3714 if (!set->dim)
3715 goto error;
3716 isl_basic_set_free(like);
3717 return set;
3718 error:
3719 isl_basic_set_free(like);
3720 isl_set_free(set);
3721 return NULL;
3724 struct isl_set *isl_map_underlying_set(struct isl_map *map)
3726 int i;
3728 map = isl_map_cow(map);
3729 if (!map)
3730 return NULL;
3731 map->dim = isl_dim_cow(map->dim);
3732 if (!map->dim)
3733 goto error;
3735 for (i = 1; i < map->n; ++i)
3736 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
3737 goto error);
3738 for (i = 0; i < map->n; ++i) {
3739 map->p[i] = (struct isl_basic_map *)
3740 isl_basic_map_underlying_set(map->p[i]);
3741 if (!map->p[i])
3742 goto error;
3744 if (map->n == 0)
3745 map->dim = isl_dim_underlying(map->dim, 0);
3746 else {
3747 isl_dim_free(map->dim);
3748 map->dim = isl_dim_copy(map->p[0]->dim);
3750 if (!map->dim)
3751 goto error;
3752 return (struct isl_set *)map;
3753 error:
3754 isl_map_free(map);
3755 return NULL;
3758 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
3760 return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
3763 __isl_give isl_basic_map *isl_basic_map_reset_dim(
3764 __isl_take isl_basic_map *bmap, __isl_take isl_dim *dim)
3766 bmap = isl_basic_map_cow(bmap);
3767 if (!bmap || !dim)
3768 goto error;
3770 isl_dim_free(bmap->dim);
3771 bmap->dim = dim;
3773 bmap = isl_basic_map_finalize(bmap);
3775 return bmap;
3776 error:
3777 isl_basic_map_free(bmap);
3778 isl_dim_free(dim);
3779 return NULL;
3782 __isl_give isl_basic_set *isl_basic_set_reset_dim(
3783 __isl_take isl_basic_set *bset, __isl_take isl_dim *dim)
3785 return (isl_basic_set *)isl_basic_map_reset_dim((isl_basic_map *)bset,
3786 dim);
3789 __isl_give isl_map *isl_map_reset_dim(__isl_take isl_map *map,
3790 __isl_take isl_dim *dim)
3792 int i;
3794 map = isl_map_cow(map);
3795 if (!map || !dim)
3796 goto error;
3798 for (i = 0; i < map->n; ++i) {
3799 map->p[i] = isl_basic_map_reset_dim(map->p[i],
3800 isl_dim_copy(dim));
3801 if (!map->p[i])
3802 goto error;
3804 isl_dim_free(map->dim);
3805 map->dim = dim;
3807 return map;
3808 error:
3809 isl_map_free(map);
3810 isl_dim_free(dim);
3811 return NULL;
3814 __isl_give isl_set *isl_set_reset_dim(__isl_take isl_set *set,
3815 __isl_take isl_dim *dim)
3817 return (struct isl_set *) isl_map_reset_dim((struct isl_map *)set, dim);
3820 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
3822 isl_dim *dim;
3823 struct isl_basic_set *domain;
3824 unsigned n_in;
3825 unsigned n_out;
3827 if (!bmap)
3828 return NULL;
3829 dim = isl_dim_domain(isl_basic_map_get_dim(bmap));
3831 n_in = isl_basic_map_n_in(bmap);
3832 n_out = isl_basic_map_n_out(bmap);
3833 domain = isl_basic_set_from_basic_map(bmap);
3834 domain = isl_basic_set_project_out(domain, isl_dim_set, n_in, n_out);
3836 domain = isl_basic_set_reset_dim(domain, dim);
3838 return domain;
3841 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
3843 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
3846 __isl_give isl_basic_map *isl_basic_map_domain_map(
3847 __isl_take isl_basic_map *bmap)
3849 int i, k;
3850 isl_dim *dim;
3851 isl_basic_map *domain;
3852 isl_basic_set *bset;
3853 int nparam, n_in, n_out;
3854 unsigned total;
3856 nparam = isl_basic_map_dim(bmap, isl_dim_param);
3857 n_in = isl_basic_map_dim(bmap, isl_dim_in);
3858 n_out = isl_basic_map_dim(bmap, isl_dim_out);
3860 dim = isl_dim_from_range(isl_dim_domain(isl_basic_map_get_dim(bmap)));
3861 domain = isl_basic_map_universe(dim);
3863 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
3864 bmap = isl_basic_map_apply_range(bmap, domain);
3865 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
3867 total = isl_basic_map_total_dim(bmap);
3869 for (i = 0; i < n_in; ++i) {
3870 k = isl_basic_map_alloc_equality(bmap);
3871 if (k < 0)
3872 goto error;
3873 isl_seq_clr(bmap->eq[k], 1 + total);
3874 isl_int_set_si(bmap->eq[k][1 + nparam + i], -1);
3875 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
3878 bmap = isl_basic_map_gauss(bmap, NULL);
3879 return isl_basic_map_finalize(bmap);
3880 error:
3881 isl_basic_map_free(bmap);
3882 return NULL;
3885 __isl_give isl_basic_map *isl_basic_map_range_map(
3886 __isl_take isl_basic_map *bmap)
3888 int i, k;
3889 isl_dim *dim;
3890 isl_basic_map *range;
3891 isl_basic_set *bset;
3892 int nparam, n_in, n_out;
3893 unsigned total;
3895 nparam = isl_basic_map_dim(bmap, isl_dim_param);
3896 n_in = isl_basic_map_dim(bmap, isl_dim_in);
3897 n_out = isl_basic_map_dim(bmap, isl_dim_out);
3899 dim = isl_dim_from_range(isl_dim_range(isl_basic_map_get_dim(bmap)));
3900 range = isl_basic_map_universe(dim);
3902 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
3903 bmap = isl_basic_map_apply_range(bmap, range);
3904 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
3906 total = isl_basic_map_total_dim(bmap);
3908 for (i = 0; i < n_out; ++i) {
3909 k = isl_basic_map_alloc_equality(bmap);
3910 if (k < 0)
3911 goto error;
3912 isl_seq_clr(bmap->eq[k], 1 + total);
3913 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + i], -1);
3914 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
3917 bmap = isl_basic_map_gauss(bmap, NULL);
3918 return isl_basic_map_finalize(bmap);
3919 error:
3920 isl_basic_map_free(bmap);
3921 return NULL;
3924 struct isl_set *isl_map_range(struct isl_map *map)
3926 int i;
3927 struct isl_set *set;
3929 if (!map)
3930 goto error;
3931 if (isl_map_dim(map, isl_dim_in) == 0)
3932 return (isl_set *)map;
3934 map = isl_map_cow(map);
3935 if (!map)
3936 goto error;
3938 set = (struct isl_set *) map;
3939 set->dim = isl_dim_drop_inputs(set->dim, 0, set->dim->n_in);
3940 if (!set->dim)
3941 goto error;
3942 for (i = 0; i < map->n; ++i) {
3943 set->p[i] = isl_basic_map_range(map->p[i]);
3944 if (!set->p[i])
3945 goto error;
3947 ISL_F_CLR(set, ISL_MAP_DISJOINT);
3948 ISL_F_CLR(set, ISL_SET_NORMALIZED);
3949 return set;
3950 error:
3951 isl_map_free(map);
3952 return NULL;
3955 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
3957 int i;
3958 isl_dim *domain_dim;
3960 map = isl_map_cow(map);
3961 if (!map)
3962 return NULL;
3964 domain_dim = isl_dim_from_range(isl_dim_domain(isl_map_get_dim(map)));
3965 map->dim = isl_dim_from_domain(isl_dim_wrap(map->dim));
3966 map->dim = isl_dim_join(map->dim, domain_dim);
3967 if (!map->dim)
3968 goto error;
3969 for (i = 0; i < map->n; ++i) {
3970 map->p[i] = isl_basic_map_domain_map(map->p[i]);
3971 if (!map->p[i])
3972 goto error;
3974 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3975 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3976 return map;
3977 error:
3978 isl_map_free(map);
3979 return NULL;
3982 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
3984 int i;
3985 isl_dim *range_dim;
3987 map = isl_map_cow(map);
3988 if (!map)
3989 return NULL;
3991 range_dim = isl_dim_range(isl_map_get_dim(map));
3992 map->dim = isl_dim_from_domain(isl_dim_wrap(map->dim));
3993 map->dim = isl_dim_join(map->dim, range_dim);
3994 if (!map->dim)
3995 goto error;
3996 for (i = 0; i < map->n; ++i) {
3997 map->p[i] = isl_basic_map_range_map(map->p[i]);
3998 if (!map->p[i])
3999 goto error;
4001 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4002 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4003 return map;
4004 error:
4005 isl_map_free(map);
4006 return NULL;
4009 struct isl_map *isl_map_from_set(struct isl_set *set, struct isl_dim *dim)
4011 int i;
4012 struct isl_map *map = NULL;
4014 set = isl_set_cow(set);
4015 if (!set || !dim)
4016 goto error;
4017 isl_assert(set->ctx, isl_dim_compatible(set->dim, dim), goto error);
4018 map = (struct isl_map *)set;
4019 for (i = 0; i < set->n; ++i) {
4020 map->p[i] = isl_basic_map_from_basic_set(
4021 set->p[i], isl_dim_copy(dim));
4022 if (!map->p[i])
4023 goto error;
4025 isl_dim_free(map->dim);
4026 map->dim = dim;
4027 return map;
4028 error:
4029 isl_dim_free(dim);
4030 isl_set_free(set);
4031 return NULL;
4034 __isl_give isl_basic_map *isl_basic_map_from_domain(
4035 __isl_take isl_basic_set *bset)
4037 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
4040 __isl_give isl_basic_map *isl_basic_map_from_range(
4041 __isl_take isl_basic_set *bset)
4043 return (isl_basic_map *)bset;
4046 struct isl_map *isl_map_from_range(struct isl_set *set)
4048 return (struct isl_map *)set;
4051 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
4053 return isl_map_reverse(isl_map_from_range(set));
4056 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
4057 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
4059 return isl_basic_map_apply_range(isl_basic_map_from_domain(domain),
4060 isl_basic_map_from_range(range));
4063 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
4064 __isl_take isl_set *range)
4066 return isl_map_apply_range(isl_map_from_domain(domain),
4067 isl_map_from_range(range));
4070 struct isl_set *isl_set_from_map(struct isl_map *map)
4072 int i;
4073 struct isl_set *set = NULL;
4075 if (!map)
4076 return NULL;
4077 map = isl_map_cow(map);
4078 if (!map)
4079 return NULL;
4080 map->dim = isl_dim_as_set_dim(map->dim);
4081 if (!map->dim)
4082 goto error;
4083 set = (struct isl_set *)map;
4084 for (i = 0; i < map->n; ++i) {
4085 set->p[i] = isl_basic_set_from_basic_map(map->p[i]);
4086 if (!set->p[i])
4087 goto error;
4089 return set;
4090 error:
4091 isl_map_free(map);
4092 return NULL;
4095 struct isl_map *isl_map_alloc_dim(struct isl_dim *dim, int n, unsigned flags)
4097 struct isl_map *map;
4099 if (!dim)
4100 return NULL;
4101 isl_assert(dim->ctx, n >= 0, return NULL);
4102 map = isl_alloc(dim->ctx, struct isl_map,
4103 sizeof(struct isl_map) +
4104 (n - 1) * sizeof(struct isl_basic_map *));
4105 if (!map)
4106 goto error;
4108 map->ctx = dim->ctx;
4109 isl_ctx_ref(map->ctx);
4110 map->ref = 1;
4111 map->size = n;
4112 map->n = 0;
4113 map->dim = dim;
4114 map->flags = flags;
4115 return map;
4116 error:
4117 isl_dim_free(dim);
4118 return NULL;
4121 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
4122 unsigned nparam, unsigned in, unsigned out, int n,
4123 unsigned flags)
4125 struct isl_map *map;
4126 struct isl_dim *dims;
4128 dims = isl_dim_alloc(ctx, nparam, in, out);
4129 if (!dims)
4130 return NULL;
4132 map = isl_map_alloc_dim(dims, n, flags);
4133 return map;
4136 struct isl_basic_map *isl_basic_map_empty(struct isl_dim *dim)
4138 struct isl_basic_map *bmap;
4139 bmap = isl_basic_map_alloc_dim(dim, 0, 1, 0);
4140 bmap = isl_basic_map_set_to_empty(bmap);
4141 return bmap;
4144 struct isl_basic_set *isl_basic_set_empty(struct isl_dim *dim)
4146 struct isl_basic_set *bset;
4147 bset = isl_basic_set_alloc_dim(dim, 0, 1, 0);
4148 bset = isl_basic_set_set_to_empty(bset);
4149 return bset;
4152 struct isl_basic_map *isl_basic_map_empty_like(struct isl_basic_map *model)
4154 struct isl_basic_map *bmap;
4155 if (!model)
4156 return NULL;
4157 bmap = isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
4158 bmap = isl_basic_map_set_to_empty(bmap);
4159 return bmap;
4162 struct isl_basic_map *isl_basic_map_empty_like_map(struct isl_map *model)
4164 struct isl_basic_map *bmap;
4165 if (!model)
4166 return NULL;
4167 bmap = isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
4168 bmap = isl_basic_map_set_to_empty(bmap);
4169 return bmap;
4172 struct isl_basic_set *isl_basic_set_empty_like(struct isl_basic_set *model)
4174 struct isl_basic_set *bset;
4175 if (!model)
4176 return NULL;
4177 bset = isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
4178 bset = isl_basic_set_set_to_empty(bset);
4179 return bset;
4182 struct isl_basic_map *isl_basic_map_universe(struct isl_dim *dim)
4184 struct isl_basic_map *bmap;
4185 bmap = isl_basic_map_alloc_dim(dim, 0, 0, 0);
4186 bmap = isl_basic_map_finalize(bmap);
4187 return bmap;
4190 struct isl_basic_set *isl_basic_set_universe(struct isl_dim *dim)
4192 struct isl_basic_set *bset;
4193 bset = isl_basic_set_alloc_dim(dim, 0, 0, 0);
4194 bset = isl_basic_set_finalize(bset);
4195 return bset;
4198 __isl_give isl_basic_map *isl_basic_map_nat_universe(__isl_take isl_dim *dim)
4200 int i;
4201 unsigned total = isl_dim_total(dim);
4202 isl_basic_map *bmap;
4204 bmap= isl_basic_map_alloc_dim(dim, 0, 0, total);
4205 for (i = 0; i < total; ++i) {
4206 int k = isl_basic_map_alloc_inequality(bmap);
4207 if (k < 0)
4208 goto error;
4209 isl_seq_clr(bmap->ineq[k], 1 + total);
4210 isl_int_set_si(bmap->ineq[k][1 + i], 1);
4212 return bmap;
4213 error:
4214 isl_basic_map_free(bmap);
4215 return NULL;
4218 __isl_give isl_basic_set *isl_basic_set_nat_universe(__isl_take isl_dim *dim)
4220 return isl_basic_map_nat_universe(dim);
4223 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_dim *dim)
4225 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
4228 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_dim *dim)
4230 return isl_map_nat_universe(dim);
4233 __isl_give isl_basic_map *isl_basic_map_universe_like(
4234 __isl_keep isl_basic_map *model)
4236 if (!model)
4237 return NULL;
4238 return isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
4241 struct isl_basic_set *isl_basic_set_universe_like(struct isl_basic_set *model)
4243 if (!model)
4244 return NULL;
4245 return isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
4248 __isl_give isl_basic_set *isl_basic_set_universe_like_set(
4249 __isl_keep isl_set *model)
4251 if (!model)
4252 return NULL;
4253 return isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
4256 struct isl_map *isl_map_empty(struct isl_dim *dim)
4258 return isl_map_alloc_dim(dim, 0, ISL_MAP_DISJOINT);
4261 struct isl_map *isl_map_empty_like(struct isl_map *model)
4263 if (!model)
4264 return NULL;
4265 return isl_map_alloc_dim(isl_dim_copy(model->dim), 0, ISL_MAP_DISJOINT);
4268 struct isl_map *isl_map_empty_like_basic_map(struct isl_basic_map *model)
4270 if (!model)
4271 return NULL;
4272 return isl_map_alloc_dim(isl_dim_copy(model->dim), 0, ISL_MAP_DISJOINT);
4275 struct isl_set *isl_set_empty(struct isl_dim *dim)
4277 return isl_set_alloc_dim(dim, 0, ISL_MAP_DISJOINT);
4280 struct isl_set *isl_set_empty_like(struct isl_set *model)
4282 if (!model)
4283 return NULL;
4284 return isl_set_empty(isl_dim_copy(model->dim));
4287 struct isl_map *isl_map_universe(struct isl_dim *dim)
4289 struct isl_map *map;
4290 if (!dim)
4291 return NULL;
4292 map = isl_map_alloc_dim(isl_dim_copy(dim), 1, ISL_MAP_DISJOINT);
4293 map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
4294 return map;
4297 struct isl_set *isl_set_universe(struct isl_dim *dim)
4299 struct isl_set *set;
4300 if (!dim)
4301 return NULL;
4302 set = isl_set_alloc_dim(isl_dim_copy(dim), 1, ISL_MAP_DISJOINT);
4303 set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
4304 return set;
4307 __isl_give isl_set *isl_set_universe_like(__isl_keep isl_set *model)
4309 if (!model)
4310 return NULL;
4311 return isl_set_universe(isl_dim_copy(model->dim));
4314 struct isl_map *isl_map_dup(struct isl_map *map)
4316 int i;
4317 struct isl_map *dup;
4319 if (!map)
4320 return NULL;
4321 dup = isl_map_alloc_dim(isl_dim_copy(map->dim), map->n, map->flags);
4322 for (i = 0; i < map->n; ++i)
4323 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
4324 return dup;
4327 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
4328 __isl_take isl_basic_map *bmap)
4330 if (!bmap || !map)
4331 goto error;
4332 if (isl_basic_map_fast_is_empty(bmap)) {
4333 isl_basic_map_free(bmap);
4334 return map;
4336 isl_assert(map->ctx, isl_dim_equal(map->dim, bmap->dim), goto error);
4337 isl_assert(map->ctx, map->n < map->size, goto error);
4338 map->p[map->n] = bmap;
4339 map->n++;
4340 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4341 return map;
4342 error:
4343 if (map)
4344 isl_map_free(map);
4345 if (bmap)
4346 isl_basic_map_free(bmap);
4347 return NULL;
4350 void isl_map_free(struct isl_map *map)
4352 int i;
4354 if (!map)
4355 return;
4357 if (--map->ref > 0)
4358 return;
4360 isl_ctx_deref(map->ctx);
4361 for (i = 0; i < map->n; ++i)
4362 isl_basic_map_free(map->p[i]);
4363 isl_dim_free(map->dim);
4364 free(map);
4367 struct isl_map *isl_map_extend(struct isl_map *base,
4368 unsigned nparam, unsigned n_in, unsigned n_out)
4370 int i;
4372 base = isl_map_cow(base);
4373 if (!base)
4374 return NULL;
4376 base->dim = isl_dim_extend(base->dim, nparam, n_in, n_out);
4377 if (!base->dim)
4378 goto error;
4379 for (i = 0; i < base->n; ++i) {
4380 base->p[i] = isl_basic_map_extend_dim(base->p[i],
4381 isl_dim_copy(base->dim), 0, 0, 0);
4382 if (!base->p[i])
4383 goto error;
4385 return base;
4386 error:
4387 isl_map_free(base);
4388 return NULL;
4391 struct isl_set *isl_set_extend(struct isl_set *base,
4392 unsigned nparam, unsigned dim)
4394 return (struct isl_set *)isl_map_extend((struct isl_map *)base,
4395 nparam, 0, dim);
4398 static struct isl_basic_map *isl_basic_map_fix_pos_si(
4399 struct isl_basic_map *bmap, unsigned pos, int value)
4401 int j;
4403 bmap = isl_basic_map_cow(bmap);
4404 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
4405 j = isl_basic_map_alloc_equality(bmap);
4406 if (j < 0)
4407 goto error;
4408 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
4409 isl_int_set_si(bmap->eq[j][pos], -1);
4410 isl_int_set_si(bmap->eq[j][0], value);
4411 bmap = isl_basic_map_simplify(bmap);
4412 return isl_basic_map_finalize(bmap);
4413 error:
4414 isl_basic_map_free(bmap);
4415 return NULL;
4418 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
4419 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
4421 int j;
4423 bmap = isl_basic_map_cow(bmap);
4424 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
4425 j = isl_basic_map_alloc_equality(bmap);
4426 if (j < 0)
4427 goto error;
4428 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
4429 isl_int_set_si(bmap->eq[j][pos], -1);
4430 isl_int_set(bmap->eq[j][0], value);
4431 bmap = isl_basic_map_simplify(bmap);
4432 return isl_basic_map_finalize(bmap);
4433 error:
4434 isl_basic_map_free(bmap);
4435 return NULL;
4438 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
4439 enum isl_dim_type type, unsigned pos, int value)
4441 if (!bmap)
4442 return NULL;
4443 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
4444 return isl_basic_map_fix_pos_si(bmap,
4445 isl_basic_map_offset(bmap, type) + pos, value);
4446 error:
4447 isl_basic_map_free(bmap);
4448 return NULL;
4451 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
4452 enum isl_dim_type type, unsigned pos, isl_int value)
4454 if (!bmap)
4455 return NULL;
4456 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
4457 return isl_basic_map_fix_pos(bmap,
4458 isl_basic_map_offset(bmap, type) + pos, value);
4459 error:
4460 isl_basic_map_free(bmap);
4461 return NULL;
4464 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
4465 enum isl_dim_type type, unsigned pos, int value)
4467 return (struct isl_basic_set *)
4468 isl_basic_map_fix_si((struct isl_basic_map *)bset,
4469 type, pos, value);
4472 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
4473 enum isl_dim_type type, unsigned pos, isl_int value)
4475 return (struct isl_basic_set *)
4476 isl_basic_map_fix((struct isl_basic_map *)bset,
4477 type, pos, value);
4480 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
4481 unsigned input, int value)
4483 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
4486 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
4487 unsigned dim, int value)
4489 return (struct isl_basic_set *)
4490 isl_basic_map_fix_si((struct isl_basic_map *)bset,
4491 isl_dim_set, dim, value);
4494 struct isl_map *isl_map_fix_si(struct isl_map *map,
4495 enum isl_dim_type type, unsigned pos, int value)
4497 int i;
4499 map = isl_map_cow(map);
4500 if (!map)
4501 return NULL;
4503 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
4504 for (i = 0; i < map->n; ++i) {
4505 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
4506 if (!map->p[i])
4507 goto error;
4509 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4510 return map;
4511 error:
4512 isl_map_free(map);
4513 return NULL;
4516 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
4517 enum isl_dim_type type, unsigned pos, int value)
4519 return (struct isl_set *)
4520 isl_map_fix_si((struct isl_map *)set, type, pos, value);
4523 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
4524 enum isl_dim_type type, unsigned pos, isl_int value)
4526 int i;
4528 map = isl_map_cow(map);
4529 if (!map)
4530 return NULL;
4532 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
4533 for (i = 0; i < map->n; ++i) {
4534 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
4535 if (!map->p[i])
4536 goto error;
4538 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4539 return map;
4540 error:
4541 isl_map_free(map);
4542 return NULL;
4545 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
4546 enum isl_dim_type type, unsigned pos, isl_int value)
4548 return (struct isl_set *)isl_map_fix((isl_map *)set, type, pos, value);
4551 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
4552 unsigned input, int value)
4554 return isl_map_fix_si(map, isl_dim_in, input, value);
4557 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
4559 return (struct isl_set *)
4560 isl_map_fix_si((struct isl_map *)set, isl_dim_set, dim, value);
4563 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
4564 __isl_take isl_basic_map *bmap,
4565 enum isl_dim_type type, unsigned pos, int value)
4567 int j;
4569 if (!bmap)
4570 return NULL;
4571 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
4572 pos += isl_basic_map_offset(bmap, type);
4573 bmap = isl_basic_map_cow(bmap);
4574 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
4575 j = isl_basic_map_alloc_inequality(bmap);
4576 if (j < 0)
4577 goto error;
4578 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
4579 isl_int_set_si(bmap->ineq[j][pos], 1);
4580 isl_int_set_si(bmap->ineq[j][0], -value);
4581 bmap = isl_basic_map_simplify(bmap);
4582 return isl_basic_map_finalize(bmap);
4583 error:
4584 isl_basic_map_free(bmap);
4585 return NULL;
4588 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
4589 unsigned dim, isl_int value)
4591 int j;
4593 bset = isl_basic_set_cow(bset);
4594 bset = isl_basic_set_extend_constraints(bset, 0, 1);
4595 j = isl_basic_set_alloc_inequality(bset);
4596 if (j < 0)
4597 goto error;
4598 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
4599 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
4600 isl_int_neg(bset->ineq[j][0], value);
4601 bset = isl_basic_set_simplify(bset);
4602 return isl_basic_set_finalize(bset);
4603 error:
4604 isl_basic_set_free(bset);
4605 return NULL;
4608 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
4609 enum isl_dim_type type, unsigned pos, int value)
4611 int i;
4613 map = isl_map_cow(map);
4614 if (!map)
4615 return NULL;
4617 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
4618 for (i = 0; i < map->n; ++i) {
4619 map->p[i] = isl_basic_map_lower_bound_si(map->p[i],
4620 type, pos, value);
4621 if (!map->p[i])
4622 goto error;
4624 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4625 return map;
4626 error:
4627 isl_map_free(map);
4628 return NULL;
4631 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
4632 enum isl_dim_type type, unsigned pos, int value)
4634 return (struct isl_set *)
4635 isl_map_lower_bound_si((struct isl_map *)set, type, pos, value);
4638 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
4639 isl_int value)
4641 int i;
4643 set = isl_set_cow(set);
4644 if (!set)
4645 return NULL;
4647 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
4648 for (i = 0; i < set->n; ++i) {
4649 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
4650 if (!set->p[i])
4651 goto error;
4653 return set;
4654 error:
4655 isl_set_free(set);
4656 return NULL;
4659 struct isl_map *isl_map_reverse(struct isl_map *map)
4661 int i;
4663 map = isl_map_cow(map);
4664 if (!map)
4665 return NULL;
4667 map->dim = isl_dim_reverse(map->dim);
4668 if (!map->dim)
4669 goto error;
4670 for (i = 0; i < map->n; ++i) {
4671 map->p[i] = isl_basic_map_reverse(map->p[i]);
4672 if (!map->p[i])
4673 goto error;
4675 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4676 return map;
4677 error:
4678 isl_map_free(map);
4679 return NULL;
4682 static struct isl_map *isl_basic_map_partial_lexopt(
4683 struct isl_basic_map *bmap, struct isl_basic_set *dom,
4684 struct isl_set **empty, int max)
4686 if (!bmap)
4687 goto error;
4688 if (bmap->ctx->opt->pip == ISL_PIP_PIP)
4689 return isl_pip_basic_map_lexopt(bmap, dom, empty, max);
4690 else
4691 return isl_tab_basic_map_partial_lexopt(bmap, dom, empty, max);
4692 error:
4693 isl_basic_map_free(bmap);
4694 isl_basic_set_free(dom);
4695 if (empty)
4696 *empty = NULL;
4697 return NULL;
4700 struct isl_map *isl_basic_map_partial_lexmax(
4701 struct isl_basic_map *bmap, struct isl_basic_set *dom,
4702 struct isl_set **empty)
4704 return isl_basic_map_partial_lexopt(bmap, dom, empty, 1);
4707 struct isl_map *isl_basic_map_partial_lexmin(
4708 struct isl_basic_map *bmap, struct isl_basic_set *dom,
4709 struct isl_set **empty)
4711 return isl_basic_map_partial_lexopt(bmap, dom, empty, 0);
4714 struct isl_set *isl_basic_set_partial_lexmin(
4715 struct isl_basic_set *bset, struct isl_basic_set *dom,
4716 struct isl_set **empty)
4718 return (struct isl_set *)
4719 isl_basic_map_partial_lexmin((struct isl_basic_map *)bset,
4720 dom, empty);
4723 struct isl_set *isl_basic_set_partial_lexmax(
4724 struct isl_basic_set *bset, struct isl_basic_set *dom,
4725 struct isl_set **empty)
4727 return (struct isl_set *)
4728 isl_basic_map_partial_lexmax((struct isl_basic_map *)bset,
4729 dom, empty);
4732 /* Given a basic map "bmap", compute the lexicographically minimal
4733 * (or maximal) image element for each domain element in dom.
4734 * Set *empty to those elements in dom that do not have an image element.
4736 * We first make sure the basic sets in dom are disjoint and then
4737 * simply collect the results over each of the basic sets separately.
4738 * We could probably improve the efficiency a bit by moving the union
4739 * domain down into the parametric integer programming.
4741 static __isl_give isl_map *basic_map_partial_lexopt(
4742 __isl_take isl_basic_map *bmap, __isl_take isl_set *dom,
4743 __isl_give isl_set **empty, int max)
4745 int i;
4746 struct isl_map *res;
4748 dom = isl_set_make_disjoint(dom);
4749 if (!dom)
4750 goto error;
4752 if (isl_set_fast_is_empty(dom)) {
4753 res = isl_map_empty_like_basic_map(bmap);
4754 *empty = isl_set_empty_like(dom);
4755 isl_set_free(dom);
4756 isl_basic_map_free(bmap);
4757 return res;
4760 res = isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap),
4761 isl_basic_set_copy(dom->p[0]), empty, max);
4763 for (i = 1; i < dom->n; ++i) {
4764 struct isl_map *res_i;
4765 struct isl_set *empty_i;
4767 res_i = isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap),
4768 isl_basic_set_copy(dom->p[i]), &empty_i, max);
4770 res = isl_map_union_disjoint(res, res_i);
4771 *empty = isl_set_union_disjoint(*empty, empty_i);
4774 isl_set_free(dom);
4775 isl_basic_map_free(bmap);
4776 return res;
4777 error:
4778 *empty = NULL;
4779 isl_set_free(dom);
4780 isl_basic_map_free(bmap);
4781 return NULL;
4784 /* Given a map "map", compute the lexicographically minimal
4785 * (or maximal) image element for each domain element in dom.
4786 * Set *empty to those elements in dom that do not have an image element.
4788 * We first compute the lexicographically minimal or maximal element
4789 * in the first basic map. This results in a partial solution "res"
4790 * and a subset "todo" of dom that still need to be handled.
4791 * We then consider each of the remaining maps in "map" and successively
4792 * improve both "res" and "todo".
4794 * Let res^k and todo^k be the results after k steps and let i = k + 1.
4795 * Assume we are computing the lexicographical maximum.
4796 * We first compute the lexicographically maximal element in basic map i.
4797 * This results in a partial solution res_i and a subset todo_i.
4798 * Then we combine these results with those obtain for the first k basic maps
4799 * to obtain a result that is valid for the first k+1 basic maps.
4800 * In particular, the set where there is no solution is the set where
4801 * there is no solution for the first k basic maps and also no solution
4802 * for the ith basic map, i.e.,
4804 * todo^i = todo^k * todo_i
4806 * On dom(res^k) * dom(res_i), we need to pick the larger of the two
4807 * solutions, arbitrarily breaking ties in favor of res^k.
4808 * That is, when res^k(a) >= res_i(a), we pick res^k and
4809 * when res^k(a) < res_i(a), we pick res_i. (Here, ">=" and "<" denote
4810 * the lexicographic order.)
4811 * In practice, we compute
4813 * res^k * (res_i . "<=")
4815 * and
4817 * res_i * (res^k . "<")
4819 * Finally, we consider the symmetric difference of dom(res^k) and dom(res_i),
4820 * where only one of res^k and res_i provides a solution and we simply pick
4821 * that one, i.e.,
4823 * res^k * todo_i
4824 * and
4825 * res_i * todo^k
4827 * Note that we only compute these intersections when dom(res^k) intersects
4828 * dom(res_i). Otherwise, the only effect of these intersections is to
4829 * potentially break up res^k and res_i into smaller pieces.
4830 * We want to avoid such splintering as much as possible.
4831 * In fact, an earlier implementation of this function would look for
4832 * better results in the domain of res^k and for extra results in todo^k,
4833 * but this would always result in a splintering according to todo^k,
4834 * even when the domain of basic map i is disjoint from the domains of
4835 * the previous basic maps.
4837 static __isl_give isl_map *isl_map_partial_lexopt(
4838 __isl_take isl_map *map, __isl_take isl_set *dom,
4839 __isl_give isl_set **empty, int max)
4841 int i;
4842 struct isl_map *res;
4843 struct isl_set *todo;
4845 if (!map || !dom)
4846 goto error;
4848 if (isl_map_fast_is_empty(map)) {
4849 if (empty)
4850 *empty = dom;
4851 else
4852 isl_set_free(dom);
4853 return map;
4856 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
4857 isl_set_copy(dom), &todo, max);
4859 for (i = 1; i < map->n; ++i) {
4860 isl_map *lt, *le;
4861 isl_map *res_i;
4862 isl_set *todo_i;
4863 isl_dim *dim = isl_dim_range(isl_map_get_dim(res));
4865 res_i = basic_map_partial_lexopt(isl_basic_map_copy(map->p[i]),
4866 isl_set_copy(dom), &todo_i, max);
4868 if (max) {
4869 lt = isl_map_lex_lt(isl_dim_copy(dim));
4870 le = isl_map_lex_le(dim);
4871 } else {
4872 lt = isl_map_lex_gt(isl_dim_copy(dim));
4873 le = isl_map_lex_ge(dim);
4875 lt = isl_map_apply_range(isl_map_copy(res), lt);
4876 lt = isl_map_intersect(lt, isl_map_copy(res_i));
4877 le = isl_map_apply_range(isl_map_copy(res_i), le);
4878 le = isl_map_intersect(le, isl_map_copy(res));
4880 if (!isl_map_is_empty(lt) || !isl_map_is_empty(le)) {
4881 res = isl_map_intersect_domain(res,
4882 isl_set_copy(todo_i));
4883 res_i = isl_map_intersect_domain(res_i,
4884 isl_set_copy(todo));
4887 res = isl_map_union_disjoint(res, res_i);
4888 res = isl_map_union_disjoint(res, lt);
4889 res = isl_map_union_disjoint(res, le);
4891 todo = isl_set_intersect(todo, todo_i);
4894 isl_set_free(dom);
4895 isl_map_free(map);
4897 if (empty)
4898 *empty = todo;
4899 else
4900 isl_set_free(todo);
4902 return res;
4903 error:
4904 if (empty)
4905 *empty = NULL;
4906 isl_set_free(dom);
4907 isl_map_free(map);
4908 return NULL;
4911 __isl_give isl_map *isl_map_partial_lexmax(
4912 __isl_take isl_map *map, __isl_take isl_set *dom,
4913 __isl_give isl_set **empty)
4915 return isl_map_partial_lexopt(map, dom, empty, 1);
4918 __isl_give isl_map *isl_map_partial_lexmin(
4919 __isl_take isl_map *map, __isl_take isl_set *dom,
4920 __isl_give isl_set **empty)
4922 return isl_map_partial_lexopt(map, dom, empty, 0);
4925 __isl_give isl_set *isl_set_partial_lexmin(
4926 __isl_take isl_set *set, __isl_take isl_set *dom,
4927 __isl_give isl_set **empty)
4929 return (struct isl_set *)
4930 isl_map_partial_lexmin((struct isl_map *)set,
4931 dom, empty);
4934 __isl_give isl_set *isl_set_partial_lexmax(
4935 __isl_take isl_set *set, __isl_take isl_set *dom,
4936 __isl_give isl_set **empty)
4938 return (struct isl_set *)
4939 isl_map_partial_lexmax((struct isl_map *)set,
4940 dom, empty);
4943 __isl_give isl_map *isl_basic_map_lexopt(__isl_take isl_basic_map *bmap, int max)
4945 struct isl_basic_set *dom = NULL;
4946 struct isl_dim *dom_dim;
4948 if (!bmap)
4949 goto error;
4950 dom_dim = isl_dim_domain(isl_dim_copy(bmap->dim));
4951 dom = isl_basic_set_universe(dom_dim);
4952 return isl_basic_map_partial_lexopt(bmap, dom, NULL, max);
4953 error:
4954 isl_basic_map_free(bmap);
4955 return NULL;
4958 __isl_give isl_map *isl_basic_map_lexmin(__isl_take isl_basic_map *bmap)
4960 return isl_basic_map_lexopt(bmap, 0);
4963 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
4965 return isl_basic_map_lexopt(bmap, 1);
4968 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
4970 return (isl_set *)isl_basic_map_lexmin((isl_basic_map *)bset);
4973 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
4975 return (isl_set *)isl_basic_map_lexmax((isl_basic_map *)bset);
4978 __isl_give isl_map *isl_map_lexopt(__isl_take isl_map *map, int max)
4980 struct isl_set *dom = NULL;
4981 struct isl_dim *dom_dim;
4983 if (!map)
4984 goto error;
4985 dom_dim = isl_dim_domain(isl_dim_copy(map->dim));
4986 dom = isl_set_universe(dom_dim);
4987 return isl_map_partial_lexopt(map, dom, NULL, max);
4988 error:
4989 isl_map_free(map);
4990 return NULL;
4993 __isl_give isl_map *isl_map_lexmin(__isl_take isl_map *map)
4995 return isl_map_lexopt(map, 0);
4998 __isl_give isl_map *isl_map_lexmax(__isl_take isl_map *map)
5000 return isl_map_lexopt(map, 1);
5003 __isl_give isl_set *isl_set_lexmin(__isl_take isl_set *set)
5005 return (isl_set *)isl_map_lexmin((isl_map *)set);
5008 __isl_give isl_set *isl_set_lexmax(__isl_take isl_set *set)
5010 return (isl_set *)isl_map_lexmax((isl_map *)set);
5013 /* Apply a preimage specified by "mat" on the parameters of "bset".
5014 * bset is assumed to have only parameters and divs.
5016 static struct isl_basic_set *basic_set_parameter_preimage(
5017 struct isl_basic_set *bset, struct isl_mat *mat)
5019 unsigned nparam;
5021 if (!bset || !mat)
5022 goto error;
5024 bset->dim = isl_dim_cow(bset->dim);
5025 if (!bset->dim)
5026 goto error;
5028 nparam = isl_basic_set_dim(bset, isl_dim_param);
5030 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
5032 bset->dim->nparam = 0;
5033 bset->dim->n_out = nparam;
5034 bset = isl_basic_set_preimage(bset, mat);
5035 if (bset) {
5036 bset->dim->nparam = bset->dim->n_out;
5037 bset->dim->n_out = 0;
5039 return bset;
5040 error:
5041 isl_mat_free(mat);
5042 isl_basic_set_free(bset);
5043 return NULL;
5046 /* Apply a preimage specified by "mat" on the parameters of "set".
5047 * set is assumed to have only parameters and divs.
5049 static struct isl_set *set_parameter_preimage(
5050 struct isl_set *set, struct isl_mat *mat)
5052 struct isl_dim *dim = NULL;
5053 unsigned nparam;
5055 if (!set || !mat)
5056 goto error;
5058 dim = isl_dim_copy(set->dim);
5059 dim = isl_dim_cow(dim);
5060 if (!dim)
5061 goto error;
5063 nparam = isl_set_dim(set, isl_dim_param);
5065 isl_assert(set->ctx, mat->n_row == 1 + nparam, goto error);
5067 dim->nparam = 0;
5068 dim->n_out = nparam;
5069 isl_set_reset_dim(set, dim);
5070 set = isl_set_preimage(set, mat);
5071 if (!set)
5072 goto error2;
5073 dim = isl_dim_copy(set->dim);
5074 dim = isl_dim_cow(dim);
5075 if (!dim)
5076 goto error2;
5077 dim->nparam = dim->n_out;
5078 dim->n_out = 0;
5079 isl_set_reset_dim(set, dim);
5080 return set;
5081 error:
5082 isl_dim_free(dim);
5083 isl_mat_free(mat);
5084 error2:
5085 isl_set_free(set);
5086 return NULL;
5089 /* Intersect the basic set "bset" with the affine space specified by the
5090 * equalities in "eq".
5092 static struct isl_basic_set *basic_set_append_equalities(
5093 struct isl_basic_set *bset, struct isl_mat *eq)
5095 int i, k;
5096 unsigned len;
5098 if (!bset || !eq)
5099 goto error;
5101 bset = isl_basic_set_extend_dim(bset, isl_dim_copy(bset->dim), 0,
5102 eq->n_row, 0);
5103 if (!bset)
5104 goto error;
5106 len = 1 + isl_dim_total(bset->dim) + bset->extra;
5107 for (i = 0; i < eq->n_row; ++i) {
5108 k = isl_basic_set_alloc_equality(bset);
5109 if (k < 0)
5110 goto error;
5111 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
5112 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
5114 isl_mat_free(eq);
5116 bset = isl_basic_set_gauss(bset, NULL);
5117 bset = isl_basic_set_finalize(bset);
5119 return bset;
5120 error:
5121 isl_mat_free(eq);
5122 isl_basic_set_free(bset);
5123 return NULL;
5126 /* Intersect the set "set" with the affine space specified by the
5127 * equalities in "eq".
5129 static struct isl_set *set_append_equalities(struct isl_set *set,
5130 struct isl_mat *eq)
5132 int i;
5134 if (!set || !eq)
5135 goto error;
5137 for (i = 0; i < set->n; ++i) {
5138 set->p[i] = basic_set_append_equalities(set->p[i],
5139 isl_mat_copy(eq));
5140 if (!set->p[i])
5141 goto error;
5143 isl_mat_free(eq);
5144 return set;
5145 error:
5146 isl_mat_free(eq);
5147 isl_set_free(set);
5148 return NULL;
5151 /* Project the given basic set onto its parameter domain, possibly introducing
5152 * new, explicit, existential variables in the constraints.
5153 * The input has parameters and (possibly implicit) existential variables.
5154 * The output has the same parameters, but only
5155 * explicit existentially quantified variables.
5157 * The actual projection is performed by pip, but pip doesn't seem
5158 * to like equalities very much, so we first remove the equalities
5159 * among the parameters by performing a variable compression on
5160 * the parameters. Afterward, an inverse transformation is performed
5161 * and the equalities among the parameters are inserted back in.
5163 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
5165 int i, j;
5166 struct isl_mat *eq;
5167 struct isl_mat *T, *T2;
5168 struct isl_set *set;
5169 unsigned nparam, n_div;
5171 bset = isl_basic_set_cow(bset);
5172 if (!bset)
5173 return NULL;
5175 if (bset->n_eq == 0)
5176 return isl_basic_set_lexmin(bset);
5178 isl_basic_set_gauss(bset, NULL);
5180 nparam = isl_basic_set_dim(bset, isl_dim_param);
5181 n_div = isl_basic_set_dim(bset, isl_dim_div);
5183 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
5184 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
5185 ++i;
5187 if (i == bset->n_eq)
5188 return isl_basic_set_lexmin(bset);
5190 eq = isl_mat_sub_alloc(bset->ctx, bset->eq, i, bset->n_eq - i,
5191 0, 1 + nparam);
5192 eq = isl_mat_cow(eq);
5193 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
5194 if (T && T->n_col == 0) {
5195 isl_mat_free(T);
5196 isl_mat_free(T2);
5197 isl_mat_free(eq);
5198 bset = isl_basic_set_set_to_empty(bset);
5199 return isl_set_from_basic_set(bset);
5201 bset = basic_set_parameter_preimage(bset, T);
5203 set = isl_basic_set_lexmin(bset);
5204 set = set_parameter_preimage(set, T2);
5205 set = set_append_equalities(set, eq);
5206 return set;
5209 /* Compute an explicit representation for all the existentially
5210 * quantified variables.
5211 * The input and output dimensions are first turned into parameters.
5212 * compute_divs then returns a map with the same parameters and
5213 * no input or output dimensions and the dimension specification
5214 * is reset to that of the input.
5216 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
5218 struct isl_basic_set *bset;
5219 struct isl_set *set;
5220 struct isl_map *map;
5221 struct isl_dim *dim, *orig_dim = NULL;
5222 unsigned nparam;
5223 unsigned n_in;
5224 unsigned n_out;
5226 bmap = isl_basic_map_cow(bmap);
5227 if (!bmap)
5228 return NULL;
5230 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5231 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5232 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5233 dim = isl_dim_set_alloc(bmap->ctx, nparam + n_in + n_out, 0);
5234 if (!dim)
5235 goto error;
5237 orig_dim = bmap->dim;
5238 bmap->dim = dim;
5239 bset = (struct isl_basic_set *)bmap;
5241 set = parameter_compute_divs(bset);
5242 map = (struct isl_map *)set;
5243 map = isl_map_reset_dim(map, orig_dim);
5245 return map;
5246 error:
5247 isl_basic_map_free(bmap);
5248 return NULL;
5251 static int basic_map_divs_known(__isl_keep isl_basic_map *bmap)
5253 int i;
5254 unsigned off;
5256 if (!bmap)
5257 return -1;
5259 off = isl_dim_total(bmap->dim);
5260 for (i = 0; i < bmap->n_div; ++i) {
5261 if (isl_int_is_zero(bmap->div[i][0]))
5262 return 0;
5263 isl_assert(bmap->ctx, isl_int_is_zero(bmap->div[i][1+1+off+i]),
5264 return -1);
5266 return 1;
5269 static int map_divs_known(__isl_keep isl_map *map)
5271 int i;
5273 if (!map)
5274 return -1;
5276 for (i = 0; i < map->n; ++i) {
5277 int known = basic_map_divs_known(map->p[i]);
5278 if (known <= 0)
5279 return known;
5282 return 1;
5285 /* If bmap contains any unknown divs, then compute explicit
5286 * expressions for them. However, this computation may be
5287 * quite expensive, so first try to remove divs that aren't
5288 * strictly needed.
5290 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
5292 int i;
5293 int known;
5294 struct isl_map *map;
5296 known = basic_map_divs_known(bmap);
5297 if (known < 0)
5298 goto error;
5299 if (known)
5300 return isl_map_from_basic_map(bmap);
5302 bmap = isl_basic_map_drop_redundant_divs(bmap);
5304 known = basic_map_divs_known(bmap);
5305 if (known < 0)
5306 goto error;
5307 if (known)
5308 return isl_map_from_basic_map(bmap);
5310 map = compute_divs(bmap);
5311 return map;
5312 error:
5313 isl_basic_map_free(bmap);
5314 return NULL;
5317 struct isl_map *isl_map_compute_divs(struct isl_map *map)
5319 int i;
5320 int known;
5321 struct isl_map *res;
5323 if (!map)
5324 return NULL;
5325 if (map->n == 0)
5326 return map;
5328 known = map_divs_known(map);
5329 if (known < 0) {
5330 isl_map_free(map);
5331 return NULL;
5333 if (known)
5334 return map;
5336 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
5337 for (i = 1 ; i < map->n; ++i) {
5338 struct isl_map *r2;
5339 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
5340 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
5341 res = isl_map_union_disjoint(res, r2);
5342 else
5343 res = isl_map_union(res, r2);
5345 isl_map_free(map);
5347 return res;
5350 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
5352 return (struct isl_set *)
5353 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
5356 struct isl_set *isl_set_compute_divs(struct isl_set *set)
5358 return (struct isl_set *)
5359 isl_map_compute_divs((struct isl_map *)set);
5362 struct isl_set *isl_map_domain(struct isl_map *map)
5364 int i;
5365 struct isl_set *set;
5367 if (!map)
5368 goto error;
5370 map = isl_map_cow(map);
5371 if (!map)
5372 return NULL;
5374 set = (struct isl_set *)map;
5375 set->dim = isl_dim_domain(set->dim);
5376 if (!set->dim)
5377 goto error;
5378 for (i = 0; i < map->n; ++i) {
5379 set->p[i] = isl_basic_map_domain(map->p[i]);
5380 if (!set->p[i])
5381 goto error;
5383 ISL_F_CLR(set, ISL_MAP_DISJOINT);
5384 ISL_F_CLR(set, ISL_SET_NORMALIZED);
5385 return set;
5386 error:
5387 isl_map_free(map);
5388 return NULL;
5391 struct isl_map *isl_map_union_disjoint(
5392 struct isl_map *map1, struct isl_map *map2)
5394 int i;
5395 unsigned flags = 0;
5396 struct isl_map *map = NULL;
5398 if (!map1 || !map2)
5399 goto error;
5401 if (map1->n == 0) {
5402 isl_map_free(map1);
5403 return map2;
5405 if (map2->n == 0) {
5406 isl_map_free(map2);
5407 return map1;
5410 isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
5412 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
5413 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
5414 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
5416 map = isl_map_alloc_dim(isl_dim_copy(map1->dim),
5417 map1->n + map2->n, flags);
5418 if (!map)
5419 goto error;
5420 for (i = 0; i < map1->n; ++i) {
5421 map = isl_map_add_basic_map(map,
5422 isl_basic_map_copy(map1->p[i]));
5423 if (!map)
5424 goto error;
5426 for (i = 0; i < map2->n; ++i) {
5427 map = isl_map_add_basic_map(map,
5428 isl_basic_map_copy(map2->p[i]));
5429 if (!map)
5430 goto error;
5432 isl_map_free(map1);
5433 isl_map_free(map2);
5434 return map;
5435 error:
5436 isl_map_free(map);
5437 isl_map_free(map1);
5438 isl_map_free(map2);
5439 return NULL;
5442 struct isl_map *isl_map_union(struct isl_map *map1, struct isl_map *map2)
5444 map1 = isl_map_union_disjoint(map1, map2);
5445 if (!map1)
5446 return NULL;
5447 if (map1->n > 1)
5448 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
5449 return map1;
5452 struct isl_set *isl_set_union_disjoint(
5453 struct isl_set *set1, struct isl_set *set2)
5455 return (struct isl_set *)
5456 isl_map_union_disjoint(
5457 (struct isl_map *)set1, (struct isl_map *)set2);
5460 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
5462 return (struct isl_set *)
5463 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
5466 struct isl_map *isl_map_intersect_range(
5467 struct isl_map *map, struct isl_set *set)
5469 unsigned flags = 0;
5470 struct isl_map *result;
5471 int i, j;
5473 if (!map || !set)
5474 goto error;
5476 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
5477 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
5478 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
5480 result = isl_map_alloc_dim(isl_dim_copy(map->dim),
5481 map->n * set->n, flags);
5482 if (!result)
5483 goto error;
5484 for (i = 0; i < map->n; ++i)
5485 for (j = 0; j < set->n; ++j) {
5486 result = isl_map_add_basic_map(result,
5487 isl_basic_map_intersect_range(
5488 isl_basic_map_copy(map->p[i]),
5489 isl_basic_set_copy(set->p[j])));
5490 if (!result)
5491 goto error;
5493 isl_map_free(map);
5494 isl_set_free(set);
5495 return result;
5496 error:
5497 isl_map_free(map);
5498 isl_set_free(set);
5499 return NULL;
5502 struct isl_map *isl_map_intersect_domain(
5503 struct isl_map *map, struct isl_set *set)
5505 return isl_map_reverse(
5506 isl_map_intersect_range(isl_map_reverse(map), set));
5509 struct isl_map *isl_map_apply_domain(
5510 struct isl_map *map1, struct isl_map *map2)
5512 if (!map1 || !map2)
5513 goto error;
5514 map1 = isl_map_reverse(map1);
5515 map1 = isl_map_apply_range(map1, map2);
5516 return isl_map_reverse(map1);
5517 error:
5518 isl_map_free(map1);
5519 isl_map_free(map2);
5520 return NULL;
5523 struct isl_map *isl_map_apply_range(
5524 struct isl_map *map1, struct isl_map *map2)
5526 struct isl_dim *dim_result;
5527 struct isl_map *result;
5528 int i, j;
5530 if (!map1 || !map2)
5531 goto error;
5533 dim_result = isl_dim_join(isl_dim_copy(map1->dim),
5534 isl_dim_copy(map2->dim));
5536 result = isl_map_alloc_dim(dim_result, map1->n * map2->n, 0);
5537 if (!result)
5538 goto error;
5539 for (i = 0; i < map1->n; ++i)
5540 for (j = 0; j < map2->n; ++j) {
5541 result = isl_map_add_basic_map(result,
5542 isl_basic_map_apply_range(
5543 isl_basic_map_copy(map1->p[i]),
5544 isl_basic_map_copy(map2->p[j])));
5545 if (!result)
5546 goto error;
5548 isl_map_free(map1);
5549 isl_map_free(map2);
5550 if (result && result->n <= 1)
5551 ISL_F_SET(result, ISL_MAP_DISJOINT);
5552 return result;
5553 error:
5554 isl_map_free(map1);
5555 isl_map_free(map2);
5556 return NULL;
5560 * returns range - domain
5562 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
5564 isl_dim *dims, *target_dim;
5565 struct isl_basic_set *bset;
5566 unsigned dim;
5567 unsigned nparam;
5568 int i;
5570 if (!bmap)
5571 goto error;
5572 isl_assert(bmap->ctx, isl_dim_tuple_match(bmap->dim, isl_dim_in,
5573 bmap->dim, isl_dim_out),
5574 goto error);
5575 target_dim = isl_dim_domain(isl_basic_map_get_dim(bmap));
5576 dim = isl_basic_map_n_in(bmap);
5577 nparam = isl_basic_map_n_param(bmap);
5578 bset = isl_basic_set_from_basic_map(bmap);
5579 bset = isl_basic_set_cow(bset);
5580 dims = isl_basic_set_get_dim(bset);
5581 dims = isl_dim_add(dims, isl_dim_set, dim);
5582 bset = isl_basic_set_extend_dim(bset, dims, 0, dim, 0);
5583 bset = isl_basic_set_swap_vars(bset, 2*dim);
5584 for (i = 0; i < dim; ++i) {
5585 int j = isl_basic_map_alloc_equality(
5586 (struct isl_basic_map *)bset);
5587 if (j < 0)
5588 goto error;
5589 isl_seq_clr(bset->eq[j], 1 + isl_basic_set_total_dim(bset));
5590 isl_int_set_si(bset->eq[j][1+nparam+i], 1);
5591 isl_int_set_si(bset->eq[j][1+nparam+dim+i], 1);
5592 isl_int_set_si(bset->eq[j][1+nparam+2*dim+i], -1);
5594 bset = isl_basic_set_project_out(bset, isl_dim_set, dim, 2*dim);
5595 bset = isl_basic_set_reset_dim(bset, target_dim);
5596 return bset;
5597 error:
5598 isl_basic_map_free(bmap);
5599 return NULL;
5603 * returns range - domain
5605 struct isl_set *isl_map_deltas(struct isl_map *map)
5607 int i;
5608 isl_dim *dim;
5609 struct isl_set *result;
5611 if (!map)
5612 return NULL;
5614 isl_assert(map->ctx, isl_dim_tuple_match(map->dim, isl_dim_in,
5615 map->dim, isl_dim_out),
5616 goto error);
5617 dim = isl_map_get_dim(map);
5618 dim = isl_dim_domain(dim);
5619 result = isl_set_alloc_dim(dim, map->n, 0);
5620 if (!result)
5621 goto error;
5622 for (i = 0; i < map->n; ++i)
5623 result = isl_set_add_basic_set(result,
5624 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
5625 isl_map_free(map);
5626 return result;
5627 error:
5628 isl_map_free(map);
5629 return NULL;
5633 * returns [domain -> range] -> range - domain
5635 __isl_give isl_basic_map *isl_basic_map_deltas_map(
5636 __isl_take isl_basic_map *bmap)
5638 int i, k;
5639 isl_dim *dim;
5640 isl_basic_map *domain;
5641 isl_basic_set *bset;
5642 int nparam, n;
5643 unsigned total;
5645 if (!isl_dim_tuple_match(bmap->dim, isl_dim_in, bmap->dim, isl_dim_out))
5646 isl_die(bmap->ctx, isl_error_invalid,
5647 "domain and range don't match", goto error);
5649 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5650 n = isl_basic_map_dim(bmap, isl_dim_in);
5652 dim = isl_dim_from_range(isl_dim_domain(isl_basic_map_get_dim(bmap)));
5653 domain = isl_basic_map_universe(dim);
5655 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5656 bmap = isl_basic_map_apply_range(bmap, domain);
5657 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
5659 total = isl_basic_map_total_dim(bmap);
5661 for (i = 0; i < n; ++i) {
5662 k = isl_basic_map_alloc_equality(bmap);
5663 if (k < 0)
5664 goto error;
5665 isl_seq_clr(bmap->eq[k], 1 + total);
5666 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
5667 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
5668 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
5671 bmap = isl_basic_map_gauss(bmap, NULL);
5672 return isl_basic_map_finalize(bmap);
5673 error:
5674 isl_basic_map_free(bmap);
5675 return NULL;
5679 * returns [domain -> range] -> range - domain
5681 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
5683 int i;
5684 isl_dim *domain_dim;
5686 if (!map)
5687 return NULL;
5689 if (!isl_dim_tuple_match(map->dim, isl_dim_in, map->dim, isl_dim_out))
5690 isl_die(map->ctx, isl_error_invalid,
5691 "domain and range don't match", goto error);
5693 map = isl_map_cow(map);
5694 if (!map)
5695 return NULL;
5697 domain_dim = isl_dim_from_range(isl_dim_domain(isl_map_get_dim(map)));
5698 map->dim = isl_dim_from_domain(isl_dim_wrap(map->dim));
5699 map->dim = isl_dim_join(map->dim, domain_dim);
5700 if (!map->dim)
5701 goto error;
5702 for (i = 0; i < map->n; ++i) {
5703 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
5704 if (!map->p[i])
5705 goto error;
5707 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5708 return map;
5709 error:
5710 isl_map_free(map);
5711 return NULL;
5714 static struct isl_basic_map *basic_map_identity(struct isl_dim *dims)
5716 struct isl_basic_map *bmap;
5717 unsigned nparam;
5718 unsigned dim;
5719 int i;
5721 if (!dims)
5722 return NULL;
5724 nparam = dims->nparam;
5725 dim = dims->n_out;
5726 bmap = isl_basic_map_alloc_dim(dims, 0, dim, 0);
5727 if (!bmap)
5728 goto error;
5730 for (i = 0; i < dim; ++i) {
5731 int j = isl_basic_map_alloc_equality(bmap);
5732 if (j < 0)
5733 goto error;
5734 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
5735 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
5736 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
5738 return isl_basic_map_finalize(bmap);
5739 error:
5740 isl_basic_map_free(bmap);
5741 return NULL;
5744 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_dim *dim)
5746 if (!dim)
5747 return NULL;
5748 if (dim->n_in != dim->n_out)
5749 isl_die(dim->ctx, isl_error_invalid,
5750 "number of input and output dimensions needs to be "
5751 "the same", goto error);
5752 return basic_map_identity(dim);
5753 error:
5754 isl_dim_free(dim);
5755 return NULL;
5758 struct isl_basic_map *isl_basic_map_identity_like(struct isl_basic_map *model)
5760 if (!model || !model->dim)
5761 return NULL;
5762 return isl_basic_map_identity(isl_dim_copy(model->dim));
5765 __isl_give isl_map *isl_map_identity(__isl_take isl_dim *dim)
5767 return isl_map_from_basic_map(isl_basic_map_identity(dim));
5770 struct isl_map *isl_map_identity_like(struct isl_map *model)
5772 if (!model || !model->dim)
5773 return NULL;
5774 return isl_map_identity(isl_dim_copy(model->dim));
5777 struct isl_map *isl_map_identity_like_basic_map(struct isl_basic_map *model)
5779 if (!model || !model->dim)
5780 return NULL;
5781 return isl_map_identity(isl_dim_copy(model->dim));
5784 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
5786 isl_dim *dim = isl_set_get_dim(set);
5787 isl_map *id;
5788 id = isl_map_identity(isl_dim_map_from_set(dim));
5789 return isl_map_intersect_range(id, set);
5792 /* Construct a basic set with all set dimensions having only non-negative
5793 * values.
5795 struct isl_basic_set *isl_basic_set_positive_orthant(struct isl_dim *dims)
5797 int i;
5798 unsigned nparam;
5799 unsigned dim;
5800 struct isl_basic_set *bset;
5802 if (!dims)
5803 return NULL;
5804 nparam = dims->nparam;
5805 dim = dims->n_out;
5806 bset = isl_basic_set_alloc_dim(dims, 0, 0, dim);
5807 if (!bset)
5808 return NULL;
5809 for (i = 0; i < dim; ++i) {
5810 int k = isl_basic_set_alloc_inequality(bset);
5811 if (k < 0)
5812 goto error;
5813 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
5814 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
5816 return bset;
5817 error:
5818 isl_basic_set_free(bset);
5819 return NULL;
5822 /* Construct the half-space x_pos >= 0.
5824 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_dim *dim,
5825 int pos)
5827 int k;
5828 isl_basic_set *nonneg;
5830 nonneg = isl_basic_set_alloc_dim(dim, 0, 0, 1);
5831 k = isl_basic_set_alloc_inequality(nonneg);
5832 if (k < 0)
5833 goto error;
5834 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
5835 isl_int_set_si(nonneg->ineq[k][pos], 1);
5837 return isl_basic_set_finalize(nonneg);
5838 error:
5839 isl_basic_set_free(nonneg);
5840 return NULL;
5843 /* Construct the half-space x_pos <= -1.
5845 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_dim *dim, int pos)
5847 int k;
5848 isl_basic_set *neg;
5850 neg = isl_basic_set_alloc_dim(dim, 0, 0, 1);
5851 k = isl_basic_set_alloc_inequality(neg);
5852 if (k < 0)
5853 goto error;
5854 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
5855 isl_int_set_si(neg->ineq[k][0], -1);
5856 isl_int_set_si(neg->ineq[k][pos], -1);
5858 return isl_basic_set_finalize(neg);
5859 error:
5860 isl_basic_set_free(neg);
5861 return NULL;
5864 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
5865 enum isl_dim_type type, unsigned first, unsigned n)
5867 int i;
5868 isl_basic_set *nonneg;
5869 isl_basic_set *neg;
5871 if (!set)
5872 return NULL;
5873 if (n == 0)
5874 return set;
5876 isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
5878 for (i = 0; i < n; ++i) {
5879 nonneg = nonneg_halfspace(isl_set_get_dim(set),
5880 pos(set->dim, type) + first + i);
5881 neg = neg_halfspace(isl_set_get_dim(set),
5882 pos(set->dim, type) + first + i);
5884 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
5887 return set;
5888 error:
5889 isl_set_free(set);
5890 return NULL;
5893 static int foreach_orthant(__isl_take isl_set *set, int *signs, int first,
5894 int len, int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
5895 void *user)
5897 isl_set *half;
5899 if (!set)
5900 return -1;
5901 if (isl_set_fast_is_empty(set)) {
5902 isl_set_free(set);
5903 return 0;
5905 if (first == len)
5906 return fn(set, signs, user);
5908 signs[first] = 1;
5909 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_dim(set),
5910 1 + first));
5911 half = isl_set_intersect(half, isl_set_copy(set));
5912 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
5913 goto error;
5915 signs[first] = -1;
5916 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_dim(set),
5917 1 + first));
5918 half = isl_set_intersect(half, set);
5919 return foreach_orthant(half, signs, first + 1, len, fn, user);
5920 error:
5921 isl_set_free(set);
5922 return -1;
5925 /* Call "fn" on the intersections of "set" with each of the orthants
5926 * (except for obviously empty intersections). The orthant is identified
5927 * by the signs array, with each entry having value 1 or -1 according
5928 * to the sign of the corresponding variable.
5930 int isl_set_foreach_orthant(__isl_keep isl_set *set,
5931 int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
5932 void *user)
5934 unsigned nparam;
5935 unsigned nvar;
5936 int *signs;
5937 int r;
5939 if (!set)
5940 return -1;
5941 if (isl_set_fast_is_empty(set))
5942 return 0;
5944 nparam = isl_set_dim(set, isl_dim_param);
5945 nvar = isl_set_dim(set, isl_dim_set);
5947 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
5949 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
5950 fn, user);
5952 free(signs);
5954 return r;
5957 int isl_set_is_equal(struct isl_set *set1, struct isl_set *set2)
5959 return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
5962 int isl_basic_map_is_subset(
5963 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
5965 int is_subset;
5966 struct isl_map *map1;
5967 struct isl_map *map2;
5969 if (!bmap1 || !bmap2)
5970 return -1;
5972 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
5973 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
5975 is_subset = isl_map_is_subset(map1, map2);
5977 isl_map_free(map1);
5978 isl_map_free(map2);
5980 return is_subset;
5983 int isl_basic_map_is_equal(
5984 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
5986 int is_subset;
5988 if (!bmap1 || !bmap2)
5989 return -1;
5990 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
5991 if (is_subset != 1)
5992 return is_subset;
5993 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
5994 return is_subset;
5997 int isl_basic_set_is_equal(
5998 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
6000 return isl_basic_map_is_equal(
6001 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
6004 int isl_map_is_empty(struct isl_map *map)
6006 int i;
6007 int is_empty;
6009 if (!map)
6010 return -1;
6011 for (i = 0; i < map->n; ++i) {
6012 is_empty = isl_basic_map_is_empty(map->p[i]);
6013 if (is_empty < 0)
6014 return -1;
6015 if (!is_empty)
6016 return 0;
6018 return 1;
6021 int isl_map_fast_is_empty(struct isl_map *map)
6023 return map ? map->n == 0 : -1;
6026 int isl_set_fast_is_empty(struct isl_set *set)
6028 return set ? set->n == 0 : -1;
6031 int isl_set_is_empty(struct isl_set *set)
6033 return isl_map_is_empty((struct isl_map *)set);
6036 int isl_map_has_equal_dim(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
6038 if (!map1 || !map2)
6039 return -1;
6041 return isl_dim_equal(map1->dim, map2->dim);
6044 int isl_set_has_equal_dim(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
6046 if (!set1 || !set2)
6047 return -1;
6049 return isl_dim_equal(set1->dim, set2->dim);
6052 int isl_map_is_equal(struct isl_map *map1, struct isl_map *map2)
6054 int is_subset;
6056 if (!map1 || !map2)
6057 return -1;
6058 is_subset = isl_map_is_subset(map1, map2);
6059 if (is_subset != 1)
6060 return is_subset;
6061 is_subset = isl_map_is_subset(map2, map1);
6062 return is_subset;
6065 int isl_basic_map_is_strict_subset(
6066 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6068 int is_subset;
6070 if (!bmap1 || !bmap2)
6071 return -1;
6072 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
6073 if (is_subset != 1)
6074 return is_subset;
6075 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
6076 if (is_subset == -1)
6077 return is_subset;
6078 return !is_subset;
6081 int isl_map_is_strict_subset(struct isl_map *map1, struct isl_map *map2)
6083 int is_subset;
6085 if (!map1 || !map2)
6086 return -1;
6087 is_subset = isl_map_is_subset(map1, map2);
6088 if (is_subset != 1)
6089 return is_subset;
6090 is_subset = isl_map_is_subset(map2, map1);
6091 if (is_subset == -1)
6092 return is_subset;
6093 return !is_subset;
6096 int isl_set_is_strict_subset(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
6098 return isl_map_is_strict_subset((isl_map *)set1, (isl_map *)set2);
6101 int isl_basic_map_is_universe(struct isl_basic_map *bmap)
6103 if (!bmap)
6104 return -1;
6105 return bmap->n_eq == 0 && bmap->n_ineq == 0;
6108 int isl_basic_set_is_universe(struct isl_basic_set *bset)
6110 if (!bset)
6111 return -1;
6112 return bset->n_eq == 0 && bset->n_ineq == 0;
6115 int isl_map_fast_is_universe(__isl_keep isl_map *map)
6117 int i;
6119 if (!map)
6120 return -1;
6122 for (i = 0; i < map->n; ++i) {
6123 int r = isl_basic_map_is_universe(map->p[i]);
6124 if (r < 0 || r)
6125 return r;
6128 return 0;
6131 int isl_set_fast_is_universe(__isl_keep isl_set *set)
6133 return isl_map_fast_is_universe((isl_map *) set);
6136 int isl_basic_map_is_empty(struct isl_basic_map *bmap)
6138 struct isl_basic_set *bset = NULL;
6139 struct isl_vec *sample = NULL;
6140 int empty;
6141 unsigned total;
6143 if (!bmap)
6144 return -1;
6146 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
6147 return 1;
6149 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
6150 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
6151 copy = isl_basic_map_remove_redundancies(copy);
6152 empty = ISL_F_ISSET(copy, ISL_BASIC_MAP_EMPTY);
6153 isl_basic_map_free(copy);
6154 return empty;
6157 total = 1 + isl_basic_map_total_dim(bmap);
6158 if (bmap->sample && bmap->sample->size == total) {
6159 int contains = isl_basic_map_contains(bmap, bmap->sample);
6160 if (contains < 0)
6161 return -1;
6162 if (contains)
6163 return 0;
6165 isl_vec_free(bmap->sample);
6166 bmap->sample = NULL;
6167 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
6168 if (!bset)
6169 return -1;
6170 sample = isl_basic_set_sample_vec(bset);
6171 if (!sample)
6172 return -1;
6173 empty = sample->size == 0;
6174 isl_vec_free(bmap->sample);
6175 bmap->sample = sample;
6176 if (empty)
6177 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
6179 return empty;
6182 int isl_basic_map_fast_is_empty(struct isl_basic_map *bmap)
6184 if (!bmap)
6185 return -1;
6186 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
6189 int isl_basic_set_fast_is_empty(struct isl_basic_set *bset)
6191 if (!bset)
6192 return -1;
6193 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
6196 int isl_basic_set_is_empty(struct isl_basic_set *bset)
6198 return isl_basic_map_is_empty((struct isl_basic_map *)bset);
6201 struct isl_map *isl_basic_map_union(
6202 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6204 struct isl_map *map;
6205 if (!bmap1 || !bmap2)
6206 return NULL;
6208 isl_assert(bmap1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim), goto error);
6210 map = isl_map_alloc_dim(isl_dim_copy(bmap1->dim), 2, 0);
6211 if (!map)
6212 goto error;
6213 map = isl_map_add_basic_map(map, bmap1);
6214 map = isl_map_add_basic_map(map, bmap2);
6215 return map;
6216 error:
6217 isl_basic_map_free(bmap1);
6218 isl_basic_map_free(bmap2);
6219 return NULL;
6222 struct isl_set *isl_basic_set_union(
6223 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
6225 return (struct isl_set *)isl_basic_map_union(
6226 (struct isl_basic_map *)bset1,
6227 (struct isl_basic_map *)bset2);
6230 /* Order divs such that any div only depends on previous divs */
6231 struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
6233 int i;
6234 unsigned off;
6236 if (!bmap)
6237 return NULL;
6239 off = isl_dim_total(bmap->dim);
6241 for (i = 0; i < bmap->n_div; ++i) {
6242 int pos;
6243 if (isl_int_is_zero(bmap->div[i][0]))
6244 continue;
6245 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
6246 bmap->n_div-i);
6247 if (pos == -1)
6248 continue;
6249 isl_basic_map_swap_div(bmap, i, i + pos);
6250 --i;
6252 return bmap;
6255 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
6257 return (struct isl_basic_set *)
6258 isl_basic_map_order_divs((struct isl_basic_map *)bset);
6261 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
6263 int i;
6265 if (!map)
6266 return 0;
6268 for (i = 0; i < map->n; ++i) {
6269 map->p[i] = isl_basic_map_order_divs(map->p[i]);
6270 if (!map->p[i])
6271 goto error;
6274 return map;
6275 error:
6276 isl_map_free(map);
6277 return NULL;
6280 /* Look for a div in dst that corresponds to the div "div" in src.
6281 * The divs before "div" in src and dst are assumed to be the same.
6283 * Returns -1 if no corresponding div was found and the position
6284 * of the corresponding div in dst otherwise.
6286 static int find_div(struct isl_basic_map *dst,
6287 struct isl_basic_map *src, unsigned div)
6289 int i;
6291 unsigned total = isl_dim_total(src->dim);
6293 isl_assert(dst->ctx, div <= dst->n_div, return -1);
6294 for (i = div; i < dst->n_div; ++i)
6295 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
6296 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
6297 dst->n_div - div) == -1)
6298 return i;
6299 return -1;
6302 struct isl_basic_map *isl_basic_map_align_divs(
6303 struct isl_basic_map *dst, struct isl_basic_map *src)
6305 int i;
6306 unsigned total = isl_dim_total(src->dim);
6308 if (!dst || !src)
6309 goto error;
6311 if (src->n_div == 0)
6312 return dst;
6314 for (i = 0; i < src->n_div; ++i)
6315 isl_assert(src->ctx, !isl_int_is_zero(src->div[i][0]), goto error);
6317 src = isl_basic_map_order_divs(src);
6318 dst = isl_basic_map_cow(dst);
6319 dst = isl_basic_map_extend_dim(dst, isl_dim_copy(dst->dim),
6320 src->n_div, 0, 2 * src->n_div);
6321 if (!dst)
6322 return NULL;
6323 for (i = 0; i < src->n_div; ++i) {
6324 int j = find_div(dst, src, i);
6325 if (j < 0) {
6326 j = isl_basic_map_alloc_div(dst);
6327 if (j < 0)
6328 goto error;
6329 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
6330 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
6331 if (isl_basic_map_add_div_constraints(dst, j) < 0)
6332 goto error;
6334 if (j != i)
6335 isl_basic_map_swap_div(dst, i, j);
6337 return dst;
6338 error:
6339 isl_basic_map_free(dst);
6340 return NULL;
6343 struct isl_basic_set *isl_basic_set_align_divs(
6344 struct isl_basic_set *dst, struct isl_basic_set *src)
6346 return (struct isl_basic_set *)isl_basic_map_align_divs(
6347 (struct isl_basic_map *)dst, (struct isl_basic_map *)src);
6350 struct isl_map *isl_map_align_divs(struct isl_map *map)
6352 int i;
6354 if (!map)
6355 return NULL;
6356 if (map->n == 0)
6357 return map;
6358 map = isl_map_compute_divs(map);
6359 map = isl_map_cow(map);
6360 if (!map)
6361 return NULL;
6363 for (i = 1; i < map->n; ++i)
6364 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
6365 for (i = 1; i < map->n; ++i)
6366 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
6368 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6369 return map;
6372 struct isl_set *isl_set_align_divs(struct isl_set *set)
6374 return (struct isl_set *)isl_map_align_divs((struct isl_map *)set);
6377 struct isl_set *isl_set_apply(struct isl_set *set, struct isl_map *map)
6379 if (!set || !map)
6380 goto error;
6381 isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
6382 map = isl_map_intersect_domain(map, set);
6383 set = isl_map_range(map);
6384 return set;
6385 error:
6386 isl_set_free(set);
6387 isl_map_free(map);
6388 return NULL;
6391 /* There is no need to cow as removing empty parts doesn't change
6392 * the meaning of the set.
6394 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
6396 int i;
6398 if (!map)
6399 return NULL;
6401 for (i = map->n-1; i >= 0; --i) {
6402 if (!ISL_F_ISSET(map->p[i], ISL_BASIC_MAP_EMPTY))
6403 continue;
6404 isl_basic_map_free(map->p[i]);
6405 if (i != map->n-1) {
6406 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6407 map->p[i] = map->p[map->n-1];
6409 map->n--;
6412 return map;
6415 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
6417 return (struct isl_set *)
6418 isl_map_remove_empty_parts((struct isl_map *)set);
6421 struct isl_basic_map *isl_map_copy_basic_map(struct isl_map *map)
6423 struct isl_basic_map *bmap;
6424 if (!map || map->n == 0)
6425 return NULL;
6426 bmap = map->p[map->n-1];
6427 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
6428 return isl_basic_map_copy(bmap);
6431 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
6433 return (struct isl_basic_set *)
6434 isl_map_copy_basic_map((struct isl_map *)set);
6437 __isl_give isl_map *isl_map_drop_basic_map(__isl_take isl_map *map,
6438 __isl_keep isl_basic_map *bmap)
6440 int i;
6442 if (!map || !bmap)
6443 goto error;
6444 for (i = map->n-1; i >= 0; --i) {
6445 if (map->p[i] != bmap)
6446 continue;
6447 map = isl_map_cow(map);
6448 if (!map)
6449 goto error;
6450 isl_basic_map_free(map->p[i]);
6451 if (i != map->n-1) {
6452 ISL_F_CLR(map, ISL_SET_NORMALIZED);
6453 map->p[i] = map->p[map->n-1];
6455 map->n--;
6456 return map;
6458 return map;
6459 error:
6460 isl_map_free(map);
6461 return NULL;
6464 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
6465 struct isl_basic_set *bset)
6467 return (struct isl_set *)isl_map_drop_basic_map((struct isl_map *)set,
6468 (struct isl_basic_map *)bset);
6471 /* Given two basic sets bset1 and bset2, compute the maximal difference
6472 * between the values of dimension pos in bset1 and those in bset2
6473 * for any common value of the parameters and dimensions preceding pos.
6475 static enum isl_lp_result basic_set_maximal_difference_at(
6476 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
6477 int pos, isl_int *opt)
6479 struct isl_dim *dims;
6480 struct isl_basic_map *bmap1 = NULL;
6481 struct isl_basic_map *bmap2 = NULL;
6482 struct isl_ctx *ctx;
6483 struct isl_vec *obj;
6484 unsigned total;
6485 unsigned nparam;
6486 unsigned dim1, dim2;
6487 enum isl_lp_result res;
6489 if (!bset1 || !bset2)
6490 return isl_lp_error;
6492 nparam = isl_basic_set_n_param(bset1);
6493 dim1 = isl_basic_set_n_dim(bset1);
6494 dim2 = isl_basic_set_n_dim(bset2);
6495 dims = isl_dim_alloc(bset1->ctx, nparam, pos, dim1 - pos);
6496 bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1), dims);
6497 dims = isl_dim_alloc(bset2->ctx, nparam, pos, dim2 - pos);
6498 bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims);
6499 if (!bmap1 || !bmap2)
6500 goto error;
6501 bmap1 = isl_basic_map_cow(bmap1);
6502 bmap1 = isl_basic_map_extend(bmap1, nparam,
6503 pos, (dim1 - pos) + (dim2 - pos),
6504 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
6505 bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
6506 if (!bmap1)
6507 goto error;
6508 total = isl_basic_map_total_dim(bmap1);
6509 ctx = bmap1->ctx;
6510 obj = isl_vec_alloc(ctx, 1 + total);
6511 isl_seq_clr(obj->block.data, 1 + total);
6512 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
6513 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
6514 if (!obj)
6515 goto error;
6516 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
6517 opt, NULL, NULL);
6518 isl_basic_map_free(bmap1);
6519 isl_vec_free(obj);
6520 return res;
6521 error:
6522 isl_basic_map_free(bmap1);
6523 isl_basic_map_free(bmap2);
6524 return isl_lp_error;
6527 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
6528 * for any common value of the parameters and dimensions preceding pos
6529 * in both basic sets, the values of dimension pos in bset1 are
6530 * smaller or larger than those in bset2.
6532 * Returns
6533 * 1 if bset1 follows bset2
6534 * -1 if bset1 precedes bset2
6535 * 0 if bset1 and bset2 are incomparable
6536 * -2 if some error occurred.
6538 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
6539 struct isl_basic_set *bset2, int pos)
6541 isl_int opt;
6542 enum isl_lp_result res;
6543 int cmp;
6545 isl_int_init(opt);
6547 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
6549 if (res == isl_lp_empty)
6550 cmp = 0;
6551 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
6552 res == isl_lp_unbounded)
6553 cmp = 1;
6554 else if (res == isl_lp_ok && isl_int_is_neg(opt))
6555 cmp = -1;
6556 else
6557 cmp = -2;
6559 isl_int_clear(opt);
6560 return cmp;
6563 /* Given two basic sets bset1 and bset2, check whether
6564 * for any common value of the parameters and dimensions preceding pos
6565 * there is a value of dimension pos in bset1 that is larger
6566 * than a value of the same dimension in bset2.
6568 * Return
6569 * 1 if there exists such a pair
6570 * 0 if there is no such pair, but there is a pair of equal values
6571 * -1 otherwise
6572 * -2 if some error occurred.
6574 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
6575 __isl_keep isl_basic_set *bset2, int pos)
6577 isl_int opt;
6578 enum isl_lp_result res;
6579 int cmp;
6581 isl_int_init(opt);
6583 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
6585 if (res == isl_lp_empty)
6586 cmp = -1;
6587 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
6588 res == isl_lp_unbounded)
6589 cmp = 1;
6590 else if (res == isl_lp_ok && isl_int_is_neg(opt))
6591 cmp = -1;
6592 else if (res == isl_lp_ok)
6593 cmp = 0;
6594 else
6595 cmp = -2;
6597 isl_int_clear(opt);
6598 return cmp;
6601 /* Given two sets set1 and set2, check whether
6602 * for any common value of the parameters and dimensions preceding pos
6603 * there is a value of dimension pos in set1 that is larger
6604 * than a value of the same dimension in set2.
6606 * Return
6607 * 1 if there exists such a pair
6608 * 0 if there is no such pair, but there is a pair of equal values
6609 * -1 otherwise
6610 * -2 if some error occurred.
6612 int isl_set_follows_at(__isl_keep isl_set *set1,
6613 __isl_keep isl_set *set2, int pos)
6615 int i, j;
6616 int follows = -1;
6618 if (!set1 || !set2)
6619 return -2;
6621 for (i = 0; i < set1->n; ++i)
6622 for (j = 0; j < set2->n; ++j) {
6623 int f;
6624 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
6625 if (f == 1 || f == -2)
6626 return f;
6627 if (f > follows)
6628 follows = f;
6631 return follows;
6634 static int isl_basic_map_fast_has_fixed_var(struct isl_basic_map *bmap,
6635 unsigned pos, isl_int *val)
6637 int i;
6638 int d;
6639 unsigned total;
6641 if (!bmap)
6642 return -1;
6643 total = isl_basic_map_total_dim(bmap);
6644 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
6645 for (; d+1 > pos; --d)
6646 if (!isl_int_is_zero(bmap->eq[i][1+d]))
6647 break;
6648 if (d != pos)
6649 continue;
6650 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
6651 return 0;
6652 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
6653 return 0;
6654 if (!isl_int_is_one(bmap->eq[i][1+d]))
6655 return 0;
6656 if (val)
6657 isl_int_neg(*val, bmap->eq[i][0]);
6658 return 1;
6660 return 0;
6663 static int isl_map_fast_has_fixed_var(struct isl_map *map,
6664 unsigned pos, isl_int *val)
6666 int i;
6667 isl_int v;
6668 isl_int tmp;
6669 int fixed;
6671 if (!map)
6672 return -1;
6673 if (map->n == 0)
6674 return 0;
6675 if (map->n == 1)
6676 return isl_basic_map_fast_has_fixed_var(map->p[0], pos, val);
6677 isl_int_init(v);
6678 isl_int_init(tmp);
6679 fixed = isl_basic_map_fast_has_fixed_var(map->p[0], pos, &v);
6680 for (i = 1; fixed == 1 && i < map->n; ++i) {
6681 fixed = isl_basic_map_fast_has_fixed_var(map->p[i], pos, &tmp);
6682 if (fixed == 1 && isl_int_ne(tmp, v))
6683 fixed = 0;
6685 if (val)
6686 isl_int_set(*val, v);
6687 isl_int_clear(tmp);
6688 isl_int_clear(v);
6689 return fixed;
6692 static int isl_basic_set_fast_has_fixed_var(struct isl_basic_set *bset,
6693 unsigned pos, isl_int *val)
6695 return isl_basic_map_fast_has_fixed_var((struct isl_basic_map *)bset,
6696 pos, val);
6699 static int isl_set_fast_has_fixed_var(struct isl_set *set, unsigned pos,
6700 isl_int *val)
6702 return isl_map_fast_has_fixed_var((struct isl_map *)set, pos, val);
6705 int isl_basic_map_fast_is_fixed(struct isl_basic_map *bmap,
6706 enum isl_dim_type type, unsigned pos, isl_int *val)
6708 if (pos >= isl_basic_map_dim(bmap, type))
6709 return -1;
6710 return isl_basic_map_fast_has_fixed_var(bmap,
6711 isl_basic_map_offset(bmap, type) - 1 + pos, val);
6714 int isl_map_fast_is_fixed(struct isl_map *map,
6715 enum isl_dim_type type, unsigned pos, isl_int *val)
6717 if (pos >= isl_map_dim(map, type))
6718 return -1;
6719 return isl_map_fast_has_fixed_var(map,
6720 map_offset(map, type) - 1 + pos, val);
6723 /* Check if dimension dim has fixed value and if so and if val is not NULL,
6724 * then return this fixed value in *val.
6726 int isl_basic_set_fast_dim_is_fixed(struct isl_basic_set *bset, unsigned dim,
6727 isl_int *val)
6729 return isl_basic_set_fast_has_fixed_var(bset,
6730 isl_basic_set_n_param(bset) + dim, val);
6733 /* Check if dimension dim has fixed value and if so and if val is not NULL,
6734 * then return this fixed value in *val.
6736 int isl_set_fast_dim_is_fixed(struct isl_set *set, unsigned dim, isl_int *val)
6738 return isl_set_fast_has_fixed_var(set, isl_set_n_param(set) + dim, val);
6741 /* Check if input variable in has fixed value and if so and if val is not NULL,
6742 * then return this fixed value in *val.
6744 int isl_map_fast_input_is_fixed(struct isl_map *map, unsigned in, isl_int *val)
6746 return isl_map_fast_has_fixed_var(map, isl_map_n_param(map) + in, val);
6749 /* Check if dimension dim has an (obvious) fixed lower bound and if so
6750 * and if val is not NULL, then return this lower bound in *val.
6752 int isl_basic_set_fast_dim_has_fixed_lower_bound(struct isl_basic_set *bset,
6753 unsigned dim, isl_int *val)
6755 int i, i_eq = -1, i_ineq = -1;
6756 isl_int *c;
6757 unsigned total;
6758 unsigned nparam;
6760 if (!bset)
6761 return -1;
6762 total = isl_basic_set_total_dim(bset);
6763 nparam = isl_basic_set_n_param(bset);
6764 for (i = 0; i < bset->n_eq; ++i) {
6765 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
6766 continue;
6767 if (i_eq != -1)
6768 return 0;
6769 i_eq = i;
6771 for (i = 0; i < bset->n_ineq; ++i) {
6772 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
6773 continue;
6774 if (i_eq != -1 || i_ineq != -1)
6775 return 0;
6776 i_ineq = i;
6778 if (i_eq == -1 && i_ineq == -1)
6779 return 0;
6780 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
6781 /* The coefficient should always be one due to normalization. */
6782 if (!isl_int_is_one(c[1+nparam+dim]))
6783 return 0;
6784 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
6785 return 0;
6786 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
6787 total - nparam - dim - 1) != -1)
6788 return 0;
6789 if (val)
6790 isl_int_neg(*val, c[0]);
6791 return 1;
6794 int isl_set_fast_dim_has_fixed_lower_bound(struct isl_set *set,
6795 unsigned dim, isl_int *val)
6797 int i;
6798 isl_int v;
6799 isl_int tmp;
6800 int fixed;
6802 if (!set)
6803 return -1;
6804 if (set->n == 0)
6805 return 0;
6806 if (set->n == 1)
6807 return isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[0],
6808 dim, val);
6809 isl_int_init(v);
6810 isl_int_init(tmp);
6811 fixed = isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[0],
6812 dim, &v);
6813 for (i = 1; fixed == 1 && i < set->n; ++i) {
6814 fixed = isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[i],
6815 dim, &tmp);
6816 if (fixed == 1 && isl_int_ne(tmp, v))
6817 fixed = 0;
6819 if (val)
6820 isl_int_set(*val, v);
6821 isl_int_clear(tmp);
6822 isl_int_clear(v);
6823 return fixed;
6826 struct constraint {
6827 unsigned size;
6828 isl_int *c;
6831 /* uset_gist depends on constraints without existentially quantified
6832 * variables sorting first.
6834 static int qsort_constraint_cmp(const void *p1, const void *p2)
6836 const struct constraint *c1 = (const struct constraint *)p1;
6837 const struct constraint *c2 = (const struct constraint *)p2;
6838 int l1, l2;
6839 unsigned size = isl_min(c1->size, c2->size);
6841 l1 = isl_seq_last_non_zero(c1->c, size);
6842 l2 = isl_seq_last_non_zero(c2->c, size);
6844 if (l1 != l2)
6845 return l1 - l2;
6847 return isl_seq_cmp(c1->c, c2->c, size);
6850 static struct isl_basic_map *isl_basic_map_sort_constraints(
6851 struct isl_basic_map *bmap)
6853 int i;
6854 struct constraint *c;
6855 unsigned total;
6857 if (!bmap)
6858 return NULL;
6859 total = isl_basic_map_total_dim(bmap);
6860 c = isl_alloc_array(bmap->ctx, struct constraint, bmap->n_ineq);
6861 if (!c)
6862 goto error;
6863 for (i = 0; i < bmap->n_ineq; ++i) {
6864 c[i].size = total;
6865 c[i].c = bmap->ineq[i];
6867 qsort(c, bmap->n_ineq, sizeof(struct constraint), qsort_constraint_cmp);
6868 for (i = 0; i < bmap->n_ineq; ++i)
6869 bmap->ineq[i] = c[i].c;
6870 free(c);
6871 return bmap;
6872 error:
6873 isl_basic_map_free(bmap);
6874 return NULL;
6877 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
6878 __isl_take isl_basic_set *bset)
6880 return (struct isl_basic_set *)isl_basic_map_sort_constraints(
6881 (struct isl_basic_map *)bset);
6884 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
6886 if (!bmap)
6887 return NULL;
6888 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
6889 return bmap;
6890 bmap = isl_basic_map_remove_redundancies(bmap);
6891 bmap = isl_basic_map_sort_constraints(bmap);
6892 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
6893 return bmap;
6896 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
6898 return (struct isl_basic_set *)isl_basic_map_normalize(
6899 (struct isl_basic_map *)bset);
6902 int isl_basic_map_fast_cmp(const __isl_keep isl_basic_map *bmap1,
6903 const __isl_keep isl_basic_map *bmap2)
6905 int i, cmp;
6906 unsigned total;
6908 if (bmap1 == bmap2)
6909 return 0;
6910 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
6911 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
6912 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
6913 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
6914 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
6915 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
6916 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
6917 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
6918 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
6919 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
6920 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
6921 return 0;
6922 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
6923 return 1;
6924 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
6925 return -1;
6926 if (bmap1->n_eq != bmap2->n_eq)
6927 return bmap1->n_eq - bmap2->n_eq;
6928 if (bmap1->n_ineq != bmap2->n_ineq)
6929 return bmap1->n_ineq - bmap2->n_ineq;
6930 if (bmap1->n_div != bmap2->n_div)
6931 return bmap1->n_div - bmap2->n_div;
6932 total = isl_basic_map_total_dim(bmap1);
6933 for (i = 0; i < bmap1->n_eq; ++i) {
6934 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
6935 if (cmp)
6936 return cmp;
6938 for (i = 0; i < bmap1->n_ineq; ++i) {
6939 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
6940 if (cmp)
6941 return cmp;
6943 for (i = 0; i < bmap1->n_div; ++i) {
6944 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
6945 if (cmp)
6946 return cmp;
6948 return 0;
6951 int isl_basic_map_fast_is_equal(__isl_keep isl_basic_map *bmap1,
6952 __isl_keep isl_basic_map *bmap2)
6954 return isl_basic_map_fast_cmp(bmap1, bmap2) == 0;
6957 int isl_basic_set_fast_is_equal(__isl_keep isl_basic_set *bset1,
6958 __isl_keep isl_basic_set *bset2)
6960 return isl_basic_map_fast_is_equal((isl_basic_map *)bset1,
6961 (isl_basic_map *)bset2);
6964 static int qsort_bmap_cmp(const void *p1, const void *p2)
6966 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
6967 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
6969 return isl_basic_map_fast_cmp(bmap1, bmap2);
6972 /* We normalize in place, but if anything goes wrong we need
6973 * to return NULL, so we need to make sure we don't change the
6974 * meaning of any possible other copies of map.
6976 struct isl_map *isl_map_normalize(struct isl_map *map)
6978 int i, j;
6979 struct isl_basic_map *bmap;
6981 if (!map)
6982 return NULL;
6983 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
6984 return map;
6985 for (i = 0; i < map->n; ++i) {
6986 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
6987 if (!bmap)
6988 goto error;
6989 isl_basic_map_free(map->p[i]);
6990 map->p[i] = bmap;
6992 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
6993 ISL_F_SET(map, ISL_MAP_NORMALIZED);
6994 map = isl_map_remove_empty_parts(map);
6995 if (!map)
6996 return NULL;
6997 for (i = map->n - 1; i >= 1; --i) {
6998 if (!isl_basic_map_fast_is_equal(map->p[i-1], map->p[i]))
6999 continue;
7000 isl_basic_map_free(map->p[i-1]);
7001 for (j = i; j < map->n; ++j)
7002 map->p[j-1] = map->p[j];
7003 map->n--;
7005 return map;
7006 error:
7007 isl_map_free(map);
7008 return NULL;
7012 struct isl_set *isl_set_normalize(struct isl_set *set)
7014 return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
7017 int isl_map_fast_is_equal(struct isl_map *map1, struct isl_map *map2)
7019 int i;
7020 int equal;
7022 if (!map1 || !map2)
7023 return -1;
7025 if (map1 == map2)
7026 return 1;
7027 if (!isl_dim_equal(map1->dim, map2->dim))
7028 return 0;
7030 map1 = isl_map_copy(map1);
7031 map2 = isl_map_copy(map2);
7032 map1 = isl_map_normalize(map1);
7033 map2 = isl_map_normalize(map2);
7034 if (!map1 || !map2)
7035 goto error;
7036 equal = map1->n == map2->n;
7037 for (i = 0; equal && i < map1->n; ++i) {
7038 equal = isl_basic_map_fast_is_equal(map1->p[i], map2->p[i]);
7039 if (equal < 0)
7040 goto error;
7042 isl_map_free(map1);
7043 isl_map_free(map2);
7044 return equal;
7045 error:
7046 isl_map_free(map1);
7047 isl_map_free(map2);
7048 return -1;
7051 int isl_set_fast_is_equal(struct isl_set *set1, struct isl_set *set2)
7053 return isl_map_fast_is_equal((struct isl_map *)set1,
7054 (struct isl_map *)set2);
7057 /* Return an interval that ranges from min to max (inclusive)
7059 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
7060 isl_int min, isl_int max)
7062 int k;
7063 struct isl_basic_set *bset = NULL;
7065 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
7066 if (!bset)
7067 goto error;
7069 k = isl_basic_set_alloc_inequality(bset);
7070 if (k < 0)
7071 goto error;
7072 isl_int_set_si(bset->ineq[k][1], 1);
7073 isl_int_neg(bset->ineq[k][0], min);
7075 k = isl_basic_set_alloc_inequality(bset);
7076 if (k < 0)
7077 goto error;
7078 isl_int_set_si(bset->ineq[k][1], -1);
7079 isl_int_set(bset->ineq[k][0], max);
7081 return bset;
7082 error:
7083 isl_basic_set_free(bset);
7084 return NULL;
7087 /* Return the Cartesian product of the basic sets in list (in the given order).
7089 __isl_give isl_basic_set *isl_basic_set_list_product(
7090 __isl_take struct isl_basic_set_list *list)
7092 int i;
7093 unsigned dim;
7094 unsigned nparam;
7095 unsigned extra;
7096 unsigned n_eq;
7097 unsigned n_ineq;
7098 struct isl_basic_set *product = NULL;
7100 if (!list)
7101 goto error;
7102 isl_assert(list->ctx, list->n > 0, goto error);
7103 isl_assert(list->ctx, list->p[0], goto error);
7104 nparam = isl_basic_set_n_param(list->p[0]);
7105 dim = isl_basic_set_n_dim(list->p[0]);
7106 extra = list->p[0]->n_div;
7107 n_eq = list->p[0]->n_eq;
7108 n_ineq = list->p[0]->n_ineq;
7109 for (i = 1; i < list->n; ++i) {
7110 isl_assert(list->ctx, list->p[i], goto error);
7111 isl_assert(list->ctx,
7112 nparam == isl_basic_set_n_param(list->p[i]), goto error);
7113 dim += isl_basic_set_n_dim(list->p[i]);
7114 extra += list->p[i]->n_div;
7115 n_eq += list->p[i]->n_eq;
7116 n_ineq += list->p[i]->n_ineq;
7118 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
7119 n_eq, n_ineq);
7120 if (!product)
7121 goto error;
7122 dim = 0;
7123 for (i = 0; i < list->n; ++i) {
7124 isl_basic_set_add_constraints(product,
7125 isl_basic_set_copy(list->p[i]), dim);
7126 dim += isl_basic_set_n_dim(list->p[i]);
7128 isl_basic_set_list_free(list);
7129 return product;
7130 error:
7131 isl_basic_set_free(product);
7132 isl_basic_set_list_free(list);
7133 return NULL;
7136 struct isl_basic_map *isl_basic_map_product(
7137 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7139 struct isl_dim *dim_result = NULL;
7140 struct isl_basic_map *bmap;
7141 unsigned in1, in2, out1, out2, nparam, total, pos;
7142 struct isl_dim_map *dim_map1, *dim_map2;
7144 if (!bmap1 || !bmap2)
7145 goto error;
7147 isl_assert(bmap1->ctx, isl_dim_match(bmap1->dim, isl_dim_param,
7148 bmap2->dim, isl_dim_param), goto error);
7149 dim_result = isl_dim_product(isl_dim_copy(bmap1->dim),
7150 isl_dim_copy(bmap2->dim));
7152 in1 = isl_basic_map_n_in(bmap1);
7153 in2 = isl_basic_map_n_in(bmap2);
7154 out1 = isl_basic_map_n_out(bmap1);
7155 out2 = isl_basic_map_n_out(bmap2);
7156 nparam = isl_basic_map_n_param(bmap1);
7158 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
7159 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
7160 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
7161 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
7162 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
7163 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
7164 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
7165 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
7166 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
7167 isl_dim_map_div(dim_map1, bmap1, pos += out2);
7168 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
7170 bmap = isl_basic_map_alloc_dim(dim_result,
7171 bmap1->n_div + bmap2->n_div,
7172 bmap1->n_eq + bmap2->n_eq,
7173 bmap1->n_ineq + bmap2->n_ineq);
7174 bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
7175 bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
7176 bmap = isl_basic_map_simplify(bmap);
7177 return isl_basic_map_finalize(bmap);
7178 error:
7179 isl_basic_map_free(bmap1);
7180 isl_basic_map_free(bmap2);
7181 return NULL;
7184 __isl_give isl_basic_map *isl_basic_map_flat_product(
7185 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
7187 isl_basic_map *prod;
7189 prod = isl_basic_map_product(bmap1, bmap2);
7190 prod = isl_basic_map_flatten(prod);
7191 return prod;
7194 __isl_give isl_basic_set *isl_basic_set_flat_product(
7195 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
7197 return isl_basic_map_flat_product(bset1, bset2);
7200 __isl_give isl_basic_map *isl_basic_map_range_product(
7201 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
7203 isl_dim *dim_result = NULL;
7204 isl_basic_map *bmap;
7205 unsigned in, out1, out2, nparam, total, pos;
7206 struct isl_dim_map *dim_map1, *dim_map2;
7208 if (!bmap1 || !bmap2)
7209 goto error;
7211 dim_result = isl_dim_range_product(isl_dim_copy(bmap1->dim),
7212 isl_dim_copy(bmap2->dim));
7214 in = isl_basic_map_dim(bmap1, isl_dim_in);
7215 out1 = isl_basic_map_n_out(bmap1);
7216 out2 = isl_basic_map_n_out(bmap2);
7217 nparam = isl_basic_map_n_param(bmap1);
7219 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
7220 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
7221 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
7222 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
7223 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
7224 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
7225 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
7226 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
7227 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
7228 isl_dim_map_div(dim_map1, bmap1, pos += out2);
7229 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
7231 bmap = isl_basic_map_alloc_dim(dim_result,
7232 bmap1->n_div + bmap2->n_div,
7233 bmap1->n_eq + bmap2->n_eq,
7234 bmap1->n_ineq + bmap2->n_ineq);
7235 bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
7236 bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
7237 bmap = isl_basic_map_simplify(bmap);
7238 return isl_basic_map_finalize(bmap);
7239 error:
7240 isl_basic_map_free(bmap1);
7241 isl_basic_map_free(bmap2);
7242 return NULL;
7245 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
7246 __isl_take isl_map *map2,
7247 __isl_give isl_dim *(*dim_product)(__isl_take isl_dim *left,
7248 __isl_take isl_dim *right),
7249 __isl_give isl_basic_map *(*basic_map_product)(
7250 __isl_take isl_basic_map *left, __isl_take isl_basic_map *right))
7252 unsigned flags = 0;
7253 struct isl_map *result;
7254 int i, j;
7256 if (!map1 || !map2)
7257 goto error;
7259 isl_assert(map1->ctx, isl_dim_match(map1->dim, isl_dim_param,
7260 map2->dim, isl_dim_param), goto error);
7262 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
7263 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
7264 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7266 result = isl_map_alloc_dim(dim_product(isl_dim_copy(map1->dim),
7267 isl_dim_copy(map2->dim)),
7268 map1->n * map2->n, flags);
7269 if (!result)
7270 goto error;
7271 for (i = 0; i < map1->n; ++i)
7272 for (j = 0; j < map2->n; ++j) {
7273 struct isl_basic_map *part;
7274 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
7275 isl_basic_map_copy(map2->p[j]));
7276 if (isl_basic_map_is_empty(part))
7277 isl_basic_map_free(part);
7278 else
7279 result = isl_map_add_basic_map(result, part);
7280 if (!result)
7281 goto error;
7283 isl_map_free(map1);
7284 isl_map_free(map2);
7285 return result;
7286 error:
7287 isl_map_free(map1);
7288 isl_map_free(map2);
7289 return NULL;
7292 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
7294 struct isl_map *isl_map_product(struct isl_map *map1, struct isl_map *map2)
7296 return map_product(map1, map2, &isl_dim_product, &isl_basic_map_product);
7299 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
7301 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
7302 __isl_take isl_map *map2)
7304 isl_map *prod;
7306 prod = isl_map_product(map1, map2);
7307 prod = isl_map_flatten(prod);
7308 return prod;
7311 /* Given two set A and B, construct its Cartesian product A x B.
7313 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
7315 return (struct isl_set *)isl_map_product((struct isl_map *)set1,
7316 (struct isl_map *)set2);
7319 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
7320 __isl_take isl_set *set2)
7322 return (isl_set *)isl_map_flat_product((isl_map *)set1, (isl_map *)set2);
7325 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
7327 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
7328 __isl_take isl_map *map2)
7330 return map_product(map1, map2, &isl_dim_range_product,
7331 &isl_basic_map_range_product);
7334 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
7336 int i;
7337 uint32_t hash = isl_hash_init();
7338 unsigned total;
7340 if (!bmap)
7341 return 0;
7342 bmap = isl_basic_map_copy(bmap);
7343 bmap = isl_basic_map_normalize(bmap);
7344 if (!bmap)
7345 return 0;
7346 total = isl_basic_map_total_dim(bmap);
7347 isl_hash_byte(hash, bmap->n_eq & 0xFF);
7348 for (i = 0; i < bmap->n_eq; ++i) {
7349 uint32_t c_hash;
7350 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
7351 isl_hash_hash(hash, c_hash);
7353 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
7354 for (i = 0; i < bmap->n_ineq; ++i) {
7355 uint32_t c_hash;
7356 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
7357 isl_hash_hash(hash, c_hash);
7359 isl_hash_byte(hash, bmap->n_div & 0xFF);
7360 for (i = 0; i < bmap->n_div; ++i) {
7361 uint32_t c_hash;
7362 if (isl_int_is_zero(bmap->div[i][0]))
7363 continue;
7364 isl_hash_byte(hash, i & 0xFF);
7365 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
7366 isl_hash_hash(hash, c_hash);
7368 isl_basic_map_free(bmap);
7369 return hash;
7372 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
7374 return isl_basic_map_get_hash((isl_basic_map *)bset);
7377 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
7379 int i;
7380 uint32_t hash;
7382 if (!map)
7383 return 0;
7384 map = isl_map_copy(map);
7385 map = isl_map_normalize(map);
7386 if (!map)
7387 return 0;
7389 hash = isl_hash_init();
7390 for (i = 0; i < map->n; ++i) {
7391 uint32_t bmap_hash;
7392 bmap_hash = isl_basic_map_get_hash(map->p[i]);
7393 isl_hash_hash(hash, bmap_hash);
7396 isl_map_free(map);
7398 return hash;
7401 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
7403 return isl_map_get_hash((isl_map *)set);
7406 /* Check if the value for dimension dim is completely determined
7407 * by the values of the other parameters and variables.
7408 * That is, check if dimension dim is involved in an equality.
7410 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
7412 int i;
7413 unsigned nparam;
7415 if (!bset)
7416 return -1;
7417 nparam = isl_basic_set_n_param(bset);
7418 for (i = 0; i < bset->n_eq; ++i)
7419 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
7420 return 1;
7421 return 0;
7424 /* Check if the value for dimension dim is completely determined
7425 * by the values of the other parameters and variables.
7426 * That is, check if dimension dim is involved in an equality
7427 * for each of the subsets.
7429 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
7431 int i;
7433 if (!set)
7434 return -1;
7435 for (i = 0; i < set->n; ++i) {
7436 int unique;
7437 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
7438 if (unique != 1)
7439 return unique;
7441 return 1;
7444 int isl_set_n_basic_set(__isl_keep isl_set *set)
7446 return set ? set->n : 0;
7449 int isl_map_foreach_basic_map(__isl_keep isl_map *map,
7450 int (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
7452 int i;
7454 if (!map)
7455 return -1;
7457 for (i = 0; i < map->n; ++i)
7458 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
7459 return -1;
7461 return 0;
7464 int isl_set_foreach_basic_set(__isl_keep isl_set *set,
7465 int (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
7467 int i;
7469 if (!set)
7470 return -1;
7472 for (i = 0; i < set->n; ++i)
7473 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
7474 return -1;
7476 return 0;
7479 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
7481 struct isl_dim *dim;
7483 if (!bset)
7484 return NULL;
7486 bset = isl_basic_set_cow(bset);
7487 if (!bset)
7488 return NULL;
7490 dim = isl_basic_set_get_dim(bset);
7491 dim = isl_dim_lift(dim, bset->n_div);
7492 if (!dim)
7493 goto error;
7494 isl_dim_free(bset->dim);
7495 bset->dim = dim;
7496 bset->extra -= bset->n_div;
7497 bset->n_div = 0;
7499 bset = isl_basic_set_finalize(bset);
7501 return bset;
7502 error:
7503 isl_basic_set_free(bset);
7504 return NULL;
7507 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
7509 int i;
7510 struct isl_dim *dim;
7511 unsigned n_div;
7513 set = isl_set_align_divs(set);
7515 if (!set)
7516 return NULL;
7518 set = isl_set_cow(set);
7519 if (!set)
7520 return NULL;
7522 n_div = set->p[0]->n_div;
7523 dim = isl_set_get_dim(set);
7524 dim = isl_dim_lift(dim, n_div);
7525 if (!dim)
7526 goto error;
7527 isl_dim_free(set->dim);
7528 set->dim = dim;
7530 for (i = 0; i < set->n; ++i) {
7531 set->p[i] = isl_basic_set_lift(set->p[i]);
7532 if (!set->p[i])
7533 goto error;
7536 return set;
7537 error:
7538 isl_set_free(set);
7539 return NULL;
7542 __isl_give isl_map *isl_set_lifting(__isl_take isl_set *set)
7544 struct isl_dim *dim;
7545 struct isl_basic_map *bmap;
7546 unsigned n_set;
7547 unsigned n_div;
7548 unsigned n_param;
7549 unsigned total;
7550 int i, k, l;
7552 set = isl_set_align_divs(set);
7554 if (!set)
7555 return NULL;
7557 dim = isl_set_get_dim(set);
7558 if (set->n == 0 || set->p[0]->n_div == 0) {
7559 isl_set_free(set);
7560 return isl_map_identity(isl_dim_map_from_set(dim));
7563 n_div = set->p[0]->n_div;
7564 dim = isl_dim_map_from_set(dim);
7565 n_param = isl_dim_size(dim, isl_dim_param);
7566 n_set = isl_dim_size(dim, isl_dim_in);
7567 dim = isl_dim_extend(dim, n_param, n_set, n_set + n_div);
7568 bmap = isl_basic_map_alloc_dim(dim, 0, n_set, 2 * n_div);
7569 for (i = 0; i < n_set; ++i)
7570 bmap = var_equal(bmap, i);
7572 total = n_param + n_set + n_set + n_div;
7573 for (i = 0; i < n_div; ++i) {
7574 k = isl_basic_map_alloc_inequality(bmap);
7575 if (k < 0)
7576 goto error;
7577 isl_seq_cpy(bmap->ineq[k], set->p[0]->div[i]+1, 1+n_param);
7578 isl_seq_clr(bmap->ineq[k]+1+n_param, n_set);
7579 isl_seq_cpy(bmap->ineq[k]+1+n_param+n_set,
7580 set->p[0]->div[i]+1+1+n_param, n_set + n_div);
7581 isl_int_neg(bmap->ineq[k][1+n_param+n_set+n_set+i],
7582 set->p[0]->div[i][0]);
7584 l = isl_basic_map_alloc_inequality(bmap);
7585 if (l < 0)
7586 goto error;
7587 isl_seq_neg(bmap->ineq[l], bmap->ineq[k], 1 + total);
7588 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0],
7589 set->p[0]->div[i][0]);
7590 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
7593 isl_set_free(set);
7594 bmap = isl_basic_map_simplify(bmap);
7595 bmap = isl_basic_map_finalize(bmap);
7596 return isl_map_from_basic_map(bmap);
7597 error:
7598 isl_set_free(set);
7599 isl_basic_map_free(bmap);
7600 return NULL;
7603 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
7605 unsigned dim;
7606 int size = 0;
7608 if (!bset)
7609 return -1;
7611 dim = isl_basic_set_total_dim(bset);
7612 size += bset->n_eq * (1 + dim);
7613 size += bset->n_ineq * (1 + dim);
7614 size += bset->n_div * (2 + dim);
7616 return size;
7619 int isl_set_size(__isl_keep isl_set *set)
7621 int i;
7622 int size = 0;
7624 if (!set)
7625 return -1;
7627 for (i = 0; i < set->n; ++i)
7628 size += isl_basic_set_size(set->p[i]);
7630 return size;
7633 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
7634 enum isl_dim_type type, unsigned pos)
7636 int i;
7637 int lower, upper;
7639 if (!bmap)
7640 return -1;
7642 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), return -1);
7644 pos += isl_basic_map_offset(bmap, type);
7646 for (i = 0; i < bmap->n_eq; ++i)
7647 if (!isl_int_is_zero(bmap->eq[i][pos]))
7648 return 1;
7650 lower = upper = 0;
7651 for (i = 0; i < bmap->n_ineq; ++i) {
7652 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
7653 if (sgn > 0)
7654 lower = 1;
7655 if (sgn < 0)
7656 upper = 1;
7659 return lower && upper;
7662 int isl_map_dim_is_bounded(__isl_keep isl_map *map,
7663 enum isl_dim_type type, unsigned pos)
7665 int i;
7667 if (!map)
7668 return -1;
7670 for (i = 0; i < map->n; ++i) {
7671 int bounded;
7672 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
7673 if (bounded < 0 || !bounded)
7674 return bounded;
7677 return 1;
7680 /* Return 1 if the specified dim is involved in both an upper bound
7681 * and a lower bound.
7683 int isl_set_dim_is_bounded(__isl_keep isl_set *set,
7684 enum isl_dim_type type, unsigned pos)
7686 return isl_map_dim_is_bounded((isl_map *)set, type, pos);
7689 /* For each of the "n" variables starting at "first", determine
7690 * the sign of the variable and put the results in the first "n"
7691 * elements of the array "signs".
7692 * Sign
7693 * 1 means that the variable is non-negative
7694 * -1 means that the variable is non-positive
7695 * 0 means the variable attains both positive and negative values.
7697 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
7698 unsigned first, unsigned n, int *signs)
7700 isl_vec *bound = NULL;
7701 struct isl_tab *tab = NULL;
7702 struct isl_tab_undo *snap;
7703 int i;
7705 if (!bset || !signs)
7706 return -1;
7708 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
7709 tab = isl_tab_from_basic_set(bset);
7710 if (!bound || !tab)
7711 goto error;
7713 isl_seq_clr(bound->el, bound->size);
7714 isl_int_set_si(bound->el[0], -1);
7716 snap = isl_tab_snap(tab);
7717 for (i = 0; i < n; ++i) {
7718 int empty;
7720 isl_int_set_si(bound->el[1 + first + i], -1);
7721 if (isl_tab_add_ineq(tab, bound->el) < 0)
7722 goto error;
7723 empty = tab->empty;
7724 isl_int_set_si(bound->el[1 + first + i], 0);
7725 if (isl_tab_rollback(tab, snap) < 0)
7726 goto error;
7728 if (empty) {
7729 signs[i] = 1;
7730 continue;
7733 isl_int_set_si(bound->el[1 + first + i], 1);
7734 if (isl_tab_add_ineq(tab, bound->el) < 0)
7735 goto error;
7736 empty = tab->empty;
7737 isl_int_set_si(bound->el[1 + first + i], 0);
7738 if (isl_tab_rollback(tab, snap) < 0)
7739 goto error;
7741 signs[i] = empty ? -1 : 0;
7744 isl_tab_free(tab);
7745 isl_vec_free(bound);
7746 return 0;
7747 error:
7748 isl_tab_free(tab);
7749 isl_vec_free(bound);
7750 return -1;
7753 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
7754 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
7756 if (!bset || !signs)
7757 return -1;
7758 isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
7759 return -1);
7761 first += pos(bset->dim, type) - 1;
7762 return isl_basic_set_vars_get_sign(bset, first, n, signs);
7765 /* Check if the given basic map is obviously single-valued.
7766 * In particular, for each output dimension, check that there is
7767 * an equality that defines the output dimension in terms of
7768 * earlier dimensions.
7770 int isl_basic_map_fast_is_single_valued(__isl_keep isl_basic_map *bmap)
7772 int i, j;
7773 unsigned total;
7774 unsigned n_out;
7775 unsigned o_out;
7777 if (!bmap)
7778 return -1;
7780 total = 1 + isl_basic_map_total_dim(bmap);
7781 n_out = isl_basic_map_dim(bmap, isl_dim_out);
7782 o_out = isl_basic_map_offset(bmap, isl_dim_out);
7784 for (i = 0; i < n_out; ++i) {
7785 for (j = 0; j < bmap->n_eq; ++j) {
7786 if (isl_int_is_zero(bmap->eq[j][o_out + i]))
7787 continue;
7788 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + i + 1,
7789 total - (o_out + i + 1)) == -1)
7790 break;
7792 if (j >= bmap->n_eq)
7793 return 0;
7796 return 1;
7799 /* Check if the given map is obviously single-valued.
7801 int isl_map_fast_is_single_valued(__isl_keep isl_map *map)
7803 int sv;
7805 if (!map)
7806 return -1;
7807 if (map->n == 0)
7808 return 1;
7809 if (map->n >= 2)
7810 return 0;
7812 return isl_basic_map_fast_is_single_valued(map->p[0]);
7815 /* Check if the given map is single-valued.
7816 * We simply compute
7818 * M \circ M^-1
7820 * and check if the result is a subset of the identity mapping.
7822 int isl_map_is_single_valued(__isl_keep isl_map *map)
7824 isl_dim *dim;
7825 isl_map *test;
7826 isl_map *id;
7827 int sv;
7829 sv = isl_map_fast_is_single_valued(map);
7830 if (sv < 0 || sv)
7831 return sv;
7833 test = isl_map_reverse(isl_map_copy(map));
7834 test = isl_map_apply_range(test, isl_map_copy(map));
7836 dim = isl_dim_map_from_set(isl_dim_range(isl_map_get_dim(map)));
7837 id = isl_map_identity(dim);
7839 sv = isl_map_is_subset(test, id);
7841 isl_map_free(test);
7842 isl_map_free(id);
7844 return sv;
7847 int isl_map_is_bijective(__isl_keep isl_map *map)
7849 int sv;
7851 sv = isl_map_is_single_valued(map);
7852 if (sv < 0 || !sv)
7853 return sv;
7855 map = isl_map_copy(map);
7856 map = isl_map_reverse(map);
7857 sv = isl_map_is_single_valued(map);
7858 isl_map_free(map);
7860 return sv;
7863 int isl_set_is_singleton(__isl_keep isl_set *set)
7865 return isl_map_is_single_valued((isl_map *)set);
7868 int isl_map_is_translation(__isl_keep isl_map *map)
7870 int ok;
7871 isl_set *delta;
7873 delta = isl_map_deltas(isl_map_copy(map));
7874 ok = isl_set_is_singleton(delta);
7875 isl_set_free(delta);
7877 return ok;
7880 static int unique(isl_int *p, unsigned pos, unsigned len)
7882 if (isl_seq_first_non_zero(p, pos) != -1)
7883 return 0;
7884 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
7885 return 0;
7886 return 1;
7889 int isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
7891 int i, j;
7892 unsigned nvar;
7893 unsigned ovar;
7895 if (!bset)
7896 return -1;
7898 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
7899 return 0;
7901 nvar = isl_basic_set_dim(bset, isl_dim_set);
7902 ovar = isl_dim_offset(bset->dim, isl_dim_set);
7903 for (j = 0; j < nvar; ++j) {
7904 int lower = 0, upper = 0;
7905 for (i = 0; i < bset->n_eq; ++i) {
7906 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
7907 continue;
7908 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
7909 return 0;
7910 break;
7912 if (i < bset->n_eq)
7913 continue;
7914 for (i = 0; i < bset->n_ineq; ++i) {
7915 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
7916 continue;
7917 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
7918 return 0;
7919 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
7920 lower = 1;
7921 else
7922 upper = 1;
7924 if (!lower || !upper)
7925 return 0;
7928 return 1;
7931 int isl_set_is_box(__isl_keep isl_set *set)
7933 if (!set)
7934 return -1;
7935 if (set->n != 1)
7936 return 0;
7938 return isl_basic_set_is_box(set->p[0]);
7941 int isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
7943 if (!bset)
7944 return -1;
7946 return isl_dim_is_wrapping(bset->dim);
7949 int isl_set_is_wrapping(__isl_keep isl_set *set)
7951 if (!set)
7952 return -1;
7954 return isl_dim_is_wrapping(set->dim);
7957 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
7959 bmap = isl_basic_map_cow(bmap);
7960 if (!bmap)
7961 return NULL;
7963 bmap->dim = isl_dim_wrap(bmap->dim);
7964 if (!bmap->dim)
7965 goto error;
7967 bmap = isl_basic_map_finalize(bmap);
7969 return (isl_basic_set *)bmap;
7970 error:
7971 isl_basic_map_free(bmap);
7972 return NULL;
7975 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
7977 int i;
7979 map = isl_map_cow(map);
7980 if (!map)
7981 return NULL;
7983 for (i = 0; i < map->n; ++i) {
7984 map->p[i] = (isl_basic_map *)isl_basic_map_wrap(map->p[i]);
7985 if (!map->p[i])
7986 goto error;
7988 map->dim = isl_dim_wrap(map->dim);
7989 if (!map->dim)
7990 goto error;
7992 return (isl_set *)map;
7993 error:
7994 isl_map_free(map);
7995 return NULL;
7998 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
8000 bset = isl_basic_set_cow(bset);
8001 if (!bset)
8002 return NULL;
8004 bset->dim = isl_dim_unwrap(bset->dim);
8005 if (!bset->dim)
8006 goto error;
8008 bset = isl_basic_set_finalize(bset);
8010 return (isl_basic_map *)bset;
8011 error:
8012 isl_basic_set_free(bset);
8013 return NULL;
8016 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
8018 int i;
8020 if (!set)
8021 return NULL;
8023 if (!isl_set_is_wrapping(set))
8024 isl_die(set->ctx, isl_error_invalid, "not a wrapping set",
8025 goto error);
8027 set = isl_set_cow(set);
8028 if (!set)
8029 return NULL;
8031 for (i = 0; i < set->n; ++i) {
8032 set->p[i] = (isl_basic_set *)isl_basic_set_unwrap(set->p[i]);
8033 if (!set->p[i])
8034 goto error;
8037 set->dim = isl_dim_unwrap(set->dim);
8038 if (!set->dim)
8039 goto error;
8041 return (isl_map *)set;
8042 error:
8043 isl_set_free(set);
8044 return NULL;
8047 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
8048 enum isl_dim_type type)
8050 if (!bmap)
8051 return NULL;
8053 if (!isl_dim_is_named_or_nested(bmap->dim, type))
8054 return bmap;
8056 bmap = isl_basic_map_cow(bmap);
8057 if (!bmap)
8058 return NULL;
8060 bmap->dim = isl_dim_reset(bmap->dim, type);
8061 if (!bmap->dim)
8062 goto error;
8064 bmap = isl_basic_map_finalize(bmap);
8066 return bmap;
8067 error:
8068 isl_basic_map_free(bmap);
8069 return NULL;
8072 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
8073 enum isl_dim_type type)
8075 int i;
8077 if (!map)
8078 return NULL;
8080 if (!isl_dim_is_named_or_nested(map->dim, type))
8081 return map;
8083 map = isl_map_cow(map);
8084 if (!map)
8085 return NULL;
8087 for (i = 0; i < map->n; ++i) {
8088 map->p[i] = isl_basic_map_reset(map->p[i], type);
8089 if (!map->p[i])
8090 goto error;
8092 map->dim = isl_dim_reset(map->dim, type);
8093 if (!map->dim)
8094 goto error;
8096 return map;
8097 error:
8098 isl_map_free(map);
8099 return NULL;
8102 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
8104 if (!bmap)
8105 return NULL;
8107 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
8108 return bmap;
8110 bmap = isl_basic_map_cow(bmap);
8111 if (!bmap)
8112 return NULL;
8114 bmap->dim = isl_dim_flatten(bmap->dim);
8115 if (!bmap->dim)
8116 goto error;
8118 bmap = isl_basic_map_finalize(bmap);
8120 return bmap;
8121 error:
8122 isl_basic_map_free(bmap);
8123 return NULL;
8126 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
8128 return (isl_basic_set *)isl_basic_map_flatten((isl_basic_map *)bset);
8131 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
8133 int i;
8135 if (!map)
8136 return NULL;
8138 if (!map->dim->nested[0] && !map->dim->nested[1])
8139 return map;
8141 map = isl_map_cow(map);
8142 if (!map)
8143 return NULL;
8145 for (i = 0; i < map->n; ++i) {
8146 map->p[i] = isl_basic_map_flatten(map->p[i]);
8147 if (!map->p[i])
8148 goto error;
8150 map->dim = isl_dim_flatten(map->dim);
8151 if (!map->dim)
8152 goto error;
8154 return map;
8155 error:
8156 isl_map_free(map);
8157 return NULL;
8160 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
8162 return (isl_set *)isl_map_flatten((isl_map *)set);
8165 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
8167 isl_dim *dim, *flat_dim;
8168 isl_map *map;
8170 dim = isl_set_get_dim(set);
8171 flat_dim = isl_dim_flatten(isl_dim_copy(dim));
8172 map = isl_map_identity(isl_dim_join(isl_dim_reverse(dim), flat_dim));
8173 map = isl_map_intersect_domain(map, set);
8175 return map;
8178 /* Extend the given dim_map with mappings for the divs in bmap.
8180 static __isl_give struct isl_dim_map *extend_dim_map(
8181 __isl_keep struct isl_dim_map *dim_map,
8182 __isl_keep isl_basic_map *bmap)
8184 int i;
8185 struct isl_dim_map *res;
8186 int offset;
8188 offset = isl_basic_map_offset(bmap, isl_dim_div);
8190 res = isl_dim_map_alloc(bmap->ctx, dim_map->len - 1 + bmap->n_div);
8191 if (!res)
8192 return NULL;
8194 for (i = 0; i < dim_map->len; ++i)
8195 res->pos[i] = dim_map->pos[i];
8196 for (i = 0; i < bmap->n_div; ++i)
8197 res->pos[dim_map->len + i] = offset + i;
8199 return res;
8202 /* Extract a dim_map from a reordering.
8203 * We essentially need to reverse the mapping, and add an offset
8204 * of 1 for the constant term.
8206 __isl_give struct isl_dim_map *isl_dim_map_from_reordering(
8207 __isl_keep isl_reordering *exp)
8209 int i;
8210 struct isl_dim_map *dim_map;
8212 if (!exp)
8213 return NULL;
8215 dim_map = isl_dim_map_alloc(exp->dim->ctx, isl_dim_total(exp->dim));
8216 if (!dim_map)
8217 return NULL;
8219 for (i = 0; i < exp->len; ++i)
8220 dim_map->pos[1 + exp->pos[i]] = 1 + i;
8222 return dim_map;
8225 /* Reorder the dimensions of "bmap" according to the given dim_map
8226 * and set the dimension specification to "dim".
8228 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
8229 __isl_take isl_dim *dim, __isl_take struct isl_dim_map *dim_map)
8231 isl_basic_map *res;
8233 bmap = isl_basic_map_cow(bmap);
8234 if (!bmap || !dim || !dim_map)
8235 goto error;
8237 res = isl_basic_map_alloc_dim(dim,
8238 bmap->n_div, bmap->n_eq, bmap->n_ineq);
8239 res = add_constraints_dim_map(res, bmap, dim_map);
8240 res = isl_basic_map_finalize(res);
8241 return res;
8242 error:
8243 free(dim_map);
8244 isl_basic_map_free(bmap);
8245 isl_dim_free(dim);
8246 return NULL;
8249 /* Reorder the dimensions of "map" according to given reordering.
8251 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
8252 __isl_take isl_reordering *r)
8254 int i;
8255 struct isl_dim_map *dim_map;
8257 map = isl_map_cow(map);
8258 dim_map = isl_dim_map_from_reordering(r);
8259 if (!map || !r || !dim_map)
8260 goto error;
8262 for (i = 0; i < map->n; ++i) {
8263 struct isl_dim_map *dim_map_i;
8265 dim_map_i = extend_dim_map(dim_map, map->p[i]);
8267 map->p[i] = isl_basic_map_realign(map->p[i],
8268 isl_dim_copy(r->dim), dim_map_i);
8270 if (!map->p[i])
8271 goto error;
8274 map = isl_map_reset_dim(map, isl_dim_copy(r->dim));
8276 isl_reordering_free(r);
8277 free(dim_map);
8278 return map;
8279 error:
8280 free(dim_map);
8281 isl_map_free(map);
8282 isl_reordering_free(r);
8283 return NULL;
8286 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
8287 __isl_take isl_reordering *r)
8289 return (isl_set *)isl_map_realign((isl_map *)set, r);
8292 __isl_give isl_mat *isl_basic_map_equalities_matrix(
8293 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
8294 enum isl_dim_type c2, enum isl_dim_type c3,
8295 enum isl_dim_type c4, enum isl_dim_type c5)
8297 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
8298 struct isl_mat *mat;
8299 int i, j, k;
8300 int pos;
8302 if (!bmap)
8303 return NULL;
8304 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
8305 isl_basic_map_total_dim(bmap) + 1);
8306 if (!mat)
8307 return NULL;
8308 for (i = 0; i < bmap->n_eq; ++i)
8309 for (j = 0, pos = 0; j < 5; ++j) {
8310 int off = isl_basic_map_offset(bmap, c[j]);
8311 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
8312 isl_int_set(mat->row[i][pos],
8313 bmap->eq[i][off + k]);
8314 ++pos;
8318 return mat;
8321 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
8322 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
8323 enum isl_dim_type c2, enum isl_dim_type c3,
8324 enum isl_dim_type c4, enum isl_dim_type c5)
8326 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
8327 struct isl_mat *mat;
8328 int i, j, k;
8329 int pos;
8331 if (!bmap)
8332 return NULL;
8333 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
8334 isl_basic_map_total_dim(bmap) + 1);
8335 if (!mat)
8336 return NULL;
8337 for (i = 0; i < bmap->n_ineq; ++i)
8338 for (j = 0, pos = 0; j < 5; ++j) {
8339 int off = isl_basic_map_offset(bmap, c[j]);
8340 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
8341 isl_int_set(mat->row[i][pos],
8342 bmap->ineq[i][off + k]);
8343 ++pos;
8347 return mat;
8350 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
8351 __isl_take isl_dim *dim,
8352 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
8353 enum isl_dim_type c2, enum isl_dim_type c3,
8354 enum isl_dim_type c4, enum isl_dim_type c5)
8356 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
8357 isl_basic_map *bmap;
8358 unsigned total;
8359 unsigned extra;
8360 int i, j, k, l;
8361 int pos;
8363 if (!dim || !eq || !ineq)
8364 goto error;
8366 if (eq->n_col != ineq->n_col)
8367 isl_die(dim->ctx, isl_error_invalid,
8368 "equalities and inequalities matrices should have "
8369 "same number of columns", goto error);
8371 total = 1 + isl_dim_total(dim);
8373 if (eq->n_col < total)
8374 isl_die(dim->ctx, isl_error_invalid,
8375 "number of columns too small", goto error);
8377 extra = eq->n_col - total;
8379 bmap = isl_basic_map_alloc_dim(isl_dim_copy(dim), extra,
8380 eq->n_row, ineq->n_row);
8381 if (!bmap)
8382 goto error;
8383 for (i = 0; i < extra; ++i) {
8384 k = isl_basic_map_alloc_div(bmap);
8385 if (k < 0)
8386 goto error;
8387 isl_int_set_si(bmap->div[k][0], 0);
8389 for (i = 0; i < eq->n_row; ++i) {
8390 l = isl_basic_map_alloc_equality(bmap);
8391 if (l < 0)
8392 goto error;
8393 for (j = 0, pos = 0; j < 5; ++j) {
8394 int off = isl_basic_map_offset(bmap, c[j]);
8395 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
8396 isl_int_set(bmap->eq[l][off + k],
8397 eq->row[i][pos]);
8398 ++pos;
8402 for (i = 0; i < ineq->n_row; ++i) {
8403 l = isl_basic_map_alloc_inequality(bmap);
8404 if (l < 0)
8405 goto error;
8406 for (j = 0, pos = 0; j < 5; ++j) {
8407 int off = isl_basic_map_offset(bmap, c[j]);
8408 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
8409 isl_int_set(bmap->ineq[l][off + k],
8410 ineq->row[i][pos]);
8411 ++pos;
8416 isl_dim_free(dim);
8417 isl_mat_free(eq);
8418 isl_mat_free(ineq);
8420 return bmap;
8421 error:
8422 isl_dim_free(dim);
8423 isl_mat_free(eq);
8424 isl_mat_free(ineq);
8425 return NULL;
8428 __isl_give isl_mat *isl_basic_set_equalities_matrix(
8429 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
8430 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
8432 return isl_basic_map_equalities_matrix((isl_basic_map *)bset,
8433 c1, c2, c3, c4, isl_dim_in);
8436 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
8437 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
8438 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
8440 return isl_basic_map_inequalities_matrix((isl_basic_map *)bset,
8441 c1, c2, c3, c4, isl_dim_in);
8444 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
8445 __isl_take isl_dim *dim,
8446 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
8447 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
8449 return (isl_basic_set*)
8450 isl_basic_map_from_constraint_matrices(dim, eq, ineq,
8451 c1, c2, c3, c4, isl_dim_in);
8454 int isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
8456 if (!bmap)
8457 return -1;
8459 return isl_dim_can_zip(bmap->dim);
8462 int isl_map_can_zip(__isl_keep isl_map *map)
8464 if (!map)
8465 return -1;
8467 return isl_dim_can_zip(map->dim);
8470 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
8471 * (A -> C) -> (B -> D).
8473 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
8475 unsigned pos;
8476 unsigned n1;
8477 unsigned n2;
8479 if (!bmap)
8480 return NULL;
8482 if (!isl_basic_map_can_zip(bmap))
8483 isl_die(bmap->ctx, isl_error_invalid,
8484 "basic map cannot be zipped", goto error);
8485 pos = isl_basic_map_offset(bmap, isl_dim_in) +
8486 isl_dim_size(bmap->dim->nested[0], isl_dim_in);
8487 n1 = isl_dim_size(bmap->dim->nested[0], isl_dim_out);
8488 n2 = isl_dim_size(bmap->dim->nested[1], isl_dim_in);
8489 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
8490 if (!bmap)
8491 return NULL;
8492 bmap->dim = isl_dim_zip(bmap->dim);
8493 if (!bmap->dim)
8494 goto error;
8495 return bmap;
8496 error:
8497 isl_basic_map_free(bmap);
8498 return NULL;
8501 /* Given a map (A -> B) -> (C -> D), return the corresponding map
8502 * (A -> C) -> (B -> D).
8504 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
8506 int i;
8508 if (!map)
8509 return NULL;
8511 if (!isl_map_can_zip(map))
8512 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
8513 goto error);
8515 map = isl_map_cow(map);
8516 if (!map)
8517 return NULL;
8519 for (i = 0; i < map->n; ++i) {
8520 map->p[i] = isl_basic_map_zip(map->p[i]);
8521 if (!map->p[i])
8522 goto error;
8525 map->dim = isl_dim_zip(map->dim);
8526 if (!map->dim)
8527 goto error;
8529 return map;
8530 error:
8531 isl_map_free(map);
8532 return NULL;