add isl_basic_set_free_div
[isl.git] / isl_map.c
blob29f520dcfe3294be9a667fd306fe151d3668235d
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_list.h"
7 #include "isl_lp.h"
8 #include "isl_seq.h"
9 #include "isl_set.h"
10 #include "isl_map.h"
11 #include "isl_map_private.h"
12 #include "isl_map_piplib.h"
13 #include "isl_sample.h"
14 #include "isl_vec.h"
16 /* Maps dst positions to src positions */
17 struct isl_dim_map {
18 unsigned len;
19 int pos[1];
22 static struct isl_dim_map *isl_dim_map_alloc(struct isl_ctx *ctx, unsigned len)
24 int i;
25 struct isl_dim_map *dim_map;
26 dim_map = isl_alloc(ctx, struct isl_dim_map,
27 sizeof(struct isl_dim_map) + len * sizeof(int));
28 if (!dim_map)
29 return NULL;
30 dim_map->len = 1 + len;
31 dim_map->pos[0] = 0;
32 for (i = 0; i < len; ++i)
33 dim_map->pos[1 + i] = -1;
34 return dim_map;
37 static unsigned n(struct isl_dim *dim, enum isl_dim_type type)
39 switch (type) {
40 case isl_dim_param: return dim->nparam;
41 case isl_dim_in: return dim->n_in;
42 case isl_dim_out: return dim->n_out;
43 case isl_dim_all: return dim->nparam + dim->n_in + dim->n_out;
47 static unsigned pos(struct isl_dim *dim, enum isl_dim_type type)
49 switch (type) {
50 case isl_dim_param: return 1;
51 case isl_dim_in: return 1 + dim->nparam;
52 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
56 static void isl_dim_map_dim(struct isl_dim_map *dim_map, struct isl_dim *dim,
57 enum isl_dim_type type, unsigned dst_pos)
59 int i;
60 unsigned src_pos;
62 if (!dim_map || !dim)
63 return;
65 src_pos = pos(dim, type);
66 for (i = 0; i < n(dim, type); ++i)
67 dim_map->pos[1 + dst_pos + i] = src_pos + i;
70 static void isl_dim_map_div(struct isl_dim_map *dim_map,
71 struct isl_basic_map *bmap, unsigned dst_pos)
73 int i;
74 unsigned src_pos;
76 if (!dim_map || !bmap)
77 return;
79 src_pos = 1 + isl_dim_total(bmap->dim);
80 for (i = 0; i < bmap->n_div; ++i)
81 dim_map->pos[1 + dst_pos + i] = src_pos + i;
84 static void isl_dim_map_dump(struct isl_dim_map *dim_map)
86 int i;
88 for (i = 0; i < dim_map->len; ++i)
89 fprintf(stderr, "%d -> %d; ", i, dim_map->pos[i]);
90 fprintf(stderr, "\n");
93 unsigned isl_basic_map_dim(const struct isl_basic_map *bmap,
94 enum isl_dim_type type)
96 struct isl_dim *dim = bmap->dim;
97 switch (type) {
98 case isl_dim_param:
99 case isl_dim_in:
100 case isl_dim_out: return isl_dim_size(bmap->dim, type);
101 case isl_dim_div: return bmap->n_div;
102 case isl_dim_all: return isl_basic_map_total_dim(bmap);
106 unsigned isl_map_dim(const struct isl_map *map, enum isl_dim_type type)
108 return n(map->dim, type);
111 unsigned isl_set_dim(const struct isl_set *set, enum isl_dim_type type)
113 return n(set->dim, type);
116 unsigned isl_basic_map_offset(struct isl_basic_map *bmap,
117 enum isl_dim_type type)
119 struct isl_dim *dim = bmap->dim;
120 switch (type) {
121 case isl_dim_param: return 1;
122 case isl_dim_in: return 1 + dim->nparam;
123 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
124 case isl_dim_div: return 1 + dim->nparam + dim->n_in + dim->n_out;
128 static unsigned map_offset(struct isl_map *map, enum isl_dim_type type)
130 return pos(map->dim, type);
133 unsigned isl_basic_set_dim(const struct isl_basic_set *bset,
134 enum isl_dim_type type)
136 return isl_basic_map_dim((const struct isl_basic_map*)bset, type);
139 unsigned isl_basic_set_n_dim(const struct isl_basic_set *bset)
141 return bset->dim->n_out;
144 unsigned isl_basic_set_n_param(const struct isl_basic_set *bset)
146 return bset->dim->nparam;
149 unsigned isl_basic_set_total_dim(const struct isl_basic_set *bset)
151 return isl_dim_total(bset->dim) + bset->n_div;
154 unsigned isl_set_n_dim(const struct isl_set *set)
156 return set->dim->n_out;
159 unsigned isl_set_n_param(const struct isl_set *set)
161 return set->dim->nparam;
164 unsigned isl_basic_map_n_in(const struct isl_basic_map *bmap)
166 return bmap->dim->n_in;
169 unsigned isl_basic_map_n_out(const struct isl_basic_map *bmap)
171 return bmap->dim->n_out;
174 unsigned isl_basic_map_n_param(const struct isl_basic_map *bmap)
176 return bmap->dim->nparam;
179 unsigned isl_basic_map_n_div(const struct isl_basic_map *bmap)
181 return bmap->n_div;
184 unsigned isl_basic_map_total_dim(const struct isl_basic_map *bmap)
186 return isl_dim_total(bmap->dim) + bmap->n_div;
189 unsigned isl_map_n_in(const struct isl_map *map)
191 return map->dim->n_in;
194 unsigned isl_map_n_out(const struct isl_map *map)
196 return map->dim->n_out;
199 unsigned isl_map_n_param(const struct isl_map *map)
201 return map->dim->nparam;
204 int isl_map_compatible_domain(struct isl_map *map, struct isl_set *set)
206 return map->dim->n_in == set->dim->n_out &&
207 map->dim->nparam == set->dim->nparam;
210 int isl_basic_map_compatible_domain(struct isl_basic_map *bmap,
211 struct isl_basic_set *bset)
213 return bmap->dim->n_in == bset->dim->n_out &&
214 bmap->dim->nparam == bset->dim->nparam;
217 int isl_basic_map_compatible_range(struct isl_basic_map *bmap,
218 struct isl_basic_set *bset)
220 return bmap->dim->n_out == bset->dim->n_out &&
221 bmap->dim->nparam == bset->dim->nparam;
224 struct isl_dim *isl_basic_map_get_dim(struct isl_basic_map *bmap)
226 if (!bmap)
227 return NULL;
228 return isl_dim_copy(bmap->dim);
231 struct isl_dim *isl_basic_set_get_dim(struct isl_basic_set *bset)
233 if (!bset)
234 return NULL;
235 return isl_dim_copy(bset->dim);
238 struct isl_dim *isl_map_get_dim(struct isl_map *map)
240 if (!map)
241 return NULL;
242 return isl_dim_copy(map->dim);
245 struct isl_dim *isl_set_get_dim(struct isl_set *set)
247 if (!set)
248 return NULL;
249 return isl_dim_copy(set->dim);
252 static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
253 struct isl_basic_map *bmap, unsigned extra,
254 unsigned n_eq, unsigned n_ineq)
256 int i;
257 size_t row_size = 1 + isl_dim_total(bmap->dim) + extra;
259 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
260 if (isl_blk_is_error(bmap->block)) {
261 free(bmap);
262 return NULL;
265 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
266 if (!bmap->ineq) {
267 isl_blk_free(ctx, bmap->block);
268 free(bmap);
269 return NULL;
272 if (extra == 0) {
273 bmap->block2 = isl_blk_empty();
274 bmap->div = NULL;
275 } else {
276 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
277 if (isl_blk_is_error(bmap->block2)) {
278 free(bmap->ineq);
279 isl_blk_free(ctx, bmap->block);
280 free(bmap);
281 return NULL;
284 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
285 if (!bmap->div) {
286 isl_blk_free(ctx, bmap->block2);
287 free(bmap->ineq);
288 isl_blk_free(ctx, bmap->block);
289 free(bmap);
290 return NULL;
294 for (i = 0; i < n_ineq + n_eq; ++i)
295 bmap->ineq[i] = bmap->block.data + i * row_size;
297 for (i = 0; i < extra; ++i)
298 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
300 bmap->ctx = ctx;
301 isl_ctx_ref(ctx);
302 bmap->ref = 1;
303 bmap->flags = 0;
304 bmap->c_size = n_eq + n_ineq;
305 bmap->eq = bmap->ineq + n_ineq;
306 bmap->extra = extra;
307 bmap->n_eq = 0;
308 bmap->n_ineq = 0;
309 bmap->n_div = 0;
310 bmap->sample = NULL;
312 return bmap;
313 error:
314 isl_basic_map_free(bmap);
315 return NULL;
318 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
319 unsigned nparam, unsigned dim, unsigned extra,
320 unsigned n_eq, unsigned n_ineq)
322 struct isl_basic_map *bmap;
323 bmap = isl_basic_map_alloc(ctx, nparam, 0, dim, extra, n_eq, n_ineq);
324 return (struct isl_basic_set *)bmap;
327 struct isl_basic_set *isl_basic_set_alloc_dim(struct isl_dim *dim,
328 unsigned extra, unsigned n_eq, unsigned n_ineq)
330 struct isl_basic_map *bmap;
331 if (!dim)
332 return NULL;
333 isl_assert(dim->ctx, dim->n_in == 0, return NULL);
334 bmap = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
335 return (struct isl_basic_set *)bmap;
338 struct isl_basic_map *isl_basic_map_alloc_dim(struct isl_dim *dim,
339 unsigned extra, unsigned n_eq, unsigned n_ineq)
341 struct isl_basic_map *bmap;
343 if (!dim)
344 return NULL;
345 bmap = isl_alloc_type(dim->ctx, struct isl_basic_map);
346 if (!bmap)
347 goto error;
348 bmap->dim = dim;
350 return basic_map_init(dim->ctx, bmap, extra, n_eq, n_ineq);
351 error:
352 isl_dim_free(dim);
353 return NULL;
356 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
357 unsigned nparam, unsigned in, unsigned out, unsigned extra,
358 unsigned n_eq, unsigned n_ineq)
360 struct isl_basic_map *bmap;
361 struct isl_dim *dim;
363 dim = isl_dim_alloc(ctx, nparam, in, out);
364 if (!dim)
365 return NULL;
367 bmap = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
368 return bmap;
371 static void dup_constraints(
372 struct isl_basic_map *dst, struct isl_basic_map *src)
374 int i;
375 unsigned total = isl_basic_map_total_dim(src);
377 for (i = 0; i < src->n_eq; ++i) {
378 int j = isl_basic_map_alloc_equality(dst);
379 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
382 for (i = 0; i < src->n_ineq; ++i) {
383 int j = isl_basic_map_alloc_inequality(dst);
384 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
387 for (i = 0; i < src->n_div; ++i) {
388 int j = isl_basic_map_alloc_div(dst);
389 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
391 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
394 struct isl_basic_map *isl_basic_map_dup(struct isl_basic_map *bmap)
396 struct isl_basic_map *dup;
398 if (!bmap)
399 return NULL;
400 dup = isl_basic_map_alloc_dim(isl_dim_copy(bmap->dim),
401 bmap->n_div, bmap->n_eq, bmap->n_ineq);
402 if (!dup)
403 return NULL;
404 dup_constraints(dup, bmap);
405 dup->flags = bmap->flags;
406 dup->sample = isl_vec_copy(bmap->sample);
407 return dup;
410 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
412 struct isl_basic_map *dup;
414 dup = isl_basic_map_dup((struct isl_basic_map *)bset);
415 return (struct isl_basic_set *)dup;
418 struct isl_basic_set *isl_basic_set_copy(struct isl_basic_set *bset)
420 if (!bset)
421 return NULL;
423 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
424 bset->ref++;
425 return bset;
427 return isl_basic_set_dup(bset);
430 struct isl_set *isl_set_copy(struct isl_set *set)
432 if (!set)
433 return NULL;
435 set->ref++;
436 return set;
439 struct isl_basic_map *isl_basic_map_copy(struct isl_basic_map *bmap)
441 if (!bmap)
442 return NULL;
444 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
445 bmap->ref++;
446 return bmap;
448 return isl_basic_map_dup(bmap);
451 struct isl_map *isl_map_copy(struct isl_map *map)
453 if (!map)
454 return NULL;
456 map->ref++;
457 return map;
460 void isl_basic_map_free(struct isl_basic_map *bmap)
462 if (!bmap)
463 return;
465 if (--bmap->ref > 0)
466 return;
468 isl_ctx_deref(bmap->ctx);
469 free(bmap->div);
470 isl_blk_free(bmap->ctx, bmap->block2);
471 free(bmap->ineq);
472 isl_blk_free(bmap->ctx, bmap->block);
473 isl_vec_free(bmap->sample);
474 isl_dim_free(bmap->dim);
475 free(bmap);
478 void isl_basic_set_free(struct isl_basic_set *bset)
480 isl_basic_map_free((struct isl_basic_map *)bset);
483 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
485 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
488 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
490 struct isl_ctx *ctx;
491 if (!bmap)
492 return -1;
493 ctx = bmap->ctx;
494 isl_assert(ctx, room_for_con(bmap, 1), return -1);
495 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
496 return -1);
497 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
498 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
499 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
500 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
501 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
502 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
503 isl_int *t;
504 int j = isl_basic_map_alloc_inequality(bmap);
505 if (j < 0)
506 return -1;
507 t = bmap->ineq[j];
508 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
509 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
510 bmap->eq[-1] = t;
511 bmap->n_eq++;
512 bmap->n_ineq--;
513 bmap->eq--;
514 return 0;
516 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
517 bmap->extra - bmap->n_div);
518 return bmap->n_eq++;
521 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
523 return isl_basic_map_alloc_equality((struct isl_basic_map *)bset);
526 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
528 if (!bmap)
529 return -1;
530 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
531 bmap->n_eq -= n;
532 return 0;
535 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
537 return isl_basic_map_free_equality((struct isl_basic_map *)bset, n);
540 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
542 isl_int *t;
543 if (!bmap)
544 return -1;
545 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
547 if (pos != bmap->n_eq - 1) {
548 t = bmap->eq[pos];
549 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
550 bmap->eq[bmap->n_eq - 1] = t;
552 bmap->n_eq--;
553 return 0;
556 int isl_basic_set_drop_equality(struct isl_basic_set *bset, unsigned pos)
558 return isl_basic_map_drop_equality((struct isl_basic_map *)bset, pos);
561 void isl_basic_map_inequality_to_equality(
562 struct isl_basic_map *bmap, unsigned pos)
564 isl_int *t;
566 t = bmap->ineq[pos];
567 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
568 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
569 bmap->eq[-1] = t;
570 bmap->n_eq++;
571 bmap->n_ineq--;
572 bmap->eq--;
573 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
574 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
575 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
576 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
579 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
581 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
584 int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap)
586 struct isl_ctx *ctx;
587 if (!bmap)
588 return -1;
589 ctx = bmap->ctx;
590 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
591 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
592 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
593 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
594 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
595 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
596 1 + isl_basic_map_total_dim(bmap),
597 bmap->extra - bmap->n_div);
598 return bmap->n_ineq++;
601 int isl_basic_set_alloc_inequality(struct isl_basic_set *bset)
603 return isl_basic_map_alloc_inequality((struct isl_basic_map *)bset);
606 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
608 if (!bmap)
609 return -1;
610 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
611 bmap->n_ineq -= n;
612 return 0;
615 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
617 return isl_basic_map_free_inequality((struct isl_basic_map *)bset, n);
620 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
622 isl_int *t;
623 if (!bmap)
624 return -1;
625 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
627 if (pos != bmap->n_ineq - 1) {
628 t = bmap->ineq[pos];
629 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
630 bmap->ineq[bmap->n_ineq - 1] = t;
631 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
633 bmap->n_ineq--;
634 return 0;
637 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
639 return isl_basic_map_drop_inequality((struct isl_basic_map *)bset, pos);
642 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
644 if (!bmap)
645 return -1;
646 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
647 isl_seq_clr(bmap->div[bmap->n_div] +
648 1 + 1 + isl_basic_map_total_dim(bmap),
649 bmap->extra - bmap->n_div);
650 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
651 return bmap->n_div++;
654 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
656 return isl_basic_map_alloc_div((struct isl_basic_map *)bset);
659 int isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
661 if (!bmap)
662 return -1;
663 isl_assert(bmap->ctx, n <= bmap->n_div, return -1);
664 bmap->n_div -= n;
665 return 0;
668 int isl_basic_set_free_div(struct isl_basic_set *bset, unsigned n)
670 return isl_basic_map_free_div((struct isl_basic_map *)bset, n);
673 /* Copy constraint from src to dst, putting the vars of src at offset
674 * dim_off in dst and the divs of src at offset div_off in dst.
675 * If both sets are actually map, then dim_off applies to the input
676 * variables.
678 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
679 struct isl_basic_map *src_map, isl_int *src,
680 unsigned in_off, unsigned out_off, unsigned div_off)
682 unsigned src_nparam = isl_basic_map_n_param(src_map);
683 unsigned dst_nparam = isl_basic_map_n_param(dst_map);
684 unsigned src_in = isl_basic_map_n_in(src_map);
685 unsigned dst_in = isl_basic_map_n_in(dst_map);
686 unsigned src_out = isl_basic_map_n_out(src_map);
687 unsigned dst_out = isl_basic_map_n_out(dst_map);
688 isl_int_set(dst[0], src[0]);
689 isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
690 if (dst_nparam > src_nparam)
691 isl_seq_clr(dst+1+src_nparam,
692 dst_nparam - src_nparam);
693 isl_seq_clr(dst+1+dst_nparam, in_off);
694 isl_seq_cpy(dst+1+dst_nparam+in_off,
695 src+1+src_nparam,
696 isl_min(dst_in-in_off, src_in));
697 if (dst_in-in_off > src_in)
698 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
699 dst_in - in_off - src_in);
700 isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
701 isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
702 src+1+src_nparam+src_in,
703 isl_min(dst_out-out_off, src_out));
704 if (dst_out-out_off > src_out)
705 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
706 dst_out - out_off - src_out);
707 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
708 isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
709 src+1+src_nparam+src_in+src_out,
710 isl_min(dst_map->extra-div_off, src_map->n_div));
711 if (dst_map->n_div-div_off > src_map->n_div)
712 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
713 div_off+src_map->n_div,
714 dst_map->n_div - div_off - src_map->n_div);
717 static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
718 struct isl_basic_map *src_map, isl_int *src,
719 unsigned in_off, unsigned out_off, unsigned div_off)
721 isl_int_set(dst[0], src[0]);
722 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
725 static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
726 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
728 int i;
729 unsigned div_off;
731 if (!bmap1 || !bmap2)
732 goto error;
734 div_off = bmap1->n_div;
736 for (i = 0; i < bmap2->n_eq; ++i) {
737 int i1 = isl_basic_map_alloc_equality(bmap1);
738 if (i1 < 0)
739 goto error;
740 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
741 i_pos, o_pos, div_off);
744 for (i = 0; i < bmap2->n_ineq; ++i) {
745 int i1 = isl_basic_map_alloc_inequality(bmap1);
746 if (i1 < 0)
747 goto error;
748 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
749 i_pos, o_pos, div_off);
752 for (i = 0; i < bmap2->n_div; ++i) {
753 int i1 = isl_basic_map_alloc_div(bmap1);
754 if (i1 < 0)
755 goto error;
756 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
757 i_pos, o_pos, div_off);
760 isl_basic_map_free(bmap2);
762 return bmap1;
764 error:
765 isl_basic_map_free(bmap1);
766 isl_basic_map_free(bmap2);
767 return NULL;
770 static void copy_constraint_dim_map(isl_int *dst, isl_int *src,
771 struct isl_dim_map *dim_map)
773 int i;
775 for (i = 0; i < dim_map->len; ++i) {
776 if (dim_map->pos[i] < 0)
777 isl_int_set_si(dst[i], 0);
778 else
779 isl_int_set(dst[i], src[dim_map->pos[i]]);
783 static void copy_div_dim_map(isl_int *dst, isl_int *src,
784 struct isl_dim_map *dim_map)
786 isl_int_set(dst[0], src[0]);
787 copy_constraint_dim_map(dst+1, src+1, dim_map);
790 static struct isl_basic_map *add_constraints_dim_map(struct isl_basic_map *dst,
791 struct isl_basic_map *src, struct isl_dim_map *dim_map)
793 int i;
795 if (!src || !dst || !dim_map)
796 goto error;
798 for (i = 0; i < src->n_eq; ++i) {
799 int i1 = isl_basic_map_alloc_equality(dst);
800 if (i1 < 0)
801 goto error;
802 copy_constraint_dim_map(dst->eq[i1], src->eq[i], dim_map);
805 for (i = 0; i < src->n_ineq; ++i) {
806 int i1 = isl_basic_map_alloc_inequality(dst);
807 if (i1 < 0)
808 goto error;
809 copy_constraint_dim_map(dst->ineq[i1], src->ineq[i], dim_map);
812 for (i = 0; i < src->n_div; ++i) {
813 int i1 = isl_basic_map_alloc_div(dst);
814 if (i1 < 0)
815 goto error;
816 copy_div_dim_map(dst->div[i1], src->div[i], dim_map);
819 free(dim_map);
820 isl_basic_map_free(src);
822 return dst;
823 error:
824 free(dim_map);
825 isl_basic_map_free(src);
826 isl_basic_map_free(dst);
827 return NULL;
830 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
831 struct isl_basic_set *bset2, unsigned pos)
833 return (struct isl_basic_set *)
834 add_constraints((struct isl_basic_map *)bset1,
835 (struct isl_basic_map *)bset2, 0, pos);
838 struct isl_basic_map *isl_basic_map_extend_dim(struct isl_basic_map *base,
839 struct isl_dim *dim, unsigned extra,
840 unsigned n_eq, unsigned n_ineq)
842 struct isl_basic_map *ext;
843 unsigned flags;
844 int dims_ok;
846 if (!dim)
847 goto error;
849 if (!base)
850 goto error;
852 dims_ok = isl_dim_equal(base->dim, dim) &&
853 base->extra >= base->n_div + extra;
855 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
856 room_for_ineq(base, n_ineq)) {
857 isl_dim_free(dim);
858 return base;
861 isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error);
862 isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error);
863 isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error);
864 extra += base->extra;
865 n_eq += base->n_eq;
866 n_ineq += base->n_ineq;
868 ext = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
869 dim = NULL;
870 if (!ext)
871 goto error;
873 flags = base->flags;
874 ext = add_constraints(ext, base, 0, 0);
875 if (ext) {
876 ext->flags = flags;
877 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
880 return ext;
882 error:
883 isl_dim_free(dim);
884 isl_basic_map_free(base);
885 return NULL;
888 struct isl_basic_set *isl_basic_set_extend_dim(struct isl_basic_set *base,
889 struct isl_dim *dim, unsigned extra,
890 unsigned n_eq, unsigned n_ineq)
892 return (struct isl_basic_set *)
893 isl_basic_map_extend_dim((struct isl_basic_map *)base, dim,
894 extra, n_eq, n_ineq);
897 struct isl_basic_map *isl_basic_map_extend_constraints(
898 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
900 if (!base)
901 return NULL;
902 return isl_basic_map_extend_dim(base, isl_dim_copy(base->dim),
903 0, n_eq, n_ineq);
906 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
907 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
908 unsigned n_eq, unsigned n_ineq)
910 struct isl_basic_map *bmap;
911 struct isl_dim *dim;
913 if (!base)
914 return NULL;
915 dim = isl_dim_alloc(base->ctx, nparam, n_in, n_out);
916 if (!dim)
917 return NULL;
919 bmap = isl_basic_map_extend_dim(base, dim, extra, n_eq, n_ineq);
920 return bmap;
923 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
924 unsigned nparam, unsigned dim, unsigned extra,
925 unsigned n_eq, unsigned n_ineq)
927 return (struct isl_basic_set *)
928 isl_basic_map_extend((struct isl_basic_map *)base,
929 nparam, 0, dim, extra, n_eq, n_ineq);
932 struct isl_basic_set *isl_basic_set_extend_constraints(
933 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
935 return (struct isl_basic_set *)
936 isl_basic_map_extend_constraints((struct isl_basic_map *)base,
937 n_eq, n_ineq);
940 struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset)
942 return (struct isl_basic_set *)
943 isl_basic_map_cow((struct isl_basic_map *)bset);
946 struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
948 if (!bmap)
949 return NULL;
951 if (bmap->ref > 1) {
952 bmap->ref--;
953 bmap = isl_basic_map_dup(bmap);
955 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
956 return bmap;
959 struct isl_set *isl_set_cow(struct isl_set *set)
961 if (!set)
962 return NULL;
964 if (set->ref == 1)
965 return set;
966 set->ref--;
967 return isl_set_dup(set);
970 struct isl_map *isl_map_cow(struct isl_map *map)
972 if (!map)
973 return NULL;
975 if (map->ref == 1)
976 return map;
977 map->ref--;
978 return isl_map_dup(map);
981 static void swap_vars(struct isl_blk blk, isl_int *a,
982 unsigned a_len, unsigned b_len)
984 isl_seq_cpy(blk.data, a+a_len, b_len);
985 isl_seq_cpy(blk.data+b_len, a, a_len);
986 isl_seq_cpy(a, blk.data, b_len+a_len);
989 struct isl_basic_set *isl_basic_set_swap_vars(
990 struct isl_basic_set *bset, unsigned n)
992 int i;
993 struct isl_blk blk;
994 unsigned dim;
995 unsigned nparam;
997 if (!bset)
998 goto error;
1000 nparam = isl_basic_set_n_param(bset);
1001 dim = isl_basic_set_n_dim(bset);
1002 isl_assert(bset->ctx, n <= dim, goto error);
1004 if (n == dim)
1005 return bset;
1007 bset = isl_basic_set_cow(bset);
1008 if (!bset)
1009 return NULL;
1011 blk = isl_blk_alloc(bset->ctx, dim);
1012 if (isl_blk_is_error(blk))
1013 goto error;
1015 for (i = 0; i < bset->n_eq; ++i)
1016 swap_vars(blk,
1017 bset->eq[i]+1+nparam, n, dim - n);
1019 for (i = 0; i < bset->n_ineq; ++i)
1020 swap_vars(blk,
1021 bset->ineq[i]+1+nparam, n, dim - n);
1023 for (i = 0; i < bset->n_div; ++i)
1024 swap_vars(blk,
1025 bset->div[i]+1+1+nparam, n, dim - n);
1027 isl_blk_free(bset->ctx, blk);
1029 ISL_F_CLR(bset, ISL_BASIC_SET_NORMALIZED);
1030 return bset;
1032 error:
1033 isl_basic_set_free(bset);
1034 return NULL;
1037 struct isl_set *isl_set_swap_vars(struct isl_set *set, unsigned n)
1039 int i;
1040 set = isl_set_cow(set);
1041 if (!set)
1042 return NULL;
1044 for (i = 0; i < set->n; ++i) {
1045 set->p[i] = isl_basic_set_swap_vars(set->p[i], n);
1046 if (!set->p[i]) {
1047 isl_set_free(set);
1048 return NULL;
1051 ISL_F_CLR(set, ISL_SET_NORMALIZED);
1052 return set;
1055 struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
1057 int i = 0;
1058 unsigned total;
1059 if (!bmap)
1060 goto error;
1061 total = isl_basic_map_total_dim(bmap);
1062 isl_basic_map_free_div(bmap, bmap->n_div);
1063 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1064 if (bmap->n_eq > 0)
1065 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
1066 else {
1067 isl_basic_map_alloc_equality(bmap);
1068 if (i < 0)
1069 goto error;
1071 isl_int_set_si(bmap->eq[i][0], 1);
1072 isl_seq_clr(bmap->eq[i]+1, total);
1073 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
1074 return isl_basic_map_finalize(bmap);
1075 error:
1076 isl_basic_map_free(bmap);
1077 return NULL;
1080 struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
1082 return (struct isl_basic_set *)
1083 isl_basic_map_set_to_empty((struct isl_basic_map *)bset);
1086 static void swap_div(struct isl_basic_map *bmap, int a, int b)
1088 int i;
1089 unsigned off = isl_dim_total(bmap->dim);
1090 isl_int *t = bmap->div[a];
1091 bmap->div[a] = bmap->div[b];
1092 bmap->div[b] = t;
1094 for (i = 0; i < bmap->n_eq; ++i)
1095 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
1097 for (i = 0; i < bmap->n_ineq; ++i)
1098 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
1100 for (i = 0; i < bmap->n_div; ++i)
1101 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
1102 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1105 /* Eliminate the specified n dimensions starting at first from the
1106 * constraints using Fourier-Motzkin, The dimensions themselves
1107 * are not removed.
1109 struct isl_set *isl_set_eliminate_dims(struct isl_set *set,
1110 unsigned first, unsigned n)
1112 int i;
1113 unsigned nparam;
1115 if (!set)
1116 return NULL;
1117 if (n == 0)
1118 return set;
1120 set = isl_set_cow(set);
1121 if (!set)
1122 return NULL;
1123 isl_assert(set->ctx, first+n <= isl_set_n_dim(set), goto error);
1124 nparam = isl_set_n_param(set);
1126 for (i = 0; i < set->n; ++i) {
1127 set->p[i] = isl_basic_set_eliminate_vars(set->p[i],
1128 nparam + first, n);
1129 if (!set->p[i])
1130 goto error;
1132 return set;
1133 error:
1134 isl_set_free(set);
1135 return NULL;
1138 /* Project out n dimensions starting at first using Fourier-Motzkin */
1139 struct isl_set *isl_set_remove_dims(struct isl_set *set,
1140 unsigned first, unsigned n)
1142 set = isl_set_eliminate_dims(set, first, n);
1143 set = isl_set_drop_dims(set, first, n);
1144 return set;
1147 struct isl_basic_set *isl_basic_set_remove_divs(struct isl_basic_set *bset)
1149 bset = isl_basic_set_eliminate_vars(bset, isl_dim_total(bset->dim),
1150 bset->n_div);
1151 if (!bset)
1152 return NULL;
1153 bset->n_div = 0;
1154 return bset;
1157 struct isl_set *isl_set_remove_divs(struct isl_set *set)
1159 int i;
1161 if (!set)
1162 return NULL;
1163 if (set->n == 0)
1164 return set;
1166 set = isl_set_cow(set);
1167 if (!set)
1168 return NULL;
1170 for (i = 0; i < set->n; ++i) {
1171 set->p[i] = isl_basic_set_remove_divs(set->p[i]);
1172 if (!set->p[i])
1173 goto error;
1175 return set;
1176 error:
1177 isl_set_free(set);
1178 return NULL;
1181 struct isl_basic_map *isl_basic_map_remove(struct isl_basic_map *bmap,
1182 enum isl_dim_type type, unsigned first, unsigned n)
1184 if (!bmap)
1185 return NULL;
1186 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1187 goto error);
1188 if (n == 0)
1189 return bmap;
1190 bmap = isl_basic_map_eliminate_vars(bmap,
1191 isl_basic_map_offset(bmap, type) - 1 + first, n);
1192 bmap = isl_basic_map_drop(bmap, type, first, n);
1193 return bmap;
1194 error:
1195 isl_basic_map_free(bmap);
1196 return NULL;
1199 struct isl_map *isl_map_remove(struct isl_map *map,
1200 enum isl_dim_type type, unsigned first, unsigned n)
1202 int i;
1203 unsigned nparam;
1205 if (n == 0)
1206 return map;
1208 map = isl_map_cow(map);
1209 if (!map)
1210 return NULL;
1211 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
1213 for (i = 0; i < map->n; ++i) {
1214 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
1215 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
1216 if (!map->p[i])
1217 goto error;
1219 map = isl_map_drop(map, type, first, n);
1220 return map;
1221 error:
1222 isl_map_free(map);
1223 return NULL;
1226 /* Project out n inputs starting at first using Fourier-Motzkin */
1227 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
1228 unsigned first, unsigned n)
1230 return isl_map_remove(map, isl_dim_in, first, n);
1233 /* Project out n dimensions starting at first using Fourier-Motzkin */
1234 struct isl_basic_set *isl_basic_set_remove_dims(struct isl_basic_set *bset,
1235 unsigned first, unsigned n)
1237 unsigned nparam = isl_basic_set_n_param(bset);
1238 bset = isl_basic_set_eliminate_vars(bset, nparam + first, n);
1239 bset = isl_basic_set_drop_dims(bset, first, n);
1240 return bset;
1243 static void dump_term(struct isl_basic_map *bmap,
1244 isl_int c, int pos, FILE *out)
1246 const char *name;
1247 unsigned in = isl_basic_map_n_in(bmap);
1248 unsigned dim = in + isl_basic_map_n_out(bmap);
1249 unsigned nparam = isl_basic_map_n_param(bmap);
1250 if (!pos)
1251 isl_int_print(out, c, 0);
1252 else {
1253 if (!isl_int_is_one(c))
1254 isl_int_print(out, c, 0);
1255 if (pos < 1 + nparam) {
1256 name = isl_dim_get_name(bmap->dim,
1257 isl_dim_param, pos - 1);
1258 if (name)
1259 fprintf(out, "%s", name);
1260 else
1261 fprintf(out, "p%d", pos - 1);
1262 } else if (pos < 1 + nparam + in)
1263 fprintf(out, "i%d", pos - 1 - nparam);
1264 else if (pos < 1 + nparam + dim)
1265 fprintf(out, "o%d", pos - 1 - nparam - in);
1266 else
1267 fprintf(out, "e%d", pos - 1 - nparam - dim);
1271 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
1272 int sign, FILE *out)
1274 int i;
1275 int first;
1276 unsigned len = 1 + isl_basic_map_total_dim(bmap);
1277 isl_int v;
1279 isl_int_init(v);
1280 for (i = 0, first = 1; i < len; ++i) {
1281 if (isl_int_sgn(c[i]) * sign <= 0)
1282 continue;
1283 if (!first)
1284 fprintf(out, " + ");
1285 first = 0;
1286 isl_int_abs(v, c[i]);
1287 dump_term(bmap, v, i, out);
1289 isl_int_clear(v);
1290 if (first)
1291 fprintf(out, "0");
1294 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
1295 const char *op, FILE *out, int indent)
1297 int i;
1299 fprintf(out, "%*s", indent, "");
1301 dump_constraint_sign(bmap, c, 1, out);
1302 fprintf(out, " %s ", op);
1303 dump_constraint_sign(bmap, c, -1, out);
1305 fprintf(out, "\n");
1307 for (i = bmap->n_div; i < bmap->extra; ++i) {
1308 if (isl_int_is_zero(c[1+isl_dim_total(bmap->dim)+i]))
1309 continue;
1310 fprintf(out, "%*s", indent, "");
1311 fprintf(out, "ERROR: unused div coefficient not zero\n");
1312 abort();
1316 static void dump_constraints(struct isl_basic_map *bmap,
1317 isl_int **c, unsigned n,
1318 const char *op, FILE *out, int indent)
1320 int i;
1322 for (i = 0; i < n; ++i)
1323 dump_constraint(bmap, c[i], op, out, indent);
1326 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
1328 int j;
1329 int first = 1;
1330 unsigned total = isl_basic_map_total_dim(bmap);
1332 for (j = 0; j < 1 + total; ++j) {
1333 if (isl_int_is_zero(exp[j]))
1334 continue;
1335 if (!first && isl_int_is_pos(exp[j]))
1336 fprintf(out, "+");
1337 dump_term(bmap, exp[j], j, out);
1338 first = 0;
1342 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
1344 int i;
1346 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
1347 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
1349 for (i = 0; i < bmap->n_div; ++i) {
1350 fprintf(out, "%*s", indent, "");
1351 fprintf(out, "e%d = [(", i);
1352 dump_affine(bmap, bmap->div[i]+1, out);
1353 fprintf(out, ")/");
1354 isl_int_print(out, bmap->div[i][0], 0);
1355 fprintf(out, "]\n");
1359 void isl_basic_set_dump(struct isl_basic_set *bset, FILE *out, int indent)
1361 if (!bset) {
1362 fprintf(out, "null basic set\n");
1363 return;
1366 fprintf(out, "%*s", indent, "");
1367 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
1368 bset->ref, bset->dim->nparam, bset->dim->n_out,
1369 bset->extra, bset->flags);
1370 dump((struct isl_basic_map *)bset, out, indent);
1373 void isl_basic_map_dump(struct isl_basic_map *bmap, FILE *out, int indent)
1375 if (!bmap) {
1376 fprintf(out, "null basic map\n");
1377 return;
1380 fprintf(out, "%*s", indent, "");
1381 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
1382 "flags: %x, n_name: %d\n",
1383 bmap->ref,
1384 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
1385 bmap->extra, bmap->flags, bmap->dim->n_name);
1386 dump(bmap, out, indent);
1389 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
1391 unsigned total;
1392 if (!bmap)
1393 return -1;
1394 total = isl_basic_map_total_dim(bmap);
1395 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1396 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
1397 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
1398 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1399 return 0;
1402 struct isl_set *isl_set_alloc_dim(struct isl_dim *dim, int n, unsigned flags)
1404 struct isl_set *set;
1406 if (!dim)
1407 return NULL;
1408 isl_assert(dim->ctx, dim->n_in == 0, return NULL);
1409 isl_assert(dim->ctx, n >= 0, return NULL);
1410 set = isl_alloc(dim->ctx, struct isl_set,
1411 sizeof(struct isl_set) +
1412 n * sizeof(struct isl_basic_set *));
1413 if (!set)
1414 goto error;
1416 set->ctx = dim->ctx;
1417 isl_ctx_ref(set->ctx);
1418 set->ref = 1;
1419 set->size = n;
1420 set->n = 0;
1421 set->dim = dim;
1422 set->flags = flags;
1423 return set;
1424 error:
1425 isl_dim_free(dim);
1426 return NULL;
1429 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
1430 unsigned nparam, unsigned dim, int n, unsigned flags)
1432 struct isl_set *set;
1433 struct isl_dim *dims;
1435 dims = isl_dim_alloc(ctx, nparam, 0, dim);
1436 if (!dims)
1437 return NULL;
1439 set = isl_set_alloc_dim(dims, n, flags);
1440 return set;
1443 /* Make sure "map" has room for at least "n" more basic maps.
1445 struct isl_map *isl_map_grow(struct isl_map *map, int n)
1447 int i;
1448 struct isl_map *grown = NULL;
1450 if (!map)
1451 return NULL;
1452 isl_assert(map->ctx, n >= 0, goto error);
1453 if (map->n + n <= map->size)
1454 return map;
1455 grown = isl_map_alloc_dim(isl_map_get_dim(map), map->n + n, map->flags);
1456 if (!grown)
1457 goto error;
1458 for (i = 0; i < map->n; ++i) {
1459 grown->p[i] = isl_basic_map_copy(map->p[i]);
1460 if (!grown->p[i])
1461 goto error;
1462 grown->n++;
1464 isl_map_free(map);
1465 return grown;
1466 error:
1467 isl_map_free(grown);
1468 isl_map_free(map);
1469 return NULL;
1472 /* Make sure "set" has room for at least "n" more basic sets.
1474 struct isl_set *isl_set_grow(struct isl_set *set, int n)
1476 return (struct isl_set *)isl_map_grow((struct isl_map *)set, n);
1479 struct isl_set *isl_set_dup(struct isl_set *set)
1481 int i;
1482 struct isl_set *dup;
1484 if (!set)
1485 return NULL;
1487 dup = isl_set_alloc_dim(isl_dim_copy(set->dim), set->n, set->flags);
1488 if (!dup)
1489 return NULL;
1490 for (i = 0; i < set->n; ++i)
1491 dup = isl_set_add(dup, isl_basic_set_copy(set->p[i]));
1492 return dup;
1495 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
1497 struct isl_set *set;
1499 if (!bset)
1500 return NULL;
1502 set = isl_set_alloc_dim(isl_dim_copy(bset->dim), 1, ISL_MAP_DISJOINT);
1503 if (!set) {
1504 isl_basic_set_free(bset);
1505 return NULL;
1507 return isl_set_add(set, bset);
1510 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
1512 struct isl_map *map;
1514 if (!bmap)
1515 return NULL;
1517 map = isl_map_alloc_dim(isl_dim_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
1518 if (!map) {
1519 isl_basic_map_free(bmap);
1520 return NULL;
1522 return isl_map_add(map, bmap);
1525 struct isl_set *isl_set_add(struct isl_set *set, struct isl_basic_set *bset)
1527 if (!bset || !set)
1528 goto error;
1529 isl_assert(set->ctx, isl_dim_equal(set->dim, bset->dim), goto error);
1530 isl_assert(set->ctx, set->n < set->size, goto error);
1531 set->p[set->n] = bset;
1532 set->n++;
1533 return set;
1534 error:
1535 if (set)
1536 isl_set_free(set);
1537 if (bset)
1538 isl_basic_set_free(bset);
1539 return NULL;
1542 void isl_set_free(struct isl_set *set)
1544 int i;
1546 if (!set)
1547 return;
1549 if (--set->ref > 0)
1550 return;
1552 isl_ctx_deref(set->ctx);
1553 for (i = 0; i < set->n; ++i)
1554 isl_basic_set_free(set->p[i]);
1555 isl_dim_free(set->dim);
1556 free(set);
1559 void isl_set_dump(struct isl_set *set, FILE *out, int indent)
1561 int i;
1563 if (!set) {
1564 fprintf(out, "null set\n");
1565 return;
1568 fprintf(out, "%*s", indent, "");
1569 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
1570 set->ref, set->n, set->dim->nparam, set->dim->n_out,
1571 set->flags);
1572 for (i = 0; i < set->n; ++i) {
1573 fprintf(out, "%*s", indent, "");
1574 fprintf(out, "basic set %d:\n", i);
1575 isl_basic_set_dump(set->p[i], out, indent+4);
1579 void isl_map_dump(struct isl_map *map, FILE *out, int indent)
1581 int i;
1583 if (!map) {
1584 fprintf(out, "null map\n");
1585 return;
1588 fprintf(out, "%*s", indent, "");
1589 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
1590 "flags: %x, n_name: %d\n",
1591 map->ref, map->n, map->dim->nparam, map->dim->n_in,
1592 map->dim->n_out, map->flags, map->dim->n_name);
1593 for (i = 0; i < map->n; ++i) {
1594 fprintf(out, "%*s", indent, "");
1595 fprintf(out, "basic map %d:\n", i);
1596 isl_basic_map_dump(map->p[i], out, indent+4);
1600 struct isl_basic_map *isl_basic_map_intersect_domain(
1601 struct isl_basic_map *bmap, struct isl_basic_set *bset)
1603 struct isl_basic_map *bmap_domain;
1604 struct isl_dim *dim;
1606 if (!bmap || !bset)
1607 goto error;
1609 isl_assert(bset->ctx, isl_dim_match(bmap->dim, isl_dim_param,
1610 bset->dim, isl_dim_param), goto error);
1612 if (isl_dim_size(bset->dim, isl_dim_set) != 0)
1613 isl_assert(bset->ctx,
1614 isl_basic_map_compatible_domain(bmap, bset), goto error);
1616 bmap = isl_basic_map_cow(bmap);
1617 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
1618 bset->n_div, bset->n_eq, bset->n_ineq);
1619 if (!bmap)
1620 goto error;
1621 dim = isl_dim_reverse(isl_dim_copy(bset->dim));
1622 bmap_domain = isl_basic_map_from_basic_set(bset, dim);
1623 bmap = add_constraints(bmap, bmap_domain, 0, 0);
1625 bmap = isl_basic_map_simplify(bmap);
1626 return isl_basic_map_finalize(bmap);
1627 error:
1628 isl_basic_map_free(bmap);
1629 isl_basic_set_free(bset);
1630 return NULL;
1633 struct isl_basic_map *isl_basic_map_intersect_range(
1634 struct isl_basic_map *bmap, struct isl_basic_set *bset)
1636 struct isl_basic_map *bmap_range;
1638 if (!bmap || !bset)
1639 goto error;
1641 isl_assert(bset->ctx, isl_dim_match(bmap->dim, isl_dim_param,
1642 bset->dim, isl_dim_param), goto error);
1644 if (isl_dim_size(bset->dim, isl_dim_set) != 0)
1645 isl_assert(bset->ctx,
1646 isl_basic_map_compatible_range(bmap, bset), goto error);
1648 bmap = isl_basic_map_cow(bmap);
1649 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
1650 bset->n_div, bset->n_eq, bset->n_ineq);
1651 if (!bmap)
1652 goto error;
1653 bmap_range = isl_basic_map_from_basic_set(bset, isl_dim_copy(bset->dim));
1654 bmap = add_constraints(bmap, bmap_range, 0, 0);
1656 bmap = isl_basic_map_simplify(bmap);
1657 return isl_basic_map_finalize(bmap);
1658 error:
1659 isl_basic_map_free(bmap);
1660 isl_basic_set_free(bset);
1661 return NULL;
1664 static int basic_map_contains(struct isl_basic_map *bmap, struct isl_vec *vec)
1666 int i;
1667 unsigned total;
1668 isl_int s;
1670 total = 1 + isl_basic_map_total_dim(bmap);
1671 if (total != vec->size)
1672 return -1;
1674 isl_int_init(s);
1676 for (i = 0; i < bmap->n_eq; ++i) {
1677 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
1678 if (!isl_int_is_zero(s)) {
1679 isl_int_clear(s);
1680 return 0;
1684 for (i = 0; i < bmap->n_ineq; ++i) {
1685 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
1686 if (isl_int_is_neg(s)) {
1687 isl_int_clear(s);
1688 return 0;
1692 isl_int_clear(s);
1694 return 1;
1697 int isl_basic_set_contains(struct isl_basic_set *bset, struct isl_vec *vec)
1699 return basic_map_contains((struct isl_basic_map *)bset, vec);
1702 struct isl_basic_map *isl_basic_map_intersect(
1703 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
1705 struct isl_vec *sample = NULL;
1707 if (!bmap1 || !bmap2)
1708 goto error;
1710 isl_assert(bmap1->ctx, isl_dim_match(bmap1->dim, isl_dim_param,
1711 bmap2->dim, isl_dim_param), goto error);
1712 if (isl_dim_total(bmap1->dim) ==
1713 isl_dim_size(bmap1->dim, isl_dim_param) &&
1714 isl_dim_total(bmap2->dim) !=
1715 isl_dim_size(bmap2->dim, isl_dim_param))
1716 return isl_basic_map_intersect(bmap2, bmap1);
1718 if (isl_dim_total(bmap2->dim) !=
1719 isl_dim_size(bmap2->dim, isl_dim_param))
1720 isl_assert(bmap1->ctx,
1721 isl_dim_equal(bmap1->dim, bmap2->dim), goto error);
1723 if (bmap1->sample &&
1724 basic_map_contains(bmap1, bmap1->sample) > 0 &&
1725 basic_map_contains(bmap2, bmap1->sample) > 0)
1726 sample = isl_vec_copy(bmap1->sample);
1727 else if (bmap2->sample &&
1728 basic_map_contains(bmap1, bmap2->sample) > 0 &&
1729 basic_map_contains(bmap2, bmap2->sample) > 0)
1730 sample = isl_vec_copy(bmap2->sample);
1732 bmap1 = isl_basic_map_cow(bmap1);
1733 bmap1 = isl_basic_map_extend_dim(bmap1, isl_dim_copy(bmap1->dim),
1734 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
1735 if (!bmap1)
1736 goto error;
1737 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
1739 if (sample) {
1740 isl_vec_free(bmap1->sample);
1741 bmap1->sample = sample;
1744 bmap1 = isl_basic_map_simplify(bmap1);
1745 return isl_basic_map_finalize(bmap1);
1746 error:
1747 if (sample)
1748 isl_vec_free(sample);
1749 isl_basic_map_free(bmap1);
1750 isl_basic_map_free(bmap2);
1751 return NULL;
1754 struct isl_basic_set *isl_basic_set_intersect(
1755 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
1757 return (struct isl_basic_set *)
1758 isl_basic_map_intersect(
1759 (struct isl_basic_map *)bset1,
1760 (struct isl_basic_map *)bset2);
1763 struct isl_map *isl_map_intersect(struct isl_map *map1, struct isl_map *map2)
1765 unsigned flags = 0;
1766 struct isl_map *result;
1767 int i, j;
1769 if (!map1 || !map2)
1770 goto error;
1772 isl_assert(map1->ctx, isl_dim_match(map1->dim, isl_dim_param,
1773 map2->dim, isl_dim_param), goto error);
1774 if (isl_dim_total(map1->dim) ==
1775 isl_dim_size(map1->dim, isl_dim_param) &&
1776 isl_dim_total(map2->dim) != isl_dim_size(map2->dim, isl_dim_param))
1777 return isl_map_intersect(map2, map1);
1779 if (isl_dim_total(map2->dim) != isl_dim_size(map2->dim, isl_dim_param))
1780 isl_assert(map1->ctx,
1781 isl_dim_equal(map1->dim, map2->dim), goto error);
1783 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
1784 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
1785 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
1787 result = isl_map_alloc_dim(isl_dim_copy(map1->dim),
1788 map1->n * map2->n, flags);
1789 if (!result)
1790 goto error;
1791 for (i = 0; i < map1->n; ++i)
1792 for (j = 0; j < map2->n; ++j) {
1793 struct isl_basic_map *part;
1794 part = isl_basic_map_intersect(
1795 isl_basic_map_copy(map1->p[i]),
1796 isl_basic_map_copy(map2->p[j]));
1797 if (isl_basic_map_is_empty(part))
1798 isl_basic_map_free(part);
1799 else
1800 result = isl_map_add(result, part);
1801 if (!result)
1802 goto error;
1804 isl_map_free(map1);
1805 isl_map_free(map2);
1806 return result;
1807 error:
1808 isl_map_free(map1);
1809 isl_map_free(map2);
1810 return NULL;
1813 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
1815 return (struct isl_set *)
1816 isl_map_intersect((struct isl_map *)set1,
1817 (struct isl_map *)set2);
1820 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
1822 struct isl_dim *dim;
1823 struct isl_basic_set *bset;
1824 unsigned in;
1826 if (!bmap)
1827 return NULL;
1828 bmap = isl_basic_map_cow(bmap);
1829 if (!bmap)
1830 return NULL;
1831 dim = isl_dim_reverse(isl_dim_copy(bmap->dim));
1832 in = isl_basic_map_n_in(bmap);
1833 bset = isl_basic_set_from_basic_map(bmap);
1834 bset = isl_basic_set_swap_vars(bset, in);
1835 return isl_basic_map_from_basic_set(bset, dim);
1838 /* Turn final n dimensions into existentially quantified variables.
1840 struct isl_basic_set *isl_basic_set_project_out(
1841 struct isl_basic_set *bset, unsigned n, unsigned flags)
1843 int i;
1844 size_t row_size;
1845 isl_int **new_div;
1846 isl_int *old;
1848 if (!bset)
1849 return NULL;
1851 isl_assert(bset->ctx, n <= isl_basic_set_n_dim(bset), goto error);
1853 if (n == 0)
1854 return bset;
1856 bset = isl_basic_set_cow(bset);
1858 row_size = 1 + isl_dim_total(bset->dim) + bset->extra;
1859 old = bset->block2.data;
1860 bset->block2 = isl_blk_extend(bset->ctx, bset->block2,
1861 (bset->extra + n) * (1 + row_size));
1862 if (!bset->block2.data)
1863 goto error;
1864 new_div = isl_alloc_array(ctx, isl_int *, bset->extra + n);
1865 if (!new_div)
1866 goto error;
1867 for (i = 0; i < n; ++i) {
1868 new_div[i] = bset->block2.data +
1869 (bset->extra + i) * (1 + row_size);
1870 isl_seq_clr(new_div[i], 1 + row_size);
1872 for (i = 0; i < bset->extra; ++i)
1873 new_div[n + i] = bset->block2.data + (bset->div[i] - old);
1874 free(bset->div);
1875 bset->div = new_div;
1876 bset->n_div += n;
1877 bset->extra += n;
1878 bset->dim = isl_dim_drop_outputs(bset->dim,
1879 isl_basic_set_n_dim(bset) - n, n);
1880 if (!bset->dim)
1881 goto error;
1882 bset = isl_basic_set_simplify(bset);
1883 return isl_basic_set_finalize(bset);
1884 error:
1885 isl_basic_set_free(bset);
1886 return NULL;
1889 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
1891 int i, j;
1893 for (i = 0; i < n; ++i) {
1894 j = isl_basic_map_alloc_div(bmap);
1895 if (j < 0)
1896 goto error;
1897 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
1899 return bmap;
1900 error:
1901 isl_basic_map_free(bmap);
1902 return NULL;
1905 struct isl_basic_map *isl_basic_map_apply_range(
1906 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
1908 struct isl_dim *dim_result = NULL;
1909 struct isl_basic_map *bmap;
1910 unsigned n_in, n_out, n, nparam, total, pos;
1911 struct isl_dim_map *dim_map1, *dim_map2;
1913 if (!bmap1 || !bmap2)
1914 goto error;
1916 dim_result = isl_dim_join(isl_dim_copy(bmap1->dim),
1917 isl_dim_copy(bmap2->dim));
1919 n_in = isl_basic_map_n_in(bmap1);
1920 n_out = isl_basic_map_n_out(bmap2);
1921 n = isl_basic_map_n_out(bmap1);
1922 nparam = isl_basic_map_n_param(bmap1);
1924 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
1925 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
1926 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
1927 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
1928 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
1929 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
1930 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
1931 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
1932 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
1933 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
1934 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
1936 bmap = isl_basic_map_alloc_dim(dim_result,
1937 bmap1->n_div + bmap2->n_div + n,
1938 bmap1->n_eq + bmap2->n_eq,
1939 bmap1->n_ineq + bmap2->n_ineq);
1940 bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
1941 bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
1942 bmap = add_divs(bmap, n);
1943 bmap = isl_basic_map_simplify(bmap);
1944 return isl_basic_map_finalize(bmap);
1945 error:
1946 isl_basic_map_free(bmap1);
1947 isl_basic_map_free(bmap2);
1948 return NULL;
1951 struct isl_basic_set *isl_basic_set_apply(
1952 struct isl_basic_set *bset, struct isl_basic_map *bmap)
1954 if (!bset || !bmap)
1955 goto error;
1957 isl_assert(set->ctx, isl_basic_map_compatible_domain(bmap, bset),
1958 goto error);
1960 return (struct isl_basic_set *)
1961 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
1962 error:
1963 isl_basic_set_free(bset);
1964 isl_basic_map_free(bmap);
1965 return NULL;
1968 struct isl_basic_map *isl_basic_map_apply_domain(
1969 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
1971 if (!bmap1 || !bmap2)
1972 goto error;
1974 isl_assert(ctx,
1975 isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
1976 isl_assert(ctx,
1977 isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
1978 goto error);
1980 bmap1 = isl_basic_map_reverse(bmap1);
1981 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
1982 return isl_basic_map_reverse(bmap1);
1983 error:
1984 isl_basic_map_free(bmap1);
1985 isl_basic_map_free(bmap2);
1986 return NULL;
1989 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
1990 * A \cap B -> f(A) + f(B)
1992 struct isl_basic_map *isl_basic_map_sum(
1993 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
1995 unsigned n_in, n_out, nparam, total, pos;
1996 struct isl_basic_map *bmap = NULL;
1997 struct isl_dim_map *dim_map1, *dim_map2;
1998 int i;
2000 if (!bmap1 || !bmap2)
2001 goto error;
2003 isl_assert(bmap1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim),
2004 goto error);
2006 nparam = isl_basic_map_n_param(bmap1);
2007 n_in = isl_basic_map_n_in(bmap1);
2008 n_out = isl_basic_map_n_out(bmap1);
2010 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
2011 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
2012 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
2013 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
2014 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
2015 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
2016 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
2017 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
2018 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
2019 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
2020 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
2022 bmap = isl_basic_map_alloc_dim(isl_dim_copy(bmap1->dim),
2023 bmap1->n_div + bmap2->n_div + 2 * n_out,
2024 bmap1->n_eq + bmap2->n_eq + n_out,
2025 bmap1->n_ineq + bmap2->n_ineq);
2026 for (i = 0; i < n_out; ++i) {
2027 int j = isl_basic_map_alloc_equality(bmap);
2028 if (j < 0)
2029 goto error;
2030 isl_seq_clr(bmap->eq[j], 1+total);
2031 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
2032 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
2033 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
2035 bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
2036 bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
2037 bmap = add_divs(bmap, 2 * n_out);
2039 bmap = isl_basic_map_simplify(bmap);
2040 return isl_basic_map_finalize(bmap);
2041 error:
2042 isl_basic_map_free(bmap);
2043 isl_basic_map_free(bmap1);
2044 isl_basic_map_free(bmap2);
2045 return NULL;
2048 /* Given a basic map A -> f(A), construct A -> -f(A).
2050 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
2052 int i, j;
2053 unsigned off, n;
2055 bmap = isl_basic_map_cow(bmap);
2056 if (!bmap)
2057 return NULL;
2059 n = isl_basic_map_dim(bmap, isl_dim_out);
2060 off = isl_basic_map_offset(bmap, isl_dim_out);
2061 for (i = 0; i < bmap->n_eq; ++i)
2062 for (j = 0; j < n; ++j)
2063 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
2064 for (i = 0; i < bmap->n_ineq; ++i)
2065 for (j = 0; j < n; ++j)
2066 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
2067 for (i = 0; i < bmap->n_div; ++i)
2068 for (j = 0; j < n; ++j)
2069 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
2070 return isl_basic_map_finalize(bmap);
2073 /* Given a basic map A -> f(A) and an integer d, construct a basic map
2074 * A -> floor(f(A)/d).
2076 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
2077 isl_int d)
2079 unsigned n_in, n_out, nparam, total, pos;
2080 struct isl_basic_map *result = NULL;
2081 struct isl_dim_map *dim_map;
2082 int i;
2084 if (!bmap)
2085 return NULL;
2087 nparam = isl_basic_map_n_param(bmap);
2088 n_in = isl_basic_map_n_in(bmap);
2089 n_out = isl_basic_map_n_out(bmap);
2091 total = nparam + n_in + n_out + bmap->n_div + n_out;
2092 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2093 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
2094 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
2095 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
2096 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
2098 result = isl_basic_map_alloc_dim(isl_dim_copy(bmap->dim),
2099 bmap->n_div + n_out,
2100 bmap->n_eq, bmap->n_ineq + 2 * n_out);
2101 result = add_constraints_dim_map(result, bmap, dim_map);
2102 result = add_divs(result, n_out);
2103 for (i = 0; i < n_out; ++i) {
2104 int j;
2105 j = isl_basic_map_alloc_inequality(result);
2106 if (j < 0)
2107 goto error;
2108 isl_seq_clr(result->ineq[j], 1+total);
2109 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
2110 isl_int_set_si(result->ineq[j][1+pos+i], 1);
2111 j = isl_basic_map_alloc_inequality(result);
2112 if (j < 0)
2113 goto error;
2114 isl_seq_clr(result->ineq[j], 1+total);
2115 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
2116 isl_int_set_si(result->ineq[j][1+pos+i], -1);
2117 isl_int_sub_ui(result->ineq[j][0], d, 1);
2120 result = isl_basic_map_simplify(result);
2121 return isl_basic_map_finalize(result);
2122 error:
2123 isl_basic_map_free(result);
2124 return NULL;
2127 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
2129 int i;
2130 unsigned nparam;
2131 unsigned n_in;
2133 i = isl_basic_map_alloc_equality(bmap);
2134 if (i < 0)
2135 goto error;
2136 nparam = isl_basic_map_n_param(bmap);
2137 n_in = isl_basic_map_n_in(bmap);
2138 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
2139 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
2140 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
2141 return isl_basic_map_finalize(bmap);
2142 error:
2143 isl_basic_map_free(bmap);
2144 return NULL;
2147 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
2149 int i;
2150 unsigned nparam;
2151 unsigned n_in;
2153 i = isl_basic_map_alloc_inequality(bmap);
2154 if (i < 0)
2155 goto error;
2156 nparam = isl_basic_map_n_param(bmap);
2157 n_in = isl_basic_map_n_in(bmap);
2158 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
2159 isl_int_set_si(bmap->ineq[i][0], -1);
2160 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
2161 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
2162 return isl_basic_map_finalize(bmap);
2163 error:
2164 isl_basic_map_free(bmap);
2165 return NULL;
2168 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
2170 int i;
2171 unsigned nparam;
2172 unsigned n_in;
2174 i = isl_basic_map_alloc_inequality(bmap);
2175 if (i < 0)
2176 goto error;
2177 nparam = isl_basic_map_n_param(bmap);
2178 n_in = isl_basic_map_n_in(bmap);
2179 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
2180 isl_int_set_si(bmap->ineq[i][0], -1);
2181 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
2182 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
2183 return isl_basic_map_finalize(bmap);
2184 error:
2185 isl_basic_map_free(bmap);
2186 return NULL;
2189 struct isl_basic_map *isl_basic_map_equal(struct isl_dim *dim, unsigned n_equal)
2191 int i;
2192 struct isl_basic_map *bmap;
2193 bmap = isl_basic_map_alloc_dim(dim, 0, n_equal, 0);
2194 if (!bmap)
2195 return NULL;
2196 for (i = 0; i < n_equal && bmap; ++i)
2197 bmap = var_equal(bmap, i);
2198 return isl_basic_map_finalize(bmap);
2201 struct isl_basic_map *isl_basic_map_less_at(struct isl_dim *dim, unsigned pos)
2203 int i;
2204 struct isl_basic_map *bmap;
2205 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
2206 if (!bmap)
2207 return NULL;
2208 for (i = 0; i < pos && bmap; ++i)
2209 bmap = var_equal(bmap, i);
2210 if (bmap)
2211 bmap = var_less(bmap, pos);
2212 return isl_basic_map_finalize(bmap);
2215 struct isl_basic_map *isl_basic_map_more_at(struct isl_dim *dim, unsigned pos)
2217 int i;
2218 struct isl_basic_map *bmap;
2219 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
2220 if (!bmap)
2221 return NULL;
2222 for (i = 0; i < pos && bmap; ++i)
2223 bmap = var_equal(bmap, i);
2224 if (bmap)
2225 bmap = var_more(bmap, pos);
2226 return isl_basic_map_finalize(bmap);
2229 struct isl_basic_map *isl_basic_map_from_basic_set(
2230 struct isl_basic_set *bset, struct isl_dim *dim)
2232 struct isl_basic_map *bmap;
2234 bset = isl_basic_set_cow(bset);
2235 if (!bset || !dim)
2236 goto error;
2238 isl_assert(bset->ctx, isl_dim_compatible(bset->dim, dim), goto error);
2239 isl_dim_free(bset->dim);
2240 bmap = (struct isl_basic_map *) bset;
2241 bmap->dim = dim;
2242 return isl_basic_map_finalize(bmap);
2243 error:
2244 isl_basic_set_free(bset);
2245 isl_dim_free(dim);
2246 return NULL;
2249 struct isl_basic_set *isl_basic_set_from_basic_map(struct isl_basic_map *bmap)
2251 if (!bmap)
2252 goto error;
2253 if (bmap->dim->n_in == 0)
2254 return (struct isl_basic_set *)bmap;
2255 bmap = isl_basic_map_cow(bmap);
2256 if (!bmap)
2257 goto error;
2258 bmap->dim = isl_dim_cow(bmap->dim);
2259 if (!bmap->dim)
2260 goto error;
2261 bmap->dim->n_out += bmap->dim->n_in;
2262 bmap->dim->n_in = 0;
2263 bmap = isl_basic_map_finalize(bmap);
2264 return (struct isl_basic_set *)bmap;
2265 error:
2266 isl_basic_map_free(bmap);
2267 return NULL;
2270 /* For a div d = floor(f/m), add the constraints
2272 * f - m d >= 0
2273 * -(f-(n-1)) + m d >= 0
2275 * Note that the second constraint is the negation of
2277 * f - m d >= n
2279 static int add_div_constraints(struct isl_basic_map *bmap, unsigned div)
2281 int i, j;
2282 unsigned total = isl_basic_map_total_dim(bmap);
2283 unsigned div_pos = 1 + total - bmap->n_div + div;
2285 i = isl_basic_map_alloc_inequality(bmap);
2286 if (i < 0)
2287 return -1;
2288 isl_seq_cpy(bmap->ineq[i], bmap->div[div]+1, 1+total);
2289 isl_int_neg(bmap->ineq[i][div_pos], bmap->div[div][0]);
2291 j = isl_basic_map_alloc_inequality(bmap);
2292 if (j < 0)
2293 return -1;
2294 isl_seq_neg(bmap->ineq[j], bmap->ineq[i], 1 + total);
2295 isl_int_add(bmap->ineq[j][0], bmap->ineq[j][0], bmap->ineq[j][div_pos]);
2296 isl_int_sub_ui(bmap->ineq[j][0], bmap->ineq[j][0], 1);
2297 return j;
2300 struct isl_basic_set *isl_basic_map_underlying_set(
2301 struct isl_basic_map *bmap)
2303 if (!bmap)
2304 goto error;
2305 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 && bmap->n_div == 0)
2306 return (struct isl_basic_set *)bmap;
2307 bmap = isl_basic_map_cow(bmap);
2308 if (!bmap)
2309 goto error;
2310 bmap->dim = isl_dim_underlying(bmap->dim, bmap->n_div);
2311 if (!bmap->dim)
2312 goto error;
2313 bmap->extra -= bmap->n_div;
2314 bmap->n_div = 0;
2315 bmap = isl_basic_map_finalize(bmap);
2316 return (struct isl_basic_set *)bmap;
2317 error:
2318 return NULL;
2321 struct isl_basic_map *isl_basic_map_overlying_set(
2322 struct isl_basic_set *bset, struct isl_basic_map *like)
2324 struct isl_basic_map *bmap;
2325 struct isl_ctx *ctx;
2326 unsigned total;
2327 int i;
2329 if (!bset || !like)
2330 goto error;
2331 ctx = bset->ctx;
2332 isl_assert(ctx, bset->n_div == 0, goto error);
2333 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
2334 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
2335 goto error);
2336 if (isl_dim_equal(bset->dim, like->dim) && like->n_div == 0) {
2337 isl_basic_map_free(like);
2338 return (struct isl_basic_map *)bset;
2340 bset = isl_basic_set_cow(bset);
2341 if (!bset)
2342 goto error;
2343 total = bset->dim->n_out + bset->extra;
2344 bmap = (struct isl_basic_map *)bset;
2345 isl_dim_free(bmap->dim);
2346 bmap->dim = isl_dim_copy(like->dim);
2347 if (!bmap->dim)
2348 goto error;
2349 bmap->n_div = like->n_div;
2350 bmap->extra += like->n_div;
2351 if (bmap->extra) {
2352 unsigned ltotal;
2353 ltotal = total - bmap->extra + like->extra;
2354 if (ltotal > total)
2355 ltotal = total;
2356 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
2357 bmap->extra * (1 + 1 + total));
2358 if (isl_blk_is_error(bmap->block2))
2359 goto error;
2360 bmap->div = isl_realloc_array(ctx, bmap->div, isl_int *,
2361 bmap->extra);
2362 if (!bmap->div)
2363 goto error;
2364 for (i = 0; i < bmap->extra; ++i)
2365 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
2366 for (i = 0; i < like->n_div; ++i) {
2367 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
2368 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
2370 bmap = isl_basic_map_extend_constraints(bmap,
2371 0, 2 * like->n_div);
2372 for (i = 0; i < like->n_div; ++i) {
2373 if (isl_int_is_zero(bmap->div[i][0]))
2374 continue;
2375 if (add_div_constraints(bmap, i) < 0)
2376 goto error;
2379 isl_basic_map_free(like);
2380 bmap = isl_basic_map_simplify(bmap);
2381 bmap = isl_basic_map_finalize(bmap);
2382 return bmap;
2383 error:
2384 isl_basic_map_free(like);
2385 isl_basic_set_free(bset);
2386 return NULL;
2389 struct isl_basic_set *isl_basic_set_from_underlying_set(
2390 struct isl_basic_set *bset, struct isl_basic_set *like)
2392 return (struct isl_basic_set *)
2393 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
2396 struct isl_set *isl_set_from_underlying_set(
2397 struct isl_set *set, struct isl_basic_set *like)
2399 int i;
2401 if (!set || !like)
2402 goto error;
2403 isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
2404 goto error);
2405 if (isl_dim_equal(set->dim, like->dim) && like->n_div == 0) {
2406 isl_basic_set_free(like);
2407 return set;
2409 set = isl_set_cow(set);
2410 if (!set)
2411 goto error;
2412 for (i = 0; i < set->n; ++i) {
2413 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
2414 isl_basic_set_copy(like));
2415 if (!set->p[i])
2416 goto error;
2418 isl_dim_free(set->dim);
2419 set->dim = isl_dim_copy(like->dim);
2420 if (!set->dim)
2421 goto error;
2422 isl_basic_set_free(like);
2423 return set;
2424 error:
2425 isl_basic_set_free(like);
2426 isl_set_free(set);
2427 return NULL;
2430 struct isl_set *isl_map_underlying_set(struct isl_map *map)
2432 int i;
2434 map = isl_map_cow(map);
2435 if (!map)
2436 return NULL;
2437 map->dim = isl_dim_cow(map->dim);
2438 if (!map->dim)
2439 goto error;
2441 for (i = 1; i < map->n; ++i)
2442 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
2443 goto error);
2444 for (i = 0; i < map->n; ++i) {
2445 map->p[i] = (struct isl_basic_map *)
2446 isl_basic_map_underlying_set(map->p[i]);
2447 if (!map->p[i])
2448 goto error;
2450 if (map->n == 0)
2451 map->dim = isl_dim_underlying(map->dim, 0);
2452 else {
2453 isl_dim_free(map->dim);
2454 map->dim = isl_dim_copy(map->p[0]->dim);
2456 if (!map->dim)
2457 goto error;
2458 return (struct isl_set *)map;
2459 error:
2460 isl_map_free(map);
2461 return NULL;
2464 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
2466 return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
2469 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
2471 struct isl_basic_set *domain;
2472 unsigned n_out;
2473 if (!bmap)
2474 return NULL;
2475 n_out = isl_basic_map_n_out(bmap);
2476 domain = isl_basic_set_from_basic_map(bmap);
2477 return isl_basic_set_project_out(domain, n_out, 0);
2480 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
2482 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
2485 struct isl_set *isl_map_range(struct isl_map *map)
2487 int i;
2488 struct isl_set *set;
2490 if (!map)
2491 goto error;
2492 map = isl_map_cow(map);
2493 if (!map)
2494 goto error;
2496 set = (struct isl_set *) map;
2497 if (set->dim->n_in != 0) {
2498 set->dim = isl_dim_drop_inputs(set->dim, 0, set->dim->n_in);
2499 if (!set->dim)
2500 goto error;
2502 for (i = 0; i < map->n; ++i) {
2503 set->p[i] = isl_basic_map_range(map->p[i]);
2504 if (!set->p[i])
2505 goto error;
2507 ISL_F_CLR(set, ISL_MAP_DISJOINT);
2508 ISL_F_CLR(set, ISL_SET_NORMALIZED);
2509 return set;
2510 error:
2511 isl_map_free(map);
2512 return NULL;
2515 struct isl_map *isl_map_from_set(struct isl_set *set, struct isl_dim *dim)
2517 int i;
2518 struct isl_map *map = NULL;
2520 set = isl_set_cow(set);
2521 if (!set || !dim)
2522 goto error;
2523 isl_assert(set->ctx, isl_dim_compatible(set->dim, dim), goto error);
2524 map = (struct isl_map *)set;
2525 for (i = 0; i < set->n; ++i) {
2526 map->p[i] = isl_basic_map_from_basic_set(
2527 set->p[i], isl_dim_copy(dim));
2528 if (!map->p[i])
2529 goto error;
2531 isl_dim_free(map->dim);
2532 map->dim = dim;
2533 return map;
2534 error:
2535 isl_dim_free(dim);
2536 isl_set_free(set);
2537 return NULL;
2540 struct isl_map *isl_map_from_range(struct isl_set *set)
2542 return (struct isl_map *)set;
2545 struct isl_set *isl_set_from_map(struct isl_map *map)
2547 int i;
2548 struct isl_set *set = NULL;
2550 if (!map)
2551 return NULL;
2552 map = isl_map_cow(map);
2553 if (!map)
2554 return NULL;
2555 map->dim = isl_dim_cow(map->dim);
2556 if (!map->dim)
2557 goto error;
2558 map->dim->n_out += map->dim->n_in;
2559 map->dim->n_in = 0;
2560 set = (struct isl_set *)map;
2561 for (i = 0; i < map->n; ++i) {
2562 set->p[i] = isl_basic_set_from_basic_map(map->p[i]);
2563 if (!set->p[i])
2564 goto error;
2566 return set;
2567 error:
2568 isl_map_free(map);
2569 return NULL;
2572 struct isl_map *isl_map_alloc_dim(struct isl_dim *dim, int n, unsigned flags)
2574 struct isl_map *map;
2576 if (!dim)
2577 return NULL;
2578 isl_assert(dim->ctx, n >= 0, return NULL);
2579 map = isl_alloc(dim->ctx, struct isl_map,
2580 sizeof(struct isl_map) +
2581 n * sizeof(struct isl_basic_map *));
2582 if (!map)
2583 goto error;
2585 map->ctx = dim->ctx;
2586 isl_ctx_ref(map->ctx);
2587 map->ref = 1;
2588 map->size = n;
2589 map->n = 0;
2590 map->dim = dim;
2591 map->flags = flags;
2592 return map;
2593 error:
2594 isl_dim_free(dim);
2595 return NULL;
2598 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
2599 unsigned nparam, unsigned in, unsigned out, int n,
2600 unsigned flags)
2602 struct isl_map *map;
2603 struct isl_dim *dims;
2605 dims = isl_dim_alloc(ctx, nparam, in, out);
2606 if (!dims)
2607 return NULL;
2609 map = isl_map_alloc_dim(dims, n, flags);
2610 return map;
2613 struct isl_basic_map *isl_basic_map_empty(struct isl_ctx *ctx,
2614 unsigned nparam, unsigned in, unsigned out)
2616 struct isl_basic_map *bmap;
2617 bmap = isl_basic_map_alloc(ctx, nparam, in, out, 0, 1, 0);
2618 bmap = isl_basic_map_set_to_empty(bmap);
2619 return bmap;
2622 struct isl_basic_set *isl_basic_set_empty(struct isl_dim *dim)
2624 struct isl_basic_set *bset;
2625 bset = isl_basic_set_alloc_dim(dim, 0, 1, 0);
2626 bset = isl_basic_set_set_to_empty(bset);
2627 return bset;
2630 struct isl_basic_map *isl_basic_map_empty_like(struct isl_basic_map *model)
2632 struct isl_basic_map *bmap;
2633 if (!model)
2634 return NULL;
2635 bmap = isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
2636 bmap = isl_basic_map_set_to_empty(bmap);
2637 return bmap;
2640 struct isl_basic_map *isl_basic_map_empty_like_map(struct isl_map *model)
2642 struct isl_basic_map *bmap;
2643 if (!model)
2644 return NULL;
2645 bmap = isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
2646 bmap = isl_basic_map_set_to_empty(bmap);
2647 return bmap;
2650 struct isl_basic_set *isl_basic_set_empty_like(struct isl_basic_set *model)
2652 struct isl_basic_set *bset;
2653 if (!model)
2654 return NULL;
2655 bset = isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
2656 bset = isl_basic_set_set_to_empty(bset);
2657 return bset;
2660 struct isl_basic_map *isl_basic_map_universe(struct isl_dim *dim)
2662 struct isl_basic_map *bmap;
2663 bmap = isl_basic_map_alloc_dim(dim, 0, 0, 0);
2664 return bmap;
2667 struct isl_basic_set *isl_basic_set_universe(struct isl_dim *dim)
2669 struct isl_basic_set *bset;
2670 bset = isl_basic_set_alloc_dim(dim, 0, 0, 0);
2671 return bset;
2674 struct isl_basic_set *isl_basic_set_universe_like(struct isl_basic_set *model)
2676 if (!model)
2677 return NULL;
2678 return isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
2681 struct isl_map *isl_map_empty(struct isl_dim *dim)
2683 return isl_map_alloc_dim(dim, 0, ISL_MAP_DISJOINT);
2686 struct isl_map *isl_map_empty_like(struct isl_map *model)
2688 if (!model)
2689 return NULL;
2690 return isl_map_alloc_dim(isl_dim_copy(model->dim), 0, ISL_MAP_DISJOINT);
2693 struct isl_map *isl_map_empty_like_basic_map(struct isl_basic_map *model)
2695 if (!model)
2696 return NULL;
2697 return isl_map_alloc_dim(isl_dim_copy(model->dim), 0, ISL_MAP_DISJOINT);
2700 struct isl_set *isl_set_empty(struct isl_dim *dim)
2702 return isl_set_alloc_dim(dim, 0, ISL_MAP_DISJOINT);
2705 struct isl_set *isl_set_empty_like(struct isl_set *model)
2707 if (!model)
2708 return NULL;
2709 return isl_set_empty(isl_dim_copy(model->dim));
2712 struct isl_map *isl_map_universe(struct isl_dim *dim)
2714 struct isl_map *map;
2715 if (!dim)
2716 return NULL;
2717 map = isl_map_alloc_dim(isl_dim_copy(dim), 1, ISL_MAP_DISJOINT);
2718 map = isl_map_add(map, isl_basic_map_universe(dim));
2719 return map;
2722 struct isl_set *isl_set_universe(struct isl_dim *dim)
2724 struct isl_set *set;
2725 if (!dim)
2726 return NULL;
2727 set = isl_set_alloc_dim(isl_dim_copy(dim), 1, ISL_MAP_DISJOINT);
2728 set = isl_set_add(set, isl_basic_set_universe(dim));
2729 return set;
2732 struct isl_map *isl_map_dup(struct isl_map *map)
2734 int i;
2735 struct isl_map *dup;
2737 if (!map)
2738 return NULL;
2739 dup = isl_map_alloc_dim(isl_dim_copy(map->dim), map->n, map->flags);
2740 for (i = 0; i < map->n; ++i)
2741 dup = isl_map_add(dup, isl_basic_map_copy(map->p[i]));
2742 return dup;
2745 struct isl_map *isl_map_add(struct isl_map *map, struct isl_basic_map *bmap)
2747 if (!bmap || !map)
2748 goto error;
2749 isl_assert(map->ctx, isl_dim_equal(map->dim, bmap->dim), goto error);
2750 isl_assert(map->ctx, map->n < map->size, goto error);
2751 map->p[map->n] = bmap;
2752 map->n++;
2753 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
2754 return map;
2755 error:
2756 if (map)
2757 isl_map_free(map);
2758 if (bmap)
2759 isl_basic_map_free(bmap);
2760 return NULL;
2763 void isl_map_free(struct isl_map *map)
2765 int i;
2767 if (!map)
2768 return;
2770 if (--map->ref > 0)
2771 return;
2773 isl_ctx_deref(map->ctx);
2774 for (i = 0; i < map->n; ++i)
2775 isl_basic_map_free(map->p[i]);
2776 isl_dim_free(map->dim);
2777 free(map);
2780 struct isl_map *isl_map_extend(struct isl_map *base,
2781 unsigned nparam, unsigned n_in, unsigned n_out)
2783 int i;
2785 base = isl_map_cow(base);
2786 if (!base)
2787 return NULL;
2789 base->dim = isl_dim_extend(base->dim, nparam, n_in, n_out);
2790 if (!base->dim)
2791 goto error;
2792 for (i = 0; i < base->n; ++i) {
2793 base->p[i] = isl_basic_map_extend_dim(base->p[i],
2794 isl_dim_copy(base->dim), 0, 0, 0);
2795 if (!base->p[i])
2796 goto error;
2798 return base;
2799 error:
2800 isl_map_free(base);
2801 return NULL;
2804 struct isl_set *isl_set_extend(struct isl_set *base,
2805 unsigned nparam, unsigned dim)
2807 return (struct isl_set *)isl_map_extend((struct isl_map *)base,
2808 nparam, 0, dim);
2811 static struct isl_basic_map *isl_basic_map_fix_pos(struct isl_basic_map *bmap,
2812 unsigned pos, int value)
2814 int j;
2816 bmap = isl_basic_map_cow(bmap);
2817 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
2818 j = isl_basic_map_alloc_equality(bmap);
2819 if (j < 0)
2820 goto error;
2821 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
2822 isl_int_set_si(bmap->eq[j][pos], -1);
2823 isl_int_set_si(bmap->eq[j][0], value);
2824 bmap = isl_basic_map_simplify(bmap);
2825 return isl_basic_map_finalize(bmap);
2826 error:
2827 isl_basic_map_free(bmap);
2828 return NULL;
2831 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
2832 enum isl_dim_type type, unsigned pos, int value)
2834 if (!bmap)
2835 return NULL;
2836 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
2837 return isl_basic_map_fix_pos(bmap, isl_basic_map_offset(bmap, type) + pos,
2838 value);
2839 error:
2840 isl_basic_map_free(bmap);
2841 return NULL;
2844 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
2845 enum isl_dim_type type, unsigned pos, int value)
2847 return (struct isl_basic_set *)
2848 isl_basic_map_fix_si((struct isl_basic_map *)bset,
2849 type, pos, value);
2852 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
2853 unsigned input, int value)
2855 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
2858 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
2859 unsigned dim, int value)
2861 return (struct isl_basic_set *)
2862 isl_basic_map_fix_si((struct isl_basic_map *)bset,
2863 isl_dim_set, dim, value);
2866 struct isl_map *isl_map_fix_si(struct isl_map *map,
2867 enum isl_dim_type type, unsigned pos, int value)
2869 int i;
2871 map = isl_map_cow(map);
2872 if (!map)
2873 return NULL;
2875 isl_assert(ctx, pos < isl_map_dim(map, type), goto error);
2876 for (i = 0; i < map->n; ++i) {
2877 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
2878 if (!map->p[i])
2879 goto error;
2881 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
2882 return map;
2883 error:
2884 isl_map_free(map);
2885 return NULL;
2888 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
2889 unsigned input, int value)
2891 return isl_map_fix_si(map, isl_dim_in, input, value);
2894 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
2896 return (struct isl_set *)
2897 isl_map_fix_si((struct isl_map *)set, isl_dim_set, dim, value);
2900 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
2901 unsigned dim, isl_int value)
2903 int j;
2904 unsigned nparam;
2906 bset = isl_basic_set_cow(bset);
2907 bset = isl_basic_set_extend_constraints(bset, 0, 1);
2908 j = isl_basic_set_alloc_inequality(bset);
2909 if (j < 0)
2910 goto error;
2911 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
2912 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
2913 isl_int_neg(bset->ineq[j][0], value);
2914 bset = isl_basic_set_simplify(bset);
2915 return isl_basic_set_finalize(bset);
2916 error:
2917 isl_basic_set_free(bset);
2918 return NULL;
2921 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
2922 isl_int value)
2924 int i;
2926 set = isl_set_cow(set);
2927 if (!set)
2928 return NULL;
2930 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
2931 for (i = 0; i < set->n; ++i) {
2932 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
2933 if (!set->p[i])
2934 goto error;
2936 return set;
2937 error:
2938 isl_set_free(set);
2939 return NULL;
2942 struct isl_map *isl_map_reverse(struct isl_map *map)
2944 int i;
2945 unsigned t;
2947 map = isl_map_cow(map);
2948 if (!map)
2949 return NULL;
2951 map->dim = isl_dim_reverse(map->dim);
2952 if (!map->dim)
2953 goto error;
2954 for (i = 0; i < map->n; ++i) {
2955 map->p[i] = isl_basic_map_reverse(map->p[i]);
2956 if (!map->p[i])
2957 goto error;
2959 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
2960 return map;
2961 error:
2962 isl_map_free(map);
2963 return NULL;
2966 struct isl_map *isl_basic_map_lexmax(
2967 struct isl_basic_map *bmap, struct isl_basic_set *dom,
2968 struct isl_set **empty)
2970 return isl_pip_basic_map_lexmax(bmap, dom, empty);
2973 struct isl_map *isl_basic_map_lexmin(
2974 struct isl_basic_map *bmap, struct isl_basic_set *dom,
2975 struct isl_set **empty)
2977 return isl_pip_basic_map_lexmin(bmap, dom, empty);
2980 struct isl_set *isl_basic_set_lexmin(struct isl_basic_set *bset)
2982 struct isl_basic_map *bmap = NULL;
2983 struct isl_basic_set *dom = NULL;
2984 struct isl_map *min;
2985 struct isl_dim *param_dim;
2987 if (!bset)
2988 goto error;
2989 bmap = isl_basic_map_from_basic_set(bset, isl_dim_copy(bset->dim));
2990 if (!bmap)
2991 goto error;
2992 param_dim = isl_dim_domain(isl_dim_copy(bmap->dim));
2993 dom = isl_basic_set_universe(param_dim);
2994 if (!dom)
2995 goto error;
2996 min = isl_basic_map_lexmin(bmap, dom, NULL);
2997 return isl_map_range(min);
2998 error:
2999 isl_basic_map_free(bmap);
3000 return NULL;
3003 /* If bmap contains any unknown divs, then compute explicit
3004 * expressions for them. However, this computation may be
3005 * quite expensive, so first try to remove divs that aren't
3006 * strictly needed.
3008 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
3010 int i;
3011 unsigned off;
3013 if (!bmap)
3014 return NULL;
3015 off = isl_dim_total(bmap->dim);
3016 for (i = 0; i < bmap->n_div; ++i) {
3017 if (isl_int_is_zero(bmap->div[i][0]))
3018 break;
3019 isl_assert(bmap->ctx, isl_int_is_zero(bmap->div[i][1+1+off+i]),
3020 goto error);
3022 if (i == bmap->n_div)
3023 return isl_map_from_basic_map(bmap);
3024 bmap = isl_basic_map_drop_redundant_divs(bmap);
3025 if (!bmap)
3026 goto error;
3027 for (i = 0; i < bmap->n_div; ++i)
3028 if (isl_int_is_zero(bmap->div[i][0]))
3029 break;
3030 if (i == bmap->n_div)
3031 return isl_map_from_basic_map(bmap);
3032 struct isl_map *map = isl_pip_basic_map_compute_divs(bmap);
3033 return map;
3034 error:
3035 isl_basic_map_free(bmap);
3036 return NULL;
3039 struct isl_map *isl_map_compute_divs(struct isl_map *map)
3041 int i;
3042 struct isl_map *res;
3044 if (!map)
3045 return NULL;
3046 if (map->n == 0)
3047 return map;
3048 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
3049 for (i = 1 ; i < map->n; ++i) {
3050 struct isl_map *r2;
3051 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
3052 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
3053 res = isl_map_union_disjoint(res, r2);
3054 else
3055 res = isl_map_union(res, r2);
3057 isl_map_free(map);
3059 return res;
3062 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
3064 return (struct isl_set *)
3065 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
3068 struct isl_set *isl_set_compute_divs(struct isl_set *set)
3070 return (struct isl_set *)
3071 isl_map_compute_divs((struct isl_map *)set);
3074 struct isl_set *isl_map_domain(struct isl_map *map)
3076 int i;
3077 struct isl_set *set;
3079 if (!map)
3080 goto error;
3082 map = isl_map_cow(map);
3083 if (!map)
3084 return NULL;
3086 set = (struct isl_set *)map;
3087 set->dim = isl_dim_domain(set->dim);
3088 if (!set->dim)
3089 goto error;
3090 for (i = 0; i < map->n; ++i) {
3091 set->p[i] = isl_basic_map_domain(map->p[i]);
3092 if (!set->p[i])
3093 goto error;
3095 ISL_F_CLR(set, ISL_MAP_DISJOINT);
3096 ISL_F_CLR(set, ISL_SET_NORMALIZED);
3097 return set;
3098 error:
3099 isl_map_free(map);
3100 return NULL;
3103 struct isl_map *isl_map_union_disjoint(
3104 struct isl_map *map1, struct isl_map *map2)
3106 int i;
3107 unsigned flags = 0;
3108 struct isl_map *map = NULL;
3110 if (!map1 || !map2)
3111 goto error;
3113 if (map1->n == 0) {
3114 isl_map_free(map1);
3115 return map2;
3117 if (map2->n == 0) {
3118 isl_map_free(map2);
3119 return map1;
3122 isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
3124 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
3125 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
3126 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3128 map = isl_map_alloc_dim(isl_dim_copy(map1->dim),
3129 map1->n + map2->n, flags);
3130 if (!map)
3131 goto error;
3132 for (i = 0; i < map1->n; ++i) {
3133 map = isl_map_add(map,
3134 isl_basic_map_copy(map1->p[i]));
3135 if (!map)
3136 goto error;
3138 for (i = 0; i < map2->n; ++i) {
3139 map = isl_map_add(map,
3140 isl_basic_map_copy(map2->p[i]));
3141 if (!map)
3142 goto error;
3144 isl_map_free(map1);
3145 isl_map_free(map2);
3146 return map;
3147 error:
3148 isl_map_free(map);
3149 isl_map_free(map1);
3150 isl_map_free(map2);
3151 return NULL;
3154 struct isl_map *isl_map_union(struct isl_map *map1, struct isl_map *map2)
3156 map1 = isl_map_union_disjoint(map1, map2);
3157 if (!map1)
3158 return NULL;
3159 if (map1->n > 1)
3160 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
3161 return map1;
3164 struct isl_set *isl_set_union_disjoint(
3165 struct isl_set *set1, struct isl_set *set2)
3167 return (struct isl_set *)
3168 isl_map_union_disjoint(
3169 (struct isl_map *)set1, (struct isl_map *)set2);
3172 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
3174 return (struct isl_set *)
3175 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
3178 struct isl_map *isl_map_intersect_range(
3179 struct isl_map *map, struct isl_set *set)
3181 unsigned flags = 0;
3182 struct isl_map *result;
3183 int i, j;
3185 if (!map || !set)
3186 goto error;
3188 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
3189 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
3190 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3192 result = isl_map_alloc_dim(isl_dim_copy(map->dim),
3193 map->n * set->n, flags);
3194 if (!result)
3195 goto error;
3196 for (i = 0; i < map->n; ++i)
3197 for (j = 0; j < set->n; ++j) {
3198 result = isl_map_add(result,
3199 isl_basic_map_intersect_range(
3200 isl_basic_map_copy(map->p[i]),
3201 isl_basic_set_copy(set->p[j])));
3202 if (!result)
3203 goto error;
3205 isl_map_free(map);
3206 isl_set_free(set);
3207 return result;
3208 error:
3209 isl_map_free(map);
3210 isl_set_free(set);
3211 return NULL;
3214 struct isl_map *isl_map_intersect_domain(
3215 struct isl_map *map, struct isl_set *set)
3217 return isl_map_reverse(
3218 isl_map_intersect_range(isl_map_reverse(map), set));
3221 struct isl_map *isl_map_apply_domain(
3222 struct isl_map *map1, struct isl_map *map2)
3224 if (!map1 || !map2)
3225 goto error;
3226 map1 = isl_map_reverse(map1);
3227 map1 = isl_map_apply_range(map1, map2);
3228 return isl_map_reverse(map1);
3229 error:
3230 isl_map_free(map1);
3231 isl_map_free(map2);
3232 return NULL;
3235 struct isl_map *isl_map_apply_range(
3236 struct isl_map *map1, struct isl_map *map2)
3238 struct isl_dim *dim_result;
3239 struct isl_map *result;
3240 int i, j;
3241 unsigned nparam;
3242 unsigned n_in;
3243 unsigned n_out;
3245 if (!map1 || !map2)
3246 goto error;
3248 dim_result = isl_dim_join(isl_dim_copy(map1->dim),
3249 isl_dim_copy(map2->dim));
3251 result = isl_map_alloc_dim(dim_result, map1->n * map2->n, 0);
3252 if (!result)
3253 goto error;
3254 for (i = 0; i < map1->n; ++i)
3255 for (j = 0; j < map2->n; ++j) {
3256 result = isl_map_add(result,
3257 isl_basic_map_apply_range(
3258 isl_basic_map_copy(map1->p[i]),
3259 isl_basic_map_copy(map2->p[j])));
3260 if (!result)
3261 goto error;
3263 isl_map_free(map1);
3264 isl_map_free(map2);
3265 if (result && result->n <= 1)
3266 ISL_F_SET(result, ISL_MAP_DISJOINT);
3267 return result;
3268 error:
3269 isl_map_free(map1);
3270 isl_map_free(map2);
3271 return NULL;
3275 * returns range - domain
3277 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
3279 struct isl_basic_set *bset;
3280 unsigned dim;
3281 unsigned nparam;
3282 int i;
3284 if (!bmap)
3285 goto error;
3286 dim = isl_basic_map_n_in(bmap);
3287 nparam = isl_basic_map_n_param(bmap);
3288 isl_assert(bmap->ctx, dim == isl_basic_map_n_out(bmap), goto error);
3289 bset = isl_basic_set_from_basic_map(bmap);
3290 bset = isl_basic_set_cow(bset);
3291 bset = isl_basic_set_extend(bset, nparam, 3*dim, 0, dim, 0);
3292 bset = isl_basic_set_swap_vars(bset, 2*dim);
3293 for (i = 0; i < dim; ++i) {
3294 int j = isl_basic_map_alloc_equality(
3295 (struct isl_basic_map *)bset);
3296 if (j < 0)
3297 goto error;
3298 isl_seq_clr(bset->eq[j], 1 + isl_basic_set_total_dim(bset));
3299 isl_int_set_si(bset->eq[j][1+nparam+i], 1);
3300 isl_int_set_si(bset->eq[j][1+nparam+dim+i], 1);
3301 isl_int_set_si(bset->eq[j][1+nparam+2*dim+i], -1);
3303 return isl_basic_set_project_out(bset, 2*dim, 0);
3304 error:
3305 isl_basic_map_free(bmap);
3306 return NULL;
3310 * returns range - domain
3312 struct isl_set *isl_map_deltas(struct isl_map *map)
3314 int i;
3315 struct isl_set *result;
3317 if (!map)
3318 return NULL;
3320 isl_assert(map->ctx, isl_map_n_in(map) == isl_map_n_out(map), goto error);
3321 result = isl_set_alloc(map->ctx, isl_map_n_param(map),
3322 isl_map_n_in(map), map->n, map->flags);
3323 if (!result)
3324 goto error;
3325 for (i = 0; i < map->n; ++i)
3326 result = isl_set_add(result,
3327 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
3328 isl_map_free(map);
3329 return result;
3330 error:
3331 isl_map_free(map);
3332 return NULL;
3335 static struct isl_basic_map *basic_map_identity(struct isl_dim *dims)
3337 struct isl_basic_map *bmap;
3338 unsigned nparam;
3339 unsigned dim;
3340 int i;
3342 if (!dims)
3343 return NULL;
3345 nparam = dims->nparam;
3346 dim = dims->n_out;
3347 bmap = isl_basic_map_alloc_dim(dims, 0, dim, 0);
3348 if (!bmap)
3349 goto error;
3351 for (i = 0; i < dim; ++i) {
3352 int j = isl_basic_map_alloc_equality(bmap);
3353 if (j < 0)
3354 goto error;
3355 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
3356 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
3357 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
3359 return isl_basic_map_finalize(bmap);
3360 error:
3361 isl_basic_map_free(bmap);
3362 return NULL;
3365 struct isl_basic_map *isl_basic_map_identity(struct isl_dim *set_dim)
3367 struct isl_dim *dim = isl_dim_map(set_dim);
3368 if (!dim)
3369 return NULL;
3370 return basic_map_identity(dim);
3373 struct isl_basic_map *isl_basic_map_identity_like(struct isl_basic_map *model)
3375 if (!model || !model->dim)
3376 return NULL;
3377 isl_assert(model->ctx,
3378 model->dim->n_in == model->dim->n_out, return NULL);
3379 return basic_map_identity(isl_dim_copy(model->dim));
3382 static struct isl_map *map_identity(struct isl_dim *dim)
3384 struct isl_map *map = isl_map_alloc_dim(dim, 1, ISL_MAP_DISJOINT);
3385 return isl_map_add(map, basic_map_identity(isl_dim_copy(dim)));
3388 struct isl_map *isl_map_identity(struct isl_dim *set_dim)
3390 struct isl_dim *dim = isl_dim_map(set_dim);
3391 if (!dim)
3392 return NULL;
3393 return map_identity(dim);
3396 struct isl_map *isl_map_identity_like(struct isl_basic_map *model)
3398 if (!model || !model->dim)
3399 return NULL;
3400 isl_assert(model->ctx,
3401 model->dim->n_in == model->dim->n_out, return NULL);
3402 return map_identity(isl_dim_copy(model->dim));
3405 int isl_set_is_equal(struct isl_set *set1, struct isl_set *set2)
3407 return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
3410 int isl_set_is_subset(struct isl_set *set1, struct isl_set *set2)
3412 return isl_map_is_subset(
3413 (struct isl_map *)set1, (struct isl_map *)set2);
3416 int isl_basic_map_is_subset(
3417 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3419 int is_subset;
3420 struct isl_map *map1;
3421 struct isl_map *map2;
3423 if (!bmap1 || !bmap2)
3424 return -1;
3426 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
3427 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
3429 is_subset = isl_map_is_subset(map1, map2);
3431 isl_map_free(map1);
3432 isl_map_free(map2);
3434 return is_subset;
3437 int isl_basic_map_is_equal(
3438 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3440 int is_subset;
3442 if (!bmap1 || !bmap2)
3443 return -1;
3444 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
3445 if (is_subset != 1)
3446 return is_subset;
3447 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
3448 return is_subset;
3451 int isl_basic_set_is_equal(
3452 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
3454 return isl_basic_map_is_equal(
3455 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
3458 int isl_map_is_empty(struct isl_map *map)
3460 int i;
3461 int is_empty;
3463 if (!map)
3464 return -1;
3465 for (i = 0; i < map->n; ++i) {
3466 is_empty = isl_basic_map_is_empty(map->p[i]);
3467 if (is_empty < 0)
3468 return -1;
3469 if (!is_empty)
3470 return 0;
3472 return 1;
3475 int isl_map_fast_is_empty(struct isl_map *map)
3477 return map->n == 0;
3480 int isl_set_is_empty(struct isl_set *set)
3482 return isl_map_is_empty((struct isl_map *)set);
3485 int isl_map_is_subset(struct isl_map *map1, struct isl_map *map2)
3487 int i;
3488 int is_subset = 0;
3489 struct isl_map *diff;
3491 if (!map1 || !map2)
3492 return -1;
3494 if (isl_map_is_empty(map1))
3495 return 1;
3497 if (isl_map_is_empty(map2))
3498 return 0;
3500 diff = isl_map_subtract(isl_map_copy(map1), isl_map_copy(map2));
3501 if (!diff)
3502 return -1;
3504 is_subset = isl_map_is_empty(diff);
3505 isl_map_free(diff);
3507 return is_subset;
3510 int isl_map_is_equal(struct isl_map *map1, struct isl_map *map2)
3512 int is_subset;
3514 if (!map1 || !map2)
3515 return -1;
3516 is_subset = isl_map_is_subset(map1, map2);
3517 if (is_subset != 1)
3518 return is_subset;
3519 is_subset = isl_map_is_subset(map2, map1);
3520 return is_subset;
3523 int isl_basic_map_is_strict_subset(
3524 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3526 int is_subset;
3528 if (!bmap1 || !bmap2)
3529 return -1;
3530 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
3531 if (is_subset != 1)
3532 return is_subset;
3533 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
3534 if (is_subset == -1)
3535 return is_subset;
3536 return !is_subset;
3539 int isl_basic_map_is_universe(struct isl_basic_map *bmap)
3541 if (!bmap)
3542 return -1;
3543 return bmap->n_eq == 0 && bmap->n_ineq == 0;
3546 int isl_basic_set_is_universe(struct isl_basic_set *bset)
3548 if (!bset)
3549 return -1;
3550 return bset->n_eq == 0 && bset->n_ineq == 0;
3553 int isl_basic_map_is_empty(struct isl_basic_map *bmap)
3555 struct isl_basic_set *bset = NULL;
3556 struct isl_vec *sample = NULL;
3557 int empty;
3558 unsigned total;
3560 if (!bmap)
3561 return -1;
3563 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
3564 return 1;
3566 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
3567 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
3568 copy = isl_basic_map_convex_hull(copy);
3569 empty = ISL_F_ISSET(copy, ISL_BASIC_MAP_EMPTY);
3570 isl_basic_map_free(copy);
3571 return empty;
3574 total = 1 + isl_basic_map_total_dim(bmap);
3575 if (bmap->sample && bmap->sample->size == total) {
3576 int contains = basic_map_contains(bmap, bmap->sample);
3577 if (contains < 0)
3578 return -1;
3579 if (contains)
3580 return 0;
3582 isl_vec_free(bmap->sample);
3583 bmap->sample = NULL;
3584 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
3585 if (!bset)
3586 return -1;
3587 sample = isl_basic_set_sample(bset);
3588 if (!sample)
3589 return -1;
3590 empty = sample->size == 0;
3591 isl_vec_free(bmap->sample);
3592 bmap->sample = sample;
3593 if (empty)
3594 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
3596 return empty;
3599 int isl_basic_map_fast_is_empty(struct isl_basic_map *bmap)
3601 if (!bmap)
3602 return -1;
3603 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
3606 int isl_basic_set_fast_is_empty(struct isl_basic_set *bset)
3608 if (!bset)
3609 return -1;
3610 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
3613 int isl_basic_set_is_empty(struct isl_basic_set *bset)
3615 return isl_basic_map_is_empty((struct isl_basic_map *)bset);
3618 struct isl_map *isl_basic_map_union(
3619 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3621 struct isl_map *map;
3622 if (!bmap1 || !bmap2)
3623 return NULL;
3625 isl_assert(map1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim), goto error);
3627 map = isl_map_alloc_dim(isl_dim_copy(bmap1->dim), 2, 0);
3628 if (!map)
3629 goto error;
3630 map = isl_map_add(map, bmap1);
3631 map = isl_map_add(map, bmap2);
3632 return map;
3633 error:
3634 isl_basic_map_free(bmap1);
3635 isl_basic_map_free(bmap2);
3636 return NULL;
3639 struct isl_set *isl_basic_set_union(
3640 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
3642 return (struct isl_set *)isl_basic_map_union(
3643 (struct isl_basic_map *)bset1,
3644 (struct isl_basic_map *)bset2);
3647 /* Order divs such that any div only depends on previous divs */
3648 static struct isl_basic_map *order_divs(struct isl_basic_map *bmap)
3650 int i;
3651 unsigned off = isl_dim_total(bmap->dim);
3653 for (i = 0; i < bmap->n_div; ++i) {
3654 int pos;
3655 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
3656 bmap->n_div-i);
3657 if (pos == -1)
3658 continue;
3659 swap_div(bmap, i, i + pos);
3660 --i;
3662 return bmap;
3665 /* Look for a div in dst that corresponds to the div "div" in src.
3666 * The divs before "div" in src and dst are assumed to be the same.
3668 * Returns -1 if no corresponding div was found and the position
3669 * of the corresponding div in dst otherwise.
3671 static int find_div(struct isl_basic_map *dst,
3672 struct isl_basic_map *src, unsigned div)
3674 int i;
3676 unsigned total = isl_dim_total(src->dim);
3678 isl_assert(dst->ctx, div <= dst->n_div, return -1);
3679 for (i = div; i < dst->n_div; ++i)
3680 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
3681 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
3682 dst->n_div - div) == -1)
3683 return i;
3684 return -1;
3687 struct isl_basic_map *isl_basic_map_align_divs(
3688 struct isl_basic_map *dst, struct isl_basic_map *src)
3690 int i;
3691 unsigned total = isl_dim_total(src->dim);
3693 if (!dst || !src)
3694 goto error;
3696 if (src->n_div == 0)
3697 return dst;
3699 for (i = 0; i < src->n_div; ++i)
3700 isl_assert(src->ctx, !isl_int_is_zero(src->div[i][0]), goto error);
3702 src = order_divs(src);
3703 dst = isl_basic_map_cow(dst);
3704 dst = isl_basic_map_extend_dim(dst, isl_dim_copy(dst->dim),
3705 src->n_div, 0, 2 * src->n_div);
3706 if (!dst)
3707 return NULL;
3708 for (i = 0; i < src->n_div; ++i) {
3709 int j = find_div(dst, src, i);
3710 if (j < 0) {
3711 j = isl_basic_map_alloc_div(dst);
3712 if (j < 0)
3713 goto error;
3714 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
3715 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
3716 if (add_div_constraints(dst, j) < 0)
3717 goto error;
3719 if (j != i)
3720 swap_div(dst, i, j);
3722 return dst;
3723 error:
3724 isl_basic_map_free(dst);
3725 return NULL;
3728 struct isl_basic_set *isl_basic_set_align_divs(
3729 struct isl_basic_set *dst, struct isl_basic_set *src)
3731 return (struct isl_basic_set *)isl_basic_map_align_divs(
3732 (struct isl_basic_map *)dst, (struct isl_basic_map *)src);
3735 struct isl_map *isl_map_align_divs(struct isl_map *map)
3737 int i;
3739 map = isl_map_compute_divs(map);
3740 map = isl_map_cow(map);
3741 if (!map)
3742 return NULL;
3744 for (i = 1; i < map->n; ++i)
3745 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
3746 for (i = 1; i < map->n; ++i)
3747 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
3749 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3750 return map;
3753 struct isl_set *isl_set_align_divs(struct isl_set *set)
3755 return (struct isl_set *)isl_map_align_divs((struct isl_map *)set);
3758 static struct isl_map *add_cut_constraint(struct isl_map *dst,
3759 struct isl_basic_map *src, isl_int *c,
3760 unsigned len, int oppose)
3762 struct isl_basic_map *copy = NULL;
3763 int is_empty;
3764 int k;
3765 unsigned total;
3767 copy = isl_basic_map_copy(src);
3768 copy = isl_basic_map_cow(copy);
3769 if (!copy)
3770 goto error;
3771 copy = isl_basic_map_extend_constraints(copy, 0, 1);
3772 k = isl_basic_map_alloc_inequality(copy);
3773 if (k < 0)
3774 goto error;
3775 if (oppose)
3776 isl_seq_neg(copy->ineq[k], c, len);
3777 else
3778 isl_seq_cpy(copy->ineq[k], c, len);
3779 total = 1 + isl_basic_map_total_dim(copy);
3780 isl_seq_clr(copy->ineq[k]+len, total - len);
3781 isl_inequality_negate(copy, k);
3782 copy = isl_basic_map_simplify(copy);
3783 copy = isl_basic_map_finalize(copy);
3784 is_empty = isl_basic_map_is_empty(copy);
3785 if (is_empty < 0)
3786 goto error;
3787 if (!is_empty)
3788 dst = isl_map_add(dst, copy);
3789 else
3790 isl_basic_map_free(copy);
3791 return dst;
3792 error:
3793 isl_basic_map_free(copy);
3794 isl_map_free(dst);
3795 return NULL;
3798 static struct isl_map *subtract(struct isl_map *map, struct isl_basic_map *bmap)
3800 int i, j, k;
3801 unsigned flags = 0;
3802 struct isl_map *rest = NULL;
3803 unsigned max;
3804 unsigned total = isl_basic_map_total_dim(bmap);
3806 assert(bmap);
3808 if (!map)
3809 goto error;
3811 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
3812 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3814 max = map->n * (2 * bmap->n_eq + bmap->n_ineq);
3815 rest = isl_map_alloc_dim(isl_dim_copy(map->dim), max, flags);
3816 if (!rest)
3817 goto error;
3819 for (i = 0; i < map->n; ++i) {
3820 map->p[i] = isl_basic_map_align_divs(map->p[i], bmap);
3821 if (!map->p[i])
3822 goto error;
3825 for (j = 0; j < map->n; ++j)
3826 map->p[j] = isl_basic_map_cow(map->p[j]);
3828 for (i = 0; i < bmap->n_eq; ++i) {
3829 for (j = 0; j < map->n; ++j) {
3830 rest = add_cut_constraint(rest,
3831 map->p[j], bmap->eq[i], 1+total, 0);
3832 if (!rest)
3833 goto error;
3835 rest = add_cut_constraint(rest,
3836 map->p[j], bmap->eq[i], 1+total, 1);
3837 if (!rest)
3838 goto error;
3840 map->p[j] = isl_basic_map_extend_constraints(map->p[j],
3841 1, 0);
3842 if (!map->p[j])
3843 goto error;
3844 k = isl_basic_map_alloc_equality(map->p[j]);
3845 if (k < 0)
3846 goto error;
3847 isl_seq_cpy(map->p[j]->eq[k], bmap->eq[i], 1+total);
3848 isl_seq_clr(map->p[j]->eq[k]+1+total,
3849 map->p[j]->n_div - bmap->n_div);
3853 for (i = 0; i < bmap->n_ineq; ++i) {
3854 for (j = 0; j < map->n; ++j) {
3855 rest = add_cut_constraint(rest,
3856 map->p[j], bmap->ineq[i], 1+total, 0);
3857 if (!rest)
3858 goto error;
3860 map->p[j] = isl_basic_map_extend_constraints(map->p[j],
3861 0, 1);
3862 if (!map->p[j])
3863 goto error;
3864 k = isl_basic_map_alloc_inequality(map->p[j]);
3865 if (k < 0)
3866 goto error;
3867 isl_seq_cpy(map->p[j]->ineq[k], bmap->ineq[i], 1+total);
3868 isl_seq_clr(map->p[j]->ineq[k]+1+total,
3869 map->p[j]->n_div - bmap->n_div);
3873 isl_map_free(map);
3874 return rest;
3875 error:
3876 isl_map_free(map);
3877 isl_map_free(rest);
3878 return NULL;
3881 struct isl_map *isl_map_subtract(struct isl_map *map1, struct isl_map *map2)
3883 int i;
3884 if (!map1 || !map2)
3885 goto error;
3887 isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
3889 if (isl_map_is_empty(map2)) {
3890 isl_map_free(map2);
3891 return map1;
3894 map1 = isl_map_compute_divs(map1);
3895 map2 = isl_map_compute_divs(map2);
3896 if (!map1 || !map2)
3897 goto error;
3899 for (i = 0; map1 && i < map2->n; ++i)
3900 map1 = subtract(map1, map2->p[i]);
3902 isl_map_free(map2);
3903 return map1;
3904 error:
3905 isl_map_free(map1);
3906 isl_map_free(map2);
3907 return NULL;
3910 struct isl_set *isl_set_subtract(struct isl_set *set1, struct isl_set *set2)
3912 return (struct isl_set *)
3913 isl_map_subtract(
3914 (struct isl_map *)set1, (struct isl_map *)set2);
3917 struct isl_set *isl_set_apply(struct isl_set *set, struct isl_map *map)
3919 if (!set || !map)
3920 goto error;
3921 isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
3922 map = isl_map_intersect_domain(map, set);
3923 set = isl_map_range(map);
3924 return set;
3925 error:
3926 isl_set_free(set);
3927 isl_map_free(map);
3928 return NULL;
3931 /* There is no need to cow as removing empty parts doesn't change
3932 * the meaning of the set.
3934 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
3936 int i;
3938 if (!map)
3939 return NULL;
3941 for (i = map->n-1; i >= 0; --i) {
3942 if (!ISL_F_ISSET(map->p[i], ISL_BASIC_MAP_EMPTY))
3943 continue;
3944 isl_basic_map_free(map->p[i]);
3945 if (i != map->n-1) {
3946 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3947 map->p[i] = map->p[map->n-1];
3949 map->n--;
3952 return map;
3955 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
3957 return (struct isl_set *)
3958 isl_map_remove_empty_parts((struct isl_map *)set);
3961 struct isl_basic_map *isl_map_copy_basic_map(struct isl_map *map)
3963 struct isl_basic_map *bmap;
3964 if (!map || map->n == 0)
3965 return NULL;
3966 bmap = map->p[map->n-1];
3967 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
3968 return isl_basic_map_copy(bmap);
3971 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
3973 (struct isl_basic_set *)isl_map_copy_basic_map((struct isl_map *)set);
3976 struct isl_map *isl_map_drop_basic_map(struct isl_map *map,
3977 struct isl_basic_map *bmap)
3979 int i;
3981 if (!map || !bmap)
3982 goto error;
3983 for (i = map->n-1; i >= 0; --i) {
3984 if (map->p[i] != bmap)
3985 continue;
3986 map = isl_map_cow(map);
3987 if (!map)
3988 goto error;
3989 isl_basic_map_free(map->p[i]);
3990 if (i != map->n-1) {
3991 ISL_F_CLR(map, ISL_SET_NORMALIZED);
3992 map->p[i] = map->p[map->n-1];
3994 map->n--;
3995 return map;
3997 isl_basic_map_free(bmap);
3998 return map;
3999 error:
4000 isl_map_free(map);
4001 isl_basic_map_free(bmap);
4002 return NULL;
4005 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
4006 struct isl_basic_set *bset)
4008 (struct isl_set *)isl_map_drop_basic_map((struct isl_map *)set,
4009 (struct isl_basic_map *)bset);
4012 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
4013 * for any common value of the parameters and dimensions preceding dim
4014 * in both basic sets, the values of dimension pos in bset1 are
4015 * smaller or larger than those in bset2.
4017 * Returns
4018 * 1 if bset1 follows bset2
4019 * -1 if bset1 precedes bset2
4020 * 0 if bset1 and bset2 are incomparable
4021 * -2 if some error occurred.
4023 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
4024 struct isl_basic_set *bset2, int pos)
4026 struct isl_dim *dims;
4027 struct isl_basic_map *bmap1 = NULL;
4028 struct isl_basic_map *bmap2 = NULL;
4029 struct isl_ctx *ctx;
4030 struct isl_vec *obj;
4031 unsigned total;
4032 unsigned nparam;
4033 unsigned dim1, dim2;
4034 isl_int num, den;
4035 enum isl_lp_result res;
4036 int cmp;
4038 if (!bset1 || !bset2)
4039 return -2;
4041 nparam = isl_basic_set_n_param(bset1);
4042 dim1 = isl_basic_set_n_dim(bset1);
4043 dim2 = isl_basic_set_n_dim(bset2);
4044 dims = isl_dim_alloc(bset1->ctx, nparam, pos, dim1 - pos);
4045 bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1), dims);
4046 dims = isl_dim_alloc(bset2->ctx, nparam, pos, dim2 - pos);
4047 bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims);
4048 if (!bmap1 || !bmap2)
4049 goto error;
4050 bmap1 = isl_basic_map_cow(bmap1);
4051 bmap1 = isl_basic_map_extend(bmap1, nparam,
4052 pos, (dim1 - pos) + (dim2 - pos),
4053 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
4054 bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
4055 if (!bmap1)
4056 goto error;
4057 total = isl_basic_map_total_dim(bmap1);
4058 ctx = bmap1->ctx;
4059 obj = isl_vec_alloc(ctx, 1 + total);
4060 isl_seq_clr(obj->block.data, 1 + total);
4061 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
4062 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
4063 if (!obj)
4064 goto error;
4065 isl_int_init(num);
4066 isl_int_init(den);
4067 res = isl_solve_lp(bmap1, 0, obj->block.data, ctx->one, &num, &den);
4068 if (res == isl_lp_empty)
4069 cmp = 0;
4070 else if (res == isl_lp_ok && isl_int_is_pos(num))
4071 cmp = 1;
4072 else if ((res == isl_lp_ok && isl_int_is_neg(num)) ||
4073 res == isl_lp_unbounded)
4074 cmp = -1;
4075 else
4076 cmp = -2;
4077 isl_int_clear(num);
4078 isl_int_clear(den);
4079 isl_basic_map_free(bmap1);
4080 isl_vec_free(obj);
4081 return cmp;
4082 error:
4083 isl_basic_map_free(bmap1);
4084 isl_basic_map_free(bmap2);
4085 return -2;
4088 static int isl_basic_map_fast_has_fixed_var(struct isl_basic_map *bmap,
4089 unsigned pos, isl_int *val)
4091 int i;
4092 int d;
4093 unsigned total;
4095 if (!bmap)
4096 return -1;
4097 total = isl_basic_map_total_dim(bmap);
4098 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
4099 for (; d+1 > pos; --d)
4100 if (!isl_int_is_zero(bmap->eq[i][1+d]))
4101 break;
4102 if (d != pos)
4103 continue;
4104 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
4105 return 0;
4106 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
4107 return 0;
4108 if (!isl_int_is_one(bmap->eq[i][1+d]))
4109 return 0;
4110 if (val)
4111 isl_int_neg(*val, bmap->eq[i][0]);
4112 return 1;
4114 return 0;
4117 static int isl_map_fast_has_fixed_var(struct isl_map *map,
4118 unsigned pos, isl_int *val)
4120 int i;
4121 isl_int v;
4122 isl_int tmp;
4123 int fixed;
4125 if (!map)
4126 return -1;
4127 if (map->n == 0)
4128 return 0;
4129 if (map->n == 1)
4130 return isl_basic_map_fast_has_fixed_var(map->p[0], pos, val);
4131 isl_int_init(v);
4132 isl_int_init(tmp);
4133 fixed = isl_basic_map_fast_has_fixed_var(map->p[0], pos, &v);
4134 for (i = 1; fixed == 1 && i < map->n; ++i) {
4135 fixed = isl_basic_map_fast_has_fixed_var(map->p[i], pos, &tmp);
4136 if (fixed == 1 && isl_int_ne(tmp, v))
4137 fixed = 0;
4139 if (val)
4140 isl_int_set(*val, v);
4141 isl_int_clear(tmp);
4142 isl_int_clear(v);
4143 return fixed;
4146 static int isl_basic_set_fast_has_fixed_var(struct isl_basic_set *bset,
4147 unsigned pos, isl_int *val)
4149 return isl_basic_map_fast_has_fixed_var((struct isl_basic_map *)bset,
4150 pos, val);
4153 static int isl_set_fast_has_fixed_var(struct isl_set *set, unsigned pos,
4154 isl_int *val)
4156 return isl_map_fast_has_fixed_var((struct isl_map *)set, pos, val);
4159 int isl_basic_map_fast_is_fixed(struct isl_basic_map *bmap,
4160 enum isl_dim_type type, unsigned pos, isl_int *val)
4162 if (pos >= isl_basic_map_dim(bmap, type))
4163 return -1;
4164 return isl_basic_map_fast_has_fixed_var(bmap,
4165 isl_basic_map_offset(bmap, type) - 1 + pos, val);
4168 /* Check if dimension dim has fixed value and if so and if val is not NULL,
4169 * then return this fixed value in *val.
4171 int isl_basic_set_fast_dim_is_fixed(struct isl_basic_set *bset, unsigned dim,
4172 isl_int *val)
4174 return isl_basic_set_fast_has_fixed_var(bset,
4175 isl_basic_set_n_param(bset) + dim, val);
4178 /* Check if dimension dim has fixed value and if so and if val is not NULL,
4179 * then return this fixed value in *val.
4181 int isl_set_fast_dim_is_fixed(struct isl_set *set, unsigned dim, isl_int *val)
4183 return isl_set_fast_has_fixed_var(set, isl_set_n_param(set) + dim, val);
4186 /* Check if input variable in has fixed value and if so and if val is not NULL,
4187 * then return this fixed value in *val.
4189 int isl_map_fast_input_is_fixed(struct isl_map *map, unsigned in, isl_int *val)
4191 return isl_map_fast_has_fixed_var(map, isl_map_n_param(map) + in, val);
4194 /* Check if dimension dim has an (obvious) fixed lower bound and if so
4195 * and if val is not NULL, then return this lower bound in *val.
4197 int isl_basic_set_fast_dim_has_fixed_lower_bound(struct isl_basic_set *bset,
4198 unsigned dim, isl_int *val)
4200 int i, i_eq = -1, i_ineq = -1;
4201 isl_int *c;
4202 unsigned total;
4203 unsigned nparam;
4205 if (!bset)
4206 return -1;
4207 total = isl_basic_set_total_dim(bset);
4208 nparam = isl_basic_set_n_param(bset);
4209 for (i = 0; i < bset->n_eq; ++i) {
4210 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
4211 continue;
4212 if (i_eq != -1)
4213 return 0;
4214 i_eq = i;
4216 for (i = 0; i < bset->n_ineq; ++i) {
4217 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
4218 continue;
4219 if (i_eq != -1 || i_ineq != -1)
4220 return 0;
4221 i_ineq = i;
4223 if (i_eq == -1 && i_ineq == -1)
4224 return 0;
4225 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
4226 /* The coefficient should always be one due to normalization. */
4227 if (!isl_int_is_one(c[1+nparam+dim]))
4228 return 0;
4229 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
4230 return 0;
4231 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
4232 total - nparam - dim - 1) != -1)
4233 return 0;
4234 if (val)
4235 isl_int_neg(*val, c[0]);
4236 return 1;
4239 int isl_set_fast_dim_has_fixed_lower_bound(struct isl_set *set,
4240 unsigned dim, isl_int *val)
4242 int i;
4243 isl_int v;
4244 isl_int tmp;
4245 int fixed;
4247 if (!set)
4248 return -1;
4249 if (set->n == 0)
4250 return 0;
4251 if (set->n == 1)
4252 return isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[0],
4253 dim, val);
4254 isl_int_init(v);
4255 isl_int_init(tmp);
4256 fixed = isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[0],
4257 dim, &v);
4258 for (i = 1; fixed == 1 && i < set->n; ++i) {
4259 fixed = isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[i],
4260 dim, &tmp);
4261 if (fixed == 1 && isl_int_ne(tmp, v))
4262 fixed = 0;
4264 if (val)
4265 isl_int_set(*val, v);
4266 isl_int_clear(tmp);
4267 isl_int_clear(v);
4268 return fixed;
4271 struct constraint {
4272 unsigned size;
4273 isl_int *c;
4276 static int qsort_constraint_cmp(const void *p1, const void *p2)
4278 const struct constraint *c1 = (const struct constraint *)p1;
4279 const struct constraint *c2 = (const struct constraint *)p2;
4280 unsigned size = isl_min(c1->size, c2->size);
4281 return isl_seq_cmp(c1->c, c2->c, size);
4284 static struct isl_basic_map *isl_basic_map_sort_constraints(
4285 struct isl_basic_map *bmap)
4287 int i;
4288 struct constraint *c;
4289 unsigned total;
4291 if (!bmap)
4292 return NULL;
4293 total = isl_basic_map_total_dim(bmap);
4294 c = isl_alloc_array(bmap->ctx, struct constraint, bmap->n_ineq);
4295 if (!c)
4296 goto error;
4297 for (i = 0; i < bmap->n_ineq; ++i) {
4298 c[i].size = total;
4299 c[i].c = bmap->ineq[i];
4301 qsort(c, bmap->n_ineq, sizeof(struct constraint), qsort_constraint_cmp);
4302 for (i = 0; i < bmap->n_ineq; ++i)
4303 bmap->ineq[i] = c[i].c;
4304 free(c);
4305 return bmap;
4306 error:
4307 isl_basic_map_free(bmap);
4308 return NULL;
4311 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
4313 if (!bmap)
4314 return NULL;
4315 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
4316 return bmap;
4317 bmap = isl_basic_map_convex_hull(bmap);
4318 bmap = isl_basic_map_sort_constraints(bmap);
4319 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
4320 return bmap;
4323 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
4325 return (struct isl_basic_set *)isl_basic_map_normalize(
4326 (struct isl_basic_map *)bset);
4329 static int isl_basic_map_fast_cmp(const struct isl_basic_map *bmap1,
4330 const struct isl_basic_map *bmap2)
4332 int i, cmp;
4333 unsigned total;
4335 if (bmap1 == bmap2)
4336 return 0;
4337 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
4338 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
4339 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
4340 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
4341 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
4342 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
4343 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
4344 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
4345 return 0;
4346 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
4347 return 1;
4348 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
4349 return -1;
4350 if (bmap1->n_eq != bmap2->n_eq)
4351 return bmap1->n_eq - bmap2->n_eq;
4352 if (bmap1->n_ineq != bmap2->n_ineq)
4353 return bmap1->n_ineq - bmap2->n_ineq;
4354 if (bmap1->n_div != bmap2->n_div)
4355 return bmap1->n_div - bmap2->n_div;
4356 total = isl_basic_map_total_dim(bmap1);
4357 for (i = 0; i < bmap1->n_eq; ++i) {
4358 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
4359 if (cmp)
4360 return cmp;
4362 for (i = 0; i < bmap1->n_ineq; ++i) {
4363 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
4364 if (cmp)
4365 return cmp;
4367 for (i = 0; i < bmap1->n_div; ++i) {
4368 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
4369 if (cmp)
4370 return cmp;
4372 return 0;
4375 static int isl_basic_map_fast_is_equal(struct isl_basic_map *bmap1,
4376 struct isl_basic_map *bmap2)
4378 return isl_basic_map_fast_cmp(bmap1, bmap2) == 0;
4381 static int qsort_bmap_cmp(const void *p1, const void *p2)
4383 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
4384 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
4386 return isl_basic_map_fast_cmp(bmap1, bmap2);
4389 /* We normalize in place, but if anything goes wrong we need
4390 * to return NULL, so we need to make sure we don't change the
4391 * meaning of any possible other copies of map.
4393 struct isl_map *isl_map_normalize(struct isl_map *map)
4395 int i, j;
4396 struct isl_basic_map *bmap;
4398 if (!map)
4399 return NULL;
4400 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
4401 return map;
4402 for (i = 0; i < map->n; ++i) {
4403 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
4404 if (!bmap)
4405 goto error;
4406 isl_basic_map_free(map->p[i]);
4407 map->p[i] = bmap;
4409 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
4410 ISL_F_SET(map, ISL_MAP_NORMALIZED);
4411 map = isl_map_remove_empty_parts(map);
4412 if (!map)
4413 return NULL;
4414 for (i = map->n - 1; i >= 1; --i) {
4415 if (!isl_basic_map_fast_is_equal(map->p[i-1], map->p[i]))
4416 continue;
4417 isl_basic_map_free(map->p[i-1]);
4418 for (j = i; j < map->n; ++j)
4419 map->p[j-1] = map->p[j];
4420 map->n--;
4422 return map;
4423 error:
4424 isl_map_free(map);
4425 return NULL;
4429 struct isl_set *isl_set_normalize(struct isl_set *set)
4431 return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
4434 int isl_map_fast_is_equal(struct isl_map *map1, struct isl_map *map2)
4436 int i;
4437 int equal;
4439 if (!map1 || !map2)
4440 return -1;
4442 if (map1 == map2)
4443 return 1;
4444 if (!isl_dim_equal(map1->dim, map2->dim))
4445 return 0;
4447 map1 = isl_map_copy(map1);
4448 map2 = isl_map_copy(map2);
4449 map1 = isl_map_normalize(map1);
4450 map2 = isl_map_normalize(map2);
4451 if (!map1 || !map2)
4452 goto error;
4453 equal = map1->n == map2->n;
4454 for (i = 0; equal && i < map1->n; ++i) {
4455 equal = isl_basic_map_fast_is_equal(map1->p[i], map2->p[i]);
4456 if (equal < 0)
4457 goto error;
4459 isl_map_free(map1);
4460 isl_map_free(map2);
4461 return equal;
4462 error:
4463 isl_map_free(map1);
4464 isl_map_free(map2);
4465 return -1;
4468 int isl_set_fast_is_equal(struct isl_set *set1, struct isl_set *set2)
4470 return isl_map_fast_is_equal((struct isl_map *)set1,
4471 (struct isl_map *)set2);
4474 /* Return an interval that ranges from min to max (inclusive)
4476 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
4477 isl_int min, isl_int max)
4479 int k;
4480 struct isl_basic_set *bset = NULL;
4482 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
4483 if (!bset)
4484 goto error;
4486 k = isl_basic_set_alloc_inequality(bset);
4487 if (k < 0)
4488 goto error;
4489 isl_int_set_si(bset->ineq[k][1], 1);
4490 isl_int_neg(bset->ineq[k][0], min);
4492 k = isl_basic_set_alloc_inequality(bset);
4493 if (k < 0)
4494 goto error;
4495 isl_int_set_si(bset->ineq[k][1], -1);
4496 isl_int_set(bset->ineq[k][0], max);
4498 return bset;
4499 error:
4500 isl_basic_set_free(bset);
4501 return NULL;
4504 /* Return the Cartesian product of the basic sets in list (in the given order).
4506 struct isl_basic_set *isl_basic_set_product(struct isl_basic_set_list *list)
4508 int i;
4509 unsigned dim;
4510 unsigned nparam;
4511 unsigned extra;
4512 unsigned n_eq;
4513 unsigned n_ineq;
4514 struct isl_basic_set *product = NULL;
4516 if (!list)
4517 goto error;
4518 isl_assert(list->ctx, list->n > 0, goto error);
4519 isl_assert(list->ctx, list->p[0], goto error);
4520 nparam = isl_basic_set_n_param(list->p[0]);
4521 dim = isl_basic_set_n_dim(list->p[0]);
4522 extra = list->p[0]->n_div;
4523 n_eq = list->p[0]->n_eq;
4524 n_ineq = list->p[0]->n_ineq;
4525 for (i = 1; i < list->n; ++i) {
4526 isl_assert(list->ctx, list->p[i], goto error);
4527 isl_assert(list->ctx,
4528 nparam == isl_basic_set_n_param(list->p[i]), goto error);
4529 dim += isl_basic_set_n_dim(list->p[i]);
4530 extra += list->p[i]->n_div;
4531 n_eq += list->p[i]->n_eq;
4532 n_ineq += list->p[i]->n_ineq;
4534 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
4535 n_eq, n_ineq);
4536 if (!product)
4537 goto error;
4538 dim = 0;
4539 for (i = 0; i < list->n; ++i) {
4540 isl_basic_set_add_constraints(product,
4541 isl_basic_set_copy(list->p[i]), dim);
4542 dim += isl_basic_set_n_dim(list->p[i]);
4544 isl_basic_set_list_free(list);
4545 return product;
4546 error:
4547 isl_basic_set_free(product);
4548 isl_basic_set_list_free(list);
4549 return NULL;
4552 struct isl_basic_map *isl_basic_map_product(
4553 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4555 struct isl_dim *dim_result = NULL;
4556 struct isl_basic_map *bmap;
4557 unsigned in1, in2, out1, out2, nparam, total, pos;
4558 struct isl_dim_map *dim_map1, *dim_map2;
4560 if (!bmap1 || !bmap2)
4561 goto error;
4563 isl_assert(map1->ctx, isl_dim_match(bmap1->dim, isl_dim_param,
4564 bmap2->dim, isl_dim_param), goto error);
4565 dim_result = isl_dim_product(isl_dim_copy(bmap1->dim),
4566 isl_dim_copy(bmap2->dim));
4568 in1 = isl_basic_map_n_in(bmap1);
4569 in2 = isl_basic_map_n_in(bmap2);
4570 out1 = isl_basic_map_n_out(bmap1);
4571 out2 = isl_basic_map_n_out(bmap2);
4572 nparam = isl_basic_map_n_param(bmap1);
4574 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
4575 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4576 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
4577 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4578 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
4579 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4580 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
4581 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
4582 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
4583 isl_dim_map_div(dim_map1, bmap1, pos += out2);
4584 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4586 bmap = isl_basic_map_alloc_dim(dim_result,
4587 bmap1->n_div + bmap2->n_div,
4588 bmap1->n_eq + bmap2->n_eq,
4589 bmap1->n_ineq + bmap2->n_ineq);
4590 bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
4591 bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
4592 bmap = isl_basic_map_simplify(bmap);
4593 return isl_basic_map_finalize(bmap);
4594 error:
4595 isl_basic_map_free(bmap1);
4596 isl_basic_map_free(bmap2);
4597 return NULL;
4600 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
4602 struct isl_map *isl_map_product(struct isl_map *map1, struct isl_map *map2)
4604 unsigned flags = 0;
4605 struct isl_map *result;
4606 int i, j;
4608 if (!map1 || !map2)
4609 goto error;
4611 isl_assert(map1->ctx, isl_dim_match(map1->dim, isl_dim_param,
4612 map2->dim, isl_dim_param), goto error);
4614 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
4615 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
4616 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
4618 result = isl_map_alloc_dim(isl_dim_product(isl_dim_copy(map1->dim),
4619 isl_dim_copy(map2->dim)),
4620 map1->n * map2->n, flags);
4621 if (!result)
4622 goto error;
4623 for (i = 0; i < map1->n; ++i)
4624 for (j = 0; j < map2->n; ++j) {
4625 struct isl_basic_map *part;
4626 part = isl_basic_map_product(
4627 isl_basic_map_copy(map1->p[i]),
4628 isl_basic_map_copy(map2->p[j]));
4629 if (isl_basic_map_is_empty(part))
4630 isl_basic_map_free(part);
4631 else
4632 result = isl_map_add(result, part);
4633 if (!result)
4634 goto error;
4636 isl_map_free(map1);
4637 isl_map_free(map2);
4638 return result;
4639 error:
4640 isl_map_free(map1);
4641 isl_map_free(map2);
4642 return NULL;
4645 /* Given two set A and B, construct its Cartesian product A x B.
4647 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
4649 return (struct isl_set *)isl_map_product((struct isl_map *)set1,
4650 (struct isl_map *)set2);
4653 uint32_t isl_basic_set_get_hash(struct isl_basic_set *bset)
4655 int i;
4656 uint32_t hash;
4657 unsigned total;
4659 if (!bset)
4660 return 0;
4661 bset = isl_basic_set_copy(bset);
4662 bset = isl_basic_set_normalize(bset);
4663 if (!bset)
4664 return 0;
4665 total = isl_basic_set_total_dim(bset);
4666 isl_hash_byte(hash, bset->n_eq & 0xFF);
4667 for (i = 0; i < bset->n_eq; ++i) {
4668 uint32_t c_hash;
4669 c_hash = isl_seq_get_hash(bset->eq[i], 1 + total);
4670 isl_hash_hash(hash, c_hash);
4672 isl_hash_byte(hash, bset->n_ineq & 0xFF);
4673 for (i = 0; i < bset->n_ineq; ++i) {
4674 uint32_t c_hash;
4675 c_hash = isl_seq_get_hash(bset->ineq[i], 1 + total);
4676 isl_hash_hash(hash, c_hash);
4678 isl_hash_byte(hash, bset->n_div & 0xFF);
4679 for (i = 0; i < bset->n_div; ++i) {
4680 uint32_t c_hash;
4681 if (isl_int_is_zero(bset->div[i][0]))
4682 continue;
4683 isl_hash_byte(hash, i & 0xFF);
4684 c_hash = isl_seq_get_hash(bset->div[i], 1 + 1 + total);
4685 isl_hash_hash(hash, c_hash);
4687 isl_basic_set_free(bset);
4688 return hash;
4691 uint32_t isl_set_get_hash(struct isl_set *set)
4693 int i;
4694 uint32_t hash;
4696 if (!set)
4697 return 0;
4698 set = isl_set_copy(set);
4699 set = isl_set_normalize(set);
4700 if (!set)
4701 return 0;
4703 hash = isl_hash_init();
4704 for (i = 0; i < set->n; ++i) {
4705 uint32_t bset_hash;
4706 bset_hash = isl_basic_set_get_hash(set->p[i]);
4707 isl_hash_hash(hash, bset_hash);
4710 isl_set_free(set);
4712 return hash;
4715 /* Check if the value for dimension dim is completely determined
4716 * by the values of the other parameters and variables.
4717 * That is, check if dimension dim is involved in an equality.
4719 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
4721 int i;
4722 unsigned nparam;
4724 if (!bset)
4725 return -1;
4726 nparam = isl_basic_set_n_param(bset);
4727 for (i = 0; i < bset->n_eq; ++i)
4728 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
4729 return 1;
4730 return 0;
4733 /* Check if the value for dimension dim is completely determined
4734 * by the values of the other parameters and variables.
4735 * That is, check if dimension dim is involved in an equality
4736 * for each of the subsets.
4738 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
4740 int i;
4742 if (!set)
4743 return -1;
4744 for (i = 0; i < set->n; ++i) {
4745 int unique;
4746 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
4747 if (unique != 1)
4748 return unique;
4750 return 1;