6 #include "isl_equalities.h"
12 #include "isl_map_private.h"
13 #include "isl_map_piplib.h"
14 #include "isl_sample.h"
18 /* Maps dst positions to src positions */
24 static struct isl_dim_map
*isl_dim_map_alloc(struct isl_ctx
*ctx
, unsigned len
)
27 struct isl_dim_map
*dim_map
;
28 dim_map
= isl_alloc(ctx
, struct isl_dim_map
,
29 sizeof(struct isl_dim_map
) + len
* sizeof(int));
32 dim_map
->len
= 1 + len
;
34 for (i
= 0; i
< len
; ++i
)
35 dim_map
->pos
[1 + i
] = -1;
39 static unsigned n(struct isl_dim
*dim
, enum isl_dim_type type
)
42 case isl_dim_param
: return dim
->nparam
;
43 case isl_dim_in
: return dim
->n_in
;
44 case isl_dim_out
: return dim
->n_out
;
45 case isl_dim_all
: return dim
->nparam
+ dim
->n_in
+ dim
->n_out
;
49 static unsigned pos(struct isl_dim
*dim
, enum isl_dim_type type
)
52 case isl_dim_param
: return 1;
53 case isl_dim_in
: return 1 + dim
->nparam
;
54 case isl_dim_out
: return 1 + dim
->nparam
+ dim
->n_in
;
58 static void isl_dim_map_dim(struct isl_dim_map
*dim_map
, struct isl_dim
*dim
,
59 enum isl_dim_type type
, unsigned dst_pos
)
67 src_pos
= pos(dim
, type
);
68 for (i
= 0; i
< n(dim
, type
); ++i
)
69 dim_map
->pos
[1 + dst_pos
+ i
] = src_pos
+ i
;
72 static void isl_dim_map_div(struct isl_dim_map
*dim_map
,
73 struct isl_basic_map
*bmap
, unsigned dst_pos
)
78 if (!dim_map
|| !bmap
)
81 src_pos
= 1 + isl_dim_total(bmap
->dim
);
82 for (i
= 0; i
< bmap
->n_div
; ++i
)
83 dim_map
->pos
[1 + dst_pos
+ i
] = src_pos
+ i
;
86 static void isl_dim_map_dump(struct isl_dim_map
*dim_map
)
90 for (i
= 0; i
< dim_map
->len
; ++i
)
91 fprintf(stderr
, "%d -> %d; ", i
, dim_map
->pos
[i
]);
92 fprintf(stderr
, "\n");
95 unsigned isl_basic_map_dim(const struct isl_basic_map
*bmap
,
96 enum isl_dim_type type
)
101 case isl_dim_out
: return isl_dim_size(bmap
->dim
, type
);
102 case isl_dim_div
: return bmap
->n_div
;
103 case isl_dim_all
: return isl_basic_map_total_dim(bmap
);
107 unsigned isl_map_dim(const struct isl_map
*map
, enum isl_dim_type type
)
109 return n(map
->dim
, type
);
112 unsigned isl_set_dim(const struct isl_set
*set
, enum isl_dim_type type
)
114 return n(set
->dim
, type
);
117 unsigned isl_basic_map_offset(struct isl_basic_map
*bmap
,
118 enum isl_dim_type type
)
120 struct isl_dim
*dim
= bmap
->dim
;
122 case isl_dim_param
: return 1;
123 case isl_dim_in
: return 1 + dim
->nparam
;
124 case isl_dim_out
: return 1 + dim
->nparam
+ dim
->n_in
;
125 case isl_dim_div
: return 1 + dim
->nparam
+ dim
->n_in
+ dim
->n_out
;
129 static unsigned map_offset(struct isl_map
*map
, enum isl_dim_type type
)
131 return pos(map
->dim
, type
);
134 unsigned isl_basic_set_dim(const struct isl_basic_set
*bset
,
135 enum isl_dim_type type
)
137 return isl_basic_map_dim((const struct isl_basic_map
*)bset
, type
);
140 unsigned isl_basic_set_n_dim(const struct isl_basic_set
*bset
)
142 return bset
->dim
->n_out
;
145 unsigned isl_basic_set_n_param(const struct isl_basic_set
*bset
)
147 return bset
->dim
->nparam
;
150 unsigned isl_basic_set_total_dim(const struct isl_basic_set
*bset
)
152 return isl_dim_total(bset
->dim
) + bset
->n_div
;
155 unsigned isl_set_n_dim(const struct isl_set
*set
)
157 return set
->dim
->n_out
;
160 unsigned isl_set_n_param(const struct isl_set
*set
)
162 return set
->dim
->nparam
;
165 unsigned isl_basic_map_n_in(const struct isl_basic_map
*bmap
)
167 return bmap
->dim
->n_in
;
170 unsigned isl_basic_map_n_out(const struct isl_basic_map
*bmap
)
172 return bmap
->dim
->n_out
;
175 unsigned isl_basic_map_n_param(const struct isl_basic_map
*bmap
)
177 return bmap
->dim
->nparam
;
180 unsigned isl_basic_map_n_div(const struct isl_basic_map
*bmap
)
185 unsigned isl_basic_map_total_dim(const struct isl_basic_map
*bmap
)
187 return isl_dim_total(bmap
->dim
) + bmap
->n_div
;
190 unsigned isl_map_n_in(const struct isl_map
*map
)
192 return map
->dim
->n_in
;
195 unsigned isl_map_n_out(const struct isl_map
*map
)
197 return map
->dim
->n_out
;
200 unsigned isl_map_n_param(const struct isl_map
*map
)
202 return map
->dim
->nparam
;
205 int isl_map_compatible_domain(struct isl_map
*map
, struct isl_set
*set
)
207 return map
->dim
->n_in
== set
->dim
->n_out
&&
208 map
->dim
->nparam
== set
->dim
->nparam
;
211 int isl_basic_map_compatible_domain(struct isl_basic_map
*bmap
,
212 struct isl_basic_set
*bset
)
214 return bmap
->dim
->n_in
== bset
->dim
->n_out
&&
215 bmap
->dim
->nparam
== bset
->dim
->nparam
;
218 int isl_basic_map_compatible_range(struct isl_basic_map
*bmap
,
219 struct isl_basic_set
*bset
)
221 return bmap
->dim
->n_out
== bset
->dim
->n_out
&&
222 bmap
->dim
->nparam
== bset
->dim
->nparam
;
225 struct isl_dim
*isl_basic_map_get_dim(struct isl_basic_map
*bmap
)
229 return isl_dim_copy(bmap
->dim
);
232 struct isl_dim
*isl_basic_set_get_dim(struct isl_basic_set
*bset
)
236 return isl_dim_copy(bset
->dim
);
239 struct isl_dim
*isl_map_get_dim(struct isl_map
*map
)
243 return isl_dim_copy(map
->dim
);
246 struct isl_dim
*isl_set_get_dim(struct isl_set
*set
)
250 return isl_dim_copy(set
->dim
);
253 static struct isl_basic_map
*basic_map_init(struct isl_ctx
*ctx
,
254 struct isl_basic_map
*bmap
, unsigned extra
,
255 unsigned n_eq
, unsigned n_ineq
)
258 size_t row_size
= 1 + isl_dim_total(bmap
->dim
) + extra
;
260 bmap
->block
= isl_blk_alloc(ctx
, (n_ineq
+ n_eq
) * row_size
);
261 if (isl_blk_is_error(bmap
->block
)) {
266 bmap
->ineq
= isl_alloc_array(ctx
, isl_int
*, n_ineq
+ n_eq
);
268 isl_blk_free(ctx
, bmap
->block
);
274 bmap
->block2
= isl_blk_empty();
277 bmap
->block2
= isl_blk_alloc(ctx
, extra
* (1 + row_size
));
278 if (isl_blk_is_error(bmap
->block2
)) {
280 isl_blk_free(ctx
, bmap
->block
);
285 bmap
->div
= isl_alloc_array(ctx
, isl_int
*, extra
);
287 isl_blk_free(ctx
, bmap
->block2
);
289 isl_blk_free(ctx
, bmap
->block
);
295 for (i
= 0; i
< n_ineq
+ n_eq
; ++i
)
296 bmap
->ineq
[i
] = bmap
->block
.data
+ i
* row_size
;
298 for (i
= 0; i
< extra
; ++i
)
299 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + row_size
);
305 bmap
->c_size
= n_eq
+ n_ineq
;
306 bmap
->eq
= bmap
->ineq
+ n_ineq
;
315 isl_basic_map_free(bmap
);
319 struct isl_basic_set
*isl_basic_set_alloc(struct isl_ctx
*ctx
,
320 unsigned nparam
, unsigned dim
, unsigned extra
,
321 unsigned n_eq
, unsigned n_ineq
)
323 struct isl_basic_map
*bmap
;
324 bmap
= isl_basic_map_alloc(ctx
, nparam
, 0, dim
, extra
, n_eq
, n_ineq
);
325 return (struct isl_basic_set
*)bmap
;
328 struct isl_basic_set
*isl_basic_set_alloc_dim(struct isl_dim
*dim
,
329 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
331 struct isl_basic_map
*bmap
;
334 isl_assert(dim
->ctx
, dim
->n_in
== 0, return NULL
);
335 bmap
= isl_basic_map_alloc_dim(dim
, extra
, n_eq
, n_ineq
);
336 return (struct isl_basic_set
*)bmap
;
339 struct isl_basic_map
*isl_basic_map_alloc_dim(struct isl_dim
*dim
,
340 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
342 struct isl_basic_map
*bmap
;
346 bmap
= isl_alloc_type(dim
->ctx
, struct isl_basic_map
);
351 return basic_map_init(dim
->ctx
, bmap
, extra
, n_eq
, n_ineq
);
357 struct isl_basic_map
*isl_basic_map_alloc(struct isl_ctx
*ctx
,
358 unsigned nparam
, unsigned in
, unsigned out
, unsigned extra
,
359 unsigned n_eq
, unsigned n_ineq
)
361 struct isl_basic_map
*bmap
;
364 dim
= isl_dim_alloc(ctx
, nparam
, in
, out
);
368 bmap
= isl_basic_map_alloc_dim(dim
, extra
, n_eq
, n_ineq
);
372 static void dup_constraints(
373 struct isl_basic_map
*dst
, struct isl_basic_map
*src
)
376 unsigned total
= isl_basic_map_total_dim(src
);
378 for (i
= 0; i
< src
->n_eq
; ++i
) {
379 int j
= isl_basic_map_alloc_equality(dst
);
380 isl_seq_cpy(dst
->eq
[j
], src
->eq
[i
], 1+total
);
383 for (i
= 0; i
< src
->n_ineq
; ++i
) {
384 int j
= isl_basic_map_alloc_inequality(dst
);
385 isl_seq_cpy(dst
->ineq
[j
], src
->ineq
[i
], 1+total
);
388 for (i
= 0; i
< src
->n_div
; ++i
) {
389 int j
= isl_basic_map_alloc_div(dst
);
390 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
);
392 ISL_F_SET(dst
, ISL_BASIC_SET_FINAL
);
395 struct isl_basic_map
*isl_basic_map_dup(struct isl_basic_map
*bmap
)
397 struct isl_basic_map
*dup
;
401 dup
= isl_basic_map_alloc_dim(isl_dim_copy(bmap
->dim
),
402 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
405 dup_constraints(dup
, bmap
);
406 dup
->flags
= bmap
->flags
;
407 dup
->sample
= isl_vec_copy(bmap
->sample
);
411 struct isl_basic_set
*isl_basic_set_dup(struct isl_basic_set
*bset
)
413 struct isl_basic_map
*dup
;
415 dup
= isl_basic_map_dup((struct isl_basic_map
*)bset
);
416 return (struct isl_basic_set
*)dup
;
419 struct isl_basic_set
*isl_basic_set_copy(struct isl_basic_set
*bset
)
424 if (ISL_F_ISSET(bset
, ISL_BASIC_SET_FINAL
)) {
428 return isl_basic_set_dup(bset
);
431 struct isl_set
*isl_set_copy(struct isl_set
*set
)
440 struct isl_basic_map
*isl_basic_map_copy(struct isl_basic_map
*bmap
)
445 if (ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
)) {
449 return isl_basic_map_dup(bmap
);
452 struct isl_map
*isl_map_copy(struct isl_map
*map
)
461 void isl_basic_map_free(struct isl_basic_map
*bmap
)
469 isl_ctx_deref(bmap
->ctx
);
471 isl_blk_free(bmap
->ctx
, bmap
->block2
);
473 isl_blk_free(bmap
->ctx
, bmap
->block
);
474 isl_vec_free(bmap
->sample
);
475 isl_dim_free(bmap
->dim
);
479 void isl_basic_set_free(struct isl_basic_set
*bset
)
481 isl_basic_map_free((struct isl_basic_map
*)bset
);
484 static int room_for_con(struct isl_basic_map
*bmap
, unsigned n
)
486 return bmap
->n_eq
+ bmap
->n_ineq
+ n
<= bmap
->c_size
;
489 int isl_basic_map_alloc_equality(struct isl_basic_map
*bmap
)
495 isl_assert(ctx
, room_for_con(bmap
, 1), return -1);
496 isl_assert(ctx
, (bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
<= bmap
->c_size
,
498 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
499 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
500 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
501 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
502 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
503 if ((bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
== bmap
->c_size
) {
505 int j
= isl_basic_map_alloc_inequality(bmap
);
509 bmap
->ineq
[j
] = bmap
->ineq
[bmap
->n_ineq
- 1];
510 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
517 isl_seq_clr(bmap
->eq
[bmap
->n_eq
] + 1 + isl_basic_map_total_dim(bmap
),
518 bmap
->extra
- bmap
->n_div
);
522 int isl_basic_set_alloc_equality(struct isl_basic_set
*bset
)
524 return isl_basic_map_alloc_equality((struct isl_basic_map
*)bset
);
527 int isl_basic_map_free_equality(struct isl_basic_map
*bmap
, unsigned n
)
531 isl_assert(bmap
->ctx
, n
<= bmap
->n_eq
, return -1);
536 int isl_basic_set_free_equality(struct isl_basic_set
*bset
, unsigned n
)
538 return isl_basic_map_free_equality((struct isl_basic_map
*)bset
, n
);
541 int isl_basic_map_drop_equality(struct isl_basic_map
*bmap
, unsigned pos
)
546 isl_assert(bmap
->ctx
, pos
< bmap
->n_eq
, return -1);
548 if (pos
!= bmap
->n_eq
- 1) {
550 bmap
->eq
[pos
] = bmap
->eq
[bmap
->n_eq
- 1];
551 bmap
->eq
[bmap
->n_eq
- 1] = t
;
557 int isl_basic_set_drop_equality(struct isl_basic_set
*bset
, unsigned pos
)
559 return isl_basic_map_drop_equality((struct isl_basic_map
*)bset
, pos
);
562 void isl_basic_map_inequality_to_equality(
563 struct isl_basic_map
*bmap
, unsigned pos
)
568 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
569 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
574 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
575 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
576 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
577 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
580 static int room_for_ineq(struct isl_basic_map
*bmap
, unsigned n
)
582 return bmap
->n_ineq
+ n
<= bmap
->eq
- bmap
->ineq
;
585 int isl_basic_map_alloc_inequality(struct isl_basic_map
*bmap
)
591 isl_assert(ctx
, room_for_ineq(bmap
, 1), return -1);
592 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
593 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
594 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
595 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
596 isl_seq_clr(bmap
->ineq
[bmap
->n_ineq
] +
597 1 + isl_basic_map_total_dim(bmap
),
598 bmap
->extra
- bmap
->n_div
);
599 return bmap
->n_ineq
++;
602 int isl_basic_set_alloc_inequality(struct isl_basic_set
*bset
)
604 return isl_basic_map_alloc_inequality((struct isl_basic_map
*)bset
);
607 int isl_basic_map_free_inequality(struct isl_basic_map
*bmap
, unsigned n
)
611 isl_assert(bmap
->ctx
, n
<= bmap
->n_ineq
, return -1);
616 int isl_basic_set_free_inequality(struct isl_basic_set
*bset
, unsigned n
)
618 return isl_basic_map_free_inequality((struct isl_basic_map
*)bset
, n
);
621 int isl_basic_map_drop_inequality(struct isl_basic_map
*bmap
, unsigned pos
)
626 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
628 if (pos
!= bmap
->n_ineq
- 1) {
630 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
631 bmap
->ineq
[bmap
->n_ineq
- 1] = t
;
632 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
638 int isl_basic_set_drop_inequality(struct isl_basic_set
*bset
, unsigned pos
)
640 return isl_basic_map_drop_inequality((struct isl_basic_map
*)bset
, pos
);
643 __isl_give isl_basic_set
*isl_basic_set_add_eq(__isl_take isl_basic_set
*bset
,
648 bset
= isl_basic_set_extend_constraints(bset
, 1, 0);
651 k
= isl_basic_set_alloc_equality(bset
);
654 isl_seq_cpy(bset
->eq
[k
], eq
, 1 + isl_basic_set_total_dim(bset
));
657 isl_basic_set_free(bset
);
661 __isl_give isl_basic_set
*isl_basic_set_add_ineq(__isl_take isl_basic_set
*bset
,
666 bset
= isl_basic_set_extend_constraints(bset
, 0, 1);
669 k
= isl_basic_set_alloc_inequality(bset
);
672 isl_seq_cpy(bset
->ineq
[k
], ineq
, 1 + isl_basic_set_total_dim(bset
));
675 isl_basic_set_free(bset
);
679 int isl_basic_map_alloc_div(struct isl_basic_map
*bmap
)
683 isl_assert(bmap
->ctx
, bmap
->n_div
< bmap
->extra
, return -1);
684 isl_seq_clr(bmap
->div
[bmap
->n_div
] +
685 1 + 1 + isl_basic_map_total_dim(bmap
),
686 bmap
->extra
- bmap
->n_div
);
687 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
688 return bmap
->n_div
++;
691 int isl_basic_set_alloc_div(struct isl_basic_set
*bset
)
693 return isl_basic_map_alloc_div((struct isl_basic_map
*)bset
);
696 int isl_basic_map_free_div(struct isl_basic_map
*bmap
, unsigned n
)
700 isl_assert(bmap
->ctx
, n
<= bmap
->n_div
, return -1);
705 int isl_basic_set_free_div(struct isl_basic_set
*bset
, unsigned n
)
707 return isl_basic_map_free_div((struct isl_basic_map
*)bset
, n
);
710 /* Copy constraint from src to dst, putting the vars of src at offset
711 * dim_off in dst and the divs of src at offset div_off in dst.
712 * If both sets are actually map, then dim_off applies to the input
715 static void copy_constraint(struct isl_basic_map
*dst_map
, isl_int
*dst
,
716 struct isl_basic_map
*src_map
, isl_int
*src
,
717 unsigned in_off
, unsigned out_off
, unsigned div_off
)
719 unsigned src_nparam
= isl_basic_map_n_param(src_map
);
720 unsigned dst_nparam
= isl_basic_map_n_param(dst_map
);
721 unsigned src_in
= isl_basic_map_n_in(src_map
);
722 unsigned dst_in
= isl_basic_map_n_in(dst_map
);
723 unsigned src_out
= isl_basic_map_n_out(src_map
);
724 unsigned dst_out
= isl_basic_map_n_out(dst_map
);
725 isl_int_set(dst
[0], src
[0]);
726 isl_seq_cpy(dst
+1, src
+1, isl_min(dst_nparam
, src_nparam
));
727 if (dst_nparam
> src_nparam
)
728 isl_seq_clr(dst
+1+src_nparam
,
729 dst_nparam
- src_nparam
);
730 isl_seq_clr(dst
+1+dst_nparam
, in_off
);
731 isl_seq_cpy(dst
+1+dst_nparam
+in_off
,
733 isl_min(dst_in
-in_off
, src_in
));
734 if (dst_in
-in_off
> src_in
)
735 isl_seq_clr(dst
+1+dst_nparam
+in_off
+src_in
,
736 dst_in
- in_off
- src_in
);
737 isl_seq_clr(dst
+1+dst_nparam
+dst_in
, out_off
);
738 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+out_off
,
739 src
+1+src_nparam
+src_in
,
740 isl_min(dst_out
-out_off
, src_out
));
741 if (dst_out
-out_off
> src_out
)
742 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+out_off
+src_out
,
743 dst_out
- out_off
- src_out
);
744 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
, div_off
);
745 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+dst_out
+div_off
,
746 src
+1+src_nparam
+src_in
+src_out
,
747 isl_min(dst_map
->extra
-div_off
, src_map
->n_div
));
748 if (dst_map
->n_div
-div_off
> src_map
->n_div
)
749 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
+
750 div_off
+src_map
->n_div
,
751 dst_map
->n_div
- div_off
- src_map
->n_div
);
754 static void copy_div(struct isl_basic_map
*dst_map
, isl_int
*dst
,
755 struct isl_basic_map
*src_map
, isl_int
*src
,
756 unsigned in_off
, unsigned out_off
, unsigned div_off
)
758 isl_int_set(dst
[0], src
[0]);
759 copy_constraint(dst_map
, dst
+1, src_map
, src
+1, in_off
, out_off
, div_off
);
762 static struct isl_basic_map
*add_constraints(struct isl_basic_map
*bmap1
,
763 struct isl_basic_map
*bmap2
, unsigned i_pos
, unsigned o_pos
)
768 if (!bmap1
|| !bmap2
)
771 div_off
= bmap1
->n_div
;
773 for (i
= 0; i
< bmap2
->n_eq
; ++i
) {
774 int i1
= isl_basic_map_alloc_equality(bmap1
);
777 copy_constraint(bmap1
, bmap1
->eq
[i1
], bmap2
, bmap2
->eq
[i
],
778 i_pos
, o_pos
, div_off
);
781 for (i
= 0; i
< bmap2
->n_ineq
; ++i
) {
782 int i1
= isl_basic_map_alloc_inequality(bmap1
);
785 copy_constraint(bmap1
, bmap1
->ineq
[i1
], bmap2
, bmap2
->ineq
[i
],
786 i_pos
, o_pos
, div_off
);
789 for (i
= 0; i
< bmap2
->n_div
; ++i
) {
790 int i1
= isl_basic_map_alloc_div(bmap1
);
793 copy_div(bmap1
, bmap1
->div
[i1
], bmap2
, bmap2
->div
[i
],
794 i_pos
, o_pos
, div_off
);
797 isl_basic_map_free(bmap2
);
802 isl_basic_map_free(bmap1
);
803 isl_basic_map_free(bmap2
);
807 static void copy_constraint_dim_map(isl_int
*dst
, isl_int
*src
,
808 struct isl_dim_map
*dim_map
)
812 for (i
= 0; i
< dim_map
->len
; ++i
) {
813 if (dim_map
->pos
[i
] < 0)
814 isl_int_set_si(dst
[i
], 0);
816 isl_int_set(dst
[i
], src
[dim_map
->pos
[i
]]);
820 static void copy_div_dim_map(isl_int
*dst
, isl_int
*src
,
821 struct isl_dim_map
*dim_map
)
823 isl_int_set(dst
[0], src
[0]);
824 copy_constraint_dim_map(dst
+1, src
+1, dim_map
);
827 static struct isl_basic_map
*add_constraints_dim_map(struct isl_basic_map
*dst
,
828 struct isl_basic_map
*src
, struct isl_dim_map
*dim_map
)
832 if (!src
|| !dst
|| !dim_map
)
835 for (i
= 0; i
< src
->n_eq
; ++i
) {
836 int i1
= isl_basic_map_alloc_equality(dst
);
839 copy_constraint_dim_map(dst
->eq
[i1
], src
->eq
[i
], dim_map
);
842 for (i
= 0; i
< src
->n_ineq
; ++i
) {
843 int i1
= isl_basic_map_alloc_inequality(dst
);
846 copy_constraint_dim_map(dst
->ineq
[i1
], src
->ineq
[i
], dim_map
);
849 for (i
= 0; i
< src
->n_div
; ++i
) {
850 int i1
= isl_basic_map_alloc_div(dst
);
853 copy_div_dim_map(dst
->div
[i1
], src
->div
[i
], dim_map
);
857 isl_basic_map_free(src
);
862 isl_basic_map_free(src
);
863 isl_basic_map_free(dst
);
867 struct isl_basic_set
*isl_basic_set_add_constraints(struct isl_basic_set
*bset1
,
868 struct isl_basic_set
*bset2
, unsigned pos
)
870 return (struct isl_basic_set
*)
871 add_constraints((struct isl_basic_map
*)bset1
,
872 (struct isl_basic_map
*)bset2
, 0, pos
);
875 struct isl_basic_map
*isl_basic_map_extend_dim(struct isl_basic_map
*base
,
876 struct isl_dim
*dim
, unsigned extra
,
877 unsigned n_eq
, unsigned n_ineq
)
879 struct isl_basic_map
*ext
;
889 dims_ok
= isl_dim_equal(base
->dim
, dim
) &&
890 base
->extra
>= base
->n_div
+ extra
;
892 if (dims_ok
&& room_for_con(base
, n_eq
+ n_ineq
) &&
893 room_for_ineq(base
, n_ineq
)) {
898 isl_assert(base
->ctx
, base
->dim
->nparam
<= dim
->nparam
, goto error
);
899 isl_assert(base
->ctx
, base
->dim
->n_in
<= dim
->n_in
, goto error
);
900 isl_assert(base
->ctx
, base
->dim
->n_out
<= dim
->n_out
, goto error
);
901 extra
+= base
->extra
;
903 n_ineq
+= base
->n_ineq
;
905 ext
= isl_basic_map_alloc_dim(dim
, extra
, n_eq
, n_ineq
);
911 ext
->sample
= isl_vec_copy(base
->sample
);
913 ext
= add_constraints(ext
, base
, 0, 0);
916 ISL_F_CLR(ext
, ISL_BASIC_SET_FINAL
);
923 isl_basic_map_free(base
);
927 struct isl_basic_set
*isl_basic_set_extend_dim(struct isl_basic_set
*base
,
928 struct isl_dim
*dim
, unsigned extra
,
929 unsigned n_eq
, unsigned n_ineq
)
931 return (struct isl_basic_set
*)
932 isl_basic_map_extend_dim((struct isl_basic_map
*)base
, dim
,
933 extra
, n_eq
, n_ineq
);
936 struct isl_basic_map
*isl_basic_map_extend_constraints(
937 struct isl_basic_map
*base
, unsigned n_eq
, unsigned n_ineq
)
941 return isl_basic_map_extend_dim(base
, isl_dim_copy(base
->dim
),
945 struct isl_basic_map
*isl_basic_map_extend(struct isl_basic_map
*base
,
946 unsigned nparam
, unsigned n_in
, unsigned n_out
, unsigned extra
,
947 unsigned n_eq
, unsigned n_ineq
)
949 struct isl_basic_map
*bmap
;
954 dim
= isl_dim_alloc(base
->ctx
, nparam
, n_in
, n_out
);
958 bmap
= isl_basic_map_extend_dim(base
, dim
, extra
, n_eq
, n_ineq
);
962 struct isl_basic_set
*isl_basic_set_extend(struct isl_basic_set
*base
,
963 unsigned nparam
, unsigned dim
, unsigned extra
,
964 unsigned n_eq
, unsigned n_ineq
)
966 return (struct isl_basic_set
*)
967 isl_basic_map_extend((struct isl_basic_map
*)base
,
968 nparam
, 0, dim
, extra
, n_eq
, n_ineq
);
971 struct isl_basic_set
*isl_basic_set_extend_constraints(
972 struct isl_basic_set
*base
, unsigned n_eq
, unsigned n_ineq
)
974 return (struct isl_basic_set
*)
975 isl_basic_map_extend_constraints((struct isl_basic_map
*)base
,
979 struct isl_basic_set
*isl_basic_set_cow(struct isl_basic_set
*bset
)
981 return (struct isl_basic_set
*)
982 isl_basic_map_cow((struct isl_basic_map
*)bset
);
985 struct isl_basic_map
*isl_basic_map_cow(struct isl_basic_map
*bmap
)
992 bmap
= isl_basic_map_dup(bmap
);
994 ISL_F_CLR(bmap
, ISL_BASIC_SET_FINAL
);
998 struct isl_set
*isl_set_cow(struct isl_set
*set
)
1006 return isl_set_dup(set
);
1009 struct isl_map
*isl_map_cow(struct isl_map
*map
)
1017 return isl_map_dup(map
);
1020 static void swap_vars(struct isl_blk blk
, isl_int
*a
,
1021 unsigned a_len
, unsigned b_len
)
1023 isl_seq_cpy(blk
.data
, a
+a_len
, b_len
);
1024 isl_seq_cpy(blk
.data
+b_len
, a
, a_len
);
1025 isl_seq_cpy(a
, blk
.data
, b_len
+a_len
);
1028 struct isl_basic_set
*isl_basic_set_swap_vars(
1029 struct isl_basic_set
*bset
, unsigned n
)
1039 nparam
= isl_basic_set_n_param(bset
);
1040 dim
= isl_basic_set_n_dim(bset
);
1041 isl_assert(bset
->ctx
, n
<= dim
, goto error
);
1046 bset
= isl_basic_set_cow(bset
);
1050 blk
= isl_blk_alloc(bset
->ctx
, dim
);
1051 if (isl_blk_is_error(blk
))
1054 for (i
= 0; i
< bset
->n_eq
; ++i
)
1056 bset
->eq
[i
]+1+nparam
, n
, dim
- n
);
1058 for (i
= 0; i
< bset
->n_ineq
; ++i
)
1060 bset
->ineq
[i
]+1+nparam
, n
, dim
- n
);
1062 for (i
= 0; i
< bset
->n_div
; ++i
)
1064 bset
->div
[i
]+1+1+nparam
, n
, dim
- n
);
1066 isl_blk_free(bset
->ctx
, blk
);
1068 ISL_F_CLR(bset
, ISL_BASIC_SET_NORMALIZED
);
1072 isl_basic_set_free(bset
);
1076 struct isl_set
*isl_set_swap_vars(struct isl_set
*set
, unsigned n
)
1079 set
= isl_set_cow(set
);
1083 for (i
= 0; i
< set
->n
; ++i
) {
1084 set
->p
[i
] = isl_basic_set_swap_vars(set
->p
[i
], n
);
1090 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
1094 struct isl_basic_map
*isl_basic_map_set_to_empty(struct isl_basic_map
*bmap
)
1100 total
= isl_basic_map_total_dim(bmap
);
1101 isl_basic_map_free_div(bmap
, bmap
->n_div
);
1102 isl_basic_map_free_inequality(bmap
, bmap
->n_ineq
);
1104 isl_basic_map_free_equality(bmap
, bmap
->n_eq
-1);
1106 isl_basic_map_alloc_equality(bmap
);
1110 isl_int_set_si(bmap
->eq
[i
][0], 1);
1111 isl_seq_clr(bmap
->eq
[i
]+1, total
);
1112 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
1113 return isl_basic_map_finalize(bmap
);
1115 isl_basic_map_free(bmap
);
1119 struct isl_basic_set
*isl_basic_set_set_to_empty(struct isl_basic_set
*bset
)
1121 return (struct isl_basic_set
*)
1122 isl_basic_map_set_to_empty((struct isl_basic_map
*)bset
);
1125 void isl_basic_map_swap_div(struct isl_basic_map
*bmap
, int a
, int b
)
1128 unsigned off
= isl_dim_total(bmap
->dim
);
1129 isl_int
*t
= bmap
->div
[a
];
1130 bmap
->div
[a
] = bmap
->div
[b
];
1133 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1134 isl_int_swap(bmap
->eq
[i
][1+off
+a
], bmap
->eq
[i
][1+off
+b
]);
1136 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1137 isl_int_swap(bmap
->ineq
[i
][1+off
+a
], bmap
->ineq
[i
][1+off
+b
]);
1139 for (i
= 0; i
< bmap
->n_div
; ++i
)
1140 isl_int_swap(bmap
->div
[i
][1+1+off
+a
], bmap
->div
[i
][1+1+off
+b
]);
1141 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1144 /* Eliminate the specified n dimensions starting at first from the
1145 * constraints using Fourier-Motzkin, The dimensions themselves
1148 struct isl_set
*isl_set_eliminate_dims(struct isl_set
*set
,
1149 unsigned first
, unsigned n
)
1159 set
= isl_set_cow(set
);
1162 isl_assert(set
->ctx
, first
+n
<= isl_set_n_dim(set
), goto error
);
1163 nparam
= isl_set_n_param(set
);
1165 for (i
= 0; i
< set
->n
; ++i
) {
1166 set
->p
[i
] = isl_basic_set_eliminate_vars(set
->p
[i
],
1177 /* Project out n dimensions starting at first using Fourier-Motzkin */
1178 struct isl_set
*isl_set_remove_dims(struct isl_set
*set
,
1179 unsigned first
, unsigned n
)
1181 set
= isl_set_eliminate_dims(set
, first
, n
);
1182 set
= isl_set_drop_dims(set
, first
, n
);
1186 struct isl_basic_set
*isl_basic_set_remove_divs(struct isl_basic_set
*bset
)
1188 bset
= isl_basic_set_eliminate_vars(bset
, isl_dim_total(bset
->dim
),
1196 struct isl_set
*isl_set_remove_divs(struct isl_set
*set
)
1205 set
= isl_set_cow(set
);
1209 for (i
= 0; i
< set
->n
; ++i
) {
1210 set
->p
[i
] = isl_basic_set_remove_divs(set
->p
[i
]);
1220 struct isl_basic_map
*isl_basic_map_remove(struct isl_basic_map
*bmap
,
1221 enum isl_dim_type type
, unsigned first
, unsigned n
)
1225 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
1229 bmap
= isl_basic_map_eliminate_vars(bmap
,
1230 isl_basic_map_offset(bmap
, type
) - 1 + first
, n
);
1233 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
) && type
== isl_dim_div
)
1235 bmap
= isl_basic_map_drop(bmap
, type
, first
, n
);
1238 isl_basic_map_free(bmap
);
1242 __isl_give isl_basic_set
*isl_basic_set_remove(__isl_take isl_basic_set
*bset
,
1243 enum isl_dim_type type
, unsigned first
, unsigned n
)
1245 return (isl_basic_set
*)
1246 isl_basic_map_remove((isl_basic_map
*)bset
, type
, first
, n
);
1249 struct isl_map
*isl_map_remove(struct isl_map
*map
,
1250 enum isl_dim_type type
, unsigned first
, unsigned n
)
1257 map
= isl_map_cow(map
);
1260 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
1262 for (i
= 0; i
< map
->n
; ++i
) {
1263 map
->p
[i
] = isl_basic_map_eliminate_vars(map
->p
[i
],
1264 isl_basic_map_offset(map
->p
[i
], type
) - 1 + first
, n
);
1268 map
= isl_map_drop(map
, type
, first
, n
);
1275 __isl_give isl_set
*isl_set_remove(__isl_take isl_set
*bset
,
1276 enum isl_dim_type type
, unsigned first
, unsigned n
)
1278 return (isl_set
*)isl_map_remove((isl_map
*)bset
, type
, first
, n
);
1281 /* Project out n inputs starting at first using Fourier-Motzkin */
1282 struct isl_map
*isl_map_remove_inputs(struct isl_map
*map
,
1283 unsigned first
, unsigned n
)
1285 return isl_map_remove(map
, isl_dim_in
, first
, n
);
1288 /* Project out n dimensions starting at first using Fourier-Motzkin */
1289 struct isl_basic_set
*isl_basic_set_remove_dims(struct isl_basic_set
*bset
,
1290 unsigned first
, unsigned n
)
1292 unsigned nparam
= isl_basic_set_n_param(bset
);
1293 bset
= isl_basic_set_eliminate_vars(bset
, nparam
+ first
, n
);
1294 bset
= isl_basic_set_drop_dims(bset
, first
, n
);
1298 static void dump_term(struct isl_basic_map
*bmap
,
1299 isl_int c
, int pos
, FILE *out
)
1302 unsigned in
= isl_basic_map_n_in(bmap
);
1303 unsigned dim
= in
+ isl_basic_map_n_out(bmap
);
1304 unsigned nparam
= isl_basic_map_n_param(bmap
);
1306 isl_int_print(out
, c
, 0);
1308 if (!isl_int_is_one(c
))
1309 isl_int_print(out
, c
, 0);
1310 if (pos
< 1 + nparam
) {
1311 name
= isl_dim_get_name(bmap
->dim
,
1312 isl_dim_param
, pos
- 1);
1314 fprintf(out
, "%s", name
);
1316 fprintf(out
, "p%d", pos
- 1);
1317 } else if (pos
< 1 + nparam
+ in
)
1318 fprintf(out
, "i%d", pos
- 1 - nparam
);
1319 else if (pos
< 1 + nparam
+ dim
)
1320 fprintf(out
, "o%d", pos
- 1 - nparam
- in
);
1322 fprintf(out
, "e%d", pos
- 1 - nparam
- dim
);
1326 static void dump_constraint_sign(struct isl_basic_map
*bmap
, isl_int
*c
,
1327 int sign
, FILE *out
)
1331 unsigned len
= 1 + isl_basic_map_total_dim(bmap
);
1335 for (i
= 0, first
= 1; i
< len
; ++i
) {
1336 if (isl_int_sgn(c
[i
]) * sign
<= 0)
1339 fprintf(out
, " + ");
1341 isl_int_abs(v
, c
[i
]);
1342 dump_term(bmap
, v
, i
, out
);
1349 static void dump_constraint(struct isl_basic_map
*bmap
, isl_int
*c
,
1350 const char *op
, FILE *out
, int indent
)
1354 fprintf(out
, "%*s", indent
, "");
1356 dump_constraint_sign(bmap
, c
, 1, out
);
1357 fprintf(out
, " %s ", op
);
1358 dump_constraint_sign(bmap
, c
, -1, out
);
1362 for (i
= bmap
->n_div
; i
< bmap
->extra
; ++i
) {
1363 if (isl_int_is_zero(c
[1+isl_dim_total(bmap
->dim
)+i
]))
1365 fprintf(out
, "%*s", indent
, "");
1366 fprintf(out
, "ERROR: unused div coefficient not zero\n");
1371 static void dump_constraints(struct isl_basic_map
*bmap
,
1372 isl_int
**c
, unsigned n
,
1373 const char *op
, FILE *out
, int indent
)
1377 for (i
= 0; i
< n
; ++i
)
1378 dump_constraint(bmap
, c
[i
], op
, out
, indent
);
1381 static void dump_affine(struct isl_basic_map
*bmap
, isl_int
*exp
, FILE *out
)
1385 unsigned total
= isl_basic_map_total_dim(bmap
);
1387 for (j
= 0; j
< 1 + total
; ++j
) {
1388 if (isl_int_is_zero(exp
[j
]))
1390 if (!first
&& isl_int_is_pos(exp
[j
]))
1392 dump_term(bmap
, exp
[j
], j
, out
);
1397 static void dump(struct isl_basic_map
*bmap
, FILE *out
, int indent
)
1401 dump_constraints(bmap
, bmap
->eq
, bmap
->n_eq
, "=", out
, indent
);
1402 dump_constraints(bmap
, bmap
->ineq
, bmap
->n_ineq
, ">=", out
, indent
);
1404 for (i
= 0; i
< bmap
->n_div
; ++i
) {
1405 fprintf(out
, "%*s", indent
, "");
1406 fprintf(out
, "e%d = [(", i
);
1407 dump_affine(bmap
, bmap
->div
[i
]+1, out
);
1409 isl_int_print(out
, bmap
->div
[i
][0], 0);
1410 fprintf(out
, "]\n");
1414 void isl_basic_set_dump(struct isl_basic_set
*bset
, FILE *out
, int indent
)
1417 fprintf(out
, "null basic set\n");
1421 fprintf(out
, "%*s", indent
, "");
1422 fprintf(out
, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
1423 bset
->ref
, bset
->dim
->nparam
, bset
->dim
->n_out
,
1424 bset
->extra
, bset
->flags
);
1425 dump((struct isl_basic_map
*)bset
, out
, indent
);
1428 void isl_basic_map_dump(struct isl_basic_map
*bmap
, FILE *out
, int indent
)
1431 fprintf(out
, "null basic map\n");
1435 fprintf(out
, "%*s", indent
, "");
1436 fprintf(out
, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
1437 "flags: %x, n_name: %d\n",
1439 bmap
->dim
->nparam
, bmap
->dim
->n_in
, bmap
->dim
->n_out
,
1440 bmap
->extra
, bmap
->flags
, bmap
->dim
->n_name
);
1441 dump(bmap
, out
, indent
);
1444 int isl_inequality_negate(struct isl_basic_map
*bmap
, unsigned pos
)
1449 total
= isl_basic_map_total_dim(bmap
);
1450 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
1451 isl_seq_neg(bmap
->ineq
[pos
], bmap
->ineq
[pos
], 1 + total
);
1452 isl_int_sub_ui(bmap
->ineq
[pos
][0], bmap
->ineq
[pos
][0], 1);
1453 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1457 struct isl_set
*isl_set_alloc_dim(struct isl_dim
*dim
, int n
, unsigned flags
)
1459 struct isl_set
*set
;
1463 isl_assert(dim
->ctx
, dim
->n_in
== 0, return NULL
);
1464 isl_assert(dim
->ctx
, n
>= 0, return NULL
);
1465 set
= isl_alloc(dim
->ctx
, struct isl_set
,
1466 sizeof(struct isl_set
) +
1467 (n
- 1) * sizeof(struct isl_basic_set
*));
1471 set
->ctx
= dim
->ctx
;
1472 isl_ctx_ref(set
->ctx
);
1484 struct isl_set
*isl_set_alloc(struct isl_ctx
*ctx
,
1485 unsigned nparam
, unsigned dim
, int n
, unsigned flags
)
1487 struct isl_set
*set
;
1488 struct isl_dim
*dims
;
1490 dims
= isl_dim_alloc(ctx
, nparam
, 0, dim
);
1494 set
= isl_set_alloc_dim(dims
, n
, flags
);
1498 /* Make sure "map" has room for at least "n" more basic maps.
1500 struct isl_map
*isl_map_grow(struct isl_map
*map
, int n
)
1503 struct isl_map
*grown
= NULL
;
1507 isl_assert(map
->ctx
, n
>= 0, goto error
);
1508 if (map
->n
+ n
<= map
->size
)
1510 grown
= isl_map_alloc_dim(isl_map_get_dim(map
), map
->n
+ n
, map
->flags
);
1513 for (i
= 0; i
< map
->n
; ++i
) {
1514 grown
->p
[i
] = isl_basic_map_copy(map
->p
[i
]);
1522 isl_map_free(grown
);
1527 /* Make sure "set" has room for at least "n" more basic sets.
1529 struct isl_set
*isl_set_grow(struct isl_set
*set
, int n
)
1531 return (struct isl_set
*)isl_map_grow((struct isl_map
*)set
, n
);
1534 struct isl_set
*isl_set_dup(struct isl_set
*set
)
1537 struct isl_set
*dup
;
1542 dup
= isl_set_alloc_dim(isl_dim_copy(set
->dim
), set
->n
, set
->flags
);
1545 for (i
= 0; i
< set
->n
; ++i
)
1546 dup
= isl_set_add(dup
, isl_basic_set_copy(set
->p
[i
]));
1550 struct isl_set
*isl_set_from_basic_set(struct isl_basic_set
*bset
)
1552 struct isl_set
*set
;
1557 set
= isl_set_alloc_dim(isl_dim_copy(bset
->dim
), 1, ISL_MAP_DISJOINT
);
1559 isl_basic_set_free(bset
);
1562 return isl_set_add(set
, bset
);
1565 struct isl_map
*isl_map_from_basic_map(struct isl_basic_map
*bmap
)
1567 struct isl_map
*map
;
1572 map
= isl_map_alloc_dim(isl_dim_copy(bmap
->dim
), 1, ISL_MAP_DISJOINT
);
1574 isl_basic_map_free(bmap
);
1577 return isl_map_add(map
, bmap
);
1580 struct isl_set
*isl_set_add(struct isl_set
*set
, struct isl_basic_set
*bset
)
1582 return (struct isl_set
*)isl_map_add((struct isl_map
*)set
,
1583 (struct isl_basic_map
*)bset
);
1586 void isl_set_free(struct isl_set
*set
)
1596 isl_ctx_deref(set
->ctx
);
1597 for (i
= 0; i
< set
->n
; ++i
)
1598 isl_basic_set_free(set
->p
[i
]);
1599 isl_dim_free(set
->dim
);
1603 void isl_set_dump(struct isl_set
*set
, FILE *out
, int indent
)
1608 fprintf(out
, "null set\n");
1612 fprintf(out
, "%*s", indent
, "");
1613 fprintf(out
, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
1614 set
->ref
, set
->n
, set
->dim
->nparam
, set
->dim
->n_out
,
1616 for (i
= 0; i
< set
->n
; ++i
) {
1617 fprintf(out
, "%*s", indent
, "");
1618 fprintf(out
, "basic set %d:\n", i
);
1619 isl_basic_set_dump(set
->p
[i
], out
, indent
+4);
1623 void isl_map_dump(struct isl_map
*map
, FILE *out
, int indent
)
1628 fprintf(out
, "null map\n");
1632 fprintf(out
, "%*s", indent
, "");
1633 fprintf(out
, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
1634 "flags: %x, n_name: %d\n",
1635 map
->ref
, map
->n
, map
->dim
->nparam
, map
->dim
->n_in
,
1636 map
->dim
->n_out
, map
->flags
, map
->dim
->n_name
);
1637 for (i
= 0; i
< map
->n
; ++i
) {
1638 fprintf(out
, "%*s", indent
, "");
1639 fprintf(out
, "basic map %d:\n", i
);
1640 isl_basic_map_dump(map
->p
[i
], out
, indent
+4);
1644 struct isl_basic_map
*isl_basic_map_intersect_domain(
1645 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
1647 struct isl_basic_map
*bmap_domain
;
1648 struct isl_dim
*dim
;
1653 isl_assert(bset
->ctx
, isl_dim_match(bmap
->dim
, isl_dim_param
,
1654 bset
->dim
, isl_dim_param
), goto error
);
1656 if (isl_dim_size(bset
->dim
, isl_dim_set
) != 0)
1657 isl_assert(bset
->ctx
,
1658 isl_basic_map_compatible_domain(bmap
, bset
), goto error
);
1660 bmap
= isl_basic_map_cow(bmap
);
1661 bmap
= isl_basic_map_extend_dim(bmap
, isl_dim_copy(bmap
->dim
),
1662 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
1665 dim
= isl_dim_reverse(isl_dim_copy(bset
->dim
));
1666 bmap_domain
= isl_basic_map_from_basic_set(bset
, dim
);
1667 bmap
= add_constraints(bmap
, bmap_domain
, 0, 0);
1669 bmap
= isl_basic_map_simplify(bmap
);
1670 return isl_basic_map_finalize(bmap
);
1672 isl_basic_map_free(bmap
);
1673 isl_basic_set_free(bset
);
1677 struct isl_basic_map
*isl_basic_map_intersect_range(
1678 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
1680 struct isl_basic_map
*bmap_range
;
1685 isl_assert(bset
->ctx
, isl_dim_match(bmap
->dim
, isl_dim_param
,
1686 bset
->dim
, isl_dim_param
), goto error
);
1688 if (isl_dim_size(bset
->dim
, isl_dim_set
) != 0)
1689 isl_assert(bset
->ctx
,
1690 isl_basic_map_compatible_range(bmap
, bset
), goto error
);
1692 bmap
= isl_basic_map_cow(bmap
);
1693 bmap
= isl_basic_map_extend_dim(bmap
, isl_dim_copy(bmap
->dim
),
1694 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
1697 bmap_range
= isl_basic_map_from_basic_set(bset
, isl_dim_copy(bset
->dim
));
1698 bmap
= add_constraints(bmap
, bmap_range
, 0, 0);
1700 bmap
= isl_basic_map_simplify(bmap
);
1701 return isl_basic_map_finalize(bmap
);
1703 isl_basic_map_free(bmap
);
1704 isl_basic_set_free(bset
);
1708 static int basic_map_contains(struct isl_basic_map
*bmap
, struct isl_vec
*vec
)
1714 total
= 1 + isl_basic_map_total_dim(bmap
);
1715 if (total
!= vec
->size
)
1720 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
1721 isl_seq_inner_product(vec
->el
, bmap
->eq
[i
], total
, &s
);
1722 if (!isl_int_is_zero(s
)) {
1728 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
1729 isl_seq_inner_product(vec
->el
, bmap
->ineq
[i
], total
, &s
);
1730 if (isl_int_is_neg(s
)) {
1741 int isl_basic_set_contains(struct isl_basic_set
*bset
, struct isl_vec
*vec
)
1743 return basic_map_contains((struct isl_basic_map
*)bset
, vec
);
1746 struct isl_basic_map
*isl_basic_map_intersect(
1747 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
1749 struct isl_vec
*sample
= NULL
;
1751 if (!bmap1
|| !bmap2
)
1754 isl_assert(bmap1
->ctx
, isl_dim_match(bmap1
->dim
, isl_dim_param
,
1755 bmap2
->dim
, isl_dim_param
), goto error
);
1756 if (isl_dim_total(bmap1
->dim
) ==
1757 isl_dim_size(bmap1
->dim
, isl_dim_param
) &&
1758 isl_dim_total(bmap2
->dim
) !=
1759 isl_dim_size(bmap2
->dim
, isl_dim_param
))
1760 return isl_basic_map_intersect(bmap2
, bmap1
);
1762 if (isl_dim_total(bmap2
->dim
) !=
1763 isl_dim_size(bmap2
->dim
, isl_dim_param
))
1764 isl_assert(bmap1
->ctx
,
1765 isl_dim_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
1767 if (bmap1
->sample
&&
1768 basic_map_contains(bmap1
, bmap1
->sample
) > 0 &&
1769 basic_map_contains(bmap2
, bmap1
->sample
) > 0)
1770 sample
= isl_vec_copy(bmap1
->sample
);
1771 else if (bmap2
->sample
&&
1772 basic_map_contains(bmap1
, bmap2
->sample
) > 0 &&
1773 basic_map_contains(bmap2
, bmap2
->sample
) > 0)
1774 sample
= isl_vec_copy(bmap2
->sample
);
1776 bmap1
= isl_basic_map_cow(bmap1
);
1777 bmap1
= isl_basic_map_extend_dim(bmap1
, isl_dim_copy(bmap1
->dim
),
1778 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
1781 bmap1
= add_constraints(bmap1
, bmap2
, 0, 0);
1784 isl_vec_free(bmap1
->sample
);
1785 bmap1
->sample
= sample
;
1788 bmap1
= isl_basic_map_simplify(bmap1
);
1789 return isl_basic_map_finalize(bmap1
);
1792 isl_vec_free(sample
);
1793 isl_basic_map_free(bmap1
);
1794 isl_basic_map_free(bmap2
);
1798 struct isl_basic_set
*isl_basic_set_intersect(
1799 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
1801 return (struct isl_basic_set
*)
1802 isl_basic_map_intersect(
1803 (struct isl_basic_map
*)bset1
,
1804 (struct isl_basic_map
*)bset2
);
1807 struct isl_map
*isl_map_intersect(struct isl_map
*map1
, struct isl_map
*map2
)
1810 struct isl_map
*result
;
1816 isl_assert(map1
->ctx
, isl_dim_match(map1
->dim
, isl_dim_param
,
1817 map2
->dim
, isl_dim_param
), goto error
);
1818 if (isl_dim_total(map1
->dim
) ==
1819 isl_dim_size(map1
->dim
, isl_dim_param
) &&
1820 isl_dim_total(map2
->dim
) != isl_dim_size(map2
->dim
, isl_dim_param
))
1821 return isl_map_intersect(map2
, map1
);
1823 if (isl_dim_total(map2
->dim
) != isl_dim_size(map2
->dim
, isl_dim_param
))
1824 isl_assert(map1
->ctx
,
1825 isl_dim_equal(map1
->dim
, map2
->dim
), goto error
);
1827 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
1828 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
1829 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
1831 result
= isl_map_alloc_dim(isl_dim_copy(map1
->dim
),
1832 map1
->n
* map2
->n
, flags
);
1835 for (i
= 0; i
< map1
->n
; ++i
)
1836 for (j
= 0; j
< map2
->n
; ++j
) {
1837 struct isl_basic_map
*part
;
1838 part
= isl_basic_map_intersect(
1839 isl_basic_map_copy(map1
->p
[i
]),
1840 isl_basic_map_copy(map2
->p
[j
]));
1841 if (isl_basic_map_is_empty(part
))
1842 isl_basic_map_free(part
);
1844 result
= isl_map_add(result
, part
);
1857 struct isl_set
*isl_set_intersect(struct isl_set
*set1
, struct isl_set
*set2
)
1859 return (struct isl_set
*)
1860 isl_map_intersect((struct isl_map
*)set1
,
1861 (struct isl_map
*)set2
);
1864 struct isl_basic_map
*isl_basic_map_reverse(struct isl_basic_map
*bmap
)
1866 struct isl_dim
*dim
;
1867 struct isl_basic_set
*bset
;
1872 bmap
= isl_basic_map_cow(bmap
);
1875 dim
= isl_dim_reverse(isl_dim_copy(bmap
->dim
));
1876 in
= isl_basic_map_n_in(bmap
);
1877 bset
= isl_basic_set_from_basic_map(bmap
);
1878 bset
= isl_basic_set_swap_vars(bset
, in
);
1879 return isl_basic_map_from_basic_set(bset
, dim
);
1882 /* Turn final n dimensions into existentially quantified variables.
1884 struct isl_basic_set
*isl_basic_set_project_out(struct isl_basic_set
*bset
,
1885 enum isl_dim_type type
, unsigned first
, unsigned n
)
1895 isl_assert(bset
->ctx
, type
== isl_dim_set
, goto error
);
1896 isl_assert(bset
->ctx
, first
+ n
== isl_basic_set_n_dim(bset
), goto error
);
1901 if (ISL_F_ISSET(bset
, ISL_BASIC_SET_RATIONAL
))
1902 return isl_basic_set_remove(bset
, type
, first
, n
);
1904 bset
= isl_basic_set_cow(bset
);
1906 row_size
= 1 + isl_dim_total(bset
->dim
) + bset
->extra
;
1907 old
= bset
->block2
.data
;
1908 bset
->block2
= isl_blk_extend(bset
->ctx
, bset
->block2
,
1909 (bset
->extra
+ n
) * (1 + row_size
));
1910 if (!bset
->block2
.data
)
1912 new_div
= isl_alloc_array(ctx
, isl_int
*, bset
->extra
+ n
);
1915 for (i
= 0; i
< n
; ++i
) {
1916 new_div
[i
] = bset
->block2
.data
+
1917 (bset
->extra
+ i
) * (1 + row_size
);
1918 isl_seq_clr(new_div
[i
], 1 + row_size
);
1920 for (i
= 0; i
< bset
->extra
; ++i
)
1921 new_div
[n
+ i
] = bset
->block2
.data
+ (bset
->div
[i
] - old
);
1923 bset
->div
= new_div
;
1926 bset
->dim
= isl_dim_drop_outputs(bset
->dim
,
1927 isl_basic_set_n_dim(bset
) - n
, n
);
1930 bset
= isl_basic_set_simplify(bset
);
1931 bset
= isl_basic_set_drop_redundant_divs(bset
);
1932 return isl_basic_set_finalize(bset
);
1934 isl_basic_set_free(bset
);
1938 /* Turn final n dimensions into existentially quantified variables.
1940 __isl_give isl_set
*isl_set_project_out(__isl_take isl_set
*set
,
1941 enum isl_dim_type type
, unsigned first
, unsigned n
)
1948 isl_assert(set
->ctx
, type
== isl_dim_set
, goto error
);
1949 isl_assert(set
->ctx
, first
+ n
== isl_set_n_dim(set
), goto error
);
1954 set
= isl_set_cow(set
);
1957 set
->dim
= isl_dim_drop_outputs(set
->dim
, first
, n
);
1958 for (i
= 0; i
< set
->n
; ++i
) {
1959 set
->p
[i
] = isl_basic_set_project_out(set
->p
[i
], type
, first
, n
);
1964 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
1971 static struct isl_basic_map
*add_divs(struct isl_basic_map
*bmap
, unsigned n
)
1975 for (i
= 0; i
< n
; ++i
) {
1976 j
= isl_basic_map_alloc_div(bmap
);
1979 isl_seq_clr(bmap
->div
[j
], 1+1+isl_basic_map_total_dim(bmap
));
1983 isl_basic_map_free(bmap
);
1987 struct isl_basic_map
*isl_basic_map_apply_range(
1988 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
1990 struct isl_dim
*dim_result
= NULL
;
1991 struct isl_basic_map
*bmap
;
1992 unsigned n_in
, n_out
, n
, nparam
, total
, pos
;
1993 struct isl_dim_map
*dim_map1
, *dim_map2
;
1995 if (!bmap1
|| !bmap2
)
1998 dim_result
= isl_dim_join(isl_dim_copy(bmap1
->dim
),
1999 isl_dim_copy(bmap2
->dim
));
2001 n_in
= isl_basic_map_n_in(bmap1
);
2002 n_out
= isl_basic_map_n_out(bmap2
);
2003 n
= isl_basic_map_n_out(bmap1
);
2004 nparam
= isl_basic_map_n_param(bmap1
);
2006 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ n
;
2007 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
2008 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
2009 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
2010 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
2011 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
2012 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_in
);
2013 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_out
);
2014 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
2015 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
2016 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
2018 bmap
= isl_basic_map_alloc_dim(dim_result
,
2019 bmap1
->n_div
+ bmap2
->n_div
+ n
,
2020 bmap1
->n_eq
+ bmap2
->n_eq
,
2021 bmap1
->n_ineq
+ bmap2
->n_ineq
);
2022 bmap
= add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
2023 bmap
= add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
2024 bmap
= add_divs(bmap
, n
);
2025 bmap
= isl_basic_map_simplify(bmap
);
2026 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
2027 return isl_basic_map_finalize(bmap
);
2029 isl_basic_map_free(bmap1
);
2030 isl_basic_map_free(bmap2
);
2034 struct isl_basic_set
*isl_basic_set_apply(
2035 struct isl_basic_set
*bset
, struct isl_basic_map
*bmap
)
2040 isl_assert(bset
->ctx
, isl_basic_map_compatible_domain(bmap
, bset
),
2043 return (struct isl_basic_set
*)
2044 isl_basic_map_apply_range((struct isl_basic_map
*)bset
, bmap
);
2046 isl_basic_set_free(bset
);
2047 isl_basic_map_free(bmap
);
2051 struct isl_basic_map
*isl_basic_map_apply_domain(
2052 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
2054 if (!bmap1
|| !bmap2
)
2057 isl_assert(bmap1
->ctx
,
2058 isl_basic_map_n_in(bmap1
) == isl_basic_map_n_in(bmap2
), goto error
);
2059 isl_assert(bmap1
->ctx
,
2060 isl_basic_map_n_param(bmap1
) == isl_basic_map_n_param(bmap2
),
2063 bmap1
= isl_basic_map_reverse(bmap1
);
2064 bmap1
= isl_basic_map_apply_range(bmap1
, bmap2
);
2065 return isl_basic_map_reverse(bmap1
);
2067 isl_basic_map_free(bmap1
);
2068 isl_basic_map_free(bmap2
);
2072 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
2073 * A \cap B -> f(A) + f(B)
2075 struct isl_basic_map
*isl_basic_map_sum(
2076 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
2078 unsigned n_in
, n_out
, nparam
, total
, pos
;
2079 struct isl_basic_map
*bmap
= NULL
;
2080 struct isl_dim_map
*dim_map1
, *dim_map2
;
2083 if (!bmap1
|| !bmap2
)
2086 isl_assert(bmap1
->ctx
, isl_dim_equal(bmap1
->dim
, bmap2
->dim
),
2089 nparam
= isl_basic_map_n_param(bmap1
);
2090 n_in
= isl_basic_map_n_in(bmap1
);
2091 n_out
= isl_basic_map_n_out(bmap1
);
2093 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
;
2094 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
2095 dim_map2
= isl_dim_map_alloc(bmap2
->ctx
, total
);
2096 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
2097 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
);
2098 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
2099 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
2100 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_in
+ n_out
);
2101 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
2102 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
2103 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_out
);
2105 bmap
= isl_basic_map_alloc_dim(isl_dim_copy(bmap1
->dim
),
2106 bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
,
2107 bmap1
->n_eq
+ bmap2
->n_eq
+ n_out
,
2108 bmap1
->n_ineq
+ bmap2
->n_ineq
);
2109 for (i
= 0; i
< n_out
; ++i
) {
2110 int j
= isl_basic_map_alloc_equality(bmap
);
2113 isl_seq_clr(bmap
->eq
[j
], 1+total
);
2114 isl_int_set_si(bmap
->eq
[j
][1+nparam
+n_in
+i
], -1);
2115 isl_int_set_si(bmap
->eq
[j
][1+pos
+i
], 1);
2116 isl_int_set_si(bmap
->eq
[j
][1+pos
-n_out
+i
], 1);
2118 bmap
= add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
2119 bmap
= add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
2120 bmap
= add_divs(bmap
, 2 * n_out
);
2122 bmap
= isl_basic_map_simplify(bmap
);
2123 return isl_basic_map_finalize(bmap
);
2125 isl_basic_map_free(bmap
);
2126 isl_basic_map_free(bmap1
);
2127 isl_basic_map_free(bmap2
);
2131 /* Given two maps A -> f(A) and B -> g(B), construct a map
2132 * A \cap B -> f(A) + f(B)
2134 struct isl_map
*isl_map_sum(struct isl_map
*map1
, struct isl_map
*map2
)
2136 struct isl_map
*result
;
2142 isl_assert(map1
->ctx
, isl_dim_equal(map1
->dim
, map2
->dim
), goto error
);
2144 result
= isl_map_alloc_dim(isl_dim_copy(map1
->dim
),
2145 map1
->n
* map2
->n
, 0);
2148 for (i
= 0; i
< map1
->n
; ++i
)
2149 for (j
= 0; j
< map2
->n
; ++j
) {
2150 struct isl_basic_map
*part
;
2151 part
= isl_basic_map_sum(
2152 isl_basic_map_copy(map1
->p
[i
]),
2153 isl_basic_map_copy(map2
->p
[j
]));
2154 if (isl_basic_map_is_empty(part
))
2155 isl_basic_map_free(part
);
2157 result
= isl_map_add(result
, part
);
2170 /* Given a basic map A -> f(A), construct A -> -f(A).
2172 struct isl_basic_map
*isl_basic_map_neg(struct isl_basic_map
*bmap
)
2177 bmap
= isl_basic_map_cow(bmap
);
2181 n
= isl_basic_map_dim(bmap
, isl_dim_out
);
2182 off
= isl_basic_map_offset(bmap
, isl_dim_out
);
2183 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2184 for (j
= 0; j
< n
; ++j
)
2185 isl_int_neg(bmap
->eq
[i
][off
+j
], bmap
->eq
[i
][off
+j
]);
2186 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2187 for (j
= 0; j
< n
; ++j
)
2188 isl_int_neg(bmap
->ineq
[i
][off
+j
], bmap
->ineq
[i
][off
+j
]);
2189 for (i
= 0; i
< bmap
->n_div
; ++i
)
2190 for (j
= 0; j
< n
; ++j
)
2191 isl_int_neg(bmap
->div
[i
][1+off
+j
], bmap
->div
[i
][1+off
+j
]);
2192 return isl_basic_map_finalize(bmap
);
2195 /* Given a map A -> f(A), construct A -> -f(A).
2197 struct isl_map
*isl_map_neg(struct isl_map
*map
)
2201 map
= isl_map_cow(map
);
2205 for (i
= 0; i
< map
->n
; ++i
) {
2206 map
->p
[i
] = isl_basic_map_neg(map
->p
[i
]);
2217 /* Given a basic map A -> f(A) and an integer d, construct a basic map
2218 * A -> floor(f(A)/d).
2220 struct isl_basic_map
*isl_basic_map_floordiv(struct isl_basic_map
*bmap
,
2223 unsigned n_in
, n_out
, nparam
, total
, pos
;
2224 struct isl_basic_map
*result
= NULL
;
2225 struct isl_dim_map
*dim_map
;
2231 nparam
= isl_basic_map_n_param(bmap
);
2232 n_in
= isl_basic_map_n_in(bmap
);
2233 n_out
= isl_basic_map_n_out(bmap
);
2235 total
= nparam
+ n_in
+ n_out
+ bmap
->n_div
+ n_out
;
2236 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
2237 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_param
, pos
= 0);
2238 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_in
, pos
+= nparam
);
2239 isl_dim_map_div(dim_map
, bmap
, pos
+= n_in
+ n_out
);
2240 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_out
, pos
+= bmap
->n_div
);
2242 result
= isl_basic_map_alloc_dim(isl_dim_copy(bmap
->dim
),
2243 bmap
->n_div
+ n_out
,
2244 bmap
->n_eq
, bmap
->n_ineq
+ 2 * n_out
);
2245 result
= add_constraints_dim_map(result
, bmap
, dim_map
);
2246 result
= add_divs(result
, n_out
);
2247 for (i
= 0; i
< n_out
; ++i
) {
2249 j
= isl_basic_map_alloc_inequality(result
);
2252 isl_seq_clr(result
->ineq
[j
], 1+total
);
2253 isl_int_neg(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
2254 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], 1);
2255 j
= isl_basic_map_alloc_inequality(result
);
2258 isl_seq_clr(result
->ineq
[j
], 1+total
);
2259 isl_int_set(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
2260 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], -1);
2261 isl_int_sub_ui(result
->ineq
[j
][0], d
, 1);
2264 result
= isl_basic_map_simplify(result
);
2265 return isl_basic_map_finalize(result
);
2267 isl_basic_map_free(result
);
2271 /* Given a map A -> f(A) and an integer d, construct a map
2272 * A -> floor(f(A)/d).
2274 struct isl_map
*isl_map_floordiv(struct isl_map
*map
, isl_int d
)
2278 map
= isl_map_cow(map
);
2282 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
2283 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
2284 for (i
= 0; i
< map
->n
; ++i
) {
2285 map
->p
[i
] = isl_basic_map_floordiv(map
->p
[i
], d
);
2296 static struct isl_basic_map
*var_equal(struct isl_basic_map
*bmap
, unsigned pos
)
2302 i
= isl_basic_map_alloc_equality(bmap
);
2305 nparam
= isl_basic_map_n_param(bmap
);
2306 n_in
= isl_basic_map_n_in(bmap
);
2307 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
2308 isl_int_set_si(bmap
->eq
[i
][1+nparam
+pos
], -1);
2309 isl_int_set_si(bmap
->eq
[i
][1+nparam
+n_in
+pos
], 1);
2310 return isl_basic_map_finalize(bmap
);
2312 isl_basic_map_free(bmap
);
2316 /* Add a constraints to "bmap" expressing i_pos < o_pos
2318 static struct isl_basic_map
*var_less(struct isl_basic_map
*bmap
, unsigned pos
)
2324 i
= isl_basic_map_alloc_inequality(bmap
);
2327 nparam
= isl_basic_map_n_param(bmap
);
2328 n_in
= isl_basic_map_n_in(bmap
);
2329 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
2330 isl_int_set_si(bmap
->ineq
[i
][0], -1);
2331 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
2332 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
2333 return isl_basic_map_finalize(bmap
);
2335 isl_basic_map_free(bmap
);
2339 /* Add a constraints to "bmap" expressing i_pos > o_pos
2341 static struct isl_basic_map
*var_more(struct isl_basic_map
*bmap
, unsigned pos
)
2347 i
= isl_basic_map_alloc_inequality(bmap
);
2350 nparam
= isl_basic_map_n_param(bmap
);
2351 n_in
= isl_basic_map_n_in(bmap
);
2352 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
2353 isl_int_set_si(bmap
->ineq
[i
][0], -1);
2354 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
2355 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
2356 return isl_basic_map_finalize(bmap
);
2358 isl_basic_map_free(bmap
);
2362 struct isl_basic_map
*isl_basic_map_equal(struct isl_dim
*dim
, unsigned n_equal
)
2365 struct isl_basic_map
*bmap
;
2366 bmap
= isl_basic_map_alloc_dim(dim
, 0, n_equal
, 0);
2369 for (i
= 0; i
< n_equal
&& bmap
; ++i
)
2370 bmap
= var_equal(bmap
, i
);
2371 return isl_basic_map_finalize(bmap
);
2374 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos < o_pos
2376 struct isl_basic_map
*isl_basic_map_less_at(struct isl_dim
*dim
, unsigned pos
)
2379 struct isl_basic_map
*bmap
;
2380 bmap
= isl_basic_map_alloc_dim(dim
, 0, pos
, 1);
2383 for (i
= 0; i
< pos
&& bmap
; ++i
)
2384 bmap
= var_equal(bmap
, i
);
2386 bmap
= var_less(bmap
, pos
);
2387 return isl_basic_map_finalize(bmap
);
2390 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
2392 struct isl_basic_map
*isl_basic_map_more_at(struct isl_dim
*dim
, unsigned pos
)
2395 struct isl_basic_map
*bmap
;
2396 bmap
= isl_basic_map_alloc_dim(dim
, 0, pos
, 1);
2399 for (i
= 0; i
< pos
&& bmap
; ++i
)
2400 bmap
= var_equal(bmap
, i
);
2402 bmap
= var_more(bmap
, pos
);
2403 return isl_basic_map_finalize(bmap
);
2406 static __isl_give isl_map
*map_lex_lte(__isl_take isl_dim
*dims
, int equal
)
2408 struct isl_map
*map
;
2415 map
= isl_map_alloc_dim(isl_dim_copy(dims
), dim
+ equal
, ISL_MAP_DISJOINT
);
2417 for (i
= 0; i
< dim
; ++i
)
2418 map
= isl_map_add(map
,
2419 isl_basic_map_less_at(isl_dim_copy(dims
), i
));
2421 map
= isl_map_add(map
,
2422 isl_basic_map_equal(isl_dim_copy(dims
), dim
));
2428 __isl_give isl_map
*isl_map_lex_lt(__isl_take isl_dim
*set_dim
)
2430 return map_lex_lte(isl_dim_map(set_dim
), 0);
2433 __isl_give isl_map
*isl_map_lex_le(__isl_take isl_dim
*set_dim
)
2435 return map_lex_lte(isl_dim_map(set_dim
), 1);
2438 static __isl_give isl_map
*map_lex_gte(__isl_take isl_dim
*dims
, int equal
)
2440 struct isl_map
*map
;
2447 map
= isl_map_alloc_dim(isl_dim_copy(dims
), dim
+ equal
, ISL_MAP_DISJOINT
);
2449 for (i
= 0; i
< dim
; ++i
)
2450 map
= isl_map_add(map
,
2451 isl_basic_map_more_at(isl_dim_copy(dims
), i
));
2453 map
= isl_map_add(map
,
2454 isl_basic_map_equal(isl_dim_copy(dims
), dim
));
2460 __isl_give isl_map
*isl_map_lex_gt(__isl_take isl_dim
*set_dim
)
2462 return map_lex_gte(isl_dim_map(set_dim
), 0);
2465 __isl_give isl_map
*isl_map_lex_ge(__isl_take isl_dim
*set_dim
)
2467 return map_lex_gte(isl_dim_map(set_dim
), 1);
2470 struct isl_basic_map
*isl_basic_map_from_basic_set(
2471 struct isl_basic_set
*bset
, struct isl_dim
*dim
)
2473 struct isl_basic_map
*bmap
;
2475 bset
= isl_basic_set_cow(bset
);
2479 isl_assert(bset
->ctx
, isl_dim_compatible(bset
->dim
, dim
), goto error
);
2480 isl_dim_free(bset
->dim
);
2481 bmap
= (struct isl_basic_map
*) bset
;
2483 return isl_basic_map_finalize(bmap
);
2485 isl_basic_set_free(bset
);
2490 struct isl_basic_set
*isl_basic_set_from_basic_map(struct isl_basic_map
*bmap
)
2494 if (bmap
->dim
->n_in
== 0)
2495 return (struct isl_basic_set
*)bmap
;
2496 bmap
= isl_basic_map_cow(bmap
);
2499 bmap
->dim
= isl_dim_cow(bmap
->dim
);
2502 bmap
->dim
->n_out
+= bmap
->dim
->n_in
;
2503 bmap
->dim
->n_in
= 0;
2504 bmap
= isl_basic_map_finalize(bmap
);
2505 return (struct isl_basic_set
*)bmap
;
2507 isl_basic_map_free(bmap
);
2511 /* For a div d = floor(f/m), add the constraints
2514 * -(f-(n-1)) + m d >= 0
2516 * Note that the second constraint is the negation of
2520 static int add_div_constraints(struct isl_basic_map
*bmap
, unsigned div
)
2523 unsigned total
= isl_basic_map_total_dim(bmap
);
2524 unsigned div_pos
= 1 + total
- bmap
->n_div
+ div
;
2526 i
= isl_basic_map_alloc_inequality(bmap
);
2529 isl_seq_cpy(bmap
->ineq
[i
], bmap
->div
[div
]+1, 1+total
);
2530 isl_int_neg(bmap
->ineq
[i
][div_pos
], bmap
->div
[div
][0]);
2532 j
= isl_basic_map_alloc_inequality(bmap
);
2535 isl_seq_neg(bmap
->ineq
[j
], bmap
->ineq
[i
], 1 + total
);
2536 isl_int_add(bmap
->ineq
[j
][0], bmap
->ineq
[j
][0], bmap
->ineq
[j
][div_pos
]);
2537 isl_int_sub_ui(bmap
->ineq
[j
][0], bmap
->ineq
[j
][0], 1);
2541 struct isl_basic_set
*isl_basic_map_underlying_set(
2542 struct isl_basic_map
*bmap
)
2546 if (bmap
->dim
->nparam
== 0 && bmap
->dim
->n_in
== 0 && bmap
->n_div
== 0)
2547 return (struct isl_basic_set
*)bmap
;
2548 bmap
= isl_basic_map_cow(bmap
);
2551 bmap
->dim
= isl_dim_underlying(bmap
->dim
, bmap
->n_div
);
2554 bmap
->extra
-= bmap
->n_div
;
2556 bmap
= isl_basic_map_finalize(bmap
);
2557 return (struct isl_basic_set
*)bmap
;
2562 __isl_give isl_basic_set
*isl_basic_set_underlying_set(
2563 __isl_take isl_basic_set
*bset
)
2565 return isl_basic_map_underlying_set((isl_basic_map
*)bset
);
2568 struct isl_basic_map
*isl_basic_map_overlying_set(
2569 struct isl_basic_set
*bset
, struct isl_basic_map
*like
)
2571 struct isl_basic_map
*bmap
;
2572 struct isl_ctx
*ctx
;
2579 isl_assert(ctx
, bset
->n_div
== 0, goto error
);
2580 isl_assert(ctx
, isl_basic_set_n_param(bset
) == 0, goto error
);
2581 isl_assert(ctx
, bset
->dim
->n_out
== isl_basic_map_total_dim(like
),
2583 if (isl_dim_equal(bset
->dim
, like
->dim
) && like
->n_div
== 0) {
2584 isl_basic_map_free(like
);
2585 return (struct isl_basic_map
*)bset
;
2587 bset
= isl_basic_set_cow(bset
);
2590 total
= bset
->dim
->n_out
+ bset
->extra
;
2591 bmap
= (struct isl_basic_map
*)bset
;
2592 isl_dim_free(bmap
->dim
);
2593 bmap
->dim
= isl_dim_copy(like
->dim
);
2596 bmap
->n_div
= like
->n_div
;
2597 bmap
->extra
+= like
->n_div
;
2600 ltotal
= total
- bmap
->extra
+ like
->extra
;
2603 bmap
->block2
= isl_blk_extend(ctx
, bmap
->block2
,
2604 bmap
->extra
* (1 + 1 + total
));
2605 if (isl_blk_is_error(bmap
->block2
))
2607 bmap
->div
= isl_realloc_array(ctx
, bmap
->div
, isl_int
*,
2611 for (i
= 0; i
< bmap
->extra
; ++i
)
2612 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + 1 + total
);
2613 for (i
= 0; i
< like
->n_div
; ++i
) {
2614 isl_seq_cpy(bmap
->div
[i
], like
->div
[i
], 1 + 1 + ltotal
);
2615 isl_seq_clr(bmap
->div
[i
]+1+1+ltotal
, total
- ltotal
);
2617 bmap
= isl_basic_map_extend_constraints(bmap
,
2618 0, 2 * like
->n_div
);
2619 for (i
= 0; i
< like
->n_div
; ++i
) {
2620 if (isl_int_is_zero(bmap
->div
[i
][0]))
2622 if (add_div_constraints(bmap
, i
) < 0)
2626 isl_basic_map_free(like
);
2627 bmap
= isl_basic_map_simplify(bmap
);
2628 bmap
= isl_basic_map_finalize(bmap
);
2631 isl_basic_map_free(like
);
2632 isl_basic_set_free(bset
);
2636 struct isl_basic_set
*isl_basic_set_from_underlying_set(
2637 struct isl_basic_set
*bset
, struct isl_basic_set
*like
)
2639 return (struct isl_basic_set
*)
2640 isl_basic_map_overlying_set(bset
, (struct isl_basic_map
*)like
);
2643 struct isl_set
*isl_set_from_underlying_set(
2644 struct isl_set
*set
, struct isl_basic_set
*like
)
2650 isl_assert(set
->ctx
, set
->dim
->n_out
== isl_basic_set_total_dim(like
),
2652 if (isl_dim_equal(set
->dim
, like
->dim
) && like
->n_div
== 0) {
2653 isl_basic_set_free(like
);
2656 set
= isl_set_cow(set
);
2659 for (i
= 0; i
< set
->n
; ++i
) {
2660 set
->p
[i
] = isl_basic_set_from_underlying_set(set
->p
[i
],
2661 isl_basic_set_copy(like
));
2665 isl_dim_free(set
->dim
);
2666 set
->dim
= isl_dim_copy(like
->dim
);
2669 isl_basic_set_free(like
);
2672 isl_basic_set_free(like
);
2677 struct isl_set
*isl_map_underlying_set(struct isl_map
*map
)
2681 map
= isl_map_cow(map
);
2684 map
->dim
= isl_dim_cow(map
->dim
);
2688 for (i
= 1; i
< map
->n
; ++i
)
2689 isl_assert(map
->ctx
, map
->p
[0]->n_div
== map
->p
[i
]->n_div
,
2691 for (i
= 0; i
< map
->n
; ++i
) {
2692 map
->p
[i
] = (struct isl_basic_map
*)
2693 isl_basic_map_underlying_set(map
->p
[i
]);
2698 map
->dim
= isl_dim_underlying(map
->dim
, 0);
2700 isl_dim_free(map
->dim
);
2701 map
->dim
= isl_dim_copy(map
->p
[0]->dim
);
2705 return (struct isl_set
*)map
;
2711 struct isl_set
*isl_set_to_underlying_set(struct isl_set
*set
)
2713 return (struct isl_set
*)isl_map_underlying_set((struct isl_map
*)set
);
2716 struct isl_basic_set
*isl_basic_map_domain(struct isl_basic_map
*bmap
)
2718 struct isl_basic_set
*domain
;
2723 n_in
= isl_basic_map_n_in(bmap
);
2724 n_out
= isl_basic_map_n_out(bmap
);
2725 domain
= isl_basic_set_from_basic_map(bmap
);
2726 return isl_basic_set_project_out(domain
, isl_dim_set
, n_in
, n_out
);
2729 struct isl_basic_set
*isl_basic_map_range(struct isl_basic_map
*bmap
)
2731 return isl_basic_map_domain(isl_basic_map_reverse(bmap
));
2734 struct isl_set
*isl_map_range(struct isl_map
*map
)
2737 struct isl_set
*set
;
2741 map
= isl_map_cow(map
);
2745 set
= (struct isl_set
*) map
;
2746 if (set
->dim
->n_in
!= 0) {
2747 set
->dim
= isl_dim_drop_inputs(set
->dim
, 0, set
->dim
->n_in
);
2751 for (i
= 0; i
< map
->n
; ++i
) {
2752 set
->p
[i
] = isl_basic_map_range(map
->p
[i
]);
2756 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
2757 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
2764 struct isl_map
*isl_map_from_set(struct isl_set
*set
, struct isl_dim
*dim
)
2767 struct isl_map
*map
= NULL
;
2769 set
= isl_set_cow(set
);
2772 isl_assert(set
->ctx
, isl_dim_compatible(set
->dim
, dim
), goto error
);
2773 map
= (struct isl_map
*)set
;
2774 for (i
= 0; i
< set
->n
; ++i
) {
2775 map
->p
[i
] = isl_basic_map_from_basic_set(
2776 set
->p
[i
], isl_dim_copy(dim
));
2780 isl_dim_free(map
->dim
);
2789 struct isl_map
*isl_map_from_range(struct isl_set
*set
)
2791 return (struct isl_map
*)set
;
2794 struct isl_set
*isl_set_from_map(struct isl_map
*map
)
2797 struct isl_set
*set
= NULL
;
2801 map
= isl_map_cow(map
);
2804 map
->dim
= isl_dim_cow(map
->dim
);
2807 map
->dim
->n_out
+= map
->dim
->n_in
;
2809 set
= (struct isl_set
*)map
;
2810 for (i
= 0; i
< map
->n
; ++i
) {
2811 set
->p
[i
] = isl_basic_set_from_basic_map(map
->p
[i
]);
2821 struct isl_map
*isl_map_alloc_dim(struct isl_dim
*dim
, int n
, unsigned flags
)
2823 struct isl_map
*map
;
2827 isl_assert(dim
->ctx
, n
>= 0, return NULL
);
2828 map
= isl_alloc(dim
->ctx
, struct isl_map
,
2829 sizeof(struct isl_map
) +
2830 (n
- 1) * sizeof(struct isl_basic_map
*));
2834 map
->ctx
= dim
->ctx
;
2835 isl_ctx_ref(map
->ctx
);
2847 struct isl_map
*isl_map_alloc(struct isl_ctx
*ctx
,
2848 unsigned nparam
, unsigned in
, unsigned out
, int n
,
2851 struct isl_map
*map
;
2852 struct isl_dim
*dims
;
2854 dims
= isl_dim_alloc(ctx
, nparam
, in
, out
);
2858 map
= isl_map_alloc_dim(dims
, n
, flags
);
2862 struct isl_basic_map
*isl_basic_map_empty(struct isl_dim
*dim
)
2864 struct isl_basic_map
*bmap
;
2865 bmap
= isl_basic_map_alloc_dim(dim
, 0, 1, 0);
2866 bmap
= isl_basic_map_set_to_empty(bmap
);
2870 struct isl_basic_set
*isl_basic_set_empty(struct isl_dim
*dim
)
2872 struct isl_basic_set
*bset
;
2873 bset
= isl_basic_set_alloc_dim(dim
, 0, 1, 0);
2874 bset
= isl_basic_set_set_to_empty(bset
);
2878 struct isl_basic_map
*isl_basic_map_empty_like(struct isl_basic_map
*model
)
2880 struct isl_basic_map
*bmap
;
2883 bmap
= isl_basic_map_alloc_dim(isl_dim_copy(model
->dim
), 0, 1, 0);
2884 bmap
= isl_basic_map_set_to_empty(bmap
);
2888 struct isl_basic_map
*isl_basic_map_empty_like_map(struct isl_map
*model
)
2890 struct isl_basic_map
*bmap
;
2893 bmap
= isl_basic_map_alloc_dim(isl_dim_copy(model
->dim
), 0, 1, 0);
2894 bmap
= isl_basic_map_set_to_empty(bmap
);
2898 struct isl_basic_set
*isl_basic_set_empty_like(struct isl_basic_set
*model
)
2900 struct isl_basic_set
*bset
;
2903 bset
= isl_basic_set_alloc_dim(isl_dim_copy(model
->dim
), 0, 1, 0);
2904 bset
= isl_basic_set_set_to_empty(bset
);
2908 struct isl_basic_map
*isl_basic_map_universe(struct isl_dim
*dim
)
2910 struct isl_basic_map
*bmap
;
2911 bmap
= isl_basic_map_alloc_dim(dim
, 0, 0, 0);
2915 struct isl_basic_set
*isl_basic_set_universe(struct isl_dim
*dim
)
2917 struct isl_basic_set
*bset
;
2918 bset
= isl_basic_set_alloc_dim(dim
, 0, 0, 0);
2922 __isl_give isl_basic_map
*isl_basic_map_universe_like(
2923 __isl_keep isl_basic_map
*model
)
2927 return isl_basic_map_alloc_dim(isl_dim_copy(model
->dim
), 0, 0, 0);
2930 struct isl_basic_set
*isl_basic_set_universe_like(struct isl_basic_set
*model
)
2934 return isl_basic_set_alloc_dim(isl_dim_copy(model
->dim
), 0, 0, 0);
2937 __isl_give isl_basic_set
*isl_basic_set_universe_like_set(
2938 __isl_keep isl_set
*model
)
2942 return isl_basic_set_alloc_dim(isl_dim_copy(model
->dim
), 0, 0, 0);
2945 struct isl_map
*isl_map_empty(struct isl_dim
*dim
)
2947 return isl_map_alloc_dim(dim
, 0, ISL_MAP_DISJOINT
);
2950 struct isl_map
*isl_map_empty_like(struct isl_map
*model
)
2954 return isl_map_alloc_dim(isl_dim_copy(model
->dim
), 0, ISL_MAP_DISJOINT
);
2957 struct isl_map
*isl_map_empty_like_basic_map(struct isl_basic_map
*model
)
2961 return isl_map_alloc_dim(isl_dim_copy(model
->dim
), 0, ISL_MAP_DISJOINT
);
2964 struct isl_set
*isl_set_empty(struct isl_dim
*dim
)
2966 return isl_set_alloc_dim(dim
, 0, ISL_MAP_DISJOINT
);
2969 struct isl_set
*isl_set_empty_like(struct isl_set
*model
)
2973 return isl_set_empty(isl_dim_copy(model
->dim
));
2976 struct isl_map
*isl_map_universe(struct isl_dim
*dim
)
2978 struct isl_map
*map
;
2981 map
= isl_map_alloc_dim(isl_dim_copy(dim
), 1, ISL_MAP_DISJOINT
);
2982 map
= isl_map_add(map
, isl_basic_map_universe(dim
));
2986 struct isl_set
*isl_set_universe(struct isl_dim
*dim
)
2988 struct isl_set
*set
;
2991 set
= isl_set_alloc_dim(isl_dim_copy(dim
), 1, ISL_MAP_DISJOINT
);
2992 set
= isl_set_add(set
, isl_basic_set_universe(dim
));
2996 __isl_give isl_set
*isl_set_universe_like(__isl_keep isl_set
*model
)
3000 return isl_set_universe(isl_dim_copy(model
->dim
));
3003 struct isl_map
*isl_map_dup(struct isl_map
*map
)
3006 struct isl_map
*dup
;
3010 dup
= isl_map_alloc_dim(isl_dim_copy(map
->dim
), map
->n
, map
->flags
);
3011 for (i
= 0; i
< map
->n
; ++i
)
3012 dup
= isl_map_add(dup
, isl_basic_map_copy(map
->p
[i
]));
3016 struct isl_map
*isl_map_add(struct isl_map
*map
, struct isl_basic_map
*bmap
)
3020 if (isl_basic_map_fast_is_empty(bmap
)) {
3021 isl_basic_map_free(bmap
);
3024 isl_assert(map
->ctx
, isl_dim_equal(map
->dim
, bmap
->dim
), goto error
);
3025 isl_assert(map
->ctx
, map
->n
< map
->size
, goto error
);
3026 map
->p
[map
->n
] = bmap
;
3028 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3034 isl_basic_map_free(bmap
);
3038 void isl_map_free(struct isl_map
*map
)
3048 isl_ctx_deref(map
->ctx
);
3049 for (i
= 0; i
< map
->n
; ++i
)
3050 isl_basic_map_free(map
->p
[i
]);
3051 isl_dim_free(map
->dim
);
3055 struct isl_map
*isl_map_extend(struct isl_map
*base
,
3056 unsigned nparam
, unsigned n_in
, unsigned n_out
)
3060 base
= isl_map_cow(base
);
3064 base
->dim
= isl_dim_extend(base
->dim
, nparam
, n_in
, n_out
);
3067 for (i
= 0; i
< base
->n
; ++i
) {
3068 base
->p
[i
] = isl_basic_map_extend_dim(base
->p
[i
],
3069 isl_dim_copy(base
->dim
), 0, 0, 0);
3079 struct isl_set
*isl_set_extend(struct isl_set
*base
,
3080 unsigned nparam
, unsigned dim
)
3082 return (struct isl_set
*)isl_map_extend((struct isl_map
*)base
,
3086 static struct isl_basic_map
*isl_basic_map_fix_pos_si(
3087 struct isl_basic_map
*bmap
, unsigned pos
, int value
)
3091 bmap
= isl_basic_map_cow(bmap
);
3092 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
3093 j
= isl_basic_map_alloc_equality(bmap
);
3096 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
3097 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
3098 isl_int_set_si(bmap
->eq
[j
][0], value
);
3099 bmap
= isl_basic_map_simplify(bmap
);
3100 return isl_basic_map_finalize(bmap
);
3102 isl_basic_map_free(bmap
);
3106 static __isl_give isl_basic_map
*isl_basic_map_fix_pos(
3107 __isl_take isl_basic_map
*bmap
, unsigned pos
, isl_int value
)
3111 bmap
= isl_basic_map_cow(bmap
);
3112 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
3113 j
= isl_basic_map_alloc_equality(bmap
);
3116 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
3117 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
3118 isl_int_set(bmap
->eq
[j
][0], value
);
3119 bmap
= isl_basic_map_simplify(bmap
);
3120 return isl_basic_map_finalize(bmap
);
3122 isl_basic_map_free(bmap
);
3126 struct isl_basic_map
*isl_basic_map_fix_si(struct isl_basic_map
*bmap
,
3127 enum isl_dim_type type
, unsigned pos
, int value
)
3131 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
3132 return isl_basic_map_fix_pos_si(bmap
,
3133 isl_basic_map_offset(bmap
, type
) + pos
, value
);
3135 isl_basic_map_free(bmap
);
3139 __isl_give isl_basic_map
*isl_basic_map_fix(__isl_take isl_basic_map
*bmap
,
3140 enum isl_dim_type type
, unsigned pos
, isl_int value
)
3144 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
3145 return isl_basic_map_fix_pos(bmap
,
3146 isl_basic_map_offset(bmap
, type
) + pos
, value
);
3148 isl_basic_map_free(bmap
);
3152 struct isl_basic_set
*isl_basic_set_fix_si(struct isl_basic_set
*bset
,
3153 enum isl_dim_type type
, unsigned pos
, int value
)
3155 return (struct isl_basic_set
*)
3156 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
3160 __isl_give isl_basic_set
*isl_basic_set_fix(__isl_take isl_basic_set
*bset
,
3161 enum isl_dim_type type
, unsigned pos
, isl_int value
)
3163 return (struct isl_basic_set
*)
3164 isl_basic_map_fix((struct isl_basic_map
*)bset
,
3168 struct isl_basic_map
*isl_basic_map_fix_input_si(struct isl_basic_map
*bmap
,
3169 unsigned input
, int value
)
3171 return isl_basic_map_fix_si(bmap
, isl_dim_in
, input
, value
);
3174 struct isl_basic_set
*isl_basic_set_fix_dim_si(struct isl_basic_set
*bset
,
3175 unsigned dim
, int value
)
3177 return (struct isl_basic_set
*)
3178 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
3179 isl_dim_set
, dim
, value
);
3182 struct isl_map
*isl_map_fix_si(struct isl_map
*map
,
3183 enum isl_dim_type type
, unsigned pos
, int value
)
3187 map
= isl_map_cow(map
);
3191 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
3192 for (i
= 0; i
< map
->n
; ++i
) {
3193 map
->p
[i
] = isl_basic_map_fix_si(map
->p
[i
], type
, pos
, value
);
3197 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3204 __isl_give isl_map
*isl_map_fix(__isl_take isl_map
*map
,
3205 enum isl_dim_type type
, unsigned pos
, isl_int value
)
3209 map
= isl_map_cow(map
);
3213 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
3214 for (i
= 0; i
< map
->n
; ++i
) {
3215 map
->p
[i
] = isl_basic_map_fix(map
->p
[i
], type
, pos
, value
);
3219 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3226 __isl_give isl_set
*isl_set_fix(__isl_take isl_set
*set
,
3227 enum isl_dim_type type
, unsigned pos
, isl_int value
)
3229 return (struct isl_set
*)isl_map_fix((isl_map
*)set
, type
, pos
, value
);
3232 struct isl_map
*isl_map_fix_input_si(struct isl_map
*map
,
3233 unsigned input
, int value
)
3235 return isl_map_fix_si(map
, isl_dim_in
, input
, value
);
3238 struct isl_set
*isl_set_fix_dim_si(struct isl_set
*set
, unsigned dim
, int value
)
3240 return (struct isl_set
*)
3241 isl_map_fix_si((struct isl_map
*)set
, isl_dim_set
, dim
, value
);
3244 struct isl_basic_set
*isl_basic_set_lower_bound_dim(struct isl_basic_set
*bset
,
3245 unsigned dim
, isl_int value
)
3249 bset
= isl_basic_set_cow(bset
);
3250 bset
= isl_basic_set_extend_constraints(bset
, 0, 1);
3251 j
= isl_basic_set_alloc_inequality(bset
);
3254 isl_seq_clr(bset
->ineq
[j
], 1 + isl_basic_set_total_dim(bset
));
3255 isl_int_set_si(bset
->ineq
[j
][1 + isl_basic_set_n_param(bset
) + dim
], 1);
3256 isl_int_neg(bset
->ineq
[j
][0], value
);
3257 bset
= isl_basic_set_simplify(bset
);
3258 return isl_basic_set_finalize(bset
);
3260 isl_basic_set_free(bset
);
3264 struct isl_set
*isl_set_lower_bound_dim(struct isl_set
*set
, unsigned dim
,
3269 set
= isl_set_cow(set
);
3273 isl_assert(set
->ctx
, dim
< isl_set_n_dim(set
), goto error
);
3274 for (i
= 0; i
< set
->n
; ++i
) {
3275 set
->p
[i
] = isl_basic_set_lower_bound_dim(set
->p
[i
], dim
, value
);
3285 struct isl_map
*isl_map_reverse(struct isl_map
*map
)
3289 map
= isl_map_cow(map
);
3293 map
->dim
= isl_dim_reverse(map
->dim
);
3296 for (i
= 0; i
< map
->n
; ++i
) {
3297 map
->p
[i
] = isl_basic_map_reverse(map
->p
[i
]);
3301 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3308 static struct isl_map
*isl_basic_map_partial_lexopt(
3309 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
3310 struct isl_set
**empty
, int max
)
3314 if (bmap
->ctx
->opt
->pip
== ISL_PIP_PIP
)
3315 return isl_pip_basic_map_lexopt(bmap
, dom
, empty
, max
);
3317 return isl_tab_basic_map_partial_lexopt(bmap
, dom
, empty
, max
);
3319 isl_basic_map_free(bmap
);
3320 isl_basic_set_free(dom
);
3326 struct isl_map
*isl_basic_map_partial_lexmax(
3327 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
3328 struct isl_set
**empty
)
3330 return isl_basic_map_partial_lexopt(bmap
, dom
, empty
, 1);
3333 struct isl_map
*isl_basic_map_partial_lexmin(
3334 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
3335 struct isl_set
**empty
)
3337 return isl_basic_map_partial_lexopt(bmap
, dom
, empty
, 0);
3340 struct isl_set
*isl_basic_set_partial_lexmin(
3341 struct isl_basic_set
*bset
, struct isl_basic_set
*dom
,
3342 struct isl_set
**empty
)
3344 return (struct isl_set
*)
3345 isl_basic_map_partial_lexmin((struct isl_basic_map
*)bset
,
3349 struct isl_set
*isl_basic_set_partial_lexmax(
3350 struct isl_basic_set
*bset
, struct isl_basic_set
*dom
,
3351 struct isl_set
**empty
)
3353 return (struct isl_set
*)
3354 isl_basic_map_partial_lexmax((struct isl_basic_map
*)bset
,
3358 __isl_give isl_map
*isl_basic_map_lexopt(__isl_take isl_basic_map
*bmap
, int max
)
3360 struct isl_basic_set
*dom
= NULL
;
3361 struct isl_dim
*dom_dim
;
3365 dom_dim
= isl_dim_domain(isl_dim_copy(bmap
->dim
));
3366 dom
= isl_basic_set_universe(dom_dim
);
3367 return isl_basic_map_partial_lexopt(bmap
, dom
, NULL
, max
);
3369 isl_basic_map_free(bmap
);
3373 __isl_give isl_map
*isl_basic_map_lexmin(__isl_take isl_basic_map
*bmap
)
3375 return isl_basic_map_lexopt(bmap
, 0);
3378 __isl_give isl_map
*isl_basic_map_lexmax(__isl_take isl_basic_map
*bmap
)
3380 return isl_basic_map_lexopt(bmap
, 1);
3383 __isl_give isl_set
*isl_basic_set_lexmin(__isl_take isl_basic_set
*bset
)
3385 return (isl_set
*)isl_basic_map_lexmin((isl_basic_map
*)bset
);
3388 __isl_give isl_set
*isl_basic_set_lexmax(__isl_take isl_basic_set
*bset
)
3390 return (isl_set
*)isl_basic_map_lexmax((isl_basic_map
*)bset
);
3393 static struct isl_map
*isl_map_reset_dim(struct isl_map
*map
,
3394 struct isl_dim
*dim
)
3401 for (i
= 0; i
< map
->n
; ++i
) {
3402 isl_dim_free(map
->p
[i
]->dim
);
3403 map
->p
[i
]->dim
= isl_dim_copy(dim
);
3405 isl_dim_free(map
->dim
);
3415 static struct isl_set
*isl_set_reset_dim(struct isl_set
*set
,
3416 struct isl_dim
*dim
)
3418 return (struct isl_set
*) isl_map_reset_dim((struct isl_map
*)set
, dim
);
3421 /* Apply a preimage specified by "mat" on the parameters of "bset".
3422 * bset is assumed to have only parameters and divs.
3424 static struct isl_basic_set
*basic_set_parameter_preimage(
3425 struct isl_basic_set
*bset
, struct isl_mat
*mat
)
3432 bset
->dim
= isl_dim_cow(bset
->dim
);
3436 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
3438 isl_assert(bset
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
3440 bset
->dim
->nparam
= 0;
3441 bset
->dim
->n_out
= nparam
;
3442 bset
= isl_basic_set_preimage(bset
, mat
);
3444 bset
->dim
->nparam
= bset
->dim
->n_out
;
3445 bset
->dim
->n_out
= 0;
3450 isl_basic_set_free(bset
);
3454 /* Apply a preimage specified by "mat" on the parameters of "set".
3455 * set is assumed to have only parameters and divs.
3457 static struct isl_set
*set_parameter_preimage(
3458 struct isl_set
*set
, struct isl_mat
*mat
)
3460 struct isl_dim
*dim
= NULL
;
3466 dim
= isl_dim_copy(set
->dim
);
3467 dim
= isl_dim_cow(dim
);
3471 nparam
= isl_set_dim(set
, isl_dim_param
);
3473 isl_assert(set
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
3476 dim
->n_out
= nparam
;
3477 isl_set_reset_dim(set
, dim
);
3478 set
= isl_set_preimage(set
, mat
);
3481 dim
= isl_dim_copy(set
->dim
);
3482 dim
= isl_dim_cow(dim
);
3485 dim
->nparam
= dim
->n_out
;
3487 isl_set_reset_dim(set
, dim
);
3497 /* Intersect the basic set "bset" with the affine space specified by the
3498 * equalities in "eq".
3500 static struct isl_basic_set
*basic_set_append_equalities(
3501 struct isl_basic_set
*bset
, struct isl_mat
*eq
)
3509 bset
= isl_basic_set_extend_dim(bset
, isl_dim_copy(bset
->dim
), 0,
3514 len
= 1 + isl_dim_total(bset
->dim
) + bset
->extra
;
3515 for (i
= 0; i
< eq
->n_row
; ++i
) {
3516 k
= isl_basic_set_alloc_equality(bset
);
3519 isl_seq_cpy(bset
->eq
[k
], eq
->row
[i
], eq
->n_col
);
3520 isl_seq_clr(bset
->eq
[k
] + eq
->n_col
, len
- eq
->n_col
);
3527 isl_basic_set_free(bset
);
3531 /* Intersect the set "set" with the affine space specified by the
3532 * equalities in "eq".
3534 static struct isl_set
*set_append_equalities(struct isl_set
*set
,
3542 for (i
= 0; i
< set
->n
; ++i
) {
3543 set
->p
[i
] = basic_set_append_equalities(set
->p
[i
],
3556 /* Project the given basic set onto its parameter domain, possibly introducing
3557 * new, explicit, existential variables in the constraints.
3558 * The input has parameters and (possibly implicit) existential variables.
3559 * The output has the same parameters, but only
3560 * explicit existentially quantified variables.
3562 * The actual projection is performed by pip, but pip doesn't seem
3563 * to like equalities very much, so we first remove the equalities
3564 * among the parameters by performing a variable compression on
3565 * the parameters. Afterward, an inverse transformation is performed
3566 * and the equalities among the parameters are inserted back in.
3568 static struct isl_set
*parameter_compute_divs(struct isl_basic_set
*bset
)
3572 struct isl_mat
*T
, *T2
;
3573 struct isl_set
*set
;
3574 unsigned nparam
, n_div
;
3576 bset
= isl_basic_set_cow(bset
);
3580 if (bset
->n_eq
== 0)
3581 return isl_basic_set_lexmin(bset
);
3583 isl_basic_set_gauss(bset
, NULL
);
3585 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
3586 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
3588 for (i
= 0, j
= n_div
- 1; i
< bset
->n_eq
&& j
>= 0; --j
) {
3589 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ j
]))
3592 if (i
== bset
->n_eq
)
3593 return isl_basic_set_lexmin(bset
);
3595 eq
= isl_mat_sub_alloc(bset
->ctx
, bset
->eq
, i
, bset
->n_eq
- i
,
3597 eq
= isl_mat_cow(eq
);
3598 T
= isl_mat_variable_compression(isl_mat_copy(eq
), &T2
);
3599 bset
= basic_set_parameter_preimage(bset
, T
);
3601 set
= isl_basic_set_lexmin(bset
);
3602 set
= set_parameter_preimage(set
, T2
);
3603 set
= set_append_equalities(set
, eq
);
3607 /* Compute an explicit representation for all the existentially
3608 * quantified variables.
3609 * The input and output dimensions are first turned into parameters.
3610 * compute_divs then returns a map with the same parameters and
3611 * no input or output dimensions and the dimension specification
3612 * is reset to that of the input.
3614 static struct isl_map
*compute_divs(struct isl_basic_map
*bmap
)
3616 struct isl_basic_set
*bset
;
3617 struct isl_set
*set
;
3618 struct isl_map
*map
;
3619 struct isl_dim
*dim
, *orig_dim
= NULL
;
3624 bmap
= isl_basic_map_cow(bmap
);
3628 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
3629 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
3630 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
3631 dim
= isl_dim_set_alloc(bmap
->ctx
, nparam
+ n_in
+ n_out
, 0);
3635 orig_dim
= bmap
->dim
;
3637 bset
= (struct isl_basic_set
*)bmap
;
3639 set
= parameter_compute_divs(bset
);
3640 map
= (struct isl_map
*)set
;
3641 map
= isl_map_reset_dim(map
, orig_dim
);
3645 isl_basic_map_free(bmap
);
3649 static int basic_map_divs_known(__isl_keep isl_basic_map
*bmap
)
3657 off
= isl_dim_total(bmap
->dim
);
3658 for (i
= 0; i
< bmap
->n_div
; ++i
) {
3659 if (isl_int_is_zero(bmap
->div
[i
][0]))
3661 isl_assert(bmap
->ctx
, isl_int_is_zero(bmap
->div
[i
][1+1+off
+i
]),
3667 static int map_divs_known(__isl_keep isl_map
*map
)
3674 for (i
= 0; i
< map
->n
; ++i
) {
3675 int known
= basic_map_divs_known(map
->p
[i
]);
3683 /* If bmap contains any unknown divs, then compute explicit
3684 * expressions for them. However, this computation may be
3685 * quite expensive, so first try to remove divs that aren't
3688 struct isl_map
*isl_basic_map_compute_divs(struct isl_basic_map
*bmap
)
3692 struct isl_map
*map
;
3694 known
= basic_map_divs_known(bmap
);
3698 return isl_map_from_basic_map(bmap
);
3700 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
3702 known
= basic_map_divs_known(bmap
);
3706 return isl_map_from_basic_map(bmap
);
3708 map
= compute_divs(bmap
);
3711 isl_basic_map_free(bmap
);
3715 struct isl_map
*isl_map_compute_divs(struct isl_map
*map
)
3719 struct isl_map
*res
;
3726 known
= map_divs_known(map
);
3734 res
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[0]));
3735 for (i
= 1 ; i
< map
->n
; ++i
) {
3737 r2
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[i
]));
3738 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
3739 res
= isl_map_union_disjoint(res
, r2
);
3741 res
= isl_map_union(res
, r2
);
3748 struct isl_set
*isl_basic_set_compute_divs(struct isl_basic_set
*bset
)
3750 return (struct isl_set
*)
3751 isl_basic_map_compute_divs((struct isl_basic_map
*)bset
);
3754 struct isl_set
*isl_set_compute_divs(struct isl_set
*set
)
3756 return (struct isl_set
*)
3757 isl_map_compute_divs((struct isl_map
*)set
);
3760 struct isl_set
*isl_map_domain(struct isl_map
*map
)
3763 struct isl_set
*set
;
3768 map
= isl_map_cow(map
);
3772 set
= (struct isl_set
*)map
;
3773 set
->dim
= isl_dim_domain(set
->dim
);
3776 for (i
= 0; i
< map
->n
; ++i
) {
3777 set
->p
[i
] = isl_basic_map_domain(map
->p
[i
]);
3781 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
3782 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
3789 struct isl_map
*isl_map_union_disjoint(
3790 struct isl_map
*map1
, struct isl_map
*map2
)
3794 struct isl_map
*map
= NULL
;
3808 isl_assert(map1
->ctx
, isl_dim_equal(map1
->dim
, map2
->dim
), goto error
);
3810 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
3811 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
3812 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
3814 map
= isl_map_alloc_dim(isl_dim_copy(map1
->dim
),
3815 map1
->n
+ map2
->n
, flags
);
3818 for (i
= 0; i
< map1
->n
; ++i
) {
3819 map
= isl_map_add(map
,
3820 isl_basic_map_copy(map1
->p
[i
]));
3824 for (i
= 0; i
< map2
->n
; ++i
) {
3825 map
= isl_map_add(map
,
3826 isl_basic_map_copy(map2
->p
[i
]));
3840 struct isl_map
*isl_map_union(struct isl_map
*map1
, struct isl_map
*map2
)
3842 map1
= isl_map_union_disjoint(map1
, map2
);
3846 ISL_F_CLR(map1
, ISL_MAP_DISJOINT
);
3850 struct isl_set
*isl_set_union_disjoint(
3851 struct isl_set
*set1
, struct isl_set
*set2
)
3853 return (struct isl_set
*)
3854 isl_map_union_disjoint(
3855 (struct isl_map
*)set1
, (struct isl_map
*)set2
);
3858 struct isl_set
*isl_set_union(struct isl_set
*set1
, struct isl_set
*set2
)
3860 return (struct isl_set
*)
3861 isl_map_union((struct isl_map
*)set1
, (struct isl_map
*)set2
);
3864 struct isl_map
*isl_map_intersect_range(
3865 struct isl_map
*map
, struct isl_set
*set
)
3868 struct isl_map
*result
;
3874 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
) &&
3875 ISL_F_ISSET(set
, ISL_MAP_DISJOINT
))
3876 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
3878 result
= isl_map_alloc_dim(isl_dim_copy(map
->dim
),
3879 map
->n
* set
->n
, flags
);
3882 for (i
= 0; i
< map
->n
; ++i
)
3883 for (j
= 0; j
< set
->n
; ++j
) {
3884 result
= isl_map_add(result
,
3885 isl_basic_map_intersect_range(
3886 isl_basic_map_copy(map
->p
[i
]),
3887 isl_basic_set_copy(set
->p
[j
])));
3900 struct isl_map
*isl_map_intersect_domain(
3901 struct isl_map
*map
, struct isl_set
*set
)
3903 return isl_map_reverse(
3904 isl_map_intersect_range(isl_map_reverse(map
), set
));
3907 struct isl_map
*isl_map_apply_domain(
3908 struct isl_map
*map1
, struct isl_map
*map2
)
3912 map1
= isl_map_reverse(map1
);
3913 map1
= isl_map_apply_range(map1
, map2
);
3914 return isl_map_reverse(map1
);
3921 struct isl_map
*isl_map_apply_range(
3922 struct isl_map
*map1
, struct isl_map
*map2
)
3924 struct isl_dim
*dim_result
;
3925 struct isl_map
*result
;
3931 dim_result
= isl_dim_join(isl_dim_copy(map1
->dim
),
3932 isl_dim_copy(map2
->dim
));
3934 result
= isl_map_alloc_dim(dim_result
, map1
->n
* map2
->n
, 0);
3937 for (i
= 0; i
< map1
->n
; ++i
)
3938 for (j
= 0; j
< map2
->n
; ++j
) {
3939 result
= isl_map_add(result
,
3940 isl_basic_map_apply_range(
3941 isl_basic_map_copy(map1
->p
[i
]),
3942 isl_basic_map_copy(map2
->p
[j
])));
3948 if (result
&& result
->n
<= 1)
3949 ISL_F_SET(result
, ISL_MAP_DISJOINT
);
3958 * returns range - domain
3960 struct isl_basic_set
*isl_basic_map_deltas(struct isl_basic_map
*bmap
)
3962 struct isl_basic_set
*bset
;
3969 dim
= isl_basic_map_n_in(bmap
);
3970 nparam
= isl_basic_map_n_param(bmap
);
3971 isl_assert(bmap
->ctx
, dim
== isl_basic_map_n_out(bmap
), goto error
);
3972 bset
= isl_basic_set_from_basic_map(bmap
);
3973 bset
= isl_basic_set_cow(bset
);
3974 bset
= isl_basic_set_extend(bset
, nparam
, 3*dim
, 0, dim
, 0);
3975 bset
= isl_basic_set_swap_vars(bset
, 2*dim
);
3976 for (i
= 0; i
< dim
; ++i
) {
3977 int j
= isl_basic_map_alloc_equality(
3978 (struct isl_basic_map
*)bset
);
3981 isl_seq_clr(bset
->eq
[j
], 1 + isl_basic_set_total_dim(bset
));
3982 isl_int_set_si(bset
->eq
[j
][1+nparam
+i
], 1);
3983 isl_int_set_si(bset
->eq
[j
][1+nparam
+dim
+i
], 1);
3984 isl_int_set_si(bset
->eq
[j
][1+nparam
+2*dim
+i
], -1);
3986 return isl_basic_set_project_out(bset
, isl_dim_set
, dim
, 2*dim
);
3988 isl_basic_map_free(bmap
);
3993 * returns range - domain
3995 struct isl_set
*isl_map_deltas(struct isl_map
*map
)
3998 struct isl_set
*result
;
4003 isl_assert(map
->ctx
, isl_map_n_in(map
) == isl_map_n_out(map
), goto error
);
4004 result
= isl_set_alloc(map
->ctx
, isl_map_n_param(map
),
4005 isl_map_n_in(map
), map
->n
, map
->flags
);
4008 for (i
= 0; i
< map
->n
; ++i
)
4009 result
= isl_set_add(result
,
4010 isl_basic_map_deltas(isl_basic_map_copy(map
->p
[i
])));
4018 static struct isl_basic_map
*basic_map_identity(struct isl_dim
*dims
)
4020 struct isl_basic_map
*bmap
;
4028 nparam
= dims
->nparam
;
4030 bmap
= isl_basic_map_alloc_dim(dims
, 0, dim
, 0);
4034 for (i
= 0; i
< dim
; ++i
) {
4035 int j
= isl_basic_map_alloc_equality(bmap
);
4038 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
4039 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
4040 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], -1);
4042 return isl_basic_map_finalize(bmap
);
4044 isl_basic_map_free(bmap
);
4048 struct isl_basic_map
*isl_basic_map_identity(struct isl_dim
*set_dim
)
4050 struct isl_dim
*dim
= isl_dim_map(set_dim
);
4053 return basic_map_identity(dim
);
4056 struct isl_basic_map
*isl_basic_map_identity_like(struct isl_basic_map
*model
)
4058 if (!model
|| !model
->dim
)
4060 isl_assert(model
->ctx
,
4061 model
->dim
->n_in
== model
->dim
->n_out
, return NULL
);
4062 return basic_map_identity(isl_dim_copy(model
->dim
));
4065 static struct isl_map
*map_identity(struct isl_dim
*dim
)
4067 struct isl_map
*map
= isl_map_alloc_dim(dim
, 1, ISL_MAP_DISJOINT
);
4068 return isl_map_add(map
, basic_map_identity(isl_dim_copy(dim
)));
4071 struct isl_map
*isl_map_identity(struct isl_dim
*set_dim
)
4073 struct isl_dim
*dim
= isl_dim_map(set_dim
);
4076 return map_identity(dim
);
4079 struct isl_map
*isl_map_identity_like(struct isl_map
*model
)
4081 if (!model
|| !model
->dim
)
4083 isl_assert(model
->ctx
,
4084 model
->dim
->n_in
== model
->dim
->n_out
, return NULL
);
4085 return map_identity(isl_dim_copy(model
->dim
));
4088 struct isl_map
*isl_map_identity_like_basic_map(struct isl_basic_map
*model
)
4090 if (!model
|| !model
->dim
)
4092 isl_assert(model
->ctx
,
4093 model
->dim
->n_in
== model
->dim
->n_out
, return NULL
);
4094 return map_identity(isl_dim_copy(model
->dim
));
4097 /* Construct a basic set with all set dimensions having only non-negative
4100 struct isl_basic_set
*isl_basic_set_positive_orthant(struct isl_dim
*dims
)
4105 struct isl_basic_set
*bset
;
4109 nparam
= dims
->nparam
;
4111 bset
= isl_basic_set_alloc_dim(dims
, 0, 0, dim
);
4114 for (i
= 0; i
< dim
; ++i
) {
4115 int k
= isl_basic_set_alloc_inequality(bset
);
4118 isl_seq_clr(bset
->ineq
[k
], 1 + isl_basic_set_total_dim(bset
));
4119 isl_int_set_si(bset
->ineq
[k
][1 + nparam
+ i
], 1);
4123 isl_basic_set_free(bset
);
4127 int isl_set_is_equal(struct isl_set
*set1
, struct isl_set
*set2
)
4129 return isl_map_is_equal((struct isl_map
*)set1
, (struct isl_map
*)set2
);
4132 int isl_set_is_subset(struct isl_set
*set1
, struct isl_set
*set2
)
4134 return isl_map_is_subset(
4135 (struct isl_map
*)set1
, (struct isl_map
*)set2
);
4138 int isl_basic_map_is_subset(
4139 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
4142 struct isl_map
*map1
;
4143 struct isl_map
*map2
;
4145 if (!bmap1
|| !bmap2
)
4148 map1
= isl_map_from_basic_map(isl_basic_map_copy(bmap1
));
4149 map2
= isl_map_from_basic_map(isl_basic_map_copy(bmap2
));
4151 is_subset
= isl_map_is_subset(map1
, map2
);
4159 int isl_basic_map_is_equal(
4160 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
4164 if (!bmap1
|| !bmap2
)
4166 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
4169 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
4173 int isl_basic_set_is_equal(
4174 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
4176 return isl_basic_map_is_equal(
4177 (struct isl_basic_map
*)bset1
, (struct isl_basic_map
*)bset2
);
4180 int isl_map_is_empty(struct isl_map
*map
)
4187 for (i
= 0; i
< map
->n
; ++i
) {
4188 is_empty
= isl_basic_map_is_empty(map
->p
[i
]);
4197 int isl_map_fast_is_empty(struct isl_map
*map
)
4202 int isl_set_is_empty(struct isl_set
*set
)
4204 return isl_map_is_empty((struct isl_map
*)set
);
4207 /* Return 1 if "bmap" contains a single element.
4209 int isl_basic_map_is_singleton(__isl_keep isl_basic_map
*bmap
)
4217 return bmap
->n_eq
== isl_basic_map_total_dim(bmap
);
4220 /* Return 1 if "map" contains a single element.
4222 int isl_map_is_singleton(__isl_keep isl_map
*map
)
4229 return isl_basic_map_is_singleton(map
->p
[0]);
4232 /* Given a singleton basic map, extract the single element
4235 static __isl_give isl_vec
*singleton_extract_point(__isl_keep isl_basic_map
*bmap
)
4239 struct isl_vec
*point
;
4245 dim
= isl_basic_map_total_dim(bmap
);
4246 isl_assert(bmap
->ctx
, bmap
->n_eq
== dim
, return NULL
);
4247 point
= isl_vec_alloc(bmap
->ctx
, 1 + dim
);
4253 isl_int_set_si(point
->el
[0], 1);
4254 for (j
= 0; j
< bmap
->n_eq
; ++j
) {
4256 int i
= dim
- 1 - j
;
4257 isl_assert(bmap
->ctx
,
4258 isl_seq_first_non_zero(bmap
->eq
[j
] + 1, i
) == -1,
4260 isl_assert(bmap
->ctx
,
4261 isl_int_is_one(bmap
->eq
[j
][1 + i
]) ||
4262 isl_int_is_negone(bmap
->eq
[j
][1 + i
]),
4264 isl_assert(bmap
->ctx
,
4265 isl_seq_first_non_zero(bmap
->eq
[j
]+1+i
+1, dim
-i
-1) == -1,
4268 isl_int_gcd(m
, point
->el
[0], bmap
->eq
[j
][1 + i
]);
4269 isl_int_divexact(m
, bmap
->eq
[j
][1 + i
], m
);
4271 isl_seq_scale(point
->el
, point
->el
, m
, 1 + i
);
4272 isl_int_divexact(m
, point
->el
[0], bmap
->eq
[j
][1 + i
]);
4274 isl_int_mul(point
->el
[1 + i
], m
, bmap
->eq
[j
][0]);
4281 isl_vec_free(point
);
4285 /* Return 1 if "bmap" contains the point "point".
4286 * "bmap" is assumed to have known divs.
4287 * The point is first extended with the divs and then passed
4288 * to basic_map_contains.
4290 static int basic_map_contains_point(__isl_keep isl_basic_map
*bmap
,
4291 __isl_keep isl_vec
*point
)
4294 struct isl_vec
*vec
;
4298 if (!bmap
|| !point
)
4300 if (bmap
->n_div
== 0)
4301 return basic_map_contains(bmap
, point
);
4303 dim
= isl_basic_map_total_dim(bmap
) - bmap
->n_div
;
4304 vec
= isl_vec_alloc(bmap
->ctx
, 1 + dim
+ bmap
->n_div
);
4308 isl_seq_cpy(vec
->el
, point
->el
, point
->size
);
4309 for (i
= 0; i
< bmap
->n_div
; ++i
) {
4310 isl_seq_inner_product(bmap
->div
[i
] + 1, vec
->el
,
4311 1 + dim
+ i
, &vec
->el
[1+dim
+i
]);
4312 isl_int_fdiv_q(vec
->el
[1+dim
+i
], vec
->el
[1+dim
+i
],
4316 contains
= basic_map_contains(bmap
, vec
);
4322 /* Return 1 is the singleton map "map1" is a subset of "map2",
4323 * i.e., if the single element of "map1" is also an element of "map2".
4325 static int map_is_singleton_subset(__isl_keep isl_map
*map1
,
4326 __isl_keep isl_map
*map2
)
4330 struct isl_vec
*point
;
4337 point
= singleton_extract_point(map1
->p
[0]);
4341 for (i
= 0; i
< map2
->n
; ++i
) {
4342 is_subset
= basic_map_contains_point(map2
->p
[i
], point
);
4347 isl_vec_free(point
);
4351 int isl_map_is_subset(struct isl_map
*map1
, struct isl_map
*map2
)
4354 struct isl_map
*diff
;
4359 if (isl_map_is_empty(map1
))
4362 if (isl_map_is_empty(map2
))
4365 if (isl_map_fast_is_universe(map2
))
4368 map2
= isl_map_compute_divs(isl_map_copy(map2
));
4369 if (isl_map_is_singleton(map1
)) {
4370 is_subset
= map_is_singleton_subset(map1
, map2
);
4374 diff
= isl_map_subtract(isl_map_copy(map1
), map2
);
4378 is_subset
= isl_map_is_empty(diff
);
4384 int isl_map_is_equal(struct isl_map
*map1
, struct isl_map
*map2
)
4390 is_subset
= isl_map_is_subset(map1
, map2
);
4393 is_subset
= isl_map_is_subset(map2
, map1
);
4397 int isl_basic_map_is_strict_subset(
4398 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
4402 if (!bmap1
|| !bmap2
)
4404 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
4407 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
4408 if (is_subset
== -1)
4413 int isl_map_is_strict_subset(struct isl_map
*map1
, struct isl_map
*map2
)
4419 is_subset
= isl_map_is_subset(map1
, map2
);
4422 is_subset
= isl_map_is_subset(map2
, map1
);
4423 if (is_subset
== -1)
4428 int isl_set_is_strict_subset(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
4430 return isl_map_is_strict_subset((isl_map
*)set1
, (isl_map
*)set2
);
4433 int isl_basic_map_is_universe(struct isl_basic_map
*bmap
)
4437 return bmap
->n_eq
== 0 && bmap
->n_ineq
== 0;
4440 int isl_basic_set_is_universe(struct isl_basic_set
*bset
)
4444 return bset
->n_eq
== 0 && bset
->n_ineq
== 0;
4447 int isl_map_fast_is_universe(__isl_keep isl_map
*map
)
4452 return map
->n
== 1 && isl_basic_map_is_universe(map
->p
[0]);
4455 int isl_basic_map_is_empty(struct isl_basic_map
*bmap
)
4457 struct isl_basic_set
*bset
= NULL
;
4458 struct isl_vec
*sample
= NULL
;
4465 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
4468 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
4469 struct isl_basic_map
*copy
= isl_basic_map_copy(bmap
);
4470 copy
= isl_basic_map_convex_hull(copy
);
4471 empty
= ISL_F_ISSET(copy
, ISL_BASIC_MAP_EMPTY
);
4472 isl_basic_map_free(copy
);
4476 total
= 1 + isl_basic_map_total_dim(bmap
);
4477 if (bmap
->sample
&& bmap
->sample
->size
== total
) {
4478 int contains
= basic_map_contains(bmap
, bmap
->sample
);
4484 isl_vec_free(bmap
->sample
);
4485 bmap
->sample
= NULL
;
4486 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
4489 sample
= isl_basic_set_sample_vec(bset
);
4492 empty
= sample
->size
== 0;
4493 isl_vec_free(bmap
->sample
);
4494 bmap
->sample
= sample
;
4496 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
4501 int isl_basic_map_fast_is_empty(struct isl_basic_map
*bmap
)
4505 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
);
4508 int isl_basic_set_fast_is_empty(struct isl_basic_set
*bset
)
4512 return ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
);
4515 int isl_basic_set_is_empty(struct isl_basic_set
*bset
)
4517 return isl_basic_map_is_empty((struct isl_basic_map
*)bset
);
4520 struct isl_map
*isl_basic_map_union(
4521 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
4523 struct isl_map
*map
;
4524 if (!bmap1
|| !bmap2
)
4527 isl_assert(bmap1
->ctx
, isl_dim_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
4529 map
= isl_map_alloc_dim(isl_dim_copy(bmap1
->dim
), 2, 0);
4532 map
= isl_map_add(map
, bmap1
);
4533 map
= isl_map_add(map
, bmap2
);
4536 isl_basic_map_free(bmap1
);
4537 isl_basic_map_free(bmap2
);
4541 struct isl_set
*isl_basic_set_union(
4542 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
4544 return (struct isl_set
*)isl_basic_map_union(
4545 (struct isl_basic_map
*)bset1
,
4546 (struct isl_basic_map
*)bset2
);
4549 /* Order divs such that any div only depends on previous divs */
4550 struct isl_basic_map
*isl_basic_map_order_divs(struct isl_basic_map
*bmap
)
4553 unsigned off
= isl_dim_total(bmap
->dim
);
4555 for (i
= 0; i
< bmap
->n_div
; ++i
) {
4557 if (isl_int_is_zero(bmap
->div
[i
][0]))
4559 pos
= isl_seq_first_non_zero(bmap
->div
[i
]+1+1+off
+i
,
4563 isl_basic_map_swap_div(bmap
, i
, i
+ pos
);
4569 struct isl_basic_set
*isl_basic_set_order_divs(struct isl_basic_set
*bset
)
4571 return (struct isl_basic_set
*)
4572 isl_basic_map_order_divs((struct isl_basic_map
*)bset
);
4575 /* Look for a div in dst that corresponds to the div "div" in src.
4576 * The divs before "div" in src and dst are assumed to be the same.
4578 * Returns -1 if no corresponding div was found and the position
4579 * of the corresponding div in dst otherwise.
4581 static int find_div(struct isl_basic_map
*dst
,
4582 struct isl_basic_map
*src
, unsigned div
)
4586 unsigned total
= isl_dim_total(src
->dim
);
4588 isl_assert(dst
->ctx
, div
<= dst
->n_div
, return -1);
4589 for (i
= div
; i
< dst
->n_div
; ++i
)
4590 if (isl_seq_eq(dst
->div
[i
], src
->div
[div
], 1+1+total
+div
) &&
4591 isl_seq_first_non_zero(dst
->div
[i
]+1+1+total
+div
,
4592 dst
->n_div
- div
) == -1)
4597 struct isl_basic_map
*isl_basic_map_align_divs(
4598 struct isl_basic_map
*dst
, struct isl_basic_map
*src
)
4601 unsigned total
= isl_dim_total(src
->dim
);
4606 if (src
->n_div
== 0)
4609 for (i
= 0; i
< src
->n_div
; ++i
)
4610 isl_assert(src
->ctx
, !isl_int_is_zero(src
->div
[i
][0]), goto error
);
4612 src
= isl_basic_map_order_divs(src
);
4613 dst
= isl_basic_map_cow(dst
);
4614 dst
= isl_basic_map_extend_dim(dst
, isl_dim_copy(dst
->dim
),
4615 src
->n_div
, 0, 2 * src
->n_div
);
4618 for (i
= 0; i
< src
->n_div
; ++i
) {
4619 int j
= find_div(dst
, src
, i
);
4621 j
= isl_basic_map_alloc_div(dst
);
4624 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
+i
);
4625 isl_seq_clr(dst
->div
[j
]+1+1+total
+i
, dst
->n_div
- i
);
4626 if (add_div_constraints(dst
, j
) < 0)
4630 isl_basic_map_swap_div(dst
, i
, j
);
4634 isl_basic_map_free(dst
);
4638 struct isl_basic_set
*isl_basic_set_align_divs(
4639 struct isl_basic_set
*dst
, struct isl_basic_set
*src
)
4641 return (struct isl_basic_set
*)isl_basic_map_align_divs(
4642 (struct isl_basic_map
*)dst
, (struct isl_basic_map
*)src
);
4645 struct isl_map
*isl_map_align_divs(struct isl_map
*map
)
4653 map
= isl_map_compute_divs(map
);
4654 map
= isl_map_cow(map
);
4658 for (i
= 1; i
< map
->n
; ++i
)
4659 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], map
->p
[i
]);
4660 for (i
= 1; i
< map
->n
; ++i
)
4661 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], map
->p
[0]);
4663 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4667 struct isl_set
*isl_set_align_divs(struct isl_set
*set
)
4669 return (struct isl_set
*)isl_map_align_divs((struct isl_map
*)set
);
4672 static struct isl_map
*add_cut_constraint(struct isl_map
*dst
,
4673 struct isl_basic_map
*src
, isl_int
*c
,
4674 unsigned len
, int oppose
)
4676 struct isl_basic_map
*copy
= NULL
;
4681 copy
= isl_basic_map_copy(src
);
4682 copy
= isl_basic_map_cow(copy
);
4685 copy
= isl_basic_map_extend_constraints(copy
, 0, 1);
4686 k
= isl_basic_map_alloc_inequality(copy
);
4690 isl_seq_neg(copy
->ineq
[k
], c
, len
);
4692 isl_seq_cpy(copy
->ineq
[k
], c
, len
);
4693 total
= 1 + isl_basic_map_total_dim(copy
);
4694 isl_seq_clr(copy
->ineq
[k
]+len
, total
- len
);
4695 isl_inequality_negate(copy
, k
);
4696 copy
= isl_basic_map_simplify(copy
);
4697 copy
= isl_basic_map_finalize(copy
);
4698 is_empty
= isl_basic_map_is_empty(copy
);
4702 dst
= isl_map_add(dst
, copy
);
4704 isl_basic_map_free(copy
);
4707 isl_basic_map_free(copy
);
4712 static struct isl_map
*subtract(struct isl_map
*map
, struct isl_basic_map
*bmap
)
4716 struct isl_map
*rest
= NULL
;
4718 unsigned total
= isl_basic_map_total_dim(bmap
);
4720 map
= isl_map_cow(map
);
4725 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
4726 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
4728 max
= map
->n
* (2 * bmap
->n_eq
+ bmap
->n_ineq
);
4729 rest
= isl_map_alloc_dim(isl_dim_copy(map
->dim
), max
, flags
);
4733 for (i
= 0; i
< map
->n
; ++i
) {
4734 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], bmap
);
4739 for (j
= 0; j
< map
->n
; ++j
)
4740 map
->p
[j
] = isl_basic_map_cow(map
->p
[j
]);
4742 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
4743 for (j
= 0; j
< map
->n
; ++j
) {
4744 rest
= add_cut_constraint(rest
,
4745 map
->p
[j
], bmap
->eq
[i
], 1+total
, 0);
4749 rest
= add_cut_constraint(rest
,
4750 map
->p
[j
], bmap
->eq
[i
], 1+total
, 1);
4754 map
->p
[j
] = isl_basic_map_extend_constraints(map
->p
[j
],
4758 k
= isl_basic_map_alloc_equality(map
->p
[j
]);
4761 isl_seq_cpy(map
->p
[j
]->eq
[k
], bmap
->eq
[i
], 1+total
);
4762 isl_seq_clr(map
->p
[j
]->eq
[k
]+1+total
,
4763 map
->p
[j
]->n_div
- bmap
->n_div
);
4767 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
4768 for (j
= 0; j
< map
->n
; ++j
) {
4769 rest
= add_cut_constraint(rest
,
4770 map
->p
[j
], bmap
->ineq
[i
], 1+total
, 0);
4774 map
->p
[j
] = isl_basic_map_extend_constraints(map
->p
[j
],
4778 k
= isl_basic_map_alloc_inequality(map
->p
[j
]);
4781 isl_seq_cpy(map
->p
[j
]->ineq
[k
], bmap
->ineq
[i
], 1+total
);
4782 isl_seq_clr(map
->p
[j
]->ineq
[k
]+1+total
,
4783 map
->p
[j
]->n_div
- bmap
->n_div
);
4795 struct isl_map
*isl_map_subtract(struct isl_map
*map1
, struct isl_map
*map2
)
4801 isl_assert(map1
->ctx
, isl_dim_equal(map1
->dim
, map2
->dim
), goto error
);
4803 if (isl_map_is_empty(map2
)) {
4808 map1
= isl_map_compute_divs(map1
);
4809 map2
= isl_map_compute_divs(map2
);
4813 map1
= isl_map_remove_empty_parts(map1
);
4814 map2
= isl_map_remove_empty_parts(map2
);
4816 for (i
= 0; map1
&& i
< map2
->n
; ++i
)
4817 map1
= subtract(map1
, map2
->p
[i
]);
4827 struct isl_set
*isl_set_subtract(struct isl_set
*set1
, struct isl_set
*set2
)
4829 return (struct isl_set
*)
4831 (struct isl_map
*)set1
, (struct isl_map
*)set2
);
4834 struct isl_set
*isl_set_apply(struct isl_set
*set
, struct isl_map
*map
)
4838 isl_assert(set
->ctx
, isl_map_compatible_domain(map
, set
), goto error
);
4839 map
= isl_map_intersect_domain(map
, set
);
4840 set
= isl_map_range(map
);
4848 /* There is no need to cow as removing empty parts doesn't change
4849 * the meaning of the set.
4851 struct isl_map
*isl_map_remove_empty_parts(struct isl_map
*map
)
4858 for (i
= map
->n
-1; i
>= 0; --i
) {
4859 if (!ISL_F_ISSET(map
->p
[i
], ISL_BASIC_MAP_EMPTY
))
4861 isl_basic_map_free(map
->p
[i
]);
4862 if (i
!= map
->n
-1) {
4863 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4864 map
->p
[i
] = map
->p
[map
->n
-1];
4872 struct isl_set
*isl_set_remove_empty_parts(struct isl_set
*set
)
4874 return (struct isl_set
*)
4875 isl_map_remove_empty_parts((struct isl_map
*)set
);
4878 struct isl_basic_map
*isl_map_copy_basic_map(struct isl_map
*map
)
4880 struct isl_basic_map
*bmap
;
4881 if (!map
|| map
->n
== 0)
4883 bmap
= map
->p
[map
->n
-1];
4884 isl_assert(map
->ctx
, ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
), return NULL
);
4885 return isl_basic_map_copy(bmap
);
4888 struct isl_basic_set
*isl_set_copy_basic_set(struct isl_set
*set
)
4890 return (struct isl_basic_set
*)
4891 isl_map_copy_basic_map((struct isl_map
*)set
);
4894 __isl_give isl_map
*isl_map_drop_basic_map(__isl_take isl_map
*map
,
4895 __isl_keep isl_basic_map
*bmap
)
4901 for (i
= map
->n
-1; i
>= 0; --i
) {
4902 if (map
->p
[i
] != bmap
)
4904 map
= isl_map_cow(map
);
4907 isl_basic_map_free(map
->p
[i
]);
4908 if (i
!= map
->n
-1) {
4909 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
4910 map
->p
[i
] = map
->p
[map
->n
-1];
4921 struct isl_set
*isl_set_drop_basic_set(struct isl_set
*set
,
4922 struct isl_basic_set
*bset
)
4924 return (struct isl_set
*)isl_map_drop_basic_map((struct isl_map
*)set
,
4925 (struct isl_basic_map
*)bset
);
4928 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
4929 * for any common value of the parameters and dimensions preceding dim
4930 * in both basic sets, the values of dimension pos in bset1 are
4931 * smaller or larger than those in bset2.
4934 * 1 if bset1 follows bset2
4935 * -1 if bset1 precedes bset2
4936 * 0 if bset1 and bset2 are incomparable
4937 * -2 if some error occurred.
4939 int isl_basic_set_compare_at(struct isl_basic_set
*bset1
,
4940 struct isl_basic_set
*bset2
, int pos
)
4942 struct isl_dim
*dims
;
4943 struct isl_basic_map
*bmap1
= NULL
;
4944 struct isl_basic_map
*bmap2
= NULL
;
4945 struct isl_ctx
*ctx
;
4946 struct isl_vec
*obj
;
4949 unsigned dim1
, dim2
;
4951 enum isl_lp_result res
;
4954 if (!bset1
|| !bset2
)
4957 nparam
= isl_basic_set_n_param(bset1
);
4958 dim1
= isl_basic_set_n_dim(bset1
);
4959 dim2
= isl_basic_set_n_dim(bset2
);
4960 dims
= isl_dim_alloc(bset1
->ctx
, nparam
, pos
, dim1
- pos
);
4961 bmap1
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset1
), dims
);
4962 dims
= isl_dim_alloc(bset2
->ctx
, nparam
, pos
, dim2
- pos
);
4963 bmap2
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset2
), dims
);
4964 if (!bmap1
|| !bmap2
)
4966 bmap1
= isl_basic_map_cow(bmap1
);
4967 bmap1
= isl_basic_map_extend(bmap1
, nparam
,
4968 pos
, (dim1
- pos
) + (dim2
- pos
),
4969 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
4970 bmap1
= add_constraints(bmap1
, bmap2
, 0, dim1
- pos
);
4973 total
= isl_basic_map_total_dim(bmap1
);
4975 obj
= isl_vec_alloc(ctx
, 1 + total
);
4976 isl_seq_clr(obj
->block
.data
, 1 + total
);
4977 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
], 1);
4978 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
+(dim1
-pos
)], -1);
4983 res
= isl_basic_map_solve_lp(bmap1
, 0, obj
->block
.data
, ctx
->one
,
4985 if (res
== isl_lp_empty
)
4987 else if (res
== isl_lp_ok
&& isl_int_is_pos(num
))
4989 else if ((res
== isl_lp_ok
&& isl_int_is_neg(num
)) ||
4990 res
== isl_lp_unbounded
)
4996 isl_basic_map_free(bmap1
);
5000 isl_basic_map_free(bmap1
);
5001 isl_basic_map_free(bmap2
);
5005 static int isl_basic_map_fast_has_fixed_var(struct isl_basic_map
*bmap
,
5006 unsigned pos
, isl_int
*val
)
5014 total
= isl_basic_map_total_dim(bmap
);
5015 for (i
= 0, d
= total
-1; i
< bmap
->n_eq
&& d
+1 > pos
; ++i
) {
5016 for (; d
+1 > pos
; --d
)
5017 if (!isl_int_is_zero(bmap
->eq
[i
][1+d
]))
5021 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1, d
) != -1)
5023 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1+d
+1, total
-d
-1) != -1)
5025 if (!isl_int_is_one(bmap
->eq
[i
][1+d
]))
5028 isl_int_neg(*val
, bmap
->eq
[i
][0]);
5034 static int isl_map_fast_has_fixed_var(struct isl_map
*map
,
5035 unsigned pos
, isl_int
*val
)
5047 return isl_basic_map_fast_has_fixed_var(map
->p
[0], pos
, val
);
5050 fixed
= isl_basic_map_fast_has_fixed_var(map
->p
[0], pos
, &v
);
5051 for (i
= 1; fixed
== 1 && i
< map
->n
; ++i
) {
5052 fixed
= isl_basic_map_fast_has_fixed_var(map
->p
[i
], pos
, &tmp
);
5053 if (fixed
== 1 && isl_int_ne(tmp
, v
))
5057 isl_int_set(*val
, v
);
5063 static int isl_basic_set_fast_has_fixed_var(struct isl_basic_set
*bset
,
5064 unsigned pos
, isl_int
*val
)
5066 return isl_basic_map_fast_has_fixed_var((struct isl_basic_map
*)bset
,
5070 static int isl_set_fast_has_fixed_var(struct isl_set
*set
, unsigned pos
,
5073 return isl_map_fast_has_fixed_var((struct isl_map
*)set
, pos
, val
);
5076 int isl_basic_map_fast_is_fixed(struct isl_basic_map
*bmap
,
5077 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
5079 if (pos
>= isl_basic_map_dim(bmap
, type
))
5081 return isl_basic_map_fast_has_fixed_var(bmap
,
5082 isl_basic_map_offset(bmap
, type
) - 1 + pos
, val
);
5085 int isl_map_fast_is_fixed(struct isl_map
*map
,
5086 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
5088 if (pos
>= isl_map_dim(map
, type
))
5090 return isl_map_fast_has_fixed_var(map
,
5091 map_offset(map
, type
) - 1 + pos
, val
);
5094 /* Check if dimension dim has fixed value and if so and if val is not NULL,
5095 * then return this fixed value in *val.
5097 int isl_basic_set_fast_dim_is_fixed(struct isl_basic_set
*bset
, unsigned dim
,
5100 return isl_basic_set_fast_has_fixed_var(bset
,
5101 isl_basic_set_n_param(bset
) + dim
, val
);
5104 /* Check if dimension dim has fixed value and if so and if val is not NULL,
5105 * then return this fixed value in *val.
5107 int isl_set_fast_dim_is_fixed(struct isl_set
*set
, unsigned dim
, isl_int
*val
)
5109 return isl_set_fast_has_fixed_var(set
, isl_set_n_param(set
) + dim
, val
);
5112 /* Check if input variable in has fixed value and if so and if val is not NULL,
5113 * then return this fixed value in *val.
5115 int isl_map_fast_input_is_fixed(struct isl_map
*map
, unsigned in
, isl_int
*val
)
5117 return isl_map_fast_has_fixed_var(map
, isl_map_n_param(map
) + in
, val
);
5120 /* Check if dimension dim has an (obvious) fixed lower bound and if so
5121 * and if val is not NULL, then return this lower bound in *val.
5123 int isl_basic_set_fast_dim_has_fixed_lower_bound(struct isl_basic_set
*bset
,
5124 unsigned dim
, isl_int
*val
)
5126 int i
, i_eq
= -1, i_ineq
= -1;
5133 total
= isl_basic_set_total_dim(bset
);
5134 nparam
= isl_basic_set_n_param(bset
);
5135 for (i
= 0; i
< bset
->n_eq
; ++i
) {
5136 if (isl_int_is_zero(bset
->eq
[i
][1+nparam
+dim
]))
5142 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
5143 if (!isl_int_is_pos(bset
->ineq
[i
][1+nparam
+dim
]))
5145 if (i_eq
!= -1 || i_ineq
!= -1)
5149 if (i_eq
== -1 && i_ineq
== -1)
5151 c
= i_eq
!= -1 ? bset
->eq
[i_eq
] : bset
->ineq
[i_ineq
];
5152 /* The coefficient should always be one due to normalization. */
5153 if (!isl_int_is_one(c
[1+nparam
+dim
]))
5155 if (isl_seq_first_non_zero(c
+1, nparam
+dim
) != -1)
5157 if (isl_seq_first_non_zero(c
+1+nparam
+dim
+1,
5158 total
- nparam
- dim
- 1) != -1)
5161 isl_int_neg(*val
, c
[0]);
5165 int isl_set_fast_dim_has_fixed_lower_bound(struct isl_set
*set
,
5166 unsigned dim
, isl_int
*val
)
5178 return isl_basic_set_fast_dim_has_fixed_lower_bound(set
->p
[0],
5182 fixed
= isl_basic_set_fast_dim_has_fixed_lower_bound(set
->p
[0],
5184 for (i
= 1; fixed
== 1 && i
< set
->n
; ++i
) {
5185 fixed
= isl_basic_set_fast_dim_has_fixed_lower_bound(set
->p
[i
],
5187 if (fixed
== 1 && isl_int_ne(tmp
, v
))
5191 isl_int_set(*val
, v
);
5202 static int qsort_constraint_cmp(const void *p1
, const void *p2
)
5204 const struct constraint
*c1
= (const struct constraint
*)p1
;
5205 const struct constraint
*c2
= (const struct constraint
*)p2
;
5206 unsigned size
= isl_min(c1
->size
, c2
->size
);
5207 return isl_seq_cmp(c1
->c
, c2
->c
, size
);
5210 static struct isl_basic_map
*isl_basic_map_sort_constraints(
5211 struct isl_basic_map
*bmap
)
5214 struct constraint
*c
;
5219 total
= isl_basic_map_total_dim(bmap
);
5220 c
= isl_alloc_array(bmap
->ctx
, struct constraint
, bmap
->n_ineq
);
5223 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
5225 c
[i
].c
= bmap
->ineq
[i
];
5227 qsort(c
, bmap
->n_ineq
, sizeof(struct constraint
), qsort_constraint_cmp
);
5228 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
5229 bmap
->ineq
[i
] = c
[i
].c
;
5233 isl_basic_map_free(bmap
);
5237 struct isl_basic_map
*isl_basic_map_normalize(struct isl_basic_map
*bmap
)
5241 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED
))
5243 bmap
= isl_basic_map_convex_hull(bmap
);
5244 bmap
= isl_basic_map_sort_constraints(bmap
);
5245 ISL_F_SET(bmap
, ISL_BASIC_MAP_NORMALIZED
);
5249 struct isl_basic_set
*isl_basic_set_normalize(struct isl_basic_set
*bset
)
5251 return (struct isl_basic_set
*)isl_basic_map_normalize(
5252 (struct isl_basic_map
*)bset
);
5255 static int isl_basic_map_fast_cmp(const struct isl_basic_map
*bmap1
,
5256 const struct isl_basic_map
*bmap2
)
5263 if (isl_basic_map_n_param(bmap1
) != isl_basic_map_n_param(bmap2
))
5264 return isl_basic_map_n_param(bmap1
) - isl_basic_map_n_param(bmap2
);
5265 if (isl_basic_map_n_in(bmap1
) != isl_basic_map_n_in(bmap2
))
5266 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
5267 if (isl_basic_map_n_out(bmap1
) != isl_basic_map_n_out(bmap2
))
5268 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
5269 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
) &&
5270 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
5272 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
))
5274 if (ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
5276 if (bmap1
->n_eq
!= bmap2
->n_eq
)
5277 return bmap1
->n_eq
- bmap2
->n_eq
;
5278 if (bmap1
->n_ineq
!= bmap2
->n_ineq
)
5279 return bmap1
->n_ineq
- bmap2
->n_ineq
;
5280 if (bmap1
->n_div
!= bmap2
->n_div
)
5281 return bmap1
->n_div
- bmap2
->n_div
;
5282 total
= isl_basic_map_total_dim(bmap1
);
5283 for (i
= 0; i
< bmap1
->n_eq
; ++i
) {
5284 cmp
= isl_seq_cmp(bmap1
->eq
[i
], bmap2
->eq
[i
], 1+total
);
5288 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
5289 cmp
= isl_seq_cmp(bmap1
->ineq
[i
], bmap2
->ineq
[i
], 1+total
);
5293 for (i
= 0; i
< bmap1
->n_div
; ++i
) {
5294 cmp
= isl_seq_cmp(bmap1
->div
[i
], bmap2
->div
[i
], 1+1+total
);
5301 static int isl_basic_map_fast_is_equal(struct isl_basic_map
*bmap1
,
5302 struct isl_basic_map
*bmap2
)
5304 return isl_basic_map_fast_cmp(bmap1
, bmap2
) == 0;
5307 static int qsort_bmap_cmp(const void *p1
, const void *p2
)
5309 const struct isl_basic_map
*bmap1
= *(const struct isl_basic_map
**)p1
;
5310 const struct isl_basic_map
*bmap2
= *(const struct isl_basic_map
**)p2
;
5312 return isl_basic_map_fast_cmp(bmap1
, bmap2
);
5315 /* We normalize in place, but if anything goes wrong we need
5316 * to return NULL, so we need to make sure we don't change the
5317 * meaning of any possible other copies of map.
5319 struct isl_map
*isl_map_normalize(struct isl_map
*map
)
5322 struct isl_basic_map
*bmap
;
5326 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
))
5328 for (i
= 0; i
< map
->n
; ++i
) {
5329 bmap
= isl_basic_map_normalize(isl_basic_map_copy(map
->p
[i
]));
5332 isl_basic_map_free(map
->p
[i
]);
5335 qsort(map
->p
, map
->n
, sizeof(struct isl_basic_map
*), qsort_bmap_cmp
);
5336 ISL_F_SET(map
, ISL_MAP_NORMALIZED
);
5337 map
= isl_map_remove_empty_parts(map
);
5340 for (i
= map
->n
- 1; i
>= 1; --i
) {
5341 if (!isl_basic_map_fast_is_equal(map
->p
[i
-1], map
->p
[i
]))
5343 isl_basic_map_free(map
->p
[i
-1]);
5344 for (j
= i
; j
< map
->n
; ++j
)
5345 map
->p
[j
-1] = map
->p
[j
];
5355 struct isl_set
*isl_set_normalize(struct isl_set
*set
)
5357 return (struct isl_set
*)isl_map_normalize((struct isl_map
*)set
);
5360 int isl_map_fast_is_equal(struct isl_map
*map1
, struct isl_map
*map2
)
5370 if (!isl_dim_equal(map1
->dim
, map2
->dim
))
5373 map1
= isl_map_copy(map1
);
5374 map2
= isl_map_copy(map2
);
5375 map1
= isl_map_normalize(map1
);
5376 map2
= isl_map_normalize(map2
);
5379 equal
= map1
->n
== map2
->n
;
5380 for (i
= 0; equal
&& i
< map1
->n
; ++i
) {
5381 equal
= isl_basic_map_fast_is_equal(map1
->p
[i
], map2
->p
[i
]);
5394 int isl_set_fast_is_equal(struct isl_set
*set1
, struct isl_set
*set2
)
5396 return isl_map_fast_is_equal((struct isl_map
*)set1
,
5397 (struct isl_map
*)set2
);
5400 /* Return an interval that ranges from min to max (inclusive)
5402 struct isl_basic_set
*isl_basic_set_interval(struct isl_ctx
*ctx
,
5403 isl_int min
, isl_int max
)
5406 struct isl_basic_set
*bset
= NULL
;
5408 bset
= isl_basic_set_alloc(ctx
, 0, 1, 0, 0, 2);
5412 k
= isl_basic_set_alloc_inequality(bset
);
5415 isl_int_set_si(bset
->ineq
[k
][1], 1);
5416 isl_int_neg(bset
->ineq
[k
][0], min
);
5418 k
= isl_basic_set_alloc_inequality(bset
);
5421 isl_int_set_si(bset
->ineq
[k
][1], -1);
5422 isl_int_set(bset
->ineq
[k
][0], max
);
5426 isl_basic_set_free(bset
);
5430 /* Return the Cartesian product of the basic sets in list (in the given order).
5432 struct isl_basic_set
*isl_basic_set_product(struct isl_basic_set_list
*list
)
5440 struct isl_basic_set
*product
= NULL
;
5444 isl_assert(list
->ctx
, list
->n
> 0, goto error
);
5445 isl_assert(list
->ctx
, list
->p
[0], goto error
);
5446 nparam
= isl_basic_set_n_param(list
->p
[0]);
5447 dim
= isl_basic_set_n_dim(list
->p
[0]);
5448 extra
= list
->p
[0]->n_div
;
5449 n_eq
= list
->p
[0]->n_eq
;
5450 n_ineq
= list
->p
[0]->n_ineq
;
5451 for (i
= 1; i
< list
->n
; ++i
) {
5452 isl_assert(list
->ctx
, list
->p
[i
], goto error
);
5453 isl_assert(list
->ctx
,
5454 nparam
== isl_basic_set_n_param(list
->p
[i
]), goto error
);
5455 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
5456 extra
+= list
->p
[i
]->n_div
;
5457 n_eq
+= list
->p
[i
]->n_eq
;
5458 n_ineq
+= list
->p
[i
]->n_ineq
;
5460 product
= isl_basic_set_alloc(list
->ctx
, nparam
, dim
, extra
,
5465 for (i
= 0; i
< list
->n
; ++i
) {
5466 isl_basic_set_add_constraints(product
,
5467 isl_basic_set_copy(list
->p
[i
]), dim
);
5468 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
5470 isl_basic_set_list_free(list
);
5473 isl_basic_set_free(product
);
5474 isl_basic_set_list_free(list
);
5478 struct isl_basic_map
*isl_basic_map_product(
5479 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
5481 struct isl_dim
*dim_result
= NULL
;
5482 struct isl_basic_map
*bmap
;
5483 unsigned in1
, in2
, out1
, out2
, nparam
, total
, pos
;
5484 struct isl_dim_map
*dim_map1
, *dim_map2
;
5486 if (!bmap1
|| !bmap2
)
5489 isl_assert(bmap1
->ctx
, isl_dim_match(bmap1
->dim
, isl_dim_param
,
5490 bmap2
->dim
, isl_dim_param
), goto error
);
5491 dim_result
= isl_dim_product(isl_dim_copy(bmap1
->dim
),
5492 isl_dim_copy(bmap2
->dim
));
5494 in1
= isl_basic_map_n_in(bmap1
);
5495 in2
= isl_basic_map_n_in(bmap2
);
5496 out1
= isl_basic_map_n_out(bmap1
);
5497 out2
= isl_basic_map_n_out(bmap2
);
5498 nparam
= isl_basic_map_n_param(bmap1
);
5500 total
= nparam
+ in1
+ in2
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
5501 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
5502 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
5503 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
5504 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
5505 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
5506 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
5507 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
5508 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
5509 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
5510 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
5512 bmap
= isl_basic_map_alloc_dim(dim_result
,
5513 bmap1
->n_div
+ bmap2
->n_div
,
5514 bmap1
->n_eq
+ bmap2
->n_eq
,
5515 bmap1
->n_ineq
+ bmap2
->n_ineq
);
5516 bmap
= add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
5517 bmap
= add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
5518 bmap
= isl_basic_map_simplify(bmap
);
5519 return isl_basic_map_finalize(bmap
);
5521 isl_basic_map_free(bmap1
);
5522 isl_basic_map_free(bmap2
);
5526 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
5528 struct isl_map
*isl_map_product(struct isl_map
*map1
, struct isl_map
*map2
)
5531 struct isl_map
*result
;
5537 isl_assert(map1
->ctx
, isl_dim_match(map1
->dim
, isl_dim_param
,
5538 map2
->dim
, isl_dim_param
), goto error
);
5540 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
5541 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
5542 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
5544 result
= isl_map_alloc_dim(isl_dim_product(isl_dim_copy(map1
->dim
),
5545 isl_dim_copy(map2
->dim
)),
5546 map1
->n
* map2
->n
, flags
);
5549 for (i
= 0; i
< map1
->n
; ++i
)
5550 for (j
= 0; j
< map2
->n
; ++j
) {
5551 struct isl_basic_map
*part
;
5552 part
= isl_basic_map_product(
5553 isl_basic_map_copy(map1
->p
[i
]),
5554 isl_basic_map_copy(map2
->p
[j
]));
5555 if (isl_basic_map_is_empty(part
))
5556 isl_basic_map_free(part
);
5558 result
= isl_map_add(result
, part
);
5571 /* Given two set A and B, construct its Cartesian product A x B.
5573 struct isl_set
*isl_set_product(struct isl_set
*set1
, struct isl_set
*set2
)
5575 return (struct isl_set
*)isl_map_product((struct isl_map
*)set1
,
5576 (struct isl_map
*)set2
);
5579 uint32_t isl_basic_set_get_hash(struct isl_basic_set
*bset
)
5582 uint32_t hash
= isl_hash_init();
5587 bset
= isl_basic_set_copy(bset
);
5588 bset
= isl_basic_set_normalize(bset
);
5591 total
= isl_basic_set_total_dim(bset
);
5592 isl_hash_byte(hash
, bset
->n_eq
& 0xFF);
5593 for (i
= 0; i
< bset
->n_eq
; ++i
) {
5595 c_hash
= isl_seq_get_hash(bset
->eq
[i
], 1 + total
);
5596 isl_hash_hash(hash
, c_hash
);
5598 isl_hash_byte(hash
, bset
->n_ineq
& 0xFF);
5599 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
5601 c_hash
= isl_seq_get_hash(bset
->ineq
[i
], 1 + total
);
5602 isl_hash_hash(hash
, c_hash
);
5604 isl_hash_byte(hash
, bset
->n_div
& 0xFF);
5605 for (i
= 0; i
< bset
->n_div
; ++i
) {
5607 if (isl_int_is_zero(bset
->div
[i
][0]))
5609 isl_hash_byte(hash
, i
& 0xFF);
5610 c_hash
= isl_seq_get_hash(bset
->div
[i
], 1 + 1 + total
);
5611 isl_hash_hash(hash
, c_hash
);
5613 isl_basic_set_free(bset
);
5617 uint32_t isl_set_get_hash(struct isl_set
*set
)
5624 set
= isl_set_copy(set
);
5625 set
= isl_set_normalize(set
);
5629 hash
= isl_hash_init();
5630 for (i
= 0; i
< set
->n
; ++i
) {
5632 bset_hash
= isl_basic_set_get_hash(set
->p
[i
]);
5633 isl_hash_hash(hash
, bset_hash
);
5641 /* Check if the value for dimension dim is completely determined
5642 * by the values of the other parameters and variables.
5643 * That is, check if dimension dim is involved in an equality.
5645 int isl_basic_set_dim_is_unique(struct isl_basic_set
*bset
, unsigned dim
)
5652 nparam
= isl_basic_set_n_param(bset
);
5653 for (i
= 0; i
< bset
->n_eq
; ++i
)
5654 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ dim
]))
5659 /* Check if the value for dimension dim is completely determined
5660 * by the values of the other parameters and variables.
5661 * That is, check if dimension dim is involved in an equality
5662 * for each of the subsets.
5664 int isl_set_dim_is_unique(struct isl_set
*set
, unsigned dim
)
5670 for (i
= 0; i
< set
->n
; ++i
) {
5672 unique
= isl_basic_set_dim_is_unique(set
->p
[i
], dim
);
5679 int isl_map_foreach_basic_map(__isl_keep isl_map
*map
,
5680 int (*fn
)(__isl_take isl_basic_map
*bmap
, void *user
), void *user
)
5687 for (i
= 0; i
< map
->n
; ++i
)
5688 if (fn(isl_basic_map_copy(map
->p
[i
]), user
) < 0)
5694 int isl_set_foreach_basic_set(__isl_keep isl_set
*set
,
5695 int (*fn
)(__isl_take isl_basic_set
*bset
, void *user
), void *user
)
5702 for (i
= 0; i
< set
->n
; ++i
)
5703 if (fn(isl_basic_set_copy(set
->p
[i
]), user
) < 0)
5709 __isl_give isl_map
*isl_set_lifting(__isl_take isl_set
*set
)
5711 struct isl_dim
*dim
;
5712 struct isl_basic_map
*bmap
;
5719 set
= isl_set_align_divs(set
);
5724 dim
= isl_set_get_dim(set
);
5725 if (set
->n
== 0 || set
->p
[0]->n_div
== 0) {
5727 return isl_map_identity(dim
);
5730 n_div
= set
->p
[0]->n_div
;
5731 dim
= isl_dim_map(dim
);
5732 n_param
= isl_dim_size(dim
, isl_dim_param
);
5733 n_set
= isl_dim_size(dim
, isl_dim_in
);
5734 dim
= isl_dim_extend(dim
, n_param
, n_set
, n_set
+ n_div
);
5735 bmap
= isl_basic_map_alloc_dim(dim
, 0, n_set
, 2 * n_div
);
5736 for (i
= 0; i
< n_set
; ++i
)
5737 bmap
= var_equal(bmap
, i
);
5739 total
= n_param
+ n_set
+ n_set
+ n_div
;
5740 for (i
= 0; i
< n_div
; ++i
) {
5741 k
= isl_basic_map_alloc_inequality(bmap
);
5744 isl_seq_cpy(bmap
->ineq
[k
], set
->p
[0]->div
[i
]+1, 1+n_param
);
5745 isl_seq_clr(bmap
->ineq
[k
]+1+n_param
, n_set
);
5746 isl_seq_cpy(bmap
->ineq
[k
]+1+n_param
+n_set
,
5747 set
->p
[0]->div
[i
]+1+1+n_param
, n_set
+ n_div
);
5748 isl_int_neg(bmap
->ineq
[k
][1+n_param
+n_set
+n_set
+i
],
5749 set
->p
[0]->div
[i
][0]);
5751 l
= isl_basic_map_alloc_inequality(bmap
);
5754 isl_seq_neg(bmap
->ineq
[l
], bmap
->ineq
[k
], 1 + total
);
5755 isl_int_add(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0],
5756 set
->p
[0]->div
[i
][0]);
5757 isl_int_sub_ui(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], 1);
5761 return isl_map_from_basic_map(bmap
);
5764 isl_basic_map_free(bmap
);
5768 int isl_basic_set_size(__isl_keep isl_basic_set
*bset
)
5776 dim
= isl_basic_set_total_dim(bset
);
5777 size
+= bset
->n_eq
* (1 + dim
);
5778 size
+= bset
->n_ineq
* (1 + dim
);
5779 size
+= bset
->n_div
* (2 + dim
);
5784 int isl_set_size(__isl_keep isl_set
*set
)
5792 for (i
= 0; i
< set
->n
; ++i
)
5793 size
+= isl_basic_set_size(set
->p
[i
]);