isl_basic_map_drop_redundant_divs: also investigate divs that have a definition
[isl.git] / isl_map.c
blob2b17f9120d06eebedd8ea6e3a5e12b9f3ca349bf
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 * 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(set->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(ctx,
1974 isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
1975 isl_assert(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 a basic map A -> f(A), construct A -> -f(A).
2049 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
2051 int i, j;
2052 unsigned off, n;
2054 bmap = isl_basic_map_cow(bmap);
2055 if (!bmap)
2056 return NULL;
2058 n = isl_basic_map_dim(bmap, isl_dim_out);
2059 off = isl_basic_map_offset(bmap, isl_dim_out);
2060 for (i = 0; i < bmap->n_eq; ++i)
2061 for (j = 0; j < n; ++j)
2062 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
2063 for (i = 0; i < bmap->n_ineq; ++i)
2064 for (j = 0; j < n; ++j)
2065 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
2066 for (i = 0; i < bmap->n_div; ++i)
2067 for (j = 0; j < n; ++j)
2068 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
2069 return isl_basic_map_finalize(bmap);
2072 /* Given a basic map A -> f(A) and an integer d, construct a basic map
2073 * A -> floor(f(A)/d).
2075 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
2076 isl_int d)
2078 unsigned n_in, n_out, nparam, total, pos;
2079 struct isl_basic_map *result = NULL;
2080 struct isl_dim_map *dim_map;
2081 int i;
2083 if (!bmap)
2084 return NULL;
2086 nparam = isl_basic_map_n_param(bmap);
2087 n_in = isl_basic_map_n_in(bmap);
2088 n_out = isl_basic_map_n_out(bmap);
2090 total = nparam + n_in + n_out + bmap->n_div + n_out;
2091 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2092 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
2093 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
2094 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
2095 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
2097 result = isl_basic_map_alloc_dim(isl_dim_copy(bmap->dim),
2098 bmap->n_div + n_out,
2099 bmap->n_eq, bmap->n_ineq + 2 * n_out);
2100 result = add_constraints_dim_map(result, bmap, dim_map);
2101 result = add_divs(result, n_out);
2102 for (i = 0; i < n_out; ++i) {
2103 int j;
2104 j = isl_basic_map_alloc_inequality(result);
2105 if (j < 0)
2106 goto error;
2107 isl_seq_clr(result->ineq[j], 1+total);
2108 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
2109 isl_int_set_si(result->ineq[j][1+pos+i], 1);
2110 j = isl_basic_map_alloc_inequality(result);
2111 if (j < 0)
2112 goto error;
2113 isl_seq_clr(result->ineq[j], 1+total);
2114 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
2115 isl_int_set_si(result->ineq[j][1+pos+i], -1);
2116 isl_int_sub_ui(result->ineq[j][0], d, 1);
2119 result = isl_basic_map_simplify(result);
2120 return isl_basic_map_finalize(result);
2121 error:
2122 isl_basic_map_free(result);
2123 return NULL;
2126 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
2128 int i;
2129 unsigned nparam;
2130 unsigned n_in;
2132 i = isl_basic_map_alloc_equality(bmap);
2133 if (i < 0)
2134 goto error;
2135 nparam = isl_basic_map_n_param(bmap);
2136 n_in = isl_basic_map_n_in(bmap);
2137 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
2138 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
2139 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
2140 return isl_basic_map_finalize(bmap);
2141 error:
2142 isl_basic_map_free(bmap);
2143 return NULL;
2146 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
2148 int i;
2149 unsigned nparam;
2150 unsigned n_in;
2152 i = isl_basic_map_alloc_inequality(bmap);
2153 if (i < 0)
2154 goto error;
2155 nparam = isl_basic_map_n_param(bmap);
2156 n_in = isl_basic_map_n_in(bmap);
2157 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
2158 isl_int_set_si(bmap->ineq[i][0], -1);
2159 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
2160 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
2161 return isl_basic_map_finalize(bmap);
2162 error:
2163 isl_basic_map_free(bmap);
2164 return NULL;
2167 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
2169 int i;
2170 unsigned nparam;
2171 unsigned n_in;
2173 i = isl_basic_map_alloc_inequality(bmap);
2174 if (i < 0)
2175 goto error;
2176 nparam = isl_basic_map_n_param(bmap);
2177 n_in = isl_basic_map_n_in(bmap);
2178 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
2179 isl_int_set_si(bmap->ineq[i][0], -1);
2180 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
2181 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
2182 return isl_basic_map_finalize(bmap);
2183 error:
2184 isl_basic_map_free(bmap);
2185 return NULL;
2188 struct isl_basic_map *isl_basic_map_equal(struct isl_dim *dim, unsigned n_equal)
2190 int i;
2191 struct isl_basic_map *bmap;
2192 bmap = isl_basic_map_alloc_dim(dim, 0, n_equal, 0);
2193 if (!bmap)
2194 return NULL;
2195 for (i = 0; i < n_equal && bmap; ++i)
2196 bmap = var_equal(bmap, i);
2197 return isl_basic_map_finalize(bmap);
2200 struct isl_basic_map *isl_basic_map_less_at(struct isl_dim *dim, unsigned pos)
2202 int i;
2203 struct isl_basic_map *bmap;
2204 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
2205 if (!bmap)
2206 return NULL;
2207 for (i = 0; i < pos && bmap; ++i)
2208 bmap = var_equal(bmap, i);
2209 if (bmap)
2210 bmap = var_less(bmap, pos);
2211 return isl_basic_map_finalize(bmap);
2214 struct isl_basic_map *isl_basic_map_more_at(struct isl_dim *dim, unsigned pos)
2216 int i;
2217 struct isl_basic_map *bmap;
2218 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
2219 if (!bmap)
2220 return NULL;
2221 for (i = 0; i < pos && bmap; ++i)
2222 bmap = var_equal(bmap, i);
2223 if (bmap)
2224 bmap = var_more(bmap, pos);
2225 return isl_basic_map_finalize(bmap);
2228 struct isl_basic_map *isl_basic_map_from_basic_set(
2229 struct isl_basic_set *bset, struct isl_dim *dim)
2231 struct isl_basic_map *bmap;
2233 bset = isl_basic_set_cow(bset);
2234 if (!bset || !dim)
2235 goto error;
2237 isl_assert(bset->ctx, isl_dim_compatible(bset->dim, dim), goto error);
2238 isl_dim_free(bset->dim);
2239 bmap = (struct isl_basic_map *) bset;
2240 bmap->dim = dim;
2241 return isl_basic_map_finalize(bmap);
2242 error:
2243 isl_basic_set_free(bset);
2244 isl_dim_free(dim);
2245 return NULL;
2248 struct isl_basic_set *isl_basic_set_from_basic_map(struct isl_basic_map *bmap)
2250 if (!bmap)
2251 goto error;
2252 if (bmap->dim->n_in == 0)
2253 return (struct isl_basic_set *)bmap;
2254 bmap = isl_basic_map_cow(bmap);
2255 if (!bmap)
2256 goto error;
2257 bmap->dim = isl_dim_cow(bmap->dim);
2258 if (!bmap->dim)
2259 goto error;
2260 bmap->dim->n_out += bmap->dim->n_in;
2261 bmap->dim->n_in = 0;
2262 bmap = isl_basic_map_finalize(bmap);
2263 return (struct isl_basic_set *)bmap;
2264 error:
2265 isl_basic_map_free(bmap);
2266 return NULL;
2269 /* For a div d = floor(f/m), add the constraints
2271 * f - m d >= 0
2272 * -(f-(n-1)) + m d >= 0
2274 * Note that the second constraint is the negation of
2276 * f - m d >= n
2278 static int add_div_constraints(struct isl_basic_map *bmap, unsigned div)
2280 int i, j;
2281 unsigned total = isl_basic_map_total_dim(bmap);
2282 unsigned div_pos = 1 + total - bmap->n_div + div;
2284 i = isl_basic_map_alloc_inequality(bmap);
2285 if (i < 0)
2286 return -1;
2287 isl_seq_cpy(bmap->ineq[i], bmap->div[div]+1, 1+total);
2288 isl_int_neg(bmap->ineq[i][div_pos], bmap->div[div][0]);
2290 j = isl_basic_map_alloc_inequality(bmap);
2291 if (j < 0)
2292 return -1;
2293 isl_seq_neg(bmap->ineq[j], bmap->ineq[i], 1 + total);
2294 isl_int_add(bmap->ineq[j][0], bmap->ineq[j][0], bmap->ineq[j][div_pos]);
2295 isl_int_sub_ui(bmap->ineq[j][0], bmap->ineq[j][0], 1);
2296 return j;
2299 struct isl_basic_set *isl_basic_map_underlying_set(
2300 struct isl_basic_map *bmap)
2302 if (!bmap)
2303 goto error;
2304 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 && bmap->n_div == 0)
2305 return (struct isl_basic_set *)bmap;
2306 bmap = isl_basic_map_cow(bmap);
2307 if (!bmap)
2308 goto error;
2309 bmap->dim = isl_dim_underlying(bmap->dim, bmap->n_div);
2310 if (!bmap->dim)
2311 goto error;
2312 bmap->extra -= bmap->n_div;
2313 bmap->n_div = 0;
2314 bmap = isl_basic_map_finalize(bmap);
2315 return (struct isl_basic_set *)bmap;
2316 error:
2317 return NULL;
2320 struct isl_basic_map *isl_basic_map_overlying_set(
2321 struct isl_basic_set *bset, struct isl_basic_map *like)
2323 struct isl_basic_map *bmap;
2324 struct isl_ctx *ctx;
2325 unsigned total;
2326 int i;
2328 if (!bset || !like)
2329 goto error;
2330 ctx = bset->ctx;
2331 isl_assert(ctx, bset->n_div == 0, goto error);
2332 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
2333 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
2334 goto error);
2335 if (isl_dim_equal(bset->dim, like->dim) && like->n_div == 0) {
2336 isl_basic_map_free(like);
2337 return (struct isl_basic_map *)bset;
2339 bset = isl_basic_set_cow(bset);
2340 if (!bset)
2341 goto error;
2342 total = bset->dim->n_out + bset->extra;
2343 bmap = (struct isl_basic_map *)bset;
2344 isl_dim_free(bmap->dim);
2345 bmap->dim = isl_dim_copy(like->dim);
2346 if (!bmap->dim)
2347 goto error;
2348 bmap->n_div = like->n_div;
2349 bmap->extra += like->n_div;
2350 if (bmap->extra) {
2351 unsigned ltotal;
2352 ltotal = total - bmap->extra + like->extra;
2353 if (ltotal > total)
2354 ltotal = total;
2355 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
2356 bmap->extra * (1 + 1 + total));
2357 if (isl_blk_is_error(bmap->block2))
2358 goto error;
2359 bmap->div = isl_realloc_array(ctx, bmap->div, isl_int *,
2360 bmap->extra);
2361 if (!bmap->div)
2362 goto error;
2363 for (i = 0; i < bmap->extra; ++i)
2364 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
2365 for (i = 0; i < like->n_div; ++i) {
2366 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
2367 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
2369 bmap = isl_basic_map_extend_constraints(bmap,
2370 0, 2 * like->n_div);
2371 for (i = 0; i < like->n_div; ++i) {
2372 if (isl_int_is_zero(bmap->div[i][0]))
2373 continue;
2374 if (add_div_constraints(bmap, i) < 0)
2375 goto error;
2378 isl_basic_map_free(like);
2379 bmap = isl_basic_map_simplify(bmap);
2380 bmap = isl_basic_map_finalize(bmap);
2381 return bmap;
2382 error:
2383 isl_basic_map_free(like);
2384 isl_basic_set_free(bset);
2385 return NULL;
2388 struct isl_basic_set *isl_basic_set_from_underlying_set(
2389 struct isl_basic_set *bset, struct isl_basic_set *like)
2391 return (struct isl_basic_set *)
2392 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
2395 struct isl_set *isl_set_from_underlying_set(
2396 struct isl_set *set, struct isl_basic_set *like)
2398 int i;
2400 if (!set || !like)
2401 goto error;
2402 isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
2403 goto error);
2404 if (isl_dim_equal(set->dim, like->dim) && like->n_div == 0) {
2405 isl_basic_set_free(like);
2406 return set;
2408 set = isl_set_cow(set);
2409 if (!set)
2410 goto error;
2411 for (i = 0; i < set->n; ++i) {
2412 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
2413 isl_basic_set_copy(like));
2414 if (!set->p[i])
2415 goto error;
2417 isl_dim_free(set->dim);
2418 set->dim = isl_dim_copy(like->dim);
2419 if (!set->dim)
2420 goto error;
2421 isl_basic_set_free(like);
2422 return set;
2423 error:
2424 isl_basic_set_free(like);
2425 isl_set_free(set);
2426 return NULL;
2429 struct isl_set *isl_map_underlying_set(struct isl_map *map)
2431 int i;
2433 map = isl_map_cow(map);
2434 if (!map)
2435 return NULL;
2436 map->dim = isl_dim_cow(map->dim);
2437 if (!map->dim)
2438 goto error;
2440 for (i = 1; i < map->n; ++i)
2441 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
2442 goto error);
2443 for (i = 0; i < map->n; ++i) {
2444 map->p[i] = (struct isl_basic_map *)
2445 isl_basic_map_underlying_set(map->p[i]);
2446 if (!map->p[i])
2447 goto error;
2449 if (map->n == 0)
2450 map->dim = isl_dim_underlying(map->dim, 0);
2451 else {
2452 isl_dim_free(map->dim);
2453 map->dim = isl_dim_copy(map->p[0]->dim);
2455 if (!map->dim)
2456 goto error;
2457 return (struct isl_set *)map;
2458 error:
2459 isl_map_free(map);
2460 return NULL;
2463 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
2465 return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
2468 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
2470 struct isl_basic_set *domain;
2471 unsigned n_out;
2472 if (!bmap)
2473 return NULL;
2474 n_out = isl_basic_map_n_out(bmap);
2475 domain = isl_basic_set_from_basic_map(bmap);
2476 return isl_basic_set_project_out(domain, n_out, 0);
2479 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
2481 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
2484 struct isl_set *isl_map_range(struct isl_map *map)
2486 int i;
2487 struct isl_set *set;
2489 if (!map)
2490 goto error;
2491 map = isl_map_cow(map);
2492 if (!map)
2493 goto error;
2495 set = (struct isl_set *) map;
2496 if (set->dim->n_in != 0) {
2497 set->dim = isl_dim_drop_inputs(set->dim, 0, set->dim->n_in);
2498 if (!set->dim)
2499 goto error;
2501 for (i = 0; i < map->n; ++i) {
2502 set->p[i] = isl_basic_map_range(map->p[i]);
2503 if (!set->p[i])
2504 goto error;
2506 ISL_F_CLR(set, ISL_MAP_DISJOINT);
2507 ISL_F_CLR(set, ISL_SET_NORMALIZED);
2508 return set;
2509 error:
2510 isl_map_free(map);
2511 return NULL;
2514 struct isl_map *isl_map_from_set(struct isl_set *set, struct isl_dim *dim)
2516 int i;
2517 struct isl_map *map = NULL;
2519 set = isl_set_cow(set);
2520 if (!set || !dim)
2521 goto error;
2522 isl_assert(set->ctx, isl_dim_compatible(set->dim, dim), goto error);
2523 map = (struct isl_map *)set;
2524 for (i = 0; i < set->n; ++i) {
2525 map->p[i] = isl_basic_map_from_basic_set(
2526 set->p[i], isl_dim_copy(dim));
2527 if (!map->p[i])
2528 goto error;
2530 isl_dim_free(map->dim);
2531 map->dim = dim;
2532 return map;
2533 error:
2534 isl_dim_free(dim);
2535 isl_set_free(set);
2536 return NULL;
2539 struct isl_map *isl_map_from_range(struct isl_set *set)
2541 return (struct isl_map *)set;
2544 struct isl_set *isl_set_from_map(struct isl_map *map)
2546 int i;
2547 struct isl_set *set = NULL;
2549 if (!map)
2550 return NULL;
2551 map = isl_map_cow(map);
2552 if (!map)
2553 return NULL;
2554 map->dim = isl_dim_cow(map->dim);
2555 if (!map->dim)
2556 goto error;
2557 map->dim->n_out += map->dim->n_in;
2558 map->dim->n_in = 0;
2559 set = (struct isl_set *)map;
2560 for (i = 0; i < map->n; ++i) {
2561 set->p[i] = isl_basic_set_from_basic_map(map->p[i]);
2562 if (!set->p[i])
2563 goto error;
2565 return set;
2566 error:
2567 isl_map_free(map);
2568 return NULL;
2571 struct isl_map *isl_map_alloc_dim(struct isl_dim *dim, int n, unsigned flags)
2573 struct isl_map *map;
2575 if (!dim)
2576 return NULL;
2577 isl_assert(dim->ctx, n >= 0, return NULL);
2578 map = isl_alloc(dim->ctx, struct isl_map,
2579 sizeof(struct isl_map) +
2580 n * sizeof(struct isl_basic_map *));
2581 if (!map)
2582 goto error;
2584 map->ctx = dim->ctx;
2585 isl_ctx_ref(map->ctx);
2586 map->ref = 1;
2587 map->size = n;
2588 map->n = 0;
2589 map->dim = dim;
2590 map->flags = flags;
2591 return map;
2592 error:
2593 isl_dim_free(dim);
2594 return NULL;
2597 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
2598 unsigned nparam, unsigned in, unsigned out, int n,
2599 unsigned flags)
2601 struct isl_map *map;
2602 struct isl_dim *dims;
2604 dims = isl_dim_alloc(ctx, nparam, in, out);
2605 if (!dims)
2606 return NULL;
2608 map = isl_map_alloc_dim(dims, n, flags);
2609 return map;
2612 struct isl_basic_map *isl_basic_map_empty(struct isl_ctx *ctx,
2613 unsigned nparam, unsigned in, unsigned out)
2615 struct isl_basic_map *bmap;
2616 bmap = isl_basic_map_alloc(ctx, nparam, in, out, 0, 1, 0);
2617 bmap = isl_basic_map_set_to_empty(bmap);
2618 return bmap;
2621 struct isl_basic_set *isl_basic_set_empty(struct isl_dim *dim)
2623 struct isl_basic_set *bset;
2624 bset = isl_basic_set_alloc_dim(dim, 0, 1, 0);
2625 bset = isl_basic_set_set_to_empty(bset);
2626 return bset;
2629 struct isl_basic_map *isl_basic_map_empty_like(struct isl_basic_map *model)
2631 struct isl_basic_map *bmap;
2632 if (!model)
2633 return NULL;
2634 bmap = isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
2635 bmap = isl_basic_map_set_to_empty(bmap);
2636 return bmap;
2639 struct isl_basic_map *isl_basic_map_empty_like_map(struct isl_map *model)
2641 struct isl_basic_map *bmap;
2642 if (!model)
2643 return NULL;
2644 bmap = isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
2645 bmap = isl_basic_map_set_to_empty(bmap);
2646 return bmap;
2649 struct isl_basic_set *isl_basic_set_empty_like(struct isl_basic_set *model)
2651 struct isl_basic_set *bset;
2652 if (!model)
2653 return NULL;
2654 bset = isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
2655 bset = isl_basic_set_set_to_empty(bset);
2656 return bset;
2659 struct isl_basic_map *isl_basic_map_universe(struct isl_dim *dim)
2661 struct isl_basic_map *bmap;
2662 bmap = isl_basic_map_alloc_dim(dim, 0, 0, 0);
2663 return bmap;
2666 struct isl_basic_set *isl_basic_set_universe(struct isl_dim *dim)
2668 struct isl_basic_set *bset;
2669 bset = isl_basic_set_alloc_dim(dim, 0, 0, 0);
2670 return bset;
2673 struct isl_basic_set *isl_basic_set_universe_like(struct isl_basic_set *model)
2675 if (!model)
2676 return NULL;
2677 return isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
2680 struct isl_map *isl_map_empty(struct isl_dim *dim)
2682 return isl_map_alloc_dim(dim, 0, ISL_MAP_DISJOINT);
2685 struct isl_map *isl_map_empty_like(struct isl_map *model)
2687 if (!model)
2688 return NULL;
2689 return isl_map_alloc_dim(isl_dim_copy(model->dim), 0, ISL_MAP_DISJOINT);
2692 struct isl_map *isl_map_empty_like_basic_map(struct isl_basic_map *model)
2694 if (!model)
2695 return NULL;
2696 return isl_map_alloc_dim(isl_dim_copy(model->dim), 0, ISL_MAP_DISJOINT);
2699 struct isl_set *isl_set_empty(struct isl_dim *dim)
2701 return isl_set_alloc_dim(dim, 0, ISL_MAP_DISJOINT);
2704 struct isl_set *isl_set_empty_like(struct isl_set *model)
2706 if (!model)
2707 return NULL;
2708 return isl_set_empty(isl_dim_copy(model->dim));
2711 struct isl_map *isl_map_universe(struct isl_dim *dim)
2713 struct isl_map *map;
2714 if (!dim)
2715 return NULL;
2716 map = isl_map_alloc_dim(isl_dim_copy(dim), 1, ISL_MAP_DISJOINT);
2717 map = isl_map_add(map, isl_basic_map_universe(dim));
2718 return map;
2721 struct isl_set *isl_set_universe(struct isl_dim *dim)
2723 struct isl_set *set;
2724 if (!dim)
2725 return NULL;
2726 set = isl_set_alloc_dim(isl_dim_copy(dim), 1, ISL_MAP_DISJOINT);
2727 set = isl_set_add(set, isl_basic_set_universe(dim));
2728 return set;
2731 struct isl_map *isl_map_dup(struct isl_map *map)
2733 int i;
2734 struct isl_map *dup;
2736 if (!map)
2737 return NULL;
2738 dup = isl_map_alloc_dim(isl_dim_copy(map->dim), map->n, map->flags);
2739 for (i = 0; i < map->n; ++i)
2740 dup = isl_map_add(dup, isl_basic_map_copy(map->p[i]));
2741 return dup;
2744 struct isl_map *isl_map_add(struct isl_map *map, struct isl_basic_map *bmap)
2746 if (!bmap || !map)
2747 goto error;
2748 if (isl_basic_map_fast_is_empty(bmap)) {
2749 isl_basic_map_free(bmap);
2750 return map;
2752 isl_assert(map->ctx, isl_dim_equal(map->dim, bmap->dim), goto error);
2753 isl_assert(map->ctx, map->n < map->size, goto error);
2754 map->p[map->n] = bmap;
2755 map->n++;
2756 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
2757 return map;
2758 error:
2759 if (map)
2760 isl_map_free(map);
2761 if (bmap)
2762 isl_basic_map_free(bmap);
2763 return NULL;
2766 void isl_map_free(struct isl_map *map)
2768 int i;
2770 if (!map)
2771 return;
2773 if (--map->ref > 0)
2774 return;
2776 isl_ctx_deref(map->ctx);
2777 for (i = 0; i < map->n; ++i)
2778 isl_basic_map_free(map->p[i]);
2779 isl_dim_free(map->dim);
2780 free(map);
2783 struct isl_map *isl_map_extend(struct isl_map *base,
2784 unsigned nparam, unsigned n_in, unsigned n_out)
2786 int i;
2788 base = isl_map_cow(base);
2789 if (!base)
2790 return NULL;
2792 base->dim = isl_dim_extend(base->dim, nparam, n_in, n_out);
2793 if (!base->dim)
2794 goto error;
2795 for (i = 0; i < base->n; ++i) {
2796 base->p[i] = isl_basic_map_extend_dim(base->p[i],
2797 isl_dim_copy(base->dim), 0, 0, 0);
2798 if (!base->p[i])
2799 goto error;
2801 return base;
2802 error:
2803 isl_map_free(base);
2804 return NULL;
2807 struct isl_set *isl_set_extend(struct isl_set *base,
2808 unsigned nparam, unsigned dim)
2810 return (struct isl_set *)isl_map_extend((struct isl_map *)base,
2811 nparam, 0, dim);
2814 static struct isl_basic_map *isl_basic_map_fix_pos(struct isl_basic_map *bmap,
2815 unsigned pos, int value)
2817 int j;
2819 bmap = isl_basic_map_cow(bmap);
2820 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
2821 j = isl_basic_map_alloc_equality(bmap);
2822 if (j < 0)
2823 goto error;
2824 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
2825 isl_int_set_si(bmap->eq[j][pos], -1);
2826 isl_int_set_si(bmap->eq[j][0], value);
2827 bmap = isl_basic_map_simplify(bmap);
2828 return isl_basic_map_finalize(bmap);
2829 error:
2830 isl_basic_map_free(bmap);
2831 return NULL;
2834 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
2835 enum isl_dim_type type, unsigned pos, int value)
2837 if (!bmap)
2838 return NULL;
2839 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
2840 return isl_basic_map_fix_pos(bmap, isl_basic_map_offset(bmap, type) + pos,
2841 value);
2842 error:
2843 isl_basic_map_free(bmap);
2844 return NULL;
2847 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
2848 enum isl_dim_type type, unsigned pos, int value)
2850 return (struct isl_basic_set *)
2851 isl_basic_map_fix_si((struct isl_basic_map *)bset,
2852 type, pos, value);
2855 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
2856 unsigned input, int value)
2858 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
2861 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
2862 unsigned dim, int value)
2864 return (struct isl_basic_set *)
2865 isl_basic_map_fix_si((struct isl_basic_map *)bset,
2866 isl_dim_set, dim, value);
2869 struct isl_map *isl_map_fix_si(struct isl_map *map,
2870 enum isl_dim_type type, unsigned pos, int value)
2872 int i;
2874 map = isl_map_cow(map);
2875 if (!map)
2876 return NULL;
2878 isl_assert(ctx, pos < isl_map_dim(map, type), goto error);
2879 for (i = 0; i < map->n; ++i) {
2880 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
2881 if (!map->p[i])
2882 goto error;
2884 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
2885 return map;
2886 error:
2887 isl_map_free(map);
2888 return NULL;
2891 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
2892 unsigned input, int value)
2894 return isl_map_fix_si(map, isl_dim_in, input, value);
2897 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
2899 return (struct isl_set *)
2900 isl_map_fix_si((struct isl_map *)set, isl_dim_set, dim, value);
2903 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
2904 unsigned dim, isl_int value)
2906 int j;
2907 unsigned nparam;
2909 bset = isl_basic_set_cow(bset);
2910 bset = isl_basic_set_extend_constraints(bset, 0, 1);
2911 j = isl_basic_set_alloc_inequality(bset);
2912 if (j < 0)
2913 goto error;
2914 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
2915 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
2916 isl_int_neg(bset->ineq[j][0], value);
2917 bset = isl_basic_set_simplify(bset);
2918 return isl_basic_set_finalize(bset);
2919 error:
2920 isl_basic_set_free(bset);
2921 return NULL;
2924 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
2925 isl_int value)
2927 int i;
2929 set = isl_set_cow(set);
2930 if (!set)
2931 return NULL;
2933 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
2934 for (i = 0; i < set->n; ++i) {
2935 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
2936 if (!set->p[i])
2937 goto error;
2939 return set;
2940 error:
2941 isl_set_free(set);
2942 return NULL;
2945 struct isl_map *isl_map_reverse(struct isl_map *map)
2947 int i;
2948 unsigned t;
2950 map = isl_map_cow(map);
2951 if (!map)
2952 return NULL;
2954 map->dim = isl_dim_reverse(map->dim);
2955 if (!map->dim)
2956 goto error;
2957 for (i = 0; i < map->n; ++i) {
2958 map->p[i] = isl_basic_map_reverse(map->p[i]);
2959 if (!map->p[i])
2960 goto error;
2962 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
2963 return map;
2964 error:
2965 isl_map_free(map);
2966 return NULL;
2969 static struct isl_map *isl_basic_map_partial_lexopt(
2970 struct isl_basic_map *bmap, struct isl_basic_set *dom,
2971 struct isl_set **empty, int max)
2973 if (!bmap)
2974 goto error;
2975 if (bmap->ctx->pip == ISL_PIP_PIP)
2976 return isl_pip_basic_map_lexopt(bmap, dom, empty, max);
2977 else
2978 return isl_tab_basic_map_partial_lexopt(bmap, dom, empty, max);
2979 error:
2980 isl_basic_map_free(bmap);
2981 isl_basic_set_free(dom);
2982 if (empty)
2983 *empty = NULL;
2984 return NULL;
2987 struct isl_map *isl_basic_map_partial_lexmax(
2988 struct isl_basic_map *bmap, struct isl_basic_set *dom,
2989 struct isl_set **empty)
2991 return isl_basic_map_partial_lexopt(bmap, dom, empty, 1);
2994 struct isl_map *isl_basic_map_partial_lexmin(
2995 struct isl_basic_map *bmap, struct isl_basic_set *dom,
2996 struct isl_set **empty)
2998 return isl_basic_map_partial_lexopt(bmap, dom, empty, 0);
3001 struct isl_set *isl_basic_set_partial_lexmin(
3002 struct isl_basic_set *bset, struct isl_basic_set *dom,
3003 struct isl_set **empty)
3005 return (struct isl_set *)
3006 isl_basic_map_partial_lexmin((struct isl_basic_map *)bset,
3007 dom, empty);
3010 struct isl_set *isl_basic_set_partial_lexmax(
3011 struct isl_basic_set *bset, struct isl_basic_set *dom,
3012 struct isl_set **empty)
3014 return (struct isl_set *)
3015 isl_basic_map_partial_lexmax((struct isl_basic_map *)bset,
3016 dom, empty);
3019 struct isl_set *isl_basic_set_lexmin(struct isl_basic_set *bset)
3021 struct isl_basic_map *bmap = NULL;
3022 struct isl_basic_set *dom = NULL;
3023 struct isl_map *min;
3024 struct isl_dim *param_dim;
3026 if (!bset)
3027 goto error;
3028 bmap = isl_basic_map_from_basic_set(bset, isl_dim_copy(bset->dim));
3029 if (!bmap)
3030 goto error;
3031 param_dim = isl_dim_domain(isl_dim_copy(bmap->dim));
3032 dom = isl_basic_set_universe(param_dim);
3033 if (!dom)
3034 goto error;
3035 min = isl_basic_map_partial_lexmin(bmap, dom, NULL);
3036 return isl_map_range(min);
3037 error:
3038 isl_basic_map_free(bmap);
3039 return NULL;
3042 static struct isl_map *isl_map_reset_dim(struct isl_map *map,
3043 struct isl_dim *dim)
3045 int i;
3047 if (!map || !dim)
3048 goto error;
3050 for (i = 0; i < map->n; ++i) {
3051 isl_dim_free(map->p[i]->dim);
3052 map->p[i]->dim = isl_dim_copy(dim);
3054 isl_dim_free(map->dim);
3055 map->dim = dim;
3057 return map;
3058 error:
3059 isl_map_free(map);
3060 isl_dim_free(dim);
3061 return NULL;
3064 static struct isl_set *isl_set_reset_dim(struct isl_set *set,
3065 struct isl_dim *dim)
3067 return (struct isl_set *) isl_map_reset_dim((struct isl_map *)set, dim);
3070 /* Apply a preimage specified by "mat" on the parameters of "bset".
3071 * bset is assumed to have only parameters and divs.
3073 static struct isl_basic_set *basic_set_parameter_preimage(
3074 struct isl_basic_set *bset, struct isl_mat *mat)
3076 unsigned nparam;
3078 if (!bset || !mat)
3079 goto error;
3081 bset->dim = isl_dim_cow(bset->dim);
3082 if (!bset->dim)
3083 goto error;
3085 nparam = isl_basic_set_dim(bset, isl_dim_param);
3087 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
3089 bset->dim->nparam = 0;
3090 bset->dim->n_out = nparam;
3091 bset = isl_basic_set_preimage(bset, mat);
3092 if (bset) {
3093 bset->dim->nparam = bset->dim->n_out;
3094 bset->dim->n_out = 0;
3096 return bset;
3097 error:
3098 isl_mat_free(mat);
3099 isl_basic_set_free(bset);
3100 return NULL;
3103 /* Apply a preimage specified by "mat" on the parameters of "set".
3104 * set is assumed to have only parameters and divs.
3106 static struct isl_set *set_parameter_preimage(
3107 struct isl_set *set, struct isl_mat *mat)
3109 struct isl_dim *dim = NULL;
3110 unsigned nparam;
3112 if (!set || !mat)
3113 goto error;
3115 dim = isl_dim_copy(set->dim);
3116 dim = isl_dim_cow(dim);
3117 if (!dim)
3118 goto error;
3120 nparam = isl_set_dim(set, isl_dim_param);
3122 isl_assert(set->ctx, mat->n_row == 1 + nparam, goto error);
3124 dim->nparam = 0;
3125 dim->n_out = nparam;
3126 isl_set_reset_dim(set, dim);
3127 set = isl_set_preimage(set, mat);
3128 if (!set)
3129 goto error2;
3130 dim = isl_dim_copy(set->dim);
3131 dim = isl_dim_cow(dim);
3132 if (!dim)
3133 goto error2;
3134 dim->nparam = dim->n_out;
3135 dim->n_out = 0;
3136 isl_set_reset_dim(set, dim);
3137 return set;
3138 error:
3139 isl_dim_free(dim);
3140 isl_mat_free(mat);
3141 error2:
3142 isl_set_free(set);
3143 return NULL;
3146 /* Intersect the basic set "bset" with the affine space specified by the
3147 * equalities in "eq".
3149 static struct isl_basic_set *basic_set_append_equalities(
3150 struct isl_basic_set *bset, struct isl_mat *eq)
3152 int i, k;
3153 unsigned len;
3155 if (!bset || !eq)
3156 goto error;
3158 bset = isl_basic_set_extend_dim(bset, isl_dim_copy(bset->dim), 0,
3159 eq->n_row, 0);
3160 if (!bset)
3161 goto error;
3163 len = 1 + isl_dim_total(bset->dim) + bset->extra;
3164 for (i = 0; i < eq->n_row; ++i) {
3165 k = isl_basic_set_alloc_equality(bset);
3166 if (k < 0)
3167 goto error;
3168 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
3169 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
3171 isl_mat_free(eq);
3173 return bset;
3174 error:
3175 isl_mat_free(eq);
3176 isl_basic_set_free(bset);
3177 return NULL;
3180 /* Intersect the set "set" with the affine space specified by the
3181 * equalities in "eq".
3183 static struct isl_set *set_append_equalities(struct isl_set *set,
3184 struct isl_mat *eq)
3186 int i;
3188 if (!set || !eq)
3189 goto error;
3191 for (i = 0; i < set->n; ++i) {
3192 set->p[i] = basic_set_append_equalities(set->p[i],
3193 isl_mat_copy(eq));
3194 if (!set->p[i])
3195 goto error;
3197 isl_mat_free(eq);
3198 return set;
3199 error:
3200 isl_mat_free(eq);
3201 isl_set_free(set);
3202 return NULL;
3205 /* Project the given basic set onto its parameter domain, possibly introducing
3206 * new, explicit, existential variables in the constraints.
3207 * The input has parameters and (possibly implicit) existential variables.
3208 * The output has the same parameters, but only
3209 * explicit existentially quantified variables.
3211 * The actual projection is performed by pip, but pip doesn't seem
3212 * to like equalities very much, so we first remove the equalities
3213 * among the parameters by performing a variable compression on
3214 * the parameters. Afterward, an inverse transformation is performed
3215 * and the equalities among the parameters are inserted back in.
3217 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
3219 int i, j;
3220 struct isl_mat *eq;
3221 struct isl_mat *T, *T2;
3222 struct isl_set *set;
3223 unsigned nparam, n_div;
3225 bset = isl_basic_set_cow(bset);
3226 if (!bset)
3227 return NULL;
3229 if (bset->n_eq == 0)
3230 return isl_basic_set_lexmin(bset);
3232 isl_basic_set_gauss(bset, NULL);
3234 nparam = isl_basic_set_dim(bset, isl_dim_param);
3235 n_div = isl_basic_set_dim(bset, isl_dim_div);
3237 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
3238 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
3239 ++i;
3241 if (i == bset->n_eq)
3242 return isl_basic_set_lexmin(bset);
3244 eq = isl_mat_sub_alloc(bset->ctx, bset->eq, i, bset->n_eq - i,
3245 0, 1 + nparam);
3246 eq = isl_mat_cow(eq);
3247 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
3248 bset = basic_set_parameter_preimage(bset, T);
3250 set = isl_basic_set_lexmin(bset);
3251 set = set_parameter_preimage(set, T2);
3252 set = set_append_equalities(set, eq);
3253 return set;
3256 /* Compute an explicit representation for all the existentially
3257 * quantified variables.
3258 * The input and output dimensions are first turned into parameters.
3259 * compute_divs then returns a map with the same parameters and
3260 * no input or output dimensions and the dimension specification
3261 * is reset to that of the input.
3263 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
3265 struct isl_basic_set *bset;
3266 struct isl_set *set;
3267 struct isl_map *map;
3268 struct isl_dim *dim, *orig_dim = NULL;
3269 unsigned nparam;
3270 unsigned n_in;
3271 unsigned n_out;
3273 bmap = isl_basic_map_cow(bmap);
3274 if (!bmap)
3275 return NULL;
3277 nparam = isl_basic_map_dim(bmap, isl_dim_param);
3278 n_in = isl_basic_map_dim(bmap, isl_dim_in);
3279 n_out = isl_basic_map_dim(bmap, isl_dim_out);
3280 dim = isl_dim_set_alloc(bmap->ctx, nparam + n_in + n_out, 0);
3281 if (!dim)
3282 goto error;
3284 orig_dim = bmap->dim;
3285 bmap->dim = dim;
3286 bset = (struct isl_basic_set *)bmap;
3288 set = parameter_compute_divs(bset);
3289 map = (struct isl_map *)set;
3290 map = isl_map_reset_dim(map, orig_dim);
3292 return map;
3293 error:
3294 isl_basic_map_free(bmap);
3295 return NULL;
3298 /* If bmap contains any unknown divs, then compute explicit
3299 * expressions for them. However, this computation may be
3300 * quite expensive, so first try to remove divs that aren't
3301 * strictly needed.
3303 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
3305 int i;
3306 unsigned off;
3308 if (!bmap)
3309 return NULL;
3310 off = isl_dim_total(bmap->dim);
3311 for (i = 0; i < bmap->n_div; ++i) {
3312 if (isl_int_is_zero(bmap->div[i][0]))
3313 break;
3314 isl_assert(bmap->ctx, isl_int_is_zero(bmap->div[i][1+1+off+i]),
3315 goto error);
3317 if (i == bmap->n_div)
3318 return isl_map_from_basic_map(bmap);
3319 bmap = isl_basic_map_drop_redundant_divs(bmap);
3320 if (!bmap)
3321 goto error;
3322 for (i = 0; i < bmap->n_div; ++i)
3323 if (isl_int_is_zero(bmap->div[i][0]))
3324 break;
3325 if (i == bmap->n_div)
3326 return isl_map_from_basic_map(bmap);
3327 struct isl_map *map = compute_divs(bmap);
3328 return map;
3329 error:
3330 isl_basic_map_free(bmap);
3331 return NULL;
3334 struct isl_map *isl_map_compute_divs(struct isl_map *map)
3336 int i;
3337 struct isl_map *res;
3339 if (!map)
3340 return NULL;
3341 if (map->n == 0)
3342 return map;
3343 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
3344 for (i = 1 ; i < map->n; ++i) {
3345 struct isl_map *r2;
3346 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
3347 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
3348 res = isl_map_union_disjoint(res, r2);
3349 else
3350 res = isl_map_union(res, r2);
3352 isl_map_free(map);
3354 return res;
3357 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
3359 return (struct isl_set *)
3360 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
3363 struct isl_set *isl_set_compute_divs(struct isl_set *set)
3365 return (struct isl_set *)
3366 isl_map_compute_divs((struct isl_map *)set);
3369 struct isl_set *isl_map_domain(struct isl_map *map)
3371 int i;
3372 struct isl_set *set;
3374 if (!map)
3375 goto error;
3377 map = isl_map_cow(map);
3378 if (!map)
3379 return NULL;
3381 set = (struct isl_set *)map;
3382 set->dim = isl_dim_domain(set->dim);
3383 if (!set->dim)
3384 goto error;
3385 for (i = 0; i < map->n; ++i) {
3386 set->p[i] = isl_basic_map_domain(map->p[i]);
3387 if (!set->p[i])
3388 goto error;
3390 ISL_F_CLR(set, ISL_MAP_DISJOINT);
3391 ISL_F_CLR(set, ISL_SET_NORMALIZED);
3392 return set;
3393 error:
3394 isl_map_free(map);
3395 return NULL;
3398 struct isl_map *isl_map_union_disjoint(
3399 struct isl_map *map1, struct isl_map *map2)
3401 int i;
3402 unsigned flags = 0;
3403 struct isl_map *map = NULL;
3405 if (!map1 || !map2)
3406 goto error;
3408 if (map1->n == 0) {
3409 isl_map_free(map1);
3410 return map2;
3412 if (map2->n == 0) {
3413 isl_map_free(map2);
3414 return map1;
3417 isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
3419 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
3420 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
3421 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3423 map = isl_map_alloc_dim(isl_dim_copy(map1->dim),
3424 map1->n + map2->n, flags);
3425 if (!map)
3426 goto error;
3427 for (i = 0; i < map1->n; ++i) {
3428 map = isl_map_add(map,
3429 isl_basic_map_copy(map1->p[i]));
3430 if (!map)
3431 goto error;
3433 for (i = 0; i < map2->n; ++i) {
3434 map = isl_map_add(map,
3435 isl_basic_map_copy(map2->p[i]));
3436 if (!map)
3437 goto error;
3439 isl_map_free(map1);
3440 isl_map_free(map2);
3441 return map;
3442 error:
3443 isl_map_free(map);
3444 isl_map_free(map1);
3445 isl_map_free(map2);
3446 return NULL;
3449 struct isl_map *isl_map_union(struct isl_map *map1, struct isl_map *map2)
3451 map1 = isl_map_union_disjoint(map1, map2);
3452 if (!map1)
3453 return NULL;
3454 if (map1->n > 1)
3455 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
3456 return map1;
3459 struct isl_set *isl_set_union_disjoint(
3460 struct isl_set *set1, struct isl_set *set2)
3462 return (struct isl_set *)
3463 isl_map_union_disjoint(
3464 (struct isl_map *)set1, (struct isl_map *)set2);
3467 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
3469 return (struct isl_set *)
3470 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
3473 struct isl_map *isl_map_intersect_range(
3474 struct isl_map *map, struct isl_set *set)
3476 unsigned flags = 0;
3477 struct isl_map *result;
3478 int i, j;
3480 if (!map || !set)
3481 goto error;
3483 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
3484 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
3485 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3487 result = isl_map_alloc_dim(isl_dim_copy(map->dim),
3488 map->n * set->n, flags);
3489 if (!result)
3490 goto error;
3491 for (i = 0; i < map->n; ++i)
3492 for (j = 0; j < set->n; ++j) {
3493 result = isl_map_add(result,
3494 isl_basic_map_intersect_range(
3495 isl_basic_map_copy(map->p[i]),
3496 isl_basic_set_copy(set->p[j])));
3497 if (!result)
3498 goto error;
3500 isl_map_free(map);
3501 isl_set_free(set);
3502 return result;
3503 error:
3504 isl_map_free(map);
3505 isl_set_free(set);
3506 return NULL;
3509 struct isl_map *isl_map_intersect_domain(
3510 struct isl_map *map, struct isl_set *set)
3512 return isl_map_reverse(
3513 isl_map_intersect_range(isl_map_reverse(map), set));
3516 struct isl_map *isl_map_apply_domain(
3517 struct isl_map *map1, struct isl_map *map2)
3519 if (!map1 || !map2)
3520 goto error;
3521 map1 = isl_map_reverse(map1);
3522 map1 = isl_map_apply_range(map1, map2);
3523 return isl_map_reverse(map1);
3524 error:
3525 isl_map_free(map1);
3526 isl_map_free(map2);
3527 return NULL;
3530 struct isl_map *isl_map_apply_range(
3531 struct isl_map *map1, struct isl_map *map2)
3533 struct isl_dim *dim_result;
3534 struct isl_map *result;
3535 int i, j;
3536 unsigned nparam;
3537 unsigned n_in;
3538 unsigned n_out;
3540 if (!map1 || !map2)
3541 goto error;
3543 dim_result = isl_dim_join(isl_dim_copy(map1->dim),
3544 isl_dim_copy(map2->dim));
3546 result = isl_map_alloc_dim(dim_result, map1->n * map2->n, 0);
3547 if (!result)
3548 goto error;
3549 for (i = 0; i < map1->n; ++i)
3550 for (j = 0; j < map2->n; ++j) {
3551 result = isl_map_add(result,
3552 isl_basic_map_apply_range(
3553 isl_basic_map_copy(map1->p[i]),
3554 isl_basic_map_copy(map2->p[j])));
3555 if (!result)
3556 goto error;
3558 isl_map_free(map1);
3559 isl_map_free(map2);
3560 if (result && result->n <= 1)
3561 ISL_F_SET(result, ISL_MAP_DISJOINT);
3562 return result;
3563 error:
3564 isl_map_free(map1);
3565 isl_map_free(map2);
3566 return NULL;
3570 * returns range - domain
3572 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
3574 struct isl_basic_set *bset;
3575 unsigned dim;
3576 unsigned nparam;
3577 int i;
3579 if (!bmap)
3580 goto error;
3581 dim = isl_basic_map_n_in(bmap);
3582 nparam = isl_basic_map_n_param(bmap);
3583 isl_assert(bmap->ctx, dim == isl_basic_map_n_out(bmap), goto error);
3584 bset = isl_basic_set_from_basic_map(bmap);
3585 bset = isl_basic_set_cow(bset);
3586 bset = isl_basic_set_extend(bset, nparam, 3*dim, 0, dim, 0);
3587 bset = isl_basic_set_swap_vars(bset, 2*dim);
3588 for (i = 0; i < dim; ++i) {
3589 int j = isl_basic_map_alloc_equality(
3590 (struct isl_basic_map *)bset);
3591 if (j < 0)
3592 goto error;
3593 isl_seq_clr(bset->eq[j], 1 + isl_basic_set_total_dim(bset));
3594 isl_int_set_si(bset->eq[j][1+nparam+i], 1);
3595 isl_int_set_si(bset->eq[j][1+nparam+dim+i], 1);
3596 isl_int_set_si(bset->eq[j][1+nparam+2*dim+i], -1);
3598 return isl_basic_set_project_out(bset, 2*dim, 0);
3599 error:
3600 isl_basic_map_free(bmap);
3601 return NULL;
3605 * returns range - domain
3607 struct isl_set *isl_map_deltas(struct isl_map *map)
3609 int i;
3610 struct isl_set *result;
3612 if (!map)
3613 return NULL;
3615 isl_assert(map->ctx, isl_map_n_in(map) == isl_map_n_out(map), goto error);
3616 result = isl_set_alloc(map->ctx, isl_map_n_param(map),
3617 isl_map_n_in(map), map->n, map->flags);
3618 if (!result)
3619 goto error;
3620 for (i = 0; i < map->n; ++i)
3621 result = isl_set_add(result,
3622 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
3623 isl_map_free(map);
3624 return result;
3625 error:
3626 isl_map_free(map);
3627 return NULL;
3630 static struct isl_basic_map *basic_map_identity(struct isl_dim *dims)
3632 struct isl_basic_map *bmap;
3633 unsigned nparam;
3634 unsigned dim;
3635 int i;
3637 if (!dims)
3638 return NULL;
3640 nparam = dims->nparam;
3641 dim = dims->n_out;
3642 bmap = isl_basic_map_alloc_dim(dims, 0, dim, 0);
3643 if (!bmap)
3644 goto error;
3646 for (i = 0; i < dim; ++i) {
3647 int j = isl_basic_map_alloc_equality(bmap);
3648 if (j < 0)
3649 goto error;
3650 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
3651 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
3652 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
3654 return isl_basic_map_finalize(bmap);
3655 error:
3656 isl_basic_map_free(bmap);
3657 return NULL;
3660 struct isl_basic_map *isl_basic_map_identity(struct isl_dim *set_dim)
3662 struct isl_dim *dim = isl_dim_map(set_dim);
3663 if (!dim)
3664 return NULL;
3665 return basic_map_identity(dim);
3668 struct isl_basic_map *isl_basic_map_identity_like(struct isl_basic_map *model)
3670 if (!model || !model->dim)
3671 return NULL;
3672 isl_assert(model->ctx,
3673 model->dim->n_in == model->dim->n_out, return NULL);
3674 return basic_map_identity(isl_dim_copy(model->dim));
3677 static struct isl_map *map_identity(struct isl_dim *dim)
3679 struct isl_map *map = isl_map_alloc_dim(dim, 1, ISL_MAP_DISJOINT);
3680 return isl_map_add(map, basic_map_identity(isl_dim_copy(dim)));
3683 struct isl_map *isl_map_identity(struct isl_dim *set_dim)
3685 struct isl_dim *dim = isl_dim_map(set_dim);
3686 if (!dim)
3687 return NULL;
3688 return map_identity(dim);
3691 struct isl_map *isl_map_identity_like(struct isl_basic_map *model)
3693 if (!model || !model->dim)
3694 return NULL;
3695 isl_assert(model->ctx,
3696 model->dim->n_in == model->dim->n_out, return NULL);
3697 return map_identity(isl_dim_copy(model->dim));
3700 /* Construct a basic set with all set dimensions having only non-negative
3701 * values.
3703 struct isl_basic_set *isl_basic_set_positive_orthant(struct isl_dim *dims)
3705 int i;
3706 unsigned nparam;
3707 unsigned dim;
3708 struct isl_basic_set *bset;
3710 if (!dims)
3711 return NULL;
3712 nparam = dims->nparam;
3713 dim = dims->n_out;
3714 bset = isl_basic_set_alloc_dim(dims, 0, 0, dim);
3715 if (!bset)
3716 return NULL;
3717 for (i = 0; i < dim; ++i) {
3718 int k = isl_basic_set_alloc_inequality(bset);
3719 if (k < 0)
3720 goto error;
3721 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
3722 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
3724 return bset;
3725 error:
3726 isl_basic_set_free(bset);
3727 return NULL;
3730 int isl_set_is_equal(struct isl_set *set1, struct isl_set *set2)
3732 return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
3735 int isl_set_is_subset(struct isl_set *set1, struct isl_set *set2)
3737 return isl_map_is_subset(
3738 (struct isl_map *)set1, (struct isl_map *)set2);
3741 int isl_basic_map_is_subset(
3742 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3744 int is_subset;
3745 struct isl_map *map1;
3746 struct isl_map *map2;
3748 if (!bmap1 || !bmap2)
3749 return -1;
3751 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
3752 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
3754 is_subset = isl_map_is_subset(map1, map2);
3756 isl_map_free(map1);
3757 isl_map_free(map2);
3759 return is_subset;
3762 int isl_basic_map_is_equal(
3763 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3765 int is_subset;
3767 if (!bmap1 || !bmap2)
3768 return -1;
3769 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
3770 if (is_subset != 1)
3771 return is_subset;
3772 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
3773 return is_subset;
3776 int isl_basic_set_is_equal(
3777 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
3779 return isl_basic_map_is_equal(
3780 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
3783 int isl_map_is_empty(struct isl_map *map)
3785 int i;
3786 int is_empty;
3788 if (!map)
3789 return -1;
3790 for (i = 0; i < map->n; ++i) {
3791 is_empty = isl_basic_map_is_empty(map->p[i]);
3792 if (is_empty < 0)
3793 return -1;
3794 if (!is_empty)
3795 return 0;
3797 return 1;
3800 int isl_map_fast_is_empty(struct isl_map *map)
3802 return map->n == 0;
3805 int isl_set_is_empty(struct isl_set *set)
3807 return isl_map_is_empty((struct isl_map *)set);
3810 int isl_map_is_subset(struct isl_map *map1, struct isl_map *map2)
3812 int i;
3813 int is_subset = 0;
3814 struct isl_map *diff;
3816 if (!map1 || !map2)
3817 return -1;
3819 if (isl_map_is_empty(map1))
3820 return 1;
3822 if (isl_map_is_empty(map2))
3823 return 0;
3825 diff = isl_map_subtract(isl_map_copy(map1), isl_map_copy(map2));
3826 if (!diff)
3827 return -1;
3829 is_subset = isl_map_is_empty(diff);
3830 isl_map_free(diff);
3832 return is_subset;
3835 int isl_map_is_equal(struct isl_map *map1, struct isl_map *map2)
3837 int is_subset;
3839 if (!map1 || !map2)
3840 return -1;
3841 is_subset = isl_map_is_subset(map1, map2);
3842 if (is_subset != 1)
3843 return is_subset;
3844 is_subset = isl_map_is_subset(map2, map1);
3845 return is_subset;
3848 int isl_basic_map_is_strict_subset(
3849 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3851 int is_subset;
3853 if (!bmap1 || !bmap2)
3854 return -1;
3855 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
3856 if (is_subset != 1)
3857 return is_subset;
3858 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
3859 if (is_subset == -1)
3860 return is_subset;
3861 return !is_subset;
3864 int isl_basic_map_is_universe(struct isl_basic_map *bmap)
3866 if (!bmap)
3867 return -1;
3868 return bmap->n_eq == 0 && bmap->n_ineq == 0;
3871 int isl_basic_set_is_universe(struct isl_basic_set *bset)
3873 if (!bset)
3874 return -1;
3875 return bset->n_eq == 0 && bset->n_ineq == 0;
3878 int isl_basic_map_is_empty(struct isl_basic_map *bmap)
3880 struct isl_basic_set *bset = NULL;
3881 struct isl_vec *sample = NULL;
3882 int empty;
3883 unsigned total;
3885 if (!bmap)
3886 return -1;
3888 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
3889 return 1;
3891 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
3892 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
3893 copy = isl_basic_map_convex_hull(copy);
3894 empty = ISL_F_ISSET(copy, ISL_BASIC_MAP_EMPTY);
3895 isl_basic_map_free(copy);
3896 return empty;
3899 total = 1 + isl_basic_map_total_dim(bmap);
3900 if (bmap->sample && bmap->sample->size == total) {
3901 int contains = basic_map_contains(bmap, bmap->sample);
3902 if (contains < 0)
3903 return -1;
3904 if (contains)
3905 return 0;
3907 isl_vec_free(bmap->sample);
3908 bmap->sample = NULL;
3909 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
3910 if (!bset)
3911 return -1;
3912 sample = isl_basic_set_sample(bset);
3913 if (!sample)
3914 return -1;
3915 empty = sample->size == 0;
3916 isl_vec_free(bmap->sample);
3917 bmap->sample = sample;
3918 if (empty)
3919 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
3921 return empty;
3924 int isl_basic_map_fast_is_empty(struct isl_basic_map *bmap)
3926 if (!bmap)
3927 return -1;
3928 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
3931 int isl_basic_set_fast_is_empty(struct isl_basic_set *bset)
3933 if (!bset)
3934 return -1;
3935 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
3938 int isl_basic_set_is_empty(struct isl_basic_set *bset)
3940 return isl_basic_map_is_empty((struct isl_basic_map *)bset);
3943 struct isl_map *isl_basic_map_union(
3944 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3946 struct isl_map *map;
3947 if (!bmap1 || !bmap2)
3948 return NULL;
3950 isl_assert(map1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim), goto error);
3952 map = isl_map_alloc_dim(isl_dim_copy(bmap1->dim), 2, 0);
3953 if (!map)
3954 goto error;
3955 map = isl_map_add(map, bmap1);
3956 map = isl_map_add(map, bmap2);
3957 return map;
3958 error:
3959 isl_basic_map_free(bmap1);
3960 isl_basic_map_free(bmap2);
3961 return NULL;
3964 struct isl_set *isl_basic_set_union(
3965 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
3967 return (struct isl_set *)isl_basic_map_union(
3968 (struct isl_basic_map *)bset1,
3969 (struct isl_basic_map *)bset2);
3972 /* Order divs such that any div only depends on previous divs */
3973 static struct isl_basic_map *order_divs(struct isl_basic_map *bmap)
3975 int i;
3976 unsigned off = isl_dim_total(bmap->dim);
3978 for (i = 0; i < bmap->n_div; ++i) {
3979 int pos;
3980 if (isl_int_is_zero(bmap->div[i][0]))
3981 continue;
3982 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
3983 bmap->n_div-i);
3984 if (pos == -1)
3985 continue;
3986 isl_basic_map_swap_div(bmap, i, i + pos);
3987 --i;
3989 return bmap;
3992 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
3994 return (struct isl_basic_set *)order_divs((struct isl_basic_map *)bset);
3997 /* Look for a div in dst that corresponds to the div "div" in src.
3998 * The divs before "div" in src and dst are assumed to be the same.
4000 * Returns -1 if no corresponding div was found and the position
4001 * of the corresponding div in dst otherwise.
4003 static int find_div(struct isl_basic_map *dst,
4004 struct isl_basic_map *src, unsigned div)
4006 int i;
4008 unsigned total = isl_dim_total(src->dim);
4010 isl_assert(dst->ctx, div <= dst->n_div, return -1);
4011 for (i = div; i < dst->n_div; ++i)
4012 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
4013 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
4014 dst->n_div - div) == -1)
4015 return i;
4016 return -1;
4019 struct isl_basic_map *isl_basic_map_align_divs(
4020 struct isl_basic_map *dst, struct isl_basic_map *src)
4022 int i;
4023 unsigned total = isl_dim_total(src->dim);
4025 if (!dst || !src)
4026 goto error;
4028 if (src->n_div == 0)
4029 return dst;
4031 for (i = 0; i < src->n_div; ++i)
4032 isl_assert(src->ctx, !isl_int_is_zero(src->div[i][0]), goto error);
4034 src = order_divs(src);
4035 dst = isl_basic_map_cow(dst);
4036 dst = isl_basic_map_extend_dim(dst, isl_dim_copy(dst->dim),
4037 src->n_div, 0, 2 * src->n_div);
4038 if (!dst)
4039 return NULL;
4040 for (i = 0; i < src->n_div; ++i) {
4041 int j = find_div(dst, src, i);
4042 if (j < 0) {
4043 j = isl_basic_map_alloc_div(dst);
4044 if (j < 0)
4045 goto error;
4046 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
4047 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
4048 if (add_div_constraints(dst, j) < 0)
4049 goto error;
4051 if (j != i)
4052 isl_basic_map_swap_div(dst, i, j);
4054 return dst;
4055 error:
4056 isl_basic_map_free(dst);
4057 return NULL;
4060 struct isl_basic_set *isl_basic_set_align_divs(
4061 struct isl_basic_set *dst, struct isl_basic_set *src)
4063 return (struct isl_basic_set *)isl_basic_map_align_divs(
4064 (struct isl_basic_map *)dst, (struct isl_basic_map *)src);
4067 struct isl_map *isl_map_align_divs(struct isl_map *map)
4069 int i;
4071 map = isl_map_compute_divs(map);
4072 map = isl_map_cow(map);
4073 if (!map)
4074 return NULL;
4076 for (i = 1; i < map->n; ++i)
4077 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
4078 for (i = 1; i < map->n; ++i)
4079 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
4081 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4082 return map;
4085 struct isl_set *isl_set_align_divs(struct isl_set *set)
4087 return (struct isl_set *)isl_map_align_divs((struct isl_map *)set);
4090 static struct isl_map *add_cut_constraint(struct isl_map *dst,
4091 struct isl_basic_map *src, isl_int *c,
4092 unsigned len, int oppose)
4094 struct isl_basic_map *copy = NULL;
4095 int is_empty;
4096 int k;
4097 unsigned total;
4099 copy = isl_basic_map_copy(src);
4100 copy = isl_basic_map_cow(copy);
4101 if (!copy)
4102 goto error;
4103 copy = isl_basic_map_extend_constraints(copy, 0, 1);
4104 k = isl_basic_map_alloc_inequality(copy);
4105 if (k < 0)
4106 goto error;
4107 if (oppose)
4108 isl_seq_neg(copy->ineq[k], c, len);
4109 else
4110 isl_seq_cpy(copy->ineq[k], c, len);
4111 total = 1 + isl_basic_map_total_dim(copy);
4112 isl_seq_clr(copy->ineq[k]+len, total - len);
4113 isl_inequality_negate(copy, k);
4114 copy = isl_basic_map_simplify(copy);
4115 copy = isl_basic_map_finalize(copy);
4116 is_empty = isl_basic_map_is_empty(copy);
4117 if (is_empty < 0)
4118 goto error;
4119 if (!is_empty)
4120 dst = isl_map_add(dst, copy);
4121 else
4122 isl_basic_map_free(copy);
4123 return dst;
4124 error:
4125 isl_basic_map_free(copy);
4126 isl_map_free(dst);
4127 return NULL;
4130 static struct isl_map *subtract(struct isl_map *map, struct isl_basic_map *bmap)
4132 int i, j, k;
4133 unsigned flags = 0;
4134 struct isl_map *rest = NULL;
4135 unsigned max;
4136 unsigned total = isl_basic_map_total_dim(bmap);
4138 assert(bmap);
4140 if (!map)
4141 goto error;
4143 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
4144 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
4146 max = map->n * (2 * bmap->n_eq + bmap->n_ineq);
4147 rest = isl_map_alloc_dim(isl_dim_copy(map->dim), max, flags);
4148 if (!rest)
4149 goto error;
4151 for (i = 0; i < map->n; ++i) {
4152 map->p[i] = isl_basic_map_align_divs(map->p[i], bmap);
4153 if (!map->p[i])
4154 goto error;
4157 for (j = 0; j < map->n; ++j)
4158 map->p[j] = isl_basic_map_cow(map->p[j]);
4160 for (i = 0; i < bmap->n_eq; ++i) {
4161 for (j = 0; j < map->n; ++j) {
4162 rest = add_cut_constraint(rest,
4163 map->p[j], bmap->eq[i], 1+total, 0);
4164 if (!rest)
4165 goto error;
4167 rest = add_cut_constraint(rest,
4168 map->p[j], bmap->eq[i], 1+total, 1);
4169 if (!rest)
4170 goto error;
4172 map->p[j] = isl_basic_map_extend_constraints(map->p[j],
4173 1, 0);
4174 if (!map->p[j])
4175 goto error;
4176 k = isl_basic_map_alloc_equality(map->p[j]);
4177 if (k < 0)
4178 goto error;
4179 isl_seq_cpy(map->p[j]->eq[k], bmap->eq[i], 1+total);
4180 isl_seq_clr(map->p[j]->eq[k]+1+total,
4181 map->p[j]->n_div - bmap->n_div);
4185 for (i = 0; i < bmap->n_ineq; ++i) {
4186 for (j = 0; j < map->n; ++j) {
4187 rest = add_cut_constraint(rest,
4188 map->p[j], bmap->ineq[i], 1+total, 0);
4189 if (!rest)
4190 goto error;
4192 map->p[j] = isl_basic_map_extend_constraints(map->p[j],
4193 0, 1);
4194 if (!map->p[j])
4195 goto error;
4196 k = isl_basic_map_alloc_inequality(map->p[j]);
4197 if (k < 0)
4198 goto error;
4199 isl_seq_cpy(map->p[j]->ineq[k], bmap->ineq[i], 1+total);
4200 isl_seq_clr(map->p[j]->ineq[k]+1+total,
4201 map->p[j]->n_div - bmap->n_div);
4205 isl_map_free(map);
4206 return rest;
4207 error:
4208 isl_map_free(map);
4209 isl_map_free(rest);
4210 return NULL;
4213 struct isl_map *isl_map_subtract(struct isl_map *map1, struct isl_map *map2)
4215 int i;
4216 if (!map1 || !map2)
4217 goto error;
4219 isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
4221 if (isl_map_is_empty(map2)) {
4222 isl_map_free(map2);
4223 return map1;
4226 map1 = isl_map_compute_divs(map1);
4227 map2 = isl_map_compute_divs(map2);
4228 if (!map1 || !map2)
4229 goto error;
4231 for (i = 0; map1 && i < map2->n; ++i)
4232 map1 = subtract(map1, map2->p[i]);
4234 isl_map_free(map2);
4235 return map1;
4236 error:
4237 isl_map_free(map1);
4238 isl_map_free(map2);
4239 return NULL;
4242 struct isl_set *isl_set_subtract(struct isl_set *set1, struct isl_set *set2)
4244 return (struct isl_set *)
4245 isl_map_subtract(
4246 (struct isl_map *)set1, (struct isl_map *)set2);
4249 struct isl_set *isl_set_apply(struct isl_set *set, struct isl_map *map)
4251 if (!set || !map)
4252 goto error;
4253 isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
4254 map = isl_map_intersect_domain(map, set);
4255 set = isl_map_range(map);
4256 return set;
4257 error:
4258 isl_set_free(set);
4259 isl_map_free(map);
4260 return NULL;
4263 /* There is no need to cow as removing empty parts doesn't change
4264 * the meaning of the set.
4266 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
4268 int i;
4270 if (!map)
4271 return NULL;
4273 for (i = map->n-1; i >= 0; --i) {
4274 if (!ISL_F_ISSET(map->p[i], ISL_BASIC_MAP_EMPTY))
4275 continue;
4276 isl_basic_map_free(map->p[i]);
4277 if (i != map->n-1) {
4278 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4279 map->p[i] = map->p[map->n-1];
4281 map->n--;
4284 return map;
4287 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
4289 return (struct isl_set *)
4290 isl_map_remove_empty_parts((struct isl_map *)set);
4293 struct isl_basic_map *isl_map_copy_basic_map(struct isl_map *map)
4295 struct isl_basic_map *bmap;
4296 if (!map || map->n == 0)
4297 return NULL;
4298 bmap = map->p[map->n-1];
4299 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
4300 return isl_basic_map_copy(bmap);
4303 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
4305 (struct isl_basic_set *)isl_map_copy_basic_map((struct isl_map *)set);
4308 struct isl_map *isl_map_drop_basic_map(struct isl_map *map,
4309 struct isl_basic_map *bmap)
4311 int i;
4313 if (!map || !bmap)
4314 goto error;
4315 for (i = map->n-1; i >= 0; --i) {
4316 if (map->p[i] != bmap)
4317 continue;
4318 map = isl_map_cow(map);
4319 if (!map)
4320 goto error;
4321 isl_basic_map_free(map->p[i]);
4322 if (i != map->n-1) {
4323 ISL_F_CLR(map, ISL_SET_NORMALIZED);
4324 map->p[i] = map->p[map->n-1];
4326 map->n--;
4327 return map;
4329 isl_basic_map_free(bmap);
4330 return map;
4331 error:
4332 isl_map_free(map);
4333 isl_basic_map_free(bmap);
4334 return NULL;
4337 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
4338 struct isl_basic_set *bset)
4340 (struct isl_set *)isl_map_drop_basic_map((struct isl_map *)set,
4341 (struct isl_basic_map *)bset);
4344 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
4345 * for any common value of the parameters and dimensions preceding dim
4346 * in both basic sets, the values of dimension pos in bset1 are
4347 * smaller or larger than those in bset2.
4349 * Returns
4350 * 1 if bset1 follows bset2
4351 * -1 if bset1 precedes bset2
4352 * 0 if bset1 and bset2 are incomparable
4353 * -2 if some error occurred.
4355 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
4356 struct isl_basic_set *bset2, int pos)
4358 struct isl_dim *dims;
4359 struct isl_basic_map *bmap1 = NULL;
4360 struct isl_basic_map *bmap2 = NULL;
4361 struct isl_ctx *ctx;
4362 struct isl_vec *obj;
4363 unsigned total;
4364 unsigned nparam;
4365 unsigned dim1, dim2;
4366 isl_int num, den;
4367 enum isl_lp_result res;
4368 int cmp;
4370 if (!bset1 || !bset2)
4371 return -2;
4373 nparam = isl_basic_set_n_param(bset1);
4374 dim1 = isl_basic_set_n_dim(bset1);
4375 dim2 = isl_basic_set_n_dim(bset2);
4376 dims = isl_dim_alloc(bset1->ctx, nparam, pos, dim1 - pos);
4377 bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1), dims);
4378 dims = isl_dim_alloc(bset2->ctx, nparam, pos, dim2 - pos);
4379 bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims);
4380 if (!bmap1 || !bmap2)
4381 goto error;
4382 bmap1 = isl_basic_map_cow(bmap1);
4383 bmap1 = isl_basic_map_extend(bmap1, nparam,
4384 pos, (dim1 - pos) + (dim2 - pos),
4385 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
4386 bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
4387 if (!bmap1)
4388 goto error;
4389 total = isl_basic_map_total_dim(bmap1);
4390 ctx = bmap1->ctx;
4391 obj = isl_vec_alloc(ctx, 1 + total);
4392 isl_seq_clr(obj->block.data, 1 + total);
4393 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
4394 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
4395 if (!obj)
4396 goto error;
4397 isl_int_init(num);
4398 isl_int_init(den);
4399 res = isl_solve_lp(bmap1, 0, obj->block.data, ctx->one, &num, &den);
4400 if (res == isl_lp_empty)
4401 cmp = 0;
4402 else if (res == isl_lp_ok && isl_int_is_pos(num))
4403 cmp = 1;
4404 else if ((res == isl_lp_ok && isl_int_is_neg(num)) ||
4405 res == isl_lp_unbounded)
4406 cmp = -1;
4407 else
4408 cmp = -2;
4409 isl_int_clear(num);
4410 isl_int_clear(den);
4411 isl_basic_map_free(bmap1);
4412 isl_vec_free(obj);
4413 return cmp;
4414 error:
4415 isl_basic_map_free(bmap1);
4416 isl_basic_map_free(bmap2);
4417 return -2;
4420 static int isl_basic_map_fast_has_fixed_var(struct isl_basic_map *bmap,
4421 unsigned pos, isl_int *val)
4423 int i;
4424 int d;
4425 unsigned total;
4427 if (!bmap)
4428 return -1;
4429 total = isl_basic_map_total_dim(bmap);
4430 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
4431 for (; d+1 > pos; --d)
4432 if (!isl_int_is_zero(bmap->eq[i][1+d]))
4433 break;
4434 if (d != pos)
4435 continue;
4436 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
4437 return 0;
4438 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
4439 return 0;
4440 if (!isl_int_is_one(bmap->eq[i][1+d]))
4441 return 0;
4442 if (val)
4443 isl_int_neg(*val, bmap->eq[i][0]);
4444 return 1;
4446 return 0;
4449 static int isl_map_fast_has_fixed_var(struct isl_map *map,
4450 unsigned pos, isl_int *val)
4452 int i;
4453 isl_int v;
4454 isl_int tmp;
4455 int fixed;
4457 if (!map)
4458 return -1;
4459 if (map->n == 0)
4460 return 0;
4461 if (map->n == 1)
4462 return isl_basic_map_fast_has_fixed_var(map->p[0], pos, val);
4463 isl_int_init(v);
4464 isl_int_init(tmp);
4465 fixed = isl_basic_map_fast_has_fixed_var(map->p[0], pos, &v);
4466 for (i = 1; fixed == 1 && i < map->n; ++i) {
4467 fixed = isl_basic_map_fast_has_fixed_var(map->p[i], pos, &tmp);
4468 if (fixed == 1 && isl_int_ne(tmp, v))
4469 fixed = 0;
4471 if (val)
4472 isl_int_set(*val, v);
4473 isl_int_clear(tmp);
4474 isl_int_clear(v);
4475 return fixed;
4478 static int isl_basic_set_fast_has_fixed_var(struct isl_basic_set *bset,
4479 unsigned pos, isl_int *val)
4481 return isl_basic_map_fast_has_fixed_var((struct isl_basic_map *)bset,
4482 pos, val);
4485 static int isl_set_fast_has_fixed_var(struct isl_set *set, unsigned pos,
4486 isl_int *val)
4488 return isl_map_fast_has_fixed_var((struct isl_map *)set, pos, val);
4491 int isl_basic_map_fast_is_fixed(struct isl_basic_map *bmap,
4492 enum isl_dim_type type, unsigned pos, isl_int *val)
4494 if (pos >= isl_basic_map_dim(bmap, type))
4495 return -1;
4496 return isl_basic_map_fast_has_fixed_var(bmap,
4497 isl_basic_map_offset(bmap, type) - 1 + pos, val);
4500 /* Check if dimension dim has fixed value and if so and if val is not NULL,
4501 * then return this fixed value in *val.
4503 int isl_basic_set_fast_dim_is_fixed(struct isl_basic_set *bset, unsigned dim,
4504 isl_int *val)
4506 return isl_basic_set_fast_has_fixed_var(bset,
4507 isl_basic_set_n_param(bset) + dim, val);
4510 /* Check if dimension dim has fixed value and if so and if val is not NULL,
4511 * then return this fixed value in *val.
4513 int isl_set_fast_dim_is_fixed(struct isl_set *set, unsigned dim, isl_int *val)
4515 return isl_set_fast_has_fixed_var(set, isl_set_n_param(set) + dim, val);
4518 /* Check if input variable in has fixed value and if so and if val is not NULL,
4519 * then return this fixed value in *val.
4521 int isl_map_fast_input_is_fixed(struct isl_map *map, unsigned in, isl_int *val)
4523 return isl_map_fast_has_fixed_var(map, isl_map_n_param(map) + in, val);
4526 /* Check if dimension dim has an (obvious) fixed lower bound and if so
4527 * and if val is not NULL, then return this lower bound in *val.
4529 int isl_basic_set_fast_dim_has_fixed_lower_bound(struct isl_basic_set *bset,
4530 unsigned dim, isl_int *val)
4532 int i, i_eq = -1, i_ineq = -1;
4533 isl_int *c;
4534 unsigned total;
4535 unsigned nparam;
4537 if (!bset)
4538 return -1;
4539 total = isl_basic_set_total_dim(bset);
4540 nparam = isl_basic_set_n_param(bset);
4541 for (i = 0; i < bset->n_eq; ++i) {
4542 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
4543 continue;
4544 if (i_eq != -1)
4545 return 0;
4546 i_eq = i;
4548 for (i = 0; i < bset->n_ineq; ++i) {
4549 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
4550 continue;
4551 if (i_eq != -1 || i_ineq != -1)
4552 return 0;
4553 i_ineq = i;
4555 if (i_eq == -1 && i_ineq == -1)
4556 return 0;
4557 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
4558 /* The coefficient should always be one due to normalization. */
4559 if (!isl_int_is_one(c[1+nparam+dim]))
4560 return 0;
4561 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
4562 return 0;
4563 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
4564 total - nparam - dim - 1) != -1)
4565 return 0;
4566 if (val)
4567 isl_int_neg(*val, c[0]);
4568 return 1;
4571 int isl_set_fast_dim_has_fixed_lower_bound(struct isl_set *set,
4572 unsigned dim, isl_int *val)
4574 int i;
4575 isl_int v;
4576 isl_int tmp;
4577 int fixed;
4579 if (!set)
4580 return -1;
4581 if (set->n == 0)
4582 return 0;
4583 if (set->n == 1)
4584 return isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[0],
4585 dim, val);
4586 isl_int_init(v);
4587 isl_int_init(tmp);
4588 fixed = isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[0],
4589 dim, &v);
4590 for (i = 1; fixed == 1 && i < set->n; ++i) {
4591 fixed = isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[i],
4592 dim, &tmp);
4593 if (fixed == 1 && isl_int_ne(tmp, v))
4594 fixed = 0;
4596 if (val)
4597 isl_int_set(*val, v);
4598 isl_int_clear(tmp);
4599 isl_int_clear(v);
4600 return fixed;
4603 struct constraint {
4604 unsigned size;
4605 isl_int *c;
4608 static int qsort_constraint_cmp(const void *p1, const void *p2)
4610 const struct constraint *c1 = (const struct constraint *)p1;
4611 const struct constraint *c2 = (const struct constraint *)p2;
4612 unsigned size = isl_min(c1->size, c2->size);
4613 return isl_seq_cmp(c1->c, c2->c, size);
4616 static struct isl_basic_map *isl_basic_map_sort_constraints(
4617 struct isl_basic_map *bmap)
4619 int i;
4620 struct constraint *c;
4621 unsigned total;
4623 if (!bmap)
4624 return NULL;
4625 total = isl_basic_map_total_dim(bmap);
4626 c = isl_alloc_array(bmap->ctx, struct constraint, bmap->n_ineq);
4627 if (!c)
4628 goto error;
4629 for (i = 0; i < bmap->n_ineq; ++i) {
4630 c[i].size = total;
4631 c[i].c = bmap->ineq[i];
4633 qsort(c, bmap->n_ineq, sizeof(struct constraint), qsort_constraint_cmp);
4634 for (i = 0; i < bmap->n_ineq; ++i)
4635 bmap->ineq[i] = c[i].c;
4636 free(c);
4637 return bmap;
4638 error:
4639 isl_basic_map_free(bmap);
4640 return NULL;
4643 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
4645 if (!bmap)
4646 return NULL;
4647 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
4648 return bmap;
4649 bmap = isl_basic_map_convex_hull(bmap);
4650 bmap = isl_basic_map_sort_constraints(bmap);
4651 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
4652 return bmap;
4655 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
4657 return (struct isl_basic_set *)isl_basic_map_normalize(
4658 (struct isl_basic_map *)bset);
4661 static int isl_basic_map_fast_cmp(const struct isl_basic_map *bmap1,
4662 const struct isl_basic_map *bmap2)
4664 int i, cmp;
4665 unsigned total;
4667 if (bmap1 == bmap2)
4668 return 0;
4669 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
4670 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
4671 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
4672 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
4673 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
4674 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
4675 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
4676 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
4677 return 0;
4678 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
4679 return 1;
4680 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
4681 return -1;
4682 if (bmap1->n_eq != bmap2->n_eq)
4683 return bmap1->n_eq - bmap2->n_eq;
4684 if (bmap1->n_ineq != bmap2->n_ineq)
4685 return bmap1->n_ineq - bmap2->n_ineq;
4686 if (bmap1->n_div != bmap2->n_div)
4687 return bmap1->n_div - bmap2->n_div;
4688 total = isl_basic_map_total_dim(bmap1);
4689 for (i = 0; i < bmap1->n_eq; ++i) {
4690 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
4691 if (cmp)
4692 return cmp;
4694 for (i = 0; i < bmap1->n_ineq; ++i) {
4695 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
4696 if (cmp)
4697 return cmp;
4699 for (i = 0; i < bmap1->n_div; ++i) {
4700 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
4701 if (cmp)
4702 return cmp;
4704 return 0;
4707 static int isl_basic_map_fast_is_equal(struct isl_basic_map *bmap1,
4708 struct isl_basic_map *bmap2)
4710 return isl_basic_map_fast_cmp(bmap1, bmap2) == 0;
4713 static int qsort_bmap_cmp(const void *p1, const void *p2)
4715 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
4716 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
4718 return isl_basic_map_fast_cmp(bmap1, bmap2);
4721 /* We normalize in place, but if anything goes wrong we need
4722 * to return NULL, so we need to make sure we don't change the
4723 * meaning of any possible other copies of map.
4725 struct isl_map *isl_map_normalize(struct isl_map *map)
4727 int i, j;
4728 struct isl_basic_map *bmap;
4730 if (!map)
4731 return NULL;
4732 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
4733 return map;
4734 for (i = 0; i < map->n; ++i) {
4735 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
4736 if (!bmap)
4737 goto error;
4738 isl_basic_map_free(map->p[i]);
4739 map->p[i] = bmap;
4741 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
4742 ISL_F_SET(map, ISL_MAP_NORMALIZED);
4743 map = isl_map_remove_empty_parts(map);
4744 if (!map)
4745 return NULL;
4746 for (i = map->n - 1; i >= 1; --i) {
4747 if (!isl_basic_map_fast_is_equal(map->p[i-1], map->p[i]))
4748 continue;
4749 isl_basic_map_free(map->p[i-1]);
4750 for (j = i; j < map->n; ++j)
4751 map->p[j-1] = map->p[j];
4752 map->n--;
4754 return map;
4755 error:
4756 isl_map_free(map);
4757 return NULL;
4761 struct isl_set *isl_set_normalize(struct isl_set *set)
4763 return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
4766 int isl_map_fast_is_equal(struct isl_map *map1, struct isl_map *map2)
4768 int i;
4769 int equal;
4771 if (!map1 || !map2)
4772 return -1;
4774 if (map1 == map2)
4775 return 1;
4776 if (!isl_dim_equal(map1->dim, map2->dim))
4777 return 0;
4779 map1 = isl_map_copy(map1);
4780 map2 = isl_map_copy(map2);
4781 map1 = isl_map_normalize(map1);
4782 map2 = isl_map_normalize(map2);
4783 if (!map1 || !map2)
4784 goto error;
4785 equal = map1->n == map2->n;
4786 for (i = 0; equal && i < map1->n; ++i) {
4787 equal = isl_basic_map_fast_is_equal(map1->p[i], map2->p[i]);
4788 if (equal < 0)
4789 goto error;
4791 isl_map_free(map1);
4792 isl_map_free(map2);
4793 return equal;
4794 error:
4795 isl_map_free(map1);
4796 isl_map_free(map2);
4797 return -1;
4800 int isl_set_fast_is_equal(struct isl_set *set1, struct isl_set *set2)
4802 return isl_map_fast_is_equal((struct isl_map *)set1,
4803 (struct isl_map *)set2);
4806 /* Return an interval that ranges from min to max (inclusive)
4808 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
4809 isl_int min, isl_int max)
4811 int k;
4812 struct isl_basic_set *bset = NULL;
4814 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
4815 if (!bset)
4816 goto error;
4818 k = isl_basic_set_alloc_inequality(bset);
4819 if (k < 0)
4820 goto error;
4821 isl_int_set_si(bset->ineq[k][1], 1);
4822 isl_int_neg(bset->ineq[k][0], min);
4824 k = isl_basic_set_alloc_inequality(bset);
4825 if (k < 0)
4826 goto error;
4827 isl_int_set_si(bset->ineq[k][1], -1);
4828 isl_int_set(bset->ineq[k][0], max);
4830 return bset;
4831 error:
4832 isl_basic_set_free(bset);
4833 return NULL;
4836 /* Return the Cartesian product of the basic sets in list (in the given order).
4838 struct isl_basic_set *isl_basic_set_product(struct isl_basic_set_list *list)
4840 int i;
4841 unsigned dim;
4842 unsigned nparam;
4843 unsigned extra;
4844 unsigned n_eq;
4845 unsigned n_ineq;
4846 struct isl_basic_set *product = NULL;
4848 if (!list)
4849 goto error;
4850 isl_assert(list->ctx, list->n > 0, goto error);
4851 isl_assert(list->ctx, list->p[0], goto error);
4852 nparam = isl_basic_set_n_param(list->p[0]);
4853 dim = isl_basic_set_n_dim(list->p[0]);
4854 extra = list->p[0]->n_div;
4855 n_eq = list->p[0]->n_eq;
4856 n_ineq = list->p[0]->n_ineq;
4857 for (i = 1; i < list->n; ++i) {
4858 isl_assert(list->ctx, list->p[i], goto error);
4859 isl_assert(list->ctx,
4860 nparam == isl_basic_set_n_param(list->p[i]), goto error);
4861 dim += isl_basic_set_n_dim(list->p[i]);
4862 extra += list->p[i]->n_div;
4863 n_eq += list->p[i]->n_eq;
4864 n_ineq += list->p[i]->n_ineq;
4866 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
4867 n_eq, n_ineq);
4868 if (!product)
4869 goto error;
4870 dim = 0;
4871 for (i = 0; i < list->n; ++i) {
4872 isl_basic_set_add_constraints(product,
4873 isl_basic_set_copy(list->p[i]), dim);
4874 dim += isl_basic_set_n_dim(list->p[i]);
4876 isl_basic_set_list_free(list);
4877 return product;
4878 error:
4879 isl_basic_set_free(product);
4880 isl_basic_set_list_free(list);
4881 return NULL;
4884 struct isl_basic_map *isl_basic_map_product(
4885 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4887 struct isl_dim *dim_result = NULL;
4888 struct isl_basic_map *bmap;
4889 unsigned in1, in2, out1, out2, nparam, total, pos;
4890 struct isl_dim_map *dim_map1, *dim_map2;
4892 if (!bmap1 || !bmap2)
4893 goto error;
4895 isl_assert(map1->ctx, isl_dim_match(bmap1->dim, isl_dim_param,
4896 bmap2->dim, isl_dim_param), goto error);
4897 dim_result = isl_dim_product(isl_dim_copy(bmap1->dim),
4898 isl_dim_copy(bmap2->dim));
4900 in1 = isl_basic_map_n_in(bmap1);
4901 in2 = isl_basic_map_n_in(bmap2);
4902 out1 = isl_basic_map_n_out(bmap1);
4903 out2 = isl_basic_map_n_out(bmap2);
4904 nparam = isl_basic_map_n_param(bmap1);
4906 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
4907 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4908 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
4909 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4910 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
4911 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4912 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
4913 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
4914 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
4915 isl_dim_map_div(dim_map1, bmap1, pos += out2);
4916 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4918 bmap = isl_basic_map_alloc_dim(dim_result,
4919 bmap1->n_div + bmap2->n_div,
4920 bmap1->n_eq + bmap2->n_eq,
4921 bmap1->n_ineq + bmap2->n_ineq);
4922 bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
4923 bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
4924 bmap = isl_basic_map_simplify(bmap);
4925 return isl_basic_map_finalize(bmap);
4926 error:
4927 isl_basic_map_free(bmap1);
4928 isl_basic_map_free(bmap2);
4929 return NULL;
4932 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
4934 struct isl_map *isl_map_product(struct isl_map *map1, struct isl_map *map2)
4936 unsigned flags = 0;
4937 struct isl_map *result;
4938 int i, j;
4940 if (!map1 || !map2)
4941 goto error;
4943 isl_assert(map1->ctx, isl_dim_match(map1->dim, isl_dim_param,
4944 map2->dim, isl_dim_param), goto error);
4946 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
4947 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
4948 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
4950 result = isl_map_alloc_dim(isl_dim_product(isl_dim_copy(map1->dim),
4951 isl_dim_copy(map2->dim)),
4952 map1->n * map2->n, flags);
4953 if (!result)
4954 goto error;
4955 for (i = 0; i < map1->n; ++i)
4956 for (j = 0; j < map2->n; ++j) {
4957 struct isl_basic_map *part;
4958 part = isl_basic_map_product(
4959 isl_basic_map_copy(map1->p[i]),
4960 isl_basic_map_copy(map2->p[j]));
4961 if (isl_basic_map_is_empty(part))
4962 isl_basic_map_free(part);
4963 else
4964 result = isl_map_add(result, part);
4965 if (!result)
4966 goto error;
4968 isl_map_free(map1);
4969 isl_map_free(map2);
4970 return result;
4971 error:
4972 isl_map_free(map1);
4973 isl_map_free(map2);
4974 return NULL;
4977 /* Given two set A and B, construct its Cartesian product A x B.
4979 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
4981 return (struct isl_set *)isl_map_product((struct isl_map *)set1,
4982 (struct isl_map *)set2);
4985 uint32_t isl_basic_set_get_hash(struct isl_basic_set *bset)
4987 int i;
4988 uint32_t hash;
4989 unsigned total;
4991 if (!bset)
4992 return 0;
4993 bset = isl_basic_set_copy(bset);
4994 bset = isl_basic_set_normalize(bset);
4995 if (!bset)
4996 return 0;
4997 total = isl_basic_set_total_dim(bset);
4998 isl_hash_byte(hash, bset->n_eq & 0xFF);
4999 for (i = 0; i < bset->n_eq; ++i) {
5000 uint32_t c_hash;
5001 c_hash = isl_seq_get_hash(bset->eq[i], 1 + total);
5002 isl_hash_hash(hash, c_hash);
5004 isl_hash_byte(hash, bset->n_ineq & 0xFF);
5005 for (i = 0; i < bset->n_ineq; ++i) {
5006 uint32_t c_hash;
5007 c_hash = isl_seq_get_hash(bset->ineq[i], 1 + total);
5008 isl_hash_hash(hash, c_hash);
5010 isl_hash_byte(hash, bset->n_div & 0xFF);
5011 for (i = 0; i < bset->n_div; ++i) {
5012 uint32_t c_hash;
5013 if (isl_int_is_zero(bset->div[i][0]))
5014 continue;
5015 isl_hash_byte(hash, i & 0xFF);
5016 c_hash = isl_seq_get_hash(bset->div[i], 1 + 1 + total);
5017 isl_hash_hash(hash, c_hash);
5019 isl_basic_set_free(bset);
5020 return hash;
5023 uint32_t isl_set_get_hash(struct isl_set *set)
5025 int i;
5026 uint32_t hash;
5028 if (!set)
5029 return 0;
5030 set = isl_set_copy(set);
5031 set = isl_set_normalize(set);
5032 if (!set)
5033 return 0;
5035 hash = isl_hash_init();
5036 for (i = 0; i < set->n; ++i) {
5037 uint32_t bset_hash;
5038 bset_hash = isl_basic_set_get_hash(set->p[i]);
5039 isl_hash_hash(hash, bset_hash);
5042 isl_set_free(set);
5044 return hash;
5047 /* Check if the value for dimension dim is completely determined
5048 * by the values of the other parameters and variables.
5049 * That is, check if dimension dim is involved in an equality.
5051 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
5053 int i;
5054 unsigned nparam;
5056 if (!bset)
5057 return -1;
5058 nparam = isl_basic_set_n_param(bset);
5059 for (i = 0; i < bset->n_eq; ++i)
5060 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
5061 return 1;
5062 return 0;
5065 /* Check if the value for dimension dim is completely determined
5066 * by the values of the other parameters and variables.
5067 * That is, check if dimension dim is involved in an equality
5068 * for each of the subsets.
5070 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
5072 int i;
5074 if (!set)
5075 return -1;
5076 for (i = 0; i < set->n; ++i) {
5077 int unique;
5078 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
5079 if (unique != 1)
5080 return unique;
5082 return 1;