6 #include "isl_equalities.h"
12 #include "isl_map_private.h"
13 #include "isl_map_piplib.h"
14 #include "isl_sample.h"
18 /* Maps dst positions to src positions */
24 static struct isl_dim_map
*isl_dim_map_alloc(struct isl_ctx
*ctx
, unsigned len
)
27 struct isl_dim_map
*dim_map
;
28 dim_map
= isl_alloc(ctx
, struct isl_dim_map
,
29 sizeof(struct isl_dim_map
) + len
* sizeof(int));
32 dim_map
->len
= 1 + len
;
34 for (i
= 0; i
< len
; ++i
)
35 dim_map
->pos
[1 + i
] = -1;
39 static unsigned n(struct isl_dim
*dim
, enum isl_dim_type type
)
42 case isl_dim_param
: return dim
->nparam
;
43 case isl_dim_in
: return dim
->n_in
;
44 case isl_dim_out
: return dim
->n_out
;
45 case isl_dim_all
: return dim
->nparam
+ dim
->n_in
+ dim
->n_out
;
49 static unsigned pos(struct isl_dim
*dim
, enum isl_dim_type type
)
52 case isl_dim_param
: return 1;
53 case isl_dim_in
: return 1 + dim
->nparam
;
54 case isl_dim_out
: return 1 + dim
->nparam
+ dim
->n_in
;
58 static void isl_dim_map_dim(struct isl_dim_map
*dim_map
, struct isl_dim
*dim
,
59 enum isl_dim_type type
, unsigned dst_pos
)
67 src_pos
= pos(dim
, type
);
68 for (i
= 0; i
< n(dim
, type
); ++i
)
69 dim_map
->pos
[1 + dst_pos
+ i
] = src_pos
+ i
;
72 static void isl_dim_map_div(struct isl_dim_map
*dim_map
,
73 struct isl_basic_map
*bmap
, unsigned dst_pos
)
78 if (!dim_map
|| !bmap
)
81 src_pos
= 1 + isl_dim_total(bmap
->dim
);
82 for (i
= 0; i
< bmap
->n_div
; ++i
)
83 dim_map
->pos
[1 + dst_pos
+ i
] = src_pos
+ i
;
86 static void isl_dim_map_dump(struct isl_dim_map
*dim_map
)
90 for (i
= 0; i
< dim_map
->len
; ++i
)
91 fprintf(stderr
, "%d -> %d; ", i
, dim_map
->pos
[i
]);
92 fprintf(stderr
, "\n");
95 unsigned isl_basic_map_dim(const struct isl_basic_map
*bmap
,
96 enum isl_dim_type type
)
98 struct isl_dim
*dim
= bmap
->dim
;
102 case isl_dim_out
: return isl_dim_size(bmap
->dim
, type
);
103 case isl_dim_div
: return bmap
->n_div
;
104 case isl_dim_all
: return isl_basic_map_total_dim(bmap
);
108 unsigned isl_map_dim(const struct isl_map
*map
, enum isl_dim_type type
)
110 return n(map
->dim
, type
);
113 unsigned isl_set_dim(const struct isl_set
*set
, enum isl_dim_type type
)
115 return n(set
->dim
, type
);
118 unsigned isl_basic_map_offset(struct isl_basic_map
*bmap
,
119 enum isl_dim_type type
)
121 struct isl_dim
*dim
= bmap
->dim
;
123 case isl_dim_param
: return 1;
124 case isl_dim_in
: return 1 + dim
->nparam
;
125 case isl_dim_out
: return 1 + dim
->nparam
+ dim
->n_in
;
126 case isl_dim_div
: return 1 + dim
->nparam
+ dim
->n_in
+ dim
->n_out
;
130 static unsigned map_offset(struct isl_map
*map
, enum isl_dim_type type
)
132 return pos(map
->dim
, type
);
135 unsigned isl_basic_set_dim(const struct isl_basic_set
*bset
,
136 enum isl_dim_type type
)
138 return isl_basic_map_dim((const struct isl_basic_map
*)bset
, type
);
141 unsigned isl_basic_set_n_dim(const struct isl_basic_set
*bset
)
143 return bset
->dim
->n_out
;
146 unsigned isl_basic_set_n_param(const struct isl_basic_set
*bset
)
148 return bset
->dim
->nparam
;
151 unsigned isl_basic_set_total_dim(const struct isl_basic_set
*bset
)
153 return isl_dim_total(bset
->dim
) + bset
->n_div
;
156 unsigned isl_set_n_dim(const struct isl_set
*set
)
158 return set
->dim
->n_out
;
161 unsigned isl_set_n_param(const struct isl_set
*set
)
163 return set
->dim
->nparam
;
166 unsigned isl_basic_map_n_in(const struct isl_basic_map
*bmap
)
168 return bmap
->dim
->n_in
;
171 unsigned isl_basic_map_n_out(const struct isl_basic_map
*bmap
)
173 return bmap
->dim
->n_out
;
176 unsigned isl_basic_map_n_param(const struct isl_basic_map
*bmap
)
178 return bmap
->dim
->nparam
;
181 unsigned isl_basic_map_n_div(const struct isl_basic_map
*bmap
)
186 unsigned isl_basic_map_total_dim(const struct isl_basic_map
*bmap
)
188 return isl_dim_total(bmap
->dim
) + bmap
->n_div
;
191 unsigned isl_map_n_in(const struct isl_map
*map
)
193 return map
->dim
->n_in
;
196 unsigned isl_map_n_out(const struct isl_map
*map
)
198 return map
->dim
->n_out
;
201 unsigned isl_map_n_param(const struct isl_map
*map
)
203 return map
->dim
->nparam
;
206 int isl_map_compatible_domain(struct isl_map
*map
, struct isl_set
*set
)
208 return map
->dim
->n_in
== set
->dim
->n_out
&&
209 map
->dim
->nparam
== set
->dim
->nparam
;
212 int isl_basic_map_compatible_domain(struct isl_basic_map
*bmap
,
213 struct isl_basic_set
*bset
)
215 return bmap
->dim
->n_in
== bset
->dim
->n_out
&&
216 bmap
->dim
->nparam
== bset
->dim
->nparam
;
219 int isl_basic_map_compatible_range(struct isl_basic_map
*bmap
,
220 struct isl_basic_set
*bset
)
222 return bmap
->dim
->n_out
== bset
->dim
->n_out
&&
223 bmap
->dim
->nparam
== bset
->dim
->nparam
;
226 struct isl_dim
*isl_basic_map_get_dim(struct isl_basic_map
*bmap
)
230 return isl_dim_copy(bmap
->dim
);
233 struct isl_dim
*isl_basic_set_get_dim(struct isl_basic_set
*bset
)
237 return isl_dim_copy(bset
->dim
);
240 struct isl_dim
*isl_map_get_dim(struct isl_map
*map
)
244 return isl_dim_copy(map
->dim
);
247 struct isl_dim
*isl_set_get_dim(struct isl_set
*set
)
251 return isl_dim_copy(set
->dim
);
254 static struct isl_basic_map
*basic_map_init(struct isl_ctx
*ctx
,
255 struct isl_basic_map
*bmap
, unsigned extra
,
256 unsigned n_eq
, unsigned n_ineq
)
259 size_t row_size
= 1 + isl_dim_total(bmap
->dim
) + extra
;
261 bmap
->block
= isl_blk_alloc(ctx
, (n_ineq
+ n_eq
) * row_size
);
262 if (isl_blk_is_error(bmap
->block
)) {
267 bmap
->ineq
= isl_alloc_array(ctx
, isl_int
*, n_ineq
+ n_eq
);
269 isl_blk_free(ctx
, bmap
->block
);
275 bmap
->block2
= isl_blk_empty();
278 bmap
->block2
= isl_blk_alloc(ctx
, extra
* (1 + row_size
));
279 if (isl_blk_is_error(bmap
->block2
)) {
281 isl_blk_free(ctx
, bmap
->block
);
286 bmap
->div
= isl_alloc_array(ctx
, isl_int
*, extra
);
288 isl_blk_free(ctx
, bmap
->block2
);
290 isl_blk_free(ctx
, bmap
->block
);
296 for (i
= 0; i
< n_ineq
+ n_eq
; ++i
)
297 bmap
->ineq
[i
] = bmap
->block
.data
+ i
* row_size
;
299 for (i
= 0; i
< extra
; ++i
)
300 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + row_size
);
306 bmap
->c_size
= n_eq
+ n_ineq
;
307 bmap
->eq
= bmap
->ineq
+ n_ineq
;
316 isl_basic_map_free(bmap
);
320 struct isl_basic_set
*isl_basic_set_alloc(struct isl_ctx
*ctx
,
321 unsigned nparam
, unsigned dim
, unsigned extra
,
322 unsigned n_eq
, unsigned n_ineq
)
324 struct isl_basic_map
*bmap
;
325 bmap
= isl_basic_map_alloc(ctx
, nparam
, 0, dim
, extra
, n_eq
, n_ineq
);
326 return (struct isl_basic_set
*)bmap
;
329 struct isl_basic_set
*isl_basic_set_alloc_dim(struct isl_dim
*dim
,
330 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
332 struct isl_basic_map
*bmap
;
335 isl_assert(dim
->ctx
, dim
->n_in
== 0, return NULL
);
336 bmap
= isl_basic_map_alloc_dim(dim
, extra
, n_eq
, n_ineq
);
337 return (struct isl_basic_set
*)bmap
;
340 struct isl_basic_map
*isl_basic_map_alloc_dim(struct isl_dim
*dim
,
341 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
343 struct isl_basic_map
*bmap
;
347 bmap
= isl_alloc_type(dim
->ctx
, struct isl_basic_map
);
352 return basic_map_init(dim
->ctx
, bmap
, extra
, n_eq
, n_ineq
);
358 struct isl_basic_map
*isl_basic_map_alloc(struct isl_ctx
*ctx
,
359 unsigned nparam
, unsigned in
, unsigned out
, unsigned extra
,
360 unsigned n_eq
, unsigned n_ineq
)
362 struct isl_basic_map
*bmap
;
365 dim
= isl_dim_alloc(ctx
, nparam
, in
, out
);
369 bmap
= isl_basic_map_alloc_dim(dim
, extra
, n_eq
, n_ineq
);
373 static void dup_constraints(
374 struct isl_basic_map
*dst
, struct isl_basic_map
*src
)
377 unsigned total
= isl_basic_map_total_dim(src
);
379 for (i
= 0; i
< src
->n_eq
; ++i
) {
380 int j
= isl_basic_map_alloc_equality(dst
);
381 isl_seq_cpy(dst
->eq
[j
], src
->eq
[i
], 1+total
);
384 for (i
= 0; i
< src
->n_ineq
; ++i
) {
385 int j
= isl_basic_map_alloc_inequality(dst
);
386 isl_seq_cpy(dst
->ineq
[j
], src
->ineq
[i
], 1+total
);
389 for (i
= 0; i
< src
->n_div
; ++i
) {
390 int j
= isl_basic_map_alloc_div(dst
);
391 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
);
393 ISL_F_SET(dst
, ISL_BASIC_SET_FINAL
);
396 struct isl_basic_map
*isl_basic_map_dup(struct isl_basic_map
*bmap
)
398 struct isl_basic_map
*dup
;
402 dup
= isl_basic_map_alloc_dim(isl_dim_copy(bmap
->dim
),
403 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
406 dup_constraints(dup
, bmap
);
407 dup
->flags
= bmap
->flags
;
408 dup
->sample
= isl_vec_copy(bmap
->sample
);
412 struct isl_basic_set
*isl_basic_set_dup(struct isl_basic_set
*bset
)
414 struct isl_basic_map
*dup
;
416 dup
= isl_basic_map_dup((struct isl_basic_map
*)bset
);
417 return (struct isl_basic_set
*)dup
;
420 struct isl_basic_set
*isl_basic_set_copy(struct isl_basic_set
*bset
)
425 if (ISL_F_ISSET(bset
, ISL_BASIC_SET_FINAL
)) {
429 return isl_basic_set_dup(bset
);
432 struct isl_set
*isl_set_copy(struct isl_set
*set
)
441 struct isl_basic_map
*isl_basic_map_copy(struct isl_basic_map
*bmap
)
446 if (ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
)) {
450 return isl_basic_map_dup(bmap
);
453 struct isl_map
*isl_map_copy(struct isl_map
*map
)
462 void isl_basic_map_free(struct isl_basic_map
*bmap
)
470 isl_ctx_deref(bmap
->ctx
);
472 isl_blk_free(bmap
->ctx
, bmap
->block2
);
474 isl_blk_free(bmap
->ctx
, bmap
->block
);
475 isl_vec_free(bmap
->sample
);
476 isl_dim_free(bmap
->dim
);
480 void isl_basic_set_free(struct isl_basic_set
*bset
)
482 isl_basic_map_free((struct isl_basic_map
*)bset
);
485 static int room_for_con(struct isl_basic_map
*bmap
, unsigned n
)
487 return bmap
->n_eq
+ bmap
->n_ineq
+ n
<= bmap
->c_size
;
490 int isl_basic_map_alloc_equality(struct isl_basic_map
*bmap
)
496 isl_assert(ctx
, room_for_con(bmap
, 1), return -1);
497 isl_assert(ctx
, (bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
<= bmap
->c_size
,
499 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
500 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
501 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
502 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
503 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
504 if ((bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
== bmap
->c_size
) {
506 int j
= isl_basic_map_alloc_inequality(bmap
);
510 bmap
->ineq
[j
] = bmap
->ineq
[bmap
->n_ineq
- 1];
511 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
518 isl_seq_clr(bmap
->eq
[bmap
->n_eq
] + 1 + isl_basic_map_total_dim(bmap
),
519 bmap
->extra
- bmap
->n_div
);
523 int isl_basic_set_alloc_equality(struct isl_basic_set
*bset
)
525 return isl_basic_map_alloc_equality((struct isl_basic_map
*)bset
);
528 int isl_basic_map_free_equality(struct isl_basic_map
*bmap
, unsigned n
)
532 isl_assert(bmap
->ctx
, n
<= bmap
->n_eq
, return -1);
537 int isl_basic_set_free_equality(struct isl_basic_set
*bset
, unsigned n
)
539 return isl_basic_map_free_equality((struct isl_basic_map
*)bset
, n
);
542 int isl_basic_map_drop_equality(struct isl_basic_map
*bmap
, unsigned pos
)
547 isl_assert(bmap
->ctx
, pos
< bmap
->n_eq
, return -1);
549 if (pos
!= bmap
->n_eq
- 1) {
551 bmap
->eq
[pos
] = bmap
->eq
[bmap
->n_eq
- 1];
552 bmap
->eq
[bmap
->n_eq
- 1] = t
;
558 int isl_basic_set_drop_equality(struct isl_basic_set
*bset
, unsigned pos
)
560 return isl_basic_map_drop_equality((struct isl_basic_map
*)bset
, pos
);
563 void isl_basic_map_inequality_to_equality(
564 struct isl_basic_map
*bmap
, unsigned pos
)
569 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
570 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
575 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
576 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
577 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
578 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
581 static int room_for_ineq(struct isl_basic_map
*bmap
, unsigned n
)
583 return bmap
->n_ineq
+ n
<= bmap
->eq
- bmap
->ineq
;
586 int isl_basic_map_alloc_inequality(struct isl_basic_map
*bmap
)
592 isl_assert(ctx
, room_for_ineq(bmap
, 1), return -1);
593 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
594 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
595 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
596 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
597 isl_seq_clr(bmap
->ineq
[bmap
->n_ineq
] +
598 1 + isl_basic_map_total_dim(bmap
),
599 bmap
->extra
- bmap
->n_div
);
600 return bmap
->n_ineq
++;
603 int isl_basic_set_alloc_inequality(struct isl_basic_set
*bset
)
605 return isl_basic_map_alloc_inequality((struct isl_basic_map
*)bset
);
608 int isl_basic_map_free_inequality(struct isl_basic_map
*bmap
, unsigned n
)
612 isl_assert(bmap
->ctx
, n
<= bmap
->n_ineq
, return -1);
617 int isl_basic_set_free_inequality(struct isl_basic_set
*bset
, unsigned n
)
619 return isl_basic_map_free_inequality((struct isl_basic_map
*)bset
, n
);
622 int isl_basic_map_drop_inequality(struct isl_basic_map
*bmap
, unsigned pos
)
627 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
629 if (pos
!= bmap
->n_ineq
- 1) {
631 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
632 bmap
->ineq
[bmap
->n_ineq
- 1] = t
;
633 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
639 int isl_basic_set_drop_inequality(struct isl_basic_set
*bset
, unsigned pos
)
641 return isl_basic_map_drop_inequality((struct isl_basic_map
*)bset
, pos
);
644 int isl_basic_map_alloc_div(struct isl_basic_map
*bmap
)
648 isl_assert(bmap
->ctx
, bmap
->n_div
< bmap
->extra
, return -1);
649 isl_seq_clr(bmap
->div
[bmap
->n_div
] +
650 1 + 1 + isl_basic_map_total_dim(bmap
),
651 bmap
->extra
- bmap
->n_div
);
652 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
653 return bmap
->n_div
++;
656 int isl_basic_set_alloc_div(struct isl_basic_set
*bset
)
658 return isl_basic_map_alloc_div((struct isl_basic_map
*)bset
);
661 int isl_basic_map_free_div(struct isl_basic_map
*bmap
, unsigned n
)
665 isl_assert(bmap
->ctx
, n
<= bmap
->n_div
, return -1);
670 int isl_basic_set_free_div(struct isl_basic_set
*bset
, unsigned n
)
672 return isl_basic_map_free_div((struct isl_basic_map
*)bset
, n
);
675 /* Copy constraint from src to dst, putting the vars of src at offset
676 * dim_off in dst and the divs of src at offset div_off in dst.
677 * If both sets are actually map, then dim_off applies to the input
680 static void copy_constraint(struct isl_basic_map
*dst_map
, isl_int
*dst
,
681 struct isl_basic_map
*src_map
, isl_int
*src
,
682 unsigned in_off
, unsigned out_off
, unsigned div_off
)
684 unsigned src_nparam
= isl_basic_map_n_param(src_map
);
685 unsigned dst_nparam
= isl_basic_map_n_param(dst_map
);
686 unsigned src_in
= isl_basic_map_n_in(src_map
);
687 unsigned dst_in
= isl_basic_map_n_in(dst_map
);
688 unsigned src_out
= isl_basic_map_n_out(src_map
);
689 unsigned dst_out
= isl_basic_map_n_out(dst_map
);
690 isl_int_set(dst
[0], src
[0]);
691 isl_seq_cpy(dst
+1, src
+1, isl_min(dst_nparam
, src_nparam
));
692 if (dst_nparam
> src_nparam
)
693 isl_seq_clr(dst
+1+src_nparam
,
694 dst_nparam
- src_nparam
);
695 isl_seq_clr(dst
+1+dst_nparam
, in_off
);
696 isl_seq_cpy(dst
+1+dst_nparam
+in_off
,
698 isl_min(dst_in
-in_off
, src_in
));
699 if (dst_in
-in_off
> src_in
)
700 isl_seq_clr(dst
+1+dst_nparam
+in_off
+src_in
,
701 dst_in
- in_off
- src_in
);
702 isl_seq_clr(dst
+1+dst_nparam
+dst_in
, out_off
);
703 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+out_off
,
704 src
+1+src_nparam
+src_in
,
705 isl_min(dst_out
-out_off
, src_out
));
706 if (dst_out
-out_off
> src_out
)
707 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+out_off
+src_out
,
708 dst_out
- out_off
- src_out
);
709 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
, div_off
);
710 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+dst_out
+div_off
,
711 src
+1+src_nparam
+src_in
+src_out
,
712 isl_min(dst_map
->extra
-div_off
, src_map
->n_div
));
713 if (dst_map
->n_div
-div_off
> src_map
->n_div
)
714 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
+
715 div_off
+src_map
->n_div
,
716 dst_map
->n_div
- div_off
- src_map
->n_div
);
719 static void copy_div(struct isl_basic_map
*dst_map
, isl_int
*dst
,
720 struct isl_basic_map
*src_map
, isl_int
*src
,
721 unsigned in_off
, unsigned out_off
, unsigned div_off
)
723 isl_int_set(dst
[0], src
[0]);
724 copy_constraint(dst_map
, dst
+1, src_map
, src
+1, in_off
, out_off
, div_off
);
727 static struct isl_basic_map
*add_constraints(struct isl_basic_map
*bmap1
,
728 struct isl_basic_map
*bmap2
, unsigned i_pos
, unsigned o_pos
)
733 if (!bmap1
|| !bmap2
)
736 div_off
= bmap1
->n_div
;
738 for (i
= 0; i
< bmap2
->n_eq
; ++i
) {
739 int i1
= isl_basic_map_alloc_equality(bmap1
);
742 copy_constraint(bmap1
, bmap1
->eq
[i1
], bmap2
, bmap2
->eq
[i
],
743 i_pos
, o_pos
, div_off
);
746 for (i
= 0; i
< bmap2
->n_ineq
; ++i
) {
747 int i1
= isl_basic_map_alloc_inequality(bmap1
);
750 copy_constraint(bmap1
, bmap1
->ineq
[i1
], bmap2
, bmap2
->ineq
[i
],
751 i_pos
, o_pos
, div_off
);
754 for (i
= 0; i
< bmap2
->n_div
; ++i
) {
755 int i1
= isl_basic_map_alloc_div(bmap1
);
758 copy_div(bmap1
, bmap1
->div
[i1
], bmap2
, bmap2
->div
[i
],
759 i_pos
, o_pos
, div_off
);
762 isl_basic_map_free(bmap2
);
767 isl_basic_map_free(bmap1
);
768 isl_basic_map_free(bmap2
);
772 static void copy_constraint_dim_map(isl_int
*dst
, isl_int
*src
,
773 struct isl_dim_map
*dim_map
)
777 for (i
= 0; i
< dim_map
->len
; ++i
) {
778 if (dim_map
->pos
[i
] < 0)
779 isl_int_set_si(dst
[i
], 0);
781 isl_int_set(dst
[i
], src
[dim_map
->pos
[i
]]);
785 static void copy_div_dim_map(isl_int
*dst
, isl_int
*src
,
786 struct isl_dim_map
*dim_map
)
788 isl_int_set(dst
[0], src
[0]);
789 copy_constraint_dim_map(dst
+1, src
+1, dim_map
);
792 static struct isl_basic_map
*add_constraints_dim_map(struct isl_basic_map
*dst
,
793 struct isl_basic_map
*src
, struct isl_dim_map
*dim_map
)
797 if (!src
|| !dst
|| !dim_map
)
800 for (i
= 0; i
< src
->n_eq
; ++i
) {
801 int i1
= isl_basic_map_alloc_equality(dst
);
804 copy_constraint_dim_map(dst
->eq
[i1
], src
->eq
[i
], dim_map
);
807 for (i
= 0; i
< src
->n_ineq
; ++i
) {
808 int i1
= isl_basic_map_alloc_inequality(dst
);
811 copy_constraint_dim_map(dst
->ineq
[i1
], src
->ineq
[i
], dim_map
);
814 for (i
= 0; i
< src
->n_div
; ++i
) {
815 int i1
= isl_basic_map_alloc_div(dst
);
818 copy_div_dim_map(dst
->div
[i1
], src
->div
[i
], dim_map
);
822 isl_basic_map_free(src
);
827 isl_basic_map_free(src
);
828 isl_basic_map_free(dst
);
832 struct isl_basic_set
*isl_basic_set_add_constraints(struct isl_basic_set
*bset1
,
833 struct isl_basic_set
*bset2
, unsigned pos
)
835 return (struct isl_basic_set
*)
836 add_constraints((struct isl_basic_map
*)bset1
,
837 (struct isl_basic_map
*)bset2
, 0, pos
);
840 struct isl_basic_map
*isl_basic_map_extend_dim(struct isl_basic_map
*base
,
841 struct isl_dim
*dim
, unsigned extra
,
842 unsigned n_eq
, unsigned n_ineq
)
844 struct isl_basic_map
*ext
;
854 dims_ok
= isl_dim_equal(base
->dim
, dim
) &&
855 base
->extra
>= base
->n_div
+ extra
;
857 if (dims_ok
&& room_for_con(base
, n_eq
+ n_ineq
) &&
858 room_for_ineq(base
, n_ineq
)) {
863 isl_assert(base
->ctx
, base
->dim
->nparam
<= dim
->nparam
, goto error
);
864 isl_assert(base
->ctx
, base
->dim
->n_in
<= dim
->n_in
, goto error
);
865 isl_assert(base
->ctx
, base
->dim
->n_out
<= dim
->n_out
, goto error
);
866 extra
+= base
->extra
;
868 n_ineq
+= base
->n_ineq
;
870 ext
= isl_basic_map_alloc_dim(dim
, extra
, n_eq
, n_ineq
);
876 ext
->sample
= isl_vec_copy(base
->sample
);
878 ext
= add_constraints(ext
, base
, 0, 0);
881 ISL_F_CLR(ext
, ISL_BASIC_SET_FINAL
);
888 isl_basic_map_free(base
);
892 struct isl_basic_set
*isl_basic_set_extend_dim(struct isl_basic_set
*base
,
893 struct isl_dim
*dim
, unsigned extra
,
894 unsigned n_eq
, unsigned n_ineq
)
896 return (struct isl_basic_set
*)
897 isl_basic_map_extend_dim((struct isl_basic_map
*)base
, dim
,
898 extra
, n_eq
, n_ineq
);
901 struct isl_basic_map
*isl_basic_map_extend_constraints(
902 struct isl_basic_map
*base
, unsigned n_eq
, unsigned n_ineq
)
906 return isl_basic_map_extend_dim(base
, isl_dim_copy(base
->dim
),
910 struct isl_basic_map
*isl_basic_map_extend(struct isl_basic_map
*base
,
911 unsigned nparam
, unsigned n_in
, unsigned n_out
, unsigned extra
,
912 unsigned n_eq
, unsigned n_ineq
)
914 struct isl_basic_map
*bmap
;
919 dim
= isl_dim_alloc(base
->ctx
, nparam
, n_in
, n_out
);
923 bmap
= isl_basic_map_extend_dim(base
, dim
, extra
, n_eq
, n_ineq
);
927 struct isl_basic_set
*isl_basic_set_extend(struct isl_basic_set
*base
,
928 unsigned nparam
, unsigned dim
, unsigned extra
,
929 unsigned n_eq
, unsigned n_ineq
)
931 return (struct isl_basic_set
*)
932 isl_basic_map_extend((struct isl_basic_map
*)base
,
933 nparam
, 0, dim
, extra
, n_eq
, n_ineq
);
936 struct isl_basic_set
*isl_basic_set_extend_constraints(
937 struct isl_basic_set
*base
, unsigned n_eq
, unsigned n_ineq
)
939 return (struct isl_basic_set
*)
940 isl_basic_map_extend_constraints((struct isl_basic_map
*)base
,
944 struct isl_basic_set
*isl_basic_set_cow(struct isl_basic_set
*bset
)
946 return (struct isl_basic_set
*)
947 isl_basic_map_cow((struct isl_basic_map
*)bset
);
950 struct isl_basic_map
*isl_basic_map_cow(struct isl_basic_map
*bmap
)
957 bmap
= isl_basic_map_dup(bmap
);
959 ISL_F_CLR(bmap
, ISL_BASIC_SET_FINAL
);
963 struct isl_set
*isl_set_cow(struct isl_set
*set
)
971 return isl_set_dup(set
);
974 struct isl_map
*isl_map_cow(struct isl_map
*map
)
982 return isl_map_dup(map
);
985 static void swap_vars(struct isl_blk blk
, isl_int
*a
,
986 unsigned a_len
, unsigned b_len
)
988 isl_seq_cpy(blk
.data
, a
+a_len
, b_len
);
989 isl_seq_cpy(blk
.data
+b_len
, a
, a_len
);
990 isl_seq_cpy(a
, blk
.data
, b_len
+a_len
);
993 struct isl_basic_set
*isl_basic_set_swap_vars(
994 struct isl_basic_set
*bset
, unsigned n
)
1004 nparam
= isl_basic_set_n_param(bset
);
1005 dim
= isl_basic_set_n_dim(bset
);
1006 isl_assert(bset
->ctx
, n
<= dim
, goto error
);
1011 bset
= isl_basic_set_cow(bset
);
1015 blk
= isl_blk_alloc(bset
->ctx
, dim
);
1016 if (isl_blk_is_error(blk
))
1019 for (i
= 0; i
< bset
->n_eq
; ++i
)
1021 bset
->eq
[i
]+1+nparam
, n
, dim
- n
);
1023 for (i
= 0; i
< bset
->n_ineq
; ++i
)
1025 bset
->ineq
[i
]+1+nparam
, n
, dim
- n
);
1027 for (i
= 0; i
< bset
->n_div
; ++i
)
1029 bset
->div
[i
]+1+1+nparam
, n
, dim
- n
);
1031 isl_blk_free(bset
->ctx
, blk
);
1033 ISL_F_CLR(bset
, ISL_BASIC_SET_NORMALIZED
);
1037 isl_basic_set_free(bset
);
1041 struct isl_set
*isl_set_swap_vars(struct isl_set
*set
, unsigned n
)
1044 set
= isl_set_cow(set
);
1048 for (i
= 0; i
< set
->n
; ++i
) {
1049 set
->p
[i
] = isl_basic_set_swap_vars(set
->p
[i
], n
);
1055 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
1059 struct isl_basic_map
*isl_basic_map_set_to_empty(struct isl_basic_map
*bmap
)
1065 total
= isl_basic_map_total_dim(bmap
);
1066 isl_basic_map_free_div(bmap
, bmap
->n_div
);
1067 isl_basic_map_free_inequality(bmap
, bmap
->n_ineq
);
1069 isl_basic_map_free_equality(bmap
, bmap
->n_eq
-1);
1071 isl_basic_map_alloc_equality(bmap
);
1075 isl_int_set_si(bmap
->eq
[i
][0], 1);
1076 isl_seq_clr(bmap
->eq
[i
]+1, total
);
1077 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
1078 return isl_basic_map_finalize(bmap
);
1080 isl_basic_map_free(bmap
);
1084 struct isl_basic_set
*isl_basic_set_set_to_empty(struct isl_basic_set
*bset
)
1086 return (struct isl_basic_set
*)
1087 isl_basic_map_set_to_empty((struct isl_basic_map
*)bset
);
1090 void isl_basic_map_swap_div(struct isl_basic_map
*bmap
, int a
, int b
)
1093 unsigned off
= isl_dim_total(bmap
->dim
);
1094 isl_int
*t
= bmap
->div
[a
];
1095 bmap
->div
[a
] = bmap
->div
[b
];
1098 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1099 isl_int_swap(bmap
->eq
[i
][1+off
+a
], bmap
->eq
[i
][1+off
+b
]);
1101 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1102 isl_int_swap(bmap
->ineq
[i
][1+off
+a
], bmap
->ineq
[i
][1+off
+b
]);
1104 for (i
= 0; i
< bmap
->n_div
; ++i
)
1105 isl_int_swap(bmap
->div
[i
][1+1+off
+a
], bmap
->div
[i
][1+1+off
+b
]);
1106 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1109 /* Eliminate the specified n dimensions starting at first from the
1110 * constraints using Fourier-Motzkin, The dimensions themselves
1113 struct isl_set
*isl_set_eliminate_dims(struct isl_set
*set
,
1114 unsigned first
, unsigned n
)
1124 set
= isl_set_cow(set
);
1127 isl_assert(set
->ctx
, first
+n
<= isl_set_n_dim(set
), goto error
);
1128 nparam
= isl_set_n_param(set
);
1130 for (i
= 0; i
< set
->n
; ++i
) {
1131 set
->p
[i
] = isl_basic_set_eliminate_vars(set
->p
[i
],
1142 /* Project out n dimensions starting at first using Fourier-Motzkin */
1143 struct isl_set
*isl_set_remove_dims(struct isl_set
*set
,
1144 unsigned first
, unsigned n
)
1146 set
= isl_set_eliminate_dims(set
, first
, n
);
1147 set
= isl_set_drop_dims(set
, first
, n
);
1151 struct isl_basic_set
*isl_basic_set_remove_divs(struct isl_basic_set
*bset
)
1153 bset
= isl_basic_set_eliminate_vars(bset
, isl_dim_total(bset
->dim
),
1161 struct isl_set
*isl_set_remove_divs(struct isl_set
*set
)
1170 set
= isl_set_cow(set
);
1174 for (i
= 0; i
< set
->n
; ++i
) {
1175 set
->p
[i
] = isl_basic_set_remove_divs(set
->p
[i
]);
1185 struct isl_basic_map
*isl_basic_map_remove(struct isl_basic_map
*bmap
,
1186 enum isl_dim_type type
, unsigned first
, unsigned n
)
1190 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
1194 bmap
= isl_basic_map_eliminate_vars(bmap
,
1195 isl_basic_map_offset(bmap
, type
) - 1 + first
, n
);
1198 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
) && type
== isl_dim_div
)
1200 bmap
= isl_basic_map_drop(bmap
, type
, first
, n
);
1203 isl_basic_map_free(bmap
);
1207 struct isl_map
*isl_map_remove(struct isl_map
*map
,
1208 enum isl_dim_type type
, unsigned first
, unsigned n
)
1216 map
= isl_map_cow(map
);
1219 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
1221 for (i
= 0; i
< map
->n
; ++i
) {
1222 map
->p
[i
] = isl_basic_map_eliminate_vars(map
->p
[i
],
1223 isl_basic_map_offset(map
->p
[i
], type
) - 1 + first
, n
);
1227 map
= isl_map_drop(map
, type
, first
, n
);
1234 /* Project out n inputs starting at first using Fourier-Motzkin */
1235 struct isl_map
*isl_map_remove_inputs(struct isl_map
*map
,
1236 unsigned first
, unsigned n
)
1238 return isl_map_remove(map
, isl_dim_in
, first
, n
);
1241 /* Project out n dimensions starting at first using Fourier-Motzkin */
1242 struct isl_basic_set
*isl_basic_set_remove_dims(struct isl_basic_set
*bset
,
1243 unsigned first
, unsigned n
)
1245 unsigned nparam
= isl_basic_set_n_param(bset
);
1246 bset
= isl_basic_set_eliminate_vars(bset
, nparam
+ first
, n
);
1247 bset
= isl_basic_set_drop_dims(bset
, first
, n
);
1251 static void dump_term(struct isl_basic_map
*bmap
,
1252 isl_int c
, int pos
, FILE *out
)
1255 unsigned in
= isl_basic_map_n_in(bmap
);
1256 unsigned dim
= in
+ isl_basic_map_n_out(bmap
);
1257 unsigned nparam
= isl_basic_map_n_param(bmap
);
1259 isl_int_print(out
, c
, 0);
1261 if (!isl_int_is_one(c
))
1262 isl_int_print(out
, c
, 0);
1263 if (pos
< 1 + nparam
) {
1264 name
= isl_dim_get_name(bmap
->dim
,
1265 isl_dim_param
, pos
- 1);
1267 fprintf(out
, "%s", name
);
1269 fprintf(out
, "p%d", pos
- 1);
1270 } else if (pos
< 1 + nparam
+ in
)
1271 fprintf(out
, "i%d", pos
- 1 - nparam
);
1272 else if (pos
< 1 + nparam
+ dim
)
1273 fprintf(out
, "o%d", pos
- 1 - nparam
- in
);
1275 fprintf(out
, "e%d", pos
- 1 - nparam
- dim
);
1279 static void dump_constraint_sign(struct isl_basic_map
*bmap
, isl_int
*c
,
1280 int sign
, FILE *out
)
1284 unsigned len
= 1 + isl_basic_map_total_dim(bmap
);
1288 for (i
= 0, first
= 1; i
< len
; ++i
) {
1289 if (isl_int_sgn(c
[i
]) * sign
<= 0)
1292 fprintf(out
, " + ");
1294 isl_int_abs(v
, c
[i
]);
1295 dump_term(bmap
, v
, i
, out
);
1302 static void dump_constraint(struct isl_basic_map
*bmap
, isl_int
*c
,
1303 const char *op
, FILE *out
, int indent
)
1307 fprintf(out
, "%*s", indent
, "");
1309 dump_constraint_sign(bmap
, c
, 1, out
);
1310 fprintf(out
, " %s ", op
);
1311 dump_constraint_sign(bmap
, c
, -1, out
);
1315 for (i
= bmap
->n_div
; i
< bmap
->extra
; ++i
) {
1316 if (isl_int_is_zero(c
[1+isl_dim_total(bmap
->dim
)+i
]))
1318 fprintf(out
, "%*s", indent
, "");
1319 fprintf(out
, "ERROR: unused div coefficient not zero\n");
1324 static void dump_constraints(struct isl_basic_map
*bmap
,
1325 isl_int
**c
, unsigned n
,
1326 const char *op
, FILE *out
, int indent
)
1330 for (i
= 0; i
< n
; ++i
)
1331 dump_constraint(bmap
, c
[i
], op
, out
, indent
);
1334 static void dump_affine(struct isl_basic_map
*bmap
, isl_int
*exp
, FILE *out
)
1338 unsigned total
= isl_basic_map_total_dim(bmap
);
1340 for (j
= 0; j
< 1 + total
; ++j
) {
1341 if (isl_int_is_zero(exp
[j
]))
1343 if (!first
&& isl_int_is_pos(exp
[j
]))
1345 dump_term(bmap
, exp
[j
], j
, out
);
1350 static void dump(struct isl_basic_map
*bmap
, FILE *out
, int indent
)
1354 dump_constraints(bmap
, bmap
->eq
, bmap
->n_eq
, "=", out
, indent
);
1355 dump_constraints(bmap
, bmap
->ineq
, bmap
->n_ineq
, ">=", out
, indent
);
1357 for (i
= 0; i
< bmap
->n_div
; ++i
) {
1358 fprintf(out
, "%*s", indent
, "");
1359 fprintf(out
, "e%d = [(", i
);
1360 dump_affine(bmap
, bmap
->div
[i
]+1, out
);
1362 isl_int_print(out
, bmap
->div
[i
][0], 0);
1363 fprintf(out
, "]\n");
1367 void isl_basic_set_dump(struct isl_basic_set
*bset
, FILE *out
, int indent
)
1370 fprintf(out
, "null basic set\n");
1374 fprintf(out
, "%*s", indent
, "");
1375 fprintf(out
, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
1376 bset
->ref
, bset
->dim
->nparam
, bset
->dim
->n_out
,
1377 bset
->extra
, bset
->flags
);
1378 dump((struct isl_basic_map
*)bset
, out
, indent
);
1381 void isl_basic_map_dump(struct isl_basic_map
*bmap
, FILE *out
, int indent
)
1384 fprintf(out
, "null basic map\n");
1388 fprintf(out
, "%*s", indent
, "");
1389 fprintf(out
, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
1390 "flags: %x, n_name: %d\n",
1392 bmap
->dim
->nparam
, bmap
->dim
->n_in
, bmap
->dim
->n_out
,
1393 bmap
->extra
, bmap
->flags
, bmap
->dim
->n_name
);
1394 dump(bmap
, out
, indent
);
1397 int isl_inequality_negate(struct isl_basic_map
*bmap
, unsigned pos
)
1402 total
= isl_basic_map_total_dim(bmap
);
1403 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
1404 isl_seq_neg(bmap
->ineq
[pos
], bmap
->ineq
[pos
], 1 + total
);
1405 isl_int_sub_ui(bmap
->ineq
[pos
][0], bmap
->ineq
[pos
][0], 1);
1406 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1410 struct isl_set
*isl_set_alloc_dim(struct isl_dim
*dim
, int n
, unsigned flags
)
1412 struct isl_set
*set
;
1416 isl_assert(dim
->ctx
, dim
->n_in
== 0, return NULL
);
1417 isl_assert(dim
->ctx
, n
>= 0, return NULL
);
1418 set
= isl_alloc(dim
->ctx
, struct isl_set
,
1419 sizeof(struct isl_set
) +
1420 n
* sizeof(struct isl_basic_set
*));
1424 set
->ctx
= dim
->ctx
;
1425 isl_ctx_ref(set
->ctx
);
1437 struct isl_set
*isl_set_alloc(struct isl_ctx
*ctx
,
1438 unsigned nparam
, unsigned dim
, int n
, unsigned flags
)
1440 struct isl_set
*set
;
1441 struct isl_dim
*dims
;
1443 dims
= isl_dim_alloc(ctx
, nparam
, 0, dim
);
1447 set
= isl_set_alloc_dim(dims
, n
, flags
);
1451 /* Make sure "map" has room for at least "n" more basic maps.
1453 struct isl_map
*isl_map_grow(struct isl_map
*map
, int n
)
1456 struct isl_map
*grown
= NULL
;
1460 isl_assert(map
->ctx
, n
>= 0, goto error
);
1461 if (map
->n
+ n
<= map
->size
)
1463 grown
= isl_map_alloc_dim(isl_map_get_dim(map
), map
->n
+ n
, map
->flags
);
1466 for (i
= 0; i
< map
->n
; ++i
) {
1467 grown
->p
[i
] = isl_basic_map_copy(map
->p
[i
]);
1475 isl_map_free(grown
);
1480 /* Make sure "set" has room for at least "n" more basic sets.
1482 struct isl_set
*isl_set_grow(struct isl_set
*set
, int n
)
1484 return (struct isl_set
*)isl_map_grow((struct isl_map
*)set
, n
);
1487 struct isl_set
*isl_set_dup(struct isl_set
*set
)
1490 struct isl_set
*dup
;
1495 dup
= isl_set_alloc_dim(isl_dim_copy(set
->dim
), set
->n
, set
->flags
);
1498 for (i
= 0; i
< set
->n
; ++i
)
1499 dup
= isl_set_add(dup
, isl_basic_set_copy(set
->p
[i
]));
1503 struct isl_set
*isl_set_from_basic_set(struct isl_basic_set
*bset
)
1505 struct isl_set
*set
;
1510 set
= isl_set_alloc_dim(isl_dim_copy(bset
->dim
), 1, ISL_MAP_DISJOINT
);
1512 isl_basic_set_free(bset
);
1515 return isl_set_add(set
, bset
);
1518 struct isl_map
*isl_map_from_basic_map(struct isl_basic_map
*bmap
)
1520 struct isl_map
*map
;
1525 map
= isl_map_alloc_dim(isl_dim_copy(bmap
->dim
), 1, ISL_MAP_DISJOINT
);
1527 isl_basic_map_free(bmap
);
1530 return isl_map_add(map
, bmap
);
1533 struct isl_set
*isl_set_add(struct isl_set
*set
, struct isl_basic_set
*bset
)
1535 return (struct isl_set
*)isl_map_add((struct isl_map
*)set
,
1536 (struct isl_basic_map
*)bset
);
1539 void isl_set_free(struct isl_set
*set
)
1549 isl_ctx_deref(set
->ctx
);
1550 for (i
= 0; i
< set
->n
; ++i
)
1551 isl_basic_set_free(set
->p
[i
]);
1552 isl_dim_free(set
->dim
);
1556 void isl_set_dump(struct isl_set
*set
, FILE *out
, int indent
)
1561 fprintf(out
, "null set\n");
1565 fprintf(out
, "%*s", indent
, "");
1566 fprintf(out
, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
1567 set
->ref
, set
->n
, set
->dim
->nparam
, set
->dim
->n_out
,
1569 for (i
= 0; i
< set
->n
; ++i
) {
1570 fprintf(out
, "%*s", indent
, "");
1571 fprintf(out
, "basic set %d:\n", i
);
1572 isl_basic_set_dump(set
->p
[i
], out
, indent
+4);
1576 void isl_map_dump(struct isl_map
*map
, FILE *out
, int indent
)
1581 fprintf(out
, "null map\n");
1585 fprintf(out
, "%*s", indent
, "");
1586 fprintf(out
, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
1587 "flags: %x, n_name: %d\n",
1588 map
->ref
, map
->n
, map
->dim
->nparam
, map
->dim
->n_in
,
1589 map
->dim
->n_out
, map
->flags
, map
->dim
->n_name
);
1590 for (i
= 0; i
< map
->n
; ++i
) {
1591 fprintf(out
, "%*s", indent
, "");
1592 fprintf(out
, "basic map %d:\n", i
);
1593 isl_basic_map_dump(map
->p
[i
], out
, indent
+4);
1597 struct isl_basic_map
*isl_basic_map_intersect_domain(
1598 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
1600 struct isl_basic_map
*bmap_domain
;
1601 struct isl_dim
*dim
;
1606 isl_assert(bset
->ctx
, isl_dim_match(bmap
->dim
, isl_dim_param
,
1607 bset
->dim
, isl_dim_param
), goto error
);
1609 if (isl_dim_size(bset
->dim
, isl_dim_set
) != 0)
1610 isl_assert(bset
->ctx
,
1611 isl_basic_map_compatible_domain(bmap
, bset
), goto error
);
1613 bmap
= isl_basic_map_cow(bmap
);
1614 bmap
= isl_basic_map_extend_dim(bmap
, isl_dim_copy(bmap
->dim
),
1615 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
1618 dim
= isl_dim_reverse(isl_dim_copy(bset
->dim
));
1619 bmap_domain
= isl_basic_map_from_basic_set(bset
, dim
);
1620 bmap
= add_constraints(bmap
, bmap_domain
, 0, 0);
1622 bmap
= isl_basic_map_simplify(bmap
);
1623 return isl_basic_map_finalize(bmap
);
1625 isl_basic_map_free(bmap
);
1626 isl_basic_set_free(bset
);
1630 struct isl_basic_map
*isl_basic_map_intersect_range(
1631 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
1633 struct isl_basic_map
*bmap_range
;
1638 isl_assert(bset
->ctx
, isl_dim_match(bmap
->dim
, isl_dim_param
,
1639 bset
->dim
, isl_dim_param
), goto error
);
1641 if (isl_dim_size(bset
->dim
, isl_dim_set
) != 0)
1642 isl_assert(bset
->ctx
,
1643 isl_basic_map_compatible_range(bmap
, bset
), goto error
);
1645 bmap
= isl_basic_map_cow(bmap
);
1646 bmap
= isl_basic_map_extend_dim(bmap
, isl_dim_copy(bmap
->dim
),
1647 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
1650 bmap_range
= isl_basic_map_from_basic_set(bset
, isl_dim_copy(bset
->dim
));
1651 bmap
= add_constraints(bmap
, bmap_range
, 0, 0);
1653 bmap
= isl_basic_map_simplify(bmap
);
1654 return isl_basic_map_finalize(bmap
);
1656 isl_basic_map_free(bmap
);
1657 isl_basic_set_free(bset
);
1661 static int basic_map_contains(struct isl_basic_map
*bmap
, struct isl_vec
*vec
)
1667 total
= 1 + isl_basic_map_total_dim(bmap
);
1668 if (total
!= vec
->size
)
1673 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
1674 isl_seq_inner_product(vec
->el
, bmap
->eq
[i
], total
, &s
);
1675 if (!isl_int_is_zero(s
)) {
1681 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
1682 isl_seq_inner_product(vec
->el
, bmap
->ineq
[i
], total
, &s
);
1683 if (isl_int_is_neg(s
)) {
1694 int isl_basic_set_contains(struct isl_basic_set
*bset
, struct isl_vec
*vec
)
1696 return basic_map_contains((struct isl_basic_map
*)bset
, vec
);
1699 struct isl_basic_map
*isl_basic_map_intersect(
1700 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
1702 struct isl_vec
*sample
= NULL
;
1704 if (!bmap1
|| !bmap2
)
1707 isl_assert(bmap1
->ctx
, isl_dim_match(bmap1
->dim
, isl_dim_param
,
1708 bmap2
->dim
, isl_dim_param
), goto error
);
1709 if (isl_dim_total(bmap1
->dim
) ==
1710 isl_dim_size(bmap1
->dim
, isl_dim_param
) &&
1711 isl_dim_total(bmap2
->dim
) !=
1712 isl_dim_size(bmap2
->dim
, isl_dim_param
))
1713 return isl_basic_map_intersect(bmap2
, bmap1
);
1715 if (isl_dim_total(bmap2
->dim
) !=
1716 isl_dim_size(bmap2
->dim
, isl_dim_param
))
1717 isl_assert(bmap1
->ctx
,
1718 isl_dim_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
1720 if (bmap1
->sample
&&
1721 basic_map_contains(bmap1
, bmap1
->sample
) > 0 &&
1722 basic_map_contains(bmap2
, bmap1
->sample
) > 0)
1723 sample
= isl_vec_copy(bmap1
->sample
);
1724 else if (bmap2
->sample
&&
1725 basic_map_contains(bmap1
, bmap2
->sample
) > 0 &&
1726 basic_map_contains(bmap2
, bmap2
->sample
) > 0)
1727 sample
= isl_vec_copy(bmap2
->sample
);
1729 bmap1
= isl_basic_map_cow(bmap1
);
1730 bmap1
= isl_basic_map_extend_dim(bmap1
, isl_dim_copy(bmap1
->dim
),
1731 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
1734 bmap1
= add_constraints(bmap1
, bmap2
, 0, 0);
1737 isl_vec_free(bmap1
->sample
);
1738 bmap1
->sample
= sample
;
1741 bmap1
= isl_basic_map_simplify(bmap1
);
1742 return isl_basic_map_finalize(bmap1
);
1745 isl_vec_free(sample
);
1746 isl_basic_map_free(bmap1
);
1747 isl_basic_map_free(bmap2
);
1751 struct isl_basic_set
*isl_basic_set_intersect(
1752 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
1754 return (struct isl_basic_set
*)
1755 isl_basic_map_intersect(
1756 (struct isl_basic_map
*)bset1
,
1757 (struct isl_basic_map
*)bset2
);
1760 struct isl_map
*isl_map_intersect(struct isl_map
*map1
, struct isl_map
*map2
)
1763 struct isl_map
*result
;
1769 isl_assert(map1
->ctx
, isl_dim_match(map1
->dim
, isl_dim_param
,
1770 map2
->dim
, isl_dim_param
), goto error
);
1771 if (isl_dim_total(map1
->dim
) ==
1772 isl_dim_size(map1
->dim
, isl_dim_param
) &&
1773 isl_dim_total(map2
->dim
) != isl_dim_size(map2
->dim
, isl_dim_param
))
1774 return isl_map_intersect(map2
, map1
);
1776 if (isl_dim_total(map2
->dim
) != isl_dim_size(map2
->dim
, isl_dim_param
))
1777 isl_assert(map1
->ctx
,
1778 isl_dim_equal(map1
->dim
, map2
->dim
), goto error
);
1780 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
1781 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
1782 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
1784 result
= isl_map_alloc_dim(isl_dim_copy(map1
->dim
),
1785 map1
->n
* map2
->n
, flags
);
1788 for (i
= 0; i
< map1
->n
; ++i
)
1789 for (j
= 0; j
< map2
->n
; ++j
) {
1790 struct isl_basic_map
*part
;
1791 part
= isl_basic_map_intersect(
1792 isl_basic_map_copy(map1
->p
[i
]),
1793 isl_basic_map_copy(map2
->p
[j
]));
1794 if (isl_basic_map_is_empty(part
))
1795 isl_basic_map_free(part
);
1797 result
= isl_map_add(result
, part
);
1810 struct isl_set
*isl_set_intersect(struct isl_set
*set1
, struct isl_set
*set2
)
1812 return (struct isl_set
*)
1813 isl_map_intersect((struct isl_map
*)set1
,
1814 (struct isl_map
*)set2
);
1817 struct isl_basic_map
*isl_basic_map_reverse(struct isl_basic_map
*bmap
)
1819 struct isl_dim
*dim
;
1820 struct isl_basic_set
*bset
;
1825 bmap
= isl_basic_map_cow(bmap
);
1828 dim
= isl_dim_reverse(isl_dim_copy(bmap
->dim
));
1829 in
= isl_basic_map_n_in(bmap
);
1830 bset
= isl_basic_set_from_basic_map(bmap
);
1831 bset
= isl_basic_set_swap_vars(bset
, in
);
1832 return isl_basic_map_from_basic_set(bset
, dim
);
1835 /* Turn final n dimensions into existentially quantified variables.
1837 struct isl_basic_set
*isl_basic_set_project_out(
1838 struct isl_basic_set
*bset
, unsigned n
, unsigned flags
)
1848 isl_assert(bset
->ctx
, n
<= isl_basic_set_n_dim(bset
), goto error
);
1853 bset
= isl_basic_set_cow(bset
);
1855 row_size
= 1 + isl_dim_total(bset
->dim
) + bset
->extra
;
1856 old
= bset
->block2
.data
;
1857 bset
->block2
= isl_blk_extend(bset
->ctx
, bset
->block2
,
1858 (bset
->extra
+ n
) * (1 + row_size
));
1859 if (!bset
->block2
.data
)
1861 new_div
= isl_alloc_array(ctx
, isl_int
*, bset
->extra
+ n
);
1864 for (i
= 0; i
< n
; ++i
) {
1865 new_div
[i
] = bset
->block2
.data
+
1866 (bset
->extra
+ i
) * (1 + row_size
);
1867 isl_seq_clr(new_div
[i
], 1 + row_size
);
1869 for (i
= 0; i
< bset
->extra
; ++i
)
1870 new_div
[n
+ i
] = bset
->block2
.data
+ (bset
->div
[i
] - old
);
1872 bset
->div
= new_div
;
1875 bset
->dim
= isl_dim_drop_outputs(bset
->dim
,
1876 isl_basic_set_n_dim(bset
) - n
, n
);
1879 bset
= isl_basic_set_simplify(bset
);
1880 bset
= isl_basic_set_drop_redundant_divs(bset
);
1881 return isl_basic_set_finalize(bset
);
1883 isl_basic_set_free(bset
);
1887 static struct isl_basic_map
*add_divs(struct isl_basic_map
*bmap
, unsigned n
)
1891 for (i
= 0; i
< n
; ++i
) {
1892 j
= isl_basic_map_alloc_div(bmap
);
1895 isl_seq_clr(bmap
->div
[j
], 1+1+isl_basic_map_total_dim(bmap
));
1899 isl_basic_map_free(bmap
);
1903 struct isl_basic_map
*isl_basic_map_apply_range(
1904 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
1906 struct isl_dim
*dim_result
= NULL
;
1907 struct isl_basic_map
*bmap
;
1908 unsigned n_in
, n_out
, n
, nparam
, total
, pos
;
1909 struct isl_dim_map
*dim_map1
, *dim_map2
;
1911 if (!bmap1
|| !bmap2
)
1914 dim_result
= isl_dim_join(isl_dim_copy(bmap1
->dim
),
1915 isl_dim_copy(bmap2
->dim
));
1917 n_in
= isl_basic_map_n_in(bmap1
);
1918 n_out
= isl_basic_map_n_out(bmap2
);
1919 n
= isl_basic_map_n_out(bmap1
);
1920 nparam
= isl_basic_map_n_param(bmap1
);
1922 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ n
;
1923 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
1924 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
1925 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
1926 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
1927 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
1928 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_in
);
1929 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_out
);
1930 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
1931 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
1932 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
1934 bmap
= isl_basic_map_alloc_dim(dim_result
,
1935 bmap1
->n_div
+ bmap2
->n_div
+ n
,
1936 bmap1
->n_eq
+ bmap2
->n_eq
,
1937 bmap1
->n_ineq
+ bmap2
->n_ineq
);
1938 bmap
= add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
1939 bmap
= add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
1940 bmap
= add_divs(bmap
, n
);
1941 bmap
= isl_basic_map_simplify(bmap
);
1942 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
1943 return isl_basic_map_finalize(bmap
);
1945 isl_basic_map_free(bmap1
);
1946 isl_basic_map_free(bmap2
);
1950 struct isl_basic_set
*isl_basic_set_apply(
1951 struct isl_basic_set
*bset
, struct isl_basic_map
*bmap
)
1956 isl_assert(set
->ctx
, isl_basic_map_compatible_domain(bmap
, bset
),
1959 return (struct isl_basic_set
*)
1960 isl_basic_map_apply_range((struct isl_basic_map
*)bset
, bmap
);
1962 isl_basic_set_free(bset
);
1963 isl_basic_map_free(bmap
);
1967 struct isl_basic_map
*isl_basic_map_apply_domain(
1968 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
1970 if (!bmap1
|| !bmap2
)
1974 isl_basic_map_n_in(bmap1
) == isl_basic_map_n_in(bmap2
), goto error
);
1976 isl_basic_map_n_param(bmap1
) == isl_basic_map_n_param(bmap2
),
1979 bmap1
= isl_basic_map_reverse(bmap1
);
1980 bmap1
= isl_basic_map_apply_range(bmap1
, bmap2
);
1981 return isl_basic_map_reverse(bmap1
);
1983 isl_basic_map_free(bmap1
);
1984 isl_basic_map_free(bmap2
);
1988 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
1989 * A \cap B -> f(A) + f(B)
1991 struct isl_basic_map
*isl_basic_map_sum(
1992 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
1994 unsigned n_in
, n_out
, nparam
, total
, pos
;
1995 struct isl_basic_map
*bmap
= NULL
;
1996 struct isl_dim_map
*dim_map1
, *dim_map2
;
1999 if (!bmap1
|| !bmap2
)
2002 isl_assert(bmap1
->ctx
, isl_dim_equal(bmap1
->dim
, bmap2
->dim
),
2005 nparam
= isl_basic_map_n_param(bmap1
);
2006 n_in
= isl_basic_map_n_in(bmap1
);
2007 n_out
= isl_basic_map_n_out(bmap1
);
2009 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
;
2010 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
2011 dim_map2
= isl_dim_map_alloc(bmap2
->ctx
, total
);
2012 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
2013 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
);
2014 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
2015 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
2016 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_in
+ n_out
);
2017 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
2018 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
2019 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_out
);
2021 bmap
= isl_basic_map_alloc_dim(isl_dim_copy(bmap1
->dim
),
2022 bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
,
2023 bmap1
->n_eq
+ bmap2
->n_eq
+ n_out
,
2024 bmap1
->n_ineq
+ bmap2
->n_ineq
);
2025 for (i
= 0; i
< n_out
; ++i
) {
2026 int j
= isl_basic_map_alloc_equality(bmap
);
2029 isl_seq_clr(bmap
->eq
[j
], 1+total
);
2030 isl_int_set_si(bmap
->eq
[j
][1+nparam
+n_in
+i
], -1);
2031 isl_int_set_si(bmap
->eq
[j
][1+pos
+i
], 1);
2032 isl_int_set_si(bmap
->eq
[j
][1+pos
-n_out
+i
], 1);
2034 bmap
= add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
2035 bmap
= add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
2036 bmap
= add_divs(bmap
, 2 * n_out
);
2038 bmap
= isl_basic_map_simplify(bmap
);
2039 return isl_basic_map_finalize(bmap
);
2041 isl_basic_map_free(bmap
);
2042 isl_basic_map_free(bmap1
);
2043 isl_basic_map_free(bmap2
);
2047 /* Given a basic map A -> f(A), construct A -> -f(A).
2049 struct isl_basic_map
*isl_basic_map_neg(struct isl_basic_map
*bmap
)
2054 bmap
= isl_basic_map_cow(bmap
);
2058 n
= isl_basic_map_dim(bmap
, isl_dim_out
);
2059 off
= isl_basic_map_offset(bmap
, isl_dim_out
);
2060 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2061 for (j
= 0; j
< n
; ++j
)
2062 isl_int_neg(bmap
->eq
[i
][off
+j
], bmap
->eq
[i
][off
+j
]);
2063 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2064 for (j
= 0; j
< n
; ++j
)
2065 isl_int_neg(bmap
->ineq
[i
][off
+j
], bmap
->ineq
[i
][off
+j
]);
2066 for (i
= 0; i
< bmap
->n_div
; ++i
)
2067 for (j
= 0; j
< n
; ++j
)
2068 isl_int_neg(bmap
->div
[i
][1+off
+j
], bmap
->div
[i
][1+off
+j
]);
2069 return isl_basic_map_finalize(bmap
);
2072 /* Given a basic map A -> f(A) and an integer d, construct a basic map
2073 * A -> floor(f(A)/d).
2075 struct isl_basic_map
*isl_basic_map_floordiv(struct isl_basic_map
*bmap
,
2078 unsigned n_in
, n_out
, nparam
, total
, pos
;
2079 struct isl_basic_map
*result
= NULL
;
2080 struct isl_dim_map
*dim_map
;
2086 nparam
= isl_basic_map_n_param(bmap
);
2087 n_in
= isl_basic_map_n_in(bmap
);
2088 n_out
= isl_basic_map_n_out(bmap
);
2090 total
= nparam
+ n_in
+ n_out
+ bmap
->n_div
+ n_out
;
2091 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
2092 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_param
, pos
= 0);
2093 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_in
, pos
+= nparam
);
2094 isl_dim_map_div(dim_map
, bmap
, pos
+= n_in
+ n_out
);
2095 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_out
, pos
+= bmap
->n_div
);
2097 result
= isl_basic_map_alloc_dim(isl_dim_copy(bmap
->dim
),
2098 bmap
->n_div
+ n_out
,
2099 bmap
->n_eq
, bmap
->n_ineq
+ 2 * n_out
);
2100 result
= add_constraints_dim_map(result
, bmap
, dim_map
);
2101 result
= add_divs(result
, n_out
);
2102 for (i
= 0; i
< n_out
; ++i
) {
2104 j
= isl_basic_map_alloc_inequality(result
);
2107 isl_seq_clr(result
->ineq
[j
], 1+total
);
2108 isl_int_neg(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
2109 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], 1);
2110 j
= isl_basic_map_alloc_inequality(result
);
2113 isl_seq_clr(result
->ineq
[j
], 1+total
);
2114 isl_int_set(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
2115 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], -1);
2116 isl_int_sub_ui(result
->ineq
[j
][0], d
, 1);
2119 result
= isl_basic_map_simplify(result
);
2120 return isl_basic_map_finalize(result
);
2122 isl_basic_map_free(result
);
2126 static struct isl_basic_map
*var_equal(struct isl_basic_map
*bmap
, unsigned pos
)
2132 i
= isl_basic_map_alloc_equality(bmap
);
2135 nparam
= isl_basic_map_n_param(bmap
);
2136 n_in
= isl_basic_map_n_in(bmap
);
2137 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
2138 isl_int_set_si(bmap
->eq
[i
][1+nparam
+pos
], -1);
2139 isl_int_set_si(bmap
->eq
[i
][1+nparam
+n_in
+pos
], 1);
2140 return isl_basic_map_finalize(bmap
);
2142 isl_basic_map_free(bmap
);
2146 static struct isl_basic_map
*var_more(struct isl_basic_map
*bmap
, unsigned pos
)
2152 i
= isl_basic_map_alloc_inequality(bmap
);
2155 nparam
= isl_basic_map_n_param(bmap
);
2156 n_in
= isl_basic_map_n_in(bmap
);
2157 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
2158 isl_int_set_si(bmap
->ineq
[i
][0], -1);
2159 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
2160 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
2161 return isl_basic_map_finalize(bmap
);
2163 isl_basic_map_free(bmap
);
2167 static struct isl_basic_map
*var_less(struct isl_basic_map
*bmap
, unsigned pos
)
2173 i
= isl_basic_map_alloc_inequality(bmap
);
2176 nparam
= isl_basic_map_n_param(bmap
);
2177 n_in
= isl_basic_map_n_in(bmap
);
2178 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
2179 isl_int_set_si(bmap
->ineq
[i
][0], -1);
2180 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
2181 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
2182 return isl_basic_map_finalize(bmap
);
2184 isl_basic_map_free(bmap
);
2188 struct isl_basic_map
*isl_basic_map_equal(struct isl_dim
*dim
, unsigned n_equal
)
2191 struct isl_basic_map
*bmap
;
2192 bmap
= isl_basic_map_alloc_dim(dim
, 0, n_equal
, 0);
2195 for (i
= 0; i
< n_equal
&& bmap
; ++i
)
2196 bmap
= var_equal(bmap
, i
);
2197 return isl_basic_map_finalize(bmap
);
2200 struct isl_basic_map
*isl_basic_map_less_at(struct isl_dim
*dim
, unsigned pos
)
2203 struct isl_basic_map
*bmap
;
2204 bmap
= isl_basic_map_alloc_dim(dim
, 0, pos
, 1);
2207 for (i
= 0; i
< pos
&& bmap
; ++i
)
2208 bmap
= var_equal(bmap
, i
);
2210 bmap
= var_less(bmap
, pos
);
2211 return isl_basic_map_finalize(bmap
);
2214 struct isl_basic_map
*isl_basic_map_more_at(struct isl_dim
*dim
, unsigned pos
)
2217 struct isl_basic_map
*bmap
;
2218 bmap
= isl_basic_map_alloc_dim(dim
, 0, pos
, 1);
2221 for (i
= 0; i
< pos
&& bmap
; ++i
)
2222 bmap
= var_equal(bmap
, i
);
2224 bmap
= var_more(bmap
, pos
);
2225 return isl_basic_map_finalize(bmap
);
2228 struct isl_basic_map
*isl_basic_map_from_basic_set(
2229 struct isl_basic_set
*bset
, struct isl_dim
*dim
)
2231 struct isl_basic_map
*bmap
;
2233 bset
= isl_basic_set_cow(bset
);
2237 isl_assert(bset
->ctx
, isl_dim_compatible(bset
->dim
, dim
), goto error
);
2238 isl_dim_free(bset
->dim
);
2239 bmap
= (struct isl_basic_map
*) bset
;
2241 return isl_basic_map_finalize(bmap
);
2243 isl_basic_set_free(bset
);
2248 struct isl_basic_set
*isl_basic_set_from_basic_map(struct isl_basic_map
*bmap
)
2252 if (bmap
->dim
->n_in
== 0)
2253 return (struct isl_basic_set
*)bmap
;
2254 bmap
= isl_basic_map_cow(bmap
);
2257 bmap
->dim
= isl_dim_cow(bmap
->dim
);
2260 bmap
->dim
->n_out
+= bmap
->dim
->n_in
;
2261 bmap
->dim
->n_in
= 0;
2262 bmap
= isl_basic_map_finalize(bmap
);
2263 return (struct isl_basic_set
*)bmap
;
2265 isl_basic_map_free(bmap
);
2269 /* For a div d = floor(f/m), add the constraints
2272 * -(f-(n-1)) + m d >= 0
2274 * Note that the second constraint is the negation of
2278 static int add_div_constraints(struct isl_basic_map
*bmap
, unsigned div
)
2281 unsigned total
= isl_basic_map_total_dim(bmap
);
2282 unsigned div_pos
= 1 + total
- bmap
->n_div
+ div
;
2284 i
= isl_basic_map_alloc_inequality(bmap
);
2287 isl_seq_cpy(bmap
->ineq
[i
], bmap
->div
[div
]+1, 1+total
);
2288 isl_int_neg(bmap
->ineq
[i
][div_pos
], bmap
->div
[div
][0]);
2290 j
= isl_basic_map_alloc_inequality(bmap
);
2293 isl_seq_neg(bmap
->ineq
[j
], bmap
->ineq
[i
], 1 + total
);
2294 isl_int_add(bmap
->ineq
[j
][0], bmap
->ineq
[j
][0], bmap
->ineq
[j
][div_pos
]);
2295 isl_int_sub_ui(bmap
->ineq
[j
][0], bmap
->ineq
[j
][0], 1);
2299 struct isl_basic_set
*isl_basic_map_underlying_set(
2300 struct isl_basic_map
*bmap
)
2304 if (bmap
->dim
->nparam
== 0 && bmap
->dim
->n_in
== 0 && bmap
->n_div
== 0)
2305 return (struct isl_basic_set
*)bmap
;
2306 bmap
= isl_basic_map_cow(bmap
);
2309 bmap
->dim
= isl_dim_underlying(bmap
->dim
, bmap
->n_div
);
2312 bmap
->extra
-= bmap
->n_div
;
2314 bmap
= isl_basic_map_finalize(bmap
);
2315 return (struct isl_basic_set
*)bmap
;
2320 struct isl_basic_map
*isl_basic_map_overlying_set(
2321 struct isl_basic_set
*bset
, struct isl_basic_map
*like
)
2323 struct isl_basic_map
*bmap
;
2324 struct isl_ctx
*ctx
;
2331 isl_assert(ctx
, bset
->n_div
== 0, goto error
);
2332 isl_assert(ctx
, isl_basic_set_n_param(bset
) == 0, goto error
);
2333 isl_assert(ctx
, bset
->dim
->n_out
== isl_basic_map_total_dim(like
),
2335 if (isl_dim_equal(bset
->dim
, like
->dim
) && like
->n_div
== 0) {
2336 isl_basic_map_free(like
);
2337 return (struct isl_basic_map
*)bset
;
2339 bset
= isl_basic_set_cow(bset
);
2342 total
= bset
->dim
->n_out
+ bset
->extra
;
2343 bmap
= (struct isl_basic_map
*)bset
;
2344 isl_dim_free(bmap
->dim
);
2345 bmap
->dim
= isl_dim_copy(like
->dim
);
2348 bmap
->n_div
= like
->n_div
;
2349 bmap
->extra
+= like
->n_div
;
2352 ltotal
= total
- bmap
->extra
+ like
->extra
;
2355 bmap
->block2
= isl_blk_extend(ctx
, bmap
->block2
,
2356 bmap
->extra
* (1 + 1 + total
));
2357 if (isl_blk_is_error(bmap
->block2
))
2359 bmap
->div
= isl_realloc_array(ctx
, bmap
->div
, isl_int
*,
2363 for (i
= 0; i
< bmap
->extra
; ++i
)
2364 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + 1 + total
);
2365 for (i
= 0; i
< like
->n_div
; ++i
) {
2366 isl_seq_cpy(bmap
->div
[i
], like
->div
[i
], 1 + 1 + ltotal
);
2367 isl_seq_clr(bmap
->div
[i
]+1+1+ltotal
, total
- ltotal
);
2369 bmap
= isl_basic_map_extend_constraints(bmap
,
2370 0, 2 * like
->n_div
);
2371 for (i
= 0; i
< like
->n_div
; ++i
) {
2372 if (isl_int_is_zero(bmap
->div
[i
][0]))
2374 if (add_div_constraints(bmap
, i
) < 0)
2378 isl_basic_map_free(like
);
2379 bmap
= isl_basic_map_simplify(bmap
);
2380 bmap
= isl_basic_map_finalize(bmap
);
2383 isl_basic_map_free(like
);
2384 isl_basic_set_free(bset
);
2388 struct isl_basic_set
*isl_basic_set_from_underlying_set(
2389 struct isl_basic_set
*bset
, struct isl_basic_set
*like
)
2391 return (struct isl_basic_set
*)
2392 isl_basic_map_overlying_set(bset
, (struct isl_basic_map
*)like
);
2395 struct isl_set
*isl_set_from_underlying_set(
2396 struct isl_set
*set
, struct isl_basic_set
*like
)
2402 isl_assert(set
->ctx
, set
->dim
->n_out
== isl_basic_set_total_dim(like
),
2404 if (isl_dim_equal(set
->dim
, like
->dim
) && like
->n_div
== 0) {
2405 isl_basic_set_free(like
);
2408 set
= isl_set_cow(set
);
2411 for (i
= 0; i
< set
->n
; ++i
) {
2412 set
->p
[i
] = isl_basic_set_from_underlying_set(set
->p
[i
],
2413 isl_basic_set_copy(like
));
2417 isl_dim_free(set
->dim
);
2418 set
->dim
= isl_dim_copy(like
->dim
);
2421 isl_basic_set_free(like
);
2424 isl_basic_set_free(like
);
2429 struct isl_set
*isl_map_underlying_set(struct isl_map
*map
)
2433 map
= isl_map_cow(map
);
2436 map
->dim
= isl_dim_cow(map
->dim
);
2440 for (i
= 1; i
< map
->n
; ++i
)
2441 isl_assert(map
->ctx
, map
->p
[0]->n_div
== map
->p
[i
]->n_div
,
2443 for (i
= 0; i
< map
->n
; ++i
) {
2444 map
->p
[i
] = (struct isl_basic_map
*)
2445 isl_basic_map_underlying_set(map
->p
[i
]);
2450 map
->dim
= isl_dim_underlying(map
->dim
, 0);
2452 isl_dim_free(map
->dim
);
2453 map
->dim
= isl_dim_copy(map
->p
[0]->dim
);
2457 return (struct isl_set
*)map
;
2463 struct isl_set
*isl_set_to_underlying_set(struct isl_set
*set
)
2465 return (struct isl_set
*)isl_map_underlying_set((struct isl_map
*)set
);
2468 struct isl_basic_set
*isl_basic_map_domain(struct isl_basic_map
*bmap
)
2470 struct isl_basic_set
*domain
;
2474 n_out
= isl_basic_map_n_out(bmap
);
2475 domain
= isl_basic_set_from_basic_map(bmap
);
2476 return isl_basic_set_project_out(domain
, n_out
, 0);
2479 struct isl_basic_set
*isl_basic_map_range(struct isl_basic_map
*bmap
)
2481 return isl_basic_map_domain(isl_basic_map_reverse(bmap
));
2484 struct isl_set
*isl_map_range(struct isl_map
*map
)
2487 struct isl_set
*set
;
2491 map
= isl_map_cow(map
);
2495 set
= (struct isl_set
*) map
;
2496 if (set
->dim
->n_in
!= 0) {
2497 set
->dim
= isl_dim_drop_inputs(set
->dim
, 0, set
->dim
->n_in
);
2501 for (i
= 0; i
< map
->n
; ++i
) {
2502 set
->p
[i
] = isl_basic_map_range(map
->p
[i
]);
2506 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
2507 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
2514 struct isl_map
*isl_map_from_set(struct isl_set
*set
, struct isl_dim
*dim
)
2517 struct isl_map
*map
= NULL
;
2519 set
= isl_set_cow(set
);
2522 isl_assert(set
->ctx
, isl_dim_compatible(set
->dim
, dim
), goto error
);
2523 map
= (struct isl_map
*)set
;
2524 for (i
= 0; i
< set
->n
; ++i
) {
2525 map
->p
[i
] = isl_basic_map_from_basic_set(
2526 set
->p
[i
], isl_dim_copy(dim
));
2530 isl_dim_free(map
->dim
);
2539 struct isl_map
*isl_map_from_range(struct isl_set
*set
)
2541 return (struct isl_map
*)set
;
2544 struct isl_set
*isl_set_from_map(struct isl_map
*map
)
2547 struct isl_set
*set
= NULL
;
2551 map
= isl_map_cow(map
);
2554 map
->dim
= isl_dim_cow(map
->dim
);
2557 map
->dim
->n_out
+= map
->dim
->n_in
;
2559 set
= (struct isl_set
*)map
;
2560 for (i
= 0; i
< map
->n
; ++i
) {
2561 set
->p
[i
] = isl_basic_set_from_basic_map(map
->p
[i
]);
2571 struct isl_map
*isl_map_alloc_dim(struct isl_dim
*dim
, int n
, unsigned flags
)
2573 struct isl_map
*map
;
2577 isl_assert(dim
->ctx
, n
>= 0, return NULL
);
2578 map
= isl_alloc(dim
->ctx
, struct isl_map
,
2579 sizeof(struct isl_map
) +
2580 n
* sizeof(struct isl_basic_map
*));
2584 map
->ctx
= dim
->ctx
;
2585 isl_ctx_ref(map
->ctx
);
2597 struct isl_map
*isl_map_alloc(struct isl_ctx
*ctx
,
2598 unsigned nparam
, unsigned in
, unsigned out
, int n
,
2601 struct isl_map
*map
;
2602 struct isl_dim
*dims
;
2604 dims
= isl_dim_alloc(ctx
, nparam
, in
, out
);
2608 map
= isl_map_alloc_dim(dims
, n
, flags
);
2612 struct isl_basic_map
*isl_basic_map_empty(struct isl_ctx
*ctx
,
2613 unsigned nparam
, unsigned in
, unsigned out
)
2615 struct isl_basic_map
*bmap
;
2616 bmap
= isl_basic_map_alloc(ctx
, nparam
, in
, out
, 0, 1, 0);
2617 bmap
= isl_basic_map_set_to_empty(bmap
);
2621 struct isl_basic_set
*isl_basic_set_empty(struct isl_dim
*dim
)
2623 struct isl_basic_set
*bset
;
2624 bset
= isl_basic_set_alloc_dim(dim
, 0, 1, 0);
2625 bset
= isl_basic_set_set_to_empty(bset
);
2629 struct isl_basic_map
*isl_basic_map_empty_like(struct isl_basic_map
*model
)
2631 struct isl_basic_map
*bmap
;
2634 bmap
= isl_basic_map_alloc_dim(isl_dim_copy(model
->dim
), 0, 1, 0);
2635 bmap
= isl_basic_map_set_to_empty(bmap
);
2639 struct isl_basic_map
*isl_basic_map_empty_like_map(struct isl_map
*model
)
2641 struct isl_basic_map
*bmap
;
2644 bmap
= isl_basic_map_alloc_dim(isl_dim_copy(model
->dim
), 0, 1, 0);
2645 bmap
= isl_basic_map_set_to_empty(bmap
);
2649 struct isl_basic_set
*isl_basic_set_empty_like(struct isl_basic_set
*model
)
2651 struct isl_basic_set
*bset
;
2654 bset
= isl_basic_set_alloc_dim(isl_dim_copy(model
->dim
), 0, 1, 0);
2655 bset
= isl_basic_set_set_to_empty(bset
);
2659 struct isl_basic_map
*isl_basic_map_universe(struct isl_dim
*dim
)
2661 struct isl_basic_map
*bmap
;
2662 bmap
= isl_basic_map_alloc_dim(dim
, 0, 0, 0);
2666 struct isl_basic_set
*isl_basic_set_universe(struct isl_dim
*dim
)
2668 struct isl_basic_set
*bset
;
2669 bset
= isl_basic_set_alloc_dim(dim
, 0, 0, 0);
2673 struct isl_basic_set
*isl_basic_set_universe_like(struct isl_basic_set
*model
)
2677 return isl_basic_set_alloc_dim(isl_dim_copy(model
->dim
), 0, 0, 0);
2680 struct isl_map
*isl_map_empty(struct isl_dim
*dim
)
2682 return isl_map_alloc_dim(dim
, 0, ISL_MAP_DISJOINT
);
2685 struct isl_map
*isl_map_empty_like(struct isl_map
*model
)
2689 return isl_map_alloc_dim(isl_dim_copy(model
->dim
), 0, ISL_MAP_DISJOINT
);
2692 struct isl_map
*isl_map_empty_like_basic_map(struct isl_basic_map
*model
)
2696 return isl_map_alloc_dim(isl_dim_copy(model
->dim
), 0, ISL_MAP_DISJOINT
);
2699 struct isl_set
*isl_set_empty(struct isl_dim
*dim
)
2701 return isl_set_alloc_dim(dim
, 0, ISL_MAP_DISJOINT
);
2704 struct isl_set
*isl_set_empty_like(struct isl_set
*model
)
2708 return isl_set_empty(isl_dim_copy(model
->dim
));
2711 struct isl_map
*isl_map_universe(struct isl_dim
*dim
)
2713 struct isl_map
*map
;
2716 map
= isl_map_alloc_dim(isl_dim_copy(dim
), 1, ISL_MAP_DISJOINT
);
2717 map
= isl_map_add(map
, isl_basic_map_universe(dim
));
2721 struct isl_set
*isl_set_universe(struct isl_dim
*dim
)
2723 struct isl_set
*set
;
2726 set
= isl_set_alloc_dim(isl_dim_copy(dim
), 1, ISL_MAP_DISJOINT
);
2727 set
= isl_set_add(set
, isl_basic_set_universe(dim
));
2731 struct isl_map
*isl_map_dup(struct isl_map
*map
)
2734 struct isl_map
*dup
;
2738 dup
= isl_map_alloc_dim(isl_dim_copy(map
->dim
), map
->n
, map
->flags
);
2739 for (i
= 0; i
< map
->n
; ++i
)
2740 dup
= isl_map_add(dup
, isl_basic_map_copy(map
->p
[i
]));
2744 struct isl_map
*isl_map_add(struct isl_map
*map
, struct isl_basic_map
*bmap
)
2748 if (isl_basic_map_fast_is_empty(bmap
)) {
2749 isl_basic_map_free(bmap
);
2752 isl_assert(map
->ctx
, isl_dim_equal(map
->dim
, bmap
->dim
), goto error
);
2753 isl_assert(map
->ctx
, map
->n
< map
->size
, goto error
);
2754 map
->p
[map
->n
] = bmap
;
2756 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
2762 isl_basic_map_free(bmap
);
2766 void isl_map_free(struct isl_map
*map
)
2776 isl_ctx_deref(map
->ctx
);
2777 for (i
= 0; i
< map
->n
; ++i
)
2778 isl_basic_map_free(map
->p
[i
]);
2779 isl_dim_free(map
->dim
);
2783 struct isl_map
*isl_map_extend(struct isl_map
*base
,
2784 unsigned nparam
, unsigned n_in
, unsigned n_out
)
2788 base
= isl_map_cow(base
);
2792 base
->dim
= isl_dim_extend(base
->dim
, nparam
, n_in
, n_out
);
2795 for (i
= 0; i
< base
->n
; ++i
) {
2796 base
->p
[i
] = isl_basic_map_extend_dim(base
->p
[i
],
2797 isl_dim_copy(base
->dim
), 0, 0, 0);
2807 struct isl_set
*isl_set_extend(struct isl_set
*base
,
2808 unsigned nparam
, unsigned dim
)
2810 return (struct isl_set
*)isl_map_extend((struct isl_map
*)base
,
2814 static struct isl_basic_map
*isl_basic_map_fix_pos(struct isl_basic_map
*bmap
,
2815 unsigned pos
, int value
)
2819 bmap
= isl_basic_map_cow(bmap
);
2820 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
2821 j
= isl_basic_map_alloc_equality(bmap
);
2824 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
2825 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
2826 isl_int_set_si(bmap
->eq
[j
][0], value
);
2827 bmap
= isl_basic_map_simplify(bmap
);
2828 return isl_basic_map_finalize(bmap
);
2830 isl_basic_map_free(bmap
);
2834 struct isl_basic_map
*isl_basic_map_fix_si(struct isl_basic_map
*bmap
,
2835 enum isl_dim_type type
, unsigned pos
, int value
)
2839 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
2840 return isl_basic_map_fix_pos(bmap
, isl_basic_map_offset(bmap
, type
) + pos
,
2843 isl_basic_map_free(bmap
);
2847 struct isl_basic_set
*isl_basic_set_fix_si(struct isl_basic_set
*bset
,
2848 enum isl_dim_type type
, unsigned pos
, int value
)
2850 return (struct isl_basic_set
*)
2851 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
2855 struct isl_basic_map
*isl_basic_map_fix_input_si(struct isl_basic_map
*bmap
,
2856 unsigned input
, int value
)
2858 return isl_basic_map_fix_si(bmap
, isl_dim_in
, input
, value
);
2861 struct isl_basic_set
*isl_basic_set_fix_dim_si(struct isl_basic_set
*bset
,
2862 unsigned dim
, int value
)
2864 return (struct isl_basic_set
*)
2865 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
2866 isl_dim_set
, dim
, value
);
2869 struct isl_map
*isl_map_fix_si(struct isl_map
*map
,
2870 enum isl_dim_type type
, unsigned pos
, int value
)
2874 map
= isl_map_cow(map
);
2878 isl_assert(ctx
, pos
< isl_map_dim(map
, type
), goto error
);
2879 for (i
= 0; i
< map
->n
; ++i
) {
2880 map
->p
[i
] = isl_basic_map_fix_si(map
->p
[i
], type
, pos
, value
);
2884 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
2891 struct isl_map
*isl_map_fix_input_si(struct isl_map
*map
,
2892 unsigned input
, int value
)
2894 return isl_map_fix_si(map
, isl_dim_in
, input
, value
);
2897 struct isl_set
*isl_set_fix_dim_si(struct isl_set
*set
, unsigned dim
, int value
)
2899 return (struct isl_set
*)
2900 isl_map_fix_si((struct isl_map
*)set
, isl_dim_set
, dim
, value
);
2903 struct isl_basic_set
*isl_basic_set_lower_bound_dim(struct isl_basic_set
*bset
,
2904 unsigned dim
, isl_int value
)
2909 bset
= isl_basic_set_cow(bset
);
2910 bset
= isl_basic_set_extend_constraints(bset
, 0, 1);
2911 j
= isl_basic_set_alloc_inequality(bset
);
2914 isl_seq_clr(bset
->ineq
[j
], 1 + isl_basic_set_total_dim(bset
));
2915 isl_int_set_si(bset
->ineq
[j
][1 + isl_basic_set_n_param(bset
) + dim
], 1);
2916 isl_int_neg(bset
->ineq
[j
][0], value
);
2917 bset
= isl_basic_set_simplify(bset
);
2918 return isl_basic_set_finalize(bset
);
2920 isl_basic_set_free(bset
);
2924 struct isl_set
*isl_set_lower_bound_dim(struct isl_set
*set
, unsigned dim
,
2929 set
= isl_set_cow(set
);
2933 isl_assert(set
->ctx
, dim
< isl_set_n_dim(set
), goto error
);
2934 for (i
= 0; i
< set
->n
; ++i
) {
2935 set
->p
[i
] = isl_basic_set_lower_bound_dim(set
->p
[i
], dim
, value
);
2945 struct isl_map
*isl_map_reverse(struct isl_map
*map
)
2950 map
= isl_map_cow(map
);
2954 map
->dim
= isl_dim_reverse(map
->dim
);
2957 for (i
= 0; i
< map
->n
; ++i
) {
2958 map
->p
[i
] = isl_basic_map_reverse(map
->p
[i
]);
2962 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
2969 static struct isl_map
*isl_basic_map_partial_lexopt(
2970 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
2971 struct isl_set
**empty
, int max
)
2975 if (bmap
->ctx
->pip
== ISL_PIP_PIP
)
2976 return isl_pip_basic_map_lexopt(bmap
, dom
, empty
, max
);
2978 return isl_tab_basic_map_partial_lexopt(bmap
, dom
, empty
, max
);
2980 isl_basic_map_free(bmap
);
2981 isl_basic_set_free(dom
);
2987 struct isl_map
*isl_basic_map_partial_lexmax(
2988 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
2989 struct isl_set
**empty
)
2991 return isl_basic_map_partial_lexopt(bmap
, dom
, empty
, 1);
2994 struct isl_map
*isl_basic_map_partial_lexmin(
2995 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
2996 struct isl_set
**empty
)
2998 return isl_basic_map_partial_lexopt(bmap
, dom
, empty
, 0);
3001 struct isl_set
*isl_basic_set_partial_lexmin(
3002 struct isl_basic_set
*bset
, struct isl_basic_set
*dom
,
3003 struct isl_set
**empty
)
3005 return (struct isl_set
*)
3006 isl_basic_map_partial_lexmin((struct isl_basic_map
*)bset
,
3010 struct isl_set
*isl_basic_set_partial_lexmax(
3011 struct isl_basic_set
*bset
, struct isl_basic_set
*dom
,
3012 struct isl_set
**empty
)
3014 return (struct isl_set
*)
3015 isl_basic_map_partial_lexmax((struct isl_basic_map
*)bset
,
3019 struct isl_set
*isl_basic_set_lexmin(struct isl_basic_set
*bset
)
3021 struct isl_basic_map
*bmap
= NULL
;
3022 struct isl_basic_set
*dom
= NULL
;
3023 struct isl_map
*min
;
3024 struct isl_dim
*param_dim
;
3028 bmap
= isl_basic_map_from_basic_set(bset
, isl_dim_copy(bset
->dim
));
3031 param_dim
= isl_dim_domain(isl_dim_copy(bmap
->dim
));
3032 dom
= isl_basic_set_universe(param_dim
);
3035 min
= isl_basic_map_partial_lexmin(bmap
, dom
, NULL
);
3036 return isl_map_range(min
);
3038 isl_basic_map_free(bmap
);
3042 static struct isl_map
*isl_map_reset_dim(struct isl_map
*map
,
3043 struct isl_dim
*dim
)
3050 for (i
= 0; i
< map
->n
; ++i
) {
3051 isl_dim_free(map
->p
[i
]->dim
);
3052 map
->p
[i
]->dim
= isl_dim_copy(dim
);
3054 isl_dim_free(map
->dim
);
3064 static struct isl_set
*isl_set_reset_dim(struct isl_set
*set
,
3065 struct isl_dim
*dim
)
3067 return (struct isl_set
*) isl_map_reset_dim((struct isl_map
*)set
, dim
);
3070 /* Apply a preimage specified by "mat" on the parameters of "bset".
3071 * bset is assumed to have only parameters and divs.
3073 static struct isl_basic_set
*basic_set_parameter_preimage(
3074 struct isl_basic_set
*bset
, struct isl_mat
*mat
)
3081 bset
->dim
= isl_dim_cow(bset
->dim
);
3085 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
3087 isl_assert(bset
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
3089 bset
->dim
->nparam
= 0;
3090 bset
->dim
->n_out
= nparam
;
3091 bset
= isl_basic_set_preimage(bset
, mat
);
3093 bset
->dim
->nparam
= bset
->dim
->n_out
;
3094 bset
->dim
->n_out
= 0;
3099 isl_basic_set_free(bset
);
3103 /* Apply a preimage specified by "mat" on the parameters of "set".
3104 * set is assumed to have only parameters and divs.
3106 static struct isl_set
*set_parameter_preimage(
3107 struct isl_set
*set
, struct isl_mat
*mat
)
3109 struct isl_dim
*dim
= NULL
;
3115 dim
= isl_dim_copy(set
->dim
);
3116 dim
= isl_dim_cow(dim
);
3120 nparam
= isl_set_dim(set
, isl_dim_param
);
3122 isl_assert(set
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
3125 dim
->n_out
= nparam
;
3126 isl_set_reset_dim(set
, dim
);
3127 set
= isl_set_preimage(set
, mat
);
3130 dim
= isl_dim_copy(set
->dim
);
3131 dim
= isl_dim_cow(dim
);
3134 dim
->nparam
= dim
->n_out
;
3136 isl_set_reset_dim(set
, dim
);
3146 /* Intersect the basic set "bset" with the affine space specified by the
3147 * equalities in "eq".
3149 static struct isl_basic_set
*basic_set_append_equalities(
3150 struct isl_basic_set
*bset
, struct isl_mat
*eq
)
3158 bset
= isl_basic_set_extend_dim(bset
, isl_dim_copy(bset
->dim
), 0,
3163 len
= 1 + isl_dim_total(bset
->dim
) + bset
->extra
;
3164 for (i
= 0; i
< eq
->n_row
; ++i
) {
3165 k
= isl_basic_set_alloc_equality(bset
);
3168 isl_seq_cpy(bset
->eq
[k
], eq
->row
[i
], eq
->n_col
);
3169 isl_seq_clr(bset
->eq
[k
] + eq
->n_col
, len
- eq
->n_col
);
3176 isl_basic_set_free(bset
);
3180 /* Intersect the set "set" with the affine space specified by the
3181 * equalities in "eq".
3183 static struct isl_set
*set_append_equalities(struct isl_set
*set
,
3191 for (i
= 0; i
< set
->n
; ++i
) {
3192 set
->p
[i
] = basic_set_append_equalities(set
->p
[i
],
3205 /* Project the given basic set onto its parameter domain, possibly introducing
3206 * new, explicit, existential variables in the constraints.
3207 * The input has parameters and (possibly implicit) existential variables.
3208 * The output has the same parameters, but only
3209 * explicit existentially quantified variables.
3211 * The actual projection is performed by pip, but pip doesn't seem
3212 * to like equalities very much, so we first remove the equalities
3213 * among the parameters by performing a variable compression on
3214 * the parameters. Afterward, an inverse transformation is performed
3215 * and the equalities among the parameters are inserted back in.
3217 static struct isl_set
*parameter_compute_divs(struct isl_basic_set
*bset
)
3221 struct isl_mat
*T
, *T2
;
3222 struct isl_set
*set
;
3223 unsigned nparam
, n_div
;
3225 bset
= isl_basic_set_cow(bset
);
3229 if (bset
->n_eq
== 0)
3230 return isl_basic_set_lexmin(bset
);
3232 isl_basic_set_gauss(bset
, NULL
);
3234 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
3235 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
3237 for (i
= 0, j
= n_div
- 1; i
< bset
->n_eq
&& j
>= 0; --j
) {
3238 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ j
]))
3241 if (i
== bset
->n_eq
)
3242 return isl_basic_set_lexmin(bset
);
3244 eq
= isl_mat_sub_alloc(bset
->ctx
, bset
->eq
, i
, bset
->n_eq
- i
,
3246 eq
= isl_mat_cow(eq
);
3247 T
= isl_mat_variable_compression(isl_mat_copy(eq
), &T2
);
3248 bset
= basic_set_parameter_preimage(bset
, T
);
3250 set
= isl_basic_set_lexmin(bset
);
3251 set
= set_parameter_preimage(set
, T2
);
3252 set
= set_append_equalities(set
, eq
);
3256 /* Compute an explicit representation for all the existentially
3257 * quantified variables.
3258 * The input and output dimensions are first turned into parameters.
3259 * compute_divs then returns a map with the same parameters and
3260 * no input or output dimensions and the dimension specification
3261 * is reset to that of the input.
3263 static struct isl_map
*compute_divs(struct isl_basic_map
*bmap
)
3265 struct isl_basic_set
*bset
;
3266 struct isl_set
*set
;
3267 struct isl_map
*map
;
3268 struct isl_dim
*dim
, *orig_dim
= NULL
;
3273 bmap
= isl_basic_map_cow(bmap
);
3277 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
3278 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
3279 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
3280 dim
= isl_dim_set_alloc(bmap
->ctx
, nparam
+ n_in
+ n_out
, 0);
3284 orig_dim
= bmap
->dim
;
3286 bset
= (struct isl_basic_set
*)bmap
;
3288 set
= parameter_compute_divs(bset
);
3289 map
= (struct isl_map
*)set
;
3290 map
= isl_map_reset_dim(map
, orig_dim
);
3294 isl_basic_map_free(bmap
);
3298 /* If bmap contains any unknown divs, then compute explicit
3299 * expressions for them. However, this computation may be
3300 * quite expensive, so first try to remove divs that aren't
3303 struct isl_map
*isl_basic_map_compute_divs(struct isl_basic_map
*bmap
)
3310 off
= isl_dim_total(bmap
->dim
);
3311 for (i
= 0; i
< bmap
->n_div
; ++i
) {
3312 if (isl_int_is_zero(bmap
->div
[i
][0]))
3314 isl_assert(bmap
->ctx
, isl_int_is_zero(bmap
->div
[i
][1+1+off
+i
]),
3317 if (i
== bmap
->n_div
)
3318 return isl_map_from_basic_map(bmap
);
3319 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
3322 for (i
= 0; i
< bmap
->n_div
; ++i
)
3323 if (isl_int_is_zero(bmap
->div
[i
][0]))
3325 if (i
== bmap
->n_div
)
3326 return isl_map_from_basic_map(bmap
);
3327 struct isl_map
*map
= compute_divs(bmap
);
3330 isl_basic_map_free(bmap
);
3334 struct isl_map
*isl_map_compute_divs(struct isl_map
*map
)
3337 struct isl_map
*res
;
3343 res
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[0]));
3344 for (i
= 1 ; i
< map
->n
; ++i
) {
3346 r2
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[i
]));
3347 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
3348 res
= isl_map_union_disjoint(res
, r2
);
3350 res
= isl_map_union(res
, r2
);
3357 struct isl_set
*isl_basic_set_compute_divs(struct isl_basic_set
*bset
)
3359 return (struct isl_set
*)
3360 isl_basic_map_compute_divs((struct isl_basic_map
*)bset
);
3363 struct isl_set
*isl_set_compute_divs(struct isl_set
*set
)
3365 return (struct isl_set
*)
3366 isl_map_compute_divs((struct isl_map
*)set
);
3369 struct isl_set
*isl_map_domain(struct isl_map
*map
)
3372 struct isl_set
*set
;
3377 map
= isl_map_cow(map
);
3381 set
= (struct isl_set
*)map
;
3382 set
->dim
= isl_dim_domain(set
->dim
);
3385 for (i
= 0; i
< map
->n
; ++i
) {
3386 set
->p
[i
] = isl_basic_map_domain(map
->p
[i
]);
3390 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
3391 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
3398 struct isl_map
*isl_map_union_disjoint(
3399 struct isl_map
*map1
, struct isl_map
*map2
)
3403 struct isl_map
*map
= NULL
;
3417 isl_assert(map1
->ctx
, isl_dim_equal(map1
->dim
, map2
->dim
), goto error
);
3419 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
3420 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
3421 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
3423 map
= isl_map_alloc_dim(isl_dim_copy(map1
->dim
),
3424 map1
->n
+ map2
->n
, flags
);
3427 for (i
= 0; i
< map1
->n
; ++i
) {
3428 map
= isl_map_add(map
,
3429 isl_basic_map_copy(map1
->p
[i
]));
3433 for (i
= 0; i
< map2
->n
; ++i
) {
3434 map
= isl_map_add(map
,
3435 isl_basic_map_copy(map2
->p
[i
]));
3449 struct isl_map
*isl_map_union(struct isl_map
*map1
, struct isl_map
*map2
)
3451 map1
= isl_map_union_disjoint(map1
, map2
);
3455 ISL_F_CLR(map1
, ISL_MAP_DISJOINT
);
3459 struct isl_set
*isl_set_union_disjoint(
3460 struct isl_set
*set1
, struct isl_set
*set2
)
3462 return (struct isl_set
*)
3463 isl_map_union_disjoint(
3464 (struct isl_map
*)set1
, (struct isl_map
*)set2
);
3467 struct isl_set
*isl_set_union(struct isl_set
*set1
, struct isl_set
*set2
)
3469 return (struct isl_set
*)
3470 isl_map_union((struct isl_map
*)set1
, (struct isl_map
*)set2
);
3473 struct isl_map
*isl_map_intersect_range(
3474 struct isl_map
*map
, struct isl_set
*set
)
3477 struct isl_map
*result
;
3483 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
) &&
3484 ISL_F_ISSET(set
, ISL_MAP_DISJOINT
))
3485 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
3487 result
= isl_map_alloc_dim(isl_dim_copy(map
->dim
),
3488 map
->n
* set
->n
, flags
);
3491 for (i
= 0; i
< map
->n
; ++i
)
3492 for (j
= 0; j
< set
->n
; ++j
) {
3493 result
= isl_map_add(result
,
3494 isl_basic_map_intersect_range(
3495 isl_basic_map_copy(map
->p
[i
]),
3496 isl_basic_set_copy(set
->p
[j
])));
3509 struct isl_map
*isl_map_intersect_domain(
3510 struct isl_map
*map
, struct isl_set
*set
)
3512 return isl_map_reverse(
3513 isl_map_intersect_range(isl_map_reverse(map
), set
));
3516 struct isl_map
*isl_map_apply_domain(
3517 struct isl_map
*map1
, struct isl_map
*map2
)
3521 map1
= isl_map_reverse(map1
);
3522 map1
= isl_map_apply_range(map1
, map2
);
3523 return isl_map_reverse(map1
);
3530 struct isl_map
*isl_map_apply_range(
3531 struct isl_map
*map1
, struct isl_map
*map2
)
3533 struct isl_dim
*dim_result
;
3534 struct isl_map
*result
;
3543 dim_result
= isl_dim_join(isl_dim_copy(map1
->dim
),
3544 isl_dim_copy(map2
->dim
));
3546 result
= isl_map_alloc_dim(dim_result
, map1
->n
* map2
->n
, 0);
3549 for (i
= 0; i
< map1
->n
; ++i
)
3550 for (j
= 0; j
< map2
->n
; ++j
) {
3551 result
= isl_map_add(result
,
3552 isl_basic_map_apply_range(
3553 isl_basic_map_copy(map1
->p
[i
]),
3554 isl_basic_map_copy(map2
->p
[j
])));
3560 if (result
&& result
->n
<= 1)
3561 ISL_F_SET(result
, ISL_MAP_DISJOINT
);
3570 * returns range - domain
3572 struct isl_basic_set
*isl_basic_map_deltas(struct isl_basic_map
*bmap
)
3574 struct isl_basic_set
*bset
;
3581 dim
= isl_basic_map_n_in(bmap
);
3582 nparam
= isl_basic_map_n_param(bmap
);
3583 isl_assert(bmap
->ctx
, dim
== isl_basic_map_n_out(bmap
), goto error
);
3584 bset
= isl_basic_set_from_basic_map(bmap
);
3585 bset
= isl_basic_set_cow(bset
);
3586 bset
= isl_basic_set_extend(bset
, nparam
, 3*dim
, 0, dim
, 0);
3587 bset
= isl_basic_set_swap_vars(bset
, 2*dim
);
3588 for (i
= 0; i
< dim
; ++i
) {
3589 int j
= isl_basic_map_alloc_equality(
3590 (struct isl_basic_map
*)bset
);
3593 isl_seq_clr(bset
->eq
[j
], 1 + isl_basic_set_total_dim(bset
));
3594 isl_int_set_si(bset
->eq
[j
][1+nparam
+i
], 1);
3595 isl_int_set_si(bset
->eq
[j
][1+nparam
+dim
+i
], 1);
3596 isl_int_set_si(bset
->eq
[j
][1+nparam
+2*dim
+i
], -1);
3598 return isl_basic_set_project_out(bset
, 2*dim
, 0);
3600 isl_basic_map_free(bmap
);
3605 * returns range - domain
3607 struct isl_set
*isl_map_deltas(struct isl_map
*map
)
3610 struct isl_set
*result
;
3615 isl_assert(map
->ctx
, isl_map_n_in(map
) == isl_map_n_out(map
), goto error
);
3616 result
= isl_set_alloc(map
->ctx
, isl_map_n_param(map
),
3617 isl_map_n_in(map
), map
->n
, map
->flags
);
3620 for (i
= 0; i
< map
->n
; ++i
)
3621 result
= isl_set_add(result
,
3622 isl_basic_map_deltas(isl_basic_map_copy(map
->p
[i
])));
3630 static struct isl_basic_map
*basic_map_identity(struct isl_dim
*dims
)
3632 struct isl_basic_map
*bmap
;
3640 nparam
= dims
->nparam
;
3642 bmap
= isl_basic_map_alloc_dim(dims
, 0, dim
, 0);
3646 for (i
= 0; i
< dim
; ++i
) {
3647 int j
= isl_basic_map_alloc_equality(bmap
);
3650 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
3651 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
3652 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], -1);
3654 return isl_basic_map_finalize(bmap
);
3656 isl_basic_map_free(bmap
);
3660 struct isl_basic_map
*isl_basic_map_identity(struct isl_dim
*set_dim
)
3662 struct isl_dim
*dim
= isl_dim_map(set_dim
);
3665 return basic_map_identity(dim
);
3668 struct isl_basic_map
*isl_basic_map_identity_like(struct isl_basic_map
*model
)
3670 if (!model
|| !model
->dim
)
3672 isl_assert(model
->ctx
,
3673 model
->dim
->n_in
== model
->dim
->n_out
, return NULL
);
3674 return basic_map_identity(isl_dim_copy(model
->dim
));
3677 static struct isl_map
*map_identity(struct isl_dim
*dim
)
3679 struct isl_map
*map
= isl_map_alloc_dim(dim
, 1, ISL_MAP_DISJOINT
);
3680 return isl_map_add(map
, basic_map_identity(isl_dim_copy(dim
)));
3683 struct isl_map
*isl_map_identity(struct isl_dim
*set_dim
)
3685 struct isl_dim
*dim
= isl_dim_map(set_dim
);
3688 return map_identity(dim
);
3691 struct isl_map
*isl_map_identity_like(struct isl_basic_map
*model
)
3693 if (!model
|| !model
->dim
)
3695 isl_assert(model
->ctx
,
3696 model
->dim
->n_in
== model
->dim
->n_out
, return NULL
);
3697 return map_identity(isl_dim_copy(model
->dim
));
3700 /* Construct a basic set with all set dimensions having only non-negative
3703 struct isl_basic_set
*isl_basic_set_positive_orthant(struct isl_dim
*dims
)
3708 struct isl_basic_set
*bset
;
3712 nparam
= dims
->nparam
;
3714 bset
= isl_basic_set_alloc_dim(dims
, 0, 0, dim
);
3717 for (i
= 0; i
< dim
; ++i
) {
3718 int k
= isl_basic_set_alloc_inequality(bset
);
3721 isl_seq_clr(bset
->ineq
[k
], 1 + isl_basic_set_total_dim(bset
));
3722 isl_int_set_si(bset
->ineq
[k
][1 + nparam
+ i
], 1);
3726 isl_basic_set_free(bset
);
3730 int isl_set_is_equal(struct isl_set
*set1
, struct isl_set
*set2
)
3732 return isl_map_is_equal((struct isl_map
*)set1
, (struct isl_map
*)set2
);
3735 int isl_set_is_subset(struct isl_set
*set1
, struct isl_set
*set2
)
3737 return isl_map_is_subset(
3738 (struct isl_map
*)set1
, (struct isl_map
*)set2
);
3741 int isl_basic_map_is_subset(
3742 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3745 struct isl_map
*map1
;
3746 struct isl_map
*map2
;
3748 if (!bmap1
|| !bmap2
)
3751 map1
= isl_map_from_basic_map(isl_basic_map_copy(bmap1
));
3752 map2
= isl_map_from_basic_map(isl_basic_map_copy(bmap2
));
3754 is_subset
= isl_map_is_subset(map1
, map2
);
3762 int isl_basic_map_is_equal(
3763 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3767 if (!bmap1
|| !bmap2
)
3769 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
3772 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
3776 int isl_basic_set_is_equal(
3777 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
3779 return isl_basic_map_is_equal(
3780 (struct isl_basic_map
*)bset1
, (struct isl_basic_map
*)bset2
);
3783 int isl_map_is_empty(struct isl_map
*map
)
3790 for (i
= 0; i
< map
->n
; ++i
) {
3791 is_empty
= isl_basic_map_is_empty(map
->p
[i
]);
3800 int isl_map_fast_is_empty(struct isl_map
*map
)
3805 int isl_set_is_empty(struct isl_set
*set
)
3807 return isl_map_is_empty((struct isl_map
*)set
);
3810 int isl_map_is_subset(struct isl_map
*map1
, struct isl_map
*map2
)
3814 struct isl_map
*diff
;
3819 if (isl_map_is_empty(map1
))
3822 if (isl_map_is_empty(map2
))
3825 diff
= isl_map_subtract(isl_map_copy(map1
), isl_map_copy(map2
));
3829 is_subset
= isl_map_is_empty(diff
);
3835 int isl_map_is_equal(struct isl_map
*map1
, struct isl_map
*map2
)
3841 is_subset
= isl_map_is_subset(map1
, map2
);
3844 is_subset
= isl_map_is_subset(map2
, map1
);
3848 int isl_basic_map_is_strict_subset(
3849 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3853 if (!bmap1
|| !bmap2
)
3855 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
3858 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
3859 if (is_subset
== -1)
3864 int isl_basic_map_is_universe(struct isl_basic_map
*bmap
)
3868 return bmap
->n_eq
== 0 && bmap
->n_ineq
== 0;
3871 int isl_basic_set_is_universe(struct isl_basic_set
*bset
)
3875 return bset
->n_eq
== 0 && bset
->n_ineq
== 0;
3878 int isl_basic_map_is_empty(struct isl_basic_map
*bmap
)
3880 struct isl_basic_set
*bset
= NULL
;
3881 struct isl_vec
*sample
= NULL
;
3888 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
3891 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
3892 struct isl_basic_map
*copy
= isl_basic_map_copy(bmap
);
3893 copy
= isl_basic_map_convex_hull(copy
);
3894 empty
= ISL_F_ISSET(copy
, ISL_BASIC_MAP_EMPTY
);
3895 isl_basic_map_free(copy
);
3899 total
= 1 + isl_basic_map_total_dim(bmap
);
3900 if (bmap
->sample
&& bmap
->sample
->size
== total
) {
3901 int contains
= basic_map_contains(bmap
, bmap
->sample
);
3907 isl_vec_free(bmap
->sample
);
3908 bmap
->sample
= NULL
;
3909 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
3912 sample
= isl_basic_set_sample(bset
);
3915 empty
= sample
->size
== 0;
3916 isl_vec_free(bmap
->sample
);
3917 bmap
->sample
= sample
;
3919 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
3924 int isl_basic_map_fast_is_empty(struct isl_basic_map
*bmap
)
3928 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
);
3931 int isl_basic_set_fast_is_empty(struct isl_basic_set
*bset
)
3935 return ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
);
3938 int isl_basic_set_is_empty(struct isl_basic_set
*bset
)
3940 return isl_basic_map_is_empty((struct isl_basic_map
*)bset
);
3943 struct isl_map
*isl_basic_map_union(
3944 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3946 struct isl_map
*map
;
3947 if (!bmap1
|| !bmap2
)
3950 isl_assert(map1
->ctx
, isl_dim_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
3952 map
= isl_map_alloc_dim(isl_dim_copy(bmap1
->dim
), 2, 0);
3955 map
= isl_map_add(map
, bmap1
);
3956 map
= isl_map_add(map
, bmap2
);
3959 isl_basic_map_free(bmap1
);
3960 isl_basic_map_free(bmap2
);
3964 struct isl_set
*isl_basic_set_union(
3965 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
3967 return (struct isl_set
*)isl_basic_map_union(
3968 (struct isl_basic_map
*)bset1
,
3969 (struct isl_basic_map
*)bset2
);
3972 /* Order divs such that any div only depends on previous divs */
3973 static struct isl_basic_map
*order_divs(struct isl_basic_map
*bmap
)
3976 unsigned off
= isl_dim_total(bmap
->dim
);
3978 for (i
= 0; i
< bmap
->n_div
; ++i
) {
3980 if (isl_int_is_zero(bmap
->div
[i
][0]))
3982 pos
= isl_seq_first_non_zero(bmap
->div
[i
]+1+1+off
+i
,
3986 isl_basic_map_swap_div(bmap
, i
, i
+ pos
);
3992 struct isl_basic_set
*isl_basic_set_order_divs(struct isl_basic_set
*bset
)
3994 return (struct isl_basic_set
*)order_divs((struct isl_basic_map
*)bset
);
3997 /* Look for a div in dst that corresponds to the div "div" in src.
3998 * The divs before "div" in src and dst are assumed to be the same.
4000 * Returns -1 if no corresponding div was found and the position
4001 * of the corresponding div in dst otherwise.
4003 static int find_div(struct isl_basic_map
*dst
,
4004 struct isl_basic_map
*src
, unsigned div
)
4008 unsigned total
= isl_dim_total(src
->dim
);
4010 isl_assert(dst
->ctx
, div
<= dst
->n_div
, return -1);
4011 for (i
= div
; i
< dst
->n_div
; ++i
)
4012 if (isl_seq_eq(dst
->div
[i
], src
->div
[div
], 1+1+total
+div
) &&
4013 isl_seq_first_non_zero(dst
->div
[i
]+1+1+total
+div
,
4014 dst
->n_div
- div
) == -1)
4019 struct isl_basic_map
*isl_basic_map_align_divs(
4020 struct isl_basic_map
*dst
, struct isl_basic_map
*src
)
4023 unsigned total
= isl_dim_total(src
->dim
);
4028 if (src
->n_div
== 0)
4031 for (i
= 0; i
< src
->n_div
; ++i
)
4032 isl_assert(src
->ctx
, !isl_int_is_zero(src
->div
[i
][0]), goto error
);
4034 src
= order_divs(src
);
4035 dst
= isl_basic_map_cow(dst
);
4036 dst
= isl_basic_map_extend_dim(dst
, isl_dim_copy(dst
->dim
),
4037 src
->n_div
, 0, 2 * src
->n_div
);
4040 for (i
= 0; i
< src
->n_div
; ++i
) {
4041 int j
= find_div(dst
, src
, i
);
4043 j
= isl_basic_map_alloc_div(dst
);
4046 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
+i
);
4047 isl_seq_clr(dst
->div
[j
]+1+1+total
+i
, dst
->n_div
- i
);
4048 if (add_div_constraints(dst
, j
) < 0)
4052 isl_basic_map_swap_div(dst
, i
, j
);
4056 isl_basic_map_free(dst
);
4060 struct isl_basic_set
*isl_basic_set_align_divs(
4061 struct isl_basic_set
*dst
, struct isl_basic_set
*src
)
4063 return (struct isl_basic_set
*)isl_basic_map_align_divs(
4064 (struct isl_basic_map
*)dst
, (struct isl_basic_map
*)src
);
4067 struct isl_map
*isl_map_align_divs(struct isl_map
*map
)
4071 map
= isl_map_compute_divs(map
);
4072 map
= isl_map_cow(map
);
4076 for (i
= 1; i
< map
->n
; ++i
)
4077 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], map
->p
[i
]);
4078 for (i
= 1; i
< map
->n
; ++i
)
4079 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], map
->p
[0]);
4081 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4085 struct isl_set
*isl_set_align_divs(struct isl_set
*set
)
4087 return (struct isl_set
*)isl_map_align_divs((struct isl_map
*)set
);
4090 static struct isl_map
*add_cut_constraint(struct isl_map
*dst
,
4091 struct isl_basic_map
*src
, isl_int
*c
,
4092 unsigned len
, int oppose
)
4094 struct isl_basic_map
*copy
= NULL
;
4099 copy
= isl_basic_map_copy(src
);
4100 copy
= isl_basic_map_cow(copy
);
4103 copy
= isl_basic_map_extend_constraints(copy
, 0, 1);
4104 k
= isl_basic_map_alloc_inequality(copy
);
4108 isl_seq_neg(copy
->ineq
[k
], c
, len
);
4110 isl_seq_cpy(copy
->ineq
[k
], c
, len
);
4111 total
= 1 + isl_basic_map_total_dim(copy
);
4112 isl_seq_clr(copy
->ineq
[k
]+len
, total
- len
);
4113 isl_inequality_negate(copy
, k
);
4114 copy
= isl_basic_map_simplify(copy
);
4115 copy
= isl_basic_map_finalize(copy
);
4116 is_empty
= isl_basic_map_is_empty(copy
);
4120 dst
= isl_map_add(dst
, copy
);
4122 isl_basic_map_free(copy
);
4125 isl_basic_map_free(copy
);
4130 static struct isl_map
*subtract(struct isl_map
*map
, struct isl_basic_map
*bmap
)
4134 struct isl_map
*rest
= NULL
;
4136 unsigned total
= isl_basic_map_total_dim(bmap
);
4143 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
4144 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
4146 max
= map
->n
* (2 * bmap
->n_eq
+ bmap
->n_ineq
);
4147 rest
= isl_map_alloc_dim(isl_dim_copy(map
->dim
), max
, flags
);
4151 for (i
= 0; i
< map
->n
; ++i
) {
4152 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], bmap
);
4157 for (j
= 0; j
< map
->n
; ++j
)
4158 map
->p
[j
] = isl_basic_map_cow(map
->p
[j
]);
4160 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
4161 for (j
= 0; j
< map
->n
; ++j
) {
4162 rest
= add_cut_constraint(rest
,
4163 map
->p
[j
], bmap
->eq
[i
], 1+total
, 0);
4167 rest
= add_cut_constraint(rest
,
4168 map
->p
[j
], bmap
->eq
[i
], 1+total
, 1);
4172 map
->p
[j
] = isl_basic_map_extend_constraints(map
->p
[j
],
4176 k
= isl_basic_map_alloc_equality(map
->p
[j
]);
4179 isl_seq_cpy(map
->p
[j
]->eq
[k
], bmap
->eq
[i
], 1+total
);
4180 isl_seq_clr(map
->p
[j
]->eq
[k
]+1+total
,
4181 map
->p
[j
]->n_div
- bmap
->n_div
);
4185 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
4186 for (j
= 0; j
< map
->n
; ++j
) {
4187 rest
= add_cut_constraint(rest
,
4188 map
->p
[j
], bmap
->ineq
[i
], 1+total
, 0);
4192 map
->p
[j
] = isl_basic_map_extend_constraints(map
->p
[j
],
4196 k
= isl_basic_map_alloc_inequality(map
->p
[j
]);
4199 isl_seq_cpy(map
->p
[j
]->ineq
[k
], bmap
->ineq
[i
], 1+total
);
4200 isl_seq_clr(map
->p
[j
]->ineq
[k
]+1+total
,
4201 map
->p
[j
]->n_div
- bmap
->n_div
);
4213 struct isl_map
*isl_map_subtract(struct isl_map
*map1
, struct isl_map
*map2
)
4219 isl_assert(map1
->ctx
, isl_dim_equal(map1
->dim
, map2
->dim
), goto error
);
4221 if (isl_map_is_empty(map2
)) {
4226 map1
= isl_map_compute_divs(map1
);
4227 map2
= isl_map_compute_divs(map2
);
4231 for (i
= 0; map1
&& i
< map2
->n
; ++i
)
4232 map1
= subtract(map1
, map2
->p
[i
]);
4242 struct isl_set
*isl_set_subtract(struct isl_set
*set1
, struct isl_set
*set2
)
4244 return (struct isl_set
*)
4246 (struct isl_map
*)set1
, (struct isl_map
*)set2
);
4249 struct isl_set
*isl_set_apply(struct isl_set
*set
, struct isl_map
*map
)
4253 isl_assert(set
->ctx
, isl_map_compatible_domain(map
, set
), goto error
);
4254 map
= isl_map_intersect_domain(map
, set
);
4255 set
= isl_map_range(map
);
4263 /* There is no need to cow as removing empty parts doesn't change
4264 * the meaning of the set.
4266 struct isl_map
*isl_map_remove_empty_parts(struct isl_map
*map
)
4273 for (i
= map
->n
-1; i
>= 0; --i
) {
4274 if (!ISL_F_ISSET(map
->p
[i
], ISL_BASIC_MAP_EMPTY
))
4276 isl_basic_map_free(map
->p
[i
]);
4277 if (i
!= map
->n
-1) {
4278 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4279 map
->p
[i
] = map
->p
[map
->n
-1];
4287 struct isl_set
*isl_set_remove_empty_parts(struct isl_set
*set
)
4289 return (struct isl_set
*)
4290 isl_map_remove_empty_parts((struct isl_map
*)set
);
4293 struct isl_basic_map
*isl_map_copy_basic_map(struct isl_map
*map
)
4295 struct isl_basic_map
*bmap
;
4296 if (!map
|| map
->n
== 0)
4298 bmap
= map
->p
[map
->n
-1];
4299 isl_assert(map
->ctx
, ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
), return NULL
);
4300 return isl_basic_map_copy(bmap
);
4303 struct isl_basic_set
*isl_set_copy_basic_set(struct isl_set
*set
)
4305 (struct isl_basic_set
*)isl_map_copy_basic_map((struct isl_map
*)set
);
4308 struct isl_map
*isl_map_drop_basic_map(struct isl_map
*map
,
4309 struct isl_basic_map
*bmap
)
4315 for (i
= map
->n
-1; i
>= 0; --i
) {
4316 if (map
->p
[i
] != bmap
)
4318 map
= isl_map_cow(map
);
4321 isl_basic_map_free(map
->p
[i
]);
4322 if (i
!= map
->n
-1) {
4323 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
4324 map
->p
[i
] = map
->p
[map
->n
-1];
4329 isl_basic_map_free(bmap
);
4333 isl_basic_map_free(bmap
);
4337 struct isl_set
*isl_set_drop_basic_set(struct isl_set
*set
,
4338 struct isl_basic_set
*bset
)
4340 (struct isl_set
*)isl_map_drop_basic_map((struct isl_map
*)set
,
4341 (struct isl_basic_map
*)bset
);
4344 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
4345 * for any common value of the parameters and dimensions preceding dim
4346 * in both basic sets, the values of dimension pos in bset1 are
4347 * smaller or larger than those in bset2.
4350 * 1 if bset1 follows bset2
4351 * -1 if bset1 precedes bset2
4352 * 0 if bset1 and bset2 are incomparable
4353 * -2 if some error occurred.
4355 int isl_basic_set_compare_at(struct isl_basic_set
*bset1
,
4356 struct isl_basic_set
*bset2
, int pos
)
4358 struct isl_dim
*dims
;
4359 struct isl_basic_map
*bmap1
= NULL
;
4360 struct isl_basic_map
*bmap2
= NULL
;
4361 struct isl_ctx
*ctx
;
4362 struct isl_vec
*obj
;
4365 unsigned dim1
, dim2
;
4367 enum isl_lp_result res
;
4370 if (!bset1
|| !bset2
)
4373 nparam
= isl_basic_set_n_param(bset1
);
4374 dim1
= isl_basic_set_n_dim(bset1
);
4375 dim2
= isl_basic_set_n_dim(bset2
);
4376 dims
= isl_dim_alloc(bset1
->ctx
, nparam
, pos
, dim1
- pos
);
4377 bmap1
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset1
), dims
);
4378 dims
= isl_dim_alloc(bset2
->ctx
, nparam
, pos
, dim2
- pos
);
4379 bmap2
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset2
), dims
);
4380 if (!bmap1
|| !bmap2
)
4382 bmap1
= isl_basic_map_cow(bmap1
);
4383 bmap1
= isl_basic_map_extend(bmap1
, nparam
,
4384 pos
, (dim1
- pos
) + (dim2
- pos
),
4385 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
4386 bmap1
= add_constraints(bmap1
, bmap2
, 0, dim1
- pos
);
4389 total
= isl_basic_map_total_dim(bmap1
);
4391 obj
= isl_vec_alloc(ctx
, 1 + total
);
4392 isl_seq_clr(obj
->block
.data
, 1 + total
);
4393 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
], 1);
4394 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
+(dim1
-pos
)], -1);
4399 res
= isl_solve_lp(bmap1
, 0, obj
->block
.data
, ctx
->one
, &num
, &den
);
4400 if (res
== isl_lp_empty
)
4402 else if (res
== isl_lp_ok
&& isl_int_is_pos(num
))
4404 else if ((res
== isl_lp_ok
&& isl_int_is_neg(num
)) ||
4405 res
== isl_lp_unbounded
)
4411 isl_basic_map_free(bmap1
);
4415 isl_basic_map_free(bmap1
);
4416 isl_basic_map_free(bmap2
);
4420 static int isl_basic_map_fast_has_fixed_var(struct isl_basic_map
*bmap
,
4421 unsigned pos
, isl_int
*val
)
4429 total
= isl_basic_map_total_dim(bmap
);
4430 for (i
= 0, d
= total
-1; i
< bmap
->n_eq
&& d
+1 > pos
; ++i
) {
4431 for (; d
+1 > pos
; --d
)
4432 if (!isl_int_is_zero(bmap
->eq
[i
][1+d
]))
4436 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1, d
) != -1)
4438 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1+d
+1, total
-d
-1) != -1)
4440 if (!isl_int_is_one(bmap
->eq
[i
][1+d
]))
4443 isl_int_neg(*val
, bmap
->eq
[i
][0]);
4449 static int isl_map_fast_has_fixed_var(struct isl_map
*map
,
4450 unsigned pos
, isl_int
*val
)
4462 return isl_basic_map_fast_has_fixed_var(map
->p
[0], pos
, val
);
4465 fixed
= isl_basic_map_fast_has_fixed_var(map
->p
[0], pos
, &v
);
4466 for (i
= 1; fixed
== 1 && i
< map
->n
; ++i
) {
4467 fixed
= isl_basic_map_fast_has_fixed_var(map
->p
[i
], pos
, &tmp
);
4468 if (fixed
== 1 && isl_int_ne(tmp
, v
))
4472 isl_int_set(*val
, v
);
4478 static int isl_basic_set_fast_has_fixed_var(struct isl_basic_set
*bset
,
4479 unsigned pos
, isl_int
*val
)
4481 return isl_basic_map_fast_has_fixed_var((struct isl_basic_map
*)bset
,
4485 static int isl_set_fast_has_fixed_var(struct isl_set
*set
, unsigned pos
,
4488 return isl_map_fast_has_fixed_var((struct isl_map
*)set
, pos
, val
);
4491 int isl_basic_map_fast_is_fixed(struct isl_basic_map
*bmap
,
4492 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
4494 if (pos
>= isl_basic_map_dim(bmap
, type
))
4496 return isl_basic_map_fast_has_fixed_var(bmap
,
4497 isl_basic_map_offset(bmap
, type
) - 1 + pos
, val
);
4500 /* Check if dimension dim has fixed value and if so and if val is not NULL,
4501 * then return this fixed value in *val.
4503 int isl_basic_set_fast_dim_is_fixed(struct isl_basic_set
*bset
, unsigned dim
,
4506 return isl_basic_set_fast_has_fixed_var(bset
,
4507 isl_basic_set_n_param(bset
) + dim
, val
);
4510 /* Check if dimension dim has fixed value and if so and if val is not NULL,
4511 * then return this fixed value in *val.
4513 int isl_set_fast_dim_is_fixed(struct isl_set
*set
, unsigned dim
, isl_int
*val
)
4515 return isl_set_fast_has_fixed_var(set
, isl_set_n_param(set
) + dim
, val
);
4518 /* Check if input variable in has fixed value and if so and if val is not NULL,
4519 * then return this fixed value in *val.
4521 int isl_map_fast_input_is_fixed(struct isl_map
*map
, unsigned in
, isl_int
*val
)
4523 return isl_map_fast_has_fixed_var(map
, isl_map_n_param(map
) + in
, val
);
4526 /* Check if dimension dim has an (obvious) fixed lower bound and if so
4527 * and if val is not NULL, then return this lower bound in *val.
4529 int isl_basic_set_fast_dim_has_fixed_lower_bound(struct isl_basic_set
*bset
,
4530 unsigned dim
, isl_int
*val
)
4532 int i
, i_eq
= -1, i_ineq
= -1;
4539 total
= isl_basic_set_total_dim(bset
);
4540 nparam
= isl_basic_set_n_param(bset
);
4541 for (i
= 0; i
< bset
->n_eq
; ++i
) {
4542 if (isl_int_is_zero(bset
->eq
[i
][1+nparam
+dim
]))
4548 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
4549 if (!isl_int_is_pos(bset
->ineq
[i
][1+nparam
+dim
]))
4551 if (i_eq
!= -1 || i_ineq
!= -1)
4555 if (i_eq
== -1 && i_ineq
== -1)
4557 c
= i_eq
!= -1 ? bset
->eq
[i_eq
] : bset
->ineq
[i_ineq
];
4558 /* The coefficient should always be one due to normalization. */
4559 if (!isl_int_is_one(c
[1+nparam
+dim
]))
4561 if (isl_seq_first_non_zero(c
+1, nparam
+dim
) != -1)
4563 if (isl_seq_first_non_zero(c
+1+nparam
+dim
+1,
4564 total
- nparam
- dim
- 1) != -1)
4567 isl_int_neg(*val
, c
[0]);
4571 int isl_set_fast_dim_has_fixed_lower_bound(struct isl_set
*set
,
4572 unsigned dim
, isl_int
*val
)
4584 return isl_basic_set_fast_dim_has_fixed_lower_bound(set
->p
[0],
4588 fixed
= isl_basic_set_fast_dim_has_fixed_lower_bound(set
->p
[0],
4590 for (i
= 1; fixed
== 1 && i
< set
->n
; ++i
) {
4591 fixed
= isl_basic_set_fast_dim_has_fixed_lower_bound(set
->p
[i
],
4593 if (fixed
== 1 && isl_int_ne(tmp
, v
))
4597 isl_int_set(*val
, v
);
4608 static int qsort_constraint_cmp(const void *p1
, const void *p2
)
4610 const struct constraint
*c1
= (const struct constraint
*)p1
;
4611 const struct constraint
*c2
= (const struct constraint
*)p2
;
4612 unsigned size
= isl_min(c1
->size
, c2
->size
);
4613 return isl_seq_cmp(c1
->c
, c2
->c
, size
);
4616 static struct isl_basic_map
*isl_basic_map_sort_constraints(
4617 struct isl_basic_map
*bmap
)
4620 struct constraint
*c
;
4625 total
= isl_basic_map_total_dim(bmap
);
4626 c
= isl_alloc_array(bmap
->ctx
, struct constraint
, bmap
->n_ineq
);
4629 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
4631 c
[i
].c
= bmap
->ineq
[i
];
4633 qsort(c
, bmap
->n_ineq
, sizeof(struct constraint
), qsort_constraint_cmp
);
4634 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
4635 bmap
->ineq
[i
] = c
[i
].c
;
4639 isl_basic_map_free(bmap
);
4643 struct isl_basic_map
*isl_basic_map_normalize(struct isl_basic_map
*bmap
)
4647 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED
))
4649 bmap
= isl_basic_map_convex_hull(bmap
);
4650 bmap
= isl_basic_map_sort_constraints(bmap
);
4651 ISL_F_SET(bmap
, ISL_BASIC_MAP_NORMALIZED
);
4655 struct isl_basic_set
*isl_basic_set_normalize(struct isl_basic_set
*bset
)
4657 return (struct isl_basic_set
*)isl_basic_map_normalize(
4658 (struct isl_basic_map
*)bset
);
4661 static int isl_basic_map_fast_cmp(const struct isl_basic_map
*bmap1
,
4662 const struct isl_basic_map
*bmap2
)
4669 if (isl_basic_map_n_param(bmap1
) != isl_basic_map_n_param(bmap2
))
4670 return isl_basic_map_n_param(bmap1
) - isl_basic_map_n_param(bmap2
);
4671 if (isl_basic_map_n_in(bmap1
) != isl_basic_map_n_in(bmap2
))
4672 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
4673 if (isl_basic_map_n_out(bmap1
) != isl_basic_map_n_out(bmap2
))
4674 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
4675 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
) &&
4676 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
4678 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
))
4680 if (ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
4682 if (bmap1
->n_eq
!= bmap2
->n_eq
)
4683 return bmap1
->n_eq
- bmap2
->n_eq
;
4684 if (bmap1
->n_ineq
!= bmap2
->n_ineq
)
4685 return bmap1
->n_ineq
- bmap2
->n_ineq
;
4686 if (bmap1
->n_div
!= bmap2
->n_div
)
4687 return bmap1
->n_div
- bmap2
->n_div
;
4688 total
= isl_basic_map_total_dim(bmap1
);
4689 for (i
= 0; i
< bmap1
->n_eq
; ++i
) {
4690 cmp
= isl_seq_cmp(bmap1
->eq
[i
], bmap2
->eq
[i
], 1+total
);
4694 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
4695 cmp
= isl_seq_cmp(bmap1
->ineq
[i
], bmap2
->ineq
[i
], 1+total
);
4699 for (i
= 0; i
< bmap1
->n_div
; ++i
) {
4700 cmp
= isl_seq_cmp(bmap1
->div
[i
], bmap2
->div
[i
], 1+1+total
);
4707 static int isl_basic_map_fast_is_equal(struct isl_basic_map
*bmap1
,
4708 struct isl_basic_map
*bmap2
)
4710 return isl_basic_map_fast_cmp(bmap1
, bmap2
) == 0;
4713 static int qsort_bmap_cmp(const void *p1
, const void *p2
)
4715 const struct isl_basic_map
*bmap1
= *(const struct isl_basic_map
**)p1
;
4716 const struct isl_basic_map
*bmap2
= *(const struct isl_basic_map
**)p2
;
4718 return isl_basic_map_fast_cmp(bmap1
, bmap2
);
4721 /* We normalize in place, but if anything goes wrong we need
4722 * to return NULL, so we need to make sure we don't change the
4723 * meaning of any possible other copies of map.
4725 struct isl_map
*isl_map_normalize(struct isl_map
*map
)
4728 struct isl_basic_map
*bmap
;
4732 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
))
4734 for (i
= 0; i
< map
->n
; ++i
) {
4735 bmap
= isl_basic_map_normalize(isl_basic_map_copy(map
->p
[i
]));
4738 isl_basic_map_free(map
->p
[i
]);
4741 qsort(map
->p
, map
->n
, sizeof(struct isl_basic_map
*), qsort_bmap_cmp
);
4742 ISL_F_SET(map
, ISL_MAP_NORMALIZED
);
4743 map
= isl_map_remove_empty_parts(map
);
4746 for (i
= map
->n
- 1; i
>= 1; --i
) {
4747 if (!isl_basic_map_fast_is_equal(map
->p
[i
-1], map
->p
[i
]))
4749 isl_basic_map_free(map
->p
[i
-1]);
4750 for (j
= i
; j
< map
->n
; ++j
)
4751 map
->p
[j
-1] = map
->p
[j
];
4761 struct isl_set
*isl_set_normalize(struct isl_set
*set
)
4763 return (struct isl_set
*)isl_map_normalize((struct isl_map
*)set
);
4766 int isl_map_fast_is_equal(struct isl_map
*map1
, struct isl_map
*map2
)
4776 if (!isl_dim_equal(map1
->dim
, map2
->dim
))
4779 map1
= isl_map_copy(map1
);
4780 map2
= isl_map_copy(map2
);
4781 map1
= isl_map_normalize(map1
);
4782 map2
= isl_map_normalize(map2
);
4785 equal
= map1
->n
== map2
->n
;
4786 for (i
= 0; equal
&& i
< map1
->n
; ++i
) {
4787 equal
= isl_basic_map_fast_is_equal(map1
->p
[i
], map2
->p
[i
]);
4800 int isl_set_fast_is_equal(struct isl_set
*set1
, struct isl_set
*set2
)
4802 return isl_map_fast_is_equal((struct isl_map
*)set1
,
4803 (struct isl_map
*)set2
);
4806 /* Return an interval that ranges from min to max (inclusive)
4808 struct isl_basic_set
*isl_basic_set_interval(struct isl_ctx
*ctx
,
4809 isl_int min
, isl_int max
)
4812 struct isl_basic_set
*bset
= NULL
;
4814 bset
= isl_basic_set_alloc(ctx
, 0, 1, 0, 0, 2);
4818 k
= isl_basic_set_alloc_inequality(bset
);
4821 isl_int_set_si(bset
->ineq
[k
][1], 1);
4822 isl_int_neg(bset
->ineq
[k
][0], min
);
4824 k
= isl_basic_set_alloc_inequality(bset
);
4827 isl_int_set_si(bset
->ineq
[k
][1], -1);
4828 isl_int_set(bset
->ineq
[k
][0], max
);
4832 isl_basic_set_free(bset
);
4836 /* Return the Cartesian product of the basic sets in list (in the given order).
4838 struct isl_basic_set
*isl_basic_set_product(struct isl_basic_set_list
*list
)
4846 struct isl_basic_set
*product
= NULL
;
4850 isl_assert(list
->ctx
, list
->n
> 0, goto error
);
4851 isl_assert(list
->ctx
, list
->p
[0], goto error
);
4852 nparam
= isl_basic_set_n_param(list
->p
[0]);
4853 dim
= isl_basic_set_n_dim(list
->p
[0]);
4854 extra
= list
->p
[0]->n_div
;
4855 n_eq
= list
->p
[0]->n_eq
;
4856 n_ineq
= list
->p
[0]->n_ineq
;
4857 for (i
= 1; i
< list
->n
; ++i
) {
4858 isl_assert(list
->ctx
, list
->p
[i
], goto error
);
4859 isl_assert(list
->ctx
,
4860 nparam
== isl_basic_set_n_param(list
->p
[i
]), goto error
);
4861 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
4862 extra
+= list
->p
[i
]->n_div
;
4863 n_eq
+= list
->p
[i
]->n_eq
;
4864 n_ineq
+= list
->p
[i
]->n_ineq
;
4866 product
= isl_basic_set_alloc(list
->ctx
, nparam
, dim
, extra
,
4871 for (i
= 0; i
< list
->n
; ++i
) {
4872 isl_basic_set_add_constraints(product
,
4873 isl_basic_set_copy(list
->p
[i
]), dim
);
4874 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
4876 isl_basic_set_list_free(list
);
4879 isl_basic_set_free(product
);
4880 isl_basic_set_list_free(list
);
4884 struct isl_basic_map
*isl_basic_map_product(
4885 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
4887 struct isl_dim
*dim_result
= NULL
;
4888 struct isl_basic_map
*bmap
;
4889 unsigned in1
, in2
, out1
, out2
, nparam
, total
, pos
;
4890 struct isl_dim_map
*dim_map1
, *dim_map2
;
4892 if (!bmap1
|| !bmap2
)
4895 isl_assert(map1
->ctx
, isl_dim_match(bmap1
->dim
, isl_dim_param
,
4896 bmap2
->dim
, isl_dim_param
), goto error
);
4897 dim_result
= isl_dim_product(isl_dim_copy(bmap1
->dim
),
4898 isl_dim_copy(bmap2
->dim
));
4900 in1
= isl_basic_map_n_in(bmap1
);
4901 in2
= isl_basic_map_n_in(bmap2
);
4902 out1
= isl_basic_map_n_out(bmap1
);
4903 out2
= isl_basic_map_n_out(bmap2
);
4904 nparam
= isl_basic_map_n_param(bmap1
);
4906 total
= nparam
+ in1
+ in2
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
4907 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4908 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4909 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
4910 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
4911 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
4912 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
4913 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
4914 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
4915 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
4916 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
4918 bmap
= isl_basic_map_alloc_dim(dim_result
,
4919 bmap1
->n_div
+ bmap2
->n_div
,
4920 bmap1
->n_eq
+ bmap2
->n_eq
,
4921 bmap1
->n_ineq
+ bmap2
->n_ineq
);
4922 bmap
= add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
4923 bmap
= add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
4924 bmap
= isl_basic_map_simplify(bmap
);
4925 return isl_basic_map_finalize(bmap
);
4927 isl_basic_map_free(bmap1
);
4928 isl_basic_map_free(bmap2
);
4932 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
4934 struct isl_map
*isl_map_product(struct isl_map
*map1
, struct isl_map
*map2
)
4937 struct isl_map
*result
;
4943 isl_assert(map1
->ctx
, isl_dim_match(map1
->dim
, isl_dim_param
,
4944 map2
->dim
, isl_dim_param
), goto error
);
4946 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
4947 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
4948 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
4950 result
= isl_map_alloc_dim(isl_dim_product(isl_dim_copy(map1
->dim
),
4951 isl_dim_copy(map2
->dim
)),
4952 map1
->n
* map2
->n
, flags
);
4955 for (i
= 0; i
< map1
->n
; ++i
)
4956 for (j
= 0; j
< map2
->n
; ++j
) {
4957 struct isl_basic_map
*part
;
4958 part
= isl_basic_map_product(
4959 isl_basic_map_copy(map1
->p
[i
]),
4960 isl_basic_map_copy(map2
->p
[j
]));
4961 if (isl_basic_map_is_empty(part
))
4962 isl_basic_map_free(part
);
4964 result
= isl_map_add(result
, part
);
4977 /* Given two set A and B, construct its Cartesian product A x B.
4979 struct isl_set
*isl_set_product(struct isl_set
*set1
, struct isl_set
*set2
)
4981 return (struct isl_set
*)isl_map_product((struct isl_map
*)set1
,
4982 (struct isl_map
*)set2
);
4985 uint32_t isl_basic_set_get_hash(struct isl_basic_set
*bset
)
4993 bset
= isl_basic_set_copy(bset
);
4994 bset
= isl_basic_set_normalize(bset
);
4997 total
= isl_basic_set_total_dim(bset
);
4998 isl_hash_byte(hash
, bset
->n_eq
& 0xFF);
4999 for (i
= 0; i
< bset
->n_eq
; ++i
) {
5001 c_hash
= isl_seq_get_hash(bset
->eq
[i
], 1 + total
);
5002 isl_hash_hash(hash
, c_hash
);
5004 isl_hash_byte(hash
, bset
->n_ineq
& 0xFF);
5005 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
5007 c_hash
= isl_seq_get_hash(bset
->ineq
[i
], 1 + total
);
5008 isl_hash_hash(hash
, c_hash
);
5010 isl_hash_byte(hash
, bset
->n_div
& 0xFF);
5011 for (i
= 0; i
< bset
->n_div
; ++i
) {
5013 if (isl_int_is_zero(bset
->div
[i
][0]))
5015 isl_hash_byte(hash
, i
& 0xFF);
5016 c_hash
= isl_seq_get_hash(bset
->div
[i
], 1 + 1 + total
);
5017 isl_hash_hash(hash
, c_hash
);
5019 isl_basic_set_free(bset
);
5023 uint32_t isl_set_get_hash(struct isl_set
*set
)
5030 set
= isl_set_copy(set
);
5031 set
= isl_set_normalize(set
);
5035 hash
= isl_hash_init();
5036 for (i
= 0; i
< set
->n
; ++i
) {
5038 bset_hash
= isl_basic_set_get_hash(set
->p
[i
]);
5039 isl_hash_hash(hash
, bset_hash
);
5047 /* Check if the value for dimension dim is completely determined
5048 * by the values of the other parameters and variables.
5049 * That is, check if dimension dim is involved in an equality.
5051 int isl_basic_set_dim_is_unique(struct isl_basic_set
*bset
, unsigned dim
)
5058 nparam
= isl_basic_set_n_param(bset
);
5059 for (i
= 0; i
< bset
->n_eq
; ++i
)
5060 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ dim
]))
5065 /* Check if the value for dimension dim is completely determined
5066 * by the values of the other parameters and variables.
5067 * That is, check if dimension dim is involved in an equality
5068 * for each of the subsets.
5070 int isl_set_dim_is_unique(struct isl_set
*set
, unsigned dim
)
5076 for (i
= 0; i
< set
->n
; ++i
) {
5078 unique
= isl_basic_set_dim_is_unique(set
->p
[i
], dim
);