6 #include "isl_equalities.h"
12 #include "isl_map_private.h"
13 #include "isl_map_piplib.h"
14 #include "isl_sample.h"
18 /* Maps dst positions to src positions */
24 static struct isl_dim_map
*isl_dim_map_alloc(struct isl_ctx
*ctx
, unsigned len
)
27 struct isl_dim_map
*dim_map
;
28 dim_map
= isl_alloc(ctx
, struct isl_dim_map
,
29 sizeof(struct isl_dim_map
) + len
* sizeof(int));
32 dim_map
->len
= 1 + len
;
34 for (i
= 0; i
< len
; ++i
)
35 dim_map
->pos
[1 + i
] = -1;
39 static unsigned n(struct isl_dim
*dim
, enum isl_dim_type type
)
42 case isl_dim_param
: return dim
->nparam
;
43 case isl_dim_in
: return dim
->n_in
;
44 case isl_dim_out
: return dim
->n_out
;
45 case isl_dim_all
: return dim
->nparam
+ dim
->n_in
+ dim
->n_out
;
49 static unsigned pos(struct isl_dim
*dim
, enum isl_dim_type type
)
52 case isl_dim_param
: return 1;
53 case isl_dim_in
: return 1 + dim
->nparam
;
54 case isl_dim_out
: return 1 + dim
->nparam
+ dim
->n_in
;
58 static void isl_dim_map_dim(struct isl_dim_map
*dim_map
, struct isl_dim
*dim
,
59 enum isl_dim_type type
, unsigned dst_pos
)
67 src_pos
= pos(dim
, type
);
68 for (i
= 0; i
< n(dim
, type
); ++i
)
69 dim_map
->pos
[1 + dst_pos
+ i
] = src_pos
+ i
;
72 static void isl_dim_map_div(struct isl_dim_map
*dim_map
,
73 struct isl_basic_map
*bmap
, unsigned dst_pos
)
78 if (!dim_map
|| !bmap
)
81 src_pos
= 1 + isl_dim_total(bmap
->dim
);
82 for (i
= 0; i
< bmap
->n_div
; ++i
)
83 dim_map
->pos
[1 + dst_pos
+ i
] = src_pos
+ i
;
86 static void isl_dim_map_dump(struct isl_dim_map
*dim_map
)
90 for (i
= 0; i
< dim_map
->len
; ++i
)
91 fprintf(stderr
, "%d -> %d; ", i
, dim_map
->pos
[i
]);
92 fprintf(stderr
, "\n");
95 unsigned isl_basic_map_dim(const struct isl_basic_map
*bmap
,
96 enum isl_dim_type type
)
98 struct isl_dim
*dim
= bmap
->dim
;
102 case isl_dim_out
: return isl_dim_size(bmap
->dim
, type
);
103 case isl_dim_div
: return bmap
->n_div
;
104 case isl_dim_all
: return isl_basic_map_total_dim(bmap
);
108 unsigned isl_map_dim(const struct isl_map
*map
, enum isl_dim_type type
)
110 return n(map
->dim
, type
);
113 unsigned isl_set_dim(const struct isl_set
*set
, enum isl_dim_type type
)
115 return n(set
->dim
, type
);
118 unsigned isl_basic_map_offset(struct isl_basic_map
*bmap
,
119 enum isl_dim_type type
)
121 struct isl_dim
*dim
= bmap
->dim
;
123 case isl_dim_param
: return 1;
124 case isl_dim_in
: return 1 + dim
->nparam
;
125 case isl_dim_out
: return 1 + dim
->nparam
+ dim
->n_in
;
126 case isl_dim_div
: return 1 + dim
->nparam
+ dim
->n_in
+ dim
->n_out
;
130 static unsigned map_offset(struct isl_map
*map
, enum isl_dim_type type
)
132 return pos(map
->dim
, type
);
135 unsigned isl_basic_set_dim(const struct isl_basic_set
*bset
,
136 enum isl_dim_type type
)
138 return isl_basic_map_dim((const struct isl_basic_map
*)bset
, type
);
141 unsigned isl_basic_set_n_dim(const struct isl_basic_set
*bset
)
143 return bset
->dim
->n_out
;
146 unsigned isl_basic_set_n_param(const struct isl_basic_set
*bset
)
148 return bset
->dim
->nparam
;
151 unsigned isl_basic_set_total_dim(const struct isl_basic_set
*bset
)
153 return isl_dim_total(bset
->dim
) + bset
->n_div
;
156 unsigned isl_set_n_dim(const struct isl_set
*set
)
158 return set
->dim
->n_out
;
161 unsigned isl_set_n_param(const struct isl_set
*set
)
163 return set
->dim
->nparam
;
166 unsigned isl_basic_map_n_in(const struct isl_basic_map
*bmap
)
168 return bmap
->dim
->n_in
;
171 unsigned isl_basic_map_n_out(const struct isl_basic_map
*bmap
)
173 return bmap
->dim
->n_out
;
176 unsigned isl_basic_map_n_param(const struct isl_basic_map
*bmap
)
178 return bmap
->dim
->nparam
;
181 unsigned isl_basic_map_n_div(const struct isl_basic_map
*bmap
)
186 unsigned isl_basic_map_total_dim(const struct isl_basic_map
*bmap
)
188 return isl_dim_total(bmap
->dim
) + bmap
->n_div
;
191 unsigned isl_map_n_in(const struct isl_map
*map
)
193 return map
->dim
->n_in
;
196 unsigned isl_map_n_out(const struct isl_map
*map
)
198 return map
->dim
->n_out
;
201 unsigned isl_map_n_param(const struct isl_map
*map
)
203 return map
->dim
->nparam
;
206 int isl_map_compatible_domain(struct isl_map
*map
, struct isl_set
*set
)
208 return map
->dim
->n_in
== set
->dim
->n_out
&&
209 map
->dim
->nparam
== set
->dim
->nparam
;
212 int isl_basic_map_compatible_domain(struct isl_basic_map
*bmap
,
213 struct isl_basic_set
*bset
)
215 return bmap
->dim
->n_in
== bset
->dim
->n_out
&&
216 bmap
->dim
->nparam
== bset
->dim
->nparam
;
219 int isl_basic_map_compatible_range(struct isl_basic_map
*bmap
,
220 struct isl_basic_set
*bset
)
222 return bmap
->dim
->n_out
== bset
->dim
->n_out
&&
223 bmap
->dim
->nparam
== bset
->dim
->nparam
;
226 struct isl_dim
*isl_basic_map_get_dim(struct isl_basic_map
*bmap
)
230 return isl_dim_copy(bmap
->dim
);
233 struct isl_dim
*isl_basic_set_get_dim(struct isl_basic_set
*bset
)
237 return isl_dim_copy(bset
->dim
);
240 struct isl_dim
*isl_map_get_dim(struct isl_map
*map
)
244 return isl_dim_copy(map
->dim
);
247 struct isl_dim
*isl_set_get_dim(struct isl_set
*set
)
251 return isl_dim_copy(set
->dim
);
254 static struct isl_basic_map
*basic_map_init(struct isl_ctx
*ctx
,
255 struct isl_basic_map
*bmap
, unsigned extra
,
256 unsigned n_eq
, unsigned n_ineq
)
259 size_t row_size
= 1 + isl_dim_total(bmap
->dim
) + extra
;
261 bmap
->block
= isl_blk_alloc(ctx
, (n_ineq
+ n_eq
) * row_size
);
262 if (isl_blk_is_error(bmap
->block
)) {
267 bmap
->ineq
= isl_alloc_array(ctx
, isl_int
*, n_ineq
+ n_eq
);
269 isl_blk_free(ctx
, bmap
->block
);
275 bmap
->block2
= isl_blk_empty();
278 bmap
->block2
= isl_blk_alloc(ctx
, extra
* (1 + row_size
));
279 if (isl_blk_is_error(bmap
->block2
)) {
281 isl_blk_free(ctx
, bmap
->block
);
286 bmap
->div
= isl_alloc_array(ctx
, isl_int
*, extra
);
288 isl_blk_free(ctx
, bmap
->block2
);
290 isl_blk_free(ctx
, bmap
->block
);
296 for (i
= 0; i
< n_ineq
+ n_eq
; ++i
)
297 bmap
->ineq
[i
] = bmap
->block
.data
+ i
* row_size
;
299 for (i
= 0; i
< extra
; ++i
)
300 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + row_size
);
306 bmap
->c_size
= n_eq
+ n_ineq
;
307 bmap
->eq
= bmap
->ineq
+ n_ineq
;
316 isl_basic_map_free(bmap
);
320 struct isl_basic_set
*isl_basic_set_alloc(struct isl_ctx
*ctx
,
321 unsigned nparam
, unsigned dim
, unsigned extra
,
322 unsigned n_eq
, unsigned n_ineq
)
324 struct isl_basic_map
*bmap
;
325 bmap
= isl_basic_map_alloc(ctx
, nparam
, 0, dim
, extra
, n_eq
, n_ineq
);
326 return (struct isl_basic_set
*)bmap
;
329 struct isl_basic_set
*isl_basic_set_alloc_dim(struct isl_dim
*dim
,
330 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
332 struct isl_basic_map
*bmap
;
335 isl_assert(dim
->ctx
, dim
->n_in
== 0, return NULL
);
336 bmap
= isl_basic_map_alloc_dim(dim
, extra
, n_eq
, n_ineq
);
337 return (struct isl_basic_set
*)bmap
;
340 struct isl_basic_map
*isl_basic_map_alloc_dim(struct isl_dim
*dim
,
341 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
343 struct isl_basic_map
*bmap
;
347 bmap
= isl_alloc_type(dim
->ctx
, struct isl_basic_map
);
352 return basic_map_init(dim
->ctx
, bmap
, extra
, n_eq
, n_ineq
);
358 struct isl_basic_map
*isl_basic_map_alloc(struct isl_ctx
*ctx
,
359 unsigned nparam
, unsigned in
, unsigned out
, unsigned extra
,
360 unsigned n_eq
, unsigned n_ineq
)
362 struct isl_basic_map
*bmap
;
365 dim
= isl_dim_alloc(ctx
, nparam
, in
, out
);
369 bmap
= isl_basic_map_alloc_dim(dim
, extra
, n_eq
, n_ineq
);
373 static void dup_constraints(
374 struct isl_basic_map
*dst
, struct isl_basic_map
*src
)
377 unsigned total
= isl_basic_map_total_dim(src
);
379 for (i
= 0; i
< src
->n_eq
; ++i
) {
380 int j
= isl_basic_map_alloc_equality(dst
);
381 isl_seq_cpy(dst
->eq
[j
], src
->eq
[i
], 1+total
);
384 for (i
= 0; i
< src
->n_ineq
; ++i
) {
385 int j
= isl_basic_map_alloc_inequality(dst
);
386 isl_seq_cpy(dst
->ineq
[j
], src
->ineq
[i
], 1+total
);
389 for (i
= 0; i
< src
->n_div
; ++i
) {
390 int j
= isl_basic_map_alloc_div(dst
);
391 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
);
393 ISL_F_SET(dst
, ISL_BASIC_SET_FINAL
);
396 struct isl_basic_map
*isl_basic_map_dup(struct isl_basic_map
*bmap
)
398 struct isl_basic_map
*dup
;
402 dup
= isl_basic_map_alloc_dim(isl_dim_copy(bmap
->dim
),
403 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
406 dup_constraints(dup
, bmap
);
407 dup
->flags
= bmap
->flags
;
408 dup
->sample
= isl_vec_copy(bmap
->sample
);
412 struct isl_basic_set
*isl_basic_set_dup(struct isl_basic_set
*bset
)
414 struct isl_basic_map
*dup
;
416 dup
= isl_basic_map_dup((struct isl_basic_map
*)bset
);
417 return (struct isl_basic_set
*)dup
;
420 struct isl_basic_set
*isl_basic_set_copy(struct isl_basic_set
*bset
)
425 if (ISL_F_ISSET(bset
, ISL_BASIC_SET_FINAL
)) {
429 return isl_basic_set_dup(bset
);
432 struct isl_set
*isl_set_copy(struct isl_set
*set
)
441 struct isl_basic_map
*isl_basic_map_copy(struct isl_basic_map
*bmap
)
446 if (ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
)) {
450 return isl_basic_map_dup(bmap
);
453 struct isl_map
*isl_map_copy(struct isl_map
*map
)
462 void isl_basic_map_free(struct isl_basic_map
*bmap
)
470 isl_ctx_deref(bmap
->ctx
);
472 isl_blk_free(bmap
->ctx
, bmap
->block2
);
474 isl_blk_free(bmap
->ctx
, bmap
->block
);
475 isl_vec_free(bmap
->sample
);
476 isl_dim_free(bmap
->dim
);
480 void isl_basic_set_free(struct isl_basic_set
*bset
)
482 isl_basic_map_free((struct isl_basic_map
*)bset
);
485 static int room_for_con(struct isl_basic_map
*bmap
, unsigned n
)
487 return bmap
->n_eq
+ bmap
->n_ineq
+ n
<= bmap
->c_size
;
490 int isl_basic_map_alloc_equality(struct isl_basic_map
*bmap
)
496 isl_assert(ctx
, room_for_con(bmap
, 1), return -1);
497 isl_assert(ctx
, (bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
<= bmap
->c_size
,
499 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
500 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
501 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
502 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
503 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
504 if ((bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
== bmap
->c_size
) {
506 int j
= isl_basic_map_alloc_inequality(bmap
);
510 bmap
->ineq
[j
] = bmap
->ineq
[bmap
->n_ineq
- 1];
511 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
518 isl_seq_clr(bmap
->eq
[bmap
->n_eq
] + 1 + isl_basic_map_total_dim(bmap
),
519 bmap
->extra
- bmap
->n_div
);
523 int isl_basic_set_alloc_equality(struct isl_basic_set
*bset
)
525 return isl_basic_map_alloc_equality((struct isl_basic_map
*)bset
);
528 int isl_basic_map_free_equality(struct isl_basic_map
*bmap
, unsigned n
)
532 isl_assert(bmap
->ctx
, n
<= bmap
->n_eq
, return -1);
537 int isl_basic_set_free_equality(struct isl_basic_set
*bset
, unsigned n
)
539 return isl_basic_map_free_equality((struct isl_basic_map
*)bset
, n
);
542 int isl_basic_map_drop_equality(struct isl_basic_map
*bmap
, unsigned pos
)
547 isl_assert(bmap
->ctx
, pos
< bmap
->n_eq
, return -1);
549 if (pos
!= bmap
->n_eq
- 1) {
551 bmap
->eq
[pos
] = bmap
->eq
[bmap
->n_eq
- 1];
552 bmap
->eq
[bmap
->n_eq
- 1] = t
;
558 int isl_basic_set_drop_equality(struct isl_basic_set
*bset
, unsigned pos
)
560 return isl_basic_map_drop_equality((struct isl_basic_map
*)bset
, pos
);
563 void isl_basic_map_inequality_to_equality(
564 struct isl_basic_map
*bmap
, unsigned pos
)
569 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
570 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
575 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
576 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
577 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
578 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
581 static int room_for_ineq(struct isl_basic_map
*bmap
, unsigned n
)
583 return bmap
->n_ineq
+ n
<= bmap
->eq
- bmap
->ineq
;
586 int isl_basic_map_alloc_inequality(struct isl_basic_map
*bmap
)
592 isl_assert(ctx
, room_for_ineq(bmap
, 1), return -1);
593 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
594 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
595 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
596 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
597 isl_seq_clr(bmap
->ineq
[bmap
->n_ineq
] +
598 1 + isl_basic_map_total_dim(bmap
),
599 bmap
->extra
- bmap
->n_div
);
600 return bmap
->n_ineq
++;
603 int isl_basic_set_alloc_inequality(struct isl_basic_set
*bset
)
605 return isl_basic_map_alloc_inequality((struct isl_basic_map
*)bset
);
608 int isl_basic_map_free_inequality(struct isl_basic_map
*bmap
, unsigned n
)
612 isl_assert(bmap
->ctx
, n
<= bmap
->n_ineq
, return -1);
617 int isl_basic_set_free_inequality(struct isl_basic_set
*bset
, unsigned n
)
619 return isl_basic_map_free_inequality((struct isl_basic_map
*)bset
, n
);
622 int isl_basic_map_drop_inequality(struct isl_basic_map
*bmap
, unsigned pos
)
627 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
629 if (pos
!= bmap
->n_ineq
- 1) {
631 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
632 bmap
->ineq
[bmap
->n_ineq
- 1] = t
;
633 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
639 int isl_basic_set_drop_inequality(struct isl_basic_set
*bset
, unsigned pos
)
641 return isl_basic_map_drop_inequality((struct isl_basic_map
*)bset
, pos
);
644 int isl_basic_map_alloc_div(struct isl_basic_map
*bmap
)
648 isl_assert(bmap
->ctx
, bmap
->n_div
< bmap
->extra
, return -1);
649 isl_seq_clr(bmap
->div
[bmap
->n_div
] +
650 1 + 1 + isl_basic_map_total_dim(bmap
),
651 bmap
->extra
- bmap
->n_div
);
652 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
653 return bmap
->n_div
++;
656 int isl_basic_set_alloc_div(struct isl_basic_set
*bset
)
658 return isl_basic_map_alloc_div((struct isl_basic_map
*)bset
);
661 int isl_basic_map_free_div(struct isl_basic_map
*bmap
, unsigned n
)
665 isl_assert(bmap
->ctx
, n
<= bmap
->n_div
, return -1);
670 int isl_basic_set_free_div(struct isl_basic_set
*bset
, unsigned n
)
672 return isl_basic_map_free_div((struct isl_basic_map
*)bset
, n
);
675 /* Copy constraint from src to dst, putting the vars of src at offset
676 * dim_off in dst and the divs of src at offset div_off in dst.
677 * If both sets are actually map, then dim_off applies to the input
680 static void copy_constraint(struct isl_basic_map
*dst_map
, isl_int
*dst
,
681 struct isl_basic_map
*src_map
, isl_int
*src
,
682 unsigned in_off
, unsigned out_off
, unsigned div_off
)
684 unsigned src_nparam
= isl_basic_map_n_param(src_map
);
685 unsigned dst_nparam
= isl_basic_map_n_param(dst_map
);
686 unsigned src_in
= isl_basic_map_n_in(src_map
);
687 unsigned dst_in
= isl_basic_map_n_in(dst_map
);
688 unsigned src_out
= isl_basic_map_n_out(src_map
);
689 unsigned dst_out
= isl_basic_map_n_out(dst_map
);
690 isl_int_set(dst
[0], src
[0]);
691 isl_seq_cpy(dst
+1, src
+1, isl_min(dst_nparam
, src_nparam
));
692 if (dst_nparam
> src_nparam
)
693 isl_seq_clr(dst
+1+src_nparam
,
694 dst_nparam
- src_nparam
);
695 isl_seq_clr(dst
+1+dst_nparam
, in_off
);
696 isl_seq_cpy(dst
+1+dst_nparam
+in_off
,
698 isl_min(dst_in
-in_off
, src_in
));
699 if (dst_in
-in_off
> src_in
)
700 isl_seq_clr(dst
+1+dst_nparam
+in_off
+src_in
,
701 dst_in
- in_off
- src_in
);
702 isl_seq_clr(dst
+1+dst_nparam
+dst_in
, out_off
);
703 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+out_off
,
704 src
+1+src_nparam
+src_in
,
705 isl_min(dst_out
-out_off
, src_out
));
706 if (dst_out
-out_off
> src_out
)
707 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+out_off
+src_out
,
708 dst_out
- out_off
- src_out
);
709 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
, div_off
);
710 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+dst_out
+div_off
,
711 src
+1+src_nparam
+src_in
+src_out
,
712 isl_min(dst_map
->extra
-div_off
, src_map
->n_div
));
713 if (dst_map
->n_div
-div_off
> src_map
->n_div
)
714 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
+
715 div_off
+src_map
->n_div
,
716 dst_map
->n_div
- div_off
- src_map
->n_div
);
719 static void copy_div(struct isl_basic_map
*dst_map
, isl_int
*dst
,
720 struct isl_basic_map
*src_map
, isl_int
*src
,
721 unsigned in_off
, unsigned out_off
, unsigned div_off
)
723 isl_int_set(dst
[0], src
[0]);
724 copy_constraint(dst_map
, dst
+1, src_map
, src
+1, in_off
, out_off
, div_off
);
727 static struct isl_basic_map
*add_constraints(struct isl_basic_map
*bmap1
,
728 struct isl_basic_map
*bmap2
, unsigned i_pos
, unsigned o_pos
)
733 if (!bmap1
|| !bmap2
)
736 div_off
= bmap1
->n_div
;
738 for (i
= 0; i
< bmap2
->n_eq
; ++i
) {
739 int i1
= isl_basic_map_alloc_equality(bmap1
);
742 copy_constraint(bmap1
, bmap1
->eq
[i1
], bmap2
, bmap2
->eq
[i
],
743 i_pos
, o_pos
, div_off
);
746 for (i
= 0; i
< bmap2
->n_ineq
; ++i
) {
747 int i1
= isl_basic_map_alloc_inequality(bmap1
);
750 copy_constraint(bmap1
, bmap1
->ineq
[i1
], bmap2
, bmap2
->ineq
[i
],
751 i_pos
, o_pos
, div_off
);
754 for (i
= 0; i
< bmap2
->n_div
; ++i
) {
755 int i1
= isl_basic_map_alloc_div(bmap1
);
758 copy_div(bmap1
, bmap1
->div
[i1
], bmap2
, bmap2
->div
[i
],
759 i_pos
, o_pos
, div_off
);
762 isl_basic_map_free(bmap2
);
767 isl_basic_map_free(bmap1
);
768 isl_basic_map_free(bmap2
);
772 static void copy_constraint_dim_map(isl_int
*dst
, isl_int
*src
,
773 struct isl_dim_map
*dim_map
)
777 for (i
= 0; i
< dim_map
->len
; ++i
) {
778 if (dim_map
->pos
[i
] < 0)
779 isl_int_set_si(dst
[i
], 0);
781 isl_int_set(dst
[i
], src
[dim_map
->pos
[i
]]);
785 static void copy_div_dim_map(isl_int
*dst
, isl_int
*src
,
786 struct isl_dim_map
*dim_map
)
788 isl_int_set(dst
[0], src
[0]);
789 copy_constraint_dim_map(dst
+1, src
+1, dim_map
);
792 static struct isl_basic_map
*add_constraints_dim_map(struct isl_basic_map
*dst
,
793 struct isl_basic_map
*src
, struct isl_dim_map
*dim_map
)
797 if (!src
|| !dst
|| !dim_map
)
800 for (i
= 0; i
< src
->n_eq
; ++i
) {
801 int i1
= isl_basic_map_alloc_equality(dst
);
804 copy_constraint_dim_map(dst
->eq
[i1
], src
->eq
[i
], dim_map
);
807 for (i
= 0; i
< src
->n_ineq
; ++i
) {
808 int i1
= isl_basic_map_alloc_inequality(dst
);
811 copy_constraint_dim_map(dst
->ineq
[i1
], src
->ineq
[i
], dim_map
);
814 for (i
= 0; i
< src
->n_div
; ++i
) {
815 int i1
= isl_basic_map_alloc_div(dst
);
818 copy_div_dim_map(dst
->div
[i1
], src
->div
[i
], dim_map
);
822 isl_basic_map_free(src
);
827 isl_basic_map_free(src
);
828 isl_basic_map_free(dst
);
832 struct isl_basic_set
*isl_basic_set_add_constraints(struct isl_basic_set
*bset1
,
833 struct isl_basic_set
*bset2
, unsigned pos
)
835 return (struct isl_basic_set
*)
836 add_constraints((struct isl_basic_map
*)bset1
,
837 (struct isl_basic_map
*)bset2
, 0, pos
);
840 struct isl_basic_map
*isl_basic_map_extend_dim(struct isl_basic_map
*base
,
841 struct isl_dim
*dim
, unsigned extra
,
842 unsigned n_eq
, unsigned n_ineq
)
844 struct isl_basic_map
*ext
;
854 dims_ok
= isl_dim_equal(base
->dim
, dim
) &&
855 base
->extra
>= base
->n_div
+ extra
;
857 if (dims_ok
&& room_for_con(base
, n_eq
+ n_ineq
) &&
858 room_for_ineq(base
, n_ineq
)) {
863 isl_assert(base
->ctx
, base
->dim
->nparam
<= dim
->nparam
, goto error
);
864 isl_assert(base
->ctx
, base
->dim
->n_in
<= dim
->n_in
, goto error
);
865 isl_assert(base
->ctx
, base
->dim
->n_out
<= dim
->n_out
, goto error
);
866 extra
+= base
->extra
;
868 n_ineq
+= base
->n_ineq
;
870 ext
= isl_basic_map_alloc_dim(dim
, extra
, n_eq
, n_ineq
);
876 ext
->sample
= isl_vec_copy(base
->sample
);
878 ext
= add_constraints(ext
, base
, 0, 0);
881 ISL_F_CLR(ext
, ISL_BASIC_SET_FINAL
);
888 isl_basic_map_free(base
);
892 struct isl_basic_set
*isl_basic_set_extend_dim(struct isl_basic_set
*base
,
893 struct isl_dim
*dim
, unsigned extra
,
894 unsigned n_eq
, unsigned n_ineq
)
896 return (struct isl_basic_set
*)
897 isl_basic_map_extend_dim((struct isl_basic_map
*)base
, dim
,
898 extra
, n_eq
, n_ineq
);
901 struct isl_basic_map
*isl_basic_map_extend_constraints(
902 struct isl_basic_map
*base
, unsigned n_eq
, unsigned n_ineq
)
906 return isl_basic_map_extend_dim(base
, isl_dim_copy(base
->dim
),
910 struct isl_basic_map
*isl_basic_map_extend(struct isl_basic_map
*base
,
911 unsigned nparam
, unsigned n_in
, unsigned n_out
, unsigned extra
,
912 unsigned n_eq
, unsigned n_ineq
)
914 struct isl_basic_map
*bmap
;
919 dim
= isl_dim_alloc(base
->ctx
, nparam
, n_in
, n_out
);
923 bmap
= isl_basic_map_extend_dim(base
, dim
, extra
, n_eq
, n_ineq
);
927 struct isl_basic_set
*isl_basic_set_extend(struct isl_basic_set
*base
,
928 unsigned nparam
, unsigned dim
, unsigned extra
,
929 unsigned n_eq
, unsigned n_ineq
)
931 return (struct isl_basic_set
*)
932 isl_basic_map_extend((struct isl_basic_map
*)base
,
933 nparam
, 0, dim
, extra
, n_eq
, n_ineq
);
936 struct isl_basic_set
*isl_basic_set_extend_constraints(
937 struct isl_basic_set
*base
, unsigned n_eq
, unsigned n_ineq
)
939 return (struct isl_basic_set
*)
940 isl_basic_map_extend_constraints((struct isl_basic_map
*)base
,
944 struct isl_basic_set
*isl_basic_set_cow(struct isl_basic_set
*bset
)
946 return (struct isl_basic_set
*)
947 isl_basic_map_cow((struct isl_basic_map
*)bset
);
950 struct isl_basic_map
*isl_basic_map_cow(struct isl_basic_map
*bmap
)
957 bmap
= isl_basic_map_dup(bmap
);
959 ISL_F_CLR(bmap
, ISL_BASIC_SET_FINAL
);
963 struct isl_set
*isl_set_cow(struct isl_set
*set
)
971 return isl_set_dup(set
);
974 struct isl_map
*isl_map_cow(struct isl_map
*map
)
982 return isl_map_dup(map
);
985 static void swap_vars(struct isl_blk blk
, isl_int
*a
,
986 unsigned a_len
, unsigned b_len
)
988 isl_seq_cpy(blk
.data
, a
+a_len
, b_len
);
989 isl_seq_cpy(blk
.data
+b_len
, a
, a_len
);
990 isl_seq_cpy(a
, blk
.data
, b_len
+a_len
);
993 struct isl_basic_set
*isl_basic_set_swap_vars(
994 struct isl_basic_set
*bset
, unsigned n
)
1004 nparam
= isl_basic_set_n_param(bset
);
1005 dim
= isl_basic_set_n_dim(bset
);
1006 isl_assert(bset
->ctx
, n
<= dim
, goto error
);
1011 bset
= isl_basic_set_cow(bset
);
1015 blk
= isl_blk_alloc(bset
->ctx
, dim
);
1016 if (isl_blk_is_error(blk
))
1019 for (i
= 0; i
< bset
->n_eq
; ++i
)
1021 bset
->eq
[i
]+1+nparam
, n
, dim
- n
);
1023 for (i
= 0; i
< bset
->n_ineq
; ++i
)
1025 bset
->ineq
[i
]+1+nparam
, n
, dim
- n
);
1027 for (i
= 0; i
< bset
->n_div
; ++i
)
1029 bset
->div
[i
]+1+1+nparam
, n
, dim
- n
);
1031 isl_blk_free(bset
->ctx
, blk
);
1033 ISL_F_CLR(bset
, ISL_BASIC_SET_NORMALIZED
);
1037 isl_basic_set_free(bset
);
1041 struct isl_set
*isl_set_swap_vars(struct isl_set
*set
, unsigned n
)
1044 set
= isl_set_cow(set
);
1048 for (i
= 0; i
< set
->n
; ++i
) {
1049 set
->p
[i
] = isl_basic_set_swap_vars(set
->p
[i
], n
);
1055 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
1059 struct isl_basic_map
*isl_basic_map_set_to_empty(struct isl_basic_map
*bmap
)
1065 total
= isl_basic_map_total_dim(bmap
);
1066 isl_basic_map_free_div(bmap
, bmap
->n_div
);
1067 isl_basic_map_free_inequality(bmap
, bmap
->n_ineq
);
1069 isl_basic_map_free_equality(bmap
, bmap
->n_eq
-1);
1071 isl_basic_map_alloc_equality(bmap
);
1075 isl_int_set_si(bmap
->eq
[i
][0], 1);
1076 isl_seq_clr(bmap
->eq
[i
]+1, total
);
1077 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
1078 return isl_basic_map_finalize(bmap
);
1080 isl_basic_map_free(bmap
);
1084 struct isl_basic_set
*isl_basic_set_set_to_empty(struct isl_basic_set
*bset
)
1086 return (struct isl_basic_set
*)
1087 isl_basic_map_set_to_empty((struct isl_basic_map
*)bset
);
1090 void isl_basic_map_swap_div(struct isl_basic_map
*bmap
, int a
, int b
)
1093 unsigned off
= isl_dim_total(bmap
->dim
);
1094 isl_int
*t
= bmap
->div
[a
];
1095 bmap
->div
[a
] = bmap
->div
[b
];
1098 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1099 isl_int_swap(bmap
->eq
[i
][1+off
+a
], bmap
->eq
[i
][1+off
+b
]);
1101 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1102 isl_int_swap(bmap
->ineq
[i
][1+off
+a
], bmap
->ineq
[i
][1+off
+b
]);
1104 for (i
= 0; i
< bmap
->n_div
; ++i
)
1105 isl_int_swap(bmap
->div
[i
][1+1+off
+a
], bmap
->div
[i
][1+1+off
+b
]);
1106 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1109 /* Eliminate the specified n dimensions starting at first from the
1110 * constraints using Fourier-Motzkin, The dimensions themselves
1113 struct isl_set
*isl_set_eliminate_dims(struct isl_set
*set
,
1114 unsigned first
, unsigned n
)
1124 set
= isl_set_cow(set
);
1127 isl_assert(set
->ctx
, first
+n
<= isl_set_n_dim(set
), goto error
);
1128 nparam
= isl_set_n_param(set
);
1130 for (i
= 0; i
< set
->n
; ++i
) {
1131 set
->p
[i
] = isl_basic_set_eliminate_vars(set
->p
[i
],
1142 /* Project out n dimensions starting at first using Fourier-Motzkin */
1143 struct isl_set
*isl_set_remove_dims(struct isl_set
*set
,
1144 unsigned first
, unsigned n
)
1146 set
= isl_set_eliminate_dims(set
, first
, n
);
1147 set
= isl_set_drop_dims(set
, first
, n
);
1151 struct isl_basic_set
*isl_basic_set_remove_divs(struct isl_basic_set
*bset
)
1153 bset
= isl_basic_set_eliminate_vars(bset
, isl_dim_total(bset
->dim
),
1161 struct isl_set
*isl_set_remove_divs(struct isl_set
*set
)
1170 set
= isl_set_cow(set
);
1174 for (i
= 0; i
< set
->n
; ++i
) {
1175 set
->p
[i
] = isl_basic_set_remove_divs(set
->p
[i
]);
1185 struct isl_basic_map
*isl_basic_map_remove(struct isl_basic_map
*bmap
,
1186 enum isl_dim_type type
, unsigned first
, unsigned n
)
1190 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
1194 bmap
= isl_basic_map_eliminate_vars(bmap
,
1195 isl_basic_map_offset(bmap
, type
) - 1 + first
, n
);
1198 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
) && type
== isl_dim_div
)
1200 bmap
= isl_basic_map_drop(bmap
, type
, first
, n
);
1203 isl_basic_map_free(bmap
);
1207 struct isl_map
*isl_map_remove(struct isl_map
*map
,
1208 enum isl_dim_type type
, unsigned first
, unsigned n
)
1216 map
= isl_map_cow(map
);
1219 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
1221 for (i
= 0; i
< map
->n
; ++i
) {
1222 map
->p
[i
] = isl_basic_map_eliminate_vars(map
->p
[i
],
1223 isl_basic_map_offset(map
->p
[i
], type
) - 1 + first
, n
);
1227 map
= isl_map_drop(map
, type
, first
, n
);
1234 /* Project out n inputs starting at first using Fourier-Motzkin */
1235 struct isl_map
*isl_map_remove_inputs(struct isl_map
*map
,
1236 unsigned first
, unsigned n
)
1238 return isl_map_remove(map
, isl_dim_in
, first
, n
);
1241 /* Project out n dimensions starting at first using Fourier-Motzkin */
1242 struct isl_basic_set
*isl_basic_set_remove_dims(struct isl_basic_set
*bset
,
1243 unsigned first
, unsigned n
)
1245 unsigned nparam
= isl_basic_set_n_param(bset
);
1246 bset
= isl_basic_set_eliminate_vars(bset
, nparam
+ first
, n
);
1247 bset
= isl_basic_set_drop_dims(bset
, first
, n
);
1251 static void dump_term(struct isl_basic_map
*bmap
,
1252 isl_int c
, int pos
, FILE *out
)
1255 unsigned in
= isl_basic_map_n_in(bmap
);
1256 unsigned dim
= in
+ isl_basic_map_n_out(bmap
);
1257 unsigned nparam
= isl_basic_map_n_param(bmap
);
1259 isl_int_print(out
, c
, 0);
1261 if (!isl_int_is_one(c
))
1262 isl_int_print(out
, c
, 0);
1263 if (pos
< 1 + nparam
) {
1264 name
= isl_dim_get_name(bmap
->dim
,
1265 isl_dim_param
, pos
- 1);
1267 fprintf(out
, "%s", name
);
1269 fprintf(out
, "p%d", pos
- 1);
1270 } else if (pos
< 1 + nparam
+ in
)
1271 fprintf(out
, "i%d", pos
- 1 - nparam
);
1272 else if (pos
< 1 + nparam
+ dim
)
1273 fprintf(out
, "o%d", pos
- 1 - nparam
- in
);
1275 fprintf(out
, "e%d", pos
- 1 - nparam
- dim
);
1279 static void dump_constraint_sign(struct isl_basic_map
*bmap
, isl_int
*c
,
1280 int sign
, FILE *out
)
1284 unsigned len
= 1 + isl_basic_map_total_dim(bmap
);
1288 for (i
= 0, first
= 1; i
< len
; ++i
) {
1289 if (isl_int_sgn(c
[i
]) * sign
<= 0)
1292 fprintf(out
, " + ");
1294 isl_int_abs(v
, c
[i
]);
1295 dump_term(bmap
, v
, i
, out
);
1302 static void dump_constraint(struct isl_basic_map
*bmap
, isl_int
*c
,
1303 const char *op
, FILE *out
, int indent
)
1307 fprintf(out
, "%*s", indent
, "");
1309 dump_constraint_sign(bmap
, c
, 1, out
);
1310 fprintf(out
, " %s ", op
);
1311 dump_constraint_sign(bmap
, c
, -1, out
);
1315 for (i
= bmap
->n_div
; i
< bmap
->extra
; ++i
) {
1316 if (isl_int_is_zero(c
[1+isl_dim_total(bmap
->dim
)+i
]))
1318 fprintf(out
, "%*s", indent
, "");
1319 fprintf(out
, "ERROR: unused div coefficient not zero\n");
1324 static void dump_constraints(struct isl_basic_map
*bmap
,
1325 isl_int
**c
, unsigned n
,
1326 const char *op
, FILE *out
, int indent
)
1330 for (i
= 0; i
< n
; ++i
)
1331 dump_constraint(bmap
, c
[i
], op
, out
, indent
);
1334 static void dump_affine(struct isl_basic_map
*bmap
, isl_int
*exp
, FILE *out
)
1338 unsigned total
= isl_basic_map_total_dim(bmap
);
1340 for (j
= 0; j
< 1 + total
; ++j
) {
1341 if (isl_int_is_zero(exp
[j
]))
1343 if (!first
&& isl_int_is_pos(exp
[j
]))
1345 dump_term(bmap
, exp
[j
], j
, out
);
1350 static void dump(struct isl_basic_map
*bmap
, FILE *out
, int indent
)
1354 dump_constraints(bmap
, bmap
->eq
, bmap
->n_eq
, "=", out
, indent
);
1355 dump_constraints(bmap
, bmap
->ineq
, bmap
->n_ineq
, ">=", out
, indent
);
1357 for (i
= 0; i
< bmap
->n_div
; ++i
) {
1358 fprintf(out
, "%*s", indent
, "");
1359 fprintf(out
, "e%d = [(", i
);
1360 dump_affine(bmap
, bmap
->div
[i
]+1, out
);
1362 isl_int_print(out
, bmap
->div
[i
][0], 0);
1363 fprintf(out
, "]\n");
1367 void isl_basic_set_dump(struct isl_basic_set
*bset
, FILE *out
, int indent
)
1370 fprintf(out
, "null basic set\n");
1374 fprintf(out
, "%*s", indent
, "");
1375 fprintf(out
, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
1376 bset
->ref
, bset
->dim
->nparam
, bset
->dim
->n_out
,
1377 bset
->extra
, bset
->flags
);
1378 dump((struct isl_basic_map
*)bset
, out
, indent
);
1381 void isl_basic_map_dump(struct isl_basic_map
*bmap
, FILE *out
, int indent
)
1384 fprintf(out
, "null basic map\n");
1388 fprintf(out
, "%*s", indent
, "");
1389 fprintf(out
, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
1390 "flags: %x, n_name: %d\n",
1392 bmap
->dim
->nparam
, bmap
->dim
->n_in
, bmap
->dim
->n_out
,
1393 bmap
->extra
, bmap
->flags
, bmap
->dim
->n_name
);
1394 dump(bmap
, out
, indent
);
1397 int isl_inequality_negate(struct isl_basic_map
*bmap
, unsigned pos
)
1402 total
= isl_basic_map_total_dim(bmap
);
1403 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
1404 isl_seq_neg(bmap
->ineq
[pos
], bmap
->ineq
[pos
], 1 + total
);
1405 isl_int_sub_ui(bmap
->ineq
[pos
][0], bmap
->ineq
[pos
][0], 1);
1406 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1410 struct isl_set
*isl_set_alloc_dim(struct isl_dim
*dim
, int n
, unsigned flags
)
1412 struct isl_set
*set
;
1416 isl_assert(dim
->ctx
, dim
->n_in
== 0, return NULL
);
1417 isl_assert(dim
->ctx
, n
>= 0, return NULL
);
1418 set
= isl_alloc(dim
->ctx
, struct isl_set
,
1419 sizeof(struct isl_set
) +
1420 (n
- 1) * sizeof(struct isl_basic_set
*));
1424 set
->ctx
= dim
->ctx
;
1425 isl_ctx_ref(set
->ctx
);
1437 struct isl_set
*isl_set_alloc(struct isl_ctx
*ctx
,
1438 unsigned nparam
, unsigned dim
, int n
, unsigned flags
)
1440 struct isl_set
*set
;
1441 struct isl_dim
*dims
;
1443 dims
= isl_dim_alloc(ctx
, nparam
, 0, dim
);
1447 set
= isl_set_alloc_dim(dims
, n
, flags
);
1451 /* Make sure "map" has room for at least "n" more basic maps.
1453 struct isl_map
*isl_map_grow(struct isl_map
*map
, int n
)
1456 struct isl_map
*grown
= NULL
;
1460 isl_assert(map
->ctx
, n
>= 0, goto error
);
1461 if (map
->n
+ n
<= map
->size
)
1463 grown
= isl_map_alloc_dim(isl_map_get_dim(map
), map
->n
+ n
, map
->flags
);
1466 for (i
= 0; i
< map
->n
; ++i
) {
1467 grown
->p
[i
] = isl_basic_map_copy(map
->p
[i
]);
1475 isl_map_free(grown
);
1480 /* Make sure "set" has room for at least "n" more basic sets.
1482 struct isl_set
*isl_set_grow(struct isl_set
*set
, int n
)
1484 return (struct isl_set
*)isl_map_grow((struct isl_map
*)set
, n
);
1487 struct isl_set
*isl_set_dup(struct isl_set
*set
)
1490 struct isl_set
*dup
;
1495 dup
= isl_set_alloc_dim(isl_dim_copy(set
->dim
), set
->n
, set
->flags
);
1498 for (i
= 0; i
< set
->n
; ++i
)
1499 dup
= isl_set_add(dup
, isl_basic_set_copy(set
->p
[i
]));
1503 struct isl_set
*isl_set_from_basic_set(struct isl_basic_set
*bset
)
1505 struct isl_set
*set
;
1510 set
= isl_set_alloc_dim(isl_dim_copy(bset
->dim
), 1, ISL_MAP_DISJOINT
);
1512 isl_basic_set_free(bset
);
1515 return isl_set_add(set
, bset
);
1518 struct isl_map
*isl_map_from_basic_map(struct isl_basic_map
*bmap
)
1520 struct isl_map
*map
;
1525 map
= isl_map_alloc_dim(isl_dim_copy(bmap
->dim
), 1, ISL_MAP_DISJOINT
);
1527 isl_basic_map_free(bmap
);
1530 return isl_map_add(map
, bmap
);
1533 struct isl_set
*isl_set_add(struct isl_set
*set
, struct isl_basic_set
*bset
)
1535 return (struct isl_set
*)isl_map_add((struct isl_map
*)set
,
1536 (struct isl_basic_map
*)bset
);
1539 void isl_set_free(struct isl_set
*set
)
1549 isl_ctx_deref(set
->ctx
);
1550 for (i
= 0; i
< set
->n
; ++i
)
1551 isl_basic_set_free(set
->p
[i
]);
1552 isl_dim_free(set
->dim
);
1556 void isl_set_dump(struct isl_set
*set
, FILE *out
, int indent
)
1561 fprintf(out
, "null set\n");
1565 fprintf(out
, "%*s", indent
, "");
1566 fprintf(out
, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
1567 set
->ref
, set
->n
, set
->dim
->nparam
, set
->dim
->n_out
,
1569 for (i
= 0; i
< set
->n
; ++i
) {
1570 fprintf(out
, "%*s", indent
, "");
1571 fprintf(out
, "basic set %d:\n", i
);
1572 isl_basic_set_dump(set
->p
[i
], out
, indent
+4);
1576 void isl_map_dump(struct isl_map
*map
, FILE *out
, int indent
)
1581 fprintf(out
, "null map\n");
1585 fprintf(out
, "%*s", indent
, "");
1586 fprintf(out
, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
1587 "flags: %x, n_name: %d\n",
1588 map
->ref
, map
->n
, map
->dim
->nparam
, map
->dim
->n_in
,
1589 map
->dim
->n_out
, map
->flags
, map
->dim
->n_name
);
1590 for (i
= 0; i
< map
->n
; ++i
) {
1591 fprintf(out
, "%*s", indent
, "");
1592 fprintf(out
, "basic map %d:\n", i
);
1593 isl_basic_map_dump(map
->p
[i
], out
, indent
+4);
1597 struct isl_basic_map
*isl_basic_map_intersect_domain(
1598 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
1600 struct isl_basic_map
*bmap_domain
;
1601 struct isl_dim
*dim
;
1606 isl_assert(bset
->ctx
, isl_dim_match(bmap
->dim
, isl_dim_param
,
1607 bset
->dim
, isl_dim_param
), goto error
);
1609 if (isl_dim_size(bset
->dim
, isl_dim_set
) != 0)
1610 isl_assert(bset
->ctx
,
1611 isl_basic_map_compatible_domain(bmap
, bset
), goto error
);
1613 bmap
= isl_basic_map_cow(bmap
);
1614 bmap
= isl_basic_map_extend_dim(bmap
, isl_dim_copy(bmap
->dim
),
1615 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
1618 dim
= isl_dim_reverse(isl_dim_copy(bset
->dim
));
1619 bmap_domain
= isl_basic_map_from_basic_set(bset
, dim
);
1620 bmap
= add_constraints(bmap
, bmap_domain
, 0, 0);
1622 bmap
= isl_basic_map_simplify(bmap
);
1623 return isl_basic_map_finalize(bmap
);
1625 isl_basic_map_free(bmap
);
1626 isl_basic_set_free(bset
);
1630 struct isl_basic_map
*isl_basic_map_intersect_range(
1631 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
1633 struct isl_basic_map
*bmap_range
;
1638 isl_assert(bset
->ctx
, isl_dim_match(bmap
->dim
, isl_dim_param
,
1639 bset
->dim
, isl_dim_param
), goto error
);
1641 if (isl_dim_size(bset
->dim
, isl_dim_set
) != 0)
1642 isl_assert(bset
->ctx
,
1643 isl_basic_map_compatible_range(bmap
, bset
), goto error
);
1645 bmap
= isl_basic_map_cow(bmap
);
1646 bmap
= isl_basic_map_extend_dim(bmap
, isl_dim_copy(bmap
->dim
),
1647 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
1650 bmap_range
= isl_basic_map_from_basic_set(bset
, isl_dim_copy(bset
->dim
));
1651 bmap
= add_constraints(bmap
, bmap_range
, 0, 0);
1653 bmap
= isl_basic_map_simplify(bmap
);
1654 return isl_basic_map_finalize(bmap
);
1656 isl_basic_map_free(bmap
);
1657 isl_basic_set_free(bset
);
1661 static int basic_map_contains(struct isl_basic_map
*bmap
, struct isl_vec
*vec
)
1667 total
= 1 + isl_basic_map_total_dim(bmap
);
1668 if (total
!= vec
->size
)
1673 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
1674 isl_seq_inner_product(vec
->el
, bmap
->eq
[i
], total
, &s
);
1675 if (!isl_int_is_zero(s
)) {
1681 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
1682 isl_seq_inner_product(vec
->el
, bmap
->ineq
[i
], total
, &s
);
1683 if (isl_int_is_neg(s
)) {
1694 int isl_basic_set_contains(struct isl_basic_set
*bset
, struct isl_vec
*vec
)
1696 return basic_map_contains((struct isl_basic_map
*)bset
, vec
);
1699 struct isl_basic_map
*isl_basic_map_intersect(
1700 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
1702 struct isl_vec
*sample
= NULL
;
1704 if (!bmap1
|| !bmap2
)
1707 isl_assert(bmap1
->ctx
, isl_dim_match(bmap1
->dim
, isl_dim_param
,
1708 bmap2
->dim
, isl_dim_param
), goto error
);
1709 if (isl_dim_total(bmap1
->dim
) ==
1710 isl_dim_size(bmap1
->dim
, isl_dim_param
) &&
1711 isl_dim_total(bmap2
->dim
) !=
1712 isl_dim_size(bmap2
->dim
, isl_dim_param
))
1713 return isl_basic_map_intersect(bmap2
, bmap1
);
1715 if (isl_dim_total(bmap2
->dim
) !=
1716 isl_dim_size(bmap2
->dim
, isl_dim_param
))
1717 isl_assert(bmap1
->ctx
,
1718 isl_dim_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
1720 if (bmap1
->sample
&&
1721 basic_map_contains(bmap1
, bmap1
->sample
) > 0 &&
1722 basic_map_contains(bmap2
, bmap1
->sample
) > 0)
1723 sample
= isl_vec_copy(bmap1
->sample
);
1724 else if (bmap2
->sample
&&
1725 basic_map_contains(bmap1
, bmap2
->sample
) > 0 &&
1726 basic_map_contains(bmap2
, bmap2
->sample
) > 0)
1727 sample
= isl_vec_copy(bmap2
->sample
);
1729 bmap1
= isl_basic_map_cow(bmap1
);
1730 bmap1
= isl_basic_map_extend_dim(bmap1
, isl_dim_copy(bmap1
->dim
),
1731 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
1734 bmap1
= add_constraints(bmap1
, bmap2
, 0, 0);
1737 isl_vec_free(bmap1
->sample
);
1738 bmap1
->sample
= sample
;
1741 bmap1
= isl_basic_map_simplify(bmap1
);
1742 return isl_basic_map_finalize(bmap1
);
1745 isl_vec_free(sample
);
1746 isl_basic_map_free(bmap1
);
1747 isl_basic_map_free(bmap2
);
1751 struct isl_basic_set
*isl_basic_set_intersect(
1752 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
1754 return (struct isl_basic_set
*)
1755 isl_basic_map_intersect(
1756 (struct isl_basic_map
*)bset1
,
1757 (struct isl_basic_map
*)bset2
);
1760 struct isl_map
*isl_map_intersect(struct isl_map
*map1
, struct isl_map
*map2
)
1763 struct isl_map
*result
;
1769 isl_assert(map1
->ctx
, isl_dim_match(map1
->dim
, isl_dim_param
,
1770 map2
->dim
, isl_dim_param
), goto error
);
1771 if (isl_dim_total(map1
->dim
) ==
1772 isl_dim_size(map1
->dim
, isl_dim_param
) &&
1773 isl_dim_total(map2
->dim
) != isl_dim_size(map2
->dim
, isl_dim_param
))
1774 return isl_map_intersect(map2
, map1
);
1776 if (isl_dim_total(map2
->dim
) != isl_dim_size(map2
->dim
, isl_dim_param
))
1777 isl_assert(map1
->ctx
,
1778 isl_dim_equal(map1
->dim
, map2
->dim
), goto error
);
1780 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
1781 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
1782 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
1784 result
= isl_map_alloc_dim(isl_dim_copy(map1
->dim
),
1785 map1
->n
* map2
->n
, flags
);
1788 for (i
= 0; i
< map1
->n
; ++i
)
1789 for (j
= 0; j
< map2
->n
; ++j
) {
1790 struct isl_basic_map
*part
;
1791 part
= isl_basic_map_intersect(
1792 isl_basic_map_copy(map1
->p
[i
]),
1793 isl_basic_map_copy(map2
->p
[j
]));
1794 if (isl_basic_map_is_empty(part
))
1795 isl_basic_map_free(part
);
1797 result
= isl_map_add(result
, part
);
1810 struct isl_set
*isl_set_intersect(struct isl_set
*set1
, struct isl_set
*set2
)
1812 return (struct isl_set
*)
1813 isl_map_intersect((struct isl_map
*)set1
,
1814 (struct isl_map
*)set2
);
1817 struct isl_basic_map
*isl_basic_map_reverse(struct isl_basic_map
*bmap
)
1819 struct isl_dim
*dim
;
1820 struct isl_basic_set
*bset
;
1825 bmap
= isl_basic_map_cow(bmap
);
1828 dim
= isl_dim_reverse(isl_dim_copy(bmap
->dim
));
1829 in
= isl_basic_map_n_in(bmap
);
1830 bset
= isl_basic_set_from_basic_map(bmap
);
1831 bset
= isl_basic_set_swap_vars(bset
, in
);
1832 return isl_basic_map_from_basic_set(bset
, dim
);
1835 /* Turn final n dimensions into existentially quantified variables.
1837 struct isl_basic_set
*isl_basic_set_project_out(
1838 struct isl_basic_set
*bset
, unsigned n
, unsigned flags
)
1848 isl_assert(bset
->ctx
, n
<= isl_basic_set_n_dim(bset
), goto error
);
1853 bset
= isl_basic_set_cow(bset
);
1855 row_size
= 1 + isl_dim_total(bset
->dim
) + bset
->extra
;
1856 old
= bset
->block2
.data
;
1857 bset
->block2
= isl_blk_extend(bset
->ctx
, bset
->block2
,
1858 (bset
->extra
+ n
) * (1 + row_size
));
1859 if (!bset
->block2
.data
)
1861 new_div
= isl_alloc_array(ctx
, isl_int
*, bset
->extra
+ n
);
1864 for (i
= 0; i
< n
; ++i
) {
1865 new_div
[i
] = bset
->block2
.data
+
1866 (bset
->extra
+ i
) * (1 + row_size
);
1867 isl_seq_clr(new_div
[i
], 1 + row_size
);
1869 for (i
= 0; i
< bset
->extra
; ++i
)
1870 new_div
[n
+ i
] = bset
->block2
.data
+ (bset
->div
[i
] - old
);
1872 bset
->div
= new_div
;
1875 bset
->dim
= isl_dim_drop_outputs(bset
->dim
,
1876 isl_basic_set_n_dim(bset
) - n
, n
);
1879 bset
= isl_basic_set_simplify(bset
);
1880 bset
= isl_basic_set_drop_redundant_divs(bset
);
1881 return isl_basic_set_finalize(bset
);
1883 isl_basic_set_free(bset
);
1887 static struct isl_basic_map
*add_divs(struct isl_basic_map
*bmap
, unsigned n
)
1891 for (i
= 0; i
< n
; ++i
) {
1892 j
= isl_basic_map_alloc_div(bmap
);
1895 isl_seq_clr(bmap
->div
[j
], 1+1+isl_basic_map_total_dim(bmap
));
1899 isl_basic_map_free(bmap
);
1903 struct isl_basic_map
*isl_basic_map_apply_range(
1904 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
1906 struct isl_dim
*dim_result
= NULL
;
1907 struct isl_basic_map
*bmap
;
1908 unsigned n_in
, n_out
, n
, nparam
, total
, pos
;
1909 struct isl_dim_map
*dim_map1
, *dim_map2
;
1911 if (!bmap1
|| !bmap2
)
1914 dim_result
= isl_dim_join(isl_dim_copy(bmap1
->dim
),
1915 isl_dim_copy(bmap2
->dim
));
1917 n_in
= isl_basic_map_n_in(bmap1
);
1918 n_out
= isl_basic_map_n_out(bmap2
);
1919 n
= isl_basic_map_n_out(bmap1
);
1920 nparam
= isl_basic_map_n_param(bmap1
);
1922 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ n
;
1923 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
1924 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
1925 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
1926 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
1927 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
1928 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_in
);
1929 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_out
);
1930 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
1931 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
1932 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
1934 bmap
= isl_basic_map_alloc_dim(dim_result
,
1935 bmap1
->n_div
+ bmap2
->n_div
+ n
,
1936 bmap1
->n_eq
+ bmap2
->n_eq
,
1937 bmap1
->n_ineq
+ bmap2
->n_ineq
);
1938 bmap
= add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
1939 bmap
= add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
1940 bmap
= add_divs(bmap
, n
);
1941 bmap
= isl_basic_map_simplify(bmap
);
1942 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
1943 return isl_basic_map_finalize(bmap
);
1945 isl_basic_map_free(bmap1
);
1946 isl_basic_map_free(bmap2
);
1950 struct isl_basic_set
*isl_basic_set_apply(
1951 struct isl_basic_set
*bset
, struct isl_basic_map
*bmap
)
1956 isl_assert(bset
->ctx
, isl_basic_map_compatible_domain(bmap
, bset
),
1959 return (struct isl_basic_set
*)
1960 isl_basic_map_apply_range((struct isl_basic_map
*)bset
, bmap
);
1962 isl_basic_set_free(bset
);
1963 isl_basic_map_free(bmap
);
1967 struct isl_basic_map
*isl_basic_map_apply_domain(
1968 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
1970 if (!bmap1
|| !bmap2
)
1973 isl_assert(bmap1
->ctx
,
1974 isl_basic_map_n_in(bmap1
) == isl_basic_map_n_in(bmap2
), goto error
);
1975 isl_assert(bmap1
->ctx
,
1976 isl_basic_map_n_param(bmap1
) == isl_basic_map_n_param(bmap2
),
1979 bmap1
= isl_basic_map_reverse(bmap1
);
1980 bmap1
= isl_basic_map_apply_range(bmap1
, bmap2
);
1981 return isl_basic_map_reverse(bmap1
);
1983 isl_basic_map_free(bmap1
);
1984 isl_basic_map_free(bmap2
);
1988 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
1989 * A \cap B -> f(A) + f(B)
1991 struct isl_basic_map
*isl_basic_map_sum(
1992 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
1994 unsigned n_in
, n_out
, nparam
, total
, pos
;
1995 struct isl_basic_map
*bmap
= NULL
;
1996 struct isl_dim_map
*dim_map1
, *dim_map2
;
1999 if (!bmap1
|| !bmap2
)
2002 isl_assert(bmap1
->ctx
, isl_dim_equal(bmap1
->dim
, bmap2
->dim
),
2005 nparam
= isl_basic_map_n_param(bmap1
);
2006 n_in
= isl_basic_map_n_in(bmap1
);
2007 n_out
= isl_basic_map_n_out(bmap1
);
2009 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
;
2010 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
2011 dim_map2
= isl_dim_map_alloc(bmap2
->ctx
, total
);
2012 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
2013 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
);
2014 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
2015 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
2016 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_in
+ n_out
);
2017 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
2018 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
2019 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_out
);
2021 bmap
= isl_basic_map_alloc_dim(isl_dim_copy(bmap1
->dim
),
2022 bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
,
2023 bmap1
->n_eq
+ bmap2
->n_eq
+ n_out
,
2024 bmap1
->n_ineq
+ bmap2
->n_ineq
);
2025 for (i
= 0; i
< n_out
; ++i
) {
2026 int j
= isl_basic_map_alloc_equality(bmap
);
2029 isl_seq_clr(bmap
->eq
[j
], 1+total
);
2030 isl_int_set_si(bmap
->eq
[j
][1+nparam
+n_in
+i
], -1);
2031 isl_int_set_si(bmap
->eq
[j
][1+pos
+i
], 1);
2032 isl_int_set_si(bmap
->eq
[j
][1+pos
-n_out
+i
], 1);
2034 bmap
= add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
2035 bmap
= add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
2036 bmap
= add_divs(bmap
, 2 * n_out
);
2038 bmap
= isl_basic_map_simplify(bmap
);
2039 return isl_basic_map_finalize(bmap
);
2041 isl_basic_map_free(bmap
);
2042 isl_basic_map_free(bmap1
);
2043 isl_basic_map_free(bmap2
);
2047 /* Given two maps A -> f(A) and B -> g(B), construct a map
2048 * A \cap B -> f(A) + f(B)
2050 struct isl_map
*isl_map_sum(struct isl_map
*map1
, struct isl_map
*map2
)
2052 struct isl_map
*result
;
2058 isl_assert(map1
->ctx
, isl_dim_equal(map1
->dim
, map2
->dim
), goto error
);
2060 result
= isl_map_alloc_dim(isl_dim_copy(map1
->dim
),
2061 map1
->n
* map2
->n
, 0);
2064 for (i
= 0; i
< map1
->n
; ++i
)
2065 for (j
= 0; j
< map2
->n
; ++j
) {
2066 struct isl_basic_map
*part
;
2067 part
= isl_basic_map_sum(
2068 isl_basic_map_copy(map1
->p
[i
]),
2069 isl_basic_map_copy(map2
->p
[j
]));
2070 if (isl_basic_map_is_empty(part
))
2071 isl_basic_map_free(part
);
2073 result
= isl_map_add(result
, part
);
2086 /* Given a basic map A -> f(A), construct A -> -f(A).
2088 struct isl_basic_map
*isl_basic_map_neg(struct isl_basic_map
*bmap
)
2093 bmap
= isl_basic_map_cow(bmap
);
2097 n
= isl_basic_map_dim(bmap
, isl_dim_out
);
2098 off
= isl_basic_map_offset(bmap
, isl_dim_out
);
2099 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2100 for (j
= 0; j
< n
; ++j
)
2101 isl_int_neg(bmap
->eq
[i
][off
+j
], bmap
->eq
[i
][off
+j
]);
2102 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2103 for (j
= 0; j
< n
; ++j
)
2104 isl_int_neg(bmap
->ineq
[i
][off
+j
], bmap
->ineq
[i
][off
+j
]);
2105 for (i
= 0; i
< bmap
->n_div
; ++i
)
2106 for (j
= 0; j
< n
; ++j
)
2107 isl_int_neg(bmap
->div
[i
][1+off
+j
], bmap
->div
[i
][1+off
+j
]);
2108 return isl_basic_map_finalize(bmap
);
2111 /* Given a map A -> f(A), construct A -> -f(A).
2113 struct isl_map
*isl_map_neg(struct isl_map
*map
)
2117 map
= isl_map_cow(map
);
2121 for (i
= 0; i
< map
->n
; ++i
) {
2122 map
->p
[i
] = isl_basic_map_neg(map
->p
[i
]);
2133 /* Given a basic map A -> f(A) and an integer d, construct a basic map
2134 * A -> floor(f(A)/d).
2136 struct isl_basic_map
*isl_basic_map_floordiv(struct isl_basic_map
*bmap
,
2139 unsigned n_in
, n_out
, nparam
, total
, pos
;
2140 struct isl_basic_map
*result
= NULL
;
2141 struct isl_dim_map
*dim_map
;
2147 nparam
= isl_basic_map_n_param(bmap
);
2148 n_in
= isl_basic_map_n_in(bmap
);
2149 n_out
= isl_basic_map_n_out(bmap
);
2151 total
= nparam
+ n_in
+ n_out
+ bmap
->n_div
+ n_out
;
2152 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
2153 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_param
, pos
= 0);
2154 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_in
, pos
+= nparam
);
2155 isl_dim_map_div(dim_map
, bmap
, pos
+= n_in
+ n_out
);
2156 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_out
, pos
+= bmap
->n_div
);
2158 result
= isl_basic_map_alloc_dim(isl_dim_copy(bmap
->dim
),
2159 bmap
->n_div
+ n_out
,
2160 bmap
->n_eq
, bmap
->n_ineq
+ 2 * n_out
);
2161 result
= add_constraints_dim_map(result
, bmap
, dim_map
);
2162 result
= add_divs(result
, n_out
);
2163 for (i
= 0; i
< n_out
; ++i
) {
2165 j
= isl_basic_map_alloc_inequality(result
);
2168 isl_seq_clr(result
->ineq
[j
], 1+total
);
2169 isl_int_neg(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
2170 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], 1);
2171 j
= isl_basic_map_alloc_inequality(result
);
2174 isl_seq_clr(result
->ineq
[j
], 1+total
);
2175 isl_int_set(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
2176 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], -1);
2177 isl_int_sub_ui(result
->ineq
[j
][0], d
, 1);
2180 result
= isl_basic_map_simplify(result
);
2181 return isl_basic_map_finalize(result
);
2183 isl_basic_map_free(result
);
2187 /* Given a map A -> f(A) and an integer d, construct a map
2188 * A -> floor(f(A)/d).
2190 struct isl_map
*isl_map_floordiv(struct isl_map
*map
, isl_int d
)
2194 map
= isl_map_cow(map
);
2198 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
2199 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
2200 for (i
= 0; i
< map
->n
; ++i
) {
2201 map
->p
[i
] = isl_basic_map_floordiv(map
->p
[i
], d
);
2212 static struct isl_basic_map
*var_equal(struct isl_basic_map
*bmap
, unsigned pos
)
2218 i
= isl_basic_map_alloc_equality(bmap
);
2221 nparam
= isl_basic_map_n_param(bmap
);
2222 n_in
= isl_basic_map_n_in(bmap
);
2223 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
2224 isl_int_set_si(bmap
->eq
[i
][1+nparam
+pos
], -1);
2225 isl_int_set_si(bmap
->eq
[i
][1+nparam
+n_in
+pos
], 1);
2226 return isl_basic_map_finalize(bmap
);
2228 isl_basic_map_free(bmap
);
2232 static struct isl_basic_map
*var_more(struct isl_basic_map
*bmap
, unsigned pos
)
2238 i
= isl_basic_map_alloc_inequality(bmap
);
2241 nparam
= isl_basic_map_n_param(bmap
);
2242 n_in
= isl_basic_map_n_in(bmap
);
2243 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
2244 isl_int_set_si(bmap
->ineq
[i
][0], -1);
2245 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
2246 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
2247 return isl_basic_map_finalize(bmap
);
2249 isl_basic_map_free(bmap
);
2253 static struct isl_basic_map
*var_less(struct isl_basic_map
*bmap
, unsigned pos
)
2259 i
= isl_basic_map_alloc_inequality(bmap
);
2262 nparam
= isl_basic_map_n_param(bmap
);
2263 n_in
= isl_basic_map_n_in(bmap
);
2264 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
2265 isl_int_set_si(bmap
->ineq
[i
][0], -1);
2266 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
2267 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
2268 return isl_basic_map_finalize(bmap
);
2270 isl_basic_map_free(bmap
);
2274 struct isl_basic_map
*isl_basic_map_equal(struct isl_dim
*dim
, unsigned n_equal
)
2277 struct isl_basic_map
*bmap
;
2278 bmap
= isl_basic_map_alloc_dim(dim
, 0, n_equal
, 0);
2281 for (i
= 0; i
< n_equal
&& bmap
; ++i
)
2282 bmap
= var_equal(bmap
, i
);
2283 return isl_basic_map_finalize(bmap
);
2286 struct isl_basic_map
*isl_basic_map_less_at(struct isl_dim
*dim
, unsigned pos
)
2289 struct isl_basic_map
*bmap
;
2290 bmap
= isl_basic_map_alloc_dim(dim
, 0, pos
, 1);
2293 for (i
= 0; i
< pos
&& bmap
; ++i
)
2294 bmap
= var_equal(bmap
, i
);
2296 bmap
= var_less(bmap
, pos
);
2297 return isl_basic_map_finalize(bmap
);
2300 struct isl_basic_map
*isl_basic_map_more_at(struct isl_dim
*dim
, unsigned pos
)
2303 struct isl_basic_map
*bmap
;
2304 bmap
= isl_basic_map_alloc_dim(dim
, 0, pos
, 1);
2307 for (i
= 0; i
< pos
&& bmap
; ++i
)
2308 bmap
= var_equal(bmap
, i
);
2310 bmap
= var_more(bmap
, pos
);
2311 return isl_basic_map_finalize(bmap
);
2314 struct isl_basic_map
*isl_basic_map_from_basic_set(
2315 struct isl_basic_set
*bset
, struct isl_dim
*dim
)
2317 struct isl_basic_map
*bmap
;
2319 bset
= isl_basic_set_cow(bset
);
2323 isl_assert(bset
->ctx
, isl_dim_compatible(bset
->dim
, dim
), goto error
);
2324 isl_dim_free(bset
->dim
);
2325 bmap
= (struct isl_basic_map
*) bset
;
2327 return isl_basic_map_finalize(bmap
);
2329 isl_basic_set_free(bset
);
2334 struct isl_basic_set
*isl_basic_set_from_basic_map(struct isl_basic_map
*bmap
)
2338 if (bmap
->dim
->n_in
== 0)
2339 return (struct isl_basic_set
*)bmap
;
2340 bmap
= isl_basic_map_cow(bmap
);
2343 bmap
->dim
= isl_dim_cow(bmap
->dim
);
2346 bmap
->dim
->n_out
+= bmap
->dim
->n_in
;
2347 bmap
->dim
->n_in
= 0;
2348 bmap
= isl_basic_map_finalize(bmap
);
2349 return (struct isl_basic_set
*)bmap
;
2351 isl_basic_map_free(bmap
);
2355 /* For a div d = floor(f/m), add the constraints
2358 * -(f-(n-1)) + m d >= 0
2360 * Note that the second constraint is the negation of
2364 static int add_div_constraints(struct isl_basic_map
*bmap
, unsigned div
)
2367 unsigned total
= isl_basic_map_total_dim(bmap
);
2368 unsigned div_pos
= 1 + total
- bmap
->n_div
+ div
;
2370 i
= isl_basic_map_alloc_inequality(bmap
);
2373 isl_seq_cpy(bmap
->ineq
[i
], bmap
->div
[div
]+1, 1+total
);
2374 isl_int_neg(bmap
->ineq
[i
][div_pos
], bmap
->div
[div
][0]);
2376 j
= isl_basic_map_alloc_inequality(bmap
);
2379 isl_seq_neg(bmap
->ineq
[j
], bmap
->ineq
[i
], 1 + total
);
2380 isl_int_add(bmap
->ineq
[j
][0], bmap
->ineq
[j
][0], bmap
->ineq
[j
][div_pos
]);
2381 isl_int_sub_ui(bmap
->ineq
[j
][0], bmap
->ineq
[j
][0], 1);
2385 struct isl_basic_set
*isl_basic_map_underlying_set(
2386 struct isl_basic_map
*bmap
)
2390 if (bmap
->dim
->nparam
== 0 && bmap
->dim
->n_in
== 0 && bmap
->n_div
== 0)
2391 return (struct isl_basic_set
*)bmap
;
2392 bmap
= isl_basic_map_cow(bmap
);
2395 bmap
->dim
= isl_dim_underlying(bmap
->dim
, bmap
->n_div
);
2398 bmap
->extra
-= bmap
->n_div
;
2400 bmap
= isl_basic_map_finalize(bmap
);
2401 return (struct isl_basic_set
*)bmap
;
2406 struct isl_basic_map
*isl_basic_map_overlying_set(
2407 struct isl_basic_set
*bset
, struct isl_basic_map
*like
)
2409 struct isl_basic_map
*bmap
;
2410 struct isl_ctx
*ctx
;
2417 isl_assert(ctx
, bset
->n_div
== 0, goto error
);
2418 isl_assert(ctx
, isl_basic_set_n_param(bset
) == 0, goto error
);
2419 isl_assert(ctx
, bset
->dim
->n_out
== isl_basic_map_total_dim(like
),
2421 if (isl_dim_equal(bset
->dim
, like
->dim
) && like
->n_div
== 0) {
2422 isl_basic_map_free(like
);
2423 return (struct isl_basic_map
*)bset
;
2425 bset
= isl_basic_set_cow(bset
);
2428 total
= bset
->dim
->n_out
+ bset
->extra
;
2429 bmap
= (struct isl_basic_map
*)bset
;
2430 isl_dim_free(bmap
->dim
);
2431 bmap
->dim
= isl_dim_copy(like
->dim
);
2434 bmap
->n_div
= like
->n_div
;
2435 bmap
->extra
+= like
->n_div
;
2438 ltotal
= total
- bmap
->extra
+ like
->extra
;
2441 bmap
->block2
= isl_blk_extend(ctx
, bmap
->block2
,
2442 bmap
->extra
* (1 + 1 + total
));
2443 if (isl_blk_is_error(bmap
->block2
))
2445 bmap
->div
= isl_realloc_array(ctx
, bmap
->div
, isl_int
*,
2449 for (i
= 0; i
< bmap
->extra
; ++i
)
2450 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + 1 + total
);
2451 for (i
= 0; i
< like
->n_div
; ++i
) {
2452 isl_seq_cpy(bmap
->div
[i
], like
->div
[i
], 1 + 1 + ltotal
);
2453 isl_seq_clr(bmap
->div
[i
]+1+1+ltotal
, total
- ltotal
);
2455 bmap
= isl_basic_map_extend_constraints(bmap
,
2456 0, 2 * like
->n_div
);
2457 for (i
= 0; i
< like
->n_div
; ++i
) {
2458 if (isl_int_is_zero(bmap
->div
[i
][0]))
2460 if (add_div_constraints(bmap
, i
) < 0)
2464 isl_basic_map_free(like
);
2465 bmap
= isl_basic_map_simplify(bmap
);
2466 bmap
= isl_basic_map_finalize(bmap
);
2469 isl_basic_map_free(like
);
2470 isl_basic_set_free(bset
);
2474 struct isl_basic_set
*isl_basic_set_from_underlying_set(
2475 struct isl_basic_set
*bset
, struct isl_basic_set
*like
)
2477 return (struct isl_basic_set
*)
2478 isl_basic_map_overlying_set(bset
, (struct isl_basic_map
*)like
);
2481 struct isl_set
*isl_set_from_underlying_set(
2482 struct isl_set
*set
, struct isl_basic_set
*like
)
2488 isl_assert(set
->ctx
, set
->dim
->n_out
== isl_basic_set_total_dim(like
),
2490 if (isl_dim_equal(set
->dim
, like
->dim
) && like
->n_div
== 0) {
2491 isl_basic_set_free(like
);
2494 set
= isl_set_cow(set
);
2497 for (i
= 0; i
< set
->n
; ++i
) {
2498 set
->p
[i
] = isl_basic_set_from_underlying_set(set
->p
[i
],
2499 isl_basic_set_copy(like
));
2503 isl_dim_free(set
->dim
);
2504 set
->dim
= isl_dim_copy(like
->dim
);
2507 isl_basic_set_free(like
);
2510 isl_basic_set_free(like
);
2515 struct isl_set
*isl_map_underlying_set(struct isl_map
*map
)
2519 map
= isl_map_cow(map
);
2522 map
->dim
= isl_dim_cow(map
->dim
);
2526 for (i
= 1; i
< map
->n
; ++i
)
2527 isl_assert(map
->ctx
, map
->p
[0]->n_div
== map
->p
[i
]->n_div
,
2529 for (i
= 0; i
< map
->n
; ++i
) {
2530 map
->p
[i
] = (struct isl_basic_map
*)
2531 isl_basic_map_underlying_set(map
->p
[i
]);
2536 map
->dim
= isl_dim_underlying(map
->dim
, 0);
2538 isl_dim_free(map
->dim
);
2539 map
->dim
= isl_dim_copy(map
->p
[0]->dim
);
2543 return (struct isl_set
*)map
;
2549 struct isl_set
*isl_set_to_underlying_set(struct isl_set
*set
)
2551 return (struct isl_set
*)isl_map_underlying_set((struct isl_map
*)set
);
2554 struct isl_basic_set
*isl_basic_map_domain(struct isl_basic_map
*bmap
)
2556 struct isl_basic_set
*domain
;
2560 n_out
= isl_basic_map_n_out(bmap
);
2561 domain
= isl_basic_set_from_basic_map(bmap
);
2562 return isl_basic_set_project_out(domain
, n_out
, 0);
2565 struct isl_basic_set
*isl_basic_map_range(struct isl_basic_map
*bmap
)
2567 return isl_basic_map_domain(isl_basic_map_reverse(bmap
));
2570 struct isl_set
*isl_map_range(struct isl_map
*map
)
2573 struct isl_set
*set
;
2577 map
= isl_map_cow(map
);
2581 set
= (struct isl_set
*) map
;
2582 if (set
->dim
->n_in
!= 0) {
2583 set
->dim
= isl_dim_drop_inputs(set
->dim
, 0, set
->dim
->n_in
);
2587 for (i
= 0; i
< map
->n
; ++i
) {
2588 set
->p
[i
] = isl_basic_map_range(map
->p
[i
]);
2592 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
2593 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
2600 struct isl_map
*isl_map_from_set(struct isl_set
*set
, struct isl_dim
*dim
)
2603 struct isl_map
*map
= NULL
;
2605 set
= isl_set_cow(set
);
2608 isl_assert(set
->ctx
, isl_dim_compatible(set
->dim
, dim
), goto error
);
2609 map
= (struct isl_map
*)set
;
2610 for (i
= 0; i
< set
->n
; ++i
) {
2611 map
->p
[i
] = isl_basic_map_from_basic_set(
2612 set
->p
[i
], isl_dim_copy(dim
));
2616 isl_dim_free(map
->dim
);
2625 struct isl_map
*isl_map_from_range(struct isl_set
*set
)
2627 return (struct isl_map
*)set
;
2630 struct isl_set
*isl_set_from_map(struct isl_map
*map
)
2633 struct isl_set
*set
= NULL
;
2637 map
= isl_map_cow(map
);
2640 map
->dim
= isl_dim_cow(map
->dim
);
2643 map
->dim
->n_out
+= map
->dim
->n_in
;
2645 set
= (struct isl_set
*)map
;
2646 for (i
= 0; i
< map
->n
; ++i
) {
2647 set
->p
[i
] = isl_basic_set_from_basic_map(map
->p
[i
]);
2657 struct isl_map
*isl_map_alloc_dim(struct isl_dim
*dim
, int n
, unsigned flags
)
2659 struct isl_map
*map
;
2663 isl_assert(dim
->ctx
, n
>= 0, return NULL
);
2664 map
= isl_alloc(dim
->ctx
, struct isl_map
,
2665 sizeof(struct isl_map
) +
2666 (n
- 1) * sizeof(struct isl_basic_map
*));
2670 map
->ctx
= dim
->ctx
;
2671 isl_ctx_ref(map
->ctx
);
2683 struct isl_map
*isl_map_alloc(struct isl_ctx
*ctx
,
2684 unsigned nparam
, unsigned in
, unsigned out
, int n
,
2687 struct isl_map
*map
;
2688 struct isl_dim
*dims
;
2690 dims
= isl_dim_alloc(ctx
, nparam
, in
, out
);
2694 map
= isl_map_alloc_dim(dims
, n
, flags
);
2698 struct isl_basic_map
*isl_basic_map_empty(struct isl_dim
*dim
)
2700 struct isl_basic_map
*bmap
;
2701 bmap
= isl_basic_map_alloc_dim(dim
, 0, 1, 0);
2702 bmap
= isl_basic_map_set_to_empty(bmap
);
2706 struct isl_basic_set
*isl_basic_set_empty(struct isl_dim
*dim
)
2708 struct isl_basic_set
*bset
;
2709 bset
= isl_basic_set_alloc_dim(dim
, 0, 1, 0);
2710 bset
= isl_basic_set_set_to_empty(bset
);
2714 struct isl_basic_map
*isl_basic_map_empty_like(struct isl_basic_map
*model
)
2716 struct isl_basic_map
*bmap
;
2719 bmap
= isl_basic_map_alloc_dim(isl_dim_copy(model
->dim
), 0, 1, 0);
2720 bmap
= isl_basic_map_set_to_empty(bmap
);
2724 struct isl_basic_map
*isl_basic_map_empty_like_map(struct isl_map
*model
)
2726 struct isl_basic_map
*bmap
;
2729 bmap
= isl_basic_map_alloc_dim(isl_dim_copy(model
->dim
), 0, 1, 0);
2730 bmap
= isl_basic_map_set_to_empty(bmap
);
2734 struct isl_basic_set
*isl_basic_set_empty_like(struct isl_basic_set
*model
)
2736 struct isl_basic_set
*bset
;
2739 bset
= isl_basic_set_alloc_dim(isl_dim_copy(model
->dim
), 0, 1, 0);
2740 bset
= isl_basic_set_set_to_empty(bset
);
2744 struct isl_basic_map
*isl_basic_map_universe(struct isl_dim
*dim
)
2746 struct isl_basic_map
*bmap
;
2747 bmap
= isl_basic_map_alloc_dim(dim
, 0, 0, 0);
2751 struct isl_basic_set
*isl_basic_set_universe(struct isl_dim
*dim
)
2753 struct isl_basic_set
*bset
;
2754 bset
= isl_basic_set_alloc_dim(dim
, 0, 0, 0);
2758 struct isl_basic_set
*isl_basic_set_universe_like(struct isl_basic_set
*model
)
2762 return isl_basic_set_alloc_dim(isl_dim_copy(model
->dim
), 0, 0, 0);
2765 struct isl_map
*isl_map_empty(struct isl_dim
*dim
)
2767 return isl_map_alloc_dim(dim
, 0, ISL_MAP_DISJOINT
);
2770 struct isl_map
*isl_map_empty_like(struct isl_map
*model
)
2774 return isl_map_alloc_dim(isl_dim_copy(model
->dim
), 0, ISL_MAP_DISJOINT
);
2777 struct isl_map
*isl_map_empty_like_basic_map(struct isl_basic_map
*model
)
2781 return isl_map_alloc_dim(isl_dim_copy(model
->dim
), 0, ISL_MAP_DISJOINT
);
2784 struct isl_set
*isl_set_empty(struct isl_dim
*dim
)
2786 return isl_set_alloc_dim(dim
, 0, ISL_MAP_DISJOINT
);
2789 struct isl_set
*isl_set_empty_like(struct isl_set
*model
)
2793 return isl_set_empty(isl_dim_copy(model
->dim
));
2796 struct isl_map
*isl_map_universe(struct isl_dim
*dim
)
2798 struct isl_map
*map
;
2801 map
= isl_map_alloc_dim(isl_dim_copy(dim
), 1, ISL_MAP_DISJOINT
);
2802 map
= isl_map_add(map
, isl_basic_map_universe(dim
));
2806 struct isl_set
*isl_set_universe(struct isl_dim
*dim
)
2808 struct isl_set
*set
;
2811 set
= isl_set_alloc_dim(isl_dim_copy(dim
), 1, ISL_MAP_DISJOINT
);
2812 set
= isl_set_add(set
, isl_basic_set_universe(dim
));
2816 struct isl_map
*isl_map_dup(struct isl_map
*map
)
2819 struct isl_map
*dup
;
2823 dup
= isl_map_alloc_dim(isl_dim_copy(map
->dim
), map
->n
, map
->flags
);
2824 for (i
= 0; i
< map
->n
; ++i
)
2825 dup
= isl_map_add(dup
, isl_basic_map_copy(map
->p
[i
]));
2829 struct isl_map
*isl_map_add(struct isl_map
*map
, struct isl_basic_map
*bmap
)
2833 if (isl_basic_map_fast_is_empty(bmap
)) {
2834 isl_basic_map_free(bmap
);
2837 isl_assert(map
->ctx
, isl_dim_equal(map
->dim
, bmap
->dim
), goto error
);
2838 isl_assert(map
->ctx
, map
->n
< map
->size
, goto error
);
2839 map
->p
[map
->n
] = bmap
;
2841 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
2847 isl_basic_map_free(bmap
);
2851 void isl_map_free(struct isl_map
*map
)
2861 isl_ctx_deref(map
->ctx
);
2862 for (i
= 0; i
< map
->n
; ++i
)
2863 isl_basic_map_free(map
->p
[i
]);
2864 isl_dim_free(map
->dim
);
2868 struct isl_map
*isl_map_extend(struct isl_map
*base
,
2869 unsigned nparam
, unsigned n_in
, unsigned n_out
)
2873 base
= isl_map_cow(base
);
2877 base
->dim
= isl_dim_extend(base
->dim
, nparam
, n_in
, n_out
);
2880 for (i
= 0; i
< base
->n
; ++i
) {
2881 base
->p
[i
] = isl_basic_map_extend_dim(base
->p
[i
],
2882 isl_dim_copy(base
->dim
), 0, 0, 0);
2892 struct isl_set
*isl_set_extend(struct isl_set
*base
,
2893 unsigned nparam
, unsigned dim
)
2895 return (struct isl_set
*)isl_map_extend((struct isl_map
*)base
,
2899 static struct isl_basic_map
*isl_basic_map_fix_pos(struct isl_basic_map
*bmap
,
2900 unsigned pos
, int value
)
2904 bmap
= isl_basic_map_cow(bmap
);
2905 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
2906 j
= isl_basic_map_alloc_equality(bmap
);
2909 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
2910 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
2911 isl_int_set_si(bmap
->eq
[j
][0], value
);
2912 bmap
= isl_basic_map_simplify(bmap
);
2913 return isl_basic_map_finalize(bmap
);
2915 isl_basic_map_free(bmap
);
2919 struct isl_basic_map
*isl_basic_map_fix_si(struct isl_basic_map
*bmap
,
2920 enum isl_dim_type type
, unsigned pos
, int value
)
2924 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
2925 return isl_basic_map_fix_pos(bmap
, isl_basic_map_offset(bmap
, type
) + pos
,
2928 isl_basic_map_free(bmap
);
2932 struct isl_basic_set
*isl_basic_set_fix_si(struct isl_basic_set
*bset
,
2933 enum isl_dim_type type
, unsigned pos
, int value
)
2935 return (struct isl_basic_set
*)
2936 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
2940 struct isl_basic_map
*isl_basic_map_fix_input_si(struct isl_basic_map
*bmap
,
2941 unsigned input
, int value
)
2943 return isl_basic_map_fix_si(bmap
, isl_dim_in
, input
, value
);
2946 struct isl_basic_set
*isl_basic_set_fix_dim_si(struct isl_basic_set
*bset
,
2947 unsigned dim
, int value
)
2949 return (struct isl_basic_set
*)
2950 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
2951 isl_dim_set
, dim
, value
);
2954 struct isl_map
*isl_map_fix_si(struct isl_map
*map
,
2955 enum isl_dim_type type
, unsigned pos
, int value
)
2959 map
= isl_map_cow(map
);
2963 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
2964 for (i
= 0; i
< map
->n
; ++i
) {
2965 map
->p
[i
] = isl_basic_map_fix_si(map
->p
[i
], type
, pos
, value
);
2969 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
2976 struct isl_map
*isl_map_fix_input_si(struct isl_map
*map
,
2977 unsigned input
, int value
)
2979 return isl_map_fix_si(map
, isl_dim_in
, input
, value
);
2982 struct isl_set
*isl_set_fix_dim_si(struct isl_set
*set
, unsigned dim
, int value
)
2984 return (struct isl_set
*)
2985 isl_map_fix_si((struct isl_map
*)set
, isl_dim_set
, dim
, value
);
2988 struct isl_basic_set
*isl_basic_set_lower_bound_dim(struct isl_basic_set
*bset
,
2989 unsigned dim
, isl_int value
)
2994 bset
= isl_basic_set_cow(bset
);
2995 bset
= isl_basic_set_extend_constraints(bset
, 0, 1);
2996 j
= isl_basic_set_alloc_inequality(bset
);
2999 isl_seq_clr(bset
->ineq
[j
], 1 + isl_basic_set_total_dim(bset
));
3000 isl_int_set_si(bset
->ineq
[j
][1 + isl_basic_set_n_param(bset
) + dim
], 1);
3001 isl_int_neg(bset
->ineq
[j
][0], value
);
3002 bset
= isl_basic_set_simplify(bset
);
3003 return isl_basic_set_finalize(bset
);
3005 isl_basic_set_free(bset
);
3009 struct isl_set
*isl_set_lower_bound_dim(struct isl_set
*set
, unsigned dim
,
3014 set
= isl_set_cow(set
);
3018 isl_assert(set
->ctx
, dim
< isl_set_n_dim(set
), goto error
);
3019 for (i
= 0; i
< set
->n
; ++i
) {
3020 set
->p
[i
] = isl_basic_set_lower_bound_dim(set
->p
[i
], dim
, value
);
3030 struct isl_map
*isl_map_reverse(struct isl_map
*map
)
3035 map
= isl_map_cow(map
);
3039 map
->dim
= isl_dim_reverse(map
->dim
);
3042 for (i
= 0; i
< map
->n
; ++i
) {
3043 map
->p
[i
] = isl_basic_map_reverse(map
->p
[i
]);
3047 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3054 static struct isl_map
*isl_basic_map_partial_lexopt(
3055 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
3056 struct isl_set
**empty
, int max
)
3060 if (bmap
->ctx
->pip
== ISL_PIP_PIP
)
3061 return isl_pip_basic_map_lexopt(bmap
, dom
, empty
, max
);
3063 return isl_tab_basic_map_partial_lexopt(bmap
, dom
, empty
, max
);
3065 isl_basic_map_free(bmap
);
3066 isl_basic_set_free(dom
);
3072 struct isl_map
*isl_basic_map_partial_lexmax(
3073 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
3074 struct isl_set
**empty
)
3076 return isl_basic_map_partial_lexopt(bmap
, dom
, empty
, 1);
3079 struct isl_map
*isl_basic_map_partial_lexmin(
3080 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
3081 struct isl_set
**empty
)
3083 return isl_basic_map_partial_lexopt(bmap
, dom
, empty
, 0);
3086 struct isl_set
*isl_basic_set_partial_lexmin(
3087 struct isl_basic_set
*bset
, struct isl_basic_set
*dom
,
3088 struct isl_set
**empty
)
3090 return (struct isl_set
*)
3091 isl_basic_map_partial_lexmin((struct isl_basic_map
*)bset
,
3095 struct isl_set
*isl_basic_set_partial_lexmax(
3096 struct isl_basic_set
*bset
, struct isl_basic_set
*dom
,
3097 struct isl_set
**empty
)
3099 return (struct isl_set
*)
3100 isl_basic_map_partial_lexmax((struct isl_basic_map
*)bset
,
3104 struct isl_set
*isl_basic_set_lexmin(struct isl_basic_set
*bset
)
3106 struct isl_basic_map
*bmap
= NULL
;
3107 struct isl_basic_set
*dom
= NULL
;
3108 struct isl_map
*min
;
3109 struct isl_dim
*param_dim
;
3113 bmap
= isl_basic_map_from_basic_set(bset
, isl_dim_copy(bset
->dim
));
3116 param_dim
= isl_dim_domain(isl_dim_copy(bmap
->dim
));
3117 dom
= isl_basic_set_universe(param_dim
);
3120 min
= isl_basic_map_partial_lexmin(bmap
, dom
, NULL
);
3121 return isl_map_range(min
);
3123 isl_basic_map_free(bmap
);
3127 static struct isl_map
*isl_map_reset_dim(struct isl_map
*map
,
3128 struct isl_dim
*dim
)
3135 for (i
= 0; i
< map
->n
; ++i
) {
3136 isl_dim_free(map
->p
[i
]->dim
);
3137 map
->p
[i
]->dim
= isl_dim_copy(dim
);
3139 isl_dim_free(map
->dim
);
3149 static struct isl_set
*isl_set_reset_dim(struct isl_set
*set
,
3150 struct isl_dim
*dim
)
3152 return (struct isl_set
*) isl_map_reset_dim((struct isl_map
*)set
, dim
);
3155 /* Apply a preimage specified by "mat" on the parameters of "bset".
3156 * bset is assumed to have only parameters and divs.
3158 static struct isl_basic_set
*basic_set_parameter_preimage(
3159 struct isl_basic_set
*bset
, struct isl_mat
*mat
)
3166 bset
->dim
= isl_dim_cow(bset
->dim
);
3170 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
3172 isl_assert(bset
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
3174 bset
->dim
->nparam
= 0;
3175 bset
->dim
->n_out
= nparam
;
3176 bset
= isl_basic_set_preimage(bset
, mat
);
3178 bset
->dim
->nparam
= bset
->dim
->n_out
;
3179 bset
->dim
->n_out
= 0;
3184 isl_basic_set_free(bset
);
3188 /* Apply a preimage specified by "mat" on the parameters of "set".
3189 * set is assumed to have only parameters and divs.
3191 static struct isl_set
*set_parameter_preimage(
3192 struct isl_set
*set
, struct isl_mat
*mat
)
3194 struct isl_dim
*dim
= NULL
;
3200 dim
= isl_dim_copy(set
->dim
);
3201 dim
= isl_dim_cow(dim
);
3205 nparam
= isl_set_dim(set
, isl_dim_param
);
3207 isl_assert(set
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
3210 dim
->n_out
= nparam
;
3211 isl_set_reset_dim(set
, dim
);
3212 set
= isl_set_preimage(set
, mat
);
3215 dim
= isl_dim_copy(set
->dim
);
3216 dim
= isl_dim_cow(dim
);
3219 dim
->nparam
= dim
->n_out
;
3221 isl_set_reset_dim(set
, dim
);
3231 /* Intersect the basic set "bset" with the affine space specified by the
3232 * equalities in "eq".
3234 static struct isl_basic_set
*basic_set_append_equalities(
3235 struct isl_basic_set
*bset
, struct isl_mat
*eq
)
3243 bset
= isl_basic_set_extend_dim(bset
, isl_dim_copy(bset
->dim
), 0,
3248 len
= 1 + isl_dim_total(bset
->dim
) + bset
->extra
;
3249 for (i
= 0; i
< eq
->n_row
; ++i
) {
3250 k
= isl_basic_set_alloc_equality(bset
);
3253 isl_seq_cpy(bset
->eq
[k
], eq
->row
[i
], eq
->n_col
);
3254 isl_seq_clr(bset
->eq
[k
] + eq
->n_col
, len
- eq
->n_col
);
3261 isl_basic_set_free(bset
);
3265 /* Intersect the set "set" with the affine space specified by the
3266 * equalities in "eq".
3268 static struct isl_set
*set_append_equalities(struct isl_set
*set
,
3276 for (i
= 0; i
< set
->n
; ++i
) {
3277 set
->p
[i
] = basic_set_append_equalities(set
->p
[i
],
3290 /* Project the given basic set onto its parameter domain, possibly introducing
3291 * new, explicit, existential variables in the constraints.
3292 * The input has parameters and (possibly implicit) existential variables.
3293 * The output has the same parameters, but only
3294 * explicit existentially quantified variables.
3296 * The actual projection is performed by pip, but pip doesn't seem
3297 * to like equalities very much, so we first remove the equalities
3298 * among the parameters by performing a variable compression on
3299 * the parameters. Afterward, an inverse transformation is performed
3300 * and the equalities among the parameters are inserted back in.
3302 static struct isl_set
*parameter_compute_divs(struct isl_basic_set
*bset
)
3306 struct isl_mat
*T
, *T2
;
3307 struct isl_set
*set
;
3308 unsigned nparam
, n_div
;
3310 bset
= isl_basic_set_cow(bset
);
3314 if (bset
->n_eq
== 0)
3315 return isl_basic_set_lexmin(bset
);
3317 isl_basic_set_gauss(bset
, NULL
);
3319 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
3320 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
3322 for (i
= 0, j
= n_div
- 1; i
< bset
->n_eq
&& j
>= 0; --j
) {
3323 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ j
]))
3326 if (i
== bset
->n_eq
)
3327 return isl_basic_set_lexmin(bset
);
3329 eq
= isl_mat_sub_alloc(bset
->ctx
, bset
->eq
, i
, bset
->n_eq
- i
,
3331 eq
= isl_mat_cow(eq
);
3332 T
= isl_mat_variable_compression(isl_mat_copy(eq
), &T2
);
3333 bset
= basic_set_parameter_preimage(bset
, T
);
3335 set
= isl_basic_set_lexmin(bset
);
3336 set
= set_parameter_preimage(set
, T2
);
3337 set
= set_append_equalities(set
, eq
);
3341 /* Compute an explicit representation for all the existentially
3342 * quantified variables.
3343 * The input and output dimensions are first turned into parameters.
3344 * compute_divs then returns a map with the same parameters and
3345 * no input or output dimensions and the dimension specification
3346 * is reset to that of the input.
3348 static struct isl_map
*compute_divs(struct isl_basic_map
*bmap
)
3350 struct isl_basic_set
*bset
;
3351 struct isl_set
*set
;
3352 struct isl_map
*map
;
3353 struct isl_dim
*dim
, *orig_dim
= NULL
;
3358 bmap
= isl_basic_map_cow(bmap
);
3362 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
3363 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
3364 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
3365 dim
= isl_dim_set_alloc(bmap
->ctx
, nparam
+ n_in
+ n_out
, 0);
3369 orig_dim
= bmap
->dim
;
3371 bset
= (struct isl_basic_set
*)bmap
;
3373 set
= parameter_compute_divs(bset
);
3374 map
= (struct isl_map
*)set
;
3375 map
= isl_map_reset_dim(map
, orig_dim
);
3379 isl_basic_map_free(bmap
);
3383 /* If bmap contains any unknown divs, then compute explicit
3384 * expressions for them. However, this computation may be
3385 * quite expensive, so first try to remove divs that aren't
3388 struct isl_map
*isl_basic_map_compute_divs(struct isl_basic_map
*bmap
)
3395 off
= isl_dim_total(bmap
->dim
);
3396 for (i
= 0; i
< bmap
->n_div
; ++i
) {
3397 if (isl_int_is_zero(bmap
->div
[i
][0]))
3399 isl_assert(bmap
->ctx
, isl_int_is_zero(bmap
->div
[i
][1+1+off
+i
]),
3402 if (i
== bmap
->n_div
)
3403 return isl_map_from_basic_map(bmap
);
3404 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
3407 for (i
= 0; i
< bmap
->n_div
; ++i
)
3408 if (isl_int_is_zero(bmap
->div
[i
][0]))
3410 if (i
== bmap
->n_div
)
3411 return isl_map_from_basic_map(bmap
);
3412 struct isl_map
*map
= compute_divs(bmap
);
3415 isl_basic_map_free(bmap
);
3419 struct isl_map
*isl_map_compute_divs(struct isl_map
*map
)
3422 struct isl_map
*res
;
3428 res
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[0]));
3429 for (i
= 1 ; i
< map
->n
; ++i
) {
3431 r2
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[i
]));
3432 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
3433 res
= isl_map_union_disjoint(res
, r2
);
3435 res
= isl_map_union(res
, r2
);
3442 struct isl_set
*isl_basic_set_compute_divs(struct isl_basic_set
*bset
)
3444 return (struct isl_set
*)
3445 isl_basic_map_compute_divs((struct isl_basic_map
*)bset
);
3448 struct isl_set
*isl_set_compute_divs(struct isl_set
*set
)
3450 return (struct isl_set
*)
3451 isl_map_compute_divs((struct isl_map
*)set
);
3454 struct isl_set
*isl_map_domain(struct isl_map
*map
)
3457 struct isl_set
*set
;
3462 map
= isl_map_cow(map
);
3466 set
= (struct isl_set
*)map
;
3467 set
->dim
= isl_dim_domain(set
->dim
);
3470 for (i
= 0; i
< map
->n
; ++i
) {
3471 set
->p
[i
] = isl_basic_map_domain(map
->p
[i
]);
3475 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
3476 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
3483 struct isl_map
*isl_map_union_disjoint(
3484 struct isl_map
*map1
, struct isl_map
*map2
)
3488 struct isl_map
*map
= NULL
;
3502 isl_assert(map1
->ctx
, isl_dim_equal(map1
->dim
, map2
->dim
), goto error
);
3504 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
3505 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
3506 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
3508 map
= isl_map_alloc_dim(isl_dim_copy(map1
->dim
),
3509 map1
->n
+ map2
->n
, flags
);
3512 for (i
= 0; i
< map1
->n
; ++i
) {
3513 map
= isl_map_add(map
,
3514 isl_basic_map_copy(map1
->p
[i
]));
3518 for (i
= 0; i
< map2
->n
; ++i
) {
3519 map
= isl_map_add(map
,
3520 isl_basic_map_copy(map2
->p
[i
]));
3534 struct isl_map
*isl_map_union(struct isl_map
*map1
, struct isl_map
*map2
)
3536 map1
= isl_map_union_disjoint(map1
, map2
);
3540 ISL_F_CLR(map1
, ISL_MAP_DISJOINT
);
3544 struct isl_set
*isl_set_union_disjoint(
3545 struct isl_set
*set1
, struct isl_set
*set2
)
3547 return (struct isl_set
*)
3548 isl_map_union_disjoint(
3549 (struct isl_map
*)set1
, (struct isl_map
*)set2
);
3552 struct isl_set
*isl_set_union(struct isl_set
*set1
, struct isl_set
*set2
)
3554 return (struct isl_set
*)
3555 isl_map_union((struct isl_map
*)set1
, (struct isl_map
*)set2
);
3558 struct isl_map
*isl_map_intersect_range(
3559 struct isl_map
*map
, struct isl_set
*set
)
3562 struct isl_map
*result
;
3568 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
) &&
3569 ISL_F_ISSET(set
, ISL_MAP_DISJOINT
))
3570 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
3572 result
= isl_map_alloc_dim(isl_dim_copy(map
->dim
),
3573 map
->n
* set
->n
, flags
);
3576 for (i
= 0; i
< map
->n
; ++i
)
3577 for (j
= 0; j
< set
->n
; ++j
) {
3578 result
= isl_map_add(result
,
3579 isl_basic_map_intersect_range(
3580 isl_basic_map_copy(map
->p
[i
]),
3581 isl_basic_set_copy(set
->p
[j
])));
3594 struct isl_map
*isl_map_intersect_domain(
3595 struct isl_map
*map
, struct isl_set
*set
)
3597 return isl_map_reverse(
3598 isl_map_intersect_range(isl_map_reverse(map
), set
));
3601 struct isl_map
*isl_map_apply_domain(
3602 struct isl_map
*map1
, struct isl_map
*map2
)
3606 map1
= isl_map_reverse(map1
);
3607 map1
= isl_map_apply_range(map1
, map2
);
3608 return isl_map_reverse(map1
);
3615 struct isl_map
*isl_map_apply_range(
3616 struct isl_map
*map1
, struct isl_map
*map2
)
3618 struct isl_dim
*dim_result
;
3619 struct isl_map
*result
;
3628 dim_result
= isl_dim_join(isl_dim_copy(map1
->dim
),
3629 isl_dim_copy(map2
->dim
));
3631 result
= isl_map_alloc_dim(dim_result
, map1
->n
* map2
->n
, 0);
3634 for (i
= 0; i
< map1
->n
; ++i
)
3635 for (j
= 0; j
< map2
->n
; ++j
) {
3636 result
= isl_map_add(result
,
3637 isl_basic_map_apply_range(
3638 isl_basic_map_copy(map1
->p
[i
]),
3639 isl_basic_map_copy(map2
->p
[j
])));
3645 if (result
&& result
->n
<= 1)
3646 ISL_F_SET(result
, ISL_MAP_DISJOINT
);
3655 * returns range - domain
3657 struct isl_basic_set
*isl_basic_map_deltas(struct isl_basic_map
*bmap
)
3659 struct isl_basic_set
*bset
;
3666 dim
= isl_basic_map_n_in(bmap
);
3667 nparam
= isl_basic_map_n_param(bmap
);
3668 isl_assert(bmap
->ctx
, dim
== isl_basic_map_n_out(bmap
), goto error
);
3669 bset
= isl_basic_set_from_basic_map(bmap
);
3670 bset
= isl_basic_set_cow(bset
);
3671 bset
= isl_basic_set_extend(bset
, nparam
, 3*dim
, 0, dim
, 0);
3672 bset
= isl_basic_set_swap_vars(bset
, 2*dim
);
3673 for (i
= 0; i
< dim
; ++i
) {
3674 int j
= isl_basic_map_alloc_equality(
3675 (struct isl_basic_map
*)bset
);
3678 isl_seq_clr(bset
->eq
[j
], 1 + isl_basic_set_total_dim(bset
));
3679 isl_int_set_si(bset
->eq
[j
][1+nparam
+i
], 1);
3680 isl_int_set_si(bset
->eq
[j
][1+nparam
+dim
+i
], 1);
3681 isl_int_set_si(bset
->eq
[j
][1+nparam
+2*dim
+i
], -1);
3683 return isl_basic_set_project_out(bset
, 2*dim
, 0);
3685 isl_basic_map_free(bmap
);
3690 * returns range - domain
3692 struct isl_set
*isl_map_deltas(struct isl_map
*map
)
3695 struct isl_set
*result
;
3700 isl_assert(map
->ctx
, isl_map_n_in(map
) == isl_map_n_out(map
), goto error
);
3701 result
= isl_set_alloc(map
->ctx
, isl_map_n_param(map
),
3702 isl_map_n_in(map
), map
->n
, map
->flags
);
3705 for (i
= 0; i
< map
->n
; ++i
)
3706 result
= isl_set_add(result
,
3707 isl_basic_map_deltas(isl_basic_map_copy(map
->p
[i
])));
3715 static struct isl_basic_map
*basic_map_identity(struct isl_dim
*dims
)
3717 struct isl_basic_map
*bmap
;
3725 nparam
= dims
->nparam
;
3727 bmap
= isl_basic_map_alloc_dim(dims
, 0, dim
, 0);
3731 for (i
= 0; i
< dim
; ++i
) {
3732 int j
= isl_basic_map_alloc_equality(bmap
);
3735 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
3736 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
3737 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], -1);
3739 return isl_basic_map_finalize(bmap
);
3741 isl_basic_map_free(bmap
);
3745 struct isl_basic_map
*isl_basic_map_identity(struct isl_dim
*set_dim
)
3747 struct isl_dim
*dim
= isl_dim_map(set_dim
);
3750 return basic_map_identity(dim
);
3753 struct isl_basic_map
*isl_basic_map_identity_like(struct isl_basic_map
*model
)
3755 if (!model
|| !model
->dim
)
3757 isl_assert(model
->ctx
,
3758 model
->dim
->n_in
== model
->dim
->n_out
, return NULL
);
3759 return basic_map_identity(isl_dim_copy(model
->dim
));
3762 static struct isl_map
*map_identity(struct isl_dim
*dim
)
3764 struct isl_map
*map
= isl_map_alloc_dim(dim
, 1, ISL_MAP_DISJOINT
);
3765 return isl_map_add(map
, basic_map_identity(isl_dim_copy(dim
)));
3768 struct isl_map
*isl_map_identity(struct isl_dim
*set_dim
)
3770 struct isl_dim
*dim
= isl_dim_map(set_dim
);
3773 return map_identity(dim
);
3776 struct isl_map
*isl_map_identity_like(struct isl_map
*model
)
3778 if (!model
|| !model
->dim
)
3780 isl_assert(model
->ctx
,
3781 model
->dim
->n_in
== model
->dim
->n_out
, return NULL
);
3782 return map_identity(isl_dim_copy(model
->dim
));
3785 struct isl_map
*isl_map_identity_like_basic_map(struct isl_basic_map
*model
)
3787 if (!model
|| !model
->dim
)
3789 isl_assert(model
->ctx
,
3790 model
->dim
->n_in
== model
->dim
->n_out
, return NULL
);
3791 return map_identity(isl_dim_copy(model
->dim
));
3794 /* Construct a basic set with all set dimensions having only non-negative
3797 struct isl_basic_set
*isl_basic_set_positive_orthant(struct isl_dim
*dims
)
3802 struct isl_basic_set
*bset
;
3806 nparam
= dims
->nparam
;
3808 bset
= isl_basic_set_alloc_dim(dims
, 0, 0, dim
);
3811 for (i
= 0; i
< dim
; ++i
) {
3812 int k
= isl_basic_set_alloc_inequality(bset
);
3815 isl_seq_clr(bset
->ineq
[k
], 1 + isl_basic_set_total_dim(bset
));
3816 isl_int_set_si(bset
->ineq
[k
][1 + nparam
+ i
], 1);
3820 isl_basic_set_free(bset
);
3824 int isl_set_is_equal(struct isl_set
*set1
, struct isl_set
*set2
)
3826 return isl_map_is_equal((struct isl_map
*)set1
, (struct isl_map
*)set2
);
3829 int isl_set_is_subset(struct isl_set
*set1
, struct isl_set
*set2
)
3831 return isl_map_is_subset(
3832 (struct isl_map
*)set1
, (struct isl_map
*)set2
);
3835 int isl_basic_map_is_subset(
3836 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3839 struct isl_map
*map1
;
3840 struct isl_map
*map2
;
3842 if (!bmap1
|| !bmap2
)
3845 map1
= isl_map_from_basic_map(isl_basic_map_copy(bmap1
));
3846 map2
= isl_map_from_basic_map(isl_basic_map_copy(bmap2
));
3848 is_subset
= isl_map_is_subset(map1
, map2
);
3856 int isl_basic_map_is_equal(
3857 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3861 if (!bmap1
|| !bmap2
)
3863 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
3866 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
3870 int isl_basic_set_is_equal(
3871 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
3873 return isl_basic_map_is_equal(
3874 (struct isl_basic_map
*)bset1
, (struct isl_basic_map
*)bset2
);
3877 int isl_map_is_empty(struct isl_map
*map
)
3884 for (i
= 0; i
< map
->n
; ++i
) {
3885 is_empty
= isl_basic_map_is_empty(map
->p
[i
]);
3894 int isl_map_fast_is_empty(struct isl_map
*map
)
3899 int isl_set_is_empty(struct isl_set
*set
)
3901 return isl_map_is_empty((struct isl_map
*)set
);
3904 int isl_map_is_subset(struct isl_map
*map1
, struct isl_map
*map2
)
3908 struct isl_map
*diff
;
3913 if (isl_map_is_empty(map1
))
3916 if (isl_map_is_empty(map2
))
3919 diff
= isl_map_subtract(isl_map_copy(map1
), isl_map_copy(map2
));
3923 is_subset
= isl_map_is_empty(diff
);
3929 int isl_map_is_equal(struct isl_map
*map1
, struct isl_map
*map2
)
3935 is_subset
= isl_map_is_subset(map1
, map2
);
3938 is_subset
= isl_map_is_subset(map2
, map1
);
3942 int isl_basic_map_is_strict_subset(
3943 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3947 if (!bmap1
|| !bmap2
)
3949 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
3952 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
3953 if (is_subset
== -1)
3958 int isl_map_is_strict_subset(struct isl_map
*map1
, struct isl_map
*map2
)
3964 is_subset
= isl_map_is_subset(map1
, map2
);
3967 is_subset
= isl_map_is_subset(map2
, map1
);
3968 if (is_subset
== -1)
3973 int isl_basic_map_is_universe(struct isl_basic_map
*bmap
)
3977 return bmap
->n_eq
== 0 && bmap
->n_ineq
== 0;
3980 int isl_basic_set_is_universe(struct isl_basic_set
*bset
)
3984 return bset
->n_eq
== 0 && bset
->n_ineq
== 0;
3987 int isl_basic_map_is_empty(struct isl_basic_map
*bmap
)
3989 struct isl_basic_set
*bset
= NULL
;
3990 struct isl_vec
*sample
= NULL
;
3997 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
4000 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
4001 struct isl_basic_map
*copy
= isl_basic_map_copy(bmap
);
4002 copy
= isl_basic_map_convex_hull(copy
);
4003 empty
= ISL_F_ISSET(copy
, ISL_BASIC_MAP_EMPTY
);
4004 isl_basic_map_free(copy
);
4008 total
= 1 + isl_basic_map_total_dim(bmap
);
4009 if (bmap
->sample
&& bmap
->sample
->size
== total
) {
4010 int contains
= basic_map_contains(bmap
, bmap
->sample
);
4016 isl_vec_free(bmap
->sample
);
4017 bmap
->sample
= NULL
;
4018 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
4021 sample
= isl_basic_set_sample(bset
);
4024 empty
= sample
->size
== 0;
4025 isl_vec_free(bmap
->sample
);
4026 bmap
->sample
= sample
;
4028 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
4033 int isl_basic_map_fast_is_empty(struct isl_basic_map
*bmap
)
4037 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
);
4040 int isl_basic_set_fast_is_empty(struct isl_basic_set
*bset
)
4044 return ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
);
4047 int isl_basic_set_is_empty(struct isl_basic_set
*bset
)
4049 return isl_basic_map_is_empty((struct isl_basic_map
*)bset
);
4052 struct isl_map
*isl_basic_map_union(
4053 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
4055 struct isl_map
*map
;
4056 if (!bmap1
|| !bmap2
)
4059 isl_assert(bmap1
->ctx
, isl_dim_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
4061 map
= isl_map_alloc_dim(isl_dim_copy(bmap1
->dim
), 2, 0);
4064 map
= isl_map_add(map
, bmap1
);
4065 map
= isl_map_add(map
, bmap2
);
4068 isl_basic_map_free(bmap1
);
4069 isl_basic_map_free(bmap2
);
4073 struct isl_set
*isl_basic_set_union(
4074 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
4076 return (struct isl_set
*)isl_basic_map_union(
4077 (struct isl_basic_map
*)bset1
,
4078 (struct isl_basic_map
*)bset2
);
4081 /* Order divs such that any div only depends on previous divs */
4082 struct isl_basic_map
*isl_basic_map_order_divs(struct isl_basic_map
*bmap
)
4085 unsigned off
= isl_dim_total(bmap
->dim
);
4087 for (i
= 0; i
< bmap
->n_div
; ++i
) {
4089 if (isl_int_is_zero(bmap
->div
[i
][0]))
4091 pos
= isl_seq_first_non_zero(bmap
->div
[i
]+1+1+off
+i
,
4095 isl_basic_map_swap_div(bmap
, i
, i
+ pos
);
4101 struct isl_basic_set
*isl_basic_set_order_divs(struct isl_basic_set
*bset
)
4103 return (struct isl_basic_set
*)
4104 isl_basic_map_order_divs((struct isl_basic_map
*)bset
);
4107 /* Look for a div in dst that corresponds to the div "div" in src.
4108 * The divs before "div" in src and dst are assumed to be the same.
4110 * Returns -1 if no corresponding div was found and the position
4111 * of the corresponding div in dst otherwise.
4113 static int find_div(struct isl_basic_map
*dst
,
4114 struct isl_basic_map
*src
, unsigned div
)
4118 unsigned total
= isl_dim_total(src
->dim
);
4120 isl_assert(dst
->ctx
, div
<= dst
->n_div
, return -1);
4121 for (i
= div
; i
< dst
->n_div
; ++i
)
4122 if (isl_seq_eq(dst
->div
[i
], src
->div
[div
], 1+1+total
+div
) &&
4123 isl_seq_first_non_zero(dst
->div
[i
]+1+1+total
+div
,
4124 dst
->n_div
- div
) == -1)
4129 struct isl_basic_map
*isl_basic_map_align_divs(
4130 struct isl_basic_map
*dst
, struct isl_basic_map
*src
)
4133 unsigned total
= isl_dim_total(src
->dim
);
4138 if (src
->n_div
== 0)
4141 for (i
= 0; i
< src
->n_div
; ++i
)
4142 isl_assert(src
->ctx
, !isl_int_is_zero(src
->div
[i
][0]), goto error
);
4144 src
= isl_basic_map_order_divs(src
);
4145 dst
= isl_basic_map_cow(dst
);
4146 dst
= isl_basic_map_extend_dim(dst
, isl_dim_copy(dst
->dim
),
4147 src
->n_div
, 0, 2 * src
->n_div
);
4150 for (i
= 0; i
< src
->n_div
; ++i
) {
4151 int j
= find_div(dst
, src
, i
);
4153 j
= isl_basic_map_alloc_div(dst
);
4156 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
+i
);
4157 isl_seq_clr(dst
->div
[j
]+1+1+total
+i
, dst
->n_div
- i
);
4158 if (add_div_constraints(dst
, j
) < 0)
4162 isl_basic_map_swap_div(dst
, i
, j
);
4166 isl_basic_map_free(dst
);
4170 struct isl_basic_set
*isl_basic_set_align_divs(
4171 struct isl_basic_set
*dst
, struct isl_basic_set
*src
)
4173 return (struct isl_basic_set
*)isl_basic_map_align_divs(
4174 (struct isl_basic_map
*)dst
, (struct isl_basic_map
*)src
);
4177 struct isl_map
*isl_map_align_divs(struct isl_map
*map
)
4181 map
= isl_map_compute_divs(map
);
4182 map
= isl_map_cow(map
);
4186 for (i
= 1; i
< map
->n
; ++i
)
4187 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], map
->p
[i
]);
4188 for (i
= 1; i
< map
->n
; ++i
)
4189 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], map
->p
[0]);
4191 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4195 struct isl_set
*isl_set_align_divs(struct isl_set
*set
)
4197 return (struct isl_set
*)isl_map_align_divs((struct isl_map
*)set
);
4200 static struct isl_map
*add_cut_constraint(struct isl_map
*dst
,
4201 struct isl_basic_map
*src
, isl_int
*c
,
4202 unsigned len
, int oppose
)
4204 struct isl_basic_map
*copy
= NULL
;
4209 copy
= isl_basic_map_copy(src
);
4210 copy
= isl_basic_map_cow(copy
);
4213 copy
= isl_basic_map_extend_constraints(copy
, 0, 1);
4214 k
= isl_basic_map_alloc_inequality(copy
);
4218 isl_seq_neg(copy
->ineq
[k
], c
, len
);
4220 isl_seq_cpy(copy
->ineq
[k
], c
, len
);
4221 total
= 1 + isl_basic_map_total_dim(copy
);
4222 isl_seq_clr(copy
->ineq
[k
]+len
, total
- len
);
4223 isl_inequality_negate(copy
, k
);
4224 copy
= isl_basic_map_simplify(copy
);
4225 copy
= isl_basic_map_finalize(copy
);
4226 is_empty
= isl_basic_map_is_empty(copy
);
4230 dst
= isl_map_add(dst
, copy
);
4232 isl_basic_map_free(copy
);
4235 isl_basic_map_free(copy
);
4240 static struct isl_map
*subtract(struct isl_map
*map
, struct isl_basic_map
*bmap
)
4244 struct isl_map
*rest
= NULL
;
4246 unsigned total
= isl_basic_map_total_dim(bmap
);
4253 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
4254 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
4256 max
= map
->n
* (2 * bmap
->n_eq
+ bmap
->n_ineq
);
4257 rest
= isl_map_alloc_dim(isl_dim_copy(map
->dim
), max
, flags
);
4261 for (i
= 0; i
< map
->n
; ++i
) {
4262 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], bmap
);
4267 for (j
= 0; j
< map
->n
; ++j
)
4268 map
->p
[j
] = isl_basic_map_cow(map
->p
[j
]);
4270 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
4271 for (j
= 0; j
< map
->n
; ++j
) {
4272 rest
= add_cut_constraint(rest
,
4273 map
->p
[j
], bmap
->eq
[i
], 1+total
, 0);
4277 rest
= add_cut_constraint(rest
,
4278 map
->p
[j
], bmap
->eq
[i
], 1+total
, 1);
4282 map
->p
[j
] = isl_basic_map_extend_constraints(map
->p
[j
],
4286 k
= isl_basic_map_alloc_equality(map
->p
[j
]);
4289 isl_seq_cpy(map
->p
[j
]->eq
[k
], bmap
->eq
[i
], 1+total
);
4290 isl_seq_clr(map
->p
[j
]->eq
[k
]+1+total
,
4291 map
->p
[j
]->n_div
- bmap
->n_div
);
4295 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
4296 for (j
= 0; j
< map
->n
; ++j
) {
4297 rest
= add_cut_constraint(rest
,
4298 map
->p
[j
], bmap
->ineq
[i
], 1+total
, 0);
4302 map
->p
[j
] = isl_basic_map_extend_constraints(map
->p
[j
],
4306 k
= isl_basic_map_alloc_inequality(map
->p
[j
]);
4309 isl_seq_cpy(map
->p
[j
]->ineq
[k
], bmap
->ineq
[i
], 1+total
);
4310 isl_seq_clr(map
->p
[j
]->ineq
[k
]+1+total
,
4311 map
->p
[j
]->n_div
- bmap
->n_div
);
4323 struct isl_map
*isl_map_subtract(struct isl_map
*map1
, struct isl_map
*map2
)
4329 isl_assert(map1
->ctx
, isl_dim_equal(map1
->dim
, map2
->dim
), goto error
);
4331 if (isl_map_is_empty(map2
)) {
4336 map1
= isl_map_compute_divs(map1
);
4337 map2
= isl_map_compute_divs(map2
);
4341 for (i
= 0; map1
&& i
< map2
->n
; ++i
)
4342 map1
= subtract(map1
, map2
->p
[i
]);
4352 struct isl_set
*isl_set_subtract(struct isl_set
*set1
, struct isl_set
*set2
)
4354 return (struct isl_set
*)
4356 (struct isl_map
*)set1
, (struct isl_map
*)set2
);
4359 struct isl_set
*isl_set_apply(struct isl_set
*set
, struct isl_map
*map
)
4363 isl_assert(set
->ctx
, isl_map_compatible_domain(map
, set
), goto error
);
4364 map
= isl_map_intersect_domain(map
, set
);
4365 set
= isl_map_range(map
);
4373 /* There is no need to cow as removing empty parts doesn't change
4374 * the meaning of the set.
4376 struct isl_map
*isl_map_remove_empty_parts(struct isl_map
*map
)
4383 for (i
= map
->n
-1; i
>= 0; --i
) {
4384 if (!ISL_F_ISSET(map
->p
[i
], ISL_BASIC_MAP_EMPTY
))
4386 isl_basic_map_free(map
->p
[i
]);
4387 if (i
!= map
->n
-1) {
4388 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4389 map
->p
[i
] = map
->p
[map
->n
-1];
4397 struct isl_set
*isl_set_remove_empty_parts(struct isl_set
*set
)
4399 return (struct isl_set
*)
4400 isl_map_remove_empty_parts((struct isl_map
*)set
);
4403 struct isl_basic_map
*isl_map_copy_basic_map(struct isl_map
*map
)
4405 struct isl_basic_map
*bmap
;
4406 if (!map
|| map
->n
== 0)
4408 bmap
= map
->p
[map
->n
-1];
4409 isl_assert(map
->ctx
, ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
), return NULL
);
4410 return isl_basic_map_copy(bmap
);
4413 struct isl_basic_set
*isl_set_copy_basic_set(struct isl_set
*set
)
4415 return (struct isl_basic_set
*)
4416 isl_map_copy_basic_map((struct isl_map
*)set
);
4419 struct isl_map
*isl_map_drop_basic_map(struct isl_map
*map
,
4420 struct isl_basic_map
*bmap
)
4426 for (i
= map
->n
-1; i
>= 0; --i
) {
4427 if (map
->p
[i
] != bmap
)
4429 map
= isl_map_cow(map
);
4432 isl_basic_map_free(map
->p
[i
]);
4433 if (i
!= map
->n
-1) {
4434 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
4435 map
->p
[i
] = map
->p
[map
->n
-1];
4440 isl_basic_map_free(bmap
);
4444 isl_basic_map_free(bmap
);
4448 struct isl_set
*isl_set_drop_basic_set(struct isl_set
*set
,
4449 struct isl_basic_set
*bset
)
4451 return (struct isl_set
*)isl_map_drop_basic_map((struct isl_map
*)set
,
4452 (struct isl_basic_map
*)bset
);
4455 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
4456 * for any common value of the parameters and dimensions preceding dim
4457 * in both basic sets, the values of dimension pos in bset1 are
4458 * smaller or larger than those in bset2.
4461 * 1 if bset1 follows bset2
4462 * -1 if bset1 precedes bset2
4463 * 0 if bset1 and bset2 are incomparable
4464 * -2 if some error occurred.
4466 int isl_basic_set_compare_at(struct isl_basic_set
*bset1
,
4467 struct isl_basic_set
*bset2
, int pos
)
4469 struct isl_dim
*dims
;
4470 struct isl_basic_map
*bmap1
= NULL
;
4471 struct isl_basic_map
*bmap2
= NULL
;
4472 struct isl_ctx
*ctx
;
4473 struct isl_vec
*obj
;
4476 unsigned dim1
, dim2
;
4478 enum isl_lp_result res
;
4481 if (!bset1
|| !bset2
)
4484 nparam
= isl_basic_set_n_param(bset1
);
4485 dim1
= isl_basic_set_n_dim(bset1
);
4486 dim2
= isl_basic_set_n_dim(bset2
);
4487 dims
= isl_dim_alloc(bset1
->ctx
, nparam
, pos
, dim1
- pos
);
4488 bmap1
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset1
), dims
);
4489 dims
= isl_dim_alloc(bset2
->ctx
, nparam
, pos
, dim2
- pos
);
4490 bmap2
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset2
), dims
);
4491 if (!bmap1
|| !bmap2
)
4493 bmap1
= isl_basic_map_cow(bmap1
);
4494 bmap1
= isl_basic_map_extend(bmap1
, nparam
,
4495 pos
, (dim1
- pos
) + (dim2
- pos
),
4496 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
4497 bmap1
= add_constraints(bmap1
, bmap2
, 0, dim1
- pos
);
4500 total
= isl_basic_map_total_dim(bmap1
);
4502 obj
= isl_vec_alloc(ctx
, 1 + total
);
4503 isl_seq_clr(obj
->block
.data
, 1 + total
);
4504 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
], 1);
4505 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
+(dim1
-pos
)], -1);
4510 res
= isl_basic_map_solve_lp(bmap1
, 0, obj
->block
.data
, ctx
->one
,
4512 if (res
== isl_lp_empty
)
4514 else if (res
== isl_lp_ok
&& isl_int_is_pos(num
))
4516 else if ((res
== isl_lp_ok
&& isl_int_is_neg(num
)) ||
4517 res
== isl_lp_unbounded
)
4523 isl_basic_map_free(bmap1
);
4527 isl_basic_map_free(bmap1
);
4528 isl_basic_map_free(bmap2
);
4532 static int isl_basic_map_fast_has_fixed_var(struct isl_basic_map
*bmap
,
4533 unsigned pos
, isl_int
*val
)
4541 total
= isl_basic_map_total_dim(bmap
);
4542 for (i
= 0, d
= total
-1; i
< bmap
->n_eq
&& d
+1 > pos
; ++i
) {
4543 for (; d
+1 > pos
; --d
)
4544 if (!isl_int_is_zero(bmap
->eq
[i
][1+d
]))
4548 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1, d
) != -1)
4550 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1+d
+1, total
-d
-1) != -1)
4552 if (!isl_int_is_one(bmap
->eq
[i
][1+d
]))
4555 isl_int_neg(*val
, bmap
->eq
[i
][0]);
4561 static int isl_map_fast_has_fixed_var(struct isl_map
*map
,
4562 unsigned pos
, isl_int
*val
)
4574 return isl_basic_map_fast_has_fixed_var(map
->p
[0], pos
, val
);
4577 fixed
= isl_basic_map_fast_has_fixed_var(map
->p
[0], pos
, &v
);
4578 for (i
= 1; fixed
== 1 && i
< map
->n
; ++i
) {
4579 fixed
= isl_basic_map_fast_has_fixed_var(map
->p
[i
], pos
, &tmp
);
4580 if (fixed
== 1 && isl_int_ne(tmp
, v
))
4584 isl_int_set(*val
, v
);
4590 static int isl_basic_set_fast_has_fixed_var(struct isl_basic_set
*bset
,
4591 unsigned pos
, isl_int
*val
)
4593 return isl_basic_map_fast_has_fixed_var((struct isl_basic_map
*)bset
,
4597 static int isl_set_fast_has_fixed_var(struct isl_set
*set
, unsigned pos
,
4600 return isl_map_fast_has_fixed_var((struct isl_map
*)set
, pos
, val
);
4603 int isl_basic_map_fast_is_fixed(struct isl_basic_map
*bmap
,
4604 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
4606 if (pos
>= isl_basic_map_dim(bmap
, type
))
4608 return isl_basic_map_fast_has_fixed_var(bmap
,
4609 isl_basic_map_offset(bmap
, type
) - 1 + pos
, val
);
4612 int isl_map_fast_is_fixed(struct isl_map
*map
,
4613 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
4615 if (pos
>= isl_map_dim(map
, type
))
4617 return isl_map_fast_has_fixed_var(map
,
4618 map_offset(map
, type
) - 1 + pos
, val
);
4621 /* Check if dimension dim has fixed value and if so and if val is not NULL,
4622 * then return this fixed value in *val.
4624 int isl_basic_set_fast_dim_is_fixed(struct isl_basic_set
*bset
, unsigned dim
,
4627 return isl_basic_set_fast_has_fixed_var(bset
,
4628 isl_basic_set_n_param(bset
) + dim
, val
);
4631 /* Check if dimension dim has fixed value and if so and if val is not NULL,
4632 * then return this fixed value in *val.
4634 int isl_set_fast_dim_is_fixed(struct isl_set
*set
, unsigned dim
, isl_int
*val
)
4636 return isl_set_fast_has_fixed_var(set
, isl_set_n_param(set
) + dim
, val
);
4639 /* Check if input variable in has fixed value and if so and if val is not NULL,
4640 * then return this fixed value in *val.
4642 int isl_map_fast_input_is_fixed(struct isl_map
*map
, unsigned in
, isl_int
*val
)
4644 return isl_map_fast_has_fixed_var(map
, isl_map_n_param(map
) + in
, val
);
4647 /* Check if dimension dim has an (obvious) fixed lower bound and if so
4648 * and if val is not NULL, then return this lower bound in *val.
4650 int isl_basic_set_fast_dim_has_fixed_lower_bound(struct isl_basic_set
*bset
,
4651 unsigned dim
, isl_int
*val
)
4653 int i
, i_eq
= -1, i_ineq
= -1;
4660 total
= isl_basic_set_total_dim(bset
);
4661 nparam
= isl_basic_set_n_param(bset
);
4662 for (i
= 0; i
< bset
->n_eq
; ++i
) {
4663 if (isl_int_is_zero(bset
->eq
[i
][1+nparam
+dim
]))
4669 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
4670 if (!isl_int_is_pos(bset
->ineq
[i
][1+nparam
+dim
]))
4672 if (i_eq
!= -1 || i_ineq
!= -1)
4676 if (i_eq
== -1 && i_ineq
== -1)
4678 c
= i_eq
!= -1 ? bset
->eq
[i_eq
] : bset
->ineq
[i_ineq
];
4679 /* The coefficient should always be one due to normalization. */
4680 if (!isl_int_is_one(c
[1+nparam
+dim
]))
4682 if (isl_seq_first_non_zero(c
+1, nparam
+dim
) != -1)
4684 if (isl_seq_first_non_zero(c
+1+nparam
+dim
+1,
4685 total
- nparam
- dim
- 1) != -1)
4688 isl_int_neg(*val
, c
[0]);
4692 int isl_set_fast_dim_has_fixed_lower_bound(struct isl_set
*set
,
4693 unsigned dim
, isl_int
*val
)
4705 return isl_basic_set_fast_dim_has_fixed_lower_bound(set
->p
[0],
4709 fixed
= isl_basic_set_fast_dim_has_fixed_lower_bound(set
->p
[0],
4711 for (i
= 1; fixed
== 1 && i
< set
->n
; ++i
) {
4712 fixed
= isl_basic_set_fast_dim_has_fixed_lower_bound(set
->p
[i
],
4714 if (fixed
== 1 && isl_int_ne(tmp
, v
))
4718 isl_int_set(*val
, v
);
4729 static int qsort_constraint_cmp(const void *p1
, const void *p2
)
4731 const struct constraint
*c1
= (const struct constraint
*)p1
;
4732 const struct constraint
*c2
= (const struct constraint
*)p2
;
4733 unsigned size
= isl_min(c1
->size
, c2
->size
);
4734 return isl_seq_cmp(c1
->c
, c2
->c
, size
);
4737 static struct isl_basic_map
*isl_basic_map_sort_constraints(
4738 struct isl_basic_map
*bmap
)
4741 struct constraint
*c
;
4746 total
= isl_basic_map_total_dim(bmap
);
4747 c
= isl_alloc_array(bmap
->ctx
, struct constraint
, bmap
->n_ineq
);
4750 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
4752 c
[i
].c
= bmap
->ineq
[i
];
4754 qsort(c
, bmap
->n_ineq
, sizeof(struct constraint
), qsort_constraint_cmp
);
4755 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
4756 bmap
->ineq
[i
] = c
[i
].c
;
4760 isl_basic_map_free(bmap
);
4764 struct isl_basic_map
*isl_basic_map_normalize(struct isl_basic_map
*bmap
)
4768 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED
))
4770 bmap
= isl_basic_map_convex_hull(bmap
);
4771 bmap
= isl_basic_map_sort_constraints(bmap
);
4772 ISL_F_SET(bmap
, ISL_BASIC_MAP_NORMALIZED
);
4776 struct isl_basic_set
*isl_basic_set_normalize(struct isl_basic_set
*bset
)
4778 return (struct isl_basic_set
*)isl_basic_map_normalize(
4779 (struct isl_basic_map
*)bset
);
4782 static int isl_basic_map_fast_cmp(const struct isl_basic_map
*bmap1
,
4783 const struct isl_basic_map
*bmap2
)
4790 if (isl_basic_map_n_param(bmap1
) != isl_basic_map_n_param(bmap2
))
4791 return isl_basic_map_n_param(bmap1
) - isl_basic_map_n_param(bmap2
);
4792 if (isl_basic_map_n_in(bmap1
) != isl_basic_map_n_in(bmap2
))
4793 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
4794 if (isl_basic_map_n_out(bmap1
) != isl_basic_map_n_out(bmap2
))
4795 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
4796 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
) &&
4797 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
4799 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
))
4801 if (ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
4803 if (bmap1
->n_eq
!= bmap2
->n_eq
)
4804 return bmap1
->n_eq
- bmap2
->n_eq
;
4805 if (bmap1
->n_ineq
!= bmap2
->n_ineq
)
4806 return bmap1
->n_ineq
- bmap2
->n_ineq
;
4807 if (bmap1
->n_div
!= bmap2
->n_div
)
4808 return bmap1
->n_div
- bmap2
->n_div
;
4809 total
= isl_basic_map_total_dim(bmap1
);
4810 for (i
= 0; i
< bmap1
->n_eq
; ++i
) {
4811 cmp
= isl_seq_cmp(bmap1
->eq
[i
], bmap2
->eq
[i
], 1+total
);
4815 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
4816 cmp
= isl_seq_cmp(bmap1
->ineq
[i
], bmap2
->ineq
[i
], 1+total
);
4820 for (i
= 0; i
< bmap1
->n_div
; ++i
) {
4821 cmp
= isl_seq_cmp(bmap1
->div
[i
], bmap2
->div
[i
], 1+1+total
);
4828 static int isl_basic_map_fast_is_equal(struct isl_basic_map
*bmap1
,
4829 struct isl_basic_map
*bmap2
)
4831 return isl_basic_map_fast_cmp(bmap1
, bmap2
) == 0;
4834 static int qsort_bmap_cmp(const void *p1
, const void *p2
)
4836 const struct isl_basic_map
*bmap1
= *(const struct isl_basic_map
**)p1
;
4837 const struct isl_basic_map
*bmap2
= *(const struct isl_basic_map
**)p2
;
4839 return isl_basic_map_fast_cmp(bmap1
, bmap2
);
4842 /* We normalize in place, but if anything goes wrong we need
4843 * to return NULL, so we need to make sure we don't change the
4844 * meaning of any possible other copies of map.
4846 struct isl_map
*isl_map_normalize(struct isl_map
*map
)
4849 struct isl_basic_map
*bmap
;
4853 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
))
4855 for (i
= 0; i
< map
->n
; ++i
) {
4856 bmap
= isl_basic_map_normalize(isl_basic_map_copy(map
->p
[i
]));
4859 isl_basic_map_free(map
->p
[i
]);
4862 qsort(map
->p
, map
->n
, sizeof(struct isl_basic_map
*), qsort_bmap_cmp
);
4863 ISL_F_SET(map
, ISL_MAP_NORMALIZED
);
4864 map
= isl_map_remove_empty_parts(map
);
4867 for (i
= map
->n
- 1; i
>= 1; --i
) {
4868 if (!isl_basic_map_fast_is_equal(map
->p
[i
-1], map
->p
[i
]))
4870 isl_basic_map_free(map
->p
[i
-1]);
4871 for (j
= i
; j
< map
->n
; ++j
)
4872 map
->p
[j
-1] = map
->p
[j
];
4882 struct isl_set
*isl_set_normalize(struct isl_set
*set
)
4884 return (struct isl_set
*)isl_map_normalize((struct isl_map
*)set
);
4887 int isl_map_fast_is_equal(struct isl_map
*map1
, struct isl_map
*map2
)
4897 if (!isl_dim_equal(map1
->dim
, map2
->dim
))
4900 map1
= isl_map_copy(map1
);
4901 map2
= isl_map_copy(map2
);
4902 map1
= isl_map_normalize(map1
);
4903 map2
= isl_map_normalize(map2
);
4906 equal
= map1
->n
== map2
->n
;
4907 for (i
= 0; equal
&& i
< map1
->n
; ++i
) {
4908 equal
= isl_basic_map_fast_is_equal(map1
->p
[i
], map2
->p
[i
]);
4921 int isl_set_fast_is_equal(struct isl_set
*set1
, struct isl_set
*set2
)
4923 return isl_map_fast_is_equal((struct isl_map
*)set1
,
4924 (struct isl_map
*)set2
);
4927 /* Return an interval that ranges from min to max (inclusive)
4929 struct isl_basic_set
*isl_basic_set_interval(struct isl_ctx
*ctx
,
4930 isl_int min
, isl_int max
)
4933 struct isl_basic_set
*bset
= NULL
;
4935 bset
= isl_basic_set_alloc(ctx
, 0, 1, 0, 0, 2);
4939 k
= isl_basic_set_alloc_inequality(bset
);
4942 isl_int_set_si(bset
->ineq
[k
][1], 1);
4943 isl_int_neg(bset
->ineq
[k
][0], min
);
4945 k
= isl_basic_set_alloc_inequality(bset
);
4948 isl_int_set_si(bset
->ineq
[k
][1], -1);
4949 isl_int_set(bset
->ineq
[k
][0], max
);
4953 isl_basic_set_free(bset
);
4957 /* Return the Cartesian product of the basic sets in list (in the given order).
4959 struct isl_basic_set
*isl_basic_set_product(struct isl_basic_set_list
*list
)
4967 struct isl_basic_set
*product
= NULL
;
4971 isl_assert(list
->ctx
, list
->n
> 0, goto error
);
4972 isl_assert(list
->ctx
, list
->p
[0], goto error
);
4973 nparam
= isl_basic_set_n_param(list
->p
[0]);
4974 dim
= isl_basic_set_n_dim(list
->p
[0]);
4975 extra
= list
->p
[0]->n_div
;
4976 n_eq
= list
->p
[0]->n_eq
;
4977 n_ineq
= list
->p
[0]->n_ineq
;
4978 for (i
= 1; i
< list
->n
; ++i
) {
4979 isl_assert(list
->ctx
, list
->p
[i
], goto error
);
4980 isl_assert(list
->ctx
,
4981 nparam
== isl_basic_set_n_param(list
->p
[i
]), goto error
);
4982 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
4983 extra
+= list
->p
[i
]->n_div
;
4984 n_eq
+= list
->p
[i
]->n_eq
;
4985 n_ineq
+= list
->p
[i
]->n_ineq
;
4987 product
= isl_basic_set_alloc(list
->ctx
, nparam
, dim
, extra
,
4992 for (i
= 0; i
< list
->n
; ++i
) {
4993 isl_basic_set_add_constraints(product
,
4994 isl_basic_set_copy(list
->p
[i
]), dim
);
4995 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
4997 isl_basic_set_list_free(list
);
5000 isl_basic_set_free(product
);
5001 isl_basic_set_list_free(list
);
5005 struct isl_basic_map
*isl_basic_map_product(
5006 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
5008 struct isl_dim
*dim_result
= NULL
;
5009 struct isl_basic_map
*bmap
;
5010 unsigned in1
, in2
, out1
, out2
, nparam
, total
, pos
;
5011 struct isl_dim_map
*dim_map1
, *dim_map2
;
5013 if (!bmap1
|| !bmap2
)
5016 isl_assert(bmap1
->ctx
, isl_dim_match(bmap1
->dim
, isl_dim_param
,
5017 bmap2
->dim
, isl_dim_param
), goto error
);
5018 dim_result
= isl_dim_product(isl_dim_copy(bmap1
->dim
),
5019 isl_dim_copy(bmap2
->dim
));
5021 in1
= isl_basic_map_n_in(bmap1
);
5022 in2
= isl_basic_map_n_in(bmap2
);
5023 out1
= isl_basic_map_n_out(bmap1
);
5024 out2
= isl_basic_map_n_out(bmap2
);
5025 nparam
= isl_basic_map_n_param(bmap1
);
5027 total
= nparam
+ in1
+ in2
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
5028 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
5029 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
5030 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
5031 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
5032 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
5033 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
5034 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
5035 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
5036 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
5037 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
5039 bmap
= isl_basic_map_alloc_dim(dim_result
,
5040 bmap1
->n_div
+ bmap2
->n_div
,
5041 bmap1
->n_eq
+ bmap2
->n_eq
,
5042 bmap1
->n_ineq
+ bmap2
->n_ineq
);
5043 bmap
= add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
5044 bmap
= add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
5045 bmap
= isl_basic_map_simplify(bmap
);
5046 return isl_basic_map_finalize(bmap
);
5048 isl_basic_map_free(bmap1
);
5049 isl_basic_map_free(bmap2
);
5053 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
5055 struct isl_map
*isl_map_product(struct isl_map
*map1
, struct isl_map
*map2
)
5058 struct isl_map
*result
;
5064 isl_assert(map1
->ctx
, isl_dim_match(map1
->dim
, isl_dim_param
,
5065 map2
->dim
, isl_dim_param
), goto error
);
5067 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
5068 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
5069 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
5071 result
= isl_map_alloc_dim(isl_dim_product(isl_dim_copy(map1
->dim
),
5072 isl_dim_copy(map2
->dim
)),
5073 map1
->n
* map2
->n
, flags
);
5076 for (i
= 0; i
< map1
->n
; ++i
)
5077 for (j
= 0; j
< map2
->n
; ++j
) {
5078 struct isl_basic_map
*part
;
5079 part
= isl_basic_map_product(
5080 isl_basic_map_copy(map1
->p
[i
]),
5081 isl_basic_map_copy(map2
->p
[j
]));
5082 if (isl_basic_map_is_empty(part
))
5083 isl_basic_map_free(part
);
5085 result
= isl_map_add(result
, part
);
5098 /* Given two set A and B, construct its Cartesian product A x B.
5100 struct isl_set
*isl_set_product(struct isl_set
*set1
, struct isl_set
*set2
)
5102 return (struct isl_set
*)isl_map_product((struct isl_map
*)set1
,
5103 (struct isl_map
*)set2
);
5106 uint32_t isl_basic_set_get_hash(struct isl_basic_set
*bset
)
5114 bset
= isl_basic_set_copy(bset
);
5115 bset
= isl_basic_set_normalize(bset
);
5118 total
= isl_basic_set_total_dim(bset
);
5119 isl_hash_byte(hash
, bset
->n_eq
& 0xFF);
5120 for (i
= 0; i
< bset
->n_eq
; ++i
) {
5122 c_hash
= isl_seq_get_hash(bset
->eq
[i
], 1 + total
);
5123 isl_hash_hash(hash
, c_hash
);
5125 isl_hash_byte(hash
, bset
->n_ineq
& 0xFF);
5126 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
5128 c_hash
= isl_seq_get_hash(bset
->ineq
[i
], 1 + total
);
5129 isl_hash_hash(hash
, c_hash
);
5131 isl_hash_byte(hash
, bset
->n_div
& 0xFF);
5132 for (i
= 0; i
< bset
->n_div
; ++i
) {
5134 if (isl_int_is_zero(bset
->div
[i
][0]))
5136 isl_hash_byte(hash
, i
& 0xFF);
5137 c_hash
= isl_seq_get_hash(bset
->div
[i
], 1 + 1 + total
);
5138 isl_hash_hash(hash
, c_hash
);
5140 isl_basic_set_free(bset
);
5144 uint32_t isl_set_get_hash(struct isl_set
*set
)
5151 set
= isl_set_copy(set
);
5152 set
= isl_set_normalize(set
);
5156 hash
= isl_hash_init();
5157 for (i
= 0; i
< set
->n
; ++i
) {
5159 bset_hash
= isl_basic_set_get_hash(set
->p
[i
]);
5160 isl_hash_hash(hash
, bset_hash
);
5168 /* Check if the value for dimension dim is completely determined
5169 * by the values of the other parameters and variables.
5170 * That is, check if dimension dim is involved in an equality.
5172 int isl_basic_set_dim_is_unique(struct isl_basic_set
*bset
, unsigned dim
)
5179 nparam
= isl_basic_set_n_param(bset
);
5180 for (i
= 0; i
< bset
->n_eq
; ++i
)
5181 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ dim
]))
5186 /* Check if the value for dimension dim is completely determined
5187 * by the values of the other parameters and variables.
5188 * That is, check if dimension dim is involved in an equality
5189 * for each of the subsets.
5191 int isl_set_dim_is_unique(struct isl_set
*set
, unsigned dim
)
5197 for (i
= 0; i
< set
->n
; ++i
) {
5199 unique
= isl_basic_set_dim_is_unique(set
->p
[i
], dim
);