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_map_drop_equality(struct isl_basic_map
*bmap
, unsigned pos
)
511 isl_assert(bmap
->ctx
, pos
< bmap
->n_eq
, return -1);
513 if (pos
!= bmap
->n_eq
- 1) {
515 bmap
->eq
[pos
] = bmap
->eq
[bmap
->n_eq
- 1];
516 bmap
->eq
[bmap
->n_eq
- 1] = t
;
522 int isl_basic_set_drop_equality(struct isl_basic_set
*bset
, unsigned pos
)
524 return isl_basic_map_drop_equality((struct isl_basic_map
*)bset
, pos
);
527 void isl_basic_map_inequality_to_equality(
528 struct isl_basic_map
*bmap
, unsigned pos
)
533 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
534 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
539 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
540 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
541 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
542 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
545 static int room_for_ineq(struct isl_basic_map
*bmap
, unsigned n
)
547 return bmap
->n_ineq
+ n
<= bmap
->eq
- bmap
->ineq
;
550 int isl_basic_map_alloc_inequality(struct isl_basic_map
*bmap
)
556 isl_assert(ctx
, room_for_ineq(bmap
, 1), return -1);
557 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
558 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
559 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
560 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
561 isl_seq_clr(bmap
->ineq
[bmap
->n_ineq
] +
562 1 + isl_basic_map_total_dim(bmap
),
563 bmap
->extra
- bmap
->n_div
);
564 return bmap
->n_ineq
++;
567 int isl_basic_set_alloc_inequality(struct isl_basic_set
*bset
)
569 return isl_basic_map_alloc_inequality((struct isl_basic_map
*)bset
);
572 int isl_basic_map_free_inequality(struct isl_basic_map
*bmap
, unsigned n
)
576 isl_assert(bmap
->ctx
, n
<= bmap
->n_ineq
, return -1);
581 int isl_basic_set_free_inequality(struct isl_basic_set
*bset
, unsigned n
)
583 return isl_basic_map_free_inequality((struct isl_basic_map
*)bset
, n
);
586 int isl_basic_map_drop_inequality(struct isl_basic_map
*bmap
, unsigned pos
)
591 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
593 if (pos
!= bmap
->n_ineq
- 1) {
595 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
596 bmap
->ineq
[bmap
->n_ineq
- 1] = t
;
597 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
603 int isl_basic_set_drop_inequality(struct isl_basic_set
*bset
, unsigned pos
)
605 return isl_basic_map_drop_inequality((struct isl_basic_map
*)bset
, pos
);
608 int isl_basic_map_alloc_div(struct isl_basic_map
*bmap
)
612 isl_assert(bmap
->ctx
, bmap
->n_div
< bmap
->extra
, return -1);
613 isl_seq_clr(bmap
->div
[bmap
->n_div
] +
614 1 + 1 + isl_basic_map_total_dim(bmap
),
615 bmap
->extra
- bmap
->n_div
);
616 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
617 return bmap
->n_div
++;
620 int isl_basic_set_alloc_div(struct isl_basic_set
*bset
)
622 return isl_basic_map_alloc_div((struct isl_basic_map
*)bset
);
625 int isl_basic_map_free_div(struct isl_basic_map
*bmap
, unsigned n
)
629 isl_assert(bmap
->ctx
, n
<= bmap
->n_div
, return -1);
634 /* Copy constraint from src to dst, putting the vars of src at offset
635 * dim_off in dst and the divs of src at offset div_off in dst.
636 * If both sets are actually map, then dim_off applies to the input
639 static void copy_constraint(struct isl_basic_map
*dst_map
, isl_int
*dst
,
640 struct isl_basic_map
*src_map
, isl_int
*src
,
641 unsigned in_off
, unsigned out_off
, unsigned div_off
)
643 unsigned src_nparam
= isl_basic_map_n_param(src_map
);
644 unsigned dst_nparam
= isl_basic_map_n_param(dst_map
);
645 unsigned src_in
= isl_basic_map_n_in(src_map
);
646 unsigned dst_in
= isl_basic_map_n_in(dst_map
);
647 unsigned src_out
= isl_basic_map_n_out(src_map
);
648 unsigned dst_out
= isl_basic_map_n_out(dst_map
);
649 isl_int_set(dst
[0], src
[0]);
650 isl_seq_cpy(dst
+1, src
+1, isl_min(dst_nparam
, src_nparam
));
651 if (dst_nparam
> src_nparam
)
652 isl_seq_clr(dst
+1+src_nparam
,
653 dst_nparam
- src_nparam
);
654 isl_seq_clr(dst
+1+dst_nparam
, in_off
);
655 isl_seq_cpy(dst
+1+dst_nparam
+in_off
,
657 isl_min(dst_in
-in_off
, src_in
));
658 if (dst_in
-in_off
> src_in
)
659 isl_seq_clr(dst
+1+dst_nparam
+in_off
+src_in
,
660 dst_in
- in_off
- src_in
);
661 isl_seq_clr(dst
+1+dst_nparam
+dst_in
, out_off
);
662 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+out_off
,
663 src
+1+src_nparam
+src_in
,
664 isl_min(dst_out
-out_off
, src_out
));
665 if (dst_out
-out_off
> src_out
)
666 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+out_off
+src_out
,
667 dst_out
- out_off
- src_out
);
668 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
, div_off
);
669 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+dst_out
+div_off
,
670 src
+1+src_nparam
+src_in
+src_out
,
671 isl_min(dst_map
->extra
-div_off
, src_map
->n_div
));
672 if (dst_map
->n_div
-div_off
> src_map
->n_div
)
673 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
+
674 div_off
+src_map
->n_div
,
675 dst_map
->n_div
- div_off
- src_map
->n_div
);
678 static void copy_div(struct isl_basic_map
*dst_map
, isl_int
*dst
,
679 struct isl_basic_map
*src_map
, isl_int
*src
,
680 unsigned in_off
, unsigned out_off
, unsigned div_off
)
682 isl_int_set(dst
[0], src
[0]);
683 copy_constraint(dst_map
, dst
+1, src_map
, src
+1, in_off
, out_off
, div_off
);
686 static struct isl_basic_map
*add_constraints(struct isl_basic_map
*bmap1
,
687 struct isl_basic_map
*bmap2
, unsigned i_pos
, unsigned o_pos
)
692 if (!bmap1
|| !bmap2
)
695 div_off
= bmap1
->n_div
;
697 for (i
= 0; i
< bmap2
->n_eq
; ++i
) {
698 int i1
= isl_basic_map_alloc_equality(bmap1
);
701 copy_constraint(bmap1
, bmap1
->eq
[i1
], bmap2
, bmap2
->eq
[i
],
702 i_pos
, o_pos
, div_off
);
705 for (i
= 0; i
< bmap2
->n_ineq
; ++i
) {
706 int i1
= isl_basic_map_alloc_inequality(bmap1
);
709 copy_constraint(bmap1
, bmap1
->ineq
[i1
], bmap2
, bmap2
->ineq
[i
],
710 i_pos
, o_pos
, div_off
);
713 for (i
= 0; i
< bmap2
->n_div
; ++i
) {
714 int i1
= isl_basic_map_alloc_div(bmap1
);
717 copy_div(bmap1
, bmap1
->div
[i1
], bmap2
, bmap2
->div
[i
],
718 i_pos
, o_pos
, div_off
);
721 isl_basic_map_free(bmap2
);
726 isl_basic_map_free(bmap1
);
727 isl_basic_map_free(bmap2
);
731 static void copy_constraint_dim_map(isl_int
*dst
, isl_int
*src
,
732 struct isl_dim_map
*dim_map
)
736 for (i
= 0; i
< dim_map
->len
; ++i
) {
737 if (dim_map
->pos
[i
] < 0)
738 isl_int_set_si(dst
[i
], 0);
740 isl_int_set(dst
[i
], src
[dim_map
->pos
[i
]]);
744 static void copy_div_dim_map(isl_int
*dst
, isl_int
*src
,
745 struct isl_dim_map
*dim_map
)
747 isl_int_set(dst
[0], src
[0]);
748 copy_constraint_dim_map(dst
+1, src
+1, dim_map
);
751 static struct isl_basic_map
*add_constraints_dim_map(struct isl_basic_map
*dst
,
752 struct isl_basic_map
*src
, struct isl_dim_map
*dim_map
)
756 if (!src
|| !dst
|| !dim_map
)
759 for (i
= 0; i
< src
->n_eq
; ++i
) {
760 int i1
= isl_basic_map_alloc_equality(dst
);
763 copy_constraint_dim_map(dst
->eq
[i1
], src
->eq
[i
], dim_map
);
766 for (i
= 0; i
< src
->n_ineq
; ++i
) {
767 int i1
= isl_basic_map_alloc_inequality(dst
);
770 copy_constraint_dim_map(dst
->ineq
[i1
], src
->ineq
[i
], dim_map
);
773 for (i
= 0; i
< src
->n_div
; ++i
) {
774 int i1
= isl_basic_map_alloc_div(dst
);
777 copy_div_dim_map(dst
->div
[i1
], src
->div
[i
], dim_map
);
781 isl_basic_map_free(src
);
786 isl_basic_map_free(src
);
787 isl_basic_map_free(dst
);
791 struct isl_basic_set
*isl_basic_set_add_constraints(struct isl_basic_set
*bset1
,
792 struct isl_basic_set
*bset2
, unsigned pos
)
794 return (struct isl_basic_set
*)
795 add_constraints((struct isl_basic_map
*)bset1
,
796 (struct isl_basic_map
*)bset2
, 0, pos
);
799 struct isl_basic_map
*isl_basic_map_extend_dim(struct isl_basic_map
*base
,
800 struct isl_dim
*dim
, unsigned extra
,
801 unsigned n_eq
, unsigned n_ineq
)
803 struct isl_basic_map
*ext
;
813 dims_ok
= isl_dim_equal(base
->dim
, dim
) &&
814 base
->extra
>= base
->n_div
+ extra
;
816 if (dims_ok
&& room_for_con(base
, n_eq
+ n_ineq
) &&
817 room_for_ineq(base
, n_ineq
)) {
822 isl_assert(base
->ctx
, base
->dim
->nparam
<= dim
->nparam
, goto error
);
823 isl_assert(base
->ctx
, base
->dim
->n_in
<= dim
->n_in
, goto error
);
824 isl_assert(base
->ctx
, base
->dim
->n_out
<= dim
->n_out
, goto error
);
825 extra
+= base
->extra
;
827 n_ineq
+= base
->n_ineq
;
829 ext
= isl_basic_map_alloc_dim(dim
, extra
, n_eq
, n_ineq
);
835 ext
= add_constraints(ext
, base
, 0, 0);
838 ISL_F_CLR(ext
, ISL_BASIC_SET_FINAL
);
845 isl_basic_map_free(base
);
849 struct isl_basic_set
*isl_basic_set_extend_dim(struct isl_basic_set
*base
,
850 struct isl_dim
*dim
, unsigned extra
,
851 unsigned n_eq
, unsigned n_ineq
)
853 return (struct isl_basic_set
*)
854 isl_basic_map_extend_dim((struct isl_basic_map
*)base
, dim
,
855 extra
, n_eq
, n_ineq
);
858 struct isl_basic_map
*isl_basic_map_extend_constraints(
859 struct isl_basic_map
*base
, unsigned n_eq
, unsigned n_ineq
)
863 return isl_basic_map_extend_dim(base
, isl_dim_copy(base
->dim
),
867 struct isl_basic_map
*isl_basic_map_extend(struct isl_basic_map
*base
,
868 unsigned nparam
, unsigned n_in
, unsigned n_out
, unsigned extra
,
869 unsigned n_eq
, unsigned n_ineq
)
871 struct isl_basic_map
*bmap
;
876 dim
= isl_dim_alloc(base
->ctx
, nparam
, n_in
, n_out
);
880 bmap
= isl_basic_map_extend_dim(base
, dim
, extra
, n_eq
, n_ineq
);
884 struct isl_basic_set
*isl_basic_set_extend(struct isl_basic_set
*base
,
885 unsigned nparam
, unsigned dim
, unsigned extra
,
886 unsigned n_eq
, unsigned n_ineq
)
888 return (struct isl_basic_set
*)
889 isl_basic_map_extend((struct isl_basic_map
*)base
,
890 nparam
, 0, dim
, extra
, n_eq
, n_ineq
);
893 struct isl_basic_set
*isl_basic_set_extend_constraints(
894 struct isl_basic_set
*base
, unsigned n_eq
, unsigned n_ineq
)
896 return (struct isl_basic_set
*)
897 isl_basic_map_extend_constraints((struct isl_basic_map
*)base
,
901 struct isl_basic_set
*isl_basic_set_cow(struct isl_basic_set
*bset
)
903 return (struct isl_basic_set
*)
904 isl_basic_map_cow((struct isl_basic_map
*)bset
);
907 struct isl_basic_map
*isl_basic_map_cow(struct isl_basic_map
*bmap
)
914 bmap
= isl_basic_map_dup(bmap
);
916 ISL_F_CLR(bmap
, ISL_BASIC_SET_FINAL
);
920 struct isl_set
*isl_set_cow(struct isl_set
*set
)
928 return isl_set_dup(set
);
931 struct isl_map
*isl_map_cow(struct isl_map
*map
)
939 return isl_map_dup(map
);
942 static void swap_vars(struct isl_blk blk
, isl_int
*a
,
943 unsigned a_len
, unsigned b_len
)
945 isl_seq_cpy(blk
.data
, a
+a_len
, b_len
);
946 isl_seq_cpy(blk
.data
+b_len
, a
, a_len
);
947 isl_seq_cpy(a
, blk
.data
, b_len
+a_len
);
950 struct isl_basic_set
*isl_basic_set_swap_vars(
951 struct isl_basic_set
*bset
, unsigned n
)
961 nparam
= isl_basic_set_n_param(bset
);
962 dim
= isl_basic_set_n_dim(bset
);
963 isl_assert(bset
->ctx
, n
<= dim
, goto error
);
968 bset
= isl_basic_set_cow(bset
);
972 blk
= isl_blk_alloc(bset
->ctx
, dim
);
973 if (isl_blk_is_error(blk
))
976 for (i
= 0; i
< bset
->n_eq
; ++i
)
978 bset
->eq
[i
]+1+nparam
, n
, dim
- n
);
980 for (i
= 0; i
< bset
->n_ineq
; ++i
)
982 bset
->ineq
[i
]+1+nparam
, n
, dim
- n
);
984 for (i
= 0; i
< bset
->n_div
; ++i
)
986 bset
->div
[i
]+1+1+nparam
, n
, dim
- n
);
988 isl_blk_free(bset
->ctx
, blk
);
990 ISL_F_CLR(bset
, ISL_BASIC_SET_NORMALIZED
);
994 isl_basic_set_free(bset
);
998 struct isl_set
*isl_set_swap_vars(struct isl_set
*set
, unsigned n
)
1001 set
= isl_set_cow(set
);
1005 for (i
= 0; i
< set
->n
; ++i
) {
1006 set
->p
[i
] = isl_basic_set_swap_vars(set
->p
[i
], n
);
1012 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
1016 struct isl_basic_map
*isl_basic_map_set_to_empty(struct isl_basic_map
*bmap
)
1022 total
= isl_basic_map_total_dim(bmap
);
1023 isl_basic_map_free_div(bmap
, bmap
->n_div
);
1024 isl_basic_map_free_inequality(bmap
, bmap
->n_ineq
);
1026 isl_basic_map_free_equality(bmap
, bmap
->n_eq
-1);
1028 isl_basic_map_alloc_equality(bmap
);
1032 isl_int_set_si(bmap
->eq
[i
][0], 1);
1033 isl_seq_clr(bmap
->eq
[i
]+1, total
);
1034 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
1035 return isl_basic_map_finalize(bmap
);
1037 isl_basic_map_free(bmap
);
1041 struct isl_basic_set
*isl_basic_set_set_to_empty(struct isl_basic_set
*bset
)
1043 return (struct isl_basic_set
*)
1044 isl_basic_map_set_to_empty((struct isl_basic_map
*)bset
);
1047 static void swap_div(struct isl_basic_map
*bmap
, int a
, int b
)
1050 unsigned off
= isl_dim_total(bmap
->dim
);
1051 isl_int
*t
= bmap
->div
[a
];
1052 bmap
->div
[a
] = bmap
->div
[b
];
1055 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1056 isl_int_swap(bmap
->eq
[i
][1+off
+a
], bmap
->eq
[i
][1+off
+b
]);
1058 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1059 isl_int_swap(bmap
->ineq
[i
][1+off
+a
], bmap
->ineq
[i
][1+off
+b
]);
1061 for (i
= 0; i
< bmap
->n_div
; ++i
)
1062 isl_int_swap(bmap
->div
[i
][1+1+off
+a
], bmap
->div
[i
][1+1+off
+b
]);
1063 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1066 /* Eliminate the specified n dimensions starting at first from the
1067 * constraints using Fourier-Motzkin, The dimensions themselves
1070 struct isl_set
*isl_set_eliminate_dims(struct isl_set
*set
,
1071 unsigned first
, unsigned n
)
1081 set
= isl_set_cow(set
);
1084 isl_assert(set
->ctx
, first
+n
<= isl_set_n_dim(set
), goto error
);
1085 nparam
= isl_set_n_param(set
);
1087 for (i
= 0; i
< set
->n
; ++i
) {
1088 set
->p
[i
] = isl_basic_set_eliminate_vars(set
->p
[i
],
1099 /* Project out n dimensions starting at first using Fourier-Motzkin */
1100 struct isl_set
*isl_set_remove_dims(struct isl_set
*set
,
1101 unsigned first
, unsigned n
)
1103 set
= isl_set_eliminate_dims(set
, first
, n
);
1104 set
= isl_set_drop_dims(set
, first
, n
);
1108 struct isl_basic_set
*isl_basic_set_remove_divs(struct isl_basic_set
*bset
)
1110 bset
= isl_basic_set_eliminate_vars(bset
, isl_dim_total(bset
->dim
),
1118 struct isl_set
*isl_set_remove_divs(struct isl_set
*set
)
1127 set
= isl_set_cow(set
);
1131 for (i
= 0; i
< set
->n
; ++i
) {
1132 set
->p
[i
] = isl_basic_set_remove_divs(set
->p
[i
]);
1142 struct isl_basic_map
*isl_basic_map_remove(struct isl_basic_map
*bmap
,
1143 enum isl_dim_type type
, unsigned first
, unsigned n
)
1147 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
1151 bmap
= isl_basic_map_eliminate_vars(bmap
,
1152 isl_basic_map_offset(bmap
, type
) - 1 + first
, n
);
1153 bmap
= isl_basic_map_drop(bmap
, type
, first
, n
);
1156 isl_basic_map_free(bmap
);
1160 struct isl_map
*isl_map_remove(struct isl_map
*map
,
1161 enum isl_dim_type type
, unsigned first
, unsigned n
)
1169 map
= isl_map_cow(map
);
1172 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
1174 for (i
= 0; i
< map
->n
; ++i
) {
1175 map
->p
[i
] = isl_basic_map_eliminate_vars(map
->p
[i
],
1176 isl_basic_map_offset(map
->p
[i
], type
) - 1 + first
, n
);
1180 map
= isl_map_drop(map
, type
, first
, n
);
1187 /* Project out n inputs starting at first using Fourier-Motzkin */
1188 struct isl_map
*isl_map_remove_inputs(struct isl_map
*map
,
1189 unsigned first
, unsigned n
)
1191 return isl_map_remove(map
, isl_dim_in
, first
, n
);
1194 /* Project out n dimensions starting at first using Fourier-Motzkin */
1195 struct isl_basic_set
*isl_basic_set_remove_dims(struct isl_basic_set
*bset
,
1196 unsigned first
, unsigned n
)
1198 unsigned nparam
= isl_basic_set_n_param(bset
);
1199 bset
= isl_basic_set_eliminate_vars(bset
, nparam
+ first
, n
);
1200 bset
= isl_basic_set_drop_dims(bset
, first
, n
);
1204 static void dump_term(struct isl_basic_map
*bmap
,
1205 isl_int c
, int pos
, FILE *out
)
1208 unsigned in
= isl_basic_map_n_in(bmap
);
1209 unsigned dim
= in
+ isl_basic_map_n_out(bmap
);
1210 unsigned nparam
= isl_basic_map_n_param(bmap
);
1212 isl_int_print(out
, c
, 0);
1214 if (!isl_int_is_one(c
))
1215 isl_int_print(out
, c
, 0);
1216 if (pos
< 1 + nparam
) {
1217 name
= isl_dim_get_name(bmap
->dim
,
1218 isl_dim_param
, pos
- 1);
1220 fprintf(out
, "%s", name
);
1222 fprintf(out
, "p%d", pos
- 1);
1223 } else if (pos
< 1 + nparam
+ in
)
1224 fprintf(out
, "i%d", pos
- 1 - nparam
);
1225 else if (pos
< 1 + nparam
+ dim
)
1226 fprintf(out
, "o%d", pos
- 1 - nparam
- in
);
1228 fprintf(out
, "e%d", pos
- 1 - nparam
- dim
);
1232 static void dump_constraint_sign(struct isl_basic_map
*bmap
, isl_int
*c
,
1233 int sign
, FILE *out
)
1237 unsigned len
= 1 + isl_basic_map_total_dim(bmap
);
1241 for (i
= 0, first
= 1; i
< len
; ++i
) {
1242 if (isl_int_sgn(c
[i
]) * sign
<= 0)
1245 fprintf(out
, " + ");
1247 isl_int_abs(v
, c
[i
]);
1248 dump_term(bmap
, v
, i
, out
);
1255 static void dump_constraint(struct isl_basic_map
*bmap
, isl_int
*c
,
1256 const char *op
, FILE *out
, int indent
)
1260 fprintf(out
, "%*s", indent
, "");
1262 dump_constraint_sign(bmap
, c
, 1, out
);
1263 fprintf(out
, " %s ", op
);
1264 dump_constraint_sign(bmap
, c
, -1, out
);
1268 for (i
= bmap
->n_div
; i
< bmap
->extra
; ++i
) {
1269 if (isl_int_is_zero(c
[1+isl_dim_total(bmap
->dim
)+i
]))
1271 fprintf(out
, "%*s", indent
, "");
1272 fprintf(out
, "ERROR: unused div coefficient not zero\n");
1277 static void dump_constraints(struct isl_basic_map
*bmap
,
1278 isl_int
**c
, unsigned n
,
1279 const char *op
, FILE *out
, int indent
)
1283 for (i
= 0; i
< n
; ++i
)
1284 dump_constraint(bmap
, c
[i
], op
, out
, indent
);
1287 static void dump_affine(struct isl_basic_map
*bmap
, isl_int
*exp
, FILE *out
)
1291 unsigned total
= isl_basic_map_total_dim(bmap
);
1293 for (j
= 0; j
< 1 + total
; ++j
) {
1294 if (isl_int_is_zero(exp
[j
]))
1296 if (!first
&& isl_int_is_pos(exp
[j
]))
1298 dump_term(bmap
, exp
[j
], j
, out
);
1303 static void dump(struct isl_basic_map
*bmap
, FILE *out
, int indent
)
1307 dump_constraints(bmap
, bmap
->eq
, bmap
->n_eq
, "=", out
, indent
);
1308 dump_constraints(bmap
, bmap
->ineq
, bmap
->n_ineq
, ">=", out
, indent
);
1310 for (i
= 0; i
< bmap
->n_div
; ++i
) {
1311 fprintf(out
, "%*s", indent
, "");
1312 fprintf(out
, "e%d = [(", i
);
1313 dump_affine(bmap
, bmap
->div
[i
]+1, out
);
1315 isl_int_print(out
, bmap
->div
[i
][0], 0);
1316 fprintf(out
, "]\n");
1320 void isl_basic_set_dump(struct isl_basic_set
*bset
, FILE *out
, int indent
)
1323 fprintf(out
, "null basic set\n");
1327 fprintf(out
, "%*s", indent
, "");
1328 fprintf(out
, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
1329 bset
->ref
, bset
->dim
->nparam
, bset
->dim
->n_out
,
1330 bset
->extra
, bset
->flags
);
1331 dump((struct isl_basic_map
*)bset
, out
, indent
);
1334 void isl_basic_map_dump(struct isl_basic_map
*bmap
, FILE *out
, int indent
)
1337 fprintf(out
, "null basic map\n");
1341 fprintf(out
, "%*s", indent
, "");
1342 fprintf(out
, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
1343 "flags: %x, n_name: %d\n",
1345 bmap
->dim
->nparam
, bmap
->dim
->n_in
, bmap
->dim
->n_out
,
1346 bmap
->extra
, bmap
->flags
, bmap
->dim
->n_name
);
1347 dump(bmap
, out
, indent
);
1350 int isl_inequality_negate(struct isl_basic_map
*bmap
, unsigned pos
)
1355 total
= isl_basic_map_total_dim(bmap
);
1356 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
1357 isl_seq_neg(bmap
->ineq
[pos
], bmap
->ineq
[pos
], 1 + total
);
1358 isl_int_sub_ui(bmap
->ineq
[pos
][0], bmap
->ineq
[pos
][0], 1);
1359 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1363 struct isl_set
*isl_set_alloc_dim(struct isl_dim
*dim
, int n
, unsigned flags
)
1365 struct isl_set
*set
;
1369 isl_assert(dim
->ctx
, dim
->n_in
== 0, return NULL
);
1370 isl_assert(dim
->ctx
, n
>= 0, return NULL
);
1371 set
= isl_alloc(dim
->ctx
, struct isl_set
,
1372 sizeof(struct isl_set
) +
1373 n
* sizeof(struct isl_basic_set
*));
1377 set
->ctx
= dim
->ctx
;
1378 isl_ctx_ref(set
->ctx
);
1390 struct isl_set
*isl_set_alloc(struct isl_ctx
*ctx
,
1391 unsigned nparam
, unsigned dim
, int n
, unsigned flags
)
1393 struct isl_set
*set
;
1394 struct isl_dim
*dims
;
1396 dims
= isl_dim_alloc(ctx
, nparam
, 0, dim
);
1400 set
= isl_set_alloc_dim(dims
, n
, flags
);
1404 struct isl_set
*isl_set_dup(struct isl_set
*set
)
1407 struct isl_set
*dup
;
1412 dup
= isl_set_alloc_dim(isl_dim_copy(set
->dim
), set
->n
, set
->flags
);
1415 for (i
= 0; i
< set
->n
; ++i
)
1416 dup
= isl_set_add(dup
, isl_basic_set_copy(set
->p
[i
]));
1420 struct isl_set
*isl_set_from_basic_set(struct isl_basic_set
*bset
)
1422 struct isl_set
*set
;
1427 set
= isl_set_alloc_dim(isl_dim_copy(bset
->dim
), 1, ISL_MAP_DISJOINT
);
1429 isl_basic_set_free(bset
);
1432 return isl_set_add(set
, bset
);
1435 struct isl_map
*isl_map_from_basic_map(struct isl_basic_map
*bmap
)
1437 struct isl_map
*map
;
1442 map
= isl_map_alloc_dim(isl_dim_copy(bmap
->dim
), 1, ISL_MAP_DISJOINT
);
1444 isl_basic_map_free(bmap
);
1447 return isl_map_add(map
, bmap
);
1450 struct isl_set
*isl_set_add(struct isl_set
*set
, struct isl_basic_set
*bset
)
1454 isl_assert(set
->ctx
, isl_dim_equal(set
->dim
, bset
->dim
), goto error
);
1455 isl_assert(set
->ctx
, set
->n
< set
->size
, goto error
);
1456 set
->p
[set
->n
] = bset
;
1463 isl_basic_set_free(bset
);
1467 void isl_set_free(struct isl_set
*set
)
1477 isl_ctx_deref(set
->ctx
);
1478 for (i
= 0; i
< set
->n
; ++i
)
1479 isl_basic_set_free(set
->p
[i
]);
1480 isl_dim_free(set
->dim
);
1484 void isl_set_dump(struct isl_set
*set
, FILE *out
, int indent
)
1489 fprintf(out
, "null set\n");
1493 fprintf(out
, "%*s", indent
, "");
1494 fprintf(out
, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
1495 set
->ref
, set
->n
, set
->dim
->nparam
, set
->dim
->n_out
,
1497 for (i
= 0; i
< set
->n
; ++i
) {
1498 fprintf(out
, "%*s", indent
, "");
1499 fprintf(out
, "basic set %d:\n", i
);
1500 isl_basic_set_dump(set
->p
[i
], out
, indent
+4);
1504 void isl_map_dump(struct isl_map
*map
, FILE *out
, int indent
)
1509 fprintf(out
, "null map\n");
1513 fprintf(out
, "%*s", indent
, "");
1514 fprintf(out
, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
1515 "flags: %x, n_name: %d\n",
1516 map
->ref
, map
->n
, map
->dim
->nparam
, map
->dim
->n_in
,
1517 map
->dim
->n_out
, map
->flags
, map
->dim
->n_name
);
1518 for (i
= 0; i
< map
->n
; ++i
) {
1519 fprintf(out
, "%*s", indent
, "");
1520 fprintf(out
, "basic map %d:\n", i
);
1521 isl_basic_map_dump(map
->p
[i
], out
, indent
+4);
1525 struct isl_basic_map
*isl_basic_map_intersect_domain(
1526 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
1528 struct isl_basic_map
*bmap_domain
;
1529 struct isl_dim
*dim
;
1534 isl_assert(bset
->ctx
, isl_dim_match(bmap
->dim
, isl_dim_param
,
1535 bset
->dim
, isl_dim_param
), goto error
);
1537 if (isl_dim_size(bset
->dim
, isl_dim_set
) != 0)
1538 isl_assert(bset
->ctx
,
1539 isl_basic_map_compatible_domain(bmap
, bset
), goto error
);
1541 bmap
= isl_basic_map_cow(bmap
);
1542 bmap
= isl_basic_map_extend_dim(bmap
, isl_dim_copy(bmap
->dim
),
1543 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
1546 dim
= isl_dim_reverse(isl_dim_copy(bset
->dim
));
1547 bmap_domain
= isl_basic_map_from_basic_set(bset
, dim
);
1548 bmap
= add_constraints(bmap
, bmap_domain
, 0, 0);
1550 bmap
= isl_basic_map_simplify(bmap
);
1551 return isl_basic_map_finalize(bmap
);
1553 isl_basic_map_free(bmap
);
1554 isl_basic_set_free(bset
);
1558 struct isl_basic_map
*isl_basic_map_intersect_range(
1559 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
1561 struct isl_basic_map
*bmap_range
;
1566 isl_assert(bset
->ctx
, isl_dim_match(bmap
->dim
, isl_dim_param
,
1567 bset
->dim
, isl_dim_param
), goto error
);
1569 if (isl_dim_size(bset
->dim
, isl_dim_set
) != 0)
1570 isl_assert(bset
->ctx
,
1571 isl_basic_map_compatible_range(bmap
, bset
), goto error
);
1573 bmap
= isl_basic_map_cow(bmap
);
1574 bmap
= isl_basic_map_extend_dim(bmap
, isl_dim_copy(bmap
->dim
),
1575 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
1578 bmap_range
= isl_basic_map_from_basic_set(bset
, isl_dim_copy(bset
->dim
));
1579 bmap
= add_constraints(bmap
, bmap_range
, 0, 0);
1581 bmap
= isl_basic_map_simplify(bmap
);
1582 return isl_basic_map_finalize(bmap
);
1584 isl_basic_map_free(bmap
);
1585 isl_basic_set_free(bset
);
1589 static int basic_map_contains(struct isl_basic_map
*bmap
, struct isl_vec
*vec
)
1595 total
= 1 + isl_basic_map_total_dim(bmap
);
1596 if (total
!= vec
->size
)
1601 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
1602 isl_seq_inner_product(vec
->block
.data
, bmap
->eq
[i
], total
, &s
);
1603 if (!isl_int_is_zero(s
)) {
1609 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
1610 isl_seq_inner_product(vec
->block
.data
, bmap
->ineq
[i
], total
, &s
);
1611 if (isl_int_is_neg(s
)) {
1622 int isl_basic_set_contains(struct isl_basic_set
*bset
, struct isl_vec
*vec
)
1624 return basic_map_contains((struct isl_basic_map
*)bset
, vec
);
1627 struct isl_basic_map
*isl_basic_map_intersect(
1628 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
1630 struct isl_vec
*sample
= NULL
;
1632 if (!bmap1
|| !bmap2
)
1635 isl_assert(bmap1
->ctx
, isl_dim_match(bmap1
->dim
, isl_dim_param
,
1636 bmap2
->dim
, isl_dim_param
), goto error
);
1637 if (isl_dim_total(bmap1
->dim
) ==
1638 isl_dim_size(bmap1
->dim
, isl_dim_param
) &&
1639 isl_dim_total(bmap2
->dim
) !=
1640 isl_dim_size(bmap2
->dim
, isl_dim_param
))
1641 return isl_basic_map_intersect(bmap2
, bmap1
);
1643 if (isl_dim_total(bmap2
->dim
) !=
1644 isl_dim_size(bmap2
->dim
, isl_dim_param
))
1645 isl_assert(bmap1
->ctx
,
1646 isl_dim_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
1648 if (bmap1
->sample
&&
1649 basic_map_contains(bmap1
, bmap1
->sample
) > 0 &&
1650 basic_map_contains(bmap2
, bmap1
->sample
) > 0)
1651 sample
= isl_vec_copy(bmap1
->ctx
, bmap1
->sample
);
1652 else if (bmap2
->sample
&&
1653 basic_map_contains(bmap1
, bmap2
->sample
) > 0 &&
1654 basic_map_contains(bmap2
, bmap2
->sample
) > 0)
1655 sample
= isl_vec_copy(bmap2
->ctx
, bmap2
->sample
);
1657 bmap1
= isl_basic_map_cow(bmap1
);
1658 bmap1
= isl_basic_map_extend_dim(bmap1
, isl_dim_copy(bmap1
->dim
),
1659 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
1662 bmap1
= add_constraints(bmap1
, bmap2
, 0, 0);
1665 isl_vec_free(bmap1
->ctx
, bmap1
->sample
);
1666 bmap1
->sample
= sample
;
1669 bmap1
= isl_basic_map_simplify(bmap1
);
1670 return isl_basic_map_finalize(bmap1
);
1673 isl_vec_free(bmap1
->ctx
, sample
);
1674 isl_basic_map_free(bmap1
);
1675 isl_basic_map_free(bmap2
);
1679 struct isl_basic_set
*isl_basic_set_intersect(
1680 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
1682 return (struct isl_basic_set
*)
1683 isl_basic_map_intersect(
1684 (struct isl_basic_map
*)bset1
,
1685 (struct isl_basic_map
*)bset2
);
1688 struct isl_map
*isl_map_intersect(struct isl_map
*map1
, struct isl_map
*map2
)
1691 struct isl_map
*result
;
1697 isl_assert(map1
->ctx
, isl_dim_match(map1
->dim
, isl_dim_param
,
1698 map2
->dim
, isl_dim_param
), goto error
);
1699 if (isl_dim_total(map1
->dim
) ==
1700 isl_dim_size(map1
->dim
, isl_dim_param
) &&
1701 isl_dim_total(map2
->dim
) != isl_dim_size(map2
->dim
, isl_dim_param
))
1702 return isl_map_intersect(map2
, map1
);
1704 if (isl_dim_total(map2
->dim
) != isl_dim_size(map2
->dim
, isl_dim_param
))
1705 isl_assert(map1
->ctx
,
1706 isl_dim_equal(map1
->dim
, map2
->dim
), goto error
);
1708 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
1709 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
1710 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
1712 result
= isl_map_alloc_dim(isl_dim_copy(map1
->dim
),
1713 map1
->n
* map2
->n
, flags
);
1716 for (i
= 0; i
< map1
->n
; ++i
)
1717 for (j
= 0; j
< map2
->n
; ++j
) {
1718 struct isl_basic_map
*part
;
1719 part
= isl_basic_map_intersect(
1720 isl_basic_map_copy(map1
->p
[i
]),
1721 isl_basic_map_copy(map2
->p
[j
]));
1722 if (isl_basic_map_is_empty(part
))
1723 isl_basic_map_free(part
);
1725 result
= isl_map_add(result
, part
);
1738 struct isl_set
*isl_set_intersect(struct isl_set
*set1
, struct isl_set
*set2
)
1740 return (struct isl_set
*)
1741 isl_map_intersect((struct isl_map
*)set1
,
1742 (struct isl_map
*)set2
);
1745 struct isl_basic_map
*isl_basic_map_reverse(struct isl_basic_map
*bmap
)
1747 struct isl_dim
*dim
;
1748 struct isl_basic_set
*bset
;
1753 bmap
= isl_basic_map_cow(bmap
);
1756 dim
= isl_dim_reverse(isl_dim_copy(bmap
->dim
));
1757 in
= isl_basic_map_n_in(bmap
);
1758 bset
= isl_basic_set_from_basic_map(bmap
);
1759 bset
= isl_basic_set_swap_vars(bset
, in
);
1760 return isl_basic_map_from_basic_set(bset
, dim
);
1763 /* Turn final n dimensions into existentially quantified variables.
1765 struct isl_basic_set
*isl_basic_set_project_out(
1766 struct isl_basic_set
*bset
, unsigned n
, unsigned flags
)
1776 isl_assert(bset
->ctx
, n
<= isl_basic_set_n_dim(bset
), goto error
);
1781 bset
= isl_basic_set_cow(bset
);
1783 row_size
= 1 + isl_dim_total(bset
->dim
) + bset
->extra
;
1784 old
= bset
->block2
.data
;
1785 bset
->block2
= isl_blk_extend(bset
->ctx
, bset
->block2
,
1786 (bset
->extra
+ n
) * (1 + row_size
));
1787 if (!bset
->block2
.data
)
1789 new_div
= isl_alloc_array(ctx
, isl_int
*, bset
->extra
+ n
);
1792 for (i
= 0; i
< n
; ++i
) {
1793 new_div
[i
] = bset
->block2
.data
+
1794 (bset
->extra
+ i
) * (1 + row_size
);
1795 isl_seq_clr(new_div
[i
], 1 + row_size
);
1797 for (i
= 0; i
< bset
->extra
; ++i
)
1798 new_div
[n
+ i
] = bset
->block2
.data
+ (bset
->div
[i
] - old
);
1800 bset
->div
= new_div
;
1803 bset
->dim
= isl_dim_drop_outputs(bset
->dim
,
1804 isl_basic_set_n_dim(bset
) - n
, n
);
1807 bset
= isl_basic_set_simplify(bset
);
1808 return isl_basic_set_finalize(bset
);
1810 isl_basic_set_free(bset
);
1814 struct isl_basic_map
*add_divs(struct isl_basic_map
*bmap
, unsigned n
)
1818 for (i
= 0; i
< n
; ++i
) {
1819 j
= isl_basic_map_alloc_div(bmap
);
1822 isl_seq_clr(bmap
->div
[j
], 1+1+isl_basic_map_total_dim(bmap
));
1826 isl_basic_map_free(bmap
);
1830 struct isl_basic_map
*isl_basic_map_apply_range(
1831 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
1833 struct isl_dim
*dim_result
= NULL
;
1834 struct isl_basic_map
*bmap
;
1835 unsigned n_in
, n_out
, n
, nparam
, total
, pos
;
1836 struct isl_dim_map
*dim_map1
, *dim_map2
;
1838 if (!bmap1
|| !bmap2
)
1841 dim_result
= isl_dim_join(isl_dim_copy(bmap1
->dim
),
1842 isl_dim_copy(bmap2
->dim
));
1844 n_in
= isl_basic_map_n_in(bmap1
);
1845 n_out
= isl_basic_map_n_out(bmap2
);
1846 n
= isl_basic_map_n_out(bmap1
);
1847 nparam
= isl_basic_map_n_param(bmap1
);
1849 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ n
;
1850 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
1851 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
1852 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
1853 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
1854 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
1855 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_in
);
1856 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_out
);
1857 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
1858 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
1859 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
1861 bmap
= isl_basic_map_alloc_dim(dim_result
,
1862 bmap1
->n_div
+ bmap2
->n_div
+ n
,
1863 bmap1
->n_eq
+ bmap2
->n_eq
,
1864 bmap1
->n_ineq
+ bmap2
->n_ineq
);
1865 bmap
= add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
1866 bmap
= add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
1867 bmap
= add_divs(bmap
, n
);
1868 bmap
= isl_basic_map_simplify(bmap
);
1869 return isl_basic_map_finalize(bmap
);
1871 isl_basic_map_free(bmap1
);
1872 isl_basic_map_free(bmap2
);
1876 struct isl_basic_set
*isl_basic_set_apply(
1877 struct isl_basic_set
*bset
, struct isl_basic_map
*bmap
)
1882 isl_assert(set
->ctx
, isl_basic_map_compatible_domain(bmap
, bset
),
1885 return (struct isl_basic_set
*)
1886 isl_basic_map_apply_range((struct isl_basic_map
*)bset
, bmap
);
1888 isl_basic_set_free(bset
);
1889 isl_basic_map_free(bmap
);
1893 struct isl_basic_map
*isl_basic_map_apply_domain(
1894 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
1896 if (!bmap1
|| !bmap2
)
1900 isl_basic_map_n_in(bmap1
) == isl_basic_map_n_in(bmap2
), goto error
);
1902 isl_basic_map_n_param(bmap1
) == isl_basic_map_n_param(bmap2
),
1905 bmap1
= isl_basic_map_reverse(bmap1
);
1906 bmap1
= isl_basic_map_apply_range(bmap1
, bmap2
);
1907 return isl_basic_map_reverse(bmap1
);
1909 isl_basic_map_free(bmap1
);
1910 isl_basic_map_free(bmap2
);
1914 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
1915 * A \cap B -> f(A) + f(B)
1917 struct isl_basic_map
*isl_basic_map_sum(
1918 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
1920 unsigned n_in
, n_out
, nparam
, total
, pos
;
1921 struct isl_basic_map
*bmap
= NULL
;
1922 struct isl_dim_map
*dim_map1
, *dim_map2
;
1925 if (!bmap1
|| !bmap2
)
1928 isl_assert(bmap1
->ctx
, isl_dim_equal(bmap1
->dim
, bmap2
->dim
),
1931 nparam
= isl_basic_map_n_param(bmap1
);
1932 n_in
= isl_basic_map_n_in(bmap1
);
1933 n_out
= isl_basic_map_n_out(bmap1
);
1935 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
;
1936 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
1937 dim_map2
= isl_dim_map_alloc(bmap2
->ctx
, total
);
1938 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
1939 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
);
1940 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
1941 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
1942 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_in
+ n_out
);
1943 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
1944 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
1945 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_out
);
1947 bmap
= isl_basic_map_alloc_dim(isl_dim_copy(bmap1
->dim
),
1948 bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
,
1949 bmap1
->n_eq
+ bmap2
->n_eq
+ n_out
,
1950 bmap1
->n_ineq
+ bmap2
->n_ineq
);
1951 for (i
= 0; i
< n_out
; ++i
) {
1952 int j
= isl_basic_map_alloc_equality(bmap
);
1955 isl_seq_clr(bmap
->eq
[j
], 1+total
);
1956 isl_int_set_si(bmap
->eq
[j
][1+nparam
+n_in
+i
], -1);
1957 isl_int_set_si(bmap
->eq
[j
][1+pos
+i
], 1);
1958 isl_int_set_si(bmap
->eq
[j
][1+pos
-n_out
+i
], 1);
1960 bmap
= add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
1961 bmap
= add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
1962 bmap
= add_divs(bmap
, 2 * n_out
);
1964 bmap
= isl_basic_map_simplify(bmap
);
1965 return isl_basic_map_finalize(bmap
);
1967 isl_basic_map_free(bmap
);
1968 isl_basic_map_free(bmap1
);
1969 isl_basic_map_free(bmap2
);
1973 /* Given a basic map A -> f(A), construct A -> -f(A).
1975 struct isl_basic_map
*isl_basic_map_neg(struct isl_basic_map
*bmap
)
1980 bmap
= isl_basic_map_cow(bmap
);
1984 n
= isl_basic_map_dim(bmap
, isl_dim_out
);
1985 off
= isl_basic_map_offset(bmap
, isl_dim_out
);
1986 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1987 for (j
= 0; j
< n
; ++j
)
1988 isl_int_neg(bmap
->eq
[i
][off
+j
], bmap
->eq
[i
][off
+j
]);
1989 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1990 for (j
= 0; j
< n
; ++j
)
1991 isl_int_neg(bmap
->ineq
[i
][off
+j
], bmap
->ineq
[i
][off
+j
]);
1992 for (i
= 0; i
< bmap
->n_div
; ++i
)
1993 for (j
= 0; j
< n
; ++j
)
1994 isl_int_neg(bmap
->div
[i
][1+off
+j
], bmap
->div
[i
][1+off
+j
]);
1995 return isl_basic_map_finalize(bmap
);
1998 /* Given a basic map A -> f(A) and an integer d, construct a basic map
1999 * A -> floor(f(A)/d).
2001 struct isl_basic_map
*isl_basic_map_floordiv(struct isl_basic_map
*bmap
,
2004 unsigned n_in
, n_out
, nparam
, total
, pos
;
2005 struct isl_basic_map
*result
= NULL
;
2006 struct isl_dim_map
*dim_map
;
2012 nparam
= isl_basic_map_n_param(bmap
);
2013 n_in
= isl_basic_map_n_in(bmap
);
2014 n_out
= isl_basic_map_n_out(bmap
);
2016 total
= nparam
+ n_in
+ n_out
+ bmap
->n_div
+ n_out
;
2017 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
2018 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_param
, pos
= 0);
2019 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_in
, pos
+= nparam
);
2020 isl_dim_map_div(dim_map
, bmap
, pos
+= n_in
+ n_out
);
2021 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_out
, pos
+= bmap
->n_div
);
2023 result
= isl_basic_map_alloc_dim(isl_dim_copy(bmap
->dim
),
2024 bmap
->n_div
+ n_out
,
2025 bmap
->n_eq
, bmap
->n_ineq
+ 2 * n_out
);
2026 result
= add_constraints_dim_map(result
, bmap
, dim_map
);
2027 result
= add_divs(result
, n_out
);
2028 for (i
= 0; i
< n_out
; ++i
) {
2030 j
= isl_basic_map_alloc_inequality(result
);
2033 isl_seq_clr(result
->ineq
[j
], 1+total
);
2034 isl_int_neg(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
2035 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], 1);
2036 j
= isl_basic_map_alloc_inequality(result
);
2039 isl_seq_clr(result
->ineq
[j
], 1+total
);
2040 isl_int_set(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
2041 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], -1);
2042 isl_int_sub_ui(result
->ineq
[j
][0], d
, 1);
2045 result
= isl_basic_map_simplify(result
);
2046 return isl_basic_map_finalize(result
);
2048 isl_basic_map_free(result
);
2052 static struct isl_basic_map
*var_equal(struct isl_basic_map
*bmap
, unsigned pos
)
2058 i
= isl_basic_map_alloc_equality(bmap
);
2061 nparam
= isl_basic_map_n_param(bmap
);
2062 n_in
= isl_basic_map_n_in(bmap
);
2063 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
2064 isl_int_set_si(bmap
->eq
[i
][1+nparam
+pos
], -1);
2065 isl_int_set_si(bmap
->eq
[i
][1+nparam
+n_in
+pos
], 1);
2066 return isl_basic_map_finalize(bmap
);
2068 isl_basic_map_free(bmap
);
2072 static struct isl_basic_map
*var_more(struct isl_basic_map
*bmap
, unsigned pos
)
2078 i
= isl_basic_map_alloc_inequality(bmap
);
2081 nparam
= isl_basic_map_n_param(bmap
);
2082 n_in
= isl_basic_map_n_in(bmap
);
2083 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
2084 isl_int_set_si(bmap
->ineq
[i
][0], -1);
2085 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
2086 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
2087 return isl_basic_map_finalize(bmap
);
2089 isl_basic_map_free(bmap
);
2093 static struct isl_basic_map
*var_less(struct isl_basic_map
*bmap
, unsigned pos
)
2099 i
= isl_basic_map_alloc_inequality(bmap
);
2102 nparam
= isl_basic_map_n_param(bmap
);
2103 n_in
= isl_basic_map_n_in(bmap
);
2104 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
2105 isl_int_set_si(bmap
->ineq
[i
][0], -1);
2106 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
2107 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
2108 return isl_basic_map_finalize(bmap
);
2110 isl_basic_map_free(bmap
);
2114 struct isl_basic_map
*isl_basic_map_equal(struct isl_dim
*dim
, unsigned n_equal
)
2117 struct isl_basic_map
*bmap
;
2118 bmap
= isl_basic_map_alloc_dim(dim
, 0, n_equal
, 0);
2121 for (i
= 0; i
< n_equal
&& bmap
; ++i
)
2122 bmap
= var_equal(bmap
, i
);
2123 return isl_basic_map_finalize(bmap
);
2126 struct isl_basic_map
*isl_basic_map_less_at(struct isl_dim
*dim
, unsigned pos
)
2129 struct isl_basic_map
*bmap
;
2130 bmap
= isl_basic_map_alloc_dim(dim
, 0, pos
, 1);
2133 for (i
= 0; i
< pos
&& bmap
; ++i
)
2134 bmap
= var_equal(bmap
, i
);
2136 bmap
= var_less(bmap
, pos
);
2137 return isl_basic_map_finalize(bmap
);
2140 struct isl_basic_map
*isl_basic_map_more_at(struct isl_dim
*dim
, unsigned pos
)
2143 struct isl_basic_map
*bmap
;
2144 bmap
= isl_basic_map_alloc_dim(dim
, 0, pos
, 1);
2147 for (i
= 0; i
< pos
&& bmap
; ++i
)
2148 bmap
= var_equal(bmap
, i
);
2150 bmap
= var_more(bmap
, pos
);
2151 return isl_basic_map_finalize(bmap
);
2154 struct isl_basic_map
*isl_basic_map_from_basic_set(
2155 struct isl_basic_set
*bset
, struct isl_dim
*dim
)
2157 struct isl_basic_map
*bmap
;
2159 bset
= isl_basic_set_cow(bset
);
2163 isl_assert(bset
->ctx
, isl_dim_compatible(bset
->dim
, dim
), goto error
);
2164 isl_dim_free(bset
->dim
);
2165 bmap
= (struct isl_basic_map
*) bset
;
2167 return isl_basic_map_finalize(bmap
);
2169 isl_basic_set_free(bset
);
2174 struct isl_basic_set
*isl_basic_set_from_basic_map(struct isl_basic_map
*bmap
)
2178 if (bmap
->dim
->n_in
== 0)
2179 return (struct isl_basic_set
*)bmap
;
2180 bmap
= isl_basic_map_cow(bmap
);
2183 bmap
->dim
= isl_dim_cow(bmap
->dim
);
2186 bmap
->dim
->n_out
+= bmap
->dim
->n_in
;
2187 bmap
->dim
->n_in
= 0;
2188 bmap
= isl_basic_map_finalize(bmap
);
2189 return (struct isl_basic_set
*)bmap
;
2191 isl_basic_map_free(bmap
);
2195 /* For a div d = floor(f/m), add the constraints
2198 * -(f-(n-1)) + m d >= 0
2200 * Note that the second constraint is the negation of
2204 static int add_div_constraints(struct isl_basic_map
*bmap
, unsigned div
)
2207 unsigned total
= isl_basic_map_total_dim(bmap
);
2208 unsigned div_pos
= 1 + total
- bmap
->n_div
+ div
;
2210 i
= isl_basic_map_alloc_inequality(bmap
);
2213 isl_seq_cpy(bmap
->ineq
[i
], bmap
->div
[div
]+1, 1+total
);
2214 isl_int_neg(bmap
->ineq
[i
][div_pos
], bmap
->div
[div
][0]);
2216 j
= isl_basic_map_alloc_inequality(bmap
);
2219 isl_seq_neg(bmap
->ineq
[j
], bmap
->ineq
[i
], 1 + total
);
2220 isl_int_add(bmap
->ineq
[j
][0], bmap
->ineq
[j
][0], bmap
->ineq
[j
][div_pos
]);
2221 isl_int_sub_ui(bmap
->ineq
[j
][0], bmap
->ineq
[j
][0], 1);
2225 struct isl_basic_set
*isl_basic_map_underlying_set(
2226 struct isl_basic_map
*bmap
)
2230 if (bmap
->dim
->nparam
== 0 && bmap
->dim
->n_in
== 0 && bmap
->n_div
== 0)
2231 return (struct isl_basic_set
*)bmap
;
2232 bmap
= isl_basic_map_cow(bmap
);
2235 bmap
->dim
= isl_dim_underlying(bmap
->dim
, bmap
->n_div
);
2238 bmap
->extra
-= bmap
->n_div
;
2240 bmap
= isl_basic_map_finalize(bmap
);
2241 return (struct isl_basic_set
*)bmap
;
2246 struct isl_basic_map
*isl_basic_map_overlying_set(
2247 struct isl_basic_set
*bset
, struct isl_basic_map
*like
)
2249 struct isl_basic_map
*bmap
;
2250 struct isl_ctx
*ctx
;
2257 isl_assert(ctx
, bset
->n_div
== 0, goto error
);
2258 isl_assert(ctx
, isl_basic_set_n_param(bset
) == 0, goto error
);
2259 isl_assert(ctx
, bset
->dim
->n_out
== isl_basic_map_total_dim(like
),
2261 if (isl_dim_equal(bset
->dim
, like
->dim
) && like
->n_div
== 0) {
2262 isl_basic_map_free(like
);
2263 return (struct isl_basic_map
*)bset
;
2265 bset
= isl_basic_set_cow(bset
);
2268 total
= bset
->dim
->n_out
+ bset
->extra
;
2269 bmap
= (struct isl_basic_map
*)bset
;
2270 isl_dim_free(bmap
->dim
);
2271 bmap
->dim
= isl_dim_copy(like
->dim
);
2274 bmap
->n_div
= like
->n_div
;
2275 bmap
->extra
+= like
->n_div
;
2278 ltotal
= total
- bmap
->extra
+ like
->extra
;
2281 bmap
->block2
= isl_blk_extend(ctx
, bmap
->block2
,
2282 bmap
->extra
* (1 + 1 + total
));
2283 if (isl_blk_is_error(bmap
->block2
))
2285 bmap
->div
= isl_realloc_array(ctx
, bmap
->div
, isl_int
*,
2289 for (i
= 0; i
< bmap
->extra
; ++i
)
2290 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + 1 + total
);
2291 for (i
= 0; i
< like
->n_div
; ++i
) {
2292 isl_seq_cpy(bmap
->div
[i
], like
->div
[i
], 1 + 1 + ltotal
);
2293 isl_seq_clr(bmap
->div
[i
]+1+1+ltotal
, total
- ltotal
);
2295 bmap
= isl_basic_map_extend_constraints(bmap
,
2296 0, 2 * like
->n_div
);
2297 for (i
= 0; i
< like
->n_div
; ++i
) {
2298 if (isl_int_is_zero(bmap
->div
[i
][0]))
2300 if (add_div_constraints(bmap
, i
) < 0)
2304 isl_basic_map_free(like
);
2305 bmap
= isl_basic_map_simplify(bmap
);
2306 bmap
= isl_basic_map_finalize(bmap
);
2309 isl_basic_map_free(like
);
2310 isl_basic_set_free(bset
);
2314 struct isl_basic_set
*isl_basic_set_from_underlying_set(
2315 struct isl_basic_set
*bset
, struct isl_basic_set
*like
)
2317 return (struct isl_basic_set
*)
2318 isl_basic_map_overlying_set(bset
, (struct isl_basic_map
*)like
);
2321 struct isl_set
*isl_set_from_underlying_set(
2322 struct isl_set
*set
, struct isl_basic_set
*like
)
2328 isl_assert(set
->ctx
, set
->dim
->n_out
== isl_basic_set_total_dim(like
),
2330 if (isl_dim_equal(set
->dim
, like
->dim
) && like
->n_div
== 0) {
2331 isl_basic_set_free(like
);
2334 set
= isl_set_cow(set
);
2337 for (i
= 0; i
< set
->n
; ++i
) {
2338 set
->p
[i
] = isl_basic_set_from_underlying_set(set
->p
[i
],
2339 isl_basic_set_copy(like
));
2343 isl_dim_free(set
->dim
);
2344 set
->dim
= isl_dim_copy(like
->dim
);
2347 isl_basic_set_free(like
);
2350 isl_basic_set_free(like
);
2355 struct isl_set
*isl_map_underlying_set(struct isl_map
*map
)
2359 map
= isl_map_cow(map
);
2362 map
->dim
= isl_dim_cow(map
->dim
);
2366 for (i
= 1; i
< map
->n
; ++i
)
2367 isl_assert(map
->ctx
, map
->p
[0]->n_div
== map
->p
[i
]->n_div
,
2369 for (i
= 0; i
< map
->n
; ++i
) {
2370 map
->p
[i
] = (struct isl_basic_map
*)
2371 isl_basic_map_underlying_set(map
->p
[i
]);
2376 map
->dim
= isl_dim_underlying(map
->dim
, 0);
2378 isl_dim_free(map
->dim
);
2379 map
->dim
= isl_dim_copy(map
->p
[0]->dim
);
2383 return (struct isl_set
*)map
;
2389 struct isl_set
*isl_set_to_underlying_set(struct isl_set
*set
)
2391 return (struct isl_set
*)isl_map_underlying_set((struct isl_map
*)set
);
2394 struct isl_basic_set
*isl_basic_map_domain(struct isl_basic_map
*bmap
)
2396 struct isl_basic_set
*domain
;
2400 n_out
= isl_basic_map_n_out(bmap
);
2401 domain
= isl_basic_set_from_basic_map(bmap
);
2402 return isl_basic_set_project_out(domain
, n_out
, 0);
2405 struct isl_basic_set
*isl_basic_map_range(struct isl_basic_map
*bmap
)
2407 return isl_basic_map_domain(isl_basic_map_reverse(bmap
));
2410 struct isl_set
*isl_map_range(struct isl_map
*map
)
2413 struct isl_set
*set
;
2417 map
= isl_map_cow(map
);
2421 set
= (struct isl_set
*) map
;
2422 if (set
->dim
->n_in
!= 0) {
2423 set
->dim
= isl_dim_drop_inputs(set
->dim
, 0, set
->dim
->n_in
);
2427 for (i
= 0; i
< map
->n
; ++i
) {
2428 set
->p
[i
] = isl_basic_map_range(map
->p
[i
]);
2432 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
2433 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
2440 struct isl_map
*isl_map_from_set(struct isl_set
*set
, struct isl_dim
*dim
)
2443 struct isl_map
*map
= NULL
;
2445 set
= isl_set_cow(set
);
2448 isl_assert(set
->ctx
, isl_dim_compatible(set
->dim
, dim
), goto error
);
2449 map
= (struct isl_map
*)set
;
2450 for (i
= 0; i
< set
->n
; ++i
) {
2451 map
->p
[i
] = isl_basic_map_from_basic_set(
2452 set
->p
[i
], isl_dim_copy(dim
));
2456 isl_dim_free(map
->dim
);
2465 struct isl_map
*isl_map_from_range(struct isl_set
*set
)
2467 return (struct isl_map
*)set
;
2470 struct isl_set
*isl_set_from_map(struct isl_map
*map
)
2473 struct isl_set
*set
= NULL
;
2477 map
= isl_map_cow(map
);
2480 map
->dim
= isl_dim_cow(map
->dim
);
2483 map
->dim
->n_out
+= map
->dim
->n_in
;
2485 set
= (struct isl_set
*)map
;
2486 for (i
= 0; i
< map
->n
; ++i
) {
2487 set
->p
[i
] = isl_basic_set_from_basic_map(map
->p
[i
]);
2497 struct isl_map
*isl_map_alloc_dim(struct isl_dim
*dim
, int n
, unsigned flags
)
2499 struct isl_map
*map
;
2503 isl_assert(dim
->ctx
, n
>= 0, return NULL
);
2504 map
= isl_alloc(dim
->ctx
, struct isl_map
,
2505 sizeof(struct isl_map
) +
2506 n
* sizeof(struct isl_basic_map
*));
2510 map
->ctx
= dim
->ctx
;
2511 isl_ctx_ref(map
->ctx
);
2523 struct isl_map
*isl_map_alloc(struct isl_ctx
*ctx
,
2524 unsigned nparam
, unsigned in
, unsigned out
, int n
,
2527 struct isl_map
*map
;
2528 struct isl_dim
*dims
;
2530 dims
= isl_dim_alloc(ctx
, nparam
, in
, out
);
2534 map
= isl_map_alloc_dim(dims
, n
, flags
);
2538 struct isl_basic_map
*isl_basic_map_empty(struct isl_ctx
*ctx
,
2539 unsigned nparam
, unsigned in
, unsigned out
)
2541 struct isl_basic_map
*bmap
;
2542 bmap
= isl_basic_map_alloc(ctx
, nparam
, in
, out
, 0, 1, 0);
2543 bmap
= isl_basic_map_set_to_empty(bmap
);
2547 struct isl_basic_set
*isl_basic_set_empty(struct isl_dim
*dim
)
2549 struct isl_basic_set
*bset
;
2550 bset
= isl_basic_set_alloc_dim(dim
, 0, 1, 0);
2551 bset
= isl_basic_set_set_to_empty(bset
);
2555 struct isl_basic_map
*isl_basic_map_empty_like(struct isl_basic_map
*model
)
2557 struct isl_basic_map
*bmap
;
2560 bmap
= isl_basic_map_alloc_dim(isl_dim_copy(model
->dim
), 0, 1, 0);
2561 bmap
= isl_basic_map_set_to_empty(bmap
);
2565 struct isl_basic_map
*isl_basic_map_empty_like_map(struct isl_map
*model
)
2567 struct isl_basic_map
*bmap
;
2570 bmap
= isl_basic_map_alloc_dim(isl_dim_copy(model
->dim
), 0, 1, 0);
2571 bmap
= isl_basic_map_set_to_empty(bmap
);
2575 struct isl_basic_set
*isl_basic_set_empty_like(struct isl_basic_set
*model
)
2577 struct isl_basic_set
*bset
;
2580 bset
= isl_basic_set_alloc_dim(isl_dim_copy(model
->dim
), 0, 1, 0);
2581 bset
= isl_basic_set_set_to_empty(bset
);
2585 struct isl_basic_map
*isl_basic_map_universe(struct isl_dim
*dim
)
2587 struct isl_basic_map
*bmap
;
2588 bmap
= isl_basic_map_alloc_dim(dim
, 0, 0, 0);
2592 struct isl_basic_set
*isl_basic_set_universe(struct isl_dim
*dim
)
2594 struct isl_basic_set
*bset
;
2595 bset
= isl_basic_set_alloc_dim(dim
, 0, 0, 0);
2599 struct isl_basic_set
*isl_basic_set_universe_like(struct isl_basic_set
*model
)
2603 return isl_basic_set_alloc_dim(isl_dim_copy(model
->dim
), 0, 0, 0);
2606 struct isl_map
*isl_map_empty(struct isl_dim
*dim
)
2608 return isl_map_alloc_dim(dim
, 0, ISL_MAP_DISJOINT
);
2611 struct isl_map
*isl_map_empty_like(struct isl_map
*model
)
2615 return isl_map_alloc_dim(isl_dim_copy(model
->dim
), 0, ISL_MAP_DISJOINT
);
2618 struct isl_map
*isl_map_empty_like_basic_map(struct isl_basic_map
*model
)
2622 return isl_map_alloc_dim(isl_dim_copy(model
->dim
), 0, ISL_MAP_DISJOINT
);
2625 struct isl_set
*isl_set_empty(struct isl_dim
*dim
)
2627 return isl_set_alloc_dim(dim
, 0, ISL_MAP_DISJOINT
);
2630 struct isl_set
*isl_set_empty_like(struct isl_set
*model
)
2634 return isl_set_empty(isl_dim_copy(model
->dim
));
2637 struct isl_set
*isl_set_universe(struct isl_dim
*dim
)
2639 struct isl_set
*set
;
2642 set
= isl_set_alloc_dim(isl_dim_copy(dim
), 1, ISL_MAP_DISJOINT
);
2643 set
= isl_set_add(set
, isl_basic_set_universe(dim
));
2647 struct isl_map
*isl_map_dup(struct isl_map
*map
)
2650 struct isl_map
*dup
;
2654 dup
= isl_map_alloc_dim(isl_dim_copy(map
->dim
), map
->n
, map
->flags
);
2655 for (i
= 0; i
< map
->n
; ++i
)
2656 dup
= isl_map_add(dup
, isl_basic_map_copy(map
->p
[i
]));
2660 struct isl_map
*isl_map_add(struct isl_map
*map
, struct isl_basic_map
*bmap
)
2664 isl_assert(map
->ctx
, isl_dim_equal(map
->dim
, bmap
->dim
), goto error
);
2665 isl_assert(map
->ctx
, map
->n
< map
->size
, goto error
);
2666 map
->p
[map
->n
] = bmap
;
2668 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
2674 isl_basic_map_free(bmap
);
2678 void isl_map_free(struct isl_map
*map
)
2688 isl_ctx_deref(map
->ctx
);
2689 for (i
= 0; i
< map
->n
; ++i
)
2690 isl_basic_map_free(map
->p
[i
]);
2691 isl_dim_free(map
->dim
);
2695 struct isl_map
*isl_map_extend(struct isl_map
*base
,
2696 unsigned nparam
, unsigned n_in
, unsigned n_out
)
2700 base
= isl_map_cow(base
);
2704 base
->dim
= isl_dim_extend(base
->dim
, nparam
, n_in
, n_out
);
2707 for (i
= 0; i
< base
->n
; ++i
) {
2708 base
->p
[i
] = isl_basic_map_extend_dim(base
->p
[i
],
2709 isl_dim_copy(base
->dim
), 0, 0, 0);
2719 struct isl_set
*isl_set_extend(struct isl_set
*base
,
2720 unsigned nparam
, unsigned dim
)
2722 return (struct isl_set
*)isl_map_extend((struct isl_map
*)base
,
2726 static struct isl_basic_map
*isl_basic_map_fix_pos(struct isl_basic_map
*bmap
,
2727 unsigned pos
, int value
)
2731 bmap
= isl_basic_map_cow(bmap
);
2732 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
2733 j
= isl_basic_map_alloc_equality(bmap
);
2736 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
2737 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
2738 isl_int_set_si(bmap
->eq
[j
][0], value
);
2739 bmap
= isl_basic_map_simplify(bmap
);
2740 return isl_basic_map_finalize(bmap
);
2742 isl_basic_map_free(bmap
);
2746 struct isl_basic_map
*isl_basic_map_fix_si(struct isl_basic_map
*bmap
,
2747 enum isl_dim_type type
, unsigned pos
, int value
)
2751 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
2752 return isl_basic_map_fix_pos(bmap
, isl_basic_map_offset(bmap
, type
) + pos
,
2755 isl_basic_map_free(bmap
);
2759 struct isl_basic_set
*isl_basic_set_fix_si(struct isl_basic_set
*bset
,
2760 enum isl_dim_type type
, unsigned pos
, int value
)
2762 return (struct isl_basic_set
*)
2763 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
2767 struct isl_basic_map
*isl_basic_map_fix_input_si(struct isl_basic_map
*bmap
,
2768 unsigned input
, int value
)
2770 return isl_basic_map_fix_si(bmap
, isl_dim_in
, input
, value
);
2773 struct isl_basic_set
*isl_basic_set_fix_dim_si(struct isl_basic_set
*bset
,
2774 unsigned dim
, int value
)
2776 return (struct isl_basic_set
*)
2777 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
2778 isl_dim_set
, dim
, value
);
2781 struct isl_map
*isl_map_fix_si(struct isl_map
*map
,
2782 enum isl_dim_type type
, unsigned pos
, int value
)
2786 map
= isl_map_cow(map
);
2790 isl_assert(ctx
, pos
< isl_map_dim(map
, type
), goto error
);
2791 for (i
= 0; i
< map
->n
; ++i
) {
2792 map
->p
[i
] = isl_basic_map_fix_si(map
->p
[i
], type
, pos
, value
);
2796 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
2803 struct isl_map
*isl_map_fix_input_si(struct isl_map
*map
,
2804 unsigned input
, int value
)
2806 return isl_map_fix_si(map
, isl_dim_in
, input
, value
);
2809 struct isl_set
*isl_set_fix_dim_si(struct isl_set
*set
, unsigned dim
, int value
)
2811 return (struct isl_set
*)
2812 isl_map_fix_si((struct isl_map
*)set
, isl_dim_set
, dim
, value
);
2815 struct isl_basic_set
*isl_basic_set_lower_bound_dim(struct isl_basic_set
*bset
,
2816 unsigned dim
, isl_int value
)
2821 bset
= isl_basic_set_cow(bset
);
2822 bset
= isl_basic_set_extend_constraints(bset
, 0, 1);
2823 j
= isl_basic_set_alloc_inequality(bset
);
2826 isl_seq_clr(bset
->ineq
[j
], 1 + isl_basic_set_total_dim(bset
));
2827 isl_int_set_si(bset
->ineq
[j
][1 + isl_basic_set_n_param(bset
) + dim
], 1);
2828 isl_int_neg(bset
->ineq
[j
][0], value
);
2829 bset
= isl_basic_set_simplify(bset
);
2830 return isl_basic_set_finalize(bset
);
2832 isl_basic_set_free(bset
);
2836 struct isl_set
*isl_set_lower_bound_dim(struct isl_set
*set
, unsigned dim
,
2841 set
= isl_set_cow(set
);
2845 isl_assert(set
->ctx
, dim
< isl_set_n_dim(set
), goto error
);
2846 for (i
= 0; i
< set
->n
; ++i
) {
2847 set
->p
[i
] = isl_basic_set_lower_bound_dim(set
->p
[i
], dim
, value
);
2857 struct isl_map
*isl_map_reverse(struct isl_map
*map
)
2862 map
= isl_map_cow(map
);
2866 map
->dim
= isl_dim_reverse(map
->dim
);
2869 for (i
= 0; i
< map
->n
; ++i
) {
2870 map
->p
[i
] = isl_basic_map_reverse(map
->p
[i
]);
2874 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
2881 struct isl_map
*isl_basic_map_lexmax(
2882 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
2883 struct isl_set
**empty
)
2885 return isl_pip_basic_map_lexmax(bmap
, dom
, empty
);
2888 struct isl_map
*isl_basic_map_lexmin(
2889 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
2890 struct isl_set
**empty
)
2892 return isl_pip_basic_map_lexmin(bmap
, dom
, empty
);
2895 struct isl_set
*isl_basic_set_lexmin(struct isl_basic_set
*bset
)
2897 struct isl_basic_map
*bmap
= NULL
;
2898 struct isl_basic_set
*dom
= NULL
;
2899 struct isl_map
*min
;
2900 struct isl_dim
*param_dim
;
2904 bmap
= isl_basic_map_from_basic_set(bset
, isl_dim_copy(bset
->dim
));
2907 param_dim
= isl_dim_domain(isl_dim_copy(bmap
->dim
));
2908 dom
= isl_basic_set_universe(param_dim
);
2911 min
= isl_basic_map_lexmin(bmap
, dom
, NULL
);
2912 return isl_map_range(min
);
2914 isl_basic_map_free(bmap
);
2918 struct isl_map
*isl_basic_map_compute_divs(struct isl_basic_map
*bmap
)
2925 off
= isl_dim_total(bmap
->dim
);
2926 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2927 if (isl_int_is_zero(bmap
->div
[i
][0]))
2928 return isl_pip_basic_map_compute_divs(bmap
);
2929 isl_assert(bmap
->ctx
, isl_int_is_zero(bmap
->div
[i
][1+1+off
+i
]),
2932 return isl_map_from_basic_map(bmap
);
2934 isl_basic_map_free(bmap
);
2938 struct isl_map
*isl_map_compute_divs(struct isl_map
*map
)
2941 struct isl_map
*res
;
2947 res
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[0]));
2948 for (i
= 1 ; i
< map
->n
; ++i
) {
2950 r2
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[i
]));
2951 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
2952 res
= isl_map_union_disjoint(res
, r2
);
2954 res
= isl_map_union(res
, r2
);
2961 struct isl_set
*isl_basic_set_compute_divs(struct isl_basic_set
*bset
)
2963 return (struct isl_set
*)
2964 isl_basic_map_compute_divs((struct isl_basic_map
*)bset
);
2967 struct isl_set
*isl_set_compute_divs(struct isl_set
*set
)
2969 return (struct isl_set
*)
2970 isl_map_compute_divs((struct isl_map
*)set
);
2973 struct isl_set
*isl_map_domain(struct isl_map
*map
)
2976 struct isl_set
*set
;
2981 map
= isl_map_cow(map
);
2985 set
= (struct isl_set
*)map
;
2986 set
->dim
= isl_dim_domain(set
->dim
);
2989 for (i
= 0; i
< map
->n
; ++i
) {
2990 set
->p
[i
] = isl_basic_map_domain(map
->p
[i
]);
2994 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
2995 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
3002 struct isl_map
*isl_map_union_disjoint(
3003 struct isl_map
*map1
, struct isl_map
*map2
)
3007 struct isl_map
*map
= NULL
;
3021 isl_assert(map1
->ctx
, isl_dim_equal(map1
->dim
, map2
->dim
), goto error
);
3023 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
3024 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
3025 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
3027 map
= isl_map_alloc_dim(isl_dim_copy(map1
->dim
),
3028 map1
->n
+ map2
->n
, flags
);
3031 for (i
= 0; i
< map1
->n
; ++i
) {
3032 map
= isl_map_add(map
,
3033 isl_basic_map_copy(map1
->p
[i
]));
3037 for (i
= 0; i
< map2
->n
; ++i
) {
3038 map
= isl_map_add(map
,
3039 isl_basic_map_copy(map2
->p
[i
]));
3053 struct isl_map
*isl_map_union(struct isl_map
*map1
, struct isl_map
*map2
)
3055 map1
= isl_map_union_disjoint(map1
, map2
);
3059 ISL_F_CLR(map1
, ISL_MAP_DISJOINT
);
3063 struct isl_set
*isl_set_union_disjoint(
3064 struct isl_set
*set1
, struct isl_set
*set2
)
3066 return (struct isl_set
*)
3067 isl_map_union_disjoint(
3068 (struct isl_map
*)set1
, (struct isl_map
*)set2
);
3071 struct isl_set
*isl_set_union(struct isl_set
*set1
, struct isl_set
*set2
)
3073 return (struct isl_set
*)
3074 isl_map_union((struct isl_map
*)set1
, (struct isl_map
*)set2
);
3077 struct isl_map
*isl_map_intersect_range(
3078 struct isl_map
*map
, struct isl_set
*set
)
3081 struct isl_map
*result
;
3087 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
) &&
3088 ISL_F_ISSET(set
, ISL_MAP_DISJOINT
))
3089 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
3091 result
= isl_map_alloc_dim(isl_dim_copy(map
->dim
),
3092 map
->n
* set
->n
, flags
);
3095 for (i
= 0; i
< map
->n
; ++i
)
3096 for (j
= 0; j
< set
->n
; ++j
) {
3097 result
= isl_map_add(result
,
3098 isl_basic_map_intersect_range(
3099 isl_basic_map_copy(map
->p
[i
]),
3100 isl_basic_set_copy(set
->p
[j
])));
3113 struct isl_map
*isl_map_intersect_domain(
3114 struct isl_map
*map
, struct isl_set
*set
)
3116 return isl_map_reverse(
3117 isl_map_intersect_range(isl_map_reverse(map
), set
));
3120 struct isl_map
*isl_map_apply_domain(
3121 struct isl_map
*map1
, struct isl_map
*map2
)
3125 map1
= isl_map_reverse(map1
);
3126 map1
= isl_map_apply_range(map1
, map2
);
3127 return isl_map_reverse(map1
);
3134 struct isl_map
*isl_map_apply_range(
3135 struct isl_map
*map1
, struct isl_map
*map2
)
3137 struct isl_dim
*dim_result
;
3138 struct isl_map
*result
;
3147 dim_result
= isl_dim_join(isl_dim_copy(map1
->dim
),
3148 isl_dim_copy(map2
->dim
));
3150 result
= isl_map_alloc_dim(dim_result
, map1
->n
* map2
->n
, 0);
3153 for (i
= 0; i
< map1
->n
; ++i
)
3154 for (j
= 0; j
< map2
->n
; ++j
) {
3155 result
= isl_map_add(result
,
3156 isl_basic_map_apply_range(
3157 isl_basic_map_copy(map1
->p
[i
]),
3158 isl_basic_map_copy(map2
->p
[j
])));
3164 if (result
&& result
->n
<= 1)
3165 ISL_F_SET(result
, ISL_MAP_DISJOINT
);
3174 * returns range - domain
3176 struct isl_basic_set
*isl_basic_map_deltas(struct isl_basic_map
*bmap
)
3178 struct isl_basic_set
*bset
;
3185 dim
= isl_basic_map_n_in(bmap
);
3186 nparam
= isl_basic_map_n_param(bmap
);
3187 isl_assert(bmap
->ctx
, dim
== isl_basic_map_n_out(bmap
), goto error
);
3188 bset
= isl_basic_set_from_basic_map(bmap
);
3189 bset
= isl_basic_set_cow(bset
);
3190 bset
= isl_basic_set_extend(bset
, nparam
, 3*dim
, 0, dim
, 0);
3191 bset
= isl_basic_set_swap_vars(bset
, 2*dim
);
3192 for (i
= 0; i
< dim
; ++i
) {
3193 int j
= isl_basic_map_alloc_equality(
3194 (struct isl_basic_map
*)bset
);
3197 isl_seq_clr(bset
->eq
[j
], 1 + isl_basic_set_total_dim(bset
));
3198 isl_int_set_si(bset
->eq
[j
][1+nparam
+i
], 1);
3199 isl_int_set_si(bset
->eq
[j
][1+nparam
+dim
+i
], 1);
3200 isl_int_set_si(bset
->eq
[j
][1+nparam
+2*dim
+i
], -1);
3202 return isl_basic_set_project_out(bset
, 2*dim
, 0);
3204 isl_basic_map_free(bmap
);
3209 * returns range - domain
3211 struct isl_set
*isl_map_deltas(struct isl_map
*map
)
3214 struct isl_set
*result
;
3219 isl_assert(map
->ctx
, isl_map_n_in(map
) == isl_map_n_out(map
), goto error
);
3220 result
= isl_set_alloc(map
->ctx
, isl_map_n_param(map
),
3221 isl_map_n_in(map
), map
->n
, map
->flags
);
3224 for (i
= 0; i
< map
->n
; ++i
)
3225 result
= isl_set_add(result
,
3226 isl_basic_map_deltas(isl_basic_map_copy(map
->p
[i
])));
3234 static struct isl_basic_map
*basic_map_identity(struct isl_dim
*dims
)
3236 struct isl_basic_map
*bmap
;
3244 nparam
= dims
->nparam
;
3246 bmap
= isl_basic_map_alloc_dim(dims
, 0, dim
, 0);
3250 for (i
= 0; i
< dim
; ++i
) {
3251 int j
= isl_basic_map_alloc_equality(bmap
);
3254 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
3255 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
3256 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], -1);
3258 return isl_basic_map_finalize(bmap
);
3260 isl_basic_map_free(bmap
);
3264 struct isl_basic_map
*isl_basic_map_identity(struct isl_dim
*set_dim
)
3266 struct isl_dim
*dim
= isl_dim_map(set_dim
);
3269 return basic_map_identity(dim
);
3272 struct isl_basic_map
*isl_basic_map_identity_like(struct isl_basic_map
*model
)
3274 if (!model
|| !model
->dim
)
3276 isl_assert(model
->ctx
,
3277 model
->dim
->n_in
== model
->dim
->n_out
, return NULL
);
3278 return basic_map_identity(isl_dim_copy(model
->dim
));
3281 static struct isl_map
*map_identity(struct isl_dim
*dim
)
3283 struct isl_map
*map
= isl_map_alloc_dim(dim
, 1, ISL_MAP_DISJOINT
);
3284 return isl_map_add(map
, basic_map_identity(isl_dim_copy(dim
)));
3287 struct isl_map
*isl_map_identity(struct isl_dim
*set_dim
)
3289 struct isl_dim
*dim
= isl_dim_map(set_dim
);
3292 return map_identity(dim
);
3295 struct isl_map
*isl_map_identity_like(struct isl_basic_map
*model
)
3297 if (!model
|| !model
->dim
)
3299 isl_assert(model
->ctx
,
3300 model
->dim
->n_in
== model
->dim
->n_out
, return NULL
);
3301 return map_identity(isl_dim_copy(model
->dim
));
3304 int isl_set_is_equal(struct isl_set
*set1
, struct isl_set
*set2
)
3306 return isl_map_is_equal((struct isl_map
*)set1
, (struct isl_map
*)set2
);
3309 int isl_set_is_subset(struct isl_set
*set1
, struct isl_set
*set2
)
3311 return isl_map_is_subset(
3312 (struct isl_map
*)set1
, (struct isl_map
*)set2
);
3315 int isl_basic_map_is_subset(
3316 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3319 struct isl_map
*map1
;
3320 struct isl_map
*map2
;
3322 if (!bmap1
|| !bmap2
)
3325 map1
= isl_map_from_basic_map(isl_basic_map_copy(bmap1
));
3326 map2
= isl_map_from_basic_map(isl_basic_map_copy(bmap2
));
3328 is_subset
= isl_map_is_subset(map1
, map2
);
3336 int isl_basic_map_is_equal(
3337 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3341 if (!bmap1
|| !bmap2
)
3343 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
3346 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
3350 int isl_basic_set_is_equal(
3351 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
3353 return isl_basic_map_is_equal(
3354 (struct isl_basic_map
*)bset1
, (struct isl_basic_map
*)bset2
);
3357 int isl_map_is_empty(struct isl_map
*map
)
3364 for (i
= 0; i
< map
->n
; ++i
) {
3365 is_empty
= isl_basic_map_is_empty(map
->p
[i
]);
3374 int isl_map_fast_is_empty(struct isl_map
*map
)
3379 int isl_set_is_empty(struct isl_set
*set
)
3381 return isl_map_is_empty((struct isl_map
*)set
);
3384 int isl_map_is_subset(struct isl_map
*map1
, struct isl_map
*map2
)
3388 struct isl_map
*diff
;
3393 if (isl_map_is_empty(map1
))
3396 if (isl_map_is_empty(map2
))
3399 diff
= isl_map_subtract(isl_map_copy(map1
), isl_map_copy(map2
));
3403 is_subset
= isl_map_is_empty(diff
);
3409 int isl_map_is_equal(struct isl_map
*map1
, struct isl_map
*map2
)
3415 is_subset
= isl_map_is_subset(map1
, map2
);
3418 is_subset
= isl_map_is_subset(map2
, map1
);
3422 int isl_basic_map_is_strict_subset(
3423 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3427 if (!bmap1
|| !bmap2
)
3429 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
3432 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
3433 if (is_subset
== -1)
3438 int isl_basic_map_is_universe(struct isl_basic_map
*bmap
)
3442 return bmap
->n_eq
== 0 && bmap
->n_ineq
== 0;
3445 int isl_basic_map_is_empty(struct isl_basic_map
*bmap
)
3447 struct isl_basic_set
*bset
= NULL
;
3448 struct isl_vec
*sample
= NULL
;
3455 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
3458 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
3459 struct isl_basic_map
*copy
= isl_basic_map_copy(bmap
);
3460 copy
= isl_basic_map_convex_hull(copy
);
3461 empty
= ISL_F_ISSET(copy
, ISL_BASIC_MAP_EMPTY
);
3462 isl_basic_map_free(copy
);
3466 total
= 1 + isl_basic_map_total_dim(bmap
);
3467 if (bmap
->sample
&& bmap
->sample
->size
== total
) {
3468 int contains
= basic_map_contains(bmap
, bmap
->sample
);
3474 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
3477 sample
= isl_basic_set_sample(bset
);
3480 empty
= sample
->size
== 0;
3482 isl_vec_free(bmap
->ctx
, bmap
->sample
);
3483 bmap
->sample
= sample
;
3488 int isl_basic_set_is_empty(struct isl_basic_set
*bset
)
3490 return isl_basic_map_is_empty((struct isl_basic_map
*)bset
);
3493 struct isl_map
*isl_basic_map_union(
3494 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3496 struct isl_map
*map
;
3497 if (!bmap1
|| !bmap2
)
3500 isl_assert(map1
->ctx
, isl_dim_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
3502 map
= isl_map_alloc_dim(isl_dim_copy(bmap1
->dim
), 2, 0);
3505 map
= isl_map_add(map
, bmap1
);
3506 map
= isl_map_add(map
, bmap2
);
3509 isl_basic_map_free(bmap1
);
3510 isl_basic_map_free(bmap2
);
3514 struct isl_set
*isl_basic_set_union(
3515 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
3517 return (struct isl_set
*)isl_basic_map_union(
3518 (struct isl_basic_map
*)bset1
,
3519 (struct isl_basic_map
*)bset2
);
3522 /* Order divs such that any div only depends on previous divs */
3523 static struct isl_basic_map
*order_divs(struct isl_basic_map
*bmap
)
3526 unsigned off
= isl_dim_total(bmap
->dim
);
3528 for (i
= 0; i
< bmap
->n_div
; ++i
) {
3530 pos
= isl_seq_first_non_zero(bmap
->div
[i
]+1+1+off
+i
,
3534 swap_div(bmap
, i
, pos
);
3540 /* Look for a div in dst that corresponds to the div "div" in src.
3541 * The divs before "div" in src and dst are assumed to be the same.
3543 * Returns -1 if no corresponding div was found and the position
3544 * of the corresponding div in dst otherwise.
3546 static int find_div(struct isl_basic_map
*dst
,
3547 struct isl_basic_map
*src
, unsigned div
)
3551 unsigned total
= isl_dim_total(src
->dim
);
3553 isl_assert(dst
->ctx
, div
<= dst
->n_div
, return -1);
3554 for (i
= div
; i
< dst
->n_div
; ++i
)
3555 if (isl_seq_eq(dst
->div
[i
], src
->div
[div
], 1+1+total
+div
) &&
3556 isl_seq_first_non_zero(dst
->div
[i
]+1+1+total
+div
,
3557 dst
->n_div
- div
) == -1)
3562 struct isl_basic_map
*isl_basic_map_align_divs(
3563 struct isl_basic_map
*dst
, struct isl_basic_map
*src
)
3566 unsigned total
= isl_dim_total(src
->dim
);
3571 if (src
->n_div
== 0)
3574 for (i
= 0; i
< src
->n_div
; ++i
)
3575 isl_assert(src
->ctx
, !isl_int_is_zero(src
->div
[i
][0]), goto error
);
3577 src
= order_divs(src
);
3578 dst
= isl_basic_map_cow(dst
);
3579 dst
= isl_basic_map_extend_dim(dst
, isl_dim_copy(dst
->dim
),
3580 src
->n_div
, 0, 2 * src
->n_div
);
3583 for (i
= 0; i
< src
->n_div
; ++i
) {
3584 int j
= find_div(dst
, src
, i
);
3586 j
= isl_basic_map_alloc_div(dst
);
3589 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
+i
);
3590 isl_seq_clr(dst
->div
[j
]+1+1+total
+i
, dst
->n_div
- i
);
3591 if (add_div_constraints(dst
, j
) < 0)
3595 swap_div(dst
, i
, j
);
3599 isl_basic_map_free(dst
);
3603 struct isl_basic_set
*isl_basic_set_align_divs(
3604 struct isl_basic_set
*dst
, struct isl_basic_set
*src
)
3606 return (struct isl_basic_set
*)isl_basic_map_align_divs(
3607 (struct isl_basic_map
*)dst
, (struct isl_basic_map
*)src
);
3610 struct isl_map
*isl_map_align_divs(struct isl_map
*map
)
3614 map
= isl_map_compute_divs(map
);
3615 map
= isl_map_cow(map
);
3619 for (i
= 1; i
< map
->n
; ++i
)
3620 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], map
->p
[i
]);
3621 for (i
= 1; i
< map
->n
; ++i
)
3622 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], map
->p
[0]);
3624 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3628 struct isl_set
*isl_set_align_divs(struct isl_set
*set
)
3630 return (struct isl_set
*)isl_map_align_divs((struct isl_map
*)set
);
3633 static struct isl_map
*add_cut_constraint(struct isl_map
*dst
,
3634 struct isl_basic_map
*src
, isl_int
*c
,
3635 unsigned len
, int oppose
)
3637 struct isl_basic_map
*copy
= NULL
;
3642 copy
= isl_basic_map_copy(src
);
3643 copy
= isl_basic_map_cow(copy
);
3646 copy
= isl_basic_map_extend_constraints(copy
, 0, 1);
3647 k
= isl_basic_map_alloc_inequality(copy
);
3651 isl_seq_neg(copy
->ineq
[k
], c
, len
);
3653 isl_seq_cpy(copy
->ineq
[k
], c
, len
);
3654 total
= 1 + isl_basic_map_total_dim(copy
);
3655 isl_seq_clr(copy
->ineq
[k
]+len
, total
- len
);
3656 isl_inequality_negate(copy
, k
);
3657 copy
= isl_basic_map_simplify(copy
);
3658 copy
= isl_basic_map_finalize(copy
);
3659 is_empty
= isl_basic_map_is_empty(copy
);
3663 dst
= isl_map_add(dst
, copy
);
3665 isl_basic_map_free(copy
);
3668 isl_basic_map_free(copy
);
3673 static struct isl_map
*subtract(struct isl_map
*map
, struct isl_basic_map
*bmap
)
3677 struct isl_map
*rest
= NULL
;
3679 unsigned total
= isl_basic_map_total_dim(bmap
);
3686 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
3687 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
3689 max
= map
->n
* (2 * bmap
->n_eq
+ bmap
->n_ineq
);
3690 rest
= isl_map_alloc_dim(isl_dim_copy(map
->dim
), max
, flags
);
3694 for (i
= 0; i
< map
->n
; ++i
) {
3695 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], bmap
);
3700 for (j
= 0; j
< map
->n
; ++j
)
3701 map
->p
[j
] = isl_basic_map_cow(map
->p
[j
]);
3703 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
3704 for (j
= 0; j
< map
->n
; ++j
) {
3705 rest
= add_cut_constraint(rest
,
3706 map
->p
[j
], bmap
->eq
[i
], 1+total
, 0);
3710 rest
= add_cut_constraint(rest
,
3711 map
->p
[j
], bmap
->eq
[i
], 1+total
, 1);
3715 map
->p
[j
] = isl_basic_map_extend_constraints(map
->p
[j
],
3719 k
= isl_basic_map_alloc_equality(map
->p
[j
]);
3722 isl_seq_cpy(map
->p
[j
]->eq
[k
], bmap
->eq
[i
], 1+total
);
3723 isl_seq_clr(map
->p
[j
]->eq
[k
]+1+total
,
3724 map
->p
[j
]->n_div
- bmap
->n_div
);
3728 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
3729 for (j
= 0; j
< map
->n
; ++j
) {
3730 rest
= add_cut_constraint(rest
,
3731 map
->p
[j
], bmap
->ineq
[i
], 1+total
, 0);
3735 map
->p
[j
] = isl_basic_map_extend_constraints(map
->p
[j
],
3739 k
= isl_basic_map_alloc_inequality(map
->p
[j
]);
3742 isl_seq_cpy(map
->p
[j
]->ineq
[k
], bmap
->ineq
[i
], 1+total
);
3743 isl_seq_clr(map
->p
[j
]->ineq
[k
]+1+total
,
3744 map
->p
[j
]->n_div
- bmap
->n_div
);
3756 struct isl_map
*isl_map_subtract(struct isl_map
*map1
, struct isl_map
*map2
)
3762 isl_assert(map1
->ctx
, isl_dim_equal(map1
->dim
, map2
->dim
), goto error
);
3764 if (isl_map_is_empty(map2
)) {
3769 map1
= isl_map_compute_divs(map1
);
3770 map2
= isl_map_compute_divs(map2
);
3774 for (i
= 0; map1
&& i
< map2
->n
; ++i
)
3775 map1
= subtract(map1
, map2
->p
[i
]);
3785 struct isl_set
*isl_set_subtract(struct isl_set
*set1
, struct isl_set
*set2
)
3787 return (struct isl_set
*)
3789 (struct isl_map
*)set1
, (struct isl_map
*)set2
);
3792 struct isl_set
*isl_set_apply(struct isl_set
*set
, struct isl_map
*map
)
3796 isl_assert(set
->ctx
, isl_map_compatible_domain(map
, set
), goto error
);
3797 map
= isl_map_intersect_domain(map
, set
);
3798 set
= isl_map_range(map
);
3806 /* There is no need to cow as removing empty parts doesn't change
3807 * the meaning of the set.
3809 struct isl_map
*isl_map_remove_empty_parts(struct isl_map
*map
)
3816 for (i
= map
->n
-1; i
>= 0; --i
) {
3817 if (!ISL_F_ISSET(map
->p
[i
], ISL_BASIC_MAP_EMPTY
))
3819 isl_basic_map_free(map
->p
[i
]);
3820 if (i
!= map
->n
-1) {
3821 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3822 map
->p
[i
] = map
->p
[map
->n
-1];
3830 struct isl_set
*isl_set_remove_empty_parts(struct isl_set
*set
)
3832 return (struct isl_set
*)
3833 isl_map_remove_empty_parts((struct isl_map
*)set
);
3836 struct isl_basic_map
*isl_map_copy_basic_map(struct isl_map
*map
)
3838 struct isl_basic_map
*bmap
;
3839 if (!map
|| map
->n
== 0)
3841 bmap
= map
->p
[map
->n
-1];
3842 isl_assert(map
->ctx
, ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
), return NULL
);
3843 return isl_basic_map_copy(bmap
);
3846 struct isl_basic_set
*isl_set_copy_basic_set(struct isl_set
*set
)
3848 (struct isl_basic_set
*)isl_map_copy_basic_map((struct isl_map
*)set
);
3851 struct isl_map
*isl_map_drop_basic_map(struct isl_map
*map
,
3852 struct isl_basic_map
*bmap
)
3858 for (i
= map
->n
-1; i
>= 0; --i
) {
3859 if (map
->p
[i
] != bmap
)
3861 map
= isl_map_cow(map
);
3864 isl_basic_map_free(map
->p
[i
]);
3865 if (i
!= map
->n
-1) {
3866 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
3867 map
->p
[i
] = map
->p
[map
->n
-1];
3872 isl_basic_map_free(bmap
);
3876 isl_basic_map_free(bmap
);
3880 struct isl_set
*isl_set_drop_basic_set(struct isl_set
*set
,
3881 struct isl_basic_set
*bset
)
3883 (struct isl_set
*)isl_map_drop_basic_map((struct isl_map
*)set
,
3884 (struct isl_basic_map
*)bset
);
3887 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
3888 * for any common value of the parameters and dimensions preceding dim
3889 * in both basic sets, the values of dimension pos in bset1 are
3890 * smaller or larger than those in bset2.
3893 * 1 if bset1 follows bset2
3894 * -1 if bset1 precedes bset2
3895 * 0 if bset1 and bset2 are incomparable
3896 * -2 if some error occurred.
3898 int isl_basic_set_compare_at(struct isl_basic_set
*bset1
,
3899 struct isl_basic_set
*bset2
, int pos
)
3901 struct isl_dim
*dims
;
3902 struct isl_basic_map
*bmap1
= NULL
;
3903 struct isl_basic_map
*bmap2
= NULL
;
3904 struct isl_ctx
*ctx
;
3905 struct isl_vec
*obj
;
3908 unsigned dim1
, dim2
;
3910 enum isl_lp_result res
;
3913 if (!bset1
|| !bset2
)
3916 nparam
= isl_basic_set_n_param(bset1
);
3917 dim1
= isl_basic_set_n_dim(bset1
);
3918 dim2
= isl_basic_set_n_dim(bset2
);
3919 dims
= isl_dim_alloc(bset1
->ctx
, nparam
, pos
, dim1
- pos
);
3920 bmap1
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset1
), dims
);
3921 dims
= isl_dim_alloc(bset2
->ctx
, nparam
, pos
, dim2
- pos
);
3922 bmap2
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset2
), dims
);
3923 if (!bmap1
|| !bmap2
)
3925 bmap1
= isl_basic_map_cow(bmap1
);
3926 bmap1
= isl_basic_map_extend(bmap1
, nparam
,
3927 pos
, (dim1
- pos
) + (dim2
- pos
),
3928 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
3929 bmap1
= add_constraints(bmap1
, bmap2
, 0, dim1
- pos
);
3932 total
= isl_basic_map_total_dim(bmap1
);
3934 obj
= isl_vec_alloc(ctx
, 1 + total
);
3935 isl_seq_clr(obj
->block
.data
, 1 + total
);
3936 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
], 1);
3937 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
+(dim1
-pos
)], -1);
3942 res
= isl_solve_lp(bmap1
, 0, obj
->block
.data
, ctx
->one
, &num
, &den
);
3943 if (res
== isl_lp_empty
)
3945 else if (res
== isl_lp_ok
&& isl_int_is_pos(num
))
3947 else if ((res
== isl_lp_ok
&& isl_int_is_neg(num
)) ||
3948 res
== isl_lp_unbounded
)
3954 isl_basic_map_free(bmap1
);
3955 isl_vec_free(ctx
, obj
);
3958 isl_basic_map_free(bmap1
);
3959 isl_basic_map_free(bmap2
);
3963 static int isl_basic_map_fast_has_fixed_var(struct isl_basic_map
*bmap
,
3964 unsigned pos
, isl_int
*val
)
3972 total
= isl_basic_map_total_dim(bmap
);
3973 for (i
= 0, d
= total
-1; i
< bmap
->n_eq
&& d
+1 > pos
; ++i
) {
3974 for (; d
+1 > pos
; --d
)
3975 if (!isl_int_is_zero(bmap
->eq
[i
][1+d
]))
3979 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1, d
) != -1)
3981 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1+d
+1, total
-d
-1) != -1)
3983 if (!isl_int_is_one(bmap
->eq
[i
][1+d
]))
3986 isl_int_neg(*val
, bmap
->eq
[i
][0]);
3992 static int isl_map_fast_has_fixed_var(struct isl_map
*map
,
3993 unsigned pos
, isl_int
*val
)
4005 return isl_basic_map_fast_has_fixed_var(map
->p
[0], pos
, val
);
4008 fixed
= isl_basic_map_fast_has_fixed_var(map
->p
[0], pos
, &v
);
4009 for (i
= 1; fixed
== 1 && i
< map
->n
; ++i
) {
4010 fixed
= isl_basic_map_fast_has_fixed_var(map
->p
[i
], pos
, &tmp
);
4011 if (fixed
== 1 && isl_int_ne(tmp
, v
))
4015 isl_int_set(*val
, v
);
4021 static int isl_set_fast_has_fixed_var(struct isl_set
*set
, unsigned pos
,
4024 return isl_map_fast_has_fixed_var((struct isl_map
*)set
, pos
, val
);
4027 int isl_basic_map_fast_is_fixed(struct isl_basic_map
*bmap
,
4028 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
4030 if (pos
>= isl_basic_map_dim(bmap
, type
))
4032 return isl_basic_map_fast_has_fixed_var(bmap
,
4033 isl_basic_map_offset(bmap
, type
) - 1 + pos
, val
);
4036 /* Check if dimension dim has fixed value and if so and if val is not NULL,
4037 * then return this fixed value in *val.
4039 int isl_set_fast_dim_is_fixed(struct isl_set
*set
, unsigned dim
, isl_int
*val
)
4041 return isl_set_fast_has_fixed_var(set
, isl_set_n_param(set
) + dim
, val
);
4044 /* Check if input variable in has fixed value and if so and if val is not NULL,
4045 * then return this fixed value in *val.
4047 int isl_map_fast_input_is_fixed(struct isl_map
*map
, unsigned in
, isl_int
*val
)
4049 return isl_map_fast_has_fixed_var(map
, isl_map_n_param(map
) + in
, val
);
4052 /* Check if dimension dim has an (obvious) fixed lower bound and if so
4053 * and if val is not NULL, then return this lower bound in *val.
4055 int isl_basic_set_fast_dim_has_fixed_lower_bound(struct isl_basic_set
*bset
,
4056 unsigned dim
, isl_int
*val
)
4058 int i
, i_eq
= -1, i_ineq
= -1;
4065 total
= isl_basic_set_total_dim(bset
);
4066 nparam
= isl_basic_set_n_param(bset
);
4067 for (i
= 0; i
< bset
->n_eq
; ++i
) {
4068 if (isl_int_is_zero(bset
->eq
[i
][1+nparam
+dim
]))
4074 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
4075 if (!isl_int_is_pos(bset
->ineq
[i
][1+nparam
+dim
]))
4077 if (i_eq
!= -1 || i_ineq
!= -1)
4081 if (i_eq
== -1 && i_ineq
== -1)
4083 c
= i_eq
!= -1 ? bset
->eq
[i_eq
] : bset
->ineq
[i_ineq
];
4084 /* The coefficient should always be one due to normalization. */
4085 if (!isl_int_is_one(c
[1+nparam
+dim
]))
4087 if (isl_seq_first_non_zero(c
+1, nparam
+dim
) != -1)
4089 if (isl_seq_first_non_zero(c
+1+nparam
+dim
+1,
4090 total
- nparam
- dim
- 1) != -1)
4093 isl_int_neg(*val
, c
[0]);
4097 int isl_set_fast_dim_has_fixed_lower_bound(struct isl_set
*set
,
4098 unsigned dim
, isl_int
*val
)
4110 return isl_basic_set_fast_dim_has_fixed_lower_bound(set
->p
[0],
4114 fixed
= isl_basic_set_fast_dim_has_fixed_lower_bound(set
->p
[0],
4116 for (i
= 1; fixed
== 1 && i
< set
->n
; ++i
) {
4117 fixed
= isl_basic_set_fast_dim_has_fixed_lower_bound(set
->p
[i
],
4119 if (fixed
== 1 && isl_int_ne(tmp
, v
))
4123 isl_int_set(*val
, v
);
4134 static int qsort_constraint_cmp(const void *p1
, const void *p2
)
4136 const struct constraint
*c1
= (const struct constraint
*)p1
;
4137 const struct constraint
*c2
= (const struct constraint
*)p2
;
4138 unsigned size
= isl_min(c1
->size
, c2
->size
);
4139 return isl_seq_cmp(c1
->c
, c2
->c
, size
);
4142 static struct isl_basic_map
*isl_basic_map_sort_constraints(
4143 struct isl_basic_map
*bmap
)
4146 struct constraint
*c
;
4151 total
= isl_basic_map_total_dim(bmap
);
4152 c
= isl_alloc_array(bmap
->ctx
, struct constraint
, bmap
->n_ineq
);
4155 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
4157 c
[i
].c
= bmap
->ineq
[i
];
4159 qsort(c
, bmap
->n_ineq
, sizeof(struct constraint
), qsort_constraint_cmp
);
4160 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
4161 bmap
->ineq
[i
] = c
[i
].c
;
4165 isl_basic_map_free(bmap
);
4169 struct isl_basic_map
*isl_basic_map_normalize(struct isl_basic_map
*bmap
)
4173 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED
))
4175 bmap
= isl_basic_map_convex_hull(bmap
);
4176 bmap
= isl_basic_map_sort_constraints(bmap
);
4177 ISL_F_SET(bmap
, ISL_BASIC_MAP_NORMALIZED
);
4181 struct isl_basic_set
*isl_basic_set_normalize(struct isl_basic_set
*bset
)
4183 return (struct isl_basic_set
*)isl_basic_map_normalize(
4184 (struct isl_basic_map
*)bset
);
4187 static int isl_basic_map_fast_cmp(const struct isl_basic_map
*bmap1
,
4188 const struct isl_basic_map
*bmap2
)
4195 if (isl_basic_map_n_param(bmap1
) != isl_basic_map_n_param(bmap2
))
4196 return isl_basic_map_n_param(bmap1
) - isl_basic_map_n_param(bmap2
);
4197 if (isl_basic_map_n_in(bmap1
) != isl_basic_map_n_in(bmap2
))
4198 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
4199 if (isl_basic_map_n_out(bmap1
) != isl_basic_map_n_out(bmap2
))
4200 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
4201 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
) &&
4202 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
4204 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
))
4206 if (ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
4208 if (bmap1
->n_eq
!= bmap2
->n_eq
)
4209 return bmap1
->n_eq
- bmap2
->n_eq
;
4210 if (bmap1
->n_ineq
!= bmap2
->n_ineq
)
4211 return bmap1
->n_ineq
- bmap2
->n_ineq
;
4212 if (bmap1
->n_div
!= bmap2
->n_div
)
4213 return bmap1
->n_div
- bmap2
->n_div
;
4214 total
= isl_basic_map_total_dim(bmap1
);
4215 for (i
= 0; i
< bmap1
->n_eq
; ++i
) {
4216 cmp
= isl_seq_cmp(bmap1
->eq
[i
], bmap2
->eq
[i
], 1+total
);
4220 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
4221 cmp
= isl_seq_cmp(bmap1
->ineq
[i
], bmap2
->ineq
[i
], 1+total
);
4225 for (i
= 0; i
< bmap1
->n_div
; ++i
) {
4226 cmp
= isl_seq_cmp(bmap1
->div
[i
], bmap2
->div
[i
], 1+1+total
);
4233 static int isl_basic_map_fast_is_equal(struct isl_basic_map
*bmap1
,
4234 struct isl_basic_map
*bmap2
)
4236 return isl_basic_map_fast_cmp(bmap1
, bmap2
) == 0;
4239 static int qsort_bmap_cmp(const void *p1
, const void *p2
)
4241 const struct isl_basic_map
*bmap1
= *(const struct isl_basic_map
**)p1
;
4242 const struct isl_basic_map
*bmap2
= *(const struct isl_basic_map
**)p2
;
4244 return isl_basic_map_fast_cmp(bmap1
, bmap2
);
4247 /* We normalize in place, but if anything goes wrong we need
4248 * to return NULL, so we need to make sure we don't change the
4249 * meaning of any possible other copies of map.
4251 struct isl_map
*isl_map_normalize(struct isl_map
*map
)
4254 struct isl_basic_map
*bmap
;
4258 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
))
4260 for (i
= 0; i
< map
->n
; ++i
) {
4261 bmap
= isl_basic_map_normalize(isl_basic_map_copy(map
->p
[i
]));
4264 isl_basic_map_free(map
->p
[i
]);
4267 qsort(map
->p
, map
->n
, sizeof(struct isl_basic_map
*), qsort_bmap_cmp
);
4268 ISL_F_SET(map
, ISL_MAP_NORMALIZED
);
4269 map
= isl_map_remove_empty_parts(map
);
4272 for (i
= map
->n
- 1; i
>= 1; --i
) {
4273 if (!isl_basic_map_fast_is_equal(map
->p
[i
-1], map
->p
[i
]))
4275 isl_basic_map_free(map
->p
[i
-1]);
4276 for (j
= i
; j
< map
->n
; ++j
)
4277 map
->p
[j
-1] = map
->p
[j
];
4287 struct isl_set
*isl_set_normalize(struct isl_set
*set
)
4289 return (struct isl_set
*)isl_map_normalize((struct isl_map
*)set
);
4292 int isl_map_fast_is_equal(struct isl_map
*map1
, struct isl_map
*map2
)
4302 if (!isl_dim_equal(map1
->dim
, map2
->dim
))
4305 map1
= isl_map_copy(map1
);
4306 map2
= isl_map_copy(map2
);
4307 map1
= isl_map_normalize(map1
);
4308 map2
= isl_map_normalize(map2
);
4311 equal
= map1
->n
== map2
->n
;
4312 for (i
= 0; equal
&& i
< map1
->n
; ++i
) {
4313 equal
= isl_basic_map_fast_is_equal(map1
->p
[i
], map2
->p
[i
]);
4326 int isl_set_fast_is_equal(struct isl_set
*set1
, struct isl_set
*set2
)
4328 return isl_map_fast_is_equal((struct isl_map
*)set1
,
4329 (struct isl_map
*)set2
);
4332 /* Return an interval that ranges from min to max (inclusive)
4334 struct isl_basic_set
*isl_basic_set_interval(struct isl_ctx
*ctx
,
4335 isl_int min
, isl_int max
)
4338 struct isl_basic_set
*bset
= NULL
;
4340 bset
= isl_basic_set_alloc(ctx
, 0, 1, 0, 0, 2);
4344 k
= isl_basic_set_alloc_inequality(bset
);
4347 isl_int_set_si(bset
->ineq
[k
][1], 1);
4348 isl_int_neg(bset
->ineq
[k
][0], min
);
4350 k
= isl_basic_set_alloc_inequality(bset
);
4353 isl_int_set_si(bset
->ineq
[k
][1], -1);
4354 isl_int_set(bset
->ineq
[k
][0], max
);
4358 isl_basic_set_free(bset
);
4362 /* Return the Cartesian product of the basic sets in list (in the given order).
4364 struct isl_basic_set
*isl_basic_set_product(struct isl_basic_set_list
*list
)
4372 struct isl_basic_set
*product
= NULL
;
4376 isl_assert(list
->ctx
, list
->n
> 0, goto error
);
4377 isl_assert(list
->ctx
, list
->p
[0], goto error
);
4378 nparam
= isl_basic_set_n_param(list
->p
[0]);
4379 dim
= isl_basic_set_n_dim(list
->p
[0]);
4380 extra
= list
->p
[0]->n_div
;
4381 n_eq
= list
->p
[0]->n_eq
;
4382 n_ineq
= list
->p
[0]->n_ineq
;
4383 for (i
= 1; i
< list
->n
; ++i
) {
4384 isl_assert(list
->ctx
, list
->p
[i
], goto error
);
4385 isl_assert(list
->ctx
,
4386 nparam
== isl_basic_set_n_param(list
->p
[i
]), goto error
);
4387 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
4388 extra
+= list
->p
[i
]->n_div
;
4389 n_eq
+= list
->p
[i
]->n_eq
;
4390 n_ineq
+= list
->p
[i
]->n_ineq
;
4392 product
= isl_basic_set_alloc(list
->ctx
, nparam
, dim
, extra
,
4397 for (i
= 0; i
< list
->n
; ++i
) {
4398 isl_basic_set_add_constraints(product
,
4399 isl_basic_set_copy(list
->p
[i
]), dim
);
4400 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
4402 isl_basic_set_list_free(list
);
4405 isl_basic_set_free(product
);
4406 isl_basic_set_list_free(list
);
4410 struct isl_basic_map
*isl_basic_map_product(
4411 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
4413 struct isl_dim
*dim_result
= NULL
;
4414 struct isl_basic_map
*bmap
;
4415 unsigned in1
, in2
, out1
, out2
, nparam
, total
, pos
;
4416 struct isl_dim_map
*dim_map1
, *dim_map2
;
4418 if (!bmap1
|| !bmap2
)
4421 isl_assert(map1
->ctx
, isl_dim_match(bmap1
->dim
, isl_dim_param
,
4422 bmap2
->dim
, isl_dim_param
), goto error
);
4423 dim_result
= isl_dim_product(isl_dim_copy(bmap1
->dim
),
4424 isl_dim_copy(bmap2
->dim
));
4426 in1
= isl_basic_map_n_in(bmap1
);
4427 in2
= isl_basic_map_n_in(bmap2
);
4428 out1
= isl_basic_map_n_out(bmap1
);
4429 out2
= isl_basic_map_n_out(bmap2
);
4430 nparam
= isl_basic_map_n_param(bmap1
);
4432 total
= nparam
+ in1
+ in2
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
4433 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4434 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4435 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
4436 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
4437 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
4438 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
4439 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
4440 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
4441 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
4442 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
4444 bmap
= isl_basic_map_alloc_dim(dim_result
,
4445 bmap1
->n_div
+ bmap2
->n_div
,
4446 bmap1
->n_eq
+ bmap2
->n_eq
,
4447 bmap1
->n_ineq
+ bmap2
->n_ineq
);
4448 bmap
= add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
4449 bmap
= add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
4450 bmap
= isl_basic_map_simplify(bmap
);
4451 return isl_basic_map_finalize(bmap
);
4453 isl_basic_map_free(bmap1
);
4454 isl_basic_map_free(bmap2
);
4458 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
4460 struct isl_map
*isl_map_product(struct isl_map
*map1
, struct isl_map
*map2
)
4463 struct isl_map
*result
;
4469 isl_assert(map1
->ctx
, isl_dim_match(map1
->dim
, isl_dim_param
,
4470 map2
->dim
, isl_dim_param
), goto error
);
4472 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
4473 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
4474 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
4476 result
= isl_map_alloc_dim(isl_dim_product(isl_dim_copy(map1
->dim
),
4477 isl_dim_copy(map2
->dim
)),
4478 map1
->n
* map2
->n
, flags
);
4481 for (i
= 0; i
< map1
->n
; ++i
)
4482 for (j
= 0; j
< map2
->n
; ++j
) {
4483 struct isl_basic_map
*part
;
4484 part
= isl_basic_map_product(
4485 isl_basic_map_copy(map1
->p
[i
]),
4486 isl_basic_map_copy(map2
->p
[j
]));
4487 if (isl_basic_map_is_empty(part
))
4488 isl_basic_map_free(part
);
4490 result
= isl_map_add(result
, part
);
4503 uint32_t isl_basic_set_get_hash(struct isl_basic_set
*bset
)
4511 bset
= isl_basic_set_copy(bset
);
4512 bset
= isl_basic_set_normalize(bset
);
4515 total
= isl_basic_set_total_dim(bset
);
4516 isl_hash_byte(hash
, bset
->n_eq
& 0xFF);
4517 for (i
= 0; i
< bset
->n_eq
; ++i
) {
4519 c_hash
= isl_seq_get_hash(bset
->eq
[i
], 1 + total
);
4520 isl_hash_hash(hash
, c_hash
);
4522 isl_hash_byte(hash
, bset
->n_ineq
& 0xFF);
4523 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
4525 c_hash
= isl_seq_get_hash(bset
->ineq
[i
], 1 + total
);
4526 isl_hash_hash(hash
, c_hash
);
4528 isl_hash_byte(hash
, bset
->n_div
& 0xFF);
4529 for (i
= 0; i
< bset
->n_div
; ++i
) {
4531 if (isl_int_is_zero(bset
->div
[i
][0]))
4533 isl_hash_byte(hash
, i
& 0xFF);
4534 c_hash
= isl_seq_get_hash(bset
->div
[i
], 1 + 1 + total
);
4535 isl_hash_hash(hash
, c_hash
);
4537 isl_basic_set_free(bset
);
4541 uint32_t isl_set_get_hash(struct isl_set
*set
)
4548 set
= isl_set_copy(set
);
4549 set
= isl_set_normalize(set
);
4553 hash
= isl_hash_init();
4554 for (i
= 0; i
< set
->n
; ++i
) {
4556 bset_hash
= isl_basic_set_get_hash(set
->p
[i
]);
4557 isl_hash_hash(hash
, bset_hash
);
4565 /* Check if the value for dimension dim is completely determined
4566 * by the values of the other parameters and variables.
4567 * That is, check if dimension dim is involved in an equality.
4569 int isl_basic_set_dim_is_unique(struct isl_basic_set
*bset
, unsigned dim
)
4576 nparam
= isl_basic_set_n_param(bset
);
4577 for (i
= 0; i
< bset
->n_eq
; ++i
)
4578 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ dim
]))
4583 /* Check if the value for dimension dim is completely determined
4584 * by the values of the other parameters and variables.
4585 * That is, check if dimension dim is involved in an equality
4586 * for each of the subsets.
4588 int isl_set_dim_is_unique(struct isl_set
*set
, unsigned dim
)
4594 for (i
= 0; i
< set
->n
; ++i
) {
4596 unique
= isl_basic_set_dim_is_unique(set
->p
[i
], dim
);