isl_equalities.c: remove unused variable
[isl.git] / isl_map.c
bloba497b5c64cf795ce4ebc1d0e0ac535e96215ae26
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 struct isl_dim *dim = bmap->dim;
99 switch (type) {
100 case isl_dim_param:
101 case isl_dim_in:
102 case isl_dim_out: return isl_dim_size(bmap->dim, type);
103 case isl_dim_div: return bmap->n_div;
104 case isl_dim_all: return isl_basic_map_total_dim(bmap);
108 unsigned isl_map_dim(const struct isl_map *map, enum isl_dim_type type)
110 return n(map->dim, type);
113 unsigned isl_set_dim(const struct isl_set *set, enum isl_dim_type type)
115 return n(set->dim, type);
118 unsigned isl_basic_map_offset(struct isl_basic_map *bmap,
119 enum isl_dim_type type)
121 struct isl_dim *dim = bmap->dim;
122 switch (type) {
123 case isl_dim_param: return 1;
124 case isl_dim_in: return 1 + dim->nparam;
125 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
126 case isl_dim_div: return 1 + dim->nparam + dim->n_in + dim->n_out;
130 static unsigned map_offset(struct isl_map *map, enum isl_dim_type type)
132 return pos(map->dim, type);
135 unsigned isl_basic_set_dim(const struct isl_basic_set *bset,
136 enum isl_dim_type type)
138 return isl_basic_map_dim((const struct isl_basic_map*)bset, type);
141 unsigned isl_basic_set_n_dim(const struct isl_basic_set *bset)
143 return bset->dim->n_out;
146 unsigned isl_basic_set_n_param(const struct isl_basic_set *bset)
148 return bset->dim->nparam;
151 unsigned isl_basic_set_total_dim(const struct isl_basic_set *bset)
153 return isl_dim_total(bset->dim) + bset->n_div;
156 unsigned isl_set_n_dim(const struct isl_set *set)
158 return set->dim->n_out;
161 unsigned isl_set_n_param(const struct isl_set *set)
163 return set->dim->nparam;
166 unsigned isl_basic_map_n_in(const struct isl_basic_map *bmap)
168 return bmap->dim->n_in;
171 unsigned isl_basic_map_n_out(const struct isl_basic_map *bmap)
173 return bmap->dim->n_out;
176 unsigned isl_basic_map_n_param(const struct isl_basic_map *bmap)
178 return bmap->dim->nparam;
181 unsigned isl_basic_map_n_div(const struct isl_basic_map *bmap)
183 return bmap->n_div;
186 unsigned isl_basic_map_total_dim(const struct isl_basic_map *bmap)
188 return isl_dim_total(bmap->dim) + bmap->n_div;
191 unsigned isl_map_n_in(const struct isl_map *map)
193 return map->dim->n_in;
196 unsigned isl_map_n_out(const struct isl_map *map)
198 return map->dim->n_out;
201 unsigned isl_map_n_param(const struct isl_map *map)
203 return map->dim->nparam;
206 int isl_map_compatible_domain(struct isl_map *map, struct isl_set *set)
208 return map->dim->n_in == set->dim->n_out &&
209 map->dim->nparam == set->dim->nparam;
212 int isl_basic_map_compatible_domain(struct isl_basic_map *bmap,
213 struct isl_basic_set *bset)
215 return bmap->dim->n_in == bset->dim->n_out &&
216 bmap->dim->nparam == bset->dim->nparam;
219 int isl_basic_map_compatible_range(struct isl_basic_map *bmap,
220 struct isl_basic_set *bset)
222 return bmap->dim->n_out == bset->dim->n_out &&
223 bmap->dim->nparam == bset->dim->nparam;
226 struct isl_dim *isl_basic_map_get_dim(struct isl_basic_map *bmap)
228 if (!bmap)
229 return NULL;
230 return isl_dim_copy(bmap->dim);
233 struct isl_dim *isl_basic_set_get_dim(struct isl_basic_set *bset)
235 if (!bset)
236 return NULL;
237 return isl_dim_copy(bset->dim);
240 struct isl_dim *isl_map_get_dim(struct isl_map *map)
242 if (!map)
243 return NULL;
244 return isl_dim_copy(map->dim);
247 struct isl_dim *isl_set_get_dim(struct isl_set *set)
249 if (!set)
250 return NULL;
251 return isl_dim_copy(set->dim);
254 static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
255 struct isl_basic_map *bmap, unsigned extra,
256 unsigned n_eq, unsigned n_ineq)
258 int i;
259 size_t row_size = 1 + isl_dim_total(bmap->dim) + extra;
261 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
262 if (isl_blk_is_error(bmap->block)) {
263 free(bmap);
264 return NULL;
267 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
268 if (!bmap->ineq) {
269 isl_blk_free(ctx, bmap->block);
270 free(bmap);
271 return NULL;
274 if (extra == 0) {
275 bmap->block2 = isl_blk_empty();
276 bmap->div = NULL;
277 } else {
278 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
279 if (isl_blk_is_error(bmap->block2)) {
280 free(bmap->ineq);
281 isl_blk_free(ctx, bmap->block);
282 free(bmap);
283 return NULL;
286 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
287 if (!bmap->div) {
288 isl_blk_free(ctx, bmap->block2);
289 free(bmap->ineq);
290 isl_blk_free(ctx, bmap->block);
291 free(bmap);
292 return NULL;
296 for (i = 0; i < n_ineq + n_eq; ++i)
297 bmap->ineq[i] = bmap->block.data + i * row_size;
299 for (i = 0; i < extra; ++i)
300 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
302 bmap->ctx = ctx;
303 isl_ctx_ref(ctx);
304 bmap->ref = 1;
305 bmap->flags = 0;
306 bmap->c_size = n_eq + n_ineq;
307 bmap->eq = bmap->ineq + n_ineq;
308 bmap->extra = extra;
309 bmap->n_eq = 0;
310 bmap->n_ineq = 0;
311 bmap->n_div = 0;
312 bmap->sample = NULL;
314 return bmap;
315 error:
316 isl_basic_map_free(bmap);
317 return NULL;
320 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
321 unsigned nparam, unsigned dim, unsigned extra,
322 unsigned n_eq, unsigned n_ineq)
324 struct isl_basic_map *bmap;
325 bmap = isl_basic_map_alloc(ctx, nparam, 0, dim, extra, n_eq, n_ineq);
326 return (struct isl_basic_set *)bmap;
329 struct isl_basic_set *isl_basic_set_alloc_dim(struct isl_dim *dim,
330 unsigned extra, unsigned n_eq, unsigned n_ineq)
332 struct isl_basic_map *bmap;
333 if (!dim)
334 return NULL;
335 isl_assert(dim->ctx, dim->n_in == 0, return NULL);
336 bmap = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
337 return (struct isl_basic_set *)bmap;
340 struct isl_basic_map *isl_basic_map_alloc_dim(struct isl_dim *dim,
341 unsigned extra, unsigned n_eq, unsigned n_ineq)
343 struct isl_basic_map *bmap;
345 if (!dim)
346 return NULL;
347 bmap = isl_alloc_type(dim->ctx, struct isl_basic_map);
348 if (!bmap)
349 goto error;
350 bmap->dim = dim;
352 return basic_map_init(dim->ctx, bmap, extra, n_eq, n_ineq);
353 error:
354 isl_dim_free(dim);
355 return NULL;
358 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
359 unsigned nparam, unsigned in, unsigned out, unsigned extra,
360 unsigned n_eq, unsigned n_ineq)
362 struct isl_basic_map *bmap;
363 struct isl_dim *dim;
365 dim = isl_dim_alloc(ctx, nparam, in, out);
366 if (!dim)
367 return NULL;
369 bmap = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
370 return bmap;
373 static void dup_constraints(
374 struct isl_basic_map *dst, struct isl_basic_map *src)
376 int i;
377 unsigned total = isl_basic_map_total_dim(src);
379 for (i = 0; i < src->n_eq; ++i) {
380 int j = isl_basic_map_alloc_equality(dst);
381 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
384 for (i = 0; i < src->n_ineq; ++i) {
385 int j = isl_basic_map_alloc_inequality(dst);
386 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
389 for (i = 0; i < src->n_div; ++i) {
390 int j = isl_basic_map_alloc_div(dst);
391 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
393 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
396 struct isl_basic_map *isl_basic_map_dup(struct isl_basic_map *bmap)
398 struct isl_basic_map *dup;
400 if (!bmap)
401 return NULL;
402 dup = isl_basic_map_alloc_dim(isl_dim_copy(bmap->dim),
403 bmap->n_div, bmap->n_eq, bmap->n_ineq);
404 if (!dup)
405 return NULL;
406 dup_constraints(dup, bmap);
407 dup->flags = bmap->flags;
408 dup->sample = isl_vec_copy(bmap->sample);
409 return dup;
412 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
414 struct isl_basic_map *dup;
416 dup = isl_basic_map_dup((struct isl_basic_map *)bset);
417 return (struct isl_basic_set *)dup;
420 struct isl_basic_set *isl_basic_set_copy(struct isl_basic_set *bset)
422 if (!bset)
423 return NULL;
425 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
426 bset->ref++;
427 return bset;
429 return isl_basic_set_dup(bset);
432 struct isl_set *isl_set_copy(struct isl_set *set)
434 if (!set)
435 return NULL;
437 set->ref++;
438 return set;
441 struct isl_basic_map *isl_basic_map_copy(struct isl_basic_map *bmap)
443 if (!bmap)
444 return NULL;
446 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
447 bmap->ref++;
448 return bmap;
450 return isl_basic_map_dup(bmap);
453 struct isl_map *isl_map_copy(struct isl_map *map)
455 if (!map)
456 return NULL;
458 map->ref++;
459 return map;
462 void isl_basic_map_free(struct isl_basic_map *bmap)
464 if (!bmap)
465 return;
467 if (--bmap->ref > 0)
468 return;
470 isl_ctx_deref(bmap->ctx);
471 free(bmap->div);
472 isl_blk_free(bmap->ctx, bmap->block2);
473 free(bmap->ineq);
474 isl_blk_free(bmap->ctx, bmap->block);
475 isl_vec_free(bmap->sample);
476 isl_dim_free(bmap->dim);
477 free(bmap);
480 void isl_basic_set_free(struct isl_basic_set *bset)
482 isl_basic_map_free((struct isl_basic_map *)bset);
485 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
487 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
490 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
492 struct isl_ctx *ctx;
493 if (!bmap)
494 return -1;
495 ctx = bmap->ctx;
496 isl_assert(ctx, room_for_con(bmap, 1), return -1);
497 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
498 return -1);
499 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
500 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
501 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
502 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
503 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
504 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
505 isl_int *t;
506 int j = isl_basic_map_alloc_inequality(bmap);
507 if (j < 0)
508 return -1;
509 t = bmap->ineq[j];
510 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
511 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
512 bmap->eq[-1] = t;
513 bmap->n_eq++;
514 bmap->n_ineq--;
515 bmap->eq--;
516 return 0;
518 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
519 bmap->extra - bmap->n_div);
520 return bmap->n_eq++;
523 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
525 return isl_basic_map_alloc_equality((struct isl_basic_map *)bset);
528 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
530 if (!bmap)
531 return -1;
532 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
533 bmap->n_eq -= n;
534 return 0;
537 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
539 return isl_basic_map_free_equality((struct isl_basic_map *)bset, n);
542 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
544 isl_int *t;
545 if (!bmap)
546 return -1;
547 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
549 if (pos != bmap->n_eq - 1) {
550 t = bmap->eq[pos];
551 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
552 bmap->eq[bmap->n_eq - 1] = t;
554 bmap->n_eq--;
555 return 0;
558 int isl_basic_set_drop_equality(struct isl_basic_set *bset, unsigned pos)
560 return isl_basic_map_drop_equality((struct isl_basic_map *)bset, pos);
563 void isl_basic_map_inequality_to_equality(
564 struct isl_basic_map *bmap, unsigned pos)
566 isl_int *t;
568 t = bmap->ineq[pos];
569 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
570 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
571 bmap->eq[-1] = t;
572 bmap->n_eq++;
573 bmap->n_ineq--;
574 bmap->eq--;
575 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
576 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
577 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
578 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
581 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
583 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
586 int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap)
588 struct isl_ctx *ctx;
589 if (!bmap)
590 return -1;
591 ctx = bmap->ctx;
592 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
593 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
594 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
595 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
596 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
597 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
598 1 + isl_basic_map_total_dim(bmap),
599 bmap->extra - bmap->n_div);
600 return bmap->n_ineq++;
603 int isl_basic_set_alloc_inequality(struct isl_basic_set *bset)
605 return isl_basic_map_alloc_inequality((struct isl_basic_map *)bset);
608 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
610 if (!bmap)
611 return -1;
612 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
613 bmap->n_ineq -= n;
614 return 0;
617 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
619 return isl_basic_map_free_inequality((struct isl_basic_map *)bset, n);
622 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
624 isl_int *t;
625 if (!bmap)
626 return -1;
627 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
629 if (pos != bmap->n_ineq - 1) {
630 t = bmap->ineq[pos];
631 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
632 bmap->ineq[bmap->n_ineq - 1] = t;
633 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
635 bmap->n_ineq--;
636 return 0;
639 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
641 return isl_basic_map_drop_inequality((struct isl_basic_map *)bset, pos);
644 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
646 if (!bmap)
647 return -1;
648 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
649 isl_seq_clr(bmap->div[bmap->n_div] +
650 1 + 1 + isl_basic_map_total_dim(bmap),
651 bmap->extra - bmap->n_div);
652 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
653 return bmap->n_div++;
656 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
658 return isl_basic_map_alloc_div((struct isl_basic_map *)bset);
661 int isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
663 if (!bmap)
664 return -1;
665 isl_assert(bmap->ctx, n <= bmap->n_div, return -1);
666 bmap->n_div -= n;
667 return 0;
670 int isl_basic_set_free_div(struct isl_basic_set *bset, unsigned n)
672 return isl_basic_map_free_div((struct isl_basic_map *)bset, n);
675 /* Copy constraint from src to dst, putting the vars of src at offset
676 * dim_off in dst and the divs of src at offset div_off in dst.
677 * If both sets are actually map, then dim_off applies to the input
678 * variables.
680 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
681 struct isl_basic_map *src_map, isl_int *src,
682 unsigned in_off, unsigned out_off, unsigned div_off)
684 unsigned src_nparam = isl_basic_map_n_param(src_map);
685 unsigned dst_nparam = isl_basic_map_n_param(dst_map);
686 unsigned src_in = isl_basic_map_n_in(src_map);
687 unsigned dst_in = isl_basic_map_n_in(dst_map);
688 unsigned src_out = isl_basic_map_n_out(src_map);
689 unsigned dst_out = isl_basic_map_n_out(dst_map);
690 isl_int_set(dst[0], src[0]);
691 isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
692 if (dst_nparam > src_nparam)
693 isl_seq_clr(dst+1+src_nparam,
694 dst_nparam - src_nparam);
695 isl_seq_clr(dst+1+dst_nparam, in_off);
696 isl_seq_cpy(dst+1+dst_nparam+in_off,
697 src+1+src_nparam,
698 isl_min(dst_in-in_off, src_in));
699 if (dst_in-in_off > src_in)
700 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
701 dst_in - in_off - src_in);
702 isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
703 isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
704 src+1+src_nparam+src_in,
705 isl_min(dst_out-out_off, src_out));
706 if (dst_out-out_off > src_out)
707 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
708 dst_out - out_off - src_out);
709 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
710 isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
711 src+1+src_nparam+src_in+src_out,
712 isl_min(dst_map->extra-div_off, src_map->n_div));
713 if (dst_map->n_div-div_off > src_map->n_div)
714 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
715 div_off+src_map->n_div,
716 dst_map->n_div - div_off - src_map->n_div);
719 static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
720 struct isl_basic_map *src_map, isl_int *src,
721 unsigned in_off, unsigned out_off, unsigned div_off)
723 isl_int_set(dst[0], src[0]);
724 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
727 static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
728 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
730 int i;
731 unsigned div_off;
733 if (!bmap1 || !bmap2)
734 goto error;
736 div_off = bmap1->n_div;
738 for (i = 0; i < bmap2->n_eq; ++i) {
739 int i1 = isl_basic_map_alloc_equality(bmap1);
740 if (i1 < 0)
741 goto error;
742 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
743 i_pos, o_pos, div_off);
746 for (i = 0; i < bmap2->n_ineq; ++i) {
747 int i1 = isl_basic_map_alloc_inequality(bmap1);
748 if (i1 < 0)
749 goto error;
750 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
751 i_pos, o_pos, div_off);
754 for (i = 0; i < bmap2->n_div; ++i) {
755 int i1 = isl_basic_map_alloc_div(bmap1);
756 if (i1 < 0)
757 goto error;
758 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
759 i_pos, o_pos, div_off);
762 isl_basic_map_free(bmap2);
764 return bmap1;
766 error:
767 isl_basic_map_free(bmap1);
768 isl_basic_map_free(bmap2);
769 return NULL;
772 static void copy_constraint_dim_map(isl_int *dst, isl_int *src,
773 struct isl_dim_map *dim_map)
775 int i;
777 for (i = 0; i < dim_map->len; ++i) {
778 if (dim_map->pos[i] < 0)
779 isl_int_set_si(dst[i], 0);
780 else
781 isl_int_set(dst[i], src[dim_map->pos[i]]);
785 static void copy_div_dim_map(isl_int *dst, isl_int *src,
786 struct isl_dim_map *dim_map)
788 isl_int_set(dst[0], src[0]);
789 copy_constraint_dim_map(dst+1, src+1, dim_map);
792 static struct isl_basic_map *add_constraints_dim_map(struct isl_basic_map *dst,
793 struct isl_basic_map *src, struct isl_dim_map *dim_map)
795 int i;
797 if (!src || !dst || !dim_map)
798 goto error;
800 for (i = 0; i < src->n_eq; ++i) {
801 int i1 = isl_basic_map_alloc_equality(dst);
802 if (i1 < 0)
803 goto error;
804 copy_constraint_dim_map(dst->eq[i1], src->eq[i], dim_map);
807 for (i = 0; i < src->n_ineq; ++i) {
808 int i1 = isl_basic_map_alloc_inequality(dst);
809 if (i1 < 0)
810 goto error;
811 copy_constraint_dim_map(dst->ineq[i1], src->ineq[i], dim_map);
814 for (i = 0; i < src->n_div; ++i) {
815 int i1 = isl_basic_map_alloc_div(dst);
816 if (i1 < 0)
817 goto error;
818 copy_div_dim_map(dst->div[i1], src->div[i], dim_map);
821 free(dim_map);
822 isl_basic_map_free(src);
824 return dst;
825 error:
826 free(dim_map);
827 isl_basic_map_free(src);
828 isl_basic_map_free(dst);
829 return NULL;
832 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
833 struct isl_basic_set *bset2, unsigned pos)
835 return (struct isl_basic_set *)
836 add_constraints((struct isl_basic_map *)bset1,
837 (struct isl_basic_map *)bset2, 0, pos);
840 struct isl_basic_map *isl_basic_map_extend_dim(struct isl_basic_map *base,
841 struct isl_dim *dim, unsigned extra,
842 unsigned n_eq, unsigned n_ineq)
844 struct isl_basic_map *ext;
845 unsigned flags;
846 int dims_ok;
848 if (!dim)
849 goto error;
851 if (!base)
852 goto error;
854 dims_ok = isl_dim_equal(base->dim, dim) &&
855 base->extra >= base->n_div + extra;
857 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
858 room_for_ineq(base, n_ineq)) {
859 isl_dim_free(dim);
860 return base;
863 isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error);
864 isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error);
865 isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error);
866 extra += base->extra;
867 n_eq += base->n_eq;
868 n_ineq += base->n_ineq;
870 ext = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
871 dim = NULL;
872 if (!ext)
873 goto error;
875 if (dims_ok)
876 ext->sample = isl_vec_copy(base->sample);
877 flags = base->flags;
878 ext = add_constraints(ext, base, 0, 0);
879 if (ext) {
880 ext->flags = flags;
881 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
884 return ext;
886 error:
887 isl_dim_free(dim);
888 isl_basic_map_free(base);
889 return NULL;
892 struct isl_basic_set *isl_basic_set_extend_dim(struct isl_basic_set *base,
893 struct isl_dim *dim, unsigned extra,
894 unsigned n_eq, unsigned n_ineq)
896 return (struct isl_basic_set *)
897 isl_basic_map_extend_dim((struct isl_basic_map *)base, dim,
898 extra, n_eq, n_ineq);
901 struct isl_basic_map *isl_basic_map_extend_constraints(
902 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
904 if (!base)
905 return NULL;
906 return isl_basic_map_extend_dim(base, isl_dim_copy(base->dim),
907 0, n_eq, n_ineq);
910 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
911 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
912 unsigned n_eq, unsigned n_ineq)
914 struct isl_basic_map *bmap;
915 struct isl_dim *dim;
917 if (!base)
918 return NULL;
919 dim = isl_dim_alloc(base->ctx, nparam, n_in, n_out);
920 if (!dim)
921 return NULL;
923 bmap = isl_basic_map_extend_dim(base, dim, extra, n_eq, n_ineq);
924 return bmap;
927 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
928 unsigned nparam, unsigned dim, unsigned extra,
929 unsigned n_eq, unsigned n_ineq)
931 return (struct isl_basic_set *)
932 isl_basic_map_extend((struct isl_basic_map *)base,
933 nparam, 0, dim, extra, n_eq, n_ineq);
936 struct isl_basic_set *isl_basic_set_extend_constraints(
937 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
939 return (struct isl_basic_set *)
940 isl_basic_map_extend_constraints((struct isl_basic_map *)base,
941 n_eq, n_ineq);
944 struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset)
946 return (struct isl_basic_set *)
947 isl_basic_map_cow((struct isl_basic_map *)bset);
950 struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
952 if (!bmap)
953 return NULL;
955 if (bmap->ref > 1) {
956 bmap->ref--;
957 bmap = isl_basic_map_dup(bmap);
959 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
960 return bmap;
963 struct isl_set *isl_set_cow(struct isl_set *set)
965 if (!set)
966 return NULL;
968 if (set->ref == 1)
969 return set;
970 set->ref--;
971 return isl_set_dup(set);
974 struct isl_map *isl_map_cow(struct isl_map *map)
976 if (!map)
977 return NULL;
979 if (map->ref == 1)
980 return map;
981 map->ref--;
982 return isl_map_dup(map);
985 static void swap_vars(struct isl_blk blk, isl_int *a,
986 unsigned a_len, unsigned b_len)
988 isl_seq_cpy(blk.data, a+a_len, b_len);
989 isl_seq_cpy(blk.data+b_len, a, a_len);
990 isl_seq_cpy(a, blk.data, b_len+a_len);
993 struct isl_basic_set *isl_basic_set_swap_vars(
994 struct isl_basic_set *bset, unsigned n)
996 int i;
997 struct isl_blk blk;
998 unsigned dim;
999 unsigned nparam;
1001 if (!bset)
1002 goto error;
1004 nparam = isl_basic_set_n_param(bset);
1005 dim = isl_basic_set_n_dim(bset);
1006 isl_assert(bset->ctx, n <= dim, goto error);
1008 if (n == dim)
1009 return bset;
1011 bset = isl_basic_set_cow(bset);
1012 if (!bset)
1013 return NULL;
1015 blk = isl_blk_alloc(bset->ctx, dim);
1016 if (isl_blk_is_error(blk))
1017 goto error;
1019 for (i = 0; i < bset->n_eq; ++i)
1020 swap_vars(blk,
1021 bset->eq[i]+1+nparam, n, dim - n);
1023 for (i = 0; i < bset->n_ineq; ++i)
1024 swap_vars(blk,
1025 bset->ineq[i]+1+nparam, n, dim - n);
1027 for (i = 0; i < bset->n_div; ++i)
1028 swap_vars(blk,
1029 bset->div[i]+1+1+nparam, n, dim - n);
1031 isl_blk_free(bset->ctx, blk);
1033 ISL_F_CLR(bset, ISL_BASIC_SET_NORMALIZED);
1034 return bset;
1036 error:
1037 isl_basic_set_free(bset);
1038 return NULL;
1041 struct isl_set *isl_set_swap_vars(struct isl_set *set, unsigned n)
1043 int i;
1044 set = isl_set_cow(set);
1045 if (!set)
1046 return NULL;
1048 for (i = 0; i < set->n; ++i) {
1049 set->p[i] = isl_basic_set_swap_vars(set->p[i], n);
1050 if (!set->p[i]) {
1051 isl_set_free(set);
1052 return NULL;
1055 ISL_F_CLR(set, ISL_SET_NORMALIZED);
1056 return set;
1059 struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
1061 int i = 0;
1062 unsigned total;
1063 if (!bmap)
1064 goto error;
1065 total = isl_basic_map_total_dim(bmap);
1066 isl_basic_map_free_div(bmap, bmap->n_div);
1067 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1068 if (bmap->n_eq > 0)
1069 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
1070 else {
1071 isl_basic_map_alloc_equality(bmap);
1072 if (i < 0)
1073 goto error;
1075 isl_int_set_si(bmap->eq[i][0], 1);
1076 isl_seq_clr(bmap->eq[i]+1, total);
1077 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
1078 return isl_basic_map_finalize(bmap);
1079 error:
1080 isl_basic_map_free(bmap);
1081 return NULL;
1084 struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
1086 return (struct isl_basic_set *)
1087 isl_basic_map_set_to_empty((struct isl_basic_map *)bset);
1090 void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
1092 int i;
1093 unsigned off = isl_dim_total(bmap->dim);
1094 isl_int *t = bmap->div[a];
1095 bmap->div[a] = bmap->div[b];
1096 bmap->div[b] = t;
1098 for (i = 0; i < bmap->n_eq; ++i)
1099 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
1101 for (i = 0; i < bmap->n_ineq; ++i)
1102 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
1104 for (i = 0; i < bmap->n_div; ++i)
1105 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
1106 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1109 /* Eliminate the specified n dimensions starting at first from the
1110 * constraints using Fourier-Motzkin, The dimensions themselves
1111 * are not removed.
1113 struct isl_set *isl_set_eliminate_dims(struct isl_set *set,
1114 unsigned first, unsigned n)
1116 int i;
1117 unsigned nparam;
1119 if (!set)
1120 return NULL;
1121 if (n == 0)
1122 return set;
1124 set = isl_set_cow(set);
1125 if (!set)
1126 return NULL;
1127 isl_assert(set->ctx, first+n <= isl_set_n_dim(set), goto error);
1128 nparam = isl_set_n_param(set);
1130 for (i = 0; i < set->n; ++i) {
1131 set->p[i] = isl_basic_set_eliminate_vars(set->p[i],
1132 nparam + first, n);
1133 if (!set->p[i])
1134 goto error;
1136 return set;
1137 error:
1138 isl_set_free(set);
1139 return NULL;
1142 /* Project out n dimensions starting at first using Fourier-Motzkin */
1143 struct isl_set *isl_set_remove_dims(struct isl_set *set,
1144 unsigned first, unsigned n)
1146 set = isl_set_eliminate_dims(set, first, n);
1147 set = isl_set_drop_dims(set, first, n);
1148 return set;
1151 struct isl_basic_set *isl_basic_set_remove_divs(struct isl_basic_set *bset)
1153 bset = isl_basic_set_eliminate_vars(bset, isl_dim_total(bset->dim),
1154 bset->n_div);
1155 if (!bset)
1156 return NULL;
1157 bset->n_div = 0;
1158 return bset;
1161 struct isl_set *isl_set_remove_divs(struct isl_set *set)
1163 int i;
1165 if (!set)
1166 return NULL;
1167 if (set->n == 0)
1168 return set;
1170 set = isl_set_cow(set);
1171 if (!set)
1172 return NULL;
1174 for (i = 0; i < set->n; ++i) {
1175 set->p[i] = isl_basic_set_remove_divs(set->p[i]);
1176 if (!set->p[i])
1177 goto error;
1179 return set;
1180 error:
1181 isl_set_free(set);
1182 return NULL;
1185 struct isl_basic_map *isl_basic_map_remove(struct isl_basic_map *bmap,
1186 enum isl_dim_type type, unsigned first, unsigned n)
1188 if (!bmap)
1189 return NULL;
1190 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1191 goto error);
1192 if (n == 0)
1193 return bmap;
1194 bmap = isl_basic_map_eliminate_vars(bmap,
1195 isl_basic_map_offset(bmap, type) - 1 + first, n);
1196 if (!bmap)
1197 return bmap;
1198 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
1199 return bmap;
1200 bmap = isl_basic_map_drop(bmap, type, first, n);
1201 return bmap;
1202 error:
1203 isl_basic_map_free(bmap);
1204 return NULL;
1207 struct isl_map *isl_map_remove(struct isl_map *map,
1208 enum isl_dim_type type, unsigned first, unsigned n)
1210 int i;
1211 unsigned nparam;
1213 if (n == 0)
1214 return map;
1216 map = isl_map_cow(map);
1217 if (!map)
1218 return NULL;
1219 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
1221 for (i = 0; i < map->n; ++i) {
1222 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
1223 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
1224 if (!map->p[i])
1225 goto error;
1227 map = isl_map_drop(map, type, first, n);
1228 return map;
1229 error:
1230 isl_map_free(map);
1231 return NULL;
1234 /* Project out n inputs starting at first using Fourier-Motzkin */
1235 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
1236 unsigned first, unsigned n)
1238 return isl_map_remove(map, isl_dim_in, first, n);
1241 /* Project out n dimensions starting at first using Fourier-Motzkin */
1242 struct isl_basic_set *isl_basic_set_remove_dims(struct isl_basic_set *bset,
1243 unsigned first, unsigned n)
1245 unsigned nparam = isl_basic_set_n_param(bset);
1246 bset = isl_basic_set_eliminate_vars(bset, nparam + first, n);
1247 bset = isl_basic_set_drop_dims(bset, first, n);
1248 return bset;
1251 static void dump_term(struct isl_basic_map *bmap,
1252 isl_int c, int pos, FILE *out)
1254 const char *name;
1255 unsigned in = isl_basic_map_n_in(bmap);
1256 unsigned dim = in + isl_basic_map_n_out(bmap);
1257 unsigned nparam = isl_basic_map_n_param(bmap);
1258 if (!pos)
1259 isl_int_print(out, c, 0);
1260 else {
1261 if (!isl_int_is_one(c))
1262 isl_int_print(out, c, 0);
1263 if (pos < 1 + nparam) {
1264 name = isl_dim_get_name(bmap->dim,
1265 isl_dim_param, pos - 1);
1266 if (name)
1267 fprintf(out, "%s", name);
1268 else
1269 fprintf(out, "p%d", pos - 1);
1270 } else if (pos < 1 + nparam + in)
1271 fprintf(out, "i%d", pos - 1 - nparam);
1272 else if (pos < 1 + nparam + dim)
1273 fprintf(out, "o%d", pos - 1 - nparam - in);
1274 else
1275 fprintf(out, "e%d", pos - 1 - nparam - dim);
1279 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
1280 int sign, FILE *out)
1282 int i;
1283 int first;
1284 unsigned len = 1 + isl_basic_map_total_dim(bmap);
1285 isl_int v;
1287 isl_int_init(v);
1288 for (i = 0, first = 1; i < len; ++i) {
1289 if (isl_int_sgn(c[i]) * sign <= 0)
1290 continue;
1291 if (!first)
1292 fprintf(out, " + ");
1293 first = 0;
1294 isl_int_abs(v, c[i]);
1295 dump_term(bmap, v, i, out);
1297 isl_int_clear(v);
1298 if (first)
1299 fprintf(out, "0");
1302 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
1303 const char *op, FILE *out, int indent)
1305 int i;
1307 fprintf(out, "%*s", indent, "");
1309 dump_constraint_sign(bmap, c, 1, out);
1310 fprintf(out, " %s ", op);
1311 dump_constraint_sign(bmap, c, -1, out);
1313 fprintf(out, "\n");
1315 for (i = bmap->n_div; i < bmap->extra; ++i) {
1316 if (isl_int_is_zero(c[1+isl_dim_total(bmap->dim)+i]))
1317 continue;
1318 fprintf(out, "%*s", indent, "");
1319 fprintf(out, "ERROR: unused div coefficient not zero\n");
1320 abort();
1324 static void dump_constraints(struct isl_basic_map *bmap,
1325 isl_int **c, unsigned n,
1326 const char *op, FILE *out, int indent)
1328 int i;
1330 for (i = 0; i < n; ++i)
1331 dump_constraint(bmap, c[i], op, out, indent);
1334 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
1336 int j;
1337 int first = 1;
1338 unsigned total = isl_basic_map_total_dim(bmap);
1340 for (j = 0; j < 1 + total; ++j) {
1341 if (isl_int_is_zero(exp[j]))
1342 continue;
1343 if (!first && isl_int_is_pos(exp[j]))
1344 fprintf(out, "+");
1345 dump_term(bmap, exp[j], j, out);
1346 first = 0;
1350 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
1352 int i;
1354 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
1355 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
1357 for (i = 0; i < bmap->n_div; ++i) {
1358 fprintf(out, "%*s", indent, "");
1359 fprintf(out, "e%d = [(", i);
1360 dump_affine(bmap, bmap->div[i]+1, out);
1361 fprintf(out, ")/");
1362 isl_int_print(out, bmap->div[i][0], 0);
1363 fprintf(out, "]\n");
1367 void isl_basic_set_dump(struct isl_basic_set *bset, FILE *out, int indent)
1369 if (!bset) {
1370 fprintf(out, "null basic set\n");
1371 return;
1374 fprintf(out, "%*s", indent, "");
1375 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
1376 bset->ref, bset->dim->nparam, bset->dim->n_out,
1377 bset->extra, bset->flags);
1378 dump((struct isl_basic_map *)bset, out, indent);
1381 void isl_basic_map_dump(struct isl_basic_map *bmap, FILE *out, int indent)
1383 if (!bmap) {
1384 fprintf(out, "null basic map\n");
1385 return;
1388 fprintf(out, "%*s", indent, "");
1389 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
1390 "flags: %x, n_name: %d\n",
1391 bmap->ref,
1392 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
1393 bmap->extra, bmap->flags, bmap->dim->n_name);
1394 dump(bmap, out, indent);
1397 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
1399 unsigned total;
1400 if (!bmap)
1401 return -1;
1402 total = isl_basic_map_total_dim(bmap);
1403 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1404 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
1405 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
1406 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1407 return 0;
1410 struct isl_set *isl_set_alloc_dim(struct isl_dim *dim, int n, unsigned flags)
1412 struct isl_set *set;
1414 if (!dim)
1415 return NULL;
1416 isl_assert(dim->ctx, dim->n_in == 0, return NULL);
1417 isl_assert(dim->ctx, n >= 0, return NULL);
1418 set = isl_alloc(dim->ctx, struct isl_set,
1419 sizeof(struct isl_set) +
1420 (n - 1) * sizeof(struct isl_basic_set *));
1421 if (!set)
1422 goto error;
1424 set->ctx = dim->ctx;
1425 isl_ctx_ref(set->ctx);
1426 set->ref = 1;
1427 set->size = n;
1428 set->n = 0;
1429 set->dim = dim;
1430 set->flags = flags;
1431 return set;
1432 error:
1433 isl_dim_free(dim);
1434 return NULL;
1437 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
1438 unsigned nparam, unsigned dim, int n, unsigned flags)
1440 struct isl_set *set;
1441 struct isl_dim *dims;
1443 dims = isl_dim_alloc(ctx, nparam, 0, dim);
1444 if (!dims)
1445 return NULL;
1447 set = isl_set_alloc_dim(dims, n, flags);
1448 return set;
1451 /* Make sure "map" has room for at least "n" more basic maps.
1453 struct isl_map *isl_map_grow(struct isl_map *map, int n)
1455 int i;
1456 struct isl_map *grown = NULL;
1458 if (!map)
1459 return NULL;
1460 isl_assert(map->ctx, n >= 0, goto error);
1461 if (map->n + n <= map->size)
1462 return map;
1463 grown = isl_map_alloc_dim(isl_map_get_dim(map), map->n + n, map->flags);
1464 if (!grown)
1465 goto error;
1466 for (i = 0; i < map->n; ++i) {
1467 grown->p[i] = isl_basic_map_copy(map->p[i]);
1468 if (!grown->p[i])
1469 goto error;
1470 grown->n++;
1472 isl_map_free(map);
1473 return grown;
1474 error:
1475 isl_map_free(grown);
1476 isl_map_free(map);
1477 return NULL;
1480 /* Make sure "set" has room for at least "n" more basic sets.
1482 struct isl_set *isl_set_grow(struct isl_set *set, int n)
1484 return (struct isl_set *)isl_map_grow((struct isl_map *)set, n);
1487 struct isl_set *isl_set_dup(struct isl_set *set)
1489 int i;
1490 struct isl_set *dup;
1492 if (!set)
1493 return NULL;
1495 dup = isl_set_alloc_dim(isl_dim_copy(set->dim), set->n, set->flags);
1496 if (!dup)
1497 return NULL;
1498 for (i = 0; i < set->n; ++i)
1499 dup = isl_set_add(dup, isl_basic_set_copy(set->p[i]));
1500 return dup;
1503 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
1505 struct isl_set *set;
1507 if (!bset)
1508 return NULL;
1510 set = isl_set_alloc_dim(isl_dim_copy(bset->dim), 1, ISL_MAP_DISJOINT);
1511 if (!set) {
1512 isl_basic_set_free(bset);
1513 return NULL;
1515 return isl_set_add(set, bset);
1518 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
1520 struct isl_map *map;
1522 if (!bmap)
1523 return NULL;
1525 map = isl_map_alloc_dim(isl_dim_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
1526 if (!map) {
1527 isl_basic_map_free(bmap);
1528 return NULL;
1530 return isl_map_add(map, bmap);
1533 struct isl_set *isl_set_add(struct isl_set *set, struct isl_basic_set *bset)
1535 return (struct isl_set *)isl_map_add((struct isl_map *)set,
1536 (struct isl_basic_map *)bset);
1539 void isl_set_free(struct isl_set *set)
1541 int i;
1543 if (!set)
1544 return;
1546 if (--set->ref > 0)
1547 return;
1549 isl_ctx_deref(set->ctx);
1550 for (i = 0; i < set->n; ++i)
1551 isl_basic_set_free(set->p[i]);
1552 isl_dim_free(set->dim);
1553 free(set);
1556 void isl_set_dump(struct isl_set *set, FILE *out, int indent)
1558 int i;
1560 if (!set) {
1561 fprintf(out, "null set\n");
1562 return;
1565 fprintf(out, "%*s", indent, "");
1566 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
1567 set->ref, set->n, set->dim->nparam, set->dim->n_out,
1568 set->flags);
1569 for (i = 0; i < set->n; ++i) {
1570 fprintf(out, "%*s", indent, "");
1571 fprintf(out, "basic set %d:\n", i);
1572 isl_basic_set_dump(set->p[i], out, indent+4);
1576 void isl_map_dump(struct isl_map *map, FILE *out, int indent)
1578 int i;
1580 if (!map) {
1581 fprintf(out, "null map\n");
1582 return;
1585 fprintf(out, "%*s", indent, "");
1586 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
1587 "flags: %x, n_name: %d\n",
1588 map->ref, map->n, map->dim->nparam, map->dim->n_in,
1589 map->dim->n_out, map->flags, map->dim->n_name);
1590 for (i = 0; i < map->n; ++i) {
1591 fprintf(out, "%*s", indent, "");
1592 fprintf(out, "basic map %d:\n", i);
1593 isl_basic_map_dump(map->p[i], out, indent+4);
1597 struct isl_basic_map *isl_basic_map_intersect_domain(
1598 struct isl_basic_map *bmap, struct isl_basic_set *bset)
1600 struct isl_basic_map *bmap_domain;
1601 struct isl_dim *dim;
1603 if (!bmap || !bset)
1604 goto error;
1606 isl_assert(bset->ctx, isl_dim_match(bmap->dim, isl_dim_param,
1607 bset->dim, isl_dim_param), goto error);
1609 if (isl_dim_size(bset->dim, isl_dim_set) != 0)
1610 isl_assert(bset->ctx,
1611 isl_basic_map_compatible_domain(bmap, bset), goto error);
1613 bmap = isl_basic_map_cow(bmap);
1614 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
1615 bset->n_div, bset->n_eq, bset->n_ineq);
1616 if (!bmap)
1617 goto error;
1618 dim = isl_dim_reverse(isl_dim_copy(bset->dim));
1619 bmap_domain = isl_basic_map_from_basic_set(bset, dim);
1620 bmap = add_constraints(bmap, bmap_domain, 0, 0);
1622 bmap = isl_basic_map_simplify(bmap);
1623 return isl_basic_map_finalize(bmap);
1624 error:
1625 isl_basic_map_free(bmap);
1626 isl_basic_set_free(bset);
1627 return NULL;
1630 struct isl_basic_map *isl_basic_map_intersect_range(
1631 struct isl_basic_map *bmap, struct isl_basic_set *bset)
1633 struct isl_basic_map *bmap_range;
1635 if (!bmap || !bset)
1636 goto error;
1638 isl_assert(bset->ctx, isl_dim_match(bmap->dim, isl_dim_param,
1639 bset->dim, isl_dim_param), goto error);
1641 if (isl_dim_size(bset->dim, isl_dim_set) != 0)
1642 isl_assert(bset->ctx,
1643 isl_basic_map_compatible_range(bmap, bset), goto error);
1645 bmap = isl_basic_map_cow(bmap);
1646 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
1647 bset->n_div, bset->n_eq, bset->n_ineq);
1648 if (!bmap)
1649 goto error;
1650 bmap_range = isl_basic_map_from_basic_set(bset, isl_dim_copy(bset->dim));
1651 bmap = add_constraints(bmap, bmap_range, 0, 0);
1653 bmap = isl_basic_map_simplify(bmap);
1654 return isl_basic_map_finalize(bmap);
1655 error:
1656 isl_basic_map_free(bmap);
1657 isl_basic_set_free(bset);
1658 return NULL;
1661 static int basic_map_contains(struct isl_basic_map *bmap, struct isl_vec *vec)
1663 int i;
1664 unsigned total;
1665 isl_int s;
1667 total = 1 + isl_basic_map_total_dim(bmap);
1668 if (total != vec->size)
1669 return -1;
1671 isl_int_init(s);
1673 for (i = 0; i < bmap->n_eq; ++i) {
1674 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
1675 if (!isl_int_is_zero(s)) {
1676 isl_int_clear(s);
1677 return 0;
1681 for (i = 0; i < bmap->n_ineq; ++i) {
1682 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
1683 if (isl_int_is_neg(s)) {
1684 isl_int_clear(s);
1685 return 0;
1689 isl_int_clear(s);
1691 return 1;
1694 int isl_basic_set_contains(struct isl_basic_set *bset, struct isl_vec *vec)
1696 return basic_map_contains((struct isl_basic_map *)bset, vec);
1699 struct isl_basic_map *isl_basic_map_intersect(
1700 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
1702 struct isl_vec *sample = NULL;
1704 if (!bmap1 || !bmap2)
1705 goto error;
1707 isl_assert(bmap1->ctx, isl_dim_match(bmap1->dim, isl_dim_param,
1708 bmap2->dim, isl_dim_param), goto error);
1709 if (isl_dim_total(bmap1->dim) ==
1710 isl_dim_size(bmap1->dim, isl_dim_param) &&
1711 isl_dim_total(bmap2->dim) !=
1712 isl_dim_size(bmap2->dim, isl_dim_param))
1713 return isl_basic_map_intersect(bmap2, bmap1);
1715 if (isl_dim_total(bmap2->dim) !=
1716 isl_dim_size(bmap2->dim, isl_dim_param))
1717 isl_assert(bmap1->ctx,
1718 isl_dim_equal(bmap1->dim, bmap2->dim), goto error);
1720 if (bmap1->sample &&
1721 basic_map_contains(bmap1, bmap1->sample) > 0 &&
1722 basic_map_contains(bmap2, bmap1->sample) > 0)
1723 sample = isl_vec_copy(bmap1->sample);
1724 else if (bmap2->sample &&
1725 basic_map_contains(bmap1, bmap2->sample) > 0 &&
1726 basic_map_contains(bmap2, bmap2->sample) > 0)
1727 sample = isl_vec_copy(bmap2->sample);
1729 bmap1 = isl_basic_map_cow(bmap1);
1730 bmap1 = isl_basic_map_extend_dim(bmap1, isl_dim_copy(bmap1->dim),
1731 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
1732 if (!bmap1)
1733 goto error;
1734 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
1736 if (sample) {
1737 isl_vec_free(bmap1->sample);
1738 bmap1->sample = sample;
1741 bmap1 = isl_basic_map_simplify(bmap1);
1742 return isl_basic_map_finalize(bmap1);
1743 error:
1744 if (sample)
1745 isl_vec_free(sample);
1746 isl_basic_map_free(bmap1);
1747 isl_basic_map_free(bmap2);
1748 return NULL;
1751 struct isl_basic_set *isl_basic_set_intersect(
1752 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
1754 return (struct isl_basic_set *)
1755 isl_basic_map_intersect(
1756 (struct isl_basic_map *)bset1,
1757 (struct isl_basic_map *)bset2);
1760 struct isl_map *isl_map_intersect(struct isl_map *map1, struct isl_map *map2)
1762 unsigned flags = 0;
1763 struct isl_map *result;
1764 int i, j;
1766 if (!map1 || !map2)
1767 goto error;
1769 isl_assert(map1->ctx, isl_dim_match(map1->dim, isl_dim_param,
1770 map2->dim, isl_dim_param), goto error);
1771 if (isl_dim_total(map1->dim) ==
1772 isl_dim_size(map1->dim, isl_dim_param) &&
1773 isl_dim_total(map2->dim) != isl_dim_size(map2->dim, isl_dim_param))
1774 return isl_map_intersect(map2, map1);
1776 if (isl_dim_total(map2->dim) != isl_dim_size(map2->dim, isl_dim_param))
1777 isl_assert(map1->ctx,
1778 isl_dim_equal(map1->dim, map2->dim), goto error);
1780 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
1781 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
1782 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
1784 result = isl_map_alloc_dim(isl_dim_copy(map1->dim),
1785 map1->n * map2->n, flags);
1786 if (!result)
1787 goto error;
1788 for (i = 0; i < map1->n; ++i)
1789 for (j = 0; j < map2->n; ++j) {
1790 struct isl_basic_map *part;
1791 part = isl_basic_map_intersect(
1792 isl_basic_map_copy(map1->p[i]),
1793 isl_basic_map_copy(map2->p[j]));
1794 if (isl_basic_map_is_empty(part))
1795 isl_basic_map_free(part);
1796 else
1797 result = isl_map_add(result, part);
1798 if (!result)
1799 goto error;
1801 isl_map_free(map1);
1802 isl_map_free(map2);
1803 return result;
1804 error:
1805 isl_map_free(map1);
1806 isl_map_free(map2);
1807 return NULL;
1810 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
1812 return (struct isl_set *)
1813 isl_map_intersect((struct isl_map *)set1,
1814 (struct isl_map *)set2);
1817 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
1819 struct isl_dim *dim;
1820 struct isl_basic_set *bset;
1821 unsigned in;
1823 if (!bmap)
1824 return NULL;
1825 bmap = isl_basic_map_cow(bmap);
1826 if (!bmap)
1827 return NULL;
1828 dim = isl_dim_reverse(isl_dim_copy(bmap->dim));
1829 in = isl_basic_map_n_in(bmap);
1830 bset = isl_basic_set_from_basic_map(bmap);
1831 bset = isl_basic_set_swap_vars(bset, in);
1832 return isl_basic_map_from_basic_set(bset, dim);
1835 /* Turn final n dimensions into existentially quantified variables.
1837 struct isl_basic_set *isl_basic_set_project_out(
1838 struct isl_basic_set *bset, unsigned n, unsigned flags)
1840 int i;
1841 size_t row_size;
1842 isl_int **new_div;
1843 isl_int *old;
1845 if (!bset)
1846 return NULL;
1848 isl_assert(bset->ctx, n <= isl_basic_set_n_dim(bset), goto error);
1850 if (n == 0)
1851 return bset;
1853 bset = isl_basic_set_cow(bset);
1855 row_size = 1 + isl_dim_total(bset->dim) + bset->extra;
1856 old = bset->block2.data;
1857 bset->block2 = isl_blk_extend(bset->ctx, bset->block2,
1858 (bset->extra + n) * (1 + row_size));
1859 if (!bset->block2.data)
1860 goto error;
1861 new_div = isl_alloc_array(ctx, isl_int *, bset->extra + n);
1862 if (!new_div)
1863 goto error;
1864 for (i = 0; i < n; ++i) {
1865 new_div[i] = bset->block2.data +
1866 (bset->extra + i) * (1 + row_size);
1867 isl_seq_clr(new_div[i], 1 + row_size);
1869 for (i = 0; i < bset->extra; ++i)
1870 new_div[n + i] = bset->block2.data + (bset->div[i] - old);
1871 free(bset->div);
1872 bset->div = new_div;
1873 bset->n_div += n;
1874 bset->extra += n;
1875 bset->dim = isl_dim_drop_outputs(bset->dim,
1876 isl_basic_set_n_dim(bset) - n, n);
1877 if (!bset->dim)
1878 goto error;
1879 bset = isl_basic_set_simplify(bset);
1880 bset = isl_basic_set_drop_redundant_divs(bset);
1881 return isl_basic_set_finalize(bset);
1882 error:
1883 isl_basic_set_free(bset);
1884 return NULL;
1887 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
1889 int i, j;
1891 for (i = 0; i < n; ++i) {
1892 j = isl_basic_map_alloc_div(bmap);
1893 if (j < 0)
1894 goto error;
1895 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
1897 return bmap;
1898 error:
1899 isl_basic_map_free(bmap);
1900 return NULL;
1903 struct isl_basic_map *isl_basic_map_apply_range(
1904 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
1906 struct isl_dim *dim_result = NULL;
1907 struct isl_basic_map *bmap;
1908 unsigned n_in, n_out, n, nparam, total, pos;
1909 struct isl_dim_map *dim_map1, *dim_map2;
1911 if (!bmap1 || !bmap2)
1912 goto error;
1914 dim_result = isl_dim_join(isl_dim_copy(bmap1->dim),
1915 isl_dim_copy(bmap2->dim));
1917 n_in = isl_basic_map_n_in(bmap1);
1918 n_out = isl_basic_map_n_out(bmap2);
1919 n = isl_basic_map_n_out(bmap1);
1920 nparam = isl_basic_map_n_param(bmap1);
1922 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
1923 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
1924 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
1925 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
1926 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
1927 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
1928 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
1929 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
1930 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
1931 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
1932 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
1934 bmap = isl_basic_map_alloc_dim(dim_result,
1935 bmap1->n_div + bmap2->n_div + n,
1936 bmap1->n_eq + bmap2->n_eq,
1937 bmap1->n_ineq + bmap2->n_ineq);
1938 bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
1939 bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
1940 bmap = add_divs(bmap, n);
1941 bmap = isl_basic_map_simplify(bmap);
1942 bmap = isl_basic_map_drop_redundant_divs(bmap);
1943 return isl_basic_map_finalize(bmap);
1944 error:
1945 isl_basic_map_free(bmap1);
1946 isl_basic_map_free(bmap2);
1947 return NULL;
1950 struct isl_basic_set *isl_basic_set_apply(
1951 struct isl_basic_set *bset, struct isl_basic_map *bmap)
1953 if (!bset || !bmap)
1954 goto error;
1956 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
1957 goto error);
1959 return (struct isl_basic_set *)
1960 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
1961 error:
1962 isl_basic_set_free(bset);
1963 isl_basic_map_free(bmap);
1964 return NULL;
1967 struct isl_basic_map *isl_basic_map_apply_domain(
1968 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
1970 if (!bmap1 || !bmap2)
1971 goto error;
1973 isl_assert(bmap1->ctx,
1974 isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
1975 isl_assert(bmap1->ctx,
1976 isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
1977 goto error);
1979 bmap1 = isl_basic_map_reverse(bmap1);
1980 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
1981 return isl_basic_map_reverse(bmap1);
1982 error:
1983 isl_basic_map_free(bmap1);
1984 isl_basic_map_free(bmap2);
1985 return NULL;
1988 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
1989 * A \cap B -> f(A) + f(B)
1991 struct isl_basic_map *isl_basic_map_sum(
1992 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
1994 unsigned n_in, n_out, nparam, total, pos;
1995 struct isl_basic_map *bmap = NULL;
1996 struct isl_dim_map *dim_map1, *dim_map2;
1997 int i;
1999 if (!bmap1 || !bmap2)
2000 goto error;
2002 isl_assert(bmap1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim),
2003 goto error);
2005 nparam = isl_basic_map_n_param(bmap1);
2006 n_in = isl_basic_map_n_in(bmap1);
2007 n_out = isl_basic_map_n_out(bmap1);
2009 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
2010 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
2011 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
2012 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
2013 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
2014 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
2015 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
2016 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
2017 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
2018 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
2019 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
2021 bmap = isl_basic_map_alloc_dim(isl_dim_copy(bmap1->dim),
2022 bmap1->n_div + bmap2->n_div + 2 * n_out,
2023 bmap1->n_eq + bmap2->n_eq + n_out,
2024 bmap1->n_ineq + bmap2->n_ineq);
2025 for (i = 0; i < n_out; ++i) {
2026 int j = isl_basic_map_alloc_equality(bmap);
2027 if (j < 0)
2028 goto error;
2029 isl_seq_clr(bmap->eq[j], 1+total);
2030 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
2031 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
2032 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
2034 bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
2035 bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
2036 bmap = add_divs(bmap, 2 * n_out);
2038 bmap = isl_basic_map_simplify(bmap);
2039 return isl_basic_map_finalize(bmap);
2040 error:
2041 isl_basic_map_free(bmap);
2042 isl_basic_map_free(bmap1);
2043 isl_basic_map_free(bmap2);
2044 return NULL;
2047 /* Given two maps A -> f(A) and B -> g(B), construct a map
2048 * A \cap B -> f(A) + f(B)
2050 struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
2052 struct isl_map *result;
2053 int i, j;
2055 if (!map1 || !map2)
2056 goto error;
2058 isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
2060 result = isl_map_alloc_dim(isl_dim_copy(map1->dim),
2061 map1->n * map2->n, 0);
2062 if (!result)
2063 goto error;
2064 for (i = 0; i < map1->n; ++i)
2065 for (j = 0; j < map2->n; ++j) {
2066 struct isl_basic_map *part;
2067 part = isl_basic_map_sum(
2068 isl_basic_map_copy(map1->p[i]),
2069 isl_basic_map_copy(map2->p[j]));
2070 if (isl_basic_map_is_empty(part))
2071 isl_basic_map_free(part);
2072 else
2073 result = isl_map_add(result, part);
2074 if (!result)
2075 goto error;
2077 isl_map_free(map1);
2078 isl_map_free(map2);
2079 return result;
2080 error:
2081 isl_map_free(map1);
2082 isl_map_free(map2);
2083 return NULL;
2086 /* Given a basic map A -> f(A), construct A -> -f(A).
2088 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
2090 int i, j;
2091 unsigned off, n;
2093 bmap = isl_basic_map_cow(bmap);
2094 if (!bmap)
2095 return NULL;
2097 n = isl_basic_map_dim(bmap, isl_dim_out);
2098 off = isl_basic_map_offset(bmap, isl_dim_out);
2099 for (i = 0; i < bmap->n_eq; ++i)
2100 for (j = 0; j < n; ++j)
2101 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
2102 for (i = 0; i < bmap->n_ineq; ++i)
2103 for (j = 0; j < n; ++j)
2104 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
2105 for (i = 0; i < bmap->n_div; ++i)
2106 for (j = 0; j < n; ++j)
2107 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
2108 return isl_basic_map_finalize(bmap);
2111 /* Given a map A -> f(A), construct A -> -f(A).
2113 struct isl_map *isl_map_neg(struct isl_map *map)
2115 int i;
2117 map = isl_map_cow(map);
2118 if (!map)
2119 return NULL;
2121 for (i = 0; i < map->n; ++i) {
2122 map->p[i] = isl_basic_map_neg(map->p[i]);
2123 if (!map->p[i])
2124 goto error;
2127 return map;
2128 error:
2129 isl_map_free(map);
2130 return NULL;
2133 /* Given a basic map A -> f(A) and an integer d, construct a basic map
2134 * A -> floor(f(A)/d).
2136 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
2137 isl_int d)
2139 unsigned n_in, n_out, nparam, total, pos;
2140 struct isl_basic_map *result = NULL;
2141 struct isl_dim_map *dim_map;
2142 int i;
2144 if (!bmap)
2145 return NULL;
2147 nparam = isl_basic_map_n_param(bmap);
2148 n_in = isl_basic_map_n_in(bmap);
2149 n_out = isl_basic_map_n_out(bmap);
2151 total = nparam + n_in + n_out + bmap->n_div + n_out;
2152 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2153 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
2154 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
2155 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
2156 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
2158 result = isl_basic_map_alloc_dim(isl_dim_copy(bmap->dim),
2159 bmap->n_div + n_out,
2160 bmap->n_eq, bmap->n_ineq + 2 * n_out);
2161 result = add_constraints_dim_map(result, bmap, dim_map);
2162 result = add_divs(result, n_out);
2163 for (i = 0; i < n_out; ++i) {
2164 int j;
2165 j = isl_basic_map_alloc_inequality(result);
2166 if (j < 0)
2167 goto error;
2168 isl_seq_clr(result->ineq[j], 1+total);
2169 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
2170 isl_int_set_si(result->ineq[j][1+pos+i], 1);
2171 j = isl_basic_map_alloc_inequality(result);
2172 if (j < 0)
2173 goto error;
2174 isl_seq_clr(result->ineq[j], 1+total);
2175 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
2176 isl_int_set_si(result->ineq[j][1+pos+i], -1);
2177 isl_int_sub_ui(result->ineq[j][0], d, 1);
2180 result = isl_basic_map_simplify(result);
2181 return isl_basic_map_finalize(result);
2182 error:
2183 isl_basic_map_free(result);
2184 return NULL;
2187 /* Given a map A -> f(A) and an integer d, construct a map
2188 * A -> floor(f(A)/d).
2190 struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
2192 int i;
2194 map = isl_map_cow(map);
2195 if (!map)
2196 return NULL;
2198 ISL_F_CLR(map, ISL_MAP_DISJOINT);
2199 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
2200 for (i = 0; i < map->n; ++i) {
2201 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
2202 if (!map->p[i])
2203 goto error;
2206 return map;
2207 error:
2208 isl_map_free(map);
2209 return NULL;
2212 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
2214 int i;
2215 unsigned nparam;
2216 unsigned n_in;
2218 i = isl_basic_map_alloc_equality(bmap);
2219 if (i < 0)
2220 goto error;
2221 nparam = isl_basic_map_n_param(bmap);
2222 n_in = isl_basic_map_n_in(bmap);
2223 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
2224 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
2225 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
2226 return isl_basic_map_finalize(bmap);
2227 error:
2228 isl_basic_map_free(bmap);
2229 return NULL;
2232 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
2234 int i;
2235 unsigned nparam;
2236 unsigned n_in;
2238 i = isl_basic_map_alloc_inequality(bmap);
2239 if (i < 0)
2240 goto error;
2241 nparam = isl_basic_map_n_param(bmap);
2242 n_in = isl_basic_map_n_in(bmap);
2243 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
2244 isl_int_set_si(bmap->ineq[i][0], -1);
2245 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
2246 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
2247 return isl_basic_map_finalize(bmap);
2248 error:
2249 isl_basic_map_free(bmap);
2250 return NULL;
2253 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
2255 int i;
2256 unsigned nparam;
2257 unsigned n_in;
2259 i = isl_basic_map_alloc_inequality(bmap);
2260 if (i < 0)
2261 goto error;
2262 nparam = isl_basic_map_n_param(bmap);
2263 n_in = isl_basic_map_n_in(bmap);
2264 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
2265 isl_int_set_si(bmap->ineq[i][0], -1);
2266 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
2267 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
2268 return isl_basic_map_finalize(bmap);
2269 error:
2270 isl_basic_map_free(bmap);
2271 return NULL;
2274 struct isl_basic_map *isl_basic_map_equal(struct isl_dim *dim, unsigned n_equal)
2276 int i;
2277 struct isl_basic_map *bmap;
2278 bmap = isl_basic_map_alloc_dim(dim, 0, n_equal, 0);
2279 if (!bmap)
2280 return NULL;
2281 for (i = 0; i < n_equal && bmap; ++i)
2282 bmap = var_equal(bmap, i);
2283 return isl_basic_map_finalize(bmap);
2286 struct isl_basic_map *isl_basic_map_less_at(struct isl_dim *dim, unsigned pos)
2288 int i;
2289 struct isl_basic_map *bmap;
2290 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
2291 if (!bmap)
2292 return NULL;
2293 for (i = 0; i < pos && bmap; ++i)
2294 bmap = var_equal(bmap, i);
2295 if (bmap)
2296 bmap = var_less(bmap, pos);
2297 return isl_basic_map_finalize(bmap);
2300 struct isl_basic_map *isl_basic_map_more_at(struct isl_dim *dim, unsigned pos)
2302 int i;
2303 struct isl_basic_map *bmap;
2304 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
2305 if (!bmap)
2306 return NULL;
2307 for (i = 0; i < pos && bmap; ++i)
2308 bmap = var_equal(bmap, i);
2309 if (bmap)
2310 bmap = var_more(bmap, pos);
2311 return isl_basic_map_finalize(bmap);
2314 struct isl_basic_map *isl_basic_map_from_basic_set(
2315 struct isl_basic_set *bset, struct isl_dim *dim)
2317 struct isl_basic_map *bmap;
2319 bset = isl_basic_set_cow(bset);
2320 if (!bset || !dim)
2321 goto error;
2323 isl_assert(bset->ctx, isl_dim_compatible(bset->dim, dim), goto error);
2324 isl_dim_free(bset->dim);
2325 bmap = (struct isl_basic_map *) bset;
2326 bmap->dim = dim;
2327 return isl_basic_map_finalize(bmap);
2328 error:
2329 isl_basic_set_free(bset);
2330 isl_dim_free(dim);
2331 return NULL;
2334 struct isl_basic_set *isl_basic_set_from_basic_map(struct isl_basic_map *bmap)
2336 if (!bmap)
2337 goto error;
2338 if (bmap->dim->n_in == 0)
2339 return (struct isl_basic_set *)bmap;
2340 bmap = isl_basic_map_cow(bmap);
2341 if (!bmap)
2342 goto error;
2343 bmap->dim = isl_dim_cow(bmap->dim);
2344 if (!bmap->dim)
2345 goto error;
2346 bmap->dim->n_out += bmap->dim->n_in;
2347 bmap->dim->n_in = 0;
2348 bmap = isl_basic_map_finalize(bmap);
2349 return (struct isl_basic_set *)bmap;
2350 error:
2351 isl_basic_map_free(bmap);
2352 return NULL;
2355 /* For a div d = floor(f/m), add the constraints
2357 * f - m d >= 0
2358 * -(f-(n-1)) + m d >= 0
2360 * Note that the second constraint is the negation of
2362 * f - m d >= n
2364 static int add_div_constraints(struct isl_basic_map *bmap, unsigned div)
2366 int i, j;
2367 unsigned total = isl_basic_map_total_dim(bmap);
2368 unsigned div_pos = 1 + total - bmap->n_div + div;
2370 i = isl_basic_map_alloc_inequality(bmap);
2371 if (i < 0)
2372 return -1;
2373 isl_seq_cpy(bmap->ineq[i], bmap->div[div]+1, 1+total);
2374 isl_int_neg(bmap->ineq[i][div_pos], bmap->div[div][0]);
2376 j = isl_basic_map_alloc_inequality(bmap);
2377 if (j < 0)
2378 return -1;
2379 isl_seq_neg(bmap->ineq[j], bmap->ineq[i], 1 + total);
2380 isl_int_add(bmap->ineq[j][0], bmap->ineq[j][0], bmap->ineq[j][div_pos]);
2381 isl_int_sub_ui(bmap->ineq[j][0], bmap->ineq[j][0], 1);
2382 return j;
2385 struct isl_basic_set *isl_basic_map_underlying_set(
2386 struct isl_basic_map *bmap)
2388 if (!bmap)
2389 goto error;
2390 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 && bmap->n_div == 0)
2391 return (struct isl_basic_set *)bmap;
2392 bmap = isl_basic_map_cow(bmap);
2393 if (!bmap)
2394 goto error;
2395 bmap->dim = isl_dim_underlying(bmap->dim, bmap->n_div);
2396 if (!bmap->dim)
2397 goto error;
2398 bmap->extra -= bmap->n_div;
2399 bmap->n_div = 0;
2400 bmap = isl_basic_map_finalize(bmap);
2401 return (struct isl_basic_set *)bmap;
2402 error:
2403 return NULL;
2406 struct isl_basic_map *isl_basic_map_overlying_set(
2407 struct isl_basic_set *bset, struct isl_basic_map *like)
2409 struct isl_basic_map *bmap;
2410 struct isl_ctx *ctx;
2411 unsigned total;
2412 int i;
2414 if (!bset || !like)
2415 goto error;
2416 ctx = bset->ctx;
2417 isl_assert(ctx, bset->n_div == 0, goto error);
2418 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
2419 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
2420 goto error);
2421 if (isl_dim_equal(bset->dim, like->dim) && like->n_div == 0) {
2422 isl_basic_map_free(like);
2423 return (struct isl_basic_map *)bset;
2425 bset = isl_basic_set_cow(bset);
2426 if (!bset)
2427 goto error;
2428 total = bset->dim->n_out + bset->extra;
2429 bmap = (struct isl_basic_map *)bset;
2430 isl_dim_free(bmap->dim);
2431 bmap->dim = isl_dim_copy(like->dim);
2432 if (!bmap->dim)
2433 goto error;
2434 bmap->n_div = like->n_div;
2435 bmap->extra += like->n_div;
2436 if (bmap->extra) {
2437 unsigned ltotal;
2438 ltotal = total - bmap->extra + like->extra;
2439 if (ltotal > total)
2440 ltotal = total;
2441 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
2442 bmap->extra * (1 + 1 + total));
2443 if (isl_blk_is_error(bmap->block2))
2444 goto error;
2445 bmap->div = isl_realloc_array(ctx, bmap->div, isl_int *,
2446 bmap->extra);
2447 if (!bmap->div)
2448 goto error;
2449 for (i = 0; i < bmap->extra; ++i)
2450 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
2451 for (i = 0; i < like->n_div; ++i) {
2452 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
2453 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
2455 bmap = isl_basic_map_extend_constraints(bmap,
2456 0, 2 * like->n_div);
2457 for (i = 0; i < like->n_div; ++i) {
2458 if (isl_int_is_zero(bmap->div[i][0]))
2459 continue;
2460 if (add_div_constraints(bmap, i) < 0)
2461 goto error;
2464 isl_basic_map_free(like);
2465 bmap = isl_basic_map_simplify(bmap);
2466 bmap = isl_basic_map_finalize(bmap);
2467 return bmap;
2468 error:
2469 isl_basic_map_free(like);
2470 isl_basic_set_free(bset);
2471 return NULL;
2474 struct isl_basic_set *isl_basic_set_from_underlying_set(
2475 struct isl_basic_set *bset, struct isl_basic_set *like)
2477 return (struct isl_basic_set *)
2478 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
2481 struct isl_set *isl_set_from_underlying_set(
2482 struct isl_set *set, struct isl_basic_set *like)
2484 int i;
2486 if (!set || !like)
2487 goto error;
2488 isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
2489 goto error);
2490 if (isl_dim_equal(set->dim, like->dim) && like->n_div == 0) {
2491 isl_basic_set_free(like);
2492 return set;
2494 set = isl_set_cow(set);
2495 if (!set)
2496 goto error;
2497 for (i = 0; i < set->n; ++i) {
2498 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
2499 isl_basic_set_copy(like));
2500 if (!set->p[i])
2501 goto error;
2503 isl_dim_free(set->dim);
2504 set->dim = isl_dim_copy(like->dim);
2505 if (!set->dim)
2506 goto error;
2507 isl_basic_set_free(like);
2508 return set;
2509 error:
2510 isl_basic_set_free(like);
2511 isl_set_free(set);
2512 return NULL;
2515 struct isl_set *isl_map_underlying_set(struct isl_map *map)
2517 int i;
2519 map = isl_map_cow(map);
2520 if (!map)
2521 return NULL;
2522 map->dim = isl_dim_cow(map->dim);
2523 if (!map->dim)
2524 goto error;
2526 for (i = 1; i < map->n; ++i)
2527 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
2528 goto error);
2529 for (i = 0; i < map->n; ++i) {
2530 map->p[i] = (struct isl_basic_map *)
2531 isl_basic_map_underlying_set(map->p[i]);
2532 if (!map->p[i])
2533 goto error;
2535 if (map->n == 0)
2536 map->dim = isl_dim_underlying(map->dim, 0);
2537 else {
2538 isl_dim_free(map->dim);
2539 map->dim = isl_dim_copy(map->p[0]->dim);
2541 if (!map->dim)
2542 goto error;
2543 return (struct isl_set *)map;
2544 error:
2545 isl_map_free(map);
2546 return NULL;
2549 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
2551 return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
2554 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
2556 struct isl_basic_set *domain;
2557 unsigned n_out;
2558 if (!bmap)
2559 return NULL;
2560 n_out = isl_basic_map_n_out(bmap);
2561 domain = isl_basic_set_from_basic_map(bmap);
2562 return isl_basic_set_project_out(domain, n_out, 0);
2565 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
2567 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
2570 struct isl_set *isl_map_range(struct isl_map *map)
2572 int i;
2573 struct isl_set *set;
2575 if (!map)
2576 goto error;
2577 map = isl_map_cow(map);
2578 if (!map)
2579 goto error;
2581 set = (struct isl_set *) map;
2582 if (set->dim->n_in != 0) {
2583 set->dim = isl_dim_drop_inputs(set->dim, 0, set->dim->n_in);
2584 if (!set->dim)
2585 goto error;
2587 for (i = 0; i < map->n; ++i) {
2588 set->p[i] = isl_basic_map_range(map->p[i]);
2589 if (!set->p[i])
2590 goto error;
2592 ISL_F_CLR(set, ISL_MAP_DISJOINT);
2593 ISL_F_CLR(set, ISL_SET_NORMALIZED);
2594 return set;
2595 error:
2596 isl_map_free(map);
2597 return NULL;
2600 struct isl_map *isl_map_from_set(struct isl_set *set, struct isl_dim *dim)
2602 int i;
2603 struct isl_map *map = NULL;
2605 set = isl_set_cow(set);
2606 if (!set || !dim)
2607 goto error;
2608 isl_assert(set->ctx, isl_dim_compatible(set->dim, dim), goto error);
2609 map = (struct isl_map *)set;
2610 for (i = 0; i < set->n; ++i) {
2611 map->p[i] = isl_basic_map_from_basic_set(
2612 set->p[i], isl_dim_copy(dim));
2613 if (!map->p[i])
2614 goto error;
2616 isl_dim_free(map->dim);
2617 map->dim = dim;
2618 return map;
2619 error:
2620 isl_dim_free(dim);
2621 isl_set_free(set);
2622 return NULL;
2625 struct isl_map *isl_map_from_range(struct isl_set *set)
2627 return (struct isl_map *)set;
2630 struct isl_set *isl_set_from_map(struct isl_map *map)
2632 int i;
2633 struct isl_set *set = NULL;
2635 if (!map)
2636 return NULL;
2637 map = isl_map_cow(map);
2638 if (!map)
2639 return NULL;
2640 map->dim = isl_dim_cow(map->dim);
2641 if (!map->dim)
2642 goto error;
2643 map->dim->n_out += map->dim->n_in;
2644 map->dim->n_in = 0;
2645 set = (struct isl_set *)map;
2646 for (i = 0; i < map->n; ++i) {
2647 set->p[i] = isl_basic_set_from_basic_map(map->p[i]);
2648 if (!set->p[i])
2649 goto error;
2651 return set;
2652 error:
2653 isl_map_free(map);
2654 return NULL;
2657 struct isl_map *isl_map_alloc_dim(struct isl_dim *dim, int n, unsigned flags)
2659 struct isl_map *map;
2661 if (!dim)
2662 return NULL;
2663 isl_assert(dim->ctx, n >= 0, return NULL);
2664 map = isl_alloc(dim->ctx, struct isl_map,
2665 sizeof(struct isl_map) +
2666 (n - 1) * sizeof(struct isl_basic_map *));
2667 if (!map)
2668 goto error;
2670 map->ctx = dim->ctx;
2671 isl_ctx_ref(map->ctx);
2672 map->ref = 1;
2673 map->size = n;
2674 map->n = 0;
2675 map->dim = dim;
2676 map->flags = flags;
2677 return map;
2678 error:
2679 isl_dim_free(dim);
2680 return NULL;
2683 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
2684 unsigned nparam, unsigned in, unsigned out, int n,
2685 unsigned flags)
2687 struct isl_map *map;
2688 struct isl_dim *dims;
2690 dims = isl_dim_alloc(ctx, nparam, in, out);
2691 if (!dims)
2692 return NULL;
2694 map = isl_map_alloc_dim(dims, n, flags);
2695 return map;
2698 struct isl_basic_map *isl_basic_map_empty(struct isl_dim *dim)
2700 struct isl_basic_map *bmap;
2701 bmap = isl_basic_map_alloc_dim(dim, 0, 1, 0);
2702 bmap = isl_basic_map_set_to_empty(bmap);
2703 return bmap;
2706 struct isl_basic_set *isl_basic_set_empty(struct isl_dim *dim)
2708 struct isl_basic_set *bset;
2709 bset = isl_basic_set_alloc_dim(dim, 0, 1, 0);
2710 bset = isl_basic_set_set_to_empty(bset);
2711 return bset;
2714 struct isl_basic_map *isl_basic_map_empty_like(struct isl_basic_map *model)
2716 struct isl_basic_map *bmap;
2717 if (!model)
2718 return NULL;
2719 bmap = isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
2720 bmap = isl_basic_map_set_to_empty(bmap);
2721 return bmap;
2724 struct isl_basic_map *isl_basic_map_empty_like_map(struct isl_map *model)
2726 struct isl_basic_map *bmap;
2727 if (!model)
2728 return NULL;
2729 bmap = isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
2730 bmap = isl_basic_map_set_to_empty(bmap);
2731 return bmap;
2734 struct isl_basic_set *isl_basic_set_empty_like(struct isl_basic_set *model)
2736 struct isl_basic_set *bset;
2737 if (!model)
2738 return NULL;
2739 bset = isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
2740 bset = isl_basic_set_set_to_empty(bset);
2741 return bset;
2744 struct isl_basic_map *isl_basic_map_universe(struct isl_dim *dim)
2746 struct isl_basic_map *bmap;
2747 bmap = isl_basic_map_alloc_dim(dim, 0, 0, 0);
2748 return bmap;
2751 struct isl_basic_set *isl_basic_set_universe(struct isl_dim *dim)
2753 struct isl_basic_set *bset;
2754 bset = isl_basic_set_alloc_dim(dim, 0, 0, 0);
2755 return bset;
2758 struct isl_basic_set *isl_basic_set_universe_like(struct isl_basic_set *model)
2760 if (!model)
2761 return NULL;
2762 return isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
2765 struct isl_map *isl_map_empty(struct isl_dim *dim)
2767 return isl_map_alloc_dim(dim, 0, ISL_MAP_DISJOINT);
2770 struct isl_map *isl_map_empty_like(struct isl_map *model)
2772 if (!model)
2773 return NULL;
2774 return isl_map_alloc_dim(isl_dim_copy(model->dim), 0, ISL_MAP_DISJOINT);
2777 struct isl_map *isl_map_empty_like_basic_map(struct isl_basic_map *model)
2779 if (!model)
2780 return NULL;
2781 return isl_map_alloc_dim(isl_dim_copy(model->dim), 0, ISL_MAP_DISJOINT);
2784 struct isl_set *isl_set_empty(struct isl_dim *dim)
2786 return isl_set_alloc_dim(dim, 0, ISL_MAP_DISJOINT);
2789 struct isl_set *isl_set_empty_like(struct isl_set *model)
2791 if (!model)
2792 return NULL;
2793 return isl_set_empty(isl_dim_copy(model->dim));
2796 struct isl_map *isl_map_universe(struct isl_dim *dim)
2798 struct isl_map *map;
2799 if (!dim)
2800 return NULL;
2801 map = isl_map_alloc_dim(isl_dim_copy(dim), 1, ISL_MAP_DISJOINT);
2802 map = isl_map_add(map, isl_basic_map_universe(dim));
2803 return map;
2806 struct isl_set *isl_set_universe(struct isl_dim *dim)
2808 struct isl_set *set;
2809 if (!dim)
2810 return NULL;
2811 set = isl_set_alloc_dim(isl_dim_copy(dim), 1, ISL_MAP_DISJOINT);
2812 set = isl_set_add(set, isl_basic_set_universe(dim));
2813 return set;
2816 struct isl_map *isl_map_dup(struct isl_map *map)
2818 int i;
2819 struct isl_map *dup;
2821 if (!map)
2822 return NULL;
2823 dup = isl_map_alloc_dim(isl_dim_copy(map->dim), map->n, map->flags);
2824 for (i = 0; i < map->n; ++i)
2825 dup = isl_map_add(dup, isl_basic_map_copy(map->p[i]));
2826 return dup;
2829 struct isl_map *isl_map_add(struct isl_map *map, struct isl_basic_map *bmap)
2831 if (!bmap || !map)
2832 goto error;
2833 if (isl_basic_map_fast_is_empty(bmap)) {
2834 isl_basic_map_free(bmap);
2835 return map;
2837 isl_assert(map->ctx, isl_dim_equal(map->dim, bmap->dim), goto error);
2838 isl_assert(map->ctx, map->n < map->size, goto error);
2839 map->p[map->n] = bmap;
2840 map->n++;
2841 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
2842 return map;
2843 error:
2844 if (map)
2845 isl_map_free(map);
2846 if (bmap)
2847 isl_basic_map_free(bmap);
2848 return NULL;
2851 void isl_map_free(struct isl_map *map)
2853 int i;
2855 if (!map)
2856 return;
2858 if (--map->ref > 0)
2859 return;
2861 isl_ctx_deref(map->ctx);
2862 for (i = 0; i < map->n; ++i)
2863 isl_basic_map_free(map->p[i]);
2864 isl_dim_free(map->dim);
2865 free(map);
2868 struct isl_map *isl_map_extend(struct isl_map *base,
2869 unsigned nparam, unsigned n_in, unsigned n_out)
2871 int i;
2873 base = isl_map_cow(base);
2874 if (!base)
2875 return NULL;
2877 base->dim = isl_dim_extend(base->dim, nparam, n_in, n_out);
2878 if (!base->dim)
2879 goto error;
2880 for (i = 0; i < base->n; ++i) {
2881 base->p[i] = isl_basic_map_extend_dim(base->p[i],
2882 isl_dim_copy(base->dim), 0, 0, 0);
2883 if (!base->p[i])
2884 goto error;
2886 return base;
2887 error:
2888 isl_map_free(base);
2889 return NULL;
2892 struct isl_set *isl_set_extend(struct isl_set *base,
2893 unsigned nparam, unsigned dim)
2895 return (struct isl_set *)isl_map_extend((struct isl_map *)base,
2896 nparam, 0, dim);
2899 static struct isl_basic_map *isl_basic_map_fix_pos(struct isl_basic_map *bmap,
2900 unsigned pos, int value)
2902 int j;
2904 bmap = isl_basic_map_cow(bmap);
2905 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
2906 j = isl_basic_map_alloc_equality(bmap);
2907 if (j < 0)
2908 goto error;
2909 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
2910 isl_int_set_si(bmap->eq[j][pos], -1);
2911 isl_int_set_si(bmap->eq[j][0], value);
2912 bmap = isl_basic_map_simplify(bmap);
2913 return isl_basic_map_finalize(bmap);
2914 error:
2915 isl_basic_map_free(bmap);
2916 return NULL;
2919 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
2920 enum isl_dim_type type, unsigned pos, int value)
2922 if (!bmap)
2923 return NULL;
2924 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
2925 return isl_basic_map_fix_pos(bmap, isl_basic_map_offset(bmap, type) + pos,
2926 value);
2927 error:
2928 isl_basic_map_free(bmap);
2929 return NULL;
2932 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
2933 enum isl_dim_type type, unsigned pos, int value)
2935 return (struct isl_basic_set *)
2936 isl_basic_map_fix_si((struct isl_basic_map *)bset,
2937 type, pos, value);
2940 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
2941 unsigned input, int value)
2943 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
2946 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
2947 unsigned dim, int value)
2949 return (struct isl_basic_set *)
2950 isl_basic_map_fix_si((struct isl_basic_map *)bset,
2951 isl_dim_set, dim, value);
2954 struct isl_map *isl_map_fix_si(struct isl_map *map,
2955 enum isl_dim_type type, unsigned pos, int value)
2957 int i;
2959 map = isl_map_cow(map);
2960 if (!map)
2961 return NULL;
2963 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
2964 for (i = 0; i < map->n; ++i) {
2965 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
2966 if (!map->p[i])
2967 goto error;
2969 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
2970 return map;
2971 error:
2972 isl_map_free(map);
2973 return NULL;
2976 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
2977 unsigned input, int value)
2979 return isl_map_fix_si(map, isl_dim_in, input, value);
2982 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
2984 return (struct isl_set *)
2985 isl_map_fix_si((struct isl_map *)set, isl_dim_set, dim, value);
2988 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
2989 unsigned dim, isl_int value)
2991 int j;
2992 unsigned nparam;
2994 bset = isl_basic_set_cow(bset);
2995 bset = isl_basic_set_extend_constraints(bset, 0, 1);
2996 j = isl_basic_set_alloc_inequality(bset);
2997 if (j < 0)
2998 goto error;
2999 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
3000 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
3001 isl_int_neg(bset->ineq[j][0], value);
3002 bset = isl_basic_set_simplify(bset);
3003 return isl_basic_set_finalize(bset);
3004 error:
3005 isl_basic_set_free(bset);
3006 return NULL;
3009 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
3010 isl_int value)
3012 int i;
3014 set = isl_set_cow(set);
3015 if (!set)
3016 return NULL;
3018 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
3019 for (i = 0; i < set->n; ++i) {
3020 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
3021 if (!set->p[i])
3022 goto error;
3024 return set;
3025 error:
3026 isl_set_free(set);
3027 return NULL;
3030 struct isl_map *isl_map_reverse(struct isl_map *map)
3032 int i;
3033 unsigned t;
3035 map = isl_map_cow(map);
3036 if (!map)
3037 return NULL;
3039 map->dim = isl_dim_reverse(map->dim);
3040 if (!map->dim)
3041 goto error;
3042 for (i = 0; i < map->n; ++i) {
3043 map->p[i] = isl_basic_map_reverse(map->p[i]);
3044 if (!map->p[i])
3045 goto error;
3047 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3048 return map;
3049 error:
3050 isl_map_free(map);
3051 return NULL;
3054 static struct isl_map *isl_basic_map_partial_lexopt(
3055 struct isl_basic_map *bmap, struct isl_basic_set *dom,
3056 struct isl_set **empty, int max)
3058 if (!bmap)
3059 goto error;
3060 if (bmap->ctx->pip == ISL_PIP_PIP)
3061 return isl_pip_basic_map_lexopt(bmap, dom, empty, max);
3062 else
3063 return isl_tab_basic_map_partial_lexopt(bmap, dom, empty, max);
3064 error:
3065 isl_basic_map_free(bmap);
3066 isl_basic_set_free(dom);
3067 if (empty)
3068 *empty = NULL;
3069 return NULL;
3072 struct isl_map *isl_basic_map_partial_lexmax(
3073 struct isl_basic_map *bmap, struct isl_basic_set *dom,
3074 struct isl_set **empty)
3076 return isl_basic_map_partial_lexopt(bmap, dom, empty, 1);
3079 struct isl_map *isl_basic_map_partial_lexmin(
3080 struct isl_basic_map *bmap, struct isl_basic_set *dom,
3081 struct isl_set **empty)
3083 return isl_basic_map_partial_lexopt(bmap, dom, empty, 0);
3086 struct isl_set *isl_basic_set_partial_lexmin(
3087 struct isl_basic_set *bset, struct isl_basic_set *dom,
3088 struct isl_set **empty)
3090 return (struct isl_set *)
3091 isl_basic_map_partial_lexmin((struct isl_basic_map *)bset,
3092 dom, empty);
3095 struct isl_set *isl_basic_set_partial_lexmax(
3096 struct isl_basic_set *bset, struct isl_basic_set *dom,
3097 struct isl_set **empty)
3099 return (struct isl_set *)
3100 isl_basic_map_partial_lexmax((struct isl_basic_map *)bset,
3101 dom, empty);
3104 struct isl_set *isl_basic_set_lexmin(struct isl_basic_set *bset)
3106 struct isl_basic_map *bmap = NULL;
3107 struct isl_basic_set *dom = NULL;
3108 struct isl_map *min;
3109 struct isl_dim *param_dim;
3111 if (!bset)
3112 goto error;
3113 bmap = isl_basic_map_from_basic_set(bset, isl_dim_copy(bset->dim));
3114 if (!bmap)
3115 goto error;
3116 param_dim = isl_dim_domain(isl_dim_copy(bmap->dim));
3117 dom = isl_basic_set_universe(param_dim);
3118 if (!dom)
3119 goto error;
3120 min = isl_basic_map_partial_lexmin(bmap, dom, NULL);
3121 return isl_map_range(min);
3122 error:
3123 isl_basic_map_free(bmap);
3124 return NULL;
3127 static struct isl_map *isl_map_reset_dim(struct isl_map *map,
3128 struct isl_dim *dim)
3130 int i;
3132 if (!map || !dim)
3133 goto error;
3135 for (i = 0; i < map->n; ++i) {
3136 isl_dim_free(map->p[i]->dim);
3137 map->p[i]->dim = isl_dim_copy(dim);
3139 isl_dim_free(map->dim);
3140 map->dim = dim;
3142 return map;
3143 error:
3144 isl_map_free(map);
3145 isl_dim_free(dim);
3146 return NULL;
3149 static struct isl_set *isl_set_reset_dim(struct isl_set *set,
3150 struct isl_dim *dim)
3152 return (struct isl_set *) isl_map_reset_dim((struct isl_map *)set, dim);
3155 /* Apply a preimage specified by "mat" on the parameters of "bset".
3156 * bset is assumed to have only parameters and divs.
3158 static struct isl_basic_set *basic_set_parameter_preimage(
3159 struct isl_basic_set *bset, struct isl_mat *mat)
3161 unsigned nparam;
3163 if (!bset || !mat)
3164 goto error;
3166 bset->dim = isl_dim_cow(bset->dim);
3167 if (!bset->dim)
3168 goto error;
3170 nparam = isl_basic_set_dim(bset, isl_dim_param);
3172 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
3174 bset->dim->nparam = 0;
3175 bset->dim->n_out = nparam;
3176 bset = isl_basic_set_preimage(bset, mat);
3177 if (bset) {
3178 bset->dim->nparam = bset->dim->n_out;
3179 bset->dim->n_out = 0;
3181 return bset;
3182 error:
3183 isl_mat_free(mat);
3184 isl_basic_set_free(bset);
3185 return NULL;
3188 /* Apply a preimage specified by "mat" on the parameters of "set".
3189 * set is assumed to have only parameters and divs.
3191 static struct isl_set *set_parameter_preimage(
3192 struct isl_set *set, struct isl_mat *mat)
3194 struct isl_dim *dim = NULL;
3195 unsigned nparam;
3197 if (!set || !mat)
3198 goto error;
3200 dim = isl_dim_copy(set->dim);
3201 dim = isl_dim_cow(dim);
3202 if (!dim)
3203 goto error;
3205 nparam = isl_set_dim(set, isl_dim_param);
3207 isl_assert(set->ctx, mat->n_row == 1 + nparam, goto error);
3209 dim->nparam = 0;
3210 dim->n_out = nparam;
3211 isl_set_reset_dim(set, dim);
3212 set = isl_set_preimage(set, mat);
3213 if (!set)
3214 goto error2;
3215 dim = isl_dim_copy(set->dim);
3216 dim = isl_dim_cow(dim);
3217 if (!dim)
3218 goto error2;
3219 dim->nparam = dim->n_out;
3220 dim->n_out = 0;
3221 isl_set_reset_dim(set, dim);
3222 return set;
3223 error:
3224 isl_dim_free(dim);
3225 isl_mat_free(mat);
3226 error2:
3227 isl_set_free(set);
3228 return NULL;
3231 /* Intersect the basic set "bset" with the affine space specified by the
3232 * equalities in "eq".
3234 static struct isl_basic_set *basic_set_append_equalities(
3235 struct isl_basic_set *bset, struct isl_mat *eq)
3237 int i, k;
3238 unsigned len;
3240 if (!bset || !eq)
3241 goto error;
3243 bset = isl_basic_set_extend_dim(bset, isl_dim_copy(bset->dim), 0,
3244 eq->n_row, 0);
3245 if (!bset)
3246 goto error;
3248 len = 1 + isl_dim_total(bset->dim) + bset->extra;
3249 for (i = 0; i < eq->n_row; ++i) {
3250 k = isl_basic_set_alloc_equality(bset);
3251 if (k < 0)
3252 goto error;
3253 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
3254 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
3256 isl_mat_free(eq);
3258 return bset;
3259 error:
3260 isl_mat_free(eq);
3261 isl_basic_set_free(bset);
3262 return NULL;
3265 /* Intersect the set "set" with the affine space specified by the
3266 * equalities in "eq".
3268 static struct isl_set *set_append_equalities(struct isl_set *set,
3269 struct isl_mat *eq)
3271 int i;
3273 if (!set || !eq)
3274 goto error;
3276 for (i = 0; i < set->n; ++i) {
3277 set->p[i] = basic_set_append_equalities(set->p[i],
3278 isl_mat_copy(eq));
3279 if (!set->p[i])
3280 goto error;
3282 isl_mat_free(eq);
3283 return set;
3284 error:
3285 isl_mat_free(eq);
3286 isl_set_free(set);
3287 return NULL;
3290 /* Project the given basic set onto its parameter domain, possibly introducing
3291 * new, explicit, existential variables in the constraints.
3292 * The input has parameters and (possibly implicit) existential variables.
3293 * The output has the same parameters, but only
3294 * explicit existentially quantified variables.
3296 * The actual projection is performed by pip, but pip doesn't seem
3297 * to like equalities very much, so we first remove the equalities
3298 * among the parameters by performing a variable compression on
3299 * the parameters. Afterward, an inverse transformation is performed
3300 * and the equalities among the parameters are inserted back in.
3302 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
3304 int i, j;
3305 struct isl_mat *eq;
3306 struct isl_mat *T, *T2;
3307 struct isl_set *set;
3308 unsigned nparam, n_div;
3310 bset = isl_basic_set_cow(bset);
3311 if (!bset)
3312 return NULL;
3314 if (bset->n_eq == 0)
3315 return isl_basic_set_lexmin(bset);
3317 isl_basic_set_gauss(bset, NULL);
3319 nparam = isl_basic_set_dim(bset, isl_dim_param);
3320 n_div = isl_basic_set_dim(bset, isl_dim_div);
3322 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
3323 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
3324 ++i;
3326 if (i == bset->n_eq)
3327 return isl_basic_set_lexmin(bset);
3329 eq = isl_mat_sub_alloc(bset->ctx, bset->eq, i, bset->n_eq - i,
3330 0, 1 + nparam);
3331 eq = isl_mat_cow(eq);
3332 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
3333 bset = basic_set_parameter_preimage(bset, T);
3335 set = isl_basic_set_lexmin(bset);
3336 set = set_parameter_preimage(set, T2);
3337 set = set_append_equalities(set, eq);
3338 return set;
3341 /* Compute an explicit representation for all the existentially
3342 * quantified variables.
3343 * The input and output dimensions are first turned into parameters.
3344 * compute_divs then returns a map with the same parameters and
3345 * no input or output dimensions and the dimension specification
3346 * is reset to that of the input.
3348 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
3350 struct isl_basic_set *bset;
3351 struct isl_set *set;
3352 struct isl_map *map;
3353 struct isl_dim *dim, *orig_dim = NULL;
3354 unsigned nparam;
3355 unsigned n_in;
3356 unsigned n_out;
3358 bmap = isl_basic_map_cow(bmap);
3359 if (!bmap)
3360 return NULL;
3362 nparam = isl_basic_map_dim(bmap, isl_dim_param);
3363 n_in = isl_basic_map_dim(bmap, isl_dim_in);
3364 n_out = isl_basic_map_dim(bmap, isl_dim_out);
3365 dim = isl_dim_set_alloc(bmap->ctx, nparam + n_in + n_out, 0);
3366 if (!dim)
3367 goto error;
3369 orig_dim = bmap->dim;
3370 bmap->dim = dim;
3371 bset = (struct isl_basic_set *)bmap;
3373 set = parameter_compute_divs(bset);
3374 map = (struct isl_map *)set;
3375 map = isl_map_reset_dim(map, orig_dim);
3377 return map;
3378 error:
3379 isl_basic_map_free(bmap);
3380 return NULL;
3383 /* If bmap contains any unknown divs, then compute explicit
3384 * expressions for them. However, this computation may be
3385 * quite expensive, so first try to remove divs that aren't
3386 * strictly needed.
3388 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
3390 int i;
3391 unsigned off;
3393 if (!bmap)
3394 return NULL;
3395 off = isl_dim_total(bmap->dim);
3396 for (i = 0; i < bmap->n_div; ++i) {
3397 if (isl_int_is_zero(bmap->div[i][0]))
3398 break;
3399 isl_assert(bmap->ctx, isl_int_is_zero(bmap->div[i][1+1+off+i]),
3400 goto error);
3402 if (i == bmap->n_div)
3403 return isl_map_from_basic_map(bmap);
3404 bmap = isl_basic_map_drop_redundant_divs(bmap);
3405 if (!bmap)
3406 goto error;
3407 for (i = 0; i < bmap->n_div; ++i)
3408 if (isl_int_is_zero(bmap->div[i][0]))
3409 break;
3410 if (i == bmap->n_div)
3411 return isl_map_from_basic_map(bmap);
3412 struct isl_map *map = compute_divs(bmap);
3413 return map;
3414 error:
3415 isl_basic_map_free(bmap);
3416 return NULL;
3419 struct isl_map *isl_map_compute_divs(struct isl_map *map)
3421 int i;
3422 struct isl_map *res;
3424 if (!map)
3425 return NULL;
3426 if (map->n == 0)
3427 return map;
3428 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
3429 for (i = 1 ; i < map->n; ++i) {
3430 struct isl_map *r2;
3431 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
3432 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
3433 res = isl_map_union_disjoint(res, r2);
3434 else
3435 res = isl_map_union(res, r2);
3437 isl_map_free(map);
3439 return res;
3442 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
3444 return (struct isl_set *)
3445 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
3448 struct isl_set *isl_set_compute_divs(struct isl_set *set)
3450 return (struct isl_set *)
3451 isl_map_compute_divs((struct isl_map *)set);
3454 struct isl_set *isl_map_domain(struct isl_map *map)
3456 int i;
3457 struct isl_set *set;
3459 if (!map)
3460 goto error;
3462 map = isl_map_cow(map);
3463 if (!map)
3464 return NULL;
3466 set = (struct isl_set *)map;
3467 set->dim = isl_dim_domain(set->dim);
3468 if (!set->dim)
3469 goto error;
3470 for (i = 0; i < map->n; ++i) {
3471 set->p[i] = isl_basic_map_domain(map->p[i]);
3472 if (!set->p[i])
3473 goto error;
3475 ISL_F_CLR(set, ISL_MAP_DISJOINT);
3476 ISL_F_CLR(set, ISL_SET_NORMALIZED);
3477 return set;
3478 error:
3479 isl_map_free(map);
3480 return NULL;
3483 struct isl_map *isl_map_union_disjoint(
3484 struct isl_map *map1, struct isl_map *map2)
3486 int i;
3487 unsigned flags = 0;
3488 struct isl_map *map = NULL;
3490 if (!map1 || !map2)
3491 goto error;
3493 if (map1->n == 0) {
3494 isl_map_free(map1);
3495 return map2;
3497 if (map2->n == 0) {
3498 isl_map_free(map2);
3499 return map1;
3502 isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
3504 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
3505 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
3506 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3508 map = isl_map_alloc_dim(isl_dim_copy(map1->dim),
3509 map1->n + map2->n, flags);
3510 if (!map)
3511 goto error;
3512 for (i = 0; i < map1->n; ++i) {
3513 map = isl_map_add(map,
3514 isl_basic_map_copy(map1->p[i]));
3515 if (!map)
3516 goto error;
3518 for (i = 0; i < map2->n; ++i) {
3519 map = isl_map_add(map,
3520 isl_basic_map_copy(map2->p[i]));
3521 if (!map)
3522 goto error;
3524 isl_map_free(map1);
3525 isl_map_free(map2);
3526 return map;
3527 error:
3528 isl_map_free(map);
3529 isl_map_free(map1);
3530 isl_map_free(map2);
3531 return NULL;
3534 struct isl_map *isl_map_union(struct isl_map *map1, struct isl_map *map2)
3536 map1 = isl_map_union_disjoint(map1, map2);
3537 if (!map1)
3538 return NULL;
3539 if (map1->n > 1)
3540 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
3541 return map1;
3544 struct isl_set *isl_set_union_disjoint(
3545 struct isl_set *set1, struct isl_set *set2)
3547 return (struct isl_set *)
3548 isl_map_union_disjoint(
3549 (struct isl_map *)set1, (struct isl_map *)set2);
3552 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
3554 return (struct isl_set *)
3555 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
3558 struct isl_map *isl_map_intersect_range(
3559 struct isl_map *map, struct isl_set *set)
3561 unsigned flags = 0;
3562 struct isl_map *result;
3563 int i, j;
3565 if (!map || !set)
3566 goto error;
3568 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
3569 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
3570 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3572 result = isl_map_alloc_dim(isl_dim_copy(map->dim),
3573 map->n * set->n, flags);
3574 if (!result)
3575 goto error;
3576 for (i = 0; i < map->n; ++i)
3577 for (j = 0; j < set->n; ++j) {
3578 result = isl_map_add(result,
3579 isl_basic_map_intersect_range(
3580 isl_basic_map_copy(map->p[i]),
3581 isl_basic_set_copy(set->p[j])));
3582 if (!result)
3583 goto error;
3585 isl_map_free(map);
3586 isl_set_free(set);
3587 return result;
3588 error:
3589 isl_map_free(map);
3590 isl_set_free(set);
3591 return NULL;
3594 struct isl_map *isl_map_intersect_domain(
3595 struct isl_map *map, struct isl_set *set)
3597 return isl_map_reverse(
3598 isl_map_intersect_range(isl_map_reverse(map), set));
3601 struct isl_map *isl_map_apply_domain(
3602 struct isl_map *map1, struct isl_map *map2)
3604 if (!map1 || !map2)
3605 goto error;
3606 map1 = isl_map_reverse(map1);
3607 map1 = isl_map_apply_range(map1, map2);
3608 return isl_map_reverse(map1);
3609 error:
3610 isl_map_free(map1);
3611 isl_map_free(map2);
3612 return NULL;
3615 struct isl_map *isl_map_apply_range(
3616 struct isl_map *map1, struct isl_map *map2)
3618 struct isl_dim *dim_result;
3619 struct isl_map *result;
3620 int i, j;
3621 unsigned nparam;
3622 unsigned n_in;
3623 unsigned n_out;
3625 if (!map1 || !map2)
3626 goto error;
3628 dim_result = isl_dim_join(isl_dim_copy(map1->dim),
3629 isl_dim_copy(map2->dim));
3631 result = isl_map_alloc_dim(dim_result, map1->n * map2->n, 0);
3632 if (!result)
3633 goto error;
3634 for (i = 0; i < map1->n; ++i)
3635 for (j = 0; j < map2->n; ++j) {
3636 result = isl_map_add(result,
3637 isl_basic_map_apply_range(
3638 isl_basic_map_copy(map1->p[i]),
3639 isl_basic_map_copy(map2->p[j])));
3640 if (!result)
3641 goto error;
3643 isl_map_free(map1);
3644 isl_map_free(map2);
3645 if (result && result->n <= 1)
3646 ISL_F_SET(result, ISL_MAP_DISJOINT);
3647 return result;
3648 error:
3649 isl_map_free(map1);
3650 isl_map_free(map2);
3651 return NULL;
3655 * returns range - domain
3657 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
3659 struct isl_basic_set *bset;
3660 unsigned dim;
3661 unsigned nparam;
3662 int i;
3664 if (!bmap)
3665 goto error;
3666 dim = isl_basic_map_n_in(bmap);
3667 nparam = isl_basic_map_n_param(bmap);
3668 isl_assert(bmap->ctx, dim == isl_basic_map_n_out(bmap), goto error);
3669 bset = isl_basic_set_from_basic_map(bmap);
3670 bset = isl_basic_set_cow(bset);
3671 bset = isl_basic_set_extend(bset, nparam, 3*dim, 0, dim, 0);
3672 bset = isl_basic_set_swap_vars(bset, 2*dim);
3673 for (i = 0; i < dim; ++i) {
3674 int j = isl_basic_map_alloc_equality(
3675 (struct isl_basic_map *)bset);
3676 if (j < 0)
3677 goto error;
3678 isl_seq_clr(bset->eq[j], 1 + isl_basic_set_total_dim(bset));
3679 isl_int_set_si(bset->eq[j][1+nparam+i], 1);
3680 isl_int_set_si(bset->eq[j][1+nparam+dim+i], 1);
3681 isl_int_set_si(bset->eq[j][1+nparam+2*dim+i], -1);
3683 return isl_basic_set_project_out(bset, 2*dim, 0);
3684 error:
3685 isl_basic_map_free(bmap);
3686 return NULL;
3690 * returns range - domain
3692 struct isl_set *isl_map_deltas(struct isl_map *map)
3694 int i;
3695 struct isl_set *result;
3697 if (!map)
3698 return NULL;
3700 isl_assert(map->ctx, isl_map_n_in(map) == isl_map_n_out(map), goto error);
3701 result = isl_set_alloc(map->ctx, isl_map_n_param(map),
3702 isl_map_n_in(map), map->n, map->flags);
3703 if (!result)
3704 goto error;
3705 for (i = 0; i < map->n; ++i)
3706 result = isl_set_add(result,
3707 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
3708 isl_map_free(map);
3709 return result;
3710 error:
3711 isl_map_free(map);
3712 return NULL;
3715 static struct isl_basic_map *basic_map_identity(struct isl_dim *dims)
3717 struct isl_basic_map *bmap;
3718 unsigned nparam;
3719 unsigned dim;
3720 int i;
3722 if (!dims)
3723 return NULL;
3725 nparam = dims->nparam;
3726 dim = dims->n_out;
3727 bmap = isl_basic_map_alloc_dim(dims, 0, dim, 0);
3728 if (!bmap)
3729 goto error;
3731 for (i = 0; i < dim; ++i) {
3732 int j = isl_basic_map_alloc_equality(bmap);
3733 if (j < 0)
3734 goto error;
3735 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
3736 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
3737 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
3739 return isl_basic_map_finalize(bmap);
3740 error:
3741 isl_basic_map_free(bmap);
3742 return NULL;
3745 struct isl_basic_map *isl_basic_map_identity(struct isl_dim *set_dim)
3747 struct isl_dim *dim = isl_dim_map(set_dim);
3748 if (!dim)
3749 return NULL;
3750 return basic_map_identity(dim);
3753 struct isl_basic_map *isl_basic_map_identity_like(struct isl_basic_map *model)
3755 if (!model || !model->dim)
3756 return NULL;
3757 isl_assert(model->ctx,
3758 model->dim->n_in == model->dim->n_out, return NULL);
3759 return basic_map_identity(isl_dim_copy(model->dim));
3762 static struct isl_map *map_identity(struct isl_dim *dim)
3764 struct isl_map *map = isl_map_alloc_dim(dim, 1, ISL_MAP_DISJOINT);
3765 return isl_map_add(map, basic_map_identity(isl_dim_copy(dim)));
3768 struct isl_map *isl_map_identity(struct isl_dim *set_dim)
3770 struct isl_dim *dim = isl_dim_map(set_dim);
3771 if (!dim)
3772 return NULL;
3773 return map_identity(dim);
3776 struct isl_map *isl_map_identity_like(struct isl_map *model)
3778 if (!model || !model->dim)
3779 return NULL;
3780 isl_assert(model->ctx,
3781 model->dim->n_in == model->dim->n_out, return NULL);
3782 return map_identity(isl_dim_copy(model->dim));
3785 struct isl_map *isl_map_identity_like_basic_map(struct isl_basic_map *model)
3787 if (!model || !model->dim)
3788 return NULL;
3789 isl_assert(model->ctx,
3790 model->dim->n_in == model->dim->n_out, return NULL);
3791 return map_identity(isl_dim_copy(model->dim));
3794 /* Construct a basic set with all set dimensions having only non-negative
3795 * values.
3797 struct isl_basic_set *isl_basic_set_positive_orthant(struct isl_dim *dims)
3799 int i;
3800 unsigned nparam;
3801 unsigned dim;
3802 struct isl_basic_set *bset;
3804 if (!dims)
3805 return NULL;
3806 nparam = dims->nparam;
3807 dim = dims->n_out;
3808 bset = isl_basic_set_alloc_dim(dims, 0, 0, dim);
3809 if (!bset)
3810 return NULL;
3811 for (i = 0; i < dim; ++i) {
3812 int k = isl_basic_set_alloc_inequality(bset);
3813 if (k < 0)
3814 goto error;
3815 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
3816 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
3818 return bset;
3819 error:
3820 isl_basic_set_free(bset);
3821 return NULL;
3824 int isl_set_is_equal(struct isl_set *set1, struct isl_set *set2)
3826 return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
3829 int isl_set_is_subset(struct isl_set *set1, struct isl_set *set2)
3831 return isl_map_is_subset(
3832 (struct isl_map *)set1, (struct isl_map *)set2);
3835 int isl_basic_map_is_subset(
3836 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3838 int is_subset;
3839 struct isl_map *map1;
3840 struct isl_map *map2;
3842 if (!bmap1 || !bmap2)
3843 return -1;
3845 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
3846 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
3848 is_subset = isl_map_is_subset(map1, map2);
3850 isl_map_free(map1);
3851 isl_map_free(map2);
3853 return is_subset;
3856 int isl_basic_map_is_equal(
3857 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3859 int is_subset;
3861 if (!bmap1 || !bmap2)
3862 return -1;
3863 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
3864 if (is_subset != 1)
3865 return is_subset;
3866 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
3867 return is_subset;
3870 int isl_basic_set_is_equal(
3871 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
3873 return isl_basic_map_is_equal(
3874 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
3877 int isl_map_is_empty(struct isl_map *map)
3879 int i;
3880 int is_empty;
3882 if (!map)
3883 return -1;
3884 for (i = 0; i < map->n; ++i) {
3885 is_empty = isl_basic_map_is_empty(map->p[i]);
3886 if (is_empty < 0)
3887 return -1;
3888 if (!is_empty)
3889 return 0;
3891 return 1;
3894 int isl_map_fast_is_empty(struct isl_map *map)
3896 return map->n == 0;
3899 int isl_set_is_empty(struct isl_set *set)
3901 return isl_map_is_empty((struct isl_map *)set);
3904 int isl_map_is_subset(struct isl_map *map1, struct isl_map *map2)
3906 int i;
3907 int is_subset = 0;
3908 struct isl_map *diff;
3910 if (!map1 || !map2)
3911 return -1;
3913 if (isl_map_is_empty(map1))
3914 return 1;
3916 if (isl_map_is_empty(map2))
3917 return 0;
3919 diff = isl_map_subtract(isl_map_copy(map1), isl_map_copy(map2));
3920 if (!diff)
3921 return -1;
3923 is_subset = isl_map_is_empty(diff);
3924 isl_map_free(diff);
3926 return is_subset;
3929 int isl_map_is_equal(struct isl_map *map1, struct isl_map *map2)
3931 int is_subset;
3933 if (!map1 || !map2)
3934 return -1;
3935 is_subset = isl_map_is_subset(map1, map2);
3936 if (is_subset != 1)
3937 return is_subset;
3938 is_subset = isl_map_is_subset(map2, map1);
3939 return is_subset;
3942 int isl_basic_map_is_strict_subset(
3943 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3945 int is_subset;
3947 if (!bmap1 || !bmap2)
3948 return -1;
3949 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
3950 if (is_subset != 1)
3951 return is_subset;
3952 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
3953 if (is_subset == -1)
3954 return is_subset;
3955 return !is_subset;
3958 int isl_map_is_strict_subset(struct isl_map *map1, struct isl_map *map2)
3960 int is_subset;
3962 if (!map1 || !map2)
3963 return -1;
3964 is_subset = isl_map_is_subset(map1, map2);
3965 if (is_subset != 1)
3966 return is_subset;
3967 is_subset = isl_map_is_subset(map2, map1);
3968 if (is_subset == -1)
3969 return is_subset;
3970 return !is_subset;
3973 int isl_basic_map_is_universe(struct isl_basic_map *bmap)
3975 if (!bmap)
3976 return -1;
3977 return bmap->n_eq == 0 && bmap->n_ineq == 0;
3980 int isl_basic_set_is_universe(struct isl_basic_set *bset)
3982 if (!bset)
3983 return -1;
3984 return bset->n_eq == 0 && bset->n_ineq == 0;
3987 int isl_basic_map_is_empty(struct isl_basic_map *bmap)
3989 struct isl_basic_set *bset = NULL;
3990 struct isl_vec *sample = NULL;
3991 int empty;
3992 unsigned total;
3994 if (!bmap)
3995 return -1;
3997 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
3998 return 1;
4000 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
4001 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
4002 copy = isl_basic_map_convex_hull(copy);
4003 empty = ISL_F_ISSET(copy, ISL_BASIC_MAP_EMPTY);
4004 isl_basic_map_free(copy);
4005 return empty;
4008 total = 1 + isl_basic_map_total_dim(bmap);
4009 if (bmap->sample && bmap->sample->size == total) {
4010 int contains = basic_map_contains(bmap, bmap->sample);
4011 if (contains < 0)
4012 return -1;
4013 if (contains)
4014 return 0;
4016 isl_vec_free(bmap->sample);
4017 bmap->sample = NULL;
4018 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
4019 if (!bset)
4020 return -1;
4021 sample = isl_basic_set_sample(bset);
4022 if (!sample)
4023 return -1;
4024 empty = sample->size == 0;
4025 isl_vec_free(bmap->sample);
4026 bmap->sample = sample;
4027 if (empty)
4028 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
4030 return empty;
4033 int isl_basic_map_fast_is_empty(struct isl_basic_map *bmap)
4035 if (!bmap)
4036 return -1;
4037 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
4040 int isl_basic_set_fast_is_empty(struct isl_basic_set *bset)
4042 if (!bset)
4043 return -1;
4044 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
4047 int isl_basic_set_is_empty(struct isl_basic_set *bset)
4049 return isl_basic_map_is_empty((struct isl_basic_map *)bset);
4052 struct isl_map *isl_basic_map_union(
4053 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4055 struct isl_map *map;
4056 if (!bmap1 || !bmap2)
4057 return NULL;
4059 isl_assert(bmap1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim), goto error);
4061 map = isl_map_alloc_dim(isl_dim_copy(bmap1->dim), 2, 0);
4062 if (!map)
4063 goto error;
4064 map = isl_map_add(map, bmap1);
4065 map = isl_map_add(map, bmap2);
4066 return map;
4067 error:
4068 isl_basic_map_free(bmap1);
4069 isl_basic_map_free(bmap2);
4070 return NULL;
4073 struct isl_set *isl_basic_set_union(
4074 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
4076 return (struct isl_set *)isl_basic_map_union(
4077 (struct isl_basic_map *)bset1,
4078 (struct isl_basic_map *)bset2);
4081 /* Order divs such that any div only depends on previous divs */
4082 struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
4084 int i;
4085 unsigned off = isl_dim_total(bmap->dim);
4087 for (i = 0; i < bmap->n_div; ++i) {
4088 int pos;
4089 if (isl_int_is_zero(bmap->div[i][0]))
4090 continue;
4091 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
4092 bmap->n_div-i);
4093 if (pos == -1)
4094 continue;
4095 isl_basic_map_swap_div(bmap, i, i + pos);
4096 --i;
4098 return bmap;
4101 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
4103 return (struct isl_basic_set *)
4104 isl_basic_map_order_divs((struct isl_basic_map *)bset);
4107 /* Look for a div in dst that corresponds to the div "div" in src.
4108 * The divs before "div" in src and dst are assumed to be the same.
4110 * Returns -1 if no corresponding div was found and the position
4111 * of the corresponding div in dst otherwise.
4113 static int find_div(struct isl_basic_map *dst,
4114 struct isl_basic_map *src, unsigned div)
4116 int i;
4118 unsigned total = isl_dim_total(src->dim);
4120 isl_assert(dst->ctx, div <= dst->n_div, return -1);
4121 for (i = div; i < dst->n_div; ++i)
4122 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
4123 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
4124 dst->n_div - div) == -1)
4125 return i;
4126 return -1;
4129 struct isl_basic_map *isl_basic_map_align_divs(
4130 struct isl_basic_map *dst, struct isl_basic_map *src)
4132 int i;
4133 unsigned total = isl_dim_total(src->dim);
4135 if (!dst || !src)
4136 goto error;
4138 if (src->n_div == 0)
4139 return dst;
4141 for (i = 0; i < src->n_div; ++i)
4142 isl_assert(src->ctx, !isl_int_is_zero(src->div[i][0]), goto error);
4144 src = isl_basic_map_order_divs(src);
4145 dst = isl_basic_map_cow(dst);
4146 dst = isl_basic_map_extend_dim(dst, isl_dim_copy(dst->dim),
4147 src->n_div, 0, 2 * src->n_div);
4148 if (!dst)
4149 return NULL;
4150 for (i = 0; i < src->n_div; ++i) {
4151 int j = find_div(dst, src, i);
4152 if (j < 0) {
4153 j = isl_basic_map_alloc_div(dst);
4154 if (j < 0)
4155 goto error;
4156 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
4157 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
4158 if (add_div_constraints(dst, j) < 0)
4159 goto error;
4161 if (j != i)
4162 isl_basic_map_swap_div(dst, i, j);
4164 return dst;
4165 error:
4166 isl_basic_map_free(dst);
4167 return NULL;
4170 struct isl_basic_set *isl_basic_set_align_divs(
4171 struct isl_basic_set *dst, struct isl_basic_set *src)
4173 return (struct isl_basic_set *)isl_basic_map_align_divs(
4174 (struct isl_basic_map *)dst, (struct isl_basic_map *)src);
4177 struct isl_map *isl_map_align_divs(struct isl_map *map)
4179 int i;
4181 map = isl_map_compute_divs(map);
4182 map = isl_map_cow(map);
4183 if (!map)
4184 return NULL;
4186 for (i = 1; i < map->n; ++i)
4187 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
4188 for (i = 1; i < map->n; ++i)
4189 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
4191 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4192 return map;
4195 struct isl_set *isl_set_align_divs(struct isl_set *set)
4197 return (struct isl_set *)isl_map_align_divs((struct isl_map *)set);
4200 static struct isl_map *add_cut_constraint(struct isl_map *dst,
4201 struct isl_basic_map *src, isl_int *c,
4202 unsigned len, int oppose)
4204 struct isl_basic_map *copy = NULL;
4205 int is_empty;
4206 int k;
4207 unsigned total;
4209 copy = isl_basic_map_copy(src);
4210 copy = isl_basic_map_cow(copy);
4211 if (!copy)
4212 goto error;
4213 copy = isl_basic_map_extend_constraints(copy, 0, 1);
4214 k = isl_basic_map_alloc_inequality(copy);
4215 if (k < 0)
4216 goto error;
4217 if (oppose)
4218 isl_seq_neg(copy->ineq[k], c, len);
4219 else
4220 isl_seq_cpy(copy->ineq[k], c, len);
4221 total = 1 + isl_basic_map_total_dim(copy);
4222 isl_seq_clr(copy->ineq[k]+len, total - len);
4223 isl_inequality_negate(copy, k);
4224 copy = isl_basic_map_simplify(copy);
4225 copy = isl_basic_map_finalize(copy);
4226 is_empty = isl_basic_map_is_empty(copy);
4227 if (is_empty < 0)
4228 goto error;
4229 if (!is_empty)
4230 dst = isl_map_add(dst, copy);
4231 else
4232 isl_basic_map_free(copy);
4233 return dst;
4234 error:
4235 isl_basic_map_free(copy);
4236 isl_map_free(dst);
4237 return NULL;
4240 static struct isl_map *subtract(struct isl_map *map, struct isl_basic_map *bmap)
4242 int i, j, k;
4243 unsigned flags = 0;
4244 struct isl_map *rest = NULL;
4245 unsigned max;
4246 unsigned total = isl_basic_map_total_dim(bmap);
4248 assert(bmap);
4250 if (!map)
4251 goto error;
4253 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
4254 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
4256 max = map->n * (2 * bmap->n_eq + bmap->n_ineq);
4257 rest = isl_map_alloc_dim(isl_dim_copy(map->dim), max, flags);
4258 if (!rest)
4259 goto error;
4261 for (i = 0; i < map->n; ++i) {
4262 map->p[i] = isl_basic_map_align_divs(map->p[i], bmap);
4263 if (!map->p[i])
4264 goto error;
4267 for (j = 0; j < map->n; ++j)
4268 map->p[j] = isl_basic_map_cow(map->p[j]);
4270 for (i = 0; i < bmap->n_eq; ++i) {
4271 for (j = 0; j < map->n; ++j) {
4272 rest = add_cut_constraint(rest,
4273 map->p[j], bmap->eq[i], 1+total, 0);
4274 if (!rest)
4275 goto error;
4277 rest = add_cut_constraint(rest,
4278 map->p[j], bmap->eq[i], 1+total, 1);
4279 if (!rest)
4280 goto error;
4282 map->p[j] = isl_basic_map_extend_constraints(map->p[j],
4283 1, 0);
4284 if (!map->p[j])
4285 goto error;
4286 k = isl_basic_map_alloc_equality(map->p[j]);
4287 if (k < 0)
4288 goto error;
4289 isl_seq_cpy(map->p[j]->eq[k], bmap->eq[i], 1+total);
4290 isl_seq_clr(map->p[j]->eq[k]+1+total,
4291 map->p[j]->n_div - bmap->n_div);
4295 for (i = 0; i < bmap->n_ineq; ++i) {
4296 for (j = 0; j < map->n; ++j) {
4297 rest = add_cut_constraint(rest,
4298 map->p[j], bmap->ineq[i], 1+total, 0);
4299 if (!rest)
4300 goto error;
4302 map->p[j] = isl_basic_map_extend_constraints(map->p[j],
4303 0, 1);
4304 if (!map->p[j])
4305 goto error;
4306 k = isl_basic_map_alloc_inequality(map->p[j]);
4307 if (k < 0)
4308 goto error;
4309 isl_seq_cpy(map->p[j]->ineq[k], bmap->ineq[i], 1+total);
4310 isl_seq_clr(map->p[j]->ineq[k]+1+total,
4311 map->p[j]->n_div - bmap->n_div);
4315 isl_map_free(map);
4316 return rest;
4317 error:
4318 isl_map_free(map);
4319 isl_map_free(rest);
4320 return NULL;
4323 struct isl_map *isl_map_subtract(struct isl_map *map1, struct isl_map *map2)
4325 int i;
4326 if (!map1 || !map2)
4327 goto error;
4329 isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
4331 if (isl_map_is_empty(map2)) {
4332 isl_map_free(map2);
4333 return map1;
4336 map1 = isl_map_compute_divs(map1);
4337 map2 = isl_map_compute_divs(map2);
4338 if (!map1 || !map2)
4339 goto error;
4341 for (i = 0; map1 && i < map2->n; ++i)
4342 map1 = subtract(map1, map2->p[i]);
4344 isl_map_free(map2);
4345 return map1;
4346 error:
4347 isl_map_free(map1);
4348 isl_map_free(map2);
4349 return NULL;
4352 struct isl_set *isl_set_subtract(struct isl_set *set1, struct isl_set *set2)
4354 return (struct isl_set *)
4355 isl_map_subtract(
4356 (struct isl_map *)set1, (struct isl_map *)set2);
4359 struct isl_set *isl_set_apply(struct isl_set *set, struct isl_map *map)
4361 if (!set || !map)
4362 goto error;
4363 isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
4364 map = isl_map_intersect_domain(map, set);
4365 set = isl_map_range(map);
4366 return set;
4367 error:
4368 isl_set_free(set);
4369 isl_map_free(map);
4370 return NULL;
4373 /* There is no need to cow as removing empty parts doesn't change
4374 * the meaning of the set.
4376 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
4378 int i;
4380 if (!map)
4381 return NULL;
4383 for (i = map->n-1; i >= 0; --i) {
4384 if (!ISL_F_ISSET(map->p[i], ISL_BASIC_MAP_EMPTY))
4385 continue;
4386 isl_basic_map_free(map->p[i]);
4387 if (i != map->n-1) {
4388 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4389 map->p[i] = map->p[map->n-1];
4391 map->n--;
4394 return map;
4397 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
4399 return (struct isl_set *)
4400 isl_map_remove_empty_parts((struct isl_map *)set);
4403 struct isl_basic_map *isl_map_copy_basic_map(struct isl_map *map)
4405 struct isl_basic_map *bmap;
4406 if (!map || map->n == 0)
4407 return NULL;
4408 bmap = map->p[map->n-1];
4409 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
4410 return isl_basic_map_copy(bmap);
4413 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
4415 return (struct isl_basic_set *)
4416 isl_map_copy_basic_map((struct isl_map *)set);
4419 struct isl_map *isl_map_drop_basic_map(struct isl_map *map,
4420 struct isl_basic_map *bmap)
4422 int i;
4424 if (!map || !bmap)
4425 goto error;
4426 for (i = map->n-1; i >= 0; --i) {
4427 if (map->p[i] != bmap)
4428 continue;
4429 map = isl_map_cow(map);
4430 if (!map)
4431 goto error;
4432 isl_basic_map_free(map->p[i]);
4433 if (i != map->n-1) {
4434 ISL_F_CLR(map, ISL_SET_NORMALIZED);
4435 map->p[i] = map->p[map->n-1];
4437 map->n--;
4438 return map;
4440 isl_basic_map_free(bmap);
4441 return map;
4442 error:
4443 isl_map_free(map);
4444 isl_basic_map_free(bmap);
4445 return NULL;
4448 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
4449 struct isl_basic_set *bset)
4451 return (struct isl_set *)isl_map_drop_basic_map((struct isl_map *)set,
4452 (struct isl_basic_map *)bset);
4455 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
4456 * for any common value of the parameters and dimensions preceding dim
4457 * in both basic sets, the values of dimension pos in bset1 are
4458 * smaller or larger than those in bset2.
4460 * Returns
4461 * 1 if bset1 follows bset2
4462 * -1 if bset1 precedes bset2
4463 * 0 if bset1 and bset2 are incomparable
4464 * -2 if some error occurred.
4466 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
4467 struct isl_basic_set *bset2, int pos)
4469 struct isl_dim *dims;
4470 struct isl_basic_map *bmap1 = NULL;
4471 struct isl_basic_map *bmap2 = NULL;
4472 struct isl_ctx *ctx;
4473 struct isl_vec *obj;
4474 unsigned total;
4475 unsigned nparam;
4476 unsigned dim1, dim2;
4477 isl_int num, den;
4478 enum isl_lp_result res;
4479 int cmp;
4481 if (!bset1 || !bset2)
4482 return -2;
4484 nparam = isl_basic_set_n_param(bset1);
4485 dim1 = isl_basic_set_n_dim(bset1);
4486 dim2 = isl_basic_set_n_dim(bset2);
4487 dims = isl_dim_alloc(bset1->ctx, nparam, pos, dim1 - pos);
4488 bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1), dims);
4489 dims = isl_dim_alloc(bset2->ctx, nparam, pos, dim2 - pos);
4490 bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims);
4491 if (!bmap1 || !bmap2)
4492 goto error;
4493 bmap1 = isl_basic_map_cow(bmap1);
4494 bmap1 = isl_basic_map_extend(bmap1, nparam,
4495 pos, (dim1 - pos) + (dim2 - pos),
4496 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
4497 bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
4498 if (!bmap1)
4499 goto error;
4500 total = isl_basic_map_total_dim(bmap1);
4501 ctx = bmap1->ctx;
4502 obj = isl_vec_alloc(ctx, 1 + total);
4503 isl_seq_clr(obj->block.data, 1 + total);
4504 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
4505 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
4506 if (!obj)
4507 goto error;
4508 isl_int_init(num);
4509 isl_int_init(den);
4510 res = isl_basic_map_solve_lp(bmap1, 0, obj->block.data, ctx->one,
4511 &num, &den, NULL);
4512 if (res == isl_lp_empty)
4513 cmp = 0;
4514 else if (res == isl_lp_ok && isl_int_is_pos(num))
4515 cmp = 1;
4516 else if ((res == isl_lp_ok && isl_int_is_neg(num)) ||
4517 res == isl_lp_unbounded)
4518 cmp = -1;
4519 else
4520 cmp = -2;
4521 isl_int_clear(num);
4522 isl_int_clear(den);
4523 isl_basic_map_free(bmap1);
4524 isl_vec_free(obj);
4525 return cmp;
4526 error:
4527 isl_basic_map_free(bmap1);
4528 isl_basic_map_free(bmap2);
4529 return -2;
4532 static int isl_basic_map_fast_has_fixed_var(struct isl_basic_map *bmap,
4533 unsigned pos, isl_int *val)
4535 int i;
4536 int d;
4537 unsigned total;
4539 if (!bmap)
4540 return -1;
4541 total = isl_basic_map_total_dim(bmap);
4542 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
4543 for (; d+1 > pos; --d)
4544 if (!isl_int_is_zero(bmap->eq[i][1+d]))
4545 break;
4546 if (d != pos)
4547 continue;
4548 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
4549 return 0;
4550 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
4551 return 0;
4552 if (!isl_int_is_one(bmap->eq[i][1+d]))
4553 return 0;
4554 if (val)
4555 isl_int_neg(*val, bmap->eq[i][0]);
4556 return 1;
4558 return 0;
4561 static int isl_map_fast_has_fixed_var(struct isl_map *map,
4562 unsigned pos, isl_int *val)
4564 int i;
4565 isl_int v;
4566 isl_int tmp;
4567 int fixed;
4569 if (!map)
4570 return -1;
4571 if (map->n == 0)
4572 return 0;
4573 if (map->n == 1)
4574 return isl_basic_map_fast_has_fixed_var(map->p[0], pos, val);
4575 isl_int_init(v);
4576 isl_int_init(tmp);
4577 fixed = isl_basic_map_fast_has_fixed_var(map->p[0], pos, &v);
4578 for (i = 1; fixed == 1 && i < map->n; ++i) {
4579 fixed = isl_basic_map_fast_has_fixed_var(map->p[i], pos, &tmp);
4580 if (fixed == 1 && isl_int_ne(tmp, v))
4581 fixed = 0;
4583 if (val)
4584 isl_int_set(*val, v);
4585 isl_int_clear(tmp);
4586 isl_int_clear(v);
4587 return fixed;
4590 static int isl_basic_set_fast_has_fixed_var(struct isl_basic_set *bset,
4591 unsigned pos, isl_int *val)
4593 return isl_basic_map_fast_has_fixed_var((struct isl_basic_map *)bset,
4594 pos, val);
4597 static int isl_set_fast_has_fixed_var(struct isl_set *set, unsigned pos,
4598 isl_int *val)
4600 return isl_map_fast_has_fixed_var((struct isl_map *)set, pos, val);
4603 int isl_basic_map_fast_is_fixed(struct isl_basic_map *bmap,
4604 enum isl_dim_type type, unsigned pos, isl_int *val)
4606 if (pos >= isl_basic_map_dim(bmap, type))
4607 return -1;
4608 return isl_basic_map_fast_has_fixed_var(bmap,
4609 isl_basic_map_offset(bmap, type) - 1 + pos, val);
4612 int isl_map_fast_is_fixed(struct isl_map *map,
4613 enum isl_dim_type type, unsigned pos, isl_int *val)
4615 if (pos >= isl_map_dim(map, type))
4616 return -1;
4617 return isl_map_fast_has_fixed_var(map,
4618 map_offset(map, type) - 1 + pos, val);
4621 /* Check if dimension dim has fixed value and if so and if val is not NULL,
4622 * then return this fixed value in *val.
4624 int isl_basic_set_fast_dim_is_fixed(struct isl_basic_set *bset, unsigned dim,
4625 isl_int *val)
4627 return isl_basic_set_fast_has_fixed_var(bset,
4628 isl_basic_set_n_param(bset) + dim, val);
4631 /* Check if dimension dim has fixed value and if so and if val is not NULL,
4632 * then return this fixed value in *val.
4634 int isl_set_fast_dim_is_fixed(struct isl_set *set, unsigned dim, isl_int *val)
4636 return isl_set_fast_has_fixed_var(set, isl_set_n_param(set) + dim, val);
4639 /* Check if input variable in has fixed value and if so and if val is not NULL,
4640 * then return this fixed value in *val.
4642 int isl_map_fast_input_is_fixed(struct isl_map *map, unsigned in, isl_int *val)
4644 return isl_map_fast_has_fixed_var(map, isl_map_n_param(map) + in, val);
4647 /* Check if dimension dim has an (obvious) fixed lower bound and if so
4648 * and if val is not NULL, then return this lower bound in *val.
4650 int isl_basic_set_fast_dim_has_fixed_lower_bound(struct isl_basic_set *bset,
4651 unsigned dim, isl_int *val)
4653 int i, i_eq = -1, i_ineq = -1;
4654 isl_int *c;
4655 unsigned total;
4656 unsigned nparam;
4658 if (!bset)
4659 return -1;
4660 total = isl_basic_set_total_dim(bset);
4661 nparam = isl_basic_set_n_param(bset);
4662 for (i = 0; i < bset->n_eq; ++i) {
4663 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
4664 continue;
4665 if (i_eq != -1)
4666 return 0;
4667 i_eq = i;
4669 for (i = 0; i < bset->n_ineq; ++i) {
4670 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
4671 continue;
4672 if (i_eq != -1 || i_ineq != -1)
4673 return 0;
4674 i_ineq = i;
4676 if (i_eq == -1 && i_ineq == -1)
4677 return 0;
4678 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
4679 /* The coefficient should always be one due to normalization. */
4680 if (!isl_int_is_one(c[1+nparam+dim]))
4681 return 0;
4682 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
4683 return 0;
4684 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
4685 total - nparam - dim - 1) != -1)
4686 return 0;
4687 if (val)
4688 isl_int_neg(*val, c[0]);
4689 return 1;
4692 int isl_set_fast_dim_has_fixed_lower_bound(struct isl_set *set,
4693 unsigned dim, isl_int *val)
4695 int i;
4696 isl_int v;
4697 isl_int tmp;
4698 int fixed;
4700 if (!set)
4701 return -1;
4702 if (set->n == 0)
4703 return 0;
4704 if (set->n == 1)
4705 return isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[0],
4706 dim, val);
4707 isl_int_init(v);
4708 isl_int_init(tmp);
4709 fixed = isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[0],
4710 dim, &v);
4711 for (i = 1; fixed == 1 && i < set->n; ++i) {
4712 fixed = isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[i],
4713 dim, &tmp);
4714 if (fixed == 1 && isl_int_ne(tmp, v))
4715 fixed = 0;
4717 if (val)
4718 isl_int_set(*val, v);
4719 isl_int_clear(tmp);
4720 isl_int_clear(v);
4721 return fixed;
4724 struct constraint {
4725 unsigned size;
4726 isl_int *c;
4729 static int qsort_constraint_cmp(const void *p1, const void *p2)
4731 const struct constraint *c1 = (const struct constraint *)p1;
4732 const struct constraint *c2 = (const struct constraint *)p2;
4733 unsigned size = isl_min(c1->size, c2->size);
4734 return isl_seq_cmp(c1->c, c2->c, size);
4737 static struct isl_basic_map *isl_basic_map_sort_constraints(
4738 struct isl_basic_map *bmap)
4740 int i;
4741 struct constraint *c;
4742 unsigned total;
4744 if (!bmap)
4745 return NULL;
4746 total = isl_basic_map_total_dim(bmap);
4747 c = isl_alloc_array(bmap->ctx, struct constraint, bmap->n_ineq);
4748 if (!c)
4749 goto error;
4750 for (i = 0; i < bmap->n_ineq; ++i) {
4751 c[i].size = total;
4752 c[i].c = bmap->ineq[i];
4754 qsort(c, bmap->n_ineq, sizeof(struct constraint), qsort_constraint_cmp);
4755 for (i = 0; i < bmap->n_ineq; ++i)
4756 bmap->ineq[i] = c[i].c;
4757 free(c);
4758 return bmap;
4759 error:
4760 isl_basic_map_free(bmap);
4761 return NULL;
4764 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
4766 if (!bmap)
4767 return NULL;
4768 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
4769 return bmap;
4770 bmap = isl_basic_map_convex_hull(bmap);
4771 bmap = isl_basic_map_sort_constraints(bmap);
4772 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
4773 return bmap;
4776 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
4778 return (struct isl_basic_set *)isl_basic_map_normalize(
4779 (struct isl_basic_map *)bset);
4782 static int isl_basic_map_fast_cmp(const struct isl_basic_map *bmap1,
4783 const struct isl_basic_map *bmap2)
4785 int i, cmp;
4786 unsigned total;
4788 if (bmap1 == bmap2)
4789 return 0;
4790 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
4791 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
4792 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
4793 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
4794 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
4795 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
4796 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
4797 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
4798 return 0;
4799 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
4800 return 1;
4801 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
4802 return -1;
4803 if (bmap1->n_eq != bmap2->n_eq)
4804 return bmap1->n_eq - bmap2->n_eq;
4805 if (bmap1->n_ineq != bmap2->n_ineq)
4806 return bmap1->n_ineq - bmap2->n_ineq;
4807 if (bmap1->n_div != bmap2->n_div)
4808 return bmap1->n_div - bmap2->n_div;
4809 total = isl_basic_map_total_dim(bmap1);
4810 for (i = 0; i < bmap1->n_eq; ++i) {
4811 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
4812 if (cmp)
4813 return cmp;
4815 for (i = 0; i < bmap1->n_ineq; ++i) {
4816 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
4817 if (cmp)
4818 return cmp;
4820 for (i = 0; i < bmap1->n_div; ++i) {
4821 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
4822 if (cmp)
4823 return cmp;
4825 return 0;
4828 static int isl_basic_map_fast_is_equal(struct isl_basic_map *bmap1,
4829 struct isl_basic_map *bmap2)
4831 return isl_basic_map_fast_cmp(bmap1, bmap2) == 0;
4834 static int qsort_bmap_cmp(const void *p1, const void *p2)
4836 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
4837 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
4839 return isl_basic_map_fast_cmp(bmap1, bmap2);
4842 /* We normalize in place, but if anything goes wrong we need
4843 * to return NULL, so we need to make sure we don't change the
4844 * meaning of any possible other copies of map.
4846 struct isl_map *isl_map_normalize(struct isl_map *map)
4848 int i, j;
4849 struct isl_basic_map *bmap;
4851 if (!map)
4852 return NULL;
4853 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
4854 return map;
4855 for (i = 0; i < map->n; ++i) {
4856 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
4857 if (!bmap)
4858 goto error;
4859 isl_basic_map_free(map->p[i]);
4860 map->p[i] = bmap;
4862 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
4863 ISL_F_SET(map, ISL_MAP_NORMALIZED);
4864 map = isl_map_remove_empty_parts(map);
4865 if (!map)
4866 return NULL;
4867 for (i = map->n - 1; i >= 1; --i) {
4868 if (!isl_basic_map_fast_is_equal(map->p[i-1], map->p[i]))
4869 continue;
4870 isl_basic_map_free(map->p[i-1]);
4871 for (j = i; j < map->n; ++j)
4872 map->p[j-1] = map->p[j];
4873 map->n--;
4875 return map;
4876 error:
4877 isl_map_free(map);
4878 return NULL;
4882 struct isl_set *isl_set_normalize(struct isl_set *set)
4884 return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
4887 int isl_map_fast_is_equal(struct isl_map *map1, struct isl_map *map2)
4889 int i;
4890 int equal;
4892 if (!map1 || !map2)
4893 return -1;
4895 if (map1 == map2)
4896 return 1;
4897 if (!isl_dim_equal(map1->dim, map2->dim))
4898 return 0;
4900 map1 = isl_map_copy(map1);
4901 map2 = isl_map_copy(map2);
4902 map1 = isl_map_normalize(map1);
4903 map2 = isl_map_normalize(map2);
4904 if (!map1 || !map2)
4905 goto error;
4906 equal = map1->n == map2->n;
4907 for (i = 0; equal && i < map1->n; ++i) {
4908 equal = isl_basic_map_fast_is_equal(map1->p[i], map2->p[i]);
4909 if (equal < 0)
4910 goto error;
4912 isl_map_free(map1);
4913 isl_map_free(map2);
4914 return equal;
4915 error:
4916 isl_map_free(map1);
4917 isl_map_free(map2);
4918 return -1;
4921 int isl_set_fast_is_equal(struct isl_set *set1, struct isl_set *set2)
4923 return isl_map_fast_is_equal((struct isl_map *)set1,
4924 (struct isl_map *)set2);
4927 /* Return an interval that ranges from min to max (inclusive)
4929 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
4930 isl_int min, isl_int max)
4932 int k;
4933 struct isl_basic_set *bset = NULL;
4935 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
4936 if (!bset)
4937 goto error;
4939 k = isl_basic_set_alloc_inequality(bset);
4940 if (k < 0)
4941 goto error;
4942 isl_int_set_si(bset->ineq[k][1], 1);
4943 isl_int_neg(bset->ineq[k][0], min);
4945 k = isl_basic_set_alloc_inequality(bset);
4946 if (k < 0)
4947 goto error;
4948 isl_int_set_si(bset->ineq[k][1], -1);
4949 isl_int_set(bset->ineq[k][0], max);
4951 return bset;
4952 error:
4953 isl_basic_set_free(bset);
4954 return NULL;
4957 /* Return the Cartesian product of the basic sets in list (in the given order).
4959 struct isl_basic_set *isl_basic_set_product(struct isl_basic_set_list *list)
4961 int i;
4962 unsigned dim;
4963 unsigned nparam;
4964 unsigned extra;
4965 unsigned n_eq;
4966 unsigned n_ineq;
4967 struct isl_basic_set *product = NULL;
4969 if (!list)
4970 goto error;
4971 isl_assert(list->ctx, list->n > 0, goto error);
4972 isl_assert(list->ctx, list->p[0], goto error);
4973 nparam = isl_basic_set_n_param(list->p[0]);
4974 dim = isl_basic_set_n_dim(list->p[0]);
4975 extra = list->p[0]->n_div;
4976 n_eq = list->p[0]->n_eq;
4977 n_ineq = list->p[0]->n_ineq;
4978 for (i = 1; i < list->n; ++i) {
4979 isl_assert(list->ctx, list->p[i], goto error);
4980 isl_assert(list->ctx,
4981 nparam == isl_basic_set_n_param(list->p[i]), goto error);
4982 dim += isl_basic_set_n_dim(list->p[i]);
4983 extra += list->p[i]->n_div;
4984 n_eq += list->p[i]->n_eq;
4985 n_ineq += list->p[i]->n_ineq;
4987 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
4988 n_eq, n_ineq);
4989 if (!product)
4990 goto error;
4991 dim = 0;
4992 for (i = 0; i < list->n; ++i) {
4993 isl_basic_set_add_constraints(product,
4994 isl_basic_set_copy(list->p[i]), dim);
4995 dim += isl_basic_set_n_dim(list->p[i]);
4997 isl_basic_set_list_free(list);
4998 return product;
4999 error:
5000 isl_basic_set_free(product);
5001 isl_basic_set_list_free(list);
5002 return NULL;
5005 struct isl_basic_map *isl_basic_map_product(
5006 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
5008 struct isl_dim *dim_result = NULL;
5009 struct isl_basic_map *bmap;
5010 unsigned in1, in2, out1, out2, nparam, total, pos;
5011 struct isl_dim_map *dim_map1, *dim_map2;
5013 if (!bmap1 || !bmap2)
5014 goto error;
5016 isl_assert(bmap1->ctx, isl_dim_match(bmap1->dim, isl_dim_param,
5017 bmap2->dim, isl_dim_param), goto error);
5018 dim_result = isl_dim_product(isl_dim_copy(bmap1->dim),
5019 isl_dim_copy(bmap2->dim));
5021 in1 = isl_basic_map_n_in(bmap1);
5022 in2 = isl_basic_map_n_in(bmap2);
5023 out1 = isl_basic_map_n_out(bmap1);
5024 out2 = isl_basic_map_n_out(bmap2);
5025 nparam = isl_basic_map_n_param(bmap1);
5027 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
5028 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
5029 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
5030 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
5031 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
5032 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
5033 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
5034 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
5035 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
5036 isl_dim_map_div(dim_map1, bmap1, pos += out2);
5037 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
5039 bmap = isl_basic_map_alloc_dim(dim_result,
5040 bmap1->n_div + bmap2->n_div,
5041 bmap1->n_eq + bmap2->n_eq,
5042 bmap1->n_ineq + bmap2->n_ineq);
5043 bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
5044 bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
5045 bmap = isl_basic_map_simplify(bmap);
5046 return isl_basic_map_finalize(bmap);
5047 error:
5048 isl_basic_map_free(bmap1);
5049 isl_basic_map_free(bmap2);
5050 return NULL;
5053 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
5055 struct isl_map *isl_map_product(struct isl_map *map1, struct isl_map *map2)
5057 unsigned flags = 0;
5058 struct isl_map *result;
5059 int i, j;
5061 if (!map1 || !map2)
5062 goto error;
5064 isl_assert(map1->ctx, isl_dim_match(map1->dim, isl_dim_param,
5065 map2->dim, isl_dim_param), goto error);
5067 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
5068 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
5069 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
5071 result = isl_map_alloc_dim(isl_dim_product(isl_dim_copy(map1->dim),
5072 isl_dim_copy(map2->dim)),
5073 map1->n * map2->n, flags);
5074 if (!result)
5075 goto error;
5076 for (i = 0; i < map1->n; ++i)
5077 for (j = 0; j < map2->n; ++j) {
5078 struct isl_basic_map *part;
5079 part = isl_basic_map_product(
5080 isl_basic_map_copy(map1->p[i]),
5081 isl_basic_map_copy(map2->p[j]));
5082 if (isl_basic_map_is_empty(part))
5083 isl_basic_map_free(part);
5084 else
5085 result = isl_map_add(result, part);
5086 if (!result)
5087 goto error;
5089 isl_map_free(map1);
5090 isl_map_free(map2);
5091 return result;
5092 error:
5093 isl_map_free(map1);
5094 isl_map_free(map2);
5095 return NULL;
5098 /* Given two set A and B, construct its Cartesian product A x B.
5100 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
5102 return (struct isl_set *)isl_map_product((struct isl_map *)set1,
5103 (struct isl_map *)set2);
5106 uint32_t isl_basic_set_get_hash(struct isl_basic_set *bset)
5108 int i;
5109 uint32_t hash;
5110 unsigned total;
5112 if (!bset)
5113 return 0;
5114 bset = isl_basic_set_copy(bset);
5115 bset = isl_basic_set_normalize(bset);
5116 if (!bset)
5117 return 0;
5118 total = isl_basic_set_total_dim(bset);
5119 isl_hash_byte(hash, bset->n_eq & 0xFF);
5120 for (i = 0; i < bset->n_eq; ++i) {
5121 uint32_t c_hash;
5122 c_hash = isl_seq_get_hash(bset->eq[i], 1 + total);
5123 isl_hash_hash(hash, c_hash);
5125 isl_hash_byte(hash, bset->n_ineq & 0xFF);
5126 for (i = 0; i < bset->n_ineq; ++i) {
5127 uint32_t c_hash;
5128 c_hash = isl_seq_get_hash(bset->ineq[i], 1 + total);
5129 isl_hash_hash(hash, c_hash);
5131 isl_hash_byte(hash, bset->n_div & 0xFF);
5132 for (i = 0; i < bset->n_div; ++i) {
5133 uint32_t c_hash;
5134 if (isl_int_is_zero(bset->div[i][0]))
5135 continue;
5136 isl_hash_byte(hash, i & 0xFF);
5137 c_hash = isl_seq_get_hash(bset->div[i], 1 + 1 + total);
5138 isl_hash_hash(hash, c_hash);
5140 isl_basic_set_free(bset);
5141 return hash;
5144 uint32_t isl_set_get_hash(struct isl_set *set)
5146 int i;
5147 uint32_t hash;
5149 if (!set)
5150 return 0;
5151 set = isl_set_copy(set);
5152 set = isl_set_normalize(set);
5153 if (!set)
5154 return 0;
5156 hash = isl_hash_init();
5157 for (i = 0; i < set->n; ++i) {
5158 uint32_t bset_hash;
5159 bset_hash = isl_basic_set_get_hash(set->p[i]);
5160 isl_hash_hash(hash, bset_hash);
5163 isl_set_free(set);
5165 return hash;
5168 /* Check if the value for dimension dim is completely determined
5169 * by the values of the other parameters and variables.
5170 * That is, check if dimension dim is involved in an equality.
5172 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
5174 int i;
5175 unsigned nparam;
5177 if (!bset)
5178 return -1;
5179 nparam = isl_basic_set_n_param(bset);
5180 for (i = 0; i < bset->n_eq; ++i)
5181 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
5182 return 1;
5183 return 0;
5186 /* Check if the value for dimension dim is completely determined
5187 * by the values of the other parameters and variables.
5188 * That is, check if dimension dim is involved in an equality
5189 * for each of the subsets.
5191 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
5193 int i;
5195 if (!set)
5196 return -1;
5197 for (i = 0; i < set->n; ++i) {
5198 int unique;
5199 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
5200 if (unique != 1)
5201 return unique;
5203 return 1;