11 #include "isl_map_private.h"
12 #include "isl_map_piplib.h"
13 #include "isl_sample.h"
16 /* Maps dst positions to src positions */
22 static struct isl_dim_map *isl_dim_map_alloc(struct isl_ctx *ctx, unsigned len)
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));
30 dim_map->len = 1 + len;
32 for (i = 0; i < len; ++i)
33 dim_map->pos[1 + i] = -1;
37 static unsigned n(struct isl_dim *dim, enum isl_dim_type 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)
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)
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)
75 if (!dim_map || !bmap)
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)
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;
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;
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)
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 struct isl_dim *isl_basic_set_get_dim(struct isl_basic_set *bset)
227 return isl_dim_copy(bset->dim);
230 struct isl_dim *isl_set_get_dim(struct isl_set *set)
234 return isl_dim_copy(set->dim);
237 static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
238 struct isl_basic_map *bmap, unsigned extra,
239 unsigned n_eq, unsigned n_ineq)
242 size_t row_size = 1 + isl_dim_total(bmap->dim) + extra;
244 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
245 if (isl_blk_is_error(bmap->block)) {
250 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
252 isl_blk_free(ctx, bmap->block);
258 bmap->block2 = isl_blk_empty();
261 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
262 if (isl_blk_is_error(bmap->block2)) {
264 isl_blk_free(ctx, bmap->block);
269 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
271 isl_blk_free(ctx, bmap->block2);
273 isl_blk_free(ctx, bmap->block);
279 for (i = 0; i < n_ineq + n_eq; ++i)
280 bmap->ineq[i] = bmap->block.data + i * row_size;
282 for (i = 0; i < extra; ++i)
283 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
289 bmap->c_size = n_eq + n_ineq;
290 bmap->eq = bmap->ineq + n_ineq;
299 isl_basic_map_free(bmap);
303 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
304 unsigned nparam, unsigned dim, unsigned extra,
305 unsigned n_eq, unsigned n_ineq)
307 struct isl_basic_map *bmap;
308 bmap = isl_basic_map_alloc(ctx, nparam, 0, dim, extra, n_eq, n_ineq);
309 return (struct isl_basic_set *)bmap;
312 struct isl_basic_set *isl_basic_set_alloc_dim(struct isl_dim *dim,
313 unsigned extra, unsigned n_eq, unsigned n_ineq)
315 struct isl_basic_map *bmap;
318 isl_assert(dim->ctx, dim->n_in == 0, return NULL);
319 bmap = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
320 return (struct isl_basic_set *)bmap;
323 struct isl_basic_map *isl_basic_map_alloc_dim(struct isl_dim *dim,
324 unsigned extra, unsigned n_eq, unsigned n_ineq)
326 struct isl_basic_map *bmap;
330 bmap = isl_alloc_type(dim->ctx, struct isl_basic_map);
335 return basic_map_init(dim->ctx, bmap, extra, n_eq, n_ineq);
341 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
342 unsigned nparam, unsigned in, unsigned out, unsigned extra,
343 unsigned n_eq, unsigned n_ineq)
345 struct isl_basic_map *bmap;
348 dim = isl_dim_alloc(ctx, nparam, in, out);
352 bmap = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
356 static void dup_constraints(
357 struct isl_basic_map *dst, struct isl_basic_map *src)
360 unsigned total = isl_basic_map_total_dim(src);
362 for (i = 0; i < src->n_eq; ++i) {
363 int j = isl_basic_map_alloc_equality(dst);
364 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
367 for (i = 0; i < src->n_ineq; ++i) {
368 int j = isl_basic_map_alloc_inequality(dst);
369 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
372 for (i = 0; i < src->n_div; ++i) {
373 int j = isl_basic_map_alloc_div(dst);
374 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
376 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
379 struct isl_basic_map *isl_basic_map_dup(struct isl_basic_map *bmap)
381 struct isl_basic_map *dup;
385 dup = isl_basic_map_alloc_dim(isl_dim_copy(bmap->dim),
386 bmap->n_div, bmap->n_eq, bmap->n_ineq);
389 dup_constraints(dup, bmap);
390 dup->flags = bmap->flags;
391 dup->sample = isl_vec_copy(bmap->sample);
395 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
397 struct isl_basic_map *dup;
399 dup = isl_basic_map_dup((struct isl_basic_map *)bset);
400 return (struct isl_basic_set *)dup;
403 struct isl_basic_set *isl_basic_set_copy(struct isl_basic_set *bset)
408 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
412 return isl_basic_set_dup(bset);
415 struct isl_set *isl_set_copy(struct isl_set *set)
424 struct isl_basic_map *isl_basic_map_copy(struct isl_basic_map *bmap)
429 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
433 return isl_basic_map_dup(bmap);
436 struct isl_map *isl_map_copy(struct isl_map *map)
445 void isl_basic_map_free(struct isl_basic_map *bmap)
453 isl_ctx_deref(bmap->ctx);
455 isl_blk_free(bmap->ctx, bmap->block2);
457 isl_blk_free(bmap->ctx, bmap->block);
458 isl_vec_free(bmap->sample);
459 isl_dim_free(bmap->dim);
463 void isl_basic_set_free(struct isl_basic_set *bset)
465 isl_basic_map_free((struct isl_basic_map *)bset);
468 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
470 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
473 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
479 isl_assert(ctx, room_for_con(bmap, 1), return -1);
480 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
482 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
483 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
484 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
485 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
486 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
487 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
489 int j = isl_basic_map_alloc_inequality(bmap);
493 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
494 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
501 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
502 bmap->extra - bmap->n_div);
506 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
508 return isl_basic_map_alloc_equality((struct isl_basic_map *)bset);
511 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
515 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
520 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
522 return isl_basic_map_free_equality((struct isl_basic_map *)bset, n);
525 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
530 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
532 if (pos != bmap->n_eq - 1) {
534 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
535 bmap->eq[bmap->n_eq - 1] = t;
541 int isl_basic_set_drop_equality(struct isl_basic_set *bset, unsigned pos)
543 return isl_basic_map_drop_equality((struct isl_basic_map *)bset, pos);
546 void isl_basic_map_inequality_to_equality(
547 struct isl_basic_map *bmap, unsigned pos)
552 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
553 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
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_NORMALIZED_DIVS);
561 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
564 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
566 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
569 int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap)
575 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
576 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
577 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
578 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
579 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
580 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
581 1 + isl_basic_map_total_dim(bmap),
582 bmap->extra - bmap->n_div);
583 return bmap->n_ineq++;
586 int isl_basic_set_alloc_inequality(struct isl_basic_set *bset)
588 return isl_basic_map_alloc_inequality((struct isl_basic_map *)bset);
591 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
595 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
600 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
602 return isl_basic_map_free_inequality((struct isl_basic_map *)bset, n);
605 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
610 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
612 if (pos != bmap->n_ineq - 1) {
614 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
615 bmap->ineq[bmap->n_ineq - 1] = t;
616 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
622 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
624 return isl_basic_map_drop_inequality((struct isl_basic_map *)bset, pos);
627 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
631 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
632 isl_seq_clr(bmap->div[bmap->n_div] +
633 1 + 1 + isl_basic_map_total_dim(bmap),
634 bmap->extra - bmap->n_div);
635 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
636 return bmap->n_div++;
639 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
641 return isl_basic_map_alloc_div((struct isl_basic_map *)bset);
644 int isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
648 isl_assert(bmap->ctx, n <= bmap->n_div, return -1);
653 /* Copy constraint from src to dst, putting the vars of src at offset
654 * dim_off in dst and the divs of src at offset div_off in dst.
655 * If both sets are actually map, then dim_off applies to the input
658 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
659 struct isl_basic_map *src_map, isl_int *src,
660 unsigned in_off, unsigned out_off, unsigned div_off)
662 unsigned src_nparam = isl_basic_map_n_param(src_map);
663 unsigned dst_nparam = isl_basic_map_n_param(dst_map);
664 unsigned src_in = isl_basic_map_n_in(src_map);
665 unsigned dst_in = isl_basic_map_n_in(dst_map);
666 unsigned src_out = isl_basic_map_n_out(src_map);
667 unsigned dst_out = isl_basic_map_n_out(dst_map);
668 isl_int_set(dst[0], src[0]);
669 isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
670 if (dst_nparam > src_nparam)
671 isl_seq_clr(dst+1+src_nparam,
672 dst_nparam - src_nparam);
673 isl_seq_clr(dst+1+dst_nparam, in_off);
674 isl_seq_cpy(dst+1+dst_nparam+in_off,
676 isl_min(dst_in-in_off, src_in));
677 if (dst_in-in_off > src_in)
678 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
679 dst_in - in_off - src_in);
680 isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
681 isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
682 src+1+src_nparam+src_in,
683 isl_min(dst_out-out_off, src_out));
684 if (dst_out-out_off > src_out)
685 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
686 dst_out - out_off - src_out);
687 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
688 isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
689 src+1+src_nparam+src_in+src_out,
690 isl_min(dst_map->extra-div_off, src_map->n_div));
691 if (dst_map->n_div-div_off > src_map->n_div)
692 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
693 div_off+src_map->n_div,
694 dst_map->n_div - div_off - src_map->n_div);
697 static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
698 struct isl_basic_map *src_map, isl_int *src,
699 unsigned in_off, unsigned out_off, unsigned div_off)
701 isl_int_set(dst[0], src[0]);
702 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
705 static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
706 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
711 if (!bmap1 || !bmap2)
714 div_off = bmap1->n_div;
716 for (i = 0; i < bmap2->n_eq; ++i) {
717 int i1 = isl_basic_map_alloc_equality(bmap1);
720 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
721 i_pos, o_pos, div_off);
724 for (i = 0; i < bmap2->n_ineq; ++i) {
725 int i1 = isl_basic_map_alloc_inequality(bmap1);
728 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
729 i_pos, o_pos, div_off);
732 for (i = 0; i < bmap2->n_div; ++i) {
733 int i1 = isl_basic_map_alloc_div(bmap1);
736 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
737 i_pos, o_pos, div_off);
740 isl_basic_map_free(bmap2);
745 isl_basic_map_free(bmap1);
746 isl_basic_map_free(bmap2);
750 static void copy_constraint_dim_map(isl_int *dst, isl_int *src,
751 struct isl_dim_map *dim_map)
755 for (i = 0; i < dim_map->len; ++i) {
756 if (dim_map->pos[i] < 0)
757 isl_int_set_si(dst[i], 0);
759 isl_int_set(dst[i], src[dim_map->pos[i]]);
763 static void copy_div_dim_map(isl_int *dst, isl_int *src,
764 struct isl_dim_map *dim_map)
766 isl_int_set(dst[0], src[0]);
767 copy_constraint_dim_map(dst+1, src+1, dim_map);
770 static struct isl_basic_map *add_constraints_dim_map(struct isl_basic_map *dst,
771 struct isl_basic_map *src, struct isl_dim_map *dim_map)
775 if (!src || !dst || !dim_map)
778 for (i = 0; i < src->n_eq; ++i) {
779 int i1 = isl_basic_map_alloc_equality(dst);
782 copy_constraint_dim_map(dst->eq[i1], src->eq[i], dim_map);
785 for (i = 0; i < src->n_ineq; ++i) {
786 int i1 = isl_basic_map_alloc_inequality(dst);
789 copy_constraint_dim_map(dst->ineq[i1], src->ineq[i], dim_map);
792 for (i = 0; i < src->n_div; ++i) {
793 int i1 = isl_basic_map_alloc_div(dst);
796 copy_div_dim_map(dst->div[i1], src->div[i], dim_map);
800 isl_basic_map_free(src);
805 isl_basic_map_free(src);
806 isl_basic_map_free(dst);
810 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
811 struct isl_basic_set *bset2, unsigned pos)
813 return (struct isl_basic_set *)
814 add_constraints((struct isl_basic_map *)bset1,
815 (struct isl_basic_map *)bset2, 0, pos);
818 struct isl_basic_map *isl_basic_map_extend_dim(struct isl_basic_map *base,
819 struct isl_dim *dim, unsigned extra,
820 unsigned n_eq, unsigned n_ineq)
822 struct isl_basic_map *ext;
832 dims_ok = isl_dim_equal(base->dim, dim) &&
833 base->extra >= base->n_div + extra;
835 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
836 room_for_ineq(base, n_ineq)) {
841 isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error);
842 isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error);
843 isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error);
844 extra += base->extra;
846 n_ineq += base->n_ineq;
848 ext = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
854 ext = add_constraints(ext, base, 0, 0);
857 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
864 isl_basic_map_free(base);
868 struct isl_basic_set *isl_basic_set_extend_dim(struct isl_basic_set *base,
869 struct isl_dim *dim, unsigned extra,
870 unsigned n_eq, unsigned n_ineq)
872 return (struct isl_basic_set *)
873 isl_basic_map_extend_dim((struct isl_basic_map *)base, dim,
874 extra, n_eq, n_ineq);
877 struct isl_basic_map *isl_basic_map_extend_constraints(
878 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
882 return isl_basic_map_extend_dim(base, isl_dim_copy(base->dim),
886 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
887 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
888 unsigned n_eq, unsigned n_ineq)
890 struct isl_basic_map *bmap;
895 dim = isl_dim_alloc(base->ctx, nparam, n_in, n_out);
899 bmap = isl_basic_map_extend_dim(base, dim, extra, n_eq, n_ineq);
903 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
904 unsigned nparam, unsigned dim, unsigned extra,
905 unsigned n_eq, unsigned n_ineq)
907 return (struct isl_basic_set *)
908 isl_basic_map_extend((struct isl_basic_map *)base,
909 nparam, 0, dim, extra, n_eq, n_ineq);
912 struct isl_basic_set *isl_basic_set_extend_constraints(
913 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
915 return (struct isl_basic_set *)
916 isl_basic_map_extend_constraints((struct isl_basic_map *)base,
920 struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset)
922 return (struct isl_basic_set *)
923 isl_basic_map_cow((struct isl_basic_map *)bset);
926 struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
933 bmap = isl_basic_map_dup(bmap);
935 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
939 struct isl_set *isl_set_cow(struct isl_set *set)
947 return isl_set_dup(set);
950 struct isl_map *isl_map_cow(struct isl_map *map)
958 return isl_map_dup(map);
961 static void swap_vars(struct isl_blk blk, isl_int *a,
962 unsigned a_len, unsigned b_len)
964 isl_seq_cpy(blk.data, a+a_len, b_len);
965 isl_seq_cpy(blk.data+b_len, a, a_len);
966 isl_seq_cpy(a, blk.data, b_len+a_len);
969 struct isl_basic_set *isl_basic_set_swap_vars(
970 struct isl_basic_set *bset, unsigned n)
980 nparam = isl_basic_set_n_param(bset);
981 dim = isl_basic_set_n_dim(bset);
982 isl_assert(bset->ctx, n <= dim, goto error);
987 bset = isl_basic_set_cow(bset);
991 blk = isl_blk_alloc(bset->ctx, dim);
992 if (isl_blk_is_error(blk))
995 for (i = 0; i < bset->n_eq; ++i)
997 bset->eq[i]+1+nparam, n, dim - n);
999 for (i = 0; i < bset->n_ineq; ++i)
1001 bset->ineq[i]+1+nparam, n, dim - n);
1003 for (i = 0; i < bset->n_div; ++i)
1005 bset->div[i]+1+1+nparam, n, dim - n);
1007 isl_blk_free(bset->ctx, blk);
1009 ISL_F_CLR(bset, ISL_BASIC_SET_NORMALIZED);
1013 isl_basic_set_free(bset);
1017 struct isl_set *isl_set_swap_vars(struct isl_set *set, unsigned n)
1020 set = isl_set_cow(set);
1024 for (i = 0; i < set->n; ++i) {
1025 set->p[i] = isl_basic_set_swap_vars(set->p[i], n);
1031 ISL_F_CLR(set, ISL_SET_NORMALIZED);
1035 struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
1041 total = isl_basic_map_total_dim(bmap);
1042 isl_basic_map_free_div(bmap, bmap->n_div);
1043 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1045 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
1047 isl_basic_map_alloc_equality(bmap);
1051 isl_int_set_si(bmap->eq[i][0], 1);
1052 isl_seq_clr(bmap->eq[i]+1, total);
1053 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
1054 return isl_basic_map_finalize(bmap);
1056 isl_basic_map_free(bmap);
1060 struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
1062 return (struct isl_basic_set *)
1063 isl_basic_map_set_to_empty((struct isl_basic_map *)bset);
1066 static void swap_div(struct isl_basic_map *bmap, int a, int b)
1069 unsigned off = isl_dim_total(bmap->dim);
1070 isl_int *t = bmap->div[a];
1071 bmap->div[a] = bmap->div[b];
1074 for (i = 0; i < bmap->n_eq; ++i)
1075 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
1077 for (i = 0; i < bmap->n_ineq; ++i)
1078 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
1080 for (i = 0; i < bmap->n_div; ++i)
1081 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
1082 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1085 /* Eliminate the specified n dimensions starting at first from the
1086 * constraints using Fourier-Motzkin, The dimensions themselves
1089 struct isl_set *isl_set_eliminate_dims(struct isl_set *set,
1090 unsigned first, unsigned n)
1100 set = isl_set_cow(set);
1103 isl_assert(set->ctx, first+n <= isl_set_n_dim(set), goto error);
1104 nparam = isl_set_n_param(set);
1106 for (i = 0; i < set->n; ++i) {
1107 set->p[i] = isl_basic_set_eliminate_vars(set->p[i],
1118 /* Project out n dimensions starting at first using Fourier-Motzkin */
1119 struct isl_set *isl_set_remove_dims(struct isl_set *set,
1120 unsigned first, unsigned n)
1122 set = isl_set_eliminate_dims(set, first, n);
1123 set = isl_set_drop_dims(set, first, n);
1127 struct isl_basic_set *isl_basic_set_remove_divs(struct isl_basic_set *bset)
1129 bset = isl_basic_set_eliminate_vars(bset, isl_dim_total(bset->dim),
1137 struct isl_set *isl_set_remove_divs(struct isl_set *set)
1146 set = isl_set_cow(set);
1150 for (i = 0; i < set->n; ++i) {
1151 set->p[i] = isl_basic_set_remove_divs(set->p[i]);
1161 struct isl_basic_map *isl_basic_map_remove(struct isl_basic_map *bmap,
1162 enum isl_dim_type type, unsigned first, unsigned n)
1166 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1170 bmap = isl_basic_map_eliminate_vars(bmap,
1171 isl_basic_map_offset(bmap, type) - 1 + first, n);
1172 bmap = isl_basic_map_drop(bmap, type, first, n);
1175 isl_basic_map_free(bmap);
1179 struct isl_map *isl_map_remove(struct isl_map *map,
1180 enum isl_dim_type type, unsigned first, unsigned n)
1188 map = isl_map_cow(map);
1191 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
1193 for (i = 0; i < map->n; ++i) {
1194 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
1195 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
1199 map = isl_map_drop(map, type, first, n);
1206 /* Project out n inputs starting at first using Fourier-Motzkin */
1207 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
1208 unsigned first, unsigned n)
1210 return isl_map_remove(map, isl_dim_in, first, n);
1213 /* Project out n dimensions starting at first using Fourier-Motzkin */
1214 struct isl_basic_set *isl_basic_set_remove_dims(struct isl_basic_set *bset,
1215 unsigned first, unsigned n)
1217 unsigned nparam = isl_basic_set_n_param(bset);
1218 bset = isl_basic_set_eliminate_vars(bset, nparam + first, n);
1219 bset = isl_basic_set_drop_dims(bset, first, n);
1223 static void dump_term(struct isl_basic_map *bmap,
1224 isl_int c, int pos, FILE *out)
1227 unsigned in = isl_basic_map_n_in(bmap);
1228 unsigned dim = in + isl_basic_map_n_out(bmap);
1229 unsigned nparam = isl_basic_map_n_param(bmap);
1231 isl_int_print(out, c, 0);
1233 if (!isl_int_is_one(c))
1234 isl_int_print(out, c, 0);
1235 if (pos < 1 + nparam) {
1236 name = isl_dim_get_name(bmap->dim,
1237 isl_dim_param, pos - 1);
1239 fprintf(out, "%s", name);
1241 fprintf(out, "p%d", pos - 1);
1242 } else if (pos < 1 + nparam + in)
1243 fprintf(out, "i%d", pos - 1 - nparam);
1244 else if (pos < 1 + nparam + dim)
1245 fprintf(out, "o%d", pos - 1 - nparam - in);
1247 fprintf(out, "e%d", pos - 1 - nparam - dim);
1251 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
1252 int sign, FILE *out)
1256 unsigned len = 1 + isl_basic_map_total_dim(bmap);
1260 for (i = 0, first = 1; i < len; ++i) {
1261 if (isl_int_sgn(c[i]) * sign <= 0)
1264 fprintf(out, " + ");
1266 isl_int_abs(v, c[i]);
1267 dump_term(bmap, v, i, out);
1274 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
1275 const char *op, FILE *out, int indent)
1279 fprintf(out, "%*s", indent, "");
1281 dump_constraint_sign(bmap, c, 1, out);
1282 fprintf(out, " %s ", op);
1283 dump_constraint_sign(bmap, c, -1, out);
1287 for (i = bmap->n_div; i < bmap->extra; ++i) {
1288 if (isl_int_is_zero(c[1+isl_dim_total(bmap->dim)+i]))
1290 fprintf(out, "%*s", indent, "");
1291 fprintf(out, "ERROR: unused div coefficient not zero\n");
1296 static void dump_constraints(struct isl_basic_map *bmap,
1297 isl_int **c, unsigned n,
1298 const char *op, FILE *out, int indent)
1302 for (i = 0; i < n; ++i)
1303 dump_constraint(bmap, c[i], op, out, indent);
1306 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
1310 unsigned total = isl_basic_map_total_dim(bmap);
1312 for (j = 0; j < 1 + total; ++j) {
1313 if (isl_int_is_zero(exp[j]))
1315 if (!first && isl_int_is_pos(exp[j]))
1317 dump_term(bmap, exp[j], j, out);
1322 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
1326 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
1327 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
1329 for (i = 0; i < bmap->n_div; ++i) {
1330 fprintf(out, "%*s", indent, "");
1331 fprintf(out, "e%d = [(", i);
1332 dump_affine(bmap, bmap->div[i]+1, out);
1334 isl_int_print(out, bmap->div[i][0], 0);
1335 fprintf(out, "]\n");
1339 void isl_basic_set_dump(struct isl_basic_set *bset, FILE *out, int indent)
1342 fprintf(out, "null basic set\n");
1346 fprintf(out, "%*s", indent, "");
1347 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
1348 bset->ref, bset->dim->nparam, bset->dim->n_out,
1349 bset->extra, bset->flags);
1350 dump((struct isl_basic_map *)bset, out, indent);
1353 void isl_basic_map_dump(struct isl_basic_map *bmap, FILE *out, int indent)
1356 fprintf(out, "null basic map\n");
1360 fprintf(out, "%*s", indent, "");
1361 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
1362 "flags: %x, n_name: %d\n",
1364 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
1365 bmap->extra, bmap->flags, bmap->dim->n_name);
1366 dump(bmap, out, indent);
1369 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
1374 total = isl_basic_map_total_dim(bmap);
1375 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1376 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
1377 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
1378 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1382 struct isl_set *isl_set_alloc_dim(struct isl_dim *dim, int n, unsigned flags)
1384 struct isl_set *set;
1388 isl_assert(dim->ctx, dim->n_in == 0, return NULL);
1389 isl_assert(dim->ctx, n >= 0, return NULL);
1390 set = isl_alloc(dim->ctx, struct isl_set,
1391 sizeof(struct isl_set) +
1392 n * sizeof(struct isl_basic_set *));
1396 set->ctx = dim->ctx;
1397 isl_ctx_ref(set->ctx);
1409 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
1410 unsigned nparam, unsigned dim, int n, unsigned flags)
1412 struct isl_set *set;
1413 struct isl_dim *dims;
1415 dims = isl_dim_alloc(ctx, nparam, 0, dim);
1419 set = isl_set_alloc_dim(dims, n, flags);
1423 struct isl_set *isl_set_dup(struct isl_set *set)
1426 struct isl_set *dup;
1431 dup = isl_set_alloc_dim(isl_dim_copy(set->dim), set->n, set->flags);
1434 for (i = 0; i < set->n; ++i)
1435 dup = isl_set_add(dup, isl_basic_set_copy(set->p[i]));
1439 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
1441 struct isl_set *set;
1446 set = isl_set_alloc_dim(isl_dim_copy(bset->dim), 1, ISL_MAP_DISJOINT);
1448 isl_basic_set_free(bset);
1451 return isl_set_add(set, bset);
1454 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
1456 struct isl_map *map;
1461 map = isl_map_alloc_dim(isl_dim_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
1463 isl_basic_map_free(bmap);
1466 return isl_map_add(map, bmap);
1469 struct isl_set *isl_set_add(struct isl_set *set, struct isl_basic_set *bset)
1473 isl_assert(set->ctx, isl_dim_equal(set->dim, bset->dim), goto error);
1474 isl_assert(set->ctx, set->n < set->size, goto error);
1475 set->p[set->n] = bset;
1482 isl_basic_set_free(bset);
1486 void isl_set_free(struct isl_set *set)
1496 isl_ctx_deref(set->ctx);
1497 for (i = 0; i < set->n; ++i)
1498 isl_basic_set_free(set->p[i]);
1499 isl_dim_free(set->dim);
1503 void isl_set_dump(struct isl_set *set, FILE *out, int indent)
1508 fprintf(out, "null set\n");
1512 fprintf(out, "%*s", indent, "");
1513 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
1514 set->ref, set->n, set->dim->nparam, set->dim->n_out,
1516 for (i = 0; i < set->n; ++i) {
1517 fprintf(out, "%*s", indent, "");
1518 fprintf(out, "basic set %d:\n", i);
1519 isl_basic_set_dump(set->p[i], out, indent+4);
1523 void isl_map_dump(struct isl_map *map, FILE *out, int indent)
1528 fprintf(out, "null map\n");
1532 fprintf(out, "%*s", indent, "");
1533 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
1534 "flags: %x, n_name: %d\n",
1535 map->ref, map->n, map->dim->nparam, map->dim->n_in,
1536 map->dim->n_out, map->flags, map->dim->n_name);
1537 for (i = 0; i < map->n; ++i) {
1538 fprintf(out, "%*s", indent, "");
1539 fprintf(out, "basic map %d:\n", i);
1540 isl_basic_map_dump(map->p[i], out, indent+4);
1544 struct isl_basic_map *isl_basic_map_intersect_domain(
1545 struct isl_basic_map *bmap, struct isl_basic_set *bset)
1547 struct isl_basic_map *bmap_domain;
1548 struct isl_dim *dim;
1553 isl_assert(bset->ctx, isl_dim_match(bmap->dim, isl_dim_param,
1554 bset->dim, isl_dim_param), goto error);
1556 if (isl_dim_size(bset->dim, isl_dim_set) != 0)
1557 isl_assert(bset->ctx,
1558 isl_basic_map_compatible_domain(bmap, bset), goto error);
1560 bmap = isl_basic_map_cow(bmap);
1561 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
1562 bset->n_div, bset->n_eq, bset->n_ineq);
1565 dim = isl_dim_reverse(isl_dim_copy(bset->dim));
1566 bmap_domain = isl_basic_map_from_basic_set(bset, dim);
1567 bmap = add_constraints(bmap, bmap_domain, 0, 0);
1569 bmap = isl_basic_map_simplify(bmap);
1570 return isl_basic_map_finalize(bmap);
1572 isl_basic_map_free(bmap);
1573 isl_basic_set_free(bset);
1577 struct isl_basic_map *isl_basic_map_intersect_range(
1578 struct isl_basic_map *bmap, struct isl_basic_set *bset)
1580 struct isl_basic_map *bmap_range;
1585 isl_assert(bset->ctx, isl_dim_match(bmap->dim, isl_dim_param,
1586 bset->dim, isl_dim_param), goto error);
1588 if (isl_dim_size(bset->dim, isl_dim_set) != 0)
1589 isl_assert(bset->ctx,
1590 isl_basic_map_compatible_range(bmap, bset), goto error);
1592 bmap = isl_basic_map_cow(bmap);
1593 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
1594 bset->n_div, bset->n_eq, bset->n_ineq);
1597 bmap_range = isl_basic_map_from_basic_set(bset, isl_dim_copy(bset->dim));
1598 bmap = add_constraints(bmap, bmap_range, 0, 0);
1600 bmap = isl_basic_map_simplify(bmap);
1601 return isl_basic_map_finalize(bmap);
1603 isl_basic_map_free(bmap);
1604 isl_basic_set_free(bset);
1608 static int basic_map_contains(struct isl_basic_map *bmap, struct isl_vec *vec)
1614 total = 1 + isl_basic_map_total_dim(bmap);
1615 if (total != vec->size)
1620 for (i = 0; i < bmap->n_eq; ++i) {
1621 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
1622 if (!isl_int_is_zero(s)) {
1628 for (i = 0; i < bmap->n_ineq; ++i) {
1629 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
1630 if (isl_int_is_neg(s)) {
1641 int isl_basic_set_contains(struct isl_basic_set *bset, struct isl_vec *vec)
1643 return basic_map_contains((struct isl_basic_map *)bset, vec);
1646 struct isl_basic_map *isl_basic_map_intersect(
1647 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
1649 struct isl_vec *sample = NULL;
1651 if (!bmap1 || !bmap2)
1654 isl_assert(bmap1->ctx, isl_dim_match(bmap1->dim, isl_dim_param,
1655 bmap2->dim, isl_dim_param), goto error);
1656 if (isl_dim_total(bmap1->dim) ==
1657 isl_dim_size(bmap1->dim, isl_dim_param) &&
1658 isl_dim_total(bmap2->dim) !=
1659 isl_dim_size(bmap2->dim, isl_dim_param))
1660 return isl_basic_map_intersect(bmap2, bmap1);
1662 if (isl_dim_total(bmap2->dim) !=
1663 isl_dim_size(bmap2->dim, isl_dim_param))
1664 isl_assert(bmap1->ctx,
1665 isl_dim_equal(bmap1->dim, bmap2->dim), goto error);
1667 if (bmap1->sample &&
1668 basic_map_contains(bmap1, bmap1->sample) > 0 &&
1669 basic_map_contains(bmap2, bmap1->sample) > 0)
1670 sample = isl_vec_copy(bmap1->sample);
1671 else if (bmap2->sample &&
1672 basic_map_contains(bmap1, bmap2->sample) > 0 &&
1673 basic_map_contains(bmap2, bmap2->sample) > 0)
1674 sample = isl_vec_copy(bmap2->sample);
1676 bmap1 = isl_basic_map_cow(bmap1);
1677 bmap1 = isl_basic_map_extend_dim(bmap1, isl_dim_copy(bmap1->dim),
1678 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
1681 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
1684 isl_vec_free(bmap1->sample);
1685 bmap1->sample = sample;
1688 bmap1 = isl_basic_map_simplify(bmap1);
1689 return isl_basic_map_finalize(bmap1);
1692 isl_vec_free(sample);
1693 isl_basic_map_free(bmap1);
1694 isl_basic_map_free(bmap2);
1698 struct isl_basic_set *isl_basic_set_intersect(
1699 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
1701 return (struct isl_basic_set *)
1702 isl_basic_map_intersect(
1703 (struct isl_basic_map *)bset1,
1704 (struct isl_basic_map *)bset2);
1707 struct isl_map *isl_map_intersect(struct isl_map *map1, struct isl_map *map2)
1710 struct isl_map *result;
1716 isl_assert(map1->ctx, isl_dim_match(map1->dim, isl_dim_param,
1717 map2->dim, isl_dim_param), goto error);
1718 if (isl_dim_total(map1->dim) ==
1719 isl_dim_size(map1->dim, isl_dim_param) &&
1720 isl_dim_total(map2->dim) != isl_dim_size(map2->dim, isl_dim_param))
1721 return isl_map_intersect(map2, map1);
1723 if (isl_dim_total(map2->dim) != isl_dim_size(map2->dim, isl_dim_param))
1724 isl_assert(map1->ctx,
1725 isl_dim_equal(map1->dim, map2->dim), goto error);
1727 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
1728 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
1729 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
1731 result = isl_map_alloc_dim(isl_dim_copy(map1->dim),
1732 map1->n * map2->n, flags);
1735 for (i = 0; i < map1->n; ++i)
1736 for (j = 0; j < map2->n; ++j) {
1737 struct isl_basic_map *part;
1738 part = isl_basic_map_intersect(
1739 isl_basic_map_copy(map1->p[i]),
1740 isl_basic_map_copy(map2->p[j]));
1741 if (isl_basic_map_is_empty(part))
1742 isl_basic_map_free(part);
1744 result = isl_map_add(result, part);
1757 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
1759 return (struct isl_set *)
1760 isl_map_intersect((struct isl_map *)set1,
1761 (struct isl_map *)set2);
1764 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
1766 struct isl_dim *dim;
1767 struct isl_basic_set *bset;
1772 bmap = isl_basic_map_cow(bmap);
1775 dim = isl_dim_reverse(isl_dim_copy(bmap->dim));
1776 in = isl_basic_map_n_in(bmap);
1777 bset = isl_basic_set_from_basic_map(bmap);
1778 bset = isl_basic_set_swap_vars(bset, in);
1779 return isl_basic_map_from_basic_set(bset, dim);
1782 /* Turn final n dimensions into existentially quantified variables.
1784 struct isl_basic_set *isl_basic_set_project_out(
1785 struct isl_basic_set *bset, unsigned n, unsigned flags)
1795 isl_assert(bset->ctx, n <= isl_basic_set_n_dim(bset), goto error);
1800 bset = isl_basic_set_cow(bset);
1802 row_size = 1 + isl_dim_total(bset->dim) + bset->extra;
1803 old = bset->block2.data;
1804 bset->block2 = isl_blk_extend(bset->ctx, bset->block2,
1805 (bset->extra + n) * (1 + row_size));
1806 if (!bset->block2.data)
1808 new_div = isl_alloc_array(ctx, isl_int *, bset->extra + n);
1811 for (i = 0; i < n; ++i) {
1812 new_div[i] = bset->block2.data +
1813 (bset->extra + i) * (1 + row_size);
1814 isl_seq_clr(new_div[i], 1 + row_size);
1816 for (i = 0; i < bset->extra; ++i)
1817 new_div[n + i] = bset->block2.data + (bset->div[i] - old);
1819 bset->div = new_div;
1822 bset->dim = isl_dim_drop_outputs(bset->dim,
1823 isl_basic_set_n_dim(bset) - n, n);
1826 bset = isl_basic_set_simplify(bset);
1827 return isl_basic_set_finalize(bset);
1829 isl_basic_set_free(bset);
1833 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
1837 for (i = 0; i < n; ++i) {
1838 j = isl_basic_map_alloc_div(bmap);
1841 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
1845 isl_basic_map_free(bmap);
1849 struct isl_basic_map *isl_basic_map_apply_range(
1850 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
1852 struct isl_dim *dim_result = NULL;
1853 struct isl_basic_map *bmap;
1854 unsigned n_in, n_out, n, nparam, total, pos;
1855 struct isl_dim_map *dim_map1, *dim_map2;
1857 if (!bmap1 || !bmap2)
1860 dim_result = isl_dim_join(isl_dim_copy(bmap1->dim),
1861 isl_dim_copy(bmap2->dim));
1863 n_in = isl_basic_map_n_in(bmap1);
1864 n_out = isl_basic_map_n_out(bmap2);
1865 n = isl_basic_map_n_out(bmap1);
1866 nparam = isl_basic_map_n_param(bmap1);
1868 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
1869 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
1870 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
1871 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
1872 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
1873 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
1874 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
1875 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
1876 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
1877 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
1878 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
1880 bmap = isl_basic_map_alloc_dim(dim_result,
1881 bmap1->n_div + bmap2->n_div + n,
1882 bmap1->n_eq + bmap2->n_eq,
1883 bmap1->n_ineq + bmap2->n_ineq);
1884 bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
1885 bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
1886 bmap = add_divs(bmap, n);
1887 bmap = isl_basic_map_simplify(bmap);
1888 return isl_basic_map_finalize(bmap);
1890 isl_basic_map_free(bmap1);
1891 isl_basic_map_free(bmap2);
1895 struct isl_basic_set *isl_basic_set_apply(
1896 struct isl_basic_set *bset, struct isl_basic_map *bmap)
1901 isl_assert(set->ctx, isl_basic_map_compatible_domain(bmap, bset),
1904 return (struct isl_basic_set *)
1905 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
1907 isl_basic_set_free(bset);
1908 isl_basic_map_free(bmap);
1912 struct isl_basic_map *isl_basic_map_apply_domain(
1913 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
1915 if (!bmap1 || !bmap2)
1919 isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
1921 isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
1924 bmap1 = isl_basic_map_reverse(bmap1);
1925 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
1926 return isl_basic_map_reverse(bmap1);
1928 isl_basic_map_free(bmap1);
1929 isl_basic_map_free(bmap2);
1933 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
1934 * A \cap B -> f(A) + f(B)
1936 struct isl_basic_map *isl_basic_map_sum(
1937 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
1939 unsigned n_in, n_out, nparam, total, pos;
1940 struct isl_basic_map *bmap = NULL;
1941 struct isl_dim_map *dim_map1, *dim_map2;
1944 if (!bmap1 || !bmap2)
1947 isl_assert(bmap1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim),
1950 nparam = isl_basic_map_n_param(bmap1);
1951 n_in = isl_basic_map_n_in(bmap1);
1952 n_out = isl_basic_map_n_out(bmap1);
1954 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
1955 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
1956 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
1957 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
1958 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
1959 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
1960 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
1961 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
1962 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
1963 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
1964 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
1966 bmap = isl_basic_map_alloc_dim(isl_dim_copy(bmap1->dim),
1967 bmap1->n_div + bmap2->n_div + 2 * n_out,
1968 bmap1->n_eq + bmap2->n_eq + n_out,
1969 bmap1->n_ineq + bmap2->n_ineq);
1970 for (i = 0; i < n_out; ++i) {
1971 int j = isl_basic_map_alloc_equality(bmap);
1974 isl_seq_clr(bmap->eq[j], 1+total);
1975 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
1976 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
1977 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
1979 bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
1980 bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
1981 bmap = add_divs(bmap, 2 * n_out);
1983 bmap = isl_basic_map_simplify(bmap);
1984 return isl_basic_map_finalize(bmap);
1986 isl_basic_map_free(bmap);
1987 isl_basic_map_free(bmap1);
1988 isl_basic_map_free(bmap2);
1992 /* Given a basic map A -> f(A), construct A -> -f(A).
1994 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
1999 bmap = isl_basic_map_cow(bmap);
2003 n = isl_basic_map_dim(bmap, isl_dim_out);
2004 off = isl_basic_map_offset(bmap, isl_dim_out);
2005 for (i = 0; i < bmap->n_eq; ++i)
2006 for (j = 0; j < n; ++j)
2007 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
2008 for (i = 0; i < bmap->n_ineq; ++i)
2009 for (j = 0; j < n; ++j)
2010 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
2011 for (i = 0; i < bmap->n_div; ++i)
2012 for (j = 0; j < n; ++j)
2013 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
2014 return isl_basic_map_finalize(bmap);
2017 /* Given a basic map A -> f(A) and an integer d, construct a basic map
2018 * A -> floor(f(A)/d).
2020 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
2023 unsigned n_in, n_out, nparam, total, pos;
2024 struct isl_basic_map *result = NULL;
2025 struct isl_dim_map *dim_map;
2031 nparam = isl_basic_map_n_param(bmap);
2032 n_in = isl_basic_map_n_in(bmap);
2033 n_out = isl_basic_map_n_out(bmap);
2035 total = nparam + n_in + n_out + bmap->n_div + n_out;
2036 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2037 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
2038 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
2039 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
2040 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
2042 result = isl_basic_map_alloc_dim(isl_dim_copy(bmap->dim),
2043 bmap->n_div + n_out,
2044 bmap->n_eq, bmap->n_ineq + 2 * n_out);
2045 result = add_constraints_dim_map(result, bmap, dim_map);
2046 result = add_divs(result, n_out);
2047 for (i = 0; i < n_out; ++i) {
2049 j = isl_basic_map_alloc_inequality(result);
2052 isl_seq_clr(result->ineq[j], 1+total);
2053 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
2054 isl_int_set_si(result->ineq[j][1+pos+i], 1);
2055 j = isl_basic_map_alloc_inequality(result);
2058 isl_seq_clr(result->ineq[j], 1+total);
2059 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
2060 isl_int_set_si(result->ineq[j][1+pos+i], -1);
2061 isl_int_sub_ui(result->ineq[j][0], d, 1);
2064 result = isl_basic_map_simplify(result);
2065 return isl_basic_map_finalize(result);
2067 isl_basic_map_free(result);
2071 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
2077 i = isl_basic_map_alloc_equality(bmap);
2080 nparam = isl_basic_map_n_param(bmap);
2081 n_in = isl_basic_map_n_in(bmap);
2082 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
2083 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
2084 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
2085 return isl_basic_map_finalize(bmap);
2087 isl_basic_map_free(bmap);
2091 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
2097 i = isl_basic_map_alloc_inequality(bmap);
2100 nparam = isl_basic_map_n_param(bmap);
2101 n_in = isl_basic_map_n_in(bmap);
2102 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
2103 isl_int_set_si(bmap->ineq[i][0], -1);
2104 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
2105 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
2106 return isl_basic_map_finalize(bmap);
2108 isl_basic_map_free(bmap);
2112 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
2118 i = isl_basic_map_alloc_inequality(bmap);
2121 nparam = isl_basic_map_n_param(bmap);
2122 n_in = isl_basic_map_n_in(bmap);
2123 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
2124 isl_int_set_si(bmap->ineq[i][0], -1);
2125 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
2126 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
2127 return isl_basic_map_finalize(bmap);
2129 isl_basic_map_free(bmap);
2133 struct isl_basic_map *isl_basic_map_equal(struct isl_dim *dim, unsigned n_equal)
2136 struct isl_basic_map *bmap;
2137 bmap = isl_basic_map_alloc_dim(dim, 0, n_equal, 0);
2140 for (i = 0; i < n_equal && bmap; ++i)
2141 bmap = var_equal(bmap, i);
2142 return isl_basic_map_finalize(bmap);
2145 struct isl_basic_map *isl_basic_map_less_at(struct isl_dim *dim, unsigned pos)
2148 struct isl_basic_map *bmap;
2149 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
2152 for (i = 0; i < pos && bmap; ++i)
2153 bmap = var_equal(bmap, i);
2155 bmap = var_less(bmap, pos);
2156 return isl_basic_map_finalize(bmap);
2159 struct isl_basic_map *isl_basic_map_more_at(struct isl_dim *dim, unsigned pos)
2162 struct isl_basic_map *bmap;
2163 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
2166 for (i = 0; i < pos && bmap; ++i)
2167 bmap = var_equal(bmap, i);
2169 bmap = var_more(bmap, pos);
2170 return isl_basic_map_finalize(bmap);
2173 struct isl_basic_map *isl_basic_map_from_basic_set(
2174 struct isl_basic_set *bset, struct isl_dim *dim)
2176 struct isl_basic_map *bmap;
2178 bset = isl_basic_set_cow(bset);
2182 isl_assert(bset->ctx, isl_dim_compatible(bset->dim, dim), goto error);
2183 isl_dim_free(bset->dim);
2184 bmap = (struct isl_basic_map *) bset;
2186 return isl_basic_map_finalize(bmap);
2188 isl_basic_set_free(bset);
2193 struct isl_basic_set *isl_basic_set_from_basic_map(struct isl_basic_map *bmap)
2197 if (bmap->dim->n_in == 0)
2198 return (struct isl_basic_set *)bmap;
2199 bmap = isl_basic_map_cow(bmap);
2202 bmap->dim = isl_dim_cow(bmap->dim);
2205 bmap->dim->n_out += bmap->dim->n_in;
2206 bmap->dim->n_in = 0;
2207 bmap = isl_basic_map_finalize(bmap);
2208 return (struct isl_basic_set *)bmap;
2210 isl_basic_map_free(bmap);
2214 /* For a div d = floor(f/m), add the constraints
2217 * -(f-(n-1)) + m d >= 0
2219 * Note that the second constraint is the negation of
2223 static int add_div_constraints(struct isl_basic_map *bmap, unsigned div)
2226 unsigned total = isl_basic_map_total_dim(bmap);
2227 unsigned div_pos = 1 + total - bmap->n_div + div;
2229 i = isl_basic_map_alloc_inequality(bmap);
2232 isl_seq_cpy(bmap->ineq[i], bmap->div[div]+1, 1+total);
2233 isl_int_neg(bmap->ineq[i][div_pos], bmap->div[div][0]);
2235 j = isl_basic_map_alloc_inequality(bmap);
2238 isl_seq_neg(bmap->ineq[j], bmap->ineq[i], 1 + total);
2239 isl_int_add(bmap->ineq[j][0], bmap->ineq[j][0], bmap->ineq[j][div_pos]);
2240 isl_int_sub_ui(bmap->ineq[j][0], bmap->ineq[j][0], 1);
2244 struct isl_basic_set *isl_basic_map_underlying_set(
2245 struct isl_basic_map *bmap)
2249 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 && bmap->n_div == 0)
2250 return (struct isl_basic_set *)bmap;
2251 bmap = isl_basic_map_cow(bmap);
2254 bmap->dim = isl_dim_underlying(bmap->dim, bmap->n_div);
2257 bmap->extra -= bmap->n_div;
2259 bmap = isl_basic_map_finalize(bmap);
2260 return (struct isl_basic_set *)bmap;
2265 struct isl_basic_map *isl_basic_map_overlying_set(
2266 struct isl_basic_set *bset, struct isl_basic_map *like)
2268 struct isl_basic_map *bmap;
2269 struct isl_ctx *ctx;
2276 isl_assert(ctx, bset->n_div == 0, goto error);
2277 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
2278 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
2280 if (isl_dim_equal(bset->dim, like->dim) && like->n_div == 0) {
2281 isl_basic_map_free(like);
2282 return (struct isl_basic_map *)bset;
2284 bset = isl_basic_set_cow(bset);
2287 total = bset->dim->n_out + bset->extra;
2288 bmap = (struct isl_basic_map *)bset;
2289 isl_dim_free(bmap->dim);
2290 bmap->dim = isl_dim_copy(like->dim);
2293 bmap->n_div = like->n_div;
2294 bmap->extra += like->n_div;
2297 ltotal = total - bmap->extra + like->extra;
2300 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
2301 bmap->extra * (1 + 1 + total));
2302 if (isl_blk_is_error(bmap->block2))
2304 bmap->div = isl_realloc_array(ctx, bmap->div, isl_int *,
2308 for (i = 0; i < bmap->extra; ++i)
2309 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
2310 for (i = 0; i < like->n_div; ++i) {
2311 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
2312 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
2314 bmap = isl_basic_map_extend_constraints(bmap,
2315 0, 2 * like->n_div);
2316 for (i = 0; i < like->n_div; ++i) {
2317 if (isl_int_is_zero(bmap->div[i][0]))
2319 if (add_div_constraints(bmap, i) < 0)
2323 isl_basic_map_free(like);
2324 bmap = isl_basic_map_simplify(bmap);
2325 bmap = isl_basic_map_finalize(bmap);
2328 isl_basic_map_free(like);
2329 isl_basic_set_free(bset);
2333 struct isl_basic_set *isl_basic_set_from_underlying_set(
2334 struct isl_basic_set *bset, struct isl_basic_set *like)
2336 return (struct isl_basic_set *)
2337 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
2340 struct isl_set *isl_set_from_underlying_set(
2341 struct isl_set *set, struct isl_basic_set *like)
2347 isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
2349 if (isl_dim_equal(set->dim, like->dim) && like->n_div == 0) {
2350 isl_basic_set_free(like);
2353 set = isl_set_cow(set);
2356 for (i = 0; i < set->n; ++i) {
2357 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
2358 isl_basic_set_copy(like));
2362 isl_dim_free(set->dim);
2363 set->dim = isl_dim_copy(like->dim);
2366 isl_basic_set_free(like);
2369 isl_basic_set_free(like);
2374 struct isl_set *isl_map_underlying_set(struct isl_map *map)
2378 map = isl_map_cow(map);
2381 map->dim = isl_dim_cow(map->dim);
2385 for (i = 1; i < map->n; ++i)
2386 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
2388 for (i = 0; i < map->n; ++i) {
2389 map->p[i] = (struct isl_basic_map *)
2390 isl_basic_map_underlying_set(map->p[i]);
2395 map->dim = isl_dim_underlying(map->dim, 0);
2397 isl_dim_free(map->dim);
2398 map->dim = isl_dim_copy(map->p[0]->dim);
2402 return (struct isl_set *)map;
2408 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
2410 return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
2413 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
2415 struct isl_basic_set *domain;
2419 n_out = isl_basic_map_n_out(bmap);
2420 domain = isl_basic_set_from_basic_map(bmap);
2421 return isl_basic_set_project_out(domain, n_out, 0);
2424 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
2426 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
2429 struct isl_set *isl_map_range(struct isl_map *map)
2432 struct isl_set *set;
2436 map = isl_map_cow(map);
2440 set = (struct isl_set *) map;
2441 if (set->dim->n_in != 0) {
2442 set->dim = isl_dim_drop_inputs(set->dim, 0, set->dim->n_in);
2446 for (i = 0; i < map->n; ++i) {
2447 set->p[i] = isl_basic_map_range(map->p[i]);
2451 ISL_F_CLR(set, ISL_MAP_DISJOINT);
2452 ISL_F_CLR(set, ISL_SET_NORMALIZED);
2459 struct isl_map *isl_map_from_set(struct isl_set *set, struct isl_dim *dim)
2462 struct isl_map *map = NULL;
2464 set = isl_set_cow(set);
2467 isl_assert(set->ctx, isl_dim_compatible(set->dim, dim), goto error);
2468 map = (struct isl_map *)set;
2469 for (i = 0; i < set->n; ++i) {
2470 map->p[i] = isl_basic_map_from_basic_set(
2471 set->p[i], isl_dim_copy(dim));
2475 isl_dim_free(map->dim);
2484 struct isl_map *isl_map_from_range(struct isl_set *set)
2486 return (struct isl_map *)set;
2489 struct isl_set *isl_set_from_map(struct isl_map *map)
2492 struct isl_set *set = NULL;
2496 map = isl_map_cow(map);
2499 map->dim = isl_dim_cow(map->dim);
2502 map->dim->n_out += map->dim->n_in;
2504 set = (struct isl_set *)map;
2505 for (i = 0; i < map->n; ++i) {
2506 set->p[i] = isl_basic_set_from_basic_map(map->p[i]);
2516 struct isl_map *isl_map_alloc_dim(struct isl_dim *dim, int n, unsigned flags)
2518 struct isl_map *map;
2522 isl_assert(dim->ctx, n >= 0, return NULL);
2523 map = isl_alloc(dim->ctx, struct isl_map,
2524 sizeof(struct isl_map) +
2525 n * sizeof(struct isl_basic_map *));
2529 map->ctx = dim->ctx;
2530 isl_ctx_ref(map->ctx);
2542 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
2543 unsigned nparam, unsigned in, unsigned out, int n,
2546 struct isl_map *map;
2547 struct isl_dim *dims;
2549 dims = isl_dim_alloc(ctx, nparam, in, out);
2553 map = isl_map_alloc_dim(dims, n, flags);
2557 struct isl_basic_map *isl_basic_map_empty(struct isl_ctx *ctx,
2558 unsigned nparam, unsigned in, unsigned out)
2560 struct isl_basic_map *bmap;
2561 bmap = isl_basic_map_alloc(ctx, nparam, in, out, 0, 1, 0);
2562 bmap = isl_basic_map_set_to_empty(bmap);
2566 struct isl_basic_set *isl_basic_set_empty(struct isl_dim *dim)
2568 struct isl_basic_set *bset;
2569 bset = isl_basic_set_alloc_dim(dim, 0, 1, 0);
2570 bset = isl_basic_set_set_to_empty(bset);
2574 struct isl_basic_map *isl_basic_map_empty_like(struct isl_basic_map *model)
2576 struct isl_basic_map *bmap;
2579 bmap = isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
2580 bmap = isl_basic_map_set_to_empty(bmap);
2584 struct isl_basic_map *isl_basic_map_empty_like_map(struct isl_map *model)
2586 struct isl_basic_map *bmap;
2589 bmap = isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
2590 bmap = isl_basic_map_set_to_empty(bmap);
2594 struct isl_basic_set *isl_basic_set_empty_like(struct isl_basic_set *model)
2596 struct isl_basic_set *bset;
2599 bset = isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
2600 bset = isl_basic_set_set_to_empty(bset);
2604 struct isl_basic_map *isl_basic_map_universe(struct isl_dim *dim)
2606 struct isl_basic_map *bmap;
2607 bmap = isl_basic_map_alloc_dim(dim, 0, 0, 0);
2611 struct isl_basic_set *isl_basic_set_universe(struct isl_dim *dim)
2613 struct isl_basic_set *bset;
2614 bset = isl_basic_set_alloc_dim(dim, 0, 0, 0);
2618 struct isl_basic_set *isl_basic_set_universe_like(struct isl_basic_set *model)
2622 return isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
2625 struct isl_map *isl_map_empty(struct isl_dim *dim)
2627 return isl_map_alloc_dim(dim, 0, ISL_MAP_DISJOINT);
2630 struct isl_map *isl_map_empty_like(struct isl_map *model)
2634 return isl_map_alloc_dim(isl_dim_copy(model->dim), 0, ISL_MAP_DISJOINT);
2637 struct isl_map *isl_map_empty_like_basic_map(struct isl_basic_map *model)
2641 return isl_map_alloc_dim(isl_dim_copy(model->dim), 0, ISL_MAP_DISJOINT);
2644 struct isl_set *isl_set_empty(struct isl_dim *dim)
2646 return isl_set_alloc_dim(dim, 0, ISL_MAP_DISJOINT);
2649 struct isl_set *isl_set_empty_like(struct isl_set *model)
2653 return isl_set_empty(isl_dim_copy(model->dim));
2656 struct isl_map *isl_map_universe(struct isl_dim *dim)
2658 struct isl_map *map;
2661 map = isl_map_alloc_dim(isl_dim_copy(dim), 1, ISL_MAP_DISJOINT);
2662 map = isl_map_add(map, isl_basic_map_universe(dim));
2666 struct isl_set *isl_set_universe(struct isl_dim *dim)
2668 struct isl_set *set;
2671 set = isl_set_alloc_dim(isl_dim_copy(dim), 1, ISL_MAP_DISJOINT);
2672 set = isl_set_add(set, isl_basic_set_universe(dim));
2676 struct isl_map *isl_map_dup(struct isl_map *map)
2679 struct isl_map *dup;
2683 dup = isl_map_alloc_dim(isl_dim_copy(map->dim), map->n, map->flags);
2684 for (i = 0; i < map->n; ++i)
2685 dup = isl_map_add(dup, isl_basic_map_copy(map->p[i]));
2689 struct isl_map *isl_map_add(struct isl_map *map, struct isl_basic_map *bmap)
2693 isl_assert(map->ctx, isl_dim_equal(map->dim, bmap->dim), goto error);
2694 isl_assert(map->ctx, map->n < map->size, goto error);
2695 map->p[map->n] = bmap;
2697 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
2703 isl_basic_map_free(bmap);
2707 void isl_map_free(struct isl_map *map)
2717 isl_ctx_deref(map->ctx);
2718 for (i = 0; i < map->n; ++i)
2719 isl_basic_map_free(map->p[i]);
2720 isl_dim_free(map->dim);
2724 struct isl_map *isl_map_extend(struct isl_map *base,
2725 unsigned nparam, unsigned n_in, unsigned n_out)
2729 base = isl_map_cow(base);
2733 base->dim = isl_dim_extend(base->dim, nparam, n_in, n_out);
2736 for (i = 0; i < base->n; ++i) {
2737 base->p[i] = isl_basic_map_extend_dim(base->p[i],
2738 isl_dim_copy(base->dim), 0, 0, 0);
2748 struct isl_set *isl_set_extend(struct isl_set *base,
2749 unsigned nparam, unsigned dim)
2751 return (struct isl_set *)isl_map_extend((struct isl_map *)base,
2755 static struct isl_basic_map *isl_basic_map_fix_pos(struct isl_basic_map *bmap,
2756 unsigned pos, int value)
2760 bmap = isl_basic_map_cow(bmap);
2761 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
2762 j = isl_basic_map_alloc_equality(bmap);
2765 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
2766 isl_int_set_si(bmap->eq[j][pos], -1);
2767 isl_int_set_si(bmap->eq[j][0], value);
2768 bmap = isl_basic_map_simplify(bmap);
2769 return isl_basic_map_finalize(bmap);
2771 isl_basic_map_free(bmap);
2775 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
2776 enum isl_dim_type type, unsigned pos, int value)
2780 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
2781 return isl_basic_map_fix_pos(bmap, isl_basic_map_offset(bmap, type) + pos,
2784 isl_basic_map_free(bmap);
2788 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
2789 enum isl_dim_type type, unsigned pos, int value)
2791 return (struct isl_basic_set *)
2792 isl_basic_map_fix_si((struct isl_basic_map *)bset,
2796 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
2797 unsigned input, int value)
2799 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
2802 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
2803 unsigned dim, int value)
2805 return (struct isl_basic_set *)
2806 isl_basic_map_fix_si((struct isl_basic_map *)bset,
2807 isl_dim_set, dim, value);
2810 struct isl_map *isl_map_fix_si(struct isl_map *map,
2811 enum isl_dim_type type, unsigned pos, int value)
2815 map = isl_map_cow(map);
2819 isl_assert(ctx, pos < isl_map_dim(map, type), goto error);
2820 for (i = 0; i < map->n; ++i) {
2821 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
2825 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
2832 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
2833 unsigned input, int value)
2835 return isl_map_fix_si(map, isl_dim_in, input, value);
2838 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
2840 return (struct isl_set *)
2841 isl_map_fix_si((struct isl_map *)set, isl_dim_set, dim, value);
2844 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
2845 unsigned dim, isl_int value)
2850 bset = isl_basic_set_cow(bset);
2851 bset = isl_basic_set_extend_constraints(bset, 0, 1);
2852 j = isl_basic_set_alloc_inequality(bset);
2855 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
2856 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
2857 isl_int_neg(bset->ineq[j][0], value);
2858 bset = isl_basic_set_simplify(bset);
2859 return isl_basic_set_finalize(bset);
2861 isl_basic_set_free(bset);
2865 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
2870 set = isl_set_cow(set);
2874 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
2875 for (i = 0; i < set->n; ++i) {
2876 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
2886 struct isl_map *isl_map_reverse(struct isl_map *map)
2891 map = isl_map_cow(map);
2895 map->dim = isl_dim_reverse(map->dim);
2898 for (i = 0; i < map->n; ++i) {
2899 map->p[i] = isl_basic_map_reverse(map->p[i]);
2903 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
2910 struct isl_map *isl_basic_map_lexmax(
2911 struct isl_basic_map *bmap, struct isl_basic_set *dom,
2912 struct isl_set **empty)
2914 return isl_pip_basic_map_lexmax(bmap, dom, empty);
2917 struct isl_map *isl_basic_map_lexmin(
2918 struct isl_basic_map *bmap, struct isl_basic_set *dom,
2919 struct isl_set **empty)
2921 return isl_pip_basic_map_lexmin(bmap, dom, empty);
2924 struct isl_set *isl_basic_set_lexmin(struct isl_basic_set *bset)
2926 struct isl_basic_map *bmap = NULL;
2927 struct isl_basic_set *dom = NULL;
2928 struct isl_map *min;
2929 struct isl_dim *param_dim;
2933 bmap = isl_basic_map_from_basic_set(bset, isl_dim_copy(bset->dim));
2936 param_dim = isl_dim_domain(isl_dim_copy(bmap->dim));
2937 dom = isl_basic_set_universe(param_dim);
2940 min = isl_basic_map_lexmin(bmap, dom, NULL);
2941 return isl_map_range(min);
2943 isl_basic_map_free(bmap);
2947 /* If bmap contains any unknown divs, then compute explicit
2948 * expressions for them. However, this computation may be
2949 * quite expensive, so first try to remove divs that aren't
2952 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
2959 off = isl_dim_total(bmap->dim);
2960 for (i = 0; i < bmap->n_div; ++i) {
2961 if (isl_int_is_zero(bmap->div[i][0]))
2963 isl_assert(bmap->ctx, isl_int_is_zero(bmap->div[i][1+1+off+i]),
2966 if (i == bmap->n_div)
2967 return isl_map_from_basic_map(bmap);
2968 bmap = isl_basic_map_drop_redundant_divs(bmap);
2971 for (i = 0; i < bmap->n_div; ++i)
2972 if (isl_int_is_zero(bmap->div[i][0]))
2974 if (i == bmap->n_div)
2975 return isl_map_from_basic_map(bmap);
2976 struct isl_map *map = isl_pip_basic_map_compute_divs(bmap);
2979 isl_basic_map_free(bmap);
2983 struct isl_map *isl_map_compute_divs(struct isl_map *map)
2986 struct isl_map *res;
2992 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
2993 for (i = 1 ; i < map->n; ++i) {
2995 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
2996 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
2997 res = isl_map_union_disjoint(res, r2);
2999 res = isl_map_union(res, r2);
3006 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
3008 return (struct isl_set *)
3009 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
3012 struct isl_set *isl_set_compute_divs(struct isl_set *set)
3014 return (struct isl_set *)
3015 isl_map_compute_divs((struct isl_map *)set);
3018 struct isl_set *isl_map_domain(struct isl_map *map)
3021 struct isl_set *set;
3026 map = isl_map_cow(map);
3030 set = (struct isl_set *)map;
3031 set->dim = isl_dim_domain(set->dim);
3034 for (i = 0; i < map->n; ++i) {
3035 set->p[i] = isl_basic_map_domain(map->p[i]);
3039 ISL_F_CLR(set, ISL_MAP_DISJOINT);
3040 ISL_F_CLR(set, ISL_SET_NORMALIZED);
3047 struct isl_map *isl_map_union_disjoint(
3048 struct isl_map *map1, struct isl_map *map2)
3052 struct isl_map *map = NULL;
3066 isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
3068 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
3069 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
3070 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3072 map = isl_map_alloc_dim(isl_dim_copy(map1->dim),
3073 map1->n + map2->n, flags);
3076 for (i = 0; i < map1->n; ++i) {
3077 map = isl_map_add(map,
3078 isl_basic_map_copy(map1->p[i]));
3082 for (i = 0; i < map2->n; ++i) {
3083 map = isl_map_add(map,
3084 isl_basic_map_copy(map2->p[i]));
3098 struct isl_map *isl_map_union(struct isl_map *map1, struct isl_map *map2)
3100 map1 = isl_map_union_disjoint(map1, map2);
3104 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
3108 struct isl_set *isl_set_union_disjoint(
3109 struct isl_set *set1, struct isl_set *set2)
3111 return (struct isl_set *)
3112 isl_map_union_disjoint(
3113 (struct isl_map *)set1, (struct isl_map *)set2);
3116 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
3118 return (struct isl_set *)
3119 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
3122 struct isl_map *isl_map_intersect_range(
3123 struct isl_map *map, struct isl_set *set)
3126 struct isl_map *result;
3132 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
3133 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
3134 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3136 result = isl_map_alloc_dim(isl_dim_copy(map->dim),
3137 map->n * set->n, flags);
3140 for (i = 0; i < map->n; ++i)
3141 for (j = 0; j < set->n; ++j) {
3142 result = isl_map_add(result,
3143 isl_basic_map_intersect_range(
3144 isl_basic_map_copy(map->p[i]),
3145 isl_basic_set_copy(set->p[j])));
3158 struct isl_map *isl_map_intersect_domain(
3159 struct isl_map *map, struct isl_set *set)
3161 return isl_map_reverse(
3162 isl_map_intersect_range(isl_map_reverse(map), set));
3165 struct isl_map *isl_map_apply_domain(
3166 struct isl_map *map1, struct isl_map *map2)
3170 map1 = isl_map_reverse(map1);
3171 map1 = isl_map_apply_range(map1, map2);
3172 return isl_map_reverse(map1);
3179 struct isl_map *isl_map_apply_range(
3180 struct isl_map *map1, struct isl_map *map2)
3182 struct isl_dim *dim_result;
3183 struct isl_map *result;
3192 dim_result = isl_dim_join(isl_dim_copy(map1->dim),
3193 isl_dim_copy(map2->dim));
3195 result = isl_map_alloc_dim(dim_result, map1->n * map2->n, 0);
3198 for (i = 0; i < map1->n; ++i)
3199 for (j = 0; j < map2->n; ++j) {
3200 result = isl_map_add(result,
3201 isl_basic_map_apply_range(
3202 isl_basic_map_copy(map1->p[i]),
3203 isl_basic_map_copy(map2->p[j])));
3209 if (result && result->n <= 1)
3210 ISL_F_SET(result, ISL_MAP_DISJOINT);
3219 * returns range - domain
3221 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
3223 struct isl_basic_set *bset;
3230 dim = isl_basic_map_n_in(bmap);
3231 nparam = isl_basic_map_n_param(bmap);
3232 isl_assert(bmap->ctx, dim == isl_basic_map_n_out(bmap), goto error);
3233 bset = isl_basic_set_from_basic_map(bmap);
3234 bset = isl_basic_set_cow(bset);
3235 bset = isl_basic_set_extend(bset, nparam, 3*dim, 0, dim, 0);
3236 bset = isl_basic_set_swap_vars(bset, 2*dim);
3237 for (i = 0; i < dim; ++i) {
3238 int j = isl_basic_map_alloc_equality(
3239 (struct isl_basic_map *)bset);
3242 isl_seq_clr(bset->eq[j], 1 + isl_basic_set_total_dim(bset));
3243 isl_int_set_si(bset->eq[j][1+nparam+i], 1);
3244 isl_int_set_si(bset->eq[j][1+nparam+dim+i], 1);
3245 isl_int_set_si(bset->eq[j][1+nparam+2*dim+i], -1);
3247 return isl_basic_set_project_out(bset, 2*dim, 0);
3249 isl_basic_map_free(bmap);
3254 * returns range - domain
3256 struct isl_set *isl_map_deltas(struct isl_map *map)
3259 struct isl_set *result;
3264 isl_assert(map->ctx, isl_map_n_in(map) == isl_map_n_out(map), goto error);
3265 result = isl_set_alloc(map->ctx, isl_map_n_param(map),
3266 isl_map_n_in(map), map->n, map->flags);
3269 for (i = 0; i < map->n; ++i)
3270 result = isl_set_add(result,
3271 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
3279 static struct isl_basic_map *basic_map_identity(struct isl_dim *dims)
3281 struct isl_basic_map *bmap;
3289 nparam = dims->nparam;
3291 bmap = isl_basic_map_alloc_dim(dims, 0, dim, 0);
3295 for (i = 0; i < dim; ++i) {
3296 int j = isl_basic_map_alloc_equality(bmap);
3299 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
3300 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
3301 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
3303 return isl_basic_map_finalize(bmap);
3305 isl_basic_map_free(bmap);
3309 struct isl_basic_map *isl_basic_map_identity(struct isl_dim *set_dim)
3311 struct isl_dim *dim = isl_dim_map(set_dim);
3314 return basic_map_identity(dim);
3317 struct isl_basic_map *isl_basic_map_identity_like(struct isl_basic_map *model)
3319 if (!model || !model->dim)
3321 isl_assert(model->ctx,
3322 model->dim->n_in == model->dim->n_out, return NULL);
3323 return basic_map_identity(isl_dim_copy(model->dim));
3326 static struct isl_map *map_identity(struct isl_dim *dim)
3328 struct isl_map *map = isl_map_alloc_dim(dim, 1, ISL_MAP_DISJOINT);
3329 return isl_map_add(map, basic_map_identity(isl_dim_copy(dim)));
3332 struct isl_map *isl_map_identity(struct isl_dim *set_dim)
3334 struct isl_dim *dim = isl_dim_map(set_dim);
3337 return map_identity(dim);
3340 struct isl_map *isl_map_identity_like(struct isl_basic_map *model)
3342 if (!model || !model->dim)
3344 isl_assert(model->ctx,
3345 model->dim->n_in == model->dim->n_out, return NULL);
3346 return map_identity(isl_dim_copy(model->dim));
3349 int isl_set_is_equal(struct isl_set *set1, struct isl_set *set2)
3351 return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
3354 int isl_set_is_subset(struct isl_set *set1, struct isl_set *set2)
3356 return isl_map_is_subset(
3357 (struct isl_map *)set1, (struct isl_map *)set2);
3360 int isl_basic_map_is_subset(
3361 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3364 struct isl_map *map1;
3365 struct isl_map *map2;
3367 if (!bmap1 || !bmap2)
3370 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
3371 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
3373 is_subset = isl_map_is_subset(map1, map2);
3381 int isl_basic_map_is_equal(
3382 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3386 if (!bmap1 || !bmap2)
3388 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
3391 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
3395 int isl_basic_set_is_equal(
3396 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
3398 return isl_basic_map_is_equal(
3399 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
3402 int isl_map_is_empty(struct isl_map *map)
3409 for (i = 0; i < map->n; ++i) {
3410 is_empty = isl_basic_map_is_empty(map->p[i]);
3419 int isl_map_fast_is_empty(struct isl_map *map)
3424 int isl_set_is_empty(struct isl_set *set)
3426 return isl_map_is_empty((struct isl_map *)set);
3429 int isl_map_is_subset(struct isl_map *map1, struct isl_map *map2)
3433 struct isl_map *diff;
3438 if (isl_map_is_empty(map1))
3441 if (isl_map_is_empty(map2))
3444 diff = isl_map_subtract(isl_map_copy(map1), isl_map_copy(map2));
3448 is_subset = isl_map_is_empty(diff);
3454 int isl_map_is_equal(struct isl_map *map1, struct isl_map *map2)
3460 is_subset = isl_map_is_subset(map1, map2);
3463 is_subset = isl_map_is_subset(map2, map1);
3467 int isl_basic_map_is_strict_subset(
3468 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3472 if (!bmap1 || !bmap2)
3474 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
3477 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
3478 if (is_subset == -1)
3483 int isl_basic_map_is_universe(struct isl_basic_map *bmap)
3487 return bmap->n_eq == 0 && bmap->n_ineq == 0;
3490 int isl_basic_set_is_universe(struct isl_basic_set *bset)
3494 return bset->n_eq == 0 && bset->n_ineq == 0;
3497 int isl_basic_map_is_empty(struct isl_basic_map *bmap)
3499 struct isl_basic_set *bset = NULL;
3500 struct isl_vec *sample = NULL;
3507 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
3510 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
3511 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
3512 copy = isl_basic_map_convex_hull(copy);
3513 empty = ISL_F_ISSET(copy, ISL_BASIC_MAP_EMPTY);
3514 isl_basic_map_free(copy);
3518 total = 1 + isl_basic_map_total_dim(bmap);
3519 if (bmap->sample && bmap->sample->size == total) {
3520 int contains = basic_map_contains(bmap, bmap->sample);
3526 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
3529 sample = isl_basic_set_sample(bset);
3532 empty = sample->size == 0;
3534 isl_vec_free(bmap->sample);
3535 bmap->sample = sample;
3537 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
3542 int isl_basic_map_fast_is_empty(struct isl_basic_map *bmap)
3546 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
3549 int isl_basic_set_fast_is_empty(struct isl_basic_set *bset)
3553 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
3556 int isl_basic_set_is_empty(struct isl_basic_set *bset)
3558 return isl_basic_map_is_empty((struct isl_basic_map *)bset);
3561 struct isl_map *isl_basic_map_union(
3562 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3564 struct isl_map *map;
3565 if (!bmap1 || !bmap2)
3568 isl_assert(map1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim), goto error);
3570 map = isl_map_alloc_dim(isl_dim_copy(bmap1->dim), 2, 0);
3573 map = isl_map_add(map, bmap1);
3574 map = isl_map_add(map, bmap2);
3577 isl_basic_map_free(bmap1);
3578 isl_basic_map_free(bmap2);
3582 struct isl_set *isl_basic_set_union(
3583 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
3585 return (struct isl_set *)isl_basic_map_union(
3586 (struct isl_basic_map *)bset1,
3587 (struct isl_basic_map *)bset2);
3590 /* Order divs such that any div only depends on previous divs */
3591 static struct isl_basic_map *order_divs(struct isl_basic_map *bmap)
3594 unsigned off = isl_dim_total(bmap->dim);
3596 for (i = 0; i < bmap->n_div; ++i) {
3598 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
3602 swap_div(bmap, i, i + pos);
3608 /* Look for a div in dst that corresponds to the div "div" in src.
3609 * The divs before "div" in src and dst are assumed to be the same.
3611 * Returns -1 if no corresponding div was found and the position
3612 * of the corresponding div in dst otherwise.
3614 static int find_div(struct isl_basic_map *dst,
3615 struct isl_basic_map *src, unsigned div)
3619 unsigned total = isl_dim_total(src->dim);
3621 isl_assert(dst->ctx, div <= dst->n_div, return -1);
3622 for (i = div; i < dst->n_div; ++i)
3623 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
3624 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
3625 dst->n_div - div) == -1)
3630 struct isl_basic_map *isl_basic_map_align_divs(
3631 struct isl_basic_map *dst, struct isl_basic_map *src)
3634 unsigned total = isl_dim_total(src->dim);
3639 if (src->n_div == 0)
3642 for (i = 0; i < src->n_div; ++i)
3643 isl_assert(src->ctx, !isl_int_is_zero(src->div[i][0]), goto error);
3645 src = order_divs(src);
3646 dst = isl_basic_map_cow(dst);
3647 dst = isl_basic_map_extend_dim(dst, isl_dim_copy(dst->dim),
3648 src->n_div, 0, 2 * src->n_div);
3651 for (i = 0; i < src->n_div; ++i) {
3652 int j = find_div(dst, src, i);
3654 j = isl_basic_map_alloc_div(dst);
3657 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
3658 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
3659 if (add_div_constraints(dst, j) < 0)
3663 swap_div(dst, i, j);
3667 isl_basic_map_free(dst);
3671 struct isl_basic_set *isl_basic_set_align_divs(
3672 struct isl_basic_set *dst, struct isl_basic_set *src)
3674 return (struct isl_basic_set *)isl_basic_map_align_divs(
3675 (struct isl_basic_map *)dst, (struct isl_basic_map *)src);
3678 struct isl_map *isl_map_align_divs(struct isl_map *map)
3682 map = isl_map_compute_divs(map);
3683 map = isl_map_cow(map);
3687 for (i = 1; i < map->n; ++i)
3688 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
3689 for (i = 1; i < map->n; ++i)
3690 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
3692 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3696 struct isl_set *isl_set_align_divs(struct isl_set *set)
3698 return (struct isl_set *)isl_map_align_divs((struct isl_map *)set);
3701 static struct isl_map *add_cut_constraint(struct isl_map *dst,
3702 struct isl_basic_map *src, isl_int *c,
3703 unsigned len, int oppose)
3705 struct isl_basic_map *copy = NULL;
3710 copy = isl_basic_map_copy(src);
3711 copy = isl_basic_map_cow(copy);
3714 copy = isl_basic_map_extend_constraints(copy, 0, 1);
3715 k = isl_basic_map_alloc_inequality(copy);
3719 isl_seq_neg(copy->ineq[k], c, len);
3721 isl_seq_cpy(copy->ineq[k], c, len);
3722 total = 1 + isl_basic_map_total_dim(copy);
3723 isl_seq_clr(copy->ineq[k]+len, total - len);
3724 isl_inequality_negate(copy, k);
3725 copy = isl_basic_map_simplify(copy);
3726 copy = isl_basic_map_finalize(copy);
3727 is_empty = isl_basic_map_is_empty(copy);
3731 dst = isl_map_add(dst, copy);
3733 isl_basic_map_free(copy);
3736 isl_basic_map_free(copy);
3741 static struct isl_map *subtract(struct isl_map *map, struct isl_basic_map *bmap)
3745 struct isl_map *rest = NULL;
3747 unsigned total = isl_basic_map_total_dim(bmap);
3754 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
3755 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3757 max = map->n * (2 * bmap->n_eq + bmap->n_ineq);
3758 rest = isl_map_alloc_dim(isl_dim_copy(map->dim), max, flags);
3762 for (i = 0; i < map->n; ++i) {
3763 map->p[i] = isl_basic_map_align_divs(map->p[i], bmap);
3768 for (j = 0; j < map->n; ++j)
3769 map->p[j] = isl_basic_map_cow(map->p[j]);
3771 for (i = 0; i < bmap->n_eq; ++i) {
3772 for (j = 0; j < map->n; ++j) {
3773 rest = add_cut_constraint(rest,
3774 map->p[j], bmap->eq[i], 1+total, 0);
3778 rest = add_cut_constraint(rest,
3779 map->p[j], bmap->eq[i], 1+total, 1);
3783 map->p[j] = isl_basic_map_extend_constraints(map->p[j],
3787 k = isl_basic_map_alloc_equality(map->p[j]);
3790 isl_seq_cpy(map->p[j]->eq[k], bmap->eq[i], 1+total);
3791 isl_seq_clr(map->p[j]->eq[k]+1+total,
3792 map->p[j]->n_div - bmap->n_div);
3796 for (i = 0; i < bmap->n_ineq; ++i) {
3797 for (j = 0; j < map->n; ++j) {
3798 rest = add_cut_constraint(rest,
3799 map->p[j], bmap->ineq[i], 1+total, 0);
3803 map->p[j] = isl_basic_map_extend_constraints(map->p[j],
3807 k = isl_basic_map_alloc_inequality(map->p[j]);
3810 isl_seq_cpy(map->p[j]->ineq[k], bmap->ineq[i], 1+total);
3811 isl_seq_clr(map->p[j]->ineq[k]+1+total,
3812 map->p[j]->n_div - bmap->n_div);
3824 struct isl_map *isl_map_subtract(struct isl_map *map1, struct isl_map *map2)
3830 isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
3832 if (isl_map_is_empty(map2)) {
3837 map1 = isl_map_compute_divs(map1);
3838 map2 = isl_map_compute_divs(map2);
3842 for (i = 0; map1 && i < map2->n; ++i)
3843 map1 = subtract(map1, map2->p[i]);
3853 struct isl_set *isl_set_subtract(struct isl_set *set1, struct isl_set *set2)
3855 return (struct isl_set *)
3857 (struct isl_map *)set1, (struct isl_map *)set2);
3860 struct isl_set *isl_set_apply(struct isl_set *set, struct isl_map *map)
3864 isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
3865 map = isl_map_intersect_domain(map, set);
3866 set = isl_map_range(map);
3874 /* There is no need to cow as removing empty parts doesn't change
3875 * the meaning of the set.
3877 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
3884 for (i = map->n-1; i >= 0; --i) {
3885 if (!ISL_F_ISSET(map->p[i], ISL_BASIC_MAP_EMPTY))
3887 isl_basic_map_free(map->p[i]);
3888 if (i != map->n-1) {
3889 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3890 map->p[i] = map->p[map->n-1];
3898 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
3900 return (struct isl_set *)
3901 isl_map_remove_empty_parts((struct isl_map *)set);
3904 struct isl_basic_map *isl_map_copy_basic_map(struct isl_map *map)
3906 struct isl_basic_map *bmap;
3907 if (!map || map->n == 0)
3909 bmap = map->p[map->n-1];
3910 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
3911 return isl_basic_map_copy(bmap);
3914 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
3916 (struct isl_basic_set *)isl_map_copy_basic_map((struct isl_map *)set);
3919 struct isl_map *isl_map_drop_basic_map(struct isl_map *map,
3920 struct isl_basic_map *bmap)
3926 for (i = map->n-1; i >= 0; --i) {
3927 if (map->p[i] != bmap)
3929 map = isl_map_cow(map);
3932 isl_basic_map_free(map->p[i]);
3933 if (i != map->n-1) {
3934 ISL_F_CLR(map, ISL_SET_NORMALIZED);
3935 map->p[i] = map->p[map->n-1];
3940 isl_basic_map_free(bmap);
3944 isl_basic_map_free(bmap);
3948 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
3949 struct isl_basic_set *bset)
3951 (struct isl_set *)isl_map_drop_basic_map((struct isl_map *)set,
3952 (struct isl_basic_map *)bset);
3955 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
3956 * for any common value of the parameters and dimensions preceding dim
3957 * in both basic sets, the values of dimension pos in bset1 are
3958 * smaller or larger than those in bset2.
3961 * 1 if bset1 follows bset2
3962 * -1 if bset1 precedes bset2
3963 * 0 if bset1 and bset2 are incomparable
3964 * -2 if some error occurred.
3966 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
3967 struct isl_basic_set *bset2, int pos)
3969 struct isl_dim *dims;
3970 struct isl_basic_map *bmap1 = NULL;
3971 struct isl_basic_map *bmap2 = NULL;
3972 struct isl_ctx *ctx;
3973 struct isl_vec *obj;
3976 unsigned dim1, dim2;
3978 enum isl_lp_result res;
3981 if (!bset1 || !bset2)
3984 nparam = isl_basic_set_n_param(bset1);
3985 dim1 = isl_basic_set_n_dim(bset1);
3986 dim2 = isl_basic_set_n_dim(bset2);
3987 dims = isl_dim_alloc(bset1->ctx, nparam, pos, dim1 - pos);
3988 bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1), dims);
3989 dims = isl_dim_alloc(bset2->ctx, nparam, pos, dim2 - pos);
3990 bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims);
3991 if (!bmap1 || !bmap2)
3993 bmap1 = isl_basic_map_cow(bmap1);
3994 bmap1 = isl_basic_map_extend(bmap1, nparam,
3995 pos, (dim1 - pos) + (dim2 - pos),
3996 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
3997 bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
4000 total = isl_basic_map_total_dim(bmap1);
4002 obj = isl_vec_alloc(ctx, 1 + total);
4003 isl_seq_clr(obj->block.data, 1 + total);
4004 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
4005 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
4010 res = isl_solve_lp(bmap1, 0, obj->block.data, ctx->one, &num, &den);
4011 if (res == isl_lp_empty)
4013 else if (res == isl_lp_ok && isl_int_is_pos(num))
4015 else if ((res == isl_lp_ok && isl_int_is_neg(num)) ||
4016 res == isl_lp_unbounded)
4022 isl_basic_map_free(bmap1);
4026 isl_basic_map_free(bmap1);
4027 isl_basic_map_free(bmap2);
4031 static int isl_basic_map_fast_has_fixed_var(struct isl_basic_map *bmap,
4032 unsigned pos, isl_int *val)
4040 total = isl_basic_map_total_dim(bmap);
4041 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
4042 for (; d+1 > pos; --d)
4043 if (!isl_int_is_zero(bmap->eq[i][1+d]))
4047 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
4049 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
4051 if (!isl_int_is_one(bmap->eq[i][1+d]))
4054 isl_int_neg(*val, bmap->eq[i][0]);
4060 static int isl_map_fast_has_fixed_var(struct isl_map *map,
4061 unsigned pos, isl_int *val)
4073 return isl_basic_map_fast_has_fixed_var(map->p[0], pos, val);
4076 fixed = isl_basic_map_fast_has_fixed_var(map->p[0], pos, &v);
4077 for (i = 1; fixed == 1 && i < map->n; ++i) {
4078 fixed = isl_basic_map_fast_has_fixed_var(map->p[i], pos, &tmp);
4079 if (fixed == 1 && isl_int_ne(tmp, v))
4083 isl_int_set(*val, v);
4089 static int isl_set_fast_has_fixed_var(struct isl_set *set, unsigned pos,
4092 return isl_map_fast_has_fixed_var((struct isl_map *)set, pos, val);
4095 int isl_basic_map_fast_is_fixed(struct isl_basic_map *bmap,
4096 enum isl_dim_type type, unsigned pos, isl_int *val)
4098 if (pos >= isl_basic_map_dim(bmap, type))
4100 return isl_basic_map_fast_has_fixed_var(bmap,
4101 isl_basic_map_offset(bmap, type) - 1 + pos, val);
4104 /* Check if dimension dim has fixed value and if so and if val is not NULL,
4105 * then return this fixed value in *val.
4107 int isl_set_fast_dim_is_fixed(struct isl_set *set, unsigned dim, isl_int *val)
4109 return isl_set_fast_has_fixed_var(set, isl_set_n_param(set) + dim, val);
4112 /* Check if input variable in has fixed value and if so and if val is not NULL,
4113 * then return this fixed value in *val.
4115 int isl_map_fast_input_is_fixed(struct isl_map *map, unsigned in, isl_int *val)
4117 return isl_map_fast_has_fixed_var(map, isl_map_n_param(map) + in, val);
4120 /* Check if dimension dim has an (obvious) fixed lower bound and if so
4121 * and if val is not NULL, then return this lower bound in *val.
4123 int isl_basic_set_fast_dim_has_fixed_lower_bound(struct isl_basic_set *bset,
4124 unsigned dim, isl_int *val)
4126 int i, i_eq = -1, i_ineq = -1;
4133 total = isl_basic_set_total_dim(bset);
4134 nparam = isl_basic_set_n_param(bset);
4135 for (i = 0; i < bset->n_eq; ++i) {
4136 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
4142 for (i = 0; i < bset->n_ineq; ++i) {
4143 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
4145 if (i_eq != -1 || i_ineq != -1)
4149 if (i_eq == -1 && i_ineq == -1)
4151 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
4152 /* The coefficient should always be one due to normalization. */
4153 if (!isl_int_is_one(c[1+nparam+dim]))
4155 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
4157 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
4158 total - nparam - dim - 1) != -1)
4161 isl_int_neg(*val, c[0]);
4165 int isl_set_fast_dim_has_fixed_lower_bound(struct isl_set *set,
4166 unsigned dim, isl_int *val)
4178 return isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[0],
4182 fixed = isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[0],
4184 for (i = 1; fixed == 1 && i < set->n; ++i) {
4185 fixed = isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[i],
4187 if (fixed == 1 && isl_int_ne(tmp, v))
4191 isl_int_set(*val, v);
4202 static int qsort_constraint_cmp(const void *p1, const void *p2)
4204 const struct constraint *c1 = (const struct constraint *)p1;
4205 const struct constraint *c2 = (const struct constraint *)p2;
4206 unsigned size = isl_min(c1->size, c2->size);
4207 return isl_seq_cmp(c1->c, c2->c, size);
4210 static struct isl_basic_map *isl_basic_map_sort_constraints(
4211 struct isl_basic_map *bmap)
4214 struct constraint *c;
4219 total = isl_basic_map_total_dim(bmap);
4220 c = isl_alloc_array(bmap->ctx, struct constraint, bmap->n_ineq);
4223 for (i = 0; i < bmap->n_ineq; ++i) {
4225 c[i].c = bmap->ineq[i];
4227 qsort(c, bmap->n_ineq, sizeof(struct constraint), qsort_constraint_cmp);
4228 for (i = 0; i < bmap->n_ineq; ++i)
4229 bmap->ineq[i] = c[i].c;
4233 isl_basic_map_free(bmap);
4237 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
4241 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
4243 bmap = isl_basic_map_convex_hull(bmap);
4244 bmap = isl_basic_map_sort_constraints(bmap);
4245 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
4249 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
4251 return (struct isl_basic_set *)isl_basic_map_normalize(
4252 (struct isl_basic_map *)bset);
4255 static int isl_basic_map_fast_cmp(const struct isl_basic_map *bmap1,
4256 const struct isl_basic_map *bmap2)
4263 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
4264 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
4265 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
4266 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
4267 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
4268 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
4269 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
4270 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
4272 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
4274 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
4276 if (bmap1->n_eq != bmap2->n_eq)
4277 return bmap1->n_eq - bmap2->n_eq;
4278 if (bmap1->n_ineq != bmap2->n_ineq)
4279 return bmap1->n_ineq - bmap2->n_ineq;
4280 if (bmap1->n_div != bmap2->n_div)
4281 return bmap1->n_div - bmap2->n_div;
4282 total = isl_basic_map_total_dim(bmap1);
4283 for (i = 0; i < bmap1->n_eq; ++i) {
4284 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
4288 for (i = 0; i < bmap1->n_ineq; ++i) {
4289 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
4293 for (i = 0; i < bmap1->n_div; ++i) {
4294 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
4301 static int isl_basic_map_fast_is_equal(struct isl_basic_map *bmap1,
4302 struct isl_basic_map *bmap2)
4304 return isl_basic_map_fast_cmp(bmap1, bmap2) == 0;
4307 static int qsort_bmap_cmp(const void *p1, const void *p2)
4309 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
4310 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
4312 return isl_basic_map_fast_cmp(bmap1, bmap2);
4315 /* We normalize in place, but if anything goes wrong we need
4316 * to return NULL, so we need to make sure we don't change the
4317 * meaning of any possible other copies of map.
4319 struct isl_map *isl_map_normalize(struct isl_map *map)
4322 struct isl_basic_map *bmap;
4326 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
4328 for (i = 0; i < map->n; ++i) {
4329 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
4332 isl_basic_map_free(map->p[i]);
4335 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
4336 ISL_F_SET(map, ISL_MAP_NORMALIZED);
4337 map = isl_map_remove_empty_parts(map);
4340 for (i = map->n - 1; i >= 1; --i) {
4341 if (!isl_basic_map_fast_is_equal(map->p[i-1], map->p[i]))
4343 isl_basic_map_free(map->p[i-1]);
4344 for (j = i; j < map->n; ++j)
4345 map->p[j-1] = map->p[j];
4355 struct isl_set *isl_set_normalize(struct isl_set *set)
4357 return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
4360 int isl_map_fast_is_equal(struct isl_map *map1, struct isl_map *map2)
4370 if (!isl_dim_equal(map1->dim, map2->dim))
4373 map1 = isl_map_copy(map1);
4374 map2 = isl_map_copy(map2);
4375 map1 = isl_map_normalize(map1);
4376 map2 = isl_map_normalize(map2);
4379 equal = map1->n == map2->n;
4380 for (i = 0; equal && i < map1->n; ++i) {
4381 equal = isl_basic_map_fast_is_equal(map1->p[i], map2->p[i]);
4394 int isl_set_fast_is_equal(struct isl_set *set1, struct isl_set *set2)
4396 return isl_map_fast_is_equal((struct isl_map *)set1,
4397 (struct isl_map *)set2);
4400 /* Return an interval that ranges from min to max (inclusive)
4402 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
4403 isl_int min, isl_int max)
4406 struct isl_basic_set *bset = NULL;
4408 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
4412 k = isl_basic_set_alloc_inequality(bset);
4415 isl_int_set_si(bset->ineq[k][1], 1);
4416 isl_int_neg(bset->ineq[k][0], min);
4418 k = isl_basic_set_alloc_inequality(bset);
4421 isl_int_set_si(bset->ineq[k][1], -1);
4422 isl_int_set(bset->ineq[k][0], max);
4426 isl_basic_set_free(bset);
4430 /* Return the Cartesian product of the basic sets in list (in the given order).
4432 struct isl_basic_set *isl_basic_set_product(struct isl_basic_set_list *list)
4440 struct isl_basic_set *product = NULL;
4444 isl_assert(list->ctx, list->n > 0, goto error);
4445 isl_assert(list->ctx, list->p[0], goto error);
4446 nparam = isl_basic_set_n_param(list->p[0]);
4447 dim = isl_basic_set_n_dim(list->p[0]);
4448 extra = list->p[0]->n_div;
4449 n_eq = list->p[0]->n_eq;
4450 n_ineq = list->p[0]->n_ineq;
4451 for (i = 1; i < list->n; ++i) {
4452 isl_assert(list->ctx, list->p[i], goto error);
4453 isl_assert(list->ctx,
4454 nparam == isl_basic_set_n_param(list->p[i]), goto error);
4455 dim += isl_basic_set_n_dim(list->p[i]);
4456 extra += list->p[i]->n_div;
4457 n_eq += list->p[i]->n_eq;
4458 n_ineq += list->p[i]->n_ineq;
4460 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
4465 for (i = 0; i < list->n; ++i) {
4466 isl_basic_set_add_constraints(product,
4467 isl_basic_set_copy(list->p[i]), dim);
4468 dim += isl_basic_set_n_dim(list->p[i]);
4470 isl_basic_set_list_free(list);
4473 isl_basic_set_free(product);
4474 isl_basic_set_list_free(list);
4478 struct isl_basic_map *isl_basic_map_product(
4479 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4481 struct isl_dim *dim_result = NULL;
4482 struct isl_basic_map *bmap;
4483 unsigned in1, in2, out1, out2, nparam, total, pos;
4484 struct isl_dim_map *dim_map1, *dim_map2;
4486 if (!bmap1 || !bmap2)
4489 isl_assert(map1->ctx, isl_dim_match(bmap1->dim, isl_dim_param,
4490 bmap2->dim, isl_dim_param), goto error);
4491 dim_result = isl_dim_product(isl_dim_copy(bmap1->dim),
4492 isl_dim_copy(bmap2->dim));
4494 in1 = isl_basic_map_n_in(bmap1);
4495 in2 = isl_basic_map_n_in(bmap2);
4496 out1 = isl_basic_map_n_out(bmap1);
4497 out2 = isl_basic_map_n_out(bmap2);
4498 nparam = isl_basic_map_n_param(bmap1);
4500 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
4501 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4502 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
4503 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4504 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
4505 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4506 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
4507 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
4508 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
4509 isl_dim_map_div(dim_map1, bmap1, pos += out2);
4510 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4512 bmap = isl_basic_map_alloc_dim(dim_result,
4513 bmap1->n_div + bmap2->n_div,
4514 bmap1->n_eq + bmap2->n_eq,
4515 bmap1->n_ineq + bmap2->n_ineq);
4516 bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
4517 bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
4518 bmap = isl_basic_map_simplify(bmap);
4519 return isl_basic_map_finalize(bmap);
4521 isl_basic_map_free(bmap1);
4522 isl_basic_map_free(bmap2);
4526 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
4528 struct isl_map *isl_map_product(struct isl_map *map1, struct isl_map *map2)
4531 struct isl_map *result;
4537 isl_assert(map1->ctx, isl_dim_match(map1->dim, isl_dim_param,
4538 map2->dim, isl_dim_param), goto error);
4540 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
4541 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
4542 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
4544 result = isl_map_alloc_dim(isl_dim_product(isl_dim_copy(map1->dim),
4545 isl_dim_copy(map2->dim)),
4546 map1->n * map2->n, flags);
4549 for (i = 0; i < map1->n; ++i)
4550 for (j = 0; j < map2->n; ++j) {
4551 struct isl_basic_map *part;
4552 part = isl_basic_map_product(
4553 isl_basic_map_copy(map1->p[i]),
4554 isl_basic_map_copy(map2->p[j]));
4555 if (isl_basic_map_is_empty(part))
4556 isl_basic_map_free(part);
4558 result = isl_map_add(result, part);
4571 /* Given two set A and B, construct its Cartesian product A x B.
4573 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
4575 return (struct isl_set *)isl_map_product((struct isl_map *)set1,
4576 (struct isl_map *)set2);
4579 uint32_t isl_basic_set_get_hash(struct isl_basic_set *bset)
4587 bset = isl_basic_set_copy(bset);
4588 bset = isl_basic_set_normalize(bset);
4591 total = isl_basic_set_total_dim(bset);
4592 isl_hash_byte(hash, bset->n_eq & 0xFF);
4593 for (i = 0; i < bset->n_eq; ++i) {
4595 c_hash = isl_seq_get_hash(bset->eq[i], 1 + total);
4596 isl_hash_hash(hash, c_hash);
4598 isl_hash_byte(hash, bset->n_ineq & 0xFF);
4599 for (i = 0; i < bset->n_ineq; ++i) {
4601 c_hash = isl_seq_get_hash(bset->ineq[i], 1 + total);
4602 isl_hash_hash(hash, c_hash);
4604 isl_hash_byte(hash, bset->n_div & 0xFF);
4605 for (i = 0; i < bset->n_div; ++i) {
4607 if (isl_int_is_zero(bset->div[i][0]))
4609 isl_hash_byte(hash, i & 0xFF);
4610 c_hash = isl_seq_get_hash(bset->div[i], 1 + 1 + total);
4611 isl_hash_hash(hash, c_hash);
4613 isl_basic_set_free(bset);
4617 uint32_t isl_set_get_hash(struct isl_set *set)
4624 set = isl_set_copy(set);
4625 set = isl_set_normalize(set);
4629 hash = isl_hash_init();
4630 for (i = 0; i < set->n; ++i) {
4632 bset_hash = isl_basic_set_get_hash(set->p[i]);
4633 isl_hash_hash(hash, bset_hash);
4641 /* Check if the value for dimension dim is completely determined
4642 * by the values of the other parameters and variables.
4643 * That is, check if dimension dim is involved in an equality.
4645 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
4652 nparam = isl_basic_set_n_param(bset);
4653 for (i = 0; i < bset->n_eq; ++i)
4654 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
4659 /* Check if the value for dimension dim is completely determined
4660 * by the values of the other parameters and variables.
4661 * That is, check if dimension dim is involved in an equality
4662 * for each of the subsets.
4664 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
4670 for (i = 0; i < set->n; ++i) {
4672 unique = isl_basic_set_dim_is_unique(set->p[i], dim);