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 int isl_basic_map_alloc_div(struct isl_basic_map
*bmap
)
647 isl_assert(bmap
->ctx
, bmap
->n_div
< bmap
->extra
, return -1);
648 isl_seq_clr(bmap
->div
[bmap
->n_div
] +
649 1 + 1 + isl_basic_map_total_dim(bmap
),
650 bmap
->extra
- bmap
->n_div
);
651 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
652 return bmap
->n_div
++;
655 int isl_basic_set_alloc_div(struct isl_basic_set
*bset
)
657 return isl_basic_map_alloc_div((struct isl_basic_map
*)bset
);
660 int isl_basic_map_free_div(struct isl_basic_map
*bmap
, unsigned n
)
664 isl_assert(bmap
->ctx
, n
<= bmap
->n_div
, return -1);
669 int isl_basic_set_free_div(struct isl_basic_set
*bset
, unsigned n
)
671 return isl_basic_map_free_div((struct isl_basic_map
*)bset
, n
);
674 /* Copy constraint from src to dst, putting the vars of src at offset
675 * dim_off in dst and the divs of src at offset div_off in dst.
676 * If both sets are actually map, then dim_off applies to the input
679 static void copy_constraint(struct isl_basic_map
*dst_map
, isl_int
*dst
,
680 struct isl_basic_map
*src_map
, isl_int
*src
,
681 unsigned in_off
, unsigned out_off
, unsigned div_off
)
683 unsigned src_nparam
= isl_basic_map_n_param(src_map
);
684 unsigned dst_nparam
= isl_basic_map_n_param(dst_map
);
685 unsigned src_in
= isl_basic_map_n_in(src_map
);
686 unsigned dst_in
= isl_basic_map_n_in(dst_map
);
687 unsigned src_out
= isl_basic_map_n_out(src_map
);
688 unsigned dst_out
= isl_basic_map_n_out(dst_map
);
689 isl_int_set(dst
[0], src
[0]);
690 isl_seq_cpy(dst
+1, src
+1, isl_min(dst_nparam
, src_nparam
));
691 if (dst_nparam
> src_nparam
)
692 isl_seq_clr(dst
+1+src_nparam
,
693 dst_nparam
- src_nparam
);
694 isl_seq_clr(dst
+1+dst_nparam
, in_off
);
695 isl_seq_cpy(dst
+1+dst_nparam
+in_off
,
697 isl_min(dst_in
-in_off
, src_in
));
698 if (dst_in
-in_off
> src_in
)
699 isl_seq_clr(dst
+1+dst_nparam
+in_off
+src_in
,
700 dst_in
- in_off
- src_in
);
701 isl_seq_clr(dst
+1+dst_nparam
+dst_in
, out_off
);
702 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+out_off
,
703 src
+1+src_nparam
+src_in
,
704 isl_min(dst_out
-out_off
, src_out
));
705 if (dst_out
-out_off
> src_out
)
706 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+out_off
+src_out
,
707 dst_out
- out_off
- src_out
);
708 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
, div_off
);
709 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+dst_out
+div_off
,
710 src
+1+src_nparam
+src_in
+src_out
,
711 isl_min(dst_map
->extra
-div_off
, src_map
->n_div
));
712 if (dst_map
->n_div
-div_off
> src_map
->n_div
)
713 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
+
714 div_off
+src_map
->n_div
,
715 dst_map
->n_div
- div_off
- src_map
->n_div
);
718 static void copy_div(struct isl_basic_map
*dst_map
, isl_int
*dst
,
719 struct isl_basic_map
*src_map
, isl_int
*src
,
720 unsigned in_off
, unsigned out_off
, unsigned div_off
)
722 isl_int_set(dst
[0], src
[0]);
723 copy_constraint(dst_map
, dst
+1, src_map
, src
+1, in_off
, out_off
, div_off
);
726 static struct isl_basic_map
*add_constraints(struct isl_basic_map
*bmap1
,
727 struct isl_basic_map
*bmap2
, unsigned i_pos
, unsigned o_pos
)
732 if (!bmap1
|| !bmap2
)
735 div_off
= bmap1
->n_div
;
737 for (i
= 0; i
< bmap2
->n_eq
; ++i
) {
738 int i1
= isl_basic_map_alloc_equality(bmap1
);
741 copy_constraint(bmap1
, bmap1
->eq
[i1
], bmap2
, bmap2
->eq
[i
],
742 i_pos
, o_pos
, div_off
);
745 for (i
= 0; i
< bmap2
->n_ineq
; ++i
) {
746 int i1
= isl_basic_map_alloc_inequality(bmap1
);
749 copy_constraint(bmap1
, bmap1
->ineq
[i1
], bmap2
, bmap2
->ineq
[i
],
750 i_pos
, o_pos
, div_off
);
753 for (i
= 0; i
< bmap2
->n_div
; ++i
) {
754 int i1
= isl_basic_map_alloc_div(bmap1
);
757 copy_div(bmap1
, bmap1
->div
[i1
], bmap2
, bmap2
->div
[i
],
758 i_pos
, o_pos
, div_off
);
761 isl_basic_map_free(bmap2
);
766 isl_basic_map_free(bmap1
);
767 isl_basic_map_free(bmap2
);
771 static void copy_constraint_dim_map(isl_int
*dst
, isl_int
*src
,
772 struct isl_dim_map
*dim_map
)
776 for (i
= 0; i
< dim_map
->len
; ++i
) {
777 if (dim_map
->pos
[i
] < 0)
778 isl_int_set_si(dst
[i
], 0);
780 isl_int_set(dst
[i
], src
[dim_map
->pos
[i
]]);
784 static void copy_div_dim_map(isl_int
*dst
, isl_int
*src
,
785 struct isl_dim_map
*dim_map
)
787 isl_int_set(dst
[0], src
[0]);
788 copy_constraint_dim_map(dst
+1, src
+1, dim_map
);
791 static struct isl_basic_map
*add_constraints_dim_map(struct isl_basic_map
*dst
,
792 struct isl_basic_map
*src
, struct isl_dim_map
*dim_map
)
796 if (!src
|| !dst
|| !dim_map
)
799 for (i
= 0; i
< src
->n_eq
; ++i
) {
800 int i1
= isl_basic_map_alloc_equality(dst
);
803 copy_constraint_dim_map(dst
->eq
[i1
], src
->eq
[i
], dim_map
);
806 for (i
= 0; i
< src
->n_ineq
; ++i
) {
807 int i1
= isl_basic_map_alloc_inequality(dst
);
810 copy_constraint_dim_map(dst
->ineq
[i1
], src
->ineq
[i
], dim_map
);
813 for (i
= 0; i
< src
->n_div
; ++i
) {
814 int i1
= isl_basic_map_alloc_div(dst
);
817 copy_div_dim_map(dst
->div
[i1
], src
->div
[i
], dim_map
);
821 isl_basic_map_free(src
);
826 isl_basic_map_free(src
);
827 isl_basic_map_free(dst
);
831 struct isl_basic_set
*isl_basic_set_add_constraints(struct isl_basic_set
*bset1
,
832 struct isl_basic_set
*bset2
, unsigned pos
)
834 return (struct isl_basic_set
*)
835 add_constraints((struct isl_basic_map
*)bset1
,
836 (struct isl_basic_map
*)bset2
, 0, pos
);
839 struct isl_basic_map
*isl_basic_map_extend_dim(struct isl_basic_map
*base
,
840 struct isl_dim
*dim
, unsigned extra
,
841 unsigned n_eq
, unsigned n_ineq
)
843 struct isl_basic_map
*ext
;
853 dims_ok
= isl_dim_equal(base
->dim
, dim
) &&
854 base
->extra
>= base
->n_div
+ extra
;
856 if (dims_ok
&& room_for_con(base
, n_eq
+ n_ineq
) &&
857 room_for_ineq(base
, n_ineq
)) {
862 isl_assert(base
->ctx
, base
->dim
->nparam
<= dim
->nparam
, goto error
);
863 isl_assert(base
->ctx
, base
->dim
->n_in
<= dim
->n_in
, goto error
);
864 isl_assert(base
->ctx
, base
->dim
->n_out
<= dim
->n_out
, goto error
);
865 extra
+= base
->extra
;
867 n_ineq
+= base
->n_ineq
;
869 ext
= isl_basic_map_alloc_dim(dim
, extra
, n_eq
, n_ineq
);
875 ext
->sample
= isl_vec_copy(base
->sample
);
877 ext
= add_constraints(ext
, base
, 0, 0);
880 ISL_F_CLR(ext
, ISL_BASIC_SET_FINAL
);
887 isl_basic_map_free(base
);
891 struct isl_basic_set
*isl_basic_set_extend_dim(struct isl_basic_set
*base
,
892 struct isl_dim
*dim
, unsigned extra
,
893 unsigned n_eq
, unsigned n_ineq
)
895 return (struct isl_basic_set
*)
896 isl_basic_map_extend_dim((struct isl_basic_map
*)base
, dim
,
897 extra
, n_eq
, n_ineq
);
900 struct isl_basic_map
*isl_basic_map_extend_constraints(
901 struct isl_basic_map
*base
, unsigned n_eq
, unsigned n_ineq
)
905 return isl_basic_map_extend_dim(base
, isl_dim_copy(base
->dim
),
909 struct isl_basic_map
*isl_basic_map_extend(struct isl_basic_map
*base
,
910 unsigned nparam
, unsigned n_in
, unsigned n_out
, unsigned extra
,
911 unsigned n_eq
, unsigned n_ineq
)
913 struct isl_basic_map
*bmap
;
918 dim
= isl_dim_alloc(base
->ctx
, nparam
, n_in
, n_out
);
922 bmap
= isl_basic_map_extend_dim(base
, dim
, extra
, n_eq
, n_ineq
);
926 struct isl_basic_set
*isl_basic_set_extend(struct isl_basic_set
*base
,
927 unsigned nparam
, unsigned dim
, unsigned extra
,
928 unsigned n_eq
, unsigned n_ineq
)
930 return (struct isl_basic_set
*)
931 isl_basic_map_extend((struct isl_basic_map
*)base
,
932 nparam
, 0, dim
, extra
, n_eq
, n_ineq
);
935 struct isl_basic_set
*isl_basic_set_extend_constraints(
936 struct isl_basic_set
*base
, unsigned n_eq
, unsigned n_ineq
)
938 return (struct isl_basic_set
*)
939 isl_basic_map_extend_constraints((struct isl_basic_map
*)base
,
943 struct isl_basic_set
*isl_basic_set_cow(struct isl_basic_set
*bset
)
945 return (struct isl_basic_set
*)
946 isl_basic_map_cow((struct isl_basic_map
*)bset
);
949 struct isl_basic_map
*isl_basic_map_cow(struct isl_basic_map
*bmap
)
956 bmap
= isl_basic_map_dup(bmap
);
958 ISL_F_CLR(bmap
, ISL_BASIC_SET_FINAL
);
962 struct isl_set
*isl_set_cow(struct isl_set
*set
)
970 return isl_set_dup(set
);
973 struct isl_map
*isl_map_cow(struct isl_map
*map
)
981 return isl_map_dup(map
);
984 static void swap_vars(struct isl_blk blk
, isl_int
*a
,
985 unsigned a_len
, unsigned b_len
)
987 isl_seq_cpy(blk
.data
, a
+a_len
, b_len
);
988 isl_seq_cpy(blk
.data
+b_len
, a
, a_len
);
989 isl_seq_cpy(a
, blk
.data
, b_len
+a_len
);
992 struct isl_basic_set
*isl_basic_set_swap_vars(
993 struct isl_basic_set
*bset
, unsigned n
)
1003 nparam
= isl_basic_set_n_param(bset
);
1004 dim
= isl_basic_set_n_dim(bset
);
1005 isl_assert(bset
->ctx
, n
<= dim
, goto error
);
1010 bset
= isl_basic_set_cow(bset
);
1014 blk
= isl_blk_alloc(bset
->ctx
, dim
);
1015 if (isl_blk_is_error(blk
))
1018 for (i
= 0; i
< bset
->n_eq
; ++i
)
1020 bset
->eq
[i
]+1+nparam
, n
, dim
- n
);
1022 for (i
= 0; i
< bset
->n_ineq
; ++i
)
1024 bset
->ineq
[i
]+1+nparam
, n
, dim
- n
);
1026 for (i
= 0; i
< bset
->n_div
; ++i
)
1028 bset
->div
[i
]+1+1+nparam
, n
, dim
- n
);
1030 isl_blk_free(bset
->ctx
, blk
);
1032 ISL_F_CLR(bset
, ISL_BASIC_SET_NORMALIZED
);
1036 isl_basic_set_free(bset
);
1040 struct isl_set
*isl_set_swap_vars(struct isl_set
*set
, unsigned n
)
1043 set
= isl_set_cow(set
);
1047 for (i
= 0; i
< set
->n
; ++i
) {
1048 set
->p
[i
] = isl_basic_set_swap_vars(set
->p
[i
], n
);
1054 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
1058 struct isl_basic_map
*isl_basic_map_set_to_empty(struct isl_basic_map
*bmap
)
1064 total
= isl_basic_map_total_dim(bmap
);
1065 isl_basic_map_free_div(bmap
, bmap
->n_div
);
1066 isl_basic_map_free_inequality(bmap
, bmap
->n_ineq
);
1068 isl_basic_map_free_equality(bmap
, bmap
->n_eq
-1);
1070 isl_basic_map_alloc_equality(bmap
);
1074 isl_int_set_si(bmap
->eq
[i
][0], 1);
1075 isl_seq_clr(bmap
->eq
[i
]+1, total
);
1076 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
1077 return isl_basic_map_finalize(bmap
);
1079 isl_basic_map_free(bmap
);
1083 struct isl_basic_set
*isl_basic_set_set_to_empty(struct isl_basic_set
*bset
)
1085 return (struct isl_basic_set
*)
1086 isl_basic_map_set_to_empty((struct isl_basic_map
*)bset
);
1089 void isl_basic_map_swap_div(struct isl_basic_map
*bmap
, int a
, int b
)
1092 unsigned off
= isl_dim_total(bmap
->dim
);
1093 isl_int
*t
= bmap
->div
[a
];
1094 bmap
->div
[a
] = bmap
->div
[b
];
1097 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1098 isl_int_swap(bmap
->eq
[i
][1+off
+a
], bmap
->eq
[i
][1+off
+b
]);
1100 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1101 isl_int_swap(bmap
->ineq
[i
][1+off
+a
], bmap
->ineq
[i
][1+off
+b
]);
1103 for (i
= 0; i
< bmap
->n_div
; ++i
)
1104 isl_int_swap(bmap
->div
[i
][1+1+off
+a
], bmap
->div
[i
][1+1+off
+b
]);
1105 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1108 /* Eliminate the specified n dimensions starting at first from the
1109 * constraints using Fourier-Motzkin, The dimensions themselves
1112 struct isl_set
*isl_set_eliminate_dims(struct isl_set
*set
,
1113 unsigned first
, unsigned n
)
1123 set
= isl_set_cow(set
);
1126 isl_assert(set
->ctx
, first
+n
<= isl_set_n_dim(set
), goto error
);
1127 nparam
= isl_set_n_param(set
);
1129 for (i
= 0; i
< set
->n
; ++i
) {
1130 set
->p
[i
] = isl_basic_set_eliminate_vars(set
->p
[i
],
1141 /* Project out n dimensions starting at first using Fourier-Motzkin */
1142 struct isl_set
*isl_set_remove_dims(struct isl_set
*set
,
1143 unsigned first
, unsigned n
)
1145 set
= isl_set_eliminate_dims(set
, first
, n
);
1146 set
= isl_set_drop_dims(set
, first
, n
);
1150 struct isl_basic_set
*isl_basic_set_remove_divs(struct isl_basic_set
*bset
)
1152 bset
= isl_basic_set_eliminate_vars(bset
, isl_dim_total(bset
->dim
),
1160 struct isl_set
*isl_set_remove_divs(struct isl_set
*set
)
1169 set
= isl_set_cow(set
);
1173 for (i
= 0; i
< set
->n
; ++i
) {
1174 set
->p
[i
] = isl_basic_set_remove_divs(set
->p
[i
]);
1184 struct isl_basic_map
*isl_basic_map_remove(struct isl_basic_map
*bmap
,
1185 enum isl_dim_type type
, unsigned first
, unsigned n
)
1189 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
1193 bmap
= isl_basic_map_eliminate_vars(bmap
,
1194 isl_basic_map_offset(bmap
, type
) - 1 + first
, n
);
1197 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
) && type
== isl_dim_div
)
1199 bmap
= isl_basic_map_drop(bmap
, type
, first
, n
);
1202 isl_basic_map_free(bmap
);
1206 struct isl_map
*isl_map_remove(struct isl_map
*map
,
1207 enum isl_dim_type type
, unsigned first
, unsigned n
)
1214 map
= isl_map_cow(map
);
1217 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
1219 for (i
= 0; i
< map
->n
; ++i
) {
1220 map
->p
[i
] = isl_basic_map_eliminate_vars(map
->p
[i
],
1221 isl_basic_map_offset(map
->p
[i
], type
) - 1 + first
, n
);
1225 map
= isl_map_drop(map
, type
, first
, n
);
1232 /* Project out n inputs starting at first using Fourier-Motzkin */
1233 struct isl_map
*isl_map_remove_inputs(struct isl_map
*map
,
1234 unsigned first
, unsigned n
)
1236 return isl_map_remove(map
, isl_dim_in
, first
, n
);
1239 /* Project out n dimensions starting at first using Fourier-Motzkin */
1240 struct isl_basic_set
*isl_basic_set_remove_dims(struct isl_basic_set
*bset
,
1241 unsigned first
, unsigned n
)
1243 unsigned nparam
= isl_basic_set_n_param(bset
);
1244 bset
= isl_basic_set_eliminate_vars(bset
, nparam
+ first
, n
);
1245 bset
= isl_basic_set_drop_dims(bset
, first
, n
);
1249 static void dump_term(struct isl_basic_map
*bmap
,
1250 isl_int c
, int pos
, FILE *out
)
1253 unsigned in
= isl_basic_map_n_in(bmap
);
1254 unsigned dim
= in
+ isl_basic_map_n_out(bmap
);
1255 unsigned nparam
= isl_basic_map_n_param(bmap
);
1257 isl_int_print(out
, c
, 0);
1259 if (!isl_int_is_one(c
))
1260 isl_int_print(out
, c
, 0);
1261 if (pos
< 1 + nparam
) {
1262 name
= isl_dim_get_name(bmap
->dim
,
1263 isl_dim_param
, pos
- 1);
1265 fprintf(out
, "%s", name
);
1267 fprintf(out
, "p%d", pos
- 1);
1268 } else if (pos
< 1 + nparam
+ in
)
1269 fprintf(out
, "i%d", pos
- 1 - nparam
);
1270 else if (pos
< 1 + nparam
+ dim
)
1271 fprintf(out
, "o%d", pos
- 1 - nparam
- in
);
1273 fprintf(out
, "e%d", pos
- 1 - nparam
- dim
);
1277 static void dump_constraint_sign(struct isl_basic_map
*bmap
, isl_int
*c
,
1278 int sign
, FILE *out
)
1282 unsigned len
= 1 + isl_basic_map_total_dim(bmap
);
1286 for (i
= 0, first
= 1; i
< len
; ++i
) {
1287 if (isl_int_sgn(c
[i
]) * sign
<= 0)
1290 fprintf(out
, " + ");
1292 isl_int_abs(v
, c
[i
]);
1293 dump_term(bmap
, v
, i
, out
);
1300 static void dump_constraint(struct isl_basic_map
*bmap
, isl_int
*c
,
1301 const char *op
, FILE *out
, int indent
)
1305 fprintf(out
, "%*s", indent
, "");
1307 dump_constraint_sign(bmap
, c
, 1, out
);
1308 fprintf(out
, " %s ", op
);
1309 dump_constraint_sign(bmap
, c
, -1, out
);
1313 for (i
= bmap
->n_div
; i
< bmap
->extra
; ++i
) {
1314 if (isl_int_is_zero(c
[1+isl_dim_total(bmap
->dim
)+i
]))
1316 fprintf(out
, "%*s", indent
, "");
1317 fprintf(out
, "ERROR: unused div coefficient not zero\n");
1322 static void dump_constraints(struct isl_basic_map
*bmap
,
1323 isl_int
**c
, unsigned n
,
1324 const char *op
, FILE *out
, int indent
)
1328 for (i
= 0; i
< n
; ++i
)
1329 dump_constraint(bmap
, c
[i
], op
, out
, indent
);
1332 static void dump_affine(struct isl_basic_map
*bmap
, isl_int
*exp
, FILE *out
)
1336 unsigned total
= isl_basic_map_total_dim(bmap
);
1338 for (j
= 0; j
< 1 + total
; ++j
) {
1339 if (isl_int_is_zero(exp
[j
]))
1341 if (!first
&& isl_int_is_pos(exp
[j
]))
1343 dump_term(bmap
, exp
[j
], j
, out
);
1348 static void dump(struct isl_basic_map
*bmap
, FILE *out
, int indent
)
1352 dump_constraints(bmap
, bmap
->eq
, bmap
->n_eq
, "=", out
, indent
);
1353 dump_constraints(bmap
, bmap
->ineq
, bmap
->n_ineq
, ">=", out
, indent
);
1355 for (i
= 0; i
< bmap
->n_div
; ++i
) {
1356 fprintf(out
, "%*s", indent
, "");
1357 fprintf(out
, "e%d = [(", i
);
1358 dump_affine(bmap
, bmap
->div
[i
]+1, out
);
1360 isl_int_print(out
, bmap
->div
[i
][0], 0);
1361 fprintf(out
, "]\n");
1365 void isl_basic_set_dump(struct isl_basic_set
*bset
, FILE *out
, int indent
)
1368 fprintf(out
, "null basic set\n");
1372 fprintf(out
, "%*s", indent
, "");
1373 fprintf(out
, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
1374 bset
->ref
, bset
->dim
->nparam
, bset
->dim
->n_out
,
1375 bset
->extra
, bset
->flags
);
1376 dump((struct isl_basic_map
*)bset
, out
, indent
);
1379 void isl_basic_map_dump(struct isl_basic_map
*bmap
, FILE *out
, int indent
)
1382 fprintf(out
, "null basic map\n");
1386 fprintf(out
, "%*s", indent
, "");
1387 fprintf(out
, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
1388 "flags: %x, n_name: %d\n",
1390 bmap
->dim
->nparam
, bmap
->dim
->n_in
, bmap
->dim
->n_out
,
1391 bmap
->extra
, bmap
->flags
, bmap
->dim
->n_name
);
1392 dump(bmap
, out
, indent
);
1395 int isl_inequality_negate(struct isl_basic_map
*bmap
, unsigned pos
)
1400 total
= isl_basic_map_total_dim(bmap
);
1401 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
1402 isl_seq_neg(bmap
->ineq
[pos
], bmap
->ineq
[pos
], 1 + total
);
1403 isl_int_sub_ui(bmap
->ineq
[pos
][0], bmap
->ineq
[pos
][0], 1);
1404 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1408 struct isl_set
*isl_set_alloc_dim(struct isl_dim
*dim
, int n
, unsigned flags
)
1410 struct isl_set
*set
;
1414 isl_assert(dim
->ctx
, dim
->n_in
== 0, return NULL
);
1415 isl_assert(dim
->ctx
, n
>= 0, return NULL
);
1416 set
= isl_alloc(dim
->ctx
, struct isl_set
,
1417 sizeof(struct isl_set
) +
1418 (n
- 1) * sizeof(struct isl_basic_set
*));
1422 set
->ctx
= dim
->ctx
;
1423 isl_ctx_ref(set
->ctx
);
1435 struct isl_set
*isl_set_alloc(struct isl_ctx
*ctx
,
1436 unsigned nparam
, unsigned dim
, int n
, unsigned flags
)
1438 struct isl_set
*set
;
1439 struct isl_dim
*dims
;
1441 dims
= isl_dim_alloc(ctx
, nparam
, 0, dim
);
1445 set
= isl_set_alloc_dim(dims
, n
, flags
);
1449 /* Make sure "map" has room for at least "n" more basic maps.
1451 struct isl_map
*isl_map_grow(struct isl_map
*map
, int n
)
1454 struct isl_map
*grown
= NULL
;
1458 isl_assert(map
->ctx
, n
>= 0, goto error
);
1459 if (map
->n
+ n
<= map
->size
)
1461 grown
= isl_map_alloc_dim(isl_map_get_dim(map
), map
->n
+ n
, map
->flags
);
1464 for (i
= 0; i
< map
->n
; ++i
) {
1465 grown
->p
[i
] = isl_basic_map_copy(map
->p
[i
]);
1473 isl_map_free(grown
);
1478 /* Make sure "set" has room for at least "n" more basic sets.
1480 struct isl_set
*isl_set_grow(struct isl_set
*set
, int n
)
1482 return (struct isl_set
*)isl_map_grow((struct isl_map
*)set
, n
);
1485 struct isl_set
*isl_set_dup(struct isl_set
*set
)
1488 struct isl_set
*dup
;
1493 dup
= isl_set_alloc_dim(isl_dim_copy(set
->dim
), set
->n
, set
->flags
);
1496 for (i
= 0; i
< set
->n
; ++i
)
1497 dup
= isl_set_add(dup
, isl_basic_set_copy(set
->p
[i
]));
1501 struct isl_set
*isl_set_from_basic_set(struct isl_basic_set
*bset
)
1503 struct isl_set
*set
;
1508 set
= isl_set_alloc_dim(isl_dim_copy(bset
->dim
), 1, ISL_MAP_DISJOINT
);
1510 isl_basic_set_free(bset
);
1513 return isl_set_add(set
, bset
);
1516 struct isl_map
*isl_map_from_basic_map(struct isl_basic_map
*bmap
)
1518 struct isl_map
*map
;
1523 map
= isl_map_alloc_dim(isl_dim_copy(bmap
->dim
), 1, ISL_MAP_DISJOINT
);
1525 isl_basic_map_free(bmap
);
1528 return isl_map_add(map
, bmap
);
1531 struct isl_set
*isl_set_add(struct isl_set
*set
, struct isl_basic_set
*bset
)
1533 return (struct isl_set
*)isl_map_add((struct isl_map
*)set
,
1534 (struct isl_basic_map
*)bset
);
1537 void isl_set_free(struct isl_set
*set
)
1547 isl_ctx_deref(set
->ctx
);
1548 for (i
= 0; i
< set
->n
; ++i
)
1549 isl_basic_set_free(set
->p
[i
]);
1550 isl_dim_free(set
->dim
);
1554 void isl_set_dump(struct isl_set
*set
, FILE *out
, int indent
)
1559 fprintf(out
, "null set\n");
1563 fprintf(out
, "%*s", indent
, "");
1564 fprintf(out
, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
1565 set
->ref
, set
->n
, set
->dim
->nparam
, set
->dim
->n_out
,
1567 for (i
= 0; i
< set
->n
; ++i
) {
1568 fprintf(out
, "%*s", indent
, "");
1569 fprintf(out
, "basic set %d:\n", i
);
1570 isl_basic_set_dump(set
->p
[i
], out
, indent
+4);
1574 void isl_map_dump(struct isl_map
*map
, FILE *out
, int indent
)
1579 fprintf(out
, "null map\n");
1583 fprintf(out
, "%*s", indent
, "");
1584 fprintf(out
, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
1585 "flags: %x, n_name: %d\n",
1586 map
->ref
, map
->n
, map
->dim
->nparam
, map
->dim
->n_in
,
1587 map
->dim
->n_out
, map
->flags
, map
->dim
->n_name
);
1588 for (i
= 0; i
< map
->n
; ++i
) {
1589 fprintf(out
, "%*s", indent
, "");
1590 fprintf(out
, "basic map %d:\n", i
);
1591 isl_basic_map_dump(map
->p
[i
], out
, indent
+4);
1595 struct isl_basic_map
*isl_basic_map_intersect_domain(
1596 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
1598 struct isl_basic_map
*bmap_domain
;
1599 struct isl_dim
*dim
;
1604 isl_assert(bset
->ctx
, isl_dim_match(bmap
->dim
, isl_dim_param
,
1605 bset
->dim
, isl_dim_param
), goto error
);
1607 if (isl_dim_size(bset
->dim
, isl_dim_set
) != 0)
1608 isl_assert(bset
->ctx
,
1609 isl_basic_map_compatible_domain(bmap
, bset
), goto error
);
1611 bmap
= isl_basic_map_cow(bmap
);
1612 bmap
= isl_basic_map_extend_dim(bmap
, isl_dim_copy(bmap
->dim
),
1613 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
1616 dim
= isl_dim_reverse(isl_dim_copy(bset
->dim
));
1617 bmap_domain
= isl_basic_map_from_basic_set(bset
, dim
);
1618 bmap
= add_constraints(bmap
, bmap_domain
, 0, 0);
1620 bmap
= isl_basic_map_simplify(bmap
);
1621 return isl_basic_map_finalize(bmap
);
1623 isl_basic_map_free(bmap
);
1624 isl_basic_set_free(bset
);
1628 struct isl_basic_map
*isl_basic_map_intersect_range(
1629 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
1631 struct isl_basic_map
*bmap_range
;
1636 isl_assert(bset
->ctx
, isl_dim_match(bmap
->dim
, isl_dim_param
,
1637 bset
->dim
, isl_dim_param
), goto error
);
1639 if (isl_dim_size(bset
->dim
, isl_dim_set
) != 0)
1640 isl_assert(bset
->ctx
,
1641 isl_basic_map_compatible_range(bmap
, bset
), goto error
);
1643 bmap
= isl_basic_map_cow(bmap
);
1644 bmap
= isl_basic_map_extend_dim(bmap
, isl_dim_copy(bmap
->dim
),
1645 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
1648 bmap_range
= isl_basic_map_from_basic_set(bset
, isl_dim_copy(bset
->dim
));
1649 bmap
= add_constraints(bmap
, bmap_range
, 0, 0);
1651 bmap
= isl_basic_map_simplify(bmap
);
1652 return isl_basic_map_finalize(bmap
);
1654 isl_basic_map_free(bmap
);
1655 isl_basic_set_free(bset
);
1659 static int basic_map_contains(struct isl_basic_map
*bmap
, struct isl_vec
*vec
)
1665 total
= 1 + isl_basic_map_total_dim(bmap
);
1666 if (total
!= vec
->size
)
1671 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
1672 isl_seq_inner_product(vec
->el
, bmap
->eq
[i
], total
, &s
);
1673 if (!isl_int_is_zero(s
)) {
1679 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
1680 isl_seq_inner_product(vec
->el
, bmap
->ineq
[i
], total
, &s
);
1681 if (isl_int_is_neg(s
)) {
1692 int isl_basic_set_contains(struct isl_basic_set
*bset
, struct isl_vec
*vec
)
1694 return basic_map_contains((struct isl_basic_map
*)bset
, vec
);
1697 struct isl_basic_map
*isl_basic_map_intersect(
1698 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
1700 struct isl_vec
*sample
= NULL
;
1702 if (!bmap1
|| !bmap2
)
1705 isl_assert(bmap1
->ctx
, isl_dim_match(bmap1
->dim
, isl_dim_param
,
1706 bmap2
->dim
, isl_dim_param
), goto error
);
1707 if (isl_dim_total(bmap1
->dim
) ==
1708 isl_dim_size(bmap1
->dim
, isl_dim_param
) &&
1709 isl_dim_total(bmap2
->dim
) !=
1710 isl_dim_size(bmap2
->dim
, isl_dim_param
))
1711 return isl_basic_map_intersect(bmap2
, bmap1
);
1713 if (isl_dim_total(bmap2
->dim
) !=
1714 isl_dim_size(bmap2
->dim
, isl_dim_param
))
1715 isl_assert(bmap1
->ctx
,
1716 isl_dim_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
1718 if (bmap1
->sample
&&
1719 basic_map_contains(bmap1
, bmap1
->sample
) > 0 &&
1720 basic_map_contains(bmap2
, bmap1
->sample
) > 0)
1721 sample
= isl_vec_copy(bmap1
->sample
);
1722 else if (bmap2
->sample
&&
1723 basic_map_contains(bmap1
, bmap2
->sample
) > 0 &&
1724 basic_map_contains(bmap2
, bmap2
->sample
) > 0)
1725 sample
= isl_vec_copy(bmap2
->sample
);
1727 bmap1
= isl_basic_map_cow(bmap1
);
1728 bmap1
= isl_basic_map_extend_dim(bmap1
, isl_dim_copy(bmap1
->dim
),
1729 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
1732 bmap1
= add_constraints(bmap1
, bmap2
, 0, 0);
1735 isl_vec_free(bmap1
->sample
);
1736 bmap1
->sample
= sample
;
1739 bmap1
= isl_basic_map_simplify(bmap1
);
1740 return isl_basic_map_finalize(bmap1
);
1743 isl_vec_free(sample
);
1744 isl_basic_map_free(bmap1
);
1745 isl_basic_map_free(bmap2
);
1749 struct isl_basic_set
*isl_basic_set_intersect(
1750 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
1752 return (struct isl_basic_set
*)
1753 isl_basic_map_intersect(
1754 (struct isl_basic_map
*)bset1
,
1755 (struct isl_basic_map
*)bset2
);
1758 struct isl_map
*isl_map_intersect(struct isl_map
*map1
, struct isl_map
*map2
)
1761 struct isl_map
*result
;
1767 isl_assert(map1
->ctx
, isl_dim_match(map1
->dim
, isl_dim_param
,
1768 map2
->dim
, isl_dim_param
), goto error
);
1769 if (isl_dim_total(map1
->dim
) ==
1770 isl_dim_size(map1
->dim
, isl_dim_param
) &&
1771 isl_dim_total(map2
->dim
) != isl_dim_size(map2
->dim
, isl_dim_param
))
1772 return isl_map_intersect(map2
, map1
);
1774 if (isl_dim_total(map2
->dim
) != isl_dim_size(map2
->dim
, isl_dim_param
))
1775 isl_assert(map1
->ctx
,
1776 isl_dim_equal(map1
->dim
, map2
->dim
), goto error
);
1778 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
1779 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
1780 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
1782 result
= isl_map_alloc_dim(isl_dim_copy(map1
->dim
),
1783 map1
->n
* map2
->n
, flags
);
1786 for (i
= 0; i
< map1
->n
; ++i
)
1787 for (j
= 0; j
< map2
->n
; ++j
) {
1788 struct isl_basic_map
*part
;
1789 part
= isl_basic_map_intersect(
1790 isl_basic_map_copy(map1
->p
[i
]),
1791 isl_basic_map_copy(map2
->p
[j
]));
1792 if (isl_basic_map_is_empty(part
))
1793 isl_basic_map_free(part
);
1795 result
= isl_map_add(result
, part
);
1808 struct isl_set
*isl_set_intersect(struct isl_set
*set1
, struct isl_set
*set2
)
1810 return (struct isl_set
*)
1811 isl_map_intersect((struct isl_map
*)set1
,
1812 (struct isl_map
*)set2
);
1815 struct isl_basic_map
*isl_basic_map_reverse(struct isl_basic_map
*bmap
)
1817 struct isl_dim
*dim
;
1818 struct isl_basic_set
*bset
;
1823 bmap
= isl_basic_map_cow(bmap
);
1826 dim
= isl_dim_reverse(isl_dim_copy(bmap
->dim
));
1827 in
= isl_basic_map_n_in(bmap
);
1828 bset
= isl_basic_set_from_basic_map(bmap
);
1829 bset
= isl_basic_set_swap_vars(bset
, in
);
1830 return isl_basic_map_from_basic_set(bset
, dim
);
1833 /* Turn final n dimensions into existentially quantified variables.
1835 struct isl_basic_set
*isl_basic_set_project_out(struct isl_basic_set
*bset
,
1836 enum isl_dim_type type
, unsigned first
, unsigned n
)
1846 isl_assert(bset
->ctx
, type
== isl_dim_set
, goto error
);
1847 isl_assert(bset
->ctx
, first
+ n
== isl_basic_set_n_dim(bset
), goto error
);
1852 bset
= isl_basic_set_cow(bset
);
1854 row_size
= 1 + isl_dim_total(bset
->dim
) + bset
->extra
;
1855 old
= bset
->block2
.data
;
1856 bset
->block2
= isl_blk_extend(bset
->ctx
, bset
->block2
,
1857 (bset
->extra
+ n
) * (1 + row_size
));
1858 if (!bset
->block2
.data
)
1860 new_div
= isl_alloc_array(ctx
, isl_int
*, bset
->extra
+ n
);
1863 for (i
= 0; i
< n
; ++i
) {
1864 new_div
[i
] = bset
->block2
.data
+
1865 (bset
->extra
+ i
) * (1 + row_size
);
1866 isl_seq_clr(new_div
[i
], 1 + row_size
);
1868 for (i
= 0; i
< bset
->extra
; ++i
)
1869 new_div
[n
+ i
] = bset
->block2
.data
+ (bset
->div
[i
] - old
);
1871 bset
->div
= new_div
;
1874 bset
->dim
= isl_dim_drop_outputs(bset
->dim
,
1875 isl_basic_set_n_dim(bset
) - n
, n
);
1878 bset
= isl_basic_set_simplify(bset
);
1879 bset
= isl_basic_set_drop_redundant_divs(bset
);
1880 return isl_basic_set_finalize(bset
);
1882 isl_basic_set_free(bset
);
1886 static struct isl_basic_map
*add_divs(struct isl_basic_map
*bmap
, unsigned n
)
1890 for (i
= 0; i
< n
; ++i
) {
1891 j
= isl_basic_map_alloc_div(bmap
);
1894 isl_seq_clr(bmap
->div
[j
], 1+1+isl_basic_map_total_dim(bmap
));
1898 isl_basic_map_free(bmap
);
1902 struct isl_basic_map
*isl_basic_map_apply_range(
1903 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
1905 struct isl_dim
*dim_result
= NULL
;
1906 struct isl_basic_map
*bmap
;
1907 unsigned n_in
, n_out
, n
, nparam
, total
, pos
;
1908 struct isl_dim_map
*dim_map1
, *dim_map2
;
1910 if (!bmap1
|| !bmap2
)
1913 dim_result
= isl_dim_join(isl_dim_copy(bmap1
->dim
),
1914 isl_dim_copy(bmap2
->dim
));
1916 n_in
= isl_basic_map_n_in(bmap1
);
1917 n_out
= isl_basic_map_n_out(bmap2
);
1918 n
= isl_basic_map_n_out(bmap1
);
1919 nparam
= isl_basic_map_n_param(bmap1
);
1921 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ n
;
1922 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
1923 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
1924 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
1925 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
1926 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
1927 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_in
);
1928 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_out
);
1929 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
1930 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
1931 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
1933 bmap
= isl_basic_map_alloc_dim(dim_result
,
1934 bmap1
->n_div
+ bmap2
->n_div
+ n
,
1935 bmap1
->n_eq
+ bmap2
->n_eq
,
1936 bmap1
->n_ineq
+ bmap2
->n_ineq
);
1937 bmap
= add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
1938 bmap
= add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
1939 bmap
= add_divs(bmap
, n
);
1940 bmap
= isl_basic_map_simplify(bmap
);
1941 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
1942 return isl_basic_map_finalize(bmap
);
1944 isl_basic_map_free(bmap1
);
1945 isl_basic_map_free(bmap2
);
1949 struct isl_basic_set
*isl_basic_set_apply(
1950 struct isl_basic_set
*bset
, struct isl_basic_map
*bmap
)
1955 isl_assert(bset
->ctx
, isl_basic_map_compatible_domain(bmap
, bset
),
1958 return (struct isl_basic_set
*)
1959 isl_basic_map_apply_range((struct isl_basic_map
*)bset
, bmap
);
1961 isl_basic_set_free(bset
);
1962 isl_basic_map_free(bmap
);
1966 struct isl_basic_map
*isl_basic_map_apply_domain(
1967 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
1969 if (!bmap1
|| !bmap2
)
1972 isl_assert(bmap1
->ctx
,
1973 isl_basic_map_n_in(bmap1
) == isl_basic_map_n_in(bmap2
), goto error
);
1974 isl_assert(bmap1
->ctx
,
1975 isl_basic_map_n_param(bmap1
) == isl_basic_map_n_param(bmap2
),
1978 bmap1
= isl_basic_map_reverse(bmap1
);
1979 bmap1
= isl_basic_map_apply_range(bmap1
, bmap2
);
1980 return isl_basic_map_reverse(bmap1
);
1982 isl_basic_map_free(bmap1
);
1983 isl_basic_map_free(bmap2
);
1987 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
1988 * A \cap B -> f(A) + f(B)
1990 struct isl_basic_map
*isl_basic_map_sum(
1991 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
1993 unsigned n_in
, n_out
, nparam
, total
, pos
;
1994 struct isl_basic_map
*bmap
= NULL
;
1995 struct isl_dim_map
*dim_map1
, *dim_map2
;
1998 if (!bmap1
|| !bmap2
)
2001 isl_assert(bmap1
->ctx
, isl_dim_equal(bmap1
->dim
, bmap2
->dim
),
2004 nparam
= isl_basic_map_n_param(bmap1
);
2005 n_in
= isl_basic_map_n_in(bmap1
);
2006 n_out
= isl_basic_map_n_out(bmap1
);
2008 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
;
2009 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
2010 dim_map2
= isl_dim_map_alloc(bmap2
->ctx
, total
);
2011 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
2012 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
);
2013 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
2014 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
2015 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_in
+ n_out
);
2016 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
2017 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
2018 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_out
);
2020 bmap
= isl_basic_map_alloc_dim(isl_dim_copy(bmap1
->dim
),
2021 bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
,
2022 bmap1
->n_eq
+ bmap2
->n_eq
+ n_out
,
2023 bmap1
->n_ineq
+ bmap2
->n_ineq
);
2024 for (i
= 0; i
< n_out
; ++i
) {
2025 int j
= isl_basic_map_alloc_equality(bmap
);
2028 isl_seq_clr(bmap
->eq
[j
], 1+total
);
2029 isl_int_set_si(bmap
->eq
[j
][1+nparam
+n_in
+i
], -1);
2030 isl_int_set_si(bmap
->eq
[j
][1+pos
+i
], 1);
2031 isl_int_set_si(bmap
->eq
[j
][1+pos
-n_out
+i
], 1);
2033 bmap
= add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
2034 bmap
= add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
2035 bmap
= add_divs(bmap
, 2 * n_out
);
2037 bmap
= isl_basic_map_simplify(bmap
);
2038 return isl_basic_map_finalize(bmap
);
2040 isl_basic_map_free(bmap
);
2041 isl_basic_map_free(bmap1
);
2042 isl_basic_map_free(bmap2
);
2046 /* Given two maps A -> f(A) and B -> g(B), construct a map
2047 * A \cap B -> f(A) + f(B)
2049 struct isl_map
*isl_map_sum(struct isl_map
*map1
, struct isl_map
*map2
)
2051 struct isl_map
*result
;
2057 isl_assert(map1
->ctx
, isl_dim_equal(map1
->dim
, map2
->dim
), goto error
);
2059 result
= isl_map_alloc_dim(isl_dim_copy(map1
->dim
),
2060 map1
->n
* map2
->n
, 0);
2063 for (i
= 0; i
< map1
->n
; ++i
)
2064 for (j
= 0; j
< map2
->n
; ++j
) {
2065 struct isl_basic_map
*part
;
2066 part
= isl_basic_map_sum(
2067 isl_basic_map_copy(map1
->p
[i
]),
2068 isl_basic_map_copy(map2
->p
[j
]));
2069 if (isl_basic_map_is_empty(part
))
2070 isl_basic_map_free(part
);
2072 result
= isl_map_add(result
, part
);
2085 /* Given a basic map A -> f(A), construct A -> -f(A).
2087 struct isl_basic_map
*isl_basic_map_neg(struct isl_basic_map
*bmap
)
2092 bmap
= isl_basic_map_cow(bmap
);
2096 n
= isl_basic_map_dim(bmap
, isl_dim_out
);
2097 off
= isl_basic_map_offset(bmap
, isl_dim_out
);
2098 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2099 for (j
= 0; j
< n
; ++j
)
2100 isl_int_neg(bmap
->eq
[i
][off
+j
], bmap
->eq
[i
][off
+j
]);
2101 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2102 for (j
= 0; j
< n
; ++j
)
2103 isl_int_neg(bmap
->ineq
[i
][off
+j
], bmap
->ineq
[i
][off
+j
]);
2104 for (i
= 0; i
< bmap
->n_div
; ++i
)
2105 for (j
= 0; j
< n
; ++j
)
2106 isl_int_neg(bmap
->div
[i
][1+off
+j
], bmap
->div
[i
][1+off
+j
]);
2107 return isl_basic_map_finalize(bmap
);
2110 /* Given a map A -> f(A), construct A -> -f(A).
2112 struct isl_map
*isl_map_neg(struct isl_map
*map
)
2116 map
= isl_map_cow(map
);
2120 for (i
= 0; i
< map
->n
; ++i
) {
2121 map
->p
[i
] = isl_basic_map_neg(map
->p
[i
]);
2132 /* Given a basic map A -> f(A) and an integer d, construct a basic map
2133 * A -> floor(f(A)/d).
2135 struct isl_basic_map
*isl_basic_map_floordiv(struct isl_basic_map
*bmap
,
2138 unsigned n_in
, n_out
, nparam
, total
, pos
;
2139 struct isl_basic_map
*result
= NULL
;
2140 struct isl_dim_map
*dim_map
;
2146 nparam
= isl_basic_map_n_param(bmap
);
2147 n_in
= isl_basic_map_n_in(bmap
);
2148 n_out
= isl_basic_map_n_out(bmap
);
2150 total
= nparam
+ n_in
+ n_out
+ bmap
->n_div
+ n_out
;
2151 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
2152 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_param
, pos
= 0);
2153 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_in
, pos
+= nparam
);
2154 isl_dim_map_div(dim_map
, bmap
, pos
+= n_in
+ n_out
);
2155 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_out
, pos
+= bmap
->n_div
);
2157 result
= isl_basic_map_alloc_dim(isl_dim_copy(bmap
->dim
),
2158 bmap
->n_div
+ n_out
,
2159 bmap
->n_eq
, bmap
->n_ineq
+ 2 * n_out
);
2160 result
= add_constraints_dim_map(result
, bmap
, dim_map
);
2161 result
= add_divs(result
, n_out
);
2162 for (i
= 0; i
< n_out
; ++i
) {
2164 j
= isl_basic_map_alloc_inequality(result
);
2167 isl_seq_clr(result
->ineq
[j
], 1+total
);
2168 isl_int_neg(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
2169 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], 1);
2170 j
= isl_basic_map_alloc_inequality(result
);
2173 isl_seq_clr(result
->ineq
[j
], 1+total
);
2174 isl_int_set(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
2175 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], -1);
2176 isl_int_sub_ui(result
->ineq
[j
][0], d
, 1);
2179 result
= isl_basic_map_simplify(result
);
2180 return isl_basic_map_finalize(result
);
2182 isl_basic_map_free(result
);
2186 /* Given a map A -> f(A) and an integer d, construct a map
2187 * A -> floor(f(A)/d).
2189 struct isl_map
*isl_map_floordiv(struct isl_map
*map
, isl_int d
)
2193 map
= isl_map_cow(map
);
2197 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
2198 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
2199 for (i
= 0; i
< map
->n
; ++i
) {
2200 map
->p
[i
] = isl_basic_map_floordiv(map
->p
[i
], d
);
2211 static struct isl_basic_map
*var_equal(struct isl_basic_map
*bmap
, unsigned pos
)
2217 i
= isl_basic_map_alloc_equality(bmap
);
2220 nparam
= isl_basic_map_n_param(bmap
);
2221 n_in
= isl_basic_map_n_in(bmap
);
2222 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
2223 isl_int_set_si(bmap
->eq
[i
][1+nparam
+pos
], -1);
2224 isl_int_set_si(bmap
->eq
[i
][1+nparam
+n_in
+pos
], 1);
2225 return isl_basic_map_finalize(bmap
);
2227 isl_basic_map_free(bmap
);
2231 /* Add a constraints to "bmap" expressing i_pos < o_pos
2233 static struct isl_basic_map
*var_less(struct isl_basic_map
*bmap
, unsigned pos
)
2239 i
= isl_basic_map_alloc_inequality(bmap
);
2242 nparam
= isl_basic_map_n_param(bmap
);
2243 n_in
= isl_basic_map_n_in(bmap
);
2244 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
2245 isl_int_set_si(bmap
->ineq
[i
][0], -1);
2246 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
2247 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
2248 return isl_basic_map_finalize(bmap
);
2250 isl_basic_map_free(bmap
);
2254 /* Add a constraints to "bmap" expressing i_pos > o_pos
2256 static struct isl_basic_map
*var_more(struct isl_basic_map
*bmap
, unsigned pos
)
2262 i
= isl_basic_map_alloc_inequality(bmap
);
2265 nparam
= isl_basic_map_n_param(bmap
);
2266 n_in
= isl_basic_map_n_in(bmap
);
2267 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
2268 isl_int_set_si(bmap
->ineq
[i
][0], -1);
2269 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
2270 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
2271 return isl_basic_map_finalize(bmap
);
2273 isl_basic_map_free(bmap
);
2277 struct isl_basic_map
*isl_basic_map_equal(struct isl_dim
*dim
, unsigned n_equal
)
2280 struct isl_basic_map
*bmap
;
2281 bmap
= isl_basic_map_alloc_dim(dim
, 0, n_equal
, 0);
2284 for (i
= 0; i
< n_equal
&& bmap
; ++i
)
2285 bmap
= var_equal(bmap
, i
);
2286 return isl_basic_map_finalize(bmap
);
2289 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos < o_pos
2291 struct isl_basic_map
*isl_basic_map_less_at(struct isl_dim
*dim
, unsigned pos
)
2294 struct isl_basic_map
*bmap
;
2295 bmap
= isl_basic_map_alloc_dim(dim
, 0, pos
, 1);
2298 for (i
= 0; i
< pos
&& bmap
; ++i
)
2299 bmap
= var_equal(bmap
, i
);
2301 bmap
= var_less(bmap
, pos
);
2302 return isl_basic_map_finalize(bmap
);
2305 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
2307 struct isl_basic_map
*isl_basic_map_more_at(struct isl_dim
*dim
, unsigned pos
)
2310 struct isl_basic_map
*bmap
;
2311 bmap
= isl_basic_map_alloc_dim(dim
, 0, pos
, 1);
2314 for (i
= 0; i
< pos
&& bmap
; ++i
)
2315 bmap
= var_equal(bmap
, i
);
2317 bmap
= var_more(bmap
, pos
);
2318 return isl_basic_map_finalize(bmap
);
2321 static __isl_give isl_map
*map_lex_lt(__isl_take isl_dim
*dims
)
2323 struct isl_map
*map
;
2330 map
= isl_map_alloc_dim(isl_dim_copy(dims
), dim
, ISL_MAP_DISJOINT
);
2332 for (i
= 0; i
< dim
; ++i
)
2333 map
= isl_map_add(map
,
2334 isl_basic_map_less_at(isl_dim_copy(dims
), i
));
2340 __isl_give isl_map
*isl_map_lex_lt(__isl_take isl_dim
*set_dim
)
2342 return map_lex_lt(isl_dim_map(set_dim
));
2345 static __isl_give isl_map
*map_lex_gt(__isl_take isl_dim
*dims
)
2347 struct isl_map
*map
;
2354 map
= isl_map_alloc_dim(isl_dim_copy(dims
), dim
, ISL_MAP_DISJOINT
);
2356 for (i
= 0; i
< dim
; ++i
)
2357 map
= isl_map_add(map
,
2358 isl_basic_map_more_at(isl_dim_copy(dims
), i
));
2364 __isl_give isl_map
*isl_map_lex_gt(__isl_take isl_dim
*set_dim
)
2366 return map_lex_gt(isl_dim_map(set_dim
));
2369 struct isl_basic_map
*isl_basic_map_from_basic_set(
2370 struct isl_basic_set
*bset
, struct isl_dim
*dim
)
2372 struct isl_basic_map
*bmap
;
2374 bset
= isl_basic_set_cow(bset
);
2378 isl_assert(bset
->ctx
, isl_dim_compatible(bset
->dim
, dim
), goto error
);
2379 isl_dim_free(bset
->dim
);
2380 bmap
= (struct isl_basic_map
*) bset
;
2382 return isl_basic_map_finalize(bmap
);
2384 isl_basic_set_free(bset
);
2389 struct isl_basic_set
*isl_basic_set_from_basic_map(struct isl_basic_map
*bmap
)
2393 if (bmap
->dim
->n_in
== 0)
2394 return (struct isl_basic_set
*)bmap
;
2395 bmap
= isl_basic_map_cow(bmap
);
2398 bmap
->dim
= isl_dim_cow(bmap
->dim
);
2401 bmap
->dim
->n_out
+= bmap
->dim
->n_in
;
2402 bmap
->dim
->n_in
= 0;
2403 bmap
= isl_basic_map_finalize(bmap
);
2404 return (struct isl_basic_set
*)bmap
;
2406 isl_basic_map_free(bmap
);
2410 /* For a div d = floor(f/m), add the constraints
2413 * -(f-(n-1)) + m d >= 0
2415 * Note that the second constraint is the negation of
2419 static int add_div_constraints(struct isl_basic_map
*bmap
, unsigned div
)
2422 unsigned total
= isl_basic_map_total_dim(bmap
);
2423 unsigned div_pos
= 1 + total
- bmap
->n_div
+ div
;
2425 i
= isl_basic_map_alloc_inequality(bmap
);
2428 isl_seq_cpy(bmap
->ineq
[i
], bmap
->div
[div
]+1, 1+total
);
2429 isl_int_neg(bmap
->ineq
[i
][div_pos
], bmap
->div
[div
][0]);
2431 j
= isl_basic_map_alloc_inequality(bmap
);
2434 isl_seq_neg(bmap
->ineq
[j
], bmap
->ineq
[i
], 1 + total
);
2435 isl_int_add(bmap
->ineq
[j
][0], bmap
->ineq
[j
][0], bmap
->ineq
[j
][div_pos
]);
2436 isl_int_sub_ui(bmap
->ineq
[j
][0], bmap
->ineq
[j
][0], 1);
2440 struct isl_basic_set
*isl_basic_map_underlying_set(
2441 struct isl_basic_map
*bmap
)
2445 if (bmap
->dim
->nparam
== 0 && bmap
->dim
->n_in
== 0 && bmap
->n_div
== 0)
2446 return (struct isl_basic_set
*)bmap
;
2447 bmap
= isl_basic_map_cow(bmap
);
2450 bmap
->dim
= isl_dim_underlying(bmap
->dim
, bmap
->n_div
);
2453 bmap
->extra
-= bmap
->n_div
;
2455 bmap
= isl_basic_map_finalize(bmap
);
2456 return (struct isl_basic_set
*)bmap
;
2461 struct isl_basic_map
*isl_basic_map_overlying_set(
2462 struct isl_basic_set
*bset
, struct isl_basic_map
*like
)
2464 struct isl_basic_map
*bmap
;
2465 struct isl_ctx
*ctx
;
2472 isl_assert(ctx
, bset
->n_div
== 0, goto error
);
2473 isl_assert(ctx
, isl_basic_set_n_param(bset
) == 0, goto error
);
2474 isl_assert(ctx
, bset
->dim
->n_out
== isl_basic_map_total_dim(like
),
2476 if (isl_dim_equal(bset
->dim
, like
->dim
) && like
->n_div
== 0) {
2477 isl_basic_map_free(like
);
2478 return (struct isl_basic_map
*)bset
;
2480 bset
= isl_basic_set_cow(bset
);
2483 total
= bset
->dim
->n_out
+ bset
->extra
;
2484 bmap
= (struct isl_basic_map
*)bset
;
2485 isl_dim_free(bmap
->dim
);
2486 bmap
->dim
= isl_dim_copy(like
->dim
);
2489 bmap
->n_div
= like
->n_div
;
2490 bmap
->extra
+= like
->n_div
;
2493 ltotal
= total
- bmap
->extra
+ like
->extra
;
2496 bmap
->block2
= isl_blk_extend(ctx
, bmap
->block2
,
2497 bmap
->extra
* (1 + 1 + total
));
2498 if (isl_blk_is_error(bmap
->block2
))
2500 bmap
->div
= isl_realloc_array(ctx
, bmap
->div
, isl_int
*,
2504 for (i
= 0; i
< bmap
->extra
; ++i
)
2505 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + 1 + total
);
2506 for (i
= 0; i
< like
->n_div
; ++i
) {
2507 isl_seq_cpy(bmap
->div
[i
], like
->div
[i
], 1 + 1 + ltotal
);
2508 isl_seq_clr(bmap
->div
[i
]+1+1+ltotal
, total
- ltotal
);
2510 bmap
= isl_basic_map_extend_constraints(bmap
,
2511 0, 2 * like
->n_div
);
2512 for (i
= 0; i
< like
->n_div
; ++i
) {
2513 if (isl_int_is_zero(bmap
->div
[i
][0]))
2515 if (add_div_constraints(bmap
, i
) < 0)
2519 isl_basic_map_free(like
);
2520 bmap
= isl_basic_map_simplify(bmap
);
2521 bmap
= isl_basic_map_finalize(bmap
);
2524 isl_basic_map_free(like
);
2525 isl_basic_set_free(bset
);
2529 struct isl_basic_set
*isl_basic_set_from_underlying_set(
2530 struct isl_basic_set
*bset
, struct isl_basic_set
*like
)
2532 return (struct isl_basic_set
*)
2533 isl_basic_map_overlying_set(bset
, (struct isl_basic_map
*)like
);
2536 struct isl_set
*isl_set_from_underlying_set(
2537 struct isl_set
*set
, struct isl_basic_set
*like
)
2543 isl_assert(set
->ctx
, set
->dim
->n_out
== isl_basic_set_total_dim(like
),
2545 if (isl_dim_equal(set
->dim
, like
->dim
) && like
->n_div
== 0) {
2546 isl_basic_set_free(like
);
2549 set
= isl_set_cow(set
);
2552 for (i
= 0; i
< set
->n
; ++i
) {
2553 set
->p
[i
] = isl_basic_set_from_underlying_set(set
->p
[i
],
2554 isl_basic_set_copy(like
));
2558 isl_dim_free(set
->dim
);
2559 set
->dim
= isl_dim_copy(like
->dim
);
2562 isl_basic_set_free(like
);
2565 isl_basic_set_free(like
);
2570 struct isl_set
*isl_map_underlying_set(struct isl_map
*map
)
2574 map
= isl_map_cow(map
);
2577 map
->dim
= isl_dim_cow(map
->dim
);
2581 for (i
= 1; i
< map
->n
; ++i
)
2582 isl_assert(map
->ctx
, map
->p
[0]->n_div
== map
->p
[i
]->n_div
,
2584 for (i
= 0; i
< map
->n
; ++i
) {
2585 map
->p
[i
] = (struct isl_basic_map
*)
2586 isl_basic_map_underlying_set(map
->p
[i
]);
2591 map
->dim
= isl_dim_underlying(map
->dim
, 0);
2593 isl_dim_free(map
->dim
);
2594 map
->dim
= isl_dim_copy(map
->p
[0]->dim
);
2598 return (struct isl_set
*)map
;
2604 struct isl_set
*isl_set_to_underlying_set(struct isl_set
*set
)
2606 return (struct isl_set
*)isl_map_underlying_set((struct isl_map
*)set
);
2609 struct isl_basic_set
*isl_basic_map_domain(struct isl_basic_map
*bmap
)
2611 struct isl_basic_set
*domain
;
2616 n_in
= isl_basic_map_n_in(bmap
);
2617 n_out
= isl_basic_map_n_out(bmap
);
2618 domain
= isl_basic_set_from_basic_map(bmap
);
2619 return isl_basic_set_project_out(domain
, isl_dim_set
, n_in
, n_out
);
2622 struct isl_basic_set
*isl_basic_map_range(struct isl_basic_map
*bmap
)
2624 return isl_basic_map_domain(isl_basic_map_reverse(bmap
));
2627 struct isl_set
*isl_map_range(struct isl_map
*map
)
2630 struct isl_set
*set
;
2634 map
= isl_map_cow(map
);
2638 set
= (struct isl_set
*) map
;
2639 if (set
->dim
->n_in
!= 0) {
2640 set
->dim
= isl_dim_drop_inputs(set
->dim
, 0, set
->dim
->n_in
);
2644 for (i
= 0; i
< map
->n
; ++i
) {
2645 set
->p
[i
] = isl_basic_map_range(map
->p
[i
]);
2649 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
2650 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
2657 struct isl_map
*isl_map_from_set(struct isl_set
*set
, struct isl_dim
*dim
)
2660 struct isl_map
*map
= NULL
;
2662 set
= isl_set_cow(set
);
2665 isl_assert(set
->ctx
, isl_dim_compatible(set
->dim
, dim
), goto error
);
2666 map
= (struct isl_map
*)set
;
2667 for (i
= 0; i
< set
->n
; ++i
) {
2668 map
->p
[i
] = isl_basic_map_from_basic_set(
2669 set
->p
[i
], isl_dim_copy(dim
));
2673 isl_dim_free(map
->dim
);
2682 struct isl_map
*isl_map_from_range(struct isl_set
*set
)
2684 return (struct isl_map
*)set
;
2687 struct isl_set
*isl_set_from_map(struct isl_map
*map
)
2690 struct isl_set
*set
= NULL
;
2694 map
= isl_map_cow(map
);
2697 map
->dim
= isl_dim_cow(map
->dim
);
2700 map
->dim
->n_out
+= map
->dim
->n_in
;
2702 set
= (struct isl_set
*)map
;
2703 for (i
= 0; i
< map
->n
; ++i
) {
2704 set
->p
[i
] = isl_basic_set_from_basic_map(map
->p
[i
]);
2714 struct isl_map
*isl_map_alloc_dim(struct isl_dim
*dim
, int n
, unsigned flags
)
2716 struct isl_map
*map
;
2720 isl_assert(dim
->ctx
, n
>= 0, return NULL
);
2721 map
= isl_alloc(dim
->ctx
, struct isl_map
,
2722 sizeof(struct isl_map
) +
2723 (n
- 1) * sizeof(struct isl_basic_map
*));
2727 map
->ctx
= dim
->ctx
;
2728 isl_ctx_ref(map
->ctx
);
2740 struct isl_map
*isl_map_alloc(struct isl_ctx
*ctx
,
2741 unsigned nparam
, unsigned in
, unsigned out
, int n
,
2744 struct isl_map
*map
;
2745 struct isl_dim
*dims
;
2747 dims
= isl_dim_alloc(ctx
, nparam
, in
, out
);
2751 map
= isl_map_alloc_dim(dims
, n
, flags
);
2755 struct isl_basic_map
*isl_basic_map_empty(struct isl_dim
*dim
)
2757 struct isl_basic_map
*bmap
;
2758 bmap
= isl_basic_map_alloc_dim(dim
, 0, 1, 0);
2759 bmap
= isl_basic_map_set_to_empty(bmap
);
2763 struct isl_basic_set
*isl_basic_set_empty(struct isl_dim
*dim
)
2765 struct isl_basic_set
*bset
;
2766 bset
= isl_basic_set_alloc_dim(dim
, 0, 1, 0);
2767 bset
= isl_basic_set_set_to_empty(bset
);
2771 struct isl_basic_map
*isl_basic_map_empty_like(struct isl_basic_map
*model
)
2773 struct isl_basic_map
*bmap
;
2776 bmap
= isl_basic_map_alloc_dim(isl_dim_copy(model
->dim
), 0, 1, 0);
2777 bmap
= isl_basic_map_set_to_empty(bmap
);
2781 struct isl_basic_map
*isl_basic_map_empty_like_map(struct isl_map
*model
)
2783 struct isl_basic_map
*bmap
;
2786 bmap
= isl_basic_map_alloc_dim(isl_dim_copy(model
->dim
), 0, 1, 0);
2787 bmap
= isl_basic_map_set_to_empty(bmap
);
2791 struct isl_basic_set
*isl_basic_set_empty_like(struct isl_basic_set
*model
)
2793 struct isl_basic_set
*bset
;
2796 bset
= isl_basic_set_alloc_dim(isl_dim_copy(model
->dim
), 0, 1, 0);
2797 bset
= isl_basic_set_set_to_empty(bset
);
2801 struct isl_basic_map
*isl_basic_map_universe(struct isl_dim
*dim
)
2803 struct isl_basic_map
*bmap
;
2804 bmap
= isl_basic_map_alloc_dim(dim
, 0, 0, 0);
2808 struct isl_basic_set
*isl_basic_set_universe(struct isl_dim
*dim
)
2810 struct isl_basic_set
*bset
;
2811 bset
= isl_basic_set_alloc_dim(dim
, 0, 0, 0);
2815 __isl_give isl_basic_map
*isl_basic_map_universe_like(
2816 __isl_keep isl_basic_map
*model
)
2820 return isl_basic_map_alloc_dim(isl_dim_copy(model
->dim
), 0, 0, 0);
2823 struct isl_basic_set
*isl_basic_set_universe_like(struct isl_basic_set
*model
)
2827 return isl_basic_set_alloc_dim(isl_dim_copy(model
->dim
), 0, 0, 0);
2830 struct isl_map
*isl_map_empty(struct isl_dim
*dim
)
2832 return isl_map_alloc_dim(dim
, 0, ISL_MAP_DISJOINT
);
2835 struct isl_map
*isl_map_empty_like(struct isl_map
*model
)
2839 return isl_map_alloc_dim(isl_dim_copy(model
->dim
), 0, ISL_MAP_DISJOINT
);
2842 struct isl_map
*isl_map_empty_like_basic_map(struct isl_basic_map
*model
)
2846 return isl_map_alloc_dim(isl_dim_copy(model
->dim
), 0, ISL_MAP_DISJOINT
);
2849 struct isl_set
*isl_set_empty(struct isl_dim
*dim
)
2851 return isl_set_alloc_dim(dim
, 0, ISL_MAP_DISJOINT
);
2854 struct isl_set
*isl_set_empty_like(struct isl_set
*model
)
2858 return isl_set_empty(isl_dim_copy(model
->dim
));
2861 struct isl_map
*isl_map_universe(struct isl_dim
*dim
)
2863 struct isl_map
*map
;
2866 map
= isl_map_alloc_dim(isl_dim_copy(dim
), 1, ISL_MAP_DISJOINT
);
2867 map
= isl_map_add(map
, isl_basic_map_universe(dim
));
2871 struct isl_set
*isl_set_universe(struct isl_dim
*dim
)
2873 struct isl_set
*set
;
2876 set
= isl_set_alloc_dim(isl_dim_copy(dim
), 1, ISL_MAP_DISJOINT
);
2877 set
= isl_set_add(set
, isl_basic_set_universe(dim
));
2881 struct isl_map
*isl_map_dup(struct isl_map
*map
)
2884 struct isl_map
*dup
;
2888 dup
= isl_map_alloc_dim(isl_dim_copy(map
->dim
), map
->n
, map
->flags
);
2889 for (i
= 0; i
< map
->n
; ++i
)
2890 dup
= isl_map_add(dup
, isl_basic_map_copy(map
->p
[i
]));
2894 struct isl_map
*isl_map_add(struct isl_map
*map
, struct isl_basic_map
*bmap
)
2898 if (isl_basic_map_fast_is_empty(bmap
)) {
2899 isl_basic_map_free(bmap
);
2902 isl_assert(map
->ctx
, isl_dim_equal(map
->dim
, bmap
->dim
), goto error
);
2903 isl_assert(map
->ctx
, map
->n
< map
->size
, goto error
);
2904 map
->p
[map
->n
] = bmap
;
2906 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
2912 isl_basic_map_free(bmap
);
2916 void isl_map_free(struct isl_map
*map
)
2926 isl_ctx_deref(map
->ctx
);
2927 for (i
= 0; i
< map
->n
; ++i
)
2928 isl_basic_map_free(map
->p
[i
]);
2929 isl_dim_free(map
->dim
);
2933 struct isl_map
*isl_map_extend(struct isl_map
*base
,
2934 unsigned nparam
, unsigned n_in
, unsigned n_out
)
2938 base
= isl_map_cow(base
);
2942 base
->dim
= isl_dim_extend(base
->dim
, nparam
, n_in
, n_out
);
2945 for (i
= 0; i
< base
->n
; ++i
) {
2946 base
->p
[i
] = isl_basic_map_extend_dim(base
->p
[i
],
2947 isl_dim_copy(base
->dim
), 0, 0, 0);
2957 struct isl_set
*isl_set_extend(struct isl_set
*base
,
2958 unsigned nparam
, unsigned dim
)
2960 return (struct isl_set
*)isl_map_extend((struct isl_map
*)base
,
2964 static struct isl_basic_map
*isl_basic_map_fix_pos(struct isl_basic_map
*bmap
,
2965 unsigned pos
, int value
)
2969 bmap
= isl_basic_map_cow(bmap
);
2970 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
2971 j
= isl_basic_map_alloc_equality(bmap
);
2974 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
2975 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
2976 isl_int_set_si(bmap
->eq
[j
][0], value
);
2977 bmap
= isl_basic_map_simplify(bmap
);
2978 return isl_basic_map_finalize(bmap
);
2980 isl_basic_map_free(bmap
);
2984 struct isl_basic_map
*isl_basic_map_fix_si(struct isl_basic_map
*bmap
,
2985 enum isl_dim_type type
, unsigned pos
, int value
)
2989 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
2990 return isl_basic_map_fix_pos(bmap
, isl_basic_map_offset(bmap
, type
) + pos
,
2993 isl_basic_map_free(bmap
);
2997 struct isl_basic_set
*isl_basic_set_fix_si(struct isl_basic_set
*bset
,
2998 enum isl_dim_type type
, unsigned pos
, int value
)
3000 return (struct isl_basic_set
*)
3001 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
3005 struct isl_basic_map
*isl_basic_map_fix_input_si(struct isl_basic_map
*bmap
,
3006 unsigned input
, int value
)
3008 return isl_basic_map_fix_si(bmap
, isl_dim_in
, input
, value
);
3011 struct isl_basic_set
*isl_basic_set_fix_dim_si(struct isl_basic_set
*bset
,
3012 unsigned dim
, int value
)
3014 return (struct isl_basic_set
*)
3015 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
3016 isl_dim_set
, dim
, value
);
3019 struct isl_map
*isl_map_fix_si(struct isl_map
*map
,
3020 enum isl_dim_type type
, unsigned pos
, int value
)
3024 map
= isl_map_cow(map
);
3028 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
3029 for (i
= 0; i
< map
->n
; ++i
) {
3030 map
->p
[i
] = isl_basic_map_fix_si(map
->p
[i
], type
, pos
, value
);
3034 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3041 struct isl_map
*isl_map_fix_input_si(struct isl_map
*map
,
3042 unsigned input
, int value
)
3044 return isl_map_fix_si(map
, isl_dim_in
, input
, value
);
3047 struct isl_set
*isl_set_fix_dim_si(struct isl_set
*set
, unsigned dim
, int value
)
3049 return (struct isl_set
*)
3050 isl_map_fix_si((struct isl_map
*)set
, isl_dim_set
, dim
, value
);
3053 struct isl_basic_set
*isl_basic_set_lower_bound_dim(struct isl_basic_set
*bset
,
3054 unsigned dim
, isl_int value
)
3058 bset
= isl_basic_set_cow(bset
);
3059 bset
= isl_basic_set_extend_constraints(bset
, 0, 1);
3060 j
= isl_basic_set_alloc_inequality(bset
);
3063 isl_seq_clr(bset
->ineq
[j
], 1 + isl_basic_set_total_dim(bset
));
3064 isl_int_set_si(bset
->ineq
[j
][1 + isl_basic_set_n_param(bset
) + dim
], 1);
3065 isl_int_neg(bset
->ineq
[j
][0], value
);
3066 bset
= isl_basic_set_simplify(bset
);
3067 return isl_basic_set_finalize(bset
);
3069 isl_basic_set_free(bset
);
3073 struct isl_set
*isl_set_lower_bound_dim(struct isl_set
*set
, unsigned dim
,
3078 set
= isl_set_cow(set
);
3082 isl_assert(set
->ctx
, dim
< isl_set_n_dim(set
), goto error
);
3083 for (i
= 0; i
< set
->n
; ++i
) {
3084 set
->p
[i
] = isl_basic_set_lower_bound_dim(set
->p
[i
], dim
, value
);
3094 struct isl_map
*isl_map_reverse(struct isl_map
*map
)
3098 map
= isl_map_cow(map
);
3102 map
->dim
= isl_dim_reverse(map
->dim
);
3105 for (i
= 0; i
< map
->n
; ++i
) {
3106 map
->p
[i
] = isl_basic_map_reverse(map
->p
[i
]);
3110 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3117 static struct isl_map
*isl_basic_map_partial_lexopt(
3118 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
3119 struct isl_set
**empty
, int max
)
3123 if (bmap
->ctx
->pip
== ISL_PIP_PIP
)
3124 return isl_pip_basic_map_lexopt(bmap
, dom
, empty
, max
);
3126 return isl_tab_basic_map_partial_lexopt(bmap
, dom
, empty
, max
);
3128 isl_basic_map_free(bmap
);
3129 isl_basic_set_free(dom
);
3135 struct isl_map
*isl_basic_map_partial_lexmax(
3136 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
3137 struct isl_set
**empty
)
3139 return isl_basic_map_partial_lexopt(bmap
, dom
, empty
, 1);
3142 struct isl_map
*isl_basic_map_partial_lexmin(
3143 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
3144 struct isl_set
**empty
)
3146 return isl_basic_map_partial_lexopt(bmap
, dom
, empty
, 0);
3149 struct isl_set
*isl_basic_set_partial_lexmin(
3150 struct isl_basic_set
*bset
, struct isl_basic_set
*dom
,
3151 struct isl_set
**empty
)
3153 return (struct isl_set
*)
3154 isl_basic_map_partial_lexmin((struct isl_basic_map
*)bset
,
3158 struct isl_set
*isl_basic_set_partial_lexmax(
3159 struct isl_basic_set
*bset
, struct isl_basic_set
*dom
,
3160 struct isl_set
**empty
)
3162 return (struct isl_set
*)
3163 isl_basic_map_partial_lexmax((struct isl_basic_map
*)bset
,
3167 struct isl_set
*isl_basic_set_lexmin(struct isl_basic_set
*bset
)
3169 struct isl_basic_map
*bmap
= NULL
;
3170 struct isl_basic_set
*dom
= NULL
;
3171 struct isl_map
*min
;
3172 struct isl_dim
*param_dim
;
3176 bmap
= isl_basic_map_from_basic_set(bset
, isl_dim_copy(bset
->dim
));
3179 param_dim
= isl_dim_domain(isl_dim_copy(bmap
->dim
));
3180 dom
= isl_basic_set_universe(param_dim
);
3183 min
= isl_basic_map_partial_lexmin(bmap
, dom
, NULL
);
3184 return isl_map_range(min
);
3186 isl_basic_map_free(bmap
);
3190 static struct isl_map
*isl_map_reset_dim(struct isl_map
*map
,
3191 struct isl_dim
*dim
)
3198 for (i
= 0; i
< map
->n
; ++i
) {
3199 isl_dim_free(map
->p
[i
]->dim
);
3200 map
->p
[i
]->dim
= isl_dim_copy(dim
);
3202 isl_dim_free(map
->dim
);
3212 static struct isl_set
*isl_set_reset_dim(struct isl_set
*set
,
3213 struct isl_dim
*dim
)
3215 return (struct isl_set
*) isl_map_reset_dim((struct isl_map
*)set
, dim
);
3218 /* Apply a preimage specified by "mat" on the parameters of "bset".
3219 * bset is assumed to have only parameters and divs.
3221 static struct isl_basic_set
*basic_set_parameter_preimage(
3222 struct isl_basic_set
*bset
, struct isl_mat
*mat
)
3229 bset
->dim
= isl_dim_cow(bset
->dim
);
3233 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
3235 isl_assert(bset
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
3237 bset
->dim
->nparam
= 0;
3238 bset
->dim
->n_out
= nparam
;
3239 bset
= isl_basic_set_preimage(bset
, mat
);
3241 bset
->dim
->nparam
= bset
->dim
->n_out
;
3242 bset
->dim
->n_out
= 0;
3247 isl_basic_set_free(bset
);
3251 /* Apply a preimage specified by "mat" on the parameters of "set".
3252 * set is assumed to have only parameters and divs.
3254 static struct isl_set
*set_parameter_preimage(
3255 struct isl_set
*set
, struct isl_mat
*mat
)
3257 struct isl_dim
*dim
= NULL
;
3263 dim
= isl_dim_copy(set
->dim
);
3264 dim
= isl_dim_cow(dim
);
3268 nparam
= isl_set_dim(set
, isl_dim_param
);
3270 isl_assert(set
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
3273 dim
->n_out
= nparam
;
3274 isl_set_reset_dim(set
, dim
);
3275 set
= isl_set_preimage(set
, mat
);
3278 dim
= isl_dim_copy(set
->dim
);
3279 dim
= isl_dim_cow(dim
);
3282 dim
->nparam
= dim
->n_out
;
3284 isl_set_reset_dim(set
, dim
);
3294 /* Intersect the basic set "bset" with the affine space specified by the
3295 * equalities in "eq".
3297 static struct isl_basic_set
*basic_set_append_equalities(
3298 struct isl_basic_set
*bset
, struct isl_mat
*eq
)
3306 bset
= isl_basic_set_extend_dim(bset
, isl_dim_copy(bset
->dim
), 0,
3311 len
= 1 + isl_dim_total(bset
->dim
) + bset
->extra
;
3312 for (i
= 0; i
< eq
->n_row
; ++i
) {
3313 k
= isl_basic_set_alloc_equality(bset
);
3316 isl_seq_cpy(bset
->eq
[k
], eq
->row
[i
], eq
->n_col
);
3317 isl_seq_clr(bset
->eq
[k
] + eq
->n_col
, len
- eq
->n_col
);
3324 isl_basic_set_free(bset
);
3328 /* Intersect the set "set" with the affine space specified by the
3329 * equalities in "eq".
3331 static struct isl_set
*set_append_equalities(struct isl_set
*set
,
3339 for (i
= 0; i
< set
->n
; ++i
) {
3340 set
->p
[i
] = basic_set_append_equalities(set
->p
[i
],
3353 /* Project the given basic set onto its parameter domain, possibly introducing
3354 * new, explicit, existential variables in the constraints.
3355 * The input has parameters and (possibly implicit) existential variables.
3356 * The output has the same parameters, but only
3357 * explicit existentially quantified variables.
3359 * The actual projection is performed by pip, but pip doesn't seem
3360 * to like equalities very much, so we first remove the equalities
3361 * among the parameters by performing a variable compression on
3362 * the parameters. Afterward, an inverse transformation is performed
3363 * and the equalities among the parameters are inserted back in.
3365 static struct isl_set
*parameter_compute_divs(struct isl_basic_set
*bset
)
3369 struct isl_mat
*T
, *T2
;
3370 struct isl_set
*set
;
3371 unsigned nparam
, n_div
;
3373 bset
= isl_basic_set_cow(bset
);
3377 if (bset
->n_eq
== 0)
3378 return isl_basic_set_lexmin(bset
);
3380 isl_basic_set_gauss(bset
, NULL
);
3382 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
3383 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
3385 for (i
= 0, j
= n_div
- 1; i
< bset
->n_eq
&& j
>= 0; --j
) {
3386 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ j
]))
3389 if (i
== bset
->n_eq
)
3390 return isl_basic_set_lexmin(bset
);
3392 eq
= isl_mat_sub_alloc(bset
->ctx
, bset
->eq
, i
, bset
->n_eq
- i
,
3394 eq
= isl_mat_cow(eq
);
3395 T
= isl_mat_variable_compression(isl_mat_copy(eq
), &T2
);
3396 bset
= basic_set_parameter_preimage(bset
, T
);
3398 set
= isl_basic_set_lexmin(bset
);
3399 set
= set_parameter_preimage(set
, T2
);
3400 set
= set_append_equalities(set
, eq
);
3404 /* Compute an explicit representation for all the existentially
3405 * quantified variables.
3406 * The input and output dimensions are first turned into parameters.
3407 * compute_divs then returns a map with the same parameters and
3408 * no input or output dimensions and the dimension specification
3409 * is reset to that of the input.
3411 static struct isl_map
*compute_divs(struct isl_basic_map
*bmap
)
3413 struct isl_basic_set
*bset
;
3414 struct isl_set
*set
;
3415 struct isl_map
*map
;
3416 struct isl_dim
*dim
, *orig_dim
= NULL
;
3421 bmap
= isl_basic_map_cow(bmap
);
3425 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
3426 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
3427 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
3428 dim
= isl_dim_set_alloc(bmap
->ctx
, nparam
+ n_in
+ n_out
, 0);
3432 orig_dim
= bmap
->dim
;
3434 bset
= (struct isl_basic_set
*)bmap
;
3436 set
= parameter_compute_divs(bset
);
3437 map
= (struct isl_map
*)set
;
3438 map
= isl_map_reset_dim(map
, orig_dim
);
3442 isl_basic_map_free(bmap
);
3446 /* If bmap contains any unknown divs, then compute explicit
3447 * expressions for them. However, this computation may be
3448 * quite expensive, so first try to remove divs that aren't
3451 struct isl_map
*isl_basic_map_compute_divs(struct isl_basic_map
*bmap
)
3458 off
= isl_dim_total(bmap
->dim
);
3459 for (i
= 0; i
< bmap
->n_div
; ++i
) {
3460 if (isl_int_is_zero(bmap
->div
[i
][0]))
3462 isl_assert(bmap
->ctx
, isl_int_is_zero(bmap
->div
[i
][1+1+off
+i
]),
3465 if (i
== bmap
->n_div
)
3466 return isl_map_from_basic_map(bmap
);
3467 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
3470 for (i
= 0; i
< bmap
->n_div
; ++i
)
3471 if (isl_int_is_zero(bmap
->div
[i
][0]))
3473 if (i
== bmap
->n_div
)
3474 return isl_map_from_basic_map(bmap
);
3475 struct isl_map
*map
= compute_divs(bmap
);
3478 isl_basic_map_free(bmap
);
3482 struct isl_map
*isl_map_compute_divs(struct isl_map
*map
)
3485 struct isl_map
*res
;
3491 res
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[0]));
3492 for (i
= 1 ; i
< map
->n
; ++i
) {
3494 r2
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[i
]));
3495 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
3496 res
= isl_map_union_disjoint(res
, r2
);
3498 res
= isl_map_union(res
, r2
);
3505 struct isl_set
*isl_basic_set_compute_divs(struct isl_basic_set
*bset
)
3507 return (struct isl_set
*)
3508 isl_basic_map_compute_divs((struct isl_basic_map
*)bset
);
3511 struct isl_set
*isl_set_compute_divs(struct isl_set
*set
)
3513 return (struct isl_set
*)
3514 isl_map_compute_divs((struct isl_map
*)set
);
3517 struct isl_set
*isl_map_domain(struct isl_map
*map
)
3520 struct isl_set
*set
;
3525 map
= isl_map_cow(map
);
3529 set
= (struct isl_set
*)map
;
3530 set
->dim
= isl_dim_domain(set
->dim
);
3533 for (i
= 0; i
< map
->n
; ++i
) {
3534 set
->p
[i
] = isl_basic_map_domain(map
->p
[i
]);
3538 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
3539 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
3546 struct isl_map
*isl_map_union_disjoint(
3547 struct isl_map
*map1
, struct isl_map
*map2
)
3551 struct isl_map
*map
= NULL
;
3565 isl_assert(map1
->ctx
, isl_dim_equal(map1
->dim
, map2
->dim
), goto error
);
3567 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
3568 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
3569 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
3571 map
= isl_map_alloc_dim(isl_dim_copy(map1
->dim
),
3572 map1
->n
+ map2
->n
, flags
);
3575 for (i
= 0; i
< map1
->n
; ++i
) {
3576 map
= isl_map_add(map
,
3577 isl_basic_map_copy(map1
->p
[i
]));
3581 for (i
= 0; i
< map2
->n
; ++i
) {
3582 map
= isl_map_add(map
,
3583 isl_basic_map_copy(map2
->p
[i
]));
3597 struct isl_map
*isl_map_union(struct isl_map
*map1
, struct isl_map
*map2
)
3599 map1
= isl_map_union_disjoint(map1
, map2
);
3603 ISL_F_CLR(map1
, ISL_MAP_DISJOINT
);
3607 struct isl_set
*isl_set_union_disjoint(
3608 struct isl_set
*set1
, struct isl_set
*set2
)
3610 return (struct isl_set
*)
3611 isl_map_union_disjoint(
3612 (struct isl_map
*)set1
, (struct isl_map
*)set2
);
3615 struct isl_set
*isl_set_union(struct isl_set
*set1
, struct isl_set
*set2
)
3617 return (struct isl_set
*)
3618 isl_map_union((struct isl_map
*)set1
, (struct isl_map
*)set2
);
3621 struct isl_map
*isl_map_intersect_range(
3622 struct isl_map
*map
, struct isl_set
*set
)
3625 struct isl_map
*result
;
3631 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
) &&
3632 ISL_F_ISSET(set
, ISL_MAP_DISJOINT
))
3633 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
3635 result
= isl_map_alloc_dim(isl_dim_copy(map
->dim
),
3636 map
->n
* set
->n
, flags
);
3639 for (i
= 0; i
< map
->n
; ++i
)
3640 for (j
= 0; j
< set
->n
; ++j
) {
3641 result
= isl_map_add(result
,
3642 isl_basic_map_intersect_range(
3643 isl_basic_map_copy(map
->p
[i
]),
3644 isl_basic_set_copy(set
->p
[j
])));
3657 struct isl_map
*isl_map_intersect_domain(
3658 struct isl_map
*map
, struct isl_set
*set
)
3660 return isl_map_reverse(
3661 isl_map_intersect_range(isl_map_reverse(map
), set
));
3664 struct isl_map
*isl_map_apply_domain(
3665 struct isl_map
*map1
, struct isl_map
*map2
)
3669 map1
= isl_map_reverse(map1
);
3670 map1
= isl_map_apply_range(map1
, map2
);
3671 return isl_map_reverse(map1
);
3678 struct isl_map
*isl_map_apply_range(
3679 struct isl_map
*map1
, struct isl_map
*map2
)
3681 struct isl_dim
*dim_result
;
3682 struct isl_map
*result
;
3688 dim_result
= isl_dim_join(isl_dim_copy(map1
->dim
),
3689 isl_dim_copy(map2
->dim
));
3691 result
= isl_map_alloc_dim(dim_result
, map1
->n
* map2
->n
, 0);
3694 for (i
= 0; i
< map1
->n
; ++i
)
3695 for (j
= 0; j
< map2
->n
; ++j
) {
3696 result
= isl_map_add(result
,
3697 isl_basic_map_apply_range(
3698 isl_basic_map_copy(map1
->p
[i
]),
3699 isl_basic_map_copy(map2
->p
[j
])));
3705 if (result
&& result
->n
<= 1)
3706 ISL_F_SET(result
, ISL_MAP_DISJOINT
);
3715 * returns range - domain
3717 struct isl_basic_set
*isl_basic_map_deltas(struct isl_basic_map
*bmap
)
3719 struct isl_basic_set
*bset
;
3726 dim
= isl_basic_map_n_in(bmap
);
3727 nparam
= isl_basic_map_n_param(bmap
);
3728 isl_assert(bmap
->ctx
, dim
== isl_basic_map_n_out(bmap
), goto error
);
3729 bset
= isl_basic_set_from_basic_map(bmap
);
3730 bset
= isl_basic_set_cow(bset
);
3731 bset
= isl_basic_set_extend(bset
, nparam
, 3*dim
, 0, dim
, 0);
3732 bset
= isl_basic_set_swap_vars(bset
, 2*dim
);
3733 for (i
= 0; i
< dim
; ++i
) {
3734 int j
= isl_basic_map_alloc_equality(
3735 (struct isl_basic_map
*)bset
);
3738 isl_seq_clr(bset
->eq
[j
], 1 + isl_basic_set_total_dim(bset
));
3739 isl_int_set_si(bset
->eq
[j
][1+nparam
+i
], 1);
3740 isl_int_set_si(bset
->eq
[j
][1+nparam
+dim
+i
], 1);
3741 isl_int_set_si(bset
->eq
[j
][1+nparam
+2*dim
+i
], -1);
3743 return isl_basic_set_project_out(bset
, isl_dim_set
, dim
, 2*dim
);
3745 isl_basic_map_free(bmap
);
3750 * returns range - domain
3752 struct isl_set
*isl_map_deltas(struct isl_map
*map
)
3755 struct isl_set
*result
;
3760 isl_assert(map
->ctx
, isl_map_n_in(map
) == isl_map_n_out(map
), goto error
);
3761 result
= isl_set_alloc(map
->ctx
, isl_map_n_param(map
),
3762 isl_map_n_in(map
), map
->n
, map
->flags
);
3765 for (i
= 0; i
< map
->n
; ++i
)
3766 result
= isl_set_add(result
,
3767 isl_basic_map_deltas(isl_basic_map_copy(map
->p
[i
])));
3775 static struct isl_basic_map
*basic_map_identity(struct isl_dim
*dims
)
3777 struct isl_basic_map
*bmap
;
3785 nparam
= dims
->nparam
;
3787 bmap
= isl_basic_map_alloc_dim(dims
, 0, dim
, 0);
3791 for (i
= 0; i
< dim
; ++i
) {
3792 int j
= isl_basic_map_alloc_equality(bmap
);
3795 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
3796 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
3797 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], -1);
3799 return isl_basic_map_finalize(bmap
);
3801 isl_basic_map_free(bmap
);
3805 struct isl_basic_map
*isl_basic_map_identity(struct isl_dim
*set_dim
)
3807 struct isl_dim
*dim
= isl_dim_map(set_dim
);
3810 return basic_map_identity(dim
);
3813 struct isl_basic_map
*isl_basic_map_identity_like(struct isl_basic_map
*model
)
3815 if (!model
|| !model
->dim
)
3817 isl_assert(model
->ctx
,
3818 model
->dim
->n_in
== model
->dim
->n_out
, return NULL
);
3819 return basic_map_identity(isl_dim_copy(model
->dim
));
3822 static struct isl_map
*map_identity(struct isl_dim
*dim
)
3824 struct isl_map
*map
= isl_map_alloc_dim(dim
, 1, ISL_MAP_DISJOINT
);
3825 return isl_map_add(map
, basic_map_identity(isl_dim_copy(dim
)));
3828 struct isl_map
*isl_map_identity(struct isl_dim
*set_dim
)
3830 struct isl_dim
*dim
= isl_dim_map(set_dim
);
3833 return map_identity(dim
);
3836 struct isl_map
*isl_map_identity_like(struct isl_map
*model
)
3838 if (!model
|| !model
->dim
)
3840 isl_assert(model
->ctx
,
3841 model
->dim
->n_in
== model
->dim
->n_out
, return NULL
);
3842 return map_identity(isl_dim_copy(model
->dim
));
3845 struct isl_map
*isl_map_identity_like_basic_map(struct isl_basic_map
*model
)
3847 if (!model
|| !model
->dim
)
3849 isl_assert(model
->ctx
,
3850 model
->dim
->n_in
== model
->dim
->n_out
, return NULL
);
3851 return map_identity(isl_dim_copy(model
->dim
));
3854 /* Construct a basic set with all set dimensions having only non-negative
3857 struct isl_basic_set
*isl_basic_set_positive_orthant(struct isl_dim
*dims
)
3862 struct isl_basic_set
*bset
;
3866 nparam
= dims
->nparam
;
3868 bset
= isl_basic_set_alloc_dim(dims
, 0, 0, dim
);
3871 for (i
= 0; i
< dim
; ++i
) {
3872 int k
= isl_basic_set_alloc_inequality(bset
);
3875 isl_seq_clr(bset
->ineq
[k
], 1 + isl_basic_set_total_dim(bset
));
3876 isl_int_set_si(bset
->ineq
[k
][1 + nparam
+ i
], 1);
3880 isl_basic_set_free(bset
);
3884 int isl_set_is_equal(struct isl_set
*set1
, struct isl_set
*set2
)
3886 return isl_map_is_equal((struct isl_map
*)set1
, (struct isl_map
*)set2
);
3889 int isl_set_is_subset(struct isl_set
*set1
, struct isl_set
*set2
)
3891 return isl_map_is_subset(
3892 (struct isl_map
*)set1
, (struct isl_map
*)set2
);
3895 int isl_basic_map_is_subset(
3896 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3899 struct isl_map
*map1
;
3900 struct isl_map
*map2
;
3902 if (!bmap1
|| !bmap2
)
3905 map1
= isl_map_from_basic_map(isl_basic_map_copy(bmap1
));
3906 map2
= isl_map_from_basic_map(isl_basic_map_copy(bmap2
));
3908 is_subset
= isl_map_is_subset(map1
, map2
);
3916 int isl_basic_map_is_equal(
3917 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3921 if (!bmap1
|| !bmap2
)
3923 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
3926 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
3930 int isl_basic_set_is_equal(
3931 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
3933 return isl_basic_map_is_equal(
3934 (struct isl_basic_map
*)bset1
, (struct isl_basic_map
*)bset2
);
3937 int isl_map_is_empty(struct isl_map
*map
)
3944 for (i
= 0; i
< map
->n
; ++i
) {
3945 is_empty
= isl_basic_map_is_empty(map
->p
[i
]);
3954 int isl_map_fast_is_empty(struct isl_map
*map
)
3959 int isl_set_is_empty(struct isl_set
*set
)
3961 return isl_map_is_empty((struct isl_map
*)set
);
3964 int isl_map_is_subset(struct isl_map
*map1
, struct isl_map
*map2
)
3967 struct isl_map
*diff
;
3972 if (isl_map_is_empty(map1
))
3975 if (isl_map_is_empty(map2
))
3978 diff
= isl_map_subtract(isl_map_copy(map1
), isl_map_copy(map2
));
3982 is_subset
= isl_map_is_empty(diff
);
3988 int isl_map_is_equal(struct isl_map
*map1
, struct isl_map
*map2
)
3994 is_subset
= isl_map_is_subset(map1
, map2
);
3997 is_subset
= isl_map_is_subset(map2
, map1
);
4001 int isl_basic_map_is_strict_subset(
4002 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
4006 if (!bmap1
|| !bmap2
)
4008 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
4011 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
4012 if (is_subset
== -1)
4017 int isl_map_is_strict_subset(struct isl_map
*map1
, struct isl_map
*map2
)
4023 is_subset
= isl_map_is_subset(map1
, map2
);
4026 is_subset
= isl_map_is_subset(map2
, map1
);
4027 if (is_subset
== -1)
4032 int isl_basic_map_is_universe(struct isl_basic_map
*bmap
)
4036 return bmap
->n_eq
== 0 && bmap
->n_ineq
== 0;
4039 int isl_basic_set_is_universe(struct isl_basic_set
*bset
)
4043 return bset
->n_eq
== 0 && bset
->n_ineq
== 0;
4046 int isl_basic_map_is_empty(struct isl_basic_map
*bmap
)
4048 struct isl_basic_set
*bset
= NULL
;
4049 struct isl_vec
*sample
= NULL
;
4056 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
4059 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
4060 struct isl_basic_map
*copy
= isl_basic_map_copy(bmap
);
4061 copy
= isl_basic_map_convex_hull(copy
);
4062 empty
= ISL_F_ISSET(copy
, ISL_BASIC_MAP_EMPTY
);
4063 isl_basic_map_free(copy
);
4067 total
= 1 + isl_basic_map_total_dim(bmap
);
4068 if (bmap
->sample
&& bmap
->sample
->size
== total
) {
4069 int contains
= basic_map_contains(bmap
, bmap
->sample
);
4075 isl_vec_free(bmap
->sample
);
4076 bmap
->sample
= NULL
;
4077 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
4080 sample
= isl_basic_set_sample_vec(bset
);
4083 empty
= sample
->size
== 0;
4084 isl_vec_free(bmap
->sample
);
4085 bmap
->sample
= sample
;
4087 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
4092 int isl_basic_map_fast_is_empty(struct isl_basic_map
*bmap
)
4096 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
);
4099 int isl_basic_set_fast_is_empty(struct isl_basic_set
*bset
)
4103 return ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
);
4106 int isl_basic_set_is_empty(struct isl_basic_set
*bset
)
4108 return isl_basic_map_is_empty((struct isl_basic_map
*)bset
);
4111 struct isl_map
*isl_basic_map_union(
4112 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
4114 struct isl_map
*map
;
4115 if (!bmap1
|| !bmap2
)
4118 isl_assert(bmap1
->ctx
, isl_dim_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
4120 map
= isl_map_alloc_dim(isl_dim_copy(bmap1
->dim
), 2, 0);
4123 map
= isl_map_add(map
, bmap1
);
4124 map
= isl_map_add(map
, bmap2
);
4127 isl_basic_map_free(bmap1
);
4128 isl_basic_map_free(bmap2
);
4132 struct isl_set
*isl_basic_set_union(
4133 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
4135 return (struct isl_set
*)isl_basic_map_union(
4136 (struct isl_basic_map
*)bset1
,
4137 (struct isl_basic_map
*)bset2
);
4140 /* Order divs such that any div only depends on previous divs */
4141 struct isl_basic_map
*isl_basic_map_order_divs(struct isl_basic_map
*bmap
)
4144 unsigned off
= isl_dim_total(bmap
->dim
);
4146 for (i
= 0; i
< bmap
->n_div
; ++i
) {
4148 if (isl_int_is_zero(bmap
->div
[i
][0]))
4150 pos
= isl_seq_first_non_zero(bmap
->div
[i
]+1+1+off
+i
,
4154 isl_basic_map_swap_div(bmap
, i
, i
+ pos
);
4160 struct isl_basic_set
*isl_basic_set_order_divs(struct isl_basic_set
*bset
)
4162 return (struct isl_basic_set
*)
4163 isl_basic_map_order_divs((struct isl_basic_map
*)bset
);
4166 /* Look for a div in dst that corresponds to the div "div" in src.
4167 * The divs before "div" in src and dst are assumed to be the same.
4169 * Returns -1 if no corresponding div was found and the position
4170 * of the corresponding div in dst otherwise.
4172 static int find_div(struct isl_basic_map
*dst
,
4173 struct isl_basic_map
*src
, unsigned div
)
4177 unsigned total
= isl_dim_total(src
->dim
);
4179 isl_assert(dst
->ctx
, div
<= dst
->n_div
, return -1);
4180 for (i
= div
; i
< dst
->n_div
; ++i
)
4181 if (isl_seq_eq(dst
->div
[i
], src
->div
[div
], 1+1+total
+div
) &&
4182 isl_seq_first_non_zero(dst
->div
[i
]+1+1+total
+div
,
4183 dst
->n_div
- div
) == -1)
4188 struct isl_basic_map
*isl_basic_map_align_divs(
4189 struct isl_basic_map
*dst
, struct isl_basic_map
*src
)
4192 unsigned total
= isl_dim_total(src
->dim
);
4197 if (src
->n_div
== 0)
4200 for (i
= 0; i
< src
->n_div
; ++i
)
4201 isl_assert(src
->ctx
, !isl_int_is_zero(src
->div
[i
][0]), goto error
);
4203 src
= isl_basic_map_order_divs(src
);
4204 dst
= isl_basic_map_cow(dst
);
4205 dst
= isl_basic_map_extend_dim(dst
, isl_dim_copy(dst
->dim
),
4206 src
->n_div
, 0, 2 * src
->n_div
);
4209 for (i
= 0; i
< src
->n_div
; ++i
) {
4210 int j
= find_div(dst
, src
, i
);
4212 j
= isl_basic_map_alloc_div(dst
);
4215 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
+i
);
4216 isl_seq_clr(dst
->div
[j
]+1+1+total
+i
, dst
->n_div
- i
);
4217 if (add_div_constraints(dst
, j
) < 0)
4221 isl_basic_map_swap_div(dst
, i
, j
);
4225 isl_basic_map_free(dst
);
4229 struct isl_basic_set
*isl_basic_set_align_divs(
4230 struct isl_basic_set
*dst
, struct isl_basic_set
*src
)
4232 return (struct isl_basic_set
*)isl_basic_map_align_divs(
4233 (struct isl_basic_map
*)dst
, (struct isl_basic_map
*)src
);
4236 struct isl_map
*isl_map_align_divs(struct isl_map
*map
)
4240 map
= isl_map_compute_divs(map
);
4241 map
= isl_map_cow(map
);
4245 for (i
= 1; i
< map
->n
; ++i
)
4246 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], map
->p
[i
]);
4247 for (i
= 1; i
< map
->n
; ++i
)
4248 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], map
->p
[0]);
4250 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4254 struct isl_set
*isl_set_align_divs(struct isl_set
*set
)
4256 return (struct isl_set
*)isl_map_align_divs((struct isl_map
*)set
);
4259 static struct isl_map
*add_cut_constraint(struct isl_map
*dst
,
4260 struct isl_basic_map
*src
, isl_int
*c
,
4261 unsigned len
, int oppose
)
4263 struct isl_basic_map
*copy
= NULL
;
4268 copy
= isl_basic_map_copy(src
);
4269 copy
= isl_basic_map_cow(copy
);
4272 copy
= isl_basic_map_extend_constraints(copy
, 0, 1);
4273 k
= isl_basic_map_alloc_inequality(copy
);
4277 isl_seq_neg(copy
->ineq
[k
], c
, len
);
4279 isl_seq_cpy(copy
->ineq
[k
], c
, len
);
4280 total
= 1 + isl_basic_map_total_dim(copy
);
4281 isl_seq_clr(copy
->ineq
[k
]+len
, total
- len
);
4282 isl_inequality_negate(copy
, k
);
4283 copy
= isl_basic_map_simplify(copy
);
4284 copy
= isl_basic_map_finalize(copy
);
4285 is_empty
= isl_basic_map_is_empty(copy
);
4289 dst
= isl_map_add(dst
, copy
);
4291 isl_basic_map_free(copy
);
4294 isl_basic_map_free(copy
);
4299 static struct isl_map
*subtract(struct isl_map
*map
, struct isl_basic_map
*bmap
)
4303 struct isl_map
*rest
= NULL
;
4305 unsigned total
= isl_basic_map_total_dim(bmap
);
4312 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
4313 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
4315 max
= map
->n
* (2 * bmap
->n_eq
+ bmap
->n_ineq
);
4316 rest
= isl_map_alloc_dim(isl_dim_copy(map
->dim
), max
, flags
);
4320 for (i
= 0; i
< map
->n
; ++i
) {
4321 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], bmap
);
4326 for (j
= 0; j
< map
->n
; ++j
)
4327 map
->p
[j
] = isl_basic_map_cow(map
->p
[j
]);
4329 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
4330 for (j
= 0; j
< map
->n
; ++j
) {
4331 rest
= add_cut_constraint(rest
,
4332 map
->p
[j
], bmap
->eq
[i
], 1+total
, 0);
4336 rest
= add_cut_constraint(rest
,
4337 map
->p
[j
], bmap
->eq
[i
], 1+total
, 1);
4341 map
->p
[j
] = isl_basic_map_extend_constraints(map
->p
[j
],
4345 k
= isl_basic_map_alloc_equality(map
->p
[j
]);
4348 isl_seq_cpy(map
->p
[j
]->eq
[k
], bmap
->eq
[i
], 1+total
);
4349 isl_seq_clr(map
->p
[j
]->eq
[k
]+1+total
,
4350 map
->p
[j
]->n_div
- bmap
->n_div
);
4354 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
4355 for (j
= 0; j
< map
->n
; ++j
) {
4356 rest
= add_cut_constraint(rest
,
4357 map
->p
[j
], bmap
->ineq
[i
], 1+total
, 0);
4361 map
->p
[j
] = isl_basic_map_extend_constraints(map
->p
[j
],
4365 k
= isl_basic_map_alloc_inequality(map
->p
[j
]);
4368 isl_seq_cpy(map
->p
[j
]->ineq
[k
], bmap
->ineq
[i
], 1+total
);
4369 isl_seq_clr(map
->p
[j
]->ineq
[k
]+1+total
,
4370 map
->p
[j
]->n_div
- bmap
->n_div
);
4382 struct isl_map
*isl_map_subtract(struct isl_map
*map1
, struct isl_map
*map2
)
4388 isl_assert(map1
->ctx
, isl_dim_equal(map1
->dim
, map2
->dim
), goto error
);
4390 if (isl_map_is_empty(map2
)) {
4395 map1
= isl_map_compute_divs(map1
);
4396 map2
= isl_map_compute_divs(map2
);
4400 for (i
= 0; map1
&& i
< map2
->n
; ++i
)
4401 map1
= subtract(map1
, map2
->p
[i
]);
4411 struct isl_set
*isl_set_subtract(struct isl_set
*set1
, struct isl_set
*set2
)
4413 return (struct isl_set
*)
4415 (struct isl_map
*)set1
, (struct isl_map
*)set2
);
4418 struct isl_set
*isl_set_apply(struct isl_set
*set
, struct isl_map
*map
)
4422 isl_assert(set
->ctx
, isl_map_compatible_domain(map
, set
), goto error
);
4423 map
= isl_map_intersect_domain(map
, set
);
4424 set
= isl_map_range(map
);
4432 /* There is no need to cow as removing empty parts doesn't change
4433 * the meaning of the set.
4435 struct isl_map
*isl_map_remove_empty_parts(struct isl_map
*map
)
4442 for (i
= map
->n
-1; i
>= 0; --i
) {
4443 if (!ISL_F_ISSET(map
->p
[i
], ISL_BASIC_MAP_EMPTY
))
4445 isl_basic_map_free(map
->p
[i
]);
4446 if (i
!= map
->n
-1) {
4447 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4448 map
->p
[i
] = map
->p
[map
->n
-1];
4456 struct isl_set
*isl_set_remove_empty_parts(struct isl_set
*set
)
4458 return (struct isl_set
*)
4459 isl_map_remove_empty_parts((struct isl_map
*)set
);
4462 struct isl_basic_map
*isl_map_copy_basic_map(struct isl_map
*map
)
4464 struct isl_basic_map
*bmap
;
4465 if (!map
|| map
->n
== 0)
4467 bmap
= map
->p
[map
->n
-1];
4468 isl_assert(map
->ctx
, ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
), return NULL
);
4469 return isl_basic_map_copy(bmap
);
4472 struct isl_basic_set
*isl_set_copy_basic_set(struct isl_set
*set
)
4474 return (struct isl_basic_set
*)
4475 isl_map_copy_basic_map((struct isl_map
*)set
);
4478 struct isl_map
*isl_map_drop_basic_map(struct isl_map
*map
,
4479 struct isl_basic_map
*bmap
)
4485 for (i
= map
->n
-1; i
>= 0; --i
) {
4486 if (map
->p
[i
] != bmap
)
4488 map
= isl_map_cow(map
);
4491 isl_basic_map_free(map
->p
[i
]);
4492 if (i
!= map
->n
-1) {
4493 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
4494 map
->p
[i
] = map
->p
[map
->n
-1];
4499 isl_basic_map_free(bmap
);
4503 isl_basic_map_free(bmap
);
4507 struct isl_set
*isl_set_drop_basic_set(struct isl_set
*set
,
4508 struct isl_basic_set
*bset
)
4510 return (struct isl_set
*)isl_map_drop_basic_map((struct isl_map
*)set
,
4511 (struct isl_basic_map
*)bset
);
4514 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
4515 * for any common value of the parameters and dimensions preceding dim
4516 * in both basic sets, the values of dimension pos in bset1 are
4517 * smaller or larger than those in bset2.
4520 * 1 if bset1 follows bset2
4521 * -1 if bset1 precedes bset2
4522 * 0 if bset1 and bset2 are incomparable
4523 * -2 if some error occurred.
4525 int isl_basic_set_compare_at(struct isl_basic_set
*bset1
,
4526 struct isl_basic_set
*bset2
, int pos
)
4528 struct isl_dim
*dims
;
4529 struct isl_basic_map
*bmap1
= NULL
;
4530 struct isl_basic_map
*bmap2
= NULL
;
4531 struct isl_ctx
*ctx
;
4532 struct isl_vec
*obj
;
4535 unsigned dim1
, dim2
;
4537 enum isl_lp_result res
;
4540 if (!bset1
|| !bset2
)
4543 nparam
= isl_basic_set_n_param(bset1
);
4544 dim1
= isl_basic_set_n_dim(bset1
);
4545 dim2
= isl_basic_set_n_dim(bset2
);
4546 dims
= isl_dim_alloc(bset1
->ctx
, nparam
, pos
, dim1
- pos
);
4547 bmap1
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset1
), dims
);
4548 dims
= isl_dim_alloc(bset2
->ctx
, nparam
, pos
, dim2
- pos
);
4549 bmap2
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset2
), dims
);
4550 if (!bmap1
|| !bmap2
)
4552 bmap1
= isl_basic_map_cow(bmap1
);
4553 bmap1
= isl_basic_map_extend(bmap1
, nparam
,
4554 pos
, (dim1
- pos
) + (dim2
- pos
),
4555 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
4556 bmap1
= add_constraints(bmap1
, bmap2
, 0, dim1
- pos
);
4559 total
= isl_basic_map_total_dim(bmap1
);
4561 obj
= isl_vec_alloc(ctx
, 1 + total
);
4562 isl_seq_clr(obj
->block
.data
, 1 + total
);
4563 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
], 1);
4564 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
+(dim1
-pos
)], -1);
4569 res
= isl_basic_map_solve_lp(bmap1
, 0, obj
->block
.data
, ctx
->one
,
4571 if (res
== isl_lp_empty
)
4573 else if (res
== isl_lp_ok
&& isl_int_is_pos(num
))
4575 else if ((res
== isl_lp_ok
&& isl_int_is_neg(num
)) ||
4576 res
== isl_lp_unbounded
)
4582 isl_basic_map_free(bmap1
);
4586 isl_basic_map_free(bmap1
);
4587 isl_basic_map_free(bmap2
);
4591 static int isl_basic_map_fast_has_fixed_var(struct isl_basic_map
*bmap
,
4592 unsigned pos
, isl_int
*val
)
4600 total
= isl_basic_map_total_dim(bmap
);
4601 for (i
= 0, d
= total
-1; i
< bmap
->n_eq
&& d
+1 > pos
; ++i
) {
4602 for (; d
+1 > pos
; --d
)
4603 if (!isl_int_is_zero(bmap
->eq
[i
][1+d
]))
4607 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1, d
) != -1)
4609 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1+d
+1, total
-d
-1) != -1)
4611 if (!isl_int_is_one(bmap
->eq
[i
][1+d
]))
4614 isl_int_neg(*val
, bmap
->eq
[i
][0]);
4620 static int isl_map_fast_has_fixed_var(struct isl_map
*map
,
4621 unsigned pos
, isl_int
*val
)
4633 return isl_basic_map_fast_has_fixed_var(map
->p
[0], pos
, val
);
4636 fixed
= isl_basic_map_fast_has_fixed_var(map
->p
[0], pos
, &v
);
4637 for (i
= 1; fixed
== 1 && i
< map
->n
; ++i
) {
4638 fixed
= isl_basic_map_fast_has_fixed_var(map
->p
[i
], pos
, &tmp
);
4639 if (fixed
== 1 && isl_int_ne(tmp
, v
))
4643 isl_int_set(*val
, v
);
4649 static int isl_basic_set_fast_has_fixed_var(struct isl_basic_set
*bset
,
4650 unsigned pos
, isl_int
*val
)
4652 return isl_basic_map_fast_has_fixed_var((struct isl_basic_map
*)bset
,
4656 static int isl_set_fast_has_fixed_var(struct isl_set
*set
, unsigned pos
,
4659 return isl_map_fast_has_fixed_var((struct isl_map
*)set
, pos
, val
);
4662 int isl_basic_map_fast_is_fixed(struct isl_basic_map
*bmap
,
4663 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
4665 if (pos
>= isl_basic_map_dim(bmap
, type
))
4667 return isl_basic_map_fast_has_fixed_var(bmap
,
4668 isl_basic_map_offset(bmap
, type
) - 1 + pos
, val
);
4671 int isl_map_fast_is_fixed(struct isl_map
*map
,
4672 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
4674 if (pos
>= isl_map_dim(map
, type
))
4676 return isl_map_fast_has_fixed_var(map
,
4677 map_offset(map
, type
) - 1 + pos
, val
);
4680 /* Check if dimension dim has fixed value and if so and if val is not NULL,
4681 * then return this fixed value in *val.
4683 int isl_basic_set_fast_dim_is_fixed(struct isl_basic_set
*bset
, unsigned dim
,
4686 return isl_basic_set_fast_has_fixed_var(bset
,
4687 isl_basic_set_n_param(bset
) + dim
, val
);
4690 /* Check if dimension dim has fixed value and if so and if val is not NULL,
4691 * then return this fixed value in *val.
4693 int isl_set_fast_dim_is_fixed(struct isl_set
*set
, unsigned dim
, isl_int
*val
)
4695 return isl_set_fast_has_fixed_var(set
, isl_set_n_param(set
) + dim
, val
);
4698 /* Check if input variable in has fixed value and if so and if val is not NULL,
4699 * then return this fixed value in *val.
4701 int isl_map_fast_input_is_fixed(struct isl_map
*map
, unsigned in
, isl_int
*val
)
4703 return isl_map_fast_has_fixed_var(map
, isl_map_n_param(map
) + in
, val
);
4706 /* Check if dimension dim has an (obvious) fixed lower bound and if so
4707 * and if val is not NULL, then return this lower bound in *val.
4709 int isl_basic_set_fast_dim_has_fixed_lower_bound(struct isl_basic_set
*bset
,
4710 unsigned dim
, isl_int
*val
)
4712 int i
, i_eq
= -1, i_ineq
= -1;
4719 total
= isl_basic_set_total_dim(bset
);
4720 nparam
= isl_basic_set_n_param(bset
);
4721 for (i
= 0; i
< bset
->n_eq
; ++i
) {
4722 if (isl_int_is_zero(bset
->eq
[i
][1+nparam
+dim
]))
4728 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
4729 if (!isl_int_is_pos(bset
->ineq
[i
][1+nparam
+dim
]))
4731 if (i_eq
!= -1 || i_ineq
!= -1)
4735 if (i_eq
== -1 && i_ineq
== -1)
4737 c
= i_eq
!= -1 ? bset
->eq
[i_eq
] : bset
->ineq
[i_ineq
];
4738 /* The coefficient should always be one due to normalization. */
4739 if (!isl_int_is_one(c
[1+nparam
+dim
]))
4741 if (isl_seq_first_non_zero(c
+1, nparam
+dim
) != -1)
4743 if (isl_seq_first_non_zero(c
+1+nparam
+dim
+1,
4744 total
- nparam
- dim
- 1) != -1)
4747 isl_int_neg(*val
, c
[0]);
4751 int isl_set_fast_dim_has_fixed_lower_bound(struct isl_set
*set
,
4752 unsigned dim
, isl_int
*val
)
4764 return isl_basic_set_fast_dim_has_fixed_lower_bound(set
->p
[0],
4768 fixed
= isl_basic_set_fast_dim_has_fixed_lower_bound(set
->p
[0],
4770 for (i
= 1; fixed
== 1 && i
< set
->n
; ++i
) {
4771 fixed
= isl_basic_set_fast_dim_has_fixed_lower_bound(set
->p
[i
],
4773 if (fixed
== 1 && isl_int_ne(tmp
, v
))
4777 isl_int_set(*val
, v
);
4788 static int qsort_constraint_cmp(const void *p1
, const void *p2
)
4790 const struct constraint
*c1
= (const struct constraint
*)p1
;
4791 const struct constraint
*c2
= (const struct constraint
*)p2
;
4792 unsigned size
= isl_min(c1
->size
, c2
->size
);
4793 return isl_seq_cmp(c1
->c
, c2
->c
, size
);
4796 static struct isl_basic_map
*isl_basic_map_sort_constraints(
4797 struct isl_basic_map
*bmap
)
4800 struct constraint
*c
;
4805 total
= isl_basic_map_total_dim(bmap
);
4806 c
= isl_alloc_array(bmap
->ctx
, struct constraint
, bmap
->n_ineq
);
4809 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
4811 c
[i
].c
= bmap
->ineq
[i
];
4813 qsort(c
, bmap
->n_ineq
, sizeof(struct constraint
), qsort_constraint_cmp
);
4814 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
4815 bmap
->ineq
[i
] = c
[i
].c
;
4819 isl_basic_map_free(bmap
);
4823 struct isl_basic_map
*isl_basic_map_normalize(struct isl_basic_map
*bmap
)
4827 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED
))
4829 bmap
= isl_basic_map_convex_hull(bmap
);
4830 bmap
= isl_basic_map_sort_constraints(bmap
);
4831 ISL_F_SET(bmap
, ISL_BASIC_MAP_NORMALIZED
);
4835 struct isl_basic_set
*isl_basic_set_normalize(struct isl_basic_set
*bset
)
4837 return (struct isl_basic_set
*)isl_basic_map_normalize(
4838 (struct isl_basic_map
*)bset
);
4841 static int isl_basic_map_fast_cmp(const struct isl_basic_map
*bmap1
,
4842 const struct isl_basic_map
*bmap2
)
4849 if (isl_basic_map_n_param(bmap1
) != isl_basic_map_n_param(bmap2
))
4850 return isl_basic_map_n_param(bmap1
) - isl_basic_map_n_param(bmap2
);
4851 if (isl_basic_map_n_in(bmap1
) != isl_basic_map_n_in(bmap2
))
4852 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
4853 if (isl_basic_map_n_out(bmap1
) != isl_basic_map_n_out(bmap2
))
4854 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
4855 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
) &&
4856 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
4858 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
))
4860 if (ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
4862 if (bmap1
->n_eq
!= bmap2
->n_eq
)
4863 return bmap1
->n_eq
- bmap2
->n_eq
;
4864 if (bmap1
->n_ineq
!= bmap2
->n_ineq
)
4865 return bmap1
->n_ineq
- bmap2
->n_ineq
;
4866 if (bmap1
->n_div
!= bmap2
->n_div
)
4867 return bmap1
->n_div
- bmap2
->n_div
;
4868 total
= isl_basic_map_total_dim(bmap1
);
4869 for (i
= 0; i
< bmap1
->n_eq
; ++i
) {
4870 cmp
= isl_seq_cmp(bmap1
->eq
[i
], bmap2
->eq
[i
], 1+total
);
4874 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
4875 cmp
= isl_seq_cmp(bmap1
->ineq
[i
], bmap2
->ineq
[i
], 1+total
);
4879 for (i
= 0; i
< bmap1
->n_div
; ++i
) {
4880 cmp
= isl_seq_cmp(bmap1
->div
[i
], bmap2
->div
[i
], 1+1+total
);
4887 static int isl_basic_map_fast_is_equal(struct isl_basic_map
*bmap1
,
4888 struct isl_basic_map
*bmap2
)
4890 return isl_basic_map_fast_cmp(bmap1
, bmap2
) == 0;
4893 static int qsort_bmap_cmp(const void *p1
, const void *p2
)
4895 const struct isl_basic_map
*bmap1
= *(const struct isl_basic_map
**)p1
;
4896 const struct isl_basic_map
*bmap2
= *(const struct isl_basic_map
**)p2
;
4898 return isl_basic_map_fast_cmp(bmap1
, bmap2
);
4901 /* We normalize in place, but if anything goes wrong we need
4902 * to return NULL, so we need to make sure we don't change the
4903 * meaning of any possible other copies of map.
4905 struct isl_map
*isl_map_normalize(struct isl_map
*map
)
4908 struct isl_basic_map
*bmap
;
4912 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
))
4914 for (i
= 0; i
< map
->n
; ++i
) {
4915 bmap
= isl_basic_map_normalize(isl_basic_map_copy(map
->p
[i
]));
4918 isl_basic_map_free(map
->p
[i
]);
4921 qsort(map
->p
, map
->n
, sizeof(struct isl_basic_map
*), qsort_bmap_cmp
);
4922 ISL_F_SET(map
, ISL_MAP_NORMALIZED
);
4923 map
= isl_map_remove_empty_parts(map
);
4926 for (i
= map
->n
- 1; i
>= 1; --i
) {
4927 if (!isl_basic_map_fast_is_equal(map
->p
[i
-1], map
->p
[i
]))
4929 isl_basic_map_free(map
->p
[i
-1]);
4930 for (j
= i
; j
< map
->n
; ++j
)
4931 map
->p
[j
-1] = map
->p
[j
];
4941 struct isl_set
*isl_set_normalize(struct isl_set
*set
)
4943 return (struct isl_set
*)isl_map_normalize((struct isl_map
*)set
);
4946 int isl_map_fast_is_equal(struct isl_map
*map1
, struct isl_map
*map2
)
4956 if (!isl_dim_equal(map1
->dim
, map2
->dim
))
4959 map1
= isl_map_copy(map1
);
4960 map2
= isl_map_copy(map2
);
4961 map1
= isl_map_normalize(map1
);
4962 map2
= isl_map_normalize(map2
);
4965 equal
= map1
->n
== map2
->n
;
4966 for (i
= 0; equal
&& i
< map1
->n
; ++i
) {
4967 equal
= isl_basic_map_fast_is_equal(map1
->p
[i
], map2
->p
[i
]);
4980 int isl_set_fast_is_equal(struct isl_set
*set1
, struct isl_set
*set2
)
4982 return isl_map_fast_is_equal((struct isl_map
*)set1
,
4983 (struct isl_map
*)set2
);
4986 /* Return an interval that ranges from min to max (inclusive)
4988 struct isl_basic_set
*isl_basic_set_interval(struct isl_ctx
*ctx
,
4989 isl_int min
, isl_int max
)
4992 struct isl_basic_set
*bset
= NULL
;
4994 bset
= isl_basic_set_alloc(ctx
, 0, 1, 0, 0, 2);
4998 k
= isl_basic_set_alloc_inequality(bset
);
5001 isl_int_set_si(bset
->ineq
[k
][1], 1);
5002 isl_int_neg(bset
->ineq
[k
][0], min
);
5004 k
= isl_basic_set_alloc_inequality(bset
);
5007 isl_int_set_si(bset
->ineq
[k
][1], -1);
5008 isl_int_set(bset
->ineq
[k
][0], max
);
5012 isl_basic_set_free(bset
);
5016 /* Return the Cartesian product of the basic sets in list (in the given order).
5018 struct isl_basic_set
*isl_basic_set_product(struct isl_basic_set_list
*list
)
5026 struct isl_basic_set
*product
= NULL
;
5030 isl_assert(list
->ctx
, list
->n
> 0, goto error
);
5031 isl_assert(list
->ctx
, list
->p
[0], goto error
);
5032 nparam
= isl_basic_set_n_param(list
->p
[0]);
5033 dim
= isl_basic_set_n_dim(list
->p
[0]);
5034 extra
= list
->p
[0]->n_div
;
5035 n_eq
= list
->p
[0]->n_eq
;
5036 n_ineq
= list
->p
[0]->n_ineq
;
5037 for (i
= 1; i
< list
->n
; ++i
) {
5038 isl_assert(list
->ctx
, list
->p
[i
], goto error
);
5039 isl_assert(list
->ctx
,
5040 nparam
== isl_basic_set_n_param(list
->p
[i
]), goto error
);
5041 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
5042 extra
+= list
->p
[i
]->n_div
;
5043 n_eq
+= list
->p
[i
]->n_eq
;
5044 n_ineq
+= list
->p
[i
]->n_ineq
;
5046 product
= isl_basic_set_alloc(list
->ctx
, nparam
, dim
, extra
,
5051 for (i
= 0; i
< list
->n
; ++i
) {
5052 isl_basic_set_add_constraints(product
,
5053 isl_basic_set_copy(list
->p
[i
]), dim
);
5054 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
5056 isl_basic_set_list_free(list
);
5059 isl_basic_set_free(product
);
5060 isl_basic_set_list_free(list
);
5064 struct isl_basic_map
*isl_basic_map_product(
5065 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
5067 struct isl_dim
*dim_result
= NULL
;
5068 struct isl_basic_map
*bmap
;
5069 unsigned in1
, in2
, out1
, out2
, nparam
, total
, pos
;
5070 struct isl_dim_map
*dim_map1
, *dim_map2
;
5072 if (!bmap1
|| !bmap2
)
5075 isl_assert(bmap1
->ctx
, isl_dim_match(bmap1
->dim
, isl_dim_param
,
5076 bmap2
->dim
, isl_dim_param
), goto error
);
5077 dim_result
= isl_dim_product(isl_dim_copy(bmap1
->dim
),
5078 isl_dim_copy(bmap2
->dim
));
5080 in1
= isl_basic_map_n_in(bmap1
);
5081 in2
= isl_basic_map_n_in(bmap2
);
5082 out1
= isl_basic_map_n_out(bmap1
);
5083 out2
= isl_basic_map_n_out(bmap2
);
5084 nparam
= isl_basic_map_n_param(bmap1
);
5086 total
= nparam
+ in1
+ in2
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
5087 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
5088 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
5089 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
5090 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
5091 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
5092 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
5093 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
5094 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
5095 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
5096 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
5098 bmap
= isl_basic_map_alloc_dim(dim_result
,
5099 bmap1
->n_div
+ bmap2
->n_div
,
5100 bmap1
->n_eq
+ bmap2
->n_eq
,
5101 bmap1
->n_ineq
+ bmap2
->n_ineq
);
5102 bmap
= add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
5103 bmap
= add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
5104 bmap
= isl_basic_map_simplify(bmap
);
5105 return isl_basic_map_finalize(bmap
);
5107 isl_basic_map_free(bmap1
);
5108 isl_basic_map_free(bmap2
);
5112 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
5114 struct isl_map
*isl_map_product(struct isl_map
*map1
, struct isl_map
*map2
)
5117 struct isl_map
*result
;
5123 isl_assert(map1
->ctx
, isl_dim_match(map1
->dim
, isl_dim_param
,
5124 map2
->dim
, isl_dim_param
), goto error
);
5126 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
5127 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
5128 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
5130 result
= isl_map_alloc_dim(isl_dim_product(isl_dim_copy(map1
->dim
),
5131 isl_dim_copy(map2
->dim
)),
5132 map1
->n
* map2
->n
, flags
);
5135 for (i
= 0; i
< map1
->n
; ++i
)
5136 for (j
= 0; j
< map2
->n
; ++j
) {
5137 struct isl_basic_map
*part
;
5138 part
= isl_basic_map_product(
5139 isl_basic_map_copy(map1
->p
[i
]),
5140 isl_basic_map_copy(map2
->p
[j
]));
5141 if (isl_basic_map_is_empty(part
))
5142 isl_basic_map_free(part
);
5144 result
= isl_map_add(result
, part
);
5157 /* Given two set A and B, construct its Cartesian product A x B.
5159 struct isl_set
*isl_set_product(struct isl_set
*set1
, struct isl_set
*set2
)
5161 return (struct isl_set
*)isl_map_product((struct isl_map
*)set1
,
5162 (struct isl_map
*)set2
);
5165 uint32_t isl_basic_set_get_hash(struct isl_basic_set
*bset
)
5168 uint32_t hash
= isl_hash_init();
5173 bset
= isl_basic_set_copy(bset
);
5174 bset
= isl_basic_set_normalize(bset
);
5177 total
= isl_basic_set_total_dim(bset
);
5178 isl_hash_byte(hash
, bset
->n_eq
& 0xFF);
5179 for (i
= 0; i
< bset
->n_eq
; ++i
) {
5181 c_hash
= isl_seq_get_hash(bset
->eq
[i
], 1 + total
);
5182 isl_hash_hash(hash
, c_hash
);
5184 isl_hash_byte(hash
, bset
->n_ineq
& 0xFF);
5185 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
5187 c_hash
= isl_seq_get_hash(bset
->ineq
[i
], 1 + total
);
5188 isl_hash_hash(hash
, c_hash
);
5190 isl_hash_byte(hash
, bset
->n_div
& 0xFF);
5191 for (i
= 0; i
< bset
->n_div
; ++i
) {
5193 if (isl_int_is_zero(bset
->div
[i
][0]))
5195 isl_hash_byte(hash
, i
& 0xFF);
5196 c_hash
= isl_seq_get_hash(bset
->div
[i
], 1 + 1 + total
);
5197 isl_hash_hash(hash
, c_hash
);
5199 isl_basic_set_free(bset
);
5203 uint32_t isl_set_get_hash(struct isl_set
*set
)
5210 set
= isl_set_copy(set
);
5211 set
= isl_set_normalize(set
);
5215 hash
= isl_hash_init();
5216 for (i
= 0; i
< set
->n
; ++i
) {
5218 bset_hash
= isl_basic_set_get_hash(set
->p
[i
]);
5219 isl_hash_hash(hash
, bset_hash
);
5227 /* Check if the value for dimension dim is completely determined
5228 * by the values of the other parameters and variables.
5229 * That is, check if dimension dim is involved in an equality.
5231 int isl_basic_set_dim_is_unique(struct isl_basic_set
*bset
, unsigned dim
)
5238 nparam
= isl_basic_set_n_param(bset
);
5239 for (i
= 0; i
< bset
->n_eq
; ++i
)
5240 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ dim
]))
5245 /* Check if the value for dimension dim is completely determined
5246 * by the values of the other parameters and variables.
5247 * That is, check if dimension dim is involved in an equality
5248 * for each of the subsets.
5250 int isl_set_dim_is_unique(struct isl_set
*set
, unsigned dim
)
5256 for (i
= 0; i
< set
->n
; ++i
) {
5258 unique
= isl_basic_set_dim_is_unique(set
->p
[i
], dim
);