add isl_pw_qpolynomial_split_periods
[isl.git] / isl_map.c
blobc6eeaf8f76e6e85d70ba1f6c77cbc8c21620428d
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
5 * Use of this software is governed by the GNU LGPLv2.1 license
7 * Written by Sven Verdoolaege, K.U.Leuven, Departement
8 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
9 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
10 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
13 #include <string.h>
14 #include <strings.h>
15 #include "isl_ctx.h"
16 #include "isl_blk.h"
17 #include "isl_dim_private.h"
18 #include "isl_equalities.h"
19 #include "isl_list.h"
20 #include "isl_lp.h"
21 #include "isl_seq.h"
22 #include "isl_set.h"
23 #include "isl_map.h"
24 #include "isl_map_private.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"
31 /* Maps dst positions to src positions */
32 struct isl_dim_map {
33 unsigned len;
34 int pos[1];
37 static struct isl_dim_map *isl_dim_map_alloc(struct isl_ctx *ctx, unsigned len)
39 int i;
40 struct isl_dim_map *dim_map;
41 dim_map = isl_alloc(ctx, struct isl_dim_map,
42 sizeof(struct isl_dim_map) + len * sizeof(int));
43 if (!dim_map)
44 return NULL;
45 dim_map->len = 1 + len;
46 dim_map->pos[0] = 0;
47 for (i = 0; i < len; ++i)
48 dim_map->pos[1 + i] = -1;
49 return dim_map;
52 static unsigned n(struct isl_dim *dim, enum isl_dim_type type)
54 switch (type) {
55 case isl_dim_param: return dim->nparam;
56 case isl_dim_in: return dim->n_in;
57 case isl_dim_out: return dim->n_out;
58 case isl_dim_all: return dim->nparam + dim->n_in + dim->n_out;
59 default: return 0;
63 static unsigned pos(struct isl_dim *dim, enum isl_dim_type type)
65 switch (type) {
66 case isl_dim_param: return 1;
67 case isl_dim_in: return 1 + dim->nparam;
68 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
69 default: return 0;
73 static void isl_dim_map_dim_range(struct isl_dim_map *dim_map,
74 struct isl_dim *dim, enum isl_dim_type type,
75 unsigned first, unsigned n, unsigned dst_pos)
77 int i;
78 unsigned src_pos;
80 if (!dim_map || !dim)
81 return;
83 src_pos = pos(dim, type);
84 for (i = 0; i < n; ++i)
85 dim_map->pos[1 + dst_pos + i] = src_pos + first + i;
88 static void isl_dim_map_dim(struct isl_dim_map *dim_map, struct isl_dim *dim,
89 enum isl_dim_type type, unsigned dst_pos)
91 isl_dim_map_dim_range(dim_map, dim, type, 0, n(dim, type), dst_pos);
94 static void isl_dim_map_div(struct isl_dim_map *dim_map,
95 struct isl_basic_map *bmap, unsigned dst_pos)
97 int i;
98 unsigned src_pos;
100 if (!dim_map || !bmap)
101 return;
103 src_pos = 1 + isl_dim_total(bmap->dim);
104 for (i = 0; i < bmap->n_div; ++i)
105 dim_map->pos[1 + dst_pos + i] = src_pos + i;
108 static void isl_dim_map_dump(struct isl_dim_map *dim_map)
110 int i;
112 for (i = 0; i < dim_map->len; ++i)
113 fprintf(stderr, "%d -> %d; ", i, dim_map->pos[i]);
114 fprintf(stderr, "\n");
117 unsigned isl_basic_map_dim(const struct isl_basic_map *bmap,
118 enum isl_dim_type type)
120 if (!bmap)
121 return 0;
122 switch (type) {
123 case isl_dim_param:
124 case isl_dim_in:
125 case isl_dim_out: return isl_dim_size(bmap->dim, type);
126 case isl_dim_div: return bmap->n_div;
127 case isl_dim_all: return isl_basic_map_total_dim(bmap);
128 default: return 0;
132 unsigned isl_map_dim(const struct isl_map *map, enum isl_dim_type type)
134 return map ? n(map->dim, type) : 0;
137 unsigned isl_set_dim(const struct isl_set *set, enum isl_dim_type type)
139 return set ? n(set->dim, type) : 0;
142 unsigned isl_basic_map_offset(struct isl_basic_map *bmap,
143 enum isl_dim_type type)
145 struct isl_dim *dim = bmap->dim;
146 switch (type) {
147 case isl_dim_param: return 1;
148 case isl_dim_in: return 1 + dim->nparam;
149 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
150 case isl_dim_div: return 1 + dim->nparam + dim->n_in + dim->n_out;
151 default: return 0;
155 static unsigned map_offset(struct isl_map *map, enum isl_dim_type type)
157 return pos(map->dim, type);
160 unsigned isl_basic_set_dim(const struct isl_basic_set *bset,
161 enum isl_dim_type type)
163 return isl_basic_map_dim((const struct isl_basic_map*)bset, type);
166 unsigned isl_basic_set_n_dim(const struct isl_basic_set *bset)
168 return isl_basic_set_dim(bset, isl_dim_set);
171 unsigned isl_basic_set_n_param(const struct isl_basic_set *bset)
173 return isl_basic_set_dim(bset, isl_dim_param);
176 unsigned isl_basic_set_total_dim(const struct isl_basic_set *bset)
178 return isl_dim_total(bset->dim) + bset->n_div;
181 unsigned isl_set_n_dim(const struct isl_set *set)
183 return isl_set_dim(set, isl_dim_set);
186 unsigned isl_set_n_param(const struct isl_set *set)
188 return isl_set_dim(set, isl_dim_param);
191 unsigned isl_basic_map_n_in(const struct isl_basic_map *bmap)
193 return bmap ? bmap->dim->n_in : 0;
196 unsigned isl_basic_map_n_out(const struct isl_basic_map *bmap)
198 return bmap ? bmap->dim->n_out : 0;
201 unsigned isl_basic_map_n_param(const struct isl_basic_map *bmap)
203 return bmap ? bmap->dim->nparam : 0;
206 unsigned isl_basic_map_n_div(const struct isl_basic_map *bmap)
208 return bmap ? bmap->n_div : 0;
211 unsigned isl_basic_map_total_dim(const struct isl_basic_map *bmap)
213 return bmap ? isl_dim_total(bmap->dim) + bmap->n_div : 0;
216 unsigned isl_map_n_in(const struct isl_map *map)
218 return map->dim->n_in;
221 unsigned isl_map_n_out(const struct isl_map *map)
223 return map->dim->n_out;
226 unsigned isl_map_n_param(const struct isl_map *map)
228 return map->dim->nparam;
231 int isl_map_compatible_domain(struct isl_map *map, struct isl_set *set)
233 int m;
234 if (!map || !set)
235 return -1;
236 m = isl_dim_match(map->dim, isl_dim_param, set->dim, isl_dim_param);
237 if (m < 0 || !m)
238 return m;
239 return isl_dim_tuple_match(map->dim, isl_dim_in, set->dim, isl_dim_set);
242 int isl_basic_map_compatible_domain(struct isl_basic_map *bmap,
243 struct isl_basic_set *bset)
245 int m;
246 if (!bmap || !bset)
247 return -1;
248 m = isl_dim_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
249 if (m < 0 || !m)
250 return m;
251 return isl_dim_tuple_match(bmap->dim, isl_dim_in, bset->dim, isl_dim_set);
254 int isl_basic_map_compatible_range(struct isl_basic_map *bmap,
255 struct isl_basic_set *bset)
257 int m;
258 if (!bmap || !bset)
259 return -1;
260 m = isl_dim_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
261 if (m < 0 || !m)
262 return m;
263 return isl_dim_tuple_match(bmap->dim, isl_dim_out, bset->dim, isl_dim_set);
266 isl_ctx *isl_basic_map_get_ctx(__isl_keep isl_basic_map *bmap)
268 return bmap ? bmap->ctx : NULL;
271 isl_ctx *isl_basic_set_get_ctx(__isl_keep isl_basic_set *bset)
273 return bset ? bset->ctx : NULL;
276 isl_ctx *isl_map_get_ctx(__isl_keep isl_map *map)
278 return map ? map->ctx : NULL;
281 isl_ctx *isl_set_get_ctx(__isl_keep isl_set *set)
283 return set ? set->ctx : NULL;
286 struct isl_dim *isl_basic_map_get_dim(struct isl_basic_map *bmap)
288 if (!bmap)
289 return NULL;
290 return isl_dim_copy(bmap->dim);
293 struct isl_dim *isl_basic_set_get_dim(struct isl_basic_set *bset)
295 if (!bset)
296 return NULL;
297 return isl_dim_copy(bset->dim);
300 struct isl_dim *isl_map_get_dim(struct isl_map *map)
302 if (!map)
303 return NULL;
304 return isl_dim_copy(map->dim);
307 struct isl_dim *isl_set_get_dim(struct isl_set *set)
309 if (!set)
310 return NULL;
311 return isl_dim_copy(set->dim);
314 __isl_give isl_basic_map *isl_basic_map_set_tuple_name(
315 __isl_take isl_basic_map *bmap, enum isl_dim_type type, const char *s)
317 bmap = isl_basic_map_cow(bmap);
318 if (!bmap)
319 return NULL;
320 bmap->dim = isl_dim_set_tuple_name(bmap->dim, type, s);
321 if (!bmap->dim)
322 goto error;
323 return bmap;
324 error:
325 isl_basic_map_free(bmap);
326 return NULL;
329 __isl_give isl_map *isl_map_set_tuple_name(__isl_take isl_map *map,
330 enum isl_dim_type type, const char *s)
332 int i;
334 map = isl_map_cow(map);
335 if (!map)
336 return NULL;
338 map->dim = isl_dim_set_tuple_name(map->dim, type, s);
339 if (!map->dim)
340 goto error;
342 for (i = 0; i < map->n; ++i) {
343 map->p[i] = isl_basic_map_set_tuple_name(map->p[i], type, s);
344 if (!map->p[i])
345 goto error;
348 return map;
349 error:
350 isl_map_free(map);
351 return NULL;
354 __isl_give isl_basic_map *isl_basic_map_set_dim_name(
355 __isl_take isl_basic_map *bmap,
356 enum isl_dim_type type, unsigned pos, const char *s)
358 if (!bmap)
359 return NULL;
360 bmap->dim = isl_dim_set_name(bmap->dim, type, pos, s);
361 if (!bmap->dim)
362 goto error;
363 return bmap;
364 error:
365 isl_basic_map_free(bmap);
366 return NULL;
369 __isl_give isl_map *isl_map_set_dim_name(__isl_take isl_map *map,
370 enum isl_dim_type type, unsigned pos, const char *s)
372 int i;
374 if (!map)
375 return NULL;
377 map->dim = isl_dim_set_name(map->dim, type, pos, s);
378 if (!map->dim)
379 goto error;
381 for (i = 0; i < map->n; ++i) {
382 map->p[i] = isl_basic_map_set_dim_name(map->p[i], type, pos, s);
383 if (!map->p[i])
384 goto error;
387 return map;
388 error:
389 isl_map_free(map);
390 return NULL;
393 __isl_give isl_basic_set *isl_basic_set_set_dim_name(
394 __isl_take isl_basic_set *bset,
395 enum isl_dim_type type, unsigned pos, const char *s)
397 return (isl_basic_set *)isl_basic_map_set_dim_name(
398 (isl_basic_map *)bset, type, pos, s);
401 __isl_give isl_set *isl_set_set_dim_name(__isl_take isl_set *set,
402 enum isl_dim_type type, unsigned pos, const char *s)
404 return (isl_set *)isl_map_set_dim_name((isl_map *)set, type, pos, s);
407 int isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
409 if (!bmap)
410 return -1;
411 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
414 static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
415 struct isl_basic_map *bmap, unsigned extra,
416 unsigned n_eq, unsigned n_ineq)
418 int i;
419 size_t row_size = 1 + isl_dim_total(bmap->dim) + extra;
421 bmap->ctx = ctx;
422 isl_ctx_ref(ctx);
424 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
425 if (isl_blk_is_error(bmap->block))
426 goto error;
428 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
429 if (!bmap->ineq)
430 goto error;
432 if (extra == 0) {
433 bmap->block2 = isl_blk_empty();
434 bmap->div = NULL;
435 } else {
436 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
437 if (isl_blk_is_error(bmap->block2))
438 goto error;
440 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
441 if (!bmap->div)
442 goto error;
445 for (i = 0; i < n_ineq + n_eq; ++i)
446 bmap->ineq[i] = bmap->block.data + i * row_size;
448 for (i = 0; i < extra; ++i)
449 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
451 bmap->ref = 1;
452 bmap->flags = 0;
453 bmap->c_size = n_eq + n_ineq;
454 bmap->eq = bmap->ineq + n_ineq;
455 bmap->extra = extra;
456 bmap->n_eq = 0;
457 bmap->n_ineq = 0;
458 bmap->n_div = 0;
459 bmap->sample = NULL;
461 return bmap;
462 error:
463 isl_basic_map_free(bmap);
464 return NULL;
467 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
468 unsigned nparam, unsigned dim, unsigned extra,
469 unsigned n_eq, unsigned n_ineq)
471 struct isl_basic_map *bmap;
472 bmap = isl_basic_map_alloc(ctx, nparam, 0, dim, extra, n_eq, n_ineq);
473 return (struct isl_basic_set *)bmap;
476 struct isl_basic_set *isl_basic_set_alloc_dim(struct isl_dim *dim,
477 unsigned extra, unsigned n_eq, unsigned n_ineq)
479 struct isl_basic_map *bmap;
480 if (!dim)
481 return NULL;
482 isl_assert(dim->ctx, dim->n_in == 0, return NULL);
483 bmap = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
484 return (struct isl_basic_set *)bmap;
487 struct isl_basic_map *isl_basic_map_alloc_dim(struct isl_dim *dim,
488 unsigned extra, unsigned n_eq, unsigned n_ineq)
490 struct isl_basic_map *bmap;
492 if (!dim)
493 return NULL;
494 bmap = isl_calloc_type(dim->ctx, struct isl_basic_map);
495 if (!bmap)
496 goto error;
497 bmap->dim = dim;
499 return basic_map_init(dim->ctx, bmap, extra, n_eq, n_ineq);
500 error:
501 isl_dim_free(dim);
502 return NULL;
505 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
506 unsigned nparam, unsigned in, unsigned out, unsigned extra,
507 unsigned n_eq, unsigned n_ineq)
509 struct isl_basic_map *bmap;
510 struct isl_dim *dim;
512 dim = isl_dim_alloc(ctx, nparam, in, out);
513 if (!dim)
514 return NULL;
516 bmap = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
517 return bmap;
520 static void dup_constraints(
521 struct isl_basic_map *dst, struct isl_basic_map *src)
523 int i;
524 unsigned total = isl_basic_map_total_dim(src);
526 for (i = 0; i < src->n_eq; ++i) {
527 int j = isl_basic_map_alloc_equality(dst);
528 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
531 for (i = 0; i < src->n_ineq; ++i) {
532 int j = isl_basic_map_alloc_inequality(dst);
533 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
536 for (i = 0; i < src->n_div; ++i) {
537 int j = isl_basic_map_alloc_div(dst);
538 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
540 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
543 struct isl_basic_map *isl_basic_map_dup(struct isl_basic_map *bmap)
545 struct isl_basic_map *dup;
547 if (!bmap)
548 return NULL;
549 dup = isl_basic_map_alloc_dim(isl_dim_copy(bmap->dim),
550 bmap->n_div, bmap->n_eq, bmap->n_ineq);
551 if (!dup)
552 return NULL;
553 dup_constraints(dup, bmap);
554 dup->flags = bmap->flags;
555 dup->sample = isl_vec_copy(bmap->sample);
556 return dup;
559 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
561 struct isl_basic_map *dup;
563 dup = isl_basic_map_dup((struct isl_basic_map *)bset);
564 return (struct isl_basic_set *)dup;
567 struct isl_basic_set *isl_basic_set_copy(struct isl_basic_set *bset)
569 if (!bset)
570 return NULL;
572 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
573 bset->ref++;
574 return bset;
576 return isl_basic_set_dup(bset);
579 struct isl_set *isl_set_copy(struct isl_set *set)
581 if (!set)
582 return NULL;
584 set->ref++;
585 return set;
588 struct isl_basic_map *isl_basic_map_copy(struct isl_basic_map *bmap)
590 if (!bmap)
591 return NULL;
593 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
594 bmap->ref++;
595 return bmap;
597 bmap = isl_basic_map_dup(bmap);
598 if (bmap)
599 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
600 return bmap;
603 struct isl_map *isl_map_copy(struct isl_map *map)
605 if (!map)
606 return NULL;
608 map->ref++;
609 return map;
612 void isl_basic_map_free(struct isl_basic_map *bmap)
614 if (!bmap)
615 return;
617 if (--bmap->ref > 0)
618 return;
620 isl_ctx_deref(bmap->ctx);
621 free(bmap->div);
622 isl_blk_free(bmap->ctx, bmap->block2);
623 free(bmap->ineq);
624 isl_blk_free(bmap->ctx, bmap->block);
625 isl_vec_free(bmap->sample);
626 isl_dim_free(bmap->dim);
627 free(bmap);
630 void isl_basic_set_free(struct isl_basic_set *bset)
632 isl_basic_map_free((struct isl_basic_map *)bset);
635 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
637 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
640 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
642 struct isl_ctx *ctx;
643 if (!bmap)
644 return -1;
645 ctx = bmap->ctx;
646 isl_assert(ctx, room_for_con(bmap, 1), return -1);
647 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
648 return -1);
649 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
650 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
651 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
652 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
653 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
654 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
655 isl_int *t;
656 int j = isl_basic_map_alloc_inequality(bmap);
657 if (j < 0)
658 return -1;
659 t = bmap->ineq[j];
660 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
661 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
662 bmap->eq[-1] = t;
663 bmap->n_eq++;
664 bmap->n_ineq--;
665 bmap->eq--;
666 return 0;
668 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
669 bmap->extra - bmap->n_div);
670 return bmap->n_eq++;
673 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
675 return isl_basic_map_alloc_equality((struct isl_basic_map *)bset);
678 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
680 if (!bmap)
681 return -1;
682 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
683 bmap->n_eq -= n;
684 return 0;
687 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
689 return isl_basic_map_free_equality((struct isl_basic_map *)bset, n);
692 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
694 isl_int *t;
695 if (!bmap)
696 return -1;
697 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
699 if (pos != bmap->n_eq - 1) {
700 t = bmap->eq[pos];
701 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
702 bmap->eq[bmap->n_eq - 1] = t;
704 bmap->n_eq--;
705 return 0;
708 int isl_basic_set_drop_equality(struct isl_basic_set *bset, unsigned pos)
710 return isl_basic_map_drop_equality((struct isl_basic_map *)bset, pos);
713 void isl_basic_map_inequality_to_equality(
714 struct isl_basic_map *bmap, unsigned pos)
716 isl_int *t;
718 t = bmap->ineq[pos];
719 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
720 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
721 bmap->eq[-1] = t;
722 bmap->n_eq++;
723 bmap->n_ineq--;
724 bmap->eq--;
725 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
726 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
727 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
728 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
731 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
733 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
736 int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap)
738 struct isl_ctx *ctx;
739 if (!bmap)
740 return -1;
741 ctx = bmap->ctx;
742 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
743 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
744 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
745 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
746 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
747 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
748 1 + isl_basic_map_total_dim(bmap),
749 bmap->extra - bmap->n_div);
750 return bmap->n_ineq++;
753 int isl_basic_set_alloc_inequality(struct isl_basic_set *bset)
755 return isl_basic_map_alloc_inequality((struct isl_basic_map *)bset);
758 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
760 if (!bmap)
761 return -1;
762 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
763 bmap->n_ineq -= n;
764 return 0;
767 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
769 return isl_basic_map_free_inequality((struct isl_basic_map *)bset, n);
772 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
774 isl_int *t;
775 if (!bmap)
776 return -1;
777 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
779 if (pos != bmap->n_ineq - 1) {
780 t = bmap->ineq[pos];
781 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
782 bmap->ineq[bmap->n_ineq - 1] = t;
783 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
785 bmap->n_ineq--;
786 return 0;
789 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
791 return isl_basic_map_drop_inequality((struct isl_basic_map *)bset, pos);
794 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
795 isl_int *eq)
797 int k;
799 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
800 if (!bmap)
801 return NULL;
802 k = isl_basic_map_alloc_equality(bmap);
803 if (k < 0)
804 goto error;
805 isl_seq_cpy(bmap->eq[k], eq, 1 + isl_basic_map_total_dim(bmap));
806 return bmap;
807 error:
808 isl_basic_map_free(bmap);
809 return NULL;
812 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
813 isl_int *eq)
815 return (isl_basic_set *)
816 isl_basic_map_add_eq((isl_basic_map *)bset, eq);
819 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
820 isl_int *ineq)
822 int k;
824 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
825 if (!bmap)
826 return NULL;
827 k = isl_basic_map_alloc_inequality(bmap);
828 if (k < 0)
829 goto error;
830 isl_seq_cpy(bmap->ineq[k], ineq, 1 + isl_basic_map_total_dim(bmap));
831 return bmap;
832 error:
833 isl_basic_map_free(bmap);
834 return NULL;
837 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
838 isl_int *ineq)
840 return (isl_basic_set *)
841 isl_basic_map_add_ineq((isl_basic_map *)bset, ineq);
844 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
846 if (!bmap)
847 return -1;
848 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
849 isl_seq_clr(bmap->div[bmap->n_div] +
850 1 + 1 + isl_basic_map_total_dim(bmap),
851 bmap->extra - bmap->n_div);
852 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
853 return bmap->n_div++;
856 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
858 return isl_basic_map_alloc_div((struct isl_basic_map *)bset);
861 int isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
863 if (!bmap)
864 return -1;
865 isl_assert(bmap->ctx, n <= bmap->n_div, return -1);
866 bmap->n_div -= n;
867 return 0;
870 int isl_basic_set_free_div(struct isl_basic_set *bset, unsigned n)
872 return isl_basic_map_free_div((struct isl_basic_map *)bset, n);
875 /* Copy constraint from src to dst, putting the vars of src at offset
876 * dim_off in dst and the divs of src at offset div_off in dst.
877 * If both sets are actually map, then dim_off applies to the input
878 * variables.
880 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
881 struct isl_basic_map *src_map, isl_int *src,
882 unsigned in_off, unsigned out_off, unsigned div_off)
884 unsigned src_nparam = isl_basic_map_n_param(src_map);
885 unsigned dst_nparam = isl_basic_map_n_param(dst_map);
886 unsigned src_in = isl_basic_map_n_in(src_map);
887 unsigned dst_in = isl_basic_map_n_in(dst_map);
888 unsigned src_out = isl_basic_map_n_out(src_map);
889 unsigned dst_out = isl_basic_map_n_out(dst_map);
890 isl_int_set(dst[0], src[0]);
891 isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
892 if (dst_nparam > src_nparam)
893 isl_seq_clr(dst+1+src_nparam,
894 dst_nparam - src_nparam);
895 isl_seq_clr(dst+1+dst_nparam, in_off);
896 isl_seq_cpy(dst+1+dst_nparam+in_off,
897 src+1+src_nparam,
898 isl_min(dst_in-in_off, src_in));
899 if (dst_in-in_off > src_in)
900 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
901 dst_in - in_off - src_in);
902 isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
903 isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
904 src+1+src_nparam+src_in,
905 isl_min(dst_out-out_off, src_out));
906 if (dst_out-out_off > src_out)
907 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
908 dst_out - out_off - src_out);
909 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
910 isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
911 src+1+src_nparam+src_in+src_out,
912 isl_min(dst_map->extra-div_off, src_map->n_div));
913 if (dst_map->n_div-div_off > src_map->n_div)
914 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
915 div_off+src_map->n_div,
916 dst_map->n_div - div_off - src_map->n_div);
919 static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
920 struct isl_basic_map *src_map, isl_int *src,
921 unsigned in_off, unsigned out_off, unsigned div_off)
923 isl_int_set(dst[0], src[0]);
924 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
927 static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
928 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
930 int i;
931 unsigned div_off;
933 if (!bmap1 || !bmap2)
934 goto error;
936 div_off = bmap1->n_div;
938 for (i = 0; i < bmap2->n_eq; ++i) {
939 int i1 = isl_basic_map_alloc_equality(bmap1);
940 if (i1 < 0)
941 goto error;
942 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
943 i_pos, o_pos, div_off);
946 for (i = 0; i < bmap2->n_ineq; ++i) {
947 int i1 = isl_basic_map_alloc_inequality(bmap1);
948 if (i1 < 0)
949 goto error;
950 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
951 i_pos, o_pos, div_off);
954 for (i = 0; i < bmap2->n_div; ++i) {
955 int i1 = isl_basic_map_alloc_div(bmap1);
956 if (i1 < 0)
957 goto error;
958 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
959 i_pos, o_pos, div_off);
962 isl_basic_map_free(bmap2);
964 return bmap1;
966 error:
967 isl_basic_map_free(bmap1);
968 isl_basic_map_free(bmap2);
969 return NULL;
972 static void copy_constraint_dim_map(isl_int *dst, isl_int *src,
973 struct isl_dim_map *dim_map)
975 int i;
977 for (i = 0; i < dim_map->len; ++i) {
978 if (dim_map->pos[i] < 0)
979 isl_int_set_si(dst[i], 0);
980 else
981 isl_int_set(dst[i], src[dim_map->pos[i]]);
985 static void copy_div_dim_map(isl_int *dst, isl_int *src,
986 struct isl_dim_map *dim_map)
988 isl_int_set(dst[0], src[0]);
989 copy_constraint_dim_map(dst+1, src+1, dim_map);
992 static struct isl_basic_map *add_constraints_dim_map(struct isl_basic_map *dst,
993 struct isl_basic_map *src, struct isl_dim_map *dim_map)
995 int i;
997 if (!src || !dst || !dim_map)
998 goto error;
1000 for (i = 0; i < src->n_eq; ++i) {
1001 int i1 = isl_basic_map_alloc_equality(dst);
1002 if (i1 < 0)
1003 goto error;
1004 copy_constraint_dim_map(dst->eq[i1], src->eq[i], dim_map);
1007 for (i = 0; i < src->n_ineq; ++i) {
1008 int i1 = isl_basic_map_alloc_inequality(dst);
1009 if (i1 < 0)
1010 goto error;
1011 copy_constraint_dim_map(dst->ineq[i1], src->ineq[i], dim_map);
1014 for (i = 0; i < src->n_div; ++i) {
1015 int i1 = isl_basic_map_alloc_div(dst);
1016 if (i1 < 0)
1017 goto error;
1018 copy_div_dim_map(dst->div[i1], src->div[i], dim_map);
1021 free(dim_map);
1022 isl_basic_map_free(src);
1024 return dst;
1025 error:
1026 free(dim_map);
1027 isl_basic_map_free(src);
1028 isl_basic_map_free(dst);
1029 return NULL;
1032 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
1033 struct isl_basic_set *bset2, unsigned pos)
1035 return (struct isl_basic_set *)
1036 add_constraints((struct isl_basic_map *)bset1,
1037 (struct isl_basic_map *)bset2, 0, pos);
1040 struct isl_basic_map *isl_basic_map_extend_dim(struct isl_basic_map *base,
1041 struct isl_dim *dim, unsigned extra,
1042 unsigned n_eq, unsigned n_ineq)
1044 struct isl_basic_map *ext;
1045 unsigned flags;
1046 int dims_ok;
1048 if (!dim)
1049 goto error;
1051 if (!base)
1052 goto error;
1054 dims_ok = isl_dim_equal(base->dim, dim) &&
1055 base->extra >= base->n_div + extra;
1057 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1058 room_for_ineq(base, n_ineq)) {
1059 isl_dim_free(dim);
1060 return base;
1063 isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error);
1064 isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error);
1065 isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error);
1066 extra += base->extra;
1067 n_eq += base->n_eq;
1068 n_ineq += base->n_ineq;
1070 ext = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
1071 dim = NULL;
1072 if (!ext)
1073 goto error;
1075 if (dims_ok)
1076 ext->sample = isl_vec_copy(base->sample);
1077 flags = base->flags;
1078 ext = add_constraints(ext, base, 0, 0);
1079 if (ext) {
1080 ext->flags = flags;
1081 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
1084 return ext;
1086 error:
1087 isl_dim_free(dim);
1088 isl_basic_map_free(base);
1089 return NULL;
1092 struct isl_basic_set *isl_basic_set_extend_dim(struct isl_basic_set *base,
1093 struct isl_dim *dim, unsigned extra,
1094 unsigned n_eq, unsigned n_ineq)
1096 return (struct isl_basic_set *)
1097 isl_basic_map_extend_dim((struct isl_basic_map *)base, dim,
1098 extra, n_eq, n_ineq);
1101 struct isl_basic_map *isl_basic_map_extend_constraints(
1102 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1104 if (!base)
1105 return NULL;
1106 return isl_basic_map_extend_dim(base, isl_dim_copy(base->dim),
1107 0, n_eq, n_ineq);
1110 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
1111 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
1112 unsigned n_eq, unsigned n_ineq)
1114 struct isl_basic_map *bmap;
1115 struct isl_dim *dim;
1117 if (!base)
1118 return NULL;
1119 dim = isl_dim_alloc(base->ctx, nparam, n_in, n_out);
1120 if (!dim)
1121 goto error;
1123 bmap = isl_basic_map_extend_dim(base, dim, extra, n_eq, n_ineq);
1124 return bmap;
1125 error:
1126 isl_basic_map_free(base);
1127 return NULL;
1130 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
1131 unsigned nparam, unsigned dim, unsigned extra,
1132 unsigned n_eq, unsigned n_ineq)
1134 return (struct isl_basic_set *)
1135 isl_basic_map_extend((struct isl_basic_map *)base,
1136 nparam, 0, dim, extra, n_eq, n_ineq);
1139 struct isl_basic_set *isl_basic_set_extend_constraints(
1140 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1142 return (struct isl_basic_set *)
1143 isl_basic_map_extend_constraints((struct isl_basic_map *)base,
1144 n_eq, n_ineq);
1147 struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset)
1149 return (struct isl_basic_set *)
1150 isl_basic_map_cow((struct isl_basic_map *)bset);
1153 struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
1155 if (!bmap)
1156 return NULL;
1158 if (bmap->ref > 1) {
1159 bmap->ref--;
1160 bmap = isl_basic_map_dup(bmap);
1162 if (bmap)
1163 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
1164 return bmap;
1167 struct isl_set *isl_set_cow(struct isl_set *set)
1169 if (!set)
1170 return NULL;
1172 if (set->ref == 1)
1173 return set;
1174 set->ref--;
1175 return isl_set_dup(set);
1178 struct isl_map *isl_map_cow(struct isl_map *map)
1180 if (!map)
1181 return NULL;
1183 if (map->ref == 1)
1184 return map;
1185 map->ref--;
1186 return isl_map_dup(map);
1189 static void swap_vars(struct isl_blk blk, isl_int *a,
1190 unsigned a_len, unsigned b_len)
1192 isl_seq_cpy(blk.data, a+a_len, b_len);
1193 isl_seq_cpy(blk.data+b_len, a, a_len);
1194 isl_seq_cpy(a, blk.data, b_len+a_len);
1197 struct isl_basic_set *isl_basic_set_swap_vars(
1198 struct isl_basic_set *bset, unsigned n)
1200 int i;
1201 struct isl_blk blk;
1202 unsigned dim;
1203 unsigned nparam;
1205 if (!bset)
1206 goto error;
1208 nparam = isl_basic_set_n_param(bset);
1209 dim = isl_basic_set_n_dim(bset);
1210 isl_assert(bset->ctx, n <= dim, goto error);
1212 if (n == dim)
1213 return bset;
1215 bset = isl_basic_set_cow(bset);
1216 if (!bset)
1217 return NULL;
1219 blk = isl_blk_alloc(bset->ctx, dim);
1220 if (isl_blk_is_error(blk))
1221 goto error;
1223 for (i = 0; i < bset->n_eq; ++i)
1224 swap_vars(blk,
1225 bset->eq[i]+1+nparam, n, dim - n);
1227 for (i = 0; i < bset->n_ineq; ++i)
1228 swap_vars(blk,
1229 bset->ineq[i]+1+nparam, n, dim - n);
1231 for (i = 0; i < bset->n_div; ++i)
1232 swap_vars(blk,
1233 bset->div[i]+1+1+nparam, n, dim - n);
1235 isl_blk_free(bset->ctx, blk);
1237 ISL_F_CLR(bset, ISL_BASIC_SET_NORMALIZED);
1238 return isl_basic_set_gauss(bset, NULL);
1239 error:
1240 isl_basic_set_free(bset);
1241 return NULL;
1244 struct isl_set *isl_set_swap_vars(struct isl_set *set, unsigned n)
1246 int i;
1247 set = isl_set_cow(set);
1248 if (!set)
1249 return NULL;
1251 for (i = 0; i < set->n; ++i) {
1252 set->p[i] = isl_basic_set_swap_vars(set->p[i], n);
1253 if (!set->p[i]) {
1254 isl_set_free(set);
1255 return NULL;
1258 ISL_F_CLR(set, ISL_SET_NORMALIZED);
1259 return set;
1262 struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
1264 int i = 0;
1265 unsigned total;
1266 if (!bmap)
1267 goto error;
1268 total = isl_basic_map_total_dim(bmap);
1269 isl_basic_map_free_div(bmap, bmap->n_div);
1270 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1271 if (bmap->n_eq > 0)
1272 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
1273 else {
1274 i = isl_basic_map_alloc_equality(bmap);
1275 if (i < 0)
1276 goto error;
1278 isl_int_set_si(bmap->eq[i][0], 1);
1279 isl_seq_clr(bmap->eq[i]+1, total);
1280 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
1281 isl_vec_free(bmap->sample);
1282 bmap->sample = NULL;
1283 return isl_basic_map_finalize(bmap);
1284 error:
1285 isl_basic_map_free(bmap);
1286 return NULL;
1289 struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
1291 return (struct isl_basic_set *)
1292 isl_basic_map_set_to_empty((struct isl_basic_map *)bset);
1295 void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
1297 int i;
1298 unsigned off = isl_dim_total(bmap->dim);
1299 isl_int *t = bmap->div[a];
1300 bmap->div[a] = bmap->div[b];
1301 bmap->div[b] = t;
1303 for (i = 0; i < bmap->n_eq; ++i)
1304 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
1306 for (i = 0; i < bmap->n_ineq; ++i)
1307 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
1309 for (i = 0; i < bmap->n_div; ++i)
1310 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
1311 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1314 /* Eliminate the specified n dimensions starting at first from the
1315 * constraints using Fourier-Motzkin. The dimensions themselves
1316 * are not removed.
1318 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
1319 enum isl_dim_type type, unsigned first, unsigned n)
1321 int i;
1323 if (!map)
1324 return NULL;
1325 if (n == 0)
1326 return map;
1328 map = isl_map_cow(map);
1329 if (!map)
1330 return NULL;
1331 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
1332 first += pos(map->dim, type) - 1;
1334 for (i = 0; i < map->n; ++i) {
1335 map->p[i] = isl_basic_map_eliminate_vars(map->p[i], first, n);
1336 if (!map->p[i])
1337 goto error;
1339 return map;
1340 error:
1341 isl_map_free(map);
1342 return NULL;
1345 /* Eliminate the specified n dimensions starting at first from the
1346 * constraints using Fourier-Motzkin. The dimensions themselves
1347 * are not removed.
1349 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
1350 enum isl_dim_type type, unsigned first, unsigned n)
1352 return (isl_set *)isl_map_eliminate((isl_map *)set, type, first, n);
1355 /* Eliminate the specified n dimensions starting at first from the
1356 * constraints using Fourier-Motzkin. The dimensions themselves
1357 * are not removed.
1359 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
1360 unsigned first, unsigned n)
1362 return isl_set_eliminate(set, isl_dim_set, first, n);
1365 /* Project out n dimensions starting at first using Fourier-Motzkin */
1366 struct isl_set *isl_set_remove_dims(struct isl_set *set,
1367 unsigned first, unsigned n)
1369 set = isl_set_eliminate_dims(set, first, n);
1370 set = isl_set_drop_dims(set, first, n);
1371 return set;
1374 struct isl_basic_set *isl_basic_set_remove_divs(struct isl_basic_set *bset)
1376 bset = isl_basic_set_eliminate_vars(bset, isl_dim_total(bset->dim),
1377 bset->n_div);
1378 if (!bset)
1379 return NULL;
1380 bset->n_div = 0;
1381 return bset;
1384 struct isl_set *isl_set_remove_divs(struct isl_set *set)
1386 int i;
1388 if (!set)
1389 return NULL;
1390 if (set->n == 0)
1391 return set;
1393 set = isl_set_cow(set);
1394 if (!set)
1395 return NULL;
1397 for (i = 0; i < set->n; ++i) {
1398 set->p[i] = isl_basic_set_remove_divs(set->p[i]);
1399 if (!set->p[i])
1400 goto error;
1402 return set;
1403 error:
1404 isl_set_free(set);
1405 return NULL;
1408 struct isl_basic_map *isl_basic_map_remove(struct isl_basic_map *bmap,
1409 enum isl_dim_type type, unsigned first, unsigned n)
1411 if (!bmap)
1412 return NULL;
1413 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1414 goto error);
1415 if (n == 0)
1416 return bmap;
1417 bmap = isl_basic_map_eliminate_vars(bmap,
1418 isl_basic_map_offset(bmap, type) - 1 + first, n);
1419 if (!bmap)
1420 return bmap;
1421 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
1422 return bmap;
1423 bmap = isl_basic_map_drop(bmap, type, first, n);
1424 return bmap;
1425 error:
1426 isl_basic_map_free(bmap);
1427 return NULL;
1430 __isl_give isl_basic_set *isl_basic_set_remove(__isl_take isl_basic_set *bset,
1431 enum isl_dim_type type, unsigned first, unsigned n)
1433 return (isl_basic_set *)
1434 isl_basic_map_remove((isl_basic_map *)bset, type, first, n);
1437 struct isl_map *isl_map_remove(struct isl_map *map,
1438 enum isl_dim_type type, unsigned first, unsigned n)
1440 int i;
1442 if (n == 0)
1443 return map;
1445 map = isl_map_cow(map);
1446 if (!map)
1447 return NULL;
1448 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
1450 for (i = 0; i < map->n; ++i) {
1451 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
1452 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
1453 if (!map->p[i])
1454 goto error;
1456 map = isl_map_drop(map, type, first, n);
1457 return map;
1458 error:
1459 isl_map_free(map);
1460 return NULL;
1463 __isl_give isl_set *isl_set_remove(__isl_take isl_set *bset,
1464 enum isl_dim_type type, unsigned first, unsigned n)
1466 return (isl_set *)isl_map_remove((isl_map *)bset, type, first, n);
1469 /* Project out n inputs starting at first using Fourier-Motzkin */
1470 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
1471 unsigned first, unsigned n)
1473 return isl_map_remove(map, isl_dim_in, first, n);
1476 /* Project out n dimensions starting at first using Fourier-Motzkin */
1477 struct isl_basic_set *isl_basic_set_remove_dims(struct isl_basic_set *bset,
1478 unsigned first, unsigned n)
1480 unsigned nparam = isl_basic_set_n_param(bset);
1481 bset = isl_basic_set_eliminate_vars(bset, nparam + first, n);
1482 bset = isl_basic_set_drop_dims(bset, first, n);
1483 return bset;
1486 static void dump_term(struct isl_basic_map *bmap,
1487 isl_int c, int pos, FILE *out)
1489 const char *name;
1490 unsigned in = isl_basic_map_n_in(bmap);
1491 unsigned dim = in + isl_basic_map_n_out(bmap);
1492 unsigned nparam = isl_basic_map_n_param(bmap);
1493 if (!pos)
1494 isl_int_print(out, c, 0);
1495 else {
1496 if (!isl_int_is_one(c))
1497 isl_int_print(out, c, 0);
1498 if (pos < 1 + nparam) {
1499 name = isl_dim_get_name(bmap->dim,
1500 isl_dim_param, pos - 1);
1501 if (name)
1502 fprintf(out, "%s", name);
1503 else
1504 fprintf(out, "p%d", pos - 1);
1505 } else if (pos < 1 + nparam + in)
1506 fprintf(out, "i%d", pos - 1 - nparam);
1507 else if (pos < 1 + nparam + dim)
1508 fprintf(out, "o%d", pos - 1 - nparam - in);
1509 else
1510 fprintf(out, "e%d", pos - 1 - nparam - dim);
1514 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
1515 int sign, FILE *out)
1517 int i;
1518 int first;
1519 unsigned len = 1 + isl_basic_map_total_dim(bmap);
1520 isl_int v;
1522 isl_int_init(v);
1523 for (i = 0, first = 1; i < len; ++i) {
1524 if (isl_int_sgn(c[i]) * sign <= 0)
1525 continue;
1526 if (!first)
1527 fprintf(out, " + ");
1528 first = 0;
1529 isl_int_abs(v, c[i]);
1530 dump_term(bmap, v, i, out);
1532 isl_int_clear(v);
1533 if (first)
1534 fprintf(out, "0");
1537 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
1538 const char *op, FILE *out, int indent)
1540 int i;
1542 fprintf(out, "%*s", indent, "");
1544 dump_constraint_sign(bmap, c, 1, out);
1545 fprintf(out, " %s ", op);
1546 dump_constraint_sign(bmap, c, -1, out);
1548 fprintf(out, "\n");
1550 for (i = bmap->n_div; i < bmap->extra; ++i) {
1551 if (isl_int_is_zero(c[1+isl_dim_total(bmap->dim)+i]))
1552 continue;
1553 fprintf(out, "%*s", indent, "");
1554 fprintf(out, "ERROR: unused div coefficient not zero\n");
1555 abort();
1559 static void dump_constraints(struct isl_basic_map *bmap,
1560 isl_int **c, unsigned n,
1561 const char *op, FILE *out, int indent)
1563 int i;
1565 for (i = 0; i < n; ++i)
1566 dump_constraint(bmap, c[i], op, out, indent);
1569 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
1571 int j;
1572 int first = 1;
1573 unsigned total = isl_basic_map_total_dim(bmap);
1575 for (j = 0; j < 1 + total; ++j) {
1576 if (isl_int_is_zero(exp[j]))
1577 continue;
1578 if (!first && isl_int_is_pos(exp[j]))
1579 fprintf(out, "+");
1580 dump_term(bmap, exp[j], j, out);
1581 first = 0;
1585 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
1587 int i;
1589 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
1590 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
1592 for (i = 0; i < bmap->n_div; ++i) {
1593 fprintf(out, "%*s", indent, "");
1594 fprintf(out, "e%d = [(", i);
1595 dump_affine(bmap, bmap->div[i]+1, out);
1596 fprintf(out, ")/");
1597 isl_int_print(out, bmap->div[i][0], 0);
1598 fprintf(out, "]\n");
1602 void isl_basic_set_dump(struct isl_basic_set *bset, FILE *out, int indent)
1604 if (!bset) {
1605 fprintf(out, "null basic set\n");
1606 return;
1609 fprintf(out, "%*s", indent, "");
1610 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
1611 bset->ref, bset->dim->nparam, bset->dim->n_out,
1612 bset->extra, bset->flags);
1613 dump((struct isl_basic_map *)bset, out, indent);
1616 void isl_basic_map_dump(struct isl_basic_map *bmap, FILE *out, int indent)
1618 if (!bmap) {
1619 fprintf(out, "null basic map\n");
1620 return;
1623 fprintf(out, "%*s", indent, "");
1624 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
1625 "flags: %x, n_name: %d\n",
1626 bmap->ref,
1627 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
1628 bmap->extra, bmap->flags, bmap->dim->n_name);
1629 dump(bmap, out, indent);
1632 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
1634 unsigned total;
1635 if (!bmap)
1636 return -1;
1637 total = isl_basic_map_total_dim(bmap);
1638 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1639 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
1640 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
1641 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1642 return 0;
1645 struct isl_set *isl_set_alloc_dim(struct isl_dim *dim, int n, unsigned flags)
1647 struct isl_set *set;
1649 if (!dim)
1650 return NULL;
1651 isl_assert(dim->ctx, dim->n_in == 0, return NULL);
1652 isl_assert(dim->ctx, n >= 0, return NULL);
1653 set = isl_alloc(dim->ctx, struct isl_set,
1654 sizeof(struct isl_set) +
1655 (n - 1) * sizeof(struct isl_basic_set *));
1656 if (!set)
1657 goto error;
1659 set->ctx = dim->ctx;
1660 isl_ctx_ref(set->ctx);
1661 set->ref = 1;
1662 set->size = n;
1663 set->n = 0;
1664 set->dim = dim;
1665 set->flags = flags;
1666 return set;
1667 error:
1668 isl_dim_free(dim);
1669 return NULL;
1672 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
1673 unsigned nparam, unsigned dim, int n, unsigned flags)
1675 struct isl_set *set;
1676 struct isl_dim *dims;
1678 dims = isl_dim_alloc(ctx, nparam, 0, dim);
1679 if (!dims)
1680 return NULL;
1682 set = isl_set_alloc_dim(dims, n, flags);
1683 return set;
1686 /* Make sure "map" has room for at least "n" more basic maps.
1688 struct isl_map *isl_map_grow(struct isl_map *map, int n)
1690 int i;
1691 struct isl_map *grown = NULL;
1693 if (!map)
1694 return NULL;
1695 isl_assert(map->ctx, n >= 0, goto error);
1696 if (map->n + n <= map->size)
1697 return map;
1698 grown = isl_map_alloc_dim(isl_map_get_dim(map), map->n + n, map->flags);
1699 if (!grown)
1700 goto error;
1701 for (i = 0; i < map->n; ++i) {
1702 grown->p[i] = isl_basic_map_copy(map->p[i]);
1703 if (!grown->p[i])
1704 goto error;
1705 grown->n++;
1707 isl_map_free(map);
1708 return grown;
1709 error:
1710 isl_map_free(grown);
1711 isl_map_free(map);
1712 return NULL;
1715 /* Make sure "set" has room for at least "n" more basic sets.
1717 struct isl_set *isl_set_grow(struct isl_set *set, int n)
1719 return (struct isl_set *)isl_map_grow((struct isl_map *)set, n);
1722 struct isl_set *isl_set_dup(struct isl_set *set)
1724 int i;
1725 struct isl_set *dup;
1727 if (!set)
1728 return NULL;
1730 dup = isl_set_alloc_dim(isl_dim_copy(set->dim), set->n, set->flags);
1731 if (!dup)
1732 return NULL;
1733 for (i = 0; i < set->n; ++i)
1734 dup = isl_set_add_basic_set(dup, isl_basic_set_copy(set->p[i]));
1735 return dup;
1738 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
1740 struct isl_set *set;
1742 if (!bset)
1743 return NULL;
1745 set = isl_set_alloc_dim(isl_dim_copy(bset->dim), 1, ISL_MAP_DISJOINT);
1746 if (!set) {
1747 isl_basic_set_free(bset);
1748 return NULL;
1750 return isl_set_add_basic_set(set, bset);
1753 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
1755 struct isl_map *map;
1757 if (!bmap)
1758 return NULL;
1760 map = isl_map_alloc_dim(isl_dim_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
1761 if (!map) {
1762 isl_basic_map_free(bmap);
1763 return NULL;
1765 return isl_map_add_basic_map(map, bmap);
1768 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
1769 __isl_take isl_basic_set *bset)
1771 return (struct isl_set *)isl_map_add_basic_map((struct isl_map *)set,
1772 (struct isl_basic_map *)bset);
1775 void isl_set_free(struct isl_set *set)
1777 int i;
1779 if (!set)
1780 return;
1782 if (--set->ref > 0)
1783 return;
1785 isl_ctx_deref(set->ctx);
1786 for (i = 0; i < set->n; ++i)
1787 isl_basic_set_free(set->p[i]);
1788 isl_dim_free(set->dim);
1789 free(set);
1792 void isl_set_dump(struct isl_set *set, FILE *out, int indent)
1794 int i;
1796 if (!set) {
1797 fprintf(out, "null set\n");
1798 return;
1801 fprintf(out, "%*s", indent, "");
1802 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
1803 set->ref, set->n, set->dim->nparam, set->dim->n_out,
1804 set->flags);
1805 for (i = 0; i < set->n; ++i) {
1806 fprintf(out, "%*s", indent, "");
1807 fprintf(out, "basic set %d:\n", i);
1808 isl_basic_set_dump(set->p[i], out, indent+4);
1812 void isl_map_dump(struct isl_map *map, FILE *out, int indent)
1814 int i;
1816 if (!map) {
1817 fprintf(out, "null map\n");
1818 return;
1821 fprintf(out, "%*s", indent, "");
1822 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
1823 "flags: %x, n_name: %d\n",
1824 map->ref, map->n, map->dim->nparam, map->dim->n_in,
1825 map->dim->n_out, map->flags, map->dim->n_name);
1826 for (i = 0; i < map->n; ++i) {
1827 fprintf(out, "%*s", indent, "");
1828 fprintf(out, "basic map %d:\n", i);
1829 isl_basic_map_dump(map->p[i], out, indent+4);
1833 struct isl_basic_map *isl_basic_map_intersect_domain(
1834 struct isl_basic_map *bmap, struct isl_basic_set *bset)
1836 struct isl_basic_map *bmap_domain;
1837 struct isl_dim *dim;
1839 if (!bmap || !bset)
1840 goto error;
1842 isl_assert(bset->ctx, isl_dim_match(bmap->dim, isl_dim_param,
1843 bset->dim, isl_dim_param), goto error);
1845 if (isl_dim_size(bset->dim, isl_dim_set) != 0)
1846 isl_assert(bset->ctx,
1847 isl_basic_map_compatible_domain(bmap, bset), goto error);
1849 bmap = isl_basic_map_cow(bmap);
1850 if (!bmap)
1851 goto error;
1852 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
1853 bset->n_div, bset->n_eq, bset->n_ineq);
1854 dim = isl_dim_reverse(isl_dim_copy(bset->dim));
1855 bmap_domain = isl_basic_map_from_basic_set(bset, dim);
1856 bmap = add_constraints(bmap, bmap_domain, 0, 0);
1858 bmap = isl_basic_map_simplify(bmap);
1859 return isl_basic_map_finalize(bmap);
1860 error:
1861 isl_basic_map_free(bmap);
1862 isl_basic_set_free(bset);
1863 return NULL;
1866 struct isl_basic_map *isl_basic_map_intersect_range(
1867 struct isl_basic_map *bmap, struct isl_basic_set *bset)
1869 struct isl_basic_map *bmap_range;
1871 if (!bmap || !bset)
1872 goto error;
1874 isl_assert(bset->ctx, isl_dim_match(bmap->dim, isl_dim_param,
1875 bset->dim, isl_dim_param), goto error);
1877 if (isl_dim_size(bset->dim, isl_dim_set) != 0)
1878 isl_assert(bset->ctx,
1879 isl_basic_map_compatible_range(bmap, bset), goto error);
1881 bmap = isl_basic_map_cow(bmap);
1882 if (!bmap)
1883 goto error;
1884 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
1885 bset->n_div, bset->n_eq, bset->n_ineq);
1886 bmap_range = isl_basic_map_from_basic_set(bset, isl_dim_copy(bset->dim));
1887 bmap = add_constraints(bmap, bmap_range, 0, 0);
1889 bmap = isl_basic_map_simplify(bmap);
1890 return isl_basic_map_finalize(bmap);
1891 error:
1892 isl_basic_map_free(bmap);
1893 isl_basic_set_free(bset);
1894 return NULL;
1897 int isl_basic_map_contains(struct isl_basic_map *bmap, struct isl_vec *vec)
1899 int i;
1900 unsigned total;
1901 isl_int s;
1903 total = 1 + isl_basic_map_total_dim(bmap);
1904 if (total != vec->size)
1905 return -1;
1907 isl_int_init(s);
1909 for (i = 0; i < bmap->n_eq; ++i) {
1910 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
1911 if (!isl_int_is_zero(s)) {
1912 isl_int_clear(s);
1913 return 0;
1917 for (i = 0; i < bmap->n_ineq; ++i) {
1918 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
1919 if (isl_int_is_neg(s)) {
1920 isl_int_clear(s);
1921 return 0;
1925 isl_int_clear(s);
1927 return 1;
1930 int isl_basic_set_contains(struct isl_basic_set *bset, struct isl_vec *vec)
1932 return isl_basic_map_contains((struct isl_basic_map *)bset, vec);
1935 struct isl_basic_map *isl_basic_map_intersect(
1936 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
1938 struct isl_vec *sample = NULL;
1940 if (!bmap1 || !bmap2)
1941 goto error;
1943 isl_assert(bmap1->ctx, isl_dim_match(bmap1->dim, isl_dim_param,
1944 bmap2->dim, isl_dim_param), goto error);
1945 if (isl_dim_total(bmap1->dim) ==
1946 isl_dim_size(bmap1->dim, isl_dim_param) &&
1947 isl_dim_total(bmap2->dim) !=
1948 isl_dim_size(bmap2->dim, isl_dim_param))
1949 return isl_basic_map_intersect(bmap2, bmap1);
1951 if (isl_dim_total(bmap2->dim) !=
1952 isl_dim_size(bmap2->dim, isl_dim_param))
1953 isl_assert(bmap1->ctx,
1954 isl_dim_equal(bmap1->dim, bmap2->dim), goto error);
1956 if (bmap1->sample &&
1957 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
1958 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
1959 sample = isl_vec_copy(bmap1->sample);
1960 else if (bmap2->sample &&
1961 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
1962 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
1963 sample = isl_vec_copy(bmap2->sample);
1965 bmap1 = isl_basic_map_cow(bmap1);
1966 if (!bmap1)
1967 goto error;
1968 bmap1 = isl_basic_map_extend_dim(bmap1, isl_dim_copy(bmap1->dim),
1969 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
1970 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
1972 if (!bmap1)
1973 isl_vec_free(sample);
1974 else if (sample) {
1975 isl_vec_free(bmap1->sample);
1976 bmap1->sample = sample;
1979 bmap1 = isl_basic_map_simplify(bmap1);
1980 return isl_basic_map_finalize(bmap1);
1981 error:
1982 if (sample)
1983 isl_vec_free(sample);
1984 isl_basic_map_free(bmap1);
1985 isl_basic_map_free(bmap2);
1986 return NULL;
1989 struct isl_basic_set *isl_basic_set_intersect(
1990 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
1992 return (struct isl_basic_set *)
1993 isl_basic_map_intersect(
1994 (struct isl_basic_map *)bset1,
1995 (struct isl_basic_map *)bset2);
1998 /* Special case of isl_map_intersect, where both map1 and map2
1999 * are convex, without any divs and such that either map1 or map2
2000 * contains a single constraint. This constraint is then simply
2001 * added to the other map.
2003 static __isl_give isl_map *map_intersect_add_constraint(
2004 __isl_take isl_map *map1, __isl_take isl_map *map2)
2006 struct isl_basic_map *bmap1;
2007 struct isl_basic_map *bmap2;
2009 isl_assert(map1->ctx, map1->n == 1, goto error);
2010 isl_assert(map2->ctx, map1->n == 1, goto error);
2011 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
2012 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
2014 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
2015 return isl_map_intersect(map2, map1);
2017 isl_assert(map2->ctx,
2018 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1, goto error);
2020 map1 = isl_map_cow(map1);
2021 if (!map1)
2022 goto error;
2023 if (isl_map_fast_is_empty(map1)) {
2024 isl_map_free(map2);
2025 return map1;
2027 map1->p[0] = isl_basic_map_cow(map1->p[0]);
2028 if (map2->p[0]->n_eq == 1)
2029 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
2030 else
2031 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
2032 map2->p[0]->ineq[0]);
2034 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
2035 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
2036 if (!map1->p[0])
2037 goto error;
2039 if (isl_basic_map_fast_is_empty(map1->p[0])) {
2040 isl_basic_map_free(map1->p[0]);
2041 map1->n = 0;
2044 isl_map_free(map2);
2046 return map1;
2047 error:
2048 isl_map_free(map1);
2049 isl_map_free(map2);
2050 return NULL;
2053 struct isl_map *isl_map_intersect(struct isl_map *map1, struct isl_map *map2)
2055 unsigned flags = 0;
2056 struct isl_map *result;
2057 int i, j;
2059 if (!map1 || !map2)
2060 goto error;
2062 if (isl_map_fast_is_empty(map1)) {
2063 isl_map_free(map2);
2064 return map1;
2066 if (isl_map_fast_is_empty(map2)) {
2067 isl_map_free(map1);
2068 return map2;
2071 if (map1->n == 1 && map2->n == 1 &&
2072 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
2073 isl_dim_equal(map1->dim, map2->dim) &&
2074 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
2075 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
2076 return map_intersect_add_constraint(map1, map2);
2077 isl_assert(map1->ctx, isl_dim_match(map1->dim, isl_dim_param,
2078 map2->dim, isl_dim_param), goto error);
2079 if (isl_dim_total(map1->dim) ==
2080 isl_dim_size(map1->dim, isl_dim_param) &&
2081 isl_dim_total(map2->dim) != isl_dim_size(map2->dim, isl_dim_param))
2082 return isl_map_intersect(map2, map1);
2084 if (isl_dim_total(map2->dim) != isl_dim_size(map2->dim, isl_dim_param))
2085 isl_assert(map1->ctx,
2086 isl_dim_equal(map1->dim, map2->dim), goto error);
2088 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
2089 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
2090 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
2092 result = isl_map_alloc_dim(isl_dim_copy(map1->dim),
2093 map1->n * map2->n, flags);
2094 if (!result)
2095 goto error;
2096 for (i = 0; i < map1->n; ++i)
2097 for (j = 0; j < map2->n; ++j) {
2098 struct isl_basic_map *part;
2099 part = isl_basic_map_intersect(
2100 isl_basic_map_copy(map1->p[i]),
2101 isl_basic_map_copy(map2->p[j]));
2102 if (isl_basic_map_is_empty(part))
2103 isl_basic_map_free(part);
2104 else
2105 result = isl_map_add_basic_map(result, part);
2106 if (!result)
2107 goto error;
2109 isl_map_free(map1);
2110 isl_map_free(map2);
2111 return result;
2112 error:
2113 isl_map_free(map1);
2114 isl_map_free(map2);
2115 return NULL;
2118 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
2120 return (struct isl_set *)
2121 isl_map_intersect((struct isl_map *)set1,
2122 (struct isl_map *)set2);
2125 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
2127 struct isl_dim *dim;
2128 struct isl_basic_set *bset;
2129 unsigned in;
2131 if (!bmap)
2132 return NULL;
2133 bmap = isl_basic_map_cow(bmap);
2134 if (!bmap)
2135 return NULL;
2136 dim = isl_dim_reverse(isl_dim_copy(bmap->dim));
2137 in = isl_basic_map_n_in(bmap);
2138 bset = isl_basic_set_from_basic_map(bmap);
2139 bset = isl_basic_set_swap_vars(bset, in);
2140 return isl_basic_map_from_basic_set(bset, dim);
2143 __isl_give isl_basic_map *isl_basic_map_insert(__isl_take isl_basic_map *bmap,
2144 enum isl_dim_type type, unsigned pos, unsigned n)
2146 struct isl_dim *res_dim;
2147 struct isl_basic_map *res;
2148 struct isl_dim_map *dim_map;
2149 unsigned total, off;
2150 enum isl_dim_type t;
2152 if (n == 0)
2153 return bmap;
2155 if (!bmap)
2156 return NULL;
2158 res_dim = isl_dim_insert(isl_basic_map_get_dim(bmap), type, pos, n);
2160 total = isl_basic_map_total_dim(bmap) + n;
2161 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2162 off = 0;
2163 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2164 if (t != type) {
2165 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2166 } else {
2167 unsigned size = isl_basic_map_dim(bmap, t);
2168 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2169 0, pos, off);
2170 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2171 pos, size - pos, off + pos + n);
2173 off += isl_dim_size(res_dim, t);
2175 isl_dim_map_div(dim_map, bmap, off);
2177 res = isl_basic_map_alloc_dim(res_dim,
2178 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2179 res = add_constraints_dim_map(res, bmap, dim_map);
2180 res = isl_basic_map_simplify(res);
2181 return isl_basic_map_finalize(res);
2184 __isl_give isl_basic_map *isl_basic_map_add(__isl_take isl_basic_map *bmap,
2185 enum isl_dim_type type, unsigned n)
2187 if (!bmap)
2188 return NULL;
2189 return isl_basic_map_insert(bmap, type,
2190 isl_basic_map_dim(bmap, type), n);
2193 __isl_give isl_basic_set *isl_basic_set_add(__isl_take isl_basic_set *bset,
2194 enum isl_dim_type type, unsigned n)
2196 if (!bset)
2197 return NULL;
2198 isl_assert(bset->ctx, type != isl_dim_in, goto error);
2199 return (isl_basic_set *)isl_basic_map_add((isl_basic_map *)bset, type, n);
2200 error:
2201 isl_basic_set_free(bset);
2202 return NULL;
2205 __isl_give isl_map *isl_map_insert(__isl_take isl_map *map,
2206 enum isl_dim_type type, unsigned pos, unsigned n)
2208 int i;
2210 if (n == 0)
2211 return map;
2213 map = isl_map_cow(map);
2214 if (!map)
2215 return NULL;
2217 map->dim = isl_dim_insert(map->dim, type, pos, n);
2218 if (!map->dim)
2219 goto error;
2221 for (i = 0; i < map->n; ++i) {
2222 map->p[i] = isl_basic_map_insert(map->p[i], type, pos, n);
2223 if (!map->p[i])
2224 goto error;
2227 return map;
2228 error:
2229 isl_map_free(map);
2230 return NULL;
2233 __isl_give isl_set *isl_set_insert(__isl_take isl_set *set,
2234 enum isl_dim_type type, unsigned pos, unsigned n)
2236 return (isl_set *)isl_map_insert((isl_map *)set, type, pos, n);
2239 __isl_give isl_map *isl_map_add(__isl_take isl_map *map,
2240 enum isl_dim_type type, unsigned n)
2242 if (!map)
2243 return NULL;
2244 return isl_map_insert(map, type, isl_map_dim(map, type), n);
2247 __isl_give isl_set *isl_set_add(__isl_take isl_set *set,
2248 enum isl_dim_type type, unsigned n)
2250 if (!set)
2251 return NULL;
2252 isl_assert(set->ctx, type != isl_dim_in, goto error);
2253 return (isl_set *)isl_map_add((isl_map *)set, type, n);
2254 error:
2255 isl_set_free(set);
2256 return NULL;
2259 __isl_give isl_basic_map *isl_basic_map_move_dims(
2260 __isl_take isl_basic_map *bmap,
2261 enum isl_dim_type dst_type, unsigned dst_pos,
2262 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2264 int i;
2265 struct isl_dim_map *dim_map;
2266 struct isl_basic_map *res;
2267 enum isl_dim_type t;
2268 unsigned total, off;
2270 if (!bmap)
2271 return NULL;
2272 if (n == 0)
2273 return bmap;
2275 isl_assert(bmap->ctx, src_pos + n <= isl_basic_map_dim(bmap, src_type),
2276 goto error);
2278 if (dst_type == src_type && dst_pos == src_pos)
2279 return bmap;
2281 isl_assert(bmap->ctx, dst_type != src_type, goto error);
2283 if (pos(bmap->dim, dst_type) + dst_pos ==
2284 pos(bmap->dim, src_type) + src_pos +
2285 ((src_type < dst_type) ? n : 0)) {
2286 bmap = isl_basic_map_cow(bmap);
2287 if (!bmap)
2288 return NULL;
2290 bmap->dim = isl_dim_move(bmap->dim, dst_type, dst_pos,
2291 src_type, src_pos, n);
2292 if (!bmap->dim)
2293 goto error;
2295 bmap = isl_basic_map_finalize(bmap);
2297 return bmap;
2300 total = isl_basic_map_total_dim(bmap);
2301 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2303 off = 0;
2304 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2305 unsigned size = isl_dim_size(bmap->dim, t);
2306 if (t == dst_type) {
2307 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2308 0, dst_pos, off);
2309 off += dst_pos;
2310 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
2311 src_pos, n, off);
2312 off += n;
2313 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2314 dst_pos, size - dst_pos, off);
2315 off += size - dst_pos;
2316 } else if (t == src_type) {
2317 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2318 0, src_pos, off);
2319 off += src_pos;
2320 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2321 src_pos + n, size - src_pos - n, off);
2322 off += size - src_pos - n;
2323 } else {
2324 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2325 off += size;
2328 isl_dim_map_div(dim_map, bmap, off);
2330 res = isl_basic_map_alloc_dim(isl_basic_map_get_dim(bmap),
2331 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2332 bmap = add_constraints_dim_map(res, bmap, dim_map);
2334 bmap->dim = isl_dim_move(bmap->dim, dst_type, dst_pos,
2335 src_type, src_pos, n);
2336 if (!bmap->dim)
2337 goto error;
2339 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2340 bmap = isl_basic_map_gauss(bmap, NULL);
2341 bmap = isl_basic_map_finalize(bmap);
2343 return bmap;
2344 error:
2345 isl_basic_map_free(bmap);
2346 return NULL;
2349 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
2350 enum isl_dim_type dst_type, unsigned dst_pos,
2351 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2353 return (isl_basic_set *)isl_basic_map_move_dims(
2354 (isl_basic_map *)bset, dst_type, dst_pos, src_type, src_pos, n);
2357 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
2358 enum isl_dim_type dst_type, unsigned dst_pos,
2359 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2361 if (!set)
2362 return NULL;
2363 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
2364 return (isl_set *)isl_map_move_dims((isl_map *)set, dst_type, dst_pos,
2365 src_type, src_pos, n);
2366 error:
2367 isl_set_free(set);
2368 return NULL;
2371 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
2372 enum isl_dim_type dst_type, unsigned dst_pos,
2373 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2375 int i;
2377 if (!map)
2378 return NULL;
2379 if (n == 0)
2380 return map;
2382 isl_assert(map->ctx, src_pos + n <= isl_map_dim(map, src_type),
2383 goto error);
2385 if (dst_type == src_type && dst_pos == src_pos)
2386 return map;
2388 isl_assert(map->ctx, dst_type != src_type, goto error);
2390 map = isl_map_cow(map);
2391 if (!map)
2392 return NULL;
2394 map->dim = isl_dim_move(map->dim, dst_type, dst_pos, src_type, src_pos, n);
2395 if (!map->dim)
2396 goto error;
2398 for (i = 0; i < map->n; ++i) {
2399 map->p[i] = isl_basic_map_move_dims(map->p[i],
2400 dst_type, dst_pos,
2401 src_type, src_pos, n);
2402 if (!map->p[i])
2403 goto error;
2406 return map;
2407 error:
2408 isl_map_free(map);
2409 return NULL;
2412 /* Move the specified dimensions to the last columns right before
2413 * the divs. Don't change the dimension specification of bmap.
2414 * That's the responsibility of the caller.
2416 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
2417 enum isl_dim_type type, unsigned first, unsigned n)
2419 struct isl_dim_map *dim_map;
2420 struct isl_basic_map *res;
2421 enum isl_dim_type t;
2422 unsigned total, off;
2424 if (!bmap)
2425 return NULL;
2426 if (pos(bmap->dim, type) + first + n == 1 + isl_dim_total(bmap->dim))
2427 return bmap;
2429 total = isl_basic_map_total_dim(bmap);
2430 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2432 off = 0;
2433 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2434 unsigned size = isl_dim_size(bmap->dim, t);
2435 if (t == type) {
2436 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2437 0, first, off);
2438 off += first;
2439 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2440 first, n, total - bmap->n_div - n);
2441 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2442 first + n, size - (first + n), off);
2443 off += size - (first + n);
2444 } else {
2445 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2446 off += size;
2449 isl_dim_map_div(dim_map, bmap, off + n);
2451 res = isl_basic_map_alloc_dim(isl_basic_map_get_dim(bmap),
2452 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2453 res = add_constraints_dim_map(res, bmap, dim_map);
2454 return res;
2457 /* Turn the n dimensions of type type, starting at first
2458 * into existentially quantified variables.
2460 __isl_give isl_basic_map *isl_basic_map_project_out(
2461 __isl_take isl_basic_map *bmap,
2462 enum isl_dim_type type, unsigned first, unsigned n)
2464 int i;
2465 size_t row_size;
2466 isl_int **new_div;
2467 isl_int *old;
2469 if (n == 0)
2470 return bmap;
2472 if (!bmap)
2473 return NULL;
2475 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
2476 return isl_basic_map_remove(bmap, type, first, n);
2478 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
2479 goto error);
2481 bmap = move_last(bmap, type, first, n);
2482 bmap = isl_basic_map_cow(bmap);
2483 if (!bmap)
2484 return NULL;
2486 row_size = 1 + isl_dim_total(bmap->dim) + bmap->extra;
2487 old = bmap->block2.data;
2488 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
2489 (bmap->extra + n) * (1 + row_size));
2490 if (!bmap->block2.data)
2491 goto error;
2492 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
2493 if (!new_div)
2494 goto error;
2495 for (i = 0; i < n; ++i) {
2496 new_div[i] = bmap->block2.data +
2497 (bmap->extra + i) * (1 + row_size);
2498 isl_seq_clr(new_div[i], 1 + row_size);
2500 for (i = 0; i < bmap->extra; ++i)
2501 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
2502 free(bmap->div);
2503 bmap->div = new_div;
2504 bmap->n_div += n;
2505 bmap->extra += n;
2507 bmap->dim = isl_dim_drop(bmap->dim, type, first, n);
2508 if (!bmap->dim)
2509 goto error;
2510 bmap = isl_basic_map_simplify(bmap);
2511 bmap = isl_basic_map_drop_redundant_divs(bmap);
2512 return isl_basic_map_finalize(bmap);
2513 error:
2514 isl_basic_map_free(bmap);
2515 return NULL;
2518 /* Turn the n dimensions of type type, starting at first
2519 * into existentially quantified variables.
2521 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
2522 enum isl_dim_type type, unsigned first, unsigned n)
2524 return (isl_basic_set *)isl_basic_map_project_out(
2525 (isl_basic_map *)bset, type, first, n);
2528 /* Turn the n dimensions of type type, starting at first
2529 * into existentially quantified variables.
2531 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
2532 enum isl_dim_type type, unsigned first, unsigned n)
2534 int i;
2536 if (!map)
2537 return NULL;
2539 if (n == 0)
2540 return map;
2542 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
2544 map = isl_map_cow(map);
2545 if (!map)
2546 return NULL;
2548 map->dim = isl_dim_drop(map->dim, type, first, n);
2549 if (!map->dim)
2550 goto error;
2552 for (i = 0; i < map->n; ++i) {
2553 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
2554 if (!map->p[i])
2555 goto error;
2558 return map;
2559 error:
2560 isl_map_free(map);
2561 return NULL;
2564 /* Turn the n dimensions of type type, starting at first
2565 * into existentially quantified variables.
2567 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
2568 enum isl_dim_type type, unsigned first, unsigned n)
2570 return (isl_set *)isl_map_project_out((isl_map *)set, type, first, n);
2573 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
2575 int i, j;
2577 for (i = 0; i < n; ++i) {
2578 j = isl_basic_map_alloc_div(bmap);
2579 if (j < 0)
2580 goto error;
2581 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
2583 return bmap;
2584 error:
2585 isl_basic_map_free(bmap);
2586 return NULL;
2589 struct isl_basic_map *isl_basic_map_apply_range(
2590 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2592 struct isl_dim *dim_result = NULL;
2593 struct isl_basic_map *bmap;
2594 unsigned n_in, n_out, n, nparam, total, pos;
2595 struct isl_dim_map *dim_map1, *dim_map2;
2597 if (!bmap1 || !bmap2)
2598 goto error;
2600 dim_result = isl_dim_join(isl_dim_copy(bmap1->dim),
2601 isl_dim_copy(bmap2->dim));
2603 n_in = isl_basic_map_n_in(bmap1);
2604 n_out = isl_basic_map_n_out(bmap2);
2605 n = isl_basic_map_n_out(bmap1);
2606 nparam = isl_basic_map_n_param(bmap1);
2608 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
2609 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
2610 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
2611 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
2612 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
2613 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
2614 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
2615 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
2616 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
2617 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
2618 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
2620 bmap = isl_basic_map_alloc_dim(dim_result,
2621 bmap1->n_div + bmap2->n_div + n,
2622 bmap1->n_eq + bmap2->n_eq,
2623 bmap1->n_ineq + bmap2->n_ineq);
2624 bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
2625 bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
2626 bmap = add_divs(bmap, n);
2627 bmap = isl_basic_map_simplify(bmap);
2628 bmap = isl_basic_map_drop_redundant_divs(bmap);
2629 return isl_basic_map_finalize(bmap);
2630 error:
2631 isl_basic_map_free(bmap1);
2632 isl_basic_map_free(bmap2);
2633 return NULL;
2636 struct isl_basic_set *isl_basic_set_apply(
2637 struct isl_basic_set *bset, struct isl_basic_map *bmap)
2639 if (!bset || !bmap)
2640 goto error;
2642 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
2643 goto error);
2645 return (struct isl_basic_set *)
2646 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
2647 error:
2648 isl_basic_set_free(bset);
2649 isl_basic_map_free(bmap);
2650 return NULL;
2653 struct isl_basic_map *isl_basic_map_apply_domain(
2654 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2656 if (!bmap1 || !bmap2)
2657 goto error;
2659 isl_assert(bmap1->ctx,
2660 isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
2661 isl_assert(bmap1->ctx,
2662 isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
2663 goto error);
2665 bmap1 = isl_basic_map_reverse(bmap1);
2666 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
2667 return isl_basic_map_reverse(bmap1);
2668 error:
2669 isl_basic_map_free(bmap1);
2670 isl_basic_map_free(bmap2);
2671 return NULL;
2674 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
2675 * A \cap B -> f(A) + f(B)
2677 struct isl_basic_map *isl_basic_map_sum(
2678 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2680 unsigned n_in, n_out, nparam, total, pos;
2681 struct isl_basic_map *bmap = NULL;
2682 struct isl_dim_map *dim_map1, *dim_map2;
2683 int i;
2685 if (!bmap1 || !bmap2)
2686 goto error;
2688 isl_assert(bmap1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim),
2689 goto error);
2691 nparam = isl_basic_map_n_param(bmap1);
2692 n_in = isl_basic_map_n_in(bmap1);
2693 n_out = isl_basic_map_n_out(bmap1);
2695 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
2696 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
2697 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
2698 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
2699 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
2700 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
2701 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
2702 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
2703 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
2704 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
2705 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
2707 bmap = isl_basic_map_alloc_dim(isl_dim_copy(bmap1->dim),
2708 bmap1->n_div + bmap2->n_div + 2 * n_out,
2709 bmap1->n_eq + bmap2->n_eq + n_out,
2710 bmap1->n_ineq + bmap2->n_ineq);
2711 for (i = 0; i < n_out; ++i) {
2712 int j = isl_basic_map_alloc_equality(bmap);
2713 if (j < 0)
2714 goto error;
2715 isl_seq_clr(bmap->eq[j], 1+total);
2716 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
2717 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
2718 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
2720 bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
2721 bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
2722 bmap = add_divs(bmap, 2 * n_out);
2724 bmap = isl_basic_map_simplify(bmap);
2725 return isl_basic_map_finalize(bmap);
2726 error:
2727 isl_basic_map_free(bmap);
2728 isl_basic_map_free(bmap1);
2729 isl_basic_map_free(bmap2);
2730 return NULL;
2733 /* Given two maps A -> f(A) and B -> g(B), construct a map
2734 * A \cap B -> f(A) + f(B)
2736 struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
2738 struct isl_map *result;
2739 int i, j;
2741 if (!map1 || !map2)
2742 goto error;
2744 isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
2746 result = isl_map_alloc_dim(isl_dim_copy(map1->dim),
2747 map1->n * map2->n, 0);
2748 if (!result)
2749 goto error;
2750 for (i = 0; i < map1->n; ++i)
2751 for (j = 0; j < map2->n; ++j) {
2752 struct isl_basic_map *part;
2753 part = isl_basic_map_sum(
2754 isl_basic_map_copy(map1->p[i]),
2755 isl_basic_map_copy(map2->p[j]));
2756 if (isl_basic_map_is_empty(part))
2757 isl_basic_map_free(part);
2758 else
2759 result = isl_map_add_basic_map(result, part);
2760 if (!result)
2761 goto error;
2763 isl_map_free(map1);
2764 isl_map_free(map2);
2765 return result;
2766 error:
2767 isl_map_free(map1);
2768 isl_map_free(map2);
2769 return NULL;
2772 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
2773 __isl_take isl_set *set2)
2775 return (isl_set *)isl_map_sum((isl_map *)set1, (isl_map *)set2);
2778 /* Given a basic map A -> f(A), construct A -> -f(A).
2780 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
2782 int i, j;
2783 unsigned off, n;
2785 bmap = isl_basic_map_cow(bmap);
2786 if (!bmap)
2787 return NULL;
2789 n = isl_basic_map_dim(bmap, isl_dim_out);
2790 off = isl_basic_map_offset(bmap, isl_dim_out);
2791 for (i = 0; i < bmap->n_eq; ++i)
2792 for (j = 0; j < n; ++j)
2793 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
2794 for (i = 0; i < bmap->n_ineq; ++i)
2795 for (j = 0; j < n; ++j)
2796 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
2797 for (i = 0; i < bmap->n_div; ++i)
2798 for (j = 0; j < n; ++j)
2799 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
2800 return isl_basic_map_finalize(bmap);
2803 /* Given a map A -> f(A), construct A -> -f(A).
2805 struct isl_map *isl_map_neg(struct isl_map *map)
2807 int i;
2809 map = isl_map_cow(map);
2810 if (!map)
2811 return NULL;
2813 for (i = 0; i < map->n; ++i) {
2814 map->p[i] = isl_basic_map_neg(map->p[i]);
2815 if (!map->p[i])
2816 goto error;
2819 return map;
2820 error:
2821 isl_map_free(map);
2822 return NULL;
2825 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
2827 return (isl_set *)isl_map_neg((isl_map *)set);
2830 /* Given a basic map A -> f(A) and an integer d, construct a basic map
2831 * A -> floor(f(A)/d).
2833 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
2834 isl_int d)
2836 unsigned n_in, n_out, nparam, total, pos;
2837 struct isl_basic_map *result = NULL;
2838 struct isl_dim_map *dim_map;
2839 int i;
2841 if (!bmap)
2842 return NULL;
2844 nparam = isl_basic_map_n_param(bmap);
2845 n_in = isl_basic_map_n_in(bmap);
2846 n_out = isl_basic_map_n_out(bmap);
2848 total = nparam + n_in + n_out + bmap->n_div + n_out;
2849 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2850 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
2851 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
2852 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
2853 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
2855 result = isl_basic_map_alloc_dim(isl_dim_copy(bmap->dim),
2856 bmap->n_div + n_out,
2857 bmap->n_eq, bmap->n_ineq + 2 * n_out);
2858 result = add_constraints_dim_map(result, bmap, dim_map);
2859 result = add_divs(result, n_out);
2860 for (i = 0; i < n_out; ++i) {
2861 int j;
2862 j = isl_basic_map_alloc_inequality(result);
2863 if (j < 0)
2864 goto error;
2865 isl_seq_clr(result->ineq[j], 1+total);
2866 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
2867 isl_int_set_si(result->ineq[j][1+pos+i], 1);
2868 j = isl_basic_map_alloc_inequality(result);
2869 if (j < 0)
2870 goto error;
2871 isl_seq_clr(result->ineq[j], 1+total);
2872 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
2873 isl_int_set_si(result->ineq[j][1+pos+i], -1);
2874 isl_int_sub_ui(result->ineq[j][0], d, 1);
2877 result = isl_basic_map_simplify(result);
2878 return isl_basic_map_finalize(result);
2879 error:
2880 isl_basic_map_free(result);
2881 return NULL;
2884 /* Given a map A -> f(A) and an integer d, construct a map
2885 * A -> floor(f(A)/d).
2887 struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
2889 int i;
2891 map = isl_map_cow(map);
2892 if (!map)
2893 return NULL;
2895 ISL_F_CLR(map, ISL_MAP_DISJOINT);
2896 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
2897 for (i = 0; i < map->n; ++i) {
2898 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
2899 if (!map->p[i])
2900 goto error;
2903 return map;
2904 error:
2905 isl_map_free(map);
2906 return NULL;
2909 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
2911 int i;
2912 unsigned nparam;
2913 unsigned n_in;
2915 i = isl_basic_map_alloc_equality(bmap);
2916 if (i < 0)
2917 goto error;
2918 nparam = isl_basic_map_n_param(bmap);
2919 n_in = isl_basic_map_n_in(bmap);
2920 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
2921 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
2922 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
2923 return isl_basic_map_finalize(bmap);
2924 error:
2925 isl_basic_map_free(bmap);
2926 return NULL;
2929 /* Add a constraints to "bmap" expressing i_pos < o_pos
2931 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
2933 int i;
2934 unsigned nparam;
2935 unsigned n_in;
2937 i = isl_basic_map_alloc_inequality(bmap);
2938 if (i < 0)
2939 goto error;
2940 nparam = isl_basic_map_n_param(bmap);
2941 n_in = isl_basic_map_n_in(bmap);
2942 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
2943 isl_int_set_si(bmap->ineq[i][0], -1);
2944 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
2945 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
2946 return isl_basic_map_finalize(bmap);
2947 error:
2948 isl_basic_map_free(bmap);
2949 return NULL;
2952 /* Add a constraint to "bmap" expressing i_pos <= o_pos
2954 static __isl_give isl_basic_map *var_less_or_equal(
2955 __isl_take isl_basic_map *bmap, unsigned pos)
2957 int i;
2958 unsigned nparam;
2959 unsigned n_in;
2961 i = isl_basic_map_alloc_inequality(bmap);
2962 if (i < 0)
2963 goto error;
2964 nparam = isl_basic_map_n_param(bmap);
2965 n_in = isl_basic_map_n_in(bmap);
2966 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
2967 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
2968 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
2969 return isl_basic_map_finalize(bmap);
2970 error:
2971 isl_basic_map_free(bmap);
2972 return NULL;
2975 /* Add a constraints to "bmap" expressing i_pos > o_pos
2977 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
2979 int i;
2980 unsigned nparam;
2981 unsigned n_in;
2983 i = isl_basic_map_alloc_inequality(bmap);
2984 if (i < 0)
2985 goto error;
2986 nparam = isl_basic_map_n_param(bmap);
2987 n_in = isl_basic_map_n_in(bmap);
2988 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
2989 isl_int_set_si(bmap->ineq[i][0], -1);
2990 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
2991 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
2992 return isl_basic_map_finalize(bmap);
2993 error:
2994 isl_basic_map_free(bmap);
2995 return NULL;
2998 /* Add a constraint to "bmap" expressing i_pos >= o_pos
3000 static __isl_give isl_basic_map *var_more_or_equal(
3001 __isl_take isl_basic_map *bmap, unsigned pos)
3003 int i;
3004 unsigned nparam;
3005 unsigned n_in;
3007 i = isl_basic_map_alloc_inequality(bmap);
3008 if (i < 0)
3009 goto error;
3010 nparam = isl_basic_map_n_param(bmap);
3011 n_in = isl_basic_map_n_in(bmap);
3012 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3013 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
3014 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
3015 return isl_basic_map_finalize(bmap);
3016 error:
3017 isl_basic_map_free(bmap);
3018 return NULL;
3021 struct isl_basic_map *isl_basic_map_equal(struct isl_dim *dim, unsigned n_equal)
3023 int i;
3024 struct isl_basic_map *bmap;
3025 bmap = isl_basic_map_alloc_dim(dim, 0, n_equal, 0);
3026 if (!bmap)
3027 return NULL;
3028 for (i = 0; i < n_equal && bmap; ++i)
3029 bmap = var_equal(bmap, i);
3030 return isl_basic_map_finalize(bmap);
3033 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
3035 struct isl_basic_map *isl_basic_map_less_at(struct isl_dim *dim, unsigned pos)
3037 int i;
3038 struct isl_basic_map *bmap;
3039 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
3040 if (!bmap)
3041 return NULL;
3042 for (i = 0; i < pos && bmap; ++i)
3043 bmap = var_equal(bmap, i);
3044 if (bmap)
3045 bmap = var_less(bmap, pos);
3046 return isl_basic_map_finalize(bmap);
3049 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
3051 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
3052 __isl_take isl_dim *dim, unsigned pos)
3054 int i;
3055 isl_basic_map *bmap;
3057 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
3058 for (i = 0; i < pos; ++i)
3059 bmap = var_equal(bmap, i);
3060 bmap = var_less_or_equal(bmap, pos);
3061 return isl_basic_map_finalize(bmap);
3064 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
3066 struct isl_basic_map *isl_basic_map_more_at(struct isl_dim *dim, unsigned pos)
3068 int i;
3069 struct isl_basic_map *bmap;
3070 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
3071 if (!bmap)
3072 return NULL;
3073 for (i = 0; i < pos && bmap; ++i)
3074 bmap = var_equal(bmap, i);
3075 if (bmap)
3076 bmap = var_more(bmap, pos);
3077 return isl_basic_map_finalize(bmap);
3080 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
3082 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
3083 __isl_take isl_dim *dim, unsigned pos)
3085 int i;
3086 isl_basic_map *bmap;
3088 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
3089 for (i = 0; i < pos; ++i)
3090 bmap = var_equal(bmap, i);
3091 bmap = var_more_or_equal(bmap, pos);
3092 return isl_basic_map_finalize(bmap);
3095 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_dim *dims,
3096 unsigned n, int equal)
3098 struct isl_map *map;
3099 int i;
3101 if (n == 0 && equal)
3102 return isl_map_universe(dims);
3104 map = isl_map_alloc_dim(isl_dim_copy(dims), n, ISL_MAP_DISJOINT);
3106 for (i = 0; i + 1 < n; ++i)
3107 map = isl_map_add_basic_map(map,
3108 isl_basic_map_less_at(isl_dim_copy(dims), i));
3109 if (n > 0) {
3110 if (equal)
3111 map = isl_map_add_basic_map(map,
3112 isl_basic_map_less_or_equal_at(dims, n - 1));
3113 else
3114 map = isl_map_add_basic_map(map,
3115 isl_basic_map_less_at(dims, n - 1));
3116 } else
3117 isl_dim_free(dims);
3119 return map;
3122 static __isl_give isl_map *map_lex_lte(__isl_take isl_dim *dims, int equal)
3124 if (!dims)
3125 return NULL;
3126 return map_lex_lte_first(dims, dims->n_out, equal);
3129 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_dim *dim, unsigned n)
3131 return map_lex_lte_first(dim, n, 0);
3134 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_dim *dim, unsigned n)
3136 return map_lex_lte_first(dim, n, 1);
3139 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_dim *set_dim)
3141 return map_lex_lte(isl_dim_map(set_dim), 0);
3144 __isl_give isl_map *isl_map_lex_le(__isl_take isl_dim *set_dim)
3146 return map_lex_lte(isl_dim_map(set_dim), 1);
3149 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_dim *dims,
3150 unsigned n, int equal)
3152 struct isl_map *map;
3153 int i;
3155 if (n == 0 && equal)
3156 return isl_map_universe(dims);
3158 map = isl_map_alloc_dim(isl_dim_copy(dims), n, ISL_MAP_DISJOINT);
3160 for (i = 0; i + 1 < n; ++i)
3161 map = isl_map_add_basic_map(map,
3162 isl_basic_map_more_at(isl_dim_copy(dims), i));
3163 if (n > 0) {
3164 if (equal)
3165 map = isl_map_add_basic_map(map,
3166 isl_basic_map_more_or_equal_at(dims, n - 1));
3167 else
3168 map = isl_map_add_basic_map(map,
3169 isl_basic_map_more_at(dims, n - 1));
3170 } else
3171 isl_dim_free(dims);
3173 return map;
3176 static __isl_give isl_map *map_lex_gte(__isl_take isl_dim *dims, int equal)
3178 if (!dims)
3179 return NULL;
3180 return map_lex_gte_first(dims, dims->n_out, equal);
3183 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_dim *dim, unsigned n)
3185 return map_lex_gte_first(dim, n, 0);
3188 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_dim *dim, unsigned n)
3190 return map_lex_gte_first(dim, n, 1);
3193 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_dim *set_dim)
3195 return map_lex_gte(isl_dim_map(set_dim), 0);
3198 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_dim *set_dim)
3200 return map_lex_gte(isl_dim_map(set_dim), 1);
3203 struct isl_basic_map *isl_basic_map_from_basic_set(
3204 struct isl_basic_set *bset, struct isl_dim *dim)
3206 struct isl_basic_map *bmap;
3208 bset = isl_basic_set_cow(bset);
3209 if (!bset || !dim)
3210 goto error;
3212 isl_assert(bset->ctx, isl_dim_compatible(bset->dim, dim), goto error);
3213 isl_dim_free(bset->dim);
3214 bmap = (struct isl_basic_map *) bset;
3215 bmap->dim = dim;
3216 return isl_basic_map_finalize(bmap);
3217 error:
3218 isl_basic_set_free(bset);
3219 isl_dim_free(dim);
3220 return NULL;
3223 struct isl_basic_set *isl_basic_set_from_basic_map(struct isl_basic_map *bmap)
3225 if (!bmap)
3226 goto error;
3227 if (bmap->dim->n_in == 0)
3228 return (struct isl_basic_set *)bmap;
3229 bmap = isl_basic_map_cow(bmap);
3230 if (!bmap)
3231 goto error;
3232 bmap->dim = isl_dim_as_set_dim(bmap->dim);
3233 if (!bmap->dim)
3234 goto error;
3235 bmap = isl_basic_map_finalize(bmap);
3236 return (struct isl_basic_set *)bmap;
3237 error:
3238 isl_basic_map_free(bmap);
3239 return NULL;
3242 /* For a div d = floor(f/m), add the constraints
3244 * f - m d >= 0
3245 * -(f-(n-1)) + m d >= 0
3247 * Note that the second constraint is the negation of
3249 * f - m d >= n
3251 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
3252 unsigned pos, isl_int *div)
3254 int i, j;
3255 unsigned total = isl_basic_map_total_dim(bmap);
3257 i = isl_basic_map_alloc_inequality(bmap);
3258 if (i < 0)
3259 return -1;
3260 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
3261 isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
3263 j = isl_basic_map_alloc_inequality(bmap);
3264 if (j < 0)
3265 return -1;
3266 isl_seq_neg(bmap->ineq[j], bmap->ineq[i], 1 + total);
3267 isl_int_add(bmap->ineq[j][0], bmap->ineq[j][0], bmap->ineq[j][1 + pos]);
3268 isl_int_sub_ui(bmap->ineq[j][0], bmap->ineq[j][0], 1);
3269 return j;
3272 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
3273 unsigned pos, isl_int *div)
3275 return isl_basic_map_add_div_constraints_var((isl_basic_map *)bset,
3276 pos, div);
3279 int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
3281 unsigned total = isl_basic_map_total_dim(bmap);
3282 unsigned div_pos = total - bmap->n_div + div;
3284 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
3285 bmap->div[div]);
3288 struct isl_basic_set *isl_basic_map_underlying_set(
3289 struct isl_basic_map *bmap)
3291 if (!bmap)
3292 goto error;
3293 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
3294 bmap->n_div == 0 && !isl_dim_get_tuple_name(bmap->dim, isl_dim_out))
3295 return (struct isl_basic_set *)bmap;
3296 bmap = isl_basic_map_cow(bmap);
3297 if (!bmap)
3298 goto error;
3299 bmap->dim = isl_dim_underlying(bmap->dim, bmap->n_div);
3300 if (!bmap->dim)
3301 goto error;
3302 bmap->extra -= bmap->n_div;
3303 bmap->n_div = 0;
3304 bmap = isl_basic_map_finalize(bmap);
3305 return (struct isl_basic_set *)bmap;
3306 error:
3307 return NULL;
3310 __isl_give isl_basic_set *isl_basic_set_underlying_set(
3311 __isl_take isl_basic_set *bset)
3313 return isl_basic_map_underlying_set((isl_basic_map *)bset);
3316 struct isl_basic_map *isl_basic_map_overlying_set(
3317 struct isl_basic_set *bset, struct isl_basic_map *like)
3319 struct isl_basic_map *bmap;
3320 struct isl_ctx *ctx;
3321 unsigned total;
3322 int i;
3324 if (!bset || !like)
3325 goto error;
3326 ctx = bset->ctx;
3327 isl_assert(ctx, bset->n_div == 0, goto error);
3328 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
3329 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
3330 goto error);
3331 if (isl_dim_equal(bset->dim, like->dim) && like->n_div == 0) {
3332 isl_basic_map_free(like);
3333 return (struct isl_basic_map *)bset;
3335 bset = isl_basic_set_cow(bset);
3336 if (!bset)
3337 goto error;
3338 total = bset->dim->n_out + bset->extra;
3339 bmap = (struct isl_basic_map *)bset;
3340 isl_dim_free(bmap->dim);
3341 bmap->dim = isl_dim_copy(like->dim);
3342 if (!bmap->dim)
3343 goto error;
3344 bmap->n_div = like->n_div;
3345 bmap->extra += like->n_div;
3346 if (bmap->extra) {
3347 unsigned ltotal;
3348 ltotal = total - bmap->extra + like->extra;
3349 if (ltotal > total)
3350 ltotal = total;
3351 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
3352 bmap->extra * (1 + 1 + total));
3353 if (isl_blk_is_error(bmap->block2))
3354 goto error;
3355 bmap->div = isl_realloc_array(ctx, bmap->div, isl_int *,
3356 bmap->extra);
3357 if (!bmap->div)
3358 goto error;
3359 for (i = 0; i < bmap->extra; ++i)
3360 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
3361 for (i = 0; i < like->n_div; ++i) {
3362 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
3363 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
3365 bmap = isl_basic_map_extend_constraints(bmap,
3366 0, 2 * like->n_div);
3367 for (i = 0; i < like->n_div; ++i) {
3368 if (isl_int_is_zero(bmap->div[i][0]))
3369 continue;
3370 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
3371 goto error;
3374 isl_basic_map_free(like);
3375 bmap = isl_basic_map_simplify(bmap);
3376 bmap = isl_basic_map_finalize(bmap);
3377 return bmap;
3378 error:
3379 isl_basic_map_free(like);
3380 isl_basic_set_free(bset);
3381 return NULL;
3384 struct isl_basic_set *isl_basic_set_from_underlying_set(
3385 struct isl_basic_set *bset, struct isl_basic_set *like)
3387 return (struct isl_basic_set *)
3388 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
3391 struct isl_set *isl_set_from_underlying_set(
3392 struct isl_set *set, struct isl_basic_set *like)
3394 int i;
3396 if (!set || !like)
3397 goto error;
3398 isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
3399 goto error);
3400 if (isl_dim_equal(set->dim, like->dim) && like->n_div == 0) {
3401 isl_basic_set_free(like);
3402 return set;
3404 set = isl_set_cow(set);
3405 if (!set)
3406 goto error;
3407 for (i = 0; i < set->n; ++i) {
3408 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
3409 isl_basic_set_copy(like));
3410 if (!set->p[i])
3411 goto error;
3413 isl_dim_free(set->dim);
3414 set->dim = isl_dim_copy(like->dim);
3415 if (!set->dim)
3416 goto error;
3417 isl_basic_set_free(like);
3418 return set;
3419 error:
3420 isl_basic_set_free(like);
3421 isl_set_free(set);
3422 return NULL;
3425 struct isl_set *isl_map_underlying_set(struct isl_map *map)
3427 int i;
3429 map = isl_map_cow(map);
3430 if (!map)
3431 return NULL;
3432 map->dim = isl_dim_cow(map->dim);
3433 if (!map->dim)
3434 goto error;
3436 for (i = 1; i < map->n; ++i)
3437 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
3438 goto error);
3439 for (i = 0; i < map->n; ++i) {
3440 map->p[i] = (struct isl_basic_map *)
3441 isl_basic_map_underlying_set(map->p[i]);
3442 if (!map->p[i])
3443 goto error;
3445 if (map->n == 0)
3446 map->dim = isl_dim_underlying(map->dim, 0);
3447 else {
3448 isl_dim_free(map->dim);
3449 map->dim = isl_dim_copy(map->p[0]->dim);
3451 if (!map->dim)
3452 goto error;
3453 return (struct isl_set *)map;
3454 error:
3455 isl_map_free(map);
3456 return NULL;
3459 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
3461 return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
3464 static __isl_give isl_basic_map *isl_basic_map_reset_dim(
3465 __isl_take isl_basic_map *bmap, __isl_take isl_dim *dim)
3467 bmap = isl_basic_map_cow(bmap);
3468 if (!bmap || !dim)
3469 goto error;
3471 isl_dim_free(bmap->dim);
3472 bmap->dim = dim;
3474 bmap = isl_basic_map_finalize(bmap);
3476 return bmap;
3477 error:
3478 isl_basic_map_free(bmap);
3479 isl_dim_free(dim);
3480 return NULL;
3483 static __isl_give isl_basic_set *isl_basic_set_reset_dim(
3484 __isl_take isl_basic_set *bset, __isl_take isl_dim *dim)
3486 return (isl_basic_set *)isl_basic_map_reset_dim((isl_basic_map *)bset,
3487 dim);
3490 __isl_give isl_map *isl_map_reset_dim(__isl_take isl_map *map,
3491 __isl_take isl_dim *dim)
3493 int i;
3495 map = isl_map_cow(map);
3496 if (!map || !dim)
3497 goto error;
3499 for (i = 0; i < map->n; ++i) {
3500 map->p[i] = isl_basic_map_reset_dim(map->p[i],
3501 isl_dim_copy(dim));
3502 if (!map->p[i])
3503 goto error;
3505 isl_dim_free(map->dim);
3506 map->dim = dim;
3508 return map;
3509 error:
3510 isl_map_free(map);
3511 isl_dim_free(dim);
3512 return NULL;
3515 __isl_give isl_set *isl_set_reset_dim(__isl_take isl_set *set,
3516 __isl_take isl_dim *dim)
3518 return (struct isl_set *) isl_map_reset_dim((struct isl_map *)set, dim);
3521 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
3523 isl_dim *dim;
3524 struct isl_basic_set *domain;
3525 unsigned n_in;
3526 unsigned n_out;
3528 if (!bmap)
3529 return NULL;
3530 dim = isl_dim_domain(isl_basic_map_get_dim(bmap));
3532 n_in = isl_basic_map_n_in(bmap);
3533 n_out = isl_basic_map_n_out(bmap);
3534 domain = isl_basic_set_from_basic_map(bmap);
3535 domain = isl_basic_set_project_out(domain, isl_dim_set, n_in, n_out);
3537 domain = isl_basic_set_reset_dim(domain, dim);
3539 return domain;
3542 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
3544 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
3547 struct isl_set *isl_map_range(struct isl_map *map)
3549 int i;
3550 struct isl_set *set;
3552 if (!map)
3553 goto error;
3554 if (isl_map_dim(map, isl_dim_in) == 0)
3555 return (isl_set *)map;
3557 map = isl_map_cow(map);
3558 if (!map)
3559 goto error;
3561 set = (struct isl_set *) map;
3562 set->dim = isl_dim_drop_inputs(set->dim, 0, set->dim->n_in);
3563 if (!set->dim)
3564 goto error;
3565 for (i = 0; i < map->n; ++i) {
3566 set->p[i] = isl_basic_map_range(map->p[i]);
3567 if (!set->p[i])
3568 goto error;
3570 ISL_F_CLR(set, ISL_MAP_DISJOINT);
3571 ISL_F_CLR(set, ISL_SET_NORMALIZED);
3572 return set;
3573 error:
3574 isl_map_free(map);
3575 return NULL;
3578 struct isl_map *isl_map_from_set(struct isl_set *set, struct isl_dim *dim)
3580 int i;
3581 struct isl_map *map = NULL;
3583 set = isl_set_cow(set);
3584 if (!set || !dim)
3585 goto error;
3586 isl_assert(set->ctx, isl_dim_compatible(set->dim, dim), goto error);
3587 map = (struct isl_map *)set;
3588 for (i = 0; i < set->n; ++i) {
3589 map->p[i] = isl_basic_map_from_basic_set(
3590 set->p[i], isl_dim_copy(dim));
3591 if (!map->p[i])
3592 goto error;
3594 isl_dim_free(map->dim);
3595 map->dim = dim;
3596 return map;
3597 error:
3598 isl_dim_free(dim);
3599 isl_set_free(set);
3600 return NULL;
3603 struct isl_map *isl_map_from_range(struct isl_set *set)
3605 return (struct isl_map *)set;
3608 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
3610 return isl_map_reverse(isl_map_from_range(set));
3613 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
3614 __isl_take isl_set *range)
3616 return isl_map_apply_range(isl_map_from_domain(domain),
3617 isl_map_from_range(range));
3620 struct isl_set *isl_set_from_map(struct isl_map *map)
3622 int i;
3623 struct isl_set *set = NULL;
3625 if (!map)
3626 return NULL;
3627 map = isl_map_cow(map);
3628 if (!map)
3629 return NULL;
3630 map->dim = isl_dim_as_set_dim(map->dim);
3631 if (!map->dim)
3632 goto error;
3633 set = (struct isl_set *)map;
3634 for (i = 0; i < map->n; ++i) {
3635 set->p[i] = isl_basic_set_from_basic_map(map->p[i]);
3636 if (!set->p[i])
3637 goto error;
3639 return set;
3640 error:
3641 isl_map_free(map);
3642 return NULL;
3645 struct isl_map *isl_map_alloc_dim(struct isl_dim *dim, int n, unsigned flags)
3647 struct isl_map *map;
3649 if (!dim)
3650 return NULL;
3651 isl_assert(dim->ctx, n >= 0, return NULL);
3652 map = isl_alloc(dim->ctx, struct isl_map,
3653 sizeof(struct isl_map) +
3654 (n - 1) * sizeof(struct isl_basic_map *));
3655 if (!map)
3656 goto error;
3658 map->ctx = dim->ctx;
3659 isl_ctx_ref(map->ctx);
3660 map->ref = 1;
3661 map->size = n;
3662 map->n = 0;
3663 map->dim = dim;
3664 map->flags = flags;
3665 return map;
3666 error:
3667 isl_dim_free(dim);
3668 return NULL;
3671 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
3672 unsigned nparam, unsigned in, unsigned out, int n,
3673 unsigned flags)
3675 struct isl_map *map;
3676 struct isl_dim *dims;
3678 dims = isl_dim_alloc(ctx, nparam, in, out);
3679 if (!dims)
3680 return NULL;
3682 map = isl_map_alloc_dim(dims, n, flags);
3683 return map;
3686 struct isl_basic_map *isl_basic_map_empty(struct isl_dim *dim)
3688 struct isl_basic_map *bmap;
3689 bmap = isl_basic_map_alloc_dim(dim, 0, 1, 0);
3690 bmap = isl_basic_map_set_to_empty(bmap);
3691 return bmap;
3694 struct isl_basic_set *isl_basic_set_empty(struct isl_dim *dim)
3696 struct isl_basic_set *bset;
3697 bset = isl_basic_set_alloc_dim(dim, 0, 1, 0);
3698 bset = isl_basic_set_set_to_empty(bset);
3699 return bset;
3702 struct isl_basic_map *isl_basic_map_empty_like(struct isl_basic_map *model)
3704 struct isl_basic_map *bmap;
3705 if (!model)
3706 return NULL;
3707 bmap = isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
3708 bmap = isl_basic_map_set_to_empty(bmap);
3709 return bmap;
3712 struct isl_basic_map *isl_basic_map_empty_like_map(struct isl_map *model)
3714 struct isl_basic_map *bmap;
3715 if (!model)
3716 return NULL;
3717 bmap = isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
3718 bmap = isl_basic_map_set_to_empty(bmap);
3719 return bmap;
3722 struct isl_basic_set *isl_basic_set_empty_like(struct isl_basic_set *model)
3724 struct isl_basic_set *bset;
3725 if (!model)
3726 return NULL;
3727 bset = isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
3728 bset = isl_basic_set_set_to_empty(bset);
3729 return bset;
3732 struct isl_basic_map *isl_basic_map_universe(struct isl_dim *dim)
3734 struct isl_basic_map *bmap;
3735 bmap = isl_basic_map_alloc_dim(dim, 0, 0, 0);
3736 return bmap;
3739 struct isl_basic_set *isl_basic_set_universe(struct isl_dim *dim)
3741 struct isl_basic_set *bset;
3742 bset = isl_basic_set_alloc_dim(dim, 0, 0, 0);
3743 return bset;
3746 __isl_give isl_basic_map *isl_basic_map_universe_like(
3747 __isl_keep isl_basic_map *model)
3749 if (!model)
3750 return NULL;
3751 return isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
3754 struct isl_basic_set *isl_basic_set_universe_like(struct isl_basic_set *model)
3756 if (!model)
3757 return NULL;
3758 return isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
3761 __isl_give isl_basic_set *isl_basic_set_universe_like_set(
3762 __isl_keep isl_set *model)
3764 if (!model)
3765 return NULL;
3766 return isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
3769 struct isl_map *isl_map_empty(struct isl_dim *dim)
3771 return isl_map_alloc_dim(dim, 0, ISL_MAP_DISJOINT);
3774 struct isl_map *isl_map_empty_like(struct isl_map *model)
3776 if (!model)
3777 return NULL;
3778 return isl_map_alloc_dim(isl_dim_copy(model->dim), 0, ISL_MAP_DISJOINT);
3781 struct isl_map *isl_map_empty_like_basic_map(struct isl_basic_map *model)
3783 if (!model)
3784 return NULL;
3785 return isl_map_alloc_dim(isl_dim_copy(model->dim), 0, ISL_MAP_DISJOINT);
3788 struct isl_set *isl_set_empty(struct isl_dim *dim)
3790 return isl_set_alloc_dim(dim, 0, ISL_MAP_DISJOINT);
3793 struct isl_set *isl_set_empty_like(struct isl_set *model)
3795 if (!model)
3796 return NULL;
3797 return isl_set_empty(isl_dim_copy(model->dim));
3800 struct isl_map *isl_map_universe(struct isl_dim *dim)
3802 struct isl_map *map;
3803 if (!dim)
3804 return NULL;
3805 map = isl_map_alloc_dim(isl_dim_copy(dim), 1, ISL_MAP_DISJOINT);
3806 map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
3807 return map;
3810 struct isl_set *isl_set_universe(struct isl_dim *dim)
3812 struct isl_set *set;
3813 if (!dim)
3814 return NULL;
3815 set = isl_set_alloc_dim(isl_dim_copy(dim), 1, ISL_MAP_DISJOINT);
3816 set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
3817 return set;
3820 __isl_give isl_set *isl_set_universe_like(__isl_keep isl_set *model)
3822 if (!model)
3823 return NULL;
3824 return isl_set_universe(isl_dim_copy(model->dim));
3827 struct isl_map *isl_map_dup(struct isl_map *map)
3829 int i;
3830 struct isl_map *dup;
3832 if (!map)
3833 return NULL;
3834 dup = isl_map_alloc_dim(isl_dim_copy(map->dim), map->n, map->flags);
3835 for (i = 0; i < map->n; ++i)
3836 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
3837 return dup;
3840 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
3841 __isl_take isl_basic_map *bmap)
3843 if (!bmap || !map)
3844 goto error;
3845 if (isl_basic_map_fast_is_empty(bmap)) {
3846 isl_basic_map_free(bmap);
3847 return map;
3849 isl_assert(map->ctx, isl_dim_equal(map->dim, bmap->dim), goto error);
3850 isl_assert(map->ctx, map->n < map->size, goto error);
3851 map->p[map->n] = bmap;
3852 map->n++;
3853 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3854 return map;
3855 error:
3856 if (map)
3857 isl_map_free(map);
3858 if (bmap)
3859 isl_basic_map_free(bmap);
3860 return NULL;
3863 void isl_map_free(struct isl_map *map)
3865 int i;
3867 if (!map)
3868 return;
3870 if (--map->ref > 0)
3871 return;
3873 isl_ctx_deref(map->ctx);
3874 for (i = 0; i < map->n; ++i)
3875 isl_basic_map_free(map->p[i]);
3876 isl_dim_free(map->dim);
3877 free(map);
3880 struct isl_map *isl_map_extend(struct isl_map *base,
3881 unsigned nparam, unsigned n_in, unsigned n_out)
3883 int i;
3885 base = isl_map_cow(base);
3886 if (!base)
3887 return NULL;
3889 base->dim = isl_dim_extend(base->dim, nparam, n_in, n_out);
3890 if (!base->dim)
3891 goto error;
3892 for (i = 0; i < base->n; ++i) {
3893 base->p[i] = isl_basic_map_extend_dim(base->p[i],
3894 isl_dim_copy(base->dim), 0, 0, 0);
3895 if (!base->p[i])
3896 goto error;
3898 return base;
3899 error:
3900 isl_map_free(base);
3901 return NULL;
3904 struct isl_set *isl_set_extend(struct isl_set *base,
3905 unsigned nparam, unsigned dim)
3907 return (struct isl_set *)isl_map_extend((struct isl_map *)base,
3908 nparam, 0, dim);
3911 static struct isl_basic_map *isl_basic_map_fix_pos_si(
3912 struct isl_basic_map *bmap, unsigned pos, int value)
3914 int j;
3916 bmap = isl_basic_map_cow(bmap);
3917 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
3918 j = isl_basic_map_alloc_equality(bmap);
3919 if (j < 0)
3920 goto error;
3921 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
3922 isl_int_set_si(bmap->eq[j][pos], -1);
3923 isl_int_set_si(bmap->eq[j][0], value);
3924 bmap = isl_basic_map_simplify(bmap);
3925 return isl_basic_map_finalize(bmap);
3926 error:
3927 isl_basic_map_free(bmap);
3928 return NULL;
3931 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
3932 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
3934 int j;
3936 bmap = isl_basic_map_cow(bmap);
3937 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
3938 j = isl_basic_map_alloc_equality(bmap);
3939 if (j < 0)
3940 goto error;
3941 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
3942 isl_int_set_si(bmap->eq[j][pos], -1);
3943 isl_int_set(bmap->eq[j][0], value);
3944 bmap = isl_basic_map_simplify(bmap);
3945 return isl_basic_map_finalize(bmap);
3946 error:
3947 isl_basic_map_free(bmap);
3948 return NULL;
3951 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
3952 enum isl_dim_type type, unsigned pos, int value)
3954 if (!bmap)
3955 return NULL;
3956 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
3957 return isl_basic_map_fix_pos_si(bmap,
3958 isl_basic_map_offset(bmap, type) + pos, value);
3959 error:
3960 isl_basic_map_free(bmap);
3961 return NULL;
3964 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
3965 enum isl_dim_type type, unsigned pos, isl_int value)
3967 if (!bmap)
3968 return NULL;
3969 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
3970 return isl_basic_map_fix_pos(bmap,
3971 isl_basic_map_offset(bmap, type) + pos, value);
3972 error:
3973 isl_basic_map_free(bmap);
3974 return NULL;
3977 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
3978 enum isl_dim_type type, unsigned pos, int value)
3980 return (struct isl_basic_set *)
3981 isl_basic_map_fix_si((struct isl_basic_map *)bset,
3982 type, pos, value);
3985 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
3986 enum isl_dim_type type, unsigned pos, isl_int value)
3988 return (struct isl_basic_set *)
3989 isl_basic_map_fix((struct isl_basic_map *)bset,
3990 type, pos, value);
3993 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
3994 unsigned input, int value)
3996 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
3999 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
4000 unsigned dim, int value)
4002 return (struct isl_basic_set *)
4003 isl_basic_map_fix_si((struct isl_basic_map *)bset,
4004 isl_dim_set, dim, value);
4007 struct isl_map *isl_map_fix_si(struct isl_map *map,
4008 enum isl_dim_type type, unsigned pos, int value)
4010 int i;
4012 map = isl_map_cow(map);
4013 if (!map)
4014 return NULL;
4016 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
4017 for (i = 0; i < map->n; ++i) {
4018 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
4019 if (!map->p[i])
4020 goto error;
4022 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4023 return map;
4024 error:
4025 isl_map_free(map);
4026 return NULL;
4029 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
4030 enum isl_dim_type type, unsigned pos, int value)
4032 return (struct isl_set *)
4033 isl_map_fix_si((struct isl_map *)set, type, pos, value);
4036 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
4037 enum isl_dim_type type, unsigned pos, isl_int value)
4039 int i;
4041 map = isl_map_cow(map);
4042 if (!map)
4043 return NULL;
4045 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
4046 for (i = 0; i < map->n; ++i) {
4047 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
4048 if (!map->p[i])
4049 goto error;
4051 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4052 return map;
4053 error:
4054 isl_map_free(map);
4055 return NULL;
4058 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
4059 enum isl_dim_type type, unsigned pos, isl_int value)
4061 return (struct isl_set *)isl_map_fix((isl_map *)set, type, pos, value);
4064 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
4065 unsigned input, int value)
4067 return isl_map_fix_si(map, isl_dim_in, input, value);
4070 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
4072 return (struct isl_set *)
4073 isl_map_fix_si((struct isl_map *)set, isl_dim_set, dim, value);
4076 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
4077 __isl_take isl_basic_map *bmap,
4078 enum isl_dim_type type, unsigned pos, int value)
4080 int j;
4082 if (!bmap)
4083 return NULL;
4084 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
4085 pos += isl_basic_map_offset(bmap, type);
4086 bmap = isl_basic_map_cow(bmap);
4087 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
4088 j = isl_basic_map_alloc_inequality(bmap);
4089 if (j < 0)
4090 goto error;
4091 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
4092 isl_int_set_si(bmap->ineq[j][pos], 1);
4093 isl_int_set_si(bmap->ineq[j][0], -value);
4094 bmap = isl_basic_map_simplify(bmap);
4095 return isl_basic_map_finalize(bmap);
4096 error:
4097 isl_basic_map_free(bmap);
4098 return NULL;
4101 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
4102 unsigned dim, isl_int value)
4104 int j;
4106 bset = isl_basic_set_cow(bset);
4107 bset = isl_basic_set_extend_constraints(bset, 0, 1);
4108 j = isl_basic_set_alloc_inequality(bset);
4109 if (j < 0)
4110 goto error;
4111 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
4112 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
4113 isl_int_neg(bset->ineq[j][0], value);
4114 bset = isl_basic_set_simplify(bset);
4115 return isl_basic_set_finalize(bset);
4116 error:
4117 isl_basic_set_free(bset);
4118 return NULL;
4121 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
4122 enum isl_dim_type type, unsigned pos, int value)
4124 int i;
4126 map = isl_map_cow(map);
4127 if (!map)
4128 return NULL;
4130 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
4131 for (i = 0; i < map->n; ++i) {
4132 map->p[i] = isl_basic_map_lower_bound_si(map->p[i],
4133 type, pos, value);
4134 if (!map->p[i])
4135 goto error;
4137 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4138 return map;
4139 error:
4140 isl_map_free(map);
4141 return NULL;
4144 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
4145 enum isl_dim_type type, unsigned pos, int value)
4147 return (struct isl_set *)
4148 isl_map_lower_bound_si((struct isl_map *)set, type, pos, value);
4151 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
4152 isl_int value)
4154 int i;
4156 set = isl_set_cow(set);
4157 if (!set)
4158 return NULL;
4160 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
4161 for (i = 0; i < set->n; ++i) {
4162 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
4163 if (!set->p[i])
4164 goto error;
4166 return set;
4167 error:
4168 isl_set_free(set);
4169 return NULL;
4172 struct isl_map *isl_map_reverse(struct isl_map *map)
4174 int i;
4176 map = isl_map_cow(map);
4177 if (!map)
4178 return NULL;
4180 map->dim = isl_dim_reverse(map->dim);
4181 if (!map->dim)
4182 goto error;
4183 for (i = 0; i < map->n; ++i) {
4184 map->p[i] = isl_basic_map_reverse(map->p[i]);
4185 if (!map->p[i])
4186 goto error;
4188 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4189 return map;
4190 error:
4191 isl_map_free(map);
4192 return NULL;
4195 static struct isl_map *isl_basic_map_partial_lexopt(
4196 struct isl_basic_map *bmap, struct isl_basic_set *dom,
4197 struct isl_set **empty, int max)
4199 if (!bmap)
4200 goto error;
4201 if (bmap->ctx->opt->pip == ISL_PIP_PIP)
4202 return isl_pip_basic_map_lexopt(bmap, dom, empty, max);
4203 else
4204 return isl_tab_basic_map_partial_lexopt(bmap, dom, empty, max);
4205 error:
4206 isl_basic_map_free(bmap);
4207 isl_basic_set_free(dom);
4208 if (empty)
4209 *empty = NULL;
4210 return NULL;
4213 struct isl_map *isl_basic_map_partial_lexmax(
4214 struct isl_basic_map *bmap, struct isl_basic_set *dom,
4215 struct isl_set **empty)
4217 return isl_basic_map_partial_lexopt(bmap, dom, empty, 1);
4220 struct isl_map *isl_basic_map_partial_lexmin(
4221 struct isl_basic_map *bmap, struct isl_basic_set *dom,
4222 struct isl_set **empty)
4224 return isl_basic_map_partial_lexopt(bmap, dom, empty, 0);
4227 struct isl_set *isl_basic_set_partial_lexmin(
4228 struct isl_basic_set *bset, struct isl_basic_set *dom,
4229 struct isl_set **empty)
4231 return (struct isl_set *)
4232 isl_basic_map_partial_lexmin((struct isl_basic_map *)bset,
4233 dom, empty);
4236 struct isl_set *isl_basic_set_partial_lexmax(
4237 struct isl_basic_set *bset, struct isl_basic_set *dom,
4238 struct isl_set **empty)
4240 return (struct isl_set *)
4241 isl_basic_map_partial_lexmax((struct isl_basic_map *)bset,
4242 dom, empty);
4245 /* Given a basic map "bmap", compute the lexicograhically minimal
4246 * (or maximal) image element for each domain element in dom.
4247 * Set *empty to those elements in dom that do not have an image element.
4249 * We first make sure the basic sets in dom are disjoint and then
4250 * simply collect the results over each of the basic sets separately.
4251 * We could probably improve the efficiency a bit by moving the union
4252 * domain down into the parametric integer programming.
4254 static __isl_give isl_map *basic_map_partial_lexopt(
4255 __isl_take isl_basic_map *bmap, __isl_take isl_set *dom,
4256 __isl_give isl_set **empty, int max)
4258 int i;
4259 struct isl_map *res;
4261 dom = isl_set_make_disjoint(dom);
4262 if (!dom)
4263 goto error;
4265 if (isl_set_fast_is_empty(dom)) {
4266 res = isl_map_empty_like_basic_map(bmap);
4267 *empty = isl_set_empty_like(dom);
4268 isl_set_free(dom);
4269 isl_basic_map_free(bmap);
4270 return res;
4273 res = isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap),
4274 isl_basic_set_copy(dom->p[0]), empty, max);
4276 for (i = 1; i < dom->n; ++i) {
4277 struct isl_map *res_i;
4278 struct isl_set *empty_i;
4280 res_i = isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap),
4281 isl_basic_set_copy(dom->p[i]), &empty_i, max);
4283 res = isl_map_union_disjoint(res, res_i);
4284 *empty = isl_set_union_disjoint(*empty, empty_i);
4287 isl_set_free(dom);
4288 isl_basic_map_free(bmap);
4289 return res;
4290 error:
4291 *empty = NULL;
4292 isl_set_free(dom);
4293 isl_basic_map_free(bmap);
4294 return NULL;
4297 /* Given a map "map", compute the lexicograhically minimal
4298 * (or maximal) image element for each domain element in dom.
4299 * Set *empty to those elements in dom that do not have an image element.
4301 * We first compute the lexicographically minimal or maximal element
4302 * in the first basic map. This results in a partial solution "res"
4303 * and a subset "todo" of dom that still need to be handled.
4304 * We then consider each of the remaining maps in "map" and successively
4305 * improve both "res" and "todo".
4307 * Let res^k and todo^k be the results after k steps and let i = k + 1.
4308 * Assume we are computing the lexicographical maximum.
4309 * We first intersect basic map i with a relation that maps elements
4310 * to elements that are lexicographically larger than the image elements
4311 * in res^k and the compute the maximum image element of this intersection.
4312 * The result ("better") corresponds to those image elements in basic map i
4313 * that are better than what we had before. The remainder ("keep") are the
4314 * domain elements for which the image element in res_k was better.
4315 * We also compute the lexicographical maximum of basic map i in todo^k.
4316 * res^i is the result of the operation + better + those elements in
4317 * res^k that we should keep
4318 * todo^i is the remainder of the maximum operation on todo^k.
4320 static __isl_give isl_map *isl_map_partial_lexopt(
4321 __isl_take isl_map *map, __isl_take isl_set *dom,
4322 __isl_give isl_set **empty, int max)
4324 int i;
4325 struct isl_map *res;
4326 struct isl_set *todo;
4328 if (!map || !dom)
4329 goto error;
4331 if (isl_map_fast_is_empty(map)) {
4332 if (empty)
4333 *empty = dom;
4334 else
4335 isl_set_free(dom);
4336 return map;
4339 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
4340 isl_set_copy(dom), &todo, max);
4342 for (i = 1; i < map->n; ++i) {
4343 struct isl_map *lt;
4344 struct isl_map *better;
4345 struct isl_set *keep;
4346 struct isl_map *res_i;
4347 struct isl_set *todo_i;
4348 struct isl_dim *dim = isl_map_get_dim(res);
4350 dim = isl_dim_range(dim);
4351 if (max)
4352 lt = isl_map_lex_lt(dim);
4353 else
4354 lt = isl_map_lex_gt(dim);
4355 lt = isl_map_apply_range(isl_map_copy(res), lt);
4356 lt = isl_map_intersect(lt,
4357 isl_map_from_basic_map(isl_basic_map_copy(map->p[i])));
4358 better = isl_map_partial_lexopt(lt,
4359 isl_map_domain(isl_map_copy(res)),
4360 &keep, max);
4362 res_i = basic_map_partial_lexopt(isl_basic_map_copy(map->p[i]),
4363 todo, &todo_i, max);
4365 res = isl_map_intersect_domain(res, keep);
4366 res = isl_map_union_disjoint(res, res_i);
4367 res = isl_map_union_disjoint(res, better);
4368 todo = todo_i;
4371 isl_set_free(dom);
4372 isl_map_free(map);
4374 if (empty)
4375 *empty = todo;
4376 else
4377 isl_set_free(todo);
4379 return res;
4380 error:
4381 if (empty)
4382 *empty = NULL;
4383 isl_set_free(dom);
4384 isl_map_free(map);
4385 return NULL;
4388 __isl_give isl_map *isl_map_partial_lexmax(
4389 __isl_take isl_map *map, __isl_take isl_set *dom,
4390 __isl_give isl_set **empty)
4392 return isl_map_partial_lexopt(map, dom, empty, 1);
4395 __isl_give isl_map *isl_map_partial_lexmin(
4396 __isl_take isl_map *map, __isl_take isl_set *dom,
4397 __isl_give isl_set **empty)
4399 return isl_map_partial_lexopt(map, dom, empty, 0);
4402 __isl_give isl_set *isl_set_partial_lexmin(
4403 __isl_take isl_set *set, __isl_take isl_set *dom,
4404 __isl_give isl_set **empty)
4406 return (struct isl_set *)
4407 isl_map_partial_lexmin((struct isl_map *)set,
4408 dom, empty);
4411 __isl_give isl_set *isl_set_partial_lexmax(
4412 __isl_take isl_set *set, __isl_take isl_set *dom,
4413 __isl_give isl_set **empty)
4415 return (struct isl_set *)
4416 isl_map_partial_lexmax((struct isl_map *)set,
4417 dom, empty);
4420 __isl_give isl_map *isl_basic_map_lexopt(__isl_take isl_basic_map *bmap, int max)
4422 struct isl_basic_set *dom = NULL;
4423 struct isl_dim *dom_dim;
4425 if (!bmap)
4426 goto error;
4427 dom_dim = isl_dim_domain(isl_dim_copy(bmap->dim));
4428 dom = isl_basic_set_universe(dom_dim);
4429 return isl_basic_map_partial_lexopt(bmap, dom, NULL, max);
4430 error:
4431 isl_basic_map_free(bmap);
4432 return NULL;
4435 __isl_give isl_map *isl_basic_map_lexmin(__isl_take isl_basic_map *bmap)
4437 return isl_basic_map_lexopt(bmap, 0);
4440 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
4442 return isl_basic_map_lexopt(bmap, 1);
4445 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
4447 return (isl_set *)isl_basic_map_lexmin((isl_basic_map *)bset);
4450 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
4452 return (isl_set *)isl_basic_map_lexmax((isl_basic_map *)bset);
4455 __isl_give isl_map *isl_map_lexopt(__isl_take isl_map *map, int max)
4457 struct isl_set *dom = NULL;
4458 struct isl_dim *dom_dim;
4460 if (!map)
4461 goto error;
4462 dom_dim = isl_dim_domain(isl_dim_copy(map->dim));
4463 dom = isl_set_universe(dom_dim);
4464 return isl_map_partial_lexopt(map, dom, NULL, max);
4465 error:
4466 isl_map_free(map);
4467 return NULL;
4470 __isl_give isl_map *isl_map_lexmin(__isl_take isl_map *map)
4472 return isl_map_lexopt(map, 0);
4475 __isl_give isl_map *isl_map_lexmax(__isl_take isl_map *map)
4477 return isl_map_lexopt(map, 1);
4480 __isl_give isl_set *isl_set_lexmin(__isl_take isl_set *set)
4482 return (isl_set *)isl_map_lexmin((isl_map *)set);
4485 __isl_give isl_set *isl_set_lexmax(__isl_take isl_set *set)
4487 return (isl_set *)isl_map_lexmax((isl_map *)set);
4490 /* Apply a preimage specified by "mat" on the parameters of "bset".
4491 * bset is assumed to have only parameters and divs.
4493 static struct isl_basic_set *basic_set_parameter_preimage(
4494 struct isl_basic_set *bset, struct isl_mat *mat)
4496 unsigned nparam;
4498 if (!bset || !mat)
4499 goto error;
4501 bset->dim = isl_dim_cow(bset->dim);
4502 if (!bset->dim)
4503 goto error;
4505 nparam = isl_basic_set_dim(bset, isl_dim_param);
4507 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
4509 bset->dim->nparam = 0;
4510 bset->dim->n_out = nparam;
4511 bset = isl_basic_set_preimage(bset, mat);
4512 if (bset) {
4513 bset->dim->nparam = bset->dim->n_out;
4514 bset->dim->n_out = 0;
4516 return bset;
4517 error:
4518 isl_mat_free(mat);
4519 isl_basic_set_free(bset);
4520 return NULL;
4523 /* Apply a preimage specified by "mat" on the parameters of "set".
4524 * set is assumed to have only parameters and divs.
4526 static struct isl_set *set_parameter_preimage(
4527 struct isl_set *set, struct isl_mat *mat)
4529 struct isl_dim *dim = NULL;
4530 unsigned nparam;
4532 if (!set || !mat)
4533 goto error;
4535 dim = isl_dim_copy(set->dim);
4536 dim = isl_dim_cow(dim);
4537 if (!dim)
4538 goto error;
4540 nparam = isl_set_dim(set, isl_dim_param);
4542 isl_assert(set->ctx, mat->n_row == 1 + nparam, goto error);
4544 dim->nparam = 0;
4545 dim->n_out = nparam;
4546 isl_set_reset_dim(set, dim);
4547 set = isl_set_preimage(set, mat);
4548 if (!set)
4549 goto error2;
4550 dim = isl_dim_copy(set->dim);
4551 dim = isl_dim_cow(dim);
4552 if (!dim)
4553 goto error2;
4554 dim->nparam = dim->n_out;
4555 dim->n_out = 0;
4556 isl_set_reset_dim(set, dim);
4557 return set;
4558 error:
4559 isl_dim_free(dim);
4560 isl_mat_free(mat);
4561 error2:
4562 isl_set_free(set);
4563 return NULL;
4566 /* Intersect the basic set "bset" with the affine space specified by the
4567 * equalities in "eq".
4569 static struct isl_basic_set *basic_set_append_equalities(
4570 struct isl_basic_set *bset, struct isl_mat *eq)
4572 int i, k;
4573 unsigned len;
4575 if (!bset || !eq)
4576 goto error;
4578 bset = isl_basic_set_extend_dim(bset, isl_dim_copy(bset->dim), 0,
4579 eq->n_row, 0);
4580 if (!bset)
4581 goto error;
4583 len = 1 + isl_dim_total(bset->dim) + bset->extra;
4584 for (i = 0; i < eq->n_row; ++i) {
4585 k = isl_basic_set_alloc_equality(bset);
4586 if (k < 0)
4587 goto error;
4588 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
4589 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
4591 isl_mat_free(eq);
4593 bset = isl_basic_set_gauss(bset, NULL);
4594 bset = isl_basic_set_finalize(bset);
4596 return bset;
4597 error:
4598 isl_mat_free(eq);
4599 isl_basic_set_free(bset);
4600 return NULL;
4603 /* Intersect the set "set" with the affine space specified by the
4604 * equalities in "eq".
4606 static struct isl_set *set_append_equalities(struct isl_set *set,
4607 struct isl_mat *eq)
4609 int i;
4611 if (!set || !eq)
4612 goto error;
4614 for (i = 0; i < set->n; ++i) {
4615 set->p[i] = basic_set_append_equalities(set->p[i],
4616 isl_mat_copy(eq));
4617 if (!set->p[i])
4618 goto error;
4620 isl_mat_free(eq);
4621 return set;
4622 error:
4623 isl_mat_free(eq);
4624 isl_set_free(set);
4625 return NULL;
4628 /* Project the given basic set onto its parameter domain, possibly introducing
4629 * new, explicit, existential variables in the constraints.
4630 * The input has parameters and (possibly implicit) existential variables.
4631 * The output has the same parameters, but only
4632 * explicit existentially quantified variables.
4634 * The actual projection is performed by pip, but pip doesn't seem
4635 * to like equalities very much, so we first remove the equalities
4636 * among the parameters by performing a variable compression on
4637 * the parameters. Afterward, an inverse transformation is performed
4638 * and the equalities among the parameters are inserted back in.
4640 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
4642 int i, j;
4643 struct isl_mat *eq;
4644 struct isl_mat *T, *T2;
4645 struct isl_set *set;
4646 unsigned nparam, n_div;
4648 bset = isl_basic_set_cow(bset);
4649 if (!bset)
4650 return NULL;
4652 if (bset->n_eq == 0)
4653 return isl_basic_set_lexmin(bset);
4655 isl_basic_set_gauss(bset, NULL);
4657 nparam = isl_basic_set_dim(bset, isl_dim_param);
4658 n_div = isl_basic_set_dim(bset, isl_dim_div);
4660 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
4661 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
4662 ++i;
4664 if (i == bset->n_eq)
4665 return isl_basic_set_lexmin(bset);
4667 eq = isl_mat_sub_alloc(bset->ctx, bset->eq, i, bset->n_eq - i,
4668 0, 1 + nparam);
4669 eq = isl_mat_cow(eq);
4670 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
4671 if (T && T->n_col == 0) {
4672 isl_mat_free(T);
4673 isl_mat_free(T2);
4674 isl_mat_free(eq);
4675 bset = isl_basic_set_set_to_empty(bset);
4676 return isl_set_from_basic_set(bset);
4678 bset = basic_set_parameter_preimage(bset, T);
4680 set = isl_basic_set_lexmin(bset);
4681 set = set_parameter_preimage(set, T2);
4682 set = set_append_equalities(set, eq);
4683 return set;
4686 /* Compute an explicit representation for all the existentially
4687 * quantified variables.
4688 * The input and output dimensions are first turned into parameters.
4689 * compute_divs then returns a map with the same parameters and
4690 * no input or output dimensions and the dimension specification
4691 * is reset to that of the input.
4693 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
4695 struct isl_basic_set *bset;
4696 struct isl_set *set;
4697 struct isl_map *map;
4698 struct isl_dim *dim, *orig_dim = NULL;
4699 unsigned nparam;
4700 unsigned n_in;
4701 unsigned n_out;
4703 bmap = isl_basic_map_cow(bmap);
4704 if (!bmap)
4705 return NULL;
4707 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4708 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4709 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4710 dim = isl_dim_set_alloc(bmap->ctx, nparam + n_in + n_out, 0);
4711 if (!dim)
4712 goto error;
4714 orig_dim = bmap->dim;
4715 bmap->dim = dim;
4716 bset = (struct isl_basic_set *)bmap;
4718 set = parameter_compute_divs(bset);
4719 map = (struct isl_map *)set;
4720 map = isl_map_reset_dim(map, orig_dim);
4722 return map;
4723 error:
4724 isl_basic_map_free(bmap);
4725 return NULL;
4728 static int basic_map_divs_known(__isl_keep isl_basic_map *bmap)
4730 int i;
4731 unsigned off;
4733 if (!bmap)
4734 return -1;
4736 off = isl_dim_total(bmap->dim);
4737 for (i = 0; i < bmap->n_div; ++i) {
4738 if (isl_int_is_zero(bmap->div[i][0]))
4739 return 0;
4740 isl_assert(bmap->ctx, isl_int_is_zero(bmap->div[i][1+1+off+i]),
4741 return -1);
4743 return 1;
4746 static int map_divs_known(__isl_keep isl_map *map)
4748 int i;
4750 if (!map)
4751 return -1;
4753 for (i = 0; i < map->n; ++i) {
4754 int known = basic_map_divs_known(map->p[i]);
4755 if (known <= 0)
4756 return known;
4759 return 1;
4762 /* If bmap contains any unknown divs, then compute explicit
4763 * expressions for them. However, this computation may be
4764 * quite expensive, so first try to remove divs that aren't
4765 * strictly needed.
4767 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
4769 int i;
4770 int known;
4771 struct isl_map *map;
4773 known = basic_map_divs_known(bmap);
4774 if (known < 0)
4775 goto error;
4776 if (known)
4777 return isl_map_from_basic_map(bmap);
4779 bmap = isl_basic_map_drop_redundant_divs(bmap);
4781 known = basic_map_divs_known(bmap);
4782 if (known < 0)
4783 goto error;
4784 if (known)
4785 return isl_map_from_basic_map(bmap);
4787 map = compute_divs(bmap);
4788 return map;
4789 error:
4790 isl_basic_map_free(bmap);
4791 return NULL;
4794 struct isl_map *isl_map_compute_divs(struct isl_map *map)
4796 int i;
4797 int known;
4798 struct isl_map *res;
4800 if (!map)
4801 return NULL;
4802 if (map->n == 0)
4803 return map;
4805 known = map_divs_known(map);
4806 if (known < 0) {
4807 isl_map_free(map);
4808 return NULL;
4810 if (known)
4811 return map;
4813 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
4814 for (i = 1 ; i < map->n; ++i) {
4815 struct isl_map *r2;
4816 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
4817 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
4818 res = isl_map_union_disjoint(res, r2);
4819 else
4820 res = isl_map_union(res, r2);
4822 isl_map_free(map);
4824 return res;
4827 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
4829 return (struct isl_set *)
4830 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
4833 struct isl_set *isl_set_compute_divs(struct isl_set *set)
4835 return (struct isl_set *)
4836 isl_map_compute_divs((struct isl_map *)set);
4839 struct isl_set *isl_map_domain(struct isl_map *map)
4841 int i;
4842 struct isl_set *set;
4844 if (!map)
4845 goto error;
4847 map = isl_map_cow(map);
4848 if (!map)
4849 return NULL;
4851 set = (struct isl_set *)map;
4852 set->dim = isl_dim_domain(set->dim);
4853 if (!set->dim)
4854 goto error;
4855 for (i = 0; i < map->n; ++i) {
4856 set->p[i] = isl_basic_map_domain(map->p[i]);
4857 if (!set->p[i])
4858 goto error;
4860 ISL_F_CLR(set, ISL_MAP_DISJOINT);
4861 ISL_F_CLR(set, ISL_SET_NORMALIZED);
4862 return set;
4863 error:
4864 isl_map_free(map);
4865 return NULL;
4868 struct isl_map *isl_map_union_disjoint(
4869 struct isl_map *map1, struct isl_map *map2)
4871 int i;
4872 unsigned flags = 0;
4873 struct isl_map *map = NULL;
4875 if (!map1 || !map2)
4876 goto error;
4878 if (map1->n == 0) {
4879 isl_map_free(map1);
4880 return map2;
4882 if (map2->n == 0) {
4883 isl_map_free(map2);
4884 return map1;
4887 isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
4889 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
4890 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
4891 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
4893 map = isl_map_alloc_dim(isl_dim_copy(map1->dim),
4894 map1->n + map2->n, flags);
4895 if (!map)
4896 goto error;
4897 for (i = 0; i < map1->n; ++i) {
4898 map = isl_map_add_basic_map(map,
4899 isl_basic_map_copy(map1->p[i]));
4900 if (!map)
4901 goto error;
4903 for (i = 0; i < map2->n; ++i) {
4904 map = isl_map_add_basic_map(map,
4905 isl_basic_map_copy(map2->p[i]));
4906 if (!map)
4907 goto error;
4909 isl_map_free(map1);
4910 isl_map_free(map2);
4911 return map;
4912 error:
4913 isl_map_free(map);
4914 isl_map_free(map1);
4915 isl_map_free(map2);
4916 return NULL;
4919 struct isl_map *isl_map_union(struct isl_map *map1, struct isl_map *map2)
4921 map1 = isl_map_union_disjoint(map1, map2);
4922 if (!map1)
4923 return NULL;
4924 if (map1->n > 1)
4925 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
4926 return map1;
4929 struct isl_set *isl_set_union_disjoint(
4930 struct isl_set *set1, struct isl_set *set2)
4932 return (struct isl_set *)
4933 isl_map_union_disjoint(
4934 (struct isl_map *)set1, (struct isl_map *)set2);
4937 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
4939 return (struct isl_set *)
4940 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
4943 struct isl_map *isl_map_intersect_range(
4944 struct isl_map *map, struct isl_set *set)
4946 unsigned flags = 0;
4947 struct isl_map *result;
4948 int i, j;
4950 if (!map || !set)
4951 goto error;
4953 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
4954 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
4955 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
4957 result = isl_map_alloc_dim(isl_dim_copy(map->dim),
4958 map->n * set->n, flags);
4959 if (!result)
4960 goto error;
4961 for (i = 0; i < map->n; ++i)
4962 for (j = 0; j < set->n; ++j) {
4963 result = isl_map_add_basic_map(result,
4964 isl_basic_map_intersect_range(
4965 isl_basic_map_copy(map->p[i]),
4966 isl_basic_set_copy(set->p[j])));
4967 if (!result)
4968 goto error;
4970 isl_map_free(map);
4971 isl_set_free(set);
4972 return result;
4973 error:
4974 isl_map_free(map);
4975 isl_set_free(set);
4976 return NULL;
4979 struct isl_map *isl_map_intersect_domain(
4980 struct isl_map *map, struct isl_set *set)
4982 return isl_map_reverse(
4983 isl_map_intersect_range(isl_map_reverse(map), set));
4986 struct isl_map *isl_map_apply_domain(
4987 struct isl_map *map1, struct isl_map *map2)
4989 if (!map1 || !map2)
4990 goto error;
4991 map1 = isl_map_reverse(map1);
4992 map1 = isl_map_apply_range(map1, map2);
4993 return isl_map_reverse(map1);
4994 error:
4995 isl_map_free(map1);
4996 isl_map_free(map2);
4997 return NULL;
5000 struct isl_map *isl_map_apply_range(
5001 struct isl_map *map1, struct isl_map *map2)
5003 struct isl_dim *dim_result;
5004 struct isl_map *result;
5005 int i, j;
5007 if (!map1 || !map2)
5008 goto error;
5010 dim_result = isl_dim_join(isl_dim_copy(map1->dim),
5011 isl_dim_copy(map2->dim));
5013 result = isl_map_alloc_dim(dim_result, map1->n * map2->n, 0);
5014 if (!result)
5015 goto error;
5016 for (i = 0; i < map1->n; ++i)
5017 for (j = 0; j < map2->n; ++j) {
5018 result = isl_map_add_basic_map(result,
5019 isl_basic_map_apply_range(
5020 isl_basic_map_copy(map1->p[i]),
5021 isl_basic_map_copy(map2->p[j])));
5022 if (!result)
5023 goto error;
5025 isl_map_free(map1);
5026 isl_map_free(map2);
5027 if (result && result->n <= 1)
5028 ISL_F_SET(result, ISL_MAP_DISJOINT);
5029 return result;
5030 error:
5031 isl_map_free(map1);
5032 isl_map_free(map2);
5033 return NULL;
5037 * returns range - domain
5039 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
5041 isl_dim *dims, *target_dim;
5042 struct isl_basic_set *bset;
5043 unsigned dim;
5044 unsigned nparam;
5045 int i;
5047 if (!bmap)
5048 goto error;
5049 isl_assert(bmap->ctx, isl_dim_tuple_match(bmap->dim, isl_dim_in,
5050 bmap->dim, isl_dim_out),
5051 goto error);
5052 target_dim = isl_dim_domain(isl_basic_map_get_dim(bmap));
5053 dim = isl_basic_map_n_in(bmap);
5054 nparam = isl_basic_map_n_param(bmap);
5055 bset = isl_basic_set_from_basic_map(bmap);
5056 bset = isl_basic_set_cow(bset);
5057 dims = isl_basic_set_get_dim(bset);
5058 dims = isl_dim_add(dims, isl_dim_set, dim);
5059 bset = isl_basic_set_extend_dim(bset, dims, 0, dim, 0);
5060 bset = isl_basic_set_swap_vars(bset, 2*dim);
5061 for (i = 0; i < dim; ++i) {
5062 int j = isl_basic_map_alloc_equality(
5063 (struct isl_basic_map *)bset);
5064 if (j < 0)
5065 goto error;
5066 isl_seq_clr(bset->eq[j], 1 + isl_basic_set_total_dim(bset));
5067 isl_int_set_si(bset->eq[j][1+nparam+i], 1);
5068 isl_int_set_si(bset->eq[j][1+nparam+dim+i], 1);
5069 isl_int_set_si(bset->eq[j][1+nparam+2*dim+i], -1);
5071 bset = isl_basic_set_project_out(bset, isl_dim_set, dim, 2*dim);
5072 bset = isl_basic_set_reset_dim(bset, target_dim);
5073 return bset;
5074 error:
5075 isl_basic_map_free(bmap);
5076 return NULL;
5080 * returns range - domain
5082 struct isl_set *isl_map_deltas(struct isl_map *map)
5084 int i;
5085 isl_dim *dim;
5086 struct isl_set *result;
5088 if (!map)
5089 return NULL;
5091 isl_assert(map->ctx, isl_dim_tuple_match(map->dim, isl_dim_in,
5092 map->dim, isl_dim_out),
5093 goto error);
5094 dim = isl_map_get_dim(map);
5095 dim = isl_dim_domain(dim);
5096 result = isl_set_alloc_dim(dim, map->n, map->flags);
5097 if (!result)
5098 goto error;
5099 for (i = 0; i < map->n; ++i)
5100 result = isl_set_add_basic_set(result,
5101 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
5102 isl_map_free(map);
5103 return result;
5104 error:
5105 isl_map_free(map);
5106 return NULL;
5109 static struct isl_basic_map *basic_map_identity(struct isl_dim *dims)
5111 struct isl_basic_map *bmap;
5112 unsigned nparam;
5113 unsigned dim;
5114 int i;
5116 if (!dims)
5117 return NULL;
5119 nparam = dims->nparam;
5120 dim = dims->n_out;
5121 bmap = isl_basic_map_alloc_dim(dims, 0, dim, 0);
5122 if (!bmap)
5123 goto error;
5125 for (i = 0; i < dim; ++i) {
5126 int j = isl_basic_map_alloc_equality(bmap);
5127 if (j < 0)
5128 goto error;
5129 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
5130 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
5131 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
5133 return isl_basic_map_finalize(bmap);
5134 error:
5135 isl_basic_map_free(bmap);
5136 return NULL;
5139 struct isl_basic_map *isl_basic_map_identity(struct isl_dim *set_dim)
5141 struct isl_dim *dim = isl_dim_map(set_dim);
5142 if (!dim)
5143 return NULL;
5144 return basic_map_identity(dim);
5147 struct isl_basic_map *isl_basic_map_identity_like(struct isl_basic_map *model)
5149 if (!model || !model->dim)
5150 return NULL;
5151 isl_assert(model->ctx,
5152 model->dim->n_in == model->dim->n_out, return NULL);
5153 return basic_map_identity(isl_dim_copy(model->dim));
5156 static struct isl_map *map_identity(struct isl_dim *dim)
5158 struct isl_map *map = isl_map_alloc_dim(dim, 1, ISL_MAP_DISJOINT);
5159 return isl_map_add_basic_map(map, basic_map_identity(isl_dim_copy(dim)));
5162 struct isl_map *isl_map_identity(struct isl_dim *set_dim)
5164 struct isl_dim *dim = isl_dim_map(set_dim);
5165 if (!dim)
5166 return NULL;
5167 return map_identity(dim);
5170 struct isl_map *isl_map_identity_like(struct isl_map *model)
5172 if (!model || !model->dim)
5173 return NULL;
5174 isl_assert(model->ctx,
5175 model->dim->n_in == model->dim->n_out, return NULL);
5176 return map_identity(isl_dim_copy(model->dim));
5179 struct isl_map *isl_map_identity_like_basic_map(struct isl_basic_map *model)
5181 if (!model || !model->dim)
5182 return NULL;
5183 isl_assert(model->ctx,
5184 model->dim->n_in == model->dim->n_out, return NULL);
5185 return map_identity(isl_dim_copy(model->dim));
5188 /* Construct a basic set with all set dimensions having only non-negative
5189 * values.
5191 struct isl_basic_set *isl_basic_set_positive_orthant(struct isl_dim *dims)
5193 int i;
5194 unsigned nparam;
5195 unsigned dim;
5196 struct isl_basic_set *bset;
5198 if (!dims)
5199 return NULL;
5200 nparam = dims->nparam;
5201 dim = dims->n_out;
5202 bset = isl_basic_set_alloc_dim(dims, 0, 0, dim);
5203 if (!bset)
5204 return NULL;
5205 for (i = 0; i < dim; ++i) {
5206 int k = isl_basic_set_alloc_inequality(bset);
5207 if (k < 0)
5208 goto error;
5209 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
5210 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
5212 return bset;
5213 error:
5214 isl_basic_set_free(bset);
5215 return NULL;
5218 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
5219 enum isl_dim_type type, unsigned first, unsigned n)
5221 int i;
5222 isl_basic_set *nonneg = NULL;
5223 isl_basic_set *neg = NULL;
5225 if (!set)
5226 return NULL;
5227 if (n == 0)
5228 return set;
5230 isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
5232 for (i = 0; i < n; ++i) {
5233 int k;
5235 neg = NULL;
5236 nonneg = isl_basic_set_alloc_dim(isl_set_get_dim(set), 0, 0, 1);
5237 k = isl_basic_set_alloc_inequality(nonneg);
5238 if (k < 0)
5239 goto error;
5240 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
5241 isl_int_set_si(nonneg->ineq[k][pos(set->dim, type) + first + i], 1);
5243 neg = isl_basic_set_alloc_dim(isl_set_get_dim(set), 0, 0, 1);
5244 k = isl_basic_set_alloc_inequality(neg);
5245 if (k < 0)
5246 goto error;
5247 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
5248 isl_int_set_si(neg->ineq[k][0], -1);
5249 isl_int_set_si(neg->ineq[k][pos(set->dim, type) + first + i], -1);
5251 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
5254 return set;
5255 error:
5256 isl_basic_set_free(nonneg);
5257 isl_basic_set_free(neg);
5258 isl_set_free(set);
5259 return NULL;
5262 int isl_set_is_equal(struct isl_set *set1, struct isl_set *set2)
5264 return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
5267 int isl_basic_map_is_subset(
5268 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
5270 int is_subset;
5271 struct isl_map *map1;
5272 struct isl_map *map2;
5274 if (!bmap1 || !bmap2)
5275 return -1;
5277 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
5278 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
5280 is_subset = isl_map_is_subset(map1, map2);
5282 isl_map_free(map1);
5283 isl_map_free(map2);
5285 return is_subset;
5288 int isl_basic_map_is_equal(
5289 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
5291 int is_subset;
5293 if (!bmap1 || !bmap2)
5294 return -1;
5295 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
5296 if (is_subset != 1)
5297 return is_subset;
5298 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
5299 return is_subset;
5302 int isl_basic_set_is_equal(
5303 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
5305 return isl_basic_map_is_equal(
5306 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
5309 int isl_map_is_empty(struct isl_map *map)
5311 int i;
5312 int is_empty;
5314 if (!map)
5315 return -1;
5316 for (i = 0; i < map->n; ++i) {
5317 is_empty = isl_basic_map_is_empty(map->p[i]);
5318 if (is_empty < 0)
5319 return -1;
5320 if (!is_empty)
5321 return 0;
5323 return 1;
5326 int isl_map_fast_is_empty(struct isl_map *map)
5328 return map ? map->n == 0 : -1;
5331 int isl_set_fast_is_empty(struct isl_set *set)
5333 return set ? set->n == 0 : -1;
5336 int isl_set_is_empty(struct isl_set *set)
5338 return isl_map_is_empty((struct isl_map *)set);
5341 int isl_map_has_equal_dim(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
5343 if (!map1 || !map2)
5344 return -1;
5346 return isl_dim_equal(map1->dim, map2->dim);
5349 int isl_set_has_equal_dim(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
5351 if (!set1 || !set2)
5352 return -1;
5354 return isl_dim_equal(set1->dim, set2->dim);
5357 int isl_map_is_equal(struct isl_map *map1, struct isl_map *map2)
5359 int is_subset;
5361 if (!map1 || !map2)
5362 return -1;
5363 is_subset = isl_map_is_subset(map1, map2);
5364 if (is_subset != 1)
5365 return is_subset;
5366 is_subset = isl_map_is_subset(map2, map1);
5367 return is_subset;
5370 int isl_basic_map_is_strict_subset(
5371 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
5373 int is_subset;
5375 if (!bmap1 || !bmap2)
5376 return -1;
5377 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
5378 if (is_subset != 1)
5379 return is_subset;
5380 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
5381 if (is_subset == -1)
5382 return is_subset;
5383 return !is_subset;
5386 int isl_map_is_strict_subset(struct isl_map *map1, struct isl_map *map2)
5388 int is_subset;
5390 if (!map1 || !map2)
5391 return -1;
5392 is_subset = isl_map_is_subset(map1, map2);
5393 if (is_subset != 1)
5394 return is_subset;
5395 is_subset = isl_map_is_subset(map2, map1);
5396 if (is_subset == -1)
5397 return is_subset;
5398 return !is_subset;
5401 int isl_set_is_strict_subset(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
5403 return isl_map_is_strict_subset((isl_map *)set1, (isl_map *)set2);
5406 int isl_basic_map_is_universe(struct isl_basic_map *bmap)
5408 if (!bmap)
5409 return -1;
5410 return bmap->n_eq == 0 && bmap->n_ineq == 0;
5413 int isl_basic_set_is_universe(struct isl_basic_set *bset)
5415 if (!bset)
5416 return -1;
5417 return bset->n_eq == 0 && bset->n_ineq == 0;
5420 int isl_map_fast_is_universe(__isl_keep isl_map *map)
5422 int i;
5424 if (!map)
5425 return -1;
5427 for (i = 0; i < map->n; ++i) {
5428 int r = isl_basic_map_is_universe(map->p[i]);
5429 if (r < 0 || r)
5430 return r;
5433 return 0;
5436 int isl_set_fast_is_universe(__isl_keep isl_set *set)
5438 return isl_map_fast_is_universe((isl_map *) set);
5441 int isl_basic_map_is_empty(struct isl_basic_map *bmap)
5443 struct isl_basic_set *bset = NULL;
5444 struct isl_vec *sample = NULL;
5445 int empty;
5446 unsigned total;
5448 if (!bmap)
5449 return -1;
5451 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
5452 return 1;
5454 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
5455 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
5456 copy = isl_basic_map_remove_redundancies(copy);
5457 empty = ISL_F_ISSET(copy, ISL_BASIC_MAP_EMPTY);
5458 isl_basic_map_free(copy);
5459 return empty;
5462 total = 1 + isl_basic_map_total_dim(bmap);
5463 if (bmap->sample && bmap->sample->size == total) {
5464 int contains = isl_basic_map_contains(bmap, bmap->sample);
5465 if (contains < 0)
5466 return -1;
5467 if (contains)
5468 return 0;
5470 isl_vec_free(bmap->sample);
5471 bmap->sample = NULL;
5472 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
5473 if (!bset)
5474 return -1;
5475 sample = isl_basic_set_sample_vec(bset);
5476 if (!sample)
5477 return -1;
5478 empty = sample->size == 0;
5479 isl_vec_free(bmap->sample);
5480 bmap->sample = sample;
5481 if (empty)
5482 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
5484 return empty;
5487 int isl_basic_map_fast_is_empty(struct isl_basic_map *bmap)
5489 if (!bmap)
5490 return -1;
5491 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
5494 int isl_basic_set_fast_is_empty(struct isl_basic_set *bset)
5496 if (!bset)
5497 return -1;
5498 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
5501 int isl_basic_set_is_empty(struct isl_basic_set *bset)
5503 return isl_basic_map_is_empty((struct isl_basic_map *)bset);
5506 struct isl_map *isl_basic_map_union(
5507 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
5509 struct isl_map *map;
5510 if (!bmap1 || !bmap2)
5511 return NULL;
5513 isl_assert(bmap1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim), goto error);
5515 map = isl_map_alloc_dim(isl_dim_copy(bmap1->dim), 2, 0);
5516 if (!map)
5517 goto error;
5518 map = isl_map_add_basic_map(map, bmap1);
5519 map = isl_map_add_basic_map(map, bmap2);
5520 return map;
5521 error:
5522 isl_basic_map_free(bmap1);
5523 isl_basic_map_free(bmap2);
5524 return NULL;
5527 struct isl_set *isl_basic_set_union(
5528 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
5530 return (struct isl_set *)isl_basic_map_union(
5531 (struct isl_basic_map *)bset1,
5532 (struct isl_basic_map *)bset2);
5535 /* Order divs such that any div only depends on previous divs */
5536 struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
5538 int i;
5539 unsigned off;
5541 if (!bmap)
5542 return NULL;
5544 off = isl_dim_total(bmap->dim);
5546 for (i = 0; i < bmap->n_div; ++i) {
5547 int pos;
5548 if (isl_int_is_zero(bmap->div[i][0]))
5549 continue;
5550 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
5551 bmap->n_div-i);
5552 if (pos == -1)
5553 continue;
5554 isl_basic_map_swap_div(bmap, i, i + pos);
5555 --i;
5557 return bmap;
5560 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
5562 return (struct isl_basic_set *)
5563 isl_basic_map_order_divs((struct isl_basic_map *)bset);
5566 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
5568 int i;
5570 if (!map)
5571 return 0;
5573 for (i = 0; i < map->n; ++i) {
5574 map->p[i] = isl_basic_map_order_divs(map->p[i]);
5575 if (!map->p[i])
5576 goto error;
5579 return map;
5580 error:
5581 isl_map_free(map);
5582 return NULL;
5585 /* Look for a div in dst that corresponds to the div "div" in src.
5586 * The divs before "div" in src and dst are assumed to be the same.
5588 * Returns -1 if no corresponding div was found and the position
5589 * of the corresponding div in dst otherwise.
5591 static int find_div(struct isl_basic_map *dst,
5592 struct isl_basic_map *src, unsigned div)
5594 int i;
5596 unsigned total = isl_dim_total(src->dim);
5598 isl_assert(dst->ctx, div <= dst->n_div, return -1);
5599 for (i = div; i < dst->n_div; ++i)
5600 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
5601 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
5602 dst->n_div - div) == -1)
5603 return i;
5604 return -1;
5607 struct isl_basic_map *isl_basic_map_align_divs(
5608 struct isl_basic_map *dst, struct isl_basic_map *src)
5610 int i;
5611 unsigned total = isl_dim_total(src->dim);
5613 if (!dst || !src)
5614 goto error;
5616 if (src->n_div == 0)
5617 return dst;
5619 for (i = 0; i < src->n_div; ++i)
5620 isl_assert(src->ctx, !isl_int_is_zero(src->div[i][0]), goto error);
5622 src = isl_basic_map_order_divs(src);
5623 dst = isl_basic_map_cow(dst);
5624 dst = isl_basic_map_extend_dim(dst, isl_dim_copy(dst->dim),
5625 src->n_div, 0, 2 * src->n_div);
5626 if (!dst)
5627 return NULL;
5628 for (i = 0; i < src->n_div; ++i) {
5629 int j = find_div(dst, src, i);
5630 if (j < 0) {
5631 j = isl_basic_map_alloc_div(dst);
5632 if (j < 0)
5633 goto error;
5634 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
5635 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
5636 if (isl_basic_map_add_div_constraints(dst, j) < 0)
5637 goto error;
5639 if (j != i)
5640 isl_basic_map_swap_div(dst, i, j);
5642 return dst;
5643 error:
5644 isl_basic_map_free(dst);
5645 return NULL;
5648 struct isl_basic_set *isl_basic_set_align_divs(
5649 struct isl_basic_set *dst, struct isl_basic_set *src)
5651 return (struct isl_basic_set *)isl_basic_map_align_divs(
5652 (struct isl_basic_map *)dst, (struct isl_basic_map *)src);
5655 struct isl_map *isl_map_align_divs(struct isl_map *map)
5657 int i;
5659 if (!map)
5660 return NULL;
5661 if (map->n == 0)
5662 return map;
5663 map = isl_map_compute_divs(map);
5664 map = isl_map_cow(map);
5665 if (!map)
5666 return NULL;
5668 for (i = 1; i < map->n; ++i)
5669 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
5670 for (i = 1; i < map->n; ++i)
5671 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
5673 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5674 return map;
5677 struct isl_set *isl_set_align_divs(struct isl_set *set)
5679 return (struct isl_set *)isl_map_align_divs((struct isl_map *)set);
5682 struct isl_set *isl_set_apply(struct isl_set *set, struct isl_map *map)
5684 if (!set || !map)
5685 goto error;
5686 isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
5687 map = isl_map_intersect_domain(map, set);
5688 set = isl_map_range(map);
5689 return set;
5690 error:
5691 isl_set_free(set);
5692 isl_map_free(map);
5693 return NULL;
5696 /* There is no need to cow as removing empty parts doesn't change
5697 * the meaning of the set.
5699 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
5701 int i;
5703 if (!map)
5704 return NULL;
5706 for (i = map->n-1; i >= 0; --i) {
5707 if (!ISL_F_ISSET(map->p[i], ISL_BASIC_MAP_EMPTY))
5708 continue;
5709 isl_basic_map_free(map->p[i]);
5710 if (i != map->n-1) {
5711 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5712 map->p[i] = map->p[map->n-1];
5714 map->n--;
5717 return map;
5720 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
5722 return (struct isl_set *)
5723 isl_map_remove_empty_parts((struct isl_map *)set);
5726 struct isl_basic_map *isl_map_copy_basic_map(struct isl_map *map)
5728 struct isl_basic_map *bmap;
5729 if (!map || map->n == 0)
5730 return NULL;
5731 bmap = map->p[map->n-1];
5732 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
5733 return isl_basic_map_copy(bmap);
5736 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
5738 return (struct isl_basic_set *)
5739 isl_map_copy_basic_map((struct isl_map *)set);
5742 __isl_give isl_map *isl_map_drop_basic_map(__isl_take isl_map *map,
5743 __isl_keep isl_basic_map *bmap)
5745 int i;
5747 if (!map || !bmap)
5748 goto error;
5749 for (i = map->n-1; i >= 0; --i) {
5750 if (map->p[i] != bmap)
5751 continue;
5752 map = isl_map_cow(map);
5753 if (!map)
5754 goto error;
5755 isl_basic_map_free(map->p[i]);
5756 if (i != map->n-1) {
5757 ISL_F_CLR(map, ISL_SET_NORMALIZED);
5758 map->p[i] = map->p[map->n-1];
5760 map->n--;
5761 return map;
5763 return map;
5764 error:
5765 isl_map_free(map);
5766 return NULL;
5769 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
5770 struct isl_basic_set *bset)
5772 return (struct isl_set *)isl_map_drop_basic_map((struct isl_map *)set,
5773 (struct isl_basic_map *)bset);
5776 /* Given two basic sets bset1 and bset2, compute the maximal difference
5777 * between the values of dimension pos in bset1 and those in bset2
5778 * for any common value of the parameters and dimensions preceding pos.
5780 static enum isl_lp_result basic_set_maximal_difference_at(
5781 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
5782 int pos, isl_int *opt)
5784 struct isl_dim *dims;
5785 struct isl_basic_map *bmap1 = NULL;
5786 struct isl_basic_map *bmap2 = NULL;
5787 struct isl_ctx *ctx;
5788 struct isl_vec *obj;
5789 unsigned total;
5790 unsigned nparam;
5791 unsigned dim1, dim2;
5792 enum isl_lp_result res;
5794 if (!bset1 || !bset2)
5795 return isl_lp_error;
5797 nparam = isl_basic_set_n_param(bset1);
5798 dim1 = isl_basic_set_n_dim(bset1);
5799 dim2 = isl_basic_set_n_dim(bset2);
5800 dims = isl_dim_alloc(bset1->ctx, nparam, pos, dim1 - pos);
5801 bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1), dims);
5802 dims = isl_dim_alloc(bset2->ctx, nparam, pos, dim2 - pos);
5803 bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims);
5804 if (!bmap1 || !bmap2)
5805 goto error;
5806 bmap1 = isl_basic_map_cow(bmap1);
5807 bmap1 = isl_basic_map_extend(bmap1, nparam,
5808 pos, (dim1 - pos) + (dim2 - pos),
5809 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
5810 bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
5811 if (!bmap1)
5812 goto error;
5813 total = isl_basic_map_total_dim(bmap1);
5814 ctx = bmap1->ctx;
5815 obj = isl_vec_alloc(ctx, 1 + total);
5816 isl_seq_clr(obj->block.data, 1 + total);
5817 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
5818 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
5819 if (!obj)
5820 goto error;
5821 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
5822 opt, NULL, NULL);
5823 isl_basic_map_free(bmap1);
5824 isl_vec_free(obj);
5825 return res;
5826 error:
5827 isl_basic_map_free(bmap1);
5828 isl_basic_map_free(bmap2);
5829 return isl_lp_error;
5832 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
5833 * for any common value of the parameters and dimensions preceding pos
5834 * in both basic sets, the values of dimension pos in bset1 are
5835 * smaller or larger than those in bset2.
5837 * Returns
5838 * 1 if bset1 follows bset2
5839 * -1 if bset1 precedes bset2
5840 * 0 if bset1 and bset2 are incomparable
5841 * -2 if some error occurred.
5843 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
5844 struct isl_basic_set *bset2, int pos)
5846 isl_int opt;
5847 enum isl_lp_result res;
5848 int cmp;
5850 isl_int_init(opt);
5852 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
5854 if (res == isl_lp_empty)
5855 cmp = 0;
5856 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
5857 res == isl_lp_unbounded)
5858 cmp = 1;
5859 else if (res == isl_lp_ok && isl_int_is_neg(opt))
5860 cmp = -1;
5861 else
5862 cmp = -2;
5864 isl_int_clear(opt);
5865 return cmp;
5868 /* Given two basic sets bset1 and bset2, check whether
5869 * for any common value of the parameters and dimensions preceding pos
5870 * there is a value of dimension pos in bset1 that is larger
5871 * than a value of the same dimension in bset2.
5873 * Return
5874 * 1 if there exists such a pair
5875 * 0 if there is no such pair, but there is a pair of equal values
5876 * -1 otherwise
5877 * -2 if some error occurred.
5879 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
5880 __isl_keep isl_basic_set *bset2, int pos)
5882 isl_int opt;
5883 enum isl_lp_result res;
5884 int cmp;
5886 isl_int_init(opt);
5888 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
5890 if (res == isl_lp_empty)
5891 cmp = -1;
5892 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
5893 res == isl_lp_unbounded)
5894 cmp = 1;
5895 else if (res == isl_lp_ok && isl_int_is_neg(opt))
5896 cmp = -1;
5897 else if (res == isl_lp_ok)
5898 cmp = 0;
5899 else
5900 cmp = -2;
5902 isl_int_clear(opt);
5903 return cmp;
5906 /* Given two sets set1 and set2, check whether
5907 * for any common value of the parameters and dimensions preceding pos
5908 * there is a value of dimension pos in set1 that is larger
5909 * than a value of the same dimension in set2.
5911 * Return
5912 * 1 if there exists such a pair
5913 * 0 if there is no such pair, but there is a pair of equal values
5914 * -1 otherwise
5915 * -2 if some error occurred.
5917 int isl_set_follows_at(__isl_keep isl_set *set1,
5918 __isl_keep isl_set *set2, int pos)
5920 int i, j;
5921 int follows = -1;
5923 if (!set1 || !set2)
5924 return -2;
5926 for (i = 0; i < set1->n; ++i)
5927 for (j = 0; j < set2->n; ++j) {
5928 int f;
5929 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
5930 if (f == 1 || f == -2)
5931 return f;
5932 if (f > follows)
5933 follows = f;
5936 return follows;
5939 static int isl_basic_map_fast_has_fixed_var(struct isl_basic_map *bmap,
5940 unsigned pos, isl_int *val)
5942 int i;
5943 int d;
5944 unsigned total;
5946 if (!bmap)
5947 return -1;
5948 total = isl_basic_map_total_dim(bmap);
5949 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
5950 for (; d+1 > pos; --d)
5951 if (!isl_int_is_zero(bmap->eq[i][1+d]))
5952 break;
5953 if (d != pos)
5954 continue;
5955 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
5956 return 0;
5957 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
5958 return 0;
5959 if (!isl_int_is_one(bmap->eq[i][1+d]))
5960 return 0;
5961 if (val)
5962 isl_int_neg(*val, bmap->eq[i][0]);
5963 return 1;
5965 return 0;
5968 static int isl_map_fast_has_fixed_var(struct isl_map *map,
5969 unsigned pos, isl_int *val)
5971 int i;
5972 isl_int v;
5973 isl_int tmp;
5974 int fixed;
5976 if (!map)
5977 return -1;
5978 if (map->n == 0)
5979 return 0;
5980 if (map->n == 1)
5981 return isl_basic_map_fast_has_fixed_var(map->p[0], pos, val);
5982 isl_int_init(v);
5983 isl_int_init(tmp);
5984 fixed = isl_basic_map_fast_has_fixed_var(map->p[0], pos, &v);
5985 for (i = 1; fixed == 1 && i < map->n; ++i) {
5986 fixed = isl_basic_map_fast_has_fixed_var(map->p[i], pos, &tmp);
5987 if (fixed == 1 && isl_int_ne(tmp, v))
5988 fixed = 0;
5990 if (val)
5991 isl_int_set(*val, v);
5992 isl_int_clear(tmp);
5993 isl_int_clear(v);
5994 return fixed;
5997 static int isl_basic_set_fast_has_fixed_var(struct isl_basic_set *bset,
5998 unsigned pos, isl_int *val)
6000 return isl_basic_map_fast_has_fixed_var((struct isl_basic_map *)bset,
6001 pos, val);
6004 static int isl_set_fast_has_fixed_var(struct isl_set *set, unsigned pos,
6005 isl_int *val)
6007 return isl_map_fast_has_fixed_var((struct isl_map *)set, pos, val);
6010 int isl_basic_map_fast_is_fixed(struct isl_basic_map *bmap,
6011 enum isl_dim_type type, unsigned pos, isl_int *val)
6013 if (pos >= isl_basic_map_dim(bmap, type))
6014 return -1;
6015 return isl_basic_map_fast_has_fixed_var(bmap,
6016 isl_basic_map_offset(bmap, type) - 1 + pos, val);
6019 int isl_map_fast_is_fixed(struct isl_map *map,
6020 enum isl_dim_type type, unsigned pos, isl_int *val)
6022 if (pos >= isl_map_dim(map, type))
6023 return -1;
6024 return isl_map_fast_has_fixed_var(map,
6025 map_offset(map, type) - 1 + pos, val);
6028 /* Check if dimension dim has fixed value and if so and if val is not NULL,
6029 * then return this fixed value in *val.
6031 int isl_basic_set_fast_dim_is_fixed(struct isl_basic_set *bset, unsigned dim,
6032 isl_int *val)
6034 return isl_basic_set_fast_has_fixed_var(bset,
6035 isl_basic_set_n_param(bset) + dim, val);
6038 /* Check if dimension dim has fixed value and if so and if val is not NULL,
6039 * then return this fixed value in *val.
6041 int isl_set_fast_dim_is_fixed(struct isl_set *set, unsigned dim, isl_int *val)
6043 return isl_set_fast_has_fixed_var(set, isl_set_n_param(set) + dim, val);
6046 /* Check if input variable in has fixed value and if so and if val is not NULL,
6047 * then return this fixed value in *val.
6049 int isl_map_fast_input_is_fixed(struct isl_map *map, unsigned in, isl_int *val)
6051 return isl_map_fast_has_fixed_var(map, isl_map_n_param(map) + in, val);
6054 /* Check if dimension dim has an (obvious) fixed lower bound and if so
6055 * and if val is not NULL, then return this lower bound in *val.
6057 int isl_basic_set_fast_dim_has_fixed_lower_bound(struct isl_basic_set *bset,
6058 unsigned dim, isl_int *val)
6060 int i, i_eq = -1, i_ineq = -1;
6061 isl_int *c;
6062 unsigned total;
6063 unsigned nparam;
6065 if (!bset)
6066 return -1;
6067 total = isl_basic_set_total_dim(bset);
6068 nparam = isl_basic_set_n_param(bset);
6069 for (i = 0; i < bset->n_eq; ++i) {
6070 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
6071 continue;
6072 if (i_eq != -1)
6073 return 0;
6074 i_eq = i;
6076 for (i = 0; i < bset->n_ineq; ++i) {
6077 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
6078 continue;
6079 if (i_eq != -1 || i_ineq != -1)
6080 return 0;
6081 i_ineq = i;
6083 if (i_eq == -1 && i_ineq == -1)
6084 return 0;
6085 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
6086 /* The coefficient should always be one due to normalization. */
6087 if (!isl_int_is_one(c[1+nparam+dim]))
6088 return 0;
6089 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
6090 return 0;
6091 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
6092 total - nparam - dim - 1) != -1)
6093 return 0;
6094 if (val)
6095 isl_int_neg(*val, c[0]);
6096 return 1;
6099 int isl_set_fast_dim_has_fixed_lower_bound(struct isl_set *set,
6100 unsigned dim, isl_int *val)
6102 int i;
6103 isl_int v;
6104 isl_int tmp;
6105 int fixed;
6107 if (!set)
6108 return -1;
6109 if (set->n == 0)
6110 return 0;
6111 if (set->n == 1)
6112 return isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[0],
6113 dim, val);
6114 isl_int_init(v);
6115 isl_int_init(tmp);
6116 fixed = isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[0],
6117 dim, &v);
6118 for (i = 1; fixed == 1 && i < set->n; ++i) {
6119 fixed = isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[i],
6120 dim, &tmp);
6121 if (fixed == 1 && isl_int_ne(tmp, v))
6122 fixed = 0;
6124 if (val)
6125 isl_int_set(*val, v);
6126 isl_int_clear(tmp);
6127 isl_int_clear(v);
6128 return fixed;
6131 struct constraint {
6132 unsigned size;
6133 isl_int *c;
6136 static int qsort_constraint_cmp(const void *p1, const void *p2)
6138 const struct constraint *c1 = (const struct constraint *)p1;
6139 const struct constraint *c2 = (const struct constraint *)p2;
6140 unsigned size = isl_min(c1->size, c2->size);
6141 return isl_seq_cmp(c1->c, c2->c, size);
6144 static struct isl_basic_map *isl_basic_map_sort_constraints(
6145 struct isl_basic_map *bmap)
6147 int i;
6148 struct constraint *c;
6149 unsigned total;
6151 if (!bmap)
6152 return NULL;
6153 total = isl_basic_map_total_dim(bmap);
6154 c = isl_alloc_array(bmap->ctx, struct constraint, bmap->n_ineq);
6155 if (!c)
6156 goto error;
6157 for (i = 0; i < bmap->n_ineq; ++i) {
6158 c[i].size = total;
6159 c[i].c = bmap->ineq[i];
6161 qsort(c, bmap->n_ineq, sizeof(struct constraint), qsort_constraint_cmp);
6162 for (i = 0; i < bmap->n_ineq; ++i)
6163 bmap->ineq[i] = c[i].c;
6164 free(c);
6165 return bmap;
6166 error:
6167 isl_basic_map_free(bmap);
6168 return NULL;
6171 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
6172 __isl_take isl_basic_set *bset)
6174 return (struct isl_basic_set *)isl_basic_map_sort_constraints(
6175 (struct isl_basic_map *)bset);
6178 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
6180 if (!bmap)
6181 return NULL;
6182 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
6183 return bmap;
6184 bmap = isl_basic_map_remove_redundancies(bmap);
6185 bmap = isl_basic_map_sort_constraints(bmap);
6186 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
6187 return bmap;
6190 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
6192 return (struct isl_basic_set *)isl_basic_map_normalize(
6193 (struct isl_basic_map *)bset);
6196 int isl_basic_map_fast_cmp(const __isl_keep isl_basic_map *bmap1,
6197 const __isl_keep isl_basic_map *bmap2)
6199 int i, cmp;
6200 unsigned total;
6202 if (bmap1 == bmap2)
6203 return 0;
6204 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
6205 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
6206 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
6207 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
6208 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
6209 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
6210 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
6211 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
6212 return 0;
6213 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
6214 return 1;
6215 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
6216 return -1;
6217 if (bmap1->n_eq != bmap2->n_eq)
6218 return bmap1->n_eq - bmap2->n_eq;
6219 if (bmap1->n_ineq != bmap2->n_ineq)
6220 return bmap1->n_ineq - bmap2->n_ineq;
6221 if (bmap1->n_div != bmap2->n_div)
6222 return bmap1->n_div - bmap2->n_div;
6223 total = isl_basic_map_total_dim(bmap1);
6224 for (i = 0; i < bmap1->n_eq; ++i) {
6225 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
6226 if (cmp)
6227 return cmp;
6229 for (i = 0; i < bmap1->n_ineq; ++i) {
6230 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
6231 if (cmp)
6232 return cmp;
6234 for (i = 0; i < bmap1->n_div; ++i) {
6235 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
6236 if (cmp)
6237 return cmp;
6239 return 0;
6242 int isl_basic_map_fast_is_equal(__isl_keep isl_basic_map *bmap1,
6243 __isl_keep isl_basic_map *bmap2)
6245 return isl_basic_map_fast_cmp(bmap1, bmap2) == 0;
6248 int isl_basic_set_fast_is_equal(__isl_keep isl_basic_set *bset1,
6249 __isl_keep isl_basic_set *bset2)
6251 return isl_basic_map_fast_is_equal((isl_basic_map *)bset1,
6252 (isl_basic_map *)bset2);
6255 static int qsort_bmap_cmp(const void *p1, const void *p2)
6257 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
6258 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
6260 return isl_basic_map_fast_cmp(bmap1, bmap2);
6263 /* We normalize in place, but if anything goes wrong we need
6264 * to return NULL, so we need to make sure we don't change the
6265 * meaning of any possible other copies of map.
6267 struct isl_map *isl_map_normalize(struct isl_map *map)
6269 int i, j;
6270 struct isl_basic_map *bmap;
6272 if (!map)
6273 return NULL;
6274 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
6275 return map;
6276 for (i = 0; i < map->n; ++i) {
6277 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
6278 if (!bmap)
6279 goto error;
6280 isl_basic_map_free(map->p[i]);
6281 map->p[i] = bmap;
6283 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
6284 ISL_F_SET(map, ISL_MAP_NORMALIZED);
6285 map = isl_map_remove_empty_parts(map);
6286 if (!map)
6287 return NULL;
6288 for (i = map->n - 1; i >= 1; --i) {
6289 if (!isl_basic_map_fast_is_equal(map->p[i-1], map->p[i]))
6290 continue;
6291 isl_basic_map_free(map->p[i-1]);
6292 for (j = i; j < map->n; ++j)
6293 map->p[j-1] = map->p[j];
6294 map->n--;
6296 return map;
6297 error:
6298 isl_map_free(map);
6299 return NULL;
6303 struct isl_set *isl_set_normalize(struct isl_set *set)
6305 return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
6308 int isl_map_fast_is_equal(struct isl_map *map1, struct isl_map *map2)
6310 int i;
6311 int equal;
6313 if (!map1 || !map2)
6314 return -1;
6316 if (map1 == map2)
6317 return 1;
6318 if (!isl_dim_equal(map1->dim, map2->dim))
6319 return 0;
6321 map1 = isl_map_copy(map1);
6322 map2 = isl_map_copy(map2);
6323 map1 = isl_map_normalize(map1);
6324 map2 = isl_map_normalize(map2);
6325 if (!map1 || !map2)
6326 goto error;
6327 equal = map1->n == map2->n;
6328 for (i = 0; equal && i < map1->n; ++i) {
6329 equal = isl_basic_map_fast_is_equal(map1->p[i], map2->p[i]);
6330 if (equal < 0)
6331 goto error;
6333 isl_map_free(map1);
6334 isl_map_free(map2);
6335 return equal;
6336 error:
6337 isl_map_free(map1);
6338 isl_map_free(map2);
6339 return -1;
6342 int isl_set_fast_is_equal(struct isl_set *set1, struct isl_set *set2)
6344 return isl_map_fast_is_equal((struct isl_map *)set1,
6345 (struct isl_map *)set2);
6348 /* Return an interval that ranges from min to max (inclusive)
6350 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
6351 isl_int min, isl_int max)
6353 int k;
6354 struct isl_basic_set *bset = NULL;
6356 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
6357 if (!bset)
6358 goto error;
6360 k = isl_basic_set_alloc_inequality(bset);
6361 if (k < 0)
6362 goto error;
6363 isl_int_set_si(bset->ineq[k][1], 1);
6364 isl_int_neg(bset->ineq[k][0], min);
6366 k = isl_basic_set_alloc_inequality(bset);
6367 if (k < 0)
6368 goto error;
6369 isl_int_set_si(bset->ineq[k][1], -1);
6370 isl_int_set(bset->ineq[k][0], max);
6372 return bset;
6373 error:
6374 isl_basic_set_free(bset);
6375 return NULL;
6378 /* Return the Cartesian product of the basic sets in list (in the given order).
6380 struct isl_basic_set *isl_basic_set_product(struct isl_basic_set_list *list)
6382 int i;
6383 unsigned dim;
6384 unsigned nparam;
6385 unsigned extra;
6386 unsigned n_eq;
6387 unsigned n_ineq;
6388 struct isl_basic_set *product = NULL;
6390 if (!list)
6391 goto error;
6392 isl_assert(list->ctx, list->n > 0, goto error);
6393 isl_assert(list->ctx, list->p[0], goto error);
6394 nparam = isl_basic_set_n_param(list->p[0]);
6395 dim = isl_basic_set_n_dim(list->p[0]);
6396 extra = list->p[0]->n_div;
6397 n_eq = list->p[0]->n_eq;
6398 n_ineq = list->p[0]->n_ineq;
6399 for (i = 1; i < list->n; ++i) {
6400 isl_assert(list->ctx, list->p[i], goto error);
6401 isl_assert(list->ctx,
6402 nparam == isl_basic_set_n_param(list->p[i]), goto error);
6403 dim += isl_basic_set_n_dim(list->p[i]);
6404 extra += list->p[i]->n_div;
6405 n_eq += list->p[i]->n_eq;
6406 n_ineq += list->p[i]->n_ineq;
6408 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
6409 n_eq, n_ineq);
6410 if (!product)
6411 goto error;
6412 dim = 0;
6413 for (i = 0; i < list->n; ++i) {
6414 isl_basic_set_add_constraints(product,
6415 isl_basic_set_copy(list->p[i]), dim);
6416 dim += isl_basic_set_n_dim(list->p[i]);
6418 isl_basic_set_list_free(list);
6419 return product;
6420 error:
6421 isl_basic_set_free(product);
6422 isl_basic_set_list_free(list);
6423 return NULL;
6426 struct isl_basic_map *isl_basic_map_product(
6427 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6429 struct isl_dim *dim_result = NULL;
6430 struct isl_basic_map *bmap;
6431 unsigned in1, in2, out1, out2, nparam, total, pos;
6432 struct isl_dim_map *dim_map1, *dim_map2;
6434 if (!bmap1 || !bmap2)
6435 goto error;
6437 isl_assert(bmap1->ctx, isl_dim_match(bmap1->dim, isl_dim_param,
6438 bmap2->dim, isl_dim_param), goto error);
6439 dim_result = isl_dim_product(isl_dim_copy(bmap1->dim),
6440 isl_dim_copy(bmap2->dim));
6442 in1 = isl_basic_map_n_in(bmap1);
6443 in2 = isl_basic_map_n_in(bmap2);
6444 out1 = isl_basic_map_n_out(bmap1);
6445 out2 = isl_basic_map_n_out(bmap2);
6446 nparam = isl_basic_map_n_param(bmap1);
6448 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
6449 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
6450 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
6451 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
6452 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
6453 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
6454 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
6455 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
6456 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
6457 isl_dim_map_div(dim_map1, bmap1, pos += out2);
6458 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
6460 bmap = isl_basic_map_alloc_dim(dim_result,
6461 bmap1->n_div + bmap2->n_div,
6462 bmap1->n_eq + bmap2->n_eq,
6463 bmap1->n_ineq + bmap2->n_ineq);
6464 bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
6465 bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
6466 bmap = isl_basic_map_simplify(bmap);
6467 return isl_basic_map_finalize(bmap);
6468 error:
6469 isl_basic_map_free(bmap1);
6470 isl_basic_map_free(bmap2);
6471 return NULL;
6474 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
6476 struct isl_map *isl_map_product(struct isl_map *map1, struct isl_map *map2)
6478 unsigned flags = 0;
6479 struct isl_map *result;
6480 int i, j;
6482 if (!map1 || !map2)
6483 goto error;
6485 isl_assert(map1->ctx, isl_dim_match(map1->dim, isl_dim_param,
6486 map2->dim, isl_dim_param), goto error);
6488 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
6489 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
6490 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
6492 result = isl_map_alloc_dim(isl_dim_product(isl_dim_copy(map1->dim),
6493 isl_dim_copy(map2->dim)),
6494 map1->n * map2->n, flags);
6495 if (!result)
6496 goto error;
6497 for (i = 0; i < map1->n; ++i)
6498 for (j = 0; j < map2->n; ++j) {
6499 struct isl_basic_map *part;
6500 part = isl_basic_map_product(
6501 isl_basic_map_copy(map1->p[i]),
6502 isl_basic_map_copy(map2->p[j]));
6503 if (isl_basic_map_is_empty(part))
6504 isl_basic_map_free(part);
6505 else
6506 result = isl_map_add_basic_map(result, part);
6507 if (!result)
6508 goto error;
6510 isl_map_free(map1);
6511 isl_map_free(map2);
6512 return result;
6513 error:
6514 isl_map_free(map1);
6515 isl_map_free(map2);
6516 return NULL;
6519 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
6521 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
6522 __isl_take isl_map *map2)
6524 isl_map *prod;
6526 prod = isl_map_product(map1, map2);
6527 prod = isl_map_flatten(prod);
6528 return prod;
6531 /* Given two set A and B, construct its Cartesian product A x B.
6533 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
6535 return (struct isl_set *)isl_map_product((struct isl_map *)set1,
6536 (struct isl_map *)set2);
6539 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
6540 __isl_take isl_set *set2)
6542 return (isl_set *)isl_map_flat_product((isl_map *)set1, (isl_map *)set2);
6545 uint32_t isl_basic_set_get_hash(struct isl_basic_set *bset)
6547 int i;
6548 uint32_t hash = isl_hash_init();
6549 unsigned total;
6551 if (!bset)
6552 return 0;
6553 bset = isl_basic_set_copy(bset);
6554 bset = isl_basic_set_normalize(bset);
6555 if (!bset)
6556 return 0;
6557 total = isl_basic_set_total_dim(bset);
6558 isl_hash_byte(hash, bset->n_eq & 0xFF);
6559 for (i = 0; i < bset->n_eq; ++i) {
6560 uint32_t c_hash;
6561 c_hash = isl_seq_get_hash(bset->eq[i], 1 + total);
6562 isl_hash_hash(hash, c_hash);
6564 isl_hash_byte(hash, bset->n_ineq & 0xFF);
6565 for (i = 0; i < bset->n_ineq; ++i) {
6566 uint32_t c_hash;
6567 c_hash = isl_seq_get_hash(bset->ineq[i], 1 + total);
6568 isl_hash_hash(hash, c_hash);
6570 isl_hash_byte(hash, bset->n_div & 0xFF);
6571 for (i = 0; i < bset->n_div; ++i) {
6572 uint32_t c_hash;
6573 if (isl_int_is_zero(bset->div[i][0]))
6574 continue;
6575 isl_hash_byte(hash, i & 0xFF);
6576 c_hash = isl_seq_get_hash(bset->div[i], 1 + 1 + total);
6577 isl_hash_hash(hash, c_hash);
6579 isl_basic_set_free(bset);
6580 return hash;
6583 uint32_t isl_set_get_hash(struct isl_set *set)
6585 int i;
6586 uint32_t hash;
6588 if (!set)
6589 return 0;
6590 set = isl_set_copy(set);
6591 set = isl_set_normalize(set);
6592 if (!set)
6593 return 0;
6595 hash = isl_hash_init();
6596 for (i = 0; i < set->n; ++i) {
6597 uint32_t bset_hash;
6598 bset_hash = isl_basic_set_get_hash(set->p[i]);
6599 isl_hash_hash(hash, bset_hash);
6602 isl_set_free(set);
6604 return hash;
6607 /* Check if the value for dimension dim is completely determined
6608 * by the values of the other parameters and variables.
6609 * That is, check if dimension dim is involved in an equality.
6611 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
6613 int i;
6614 unsigned nparam;
6616 if (!bset)
6617 return -1;
6618 nparam = isl_basic_set_n_param(bset);
6619 for (i = 0; i < bset->n_eq; ++i)
6620 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
6621 return 1;
6622 return 0;
6625 /* Check if the value for dimension dim is completely determined
6626 * by the values of the other parameters and variables.
6627 * That is, check if dimension dim is involved in an equality
6628 * for each of the subsets.
6630 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
6632 int i;
6634 if (!set)
6635 return -1;
6636 for (i = 0; i < set->n; ++i) {
6637 int unique;
6638 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
6639 if (unique != 1)
6640 return unique;
6642 return 1;
6645 int isl_map_foreach_basic_map(__isl_keep isl_map *map,
6646 int (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
6648 int i;
6650 if (!map)
6651 return -1;
6653 for (i = 0; i < map->n; ++i)
6654 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
6655 return -1;
6657 return 0;
6660 int isl_set_foreach_basic_set(__isl_keep isl_set *set,
6661 int (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
6663 int i;
6665 if (!set)
6666 return -1;
6668 for (i = 0; i < set->n; ++i)
6669 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
6670 return -1;
6672 return 0;
6675 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
6677 struct isl_dim *dim;
6679 if (!bset)
6680 return NULL;
6682 if (bset->n_div == 0)
6683 return bset;
6685 bset = isl_basic_set_cow(bset);
6686 if (!bset)
6687 return NULL;
6689 dim = isl_basic_set_get_dim(bset);
6690 dim = isl_dim_add(dim, isl_dim_set, bset->n_div);
6691 if (!dim)
6692 goto error;
6693 isl_dim_free(bset->dim);
6694 bset->dim = dim;
6695 bset->n_div = 0;
6697 bset = isl_basic_set_finalize(bset);
6699 return bset;
6700 error:
6701 isl_basic_set_free(bset);
6702 return NULL;
6705 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
6707 int i;
6708 struct isl_dim *dim;
6709 unsigned n_div;
6711 set = isl_set_align_divs(set);
6713 if (!set)
6714 return NULL;
6715 if (set->n == 0 || set->p[0]->n_div == 0)
6716 return set;
6718 set = isl_set_cow(set);
6719 if (!set)
6720 return NULL;
6722 n_div = set->p[0]->n_div;
6723 dim = isl_set_get_dim(set);
6724 dim = isl_dim_add(dim, isl_dim_set, n_div);
6725 if (!dim)
6726 goto error;
6727 isl_dim_free(set->dim);
6728 set->dim = dim;
6730 for (i = 0; i < set->n; ++i) {
6731 set->p[i] = isl_basic_set_lift(set->p[i]);
6732 if (!set->p[i])
6733 goto error;
6736 return set;
6737 error:
6738 isl_set_free(set);
6739 return NULL;
6742 __isl_give isl_map *isl_set_lifting(__isl_take isl_set *set)
6744 struct isl_dim *dim;
6745 struct isl_basic_map *bmap;
6746 unsigned n_set;
6747 unsigned n_div;
6748 unsigned n_param;
6749 unsigned total;
6750 int i, k, l;
6752 set = isl_set_align_divs(set);
6754 if (!set)
6755 return NULL;
6757 dim = isl_set_get_dim(set);
6758 if (set->n == 0 || set->p[0]->n_div == 0) {
6759 isl_set_free(set);
6760 return isl_map_identity(dim);
6763 n_div = set->p[0]->n_div;
6764 dim = isl_dim_map(dim);
6765 n_param = isl_dim_size(dim, isl_dim_param);
6766 n_set = isl_dim_size(dim, isl_dim_in);
6767 dim = isl_dim_extend(dim, n_param, n_set, n_set + n_div);
6768 bmap = isl_basic_map_alloc_dim(dim, 0, n_set, 2 * n_div);
6769 for (i = 0; i < n_set; ++i)
6770 bmap = var_equal(bmap, i);
6772 total = n_param + n_set + n_set + n_div;
6773 for (i = 0; i < n_div; ++i) {
6774 k = isl_basic_map_alloc_inequality(bmap);
6775 if (k < 0)
6776 goto error;
6777 isl_seq_cpy(bmap->ineq[k], set->p[0]->div[i]+1, 1+n_param);
6778 isl_seq_clr(bmap->ineq[k]+1+n_param, n_set);
6779 isl_seq_cpy(bmap->ineq[k]+1+n_param+n_set,
6780 set->p[0]->div[i]+1+1+n_param, n_set + n_div);
6781 isl_int_neg(bmap->ineq[k][1+n_param+n_set+n_set+i],
6782 set->p[0]->div[i][0]);
6784 l = isl_basic_map_alloc_inequality(bmap);
6785 if (l < 0)
6786 goto error;
6787 isl_seq_neg(bmap->ineq[l], bmap->ineq[k], 1 + total);
6788 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0],
6789 set->p[0]->div[i][0]);
6790 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
6793 isl_set_free(set);
6794 return isl_map_from_basic_map(bmap);
6795 error:
6796 isl_set_free(set);
6797 isl_basic_map_free(bmap);
6798 return NULL;
6801 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
6803 unsigned dim;
6804 int size = 0;
6806 if (!bset)
6807 return -1;
6809 dim = isl_basic_set_total_dim(bset);
6810 size += bset->n_eq * (1 + dim);
6811 size += bset->n_ineq * (1 + dim);
6812 size += bset->n_div * (2 + dim);
6814 return size;
6817 int isl_set_size(__isl_keep isl_set *set)
6819 int i;
6820 int size = 0;
6822 if (!set)
6823 return -1;
6825 for (i = 0; i < set->n; ++i)
6826 size += isl_basic_set_size(set->p[i]);
6828 return size;
6831 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
6832 enum isl_dim_type type, unsigned pos)
6834 int i;
6835 int lower, upper;
6837 if (!bmap)
6838 return -1;
6840 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), return -1);
6842 pos += isl_basic_map_offset(bmap, type);
6844 for (i = 0; i < bmap->n_eq; ++i)
6845 if (!isl_int_is_zero(bmap->eq[i][pos]))
6846 return 1;
6848 lower = upper = 0;
6849 for (i = 0; i < bmap->n_ineq; ++i) {
6850 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
6851 if (sgn > 0)
6852 lower = 1;
6853 if (sgn < 0)
6854 upper = 1;
6857 return lower && upper;
6860 int isl_map_dim_is_bounded(__isl_keep isl_map *map,
6861 enum isl_dim_type type, unsigned pos)
6863 int i;
6865 if (!map)
6866 return -1;
6868 for (i = 0; i < map->n; ++i) {
6869 int bounded;
6870 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
6871 if (bounded < 0 || !bounded)
6872 return bounded;
6875 return 1;
6878 /* Return 1 if the specified dim is involved in both an upper bound
6879 * and a lower bound.
6881 int isl_set_dim_is_bounded(__isl_keep isl_set *set,
6882 enum isl_dim_type type, unsigned pos)
6884 return isl_map_dim_is_bounded((isl_map *)set, type, pos);
6887 /* For each of the "n" variables starting at "first", determine
6888 * the sign of the variable and put the results in the first "n"
6889 * elements of the array "signs".
6890 * Sign
6891 * 1 means that the variable is non-negative
6892 * -1 means that the variable is non-positive
6893 * 0 means the variable attains both positive and negative values.
6895 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
6896 unsigned first, unsigned n, int *signs)
6898 isl_vec *bound = NULL;
6899 struct isl_tab *tab = NULL;
6900 struct isl_tab_undo *snap;
6901 int i;
6903 if (!bset || !signs)
6904 return -1;
6906 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
6907 tab = isl_tab_from_basic_set(bset);
6908 if (!bound || !tab)
6909 goto error;
6911 isl_seq_clr(bound->el, bound->size);
6912 isl_int_set_si(bound->el[0], -1);
6914 snap = isl_tab_snap(tab);
6915 for (i = 0; i < n; ++i) {
6916 int empty;
6918 isl_int_set_si(bound->el[1 + first + i], -1);
6919 if (isl_tab_add_ineq(tab, bound->el) < 0)
6920 goto error;
6921 empty = tab->empty;
6922 isl_int_set_si(bound->el[1 + first + i], 0);
6923 if (isl_tab_rollback(tab, snap) < 0)
6924 goto error;
6926 if (empty) {
6927 signs[i] = 1;
6928 continue;
6931 isl_int_set_si(bound->el[1 + first + i], 1);
6932 if (isl_tab_add_ineq(tab, bound->el) < 0)
6933 goto error;
6934 empty = tab->empty;
6935 isl_int_set_si(bound->el[1 + first + i], 0);
6936 if (isl_tab_rollback(tab, snap) < 0)
6937 goto error;
6939 signs[i] = empty ? -1 : 0;
6942 isl_tab_free(tab);
6943 isl_vec_free(bound);
6944 return 0;
6945 error:
6946 isl_tab_free(tab);
6947 isl_vec_free(bound);
6948 return -1;
6951 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
6952 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
6954 if (!bset || !signs)
6955 return -1;
6956 isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
6957 return -1);
6959 first += pos(bset->dim, type) - 1;
6960 return isl_basic_set_vars_get_sign(bset, first, n, signs);
6963 /* Check if the given map is single-valued.
6964 * We simply compute
6966 * M \circ M^-1
6968 * and check if the result is a subset of the identity mapping.
6970 int isl_map_is_single_valued(__isl_keep isl_map *map)
6972 isl_map *test;
6973 isl_map *id;
6974 int sv;
6976 test = isl_map_reverse(isl_map_copy(map));
6977 test = isl_map_apply_range(test, isl_map_copy(map));
6979 id = isl_map_identity(isl_dim_range(isl_map_get_dim(map)));
6981 sv = isl_map_is_subset(test, id);
6983 isl_map_free(test);
6984 isl_map_free(id);
6986 return sv;
6989 int isl_map_is_bijective(__isl_keep isl_map *map)
6991 int sv;
6993 sv = isl_map_is_single_valued(map);
6994 if (sv < 0 || !sv)
6995 return sv;
6997 map = isl_map_copy(map);
6998 map = isl_map_reverse(map);
6999 sv = isl_map_is_single_valued(map);
7000 isl_map_free(map);
7002 return sv;
7005 int isl_set_is_singleton(__isl_keep isl_set *set)
7007 return isl_map_is_single_valued((isl_map *)set);
7010 int isl_map_is_translation(__isl_keep isl_map *map)
7012 int ok;
7013 isl_set *delta;
7015 delta = isl_map_deltas(isl_map_copy(map));
7016 ok = isl_set_is_singleton(delta);
7017 isl_set_free(delta);
7019 return ok;
7022 static int unique(isl_int *p, unsigned pos, unsigned len)
7024 if (isl_seq_first_non_zero(p, pos) != -1)
7025 return 0;
7026 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
7027 return 0;
7028 return 1;
7031 int isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
7033 int i, j;
7034 unsigned nvar;
7035 unsigned ovar;
7037 if (!bset)
7038 return -1;
7040 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
7041 return 0;
7043 nvar = isl_basic_set_dim(bset, isl_dim_set);
7044 ovar = isl_dim_offset(bset->dim, isl_dim_set);
7045 for (j = 0; j < nvar; ++j) {
7046 int lower = 0, upper = 0;
7047 for (i = 0; i < bset->n_eq; ++i) {
7048 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
7049 continue;
7050 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
7051 return 0;
7052 break;
7054 if (i < bset->n_eq)
7055 continue;
7056 for (i = 0; i < bset->n_ineq; ++i) {
7057 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
7058 continue;
7059 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
7060 return 0;
7061 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
7062 lower = 1;
7063 else
7064 upper = 1;
7066 if (!lower || !upper)
7067 return 0;
7070 return 1;
7073 int isl_set_is_box(__isl_keep isl_set *set)
7075 if (!set)
7076 return -1;
7077 if (set->n != 1)
7078 return 0;
7080 return isl_basic_set_is_box(set->p[0]);
7083 int isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
7085 if (!bset)
7086 return -1;
7088 return isl_dim_is_wrapping(bset->dim);
7091 int isl_set_is_wrapping(__isl_keep isl_set *set)
7093 if (!set)
7094 return -1;
7096 return isl_dim_is_wrapping(set->dim);
7099 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
7101 bmap = isl_basic_map_cow(bmap);
7102 if (!bmap)
7103 return NULL;
7105 bmap->dim = isl_dim_wrap(bmap->dim);
7106 if (!bmap->dim)
7107 goto error;
7109 bmap = isl_basic_map_finalize(bmap);
7111 return (isl_basic_set *)bmap;
7112 error:
7113 isl_basic_map_free(bmap);
7114 return NULL;
7117 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
7119 int i;
7121 map = isl_map_cow(map);
7122 if (!map)
7123 return NULL;
7125 for (i = 0; i < map->n; ++i) {
7126 map->p[i] = (isl_basic_map *)isl_basic_map_wrap(map->p[i]);
7127 if (!map->p[i])
7128 goto error;
7130 map->dim = isl_dim_wrap(map->dim);
7131 if (!map->dim)
7132 goto error;
7134 return (isl_set *)map;
7135 error:
7136 isl_map_free(map);
7137 return NULL;
7140 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
7142 bset = isl_basic_set_cow(bset);
7143 if (!bset)
7144 return NULL;
7146 bset->dim = isl_dim_unwrap(bset->dim);
7147 if (!bset->dim)
7148 goto error;
7150 bset = isl_basic_set_finalize(bset);
7152 return (isl_basic_map *)bset;
7153 error:
7154 isl_basic_set_free(bset);
7155 return NULL;
7158 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
7160 int i;
7162 if (!set)
7163 return NULL;
7165 if (!isl_set_is_wrapping(set))
7166 isl_die(set->ctx, isl_error_invalid, "not a wrapping set",
7167 goto error);
7169 set = isl_set_cow(set);
7170 if (!set)
7171 return NULL;
7173 for (i = 0; i < set->n; ++i) {
7174 set->p[i] = (isl_basic_set *)isl_basic_set_unwrap(set->p[i]);
7175 if (!set->p[i])
7176 goto error;
7179 set->dim = isl_dim_unwrap(set->dim);
7180 if (!set->dim)
7181 goto error;
7183 return (isl_map *)set;
7184 error:
7185 isl_set_free(set);
7186 return NULL;
7189 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
7190 enum isl_dim_type type)
7192 if (!bmap)
7193 return NULL;
7195 if (!isl_dim_is_named_or_nested(bmap->dim, type))
7196 return bmap;
7198 bmap = isl_basic_map_cow(bmap);
7199 if (!bmap)
7200 return NULL;
7202 bmap->dim = isl_dim_reset(bmap->dim, type);
7203 if (!bmap->dim)
7204 goto error;
7206 bmap = isl_basic_map_finalize(bmap);
7208 return bmap;
7209 error:
7210 isl_basic_map_free(bmap);
7211 return NULL;
7214 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
7215 enum isl_dim_type type)
7217 int i;
7219 if (!map)
7220 return NULL;
7222 if (!isl_dim_is_named_or_nested(map->dim, type))
7223 return map;
7225 map = isl_map_cow(map);
7226 if (!map)
7227 return NULL;
7229 for (i = 0; i < map->n; ++i) {
7230 map->p[i] = isl_basic_map_reset(map->p[i], type);
7231 if (!map->p[i])
7232 goto error;
7234 map->dim = isl_dim_reset(map->dim, type);
7235 if (!map->dim)
7236 goto error;
7238 return map;
7239 error:
7240 isl_map_free(map);
7241 return NULL;
7244 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
7246 if (!bmap)
7247 return NULL;
7249 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
7250 return bmap;
7252 bmap = isl_basic_map_cow(bmap);
7253 if (!bmap)
7254 return NULL;
7256 bmap->dim = isl_dim_flatten(bmap->dim);
7257 if (!bmap->dim)
7258 goto error;
7260 bmap = isl_basic_map_finalize(bmap);
7262 return bmap;
7263 error:
7264 isl_basic_map_free(bmap);
7265 return NULL;
7268 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
7270 int i;
7272 if (!map)
7273 return NULL;
7275 if (!map->dim->nested[0] && !map->dim->nested[1])
7276 return map;
7278 map = isl_map_cow(map);
7279 if (!map)
7280 return NULL;
7282 for (i = 0; i < map->n; ++i) {
7283 map->p[i] = isl_basic_map_flatten(map->p[i]);
7284 if (!map->p[i])
7285 goto error;
7287 map->dim = isl_dim_flatten(map->dim);
7288 if (!map->dim)
7289 goto error;
7291 return map;
7292 error:
7293 isl_map_free(map);
7294 return NULL;
7297 /* Extend the given dim_map with mappings for the divs in bmap.
7299 static __isl_give struct isl_dim_map *extend_dim_map(
7300 __isl_keep struct isl_dim_map *dim_map,
7301 __isl_keep isl_basic_map *bmap)
7303 int i;
7304 struct isl_dim_map *res;
7305 int offset;
7307 offset = isl_basic_map_offset(bmap, isl_dim_div);
7309 res = isl_dim_map_alloc(bmap->ctx, dim_map->len - 1 + bmap->n_div);
7310 if (!res)
7311 return NULL;
7313 for (i = 0; i < dim_map->len; ++i)
7314 res->pos[i] = dim_map->pos[i];
7315 for (i = 0; i < bmap->n_div; ++i)
7316 res->pos[dim_map->len + i] = offset + i;
7318 return res;
7321 /* Extract a dim_map from a reordering.
7322 * We essentially need to reverse the mapping, and add an offset
7323 * of 1 for the constant term.
7325 __isl_give struct isl_dim_map *isl_dim_map_from_reordering(
7326 __isl_keep isl_reordering *exp)
7328 int i;
7329 struct isl_dim_map *dim_map;
7331 if (!exp)
7332 return NULL;
7334 dim_map = isl_dim_map_alloc(exp->dim->ctx, isl_dim_total(exp->dim));
7335 if (!dim_map)
7336 return NULL;
7338 for (i = 0; i < exp->len; ++i)
7339 dim_map->pos[1 + exp->pos[i]] = 1 + i;
7341 return dim_map;
7344 /* Reorder the dimensions of "bmap" according to the given dim_map
7345 * and set the dimension specification to "dim".
7347 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
7348 __isl_take isl_dim *dim, __isl_take struct isl_dim_map *dim_map)
7350 isl_basic_map *res;
7352 bmap = isl_basic_map_cow(bmap);
7353 if (!bmap || !dim || !dim_map)
7354 goto error;
7356 res = isl_basic_map_alloc_dim(dim,
7357 bmap->n_div, bmap->n_eq, bmap->n_ineq);
7358 res = add_constraints_dim_map(res, bmap, dim_map);
7359 res = isl_basic_map_finalize(res);
7360 return res;
7361 error:
7362 free(dim_map);
7363 isl_basic_map_free(bmap);
7364 isl_dim_free(dim);
7365 return NULL;
7368 /* Reorder the dimensions of "map" according to given reordering.
7370 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
7371 __isl_take isl_reordering *r)
7373 int i;
7374 struct isl_dim_map *dim_map;
7376 map = isl_map_cow(map);
7377 dim_map = isl_dim_map_from_reordering(r);
7378 if (!map || !r || !dim_map)
7379 goto error;
7381 for (i = 0; i < map->n; ++i) {
7382 struct isl_dim_map *dim_map_i;
7384 dim_map_i = extend_dim_map(dim_map, map->p[i]);
7386 map->p[i] = isl_basic_map_realign(map->p[i],
7387 isl_dim_copy(r->dim), dim_map_i);
7389 if (!map->p[i])
7390 goto error;
7393 map = isl_map_reset_dim(map, isl_dim_copy(r->dim));
7395 isl_reordering_free(r);
7396 free(dim_map);
7397 return map;
7398 error:
7399 free(dim_map);
7400 isl_map_free(map);
7401 isl_reordering_free(r);
7402 return NULL;
7405 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
7406 __isl_take isl_reordering *r)
7408 return (isl_set *)isl_map_realign((isl_map *)set, r);