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 struct isl_map
*isl_basic_map_lexmax(
2967 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
2968 struct isl_set
**empty
)
2970 return isl_pip_basic_map_lexmax(bmap
, dom
, empty
);
2973 struct isl_map
*isl_basic_map_lexmin(
2974 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
2975 struct isl_set
**empty
)
2977 return isl_pip_basic_map_lexmin(bmap
, dom
, empty
);
2980 struct isl_set
*isl_basic_set_lexmin(struct isl_basic_set
*bset
)
2982 struct isl_basic_map
*bmap
= NULL
;
2983 struct isl_basic_set
*dom
= NULL
;
2984 struct isl_map
*min
;
2985 struct isl_dim
*param_dim
;
2989 bmap
= isl_basic_map_from_basic_set(bset
, isl_dim_copy(bset
->dim
));
2992 param_dim
= isl_dim_domain(isl_dim_copy(bmap
->dim
));
2993 dom
= isl_basic_set_universe(param_dim
);
2996 min
= isl_basic_map_lexmin(bmap
, dom
, NULL
);
2997 return isl_map_range(min
);
2999 isl_basic_map_free(bmap
);
3003 /* If bmap contains any unknown divs, then compute explicit
3004 * expressions for them. However, this computation may be
3005 * quite expensive, so first try to remove divs that aren't
3008 struct isl_map
*isl_basic_map_compute_divs(struct isl_basic_map
*bmap
)
3015 off
= isl_dim_total(bmap
->dim
);
3016 for (i
= 0; i
< bmap
->n_div
; ++i
) {
3017 if (isl_int_is_zero(bmap
->div
[i
][0]))
3019 isl_assert(bmap
->ctx
, isl_int_is_zero(bmap
->div
[i
][1+1+off
+i
]),
3022 if (i
== bmap
->n_div
)
3023 return isl_map_from_basic_map(bmap
);
3024 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
3027 for (i
= 0; i
< bmap
->n_div
; ++i
)
3028 if (isl_int_is_zero(bmap
->div
[i
][0]))
3030 if (i
== bmap
->n_div
)
3031 return isl_map_from_basic_map(bmap
);
3032 struct isl_map
*map
= isl_pip_basic_map_compute_divs(bmap
);
3035 isl_basic_map_free(bmap
);
3039 struct isl_map
*isl_map_compute_divs(struct isl_map
*map
)
3042 struct isl_map
*res
;
3048 res
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[0]));
3049 for (i
= 1 ; i
< map
->n
; ++i
) {
3051 r2
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[i
]));
3052 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
3053 res
= isl_map_union_disjoint(res
, r2
);
3055 res
= isl_map_union(res
, r2
);
3062 struct isl_set
*isl_basic_set_compute_divs(struct isl_basic_set
*bset
)
3064 return (struct isl_set
*)
3065 isl_basic_map_compute_divs((struct isl_basic_map
*)bset
);
3068 struct isl_set
*isl_set_compute_divs(struct isl_set
*set
)
3070 return (struct isl_set
*)
3071 isl_map_compute_divs((struct isl_map
*)set
);
3074 struct isl_set
*isl_map_domain(struct isl_map
*map
)
3077 struct isl_set
*set
;
3082 map
= isl_map_cow(map
);
3086 set
= (struct isl_set
*)map
;
3087 set
->dim
= isl_dim_domain(set
->dim
);
3090 for (i
= 0; i
< map
->n
; ++i
) {
3091 set
->p
[i
] = isl_basic_map_domain(map
->p
[i
]);
3095 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
3096 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
3103 struct isl_map
*isl_map_union_disjoint(
3104 struct isl_map
*map1
, struct isl_map
*map2
)
3108 struct isl_map
*map
= NULL
;
3122 isl_assert(map1
->ctx
, isl_dim_equal(map1
->dim
, map2
->dim
), goto error
);
3124 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
3125 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
3126 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
3128 map
= isl_map_alloc_dim(isl_dim_copy(map1
->dim
),
3129 map1
->n
+ map2
->n
, flags
);
3132 for (i
= 0; i
< map1
->n
; ++i
) {
3133 map
= isl_map_add(map
,
3134 isl_basic_map_copy(map1
->p
[i
]));
3138 for (i
= 0; i
< map2
->n
; ++i
) {
3139 map
= isl_map_add(map
,
3140 isl_basic_map_copy(map2
->p
[i
]));
3154 struct isl_map
*isl_map_union(struct isl_map
*map1
, struct isl_map
*map2
)
3156 map1
= isl_map_union_disjoint(map1
, map2
);
3160 ISL_F_CLR(map1
, ISL_MAP_DISJOINT
);
3164 struct isl_set
*isl_set_union_disjoint(
3165 struct isl_set
*set1
, struct isl_set
*set2
)
3167 return (struct isl_set
*)
3168 isl_map_union_disjoint(
3169 (struct isl_map
*)set1
, (struct isl_map
*)set2
);
3172 struct isl_set
*isl_set_union(struct isl_set
*set1
, struct isl_set
*set2
)
3174 return (struct isl_set
*)
3175 isl_map_union((struct isl_map
*)set1
, (struct isl_map
*)set2
);
3178 struct isl_map
*isl_map_intersect_range(
3179 struct isl_map
*map
, struct isl_set
*set
)
3182 struct isl_map
*result
;
3188 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
) &&
3189 ISL_F_ISSET(set
, ISL_MAP_DISJOINT
))
3190 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
3192 result
= isl_map_alloc_dim(isl_dim_copy(map
->dim
),
3193 map
->n
* set
->n
, flags
);
3196 for (i
= 0; i
< map
->n
; ++i
)
3197 for (j
= 0; j
< set
->n
; ++j
) {
3198 result
= isl_map_add(result
,
3199 isl_basic_map_intersect_range(
3200 isl_basic_map_copy(map
->p
[i
]),
3201 isl_basic_set_copy(set
->p
[j
])));
3214 struct isl_map
*isl_map_intersect_domain(
3215 struct isl_map
*map
, struct isl_set
*set
)
3217 return isl_map_reverse(
3218 isl_map_intersect_range(isl_map_reverse(map
), set
));
3221 struct isl_map
*isl_map_apply_domain(
3222 struct isl_map
*map1
, struct isl_map
*map2
)
3226 map1
= isl_map_reverse(map1
);
3227 map1
= isl_map_apply_range(map1
, map2
);
3228 return isl_map_reverse(map1
);
3235 struct isl_map
*isl_map_apply_range(
3236 struct isl_map
*map1
, struct isl_map
*map2
)
3238 struct isl_dim
*dim_result
;
3239 struct isl_map
*result
;
3248 dim_result
= isl_dim_join(isl_dim_copy(map1
->dim
),
3249 isl_dim_copy(map2
->dim
));
3251 result
= isl_map_alloc_dim(dim_result
, map1
->n
* map2
->n
, 0);
3254 for (i
= 0; i
< map1
->n
; ++i
)
3255 for (j
= 0; j
< map2
->n
; ++j
) {
3256 result
= isl_map_add(result
,
3257 isl_basic_map_apply_range(
3258 isl_basic_map_copy(map1
->p
[i
]),
3259 isl_basic_map_copy(map2
->p
[j
])));
3265 if (result
&& result
->n
<= 1)
3266 ISL_F_SET(result
, ISL_MAP_DISJOINT
);
3275 * returns range - domain
3277 struct isl_basic_set
*isl_basic_map_deltas(struct isl_basic_map
*bmap
)
3279 struct isl_basic_set
*bset
;
3286 dim
= isl_basic_map_n_in(bmap
);
3287 nparam
= isl_basic_map_n_param(bmap
);
3288 isl_assert(bmap
->ctx
, dim
== isl_basic_map_n_out(bmap
), goto error
);
3289 bset
= isl_basic_set_from_basic_map(bmap
);
3290 bset
= isl_basic_set_cow(bset
);
3291 bset
= isl_basic_set_extend(bset
, nparam
, 3*dim
, 0, dim
, 0);
3292 bset
= isl_basic_set_swap_vars(bset
, 2*dim
);
3293 for (i
= 0; i
< dim
; ++i
) {
3294 int j
= isl_basic_map_alloc_equality(
3295 (struct isl_basic_map
*)bset
);
3298 isl_seq_clr(bset
->eq
[j
], 1 + isl_basic_set_total_dim(bset
));
3299 isl_int_set_si(bset
->eq
[j
][1+nparam
+i
], 1);
3300 isl_int_set_si(bset
->eq
[j
][1+nparam
+dim
+i
], 1);
3301 isl_int_set_si(bset
->eq
[j
][1+nparam
+2*dim
+i
], -1);
3303 return isl_basic_set_project_out(bset
, 2*dim
, 0);
3305 isl_basic_map_free(bmap
);
3310 * returns range - domain
3312 struct isl_set
*isl_map_deltas(struct isl_map
*map
)
3315 struct isl_set
*result
;
3320 isl_assert(map
->ctx
, isl_map_n_in(map
) == isl_map_n_out(map
), goto error
);
3321 result
= isl_set_alloc(map
->ctx
, isl_map_n_param(map
),
3322 isl_map_n_in(map
), map
->n
, map
->flags
);
3325 for (i
= 0; i
< map
->n
; ++i
)
3326 result
= isl_set_add(result
,
3327 isl_basic_map_deltas(isl_basic_map_copy(map
->p
[i
])));
3335 static struct isl_basic_map
*basic_map_identity(struct isl_dim
*dims
)
3337 struct isl_basic_map
*bmap
;
3345 nparam
= dims
->nparam
;
3347 bmap
= isl_basic_map_alloc_dim(dims
, 0, dim
, 0);
3351 for (i
= 0; i
< dim
; ++i
) {
3352 int j
= isl_basic_map_alloc_equality(bmap
);
3355 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
3356 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
3357 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], -1);
3359 return isl_basic_map_finalize(bmap
);
3361 isl_basic_map_free(bmap
);
3365 struct isl_basic_map
*isl_basic_map_identity(struct isl_dim
*set_dim
)
3367 struct isl_dim
*dim
= isl_dim_map(set_dim
);
3370 return basic_map_identity(dim
);
3373 struct isl_basic_map
*isl_basic_map_identity_like(struct isl_basic_map
*model
)
3375 if (!model
|| !model
->dim
)
3377 isl_assert(model
->ctx
,
3378 model
->dim
->n_in
== model
->dim
->n_out
, return NULL
);
3379 return basic_map_identity(isl_dim_copy(model
->dim
));
3382 static struct isl_map
*map_identity(struct isl_dim
*dim
)
3384 struct isl_map
*map
= isl_map_alloc_dim(dim
, 1, ISL_MAP_DISJOINT
);
3385 return isl_map_add(map
, basic_map_identity(isl_dim_copy(dim
)));
3388 struct isl_map
*isl_map_identity(struct isl_dim
*set_dim
)
3390 struct isl_dim
*dim
= isl_dim_map(set_dim
);
3393 return map_identity(dim
);
3396 struct isl_map
*isl_map_identity_like(struct isl_basic_map
*model
)
3398 if (!model
|| !model
->dim
)
3400 isl_assert(model
->ctx
,
3401 model
->dim
->n_in
== model
->dim
->n_out
, return NULL
);
3402 return map_identity(isl_dim_copy(model
->dim
));
3405 int isl_set_is_equal(struct isl_set
*set1
, struct isl_set
*set2
)
3407 return isl_map_is_equal((struct isl_map
*)set1
, (struct isl_map
*)set2
);
3410 int isl_set_is_subset(struct isl_set
*set1
, struct isl_set
*set2
)
3412 return isl_map_is_subset(
3413 (struct isl_map
*)set1
, (struct isl_map
*)set2
);
3416 int isl_basic_map_is_subset(
3417 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3420 struct isl_map
*map1
;
3421 struct isl_map
*map2
;
3423 if (!bmap1
|| !bmap2
)
3426 map1
= isl_map_from_basic_map(isl_basic_map_copy(bmap1
));
3427 map2
= isl_map_from_basic_map(isl_basic_map_copy(bmap2
));
3429 is_subset
= isl_map_is_subset(map1
, map2
);
3437 int isl_basic_map_is_equal(
3438 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3442 if (!bmap1
|| !bmap2
)
3444 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
3447 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
3451 int isl_basic_set_is_equal(
3452 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
3454 return isl_basic_map_is_equal(
3455 (struct isl_basic_map
*)bset1
, (struct isl_basic_map
*)bset2
);
3458 int isl_map_is_empty(struct isl_map
*map
)
3465 for (i
= 0; i
< map
->n
; ++i
) {
3466 is_empty
= isl_basic_map_is_empty(map
->p
[i
]);
3475 int isl_map_fast_is_empty(struct isl_map
*map
)
3480 int isl_set_is_empty(struct isl_set
*set
)
3482 return isl_map_is_empty((struct isl_map
*)set
);
3485 int isl_map_is_subset(struct isl_map
*map1
, struct isl_map
*map2
)
3489 struct isl_map
*diff
;
3494 if (isl_map_is_empty(map1
))
3497 if (isl_map_is_empty(map2
))
3500 diff
= isl_map_subtract(isl_map_copy(map1
), isl_map_copy(map2
));
3504 is_subset
= isl_map_is_empty(diff
);
3510 int isl_map_is_equal(struct isl_map
*map1
, struct isl_map
*map2
)
3516 is_subset
= isl_map_is_subset(map1
, map2
);
3519 is_subset
= isl_map_is_subset(map2
, map1
);
3523 int isl_basic_map_is_strict_subset(
3524 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3528 if (!bmap1
|| !bmap2
)
3530 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
3533 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
3534 if (is_subset
== -1)
3539 int isl_basic_map_is_universe(struct isl_basic_map
*bmap
)
3543 return bmap
->n_eq
== 0 && bmap
->n_ineq
== 0;
3546 int isl_basic_set_is_universe(struct isl_basic_set
*bset
)
3550 return bset
->n_eq
== 0 && bset
->n_ineq
== 0;
3553 int isl_basic_map_is_empty(struct isl_basic_map
*bmap
)
3555 struct isl_basic_set
*bset
= NULL
;
3556 struct isl_vec
*sample
= NULL
;
3563 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
3566 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
3567 struct isl_basic_map
*copy
= isl_basic_map_copy(bmap
);
3568 copy
= isl_basic_map_convex_hull(copy
);
3569 empty
= ISL_F_ISSET(copy
, ISL_BASIC_MAP_EMPTY
);
3570 isl_basic_map_free(copy
);
3574 total
= 1 + isl_basic_map_total_dim(bmap
);
3575 if (bmap
->sample
&& bmap
->sample
->size
== total
) {
3576 int contains
= basic_map_contains(bmap
, bmap
->sample
);
3582 isl_vec_free(bmap
->sample
);
3583 bmap
->sample
= NULL
;
3584 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
3587 sample
= isl_basic_set_sample(bset
);
3590 empty
= sample
->size
== 0;
3591 isl_vec_free(bmap
->sample
);
3592 bmap
->sample
= sample
;
3594 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
3599 int isl_basic_map_fast_is_empty(struct isl_basic_map
*bmap
)
3603 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
);
3606 int isl_basic_set_fast_is_empty(struct isl_basic_set
*bset
)
3610 return ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
);
3613 int isl_basic_set_is_empty(struct isl_basic_set
*bset
)
3615 return isl_basic_map_is_empty((struct isl_basic_map
*)bset
);
3618 struct isl_map
*isl_basic_map_union(
3619 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3621 struct isl_map
*map
;
3622 if (!bmap1
|| !bmap2
)
3625 isl_assert(map1
->ctx
, isl_dim_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
3627 map
= isl_map_alloc_dim(isl_dim_copy(bmap1
->dim
), 2, 0);
3630 map
= isl_map_add(map
, bmap1
);
3631 map
= isl_map_add(map
, bmap2
);
3634 isl_basic_map_free(bmap1
);
3635 isl_basic_map_free(bmap2
);
3639 struct isl_set
*isl_basic_set_union(
3640 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
3642 return (struct isl_set
*)isl_basic_map_union(
3643 (struct isl_basic_map
*)bset1
,
3644 (struct isl_basic_map
*)bset2
);
3647 /* Order divs such that any div only depends on previous divs */
3648 static struct isl_basic_map
*order_divs(struct isl_basic_map
*bmap
)
3651 unsigned off
= isl_dim_total(bmap
->dim
);
3653 for (i
= 0; i
< bmap
->n_div
; ++i
) {
3655 pos
= isl_seq_first_non_zero(bmap
->div
[i
]+1+1+off
+i
,
3659 swap_div(bmap
, i
, i
+ pos
);
3665 /* Look for a div in dst that corresponds to the div "div" in src.
3666 * The divs before "div" in src and dst are assumed to be the same.
3668 * Returns -1 if no corresponding div was found and the position
3669 * of the corresponding div in dst otherwise.
3671 static int find_div(struct isl_basic_map
*dst
,
3672 struct isl_basic_map
*src
, unsigned div
)
3676 unsigned total
= isl_dim_total(src
->dim
);
3678 isl_assert(dst
->ctx
, div
<= dst
->n_div
, return -1);
3679 for (i
= div
; i
< dst
->n_div
; ++i
)
3680 if (isl_seq_eq(dst
->div
[i
], src
->div
[div
], 1+1+total
+div
) &&
3681 isl_seq_first_non_zero(dst
->div
[i
]+1+1+total
+div
,
3682 dst
->n_div
- div
) == -1)
3687 struct isl_basic_map
*isl_basic_map_align_divs(
3688 struct isl_basic_map
*dst
, struct isl_basic_map
*src
)
3691 unsigned total
= isl_dim_total(src
->dim
);
3696 if (src
->n_div
== 0)
3699 for (i
= 0; i
< src
->n_div
; ++i
)
3700 isl_assert(src
->ctx
, !isl_int_is_zero(src
->div
[i
][0]), goto error
);
3702 src
= order_divs(src
);
3703 dst
= isl_basic_map_cow(dst
);
3704 dst
= isl_basic_map_extend_dim(dst
, isl_dim_copy(dst
->dim
),
3705 src
->n_div
, 0, 2 * src
->n_div
);
3708 for (i
= 0; i
< src
->n_div
; ++i
) {
3709 int j
= find_div(dst
, src
, i
);
3711 j
= isl_basic_map_alloc_div(dst
);
3714 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
+i
);
3715 isl_seq_clr(dst
->div
[j
]+1+1+total
+i
, dst
->n_div
- i
);
3716 if (add_div_constraints(dst
, j
) < 0)
3720 swap_div(dst
, i
, j
);
3724 isl_basic_map_free(dst
);
3728 struct isl_basic_set
*isl_basic_set_align_divs(
3729 struct isl_basic_set
*dst
, struct isl_basic_set
*src
)
3731 return (struct isl_basic_set
*)isl_basic_map_align_divs(
3732 (struct isl_basic_map
*)dst
, (struct isl_basic_map
*)src
);
3735 struct isl_map
*isl_map_align_divs(struct isl_map
*map
)
3739 map
= isl_map_compute_divs(map
);
3740 map
= isl_map_cow(map
);
3744 for (i
= 1; i
< map
->n
; ++i
)
3745 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], map
->p
[i
]);
3746 for (i
= 1; i
< map
->n
; ++i
)
3747 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], map
->p
[0]);
3749 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3753 struct isl_set
*isl_set_align_divs(struct isl_set
*set
)
3755 return (struct isl_set
*)isl_map_align_divs((struct isl_map
*)set
);
3758 static struct isl_map
*add_cut_constraint(struct isl_map
*dst
,
3759 struct isl_basic_map
*src
, isl_int
*c
,
3760 unsigned len
, int oppose
)
3762 struct isl_basic_map
*copy
= NULL
;
3767 copy
= isl_basic_map_copy(src
);
3768 copy
= isl_basic_map_cow(copy
);
3771 copy
= isl_basic_map_extend_constraints(copy
, 0, 1);
3772 k
= isl_basic_map_alloc_inequality(copy
);
3776 isl_seq_neg(copy
->ineq
[k
], c
, len
);
3778 isl_seq_cpy(copy
->ineq
[k
], c
, len
);
3779 total
= 1 + isl_basic_map_total_dim(copy
);
3780 isl_seq_clr(copy
->ineq
[k
]+len
, total
- len
);
3781 isl_inequality_negate(copy
, k
);
3782 copy
= isl_basic_map_simplify(copy
);
3783 copy
= isl_basic_map_finalize(copy
);
3784 is_empty
= isl_basic_map_is_empty(copy
);
3788 dst
= isl_map_add(dst
, copy
);
3790 isl_basic_map_free(copy
);
3793 isl_basic_map_free(copy
);
3798 static struct isl_map
*subtract(struct isl_map
*map
, struct isl_basic_map
*bmap
)
3802 struct isl_map
*rest
= NULL
;
3804 unsigned total
= isl_basic_map_total_dim(bmap
);
3811 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
3812 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
3814 max
= map
->n
* (2 * bmap
->n_eq
+ bmap
->n_ineq
);
3815 rest
= isl_map_alloc_dim(isl_dim_copy(map
->dim
), max
, flags
);
3819 for (i
= 0; i
< map
->n
; ++i
) {
3820 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], bmap
);
3825 for (j
= 0; j
< map
->n
; ++j
)
3826 map
->p
[j
] = isl_basic_map_cow(map
->p
[j
]);
3828 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
3829 for (j
= 0; j
< map
->n
; ++j
) {
3830 rest
= add_cut_constraint(rest
,
3831 map
->p
[j
], bmap
->eq
[i
], 1+total
, 0);
3835 rest
= add_cut_constraint(rest
,
3836 map
->p
[j
], bmap
->eq
[i
], 1+total
, 1);
3840 map
->p
[j
] = isl_basic_map_extend_constraints(map
->p
[j
],
3844 k
= isl_basic_map_alloc_equality(map
->p
[j
]);
3847 isl_seq_cpy(map
->p
[j
]->eq
[k
], bmap
->eq
[i
], 1+total
);
3848 isl_seq_clr(map
->p
[j
]->eq
[k
]+1+total
,
3849 map
->p
[j
]->n_div
- bmap
->n_div
);
3853 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
3854 for (j
= 0; j
< map
->n
; ++j
) {
3855 rest
= add_cut_constraint(rest
,
3856 map
->p
[j
], bmap
->ineq
[i
], 1+total
, 0);
3860 map
->p
[j
] = isl_basic_map_extend_constraints(map
->p
[j
],
3864 k
= isl_basic_map_alloc_inequality(map
->p
[j
]);
3867 isl_seq_cpy(map
->p
[j
]->ineq
[k
], bmap
->ineq
[i
], 1+total
);
3868 isl_seq_clr(map
->p
[j
]->ineq
[k
]+1+total
,
3869 map
->p
[j
]->n_div
- bmap
->n_div
);
3881 struct isl_map
*isl_map_subtract(struct isl_map
*map1
, struct isl_map
*map2
)
3887 isl_assert(map1
->ctx
, isl_dim_equal(map1
->dim
, map2
->dim
), goto error
);
3889 if (isl_map_is_empty(map2
)) {
3894 map1
= isl_map_compute_divs(map1
);
3895 map2
= isl_map_compute_divs(map2
);
3899 for (i
= 0; map1
&& i
< map2
->n
; ++i
)
3900 map1
= subtract(map1
, map2
->p
[i
]);
3910 struct isl_set
*isl_set_subtract(struct isl_set
*set1
, struct isl_set
*set2
)
3912 return (struct isl_set
*)
3914 (struct isl_map
*)set1
, (struct isl_map
*)set2
);
3917 struct isl_set
*isl_set_apply(struct isl_set
*set
, struct isl_map
*map
)
3921 isl_assert(set
->ctx
, isl_map_compatible_domain(map
, set
), goto error
);
3922 map
= isl_map_intersect_domain(map
, set
);
3923 set
= isl_map_range(map
);
3931 /* There is no need to cow as removing empty parts doesn't change
3932 * the meaning of the set.
3934 struct isl_map
*isl_map_remove_empty_parts(struct isl_map
*map
)
3941 for (i
= map
->n
-1; i
>= 0; --i
) {
3942 if (!ISL_F_ISSET(map
->p
[i
], ISL_BASIC_MAP_EMPTY
))
3944 isl_basic_map_free(map
->p
[i
]);
3945 if (i
!= map
->n
-1) {
3946 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3947 map
->p
[i
] = map
->p
[map
->n
-1];
3955 struct isl_set
*isl_set_remove_empty_parts(struct isl_set
*set
)
3957 return (struct isl_set
*)
3958 isl_map_remove_empty_parts((struct isl_map
*)set
);
3961 struct isl_basic_map
*isl_map_copy_basic_map(struct isl_map
*map
)
3963 struct isl_basic_map
*bmap
;
3964 if (!map
|| map
->n
== 0)
3966 bmap
= map
->p
[map
->n
-1];
3967 isl_assert(map
->ctx
, ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
), return NULL
);
3968 return isl_basic_map_copy(bmap
);
3971 struct isl_basic_set
*isl_set_copy_basic_set(struct isl_set
*set
)
3973 (struct isl_basic_set
*)isl_map_copy_basic_map((struct isl_map
*)set
);
3976 struct isl_map
*isl_map_drop_basic_map(struct isl_map
*map
,
3977 struct isl_basic_map
*bmap
)
3983 for (i
= map
->n
-1; i
>= 0; --i
) {
3984 if (map
->p
[i
] != bmap
)
3986 map
= isl_map_cow(map
);
3989 isl_basic_map_free(map
->p
[i
]);
3990 if (i
!= map
->n
-1) {
3991 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
3992 map
->p
[i
] = map
->p
[map
->n
-1];
3997 isl_basic_map_free(bmap
);
4001 isl_basic_map_free(bmap
);
4005 struct isl_set
*isl_set_drop_basic_set(struct isl_set
*set
,
4006 struct isl_basic_set
*bset
)
4008 (struct isl_set
*)isl_map_drop_basic_map((struct isl_map
*)set
,
4009 (struct isl_basic_map
*)bset
);
4012 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
4013 * for any common value of the parameters and dimensions preceding dim
4014 * in both basic sets, the values of dimension pos in bset1 are
4015 * smaller or larger than those in bset2.
4018 * 1 if bset1 follows bset2
4019 * -1 if bset1 precedes bset2
4020 * 0 if bset1 and bset2 are incomparable
4021 * -2 if some error occurred.
4023 int isl_basic_set_compare_at(struct isl_basic_set
*bset1
,
4024 struct isl_basic_set
*bset2
, int pos
)
4026 struct isl_dim
*dims
;
4027 struct isl_basic_map
*bmap1
= NULL
;
4028 struct isl_basic_map
*bmap2
= NULL
;
4029 struct isl_ctx
*ctx
;
4030 struct isl_vec
*obj
;
4033 unsigned dim1
, dim2
;
4035 enum isl_lp_result res
;
4038 if (!bset1
|| !bset2
)
4041 nparam
= isl_basic_set_n_param(bset1
);
4042 dim1
= isl_basic_set_n_dim(bset1
);
4043 dim2
= isl_basic_set_n_dim(bset2
);
4044 dims
= isl_dim_alloc(bset1
->ctx
, nparam
, pos
, dim1
- pos
);
4045 bmap1
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset1
), dims
);
4046 dims
= isl_dim_alloc(bset2
->ctx
, nparam
, pos
, dim2
- pos
);
4047 bmap2
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset2
), dims
);
4048 if (!bmap1
|| !bmap2
)
4050 bmap1
= isl_basic_map_cow(bmap1
);
4051 bmap1
= isl_basic_map_extend(bmap1
, nparam
,
4052 pos
, (dim1
- pos
) + (dim2
- pos
),
4053 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
4054 bmap1
= add_constraints(bmap1
, bmap2
, 0, dim1
- pos
);
4057 total
= isl_basic_map_total_dim(bmap1
);
4059 obj
= isl_vec_alloc(ctx
, 1 + total
);
4060 isl_seq_clr(obj
->block
.data
, 1 + total
);
4061 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
], 1);
4062 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
+(dim1
-pos
)], -1);
4067 res
= isl_solve_lp(bmap1
, 0, obj
->block
.data
, ctx
->one
, &num
, &den
);
4068 if (res
== isl_lp_empty
)
4070 else if (res
== isl_lp_ok
&& isl_int_is_pos(num
))
4072 else if ((res
== isl_lp_ok
&& isl_int_is_neg(num
)) ||
4073 res
== isl_lp_unbounded
)
4079 isl_basic_map_free(bmap1
);
4083 isl_basic_map_free(bmap1
);
4084 isl_basic_map_free(bmap2
);
4088 static int isl_basic_map_fast_has_fixed_var(struct isl_basic_map
*bmap
,
4089 unsigned pos
, isl_int
*val
)
4097 total
= isl_basic_map_total_dim(bmap
);
4098 for (i
= 0, d
= total
-1; i
< bmap
->n_eq
&& d
+1 > pos
; ++i
) {
4099 for (; d
+1 > pos
; --d
)
4100 if (!isl_int_is_zero(bmap
->eq
[i
][1+d
]))
4104 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1, d
) != -1)
4106 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1+d
+1, total
-d
-1) != -1)
4108 if (!isl_int_is_one(bmap
->eq
[i
][1+d
]))
4111 isl_int_neg(*val
, bmap
->eq
[i
][0]);
4117 static int isl_map_fast_has_fixed_var(struct isl_map
*map
,
4118 unsigned pos
, isl_int
*val
)
4130 return isl_basic_map_fast_has_fixed_var(map
->p
[0], pos
, val
);
4133 fixed
= isl_basic_map_fast_has_fixed_var(map
->p
[0], pos
, &v
);
4134 for (i
= 1; fixed
== 1 && i
< map
->n
; ++i
) {
4135 fixed
= isl_basic_map_fast_has_fixed_var(map
->p
[i
], pos
, &tmp
);
4136 if (fixed
== 1 && isl_int_ne(tmp
, v
))
4140 isl_int_set(*val
, v
);
4146 static int isl_basic_set_fast_has_fixed_var(struct isl_basic_set
*bset
,
4147 unsigned pos
, isl_int
*val
)
4149 return isl_basic_map_fast_has_fixed_var((struct isl_basic_map
*)bset
,
4153 static int isl_set_fast_has_fixed_var(struct isl_set
*set
, unsigned pos
,
4156 return isl_map_fast_has_fixed_var((struct isl_map
*)set
, pos
, val
);
4159 int isl_basic_map_fast_is_fixed(struct isl_basic_map
*bmap
,
4160 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
4162 if (pos
>= isl_basic_map_dim(bmap
, type
))
4164 return isl_basic_map_fast_has_fixed_var(bmap
,
4165 isl_basic_map_offset(bmap
, type
) - 1 + pos
, val
);
4168 /* Check if dimension dim has fixed value and if so and if val is not NULL,
4169 * then return this fixed value in *val.
4171 int isl_basic_set_fast_dim_is_fixed(struct isl_basic_set
*bset
, unsigned dim
,
4174 return isl_basic_set_fast_has_fixed_var(bset
,
4175 isl_basic_set_n_param(bset
) + dim
, val
);
4178 /* Check if dimension dim has fixed value and if so and if val is not NULL,
4179 * then return this fixed value in *val.
4181 int isl_set_fast_dim_is_fixed(struct isl_set
*set
, unsigned dim
, isl_int
*val
)
4183 return isl_set_fast_has_fixed_var(set
, isl_set_n_param(set
) + dim
, val
);
4186 /* Check if input variable in has fixed value and if so and if val is not NULL,
4187 * then return this fixed value in *val.
4189 int isl_map_fast_input_is_fixed(struct isl_map
*map
, unsigned in
, isl_int
*val
)
4191 return isl_map_fast_has_fixed_var(map
, isl_map_n_param(map
) + in
, val
);
4194 /* Check if dimension dim has an (obvious) fixed lower bound and if so
4195 * and if val is not NULL, then return this lower bound in *val.
4197 int isl_basic_set_fast_dim_has_fixed_lower_bound(struct isl_basic_set
*bset
,
4198 unsigned dim
, isl_int
*val
)
4200 int i
, i_eq
= -1, i_ineq
= -1;
4207 total
= isl_basic_set_total_dim(bset
);
4208 nparam
= isl_basic_set_n_param(bset
);
4209 for (i
= 0; i
< bset
->n_eq
; ++i
) {
4210 if (isl_int_is_zero(bset
->eq
[i
][1+nparam
+dim
]))
4216 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
4217 if (!isl_int_is_pos(bset
->ineq
[i
][1+nparam
+dim
]))
4219 if (i_eq
!= -1 || i_ineq
!= -1)
4223 if (i_eq
== -1 && i_ineq
== -1)
4225 c
= i_eq
!= -1 ? bset
->eq
[i_eq
] : bset
->ineq
[i_ineq
];
4226 /* The coefficient should always be one due to normalization. */
4227 if (!isl_int_is_one(c
[1+nparam
+dim
]))
4229 if (isl_seq_first_non_zero(c
+1, nparam
+dim
) != -1)
4231 if (isl_seq_first_non_zero(c
+1+nparam
+dim
+1,
4232 total
- nparam
- dim
- 1) != -1)
4235 isl_int_neg(*val
, c
[0]);
4239 int isl_set_fast_dim_has_fixed_lower_bound(struct isl_set
*set
,
4240 unsigned dim
, isl_int
*val
)
4252 return isl_basic_set_fast_dim_has_fixed_lower_bound(set
->p
[0],
4256 fixed
= isl_basic_set_fast_dim_has_fixed_lower_bound(set
->p
[0],
4258 for (i
= 1; fixed
== 1 && i
< set
->n
; ++i
) {
4259 fixed
= isl_basic_set_fast_dim_has_fixed_lower_bound(set
->p
[i
],
4261 if (fixed
== 1 && isl_int_ne(tmp
, v
))
4265 isl_int_set(*val
, v
);
4276 static int qsort_constraint_cmp(const void *p1
, const void *p2
)
4278 const struct constraint
*c1
= (const struct constraint
*)p1
;
4279 const struct constraint
*c2
= (const struct constraint
*)p2
;
4280 unsigned size
= isl_min(c1
->size
, c2
->size
);
4281 return isl_seq_cmp(c1
->c
, c2
->c
, size
);
4284 static struct isl_basic_map
*isl_basic_map_sort_constraints(
4285 struct isl_basic_map
*bmap
)
4288 struct constraint
*c
;
4293 total
= isl_basic_map_total_dim(bmap
);
4294 c
= isl_alloc_array(bmap
->ctx
, struct constraint
, bmap
->n_ineq
);
4297 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
4299 c
[i
].c
= bmap
->ineq
[i
];
4301 qsort(c
, bmap
->n_ineq
, sizeof(struct constraint
), qsort_constraint_cmp
);
4302 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
4303 bmap
->ineq
[i
] = c
[i
].c
;
4307 isl_basic_map_free(bmap
);
4311 struct isl_basic_map
*isl_basic_map_normalize(struct isl_basic_map
*bmap
)
4315 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED
))
4317 bmap
= isl_basic_map_convex_hull(bmap
);
4318 bmap
= isl_basic_map_sort_constraints(bmap
);
4319 ISL_F_SET(bmap
, ISL_BASIC_MAP_NORMALIZED
);
4323 struct isl_basic_set
*isl_basic_set_normalize(struct isl_basic_set
*bset
)
4325 return (struct isl_basic_set
*)isl_basic_map_normalize(
4326 (struct isl_basic_map
*)bset
);
4329 static int isl_basic_map_fast_cmp(const struct isl_basic_map
*bmap1
,
4330 const struct isl_basic_map
*bmap2
)
4337 if (isl_basic_map_n_param(bmap1
) != isl_basic_map_n_param(bmap2
))
4338 return isl_basic_map_n_param(bmap1
) - isl_basic_map_n_param(bmap2
);
4339 if (isl_basic_map_n_in(bmap1
) != isl_basic_map_n_in(bmap2
))
4340 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
4341 if (isl_basic_map_n_out(bmap1
) != isl_basic_map_n_out(bmap2
))
4342 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
4343 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
) &&
4344 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
4346 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
))
4348 if (ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
4350 if (bmap1
->n_eq
!= bmap2
->n_eq
)
4351 return bmap1
->n_eq
- bmap2
->n_eq
;
4352 if (bmap1
->n_ineq
!= bmap2
->n_ineq
)
4353 return bmap1
->n_ineq
- bmap2
->n_ineq
;
4354 if (bmap1
->n_div
!= bmap2
->n_div
)
4355 return bmap1
->n_div
- bmap2
->n_div
;
4356 total
= isl_basic_map_total_dim(bmap1
);
4357 for (i
= 0; i
< bmap1
->n_eq
; ++i
) {
4358 cmp
= isl_seq_cmp(bmap1
->eq
[i
], bmap2
->eq
[i
], 1+total
);
4362 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
4363 cmp
= isl_seq_cmp(bmap1
->ineq
[i
], bmap2
->ineq
[i
], 1+total
);
4367 for (i
= 0; i
< bmap1
->n_div
; ++i
) {
4368 cmp
= isl_seq_cmp(bmap1
->div
[i
], bmap2
->div
[i
], 1+1+total
);
4375 static int isl_basic_map_fast_is_equal(struct isl_basic_map
*bmap1
,
4376 struct isl_basic_map
*bmap2
)
4378 return isl_basic_map_fast_cmp(bmap1
, bmap2
) == 0;
4381 static int qsort_bmap_cmp(const void *p1
, const void *p2
)
4383 const struct isl_basic_map
*bmap1
= *(const struct isl_basic_map
**)p1
;
4384 const struct isl_basic_map
*bmap2
= *(const struct isl_basic_map
**)p2
;
4386 return isl_basic_map_fast_cmp(bmap1
, bmap2
);
4389 /* We normalize in place, but if anything goes wrong we need
4390 * to return NULL, so we need to make sure we don't change the
4391 * meaning of any possible other copies of map.
4393 struct isl_map
*isl_map_normalize(struct isl_map
*map
)
4396 struct isl_basic_map
*bmap
;
4400 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
))
4402 for (i
= 0; i
< map
->n
; ++i
) {
4403 bmap
= isl_basic_map_normalize(isl_basic_map_copy(map
->p
[i
]));
4406 isl_basic_map_free(map
->p
[i
]);
4409 qsort(map
->p
, map
->n
, sizeof(struct isl_basic_map
*), qsort_bmap_cmp
);
4410 ISL_F_SET(map
, ISL_MAP_NORMALIZED
);
4411 map
= isl_map_remove_empty_parts(map
);
4414 for (i
= map
->n
- 1; i
>= 1; --i
) {
4415 if (!isl_basic_map_fast_is_equal(map
->p
[i
-1], map
->p
[i
]))
4417 isl_basic_map_free(map
->p
[i
-1]);
4418 for (j
= i
; j
< map
->n
; ++j
)
4419 map
->p
[j
-1] = map
->p
[j
];
4429 struct isl_set
*isl_set_normalize(struct isl_set
*set
)
4431 return (struct isl_set
*)isl_map_normalize((struct isl_map
*)set
);
4434 int isl_map_fast_is_equal(struct isl_map
*map1
, struct isl_map
*map2
)
4444 if (!isl_dim_equal(map1
->dim
, map2
->dim
))
4447 map1
= isl_map_copy(map1
);
4448 map2
= isl_map_copy(map2
);
4449 map1
= isl_map_normalize(map1
);
4450 map2
= isl_map_normalize(map2
);
4453 equal
= map1
->n
== map2
->n
;
4454 for (i
= 0; equal
&& i
< map1
->n
; ++i
) {
4455 equal
= isl_basic_map_fast_is_equal(map1
->p
[i
], map2
->p
[i
]);
4468 int isl_set_fast_is_equal(struct isl_set
*set1
, struct isl_set
*set2
)
4470 return isl_map_fast_is_equal((struct isl_map
*)set1
,
4471 (struct isl_map
*)set2
);
4474 /* Return an interval that ranges from min to max (inclusive)
4476 struct isl_basic_set
*isl_basic_set_interval(struct isl_ctx
*ctx
,
4477 isl_int min
, isl_int max
)
4480 struct isl_basic_set
*bset
= NULL
;
4482 bset
= isl_basic_set_alloc(ctx
, 0, 1, 0, 0, 2);
4486 k
= isl_basic_set_alloc_inequality(bset
);
4489 isl_int_set_si(bset
->ineq
[k
][1], 1);
4490 isl_int_neg(bset
->ineq
[k
][0], min
);
4492 k
= isl_basic_set_alloc_inequality(bset
);
4495 isl_int_set_si(bset
->ineq
[k
][1], -1);
4496 isl_int_set(bset
->ineq
[k
][0], max
);
4500 isl_basic_set_free(bset
);
4504 /* Return the Cartesian product of the basic sets in list (in the given order).
4506 struct isl_basic_set
*isl_basic_set_product(struct isl_basic_set_list
*list
)
4514 struct isl_basic_set
*product
= NULL
;
4518 isl_assert(list
->ctx
, list
->n
> 0, goto error
);
4519 isl_assert(list
->ctx
, list
->p
[0], goto error
);
4520 nparam
= isl_basic_set_n_param(list
->p
[0]);
4521 dim
= isl_basic_set_n_dim(list
->p
[0]);
4522 extra
= list
->p
[0]->n_div
;
4523 n_eq
= list
->p
[0]->n_eq
;
4524 n_ineq
= list
->p
[0]->n_ineq
;
4525 for (i
= 1; i
< list
->n
; ++i
) {
4526 isl_assert(list
->ctx
, list
->p
[i
], goto error
);
4527 isl_assert(list
->ctx
,
4528 nparam
== isl_basic_set_n_param(list
->p
[i
]), goto error
);
4529 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
4530 extra
+= list
->p
[i
]->n_div
;
4531 n_eq
+= list
->p
[i
]->n_eq
;
4532 n_ineq
+= list
->p
[i
]->n_ineq
;
4534 product
= isl_basic_set_alloc(list
->ctx
, nparam
, dim
, extra
,
4539 for (i
= 0; i
< list
->n
; ++i
) {
4540 isl_basic_set_add_constraints(product
,
4541 isl_basic_set_copy(list
->p
[i
]), dim
);
4542 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
4544 isl_basic_set_list_free(list
);
4547 isl_basic_set_free(product
);
4548 isl_basic_set_list_free(list
);
4552 struct isl_basic_map
*isl_basic_map_product(
4553 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
4555 struct isl_dim
*dim_result
= NULL
;
4556 struct isl_basic_map
*bmap
;
4557 unsigned in1
, in2
, out1
, out2
, nparam
, total
, pos
;
4558 struct isl_dim_map
*dim_map1
, *dim_map2
;
4560 if (!bmap1
|| !bmap2
)
4563 isl_assert(map1
->ctx
, isl_dim_match(bmap1
->dim
, isl_dim_param
,
4564 bmap2
->dim
, isl_dim_param
), goto error
);
4565 dim_result
= isl_dim_product(isl_dim_copy(bmap1
->dim
),
4566 isl_dim_copy(bmap2
->dim
));
4568 in1
= isl_basic_map_n_in(bmap1
);
4569 in2
= isl_basic_map_n_in(bmap2
);
4570 out1
= isl_basic_map_n_out(bmap1
);
4571 out2
= isl_basic_map_n_out(bmap2
);
4572 nparam
= isl_basic_map_n_param(bmap1
);
4574 total
= nparam
+ in1
+ in2
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
4575 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4576 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4577 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
4578 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
4579 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
4580 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
4581 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
4582 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
4583 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
4584 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
4586 bmap
= isl_basic_map_alloc_dim(dim_result
,
4587 bmap1
->n_div
+ bmap2
->n_div
,
4588 bmap1
->n_eq
+ bmap2
->n_eq
,
4589 bmap1
->n_ineq
+ bmap2
->n_ineq
);
4590 bmap
= add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
4591 bmap
= add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
4592 bmap
= isl_basic_map_simplify(bmap
);
4593 return isl_basic_map_finalize(bmap
);
4595 isl_basic_map_free(bmap1
);
4596 isl_basic_map_free(bmap2
);
4600 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
4602 struct isl_map
*isl_map_product(struct isl_map
*map1
, struct isl_map
*map2
)
4605 struct isl_map
*result
;
4611 isl_assert(map1
->ctx
, isl_dim_match(map1
->dim
, isl_dim_param
,
4612 map2
->dim
, isl_dim_param
), goto error
);
4614 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
4615 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
4616 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
4618 result
= isl_map_alloc_dim(isl_dim_product(isl_dim_copy(map1
->dim
),
4619 isl_dim_copy(map2
->dim
)),
4620 map1
->n
* map2
->n
, flags
);
4623 for (i
= 0; i
< map1
->n
; ++i
)
4624 for (j
= 0; j
< map2
->n
; ++j
) {
4625 struct isl_basic_map
*part
;
4626 part
= isl_basic_map_product(
4627 isl_basic_map_copy(map1
->p
[i
]),
4628 isl_basic_map_copy(map2
->p
[j
]));
4629 if (isl_basic_map_is_empty(part
))
4630 isl_basic_map_free(part
);
4632 result
= isl_map_add(result
, part
);
4645 /* Given two set A and B, construct its Cartesian product A x B.
4647 struct isl_set
*isl_set_product(struct isl_set
*set1
, struct isl_set
*set2
)
4649 return (struct isl_set
*)isl_map_product((struct isl_map
*)set1
,
4650 (struct isl_map
*)set2
);
4653 uint32_t isl_basic_set_get_hash(struct isl_basic_set
*bset
)
4661 bset
= isl_basic_set_copy(bset
);
4662 bset
= isl_basic_set_normalize(bset
);
4665 total
= isl_basic_set_total_dim(bset
);
4666 isl_hash_byte(hash
, bset
->n_eq
& 0xFF);
4667 for (i
= 0; i
< bset
->n_eq
; ++i
) {
4669 c_hash
= isl_seq_get_hash(bset
->eq
[i
], 1 + total
);
4670 isl_hash_hash(hash
, c_hash
);
4672 isl_hash_byte(hash
, bset
->n_ineq
& 0xFF);
4673 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
4675 c_hash
= isl_seq_get_hash(bset
->ineq
[i
], 1 + total
);
4676 isl_hash_hash(hash
, c_hash
);
4678 isl_hash_byte(hash
, bset
->n_div
& 0xFF);
4679 for (i
= 0; i
< bset
->n_div
; ++i
) {
4681 if (isl_int_is_zero(bset
->div
[i
][0]))
4683 isl_hash_byte(hash
, i
& 0xFF);
4684 c_hash
= isl_seq_get_hash(bset
->div
[i
], 1 + 1 + total
);
4685 isl_hash_hash(hash
, c_hash
);
4687 isl_basic_set_free(bset
);
4691 uint32_t isl_set_get_hash(struct isl_set
*set
)
4698 set
= isl_set_copy(set
);
4699 set
= isl_set_normalize(set
);
4703 hash
= isl_hash_init();
4704 for (i
= 0; i
< set
->n
; ++i
) {
4706 bset_hash
= isl_basic_set_get_hash(set
->p
[i
]);
4707 isl_hash_hash(hash
, bset_hash
);
4715 /* Check if the value for dimension dim is completely determined
4716 * by the values of the other parameters and variables.
4717 * That is, check if dimension dim is involved in an equality.
4719 int isl_basic_set_dim_is_unique(struct isl_basic_set
*bset
, unsigned dim
)
4726 nparam
= isl_basic_set_n_param(bset
);
4727 for (i
= 0; i
< bset
->n_eq
; ++i
)
4728 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ dim
]))
4733 /* Check if the value for dimension dim is completely determined
4734 * by the values of the other parameters and variables.
4735 * That is, check if dimension dim is involved in an equality
4736 * for each of the subsets.
4738 int isl_set_dim_is_unique(struct isl_set
*set
, unsigned dim
)
4744 for (i
= 0; i
< set
->n
; ++i
) {
4746 unique
= isl_basic_set_dim_is_unique(set
->p
[i
], dim
);