isl_basic_map_insert: don't simplify the result
[isl.git] / isl_map.c
blob85124bb473cdc4d90348a21f2d5cadf1826a8f1c
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>
30 #include <isl_mat_private.h>
32 /* Maps dst positions to src positions */
33 struct isl_dim_map {
34 unsigned len;
35 int pos[1];
38 static struct isl_dim_map *isl_dim_map_alloc(struct isl_ctx *ctx, unsigned len)
40 int i;
41 struct isl_dim_map *dim_map;
42 dim_map = isl_alloc(ctx, struct isl_dim_map,
43 sizeof(struct isl_dim_map) + len * sizeof(int));
44 if (!dim_map)
45 return NULL;
46 dim_map->len = 1 + len;
47 dim_map->pos[0] = 0;
48 for (i = 0; i < len; ++i)
49 dim_map->pos[1 + i] = -1;
50 return dim_map;
53 static unsigned n(struct isl_dim *dim, enum isl_dim_type type)
55 switch (type) {
56 case isl_dim_param: return dim->nparam;
57 case isl_dim_in: return dim->n_in;
58 case isl_dim_out: return dim->n_out;
59 case isl_dim_all: return dim->nparam + dim->n_in + dim->n_out;
60 default: return 0;
64 static unsigned pos(struct isl_dim *dim, enum isl_dim_type type)
66 switch (type) {
67 case isl_dim_param: return 1;
68 case isl_dim_in: return 1 + dim->nparam;
69 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
70 default: return 0;
74 static void isl_dim_map_dim_range(struct isl_dim_map *dim_map,
75 struct isl_dim *dim, enum isl_dim_type type,
76 unsigned first, unsigned n, unsigned dst_pos)
78 int i;
79 unsigned src_pos;
81 if (!dim_map || !dim)
82 return;
84 src_pos = pos(dim, type);
85 for (i = 0; i < n; ++i)
86 dim_map->pos[1 + dst_pos + i] = src_pos + first + i;
89 static void isl_dim_map_dim(struct isl_dim_map *dim_map, struct isl_dim *dim,
90 enum isl_dim_type type, unsigned dst_pos)
92 isl_dim_map_dim_range(dim_map, dim, type, 0, n(dim, type), dst_pos);
95 static void isl_dim_map_div(struct isl_dim_map *dim_map,
96 struct isl_basic_map *bmap, unsigned dst_pos)
98 int i;
99 unsigned src_pos;
101 if (!dim_map || !bmap)
102 return;
104 src_pos = 1 + isl_dim_total(bmap->dim);
105 for (i = 0; i < bmap->n_div; ++i)
106 dim_map->pos[1 + dst_pos + i] = src_pos + i;
109 static void isl_dim_map_dump(struct isl_dim_map *dim_map)
111 int i;
113 for (i = 0; i < dim_map->len; ++i)
114 fprintf(stderr, "%d -> %d; ", i, dim_map->pos[i]);
115 fprintf(stderr, "\n");
118 unsigned isl_basic_map_dim(const struct isl_basic_map *bmap,
119 enum isl_dim_type type)
121 if (!bmap)
122 return 0;
123 switch (type) {
124 case isl_dim_cst: return 1;
125 case isl_dim_param:
126 case isl_dim_in:
127 case isl_dim_out: return isl_dim_size(bmap->dim, type);
128 case isl_dim_div: return bmap->n_div;
129 case isl_dim_all: return isl_basic_map_total_dim(bmap);
130 default: return 0;
134 unsigned isl_map_dim(const struct isl_map *map, enum isl_dim_type type)
136 return map ? n(map->dim, type) : 0;
139 unsigned isl_set_dim(const struct isl_set *set, enum isl_dim_type type)
141 return set ? n(set->dim, type) : 0;
144 unsigned isl_basic_map_offset(struct isl_basic_map *bmap,
145 enum isl_dim_type type)
147 struct isl_dim *dim = bmap->dim;
148 switch (type) {
149 case isl_dim_cst: return 0;
150 case isl_dim_param: return 1;
151 case isl_dim_in: return 1 + dim->nparam;
152 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
153 case isl_dim_div: return 1 + dim->nparam + dim->n_in + dim->n_out;
154 default: return 0;
158 static unsigned map_offset(struct isl_map *map, enum isl_dim_type type)
160 return pos(map->dim, type);
163 unsigned isl_basic_set_dim(const struct isl_basic_set *bset,
164 enum isl_dim_type type)
166 return isl_basic_map_dim((const struct isl_basic_map*)bset, type);
169 unsigned isl_basic_set_n_dim(const struct isl_basic_set *bset)
171 return isl_basic_set_dim(bset, isl_dim_set);
174 unsigned isl_basic_set_n_param(const struct isl_basic_set *bset)
176 return isl_basic_set_dim(bset, isl_dim_param);
179 unsigned isl_basic_set_total_dim(const struct isl_basic_set *bset)
181 return isl_dim_total(bset->dim) + bset->n_div;
184 unsigned isl_set_n_dim(const struct isl_set *set)
186 return isl_set_dim(set, isl_dim_set);
189 unsigned isl_set_n_param(const struct isl_set *set)
191 return isl_set_dim(set, isl_dim_param);
194 unsigned isl_basic_map_n_in(const struct isl_basic_map *bmap)
196 return bmap ? bmap->dim->n_in : 0;
199 unsigned isl_basic_map_n_out(const struct isl_basic_map *bmap)
201 return bmap ? bmap->dim->n_out : 0;
204 unsigned isl_basic_map_n_param(const struct isl_basic_map *bmap)
206 return bmap ? bmap->dim->nparam : 0;
209 unsigned isl_basic_map_n_div(const struct isl_basic_map *bmap)
211 return bmap ? bmap->n_div : 0;
214 unsigned isl_basic_map_total_dim(const struct isl_basic_map *bmap)
216 return bmap ? isl_dim_total(bmap->dim) + bmap->n_div : 0;
219 unsigned isl_map_n_in(const struct isl_map *map)
221 return map->dim->n_in;
224 unsigned isl_map_n_out(const struct isl_map *map)
226 return map->dim->n_out;
229 unsigned isl_map_n_param(const struct isl_map *map)
231 return map->dim->nparam;
234 int isl_map_compatible_domain(struct isl_map *map, struct isl_set *set)
236 int m;
237 if (!map || !set)
238 return -1;
239 m = isl_dim_match(map->dim, isl_dim_param, set->dim, isl_dim_param);
240 if (m < 0 || !m)
241 return m;
242 return isl_dim_tuple_match(map->dim, isl_dim_in, set->dim, isl_dim_set);
245 int isl_basic_map_compatible_domain(struct isl_basic_map *bmap,
246 struct isl_basic_set *bset)
248 int m;
249 if (!bmap || !bset)
250 return -1;
251 m = isl_dim_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
252 if (m < 0 || !m)
253 return m;
254 return isl_dim_tuple_match(bmap->dim, isl_dim_in, bset->dim, isl_dim_set);
257 int isl_basic_map_compatible_range(struct isl_basic_map *bmap,
258 struct isl_basic_set *bset)
260 int m;
261 if (!bmap || !bset)
262 return -1;
263 m = isl_dim_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
264 if (m < 0 || !m)
265 return m;
266 return isl_dim_tuple_match(bmap->dim, isl_dim_out, bset->dim, isl_dim_set);
269 isl_ctx *isl_basic_map_get_ctx(__isl_keep isl_basic_map *bmap)
271 return bmap ? bmap->ctx : NULL;
274 isl_ctx *isl_basic_set_get_ctx(__isl_keep isl_basic_set *bset)
276 return bset ? bset->ctx : NULL;
279 isl_ctx *isl_map_get_ctx(__isl_keep isl_map *map)
281 return map ? map->ctx : NULL;
284 isl_ctx *isl_set_get_ctx(__isl_keep isl_set *set)
286 return set ? set->ctx : NULL;
289 struct isl_dim *isl_basic_map_get_dim(struct isl_basic_map *bmap)
291 if (!bmap)
292 return NULL;
293 return isl_dim_copy(bmap->dim);
296 struct isl_dim *isl_basic_set_get_dim(struct isl_basic_set *bset)
298 if (!bset)
299 return NULL;
300 return isl_dim_copy(bset->dim);
303 struct isl_dim *isl_map_get_dim(struct isl_map *map)
305 if (!map)
306 return NULL;
307 return isl_dim_copy(map->dim);
310 struct isl_dim *isl_set_get_dim(struct isl_set *set)
312 if (!set)
313 return NULL;
314 return isl_dim_copy(set->dim);
317 __isl_give isl_basic_map *isl_basic_map_set_tuple_name(
318 __isl_take isl_basic_map *bmap, enum isl_dim_type type, const char *s)
320 bmap = isl_basic_map_cow(bmap);
321 if (!bmap)
322 return NULL;
323 bmap->dim = isl_dim_set_tuple_name(bmap->dim, type, s);
324 if (!bmap->dim)
325 goto error;
326 return bmap;
327 error:
328 isl_basic_map_free(bmap);
329 return NULL;
332 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map *bmap,
333 enum isl_dim_type type)
335 return bmap ? isl_dim_get_tuple_name(bmap->dim, type) : NULL;
338 __isl_give isl_map *isl_map_set_tuple_name(__isl_take isl_map *map,
339 enum isl_dim_type type, const char *s)
341 int i;
343 map = isl_map_cow(map);
344 if (!map)
345 return NULL;
347 map->dim = isl_dim_set_tuple_name(map->dim, type, s);
348 if (!map->dim)
349 goto error;
351 for (i = 0; i < map->n; ++i) {
352 map->p[i] = isl_basic_map_set_tuple_name(map->p[i], type, s);
353 if (!map->p[i])
354 goto error;
357 return map;
358 error:
359 isl_map_free(map);
360 return NULL;
363 const char *isl_map_get_tuple_name(__isl_keep isl_map *map,
364 enum isl_dim_type type)
366 return map ? isl_dim_get_tuple_name(map->dim, type) : NULL;
369 __isl_give isl_set *isl_set_set_tuple_name(__isl_take isl_set *set,
370 const char *s)
372 return (isl_set *)isl_map_set_tuple_name((isl_map *)set, isl_dim_set, s);
375 const char *isl_set_get_tuple_name(__isl_keep isl_set *set)
377 return set ? isl_dim_get_tuple_name(set->dim, isl_dim_set) : NULL;
380 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map *bmap,
381 enum isl_dim_type type, unsigned pos)
383 return bmap ? isl_dim_get_name(bmap->dim, type, pos) : NULL;
386 const char *isl_map_get_dim_name(__isl_keep isl_map *map,
387 enum isl_dim_type type, unsigned pos)
389 return map ? isl_dim_get_name(map->dim, type, pos) : NULL;
392 const char *isl_set_get_dim_name(__isl_keep isl_set *set,
393 enum isl_dim_type type, unsigned pos)
395 return set ? isl_dim_get_name(set->dim, type, pos) : NULL;
398 __isl_give isl_basic_map *isl_basic_map_set_dim_name(
399 __isl_take isl_basic_map *bmap,
400 enum isl_dim_type type, unsigned pos, const char *s)
402 if (!bmap)
403 return NULL;
404 bmap->dim = isl_dim_set_name(bmap->dim, type, pos, s);
405 if (!bmap->dim)
406 goto error;
407 return bmap;
408 error:
409 isl_basic_map_free(bmap);
410 return NULL;
413 __isl_give isl_map *isl_map_set_dim_name(__isl_take isl_map *map,
414 enum isl_dim_type type, unsigned pos, const char *s)
416 int i;
418 if (!map)
419 return NULL;
421 map->dim = isl_dim_set_name(map->dim, type, pos, s);
422 if (!map->dim)
423 goto error;
425 for (i = 0; i < map->n; ++i) {
426 map->p[i] = isl_basic_map_set_dim_name(map->p[i], type, pos, s);
427 if (!map->p[i])
428 goto error;
431 return map;
432 error:
433 isl_map_free(map);
434 return NULL;
437 __isl_give isl_basic_set *isl_basic_set_set_dim_name(
438 __isl_take isl_basic_set *bset,
439 enum isl_dim_type type, unsigned pos, const char *s)
441 return (isl_basic_set *)isl_basic_map_set_dim_name(
442 (isl_basic_map *)bset, type, pos, s);
445 __isl_give isl_set *isl_set_set_dim_name(__isl_take isl_set *set,
446 enum isl_dim_type type, unsigned pos, const char *s)
448 return (isl_set *)isl_map_set_dim_name((isl_map *)set, type, pos, s);
451 int isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
453 if (!bmap)
454 return -1;
455 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
458 static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
459 struct isl_basic_map *bmap, unsigned extra,
460 unsigned n_eq, unsigned n_ineq)
462 int i;
463 size_t row_size = 1 + isl_dim_total(bmap->dim) + extra;
465 bmap->ctx = ctx;
466 isl_ctx_ref(ctx);
468 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
469 if (isl_blk_is_error(bmap->block))
470 goto error;
472 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
473 if (!bmap->ineq)
474 goto error;
476 if (extra == 0) {
477 bmap->block2 = isl_blk_empty();
478 bmap->div = NULL;
479 } else {
480 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
481 if (isl_blk_is_error(bmap->block2))
482 goto error;
484 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
485 if (!bmap->div)
486 goto error;
489 for (i = 0; i < n_ineq + n_eq; ++i)
490 bmap->ineq[i] = bmap->block.data + i * row_size;
492 for (i = 0; i < extra; ++i)
493 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
495 bmap->ref = 1;
496 bmap->flags = 0;
497 bmap->c_size = n_eq + n_ineq;
498 bmap->eq = bmap->ineq + n_ineq;
499 bmap->extra = extra;
500 bmap->n_eq = 0;
501 bmap->n_ineq = 0;
502 bmap->n_div = 0;
503 bmap->sample = NULL;
505 return bmap;
506 error:
507 isl_basic_map_free(bmap);
508 return NULL;
511 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
512 unsigned nparam, unsigned dim, unsigned extra,
513 unsigned n_eq, unsigned n_ineq)
515 struct isl_basic_map *bmap;
516 bmap = isl_basic_map_alloc(ctx, nparam, 0, dim, extra, n_eq, n_ineq);
517 return (struct isl_basic_set *)bmap;
520 struct isl_basic_set *isl_basic_set_alloc_dim(struct isl_dim *dim,
521 unsigned extra, unsigned n_eq, unsigned n_ineq)
523 struct isl_basic_map *bmap;
524 if (!dim)
525 return NULL;
526 isl_assert(dim->ctx, dim->n_in == 0, goto error);
527 bmap = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
528 return (struct isl_basic_set *)bmap;
529 error:
530 isl_dim_free(dim);
531 return NULL;
534 struct isl_basic_map *isl_basic_map_alloc_dim(struct isl_dim *dim,
535 unsigned extra, unsigned n_eq, unsigned n_ineq)
537 struct isl_basic_map *bmap;
539 if (!dim)
540 return NULL;
541 bmap = isl_calloc_type(dim->ctx, struct isl_basic_map);
542 if (!bmap)
543 goto error;
544 bmap->dim = dim;
546 return basic_map_init(dim->ctx, bmap, extra, n_eq, n_ineq);
547 error:
548 isl_dim_free(dim);
549 return NULL;
552 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
553 unsigned nparam, unsigned in, unsigned out, unsigned extra,
554 unsigned n_eq, unsigned n_ineq)
556 struct isl_basic_map *bmap;
557 struct isl_dim *dim;
559 dim = isl_dim_alloc(ctx, nparam, in, out);
560 if (!dim)
561 return NULL;
563 bmap = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
564 return bmap;
567 static void dup_constraints(
568 struct isl_basic_map *dst, struct isl_basic_map *src)
570 int i;
571 unsigned total = isl_basic_map_total_dim(src);
573 for (i = 0; i < src->n_eq; ++i) {
574 int j = isl_basic_map_alloc_equality(dst);
575 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
578 for (i = 0; i < src->n_ineq; ++i) {
579 int j = isl_basic_map_alloc_inequality(dst);
580 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
583 for (i = 0; i < src->n_div; ++i) {
584 int j = isl_basic_map_alloc_div(dst);
585 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
587 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
590 struct isl_basic_map *isl_basic_map_dup(struct isl_basic_map *bmap)
592 struct isl_basic_map *dup;
594 if (!bmap)
595 return NULL;
596 dup = isl_basic_map_alloc_dim(isl_dim_copy(bmap->dim),
597 bmap->n_div, bmap->n_eq, bmap->n_ineq);
598 if (!dup)
599 return NULL;
600 dup_constraints(dup, bmap);
601 dup->flags = bmap->flags;
602 dup->sample = isl_vec_copy(bmap->sample);
603 return dup;
606 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
608 struct isl_basic_map *dup;
610 dup = isl_basic_map_dup((struct isl_basic_map *)bset);
611 return (struct isl_basic_set *)dup;
614 struct isl_basic_set *isl_basic_set_copy(struct isl_basic_set *bset)
616 if (!bset)
617 return NULL;
619 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
620 bset->ref++;
621 return bset;
623 return isl_basic_set_dup(bset);
626 struct isl_set *isl_set_copy(struct isl_set *set)
628 if (!set)
629 return NULL;
631 set->ref++;
632 return set;
635 struct isl_basic_map *isl_basic_map_copy(struct isl_basic_map *bmap)
637 if (!bmap)
638 return NULL;
640 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
641 bmap->ref++;
642 return bmap;
644 bmap = isl_basic_map_dup(bmap);
645 if (bmap)
646 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
647 return bmap;
650 struct isl_map *isl_map_copy(struct isl_map *map)
652 if (!map)
653 return NULL;
655 map->ref++;
656 return map;
659 void isl_basic_map_free(struct isl_basic_map *bmap)
661 if (!bmap)
662 return;
664 if (--bmap->ref > 0)
665 return;
667 isl_ctx_deref(bmap->ctx);
668 free(bmap->div);
669 isl_blk_free(bmap->ctx, bmap->block2);
670 free(bmap->ineq);
671 isl_blk_free(bmap->ctx, bmap->block);
672 isl_vec_free(bmap->sample);
673 isl_dim_free(bmap->dim);
674 free(bmap);
677 void isl_basic_set_free(struct isl_basic_set *bset)
679 isl_basic_map_free((struct isl_basic_map *)bset);
682 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
684 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
687 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
689 struct isl_ctx *ctx;
690 if (!bmap)
691 return -1;
692 ctx = bmap->ctx;
693 isl_assert(ctx, room_for_con(bmap, 1), return -1);
694 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
695 return -1);
696 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
697 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
698 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
699 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
700 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
701 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
702 isl_int *t;
703 int j = isl_basic_map_alloc_inequality(bmap);
704 if (j < 0)
705 return -1;
706 t = bmap->ineq[j];
707 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
708 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
709 bmap->eq[-1] = t;
710 bmap->n_eq++;
711 bmap->n_ineq--;
712 bmap->eq--;
713 return 0;
715 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
716 bmap->extra - bmap->n_div);
717 return bmap->n_eq++;
720 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
722 return isl_basic_map_alloc_equality((struct isl_basic_map *)bset);
725 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
727 if (!bmap)
728 return -1;
729 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
730 bmap->n_eq -= n;
731 return 0;
734 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
736 return isl_basic_map_free_equality((struct isl_basic_map *)bset, n);
739 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
741 isl_int *t;
742 if (!bmap)
743 return -1;
744 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
746 if (pos != bmap->n_eq - 1) {
747 t = bmap->eq[pos];
748 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
749 bmap->eq[bmap->n_eq - 1] = t;
751 bmap->n_eq--;
752 return 0;
755 int isl_basic_set_drop_equality(struct isl_basic_set *bset, unsigned pos)
757 return isl_basic_map_drop_equality((struct isl_basic_map *)bset, pos);
760 void isl_basic_map_inequality_to_equality(
761 struct isl_basic_map *bmap, unsigned pos)
763 isl_int *t;
765 t = bmap->ineq[pos];
766 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
767 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
768 bmap->eq[-1] = t;
769 bmap->n_eq++;
770 bmap->n_ineq--;
771 bmap->eq--;
772 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
773 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
774 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
775 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
778 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
780 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
783 int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap)
785 struct isl_ctx *ctx;
786 if (!bmap)
787 return -1;
788 ctx = bmap->ctx;
789 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
790 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
791 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
792 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
793 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
794 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
795 1 + isl_basic_map_total_dim(bmap),
796 bmap->extra - bmap->n_div);
797 return bmap->n_ineq++;
800 int isl_basic_set_alloc_inequality(struct isl_basic_set *bset)
802 return isl_basic_map_alloc_inequality((struct isl_basic_map *)bset);
805 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
807 if (!bmap)
808 return -1;
809 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
810 bmap->n_ineq -= n;
811 return 0;
814 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
816 return isl_basic_map_free_inequality((struct isl_basic_map *)bset, n);
819 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
821 isl_int *t;
822 if (!bmap)
823 return -1;
824 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
826 if (pos != bmap->n_ineq - 1) {
827 t = bmap->ineq[pos];
828 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
829 bmap->ineq[bmap->n_ineq - 1] = t;
830 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
832 bmap->n_ineq--;
833 return 0;
836 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
838 return isl_basic_map_drop_inequality((struct isl_basic_map *)bset, pos);
841 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
842 isl_int *eq)
844 int k;
846 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
847 if (!bmap)
848 return NULL;
849 k = isl_basic_map_alloc_equality(bmap);
850 if (k < 0)
851 goto error;
852 isl_seq_cpy(bmap->eq[k], eq, 1 + isl_basic_map_total_dim(bmap));
853 return bmap;
854 error:
855 isl_basic_map_free(bmap);
856 return NULL;
859 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
860 isl_int *eq)
862 return (isl_basic_set *)
863 isl_basic_map_add_eq((isl_basic_map *)bset, eq);
866 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
867 isl_int *ineq)
869 int k;
871 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
872 if (!bmap)
873 return NULL;
874 k = isl_basic_map_alloc_inequality(bmap);
875 if (k < 0)
876 goto error;
877 isl_seq_cpy(bmap->ineq[k], ineq, 1 + isl_basic_map_total_dim(bmap));
878 return bmap;
879 error:
880 isl_basic_map_free(bmap);
881 return NULL;
884 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
885 isl_int *ineq)
887 return (isl_basic_set *)
888 isl_basic_map_add_ineq((isl_basic_map *)bset, ineq);
891 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
893 if (!bmap)
894 return -1;
895 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
896 isl_seq_clr(bmap->div[bmap->n_div] +
897 1 + 1 + isl_basic_map_total_dim(bmap),
898 bmap->extra - bmap->n_div);
899 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
900 return bmap->n_div++;
903 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
905 return isl_basic_map_alloc_div((struct isl_basic_map *)bset);
908 int isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
910 if (!bmap)
911 return -1;
912 isl_assert(bmap->ctx, n <= bmap->n_div, return -1);
913 bmap->n_div -= n;
914 return 0;
917 int isl_basic_set_free_div(struct isl_basic_set *bset, unsigned n)
919 return isl_basic_map_free_div((struct isl_basic_map *)bset, n);
922 /* Copy constraint from src to dst, putting the vars of src at offset
923 * dim_off in dst and the divs of src at offset div_off in dst.
924 * If both sets are actually map, then dim_off applies to the input
925 * variables.
927 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
928 struct isl_basic_map *src_map, isl_int *src,
929 unsigned in_off, unsigned out_off, unsigned div_off)
931 unsigned src_nparam = isl_basic_map_n_param(src_map);
932 unsigned dst_nparam = isl_basic_map_n_param(dst_map);
933 unsigned src_in = isl_basic_map_n_in(src_map);
934 unsigned dst_in = isl_basic_map_n_in(dst_map);
935 unsigned src_out = isl_basic_map_n_out(src_map);
936 unsigned dst_out = isl_basic_map_n_out(dst_map);
937 isl_int_set(dst[0], src[0]);
938 isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
939 if (dst_nparam > src_nparam)
940 isl_seq_clr(dst+1+src_nparam,
941 dst_nparam - src_nparam);
942 isl_seq_clr(dst+1+dst_nparam, in_off);
943 isl_seq_cpy(dst+1+dst_nparam+in_off,
944 src+1+src_nparam,
945 isl_min(dst_in-in_off, src_in));
946 if (dst_in-in_off > src_in)
947 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
948 dst_in - in_off - src_in);
949 isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
950 isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
951 src+1+src_nparam+src_in,
952 isl_min(dst_out-out_off, src_out));
953 if (dst_out-out_off > src_out)
954 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
955 dst_out - out_off - src_out);
956 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
957 isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
958 src+1+src_nparam+src_in+src_out,
959 isl_min(dst_map->extra-div_off, src_map->n_div));
960 if (dst_map->n_div-div_off > src_map->n_div)
961 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
962 div_off+src_map->n_div,
963 dst_map->n_div - div_off - src_map->n_div);
966 static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
967 struct isl_basic_map *src_map, isl_int *src,
968 unsigned in_off, unsigned out_off, unsigned div_off)
970 isl_int_set(dst[0], src[0]);
971 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
974 static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
975 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
977 int i;
978 unsigned div_off;
980 if (!bmap1 || !bmap2)
981 goto error;
983 div_off = bmap1->n_div;
985 for (i = 0; i < bmap2->n_eq; ++i) {
986 int i1 = isl_basic_map_alloc_equality(bmap1);
987 if (i1 < 0)
988 goto error;
989 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
990 i_pos, o_pos, div_off);
993 for (i = 0; i < bmap2->n_ineq; ++i) {
994 int i1 = isl_basic_map_alloc_inequality(bmap1);
995 if (i1 < 0)
996 goto error;
997 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
998 i_pos, o_pos, div_off);
1001 for (i = 0; i < bmap2->n_div; ++i) {
1002 int i1 = isl_basic_map_alloc_div(bmap1);
1003 if (i1 < 0)
1004 goto error;
1005 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
1006 i_pos, o_pos, div_off);
1009 isl_basic_map_free(bmap2);
1011 return bmap1;
1013 error:
1014 isl_basic_map_free(bmap1);
1015 isl_basic_map_free(bmap2);
1016 return NULL;
1019 static void copy_constraint_dim_map(isl_int *dst, isl_int *src,
1020 struct isl_dim_map *dim_map)
1022 int i;
1024 for (i = 0; i < dim_map->len; ++i) {
1025 if (dim_map->pos[i] < 0)
1026 isl_int_set_si(dst[i], 0);
1027 else
1028 isl_int_set(dst[i], src[dim_map->pos[i]]);
1032 static void copy_div_dim_map(isl_int *dst, isl_int *src,
1033 struct isl_dim_map *dim_map)
1035 isl_int_set(dst[0], src[0]);
1036 copy_constraint_dim_map(dst+1, src+1, dim_map);
1039 static struct isl_basic_map *add_constraints_dim_map(struct isl_basic_map *dst,
1040 struct isl_basic_map *src, struct isl_dim_map *dim_map)
1042 int i;
1044 if (!src || !dst || !dim_map)
1045 goto error;
1047 for (i = 0; i < src->n_eq; ++i) {
1048 int i1 = isl_basic_map_alloc_equality(dst);
1049 if (i1 < 0)
1050 goto error;
1051 copy_constraint_dim_map(dst->eq[i1], src->eq[i], dim_map);
1054 for (i = 0; i < src->n_ineq; ++i) {
1055 int i1 = isl_basic_map_alloc_inequality(dst);
1056 if (i1 < 0)
1057 goto error;
1058 copy_constraint_dim_map(dst->ineq[i1], src->ineq[i], dim_map);
1061 for (i = 0; i < src->n_div; ++i) {
1062 int i1 = isl_basic_map_alloc_div(dst);
1063 if (i1 < 0)
1064 goto error;
1065 copy_div_dim_map(dst->div[i1], src->div[i], dim_map);
1068 free(dim_map);
1069 isl_basic_map_free(src);
1071 return dst;
1072 error:
1073 free(dim_map);
1074 isl_basic_map_free(src);
1075 isl_basic_map_free(dst);
1076 return NULL;
1079 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
1080 struct isl_basic_set *bset2, unsigned pos)
1082 return (struct isl_basic_set *)
1083 add_constraints((struct isl_basic_map *)bset1,
1084 (struct isl_basic_map *)bset2, 0, pos);
1087 struct isl_basic_map *isl_basic_map_extend_dim(struct isl_basic_map *base,
1088 struct isl_dim *dim, unsigned extra,
1089 unsigned n_eq, unsigned n_ineq)
1091 struct isl_basic_map *ext;
1092 unsigned flags;
1093 int dims_ok;
1095 if (!dim)
1096 goto error;
1098 if (!base)
1099 goto error;
1101 dims_ok = isl_dim_equal(base->dim, dim) &&
1102 base->extra >= base->n_div + extra;
1104 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1105 room_for_ineq(base, n_ineq)) {
1106 isl_dim_free(dim);
1107 return base;
1110 isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error);
1111 isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error);
1112 isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error);
1113 extra += base->extra;
1114 n_eq += base->n_eq;
1115 n_ineq += base->n_ineq;
1117 ext = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
1118 dim = NULL;
1119 if (!ext)
1120 goto error;
1122 if (dims_ok)
1123 ext->sample = isl_vec_copy(base->sample);
1124 flags = base->flags;
1125 ext = add_constraints(ext, base, 0, 0);
1126 if (ext) {
1127 ext->flags = flags;
1128 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
1131 return ext;
1133 error:
1134 isl_dim_free(dim);
1135 isl_basic_map_free(base);
1136 return NULL;
1139 struct isl_basic_set *isl_basic_set_extend_dim(struct isl_basic_set *base,
1140 struct isl_dim *dim, unsigned extra,
1141 unsigned n_eq, unsigned n_ineq)
1143 return (struct isl_basic_set *)
1144 isl_basic_map_extend_dim((struct isl_basic_map *)base, dim,
1145 extra, n_eq, n_ineq);
1148 struct isl_basic_map *isl_basic_map_extend_constraints(
1149 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1151 if (!base)
1152 return NULL;
1153 return isl_basic_map_extend_dim(base, isl_dim_copy(base->dim),
1154 0, n_eq, n_ineq);
1157 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
1158 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
1159 unsigned n_eq, unsigned n_ineq)
1161 struct isl_basic_map *bmap;
1162 struct isl_dim *dim;
1164 if (!base)
1165 return NULL;
1166 dim = isl_dim_alloc(base->ctx, nparam, n_in, n_out);
1167 if (!dim)
1168 goto error;
1170 bmap = isl_basic_map_extend_dim(base, dim, extra, n_eq, n_ineq);
1171 return bmap;
1172 error:
1173 isl_basic_map_free(base);
1174 return NULL;
1177 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
1178 unsigned nparam, unsigned dim, unsigned extra,
1179 unsigned n_eq, unsigned n_ineq)
1181 return (struct isl_basic_set *)
1182 isl_basic_map_extend((struct isl_basic_map *)base,
1183 nparam, 0, dim, extra, n_eq, n_ineq);
1186 struct isl_basic_set *isl_basic_set_extend_constraints(
1187 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1189 return (struct isl_basic_set *)
1190 isl_basic_map_extend_constraints((struct isl_basic_map *)base,
1191 n_eq, n_ineq);
1194 struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset)
1196 return (struct isl_basic_set *)
1197 isl_basic_map_cow((struct isl_basic_map *)bset);
1200 struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
1202 if (!bmap)
1203 return NULL;
1205 if (bmap->ref > 1) {
1206 bmap->ref--;
1207 bmap = isl_basic_map_dup(bmap);
1209 if (bmap)
1210 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
1211 return bmap;
1214 struct isl_set *isl_set_cow(struct isl_set *set)
1216 if (!set)
1217 return NULL;
1219 if (set->ref == 1)
1220 return set;
1221 set->ref--;
1222 return isl_set_dup(set);
1225 struct isl_map *isl_map_cow(struct isl_map *map)
1227 if (!map)
1228 return NULL;
1230 if (map->ref == 1)
1231 return map;
1232 map->ref--;
1233 return isl_map_dup(map);
1236 static void swap_vars(struct isl_blk blk, isl_int *a,
1237 unsigned a_len, unsigned b_len)
1239 isl_seq_cpy(blk.data, a+a_len, b_len);
1240 isl_seq_cpy(blk.data+b_len, a, a_len);
1241 isl_seq_cpy(a, blk.data, b_len+a_len);
1244 struct isl_basic_set *isl_basic_set_swap_vars(
1245 struct isl_basic_set *bset, unsigned n)
1247 int i;
1248 struct isl_blk blk;
1249 unsigned dim;
1250 unsigned nparam;
1252 if (!bset)
1253 goto error;
1255 nparam = isl_basic_set_n_param(bset);
1256 dim = isl_basic_set_n_dim(bset);
1257 isl_assert(bset->ctx, n <= dim, goto error);
1259 if (n == dim)
1260 return bset;
1262 bset = isl_basic_set_cow(bset);
1263 if (!bset)
1264 return NULL;
1266 blk = isl_blk_alloc(bset->ctx, dim);
1267 if (isl_blk_is_error(blk))
1268 goto error;
1270 for (i = 0; i < bset->n_eq; ++i)
1271 swap_vars(blk,
1272 bset->eq[i]+1+nparam, n, dim - n);
1274 for (i = 0; i < bset->n_ineq; ++i)
1275 swap_vars(blk,
1276 bset->ineq[i]+1+nparam, n, dim - n);
1278 for (i = 0; i < bset->n_div; ++i)
1279 swap_vars(blk,
1280 bset->div[i]+1+1+nparam, n, dim - n);
1282 isl_blk_free(bset->ctx, blk);
1284 ISL_F_CLR(bset, ISL_BASIC_SET_NORMALIZED);
1285 bset = isl_basic_set_gauss(bset, NULL);
1286 return isl_basic_set_finalize(bset);
1287 error:
1288 isl_basic_set_free(bset);
1289 return NULL;
1292 struct isl_set *isl_set_swap_vars(struct isl_set *set, unsigned n)
1294 int i;
1295 set = isl_set_cow(set);
1296 if (!set)
1297 return NULL;
1299 for (i = 0; i < set->n; ++i) {
1300 set->p[i] = isl_basic_set_swap_vars(set->p[i], n);
1301 if (!set->p[i]) {
1302 isl_set_free(set);
1303 return NULL;
1306 ISL_F_CLR(set, ISL_SET_NORMALIZED);
1307 return set;
1310 struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
1312 int i = 0;
1313 unsigned total;
1314 if (!bmap)
1315 goto error;
1316 total = isl_basic_map_total_dim(bmap);
1317 isl_basic_map_free_div(bmap, bmap->n_div);
1318 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1319 if (bmap->n_eq > 0)
1320 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
1321 else {
1322 i = isl_basic_map_alloc_equality(bmap);
1323 if (i < 0)
1324 goto error;
1326 isl_int_set_si(bmap->eq[i][0], 1);
1327 isl_seq_clr(bmap->eq[i]+1, total);
1328 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
1329 isl_vec_free(bmap->sample);
1330 bmap->sample = NULL;
1331 return isl_basic_map_finalize(bmap);
1332 error:
1333 isl_basic_map_free(bmap);
1334 return NULL;
1337 struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
1339 return (struct isl_basic_set *)
1340 isl_basic_map_set_to_empty((struct isl_basic_map *)bset);
1343 void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
1345 int i;
1346 unsigned off = isl_dim_total(bmap->dim);
1347 isl_int *t = bmap->div[a];
1348 bmap->div[a] = bmap->div[b];
1349 bmap->div[b] = t;
1351 for (i = 0; i < bmap->n_eq; ++i)
1352 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
1354 for (i = 0; i < bmap->n_ineq; ++i)
1355 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
1357 for (i = 0; i < bmap->n_div; ++i)
1358 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
1359 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1362 /* Eliminate the specified n dimensions starting at first from the
1363 * constraints using Fourier-Motzkin. The dimensions themselves
1364 * are not removed.
1366 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
1367 enum isl_dim_type type, unsigned first, unsigned n)
1369 int i;
1371 if (!map)
1372 return NULL;
1373 if (n == 0)
1374 return map;
1376 map = isl_map_cow(map);
1377 if (!map)
1378 return NULL;
1379 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
1380 first += pos(map->dim, type) - 1;
1382 for (i = 0; i < map->n; ++i) {
1383 map->p[i] = isl_basic_map_eliminate_vars(map->p[i], first, n);
1384 if (!map->p[i])
1385 goto error;
1387 return map;
1388 error:
1389 isl_map_free(map);
1390 return NULL;
1393 /* Eliminate the specified n dimensions starting at first from the
1394 * constraints using Fourier-Motzkin. The dimensions themselves
1395 * are not removed.
1397 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
1398 enum isl_dim_type type, unsigned first, unsigned n)
1400 return (isl_set *)isl_map_eliminate((isl_map *)set, type, first, n);
1403 /* Eliminate the specified n dimensions starting at first from the
1404 * constraints using Fourier-Motzkin. The dimensions themselves
1405 * are not removed.
1407 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
1408 unsigned first, unsigned n)
1410 return isl_set_eliminate(set, isl_dim_set, first, n);
1413 __isl_give isl_basic_map *isl_basic_map_remove_divs(
1414 __isl_take isl_basic_map *bmap)
1416 bmap = isl_basic_map_eliminate_vars(bmap, isl_dim_total(bmap->dim),
1417 bmap->n_div);
1418 if (!bmap)
1419 return NULL;
1420 bmap->n_div = 0;
1421 return bmap;
1424 __isl_give isl_basic_set *isl_basic_set_remove_divs(
1425 __isl_take isl_basic_set *bset)
1427 return (struct isl_basic_set *)isl_basic_map_remove_divs(
1428 (struct isl_basic_map *)bset);
1431 struct isl_set *isl_set_remove_divs(struct isl_set *set)
1433 int i;
1435 if (!set)
1436 return NULL;
1437 if (set->n == 0)
1438 return set;
1440 set = isl_set_cow(set);
1441 if (!set)
1442 return NULL;
1444 for (i = 0; i < set->n; ++i) {
1445 set->p[i] = isl_basic_set_remove_divs(set->p[i]);
1446 if (!set->p[i])
1447 goto error;
1449 return set;
1450 error:
1451 isl_set_free(set);
1452 return NULL;
1455 struct isl_basic_map *isl_basic_map_remove_dims(struct isl_basic_map *bmap,
1456 enum isl_dim_type type, unsigned first, unsigned n)
1458 if (!bmap)
1459 return NULL;
1460 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1461 goto error);
1462 if (n == 0)
1463 return bmap;
1464 bmap = isl_basic_map_eliminate_vars(bmap,
1465 isl_basic_map_offset(bmap, type) - 1 + first, n);
1466 if (!bmap)
1467 return bmap;
1468 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
1469 return bmap;
1470 bmap = isl_basic_map_drop(bmap, type, first, n);
1471 return bmap;
1472 error:
1473 isl_basic_map_free(bmap);
1474 return NULL;
1477 /* Return true if the definition of the given div (recursively) involves
1478 * any of the given variables.
1480 static int div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
1481 unsigned first, unsigned n)
1483 int i;
1484 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
1486 if (isl_int_is_zero(bmap->div[div][0]))
1487 return 0;
1488 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
1489 return 1;
1491 for (i = bmap->n_div - 1; i >= 0; --i) {
1492 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
1493 continue;
1494 if (div_involves_vars(bmap, i, first, n))
1495 return 1;
1498 return 0;
1501 /* Remove all divs (recursively) involving any of the given dimensions
1502 * in their definitions.
1504 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
1505 __isl_take isl_basic_map *bmap,
1506 enum isl_dim_type type, unsigned first, unsigned n)
1508 int i;
1510 if (!bmap)
1511 return NULL;
1512 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1513 goto error);
1514 first += isl_basic_map_offset(bmap, type);
1516 for (i = bmap->n_div - 1; i >= 0; --i) {
1517 if (!div_involves_vars(bmap, i, first, n))
1518 continue;
1519 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
1522 return bmap;
1523 error:
1524 isl_basic_map_free(bmap);
1525 return NULL;
1528 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
1529 enum isl_dim_type type, unsigned first, unsigned n)
1531 int i;
1533 if (!map)
1534 return NULL;
1535 if (map->n == 0)
1536 return map;
1538 map = isl_map_cow(map);
1539 if (!map)
1540 return NULL;
1542 for (i = 0; i < map->n; ++i) {
1543 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
1544 type, first, n);
1545 if (!map->p[i])
1546 goto error;
1548 return map;
1549 error:
1550 isl_map_free(map);
1551 return NULL;
1554 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
1555 enum isl_dim_type type, unsigned first, unsigned n)
1557 return (isl_set *)isl_map_remove_divs_involving_dims((isl_map *)set,
1558 type, first, n);
1561 /* Return true if the definition of the given div is unknown or depends
1562 * on unknown divs.
1564 static int div_is_unknown(__isl_keep isl_basic_map *bmap, int div)
1566 int i;
1567 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
1569 if (isl_int_is_zero(bmap->div[div][0]))
1570 return 1;
1572 for (i = bmap->n_div - 1; i >= 0; --i) {
1573 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
1574 continue;
1575 if (div_is_unknown(bmap, i))
1576 return 1;
1579 return 0;
1582 /* Remove all divs that are unknown or defined in terms of unknown divs.
1584 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
1585 __isl_take isl_basic_map *bmap)
1587 int i;
1589 if (!bmap)
1590 return NULL;
1592 for (i = bmap->n_div - 1; i >= 0; --i) {
1593 if (!div_is_unknown(bmap, i))
1594 continue;
1595 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
1598 return bmap;
1599 error:
1600 isl_basic_map_free(bmap);
1601 return NULL;
1604 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
1606 int i;
1608 if (!map)
1609 return NULL;
1610 if (map->n == 0)
1611 return map;
1613 map = isl_map_cow(map);
1614 if (!map)
1615 return NULL;
1617 for (i = 0; i < map->n; ++i) {
1618 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
1619 if (!map->p[i])
1620 goto error;
1622 return map;
1623 error:
1624 isl_map_free(map);
1625 return NULL;
1628 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
1630 return (isl_set *)isl_map_remove_unknown_divs((isl_map *)set);
1633 __isl_give isl_basic_set *isl_basic_set_remove_dims(
1634 __isl_take isl_basic_set *bset,
1635 enum isl_dim_type type, unsigned first, unsigned n)
1637 return (isl_basic_set *)
1638 isl_basic_map_remove_dims((isl_basic_map *)bset, type, first, n);
1641 struct isl_map *isl_map_remove_dims(struct isl_map *map,
1642 enum isl_dim_type type, unsigned first, unsigned n)
1644 int i;
1646 if (n == 0)
1647 return map;
1649 map = isl_map_cow(map);
1650 if (!map)
1651 return NULL;
1652 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
1654 for (i = 0; i < map->n; ++i) {
1655 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
1656 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
1657 if (!map->p[i])
1658 goto error;
1660 map = isl_map_drop(map, type, first, n);
1661 return map;
1662 error:
1663 isl_map_free(map);
1664 return NULL;
1667 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
1668 enum isl_dim_type type, unsigned first, unsigned n)
1670 return (isl_set *)isl_map_remove_dims((isl_map *)bset, type, first, n);
1673 /* Project out n inputs starting at first using Fourier-Motzkin */
1674 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
1675 unsigned first, unsigned n)
1677 return isl_map_remove_dims(map, isl_dim_in, first, n);
1680 static void dump_term(struct isl_basic_map *bmap,
1681 isl_int c, int pos, FILE *out)
1683 const char *name;
1684 unsigned in = isl_basic_map_n_in(bmap);
1685 unsigned dim = in + isl_basic_map_n_out(bmap);
1686 unsigned nparam = isl_basic_map_n_param(bmap);
1687 if (!pos)
1688 isl_int_print(out, c, 0);
1689 else {
1690 if (!isl_int_is_one(c))
1691 isl_int_print(out, c, 0);
1692 if (pos < 1 + nparam) {
1693 name = isl_dim_get_name(bmap->dim,
1694 isl_dim_param, pos - 1);
1695 if (name)
1696 fprintf(out, "%s", name);
1697 else
1698 fprintf(out, "p%d", pos - 1);
1699 } else if (pos < 1 + nparam + in)
1700 fprintf(out, "i%d", pos - 1 - nparam);
1701 else if (pos < 1 + nparam + dim)
1702 fprintf(out, "o%d", pos - 1 - nparam - in);
1703 else
1704 fprintf(out, "e%d", pos - 1 - nparam - dim);
1708 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
1709 int sign, FILE *out)
1711 int i;
1712 int first;
1713 unsigned len = 1 + isl_basic_map_total_dim(bmap);
1714 isl_int v;
1716 isl_int_init(v);
1717 for (i = 0, first = 1; i < len; ++i) {
1718 if (isl_int_sgn(c[i]) * sign <= 0)
1719 continue;
1720 if (!first)
1721 fprintf(out, " + ");
1722 first = 0;
1723 isl_int_abs(v, c[i]);
1724 dump_term(bmap, v, i, out);
1726 isl_int_clear(v);
1727 if (first)
1728 fprintf(out, "0");
1731 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
1732 const char *op, FILE *out, int indent)
1734 int i;
1736 fprintf(out, "%*s", indent, "");
1738 dump_constraint_sign(bmap, c, 1, out);
1739 fprintf(out, " %s ", op);
1740 dump_constraint_sign(bmap, c, -1, out);
1742 fprintf(out, "\n");
1744 for (i = bmap->n_div; i < bmap->extra; ++i) {
1745 if (isl_int_is_zero(c[1+isl_dim_total(bmap->dim)+i]))
1746 continue;
1747 fprintf(out, "%*s", indent, "");
1748 fprintf(out, "ERROR: unused div coefficient not zero\n");
1749 abort();
1753 static void dump_constraints(struct isl_basic_map *bmap,
1754 isl_int **c, unsigned n,
1755 const char *op, FILE *out, int indent)
1757 int i;
1759 for (i = 0; i < n; ++i)
1760 dump_constraint(bmap, c[i], op, out, indent);
1763 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
1765 int j;
1766 int first = 1;
1767 unsigned total = isl_basic_map_total_dim(bmap);
1769 for (j = 0; j < 1 + total; ++j) {
1770 if (isl_int_is_zero(exp[j]))
1771 continue;
1772 if (!first && isl_int_is_pos(exp[j]))
1773 fprintf(out, "+");
1774 dump_term(bmap, exp[j], j, out);
1775 first = 0;
1779 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
1781 int i;
1783 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
1784 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
1786 for (i = 0; i < bmap->n_div; ++i) {
1787 fprintf(out, "%*s", indent, "");
1788 fprintf(out, "e%d = [(", i);
1789 dump_affine(bmap, bmap->div[i]+1, out);
1790 fprintf(out, ")/");
1791 isl_int_print(out, bmap->div[i][0], 0);
1792 fprintf(out, "]\n");
1796 void isl_basic_set_print_internal(struct isl_basic_set *bset,
1797 FILE *out, int indent)
1799 if (!bset) {
1800 fprintf(out, "null basic set\n");
1801 return;
1804 fprintf(out, "%*s", indent, "");
1805 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
1806 bset->ref, bset->dim->nparam, bset->dim->n_out,
1807 bset->extra, bset->flags);
1808 dump((struct isl_basic_map *)bset, out, indent);
1811 void isl_basic_map_print_internal(struct isl_basic_map *bmap,
1812 FILE *out, int indent)
1814 if (!bmap) {
1815 fprintf(out, "null basic map\n");
1816 return;
1819 fprintf(out, "%*s", indent, "");
1820 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
1821 "flags: %x, n_name: %d\n",
1822 bmap->ref,
1823 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
1824 bmap->extra, bmap->flags, bmap->dim->n_name);
1825 dump(bmap, out, indent);
1828 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
1830 unsigned total;
1831 if (!bmap)
1832 return -1;
1833 total = isl_basic_map_total_dim(bmap);
1834 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1835 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
1836 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
1837 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1838 return 0;
1841 struct isl_set *isl_set_alloc_dim(struct isl_dim *dim, int n, unsigned flags)
1843 struct isl_set *set;
1845 if (!dim)
1846 return NULL;
1847 isl_assert(dim->ctx, dim->n_in == 0, goto error);
1848 isl_assert(dim->ctx, n >= 0, goto error);
1849 set = isl_alloc(dim->ctx, struct isl_set,
1850 sizeof(struct isl_set) +
1851 (n - 1) * sizeof(struct isl_basic_set *));
1852 if (!set)
1853 goto error;
1855 set->ctx = dim->ctx;
1856 isl_ctx_ref(set->ctx);
1857 set->ref = 1;
1858 set->size = n;
1859 set->n = 0;
1860 set->dim = dim;
1861 set->flags = flags;
1862 return set;
1863 error:
1864 isl_dim_free(dim);
1865 return NULL;
1868 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
1869 unsigned nparam, unsigned dim, int n, unsigned flags)
1871 struct isl_set *set;
1872 struct isl_dim *dims;
1874 dims = isl_dim_alloc(ctx, nparam, 0, dim);
1875 if (!dims)
1876 return NULL;
1878 set = isl_set_alloc_dim(dims, n, flags);
1879 return set;
1882 /* Make sure "map" has room for at least "n" more basic maps.
1884 struct isl_map *isl_map_grow(struct isl_map *map, int n)
1886 int i;
1887 struct isl_map *grown = NULL;
1889 if (!map)
1890 return NULL;
1891 isl_assert(map->ctx, n >= 0, goto error);
1892 if (map->n + n <= map->size)
1893 return map;
1894 grown = isl_map_alloc_dim(isl_map_get_dim(map), map->n + n, map->flags);
1895 if (!grown)
1896 goto error;
1897 for (i = 0; i < map->n; ++i) {
1898 grown->p[i] = isl_basic_map_copy(map->p[i]);
1899 if (!grown->p[i])
1900 goto error;
1901 grown->n++;
1903 isl_map_free(map);
1904 return grown;
1905 error:
1906 isl_map_free(grown);
1907 isl_map_free(map);
1908 return NULL;
1911 /* Make sure "set" has room for at least "n" more basic sets.
1913 struct isl_set *isl_set_grow(struct isl_set *set, int n)
1915 return (struct isl_set *)isl_map_grow((struct isl_map *)set, n);
1918 struct isl_set *isl_set_dup(struct isl_set *set)
1920 int i;
1921 struct isl_set *dup;
1923 if (!set)
1924 return NULL;
1926 dup = isl_set_alloc_dim(isl_dim_copy(set->dim), set->n, set->flags);
1927 if (!dup)
1928 return NULL;
1929 for (i = 0; i < set->n; ++i)
1930 dup = isl_set_add_basic_set(dup, isl_basic_set_copy(set->p[i]));
1931 return dup;
1934 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
1936 struct isl_set *set;
1938 if (!bset)
1939 return NULL;
1941 set = isl_set_alloc_dim(isl_dim_copy(bset->dim), 1, ISL_MAP_DISJOINT);
1942 if (!set) {
1943 isl_basic_set_free(bset);
1944 return NULL;
1946 return isl_set_add_basic_set(set, bset);
1949 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
1951 struct isl_map *map;
1953 if (!bmap)
1954 return NULL;
1956 map = isl_map_alloc_dim(isl_dim_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
1957 if (!map) {
1958 isl_basic_map_free(bmap);
1959 return NULL;
1961 return isl_map_add_basic_map(map, bmap);
1964 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
1965 __isl_take isl_basic_set *bset)
1967 return (struct isl_set *)isl_map_add_basic_map((struct isl_map *)set,
1968 (struct isl_basic_map *)bset);
1971 void isl_set_free(struct isl_set *set)
1973 int i;
1975 if (!set)
1976 return;
1978 if (--set->ref > 0)
1979 return;
1981 isl_ctx_deref(set->ctx);
1982 for (i = 0; i < set->n; ++i)
1983 isl_basic_set_free(set->p[i]);
1984 isl_dim_free(set->dim);
1985 free(set);
1988 void isl_set_print_internal(struct isl_set *set, FILE *out, int indent)
1990 int i;
1992 if (!set) {
1993 fprintf(out, "null set\n");
1994 return;
1997 fprintf(out, "%*s", indent, "");
1998 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
1999 set->ref, set->n, set->dim->nparam, set->dim->n_out,
2000 set->flags);
2001 for (i = 0; i < set->n; ++i) {
2002 fprintf(out, "%*s", indent, "");
2003 fprintf(out, "basic set %d:\n", i);
2004 isl_basic_set_print_internal(set->p[i], out, indent+4);
2008 void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
2010 int i;
2012 if (!map) {
2013 fprintf(out, "null map\n");
2014 return;
2017 fprintf(out, "%*s", indent, "");
2018 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2019 "flags: %x, n_name: %d\n",
2020 map->ref, map->n, map->dim->nparam, map->dim->n_in,
2021 map->dim->n_out, map->flags, map->dim->n_name);
2022 for (i = 0; i < map->n; ++i) {
2023 fprintf(out, "%*s", indent, "");
2024 fprintf(out, "basic map %d:\n", i);
2025 isl_basic_map_print_internal(map->p[i], out, indent+4);
2029 struct isl_basic_map *isl_basic_map_intersect_domain(
2030 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2032 struct isl_basic_map *bmap_domain;
2033 struct isl_dim *dim;
2035 if (!bmap || !bset)
2036 goto error;
2038 isl_assert(bset->ctx, isl_dim_match(bmap->dim, isl_dim_param,
2039 bset->dim, isl_dim_param), goto error);
2041 if (isl_dim_size(bset->dim, isl_dim_set) != 0)
2042 isl_assert(bset->ctx,
2043 isl_basic_map_compatible_domain(bmap, bset), goto error);
2045 bmap = isl_basic_map_cow(bmap);
2046 if (!bmap)
2047 goto error;
2048 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
2049 bset->n_div, bset->n_eq, bset->n_ineq);
2050 dim = isl_dim_reverse(isl_dim_copy(bset->dim));
2051 bmap_domain = isl_basic_map_from_basic_set(bset, dim);
2052 bmap = add_constraints(bmap, bmap_domain, 0, 0);
2054 bmap = isl_basic_map_simplify(bmap);
2055 return isl_basic_map_finalize(bmap);
2056 error:
2057 isl_basic_map_free(bmap);
2058 isl_basic_set_free(bset);
2059 return NULL;
2062 struct isl_basic_map *isl_basic_map_intersect_range(
2063 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2065 struct isl_basic_map *bmap_range;
2067 if (!bmap || !bset)
2068 goto error;
2070 isl_assert(bset->ctx, isl_dim_match(bmap->dim, isl_dim_param,
2071 bset->dim, isl_dim_param), goto error);
2073 if (isl_dim_size(bset->dim, isl_dim_set) != 0)
2074 isl_assert(bset->ctx,
2075 isl_basic_map_compatible_range(bmap, bset), goto error);
2077 bmap = isl_basic_map_cow(bmap);
2078 if (!bmap)
2079 goto error;
2080 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
2081 bset->n_div, bset->n_eq, bset->n_ineq);
2082 bmap_range = isl_basic_map_from_basic_set(bset, isl_dim_copy(bset->dim));
2083 bmap = add_constraints(bmap, bmap_range, 0, 0);
2085 bmap = isl_basic_map_simplify(bmap);
2086 return isl_basic_map_finalize(bmap);
2087 error:
2088 isl_basic_map_free(bmap);
2089 isl_basic_set_free(bset);
2090 return NULL;
2093 int isl_basic_map_contains(struct isl_basic_map *bmap, struct isl_vec *vec)
2095 int i;
2096 unsigned total;
2097 isl_int s;
2099 total = 1 + isl_basic_map_total_dim(bmap);
2100 if (total != vec->size)
2101 return -1;
2103 isl_int_init(s);
2105 for (i = 0; i < bmap->n_eq; ++i) {
2106 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
2107 if (!isl_int_is_zero(s)) {
2108 isl_int_clear(s);
2109 return 0;
2113 for (i = 0; i < bmap->n_ineq; ++i) {
2114 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
2115 if (isl_int_is_neg(s)) {
2116 isl_int_clear(s);
2117 return 0;
2121 isl_int_clear(s);
2123 return 1;
2126 int isl_basic_set_contains(struct isl_basic_set *bset, struct isl_vec *vec)
2128 return isl_basic_map_contains((struct isl_basic_map *)bset, vec);
2131 struct isl_basic_map *isl_basic_map_intersect(
2132 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2134 struct isl_vec *sample = NULL;
2136 if (!bmap1 || !bmap2)
2137 goto error;
2139 isl_assert(bmap1->ctx, isl_dim_match(bmap1->dim, isl_dim_param,
2140 bmap2->dim, isl_dim_param), goto error);
2141 if (isl_dim_total(bmap1->dim) ==
2142 isl_dim_size(bmap1->dim, isl_dim_param) &&
2143 isl_dim_total(bmap2->dim) !=
2144 isl_dim_size(bmap2->dim, isl_dim_param))
2145 return isl_basic_map_intersect(bmap2, bmap1);
2147 if (isl_dim_total(bmap2->dim) !=
2148 isl_dim_size(bmap2->dim, isl_dim_param))
2149 isl_assert(bmap1->ctx,
2150 isl_dim_equal(bmap1->dim, bmap2->dim), goto error);
2152 if (bmap1->sample &&
2153 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
2154 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
2155 sample = isl_vec_copy(bmap1->sample);
2156 else if (bmap2->sample &&
2157 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
2158 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
2159 sample = isl_vec_copy(bmap2->sample);
2161 bmap1 = isl_basic_map_cow(bmap1);
2162 if (!bmap1)
2163 goto error;
2164 bmap1 = isl_basic_map_extend_dim(bmap1, isl_dim_copy(bmap1->dim),
2165 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
2166 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
2168 if (!bmap1)
2169 isl_vec_free(sample);
2170 else if (sample) {
2171 isl_vec_free(bmap1->sample);
2172 bmap1->sample = sample;
2175 bmap1 = isl_basic_map_simplify(bmap1);
2176 return isl_basic_map_finalize(bmap1);
2177 error:
2178 if (sample)
2179 isl_vec_free(sample);
2180 isl_basic_map_free(bmap1);
2181 isl_basic_map_free(bmap2);
2182 return NULL;
2185 struct isl_basic_set *isl_basic_set_intersect(
2186 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
2188 return (struct isl_basic_set *)
2189 isl_basic_map_intersect(
2190 (struct isl_basic_map *)bset1,
2191 (struct isl_basic_map *)bset2);
2194 /* Special case of isl_map_intersect, where both map1 and map2
2195 * are convex, without any divs and such that either map1 or map2
2196 * contains a single constraint. This constraint is then simply
2197 * added to the other map.
2199 static __isl_give isl_map *map_intersect_add_constraint(
2200 __isl_take isl_map *map1, __isl_take isl_map *map2)
2202 struct isl_basic_map *bmap1;
2203 struct isl_basic_map *bmap2;
2205 isl_assert(map1->ctx, map1->n == 1, goto error);
2206 isl_assert(map2->ctx, map1->n == 1, goto error);
2207 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
2208 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
2210 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
2211 return isl_map_intersect(map2, map1);
2213 isl_assert(map2->ctx,
2214 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1, goto error);
2216 map1 = isl_map_cow(map1);
2217 if (!map1)
2218 goto error;
2219 if (isl_map_fast_is_empty(map1)) {
2220 isl_map_free(map2);
2221 return map1;
2223 map1->p[0] = isl_basic_map_cow(map1->p[0]);
2224 if (map2->p[0]->n_eq == 1)
2225 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
2226 else
2227 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
2228 map2->p[0]->ineq[0]);
2230 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
2231 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
2232 if (!map1->p[0])
2233 goto error;
2235 if (isl_basic_map_fast_is_empty(map1->p[0])) {
2236 isl_basic_map_free(map1->p[0]);
2237 map1->n = 0;
2240 isl_map_free(map2);
2242 return map1;
2243 error:
2244 isl_map_free(map1);
2245 isl_map_free(map2);
2246 return NULL;
2249 struct isl_map *isl_map_intersect(struct isl_map *map1, struct isl_map *map2)
2251 unsigned flags = 0;
2252 struct isl_map *result;
2253 int i, j;
2255 if (!map1 || !map2)
2256 goto error;
2258 if (isl_map_fast_is_empty(map1)) {
2259 isl_map_free(map2);
2260 return map1;
2262 if (isl_map_fast_is_empty(map2)) {
2263 isl_map_free(map1);
2264 return map2;
2267 if (map1->n == 1 && map2->n == 1 &&
2268 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
2269 isl_dim_equal(map1->dim, map2->dim) &&
2270 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
2271 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
2272 return map_intersect_add_constraint(map1, map2);
2273 isl_assert(map1->ctx, isl_dim_match(map1->dim, isl_dim_param,
2274 map2->dim, isl_dim_param), goto error);
2275 if (isl_dim_total(map1->dim) ==
2276 isl_dim_size(map1->dim, isl_dim_param) &&
2277 isl_dim_total(map2->dim) != isl_dim_size(map2->dim, isl_dim_param))
2278 return isl_map_intersect(map2, map1);
2280 if (isl_dim_total(map2->dim) != isl_dim_size(map2->dim, isl_dim_param))
2281 isl_assert(map1->ctx,
2282 isl_dim_equal(map1->dim, map2->dim), goto error);
2284 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
2285 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
2286 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
2288 result = isl_map_alloc_dim(isl_dim_copy(map1->dim),
2289 map1->n * map2->n, flags);
2290 if (!result)
2291 goto error;
2292 for (i = 0; i < map1->n; ++i)
2293 for (j = 0; j < map2->n; ++j) {
2294 struct isl_basic_map *part;
2295 part = isl_basic_map_intersect(
2296 isl_basic_map_copy(map1->p[i]),
2297 isl_basic_map_copy(map2->p[j]));
2298 if (isl_basic_map_is_empty(part))
2299 isl_basic_map_free(part);
2300 else
2301 result = isl_map_add_basic_map(result, part);
2302 if (!result)
2303 goto error;
2305 isl_map_free(map1);
2306 isl_map_free(map2);
2307 return result;
2308 error:
2309 isl_map_free(map1);
2310 isl_map_free(map2);
2311 return NULL;
2314 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
2316 return (struct isl_set *)
2317 isl_map_intersect((struct isl_map *)set1,
2318 (struct isl_map *)set2);
2321 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
2323 struct isl_dim *dim;
2324 struct isl_basic_set *bset;
2325 unsigned in;
2327 if (!bmap)
2328 return NULL;
2329 bmap = isl_basic_map_cow(bmap);
2330 if (!bmap)
2331 return NULL;
2332 dim = isl_dim_reverse(isl_dim_copy(bmap->dim));
2333 in = isl_basic_map_n_in(bmap);
2334 bset = isl_basic_set_from_basic_map(bmap);
2335 bset = isl_basic_set_swap_vars(bset, in);
2336 return isl_basic_map_from_basic_set(bset, dim);
2339 __isl_give isl_basic_map *isl_basic_map_insert(__isl_take isl_basic_map *bmap,
2340 enum isl_dim_type type, unsigned pos, unsigned n)
2342 struct isl_dim *res_dim;
2343 struct isl_basic_map *res;
2344 struct isl_dim_map *dim_map;
2345 unsigned total, off;
2346 enum isl_dim_type t;
2348 if (n == 0)
2349 return bmap;
2351 if (!bmap)
2352 return NULL;
2354 res_dim = isl_dim_insert(isl_basic_map_get_dim(bmap), type, pos, n);
2356 total = isl_basic_map_total_dim(bmap) + n;
2357 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2358 off = 0;
2359 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2360 if (t != type) {
2361 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2362 } else {
2363 unsigned size = isl_basic_map_dim(bmap, t);
2364 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2365 0, pos, off);
2366 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2367 pos, size - pos, off + pos + n);
2369 off += isl_dim_size(res_dim, t);
2371 isl_dim_map_div(dim_map, bmap, off);
2373 res = isl_basic_map_alloc_dim(res_dim,
2374 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2375 res = add_constraints_dim_map(res, bmap, dim_map);
2376 return isl_basic_map_finalize(res);
2379 __isl_give isl_basic_map *isl_basic_map_add(__isl_take isl_basic_map *bmap,
2380 enum isl_dim_type type, unsigned n)
2382 if (!bmap)
2383 return NULL;
2384 return isl_basic_map_insert(bmap, type,
2385 isl_basic_map_dim(bmap, type), n);
2388 __isl_give isl_basic_set *isl_basic_set_add(__isl_take isl_basic_set *bset,
2389 enum isl_dim_type type, unsigned n)
2391 if (!bset)
2392 return NULL;
2393 isl_assert(bset->ctx, type != isl_dim_in, goto error);
2394 return (isl_basic_set *)isl_basic_map_add((isl_basic_map *)bset, type, n);
2395 error:
2396 isl_basic_set_free(bset);
2397 return NULL;
2400 __isl_give isl_map *isl_map_insert(__isl_take isl_map *map,
2401 enum isl_dim_type type, unsigned pos, unsigned n)
2403 int i;
2405 if (n == 0)
2406 return map;
2408 map = isl_map_cow(map);
2409 if (!map)
2410 return NULL;
2412 map->dim = isl_dim_insert(map->dim, type, pos, n);
2413 if (!map->dim)
2414 goto error;
2416 for (i = 0; i < map->n; ++i) {
2417 map->p[i] = isl_basic_map_insert(map->p[i], type, pos, n);
2418 if (!map->p[i])
2419 goto error;
2422 return map;
2423 error:
2424 isl_map_free(map);
2425 return NULL;
2428 __isl_give isl_set *isl_set_insert(__isl_take isl_set *set,
2429 enum isl_dim_type type, unsigned pos, unsigned n)
2431 return (isl_set *)isl_map_insert((isl_map *)set, type, pos, n);
2434 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
2435 enum isl_dim_type type, unsigned n)
2437 if (!map)
2438 return NULL;
2439 return isl_map_insert(map, type, isl_map_dim(map, type), n);
2442 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
2443 enum isl_dim_type type, unsigned n)
2445 if (!set)
2446 return NULL;
2447 isl_assert(set->ctx, type != isl_dim_in, goto error);
2448 return (isl_set *)isl_map_add_dims((isl_map *)set, type, n);
2449 error:
2450 isl_set_free(set);
2451 return NULL;
2454 __isl_give isl_basic_map *isl_basic_map_move_dims(
2455 __isl_take isl_basic_map *bmap,
2456 enum isl_dim_type dst_type, unsigned dst_pos,
2457 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2459 int i;
2460 struct isl_dim_map *dim_map;
2461 struct isl_basic_map *res;
2462 enum isl_dim_type t;
2463 unsigned total, off;
2465 if (!bmap)
2466 return NULL;
2467 if (n == 0)
2468 return bmap;
2470 isl_assert(bmap->ctx, src_pos + n <= isl_basic_map_dim(bmap, src_type),
2471 goto error);
2473 if (dst_type == src_type && dst_pos == src_pos)
2474 return bmap;
2476 isl_assert(bmap->ctx, dst_type != src_type, goto error);
2478 if (pos(bmap->dim, dst_type) + dst_pos ==
2479 pos(bmap->dim, src_type) + src_pos +
2480 ((src_type < dst_type) ? n : 0)) {
2481 bmap = isl_basic_map_cow(bmap);
2482 if (!bmap)
2483 return NULL;
2485 bmap->dim = isl_dim_move(bmap->dim, dst_type, dst_pos,
2486 src_type, src_pos, n);
2487 if (!bmap->dim)
2488 goto error;
2490 bmap = isl_basic_map_finalize(bmap);
2492 return bmap;
2495 total = isl_basic_map_total_dim(bmap);
2496 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2498 off = 0;
2499 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2500 unsigned size = isl_dim_size(bmap->dim, t);
2501 if (t == dst_type) {
2502 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2503 0, dst_pos, off);
2504 off += dst_pos;
2505 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
2506 src_pos, n, off);
2507 off += n;
2508 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2509 dst_pos, size - dst_pos, off);
2510 off += size - dst_pos;
2511 } else if (t == src_type) {
2512 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2513 0, src_pos, off);
2514 off += src_pos;
2515 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2516 src_pos + n, size - src_pos - n, off);
2517 off += size - src_pos - n;
2518 } else {
2519 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2520 off += size;
2523 isl_dim_map_div(dim_map, bmap, off);
2525 res = isl_basic_map_alloc_dim(isl_basic_map_get_dim(bmap),
2526 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2527 bmap = add_constraints_dim_map(res, bmap, dim_map);
2529 bmap->dim = isl_dim_move(bmap->dim, dst_type, dst_pos,
2530 src_type, src_pos, n);
2531 if (!bmap->dim)
2532 goto error;
2534 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2535 bmap = isl_basic_map_gauss(bmap, NULL);
2536 bmap = isl_basic_map_finalize(bmap);
2538 return bmap;
2539 error:
2540 isl_basic_map_free(bmap);
2541 return NULL;
2544 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
2545 enum isl_dim_type dst_type, unsigned dst_pos,
2546 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2548 return (isl_basic_set *)isl_basic_map_move_dims(
2549 (isl_basic_map *)bset, dst_type, dst_pos, src_type, src_pos, n);
2552 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
2553 enum isl_dim_type dst_type, unsigned dst_pos,
2554 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2556 if (!set)
2557 return NULL;
2558 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
2559 return (isl_set *)isl_map_move_dims((isl_map *)set, dst_type, dst_pos,
2560 src_type, src_pos, n);
2561 error:
2562 isl_set_free(set);
2563 return NULL;
2566 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
2567 enum isl_dim_type dst_type, unsigned dst_pos,
2568 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2570 int i;
2572 if (!map)
2573 return NULL;
2574 if (n == 0)
2575 return map;
2577 isl_assert(map->ctx, src_pos + n <= isl_map_dim(map, src_type),
2578 goto error);
2580 if (dst_type == src_type && dst_pos == src_pos)
2581 return map;
2583 isl_assert(map->ctx, dst_type != src_type, goto error);
2585 map = isl_map_cow(map);
2586 if (!map)
2587 return NULL;
2589 map->dim = isl_dim_move(map->dim, dst_type, dst_pos, src_type, src_pos, n);
2590 if (!map->dim)
2591 goto error;
2593 for (i = 0; i < map->n; ++i) {
2594 map->p[i] = isl_basic_map_move_dims(map->p[i],
2595 dst_type, dst_pos,
2596 src_type, src_pos, n);
2597 if (!map->p[i])
2598 goto error;
2601 return map;
2602 error:
2603 isl_map_free(map);
2604 return NULL;
2607 /* Move the specified dimensions to the last columns right before
2608 * the divs. Don't change the dimension specification of bmap.
2609 * That's the responsibility of the caller.
2611 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
2612 enum isl_dim_type type, unsigned first, unsigned n)
2614 struct isl_dim_map *dim_map;
2615 struct isl_basic_map *res;
2616 enum isl_dim_type t;
2617 unsigned total, off;
2619 if (!bmap)
2620 return NULL;
2621 if (pos(bmap->dim, type) + first + n == 1 + isl_dim_total(bmap->dim))
2622 return bmap;
2624 total = isl_basic_map_total_dim(bmap);
2625 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2627 off = 0;
2628 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2629 unsigned size = isl_dim_size(bmap->dim, t);
2630 if (t == type) {
2631 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2632 0, first, off);
2633 off += first;
2634 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2635 first, n, total - bmap->n_div - n);
2636 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2637 first + n, size - (first + n), off);
2638 off += size - (first + n);
2639 } else {
2640 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2641 off += size;
2644 isl_dim_map_div(dim_map, bmap, off + n);
2646 res = isl_basic_map_alloc_dim(isl_basic_map_get_dim(bmap),
2647 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2648 res = add_constraints_dim_map(res, bmap, dim_map);
2649 return res;
2652 /* Turn the n dimensions of type type, starting at first
2653 * into existentially quantified variables.
2655 __isl_give isl_basic_map *isl_basic_map_project_out(
2656 __isl_take isl_basic_map *bmap,
2657 enum isl_dim_type type, unsigned first, unsigned n)
2659 int i;
2660 size_t row_size;
2661 isl_int **new_div;
2662 isl_int *old;
2664 if (n == 0)
2665 return bmap;
2667 if (!bmap)
2668 return NULL;
2670 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
2671 return isl_basic_map_remove_dims(bmap, type, first, n);
2673 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
2674 goto error);
2676 bmap = move_last(bmap, type, first, n);
2677 bmap = isl_basic_map_cow(bmap);
2678 if (!bmap)
2679 return NULL;
2681 row_size = 1 + isl_dim_total(bmap->dim) + bmap->extra;
2682 old = bmap->block2.data;
2683 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
2684 (bmap->extra + n) * (1 + row_size));
2685 if (!bmap->block2.data)
2686 goto error;
2687 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
2688 if (!new_div)
2689 goto error;
2690 for (i = 0; i < n; ++i) {
2691 new_div[i] = bmap->block2.data +
2692 (bmap->extra + i) * (1 + row_size);
2693 isl_seq_clr(new_div[i], 1 + row_size);
2695 for (i = 0; i < bmap->extra; ++i)
2696 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
2697 free(bmap->div);
2698 bmap->div = new_div;
2699 bmap->n_div += n;
2700 bmap->extra += n;
2702 bmap->dim = isl_dim_drop(bmap->dim, type, first, n);
2703 if (!bmap->dim)
2704 goto error;
2705 bmap = isl_basic_map_simplify(bmap);
2706 bmap = isl_basic_map_drop_redundant_divs(bmap);
2707 return isl_basic_map_finalize(bmap);
2708 error:
2709 isl_basic_map_free(bmap);
2710 return NULL;
2713 /* Turn the n dimensions of type type, starting at first
2714 * into existentially quantified variables.
2716 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
2717 enum isl_dim_type type, unsigned first, unsigned n)
2719 return (isl_basic_set *)isl_basic_map_project_out(
2720 (isl_basic_map *)bset, type, first, n);
2723 /* Turn the n dimensions of type type, starting at first
2724 * into existentially quantified variables.
2726 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
2727 enum isl_dim_type type, unsigned first, unsigned n)
2729 int i;
2731 if (!map)
2732 return NULL;
2734 if (n == 0)
2735 return map;
2737 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
2739 map = isl_map_cow(map);
2740 if (!map)
2741 return NULL;
2743 map->dim = isl_dim_drop(map->dim, type, first, n);
2744 if (!map->dim)
2745 goto error;
2747 for (i = 0; i < map->n; ++i) {
2748 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
2749 if (!map->p[i])
2750 goto error;
2753 return map;
2754 error:
2755 isl_map_free(map);
2756 return NULL;
2759 /* Turn the n dimensions of type type, starting at first
2760 * into existentially quantified variables.
2762 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
2763 enum isl_dim_type type, unsigned first, unsigned n)
2765 return (isl_set *)isl_map_project_out((isl_map *)set, type, first, n);
2768 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
2770 int i, j;
2772 for (i = 0; i < n; ++i) {
2773 j = isl_basic_map_alloc_div(bmap);
2774 if (j < 0)
2775 goto error;
2776 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
2778 return bmap;
2779 error:
2780 isl_basic_map_free(bmap);
2781 return NULL;
2784 struct isl_basic_map *isl_basic_map_apply_range(
2785 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2787 struct isl_dim *dim_result = NULL;
2788 struct isl_basic_map *bmap;
2789 unsigned n_in, n_out, n, nparam, total, pos;
2790 struct isl_dim_map *dim_map1, *dim_map2;
2792 if (!bmap1 || !bmap2)
2793 goto error;
2795 dim_result = isl_dim_join(isl_dim_copy(bmap1->dim),
2796 isl_dim_copy(bmap2->dim));
2798 n_in = isl_basic_map_n_in(bmap1);
2799 n_out = isl_basic_map_n_out(bmap2);
2800 n = isl_basic_map_n_out(bmap1);
2801 nparam = isl_basic_map_n_param(bmap1);
2803 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
2804 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
2805 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
2806 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
2807 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
2808 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
2809 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
2810 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
2811 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
2812 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
2813 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
2815 bmap = isl_basic_map_alloc_dim(dim_result,
2816 bmap1->n_div + bmap2->n_div + n,
2817 bmap1->n_eq + bmap2->n_eq,
2818 bmap1->n_ineq + bmap2->n_ineq);
2819 bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
2820 bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
2821 bmap = add_divs(bmap, n);
2822 bmap = isl_basic_map_simplify(bmap);
2823 bmap = isl_basic_map_drop_redundant_divs(bmap);
2824 return isl_basic_map_finalize(bmap);
2825 error:
2826 isl_basic_map_free(bmap1);
2827 isl_basic_map_free(bmap2);
2828 return NULL;
2831 struct isl_basic_set *isl_basic_set_apply(
2832 struct isl_basic_set *bset, struct isl_basic_map *bmap)
2834 if (!bset || !bmap)
2835 goto error;
2837 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
2838 goto error);
2840 return (struct isl_basic_set *)
2841 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
2842 error:
2843 isl_basic_set_free(bset);
2844 isl_basic_map_free(bmap);
2845 return NULL;
2848 struct isl_basic_map *isl_basic_map_apply_domain(
2849 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2851 if (!bmap1 || !bmap2)
2852 goto error;
2854 isl_assert(bmap1->ctx,
2855 isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
2856 isl_assert(bmap1->ctx,
2857 isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
2858 goto error);
2860 bmap1 = isl_basic_map_reverse(bmap1);
2861 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
2862 return isl_basic_map_reverse(bmap1);
2863 error:
2864 isl_basic_map_free(bmap1);
2865 isl_basic_map_free(bmap2);
2866 return NULL;
2869 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
2870 * A \cap B -> f(A) + f(B)
2872 struct isl_basic_map *isl_basic_map_sum(
2873 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2875 unsigned n_in, n_out, nparam, total, pos;
2876 struct isl_basic_map *bmap = NULL;
2877 struct isl_dim_map *dim_map1, *dim_map2;
2878 int i;
2880 if (!bmap1 || !bmap2)
2881 goto error;
2883 isl_assert(bmap1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim),
2884 goto error);
2886 nparam = isl_basic_map_n_param(bmap1);
2887 n_in = isl_basic_map_n_in(bmap1);
2888 n_out = isl_basic_map_n_out(bmap1);
2890 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
2891 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
2892 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
2893 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
2894 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
2895 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
2896 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
2897 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
2898 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
2899 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
2900 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
2902 bmap = isl_basic_map_alloc_dim(isl_dim_copy(bmap1->dim),
2903 bmap1->n_div + bmap2->n_div + 2 * n_out,
2904 bmap1->n_eq + bmap2->n_eq + n_out,
2905 bmap1->n_ineq + bmap2->n_ineq);
2906 for (i = 0; i < n_out; ++i) {
2907 int j = isl_basic_map_alloc_equality(bmap);
2908 if (j < 0)
2909 goto error;
2910 isl_seq_clr(bmap->eq[j], 1+total);
2911 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
2912 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
2913 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
2915 bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
2916 bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
2917 bmap = add_divs(bmap, 2 * n_out);
2919 bmap = isl_basic_map_simplify(bmap);
2920 return isl_basic_map_finalize(bmap);
2921 error:
2922 isl_basic_map_free(bmap);
2923 isl_basic_map_free(bmap1);
2924 isl_basic_map_free(bmap2);
2925 return NULL;
2928 /* Given two maps A -> f(A) and B -> g(B), construct a map
2929 * A \cap B -> f(A) + f(B)
2931 struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
2933 struct isl_map *result;
2934 int i, j;
2936 if (!map1 || !map2)
2937 goto error;
2939 isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
2941 result = isl_map_alloc_dim(isl_dim_copy(map1->dim),
2942 map1->n * map2->n, 0);
2943 if (!result)
2944 goto error;
2945 for (i = 0; i < map1->n; ++i)
2946 for (j = 0; j < map2->n; ++j) {
2947 struct isl_basic_map *part;
2948 part = isl_basic_map_sum(
2949 isl_basic_map_copy(map1->p[i]),
2950 isl_basic_map_copy(map2->p[j]));
2951 if (isl_basic_map_is_empty(part))
2952 isl_basic_map_free(part);
2953 else
2954 result = isl_map_add_basic_map(result, part);
2955 if (!result)
2956 goto error;
2958 isl_map_free(map1);
2959 isl_map_free(map2);
2960 return result;
2961 error:
2962 isl_map_free(map1);
2963 isl_map_free(map2);
2964 return NULL;
2967 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
2968 __isl_take isl_set *set2)
2970 return (isl_set *)isl_map_sum((isl_map *)set1, (isl_map *)set2);
2973 /* Given a basic map A -> f(A), construct A -> -f(A).
2975 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
2977 int i, j;
2978 unsigned off, n;
2980 bmap = isl_basic_map_cow(bmap);
2981 if (!bmap)
2982 return NULL;
2984 n = isl_basic_map_dim(bmap, isl_dim_out);
2985 off = isl_basic_map_offset(bmap, isl_dim_out);
2986 for (i = 0; i < bmap->n_eq; ++i)
2987 for (j = 0; j < n; ++j)
2988 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
2989 for (i = 0; i < bmap->n_ineq; ++i)
2990 for (j = 0; j < n; ++j)
2991 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
2992 for (i = 0; i < bmap->n_div; ++i)
2993 for (j = 0; j < n; ++j)
2994 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
2995 return isl_basic_map_finalize(bmap);
2998 /* Given a map A -> f(A), construct A -> -f(A).
3000 struct isl_map *isl_map_neg(struct isl_map *map)
3002 int i;
3004 map = isl_map_cow(map);
3005 if (!map)
3006 return NULL;
3008 for (i = 0; i < map->n; ++i) {
3009 map->p[i] = isl_basic_map_neg(map->p[i]);
3010 if (!map->p[i])
3011 goto error;
3014 return map;
3015 error:
3016 isl_map_free(map);
3017 return NULL;
3020 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
3022 return (isl_set *)isl_map_neg((isl_map *)set);
3025 /* Given a basic map A -> f(A) and an integer d, construct a basic map
3026 * A -> floor(f(A)/d).
3028 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
3029 isl_int d)
3031 unsigned n_in, n_out, nparam, total, pos;
3032 struct isl_basic_map *result = NULL;
3033 struct isl_dim_map *dim_map;
3034 int i;
3036 if (!bmap)
3037 return NULL;
3039 nparam = isl_basic_map_n_param(bmap);
3040 n_in = isl_basic_map_n_in(bmap);
3041 n_out = isl_basic_map_n_out(bmap);
3043 total = nparam + n_in + n_out + bmap->n_div + n_out;
3044 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3045 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
3046 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
3047 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
3048 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
3050 result = isl_basic_map_alloc_dim(isl_dim_copy(bmap->dim),
3051 bmap->n_div + n_out,
3052 bmap->n_eq, bmap->n_ineq + 2 * n_out);
3053 result = add_constraints_dim_map(result, bmap, dim_map);
3054 result = add_divs(result, n_out);
3055 for (i = 0; i < n_out; ++i) {
3056 int j;
3057 j = isl_basic_map_alloc_inequality(result);
3058 if (j < 0)
3059 goto error;
3060 isl_seq_clr(result->ineq[j], 1+total);
3061 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
3062 isl_int_set_si(result->ineq[j][1+pos+i], 1);
3063 j = isl_basic_map_alloc_inequality(result);
3064 if (j < 0)
3065 goto error;
3066 isl_seq_clr(result->ineq[j], 1+total);
3067 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
3068 isl_int_set_si(result->ineq[j][1+pos+i], -1);
3069 isl_int_sub_ui(result->ineq[j][0], d, 1);
3072 result = isl_basic_map_simplify(result);
3073 return isl_basic_map_finalize(result);
3074 error:
3075 isl_basic_map_free(result);
3076 return NULL;
3079 /* Given a map A -> f(A) and an integer d, construct a map
3080 * A -> floor(f(A)/d).
3082 struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
3084 int i;
3086 map = isl_map_cow(map);
3087 if (!map)
3088 return NULL;
3090 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3091 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3092 for (i = 0; i < map->n; ++i) {
3093 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
3094 if (!map->p[i])
3095 goto error;
3098 return map;
3099 error:
3100 isl_map_free(map);
3101 return NULL;
3104 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
3106 int i;
3107 unsigned nparam;
3108 unsigned n_in;
3110 i = isl_basic_map_alloc_equality(bmap);
3111 if (i < 0)
3112 goto error;
3113 nparam = isl_basic_map_n_param(bmap);
3114 n_in = isl_basic_map_n_in(bmap);
3115 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
3116 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
3117 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
3118 return isl_basic_map_finalize(bmap);
3119 error:
3120 isl_basic_map_free(bmap);
3121 return NULL;
3124 /* Add a constraints to "bmap" expressing i_pos < o_pos
3126 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
3128 int i;
3129 unsigned nparam;
3130 unsigned n_in;
3132 i = isl_basic_map_alloc_inequality(bmap);
3133 if (i < 0)
3134 goto error;
3135 nparam = isl_basic_map_n_param(bmap);
3136 n_in = isl_basic_map_n_in(bmap);
3137 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3138 isl_int_set_si(bmap->ineq[i][0], -1);
3139 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
3140 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
3141 return isl_basic_map_finalize(bmap);
3142 error:
3143 isl_basic_map_free(bmap);
3144 return NULL;
3147 /* Add a constraint to "bmap" expressing i_pos <= o_pos
3149 static __isl_give isl_basic_map *var_less_or_equal(
3150 __isl_take isl_basic_map *bmap, unsigned pos)
3152 int i;
3153 unsigned nparam;
3154 unsigned n_in;
3156 i = isl_basic_map_alloc_inequality(bmap);
3157 if (i < 0)
3158 goto error;
3159 nparam = isl_basic_map_n_param(bmap);
3160 n_in = isl_basic_map_n_in(bmap);
3161 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3162 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
3163 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
3164 return isl_basic_map_finalize(bmap);
3165 error:
3166 isl_basic_map_free(bmap);
3167 return NULL;
3170 /* Add a constraints to "bmap" expressing i_pos > o_pos
3172 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
3174 int i;
3175 unsigned nparam;
3176 unsigned n_in;
3178 i = isl_basic_map_alloc_inequality(bmap);
3179 if (i < 0)
3180 goto error;
3181 nparam = isl_basic_map_n_param(bmap);
3182 n_in = isl_basic_map_n_in(bmap);
3183 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3184 isl_int_set_si(bmap->ineq[i][0], -1);
3185 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
3186 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
3187 return isl_basic_map_finalize(bmap);
3188 error:
3189 isl_basic_map_free(bmap);
3190 return NULL;
3193 /* Add a constraint to "bmap" expressing i_pos >= o_pos
3195 static __isl_give isl_basic_map *var_more_or_equal(
3196 __isl_take isl_basic_map *bmap, unsigned pos)
3198 int i;
3199 unsigned nparam;
3200 unsigned n_in;
3202 i = isl_basic_map_alloc_inequality(bmap);
3203 if (i < 0)
3204 goto error;
3205 nparam = isl_basic_map_n_param(bmap);
3206 n_in = isl_basic_map_n_in(bmap);
3207 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3208 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
3209 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
3210 return isl_basic_map_finalize(bmap);
3211 error:
3212 isl_basic_map_free(bmap);
3213 return NULL;
3216 struct isl_basic_map *isl_basic_map_equal(struct isl_dim *dim, unsigned n_equal)
3218 int i;
3219 struct isl_basic_map *bmap;
3220 bmap = isl_basic_map_alloc_dim(dim, 0, n_equal, 0);
3221 if (!bmap)
3222 return NULL;
3223 for (i = 0; i < n_equal && bmap; ++i)
3224 bmap = var_equal(bmap, i);
3225 return isl_basic_map_finalize(bmap);
3228 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
3230 struct isl_basic_map *isl_basic_map_less_at(struct isl_dim *dim, unsigned pos)
3232 int i;
3233 struct isl_basic_map *bmap;
3234 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
3235 if (!bmap)
3236 return NULL;
3237 for (i = 0; i < pos && bmap; ++i)
3238 bmap = var_equal(bmap, i);
3239 if (bmap)
3240 bmap = var_less(bmap, pos);
3241 return isl_basic_map_finalize(bmap);
3244 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
3246 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
3247 __isl_take isl_dim *dim, unsigned pos)
3249 int i;
3250 isl_basic_map *bmap;
3252 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
3253 for (i = 0; i < pos; ++i)
3254 bmap = var_equal(bmap, i);
3255 bmap = var_less_or_equal(bmap, pos);
3256 return isl_basic_map_finalize(bmap);
3259 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
3261 struct isl_basic_map *isl_basic_map_more_at(struct isl_dim *dim, unsigned pos)
3263 int i;
3264 struct isl_basic_map *bmap;
3265 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
3266 if (!bmap)
3267 return NULL;
3268 for (i = 0; i < pos && bmap; ++i)
3269 bmap = var_equal(bmap, i);
3270 if (bmap)
3271 bmap = var_more(bmap, pos);
3272 return isl_basic_map_finalize(bmap);
3275 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
3277 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
3278 __isl_take isl_dim *dim, unsigned pos)
3280 int i;
3281 isl_basic_map *bmap;
3283 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
3284 for (i = 0; i < pos; ++i)
3285 bmap = var_equal(bmap, i);
3286 bmap = var_more_or_equal(bmap, pos);
3287 return isl_basic_map_finalize(bmap);
3290 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_dim *dims,
3291 unsigned n, int equal)
3293 struct isl_map *map;
3294 int i;
3296 if (n == 0 && equal)
3297 return isl_map_universe(dims);
3299 map = isl_map_alloc_dim(isl_dim_copy(dims), n, ISL_MAP_DISJOINT);
3301 for (i = 0; i + 1 < n; ++i)
3302 map = isl_map_add_basic_map(map,
3303 isl_basic_map_less_at(isl_dim_copy(dims), i));
3304 if (n > 0) {
3305 if (equal)
3306 map = isl_map_add_basic_map(map,
3307 isl_basic_map_less_or_equal_at(dims, n - 1));
3308 else
3309 map = isl_map_add_basic_map(map,
3310 isl_basic_map_less_at(dims, n - 1));
3311 } else
3312 isl_dim_free(dims);
3314 return map;
3317 static __isl_give isl_map *map_lex_lte(__isl_take isl_dim *dims, int equal)
3319 if (!dims)
3320 return NULL;
3321 return map_lex_lte_first(dims, dims->n_out, equal);
3324 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_dim *dim, unsigned n)
3326 return map_lex_lte_first(dim, n, 0);
3329 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_dim *dim, unsigned n)
3331 return map_lex_lte_first(dim, n, 1);
3334 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_dim *set_dim)
3336 return map_lex_lte(isl_dim_map(set_dim), 0);
3339 __isl_give isl_map *isl_map_lex_le(__isl_take isl_dim *set_dim)
3341 return map_lex_lte(isl_dim_map(set_dim), 1);
3344 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_dim *dims,
3345 unsigned n, int equal)
3347 struct isl_map *map;
3348 int i;
3350 if (n == 0 && equal)
3351 return isl_map_universe(dims);
3353 map = isl_map_alloc_dim(isl_dim_copy(dims), n, ISL_MAP_DISJOINT);
3355 for (i = 0; i + 1 < n; ++i)
3356 map = isl_map_add_basic_map(map,
3357 isl_basic_map_more_at(isl_dim_copy(dims), i));
3358 if (n > 0) {
3359 if (equal)
3360 map = isl_map_add_basic_map(map,
3361 isl_basic_map_more_or_equal_at(dims, n - 1));
3362 else
3363 map = isl_map_add_basic_map(map,
3364 isl_basic_map_more_at(dims, n - 1));
3365 } else
3366 isl_dim_free(dims);
3368 return map;
3371 static __isl_give isl_map *map_lex_gte(__isl_take isl_dim *dims, int equal)
3373 if (!dims)
3374 return NULL;
3375 return map_lex_gte_first(dims, dims->n_out, equal);
3378 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_dim *dim, unsigned n)
3380 return map_lex_gte_first(dim, n, 0);
3383 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_dim *dim, unsigned n)
3385 return map_lex_gte_first(dim, n, 1);
3388 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_dim *set_dim)
3390 return map_lex_gte(isl_dim_map(set_dim), 0);
3393 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_dim *set_dim)
3395 return map_lex_gte(isl_dim_map(set_dim), 1);
3398 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
3399 __isl_take isl_set *set2)
3401 isl_map *map;
3402 map = isl_map_lex_le(isl_set_get_dim(set1));
3403 map = isl_map_intersect_domain(map, set1);
3404 map = isl_map_intersect_range(map, set2);
3405 return map;
3408 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
3409 __isl_take isl_set *set2)
3411 isl_map *map;
3412 map = isl_map_lex_lt(isl_set_get_dim(set1));
3413 map = isl_map_intersect_domain(map, set1);
3414 map = isl_map_intersect_range(map, set2);
3415 return map;
3418 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
3419 __isl_take isl_set *set2)
3421 isl_map *map;
3422 map = isl_map_lex_ge(isl_set_get_dim(set1));
3423 map = isl_map_intersect_domain(map, set1);
3424 map = isl_map_intersect_range(map, set2);
3425 return map;
3428 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
3429 __isl_take isl_set *set2)
3431 isl_map *map;
3432 map = isl_map_lex_gt(isl_set_get_dim(set1));
3433 map = isl_map_intersect_domain(map, set1);
3434 map = isl_map_intersect_range(map, set2);
3435 return map;
3438 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
3439 __isl_take isl_map *map2)
3441 isl_map *map;
3442 map = isl_map_lex_le(isl_dim_range(isl_map_get_dim(map1)));
3443 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3444 map = isl_map_apply_range(map, isl_map_reverse(map2));
3445 return map;
3448 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
3449 __isl_take isl_map *map2)
3451 isl_map *map;
3452 map = isl_map_lex_lt(isl_dim_range(isl_map_get_dim(map1)));
3453 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3454 map = isl_map_apply_range(map, isl_map_reverse(map2));
3455 return map;
3458 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
3459 __isl_take isl_map *map2)
3461 isl_map *map;
3462 map = isl_map_lex_ge(isl_dim_range(isl_map_get_dim(map1)));
3463 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3464 map = isl_map_apply_range(map, isl_map_reverse(map2));
3465 return map;
3468 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
3469 __isl_take isl_map *map2)
3471 isl_map *map;
3472 map = isl_map_lex_gt(isl_dim_range(isl_map_get_dim(map1)));
3473 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3474 map = isl_map_apply_range(map, isl_map_reverse(map2));
3475 return map;
3478 struct isl_basic_map *isl_basic_map_from_basic_set(
3479 struct isl_basic_set *bset, struct isl_dim *dim)
3481 struct isl_basic_map *bmap;
3483 bset = isl_basic_set_cow(bset);
3484 if (!bset || !dim)
3485 goto error;
3487 isl_assert(bset->ctx, isl_dim_compatible(bset->dim, dim), goto error);
3488 isl_dim_free(bset->dim);
3489 bmap = (struct isl_basic_map *) bset;
3490 bmap->dim = dim;
3491 return isl_basic_map_finalize(bmap);
3492 error:
3493 isl_basic_set_free(bset);
3494 isl_dim_free(dim);
3495 return NULL;
3498 struct isl_basic_set *isl_basic_set_from_basic_map(struct isl_basic_map *bmap)
3500 if (!bmap)
3501 goto error;
3502 if (bmap->dim->n_in == 0)
3503 return (struct isl_basic_set *)bmap;
3504 bmap = isl_basic_map_cow(bmap);
3505 if (!bmap)
3506 goto error;
3507 bmap->dim = isl_dim_as_set_dim(bmap->dim);
3508 if (!bmap->dim)
3509 goto error;
3510 bmap = isl_basic_map_finalize(bmap);
3511 return (struct isl_basic_set *)bmap;
3512 error:
3513 isl_basic_map_free(bmap);
3514 return NULL;
3517 /* For a div d = floor(f/m), add the constraints
3519 * f - m d >= 0
3520 * -(f-(n-1)) + m d >= 0
3522 * Note that the second constraint is the negation of
3524 * f - m d >= n
3526 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
3527 unsigned pos, isl_int *div)
3529 int i, j;
3530 unsigned total = isl_basic_map_total_dim(bmap);
3532 i = isl_basic_map_alloc_inequality(bmap);
3533 if (i < 0)
3534 return -1;
3535 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
3536 isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
3538 j = isl_basic_map_alloc_inequality(bmap);
3539 if (j < 0)
3540 return -1;
3541 isl_seq_neg(bmap->ineq[j], bmap->ineq[i], 1 + total);
3542 isl_int_add(bmap->ineq[j][0], bmap->ineq[j][0], bmap->ineq[j][1 + pos]);
3543 isl_int_sub_ui(bmap->ineq[j][0], bmap->ineq[j][0], 1);
3544 return j;
3547 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
3548 unsigned pos, isl_int *div)
3550 return isl_basic_map_add_div_constraints_var((isl_basic_map *)bset,
3551 pos, div);
3554 int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
3556 unsigned total = isl_basic_map_total_dim(bmap);
3557 unsigned div_pos = total - bmap->n_div + div;
3559 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
3560 bmap->div[div]);
3563 struct isl_basic_set *isl_basic_map_underlying_set(
3564 struct isl_basic_map *bmap)
3566 if (!bmap)
3567 goto error;
3568 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
3569 bmap->n_div == 0 &&
3570 !isl_dim_is_named_or_nested(bmap->dim, isl_dim_in) &&
3571 !isl_dim_is_named_or_nested(bmap->dim, isl_dim_out))
3572 return (struct isl_basic_set *)bmap;
3573 bmap = isl_basic_map_cow(bmap);
3574 if (!bmap)
3575 goto error;
3576 bmap->dim = isl_dim_underlying(bmap->dim, bmap->n_div);
3577 if (!bmap->dim)
3578 goto error;
3579 bmap->extra -= bmap->n_div;
3580 bmap->n_div = 0;
3581 bmap = isl_basic_map_finalize(bmap);
3582 return (struct isl_basic_set *)bmap;
3583 error:
3584 return NULL;
3587 __isl_give isl_basic_set *isl_basic_set_underlying_set(
3588 __isl_take isl_basic_set *bset)
3590 return isl_basic_map_underlying_set((isl_basic_map *)bset);
3593 struct isl_basic_map *isl_basic_map_overlying_set(
3594 struct isl_basic_set *bset, struct isl_basic_map *like)
3596 struct isl_basic_map *bmap;
3597 struct isl_ctx *ctx;
3598 unsigned total;
3599 int i;
3601 if (!bset || !like)
3602 goto error;
3603 ctx = bset->ctx;
3604 isl_assert(ctx, bset->n_div == 0, goto error);
3605 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
3606 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
3607 goto error);
3608 if (isl_dim_equal(bset->dim, like->dim) && like->n_div == 0) {
3609 isl_basic_map_free(like);
3610 return (struct isl_basic_map *)bset;
3612 bset = isl_basic_set_cow(bset);
3613 if (!bset)
3614 goto error;
3615 total = bset->dim->n_out + bset->extra;
3616 bmap = (struct isl_basic_map *)bset;
3617 isl_dim_free(bmap->dim);
3618 bmap->dim = isl_dim_copy(like->dim);
3619 if (!bmap->dim)
3620 goto error;
3621 bmap->n_div = like->n_div;
3622 bmap->extra += like->n_div;
3623 if (bmap->extra) {
3624 unsigned ltotal;
3625 ltotal = total - bmap->extra + like->extra;
3626 if (ltotal > total)
3627 ltotal = total;
3628 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
3629 bmap->extra * (1 + 1 + total));
3630 if (isl_blk_is_error(bmap->block2))
3631 goto error;
3632 bmap->div = isl_realloc_array(ctx, bmap->div, isl_int *,
3633 bmap->extra);
3634 if (!bmap->div)
3635 goto error;
3636 for (i = 0; i < bmap->extra; ++i)
3637 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
3638 for (i = 0; i < like->n_div; ++i) {
3639 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
3640 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
3642 bmap = isl_basic_map_extend_constraints(bmap,
3643 0, 2 * like->n_div);
3644 for (i = 0; i < like->n_div; ++i) {
3645 if (isl_int_is_zero(bmap->div[i][0]))
3646 continue;
3647 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
3648 goto error;
3651 isl_basic_map_free(like);
3652 bmap = isl_basic_map_simplify(bmap);
3653 bmap = isl_basic_map_finalize(bmap);
3654 return bmap;
3655 error:
3656 isl_basic_map_free(like);
3657 isl_basic_set_free(bset);
3658 return NULL;
3661 struct isl_basic_set *isl_basic_set_from_underlying_set(
3662 struct isl_basic_set *bset, struct isl_basic_set *like)
3664 return (struct isl_basic_set *)
3665 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
3668 struct isl_set *isl_set_from_underlying_set(
3669 struct isl_set *set, struct isl_basic_set *like)
3671 int i;
3673 if (!set || !like)
3674 goto error;
3675 isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
3676 goto error);
3677 if (isl_dim_equal(set->dim, like->dim) && like->n_div == 0) {
3678 isl_basic_set_free(like);
3679 return set;
3681 set = isl_set_cow(set);
3682 if (!set)
3683 goto error;
3684 for (i = 0; i < set->n; ++i) {
3685 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
3686 isl_basic_set_copy(like));
3687 if (!set->p[i])
3688 goto error;
3690 isl_dim_free(set->dim);
3691 set->dim = isl_dim_copy(like->dim);
3692 if (!set->dim)
3693 goto error;
3694 isl_basic_set_free(like);
3695 return set;
3696 error:
3697 isl_basic_set_free(like);
3698 isl_set_free(set);
3699 return NULL;
3702 struct isl_set *isl_map_underlying_set(struct isl_map *map)
3704 int i;
3706 map = isl_map_cow(map);
3707 if (!map)
3708 return NULL;
3709 map->dim = isl_dim_cow(map->dim);
3710 if (!map->dim)
3711 goto error;
3713 for (i = 1; i < map->n; ++i)
3714 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
3715 goto error);
3716 for (i = 0; i < map->n; ++i) {
3717 map->p[i] = (struct isl_basic_map *)
3718 isl_basic_map_underlying_set(map->p[i]);
3719 if (!map->p[i])
3720 goto error;
3722 if (map->n == 0)
3723 map->dim = isl_dim_underlying(map->dim, 0);
3724 else {
3725 isl_dim_free(map->dim);
3726 map->dim = isl_dim_copy(map->p[0]->dim);
3728 if (!map->dim)
3729 goto error;
3730 return (struct isl_set *)map;
3731 error:
3732 isl_map_free(map);
3733 return NULL;
3736 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
3738 return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
3741 __isl_give isl_basic_map *isl_basic_map_reset_dim(
3742 __isl_take isl_basic_map *bmap, __isl_take isl_dim *dim)
3744 bmap = isl_basic_map_cow(bmap);
3745 if (!bmap || !dim)
3746 goto error;
3748 isl_dim_free(bmap->dim);
3749 bmap->dim = dim;
3751 bmap = isl_basic_map_finalize(bmap);
3753 return bmap;
3754 error:
3755 isl_basic_map_free(bmap);
3756 isl_dim_free(dim);
3757 return NULL;
3760 static __isl_give isl_basic_set *isl_basic_set_reset_dim(
3761 __isl_take isl_basic_set *bset, __isl_take isl_dim *dim)
3763 return (isl_basic_set *)isl_basic_map_reset_dim((isl_basic_map *)bset,
3764 dim);
3767 __isl_give isl_map *isl_map_reset_dim(__isl_take isl_map *map,
3768 __isl_take isl_dim *dim)
3770 int i;
3772 map = isl_map_cow(map);
3773 if (!map || !dim)
3774 goto error;
3776 for (i = 0; i < map->n; ++i) {
3777 map->p[i] = isl_basic_map_reset_dim(map->p[i],
3778 isl_dim_copy(dim));
3779 if (!map->p[i])
3780 goto error;
3782 isl_dim_free(map->dim);
3783 map->dim = dim;
3785 return map;
3786 error:
3787 isl_map_free(map);
3788 isl_dim_free(dim);
3789 return NULL;
3792 __isl_give isl_set *isl_set_reset_dim(__isl_take isl_set *set,
3793 __isl_take isl_dim *dim)
3795 return (struct isl_set *) isl_map_reset_dim((struct isl_map *)set, dim);
3798 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
3800 isl_dim *dim;
3801 struct isl_basic_set *domain;
3802 unsigned n_in;
3803 unsigned n_out;
3805 if (!bmap)
3806 return NULL;
3807 dim = isl_dim_domain(isl_basic_map_get_dim(bmap));
3809 n_in = isl_basic_map_n_in(bmap);
3810 n_out = isl_basic_map_n_out(bmap);
3811 domain = isl_basic_set_from_basic_map(bmap);
3812 domain = isl_basic_set_project_out(domain, isl_dim_set, n_in, n_out);
3814 domain = isl_basic_set_reset_dim(domain, dim);
3816 return domain;
3819 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
3821 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
3824 __isl_give isl_basic_map *isl_basic_map_domain_map(
3825 __isl_take isl_basic_map *bmap)
3827 int i, k;
3828 isl_dim *dim;
3829 isl_basic_map *domain;
3830 isl_basic_set *bset;
3831 int nparam, n_in, n_out;
3832 unsigned total;
3834 nparam = isl_basic_map_dim(bmap, isl_dim_param);
3835 n_in = isl_basic_map_dim(bmap, isl_dim_in);
3836 n_out = isl_basic_map_dim(bmap, isl_dim_out);
3838 dim = isl_dim_from_range(isl_dim_domain(isl_basic_map_get_dim(bmap)));
3839 domain = isl_basic_map_universe(dim);
3841 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
3842 bmap = isl_basic_map_apply_range(bmap, domain);
3843 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
3845 total = isl_basic_map_total_dim(bmap);
3847 for (i = 0; i < n_in; ++i) {
3848 k = isl_basic_map_alloc_equality(bmap);
3849 if (k < 0)
3850 goto error;
3851 isl_seq_clr(bmap->eq[k], 1 + total);
3852 isl_int_set_si(bmap->eq[k][1 + nparam + i], -1);
3853 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
3856 bmap = isl_basic_map_gauss(bmap, NULL);
3857 return isl_basic_map_finalize(bmap);
3858 error:
3859 isl_basic_map_free(bmap);
3860 return NULL;
3863 __isl_give isl_basic_map *isl_basic_map_range_map(
3864 __isl_take isl_basic_map *bmap)
3866 int i, k;
3867 isl_dim *dim;
3868 isl_basic_map *range;
3869 isl_basic_set *bset;
3870 int nparam, n_in, n_out;
3871 unsigned total;
3873 nparam = isl_basic_map_dim(bmap, isl_dim_param);
3874 n_in = isl_basic_map_dim(bmap, isl_dim_in);
3875 n_out = isl_basic_map_dim(bmap, isl_dim_out);
3877 dim = isl_dim_from_range(isl_dim_range(isl_basic_map_get_dim(bmap)));
3878 range = isl_basic_map_universe(dim);
3880 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
3881 bmap = isl_basic_map_apply_range(bmap, range);
3882 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
3884 total = isl_basic_map_total_dim(bmap);
3886 for (i = 0; i < n_out; ++i) {
3887 k = isl_basic_map_alloc_equality(bmap);
3888 if (k < 0)
3889 goto error;
3890 isl_seq_clr(bmap->eq[k], 1 + total);
3891 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + i], -1);
3892 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
3895 bmap = isl_basic_map_gauss(bmap, NULL);
3896 return isl_basic_map_finalize(bmap);
3897 error:
3898 isl_basic_map_free(bmap);
3899 return NULL;
3902 struct isl_set *isl_map_range(struct isl_map *map)
3904 int i;
3905 struct isl_set *set;
3907 if (!map)
3908 goto error;
3909 if (isl_map_dim(map, isl_dim_in) == 0)
3910 return (isl_set *)map;
3912 map = isl_map_cow(map);
3913 if (!map)
3914 goto error;
3916 set = (struct isl_set *) map;
3917 set->dim = isl_dim_drop_inputs(set->dim, 0, set->dim->n_in);
3918 if (!set->dim)
3919 goto error;
3920 for (i = 0; i < map->n; ++i) {
3921 set->p[i] = isl_basic_map_range(map->p[i]);
3922 if (!set->p[i])
3923 goto error;
3925 ISL_F_CLR(set, ISL_MAP_DISJOINT);
3926 ISL_F_CLR(set, ISL_SET_NORMALIZED);
3927 return set;
3928 error:
3929 isl_map_free(map);
3930 return NULL;
3933 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
3935 int i;
3936 isl_dim *domain_dim;
3938 map = isl_map_cow(map);
3939 if (!map)
3940 return NULL;
3942 domain_dim = isl_dim_from_range(isl_dim_domain(isl_map_get_dim(map)));
3943 map->dim = isl_dim_from_domain(isl_dim_wrap(map->dim));
3944 map->dim = isl_dim_join(map->dim, domain_dim);
3945 if (!map->dim)
3946 goto error;
3947 for (i = 0; i < map->n; ++i) {
3948 map->p[i] = isl_basic_map_domain_map(map->p[i]);
3949 if (!map->p[i])
3950 goto error;
3952 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3953 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3954 return map;
3955 error:
3956 isl_map_free(map);
3957 return NULL;
3960 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
3962 int i;
3963 isl_dim *range_dim;
3965 map = isl_map_cow(map);
3966 if (!map)
3967 return NULL;
3969 range_dim = isl_dim_range(isl_map_get_dim(map));
3970 map->dim = isl_dim_from_domain(isl_dim_wrap(map->dim));
3971 map->dim = isl_dim_join(map->dim, range_dim);
3972 if (!map->dim)
3973 goto error;
3974 for (i = 0; i < map->n; ++i) {
3975 map->p[i] = isl_basic_map_range_map(map->p[i]);
3976 if (!map->p[i])
3977 goto error;
3979 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3980 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3981 return map;
3982 error:
3983 isl_map_free(map);
3984 return NULL;
3987 struct isl_map *isl_map_from_set(struct isl_set *set, struct isl_dim *dim)
3989 int i;
3990 struct isl_map *map = NULL;
3992 set = isl_set_cow(set);
3993 if (!set || !dim)
3994 goto error;
3995 isl_assert(set->ctx, isl_dim_compatible(set->dim, dim), goto error);
3996 map = (struct isl_map *)set;
3997 for (i = 0; i < set->n; ++i) {
3998 map->p[i] = isl_basic_map_from_basic_set(
3999 set->p[i], isl_dim_copy(dim));
4000 if (!map->p[i])
4001 goto error;
4003 isl_dim_free(map->dim);
4004 map->dim = dim;
4005 return map;
4006 error:
4007 isl_dim_free(dim);
4008 isl_set_free(set);
4009 return NULL;
4012 __isl_give isl_basic_map *isl_basic_map_from_domain(
4013 __isl_take isl_basic_set *bset)
4015 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
4018 __isl_give isl_basic_map *isl_basic_map_from_range(
4019 __isl_take isl_basic_set *bset)
4021 return (isl_basic_map *)bset;
4024 struct isl_map *isl_map_from_range(struct isl_set *set)
4026 return (struct isl_map *)set;
4029 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
4031 return isl_map_reverse(isl_map_from_range(set));
4034 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
4035 __isl_take isl_set *range)
4037 return isl_map_apply_range(isl_map_from_domain(domain),
4038 isl_map_from_range(range));
4041 struct isl_set *isl_set_from_map(struct isl_map *map)
4043 int i;
4044 struct isl_set *set = NULL;
4046 if (!map)
4047 return NULL;
4048 map = isl_map_cow(map);
4049 if (!map)
4050 return NULL;
4051 map->dim = isl_dim_as_set_dim(map->dim);
4052 if (!map->dim)
4053 goto error;
4054 set = (struct isl_set *)map;
4055 for (i = 0; i < map->n; ++i) {
4056 set->p[i] = isl_basic_set_from_basic_map(map->p[i]);
4057 if (!set->p[i])
4058 goto error;
4060 return set;
4061 error:
4062 isl_map_free(map);
4063 return NULL;
4066 struct isl_map *isl_map_alloc_dim(struct isl_dim *dim, int n, unsigned flags)
4068 struct isl_map *map;
4070 if (!dim)
4071 return NULL;
4072 isl_assert(dim->ctx, n >= 0, return NULL);
4073 map = isl_alloc(dim->ctx, struct isl_map,
4074 sizeof(struct isl_map) +
4075 (n - 1) * sizeof(struct isl_basic_map *));
4076 if (!map)
4077 goto error;
4079 map->ctx = dim->ctx;
4080 isl_ctx_ref(map->ctx);
4081 map->ref = 1;
4082 map->size = n;
4083 map->n = 0;
4084 map->dim = dim;
4085 map->flags = flags;
4086 return map;
4087 error:
4088 isl_dim_free(dim);
4089 return NULL;
4092 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
4093 unsigned nparam, unsigned in, unsigned out, int n,
4094 unsigned flags)
4096 struct isl_map *map;
4097 struct isl_dim *dims;
4099 dims = isl_dim_alloc(ctx, nparam, in, out);
4100 if (!dims)
4101 return NULL;
4103 map = isl_map_alloc_dim(dims, n, flags);
4104 return map;
4107 struct isl_basic_map *isl_basic_map_empty(struct isl_dim *dim)
4109 struct isl_basic_map *bmap;
4110 bmap = isl_basic_map_alloc_dim(dim, 0, 1, 0);
4111 bmap = isl_basic_map_set_to_empty(bmap);
4112 return bmap;
4115 struct isl_basic_set *isl_basic_set_empty(struct isl_dim *dim)
4117 struct isl_basic_set *bset;
4118 bset = isl_basic_set_alloc_dim(dim, 0, 1, 0);
4119 bset = isl_basic_set_set_to_empty(bset);
4120 return bset;
4123 struct isl_basic_map *isl_basic_map_empty_like(struct isl_basic_map *model)
4125 struct isl_basic_map *bmap;
4126 if (!model)
4127 return NULL;
4128 bmap = isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
4129 bmap = isl_basic_map_set_to_empty(bmap);
4130 return bmap;
4133 struct isl_basic_map *isl_basic_map_empty_like_map(struct isl_map *model)
4135 struct isl_basic_map *bmap;
4136 if (!model)
4137 return NULL;
4138 bmap = isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
4139 bmap = isl_basic_map_set_to_empty(bmap);
4140 return bmap;
4143 struct isl_basic_set *isl_basic_set_empty_like(struct isl_basic_set *model)
4145 struct isl_basic_set *bset;
4146 if (!model)
4147 return NULL;
4148 bset = isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
4149 bset = isl_basic_set_set_to_empty(bset);
4150 return bset;
4153 struct isl_basic_map *isl_basic_map_universe(struct isl_dim *dim)
4155 struct isl_basic_map *bmap;
4156 bmap = isl_basic_map_alloc_dim(dim, 0, 0, 0);
4157 bmap = isl_basic_map_finalize(bmap);
4158 return bmap;
4161 struct isl_basic_set *isl_basic_set_universe(struct isl_dim *dim)
4163 struct isl_basic_set *bset;
4164 bset = isl_basic_set_alloc_dim(dim, 0, 0, 0);
4165 bset = isl_basic_set_finalize(bset);
4166 return bset;
4169 __isl_give isl_basic_map *isl_basic_map_universe_like(
4170 __isl_keep isl_basic_map *model)
4172 if (!model)
4173 return NULL;
4174 return isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
4177 struct isl_basic_set *isl_basic_set_universe_like(struct isl_basic_set *model)
4179 if (!model)
4180 return NULL;
4181 return isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
4184 __isl_give isl_basic_set *isl_basic_set_universe_like_set(
4185 __isl_keep isl_set *model)
4187 if (!model)
4188 return NULL;
4189 return isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
4192 struct isl_map *isl_map_empty(struct isl_dim *dim)
4194 return isl_map_alloc_dim(dim, 0, ISL_MAP_DISJOINT);
4197 struct isl_map *isl_map_empty_like(struct isl_map *model)
4199 if (!model)
4200 return NULL;
4201 return isl_map_alloc_dim(isl_dim_copy(model->dim), 0, ISL_MAP_DISJOINT);
4204 struct isl_map *isl_map_empty_like_basic_map(struct isl_basic_map *model)
4206 if (!model)
4207 return NULL;
4208 return isl_map_alloc_dim(isl_dim_copy(model->dim), 0, ISL_MAP_DISJOINT);
4211 struct isl_set *isl_set_empty(struct isl_dim *dim)
4213 return isl_set_alloc_dim(dim, 0, ISL_MAP_DISJOINT);
4216 struct isl_set *isl_set_empty_like(struct isl_set *model)
4218 if (!model)
4219 return NULL;
4220 return isl_set_empty(isl_dim_copy(model->dim));
4223 struct isl_map *isl_map_universe(struct isl_dim *dim)
4225 struct isl_map *map;
4226 if (!dim)
4227 return NULL;
4228 map = isl_map_alloc_dim(isl_dim_copy(dim), 1, ISL_MAP_DISJOINT);
4229 map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
4230 return map;
4233 struct isl_set *isl_set_universe(struct isl_dim *dim)
4235 struct isl_set *set;
4236 if (!dim)
4237 return NULL;
4238 set = isl_set_alloc_dim(isl_dim_copy(dim), 1, ISL_MAP_DISJOINT);
4239 set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
4240 return set;
4243 __isl_give isl_set *isl_set_universe_like(__isl_keep isl_set *model)
4245 if (!model)
4246 return NULL;
4247 return isl_set_universe(isl_dim_copy(model->dim));
4250 struct isl_map *isl_map_dup(struct isl_map *map)
4252 int i;
4253 struct isl_map *dup;
4255 if (!map)
4256 return NULL;
4257 dup = isl_map_alloc_dim(isl_dim_copy(map->dim), map->n, map->flags);
4258 for (i = 0; i < map->n; ++i)
4259 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
4260 return dup;
4263 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
4264 __isl_take isl_basic_map *bmap)
4266 if (!bmap || !map)
4267 goto error;
4268 if (isl_basic_map_fast_is_empty(bmap)) {
4269 isl_basic_map_free(bmap);
4270 return map;
4272 isl_assert(map->ctx, isl_dim_equal(map->dim, bmap->dim), goto error);
4273 isl_assert(map->ctx, map->n < map->size, goto error);
4274 map->p[map->n] = bmap;
4275 map->n++;
4276 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4277 return map;
4278 error:
4279 if (map)
4280 isl_map_free(map);
4281 if (bmap)
4282 isl_basic_map_free(bmap);
4283 return NULL;
4286 void isl_map_free(struct isl_map *map)
4288 int i;
4290 if (!map)
4291 return;
4293 if (--map->ref > 0)
4294 return;
4296 isl_ctx_deref(map->ctx);
4297 for (i = 0; i < map->n; ++i)
4298 isl_basic_map_free(map->p[i]);
4299 isl_dim_free(map->dim);
4300 free(map);
4303 struct isl_map *isl_map_extend(struct isl_map *base,
4304 unsigned nparam, unsigned n_in, unsigned n_out)
4306 int i;
4308 base = isl_map_cow(base);
4309 if (!base)
4310 return NULL;
4312 base->dim = isl_dim_extend(base->dim, nparam, n_in, n_out);
4313 if (!base->dim)
4314 goto error;
4315 for (i = 0; i < base->n; ++i) {
4316 base->p[i] = isl_basic_map_extend_dim(base->p[i],
4317 isl_dim_copy(base->dim), 0, 0, 0);
4318 if (!base->p[i])
4319 goto error;
4321 return base;
4322 error:
4323 isl_map_free(base);
4324 return NULL;
4327 struct isl_set *isl_set_extend(struct isl_set *base,
4328 unsigned nparam, unsigned dim)
4330 return (struct isl_set *)isl_map_extend((struct isl_map *)base,
4331 nparam, 0, dim);
4334 static struct isl_basic_map *isl_basic_map_fix_pos_si(
4335 struct isl_basic_map *bmap, unsigned pos, int value)
4337 int j;
4339 bmap = isl_basic_map_cow(bmap);
4340 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
4341 j = isl_basic_map_alloc_equality(bmap);
4342 if (j < 0)
4343 goto error;
4344 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
4345 isl_int_set_si(bmap->eq[j][pos], -1);
4346 isl_int_set_si(bmap->eq[j][0], value);
4347 bmap = isl_basic_map_simplify(bmap);
4348 return isl_basic_map_finalize(bmap);
4349 error:
4350 isl_basic_map_free(bmap);
4351 return NULL;
4354 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
4355 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
4357 int j;
4359 bmap = isl_basic_map_cow(bmap);
4360 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
4361 j = isl_basic_map_alloc_equality(bmap);
4362 if (j < 0)
4363 goto error;
4364 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
4365 isl_int_set_si(bmap->eq[j][pos], -1);
4366 isl_int_set(bmap->eq[j][0], value);
4367 bmap = isl_basic_map_simplify(bmap);
4368 return isl_basic_map_finalize(bmap);
4369 error:
4370 isl_basic_map_free(bmap);
4371 return NULL;
4374 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
4375 enum isl_dim_type type, unsigned pos, int value)
4377 if (!bmap)
4378 return NULL;
4379 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
4380 return isl_basic_map_fix_pos_si(bmap,
4381 isl_basic_map_offset(bmap, type) + pos, value);
4382 error:
4383 isl_basic_map_free(bmap);
4384 return NULL;
4387 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
4388 enum isl_dim_type type, unsigned pos, isl_int value)
4390 if (!bmap)
4391 return NULL;
4392 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
4393 return isl_basic_map_fix_pos(bmap,
4394 isl_basic_map_offset(bmap, type) + pos, value);
4395 error:
4396 isl_basic_map_free(bmap);
4397 return NULL;
4400 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
4401 enum isl_dim_type type, unsigned pos, int value)
4403 return (struct isl_basic_set *)
4404 isl_basic_map_fix_si((struct isl_basic_map *)bset,
4405 type, pos, value);
4408 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
4409 enum isl_dim_type type, unsigned pos, isl_int value)
4411 return (struct isl_basic_set *)
4412 isl_basic_map_fix((struct isl_basic_map *)bset,
4413 type, pos, value);
4416 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
4417 unsigned input, int value)
4419 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
4422 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
4423 unsigned dim, int value)
4425 return (struct isl_basic_set *)
4426 isl_basic_map_fix_si((struct isl_basic_map *)bset,
4427 isl_dim_set, dim, value);
4430 struct isl_map *isl_map_fix_si(struct isl_map *map,
4431 enum isl_dim_type type, unsigned pos, int value)
4433 int i;
4435 map = isl_map_cow(map);
4436 if (!map)
4437 return NULL;
4439 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
4440 for (i = 0; i < map->n; ++i) {
4441 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
4442 if (!map->p[i])
4443 goto error;
4445 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4446 return map;
4447 error:
4448 isl_map_free(map);
4449 return NULL;
4452 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
4453 enum isl_dim_type type, unsigned pos, int value)
4455 return (struct isl_set *)
4456 isl_map_fix_si((struct isl_map *)set, type, pos, value);
4459 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
4460 enum isl_dim_type type, unsigned pos, isl_int value)
4462 int i;
4464 map = isl_map_cow(map);
4465 if (!map)
4466 return NULL;
4468 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
4469 for (i = 0; i < map->n; ++i) {
4470 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
4471 if (!map->p[i])
4472 goto error;
4474 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4475 return map;
4476 error:
4477 isl_map_free(map);
4478 return NULL;
4481 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
4482 enum isl_dim_type type, unsigned pos, isl_int value)
4484 return (struct isl_set *)isl_map_fix((isl_map *)set, type, pos, value);
4487 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
4488 unsigned input, int value)
4490 return isl_map_fix_si(map, isl_dim_in, input, value);
4493 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
4495 return (struct isl_set *)
4496 isl_map_fix_si((struct isl_map *)set, isl_dim_set, dim, value);
4499 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
4500 __isl_take isl_basic_map *bmap,
4501 enum isl_dim_type type, unsigned pos, int value)
4503 int j;
4505 if (!bmap)
4506 return NULL;
4507 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
4508 pos += isl_basic_map_offset(bmap, type);
4509 bmap = isl_basic_map_cow(bmap);
4510 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
4511 j = isl_basic_map_alloc_inequality(bmap);
4512 if (j < 0)
4513 goto error;
4514 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
4515 isl_int_set_si(bmap->ineq[j][pos], 1);
4516 isl_int_set_si(bmap->ineq[j][0], -value);
4517 bmap = isl_basic_map_simplify(bmap);
4518 return isl_basic_map_finalize(bmap);
4519 error:
4520 isl_basic_map_free(bmap);
4521 return NULL;
4524 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
4525 unsigned dim, isl_int value)
4527 int j;
4529 bset = isl_basic_set_cow(bset);
4530 bset = isl_basic_set_extend_constraints(bset, 0, 1);
4531 j = isl_basic_set_alloc_inequality(bset);
4532 if (j < 0)
4533 goto error;
4534 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
4535 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
4536 isl_int_neg(bset->ineq[j][0], value);
4537 bset = isl_basic_set_simplify(bset);
4538 return isl_basic_set_finalize(bset);
4539 error:
4540 isl_basic_set_free(bset);
4541 return NULL;
4544 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
4545 enum isl_dim_type type, unsigned pos, int value)
4547 int i;
4549 map = isl_map_cow(map);
4550 if (!map)
4551 return NULL;
4553 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
4554 for (i = 0; i < map->n; ++i) {
4555 map->p[i] = isl_basic_map_lower_bound_si(map->p[i],
4556 type, pos, value);
4557 if (!map->p[i])
4558 goto error;
4560 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4561 return map;
4562 error:
4563 isl_map_free(map);
4564 return NULL;
4567 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
4568 enum isl_dim_type type, unsigned pos, int value)
4570 return (struct isl_set *)
4571 isl_map_lower_bound_si((struct isl_map *)set, type, pos, value);
4574 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
4575 isl_int value)
4577 int i;
4579 set = isl_set_cow(set);
4580 if (!set)
4581 return NULL;
4583 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
4584 for (i = 0; i < set->n; ++i) {
4585 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
4586 if (!set->p[i])
4587 goto error;
4589 return set;
4590 error:
4591 isl_set_free(set);
4592 return NULL;
4595 struct isl_map *isl_map_reverse(struct isl_map *map)
4597 int i;
4599 map = isl_map_cow(map);
4600 if (!map)
4601 return NULL;
4603 map->dim = isl_dim_reverse(map->dim);
4604 if (!map->dim)
4605 goto error;
4606 for (i = 0; i < map->n; ++i) {
4607 map->p[i] = isl_basic_map_reverse(map->p[i]);
4608 if (!map->p[i])
4609 goto error;
4611 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4612 return map;
4613 error:
4614 isl_map_free(map);
4615 return NULL;
4618 static struct isl_map *isl_basic_map_partial_lexopt(
4619 struct isl_basic_map *bmap, struct isl_basic_set *dom,
4620 struct isl_set **empty, int max)
4622 if (!bmap)
4623 goto error;
4624 if (bmap->ctx->opt->pip == ISL_PIP_PIP)
4625 return isl_pip_basic_map_lexopt(bmap, dom, empty, max);
4626 else
4627 return isl_tab_basic_map_partial_lexopt(bmap, dom, empty, max);
4628 error:
4629 isl_basic_map_free(bmap);
4630 isl_basic_set_free(dom);
4631 if (empty)
4632 *empty = NULL;
4633 return NULL;
4636 struct isl_map *isl_basic_map_partial_lexmax(
4637 struct isl_basic_map *bmap, struct isl_basic_set *dom,
4638 struct isl_set **empty)
4640 return isl_basic_map_partial_lexopt(bmap, dom, empty, 1);
4643 struct isl_map *isl_basic_map_partial_lexmin(
4644 struct isl_basic_map *bmap, struct isl_basic_set *dom,
4645 struct isl_set **empty)
4647 return isl_basic_map_partial_lexopt(bmap, dom, empty, 0);
4650 struct isl_set *isl_basic_set_partial_lexmin(
4651 struct isl_basic_set *bset, struct isl_basic_set *dom,
4652 struct isl_set **empty)
4654 return (struct isl_set *)
4655 isl_basic_map_partial_lexmin((struct isl_basic_map *)bset,
4656 dom, empty);
4659 struct isl_set *isl_basic_set_partial_lexmax(
4660 struct isl_basic_set *bset, struct isl_basic_set *dom,
4661 struct isl_set **empty)
4663 return (struct isl_set *)
4664 isl_basic_map_partial_lexmax((struct isl_basic_map *)bset,
4665 dom, empty);
4668 /* Given a basic map "bmap", compute the lexicographically minimal
4669 * (or maximal) image element for each domain element in dom.
4670 * Set *empty to those elements in dom that do not have an image element.
4672 * We first make sure the basic sets in dom are disjoint and then
4673 * simply collect the results over each of the basic sets separately.
4674 * We could probably improve the efficiency a bit by moving the union
4675 * domain down into the parametric integer programming.
4677 static __isl_give isl_map *basic_map_partial_lexopt(
4678 __isl_take isl_basic_map *bmap, __isl_take isl_set *dom,
4679 __isl_give isl_set **empty, int max)
4681 int i;
4682 struct isl_map *res;
4684 dom = isl_set_make_disjoint(dom);
4685 if (!dom)
4686 goto error;
4688 if (isl_set_fast_is_empty(dom)) {
4689 res = isl_map_empty_like_basic_map(bmap);
4690 *empty = isl_set_empty_like(dom);
4691 isl_set_free(dom);
4692 isl_basic_map_free(bmap);
4693 return res;
4696 res = isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap),
4697 isl_basic_set_copy(dom->p[0]), empty, max);
4699 for (i = 1; i < dom->n; ++i) {
4700 struct isl_map *res_i;
4701 struct isl_set *empty_i;
4703 res_i = isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap),
4704 isl_basic_set_copy(dom->p[i]), &empty_i, max);
4706 res = isl_map_union_disjoint(res, res_i);
4707 *empty = isl_set_union_disjoint(*empty, empty_i);
4710 isl_set_free(dom);
4711 isl_basic_map_free(bmap);
4712 return res;
4713 error:
4714 *empty = NULL;
4715 isl_set_free(dom);
4716 isl_basic_map_free(bmap);
4717 return NULL;
4720 /* Given a map "map", compute the lexicographically minimal
4721 * (or maximal) image element for each domain element in dom.
4722 * Set *empty to those elements in dom that do not have an image element.
4724 * We first compute the lexicographically minimal or maximal element
4725 * in the first basic map. This results in a partial solution "res"
4726 * and a subset "todo" of dom that still need to be handled.
4727 * We then consider each of the remaining maps in "map" and successively
4728 * improve both "res" and "todo".
4730 * Let res^k and todo^k be the results after k steps and let i = k + 1.
4731 * Assume we are computing the lexicographical maximum.
4732 * We first compute the lexicographically maximal element in basic map i.
4733 * This results in a partial solution res_i and a subset todo_i.
4734 * Then we combine these results with those obtain for the first k basic maps
4735 * to obtain a result that is valid for the first k+1 basic maps.
4736 * In particular, the set where there is no solution is the set where
4737 * there is no solution for the first k basic maps and also no solution
4738 * for the ith basic map, i.e.,
4740 * todo^i = todo^k * todo_i
4742 * On dom(res^k) * dom(res_i), we need to pick the larger of the two
4743 * solutions, arbitrarily breaking ties in favor of res^k.
4744 * That is, when res^k(a) >= res_i(a), we pick res^k and
4745 * when res^k(a) < res_i(a), we pick res_i. (Here, ">=" and "<" denote
4746 * the lexicographic order.)
4747 * In practice, we compute
4749 * res^k * (res_i . "<=")
4751 * and
4753 * res_i * (res^k . "<")
4755 * Finally, we consider the symmetric difference of dom(res^k) and dom(res_i),
4756 * where only one of res^k and res_i provides a solution and we simply pick
4757 * that one, i.e.,
4759 * res^k * todo_i
4760 * and
4761 * res_i * todo^k
4763 * Note that we only compute these intersections when dom(res^k) intersects
4764 * dom(res_i). Otherwise, the only effect of these intersections is to
4765 * potentially break up res^k and res_i into smaller pieces.
4766 * We want to avoid such splintering as much as possible.
4767 * In fact, an earlier implementation of this function would look for
4768 * better results in the domain of res^k and for extra results in todo^k,
4769 * but this would always result in a splintering according to todo^k,
4770 * even when the domain of basic map i is disjoint from the domains of
4771 * the previous basic maps.
4773 static __isl_give isl_map *isl_map_partial_lexopt(
4774 __isl_take isl_map *map, __isl_take isl_set *dom,
4775 __isl_give isl_set **empty, int max)
4777 int i;
4778 struct isl_map *res;
4779 struct isl_set *todo;
4781 if (!map || !dom)
4782 goto error;
4784 if (isl_map_fast_is_empty(map)) {
4785 if (empty)
4786 *empty = dom;
4787 else
4788 isl_set_free(dom);
4789 return map;
4792 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
4793 isl_set_copy(dom), &todo, max);
4795 for (i = 1; i < map->n; ++i) {
4796 isl_map *lt, *le;
4797 isl_map *res_i;
4798 isl_set *todo_i;
4799 isl_dim *dim = isl_dim_range(isl_map_get_dim(res));
4801 res_i = basic_map_partial_lexopt(isl_basic_map_copy(map->p[i]),
4802 isl_set_copy(dom), &todo_i, max);
4804 if (max) {
4805 lt = isl_map_lex_lt(isl_dim_copy(dim));
4806 le = isl_map_lex_le(dim);
4807 } else {
4808 lt = isl_map_lex_gt(isl_dim_copy(dim));
4809 le = isl_map_lex_ge(dim);
4811 lt = isl_map_apply_range(isl_map_copy(res), lt);
4812 lt = isl_map_intersect(lt, isl_map_copy(res_i));
4813 le = isl_map_apply_range(isl_map_copy(res_i), le);
4814 le = isl_map_intersect(le, isl_map_copy(res));
4816 if (!isl_map_is_empty(lt) || !isl_map_is_empty(le)) {
4817 res = isl_map_intersect_domain(res,
4818 isl_set_copy(todo_i));
4819 res_i = isl_map_intersect_domain(res_i,
4820 isl_set_copy(todo));
4823 res = isl_map_union_disjoint(res, res_i);
4824 res = isl_map_union_disjoint(res, lt);
4825 res = isl_map_union_disjoint(res, le);
4827 todo = isl_set_intersect(todo, todo_i);
4830 isl_set_free(dom);
4831 isl_map_free(map);
4833 if (empty)
4834 *empty = todo;
4835 else
4836 isl_set_free(todo);
4838 return res;
4839 error:
4840 if (empty)
4841 *empty = NULL;
4842 isl_set_free(dom);
4843 isl_map_free(map);
4844 return NULL;
4847 __isl_give isl_map *isl_map_partial_lexmax(
4848 __isl_take isl_map *map, __isl_take isl_set *dom,
4849 __isl_give isl_set **empty)
4851 return isl_map_partial_lexopt(map, dom, empty, 1);
4854 __isl_give isl_map *isl_map_partial_lexmin(
4855 __isl_take isl_map *map, __isl_take isl_set *dom,
4856 __isl_give isl_set **empty)
4858 return isl_map_partial_lexopt(map, dom, empty, 0);
4861 __isl_give isl_set *isl_set_partial_lexmin(
4862 __isl_take isl_set *set, __isl_take isl_set *dom,
4863 __isl_give isl_set **empty)
4865 return (struct isl_set *)
4866 isl_map_partial_lexmin((struct isl_map *)set,
4867 dom, empty);
4870 __isl_give isl_set *isl_set_partial_lexmax(
4871 __isl_take isl_set *set, __isl_take isl_set *dom,
4872 __isl_give isl_set **empty)
4874 return (struct isl_set *)
4875 isl_map_partial_lexmax((struct isl_map *)set,
4876 dom, empty);
4879 __isl_give isl_map *isl_basic_map_lexopt(__isl_take isl_basic_map *bmap, int max)
4881 struct isl_basic_set *dom = NULL;
4882 struct isl_dim *dom_dim;
4884 if (!bmap)
4885 goto error;
4886 dom_dim = isl_dim_domain(isl_dim_copy(bmap->dim));
4887 dom = isl_basic_set_universe(dom_dim);
4888 return isl_basic_map_partial_lexopt(bmap, dom, NULL, max);
4889 error:
4890 isl_basic_map_free(bmap);
4891 return NULL;
4894 __isl_give isl_map *isl_basic_map_lexmin(__isl_take isl_basic_map *bmap)
4896 return isl_basic_map_lexopt(bmap, 0);
4899 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
4901 return isl_basic_map_lexopt(bmap, 1);
4904 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
4906 return (isl_set *)isl_basic_map_lexmin((isl_basic_map *)bset);
4909 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
4911 return (isl_set *)isl_basic_map_lexmax((isl_basic_map *)bset);
4914 __isl_give isl_map *isl_map_lexopt(__isl_take isl_map *map, int max)
4916 struct isl_set *dom = NULL;
4917 struct isl_dim *dom_dim;
4919 if (!map)
4920 goto error;
4921 dom_dim = isl_dim_domain(isl_dim_copy(map->dim));
4922 dom = isl_set_universe(dom_dim);
4923 return isl_map_partial_lexopt(map, dom, NULL, max);
4924 error:
4925 isl_map_free(map);
4926 return NULL;
4929 __isl_give isl_map *isl_map_lexmin(__isl_take isl_map *map)
4931 return isl_map_lexopt(map, 0);
4934 __isl_give isl_map *isl_map_lexmax(__isl_take isl_map *map)
4936 return isl_map_lexopt(map, 1);
4939 __isl_give isl_set *isl_set_lexmin(__isl_take isl_set *set)
4941 return (isl_set *)isl_map_lexmin((isl_map *)set);
4944 __isl_give isl_set *isl_set_lexmax(__isl_take isl_set *set)
4946 return (isl_set *)isl_map_lexmax((isl_map *)set);
4949 /* Apply a preimage specified by "mat" on the parameters of "bset".
4950 * bset is assumed to have only parameters and divs.
4952 static struct isl_basic_set *basic_set_parameter_preimage(
4953 struct isl_basic_set *bset, struct isl_mat *mat)
4955 unsigned nparam;
4957 if (!bset || !mat)
4958 goto error;
4960 bset->dim = isl_dim_cow(bset->dim);
4961 if (!bset->dim)
4962 goto error;
4964 nparam = isl_basic_set_dim(bset, isl_dim_param);
4966 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
4968 bset->dim->nparam = 0;
4969 bset->dim->n_out = nparam;
4970 bset = isl_basic_set_preimage(bset, mat);
4971 if (bset) {
4972 bset->dim->nparam = bset->dim->n_out;
4973 bset->dim->n_out = 0;
4975 return bset;
4976 error:
4977 isl_mat_free(mat);
4978 isl_basic_set_free(bset);
4979 return NULL;
4982 /* Apply a preimage specified by "mat" on the parameters of "set".
4983 * set is assumed to have only parameters and divs.
4985 static struct isl_set *set_parameter_preimage(
4986 struct isl_set *set, struct isl_mat *mat)
4988 struct isl_dim *dim = NULL;
4989 unsigned nparam;
4991 if (!set || !mat)
4992 goto error;
4994 dim = isl_dim_copy(set->dim);
4995 dim = isl_dim_cow(dim);
4996 if (!dim)
4997 goto error;
4999 nparam = isl_set_dim(set, isl_dim_param);
5001 isl_assert(set->ctx, mat->n_row == 1 + nparam, goto error);
5003 dim->nparam = 0;
5004 dim->n_out = nparam;
5005 isl_set_reset_dim(set, dim);
5006 set = isl_set_preimage(set, mat);
5007 if (!set)
5008 goto error2;
5009 dim = isl_dim_copy(set->dim);
5010 dim = isl_dim_cow(dim);
5011 if (!dim)
5012 goto error2;
5013 dim->nparam = dim->n_out;
5014 dim->n_out = 0;
5015 isl_set_reset_dim(set, dim);
5016 return set;
5017 error:
5018 isl_dim_free(dim);
5019 isl_mat_free(mat);
5020 error2:
5021 isl_set_free(set);
5022 return NULL;
5025 /* Intersect the basic set "bset" with the affine space specified by the
5026 * equalities in "eq".
5028 static struct isl_basic_set *basic_set_append_equalities(
5029 struct isl_basic_set *bset, struct isl_mat *eq)
5031 int i, k;
5032 unsigned len;
5034 if (!bset || !eq)
5035 goto error;
5037 bset = isl_basic_set_extend_dim(bset, isl_dim_copy(bset->dim), 0,
5038 eq->n_row, 0);
5039 if (!bset)
5040 goto error;
5042 len = 1 + isl_dim_total(bset->dim) + bset->extra;
5043 for (i = 0; i < eq->n_row; ++i) {
5044 k = isl_basic_set_alloc_equality(bset);
5045 if (k < 0)
5046 goto error;
5047 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
5048 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
5050 isl_mat_free(eq);
5052 bset = isl_basic_set_gauss(bset, NULL);
5053 bset = isl_basic_set_finalize(bset);
5055 return bset;
5056 error:
5057 isl_mat_free(eq);
5058 isl_basic_set_free(bset);
5059 return NULL;
5062 /* Intersect the set "set" with the affine space specified by the
5063 * equalities in "eq".
5065 static struct isl_set *set_append_equalities(struct isl_set *set,
5066 struct isl_mat *eq)
5068 int i;
5070 if (!set || !eq)
5071 goto error;
5073 for (i = 0; i < set->n; ++i) {
5074 set->p[i] = basic_set_append_equalities(set->p[i],
5075 isl_mat_copy(eq));
5076 if (!set->p[i])
5077 goto error;
5079 isl_mat_free(eq);
5080 return set;
5081 error:
5082 isl_mat_free(eq);
5083 isl_set_free(set);
5084 return NULL;
5087 /* Project the given basic set onto its parameter domain, possibly introducing
5088 * new, explicit, existential variables in the constraints.
5089 * The input has parameters and (possibly implicit) existential variables.
5090 * The output has the same parameters, but only
5091 * explicit existentially quantified variables.
5093 * The actual projection is performed by pip, but pip doesn't seem
5094 * to like equalities very much, so we first remove the equalities
5095 * among the parameters by performing a variable compression on
5096 * the parameters. Afterward, an inverse transformation is performed
5097 * and the equalities among the parameters are inserted back in.
5099 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
5101 int i, j;
5102 struct isl_mat *eq;
5103 struct isl_mat *T, *T2;
5104 struct isl_set *set;
5105 unsigned nparam, n_div;
5107 bset = isl_basic_set_cow(bset);
5108 if (!bset)
5109 return NULL;
5111 if (bset->n_eq == 0)
5112 return isl_basic_set_lexmin(bset);
5114 isl_basic_set_gauss(bset, NULL);
5116 nparam = isl_basic_set_dim(bset, isl_dim_param);
5117 n_div = isl_basic_set_dim(bset, isl_dim_div);
5119 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
5120 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
5121 ++i;
5123 if (i == bset->n_eq)
5124 return isl_basic_set_lexmin(bset);
5126 eq = isl_mat_sub_alloc(bset->ctx, bset->eq, i, bset->n_eq - i,
5127 0, 1 + nparam);
5128 eq = isl_mat_cow(eq);
5129 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
5130 if (T && T->n_col == 0) {
5131 isl_mat_free(T);
5132 isl_mat_free(T2);
5133 isl_mat_free(eq);
5134 bset = isl_basic_set_set_to_empty(bset);
5135 return isl_set_from_basic_set(bset);
5137 bset = basic_set_parameter_preimage(bset, T);
5139 set = isl_basic_set_lexmin(bset);
5140 set = set_parameter_preimage(set, T2);
5141 set = set_append_equalities(set, eq);
5142 return set;
5145 /* Compute an explicit representation for all the existentially
5146 * quantified variables.
5147 * The input and output dimensions are first turned into parameters.
5148 * compute_divs then returns a map with the same parameters and
5149 * no input or output dimensions and the dimension specification
5150 * is reset to that of the input.
5152 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
5154 struct isl_basic_set *bset;
5155 struct isl_set *set;
5156 struct isl_map *map;
5157 struct isl_dim *dim, *orig_dim = NULL;
5158 unsigned nparam;
5159 unsigned n_in;
5160 unsigned n_out;
5162 bmap = isl_basic_map_cow(bmap);
5163 if (!bmap)
5164 return NULL;
5166 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5167 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5168 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5169 dim = isl_dim_set_alloc(bmap->ctx, nparam + n_in + n_out, 0);
5170 if (!dim)
5171 goto error;
5173 orig_dim = bmap->dim;
5174 bmap->dim = dim;
5175 bset = (struct isl_basic_set *)bmap;
5177 set = parameter_compute_divs(bset);
5178 map = (struct isl_map *)set;
5179 map = isl_map_reset_dim(map, orig_dim);
5181 return map;
5182 error:
5183 isl_basic_map_free(bmap);
5184 return NULL;
5187 static int basic_map_divs_known(__isl_keep isl_basic_map *bmap)
5189 int i;
5190 unsigned off;
5192 if (!bmap)
5193 return -1;
5195 off = isl_dim_total(bmap->dim);
5196 for (i = 0; i < bmap->n_div; ++i) {
5197 if (isl_int_is_zero(bmap->div[i][0]))
5198 return 0;
5199 isl_assert(bmap->ctx, isl_int_is_zero(bmap->div[i][1+1+off+i]),
5200 return -1);
5202 return 1;
5205 static int map_divs_known(__isl_keep isl_map *map)
5207 int i;
5209 if (!map)
5210 return -1;
5212 for (i = 0; i < map->n; ++i) {
5213 int known = basic_map_divs_known(map->p[i]);
5214 if (known <= 0)
5215 return known;
5218 return 1;
5221 /* If bmap contains any unknown divs, then compute explicit
5222 * expressions for them. However, this computation may be
5223 * quite expensive, so first try to remove divs that aren't
5224 * strictly needed.
5226 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
5228 int i;
5229 int known;
5230 struct isl_map *map;
5232 known = basic_map_divs_known(bmap);
5233 if (known < 0)
5234 goto error;
5235 if (known)
5236 return isl_map_from_basic_map(bmap);
5238 bmap = isl_basic_map_drop_redundant_divs(bmap);
5240 known = basic_map_divs_known(bmap);
5241 if (known < 0)
5242 goto error;
5243 if (known)
5244 return isl_map_from_basic_map(bmap);
5246 map = compute_divs(bmap);
5247 return map;
5248 error:
5249 isl_basic_map_free(bmap);
5250 return NULL;
5253 struct isl_map *isl_map_compute_divs(struct isl_map *map)
5255 int i;
5256 int known;
5257 struct isl_map *res;
5259 if (!map)
5260 return NULL;
5261 if (map->n == 0)
5262 return map;
5264 known = map_divs_known(map);
5265 if (known < 0) {
5266 isl_map_free(map);
5267 return NULL;
5269 if (known)
5270 return map;
5272 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
5273 for (i = 1 ; i < map->n; ++i) {
5274 struct isl_map *r2;
5275 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
5276 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
5277 res = isl_map_union_disjoint(res, r2);
5278 else
5279 res = isl_map_union(res, r2);
5281 isl_map_free(map);
5283 return res;
5286 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
5288 return (struct isl_set *)
5289 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
5292 struct isl_set *isl_set_compute_divs(struct isl_set *set)
5294 return (struct isl_set *)
5295 isl_map_compute_divs((struct isl_map *)set);
5298 struct isl_set *isl_map_domain(struct isl_map *map)
5300 int i;
5301 struct isl_set *set;
5303 if (!map)
5304 goto error;
5306 map = isl_map_cow(map);
5307 if (!map)
5308 return NULL;
5310 set = (struct isl_set *)map;
5311 set->dim = isl_dim_domain(set->dim);
5312 if (!set->dim)
5313 goto error;
5314 for (i = 0; i < map->n; ++i) {
5315 set->p[i] = isl_basic_map_domain(map->p[i]);
5316 if (!set->p[i])
5317 goto error;
5319 ISL_F_CLR(set, ISL_MAP_DISJOINT);
5320 ISL_F_CLR(set, ISL_SET_NORMALIZED);
5321 return set;
5322 error:
5323 isl_map_free(map);
5324 return NULL;
5327 struct isl_map *isl_map_union_disjoint(
5328 struct isl_map *map1, struct isl_map *map2)
5330 int i;
5331 unsigned flags = 0;
5332 struct isl_map *map = NULL;
5334 if (!map1 || !map2)
5335 goto error;
5337 if (map1->n == 0) {
5338 isl_map_free(map1);
5339 return map2;
5341 if (map2->n == 0) {
5342 isl_map_free(map2);
5343 return map1;
5346 isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
5348 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
5349 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
5350 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
5352 map = isl_map_alloc_dim(isl_dim_copy(map1->dim),
5353 map1->n + map2->n, flags);
5354 if (!map)
5355 goto error;
5356 for (i = 0; i < map1->n; ++i) {
5357 map = isl_map_add_basic_map(map,
5358 isl_basic_map_copy(map1->p[i]));
5359 if (!map)
5360 goto error;
5362 for (i = 0; i < map2->n; ++i) {
5363 map = isl_map_add_basic_map(map,
5364 isl_basic_map_copy(map2->p[i]));
5365 if (!map)
5366 goto error;
5368 isl_map_free(map1);
5369 isl_map_free(map2);
5370 return map;
5371 error:
5372 isl_map_free(map);
5373 isl_map_free(map1);
5374 isl_map_free(map2);
5375 return NULL;
5378 struct isl_map *isl_map_union(struct isl_map *map1, struct isl_map *map2)
5380 map1 = isl_map_union_disjoint(map1, map2);
5381 if (!map1)
5382 return NULL;
5383 if (map1->n > 1)
5384 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
5385 return map1;
5388 struct isl_set *isl_set_union_disjoint(
5389 struct isl_set *set1, struct isl_set *set2)
5391 return (struct isl_set *)
5392 isl_map_union_disjoint(
5393 (struct isl_map *)set1, (struct isl_map *)set2);
5396 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
5398 return (struct isl_set *)
5399 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
5402 struct isl_map *isl_map_intersect_range(
5403 struct isl_map *map, struct isl_set *set)
5405 unsigned flags = 0;
5406 struct isl_map *result;
5407 int i, j;
5409 if (!map || !set)
5410 goto error;
5412 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
5413 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
5414 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
5416 result = isl_map_alloc_dim(isl_dim_copy(map->dim),
5417 map->n * set->n, flags);
5418 if (!result)
5419 goto error;
5420 for (i = 0; i < map->n; ++i)
5421 for (j = 0; j < set->n; ++j) {
5422 result = isl_map_add_basic_map(result,
5423 isl_basic_map_intersect_range(
5424 isl_basic_map_copy(map->p[i]),
5425 isl_basic_set_copy(set->p[j])));
5426 if (!result)
5427 goto error;
5429 isl_map_free(map);
5430 isl_set_free(set);
5431 return result;
5432 error:
5433 isl_map_free(map);
5434 isl_set_free(set);
5435 return NULL;
5438 struct isl_map *isl_map_intersect_domain(
5439 struct isl_map *map, struct isl_set *set)
5441 return isl_map_reverse(
5442 isl_map_intersect_range(isl_map_reverse(map), set));
5445 struct isl_map *isl_map_apply_domain(
5446 struct isl_map *map1, struct isl_map *map2)
5448 if (!map1 || !map2)
5449 goto error;
5450 map1 = isl_map_reverse(map1);
5451 map1 = isl_map_apply_range(map1, map2);
5452 return isl_map_reverse(map1);
5453 error:
5454 isl_map_free(map1);
5455 isl_map_free(map2);
5456 return NULL;
5459 struct isl_map *isl_map_apply_range(
5460 struct isl_map *map1, struct isl_map *map2)
5462 struct isl_dim *dim_result;
5463 struct isl_map *result;
5464 int i, j;
5466 if (!map1 || !map2)
5467 goto error;
5469 dim_result = isl_dim_join(isl_dim_copy(map1->dim),
5470 isl_dim_copy(map2->dim));
5472 result = isl_map_alloc_dim(dim_result, map1->n * map2->n, 0);
5473 if (!result)
5474 goto error;
5475 for (i = 0; i < map1->n; ++i)
5476 for (j = 0; j < map2->n; ++j) {
5477 result = isl_map_add_basic_map(result,
5478 isl_basic_map_apply_range(
5479 isl_basic_map_copy(map1->p[i]),
5480 isl_basic_map_copy(map2->p[j])));
5481 if (!result)
5482 goto error;
5484 isl_map_free(map1);
5485 isl_map_free(map2);
5486 if (result && result->n <= 1)
5487 ISL_F_SET(result, ISL_MAP_DISJOINT);
5488 return result;
5489 error:
5490 isl_map_free(map1);
5491 isl_map_free(map2);
5492 return NULL;
5496 * returns range - domain
5498 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
5500 isl_dim *dims, *target_dim;
5501 struct isl_basic_set *bset;
5502 unsigned dim;
5503 unsigned nparam;
5504 int i;
5506 if (!bmap)
5507 goto error;
5508 isl_assert(bmap->ctx, isl_dim_tuple_match(bmap->dim, isl_dim_in,
5509 bmap->dim, isl_dim_out),
5510 goto error);
5511 target_dim = isl_dim_domain(isl_basic_map_get_dim(bmap));
5512 dim = isl_basic_map_n_in(bmap);
5513 nparam = isl_basic_map_n_param(bmap);
5514 bset = isl_basic_set_from_basic_map(bmap);
5515 bset = isl_basic_set_cow(bset);
5516 dims = isl_basic_set_get_dim(bset);
5517 dims = isl_dim_add(dims, isl_dim_set, dim);
5518 bset = isl_basic_set_extend_dim(bset, dims, 0, dim, 0);
5519 bset = isl_basic_set_swap_vars(bset, 2*dim);
5520 for (i = 0; i < dim; ++i) {
5521 int j = isl_basic_map_alloc_equality(
5522 (struct isl_basic_map *)bset);
5523 if (j < 0)
5524 goto error;
5525 isl_seq_clr(bset->eq[j], 1 + isl_basic_set_total_dim(bset));
5526 isl_int_set_si(bset->eq[j][1+nparam+i], 1);
5527 isl_int_set_si(bset->eq[j][1+nparam+dim+i], 1);
5528 isl_int_set_si(bset->eq[j][1+nparam+2*dim+i], -1);
5530 bset = isl_basic_set_project_out(bset, isl_dim_set, dim, 2*dim);
5531 bset = isl_basic_set_reset_dim(bset, target_dim);
5532 return bset;
5533 error:
5534 isl_basic_map_free(bmap);
5535 return NULL;
5539 * returns range - domain
5541 struct isl_set *isl_map_deltas(struct isl_map *map)
5543 int i;
5544 isl_dim *dim;
5545 struct isl_set *result;
5547 if (!map)
5548 return NULL;
5550 isl_assert(map->ctx, isl_dim_tuple_match(map->dim, isl_dim_in,
5551 map->dim, isl_dim_out),
5552 goto error);
5553 dim = isl_map_get_dim(map);
5554 dim = isl_dim_domain(dim);
5555 result = isl_set_alloc_dim(dim, map->n, map->flags);
5556 if (!result)
5557 goto error;
5558 for (i = 0; i < map->n; ++i)
5559 result = isl_set_add_basic_set(result,
5560 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
5561 isl_map_free(map);
5562 return result;
5563 error:
5564 isl_map_free(map);
5565 return NULL;
5568 static struct isl_basic_map *basic_map_identity(struct isl_dim *dims)
5570 struct isl_basic_map *bmap;
5571 unsigned nparam;
5572 unsigned dim;
5573 int i;
5575 if (!dims)
5576 return NULL;
5578 nparam = dims->nparam;
5579 dim = dims->n_out;
5580 bmap = isl_basic_map_alloc_dim(dims, 0, dim, 0);
5581 if (!bmap)
5582 goto error;
5584 for (i = 0; i < dim; ++i) {
5585 int j = isl_basic_map_alloc_equality(bmap);
5586 if (j < 0)
5587 goto error;
5588 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
5589 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
5590 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
5592 return isl_basic_map_finalize(bmap);
5593 error:
5594 isl_basic_map_free(bmap);
5595 return NULL;
5598 struct isl_basic_map *isl_basic_map_identity(struct isl_dim *set_dim)
5600 struct isl_dim *dim = isl_dim_map(set_dim);
5601 if (!dim)
5602 return NULL;
5603 return basic_map_identity(dim);
5606 struct isl_basic_map *isl_basic_map_identity_like(struct isl_basic_map *model)
5608 if (!model || !model->dim)
5609 return NULL;
5610 isl_assert(model->ctx,
5611 model->dim->n_in == model->dim->n_out, return NULL);
5612 return basic_map_identity(isl_dim_copy(model->dim));
5615 static struct isl_map *map_identity(struct isl_dim *dim)
5617 struct isl_map *map = isl_map_alloc_dim(dim, 1, ISL_MAP_DISJOINT);
5618 return isl_map_add_basic_map(map, basic_map_identity(isl_dim_copy(dim)));
5621 struct isl_map *isl_map_identity(struct isl_dim *set_dim)
5623 struct isl_dim *dim = isl_dim_map(set_dim);
5624 if (!dim)
5625 return NULL;
5626 return map_identity(dim);
5629 struct isl_map *isl_map_identity_like(struct isl_map *model)
5631 if (!model || !model->dim)
5632 return NULL;
5633 isl_assert(model->ctx,
5634 model->dim->n_in == model->dim->n_out, return NULL);
5635 return map_identity(isl_dim_copy(model->dim));
5638 struct isl_map *isl_map_identity_like_basic_map(struct isl_basic_map *model)
5640 if (!model || !model->dim)
5641 return NULL;
5642 isl_assert(model->ctx,
5643 model->dim->n_in == model->dim->n_out, return NULL);
5644 return map_identity(isl_dim_copy(model->dim));
5647 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
5649 isl_dim *dim = isl_set_get_dim(set);
5650 isl_map *id;
5651 id = isl_map_identity(dim);
5652 return isl_map_intersect_range(id, set);
5655 /* Construct a basic set with all set dimensions having only non-negative
5656 * values.
5658 struct isl_basic_set *isl_basic_set_positive_orthant(struct isl_dim *dims)
5660 int i;
5661 unsigned nparam;
5662 unsigned dim;
5663 struct isl_basic_set *bset;
5665 if (!dims)
5666 return NULL;
5667 nparam = dims->nparam;
5668 dim = dims->n_out;
5669 bset = isl_basic_set_alloc_dim(dims, 0, 0, dim);
5670 if (!bset)
5671 return NULL;
5672 for (i = 0; i < dim; ++i) {
5673 int k = isl_basic_set_alloc_inequality(bset);
5674 if (k < 0)
5675 goto error;
5676 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
5677 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
5679 return bset;
5680 error:
5681 isl_basic_set_free(bset);
5682 return NULL;
5685 /* Construct the half-space x_pos >= 0.
5687 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_dim *dim,
5688 int pos)
5690 int k;
5691 isl_basic_set *nonneg;
5693 nonneg = isl_basic_set_alloc_dim(dim, 0, 0, 1);
5694 k = isl_basic_set_alloc_inequality(nonneg);
5695 if (k < 0)
5696 goto error;
5697 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
5698 isl_int_set_si(nonneg->ineq[k][pos], 1);
5700 return isl_basic_set_finalize(nonneg);
5701 error:
5702 isl_basic_set_free(nonneg);
5703 return NULL;
5706 /* Construct the half-space x_pos <= -1.
5708 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_dim *dim, int pos)
5710 int k;
5711 isl_basic_set *neg;
5713 neg = isl_basic_set_alloc_dim(dim, 0, 0, 1);
5714 k = isl_basic_set_alloc_inequality(neg);
5715 if (k < 0)
5716 goto error;
5717 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
5718 isl_int_set_si(neg->ineq[k][0], -1);
5719 isl_int_set_si(neg->ineq[k][pos], -1);
5721 return isl_basic_set_finalize(neg);
5722 error:
5723 isl_basic_set_free(neg);
5724 return NULL;
5727 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
5728 enum isl_dim_type type, unsigned first, unsigned n)
5730 int i;
5731 isl_basic_set *nonneg;
5732 isl_basic_set *neg;
5734 if (!set)
5735 return NULL;
5736 if (n == 0)
5737 return set;
5739 isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
5741 for (i = 0; i < n; ++i) {
5742 nonneg = nonneg_halfspace(isl_set_get_dim(set),
5743 pos(set->dim, type) + first + i);
5744 neg = neg_halfspace(isl_set_get_dim(set),
5745 pos(set->dim, type) + first + i);
5747 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
5750 return set;
5751 error:
5752 isl_set_free(set);
5753 return NULL;
5756 static int foreach_orthant(__isl_take isl_set *set, int *signs, int first,
5757 int len, int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
5758 void *user)
5760 isl_set *half;
5762 if (!set)
5763 return -1;
5764 if (isl_set_fast_is_empty(set)) {
5765 isl_set_free(set);
5766 return 0;
5768 if (first == len)
5769 return fn(set, signs, user);
5771 signs[first] = 1;
5772 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_dim(set),
5773 1 + first));
5774 half = isl_set_intersect(half, isl_set_copy(set));
5775 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
5776 goto error;
5778 signs[first] = -1;
5779 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_dim(set),
5780 1 + first));
5781 half = isl_set_intersect(half, set);
5782 return foreach_orthant(half, signs, first + 1, len, fn, user);
5783 error:
5784 isl_set_free(set);
5785 return -1;
5788 /* Call "fn" on the intersections of "set" with each of the orthants
5789 * (except for obviously empty intersections). The orthant is identified
5790 * by the signs array, with each entry having value 1 or -1 according
5791 * to the sign of the corresponding variable.
5793 int isl_set_foreach_orthant(__isl_keep isl_set *set,
5794 int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
5795 void *user)
5797 unsigned nparam;
5798 unsigned nvar;
5799 int *signs;
5800 int r;
5802 if (!set)
5803 return -1;
5804 if (isl_set_fast_is_empty(set))
5805 return 0;
5807 nparam = isl_set_dim(set, isl_dim_param);
5808 nvar = isl_set_dim(set, isl_dim_set);
5810 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
5812 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
5813 fn, user);
5815 free(signs);
5817 return r;
5820 int isl_set_is_equal(struct isl_set *set1, struct isl_set *set2)
5822 return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
5825 int isl_basic_map_is_subset(
5826 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
5828 int is_subset;
5829 struct isl_map *map1;
5830 struct isl_map *map2;
5832 if (!bmap1 || !bmap2)
5833 return -1;
5835 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
5836 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
5838 is_subset = isl_map_is_subset(map1, map2);
5840 isl_map_free(map1);
5841 isl_map_free(map2);
5843 return is_subset;
5846 int isl_basic_map_is_equal(
5847 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
5849 int is_subset;
5851 if (!bmap1 || !bmap2)
5852 return -1;
5853 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
5854 if (is_subset != 1)
5855 return is_subset;
5856 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
5857 return is_subset;
5860 int isl_basic_set_is_equal(
5861 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
5863 return isl_basic_map_is_equal(
5864 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
5867 int isl_map_is_empty(struct isl_map *map)
5869 int i;
5870 int is_empty;
5872 if (!map)
5873 return -1;
5874 for (i = 0; i < map->n; ++i) {
5875 is_empty = isl_basic_map_is_empty(map->p[i]);
5876 if (is_empty < 0)
5877 return -1;
5878 if (!is_empty)
5879 return 0;
5881 return 1;
5884 int isl_map_fast_is_empty(struct isl_map *map)
5886 return map ? map->n == 0 : -1;
5889 int isl_set_fast_is_empty(struct isl_set *set)
5891 return set ? set->n == 0 : -1;
5894 int isl_set_is_empty(struct isl_set *set)
5896 return isl_map_is_empty((struct isl_map *)set);
5899 int isl_map_has_equal_dim(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
5901 if (!map1 || !map2)
5902 return -1;
5904 return isl_dim_equal(map1->dim, map2->dim);
5907 int isl_set_has_equal_dim(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
5909 if (!set1 || !set2)
5910 return -1;
5912 return isl_dim_equal(set1->dim, set2->dim);
5915 int isl_map_is_equal(struct isl_map *map1, struct isl_map *map2)
5917 int is_subset;
5919 if (!map1 || !map2)
5920 return -1;
5921 is_subset = isl_map_is_subset(map1, map2);
5922 if (is_subset != 1)
5923 return is_subset;
5924 is_subset = isl_map_is_subset(map2, map1);
5925 return is_subset;
5928 int isl_basic_map_is_strict_subset(
5929 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
5931 int is_subset;
5933 if (!bmap1 || !bmap2)
5934 return -1;
5935 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
5936 if (is_subset != 1)
5937 return is_subset;
5938 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
5939 if (is_subset == -1)
5940 return is_subset;
5941 return !is_subset;
5944 int isl_map_is_strict_subset(struct isl_map *map1, struct isl_map *map2)
5946 int is_subset;
5948 if (!map1 || !map2)
5949 return -1;
5950 is_subset = isl_map_is_subset(map1, map2);
5951 if (is_subset != 1)
5952 return is_subset;
5953 is_subset = isl_map_is_subset(map2, map1);
5954 if (is_subset == -1)
5955 return is_subset;
5956 return !is_subset;
5959 int isl_set_is_strict_subset(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
5961 return isl_map_is_strict_subset((isl_map *)set1, (isl_map *)set2);
5964 int isl_basic_map_is_universe(struct isl_basic_map *bmap)
5966 if (!bmap)
5967 return -1;
5968 return bmap->n_eq == 0 && bmap->n_ineq == 0;
5971 int isl_basic_set_is_universe(struct isl_basic_set *bset)
5973 if (!bset)
5974 return -1;
5975 return bset->n_eq == 0 && bset->n_ineq == 0;
5978 int isl_map_fast_is_universe(__isl_keep isl_map *map)
5980 int i;
5982 if (!map)
5983 return -1;
5985 for (i = 0; i < map->n; ++i) {
5986 int r = isl_basic_map_is_universe(map->p[i]);
5987 if (r < 0 || r)
5988 return r;
5991 return 0;
5994 int isl_set_fast_is_universe(__isl_keep isl_set *set)
5996 return isl_map_fast_is_universe((isl_map *) set);
5999 int isl_basic_map_is_empty(struct isl_basic_map *bmap)
6001 struct isl_basic_set *bset = NULL;
6002 struct isl_vec *sample = NULL;
6003 int empty;
6004 unsigned total;
6006 if (!bmap)
6007 return -1;
6009 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
6010 return 1;
6012 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
6013 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
6014 copy = isl_basic_map_remove_redundancies(copy);
6015 empty = ISL_F_ISSET(copy, ISL_BASIC_MAP_EMPTY);
6016 isl_basic_map_free(copy);
6017 return empty;
6020 total = 1 + isl_basic_map_total_dim(bmap);
6021 if (bmap->sample && bmap->sample->size == total) {
6022 int contains = isl_basic_map_contains(bmap, bmap->sample);
6023 if (contains < 0)
6024 return -1;
6025 if (contains)
6026 return 0;
6028 isl_vec_free(bmap->sample);
6029 bmap->sample = NULL;
6030 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
6031 if (!bset)
6032 return -1;
6033 sample = isl_basic_set_sample_vec(bset);
6034 if (!sample)
6035 return -1;
6036 empty = sample->size == 0;
6037 isl_vec_free(bmap->sample);
6038 bmap->sample = sample;
6039 if (empty)
6040 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
6042 return empty;
6045 int isl_basic_map_fast_is_empty(struct isl_basic_map *bmap)
6047 if (!bmap)
6048 return -1;
6049 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
6052 int isl_basic_set_fast_is_empty(struct isl_basic_set *bset)
6054 if (!bset)
6055 return -1;
6056 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
6059 int isl_basic_set_is_empty(struct isl_basic_set *bset)
6061 return isl_basic_map_is_empty((struct isl_basic_map *)bset);
6064 struct isl_map *isl_basic_map_union(
6065 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6067 struct isl_map *map;
6068 if (!bmap1 || !bmap2)
6069 return NULL;
6071 isl_assert(bmap1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim), goto error);
6073 map = isl_map_alloc_dim(isl_dim_copy(bmap1->dim), 2, 0);
6074 if (!map)
6075 goto error;
6076 map = isl_map_add_basic_map(map, bmap1);
6077 map = isl_map_add_basic_map(map, bmap2);
6078 return map;
6079 error:
6080 isl_basic_map_free(bmap1);
6081 isl_basic_map_free(bmap2);
6082 return NULL;
6085 struct isl_set *isl_basic_set_union(
6086 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
6088 return (struct isl_set *)isl_basic_map_union(
6089 (struct isl_basic_map *)bset1,
6090 (struct isl_basic_map *)bset2);
6093 /* Order divs such that any div only depends on previous divs */
6094 struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
6096 int i;
6097 unsigned off;
6099 if (!bmap)
6100 return NULL;
6102 off = isl_dim_total(bmap->dim);
6104 for (i = 0; i < bmap->n_div; ++i) {
6105 int pos;
6106 if (isl_int_is_zero(bmap->div[i][0]))
6107 continue;
6108 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
6109 bmap->n_div-i);
6110 if (pos == -1)
6111 continue;
6112 isl_basic_map_swap_div(bmap, i, i + pos);
6113 --i;
6115 return bmap;
6118 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
6120 return (struct isl_basic_set *)
6121 isl_basic_map_order_divs((struct isl_basic_map *)bset);
6124 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
6126 int i;
6128 if (!map)
6129 return 0;
6131 for (i = 0; i < map->n; ++i) {
6132 map->p[i] = isl_basic_map_order_divs(map->p[i]);
6133 if (!map->p[i])
6134 goto error;
6137 return map;
6138 error:
6139 isl_map_free(map);
6140 return NULL;
6143 /* Look for a div in dst that corresponds to the div "div" in src.
6144 * The divs before "div" in src and dst are assumed to be the same.
6146 * Returns -1 if no corresponding div was found and the position
6147 * of the corresponding div in dst otherwise.
6149 static int find_div(struct isl_basic_map *dst,
6150 struct isl_basic_map *src, unsigned div)
6152 int i;
6154 unsigned total = isl_dim_total(src->dim);
6156 isl_assert(dst->ctx, div <= dst->n_div, return -1);
6157 for (i = div; i < dst->n_div; ++i)
6158 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
6159 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
6160 dst->n_div - div) == -1)
6161 return i;
6162 return -1;
6165 struct isl_basic_map *isl_basic_map_align_divs(
6166 struct isl_basic_map *dst, struct isl_basic_map *src)
6168 int i;
6169 unsigned total = isl_dim_total(src->dim);
6171 if (!dst || !src)
6172 goto error;
6174 if (src->n_div == 0)
6175 return dst;
6177 for (i = 0; i < src->n_div; ++i)
6178 isl_assert(src->ctx, !isl_int_is_zero(src->div[i][0]), goto error);
6180 src = isl_basic_map_order_divs(src);
6181 dst = isl_basic_map_cow(dst);
6182 dst = isl_basic_map_extend_dim(dst, isl_dim_copy(dst->dim),
6183 src->n_div, 0, 2 * src->n_div);
6184 if (!dst)
6185 return NULL;
6186 for (i = 0; i < src->n_div; ++i) {
6187 int j = find_div(dst, src, i);
6188 if (j < 0) {
6189 j = isl_basic_map_alloc_div(dst);
6190 if (j < 0)
6191 goto error;
6192 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
6193 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
6194 if (isl_basic_map_add_div_constraints(dst, j) < 0)
6195 goto error;
6197 if (j != i)
6198 isl_basic_map_swap_div(dst, i, j);
6200 return dst;
6201 error:
6202 isl_basic_map_free(dst);
6203 return NULL;
6206 struct isl_basic_set *isl_basic_set_align_divs(
6207 struct isl_basic_set *dst, struct isl_basic_set *src)
6209 return (struct isl_basic_set *)isl_basic_map_align_divs(
6210 (struct isl_basic_map *)dst, (struct isl_basic_map *)src);
6213 struct isl_map *isl_map_align_divs(struct isl_map *map)
6215 int i;
6217 if (!map)
6218 return NULL;
6219 if (map->n == 0)
6220 return map;
6221 map = isl_map_compute_divs(map);
6222 map = isl_map_cow(map);
6223 if (!map)
6224 return NULL;
6226 for (i = 1; i < map->n; ++i)
6227 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
6228 for (i = 1; i < map->n; ++i)
6229 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
6231 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6232 return map;
6235 struct isl_set *isl_set_align_divs(struct isl_set *set)
6237 return (struct isl_set *)isl_map_align_divs((struct isl_map *)set);
6240 struct isl_set *isl_set_apply(struct isl_set *set, struct isl_map *map)
6242 if (!set || !map)
6243 goto error;
6244 isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
6245 map = isl_map_intersect_domain(map, set);
6246 set = isl_map_range(map);
6247 return set;
6248 error:
6249 isl_set_free(set);
6250 isl_map_free(map);
6251 return NULL;
6254 /* There is no need to cow as removing empty parts doesn't change
6255 * the meaning of the set.
6257 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
6259 int i;
6261 if (!map)
6262 return NULL;
6264 for (i = map->n-1; i >= 0; --i) {
6265 if (!ISL_F_ISSET(map->p[i], ISL_BASIC_MAP_EMPTY))
6266 continue;
6267 isl_basic_map_free(map->p[i]);
6268 if (i != map->n-1) {
6269 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6270 map->p[i] = map->p[map->n-1];
6272 map->n--;
6275 return map;
6278 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
6280 return (struct isl_set *)
6281 isl_map_remove_empty_parts((struct isl_map *)set);
6284 struct isl_basic_map *isl_map_copy_basic_map(struct isl_map *map)
6286 struct isl_basic_map *bmap;
6287 if (!map || map->n == 0)
6288 return NULL;
6289 bmap = map->p[map->n-1];
6290 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
6291 return isl_basic_map_copy(bmap);
6294 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
6296 return (struct isl_basic_set *)
6297 isl_map_copy_basic_map((struct isl_map *)set);
6300 __isl_give isl_map *isl_map_drop_basic_map(__isl_take isl_map *map,
6301 __isl_keep isl_basic_map *bmap)
6303 int i;
6305 if (!map || !bmap)
6306 goto error;
6307 for (i = map->n-1; i >= 0; --i) {
6308 if (map->p[i] != bmap)
6309 continue;
6310 map = isl_map_cow(map);
6311 if (!map)
6312 goto error;
6313 isl_basic_map_free(map->p[i]);
6314 if (i != map->n-1) {
6315 ISL_F_CLR(map, ISL_SET_NORMALIZED);
6316 map->p[i] = map->p[map->n-1];
6318 map->n--;
6319 return map;
6321 return map;
6322 error:
6323 isl_map_free(map);
6324 return NULL;
6327 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
6328 struct isl_basic_set *bset)
6330 return (struct isl_set *)isl_map_drop_basic_map((struct isl_map *)set,
6331 (struct isl_basic_map *)bset);
6334 /* Given two basic sets bset1 and bset2, compute the maximal difference
6335 * between the values of dimension pos in bset1 and those in bset2
6336 * for any common value of the parameters and dimensions preceding pos.
6338 static enum isl_lp_result basic_set_maximal_difference_at(
6339 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
6340 int pos, isl_int *opt)
6342 struct isl_dim *dims;
6343 struct isl_basic_map *bmap1 = NULL;
6344 struct isl_basic_map *bmap2 = NULL;
6345 struct isl_ctx *ctx;
6346 struct isl_vec *obj;
6347 unsigned total;
6348 unsigned nparam;
6349 unsigned dim1, dim2;
6350 enum isl_lp_result res;
6352 if (!bset1 || !bset2)
6353 return isl_lp_error;
6355 nparam = isl_basic_set_n_param(bset1);
6356 dim1 = isl_basic_set_n_dim(bset1);
6357 dim2 = isl_basic_set_n_dim(bset2);
6358 dims = isl_dim_alloc(bset1->ctx, nparam, pos, dim1 - pos);
6359 bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1), dims);
6360 dims = isl_dim_alloc(bset2->ctx, nparam, pos, dim2 - pos);
6361 bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims);
6362 if (!bmap1 || !bmap2)
6363 goto error;
6364 bmap1 = isl_basic_map_cow(bmap1);
6365 bmap1 = isl_basic_map_extend(bmap1, nparam,
6366 pos, (dim1 - pos) + (dim2 - pos),
6367 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
6368 bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
6369 if (!bmap1)
6370 goto error;
6371 total = isl_basic_map_total_dim(bmap1);
6372 ctx = bmap1->ctx;
6373 obj = isl_vec_alloc(ctx, 1 + total);
6374 isl_seq_clr(obj->block.data, 1 + total);
6375 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
6376 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
6377 if (!obj)
6378 goto error;
6379 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
6380 opt, NULL, NULL);
6381 isl_basic_map_free(bmap1);
6382 isl_vec_free(obj);
6383 return res;
6384 error:
6385 isl_basic_map_free(bmap1);
6386 isl_basic_map_free(bmap2);
6387 return isl_lp_error;
6390 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
6391 * for any common value of the parameters and dimensions preceding pos
6392 * in both basic sets, the values of dimension pos in bset1 are
6393 * smaller or larger than those in bset2.
6395 * Returns
6396 * 1 if bset1 follows bset2
6397 * -1 if bset1 precedes bset2
6398 * 0 if bset1 and bset2 are incomparable
6399 * -2 if some error occurred.
6401 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
6402 struct isl_basic_set *bset2, int pos)
6404 isl_int opt;
6405 enum isl_lp_result res;
6406 int cmp;
6408 isl_int_init(opt);
6410 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
6412 if (res == isl_lp_empty)
6413 cmp = 0;
6414 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
6415 res == isl_lp_unbounded)
6416 cmp = 1;
6417 else if (res == isl_lp_ok && isl_int_is_neg(opt))
6418 cmp = -1;
6419 else
6420 cmp = -2;
6422 isl_int_clear(opt);
6423 return cmp;
6426 /* Given two basic sets bset1 and bset2, check whether
6427 * for any common value of the parameters and dimensions preceding pos
6428 * there is a value of dimension pos in bset1 that is larger
6429 * than a value of the same dimension in bset2.
6431 * Return
6432 * 1 if there exists such a pair
6433 * 0 if there is no such pair, but there is a pair of equal values
6434 * -1 otherwise
6435 * -2 if some error occurred.
6437 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
6438 __isl_keep isl_basic_set *bset2, int pos)
6440 isl_int opt;
6441 enum isl_lp_result res;
6442 int cmp;
6444 isl_int_init(opt);
6446 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
6448 if (res == isl_lp_empty)
6449 cmp = -1;
6450 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
6451 res == isl_lp_unbounded)
6452 cmp = 1;
6453 else if (res == isl_lp_ok && isl_int_is_neg(opt))
6454 cmp = -1;
6455 else if (res == isl_lp_ok)
6456 cmp = 0;
6457 else
6458 cmp = -2;
6460 isl_int_clear(opt);
6461 return cmp;
6464 /* Given two sets set1 and set2, check whether
6465 * for any common value of the parameters and dimensions preceding pos
6466 * there is a value of dimension pos in set1 that is larger
6467 * than a value of the same dimension in set2.
6469 * Return
6470 * 1 if there exists such a pair
6471 * 0 if there is no such pair, but there is a pair of equal values
6472 * -1 otherwise
6473 * -2 if some error occurred.
6475 int isl_set_follows_at(__isl_keep isl_set *set1,
6476 __isl_keep isl_set *set2, int pos)
6478 int i, j;
6479 int follows = -1;
6481 if (!set1 || !set2)
6482 return -2;
6484 for (i = 0; i < set1->n; ++i)
6485 for (j = 0; j < set2->n; ++j) {
6486 int f;
6487 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
6488 if (f == 1 || f == -2)
6489 return f;
6490 if (f > follows)
6491 follows = f;
6494 return follows;
6497 static int isl_basic_map_fast_has_fixed_var(struct isl_basic_map *bmap,
6498 unsigned pos, isl_int *val)
6500 int i;
6501 int d;
6502 unsigned total;
6504 if (!bmap)
6505 return -1;
6506 total = isl_basic_map_total_dim(bmap);
6507 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
6508 for (; d+1 > pos; --d)
6509 if (!isl_int_is_zero(bmap->eq[i][1+d]))
6510 break;
6511 if (d != pos)
6512 continue;
6513 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
6514 return 0;
6515 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
6516 return 0;
6517 if (!isl_int_is_one(bmap->eq[i][1+d]))
6518 return 0;
6519 if (val)
6520 isl_int_neg(*val, bmap->eq[i][0]);
6521 return 1;
6523 return 0;
6526 static int isl_map_fast_has_fixed_var(struct isl_map *map,
6527 unsigned pos, isl_int *val)
6529 int i;
6530 isl_int v;
6531 isl_int tmp;
6532 int fixed;
6534 if (!map)
6535 return -1;
6536 if (map->n == 0)
6537 return 0;
6538 if (map->n == 1)
6539 return isl_basic_map_fast_has_fixed_var(map->p[0], pos, val);
6540 isl_int_init(v);
6541 isl_int_init(tmp);
6542 fixed = isl_basic_map_fast_has_fixed_var(map->p[0], pos, &v);
6543 for (i = 1; fixed == 1 && i < map->n; ++i) {
6544 fixed = isl_basic_map_fast_has_fixed_var(map->p[i], pos, &tmp);
6545 if (fixed == 1 && isl_int_ne(tmp, v))
6546 fixed = 0;
6548 if (val)
6549 isl_int_set(*val, v);
6550 isl_int_clear(tmp);
6551 isl_int_clear(v);
6552 return fixed;
6555 static int isl_basic_set_fast_has_fixed_var(struct isl_basic_set *bset,
6556 unsigned pos, isl_int *val)
6558 return isl_basic_map_fast_has_fixed_var((struct isl_basic_map *)bset,
6559 pos, val);
6562 static int isl_set_fast_has_fixed_var(struct isl_set *set, unsigned pos,
6563 isl_int *val)
6565 return isl_map_fast_has_fixed_var((struct isl_map *)set, pos, val);
6568 int isl_basic_map_fast_is_fixed(struct isl_basic_map *bmap,
6569 enum isl_dim_type type, unsigned pos, isl_int *val)
6571 if (pos >= isl_basic_map_dim(bmap, type))
6572 return -1;
6573 return isl_basic_map_fast_has_fixed_var(bmap,
6574 isl_basic_map_offset(bmap, type) - 1 + pos, val);
6577 int isl_map_fast_is_fixed(struct isl_map *map,
6578 enum isl_dim_type type, unsigned pos, isl_int *val)
6580 if (pos >= isl_map_dim(map, type))
6581 return -1;
6582 return isl_map_fast_has_fixed_var(map,
6583 map_offset(map, type) - 1 + pos, val);
6586 /* Check if dimension dim has fixed value and if so and if val is not NULL,
6587 * then return this fixed value in *val.
6589 int isl_basic_set_fast_dim_is_fixed(struct isl_basic_set *bset, unsigned dim,
6590 isl_int *val)
6592 return isl_basic_set_fast_has_fixed_var(bset,
6593 isl_basic_set_n_param(bset) + dim, val);
6596 /* Check if dimension dim has fixed value and if so and if val is not NULL,
6597 * then return this fixed value in *val.
6599 int isl_set_fast_dim_is_fixed(struct isl_set *set, unsigned dim, isl_int *val)
6601 return isl_set_fast_has_fixed_var(set, isl_set_n_param(set) + dim, val);
6604 /* Check if input variable in has fixed value and if so and if val is not NULL,
6605 * then return this fixed value in *val.
6607 int isl_map_fast_input_is_fixed(struct isl_map *map, unsigned in, isl_int *val)
6609 return isl_map_fast_has_fixed_var(map, isl_map_n_param(map) + in, val);
6612 /* Check if dimension dim has an (obvious) fixed lower bound and if so
6613 * and if val is not NULL, then return this lower bound in *val.
6615 int isl_basic_set_fast_dim_has_fixed_lower_bound(struct isl_basic_set *bset,
6616 unsigned dim, isl_int *val)
6618 int i, i_eq = -1, i_ineq = -1;
6619 isl_int *c;
6620 unsigned total;
6621 unsigned nparam;
6623 if (!bset)
6624 return -1;
6625 total = isl_basic_set_total_dim(bset);
6626 nparam = isl_basic_set_n_param(bset);
6627 for (i = 0; i < bset->n_eq; ++i) {
6628 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
6629 continue;
6630 if (i_eq != -1)
6631 return 0;
6632 i_eq = i;
6634 for (i = 0; i < bset->n_ineq; ++i) {
6635 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
6636 continue;
6637 if (i_eq != -1 || i_ineq != -1)
6638 return 0;
6639 i_ineq = i;
6641 if (i_eq == -1 && i_ineq == -1)
6642 return 0;
6643 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
6644 /* The coefficient should always be one due to normalization. */
6645 if (!isl_int_is_one(c[1+nparam+dim]))
6646 return 0;
6647 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
6648 return 0;
6649 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
6650 total - nparam - dim - 1) != -1)
6651 return 0;
6652 if (val)
6653 isl_int_neg(*val, c[0]);
6654 return 1;
6657 int isl_set_fast_dim_has_fixed_lower_bound(struct isl_set *set,
6658 unsigned dim, isl_int *val)
6660 int i;
6661 isl_int v;
6662 isl_int tmp;
6663 int fixed;
6665 if (!set)
6666 return -1;
6667 if (set->n == 0)
6668 return 0;
6669 if (set->n == 1)
6670 return isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[0],
6671 dim, val);
6672 isl_int_init(v);
6673 isl_int_init(tmp);
6674 fixed = isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[0],
6675 dim, &v);
6676 for (i = 1; fixed == 1 && i < set->n; ++i) {
6677 fixed = isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[i],
6678 dim, &tmp);
6679 if (fixed == 1 && isl_int_ne(tmp, v))
6680 fixed = 0;
6682 if (val)
6683 isl_int_set(*val, v);
6684 isl_int_clear(tmp);
6685 isl_int_clear(v);
6686 return fixed;
6689 struct constraint {
6690 unsigned size;
6691 isl_int *c;
6694 static int qsort_constraint_cmp(const void *p1, const void *p2)
6696 const struct constraint *c1 = (const struct constraint *)p1;
6697 const struct constraint *c2 = (const struct constraint *)p2;
6698 unsigned size = isl_min(c1->size, c2->size);
6699 return isl_seq_cmp(c1->c, c2->c, size);
6702 static struct isl_basic_map *isl_basic_map_sort_constraints(
6703 struct isl_basic_map *bmap)
6705 int i;
6706 struct constraint *c;
6707 unsigned total;
6709 if (!bmap)
6710 return NULL;
6711 total = isl_basic_map_total_dim(bmap);
6712 c = isl_alloc_array(bmap->ctx, struct constraint, bmap->n_ineq);
6713 if (!c)
6714 goto error;
6715 for (i = 0; i < bmap->n_ineq; ++i) {
6716 c[i].size = total;
6717 c[i].c = bmap->ineq[i];
6719 qsort(c, bmap->n_ineq, sizeof(struct constraint), qsort_constraint_cmp);
6720 for (i = 0; i < bmap->n_ineq; ++i)
6721 bmap->ineq[i] = c[i].c;
6722 free(c);
6723 return bmap;
6724 error:
6725 isl_basic_map_free(bmap);
6726 return NULL;
6729 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
6730 __isl_take isl_basic_set *bset)
6732 return (struct isl_basic_set *)isl_basic_map_sort_constraints(
6733 (struct isl_basic_map *)bset);
6736 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
6738 if (!bmap)
6739 return NULL;
6740 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
6741 return bmap;
6742 bmap = isl_basic_map_remove_redundancies(bmap);
6743 bmap = isl_basic_map_sort_constraints(bmap);
6744 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
6745 return bmap;
6748 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
6750 return (struct isl_basic_set *)isl_basic_map_normalize(
6751 (struct isl_basic_map *)bset);
6754 int isl_basic_map_fast_cmp(const __isl_keep isl_basic_map *bmap1,
6755 const __isl_keep isl_basic_map *bmap2)
6757 int i, cmp;
6758 unsigned total;
6760 if (bmap1 == bmap2)
6761 return 0;
6762 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
6763 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
6764 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
6765 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
6766 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
6767 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
6768 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
6769 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
6770 return 0;
6771 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
6772 return 1;
6773 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
6774 return -1;
6775 if (bmap1->n_eq != bmap2->n_eq)
6776 return bmap1->n_eq - bmap2->n_eq;
6777 if (bmap1->n_ineq != bmap2->n_ineq)
6778 return bmap1->n_ineq - bmap2->n_ineq;
6779 if (bmap1->n_div != bmap2->n_div)
6780 return bmap1->n_div - bmap2->n_div;
6781 total = isl_basic_map_total_dim(bmap1);
6782 for (i = 0; i < bmap1->n_eq; ++i) {
6783 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
6784 if (cmp)
6785 return cmp;
6787 for (i = 0; i < bmap1->n_ineq; ++i) {
6788 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
6789 if (cmp)
6790 return cmp;
6792 for (i = 0; i < bmap1->n_div; ++i) {
6793 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
6794 if (cmp)
6795 return cmp;
6797 return 0;
6800 int isl_basic_map_fast_is_equal(__isl_keep isl_basic_map *bmap1,
6801 __isl_keep isl_basic_map *bmap2)
6803 return isl_basic_map_fast_cmp(bmap1, bmap2) == 0;
6806 int isl_basic_set_fast_is_equal(__isl_keep isl_basic_set *bset1,
6807 __isl_keep isl_basic_set *bset2)
6809 return isl_basic_map_fast_is_equal((isl_basic_map *)bset1,
6810 (isl_basic_map *)bset2);
6813 static int qsort_bmap_cmp(const void *p1, const void *p2)
6815 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
6816 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
6818 return isl_basic_map_fast_cmp(bmap1, bmap2);
6821 /* We normalize in place, but if anything goes wrong we need
6822 * to return NULL, so we need to make sure we don't change the
6823 * meaning of any possible other copies of map.
6825 struct isl_map *isl_map_normalize(struct isl_map *map)
6827 int i, j;
6828 struct isl_basic_map *bmap;
6830 if (!map)
6831 return NULL;
6832 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
6833 return map;
6834 for (i = 0; i < map->n; ++i) {
6835 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
6836 if (!bmap)
6837 goto error;
6838 isl_basic_map_free(map->p[i]);
6839 map->p[i] = bmap;
6841 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
6842 ISL_F_SET(map, ISL_MAP_NORMALIZED);
6843 map = isl_map_remove_empty_parts(map);
6844 if (!map)
6845 return NULL;
6846 for (i = map->n - 1; i >= 1; --i) {
6847 if (!isl_basic_map_fast_is_equal(map->p[i-1], map->p[i]))
6848 continue;
6849 isl_basic_map_free(map->p[i-1]);
6850 for (j = i; j < map->n; ++j)
6851 map->p[j-1] = map->p[j];
6852 map->n--;
6854 return map;
6855 error:
6856 isl_map_free(map);
6857 return NULL;
6861 struct isl_set *isl_set_normalize(struct isl_set *set)
6863 return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
6866 int isl_map_fast_is_equal(struct isl_map *map1, struct isl_map *map2)
6868 int i;
6869 int equal;
6871 if (!map1 || !map2)
6872 return -1;
6874 if (map1 == map2)
6875 return 1;
6876 if (!isl_dim_equal(map1->dim, map2->dim))
6877 return 0;
6879 map1 = isl_map_copy(map1);
6880 map2 = isl_map_copy(map2);
6881 map1 = isl_map_normalize(map1);
6882 map2 = isl_map_normalize(map2);
6883 if (!map1 || !map2)
6884 goto error;
6885 equal = map1->n == map2->n;
6886 for (i = 0; equal && i < map1->n; ++i) {
6887 equal = isl_basic_map_fast_is_equal(map1->p[i], map2->p[i]);
6888 if (equal < 0)
6889 goto error;
6891 isl_map_free(map1);
6892 isl_map_free(map2);
6893 return equal;
6894 error:
6895 isl_map_free(map1);
6896 isl_map_free(map2);
6897 return -1;
6900 int isl_set_fast_is_equal(struct isl_set *set1, struct isl_set *set2)
6902 return isl_map_fast_is_equal((struct isl_map *)set1,
6903 (struct isl_map *)set2);
6906 /* Return an interval that ranges from min to max (inclusive)
6908 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
6909 isl_int min, isl_int max)
6911 int k;
6912 struct isl_basic_set *bset = NULL;
6914 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
6915 if (!bset)
6916 goto error;
6918 k = isl_basic_set_alloc_inequality(bset);
6919 if (k < 0)
6920 goto error;
6921 isl_int_set_si(bset->ineq[k][1], 1);
6922 isl_int_neg(bset->ineq[k][0], min);
6924 k = isl_basic_set_alloc_inequality(bset);
6925 if (k < 0)
6926 goto error;
6927 isl_int_set_si(bset->ineq[k][1], -1);
6928 isl_int_set(bset->ineq[k][0], max);
6930 return bset;
6931 error:
6932 isl_basic_set_free(bset);
6933 return NULL;
6936 /* Return the Cartesian product of the basic sets in list (in the given order).
6938 struct isl_basic_set *isl_basic_set_product(struct isl_basic_set_list *list)
6940 int i;
6941 unsigned dim;
6942 unsigned nparam;
6943 unsigned extra;
6944 unsigned n_eq;
6945 unsigned n_ineq;
6946 struct isl_basic_set *product = NULL;
6948 if (!list)
6949 goto error;
6950 isl_assert(list->ctx, list->n > 0, goto error);
6951 isl_assert(list->ctx, list->p[0], goto error);
6952 nparam = isl_basic_set_n_param(list->p[0]);
6953 dim = isl_basic_set_n_dim(list->p[0]);
6954 extra = list->p[0]->n_div;
6955 n_eq = list->p[0]->n_eq;
6956 n_ineq = list->p[0]->n_ineq;
6957 for (i = 1; i < list->n; ++i) {
6958 isl_assert(list->ctx, list->p[i], goto error);
6959 isl_assert(list->ctx,
6960 nparam == isl_basic_set_n_param(list->p[i]), goto error);
6961 dim += isl_basic_set_n_dim(list->p[i]);
6962 extra += list->p[i]->n_div;
6963 n_eq += list->p[i]->n_eq;
6964 n_ineq += list->p[i]->n_ineq;
6966 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
6967 n_eq, n_ineq);
6968 if (!product)
6969 goto error;
6970 dim = 0;
6971 for (i = 0; i < list->n; ++i) {
6972 isl_basic_set_add_constraints(product,
6973 isl_basic_set_copy(list->p[i]), dim);
6974 dim += isl_basic_set_n_dim(list->p[i]);
6976 isl_basic_set_list_free(list);
6977 return product;
6978 error:
6979 isl_basic_set_free(product);
6980 isl_basic_set_list_free(list);
6981 return NULL;
6984 struct isl_basic_map *isl_basic_map_product(
6985 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6987 struct isl_dim *dim_result = NULL;
6988 struct isl_basic_map *bmap;
6989 unsigned in1, in2, out1, out2, nparam, total, pos;
6990 struct isl_dim_map *dim_map1, *dim_map2;
6992 if (!bmap1 || !bmap2)
6993 goto error;
6995 isl_assert(bmap1->ctx, isl_dim_match(bmap1->dim, isl_dim_param,
6996 bmap2->dim, isl_dim_param), goto error);
6997 dim_result = isl_dim_product(isl_dim_copy(bmap1->dim),
6998 isl_dim_copy(bmap2->dim));
7000 in1 = isl_basic_map_n_in(bmap1);
7001 in2 = isl_basic_map_n_in(bmap2);
7002 out1 = isl_basic_map_n_out(bmap1);
7003 out2 = isl_basic_map_n_out(bmap2);
7004 nparam = isl_basic_map_n_param(bmap1);
7006 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
7007 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
7008 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
7009 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
7010 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
7011 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
7012 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
7013 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
7014 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
7015 isl_dim_map_div(dim_map1, bmap1, pos += out2);
7016 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
7018 bmap = isl_basic_map_alloc_dim(dim_result,
7019 bmap1->n_div + bmap2->n_div,
7020 bmap1->n_eq + bmap2->n_eq,
7021 bmap1->n_ineq + bmap2->n_ineq);
7022 bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
7023 bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
7024 bmap = isl_basic_map_simplify(bmap);
7025 return isl_basic_map_finalize(bmap);
7026 error:
7027 isl_basic_map_free(bmap1);
7028 isl_basic_map_free(bmap2);
7029 return NULL;
7032 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
7034 struct isl_map *isl_map_product(struct isl_map *map1, struct isl_map *map2)
7036 unsigned flags = 0;
7037 struct isl_map *result;
7038 int i, j;
7040 if (!map1 || !map2)
7041 goto error;
7043 isl_assert(map1->ctx, isl_dim_match(map1->dim, isl_dim_param,
7044 map2->dim, isl_dim_param), goto error);
7046 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
7047 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
7048 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7050 result = isl_map_alloc_dim(isl_dim_product(isl_dim_copy(map1->dim),
7051 isl_dim_copy(map2->dim)),
7052 map1->n * map2->n, flags);
7053 if (!result)
7054 goto error;
7055 for (i = 0; i < map1->n; ++i)
7056 for (j = 0; j < map2->n; ++j) {
7057 struct isl_basic_map *part;
7058 part = isl_basic_map_product(
7059 isl_basic_map_copy(map1->p[i]),
7060 isl_basic_map_copy(map2->p[j]));
7061 if (isl_basic_map_is_empty(part))
7062 isl_basic_map_free(part);
7063 else
7064 result = isl_map_add_basic_map(result, part);
7065 if (!result)
7066 goto error;
7068 isl_map_free(map1);
7069 isl_map_free(map2);
7070 return result;
7071 error:
7072 isl_map_free(map1);
7073 isl_map_free(map2);
7074 return NULL;
7077 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
7079 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
7080 __isl_take isl_map *map2)
7082 isl_map *prod;
7084 prod = isl_map_product(map1, map2);
7085 prod = isl_map_flatten(prod);
7086 return prod;
7089 /* Given two set A and B, construct its Cartesian product A x B.
7091 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
7093 return (struct isl_set *)isl_map_product((struct isl_map *)set1,
7094 (struct isl_map *)set2);
7097 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
7098 __isl_take isl_set *set2)
7100 return (isl_set *)isl_map_flat_product((isl_map *)set1, (isl_map *)set2);
7103 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
7105 int i;
7106 uint32_t hash = isl_hash_init();
7107 unsigned total;
7109 if (!bmap)
7110 return 0;
7111 bmap = isl_basic_map_copy(bmap);
7112 bmap = isl_basic_map_normalize(bmap);
7113 if (!bmap)
7114 return 0;
7115 total = isl_basic_map_total_dim(bmap);
7116 isl_hash_byte(hash, bmap->n_eq & 0xFF);
7117 for (i = 0; i < bmap->n_eq; ++i) {
7118 uint32_t c_hash;
7119 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
7120 isl_hash_hash(hash, c_hash);
7122 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
7123 for (i = 0; i < bmap->n_ineq; ++i) {
7124 uint32_t c_hash;
7125 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
7126 isl_hash_hash(hash, c_hash);
7128 isl_hash_byte(hash, bmap->n_div & 0xFF);
7129 for (i = 0; i < bmap->n_div; ++i) {
7130 uint32_t c_hash;
7131 if (isl_int_is_zero(bmap->div[i][0]))
7132 continue;
7133 isl_hash_byte(hash, i & 0xFF);
7134 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
7135 isl_hash_hash(hash, c_hash);
7137 isl_basic_map_free(bmap);
7138 return hash;
7141 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
7143 return isl_basic_map_get_hash((isl_basic_map *)bset);
7146 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
7148 int i;
7149 uint32_t hash;
7151 if (!map)
7152 return 0;
7153 map = isl_map_copy(map);
7154 map = isl_map_normalize(map);
7155 if (!map)
7156 return 0;
7158 hash = isl_hash_init();
7159 for (i = 0; i < map->n; ++i) {
7160 uint32_t bmap_hash;
7161 bmap_hash = isl_basic_map_get_hash(map->p[i]);
7162 isl_hash_hash(hash, bmap_hash);
7165 isl_map_free(map);
7167 return hash;
7170 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
7172 return isl_map_get_hash((isl_map *)set);
7175 /* Check if the value for dimension dim is completely determined
7176 * by the values of the other parameters and variables.
7177 * That is, check if dimension dim is involved in an equality.
7179 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
7181 int i;
7182 unsigned nparam;
7184 if (!bset)
7185 return -1;
7186 nparam = isl_basic_set_n_param(bset);
7187 for (i = 0; i < bset->n_eq; ++i)
7188 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
7189 return 1;
7190 return 0;
7193 /* Check if the value for dimension dim is completely determined
7194 * by the values of the other parameters and variables.
7195 * That is, check if dimension dim is involved in an equality
7196 * for each of the subsets.
7198 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
7200 int i;
7202 if (!set)
7203 return -1;
7204 for (i = 0; i < set->n; ++i) {
7205 int unique;
7206 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
7207 if (unique != 1)
7208 return unique;
7210 return 1;
7213 int isl_set_n_basic_set(__isl_keep isl_set *set)
7215 return set ? set->n : 0;
7218 int isl_map_foreach_basic_map(__isl_keep isl_map *map,
7219 int (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
7221 int i;
7223 if (!map)
7224 return -1;
7226 for (i = 0; i < map->n; ++i)
7227 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
7228 return -1;
7230 return 0;
7233 int isl_set_foreach_basic_set(__isl_keep isl_set *set,
7234 int (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
7236 int i;
7238 if (!set)
7239 return -1;
7241 for (i = 0; i < set->n; ++i)
7242 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
7243 return -1;
7245 return 0;
7248 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
7250 struct isl_dim *dim;
7252 if (!bset)
7253 return NULL;
7255 if (bset->n_div == 0)
7256 return bset;
7258 bset = isl_basic_set_cow(bset);
7259 if (!bset)
7260 return NULL;
7262 dim = isl_basic_set_get_dim(bset);
7263 dim = isl_dim_add(dim, isl_dim_set, bset->n_div);
7264 if (!dim)
7265 goto error;
7266 isl_dim_free(bset->dim);
7267 bset->dim = dim;
7268 bset->extra -= bset->n_div;
7269 bset->n_div = 0;
7271 bset = isl_basic_set_finalize(bset);
7273 return bset;
7274 error:
7275 isl_basic_set_free(bset);
7276 return NULL;
7279 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
7281 int i;
7282 struct isl_dim *dim;
7283 unsigned n_div;
7285 set = isl_set_align_divs(set);
7287 if (!set)
7288 return NULL;
7289 if (set->n == 0 || set->p[0]->n_div == 0)
7290 return set;
7292 set = isl_set_cow(set);
7293 if (!set)
7294 return NULL;
7296 n_div = set->p[0]->n_div;
7297 dim = isl_set_get_dim(set);
7298 dim = isl_dim_add(dim, isl_dim_set, n_div);
7299 if (!dim)
7300 goto error;
7301 isl_dim_free(set->dim);
7302 set->dim = dim;
7304 for (i = 0; i < set->n; ++i) {
7305 set->p[i] = isl_basic_set_lift(set->p[i]);
7306 if (!set->p[i])
7307 goto error;
7310 return set;
7311 error:
7312 isl_set_free(set);
7313 return NULL;
7316 __isl_give isl_map *isl_set_lifting(__isl_take isl_set *set)
7318 struct isl_dim *dim;
7319 struct isl_basic_map *bmap;
7320 unsigned n_set;
7321 unsigned n_div;
7322 unsigned n_param;
7323 unsigned total;
7324 int i, k, l;
7326 set = isl_set_align_divs(set);
7328 if (!set)
7329 return NULL;
7331 dim = isl_set_get_dim(set);
7332 if (set->n == 0 || set->p[0]->n_div == 0) {
7333 isl_set_free(set);
7334 return isl_map_identity(dim);
7337 n_div = set->p[0]->n_div;
7338 dim = isl_dim_map(dim);
7339 n_param = isl_dim_size(dim, isl_dim_param);
7340 n_set = isl_dim_size(dim, isl_dim_in);
7341 dim = isl_dim_extend(dim, n_param, n_set, n_set + n_div);
7342 bmap = isl_basic_map_alloc_dim(dim, 0, n_set, 2 * n_div);
7343 for (i = 0; i < n_set; ++i)
7344 bmap = var_equal(bmap, i);
7346 total = n_param + n_set + n_set + n_div;
7347 for (i = 0; i < n_div; ++i) {
7348 k = isl_basic_map_alloc_inequality(bmap);
7349 if (k < 0)
7350 goto error;
7351 isl_seq_cpy(bmap->ineq[k], set->p[0]->div[i]+1, 1+n_param);
7352 isl_seq_clr(bmap->ineq[k]+1+n_param, n_set);
7353 isl_seq_cpy(bmap->ineq[k]+1+n_param+n_set,
7354 set->p[0]->div[i]+1+1+n_param, n_set + n_div);
7355 isl_int_neg(bmap->ineq[k][1+n_param+n_set+n_set+i],
7356 set->p[0]->div[i][0]);
7358 l = isl_basic_map_alloc_inequality(bmap);
7359 if (l < 0)
7360 goto error;
7361 isl_seq_neg(bmap->ineq[l], bmap->ineq[k], 1 + total);
7362 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0],
7363 set->p[0]->div[i][0]);
7364 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
7367 isl_set_free(set);
7368 return isl_map_from_basic_map(bmap);
7369 error:
7370 isl_set_free(set);
7371 isl_basic_map_free(bmap);
7372 return NULL;
7375 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
7377 unsigned dim;
7378 int size = 0;
7380 if (!bset)
7381 return -1;
7383 dim = isl_basic_set_total_dim(bset);
7384 size += bset->n_eq * (1 + dim);
7385 size += bset->n_ineq * (1 + dim);
7386 size += bset->n_div * (2 + dim);
7388 return size;
7391 int isl_set_size(__isl_keep isl_set *set)
7393 int i;
7394 int size = 0;
7396 if (!set)
7397 return -1;
7399 for (i = 0; i < set->n; ++i)
7400 size += isl_basic_set_size(set->p[i]);
7402 return size;
7405 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
7406 enum isl_dim_type type, unsigned pos)
7408 int i;
7409 int lower, upper;
7411 if (!bmap)
7412 return -1;
7414 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), return -1);
7416 pos += isl_basic_map_offset(bmap, type);
7418 for (i = 0; i < bmap->n_eq; ++i)
7419 if (!isl_int_is_zero(bmap->eq[i][pos]))
7420 return 1;
7422 lower = upper = 0;
7423 for (i = 0; i < bmap->n_ineq; ++i) {
7424 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
7425 if (sgn > 0)
7426 lower = 1;
7427 if (sgn < 0)
7428 upper = 1;
7431 return lower && upper;
7434 int isl_map_dim_is_bounded(__isl_keep isl_map *map,
7435 enum isl_dim_type type, unsigned pos)
7437 int i;
7439 if (!map)
7440 return -1;
7442 for (i = 0; i < map->n; ++i) {
7443 int bounded;
7444 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
7445 if (bounded < 0 || !bounded)
7446 return bounded;
7449 return 1;
7452 /* Return 1 if the specified dim is involved in both an upper bound
7453 * and a lower bound.
7455 int isl_set_dim_is_bounded(__isl_keep isl_set *set,
7456 enum isl_dim_type type, unsigned pos)
7458 return isl_map_dim_is_bounded((isl_map *)set, type, pos);
7461 /* For each of the "n" variables starting at "first", determine
7462 * the sign of the variable and put the results in the first "n"
7463 * elements of the array "signs".
7464 * Sign
7465 * 1 means that the variable is non-negative
7466 * -1 means that the variable is non-positive
7467 * 0 means the variable attains both positive and negative values.
7469 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
7470 unsigned first, unsigned n, int *signs)
7472 isl_vec *bound = NULL;
7473 struct isl_tab *tab = NULL;
7474 struct isl_tab_undo *snap;
7475 int i;
7477 if (!bset || !signs)
7478 return -1;
7480 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
7481 tab = isl_tab_from_basic_set(bset);
7482 if (!bound || !tab)
7483 goto error;
7485 isl_seq_clr(bound->el, bound->size);
7486 isl_int_set_si(bound->el[0], -1);
7488 snap = isl_tab_snap(tab);
7489 for (i = 0; i < n; ++i) {
7490 int empty;
7492 isl_int_set_si(bound->el[1 + first + i], -1);
7493 if (isl_tab_add_ineq(tab, bound->el) < 0)
7494 goto error;
7495 empty = tab->empty;
7496 isl_int_set_si(bound->el[1 + first + i], 0);
7497 if (isl_tab_rollback(tab, snap) < 0)
7498 goto error;
7500 if (empty) {
7501 signs[i] = 1;
7502 continue;
7505 isl_int_set_si(bound->el[1 + first + i], 1);
7506 if (isl_tab_add_ineq(tab, bound->el) < 0)
7507 goto error;
7508 empty = tab->empty;
7509 isl_int_set_si(bound->el[1 + first + i], 0);
7510 if (isl_tab_rollback(tab, snap) < 0)
7511 goto error;
7513 signs[i] = empty ? -1 : 0;
7516 isl_tab_free(tab);
7517 isl_vec_free(bound);
7518 return 0;
7519 error:
7520 isl_tab_free(tab);
7521 isl_vec_free(bound);
7522 return -1;
7525 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
7526 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
7528 if (!bset || !signs)
7529 return -1;
7530 isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
7531 return -1);
7533 first += pos(bset->dim, type) - 1;
7534 return isl_basic_set_vars_get_sign(bset, first, n, signs);
7537 /* Check if the given basic map is obviously single-valued.
7538 * In particular, for each output dimension, check that there is
7539 * an equality that defines the output dimension in terms of
7540 * earlier dimensions.
7542 int isl_basic_map_fast_is_single_valued(__isl_keep isl_basic_map *bmap)
7544 int i, j;
7545 unsigned total;
7546 unsigned n_out;
7547 unsigned o_out;
7549 if (!bmap)
7550 return -1;
7552 total = 1 + isl_basic_map_total_dim(bmap);
7553 n_out = isl_basic_map_dim(bmap, isl_dim_out);
7554 o_out = isl_basic_map_offset(bmap, isl_dim_out);
7556 for (i = 0; i < n_out; ++i) {
7557 for (j = 0; j < bmap->n_eq; ++j) {
7558 if (isl_int_is_zero(bmap->eq[j][o_out + i]))
7559 continue;
7560 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + i + 1,
7561 total - (o_out + i + 1)) == -1)
7562 break;
7564 if (j >= bmap->n_eq)
7565 return 0;
7568 return 1;
7571 /* Check if the given map is obviously single-valued.
7573 int isl_map_fast_is_single_valued(__isl_keep isl_map *map)
7575 int sv;
7577 if (!map)
7578 return -1;
7579 if (map->n == 0)
7580 return 1;
7581 if (map->n >= 2)
7582 return 0;
7584 return isl_basic_map_fast_is_single_valued(map->p[0]);
7587 /* Check if the given map is single-valued.
7588 * We simply compute
7590 * M \circ M^-1
7592 * and check if the result is a subset of the identity mapping.
7594 int isl_map_is_single_valued(__isl_keep isl_map *map)
7596 isl_map *test;
7597 isl_map *id;
7598 int sv;
7600 sv = isl_map_fast_is_single_valued(map);
7601 if (sv < 0 || sv)
7602 return sv;
7604 test = isl_map_reverse(isl_map_copy(map));
7605 test = isl_map_apply_range(test, isl_map_copy(map));
7607 id = isl_map_identity(isl_dim_range(isl_map_get_dim(map)));
7609 sv = isl_map_is_subset(test, id);
7611 isl_map_free(test);
7612 isl_map_free(id);
7614 return sv;
7617 int isl_map_is_bijective(__isl_keep isl_map *map)
7619 int sv;
7621 sv = isl_map_is_single_valued(map);
7622 if (sv < 0 || !sv)
7623 return sv;
7625 map = isl_map_copy(map);
7626 map = isl_map_reverse(map);
7627 sv = isl_map_is_single_valued(map);
7628 isl_map_free(map);
7630 return sv;
7633 int isl_set_is_singleton(__isl_keep isl_set *set)
7635 return isl_map_is_single_valued((isl_map *)set);
7638 int isl_map_is_translation(__isl_keep isl_map *map)
7640 int ok;
7641 isl_set *delta;
7643 delta = isl_map_deltas(isl_map_copy(map));
7644 ok = isl_set_is_singleton(delta);
7645 isl_set_free(delta);
7647 return ok;
7650 static int unique(isl_int *p, unsigned pos, unsigned len)
7652 if (isl_seq_first_non_zero(p, pos) != -1)
7653 return 0;
7654 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
7655 return 0;
7656 return 1;
7659 int isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
7661 int i, j;
7662 unsigned nvar;
7663 unsigned ovar;
7665 if (!bset)
7666 return -1;
7668 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
7669 return 0;
7671 nvar = isl_basic_set_dim(bset, isl_dim_set);
7672 ovar = isl_dim_offset(bset->dim, isl_dim_set);
7673 for (j = 0; j < nvar; ++j) {
7674 int lower = 0, upper = 0;
7675 for (i = 0; i < bset->n_eq; ++i) {
7676 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
7677 continue;
7678 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
7679 return 0;
7680 break;
7682 if (i < bset->n_eq)
7683 continue;
7684 for (i = 0; i < bset->n_ineq; ++i) {
7685 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
7686 continue;
7687 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
7688 return 0;
7689 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
7690 lower = 1;
7691 else
7692 upper = 1;
7694 if (!lower || !upper)
7695 return 0;
7698 return 1;
7701 int isl_set_is_box(__isl_keep isl_set *set)
7703 if (!set)
7704 return -1;
7705 if (set->n != 1)
7706 return 0;
7708 return isl_basic_set_is_box(set->p[0]);
7711 int isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
7713 if (!bset)
7714 return -1;
7716 return isl_dim_is_wrapping(bset->dim);
7719 int isl_set_is_wrapping(__isl_keep isl_set *set)
7721 if (!set)
7722 return -1;
7724 return isl_dim_is_wrapping(set->dim);
7727 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
7729 bmap = isl_basic_map_cow(bmap);
7730 if (!bmap)
7731 return NULL;
7733 bmap->dim = isl_dim_wrap(bmap->dim);
7734 if (!bmap->dim)
7735 goto error;
7737 bmap = isl_basic_map_finalize(bmap);
7739 return (isl_basic_set *)bmap;
7740 error:
7741 isl_basic_map_free(bmap);
7742 return NULL;
7745 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
7747 int i;
7749 map = isl_map_cow(map);
7750 if (!map)
7751 return NULL;
7753 for (i = 0; i < map->n; ++i) {
7754 map->p[i] = (isl_basic_map *)isl_basic_map_wrap(map->p[i]);
7755 if (!map->p[i])
7756 goto error;
7758 map->dim = isl_dim_wrap(map->dim);
7759 if (!map->dim)
7760 goto error;
7762 return (isl_set *)map;
7763 error:
7764 isl_map_free(map);
7765 return NULL;
7768 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
7770 bset = isl_basic_set_cow(bset);
7771 if (!bset)
7772 return NULL;
7774 bset->dim = isl_dim_unwrap(bset->dim);
7775 if (!bset->dim)
7776 goto error;
7778 bset = isl_basic_set_finalize(bset);
7780 return (isl_basic_map *)bset;
7781 error:
7782 isl_basic_set_free(bset);
7783 return NULL;
7786 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
7788 int i;
7790 if (!set)
7791 return NULL;
7793 if (!isl_set_is_wrapping(set))
7794 isl_die(set->ctx, isl_error_invalid, "not a wrapping set",
7795 goto error);
7797 set = isl_set_cow(set);
7798 if (!set)
7799 return NULL;
7801 for (i = 0; i < set->n; ++i) {
7802 set->p[i] = (isl_basic_set *)isl_basic_set_unwrap(set->p[i]);
7803 if (!set->p[i])
7804 goto error;
7807 set->dim = isl_dim_unwrap(set->dim);
7808 if (!set->dim)
7809 goto error;
7811 return (isl_map *)set;
7812 error:
7813 isl_set_free(set);
7814 return NULL;
7817 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
7818 enum isl_dim_type type)
7820 if (!bmap)
7821 return NULL;
7823 if (!isl_dim_is_named_or_nested(bmap->dim, type))
7824 return bmap;
7826 bmap = isl_basic_map_cow(bmap);
7827 if (!bmap)
7828 return NULL;
7830 bmap->dim = isl_dim_reset(bmap->dim, type);
7831 if (!bmap->dim)
7832 goto error;
7834 bmap = isl_basic_map_finalize(bmap);
7836 return bmap;
7837 error:
7838 isl_basic_map_free(bmap);
7839 return NULL;
7842 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
7843 enum isl_dim_type type)
7845 int i;
7847 if (!map)
7848 return NULL;
7850 if (!isl_dim_is_named_or_nested(map->dim, type))
7851 return map;
7853 map = isl_map_cow(map);
7854 if (!map)
7855 return NULL;
7857 for (i = 0; i < map->n; ++i) {
7858 map->p[i] = isl_basic_map_reset(map->p[i], type);
7859 if (!map->p[i])
7860 goto error;
7862 map->dim = isl_dim_reset(map->dim, type);
7863 if (!map->dim)
7864 goto error;
7866 return map;
7867 error:
7868 isl_map_free(map);
7869 return NULL;
7872 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
7874 if (!bmap)
7875 return NULL;
7877 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
7878 return bmap;
7880 bmap = isl_basic_map_cow(bmap);
7881 if (!bmap)
7882 return NULL;
7884 bmap->dim = isl_dim_flatten(bmap->dim);
7885 if (!bmap->dim)
7886 goto error;
7888 bmap = isl_basic_map_finalize(bmap);
7890 return bmap;
7891 error:
7892 isl_basic_map_free(bmap);
7893 return NULL;
7896 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
7898 return (isl_basic_set *)isl_basic_map_flatten((isl_basic_map *)bset);
7901 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
7903 int i;
7905 if (!map)
7906 return NULL;
7908 if (!map->dim->nested[0] && !map->dim->nested[1])
7909 return map;
7911 map = isl_map_cow(map);
7912 if (!map)
7913 return NULL;
7915 for (i = 0; i < map->n; ++i) {
7916 map->p[i] = isl_basic_map_flatten(map->p[i]);
7917 if (!map->p[i])
7918 goto error;
7920 map->dim = isl_dim_flatten(map->dim);
7921 if (!map->dim)
7922 goto error;
7924 return map;
7925 error:
7926 isl_map_free(map);
7927 return NULL;
7930 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
7932 return (isl_set *)isl_map_flatten((isl_map *)set);
7935 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
7937 isl_dim *dim, *flat_dim;
7938 isl_map *map;
7940 dim = isl_set_get_dim(set);
7941 flat_dim = isl_dim_flatten(isl_dim_copy(dim));
7942 map = map_identity(isl_dim_join(isl_dim_reverse(dim), flat_dim));
7943 map = isl_map_intersect_domain(map, set);
7945 return map;
7948 /* Extend the given dim_map with mappings for the divs in bmap.
7950 static __isl_give struct isl_dim_map *extend_dim_map(
7951 __isl_keep struct isl_dim_map *dim_map,
7952 __isl_keep isl_basic_map *bmap)
7954 int i;
7955 struct isl_dim_map *res;
7956 int offset;
7958 offset = isl_basic_map_offset(bmap, isl_dim_div);
7960 res = isl_dim_map_alloc(bmap->ctx, dim_map->len - 1 + bmap->n_div);
7961 if (!res)
7962 return NULL;
7964 for (i = 0; i < dim_map->len; ++i)
7965 res->pos[i] = dim_map->pos[i];
7966 for (i = 0; i < bmap->n_div; ++i)
7967 res->pos[dim_map->len + i] = offset + i;
7969 return res;
7972 /* Extract a dim_map from a reordering.
7973 * We essentially need to reverse the mapping, and add an offset
7974 * of 1 for the constant term.
7976 __isl_give struct isl_dim_map *isl_dim_map_from_reordering(
7977 __isl_keep isl_reordering *exp)
7979 int i;
7980 struct isl_dim_map *dim_map;
7982 if (!exp)
7983 return NULL;
7985 dim_map = isl_dim_map_alloc(exp->dim->ctx, isl_dim_total(exp->dim));
7986 if (!dim_map)
7987 return NULL;
7989 for (i = 0; i < exp->len; ++i)
7990 dim_map->pos[1 + exp->pos[i]] = 1 + i;
7992 return dim_map;
7995 /* Reorder the dimensions of "bmap" according to the given dim_map
7996 * and set the dimension specification to "dim".
7998 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
7999 __isl_take isl_dim *dim, __isl_take struct isl_dim_map *dim_map)
8001 isl_basic_map *res;
8003 bmap = isl_basic_map_cow(bmap);
8004 if (!bmap || !dim || !dim_map)
8005 goto error;
8007 res = isl_basic_map_alloc_dim(dim,
8008 bmap->n_div, bmap->n_eq, bmap->n_ineq);
8009 res = add_constraints_dim_map(res, bmap, dim_map);
8010 res = isl_basic_map_finalize(res);
8011 return res;
8012 error:
8013 free(dim_map);
8014 isl_basic_map_free(bmap);
8015 isl_dim_free(dim);
8016 return NULL;
8019 /* Reorder the dimensions of "map" according to given reordering.
8021 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
8022 __isl_take isl_reordering *r)
8024 int i;
8025 struct isl_dim_map *dim_map;
8027 map = isl_map_cow(map);
8028 dim_map = isl_dim_map_from_reordering(r);
8029 if (!map || !r || !dim_map)
8030 goto error;
8032 for (i = 0; i < map->n; ++i) {
8033 struct isl_dim_map *dim_map_i;
8035 dim_map_i = extend_dim_map(dim_map, map->p[i]);
8037 map->p[i] = isl_basic_map_realign(map->p[i],
8038 isl_dim_copy(r->dim), dim_map_i);
8040 if (!map->p[i])
8041 goto error;
8044 map = isl_map_reset_dim(map, isl_dim_copy(r->dim));
8046 isl_reordering_free(r);
8047 free(dim_map);
8048 return map;
8049 error:
8050 free(dim_map);
8051 isl_map_free(map);
8052 isl_reordering_free(r);
8053 return NULL;
8056 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
8057 __isl_take isl_reordering *r)
8059 return (isl_set *)isl_map_realign((isl_map *)set, r);
8062 __isl_give isl_mat *isl_basic_map_equalities_matrix(
8063 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
8064 enum isl_dim_type c2, enum isl_dim_type c3,
8065 enum isl_dim_type c4, enum isl_dim_type c5)
8067 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
8068 struct isl_mat *mat;
8069 int i, j, k;
8070 int pos;
8072 if (!bmap)
8073 return NULL;
8074 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
8075 isl_basic_map_total_dim(bmap) + 1);
8076 if (!mat)
8077 return NULL;
8078 for (i = 0; i < bmap->n_eq; ++i)
8079 for (j = 0, pos = 0; j < 5; ++j) {
8080 int off = isl_basic_map_offset(bmap, c[j]);
8081 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
8082 isl_int_set(mat->row[i][pos],
8083 bmap->eq[i][off + k]);
8084 ++pos;
8088 return mat;
8091 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
8092 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
8093 enum isl_dim_type c2, enum isl_dim_type c3,
8094 enum isl_dim_type c4, enum isl_dim_type c5)
8096 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
8097 struct isl_mat *mat;
8098 int i, j, k;
8099 int pos;
8101 if (!bmap)
8102 return NULL;
8103 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
8104 isl_basic_map_total_dim(bmap) + 1);
8105 if (!mat)
8106 return NULL;
8107 for (i = 0; i < bmap->n_ineq; ++i)
8108 for (j = 0, pos = 0; j < 5; ++j) {
8109 int off = isl_basic_map_offset(bmap, c[j]);
8110 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
8111 isl_int_set(mat->row[i][pos],
8112 bmap->ineq[i][off + k]);
8113 ++pos;
8117 return mat;
8120 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
8121 __isl_take isl_dim *dim,
8122 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
8123 enum isl_dim_type c2, enum isl_dim_type c3,
8124 enum isl_dim_type c4, enum isl_dim_type c5)
8126 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
8127 isl_basic_map *bmap;
8128 unsigned total;
8129 unsigned extra;
8130 int i, j, k, l;
8131 int pos;
8133 if (!dim || !eq || !ineq)
8134 goto error;
8136 if (eq->n_col != ineq->n_col)
8137 isl_die(dim->ctx, isl_error_invalid,
8138 "equalities and inequalities matrices should have "
8139 "same number of columns", goto error);
8141 total = 1 + isl_dim_total(dim);
8143 if (eq->n_col < total)
8144 isl_die(dim->ctx, isl_error_invalid,
8145 "number of columns too small", goto error);
8147 extra = eq->n_col - total;
8149 bmap = isl_basic_map_alloc_dim(isl_dim_copy(dim), extra,
8150 eq->n_row, ineq->n_row);
8151 if (!bmap)
8152 goto error;
8153 for (i = 0; i < extra; ++i) {
8154 k = isl_basic_map_alloc_div(bmap);
8155 if (k < 0)
8156 goto error;
8157 isl_int_set_si(bmap->div[k][0], 0);
8159 for (i = 0; i < eq->n_row; ++i) {
8160 l = isl_basic_map_alloc_equality(bmap);
8161 if (l < 0)
8162 goto error;
8163 for (j = 0, pos = 0; j < 5; ++j) {
8164 int off = isl_basic_map_offset(bmap, c[j]);
8165 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
8166 isl_int_set(bmap->eq[l][off + k],
8167 eq->row[i][pos]);
8168 ++pos;
8172 for (i = 0; i < ineq->n_row; ++i) {
8173 l = isl_basic_map_alloc_inequality(bmap);
8174 if (l < 0)
8175 goto error;
8176 for (j = 0, pos = 0; j < 5; ++j) {
8177 int off = isl_basic_map_offset(bmap, c[j]);
8178 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
8179 isl_int_set(bmap->ineq[l][off + k],
8180 ineq->row[i][pos]);
8181 ++pos;
8186 isl_dim_free(dim);
8187 isl_mat_free(eq);
8188 isl_mat_free(ineq);
8190 return bmap;
8191 error:
8192 isl_dim_free(dim);
8193 isl_mat_free(eq);
8194 isl_mat_free(ineq);
8195 return NULL;
8198 __isl_give isl_mat *isl_basic_set_equalities_matrix(
8199 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
8200 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
8202 return isl_basic_map_equalities_matrix((isl_basic_map *)bset,
8203 c1, c2, c3, c4, isl_dim_in);
8206 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
8207 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
8208 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
8210 return isl_basic_map_inequalities_matrix((isl_basic_map *)bset,
8211 c1, c2, c3, c4, isl_dim_in);
8214 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
8215 __isl_take isl_dim *dim,
8216 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
8217 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
8219 return (isl_basic_set*)
8220 isl_basic_map_from_constraint_matrices(dim, eq, ineq,
8221 c1, c2, c3, c4, isl_dim_in);