combine isl_pip_basic_map_lex{min,max} into isl_pip_basic_map_lexopt
[isl.git] / isl_map.c
blob0f2f8040842d838b1241389ee31a52e579983e0d
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 static struct isl_map *isl_basic_map_partial_lexopt(
2967 struct isl_basic_map *bmap, struct isl_basic_set *dom,
2968 struct isl_set **empty, int max)
2970 return isl_pip_basic_map_lexopt(bmap, dom, empty, max);
2973 struct isl_map *isl_basic_map_partial_lexmax(
2974 struct isl_basic_map *bmap, struct isl_basic_set *dom,
2975 struct isl_set **empty)
2977 return isl_basic_map_partial_lexopt(bmap, dom, empty, 1);
2980 struct isl_map *isl_basic_map_partial_lexmin(
2981 struct isl_basic_map *bmap, struct isl_basic_set *dom,
2982 struct isl_set **empty)
2984 return isl_basic_map_partial_lexopt(bmap, dom, empty, 0);
2987 struct isl_set *isl_basic_set_partial_lexmin(
2988 struct isl_basic_set *bset, struct isl_basic_set *dom,
2989 struct isl_set **empty)
2991 return (struct isl_set *)
2992 isl_basic_map_partial_lexmin((struct isl_basic_map *)bset,
2993 dom, empty);
2996 struct isl_set *isl_basic_set_partial_lexmax(
2997 struct isl_basic_set *bset, struct isl_basic_set *dom,
2998 struct isl_set **empty)
3000 return (struct isl_set *)
3001 isl_basic_map_partial_lexmax((struct isl_basic_map *)bset,
3002 dom, empty);
3005 struct isl_set *isl_basic_set_lexmin(struct isl_basic_set *bset)
3007 struct isl_basic_map *bmap = NULL;
3008 struct isl_basic_set *dom = NULL;
3009 struct isl_map *min;
3010 struct isl_dim *param_dim;
3012 if (!bset)
3013 goto error;
3014 bmap = isl_basic_map_from_basic_set(bset, isl_dim_copy(bset->dim));
3015 if (!bmap)
3016 goto error;
3017 param_dim = isl_dim_domain(isl_dim_copy(bmap->dim));
3018 dom = isl_basic_set_universe(param_dim);
3019 if (!dom)
3020 goto error;
3021 min = isl_basic_map_partial_lexmin(bmap, dom, NULL);
3022 return isl_map_range(min);
3023 error:
3024 isl_basic_map_free(bmap);
3025 return NULL;
3028 /* If bmap contains any unknown divs, then compute explicit
3029 * expressions for them. However, this computation may be
3030 * quite expensive, so first try to remove divs that aren't
3031 * strictly needed.
3033 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
3035 int i;
3036 unsigned off;
3038 if (!bmap)
3039 return NULL;
3040 off = isl_dim_total(bmap->dim);
3041 for (i = 0; i < bmap->n_div; ++i) {
3042 if (isl_int_is_zero(bmap->div[i][0]))
3043 break;
3044 isl_assert(bmap->ctx, isl_int_is_zero(bmap->div[i][1+1+off+i]),
3045 goto error);
3047 if (i == bmap->n_div)
3048 return isl_map_from_basic_map(bmap);
3049 bmap = isl_basic_map_drop_redundant_divs(bmap);
3050 if (!bmap)
3051 goto error;
3052 for (i = 0; i < bmap->n_div; ++i)
3053 if (isl_int_is_zero(bmap->div[i][0]))
3054 break;
3055 if (i == bmap->n_div)
3056 return isl_map_from_basic_map(bmap);
3057 struct isl_map *map = isl_pip_basic_map_compute_divs(bmap);
3058 return map;
3059 error:
3060 isl_basic_map_free(bmap);
3061 return NULL;
3064 struct isl_map *isl_map_compute_divs(struct isl_map *map)
3066 int i;
3067 struct isl_map *res;
3069 if (!map)
3070 return NULL;
3071 if (map->n == 0)
3072 return map;
3073 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
3074 for (i = 1 ; i < map->n; ++i) {
3075 struct isl_map *r2;
3076 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
3077 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
3078 res = isl_map_union_disjoint(res, r2);
3079 else
3080 res = isl_map_union(res, r2);
3082 isl_map_free(map);
3084 return res;
3087 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
3089 return (struct isl_set *)
3090 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
3093 struct isl_set *isl_set_compute_divs(struct isl_set *set)
3095 return (struct isl_set *)
3096 isl_map_compute_divs((struct isl_map *)set);
3099 struct isl_set *isl_map_domain(struct isl_map *map)
3101 int i;
3102 struct isl_set *set;
3104 if (!map)
3105 goto error;
3107 map = isl_map_cow(map);
3108 if (!map)
3109 return NULL;
3111 set = (struct isl_set *)map;
3112 set->dim = isl_dim_domain(set->dim);
3113 if (!set->dim)
3114 goto error;
3115 for (i = 0; i < map->n; ++i) {
3116 set->p[i] = isl_basic_map_domain(map->p[i]);
3117 if (!set->p[i])
3118 goto error;
3120 ISL_F_CLR(set, ISL_MAP_DISJOINT);
3121 ISL_F_CLR(set, ISL_SET_NORMALIZED);
3122 return set;
3123 error:
3124 isl_map_free(map);
3125 return NULL;
3128 struct isl_map *isl_map_union_disjoint(
3129 struct isl_map *map1, struct isl_map *map2)
3131 int i;
3132 unsigned flags = 0;
3133 struct isl_map *map = NULL;
3135 if (!map1 || !map2)
3136 goto error;
3138 if (map1->n == 0) {
3139 isl_map_free(map1);
3140 return map2;
3142 if (map2->n == 0) {
3143 isl_map_free(map2);
3144 return map1;
3147 isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
3149 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
3150 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
3151 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3153 map = isl_map_alloc_dim(isl_dim_copy(map1->dim),
3154 map1->n + map2->n, flags);
3155 if (!map)
3156 goto error;
3157 for (i = 0; i < map1->n; ++i) {
3158 map = isl_map_add(map,
3159 isl_basic_map_copy(map1->p[i]));
3160 if (!map)
3161 goto error;
3163 for (i = 0; i < map2->n; ++i) {
3164 map = isl_map_add(map,
3165 isl_basic_map_copy(map2->p[i]));
3166 if (!map)
3167 goto error;
3169 isl_map_free(map1);
3170 isl_map_free(map2);
3171 return map;
3172 error:
3173 isl_map_free(map);
3174 isl_map_free(map1);
3175 isl_map_free(map2);
3176 return NULL;
3179 struct isl_map *isl_map_union(struct isl_map *map1, struct isl_map *map2)
3181 map1 = isl_map_union_disjoint(map1, map2);
3182 if (!map1)
3183 return NULL;
3184 if (map1->n > 1)
3185 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
3186 return map1;
3189 struct isl_set *isl_set_union_disjoint(
3190 struct isl_set *set1, struct isl_set *set2)
3192 return (struct isl_set *)
3193 isl_map_union_disjoint(
3194 (struct isl_map *)set1, (struct isl_map *)set2);
3197 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
3199 return (struct isl_set *)
3200 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
3203 struct isl_map *isl_map_intersect_range(
3204 struct isl_map *map, struct isl_set *set)
3206 unsigned flags = 0;
3207 struct isl_map *result;
3208 int i, j;
3210 if (!map || !set)
3211 goto error;
3213 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
3214 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
3215 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3217 result = isl_map_alloc_dim(isl_dim_copy(map->dim),
3218 map->n * set->n, flags);
3219 if (!result)
3220 goto error;
3221 for (i = 0; i < map->n; ++i)
3222 for (j = 0; j < set->n; ++j) {
3223 result = isl_map_add(result,
3224 isl_basic_map_intersect_range(
3225 isl_basic_map_copy(map->p[i]),
3226 isl_basic_set_copy(set->p[j])));
3227 if (!result)
3228 goto error;
3230 isl_map_free(map);
3231 isl_set_free(set);
3232 return result;
3233 error:
3234 isl_map_free(map);
3235 isl_set_free(set);
3236 return NULL;
3239 struct isl_map *isl_map_intersect_domain(
3240 struct isl_map *map, struct isl_set *set)
3242 return isl_map_reverse(
3243 isl_map_intersect_range(isl_map_reverse(map), set));
3246 struct isl_map *isl_map_apply_domain(
3247 struct isl_map *map1, struct isl_map *map2)
3249 if (!map1 || !map2)
3250 goto error;
3251 map1 = isl_map_reverse(map1);
3252 map1 = isl_map_apply_range(map1, map2);
3253 return isl_map_reverse(map1);
3254 error:
3255 isl_map_free(map1);
3256 isl_map_free(map2);
3257 return NULL;
3260 struct isl_map *isl_map_apply_range(
3261 struct isl_map *map1, struct isl_map *map2)
3263 struct isl_dim *dim_result;
3264 struct isl_map *result;
3265 int i, j;
3266 unsigned nparam;
3267 unsigned n_in;
3268 unsigned n_out;
3270 if (!map1 || !map2)
3271 goto error;
3273 dim_result = isl_dim_join(isl_dim_copy(map1->dim),
3274 isl_dim_copy(map2->dim));
3276 result = isl_map_alloc_dim(dim_result, map1->n * map2->n, 0);
3277 if (!result)
3278 goto error;
3279 for (i = 0; i < map1->n; ++i)
3280 for (j = 0; j < map2->n; ++j) {
3281 result = isl_map_add(result,
3282 isl_basic_map_apply_range(
3283 isl_basic_map_copy(map1->p[i]),
3284 isl_basic_map_copy(map2->p[j])));
3285 if (!result)
3286 goto error;
3288 isl_map_free(map1);
3289 isl_map_free(map2);
3290 if (result && result->n <= 1)
3291 ISL_F_SET(result, ISL_MAP_DISJOINT);
3292 return result;
3293 error:
3294 isl_map_free(map1);
3295 isl_map_free(map2);
3296 return NULL;
3300 * returns range - domain
3302 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
3304 struct isl_basic_set *bset;
3305 unsigned dim;
3306 unsigned nparam;
3307 int i;
3309 if (!bmap)
3310 goto error;
3311 dim = isl_basic_map_n_in(bmap);
3312 nparam = isl_basic_map_n_param(bmap);
3313 isl_assert(bmap->ctx, dim == isl_basic_map_n_out(bmap), goto error);
3314 bset = isl_basic_set_from_basic_map(bmap);
3315 bset = isl_basic_set_cow(bset);
3316 bset = isl_basic_set_extend(bset, nparam, 3*dim, 0, dim, 0);
3317 bset = isl_basic_set_swap_vars(bset, 2*dim);
3318 for (i = 0; i < dim; ++i) {
3319 int j = isl_basic_map_alloc_equality(
3320 (struct isl_basic_map *)bset);
3321 if (j < 0)
3322 goto error;
3323 isl_seq_clr(bset->eq[j], 1 + isl_basic_set_total_dim(bset));
3324 isl_int_set_si(bset->eq[j][1+nparam+i], 1);
3325 isl_int_set_si(bset->eq[j][1+nparam+dim+i], 1);
3326 isl_int_set_si(bset->eq[j][1+nparam+2*dim+i], -1);
3328 return isl_basic_set_project_out(bset, 2*dim, 0);
3329 error:
3330 isl_basic_map_free(bmap);
3331 return NULL;
3335 * returns range - domain
3337 struct isl_set *isl_map_deltas(struct isl_map *map)
3339 int i;
3340 struct isl_set *result;
3342 if (!map)
3343 return NULL;
3345 isl_assert(map->ctx, isl_map_n_in(map) == isl_map_n_out(map), goto error);
3346 result = isl_set_alloc(map->ctx, isl_map_n_param(map),
3347 isl_map_n_in(map), map->n, map->flags);
3348 if (!result)
3349 goto error;
3350 for (i = 0; i < map->n; ++i)
3351 result = isl_set_add(result,
3352 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
3353 isl_map_free(map);
3354 return result;
3355 error:
3356 isl_map_free(map);
3357 return NULL;
3360 static struct isl_basic_map *basic_map_identity(struct isl_dim *dims)
3362 struct isl_basic_map *bmap;
3363 unsigned nparam;
3364 unsigned dim;
3365 int i;
3367 if (!dims)
3368 return NULL;
3370 nparam = dims->nparam;
3371 dim = dims->n_out;
3372 bmap = isl_basic_map_alloc_dim(dims, 0, dim, 0);
3373 if (!bmap)
3374 goto error;
3376 for (i = 0; i < dim; ++i) {
3377 int j = isl_basic_map_alloc_equality(bmap);
3378 if (j < 0)
3379 goto error;
3380 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
3381 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
3382 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
3384 return isl_basic_map_finalize(bmap);
3385 error:
3386 isl_basic_map_free(bmap);
3387 return NULL;
3390 struct isl_basic_map *isl_basic_map_identity(struct isl_dim *set_dim)
3392 struct isl_dim *dim = isl_dim_map(set_dim);
3393 if (!dim)
3394 return NULL;
3395 return basic_map_identity(dim);
3398 struct isl_basic_map *isl_basic_map_identity_like(struct isl_basic_map *model)
3400 if (!model || !model->dim)
3401 return NULL;
3402 isl_assert(model->ctx,
3403 model->dim->n_in == model->dim->n_out, return NULL);
3404 return basic_map_identity(isl_dim_copy(model->dim));
3407 static struct isl_map *map_identity(struct isl_dim *dim)
3409 struct isl_map *map = isl_map_alloc_dim(dim, 1, ISL_MAP_DISJOINT);
3410 return isl_map_add(map, basic_map_identity(isl_dim_copy(dim)));
3413 struct isl_map *isl_map_identity(struct isl_dim *set_dim)
3415 struct isl_dim *dim = isl_dim_map(set_dim);
3416 if (!dim)
3417 return NULL;
3418 return map_identity(dim);
3421 struct isl_map *isl_map_identity_like(struct isl_basic_map *model)
3423 if (!model || !model->dim)
3424 return NULL;
3425 isl_assert(model->ctx,
3426 model->dim->n_in == model->dim->n_out, return NULL);
3427 return map_identity(isl_dim_copy(model->dim));
3430 /* Construct a basic set with all set dimensions having only non-negative
3431 * values.
3433 struct isl_basic_set *isl_basic_set_positive_orthant(struct isl_dim *dims)
3435 int i;
3436 unsigned nparam;
3437 unsigned dim;
3438 struct isl_basic_set *bset;
3440 if (!dims)
3441 return NULL;
3442 nparam = dims->nparam;
3443 dim = dims->n_out;
3444 bset = isl_basic_set_alloc_dim(dims, 0, 0, dim);
3445 if (!bset)
3446 return NULL;
3447 for (i = 0; i < dim; ++i) {
3448 int k = isl_basic_set_alloc_inequality(bset);
3449 if (k < 0)
3450 goto error;
3451 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
3452 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
3454 return bset;
3455 error:
3456 isl_basic_set_free(bset);
3457 return NULL;
3460 int isl_set_is_equal(struct isl_set *set1, struct isl_set *set2)
3462 return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
3465 int isl_set_is_subset(struct isl_set *set1, struct isl_set *set2)
3467 return isl_map_is_subset(
3468 (struct isl_map *)set1, (struct isl_map *)set2);
3471 int isl_basic_map_is_subset(
3472 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3474 int is_subset;
3475 struct isl_map *map1;
3476 struct isl_map *map2;
3478 if (!bmap1 || !bmap2)
3479 return -1;
3481 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
3482 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
3484 is_subset = isl_map_is_subset(map1, map2);
3486 isl_map_free(map1);
3487 isl_map_free(map2);
3489 return is_subset;
3492 int isl_basic_map_is_equal(
3493 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3495 int is_subset;
3497 if (!bmap1 || !bmap2)
3498 return -1;
3499 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
3500 if (is_subset != 1)
3501 return is_subset;
3502 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
3503 return is_subset;
3506 int isl_basic_set_is_equal(
3507 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
3509 return isl_basic_map_is_equal(
3510 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
3513 int isl_map_is_empty(struct isl_map *map)
3515 int i;
3516 int is_empty;
3518 if (!map)
3519 return -1;
3520 for (i = 0; i < map->n; ++i) {
3521 is_empty = isl_basic_map_is_empty(map->p[i]);
3522 if (is_empty < 0)
3523 return -1;
3524 if (!is_empty)
3525 return 0;
3527 return 1;
3530 int isl_map_fast_is_empty(struct isl_map *map)
3532 return map->n == 0;
3535 int isl_set_is_empty(struct isl_set *set)
3537 return isl_map_is_empty((struct isl_map *)set);
3540 int isl_map_is_subset(struct isl_map *map1, struct isl_map *map2)
3542 int i;
3543 int is_subset = 0;
3544 struct isl_map *diff;
3546 if (!map1 || !map2)
3547 return -1;
3549 if (isl_map_is_empty(map1))
3550 return 1;
3552 if (isl_map_is_empty(map2))
3553 return 0;
3555 diff = isl_map_subtract(isl_map_copy(map1), isl_map_copy(map2));
3556 if (!diff)
3557 return -1;
3559 is_subset = isl_map_is_empty(diff);
3560 isl_map_free(diff);
3562 return is_subset;
3565 int isl_map_is_equal(struct isl_map *map1, struct isl_map *map2)
3567 int is_subset;
3569 if (!map1 || !map2)
3570 return -1;
3571 is_subset = isl_map_is_subset(map1, map2);
3572 if (is_subset != 1)
3573 return is_subset;
3574 is_subset = isl_map_is_subset(map2, map1);
3575 return is_subset;
3578 int isl_basic_map_is_strict_subset(
3579 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3581 int is_subset;
3583 if (!bmap1 || !bmap2)
3584 return -1;
3585 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
3586 if (is_subset != 1)
3587 return is_subset;
3588 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
3589 if (is_subset == -1)
3590 return is_subset;
3591 return !is_subset;
3594 int isl_basic_map_is_universe(struct isl_basic_map *bmap)
3596 if (!bmap)
3597 return -1;
3598 return bmap->n_eq == 0 && bmap->n_ineq == 0;
3601 int isl_basic_set_is_universe(struct isl_basic_set *bset)
3603 if (!bset)
3604 return -1;
3605 return bset->n_eq == 0 && bset->n_ineq == 0;
3608 int isl_basic_map_is_empty(struct isl_basic_map *bmap)
3610 struct isl_basic_set *bset = NULL;
3611 struct isl_vec *sample = NULL;
3612 int empty;
3613 unsigned total;
3615 if (!bmap)
3616 return -1;
3618 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
3619 return 1;
3621 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
3622 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
3623 copy = isl_basic_map_convex_hull(copy);
3624 empty = ISL_F_ISSET(copy, ISL_BASIC_MAP_EMPTY);
3625 isl_basic_map_free(copy);
3626 return empty;
3629 total = 1 + isl_basic_map_total_dim(bmap);
3630 if (bmap->sample && bmap->sample->size == total) {
3631 int contains = basic_map_contains(bmap, bmap->sample);
3632 if (contains < 0)
3633 return -1;
3634 if (contains)
3635 return 0;
3637 isl_vec_free(bmap->sample);
3638 bmap->sample = NULL;
3639 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
3640 if (!bset)
3641 return -1;
3642 sample = isl_basic_set_sample(bset);
3643 if (!sample)
3644 return -1;
3645 empty = sample->size == 0;
3646 isl_vec_free(bmap->sample);
3647 bmap->sample = sample;
3648 if (empty)
3649 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
3651 return empty;
3654 int isl_basic_map_fast_is_empty(struct isl_basic_map *bmap)
3656 if (!bmap)
3657 return -1;
3658 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
3661 int isl_basic_set_fast_is_empty(struct isl_basic_set *bset)
3663 if (!bset)
3664 return -1;
3665 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
3668 int isl_basic_set_is_empty(struct isl_basic_set *bset)
3670 return isl_basic_map_is_empty((struct isl_basic_map *)bset);
3673 struct isl_map *isl_basic_map_union(
3674 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3676 struct isl_map *map;
3677 if (!bmap1 || !bmap2)
3678 return NULL;
3680 isl_assert(map1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim), goto error);
3682 map = isl_map_alloc_dim(isl_dim_copy(bmap1->dim), 2, 0);
3683 if (!map)
3684 goto error;
3685 map = isl_map_add(map, bmap1);
3686 map = isl_map_add(map, bmap2);
3687 return map;
3688 error:
3689 isl_basic_map_free(bmap1);
3690 isl_basic_map_free(bmap2);
3691 return NULL;
3694 struct isl_set *isl_basic_set_union(
3695 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
3697 return (struct isl_set *)isl_basic_map_union(
3698 (struct isl_basic_map *)bset1,
3699 (struct isl_basic_map *)bset2);
3702 /* Order divs such that any div only depends on previous divs */
3703 static struct isl_basic_map *order_divs(struct isl_basic_map *bmap)
3705 int i;
3706 unsigned off = isl_dim_total(bmap->dim);
3708 for (i = 0; i < bmap->n_div; ++i) {
3709 int pos;
3710 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
3711 bmap->n_div-i);
3712 if (pos == -1)
3713 continue;
3714 swap_div(bmap, i, i + pos);
3715 --i;
3717 return bmap;
3720 /* Look for a div in dst that corresponds to the div "div" in src.
3721 * The divs before "div" in src and dst are assumed to be the same.
3723 * Returns -1 if no corresponding div was found and the position
3724 * of the corresponding div in dst otherwise.
3726 static int find_div(struct isl_basic_map *dst,
3727 struct isl_basic_map *src, unsigned div)
3729 int i;
3731 unsigned total = isl_dim_total(src->dim);
3733 isl_assert(dst->ctx, div <= dst->n_div, return -1);
3734 for (i = div; i < dst->n_div; ++i)
3735 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
3736 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
3737 dst->n_div - div) == -1)
3738 return i;
3739 return -1;
3742 struct isl_basic_map *isl_basic_map_align_divs(
3743 struct isl_basic_map *dst, struct isl_basic_map *src)
3745 int i;
3746 unsigned total = isl_dim_total(src->dim);
3748 if (!dst || !src)
3749 goto error;
3751 if (src->n_div == 0)
3752 return dst;
3754 for (i = 0; i < src->n_div; ++i)
3755 isl_assert(src->ctx, !isl_int_is_zero(src->div[i][0]), goto error);
3757 src = order_divs(src);
3758 dst = isl_basic_map_cow(dst);
3759 dst = isl_basic_map_extend_dim(dst, isl_dim_copy(dst->dim),
3760 src->n_div, 0, 2 * src->n_div);
3761 if (!dst)
3762 return NULL;
3763 for (i = 0; i < src->n_div; ++i) {
3764 int j = find_div(dst, src, i);
3765 if (j < 0) {
3766 j = isl_basic_map_alloc_div(dst);
3767 if (j < 0)
3768 goto error;
3769 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
3770 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
3771 if (add_div_constraints(dst, j) < 0)
3772 goto error;
3774 if (j != i)
3775 swap_div(dst, i, j);
3777 return dst;
3778 error:
3779 isl_basic_map_free(dst);
3780 return NULL;
3783 struct isl_basic_set *isl_basic_set_align_divs(
3784 struct isl_basic_set *dst, struct isl_basic_set *src)
3786 return (struct isl_basic_set *)isl_basic_map_align_divs(
3787 (struct isl_basic_map *)dst, (struct isl_basic_map *)src);
3790 struct isl_map *isl_map_align_divs(struct isl_map *map)
3792 int i;
3794 map = isl_map_compute_divs(map);
3795 map = isl_map_cow(map);
3796 if (!map)
3797 return NULL;
3799 for (i = 1; i < map->n; ++i)
3800 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
3801 for (i = 1; i < map->n; ++i)
3802 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
3804 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3805 return map;
3808 struct isl_set *isl_set_align_divs(struct isl_set *set)
3810 return (struct isl_set *)isl_map_align_divs((struct isl_map *)set);
3813 static struct isl_map *add_cut_constraint(struct isl_map *dst,
3814 struct isl_basic_map *src, isl_int *c,
3815 unsigned len, int oppose)
3817 struct isl_basic_map *copy = NULL;
3818 int is_empty;
3819 int k;
3820 unsigned total;
3822 copy = isl_basic_map_copy(src);
3823 copy = isl_basic_map_cow(copy);
3824 if (!copy)
3825 goto error;
3826 copy = isl_basic_map_extend_constraints(copy, 0, 1);
3827 k = isl_basic_map_alloc_inequality(copy);
3828 if (k < 0)
3829 goto error;
3830 if (oppose)
3831 isl_seq_neg(copy->ineq[k], c, len);
3832 else
3833 isl_seq_cpy(copy->ineq[k], c, len);
3834 total = 1 + isl_basic_map_total_dim(copy);
3835 isl_seq_clr(copy->ineq[k]+len, total - len);
3836 isl_inequality_negate(copy, k);
3837 copy = isl_basic_map_simplify(copy);
3838 copy = isl_basic_map_finalize(copy);
3839 is_empty = isl_basic_map_is_empty(copy);
3840 if (is_empty < 0)
3841 goto error;
3842 if (!is_empty)
3843 dst = isl_map_add(dst, copy);
3844 else
3845 isl_basic_map_free(copy);
3846 return dst;
3847 error:
3848 isl_basic_map_free(copy);
3849 isl_map_free(dst);
3850 return NULL;
3853 static struct isl_map *subtract(struct isl_map *map, struct isl_basic_map *bmap)
3855 int i, j, k;
3856 unsigned flags = 0;
3857 struct isl_map *rest = NULL;
3858 unsigned max;
3859 unsigned total = isl_basic_map_total_dim(bmap);
3861 assert(bmap);
3863 if (!map)
3864 goto error;
3866 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
3867 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3869 max = map->n * (2 * bmap->n_eq + bmap->n_ineq);
3870 rest = isl_map_alloc_dim(isl_dim_copy(map->dim), max, flags);
3871 if (!rest)
3872 goto error;
3874 for (i = 0; i < map->n; ++i) {
3875 map->p[i] = isl_basic_map_align_divs(map->p[i], bmap);
3876 if (!map->p[i])
3877 goto error;
3880 for (j = 0; j < map->n; ++j)
3881 map->p[j] = isl_basic_map_cow(map->p[j]);
3883 for (i = 0; i < bmap->n_eq; ++i) {
3884 for (j = 0; j < map->n; ++j) {
3885 rest = add_cut_constraint(rest,
3886 map->p[j], bmap->eq[i], 1+total, 0);
3887 if (!rest)
3888 goto error;
3890 rest = add_cut_constraint(rest,
3891 map->p[j], bmap->eq[i], 1+total, 1);
3892 if (!rest)
3893 goto error;
3895 map->p[j] = isl_basic_map_extend_constraints(map->p[j],
3896 1, 0);
3897 if (!map->p[j])
3898 goto error;
3899 k = isl_basic_map_alloc_equality(map->p[j]);
3900 if (k < 0)
3901 goto error;
3902 isl_seq_cpy(map->p[j]->eq[k], bmap->eq[i], 1+total);
3903 isl_seq_clr(map->p[j]->eq[k]+1+total,
3904 map->p[j]->n_div - bmap->n_div);
3908 for (i = 0; i < bmap->n_ineq; ++i) {
3909 for (j = 0; j < map->n; ++j) {
3910 rest = add_cut_constraint(rest,
3911 map->p[j], bmap->ineq[i], 1+total, 0);
3912 if (!rest)
3913 goto error;
3915 map->p[j] = isl_basic_map_extend_constraints(map->p[j],
3916 0, 1);
3917 if (!map->p[j])
3918 goto error;
3919 k = isl_basic_map_alloc_inequality(map->p[j]);
3920 if (k < 0)
3921 goto error;
3922 isl_seq_cpy(map->p[j]->ineq[k], bmap->ineq[i], 1+total);
3923 isl_seq_clr(map->p[j]->ineq[k]+1+total,
3924 map->p[j]->n_div - bmap->n_div);
3928 isl_map_free(map);
3929 return rest;
3930 error:
3931 isl_map_free(map);
3932 isl_map_free(rest);
3933 return NULL;
3936 struct isl_map *isl_map_subtract(struct isl_map *map1, struct isl_map *map2)
3938 int i;
3939 if (!map1 || !map2)
3940 goto error;
3942 isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
3944 if (isl_map_is_empty(map2)) {
3945 isl_map_free(map2);
3946 return map1;
3949 map1 = isl_map_compute_divs(map1);
3950 map2 = isl_map_compute_divs(map2);
3951 if (!map1 || !map2)
3952 goto error;
3954 for (i = 0; map1 && i < map2->n; ++i)
3955 map1 = subtract(map1, map2->p[i]);
3957 isl_map_free(map2);
3958 return map1;
3959 error:
3960 isl_map_free(map1);
3961 isl_map_free(map2);
3962 return NULL;
3965 struct isl_set *isl_set_subtract(struct isl_set *set1, struct isl_set *set2)
3967 return (struct isl_set *)
3968 isl_map_subtract(
3969 (struct isl_map *)set1, (struct isl_map *)set2);
3972 struct isl_set *isl_set_apply(struct isl_set *set, struct isl_map *map)
3974 if (!set || !map)
3975 goto error;
3976 isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
3977 map = isl_map_intersect_domain(map, set);
3978 set = isl_map_range(map);
3979 return set;
3980 error:
3981 isl_set_free(set);
3982 isl_map_free(map);
3983 return NULL;
3986 /* There is no need to cow as removing empty parts doesn't change
3987 * the meaning of the set.
3989 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
3991 int i;
3993 if (!map)
3994 return NULL;
3996 for (i = map->n-1; i >= 0; --i) {
3997 if (!ISL_F_ISSET(map->p[i], ISL_BASIC_MAP_EMPTY))
3998 continue;
3999 isl_basic_map_free(map->p[i]);
4000 if (i != map->n-1) {
4001 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4002 map->p[i] = map->p[map->n-1];
4004 map->n--;
4007 return map;
4010 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
4012 return (struct isl_set *)
4013 isl_map_remove_empty_parts((struct isl_map *)set);
4016 struct isl_basic_map *isl_map_copy_basic_map(struct isl_map *map)
4018 struct isl_basic_map *bmap;
4019 if (!map || map->n == 0)
4020 return NULL;
4021 bmap = map->p[map->n-1];
4022 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
4023 return isl_basic_map_copy(bmap);
4026 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
4028 (struct isl_basic_set *)isl_map_copy_basic_map((struct isl_map *)set);
4031 struct isl_map *isl_map_drop_basic_map(struct isl_map *map,
4032 struct isl_basic_map *bmap)
4034 int i;
4036 if (!map || !bmap)
4037 goto error;
4038 for (i = map->n-1; i >= 0; --i) {
4039 if (map->p[i] != bmap)
4040 continue;
4041 map = isl_map_cow(map);
4042 if (!map)
4043 goto error;
4044 isl_basic_map_free(map->p[i]);
4045 if (i != map->n-1) {
4046 ISL_F_CLR(map, ISL_SET_NORMALIZED);
4047 map->p[i] = map->p[map->n-1];
4049 map->n--;
4050 return map;
4052 isl_basic_map_free(bmap);
4053 return map;
4054 error:
4055 isl_map_free(map);
4056 isl_basic_map_free(bmap);
4057 return NULL;
4060 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
4061 struct isl_basic_set *bset)
4063 (struct isl_set *)isl_map_drop_basic_map((struct isl_map *)set,
4064 (struct isl_basic_map *)bset);
4067 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
4068 * for any common value of the parameters and dimensions preceding dim
4069 * in both basic sets, the values of dimension pos in bset1 are
4070 * smaller or larger than those in bset2.
4072 * Returns
4073 * 1 if bset1 follows bset2
4074 * -1 if bset1 precedes bset2
4075 * 0 if bset1 and bset2 are incomparable
4076 * -2 if some error occurred.
4078 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
4079 struct isl_basic_set *bset2, int pos)
4081 struct isl_dim *dims;
4082 struct isl_basic_map *bmap1 = NULL;
4083 struct isl_basic_map *bmap2 = NULL;
4084 struct isl_ctx *ctx;
4085 struct isl_vec *obj;
4086 unsigned total;
4087 unsigned nparam;
4088 unsigned dim1, dim2;
4089 isl_int num, den;
4090 enum isl_lp_result res;
4091 int cmp;
4093 if (!bset1 || !bset2)
4094 return -2;
4096 nparam = isl_basic_set_n_param(bset1);
4097 dim1 = isl_basic_set_n_dim(bset1);
4098 dim2 = isl_basic_set_n_dim(bset2);
4099 dims = isl_dim_alloc(bset1->ctx, nparam, pos, dim1 - pos);
4100 bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1), dims);
4101 dims = isl_dim_alloc(bset2->ctx, nparam, pos, dim2 - pos);
4102 bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims);
4103 if (!bmap1 || !bmap2)
4104 goto error;
4105 bmap1 = isl_basic_map_cow(bmap1);
4106 bmap1 = isl_basic_map_extend(bmap1, nparam,
4107 pos, (dim1 - pos) + (dim2 - pos),
4108 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
4109 bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
4110 if (!bmap1)
4111 goto error;
4112 total = isl_basic_map_total_dim(bmap1);
4113 ctx = bmap1->ctx;
4114 obj = isl_vec_alloc(ctx, 1 + total);
4115 isl_seq_clr(obj->block.data, 1 + total);
4116 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
4117 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
4118 if (!obj)
4119 goto error;
4120 isl_int_init(num);
4121 isl_int_init(den);
4122 res = isl_solve_lp(bmap1, 0, obj->block.data, ctx->one, &num, &den);
4123 if (res == isl_lp_empty)
4124 cmp = 0;
4125 else if (res == isl_lp_ok && isl_int_is_pos(num))
4126 cmp = 1;
4127 else if ((res == isl_lp_ok && isl_int_is_neg(num)) ||
4128 res == isl_lp_unbounded)
4129 cmp = -1;
4130 else
4131 cmp = -2;
4132 isl_int_clear(num);
4133 isl_int_clear(den);
4134 isl_basic_map_free(bmap1);
4135 isl_vec_free(obj);
4136 return cmp;
4137 error:
4138 isl_basic_map_free(bmap1);
4139 isl_basic_map_free(bmap2);
4140 return -2;
4143 static int isl_basic_map_fast_has_fixed_var(struct isl_basic_map *bmap,
4144 unsigned pos, isl_int *val)
4146 int i;
4147 int d;
4148 unsigned total;
4150 if (!bmap)
4151 return -1;
4152 total = isl_basic_map_total_dim(bmap);
4153 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
4154 for (; d+1 > pos; --d)
4155 if (!isl_int_is_zero(bmap->eq[i][1+d]))
4156 break;
4157 if (d != pos)
4158 continue;
4159 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
4160 return 0;
4161 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
4162 return 0;
4163 if (!isl_int_is_one(bmap->eq[i][1+d]))
4164 return 0;
4165 if (val)
4166 isl_int_neg(*val, bmap->eq[i][0]);
4167 return 1;
4169 return 0;
4172 static int isl_map_fast_has_fixed_var(struct isl_map *map,
4173 unsigned pos, isl_int *val)
4175 int i;
4176 isl_int v;
4177 isl_int tmp;
4178 int fixed;
4180 if (!map)
4181 return -1;
4182 if (map->n == 0)
4183 return 0;
4184 if (map->n == 1)
4185 return isl_basic_map_fast_has_fixed_var(map->p[0], pos, val);
4186 isl_int_init(v);
4187 isl_int_init(tmp);
4188 fixed = isl_basic_map_fast_has_fixed_var(map->p[0], pos, &v);
4189 for (i = 1; fixed == 1 && i < map->n; ++i) {
4190 fixed = isl_basic_map_fast_has_fixed_var(map->p[i], pos, &tmp);
4191 if (fixed == 1 && isl_int_ne(tmp, v))
4192 fixed = 0;
4194 if (val)
4195 isl_int_set(*val, v);
4196 isl_int_clear(tmp);
4197 isl_int_clear(v);
4198 return fixed;
4201 static int isl_basic_set_fast_has_fixed_var(struct isl_basic_set *bset,
4202 unsigned pos, isl_int *val)
4204 return isl_basic_map_fast_has_fixed_var((struct isl_basic_map *)bset,
4205 pos, val);
4208 static int isl_set_fast_has_fixed_var(struct isl_set *set, unsigned pos,
4209 isl_int *val)
4211 return isl_map_fast_has_fixed_var((struct isl_map *)set, pos, val);
4214 int isl_basic_map_fast_is_fixed(struct isl_basic_map *bmap,
4215 enum isl_dim_type type, unsigned pos, isl_int *val)
4217 if (pos >= isl_basic_map_dim(bmap, type))
4218 return -1;
4219 return isl_basic_map_fast_has_fixed_var(bmap,
4220 isl_basic_map_offset(bmap, type) - 1 + pos, val);
4223 /* Check if dimension dim has fixed value and if so and if val is not NULL,
4224 * then return this fixed value in *val.
4226 int isl_basic_set_fast_dim_is_fixed(struct isl_basic_set *bset, unsigned dim,
4227 isl_int *val)
4229 return isl_basic_set_fast_has_fixed_var(bset,
4230 isl_basic_set_n_param(bset) + dim, val);
4233 /* Check if dimension dim has fixed value and if so and if val is not NULL,
4234 * then return this fixed value in *val.
4236 int isl_set_fast_dim_is_fixed(struct isl_set *set, unsigned dim, isl_int *val)
4238 return isl_set_fast_has_fixed_var(set, isl_set_n_param(set) + dim, val);
4241 /* Check if input variable in has fixed value and if so and if val is not NULL,
4242 * then return this fixed value in *val.
4244 int isl_map_fast_input_is_fixed(struct isl_map *map, unsigned in, isl_int *val)
4246 return isl_map_fast_has_fixed_var(map, isl_map_n_param(map) + in, val);
4249 /* Check if dimension dim has an (obvious) fixed lower bound and if so
4250 * and if val is not NULL, then return this lower bound in *val.
4252 int isl_basic_set_fast_dim_has_fixed_lower_bound(struct isl_basic_set *bset,
4253 unsigned dim, isl_int *val)
4255 int i, i_eq = -1, i_ineq = -1;
4256 isl_int *c;
4257 unsigned total;
4258 unsigned nparam;
4260 if (!bset)
4261 return -1;
4262 total = isl_basic_set_total_dim(bset);
4263 nparam = isl_basic_set_n_param(bset);
4264 for (i = 0; i < bset->n_eq; ++i) {
4265 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
4266 continue;
4267 if (i_eq != -1)
4268 return 0;
4269 i_eq = i;
4271 for (i = 0; i < bset->n_ineq; ++i) {
4272 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
4273 continue;
4274 if (i_eq != -1 || i_ineq != -1)
4275 return 0;
4276 i_ineq = i;
4278 if (i_eq == -1 && i_ineq == -1)
4279 return 0;
4280 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
4281 /* The coefficient should always be one due to normalization. */
4282 if (!isl_int_is_one(c[1+nparam+dim]))
4283 return 0;
4284 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
4285 return 0;
4286 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
4287 total - nparam - dim - 1) != -1)
4288 return 0;
4289 if (val)
4290 isl_int_neg(*val, c[0]);
4291 return 1;
4294 int isl_set_fast_dim_has_fixed_lower_bound(struct isl_set *set,
4295 unsigned dim, isl_int *val)
4297 int i;
4298 isl_int v;
4299 isl_int tmp;
4300 int fixed;
4302 if (!set)
4303 return -1;
4304 if (set->n == 0)
4305 return 0;
4306 if (set->n == 1)
4307 return isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[0],
4308 dim, val);
4309 isl_int_init(v);
4310 isl_int_init(tmp);
4311 fixed = isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[0],
4312 dim, &v);
4313 for (i = 1; fixed == 1 && i < set->n; ++i) {
4314 fixed = isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[i],
4315 dim, &tmp);
4316 if (fixed == 1 && isl_int_ne(tmp, v))
4317 fixed = 0;
4319 if (val)
4320 isl_int_set(*val, v);
4321 isl_int_clear(tmp);
4322 isl_int_clear(v);
4323 return fixed;
4326 struct constraint {
4327 unsigned size;
4328 isl_int *c;
4331 static int qsort_constraint_cmp(const void *p1, const void *p2)
4333 const struct constraint *c1 = (const struct constraint *)p1;
4334 const struct constraint *c2 = (const struct constraint *)p2;
4335 unsigned size = isl_min(c1->size, c2->size);
4336 return isl_seq_cmp(c1->c, c2->c, size);
4339 static struct isl_basic_map *isl_basic_map_sort_constraints(
4340 struct isl_basic_map *bmap)
4342 int i;
4343 struct constraint *c;
4344 unsigned total;
4346 if (!bmap)
4347 return NULL;
4348 total = isl_basic_map_total_dim(bmap);
4349 c = isl_alloc_array(bmap->ctx, struct constraint, bmap->n_ineq);
4350 if (!c)
4351 goto error;
4352 for (i = 0; i < bmap->n_ineq; ++i) {
4353 c[i].size = total;
4354 c[i].c = bmap->ineq[i];
4356 qsort(c, bmap->n_ineq, sizeof(struct constraint), qsort_constraint_cmp);
4357 for (i = 0; i < bmap->n_ineq; ++i)
4358 bmap->ineq[i] = c[i].c;
4359 free(c);
4360 return bmap;
4361 error:
4362 isl_basic_map_free(bmap);
4363 return NULL;
4366 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
4368 if (!bmap)
4369 return NULL;
4370 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
4371 return bmap;
4372 bmap = isl_basic_map_convex_hull(bmap);
4373 bmap = isl_basic_map_sort_constraints(bmap);
4374 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
4375 return bmap;
4378 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
4380 return (struct isl_basic_set *)isl_basic_map_normalize(
4381 (struct isl_basic_map *)bset);
4384 static int isl_basic_map_fast_cmp(const struct isl_basic_map *bmap1,
4385 const struct isl_basic_map *bmap2)
4387 int i, cmp;
4388 unsigned total;
4390 if (bmap1 == bmap2)
4391 return 0;
4392 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
4393 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
4394 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
4395 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
4396 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
4397 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
4398 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
4399 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
4400 return 0;
4401 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
4402 return 1;
4403 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
4404 return -1;
4405 if (bmap1->n_eq != bmap2->n_eq)
4406 return bmap1->n_eq - bmap2->n_eq;
4407 if (bmap1->n_ineq != bmap2->n_ineq)
4408 return bmap1->n_ineq - bmap2->n_ineq;
4409 if (bmap1->n_div != bmap2->n_div)
4410 return bmap1->n_div - bmap2->n_div;
4411 total = isl_basic_map_total_dim(bmap1);
4412 for (i = 0; i < bmap1->n_eq; ++i) {
4413 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
4414 if (cmp)
4415 return cmp;
4417 for (i = 0; i < bmap1->n_ineq; ++i) {
4418 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
4419 if (cmp)
4420 return cmp;
4422 for (i = 0; i < bmap1->n_div; ++i) {
4423 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
4424 if (cmp)
4425 return cmp;
4427 return 0;
4430 static int isl_basic_map_fast_is_equal(struct isl_basic_map *bmap1,
4431 struct isl_basic_map *bmap2)
4433 return isl_basic_map_fast_cmp(bmap1, bmap2) == 0;
4436 static int qsort_bmap_cmp(const void *p1, const void *p2)
4438 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
4439 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
4441 return isl_basic_map_fast_cmp(bmap1, bmap2);
4444 /* We normalize in place, but if anything goes wrong we need
4445 * to return NULL, so we need to make sure we don't change the
4446 * meaning of any possible other copies of map.
4448 struct isl_map *isl_map_normalize(struct isl_map *map)
4450 int i, j;
4451 struct isl_basic_map *bmap;
4453 if (!map)
4454 return NULL;
4455 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
4456 return map;
4457 for (i = 0; i < map->n; ++i) {
4458 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
4459 if (!bmap)
4460 goto error;
4461 isl_basic_map_free(map->p[i]);
4462 map->p[i] = bmap;
4464 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
4465 ISL_F_SET(map, ISL_MAP_NORMALIZED);
4466 map = isl_map_remove_empty_parts(map);
4467 if (!map)
4468 return NULL;
4469 for (i = map->n - 1; i >= 1; --i) {
4470 if (!isl_basic_map_fast_is_equal(map->p[i-1], map->p[i]))
4471 continue;
4472 isl_basic_map_free(map->p[i-1]);
4473 for (j = i; j < map->n; ++j)
4474 map->p[j-1] = map->p[j];
4475 map->n--;
4477 return map;
4478 error:
4479 isl_map_free(map);
4480 return NULL;
4484 struct isl_set *isl_set_normalize(struct isl_set *set)
4486 return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
4489 int isl_map_fast_is_equal(struct isl_map *map1, struct isl_map *map2)
4491 int i;
4492 int equal;
4494 if (!map1 || !map2)
4495 return -1;
4497 if (map1 == map2)
4498 return 1;
4499 if (!isl_dim_equal(map1->dim, map2->dim))
4500 return 0;
4502 map1 = isl_map_copy(map1);
4503 map2 = isl_map_copy(map2);
4504 map1 = isl_map_normalize(map1);
4505 map2 = isl_map_normalize(map2);
4506 if (!map1 || !map2)
4507 goto error;
4508 equal = map1->n == map2->n;
4509 for (i = 0; equal && i < map1->n; ++i) {
4510 equal = isl_basic_map_fast_is_equal(map1->p[i], map2->p[i]);
4511 if (equal < 0)
4512 goto error;
4514 isl_map_free(map1);
4515 isl_map_free(map2);
4516 return equal;
4517 error:
4518 isl_map_free(map1);
4519 isl_map_free(map2);
4520 return -1;
4523 int isl_set_fast_is_equal(struct isl_set *set1, struct isl_set *set2)
4525 return isl_map_fast_is_equal((struct isl_map *)set1,
4526 (struct isl_map *)set2);
4529 /* Return an interval that ranges from min to max (inclusive)
4531 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
4532 isl_int min, isl_int max)
4534 int k;
4535 struct isl_basic_set *bset = NULL;
4537 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
4538 if (!bset)
4539 goto error;
4541 k = isl_basic_set_alloc_inequality(bset);
4542 if (k < 0)
4543 goto error;
4544 isl_int_set_si(bset->ineq[k][1], 1);
4545 isl_int_neg(bset->ineq[k][0], min);
4547 k = isl_basic_set_alloc_inequality(bset);
4548 if (k < 0)
4549 goto error;
4550 isl_int_set_si(bset->ineq[k][1], -1);
4551 isl_int_set(bset->ineq[k][0], max);
4553 return bset;
4554 error:
4555 isl_basic_set_free(bset);
4556 return NULL;
4559 /* Return the Cartesian product of the basic sets in list (in the given order).
4561 struct isl_basic_set *isl_basic_set_product(struct isl_basic_set_list *list)
4563 int i;
4564 unsigned dim;
4565 unsigned nparam;
4566 unsigned extra;
4567 unsigned n_eq;
4568 unsigned n_ineq;
4569 struct isl_basic_set *product = NULL;
4571 if (!list)
4572 goto error;
4573 isl_assert(list->ctx, list->n > 0, goto error);
4574 isl_assert(list->ctx, list->p[0], goto error);
4575 nparam = isl_basic_set_n_param(list->p[0]);
4576 dim = isl_basic_set_n_dim(list->p[0]);
4577 extra = list->p[0]->n_div;
4578 n_eq = list->p[0]->n_eq;
4579 n_ineq = list->p[0]->n_ineq;
4580 for (i = 1; i < list->n; ++i) {
4581 isl_assert(list->ctx, list->p[i], goto error);
4582 isl_assert(list->ctx,
4583 nparam == isl_basic_set_n_param(list->p[i]), goto error);
4584 dim += isl_basic_set_n_dim(list->p[i]);
4585 extra += list->p[i]->n_div;
4586 n_eq += list->p[i]->n_eq;
4587 n_ineq += list->p[i]->n_ineq;
4589 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
4590 n_eq, n_ineq);
4591 if (!product)
4592 goto error;
4593 dim = 0;
4594 for (i = 0; i < list->n; ++i) {
4595 isl_basic_set_add_constraints(product,
4596 isl_basic_set_copy(list->p[i]), dim);
4597 dim += isl_basic_set_n_dim(list->p[i]);
4599 isl_basic_set_list_free(list);
4600 return product;
4601 error:
4602 isl_basic_set_free(product);
4603 isl_basic_set_list_free(list);
4604 return NULL;
4607 struct isl_basic_map *isl_basic_map_product(
4608 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4610 struct isl_dim *dim_result = NULL;
4611 struct isl_basic_map *bmap;
4612 unsigned in1, in2, out1, out2, nparam, total, pos;
4613 struct isl_dim_map *dim_map1, *dim_map2;
4615 if (!bmap1 || !bmap2)
4616 goto error;
4618 isl_assert(map1->ctx, isl_dim_match(bmap1->dim, isl_dim_param,
4619 bmap2->dim, isl_dim_param), goto error);
4620 dim_result = isl_dim_product(isl_dim_copy(bmap1->dim),
4621 isl_dim_copy(bmap2->dim));
4623 in1 = isl_basic_map_n_in(bmap1);
4624 in2 = isl_basic_map_n_in(bmap2);
4625 out1 = isl_basic_map_n_out(bmap1);
4626 out2 = isl_basic_map_n_out(bmap2);
4627 nparam = isl_basic_map_n_param(bmap1);
4629 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
4630 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4631 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
4632 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4633 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
4634 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4635 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
4636 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
4637 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
4638 isl_dim_map_div(dim_map1, bmap1, pos += out2);
4639 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4641 bmap = isl_basic_map_alloc_dim(dim_result,
4642 bmap1->n_div + bmap2->n_div,
4643 bmap1->n_eq + bmap2->n_eq,
4644 bmap1->n_ineq + bmap2->n_ineq);
4645 bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
4646 bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
4647 bmap = isl_basic_map_simplify(bmap);
4648 return isl_basic_map_finalize(bmap);
4649 error:
4650 isl_basic_map_free(bmap1);
4651 isl_basic_map_free(bmap2);
4652 return NULL;
4655 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
4657 struct isl_map *isl_map_product(struct isl_map *map1, struct isl_map *map2)
4659 unsigned flags = 0;
4660 struct isl_map *result;
4661 int i, j;
4663 if (!map1 || !map2)
4664 goto error;
4666 isl_assert(map1->ctx, isl_dim_match(map1->dim, isl_dim_param,
4667 map2->dim, isl_dim_param), goto error);
4669 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
4670 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
4671 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
4673 result = isl_map_alloc_dim(isl_dim_product(isl_dim_copy(map1->dim),
4674 isl_dim_copy(map2->dim)),
4675 map1->n * map2->n, flags);
4676 if (!result)
4677 goto error;
4678 for (i = 0; i < map1->n; ++i)
4679 for (j = 0; j < map2->n; ++j) {
4680 struct isl_basic_map *part;
4681 part = isl_basic_map_product(
4682 isl_basic_map_copy(map1->p[i]),
4683 isl_basic_map_copy(map2->p[j]));
4684 if (isl_basic_map_is_empty(part))
4685 isl_basic_map_free(part);
4686 else
4687 result = isl_map_add(result, part);
4688 if (!result)
4689 goto error;
4691 isl_map_free(map1);
4692 isl_map_free(map2);
4693 return result;
4694 error:
4695 isl_map_free(map1);
4696 isl_map_free(map2);
4697 return NULL;
4700 /* Given two set A and B, construct its Cartesian product A x B.
4702 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
4704 return (struct isl_set *)isl_map_product((struct isl_map *)set1,
4705 (struct isl_map *)set2);
4708 uint32_t isl_basic_set_get_hash(struct isl_basic_set *bset)
4710 int i;
4711 uint32_t hash;
4712 unsigned total;
4714 if (!bset)
4715 return 0;
4716 bset = isl_basic_set_copy(bset);
4717 bset = isl_basic_set_normalize(bset);
4718 if (!bset)
4719 return 0;
4720 total = isl_basic_set_total_dim(bset);
4721 isl_hash_byte(hash, bset->n_eq & 0xFF);
4722 for (i = 0; i < bset->n_eq; ++i) {
4723 uint32_t c_hash;
4724 c_hash = isl_seq_get_hash(bset->eq[i], 1 + total);
4725 isl_hash_hash(hash, c_hash);
4727 isl_hash_byte(hash, bset->n_ineq & 0xFF);
4728 for (i = 0; i < bset->n_ineq; ++i) {
4729 uint32_t c_hash;
4730 c_hash = isl_seq_get_hash(bset->ineq[i], 1 + total);
4731 isl_hash_hash(hash, c_hash);
4733 isl_hash_byte(hash, bset->n_div & 0xFF);
4734 for (i = 0; i < bset->n_div; ++i) {
4735 uint32_t c_hash;
4736 if (isl_int_is_zero(bset->div[i][0]))
4737 continue;
4738 isl_hash_byte(hash, i & 0xFF);
4739 c_hash = isl_seq_get_hash(bset->div[i], 1 + 1 + total);
4740 isl_hash_hash(hash, c_hash);
4742 isl_basic_set_free(bset);
4743 return hash;
4746 uint32_t isl_set_get_hash(struct isl_set *set)
4748 int i;
4749 uint32_t hash;
4751 if (!set)
4752 return 0;
4753 set = isl_set_copy(set);
4754 set = isl_set_normalize(set);
4755 if (!set)
4756 return 0;
4758 hash = isl_hash_init();
4759 for (i = 0; i < set->n; ++i) {
4760 uint32_t bset_hash;
4761 bset_hash = isl_basic_set_get_hash(set->p[i]);
4762 isl_hash_hash(hash, bset_hash);
4765 isl_set_free(set);
4767 return hash;
4770 /* Check if the value for dimension dim is completely determined
4771 * by the values of the other parameters and variables.
4772 * That is, check if dimension dim is involved in an equality.
4774 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
4776 int i;
4777 unsigned nparam;
4779 if (!bset)
4780 return -1;
4781 nparam = isl_basic_set_n_param(bset);
4782 for (i = 0; i < bset->n_eq; ++i)
4783 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
4784 return 1;
4785 return 0;
4788 /* Check if the value for dimension dim is completely determined
4789 * by the values of the other parameters and variables.
4790 * That is, check if dimension dim is involved in an equality
4791 * for each of the subsets.
4793 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
4795 int i;
4797 if (!set)
4798 return -1;
4799 for (i = 0; i < set->n; ++i) {
4800 int unique;
4801 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
4802 if (unique != 1)
4803 return unique;
4805 return 1;