11 #include "isl_map_private.h"
12 #include "isl_map_piplib.h"
13 #include "isl_sample.h"
16 /* Maps dst positions to src positions */
22 static struct isl_dim_map
*isl_dim_map_alloc(struct isl_ctx
*ctx
, unsigned len
)
25 struct isl_dim_map
*dim_map
;
26 dim_map
= isl_alloc(ctx
, struct isl_dim_map
,
27 sizeof(struct isl_dim_map
) + len
* sizeof(int));
30 dim_map
->len
= 1 + len
;
32 for (i
= 0; i
< len
; ++i
)
33 dim_map
->pos
[1 + i
] = -1;
37 static unsigned n(struct isl_dim
*dim
, enum isl_dim_type type
)
40 case isl_dim_param
: return dim
->nparam
;
41 case isl_dim_in
: return dim
->n_in
;
42 case isl_dim_out
: return dim
->n_out
;
46 static unsigned pos(struct isl_dim
*dim
, enum isl_dim_type type
)
49 case isl_dim_param
: return 1;
50 case isl_dim_in
: return 1 + dim
->nparam
;
51 case isl_dim_out
: return 1 + dim
->nparam
+ dim
->n_in
;
55 static void isl_dim_map_dim(struct isl_dim_map
*dim_map
, struct isl_dim
*dim
,
56 enum isl_dim_type type
, unsigned dst_pos
)
64 src_pos
= pos(dim
, type
);
65 for (i
= 0; i
< n(dim
, type
); ++i
)
66 dim_map
->pos
[1 + dst_pos
+ i
] = src_pos
+ i
;
69 static void isl_dim_map_div(struct isl_dim_map
*dim_map
,
70 struct isl_basic_map
*bmap
, unsigned dst_pos
)
75 if (!dim_map
|| !bmap
)
78 src_pos
= 1 + isl_dim_total(bmap
->dim
);
79 for (i
= 0; i
< bmap
->n_div
; ++i
)
80 dim_map
->pos
[1 + dst_pos
+ i
] = src_pos
+ i
;
83 static void isl_dim_map_dump(struct isl_dim_map
*dim_map
)
87 for (i
= 0; i
< dim_map
->len
; ++i
)
88 fprintf(stderr
, "%d -> %d; ", i
, dim_map
->pos
[i
]);
89 fprintf(stderr
, "\n");
92 unsigned isl_basic_map_dim(const struct isl_basic_map
*bmap
,
93 enum isl_dim_type type
)
95 struct isl_dim
*dim
= bmap
->dim
;
99 case isl_dim_out
: return isl_dim_size(bmap
->dim
, type
);
100 case isl_dim_div
: return bmap
->n_div
;
101 case isl_dim_all
: return isl_basic_map_total_dim(bmap
);
105 unsigned isl_map_dim(const struct isl_map
*map
, enum isl_dim_type type
)
107 return n(map
->dim
, type
);
110 unsigned isl_set_dim(const struct isl_set
*set
, enum isl_dim_type type
)
112 return n(set
->dim
, type
);
115 unsigned isl_basic_map_offset(struct isl_basic_map
*bmap
,
116 enum isl_dim_type type
)
118 struct isl_dim
*dim
= bmap
->dim
;
120 case isl_dim_param
: return 1;
121 case isl_dim_in
: return 1 + dim
->nparam
;
122 case isl_dim_out
: return 1 + dim
->nparam
+ dim
->n_in
;
123 case isl_dim_div
: return 1 + dim
->nparam
+ dim
->n_in
+ dim
->n_out
;
127 static unsigned map_offset(struct isl_map
*map
, enum isl_dim_type type
)
129 return pos(map
->dim
, type
);
132 unsigned isl_basic_set_dim(const struct isl_basic_set
*bset
,
133 enum isl_dim_type type
)
135 return isl_basic_map_dim((const struct isl_basic_map
*)bset
, type
);
138 unsigned isl_basic_set_n_dim(const struct isl_basic_set
*bset
)
140 return bset
->dim
->n_out
;
143 unsigned isl_basic_set_n_param(const struct isl_basic_set
*bset
)
145 return bset
->dim
->nparam
;
148 unsigned isl_basic_set_total_dim(const struct isl_basic_set
*bset
)
150 return isl_dim_total(bset
->dim
) + bset
->n_div
;
153 unsigned isl_set_n_dim(const struct isl_set
*set
)
155 return set
->dim
->n_out
;
158 unsigned isl_set_n_param(const struct isl_set
*set
)
160 return set
->dim
->nparam
;
163 unsigned isl_basic_map_n_in(const struct isl_basic_map
*bmap
)
165 return bmap
->dim
->n_in
;
168 unsigned isl_basic_map_n_out(const struct isl_basic_map
*bmap
)
170 return bmap
->dim
->n_out
;
173 unsigned isl_basic_map_n_param(const struct isl_basic_map
*bmap
)
175 return bmap
->dim
->nparam
;
178 unsigned isl_basic_map_n_div(const struct isl_basic_map
*bmap
)
183 unsigned isl_basic_map_total_dim(const struct isl_basic_map
*bmap
)
185 return isl_dim_total(bmap
->dim
) + bmap
->n_div
;
188 unsigned isl_map_n_in(const struct isl_map
*map
)
190 return map
->dim
->n_in
;
193 unsigned isl_map_n_out(const struct isl_map
*map
)
195 return map
->dim
->n_out
;
198 unsigned isl_map_n_param(const struct isl_map
*map
)
200 return map
->dim
->nparam
;
203 int isl_map_compatible_domain(struct isl_map
*map
, struct isl_set
*set
)
205 return map
->dim
->n_in
== set
->dim
->n_out
&&
206 map
->dim
->nparam
== set
->dim
->nparam
;
209 int isl_basic_map_compatible_domain(struct isl_basic_map
*bmap
,
210 struct isl_basic_set
*bset
)
212 return bmap
->dim
->n_in
== bset
->dim
->n_out
&&
213 bmap
->dim
->nparam
== bset
->dim
->nparam
;
216 int isl_basic_map_compatible_range(struct isl_basic_map
*bmap
,
217 struct isl_basic_set
*bset
)
219 return bmap
->dim
->n_out
== bset
->dim
->n_out
&&
220 bmap
->dim
->nparam
== bset
->dim
->nparam
;
223 static struct isl_basic_map
*basic_map_init(struct isl_ctx
*ctx
,
224 struct isl_basic_map
*bmap
, unsigned extra
,
225 unsigned n_eq
, unsigned n_ineq
)
228 size_t row_size
= 1 + isl_dim_total(bmap
->dim
) + extra
;
230 bmap
->block
= isl_blk_alloc(ctx
, (n_ineq
+ n_eq
) * row_size
);
231 if (isl_blk_is_error(bmap
->block
)) {
236 bmap
->ineq
= isl_alloc_array(ctx
, isl_int
*, n_ineq
+ n_eq
);
238 isl_blk_free(ctx
, bmap
->block
);
244 bmap
->block2
= isl_blk_empty();
247 bmap
->block2
= isl_blk_alloc(ctx
, extra
* (1 + row_size
));
248 if (isl_blk_is_error(bmap
->block2
)) {
250 isl_blk_free(ctx
, bmap
->block
);
255 bmap
->div
= isl_alloc_array(ctx
, isl_int
*, extra
);
257 isl_blk_free(ctx
, bmap
->block2
);
259 isl_blk_free(ctx
, bmap
->block
);
265 for (i
= 0; i
< n_ineq
+ n_eq
; ++i
)
266 bmap
->ineq
[i
] = bmap
->block
.data
+ i
* row_size
;
268 for (i
= 0; i
< extra
; ++i
)
269 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + row_size
);
275 bmap
->c_size
= n_eq
+ n_ineq
;
276 bmap
->eq
= bmap
->ineq
+ n_ineq
;
285 isl_basic_map_free(bmap
);
289 struct isl_basic_set
*isl_basic_set_alloc(struct isl_ctx
*ctx
,
290 unsigned nparam
, unsigned dim
, unsigned extra
,
291 unsigned n_eq
, unsigned n_ineq
)
293 struct isl_basic_map
*bmap
;
294 bmap
= isl_basic_map_alloc(ctx
, nparam
, 0, dim
, extra
, n_eq
, n_ineq
);
295 return (struct isl_basic_set
*)bmap
;
298 struct isl_basic_set
*isl_basic_set_alloc_dim(struct isl_dim
*dim
,
299 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
301 struct isl_basic_map
*bmap
;
304 isl_assert(dim
->ctx
, dim
->n_in
== 0, return NULL
);
305 bmap
= isl_basic_map_alloc_dim(dim
, extra
, n_eq
, n_ineq
);
306 return (struct isl_basic_set
*)bmap
;
309 struct isl_basic_map
*isl_basic_map_alloc_dim(struct isl_dim
*dim
,
310 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
312 struct isl_basic_map
*bmap
;
316 bmap
= isl_alloc_type(dim
->ctx
, struct isl_basic_map
);
321 return basic_map_init(dim
->ctx
, bmap
, extra
, n_eq
, n_ineq
);
327 struct isl_basic_map
*isl_basic_map_alloc(struct isl_ctx
*ctx
,
328 unsigned nparam
, unsigned in
, unsigned out
, unsigned extra
,
329 unsigned n_eq
, unsigned n_ineq
)
331 struct isl_basic_map
*bmap
;
334 dim
= isl_dim_alloc(ctx
, nparam
, in
, out
);
338 bmap
= isl_basic_map_alloc_dim(dim
, extra
, n_eq
, n_ineq
);
342 static void dup_constraints(
343 struct isl_basic_map
*dst
, struct isl_basic_map
*src
)
346 unsigned total
= isl_basic_map_total_dim(src
);
348 for (i
= 0; i
< src
->n_eq
; ++i
) {
349 int j
= isl_basic_map_alloc_equality(dst
);
350 isl_seq_cpy(dst
->eq
[j
], src
->eq
[i
], 1+total
);
353 for (i
= 0; i
< src
->n_ineq
; ++i
) {
354 int j
= isl_basic_map_alloc_inequality(dst
);
355 isl_seq_cpy(dst
->ineq
[j
], src
->ineq
[i
], 1+total
);
358 for (i
= 0; i
< src
->n_div
; ++i
) {
359 int j
= isl_basic_map_alloc_div(dst
);
360 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
);
362 ISL_F_SET(dst
, ISL_BASIC_SET_FINAL
);
365 struct isl_basic_map
*isl_basic_map_dup(struct isl_basic_map
*bmap
)
367 struct isl_basic_map
*dup
;
371 dup
= isl_basic_map_alloc_dim(isl_dim_copy(bmap
->dim
),
372 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
375 dup_constraints(dup
, bmap
);
376 dup
->flags
= bmap
->flags
;
377 dup
->sample
= isl_vec_copy(bmap
->ctx
, bmap
->sample
);
381 struct isl_basic_set
*isl_basic_set_dup(struct isl_basic_set
*bset
)
383 struct isl_basic_map
*dup
;
385 dup
= isl_basic_map_dup((struct isl_basic_map
*)bset
);
386 return (struct isl_basic_set
*)dup
;
389 struct isl_basic_set
*isl_basic_set_copy(struct isl_basic_set
*bset
)
394 if (ISL_F_ISSET(bset
, ISL_BASIC_SET_FINAL
)) {
398 return isl_basic_set_dup(bset
);
401 struct isl_set
*isl_set_copy(struct isl_set
*set
)
410 struct isl_basic_map
*isl_basic_map_copy(struct isl_basic_map
*bmap
)
415 if (ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
)) {
419 return isl_basic_map_dup(bmap
);
422 struct isl_map
*isl_map_copy(struct isl_map
*map
)
431 void isl_basic_map_free(struct isl_basic_map
*bmap
)
439 isl_ctx_deref(bmap
->ctx
);
441 isl_blk_free(bmap
->ctx
, bmap
->block2
);
443 isl_blk_free(bmap
->ctx
, bmap
->block
);
444 isl_vec_free(bmap
->ctx
, bmap
->sample
);
445 isl_dim_free(bmap
->dim
);
449 void isl_basic_set_free(struct isl_basic_set
*bset
)
451 isl_basic_map_free((struct isl_basic_map
*)bset
);
454 static int room_for_con(struct isl_basic_map
*bmap
, unsigned n
)
456 return bmap
->n_eq
+ bmap
->n_ineq
+ n
<= bmap
->c_size
;
459 int isl_basic_map_alloc_equality(struct isl_basic_map
*bmap
)
465 isl_assert(ctx
, room_for_con(bmap
, 1), return -1);
466 isl_assert(ctx
, (bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
<= bmap
->c_size
,
468 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
469 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
470 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
471 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
472 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
473 if ((bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
== bmap
->c_size
) {
475 int j
= isl_basic_map_alloc_inequality(bmap
);
479 bmap
->ineq
[j
] = bmap
->ineq
[bmap
->n_ineq
- 1];
480 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
487 isl_seq_clr(bmap
->eq
[bmap
->n_eq
] + 1 + isl_basic_map_total_dim(bmap
),
488 bmap
->extra
- bmap
->n_div
);
492 int isl_basic_set_alloc_equality(struct isl_basic_set
*bset
)
494 return isl_basic_map_alloc_equality((struct isl_basic_map
*)bset
);
497 int isl_basic_map_free_equality(struct isl_basic_map
*bmap
, unsigned n
)
501 isl_assert(bmap
->ctx
, n
<= bmap
->n_eq
, return -1);
506 int isl_basic_set_free_equality(struct isl_basic_set
*bset
, unsigned n
)
508 return isl_basic_map_free_equality((struct isl_basic_map
*)bset
, n
);
511 int isl_basic_map_drop_equality(struct isl_basic_map
*bmap
, unsigned pos
)
516 isl_assert(bmap
->ctx
, pos
< bmap
->n_eq
, return -1);
518 if (pos
!= bmap
->n_eq
- 1) {
520 bmap
->eq
[pos
] = bmap
->eq
[bmap
->n_eq
- 1];
521 bmap
->eq
[bmap
->n_eq
- 1] = t
;
527 int isl_basic_set_drop_equality(struct isl_basic_set
*bset
, unsigned pos
)
529 return isl_basic_map_drop_equality((struct isl_basic_map
*)bset
, pos
);
532 void isl_basic_map_inequality_to_equality(
533 struct isl_basic_map
*bmap
, unsigned pos
)
538 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
539 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
544 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
545 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
546 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
547 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
550 static int room_for_ineq(struct isl_basic_map
*bmap
, unsigned n
)
552 return bmap
->n_ineq
+ n
<= bmap
->eq
- bmap
->ineq
;
555 int isl_basic_map_alloc_inequality(struct isl_basic_map
*bmap
)
561 isl_assert(ctx
, room_for_ineq(bmap
, 1), return -1);
562 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
563 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
564 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
565 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
566 isl_seq_clr(bmap
->ineq
[bmap
->n_ineq
] +
567 1 + isl_basic_map_total_dim(bmap
),
568 bmap
->extra
- bmap
->n_div
);
569 return bmap
->n_ineq
++;
572 int isl_basic_set_alloc_inequality(struct isl_basic_set
*bset
)
574 return isl_basic_map_alloc_inequality((struct isl_basic_map
*)bset
);
577 int isl_basic_map_free_inequality(struct isl_basic_map
*bmap
, unsigned n
)
581 isl_assert(bmap
->ctx
, n
<= bmap
->n_ineq
, return -1);
586 int isl_basic_set_free_inequality(struct isl_basic_set
*bset
, unsigned n
)
588 return isl_basic_map_free_inequality((struct isl_basic_map
*)bset
, n
);
591 int isl_basic_map_drop_inequality(struct isl_basic_map
*bmap
, unsigned pos
)
596 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
598 if (pos
!= bmap
->n_ineq
- 1) {
600 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
601 bmap
->ineq
[bmap
->n_ineq
- 1] = t
;
602 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
608 int isl_basic_set_drop_inequality(struct isl_basic_set
*bset
, unsigned pos
)
610 return isl_basic_map_drop_inequality((struct isl_basic_map
*)bset
, pos
);
613 int isl_basic_map_alloc_div(struct isl_basic_map
*bmap
)
617 isl_assert(bmap
->ctx
, bmap
->n_div
< bmap
->extra
, return -1);
618 isl_seq_clr(bmap
->div
[bmap
->n_div
] +
619 1 + 1 + isl_basic_map_total_dim(bmap
),
620 bmap
->extra
- bmap
->n_div
);
621 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
622 return bmap
->n_div
++;
625 int isl_basic_set_alloc_div(struct isl_basic_set
*bset
)
627 return isl_basic_map_alloc_div((struct isl_basic_map
*)bset
);
630 int isl_basic_map_free_div(struct isl_basic_map
*bmap
, unsigned n
)
634 isl_assert(bmap
->ctx
, n
<= bmap
->n_div
, return -1);
639 /* Copy constraint from src to dst, putting the vars of src at offset
640 * dim_off in dst and the divs of src at offset div_off in dst.
641 * If both sets are actually map, then dim_off applies to the input
644 static void copy_constraint(struct isl_basic_map
*dst_map
, isl_int
*dst
,
645 struct isl_basic_map
*src_map
, isl_int
*src
,
646 unsigned in_off
, unsigned out_off
, unsigned div_off
)
648 unsigned src_nparam
= isl_basic_map_n_param(src_map
);
649 unsigned dst_nparam
= isl_basic_map_n_param(dst_map
);
650 unsigned src_in
= isl_basic_map_n_in(src_map
);
651 unsigned dst_in
= isl_basic_map_n_in(dst_map
);
652 unsigned src_out
= isl_basic_map_n_out(src_map
);
653 unsigned dst_out
= isl_basic_map_n_out(dst_map
);
654 isl_int_set(dst
[0], src
[0]);
655 isl_seq_cpy(dst
+1, src
+1, isl_min(dst_nparam
, src_nparam
));
656 if (dst_nparam
> src_nparam
)
657 isl_seq_clr(dst
+1+src_nparam
,
658 dst_nparam
- src_nparam
);
659 isl_seq_clr(dst
+1+dst_nparam
, in_off
);
660 isl_seq_cpy(dst
+1+dst_nparam
+in_off
,
662 isl_min(dst_in
-in_off
, src_in
));
663 if (dst_in
-in_off
> src_in
)
664 isl_seq_clr(dst
+1+dst_nparam
+in_off
+src_in
,
665 dst_in
- in_off
- src_in
);
666 isl_seq_clr(dst
+1+dst_nparam
+dst_in
, out_off
);
667 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+out_off
,
668 src
+1+src_nparam
+src_in
,
669 isl_min(dst_out
-out_off
, src_out
));
670 if (dst_out
-out_off
> src_out
)
671 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+out_off
+src_out
,
672 dst_out
- out_off
- src_out
);
673 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
, div_off
);
674 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+dst_out
+div_off
,
675 src
+1+src_nparam
+src_in
+src_out
,
676 isl_min(dst_map
->extra
-div_off
, src_map
->n_div
));
677 if (dst_map
->n_div
-div_off
> src_map
->n_div
)
678 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
+
679 div_off
+src_map
->n_div
,
680 dst_map
->n_div
- div_off
- src_map
->n_div
);
683 static void copy_div(struct isl_basic_map
*dst_map
, isl_int
*dst
,
684 struct isl_basic_map
*src_map
, isl_int
*src
,
685 unsigned in_off
, unsigned out_off
, unsigned div_off
)
687 isl_int_set(dst
[0], src
[0]);
688 copy_constraint(dst_map
, dst
+1, src_map
, src
+1, in_off
, out_off
, div_off
);
691 static struct isl_basic_map
*add_constraints(struct isl_basic_map
*bmap1
,
692 struct isl_basic_map
*bmap2
, unsigned i_pos
, unsigned o_pos
)
697 if (!bmap1
|| !bmap2
)
700 div_off
= bmap1
->n_div
;
702 for (i
= 0; i
< bmap2
->n_eq
; ++i
) {
703 int i1
= isl_basic_map_alloc_equality(bmap1
);
706 copy_constraint(bmap1
, bmap1
->eq
[i1
], bmap2
, bmap2
->eq
[i
],
707 i_pos
, o_pos
, div_off
);
710 for (i
= 0; i
< bmap2
->n_ineq
; ++i
) {
711 int i1
= isl_basic_map_alloc_inequality(bmap1
);
714 copy_constraint(bmap1
, bmap1
->ineq
[i1
], bmap2
, bmap2
->ineq
[i
],
715 i_pos
, o_pos
, div_off
);
718 for (i
= 0; i
< bmap2
->n_div
; ++i
) {
719 int i1
= isl_basic_map_alloc_div(bmap1
);
722 copy_div(bmap1
, bmap1
->div
[i1
], bmap2
, bmap2
->div
[i
],
723 i_pos
, o_pos
, div_off
);
726 isl_basic_map_free(bmap2
);
731 isl_basic_map_free(bmap1
);
732 isl_basic_map_free(bmap2
);
736 static void copy_constraint_dim_map(isl_int
*dst
, isl_int
*src
,
737 struct isl_dim_map
*dim_map
)
741 for (i
= 0; i
< dim_map
->len
; ++i
) {
742 if (dim_map
->pos
[i
] < 0)
743 isl_int_set_si(dst
[i
], 0);
745 isl_int_set(dst
[i
], src
[dim_map
->pos
[i
]]);
749 static void copy_div_dim_map(isl_int
*dst
, isl_int
*src
,
750 struct isl_dim_map
*dim_map
)
752 isl_int_set(dst
[0], src
[0]);
753 copy_constraint_dim_map(dst
+1, src
+1, dim_map
);
756 static struct isl_basic_map
*add_constraints_dim_map(struct isl_basic_map
*dst
,
757 struct isl_basic_map
*src
, struct isl_dim_map
*dim_map
)
761 if (!src
|| !dst
|| !dim_map
)
764 for (i
= 0; i
< src
->n_eq
; ++i
) {
765 int i1
= isl_basic_map_alloc_equality(dst
);
768 copy_constraint_dim_map(dst
->eq
[i1
], src
->eq
[i
], dim_map
);
771 for (i
= 0; i
< src
->n_ineq
; ++i
) {
772 int i1
= isl_basic_map_alloc_inequality(dst
);
775 copy_constraint_dim_map(dst
->ineq
[i1
], src
->ineq
[i
], dim_map
);
778 for (i
= 0; i
< src
->n_div
; ++i
) {
779 int i1
= isl_basic_map_alloc_div(dst
);
782 copy_div_dim_map(dst
->div
[i1
], src
->div
[i
], dim_map
);
786 isl_basic_map_free(src
);
791 isl_basic_map_free(src
);
792 isl_basic_map_free(dst
);
796 struct isl_basic_set
*isl_basic_set_add_constraints(struct isl_basic_set
*bset1
,
797 struct isl_basic_set
*bset2
, unsigned pos
)
799 return (struct isl_basic_set
*)
800 add_constraints((struct isl_basic_map
*)bset1
,
801 (struct isl_basic_map
*)bset2
, 0, pos
);
804 struct isl_basic_map
*isl_basic_map_extend_dim(struct isl_basic_map
*base
,
805 struct isl_dim
*dim
, unsigned extra
,
806 unsigned n_eq
, unsigned n_ineq
)
808 struct isl_basic_map
*ext
;
818 dims_ok
= isl_dim_equal(base
->dim
, dim
) &&
819 base
->extra
>= base
->n_div
+ extra
;
821 if (dims_ok
&& room_for_con(base
, n_eq
+ n_ineq
) &&
822 room_for_ineq(base
, n_ineq
)) {
827 isl_assert(base
->ctx
, base
->dim
->nparam
<= dim
->nparam
, goto error
);
828 isl_assert(base
->ctx
, base
->dim
->n_in
<= dim
->n_in
, goto error
);
829 isl_assert(base
->ctx
, base
->dim
->n_out
<= dim
->n_out
, goto error
);
830 extra
+= base
->extra
;
832 n_ineq
+= base
->n_ineq
;
834 ext
= isl_basic_map_alloc_dim(dim
, extra
, n_eq
, n_ineq
);
840 ext
= add_constraints(ext
, base
, 0, 0);
843 ISL_F_CLR(ext
, ISL_BASIC_SET_FINAL
);
850 isl_basic_map_free(base
);
854 struct isl_basic_set
*isl_basic_set_extend_dim(struct isl_basic_set
*base
,
855 struct isl_dim
*dim
, unsigned extra
,
856 unsigned n_eq
, unsigned n_ineq
)
858 return (struct isl_basic_set
*)
859 isl_basic_map_extend_dim((struct isl_basic_map
*)base
, dim
,
860 extra
, n_eq
, n_ineq
);
863 struct isl_basic_map
*isl_basic_map_extend_constraints(
864 struct isl_basic_map
*base
, unsigned n_eq
, unsigned n_ineq
)
868 return isl_basic_map_extend_dim(base
, isl_dim_copy(base
->dim
),
872 struct isl_basic_map
*isl_basic_map_extend(struct isl_basic_map
*base
,
873 unsigned nparam
, unsigned n_in
, unsigned n_out
, unsigned extra
,
874 unsigned n_eq
, unsigned n_ineq
)
876 struct isl_basic_map
*bmap
;
881 dim
= isl_dim_alloc(base
->ctx
, nparam
, n_in
, n_out
);
885 bmap
= isl_basic_map_extend_dim(base
, dim
, extra
, n_eq
, n_ineq
);
889 struct isl_basic_set
*isl_basic_set_extend(struct isl_basic_set
*base
,
890 unsigned nparam
, unsigned dim
, unsigned extra
,
891 unsigned n_eq
, unsigned n_ineq
)
893 return (struct isl_basic_set
*)
894 isl_basic_map_extend((struct isl_basic_map
*)base
,
895 nparam
, 0, dim
, extra
, n_eq
, n_ineq
);
898 struct isl_basic_set
*isl_basic_set_extend_constraints(
899 struct isl_basic_set
*base
, unsigned n_eq
, unsigned n_ineq
)
901 return (struct isl_basic_set
*)
902 isl_basic_map_extend_constraints((struct isl_basic_map
*)base
,
906 struct isl_basic_set
*isl_basic_set_cow(struct isl_basic_set
*bset
)
908 return (struct isl_basic_set
*)
909 isl_basic_map_cow((struct isl_basic_map
*)bset
);
912 struct isl_basic_map
*isl_basic_map_cow(struct isl_basic_map
*bmap
)
919 bmap
= isl_basic_map_dup(bmap
);
921 ISL_F_CLR(bmap
, ISL_BASIC_SET_FINAL
);
925 struct isl_set
*isl_set_cow(struct isl_set
*set
)
933 return isl_set_dup(set
);
936 struct isl_map
*isl_map_cow(struct isl_map
*map
)
944 return isl_map_dup(map
);
947 static void swap_vars(struct isl_blk blk
, isl_int
*a
,
948 unsigned a_len
, unsigned b_len
)
950 isl_seq_cpy(blk
.data
, a
+a_len
, b_len
);
951 isl_seq_cpy(blk
.data
+b_len
, a
, a_len
);
952 isl_seq_cpy(a
, blk
.data
, b_len
+a_len
);
955 struct isl_basic_set
*isl_basic_set_swap_vars(
956 struct isl_basic_set
*bset
, unsigned n
)
966 nparam
= isl_basic_set_n_param(bset
);
967 dim
= isl_basic_set_n_dim(bset
);
968 isl_assert(bset
->ctx
, n
<= dim
, goto error
);
973 bset
= isl_basic_set_cow(bset
);
977 blk
= isl_blk_alloc(bset
->ctx
, dim
);
978 if (isl_blk_is_error(blk
))
981 for (i
= 0; i
< bset
->n_eq
; ++i
)
983 bset
->eq
[i
]+1+nparam
, n
, dim
- n
);
985 for (i
= 0; i
< bset
->n_ineq
; ++i
)
987 bset
->ineq
[i
]+1+nparam
, n
, dim
- n
);
989 for (i
= 0; i
< bset
->n_div
; ++i
)
991 bset
->div
[i
]+1+1+nparam
, n
, dim
- n
);
993 isl_blk_free(bset
->ctx
, blk
);
995 ISL_F_CLR(bset
, ISL_BASIC_SET_NORMALIZED
);
999 isl_basic_set_free(bset
);
1003 struct isl_set
*isl_set_swap_vars(struct isl_set
*set
, unsigned n
)
1006 set
= isl_set_cow(set
);
1010 for (i
= 0; i
< set
->n
; ++i
) {
1011 set
->p
[i
] = isl_basic_set_swap_vars(set
->p
[i
], n
);
1017 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
1021 struct isl_basic_map
*isl_basic_map_set_to_empty(struct isl_basic_map
*bmap
)
1027 total
= isl_basic_map_total_dim(bmap
);
1028 isl_basic_map_free_div(bmap
, bmap
->n_div
);
1029 isl_basic_map_free_inequality(bmap
, bmap
->n_ineq
);
1031 isl_basic_map_free_equality(bmap
, bmap
->n_eq
-1);
1033 isl_basic_map_alloc_equality(bmap
);
1037 isl_int_set_si(bmap
->eq
[i
][0], 1);
1038 isl_seq_clr(bmap
->eq
[i
]+1, total
);
1039 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
1040 return isl_basic_map_finalize(bmap
);
1042 isl_basic_map_free(bmap
);
1046 struct isl_basic_set
*isl_basic_set_set_to_empty(struct isl_basic_set
*bset
)
1048 return (struct isl_basic_set
*)
1049 isl_basic_map_set_to_empty((struct isl_basic_map
*)bset
);
1052 static void swap_div(struct isl_basic_map
*bmap
, int a
, int b
)
1055 unsigned off
= isl_dim_total(bmap
->dim
);
1056 isl_int
*t
= bmap
->div
[a
];
1057 bmap
->div
[a
] = bmap
->div
[b
];
1060 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1061 isl_int_swap(bmap
->eq
[i
][1+off
+a
], bmap
->eq
[i
][1+off
+b
]);
1063 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1064 isl_int_swap(bmap
->ineq
[i
][1+off
+a
], bmap
->ineq
[i
][1+off
+b
]);
1066 for (i
= 0; i
< bmap
->n_div
; ++i
)
1067 isl_int_swap(bmap
->div
[i
][1+1+off
+a
], bmap
->div
[i
][1+1+off
+b
]);
1068 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1071 /* Eliminate the specified n dimensions starting at first from the
1072 * constraints using Fourier-Motzkin, The dimensions themselves
1075 struct isl_set
*isl_set_eliminate_dims(struct isl_set
*set
,
1076 unsigned first
, unsigned n
)
1086 set
= isl_set_cow(set
);
1089 isl_assert(set
->ctx
, first
+n
<= isl_set_n_dim(set
), goto error
);
1090 nparam
= isl_set_n_param(set
);
1092 for (i
= 0; i
< set
->n
; ++i
) {
1093 set
->p
[i
] = isl_basic_set_eliminate_vars(set
->p
[i
],
1104 /* Project out n dimensions starting at first using Fourier-Motzkin */
1105 struct isl_set
*isl_set_remove_dims(struct isl_set
*set
,
1106 unsigned first
, unsigned n
)
1108 set
= isl_set_eliminate_dims(set
, first
, n
);
1109 set
= isl_set_drop_dims(set
, first
, n
);
1113 struct isl_basic_set
*isl_basic_set_remove_divs(struct isl_basic_set
*bset
)
1115 bset
= isl_basic_set_eliminate_vars(bset
, isl_dim_total(bset
->dim
),
1123 struct isl_set
*isl_set_remove_divs(struct isl_set
*set
)
1132 set
= isl_set_cow(set
);
1136 for (i
= 0; i
< set
->n
; ++i
) {
1137 set
->p
[i
] = isl_basic_set_remove_divs(set
->p
[i
]);
1147 struct isl_basic_map
*isl_basic_map_remove(struct isl_basic_map
*bmap
,
1148 enum isl_dim_type type
, unsigned first
, unsigned n
)
1152 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
1156 bmap
= isl_basic_map_eliminate_vars(bmap
,
1157 isl_basic_map_offset(bmap
, type
) - 1 + first
, n
);
1158 bmap
= isl_basic_map_drop(bmap
, type
, first
, n
);
1161 isl_basic_map_free(bmap
);
1165 struct isl_map
*isl_map_remove(struct isl_map
*map
,
1166 enum isl_dim_type type
, unsigned first
, unsigned n
)
1174 map
= isl_map_cow(map
);
1177 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
1179 for (i
= 0; i
< map
->n
; ++i
) {
1180 map
->p
[i
] = isl_basic_map_eliminate_vars(map
->p
[i
],
1181 isl_basic_map_offset(map
->p
[i
], type
) - 1 + first
, n
);
1185 map
= isl_map_drop(map
, type
, first
, n
);
1192 /* Project out n inputs starting at first using Fourier-Motzkin */
1193 struct isl_map
*isl_map_remove_inputs(struct isl_map
*map
,
1194 unsigned first
, unsigned n
)
1196 return isl_map_remove(map
, isl_dim_in
, first
, n
);
1199 /* Project out n dimensions starting at first using Fourier-Motzkin */
1200 struct isl_basic_set
*isl_basic_set_remove_dims(struct isl_basic_set
*bset
,
1201 unsigned first
, unsigned n
)
1203 unsigned nparam
= isl_basic_set_n_param(bset
);
1204 bset
= isl_basic_set_eliminate_vars(bset
, nparam
+ first
, n
);
1205 bset
= isl_basic_set_drop_dims(bset
, first
, n
);
1209 static void dump_term(struct isl_basic_map
*bmap
,
1210 isl_int c
, int pos
, FILE *out
)
1213 unsigned in
= isl_basic_map_n_in(bmap
);
1214 unsigned dim
= in
+ isl_basic_map_n_out(bmap
);
1215 unsigned nparam
= isl_basic_map_n_param(bmap
);
1217 isl_int_print(out
, c
, 0);
1219 if (!isl_int_is_one(c
))
1220 isl_int_print(out
, c
, 0);
1221 if (pos
< 1 + nparam
) {
1222 name
= isl_dim_get_name(bmap
->dim
,
1223 isl_dim_param
, pos
- 1);
1225 fprintf(out
, "%s", name
);
1227 fprintf(out
, "p%d", pos
- 1);
1228 } else if (pos
< 1 + nparam
+ in
)
1229 fprintf(out
, "i%d", pos
- 1 - nparam
);
1230 else if (pos
< 1 + nparam
+ dim
)
1231 fprintf(out
, "o%d", pos
- 1 - nparam
- in
);
1233 fprintf(out
, "e%d", pos
- 1 - nparam
- dim
);
1237 static void dump_constraint_sign(struct isl_basic_map
*bmap
, isl_int
*c
,
1238 int sign
, FILE *out
)
1242 unsigned len
= 1 + isl_basic_map_total_dim(bmap
);
1246 for (i
= 0, first
= 1; i
< len
; ++i
) {
1247 if (isl_int_sgn(c
[i
]) * sign
<= 0)
1250 fprintf(out
, " + ");
1252 isl_int_abs(v
, c
[i
]);
1253 dump_term(bmap
, v
, i
, out
);
1260 static void dump_constraint(struct isl_basic_map
*bmap
, isl_int
*c
,
1261 const char *op
, FILE *out
, int indent
)
1265 fprintf(out
, "%*s", indent
, "");
1267 dump_constraint_sign(bmap
, c
, 1, out
);
1268 fprintf(out
, " %s ", op
);
1269 dump_constraint_sign(bmap
, c
, -1, out
);
1273 for (i
= bmap
->n_div
; i
< bmap
->extra
; ++i
) {
1274 if (isl_int_is_zero(c
[1+isl_dim_total(bmap
->dim
)+i
]))
1276 fprintf(out
, "%*s", indent
, "");
1277 fprintf(out
, "ERROR: unused div coefficient not zero\n");
1282 static void dump_constraints(struct isl_basic_map
*bmap
,
1283 isl_int
**c
, unsigned n
,
1284 const char *op
, FILE *out
, int indent
)
1288 for (i
= 0; i
< n
; ++i
)
1289 dump_constraint(bmap
, c
[i
], op
, out
, indent
);
1292 static void dump_affine(struct isl_basic_map
*bmap
, isl_int
*exp
, FILE *out
)
1296 unsigned total
= isl_basic_map_total_dim(bmap
);
1298 for (j
= 0; j
< 1 + total
; ++j
) {
1299 if (isl_int_is_zero(exp
[j
]))
1301 if (!first
&& isl_int_is_pos(exp
[j
]))
1303 dump_term(bmap
, exp
[j
], j
, out
);
1308 static void dump(struct isl_basic_map
*bmap
, FILE *out
, int indent
)
1312 dump_constraints(bmap
, bmap
->eq
, bmap
->n_eq
, "=", out
, indent
);
1313 dump_constraints(bmap
, bmap
->ineq
, bmap
->n_ineq
, ">=", out
, indent
);
1315 for (i
= 0; i
< bmap
->n_div
; ++i
) {
1316 fprintf(out
, "%*s", indent
, "");
1317 fprintf(out
, "e%d = [(", i
);
1318 dump_affine(bmap
, bmap
->div
[i
]+1, out
);
1320 isl_int_print(out
, bmap
->div
[i
][0], 0);
1321 fprintf(out
, "]\n");
1325 void isl_basic_set_dump(struct isl_basic_set
*bset
, FILE *out
, int indent
)
1328 fprintf(out
, "null basic set\n");
1332 fprintf(out
, "%*s", indent
, "");
1333 fprintf(out
, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
1334 bset
->ref
, bset
->dim
->nparam
, bset
->dim
->n_out
,
1335 bset
->extra
, bset
->flags
);
1336 dump((struct isl_basic_map
*)bset
, out
, indent
);
1339 void isl_basic_map_dump(struct isl_basic_map
*bmap
, FILE *out
, int indent
)
1342 fprintf(out
, "null basic map\n");
1346 fprintf(out
, "%*s", indent
, "");
1347 fprintf(out
, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
1348 "flags: %x, n_name: %d\n",
1350 bmap
->dim
->nparam
, bmap
->dim
->n_in
, bmap
->dim
->n_out
,
1351 bmap
->extra
, bmap
->flags
, bmap
->dim
->n_name
);
1352 dump(bmap
, out
, indent
);
1355 int isl_inequality_negate(struct isl_basic_map
*bmap
, unsigned pos
)
1360 total
= isl_basic_map_total_dim(bmap
);
1361 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
1362 isl_seq_neg(bmap
->ineq
[pos
], bmap
->ineq
[pos
], 1 + total
);
1363 isl_int_sub_ui(bmap
->ineq
[pos
][0], bmap
->ineq
[pos
][0], 1);
1364 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1368 struct isl_set
*isl_set_alloc_dim(struct isl_dim
*dim
, int n
, unsigned flags
)
1370 struct isl_set
*set
;
1374 isl_assert(dim
->ctx
, dim
->n_in
== 0, return NULL
);
1375 isl_assert(dim
->ctx
, n
>= 0, return NULL
);
1376 set
= isl_alloc(dim
->ctx
, struct isl_set
,
1377 sizeof(struct isl_set
) +
1378 n
* sizeof(struct isl_basic_set
*));
1382 set
->ctx
= dim
->ctx
;
1383 isl_ctx_ref(set
->ctx
);
1395 struct isl_set
*isl_set_alloc(struct isl_ctx
*ctx
,
1396 unsigned nparam
, unsigned dim
, int n
, unsigned flags
)
1398 struct isl_set
*set
;
1399 struct isl_dim
*dims
;
1401 dims
= isl_dim_alloc(ctx
, nparam
, 0, dim
);
1405 set
= isl_set_alloc_dim(dims
, n
, flags
);
1409 struct isl_set
*isl_set_dup(struct isl_set
*set
)
1412 struct isl_set
*dup
;
1417 dup
= isl_set_alloc_dim(isl_dim_copy(set
->dim
), set
->n
, set
->flags
);
1420 for (i
= 0; i
< set
->n
; ++i
)
1421 dup
= isl_set_add(dup
, isl_basic_set_copy(set
->p
[i
]));
1425 struct isl_set
*isl_set_from_basic_set(struct isl_basic_set
*bset
)
1427 struct isl_set
*set
;
1432 set
= isl_set_alloc_dim(isl_dim_copy(bset
->dim
), 1, ISL_MAP_DISJOINT
);
1434 isl_basic_set_free(bset
);
1437 return isl_set_add(set
, bset
);
1440 struct isl_map
*isl_map_from_basic_map(struct isl_basic_map
*bmap
)
1442 struct isl_map
*map
;
1447 map
= isl_map_alloc_dim(isl_dim_copy(bmap
->dim
), 1, ISL_MAP_DISJOINT
);
1449 isl_basic_map_free(bmap
);
1452 return isl_map_add(map
, bmap
);
1455 struct isl_set
*isl_set_add(struct isl_set
*set
, struct isl_basic_set
*bset
)
1459 isl_assert(set
->ctx
, isl_dim_equal(set
->dim
, bset
->dim
), goto error
);
1460 isl_assert(set
->ctx
, set
->n
< set
->size
, goto error
);
1461 set
->p
[set
->n
] = bset
;
1468 isl_basic_set_free(bset
);
1472 void isl_set_free(struct isl_set
*set
)
1482 isl_ctx_deref(set
->ctx
);
1483 for (i
= 0; i
< set
->n
; ++i
)
1484 isl_basic_set_free(set
->p
[i
]);
1485 isl_dim_free(set
->dim
);
1489 void isl_set_dump(struct isl_set
*set
, FILE *out
, int indent
)
1494 fprintf(out
, "null set\n");
1498 fprintf(out
, "%*s", indent
, "");
1499 fprintf(out
, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
1500 set
->ref
, set
->n
, set
->dim
->nparam
, set
->dim
->n_out
,
1502 for (i
= 0; i
< set
->n
; ++i
) {
1503 fprintf(out
, "%*s", indent
, "");
1504 fprintf(out
, "basic set %d:\n", i
);
1505 isl_basic_set_dump(set
->p
[i
], out
, indent
+4);
1509 void isl_map_dump(struct isl_map
*map
, FILE *out
, int indent
)
1514 fprintf(out
, "null map\n");
1518 fprintf(out
, "%*s", indent
, "");
1519 fprintf(out
, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
1520 "flags: %x, n_name: %d\n",
1521 map
->ref
, map
->n
, map
->dim
->nparam
, map
->dim
->n_in
,
1522 map
->dim
->n_out
, map
->flags
, map
->dim
->n_name
);
1523 for (i
= 0; i
< map
->n
; ++i
) {
1524 fprintf(out
, "%*s", indent
, "");
1525 fprintf(out
, "basic map %d:\n", i
);
1526 isl_basic_map_dump(map
->p
[i
], out
, indent
+4);
1530 struct isl_basic_map
*isl_basic_map_intersect_domain(
1531 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
1533 struct isl_basic_map
*bmap_domain
;
1534 struct isl_dim
*dim
;
1539 isl_assert(bset
->ctx
, isl_dim_match(bmap
->dim
, isl_dim_param
,
1540 bset
->dim
, isl_dim_param
), goto error
);
1542 if (isl_dim_size(bset
->dim
, isl_dim_set
) != 0)
1543 isl_assert(bset
->ctx
,
1544 isl_basic_map_compatible_domain(bmap
, bset
), goto error
);
1546 bmap
= isl_basic_map_cow(bmap
);
1547 bmap
= isl_basic_map_extend_dim(bmap
, isl_dim_copy(bmap
->dim
),
1548 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
1551 dim
= isl_dim_reverse(isl_dim_copy(bset
->dim
));
1552 bmap_domain
= isl_basic_map_from_basic_set(bset
, dim
);
1553 bmap
= add_constraints(bmap
, bmap_domain
, 0, 0);
1555 bmap
= isl_basic_map_simplify(bmap
);
1556 return isl_basic_map_finalize(bmap
);
1558 isl_basic_map_free(bmap
);
1559 isl_basic_set_free(bset
);
1563 struct isl_basic_map
*isl_basic_map_intersect_range(
1564 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
1566 struct isl_basic_map
*bmap_range
;
1571 isl_assert(bset
->ctx
, isl_dim_match(bmap
->dim
, isl_dim_param
,
1572 bset
->dim
, isl_dim_param
), goto error
);
1574 if (isl_dim_size(bset
->dim
, isl_dim_set
) != 0)
1575 isl_assert(bset
->ctx
,
1576 isl_basic_map_compatible_range(bmap
, bset
), goto error
);
1578 bmap
= isl_basic_map_cow(bmap
);
1579 bmap
= isl_basic_map_extend_dim(bmap
, isl_dim_copy(bmap
->dim
),
1580 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
1583 bmap_range
= isl_basic_map_from_basic_set(bset
, isl_dim_copy(bset
->dim
));
1584 bmap
= add_constraints(bmap
, bmap_range
, 0, 0);
1586 bmap
= isl_basic_map_simplify(bmap
);
1587 return isl_basic_map_finalize(bmap
);
1589 isl_basic_map_free(bmap
);
1590 isl_basic_set_free(bset
);
1594 static int basic_map_contains(struct isl_basic_map
*bmap
, struct isl_vec
*vec
)
1600 total
= 1 + isl_basic_map_total_dim(bmap
);
1601 if (total
!= vec
->size
)
1606 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
1607 isl_seq_inner_product(vec
->block
.data
, bmap
->eq
[i
], total
, &s
);
1608 if (!isl_int_is_zero(s
)) {
1614 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
1615 isl_seq_inner_product(vec
->block
.data
, bmap
->ineq
[i
], total
, &s
);
1616 if (isl_int_is_neg(s
)) {
1627 int isl_basic_set_contains(struct isl_basic_set
*bset
, struct isl_vec
*vec
)
1629 return basic_map_contains((struct isl_basic_map
*)bset
, vec
);
1632 struct isl_basic_map
*isl_basic_map_intersect(
1633 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
1635 struct isl_vec
*sample
= NULL
;
1637 if (!bmap1
|| !bmap2
)
1640 isl_assert(bmap1
->ctx
, isl_dim_match(bmap1
->dim
, isl_dim_param
,
1641 bmap2
->dim
, isl_dim_param
), goto error
);
1642 if (isl_dim_total(bmap1
->dim
) ==
1643 isl_dim_size(bmap1
->dim
, isl_dim_param
) &&
1644 isl_dim_total(bmap2
->dim
) !=
1645 isl_dim_size(bmap2
->dim
, isl_dim_param
))
1646 return isl_basic_map_intersect(bmap2
, bmap1
);
1648 if (isl_dim_total(bmap2
->dim
) !=
1649 isl_dim_size(bmap2
->dim
, isl_dim_param
))
1650 isl_assert(bmap1
->ctx
,
1651 isl_dim_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
1653 if (bmap1
->sample
&&
1654 basic_map_contains(bmap1
, bmap1
->sample
) > 0 &&
1655 basic_map_contains(bmap2
, bmap1
->sample
) > 0)
1656 sample
= isl_vec_copy(bmap1
->ctx
, bmap1
->sample
);
1657 else if (bmap2
->sample
&&
1658 basic_map_contains(bmap1
, bmap2
->sample
) > 0 &&
1659 basic_map_contains(bmap2
, bmap2
->sample
) > 0)
1660 sample
= isl_vec_copy(bmap2
->ctx
, bmap2
->sample
);
1662 bmap1
= isl_basic_map_cow(bmap1
);
1663 bmap1
= isl_basic_map_extend_dim(bmap1
, isl_dim_copy(bmap1
->dim
),
1664 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
1667 bmap1
= add_constraints(bmap1
, bmap2
, 0, 0);
1670 isl_vec_free(bmap1
->ctx
, bmap1
->sample
);
1671 bmap1
->sample
= sample
;
1674 bmap1
= isl_basic_map_simplify(bmap1
);
1675 return isl_basic_map_finalize(bmap1
);
1678 isl_vec_free(bmap1
->ctx
, sample
);
1679 isl_basic_map_free(bmap1
);
1680 isl_basic_map_free(bmap2
);
1684 struct isl_basic_set
*isl_basic_set_intersect(
1685 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
1687 return (struct isl_basic_set
*)
1688 isl_basic_map_intersect(
1689 (struct isl_basic_map
*)bset1
,
1690 (struct isl_basic_map
*)bset2
);
1693 struct isl_map
*isl_map_intersect(struct isl_map
*map1
, struct isl_map
*map2
)
1696 struct isl_map
*result
;
1702 isl_assert(map1
->ctx
, isl_dim_match(map1
->dim
, isl_dim_param
,
1703 map2
->dim
, isl_dim_param
), goto error
);
1704 if (isl_dim_total(map1
->dim
) ==
1705 isl_dim_size(map1
->dim
, isl_dim_param
) &&
1706 isl_dim_total(map2
->dim
) != isl_dim_size(map2
->dim
, isl_dim_param
))
1707 return isl_map_intersect(map2
, map1
);
1709 if (isl_dim_total(map2
->dim
) != isl_dim_size(map2
->dim
, isl_dim_param
))
1710 isl_assert(map1
->ctx
,
1711 isl_dim_equal(map1
->dim
, map2
->dim
), goto error
);
1713 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
1714 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
1715 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
1717 result
= isl_map_alloc_dim(isl_dim_copy(map1
->dim
),
1718 map1
->n
* map2
->n
, flags
);
1721 for (i
= 0; i
< map1
->n
; ++i
)
1722 for (j
= 0; j
< map2
->n
; ++j
) {
1723 struct isl_basic_map
*part
;
1724 part
= isl_basic_map_intersect(
1725 isl_basic_map_copy(map1
->p
[i
]),
1726 isl_basic_map_copy(map2
->p
[j
]));
1727 if (isl_basic_map_is_empty(part
))
1728 isl_basic_map_free(part
);
1730 result
= isl_map_add(result
, part
);
1743 struct isl_set
*isl_set_intersect(struct isl_set
*set1
, struct isl_set
*set2
)
1745 return (struct isl_set
*)
1746 isl_map_intersect((struct isl_map
*)set1
,
1747 (struct isl_map
*)set2
);
1750 struct isl_basic_map
*isl_basic_map_reverse(struct isl_basic_map
*bmap
)
1752 struct isl_dim
*dim
;
1753 struct isl_basic_set
*bset
;
1758 bmap
= isl_basic_map_cow(bmap
);
1761 dim
= isl_dim_reverse(isl_dim_copy(bmap
->dim
));
1762 in
= isl_basic_map_n_in(bmap
);
1763 bset
= isl_basic_set_from_basic_map(bmap
);
1764 bset
= isl_basic_set_swap_vars(bset
, in
);
1765 return isl_basic_map_from_basic_set(bset
, dim
);
1768 /* Turn final n dimensions into existentially quantified variables.
1770 struct isl_basic_set
*isl_basic_set_project_out(
1771 struct isl_basic_set
*bset
, unsigned n
, unsigned flags
)
1781 isl_assert(bset
->ctx
, n
<= isl_basic_set_n_dim(bset
), goto error
);
1786 bset
= isl_basic_set_cow(bset
);
1788 row_size
= 1 + isl_dim_total(bset
->dim
) + bset
->extra
;
1789 old
= bset
->block2
.data
;
1790 bset
->block2
= isl_blk_extend(bset
->ctx
, bset
->block2
,
1791 (bset
->extra
+ n
) * (1 + row_size
));
1792 if (!bset
->block2
.data
)
1794 new_div
= isl_alloc_array(ctx
, isl_int
*, bset
->extra
+ n
);
1797 for (i
= 0; i
< n
; ++i
) {
1798 new_div
[i
] = bset
->block2
.data
+
1799 (bset
->extra
+ i
) * (1 + row_size
);
1800 isl_seq_clr(new_div
[i
], 1 + row_size
);
1802 for (i
= 0; i
< bset
->extra
; ++i
)
1803 new_div
[n
+ i
] = bset
->block2
.data
+ (bset
->div
[i
] - old
);
1805 bset
->div
= new_div
;
1808 bset
->dim
= isl_dim_drop_outputs(bset
->dim
,
1809 isl_basic_set_n_dim(bset
) - n
, n
);
1812 bset
= isl_basic_set_simplify(bset
);
1813 return isl_basic_set_finalize(bset
);
1815 isl_basic_set_free(bset
);
1819 struct isl_basic_map
*add_divs(struct isl_basic_map
*bmap
, unsigned n
)
1823 for (i
= 0; i
< n
; ++i
) {
1824 j
= isl_basic_map_alloc_div(bmap
);
1827 isl_seq_clr(bmap
->div
[j
], 1+1+isl_basic_map_total_dim(bmap
));
1831 isl_basic_map_free(bmap
);
1835 struct isl_basic_map
*isl_basic_map_apply_range(
1836 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
1838 struct isl_dim
*dim_result
= NULL
;
1839 struct isl_basic_map
*bmap
;
1840 unsigned n_in
, n_out
, n
, nparam
, total
, pos
;
1841 struct isl_dim_map
*dim_map1
, *dim_map2
;
1843 if (!bmap1
|| !bmap2
)
1846 dim_result
= isl_dim_join(isl_dim_copy(bmap1
->dim
),
1847 isl_dim_copy(bmap2
->dim
));
1849 n_in
= isl_basic_map_n_in(bmap1
);
1850 n_out
= isl_basic_map_n_out(bmap2
);
1851 n
= isl_basic_map_n_out(bmap1
);
1852 nparam
= isl_basic_map_n_param(bmap1
);
1854 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ n
;
1855 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
1856 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
1857 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
1858 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
1859 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
1860 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_in
);
1861 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_out
);
1862 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
1863 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
1864 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
1866 bmap
= isl_basic_map_alloc_dim(dim_result
,
1867 bmap1
->n_div
+ bmap2
->n_div
+ n
,
1868 bmap1
->n_eq
+ bmap2
->n_eq
,
1869 bmap1
->n_ineq
+ bmap2
->n_ineq
);
1870 bmap
= add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
1871 bmap
= add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
1872 bmap
= add_divs(bmap
, n
);
1873 bmap
= isl_basic_map_simplify(bmap
);
1874 return isl_basic_map_finalize(bmap
);
1876 isl_basic_map_free(bmap1
);
1877 isl_basic_map_free(bmap2
);
1881 struct isl_basic_set
*isl_basic_set_apply(
1882 struct isl_basic_set
*bset
, struct isl_basic_map
*bmap
)
1887 isl_assert(set
->ctx
, isl_basic_map_compatible_domain(bmap
, bset
),
1890 return (struct isl_basic_set
*)
1891 isl_basic_map_apply_range((struct isl_basic_map
*)bset
, bmap
);
1893 isl_basic_set_free(bset
);
1894 isl_basic_map_free(bmap
);
1898 struct isl_basic_map
*isl_basic_map_apply_domain(
1899 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
1901 if (!bmap1
|| !bmap2
)
1905 isl_basic_map_n_in(bmap1
) == isl_basic_map_n_in(bmap2
), goto error
);
1907 isl_basic_map_n_param(bmap1
) == isl_basic_map_n_param(bmap2
),
1910 bmap1
= isl_basic_map_reverse(bmap1
);
1911 bmap1
= isl_basic_map_apply_range(bmap1
, bmap2
);
1912 return isl_basic_map_reverse(bmap1
);
1914 isl_basic_map_free(bmap1
);
1915 isl_basic_map_free(bmap2
);
1919 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
1920 * A \cap B -> f(A) + f(B)
1922 struct isl_basic_map
*isl_basic_map_sum(
1923 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
1925 unsigned n_in
, n_out
, nparam
, total
, pos
;
1926 struct isl_basic_map
*bmap
= NULL
;
1927 struct isl_dim_map
*dim_map1
, *dim_map2
;
1930 if (!bmap1
|| !bmap2
)
1933 isl_assert(bmap1
->ctx
, isl_dim_equal(bmap1
->dim
, bmap2
->dim
),
1936 nparam
= isl_basic_map_n_param(bmap1
);
1937 n_in
= isl_basic_map_n_in(bmap1
);
1938 n_out
= isl_basic_map_n_out(bmap1
);
1940 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
;
1941 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
1942 dim_map2
= isl_dim_map_alloc(bmap2
->ctx
, total
);
1943 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
1944 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
);
1945 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
1946 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
1947 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_in
+ n_out
);
1948 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
1949 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
1950 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_out
);
1952 bmap
= isl_basic_map_alloc_dim(isl_dim_copy(bmap1
->dim
),
1953 bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
,
1954 bmap1
->n_eq
+ bmap2
->n_eq
+ n_out
,
1955 bmap1
->n_ineq
+ bmap2
->n_ineq
);
1956 for (i
= 0; i
< n_out
; ++i
) {
1957 int j
= isl_basic_map_alloc_equality(bmap
);
1960 isl_seq_clr(bmap
->eq
[j
], 1+total
);
1961 isl_int_set_si(bmap
->eq
[j
][1+nparam
+n_in
+i
], -1);
1962 isl_int_set_si(bmap
->eq
[j
][1+pos
+i
], 1);
1963 isl_int_set_si(bmap
->eq
[j
][1+pos
-n_out
+i
], 1);
1965 bmap
= add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
1966 bmap
= add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
1967 bmap
= add_divs(bmap
, 2 * n_out
);
1969 bmap
= isl_basic_map_simplify(bmap
);
1970 return isl_basic_map_finalize(bmap
);
1972 isl_basic_map_free(bmap
);
1973 isl_basic_map_free(bmap1
);
1974 isl_basic_map_free(bmap2
);
1978 /* Given a basic map A -> f(A), construct A -> -f(A).
1980 struct isl_basic_map
*isl_basic_map_neg(struct isl_basic_map
*bmap
)
1985 bmap
= isl_basic_map_cow(bmap
);
1989 n
= isl_basic_map_dim(bmap
, isl_dim_out
);
1990 off
= isl_basic_map_offset(bmap
, isl_dim_out
);
1991 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1992 for (j
= 0; j
< n
; ++j
)
1993 isl_int_neg(bmap
->eq
[i
][off
+j
], bmap
->eq
[i
][off
+j
]);
1994 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1995 for (j
= 0; j
< n
; ++j
)
1996 isl_int_neg(bmap
->ineq
[i
][off
+j
], bmap
->ineq
[i
][off
+j
]);
1997 for (i
= 0; i
< bmap
->n_div
; ++i
)
1998 for (j
= 0; j
< n
; ++j
)
1999 isl_int_neg(bmap
->div
[i
][1+off
+j
], bmap
->div
[i
][1+off
+j
]);
2000 return isl_basic_map_finalize(bmap
);
2003 /* Given a basic map A -> f(A) and an integer d, construct a basic map
2004 * A -> floor(f(A)/d).
2006 struct isl_basic_map
*isl_basic_map_floordiv(struct isl_basic_map
*bmap
,
2009 unsigned n_in
, n_out
, nparam
, total
, pos
;
2010 struct isl_basic_map
*result
= NULL
;
2011 struct isl_dim_map
*dim_map
;
2017 nparam
= isl_basic_map_n_param(bmap
);
2018 n_in
= isl_basic_map_n_in(bmap
);
2019 n_out
= isl_basic_map_n_out(bmap
);
2021 total
= nparam
+ n_in
+ n_out
+ bmap
->n_div
+ n_out
;
2022 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
2023 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_param
, pos
= 0);
2024 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_in
, pos
+= nparam
);
2025 isl_dim_map_div(dim_map
, bmap
, pos
+= n_in
+ n_out
);
2026 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_out
, pos
+= bmap
->n_div
);
2028 result
= isl_basic_map_alloc_dim(isl_dim_copy(bmap
->dim
),
2029 bmap
->n_div
+ n_out
,
2030 bmap
->n_eq
, bmap
->n_ineq
+ 2 * n_out
);
2031 result
= add_constraints_dim_map(result
, bmap
, dim_map
);
2032 result
= add_divs(result
, n_out
);
2033 for (i
= 0; i
< n_out
; ++i
) {
2035 j
= isl_basic_map_alloc_inequality(result
);
2038 isl_seq_clr(result
->ineq
[j
], 1+total
);
2039 isl_int_neg(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
2040 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], 1);
2041 j
= isl_basic_map_alloc_inequality(result
);
2044 isl_seq_clr(result
->ineq
[j
], 1+total
);
2045 isl_int_set(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
2046 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], -1);
2047 isl_int_sub_ui(result
->ineq
[j
][0], d
, 1);
2050 result
= isl_basic_map_simplify(result
);
2051 return isl_basic_map_finalize(result
);
2053 isl_basic_map_free(result
);
2057 static struct isl_basic_map
*var_equal(struct isl_basic_map
*bmap
, unsigned pos
)
2063 i
= isl_basic_map_alloc_equality(bmap
);
2066 nparam
= isl_basic_map_n_param(bmap
);
2067 n_in
= isl_basic_map_n_in(bmap
);
2068 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
2069 isl_int_set_si(bmap
->eq
[i
][1+nparam
+pos
], -1);
2070 isl_int_set_si(bmap
->eq
[i
][1+nparam
+n_in
+pos
], 1);
2071 return isl_basic_map_finalize(bmap
);
2073 isl_basic_map_free(bmap
);
2077 static struct isl_basic_map
*var_more(struct isl_basic_map
*bmap
, unsigned pos
)
2083 i
= isl_basic_map_alloc_inequality(bmap
);
2086 nparam
= isl_basic_map_n_param(bmap
);
2087 n_in
= isl_basic_map_n_in(bmap
);
2088 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
2089 isl_int_set_si(bmap
->ineq
[i
][0], -1);
2090 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
2091 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
2092 return isl_basic_map_finalize(bmap
);
2094 isl_basic_map_free(bmap
);
2098 static struct isl_basic_map
*var_less(struct isl_basic_map
*bmap
, unsigned pos
)
2104 i
= isl_basic_map_alloc_inequality(bmap
);
2107 nparam
= isl_basic_map_n_param(bmap
);
2108 n_in
= isl_basic_map_n_in(bmap
);
2109 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
2110 isl_int_set_si(bmap
->ineq
[i
][0], -1);
2111 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
2112 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
2113 return isl_basic_map_finalize(bmap
);
2115 isl_basic_map_free(bmap
);
2119 struct isl_basic_map
*isl_basic_map_equal(struct isl_dim
*dim
, unsigned n_equal
)
2122 struct isl_basic_map
*bmap
;
2123 bmap
= isl_basic_map_alloc_dim(dim
, 0, n_equal
, 0);
2126 for (i
= 0; i
< n_equal
&& bmap
; ++i
)
2127 bmap
= var_equal(bmap
, i
);
2128 return isl_basic_map_finalize(bmap
);
2131 struct isl_basic_map
*isl_basic_map_less_at(struct isl_dim
*dim
, unsigned pos
)
2134 struct isl_basic_map
*bmap
;
2135 bmap
= isl_basic_map_alloc_dim(dim
, 0, pos
, 1);
2138 for (i
= 0; i
< pos
&& bmap
; ++i
)
2139 bmap
= var_equal(bmap
, i
);
2141 bmap
= var_less(bmap
, pos
);
2142 return isl_basic_map_finalize(bmap
);
2145 struct isl_basic_map
*isl_basic_map_more_at(struct isl_dim
*dim
, unsigned pos
)
2148 struct isl_basic_map
*bmap
;
2149 bmap
= isl_basic_map_alloc_dim(dim
, 0, pos
, 1);
2152 for (i
= 0; i
< pos
&& bmap
; ++i
)
2153 bmap
= var_equal(bmap
, i
);
2155 bmap
= var_more(bmap
, pos
);
2156 return isl_basic_map_finalize(bmap
);
2159 struct isl_basic_map
*isl_basic_map_from_basic_set(
2160 struct isl_basic_set
*bset
, struct isl_dim
*dim
)
2162 struct isl_basic_map
*bmap
;
2164 bset
= isl_basic_set_cow(bset
);
2168 isl_assert(bset
->ctx
, isl_dim_compatible(bset
->dim
, dim
), goto error
);
2169 isl_dim_free(bset
->dim
);
2170 bmap
= (struct isl_basic_map
*) bset
;
2172 return isl_basic_map_finalize(bmap
);
2174 isl_basic_set_free(bset
);
2179 struct isl_basic_set
*isl_basic_set_from_basic_map(struct isl_basic_map
*bmap
)
2183 if (bmap
->dim
->n_in
== 0)
2184 return (struct isl_basic_set
*)bmap
;
2185 bmap
= isl_basic_map_cow(bmap
);
2188 bmap
->dim
= isl_dim_cow(bmap
->dim
);
2191 bmap
->dim
->n_out
+= bmap
->dim
->n_in
;
2192 bmap
->dim
->n_in
= 0;
2193 bmap
= isl_basic_map_finalize(bmap
);
2194 return (struct isl_basic_set
*)bmap
;
2196 isl_basic_map_free(bmap
);
2200 /* For a div d = floor(f/m), add the constraints
2203 * -(f-(n-1)) + m d >= 0
2205 * Note that the second constraint is the negation of
2209 static int add_div_constraints(struct isl_basic_map
*bmap
, unsigned div
)
2212 unsigned total
= isl_basic_map_total_dim(bmap
);
2213 unsigned div_pos
= 1 + total
- bmap
->n_div
+ div
;
2215 i
= isl_basic_map_alloc_inequality(bmap
);
2218 isl_seq_cpy(bmap
->ineq
[i
], bmap
->div
[div
]+1, 1+total
);
2219 isl_int_neg(bmap
->ineq
[i
][div_pos
], bmap
->div
[div
][0]);
2221 j
= isl_basic_map_alloc_inequality(bmap
);
2224 isl_seq_neg(bmap
->ineq
[j
], bmap
->ineq
[i
], 1 + total
);
2225 isl_int_add(bmap
->ineq
[j
][0], bmap
->ineq
[j
][0], bmap
->ineq
[j
][div_pos
]);
2226 isl_int_sub_ui(bmap
->ineq
[j
][0], bmap
->ineq
[j
][0], 1);
2230 struct isl_basic_set
*isl_basic_map_underlying_set(
2231 struct isl_basic_map
*bmap
)
2235 if (bmap
->dim
->nparam
== 0 && bmap
->dim
->n_in
== 0 && bmap
->n_div
== 0)
2236 return (struct isl_basic_set
*)bmap
;
2237 bmap
= isl_basic_map_cow(bmap
);
2240 bmap
->dim
= isl_dim_underlying(bmap
->dim
, bmap
->n_div
);
2243 bmap
->extra
-= bmap
->n_div
;
2245 bmap
= isl_basic_map_finalize(bmap
);
2246 return (struct isl_basic_set
*)bmap
;
2251 struct isl_basic_map
*isl_basic_map_overlying_set(
2252 struct isl_basic_set
*bset
, struct isl_basic_map
*like
)
2254 struct isl_basic_map
*bmap
;
2255 struct isl_ctx
*ctx
;
2262 isl_assert(ctx
, bset
->n_div
== 0, goto error
);
2263 isl_assert(ctx
, isl_basic_set_n_param(bset
) == 0, goto error
);
2264 isl_assert(ctx
, bset
->dim
->n_out
== isl_basic_map_total_dim(like
),
2266 if (isl_dim_equal(bset
->dim
, like
->dim
) && like
->n_div
== 0) {
2267 isl_basic_map_free(like
);
2268 return (struct isl_basic_map
*)bset
;
2270 bset
= isl_basic_set_cow(bset
);
2273 total
= bset
->dim
->n_out
+ bset
->extra
;
2274 bmap
= (struct isl_basic_map
*)bset
;
2275 isl_dim_free(bmap
->dim
);
2276 bmap
->dim
= isl_dim_copy(like
->dim
);
2279 bmap
->n_div
= like
->n_div
;
2280 bmap
->extra
+= like
->n_div
;
2283 ltotal
= total
- bmap
->extra
+ like
->extra
;
2286 bmap
->block2
= isl_blk_extend(ctx
, bmap
->block2
,
2287 bmap
->extra
* (1 + 1 + total
));
2288 if (isl_blk_is_error(bmap
->block2
))
2290 bmap
->div
= isl_realloc_array(ctx
, bmap
->div
, isl_int
*,
2294 for (i
= 0; i
< bmap
->extra
; ++i
)
2295 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + 1 + total
);
2296 for (i
= 0; i
< like
->n_div
; ++i
) {
2297 isl_seq_cpy(bmap
->div
[i
], like
->div
[i
], 1 + 1 + ltotal
);
2298 isl_seq_clr(bmap
->div
[i
]+1+1+ltotal
, total
- ltotal
);
2300 bmap
= isl_basic_map_extend_constraints(bmap
,
2301 0, 2 * like
->n_div
);
2302 for (i
= 0; i
< like
->n_div
; ++i
) {
2303 if (isl_int_is_zero(bmap
->div
[i
][0]))
2305 if (add_div_constraints(bmap
, i
) < 0)
2309 isl_basic_map_free(like
);
2310 bmap
= isl_basic_map_simplify(bmap
);
2311 bmap
= isl_basic_map_finalize(bmap
);
2314 isl_basic_map_free(like
);
2315 isl_basic_set_free(bset
);
2319 struct isl_basic_set
*isl_basic_set_from_underlying_set(
2320 struct isl_basic_set
*bset
, struct isl_basic_set
*like
)
2322 return (struct isl_basic_set
*)
2323 isl_basic_map_overlying_set(bset
, (struct isl_basic_map
*)like
);
2326 struct isl_set
*isl_set_from_underlying_set(
2327 struct isl_set
*set
, struct isl_basic_set
*like
)
2333 isl_assert(set
->ctx
, set
->dim
->n_out
== isl_basic_set_total_dim(like
),
2335 if (isl_dim_equal(set
->dim
, like
->dim
) && like
->n_div
== 0) {
2336 isl_basic_set_free(like
);
2339 set
= isl_set_cow(set
);
2342 for (i
= 0; i
< set
->n
; ++i
) {
2343 set
->p
[i
] = isl_basic_set_from_underlying_set(set
->p
[i
],
2344 isl_basic_set_copy(like
));
2348 isl_dim_free(set
->dim
);
2349 set
->dim
= isl_dim_copy(like
->dim
);
2352 isl_basic_set_free(like
);
2355 isl_basic_set_free(like
);
2360 struct isl_set
*isl_map_underlying_set(struct isl_map
*map
)
2364 map
= isl_map_cow(map
);
2367 map
->dim
= isl_dim_cow(map
->dim
);
2371 for (i
= 1; i
< map
->n
; ++i
)
2372 isl_assert(map
->ctx
, map
->p
[0]->n_div
== map
->p
[i
]->n_div
,
2374 for (i
= 0; i
< map
->n
; ++i
) {
2375 map
->p
[i
] = (struct isl_basic_map
*)
2376 isl_basic_map_underlying_set(map
->p
[i
]);
2381 map
->dim
= isl_dim_underlying(map
->dim
, 0);
2383 isl_dim_free(map
->dim
);
2384 map
->dim
= isl_dim_copy(map
->p
[0]->dim
);
2388 return (struct isl_set
*)map
;
2394 struct isl_set
*isl_set_to_underlying_set(struct isl_set
*set
)
2396 return (struct isl_set
*)isl_map_underlying_set((struct isl_map
*)set
);
2399 struct isl_basic_set
*isl_basic_map_domain(struct isl_basic_map
*bmap
)
2401 struct isl_basic_set
*domain
;
2405 n_out
= isl_basic_map_n_out(bmap
);
2406 domain
= isl_basic_set_from_basic_map(bmap
);
2407 return isl_basic_set_project_out(domain
, n_out
, 0);
2410 struct isl_basic_set
*isl_basic_map_range(struct isl_basic_map
*bmap
)
2412 return isl_basic_map_domain(isl_basic_map_reverse(bmap
));
2415 struct isl_set
*isl_map_range(struct isl_map
*map
)
2418 struct isl_set
*set
;
2422 map
= isl_map_cow(map
);
2426 set
= (struct isl_set
*) map
;
2427 if (set
->dim
->n_in
!= 0) {
2428 set
->dim
= isl_dim_drop_inputs(set
->dim
, 0, set
->dim
->n_in
);
2432 for (i
= 0; i
< map
->n
; ++i
) {
2433 set
->p
[i
] = isl_basic_map_range(map
->p
[i
]);
2437 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
2438 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
2445 struct isl_map
*isl_map_from_set(struct isl_set
*set
, struct isl_dim
*dim
)
2448 struct isl_map
*map
= NULL
;
2450 set
= isl_set_cow(set
);
2453 isl_assert(set
->ctx
, isl_dim_compatible(set
->dim
, dim
), goto error
);
2454 map
= (struct isl_map
*)set
;
2455 for (i
= 0; i
< set
->n
; ++i
) {
2456 map
->p
[i
] = isl_basic_map_from_basic_set(
2457 set
->p
[i
], isl_dim_copy(dim
));
2461 isl_dim_free(map
->dim
);
2470 struct isl_map
*isl_map_from_range(struct isl_set
*set
)
2472 return (struct isl_map
*)set
;
2475 struct isl_set
*isl_set_from_map(struct isl_map
*map
)
2478 struct isl_set
*set
= NULL
;
2482 map
= isl_map_cow(map
);
2485 map
->dim
= isl_dim_cow(map
->dim
);
2488 map
->dim
->n_out
+= map
->dim
->n_in
;
2490 set
= (struct isl_set
*)map
;
2491 for (i
= 0; i
< map
->n
; ++i
) {
2492 set
->p
[i
] = isl_basic_set_from_basic_map(map
->p
[i
]);
2502 struct isl_map
*isl_map_alloc_dim(struct isl_dim
*dim
, int n
, unsigned flags
)
2504 struct isl_map
*map
;
2508 isl_assert(dim
->ctx
, n
>= 0, return NULL
);
2509 map
= isl_alloc(dim
->ctx
, struct isl_map
,
2510 sizeof(struct isl_map
) +
2511 n
* sizeof(struct isl_basic_map
*));
2515 map
->ctx
= dim
->ctx
;
2516 isl_ctx_ref(map
->ctx
);
2528 struct isl_map
*isl_map_alloc(struct isl_ctx
*ctx
,
2529 unsigned nparam
, unsigned in
, unsigned out
, int n
,
2532 struct isl_map
*map
;
2533 struct isl_dim
*dims
;
2535 dims
= isl_dim_alloc(ctx
, nparam
, in
, out
);
2539 map
= isl_map_alloc_dim(dims
, n
, flags
);
2543 struct isl_basic_map
*isl_basic_map_empty(struct isl_ctx
*ctx
,
2544 unsigned nparam
, unsigned in
, unsigned out
)
2546 struct isl_basic_map
*bmap
;
2547 bmap
= isl_basic_map_alloc(ctx
, nparam
, in
, out
, 0, 1, 0);
2548 bmap
= isl_basic_map_set_to_empty(bmap
);
2552 struct isl_basic_set
*isl_basic_set_empty(struct isl_dim
*dim
)
2554 struct isl_basic_set
*bset
;
2555 bset
= isl_basic_set_alloc_dim(dim
, 0, 1, 0);
2556 bset
= isl_basic_set_set_to_empty(bset
);
2560 struct isl_basic_map
*isl_basic_map_empty_like(struct isl_basic_map
*model
)
2562 struct isl_basic_map
*bmap
;
2565 bmap
= isl_basic_map_alloc_dim(isl_dim_copy(model
->dim
), 0, 1, 0);
2566 bmap
= isl_basic_map_set_to_empty(bmap
);
2570 struct isl_basic_map
*isl_basic_map_empty_like_map(struct isl_map
*model
)
2572 struct isl_basic_map
*bmap
;
2575 bmap
= isl_basic_map_alloc_dim(isl_dim_copy(model
->dim
), 0, 1, 0);
2576 bmap
= isl_basic_map_set_to_empty(bmap
);
2580 struct isl_basic_set
*isl_basic_set_empty_like(struct isl_basic_set
*model
)
2582 struct isl_basic_set
*bset
;
2585 bset
= isl_basic_set_alloc_dim(isl_dim_copy(model
->dim
), 0, 1, 0);
2586 bset
= isl_basic_set_set_to_empty(bset
);
2590 struct isl_basic_map
*isl_basic_map_universe(struct isl_dim
*dim
)
2592 struct isl_basic_map
*bmap
;
2593 bmap
= isl_basic_map_alloc_dim(dim
, 0, 0, 0);
2597 struct isl_basic_set
*isl_basic_set_universe(struct isl_dim
*dim
)
2599 struct isl_basic_set
*bset
;
2600 bset
= isl_basic_set_alloc_dim(dim
, 0, 0, 0);
2604 struct isl_basic_set
*isl_basic_set_universe_like(struct isl_basic_set
*model
)
2608 return isl_basic_set_alloc_dim(isl_dim_copy(model
->dim
), 0, 0, 0);
2611 struct isl_map
*isl_map_empty(struct isl_dim
*dim
)
2613 return isl_map_alloc_dim(dim
, 0, ISL_MAP_DISJOINT
);
2616 struct isl_map
*isl_map_empty_like(struct isl_map
*model
)
2620 return isl_map_alloc_dim(isl_dim_copy(model
->dim
), 0, ISL_MAP_DISJOINT
);
2623 struct isl_map
*isl_map_empty_like_basic_map(struct isl_basic_map
*model
)
2627 return isl_map_alloc_dim(isl_dim_copy(model
->dim
), 0, ISL_MAP_DISJOINT
);
2630 struct isl_set
*isl_set_empty(struct isl_dim
*dim
)
2632 return isl_set_alloc_dim(dim
, 0, ISL_MAP_DISJOINT
);
2635 struct isl_set
*isl_set_empty_like(struct isl_set
*model
)
2639 return isl_set_empty(isl_dim_copy(model
->dim
));
2642 struct isl_map
*isl_map_universe(struct isl_dim
*dim
)
2644 struct isl_map
*map
;
2647 map
= isl_map_alloc_dim(isl_dim_copy(dim
), 1, ISL_MAP_DISJOINT
);
2648 map
= isl_map_add(map
, isl_basic_map_universe(dim
));
2652 struct isl_set
*isl_set_universe(struct isl_dim
*dim
)
2654 struct isl_set
*set
;
2657 set
= isl_set_alloc_dim(isl_dim_copy(dim
), 1, ISL_MAP_DISJOINT
);
2658 set
= isl_set_add(set
, isl_basic_set_universe(dim
));
2662 struct isl_map
*isl_map_dup(struct isl_map
*map
)
2665 struct isl_map
*dup
;
2669 dup
= isl_map_alloc_dim(isl_dim_copy(map
->dim
), map
->n
, map
->flags
);
2670 for (i
= 0; i
< map
->n
; ++i
)
2671 dup
= isl_map_add(dup
, isl_basic_map_copy(map
->p
[i
]));
2675 struct isl_map
*isl_map_add(struct isl_map
*map
, struct isl_basic_map
*bmap
)
2679 isl_assert(map
->ctx
, isl_dim_equal(map
->dim
, bmap
->dim
), goto error
);
2680 isl_assert(map
->ctx
, map
->n
< map
->size
, goto error
);
2681 map
->p
[map
->n
] = bmap
;
2683 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
2689 isl_basic_map_free(bmap
);
2693 void isl_map_free(struct isl_map
*map
)
2703 isl_ctx_deref(map
->ctx
);
2704 for (i
= 0; i
< map
->n
; ++i
)
2705 isl_basic_map_free(map
->p
[i
]);
2706 isl_dim_free(map
->dim
);
2710 struct isl_map
*isl_map_extend(struct isl_map
*base
,
2711 unsigned nparam
, unsigned n_in
, unsigned n_out
)
2715 base
= isl_map_cow(base
);
2719 base
->dim
= isl_dim_extend(base
->dim
, nparam
, n_in
, n_out
);
2722 for (i
= 0; i
< base
->n
; ++i
) {
2723 base
->p
[i
] = isl_basic_map_extend_dim(base
->p
[i
],
2724 isl_dim_copy(base
->dim
), 0, 0, 0);
2734 struct isl_set
*isl_set_extend(struct isl_set
*base
,
2735 unsigned nparam
, unsigned dim
)
2737 return (struct isl_set
*)isl_map_extend((struct isl_map
*)base
,
2741 static struct isl_basic_map
*isl_basic_map_fix_pos(struct isl_basic_map
*bmap
,
2742 unsigned pos
, int value
)
2746 bmap
= isl_basic_map_cow(bmap
);
2747 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
2748 j
= isl_basic_map_alloc_equality(bmap
);
2751 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
2752 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
2753 isl_int_set_si(bmap
->eq
[j
][0], value
);
2754 bmap
= isl_basic_map_simplify(bmap
);
2755 return isl_basic_map_finalize(bmap
);
2757 isl_basic_map_free(bmap
);
2761 struct isl_basic_map
*isl_basic_map_fix_si(struct isl_basic_map
*bmap
,
2762 enum isl_dim_type type
, unsigned pos
, int value
)
2766 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
2767 return isl_basic_map_fix_pos(bmap
, isl_basic_map_offset(bmap
, type
) + pos
,
2770 isl_basic_map_free(bmap
);
2774 struct isl_basic_set
*isl_basic_set_fix_si(struct isl_basic_set
*bset
,
2775 enum isl_dim_type type
, unsigned pos
, int value
)
2777 return (struct isl_basic_set
*)
2778 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
2782 struct isl_basic_map
*isl_basic_map_fix_input_si(struct isl_basic_map
*bmap
,
2783 unsigned input
, int value
)
2785 return isl_basic_map_fix_si(bmap
, isl_dim_in
, input
, value
);
2788 struct isl_basic_set
*isl_basic_set_fix_dim_si(struct isl_basic_set
*bset
,
2789 unsigned dim
, int value
)
2791 return (struct isl_basic_set
*)
2792 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
2793 isl_dim_set
, dim
, value
);
2796 struct isl_map
*isl_map_fix_si(struct isl_map
*map
,
2797 enum isl_dim_type type
, unsigned pos
, int value
)
2801 map
= isl_map_cow(map
);
2805 isl_assert(ctx
, pos
< isl_map_dim(map
, type
), goto error
);
2806 for (i
= 0; i
< map
->n
; ++i
) {
2807 map
->p
[i
] = isl_basic_map_fix_si(map
->p
[i
], type
, pos
, value
);
2811 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
2818 struct isl_map
*isl_map_fix_input_si(struct isl_map
*map
,
2819 unsigned input
, int value
)
2821 return isl_map_fix_si(map
, isl_dim_in
, input
, value
);
2824 struct isl_set
*isl_set_fix_dim_si(struct isl_set
*set
, unsigned dim
, int value
)
2826 return (struct isl_set
*)
2827 isl_map_fix_si((struct isl_map
*)set
, isl_dim_set
, dim
, value
);
2830 struct isl_basic_set
*isl_basic_set_lower_bound_dim(struct isl_basic_set
*bset
,
2831 unsigned dim
, isl_int value
)
2836 bset
= isl_basic_set_cow(bset
);
2837 bset
= isl_basic_set_extend_constraints(bset
, 0, 1);
2838 j
= isl_basic_set_alloc_inequality(bset
);
2841 isl_seq_clr(bset
->ineq
[j
], 1 + isl_basic_set_total_dim(bset
));
2842 isl_int_set_si(bset
->ineq
[j
][1 + isl_basic_set_n_param(bset
) + dim
], 1);
2843 isl_int_neg(bset
->ineq
[j
][0], value
);
2844 bset
= isl_basic_set_simplify(bset
);
2845 return isl_basic_set_finalize(bset
);
2847 isl_basic_set_free(bset
);
2851 struct isl_set
*isl_set_lower_bound_dim(struct isl_set
*set
, unsigned dim
,
2856 set
= isl_set_cow(set
);
2860 isl_assert(set
->ctx
, dim
< isl_set_n_dim(set
), goto error
);
2861 for (i
= 0; i
< set
->n
; ++i
) {
2862 set
->p
[i
] = isl_basic_set_lower_bound_dim(set
->p
[i
], dim
, value
);
2872 struct isl_map
*isl_map_reverse(struct isl_map
*map
)
2877 map
= isl_map_cow(map
);
2881 map
->dim
= isl_dim_reverse(map
->dim
);
2884 for (i
= 0; i
< map
->n
; ++i
) {
2885 map
->p
[i
] = isl_basic_map_reverse(map
->p
[i
]);
2889 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
2896 struct isl_map
*isl_basic_map_lexmax(
2897 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
2898 struct isl_set
**empty
)
2900 return isl_pip_basic_map_lexmax(bmap
, dom
, empty
);
2903 struct isl_map
*isl_basic_map_lexmin(
2904 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
2905 struct isl_set
**empty
)
2907 return isl_pip_basic_map_lexmin(bmap
, dom
, empty
);
2910 struct isl_set
*isl_basic_set_lexmin(struct isl_basic_set
*bset
)
2912 struct isl_basic_map
*bmap
= NULL
;
2913 struct isl_basic_set
*dom
= NULL
;
2914 struct isl_map
*min
;
2915 struct isl_dim
*param_dim
;
2919 bmap
= isl_basic_map_from_basic_set(bset
, isl_dim_copy(bset
->dim
));
2922 param_dim
= isl_dim_domain(isl_dim_copy(bmap
->dim
));
2923 dom
= isl_basic_set_universe(param_dim
);
2926 min
= isl_basic_map_lexmin(bmap
, dom
, NULL
);
2927 return isl_map_range(min
);
2929 isl_basic_map_free(bmap
);
2933 struct isl_map
*isl_basic_map_compute_divs(struct isl_basic_map
*bmap
)
2940 off
= isl_dim_total(bmap
->dim
);
2941 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2942 if (isl_int_is_zero(bmap
->div
[i
][0]))
2943 return isl_pip_basic_map_compute_divs(bmap
);
2944 isl_assert(bmap
->ctx
, isl_int_is_zero(bmap
->div
[i
][1+1+off
+i
]),
2947 return isl_map_from_basic_map(bmap
);
2949 isl_basic_map_free(bmap
);
2953 struct isl_map
*isl_map_compute_divs(struct isl_map
*map
)
2956 struct isl_map
*res
;
2962 res
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[0]));
2963 for (i
= 1 ; i
< map
->n
; ++i
) {
2965 r2
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[i
]));
2966 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
2967 res
= isl_map_union_disjoint(res
, r2
);
2969 res
= isl_map_union(res
, r2
);
2976 struct isl_set
*isl_basic_set_compute_divs(struct isl_basic_set
*bset
)
2978 return (struct isl_set
*)
2979 isl_basic_map_compute_divs((struct isl_basic_map
*)bset
);
2982 struct isl_set
*isl_set_compute_divs(struct isl_set
*set
)
2984 return (struct isl_set
*)
2985 isl_map_compute_divs((struct isl_map
*)set
);
2988 struct isl_set
*isl_map_domain(struct isl_map
*map
)
2991 struct isl_set
*set
;
2996 map
= isl_map_cow(map
);
3000 set
= (struct isl_set
*)map
;
3001 set
->dim
= isl_dim_domain(set
->dim
);
3004 for (i
= 0; i
< map
->n
; ++i
) {
3005 set
->p
[i
] = isl_basic_map_domain(map
->p
[i
]);
3009 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
3010 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
3017 struct isl_map
*isl_map_union_disjoint(
3018 struct isl_map
*map1
, struct isl_map
*map2
)
3022 struct isl_map
*map
= NULL
;
3036 isl_assert(map1
->ctx
, isl_dim_equal(map1
->dim
, map2
->dim
), goto error
);
3038 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
3039 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
3040 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
3042 map
= isl_map_alloc_dim(isl_dim_copy(map1
->dim
),
3043 map1
->n
+ map2
->n
, flags
);
3046 for (i
= 0; i
< map1
->n
; ++i
) {
3047 map
= isl_map_add(map
,
3048 isl_basic_map_copy(map1
->p
[i
]));
3052 for (i
= 0; i
< map2
->n
; ++i
) {
3053 map
= isl_map_add(map
,
3054 isl_basic_map_copy(map2
->p
[i
]));
3068 struct isl_map
*isl_map_union(struct isl_map
*map1
, struct isl_map
*map2
)
3070 map1
= isl_map_union_disjoint(map1
, map2
);
3074 ISL_F_CLR(map1
, ISL_MAP_DISJOINT
);
3078 struct isl_set
*isl_set_union_disjoint(
3079 struct isl_set
*set1
, struct isl_set
*set2
)
3081 return (struct isl_set
*)
3082 isl_map_union_disjoint(
3083 (struct isl_map
*)set1
, (struct isl_map
*)set2
);
3086 struct isl_set
*isl_set_union(struct isl_set
*set1
, struct isl_set
*set2
)
3088 return (struct isl_set
*)
3089 isl_map_union((struct isl_map
*)set1
, (struct isl_map
*)set2
);
3092 struct isl_map
*isl_map_intersect_range(
3093 struct isl_map
*map
, struct isl_set
*set
)
3096 struct isl_map
*result
;
3102 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
) &&
3103 ISL_F_ISSET(set
, ISL_MAP_DISJOINT
))
3104 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
3106 result
= isl_map_alloc_dim(isl_dim_copy(map
->dim
),
3107 map
->n
* set
->n
, flags
);
3110 for (i
= 0; i
< map
->n
; ++i
)
3111 for (j
= 0; j
< set
->n
; ++j
) {
3112 result
= isl_map_add(result
,
3113 isl_basic_map_intersect_range(
3114 isl_basic_map_copy(map
->p
[i
]),
3115 isl_basic_set_copy(set
->p
[j
])));
3128 struct isl_map
*isl_map_intersect_domain(
3129 struct isl_map
*map
, struct isl_set
*set
)
3131 return isl_map_reverse(
3132 isl_map_intersect_range(isl_map_reverse(map
), set
));
3135 struct isl_map
*isl_map_apply_domain(
3136 struct isl_map
*map1
, struct isl_map
*map2
)
3140 map1
= isl_map_reverse(map1
);
3141 map1
= isl_map_apply_range(map1
, map2
);
3142 return isl_map_reverse(map1
);
3149 struct isl_map
*isl_map_apply_range(
3150 struct isl_map
*map1
, struct isl_map
*map2
)
3152 struct isl_dim
*dim_result
;
3153 struct isl_map
*result
;
3162 dim_result
= isl_dim_join(isl_dim_copy(map1
->dim
),
3163 isl_dim_copy(map2
->dim
));
3165 result
= isl_map_alloc_dim(dim_result
, map1
->n
* map2
->n
, 0);
3168 for (i
= 0; i
< map1
->n
; ++i
)
3169 for (j
= 0; j
< map2
->n
; ++j
) {
3170 result
= isl_map_add(result
,
3171 isl_basic_map_apply_range(
3172 isl_basic_map_copy(map1
->p
[i
]),
3173 isl_basic_map_copy(map2
->p
[j
])));
3179 if (result
&& result
->n
<= 1)
3180 ISL_F_SET(result
, ISL_MAP_DISJOINT
);
3189 * returns range - domain
3191 struct isl_basic_set
*isl_basic_map_deltas(struct isl_basic_map
*bmap
)
3193 struct isl_basic_set
*bset
;
3200 dim
= isl_basic_map_n_in(bmap
);
3201 nparam
= isl_basic_map_n_param(bmap
);
3202 isl_assert(bmap
->ctx
, dim
== isl_basic_map_n_out(bmap
), goto error
);
3203 bset
= isl_basic_set_from_basic_map(bmap
);
3204 bset
= isl_basic_set_cow(bset
);
3205 bset
= isl_basic_set_extend(bset
, nparam
, 3*dim
, 0, dim
, 0);
3206 bset
= isl_basic_set_swap_vars(bset
, 2*dim
);
3207 for (i
= 0; i
< dim
; ++i
) {
3208 int j
= isl_basic_map_alloc_equality(
3209 (struct isl_basic_map
*)bset
);
3212 isl_seq_clr(bset
->eq
[j
], 1 + isl_basic_set_total_dim(bset
));
3213 isl_int_set_si(bset
->eq
[j
][1+nparam
+i
], 1);
3214 isl_int_set_si(bset
->eq
[j
][1+nparam
+dim
+i
], 1);
3215 isl_int_set_si(bset
->eq
[j
][1+nparam
+2*dim
+i
], -1);
3217 return isl_basic_set_project_out(bset
, 2*dim
, 0);
3219 isl_basic_map_free(bmap
);
3224 * returns range - domain
3226 struct isl_set
*isl_map_deltas(struct isl_map
*map
)
3229 struct isl_set
*result
;
3234 isl_assert(map
->ctx
, isl_map_n_in(map
) == isl_map_n_out(map
), goto error
);
3235 result
= isl_set_alloc(map
->ctx
, isl_map_n_param(map
),
3236 isl_map_n_in(map
), map
->n
, map
->flags
);
3239 for (i
= 0; i
< map
->n
; ++i
)
3240 result
= isl_set_add(result
,
3241 isl_basic_map_deltas(isl_basic_map_copy(map
->p
[i
])));
3249 static struct isl_basic_map
*basic_map_identity(struct isl_dim
*dims
)
3251 struct isl_basic_map
*bmap
;
3259 nparam
= dims
->nparam
;
3261 bmap
= isl_basic_map_alloc_dim(dims
, 0, dim
, 0);
3265 for (i
= 0; i
< dim
; ++i
) {
3266 int j
= isl_basic_map_alloc_equality(bmap
);
3269 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
3270 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
3271 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], -1);
3273 return isl_basic_map_finalize(bmap
);
3275 isl_basic_map_free(bmap
);
3279 struct isl_basic_map
*isl_basic_map_identity(struct isl_dim
*set_dim
)
3281 struct isl_dim
*dim
= isl_dim_map(set_dim
);
3284 return basic_map_identity(dim
);
3287 struct isl_basic_map
*isl_basic_map_identity_like(struct isl_basic_map
*model
)
3289 if (!model
|| !model
->dim
)
3291 isl_assert(model
->ctx
,
3292 model
->dim
->n_in
== model
->dim
->n_out
, return NULL
);
3293 return basic_map_identity(isl_dim_copy(model
->dim
));
3296 static struct isl_map
*map_identity(struct isl_dim
*dim
)
3298 struct isl_map
*map
= isl_map_alloc_dim(dim
, 1, ISL_MAP_DISJOINT
);
3299 return isl_map_add(map
, basic_map_identity(isl_dim_copy(dim
)));
3302 struct isl_map
*isl_map_identity(struct isl_dim
*set_dim
)
3304 struct isl_dim
*dim
= isl_dim_map(set_dim
);
3307 return map_identity(dim
);
3310 struct isl_map
*isl_map_identity_like(struct isl_basic_map
*model
)
3312 if (!model
|| !model
->dim
)
3314 isl_assert(model
->ctx
,
3315 model
->dim
->n_in
== model
->dim
->n_out
, return NULL
);
3316 return map_identity(isl_dim_copy(model
->dim
));
3319 int isl_set_is_equal(struct isl_set
*set1
, struct isl_set
*set2
)
3321 return isl_map_is_equal((struct isl_map
*)set1
, (struct isl_map
*)set2
);
3324 int isl_set_is_subset(struct isl_set
*set1
, struct isl_set
*set2
)
3326 return isl_map_is_subset(
3327 (struct isl_map
*)set1
, (struct isl_map
*)set2
);
3330 int isl_basic_map_is_subset(
3331 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3334 struct isl_map
*map1
;
3335 struct isl_map
*map2
;
3337 if (!bmap1
|| !bmap2
)
3340 map1
= isl_map_from_basic_map(isl_basic_map_copy(bmap1
));
3341 map2
= isl_map_from_basic_map(isl_basic_map_copy(bmap2
));
3343 is_subset
= isl_map_is_subset(map1
, map2
);
3351 int isl_basic_map_is_equal(
3352 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3356 if (!bmap1
|| !bmap2
)
3358 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
3361 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
3365 int isl_basic_set_is_equal(
3366 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
3368 return isl_basic_map_is_equal(
3369 (struct isl_basic_map
*)bset1
, (struct isl_basic_map
*)bset2
);
3372 int isl_map_is_empty(struct isl_map
*map
)
3379 for (i
= 0; i
< map
->n
; ++i
) {
3380 is_empty
= isl_basic_map_is_empty(map
->p
[i
]);
3389 int isl_map_fast_is_empty(struct isl_map
*map
)
3394 int isl_set_is_empty(struct isl_set
*set
)
3396 return isl_map_is_empty((struct isl_map
*)set
);
3399 int isl_map_is_subset(struct isl_map
*map1
, struct isl_map
*map2
)
3403 struct isl_map
*diff
;
3408 if (isl_map_is_empty(map1
))
3411 if (isl_map_is_empty(map2
))
3414 diff
= isl_map_subtract(isl_map_copy(map1
), isl_map_copy(map2
));
3418 is_subset
= isl_map_is_empty(diff
);
3424 int isl_map_is_equal(struct isl_map
*map1
, struct isl_map
*map2
)
3430 is_subset
= isl_map_is_subset(map1
, map2
);
3433 is_subset
= isl_map_is_subset(map2
, map1
);
3437 int isl_basic_map_is_strict_subset(
3438 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3442 if (!bmap1
|| !bmap2
)
3444 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
3447 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
3448 if (is_subset
== -1)
3453 int isl_basic_map_is_universe(struct isl_basic_map
*bmap
)
3457 return bmap
->n_eq
== 0 && bmap
->n_ineq
== 0;
3460 int isl_basic_map_is_empty(struct isl_basic_map
*bmap
)
3462 struct isl_basic_set
*bset
= NULL
;
3463 struct isl_vec
*sample
= NULL
;
3470 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
3473 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
3474 struct isl_basic_map
*copy
= isl_basic_map_copy(bmap
);
3475 copy
= isl_basic_map_convex_hull(copy
);
3476 empty
= ISL_F_ISSET(copy
, ISL_BASIC_MAP_EMPTY
);
3477 isl_basic_map_free(copy
);
3481 total
= 1 + isl_basic_map_total_dim(bmap
);
3482 if (bmap
->sample
&& bmap
->sample
->size
== total
) {
3483 int contains
= basic_map_contains(bmap
, bmap
->sample
);
3489 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
3492 sample
= isl_basic_set_sample(bset
);
3495 empty
= sample
->size
== 0;
3497 isl_vec_free(bmap
->ctx
, bmap
->sample
);
3498 bmap
->sample
= sample
;
3503 int isl_basic_map_fast_is_empty(struct isl_basic_map
*bmap
)
3507 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
);
3510 int isl_basic_set_fast_is_empty(struct isl_basic_set
*bset
)
3514 return ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
);
3517 int isl_basic_set_is_empty(struct isl_basic_set
*bset
)
3519 return isl_basic_map_is_empty((struct isl_basic_map
*)bset
);
3522 struct isl_map
*isl_basic_map_union(
3523 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3525 struct isl_map
*map
;
3526 if (!bmap1
|| !bmap2
)
3529 isl_assert(map1
->ctx
, isl_dim_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
3531 map
= isl_map_alloc_dim(isl_dim_copy(bmap1
->dim
), 2, 0);
3534 map
= isl_map_add(map
, bmap1
);
3535 map
= isl_map_add(map
, bmap2
);
3538 isl_basic_map_free(bmap1
);
3539 isl_basic_map_free(bmap2
);
3543 struct isl_set
*isl_basic_set_union(
3544 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
3546 return (struct isl_set
*)isl_basic_map_union(
3547 (struct isl_basic_map
*)bset1
,
3548 (struct isl_basic_map
*)bset2
);
3551 /* Order divs such that any div only depends on previous divs */
3552 static struct isl_basic_map
*order_divs(struct isl_basic_map
*bmap
)
3555 unsigned off
= isl_dim_total(bmap
->dim
);
3557 for (i
= 0; i
< bmap
->n_div
; ++i
) {
3559 pos
= isl_seq_first_non_zero(bmap
->div
[i
]+1+1+off
+i
,
3563 swap_div(bmap
, i
, pos
);
3569 /* Look for a div in dst that corresponds to the div "div" in src.
3570 * The divs before "div" in src and dst are assumed to be the same.
3572 * Returns -1 if no corresponding div was found and the position
3573 * of the corresponding div in dst otherwise.
3575 static int find_div(struct isl_basic_map
*dst
,
3576 struct isl_basic_map
*src
, unsigned div
)
3580 unsigned total
= isl_dim_total(src
->dim
);
3582 isl_assert(dst
->ctx
, div
<= dst
->n_div
, return -1);
3583 for (i
= div
; i
< dst
->n_div
; ++i
)
3584 if (isl_seq_eq(dst
->div
[i
], src
->div
[div
], 1+1+total
+div
) &&
3585 isl_seq_first_non_zero(dst
->div
[i
]+1+1+total
+div
,
3586 dst
->n_div
- div
) == -1)
3591 struct isl_basic_map
*isl_basic_map_align_divs(
3592 struct isl_basic_map
*dst
, struct isl_basic_map
*src
)
3595 unsigned total
= isl_dim_total(src
->dim
);
3600 if (src
->n_div
== 0)
3603 for (i
= 0; i
< src
->n_div
; ++i
)
3604 isl_assert(src
->ctx
, !isl_int_is_zero(src
->div
[i
][0]), goto error
);
3606 src
= order_divs(src
);
3607 dst
= isl_basic_map_cow(dst
);
3608 dst
= isl_basic_map_extend_dim(dst
, isl_dim_copy(dst
->dim
),
3609 src
->n_div
, 0, 2 * src
->n_div
);
3612 for (i
= 0; i
< src
->n_div
; ++i
) {
3613 int j
= find_div(dst
, src
, i
);
3615 j
= isl_basic_map_alloc_div(dst
);
3618 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
+i
);
3619 isl_seq_clr(dst
->div
[j
]+1+1+total
+i
, dst
->n_div
- i
);
3620 if (add_div_constraints(dst
, j
) < 0)
3624 swap_div(dst
, i
, j
);
3628 isl_basic_map_free(dst
);
3632 struct isl_basic_set
*isl_basic_set_align_divs(
3633 struct isl_basic_set
*dst
, struct isl_basic_set
*src
)
3635 return (struct isl_basic_set
*)isl_basic_map_align_divs(
3636 (struct isl_basic_map
*)dst
, (struct isl_basic_map
*)src
);
3639 struct isl_map
*isl_map_align_divs(struct isl_map
*map
)
3643 map
= isl_map_compute_divs(map
);
3644 map
= isl_map_cow(map
);
3648 for (i
= 1; i
< map
->n
; ++i
)
3649 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], map
->p
[i
]);
3650 for (i
= 1; i
< map
->n
; ++i
)
3651 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], map
->p
[0]);
3653 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3657 struct isl_set
*isl_set_align_divs(struct isl_set
*set
)
3659 return (struct isl_set
*)isl_map_align_divs((struct isl_map
*)set
);
3662 static struct isl_map
*add_cut_constraint(struct isl_map
*dst
,
3663 struct isl_basic_map
*src
, isl_int
*c
,
3664 unsigned len
, int oppose
)
3666 struct isl_basic_map
*copy
= NULL
;
3671 copy
= isl_basic_map_copy(src
);
3672 copy
= isl_basic_map_cow(copy
);
3675 copy
= isl_basic_map_extend_constraints(copy
, 0, 1);
3676 k
= isl_basic_map_alloc_inequality(copy
);
3680 isl_seq_neg(copy
->ineq
[k
], c
, len
);
3682 isl_seq_cpy(copy
->ineq
[k
], c
, len
);
3683 total
= 1 + isl_basic_map_total_dim(copy
);
3684 isl_seq_clr(copy
->ineq
[k
]+len
, total
- len
);
3685 isl_inequality_negate(copy
, k
);
3686 copy
= isl_basic_map_simplify(copy
);
3687 copy
= isl_basic_map_finalize(copy
);
3688 is_empty
= isl_basic_map_is_empty(copy
);
3692 dst
= isl_map_add(dst
, copy
);
3694 isl_basic_map_free(copy
);
3697 isl_basic_map_free(copy
);
3702 static struct isl_map
*subtract(struct isl_map
*map
, struct isl_basic_map
*bmap
)
3706 struct isl_map
*rest
= NULL
;
3708 unsigned total
= isl_basic_map_total_dim(bmap
);
3715 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
3716 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
3718 max
= map
->n
* (2 * bmap
->n_eq
+ bmap
->n_ineq
);
3719 rest
= isl_map_alloc_dim(isl_dim_copy(map
->dim
), max
, flags
);
3723 for (i
= 0; i
< map
->n
; ++i
) {
3724 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], bmap
);
3729 for (j
= 0; j
< map
->n
; ++j
)
3730 map
->p
[j
] = isl_basic_map_cow(map
->p
[j
]);
3732 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
3733 for (j
= 0; j
< map
->n
; ++j
) {
3734 rest
= add_cut_constraint(rest
,
3735 map
->p
[j
], bmap
->eq
[i
], 1+total
, 0);
3739 rest
= add_cut_constraint(rest
,
3740 map
->p
[j
], bmap
->eq
[i
], 1+total
, 1);
3744 map
->p
[j
] = isl_basic_map_extend_constraints(map
->p
[j
],
3748 k
= isl_basic_map_alloc_equality(map
->p
[j
]);
3751 isl_seq_cpy(map
->p
[j
]->eq
[k
], bmap
->eq
[i
], 1+total
);
3752 isl_seq_clr(map
->p
[j
]->eq
[k
]+1+total
,
3753 map
->p
[j
]->n_div
- bmap
->n_div
);
3757 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
3758 for (j
= 0; j
< map
->n
; ++j
) {
3759 rest
= add_cut_constraint(rest
,
3760 map
->p
[j
], bmap
->ineq
[i
], 1+total
, 0);
3764 map
->p
[j
] = isl_basic_map_extend_constraints(map
->p
[j
],
3768 k
= isl_basic_map_alloc_inequality(map
->p
[j
]);
3771 isl_seq_cpy(map
->p
[j
]->ineq
[k
], bmap
->ineq
[i
], 1+total
);
3772 isl_seq_clr(map
->p
[j
]->ineq
[k
]+1+total
,
3773 map
->p
[j
]->n_div
- bmap
->n_div
);
3785 struct isl_map
*isl_map_subtract(struct isl_map
*map1
, struct isl_map
*map2
)
3791 isl_assert(map1
->ctx
, isl_dim_equal(map1
->dim
, map2
->dim
), goto error
);
3793 if (isl_map_is_empty(map2
)) {
3798 map1
= isl_map_compute_divs(map1
);
3799 map2
= isl_map_compute_divs(map2
);
3803 for (i
= 0; map1
&& i
< map2
->n
; ++i
)
3804 map1
= subtract(map1
, map2
->p
[i
]);
3814 struct isl_set
*isl_set_subtract(struct isl_set
*set1
, struct isl_set
*set2
)
3816 return (struct isl_set
*)
3818 (struct isl_map
*)set1
, (struct isl_map
*)set2
);
3821 struct isl_set
*isl_set_apply(struct isl_set
*set
, struct isl_map
*map
)
3825 isl_assert(set
->ctx
, isl_map_compatible_domain(map
, set
), goto error
);
3826 map
= isl_map_intersect_domain(map
, set
);
3827 set
= isl_map_range(map
);
3835 /* There is no need to cow as removing empty parts doesn't change
3836 * the meaning of the set.
3838 struct isl_map
*isl_map_remove_empty_parts(struct isl_map
*map
)
3845 for (i
= map
->n
-1; i
>= 0; --i
) {
3846 if (!ISL_F_ISSET(map
->p
[i
], ISL_BASIC_MAP_EMPTY
))
3848 isl_basic_map_free(map
->p
[i
]);
3849 if (i
!= map
->n
-1) {
3850 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3851 map
->p
[i
] = map
->p
[map
->n
-1];
3859 struct isl_set
*isl_set_remove_empty_parts(struct isl_set
*set
)
3861 return (struct isl_set
*)
3862 isl_map_remove_empty_parts((struct isl_map
*)set
);
3865 struct isl_basic_map
*isl_map_copy_basic_map(struct isl_map
*map
)
3867 struct isl_basic_map
*bmap
;
3868 if (!map
|| map
->n
== 0)
3870 bmap
= map
->p
[map
->n
-1];
3871 isl_assert(map
->ctx
, ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
), return NULL
);
3872 return isl_basic_map_copy(bmap
);
3875 struct isl_basic_set
*isl_set_copy_basic_set(struct isl_set
*set
)
3877 (struct isl_basic_set
*)isl_map_copy_basic_map((struct isl_map
*)set
);
3880 struct isl_map
*isl_map_drop_basic_map(struct isl_map
*map
,
3881 struct isl_basic_map
*bmap
)
3887 for (i
= map
->n
-1; i
>= 0; --i
) {
3888 if (map
->p
[i
] != bmap
)
3890 map
= isl_map_cow(map
);
3893 isl_basic_map_free(map
->p
[i
]);
3894 if (i
!= map
->n
-1) {
3895 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
3896 map
->p
[i
] = map
->p
[map
->n
-1];
3901 isl_basic_map_free(bmap
);
3905 isl_basic_map_free(bmap
);
3909 struct isl_set
*isl_set_drop_basic_set(struct isl_set
*set
,
3910 struct isl_basic_set
*bset
)
3912 (struct isl_set
*)isl_map_drop_basic_map((struct isl_map
*)set
,
3913 (struct isl_basic_map
*)bset
);
3916 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
3917 * for any common value of the parameters and dimensions preceding dim
3918 * in both basic sets, the values of dimension pos in bset1 are
3919 * smaller or larger than those in bset2.
3922 * 1 if bset1 follows bset2
3923 * -1 if bset1 precedes bset2
3924 * 0 if bset1 and bset2 are incomparable
3925 * -2 if some error occurred.
3927 int isl_basic_set_compare_at(struct isl_basic_set
*bset1
,
3928 struct isl_basic_set
*bset2
, int pos
)
3930 struct isl_dim
*dims
;
3931 struct isl_basic_map
*bmap1
= NULL
;
3932 struct isl_basic_map
*bmap2
= NULL
;
3933 struct isl_ctx
*ctx
;
3934 struct isl_vec
*obj
;
3937 unsigned dim1
, dim2
;
3939 enum isl_lp_result res
;
3942 if (!bset1
|| !bset2
)
3945 nparam
= isl_basic_set_n_param(bset1
);
3946 dim1
= isl_basic_set_n_dim(bset1
);
3947 dim2
= isl_basic_set_n_dim(bset2
);
3948 dims
= isl_dim_alloc(bset1
->ctx
, nparam
, pos
, dim1
- pos
);
3949 bmap1
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset1
), dims
);
3950 dims
= isl_dim_alloc(bset2
->ctx
, nparam
, pos
, dim2
- pos
);
3951 bmap2
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset2
), dims
);
3952 if (!bmap1
|| !bmap2
)
3954 bmap1
= isl_basic_map_cow(bmap1
);
3955 bmap1
= isl_basic_map_extend(bmap1
, nparam
,
3956 pos
, (dim1
- pos
) + (dim2
- pos
),
3957 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
3958 bmap1
= add_constraints(bmap1
, bmap2
, 0, dim1
- pos
);
3961 total
= isl_basic_map_total_dim(bmap1
);
3963 obj
= isl_vec_alloc(ctx
, 1 + total
);
3964 isl_seq_clr(obj
->block
.data
, 1 + total
);
3965 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
], 1);
3966 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
+(dim1
-pos
)], -1);
3971 res
= isl_solve_lp(bmap1
, 0, obj
->block
.data
, ctx
->one
, &num
, &den
);
3972 if (res
== isl_lp_empty
)
3974 else if (res
== isl_lp_ok
&& isl_int_is_pos(num
))
3976 else if ((res
== isl_lp_ok
&& isl_int_is_neg(num
)) ||
3977 res
== isl_lp_unbounded
)
3983 isl_basic_map_free(bmap1
);
3984 isl_vec_free(ctx
, obj
);
3987 isl_basic_map_free(bmap1
);
3988 isl_basic_map_free(bmap2
);
3992 static int isl_basic_map_fast_has_fixed_var(struct isl_basic_map
*bmap
,
3993 unsigned pos
, isl_int
*val
)
4001 total
= isl_basic_map_total_dim(bmap
);
4002 for (i
= 0, d
= total
-1; i
< bmap
->n_eq
&& d
+1 > pos
; ++i
) {
4003 for (; d
+1 > pos
; --d
)
4004 if (!isl_int_is_zero(bmap
->eq
[i
][1+d
]))
4008 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1, d
) != -1)
4010 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1+d
+1, total
-d
-1) != -1)
4012 if (!isl_int_is_one(bmap
->eq
[i
][1+d
]))
4015 isl_int_neg(*val
, bmap
->eq
[i
][0]);
4021 static int isl_map_fast_has_fixed_var(struct isl_map
*map
,
4022 unsigned pos
, isl_int
*val
)
4034 return isl_basic_map_fast_has_fixed_var(map
->p
[0], pos
, val
);
4037 fixed
= isl_basic_map_fast_has_fixed_var(map
->p
[0], pos
, &v
);
4038 for (i
= 1; fixed
== 1 && i
< map
->n
; ++i
) {
4039 fixed
= isl_basic_map_fast_has_fixed_var(map
->p
[i
], pos
, &tmp
);
4040 if (fixed
== 1 && isl_int_ne(tmp
, v
))
4044 isl_int_set(*val
, v
);
4050 static int isl_set_fast_has_fixed_var(struct isl_set
*set
, unsigned pos
,
4053 return isl_map_fast_has_fixed_var((struct isl_map
*)set
, pos
, val
);
4056 int isl_basic_map_fast_is_fixed(struct isl_basic_map
*bmap
,
4057 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
4059 if (pos
>= isl_basic_map_dim(bmap
, type
))
4061 return isl_basic_map_fast_has_fixed_var(bmap
,
4062 isl_basic_map_offset(bmap
, type
) - 1 + pos
, val
);
4065 /* Check if dimension dim has fixed value and if so and if val is not NULL,
4066 * then return this fixed value in *val.
4068 int isl_set_fast_dim_is_fixed(struct isl_set
*set
, unsigned dim
, isl_int
*val
)
4070 return isl_set_fast_has_fixed_var(set
, isl_set_n_param(set
) + dim
, val
);
4073 /* Check if input variable in has fixed value and if so and if val is not NULL,
4074 * then return this fixed value in *val.
4076 int isl_map_fast_input_is_fixed(struct isl_map
*map
, unsigned in
, isl_int
*val
)
4078 return isl_map_fast_has_fixed_var(map
, isl_map_n_param(map
) + in
, val
);
4081 /* Check if dimension dim has an (obvious) fixed lower bound and if so
4082 * and if val is not NULL, then return this lower bound in *val.
4084 int isl_basic_set_fast_dim_has_fixed_lower_bound(struct isl_basic_set
*bset
,
4085 unsigned dim
, isl_int
*val
)
4087 int i
, i_eq
= -1, i_ineq
= -1;
4094 total
= isl_basic_set_total_dim(bset
);
4095 nparam
= isl_basic_set_n_param(bset
);
4096 for (i
= 0; i
< bset
->n_eq
; ++i
) {
4097 if (isl_int_is_zero(bset
->eq
[i
][1+nparam
+dim
]))
4103 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
4104 if (!isl_int_is_pos(bset
->ineq
[i
][1+nparam
+dim
]))
4106 if (i_eq
!= -1 || i_ineq
!= -1)
4110 if (i_eq
== -1 && i_ineq
== -1)
4112 c
= i_eq
!= -1 ? bset
->eq
[i_eq
] : bset
->ineq
[i_ineq
];
4113 /* The coefficient should always be one due to normalization. */
4114 if (!isl_int_is_one(c
[1+nparam
+dim
]))
4116 if (isl_seq_first_non_zero(c
+1, nparam
+dim
) != -1)
4118 if (isl_seq_first_non_zero(c
+1+nparam
+dim
+1,
4119 total
- nparam
- dim
- 1) != -1)
4122 isl_int_neg(*val
, c
[0]);
4126 int isl_set_fast_dim_has_fixed_lower_bound(struct isl_set
*set
,
4127 unsigned dim
, isl_int
*val
)
4139 return isl_basic_set_fast_dim_has_fixed_lower_bound(set
->p
[0],
4143 fixed
= isl_basic_set_fast_dim_has_fixed_lower_bound(set
->p
[0],
4145 for (i
= 1; fixed
== 1 && i
< set
->n
; ++i
) {
4146 fixed
= isl_basic_set_fast_dim_has_fixed_lower_bound(set
->p
[i
],
4148 if (fixed
== 1 && isl_int_ne(tmp
, v
))
4152 isl_int_set(*val
, v
);
4163 static int qsort_constraint_cmp(const void *p1
, const void *p2
)
4165 const struct constraint
*c1
= (const struct constraint
*)p1
;
4166 const struct constraint
*c2
= (const struct constraint
*)p2
;
4167 unsigned size
= isl_min(c1
->size
, c2
->size
);
4168 return isl_seq_cmp(c1
->c
, c2
->c
, size
);
4171 static struct isl_basic_map
*isl_basic_map_sort_constraints(
4172 struct isl_basic_map
*bmap
)
4175 struct constraint
*c
;
4180 total
= isl_basic_map_total_dim(bmap
);
4181 c
= isl_alloc_array(bmap
->ctx
, struct constraint
, bmap
->n_ineq
);
4184 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
4186 c
[i
].c
= bmap
->ineq
[i
];
4188 qsort(c
, bmap
->n_ineq
, sizeof(struct constraint
), qsort_constraint_cmp
);
4189 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
4190 bmap
->ineq
[i
] = c
[i
].c
;
4194 isl_basic_map_free(bmap
);
4198 struct isl_basic_map
*isl_basic_map_normalize(struct isl_basic_map
*bmap
)
4202 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED
))
4204 bmap
= isl_basic_map_convex_hull(bmap
);
4205 bmap
= isl_basic_map_sort_constraints(bmap
);
4206 ISL_F_SET(bmap
, ISL_BASIC_MAP_NORMALIZED
);
4210 struct isl_basic_set
*isl_basic_set_normalize(struct isl_basic_set
*bset
)
4212 return (struct isl_basic_set
*)isl_basic_map_normalize(
4213 (struct isl_basic_map
*)bset
);
4216 static int isl_basic_map_fast_cmp(const struct isl_basic_map
*bmap1
,
4217 const struct isl_basic_map
*bmap2
)
4224 if (isl_basic_map_n_param(bmap1
) != isl_basic_map_n_param(bmap2
))
4225 return isl_basic_map_n_param(bmap1
) - isl_basic_map_n_param(bmap2
);
4226 if (isl_basic_map_n_in(bmap1
) != isl_basic_map_n_in(bmap2
))
4227 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
4228 if (isl_basic_map_n_out(bmap1
) != isl_basic_map_n_out(bmap2
))
4229 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
4230 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
) &&
4231 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
4233 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
))
4235 if (ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
4237 if (bmap1
->n_eq
!= bmap2
->n_eq
)
4238 return bmap1
->n_eq
- bmap2
->n_eq
;
4239 if (bmap1
->n_ineq
!= bmap2
->n_ineq
)
4240 return bmap1
->n_ineq
- bmap2
->n_ineq
;
4241 if (bmap1
->n_div
!= bmap2
->n_div
)
4242 return bmap1
->n_div
- bmap2
->n_div
;
4243 total
= isl_basic_map_total_dim(bmap1
);
4244 for (i
= 0; i
< bmap1
->n_eq
; ++i
) {
4245 cmp
= isl_seq_cmp(bmap1
->eq
[i
], bmap2
->eq
[i
], 1+total
);
4249 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
4250 cmp
= isl_seq_cmp(bmap1
->ineq
[i
], bmap2
->ineq
[i
], 1+total
);
4254 for (i
= 0; i
< bmap1
->n_div
; ++i
) {
4255 cmp
= isl_seq_cmp(bmap1
->div
[i
], bmap2
->div
[i
], 1+1+total
);
4262 static int isl_basic_map_fast_is_equal(struct isl_basic_map
*bmap1
,
4263 struct isl_basic_map
*bmap2
)
4265 return isl_basic_map_fast_cmp(bmap1
, bmap2
) == 0;
4268 static int qsort_bmap_cmp(const void *p1
, const void *p2
)
4270 const struct isl_basic_map
*bmap1
= *(const struct isl_basic_map
**)p1
;
4271 const struct isl_basic_map
*bmap2
= *(const struct isl_basic_map
**)p2
;
4273 return isl_basic_map_fast_cmp(bmap1
, bmap2
);
4276 /* We normalize in place, but if anything goes wrong we need
4277 * to return NULL, so we need to make sure we don't change the
4278 * meaning of any possible other copies of map.
4280 struct isl_map
*isl_map_normalize(struct isl_map
*map
)
4283 struct isl_basic_map
*bmap
;
4287 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
))
4289 for (i
= 0; i
< map
->n
; ++i
) {
4290 bmap
= isl_basic_map_normalize(isl_basic_map_copy(map
->p
[i
]));
4293 isl_basic_map_free(map
->p
[i
]);
4296 qsort(map
->p
, map
->n
, sizeof(struct isl_basic_map
*), qsort_bmap_cmp
);
4297 ISL_F_SET(map
, ISL_MAP_NORMALIZED
);
4298 map
= isl_map_remove_empty_parts(map
);
4301 for (i
= map
->n
- 1; i
>= 1; --i
) {
4302 if (!isl_basic_map_fast_is_equal(map
->p
[i
-1], map
->p
[i
]))
4304 isl_basic_map_free(map
->p
[i
-1]);
4305 for (j
= i
; j
< map
->n
; ++j
)
4306 map
->p
[j
-1] = map
->p
[j
];
4316 struct isl_set
*isl_set_normalize(struct isl_set
*set
)
4318 return (struct isl_set
*)isl_map_normalize((struct isl_map
*)set
);
4321 int isl_map_fast_is_equal(struct isl_map
*map1
, struct isl_map
*map2
)
4331 if (!isl_dim_equal(map1
->dim
, map2
->dim
))
4334 map1
= isl_map_copy(map1
);
4335 map2
= isl_map_copy(map2
);
4336 map1
= isl_map_normalize(map1
);
4337 map2
= isl_map_normalize(map2
);
4340 equal
= map1
->n
== map2
->n
;
4341 for (i
= 0; equal
&& i
< map1
->n
; ++i
) {
4342 equal
= isl_basic_map_fast_is_equal(map1
->p
[i
], map2
->p
[i
]);
4355 int isl_set_fast_is_equal(struct isl_set
*set1
, struct isl_set
*set2
)
4357 return isl_map_fast_is_equal((struct isl_map
*)set1
,
4358 (struct isl_map
*)set2
);
4361 /* Return an interval that ranges from min to max (inclusive)
4363 struct isl_basic_set
*isl_basic_set_interval(struct isl_ctx
*ctx
,
4364 isl_int min
, isl_int max
)
4367 struct isl_basic_set
*bset
= NULL
;
4369 bset
= isl_basic_set_alloc(ctx
, 0, 1, 0, 0, 2);
4373 k
= isl_basic_set_alloc_inequality(bset
);
4376 isl_int_set_si(bset
->ineq
[k
][1], 1);
4377 isl_int_neg(bset
->ineq
[k
][0], min
);
4379 k
= isl_basic_set_alloc_inequality(bset
);
4382 isl_int_set_si(bset
->ineq
[k
][1], -1);
4383 isl_int_set(bset
->ineq
[k
][0], max
);
4387 isl_basic_set_free(bset
);
4391 /* Return the Cartesian product of the basic sets in list (in the given order).
4393 struct isl_basic_set
*isl_basic_set_product(struct isl_basic_set_list
*list
)
4401 struct isl_basic_set
*product
= NULL
;
4405 isl_assert(list
->ctx
, list
->n
> 0, goto error
);
4406 isl_assert(list
->ctx
, list
->p
[0], goto error
);
4407 nparam
= isl_basic_set_n_param(list
->p
[0]);
4408 dim
= isl_basic_set_n_dim(list
->p
[0]);
4409 extra
= list
->p
[0]->n_div
;
4410 n_eq
= list
->p
[0]->n_eq
;
4411 n_ineq
= list
->p
[0]->n_ineq
;
4412 for (i
= 1; i
< list
->n
; ++i
) {
4413 isl_assert(list
->ctx
, list
->p
[i
], goto error
);
4414 isl_assert(list
->ctx
,
4415 nparam
== isl_basic_set_n_param(list
->p
[i
]), goto error
);
4416 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
4417 extra
+= list
->p
[i
]->n_div
;
4418 n_eq
+= list
->p
[i
]->n_eq
;
4419 n_ineq
+= list
->p
[i
]->n_ineq
;
4421 product
= isl_basic_set_alloc(list
->ctx
, nparam
, dim
, extra
,
4426 for (i
= 0; i
< list
->n
; ++i
) {
4427 isl_basic_set_add_constraints(product
,
4428 isl_basic_set_copy(list
->p
[i
]), dim
);
4429 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
4431 isl_basic_set_list_free(list
);
4434 isl_basic_set_free(product
);
4435 isl_basic_set_list_free(list
);
4439 struct isl_basic_map
*isl_basic_map_product(
4440 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
4442 struct isl_dim
*dim_result
= NULL
;
4443 struct isl_basic_map
*bmap
;
4444 unsigned in1
, in2
, out1
, out2
, nparam
, total
, pos
;
4445 struct isl_dim_map
*dim_map1
, *dim_map2
;
4447 if (!bmap1
|| !bmap2
)
4450 isl_assert(map1
->ctx
, isl_dim_match(bmap1
->dim
, isl_dim_param
,
4451 bmap2
->dim
, isl_dim_param
), goto error
);
4452 dim_result
= isl_dim_product(isl_dim_copy(bmap1
->dim
),
4453 isl_dim_copy(bmap2
->dim
));
4455 in1
= isl_basic_map_n_in(bmap1
);
4456 in2
= isl_basic_map_n_in(bmap2
);
4457 out1
= isl_basic_map_n_out(bmap1
);
4458 out2
= isl_basic_map_n_out(bmap2
);
4459 nparam
= isl_basic_map_n_param(bmap1
);
4461 total
= nparam
+ in1
+ in2
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
4462 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4463 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4464 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
4465 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
4466 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
4467 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
4468 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
4469 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
4470 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
4471 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
4473 bmap
= isl_basic_map_alloc_dim(dim_result
,
4474 bmap1
->n_div
+ bmap2
->n_div
,
4475 bmap1
->n_eq
+ bmap2
->n_eq
,
4476 bmap1
->n_ineq
+ bmap2
->n_ineq
);
4477 bmap
= add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
4478 bmap
= add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
4479 bmap
= isl_basic_map_simplify(bmap
);
4480 return isl_basic_map_finalize(bmap
);
4482 isl_basic_map_free(bmap1
);
4483 isl_basic_map_free(bmap2
);
4487 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
4489 struct isl_map
*isl_map_product(struct isl_map
*map1
, struct isl_map
*map2
)
4492 struct isl_map
*result
;
4498 isl_assert(map1
->ctx
, isl_dim_match(map1
->dim
, isl_dim_param
,
4499 map2
->dim
, isl_dim_param
), goto error
);
4501 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
4502 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
4503 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
4505 result
= isl_map_alloc_dim(isl_dim_product(isl_dim_copy(map1
->dim
),
4506 isl_dim_copy(map2
->dim
)),
4507 map1
->n
* map2
->n
, flags
);
4510 for (i
= 0; i
< map1
->n
; ++i
)
4511 for (j
= 0; j
< map2
->n
; ++j
) {
4512 struct isl_basic_map
*part
;
4513 part
= isl_basic_map_product(
4514 isl_basic_map_copy(map1
->p
[i
]),
4515 isl_basic_map_copy(map2
->p
[j
]));
4516 if (isl_basic_map_is_empty(part
))
4517 isl_basic_map_free(part
);
4519 result
= isl_map_add(result
, part
);
4532 uint32_t isl_basic_set_get_hash(struct isl_basic_set
*bset
)
4540 bset
= isl_basic_set_copy(bset
);
4541 bset
= isl_basic_set_normalize(bset
);
4544 total
= isl_basic_set_total_dim(bset
);
4545 isl_hash_byte(hash
, bset
->n_eq
& 0xFF);
4546 for (i
= 0; i
< bset
->n_eq
; ++i
) {
4548 c_hash
= isl_seq_get_hash(bset
->eq
[i
], 1 + total
);
4549 isl_hash_hash(hash
, c_hash
);
4551 isl_hash_byte(hash
, bset
->n_ineq
& 0xFF);
4552 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
4554 c_hash
= isl_seq_get_hash(bset
->ineq
[i
], 1 + total
);
4555 isl_hash_hash(hash
, c_hash
);
4557 isl_hash_byte(hash
, bset
->n_div
& 0xFF);
4558 for (i
= 0; i
< bset
->n_div
; ++i
) {
4560 if (isl_int_is_zero(bset
->div
[i
][0]))
4562 isl_hash_byte(hash
, i
& 0xFF);
4563 c_hash
= isl_seq_get_hash(bset
->div
[i
], 1 + 1 + total
);
4564 isl_hash_hash(hash
, c_hash
);
4566 isl_basic_set_free(bset
);
4570 uint32_t isl_set_get_hash(struct isl_set
*set
)
4577 set
= isl_set_copy(set
);
4578 set
= isl_set_normalize(set
);
4582 hash
= isl_hash_init();
4583 for (i
= 0; i
< set
->n
; ++i
) {
4585 bset_hash
= isl_basic_set_get_hash(set
->p
[i
]);
4586 isl_hash_hash(hash
, bset_hash
);
4594 /* Check if the value for dimension dim is completely determined
4595 * by the values of the other parameters and variables.
4596 * That is, check if dimension dim is involved in an equality.
4598 int isl_basic_set_dim_is_unique(struct isl_basic_set
*bset
, unsigned dim
)
4605 nparam
= isl_basic_set_n_param(bset
);
4606 for (i
= 0; i
< bset
->n_eq
; ++i
)
4607 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ dim
]))
4612 /* Check if the value for dimension dim is completely determined
4613 * by the values of the other parameters and variables.
4614 * That is, check if dimension dim is involved in an equality
4615 * for each of the subsets.
4617 int isl_set_dim_is_unique(struct isl_set
*set
, unsigned dim
)
4623 for (i
= 0; i
< set
->n
; ++i
) {
4625 unique
= isl_basic_set_dim_is_unique(set
->p
[i
], dim
);