11 #include "isl_map_private.h"
12 #include "isl_map_piplib.h"
13 #include "isl_sample.h"
16 /* Maps dst positions to src positions */
22 static struct isl_dim_map
*isl_dim_map_alloc(struct isl_ctx
*ctx
, unsigned len
)
25 struct isl_dim_map
*dim_map
;
26 dim_map
= isl_alloc(ctx
, struct isl_dim_map
,
27 sizeof(struct isl_dim_map
) + len
* sizeof(int));
30 dim_map
->len
= 1 + len
;
32 for (i
= 0; i
< len
; ++i
)
33 dim_map
->pos
[1 + i
] = -1;
37 static unsigned n(struct isl_dim
*dim
, enum isl_dim_type type
)
40 case isl_dim_param
: return dim
->nparam
;
41 case isl_dim_in
: return dim
->n_in
;
42 case isl_dim_out
: return dim
->n_out
;
43 case isl_dim_all
: return dim
->nparam
+ dim
->n_in
+ dim
->n_out
;
47 static unsigned pos(struct isl_dim
*dim
, enum isl_dim_type type
)
50 case isl_dim_param
: return 1;
51 case isl_dim_in
: return 1 + dim
->nparam
;
52 case isl_dim_out
: return 1 + dim
->nparam
+ dim
->n_in
;
56 static void isl_dim_map_dim(struct isl_dim_map
*dim_map
, struct isl_dim
*dim
,
57 enum isl_dim_type type
, unsigned dst_pos
)
65 src_pos
= pos(dim
, type
);
66 for (i
= 0; i
< n(dim
, type
); ++i
)
67 dim_map
->pos
[1 + dst_pos
+ i
] = src_pos
+ i
;
70 static void isl_dim_map_div(struct isl_dim_map
*dim_map
,
71 struct isl_basic_map
*bmap
, unsigned dst_pos
)
76 if (!dim_map
|| !bmap
)
79 src_pos
= 1 + isl_dim_total(bmap
->dim
);
80 for (i
= 0; i
< bmap
->n_div
; ++i
)
81 dim_map
->pos
[1 + dst_pos
+ i
] = src_pos
+ i
;
84 static void isl_dim_map_dump(struct isl_dim_map
*dim_map
)
88 for (i
= 0; i
< dim_map
->len
; ++i
)
89 fprintf(stderr
, "%d -> %d; ", i
, dim_map
->pos
[i
]);
90 fprintf(stderr
, "\n");
93 unsigned isl_basic_map_dim(const struct isl_basic_map
*bmap
,
94 enum isl_dim_type type
)
96 struct isl_dim
*dim
= bmap
->dim
;
100 case isl_dim_out
: return isl_dim_size(bmap
->dim
, type
);
101 case isl_dim_div
: return bmap
->n_div
;
102 case isl_dim_all
: return isl_basic_map_total_dim(bmap
);
106 unsigned isl_map_dim(const struct isl_map
*map
, enum isl_dim_type type
)
108 return n(map
->dim
, type
);
111 unsigned isl_set_dim(const struct isl_set
*set
, enum isl_dim_type type
)
113 return n(set
->dim
, type
);
116 unsigned isl_basic_map_offset(struct isl_basic_map
*bmap
,
117 enum isl_dim_type type
)
119 struct isl_dim
*dim
= bmap
->dim
;
121 case isl_dim_param
: return 1;
122 case isl_dim_in
: return 1 + dim
->nparam
;
123 case isl_dim_out
: return 1 + dim
->nparam
+ dim
->n_in
;
124 case isl_dim_div
: return 1 + dim
->nparam
+ dim
->n_in
+ dim
->n_out
;
128 static unsigned map_offset(struct isl_map
*map
, enum isl_dim_type type
)
130 return pos(map
->dim
, type
);
133 unsigned isl_basic_set_dim(const struct isl_basic_set
*bset
,
134 enum isl_dim_type type
)
136 return isl_basic_map_dim((const struct isl_basic_map
*)bset
, type
);
139 unsigned isl_basic_set_n_dim(const struct isl_basic_set
*bset
)
141 return bset
->dim
->n_out
;
144 unsigned isl_basic_set_n_param(const struct isl_basic_set
*bset
)
146 return bset
->dim
->nparam
;
149 unsigned isl_basic_set_total_dim(const struct isl_basic_set
*bset
)
151 return isl_dim_total(bset
->dim
) + bset
->n_div
;
154 unsigned isl_set_n_dim(const struct isl_set
*set
)
156 return set
->dim
->n_out
;
159 unsigned isl_set_n_param(const struct isl_set
*set
)
161 return set
->dim
->nparam
;
164 unsigned isl_basic_map_n_in(const struct isl_basic_map
*bmap
)
166 return bmap
->dim
->n_in
;
169 unsigned isl_basic_map_n_out(const struct isl_basic_map
*bmap
)
171 return bmap
->dim
->n_out
;
174 unsigned isl_basic_map_n_param(const struct isl_basic_map
*bmap
)
176 return bmap
->dim
->nparam
;
179 unsigned isl_basic_map_n_div(const struct isl_basic_map
*bmap
)
184 unsigned isl_basic_map_total_dim(const struct isl_basic_map
*bmap
)
186 return isl_dim_total(bmap
->dim
) + bmap
->n_div
;
189 unsigned isl_map_n_in(const struct isl_map
*map
)
191 return map
->dim
->n_in
;
194 unsigned isl_map_n_out(const struct isl_map
*map
)
196 return map
->dim
->n_out
;
199 unsigned isl_map_n_param(const struct isl_map
*map
)
201 return map
->dim
->nparam
;
204 int isl_map_compatible_domain(struct isl_map
*map
, struct isl_set
*set
)
206 return map
->dim
->n_in
== set
->dim
->n_out
&&
207 map
->dim
->nparam
== set
->dim
->nparam
;
210 int isl_basic_map_compatible_domain(struct isl_basic_map
*bmap
,
211 struct isl_basic_set
*bset
)
213 return bmap
->dim
->n_in
== bset
->dim
->n_out
&&
214 bmap
->dim
->nparam
== bset
->dim
->nparam
;
217 int isl_basic_map_compatible_range(struct isl_basic_map
*bmap
,
218 struct isl_basic_set
*bset
)
220 return bmap
->dim
->n_out
== bset
->dim
->n_out
&&
221 bmap
->dim
->nparam
== bset
->dim
->nparam
;
224 struct isl_dim
*isl_basic_map_get_dim(struct isl_basic_map
*bmap
)
228 return isl_dim_copy(bmap
->dim
);
231 struct isl_dim
*isl_basic_set_get_dim(struct isl_basic_set
*bset
)
235 return isl_dim_copy(bset
->dim
);
238 struct isl_dim
*isl_map_get_dim(struct isl_map
*map
)
242 return isl_dim_copy(map
->dim
);
245 struct isl_dim
*isl_set_get_dim(struct isl_set
*set
)
249 return isl_dim_copy(set
->dim
);
252 static struct isl_basic_map
*basic_map_init(struct isl_ctx
*ctx
,
253 struct isl_basic_map
*bmap
, unsigned extra
,
254 unsigned n_eq
, unsigned n_ineq
)
257 size_t row_size
= 1 + isl_dim_total(bmap
->dim
) + extra
;
259 bmap
->block
= isl_blk_alloc(ctx
, (n_ineq
+ n_eq
) * row_size
);
260 if (isl_blk_is_error(bmap
->block
)) {
265 bmap
->ineq
= isl_alloc_array(ctx
, isl_int
*, n_ineq
+ n_eq
);
267 isl_blk_free(ctx
, bmap
->block
);
273 bmap
->block2
= isl_blk_empty();
276 bmap
->block2
= isl_blk_alloc(ctx
, extra
* (1 + row_size
));
277 if (isl_blk_is_error(bmap
->block2
)) {
279 isl_blk_free(ctx
, bmap
->block
);
284 bmap
->div
= isl_alloc_array(ctx
, isl_int
*, extra
);
286 isl_blk_free(ctx
, bmap
->block2
);
288 isl_blk_free(ctx
, bmap
->block
);
294 for (i
= 0; i
< n_ineq
+ n_eq
; ++i
)
295 bmap
->ineq
[i
] = bmap
->block
.data
+ i
* row_size
;
297 for (i
= 0; i
< extra
; ++i
)
298 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + row_size
);
304 bmap
->c_size
= n_eq
+ n_ineq
;
305 bmap
->eq
= bmap
->ineq
+ n_ineq
;
314 isl_basic_map_free(bmap
);
318 struct isl_basic_set
*isl_basic_set_alloc(struct isl_ctx
*ctx
,
319 unsigned nparam
, unsigned dim
, unsigned extra
,
320 unsigned n_eq
, unsigned n_ineq
)
322 struct isl_basic_map
*bmap
;
323 bmap
= isl_basic_map_alloc(ctx
, nparam
, 0, dim
, extra
, n_eq
, n_ineq
);
324 return (struct isl_basic_set
*)bmap
;
327 struct isl_basic_set
*isl_basic_set_alloc_dim(struct isl_dim
*dim
,
328 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
330 struct isl_basic_map
*bmap
;
333 isl_assert(dim
->ctx
, dim
->n_in
== 0, return NULL
);
334 bmap
= isl_basic_map_alloc_dim(dim
, extra
, n_eq
, n_ineq
);
335 return (struct isl_basic_set
*)bmap
;
338 struct isl_basic_map
*isl_basic_map_alloc_dim(struct isl_dim
*dim
,
339 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
341 struct isl_basic_map
*bmap
;
345 bmap
= isl_alloc_type(dim
->ctx
, struct isl_basic_map
);
350 return basic_map_init(dim
->ctx
, bmap
, extra
, n_eq
, n_ineq
);
356 struct isl_basic_map
*isl_basic_map_alloc(struct isl_ctx
*ctx
,
357 unsigned nparam
, unsigned in
, unsigned out
, unsigned extra
,
358 unsigned n_eq
, unsigned n_ineq
)
360 struct isl_basic_map
*bmap
;
363 dim
= isl_dim_alloc(ctx
, nparam
, in
, out
);
367 bmap
= isl_basic_map_alloc_dim(dim
, extra
, n_eq
, n_ineq
);
371 static void dup_constraints(
372 struct isl_basic_map
*dst
, struct isl_basic_map
*src
)
375 unsigned total
= isl_basic_map_total_dim(src
);
377 for (i
= 0; i
< src
->n_eq
; ++i
) {
378 int j
= isl_basic_map_alloc_equality(dst
);
379 isl_seq_cpy(dst
->eq
[j
], src
->eq
[i
], 1+total
);
382 for (i
= 0; i
< src
->n_ineq
; ++i
) {
383 int j
= isl_basic_map_alloc_inequality(dst
);
384 isl_seq_cpy(dst
->ineq
[j
], src
->ineq
[i
], 1+total
);
387 for (i
= 0; i
< src
->n_div
; ++i
) {
388 int j
= isl_basic_map_alloc_div(dst
);
389 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
);
391 ISL_F_SET(dst
, ISL_BASIC_SET_FINAL
);
394 struct isl_basic_map
*isl_basic_map_dup(struct isl_basic_map
*bmap
)
396 struct isl_basic_map
*dup
;
400 dup
= isl_basic_map_alloc_dim(isl_dim_copy(bmap
->dim
),
401 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
404 dup_constraints(dup
, bmap
);
405 dup
->flags
= bmap
->flags
;
406 dup
->sample
= isl_vec_copy(bmap
->sample
);
410 struct isl_basic_set
*isl_basic_set_dup(struct isl_basic_set
*bset
)
412 struct isl_basic_map
*dup
;
414 dup
= isl_basic_map_dup((struct isl_basic_map
*)bset
);
415 return (struct isl_basic_set
*)dup
;
418 struct isl_basic_set
*isl_basic_set_copy(struct isl_basic_set
*bset
)
423 if (ISL_F_ISSET(bset
, ISL_BASIC_SET_FINAL
)) {
427 return isl_basic_set_dup(bset
);
430 struct isl_set
*isl_set_copy(struct isl_set
*set
)
439 struct isl_basic_map
*isl_basic_map_copy(struct isl_basic_map
*bmap
)
444 if (ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
)) {
448 return isl_basic_map_dup(bmap
);
451 struct isl_map
*isl_map_copy(struct isl_map
*map
)
460 void isl_basic_map_free(struct isl_basic_map
*bmap
)
468 isl_ctx_deref(bmap
->ctx
);
470 isl_blk_free(bmap
->ctx
, bmap
->block2
);
472 isl_blk_free(bmap
->ctx
, bmap
->block
);
473 isl_vec_free(bmap
->sample
);
474 isl_dim_free(bmap
->dim
);
478 void isl_basic_set_free(struct isl_basic_set
*bset
)
480 isl_basic_map_free((struct isl_basic_map
*)bset
);
483 static int room_for_con(struct isl_basic_map
*bmap
, unsigned n
)
485 return bmap
->n_eq
+ bmap
->n_ineq
+ n
<= bmap
->c_size
;
488 int isl_basic_map_alloc_equality(struct isl_basic_map
*bmap
)
494 isl_assert(ctx
, room_for_con(bmap
, 1), return -1);
495 isl_assert(ctx
, (bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
<= bmap
->c_size
,
497 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
498 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
499 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
500 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
501 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
502 if ((bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
== bmap
->c_size
) {
504 int j
= isl_basic_map_alloc_inequality(bmap
);
508 bmap
->ineq
[j
] = bmap
->ineq
[bmap
->n_ineq
- 1];
509 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
516 isl_seq_clr(bmap
->eq
[bmap
->n_eq
] + 1 + isl_basic_map_total_dim(bmap
),
517 bmap
->extra
- bmap
->n_div
);
521 int isl_basic_set_alloc_equality(struct isl_basic_set
*bset
)
523 return isl_basic_map_alloc_equality((struct isl_basic_map
*)bset
);
526 int isl_basic_map_free_equality(struct isl_basic_map
*bmap
, unsigned n
)
530 isl_assert(bmap
->ctx
, n
<= bmap
->n_eq
, return -1);
535 int isl_basic_set_free_equality(struct isl_basic_set
*bset
, unsigned n
)
537 return isl_basic_map_free_equality((struct isl_basic_map
*)bset
, n
);
540 int isl_basic_map_drop_equality(struct isl_basic_map
*bmap
, unsigned pos
)
545 isl_assert(bmap
->ctx
, pos
< bmap
->n_eq
, return -1);
547 if (pos
!= bmap
->n_eq
- 1) {
549 bmap
->eq
[pos
] = bmap
->eq
[bmap
->n_eq
- 1];
550 bmap
->eq
[bmap
->n_eq
- 1] = t
;
556 int isl_basic_set_drop_equality(struct isl_basic_set
*bset
, unsigned pos
)
558 return isl_basic_map_drop_equality((struct isl_basic_map
*)bset
, pos
);
561 void isl_basic_map_inequality_to_equality(
562 struct isl_basic_map
*bmap
, unsigned pos
)
567 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
568 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
573 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
574 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
575 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
576 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
579 static int room_for_ineq(struct isl_basic_map
*bmap
, unsigned n
)
581 return bmap
->n_ineq
+ n
<= bmap
->eq
- bmap
->ineq
;
584 int isl_basic_map_alloc_inequality(struct isl_basic_map
*bmap
)
590 isl_assert(ctx
, room_for_ineq(bmap
, 1), return -1);
591 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
592 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
593 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
594 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
595 isl_seq_clr(bmap
->ineq
[bmap
->n_ineq
] +
596 1 + isl_basic_map_total_dim(bmap
),
597 bmap
->extra
- bmap
->n_div
);
598 return bmap
->n_ineq
++;
601 int isl_basic_set_alloc_inequality(struct isl_basic_set
*bset
)
603 return isl_basic_map_alloc_inequality((struct isl_basic_map
*)bset
);
606 int isl_basic_map_free_inequality(struct isl_basic_map
*bmap
, unsigned n
)
610 isl_assert(bmap
->ctx
, n
<= bmap
->n_ineq
, return -1);
615 int isl_basic_set_free_inequality(struct isl_basic_set
*bset
, unsigned n
)
617 return isl_basic_map_free_inequality((struct isl_basic_map
*)bset
, n
);
620 int isl_basic_map_drop_inequality(struct isl_basic_map
*bmap
, unsigned pos
)
625 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
627 if (pos
!= bmap
->n_ineq
- 1) {
629 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
630 bmap
->ineq
[bmap
->n_ineq
- 1] = t
;
631 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
637 int isl_basic_set_drop_inequality(struct isl_basic_set
*bset
, unsigned pos
)
639 return isl_basic_map_drop_inequality((struct isl_basic_map
*)bset
, pos
);
642 int isl_basic_map_alloc_div(struct isl_basic_map
*bmap
)
646 isl_assert(bmap
->ctx
, bmap
->n_div
< bmap
->extra
, return -1);
647 isl_seq_clr(bmap
->div
[bmap
->n_div
] +
648 1 + 1 + isl_basic_map_total_dim(bmap
),
649 bmap
->extra
- bmap
->n_div
);
650 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
651 return bmap
->n_div
++;
654 int isl_basic_set_alloc_div(struct isl_basic_set
*bset
)
656 return isl_basic_map_alloc_div((struct isl_basic_map
*)bset
);
659 int isl_basic_map_free_div(struct isl_basic_map
*bmap
, unsigned n
)
663 isl_assert(bmap
->ctx
, n
<= bmap
->n_div
, return -1);
668 int isl_basic_set_free_div(struct isl_basic_set
*bset
, unsigned n
)
670 return isl_basic_map_free_div((struct isl_basic_map
*)bset
, n
);
673 /* Copy constraint from src to dst, putting the vars of src at offset
674 * dim_off in dst and the divs of src at offset div_off in dst.
675 * If both sets are actually map, then dim_off applies to the input
678 static void copy_constraint(struct isl_basic_map
*dst_map
, isl_int
*dst
,
679 struct isl_basic_map
*src_map
, isl_int
*src
,
680 unsigned in_off
, unsigned out_off
, unsigned div_off
)
682 unsigned src_nparam
= isl_basic_map_n_param(src_map
);
683 unsigned dst_nparam
= isl_basic_map_n_param(dst_map
);
684 unsigned src_in
= isl_basic_map_n_in(src_map
);
685 unsigned dst_in
= isl_basic_map_n_in(dst_map
);
686 unsigned src_out
= isl_basic_map_n_out(src_map
);
687 unsigned dst_out
= isl_basic_map_n_out(dst_map
);
688 isl_int_set(dst
[0], src
[0]);
689 isl_seq_cpy(dst
+1, src
+1, isl_min(dst_nparam
, src_nparam
));
690 if (dst_nparam
> src_nparam
)
691 isl_seq_clr(dst
+1+src_nparam
,
692 dst_nparam
- src_nparam
);
693 isl_seq_clr(dst
+1+dst_nparam
, in_off
);
694 isl_seq_cpy(dst
+1+dst_nparam
+in_off
,
696 isl_min(dst_in
-in_off
, src_in
));
697 if (dst_in
-in_off
> src_in
)
698 isl_seq_clr(dst
+1+dst_nparam
+in_off
+src_in
,
699 dst_in
- in_off
- src_in
);
700 isl_seq_clr(dst
+1+dst_nparam
+dst_in
, out_off
);
701 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+out_off
,
702 src
+1+src_nparam
+src_in
,
703 isl_min(dst_out
-out_off
, src_out
));
704 if (dst_out
-out_off
> src_out
)
705 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+out_off
+src_out
,
706 dst_out
- out_off
- src_out
);
707 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
, div_off
);
708 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+dst_out
+div_off
,
709 src
+1+src_nparam
+src_in
+src_out
,
710 isl_min(dst_map
->extra
-div_off
, src_map
->n_div
));
711 if (dst_map
->n_div
-div_off
> src_map
->n_div
)
712 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
+
713 div_off
+src_map
->n_div
,
714 dst_map
->n_div
- div_off
- src_map
->n_div
);
717 static void copy_div(struct isl_basic_map
*dst_map
, isl_int
*dst
,
718 struct isl_basic_map
*src_map
, isl_int
*src
,
719 unsigned in_off
, unsigned out_off
, unsigned div_off
)
721 isl_int_set(dst
[0], src
[0]);
722 copy_constraint(dst_map
, dst
+1, src_map
, src
+1, in_off
, out_off
, div_off
);
725 static struct isl_basic_map
*add_constraints(struct isl_basic_map
*bmap1
,
726 struct isl_basic_map
*bmap2
, unsigned i_pos
, unsigned o_pos
)
731 if (!bmap1
|| !bmap2
)
734 div_off
= bmap1
->n_div
;
736 for (i
= 0; i
< bmap2
->n_eq
; ++i
) {
737 int i1
= isl_basic_map_alloc_equality(bmap1
);
740 copy_constraint(bmap1
, bmap1
->eq
[i1
], bmap2
, bmap2
->eq
[i
],
741 i_pos
, o_pos
, div_off
);
744 for (i
= 0; i
< bmap2
->n_ineq
; ++i
) {
745 int i1
= isl_basic_map_alloc_inequality(bmap1
);
748 copy_constraint(bmap1
, bmap1
->ineq
[i1
], bmap2
, bmap2
->ineq
[i
],
749 i_pos
, o_pos
, div_off
);
752 for (i
= 0; i
< bmap2
->n_div
; ++i
) {
753 int i1
= isl_basic_map_alloc_div(bmap1
);
756 copy_div(bmap1
, bmap1
->div
[i1
], bmap2
, bmap2
->div
[i
],
757 i_pos
, o_pos
, div_off
);
760 isl_basic_map_free(bmap2
);
765 isl_basic_map_free(bmap1
);
766 isl_basic_map_free(bmap2
);
770 static void copy_constraint_dim_map(isl_int
*dst
, isl_int
*src
,
771 struct isl_dim_map
*dim_map
)
775 for (i
= 0; i
< dim_map
->len
; ++i
) {
776 if (dim_map
->pos
[i
] < 0)
777 isl_int_set_si(dst
[i
], 0);
779 isl_int_set(dst
[i
], src
[dim_map
->pos
[i
]]);
783 static void copy_div_dim_map(isl_int
*dst
, isl_int
*src
,
784 struct isl_dim_map
*dim_map
)
786 isl_int_set(dst
[0], src
[0]);
787 copy_constraint_dim_map(dst
+1, src
+1, dim_map
);
790 static struct isl_basic_map
*add_constraints_dim_map(struct isl_basic_map
*dst
,
791 struct isl_basic_map
*src
, struct isl_dim_map
*dim_map
)
795 if (!src
|| !dst
|| !dim_map
)
798 for (i
= 0; i
< src
->n_eq
; ++i
) {
799 int i1
= isl_basic_map_alloc_equality(dst
);
802 copy_constraint_dim_map(dst
->eq
[i1
], src
->eq
[i
], dim_map
);
805 for (i
= 0; i
< src
->n_ineq
; ++i
) {
806 int i1
= isl_basic_map_alloc_inequality(dst
);
809 copy_constraint_dim_map(dst
->ineq
[i1
], src
->ineq
[i
], dim_map
);
812 for (i
= 0; i
< src
->n_div
; ++i
) {
813 int i1
= isl_basic_map_alloc_div(dst
);
816 copy_div_dim_map(dst
->div
[i1
], src
->div
[i
], dim_map
);
820 isl_basic_map_free(src
);
825 isl_basic_map_free(src
);
826 isl_basic_map_free(dst
);
830 struct isl_basic_set
*isl_basic_set_add_constraints(struct isl_basic_set
*bset1
,
831 struct isl_basic_set
*bset2
, unsigned pos
)
833 return (struct isl_basic_set
*)
834 add_constraints((struct isl_basic_map
*)bset1
,
835 (struct isl_basic_map
*)bset2
, 0, pos
);
838 struct isl_basic_map
*isl_basic_map_extend_dim(struct isl_basic_map
*base
,
839 struct isl_dim
*dim
, unsigned extra
,
840 unsigned n_eq
, unsigned n_ineq
)
842 struct isl_basic_map
*ext
;
852 dims_ok
= isl_dim_equal(base
->dim
, dim
) &&
853 base
->extra
>= base
->n_div
+ extra
;
855 if (dims_ok
&& room_for_con(base
, n_eq
+ n_ineq
) &&
856 room_for_ineq(base
, n_ineq
)) {
861 isl_assert(base
->ctx
, base
->dim
->nparam
<= dim
->nparam
, goto error
);
862 isl_assert(base
->ctx
, base
->dim
->n_in
<= dim
->n_in
, goto error
);
863 isl_assert(base
->ctx
, base
->dim
->n_out
<= dim
->n_out
, goto error
);
864 extra
+= base
->extra
;
866 n_ineq
+= base
->n_ineq
;
868 ext
= isl_basic_map_alloc_dim(dim
, extra
, n_eq
, n_ineq
);
874 ext
= add_constraints(ext
, base
, 0, 0);
877 ISL_F_CLR(ext
, ISL_BASIC_SET_FINAL
);
884 isl_basic_map_free(base
);
888 struct isl_basic_set
*isl_basic_set_extend_dim(struct isl_basic_set
*base
,
889 struct isl_dim
*dim
, unsigned extra
,
890 unsigned n_eq
, unsigned n_ineq
)
892 return (struct isl_basic_set
*)
893 isl_basic_map_extend_dim((struct isl_basic_map
*)base
, dim
,
894 extra
, n_eq
, n_ineq
);
897 struct isl_basic_map
*isl_basic_map_extend_constraints(
898 struct isl_basic_map
*base
, unsigned n_eq
, unsigned n_ineq
)
902 return isl_basic_map_extend_dim(base
, isl_dim_copy(base
->dim
),
906 struct isl_basic_map
*isl_basic_map_extend(struct isl_basic_map
*base
,
907 unsigned nparam
, unsigned n_in
, unsigned n_out
, unsigned extra
,
908 unsigned n_eq
, unsigned n_ineq
)
910 struct isl_basic_map
*bmap
;
915 dim
= isl_dim_alloc(base
->ctx
, nparam
, n_in
, n_out
);
919 bmap
= isl_basic_map_extend_dim(base
, dim
, extra
, n_eq
, n_ineq
);
923 struct isl_basic_set
*isl_basic_set_extend(struct isl_basic_set
*base
,
924 unsigned nparam
, unsigned dim
, unsigned extra
,
925 unsigned n_eq
, unsigned n_ineq
)
927 return (struct isl_basic_set
*)
928 isl_basic_map_extend((struct isl_basic_map
*)base
,
929 nparam
, 0, dim
, extra
, n_eq
, n_ineq
);
932 struct isl_basic_set
*isl_basic_set_extend_constraints(
933 struct isl_basic_set
*base
, unsigned n_eq
, unsigned n_ineq
)
935 return (struct isl_basic_set
*)
936 isl_basic_map_extend_constraints((struct isl_basic_map
*)base
,
940 struct isl_basic_set
*isl_basic_set_cow(struct isl_basic_set
*bset
)
942 return (struct isl_basic_set
*)
943 isl_basic_map_cow((struct isl_basic_map
*)bset
);
946 struct isl_basic_map
*isl_basic_map_cow(struct isl_basic_map
*bmap
)
953 bmap
= isl_basic_map_dup(bmap
);
955 ISL_F_CLR(bmap
, ISL_BASIC_SET_FINAL
);
959 struct isl_set
*isl_set_cow(struct isl_set
*set
)
967 return isl_set_dup(set
);
970 struct isl_map
*isl_map_cow(struct isl_map
*map
)
978 return isl_map_dup(map
);
981 static void swap_vars(struct isl_blk blk
, isl_int
*a
,
982 unsigned a_len
, unsigned b_len
)
984 isl_seq_cpy(blk
.data
, a
+a_len
, b_len
);
985 isl_seq_cpy(blk
.data
+b_len
, a
, a_len
);
986 isl_seq_cpy(a
, blk
.data
, b_len
+a_len
);
989 struct isl_basic_set
*isl_basic_set_swap_vars(
990 struct isl_basic_set
*bset
, unsigned n
)
1000 nparam
= isl_basic_set_n_param(bset
);
1001 dim
= isl_basic_set_n_dim(bset
);
1002 isl_assert(bset
->ctx
, n
<= dim
, goto error
);
1007 bset
= isl_basic_set_cow(bset
);
1011 blk
= isl_blk_alloc(bset
->ctx
, dim
);
1012 if (isl_blk_is_error(blk
))
1015 for (i
= 0; i
< bset
->n_eq
; ++i
)
1017 bset
->eq
[i
]+1+nparam
, n
, dim
- n
);
1019 for (i
= 0; i
< bset
->n_ineq
; ++i
)
1021 bset
->ineq
[i
]+1+nparam
, n
, dim
- n
);
1023 for (i
= 0; i
< bset
->n_div
; ++i
)
1025 bset
->div
[i
]+1+1+nparam
, n
, dim
- n
);
1027 isl_blk_free(bset
->ctx
, blk
);
1029 ISL_F_CLR(bset
, ISL_BASIC_SET_NORMALIZED
);
1033 isl_basic_set_free(bset
);
1037 struct isl_set
*isl_set_swap_vars(struct isl_set
*set
, unsigned n
)
1040 set
= isl_set_cow(set
);
1044 for (i
= 0; i
< set
->n
; ++i
) {
1045 set
->p
[i
] = isl_basic_set_swap_vars(set
->p
[i
], n
);
1051 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
1055 struct isl_basic_map
*isl_basic_map_set_to_empty(struct isl_basic_map
*bmap
)
1061 total
= isl_basic_map_total_dim(bmap
);
1062 isl_basic_map_free_div(bmap
, bmap
->n_div
);
1063 isl_basic_map_free_inequality(bmap
, bmap
->n_ineq
);
1065 isl_basic_map_free_equality(bmap
, bmap
->n_eq
-1);
1067 isl_basic_map_alloc_equality(bmap
);
1071 isl_int_set_si(bmap
->eq
[i
][0], 1);
1072 isl_seq_clr(bmap
->eq
[i
]+1, total
);
1073 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
1074 return isl_basic_map_finalize(bmap
);
1076 isl_basic_map_free(bmap
);
1080 struct isl_basic_set
*isl_basic_set_set_to_empty(struct isl_basic_set
*bset
)
1082 return (struct isl_basic_set
*)
1083 isl_basic_map_set_to_empty((struct isl_basic_map
*)bset
);
1086 static void swap_div(struct isl_basic_map
*bmap
, int a
, int b
)
1089 unsigned off
= isl_dim_total(bmap
->dim
);
1090 isl_int
*t
= bmap
->div
[a
];
1091 bmap
->div
[a
] = bmap
->div
[b
];
1094 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1095 isl_int_swap(bmap
->eq
[i
][1+off
+a
], bmap
->eq
[i
][1+off
+b
]);
1097 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1098 isl_int_swap(bmap
->ineq
[i
][1+off
+a
], bmap
->ineq
[i
][1+off
+b
]);
1100 for (i
= 0; i
< bmap
->n_div
; ++i
)
1101 isl_int_swap(bmap
->div
[i
][1+1+off
+a
], bmap
->div
[i
][1+1+off
+b
]);
1102 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1105 /* Eliminate the specified n dimensions starting at first from the
1106 * constraints using Fourier-Motzkin, The dimensions themselves
1109 struct isl_set
*isl_set_eliminate_dims(struct isl_set
*set
,
1110 unsigned first
, unsigned n
)
1120 set
= isl_set_cow(set
);
1123 isl_assert(set
->ctx
, first
+n
<= isl_set_n_dim(set
), goto error
);
1124 nparam
= isl_set_n_param(set
);
1126 for (i
= 0; i
< set
->n
; ++i
) {
1127 set
->p
[i
] = isl_basic_set_eliminate_vars(set
->p
[i
],
1138 /* Project out n dimensions starting at first using Fourier-Motzkin */
1139 struct isl_set
*isl_set_remove_dims(struct isl_set
*set
,
1140 unsigned first
, unsigned n
)
1142 set
= isl_set_eliminate_dims(set
, first
, n
);
1143 set
= isl_set_drop_dims(set
, first
, n
);
1147 struct isl_basic_set
*isl_basic_set_remove_divs(struct isl_basic_set
*bset
)
1149 bset
= isl_basic_set_eliminate_vars(bset
, isl_dim_total(bset
->dim
),
1157 struct isl_set
*isl_set_remove_divs(struct isl_set
*set
)
1166 set
= isl_set_cow(set
);
1170 for (i
= 0; i
< set
->n
; ++i
) {
1171 set
->p
[i
] = isl_basic_set_remove_divs(set
->p
[i
]);
1181 struct isl_basic_map
*isl_basic_map_remove(struct isl_basic_map
*bmap
,
1182 enum isl_dim_type type
, unsigned first
, unsigned n
)
1186 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
1190 bmap
= isl_basic_map_eliminate_vars(bmap
,
1191 isl_basic_map_offset(bmap
, type
) - 1 + first
, n
);
1192 bmap
= isl_basic_map_drop(bmap
, type
, first
, n
);
1195 isl_basic_map_free(bmap
);
1199 struct isl_map
*isl_map_remove(struct isl_map
*map
,
1200 enum isl_dim_type type
, unsigned first
, unsigned n
)
1208 map
= isl_map_cow(map
);
1211 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
1213 for (i
= 0; i
< map
->n
; ++i
) {
1214 map
->p
[i
] = isl_basic_map_eliminate_vars(map
->p
[i
],
1215 isl_basic_map_offset(map
->p
[i
], type
) - 1 + first
, n
);
1219 map
= isl_map_drop(map
, type
, first
, n
);
1226 /* Project out n inputs starting at first using Fourier-Motzkin */
1227 struct isl_map
*isl_map_remove_inputs(struct isl_map
*map
,
1228 unsigned first
, unsigned n
)
1230 return isl_map_remove(map
, isl_dim_in
, first
, n
);
1233 /* Project out n dimensions starting at first using Fourier-Motzkin */
1234 struct isl_basic_set
*isl_basic_set_remove_dims(struct isl_basic_set
*bset
,
1235 unsigned first
, unsigned n
)
1237 unsigned nparam
= isl_basic_set_n_param(bset
);
1238 bset
= isl_basic_set_eliminate_vars(bset
, nparam
+ first
, n
);
1239 bset
= isl_basic_set_drop_dims(bset
, first
, n
);
1243 static void dump_term(struct isl_basic_map
*bmap
,
1244 isl_int c
, int pos
, FILE *out
)
1247 unsigned in
= isl_basic_map_n_in(bmap
);
1248 unsigned dim
= in
+ isl_basic_map_n_out(bmap
);
1249 unsigned nparam
= isl_basic_map_n_param(bmap
);
1251 isl_int_print(out
, c
, 0);
1253 if (!isl_int_is_one(c
))
1254 isl_int_print(out
, c
, 0);
1255 if (pos
< 1 + nparam
) {
1256 name
= isl_dim_get_name(bmap
->dim
,
1257 isl_dim_param
, pos
- 1);
1259 fprintf(out
, "%s", name
);
1261 fprintf(out
, "p%d", pos
- 1);
1262 } else if (pos
< 1 + nparam
+ in
)
1263 fprintf(out
, "i%d", pos
- 1 - nparam
);
1264 else if (pos
< 1 + nparam
+ dim
)
1265 fprintf(out
, "o%d", pos
- 1 - nparam
- in
);
1267 fprintf(out
, "e%d", pos
- 1 - nparam
- dim
);
1271 static void dump_constraint_sign(struct isl_basic_map
*bmap
, isl_int
*c
,
1272 int sign
, FILE *out
)
1276 unsigned len
= 1 + isl_basic_map_total_dim(bmap
);
1280 for (i
= 0, first
= 1; i
< len
; ++i
) {
1281 if (isl_int_sgn(c
[i
]) * sign
<= 0)
1284 fprintf(out
, " + ");
1286 isl_int_abs(v
, c
[i
]);
1287 dump_term(bmap
, v
, i
, out
);
1294 static void dump_constraint(struct isl_basic_map
*bmap
, isl_int
*c
,
1295 const char *op
, FILE *out
, int indent
)
1299 fprintf(out
, "%*s", indent
, "");
1301 dump_constraint_sign(bmap
, c
, 1, out
);
1302 fprintf(out
, " %s ", op
);
1303 dump_constraint_sign(bmap
, c
, -1, out
);
1307 for (i
= bmap
->n_div
; i
< bmap
->extra
; ++i
) {
1308 if (isl_int_is_zero(c
[1+isl_dim_total(bmap
->dim
)+i
]))
1310 fprintf(out
, "%*s", indent
, "");
1311 fprintf(out
, "ERROR: unused div coefficient not zero\n");
1316 static void dump_constraints(struct isl_basic_map
*bmap
,
1317 isl_int
**c
, unsigned n
,
1318 const char *op
, FILE *out
, int indent
)
1322 for (i
= 0; i
< n
; ++i
)
1323 dump_constraint(bmap
, c
[i
], op
, out
, indent
);
1326 static void dump_affine(struct isl_basic_map
*bmap
, isl_int
*exp
, FILE *out
)
1330 unsigned total
= isl_basic_map_total_dim(bmap
);
1332 for (j
= 0; j
< 1 + total
; ++j
) {
1333 if (isl_int_is_zero(exp
[j
]))
1335 if (!first
&& isl_int_is_pos(exp
[j
]))
1337 dump_term(bmap
, exp
[j
], j
, out
);
1342 static void dump(struct isl_basic_map
*bmap
, FILE *out
, int indent
)
1346 dump_constraints(bmap
, bmap
->eq
, bmap
->n_eq
, "=", out
, indent
);
1347 dump_constraints(bmap
, bmap
->ineq
, bmap
->n_ineq
, ">=", out
, indent
);
1349 for (i
= 0; i
< bmap
->n_div
; ++i
) {
1350 fprintf(out
, "%*s", indent
, "");
1351 fprintf(out
, "e%d = [(", i
);
1352 dump_affine(bmap
, bmap
->div
[i
]+1, out
);
1354 isl_int_print(out
, bmap
->div
[i
][0], 0);
1355 fprintf(out
, "]\n");
1359 void isl_basic_set_dump(struct isl_basic_set
*bset
, FILE *out
, int indent
)
1362 fprintf(out
, "null basic set\n");
1366 fprintf(out
, "%*s", indent
, "");
1367 fprintf(out
, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
1368 bset
->ref
, bset
->dim
->nparam
, bset
->dim
->n_out
,
1369 bset
->extra
, bset
->flags
);
1370 dump((struct isl_basic_map
*)bset
, out
, indent
);
1373 void isl_basic_map_dump(struct isl_basic_map
*bmap
, FILE *out
, int indent
)
1376 fprintf(out
, "null basic map\n");
1380 fprintf(out
, "%*s", indent
, "");
1381 fprintf(out
, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
1382 "flags: %x, n_name: %d\n",
1384 bmap
->dim
->nparam
, bmap
->dim
->n_in
, bmap
->dim
->n_out
,
1385 bmap
->extra
, bmap
->flags
, bmap
->dim
->n_name
);
1386 dump(bmap
, out
, indent
);
1389 int isl_inequality_negate(struct isl_basic_map
*bmap
, unsigned pos
)
1394 total
= isl_basic_map_total_dim(bmap
);
1395 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
1396 isl_seq_neg(bmap
->ineq
[pos
], bmap
->ineq
[pos
], 1 + total
);
1397 isl_int_sub_ui(bmap
->ineq
[pos
][0], bmap
->ineq
[pos
][0], 1);
1398 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1402 struct isl_set
*isl_set_alloc_dim(struct isl_dim
*dim
, int n
, unsigned flags
)
1404 struct isl_set
*set
;
1408 isl_assert(dim
->ctx
, dim
->n_in
== 0, return NULL
);
1409 isl_assert(dim
->ctx
, n
>= 0, return NULL
);
1410 set
= isl_alloc(dim
->ctx
, struct isl_set
,
1411 sizeof(struct isl_set
) +
1412 n
* sizeof(struct isl_basic_set
*));
1416 set
->ctx
= dim
->ctx
;
1417 isl_ctx_ref(set
->ctx
);
1429 struct isl_set
*isl_set_alloc(struct isl_ctx
*ctx
,
1430 unsigned nparam
, unsigned dim
, int n
, unsigned flags
)
1432 struct isl_set
*set
;
1433 struct isl_dim
*dims
;
1435 dims
= isl_dim_alloc(ctx
, nparam
, 0, dim
);
1439 set
= isl_set_alloc_dim(dims
, n
, flags
);
1443 /* Make sure "map" has room for at least "n" more basic maps.
1445 struct isl_map
*isl_map_grow(struct isl_map
*map
, int n
)
1448 struct isl_map
*grown
= NULL
;
1452 isl_assert(map
->ctx
, n
>= 0, goto error
);
1453 if (map
->n
+ n
<= map
->size
)
1455 grown
= isl_map_alloc_dim(isl_map_get_dim(map
), map
->n
+ n
, map
->flags
);
1458 for (i
= 0; i
< map
->n
; ++i
) {
1459 grown
->p
[i
] = isl_basic_map_copy(map
->p
[i
]);
1467 isl_map_free(grown
);
1472 /* Make sure "set" has room for at least "n" more basic sets.
1474 struct isl_set
*isl_set_grow(struct isl_set
*set
, int n
)
1476 return (struct isl_set
*)isl_map_grow((struct isl_map
*)set
, n
);
1479 struct isl_set
*isl_set_dup(struct isl_set
*set
)
1482 struct isl_set
*dup
;
1487 dup
= isl_set_alloc_dim(isl_dim_copy(set
->dim
), set
->n
, set
->flags
);
1490 for (i
= 0; i
< set
->n
; ++i
)
1491 dup
= isl_set_add(dup
, isl_basic_set_copy(set
->p
[i
]));
1495 struct isl_set
*isl_set_from_basic_set(struct isl_basic_set
*bset
)
1497 struct isl_set
*set
;
1502 set
= isl_set_alloc_dim(isl_dim_copy(bset
->dim
), 1, ISL_MAP_DISJOINT
);
1504 isl_basic_set_free(bset
);
1507 return isl_set_add(set
, bset
);
1510 struct isl_map
*isl_map_from_basic_map(struct isl_basic_map
*bmap
)
1512 struct isl_map
*map
;
1517 map
= isl_map_alloc_dim(isl_dim_copy(bmap
->dim
), 1, ISL_MAP_DISJOINT
);
1519 isl_basic_map_free(bmap
);
1522 return isl_map_add(map
, bmap
);
1525 struct isl_set
*isl_set_add(struct isl_set
*set
, struct isl_basic_set
*bset
)
1529 isl_assert(set
->ctx
, isl_dim_equal(set
->dim
, bset
->dim
), goto error
);
1530 isl_assert(set
->ctx
, set
->n
< set
->size
, goto error
);
1531 set
->p
[set
->n
] = bset
;
1538 isl_basic_set_free(bset
);
1542 void isl_set_free(struct isl_set
*set
)
1552 isl_ctx_deref(set
->ctx
);
1553 for (i
= 0; i
< set
->n
; ++i
)
1554 isl_basic_set_free(set
->p
[i
]);
1555 isl_dim_free(set
->dim
);
1559 void isl_set_dump(struct isl_set
*set
, FILE *out
, int indent
)
1564 fprintf(out
, "null set\n");
1568 fprintf(out
, "%*s", indent
, "");
1569 fprintf(out
, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
1570 set
->ref
, set
->n
, set
->dim
->nparam
, set
->dim
->n_out
,
1572 for (i
= 0; i
< set
->n
; ++i
) {
1573 fprintf(out
, "%*s", indent
, "");
1574 fprintf(out
, "basic set %d:\n", i
);
1575 isl_basic_set_dump(set
->p
[i
], out
, indent
+4);
1579 void isl_map_dump(struct isl_map
*map
, FILE *out
, int indent
)
1584 fprintf(out
, "null map\n");
1588 fprintf(out
, "%*s", indent
, "");
1589 fprintf(out
, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
1590 "flags: %x, n_name: %d\n",
1591 map
->ref
, map
->n
, map
->dim
->nparam
, map
->dim
->n_in
,
1592 map
->dim
->n_out
, map
->flags
, map
->dim
->n_name
);
1593 for (i
= 0; i
< map
->n
; ++i
) {
1594 fprintf(out
, "%*s", indent
, "");
1595 fprintf(out
, "basic map %d:\n", i
);
1596 isl_basic_map_dump(map
->p
[i
], out
, indent
+4);
1600 struct isl_basic_map
*isl_basic_map_intersect_domain(
1601 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
1603 struct isl_basic_map
*bmap_domain
;
1604 struct isl_dim
*dim
;
1609 isl_assert(bset
->ctx
, isl_dim_match(bmap
->dim
, isl_dim_param
,
1610 bset
->dim
, isl_dim_param
), goto error
);
1612 if (isl_dim_size(bset
->dim
, isl_dim_set
) != 0)
1613 isl_assert(bset
->ctx
,
1614 isl_basic_map_compatible_domain(bmap
, bset
), goto error
);
1616 bmap
= isl_basic_map_cow(bmap
);
1617 bmap
= isl_basic_map_extend_dim(bmap
, isl_dim_copy(bmap
->dim
),
1618 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
1621 dim
= isl_dim_reverse(isl_dim_copy(bset
->dim
));
1622 bmap_domain
= isl_basic_map_from_basic_set(bset
, dim
);
1623 bmap
= add_constraints(bmap
, bmap_domain
, 0, 0);
1625 bmap
= isl_basic_map_simplify(bmap
);
1626 return isl_basic_map_finalize(bmap
);
1628 isl_basic_map_free(bmap
);
1629 isl_basic_set_free(bset
);
1633 struct isl_basic_map
*isl_basic_map_intersect_range(
1634 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
1636 struct isl_basic_map
*bmap_range
;
1641 isl_assert(bset
->ctx
, isl_dim_match(bmap
->dim
, isl_dim_param
,
1642 bset
->dim
, isl_dim_param
), goto error
);
1644 if (isl_dim_size(bset
->dim
, isl_dim_set
) != 0)
1645 isl_assert(bset
->ctx
,
1646 isl_basic_map_compatible_range(bmap
, bset
), goto error
);
1648 bmap
= isl_basic_map_cow(bmap
);
1649 bmap
= isl_basic_map_extend_dim(bmap
, isl_dim_copy(bmap
->dim
),
1650 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
1653 bmap_range
= isl_basic_map_from_basic_set(bset
, isl_dim_copy(bset
->dim
));
1654 bmap
= add_constraints(bmap
, bmap_range
, 0, 0);
1656 bmap
= isl_basic_map_simplify(bmap
);
1657 return isl_basic_map_finalize(bmap
);
1659 isl_basic_map_free(bmap
);
1660 isl_basic_set_free(bset
);
1664 static int basic_map_contains(struct isl_basic_map
*bmap
, struct isl_vec
*vec
)
1670 total
= 1 + isl_basic_map_total_dim(bmap
);
1671 if (total
!= vec
->size
)
1676 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
1677 isl_seq_inner_product(vec
->el
, bmap
->eq
[i
], total
, &s
);
1678 if (!isl_int_is_zero(s
)) {
1684 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
1685 isl_seq_inner_product(vec
->el
, bmap
->ineq
[i
], total
, &s
);
1686 if (isl_int_is_neg(s
)) {
1697 int isl_basic_set_contains(struct isl_basic_set
*bset
, struct isl_vec
*vec
)
1699 return basic_map_contains((struct isl_basic_map
*)bset
, vec
);
1702 struct isl_basic_map
*isl_basic_map_intersect(
1703 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
1705 struct isl_vec
*sample
= NULL
;
1707 if (!bmap1
|| !bmap2
)
1710 isl_assert(bmap1
->ctx
, isl_dim_match(bmap1
->dim
, isl_dim_param
,
1711 bmap2
->dim
, isl_dim_param
), goto error
);
1712 if (isl_dim_total(bmap1
->dim
) ==
1713 isl_dim_size(bmap1
->dim
, isl_dim_param
) &&
1714 isl_dim_total(bmap2
->dim
) !=
1715 isl_dim_size(bmap2
->dim
, isl_dim_param
))
1716 return isl_basic_map_intersect(bmap2
, bmap1
);
1718 if (isl_dim_total(bmap2
->dim
) !=
1719 isl_dim_size(bmap2
->dim
, isl_dim_param
))
1720 isl_assert(bmap1
->ctx
,
1721 isl_dim_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
1723 if (bmap1
->sample
&&
1724 basic_map_contains(bmap1
, bmap1
->sample
) > 0 &&
1725 basic_map_contains(bmap2
, bmap1
->sample
) > 0)
1726 sample
= isl_vec_copy(bmap1
->sample
);
1727 else if (bmap2
->sample
&&
1728 basic_map_contains(bmap1
, bmap2
->sample
) > 0 &&
1729 basic_map_contains(bmap2
, bmap2
->sample
) > 0)
1730 sample
= isl_vec_copy(bmap2
->sample
);
1732 bmap1
= isl_basic_map_cow(bmap1
);
1733 bmap1
= isl_basic_map_extend_dim(bmap1
, isl_dim_copy(bmap1
->dim
),
1734 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
1737 bmap1
= add_constraints(bmap1
, bmap2
, 0, 0);
1740 isl_vec_free(bmap1
->sample
);
1741 bmap1
->sample
= sample
;
1744 bmap1
= isl_basic_map_simplify(bmap1
);
1745 return isl_basic_map_finalize(bmap1
);
1748 isl_vec_free(sample
);
1749 isl_basic_map_free(bmap1
);
1750 isl_basic_map_free(bmap2
);
1754 struct isl_basic_set
*isl_basic_set_intersect(
1755 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
1757 return (struct isl_basic_set
*)
1758 isl_basic_map_intersect(
1759 (struct isl_basic_map
*)bset1
,
1760 (struct isl_basic_map
*)bset2
);
1763 struct isl_map
*isl_map_intersect(struct isl_map
*map1
, struct isl_map
*map2
)
1766 struct isl_map
*result
;
1772 isl_assert(map1
->ctx
, isl_dim_match(map1
->dim
, isl_dim_param
,
1773 map2
->dim
, isl_dim_param
), goto error
);
1774 if (isl_dim_total(map1
->dim
) ==
1775 isl_dim_size(map1
->dim
, isl_dim_param
) &&
1776 isl_dim_total(map2
->dim
) != isl_dim_size(map2
->dim
, isl_dim_param
))
1777 return isl_map_intersect(map2
, map1
);
1779 if (isl_dim_total(map2
->dim
) != isl_dim_size(map2
->dim
, isl_dim_param
))
1780 isl_assert(map1
->ctx
,
1781 isl_dim_equal(map1
->dim
, map2
->dim
), goto error
);
1783 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
1784 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
1785 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
1787 result
= isl_map_alloc_dim(isl_dim_copy(map1
->dim
),
1788 map1
->n
* map2
->n
, flags
);
1791 for (i
= 0; i
< map1
->n
; ++i
)
1792 for (j
= 0; j
< map2
->n
; ++j
) {
1793 struct isl_basic_map
*part
;
1794 part
= isl_basic_map_intersect(
1795 isl_basic_map_copy(map1
->p
[i
]),
1796 isl_basic_map_copy(map2
->p
[j
]));
1797 if (isl_basic_map_is_empty(part
))
1798 isl_basic_map_free(part
);
1800 result
= isl_map_add(result
, part
);
1813 struct isl_set
*isl_set_intersect(struct isl_set
*set1
, struct isl_set
*set2
)
1815 return (struct isl_set
*)
1816 isl_map_intersect((struct isl_map
*)set1
,
1817 (struct isl_map
*)set2
);
1820 struct isl_basic_map
*isl_basic_map_reverse(struct isl_basic_map
*bmap
)
1822 struct isl_dim
*dim
;
1823 struct isl_basic_set
*bset
;
1828 bmap
= isl_basic_map_cow(bmap
);
1831 dim
= isl_dim_reverse(isl_dim_copy(bmap
->dim
));
1832 in
= isl_basic_map_n_in(bmap
);
1833 bset
= isl_basic_set_from_basic_map(bmap
);
1834 bset
= isl_basic_set_swap_vars(bset
, in
);
1835 return isl_basic_map_from_basic_set(bset
, dim
);
1838 /* Turn final n dimensions into existentially quantified variables.
1840 struct isl_basic_set
*isl_basic_set_project_out(
1841 struct isl_basic_set
*bset
, unsigned n
, unsigned flags
)
1851 isl_assert(bset
->ctx
, n
<= isl_basic_set_n_dim(bset
), goto error
);
1856 bset
= isl_basic_set_cow(bset
);
1858 row_size
= 1 + isl_dim_total(bset
->dim
) + bset
->extra
;
1859 old
= bset
->block2
.data
;
1860 bset
->block2
= isl_blk_extend(bset
->ctx
, bset
->block2
,
1861 (bset
->extra
+ n
) * (1 + row_size
));
1862 if (!bset
->block2
.data
)
1864 new_div
= isl_alloc_array(ctx
, isl_int
*, bset
->extra
+ n
);
1867 for (i
= 0; i
< n
; ++i
) {
1868 new_div
[i
] = bset
->block2
.data
+
1869 (bset
->extra
+ i
) * (1 + row_size
);
1870 isl_seq_clr(new_div
[i
], 1 + row_size
);
1872 for (i
= 0; i
< bset
->extra
; ++i
)
1873 new_div
[n
+ i
] = bset
->block2
.data
+ (bset
->div
[i
] - old
);
1875 bset
->div
= new_div
;
1878 bset
->dim
= isl_dim_drop_outputs(bset
->dim
,
1879 isl_basic_set_n_dim(bset
) - n
, n
);
1882 bset
= isl_basic_set_simplify(bset
);
1883 return isl_basic_set_finalize(bset
);
1885 isl_basic_set_free(bset
);
1889 static struct isl_basic_map
*add_divs(struct isl_basic_map
*bmap
, unsigned n
)
1893 for (i
= 0; i
< n
; ++i
) {
1894 j
= isl_basic_map_alloc_div(bmap
);
1897 isl_seq_clr(bmap
->div
[j
], 1+1+isl_basic_map_total_dim(bmap
));
1901 isl_basic_map_free(bmap
);
1905 struct isl_basic_map
*isl_basic_map_apply_range(
1906 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
1908 struct isl_dim
*dim_result
= NULL
;
1909 struct isl_basic_map
*bmap
;
1910 unsigned n_in
, n_out
, n
, nparam
, total
, pos
;
1911 struct isl_dim_map
*dim_map1
, *dim_map2
;
1913 if (!bmap1
|| !bmap2
)
1916 dim_result
= isl_dim_join(isl_dim_copy(bmap1
->dim
),
1917 isl_dim_copy(bmap2
->dim
));
1919 n_in
= isl_basic_map_n_in(bmap1
);
1920 n_out
= isl_basic_map_n_out(bmap2
);
1921 n
= isl_basic_map_n_out(bmap1
);
1922 nparam
= isl_basic_map_n_param(bmap1
);
1924 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ n
;
1925 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
1926 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
1927 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
1928 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
1929 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
1930 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_in
);
1931 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_out
);
1932 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
1933 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
1934 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
1936 bmap
= isl_basic_map_alloc_dim(dim_result
,
1937 bmap1
->n_div
+ bmap2
->n_div
+ n
,
1938 bmap1
->n_eq
+ bmap2
->n_eq
,
1939 bmap1
->n_ineq
+ bmap2
->n_ineq
);
1940 bmap
= add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
1941 bmap
= add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
1942 bmap
= add_divs(bmap
, n
);
1943 bmap
= isl_basic_map_simplify(bmap
);
1944 return isl_basic_map_finalize(bmap
);
1946 isl_basic_map_free(bmap1
);
1947 isl_basic_map_free(bmap2
);
1951 struct isl_basic_set
*isl_basic_set_apply(
1952 struct isl_basic_set
*bset
, struct isl_basic_map
*bmap
)
1957 isl_assert(set
->ctx
, isl_basic_map_compatible_domain(bmap
, bset
),
1960 return (struct isl_basic_set
*)
1961 isl_basic_map_apply_range((struct isl_basic_map
*)bset
, bmap
);
1963 isl_basic_set_free(bset
);
1964 isl_basic_map_free(bmap
);
1968 struct isl_basic_map
*isl_basic_map_apply_domain(
1969 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
1971 if (!bmap1
|| !bmap2
)
1975 isl_basic_map_n_in(bmap1
) == isl_basic_map_n_in(bmap2
), goto error
);
1977 isl_basic_map_n_param(bmap1
) == isl_basic_map_n_param(bmap2
),
1980 bmap1
= isl_basic_map_reverse(bmap1
);
1981 bmap1
= isl_basic_map_apply_range(bmap1
, bmap2
);
1982 return isl_basic_map_reverse(bmap1
);
1984 isl_basic_map_free(bmap1
);
1985 isl_basic_map_free(bmap2
);
1989 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
1990 * A \cap B -> f(A) + f(B)
1992 struct isl_basic_map
*isl_basic_map_sum(
1993 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
1995 unsigned n_in
, n_out
, nparam
, total
, pos
;
1996 struct isl_basic_map
*bmap
= NULL
;
1997 struct isl_dim_map
*dim_map1
, *dim_map2
;
2000 if (!bmap1
|| !bmap2
)
2003 isl_assert(bmap1
->ctx
, isl_dim_equal(bmap1
->dim
, bmap2
->dim
),
2006 nparam
= isl_basic_map_n_param(bmap1
);
2007 n_in
= isl_basic_map_n_in(bmap1
);
2008 n_out
= isl_basic_map_n_out(bmap1
);
2010 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
;
2011 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
2012 dim_map2
= isl_dim_map_alloc(bmap2
->ctx
, total
);
2013 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
2014 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
);
2015 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
2016 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
2017 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_in
+ n_out
);
2018 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
2019 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
2020 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_out
);
2022 bmap
= isl_basic_map_alloc_dim(isl_dim_copy(bmap1
->dim
),
2023 bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
,
2024 bmap1
->n_eq
+ bmap2
->n_eq
+ n_out
,
2025 bmap1
->n_ineq
+ bmap2
->n_ineq
);
2026 for (i
= 0; i
< n_out
; ++i
) {
2027 int j
= isl_basic_map_alloc_equality(bmap
);
2030 isl_seq_clr(bmap
->eq
[j
], 1+total
);
2031 isl_int_set_si(bmap
->eq
[j
][1+nparam
+n_in
+i
], -1);
2032 isl_int_set_si(bmap
->eq
[j
][1+pos
+i
], 1);
2033 isl_int_set_si(bmap
->eq
[j
][1+pos
-n_out
+i
], 1);
2035 bmap
= add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
2036 bmap
= add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
2037 bmap
= add_divs(bmap
, 2 * n_out
);
2039 bmap
= isl_basic_map_simplify(bmap
);
2040 return isl_basic_map_finalize(bmap
);
2042 isl_basic_map_free(bmap
);
2043 isl_basic_map_free(bmap1
);
2044 isl_basic_map_free(bmap2
);
2048 /* Given a basic map A -> f(A), construct A -> -f(A).
2050 struct isl_basic_map
*isl_basic_map_neg(struct isl_basic_map
*bmap
)
2055 bmap
= isl_basic_map_cow(bmap
);
2059 n
= isl_basic_map_dim(bmap
, isl_dim_out
);
2060 off
= isl_basic_map_offset(bmap
, isl_dim_out
);
2061 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2062 for (j
= 0; j
< n
; ++j
)
2063 isl_int_neg(bmap
->eq
[i
][off
+j
], bmap
->eq
[i
][off
+j
]);
2064 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2065 for (j
= 0; j
< n
; ++j
)
2066 isl_int_neg(bmap
->ineq
[i
][off
+j
], bmap
->ineq
[i
][off
+j
]);
2067 for (i
= 0; i
< bmap
->n_div
; ++i
)
2068 for (j
= 0; j
< n
; ++j
)
2069 isl_int_neg(bmap
->div
[i
][1+off
+j
], bmap
->div
[i
][1+off
+j
]);
2070 return isl_basic_map_finalize(bmap
);
2073 /* Given a basic map A -> f(A) and an integer d, construct a basic map
2074 * A -> floor(f(A)/d).
2076 struct isl_basic_map
*isl_basic_map_floordiv(struct isl_basic_map
*bmap
,
2079 unsigned n_in
, n_out
, nparam
, total
, pos
;
2080 struct isl_basic_map
*result
= NULL
;
2081 struct isl_dim_map
*dim_map
;
2087 nparam
= isl_basic_map_n_param(bmap
);
2088 n_in
= isl_basic_map_n_in(bmap
);
2089 n_out
= isl_basic_map_n_out(bmap
);
2091 total
= nparam
+ n_in
+ n_out
+ bmap
->n_div
+ n_out
;
2092 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
2093 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_param
, pos
= 0);
2094 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_in
, pos
+= nparam
);
2095 isl_dim_map_div(dim_map
, bmap
, pos
+= n_in
+ n_out
);
2096 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_out
, pos
+= bmap
->n_div
);
2098 result
= isl_basic_map_alloc_dim(isl_dim_copy(bmap
->dim
),
2099 bmap
->n_div
+ n_out
,
2100 bmap
->n_eq
, bmap
->n_ineq
+ 2 * n_out
);
2101 result
= add_constraints_dim_map(result
, bmap
, dim_map
);
2102 result
= add_divs(result
, n_out
);
2103 for (i
= 0; i
< n_out
; ++i
) {
2105 j
= isl_basic_map_alloc_inequality(result
);
2108 isl_seq_clr(result
->ineq
[j
], 1+total
);
2109 isl_int_neg(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
2110 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], 1);
2111 j
= isl_basic_map_alloc_inequality(result
);
2114 isl_seq_clr(result
->ineq
[j
], 1+total
);
2115 isl_int_set(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
2116 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], -1);
2117 isl_int_sub_ui(result
->ineq
[j
][0], d
, 1);
2120 result
= isl_basic_map_simplify(result
);
2121 return isl_basic_map_finalize(result
);
2123 isl_basic_map_free(result
);
2127 static struct isl_basic_map
*var_equal(struct isl_basic_map
*bmap
, unsigned pos
)
2133 i
= isl_basic_map_alloc_equality(bmap
);
2136 nparam
= isl_basic_map_n_param(bmap
);
2137 n_in
= isl_basic_map_n_in(bmap
);
2138 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
2139 isl_int_set_si(bmap
->eq
[i
][1+nparam
+pos
], -1);
2140 isl_int_set_si(bmap
->eq
[i
][1+nparam
+n_in
+pos
], 1);
2141 return isl_basic_map_finalize(bmap
);
2143 isl_basic_map_free(bmap
);
2147 static struct isl_basic_map
*var_more(struct isl_basic_map
*bmap
, unsigned pos
)
2153 i
= isl_basic_map_alloc_inequality(bmap
);
2156 nparam
= isl_basic_map_n_param(bmap
);
2157 n_in
= isl_basic_map_n_in(bmap
);
2158 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
2159 isl_int_set_si(bmap
->ineq
[i
][0], -1);
2160 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
2161 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
2162 return isl_basic_map_finalize(bmap
);
2164 isl_basic_map_free(bmap
);
2168 static struct isl_basic_map
*var_less(struct isl_basic_map
*bmap
, unsigned pos
)
2174 i
= isl_basic_map_alloc_inequality(bmap
);
2177 nparam
= isl_basic_map_n_param(bmap
);
2178 n_in
= isl_basic_map_n_in(bmap
);
2179 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
2180 isl_int_set_si(bmap
->ineq
[i
][0], -1);
2181 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
2182 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
2183 return isl_basic_map_finalize(bmap
);
2185 isl_basic_map_free(bmap
);
2189 struct isl_basic_map
*isl_basic_map_equal(struct isl_dim
*dim
, unsigned n_equal
)
2192 struct isl_basic_map
*bmap
;
2193 bmap
= isl_basic_map_alloc_dim(dim
, 0, n_equal
, 0);
2196 for (i
= 0; i
< n_equal
&& bmap
; ++i
)
2197 bmap
= var_equal(bmap
, i
);
2198 return isl_basic_map_finalize(bmap
);
2201 struct isl_basic_map
*isl_basic_map_less_at(struct isl_dim
*dim
, unsigned pos
)
2204 struct isl_basic_map
*bmap
;
2205 bmap
= isl_basic_map_alloc_dim(dim
, 0, pos
, 1);
2208 for (i
= 0; i
< pos
&& bmap
; ++i
)
2209 bmap
= var_equal(bmap
, i
);
2211 bmap
= var_less(bmap
, pos
);
2212 return isl_basic_map_finalize(bmap
);
2215 struct isl_basic_map
*isl_basic_map_more_at(struct isl_dim
*dim
, unsigned pos
)
2218 struct isl_basic_map
*bmap
;
2219 bmap
= isl_basic_map_alloc_dim(dim
, 0, pos
, 1);
2222 for (i
= 0; i
< pos
&& bmap
; ++i
)
2223 bmap
= var_equal(bmap
, i
);
2225 bmap
= var_more(bmap
, pos
);
2226 return isl_basic_map_finalize(bmap
);
2229 struct isl_basic_map
*isl_basic_map_from_basic_set(
2230 struct isl_basic_set
*bset
, struct isl_dim
*dim
)
2232 struct isl_basic_map
*bmap
;
2234 bset
= isl_basic_set_cow(bset
);
2238 isl_assert(bset
->ctx
, isl_dim_compatible(bset
->dim
, dim
), goto error
);
2239 isl_dim_free(bset
->dim
);
2240 bmap
= (struct isl_basic_map
*) bset
;
2242 return isl_basic_map_finalize(bmap
);
2244 isl_basic_set_free(bset
);
2249 struct isl_basic_set
*isl_basic_set_from_basic_map(struct isl_basic_map
*bmap
)
2253 if (bmap
->dim
->n_in
== 0)
2254 return (struct isl_basic_set
*)bmap
;
2255 bmap
= isl_basic_map_cow(bmap
);
2258 bmap
->dim
= isl_dim_cow(bmap
->dim
);
2261 bmap
->dim
->n_out
+= bmap
->dim
->n_in
;
2262 bmap
->dim
->n_in
= 0;
2263 bmap
= isl_basic_map_finalize(bmap
);
2264 return (struct isl_basic_set
*)bmap
;
2266 isl_basic_map_free(bmap
);
2270 /* For a div d = floor(f/m), add the constraints
2273 * -(f-(n-1)) + m d >= 0
2275 * Note that the second constraint is the negation of
2279 static int add_div_constraints(struct isl_basic_map
*bmap
, unsigned div
)
2282 unsigned total
= isl_basic_map_total_dim(bmap
);
2283 unsigned div_pos
= 1 + total
- bmap
->n_div
+ div
;
2285 i
= isl_basic_map_alloc_inequality(bmap
);
2288 isl_seq_cpy(bmap
->ineq
[i
], bmap
->div
[div
]+1, 1+total
);
2289 isl_int_neg(bmap
->ineq
[i
][div_pos
], bmap
->div
[div
][0]);
2291 j
= isl_basic_map_alloc_inequality(bmap
);
2294 isl_seq_neg(bmap
->ineq
[j
], bmap
->ineq
[i
], 1 + total
);
2295 isl_int_add(bmap
->ineq
[j
][0], bmap
->ineq
[j
][0], bmap
->ineq
[j
][div_pos
]);
2296 isl_int_sub_ui(bmap
->ineq
[j
][0], bmap
->ineq
[j
][0], 1);
2300 struct isl_basic_set
*isl_basic_map_underlying_set(
2301 struct isl_basic_map
*bmap
)
2305 if (bmap
->dim
->nparam
== 0 && bmap
->dim
->n_in
== 0 && bmap
->n_div
== 0)
2306 return (struct isl_basic_set
*)bmap
;
2307 bmap
= isl_basic_map_cow(bmap
);
2310 bmap
->dim
= isl_dim_underlying(bmap
->dim
, bmap
->n_div
);
2313 bmap
->extra
-= bmap
->n_div
;
2315 bmap
= isl_basic_map_finalize(bmap
);
2316 return (struct isl_basic_set
*)bmap
;
2321 struct isl_basic_map
*isl_basic_map_overlying_set(
2322 struct isl_basic_set
*bset
, struct isl_basic_map
*like
)
2324 struct isl_basic_map
*bmap
;
2325 struct isl_ctx
*ctx
;
2332 isl_assert(ctx
, bset
->n_div
== 0, goto error
);
2333 isl_assert(ctx
, isl_basic_set_n_param(bset
) == 0, goto error
);
2334 isl_assert(ctx
, bset
->dim
->n_out
== isl_basic_map_total_dim(like
),
2336 if (isl_dim_equal(bset
->dim
, like
->dim
) && like
->n_div
== 0) {
2337 isl_basic_map_free(like
);
2338 return (struct isl_basic_map
*)bset
;
2340 bset
= isl_basic_set_cow(bset
);
2343 total
= bset
->dim
->n_out
+ bset
->extra
;
2344 bmap
= (struct isl_basic_map
*)bset
;
2345 isl_dim_free(bmap
->dim
);
2346 bmap
->dim
= isl_dim_copy(like
->dim
);
2349 bmap
->n_div
= like
->n_div
;
2350 bmap
->extra
+= like
->n_div
;
2353 ltotal
= total
- bmap
->extra
+ like
->extra
;
2356 bmap
->block2
= isl_blk_extend(ctx
, bmap
->block2
,
2357 bmap
->extra
* (1 + 1 + total
));
2358 if (isl_blk_is_error(bmap
->block2
))
2360 bmap
->div
= isl_realloc_array(ctx
, bmap
->div
, isl_int
*,
2364 for (i
= 0; i
< bmap
->extra
; ++i
)
2365 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + 1 + total
);
2366 for (i
= 0; i
< like
->n_div
; ++i
) {
2367 isl_seq_cpy(bmap
->div
[i
], like
->div
[i
], 1 + 1 + ltotal
);
2368 isl_seq_clr(bmap
->div
[i
]+1+1+ltotal
, total
- ltotal
);
2370 bmap
= isl_basic_map_extend_constraints(bmap
,
2371 0, 2 * like
->n_div
);
2372 for (i
= 0; i
< like
->n_div
; ++i
) {
2373 if (isl_int_is_zero(bmap
->div
[i
][0]))
2375 if (add_div_constraints(bmap
, i
) < 0)
2379 isl_basic_map_free(like
);
2380 bmap
= isl_basic_map_simplify(bmap
);
2381 bmap
= isl_basic_map_finalize(bmap
);
2384 isl_basic_map_free(like
);
2385 isl_basic_set_free(bset
);
2389 struct isl_basic_set
*isl_basic_set_from_underlying_set(
2390 struct isl_basic_set
*bset
, struct isl_basic_set
*like
)
2392 return (struct isl_basic_set
*)
2393 isl_basic_map_overlying_set(bset
, (struct isl_basic_map
*)like
);
2396 struct isl_set
*isl_set_from_underlying_set(
2397 struct isl_set
*set
, struct isl_basic_set
*like
)
2403 isl_assert(set
->ctx
, set
->dim
->n_out
== isl_basic_set_total_dim(like
),
2405 if (isl_dim_equal(set
->dim
, like
->dim
) && like
->n_div
== 0) {
2406 isl_basic_set_free(like
);
2409 set
= isl_set_cow(set
);
2412 for (i
= 0; i
< set
->n
; ++i
) {
2413 set
->p
[i
] = isl_basic_set_from_underlying_set(set
->p
[i
],
2414 isl_basic_set_copy(like
));
2418 isl_dim_free(set
->dim
);
2419 set
->dim
= isl_dim_copy(like
->dim
);
2422 isl_basic_set_free(like
);
2425 isl_basic_set_free(like
);
2430 struct isl_set
*isl_map_underlying_set(struct isl_map
*map
)
2434 map
= isl_map_cow(map
);
2437 map
->dim
= isl_dim_cow(map
->dim
);
2441 for (i
= 1; i
< map
->n
; ++i
)
2442 isl_assert(map
->ctx
, map
->p
[0]->n_div
== map
->p
[i
]->n_div
,
2444 for (i
= 0; i
< map
->n
; ++i
) {
2445 map
->p
[i
] = (struct isl_basic_map
*)
2446 isl_basic_map_underlying_set(map
->p
[i
]);
2451 map
->dim
= isl_dim_underlying(map
->dim
, 0);
2453 isl_dim_free(map
->dim
);
2454 map
->dim
= isl_dim_copy(map
->p
[0]->dim
);
2458 return (struct isl_set
*)map
;
2464 struct isl_set
*isl_set_to_underlying_set(struct isl_set
*set
)
2466 return (struct isl_set
*)isl_map_underlying_set((struct isl_map
*)set
);
2469 struct isl_basic_set
*isl_basic_map_domain(struct isl_basic_map
*bmap
)
2471 struct isl_basic_set
*domain
;
2475 n_out
= isl_basic_map_n_out(bmap
);
2476 domain
= isl_basic_set_from_basic_map(bmap
);
2477 return isl_basic_set_project_out(domain
, n_out
, 0);
2480 struct isl_basic_set
*isl_basic_map_range(struct isl_basic_map
*bmap
)
2482 return isl_basic_map_domain(isl_basic_map_reverse(bmap
));
2485 struct isl_set
*isl_map_range(struct isl_map
*map
)
2488 struct isl_set
*set
;
2492 map
= isl_map_cow(map
);
2496 set
= (struct isl_set
*) map
;
2497 if (set
->dim
->n_in
!= 0) {
2498 set
->dim
= isl_dim_drop_inputs(set
->dim
, 0, set
->dim
->n_in
);
2502 for (i
= 0; i
< map
->n
; ++i
) {
2503 set
->p
[i
] = isl_basic_map_range(map
->p
[i
]);
2507 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
2508 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
2515 struct isl_map
*isl_map_from_set(struct isl_set
*set
, struct isl_dim
*dim
)
2518 struct isl_map
*map
= NULL
;
2520 set
= isl_set_cow(set
);
2523 isl_assert(set
->ctx
, isl_dim_compatible(set
->dim
, dim
), goto error
);
2524 map
= (struct isl_map
*)set
;
2525 for (i
= 0; i
< set
->n
; ++i
) {
2526 map
->p
[i
] = isl_basic_map_from_basic_set(
2527 set
->p
[i
], isl_dim_copy(dim
));
2531 isl_dim_free(map
->dim
);
2540 struct isl_map
*isl_map_from_range(struct isl_set
*set
)
2542 return (struct isl_map
*)set
;
2545 struct isl_set
*isl_set_from_map(struct isl_map
*map
)
2548 struct isl_set
*set
= NULL
;
2552 map
= isl_map_cow(map
);
2555 map
->dim
= isl_dim_cow(map
->dim
);
2558 map
->dim
->n_out
+= map
->dim
->n_in
;
2560 set
= (struct isl_set
*)map
;
2561 for (i
= 0; i
< map
->n
; ++i
) {
2562 set
->p
[i
] = isl_basic_set_from_basic_map(map
->p
[i
]);
2572 struct isl_map
*isl_map_alloc_dim(struct isl_dim
*dim
, int n
, unsigned flags
)
2574 struct isl_map
*map
;
2578 isl_assert(dim
->ctx
, n
>= 0, return NULL
);
2579 map
= isl_alloc(dim
->ctx
, struct isl_map
,
2580 sizeof(struct isl_map
) +
2581 n
* sizeof(struct isl_basic_map
*));
2585 map
->ctx
= dim
->ctx
;
2586 isl_ctx_ref(map
->ctx
);
2598 struct isl_map
*isl_map_alloc(struct isl_ctx
*ctx
,
2599 unsigned nparam
, unsigned in
, unsigned out
, int n
,
2602 struct isl_map
*map
;
2603 struct isl_dim
*dims
;
2605 dims
= isl_dim_alloc(ctx
, nparam
, in
, out
);
2609 map
= isl_map_alloc_dim(dims
, n
, flags
);
2613 struct isl_basic_map
*isl_basic_map_empty(struct isl_ctx
*ctx
,
2614 unsigned nparam
, unsigned in
, unsigned out
)
2616 struct isl_basic_map
*bmap
;
2617 bmap
= isl_basic_map_alloc(ctx
, nparam
, in
, out
, 0, 1, 0);
2618 bmap
= isl_basic_map_set_to_empty(bmap
);
2622 struct isl_basic_set
*isl_basic_set_empty(struct isl_dim
*dim
)
2624 struct isl_basic_set
*bset
;
2625 bset
= isl_basic_set_alloc_dim(dim
, 0, 1, 0);
2626 bset
= isl_basic_set_set_to_empty(bset
);
2630 struct isl_basic_map
*isl_basic_map_empty_like(struct isl_basic_map
*model
)
2632 struct isl_basic_map
*bmap
;
2635 bmap
= isl_basic_map_alloc_dim(isl_dim_copy(model
->dim
), 0, 1, 0);
2636 bmap
= isl_basic_map_set_to_empty(bmap
);
2640 struct isl_basic_map
*isl_basic_map_empty_like_map(struct isl_map
*model
)
2642 struct isl_basic_map
*bmap
;
2645 bmap
= isl_basic_map_alloc_dim(isl_dim_copy(model
->dim
), 0, 1, 0);
2646 bmap
= isl_basic_map_set_to_empty(bmap
);
2650 struct isl_basic_set
*isl_basic_set_empty_like(struct isl_basic_set
*model
)
2652 struct isl_basic_set
*bset
;
2655 bset
= isl_basic_set_alloc_dim(isl_dim_copy(model
->dim
), 0, 1, 0);
2656 bset
= isl_basic_set_set_to_empty(bset
);
2660 struct isl_basic_map
*isl_basic_map_universe(struct isl_dim
*dim
)
2662 struct isl_basic_map
*bmap
;
2663 bmap
= isl_basic_map_alloc_dim(dim
, 0, 0, 0);
2667 struct isl_basic_set
*isl_basic_set_universe(struct isl_dim
*dim
)
2669 struct isl_basic_set
*bset
;
2670 bset
= isl_basic_set_alloc_dim(dim
, 0, 0, 0);
2674 struct isl_basic_set
*isl_basic_set_universe_like(struct isl_basic_set
*model
)
2678 return isl_basic_set_alloc_dim(isl_dim_copy(model
->dim
), 0, 0, 0);
2681 struct isl_map
*isl_map_empty(struct isl_dim
*dim
)
2683 return isl_map_alloc_dim(dim
, 0, ISL_MAP_DISJOINT
);
2686 struct isl_map
*isl_map_empty_like(struct isl_map
*model
)
2690 return isl_map_alloc_dim(isl_dim_copy(model
->dim
), 0, ISL_MAP_DISJOINT
);
2693 struct isl_map
*isl_map_empty_like_basic_map(struct isl_basic_map
*model
)
2697 return isl_map_alloc_dim(isl_dim_copy(model
->dim
), 0, ISL_MAP_DISJOINT
);
2700 struct isl_set
*isl_set_empty(struct isl_dim
*dim
)
2702 return isl_set_alloc_dim(dim
, 0, ISL_MAP_DISJOINT
);
2705 struct isl_set
*isl_set_empty_like(struct isl_set
*model
)
2709 return isl_set_empty(isl_dim_copy(model
->dim
));
2712 struct isl_map
*isl_map_universe(struct isl_dim
*dim
)
2714 struct isl_map
*map
;
2717 map
= isl_map_alloc_dim(isl_dim_copy(dim
), 1, ISL_MAP_DISJOINT
);
2718 map
= isl_map_add(map
, isl_basic_map_universe(dim
));
2722 struct isl_set
*isl_set_universe(struct isl_dim
*dim
)
2724 struct isl_set
*set
;
2727 set
= isl_set_alloc_dim(isl_dim_copy(dim
), 1, ISL_MAP_DISJOINT
);
2728 set
= isl_set_add(set
, isl_basic_set_universe(dim
));
2732 struct isl_map
*isl_map_dup(struct isl_map
*map
)
2735 struct isl_map
*dup
;
2739 dup
= isl_map_alloc_dim(isl_dim_copy(map
->dim
), map
->n
, map
->flags
);
2740 for (i
= 0; i
< map
->n
; ++i
)
2741 dup
= isl_map_add(dup
, isl_basic_map_copy(map
->p
[i
]));
2745 struct isl_map
*isl_map_add(struct isl_map
*map
, struct isl_basic_map
*bmap
)
2749 isl_assert(map
->ctx
, isl_dim_equal(map
->dim
, bmap
->dim
), goto error
);
2750 isl_assert(map
->ctx
, map
->n
< map
->size
, goto error
);
2751 map
->p
[map
->n
] = bmap
;
2753 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
2759 isl_basic_map_free(bmap
);
2763 void isl_map_free(struct isl_map
*map
)
2773 isl_ctx_deref(map
->ctx
);
2774 for (i
= 0; i
< map
->n
; ++i
)
2775 isl_basic_map_free(map
->p
[i
]);
2776 isl_dim_free(map
->dim
);
2780 struct isl_map
*isl_map_extend(struct isl_map
*base
,
2781 unsigned nparam
, unsigned n_in
, unsigned n_out
)
2785 base
= isl_map_cow(base
);
2789 base
->dim
= isl_dim_extend(base
->dim
, nparam
, n_in
, n_out
);
2792 for (i
= 0; i
< base
->n
; ++i
) {
2793 base
->p
[i
] = isl_basic_map_extend_dim(base
->p
[i
],
2794 isl_dim_copy(base
->dim
), 0, 0, 0);
2804 struct isl_set
*isl_set_extend(struct isl_set
*base
,
2805 unsigned nparam
, unsigned dim
)
2807 return (struct isl_set
*)isl_map_extend((struct isl_map
*)base
,
2811 static struct isl_basic_map
*isl_basic_map_fix_pos(struct isl_basic_map
*bmap
,
2812 unsigned pos
, int value
)
2816 bmap
= isl_basic_map_cow(bmap
);
2817 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
2818 j
= isl_basic_map_alloc_equality(bmap
);
2821 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
2822 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
2823 isl_int_set_si(bmap
->eq
[j
][0], value
);
2824 bmap
= isl_basic_map_simplify(bmap
);
2825 return isl_basic_map_finalize(bmap
);
2827 isl_basic_map_free(bmap
);
2831 struct isl_basic_map
*isl_basic_map_fix_si(struct isl_basic_map
*bmap
,
2832 enum isl_dim_type type
, unsigned pos
, int value
)
2836 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
2837 return isl_basic_map_fix_pos(bmap
, isl_basic_map_offset(bmap
, type
) + pos
,
2840 isl_basic_map_free(bmap
);
2844 struct isl_basic_set
*isl_basic_set_fix_si(struct isl_basic_set
*bset
,
2845 enum isl_dim_type type
, unsigned pos
, int value
)
2847 return (struct isl_basic_set
*)
2848 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
2852 struct isl_basic_map
*isl_basic_map_fix_input_si(struct isl_basic_map
*bmap
,
2853 unsigned input
, int value
)
2855 return isl_basic_map_fix_si(bmap
, isl_dim_in
, input
, value
);
2858 struct isl_basic_set
*isl_basic_set_fix_dim_si(struct isl_basic_set
*bset
,
2859 unsigned dim
, int value
)
2861 return (struct isl_basic_set
*)
2862 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
2863 isl_dim_set
, dim
, value
);
2866 struct isl_map
*isl_map_fix_si(struct isl_map
*map
,
2867 enum isl_dim_type type
, unsigned pos
, int value
)
2871 map
= isl_map_cow(map
);
2875 isl_assert(ctx
, pos
< isl_map_dim(map
, type
), goto error
);
2876 for (i
= 0; i
< map
->n
; ++i
) {
2877 map
->p
[i
] = isl_basic_map_fix_si(map
->p
[i
], type
, pos
, value
);
2881 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
2888 struct isl_map
*isl_map_fix_input_si(struct isl_map
*map
,
2889 unsigned input
, int value
)
2891 return isl_map_fix_si(map
, isl_dim_in
, input
, value
);
2894 struct isl_set
*isl_set_fix_dim_si(struct isl_set
*set
, unsigned dim
, int value
)
2896 return (struct isl_set
*)
2897 isl_map_fix_si((struct isl_map
*)set
, isl_dim_set
, dim
, value
);
2900 struct isl_basic_set
*isl_basic_set_lower_bound_dim(struct isl_basic_set
*bset
,
2901 unsigned dim
, isl_int value
)
2906 bset
= isl_basic_set_cow(bset
);
2907 bset
= isl_basic_set_extend_constraints(bset
, 0, 1);
2908 j
= isl_basic_set_alloc_inequality(bset
);
2911 isl_seq_clr(bset
->ineq
[j
], 1 + isl_basic_set_total_dim(bset
));
2912 isl_int_set_si(bset
->ineq
[j
][1 + isl_basic_set_n_param(bset
) + dim
], 1);
2913 isl_int_neg(bset
->ineq
[j
][0], value
);
2914 bset
= isl_basic_set_simplify(bset
);
2915 return isl_basic_set_finalize(bset
);
2917 isl_basic_set_free(bset
);
2921 struct isl_set
*isl_set_lower_bound_dim(struct isl_set
*set
, unsigned dim
,
2926 set
= isl_set_cow(set
);
2930 isl_assert(set
->ctx
, dim
< isl_set_n_dim(set
), goto error
);
2931 for (i
= 0; i
< set
->n
; ++i
) {
2932 set
->p
[i
] = isl_basic_set_lower_bound_dim(set
->p
[i
], dim
, value
);
2942 struct isl_map
*isl_map_reverse(struct isl_map
*map
)
2947 map
= isl_map_cow(map
);
2951 map
->dim
= isl_dim_reverse(map
->dim
);
2954 for (i
= 0; i
< map
->n
; ++i
) {
2955 map
->p
[i
] = isl_basic_map_reverse(map
->p
[i
]);
2959 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
2966 static struct isl_map
*isl_basic_map_partial_lexopt(
2967 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
2968 struct isl_set
**empty
, int max
)
2970 return isl_pip_basic_map_lexopt(bmap
, dom
, empty
, max
);
2973 struct isl_map
*isl_basic_map_partial_lexmax(
2974 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
2975 struct isl_set
**empty
)
2977 return isl_basic_map_partial_lexopt(bmap
, dom
, empty
, 1);
2980 struct isl_map
*isl_basic_map_partial_lexmin(
2981 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
2982 struct isl_set
**empty
)
2984 return isl_basic_map_partial_lexopt(bmap
, dom
, empty
, 0);
2987 struct isl_set
*isl_basic_set_partial_lexmin(
2988 struct isl_basic_set
*bset
, struct isl_basic_set
*dom
,
2989 struct isl_set
**empty
)
2991 return (struct isl_set
*)
2992 isl_basic_map_partial_lexmin((struct isl_basic_map
*)bset
,
2996 struct isl_set
*isl_basic_set_partial_lexmax(
2997 struct isl_basic_set
*bset
, struct isl_basic_set
*dom
,
2998 struct isl_set
**empty
)
3000 return (struct isl_set
*)
3001 isl_basic_map_partial_lexmax((struct isl_basic_map
*)bset
,
3005 struct isl_set
*isl_basic_set_lexmin(struct isl_basic_set
*bset
)
3007 struct isl_basic_map
*bmap
= NULL
;
3008 struct isl_basic_set
*dom
= NULL
;
3009 struct isl_map
*min
;
3010 struct isl_dim
*param_dim
;
3014 bmap
= isl_basic_map_from_basic_set(bset
, isl_dim_copy(bset
->dim
));
3017 param_dim
= isl_dim_domain(isl_dim_copy(bmap
->dim
));
3018 dom
= isl_basic_set_universe(param_dim
);
3021 min
= isl_basic_map_partial_lexmin(bmap
, dom
, NULL
);
3022 return isl_map_range(min
);
3024 isl_basic_map_free(bmap
);
3028 /* If bmap contains any unknown divs, then compute explicit
3029 * expressions for them. However, this computation may be
3030 * quite expensive, so first try to remove divs that aren't
3033 struct isl_map
*isl_basic_map_compute_divs(struct isl_basic_map
*bmap
)
3040 off
= isl_dim_total(bmap
->dim
);
3041 for (i
= 0; i
< bmap
->n_div
; ++i
) {
3042 if (isl_int_is_zero(bmap
->div
[i
][0]))
3044 isl_assert(bmap
->ctx
, isl_int_is_zero(bmap
->div
[i
][1+1+off
+i
]),
3047 if (i
== bmap
->n_div
)
3048 return isl_map_from_basic_map(bmap
);
3049 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
3052 for (i
= 0; i
< bmap
->n_div
; ++i
)
3053 if (isl_int_is_zero(bmap
->div
[i
][0]))
3055 if (i
== bmap
->n_div
)
3056 return isl_map_from_basic_map(bmap
);
3057 struct isl_map
*map
= isl_pip_basic_map_compute_divs(bmap
);
3060 isl_basic_map_free(bmap
);
3064 struct isl_map
*isl_map_compute_divs(struct isl_map
*map
)
3067 struct isl_map
*res
;
3073 res
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[0]));
3074 for (i
= 1 ; i
< map
->n
; ++i
) {
3076 r2
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[i
]));
3077 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
3078 res
= isl_map_union_disjoint(res
, r2
);
3080 res
= isl_map_union(res
, r2
);
3087 struct isl_set
*isl_basic_set_compute_divs(struct isl_basic_set
*bset
)
3089 return (struct isl_set
*)
3090 isl_basic_map_compute_divs((struct isl_basic_map
*)bset
);
3093 struct isl_set
*isl_set_compute_divs(struct isl_set
*set
)
3095 return (struct isl_set
*)
3096 isl_map_compute_divs((struct isl_map
*)set
);
3099 struct isl_set
*isl_map_domain(struct isl_map
*map
)
3102 struct isl_set
*set
;
3107 map
= isl_map_cow(map
);
3111 set
= (struct isl_set
*)map
;
3112 set
->dim
= isl_dim_domain(set
->dim
);
3115 for (i
= 0; i
< map
->n
; ++i
) {
3116 set
->p
[i
] = isl_basic_map_domain(map
->p
[i
]);
3120 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
3121 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
3128 struct isl_map
*isl_map_union_disjoint(
3129 struct isl_map
*map1
, struct isl_map
*map2
)
3133 struct isl_map
*map
= NULL
;
3147 isl_assert(map1
->ctx
, isl_dim_equal(map1
->dim
, map2
->dim
), goto error
);
3149 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
3150 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
3151 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
3153 map
= isl_map_alloc_dim(isl_dim_copy(map1
->dim
),
3154 map1
->n
+ map2
->n
, flags
);
3157 for (i
= 0; i
< map1
->n
; ++i
) {
3158 map
= isl_map_add(map
,
3159 isl_basic_map_copy(map1
->p
[i
]));
3163 for (i
= 0; i
< map2
->n
; ++i
) {
3164 map
= isl_map_add(map
,
3165 isl_basic_map_copy(map2
->p
[i
]));
3179 struct isl_map
*isl_map_union(struct isl_map
*map1
, struct isl_map
*map2
)
3181 map1
= isl_map_union_disjoint(map1
, map2
);
3185 ISL_F_CLR(map1
, ISL_MAP_DISJOINT
);
3189 struct isl_set
*isl_set_union_disjoint(
3190 struct isl_set
*set1
, struct isl_set
*set2
)
3192 return (struct isl_set
*)
3193 isl_map_union_disjoint(
3194 (struct isl_map
*)set1
, (struct isl_map
*)set2
);
3197 struct isl_set
*isl_set_union(struct isl_set
*set1
, struct isl_set
*set2
)
3199 return (struct isl_set
*)
3200 isl_map_union((struct isl_map
*)set1
, (struct isl_map
*)set2
);
3203 struct isl_map
*isl_map_intersect_range(
3204 struct isl_map
*map
, struct isl_set
*set
)
3207 struct isl_map
*result
;
3213 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
) &&
3214 ISL_F_ISSET(set
, ISL_MAP_DISJOINT
))
3215 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
3217 result
= isl_map_alloc_dim(isl_dim_copy(map
->dim
),
3218 map
->n
* set
->n
, flags
);
3221 for (i
= 0; i
< map
->n
; ++i
)
3222 for (j
= 0; j
< set
->n
; ++j
) {
3223 result
= isl_map_add(result
,
3224 isl_basic_map_intersect_range(
3225 isl_basic_map_copy(map
->p
[i
]),
3226 isl_basic_set_copy(set
->p
[j
])));
3239 struct isl_map
*isl_map_intersect_domain(
3240 struct isl_map
*map
, struct isl_set
*set
)
3242 return isl_map_reverse(
3243 isl_map_intersect_range(isl_map_reverse(map
), set
));
3246 struct isl_map
*isl_map_apply_domain(
3247 struct isl_map
*map1
, struct isl_map
*map2
)
3251 map1
= isl_map_reverse(map1
);
3252 map1
= isl_map_apply_range(map1
, map2
);
3253 return isl_map_reverse(map1
);
3260 struct isl_map
*isl_map_apply_range(
3261 struct isl_map
*map1
, struct isl_map
*map2
)
3263 struct isl_dim
*dim_result
;
3264 struct isl_map
*result
;
3273 dim_result
= isl_dim_join(isl_dim_copy(map1
->dim
),
3274 isl_dim_copy(map2
->dim
));
3276 result
= isl_map_alloc_dim(dim_result
, map1
->n
* map2
->n
, 0);
3279 for (i
= 0; i
< map1
->n
; ++i
)
3280 for (j
= 0; j
< map2
->n
; ++j
) {
3281 result
= isl_map_add(result
,
3282 isl_basic_map_apply_range(
3283 isl_basic_map_copy(map1
->p
[i
]),
3284 isl_basic_map_copy(map2
->p
[j
])));
3290 if (result
&& result
->n
<= 1)
3291 ISL_F_SET(result
, ISL_MAP_DISJOINT
);
3300 * returns range - domain
3302 struct isl_basic_set
*isl_basic_map_deltas(struct isl_basic_map
*bmap
)
3304 struct isl_basic_set
*bset
;
3311 dim
= isl_basic_map_n_in(bmap
);
3312 nparam
= isl_basic_map_n_param(bmap
);
3313 isl_assert(bmap
->ctx
, dim
== isl_basic_map_n_out(bmap
), goto error
);
3314 bset
= isl_basic_set_from_basic_map(bmap
);
3315 bset
= isl_basic_set_cow(bset
);
3316 bset
= isl_basic_set_extend(bset
, nparam
, 3*dim
, 0, dim
, 0);
3317 bset
= isl_basic_set_swap_vars(bset
, 2*dim
);
3318 for (i
= 0; i
< dim
; ++i
) {
3319 int j
= isl_basic_map_alloc_equality(
3320 (struct isl_basic_map
*)bset
);
3323 isl_seq_clr(bset
->eq
[j
], 1 + isl_basic_set_total_dim(bset
));
3324 isl_int_set_si(bset
->eq
[j
][1+nparam
+i
], 1);
3325 isl_int_set_si(bset
->eq
[j
][1+nparam
+dim
+i
], 1);
3326 isl_int_set_si(bset
->eq
[j
][1+nparam
+2*dim
+i
], -1);
3328 return isl_basic_set_project_out(bset
, 2*dim
, 0);
3330 isl_basic_map_free(bmap
);
3335 * returns range - domain
3337 struct isl_set
*isl_map_deltas(struct isl_map
*map
)
3340 struct isl_set
*result
;
3345 isl_assert(map
->ctx
, isl_map_n_in(map
) == isl_map_n_out(map
), goto error
);
3346 result
= isl_set_alloc(map
->ctx
, isl_map_n_param(map
),
3347 isl_map_n_in(map
), map
->n
, map
->flags
);
3350 for (i
= 0; i
< map
->n
; ++i
)
3351 result
= isl_set_add(result
,
3352 isl_basic_map_deltas(isl_basic_map_copy(map
->p
[i
])));
3360 static struct isl_basic_map
*basic_map_identity(struct isl_dim
*dims
)
3362 struct isl_basic_map
*bmap
;
3370 nparam
= dims
->nparam
;
3372 bmap
= isl_basic_map_alloc_dim(dims
, 0, dim
, 0);
3376 for (i
= 0; i
< dim
; ++i
) {
3377 int j
= isl_basic_map_alloc_equality(bmap
);
3380 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
3381 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
3382 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], -1);
3384 return isl_basic_map_finalize(bmap
);
3386 isl_basic_map_free(bmap
);
3390 struct isl_basic_map
*isl_basic_map_identity(struct isl_dim
*set_dim
)
3392 struct isl_dim
*dim
= isl_dim_map(set_dim
);
3395 return basic_map_identity(dim
);
3398 struct isl_basic_map
*isl_basic_map_identity_like(struct isl_basic_map
*model
)
3400 if (!model
|| !model
->dim
)
3402 isl_assert(model
->ctx
,
3403 model
->dim
->n_in
== model
->dim
->n_out
, return NULL
);
3404 return basic_map_identity(isl_dim_copy(model
->dim
));
3407 static struct isl_map
*map_identity(struct isl_dim
*dim
)
3409 struct isl_map
*map
= isl_map_alloc_dim(dim
, 1, ISL_MAP_DISJOINT
);
3410 return isl_map_add(map
, basic_map_identity(isl_dim_copy(dim
)));
3413 struct isl_map
*isl_map_identity(struct isl_dim
*set_dim
)
3415 struct isl_dim
*dim
= isl_dim_map(set_dim
);
3418 return map_identity(dim
);
3421 struct isl_map
*isl_map_identity_like(struct isl_basic_map
*model
)
3423 if (!model
|| !model
->dim
)
3425 isl_assert(model
->ctx
,
3426 model
->dim
->n_in
== model
->dim
->n_out
, return NULL
);
3427 return map_identity(isl_dim_copy(model
->dim
));
3430 /* Construct a basic set with all set dimensions having only non-negative
3433 struct isl_basic_set
*isl_basic_set_positive_orthant(struct isl_dim
*dims
)
3438 struct isl_basic_set
*bset
;
3442 nparam
= dims
->nparam
;
3444 bset
= isl_basic_set_alloc_dim(dims
, 0, 0, dim
);
3447 for (i
= 0; i
< dim
; ++i
) {
3448 int k
= isl_basic_set_alloc_inequality(bset
);
3451 isl_seq_clr(bset
->ineq
[k
], 1 + isl_basic_set_total_dim(bset
));
3452 isl_int_set_si(bset
->ineq
[k
][1 + nparam
+ i
], 1);
3456 isl_basic_set_free(bset
);
3460 int isl_set_is_equal(struct isl_set
*set1
, struct isl_set
*set2
)
3462 return isl_map_is_equal((struct isl_map
*)set1
, (struct isl_map
*)set2
);
3465 int isl_set_is_subset(struct isl_set
*set1
, struct isl_set
*set2
)
3467 return isl_map_is_subset(
3468 (struct isl_map
*)set1
, (struct isl_map
*)set2
);
3471 int isl_basic_map_is_subset(
3472 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3475 struct isl_map
*map1
;
3476 struct isl_map
*map2
;
3478 if (!bmap1
|| !bmap2
)
3481 map1
= isl_map_from_basic_map(isl_basic_map_copy(bmap1
));
3482 map2
= isl_map_from_basic_map(isl_basic_map_copy(bmap2
));
3484 is_subset
= isl_map_is_subset(map1
, map2
);
3492 int isl_basic_map_is_equal(
3493 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3497 if (!bmap1
|| !bmap2
)
3499 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
3502 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
3506 int isl_basic_set_is_equal(
3507 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
3509 return isl_basic_map_is_equal(
3510 (struct isl_basic_map
*)bset1
, (struct isl_basic_map
*)bset2
);
3513 int isl_map_is_empty(struct isl_map
*map
)
3520 for (i
= 0; i
< map
->n
; ++i
) {
3521 is_empty
= isl_basic_map_is_empty(map
->p
[i
]);
3530 int isl_map_fast_is_empty(struct isl_map
*map
)
3535 int isl_set_is_empty(struct isl_set
*set
)
3537 return isl_map_is_empty((struct isl_map
*)set
);
3540 int isl_map_is_subset(struct isl_map
*map1
, struct isl_map
*map2
)
3544 struct isl_map
*diff
;
3549 if (isl_map_is_empty(map1
))
3552 if (isl_map_is_empty(map2
))
3555 diff
= isl_map_subtract(isl_map_copy(map1
), isl_map_copy(map2
));
3559 is_subset
= isl_map_is_empty(diff
);
3565 int isl_map_is_equal(struct isl_map
*map1
, struct isl_map
*map2
)
3571 is_subset
= isl_map_is_subset(map1
, map2
);
3574 is_subset
= isl_map_is_subset(map2
, map1
);
3578 int isl_basic_map_is_strict_subset(
3579 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3583 if (!bmap1
|| !bmap2
)
3585 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
3588 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
3589 if (is_subset
== -1)
3594 int isl_basic_map_is_universe(struct isl_basic_map
*bmap
)
3598 return bmap
->n_eq
== 0 && bmap
->n_ineq
== 0;
3601 int isl_basic_set_is_universe(struct isl_basic_set
*bset
)
3605 return bset
->n_eq
== 0 && bset
->n_ineq
== 0;
3608 int isl_basic_map_is_empty(struct isl_basic_map
*bmap
)
3610 struct isl_basic_set
*bset
= NULL
;
3611 struct isl_vec
*sample
= NULL
;
3618 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
3621 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
3622 struct isl_basic_map
*copy
= isl_basic_map_copy(bmap
);
3623 copy
= isl_basic_map_convex_hull(copy
);
3624 empty
= ISL_F_ISSET(copy
, ISL_BASIC_MAP_EMPTY
);
3625 isl_basic_map_free(copy
);
3629 total
= 1 + isl_basic_map_total_dim(bmap
);
3630 if (bmap
->sample
&& bmap
->sample
->size
== total
) {
3631 int contains
= basic_map_contains(bmap
, bmap
->sample
);
3637 isl_vec_free(bmap
->sample
);
3638 bmap
->sample
= NULL
;
3639 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
3642 sample
= isl_basic_set_sample(bset
);
3645 empty
= sample
->size
== 0;
3646 isl_vec_free(bmap
->sample
);
3647 bmap
->sample
= sample
;
3649 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
3654 int isl_basic_map_fast_is_empty(struct isl_basic_map
*bmap
)
3658 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
);
3661 int isl_basic_set_fast_is_empty(struct isl_basic_set
*bset
)
3665 return ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
);
3668 int isl_basic_set_is_empty(struct isl_basic_set
*bset
)
3670 return isl_basic_map_is_empty((struct isl_basic_map
*)bset
);
3673 struct isl_map
*isl_basic_map_union(
3674 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3676 struct isl_map
*map
;
3677 if (!bmap1
|| !bmap2
)
3680 isl_assert(map1
->ctx
, isl_dim_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
3682 map
= isl_map_alloc_dim(isl_dim_copy(bmap1
->dim
), 2, 0);
3685 map
= isl_map_add(map
, bmap1
);
3686 map
= isl_map_add(map
, bmap2
);
3689 isl_basic_map_free(bmap1
);
3690 isl_basic_map_free(bmap2
);
3694 struct isl_set
*isl_basic_set_union(
3695 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
3697 return (struct isl_set
*)isl_basic_map_union(
3698 (struct isl_basic_map
*)bset1
,
3699 (struct isl_basic_map
*)bset2
);
3702 /* Order divs such that any div only depends on previous divs */
3703 static struct isl_basic_map
*order_divs(struct isl_basic_map
*bmap
)
3706 unsigned off
= isl_dim_total(bmap
->dim
);
3708 for (i
= 0; i
< bmap
->n_div
; ++i
) {
3710 pos
= isl_seq_first_non_zero(bmap
->div
[i
]+1+1+off
+i
,
3714 swap_div(bmap
, i
, i
+ pos
);
3720 /* Look for a div in dst that corresponds to the div "div" in src.
3721 * The divs before "div" in src and dst are assumed to be the same.
3723 * Returns -1 if no corresponding div was found and the position
3724 * of the corresponding div in dst otherwise.
3726 static int find_div(struct isl_basic_map
*dst
,
3727 struct isl_basic_map
*src
, unsigned div
)
3731 unsigned total
= isl_dim_total(src
->dim
);
3733 isl_assert(dst
->ctx
, div
<= dst
->n_div
, return -1);
3734 for (i
= div
; i
< dst
->n_div
; ++i
)
3735 if (isl_seq_eq(dst
->div
[i
], src
->div
[div
], 1+1+total
+div
) &&
3736 isl_seq_first_non_zero(dst
->div
[i
]+1+1+total
+div
,
3737 dst
->n_div
- div
) == -1)
3742 struct isl_basic_map
*isl_basic_map_align_divs(
3743 struct isl_basic_map
*dst
, struct isl_basic_map
*src
)
3746 unsigned total
= isl_dim_total(src
->dim
);
3751 if (src
->n_div
== 0)
3754 for (i
= 0; i
< src
->n_div
; ++i
)
3755 isl_assert(src
->ctx
, !isl_int_is_zero(src
->div
[i
][0]), goto error
);
3757 src
= order_divs(src
);
3758 dst
= isl_basic_map_cow(dst
);
3759 dst
= isl_basic_map_extend_dim(dst
, isl_dim_copy(dst
->dim
),
3760 src
->n_div
, 0, 2 * src
->n_div
);
3763 for (i
= 0; i
< src
->n_div
; ++i
) {
3764 int j
= find_div(dst
, src
, i
);
3766 j
= isl_basic_map_alloc_div(dst
);
3769 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
+i
);
3770 isl_seq_clr(dst
->div
[j
]+1+1+total
+i
, dst
->n_div
- i
);
3771 if (add_div_constraints(dst
, j
) < 0)
3775 swap_div(dst
, i
, j
);
3779 isl_basic_map_free(dst
);
3783 struct isl_basic_set
*isl_basic_set_align_divs(
3784 struct isl_basic_set
*dst
, struct isl_basic_set
*src
)
3786 return (struct isl_basic_set
*)isl_basic_map_align_divs(
3787 (struct isl_basic_map
*)dst
, (struct isl_basic_map
*)src
);
3790 struct isl_map
*isl_map_align_divs(struct isl_map
*map
)
3794 map
= isl_map_compute_divs(map
);
3795 map
= isl_map_cow(map
);
3799 for (i
= 1; i
< map
->n
; ++i
)
3800 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], map
->p
[i
]);
3801 for (i
= 1; i
< map
->n
; ++i
)
3802 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], map
->p
[0]);
3804 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3808 struct isl_set
*isl_set_align_divs(struct isl_set
*set
)
3810 return (struct isl_set
*)isl_map_align_divs((struct isl_map
*)set
);
3813 static struct isl_map
*add_cut_constraint(struct isl_map
*dst
,
3814 struct isl_basic_map
*src
, isl_int
*c
,
3815 unsigned len
, int oppose
)
3817 struct isl_basic_map
*copy
= NULL
;
3822 copy
= isl_basic_map_copy(src
);
3823 copy
= isl_basic_map_cow(copy
);
3826 copy
= isl_basic_map_extend_constraints(copy
, 0, 1);
3827 k
= isl_basic_map_alloc_inequality(copy
);
3831 isl_seq_neg(copy
->ineq
[k
], c
, len
);
3833 isl_seq_cpy(copy
->ineq
[k
], c
, len
);
3834 total
= 1 + isl_basic_map_total_dim(copy
);
3835 isl_seq_clr(copy
->ineq
[k
]+len
, total
- len
);
3836 isl_inequality_negate(copy
, k
);
3837 copy
= isl_basic_map_simplify(copy
);
3838 copy
= isl_basic_map_finalize(copy
);
3839 is_empty
= isl_basic_map_is_empty(copy
);
3843 dst
= isl_map_add(dst
, copy
);
3845 isl_basic_map_free(copy
);
3848 isl_basic_map_free(copy
);
3853 static struct isl_map
*subtract(struct isl_map
*map
, struct isl_basic_map
*bmap
)
3857 struct isl_map
*rest
= NULL
;
3859 unsigned total
= isl_basic_map_total_dim(bmap
);
3866 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
3867 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
3869 max
= map
->n
* (2 * bmap
->n_eq
+ bmap
->n_ineq
);
3870 rest
= isl_map_alloc_dim(isl_dim_copy(map
->dim
), max
, flags
);
3874 for (i
= 0; i
< map
->n
; ++i
) {
3875 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], bmap
);
3880 for (j
= 0; j
< map
->n
; ++j
)
3881 map
->p
[j
] = isl_basic_map_cow(map
->p
[j
]);
3883 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
3884 for (j
= 0; j
< map
->n
; ++j
) {
3885 rest
= add_cut_constraint(rest
,
3886 map
->p
[j
], bmap
->eq
[i
], 1+total
, 0);
3890 rest
= add_cut_constraint(rest
,
3891 map
->p
[j
], bmap
->eq
[i
], 1+total
, 1);
3895 map
->p
[j
] = isl_basic_map_extend_constraints(map
->p
[j
],
3899 k
= isl_basic_map_alloc_equality(map
->p
[j
]);
3902 isl_seq_cpy(map
->p
[j
]->eq
[k
], bmap
->eq
[i
], 1+total
);
3903 isl_seq_clr(map
->p
[j
]->eq
[k
]+1+total
,
3904 map
->p
[j
]->n_div
- bmap
->n_div
);
3908 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
3909 for (j
= 0; j
< map
->n
; ++j
) {
3910 rest
= add_cut_constraint(rest
,
3911 map
->p
[j
], bmap
->ineq
[i
], 1+total
, 0);
3915 map
->p
[j
] = isl_basic_map_extend_constraints(map
->p
[j
],
3919 k
= isl_basic_map_alloc_inequality(map
->p
[j
]);
3922 isl_seq_cpy(map
->p
[j
]->ineq
[k
], bmap
->ineq
[i
], 1+total
);
3923 isl_seq_clr(map
->p
[j
]->ineq
[k
]+1+total
,
3924 map
->p
[j
]->n_div
- bmap
->n_div
);
3936 struct isl_map
*isl_map_subtract(struct isl_map
*map1
, struct isl_map
*map2
)
3942 isl_assert(map1
->ctx
, isl_dim_equal(map1
->dim
, map2
->dim
), goto error
);
3944 if (isl_map_is_empty(map2
)) {
3949 map1
= isl_map_compute_divs(map1
);
3950 map2
= isl_map_compute_divs(map2
);
3954 for (i
= 0; map1
&& i
< map2
->n
; ++i
)
3955 map1
= subtract(map1
, map2
->p
[i
]);
3965 struct isl_set
*isl_set_subtract(struct isl_set
*set1
, struct isl_set
*set2
)
3967 return (struct isl_set
*)
3969 (struct isl_map
*)set1
, (struct isl_map
*)set2
);
3972 struct isl_set
*isl_set_apply(struct isl_set
*set
, struct isl_map
*map
)
3976 isl_assert(set
->ctx
, isl_map_compatible_domain(map
, set
), goto error
);
3977 map
= isl_map_intersect_domain(map
, set
);
3978 set
= isl_map_range(map
);
3986 /* There is no need to cow as removing empty parts doesn't change
3987 * the meaning of the set.
3989 struct isl_map
*isl_map_remove_empty_parts(struct isl_map
*map
)
3996 for (i
= map
->n
-1; i
>= 0; --i
) {
3997 if (!ISL_F_ISSET(map
->p
[i
], ISL_BASIC_MAP_EMPTY
))
3999 isl_basic_map_free(map
->p
[i
]);
4000 if (i
!= map
->n
-1) {
4001 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4002 map
->p
[i
] = map
->p
[map
->n
-1];
4010 struct isl_set
*isl_set_remove_empty_parts(struct isl_set
*set
)
4012 return (struct isl_set
*)
4013 isl_map_remove_empty_parts((struct isl_map
*)set
);
4016 struct isl_basic_map
*isl_map_copy_basic_map(struct isl_map
*map
)
4018 struct isl_basic_map
*bmap
;
4019 if (!map
|| map
->n
== 0)
4021 bmap
= map
->p
[map
->n
-1];
4022 isl_assert(map
->ctx
, ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
), return NULL
);
4023 return isl_basic_map_copy(bmap
);
4026 struct isl_basic_set
*isl_set_copy_basic_set(struct isl_set
*set
)
4028 (struct isl_basic_set
*)isl_map_copy_basic_map((struct isl_map
*)set
);
4031 struct isl_map
*isl_map_drop_basic_map(struct isl_map
*map
,
4032 struct isl_basic_map
*bmap
)
4038 for (i
= map
->n
-1; i
>= 0; --i
) {
4039 if (map
->p
[i
] != bmap
)
4041 map
= isl_map_cow(map
);
4044 isl_basic_map_free(map
->p
[i
]);
4045 if (i
!= map
->n
-1) {
4046 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
4047 map
->p
[i
] = map
->p
[map
->n
-1];
4052 isl_basic_map_free(bmap
);
4056 isl_basic_map_free(bmap
);
4060 struct isl_set
*isl_set_drop_basic_set(struct isl_set
*set
,
4061 struct isl_basic_set
*bset
)
4063 (struct isl_set
*)isl_map_drop_basic_map((struct isl_map
*)set
,
4064 (struct isl_basic_map
*)bset
);
4067 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
4068 * for any common value of the parameters and dimensions preceding dim
4069 * in both basic sets, the values of dimension pos in bset1 are
4070 * smaller or larger than those in bset2.
4073 * 1 if bset1 follows bset2
4074 * -1 if bset1 precedes bset2
4075 * 0 if bset1 and bset2 are incomparable
4076 * -2 if some error occurred.
4078 int isl_basic_set_compare_at(struct isl_basic_set
*bset1
,
4079 struct isl_basic_set
*bset2
, int pos
)
4081 struct isl_dim
*dims
;
4082 struct isl_basic_map
*bmap1
= NULL
;
4083 struct isl_basic_map
*bmap2
= NULL
;
4084 struct isl_ctx
*ctx
;
4085 struct isl_vec
*obj
;
4088 unsigned dim1
, dim2
;
4090 enum isl_lp_result res
;
4093 if (!bset1
|| !bset2
)
4096 nparam
= isl_basic_set_n_param(bset1
);
4097 dim1
= isl_basic_set_n_dim(bset1
);
4098 dim2
= isl_basic_set_n_dim(bset2
);
4099 dims
= isl_dim_alloc(bset1
->ctx
, nparam
, pos
, dim1
- pos
);
4100 bmap1
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset1
), dims
);
4101 dims
= isl_dim_alloc(bset2
->ctx
, nparam
, pos
, dim2
- pos
);
4102 bmap2
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset2
), dims
);
4103 if (!bmap1
|| !bmap2
)
4105 bmap1
= isl_basic_map_cow(bmap1
);
4106 bmap1
= isl_basic_map_extend(bmap1
, nparam
,
4107 pos
, (dim1
- pos
) + (dim2
- pos
),
4108 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
4109 bmap1
= add_constraints(bmap1
, bmap2
, 0, dim1
- pos
);
4112 total
= isl_basic_map_total_dim(bmap1
);
4114 obj
= isl_vec_alloc(ctx
, 1 + total
);
4115 isl_seq_clr(obj
->block
.data
, 1 + total
);
4116 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
], 1);
4117 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
+(dim1
-pos
)], -1);
4122 res
= isl_solve_lp(bmap1
, 0, obj
->block
.data
, ctx
->one
, &num
, &den
);
4123 if (res
== isl_lp_empty
)
4125 else if (res
== isl_lp_ok
&& isl_int_is_pos(num
))
4127 else if ((res
== isl_lp_ok
&& isl_int_is_neg(num
)) ||
4128 res
== isl_lp_unbounded
)
4134 isl_basic_map_free(bmap1
);
4138 isl_basic_map_free(bmap1
);
4139 isl_basic_map_free(bmap2
);
4143 static int isl_basic_map_fast_has_fixed_var(struct isl_basic_map
*bmap
,
4144 unsigned pos
, isl_int
*val
)
4152 total
= isl_basic_map_total_dim(bmap
);
4153 for (i
= 0, d
= total
-1; i
< bmap
->n_eq
&& d
+1 > pos
; ++i
) {
4154 for (; d
+1 > pos
; --d
)
4155 if (!isl_int_is_zero(bmap
->eq
[i
][1+d
]))
4159 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1, d
) != -1)
4161 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1+d
+1, total
-d
-1) != -1)
4163 if (!isl_int_is_one(bmap
->eq
[i
][1+d
]))
4166 isl_int_neg(*val
, bmap
->eq
[i
][0]);
4172 static int isl_map_fast_has_fixed_var(struct isl_map
*map
,
4173 unsigned pos
, isl_int
*val
)
4185 return isl_basic_map_fast_has_fixed_var(map
->p
[0], pos
, val
);
4188 fixed
= isl_basic_map_fast_has_fixed_var(map
->p
[0], pos
, &v
);
4189 for (i
= 1; fixed
== 1 && i
< map
->n
; ++i
) {
4190 fixed
= isl_basic_map_fast_has_fixed_var(map
->p
[i
], pos
, &tmp
);
4191 if (fixed
== 1 && isl_int_ne(tmp
, v
))
4195 isl_int_set(*val
, v
);
4201 static int isl_basic_set_fast_has_fixed_var(struct isl_basic_set
*bset
,
4202 unsigned pos
, isl_int
*val
)
4204 return isl_basic_map_fast_has_fixed_var((struct isl_basic_map
*)bset
,
4208 static int isl_set_fast_has_fixed_var(struct isl_set
*set
, unsigned pos
,
4211 return isl_map_fast_has_fixed_var((struct isl_map
*)set
, pos
, val
);
4214 int isl_basic_map_fast_is_fixed(struct isl_basic_map
*bmap
,
4215 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
4217 if (pos
>= isl_basic_map_dim(bmap
, type
))
4219 return isl_basic_map_fast_has_fixed_var(bmap
,
4220 isl_basic_map_offset(bmap
, type
) - 1 + pos
, val
);
4223 /* Check if dimension dim has fixed value and if so and if val is not NULL,
4224 * then return this fixed value in *val.
4226 int isl_basic_set_fast_dim_is_fixed(struct isl_basic_set
*bset
, unsigned dim
,
4229 return isl_basic_set_fast_has_fixed_var(bset
,
4230 isl_basic_set_n_param(bset
) + dim
, val
);
4233 /* Check if dimension dim has fixed value and if so and if val is not NULL,
4234 * then return this fixed value in *val.
4236 int isl_set_fast_dim_is_fixed(struct isl_set
*set
, unsigned dim
, isl_int
*val
)
4238 return isl_set_fast_has_fixed_var(set
, isl_set_n_param(set
) + dim
, val
);
4241 /* Check if input variable in has fixed value and if so and if val is not NULL,
4242 * then return this fixed value in *val.
4244 int isl_map_fast_input_is_fixed(struct isl_map
*map
, unsigned in
, isl_int
*val
)
4246 return isl_map_fast_has_fixed_var(map
, isl_map_n_param(map
) + in
, val
);
4249 /* Check if dimension dim has an (obvious) fixed lower bound and if so
4250 * and if val is not NULL, then return this lower bound in *val.
4252 int isl_basic_set_fast_dim_has_fixed_lower_bound(struct isl_basic_set
*bset
,
4253 unsigned dim
, isl_int
*val
)
4255 int i
, i_eq
= -1, i_ineq
= -1;
4262 total
= isl_basic_set_total_dim(bset
);
4263 nparam
= isl_basic_set_n_param(bset
);
4264 for (i
= 0; i
< bset
->n_eq
; ++i
) {
4265 if (isl_int_is_zero(bset
->eq
[i
][1+nparam
+dim
]))
4271 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
4272 if (!isl_int_is_pos(bset
->ineq
[i
][1+nparam
+dim
]))
4274 if (i_eq
!= -1 || i_ineq
!= -1)
4278 if (i_eq
== -1 && i_ineq
== -1)
4280 c
= i_eq
!= -1 ? bset
->eq
[i_eq
] : bset
->ineq
[i_ineq
];
4281 /* The coefficient should always be one due to normalization. */
4282 if (!isl_int_is_one(c
[1+nparam
+dim
]))
4284 if (isl_seq_first_non_zero(c
+1, nparam
+dim
) != -1)
4286 if (isl_seq_first_non_zero(c
+1+nparam
+dim
+1,
4287 total
- nparam
- dim
- 1) != -1)
4290 isl_int_neg(*val
, c
[0]);
4294 int isl_set_fast_dim_has_fixed_lower_bound(struct isl_set
*set
,
4295 unsigned dim
, isl_int
*val
)
4307 return isl_basic_set_fast_dim_has_fixed_lower_bound(set
->p
[0],
4311 fixed
= isl_basic_set_fast_dim_has_fixed_lower_bound(set
->p
[0],
4313 for (i
= 1; fixed
== 1 && i
< set
->n
; ++i
) {
4314 fixed
= isl_basic_set_fast_dim_has_fixed_lower_bound(set
->p
[i
],
4316 if (fixed
== 1 && isl_int_ne(tmp
, v
))
4320 isl_int_set(*val
, v
);
4331 static int qsort_constraint_cmp(const void *p1
, const void *p2
)
4333 const struct constraint
*c1
= (const struct constraint
*)p1
;
4334 const struct constraint
*c2
= (const struct constraint
*)p2
;
4335 unsigned size
= isl_min(c1
->size
, c2
->size
);
4336 return isl_seq_cmp(c1
->c
, c2
->c
, size
);
4339 static struct isl_basic_map
*isl_basic_map_sort_constraints(
4340 struct isl_basic_map
*bmap
)
4343 struct constraint
*c
;
4348 total
= isl_basic_map_total_dim(bmap
);
4349 c
= isl_alloc_array(bmap
->ctx
, struct constraint
, bmap
->n_ineq
);
4352 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
4354 c
[i
].c
= bmap
->ineq
[i
];
4356 qsort(c
, bmap
->n_ineq
, sizeof(struct constraint
), qsort_constraint_cmp
);
4357 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
4358 bmap
->ineq
[i
] = c
[i
].c
;
4362 isl_basic_map_free(bmap
);
4366 struct isl_basic_map
*isl_basic_map_normalize(struct isl_basic_map
*bmap
)
4370 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED
))
4372 bmap
= isl_basic_map_convex_hull(bmap
);
4373 bmap
= isl_basic_map_sort_constraints(bmap
);
4374 ISL_F_SET(bmap
, ISL_BASIC_MAP_NORMALIZED
);
4378 struct isl_basic_set
*isl_basic_set_normalize(struct isl_basic_set
*bset
)
4380 return (struct isl_basic_set
*)isl_basic_map_normalize(
4381 (struct isl_basic_map
*)bset
);
4384 static int isl_basic_map_fast_cmp(const struct isl_basic_map
*bmap1
,
4385 const struct isl_basic_map
*bmap2
)
4392 if (isl_basic_map_n_param(bmap1
) != isl_basic_map_n_param(bmap2
))
4393 return isl_basic_map_n_param(bmap1
) - isl_basic_map_n_param(bmap2
);
4394 if (isl_basic_map_n_in(bmap1
) != isl_basic_map_n_in(bmap2
))
4395 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
4396 if (isl_basic_map_n_out(bmap1
) != isl_basic_map_n_out(bmap2
))
4397 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
4398 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
) &&
4399 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
4401 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
))
4403 if (ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
4405 if (bmap1
->n_eq
!= bmap2
->n_eq
)
4406 return bmap1
->n_eq
- bmap2
->n_eq
;
4407 if (bmap1
->n_ineq
!= bmap2
->n_ineq
)
4408 return bmap1
->n_ineq
- bmap2
->n_ineq
;
4409 if (bmap1
->n_div
!= bmap2
->n_div
)
4410 return bmap1
->n_div
- bmap2
->n_div
;
4411 total
= isl_basic_map_total_dim(bmap1
);
4412 for (i
= 0; i
< bmap1
->n_eq
; ++i
) {
4413 cmp
= isl_seq_cmp(bmap1
->eq
[i
], bmap2
->eq
[i
], 1+total
);
4417 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
4418 cmp
= isl_seq_cmp(bmap1
->ineq
[i
], bmap2
->ineq
[i
], 1+total
);
4422 for (i
= 0; i
< bmap1
->n_div
; ++i
) {
4423 cmp
= isl_seq_cmp(bmap1
->div
[i
], bmap2
->div
[i
], 1+1+total
);
4430 static int isl_basic_map_fast_is_equal(struct isl_basic_map
*bmap1
,
4431 struct isl_basic_map
*bmap2
)
4433 return isl_basic_map_fast_cmp(bmap1
, bmap2
) == 0;
4436 static int qsort_bmap_cmp(const void *p1
, const void *p2
)
4438 const struct isl_basic_map
*bmap1
= *(const struct isl_basic_map
**)p1
;
4439 const struct isl_basic_map
*bmap2
= *(const struct isl_basic_map
**)p2
;
4441 return isl_basic_map_fast_cmp(bmap1
, bmap2
);
4444 /* We normalize in place, but if anything goes wrong we need
4445 * to return NULL, so we need to make sure we don't change the
4446 * meaning of any possible other copies of map.
4448 struct isl_map
*isl_map_normalize(struct isl_map
*map
)
4451 struct isl_basic_map
*bmap
;
4455 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
))
4457 for (i
= 0; i
< map
->n
; ++i
) {
4458 bmap
= isl_basic_map_normalize(isl_basic_map_copy(map
->p
[i
]));
4461 isl_basic_map_free(map
->p
[i
]);
4464 qsort(map
->p
, map
->n
, sizeof(struct isl_basic_map
*), qsort_bmap_cmp
);
4465 ISL_F_SET(map
, ISL_MAP_NORMALIZED
);
4466 map
= isl_map_remove_empty_parts(map
);
4469 for (i
= map
->n
- 1; i
>= 1; --i
) {
4470 if (!isl_basic_map_fast_is_equal(map
->p
[i
-1], map
->p
[i
]))
4472 isl_basic_map_free(map
->p
[i
-1]);
4473 for (j
= i
; j
< map
->n
; ++j
)
4474 map
->p
[j
-1] = map
->p
[j
];
4484 struct isl_set
*isl_set_normalize(struct isl_set
*set
)
4486 return (struct isl_set
*)isl_map_normalize((struct isl_map
*)set
);
4489 int isl_map_fast_is_equal(struct isl_map
*map1
, struct isl_map
*map2
)
4499 if (!isl_dim_equal(map1
->dim
, map2
->dim
))
4502 map1
= isl_map_copy(map1
);
4503 map2
= isl_map_copy(map2
);
4504 map1
= isl_map_normalize(map1
);
4505 map2
= isl_map_normalize(map2
);
4508 equal
= map1
->n
== map2
->n
;
4509 for (i
= 0; equal
&& i
< map1
->n
; ++i
) {
4510 equal
= isl_basic_map_fast_is_equal(map1
->p
[i
], map2
->p
[i
]);
4523 int isl_set_fast_is_equal(struct isl_set
*set1
, struct isl_set
*set2
)
4525 return isl_map_fast_is_equal((struct isl_map
*)set1
,
4526 (struct isl_map
*)set2
);
4529 /* Return an interval that ranges from min to max (inclusive)
4531 struct isl_basic_set
*isl_basic_set_interval(struct isl_ctx
*ctx
,
4532 isl_int min
, isl_int max
)
4535 struct isl_basic_set
*bset
= NULL
;
4537 bset
= isl_basic_set_alloc(ctx
, 0, 1, 0, 0, 2);
4541 k
= isl_basic_set_alloc_inequality(bset
);
4544 isl_int_set_si(bset
->ineq
[k
][1], 1);
4545 isl_int_neg(bset
->ineq
[k
][0], min
);
4547 k
= isl_basic_set_alloc_inequality(bset
);
4550 isl_int_set_si(bset
->ineq
[k
][1], -1);
4551 isl_int_set(bset
->ineq
[k
][0], max
);
4555 isl_basic_set_free(bset
);
4559 /* Return the Cartesian product of the basic sets in list (in the given order).
4561 struct isl_basic_set
*isl_basic_set_product(struct isl_basic_set_list
*list
)
4569 struct isl_basic_set
*product
= NULL
;
4573 isl_assert(list
->ctx
, list
->n
> 0, goto error
);
4574 isl_assert(list
->ctx
, list
->p
[0], goto error
);
4575 nparam
= isl_basic_set_n_param(list
->p
[0]);
4576 dim
= isl_basic_set_n_dim(list
->p
[0]);
4577 extra
= list
->p
[0]->n_div
;
4578 n_eq
= list
->p
[0]->n_eq
;
4579 n_ineq
= list
->p
[0]->n_ineq
;
4580 for (i
= 1; i
< list
->n
; ++i
) {
4581 isl_assert(list
->ctx
, list
->p
[i
], goto error
);
4582 isl_assert(list
->ctx
,
4583 nparam
== isl_basic_set_n_param(list
->p
[i
]), goto error
);
4584 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
4585 extra
+= list
->p
[i
]->n_div
;
4586 n_eq
+= list
->p
[i
]->n_eq
;
4587 n_ineq
+= list
->p
[i
]->n_ineq
;
4589 product
= isl_basic_set_alloc(list
->ctx
, nparam
, dim
, extra
,
4594 for (i
= 0; i
< list
->n
; ++i
) {
4595 isl_basic_set_add_constraints(product
,
4596 isl_basic_set_copy(list
->p
[i
]), dim
);
4597 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
4599 isl_basic_set_list_free(list
);
4602 isl_basic_set_free(product
);
4603 isl_basic_set_list_free(list
);
4607 struct isl_basic_map
*isl_basic_map_product(
4608 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
4610 struct isl_dim
*dim_result
= NULL
;
4611 struct isl_basic_map
*bmap
;
4612 unsigned in1
, in2
, out1
, out2
, nparam
, total
, pos
;
4613 struct isl_dim_map
*dim_map1
, *dim_map2
;
4615 if (!bmap1
|| !bmap2
)
4618 isl_assert(map1
->ctx
, isl_dim_match(bmap1
->dim
, isl_dim_param
,
4619 bmap2
->dim
, isl_dim_param
), goto error
);
4620 dim_result
= isl_dim_product(isl_dim_copy(bmap1
->dim
),
4621 isl_dim_copy(bmap2
->dim
));
4623 in1
= isl_basic_map_n_in(bmap1
);
4624 in2
= isl_basic_map_n_in(bmap2
);
4625 out1
= isl_basic_map_n_out(bmap1
);
4626 out2
= isl_basic_map_n_out(bmap2
);
4627 nparam
= isl_basic_map_n_param(bmap1
);
4629 total
= nparam
+ in1
+ in2
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
4630 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4631 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4632 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
4633 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
4634 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
4635 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
4636 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
4637 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
4638 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
4639 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
4641 bmap
= isl_basic_map_alloc_dim(dim_result
,
4642 bmap1
->n_div
+ bmap2
->n_div
,
4643 bmap1
->n_eq
+ bmap2
->n_eq
,
4644 bmap1
->n_ineq
+ bmap2
->n_ineq
);
4645 bmap
= add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
4646 bmap
= add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
4647 bmap
= isl_basic_map_simplify(bmap
);
4648 return isl_basic_map_finalize(bmap
);
4650 isl_basic_map_free(bmap1
);
4651 isl_basic_map_free(bmap2
);
4655 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
4657 struct isl_map
*isl_map_product(struct isl_map
*map1
, struct isl_map
*map2
)
4660 struct isl_map
*result
;
4666 isl_assert(map1
->ctx
, isl_dim_match(map1
->dim
, isl_dim_param
,
4667 map2
->dim
, isl_dim_param
), goto error
);
4669 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
4670 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
4671 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
4673 result
= isl_map_alloc_dim(isl_dim_product(isl_dim_copy(map1
->dim
),
4674 isl_dim_copy(map2
->dim
)),
4675 map1
->n
* map2
->n
, flags
);
4678 for (i
= 0; i
< map1
->n
; ++i
)
4679 for (j
= 0; j
< map2
->n
; ++j
) {
4680 struct isl_basic_map
*part
;
4681 part
= isl_basic_map_product(
4682 isl_basic_map_copy(map1
->p
[i
]),
4683 isl_basic_map_copy(map2
->p
[j
]));
4684 if (isl_basic_map_is_empty(part
))
4685 isl_basic_map_free(part
);
4687 result
= isl_map_add(result
, part
);
4700 /* Given two set A and B, construct its Cartesian product A x B.
4702 struct isl_set
*isl_set_product(struct isl_set
*set1
, struct isl_set
*set2
)
4704 return (struct isl_set
*)isl_map_product((struct isl_map
*)set1
,
4705 (struct isl_map
*)set2
);
4708 uint32_t isl_basic_set_get_hash(struct isl_basic_set
*bset
)
4716 bset
= isl_basic_set_copy(bset
);
4717 bset
= isl_basic_set_normalize(bset
);
4720 total
= isl_basic_set_total_dim(bset
);
4721 isl_hash_byte(hash
, bset
->n_eq
& 0xFF);
4722 for (i
= 0; i
< bset
->n_eq
; ++i
) {
4724 c_hash
= isl_seq_get_hash(bset
->eq
[i
], 1 + total
);
4725 isl_hash_hash(hash
, c_hash
);
4727 isl_hash_byte(hash
, bset
->n_ineq
& 0xFF);
4728 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
4730 c_hash
= isl_seq_get_hash(bset
->ineq
[i
], 1 + total
);
4731 isl_hash_hash(hash
, c_hash
);
4733 isl_hash_byte(hash
, bset
->n_div
& 0xFF);
4734 for (i
= 0; i
< bset
->n_div
; ++i
) {
4736 if (isl_int_is_zero(bset
->div
[i
][0]))
4738 isl_hash_byte(hash
, i
& 0xFF);
4739 c_hash
= isl_seq_get_hash(bset
->div
[i
], 1 + 1 + total
);
4740 isl_hash_hash(hash
, c_hash
);
4742 isl_basic_set_free(bset
);
4746 uint32_t isl_set_get_hash(struct isl_set
*set
)
4753 set
= isl_set_copy(set
);
4754 set
= isl_set_normalize(set
);
4758 hash
= isl_hash_init();
4759 for (i
= 0; i
< set
->n
; ++i
) {
4761 bset_hash
= isl_basic_set_get_hash(set
->p
[i
]);
4762 isl_hash_hash(hash
, bset_hash
);
4770 /* Check if the value for dimension dim is completely determined
4771 * by the values of the other parameters and variables.
4772 * That is, check if dimension dim is involved in an equality.
4774 int isl_basic_set_dim_is_unique(struct isl_basic_set
*bset
, unsigned dim
)
4781 nparam
= isl_basic_set_n_param(bset
);
4782 for (i
= 0; i
< bset
->n_eq
; ++i
)
4783 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ dim
]))
4788 /* Check if the value for dimension dim is completely determined
4789 * by the values of the other parameters and variables.
4790 * That is, check if dimension dim is involved in an equality
4791 * for each of the subsets.
4793 int isl_set_dim_is_unique(struct isl_set
*set
, unsigned dim
)
4799 for (i
= 0; i
< set
->n
; ++i
) {
4801 unique
= isl_basic_set_dim_is_unique(set
->p
[i
], dim
);