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
;
46 static unsigned pos(struct isl_dim
*dim
, enum isl_dim_type type
)
49 case isl_dim_param
: return 1;
50 case isl_dim_in
: return 1 + dim
->nparam
;
51 case isl_dim_out
: return 1 + dim
->nparam
+ dim
->n_in
;
55 static void isl_dim_map_dim(struct isl_dim_map
*dim_map
, struct isl_dim
*dim
,
56 enum isl_dim_type type
, unsigned dst_pos
)
64 src_pos
= pos(dim
, type
);
65 for (i
= 0; i
< n(dim
, type
); ++i
)
66 dim_map
->pos
[1 + dst_pos
+ i
] = src_pos
+ i
;
69 static void isl_dim_map_div(struct isl_dim_map
*dim_map
,
70 struct isl_basic_map
*bmap
, unsigned dst_pos
)
75 if (!dim_map
|| !bmap
)
78 src_pos
= 1 + isl_dim_total(bmap
->dim
);
79 for (i
= 0; i
< bmap
->n_div
; ++i
)
80 dim_map
->pos
[1 + dst_pos
+ i
] = src_pos
+ i
;
83 static void isl_dim_map_dump(struct isl_dim_map
*dim_map
)
87 for (i
= 0; i
< dim_map
->len
; ++i
)
88 fprintf(stderr
, "%d -> %d; ", i
, dim_map
->pos
[i
]);
89 fprintf(stderr
, "\n");
92 unsigned isl_basic_map_dim(const struct isl_basic_map
*bmap
,
93 enum isl_dim_type type
)
95 struct isl_dim
*dim
= bmap
->dim
;
99 case isl_dim_out
: return isl_dim_size(bmap
->dim
, type
);
100 case isl_dim_div
: return bmap
->n_div
;
101 case isl_dim_all
: return isl_basic_map_total_dim(bmap
);
105 unsigned isl_map_dim(const struct isl_map
*map
, enum isl_dim_type type
)
107 return n(map
->dim
, type
);
110 unsigned isl_set_dim(const struct isl_set
*set
, enum isl_dim_type type
)
112 return n(set
->dim
, type
);
115 unsigned isl_basic_map_offset(struct isl_basic_map
*bmap
,
116 enum isl_dim_type type
)
118 struct isl_dim
*dim
= bmap
->dim
;
120 case isl_dim_param
: return 1;
121 case isl_dim_in
: return 1 + dim
->nparam
;
122 case isl_dim_out
: return 1 + dim
->nparam
+ dim
->n_in
;
123 case isl_dim_div
: return 1 + dim
->nparam
+ dim
->n_in
+ dim
->n_out
;
127 static unsigned map_offset(struct isl_map
*map
, enum isl_dim_type type
)
129 return pos(map
->dim
, type
);
132 unsigned isl_basic_set_dim(const struct isl_basic_set
*bset
,
133 enum isl_dim_type type
)
135 return isl_basic_map_dim((const struct isl_basic_map
*)bset
, type
);
138 unsigned isl_basic_set_n_dim(const struct isl_basic_set
*bset
)
140 return bset
->dim
->n_out
;
143 unsigned isl_basic_set_n_param(const struct isl_basic_set
*bset
)
145 return bset
->dim
->nparam
;
148 unsigned isl_basic_set_total_dim(const struct isl_basic_set
*bset
)
150 return isl_dim_total(bset
->dim
) + bset
->n_div
;
153 unsigned isl_set_n_dim(const struct isl_set
*set
)
155 return set
->dim
->n_out
;
158 unsigned isl_set_n_param(const struct isl_set
*set
)
160 return set
->dim
->nparam
;
163 unsigned isl_basic_map_n_in(const struct isl_basic_map
*bmap
)
165 return bmap
->dim
->n_in
;
168 unsigned isl_basic_map_n_out(const struct isl_basic_map
*bmap
)
170 return bmap
->dim
->n_out
;
173 unsigned isl_basic_map_n_param(const struct isl_basic_map
*bmap
)
175 return bmap
->dim
->nparam
;
178 unsigned isl_basic_map_n_div(const struct isl_basic_map
*bmap
)
183 unsigned isl_basic_map_total_dim(const struct isl_basic_map
*bmap
)
185 return isl_dim_total(bmap
->dim
) + bmap
->n_div
;
188 unsigned isl_map_n_in(const struct isl_map
*map
)
190 return map
->dim
->n_in
;
193 unsigned isl_map_n_out(const struct isl_map
*map
)
195 return map
->dim
->n_out
;
198 unsigned isl_map_n_param(const struct isl_map
*map
)
200 return map
->dim
->nparam
;
203 int isl_map_compatible_domain(struct isl_map
*map
, struct isl_set
*set
)
205 return map
->dim
->n_in
== set
->dim
->n_out
&&
206 map
->dim
->nparam
== set
->dim
->nparam
;
209 int isl_basic_map_compatible_domain(struct isl_basic_map
*bmap
,
210 struct isl_basic_set
*bset
)
212 return bmap
->dim
->n_in
== bset
->dim
->n_out
&&
213 bmap
->dim
->nparam
== bset
->dim
->nparam
;
216 int isl_basic_map_compatible_range(struct isl_basic_map
*bmap
,
217 struct isl_basic_set
*bset
)
219 return bmap
->dim
->n_out
== bset
->dim
->n_out
&&
220 bmap
->dim
->nparam
== bset
->dim
->nparam
;
223 struct isl_dim
*isl_basic_set_get_dim(struct isl_basic_set
*bset
)
227 return isl_dim_copy(bset
->dim
);
230 struct isl_dim
*isl_set_get_dim(struct isl_set
*set
)
234 return isl_dim_copy(set
->dim
);
237 static struct isl_basic_map
*basic_map_init(struct isl_ctx
*ctx
,
238 struct isl_basic_map
*bmap
, unsigned extra
,
239 unsigned n_eq
, unsigned n_ineq
)
242 size_t row_size
= 1 + isl_dim_total(bmap
->dim
) + extra
;
244 bmap
->block
= isl_blk_alloc(ctx
, (n_ineq
+ n_eq
) * row_size
);
245 if (isl_blk_is_error(bmap
->block
)) {
250 bmap
->ineq
= isl_alloc_array(ctx
, isl_int
*, n_ineq
+ n_eq
);
252 isl_blk_free(ctx
, bmap
->block
);
258 bmap
->block2
= isl_blk_empty();
261 bmap
->block2
= isl_blk_alloc(ctx
, extra
* (1 + row_size
));
262 if (isl_blk_is_error(bmap
->block2
)) {
264 isl_blk_free(ctx
, bmap
->block
);
269 bmap
->div
= isl_alloc_array(ctx
, isl_int
*, extra
);
271 isl_blk_free(ctx
, bmap
->block2
);
273 isl_blk_free(ctx
, bmap
->block
);
279 for (i
= 0; i
< n_ineq
+ n_eq
; ++i
)
280 bmap
->ineq
[i
] = bmap
->block
.data
+ i
* row_size
;
282 for (i
= 0; i
< extra
; ++i
)
283 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + row_size
);
289 bmap
->c_size
= n_eq
+ n_ineq
;
290 bmap
->eq
= bmap
->ineq
+ n_ineq
;
299 isl_basic_map_free(bmap
);
303 struct isl_basic_set
*isl_basic_set_alloc(struct isl_ctx
*ctx
,
304 unsigned nparam
, unsigned dim
, unsigned extra
,
305 unsigned n_eq
, unsigned n_ineq
)
307 struct isl_basic_map
*bmap
;
308 bmap
= isl_basic_map_alloc(ctx
, nparam
, 0, dim
, extra
, n_eq
, n_ineq
);
309 return (struct isl_basic_set
*)bmap
;
312 struct isl_basic_set
*isl_basic_set_alloc_dim(struct isl_dim
*dim
,
313 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
315 struct isl_basic_map
*bmap
;
318 isl_assert(dim
->ctx
, dim
->n_in
== 0, return NULL
);
319 bmap
= isl_basic_map_alloc_dim(dim
, extra
, n_eq
, n_ineq
);
320 return (struct isl_basic_set
*)bmap
;
323 struct isl_basic_map
*isl_basic_map_alloc_dim(struct isl_dim
*dim
,
324 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
326 struct isl_basic_map
*bmap
;
330 bmap
= isl_alloc_type(dim
->ctx
, struct isl_basic_map
);
335 return basic_map_init(dim
->ctx
, bmap
, extra
, n_eq
, n_ineq
);
341 struct isl_basic_map
*isl_basic_map_alloc(struct isl_ctx
*ctx
,
342 unsigned nparam
, unsigned in
, unsigned out
, unsigned extra
,
343 unsigned n_eq
, unsigned n_ineq
)
345 struct isl_basic_map
*bmap
;
348 dim
= isl_dim_alloc(ctx
, nparam
, in
, out
);
352 bmap
= isl_basic_map_alloc_dim(dim
, extra
, n_eq
, n_ineq
);
356 static void dup_constraints(
357 struct isl_basic_map
*dst
, struct isl_basic_map
*src
)
360 unsigned total
= isl_basic_map_total_dim(src
);
362 for (i
= 0; i
< src
->n_eq
; ++i
) {
363 int j
= isl_basic_map_alloc_equality(dst
);
364 isl_seq_cpy(dst
->eq
[j
], src
->eq
[i
], 1+total
);
367 for (i
= 0; i
< src
->n_ineq
; ++i
) {
368 int j
= isl_basic_map_alloc_inequality(dst
);
369 isl_seq_cpy(dst
->ineq
[j
], src
->ineq
[i
], 1+total
);
372 for (i
= 0; i
< src
->n_div
; ++i
) {
373 int j
= isl_basic_map_alloc_div(dst
);
374 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
);
376 ISL_F_SET(dst
, ISL_BASIC_SET_FINAL
);
379 struct isl_basic_map
*isl_basic_map_dup(struct isl_basic_map
*bmap
)
381 struct isl_basic_map
*dup
;
385 dup
= isl_basic_map_alloc_dim(isl_dim_copy(bmap
->dim
),
386 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
389 dup_constraints(dup
, bmap
);
390 dup
->flags
= bmap
->flags
;
391 dup
->sample
= isl_vec_copy(bmap
->ctx
, bmap
->sample
);
395 struct isl_basic_set
*isl_basic_set_dup(struct isl_basic_set
*bset
)
397 struct isl_basic_map
*dup
;
399 dup
= isl_basic_map_dup((struct isl_basic_map
*)bset
);
400 return (struct isl_basic_set
*)dup
;
403 struct isl_basic_set
*isl_basic_set_copy(struct isl_basic_set
*bset
)
408 if (ISL_F_ISSET(bset
, ISL_BASIC_SET_FINAL
)) {
412 return isl_basic_set_dup(bset
);
415 struct isl_set
*isl_set_copy(struct isl_set
*set
)
424 struct isl_basic_map
*isl_basic_map_copy(struct isl_basic_map
*bmap
)
429 if (ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
)) {
433 return isl_basic_map_dup(bmap
);
436 struct isl_map
*isl_map_copy(struct isl_map
*map
)
445 void isl_basic_map_free(struct isl_basic_map
*bmap
)
453 isl_ctx_deref(bmap
->ctx
);
455 isl_blk_free(bmap
->ctx
, bmap
->block2
);
457 isl_blk_free(bmap
->ctx
, bmap
->block
);
458 isl_vec_free(bmap
->ctx
, bmap
->sample
);
459 isl_dim_free(bmap
->dim
);
463 void isl_basic_set_free(struct isl_basic_set
*bset
)
465 isl_basic_map_free((struct isl_basic_map
*)bset
);
468 static int room_for_con(struct isl_basic_map
*bmap
, unsigned n
)
470 return bmap
->n_eq
+ bmap
->n_ineq
+ n
<= bmap
->c_size
;
473 int isl_basic_map_alloc_equality(struct isl_basic_map
*bmap
)
479 isl_assert(ctx
, room_for_con(bmap
, 1), return -1);
480 isl_assert(ctx
, (bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
<= bmap
->c_size
,
482 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
483 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
484 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
485 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
486 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
487 if ((bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
== bmap
->c_size
) {
489 int j
= isl_basic_map_alloc_inequality(bmap
);
493 bmap
->ineq
[j
] = bmap
->ineq
[bmap
->n_ineq
- 1];
494 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
501 isl_seq_clr(bmap
->eq
[bmap
->n_eq
] + 1 + isl_basic_map_total_dim(bmap
),
502 bmap
->extra
- bmap
->n_div
);
506 int isl_basic_set_alloc_equality(struct isl_basic_set
*bset
)
508 return isl_basic_map_alloc_equality((struct isl_basic_map
*)bset
);
511 int isl_basic_map_free_equality(struct isl_basic_map
*bmap
, unsigned n
)
515 isl_assert(bmap
->ctx
, n
<= bmap
->n_eq
, return -1);
520 int isl_basic_set_free_equality(struct isl_basic_set
*bset
, unsigned n
)
522 return isl_basic_map_free_equality((struct isl_basic_map
*)bset
, n
);
525 int isl_basic_map_drop_equality(struct isl_basic_map
*bmap
, unsigned pos
)
530 isl_assert(bmap
->ctx
, pos
< bmap
->n_eq
, return -1);
532 if (pos
!= bmap
->n_eq
- 1) {
534 bmap
->eq
[pos
] = bmap
->eq
[bmap
->n_eq
- 1];
535 bmap
->eq
[bmap
->n_eq
- 1] = t
;
541 int isl_basic_set_drop_equality(struct isl_basic_set
*bset
, unsigned pos
)
543 return isl_basic_map_drop_equality((struct isl_basic_map
*)bset
, pos
);
546 void isl_basic_map_inequality_to_equality(
547 struct isl_basic_map
*bmap
, unsigned pos
)
552 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
553 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
558 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
559 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
560 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
561 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
564 static int room_for_ineq(struct isl_basic_map
*bmap
, unsigned n
)
566 return bmap
->n_ineq
+ n
<= bmap
->eq
- bmap
->ineq
;
569 int isl_basic_map_alloc_inequality(struct isl_basic_map
*bmap
)
575 isl_assert(ctx
, room_for_ineq(bmap
, 1), return -1);
576 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
577 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
578 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
579 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
580 isl_seq_clr(bmap
->ineq
[bmap
->n_ineq
] +
581 1 + isl_basic_map_total_dim(bmap
),
582 bmap
->extra
- bmap
->n_div
);
583 return bmap
->n_ineq
++;
586 int isl_basic_set_alloc_inequality(struct isl_basic_set
*bset
)
588 return isl_basic_map_alloc_inequality((struct isl_basic_map
*)bset
);
591 int isl_basic_map_free_inequality(struct isl_basic_map
*bmap
, unsigned n
)
595 isl_assert(bmap
->ctx
, n
<= bmap
->n_ineq
, return -1);
600 int isl_basic_set_free_inequality(struct isl_basic_set
*bset
, unsigned n
)
602 return isl_basic_map_free_inequality((struct isl_basic_map
*)bset
, n
);
605 int isl_basic_map_drop_inequality(struct isl_basic_map
*bmap
, unsigned pos
)
610 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
612 if (pos
!= bmap
->n_ineq
- 1) {
614 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
615 bmap
->ineq
[bmap
->n_ineq
- 1] = t
;
616 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
622 int isl_basic_set_drop_inequality(struct isl_basic_set
*bset
, unsigned pos
)
624 return isl_basic_map_drop_inequality((struct isl_basic_map
*)bset
, pos
);
627 int isl_basic_map_alloc_div(struct isl_basic_map
*bmap
)
631 isl_assert(bmap
->ctx
, bmap
->n_div
< bmap
->extra
, return -1);
632 isl_seq_clr(bmap
->div
[bmap
->n_div
] +
633 1 + 1 + isl_basic_map_total_dim(bmap
),
634 bmap
->extra
- bmap
->n_div
);
635 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
636 return bmap
->n_div
++;
639 int isl_basic_set_alloc_div(struct isl_basic_set
*bset
)
641 return isl_basic_map_alloc_div((struct isl_basic_map
*)bset
);
644 int isl_basic_map_free_div(struct isl_basic_map
*bmap
, unsigned n
)
648 isl_assert(bmap
->ctx
, n
<= bmap
->n_div
, return -1);
653 /* Copy constraint from src to dst, putting the vars of src at offset
654 * dim_off in dst and the divs of src at offset div_off in dst.
655 * If both sets are actually map, then dim_off applies to the input
658 static void copy_constraint(struct isl_basic_map
*dst_map
, isl_int
*dst
,
659 struct isl_basic_map
*src_map
, isl_int
*src
,
660 unsigned in_off
, unsigned out_off
, unsigned div_off
)
662 unsigned src_nparam
= isl_basic_map_n_param(src_map
);
663 unsigned dst_nparam
= isl_basic_map_n_param(dst_map
);
664 unsigned src_in
= isl_basic_map_n_in(src_map
);
665 unsigned dst_in
= isl_basic_map_n_in(dst_map
);
666 unsigned src_out
= isl_basic_map_n_out(src_map
);
667 unsigned dst_out
= isl_basic_map_n_out(dst_map
);
668 isl_int_set(dst
[0], src
[0]);
669 isl_seq_cpy(dst
+1, src
+1, isl_min(dst_nparam
, src_nparam
));
670 if (dst_nparam
> src_nparam
)
671 isl_seq_clr(dst
+1+src_nparam
,
672 dst_nparam
- src_nparam
);
673 isl_seq_clr(dst
+1+dst_nparam
, in_off
);
674 isl_seq_cpy(dst
+1+dst_nparam
+in_off
,
676 isl_min(dst_in
-in_off
, src_in
));
677 if (dst_in
-in_off
> src_in
)
678 isl_seq_clr(dst
+1+dst_nparam
+in_off
+src_in
,
679 dst_in
- in_off
- src_in
);
680 isl_seq_clr(dst
+1+dst_nparam
+dst_in
, out_off
);
681 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+out_off
,
682 src
+1+src_nparam
+src_in
,
683 isl_min(dst_out
-out_off
, src_out
));
684 if (dst_out
-out_off
> src_out
)
685 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+out_off
+src_out
,
686 dst_out
- out_off
- src_out
);
687 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
, div_off
);
688 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+dst_out
+div_off
,
689 src
+1+src_nparam
+src_in
+src_out
,
690 isl_min(dst_map
->extra
-div_off
, src_map
->n_div
));
691 if (dst_map
->n_div
-div_off
> src_map
->n_div
)
692 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
+
693 div_off
+src_map
->n_div
,
694 dst_map
->n_div
- div_off
- src_map
->n_div
);
697 static void copy_div(struct isl_basic_map
*dst_map
, isl_int
*dst
,
698 struct isl_basic_map
*src_map
, isl_int
*src
,
699 unsigned in_off
, unsigned out_off
, unsigned div_off
)
701 isl_int_set(dst
[0], src
[0]);
702 copy_constraint(dst_map
, dst
+1, src_map
, src
+1, in_off
, out_off
, div_off
);
705 static struct isl_basic_map
*add_constraints(struct isl_basic_map
*bmap1
,
706 struct isl_basic_map
*bmap2
, unsigned i_pos
, unsigned o_pos
)
711 if (!bmap1
|| !bmap2
)
714 div_off
= bmap1
->n_div
;
716 for (i
= 0; i
< bmap2
->n_eq
; ++i
) {
717 int i1
= isl_basic_map_alloc_equality(bmap1
);
720 copy_constraint(bmap1
, bmap1
->eq
[i1
], bmap2
, bmap2
->eq
[i
],
721 i_pos
, o_pos
, div_off
);
724 for (i
= 0; i
< bmap2
->n_ineq
; ++i
) {
725 int i1
= isl_basic_map_alloc_inequality(bmap1
);
728 copy_constraint(bmap1
, bmap1
->ineq
[i1
], bmap2
, bmap2
->ineq
[i
],
729 i_pos
, o_pos
, div_off
);
732 for (i
= 0; i
< bmap2
->n_div
; ++i
) {
733 int i1
= isl_basic_map_alloc_div(bmap1
);
736 copy_div(bmap1
, bmap1
->div
[i1
], bmap2
, bmap2
->div
[i
],
737 i_pos
, o_pos
, div_off
);
740 isl_basic_map_free(bmap2
);
745 isl_basic_map_free(bmap1
);
746 isl_basic_map_free(bmap2
);
750 static void copy_constraint_dim_map(isl_int
*dst
, isl_int
*src
,
751 struct isl_dim_map
*dim_map
)
755 for (i
= 0; i
< dim_map
->len
; ++i
) {
756 if (dim_map
->pos
[i
] < 0)
757 isl_int_set_si(dst
[i
], 0);
759 isl_int_set(dst
[i
], src
[dim_map
->pos
[i
]]);
763 static void copy_div_dim_map(isl_int
*dst
, isl_int
*src
,
764 struct isl_dim_map
*dim_map
)
766 isl_int_set(dst
[0], src
[0]);
767 copy_constraint_dim_map(dst
+1, src
+1, dim_map
);
770 static struct isl_basic_map
*add_constraints_dim_map(struct isl_basic_map
*dst
,
771 struct isl_basic_map
*src
, struct isl_dim_map
*dim_map
)
775 if (!src
|| !dst
|| !dim_map
)
778 for (i
= 0; i
< src
->n_eq
; ++i
) {
779 int i1
= isl_basic_map_alloc_equality(dst
);
782 copy_constraint_dim_map(dst
->eq
[i1
], src
->eq
[i
], dim_map
);
785 for (i
= 0; i
< src
->n_ineq
; ++i
) {
786 int i1
= isl_basic_map_alloc_inequality(dst
);
789 copy_constraint_dim_map(dst
->ineq
[i1
], src
->ineq
[i
], dim_map
);
792 for (i
= 0; i
< src
->n_div
; ++i
) {
793 int i1
= isl_basic_map_alloc_div(dst
);
796 copy_div_dim_map(dst
->div
[i1
], src
->div
[i
], dim_map
);
800 isl_basic_map_free(src
);
805 isl_basic_map_free(src
);
806 isl_basic_map_free(dst
);
810 struct isl_basic_set
*isl_basic_set_add_constraints(struct isl_basic_set
*bset1
,
811 struct isl_basic_set
*bset2
, unsigned pos
)
813 return (struct isl_basic_set
*)
814 add_constraints((struct isl_basic_map
*)bset1
,
815 (struct isl_basic_map
*)bset2
, 0, pos
);
818 struct isl_basic_map
*isl_basic_map_extend_dim(struct isl_basic_map
*base
,
819 struct isl_dim
*dim
, unsigned extra
,
820 unsigned n_eq
, unsigned n_ineq
)
822 struct isl_basic_map
*ext
;
832 dims_ok
= isl_dim_equal(base
->dim
, dim
) &&
833 base
->extra
>= base
->n_div
+ extra
;
835 if (dims_ok
&& room_for_con(base
, n_eq
+ n_ineq
) &&
836 room_for_ineq(base
, n_ineq
)) {
841 isl_assert(base
->ctx
, base
->dim
->nparam
<= dim
->nparam
, goto error
);
842 isl_assert(base
->ctx
, base
->dim
->n_in
<= dim
->n_in
, goto error
);
843 isl_assert(base
->ctx
, base
->dim
->n_out
<= dim
->n_out
, goto error
);
844 extra
+= base
->extra
;
846 n_ineq
+= base
->n_ineq
;
848 ext
= isl_basic_map_alloc_dim(dim
, extra
, n_eq
, n_ineq
);
854 ext
= add_constraints(ext
, base
, 0, 0);
857 ISL_F_CLR(ext
, ISL_BASIC_SET_FINAL
);
864 isl_basic_map_free(base
);
868 struct isl_basic_set
*isl_basic_set_extend_dim(struct isl_basic_set
*base
,
869 struct isl_dim
*dim
, unsigned extra
,
870 unsigned n_eq
, unsigned n_ineq
)
872 return (struct isl_basic_set
*)
873 isl_basic_map_extend_dim((struct isl_basic_map
*)base
, dim
,
874 extra
, n_eq
, n_ineq
);
877 struct isl_basic_map
*isl_basic_map_extend_constraints(
878 struct isl_basic_map
*base
, unsigned n_eq
, unsigned n_ineq
)
882 return isl_basic_map_extend_dim(base
, isl_dim_copy(base
->dim
),
886 struct isl_basic_map
*isl_basic_map_extend(struct isl_basic_map
*base
,
887 unsigned nparam
, unsigned n_in
, unsigned n_out
, unsigned extra
,
888 unsigned n_eq
, unsigned n_ineq
)
890 struct isl_basic_map
*bmap
;
895 dim
= isl_dim_alloc(base
->ctx
, nparam
, n_in
, n_out
);
899 bmap
= isl_basic_map_extend_dim(base
, dim
, extra
, n_eq
, n_ineq
);
903 struct isl_basic_set
*isl_basic_set_extend(struct isl_basic_set
*base
,
904 unsigned nparam
, unsigned dim
, unsigned extra
,
905 unsigned n_eq
, unsigned n_ineq
)
907 return (struct isl_basic_set
*)
908 isl_basic_map_extend((struct isl_basic_map
*)base
,
909 nparam
, 0, dim
, extra
, n_eq
, n_ineq
);
912 struct isl_basic_set
*isl_basic_set_extend_constraints(
913 struct isl_basic_set
*base
, unsigned n_eq
, unsigned n_ineq
)
915 return (struct isl_basic_set
*)
916 isl_basic_map_extend_constraints((struct isl_basic_map
*)base
,
920 struct isl_basic_set
*isl_basic_set_cow(struct isl_basic_set
*bset
)
922 return (struct isl_basic_set
*)
923 isl_basic_map_cow((struct isl_basic_map
*)bset
);
926 struct isl_basic_map
*isl_basic_map_cow(struct isl_basic_map
*bmap
)
933 bmap
= isl_basic_map_dup(bmap
);
935 ISL_F_CLR(bmap
, ISL_BASIC_SET_FINAL
);
939 struct isl_set
*isl_set_cow(struct isl_set
*set
)
947 return isl_set_dup(set
);
950 struct isl_map
*isl_map_cow(struct isl_map
*map
)
958 return isl_map_dup(map
);
961 static void swap_vars(struct isl_blk blk
, isl_int
*a
,
962 unsigned a_len
, unsigned b_len
)
964 isl_seq_cpy(blk
.data
, a
+a_len
, b_len
);
965 isl_seq_cpy(blk
.data
+b_len
, a
, a_len
);
966 isl_seq_cpy(a
, blk
.data
, b_len
+a_len
);
969 struct isl_basic_set
*isl_basic_set_swap_vars(
970 struct isl_basic_set
*bset
, unsigned n
)
980 nparam
= isl_basic_set_n_param(bset
);
981 dim
= isl_basic_set_n_dim(bset
);
982 isl_assert(bset
->ctx
, n
<= dim
, goto error
);
987 bset
= isl_basic_set_cow(bset
);
991 blk
= isl_blk_alloc(bset
->ctx
, dim
);
992 if (isl_blk_is_error(blk
))
995 for (i
= 0; i
< bset
->n_eq
; ++i
)
997 bset
->eq
[i
]+1+nparam
, n
, dim
- n
);
999 for (i
= 0; i
< bset
->n_ineq
; ++i
)
1001 bset
->ineq
[i
]+1+nparam
, n
, dim
- n
);
1003 for (i
= 0; i
< bset
->n_div
; ++i
)
1005 bset
->div
[i
]+1+1+nparam
, n
, dim
- n
);
1007 isl_blk_free(bset
->ctx
, blk
);
1009 ISL_F_CLR(bset
, ISL_BASIC_SET_NORMALIZED
);
1013 isl_basic_set_free(bset
);
1017 struct isl_set
*isl_set_swap_vars(struct isl_set
*set
, unsigned n
)
1020 set
= isl_set_cow(set
);
1024 for (i
= 0; i
< set
->n
; ++i
) {
1025 set
->p
[i
] = isl_basic_set_swap_vars(set
->p
[i
], n
);
1031 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
1035 struct isl_basic_map
*isl_basic_map_set_to_empty(struct isl_basic_map
*bmap
)
1041 total
= isl_basic_map_total_dim(bmap
);
1042 isl_basic_map_free_div(bmap
, bmap
->n_div
);
1043 isl_basic_map_free_inequality(bmap
, bmap
->n_ineq
);
1045 isl_basic_map_free_equality(bmap
, bmap
->n_eq
-1);
1047 isl_basic_map_alloc_equality(bmap
);
1051 isl_int_set_si(bmap
->eq
[i
][0], 1);
1052 isl_seq_clr(bmap
->eq
[i
]+1, total
);
1053 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
1054 return isl_basic_map_finalize(bmap
);
1056 isl_basic_map_free(bmap
);
1060 struct isl_basic_set
*isl_basic_set_set_to_empty(struct isl_basic_set
*bset
)
1062 return (struct isl_basic_set
*)
1063 isl_basic_map_set_to_empty((struct isl_basic_map
*)bset
);
1066 static void swap_div(struct isl_basic_map
*bmap
, int a
, int b
)
1069 unsigned off
= isl_dim_total(bmap
->dim
);
1070 isl_int
*t
= bmap
->div
[a
];
1071 bmap
->div
[a
] = bmap
->div
[b
];
1074 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1075 isl_int_swap(bmap
->eq
[i
][1+off
+a
], bmap
->eq
[i
][1+off
+b
]);
1077 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1078 isl_int_swap(bmap
->ineq
[i
][1+off
+a
], bmap
->ineq
[i
][1+off
+b
]);
1080 for (i
= 0; i
< bmap
->n_div
; ++i
)
1081 isl_int_swap(bmap
->div
[i
][1+1+off
+a
], bmap
->div
[i
][1+1+off
+b
]);
1082 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1085 /* Eliminate the specified n dimensions starting at first from the
1086 * constraints using Fourier-Motzkin, The dimensions themselves
1089 struct isl_set
*isl_set_eliminate_dims(struct isl_set
*set
,
1090 unsigned first
, unsigned n
)
1100 set
= isl_set_cow(set
);
1103 isl_assert(set
->ctx
, first
+n
<= isl_set_n_dim(set
), goto error
);
1104 nparam
= isl_set_n_param(set
);
1106 for (i
= 0; i
< set
->n
; ++i
) {
1107 set
->p
[i
] = isl_basic_set_eliminate_vars(set
->p
[i
],
1118 /* Project out n dimensions starting at first using Fourier-Motzkin */
1119 struct isl_set
*isl_set_remove_dims(struct isl_set
*set
,
1120 unsigned first
, unsigned n
)
1122 set
= isl_set_eliminate_dims(set
, first
, n
);
1123 set
= isl_set_drop_dims(set
, first
, n
);
1127 struct isl_basic_set
*isl_basic_set_remove_divs(struct isl_basic_set
*bset
)
1129 bset
= isl_basic_set_eliminate_vars(bset
, isl_dim_total(bset
->dim
),
1137 struct isl_set
*isl_set_remove_divs(struct isl_set
*set
)
1146 set
= isl_set_cow(set
);
1150 for (i
= 0; i
< set
->n
; ++i
) {
1151 set
->p
[i
] = isl_basic_set_remove_divs(set
->p
[i
]);
1161 struct isl_basic_map
*isl_basic_map_remove(struct isl_basic_map
*bmap
,
1162 enum isl_dim_type type
, unsigned first
, unsigned n
)
1166 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
1170 bmap
= isl_basic_map_eliminate_vars(bmap
,
1171 isl_basic_map_offset(bmap
, type
) - 1 + first
, n
);
1172 bmap
= isl_basic_map_drop(bmap
, type
, first
, n
);
1175 isl_basic_map_free(bmap
);
1179 struct isl_map
*isl_map_remove(struct isl_map
*map
,
1180 enum isl_dim_type type
, unsigned first
, unsigned n
)
1188 map
= isl_map_cow(map
);
1191 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
1193 for (i
= 0; i
< map
->n
; ++i
) {
1194 map
->p
[i
] = isl_basic_map_eliminate_vars(map
->p
[i
],
1195 isl_basic_map_offset(map
->p
[i
], type
) - 1 + first
, n
);
1199 map
= isl_map_drop(map
, type
, first
, n
);
1206 /* Project out n inputs starting at first using Fourier-Motzkin */
1207 struct isl_map
*isl_map_remove_inputs(struct isl_map
*map
,
1208 unsigned first
, unsigned n
)
1210 return isl_map_remove(map
, isl_dim_in
, first
, n
);
1213 /* Project out n dimensions starting at first using Fourier-Motzkin */
1214 struct isl_basic_set
*isl_basic_set_remove_dims(struct isl_basic_set
*bset
,
1215 unsigned first
, unsigned n
)
1217 unsigned nparam
= isl_basic_set_n_param(bset
);
1218 bset
= isl_basic_set_eliminate_vars(bset
, nparam
+ first
, n
);
1219 bset
= isl_basic_set_drop_dims(bset
, first
, n
);
1223 static void dump_term(struct isl_basic_map
*bmap
,
1224 isl_int c
, int pos
, FILE *out
)
1227 unsigned in
= isl_basic_map_n_in(bmap
);
1228 unsigned dim
= in
+ isl_basic_map_n_out(bmap
);
1229 unsigned nparam
= isl_basic_map_n_param(bmap
);
1231 isl_int_print(out
, c
, 0);
1233 if (!isl_int_is_one(c
))
1234 isl_int_print(out
, c
, 0);
1235 if (pos
< 1 + nparam
) {
1236 name
= isl_dim_get_name(bmap
->dim
,
1237 isl_dim_param
, pos
- 1);
1239 fprintf(out
, "%s", name
);
1241 fprintf(out
, "p%d", pos
- 1);
1242 } else if (pos
< 1 + nparam
+ in
)
1243 fprintf(out
, "i%d", pos
- 1 - nparam
);
1244 else if (pos
< 1 + nparam
+ dim
)
1245 fprintf(out
, "o%d", pos
- 1 - nparam
- in
);
1247 fprintf(out
, "e%d", pos
- 1 - nparam
- dim
);
1251 static void dump_constraint_sign(struct isl_basic_map
*bmap
, isl_int
*c
,
1252 int sign
, FILE *out
)
1256 unsigned len
= 1 + isl_basic_map_total_dim(bmap
);
1260 for (i
= 0, first
= 1; i
< len
; ++i
) {
1261 if (isl_int_sgn(c
[i
]) * sign
<= 0)
1264 fprintf(out
, " + ");
1266 isl_int_abs(v
, c
[i
]);
1267 dump_term(bmap
, v
, i
, out
);
1274 static void dump_constraint(struct isl_basic_map
*bmap
, isl_int
*c
,
1275 const char *op
, FILE *out
, int indent
)
1279 fprintf(out
, "%*s", indent
, "");
1281 dump_constraint_sign(bmap
, c
, 1, out
);
1282 fprintf(out
, " %s ", op
);
1283 dump_constraint_sign(bmap
, c
, -1, out
);
1287 for (i
= bmap
->n_div
; i
< bmap
->extra
; ++i
) {
1288 if (isl_int_is_zero(c
[1+isl_dim_total(bmap
->dim
)+i
]))
1290 fprintf(out
, "%*s", indent
, "");
1291 fprintf(out
, "ERROR: unused div coefficient not zero\n");
1296 static void dump_constraints(struct isl_basic_map
*bmap
,
1297 isl_int
**c
, unsigned n
,
1298 const char *op
, FILE *out
, int indent
)
1302 for (i
= 0; i
< n
; ++i
)
1303 dump_constraint(bmap
, c
[i
], op
, out
, indent
);
1306 static void dump_affine(struct isl_basic_map
*bmap
, isl_int
*exp
, FILE *out
)
1310 unsigned total
= isl_basic_map_total_dim(bmap
);
1312 for (j
= 0; j
< 1 + total
; ++j
) {
1313 if (isl_int_is_zero(exp
[j
]))
1315 if (!first
&& isl_int_is_pos(exp
[j
]))
1317 dump_term(bmap
, exp
[j
], j
, out
);
1322 static void dump(struct isl_basic_map
*bmap
, FILE *out
, int indent
)
1326 dump_constraints(bmap
, bmap
->eq
, bmap
->n_eq
, "=", out
, indent
);
1327 dump_constraints(bmap
, bmap
->ineq
, bmap
->n_ineq
, ">=", out
, indent
);
1329 for (i
= 0; i
< bmap
->n_div
; ++i
) {
1330 fprintf(out
, "%*s", indent
, "");
1331 fprintf(out
, "e%d = [(", i
);
1332 dump_affine(bmap
, bmap
->div
[i
]+1, out
);
1334 isl_int_print(out
, bmap
->div
[i
][0], 0);
1335 fprintf(out
, "]\n");
1339 void isl_basic_set_dump(struct isl_basic_set
*bset
, FILE *out
, int indent
)
1342 fprintf(out
, "null basic set\n");
1346 fprintf(out
, "%*s", indent
, "");
1347 fprintf(out
, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
1348 bset
->ref
, bset
->dim
->nparam
, bset
->dim
->n_out
,
1349 bset
->extra
, bset
->flags
);
1350 dump((struct isl_basic_map
*)bset
, out
, indent
);
1353 void isl_basic_map_dump(struct isl_basic_map
*bmap
, FILE *out
, int indent
)
1356 fprintf(out
, "null basic map\n");
1360 fprintf(out
, "%*s", indent
, "");
1361 fprintf(out
, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
1362 "flags: %x, n_name: %d\n",
1364 bmap
->dim
->nparam
, bmap
->dim
->n_in
, bmap
->dim
->n_out
,
1365 bmap
->extra
, bmap
->flags
, bmap
->dim
->n_name
);
1366 dump(bmap
, out
, indent
);
1369 int isl_inequality_negate(struct isl_basic_map
*bmap
, unsigned pos
)
1374 total
= isl_basic_map_total_dim(bmap
);
1375 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
1376 isl_seq_neg(bmap
->ineq
[pos
], bmap
->ineq
[pos
], 1 + total
);
1377 isl_int_sub_ui(bmap
->ineq
[pos
][0], bmap
->ineq
[pos
][0], 1);
1378 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1382 struct isl_set
*isl_set_alloc_dim(struct isl_dim
*dim
, int n
, unsigned flags
)
1384 struct isl_set
*set
;
1388 isl_assert(dim
->ctx
, dim
->n_in
== 0, return NULL
);
1389 isl_assert(dim
->ctx
, n
>= 0, return NULL
);
1390 set
= isl_alloc(dim
->ctx
, struct isl_set
,
1391 sizeof(struct isl_set
) +
1392 n
* sizeof(struct isl_basic_set
*));
1396 set
->ctx
= dim
->ctx
;
1397 isl_ctx_ref(set
->ctx
);
1409 struct isl_set
*isl_set_alloc(struct isl_ctx
*ctx
,
1410 unsigned nparam
, unsigned dim
, int n
, unsigned flags
)
1412 struct isl_set
*set
;
1413 struct isl_dim
*dims
;
1415 dims
= isl_dim_alloc(ctx
, nparam
, 0, dim
);
1419 set
= isl_set_alloc_dim(dims
, n
, flags
);
1423 struct isl_set
*isl_set_dup(struct isl_set
*set
)
1426 struct isl_set
*dup
;
1431 dup
= isl_set_alloc_dim(isl_dim_copy(set
->dim
), set
->n
, set
->flags
);
1434 for (i
= 0; i
< set
->n
; ++i
)
1435 dup
= isl_set_add(dup
, isl_basic_set_copy(set
->p
[i
]));
1439 struct isl_set
*isl_set_from_basic_set(struct isl_basic_set
*bset
)
1441 struct isl_set
*set
;
1446 set
= isl_set_alloc_dim(isl_dim_copy(bset
->dim
), 1, ISL_MAP_DISJOINT
);
1448 isl_basic_set_free(bset
);
1451 return isl_set_add(set
, bset
);
1454 struct isl_map
*isl_map_from_basic_map(struct isl_basic_map
*bmap
)
1456 struct isl_map
*map
;
1461 map
= isl_map_alloc_dim(isl_dim_copy(bmap
->dim
), 1, ISL_MAP_DISJOINT
);
1463 isl_basic_map_free(bmap
);
1466 return isl_map_add(map
, bmap
);
1469 struct isl_set
*isl_set_add(struct isl_set
*set
, struct isl_basic_set
*bset
)
1473 isl_assert(set
->ctx
, isl_dim_equal(set
->dim
, bset
->dim
), goto error
);
1474 isl_assert(set
->ctx
, set
->n
< set
->size
, goto error
);
1475 set
->p
[set
->n
] = bset
;
1482 isl_basic_set_free(bset
);
1486 void isl_set_free(struct isl_set
*set
)
1496 isl_ctx_deref(set
->ctx
);
1497 for (i
= 0; i
< set
->n
; ++i
)
1498 isl_basic_set_free(set
->p
[i
]);
1499 isl_dim_free(set
->dim
);
1503 void isl_set_dump(struct isl_set
*set
, FILE *out
, int indent
)
1508 fprintf(out
, "null set\n");
1512 fprintf(out
, "%*s", indent
, "");
1513 fprintf(out
, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
1514 set
->ref
, set
->n
, set
->dim
->nparam
, set
->dim
->n_out
,
1516 for (i
= 0; i
< set
->n
; ++i
) {
1517 fprintf(out
, "%*s", indent
, "");
1518 fprintf(out
, "basic set %d:\n", i
);
1519 isl_basic_set_dump(set
->p
[i
], out
, indent
+4);
1523 void isl_map_dump(struct isl_map
*map
, FILE *out
, int indent
)
1528 fprintf(out
, "null map\n");
1532 fprintf(out
, "%*s", indent
, "");
1533 fprintf(out
, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
1534 "flags: %x, n_name: %d\n",
1535 map
->ref
, map
->n
, map
->dim
->nparam
, map
->dim
->n_in
,
1536 map
->dim
->n_out
, map
->flags
, map
->dim
->n_name
);
1537 for (i
= 0; i
< map
->n
; ++i
) {
1538 fprintf(out
, "%*s", indent
, "");
1539 fprintf(out
, "basic map %d:\n", i
);
1540 isl_basic_map_dump(map
->p
[i
], out
, indent
+4);
1544 struct isl_basic_map
*isl_basic_map_intersect_domain(
1545 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
1547 struct isl_basic_map
*bmap_domain
;
1548 struct isl_dim
*dim
;
1553 isl_assert(bset
->ctx
, isl_dim_match(bmap
->dim
, isl_dim_param
,
1554 bset
->dim
, isl_dim_param
), goto error
);
1556 if (isl_dim_size(bset
->dim
, isl_dim_set
) != 0)
1557 isl_assert(bset
->ctx
,
1558 isl_basic_map_compatible_domain(bmap
, bset
), goto error
);
1560 bmap
= isl_basic_map_cow(bmap
);
1561 bmap
= isl_basic_map_extend_dim(bmap
, isl_dim_copy(bmap
->dim
),
1562 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
1565 dim
= isl_dim_reverse(isl_dim_copy(bset
->dim
));
1566 bmap_domain
= isl_basic_map_from_basic_set(bset
, dim
);
1567 bmap
= add_constraints(bmap
, bmap_domain
, 0, 0);
1569 bmap
= isl_basic_map_simplify(bmap
);
1570 return isl_basic_map_finalize(bmap
);
1572 isl_basic_map_free(bmap
);
1573 isl_basic_set_free(bset
);
1577 struct isl_basic_map
*isl_basic_map_intersect_range(
1578 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
1580 struct isl_basic_map
*bmap_range
;
1585 isl_assert(bset
->ctx
, isl_dim_match(bmap
->dim
, isl_dim_param
,
1586 bset
->dim
, isl_dim_param
), goto error
);
1588 if (isl_dim_size(bset
->dim
, isl_dim_set
) != 0)
1589 isl_assert(bset
->ctx
,
1590 isl_basic_map_compatible_range(bmap
, bset
), goto error
);
1592 bmap
= isl_basic_map_cow(bmap
);
1593 bmap
= isl_basic_map_extend_dim(bmap
, isl_dim_copy(bmap
->dim
),
1594 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
1597 bmap_range
= isl_basic_map_from_basic_set(bset
, isl_dim_copy(bset
->dim
));
1598 bmap
= add_constraints(bmap
, bmap_range
, 0, 0);
1600 bmap
= isl_basic_map_simplify(bmap
);
1601 return isl_basic_map_finalize(bmap
);
1603 isl_basic_map_free(bmap
);
1604 isl_basic_set_free(bset
);
1608 static int basic_map_contains(struct isl_basic_map
*bmap
, struct isl_vec
*vec
)
1614 total
= 1 + isl_basic_map_total_dim(bmap
);
1615 if (total
!= vec
->size
)
1620 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
1621 isl_seq_inner_product(vec
->block
.data
, bmap
->eq
[i
], total
, &s
);
1622 if (!isl_int_is_zero(s
)) {
1628 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
1629 isl_seq_inner_product(vec
->block
.data
, bmap
->ineq
[i
], total
, &s
);
1630 if (isl_int_is_neg(s
)) {
1641 int isl_basic_set_contains(struct isl_basic_set
*bset
, struct isl_vec
*vec
)
1643 return basic_map_contains((struct isl_basic_map
*)bset
, vec
);
1646 struct isl_basic_map
*isl_basic_map_intersect(
1647 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
1649 struct isl_vec
*sample
= NULL
;
1651 if (!bmap1
|| !bmap2
)
1654 isl_assert(bmap1
->ctx
, isl_dim_match(bmap1
->dim
, isl_dim_param
,
1655 bmap2
->dim
, isl_dim_param
), goto error
);
1656 if (isl_dim_total(bmap1
->dim
) ==
1657 isl_dim_size(bmap1
->dim
, isl_dim_param
) &&
1658 isl_dim_total(bmap2
->dim
) !=
1659 isl_dim_size(bmap2
->dim
, isl_dim_param
))
1660 return isl_basic_map_intersect(bmap2
, bmap1
);
1662 if (isl_dim_total(bmap2
->dim
) !=
1663 isl_dim_size(bmap2
->dim
, isl_dim_param
))
1664 isl_assert(bmap1
->ctx
,
1665 isl_dim_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
1667 if (bmap1
->sample
&&
1668 basic_map_contains(bmap1
, bmap1
->sample
) > 0 &&
1669 basic_map_contains(bmap2
, bmap1
->sample
) > 0)
1670 sample
= isl_vec_copy(bmap1
->ctx
, bmap1
->sample
);
1671 else if (bmap2
->sample
&&
1672 basic_map_contains(bmap1
, bmap2
->sample
) > 0 &&
1673 basic_map_contains(bmap2
, bmap2
->sample
) > 0)
1674 sample
= isl_vec_copy(bmap2
->ctx
, bmap2
->sample
);
1676 bmap1
= isl_basic_map_cow(bmap1
);
1677 bmap1
= isl_basic_map_extend_dim(bmap1
, isl_dim_copy(bmap1
->dim
),
1678 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
1681 bmap1
= add_constraints(bmap1
, bmap2
, 0, 0);
1684 isl_vec_free(bmap1
->ctx
, bmap1
->sample
);
1685 bmap1
->sample
= sample
;
1688 bmap1
= isl_basic_map_simplify(bmap1
);
1689 return isl_basic_map_finalize(bmap1
);
1692 isl_vec_free(bmap1
->ctx
, sample
);
1693 isl_basic_map_free(bmap1
);
1694 isl_basic_map_free(bmap2
);
1698 struct isl_basic_set
*isl_basic_set_intersect(
1699 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
1701 return (struct isl_basic_set
*)
1702 isl_basic_map_intersect(
1703 (struct isl_basic_map
*)bset1
,
1704 (struct isl_basic_map
*)bset2
);
1707 struct isl_map
*isl_map_intersect(struct isl_map
*map1
, struct isl_map
*map2
)
1710 struct isl_map
*result
;
1716 isl_assert(map1
->ctx
, isl_dim_match(map1
->dim
, isl_dim_param
,
1717 map2
->dim
, isl_dim_param
), goto error
);
1718 if (isl_dim_total(map1
->dim
) ==
1719 isl_dim_size(map1
->dim
, isl_dim_param
) &&
1720 isl_dim_total(map2
->dim
) != isl_dim_size(map2
->dim
, isl_dim_param
))
1721 return isl_map_intersect(map2
, map1
);
1723 if (isl_dim_total(map2
->dim
) != isl_dim_size(map2
->dim
, isl_dim_param
))
1724 isl_assert(map1
->ctx
,
1725 isl_dim_equal(map1
->dim
, map2
->dim
), goto error
);
1727 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
1728 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
1729 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
1731 result
= isl_map_alloc_dim(isl_dim_copy(map1
->dim
),
1732 map1
->n
* map2
->n
, flags
);
1735 for (i
= 0; i
< map1
->n
; ++i
)
1736 for (j
= 0; j
< map2
->n
; ++j
) {
1737 struct isl_basic_map
*part
;
1738 part
= isl_basic_map_intersect(
1739 isl_basic_map_copy(map1
->p
[i
]),
1740 isl_basic_map_copy(map2
->p
[j
]));
1741 if (isl_basic_map_is_empty(part
))
1742 isl_basic_map_free(part
);
1744 result
= isl_map_add(result
, part
);
1757 struct isl_set
*isl_set_intersect(struct isl_set
*set1
, struct isl_set
*set2
)
1759 return (struct isl_set
*)
1760 isl_map_intersect((struct isl_map
*)set1
,
1761 (struct isl_map
*)set2
);
1764 struct isl_basic_map
*isl_basic_map_reverse(struct isl_basic_map
*bmap
)
1766 struct isl_dim
*dim
;
1767 struct isl_basic_set
*bset
;
1772 bmap
= isl_basic_map_cow(bmap
);
1775 dim
= isl_dim_reverse(isl_dim_copy(bmap
->dim
));
1776 in
= isl_basic_map_n_in(bmap
);
1777 bset
= isl_basic_set_from_basic_map(bmap
);
1778 bset
= isl_basic_set_swap_vars(bset
, in
);
1779 return isl_basic_map_from_basic_set(bset
, dim
);
1782 /* Turn final n dimensions into existentially quantified variables.
1784 struct isl_basic_set
*isl_basic_set_project_out(
1785 struct isl_basic_set
*bset
, unsigned n
, unsigned flags
)
1795 isl_assert(bset
->ctx
, n
<= isl_basic_set_n_dim(bset
), goto error
);
1800 bset
= isl_basic_set_cow(bset
);
1802 row_size
= 1 + isl_dim_total(bset
->dim
) + bset
->extra
;
1803 old
= bset
->block2
.data
;
1804 bset
->block2
= isl_blk_extend(bset
->ctx
, bset
->block2
,
1805 (bset
->extra
+ n
) * (1 + row_size
));
1806 if (!bset
->block2
.data
)
1808 new_div
= isl_alloc_array(ctx
, isl_int
*, bset
->extra
+ n
);
1811 for (i
= 0; i
< n
; ++i
) {
1812 new_div
[i
] = bset
->block2
.data
+
1813 (bset
->extra
+ i
) * (1 + row_size
);
1814 isl_seq_clr(new_div
[i
], 1 + row_size
);
1816 for (i
= 0; i
< bset
->extra
; ++i
)
1817 new_div
[n
+ i
] = bset
->block2
.data
+ (bset
->div
[i
] - old
);
1819 bset
->div
= new_div
;
1822 bset
->dim
= isl_dim_drop_outputs(bset
->dim
,
1823 isl_basic_set_n_dim(bset
) - n
, n
);
1826 bset
= isl_basic_set_simplify(bset
);
1827 return isl_basic_set_finalize(bset
);
1829 isl_basic_set_free(bset
);
1833 struct isl_basic_map
*add_divs(struct isl_basic_map
*bmap
, unsigned n
)
1837 for (i
= 0; i
< n
; ++i
) {
1838 j
= isl_basic_map_alloc_div(bmap
);
1841 isl_seq_clr(bmap
->div
[j
], 1+1+isl_basic_map_total_dim(bmap
));
1845 isl_basic_map_free(bmap
);
1849 struct isl_basic_map
*isl_basic_map_apply_range(
1850 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
1852 struct isl_dim
*dim_result
= NULL
;
1853 struct isl_basic_map
*bmap
;
1854 unsigned n_in
, n_out
, n
, nparam
, total
, pos
;
1855 struct isl_dim_map
*dim_map1
, *dim_map2
;
1857 if (!bmap1
|| !bmap2
)
1860 dim_result
= isl_dim_join(isl_dim_copy(bmap1
->dim
),
1861 isl_dim_copy(bmap2
->dim
));
1863 n_in
= isl_basic_map_n_in(bmap1
);
1864 n_out
= isl_basic_map_n_out(bmap2
);
1865 n
= isl_basic_map_n_out(bmap1
);
1866 nparam
= isl_basic_map_n_param(bmap1
);
1868 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ n
;
1869 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
1870 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
1871 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
1872 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
1873 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
1874 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_in
);
1875 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_out
);
1876 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
1877 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
1878 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
1880 bmap
= isl_basic_map_alloc_dim(dim_result
,
1881 bmap1
->n_div
+ bmap2
->n_div
+ n
,
1882 bmap1
->n_eq
+ bmap2
->n_eq
,
1883 bmap1
->n_ineq
+ bmap2
->n_ineq
);
1884 bmap
= add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
1885 bmap
= add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
1886 bmap
= add_divs(bmap
, n
);
1887 bmap
= isl_basic_map_simplify(bmap
);
1888 return isl_basic_map_finalize(bmap
);
1890 isl_basic_map_free(bmap1
);
1891 isl_basic_map_free(bmap2
);
1895 struct isl_basic_set
*isl_basic_set_apply(
1896 struct isl_basic_set
*bset
, struct isl_basic_map
*bmap
)
1901 isl_assert(set
->ctx
, isl_basic_map_compatible_domain(bmap
, bset
),
1904 return (struct isl_basic_set
*)
1905 isl_basic_map_apply_range((struct isl_basic_map
*)bset
, bmap
);
1907 isl_basic_set_free(bset
);
1908 isl_basic_map_free(bmap
);
1912 struct isl_basic_map
*isl_basic_map_apply_domain(
1913 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
1915 if (!bmap1
|| !bmap2
)
1919 isl_basic_map_n_in(bmap1
) == isl_basic_map_n_in(bmap2
), goto error
);
1921 isl_basic_map_n_param(bmap1
) == isl_basic_map_n_param(bmap2
),
1924 bmap1
= isl_basic_map_reverse(bmap1
);
1925 bmap1
= isl_basic_map_apply_range(bmap1
, bmap2
);
1926 return isl_basic_map_reverse(bmap1
);
1928 isl_basic_map_free(bmap1
);
1929 isl_basic_map_free(bmap2
);
1933 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
1934 * A \cap B -> f(A) + f(B)
1936 struct isl_basic_map
*isl_basic_map_sum(
1937 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
1939 unsigned n_in
, n_out
, nparam
, total
, pos
;
1940 struct isl_basic_map
*bmap
= NULL
;
1941 struct isl_dim_map
*dim_map1
, *dim_map2
;
1944 if (!bmap1
|| !bmap2
)
1947 isl_assert(bmap1
->ctx
, isl_dim_equal(bmap1
->dim
, bmap2
->dim
),
1950 nparam
= isl_basic_map_n_param(bmap1
);
1951 n_in
= isl_basic_map_n_in(bmap1
);
1952 n_out
= isl_basic_map_n_out(bmap1
);
1954 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
;
1955 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
1956 dim_map2
= isl_dim_map_alloc(bmap2
->ctx
, total
);
1957 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
1958 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
);
1959 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
1960 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
1961 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_in
+ n_out
);
1962 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
1963 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
1964 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_out
);
1966 bmap
= isl_basic_map_alloc_dim(isl_dim_copy(bmap1
->dim
),
1967 bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
,
1968 bmap1
->n_eq
+ bmap2
->n_eq
+ n_out
,
1969 bmap1
->n_ineq
+ bmap2
->n_ineq
);
1970 for (i
= 0; i
< n_out
; ++i
) {
1971 int j
= isl_basic_map_alloc_equality(bmap
);
1974 isl_seq_clr(bmap
->eq
[j
], 1+total
);
1975 isl_int_set_si(bmap
->eq
[j
][1+nparam
+n_in
+i
], -1);
1976 isl_int_set_si(bmap
->eq
[j
][1+pos
+i
], 1);
1977 isl_int_set_si(bmap
->eq
[j
][1+pos
-n_out
+i
], 1);
1979 bmap
= add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
1980 bmap
= add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
1981 bmap
= add_divs(bmap
, 2 * n_out
);
1983 bmap
= isl_basic_map_simplify(bmap
);
1984 return isl_basic_map_finalize(bmap
);
1986 isl_basic_map_free(bmap
);
1987 isl_basic_map_free(bmap1
);
1988 isl_basic_map_free(bmap2
);
1992 /* Given a basic map A -> f(A), construct A -> -f(A).
1994 struct isl_basic_map
*isl_basic_map_neg(struct isl_basic_map
*bmap
)
1999 bmap
= isl_basic_map_cow(bmap
);
2003 n
= isl_basic_map_dim(bmap
, isl_dim_out
);
2004 off
= isl_basic_map_offset(bmap
, isl_dim_out
);
2005 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2006 for (j
= 0; j
< n
; ++j
)
2007 isl_int_neg(bmap
->eq
[i
][off
+j
], bmap
->eq
[i
][off
+j
]);
2008 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2009 for (j
= 0; j
< n
; ++j
)
2010 isl_int_neg(bmap
->ineq
[i
][off
+j
], bmap
->ineq
[i
][off
+j
]);
2011 for (i
= 0; i
< bmap
->n_div
; ++i
)
2012 for (j
= 0; j
< n
; ++j
)
2013 isl_int_neg(bmap
->div
[i
][1+off
+j
], bmap
->div
[i
][1+off
+j
]);
2014 return isl_basic_map_finalize(bmap
);
2017 /* Given a basic map A -> f(A) and an integer d, construct a basic map
2018 * A -> floor(f(A)/d).
2020 struct isl_basic_map
*isl_basic_map_floordiv(struct isl_basic_map
*bmap
,
2023 unsigned n_in
, n_out
, nparam
, total
, pos
;
2024 struct isl_basic_map
*result
= NULL
;
2025 struct isl_dim_map
*dim_map
;
2031 nparam
= isl_basic_map_n_param(bmap
);
2032 n_in
= isl_basic_map_n_in(bmap
);
2033 n_out
= isl_basic_map_n_out(bmap
);
2035 total
= nparam
+ n_in
+ n_out
+ bmap
->n_div
+ n_out
;
2036 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
2037 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_param
, pos
= 0);
2038 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_in
, pos
+= nparam
);
2039 isl_dim_map_div(dim_map
, bmap
, pos
+= n_in
+ n_out
);
2040 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_out
, pos
+= bmap
->n_div
);
2042 result
= isl_basic_map_alloc_dim(isl_dim_copy(bmap
->dim
),
2043 bmap
->n_div
+ n_out
,
2044 bmap
->n_eq
, bmap
->n_ineq
+ 2 * n_out
);
2045 result
= add_constraints_dim_map(result
, bmap
, dim_map
);
2046 result
= add_divs(result
, n_out
);
2047 for (i
= 0; i
< n_out
; ++i
) {
2049 j
= isl_basic_map_alloc_inequality(result
);
2052 isl_seq_clr(result
->ineq
[j
], 1+total
);
2053 isl_int_neg(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
2054 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], 1);
2055 j
= isl_basic_map_alloc_inequality(result
);
2058 isl_seq_clr(result
->ineq
[j
], 1+total
);
2059 isl_int_set(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
2060 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], -1);
2061 isl_int_sub_ui(result
->ineq
[j
][0], d
, 1);
2064 result
= isl_basic_map_simplify(result
);
2065 return isl_basic_map_finalize(result
);
2067 isl_basic_map_free(result
);
2071 static struct isl_basic_map
*var_equal(struct isl_basic_map
*bmap
, unsigned pos
)
2077 i
= isl_basic_map_alloc_equality(bmap
);
2080 nparam
= isl_basic_map_n_param(bmap
);
2081 n_in
= isl_basic_map_n_in(bmap
);
2082 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
2083 isl_int_set_si(bmap
->eq
[i
][1+nparam
+pos
], -1);
2084 isl_int_set_si(bmap
->eq
[i
][1+nparam
+n_in
+pos
], 1);
2085 return isl_basic_map_finalize(bmap
);
2087 isl_basic_map_free(bmap
);
2091 static struct isl_basic_map
*var_more(struct isl_basic_map
*bmap
, unsigned pos
)
2097 i
= isl_basic_map_alloc_inequality(bmap
);
2100 nparam
= isl_basic_map_n_param(bmap
);
2101 n_in
= isl_basic_map_n_in(bmap
);
2102 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
2103 isl_int_set_si(bmap
->ineq
[i
][0], -1);
2104 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
2105 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
2106 return isl_basic_map_finalize(bmap
);
2108 isl_basic_map_free(bmap
);
2112 static struct isl_basic_map
*var_less(struct isl_basic_map
*bmap
, unsigned pos
)
2118 i
= isl_basic_map_alloc_inequality(bmap
);
2121 nparam
= isl_basic_map_n_param(bmap
);
2122 n_in
= isl_basic_map_n_in(bmap
);
2123 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
2124 isl_int_set_si(bmap
->ineq
[i
][0], -1);
2125 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
2126 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
2127 return isl_basic_map_finalize(bmap
);
2129 isl_basic_map_free(bmap
);
2133 struct isl_basic_map
*isl_basic_map_equal(struct isl_dim
*dim
, unsigned n_equal
)
2136 struct isl_basic_map
*bmap
;
2137 bmap
= isl_basic_map_alloc_dim(dim
, 0, n_equal
, 0);
2140 for (i
= 0; i
< n_equal
&& bmap
; ++i
)
2141 bmap
= var_equal(bmap
, i
);
2142 return isl_basic_map_finalize(bmap
);
2145 struct isl_basic_map
*isl_basic_map_less_at(struct isl_dim
*dim
, unsigned pos
)
2148 struct isl_basic_map
*bmap
;
2149 bmap
= isl_basic_map_alloc_dim(dim
, 0, pos
, 1);
2152 for (i
= 0; i
< pos
&& bmap
; ++i
)
2153 bmap
= var_equal(bmap
, i
);
2155 bmap
= var_less(bmap
, pos
);
2156 return isl_basic_map_finalize(bmap
);
2159 struct isl_basic_map
*isl_basic_map_more_at(struct isl_dim
*dim
, unsigned pos
)
2162 struct isl_basic_map
*bmap
;
2163 bmap
= isl_basic_map_alloc_dim(dim
, 0, pos
, 1);
2166 for (i
= 0; i
< pos
&& bmap
; ++i
)
2167 bmap
= var_equal(bmap
, i
);
2169 bmap
= var_more(bmap
, pos
);
2170 return isl_basic_map_finalize(bmap
);
2173 struct isl_basic_map
*isl_basic_map_from_basic_set(
2174 struct isl_basic_set
*bset
, struct isl_dim
*dim
)
2176 struct isl_basic_map
*bmap
;
2178 bset
= isl_basic_set_cow(bset
);
2182 isl_assert(bset
->ctx
, isl_dim_compatible(bset
->dim
, dim
), goto error
);
2183 isl_dim_free(bset
->dim
);
2184 bmap
= (struct isl_basic_map
*) bset
;
2186 return isl_basic_map_finalize(bmap
);
2188 isl_basic_set_free(bset
);
2193 struct isl_basic_set
*isl_basic_set_from_basic_map(struct isl_basic_map
*bmap
)
2197 if (bmap
->dim
->n_in
== 0)
2198 return (struct isl_basic_set
*)bmap
;
2199 bmap
= isl_basic_map_cow(bmap
);
2202 bmap
->dim
= isl_dim_cow(bmap
->dim
);
2205 bmap
->dim
->n_out
+= bmap
->dim
->n_in
;
2206 bmap
->dim
->n_in
= 0;
2207 bmap
= isl_basic_map_finalize(bmap
);
2208 return (struct isl_basic_set
*)bmap
;
2210 isl_basic_map_free(bmap
);
2214 /* For a div d = floor(f/m), add the constraints
2217 * -(f-(n-1)) + m d >= 0
2219 * Note that the second constraint is the negation of
2223 static int add_div_constraints(struct isl_basic_map
*bmap
, unsigned div
)
2226 unsigned total
= isl_basic_map_total_dim(bmap
);
2227 unsigned div_pos
= 1 + total
- bmap
->n_div
+ div
;
2229 i
= isl_basic_map_alloc_inequality(bmap
);
2232 isl_seq_cpy(bmap
->ineq
[i
], bmap
->div
[div
]+1, 1+total
);
2233 isl_int_neg(bmap
->ineq
[i
][div_pos
], bmap
->div
[div
][0]);
2235 j
= isl_basic_map_alloc_inequality(bmap
);
2238 isl_seq_neg(bmap
->ineq
[j
], bmap
->ineq
[i
], 1 + total
);
2239 isl_int_add(bmap
->ineq
[j
][0], bmap
->ineq
[j
][0], bmap
->ineq
[j
][div_pos
]);
2240 isl_int_sub_ui(bmap
->ineq
[j
][0], bmap
->ineq
[j
][0], 1);
2244 struct isl_basic_set
*isl_basic_map_underlying_set(
2245 struct isl_basic_map
*bmap
)
2249 if (bmap
->dim
->nparam
== 0 && bmap
->dim
->n_in
== 0 && bmap
->n_div
== 0)
2250 return (struct isl_basic_set
*)bmap
;
2251 bmap
= isl_basic_map_cow(bmap
);
2254 bmap
->dim
= isl_dim_underlying(bmap
->dim
, bmap
->n_div
);
2257 bmap
->extra
-= bmap
->n_div
;
2259 bmap
= isl_basic_map_finalize(bmap
);
2260 return (struct isl_basic_set
*)bmap
;
2265 struct isl_basic_map
*isl_basic_map_overlying_set(
2266 struct isl_basic_set
*bset
, struct isl_basic_map
*like
)
2268 struct isl_basic_map
*bmap
;
2269 struct isl_ctx
*ctx
;
2276 isl_assert(ctx
, bset
->n_div
== 0, goto error
);
2277 isl_assert(ctx
, isl_basic_set_n_param(bset
) == 0, goto error
);
2278 isl_assert(ctx
, bset
->dim
->n_out
== isl_basic_map_total_dim(like
),
2280 if (isl_dim_equal(bset
->dim
, like
->dim
) && like
->n_div
== 0) {
2281 isl_basic_map_free(like
);
2282 return (struct isl_basic_map
*)bset
;
2284 bset
= isl_basic_set_cow(bset
);
2287 total
= bset
->dim
->n_out
+ bset
->extra
;
2288 bmap
= (struct isl_basic_map
*)bset
;
2289 isl_dim_free(bmap
->dim
);
2290 bmap
->dim
= isl_dim_copy(like
->dim
);
2293 bmap
->n_div
= like
->n_div
;
2294 bmap
->extra
+= like
->n_div
;
2297 ltotal
= total
- bmap
->extra
+ like
->extra
;
2300 bmap
->block2
= isl_blk_extend(ctx
, bmap
->block2
,
2301 bmap
->extra
* (1 + 1 + total
));
2302 if (isl_blk_is_error(bmap
->block2
))
2304 bmap
->div
= isl_realloc_array(ctx
, bmap
->div
, isl_int
*,
2308 for (i
= 0; i
< bmap
->extra
; ++i
)
2309 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + 1 + total
);
2310 for (i
= 0; i
< like
->n_div
; ++i
) {
2311 isl_seq_cpy(bmap
->div
[i
], like
->div
[i
], 1 + 1 + ltotal
);
2312 isl_seq_clr(bmap
->div
[i
]+1+1+ltotal
, total
- ltotal
);
2314 bmap
= isl_basic_map_extend_constraints(bmap
,
2315 0, 2 * like
->n_div
);
2316 for (i
= 0; i
< like
->n_div
; ++i
) {
2317 if (isl_int_is_zero(bmap
->div
[i
][0]))
2319 if (add_div_constraints(bmap
, i
) < 0)
2323 isl_basic_map_free(like
);
2324 bmap
= isl_basic_map_simplify(bmap
);
2325 bmap
= isl_basic_map_finalize(bmap
);
2328 isl_basic_map_free(like
);
2329 isl_basic_set_free(bset
);
2333 struct isl_basic_set
*isl_basic_set_from_underlying_set(
2334 struct isl_basic_set
*bset
, struct isl_basic_set
*like
)
2336 return (struct isl_basic_set
*)
2337 isl_basic_map_overlying_set(bset
, (struct isl_basic_map
*)like
);
2340 struct isl_set
*isl_set_from_underlying_set(
2341 struct isl_set
*set
, struct isl_basic_set
*like
)
2347 isl_assert(set
->ctx
, set
->dim
->n_out
== isl_basic_set_total_dim(like
),
2349 if (isl_dim_equal(set
->dim
, like
->dim
) && like
->n_div
== 0) {
2350 isl_basic_set_free(like
);
2353 set
= isl_set_cow(set
);
2356 for (i
= 0; i
< set
->n
; ++i
) {
2357 set
->p
[i
] = isl_basic_set_from_underlying_set(set
->p
[i
],
2358 isl_basic_set_copy(like
));
2362 isl_dim_free(set
->dim
);
2363 set
->dim
= isl_dim_copy(like
->dim
);
2366 isl_basic_set_free(like
);
2369 isl_basic_set_free(like
);
2374 struct isl_set
*isl_map_underlying_set(struct isl_map
*map
)
2378 map
= isl_map_cow(map
);
2381 map
->dim
= isl_dim_cow(map
->dim
);
2385 for (i
= 1; i
< map
->n
; ++i
)
2386 isl_assert(map
->ctx
, map
->p
[0]->n_div
== map
->p
[i
]->n_div
,
2388 for (i
= 0; i
< map
->n
; ++i
) {
2389 map
->p
[i
] = (struct isl_basic_map
*)
2390 isl_basic_map_underlying_set(map
->p
[i
]);
2395 map
->dim
= isl_dim_underlying(map
->dim
, 0);
2397 isl_dim_free(map
->dim
);
2398 map
->dim
= isl_dim_copy(map
->p
[0]->dim
);
2402 return (struct isl_set
*)map
;
2408 struct isl_set
*isl_set_to_underlying_set(struct isl_set
*set
)
2410 return (struct isl_set
*)isl_map_underlying_set((struct isl_map
*)set
);
2413 struct isl_basic_set
*isl_basic_map_domain(struct isl_basic_map
*bmap
)
2415 struct isl_basic_set
*domain
;
2419 n_out
= isl_basic_map_n_out(bmap
);
2420 domain
= isl_basic_set_from_basic_map(bmap
);
2421 return isl_basic_set_project_out(domain
, n_out
, 0);
2424 struct isl_basic_set
*isl_basic_map_range(struct isl_basic_map
*bmap
)
2426 return isl_basic_map_domain(isl_basic_map_reverse(bmap
));
2429 struct isl_set
*isl_map_range(struct isl_map
*map
)
2432 struct isl_set
*set
;
2436 map
= isl_map_cow(map
);
2440 set
= (struct isl_set
*) map
;
2441 if (set
->dim
->n_in
!= 0) {
2442 set
->dim
= isl_dim_drop_inputs(set
->dim
, 0, set
->dim
->n_in
);
2446 for (i
= 0; i
< map
->n
; ++i
) {
2447 set
->p
[i
] = isl_basic_map_range(map
->p
[i
]);
2451 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
2452 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
2459 struct isl_map
*isl_map_from_set(struct isl_set
*set
, struct isl_dim
*dim
)
2462 struct isl_map
*map
= NULL
;
2464 set
= isl_set_cow(set
);
2467 isl_assert(set
->ctx
, isl_dim_compatible(set
->dim
, dim
), goto error
);
2468 map
= (struct isl_map
*)set
;
2469 for (i
= 0; i
< set
->n
; ++i
) {
2470 map
->p
[i
] = isl_basic_map_from_basic_set(
2471 set
->p
[i
], isl_dim_copy(dim
));
2475 isl_dim_free(map
->dim
);
2484 struct isl_map
*isl_map_from_range(struct isl_set
*set
)
2486 return (struct isl_map
*)set
;
2489 struct isl_set
*isl_set_from_map(struct isl_map
*map
)
2492 struct isl_set
*set
= NULL
;
2496 map
= isl_map_cow(map
);
2499 map
->dim
= isl_dim_cow(map
->dim
);
2502 map
->dim
->n_out
+= map
->dim
->n_in
;
2504 set
= (struct isl_set
*)map
;
2505 for (i
= 0; i
< map
->n
; ++i
) {
2506 set
->p
[i
] = isl_basic_set_from_basic_map(map
->p
[i
]);
2516 struct isl_map
*isl_map_alloc_dim(struct isl_dim
*dim
, int n
, unsigned flags
)
2518 struct isl_map
*map
;
2522 isl_assert(dim
->ctx
, n
>= 0, return NULL
);
2523 map
= isl_alloc(dim
->ctx
, struct isl_map
,
2524 sizeof(struct isl_map
) +
2525 n
* sizeof(struct isl_basic_map
*));
2529 map
->ctx
= dim
->ctx
;
2530 isl_ctx_ref(map
->ctx
);
2542 struct isl_map
*isl_map_alloc(struct isl_ctx
*ctx
,
2543 unsigned nparam
, unsigned in
, unsigned out
, int n
,
2546 struct isl_map
*map
;
2547 struct isl_dim
*dims
;
2549 dims
= isl_dim_alloc(ctx
, nparam
, in
, out
);
2553 map
= isl_map_alloc_dim(dims
, n
, flags
);
2557 struct isl_basic_map
*isl_basic_map_empty(struct isl_ctx
*ctx
,
2558 unsigned nparam
, unsigned in
, unsigned out
)
2560 struct isl_basic_map
*bmap
;
2561 bmap
= isl_basic_map_alloc(ctx
, nparam
, in
, out
, 0, 1, 0);
2562 bmap
= isl_basic_map_set_to_empty(bmap
);
2566 struct isl_basic_set
*isl_basic_set_empty(struct isl_dim
*dim
)
2568 struct isl_basic_set
*bset
;
2569 bset
= isl_basic_set_alloc_dim(dim
, 0, 1, 0);
2570 bset
= isl_basic_set_set_to_empty(bset
);
2574 struct isl_basic_map
*isl_basic_map_empty_like(struct isl_basic_map
*model
)
2576 struct isl_basic_map
*bmap
;
2579 bmap
= isl_basic_map_alloc_dim(isl_dim_copy(model
->dim
), 0, 1, 0);
2580 bmap
= isl_basic_map_set_to_empty(bmap
);
2584 struct isl_basic_map
*isl_basic_map_empty_like_map(struct isl_map
*model
)
2586 struct isl_basic_map
*bmap
;
2589 bmap
= isl_basic_map_alloc_dim(isl_dim_copy(model
->dim
), 0, 1, 0);
2590 bmap
= isl_basic_map_set_to_empty(bmap
);
2594 struct isl_basic_set
*isl_basic_set_empty_like(struct isl_basic_set
*model
)
2596 struct isl_basic_set
*bset
;
2599 bset
= isl_basic_set_alloc_dim(isl_dim_copy(model
->dim
), 0, 1, 0);
2600 bset
= isl_basic_set_set_to_empty(bset
);
2604 struct isl_basic_map
*isl_basic_map_universe(struct isl_dim
*dim
)
2606 struct isl_basic_map
*bmap
;
2607 bmap
= isl_basic_map_alloc_dim(dim
, 0, 0, 0);
2611 struct isl_basic_set
*isl_basic_set_universe(struct isl_dim
*dim
)
2613 struct isl_basic_set
*bset
;
2614 bset
= isl_basic_set_alloc_dim(dim
, 0, 0, 0);
2618 struct isl_basic_set
*isl_basic_set_universe_like(struct isl_basic_set
*model
)
2622 return isl_basic_set_alloc_dim(isl_dim_copy(model
->dim
), 0, 0, 0);
2625 struct isl_map
*isl_map_empty(struct isl_dim
*dim
)
2627 return isl_map_alloc_dim(dim
, 0, ISL_MAP_DISJOINT
);
2630 struct isl_map
*isl_map_empty_like(struct isl_map
*model
)
2634 return isl_map_alloc_dim(isl_dim_copy(model
->dim
), 0, ISL_MAP_DISJOINT
);
2637 struct isl_map
*isl_map_empty_like_basic_map(struct isl_basic_map
*model
)
2641 return isl_map_alloc_dim(isl_dim_copy(model
->dim
), 0, ISL_MAP_DISJOINT
);
2644 struct isl_set
*isl_set_empty(struct isl_dim
*dim
)
2646 return isl_set_alloc_dim(dim
, 0, ISL_MAP_DISJOINT
);
2649 struct isl_set
*isl_set_empty_like(struct isl_set
*model
)
2653 return isl_set_empty(isl_dim_copy(model
->dim
));
2656 struct isl_map
*isl_map_universe(struct isl_dim
*dim
)
2658 struct isl_map
*map
;
2661 map
= isl_map_alloc_dim(isl_dim_copy(dim
), 1, ISL_MAP_DISJOINT
);
2662 map
= isl_map_add(map
, isl_basic_map_universe(dim
));
2666 struct isl_set
*isl_set_universe(struct isl_dim
*dim
)
2668 struct isl_set
*set
;
2671 set
= isl_set_alloc_dim(isl_dim_copy(dim
), 1, ISL_MAP_DISJOINT
);
2672 set
= isl_set_add(set
, isl_basic_set_universe(dim
));
2676 struct isl_map
*isl_map_dup(struct isl_map
*map
)
2679 struct isl_map
*dup
;
2683 dup
= isl_map_alloc_dim(isl_dim_copy(map
->dim
), map
->n
, map
->flags
);
2684 for (i
= 0; i
< map
->n
; ++i
)
2685 dup
= isl_map_add(dup
, isl_basic_map_copy(map
->p
[i
]));
2689 struct isl_map
*isl_map_add(struct isl_map
*map
, struct isl_basic_map
*bmap
)
2693 isl_assert(map
->ctx
, isl_dim_equal(map
->dim
, bmap
->dim
), goto error
);
2694 isl_assert(map
->ctx
, map
->n
< map
->size
, goto error
);
2695 map
->p
[map
->n
] = bmap
;
2697 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
2703 isl_basic_map_free(bmap
);
2707 void isl_map_free(struct isl_map
*map
)
2717 isl_ctx_deref(map
->ctx
);
2718 for (i
= 0; i
< map
->n
; ++i
)
2719 isl_basic_map_free(map
->p
[i
]);
2720 isl_dim_free(map
->dim
);
2724 struct isl_map
*isl_map_extend(struct isl_map
*base
,
2725 unsigned nparam
, unsigned n_in
, unsigned n_out
)
2729 base
= isl_map_cow(base
);
2733 base
->dim
= isl_dim_extend(base
->dim
, nparam
, n_in
, n_out
);
2736 for (i
= 0; i
< base
->n
; ++i
) {
2737 base
->p
[i
] = isl_basic_map_extend_dim(base
->p
[i
],
2738 isl_dim_copy(base
->dim
), 0, 0, 0);
2748 struct isl_set
*isl_set_extend(struct isl_set
*base
,
2749 unsigned nparam
, unsigned dim
)
2751 return (struct isl_set
*)isl_map_extend((struct isl_map
*)base
,
2755 static struct isl_basic_map
*isl_basic_map_fix_pos(struct isl_basic_map
*bmap
,
2756 unsigned pos
, int value
)
2760 bmap
= isl_basic_map_cow(bmap
);
2761 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
2762 j
= isl_basic_map_alloc_equality(bmap
);
2765 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
2766 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
2767 isl_int_set_si(bmap
->eq
[j
][0], value
);
2768 bmap
= isl_basic_map_simplify(bmap
);
2769 return isl_basic_map_finalize(bmap
);
2771 isl_basic_map_free(bmap
);
2775 struct isl_basic_map
*isl_basic_map_fix_si(struct isl_basic_map
*bmap
,
2776 enum isl_dim_type type
, unsigned pos
, int value
)
2780 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
2781 return isl_basic_map_fix_pos(bmap
, isl_basic_map_offset(bmap
, type
) + pos
,
2784 isl_basic_map_free(bmap
);
2788 struct isl_basic_set
*isl_basic_set_fix_si(struct isl_basic_set
*bset
,
2789 enum isl_dim_type type
, unsigned pos
, int value
)
2791 return (struct isl_basic_set
*)
2792 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
2796 struct isl_basic_map
*isl_basic_map_fix_input_si(struct isl_basic_map
*bmap
,
2797 unsigned input
, int value
)
2799 return isl_basic_map_fix_si(bmap
, isl_dim_in
, input
, value
);
2802 struct isl_basic_set
*isl_basic_set_fix_dim_si(struct isl_basic_set
*bset
,
2803 unsigned dim
, int value
)
2805 return (struct isl_basic_set
*)
2806 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
2807 isl_dim_set
, dim
, value
);
2810 struct isl_map
*isl_map_fix_si(struct isl_map
*map
,
2811 enum isl_dim_type type
, unsigned pos
, int value
)
2815 map
= isl_map_cow(map
);
2819 isl_assert(ctx
, pos
< isl_map_dim(map
, type
), goto error
);
2820 for (i
= 0; i
< map
->n
; ++i
) {
2821 map
->p
[i
] = isl_basic_map_fix_si(map
->p
[i
], type
, pos
, value
);
2825 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
2832 struct isl_map
*isl_map_fix_input_si(struct isl_map
*map
,
2833 unsigned input
, int value
)
2835 return isl_map_fix_si(map
, isl_dim_in
, input
, value
);
2838 struct isl_set
*isl_set_fix_dim_si(struct isl_set
*set
, unsigned dim
, int value
)
2840 return (struct isl_set
*)
2841 isl_map_fix_si((struct isl_map
*)set
, isl_dim_set
, dim
, value
);
2844 struct isl_basic_set
*isl_basic_set_lower_bound_dim(struct isl_basic_set
*bset
,
2845 unsigned dim
, isl_int value
)
2850 bset
= isl_basic_set_cow(bset
);
2851 bset
= isl_basic_set_extend_constraints(bset
, 0, 1);
2852 j
= isl_basic_set_alloc_inequality(bset
);
2855 isl_seq_clr(bset
->ineq
[j
], 1 + isl_basic_set_total_dim(bset
));
2856 isl_int_set_si(bset
->ineq
[j
][1 + isl_basic_set_n_param(bset
) + dim
], 1);
2857 isl_int_neg(bset
->ineq
[j
][0], value
);
2858 bset
= isl_basic_set_simplify(bset
);
2859 return isl_basic_set_finalize(bset
);
2861 isl_basic_set_free(bset
);
2865 struct isl_set
*isl_set_lower_bound_dim(struct isl_set
*set
, unsigned dim
,
2870 set
= isl_set_cow(set
);
2874 isl_assert(set
->ctx
, dim
< isl_set_n_dim(set
), goto error
);
2875 for (i
= 0; i
< set
->n
; ++i
) {
2876 set
->p
[i
] = isl_basic_set_lower_bound_dim(set
->p
[i
], dim
, value
);
2886 struct isl_map
*isl_map_reverse(struct isl_map
*map
)
2891 map
= isl_map_cow(map
);
2895 map
->dim
= isl_dim_reverse(map
->dim
);
2898 for (i
= 0; i
< map
->n
; ++i
) {
2899 map
->p
[i
] = isl_basic_map_reverse(map
->p
[i
]);
2903 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
2910 struct isl_map
*isl_basic_map_lexmax(
2911 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
2912 struct isl_set
**empty
)
2914 return isl_pip_basic_map_lexmax(bmap
, dom
, empty
);
2917 struct isl_map
*isl_basic_map_lexmin(
2918 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
2919 struct isl_set
**empty
)
2921 return isl_pip_basic_map_lexmin(bmap
, dom
, empty
);
2924 struct isl_set
*isl_basic_set_lexmin(struct isl_basic_set
*bset
)
2926 struct isl_basic_map
*bmap
= NULL
;
2927 struct isl_basic_set
*dom
= NULL
;
2928 struct isl_map
*min
;
2929 struct isl_dim
*param_dim
;
2933 bmap
= isl_basic_map_from_basic_set(bset
, isl_dim_copy(bset
->dim
));
2936 param_dim
= isl_dim_domain(isl_dim_copy(bmap
->dim
));
2937 dom
= isl_basic_set_universe(param_dim
);
2940 min
= isl_basic_map_lexmin(bmap
, dom
, NULL
);
2941 return isl_map_range(min
);
2943 isl_basic_map_free(bmap
);
2947 struct isl_map
*isl_basic_map_compute_divs(struct isl_basic_map
*bmap
)
2954 off
= isl_dim_total(bmap
->dim
);
2955 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2956 if (isl_int_is_zero(bmap
->div
[i
][0]))
2957 return isl_pip_basic_map_compute_divs(bmap
);
2958 isl_assert(bmap
->ctx
, isl_int_is_zero(bmap
->div
[i
][1+1+off
+i
]),
2961 return isl_map_from_basic_map(bmap
);
2963 isl_basic_map_free(bmap
);
2967 struct isl_map
*isl_map_compute_divs(struct isl_map
*map
)
2970 struct isl_map
*res
;
2976 res
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[0]));
2977 for (i
= 1 ; i
< map
->n
; ++i
) {
2979 r2
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[i
]));
2980 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
2981 res
= isl_map_union_disjoint(res
, r2
);
2983 res
= isl_map_union(res
, r2
);
2990 struct isl_set
*isl_basic_set_compute_divs(struct isl_basic_set
*bset
)
2992 return (struct isl_set
*)
2993 isl_basic_map_compute_divs((struct isl_basic_map
*)bset
);
2996 struct isl_set
*isl_set_compute_divs(struct isl_set
*set
)
2998 return (struct isl_set
*)
2999 isl_map_compute_divs((struct isl_map
*)set
);
3002 struct isl_set
*isl_map_domain(struct isl_map
*map
)
3005 struct isl_set
*set
;
3010 map
= isl_map_cow(map
);
3014 set
= (struct isl_set
*)map
;
3015 set
->dim
= isl_dim_domain(set
->dim
);
3018 for (i
= 0; i
< map
->n
; ++i
) {
3019 set
->p
[i
] = isl_basic_map_domain(map
->p
[i
]);
3023 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
3024 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
3031 struct isl_map
*isl_map_union_disjoint(
3032 struct isl_map
*map1
, struct isl_map
*map2
)
3036 struct isl_map
*map
= NULL
;
3050 isl_assert(map1
->ctx
, isl_dim_equal(map1
->dim
, map2
->dim
), goto error
);
3052 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
3053 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
3054 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
3056 map
= isl_map_alloc_dim(isl_dim_copy(map1
->dim
),
3057 map1
->n
+ map2
->n
, flags
);
3060 for (i
= 0; i
< map1
->n
; ++i
) {
3061 map
= isl_map_add(map
,
3062 isl_basic_map_copy(map1
->p
[i
]));
3066 for (i
= 0; i
< map2
->n
; ++i
) {
3067 map
= isl_map_add(map
,
3068 isl_basic_map_copy(map2
->p
[i
]));
3082 struct isl_map
*isl_map_union(struct isl_map
*map1
, struct isl_map
*map2
)
3084 map1
= isl_map_union_disjoint(map1
, map2
);
3088 ISL_F_CLR(map1
, ISL_MAP_DISJOINT
);
3092 struct isl_set
*isl_set_union_disjoint(
3093 struct isl_set
*set1
, struct isl_set
*set2
)
3095 return (struct isl_set
*)
3096 isl_map_union_disjoint(
3097 (struct isl_map
*)set1
, (struct isl_map
*)set2
);
3100 struct isl_set
*isl_set_union(struct isl_set
*set1
, struct isl_set
*set2
)
3102 return (struct isl_set
*)
3103 isl_map_union((struct isl_map
*)set1
, (struct isl_map
*)set2
);
3106 struct isl_map
*isl_map_intersect_range(
3107 struct isl_map
*map
, struct isl_set
*set
)
3110 struct isl_map
*result
;
3116 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
) &&
3117 ISL_F_ISSET(set
, ISL_MAP_DISJOINT
))
3118 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
3120 result
= isl_map_alloc_dim(isl_dim_copy(map
->dim
),
3121 map
->n
* set
->n
, flags
);
3124 for (i
= 0; i
< map
->n
; ++i
)
3125 for (j
= 0; j
< set
->n
; ++j
) {
3126 result
= isl_map_add(result
,
3127 isl_basic_map_intersect_range(
3128 isl_basic_map_copy(map
->p
[i
]),
3129 isl_basic_set_copy(set
->p
[j
])));
3142 struct isl_map
*isl_map_intersect_domain(
3143 struct isl_map
*map
, struct isl_set
*set
)
3145 return isl_map_reverse(
3146 isl_map_intersect_range(isl_map_reverse(map
), set
));
3149 struct isl_map
*isl_map_apply_domain(
3150 struct isl_map
*map1
, struct isl_map
*map2
)
3154 map1
= isl_map_reverse(map1
);
3155 map1
= isl_map_apply_range(map1
, map2
);
3156 return isl_map_reverse(map1
);
3163 struct isl_map
*isl_map_apply_range(
3164 struct isl_map
*map1
, struct isl_map
*map2
)
3166 struct isl_dim
*dim_result
;
3167 struct isl_map
*result
;
3176 dim_result
= isl_dim_join(isl_dim_copy(map1
->dim
),
3177 isl_dim_copy(map2
->dim
));
3179 result
= isl_map_alloc_dim(dim_result
, map1
->n
* map2
->n
, 0);
3182 for (i
= 0; i
< map1
->n
; ++i
)
3183 for (j
= 0; j
< map2
->n
; ++j
) {
3184 result
= isl_map_add(result
,
3185 isl_basic_map_apply_range(
3186 isl_basic_map_copy(map1
->p
[i
]),
3187 isl_basic_map_copy(map2
->p
[j
])));
3193 if (result
&& result
->n
<= 1)
3194 ISL_F_SET(result
, ISL_MAP_DISJOINT
);
3203 * returns range - domain
3205 struct isl_basic_set
*isl_basic_map_deltas(struct isl_basic_map
*bmap
)
3207 struct isl_basic_set
*bset
;
3214 dim
= isl_basic_map_n_in(bmap
);
3215 nparam
= isl_basic_map_n_param(bmap
);
3216 isl_assert(bmap
->ctx
, dim
== isl_basic_map_n_out(bmap
), goto error
);
3217 bset
= isl_basic_set_from_basic_map(bmap
);
3218 bset
= isl_basic_set_cow(bset
);
3219 bset
= isl_basic_set_extend(bset
, nparam
, 3*dim
, 0, dim
, 0);
3220 bset
= isl_basic_set_swap_vars(bset
, 2*dim
);
3221 for (i
= 0; i
< dim
; ++i
) {
3222 int j
= isl_basic_map_alloc_equality(
3223 (struct isl_basic_map
*)bset
);
3226 isl_seq_clr(bset
->eq
[j
], 1 + isl_basic_set_total_dim(bset
));
3227 isl_int_set_si(bset
->eq
[j
][1+nparam
+i
], 1);
3228 isl_int_set_si(bset
->eq
[j
][1+nparam
+dim
+i
], 1);
3229 isl_int_set_si(bset
->eq
[j
][1+nparam
+2*dim
+i
], -1);
3231 return isl_basic_set_project_out(bset
, 2*dim
, 0);
3233 isl_basic_map_free(bmap
);
3238 * returns range - domain
3240 struct isl_set
*isl_map_deltas(struct isl_map
*map
)
3243 struct isl_set
*result
;
3248 isl_assert(map
->ctx
, isl_map_n_in(map
) == isl_map_n_out(map
), goto error
);
3249 result
= isl_set_alloc(map
->ctx
, isl_map_n_param(map
),
3250 isl_map_n_in(map
), map
->n
, map
->flags
);
3253 for (i
= 0; i
< map
->n
; ++i
)
3254 result
= isl_set_add(result
,
3255 isl_basic_map_deltas(isl_basic_map_copy(map
->p
[i
])));
3263 static struct isl_basic_map
*basic_map_identity(struct isl_dim
*dims
)
3265 struct isl_basic_map
*bmap
;
3273 nparam
= dims
->nparam
;
3275 bmap
= isl_basic_map_alloc_dim(dims
, 0, dim
, 0);
3279 for (i
= 0; i
< dim
; ++i
) {
3280 int j
= isl_basic_map_alloc_equality(bmap
);
3283 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
3284 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
3285 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], -1);
3287 return isl_basic_map_finalize(bmap
);
3289 isl_basic_map_free(bmap
);
3293 struct isl_basic_map
*isl_basic_map_identity(struct isl_dim
*set_dim
)
3295 struct isl_dim
*dim
= isl_dim_map(set_dim
);
3298 return basic_map_identity(dim
);
3301 struct isl_basic_map
*isl_basic_map_identity_like(struct isl_basic_map
*model
)
3303 if (!model
|| !model
->dim
)
3305 isl_assert(model
->ctx
,
3306 model
->dim
->n_in
== model
->dim
->n_out
, return NULL
);
3307 return basic_map_identity(isl_dim_copy(model
->dim
));
3310 static struct isl_map
*map_identity(struct isl_dim
*dim
)
3312 struct isl_map
*map
= isl_map_alloc_dim(dim
, 1, ISL_MAP_DISJOINT
);
3313 return isl_map_add(map
, basic_map_identity(isl_dim_copy(dim
)));
3316 struct isl_map
*isl_map_identity(struct isl_dim
*set_dim
)
3318 struct isl_dim
*dim
= isl_dim_map(set_dim
);
3321 return map_identity(dim
);
3324 struct isl_map
*isl_map_identity_like(struct isl_basic_map
*model
)
3326 if (!model
|| !model
->dim
)
3328 isl_assert(model
->ctx
,
3329 model
->dim
->n_in
== model
->dim
->n_out
, return NULL
);
3330 return map_identity(isl_dim_copy(model
->dim
));
3333 int isl_set_is_equal(struct isl_set
*set1
, struct isl_set
*set2
)
3335 return isl_map_is_equal((struct isl_map
*)set1
, (struct isl_map
*)set2
);
3338 int isl_set_is_subset(struct isl_set
*set1
, struct isl_set
*set2
)
3340 return isl_map_is_subset(
3341 (struct isl_map
*)set1
, (struct isl_map
*)set2
);
3344 int isl_basic_map_is_subset(
3345 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3348 struct isl_map
*map1
;
3349 struct isl_map
*map2
;
3351 if (!bmap1
|| !bmap2
)
3354 map1
= isl_map_from_basic_map(isl_basic_map_copy(bmap1
));
3355 map2
= isl_map_from_basic_map(isl_basic_map_copy(bmap2
));
3357 is_subset
= isl_map_is_subset(map1
, map2
);
3365 int isl_basic_map_is_equal(
3366 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3370 if (!bmap1
|| !bmap2
)
3372 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
3375 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
3379 int isl_basic_set_is_equal(
3380 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
3382 return isl_basic_map_is_equal(
3383 (struct isl_basic_map
*)bset1
, (struct isl_basic_map
*)bset2
);
3386 int isl_map_is_empty(struct isl_map
*map
)
3393 for (i
= 0; i
< map
->n
; ++i
) {
3394 is_empty
= isl_basic_map_is_empty(map
->p
[i
]);
3403 int isl_map_fast_is_empty(struct isl_map
*map
)
3408 int isl_set_is_empty(struct isl_set
*set
)
3410 return isl_map_is_empty((struct isl_map
*)set
);
3413 int isl_map_is_subset(struct isl_map
*map1
, struct isl_map
*map2
)
3417 struct isl_map
*diff
;
3422 if (isl_map_is_empty(map1
))
3425 if (isl_map_is_empty(map2
))
3428 diff
= isl_map_subtract(isl_map_copy(map1
), isl_map_copy(map2
));
3432 is_subset
= isl_map_is_empty(diff
);
3438 int isl_map_is_equal(struct isl_map
*map1
, struct isl_map
*map2
)
3444 is_subset
= isl_map_is_subset(map1
, map2
);
3447 is_subset
= isl_map_is_subset(map2
, map1
);
3451 int isl_basic_map_is_strict_subset(
3452 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3456 if (!bmap1
|| !bmap2
)
3458 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
3461 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
3462 if (is_subset
== -1)
3467 int isl_basic_map_is_universe(struct isl_basic_map
*bmap
)
3471 return bmap
->n_eq
== 0 && bmap
->n_ineq
== 0;
3474 int isl_basic_set_is_universe(struct isl_basic_set
*bset
)
3478 return bset
->n_eq
== 0 && bset
->n_ineq
== 0;
3481 int isl_basic_map_is_empty(struct isl_basic_map
*bmap
)
3483 struct isl_basic_set
*bset
= NULL
;
3484 struct isl_vec
*sample
= NULL
;
3491 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
3494 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
3495 struct isl_basic_map
*copy
= isl_basic_map_copy(bmap
);
3496 copy
= isl_basic_map_convex_hull(copy
);
3497 empty
= ISL_F_ISSET(copy
, ISL_BASIC_MAP_EMPTY
);
3498 isl_basic_map_free(copy
);
3502 total
= 1 + isl_basic_map_total_dim(bmap
);
3503 if (bmap
->sample
&& bmap
->sample
->size
== total
) {
3504 int contains
= basic_map_contains(bmap
, bmap
->sample
);
3510 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
3513 sample
= isl_basic_set_sample(bset
);
3516 empty
= sample
->size
== 0;
3518 isl_vec_free(bmap
->ctx
, bmap
->sample
);
3519 bmap
->sample
= sample
;
3524 int isl_basic_map_fast_is_empty(struct isl_basic_map
*bmap
)
3528 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
);
3531 int isl_basic_set_fast_is_empty(struct isl_basic_set
*bset
)
3535 return ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
);
3538 int isl_basic_set_is_empty(struct isl_basic_set
*bset
)
3540 return isl_basic_map_is_empty((struct isl_basic_map
*)bset
);
3543 struct isl_map
*isl_basic_map_union(
3544 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3546 struct isl_map
*map
;
3547 if (!bmap1
|| !bmap2
)
3550 isl_assert(map1
->ctx
, isl_dim_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
3552 map
= isl_map_alloc_dim(isl_dim_copy(bmap1
->dim
), 2, 0);
3555 map
= isl_map_add(map
, bmap1
);
3556 map
= isl_map_add(map
, bmap2
);
3559 isl_basic_map_free(bmap1
);
3560 isl_basic_map_free(bmap2
);
3564 struct isl_set
*isl_basic_set_union(
3565 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
3567 return (struct isl_set
*)isl_basic_map_union(
3568 (struct isl_basic_map
*)bset1
,
3569 (struct isl_basic_map
*)bset2
);
3572 /* Order divs such that any div only depends on previous divs */
3573 static struct isl_basic_map
*order_divs(struct isl_basic_map
*bmap
)
3576 unsigned off
= isl_dim_total(bmap
->dim
);
3578 for (i
= 0; i
< bmap
->n_div
; ++i
) {
3580 pos
= isl_seq_first_non_zero(bmap
->div
[i
]+1+1+off
+i
,
3584 swap_div(bmap
, i
, pos
);
3590 /* Look for a div in dst that corresponds to the div "div" in src.
3591 * The divs before "div" in src and dst are assumed to be the same.
3593 * Returns -1 if no corresponding div was found and the position
3594 * of the corresponding div in dst otherwise.
3596 static int find_div(struct isl_basic_map
*dst
,
3597 struct isl_basic_map
*src
, unsigned div
)
3601 unsigned total
= isl_dim_total(src
->dim
);
3603 isl_assert(dst
->ctx
, div
<= dst
->n_div
, return -1);
3604 for (i
= div
; i
< dst
->n_div
; ++i
)
3605 if (isl_seq_eq(dst
->div
[i
], src
->div
[div
], 1+1+total
+div
) &&
3606 isl_seq_first_non_zero(dst
->div
[i
]+1+1+total
+div
,
3607 dst
->n_div
- div
) == -1)
3612 struct isl_basic_map
*isl_basic_map_align_divs(
3613 struct isl_basic_map
*dst
, struct isl_basic_map
*src
)
3616 unsigned total
= isl_dim_total(src
->dim
);
3621 if (src
->n_div
== 0)
3624 for (i
= 0; i
< src
->n_div
; ++i
)
3625 isl_assert(src
->ctx
, !isl_int_is_zero(src
->div
[i
][0]), goto error
);
3627 src
= order_divs(src
);
3628 dst
= isl_basic_map_cow(dst
);
3629 dst
= isl_basic_map_extend_dim(dst
, isl_dim_copy(dst
->dim
),
3630 src
->n_div
, 0, 2 * src
->n_div
);
3633 for (i
= 0; i
< src
->n_div
; ++i
) {
3634 int j
= find_div(dst
, src
, i
);
3636 j
= isl_basic_map_alloc_div(dst
);
3639 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
+i
);
3640 isl_seq_clr(dst
->div
[j
]+1+1+total
+i
, dst
->n_div
- i
);
3641 if (add_div_constraints(dst
, j
) < 0)
3645 swap_div(dst
, i
, j
);
3649 isl_basic_map_free(dst
);
3653 struct isl_basic_set
*isl_basic_set_align_divs(
3654 struct isl_basic_set
*dst
, struct isl_basic_set
*src
)
3656 return (struct isl_basic_set
*)isl_basic_map_align_divs(
3657 (struct isl_basic_map
*)dst
, (struct isl_basic_map
*)src
);
3660 struct isl_map
*isl_map_align_divs(struct isl_map
*map
)
3664 map
= isl_map_compute_divs(map
);
3665 map
= isl_map_cow(map
);
3669 for (i
= 1; i
< map
->n
; ++i
)
3670 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], map
->p
[i
]);
3671 for (i
= 1; i
< map
->n
; ++i
)
3672 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], map
->p
[0]);
3674 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3678 struct isl_set
*isl_set_align_divs(struct isl_set
*set
)
3680 return (struct isl_set
*)isl_map_align_divs((struct isl_map
*)set
);
3683 static struct isl_map
*add_cut_constraint(struct isl_map
*dst
,
3684 struct isl_basic_map
*src
, isl_int
*c
,
3685 unsigned len
, int oppose
)
3687 struct isl_basic_map
*copy
= NULL
;
3692 copy
= isl_basic_map_copy(src
);
3693 copy
= isl_basic_map_cow(copy
);
3696 copy
= isl_basic_map_extend_constraints(copy
, 0, 1);
3697 k
= isl_basic_map_alloc_inequality(copy
);
3701 isl_seq_neg(copy
->ineq
[k
], c
, len
);
3703 isl_seq_cpy(copy
->ineq
[k
], c
, len
);
3704 total
= 1 + isl_basic_map_total_dim(copy
);
3705 isl_seq_clr(copy
->ineq
[k
]+len
, total
- len
);
3706 isl_inequality_negate(copy
, k
);
3707 copy
= isl_basic_map_simplify(copy
);
3708 copy
= isl_basic_map_finalize(copy
);
3709 is_empty
= isl_basic_map_is_empty(copy
);
3713 dst
= isl_map_add(dst
, copy
);
3715 isl_basic_map_free(copy
);
3718 isl_basic_map_free(copy
);
3723 static struct isl_map
*subtract(struct isl_map
*map
, struct isl_basic_map
*bmap
)
3727 struct isl_map
*rest
= NULL
;
3729 unsigned total
= isl_basic_map_total_dim(bmap
);
3736 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
3737 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
3739 max
= map
->n
* (2 * bmap
->n_eq
+ bmap
->n_ineq
);
3740 rest
= isl_map_alloc_dim(isl_dim_copy(map
->dim
), max
, flags
);
3744 for (i
= 0; i
< map
->n
; ++i
) {
3745 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], bmap
);
3750 for (j
= 0; j
< map
->n
; ++j
)
3751 map
->p
[j
] = isl_basic_map_cow(map
->p
[j
]);
3753 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
3754 for (j
= 0; j
< map
->n
; ++j
) {
3755 rest
= add_cut_constraint(rest
,
3756 map
->p
[j
], bmap
->eq
[i
], 1+total
, 0);
3760 rest
= add_cut_constraint(rest
,
3761 map
->p
[j
], bmap
->eq
[i
], 1+total
, 1);
3765 map
->p
[j
] = isl_basic_map_extend_constraints(map
->p
[j
],
3769 k
= isl_basic_map_alloc_equality(map
->p
[j
]);
3772 isl_seq_cpy(map
->p
[j
]->eq
[k
], bmap
->eq
[i
], 1+total
);
3773 isl_seq_clr(map
->p
[j
]->eq
[k
]+1+total
,
3774 map
->p
[j
]->n_div
- bmap
->n_div
);
3778 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
3779 for (j
= 0; j
< map
->n
; ++j
) {
3780 rest
= add_cut_constraint(rest
,
3781 map
->p
[j
], bmap
->ineq
[i
], 1+total
, 0);
3785 map
->p
[j
] = isl_basic_map_extend_constraints(map
->p
[j
],
3789 k
= isl_basic_map_alloc_inequality(map
->p
[j
]);
3792 isl_seq_cpy(map
->p
[j
]->ineq
[k
], bmap
->ineq
[i
], 1+total
);
3793 isl_seq_clr(map
->p
[j
]->ineq
[k
]+1+total
,
3794 map
->p
[j
]->n_div
- bmap
->n_div
);
3806 struct isl_map
*isl_map_subtract(struct isl_map
*map1
, struct isl_map
*map2
)
3812 isl_assert(map1
->ctx
, isl_dim_equal(map1
->dim
, map2
->dim
), goto error
);
3814 if (isl_map_is_empty(map2
)) {
3819 map1
= isl_map_compute_divs(map1
);
3820 map2
= isl_map_compute_divs(map2
);
3824 for (i
= 0; map1
&& i
< map2
->n
; ++i
)
3825 map1
= subtract(map1
, map2
->p
[i
]);
3835 struct isl_set
*isl_set_subtract(struct isl_set
*set1
, struct isl_set
*set2
)
3837 return (struct isl_set
*)
3839 (struct isl_map
*)set1
, (struct isl_map
*)set2
);
3842 struct isl_set
*isl_set_apply(struct isl_set
*set
, struct isl_map
*map
)
3846 isl_assert(set
->ctx
, isl_map_compatible_domain(map
, set
), goto error
);
3847 map
= isl_map_intersect_domain(map
, set
);
3848 set
= isl_map_range(map
);
3856 /* There is no need to cow as removing empty parts doesn't change
3857 * the meaning of the set.
3859 struct isl_map
*isl_map_remove_empty_parts(struct isl_map
*map
)
3866 for (i
= map
->n
-1; i
>= 0; --i
) {
3867 if (!ISL_F_ISSET(map
->p
[i
], ISL_BASIC_MAP_EMPTY
))
3869 isl_basic_map_free(map
->p
[i
]);
3870 if (i
!= map
->n
-1) {
3871 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3872 map
->p
[i
] = map
->p
[map
->n
-1];
3880 struct isl_set
*isl_set_remove_empty_parts(struct isl_set
*set
)
3882 return (struct isl_set
*)
3883 isl_map_remove_empty_parts((struct isl_map
*)set
);
3886 struct isl_basic_map
*isl_map_copy_basic_map(struct isl_map
*map
)
3888 struct isl_basic_map
*bmap
;
3889 if (!map
|| map
->n
== 0)
3891 bmap
= map
->p
[map
->n
-1];
3892 isl_assert(map
->ctx
, ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
), return NULL
);
3893 return isl_basic_map_copy(bmap
);
3896 struct isl_basic_set
*isl_set_copy_basic_set(struct isl_set
*set
)
3898 (struct isl_basic_set
*)isl_map_copy_basic_map((struct isl_map
*)set
);
3901 struct isl_map
*isl_map_drop_basic_map(struct isl_map
*map
,
3902 struct isl_basic_map
*bmap
)
3908 for (i
= map
->n
-1; i
>= 0; --i
) {
3909 if (map
->p
[i
] != bmap
)
3911 map
= isl_map_cow(map
);
3914 isl_basic_map_free(map
->p
[i
]);
3915 if (i
!= map
->n
-1) {
3916 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
3917 map
->p
[i
] = map
->p
[map
->n
-1];
3922 isl_basic_map_free(bmap
);
3926 isl_basic_map_free(bmap
);
3930 struct isl_set
*isl_set_drop_basic_set(struct isl_set
*set
,
3931 struct isl_basic_set
*bset
)
3933 (struct isl_set
*)isl_map_drop_basic_map((struct isl_map
*)set
,
3934 (struct isl_basic_map
*)bset
);
3937 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
3938 * for any common value of the parameters and dimensions preceding dim
3939 * in both basic sets, the values of dimension pos in bset1 are
3940 * smaller or larger than those in bset2.
3943 * 1 if bset1 follows bset2
3944 * -1 if bset1 precedes bset2
3945 * 0 if bset1 and bset2 are incomparable
3946 * -2 if some error occurred.
3948 int isl_basic_set_compare_at(struct isl_basic_set
*bset1
,
3949 struct isl_basic_set
*bset2
, int pos
)
3951 struct isl_dim
*dims
;
3952 struct isl_basic_map
*bmap1
= NULL
;
3953 struct isl_basic_map
*bmap2
= NULL
;
3954 struct isl_ctx
*ctx
;
3955 struct isl_vec
*obj
;
3958 unsigned dim1
, dim2
;
3960 enum isl_lp_result res
;
3963 if (!bset1
|| !bset2
)
3966 nparam
= isl_basic_set_n_param(bset1
);
3967 dim1
= isl_basic_set_n_dim(bset1
);
3968 dim2
= isl_basic_set_n_dim(bset2
);
3969 dims
= isl_dim_alloc(bset1
->ctx
, nparam
, pos
, dim1
- pos
);
3970 bmap1
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset1
), dims
);
3971 dims
= isl_dim_alloc(bset2
->ctx
, nparam
, pos
, dim2
- pos
);
3972 bmap2
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset2
), dims
);
3973 if (!bmap1
|| !bmap2
)
3975 bmap1
= isl_basic_map_cow(bmap1
);
3976 bmap1
= isl_basic_map_extend(bmap1
, nparam
,
3977 pos
, (dim1
- pos
) + (dim2
- pos
),
3978 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
3979 bmap1
= add_constraints(bmap1
, bmap2
, 0, dim1
- pos
);
3982 total
= isl_basic_map_total_dim(bmap1
);
3984 obj
= isl_vec_alloc(ctx
, 1 + total
);
3985 isl_seq_clr(obj
->block
.data
, 1 + total
);
3986 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
], 1);
3987 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
+(dim1
-pos
)], -1);
3992 res
= isl_solve_lp(bmap1
, 0, obj
->block
.data
, ctx
->one
, &num
, &den
);
3993 if (res
== isl_lp_empty
)
3995 else if (res
== isl_lp_ok
&& isl_int_is_pos(num
))
3997 else if ((res
== isl_lp_ok
&& isl_int_is_neg(num
)) ||
3998 res
== isl_lp_unbounded
)
4004 isl_basic_map_free(bmap1
);
4005 isl_vec_free(ctx
, obj
);
4008 isl_basic_map_free(bmap1
);
4009 isl_basic_map_free(bmap2
);
4013 static int isl_basic_map_fast_has_fixed_var(struct isl_basic_map
*bmap
,
4014 unsigned pos
, isl_int
*val
)
4022 total
= isl_basic_map_total_dim(bmap
);
4023 for (i
= 0, d
= total
-1; i
< bmap
->n_eq
&& d
+1 > pos
; ++i
) {
4024 for (; d
+1 > pos
; --d
)
4025 if (!isl_int_is_zero(bmap
->eq
[i
][1+d
]))
4029 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1, d
) != -1)
4031 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1+d
+1, total
-d
-1) != -1)
4033 if (!isl_int_is_one(bmap
->eq
[i
][1+d
]))
4036 isl_int_neg(*val
, bmap
->eq
[i
][0]);
4042 static int isl_map_fast_has_fixed_var(struct isl_map
*map
,
4043 unsigned pos
, isl_int
*val
)
4055 return isl_basic_map_fast_has_fixed_var(map
->p
[0], pos
, val
);
4058 fixed
= isl_basic_map_fast_has_fixed_var(map
->p
[0], pos
, &v
);
4059 for (i
= 1; fixed
== 1 && i
< map
->n
; ++i
) {
4060 fixed
= isl_basic_map_fast_has_fixed_var(map
->p
[i
], pos
, &tmp
);
4061 if (fixed
== 1 && isl_int_ne(tmp
, v
))
4065 isl_int_set(*val
, v
);
4071 static int isl_set_fast_has_fixed_var(struct isl_set
*set
, unsigned pos
,
4074 return isl_map_fast_has_fixed_var((struct isl_map
*)set
, pos
, val
);
4077 int isl_basic_map_fast_is_fixed(struct isl_basic_map
*bmap
,
4078 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
4080 if (pos
>= isl_basic_map_dim(bmap
, type
))
4082 return isl_basic_map_fast_has_fixed_var(bmap
,
4083 isl_basic_map_offset(bmap
, type
) - 1 + pos
, val
);
4086 /* Check if dimension dim has fixed value and if so and if val is not NULL,
4087 * then return this fixed value in *val.
4089 int isl_set_fast_dim_is_fixed(struct isl_set
*set
, unsigned dim
, isl_int
*val
)
4091 return isl_set_fast_has_fixed_var(set
, isl_set_n_param(set
) + dim
, val
);
4094 /* Check if input variable in has fixed value and if so and if val is not NULL,
4095 * then return this fixed value in *val.
4097 int isl_map_fast_input_is_fixed(struct isl_map
*map
, unsigned in
, isl_int
*val
)
4099 return isl_map_fast_has_fixed_var(map
, isl_map_n_param(map
) + in
, val
);
4102 /* Check if dimension dim has an (obvious) fixed lower bound and if so
4103 * and if val is not NULL, then return this lower bound in *val.
4105 int isl_basic_set_fast_dim_has_fixed_lower_bound(struct isl_basic_set
*bset
,
4106 unsigned dim
, isl_int
*val
)
4108 int i
, i_eq
= -1, i_ineq
= -1;
4115 total
= isl_basic_set_total_dim(bset
);
4116 nparam
= isl_basic_set_n_param(bset
);
4117 for (i
= 0; i
< bset
->n_eq
; ++i
) {
4118 if (isl_int_is_zero(bset
->eq
[i
][1+nparam
+dim
]))
4124 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
4125 if (!isl_int_is_pos(bset
->ineq
[i
][1+nparam
+dim
]))
4127 if (i_eq
!= -1 || i_ineq
!= -1)
4131 if (i_eq
== -1 && i_ineq
== -1)
4133 c
= i_eq
!= -1 ? bset
->eq
[i_eq
] : bset
->ineq
[i_ineq
];
4134 /* The coefficient should always be one due to normalization. */
4135 if (!isl_int_is_one(c
[1+nparam
+dim
]))
4137 if (isl_seq_first_non_zero(c
+1, nparam
+dim
) != -1)
4139 if (isl_seq_first_non_zero(c
+1+nparam
+dim
+1,
4140 total
- nparam
- dim
- 1) != -1)
4143 isl_int_neg(*val
, c
[0]);
4147 int isl_set_fast_dim_has_fixed_lower_bound(struct isl_set
*set
,
4148 unsigned dim
, isl_int
*val
)
4160 return isl_basic_set_fast_dim_has_fixed_lower_bound(set
->p
[0],
4164 fixed
= isl_basic_set_fast_dim_has_fixed_lower_bound(set
->p
[0],
4166 for (i
= 1; fixed
== 1 && i
< set
->n
; ++i
) {
4167 fixed
= isl_basic_set_fast_dim_has_fixed_lower_bound(set
->p
[i
],
4169 if (fixed
== 1 && isl_int_ne(tmp
, v
))
4173 isl_int_set(*val
, v
);
4184 static int qsort_constraint_cmp(const void *p1
, const void *p2
)
4186 const struct constraint
*c1
= (const struct constraint
*)p1
;
4187 const struct constraint
*c2
= (const struct constraint
*)p2
;
4188 unsigned size
= isl_min(c1
->size
, c2
->size
);
4189 return isl_seq_cmp(c1
->c
, c2
->c
, size
);
4192 static struct isl_basic_map
*isl_basic_map_sort_constraints(
4193 struct isl_basic_map
*bmap
)
4196 struct constraint
*c
;
4201 total
= isl_basic_map_total_dim(bmap
);
4202 c
= isl_alloc_array(bmap
->ctx
, struct constraint
, bmap
->n_ineq
);
4205 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
4207 c
[i
].c
= bmap
->ineq
[i
];
4209 qsort(c
, bmap
->n_ineq
, sizeof(struct constraint
), qsort_constraint_cmp
);
4210 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
4211 bmap
->ineq
[i
] = c
[i
].c
;
4215 isl_basic_map_free(bmap
);
4219 struct isl_basic_map
*isl_basic_map_normalize(struct isl_basic_map
*bmap
)
4223 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED
))
4225 bmap
= isl_basic_map_convex_hull(bmap
);
4226 bmap
= isl_basic_map_sort_constraints(bmap
);
4227 ISL_F_SET(bmap
, ISL_BASIC_MAP_NORMALIZED
);
4231 struct isl_basic_set
*isl_basic_set_normalize(struct isl_basic_set
*bset
)
4233 return (struct isl_basic_set
*)isl_basic_map_normalize(
4234 (struct isl_basic_map
*)bset
);
4237 static int isl_basic_map_fast_cmp(const struct isl_basic_map
*bmap1
,
4238 const struct isl_basic_map
*bmap2
)
4245 if (isl_basic_map_n_param(bmap1
) != isl_basic_map_n_param(bmap2
))
4246 return isl_basic_map_n_param(bmap1
) - isl_basic_map_n_param(bmap2
);
4247 if (isl_basic_map_n_in(bmap1
) != isl_basic_map_n_in(bmap2
))
4248 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
4249 if (isl_basic_map_n_out(bmap1
) != isl_basic_map_n_out(bmap2
))
4250 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
4251 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
) &&
4252 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
4254 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
))
4256 if (ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
4258 if (bmap1
->n_eq
!= bmap2
->n_eq
)
4259 return bmap1
->n_eq
- bmap2
->n_eq
;
4260 if (bmap1
->n_ineq
!= bmap2
->n_ineq
)
4261 return bmap1
->n_ineq
- bmap2
->n_ineq
;
4262 if (bmap1
->n_div
!= bmap2
->n_div
)
4263 return bmap1
->n_div
- bmap2
->n_div
;
4264 total
= isl_basic_map_total_dim(bmap1
);
4265 for (i
= 0; i
< bmap1
->n_eq
; ++i
) {
4266 cmp
= isl_seq_cmp(bmap1
->eq
[i
], bmap2
->eq
[i
], 1+total
);
4270 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
4271 cmp
= isl_seq_cmp(bmap1
->ineq
[i
], bmap2
->ineq
[i
], 1+total
);
4275 for (i
= 0; i
< bmap1
->n_div
; ++i
) {
4276 cmp
= isl_seq_cmp(bmap1
->div
[i
], bmap2
->div
[i
], 1+1+total
);
4283 static int isl_basic_map_fast_is_equal(struct isl_basic_map
*bmap1
,
4284 struct isl_basic_map
*bmap2
)
4286 return isl_basic_map_fast_cmp(bmap1
, bmap2
) == 0;
4289 static int qsort_bmap_cmp(const void *p1
, const void *p2
)
4291 const struct isl_basic_map
*bmap1
= *(const struct isl_basic_map
**)p1
;
4292 const struct isl_basic_map
*bmap2
= *(const struct isl_basic_map
**)p2
;
4294 return isl_basic_map_fast_cmp(bmap1
, bmap2
);
4297 /* We normalize in place, but if anything goes wrong we need
4298 * to return NULL, so we need to make sure we don't change the
4299 * meaning of any possible other copies of map.
4301 struct isl_map
*isl_map_normalize(struct isl_map
*map
)
4304 struct isl_basic_map
*bmap
;
4308 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
))
4310 for (i
= 0; i
< map
->n
; ++i
) {
4311 bmap
= isl_basic_map_normalize(isl_basic_map_copy(map
->p
[i
]));
4314 isl_basic_map_free(map
->p
[i
]);
4317 qsort(map
->p
, map
->n
, sizeof(struct isl_basic_map
*), qsort_bmap_cmp
);
4318 ISL_F_SET(map
, ISL_MAP_NORMALIZED
);
4319 map
= isl_map_remove_empty_parts(map
);
4322 for (i
= map
->n
- 1; i
>= 1; --i
) {
4323 if (!isl_basic_map_fast_is_equal(map
->p
[i
-1], map
->p
[i
]))
4325 isl_basic_map_free(map
->p
[i
-1]);
4326 for (j
= i
; j
< map
->n
; ++j
)
4327 map
->p
[j
-1] = map
->p
[j
];
4337 struct isl_set
*isl_set_normalize(struct isl_set
*set
)
4339 return (struct isl_set
*)isl_map_normalize((struct isl_map
*)set
);
4342 int isl_map_fast_is_equal(struct isl_map
*map1
, struct isl_map
*map2
)
4352 if (!isl_dim_equal(map1
->dim
, map2
->dim
))
4355 map1
= isl_map_copy(map1
);
4356 map2
= isl_map_copy(map2
);
4357 map1
= isl_map_normalize(map1
);
4358 map2
= isl_map_normalize(map2
);
4361 equal
= map1
->n
== map2
->n
;
4362 for (i
= 0; equal
&& i
< map1
->n
; ++i
) {
4363 equal
= isl_basic_map_fast_is_equal(map1
->p
[i
], map2
->p
[i
]);
4376 int isl_set_fast_is_equal(struct isl_set
*set1
, struct isl_set
*set2
)
4378 return isl_map_fast_is_equal((struct isl_map
*)set1
,
4379 (struct isl_map
*)set2
);
4382 /* Return an interval that ranges from min to max (inclusive)
4384 struct isl_basic_set
*isl_basic_set_interval(struct isl_ctx
*ctx
,
4385 isl_int min
, isl_int max
)
4388 struct isl_basic_set
*bset
= NULL
;
4390 bset
= isl_basic_set_alloc(ctx
, 0, 1, 0, 0, 2);
4394 k
= isl_basic_set_alloc_inequality(bset
);
4397 isl_int_set_si(bset
->ineq
[k
][1], 1);
4398 isl_int_neg(bset
->ineq
[k
][0], min
);
4400 k
= isl_basic_set_alloc_inequality(bset
);
4403 isl_int_set_si(bset
->ineq
[k
][1], -1);
4404 isl_int_set(bset
->ineq
[k
][0], max
);
4408 isl_basic_set_free(bset
);
4412 /* Return the Cartesian product of the basic sets in list (in the given order).
4414 struct isl_basic_set
*isl_basic_set_product(struct isl_basic_set_list
*list
)
4422 struct isl_basic_set
*product
= NULL
;
4426 isl_assert(list
->ctx
, list
->n
> 0, goto error
);
4427 isl_assert(list
->ctx
, list
->p
[0], goto error
);
4428 nparam
= isl_basic_set_n_param(list
->p
[0]);
4429 dim
= isl_basic_set_n_dim(list
->p
[0]);
4430 extra
= list
->p
[0]->n_div
;
4431 n_eq
= list
->p
[0]->n_eq
;
4432 n_ineq
= list
->p
[0]->n_ineq
;
4433 for (i
= 1; i
< list
->n
; ++i
) {
4434 isl_assert(list
->ctx
, list
->p
[i
], goto error
);
4435 isl_assert(list
->ctx
,
4436 nparam
== isl_basic_set_n_param(list
->p
[i
]), goto error
);
4437 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
4438 extra
+= list
->p
[i
]->n_div
;
4439 n_eq
+= list
->p
[i
]->n_eq
;
4440 n_ineq
+= list
->p
[i
]->n_ineq
;
4442 product
= isl_basic_set_alloc(list
->ctx
, nparam
, dim
, extra
,
4447 for (i
= 0; i
< list
->n
; ++i
) {
4448 isl_basic_set_add_constraints(product
,
4449 isl_basic_set_copy(list
->p
[i
]), dim
);
4450 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
4452 isl_basic_set_list_free(list
);
4455 isl_basic_set_free(product
);
4456 isl_basic_set_list_free(list
);
4460 struct isl_basic_map
*isl_basic_map_product(
4461 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
4463 struct isl_dim
*dim_result
= NULL
;
4464 struct isl_basic_map
*bmap
;
4465 unsigned in1
, in2
, out1
, out2
, nparam
, total
, pos
;
4466 struct isl_dim_map
*dim_map1
, *dim_map2
;
4468 if (!bmap1
|| !bmap2
)
4471 isl_assert(map1
->ctx
, isl_dim_match(bmap1
->dim
, isl_dim_param
,
4472 bmap2
->dim
, isl_dim_param
), goto error
);
4473 dim_result
= isl_dim_product(isl_dim_copy(bmap1
->dim
),
4474 isl_dim_copy(bmap2
->dim
));
4476 in1
= isl_basic_map_n_in(bmap1
);
4477 in2
= isl_basic_map_n_in(bmap2
);
4478 out1
= isl_basic_map_n_out(bmap1
);
4479 out2
= isl_basic_map_n_out(bmap2
);
4480 nparam
= isl_basic_map_n_param(bmap1
);
4482 total
= nparam
+ in1
+ in2
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
4483 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4484 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4485 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
4486 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
4487 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
4488 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
4489 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
4490 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
4491 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
4492 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
4494 bmap
= isl_basic_map_alloc_dim(dim_result
,
4495 bmap1
->n_div
+ bmap2
->n_div
,
4496 bmap1
->n_eq
+ bmap2
->n_eq
,
4497 bmap1
->n_ineq
+ bmap2
->n_ineq
);
4498 bmap
= add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
4499 bmap
= add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
4500 bmap
= isl_basic_map_simplify(bmap
);
4501 return isl_basic_map_finalize(bmap
);
4503 isl_basic_map_free(bmap1
);
4504 isl_basic_map_free(bmap2
);
4508 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
4510 struct isl_map
*isl_map_product(struct isl_map
*map1
, struct isl_map
*map2
)
4513 struct isl_map
*result
;
4519 isl_assert(map1
->ctx
, isl_dim_match(map1
->dim
, isl_dim_param
,
4520 map2
->dim
, isl_dim_param
), goto error
);
4522 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
4523 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
4524 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
4526 result
= isl_map_alloc_dim(isl_dim_product(isl_dim_copy(map1
->dim
),
4527 isl_dim_copy(map2
->dim
)),
4528 map1
->n
* map2
->n
, flags
);
4531 for (i
= 0; i
< map1
->n
; ++i
)
4532 for (j
= 0; j
< map2
->n
; ++j
) {
4533 struct isl_basic_map
*part
;
4534 part
= isl_basic_map_product(
4535 isl_basic_map_copy(map1
->p
[i
]),
4536 isl_basic_map_copy(map2
->p
[j
]));
4537 if (isl_basic_map_is_empty(part
))
4538 isl_basic_map_free(part
);
4540 result
= isl_map_add(result
, part
);
4553 /* Given two set A and B, construct its Cartesian product A x B.
4555 struct isl_set
*isl_set_product(struct isl_set
*set1
, struct isl_set
*set2
)
4557 return (struct isl_set
*)isl_map_product((struct isl_map
*)set1
,
4558 (struct isl_map
*)set2
);
4561 uint32_t isl_basic_set_get_hash(struct isl_basic_set
*bset
)
4569 bset
= isl_basic_set_copy(bset
);
4570 bset
= isl_basic_set_normalize(bset
);
4573 total
= isl_basic_set_total_dim(bset
);
4574 isl_hash_byte(hash
, bset
->n_eq
& 0xFF);
4575 for (i
= 0; i
< bset
->n_eq
; ++i
) {
4577 c_hash
= isl_seq_get_hash(bset
->eq
[i
], 1 + total
);
4578 isl_hash_hash(hash
, c_hash
);
4580 isl_hash_byte(hash
, bset
->n_ineq
& 0xFF);
4581 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
4583 c_hash
= isl_seq_get_hash(bset
->ineq
[i
], 1 + total
);
4584 isl_hash_hash(hash
, c_hash
);
4586 isl_hash_byte(hash
, bset
->n_div
& 0xFF);
4587 for (i
= 0; i
< bset
->n_div
; ++i
) {
4589 if (isl_int_is_zero(bset
->div
[i
][0]))
4591 isl_hash_byte(hash
, i
& 0xFF);
4592 c_hash
= isl_seq_get_hash(bset
->div
[i
], 1 + 1 + total
);
4593 isl_hash_hash(hash
, c_hash
);
4595 isl_basic_set_free(bset
);
4599 uint32_t isl_set_get_hash(struct isl_set
*set
)
4606 set
= isl_set_copy(set
);
4607 set
= isl_set_normalize(set
);
4611 hash
= isl_hash_init();
4612 for (i
= 0; i
< set
->n
; ++i
) {
4614 bset_hash
= isl_basic_set_get_hash(set
->p
[i
]);
4615 isl_hash_hash(hash
, bset_hash
);
4623 /* Check if the value for dimension dim is completely determined
4624 * by the values of the other parameters and variables.
4625 * That is, check if dimension dim is involved in an equality.
4627 int isl_basic_set_dim_is_unique(struct isl_basic_set
*bset
, unsigned dim
)
4634 nparam
= isl_basic_set_n_param(bset
);
4635 for (i
= 0; i
< bset
->n_eq
; ++i
)
4636 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ dim
]))
4641 /* Check if the value for dimension dim is completely determined
4642 * by the values of the other parameters and variables.
4643 * That is, check if dimension dim is involved in an equality
4644 * for each of the subsets.
4646 int isl_set_dim_is_unique(struct isl_set
*set
, unsigned dim
)
4652 for (i
= 0; i
< set
->n
; ++i
) {
4654 unique
= isl_basic_set_dim_is_unique(set
->p
[i
], dim
);