isl_tab_pip.c: ignore dead columns when checking integrality
[isl.git] / isl_map.c
blob583ce6ec42a9a1b1abcafb3ff46510d245647918
1 #include <string.h>
2 #include <strings.h>
3 #include "isl_ctx.h"
4 #include "isl_blk.h"
5 #include "isl_dim.h"
6 #include "isl_equalities.h"
7 #include "isl_list.h"
8 #include "isl_lp.h"
9 #include "isl_seq.h"
10 #include "isl_set.h"
11 #include "isl_map.h"
12 #include "isl_map_private.h"
13 #include "isl_map_piplib.h"
14 #include "isl_sample.h"
15 #include "isl_tab.h"
16 #include "isl_vec.h"
18 /* Maps dst positions to src positions */
19 struct isl_dim_map {
20 unsigned len;
21 int pos[1];
24 static struct isl_dim_map *isl_dim_map_alloc(struct isl_ctx *ctx, unsigned len)
26 int i;
27 struct isl_dim_map *dim_map;
28 dim_map = isl_alloc(ctx, struct isl_dim_map,
29 sizeof(struct isl_dim_map) + len * sizeof(int));
30 if (!dim_map)
31 return NULL;
32 dim_map->len = 1 + len;
33 dim_map->pos[0] = 0;
34 for (i = 0; i < len; ++i)
35 dim_map->pos[1 + i] = -1;
36 return dim_map;
39 static unsigned n(struct isl_dim *dim, enum isl_dim_type type)
41 switch (type) {
42 case isl_dim_param: return dim->nparam;
43 case isl_dim_in: return dim->n_in;
44 case isl_dim_out: return dim->n_out;
45 case isl_dim_all: return dim->nparam + dim->n_in + dim->n_out;
49 static unsigned pos(struct isl_dim *dim, enum isl_dim_type type)
51 switch (type) {
52 case isl_dim_param: return 1;
53 case isl_dim_in: return 1 + dim->nparam;
54 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
58 static void isl_dim_map_dim(struct isl_dim_map *dim_map, struct isl_dim *dim,
59 enum isl_dim_type type, unsigned dst_pos)
61 int i;
62 unsigned src_pos;
64 if (!dim_map || !dim)
65 return;
67 src_pos = pos(dim, type);
68 for (i = 0; i < n(dim, type); ++i)
69 dim_map->pos[1 + dst_pos + i] = src_pos + i;
72 static void isl_dim_map_div(struct isl_dim_map *dim_map,
73 struct isl_basic_map *bmap, unsigned dst_pos)
75 int i;
76 unsigned src_pos;
78 if (!dim_map || !bmap)
79 return;
81 src_pos = 1 + isl_dim_total(bmap->dim);
82 for (i = 0; i < bmap->n_div; ++i)
83 dim_map->pos[1 + dst_pos + i] = src_pos + i;
86 static void isl_dim_map_dump(struct isl_dim_map *dim_map)
88 int i;
90 for (i = 0; i < dim_map->len; ++i)
91 fprintf(stderr, "%d -> %d; ", i, dim_map->pos[i]);
92 fprintf(stderr, "\n");
95 unsigned isl_basic_map_dim(const struct isl_basic_map *bmap,
96 enum isl_dim_type type)
98 switch (type) {
99 case isl_dim_param:
100 case isl_dim_in:
101 case isl_dim_out: return isl_dim_size(bmap->dim, type);
102 case isl_dim_div: return bmap->n_div;
103 case isl_dim_all: return isl_basic_map_total_dim(bmap);
107 unsigned isl_map_dim(const struct isl_map *map, enum isl_dim_type type)
109 return n(map->dim, type);
112 unsigned isl_set_dim(const struct isl_set *set, enum isl_dim_type type)
114 return n(set->dim, type);
117 unsigned isl_basic_map_offset(struct isl_basic_map *bmap,
118 enum isl_dim_type type)
120 struct isl_dim *dim = bmap->dim;
121 switch (type) {
122 case isl_dim_param: return 1;
123 case isl_dim_in: return 1 + dim->nparam;
124 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
125 case isl_dim_div: return 1 + dim->nparam + dim->n_in + dim->n_out;
129 static unsigned map_offset(struct isl_map *map, enum isl_dim_type type)
131 return pos(map->dim, type);
134 unsigned isl_basic_set_dim(const struct isl_basic_set *bset,
135 enum isl_dim_type type)
137 return isl_basic_map_dim((const struct isl_basic_map*)bset, type);
140 unsigned isl_basic_set_n_dim(const struct isl_basic_set *bset)
142 return bset->dim->n_out;
145 unsigned isl_basic_set_n_param(const struct isl_basic_set *bset)
147 return bset->dim->nparam;
150 unsigned isl_basic_set_total_dim(const struct isl_basic_set *bset)
152 return isl_dim_total(bset->dim) + bset->n_div;
155 unsigned isl_set_n_dim(const struct isl_set *set)
157 return set->dim->n_out;
160 unsigned isl_set_n_param(const struct isl_set *set)
162 return set->dim->nparam;
165 unsigned isl_basic_map_n_in(const struct isl_basic_map *bmap)
167 return bmap->dim->n_in;
170 unsigned isl_basic_map_n_out(const struct isl_basic_map *bmap)
172 return bmap->dim->n_out;
175 unsigned isl_basic_map_n_param(const struct isl_basic_map *bmap)
177 return bmap->dim->nparam;
180 unsigned isl_basic_map_n_div(const struct isl_basic_map *bmap)
182 return bmap->n_div;
185 unsigned isl_basic_map_total_dim(const struct isl_basic_map *bmap)
187 return isl_dim_total(bmap->dim) + bmap->n_div;
190 unsigned isl_map_n_in(const struct isl_map *map)
192 return map->dim->n_in;
195 unsigned isl_map_n_out(const struct isl_map *map)
197 return map->dim->n_out;
200 unsigned isl_map_n_param(const struct isl_map *map)
202 return map->dim->nparam;
205 int isl_map_compatible_domain(struct isl_map *map, struct isl_set *set)
207 return map->dim->n_in == set->dim->n_out &&
208 map->dim->nparam == set->dim->nparam;
211 int isl_basic_map_compatible_domain(struct isl_basic_map *bmap,
212 struct isl_basic_set *bset)
214 return bmap->dim->n_in == bset->dim->n_out &&
215 bmap->dim->nparam == bset->dim->nparam;
218 int isl_basic_map_compatible_range(struct isl_basic_map *bmap,
219 struct isl_basic_set *bset)
221 return bmap->dim->n_out == bset->dim->n_out &&
222 bmap->dim->nparam == bset->dim->nparam;
225 struct isl_dim *isl_basic_map_get_dim(struct isl_basic_map *bmap)
227 if (!bmap)
228 return NULL;
229 return isl_dim_copy(bmap->dim);
232 struct isl_dim *isl_basic_set_get_dim(struct isl_basic_set *bset)
234 if (!bset)
235 return NULL;
236 return isl_dim_copy(bset->dim);
239 struct isl_dim *isl_map_get_dim(struct isl_map *map)
241 if (!map)
242 return NULL;
243 return isl_dim_copy(map->dim);
246 struct isl_dim *isl_set_get_dim(struct isl_set *set)
248 if (!set)
249 return NULL;
250 return isl_dim_copy(set->dim);
253 static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
254 struct isl_basic_map *bmap, unsigned extra,
255 unsigned n_eq, unsigned n_ineq)
257 int i;
258 size_t row_size = 1 + isl_dim_total(bmap->dim) + extra;
260 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
261 if (isl_blk_is_error(bmap->block)) {
262 free(bmap);
263 return NULL;
266 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
267 if (!bmap->ineq) {
268 isl_blk_free(ctx, bmap->block);
269 free(bmap);
270 return NULL;
273 if (extra == 0) {
274 bmap->block2 = isl_blk_empty();
275 bmap->div = NULL;
276 } else {
277 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
278 if (isl_blk_is_error(bmap->block2)) {
279 free(bmap->ineq);
280 isl_blk_free(ctx, bmap->block);
281 free(bmap);
282 return NULL;
285 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
286 if (!bmap->div) {
287 isl_blk_free(ctx, bmap->block2);
288 free(bmap->ineq);
289 isl_blk_free(ctx, bmap->block);
290 free(bmap);
291 return NULL;
295 for (i = 0; i < n_ineq + n_eq; ++i)
296 bmap->ineq[i] = bmap->block.data + i * row_size;
298 for (i = 0; i < extra; ++i)
299 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
301 bmap->ctx = ctx;
302 isl_ctx_ref(ctx);
303 bmap->ref = 1;
304 bmap->flags = 0;
305 bmap->c_size = n_eq + n_ineq;
306 bmap->eq = bmap->ineq + n_ineq;
307 bmap->extra = extra;
308 bmap->n_eq = 0;
309 bmap->n_ineq = 0;
310 bmap->n_div = 0;
311 bmap->sample = NULL;
313 return bmap;
314 error:
315 isl_basic_map_free(bmap);
316 return NULL;
319 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
320 unsigned nparam, unsigned dim, unsigned extra,
321 unsigned n_eq, unsigned n_ineq)
323 struct isl_basic_map *bmap;
324 bmap = isl_basic_map_alloc(ctx, nparam, 0, dim, extra, n_eq, n_ineq);
325 return (struct isl_basic_set *)bmap;
328 struct isl_basic_set *isl_basic_set_alloc_dim(struct isl_dim *dim,
329 unsigned extra, unsigned n_eq, unsigned n_ineq)
331 struct isl_basic_map *bmap;
332 if (!dim)
333 return NULL;
334 isl_assert(dim->ctx, dim->n_in == 0, return NULL);
335 bmap = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
336 return (struct isl_basic_set *)bmap;
339 struct isl_basic_map *isl_basic_map_alloc_dim(struct isl_dim *dim,
340 unsigned extra, unsigned n_eq, unsigned n_ineq)
342 struct isl_basic_map *bmap;
344 if (!dim)
345 return NULL;
346 bmap = isl_alloc_type(dim->ctx, struct isl_basic_map);
347 if (!bmap)
348 goto error;
349 bmap->dim = dim;
351 return basic_map_init(dim->ctx, bmap, extra, n_eq, n_ineq);
352 error:
353 isl_dim_free(dim);
354 return NULL;
357 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
358 unsigned nparam, unsigned in, unsigned out, unsigned extra,
359 unsigned n_eq, unsigned n_ineq)
361 struct isl_basic_map *bmap;
362 struct isl_dim *dim;
364 dim = isl_dim_alloc(ctx, nparam, in, out);
365 if (!dim)
366 return NULL;
368 bmap = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
369 return bmap;
372 static void dup_constraints(
373 struct isl_basic_map *dst, struct isl_basic_map *src)
375 int i;
376 unsigned total = isl_basic_map_total_dim(src);
378 for (i = 0; i < src->n_eq; ++i) {
379 int j = isl_basic_map_alloc_equality(dst);
380 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
383 for (i = 0; i < src->n_ineq; ++i) {
384 int j = isl_basic_map_alloc_inequality(dst);
385 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
388 for (i = 0; i < src->n_div; ++i) {
389 int j = isl_basic_map_alloc_div(dst);
390 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
392 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
395 struct isl_basic_map *isl_basic_map_dup(struct isl_basic_map *bmap)
397 struct isl_basic_map *dup;
399 if (!bmap)
400 return NULL;
401 dup = isl_basic_map_alloc_dim(isl_dim_copy(bmap->dim),
402 bmap->n_div, bmap->n_eq, bmap->n_ineq);
403 if (!dup)
404 return NULL;
405 dup_constraints(dup, bmap);
406 dup->flags = bmap->flags;
407 dup->sample = isl_vec_copy(bmap->sample);
408 return dup;
411 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
413 struct isl_basic_map *dup;
415 dup = isl_basic_map_dup((struct isl_basic_map *)bset);
416 return (struct isl_basic_set *)dup;
419 struct isl_basic_set *isl_basic_set_copy(struct isl_basic_set *bset)
421 if (!bset)
422 return NULL;
424 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
425 bset->ref++;
426 return bset;
428 return isl_basic_set_dup(bset);
431 struct isl_set *isl_set_copy(struct isl_set *set)
433 if (!set)
434 return NULL;
436 set->ref++;
437 return set;
440 struct isl_basic_map *isl_basic_map_copy(struct isl_basic_map *bmap)
442 if (!bmap)
443 return NULL;
445 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
446 bmap->ref++;
447 return bmap;
449 return isl_basic_map_dup(bmap);
452 struct isl_map *isl_map_copy(struct isl_map *map)
454 if (!map)
455 return NULL;
457 map->ref++;
458 return map;
461 void isl_basic_map_free(struct isl_basic_map *bmap)
463 if (!bmap)
464 return;
466 if (--bmap->ref > 0)
467 return;
469 isl_ctx_deref(bmap->ctx);
470 free(bmap->div);
471 isl_blk_free(bmap->ctx, bmap->block2);
472 free(bmap->ineq);
473 isl_blk_free(bmap->ctx, bmap->block);
474 isl_vec_free(bmap->sample);
475 isl_dim_free(bmap->dim);
476 free(bmap);
479 void isl_basic_set_free(struct isl_basic_set *bset)
481 isl_basic_map_free((struct isl_basic_map *)bset);
484 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
486 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
489 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
491 struct isl_ctx *ctx;
492 if (!bmap)
493 return -1;
494 ctx = bmap->ctx;
495 isl_assert(ctx, room_for_con(bmap, 1), return -1);
496 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
497 return -1);
498 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
499 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
500 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
501 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
502 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
503 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
504 isl_int *t;
505 int j = isl_basic_map_alloc_inequality(bmap);
506 if (j < 0)
507 return -1;
508 t = bmap->ineq[j];
509 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
510 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
511 bmap->eq[-1] = t;
512 bmap->n_eq++;
513 bmap->n_ineq--;
514 bmap->eq--;
515 return 0;
517 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
518 bmap->extra - bmap->n_div);
519 return bmap->n_eq++;
522 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
524 return isl_basic_map_alloc_equality((struct isl_basic_map *)bset);
527 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
529 if (!bmap)
530 return -1;
531 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
532 bmap->n_eq -= n;
533 return 0;
536 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
538 return isl_basic_map_free_equality((struct isl_basic_map *)bset, n);
541 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
543 isl_int *t;
544 if (!bmap)
545 return -1;
546 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
548 if (pos != bmap->n_eq - 1) {
549 t = bmap->eq[pos];
550 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
551 bmap->eq[bmap->n_eq - 1] = t;
553 bmap->n_eq--;
554 return 0;
557 int isl_basic_set_drop_equality(struct isl_basic_set *bset, unsigned pos)
559 return isl_basic_map_drop_equality((struct isl_basic_map *)bset, pos);
562 void isl_basic_map_inequality_to_equality(
563 struct isl_basic_map *bmap, unsigned pos)
565 isl_int *t;
567 t = bmap->ineq[pos];
568 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
569 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
570 bmap->eq[-1] = t;
571 bmap->n_eq++;
572 bmap->n_ineq--;
573 bmap->eq--;
574 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
575 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
576 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
577 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
580 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
582 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
585 int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap)
587 struct isl_ctx *ctx;
588 if (!bmap)
589 return -1;
590 ctx = bmap->ctx;
591 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
592 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
593 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
594 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
595 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
596 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
597 1 + isl_basic_map_total_dim(bmap),
598 bmap->extra - bmap->n_div);
599 return bmap->n_ineq++;
602 int isl_basic_set_alloc_inequality(struct isl_basic_set *bset)
604 return isl_basic_map_alloc_inequality((struct isl_basic_map *)bset);
607 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
609 if (!bmap)
610 return -1;
611 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
612 bmap->n_ineq -= n;
613 return 0;
616 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
618 return isl_basic_map_free_inequality((struct isl_basic_map *)bset, n);
621 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
623 isl_int *t;
624 if (!bmap)
625 return -1;
626 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
628 if (pos != bmap->n_ineq - 1) {
629 t = bmap->ineq[pos];
630 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
631 bmap->ineq[bmap->n_ineq - 1] = t;
632 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
634 bmap->n_ineq--;
635 return 0;
638 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
640 return isl_basic_map_drop_inequality((struct isl_basic_map *)bset, pos);
643 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
644 isl_int *eq)
646 int k;
648 bset = isl_basic_set_extend_constraints(bset, 1, 0);
649 if (!bset)
650 return NULL;
651 k = isl_basic_set_alloc_equality(bset);
652 if (k < 0)
653 goto error;
654 isl_seq_cpy(bset->eq[k], eq, 1 + isl_basic_set_total_dim(bset));
655 return bset;
656 error:
657 isl_basic_set_free(bset);
658 return NULL;
661 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
662 isl_int *ineq)
664 int k;
666 bset = isl_basic_set_extend_constraints(bset, 0, 1);
667 if (!bset)
668 return NULL;
669 k = isl_basic_set_alloc_inequality(bset);
670 if (k < 0)
671 goto error;
672 isl_seq_cpy(bset->ineq[k], ineq, 1 + isl_basic_set_total_dim(bset));
673 return bset;
674 error:
675 isl_basic_set_free(bset);
676 return NULL;
679 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
681 if (!bmap)
682 return -1;
683 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
684 isl_seq_clr(bmap->div[bmap->n_div] +
685 1 + 1 + isl_basic_map_total_dim(bmap),
686 bmap->extra - bmap->n_div);
687 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
688 return bmap->n_div++;
691 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
693 return isl_basic_map_alloc_div((struct isl_basic_map *)bset);
696 int isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
698 if (!bmap)
699 return -1;
700 isl_assert(bmap->ctx, n <= bmap->n_div, return -1);
701 bmap->n_div -= n;
702 return 0;
705 int isl_basic_set_free_div(struct isl_basic_set *bset, unsigned n)
707 return isl_basic_map_free_div((struct isl_basic_map *)bset, n);
710 /* Copy constraint from src to dst, putting the vars of src at offset
711 * dim_off in dst and the divs of src at offset div_off in dst.
712 * If both sets are actually map, then dim_off applies to the input
713 * variables.
715 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
716 struct isl_basic_map *src_map, isl_int *src,
717 unsigned in_off, unsigned out_off, unsigned div_off)
719 unsigned src_nparam = isl_basic_map_n_param(src_map);
720 unsigned dst_nparam = isl_basic_map_n_param(dst_map);
721 unsigned src_in = isl_basic_map_n_in(src_map);
722 unsigned dst_in = isl_basic_map_n_in(dst_map);
723 unsigned src_out = isl_basic_map_n_out(src_map);
724 unsigned dst_out = isl_basic_map_n_out(dst_map);
725 isl_int_set(dst[0], src[0]);
726 isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
727 if (dst_nparam > src_nparam)
728 isl_seq_clr(dst+1+src_nparam,
729 dst_nparam - src_nparam);
730 isl_seq_clr(dst+1+dst_nparam, in_off);
731 isl_seq_cpy(dst+1+dst_nparam+in_off,
732 src+1+src_nparam,
733 isl_min(dst_in-in_off, src_in));
734 if (dst_in-in_off > src_in)
735 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
736 dst_in - in_off - src_in);
737 isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
738 isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
739 src+1+src_nparam+src_in,
740 isl_min(dst_out-out_off, src_out));
741 if (dst_out-out_off > src_out)
742 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
743 dst_out - out_off - src_out);
744 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
745 isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
746 src+1+src_nparam+src_in+src_out,
747 isl_min(dst_map->extra-div_off, src_map->n_div));
748 if (dst_map->n_div-div_off > src_map->n_div)
749 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
750 div_off+src_map->n_div,
751 dst_map->n_div - div_off - src_map->n_div);
754 static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
755 struct isl_basic_map *src_map, isl_int *src,
756 unsigned in_off, unsigned out_off, unsigned div_off)
758 isl_int_set(dst[0], src[0]);
759 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
762 static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
763 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
765 int i;
766 unsigned div_off;
768 if (!bmap1 || !bmap2)
769 goto error;
771 div_off = bmap1->n_div;
773 for (i = 0; i < bmap2->n_eq; ++i) {
774 int i1 = isl_basic_map_alloc_equality(bmap1);
775 if (i1 < 0)
776 goto error;
777 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
778 i_pos, o_pos, div_off);
781 for (i = 0; i < bmap2->n_ineq; ++i) {
782 int i1 = isl_basic_map_alloc_inequality(bmap1);
783 if (i1 < 0)
784 goto error;
785 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
786 i_pos, o_pos, div_off);
789 for (i = 0; i < bmap2->n_div; ++i) {
790 int i1 = isl_basic_map_alloc_div(bmap1);
791 if (i1 < 0)
792 goto error;
793 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
794 i_pos, o_pos, div_off);
797 isl_basic_map_free(bmap2);
799 return bmap1;
801 error:
802 isl_basic_map_free(bmap1);
803 isl_basic_map_free(bmap2);
804 return NULL;
807 static void copy_constraint_dim_map(isl_int *dst, isl_int *src,
808 struct isl_dim_map *dim_map)
810 int i;
812 for (i = 0; i < dim_map->len; ++i) {
813 if (dim_map->pos[i] < 0)
814 isl_int_set_si(dst[i], 0);
815 else
816 isl_int_set(dst[i], src[dim_map->pos[i]]);
820 static void copy_div_dim_map(isl_int *dst, isl_int *src,
821 struct isl_dim_map *dim_map)
823 isl_int_set(dst[0], src[0]);
824 copy_constraint_dim_map(dst+1, src+1, dim_map);
827 static struct isl_basic_map *add_constraints_dim_map(struct isl_basic_map *dst,
828 struct isl_basic_map *src, struct isl_dim_map *dim_map)
830 int i;
832 if (!src || !dst || !dim_map)
833 goto error;
835 for (i = 0; i < src->n_eq; ++i) {
836 int i1 = isl_basic_map_alloc_equality(dst);
837 if (i1 < 0)
838 goto error;
839 copy_constraint_dim_map(dst->eq[i1], src->eq[i], dim_map);
842 for (i = 0; i < src->n_ineq; ++i) {
843 int i1 = isl_basic_map_alloc_inequality(dst);
844 if (i1 < 0)
845 goto error;
846 copy_constraint_dim_map(dst->ineq[i1], src->ineq[i], dim_map);
849 for (i = 0; i < src->n_div; ++i) {
850 int i1 = isl_basic_map_alloc_div(dst);
851 if (i1 < 0)
852 goto error;
853 copy_div_dim_map(dst->div[i1], src->div[i], dim_map);
856 free(dim_map);
857 isl_basic_map_free(src);
859 return dst;
860 error:
861 free(dim_map);
862 isl_basic_map_free(src);
863 isl_basic_map_free(dst);
864 return NULL;
867 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
868 struct isl_basic_set *bset2, unsigned pos)
870 return (struct isl_basic_set *)
871 add_constraints((struct isl_basic_map *)bset1,
872 (struct isl_basic_map *)bset2, 0, pos);
875 struct isl_basic_map *isl_basic_map_extend_dim(struct isl_basic_map *base,
876 struct isl_dim *dim, unsigned extra,
877 unsigned n_eq, unsigned n_ineq)
879 struct isl_basic_map *ext;
880 unsigned flags;
881 int dims_ok;
883 if (!dim)
884 goto error;
886 if (!base)
887 goto error;
889 dims_ok = isl_dim_equal(base->dim, dim) &&
890 base->extra >= base->n_div + extra;
892 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
893 room_for_ineq(base, n_ineq)) {
894 isl_dim_free(dim);
895 return base;
898 isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error);
899 isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error);
900 isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error);
901 extra += base->extra;
902 n_eq += base->n_eq;
903 n_ineq += base->n_ineq;
905 ext = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
906 dim = NULL;
907 if (!ext)
908 goto error;
910 if (dims_ok)
911 ext->sample = isl_vec_copy(base->sample);
912 flags = base->flags;
913 ext = add_constraints(ext, base, 0, 0);
914 if (ext) {
915 ext->flags = flags;
916 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
919 return ext;
921 error:
922 isl_dim_free(dim);
923 isl_basic_map_free(base);
924 return NULL;
927 struct isl_basic_set *isl_basic_set_extend_dim(struct isl_basic_set *base,
928 struct isl_dim *dim, unsigned extra,
929 unsigned n_eq, unsigned n_ineq)
931 return (struct isl_basic_set *)
932 isl_basic_map_extend_dim((struct isl_basic_map *)base, dim,
933 extra, n_eq, n_ineq);
936 struct isl_basic_map *isl_basic_map_extend_constraints(
937 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
939 if (!base)
940 return NULL;
941 return isl_basic_map_extend_dim(base, isl_dim_copy(base->dim),
942 0, n_eq, n_ineq);
945 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
946 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
947 unsigned n_eq, unsigned n_ineq)
949 struct isl_basic_map *bmap;
950 struct isl_dim *dim;
952 if (!base)
953 return NULL;
954 dim = isl_dim_alloc(base->ctx, nparam, n_in, n_out);
955 if (!dim)
956 return NULL;
958 bmap = isl_basic_map_extend_dim(base, dim, extra, n_eq, n_ineq);
959 return bmap;
962 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
963 unsigned nparam, unsigned dim, unsigned extra,
964 unsigned n_eq, unsigned n_ineq)
966 return (struct isl_basic_set *)
967 isl_basic_map_extend((struct isl_basic_map *)base,
968 nparam, 0, dim, extra, n_eq, n_ineq);
971 struct isl_basic_set *isl_basic_set_extend_constraints(
972 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
974 return (struct isl_basic_set *)
975 isl_basic_map_extend_constraints((struct isl_basic_map *)base,
976 n_eq, n_ineq);
979 struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset)
981 return (struct isl_basic_set *)
982 isl_basic_map_cow((struct isl_basic_map *)bset);
985 struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
987 if (!bmap)
988 return NULL;
990 if (bmap->ref > 1) {
991 bmap->ref--;
992 bmap = isl_basic_map_dup(bmap);
994 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
995 return bmap;
998 struct isl_set *isl_set_cow(struct isl_set *set)
1000 if (!set)
1001 return NULL;
1003 if (set->ref == 1)
1004 return set;
1005 set->ref--;
1006 return isl_set_dup(set);
1009 struct isl_map *isl_map_cow(struct isl_map *map)
1011 if (!map)
1012 return NULL;
1014 if (map->ref == 1)
1015 return map;
1016 map->ref--;
1017 return isl_map_dup(map);
1020 static void swap_vars(struct isl_blk blk, isl_int *a,
1021 unsigned a_len, unsigned b_len)
1023 isl_seq_cpy(blk.data, a+a_len, b_len);
1024 isl_seq_cpy(blk.data+b_len, a, a_len);
1025 isl_seq_cpy(a, blk.data, b_len+a_len);
1028 struct isl_basic_set *isl_basic_set_swap_vars(
1029 struct isl_basic_set *bset, unsigned n)
1031 int i;
1032 struct isl_blk blk;
1033 unsigned dim;
1034 unsigned nparam;
1036 if (!bset)
1037 goto error;
1039 nparam = isl_basic_set_n_param(bset);
1040 dim = isl_basic_set_n_dim(bset);
1041 isl_assert(bset->ctx, n <= dim, goto error);
1043 if (n == dim)
1044 return bset;
1046 bset = isl_basic_set_cow(bset);
1047 if (!bset)
1048 return NULL;
1050 blk = isl_blk_alloc(bset->ctx, dim);
1051 if (isl_blk_is_error(blk))
1052 goto error;
1054 for (i = 0; i < bset->n_eq; ++i)
1055 swap_vars(blk,
1056 bset->eq[i]+1+nparam, n, dim - n);
1058 for (i = 0; i < bset->n_ineq; ++i)
1059 swap_vars(blk,
1060 bset->ineq[i]+1+nparam, n, dim - n);
1062 for (i = 0; i < bset->n_div; ++i)
1063 swap_vars(blk,
1064 bset->div[i]+1+1+nparam, n, dim - n);
1066 isl_blk_free(bset->ctx, blk);
1068 ISL_F_CLR(bset, ISL_BASIC_SET_NORMALIZED);
1069 return bset;
1071 error:
1072 isl_basic_set_free(bset);
1073 return NULL;
1076 struct isl_set *isl_set_swap_vars(struct isl_set *set, unsigned n)
1078 int i;
1079 set = isl_set_cow(set);
1080 if (!set)
1081 return NULL;
1083 for (i = 0; i < set->n; ++i) {
1084 set->p[i] = isl_basic_set_swap_vars(set->p[i], n);
1085 if (!set->p[i]) {
1086 isl_set_free(set);
1087 return NULL;
1090 ISL_F_CLR(set, ISL_SET_NORMALIZED);
1091 return set;
1094 struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
1096 int i = 0;
1097 unsigned total;
1098 if (!bmap)
1099 goto error;
1100 total = isl_basic_map_total_dim(bmap);
1101 isl_basic_map_free_div(bmap, bmap->n_div);
1102 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1103 if (bmap->n_eq > 0)
1104 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
1105 else {
1106 isl_basic_map_alloc_equality(bmap);
1107 if (i < 0)
1108 goto error;
1110 isl_int_set_si(bmap->eq[i][0], 1);
1111 isl_seq_clr(bmap->eq[i]+1, total);
1112 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
1113 return isl_basic_map_finalize(bmap);
1114 error:
1115 isl_basic_map_free(bmap);
1116 return NULL;
1119 struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
1121 return (struct isl_basic_set *)
1122 isl_basic_map_set_to_empty((struct isl_basic_map *)bset);
1125 void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
1127 int i;
1128 unsigned off = isl_dim_total(bmap->dim);
1129 isl_int *t = bmap->div[a];
1130 bmap->div[a] = bmap->div[b];
1131 bmap->div[b] = t;
1133 for (i = 0; i < bmap->n_eq; ++i)
1134 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
1136 for (i = 0; i < bmap->n_ineq; ++i)
1137 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
1139 for (i = 0; i < bmap->n_div; ++i)
1140 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
1141 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1144 /* Eliminate the specified n dimensions starting at first from the
1145 * constraints using Fourier-Motzkin, The dimensions themselves
1146 * are not removed.
1148 struct isl_set *isl_set_eliminate_dims(struct isl_set *set,
1149 unsigned first, unsigned n)
1151 int i;
1152 unsigned nparam;
1154 if (!set)
1155 return NULL;
1156 if (n == 0)
1157 return set;
1159 set = isl_set_cow(set);
1160 if (!set)
1161 return NULL;
1162 isl_assert(set->ctx, first+n <= isl_set_n_dim(set), goto error);
1163 nparam = isl_set_n_param(set);
1165 for (i = 0; i < set->n; ++i) {
1166 set->p[i] = isl_basic_set_eliminate_vars(set->p[i],
1167 nparam + first, n);
1168 if (!set->p[i])
1169 goto error;
1171 return set;
1172 error:
1173 isl_set_free(set);
1174 return NULL;
1177 /* Project out n dimensions starting at first using Fourier-Motzkin */
1178 struct isl_set *isl_set_remove_dims(struct isl_set *set,
1179 unsigned first, unsigned n)
1181 set = isl_set_eliminate_dims(set, first, n);
1182 set = isl_set_drop_dims(set, first, n);
1183 return set;
1186 struct isl_basic_set *isl_basic_set_remove_divs(struct isl_basic_set *bset)
1188 bset = isl_basic_set_eliminate_vars(bset, isl_dim_total(bset->dim),
1189 bset->n_div);
1190 if (!bset)
1191 return NULL;
1192 bset->n_div = 0;
1193 return bset;
1196 struct isl_set *isl_set_remove_divs(struct isl_set *set)
1198 int i;
1200 if (!set)
1201 return NULL;
1202 if (set->n == 0)
1203 return set;
1205 set = isl_set_cow(set);
1206 if (!set)
1207 return NULL;
1209 for (i = 0; i < set->n; ++i) {
1210 set->p[i] = isl_basic_set_remove_divs(set->p[i]);
1211 if (!set->p[i])
1212 goto error;
1214 return set;
1215 error:
1216 isl_set_free(set);
1217 return NULL;
1220 struct isl_basic_map *isl_basic_map_remove(struct isl_basic_map *bmap,
1221 enum isl_dim_type type, unsigned first, unsigned n)
1223 if (!bmap)
1224 return NULL;
1225 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1226 goto error);
1227 if (n == 0)
1228 return bmap;
1229 bmap = isl_basic_map_eliminate_vars(bmap,
1230 isl_basic_map_offset(bmap, type) - 1 + first, n);
1231 if (!bmap)
1232 return bmap;
1233 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
1234 return bmap;
1235 bmap = isl_basic_map_drop(bmap, type, first, n);
1236 return bmap;
1237 error:
1238 isl_basic_map_free(bmap);
1239 return NULL;
1242 __isl_give isl_basic_set *isl_basic_set_remove(__isl_take isl_basic_set *bset,
1243 enum isl_dim_type type, unsigned first, unsigned n)
1245 return (isl_basic_set *)
1246 isl_basic_map_remove((isl_basic_map *)bset, type, first, n);
1249 struct isl_map *isl_map_remove(struct isl_map *map,
1250 enum isl_dim_type type, unsigned first, unsigned n)
1252 int i;
1254 if (n == 0)
1255 return map;
1257 map = isl_map_cow(map);
1258 if (!map)
1259 return NULL;
1260 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
1262 for (i = 0; i < map->n; ++i) {
1263 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
1264 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
1265 if (!map->p[i])
1266 goto error;
1268 map = isl_map_drop(map, type, first, n);
1269 return map;
1270 error:
1271 isl_map_free(map);
1272 return NULL;
1275 __isl_give isl_set *isl_set_remove(__isl_take isl_set *bset,
1276 enum isl_dim_type type, unsigned first, unsigned n)
1278 return (isl_set *)isl_map_remove((isl_map *)bset, type, first, n);
1281 /* Project out n inputs starting at first using Fourier-Motzkin */
1282 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
1283 unsigned first, unsigned n)
1285 return isl_map_remove(map, isl_dim_in, first, n);
1288 /* Project out n dimensions starting at first using Fourier-Motzkin */
1289 struct isl_basic_set *isl_basic_set_remove_dims(struct isl_basic_set *bset,
1290 unsigned first, unsigned n)
1292 unsigned nparam = isl_basic_set_n_param(bset);
1293 bset = isl_basic_set_eliminate_vars(bset, nparam + first, n);
1294 bset = isl_basic_set_drop_dims(bset, first, n);
1295 return bset;
1298 static void dump_term(struct isl_basic_map *bmap,
1299 isl_int c, int pos, FILE *out)
1301 const char *name;
1302 unsigned in = isl_basic_map_n_in(bmap);
1303 unsigned dim = in + isl_basic_map_n_out(bmap);
1304 unsigned nparam = isl_basic_map_n_param(bmap);
1305 if (!pos)
1306 isl_int_print(out, c, 0);
1307 else {
1308 if (!isl_int_is_one(c))
1309 isl_int_print(out, c, 0);
1310 if (pos < 1 + nparam) {
1311 name = isl_dim_get_name(bmap->dim,
1312 isl_dim_param, pos - 1);
1313 if (name)
1314 fprintf(out, "%s", name);
1315 else
1316 fprintf(out, "p%d", pos - 1);
1317 } else if (pos < 1 + nparam + in)
1318 fprintf(out, "i%d", pos - 1 - nparam);
1319 else if (pos < 1 + nparam + dim)
1320 fprintf(out, "o%d", pos - 1 - nparam - in);
1321 else
1322 fprintf(out, "e%d", pos - 1 - nparam - dim);
1326 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
1327 int sign, FILE *out)
1329 int i;
1330 int first;
1331 unsigned len = 1 + isl_basic_map_total_dim(bmap);
1332 isl_int v;
1334 isl_int_init(v);
1335 for (i = 0, first = 1; i < len; ++i) {
1336 if (isl_int_sgn(c[i]) * sign <= 0)
1337 continue;
1338 if (!first)
1339 fprintf(out, " + ");
1340 first = 0;
1341 isl_int_abs(v, c[i]);
1342 dump_term(bmap, v, i, out);
1344 isl_int_clear(v);
1345 if (first)
1346 fprintf(out, "0");
1349 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
1350 const char *op, FILE *out, int indent)
1352 int i;
1354 fprintf(out, "%*s", indent, "");
1356 dump_constraint_sign(bmap, c, 1, out);
1357 fprintf(out, " %s ", op);
1358 dump_constraint_sign(bmap, c, -1, out);
1360 fprintf(out, "\n");
1362 for (i = bmap->n_div; i < bmap->extra; ++i) {
1363 if (isl_int_is_zero(c[1+isl_dim_total(bmap->dim)+i]))
1364 continue;
1365 fprintf(out, "%*s", indent, "");
1366 fprintf(out, "ERROR: unused div coefficient not zero\n");
1367 abort();
1371 static void dump_constraints(struct isl_basic_map *bmap,
1372 isl_int **c, unsigned n,
1373 const char *op, FILE *out, int indent)
1375 int i;
1377 for (i = 0; i < n; ++i)
1378 dump_constraint(bmap, c[i], op, out, indent);
1381 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
1383 int j;
1384 int first = 1;
1385 unsigned total = isl_basic_map_total_dim(bmap);
1387 for (j = 0; j < 1 + total; ++j) {
1388 if (isl_int_is_zero(exp[j]))
1389 continue;
1390 if (!first && isl_int_is_pos(exp[j]))
1391 fprintf(out, "+");
1392 dump_term(bmap, exp[j], j, out);
1393 first = 0;
1397 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
1399 int i;
1401 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
1402 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
1404 for (i = 0; i < bmap->n_div; ++i) {
1405 fprintf(out, "%*s", indent, "");
1406 fprintf(out, "e%d = [(", i);
1407 dump_affine(bmap, bmap->div[i]+1, out);
1408 fprintf(out, ")/");
1409 isl_int_print(out, bmap->div[i][0], 0);
1410 fprintf(out, "]\n");
1414 void isl_basic_set_dump(struct isl_basic_set *bset, FILE *out, int indent)
1416 if (!bset) {
1417 fprintf(out, "null basic set\n");
1418 return;
1421 fprintf(out, "%*s", indent, "");
1422 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
1423 bset->ref, bset->dim->nparam, bset->dim->n_out,
1424 bset->extra, bset->flags);
1425 dump((struct isl_basic_map *)bset, out, indent);
1428 void isl_basic_map_dump(struct isl_basic_map *bmap, FILE *out, int indent)
1430 if (!bmap) {
1431 fprintf(out, "null basic map\n");
1432 return;
1435 fprintf(out, "%*s", indent, "");
1436 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
1437 "flags: %x, n_name: %d\n",
1438 bmap->ref,
1439 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
1440 bmap->extra, bmap->flags, bmap->dim->n_name);
1441 dump(bmap, out, indent);
1444 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
1446 unsigned total;
1447 if (!bmap)
1448 return -1;
1449 total = isl_basic_map_total_dim(bmap);
1450 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1451 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
1452 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
1453 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1454 return 0;
1457 struct isl_set *isl_set_alloc_dim(struct isl_dim *dim, int n, unsigned flags)
1459 struct isl_set *set;
1461 if (!dim)
1462 return NULL;
1463 isl_assert(dim->ctx, dim->n_in == 0, return NULL);
1464 isl_assert(dim->ctx, n >= 0, return NULL);
1465 set = isl_alloc(dim->ctx, struct isl_set,
1466 sizeof(struct isl_set) +
1467 (n - 1) * sizeof(struct isl_basic_set *));
1468 if (!set)
1469 goto error;
1471 set->ctx = dim->ctx;
1472 isl_ctx_ref(set->ctx);
1473 set->ref = 1;
1474 set->size = n;
1475 set->n = 0;
1476 set->dim = dim;
1477 set->flags = flags;
1478 return set;
1479 error:
1480 isl_dim_free(dim);
1481 return NULL;
1484 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
1485 unsigned nparam, unsigned dim, int n, unsigned flags)
1487 struct isl_set *set;
1488 struct isl_dim *dims;
1490 dims = isl_dim_alloc(ctx, nparam, 0, dim);
1491 if (!dims)
1492 return NULL;
1494 set = isl_set_alloc_dim(dims, n, flags);
1495 return set;
1498 /* Make sure "map" has room for at least "n" more basic maps.
1500 struct isl_map *isl_map_grow(struct isl_map *map, int n)
1502 int i;
1503 struct isl_map *grown = NULL;
1505 if (!map)
1506 return NULL;
1507 isl_assert(map->ctx, n >= 0, goto error);
1508 if (map->n + n <= map->size)
1509 return map;
1510 grown = isl_map_alloc_dim(isl_map_get_dim(map), map->n + n, map->flags);
1511 if (!grown)
1512 goto error;
1513 for (i = 0; i < map->n; ++i) {
1514 grown->p[i] = isl_basic_map_copy(map->p[i]);
1515 if (!grown->p[i])
1516 goto error;
1517 grown->n++;
1519 isl_map_free(map);
1520 return grown;
1521 error:
1522 isl_map_free(grown);
1523 isl_map_free(map);
1524 return NULL;
1527 /* Make sure "set" has room for at least "n" more basic sets.
1529 struct isl_set *isl_set_grow(struct isl_set *set, int n)
1531 return (struct isl_set *)isl_map_grow((struct isl_map *)set, n);
1534 struct isl_set *isl_set_dup(struct isl_set *set)
1536 int i;
1537 struct isl_set *dup;
1539 if (!set)
1540 return NULL;
1542 dup = isl_set_alloc_dim(isl_dim_copy(set->dim), set->n, set->flags);
1543 if (!dup)
1544 return NULL;
1545 for (i = 0; i < set->n; ++i)
1546 dup = isl_set_add(dup, isl_basic_set_copy(set->p[i]));
1547 return dup;
1550 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
1552 struct isl_set *set;
1554 if (!bset)
1555 return NULL;
1557 set = isl_set_alloc_dim(isl_dim_copy(bset->dim), 1, ISL_MAP_DISJOINT);
1558 if (!set) {
1559 isl_basic_set_free(bset);
1560 return NULL;
1562 return isl_set_add(set, bset);
1565 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
1567 struct isl_map *map;
1569 if (!bmap)
1570 return NULL;
1572 map = isl_map_alloc_dim(isl_dim_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
1573 if (!map) {
1574 isl_basic_map_free(bmap);
1575 return NULL;
1577 return isl_map_add(map, bmap);
1580 struct isl_set *isl_set_add(struct isl_set *set, struct isl_basic_set *bset)
1582 return (struct isl_set *)isl_map_add((struct isl_map *)set,
1583 (struct isl_basic_map *)bset);
1586 void isl_set_free(struct isl_set *set)
1588 int i;
1590 if (!set)
1591 return;
1593 if (--set->ref > 0)
1594 return;
1596 isl_ctx_deref(set->ctx);
1597 for (i = 0; i < set->n; ++i)
1598 isl_basic_set_free(set->p[i]);
1599 isl_dim_free(set->dim);
1600 free(set);
1603 void isl_set_dump(struct isl_set *set, FILE *out, int indent)
1605 int i;
1607 if (!set) {
1608 fprintf(out, "null set\n");
1609 return;
1612 fprintf(out, "%*s", indent, "");
1613 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
1614 set->ref, set->n, set->dim->nparam, set->dim->n_out,
1615 set->flags);
1616 for (i = 0; i < set->n; ++i) {
1617 fprintf(out, "%*s", indent, "");
1618 fprintf(out, "basic set %d:\n", i);
1619 isl_basic_set_dump(set->p[i], out, indent+4);
1623 void isl_map_dump(struct isl_map *map, FILE *out, int indent)
1625 int i;
1627 if (!map) {
1628 fprintf(out, "null map\n");
1629 return;
1632 fprintf(out, "%*s", indent, "");
1633 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
1634 "flags: %x, n_name: %d\n",
1635 map->ref, map->n, map->dim->nparam, map->dim->n_in,
1636 map->dim->n_out, map->flags, map->dim->n_name);
1637 for (i = 0; i < map->n; ++i) {
1638 fprintf(out, "%*s", indent, "");
1639 fprintf(out, "basic map %d:\n", i);
1640 isl_basic_map_dump(map->p[i], out, indent+4);
1644 struct isl_basic_map *isl_basic_map_intersect_domain(
1645 struct isl_basic_map *bmap, struct isl_basic_set *bset)
1647 struct isl_basic_map *bmap_domain;
1648 struct isl_dim *dim;
1650 if (!bmap || !bset)
1651 goto error;
1653 isl_assert(bset->ctx, isl_dim_match(bmap->dim, isl_dim_param,
1654 bset->dim, isl_dim_param), goto error);
1656 if (isl_dim_size(bset->dim, isl_dim_set) != 0)
1657 isl_assert(bset->ctx,
1658 isl_basic_map_compatible_domain(bmap, bset), goto error);
1660 bmap = isl_basic_map_cow(bmap);
1661 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
1662 bset->n_div, bset->n_eq, bset->n_ineq);
1663 if (!bmap)
1664 goto error;
1665 dim = isl_dim_reverse(isl_dim_copy(bset->dim));
1666 bmap_domain = isl_basic_map_from_basic_set(bset, dim);
1667 bmap = add_constraints(bmap, bmap_domain, 0, 0);
1669 bmap = isl_basic_map_simplify(bmap);
1670 return isl_basic_map_finalize(bmap);
1671 error:
1672 isl_basic_map_free(bmap);
1673 isl_basic_set_free(bset);
1674 return NULL;
1677 struct isl_basic_map *isl_basic_map_intersect_range(
1678 struct isl_basic_map *bmap, struct isl_basic_set *bset)
1680 struct isl_basic_map *bmap_range;
1682 if (!bmap || !bset)
1683 goto error;
1685 isl_assert(bset->ctx, isl_dim_match(bmap->dim, isl_dim_param,
1686 bset->dim, isl_dim_param), goto error);
1688 if (isl_dim_size(bset->dim, isl_dim_set) != 0)
1689 isl_assert(bset->ctx,
1690 isl_basic_map_compatible_range(bmap, bset), goto error);
1692 bmap = isl_basic_map_cow(bmap);
1693 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
1694 bset->n_div, bset->n_eq, bset->n_ineq);
1695 if (!bmap)
1696 goto error;
1697 bmap_range = isl_basic_map_from_basic_set(bset, isl_dim_copy(bset->dim));
1698 bmap = add_constraints(bmap, bmap_range, 0, 0);
1700 bmap = isl_basic_map_simplify(bmap);
1701 return isl_basic_map_finalize(bmap);
1702 error:
1703 isl_basic_map_free(bmap);
1704 isl_basic_set_free(bset);
1705 return NULL;
1708 static int basic_map_contains(struct isl_basic_map *bmap, struct isl_vec *vec)
1710 int i;
1711 unsigned total;
1712 isl_int s;
1714 total = 1 + isl_basic_map_total_dim(bmap);
1715 if (total != vec->size)
1716 return -1;
1718 isl_int_init(s);
1720 for (i = 0; i < bmap->n_eq; ++i) {
1721 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
1722 if (!isl_int_is_zero(s)) {
1723 isl_int_clear(s);
1724 return 0;
1728 for (i = 0; i < bmap->n_ineq; ++i) {
1729 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
1730 if (isl_int_is_neg(s)) {
1731 isl_int_clear(s);
1732 return 0;
1736 isl_int_clear(s);
1738 return 1;
1741 int isl_basic_set_contains(struct isl_basic_set *bset, struct isl_vec *vec)
1743 return basic_map_contains((struct isl_basic_map *)bset, vec);
1746 struct isl_basic_map *isl_basic_map_intersect(
1747 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
1749 struct isl_vec *sample = NULL;
1751 if (!bmap1 || !bmap2)
1752 goto error;
1754 isl_assert(bmap1->ctx, isl_dim_match(bmap1->dim, isl_dim_param,
1755 bmap2->dim, isl_dim_param), goto error);
1756 if (isl_dim_total(bmap1->dim) ==
1757 isl_dim_size(bmap1->dim, isl_dim_param) &&
1758 isl_dim_total(bmap2->dim) !=
1759 isl_dim_size(bmap2->dim, isl_dim_param))
1760 return isl_basic_map_intersect(bmap2, bmap1);
1762 if (isl_dim_total(bmap2->dim) !=
1763 isl_dim_size(bmap2->dim, isl_dim_param))
1764 isl_assert(bmap1->ctx,
1765 isl_dim_equal(bmap1->dim, bmap2->dim), goto error);
1767 if (bmap1->sample &&
1768 basic_map_contains(bmap1, bmap1->sample) > 0 &&
1769 basic_map_contains(bmap2, bmap1->sample) > 0)
1770 sample = isl_vec_copy(bmap1->sample);
1771 else if (bmap2->sample &&
1772 basic_map_contains(bmap1, bmap2->sample) > 0 &&
1773 basic_map_contains(bmap2, bmap2->sample) > 0)
1774 sample = isl_vec_copy(bmap2->sample);
1776 bmap1 = isl_basic_map_cow(bmap1);
1777 bmap1 = isl_basic_map_extend_dim(bmap1, isl_dim_copy(bmap1->dim),
1778 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
1779 if (!bmap1)
1780 goto error;
1781 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
1783 if (sample) {
1784 isl_vec_free(bmap1->sample);
1785 bmap1->sample = sample;
1788 bmap1 = isl_basic_map_simplify(bmap1);
1789 return isl_basic_map_finalize(bmap1);
1790 error:
1791 if (sample)
1792 isl_vec_free(sample);
1793 isl_basic_map_free(bmap1);
1794 isl_basic_map_free(bmap2);
1795 return NULL;
1798 struct isl_basic_set *isl_basic_set_intersect(
1799 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
1801 return (struct isl_basic_set *)
1802 isl_basic_map_intersect(
1803 (struct isl_basic_map *)bset1,
1804 (struct isl_basic_map *)bset2);
1807 struct isl_map *isl_map_intersect(struct isl_map *map1, struct isl_map *map2)
1809 unsigned flags = 0;
1810 struct isl_map *result;
1811 int i, j;
1813 if (!map1 || !map2)
1814 goto error;
1816 isl_assert(map1->ctx, isl_dim_match(map1->dim, isl_dim_param,
1817 map2->dim, isl_dim_param), goto error);
1818 if (isl_dim_total(map1->dim) ==
1819 isl_dim_size(map1->dim, isl_dim_param) &&
1820 isl_dim_total(map2->dim) != isl_dim_size(map2->dim, isl_dim_param))
1821 return isl_map_intersect(map2, map1);
1823 if (isl_dim_total(map2->dim) != isl_dim_size(map2->dim, isl_dim_param))
1824 isl_assert(map1->ctx,
1825 isl_dim_equal(map1->dim, map2->dim), goto error);
1827 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
1828 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
1829 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
1831 result = isl_map_alloc_dim(isl_dim_copy(map1->dim),
1832 map1->n * map2->n, flags);
1833 if (!result)
1834 goto error;
1835 for (i = 0; i < map1->n; ++i)
1836 for (j = 0; j < map2->n; ++j) {
1837 struct isl_basic_map *part;
1838 part = isl_basic_map_intersect(
1839 isl_basic_map_copy(map1->p[i]),
1840 isl_basic_map_copy(map2->p[j]));
1841 if (isl_basic_map_is_empty(part))
1842 isl_basic_map_free(part);
1843 else
1844 result = isl_map_add(result, part);
1845 if (!result)
1846 goto error;
1848 isl_map_free(map1);
1849 isl_map_free(map2);
1850 return result;
1851 error:
1852 isl_map_free(map1);
1853 isl_map_free(map2);
1854 return NULL;
1857 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
1859 return (struct isl_set *)
1860 isl_map_intersect((struct isl_map *)set1,
1861 (struct isl_map *)set2);
1864 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
1866 struct isl_dim *dim;
1867 struct isl_basic_set *bset;
1868 unsigned in;
1870 if (!bmap)
1871 return NULL;
1872 bmap = isl_basic_map_cow(bmap);
1873 if (!bmap)
1874 return NULL;
1875 dim = isl_dim_reverse(isl_dim_copy(bmap->dim));
1876 in = isl_basic_map_n_in(bmap);
1877 bset = isl_basic_set_from_basic_map(bmap);
1878 bset = isl_basic_set_swap_vars(bset, in);
1879 return isl_basic_map_from_basic_set(bset, dim);
1882 /* Turn final n dimensions into existentially quantified variables.
1884 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
1885 enum isl_dim_type type, unsigned first, unsigned n)
1887 int i;
1888 size_t row_size;
1889 isl_int **new_div;
1890 isl_int *old;
1892 if (!bset)
1893 return NULL;
1895 isl_assert(bset->ctx, type == isl_dim_set, goto error);
1896 isl_assert(bset->ctx, first + n == isl_basic_set_n_dim(bset), goto error);
1898 if (n == 0)
1899 return bset;
1901 if (ISL_F_ISSET(bset, ISL_BASIC_SET_RATIONAL))
1902 return isl_basic_set_remove(bset, type, first, n);
1904 bset = isl_basic_set_cow(bset);
1906 row_size = 1 + isl_dim_total(bset->dim) + bset->extra;
1907 old = bset->block2.data;
1908 bset->block2 = isl_blk_extend(bset->ctx, bset->block2,
1909 (bset->extra + n) * (1 + row_size));
1910 if (!bset->block2.data)
1911 goto error;
1912 new_div = isl_alloc_array(ctx, isl_int *, bset->extra + n);
1913 if (!new_div)
1914 goto error;
1915 for (i = 0; i < n; ++i) {
1916 new_div[i] = bset->block2.data +
1917 (bset->extra + i) * (1 + row_size);
1918 isl_seq_clr(new_div[i], 1 + row_size);
1920 for (i = 0; i < bset->extra; ++i)
1921 new_div[n + i] = bset->block2.data + (bset->div[i] - old);
1922 free(bset->div);
1923 bset->div = new_div;
1924 bset->n_div += n;
1925 bset->extra += n;
1926 bset->dim = isl_dim_drop_outputs(bset->dim,
1927 isl_basic_set_n_dim(bset) - n, n);
1928 if (!bset->dim)
1929 goto error;
1930 bset = isl_basic_set_simplify(bset);
1931 bset = isl_basic_set_drop_redundant_divs(bset);
1932 return isl_basic_set_finalize(bset);
1933 error:
1934 isl_basic_set_free(bset);
1935 return NULL;
1938 /* Turn final n dimensions into existentially quantified variables.
1940 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
1941 enum isl_dim_type type, unsigned first, unsigned n)
1943 int i;
1945 if (!set)
1946 return NULL;
1948 isl_assert(set->ctx, type == isl_dim_set, goto error);
1949 isl_assert(set->ctx, first + n == isl_set_n_dim(set), goto error);
1951 if (n == 0)
1952 return set;
1954 set = isl_set_cow(set);
1955 if (!set)
1956 goto error;
1957 set->dim = isl_dim_drop_outputs(set->dim, first, n);
1958 for (i = 0; i < set->n; ++i) {
1959 set->p[i] = isl_basic_set_project_out(set->p[i], type, first, n);
1960 if (!set->p[i])
1961 goto error;
1964 ISL_F_CLR(set, ISL_SET_NORMALIZED);
1965 return set;
1966 error:
1967 isl_set_free(set);
1968 return NULL;
1971 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
1973 int i, j;
1975 for (i = 0; i < n; ++i) {
1976 j = isl_basic_map_alloc_div(bmap);
1977 if (j < 0)
1978 goto error;
1979 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
1981 return bmap;
1982 error:
1983 isl_basic_map_free(bmap);
1984 return NULL;
1987 struct isl_basic_map *isl_basic_map_apply_range(
1988 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
1990 struct isl_dim *dim_result = NULL;
1991 struct isl_basic_map *bmap;
1992 unsigned n_in, n_out, n, nparam, total, pos;
1993 struct isl_dim_map *dim_map1, *dim_map2;
1995 if (!bmap1 || !bmap2)
1996 goto error;
1998 dim_result = isl_dim_join(isl_dim_copy(bmap1->dim),
1999 isl_dim_copy(bmap2->dim));
2001 n_in = isl_basic_map_n_in(bmap1);
2002 n_out = isl_basic_map_n_out(bmap2);
2003 n = isl_basic_map_n_out(bmap1);
2004 nparam = isl_basic_map_n_param(bmap1);
2006 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
2007 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
2008 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
2009 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
2010 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
2011 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
2012 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
2013 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
2014 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
2015 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
2016 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
2018 bmap = isl_basic_map_alloc_dim(dim_result,
2019 bmap1->n_div + bmap2->n_div + n,
2020 bmap1->n_eq + bmap2->n_eq,
2021 bmap1->n_ineq + bmap2->n_ineq);
2022 bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
2023 bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
2024 bmap = add_divs(bmap, n);
2025 bmap = isl_basic_map_simplify(bmap);
2026 bmap = isl_basic_map_drop_redundant_divs(bmap);
2027 return isl_basic_map_finalize(bmap);
2028 error:
2029 isl_basic_map_free(bmap1);
2030 isl_basic_map_free(bmap2);
2031 return NULL;
2034 struct isl_basic_set *isl_basic_set_apply(
2035 struct isl_basic_set *bset, struct isl_basic_map *bmap)
2037 if (!bset || !bmap)
2038 goto error;
2040 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
2041 goto error);
2043 return (struct isl_basic_set *)
2044 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
2045 error:
2046 isl_basic_set_free(bset);
2047 isl_basic_map_free(bmap);
2048 return NULL;
2051 struct isl_basic_map *isl_basic_map_apply_domain(
2052 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2054 if (!bmap1 || !bmap2)
2055 goto error;
2057 isl_assert(bmap1->ctx,
2058 isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
2059 isl_assert(bmap1->ctx,
2060 isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
2061 goto error);
2063 bmap1 = isl_basic_map_reverse(bmap1);
2064 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
2065 return isl_basic_map_reverse(bmap1);
2066 error:
2067 isl_basic_map_free(bmap1);
2068 isl_basic_map_free(bmap2);
2069 return NULL;
2072 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
2073 * A \cap B -> f(A) + f(B)
2075 struct isl_basic_map *isl_basic_map_sum(
2076 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2078 unsigned n_in, n_out, nparam, total, pos;
2079 struct isl_basic_map *bmap = NULL;
2080 struct isl_dim_map *dim_map1, *dim_map2;
2081 int i;
2083 if (!bmap1 || !bmap2)
2084 goto error;
2086 isl_assert(bmap1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim),
2087 goto error);
2089 nparam = isl_basic_map_n_param(bmap1);
2090 n_in = isl_basic_map_n_in(bmap1);
2091 n_out = isl_basic_map_n_out(bmap1);
2093 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
2094 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
2095 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
2096 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
2097 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
2098 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
2099 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
2100 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
2101 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
2102 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
2103 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
2105 bmap = isl_basic_map_alloc_dim(isl_dim_copy(bmap1->dim),
2106 bmap1->n_div + bmap2->n_div + 2 * n_out,
2107 bmap1->n_eq + bmap2->n_eq + n_out,
2108 bmap1->n_ineq + bmap2->n_ineq);
2109 for (i = 0; i < n_out; ++i) {
2110 int j = isl_basic_map_alloc_equality(bmap);
2111 if (j < 0)
2112 goto error;
2113 isl_seq_clr(bmap->eq[j], 1+total);
2114 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
2115 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
2116 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
2118 bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
2119 bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
2120 bmap = add_divs(bmap, 2 * n_out);
2122 bmap = isl_basic_map_simplify(bmap);
2123 return isl_basic_map_finalize(bmap);
2124 error:
2125 isl_basic_map_free(bmap);
2126 isl_basic_map_free(bmap1);
2127 isl_basic_map_free(bmap2);
2128 return NULL;
2131 /* Given two maps A -> f(A) and B -> g(B), construct a map
2132 * A \cap B -> f(A) + f(B)
2134 struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
2136 struct isl_map *result;
2137 int i, j;
2139 if (!map1 || !map2)
2140 goto error;
2142 isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
2144 result = isl_map_alloc_dim(isl_dim_copy(map1->dim),
2145 map1->n * map2->n, 0);
2146 if (!result)
2147 goto error;
2148 for (i = 0; i < map1->n; ++i)
2149 for (j = 0; j < map2->n; ++j) {
2150 struct isl_basic_map *part;
2151 part = isl_basic_map_sum(
2152 isl_basic_map_copy(map1->p[i]),
2153 isl_basic_map_copy(map2->p[j]));
2154 if (isl_basic_map_is_empty(part))
2155 isl_basic_map_free(part);
2156 else
2157 result = isl_map_add(result, part);
2158 if (!result)
2159 goto error;
2161 isl_map_free(map1);
2162 isl_map_free(map2);
2163 return result;
2164 error:
2165 isl_map_free(map1);
2166 isl_map_free(map2);
2167 return NULL;
2170 /* Given a basic map A -> f(A), construct A -> -f(A).
2172 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
2174 int i, j;
2175 unsigned off, n;
2177 bmap = isl_basic_map_cow(bmap);
2178 if (!bmap)
2179 return NULL;
2181 n = isl_basic_map_dim(bmap, isl_dim_out);
2182 off = isl_basic_map_offset(bmap, isl_dim_out);
2183 for (i = 0; i < bmap->n_eq; ++i)
2184 for (j = 0; j < n; ++j)
2185 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
2186 for (i = 0; i < bmap->n_ineq; ++i)
2187 for (j = 0; j < n; ++j)
2188 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
2189 for (i = 0; i < bmap->n_div; ++i)
2190 for (j = 0; j < n; ++j)
2191 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
2192 return isl_basic_map_finalize(bmap);
2195 /* Given a map A -> f(A), construct A -> -f(A).
2197 struct isl_map *isl_map_neg(struct isl_map *map)
2199 int i;
2201 map = isl_map_cow(map);
2202 if (!map)
2203 return NULL;
2205 for (i = 0; i < map->n; ++i) {
2206 map->p[i] = isl_basic_map_neg(map->p[i]);
2207 if (!map->p[i])
2208 goto error;
2211 return map;
2212 error:
2213 isl_map_free(map);
2214 return NULL;
2217 /* Given a basic map A -> f(A) and an integer d, construct a basic map
2218 * A -> floor(f(A)/d).
2220 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
2221 isl_int d)
2223 unsigned n_in, n_out, nparam, total, pos;
2224 struct isl_basic_map *result = NULL;
2225 struct isl_dim_map *dim_map;
2226 int i;
2228 if (!bmap)
2229 return NULL;
2231 nparam = isl_basic_map_n_param(bmap);
2232 n_in = isl_basic_map_n_in(bmap);
2233 n_out = isl_basic_map_n_out(bmap);
2235 total = nparam + n_in + n_out + bmap->n_div + n_out;
2236 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2237 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
2238 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
2239 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
2240 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
2242 result = isl_basic_map_alloc_dim(isl_dim_copy(bmap->dim),
2243 bmap->n_div + n_out,
2244 bmap->n_eq, bmap->n_ineq + 2 * n_out);
2245 result = add_constraints_dim_map(result, bmap, dim_map);
2246 result = add_divs(result, n_out);
2247 for (i = 0; i < n_out; ++i) {
2248 int j;
2249 j = isl_basic_map_alloc_inequality(result);
2250 if (j < 0)
2251 goto error;
2252 isl_seq_clr(result->ineq[j], 1+total);
2253 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
2254 isl_int_set_si(result->ineq[j][1+pos+i], 1);
2255 j = isl_basic_map_alloc_inequality(result);
2256 if (j < 0)
2257 goto error;
2258 isl_seq_clr(result->ineq[j], 1+total);
2259 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
2260 isl_int_set_si(result->ineq[j][1+pos+i], -1);
2261 isl_int_sub_ui(result->ineq[j][0], d, 1);
2264 result = isl_basic_map_simplify(result);
2265 return isl_basic_map_finalize(result);
2266 error:
2267 isl_basic_map_free(result);
2268 return NULL;
2271 /* Given a map A -> f(A) and an integer d, construct a map
2272 * A -> floor(f(A)/d).
2274 struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
2276 int i;
2278 map = isl_map_cow(map);
2279 if (!map)
2280 return NULL;
2282 ISL_F_CLR(map, ISL_MAP_DISJOINT);
2283 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
2284 for (i = 0; i < map->n; ++i) {
2285 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
2286 if (!map->p[i])
2287 goto error;
2290 return map;
2291 error:
2292 isl_map_free(map);
2293 return NULL;
2296 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
2298 int i;
2299 unsigned nparam;
2300 unsigned n_in;
2302 i = isl_basic_map_alloc_equality(bmap);
2303 if (i < 0)
2304 goto error;
2305 nparam = isl_basic_map_n_param(bmap);
2306 n_in = isl_basic_map_n_in(bmap);
2307 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
2308 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
2309 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
2310 return isl_basic_map_finalize(bmap);
2311 error:
2312 isl_basic_map_free(bmap);
2313 return NULL;
2316 /* Add a constraints to "bmap" expressing i_pos < o_pos
2318 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
2320 int i;
2321 unsigned nparam;
2322 unsigned n_in;
2324 i = isl_basic_map_alloc_inequality(bmap);
2325 if (i < 0)
2326 goto error;
2327 nparam = isl_basic_map_n_param(bmap);
2328 n_in = isl_basic_map_n_in(bmap);
2329 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
2330 isl_int_set_si(bmap->ineq[i][0], -1);
2331 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
2332 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
2333 return isl_basic_map_finalize(bmap);
2334 error:
2335 isl_basic_map_free(bmap);
2336 return NULL;
2339 /* Add a constraints to "bmap" expressing i_pos > o_pos
2341 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
2343 int i;
2344 unsigned nparam;
2345 unsigned n_in;
2347 i = isl_basic_map_alloc_inequality(bmap);
2348 if (i < 0)
2349 goto error;
2350 nparam = isl_basic_map_n_param(bmap);
2351 n_in = isl_basic_map_n_in(bmap);
2352 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
2353 isl_int_set_si(bmap->ineq[i][0], -1);
2354 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
2355 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
2356 return isl_basic_map_finalize(bmap);
2357 error:
2358 isl_basic_map_free(bmap);
2359 return NULL;
2362 struct isl_basic_map *isl_basic_map_equal(struct isl_dim *dim, unsigned n_equal)
2364 int i;
2365 struct isl_basic_map *bmap;
2366 bmap = isl_basic_map_alloc_dim(dim, 0, n_equal, 0);
2367 if (!bmap)
2368 return NULL;
2369 for (i = 0; i < n_equal && bmap; ++i)
2370 bmap = var_equal(bmap, i);
2371 return isl_basic_map_finalize(bmap);
2374 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos < o_pos
2376 struct isl_basic_map *isl_basic_map_less_at(struct isl_dim *dim, unsigned pos)
2378 int i;
2379 struct isl_basic_map *bmap;
2380 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
2381 if (!bmap)
2382 return NULL;
2383 for (i = 0; i < pos && bmap; ++i)
2384 bmap = var_equal(bmap, i);
2385 if (bmap)
2386 bmap = var_less(bmap, pos);
2387 return isl_basic_map_finalize(bmap);
2390 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
2392 struct isl_basic_map *isl_basic_map_more_at(struct isl_dim *dim, unsigned pos)
2394 int i;
2395 struct isl_basic_map *bmap;
2396 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
2397 if (!bmap)
2398 return NULL;
2399 for (i = 0; i < pos && bmap; ++i)
2400 bmap = var_equal(bmap, i);
2401 if (bmap)
2402 bmap = var_more(bmap, pos);
2403 return isl_basic_map_finalize(bmap);
2406 static __isl_give isl_map *map_lex_lte(__isl_take isl_dim *dims, int equal)
2408 struct isl_map *map;
2409 unsigned dim;
2410 int i;
2412 if (!dims)
2413 return NULL;
2414 dim = dims->n_out;
2415 map = isl_map_alloc_dim(isl_dim_copy(dims), dim + equal, ISL_MAP_DISJOINT);
2417 for (i = 0; i < dim; ++i)
2418 map = isl_map_add(map,
2419 isl_basic_map_less_at(isl_dim_copy(dims), i));
2420 if (equal)
2421 map = isl_map_add(map,
2422 isl_basic_map_equal(isl_dim_copy(dims), dim));
2424 isl_dim_free(dims);
2425 return map;
2428 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_dim *set_dim)
2430 return map_lex_lte(isl_dim_map(set_dim), 0);
2433 __isl_give isl_map *isl_map_lex_le(__isl_take isl_dim *set_dim)
2435 return map_lex_lte(isl_dim_map(set_dim), 1);
2438 static __isl_give isl_map *map_lex_gte(__isl_take isl_dim *dims, int equal)
2440 struct isl_map *map;
2441 unsigned dim;
2442 int i;
2444 if (!dims)
2445 return NULL;
2446 dim = dims->n_out;
2447 map = isl_map_alloc_dim(isl_dim_copy(dims), dim + equal, ISL_MAP_DISJOINT);
2449 for (i = 0; i < dim; ++i)
2450 map = isl_map_add(map,
2451 isl_basic_map_more_at(isl_dim_copy(dims), i));
2452 if (equal)
2453 map = isl_map_add(map,
2454 isl_basic_map_equal(isl_dim_copy(dims), dim));
2456 isl_dim_free(dims);
2457 return map;
2460 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_dim *set_dim)
2462 return map_lex_gte(isl_dim_map(set_dim), 0);
2465 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_dim *set_dim)
2467 return map_lex_gte(isl_dim_map(set_dim), 1);
2470 struct isl_basic_map *isl_basic_map_from_basic_set(
2471 struct isl_basic_set *bset, struct isl_dim *dim)
2473 struct isl_basic_map *bmap;
2475 bset = isl_basic_set_cow(bset);
2476 if (!bset || !dim)
2477 goto error;
2479 isl_assert(bset->ctx, isl_dim_compatible(bset->dim, dim), goto error);
2480 isl_dim_free(bset->dim);
2481 bmap = (struct isl_basic_map *) bset;
2482 bmap->dim = dim;
2483 return isl_basic_map_finalize(bmap);
2484 error:
2485 isl_basic_set_free(bset);
2486 isl_dim_free(dim);
2487 return NULL;
2490 struct isl_basic_set *isl_basic_set_from_basic_map(struct isl_basic_map *bmap)
2492 if (!bmap)
2493 goto error;
2494 if (bmap->dim->n_in == 0)
2495 return (struct isl_basic_set *)bmap;
2496 bmap = isl_basic_map_cow(bmap);
2497 if (!bmap)
2498 goto error;
2499 bmap->dim = isl_dim_cow(bmap->dim);
2500 if (!bmap->dim)
2501 goto error;
2502 bmap->dim->n_out += bmap->dim->n_in;
2503 bmap->dim->n_in = 0;
2504 bmap = isl_basic_map_finalize(bmap);
2505 return (struct isl_basic_set *)bmap;
2506 error:
2507 isl_basic_map_free(bmap);
2508 return NULL;
2511 /* For a div d = floor(f/m), add the constraints
2513 * f - m d >= 0
2514 * -(f-(n-1)) + m d >= 0
2516 * Note that the second constraint is the negation of
2518 * f - m d >= n
2520 static int add_div_constraints(struct isl_basic_map *bmap, unsigned div)
2522 int i, j;
2523 unsigned total = isl_basic_map_total_dim(bmap);
2524 unsigned div_pos = 1 + total - bmap->n_div + div;
2526 i = isl_basic_map_alloc_inequality(bmap);
2527 if (i < 0)
2528 return -1;
2529 isl_seq_cpy(bmap->ineq[i], bmap->div[div]+1, 1+total);
2530 isl_int_neg(bmap->ineq[i][div_pos], bmap->div[div][0]);
2532 j = isl_basic_map_alloc_inequality(bmap);
2533 if (j < 0)
2534 return -1;
2535 isl_seq_neg(bmap->ineq[j], bmap->ineq[i], 1 + total);
2536 isl_int_add(bmap->ineq[j][0], bmap->ineq[j][0], bmap->ineq[j][div_pos]);
2537 isl_int_sub_ui(bmap->ineq[j][0], bmap->ineq[j][0], 1);
2538 return j;
2541 struct isl_basic_set *isl_basic_map_underlying_set(
2542 struct isl_basic_map *bmap)
2544 if (!bmap)
2545 goto error;
2546 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 && bmap->n_div == 0)
2547 return (struct isl_basic_set *)bmap;
2548 bmap = isl_basic_map_cow(bmap);
2549 if (!bmap)
2550 goto error;
2551 bmap->dim = isl_dim_underlying(bmap->dim, bmap->n_div);
2552 if (!bmap->dim)
2553 goto error;
2554 bmap->extra -= bmap->n_div;
2555 bmap->n_div = 0;
2556 bmap = isl_basic_map_finalize(bmap);
2557 return (struct isl_basic_set *)bmap;
2558 error:
2559 return NULL;
2562 __isl_give isl_basic_set *isl_basic_set_underlying_set(
2563 __isl_take isl_basic_set *bset)
2565 return isl_basic_map_underlying_set((isl_basic_map *)bset);
2568 struct isl_basic_map *isl_basic_map_overlying_set(
2569 struct isl_basic_set *bset, struct isl_basic_map *like)
2571 struct isl_basic_map *bmap;
2572 struct isl_ctx *ctx;
2573 unsigned total;
2574 int i;
2576 if (!bset || !like)
2577 goto error;
2578 ctx = bset->ctx;
2579 isl_assert(ctx, bset->n_div == 0, goto error);
2580 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
2581 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
2582 goto error);
2583 if (isl_dim_equal(bset->dim, like->dim) && like->n_div == 0) {
2584 isl_basic_map_free(like);
2585 return (struct isl_basic_map *)bset;
2587 bset = isl_basic_set_cow(bset);
2588 if (!bset)
2589 goto error;
2590 total = bset->dim->n_out + bset->extra;
2591 bmap = (struct isl_basic_map *)bset;
2592 isl_dim_free(bmap->dim);
2593 bmap->dim = isl_dim_copy(like->dim);
2594 if (!bmap->dim)
2595 goto error;
2596 bmap->n_div = like->n_div;
2597 bmap->extra += like->n_div;
2598 if (bmap->extra) {
2599 unsigned ltotal;
2600 ltotal = total - bmap->extra + like->extra;
2601 if (ltotal > total)
2602 ltotal = total;
2603 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
2604 bmap->extra * (1 + 1 + total));
2605 if (isl_blk_is_error(bmap->block2))
2606 goto error;
2607 bmap->div = isl_realloc_array(ctx, bmap->div, isl_int *,
2608 bmap->extra);
2609 if (!bmap->div)
2610 goto error;
2611 for (i = 0; i < bmap->extra; ++i)
2612 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
2613 for (i = 0; i < like->n_div; ++i) {
2614 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
2615 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
2617 bmap = isl_basic_map_extend_constraints(bmap,
2618 0, 2 * like->n_div);
2619 for (i = 0; i < like->n_div; ++i) {
2620 if (isl_int_is_zero(bmap->div[i][0]))
2621 continue;
2622 if (add_div_constraints(bmap, i) < 0)
2623 goto error;
2626 isl_basic_map_free(like);
2627 bmap = isl_basic_map_simplify(bmap);
2628 bmap = isl_basic_map_finalize(bmap);
2629 return bmap;
2630 error:
2631 isl_basic_map_free(like);
2632 isl_basic_set_free(bset);
2633 return NULL;
2636 struct isl_basic_set *isl_basic_set_from_underlying_set(
2637 struct isl_basic_set *bset, struct isl_basic_set *like)
2639 return (struct isl_basic_set *)
2640 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
2643 struct isl_set *isl_set_from_underlying_set(
2644 struct isl_set *set, struct isl_basic_set *like)
2646 int i;
2648 if (!set || !like)
2649 goto error;
2650 isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
2651 goto error);
2652 if (isl_dim_equal(set->dim, like->dim) && like->n_div == 0) {
2653 isl_basic_set_free(like);
2654 return set;
2656 set = isl_set_cow(set);
2657 if (!set)
2658 goto error;
2659 for (i = 0; i < set->n; ++i) {
2660 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
2661 isl_basic_set_copy(like));
2662 if (!set->p[i])
2663 goto error;
2665 isl_dim_free(set->dim);
2666 set->dim = isl_dim_copy(like->dim);
2667 if (!set->dim)
2668 goto error;
2669 isl_basic_set_free(like);
2670 return set;
2671 error:
2672 isl_basic_set_free(like);
2673 isl_set_free(set);
2674 return NULL;
2677 struct isl_set *isl_map_underlying_set(struct isl_map *map)
2679 int i;
2681 map = isl_map_cow(map);
2682 if (!map)
2683 return NULL;
2684 map->dim = isl_dim_cow(map->dim);
2685 if (!map->dim)
2686 goto error;
2688 for (i = 1; i < map->n; ++i)
2689 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
2690 goto error);
2691 for (i = 0; i < map->n; ++i) {
2692 map->p[i] = (struct isl_basic_map *)
2693 isl_basic_map_underlying_set(map->p[i]);
2694 if (!map->p[i])
2695 goto error;
2697 if (map->n == 0)
2698 map->dim = isl_dim_underlying(map->dim, 0);
2699 else {
2700 isl_dim_free(map->dim);
2701 map->dim = isl_dim_copy(map->p[0]->dim);
2703 if (!map->dim)
2704 goto error;
2705 return (struct isl_set *)map;
2706 error:
2707 isl_map_free(map);
2708 return NULL;
2711 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
2713 return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
2716 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
2718 struct isl_basic_set *domain;
2719 unsigned n_in;
2720 unsigned n_out;
2721 if (!bmap)
2722 return NULL;
2723 n_in = isl_basic_map_n_in(bmap);
2724 n_out = isl_basic_map_n_out(bmap);
2725 domain = isl_basic_set_from_basic_map(bmap);
2726 return isl_basic_set_project_out(domain, isl_dim_set, n_in, n_out);
2729 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
2731 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
2734 struct isl_set *isl_map_range(struct isl_map *map)
2736 int i;
2737 struct isl_set *set;
2739 if (!map)
2740 goto error;
2741 map = isl_map_cow(map);
2742 if (!map)
2743 goto error;
2745 set = (struct isl_set *) map;
2746 if (set->dim->n_in != 0) {
2747 set->dim = isl_dim_drop_inputs(set->dim, 0, set->dim->n_in);
2748 if (!set->dim)
2749 goto error;
2751 for (i = 0; i < map->n; ++i) {
2752 set->p[i] = isl_basic_map_range(map->p[i]);
2753 if (!set->p[i])
2754 goto error;
2756 ISL_F_CLR(set, ISL_MAP_DISJOINT);
2757 ISL_F_CLR(set, ISL_SET_NORMALIZED);
2758 return set;
2759 error:
2760 isl_map_free(map);
2761 return NULL;
2764 struct isl_map *isl_map_from_set(struct isl_set *set, struct isl_dim *dim)
2766 int i;
2767 struct isl_map *map = NULL;
2769 set = isl_set_cow(set);
2770 if (!set || !dim)
2771 goto error;
2772 isl_assert(set->ctx, isl_dim_compatible(set->dim, dim), goto error);
2773 map = (struct isl_map *)set;
2774 for (i = 0; i < set->n; ++i) {
2775 map->p[i] = isl_basic_map_from_basic_set(
2776 set->p[i], isl_dim_copy(dim));
2777 if (!map->p[i])
2778 goto error;
2780 isl_dim_free(map->dim);
2781 map->dim = dim;
2782 return map;
2783 error:
2784 isl_dim_free(dim);
2785 isl_set_free(set);
2786 return NULL;
2789 struct isl_map *isl_map_from_range(struct isl_set *set)
2791 return (struct isl_map *)set;
2794 struct isl_set *isl_set_from_map(struct isl_map *map)
2796 int i;
2797 struct isl_set *set = NULL;
2799 if (!map)
2800 return NULL;
2801 map = isl_map_cow(map);
2802 if (!map)
2803 return NULL;
2804 map->dim = isl_dim_cow(map->dim);
2805 if (!map->dim)
2806 goto error;
2807 map->dim->n_out += map->dim->n_in;
2808 map->dim->n_in = 0;
2809 set = (struct isl_set *)map;
2810 for (i = 0; i < map->n; ++i) {
2811 set->p[i] = isl_basic_set_from_basic_map(map->p[i]);
2812 if (!set->p[i])
2813 goto error;
2815 return set;
2816 error:
2817 isl_map_free(map);
2818 return NULL;
2821 struct isl_map *isl_map_alloc_dim(struct isl_dim *dim, int n, unsigned flags)
2823 struct isl_map *map;
2825 if (!dim)
2826 return NULL;
2827 isl_assert(dim->ctx, n >= 0, return NULL);
2828 map = isl_alloc(dim->ctx, struct isl_map,
2829 sizeof(struct isl_map) +
2830 (n - 1) * sizeof(struct isl_basic_map *));
2831 if (!map)
2832 goto error;
2834 map->ctx = dim->ctx;
2835 isl_ctx_ref(map->ctx);
2836 map->ref = 1;
2837 map->size = n;
2838 map->n = 0;
2839 map->dim = dim;
2840 map->flags = flags;
2841 return map;
2842 error:
2843 isl_dim_free(dim);
2844 return NULL;
2847 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
2848 unsigned nparam, unsigned in, unsigned out, int n,
2849 unsigned flags)
2851 struct isl_map *map;
2852 struct isl_dim *dims;
2854 dims = isl_dim_alloc(ctx, nparam, in, out);
2855 if (!dims)
2856 return NULL;
2858 map = isl_map_alloc_dim(dims, n, flags);
2859 return map;
2862 struct isl_basic_map *isl_basic_map_empty(struct isl_dim *dim)
2864 struct isl_basic_map *bmap;
2865 bmap = isl_basic_map_alloc_dim(dim, 0, 1, 0);
2866 bmap = isl_basic_map_set_to_empty(bmap);
2867 return bmap;
2870 struct isl_basic_set *isl_basic_set_empty(struct isl_dim *dim)
2872 struct isl_basic_set *bset;
2873 bset = isl_basic_set_alloc_dim(dim, 0, 1, 0);
2874 bset = isl_basic_set_set_to_empty(bset);
2875 return bset;
2878 struct isl_basic_map *isl_basic_map_empty_like(struct isl_basic_map *model)
2880 struct isl_basic_map *bmap;
2881 if (!model)
2882 return NULL;
2883 bmap = isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
2884 bmap = isl_basic_map_set_to_empty(bmap);
2885 return bmap;
2888 struct isl_basic_map *isl_basic_map_empty_like_map(struct isl_map *model)
2890 struct isl_basic_map *bmap;
2891 if (!model)
2892 return NULL;
2893 bmap = isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
2894 bmap = isl_basic_map_set_to_empty(bmap);
2895 return bmap;
2898 struct isl_basic_set *isl_basic_set_empty_like(struct isl_basic_set *model)
2900 struct isl_basic_set *bset;
2901 if (!model)
2902 return NULL;
2903 bset = isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
2904 bset = isl_basic_set_set_to_empty(bset);
2905 return bset;
2908 struct isl_basic_map *isl_basic_map_universe(struct isl_dim *dim)
2910 struct isl_basic_map *bmap;
2911 bmap = isl_basic_map_alloc_dim(dim, 0, 0, 0);
2912 return bmap;
2915 struct isl_basic_set *isl_basic_set_universe(struct isl_dim *dim)
2917 struct isl_basic_set *bset;
2918 bset = isl_basic_set_alloc_dim(dim, 0, 0, 0);
2919 return bset;
2922 __isl_give isl_basic_map *isl_basic_map_universe_like(
2923 __isl_keep isl_basic_map *model)
2925 if (!model)
2926 return NULL;
2927 return isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
2930 struct isl_basic_set *isl_basic_set_universe_like(struct isl_basic_set *model)
2932 if (!model)
2933 return NULL;
2934 return isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
2937 __isl_give isl_basic_set *isl_basic_set_universe_like_set(
2938 __isl_keep isl_set *model)
2940 if (!model)
2941 return NULL;
2942 return isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
2945 struct isl_map *isl_map_empty(struct isl_dim *dim)
2947 return isl_map_alloc_dim(dim, 0, ISL_MAP_DISJOINT);
2950 struct isl_map *isl_map_empty_like(struct isl_map *model)
2952 if (!model)
2953 return NULL;
2954 return isl_map_alloc_dim(isl_dim_copy(model->dim), 0, ISL_MAP_DISJOINT);
2957 struct isl_map *isl_map_empty_like_basic_map(struct isl_basic_map *model)
2959 if (!model)
2960 return NULL;
2961 return isl_map_alloc_dim(isl_dim_copy(model->dim), 0, ISL_MAP_DISJOINT);
2964 struct isl_set *isl_set_empty(struct isl_dim *dim)
2966 return isl_set_alloc_dim(dim, 0, ISL_MAP_DISJOINT);
2969 struct isl_set *isl_set_empty_like(struct isl_set *model)
2971 if (!model)
2972 return NULL;
2973 return isl_set_empty(isl_dim_copy(model->dim));
2976 struct isl_map *isl_map_universe(struct isl_dim *dim)
2978 struct isl_map *map;
2979 if (!dim)
2980 return NULL;
2981 map = isl_map_alloc_dim(isl_dim_copy(dim), 1, ISL_MAP_DISJOINT);
2982 map = isl_map_add(map, isl_basic_map_universe(dim));
2983 return map;
2986 struct isl_set *isl_set_universe(struct isl_dim *dim)
2988 struct isl_set *set;
2989 if (!dim)
2990 return NULL;
2991 set = isl_set_alloc_dim(isl_dim_copy(dim), 1, ISL_MAP_DISJOINT);
2992 set = isl_set_add(set, isl_basic_set_universe(dim));
2993 return set;
2996 __isl_give isl_set *isl_set_universe_like(__isl_keep isl_set *model)
2998 if (!model)
2999 return NULL;
3000 return isl_set_universe(isl_dim_copy(model->dim));
3003 struct isl_map *isl_map_dup(struct isl_map *map)
3005 int i;
3006 struct isl_map *dup;
3008 if (!map)
3009 return NULL;
3010 dup = isl_map_alloc_dim(isl_dim_copy(map->dim), map->n, map->flags);
3011 for (i = 0; i < map->n; ++i)
3012 dup = isl_map_add(dup, isl_basic_map_copy(map->p[i]));
3013 return dup;
3016 struct isl_map *isl_map_add(struct isl_map *map, struct isl_basic_map *bmap)
3018 if (!bmap || !map)
3019 goto error;
3020 if (isl_basic_map_fast_is_empty(bmap)) {
3021 isl_basic_map_free(bmap);
3022 return map;
3024 isl_assert(map->ctx, isl_dim_equal(map->dim, bmap->dim), goto error);
3025 isl_assert(map->ctx, map->n < map->size, goto error);
3026 map->p[map->n] = bmap;
3027 map->n++;
3028 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3029 return map;
3030 error:
3031 if (map)
3032 isl_map_free(map);
3033 if (bmap)
3034 isl_basic_map_free(bmap);
3035 return NULL;
3038 void isl_map_free(struct isl_map *map)
3040 int i;
3042 if (!map)
3043 return;
3045 if (--map->ref > 0)
3046 return;
3048 isl_ctx_deref(map->ctx);
3049 for (i = 0; i < map->n; ++i)
3050 isl_basic_map_free(map->p[i]);
3051 isl_dim_free(map->dim);
3052 free(map);
3055 struct isl_map *isl_map_extend(struct isl_map *base,
3056 unsigned nparam, unsigned n_in, unsigned n_out)
3058 int i;
3060 base = isl_map_cow(base);
3061 if (!base)
3062 return NULL;
3064 base->dim = isl_dim_extend(base->dim, nparam, n_in, n_out);
3065 if (!base->dim)
3066 goto error;
3067 for (i = 0; i < base->n; ++i) {
3068 base->p[i] = isl_basic_map_extend_dim(base->p[i],
3069 isl_dim_copy(base->dim), 0, 0, 0);
3070 if (!base->p[i])
3071 goto error;
3073 return base;
3074 error:
3075 isl_map_free(base);
3076 return NULL;
3079 struct isl_set *isl_set_extend(struct isl_set *base,
3080 unsigned nparam, unsigned dim)
3082 return (struct isl_set *)isl_map_extend((struct isl_map *)base,
3083 nparam, 0, dim);
3086 static struct isl_basic_map *isl_basic_map_fix_pos_si(
3087 struct isl_basic_map *bmap, unsigned pos, int value)
3089 int j;
3091 bmap = isl_basic_map_cow(bmap);
3092 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
3093 j = isl_basic_map_alloc_equality(bmap);
3094 if (j < 0)
3095 goto error;
3096 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
3097 isl_int_set_si(bmap->eq[j][pos], -1);
3098 isl_int_set_si(bmap->eq[j][0], value);
3099 bmap = isl_basic_map_simplify(bmap);
3100 return isl_basic_map_finalize(bmap);
3101 error:
3102 isl_basic_map_free(bmap);
3103 return NULL;
3106 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
3107 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
3109 int j;
3111 bmap = isl_basic_map_cow(bmap);
3112 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
3113 j = isl_basic_map_alloc_equality(bmap);
3114 if (j < 0)
3115 goto error;
3116 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
3117 isl_int_set_si(bmap->eq[j][pos], -1);
3118 isl_int_set(bmap->eq[j][0], value);
3119 bmap = isl_basic_map_simplify(bmap);
3120 return isl_basic_map_finalize(bmap);
3121 error:
3122 isl_basic_map_free(bmap);
3123 return NULL;
3126 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
3127 enum isl_dim_type type, unsigned pos, int value)
3129 if (!bmap)
3130 return NULL;
3131 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
3132 return isl_basic_map_fix_pos_si(bmap,
3133 isl_basic_map_offset(bmap, type) + pos, value);
3134 error:
3135 isl_basic_map_free(bmap);
3136 return NULL;
3139 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
3140 enum isl_dim_type type, unsigned pos, isl_int value)
3142 if (!bmap)
3143 return NULL;
3144 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
3145 return isl_basic_map_fix_pos(bmap,
3146 isl_basic_map_offset(bmap, type) + pos, value);
3147 error:
3148 isl_basic_map_free(bmap);
3149 return NULL;
3152 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
3153 enum isl_dim_type type, unsigned pos, int value)
3155 return (struct isl_basic_set *)
3156 isl_basic_map_fix_si((struct isl_basic_map *)bset,
3157 type, pos, value);
3160 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
3161 enum isl_dim_type type, unsigned pos, isl_int value)
3163 return (struct isl_basic_set *)
3164 isl_basic_map_fix((struct isl_basic_map *)bset,
3165 type, pos, value);
3168 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
3169 unsigned input, int value)
3171 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
3174 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
3175 unsigned dim, int value)
3177 return (struct isl_basic_set *)
3178 isl_basic_map_fix_si((struct isl_basic_map *)bset,
3179 isl_dim_set, dim, value);
3182 struct isl_map *isl_map_fix_si(struct isl_map *map,
3183 enum isl_dim_type type, unsigned pos, int value)
3185 int i;
3187 map = isl_map_cow(map);
3188 if (!map)
3189 return NULL;
3191 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
3192 for (i = 0; i < map->n; ++i) {
3193 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
3194 if (!map->p[i])
3195 goto error;
3197 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3198 return map;
3199 error:
3200 isl_map_free(map);
3201 return NULL;
3204 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
3205 enum isl_dim_type type, unsigned pos, isl_int value)
3207 int i;
3209 map = isl_map_cow(map);
3210 if (!map)
3211 return NULL;
3213 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
3214 for (i = 0; i < map->n; ++i) {
3215 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
3216 if (!map->p[i])
3217 goto error;
3219 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3220 return map;
3221 error:
3222 isl_map_free(map);
3223 return NULL;
3226 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
3227 enum isl_dim_type type, unsigned pos, isl_int value)
3229 return (struct isl_set *)isl_map_fix((isl_map *)set, type, pos, value);
3232 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
3233 unsigned input, int value)
3235 return isl_map_fix_si(map, isl_dim_in, input, value);
3238 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
3240 return (struct isl_set *)
3241 isl_map_fix_si((struct isl_map *)set, isl_dim_set, dim, value);
3244 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
3245 unsigned dim, isl_int value)
3247 int j;
3249 bset = isl_basic_set_cow(bset);
3250 bset = isl_basic_set_extend_constraints(bset, 0, 1);
3251 j = isl_basic_set_alloc_inequality(bset);
3252 if (j < 0)
3253 goto error;
3254 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
3255 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
3256 isl_int_neg(bset->ineq[j][0], value);
3257 bset = isl_basic_set_simplify(bset);
3258 return isl_basic_set_finalize(bset);
3259 error:
3260 isl_basic_set_free(bset);
3261 return NULL;
3264 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
3265 isl_int value)
3267 int i;
3269 set = isl_set_cow(set);
3270 if (!set)
3271 return NULL;
3273 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
3274 for (i = 0; i < set->n; ++i) {
3275 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
3276 if (!set->p[i])
3277 goto error;
3279 return set;
3280 error:
3281 isl_set_free(set);
3282 return NULL;
3285 struct isl_map *isl_map_reverse(struct isl_map *map)
3287 int i;
3289 map = isl_map_cow(map);
3290 if (!map)
3291 return NULL;
3293 map->dim = isl_dim_reverse(map->dim);
3294 if (!map->dim)
3295 goto error;
3296 for (i = 0; i < map->n; ++i) {
3297 map->p[i] = isl_basic_map_reverse(map->p[i]);
3298 if (!map->p[i])
3299 goto error;
3301 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3302 return map;
3303 error:
3304 isl_map_free(map);
3305 return NULL;
3308 static struct isl_map *isl_basic_map_partial_lexopt(
3309 struct isl_basic_map *bmap, struct isl_basic_set *dom,
3310 struct isl_set **empty, int max)
3312 if (!bmap)
3313 goto error;
3314 if (bmap->ctx->opt->pip == ISL_PIP_PIP)
3315 return isl_pip_basic_map_lexopt(bmap, dom, empty, max);
3316 else
3317 return isl_tab_basic_map_partial_lexopt(bmap, dom, empty, max);
3318 error:
3319 isl_basic_map_free(bmap);
3320 isl_basic_set_free(dom);
3321 if (empty)
3322 *empty = NULL;
3323 return NULL;
3326 struct isl_map *isl_basic_map_partial_lexmax(
3327 struct isl_basic_map *bmap, struct isl_basic_set *dom,
3328 struct isl_set **empty)
3330 return isl_basic_map_partial_lexopt(bmap, dom, empty, 1);
3333 struct isl_map *isl_basic_map_partial_lexmin(
3334 struct isl_basic_map *bmap, struct isl_basic_set *dom,
3335 struct isl_set **empty)
3337 return isl_basic_map_partial_lexopt(bmap, dom, empty, 0);
3340 struct isl_set *isl_basic_set_partial_lexmin(
3341 struct isl_basic_set *bset, struct isl_basic_set *dom,
3342 struct isl_set **empty)
3344 return (struct isl_set *)
3345 isl_basic_map_partial_lexmin((struct isl_basic_map *)bset,
3346 dom, empty);
3349 struct isl_set *isl_basic_set_partial_lexmax(
3350 struct isl_basic_set *bset, struct isl_basic_set *dom,
3351 struct isl_set **empty)
3353 return (struct isl_set *)
3354 isl_basic_map_partial_lexmax((struct isl_basic_map *)bset,
3355 dom, empty);
3358 __isl_give isl_map *isl_basic_map_lexmin(__isl_take isl_basic_map *bmap)
3360 struct isl_basic_set *dom = NULL;
3361 struct isl_map *min;
3362 struct isl_dim *dom_dim;
3364 if (!bmap)
3365 goto error;
3366 dom_dim = isl_dim_domain(isl_dim_copy(bmap->dim));
3367 dom = isl_basic_set_universe(dom_dim);
3368 return isl_basic_map_partial_lexmin(bmap, dom, NULL);
3369 error:
3370 isl_basic_map_free(bmap);
3371 return NULL;
3374 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
3376 return (isl_set *)isl_basic_map_lexmin((isl_basic_map *)bset);
3379 static struct isl_map *isl_map_reset_dim(struct isl_map *map,
3380 struct isl_dim *dim)
3382 int i;
3384 if (!map || !dim)
3385 goto error;
3387 for (i = 0; i < map->n; ++i) {
3388 isl_dim_free(map->p[i]->dim);
3389 map->p[i]->dim = isl_dim_copy(dim);
3391 isl_dim_free(map->dim);
3392 map->dim = dim;
3394 return map;
3395 error:
3396 isl_map_free(map);
3397 isl_dim_free(dim);
3398 return NULL;
3401 static struct isl_set *isl_set_reset_dim(struct isl_set *set,
3402 struct isl_dim *dim)
3404 return (struct isl_set *) isl_map_reset_dim((struct isl_map *)set, dim);
3407 /* Apply a preimage specified by "mat" on the parameters of "bset".
3408 * bset is assumed to have only parameters and divs.
3410 static struct isl_basic_set *basic_set_parameter_preimage(
3411 struct isl_basic_set *bset, struct isl_mat *mat)
3413 unsigned nparam;
3415 if (!bset || !mat)
3416 goto error;
3418 bset->dim = isl_dim_cow(bset->dim);
3419 if (!bset->dim)
3420 goto error;
3422 nparam = isl_basic_set_dim(bset, isl_dim_param);
3424 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
3426 bset->dim->nparam = 0;
3427 bset->dim->n_out = nparam;
3428 bset = isl_basic_set_preimage(bset, mat);
3429 if (bset) {
3430 bset->dim->nparam = bset->dim->n_out;
3431 bset->dim->n_out = 0;
3433 return bset;
3434 error:
3435 isl_mat_free(mat);
3436 isl_basic_set_free(bset);
3437 return NULL;
3440 /* Apply a preimage specified by "mat" on the parameters of "set".
3441 * set is assumed to have only parameters and divs.
3443 static struct isl_set *set_parameter_preimage(
3444 struct isl_set *set, struct isl_mat *mat)
3446 struct isl_dim *dim = NULL;
3447 unsigned nparam;
3449 if (!set || !mat)
3450 goto error;
3452 dim = isl_dim_copy(set->dim);
3453 dim = isl_dim_cow(dim);
3454 if (!dim)
3455 goto error;
3457 nparam = isl_set_dim(set, isl_dim_param);
3459 isl_assert(set->ctx, mat->n_row == 1 + nparam, goto error);
3461 dim->nparam = 0;
3462 dim->n_out = nparam;
3463 isl_set_reset_dim(set, dim);
3464 set = isl_set_preimage(set, mat);
3465 if (!set)
3466 goto error2;
3467 dim = isl_dim_copy(set->dim);
3468 dim = isl_dim_cow(dim);
3469 if (!dim)
3470 goto error2;
3471 dim->nparam = dim->n_out;
3472 dim->n_out = 0;
3473 isl_set_reset_dim(set, dim);
3474 return set;
3475 error:
3476 isl_dim_free(dim);
3477 isl_mat_free(mat);
3478 error2:
3479 isl_set_free(set);
3480 return NULL;
3483 /* Intersect the basic set "bset" with the affine space specified by the
3484 * equalities in "eq".
3486 static struct isl_basic_set *basic_set_append_equalities(
3487 struct isl_basic_set *bset, struct isl_mat *eq)
3489 int i, k;
3490 unsigned len;
3492 if (!bset || !eq)
3493 goto error;
3495 bset = isl_basic_set_extend_dim(bset, isl_dim_copy(bset->dim), 0,
3496 eq->n_row, 0);
3497 if (!bset)
3498 goto error;
3500 len = 1 + isl_dim_total(bset->dim) + bset->extra;
3501 for (i = 0; i < eq->n_row; ++i) {
3502 k = isl_basic_set_alloc_equality(bset);
3503 if (k < 0)
3504 goto error;
3505 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
3506 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
3508 isl_mat_free(eq);
3510 return bset;
3511 error:
3512 isl_mat_free(eq);
3513 isl_basic_set_free(bset);
3514 return NULL;
3517 /* Intersect the set "set" with the affine space specified by the
3518 * equalities in "eq".
3520 static struct isl_set *set_append_equalities(struct isl_set *set,
3521 struct isl_mat *eq)
3523 int i;
3525 if (!set || !eq)
3526 goto error;
3528 for (i = 0; i < set->n; ++i) {
3529 set->p[i] = basic_set_append_equalities(set->p[i],
3530 isl_mat_copy(eq));
3531 if (!set->p[i])
3532 goto error;
3534 isl_mat_free(eq);
3535 return set;
3536 error:
3537 isl_mat_free(eq);
3538 isl_set_free(set);
3539 return NULL;
3542 /* Project the given basic set onto its parameter domain, possibly introducing
3543 * new, explicit, existential variables in the constraints.
3544 * The input has parameters and (possibly implicit) existential variables.
3545 * The output has the same parameters, but only
3546 * explicit existentially quantified variables.
3548 * The actual projection is performed by pip, but pip doesn't seem
3549 * to like equalities very much, so we first remove the equalities
3550 * among the parameters by performing a variable compression on
3551 * the parameters. Afterward, an inverse transformation is performed
3552 * and the equalities among the parameters are inserted back in.
3554 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
3556 int i, j;
3557 struct isl_mat *eq;
3558 struct isl_mat *T, *T2;
3559 struct isl_set *set;
3560 unsigned nparam, n_div;
3562 bset = isl_basic_set_cow(bset);
3563 if (!bset)
3564 return NULL;
3566 if (bset->n_eq == 0)
3567 return isl_basic_set_lexmin(bset);
3569 isl_basic_set_gauss(bset, NULL);
3571 nparam = isl_basic_set_dim(bset, isl_dim_param);
3572 n_div = isl_basic_set_dim(bset, isl_dim_div);
3574 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
3575 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
3576 ++i;
3578 if (i == bset->n_eq)
3579 return isl_basic_set_lexmin(bset);
3581 eq = isl_mat_sub_alloc(bset->ctx, bset->eq, i, bset->n_eq - i,
3582 0, 1 + nparam);
3583 eq = isl_mat_cow(eq);
3584 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
3585 bset = basic_set_parameter_preimage(bset, T);
3587 set = isl_basic_set_lexmin(bset);
3588 set = set_parameter_preimage(set, T2);
3589 set = set_append_equalities(set, eq);
3590 return set;
3593 /* Compute an explicit representation for all the existentially
3594 * quantified variables.
3595 * The input and output dimensions are first turned into parameters.
3596 * compute_divs then returns a map with the same parameters and
3597 * no input or output dimensions and the dimension specification
3598 * is reset to that of the input.
3600 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
3602 struct isl_basic_set *bset;
3603 struct isl_set *set;
3604 struct isl_map *map;
3605 struct isl_dim *dim, *orig_dim = NULL;
3606 unsigned nparam;
3607 unsigned n_in;
3608 unsigned n_out;
3610 bmap = isl_basic_map_cow(bmap);
3611 if (!bmap)
3612 return NULL;
3614 nparam = isl_basic_map_dim(bmap, isl_dim_param);
3615 n_in = isl_basic_map_dim(bmap, isl_dim_in);
3616 n_out = isl_basic_map_dim(bmap, isl_dim_out);
3617 dim = isl_dim_set_alloc(bmap->ctx, nparam + n_in + n_out, 0);
3618 if (!dim)
3619 goto error;
3621 orig_dim = bmap->dim;
3622 bmap->dim = dim;
3623 bset = (struct isl_basic_set *)bmap;
3625 set = parameter_compute_divs(bset);
3626 map = (struct isl_map *)set;
3627 map = isl_map_reset_dim(map, orig_dim);
3629 return map;
3630 error:
3631 isl_basic_map_free(bmap);
3632 return NULL;
3635 static int basic_map_divs_known(__isl_keep isl_basic_map *bmap)
3637 int i;
3638 unsigned off;
3640 if (!bmap)
3641 return -1;
3643 off = isl_dim_total(bmap->dim);
3644 for (i = 0; i < bmap->n_div; ++i) {
3645 if (isl_int_is_zero(bmap->div[i][0]))
3646 return 0;
3647 isl_assert(bmap->ctx, isl_int_is_zero(bmap->div[i][1+1+off+i]),
3648 return -1);
3650 return 1;
3653 static int map_divs_known(__isl_keep isl_map *map)
3655 int i;
3657 if (!map)
3658 return -1;
3660 for (i = 0; i < map->n; ++i) {
3661 int known = basic_map_divs_known(map->p[i]);
3662 if (known <= 0)
3663 return known;
3666 return 1;
3669 /* If bmap contains any unknown divs, then compute explicit
3670 * expressions for them. However, this computation may be
3671 * quite expensive, so first try to remove divs that aren't
3672 * strictly needed.
3674 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
3676 int i;
3677 int known;
3678 struct isl_map *map;
3680 known = basic_map_divs_known(bmap);
3681 if (known < 0)
3682 goto error;
3683 if (known)
3684 return isl_map_from_basic_map(bmap);
3686 bmap = isl_basic_map_drop_redundant_divs(bmap);
3688 known = basic_map_divs_known(bmap);
3689 if (known < 0)
3690 goto error;
3691 if (known)
3692 return isl_map_from_basic_map(bmap);
3694 map = compute_divs(bmap);
3695 return map;
3696 error:
3697 isl_basic_map_free(bmap);
3698 return NULL;
3701 struct isl_map *isl_map_compute_divs(struct isl_map *map)
3703 int i;
3704 int known;
3705 struct isl_map *res;
3707 if (!map)
3708 return NULL;
3709 if (map->n == 0)
3710 return map;
3712 known = map_divs_known(map);
3713 if (known < 0) {
3714 isl_map_free(map);
3715 return NULL;
3717 if (known)
3718 return map;
3720 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
3721 for (i = 1 ; i < map->n; ++i) {
3722 struct isl_map *r2;
3723 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
3724 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
3725 res = isl_map_union_disjoint(res, r2);
3726 else
3727 res = isl_map_union(res, r2);
3729 isl_map_free(map);
3731 return res;
3734 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
3736 return (struct isl_set *)
3737 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
3740 struct isl_set *isl_set_compute_divs(struct isl_set *set)
3742 return (struct isl_set *)
3743 isl_map_compute_divs((struct isl_map *)set);
3746 struct isl_set *isl_map_domain(struct isl_map *map)
3748 int i;
3749 struct isl_set *set;
3751 if (!map)
3752 goto error;
3754 map = isl_map_cow(map);
3755 if (!map)
3756 return NULL;
3758 set = (struct isl_set *)map;
3759 set->dim = isl_dim_domain(set->dim);
3760 if (!set->dim)
3761 goto error;
3762 for (i = 0; i < map->n; ++i) {
3763 set->p[i] = isl_basic_map_domain(map->p[i]);
3764 if (!set->p[i])
3765 goto error;
3767 ISL_F_CLR(set, ISL_MAP_DISJOINT);
3768 ISL_F_CLR(set, ISL_SET_NORMALIZED);
3769 return set;
3770 error:
3771 isl_map_free(map);
3772 return NULL;
3775 struct isl_map *isl_map_union_disjoint(
3776 struct isl_map *map1, struct isl_map *map2)
3778 int i;
3779 unsigned flags = 0;
3780 struct isl_map *map = NULL;
3782 if (!map1 || !map2)
3783 goto error;
3785 if (map1->n == 0) {
3786 isl_map_free(map1);
3787 return map2;
3789 if (map2->n == 0) {
3790 isl_map_free(map2);
3791 return map1;
3794 isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
3796 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
3797 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
3798 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3800 map = isl_map_alloc_dim(isl_dim_copy(map1->dim),
3801 map1->n + map2->n, flags);
3802 if (!map)
3803 goto error;
3804 for (i = 0; i < map1->n; ++i) {
3805 map = isl_map_add(map,
3806 isl_basic_map_copy(map1->p[i]));
3807 if (!map)
3808 goto error;
3810 for (i = 0; i < map2->n; ++i) {
3811 map = isl_map_add(map,
3812 isl_basic_map_copy(map2->p[i]));
3813 if (!map)
3814 goto error;
3816 isl_map_free(map1);
3817 isl_map_free(map2);
3818 return map;
3819 error:
3820 isl_map_free(map);
3821 isl_map_free(map1);
3822 isl_map_free(map2);
3823 return NULL;
3826 struct isl_map *isl_map_union(struct isl_map *map1, struct isl_map *map2)
3828 map1 = isl_map_union_disjoint(map1, map2);
3829 if (!map1)
3830 return NULL;
3831 if (map1->n > 1)
3832 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
3833 return map1;
3836 struct isl_set *isl_set_union_disjoint(
3837 struct isl_set *set1, struct isl_set *set2)
3839 return (struct isl_set *)
3840 isl_map_union_disjoint(
3841 (struct isl_map *)set1, (struct isl_map *)set2);
3844 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
3846 return (struct isl_set *)
3847 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
3850 struct isl_map *isl_map_intersect_range(
3851 struct isl_map *map, struct isl_set *set)
3853 unsigned flags = 0;
3854 struct isl_map *result;
3855 int i, j;
3857 if (!map || !set)
3858 goto error;
3860 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
3861 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
3862 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3864 result = isl_map_alloc_dim(isl_dim_copy(map->dim),
3865 map->n * set->n, flags);
3866 if (!result)
3867 goto error;
3868 for (i = 0; i < map->n; ++i)
3869 for (j = 0; j < set->n; ++j) {
3870 result = isl_map_add(result,
3871 isl_basic_map_intersect_range(
3872 isl_basic_map_copy(map->p[i]),
3873 isl_basic_set_copy(set->p[j])));
3874 if (!result)
3875 goto error;
3877 isl_map_free(map);
3878 isl_set_free(set);
3879 return result;
3880 error:
3881 isl_map_free(map);
3882 isl_set_free(set);
3883 return NULL;
3886 struct isl_map *isl_map_intersect_domain(
3887 struct isl_map *map, struct isl_set *set)
3889 return isl_map_reverse(
3890 isl_map_intersect_range(isl_map_reverse(map), set));
3893 struct isl_map *isl_map_apply_domain(
3894 struct isl_map *map1, struct isl_map *map2)
3896 if (!map1 || !map2)
3897 goto error;
3898 map1 = isl_map_reverse(map1);
3899 map1 = isl_map_apply_range(map1, map2);
3900 return isl_map_reverse(map1);
3901 error:
3902 isl_map_free(map1);
3903 isl_map_free(map2);
3904 return NULL;
3907 struct isl_map *isl_map_apply_range(
3908 struct isl_map *map1, struct isl_map *map2)
3910 struct isl_dim *dim_result;
3911 struct isl_map *result;
3912 int i, j;
3914 if (!map1 || !map2)
3915 goto error;
3917 dim_result = isl_dim_join(isl_dim_copy(map1->dim),
3918 isl_dim_copy(map2->dim));
3920 result = isl_map_alloc_dim(dim_result, map1->n * map2->n, 0);
3921 if (!result)
3922 goto error;
3923 for (i = 0; i < map1->n; ++i)
3924 for (j = 0; j < map2->n; ++j) {
3925 result = isl_map_add(result,
3926 isl_basic_map_apply_range(
3927 isl_basic_map_copy(map1->p[i]),
3928 isl_basic_map_copy(map2->p[j])));
3929 if (!result)
3930 goto error;
3932 isl_map_free(map1);
3933 isl_map_free(map2);
3934 if (result && result->n <= 1)
3935 ISL_F_SET(result, ISL_MAP_DISJOINT);
3936 return result;
3937 error:
3938 isl_map_free(map1);
3939 isl_map_free(map2);
3940 return NULL;
3944 * returns range - domain
3946 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
3948 struct isl_basic_set *bset;
3949 unsigned dim;
3950 unsigned nparam;
3951 int i;
3953 if (!bmap)
3954 goto error;
3955 dim = isl_basic_map_n_in(bmap);
3956 nparam = isl_basic_map_n_param(bmap);
3957 isl_assert(bmap->ctx, dim == isl_basic_map_n_out(bmap), goto error);
3958 bset = isl_basic_set_from_basic_map(bmap);
3959 bset = isl_basic_set_cow(bset);
3960 bset = isl_basic_set_extend(bset, nparam, 3*dim, 0, dim, 0);
3961 bset = isl_basic_set_swap_vars(bset, 2*dim);
3962 for (i = 0; i < dim; ++i) {
3963 int j = isl_basic_map_alloc_equality(
3964 (struct isl_basic_map *)bset);
3965 if (j < 0)
3966 goto error;
3967 isl_seq_clr(bset->eq[j], 1 + isl_basic_set_total_dim(bset));
3968 isl_int_set_si(bset->eq[j][1+nparam+i], 1);
3969 isl_int_set_si(bset->eq[j][1+nparam+dim+i], 1);
3970 isl_int_set_si(bset->eq[j][1+nparam+2*dim+i], -1);
3972 return isl_basic_set_project_out(bset, isl_dim_set, dim, 2*dim);
3973 error:
3974 isl_basic_map_free(bmap);
3975 return NULL;
3979 * returns range - domain
3981 struct isl_set *isl_map_deltas(struct isl_map *map)
3983 int i;
3984 struct isl_set *result;
3986 if (!map)
3987 return NULL;
3989 isl_assert(map->ctx, isl_map_n_in(map) == isl_map_n_out(map), goto error);
3990 result = isl_set_alloc(map->ctx, isl_map_n_param(map),
3991 isl_map_n_in(map), map->n, map->flags);
3992 if (!result)
3993 goto error;
3994 for (i = 0; i < map->n; ++i)
3995 result = isl_set_add(result,
3996 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
3997 isl_map_free(map);
3998 return result;
3999 error:
4000 isl_map_free(map);
4001 return NULL;
4004 static struct isl_basic_map *basic_map_identity(struct isl_dim *dims)
4006 struct isl_basic_map *bmap;
4007 unsigned nparam;
4008 unsigned dim;
4009 int i;
4011 if (!dims)
4012 return NULL;
4014 nparam = dims->nparam;
4015 dim = dims->n_out;
4016 bmap = isl_basic_map_alloc_dim(dims, 0, dim, 0);
4017 if (!bmap)
4018 goto error;
4020 for (i = 0; i < dim; ++i) {
4021 int j = isl_basic_map_alloc_equality(bmap);
4022 if (j < 0)
4023 goto error;
4024 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
4025 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
4026 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
4028 return isl_basic_map_finalize(bmap);
4029 error:
4030 isl_basic_map_free(bmap);
4031 return NULL;
4034 struct isl_basic_map *isl_basic_map_identity(struct isl_dim *set_dim)
4036 struct isl_dim *dim = isl_dim_map(set_dim);
4037 if (!dim)
4038 return NULL;
4039 return basic_map_identity(dim);
4042 struct isl_basic_map *isl_basic_map_identity_like(struct isl_basic_map *model)
4044 if (!model || !model->dim)
4045 return NULL;
4046 isl_assert(model->ctx,
4047 model->dim->n_in == model->dim->n_out, return NULL);
4048 return basic_map_identity(isl_dim_copy(model->dim));
4051 static struct isl_map *map_identity(struct isl_dim *dim)
4053 struct isl_map *map = isl_map_alloc_dim(dim, 1, ISL_MAP_DISJOINT);
4054 return isl_map_add(map, basic_map_identity(isl_dim_copy(dim)));
4057 struct isl_map *isl_map_identity(struct isl_dim *set_dim)
4059 struct isl_dim *dim = isl_dim_map(set_dim);
4060 if (!dim)
4061 return NULL;
4062 return map_identity(dim);
4065 struct isl_map *isl_map_identity_like(struct isl_map *model)
4067 if (!model || !model->dim)
4068 return NULL;
4069 isl_assert(model->ctx,
4070 model->dim->n_in == model->dim->n_out, return NULL);
4071 return map_identity(isl_dim_copy(model->dim));
4074 struct isl_map *isl_map_identity_like_basic_map(struct isl_basic_map *model)
4076 if (!model || !model->dim)
4077 return NULL;
4078 isl_assert(model->ctx,
4079 model->dim->n_in == model->dim->n_out, return NULL);
4080 return map_identity(isl_dim_copy(model->dim));
4083 /* Construct a basic set with all set dimensions having only non-negative
4084 * values.
4086 struct isl_basic_set *isl_basic_set_positive_orthant(struct isl_dim *dims)
4088 int i;
4089 unsigned nparam;
4090 unsigned dim;
4091 struct isl_basic_set *bset;
4093 if (!dims)
4094 return NULL;
4095 nparam = dims->nparam;
4096 dim = dims->n_out;
4097 bset = isl_basic_set_alloc_dim(dims, 0, 0, dim);
4098 if (!bset)
4099 return NULL;
4100 for (i = 0; i < dim; ++i) {
4101 int k = isl_basic_set_alloc_inequality(bset);
4102 if (k < 0)
4103 goto error;
4104 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
4105 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
4107 return bset;
4108 error:
4109 isl_basic_set_free(bset);
4110 return NULL;
4113 int isl_set_is_equal(struct isl_set *set1, struct isl_set *set2)
4115 return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
4118 int isl_set_is_subset(struct isl_set *set1, struct isl_set *set2)
4120 return isl_map_is_subset(
4121 (struct isl_map *)set1, (struct isl_map *)set2);
4124 int isl_basic_map_is_subset(
4125 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4127 int is_subset;
4128 struct isl_map *map1;
4129 struct isl_map *map2;
4131 if (!bmap1 || !bmap2)
4132 return -1;
4134 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
4135 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
4137 is_subset = isl_map_is_subset(map1, map2);
4139 isl_map_free(map1);
4140 isl_map_free(map2);
4142 return is_subset;
4145 int isl_basic_map_is_equal(
4146 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4148 int is_subset;
4150 if (!bmap1 || !bmap2)
4151 return -1;
4152 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
4153 if (is_subset != 1)
4154 return is_subset;
4155 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
4156 return is_subset;
4159 int isl_basic_set_is_equal(
4160 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
4162 return isl_basic_map_is_equal(
4163 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
4166 int isl_map_is_empty(struct isl_map *map)
4168 int i;
4169 int is_empty;
4171 if (!map)
4172 return -1;
4173 for (i = 0; i < map->n; ++i) {
4174 is_empty = isl_basic_map_is_empty(map->p[i]);
4175 if (is_empty < 0)
4176 return -1;
4177 if (!is_empty)
4178 return 0;
4180 return 1;
4183 int isl_map_fast_is_empty(struct isl_map *map)
4185 return map->n == 0;
4188 int isl_set_is_empty(struct isl_set *set)
4190 return isl_map_is_empty((struct isl_map *)set);
4193 /* Return 1 if "bmap" contains a single element.
4195 int isl_basic_map_is_singleton(__isl_keep isl_basic_map *bmap)
4197 if (!bmap)
4198 return -1;
4199 if (bmap->n_div)
4200 return 0;
4201 if (bmap->n_ineq)
4202 return 0;
4203 return bmap->n_eq == isl_basic_map_total_dim(bmap);
4206 /* Return 1 if "map" contains a single element.
4208 int isl_map_is_singleton(__isl_keep isl_map *map)
4210 if (!map)
4211 return -1;
4212 if (map->n != 1)
4213 return 0;
4215 return isl_basic_map_is_singleton(map->p[0]);
4218 /* Given a singleton basic map, extract the single element
4219 * as an isl_vec.
4221 static __isl_give isl_vec *singleton_extract_point(__isl_keep isl_basic_map *bmap)
4223 int i, j;
4224 unsigned dim;
4225 struct isl_vec *point;
4226 isl_int m;
4228 if (!bmap)
4229 return NULL;
4231 dim = isl_basic_map_total_dim(bmap);
4232 isl_assert(bmap->ctx, bmap->n_eq == dim, return NULL);
4233 point = isl_vec_alloc(bmap->ctx, 1 + dim);
4234 if (!point)
4235 return NULL;
4237 isl_int_init(m);
4239 isl_int_set_si(point->el[0], 1);
4240 for (j = 0; j < bmap->n_eq; ++j) {
4241 int s;
4242 int i = dim - 1 - j;
4243 isl_assert(bmap->ctx,
4244 isl_seq_first_non_zero(bmap->eq[j] + 1, i) == -1,
4245 goto error);
4246 isl_assert(bmap->ctx,
4247 isl_int_is_one(bmap->eq[j][1 + i]) ||
4248 isl_int_is_negone(bmap->eq[j][1 + i]),
4249 goto error);
4250 isl_assert(bmap->ctx,
4251 isl_seq_first_non_zero(bmap->eq[j]+1+i+1, dim-i-1) == -1,
4252 goto error);
4254 isl_int_gcd(m, point->el[0], bmap->eq[j][1 + i]);
4255 isl_int_divexact(m, bmap->eq[j][1 + i], m);
4256 isl_int_abs(m, m);
4257 isl_seq_scale(point->el, point->el, m, 1 + i);
4258 isl_int_divexact(m, point->el[0], bmap->eq[j][1 + i]);
4259 isl_int_neg(m, m);
4260 isl_int_mul(point->el[1 + i], m, bmap->eq[j][0]);
4263 isl_int_clear(m);
4264 return point;
4265 error:
4266 isl_int_clear(m);
4267 isl_vec_free(point);
4268 return NULL;
4271 /* Return 1 if "bmap" contains the point "point".
4272 * "bmap" is assumed to have known divs.
4273 * The point is first extended with the divs and then passed
4274 * to basic_map_contains.
4276 static int basic_map_contains_point(__isl_keep isl_basic_map *bmap,
4277 __isl_keep isl_vec *point)
4279 int i;
4280 struct isl_vec *vec;
4281 unsigned dim;
4282 int contains;
4284 if (!bmap || !point)
4285 return -1;
4286 if (bmap->n_div == 0)
4287 return basic_map_contains(bmap, point);
4289 dim = isl_basic_map_total_dim(bmap) - bmap->n_div;
4290 vec = isl_vec_alloc(bmap->ctx, 1 + dim + bmap->n_div);
4291 if (!vec)
4292 return -1;
4294 isl_seq_cpy(vec->el, point->el, point->size);
4295 for (i = 0; i < bmap->n_div; ++i) {
4296 isl_seq_inner_product(bmap->div[i] + 1, vec->el,
4297 1 + dim + i, &vec->el[1+dim+i]);
4298 isl_int_fdiv_q(vec->el[1+dim+i], vec->el[1+dim+i],
4299 bmap->div[i][0]);
4302 contains = basic_map_contains(bmap, vec);
4304 isl_vec_free(vec);
4305 return contains;
4308 /* Return 1 is the singleton map "map1" is a subset of "map2",
4309 * i.e., if the single element of "map1" is also an element of "map2".
4311 static int map_is_singleton_subset(__isl_keep isl_map *map1,
4312 __isl_keep isl_map *map2)
4314 int i;
4315 int is_subset = 0;
4316 struct isl_vec *point;
4318 if (!map1 || !map2)
4319 return -1;
4320 if (map1->n != 1)
4321 return -1;
4323 point = singleton_extract_point(map1->p[0]);
4324 if (!point)
4325 return -1;
4327 for (i = 0; i < map2->n; ++i) {
4328 is_subset = basic_map_contains_point(map2->p[i], point);
4329 if (is_subset)
4330 break;
4333 isl_vec_free(point);
4334 return is_subset;
4337 int isl_map_is_subset(struct isl_map *map1, struct isl_map *map2)
4339 int is_subset = 0;
4340 struct isl_map *diff;
4342 if (!map1 || !map2)
4343 return -1;
4345 if (isl_map_is_empty(map1))
4346 return 1;
4348 if (isl_map_is_empty(map2))
4349 return 0;
4351 if (isl_map_fast_is_universe(map2))
4352 return 1;
4354 map2 = isl_map_compute_divs(isl_map_copy(map2));
4355 if (isl_map_is_singleton(map1)) {
4356 is_subset = map_is_singleton_subset(map1, map2);
4357 isl_map_free(map2);
4358 return is_subset;
4360 diff = isl_map_subtract(isl_map_copy(map1), map2);
4361 if (!diff)
4362 return -1;
4364 is_subset = isl_map_is_empty(diff);
4365 isl_map_free(diff);
4367 return is_subset;
4370 int isl_map_is_equal(struct isl_map *map1, struct isl_map *map2)
4372 int is_subset;
4374 if (!map1 || !map2)
4375 return -1;
4376 is_subset = isl_map_is_subset(map1, map2);
4377 if (is_subset != 1)
4378 return is_subset;
4379 is_subset = isl_map_is_subset(map2, map1);
4380 return is_subset;
4383 int isl_basic_map_is_strict_subset(
4384 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4386 int is_subset;
4388 if (!bmap1 || !bmap2)
4389 return -1;
4390 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
4391 if (is_subset != 1)
4392 return is_subset;
4393 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
4394 if (is_subset == -1)
4395 return is_subset;
4396 return !is_subset;
4399 int isl_map_is_strict_subset(struct isl_map *map1, struct isl_map *map2)
4401 int is_subset;
4403 if (!map1 || !map2)
4404 return -1;
4405 is_subset = isl_map_is_subset(map1, map2);
4406 if (is_subset != 1)
4407 return is_subset;
4408 is_subset = isl_map_is_subset(map2, map1);
4409 if (is_subset == -1)
4410 return is_subset;
4411 return !is_subset;
4414 int isl_set_is_strict_subset(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
4416 return isl_map_is_strict_subset((isl_map *)set1, (isl_map *)set2);
4419 int isl_basic_map_is_universe(struct isl_basic_map *bmap)
4421 if (!bmap)
4422 return -1;
4423 return bmap->n_eq == 0 && bmap->n_ineq == 0;
4426 int isl_basic_set_is_universe(struct isl_basic_set *bset)
4428 if (!bset)
4429 return -1;
4430 return bset->n_eq == 0 && bset->n_ineq == 0;
4433 int isl_map_fast_is_universe(__isl_keep isl_map *map)
4435 if (!map)
4436 return -1;
4438 return map->n == 1 && isl_basic_map_is_universe(map->p[0]);
4441 int isl_basic_map_is_empty(struct isl_basic_map *bmap)
4443 struct isl_basic_set *bset = NULL;
4444 struct isl_vec *sample = NULL;
4445 int empty;
4446 unsigned total;
4448 if (!bmap)
4449 return -1;
4451 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
4452 return 1;
4454 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
4455 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
4456 copy = isl_basic_map_convex_hull(copy);
4457 empty = ISL_F_ISSET(copy, ISL_BASIC_MAP_EMPTY);
4458 isl_basic_map_free(copy);
4459 return empty;
4462 total = 1 + isl_basic_map_total_dim(bmap);
4463 if (bmap->sample && bmap->sample->size == total) {
4464 int contains = basic_map_contains(bmap, bmap->sample);
4465 if (contains < 0)
4466 return -1;
4467 if (contains)
4468 return 0;
4470 isl_vec_free(bmap->sample);
4471 bmap->sample = NULL;
4472 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
4473 if (!bset)
4474 return -1;
4475 sample = isl_basic_set_sample_vec(bset);
4476 if (!sample)
4477 return -1;
4478 empty = sample->size == 0;
4479 isl_vec_free(bmap->sample);
4480 bmap->sample = sample;
4481 if (empty)
4482 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
4484 return empty;
4487 int isl_basic_map_fast_is_empty(struct isl_basic_map *bmap)
4489 if (!bmap)
4490 return -1;
4491 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
4494 int isl_basic_set_fast_is_empty(struct isl_basic_set *bset)
4496 if (!bset)
4497 return -1;
4498 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
4501 int isl_basic_set_is_empty(struct isl_basic_set *bset)
4503 return isl_basic_map_is_empty((struct isl_basic_map *)bset);
4506 struct isl_map *isl_basic_map_union(
4507 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4509 struct isl_map *map;
4510 if (!bmap1 || !bmap2)
4511 return NULL;
4513 isl_assert(bmap1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim), goto error);
4515 map = isl_map_alloc_dim(isl_dim_copy(bmap1->dim), 2, 0);
4516 if (!map)
4517 goto error;
4518 map = isl_map_add(map, bmap1);
4519 map = isl_map_add(map, bmap2);
4520 return map;
4521 error:
4522 isl_basic_map_free(bmap1);
4523 isl_basic_map_free(bmap2);
4524 return NULL;
4527 struct isl_set *isl_basic_set_union(
4528 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
4530 return (struct isl_set *)isl_basic_map_union(
4531 (struct isl_basic_map *)bset1,
4532 (struct isl_basic_map *)bset2);
4535 /* Order divs such that any div only depends on previous divs */
4536 struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
4538 int i;
4539 unsigned off = isl_dim_total(bmap->dim);
4541 for (i = 0; i < bmap->n_div; ++i) {
4542 int pos;
4543 if (isl_int_is_zero(bmap->div[i][0]))
4544 continue;
4545 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
4546 bmap->n_div-i);
4547 if (pos == -1)
4548 continue;
4549 isl_basic_map_swap_div(bmap, i, i + pos);
4550 --i;
4552 return bmap;
4555 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
4557 return (struct isl_basic_set *)
4558 isl_basic_map_order_divs((struct isl_basic_map *)bset);
4561 /* Look for a div in dst that corresponds to the div "div" in src.
4562 * The divs before "div" in src and dst are assumed to be the same.
4564 * Returns -1 if no corresponding div was found and the position
4565 * of the corresponding div in dst otherwise.
4567 static int find_div(struct isl_basic_map *dst,
4568 struct isl_basic_map *src, unsigned div)
4570 int i;
4572 unsigned total = isl_dim_total(src->dim);
4574 isl_assert(dst->ctx, div <= dst->n_div, return -1);
4575 for (i = div; i < dst->n_div; ++i)
4576 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
4577 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
4578 dst->n_div - div) == -1)
4579 return i;
4580 return -1;
4583 struct isl_basic_map *isl_basic_map_align_divs(
4584 struct isl_basic_map *dst, struct isl_basic_map *src)
4586 int i;
4587 unsigned total = isl_dim_total(src->dim);
4589 if (!dst || !src)
4590 goto error;
4592 if (src->n_div == 0)
4593 return dst;
4595 for (i = 0; i < src->n_div; ++i)
4596 isl_assert(src->ctx, !isl_int_is_zero(src->div[i][0]), goto error);
4598 src = isl_basic_map_order_divs(src);
4599 dst = isl_basic_map_cow(dst);
4600 dst = isl_basic_map_extend_dim(dst, isl_dim_copy(dst->dim),
4601 src->n_div, 0, 2 * src->n_div);
4602 if (!dst)
4603 return NULL;
4604 for (i = 0; i < src->n_div; ++i) {
4605 int j = find_div(dst, src, i);
4606 if (j < 0) {
4607 j = isl_basic_map_alloc_div(dst);
4608 if (j < 0)
4609 goto error;
4610 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
4611 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
4612 if (add_div_constraints(dst, j) < 0)
4613 goto error;
4615 if (j != i)
4616 isl_basic_map_swap_div(dst, i, j);
4618 return dst;
4619 error:
4620 isl_basic_map_free(dst);
4621 return NULL;
4624 struct isl_basic_set *isl_basic_set_align_divs(
4625 struct isl_basic_set *dst, struct isl_basic_set *src)
4627 return (struct isl_basic_set *)isl_basic_map_align_divs(
4628 (struct isl_basic_map *)dst, (struct isl_basic_map *)src);
4631 struct isl_map *isl_map_align_divs(struct isl_map *map)
4633 int i;
4635 if (!map)
4636 return NULL;
4637 if (map->n == 0)
4638 return map;
4639 map = isl_map_compute_divs(map);
4640 map = isl_map_cow(map);
4641 if (!map)
4642 return NULL;
4644 for (i = 1; i < map->n; ++i)
4645 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
4646 for (i = 1; i < map->n; ++i)
4647 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
4649 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4650 return map;
4653 struct isl_set *isl_set_align_divs(struct isl_set *set)
4655 return (struct isl_set *)isl_map_align_divs((struct isl_map *)set);
4658 static struct isl_map *add_cut_constraint(struct isl_map *dst,
4659 struct isl_basic_map *src, isl_int *c,
4660 unsigned len, int oppose)
4662 struct isl_basic_map *copy = NULL;
4663 int is_empty;
4664 int k;
4665 unsigned total;
4667 copy = isl_basic_map_copy(src);
4668 copy = isl_basic_map_cow(copy);
4669 if (!copy)
4670 goto error;
4671 copy = isl_basic_map_extend_constraints(copy, 0, 1);
4672 k = isl_basic_map_alloc_inequality(copy);
4673 if (k < 0)
4674 goto error;
4675 if (oppose)
4676 isl_seq_neg(copy->ineq[k], c, len);
4677 else
4678 isl_seq_cpy(copy->ineq[k], c, len);
4679 total = 1 + isl_basic_map_total_dim(copy);
4680 isl_seq_clr(copy->ineq[k]+len, total - len);
4681 isl_inequality_negate(copy, k);
4682 copy = isl_basic_map_simplify(copy);
4683 copy = isl_basic_map_finalize(copy);
4684 is_empty = isl_basic_map_is_empty(copy);
4685 if (is_empty < 0)
4686 goto error;
4687 if (!is_empty)
4688 dst = isl_map_add(dst, copy);
4689 else
4690 isl_basic_map_free(copy);
4691 return dst;
4692 error:
4693 isl_basic_map_free(copy);
4694 isl_map_free(dst);
4695 return NULL;
4698 static struct isl_map *subtract(struct isl_map *map, struct isl_basic_map *bmap)
4700 int i, j, k;
4701 unsigned flags = 0;
4702 struct isl_map *rest = NULL;
4703 unsigned max;
4704 unsigned total = isl_basic_map_total_dim(bmap);
4706 map = isl_map_cow(map);
4708 if (!map || !bmap)
4709 goto error;
4711 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
4712 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
4714 max = map->n * (2 * bmap->n_eq + bmap->n_ineq);
4715 rest = isl_map_alloc_dim(isl_dim_copy(map->dim), max, flags);
4716 if (!rest)
4717 goto error;
4719 for (i = 0; i < map->n; ++i) {
4720 map->p[i] = isl_basic_map_align_divs(map->p[i], bmap);
4721 if (!map->p[i])
4722 goto error;
4725 for (j = 0; j < map->n; ++j)
4726 map->p[j] = isl_basic_map_cow(map->p[j]);
4728 for (i = 0; i < bmap->n_eq; ++i) {
4729 for (j = 0; j < map->n; ++j) {
4730 rest = add_cut_constraint(rest,
4731 map->p[j], bmap->eq[i], 1+total, 0);
4732 if (!rest)
4733 goto error;
4735 rest = add_cut_constraint(rest,
4736 map->p[j], bmap->eq[i], 1+total, 1);
4737 if (!rest)
4738 goto error;
4740 map->p[j] = isl_basic_map_extend_constraints(map->p[j],
4741 1, 0);
4742 if (!map->p[j])
4743 goto error;
4744 k = isl_basic_map_alloc_equality(map->p[j]);
4745 if (k < 0)
4746 goto error;
4747 isl_seq_cpy(map->p[j]->eq[k], bmap->eq[i], 1+total);
4748 isl_seq_clr(map->p[j]->eq[k]+1+total,
4749 map->p[j]->n_div - bmap->n_div);
4753 for (i = 0; i < bmap->n_ineq; ++i) {
4754 for (j = 0; j < map->n; ++j) {
4755 rest = add_cut_constraint(rest,
4756 map->p[j], bmap->ineq[i], 1+total, 0);
4757 if (!rest)
4758 goto error;
4760 map->p[j] = isl_basic_map_extend_constraints(map->p[j],
4761 0, 1);
4762 if (!map->p[j])
4763 goto error;
4764 k = isl_basic_map_alloc_inequality(map->p[j]);
4765 if (k < 0)
4766 goto error;
4767 isl_seq_cpy(map->p[j]->ineq[k], bmap->ineq[i], 1+total);
4768 isl_seq_clr(map->p[j]->ineq[k]+1+total,
4769 map->p[j]->n_div - bmap->n_div);
4773 isl_map_free(map);
4774 return rest;
4775 error:
4776 isl_map_free(map);
4777 isl_map_free(rest);
4778 return NULL;
4781 struct isl_map *isl_map_subtract(struct isl_map *map1, struct isl_map *map2)
4783 int i;
4784 if (!map1 || !map2)
4785 goto error;
4787 isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
4789 if (isl_map_is_empty(map2)) {
4790 isl_map_free(map2);
4791 return map1;
4794 map1 = isl_map_compute_divs(map1);
4795 map2 = isl_map_compute_divs(map2);
4796 if (!map1 || !map2)
4797 goto error;
4799 map1 = isl_map_remove_empty_parts(map1);
4800 map2 = isl_map_remove_empty_parts(map2);
4802 for (i = 0; map1 && i < map2->n; ++i)
4803 map1 = subtract(map1, map2->p[i]);
4805 isl_map_free(map2);
4806 return map1;
4807 error:
4808 isl_map_free(map1);
4809 isl_map_free(map2);
4810 return NULL;
4813 struct isl_set *isl_set_subtract(struct isl_set *set1, struct isl_set *set2)
4815 return (struct isl_set *)
4816 isl_map_subtract(
4817 (struct isl_map *)set1, (struct isl_map *)set2);
4820 struct isl_set *isl_set_apply(struct isl_set *set, struct isl_map *map)
4822 if (!set || !map)
4823 goto error;
4824 isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
4825 map = isl_map_intersect_domain(map, set);
4826 set = isl_map_range(map);
4827 return set;
4828 error:
4829 isl_set_free(set);
4830 isl_map_free(map);
4831 return NULL;
4834 /* There is no need to cow as removing empty parts doesn't change
4835 * the meaning of the set.
4837 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
4839 int i;
4841 if (!map)
4842 return NULL;
4844 for (i = map->n-1; i >= 0; --i) {
4845 if (!ISL_F_ISSET(map->p[i], ISL_BASIC_MAP_EMPTY))
4846 continue;
4847 isl_basic_map_free(map->p[i]);
4848 if (i != map->n-1) {
4849 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4850 map->p[i] = map->p[map->n-1];
4852 map->n--;
4855 return map;
4858 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
4860 return (struct isl_set *)
4861 isl_map_remove_empty_parts((struct isl_map *)set);
4864 struct isl_basic_map *isl_map_copy_basic_map(struct isl_map *map)
4866 struct isl_basic_map *bmap;
4867 if (!map || map->n == 0)
4868 return NULL;
4869 bmap = map->p[map->n-1];
4870 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
4871 return isl_basic_map_copy(bmap);
4874 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
4876 return (struct isl_basic_set *)
4877 isl_map_copy_basic_map((struct isl_map *)set);
4880 struct isl_map *isl_map_drop_basic_map(struct isl_map *map,
4881 struct isl_basic_map *bmap)
4883 int i;
4885 if (!map || !bmap)
4886 goto error;
4887 for (i = map->n-1; i >= 0; --i) {
4888 if (map->p[i] != bmap)
4889 continue;
4890 map = isl_map_cow(map);
4891 if (!map)
4892 goto error;
4893 isl_basic_map_free(map->p[i]);
4894 if (i != map->n-1) {
4895 ISL_F_CLR(map, ISL_SET_NORMALIZED);
4896 map->p[i] = map->p[map->n-1];
4898 map->n--;
4899 return map;
4901 isl_basic_map_free(bmap);
4902 return map;
4903 error:
4904 isl_map_free(map);
4905 isl_basic_map_free(bmap);
4906 return NULL;
4909 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
4910 struct isl_basic_set *bset)
4912 return (struct isl_set *)isl_map_drop_basic_map((struct isl_map *)set,
4913 (struct isl_basic_map *)bset);
4916 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
4917 * for any common value of the parameters and dimensions preceding dim
4918 * in both basic sets, the values of dimension pos in bset1 are
4919 * smaller or larger than those in bset2.
4921 * Returns
4922 * 1 if bset1 follows bset2
4923 * -1 if bset1 precedes bset2
4924 * 0 if bset1 and bset2 are incomparable
4925 * -2 if some error occurred.
4927 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
4928 struct isl_basic_set *bset2, int pos)
4930 struct isl_dim *dims;
4931 struct isl_basic_map *bmap1 = NULL;
4932 struct isl_basic_map *bmap2 = NULL;
4933 struct isl_ctx *ctx;
4934 struct isl_vec *obj;
4935 unsigned total;
4936 unsigned nparam;
4937 unsigned dim1, dim2;
4938 isl_int num, den;
4939 enum isl_lp_result res;
4940 int cmp;
4942 if (!bset1 || !bset2)
4943 return -2;
4945 nparam = isl_basic_set_n_param(bset1);
4946 dim1 = isl_basic_set_n_dim(bset1);
4947 dim2 = isl_basic_set_n_dim(bset2);
4948 dims = isl_dim_alloc(bset1->ctx, nparam, pos, dim1 - pos);
4949 bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1), dims);
4950 dims = isl_dim_alloc(bset2->ctx, nparam, pos, dim2 - pos);
4951 bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims);
4952 if (!bmap1 || !bmap2)
4953 goto error;
4954 bmap1 = isl_basic_map_cow(bmap1);
4955 bmap1 = isl_basic_map_extend(bmap1, nparam,
4956 pos, (dim1 - pos) + (dim2 - pos),
4957 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
4958 bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
4959 if (!bmap1)
4960 goto error;
4961 total = isl_basic_map_total_dim(bmap1);
4962 ctx = bmap1->ctx;
4963 obj = isl_vec_alloc(ctx, 1 + total);
4964 isl_seq_clr(obj->block.data, 1 + total);
4965 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
4966 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
4967 if (!obj)
4968 goto error;
4969 isl_int_init(num);
4970 isl_int_init(den);
4971 res = isl_basic_map_solve_lp(bmap1, 0, obj->block.data, ctx->one,
4972 &num, &den, NULL);
4973 if (res == isl_lp_empty)
4974 cmp = 0;
4975 else if (res == isl_lp_ok && isl_int_is_pos(num))
4976 cmp = 1;
4977 else if ((res == isl_lp_ok && isl_int_is_neg(num)) ||
4978 res == isl_lp_unbounded)
4979 cmp = -1;
4980 else
4981 cmp = -2;
4982 isl_int_clear(num);
4983 isl_int_clear(den);
4984 isl_basic_map_free(bmap1);
4985 isl_vec_free(obj);
4986 return cmp;
4987 error:
4988 isl_basic_map_free(bmap1);
4989 isl_basic_map_free(bmap2);
4990 return -2;
4993 static int isl_basic_map_fast_has_fixed_var(struct isl_basic_map *bmap,
4994 unsigned pos, isl_int *val)
4996 int i;
4997 int d;
4998 unsigned total;
5000 if (!bmap)
5001 return -1;
5002 total = isl_basic_map_total_dim(bmap);
5003 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
5004 for (; d+1 > pos; --d)
5005 if (!isl_int_is_zero(bmap->eq[i][1+d]))
5006 break;
5007 if (d != pos)
5008 continue;
5009 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
5010 return 0;
5011 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
5012 return 0;
5013 if (!isl_int_is_one(bmap->eq[i][1+d]))
5014 return 0;
5015 if (val)
5016 isl_int_neg(*val, bmap->eq[i][0]);
5017 return 1;
5019 return 0;
5022 static int isl_map_fast_has_fixed_var(struct isl_map *map,
5023 unsigned pos, isl_int *val)
5025 int i;
5026 isl_int v;
5027 isl_int tmp;
5028 int fixed;
5030 if (!map)
5031 return -1;
5032 if (map->n == 0)
5033 return 0;
5034 if (map->n == 1)
5035 return isl_basic_map_fast_has_fixed_var(map->p[0], pos, val);
5036 isl_int_init(v);
5037 isl_int_init(tmp);
5038 fixed = isl_basic_map_fast_has_fixed_var(map->p[0], pos, &v);
5039 for (i = 1; fixed == 1 && i < map->n; ++i) {
5040 fixed = isl_basic_map_fast_has_fixed_var(map->p[i], pos, &tmp);
5041 if (fixed == 1 && isl_int_ne(tmp, v))
5042 fixed = 0;
5044 if (val)
5045 isl_int_set(*val, v);
5046 isl_int_clear(tmp);
5047 isl_int_clear(v);
5048 return fixed;
5051 static int isl_basic_set_fast_has_fixed_var(struct isl_basic_set *bset,
5052 unsigned pos, isl_int *val)
5054 return isl_basic_map_fast_has_fixed_var((struct isl_basic_map *)bset,
5055 pos, val);
5058 static int isl_set_fast_has_fixed_var(struct isl_set *set, unsigned pos,
5059 isl_int *val)
5061 return isl_map_fast_has_fixed_var((struct isl_map *)set, pos, val);
5064 int isl_basic_map_fast_is_fixed(struct isl_basic_map *bmap,
5065 enum isl_dim_type type, unsigned pos, isl_int *val)
5067 if (pos >= isl_basic_map_dim(bmap, type))
5068 return -1;
5069 return isl_basic_map_fast_has_fixed_var(bmap,
5070 isl_basic_map_offset(bmap, type) - 1 + pos, val);
5073 int isl_map_fast_is_fixed(struct isl_map *map,
5074 enum isl_dim_type type, unsigned pos, isl_int *val)
5076 if (pos >= isl_map_dim(map, type))
5077 return -1;
5078 return isl_map_fast_has_fixed_var(map,
5079 map_offset(map, type) - 1 + pos, val);
5082 /* Check if dimension dim has fixed value and if so and if val is not NULL,
5083 * then return this fixed value in *val.
5085 int isl_basic_set_fast_dim_is_fixed(struct isl_basic_set *bset, unsigned dim,
5086 isl_int *val)
5088 return isl_basic_set_fast_has_fixed_var(bset,
5089 isl_basic_set_n_param(bset) + dim, val);
5092 /* Check if dimension dim has fixed value and if so and if val is not NULL,
5093 * then return this fixed value in *val.
5095 int isl_set_fast_dim_is_fixed(struct isl_set *set, unsigned dim, isl_int *val)
5097 return isl_set_fast_has_fixed_var(set, isl_set_n_param(set) + dim, val);
5100 /* Check if input variable in has fixed value and if so and if val is not NULL,
5101 * then return this fixed value in *val.
5103 int isl_map_fast_input_is_fixed(struct isl_map *map, unsigned in, isl_int *val)
5105 return isl_map_fast_has_fixed_var(map, isl_map_n_param(map) + in, val);
5108 /* Check if dimension dim has an (obvious) fixed lower bound and if so
5109 * and if val is not NULL, then return this lower bound in *val.
5111 int isl_basic_set_fast_dim_has_fixed_lower_bound(struct isl_basic_set *bset,
5112 unsigned dim, isl_int *val)
5114 int i, i_eq = -1, i_ineq = -1;
5115 isl_int *c;
5116 unsigned total;
5117 unsigned nparam;
5119 if (!bset)
5120 return -1;
5121 total = isl_basic_set_total_dim(bset);
5122 nparam = isl_basic_set_n_param(bset);
5123 for (i = 0; i < bset->n_eq; ++i) {
5124 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
5125 continue;
5126 if (i_eq != -1)
5127 return 0;
5128 i_eq = i;
5130 for (i = 0; i < bset->n_ineq; ++i) {
5131 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
5132 continue;
5133 if (i_eq != -1 || i_ineq != -1)
5134 return 0;
5135 i_ineq = i;
5137 if (i_eq == -1 && i_ineq == -1)
5138 return 0;
5139 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
5140 /* The coefficient should always be one due to normalization. */
5141 if (!isl_int_is_one(c[1+nparam+dim]))
5142 return 0;
5143 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
5144 return 0;
5145 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
5146 total - nparam - dim - 1) != -1)
5147 return 0;
5148 if (val)
5149 isl_int_neg(*val, c[0]);
5150 return 1;
5153 int isl_set_fast_dim_has_fixed_lower_bound(struct isl_set *set,
5154 unsigned dim, isl_int *val)
5156 int i;
5157 isl_int v;
5158 isl_int tmp;
5159 int fixed;
5161 if (!set)
5162 return -1;
5163 if (set->n == 0)
5164 return 0;
5165 if (set->n == 1)
5166 return isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[0],
5167 dim, val);
5168 isl_int_init(v);
5169 isl_int_init(tmp);
5170 fixed = isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[0],
5171 dim, &v);
5172 for (i = 1; fixed == 1 && i < set->n; ++i) {
5173 fixed = isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[i],
5174 dim, &tmp);
5175 if (fixed == 1 && isl_int_ne(tmp, v))
5176 fixed = 0;
5178 if (val)
5179 isl_int_set(*val, v);
5180 isl_int_clear(tmp);
5181 isl_int_clear(v);
5182 return fixed;
5185 struct constraint {
5186 unsigned size;
5187 isl_int *c;
5190 static int qsort_constraint_cmp(const void *p1, const void *p2)
5192 const struct constraint *c1 = (const struct constraint *)p1;
5193 const struct constraint *c2 = (const struct constraint *)p2;
5194 unsigned size = isl_min(c1->size, c2->size);
5195 return isl_seq_cmp(c1->c, c2->c, size);
5198 static struct isl_basic_map *isl_basic_map_sort_constraints(
5199 struct isl_basic_map *bmap)
5201 int i;
5202 struct constraint *c;
5203 unsigned total;
5205 if (!bmap)
5206 return NULL;
5207 total = isl_basic_map_total_dim(bmap);
5208 c = isl_alloc_array(bmap->ctx, struct constraint, bmap->n_ineq);
5209 if (!c)
5210 goto error;
5211 for (i = 0; i < bmap->n_ineq; ++i) {
5212 c[i].size = total;
5213 c[i].c = bmap->ineq[i];
5215 qsort(c, bmap->n_ineq, sizeof(struct constraint), qsort_constraint_cmp);
5216 for (i = 0; i < bmap->n_ineq; ++i)
5217 bmap->ineq[i] = c[i].c;
5218 free(c);
5219 return bmap;
5220 error:
5221 isl_basic_map_free(bmap);
5222 return NULL;
5225 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
5227 if (!bmap)
5228 return NULL;
5229 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
5230 return bmap;
5231 bmap = isl_basic_map_convex_hull(bmap);
5232 bmap = isl_basic_map_sort_constraints(bmap);
5233 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
5234 return bmap;
5237 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
5239 return (struct isl_basic_set *)isl_basic_map_normalize(
5240 (struct isl_basic_map *)bset);
5243 static int isl_basic_map_fast_cmp(const struct isl_basic_map *bmap1,
5244 const struct isl_basic_map *bmap2)
5246 int i, cmp;
5247 unsigned total;
5249 if (bmap1 == bmap2)
5250 return 0;
5251 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
5252 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
5253 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
5254 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
5255 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
5256 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
5257 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
5258 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
5259 return 0;
5260 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
5261 return 1;
5262 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
5263 return -1;
5264 if (bmap1->n_eq != bmap2->n_eq)
5265 return bmap1->n_eq - bmap2->n_eq;
5266 if (bmap1->n_ineq != bmap2->n_ineq)
5267 return bmap1->n_ineq - bmap2->n_ineq;
5268 if (bmap1->n_div != bmap2->n_div)
5269 return bmap1->n_div - bmap2->n_div;
5270 total = isl_basic_map_total_dim(bmap1);
5271 for (i = 0; i < bmap1->n_eq; ++i) {
5272 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
5273 if (cmp)
5274 return cmp;
5276 for (i = 0; i < bmap1->n_ineq; ++i) {
5277 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
5278 if (cmp)
5279 return cmp;
5281 for (i = 0; i < bmap1->n_div; ++i) {
5282 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
5283 if (cmp)
5284 return cmp;
5286 return 0;
5289 static int isl_basic_map_fast_is_equal(struct isl_basic_map *bmap1,
5290 struct isl_basic_map *bmap2)
5292 return isl_basic_map_fast_cmp(bmap1, bmap2) == 0;
5295 static int qsort_bmap_cmp(const void *p1, const void *p2)
5297 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
5298 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
5300 return isl_basic_map_fast_cmp(bmap1, bmap2);
5303 /* We normalize in place, but if anything goes wrong we need
5304 * to return NULL, so we need to make sure we don't change the
5305 * meaning of any possible other copies of map.
5307 struct isl_map *isl_map_normalize(struct isl_map *map)
5309 int i, j;
5310 struct isl_basic_map *bmap;
5312 if (!map)
5313 return NULL;
5314 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
5315 return map;
5316 for (i = 0; i < map->n; ++i) {
5317 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
5318 if (!bmap)
5319 goto error;
5320 isl_basic_map_free(map->p[i]);
5321 map->p[i] = bmap;
5323 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
5324 ISL_F_SET(map, ISL_MAP_NORMALIZED);
5325 map = isl_map_remove_empty_parts(map);
5326 if (!map)
5327 return NULL;
5328 for (i = map->n - 1; i >= 1; --i) {
5329 if (!isl_basic_map_fast_is_equal(map->p[i-1], map->p[i]))
5330 continue;
5331 isl_basic_map_free(map->p[i-1]);
5332 for (j = i; j < map->n; ++j)
5333 map->p[j-1] = map->p[j];
5334 map->n--;
5336 return map;
5337 error:
5338 isl_map_free(map);
5339 return NULL;
5343 struct isl_set *isl_set_normalize(struct isl_set *set)
5345 return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
5348 int isl_map_fast_is_equal(struct isl_map *map1, struct isl_map *map2)
5350 int i;
5351 int equal;
5353 if (!map1 || !map2)
5354 return -1;
5356 if (map1 == map2)
5357 return 1;
5358 if (!isl_dim_equal(map1->dim, map2->dim))
5359 return 0;
5361 map1 = isl_map_copy(map1);
5362 map2 = isl_map_copy(map2);
5363 map1 = isl_map_normalize(map1);
5364 map2 = isl_map_normalize(map2);
5365 if (!map1 || !map2)
5366 goto error;
5367 equal = map1->n == map2->n;
5368 for (i = 0; equal && i < map1->n; ++i) {
5369 equal = isl_basic_map_fast_is_equal(map1->p[i], map2->p[i]);
5370 if (equal < 0)
5371 goto error;
5373 isl_map_free(map1);
5374 isl_map_free(map2);
5375 return equal;
5376 error:
5377 isl_map_free(map1);
5378 isl_map_free(map2);
5379 return -1;
5382 int isl_set_fast_is_equal(struct isl_set *set1, struct isl_set *set2)
5384 return isl_map_fast_is_equal((struct isl_map *)set1,
5385 (struct isl_map *)set2);
5388 /* Return an interval that ranges from min to max (inclusive)
5390 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
5391 isl_int min, isl_int max)
5393 int k;
5394 struct isl_basic_set *bset = NULL;
5396 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
5397 if (!bset)
5398 goto error;
5400 k = isl_basic_set_alloc_inequality(bset);
5401 if (k < 0)
5402 goto error;
5403 isl_int_set_si(bset->ineq[k][1], 1);
5404 isl_int_neg(bset->ineq[k][0], min);
5406 k = isl_basic_set_alloc_inequality(bset);
5407 if (k < 0)
5408 goto error;
5409 isl_int_set_si(bset->ineq[k][1], -1);
5410 isl_int_set(bset->ineq[k][0], max);
5412 return bset;
5413 error:
5414 isl_basic_set_free(bset);
5415 return NULL;
5418 /* Return the Cartesian product of the basic sets in list (in the given order).
5420 struct isl_basic_set *isl_basic_set_product(struct isl_basic_set_list *list)
5422 int i;
5423 unsigned dim;
5424 unsigned nparam;
5425 unsigned extra;
5426 unsigned n_eq;
5427 unsigned n_ineq;
5428 struct isl_basic_set *product = NULL;
5430 if (!list)
5431 goto error;
5432 isl_assert(list->ctx, list->n > 0, goto error);
5433 isl_assert(list->ctx, list->p[0], goto error);
5434 nparam = isl_basic_set_n_param(list->p[0]);
5435 dim = isl_basic_set_n_dim(list->p[0]);
5436 extra = list->p[0]->n_div;
5437 n_eq = list->p[0]->n_eq;
5438 n_ineq = list->p[0]->n_ineq;
5439 for (i = 1; i < list->n; ++i) {
5440 isl_assert(list->ctx, list->p[i], goto error);
5441 isl_assert(list->ctx,
5442 nparam == isl_basic_set_n_param(list->p[i]), goto error);
5443 dim += isl_basic_set_n_dim(list->p[i]);
5444 extra += list->p[i]->n_div;
5445 n_eq += list->p[i]->n_eq;
5446 n_ineq += list->p[i]->n_ineq;
5448 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
5449 n_eq, n_ineq);
5450 if (!product)
5451 goto error;
5452 dim = 0;
5453 for (i = 0; i < list->n; ++i) {
5454 isl_basic_set_add_constraints(product,
5455 isl_basic_set_copy(list->p[i]), dim);
5456 dim += isl_basic_set_n_dim(list->p[i]);
5458 isl_basic_set_list_free(list);
5459 return product;
5460 error:
5461 isl_basic_set_free(product);
5462 isl_basic_set_list_free(list);
5463 return NULL;
5466 struct isl_basic_map *isl_basic_map_product(
5467 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
5469 struct isl_dim *dim_result = NULL;
5470 struct isl_basic_map *bmap;
5471 unsigned in1, in2, out1, out2, nparam, total, pos;
5472 struct isl_dim_map *dim_map1, *dim_map2;
5474 if (!bmap1 || !bmap2)
5475 goto error;
5477 isl_assert(bmap1->ctx, isl_dim_match(bmap1->dim, isl_dim_param,
5478 bmap2->dim, isl_dim_param), goto error);
5479 dim_result = isl_dim_product(isl_dim_copy(bmap1->dim),
5480 isl_dim_copy(bmap2->dim));
5482 in1 = isl_basic_map_n_in(bmap1);
5483 in2 = isl_basic_map_n_in(bmap2);
5484 out1 = isl_basic_map_n_out(bmap1);
5485 out2 = isl_basic_map_n_out(bmap2);
5486 nparam = isl_basic_map_n_param(bmap1);
5488 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
5489 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
5490 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
5491 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
5492 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
5493 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
5494 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
5495 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
5496 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
5497 isl_dim_map_div(dim_map1, bmap1, pos += out2);
5498 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
5500 bmap = isl_basic_map_alloc_dim(dim_result,
5501 bmap1->n_div + bmap2->n_div,
5502 bmap1->n_eq + bmap2->n_eq,
5503 bmap1->n_ineq + bmap2->n_ineq);
5504 bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
5505 bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
5506 bmap = isl_basic_map_simplify(bmap);
5507 return isl_basic_map_finalize(bmap);
5508 error:
5509 isl_basic_map_free(bmap1);
5510 isl_basic_map_free(bmap2);
5511 return NULL;
5514 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
5516 struct isl_map *isl_map_product(struct isl_map *map1, struct isl_map *map2)
5518 unsigned flags = 0;
5519 struct isl_map *result;
5520 int i, j;
5522 if (!map1 || !map2)
5523 goto error;
5525 isl_assert(map1->ctx, isl_dim_match(map1->dim, isl_dim_param,
5526 map2->dim, isl_dim_param), goto error);
5528 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
5529 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
5530 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
5532 result = isl_map_alloc_dim(isl_dim_product(isl_dim_copy(map1->dim),
5533 isl_dim_copy(map2->dim)),
5534 map1->n * map2->n, flags);
5535 if (!result)
5536 goto error;
5537 for (i = 0; i < map1->n; ++i)
5538 for (j = 0; j < map2->n; ++j) {
5539 struct isl_basic_map *part;
5540 part = isl_basic_map_product(
5541 isl_basic_map_copy(map1->p[i]),
5542 isl_basic_map_copy(map2->p[j]));
5543 if (isl_basic_map_is_empty(part))
5544 isl_basic_map_free(part);
5545 else
5546 result = isl_map_add(result, part);
5547 if (!result)
5548 goto error;
5550 isl_map_free(map1);
5551 isl_map_free(map2);
5552 return result;
5553 error:
5554 isl_map_free(map1);
5555 isl_map_free(map2);
5556 return NULL;
5559 /* Given two set A and B, construct its Cartesian product A x B.
5561 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
5563 return (struct isl_set *)isl_map_product((struct isl_map *)set1,
5564 (struct isl_map *)set2);
5567 uint32_t isl_basic_set_get_hash(struct isl_basic_set *bset)
5569 int i;
5570 uint32_t hash = isl_hash_init();
5571 unsigned total;
5573 if (!bset)
5574 return 0;
5575 bset = isl_basic_set_copy(bset);
5576 bset = isl_basic_set_normalize(bset);
5577 if (!bset)
5578 return 0;
5579 total = isl_basic_set_total_dim(bset);
5580 isl_hash_byte(hash, bset->n_eq & 0xFF);
5581 for (i = 0; i < bset->n_eq; ++i) {
5582 uint32_t c_hash;
5583 c_hash = isl_seq_get_hash(bset->eq[i], 1 + total);
5584 isl_hash_hash(hash, c_hash);
5586 isl_hash_byte(hash, bset->n_ineq & 0xFF);
5587 for (i = 0; i < bset->n_ineq; ++i) {
5588 uint32_t c_hash;
5589 c_hash = isl_seq_get_hash(bset->ineq[i], 1 + total);
5590 isl_hash_hash(hash, c_hash);
5592 isl_hash_byte(hash, bset->n_div & 0xFF);
5593 for (i = 0; i < bset->n_div; ++i) {
5594 uint32_t c_hash;
5595 if (isl_int_is_zero(bset->div[i][0]))
5596 continue;
5597 isl_hash_byte(hash, i & 0xFF);
5598 c_hash = isl_seq_get_hash(bset->div[i], 1 + 1 + total);
5599 isl_hash_hash(hash, c_hash);
5601 isl_basic_set_free(bset);
5602 return hash;
5605 uint32_t isl_set_get_hash(struct isl_set *set)
5607 int i;
5608 uint32_t hash;
5610 if (!set)
5611 return 0;
5612 set = isl_set_copy(set);
5613 set = isl_set_normalize(set);
5614 if (!set)
5615 return 0;
5617 hash = isl_hash_init();
5618 for (i = 0; i < set->n; ++i) {
5619 uint32_t bset_hash;
5620 bset_hash = isl_basic_set_get_hash(set->p[i]);
5621 isl_hash_hash(hash, bset_hash);
5624 isl_set_free(set);
5626 return hash;
5629 /* Check if the value for dimension dim is completely determined
5630 * by the values of the other parameters and variables.
5631 * That is, check if dimension dim is involved in an equality.
5633 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
5635 int i;
5636 unsigned nparam;
5638 if (!bset)
5639 return -1;
5640 nparam = isl_basic_set_n_param(bset);
5641 for (i = 0; i < bset->n_eq; ++i)
5642 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
5643 return 1;
5644 return 0;
5647 /* Check if the value for dimension dim is completely determined
5648 * by the values of the other parameters and variables.
5649 * That is, check if dimension dim is involved in an equality
5650 * for each of the subsets.
5652 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
5654 int i;
5656 if (!set)
5657 return -1;
5658 for (i = 0; i < set->n; ++i) {
5659 int unique;
5660 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
5661 if (unique != 1)
5662 return unique;
5664 return 1;
5667 int isl_map_foreach_basic_map(__isl_keep isl_map *map,
5668 int (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
5670 int i;
5672 if (!map)
5673 return -1;
5675 for (i = 0; i < map->n; ++i)
5676 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
5677 return -1;
5679 return 0;
5682 int isl_set_foreach_basic_set(__isl_keep isl_set *set,
5683 int (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
5685 int i;
5687 if (!set)
5688 return -1;
5690 for (i = 0; i < set->n; ++i)
5691 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
5692 return -1;
5694 return 0;
5697 __isl_give isl_map *isl_set_lifting(__isl_take isl_set *set)
5699 struct isl_dim *dim;
5700 struct isl_basic_map *bmap;
5701 unsigned n_set;
5702 unsigned n_div;
5703 unsigned n_param;
5704 unsigned total;
5705 int i, k, l;
5707 set = isl_set_align_divs(set);
5709 if (!set)
5710 return NULL;
5712 dim = isl_set_get_dim(set);
5713 if (set->n == 0 || set->p[0]->n_div == 0) {
5714 isl_set_free(set);
5715 return isl_map_identity(dim);
5718 n_div = set->p[0]->n_div;
5719 dim = isl_dim_map(dim);
5720 n_param = isl_dim_size(dim, isl_dim_param);
5721 n_set = isl_dim_size(dim, isl_dim_in);
5722 dim = isl_dim_extend(dim, n_param, n_set, n_set + n_div);
5723 bmap = isl_basic_map_alloc_dim(dim, 0, n_set, 2 * n_div);
5724 for (i = 0; i < n_set; ++i)
5725 bmap = var_equal(bmap, i);
5727 total = n_param + n_set + n_set + n_div;
5728 for (i = 0; i < n_div; ++i) {
5729 k = isl_basic_map_alloc_inequality(bmap);
5730 if (k < 0)
5731 goto error;
5732 isl_seq_cpy(bmap->ineq[k], set->p[0]->div[i]+1, 1+n_param);
5733 isl_seq_clr(bmap->ineq[k]+1+n_param, n_set);
5734 isl_seq_cpy(bmap->ineq[k]+1+n_param+n_set,
5735 set->p[0]->div[i]+1+1+n_param, n_set + n_div);
5736 isl_int_neg(bmap->ineq[k][1+n_param+n_set+n_set+i],
5737 set->p[0]->div[i][0]);
5739 l = isl_basic_map_alloc_inequality(bmap);
5740 if (l < 0)
5741 goto error;
5742 isl_seq_neg(bmap->ineq[l], bmap->ineq[k], 1 + total);
5743 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0],
5744 set->p[0]->div[i][0]);
5745 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
5748 isl_set_free(set);
5749 return isl_map_from_basic_map(bmap);
5750 error:
5751 isl_set_free(set);
5752 isl_basic_map_free(bmap);
5753 return NULL;
5756 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
5758 unsigned dim;
5759 int size = 0;
5761 if (!bset)
5762 return -1;
5764 dim = isl_basic_set_total_dim(bset);
5765 size += bset->n_eq * (1 + dim);
5766 size += bset->n_ineq * (1 + dim);
5767 size += bset->n_div * (2 + dim);
5769 return size;
5772 int isl_set_size(__isl_keep isl_set *set)
5774 int i;
5775 int size = 0;
5777 if (!set)
5778 return -1;
5780 for (i = 0; i < set->n; ++i)
5781 size += isl_basic_set_size(set->p[i]);
5783 return size;