convex_hull: coalesce set first
[isl.git] / isl_map.c
blob82f1dff8e28da9a5eae3f5fa918bfe670f8fcad4
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;
46 static unsigned pos(struct isl_dim *dim, enum isl_dim_type type)
48 switch (type) {
49 case isl_dim_param: return 1;
50 case isl_dim_in: return 1 + dim->nparam;
51 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
55 static void isl_dim_map_dim(struct isl_dim_map *dim_map, struct isl_dim *dim,
56 enum isl_dim_type type, unsigned dst_pos)
58 int i;
59 unsigned src_pos;
61 if (!dim_map || !dim)
62 return;
64 src_pos = pos(dim, type);
65 for (i = 0; i < n(dim, type); ++i)
66 dim_map->pos[1 + dst_pos + i] = src_pos + i;
69 static void isl_dim_map_div(struct isl_dim_map *dim_map,
70 struct isl_basic_map *bmap, unsigned dst_pos)
72 int i;
73 unsigned src_pos;
75 if (!dim_map || !bmap)
76 return;
78 src_pos = 1 + isl_dim_total(bmap->dim);
79 for (i = 0; i < bmap->n_div; ++i)
80 dim_map->pos[1 + dst_pos + i] = src_pos + i;
83 static void isl_dim_map_dump(struct isl_dim_map *dim_map)
85 int i;
87 for (i = 0; i < dim_map->len; ++i)
88 fprintf(stderr, "%d -> %d; ", i, dim_map->pos[i]);
89 fprintf(stderr, "\n");
92 unsigned isl_basic_map_dim(const struct isl_basic_map *bmap,
93 enum isl_dim_type type)
95 struct isl_dim *dim = bmap->dim;
96 switch (type) {
97 case isl_dim_param:
98 case isl_dim_in:
99 case isl_dim_out: return isl_dim_size(bmap->dim, type);
100 case isl_dim_div: return bmap->n_div;
101 case isl_dim_all: return isl_basic_map_total_dim(bmap);
105 unsigned isl_map_dim(const struct isl_map *map, enum isl_dim_type type)
107 return n(map->dim, type);
110 unsigned isl_set_dim(const struct isl_set *set, enum isl_dim_type type)
112 return n(set->dim, type);
115 unsigned isl_basic_map_offset(struct isl_basic_map *bmap,
116 enum isl_dim_type type)
118 struct isl_dim *dim = bmap->dim;
119 switch (type) {
120 case isl_dim_param: return 1;
121 case isl_dim_in: return 1 + dim->nparam;
122 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
123 case isl_dim_div: return 1 + dim->nparam + dim->n_in + dim->n_out;
127 static unsigned map_offset(struct isl_map *map, enum isl_dim_type type)
129 return pos(map->dim, type);
132 unsigned isl_basic_set_dim(const struct isl_basic_set *bset,
133 enum isl_dim_type type)
135 return isl_basic_map_dim((const struct isl_basic_map*)bset, type);
138 unsigned isl_basic_set_n_dim(const struct isl_basic_set *bset)
140 return bset->dim->n_out;
143 unsigned isl_basic_set_n_param(const struct isl_basic_set *bset)
145 return bset->dim->nparam;
148 unsigned isl_basic_set_total_dim(const struct isl_basic_set *bset)
150 return isl_dim_total(bset->dim) + bset->n_div;
153 unsigned isl_set_n_dim(const struct isl_set *set)
155 return set->dim->n_out;
158 unsigned isl_set_n_param(const struct isl_set *set)
160 return set->dim->nparam;
163 unsigned isl_basic_map_n_in(const struct isl_basic_map *bmap)
165 return bmap->dim->n_in;
168 unsigned isl_basic_map_n_out(const struct isl_basic_map *bmap)
170 return bmap->dim->n_out;
173 unsigned isl_basic_map_n_param(const struct isl_basic_map *bmap)
175 return bmap->dim->nparam;
178 unsigned isl_basic_map_n_div(const struct isl_basic_map *bmap)
180 return bmap->n_div;
183 unsigned isl_basic_map_total_dim(const struct isl_basic_map *bmap)
185 return isl_dim_total(bmap->dim) + bmap->n_div;
188 unsigned isl_map_n_in(const struct isl_map *map)
190 return map->dim->n_in;
193 unsigned isl_map_n_out(const struct isl_map *map)
195 return map->dim->n_out;
198 unsigned isl_map_n_param(const struct isl_map *map)
200 return map->dim->nparam;
203 int isl_map_compatible_domain(struct isl_map *map, struct isl_set *set)
205 return map->dim->n_in == set->dim->n_out &&
206 map->dim->nparam == set->dim->nparam;
209 int isl_basic_map_compatible_domain(struct isl_basic_map *bmap,
210 struct isl_basic_set *bset)
212 return bmap->dim->n_in == bset->dim->n_out &&
213 bmap->dim->nparam == bset->dim->nparam;
216 int isl_basic_map_compatible_range(struct isl_basic_map *bmap,
217 struct isl_basic_set *bset)
219 return bmap->dim->n_out == bset->dim->n_out &&
220 bmap->dim->nparam == bset->dim->nparam;
223 static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
224 struct isl_basic_map *bmap, unsigned extra,
225 unsigned n_eq, unsigned n_ineq)
227 int i;
228 size_t row_size = 1 + isl_dim_total(bmap->dim) + extra;
230 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
231 if (isl_blk_is_error(bmap->block)) {
232 free(bmap);
233 return NULL;
236 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
237 if (!bmap->ineq) {
238 isl_blk_free(ctx, bmap->block);
239 free(bmap);
240 return NULL;
243 if (extra == 0) {
244 bmap->block2 = isl_blk_empty();
245 bmap->div = NULL;
246 } else {
247 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
248 if (isl_blk_is_error(bmap->block2)) {
249 free(bmap->ineq);
250 isl_blk_free(ctx, bmap->block);
251 free(bmap);
252 return NULL;
255 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
256 if (!bmap->div) {
257 isl_blk_free(ctx, bmap->block2);
258 free(bmap->ineq);
259 isl_blk_free(ctx, bmap->block);
260 free(bmap);
261 return NULL;
265 for (i = 0; i < n_ineq + n_eq; ++i)
266 bmap->ineq[i] = bmap->block.data + i * row_size;
268 for (i = 0; i < extra; ++i)
269 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
271 bmap->ctx = ctx;
272 isl_ctx_ref(ctx);
273 bmap->ref = 1;
274 bmap->flags = 0;
275 bmap->c_size = n_eq + n_ineq;
276 bmap->eq = bmap->ineq + n_ineq;
277 bmap->extra = extra;
278 bmap->n_eq = 0;
279 bmap->n_ineq = 0;
280 bmap->n_div = 0;
281 bmap->sample = NULL;
283 return bmap;
284 error:
285 isl_basic_map_free(bmap);
286 return NULL;
289 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
290 unsigned nparam, unsigned dim, unsigned extra,
291 unsigned n_eq, unsigned n_ineq)
293 struct isl_basic_map *bmap;
294 bmap = isl_basic_map_alloc(ctx, nparam, 0, dim, extra, n_eq, n_ineq);
295 return (struct isl_basic_set *)bmap;
298 struct isl_basic_set *isl_basic_set_alloc_dim(struct isl_dim *dim,
299 unsigned extra, unsigned n_eq, unsigned n_ineq)
301 struct isl_basic_map *bmap;
302 if (!dim)
303 return NULL;
304 isl_assert(dim->ctx, dim->n_in == 0, return NULL);
305 bmap = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
306 return (struct isl_basic_set *)bmap;
309 struct isl_basic_map *isl_basic_map_alloc_dim(struct isl_dim *dim,
310 unsigned extra, unsigned n_eq, unsigned n_ineq)
312 struct isl_basic_map *bmap;
314 if (!dim)
315 return NULL;
316 bmap = isl_alloc_type(dim->ctx, struct isl_basic_map);
317 if (!bmap)
318 goto error;
319 bmap->dim = dim;
321 return basic_map_init(dim->ctx, bmap, extra, n_eq, n_ineq);
322 error:
323 isl_dim_free(dim);
324 return NULL;
327 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
328 unsigned nparam, unsigned in, unsigned out, unsigned extra,
329 unsigned n_eq, unsigned n_ineq)
331 struct isl_basic_map *bmap;
332 struct isl_dim *dim;
334 dim = isl_dim_alloc(ctx, nparam, in, out);
335 if (!dim)
336 return NULL;
338 bmap = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
339 return bmap;
342 static void dup_constraints(
343 struct isl_basic_map *dst, struct isl_basic_map *src)
345 int i;
346 unsigned total = isl_basic_map_total_dim(src);
348 for (i = 0; i < src->n_eq; ++i) {
349 int j = isl_basic_map_alloc_equality(dst);
350 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
353 for (i = 0; i < src->n_ineq; ++i) {
354 int j = isl_basic_map_alloc_inequality(dst);
355 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
358 for (i = 0; i < src->n_div; ++i) {
359 int j = isl_basic_map_alloc_div(dst);
360 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
362 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
365 struct isl_basic_map *isl_basic_map_dup(struct isl_basic_map *bmap)
367 struct isl_basic_map *dup;
369 if (!bmap)
370 return NULL;
371 dup = isl_basic_map_alloc_dim(isl_dim_copy(bmap->dim),
372 bmap->n_div, bmap->n_eq, bmap->n_ineq);
373 if (!dup)
374 return NULL;
375 dup_constraints(dup, bmap);
376 dup->flags = bmap->flags;
377 dup->sample = isl_vec_copy(bmap->ctx, bmap->sample);
378 return dup;
381 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
383 struct isl_basic_map *dup;
385 dup = isl_basic_map_dup((struct isl_basic_map *)bset);
386 return (struct isl_basic_set *)dup;
389 struct isl_basic_set *isl_basic_set_copy(struct isl_basic_set *bset)
391 if (!bset)
392 return NULL;
394 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
395 bset->ref++;
396 return bset;
398 return isl_basic_set_dup(bset);
401 struct isl_set *isl_set_copy(struct isl_set *set)
403 if (!set)
404 return NULL;
406 set->ref++;
407 return set;
410 struct isl_basic_map *isl_basic_map_copy(struct isl_basic_map *bmap)
412 if (!bmap)
413 return NULL;
415 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
416 bmap->ref++;
417 return bmap;
419 return isl_basic_map_dup(bmap);
422 struct isl_map *isl_map_copy(struct isl_map *map)
424 if (!map)
425 return NULL;
427 map->ref++;
428 return map;
431 void isl_basic_map_free(struct isl_basic_map *bmap)
433 if (!bmap)
434 return;
436 if (--bmap->ref > 0)
437 return;
439 isl_ctx_deref(bmap->ctx);
440 free(bmap->div);
441 isl_blk_free(bmap->ctx, bmap->block2);
442 free(bmap->ineq);
443 isl_blk_free(bmap->ctx, bmap->block);
444 isl_vec_free(bmap->ctx, bmap->sample);
445 isl_dim_free(bmap->dim);
446 free(bmap);
449 void isl_basic_set_free(struct isl_basic_set *bset)
451 isl_basic_map_free((struct isl_basic_map *)bset);
454 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
456 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
459 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
461 struct isl_ctx *ctx;
462 if (!bmap)
463 return -1;
464 ctx = bmap->ctx;
465 isl_assert(ctx, room_for_con(bmap, 1), return -1);
466 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
467 return -1);
468 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
469 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
470 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
471 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
472 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
473 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
474 isl_int *t;
475 int j = isl_basic_map_alloc_inequality(bmap);
476 if (j < 0)
477 return -1;
478 t = bmap->ineq[j];
479 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
480 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
481 bmap->eq[-1] = t;
482 bmap->n_eq++;
483 bmap->n_ineq--;
484 bmap->eq--;
485 return 0;
487 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
488 bmap->extra - bmap->n_div);
489 return bmap->n_eq++;
492 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
494 return isl_basic_map_alloc_equality((struct isl_basic_map *)bset);
497 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
499 if (!bmap)
500 return -1;
501 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
502 bmap->n_eq -= n;
503 return 0;
506 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
508 isl_int *t;
509 if (!bmap)
510 return -1;
511 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
513 if (pos != bmap->n_eq - 1) {
514 t = bmap->eq[pos];
515 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
516 bmap->eq[bmap->n_eq - 1] = t;
518 bmap->n_eq--;
519 return 0;
522 int isl_basic_set_drop_equality(struct isl_basic_set *bset, unsigned pos)
524 return isl_basic_map_drop_equality((struct isl_basic_map *)bset, pos);
527 void isl_basic_map_inequality_to_equality(
528 struct isl_basic_map *bmap, unsigned pos)
530 isl_int *t;
532 t = bmap->ineq[pos];
533 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
534 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
535 bmap->eq[-1] = t;
536 bmap->n_eq++;
537 bmap->n_ineq--;
538 bmap->eq--;
539 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
540 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
541 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
542 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
545 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
547 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
550 int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap)
552 struct isl_ctx *ctx;
553 if (!bmap)
554 return -1;
555 ctx = bmap->ctx;
556 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
557 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
558 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
559 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
560 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
561 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
562 1 + isl_basic_map_total_dim(bmap),
563 bmap->extra - bmap->n_div);
564 return bmap->n_ineq++;
567 int isl_basic_set_alloc_inequality(struct isl_basic_set *bset)
569 return isl_basic_map_alloc_inequality((struct isl_basic_map *)bset);
572 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
574 if (!bmap)
575 return -1;
576 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
577 bmap->n_ineq -= n;
578 return 0;
581 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
583 return isl_basic_map_free_inequality((struct isl_basic_map *)bset, n);
586 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
588 isl_int *t;
589 if (!bmap)
590 return -1;
591 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
593 if (pos != bmap->n_ineq - 1) {
594 t = bmap->ineq[pos];
595 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
596 bmap->ineq[bmap->n_ineq - 1] = t;
597 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
599 bmap->n_ineq--;
600 return 0;
603 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
605 return isl_basic_map_drop_inequality((struct isl_basic_map *)bset, pos);
608 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
610 if (!bmap)
611 return -1;
612 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
613 isl_seq_clr(bmap->div[bmap->n_div] +
614 1 + 1 + isl_basic_map_total_dim(bmap),
615 bmap->extra - bmap->n_div);
616 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
617 return bmap->n_div++;
620 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
622 return isl_basic_map_alloc_div((struct isl_basic_map *)bset);
625 int isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
627 if (!bmap)
628 return -1;
629 isl_assert(bmap->ctx, n <= bmap->n_div, return -1);
630 bmap->n_div -= n;
631 return 0;
634 /* Copy constraint from src to dst, putting the vars of src at offset
635 * dim_off in dst and the divs of src at offset div_off in dst.
636 * If both sets are actually map, then dim_off applies to the input
637 * variables.
639 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
640 struct isl_basic_map *src_map, isl_int *src,
641 unsigned in_off, unsigned out_off, unsigned div_off)
643 unsigned src_nparam = isl_basic_map_n_param(src_map);
644 unsigned dst_nparam = isl_basic_map_n_param(dst_map);
645 unsigned src_in = isl_basic_map_n_in(src_map);
646 unsigned dst_in = isl_basic_map_n_in(dst_map);
647 unsigned src_out = isl_basic_map_n_out(src_map);
648 unsigned dst_out = isl_basic_map_n_out(dst_map);
649 isl_int_set(dst[0], src[0]);
650 isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
651 if (dst_nparam > src_nparam)
652 isl_seq_clr(dst+1+src_nparam,
653 dst_nparam - src_nparam);
654 isl_seq_clr(dst+1+dst_nparam, in_off);
655 isl_seq_cpy(dst+1+dst_nparam+in_off,
656 src+1+src_nparam,
657 isl_min(dst_in-in_off, src_in));
658 if (dst_in-in_off > src_in)
659 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
660 dst_in - in_off - src_in);
661 isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
662 isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
663 src+1+src_nparam+src_in,
664 isl_min(dst_out-out_off, src_out));
665 if (dst_out-out_off > src_out)
666 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
667 dst_out - out_off - src_out);
668 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
669 isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
670 src+1+src_nparam+src_in+src_out,
671 isl_min(dst_map->extra-div_off, src_map->n_div));
672 if (dst_map->n_div-div_off > src_map->n_div)
673 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
674 div_off+src_map->n_div,
675 dst_map->n_div - div_off - src_map->n_div);
678 static void copy_div(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 isl_int_set(dst[0], src[0]);
683 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
686 static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
687 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
689 int i;
690 unsigned div_off;
692 if (!bmap1 || !bmap2)
693 goto error;
695 div_off = bmap1->n_div;
697 for (i = 0; i < bmap2->n_eq; ++i) {
698 int i1 = isl_basic_map_alloc_equality(bmap1);
699 if (i1 < 0)
700 goto error;
701 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
702 i_pos, o_pos, div_off);
705 for (i = 0; i < bmap2->n_ineq; ++i) {
706 int i1 = isl_basic_map_alloc_inequality(bmap1);
707 if (i1 < 0)
708 goto error;
709 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
710 i_pos, o_pos, div_off);
713 for (i = 0; i < bmap2->n_div; ++i) {
714 int i1 = isl_basic_map_alloc_div(bmap1);
715 if (i1 < 0)
716 goto error;
717 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
718 i_pos, o_pos, div_off);
721 isl_basic_map_free(bmap2);
723 return bmap1;
725 error:
726 isl_basic_map_free(bmap1);
727 isl_basic_map_free(bmap2);
728 return NULL;
731 static void copy_constraint_dim_map(isl_int *dst, isl_int *src,
732 struct isl_dim_map *dim_map)
734 int i;
736 for (i = 0; i < dim_map->len; ++i) {
737 if (dim_map->pos[i] < 0)
738 isl_int_set_si(dst[i], 0);
739 else
740 isl_int_set(dst[i], src[dim_map->pos[i]]);
744 static void copy_div_dim_map(isl_int *dst, isl_int *src,
745 struct isl_dim_map *dim_map)
747 isl_int_set(dst[0], src[0]);
748 copy_constraint_dim_map(dst+1, src+1, dim_map);
751 static struct isl_basic_map *add_constraints_dim_map(struct isl_basic_map *dst,
752 struct isl_basic_map *src, struct isl_dim_map *dim_map)
754 int i;
756 if (!src || !dst || !dim_map)
757 goto error;
759 for (i = 0; i < src->n_eq; ++i) {
760 int i1 = isl_basic_map_alloc_equality(dst);
761 if (i1 < 0)
762 goto error;
763 copy_constraint_dim_map(dst->eq[i1], src->eq[i], dim_map);
766 for (i = 0; i < src->n_ineq; ++i) {
767 int i1 = isl_basic_map_alloc_inequality(dst);
768 if (i1 < 0)
769 goto error;
770 copy_constraint_dim_map(dst->ineq[i1], src->ineq[i], dim_map);
773 for (i = 0; i < src->n_div; ++i) {
774 int i1 = isl_basic_map_alloc_div(dst);
775 if (i1 < 0)
776 goto error;
777 copy_div_dim_map(dst->div[i1], src->div[i], dim_map);
780 free(dim_map);
781 isl_basic_map_free(src);
783 return dst;
784 error:
785 free(dim_map);
786 isl_basic_map_free(src);
787 isl_basic_map_free(dst);
788 return NULL;
791 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
792 struct isl_basic_set *bset2, unsigned pos)
794 return (struct isl_basic_set *)
795 add_constraints((struct isl_basic_map *)bset1,
796 (struct isl_basic_map *)bset2, 0, pos);
799 struct isl_basic_map *isl_basic_map_extend_dim(struct isl_basic_map *base,
800 struct isl_dim *dim, unsigned extra,
801 unsigned n_eq, unsigned n_ineq)
803 struct isl_basic_map *ext;
804 unsigned flags;
805 int dims_ok;
807 if (!dim)
808 goto error;
810 if (!base)
811 goto error;
813 dims_ok = isl_dim_equal(base->dim, dim) &&
814 base->extra >= base->n_div + extra;
816 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
817 room_for_ineq(base, n_ineq)) {
818 isl_dim_free(dim);
819 return base;
822 isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error);
823 isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error);
824 isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error);
825 extra += base->extra;
826 n_eq += base->n_eq;
827 n_ineq += base->n_ineq;
829 ext = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
830 dim = NULL;
831 if (!ext)
832 goto error;
834 flags = base->flags;
835 ext = add_constraints(ext, base, 0, 0);
836 if (ext) {
837 ext->flags = flags;
838 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
841 return ext;
843 error:
844 isl_dim_free(dim);
845 isl_basic_map_free(base);
846 return NULL;
849 struct isl_basic_set *isl_basic_set_extend_dim(struct isl_basic_set *base,
850 struct isl_dim *dim, unsigned extra,
851 unsigned n_eq, unsigned n_ineq)
853 return (struct isl_basic_set *)
854 isl_basic_map_extend_dim((struct isl_basic_map *)base, dim,
855 extra, n_eq, n_ineq);
858 struct isl_basic_map *isl_basic_map_extend_constraints(
859 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
861 if (!base)
862 return NULL;
863 return isl_basic_map_extend_dim(base, isl_dim_copy(base->dim),
864 0, n_eq, n_ineq);
867 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
868 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
869 unsigned n_eq, unsigned n_ineq)
871 struct isl_basic_map *bmap;
872 struct isl_dim *dim;
874 if (!base)
875 return NULL;
876 dim = isl_dim_alloc(base->ctx, nparam, n_in, n_out);
877 if (!dim)
878 return NULL;
880 bmap = isl_basic_map_extend_dim(base, dim, extra, n_eq, n_ineq);
881 return bmap;
884 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
885 unsigned nparam, unsigned dim, unsigned extra,
886 unsigned n_eq, unsigned n_ineq)
888 return (struct isl_basic_set *)
889 isl_basic_map_extend((struct isl_basic_map *)base,
890 nparam, 0, dim, extra, n_eq, n_ineq);
893 struct isl_basic_set *isl_basic_set_extend_constraints(
894 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
896 return (struct isl_basic_set *)
897 isl_basic_map_extend_constraints((struct isl_basic_map *)base,
898 n_eq, n_ineq);
901 struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset)
903 return (struct isl_basic_set *)
904 isl_basic_map_cow((struct isl_basic_map *)bset);
907 struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
909 if (!bmap)
910 return NULL;
912 if (bmap->ref > 1) {
913 bmap->ref--;
914 bmap = isl_basic_map_dup(bmap);
916 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
917 return bmap;
920 struct isl_set *isl_set_cow(struct isl_set *set)
922 if (!set)
923 return NULL;
925 if (set->ref == 1)
926 return set;
927 set->ref--;
928 return isl_set_dup(set);
931 struct isl_map *isl_map_cow(struct isl_map *map)
933 if (!map)
934 return NULL;
936 if (map->ref == 1)
937 return map;
938 map->ref--;
939 return isl_map_dup(map);
942 static void swap_vars(struct isl_blk blk, isl_int *a,
943 unsigned a_len, unsigned b_len)
945 isl_seq_cpy(blk.data, a+a_len, b_len);
946 isl_seq_cpy(blk.data+b_len, a, a_len);
947 isl_seq_cpy(a, blk.data, b_len+a_len);
950 struct isl_basic_set *isl_basic_set_swap_vars(
951 struct isl_basic_set *bset, unsigned n)
953 int i;
954 struct isl_blk blk;
955 unsigned dim;
956 unsigned nparam;
958 if (!bset)
959 goto error;
961 nparam = isl_basic_set_n_param(bset);
962 dim = isl_basic_set_n_dim(bset);
963 isl_assert(bset->ctx, n <= dim, goto error);
965 if (n == dim)
966 return bset;
968 bset = isl_basic_set_cow(bset);
969 if (!bset)
970 return NULL;
972 blk = isl_blk_alloc(bset->ctx, dim);
973 if (isl_blk_is_error(blk))
974 goto error;
976 for (i = 0; i < bset->n_eq; ++i)
977 swap_vars(blk,
978 bset->eq[i]+1+nparam, n, dim - n);
980 for (i = 0; i < bset->n_ineq; ++i)
981 swap_vars(blk,
982 bset->ineq[i]+1+nparam, n, dim - n);
984 for (i = 0; i < bset->n_div; ++i)
985 swap_vars(blk,
986 bset->div[i]+1+1+nparam, n, dim - n);
988 isl_blk_free(bset->ctx, blk);
990 ISL_F_CLR(bset, ISL_BASIC_SET_NORMALIZED);
991 return bset;
993 error:
994 isl_basic_set_free(bset);
995 return NULL;
998 struct isl_set *isl_set_swap_vars(struct isl_set *set, unsigned n)
1000 int i;
1001 set = isl_set_cow(set);
1002 if (!set)
1003 return NULL;
1005 for (i = 0; i < set->n; ++i) {
1006 set->p[i] = isl_basic_set_swap_vars(set->p[i], n);
1007 if (!set->p[i]) {
1008 isl_set_free(set);
1009 return NULL;
1012 ISL_F_CLR(set, ISL_SET_NORMALIZED);
1013 return set;
1016 struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
1018 int i = 0;
1019 unsigned total;
1020 if (!bmap)
1021 goto error;
1022 total = isl_basic_map_total_dim(bmap);
1023 isl_basic_map_free_div(bmap, bmap->n_div);
1024 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1025 if (bmap->n_eq > 0)
1026 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
1027 else {
1028 isl_basic_map_alloc_equality(bmap);
1029 if (i < 0)
1030 goto error;
1032 isl_int_set_si(bmap->eq[i][0], 1);
1033 isl_seq_clr(bmap->eq[i]+1, total);
1034 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
1035 return isl_basic_map_finalize(bmap);
1036 error:
1037 isl_basic_map_free(bmap);
1038 return NULL;
1041 struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
1043 return (struct isl_basic_set *)
1044 isl_basic_map_set_to_empty((struct isl_basic_map *)bset);
1047 static void swap_div(struct isl_basic_map *bmap, int a, int b)
1049 int i;
1050 unsigned off = isl_dim_total(bmap->dim);
1051 isl_int *t = bmap->div[a];
1052 bmap->div[a] = bmap->div[b];
1053 bmap->div[b] = t;
1055 for (i = 0; i < bmap->n_eq; ++i)
1056 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
1058 for (i = 0; i < bmap->n_ineq; ++i)
1059 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
1061 for (i = 0; i < bmap->n_div; ++i)
1062 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
1063 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1066 /* Eliminate the specified n dimensions starting at first from the
1067 * constraints using Fourier-Motzkin, The dimensions themselves
1068 * are not removed.
1070 struct isl_set *isl_set_eliminate_dims(struct isl_set *set,
1071 unsigned first, unsigned n)
1073 int i;
1074 unsigned nparam;
1076 if (!set)
1077 return NULL;
1078 if (n == 0)
1079 return set;
1081 set = isl_set_cow(set);
1082 if (!set)
1083 return NULL;
1084 isl_assert(set->ctx, first+n <= isl_set_n_dim(set), goto error);
1085 nparam = isl_set_n_param(set);
1087 for (i = 0; i < set->n; ++i) {
1088 set->p[i] = isl_basic_set_eliminate_vars(set->p[i],
1089 nparam + first, n);
1090 if (!set->p[i])
1091 goto error;
1093 return set;
1094 error:
1095 isl_set_free(set);
1096 return NULL;
1099 /* Project out n dimensions starting at first using Fourier-Motzkin */
1100 struct isl_set *isl_set_remove_dims(struct isl_set *set,
1101 unsigned first, unsigned n)
1103 set = isl_set_eliminate_dims(set, first, n);
1104 set = isl_set_drop_dims(set, first, n);
1105 return set;
1108 struct isl_basic_set *isl_basic_set_remove_divs(struct isl_basic_set *bset)
1110 bset = isl_basic_set_eliminate_vars(bset, isl_dim_total(bset->dim),
1111 bset->n_div);
1112 if (!bset)
1113 return NULL;
1114 bset->n_div = 0;
1115 return bset;
1118 struct isl_set *isl_set_remove_divs(struct isl_set *set)
1120 int i;
1122 if (!set)
1123 return NULL;
1124 if (set->n == 0)
1125 return set;
1127 set = isl_set_cow(set);
1128 if (!set)
1129 return NULL;
1131 for (i = 0; i < set->n; ++i) {
1132 set->p[i] = isl_basic_set_remove_divs(set->p[i]);
1133 if (!set->p[i])
1134 goto error;
1136 return set;
1137 error:
1138 isl_set_free(set);
1139 return NULL;
1142 struct isl_basic_map *isl_basic_map_remove(struct isl_basic_map *bmap,
1143 enum isl_dim_type type, unsigned first, unsigned n)
1145 if (!bmap)
1146 return NULL;
1147 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1148 goto error);
1149 if (n == 0)
1150 return bmap;
1151 bmap = isl_basic_map_eliminate_vars(bmap,
1152 isl_basic_map_offset(bmap, type) - 1 + first, n);
1153 bmap = isl_basic_map_drop(bmap, type, first, n);
1154 return bmap;
1155 error:
1156 isl_basic_map_free(bmap);
1157 return NULL;
1160 struct isl_map *isl_map_remove(struct isl_map *map,
1161 enum isl_dim_type type, unsigned first, unsigned n)
1163 int i;
1164 unsigned nparam;
1166 if (n == 0)
1167 return map;
1169 map = isl_map_cow(map);
1170 if (!map)
1171 return NULL;
1172 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
1174 for (i = 0; i < map->n; ++i) {
1175 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
1176 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
1177 if (!map->p[i])
1178 goto error;
1180 map = isl_map_drop(map, type, first, n);
1181 return map;
1182 error:
1183 isl_map_free(map);
1184 return NULL;
1187 /* Project out n inputs starting at first using Fourier-Motzkin */
1188 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
1189 unsigned first, unsigned n)
1191 return isl_map_remove(map, isl_dim_in, first, n);
1194 /* Project out n dimensions starting at first using Fourier-Motzkin */
1195 struct isl_basic_set *isl_basic_set_remove_dims(struct isl_basic_set *bset,
1196 unsigned first, unsigned n)
1198 unsigned nparam = isl_basic_set_n_param(bset);
1199 bset = isl_basic_set_eliminate_vars(bset, nparam + first, n);
1200 bset = isl_basic_set_drop_dims(bset, first, n);
1201 return bset;
1204 static void dump_term(struct isl_basic_map *bmap,
1205 isl_int c, int pos, FILE *out)
1207 const char *name;
1208 unsigned in = isl_basic_map_n_in(bmap);
1209 unsigned dim = in + isl_basic_map_n_out(bmap);
1210 unsigned nparam = isl_basic_map_n_param(bmap);
1211 if (!pos)
1212 isl_int_print(out, c, 0);
1213 else {
1214 if (!isl_int_is_one(c))
1215 isl_int_print(out, c, 0);
1216 if (pos < 1 + nparam) {
1217 name = isl_dim_get_name(bmap->dim,
1218 isl_dim_param, pos - 1);
1219 if (name)
1220 fprintf(out, "%s", name);
1221 else
1222 fprintf(out, "p%d", pos - 1);
1223 } else if (pos < 1 + nparam + in)
1224 fprintf(out, "i%d", pos - 1 - nparam);
1225 else if (pos < 1 + nparam + dim)
1226 fprintf(out, "o%d", pos - 1 - nparam - in);
1227 else
1228 fprintf(out, "e%d", pos - 1 - nparam - dim);
1232 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
1233 int sign, FILE *out)
1235 int i;
1236 int first;
1237 unsigned len = 1 + isl_basic_map_total_dim(bmap);
1238 isl_int v;
1240 isl_int_init(v);
1241 for (i = 0, first = 1; i < len; ++i) {
1242 if (isl_int_sgn(c[i]) * sign <= 0)
1243 continue;
1244 if (!first)
1245 fprintf(out, " + ");
1246 first = 0;
1247 isl_int_abs(v, c[i]);
1248 dump_term(bmap, v, i, out);
1250 isl_int_clear(v);
1251 if (first)
1252 fprintf(out, "0");
1255 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
1256 const char *op, FILE *out, int indent)
1258 int i;
1260 fprintf(out, "%*s", indent, "");
1262 dump_constraint_sign(bmap, c, 1, out);
1263 fprintf(out, " %s ", op);
1264 dump_constraint_sign(bmap, c, -1, out);
1266 fprintf(out, "\n");
1268 for (i = bmap->n_div; i < bmap->extra; ++i) {
1269 if (isl_int_is_zero(c[1+isl_dim_total(bmap->dim)+i]))
1270 continue;
1271 fprintf(out, "%*s", indent, "");
1272 fprintf(out, "ERROR: unused div coefficient not zero\n");
1273 abort();
1277 static void dump_constraints(struct isl_basic_map *bmap,
1278 isl_int **c, unsigned n,
1279 const char *op, FILE *out, int indent)
1281 int i;
1283 for (i = 0; i < n; ++i)
1284 dump_constraint(bmap, c[i], op, out, indent);
1287 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
1289 int j;
1290 int first = 1;
1291 unsigned total = isl_basic_map_total_dim(bmap);
1293 for (j = 0; j < 1 + total; ++j) {
1294 if (isl_int_is_zero(exp[j]))
1295 continue;
1296 if (!first && isl_int_is_pos(exp[j]))
1297 fprintf(out, "+");
1298 dump_term(bmap, exp[j], j, out);
1299 first = 0;
1303 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
1305 int i;
1307 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
1308 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
1310 for (i = 0; i < bmap->n_div; ++i) {
1311 fprintf(out, "%*s", indent, "");
1312 fprintf(out, "e%d = [(", i);
1313 dump_affine(bmap, bmap->div[i]+1, out);
1314 fprintf(out, ")/");
1315 isl_int_print(out, bmap->div[i][0], 0);
1316 fprintf(out, "]\n");
1320 void isl_basic_set_dump(struct isl_basic_set *bset, FILE *out, int indent)
1322 if (!bset) {
1323 fprintf(out, "null basic set\n");
1324 return;
1327 fprintf(out, "%*s", indent, "");
1328 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
1329 bset->ref, bset->dim->nparam, bset->dim->n_out,
1330 bset->extra, bset->flags);
1331 dump((struct isl_basic_map *)bset, out, indent);
1334 void isl_basic_map_dump(struct isl_basic_map *bmap, FILE *out, int indent)
1336 if (!bmap) {
1337 fprintf(out, "null basic map\n");
1338 return;
1341 fprintf(out, "%*s", indent, "");
1342 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
1343 "flags: %x, n_name: %d\n",
1344 bmap->ref,
1345 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
1346 bmap->extra, bmap->flags, bmap->dim->n_name);
1347 dump(bmap, out, indent);
1350 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
1352 unsigned total;
1353 if (!bmap)
1354 return -1;
1355 total = isl_basic_map_total_dim(bmap);
1356 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1357 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
1358 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
1359 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1360 return 0;
1363 struct isl_set *isl_set_alloc_dim(struct isl_dim *dim, int n, unsigned flags)
1365 struct isl_set *set;
1367 if (!dim)
1368 return NULL;
1369 isl_assert(dim->ctx, dim->n_in == 0, return NULL);
1370 isl_assert(dim->ctx, n >= 0, return NULL);
1371 set = isl_alloc(dim->ctx, struct isl_set,
1372 sizeof(struct isl_set) +
1373 n * sizeof(struct isl_basic_set *));
1374 if (!set)
1375 goto error;
1377 set->ctx = dim->ctx;
1378 isl_ctx_ref(set->ctx);
1379 set->ref = 1;
1380 set->size = n;
1381 set->n = 0;
1382 set->dim = dim;
1383 set->flags = flags;
1384 return set;
1385 error:
1386 isl_dim_free(dim);
1387 return NULL;
1390 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
1391 unsigned nparam, unsigned dim, int n, unsigned flags)
1393 struct isl_set *set;
1394 struct isl_dim *dims;
1396 dims = isl_dim_alloc(ctx, nparam, 0, dim);
1397 if (!dims)
1398 return NULL;
1400 set = isl_set_alloc_dim(dims, n, flags);
1401 return set;
1404 struct isl_set *isl_set_dup(struct isl_set *set)
1406 int i;
1407 struct isl_set *dup;
1409 if (!set)
1410 return NULL;
1412 dup = isl_set_alloc_dim(isl_dim_copy(set->dim), set->n, set->flags);
1413 if (!dup)
1414 return NULL;
1415 for (i = 0; i < set->n; ++i)
1416 dup = isl_set_add(dup, isl_basic_set_copy(set->p[i]));
1417 return dup;
1420 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
1422 struct isl_set *set;
1424 if (!bset)
1425 return NULL;
1427 set = isl_set_alloc_dim(isl_dim_copy(bset->dim), 1, ISL_MAP_DISJOINT);
1428 if (!set) {
1429 isl_basic_set_free(bset);
1430 return NULL;
1432 return isl_set_add(set, bset);
1435 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
1437 struct isl_map *map;
1439 if (!bmap)
1440 return NULL;
1442 map = isl_map_alloc_dim(isl_dim_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
1443 if (!map) {
1444 isl_basic_map_free(bmap);
1445 return NULL;
1447 return isl_map_add(map, bmap);
1450 struct isl_set *isl_set_add(struct isl_set *set, struct isl_basic_set *bset)
1452 if (!bset || !set)
1453 goto error;
1454 isl_assert(set->ctx, isl_dim_equal(set->dim, bset->dim), goto error);
1455 isl_assert(set->ctx, set->n < set->size, goto error);
1456 set->p[set->n] = bset;
1457 set->n++;
1458 return set;
1459 error:
1460 if (set)
1461 isl_set_free(set);
1462 if (bset)
1463 isl_basic_set_free(bset);
1464 return NULL;
1467 void isl_set_free(struct isl_set *set)
1469 int i;
1471 if (!set)
1472 return;
1474 if (--set->ref > 0)
1475 return;
1477 isl_ctx_deref(set->ctx);
1478 for (i = 0; i < set->n; ++i)
1479 isl_basic_set_free(set->p[i]);
1480 isl_dim_free(set->dim);
1481 free(set);
1484 void isl_set_dump(struct isl_set *set, FILE *out, int indent)
1486 int i;
1488 if (!set) {
1489 fprintf(out, "null set\n");
1490 return;
1493 fprintf(out, "%*s", indent, "");
1494 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
1495 set->ref, set->n, set->dim->nparam, set->dim->n_out,
1496 set->flags);
1497 for (i = 0; i < set->n; ++i) {
1498 fprintf(out, "%*s", indent, "");
1499 fprintf(out, "basic set %d:\n", i);
1500 isl_basic_set_dump(set->p[i], out, indent+4);
1504 void isl_map_dump(struct isl_map *map, FILE *out, int indent)
1506 int i;
1508 if (!map) {
1509 fprintf(out, "null map\n");
1510 return;
1513 fprintf(out, "%*s", indent, "");
1514 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
1515 "flags: %x, n_name: %d\n",
1516 map->ref, map->n, map->dim->nparam, map->dim->n_in,
1517 map->dim->n_out, map->flags, map->dim->n_name);
1518 for (i = 0; i < map->n; ++i) {
1519 fprintf(out, "%*s", indent, "");
1520 fprintf(out, "basic map %d:\n", i);
1521 isl_basic_map_dump(map->p[i], out, indent+4);
1525 struct isl_basic_map *isl_basic_map_intersect_domain(
1526 struct isl_basic_map *bmap, struct isl_basic_set *bset)
1528 struct isl_basic_map *bmap_domain;
1529 struct isl_dim *dim;
1531 if (!bmap || !bset)
1532 goto error;
1534 isl_assert(bset->ctx, isl_dim_match(bmap->dim, isl_dim_param,
1535 bset->dim, isl_dim_param), goto error);
1537 if (isl_dim_size(bset->dim, isl_dim_set) != 0)
1538 isl_assert(bset->ctx,
1539 isl_basic_map_compatible_domain(bmap, bset), goto error);
1541 bmap = isl_basic_map_cow(bmap);
1542 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
1543 bset->n_div, bset->n_eq, bset->n_ineq);
1544 if (!bmap)
1545 goto error;
1546 dim = isl_dim_reverse(isl_dim_copy(bset->dim));
1547 bmap_domain = isl_basic_map_from_basic_set(bset, dim);
1548 bmap = add_constraints(bmap, bmap_domain, 0, 0);
1550 bmap = isl_basic_map_simplify(bmap);
1551 return isl_basic_map_finalize(bmap);
1552 error:
1553 isl_basic_map_free(bmap);
1554 isl_basic_set_free(bset);
1555 return NULL;
1558 struct isl_basic_map *isl_basic_map_intersect_range(
1559 struct isl_basic_map *bmap, struct isl_basic_set *bset)
1561 struct isl_basic_map *bmap_range;
1563 if (!bmap || !bset)
1564 goto error;
1566 isl_assert(bset->ctx, isl_dim_match(bmap->dim, isl_dim_param,
1567 bset->dim, isl_dim_param), goto error);
1569 if (isl_dim_size(bset->dim, isl_dim_set) != 0)
1570 isl_assert(bset->ctx,
1571 isl_basic_map_compatible_range(bmap, bset), goto error);
1573 bmap = isl_basic_map_cow(bmap);
1574 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
1575 bset->n_div, bset->n_eq, bset->n_ineq);
1576 if (!bmap)
1577 goto error;
1578 bmap_range = isl_basic_map_from_basic_set(bset, isl_dim_copy(bset->dim));
1579 bmap = add_constraints(bmap, bmap_range, 0, 0);
1581 bmap = isl_basic_map_simplify(bmap);
1582 return isl_basic_map_finalize(bmap);
1583 error:
1584 isl_basic_map_free(bmap);
1585 isl_basic_set_free(bset);
1586 return NULL;
1589 static int basic_map_contains(struct isl_basic_map *bmap, struct isl_vec *vec)
1591 int i;
1592 unsigned total;
1593 isl_int s;
1595 total = 1 + isl_basic_map_total_dim(bmap);
1596 if (total != vec->size)
1597 return -1;
1599 isl_int_init(s);
1601 for (i = 0; i < bmap->n_eq; ++i) {
1602 isl_seq_inner_product(vec->block.data, bmap->eq[i], total, &s);
1603 if (!isl_int_is_zero(s)) {
1604 isl_int_clear(s);
1605 return 0;
1609 for (i = 0; i < bmap->n_ineq; ++i) {
1610 isl_seq_inner_product(vec->block.data, bmap->ineq[i], total, &s);
1611 if (isl_int_is_neg(s)) {
1612 isl_int_clear(s);
1613 return 0;
1617 isl_int_clear(s);
1619 return 1;
1622 int isl_basic_set_contains(struct isl_basic_set *bset, struct isl_vec *vec)
1624 return basic_map_contains((struct isl_basic_map *)bset, vec);
1627 struct isl_basic_map *isl_basic_map_intersect(
1628 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
1630 struct isl_vec *sample = NULL;
1632 if (!bmap1 || !bmap2)
1633 goto error;
1635 isl_assert(bmap1->ctx, isl_dim_match(bmap1->dim, isl_dim_param,
1636 bmap2->dim, isl_dim_param), goto error);
1637 if (isl_dim_total(bmap1->dim) ==
1638 isl_dim_size(bmap1->dim, isl_dim_param) &&
1639 isl_dim_total(bmap2->dim) !=
1640 isl_dim_size(bmap2->dim, isl_dim_param))
1641 return isl_basic_map_intersect(bmap2, bmap1);
1643 if (isl_dim_total(bmap2->dim) !=
1644 isl_dim_size(bmap2->dim, isl_dim_param))
1645 isl_assert(bmap1->ctx,
1646 isl_dim_equal(bmap1->dim, bmap2->dim), goto error);
1648 if (bmap1->sample &&
1649 basic_map_contains(bmap1, bmap1->sample) > 0 &&
1650 basic_map_contains(bmap2, bmap1->sample) > 0)
1651 sample = isl_vec_copy(bmap1->ctx, bmap1->sample);
1652 else if (bmap2->sample &&
1653 basic_map_contains(bmap1, bmap2->sample) > 0 &&
1654 basic_map_contains(bmap2, bmap2->sample) > 0)
1655 sample = isl_vec_copy(bmap2->ctx, bmap2->sample);
1657 bmap1 = isl_basic_map_cow(bmap1);
1658 bmap1 = isl_basic_map_extend_dim(bmap1, isl_dim_copy(bmap1->dim),
1659 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
1660 if (!bmap1)
1661 goto error;
1662 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
1664 if (sample) {
1665 isl_vec_free(bmap1->ctx, bmap1->sample);
1666 bmap1->sample = sample;
1669 bmap1 = isl_basic_map_simplify(bmap1);
1670 return isl_basic_map_finalize(bmap1);
1671 error:
1672 if (sample)
1673 isl_vec_free(bmap1->ctx, sample);
1674 isl_basic_map_free(bmap1);
1675 isl_basic_map_free(bmap2);
1676 return NULL;
1679 struct isl_basic_set *isl_basic_set_intersect(
1680 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
1682 return (struct isl_basic_set *)
1683 isl_basic_map_intersect(
1684 (struct isl_basic_map *)bset1,
1685 (struct isl_basic_map *)bset2);
1688 struct isl_map *isl_map_intersect(struct isl_map *map1, struct isl_map *map2)
1690 unsigned flags = 0;
1691 struct isl_map *result;
1692 int i, j;
1694 if (!map1 || !map2)
1695 goto error;
1697 isl_assert(map1->ctx, isl_dim_match(map1->dim, isl_dim_param,
1698 map2->dim, isl_dim_param), goto error);
1699 if (isl_dim_total(map1->dim) ==
1700 isl_dim_size(map1->dim, isl_dim_param) &&
1701 isl_dim_total(map2->dim) != isl_dim_size(map2->dim, isl_dim_param))
1702 return isl_map_intersect(map2, map1);
1704 if (isl_dim_total(map2->dim) != isl_dim_size(map2->dim, isl_dim_param))
1705 isl_assert(map1->ctx,
1706 isl_dim_equal(map1->dim, map2->dim), goto error);
1708 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
1709 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
1710 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
1712 result = isl_map_alloc_dim(isl_dim_copy(map1->dim),
1713 map1->n * map2->n, flags);
1714 if (!result)
1715 goto error;
1716 for (i = 0; i < map1->n; ++i)
1717 for (j = 0; j < map2->n; ++j) {
1718 struct isl_basic_map *part;
1719 part = isl_basic_map_intersect(
1720 isl_basic_map_copy(map1->p[i]),
1721 isl_basic_map_copy(map2->p[j]));
1722 if (isl_basic_map_is_empty(part))
1723 isl_basic_map_free(part);
1724 else
1725 result = isl_map_add(result, part);
1726 if (!result)
1727 goto error;
1729 isl_map_free(map1);
1730 isl_map_free(map2);
1731 return result;
1732 error:
1733 isl_map_free(map1);
1734 isl_map_free(map2);
1735 return NULL;
1738 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
1740 return (struct isl_set *)
1741 isl_map_intersect((struct isl_map *)set1,
1742 (struct isl_map *)set2);
1745 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
1747 struct isl_dim *dim;
1748 struct isl_basic_set *bset;
1749 unsigned in;
1751 if (!bmap)
1752 return NULL;
1753 bmap = isl_basic_map_cow(bmap);
1754 if (!bmap)
1755 return NULL;
1756 dim = isl_dim_reverse(isl_dim_copy(bmap->dim));
1757 in = isl_basic_map_n_in(bmap);
1758 bset = isl_basic_set_from_basic_map(bmap);
1759 bset = isl_basic_set_swap_vars(bset, in);
1760 return isl_basic_map_from_basic_set(bset, dim);
1763 /* Turn final n dimensions into existentially quantified variables.
1765 struct isl_basic_set *isl_basic_set_project_out(
1766 struct isl_basic_set *bset, unsigned n, unsigned flags)
1768 int i;
1769 size_t row_size;
1770 isl_int **new_div;
1771 isl_int *old;
1773 if (!bset)
1774 return NULL;
1776 isl_assert(bset->ctx, n <= isl_basic_set_n_dim(bset), goto error);
1778 if (n == 0)
1779 return bset;
1781 bset = isl_basic_set_cow(bset);
1783 row_size = 1 + isl_dim_total(bset->dim) + bset->extra;
1784 old = bset->block2.data;
1785 bset->block2 = isl_blk_extend(bset->ctx, bset->block2,
1786 (bset->extra + n) * (1 + row_size));
1787 if (!bset->block2.data)
1788 goto error;
1789 new_div = isl_alloc_array(ctx, isl_int *, bset->extra + n);
1790 if (!new_div)
1791 goto error;
1792 for (i = 0; i < n; ++i) {
1793 new_div[i] = bset->block2.data +
1794 (bset->extra + i) * (1 + row_size);
1795 isl_seq_clr(new_div[i], 1 + row_size);
1797 for (i = 0; i < bset->extra; ++i)
1798 new_div[n + i] = bset->block2.data + (bset->div[i] - old);
1799 free(bset->div);
1800 bset->div = new_div;
1801 bset->n_div += n;
1802 bset->extra += n;
1803 bset->dim = isl_dim_drop_outputs(bset->dim,
1804 isl_basic_set_n_dim(bset) - n, n);
1805 if (!bset->dim)
1806 goto error;
1807 bset = isl_basic_set_simplify(bset);
1808 return isl_basic_set_finalize(bset);
1809 error:
1810 isl_basic_set_free(bset);
1811 return NULL;
1814 struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
1816 int i, j;
1818 for (i = 0; i < n; ++i) {
1819 j = isl_basic_map_alloc_div(bmap);
1820 if (j < 0)
1821 goto error;
1822 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
1824 return bmap;
1825 error:
1826 isl_basic_map_free(bmap);
1827 return NULL;
1830 struct isl_basic_map *isl_basic_map_apply_range(
1831 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
1833 struct isl_dim *dim_result = NULL;
1834 struct isl_basic_map *bmap;
1835 unsigned n_in, n_out, n, nparam, total, pos;
1836 struct isl_dim_map *dim_map1, *dim_map2;
1838 if (!bmap1 || !bmap2)
1839 goto error;
1841 dim_result = isl_dim_join(isl_dim_copy(bmap1->dim),
1842 isl_dim_copy(bmap2->dim));
1844 n_in = isl_basic_map_n_in(bmap1);
1845 n_out = isl_basic_map_n_out(bmap2);
1846 n = isl_basic_map_n_out(bmap1);
1847 nparam = isl_basic_map_n_param(bmap1);
1849 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
1850 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
1851 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
1852 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
1853 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
1854 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
1855 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
1856 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
1857 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
1858 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
1859 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
1861 bmap = isl_basic_map_alloc_dim(dim_result,
1862 bmap1->n_div + bmap2->n_div + n,
1863 bmap1->n_eq + bmap2->n_eq,
1864 bmap1->n_ineq + bmap2->n_ineq);
1865 bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
1866 bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
1867 bmap = add_divs(bmap, n);
1868 bmap = isl_basic_map_simplify(bmap);
1869 return isl_basic_map_finalize(bmap);
1870 error:
1871 isl_basic_map_free(bmap1);
1872 isl_basic_map_free(bmap2);
1873 return NULL;
1876 struct isl_basic_set *isl_basic_set_apply(
1877 struct isl_basic_set *bset, struct isl_basic_map *bmap)
1879 if (!bset || !bmap)
1880 goto error;
1882 isl_assert(set->ctx, isl_basic_map_compatible_domain(bmap, bset),
1883 goto error);
1885 return (struct isl_basic_set *)
1886 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
1887 error:
1888 isl_basic_set_free(bset);
1889 isl_basic_map_free(bmap);
1890 return NULL;
1893 struct isl_basic_map *isl_basic_map_apply_domain(
1894 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
1896 if (!bmap1 || !bmap2)
1897 goto error;
1899 isl_assert(ctx,
1900 isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
1901 isl_assert(ctx,
1902 isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
1903 goto error);
1905 bmap1 = isl_basic_map_reverse(bmap1);
1906 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
1907 return isl_basic_map_reverse(bmap1);
1908 error:
1909 isl_basic_map_free(bmap1);
1910 isl_basic_map_free(bmap2);
1911 return NULL;
1914 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
1915 * A \cap B -> f(A) + f(B)
1917 struct isl_basic_map *isl_basic_map_sum(
1918 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
1920 unsigned n_in, n_out, nparam, total, pos;
1921 struct isl_basic_map *bmap = NULL;
1922 struct isl_dim_map *dim_map1, *dim_map2;
1923 int i;
1925 if (!bmap1 || !bmap2)
1926 goto error;
1928 isl_assert(bmap1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim),
1929 goto error);
1931 nparam = isl_basic_map_n_param(bmap1);
1932 n_in = isl_basic_map_n_in(bmap1);
1933 n_out = isl_basic_map_n_out(bmap1);
1935 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
1936 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
1937 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
1938 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
1939 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
1940 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
1941 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
1942 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
1943 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
1944 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
1945 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
1947 bmap = isl_basic_map_alloc_dim(isl_dim_copy(bmap1->dim),
1948 bmap1->n_div + bmap2->n_div + 2 * n_out,
1949 bmap1->n_eq + bmap2->n_eq + n_out,
1950 bmap1->n_ineq + bmap2->n_ineq);
1951 for (i = 0; i < n_out; ++i) {
1952 int j = isl_basic_map_alloc_equality(bmap);
1953 if (j < 0)
1954 goto error;
1955 isl_seq_clr(bmap->eq[j], 1+total);
1956 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
1957 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
1958 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
1960 bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
1961 bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
1962 bmap = add_divs(bmap, 2 * n_out);
1964 bmap = isl_basic_map_simplify(bmap);
1965 return isl_basic_map_finalize(bmap);
1966 error:
1967 isl_basic_map_free(bmap);
1968 isl_basic_map_free(bmap1);
1969 isl_basic_map_free(bmap2);
1970 return NULL;
1973 /* Given a basic map A -> f(A), construct A -> -f(A).
1975 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
1977 int i, j;
1978 unsigned off, n;
1980 bmap = isl_basic_map_cow(bmap);
1981 if (!bmap)
1982 return NULL;
1984 n = isl_basic_map_dim(bmap, isl_dim_out);
1985 off = isl_basic_map_offset(bmap, isl_dim_out);
1986 for (i = 0; i < bmap->n_eq; ++i)
1987 for (j = 0; j < n; ++j)
1988 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
1989 for (i = 0; i < bmap->n_ineq; ++i)
1990 for (j = 0; j < n; ++j)
1991 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
1992 for (i = 0; i < bmap->n_div; ++i)
1993 for (j = 0; j < n; ++j)
1994 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
1995 return isl_basic_map_finalize(bmap);
1998 /* Given a basic map A -> f(A) and an integer d, construct a basic map
1999 * A -> floor(f(A)/d).
2001 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
2002 isl_int d)
2004 unsigned n_in, n_out, nparam, total, pos;
2005 struct isl_basic_map *result = NULL;
2006 struct isl_dim_map *dim_map;
2007 int i;
2009 if (!bmap)
2010 return NULL;
2012 nparam = isl_basic_map_n_param(bmap);
2013 n_in = isl_basic_map_n_in(bmap);
2014 n_out = isl_basic_map_n_out(bmap);
2016 total = nparam + n_in + n_out + bmap->n_div + n_out;
2017 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2018 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
2019 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
2020 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
2021 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
2023 result = isl_basic_map_alloc_dim(isl_dim_copy(bmap->dim),
2024 bmap->n_div + n_out,
2025 bmap->n_eq, bmap->n_ineq + 2 * n_out);
2026 result = add_constraints_dim_map(result, bmap, dim_map);
2027 result = add_divs(result, n_out);
2028 for (i = 0; i < n_out; ++i) {
2029 int j;
2030 j = isl_basic_map_alloc_inequality(result);
2031 if (j < 0)
2032 goto error;
2033 isl_seq_clr(result->ineq[j], 1+total);
2034 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
2035 isl_int_set_si(result->ineq[j][1+pos+i], 1);
2036 j = isl_basic_map_alloc_inequality(result);
2037 if (j < 0)
2038 goto error;
2039 isl_seq_clr(result->ineq[j], 1+total);
2040 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
2041 isl_int_set_si(result->ineq[j][1+pos+i], -1);
2042 isl_int_sub_ui(result->ineq[j][0], d, 1);
2045 result = isl_basic_map_simplify(result);
2046 return isl_basic_map_finalize(result);
2047 error:
2048 isl_basic_map_free(result);
2049 return NULL;
2052 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
2054 int i;
2055 unsigned nparam;
2056 unsigned n_in;
2058 i = isl_basic_map_alloc_equality(bmap);
2059 if (i < 0)
2060 goto error;
2061 nparam = isl_basic_map_n_param(bmap);
2062 n_in = isl_basic_map_n_in(bmap);
2063 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
2064 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
2065 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
2066 return isl_basic_map_finalize(bmap);
2067 error:
2068 isl_basic_map_free(bmap);
2069 return NULL;
2072 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
2074 int i;
2075 unsigned nparam;
2076 unsigned n_in;
2078 i = isl_basic_map_alloc_inequality(bmap);
2079 if (i < 0)
2080 goto error;
2081 nparam = isl_basic_map_n_param(bmap);
2082 n_in = isl_basic_map_n_in(bmap);
2083 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
2084 isl_int_set_si(bmap->ineq[i][0], -1);
2085 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
2086 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
2087 return isl_basic_map_finalize(bmap);
2088 error:
2089 isl_basic_map_free(bmap);
2090 return NULL;
2093 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
2095 int i;
2096 unsigned nparam;
2097 unsigned n_in;
2099 i = isl_basic_map_alloc_inequality(bmap);
2100 if (i < 0)
2101 goto error;
2102 nparam = isl_basic_map_n_param(bmap);
2103 n_in = isl_basic_map_n_in(bmap);
2104 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
2105 isl_int_set_si(bmap->ineq[i][0], -1);
2106 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
2107 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
2108 return isl_basic_map_finalize(bmap);
2109 error:
2110 isl_basic_map_free(bmap);
2111 return NULL;
2114 struct isl_basic_map *isl_basic_map_equal(struct isl_dim *dim, unsigned n_equal)
2116 int i;
2117 struct isl_basic_map *bmap;
2118 bmap = isl_basic_map_alloc_dim(dim, 0, n_equal, 0);
2119 if (!bmap)
2120 return NULL;
2121 for (i = 0; i < n_equal && bmap; ++i)
2122 bmap = var_equal(bmap, i);
2123 return isl_basic_map_finalize(bmap);
2126 struct isl_basic_map *isl_basic_map_less_at(struct isl_dim *dim, unsigned pos)
2128 int i;
2129 struct isl_basic_map *bmap;
2130 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
2131 if (!bmap)
2132 return NULL;
2133 for (i = 0; i < pos && bmap; ++i)
2134 bmap = var_equal(bmap, i);
2135 if (bmap)
2136 bmap = var_less(bmap, pos);
2137 return isl_basic_map_finalize(bmap);
2140 struct isl_basic_map *isl_basic_map_more_at(struct isl_dim *dim, unsigned pos)
2142 int i;
2143 struct isl_basic_map *bmap;
2144 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
2145 if (!bmap)
2146 return NULL;
2147 for (i = 0; i < pos && bmap; ++i)
2148 bmap = var_equal(bmap, i);
2149 if (bmap)
2150 bmap = var_more(bmap, pos);
2151 return isl_basic_map_finalize(bmap);
2154 struct isl_basic_map *isl_basic_map_from_basic_set(
2155 struct isl_basic_set *bset, struct isl_dim *dim)
2157 struct isl_basic_map *bmap;
2159 bset = isl_basic_set_cow(bset);
2160 if (!bset || !dim)
2161 goto error;
2163 isl_assert(bset->ctx, isl_dim_compatible(bset->dim, dim), goto error);
2164 isl_dim_free(bset->dim);
2165 bmap = (struct isl_basic_map *) bset;
2166 bmap->dim = dim;
2167 return isl_basic_map_finalize(bmap);
2168 error:
2169 isl_basic_set_free(bset);
2170 isl_dim_free(dim);
2171 return NULL;
2174 struct isl_basic_set *isl_basic_set_from_basic_map(struct isl_basic_map *bmap)
2176 if (!bmap)
2177 goto error;
2178 if (bmap->dim->n_in == 0)
2179 return (struct isl_basic_set *)bmap;
2180 bmap = isl_basic_map_cow(bmap);
2181 if (!bmap)
2182 goto error;
2183 bmap->dim = isl_dim_cow(bmap->dim);
2184 if (!bmap->dim)
2185 goto error;
2186 bmap->dim->n_out += bmap->dim->n_in;
2187 bmap->dim->n_in = 0;
2188 bmap = isl_basic_map_finalize(bmap);
2189 return (struct isl_basic_set *)bmap;
2190 error:
2191 isl_basic_map_free(bmap);
2192 return NULL;
2195 /* For a div d = floor(f/m), add the constraints
2197 * f - m d >= 0
2198 * -(f-(n-1)) + m d >= 0
2200 * Note that the second constraint is the negation of
2202 * f - m d >= n
2204 static int add_div_constraints(struct isl_basic_map *bmap, unsigned div)
2206 int i, j;
2207 unsigned total = isl_basic_map_total_dim(bmap);
2208 unsigned div_pos = 1 + total - bmap->n_div + div;
2210 i = isl_basic_map_alloc_inequality(bmap);
2211 if (i < 0)
2212 return -1;
2213 isl_seq_cpy(bmap->ineq[i], bmap->div[div]+1, 1+total);
2214 isl_int_neg(bmap->ineq[i][div_pos], bmap->div[div][0]);
2216 j = isl_basic_map_alloc_inequality(bmap);
2217 if (j < 0)
2218 return -1;
2219 isl_seq_neg(bmap->ineq[j], bmap->ineq[i], 1 + total);
2220 isl_int_add(bmap->ineq[j][0], bmap->ineq[j][0], bmap->ineq[j][div_pos]);
2221 isl_int_sub_ui(bmap->ineq[j][0], bmap->ineq[j][0], 1);
2222 return j;
2225 struct isl_basic_set *isl_basic_map_underlying_set(
2226 struct isl_basic_map *bmap)
2228 if (!bmap)
2229 goto error;
2230 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 && bmap->n_div == 0)
2231 return (struct isl_basic_set *)bmap;
2232 bmap = isl_basic_map_cow(bmap);
2233 if (!bmap)
2234 goto error;
2235 bmap->dim = isl_dim_underlying(bmap->dim, bmap->n_div);
2236 if (!bmap->dim)
2237 goto error;
2238 bmap->extra -= bmap->n_div;
2239 bmap->n_div = 0;
2240 bmap = isl_basic_map_finalize(bmap);
2241 return (struct isl_basic_set *)bmap;
2242 error:
2243 return NULL;
2246 struct isl_basic_map *isl_basic_map_overlying_set(
2247 struct isl_basic_set *bset, struct isl_basic_map *like)
2249 struct isl_basic_map *bmap;
2250 struct isl_ctx *ctx;
2251 unsigned total;
2252 int i;
2254 if (!bset || !like)
2255 goto error;
2256 ctx = bset->ctx;
2257 isl_assert(ctx, bset->n_div == 0, goto error);
2258 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
2259 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
2260 goto error);
2261 if (isl_dim_equal(bset->dim, like->dim) && like->n_div == 0) {
2262 isl_basic_map_free(like);
2263 return (struct isl_basic_map *)bset;
2265 bset = isl_basic_set_cow(bset);
2266 if (!bset)
2267 goto error;
2268 total = bset->dim->n_out + bset->extra;
2269 bmap = (struct isl_basic_map *)bset;
2270 isl_dim_free(bmap->dim);
2271 bmap->dim = isl_dim_copy(like->dim);
2272 if (!bmap->dim)
2273 goto error;
2274 bmap->n_div = like->n_div;
2275 bmap->extra += like->n_div;
2276 if (bmap->extra) {
2277 unsigned ltotal;
2278 ltotal = total - bmap->extra + like->extra;
2279 if (ltotal > total)
2280 ltotal = total;
2281 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
2282 bmap->extra * (1 + 1 + total));
2283 if (isl_blk_is_error(bmap->block2))
2284 goto error;
2285 bmap->div = isl_realloc_array(ctx, bmap->div, isl_int *,
2286 bmap->extra);
2287 if (!bmap->div)
2288 goto error;
2289 for (i = 0; i < bmap->extra; ++i)
2290 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
2291 for (i = 0; i < like->n_div; ++i) {
2292 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
2293 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
2295 bmap = isl_basic_map_extend_constraints(bmap,
2296 0, 2 * like->n_div);
2297 for (i = 0; i < like->n_div; ++i) {
2298 if (isl_int_is_zero(bmap->div[i][0]))
2299 continue;
2300 if (add_div_constraints(bmap, i) < 0)
2301 goto error;
2304 isl_basic_map_free(like);
2305 bmap = isl_basic_map_simplify(bmap);
2306 bmap = isl_basic_map_finalize(bmap);
2307 return bmap;
2308 error:
2309 isl_basic_map_free(like);
2310 isl_basic_set_free(bset);
2311 return NULL;
2314 struct isl_basic_set *isl_basic_set_from_underlying_set(
2315 struct isl_basic_set *bset, struct isl_basic_set *like)
2317 return (struct isl_basic_set *)
2318 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
2321 struct isl_set *isl_set_from_underlying_set(
2322 struct isl_set *set, struct isl_basic_set *like)
2324 int i;
2326 if (!set || !like)
2327 goto error;
2328 isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
2329 goto error);
2330 if (isl_dim_equal(set->dim, like->dim) && like->n_div == 0) {
2331 isl_basic_set_free(like);
2332 return set;
2334 set = isl_set_cow(set);
2335 if (!set)
2336 goto error;
2337 for (i = 0; i < set->n; ++i) {
2338 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
2339 isl_basic_set_copy(like));
2340 if (!set->p[i])
2341 goto error;
2343 isl_dim_free(set->dim);
2344 set->dim = isl_dim_copy(like->dim);
2345 if (!set->dim)
2346 goto error;
2347 isl_basic_set_free(like);
2348 return set;
2349 error:
2350 isl_basic_set_free(like);
2351 isl_set_free(set);
2352 return NULL;
2355 struct isl_set *isl_map_underlying_set(struct isl_map *map)
2357 int i;
2359 map = isl_map_cow(map);
2360 if (!map)
2361 return NULL;
2362 map->dim = isl_dim_cow(map->dim);
2363 if (!map->dim)
2364 goto error;
2366 for (i = 1; i < map->n; ++i)
2367 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
2368 goto error);
2369 for (i = 0; i < map->n; ++i) {
2370 map->p[i] = (struct isl_basic_map *)
2371 isl_basic_map_underlying_set(map->p[i]);
2372 if (!map->p[i])
2373 goto error;
2375 if (map->n == 0)
2376 map->dim = isl_dim_underlying(map->dim, 0);
2377 else {
2378 isl_dim_free(map->dim);
2379 map->dim = isl_dim_copy(map->p[0]->dim);
2381 if (!map->dim)
2382 goto error;
2383 return (struct isl_set *)map;
2384 error:
2385 isl_map_free(map);
2386 return NULL;
2389 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
2391 return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
2394 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
2396 struct isl_basic_set *domain;
2397 unsigned n_out;
2398 if (!bmap)
2399 return NULL;
2400 n_out = isl_basic_map_n_out(bmap);
2401 domain = isl_basic_set_from_basic_map(bmap);
2402 return isl_basic_set_project_out(domain, n_out, 0);
2405 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
2407 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
2410 struct isl_set *isl_map_range(struct isl_map *map)
2412 int i;
2413 struct isl_set *set;
2415 if (!map)
2416 goto error;
2417 map = isl_map_cow(map);
2418 if (!map)
2419 goto error;
2421 set = (struct isl_set *) map;
2422 if (set->dim->n_in != 0) {
2423 set->dim = isl_dim_drop_inputs(set->dim, 0, set->dim->n_in);
2424 if (!set->dim)
2425 goto error;
2427 for (i = 0; i < map->n; ++i) {
2428 set->p[i] = isl_basic_map_range(map->p[i]);
2429 if (!set->p[i])
2430 goto error;
2432 ISL_F_CLR(set, ISL_MAP_DISJOINT);
2433 ISL_F_CLR(set, ISL_SET_NORMALIZED);
2434 return set;
2435 error:
2436 isl_map_free(map);
2437 return NULL;
2440 struct isl_map *isl_map_from_set(struct isl_set *set, struct isl_dim *dim)
2442 int i;
2443 struct isl_map *map = NULL;
2445 set = isl_set_cow(set);
2446 if (!set || !dim)
2447 goto error;
2448 isl_assert(set->ctx, isl_dim_compatible(set->dim, dim), goto error);
2449 map = (struct isl_map *)set;
2450 for (i = 0; i < set->n; ++i) {
2451 map->p[i] = isl_basic_map_from_basic_set(
2452 set->p[i], isl_dim_copy(dim));
2453 if (!map->p[i])
2454 goto error;
2456 isl_dim_free(map->dim);
2457 map->dim = dim;
2458 return map;
2459 error:
2460 isl_dim_free(dim);
2461 isl_set_free(set);
2462 return NULL;
2465 struct isl_map *isl_map_from_range(struct isl_set *set)
2467 return (struct isl_map *)set;
2470 struct isl_set *isl_set_from_map(struct isl_map *map)
2472 int i;
2473 struct isl_set *set = NULL;
2475 if (!map)
2476 return NULL;
2477 map = isl_map_cow(map);
2478 if (!map)
2479 return NULL;
2480 map->dim = isl_dim_cow(map->dim);
2481 if (!map->dim)
2482 goto error;
2483 map->dim->n_out += map->dim->n_in;
2484 map->dim->n_in = 0;
2485 set = (struct isl_set *)map;
2486 for (i = 0; i < map->n; ++i) {
2487 set->p[i] = isl_basic_set_from_basic_map(map->p[i]);
2488 if (!set->p[i])
2489 goto error;
2491 return set;
2492 error:
2493 isl_map_free(map);
2494 return NULL;
2497 struct isl_map *isl_map_alloc_dim(struct isl_dim *dim, int n, unsigned flags)
2499 struct isl_map *map;
2501 if (!dim)
2502 return NULL;
2503 isl_assert(dim->ctx, n >= 0, return NULL);
2504 map = isl_alloc(dim->ctx, struct isl_map,
2505 sizeof(struct isl_map) +
2506 n * sizeof(struct isl_basic_map *));
2507 if (!map)
2508 goto error;
2510 map->ctx = dim->ctx;
2511 isl_ctx_ref(map->ctx);
2512 map->ref = 1;
2513 map->size = n;
2514 map->n = 0;
2515 map->dim = dim;
2516 map->flags = flags;
2517 return map;
2518 error:
2519 isl_dim_free(dim);
2520 return NULL;
2523 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
2524 unsigned nparam, unsigned in, unsigned out, int n,
2525 unsigned flags)
2527 struct isl_map *map;
2528 struct isl_dim *dims;
2530 dims = isl_dim_alloc(ctx, nparam, in, out);
2531 if (!dims)
2532 return NULL;
2534 map = isl_map_alloc_dim(dims, n, flags);
2535 return map;
2538 struct isl_basic_map *isl_basic_map_empty(struct isl_ctx *ctx,
2539 unsigned nparam, unsigned in, unsigned out)
2541 struct isl_basic_map *bmap;
2542 bmap = isl_basic_map_alloc(ctx, nparam, in, out, 0, 1, 0);
2543 bmap = isl_basic_map_set_to_empty(bmap);
2544 return bmap;
2547 struct isl_basic_set *isl_basic_set_empty(struct isl_dim *dim)
2549 struct isl_basic_set *bset;
2550 bset = isl_basic_set_alloc_dim(dim, 0, 1, 0);
2551 bset = isl_basic_set_set_to_empty(bset);
2552 return bset;
2555 struct isl_basic_map *isl_basic_map_empty_like(struct isl_basic_map *model)
2557 struct isl_basic_map *bmap;
2558 if (!model)
2559 return NULL;
2560 bmap = isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
2561 bmap = isl_basic_map_set_to_empty(bmap);
2562 return bmap;
2565 struct isl_basic_map *isl_basic_map_empty_like_map(struct isl_map *model)
2567 struct isl_basic_map *bmap;
2568 if (!model)
2569 return NULL;
2570 bmap = isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
2571 bmap = isl_basic_map_set_to_empty(bmap);
2572 return bmap;
2575 struct isl_basic_set *isl_basic_set_empty_like(struct isl_basic_set *model)
2577 struct isl_basic_set *bset;
2578 if (!model)
2579 return NULL;
2580 bset = isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
2581 bset = isl_basic_set_set_to_empty(bset);
2582 return bset;
2585 struct isl_basic_map *isl_basic_map_universe(struct isl_dim *dim)
2587 struct isl_basic_map *bmap;
2588 bmap = isl_basic_map_alloc_dim(dim, 0, 0, 0);
2589 return bmap;
2592 struct isl_basic_set *isl_basic_set_universe(struct isl_dim *dim)
2594 struct isl_basic_set *bset;
2595 bset = isl_basic_set_alloc_dim(dim, 0, 0, 0);
2596 return bset;
2599 struct isl_basic_set *isl_basic_set_universe_like(struct isl_basic_set *model)
2601 if (!model)
2602 return NULL;
2603 return isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
2606 struct isl_map *isl_map_empty(struct isl_dim *dim)
2608 return isl_map_alloc_dim(dim, 0, ISL_MAP_DISJOINT);
2611 struct isl_map *isl_map_empty_like(struct isl_map *model)
2613 if (!model)
2614 return NULL;
2615 return isl_map_alloc_dim(isl_dim_copy(model->dim), 0, ISL_MAP_DISJOINT);
2618 struct isl_map *isl_map_empty_like_basic_map(struct isl_basic_map *model)
2620 if (!model)
2621 return NULL;
2622 return isl_map_alloc_dim(isl_dim_copy(model->dim), 0, ISL_MAP_DISJOINT);
2625 struct isl_set *isl_set_empty(struct isl_dim *dim)
2627 return isl_set_alloc_dim(dim, 0, ISL_MAP_DISJOINT);
2630 struct isl_set *isl_set_empty_like(struct isl_set *model)
2632 if (!model)
2633 return NULL;
2634 return isl_set_empty(isl_dim_copy(model->dim));
2637 struct isl_set *isl_set_universe(struct isl_dim *dim)
2639 struct isl_set *set;
2640 if (!dim)
2641 return NULL;
2642 set = isl_set_alloc_dim(isl_dim_copy(dim), 1, ISL_MAP_DISJOINT);
2643 set = isl_set_add(set, isl_basic_set_universe(dim));
2644 return set;
2647 struct isl_map *isl_map_dup(struct isl_map *map)
2649 int i;
2650 struct isl_map *dup;
2652 if (!map)
2653 return NULL;
2654 dup = isl_map_alloc_dim(isl_dim_copy(map->dim), map->n, map->flags);
2655 for (i = 0; i < map->n; ++i)
2656 dup = isl_map_add(dup, isl_basic_map_copy(map->p[i]));
2657 return dup;
2660 struct isl_map *isl_map_add(struct isl_map *map, struct isl_basic_map *bmap)
2662 if (!bmap || !map)
2663 goto error;
2664 isl_assert(map->ctx, isl_dim_equal(map->dim, bmap->dim), goto error);
2665 isl_assert(map->ctx, map->n < map->size, goto error);
2666 map->p[map->n] = bmap;
2667 map->n++;
2668 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
2669 return map;
2670 error:
2671 if (map)
2672 isl_map_free(map);
2673 if (bmap)
2674 isl_basic_map_free(bmap);
2675 return NULL;
2678 void isl_map_free(struct isl_map *map)
2680 int i;
2682 if (!map)
2683 return;
2685 if (--map->ref > 0)
2686 return;
2688 isl_ctx_deref(map->ctx);
2689 for (i = 0; i < map->n; ++i)
2690 isl_basic_map_free(map->p[i]);
2691 isl_dim_free(map->dim);
2692 free(map);
2695 struct isl_map *isl_map_extend(struct isl_map *base,
2696 unsigned nparam, unsigned n_in, unsigned n_out)
2698 int i;
2700 base = isl_map_cow(base);
2701 if (!base)
2702 return NULL;
2704 base->dim = isl_dim_extend(base->dim, nparam, n_in, n_out);
2705 if (!base->dim)
2706 goto error;
2707 for (i = 0; i < base->n; ++i) {
2708 base->p[i] = isl_basic_map_extend_dim(base->p[i],
2709 isl_dim_copy(base->dim), 0, 0, 0);
2710 if (!base->p[i])
2711 goto error;
2713 return base;
2714 error:
2715 isl_map_free(base);
2716 return NULL;
2719 struct isl_set *isl_set_extend(struct isl_set *base,
2720 unsigned nparam, unsigned dim)
2722 return (struct isl_set *)isl_map_extend((struct isl_map *)base,
2723 nparam, 0, dim);
2726 static struct isl_basic_map *isl_basic_map_fix_pos(struct isl_basic_map *bmap,
2727 unsigned pos, int value)
2729 int j;
2731 bmap = isl_basic_map_cow(bmap);
2732 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
2733 j = isl_basic_map_alloc_equality(bmap);
2734 if (j < 0)
2735 goto error;
2736 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
2737 isl_int_set_si(bmap->eq[j][pos], -1);
2738 isl_int_set_si(bmap->eq[j][0], value);
2739 bmap = isl_basic_map_simplify(bmap);
2740 return isl_basic_map_finalize(bmap);
2741 error:
2742 isl_basic_map_free(bmap);
2743 return NULL;
2746 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
2747 enum isl_dim_type type, unsigned pos, int value)
2749 if (!bmap)
2750 return NULL;
2751 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
2752 return isl_basic_map_fix_pos(bmap, isl_basic_map_offset(bmap, type) + pos,
2753 value);
2754 error:
2755 isl_basic_map_free(bmap);
2756 return NULL;
2759 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
2760 enum isl_dim_type type, unsigned pos, int value)
2762 return (struct isl_basic_set *)
2763 isl_basic_map_fix_si((struct isl_basic_map *)bset,
2764 type, pos, value);
2767 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
2768 unsigned input, int value)
2770 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
2773 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
2774 unsigned dim, int value)
2776 return (struct isl_basic_set *)
2777 isl_basic_map_fix_si((struct isl_basic_map *)bset,
2778 isl_dim_set, dim, value);
2781 struct isl_map *isl_map_fix_si(struct isl_map *map,
2782 enum isl_dim_type type, unsigned pos, int value)
2784 int i;
2786 map = isl_map_cow(map);
2787 if (!map)
2788 return NULL;
2790 isl_assert(ctx, pos < isl_map_dim(map, type), goto error);
2791 for (i = 0; i < map->n; ++i) {
2792 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
2793 if (!map->p[i])
2794 goto error;
2796 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
2797 return map;
2798 error:
2799 isl_map_free(map);
2800 return NULL;
2803 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
2804 unsigned input, int value)
2806 return isl_map_fix_si(map, isl_dim_in, input, value);
2809 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
2811 return (struct isl_set *)
2812 isl_map_fix_si((struct isl_map *)set, isl_dim_set, dim, value);
2815 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
2816 unsigned dim, isl_int value)
2818 int j;
2819 unsigned nparam;
2821 bset = isl_basic_set_cow(bset);
2822 bset = isl_basic_set_extend_constraints(bset, 0, 1);
2823 j = isl_basic_set_alloc_inequality(bset);
2824 if (j < 0)
2825 goto error;
2826 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
2827 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
2828 isl_int_neg(bset->ineq[j][0], value);
2829 bset = isl_basic_set_simplify(bset);
2830 return isl_basic_set_finalize(bset);
2831 error:
2832 isl_basic_set_free(bset);
2833 return NULL;
2836 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
2837 isl_int value)
2839 int i;
2841 set = isl_set_cow(set);
2842 if (!set)
2843 return NULL;
2845 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
2846 for (i = 0; i < set->n; ++i) {
2847 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
2848 if (!set->p[i])
2849 goto error;
2851 return set;
2852 error:
2853 isl_set_free(set);
2854 return NULL;
2857 struct isl_map *isl_map_reverse(struct isl_map *map)
2859 int i;
2860 unsigned t;
2862 map = isl_map_cow(map);
2863 if (!map)
2864 return NULL;
2866 map->dim = isl_dim_reverse(map->dim);
2867 if (!map->dim)
2868 goto error;
2869 for (i = 0; i < map->n; ++i) {
2870 map->p[i] = isl_basic_map_reverse(map->p[i]);
2871 if (!map->p[i])
2872 goto error;
2874 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
2875 return map;
2876 error:
2877 isl_map_free(map);
2878 return NULL;
2881 struct isl_map *isl_basic_map_lexmax(
2882 struct isl_basic_map *bmap, struct isl_basic_set *dom,
2883 struct isl_set **empty)
2885 return isl_pip_basic_map_lexmax(bmap, dom, empty);
2888 struct isl_map *isl_basic_map_lexmin(
2889 struct isl_basic_map *bmap, struct isl_basic_set *dom,
2890 struct isl_set **empty)
2892 return isl_pip_basic_map_lexmin(bmap, dom, empty);
2895 struct isl_set *isl_basic_set_lexmin(struct isl_basic_set *bset)
2897 struct isl_basic_map *bmap = NULL;
2898 struct isl_basic_set *dom = NULL;
2899 struct isl_map *min;
2900 struct isl_dim *param_dim;
2902 if (!bset)
2903 goto error;
2904 bmap = isl_basic_map_from_basic_set(bset, isl_dim_copy(bset->dim));
2905 if (!bmap)
2906 goto error;
2907 param_dim = isl_dim_domain(isl_dim_copy(bmap->dim));
2908 dom = isl_basic_set_universe(param_dim);
2909 if (!dom)
2910 goto error;
2911 min = isl_basic_map_lexmin(bmap, dom, NULL);
2912 return isl_map_range(min);
2913 error:
2914 isl_basic_map_free(bmap);
2915 return NULL;
2918 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
2920 int i;
2921 unsigned off;
2923 if (!bmap)
2924 return NULL;
2925 off = isl_dim_total(bmap->dim);
2926 for (i = 0; i < bmap->n_div; ++i) {
2927 if (isl_int_is_zero(bmap->div[i][0]))
2928 return isl_pip_basic_map_compute_divs(bmap);
2929 isl_assert(bmap->ctx, isl_int_is_zero(bmap->div[i][1+1+off+i]),
2930 goto error);
2932 return isl_map_from_basic_map(bmap);
2933 error:
2934 isl_basic_map_free(bmap);
2935 return NULL;
2938 struct isl_map *isl_map_compute_divs(struct isl_map *map)
2940 int i;
2941 struct isl_map *res;
2943 if (!map)
2944 return NULL;
2945 if (map->n == 0)
2946 return map;
2947 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
2948 for (i = 1 ; i < map->n; ++i) {
2949 struct isl_map *r2;
2950 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
2951 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
2952 res = isl_map_union_disjoint(res, r2);
2953 else
2954 res = isl_map_union(res, r2);
2956 isl_map_free(map);
2958 return res;
2961 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
2963 return (struct isl_set *)
2964 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
2967 struct isl_set *isl_set_compute_divs(struct isl_set *set)
2969 return (struct isl_set *)
2970 isl_map_compute_divs((struct isl_map *)set);
2973 struct isl_set *isl_map_domain(struct isl_map *map)
2975 int i;
2976 struct isl_set *set;
2978 if (!map)
2979 goto error;
2981 map = isl_map_cow(map);
2982 if (!map)
2983 return NULL;
2985 set = (struct isl_set *)map;
2986 set->dim = isl_dim_domain(set->dim);
2987 if (!set->dim)
2988 goto error;
2989 for (i = 0; i < map->n; ++i) {
2990 set->p[i] = isl_basic_map_domain(map->p[i]);
2991 if (!set->p[i])
2992 goto error;
2994 ISL_F_CLR(set, ISL_MAP_DISJOINT);
2995 ISL_F_CLR(set, ISL_SET_NORMALIZED);
2996 return set;
2997 error:
2998 isl_map_free(map);
2999 return NULL;
3002 struct isl_map *isl_map_union_disjoint(
3003 struct isl_map *map1, struct isl_map *map2)
3005 int i;
3006 unsigned flags = 0;
3007 struct isl_map *map = NULL;
3009 if (!map1 || !map2)
3010 goto error;
3012 if (map1->n == 0) {
3013 isl_map_free(map1);
3014 return map2;
3016 if (map2->n == 0) {
3017 isl_map_free(map2);
3018 return map1;
3021 isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
3023 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
3024 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
3025 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3027 map = isl_map_alloc_dim(isl_dim_copy(map1->dim),
3028 map1->n + map2->n, flags);
3029 if (!map)
3030 goto error;
3031 for (i = 0; i < map1->n; ++i) {
3032 map = isl_map_add(map,
3033 isl_basic_map_copy(map1->p[i]));
3034 if (!map)
3035 goto error;
3037 for (i = 0; i < map2->n; ++i) {
3038 map = isl_map_add(map,
3039 isl_basic_map_copy(map2->p[i]));
3040 if (!map)
3041 goto error;
3043 isl_map_free(map1);
3044 isl_map_free(map2);
3045 return map;
3046 error:
3047 isl_map_free(map);
3048 isl_map_free(map1);
3049 isl_map_free(map2);
3050 return NULL;
3053 struct isl_map *isl_map_union(struct isl_map *map1, struct isl_map *map2)
3055 map1 = isl_map_union_disjoint(map1, map2);
3056 if (!map1)
3057 return NULL;
3058 if (map1->n > 1)
3059 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
3060 return map1;
3063 struct isl_set *isl_set_union_disjoint(
3064 struct isl_set *set1, struct isl_set *set2)
3066 return (struct isl_set *)
3067 isl_map_union_disjoint(
3068 (struct isl_map *)set1, (struct isl_map *)set2);
3071 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
3073 return (struct isl_set *)
3074 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
3077 struct isl_map *isl_map_intersect_range(
3078 struct isl_map *map, struct isl_set *set)
3080 unsigned flags = 0;
3081 struct isl_map *result;
3082 int i, j;
3084 if (!map || !set)
3085 goto error;
3087 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
3088 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
3089 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3091 result = isl_map_alloc_dim(isl_dim_copy(map->dim),
3092 map->n * set->n, flags);
3093 if (!result)
3094 goto error;
3095 for (i = 0; i < map->n; ++i)
3096 for (j = 0; j < set->n; ++j) {
3097 result = isl_map_add(result,
3098 isl_basic_map_intersect_range(
3099 isl_basic_map_copy(map->p[i]),
3100 isl_basic_set_copy(set->p[j])));
3101 if (!result)
3102 goto error;
3104 isl_map_free(map);
3105 isl_set_free(set);
3106 return result;
3107 error:
3108 isl_map_free(map);
3109 isl_set_free(set);
3110 return NULL;
3113 struct isl_map *isl_map_intersect_domain(
3114 struct isl_map *map, struct isl_set *set)
3116 return isl_map_reverse(
3117 isl_map_intersect_range(isl_map_reverse(map), set));
3120 struct isl_map *isl_map_apply_domain(
3121 struct isl_map *map1, struct isl_map *map2)
3123 if (!map1 || !map2)
3124 goto error;
3125 map1 = isl_map_reverse(map1);
3126 map1 = isl_map_apply_range(map1, map2);
3127 return isl_map_reverse(map1);
3128 error:
3129 isl_map_free(map1);
3130 isl_map_free(map2);
3131 return NULL;
3134 struct isl_map *isl_map_apply_range(
3135 struct isl_map *map1, struct isl_map *map2)
3137 struct isl_dim *dim_result;
3138 struct isl_map *result;
3139 int i, j;
3140 unsigned nparam;
3141 unsigned n_in;
3142 unsigned n_out;
3144 if (!map1 || !map2)
3145 goto error;
3147 dim_result = isl_dim_join(isl_dim_copy(map1->dim),
3148 isl_dim_copy(map2->dim));
3150 result = isl_map_alloc_dim(dim_result, map1->n * map2->n, 0);
3151 if (!result)
3152 goto error;
3153 for (i = 0; i < map1->n; ++i)
3154 for (j = 0; j < map2->n; ++j) {
3155 result = isl_map_add(result,
3156 isl_basic_map_apply_range(
3157 isl_basic_map_copy(map1->p[i]),
3158 isl_basic_map_copy(map2->p[j])));
3159 if (!result)
3160 goto error;
3162 isl_map_free(map1);
3163 isl_map_free(map2);
3164 if (result && result->n <= 1)
3165 ISL_F_SET(result, ISL_MAP_DISJOINT);
3166 return result;
3167 error:
3168 isl_map_free(map1);
3169 isl_map_free(map2);
3170 return NULL;
3174 * returns range - domain
3176 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
3178 struct isl_basic_set *bset;
3179 unsigned dim;
3180 unsigned nparam;
3181 int i;
3183 if (!bmap)
3184 goto error;
3185 dim = isl_basic_map_n_in(bmap);
3186 nparam = isl_basic_map_n_param(bmap);
3187 isl_assert(bmap->ctx, dim == isl_basic_map_n_out(bmap), goto error);
3188 bset = isl_basic_set_from_basic_map(bmap);
3189 bset = isl_basic_set_cow(bset);
3190 bset = isl_basic_set_extend(bset, nparam, 3*dim, 0, dim, 0);
3191 bset = isl_basic_set_swap_vars(bset, 2*dim);
3192 for (i = 0; i < dim; ++i) {
3193 int j = isl_basic_map_alloc_equality(
3194 (struct isl_basic_map *)bset);
3195 if (j < 0)
3196 goto error;
3197 isl_seq_clr(bset->eq[j], 1 + isl_basic_set_total_dim(bset));
3198 isl_int_set_si(bset->eq[j][1+nparam+i], 1);
3199 isl_int_set_si(bset->eq[j][1+nparam+dim+i], 1);
3200 isl_int_set_si(bset->eq[j][1+nparam+2*dim+i], -1);
3202 return isl_basic_set_project_out(bset, 2*dim, 0);
3203 error:
3204 isl_basic_map_free(bmap);
3205 return NULL;
3209 * returns range - domain
3211 struct isl_set *isl_map_deltas(struct isl_map *map)
3213 int i;
3214 struct isl_set *result;
3216 if (!map)
3217 return NULL;
3219 isl_assert(map->ctx, isl_map_n_in(map) == isl_map_n_out(map), goto error);
3220 result = isl_set_alloc(map->ctx, isl_map_n_param(map),
3221 isl_map_n_in(map), map->n, map->flags);
3222 if (!result)
3223 goto error;
3224 for (i = 0; i < map->n; ++i)
3225 result = isl_set_add(result,
3226 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
3227 isl_map_free(map);
3228 return result;
3229 error:
3230 isl_map_free(map);
3231 return NULL;
3234 static struct isl_basic_map *basic_map_identity(struct isl_dim *dims)
3236 struct isl_basic_map *bmap;
3237 unsigned nparam;
3238 unsigned dim;
3239 int i;
3241 if (!dims)
3242 return NULL;
3244 nparam = dims->nparam;
3245 dim = dims->n_out;
3246 bmap = isl_basic_map_alloc_dim(dims, 0, dim, 0);
3247 if (!bmap)
3248 goto error;
3250 for (i = 0; i < dim; ++i) {
3251 int j = isl_basic_map_alloc_equality(bmap);
3252 if (j < 0)
3253 goto error;
3254 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
3255 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
3256 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
3258 return isl_basic_map_finalize(bmap);
3259 error:
3260 isl_basic_map_free(bmap);
3261 return NULL;
3264 struct isl_basic_map *isl_basic_map_identity(struct isl_dim *set_dim)
3266 struct isl_dim *dim = isl_dim_map(set_dim);
3267 if (!dim)
3268 return NULL;
3269 return basic_map_identity(dim);
3272 struct isl_basic_map *isl_basic_map_identity_like(struct isl_basic_map *model)
3274 if (!model || !model->dim)
3275 return NULL;
3276 isl_assert(model->ctx,
3277 model->dim->n_in == model->dim->n_out, return NULL);
3278 return basic_map_identity(isl_dim_copy(model->dim));
3281 static struct isl_map *map_identity(struct isl_dim *dim)
3283 struct isl_map *map = isl_map_alloc_dim(dim, 1, ISL_MAP_DISJOINT);
3284 return isl_map_add(map, basic_map_identity(isl_dim_copy(dim)));
3287 struct isl_map *isl_map_identity(struct isl_dim *set_dim)
3289 struct isl_dim *dim = isl_dim_map(set_dim);
3290 if (!dim)
3291 return NULL;
3292 return map_identity(dim);
3295 struct isl_map *isl_map_identity_like(struct isl_basic_map *model)
3297 if (!model || !model->dim)
3298 return NULL;
3299 isl_assert(model->ctx,
3300 model->dim->n_in == model->dim->n_out, return NULL);
3301 return map_identity(isl_dim_copy(model->dim));
3304 int isl_set_is_equal(struct isl_set *set1, struct isl_set *set2)
3306 return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
3309 int isl_set_is_subset(struct isl_set *set1, struct isl_set *set2)
3311 return isl_map_is_subset(
3312 (struct isl_map *)set1, (struct isl_map *)set2);
3315 int isl_basic_map_is_subset(
3316 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3318 int is_subset;
3319 struct isl_map *map1;
3320 struct isl_map *map2;
3322 if (!bmap1 || !bmap2)
3323 return -1;
3325 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
3326 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
3328 is_subset = isl_map_is_subset(map1, map2);
3330 isl_map_free(map1);
3331 isl_map_free(map2);
3333 return is_subset;
3336 int isl_basic_map_is_equal(
3337 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3339 int is_subset;
3341 if (!bmap1 || !bmap2)
3342 return -1;
3343 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
3344 if (is_subset != 1)
3345 return is_subset;
3346 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
3347 return is_subset;
3350 int isl_basic_set_is_equal(
3351 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
3353 return isl_basic_map_is_equal(
3354 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
3357 int isl_map_is_empty(struct isl_map *map)
3359 int i;
3360 int is_empty;
3362 if (!map)
3363 return -1;
3364 for (i = 0; i < map->n; ++i) {
3365 is_empty = isl_basic_map_is_empty(map->p[i]);
3366 if (is_empty < 0)
3367 return -1;
3368 if (!is_empty)
3369 return 0;
3371 return 1;
3374 int isl_map_fast_is_empty(struct isl_map *map)
3376 return map->n == 0;
3379 int isl_set_is_empty(struct isl_set *set)
3381 return isl_map_is_empty((struct isl_map *)set);
3384 int isl_map_is_subset(struct isl_map *map1, struct isl_map *map2)
3386 int i;
3387 int is_subset = 0;
3388 struct isl_map *diff;
3390 if (!map1 || !map2)
3391 return -1;
3393 if (isl_map_is_empty(map1))
3394 return 1;
3396 if (isl_map_is_empty(map2))
3397 return 0;
3399 diff = isl_map_subtract(isl_map_copy(map1), isl_map_copy(map2));
3400 if (!diff)
3401 return -1;
3403 is_subset = isl_map_is_empty(diff);
3404 isl_map_free(diff);
3406 return is_subset;
3409 int isl_map_is_equal(struct isl_map *map1, struct isl_map *map2)
3411 int is_subset;
3413 if (!map1 || !map2)
3414 return -1;
3415 is_subset = isl_map_is_subset(map1, map2);
3416 if (is_subset != 1)
3417 return is_subset;
3418 is_subset = isl_map_is_subset(map2, map1);
3419 return is_subset;
3422 int isl_basic_map_is_strict_subset(
3423 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3425 int is_subset;
3427 if (!bmap1 || !bmap2)
3428 return -1;
3429 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
3430 if (is_subset != 1)
3431 return is_subset;
3432 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
3433 if (is_subset == -1)
3434 return is_subset;
3435 return !is_subset;
3438 int isl_basic_map_is_universe(struct isl_basic_map *bmap)
3440 if (!bmap)
3441 return -1;
3442 return bmap->n_eq == 0 && bmap->n_ineq == 0;
3445 int isl_basic_map_is_empty(struct isl_basic_map *bmap)
3447 struct isl_basic_set *bset = NULL;
3448 struct isl_vec *sample = NULL;
3449 int empty;
3450 unsigned total;
3452 if (!bmap)
3453 return -1;
3455 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
3456 return 1;
3458 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
3459 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
3460 copy = isl_basic_map_convex_hull(copy);
3461 empty = ISL_F_ISSET(copy, ISL_BASIC_MAP_EMPTY);
3462 isl_basic_map_free(copy);
3463 return empty;
3466 total = 1 + isl_basic_map_total_dim(bmap);
3467 if (bmap->sample && bmap->sample->size == total) {
3468 int contains = basic_map_contains(bmap, bmap->sample);
3469 if (contains < 0)
3470 return -1;
3471 if (contains)
3472 return 0;
3474 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
3475 if (!bset)
3476 return -1;
3477 sample = isl_basic_set_sample(bset);
3478 if (!sample)
3479 return -1;
3480 empty = sample->size == 0;
3481 if (bmap->sample)
3482 isl_vec_free(bmap->ctx, bmap->sample);
3483 bmap->sample = sample;
3485 return empty;
3488 int isl_basic_set_is_empty(struct isl_basic_set *bset)
3490 return isl_basic_map_is_empty((struct isl_basic_map *)bset);
3493 struct isl_map *isl_basic_map_union(
3494 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3496 struct isl_map *map;
3497 if (!bmap1 || !bmap2)
3498 return NULL;
3500 isl_assert(map1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim), goto error);
3502 map = isl_map_alloc_dim(isl_dim_copy(bmap1->dim), 2, 0);
3503 if (!map)
3504 goto error;
3505 map = isl_map_add(map, bmap1);
3506 map = isl_map_add(map, bmap2);
3507 return map;
3508 error:
3509 isl_basic_map_free(bmap1);
3510 isl_basic_map_free(bmap2);
3511 return NULL;
3514 struct isl_set *isl_basic_set_union(
3515 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
3517 return (struct isl_set *)isl_basic_map_union(
3518 (struct isl_basic_map *)bset1,
3519 (struct isl_basic_map *)bset2);
3522 /* Order divs such that any div only depends on previous divs */
3523 static struct isl_basic_map *order_divs(struct isl_basic_map *bmap)
3525 int i;
3526 unsigned off = isl_dim_total(bmap->dim);
3528 for (i = 0; i < bmap->n_div; ++i) {
3529 int pos;
3530 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
3531 bmap->n_div-i);
3532 if (pos == -1)
3533 continue;
3534 swap_div(bmap, i, pos);
3535 --i;
3537 return bmap;
3540 /* Look for a div in dst that corresponds to the div "div" in src.
3541 * The divs before "div" in src and dst are assumed to be the same.
3543 * Returns -1 if no corresponding div was found and the position
3544 * of the corresponding div in dst otherwise.
3546 static int find_div(struct isl_basic_map *dst,
3547 struct isl_basic_map *src, unsigned div)
3549 int i;
3551 unsigned total = isl_dim_total(src->dim);
3553 isl_assert(dst->ctx, div <= dst->n_div, return -1);
3554 for (i = div; i < dst->n_div; ++i)
3555 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
3556 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
3557 dst->n_div - div) == -1)
3558 return i;
3559 return -1;
3562 struct isl_basic_map *isl_basic_map_align_divs(
3563 struct isl_basic_map *dst, struct isl_basic_map *src)
3565 int i;
3566 unsigned total = isl_dim_total(src->dim);
3568 if (!dst || !src)
3569 goto error;
3571 if (src->n_div == 0)
3572 return dst;
3574 for (i = 0; i < src->n_div; ++i)
3575 isl_assert(src->ctx, !isl_int_is_zero(src->div[i][0]), goto error);
3577 src = order_divs(src);
3578 dst = isl_basic_map_cow(dst);
3579 dst = isl_basic_map_extend_dim(dst, isl_dim_copy(dst->dim),
3580 src->n_div, 0, 2 * src->n_div);
3581 if (!dst)
3582 return NULL;
3583 for (i = 0; i < src->n_div; ++i) {
3584 int j = find_div(dst, src, i);
3585 if (j < 0) {
3586 j = isl_basic_map_alloc_div(dst);
3587 if (j < 0)
3588 goto error;
3589 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
3590 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
3591 if (add_div_constraints(dst, j) < 0)
3592 goto error;
3594 if (j != i)
3595 swap_div(dst, i, j);
3597 return dst;
3598 error:
3599 isl_basic_map_free(dst);
3600 return NULL;
3603 struct isl_basic_set *isl_basic_set_align_divs(
3604 struct isl_basic_set *dst, struct isl_basic_set *src)
3606 return (struct isl_basic_set *)isl_basic_map_align_divs(
3607 (struct isl_basic_map *)dst, (struct isl_basic_map *)src);
3610 struct isl_map *isl_map_align_divs(struct isl_map *map)
3612 int i;
3614 map = isl_map_compute_divs(map);
3615 map = isl_map_cow(map);
3616 if (!map)
3617 return NULL;
3619 for (i = 1; i < map->n; ++i)
3620 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
3621 for (i = 1; i < map->n; ++i)
3622 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
3624 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3625 return map;
3628 struct isl_set *isl_set_align_divs(struct isl_set *set)
3630 return (struct isl_set *)isl_map_align_divs((struct isl_map *)set);
3633 static struct isl_map *add_cut_constraint(struct isl_map *dst,
3634 struct isl_basic_map *src, isl_int *c,
3635 unsigned len, int oppose)
3637 struct isl_basic_map *copy = NULL;
3638 int is_empty;
3639 int k;
3640 unsigned total;
3642 copy = isl_basic_map_copy(src);
3643 copy = isl_basic_map_cow(copy);
3644 if (!copy)
3645 goto error;
3646 copy = isl_basic_map_extend_constraints(copy, 0, 1);
3647 k = isl_basic_map_alloc_inequality(copy);
3648 if (k < 0)
3649 goto error;
3650 if (oppose)
3651 isl_seq_neg(copy->ineq[k], c, len);
3652 else
3653 isl_seq_cpy(copy->ineq[k], c, len);
3654 total = 1 + isl_basic_map_total_dim(copy);
3655 isl_seq_clr(copy->ineq[k]+len, total - len);
3656 isl_inequality_negate(copy, k);
3657 copy = isl_basic_map_simplify(copy);
3658 copy = isl_basic_map_finalize(copy);
3659 is_empty = isl_basic_map_is_empty(copy);
3660 if (is_empty < 0)
3661 goto error;
3662 if (!is_empty)
3663 dst = isl_map_add(dst, copy);
3664 else
3665 isl_basic_map_free(copy);
3666 return dst;
3667 error:
3668 isl_basic_map_free(copy);
3669 isl_map_free(dst);
3670 return NULL;
3673 static struct isl_map *subtract(struct isl_map *map, struct isl_basic_map *bmap)
3675 int i, j, k;
3676 unsigned flags = 0;
3677 struct isl_map *rest = NULL;
3678 unsigned max;
3679 unsigned total = isl_basic_map_total_dim(bmap);
3681 assert(bmap);
3683 if (!map)
3684 goto error;
3686 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
3687 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3689 max = map->n * (2 * bmap->n_eq + bmap->n_ineq);
3690 rest = isl_map_alloc_dim(isl_dim_copy(map->dim), max, flags);
3691 if (!rest)
3692 goto error;
3694 for (i = 0; i < map->n; ++i) {
3695 map->p[i] = isl_basic_map_align_divs(map->p[i], bmap);
3696 if (!map->p[i])
3697 goto error;
3700 for (j = 0; j < map->n; ++j)
3701 map->p[j] = isl_basic_map_cow(map->p[j]);
3703 for (i = 0; i < bmap->n_eq; ++i) {
3704 for (j = 0; j < map->n; ++j) {
3705 rest = add_cut_constraint(rest,
3706 map->p[j], bmap->eq[i], 1+total, 0);
3707 if (!rest)
3708 goto error;
3710 rest = add_cut_constraint(rest,
3711 map->p[j], bmap->eq[i], 1+total, 1);
3712 if (!rest)
3713 goto error;
3715 map->p[j] = isl_basic_map_extend_constraints(map->p[j],
3716 1, 0);
3717 if (!map->p[j])
3718 goto error;
3719 k = isl_basic_map_alloc_equality(map->p[j]);
3720 if (k < 0)
3721 goto error;
3722 isl_seq_cpy(map->p[j]->eq[k], bmap->eq[i], 1+total);
3723 isl_seq_clr(map->p[j]->eq[k]+1+total,
3724 map->p[j]->n_div - bmap->n_div);
3728 for (i = 0; i < bmap->n_ineq; ++i) {
3729 for (j = 0; j < map->n; ++j) {
3730 rest = add_cut_constraint(rest,
3731 map->p[j], bmap->ineq[i], 1+total, 0);
3732 if (!rest)
3733 goto error;
3735 map->p[j] = isl_basic_map_extend_constraints(map->p[j],
3736 0, 1);
3737 if (!map->p[j])
3738 goto error;
3739 k = isl_basic_map_alloc_inequality(map->p[j]);
3740 if (k < 0)
3741 goto error;
3742 isl_seq_cpy(map->p[j]->ineq[k], bmap->ineq[i], 1+total);
3743 isl_seq_clr(map->p[j]->ineq[k]+1+total,
3744 map->p[j]->n_div - bmap->n_div);
3748 isl_map_free(map);
3749 return rest;
3750 error:
3751 isl_map_free(map);
3752 isl_map_free(rest);
3753 return NULL;
3756 struct isl_map *isl_map_subtract(struct isl_map *map1, struct isl_map *map2)
3758 int i;
3759 if (!map1 || !map2)
3760 goto error;
3762 isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
3764 if (isl_map_is_empty(map2)) {
3765 isl_map_free(map2);
3766 return map1;
3769 map1 = isl_map_compute_divs(map1);
3770 map2 = isl_map_compute_divs(map2);
3771 if (!map1 || !map2)
3772 goto error;
3774 for (i = 0; map1 && i < map2->n; ++i)
3775 map1 = subtract(map1, map2->p[i]);
3777 isl_map_free(map2);
3778 return map1;
3779 error:
3780 isl_map_free(map1);
3781 isl_map_free(map2);
3782 return NULL;
3785 struct isl_set *isl_set_subtract(struct isl_set *set1, struct isl_set *set2)
3787 return (struct isl_set *)
3788 isl_map_subtract(
3789 (struct isl_map *)set1, (struct isl_map *)set2);
3792 struct isl_set *isl_set_apply(struct isl_set *set, struct isl_map *map)
3794 if (!set || !map)
3795 goto error;
3796 isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
3797 map = isl_map_intersect_domain(map, set);
3798 set = isl_map_range(map);
3799 return set;
3800 error:
3801 isl_set_free(set);
3802 isl_map_free(map);
3803 return NULL;
3806 /* There is no need to cow as removing empty parts doesn't change
3807 * the meaning of the set.
3809 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
3811 int i;
3813 if (!map)
3814 return NULL;
3816 for (i = map->n-1; i >= 0; --i) {
3817 if (!ISL_F_ISSET(map->p[i], ISL_BASIC_MAP_EMPTY))
3818 continue;
3819 isl_basic_map_free(map->p[i]);
3820 if (i != map->n-1) {
3821 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3822 map->p[i] = map->p[map->n-1];
3824 map->n--;
3827 return map;
3830 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
3832 return (struct isl_set *)
3833 isl_map_remove_empty_parts((struct isl_map *)set);
3836 struct isl_basic_map *isl_map_copy_basic_map(struct isl_map *map)
3838 struct isl_basic_map *bmap;
3839 if (!map || map->n == 0)
3840 return NULL;
3841 bmap = map->p[map->n-1];
3842 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
3843 return isl_basic_map_copy(bmap);
3846 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
3848 (struct isl_basic_set *)isl_map_copy_basic_map((struct isl_map *)set);
3851 struct isl_map *isl_map_drop_basic_map(struct isl_map *map,
3852 struct isl_basic_map *bmap)
3854 int i;
3856 if (!map || !bmap)
3857 goto error;
3858 for (i = map->n-1; i >= 0; --i) {
3859 if (map->p[i] != bmap)
3860 continue;
3861 map = isl_map_cow(map);
3862 if (!map)
3863 goto error;
3864 isl_basic_map_free(map->p[i]);
3865 if (i != map->n-1) {
3866 ISL_F_CLR(map, ISL_SET_NORMALIZED);
3867 map->p[i] = map->p[map->n-1];
3869 map->n--;
3870 return map;
3872 isl_basic_map_free(bmap);
3873 return map;
3874 error:
3875 isl_map_free(map);
3876 isl_basic_map_free(bmap);
3877 return NULL;
3880 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
3881 struct isl_basic_set *bset)
3883 (struct isl_set *)isl_map_drop_basic_map((struct isl_map *)set,
3884 (struct isl_basic_map *)bset);
3887 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
3888 * for any common value of the parameters and dimensions preceding dim
3889 * in both basic sets, the values of dimension pos in bset1 are
3890 * smaller or larger than those in bset2.
3892 * Returns
3893 * 1 if bset1 follows bset2
3894 * -1 if bset1 precedes bset2
3895 * 0 if bset1 and bset2 are incomparable
3896 * -2 if some error occurred.
3898 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
3899 struct isl_basic_set *bset2, int pos)
3901 struct isl_dim *dims;
3902 struct isl_basic_map *bmap1 = NULL;
3903 struct isl_basic_map *bmap2 = NULL;
3904 struct isl_ctx *ctx;
3905 struct isl_vec *obj;
3906 unsigned total;
3907 unsigned nparam;
3908 unsigned dim1, dim2;
3909 isl_int num, den;
3910 enum isl_lp_result res;
3911 int cmp;
3913 if (!bset1 || !bset2)
3914 return -2;
3916 nparam = isl_basic_set_n_param(bset1);
3917 dim1 = isl_basic_set_n_dim(bset1);
3918 dim2 = isl_basic_set_n_dim(bset2);
3919 dims = isl_dim_alloc(bset1->ctx, nparam, pos, dim1 - pos);
3920 bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1), dims);
3921 dims = isl_dim_alloc(bset2->ctx, nparam, pos, dim2 - pos);
3922 bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims);
3923 if (!bmap1 || !bmap2)
3924 goto error;
3925 bmap1 = isl_basic_map_cow(bmap1);
3926 bmap1 = isl_basic_map_extend(bmap1, nparam,
3927 pos, (dim1 - pos) + (dim2 - pos),
3928 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
3929 bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
3930 if (!bmap1)
3931 goto error;
3932 total = isl_basic_map_total_dim(bmap1);
3933 ctx = bmap1->ctx;
3934 obj = isl_vec_alloc(ctx, 1 + total);
3935 isl_seq_clr(obj->block.data, 1 + total);
3936 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
3937 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
3938 if (!obj)
3939 goto error;
3940 isl_int_init(num);
3941 isl_int_init(den);
3942 res = isl_solve_lp(bmap1, 0, obj->block.data, ctx->one, &num, &den);
3943 if (res == isl_lp_empty)
3944 cmp = 0;
3945 else if (res == isl_lp_ok && isl_int_is_pos(num))
3946 cmp = 1;
3947 else if ((res == isl_lp_ok && isl_int_is_neg(num)) ||
3948 res == isl_lp_unbounded)
3949 cmp = -1;
3950 else
3951 cmp = -2;
3952 isl_int_clear(num);
3953 isl_int_clear(den);
3954 isl_basic_map_free(bmap1);
3955 isl_vec_free(ctx, obj);
3956 return cmp;
3957 error:
3958 isl_basic_map_free(bmap1);
3959 isl_basic_map_free(bmap2);
3960 return -2;
3963 static int isl_basic_map_fast_has_fixed_var(struct isl_basic_map *bmap,
3964 unsigned pos, isl_int *val)
3966 int i;
3967 int d;
3968 unsigned total;
3970 if (!bmap)
3971 return -1;
3972 total = isl_basic_map_total_dim(bmap);
3973 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
3974 for (; d+1 > pos; --d)
3975 if (!isl_int_is_zero(bmap->eq[i][1+d]))
3976 break;
3977 if (d != pos)
3978 continue;
3979 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
3980 return 0;
3981 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
3982 return 0;
3983 if (!isl_int_is_one(bmap->eq[i][1+d]))
3984 return 0;
3985 if (val)
3986 isl_int_neg(*val, bmap->eq[i][0]);
3987 return 1;
3989 return 0;
3992 static int isl_map_fast_has_fixed_var(struct isl_map *map,
3993 unsigned pos, isl_int *val)
3995 int i;
3996 isl_int v;
3997 isl_int tmp;
3998 int fixed;
4000 if (!map)
4001 return -1;
4002 if (map->n == 0)
4003 return 0;
4004 if (map->n == 1)
4005 return isl_basic_map_fast_has_fixed_var(map->p[0], pos, val);
4006 isl_int_init(v);
4007 isl_int_init(tmp);
4008 fixed = isl_basic_map_fast_has_fixed_var(map->p[0], pos, &v);
4009 for (i = 1; fixed == 1 && i < map->n; ++i) {
4010 fixed = isl_basic_map_fast_has_fixed_var(map->p[i], pos, &tmp);
4011 if (fixed == 1 && isl_int_ne(tmp, v))
4012 fixed = 0;
4014 if (val)
4015 isl_int_set(*val, v);
4016 isl_int_clear(tmp);
4017 isl_int_clear(v);
4018 return fixed;
4021 static int isl_set_fast_has_fixed_var(struct isl_set *set, unsigned pos,
4022 isl_int *val)
4024 return isl_map_fast_has_fixed_var((struct isl_map *)set, pos, val);
4027 int isl_basic_map_fast_is_fixed(struct isl_basic_map *bmap,
4028 enum isl_dim_type type, unsigned pos, isl_int *val)
4030 if (pos >= isl_basic_map_dim(bmap, type))
4031 return -1;
4032 return isl_basic_map_fast_has_fixed_var(bmap,
4033 isl_basic_map_offset(bmap, type) - 1 + pos, val);
4036 /* Check if dimension dim has fixed value and if so and if val is not NULL,
4037 * then return this fixed value in *val.
4039 int isl_set_fast_dim_is_fixed(struct isl_set *set, unsigned dim, isl_int *val)
4041 return isl_set_fast_has_fixed_var(set, isl_set_n_param(set) + dim, val);
4044 /* Check if input variable in has fixed value and if so and if val is not NULL,
4045 * then return this fixed value in *val.
4047 int isl_map_fast_input_is_fixed(struct isl_map *map, unsigned in, isl_int *val)
4049 return isl_map_fast_has_fixed_var(map, isl_map_n_param(map) + in, val);
4052 /* Check if dimension dim has an (obvious) fixed lower bound and if so
4053 * and if val is not NULL, then return this lower bound in *val.
4055 int isl_basic_set_fast_dim_has_fixed_lower_bound(struct isl_basic_set *bset,
4056 unsigned dim, isl_int *val)
4058 int i, i_eq = -1, i_ineq = -1;
4059 isl_int *c;
4060 unsigned total;
4061 unsigned nparam;
4063 if (!bset)
4064 return -1;
4065 total = isl_basic_set_total_dim(bset);
4066 nparam = isl_basic_set_n_param(bset);
4067 for (i = 0; i < bset->n_eq; ++i) {
4068 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
4069 continue;
4070 if (i_eq != -1)
4071 return 0;
4072 i_eq = i;
4074 for (i = 0; i < bset->n_ineq; ++i) {
4075 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
4076 continue;
4077 if (i_eq != -1 || i_ineq != -1)
4078 return 0;
4079 i_ineq = i;
4081 if (i_eq == -1 && i_ineq == -1)
4082 return 0;
4083 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
4084 /* The coefficient should always be one due to normalization. */
4085 if (!isl_int_is_one(c[1+nparam+dim]))
4086 return 0;
4087 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
4088 return 0;
4089 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
4090 total - nparam - dim - 1) != -1)
4091 return 0;
4092 if (val)
4093 isl_int_neg(*val, c[0]);
4094 return 1;
4097 int isl_set_fast_dim_has_fixed_lower_bound(struct isl_set *set,
4098 unsigned dim, isl_int *val)
4100 int i;
4101 isl_int v;
4102 isl_int tmp;
4103 int fixed;
4105 if (!set)
4106 return -1;
4107 if (set->n == 0)
4108 return 0;
4109 if (set->n == 1)
4110 return isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[0],
4111 dim, val);
4112 isl_int_init(v);
4113 isl_int_init(tmp);
4114 fixed = isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[0],
4115 dim, &v);
4116 for (i = 1; fixed == 1 && i < set->n; ++i) {
4117 fixed = isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[i],
4118 dim, &tmp);
4119 if (fixed == 1 && isl_int_ne(tmp, v))
4120 fixed = 0;
4122 if (val)
4123 isl_int_set(*val, v);
4124 isl_int_clear(tmp);
4125 isl_int_clear(v);
4126 return fixed;
4129 struct constraint {
4130 unsigned size;
4131 isl_int *c;
4134 static int qsort_constraint_cmp(const void *p1, const void *p2)
4136 const struct constraint *c1 = (const struct constraint *)p1;
4137 const struct constraint *c2 = (const struct constraint *)p2;
4138 unsigned size = isl_min(c1->size, c2->size);
4139 return isl_seq_cmp(c1->c, c2->c, size);
4142 static struct isl_basic_map *isl_basic_map_sort_constraints(
4143 struct isl_basic_map *bmap)
4145 int i;
4146 struct constraint *c;
4147 unsigned total;
4149 if (!bmap)
4150 return NULL;
4151 total = isl_basic_map_total_dim(bmap);
4152 c = isl_alloc_array(bmap->ctx, struct constraint, bmap->n_ineq);
4153 if (!c)
4154 goto error;
4155 for (i = 0; i < bmap->n_ineq; ++i) {
4156 c[i].size = total;
4157 c[i].c = bmap->ineq[i];
4159 qsort(c, bmap->n_ineq, sizeof(struct constraint), qsort_constraint_cmp);
4160 for (i = 0; i < bmap->n_ineq; ++i)
4161 bmap->ineq[i] = c[i].c;
4162 free(c);
4163 return bmap;
4164 error:
4165 isl_basic_map_free(bmap);
4166 return NULL;
4169 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
4171 if (!bmap)
4172 return NULL;
4173 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
4174 return bmap;
4175 bmap = isl_basic_map_convex_hull(bmap);
4176 bmap = isl_basic_map_sort_constraints(bmap);
4177 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
4178 return bmap;
4181 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
4183 return (struct isl_basic_set *)isl_basic_map_normalize(
4184 (struct isl_basic_map *)bset);
4187 static int isl_basic_map_fast_cmp(const struct isl_basic_map *bmap1,
4188 const struct isl_basic_map *bmap2)
4190 int i, cmp;
4191 unsigned total;
4193 if (bmap1 == bmap2)
4194 return 0;
4195 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
4196 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
4197 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
4198 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
4199 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
4200 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
4201 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
4202 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
4203 return 0;
4204 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
4205 return 1;
4206 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
4207 return -1;
4208 if (bmap1->n_eq != bmap2->n_eq)
4209 return bmap1->n_eq - bmap2->n_eq;
4210 if (bmap1->n_ineq != bmap2->n_ineq)
4211 return bmap1->n_ineq - bmap2->n_ineq;
4212 if (bmap1->n_div != bmap2->n_div)
4213 return bmap1->n_div - bmap2->n_div;
4214 total = isl_basic_map_total_dim(bmap1);
4215 for (i = 0; i < bmap1->n_eq; ++i) {
4216 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
4217 if (cmp)
4218 return cmp;
4220 for (i = 0; i < bmap1->n_ineq; ++i) {
4221 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
4222 if (cmp)
4223 return cmp;
4225 for (i = 0; i < bmap1->n_div; ++i) {
4226 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
4227 if (cmp)
4228 return cmp;
4230 return 0;
4233 static int isl_basic_map_fast_is_equal(struct isl_basic_map *bmap1,
4234 struct isl_basic_map *bmap2)
4236 return isl_basic_map_fast_cmp(bmap1, bmap2) == 0;
4239 static int qsort_bmap_cmp(const void *p1, const void *p2)
4241 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
4242 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
4244 return isl_basic_map_fast_cmp(bmap1, bmap2);
4247 /* We normalize in place, but if anything goes wrong we need
4248 * to return NULL, so we need to make sure we don't change the
4249 * meaning of any possible other copies of map.
4251 struct isl_map *isl_map_normalize(struct isl_map *map)
4253 int i, j;
4254 struct isl_basic_map *bmap;
4256 if (!map)
4257 return NULL;
4258 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
4259 return map;
4260 for (i = 0; i < map->n; ++i) {
4261 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
4262 if (!bmap)
4263 goto error;
4264 isl_basic_map_free(map->p[i]);
4265 map->p[i] = bmap;
4267 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
4268 ISL_F_SET(map, ISL_MAP_NORMALIZED);
4269 map = isl_map_remove_empty_parts(map);
4270 if (!map)
4271 return NULL;
4272 for (i = map->n - 1; i >= 1; --i) {
4273 if (!isl_basic_map_fast_is_equal(map->p[i-1], map->p[i]))
4274 continue;
4275 isl_basic_map_free(map->p[i-1]);
4276 for (j = i; j < map->n; ++j)
4277 map->p[j-1] = map->p[j];
4278 map->n--;
4280 return map;
4281 error:
4282 isl_map_free(map);
4283 return NULL;
4287 struct isl_set *isl_set_normalize(struct isl_set *set)
4289 return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
4292 int isl_map_fast_is_equal(struct isl_map *map1, struct isl_map *map2)
4294 int i;
4295 int equal;
4297 if (!map1 || !map2)
4298 return -1;
4300 if (map1 == map2)
4301 return 1;
4302 if (!isl_dim_equal(map1->dim, map2->dim))
4303 return 0;
4305 map1 = isl_map_copy(map1);
4306 map2 = isl_map_copy(map2);
4307 map1 = isl_map_normalize(map1);
4308 map2 = isl_map_normalize(map2);
4309 if (!map1 || !map2)
4310 goto error;
4311 equal = map1->n == map2->n;
4312 for (i = 0; equal && i < map1->n; ++i) {
4313 equal = isl_basic_map_fast_is_equal(map1->p[i], map2->p[i]);
4314 if (equal < 0)
4315 goto error;
4317 isl_map_free(map1);
4318 isl_map_free(map2);
4319 return equal;
4320 error:
4321 isl_map_free(map1);
4322 isl_map_free(map2);
4323 return -1;
4326 int isl_set_fast_is_equal(struct isl_set *set1, struct isl_set *set2)
4328 return isl_map_fast_is_equal((struct isl_map *)set1,
4329 (struct isl_map *)set2);
4332 /* Return an interval that ranges from min to max (inclusive)
4334 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
4335 isl_int min, isl_int max)
4337 int k;
4338 struct isl_basic_set *bset = NULL;
4340 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
4341 if (!bset)
4342 goto error;
4344 k = isl_basic_set_alloc_inequality(bset);
4345 if (k < 0)
4346 goto error;
4347 isl_int_set_si(bset->ineq[k][1], 1);
4348 isl_int_neg(bset->ineq[k][0], min);
4350 k = isl_basic_set_alloc_inequality(bset);
4351 if (k < 0)
4352 goto error;
4353 isl_int_set_si(bset->ineq[k][1], -1);
4354 isl_int_set(bset->ineq[k][0], max);
4356 return bset;
4357 error:
4358 isl_basic_set_free(bset);
4359 return NULL;
4362 /* Return the Cartesian product of the basic sets in list (in the given order).
4364 struct isl_basic_set *isl_basic_set_product(struct isl_basic_set_list *list)
4366 int i;
4367 unsigned dim;
4368 unsigned nparam;
4369 unsigned extra;
4370 unsigned n_eq;
4371 unsigned n_ineq;
4372 struct isl_basic_set *product = NULL;
4374 if (!list)
4375 goto error;
4376 isl_assert(list->ctx, list->n > 0, goto error);
4377 isl_assert(list->ctx, list->p[0], goto error);
4378 nparam = isl_basic_set_n_param(list->p[0]);
4379 dim = isl_basic_set_n_dim(list->p[0]);
4380 extra = list->p[0]->n_div;
4381 n_eq = list->p[0]->n_eq;
4382 n_ineq = list->p[0]->n_ineq;
4383 for (i = 1; i < list->n; ++i) {
4384 isl_assert(list->ctx, list->p[i], goto error);
4385 isl_assert(list->ctx,
4386 nparam == isl_basic_set_n_param(list->p[i]), goto error);
4387 dim += isl_basic_set_n_dim(list->p[i]);
4388 extra += list->p[i]->n_div;
4389 n_eq += list->p[i]->n_eq;
4390 n_ineq += list->p[i]->n_ineq;
4392 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
4393 n_eq, n_ineq);
4394 if (!product)
4395 goto error;
4396 dim = 0;
4397 for (i = 0; i < list->n; ++i) {
4398 isl_basic_set_add_constraints(product,
4399 isl_basic_set_copy(list->p[i]), dim);
4400 dim += isl_basic_set_n_dim(list->p[i]);
4402 isl_basic_set_list_free(list);
4403 return product;
4404 error:
4405 isl_basic_set_free(product);
4406 isl_basic_set_list_free(list);
4407 return NULL;
4410 struct isl_basic_map *isl_basic_map_product(
4411 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4413 struct isl_dim *dim_result = NULL;
4414 struct isl_basic_map *bmap;
4415 unsigned in1, in2, out1, out2, nparam, total, pos;
4416 struct isl_dim_map *dim_map1, *dim_map2;
4418 if (!bmap1 || !bmap2)
4419 goto error;
4421 isl_assert(map1->ctx, isl_dim_match(bmap1->dim, isl_dim_param,
4422 bmap2->dim, isl_dim_param), goto error);
4423 dim_result = isl_dim_product(isl_dim_copy(bmap1->dim),
4424 isl_dim_copy(bmap2->dim));
4426 in1 = isl_basic_map_n_in(bmap1);
4427 in2 = isl_basic_map_n_in(bmap2);
4428 out1 = isl_basic_map_n_out(bmap1);
4429 out2 = isl_basic_map_n_out(bmap2);
4430 nparam = isl_basic_map_n_param(bmap1);
4432 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
4433 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4434 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
4435 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4436 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
4437 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4438 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
4439 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
4440 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
4441 isl_dim_map_div(dim_map1, bmap1, pos += out2);
4442 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4444 bmap = isl_basic_map_alloc_dim(dim_result,
4445 bmap1->n_div + bmap2->n_div,
4446 bmap1->n_eq + bmap2->n_eq,
4447 bmap1->n_ineq + bmap2->n_ineq);
4448 bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
4449 bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
4450 bmap = isl_basic_map_simplify(bmap);
4451 return isl_basic_map_finalize(bmap);
4452 error:
4453 isl_basic_map_free(bmap1);
4454 isl_basic_map_free(bmap2);
4455 return NULL;
4458 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
4460 struct isl_map *isl_map_product(struct isl_map *map1, struct isl_map *map2)
4462 unsigned flags = 0;
4463 struct isl_map *result;
4464 int i, j;
4466 if (!map1 || !map2)
4467 goto error;
4469 isl_assert(map1->ctx, isl_dim_match(map1->dim, isl_dim_param,
4470 map2->dim, isl_dim_param), goto error);
4472 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
4473 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
4474 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
4476 result = isl_map_alloc_dim(isl_dim_product(isl_dim_copy(map1->dim),
4477 isl_dim_copy(map2->dim)),
4478 map1->n * map2->n, flags);
4479 if (!result)
4480 goto error;
4481 for (i = 0; i < map1->n; ++i)
4482 for (j = 0; j < map2->n; ++j) {
4483 struct isl_basic_map *part;
4484 part = isl_basic_map_product(
4485 isl_basic_map_copy(map1->p[i]),
4486 isl_basic_map_copy(map2->p[j]));
4487 if (isl_basic_map_is_empty(part))
4488 isl_basic_map_free(part);
4489 else
4490 result = isl_map_add(result, part);
4491 if (!result)
4492 goto error;
4494 isl_map_free(map1);
4495 isl_map_free(map2);
4496 return result;
4497 error:
4498 isl_map_free(map1);
4499 isl_map_free(map2);
4500 return NULL;
4503 uint32_t isl_basic_set_get_hash(struct isl_basic_set *bset)
4505 int i;
4506 uint32_t hash;
4507 unsigned total;
4509 if (!bset)
4510 return 0;
4511 bset = isl_basic_set_copy(bset);
4512 bset = isl_basic_set_normalize(bset);
4513 if (!bset)
4514 return 0;
4515 total = isl_basic_set_total_dim(bset);
4516 isl_hash_byte(hash, bset->n_eq & 0xFF);
4517 for (i = 0; i < bset->n_eq; ++i) {
4518 uint32_t c_hash;
4519 c_hash = isl_seq_get_hash(bset->eq[i], 1 + total);
4520 isl_hash_hash(hash, c_hash);
4522 isl_hash_byte(hash, bset->n_ineq & 0xFF);
4523 for (i = 0; i < bset->n_ineq; ++i) {
4524 uint32_t c_hash;
4525 c_hash = isl_seq_get_hash(bset->ineq[i], 1 + total);
4526 isl_hash_hash(hash, c_hash);
4528 isl_hash_byte(hash, bset->n_div & 0xFF);
4529 for (i = 0; i < bset->n_div; ++i) {
4530 uint32_t c_hash;
4531 if (isl_int_is_zero(bset->div[i][0]))
4532 continue;
4533 isl_hash_byte(hash, i & 0xFF);
4534 c_hash = isl_seq_get_hash(bset->div[i], 1 + 1 + total);
4535 isl_hash_hash(hash, c_hash);
4537 isl_basic_set_free(bset);
4538 return hash;
4541 uint32_t isl_set_get_hash(struct isl_set *set)
4543 int i;
4544 uint32_t hash;
4546 if (!set)
4547 return 0;
4548 set = isl_set_copy(set);
4549 set = isl_set_normalize(set);
4550 if (!set)
4551 return 0;
4553 hash = isl_hash_init();
4554 for (i = 0; i < set->n; ++i) {
4555 uint32_t bset_hash;
4556 bset_hash = isl_basic_set_get_hash(set->p[i]);
4557 isl_hash_hash(hash, bset_hash);
4560 isl_set_free(set);
4562 return hash;
4565 /* Check if the value for dimension dim is completely determined
4566 * by the values of the other parameters and variables.
4567 * That is, check if dimension dim is involved in an equality.
4569 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
4571 int i;
4572 unsigned nparam;
4574 if (!bset)
4575 return -1;
4576 nparam = isl_basic_set_n_param(bset);
4577 for (i = 0; i < bset->n_eq; ++i)
4578 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
4579 return 1;
4580 return 0;
4583 /* Check if the value for dimension dim is completely determined
4584 * by the values of the other parameters and variables.
4585 * That is, check if dimension dim is involved in an equality
4586 * for each of the subsets.
4588 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
4590 int i;
4592 if (!set)
4593 return -1;
4594 for (i = 0; i < set->n; ++i) {
4595 int unique;
4596 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
4597 if (unique != 1)
4598 return unique;
4600 return 1;