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 static struct isl_basic_map
*basic_map_init(struct isl_ctx
*ctx
,
224 struct isl_basic_map
*bmap
, unsigned extra
,
225 unsigned n_eq
, unsigned n_ineq
)
228 size_t row_size
= 1 + isl_dim_total(bmap
->dim
) + extra
;
230 bmap
->block
= isl_blk_alloc(ctx
, (n_ineq
+ n_eq
) * row_size
);
231 if (isl_blk_is_error(bmap
->block
)) {
236 bmap
->ineq
= isl_alloc_array(ctx
, isl_int
*, n_ineq
+ n_eq
);
238 isl_blk_free(ctx
, bmap
->block
);
244 bmap
->block2
= isl_blk_empty();
247 bmap
->block2
= isl_blk_alloc(ctx
, extra
* (1 + row_size
));
248 if (isl_blk_is_error(bmap
->block2
)) {
250 isl_blk_free(ctx
, bmap
->block
);
255 bmap
->div
= isl_alloc_array(ctx
, isl_int
*, extra
);
257 isl_blk_free(ctx
, bmap
->block2
);
259 isl_blk_free(ctx
, bmap
->block
);
265 for (i
= 0; i
< n_ineq
+ n_eq
; ++i
)
266 bmap
->ineq
[i
] = bmap
->block
.data
+ i
* row_size
;
268 for (i
= 0; i
< extra
; ++i
)
269 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + row_size
);
275 bmap
->c_size
= n_eq
+ n_ineq
;
276 bmap
->eq
= bmap
->ineq
+ n_ineq
;
285 isl_basic_map_free(bmap
);
289 struct isl_basic_set
*isl_basic_set_alloc(struct isl_ctx
*ctx
,
290 unsigned nparam
, unsigned dim
, unsigned extra
,
291 unsigned n_eq
, unsigned n_ineq
)
293 struct isl_basic_map
*bmap
;
294 bmap
= isl_basic_map_alloc(ctx
, nparam
, 0, dim
, extra
, n_eq
, n_ineq
);
295 return (struct isl_basic_set
*)bmap
;
298 struct isl_basic_set
*isl_basic_set_alloc_dim(struct isl_dim
*dim
,
299 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
301 struct isl_basic_map
*bmap
;
304 isl_assert(dim
->ctx
, dim
->n_in
== 0, return NULL
);
305 bmap
= isl_basic_map_alloc_dim(dim
, extra
, n_eq
, n_ineq
);
306 return (struct isl_basic_set
*)bmap
;
309 struct isl_basic_map
*isl_basic_map_alloc_dim(struct isl_dim
*dim
,
310 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
312 struct isl_basic_map
*bmap
;
316 bmap
= isl_alloc_type(dim
->ctx
, struct isl_basic_map
);
321 return basic_map_init(dim
->ctx
, bmap
, extra
, n_eq
, n_ineq
);
327 struct isl_basic_map
*isl_basic_map_alloc(struct isl_ctx
*ctx
,
328 unsigned nparam
, unsigned in
, unsigned out
, unsigned extra
,
329 unsigned n_eq
, unsigned n_ineq
)
331 struct isl_basic_map
*bmap
;
334 dim
= isl_dim_alloc(ctx
, nparam
, in
, out
);
338 bmap
= isl_basic_map_alloc_dim(dim
, extra
, n_eq
, n_ineq
);
342 static void dup_constraints(
343 struct isl_basic_map
*dst
, struct isl_basic_map
*src
)
346 unsigned total
= isl_basic_map_total_dim(src
);
348 for (i
= 0; i
< src
->n_eq
; ++i
) {
349 int j
= isl_basic_map_alloc_equality(dst
);
350 isl_seq_cpy(dst
->eq
[j
], src
->eq
[i
], 1+total
);
353 for (i
= 0; i
< src
->n_ineq
; ++i
) {
354 int j
= isl_basic_map_alloc_inequality(dst
);
355 isl_seq_cpy(dst
->ineq
[j
], src
->ineq
[i
], 1+total
);
358 for (i
= 0; i
< src
->n_div
; ++i
) {
359 int j
= isl_basic_map_alloc_div(dst
);
360 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
);
362 ISL_F_SET(dst
, ISL_BASIC_SET_FINAL
);
365 struct isl_basic_map
*isl_basic_map_dup(struct isl_basic_map
*bmap
)
367 struct isl_basic_map
*dup
;
371 dup
= isl_basic_map_alloc_dim(isl_dim_copy(bmap
->dim
),
372 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
375 dup
->flags
= bmap
->flags
;
376 dup_constraints(dup
, bmap
);
377 dup
->sample
= isl_vec_copy(bmap
->ctx
, bmap
->sample
);
381 struct isl_basic_set
*isl_basic_set_dup(struct isl_basic_set
*bset
)
383 struct isl_basic_map
*dup
;
385 dup
= isl_basic_map_dup((struct isl_basic_map
*)bset
);
386 return (struct isl_basic_set
*)dup
;
389 struct isl_basic_set
*isl_basic_set_copy(struct isl_basic_set
*bset
)
394 if (ISL_F_ISSET(bset
, ISL_BASIC_SET_FINAL
)) {
398 return isl_basic_set_dup(bset
);
401 struct isl_set
*isl_set_copy(struct isl_set
*set
)
410 struct isl_basic_map
*isl_basic_map_copy(struct isl_basic_map
*bmap
)
415 if (ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
)) {
419 return isl_basic_map_dup(bmap
);
422 struct isl_map
*isl_map_copy(struct isl_map
*map
)
431 void isl_basic_map_free(struct isl_basic_map
*bmap
)
439 isl_ctx_deref(bmap
->ctx
);
441 isl_blk_free(bmap
->ctx
, bmap
->block2
);
443 isl_blk_free(bmap
->ctx
, bmap
->block
);
444 isl_vec_free(bmap
->ctx
, bmap
->sample
);
445 isl_dim_free(bmap
->dim
);
449 void isl_basic_set_free(struct isl_basic_set
*bset
)
451 isl_basic_map_free((struct isl_basic_map
*)bset
);
454 int isl_basic_map_alloc_equality(struct isl_basic_map
*bmap
)
460 isl_assert(ctx
, bmap
->n_eq
+ bmap
->n_ineq
< bmap
->c_size
, return -1);
461 isl_assert(ctx
, (bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
<= bmap
->c_size
,
463 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
464 if ((bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
== bmap
->c_size
) {
466 int j
= isl_basic_map_alloc_inequality(bmap
);
470 bmap
->ineq
[j
] = bmap
->ineq
[bmap
->n_ineq
- 1];
471 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
478 isl_seq_clr(bmap
->eq
[bmap
->n_eq
] + 1 + isl_basic_map_total_dim(bmap
),
479 bmap
->extra
- bmap
->n_div
);
483 int isl_basic_set_alloc_equality(struct isl_basic_set
*bset
)
485 return isl_basic_map_alloc_equality((struct isl_basic_map
*)bset
);
488 int isl_basic_map_free_equality(struct isl_basic_map
*bmap
, unsigned n
)
492 isl_assert(bmap
->ctx
, n
<= bmap
->n_eq
, return -1);
497 int isl_basic_map_drop_equality(struct isl_basic_map
*bmap
, unsigned pos
)
502 isl_assert(bmap
->ctx
, pos
< bmap
->n_eq
, return -1);
504 if (pos
!= bmap
->n_eq
- 1) {
506 bmap
->eq
[pos
] = bmap
->eq
[bmap
->n_eq
- 1];
507 bmap
->eq
[bmap
->n_eq
- 1] = t
;
513 int isl_basic_set_drop_equality(struct isl_basic_set
*bset
, unsigned pos
)
515 return isl_basic_map_drop_equality((struct isl_basic_map
*)bset
, pos
);
518 void isl_basic_map_inequality_to_equality(
519 struct isl_basic_map
*bmap
, unsigned pos
)
524 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
525 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
530 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
531 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
532 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
535 int isl_basic_map_alloc_inequality(struct isl_basic_map
*bmap
)
541 isl_assert(ctx
, bmap
->n_ineq
< bmap
->eq
- bmap
->ineq
, return -1);
542 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
543 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
544 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
545 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
546 isl_seq_clr(bmap
->ineq
[bmap
->n_ineq
] +
547 1 + isl_basic_map_total_dim(bmap
),
548 bmap
->extra
- bmap
->n_div
);
549 return bmap
->n_ineq
++;
552 int isl_basic_set_alloc_inequality(struct isl_basic_set
*bset
)
554 return isl_basic_map_alloc_inequality((struct isl_basic_map
*)bset
);
557 int isl_basic_map_free_inequality(struct isl_basic_map
*bmap
, unsigned n
)
561 isl_assert(bmap
->ctx
, n
<= bmap
->n_ineq
, return -1);
566 int isl_basic_set_free_inequality(struct isl_basic_set
*bset
, unsigned n
)
568 return isl_basic_map_free_inequality((struct isl_basic_map
*)bset
, n
);
571 int isl_basic_map_drop_inequality(struct isl_basic_map
*bmap
, unsigned pos
)
576 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
578 if (pos
!= bmap
->n_ineq
- 1) {
580 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
581 bmap
->ineq
[bmap
->n_ineq
- 1] = t
;
582 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
588 int isl_basic_set_drop_inequality(struct isl_basic_set
*bset
, unsigned pos
)
590 return isl_basic_map_drop_inequality((struct isl_basic_map
*)bset
, pos
);
593 int isl_basic_map_alloc_div(struct isl_basic_map
*bmap
)
597 isl_assert(bmap
->ctx
, bmap
->n_div
< bmap
->extra
, return -1);
598 isl_seq_clr(bmap
->div
[bmap
->n_div
] +
599 1 + 1 + isl_basic_map_total_dim(bmap
),
600 bmap
->extra
- bmap
->n_div
);
601 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
602 return bmap
->n_div
++;
605 int isl_basic_map_free_div(struct isl_basic_map
*bmap
, unsigned n
)
609 isl_assert(bmap
->ctx
, n
<= bmap
->n_div
, return -1);
614 /* Copy constraint from src to dst, putting the vars of src at offset
615 * dim_off in dst and the divs of src at offset div_off in dst.
616 * If both sets are actually map, then dim_off applies to the input
619 static void copy_constraint(struct isl_basic_map
*dst_map
, isl_int
*dst
,
620 struct isl_basic_map
*src_map
, isl_int
*src
,
621 unsigned in_off
, unsigned out_off
, unsigned div_off
)
623 unsigned src_nparam
= isl_basic_map_n_param(src_map
);
624 unsigned dst_nparam
= isl_basic_map_n_param(dst_map
);
625 unsigned src_in
= isl_basic_map_n_in(src_map
);
626 unsigned dst_in
= isl_basic_map_n_in(dst_map
);
627 unsigned src_out
= isl_basic_map_n_out(src_map
);
628 unsigned dst_out
= isl_basic_map_n_out(dst_map
);
629 isl_int_set(dst
[0], src
[0]);
630 isl_seq_cpy(dst
+1, src
+1, isl_min(dst_nparam
, src_nparam
));
631 if (dst_nparam
> src_nparam
)
632 isl_seq_clr(dst
+1+src_nparam
,
633 dst_nparam
- src_nparam
);
634 isl_seq_clr(dst
+1+dst_nparam
, in_off
);
635 isl_seq_cpy(dst
+1+dst_nparam
+in_off
,
637 isl_min(dst_in
-in_off
, src_in
));
638 if (dst_in
-in_off
> src_in
)
639 isl_seq_clr(dst
+1+dst_nparam
+in_off
+src_in
,
640 dst_in
- in_off
- src_in
);
641 isl_seq_clr(dst
+1+dst_nparam
+dst_in
, out_off
);
642 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+out_off
,
643 src
+1+src_nparam
+src_in
,
644 isl_min(dst_out
-out_off
, src_out
));
645 if (dst_out
-out_off
> src_out
)
646 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+out_off
+src_out
,
647 dst_out
- out_off
- src_out
);
648 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
, div_off
);
649 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+dst_out
+div_off
,
650 src
+1+src_nparam
+src_in
+src_out
,
651 isl_min(dst_map
->extra
-div_off
, src_map
->n_div
));
652 if (dst_map
->n_div
-div_off
> src_map
->n_div
)
653 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
+
654 div_off
+src_map
->n_div
,
655 dst_map
->n_div
- div_off
- src_map
->n_div
);
658 static void copy_div(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 isl_int_set(dst
[0], src
[0]);
663 copy_constraint(dst_map
, dst
+1, src_map
, src
+1, in_off
, out_off
, div_off
);
666 static struct isl_basic_map
*add_constraints(struct isl_basic_map
*bmap1
,
667 struct isl_basic_map
*bmap2
, unsigned i_pos
, unsigned o_pos
)
672 if (!bmap1
|| !bmap2
)
675 div_off
= bmap1
->n_div
;
677 for (i
= 0; i
< bmap2
->n_eq
; ++i
) {
678 int i1
= isl_basic_map_alloc_equality(bmap1
);
681 copy_constraint(bmap1
, bmap1
->eq
[i1
], bmap2
, bmap2
->eq
[i
],
682 i_pos
, o_pos
, div_off
);
685 for (i
= 0; i
< bmap2
->n_ineq
; ++i
) {
686 int i1
= isl_basic_map_alloc_inequality(bmap1
);
689 copy_constraint(bmap1
, bmap1
->ineq
[i1
], bmap2
, bmap2
->ineq
[i
],
690 i_pos
, o_pos
, div_off
);
693 for (i
= 0; i
< bmap2
->n_div
; ++i
) {
694 int i1
= isl_basic_map_alloc_div(bmap1
);
697 copy_div(bmap1
, bmap1
->div
[i1
], bmap2
, bmap2
->div
[i
],
698 i_pos
, o_pos
, div_off
);
701 isl_basic_map_free(bmap2
);
706 isl_basic_map_free(bmap1
);
707 isl_basic_map_free(bmap2
);
711 static void copy_constraint_dim_map(isl_int
*dst
, isl_int
*src
,
712 struct isl_dim_map
*dim_map
)
716 for (i
= 0; i
< dim_map
->len
; ++i
) {
717 if (dim_map
->pos
[i
] < 0)
718 isl_int_set_si(dst
[i
], 0);
720 isl_int_set(dst
[i
], src
[dim_map
->pos
[i
]]);
724 static void copy_div_dim_map(isl_int
*dst
, isl_int
*src
,
725 struct isl_dim_map
*dim_map
)
727 isl_int_set(dst
[0], src
[0]);
728 copy_constraint_dim_map(dst
+1, src
+1, dim_map
);
731 static struct isl_basic_map
*add_constraints_dim_map(struct isl_basic_map
*dst
,
732 struct isl_basic_map
*src
, struct isl_dim_map
*dim_map
)
736 if (!src
|| !dst
|| !dim_map
)
739 for (i
= 0; i
< src
->n_eq
; ++i
) {
740 int i1
= isl_basic_map_alloc_equality(dst
);
743 copy_constraint_dim_map(dst
->eq
[i1
], src
->eq
[i
], dim_map
);
746 for (i
= 0; i
< src
->n_ineq
; ++i
) {
747 int i1
= isl_basic_map_alloc_inequality(dst
);
750 copy_constraint_dim_map(dst
->ineq
[i1
], src
->ineq
[i
], dim_map
);
753 for (i
= 0; i
< src
->n_div
; ++i
) {
754 int i1
= isl_basic_map_alloc_div(dst
);
757 copy_div_dim_map(dst
->div
[i1
], src
->div
[i
], dim_map
);
761 isl_basic_map_free(src
);
766 isl_basic_map_free(src
);
767 isl_basic_map_free(dst
);
771 struct isl_basic_set
*isl_basic_set_add_constraints(struct isl_basic_set
*bset1
,
772 struct isl_basic_set
*bset2
, unsigned pos
)
774 return (struct isl_basic_set
*)
775 add_constraints((struct isl_basic_map
*)bset1
,
776 (struct isl_basic_map
*)bset2
, 0, pos
);
779 struct isl_basic_map
*isl_basic_map_extend_dim(struct isl_basic_map
*base
,
780 struct isl_dim
*dim
, unsigned extra
,
781 unsigned n_eq
, unsigned n_ineq
)
783 struct isl_basic_map
*ext
;
790 base
= isl_basic_map_cow(base
);
794 dims_ok
= isl_dim_equal(base
->dim
, dim
) &&
795 base
->extra
>= base
->n_div
+ extra
;
797 if (dims_ok
&& n_eq
== 0 && n_ineq
== 0) {
802 isl_assert(base
->ctx
, base
->dim
->nparam
<= dim
->nparam
, goto error
);
803 isl_assert(base
->ctx
, base
->dim
->n_in
<= dim
->n_in
, goto error
);
804 isl_assert(base
->ctx
, base
->dim
->n_out
<= dim
->n_out
, goto error
);
805 extra
+= base
->extra
;
807 n_ineq
+= base
->n_ineq
;
809 ext
= isl_basic_map_alloc_dim(dim
, extra
, n_eq
, n_ineq
);
815 ext
= add_constraints(ext
, base
, 0, 0);
818 ISL_F_CLR(ext
, ISL_BASIC_SET_FINAL
);
825 isl_basic_map_free(base
);
829 struct isl_basic_set
*isl_basic_set_extend_dim(struct isl_basic_set
*base
,
830 struct isl_dim
*dim
, unsigned extra
,
831 unsigned n_eq
, unsigned n_ineq
)
833 return (struct isl_basic_set
*)
834 isl_basic_map_extend_dim((struct isl_basic_map
*)base
, dim
,
835 extra
, n_eq
, n_ineq
);
838 struct isl_basic_map
*isl_basic_map_extend_constraints(
839 struct isl_basic_map
*base
, unsigned n_eq
, unsigned n_ineq
)
843 return isl_basic_map_extend_dim(base
, isl_dim_copy(base
->dim
),
847 struct isl_basic_map
*isl_basic_map_extend(struct isl_basic_map
*base
,
848 unsigned nparam
, unsigned n_in
, unsigned n_out
, unsigned extra
,
849 unsigned n_eq
, unsigned n_ineq
)
851 struct isl_basic_map
*bmap
;
856 dim
= isl_dim_alloc(base
->ctx
, nparam
, n_in
, n_out
);
860 bmap
= isl_basic_map_extend_dim(base
, dim
, extra
, n_eq
, n_ineq
);
864 struct isl_basic_set
*isl_basic_set_extend(struct isl_basic_set
*base
,
865 unsigned nparam
, unsigned dim
, unsigned extra
,
866 unsigned n_eq
, unsigned n_ineq
)
868 return (struct isl_basic_set
*)
869 isl_basic_map_extend((struct isl_basic_map
*)base
,
870 nparam
, 0, dim
, extra
, n_eq
, n_ineq
);
873 struct isl_basic_set
*isl_basic_set_extend_constraints(
874 struct isl_basic_set
*base
, unsigned n_eq
, unsigned n_ineq
)
876 return (struct isl_basic_set
*)
877 isl_basic_map_extend_constraints((struct isl_basic_map
*)base
,
881 struct isl_basic_set
*isl_basic_set_cow(struct isl_basic_set
*bset
)
883 return (struct isl_basic_set
*)
884 isl_basic_map_cow((struct isl_basic_map
*)bset
);
887 struct isl_basic_map
*isl_basic_map_cow(struct isl_basic_map
*bmap
)
894 bmap
= isl_basic_map_dup(bmap
);
896 ISL_F_CLR(bmap
, ISL_BASIC_SET_FINAL
);
900 struct isl_set
*isl_set_cow(struct isl_set
*set
)
908 return isl_set_dup(set
);
911 struct isl_map
*isl_map_cow(struct isl_map
*map
)
919 return isl_map_dup(map
);
922 static void swap_vars(struct isl_blk blk
, isl_int
*a
,
923 unsigned a_len
, unsigned b_len
)
925 isl_seq_cpy(blk
.data
, a
+a_len
, b_len
);
926 isl_seq_cpy(blk
.data
+b_len
, a
, a_len
);
927 isl_seq_cpy(a
, blk
.data
, b_len
+a_len
);
930 struct isl_basic_set
*isl_basic_set_swap_vars(
931 struct isl_basic_set
*bset
, unsigned n
)
941 nparam
= isl_basic_set_n_param(bset
);
942 dim
= isl_basic_set_n_dim(bset
);
943 isl_assert(bset
->ctx
, n
<= dim
, goto error
);
948 bset
= isl_basic_set_cow(bset
);
952 blk
= isl_blk_alloc(bset
->ctx
, dim
);
953 if (isl_blk_is_error(blk
))
956 for (i
= 0; i
< bset
->n_eq
; ++i
)
958 bset
->eq
[i
]+1+nparam
, n
, dim
- n
);
960 for (i
= 0; i
< bset
->n_ineq
; ++i
)
962 bset
->ineq
[i
]+1+nparam
, n
, dim
- n
);
964 for (i
= 0; i
< bset
->n_div
; ++i
)
966 bset
->div
[i
]+1+1+nparam
, n
, dim
- n
);
968 isl_blk_free(bset
->ctx
, blk
);
970 ISL_F_CLR(bset
, ISL_BASIC_SET_NORMALIZED
);
974 isl_basic_set_free(bset
);
978 struct isl_set
*isl_set_swap_vars(struct isl_set
*set
, unsigned n
)
981 set
= isl_set_cow(set
);
985 for (i
= 0; i
< set
->n
; ++i
) {
986 set
->p
[i
] = isl_basic_set_swap_vars(set
->p
[i
], n
);
992 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
996 struct isl_basic_map
*isl_basic_map_set_to_empty(struct isl_basic_map
*bmap
)
1002 total
= isl_basic_map_total_dim(bmap
);
1003 isl_basic_map_free_div(bmap
, bmap
->n_div
);
1004 isl_basic_map_free_inequality(bmap
, bmap
->n_ineq
);
1006 isl_basic_map_free_equality(bmap
, bmap
->n_eq
-1);
1008 isl_basic_map_alloc_equality(bmap
);
1012 isl_int_set_si(bmap
->eq
[i
][0], 1);
1013 isl_seq_clr(bmap
->eq
[i
]+1, total
);
1014 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
1015 return isl_basic_map_finalize(bmap
);
1017 isl_basic_map_free(bmap
);
1021 struct isl_basic_set
*isl_basic_set_set_to_empty(struct isl_basic_set
*bset
)
1023 return (struct isl_basic_set
*)
1024 isl_basic_map_set_to_empty((struct isl_basic_map
*)bset
);
1027 static void swap_div(struct isl_basic_map
*bmap
, int a
, int b
)
1030 unsigned off
= isl_dim_total(bmap
->dim
);
1031 isl_int
*t
= bmap
->div
[a
];
1032 bmap
->div
[a
] = bmap
->div
[b
];
1035 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1036 isl_int_swap(bmap
->eq
[i
][1+off
+a
], bmap
->eq
[i
][1+off
+b
]);
1038 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1039 isl_int_swap(bmap
->ineq
[i
][1+off
+a
], bmap
->ineq
[i
][1+off
+b
]);
1041 for (i
= 0; i
< bmap
->n_div
; ++i
)
1042 isl_int_swap(bmap
->div
[i
][1+1+off
+a
], bmap
->div
[i
][1+1+off
+b
]);
1043 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1046 /* Eliminate the specified n dimensions starting at first from the
1047 * constraints using Fourier-Motzkin, The dimensions themselves
1050 struct isl_set
*isl_set_eliminate_dims(struct isl_set
*set
,
1051 unsigned first
, unsigned n
)
1061 set
= isl_set_cow(set
);
1064 isl_assert(set
->ctx
, first
+n
<= isl_set_n_dim(set
), goto error
);
1065 nparam
= isl_set_n_param(set
);
1067 for (i
= 0; i
< set
->n
; ++i
) {
1068 set
->p
[i
] = isl_basic_set_eliminate_vars(set
->p
[i
],
1079 /* Project out n dimensions starting at first using Fourier-Motzkin */
1080 struct isl_set
*isl_set_remove_dims(struct isl_set
*set
,
1081 unsigned first
, unsigned n
)
1083 set
= isl_set_eliminate_dims(set
, first
, n
);
1084 set
= isl_set_drop_dims(set
, first
, n
);
1088 struct isl_basic_set
*isl_basic_set_remove_divs(struct isl_basic_set
*bset
)
1090 bset
= isl_basic_set_eliminate_vars(bset
, isl_dim_total(bset
->dim
),
1098 struct isl_set
*isl_set_remove_divs(struct isl_set
*set
)
1107 set
= isl_set_cow(set
);
1111 for (i
= 0; i
< set
->n
; ++i
) {
1112 set
->p
[i
] = isl_basic_set_remove_divs(set
->p
[i
]);
1122 struct isl_basic_map
*isl_basic_map_remove(struct isl_basic_map
*bmap
,
1123 enum isl_dim_type type
, unsigned first
, unsigned n
)
1127 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
1131 bmap
= isl_basic_map_eliminate_vars(bmap
,
1132 isl_basic_map_offset(bmap
, type
) - 1 + first
, n
);
1133 bmap
= isl_basic_map_drop(bmap
, type
, first
, n
);
1136 isl_basic_map_free(bmap
);
1140 struct isl_map
*isl_map_remove(struct isl_map
*map
,
1141 enum isl_dim_type type
, unsigned first
, unsigned n
)
1149 map
= isl_map_cow(map
);
1152 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
1154 for (i
= 0; i
< map
->n
; ++i
) {
1155 map
->p
[i
] = isl_basic_map_eliminate_vars(map
->p
[i
],
1156 isl_basic_map_offset(map
->p
[i
], type
) - 1 + first
, n
);
1160 map
= isl_map_drop(map
, type
, first
, n
);
1167 /* Project out n inputs starting at first using Fourier-Motzkin */
1168 struct isl_map
*isl_map_remove_inputs(struct isl_map
*map
,
1169 unsigned first
, unsigned n
)
1171 return isl_map_remove(map
, isl_dim_in
, first
, n
);
1174 /* Project out n dimensions starting at first using Fourier-Motzkin */
1175 struct isl_basic_set
*isl_basic_set_remove_dims(struct isl_basic_set
*bset
,
1176 unsigned first
, unsigned n
)
1178 unsigned nparam
= isl_basic_set_n_param(bset
);
1179 bset
= isl_basic_set_eliminate_vars(bset
, nparam
+ first
, n
);
1180 bset
= isl_basic_set_drop_dims(bset
, first
, n
);
1184 static void dump_term(struct isl_basic_map
*bmap
,
1185 isl_int c
, int pos
, FILE *out
)
1188 unsigned in
= isl_basic_map_n_in(bmap
);
1189 unsigned dim
= in
+ isl_basic_map_n_out(bmap
);
1190 unsigned nparam
= isl_basic_map_n_param(bmap
);
1192 isl_int_print(out
, c
, 0);
1194 if (!isl_int_is_one(c
))
1195 isl_int_print(out
, c
, 0);
1196 if (pos
< 1 + nparam
) {
1197 name
= isl_dim_get_name(bmap
->dim
,
1198 isl_dim_param
, pos
- 1);
1200 fprintf(out
, "%s", name
);
1202 fprintf(out
, "p%d", pos
- 1);
1203 } else if (pos
< 1 + nparam
+ in
)
1204 fprintf(out
, "i%d", pos
- 1 - nparam
);
1205 else if (pos
< 1 + nparam
+ dim
)
1206 fprintf(out
, "o%d", pos
- 1 - nparam
- in
);
1208 fprintf(out
, "e%d", pos
- 1 - nparam
- dim
);
1212 static void dump_constraint_sign(struct isl_basic_map
*bmap
, isl_int
*c
,
1213 int sign
, FILE *out
)
1217 unsigned len
= 1 + isl_basic_map_total_dim(bmap
);
1221 for (i
= 0, first
= 1; i
< len
; ++i
) {
1222 if (isl_int_sgn(c
[i
]) * sign
<= 0)
1225 fprintf(out
, " + ");
1227 isl_int_abs(v
, c
[i
]);
1228 dump_term(bmap
, v
, i
, out
);
1235 static void dump_constraint(struct isl_basic_map
*bmap
, isl_int
*c
,
1236 const char *op
, FILE *out
, int indent
)
1240 fprintf(out
, "%*s", indent
, "");
1242 dump_constraint_sign(bmap
, c
, 1, out
);
1243 fprintf(out
, " %s ", op
);
1244 dump_constraint_sign(bmap
, c
, -1, out
);
1248 for (i
= bmap
->n_div
; i
< bmap
->extra
; ++i
) {
1249 if (isl_int_is_zero(c
[1+isl_dim_total(bmap
->dim
)+i
]))
1251 fprintf(out
, "%*s", indent
, "");
1252 fprintf(out
, "ERROR: unused div coefficient not zero\n");
1257 static void dump_constraints(struct isl_basic_map
*bmap
,
1258 isl_int
**c
, unsigned n
,
1259 const char *op
, FILE *out
, int indent
)
1263 for (i
= 0; i
< n
; ++i
)
1264 dump_constraint(bmap
, c
[i
], op
, out
, indent
);
1267 static void dump_affine(struct isl_basic_map
*bmap
, isl_int
*exp
, FILE *out
)
1271 unsigned total
= isl_basic_map_total_dim(bmap
);
1273 for (j
= 0; j
< 1 + total
; ++j
) {
1274 if (isl_int_is_zero(exp
[j
]))
1276 if (!first
&& isl_int_is_pos(exp
[j
]))
1278 dump_term(bmap
, exp
[j
], j
, out
);
1283 static void dump(struct isl_basic_map
*bmap
, FILE *out
, int indent
)
1287 dump_constraints(bmap
, bmap
->eq
, bmap
->n_eq
, "=", out
, indent
);
1288 dump_constraints(bmap
, bmap
->ineq
, bmap
->n_ineq
, ">=", out
, indent
);
1290 for (i
= 0; i
< bmap
->n_div
; ++i
) {
1291 fprintf(out
, "%*s", indent
, "");
1292 fprintf(out
, "e%d = [(", i
);
1293 dump_affine(bmap
, bmap
->div
[i
]+1, out
);
1295 isl_int_print(out
, bmap
->div
[i
][0], 0);
1296 fprintf(out
, "]\n");
1300 void isl_basic_set_dump(struct isl_basic_set
*bset
, FILE *out
, int indent
)
1303 fprintf(out
, "null basic set\n");
1307 fprintf(out
, "%*s", indent
, "");
1308 fprintf(out
, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
1309 bset
->ref
, bset
->dim
->nparam
, bset
->dim
->n_out
,
1310 bset
->extra
, bset
->flags
);
1311 dump((struct isl_basic_map
*)bset
, out
, indent
);
1314 void isl_basic_map_dump(struct isl_basic_map
*bmap
, FILE *out
, int indent
)
1317 fprintf(out
, "null basic map\n");
1321 fprintf(out
, "%*s", indent
, "");
1322 fprintf(out
, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
1323 "flags: %x, n_name: %d\n",
1325 bmap
->dim
->nparam
, bmap
->dim
->n_in
, bmap
->dim
->n_out
,
1326 bmap
->extra
, bmap
->flags
, bmap
->dim
->n_name
);
1327 dump(bmap
, out
, indent
);
1330 int isl_inequality_negate(struct isl_basic_map
*bmap
, unsigned pos
)
1335 total
= isl_basic_map_total_dim(bmap
);
1336 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
1337 isl_seq_neg(bmap
->ineq
[pos
], bmap
->ineq
[pos
], 1 + total
);
1338 isl_int_sub_ui(bmap
->ineq
[pos
][0], bmap
->ineq
[pos
][0], 1);
1339 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1343 struct isl_set
*isl_set_alloc_dim(struct isl_dim
*dim
, int n
, unsigned flags
)
1345 struct isl_set
*set
;
1349 isl_assert(dim
->ctx
, dim
->n_in
== 0, return NULL
);
1350 isl_assert(dim
->ctx
, n
>= 0, return NULL
);
1351 set
= isl_alloc(dim
->ctx
, struct isl_set
,
1352 sizeof(struct isl_set
) +
1353 n
* sizeof(struct isl_basic_set
*));
1357 set
->ctx
= dim
->ctx
;
1358 isl_ctx_ref(set
->ctx
);
1370 struct isl_set
*isl_set_alloc(struct isl_ctx
*ctx
,
1371 unsigned nparam
, unsigned dim
, int n
, unsigned flags
)
1373 struct isl_set
*set
;
1374 struct isl_dim
*dims
;
1376 dims
= isl_dim_alloc(ctx
, nparam
, 0, dim
);
1380 set
= isl_set_alloc_dim(dims
, n
, flags
);
1384 struct isl_set
*isl_set_dup(struct isl_set
*set
)
1387 struct isl_set
*dup
;
1392 dup
= isl_set_alloc_dim(isl_dim_copy(set
->dim
), set
->n
, set
->flags
);
1395 for (i
= 0; i
< set
->n
; ++i
)
1396 dup
= isl_set_add(dup
, isl_basic_set_copy(set
->p
[i
]));
1400 struct isl_set
*isl_set_from_basic_set(struct isl_basic_set
*bset
)
1402 struct isl_set
*set
;
1407 set
= isl_set_alloc_dim(isl_dim_copy(bset
->dim
), 1, ISL_MAP_DISJOINT
);
1409 isl_basic_set_free(bset
);
1412 return isl_set_add(set
, bset
);
1415 struct isl_map
*isl_map_from_basic_map(struct isl_basic_map
*bmap
)
1417 struct isl_map
*map
;
1422 map
= isl_map_alloc_dim(isl_dim_copy(bmap
->dim
), 1, ISL_MAP_DISJOINT
);
1424 isl_basic_map_free(bmap
);
1427 return isl_map_add(map
, bmap
);
1430 struct isl_set
*isl_set_add(struct isl_set
*set
, struct isl_basic_set
*bset
)
1434 isl_assert(set
->ctx
, isl_dim_equal(set
->dim
, bset
->dim
), goto error
);
1435 isl_assert(set
->ctx
, set
->n
< set
->size
, goto error
);
1436 set
->p
[set
->n
] = bset
;
1443 isl_basic_set_free(bset
);
1447 void isl_set_free(struct isl_set
*set
)
1457 isl_ctx_deref(set
->ctx
);
1458 for (i
= 0; i
< set
->n
; ++i
)
1459 isl_basic_set_free(set
->p
[i
]);
1460 isl_dim_free(set
->dim
);
1464 void isl_set_dump(struct isl_set
*set
, FILE *out
, int indent
)
1469 fprintf(out
, "null set\n");
1473 fprintf(out
, "%*s", indent
, "");
1474 fprintf(out
, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
1475 set
->ref
, set
->n
, set
->dim
->nparam
, set
->dim
->n_out
,
1477 for (i
= 0; i
< set
->n
; ++i
) {
1478 fprintf(out
, "%*s", indent
, "");
1479 fprintf(out
, "basic set %d:\n", i
);
1480 isl_basic_set_dump(set
->p
[i
], out
, indent
+4);
1484 void isl_map_dump(struct isl_map
*map
, FILE *out
, int indent
)
1489 fprintf(out
, "null map\n");
1493 fprintf(out
, "%*s", indent
, "");
1494 fprintf(out
, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
1495 "flags: %x, n_name: %d\n",
1496 map
->ref
, map
->n
, map
->dim
->nparam
, map
->dim
->n_in
,
1497 map
->dim
->n_out
, map
->flags
, map
->dim
->n_name
);
1498 for (i
= 0; i
< map
->n
; ++i
) {
1499 fprintf(out
, "%*s", indent
, "");
1500 fprintf(out
, "basic map %d:\n", i
);
1501 isl_basic_map_dump(map
->p
[i
], out
, indent
+4);
1505 struct isl_basic_map
*isl_basic_map_intersect_domain(
1506 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
1508 struct isl_basic_map
*bmap_domain
;
1509 struct isl_dim
*dim
;
1514 isl_assert(bset
->ctx
, isl_dim_match(bmap
->dim
, isl_dim_param
,
1515 bset
->dim
, isl_dim_param
), goto error
);
1517 if (isl_dim_size(bset
->dim
, isl_dim_set
) != 0)
1518 isl_assert(bset
->ctx
,
1519 isl_basic_map_compatible_domain(bmap
, bset
), goto error
);
1521 bmap
= isl_basic_map_extend_dim(bmap
, isl_dim_copy(bmap
->dim
),
1522 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
1525 dim
= isl_dim_reverse(isl_dim_copy(bset
->dim
));
1526 bmap_domain
= isl_basic_map_from_basic_set(bset
, dim
);
1527 bmap
= add_constraints(bmap
, bmap_domain
, 0, 0);
1529 bmap
= isl_basic_map_simplify(bmap
);
1530 return isl_basic_map_finalize(bmap
);
1532 isl_basic_map_free(bmap
);
1533 isl_basic_set_free(bset
);
1537 struct isl_basic_map
*isl_basic_map_intersect_range(
1538 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
1540 struct isl_basic_map
*bmap_range
;
1545 isl_assert(bset
->ctx
, isl_dim_match(bmap
->dim
, isl_dim_param
,
1546 bset
->dim
, isl_dim_param
), goto error
);
1548 if (isl_dim_size(bset
->dim
, isl_dim_set
) != 0)
1549 isl_assert(bset
->ctx
,
1550 isl_basic_map_compatible_range(bmap
, bset
), goto error
);
1552 bmap
= isl_basic_map_extend_dim(bmap
, isl_dim_copy(bmap
->dim
),
1553 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
1556 bmap_range
= isl_basic_map_from_basic_set(bset
, isl_dim_copy(bset
->dim
));
1557 bmap
= add_constraints(bmap
, bmap_range
, 0, 0);
1559 bmap
= isl_basic_map_simplify(bmap
);
1560 return isl_basic_map_finalize(bmap
);
1562 isl_basic_map_free(bmap
);
1563 isl_basic_set_free(bset
);
1567 static int basic_map_contains(struct isl_basic_map
*bmap
, struct isl_vec
*vec
)
1573 total
= 1 + isl_basic_map_total_dim(bmap
);
1574 if (total
!= vec
->size
)
1579 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
1580 isl_seq_inner_product(vec
->block
.data
, bmap
->eq
[i
], total
, &s
);
1581 if (!isl_int_is_zero(s
)) {
1587 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
1588 isl_seq_inner_product(vec
->block
.data
, bmap
->ineq
[i
], total
, &s
);
1589 if (isl_int_is_neg(s
)) {
1600 struct isl_basic_map
*isl_basic_map_intersect(
1601 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
1603 struct isl_vec
*sample
= NULL
;
1605 if (!bmap1
|| !bmap2
)
1608 isl_assert(bmap1
->ctx
, isl_dim_match(bmap1
->dim
, isl_dim_param
,
1609 bmap2
->dim
, isl_dim_param
), goto error
);
1610 if (isl_dim_total(bmap1
->dim
) ==
1611 isl_dim_size(bmap1
->dim
, isl_dim_param
) &&
1612 isl_dim_total(bmap2
->dim
) !=
1613 isl_dim_size(bmap2
->dim
, isl_dim_param
))
1614 return isl_basic_map_intersect(bmap2
, bmap1
);
1616 if (isl_dim_total(bmap2
->dim
) !=
1617 isl_dim_size(bmap2
->dim
, isl_dim_param
))
1618 isl_assert(bmap1
->ctx
,
1619 isl_dim_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
1621 if (bmap1
->sample
&&
1622 basic_map_contains(bmap1
, bmap1
->sample
) > 0 &&
1623 basic_map_contains(bmap2
, bmap1
->sample
) > 0)
1624 sample
= isl_vec_copy(bmap1
->ctx
, bmap1
->sample
);
1625 else if (bmap2
->sample
&&
1626 basic_map_contains(bmap1
, bmap2
->sample
) > 0 &&
1627 basic_map_contains(bmap2
, bmap2
->sample
) > 0)
1628 sample
= isl_vec_copy(bmap2
->ctx
, bmap2
->sample
);
1630 bmap1
= isl_basic_map_extend_dim(bmap1
, isl_dim_copy(bmap1
->dim
),
1631 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
1634 bmap1
= add_constraints(bmap1
, bmap2
, 0, 0);
1637 isl_vec_free(bmap1
->ctx
, bmap1
->sample
);
1638 bmap1
->sample
= sample
;
1641 bmap1
= isl_basic_map_simplify(bmap1
);
1642 return isl_basic_map_finalize(bmap1
);
1645 isl_vec_free(bmap1
->ctx
, sample
);
1646 isl_basic_map_free(bmap1
);
1647 isl_basic_map_free(bmap2
);
1651 struct isl_basic_set
*isl_basic_set_intersect(
1652 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
1654 return (struct isl_basic_set
*)
1655 isl_basic_map_intersect(
1656 (struct isl_basic_map
*)bset1
,
1657 (struct isl_basic_map
*)bset2
);
1660 struct isl_map
*isl_map_intersect(struct isl_map
*map1
, struct isl_map
*map2
)
1663 struct isl_map
*result
;
1669 isl_assert(map1
->ctx
, isl_dim_match(map1
->dim
, isl_dim_param
,
1670 map2
->dim
, isl_dim_param
), goto error
);
1671 if (isl_dim_total(map1
->dim
) ==
1672 isl_dim_size(map1
->dim
, isl_dim_param
) &&
1673 isl_dim_total(map2
->dim
) != isl_dim_size(map2
->dim
, isl_dim_param
))
1674 return isl_map_intersect(map2
, map1
);
1676 if (isl_dim_total(map2
->dim
) != isl_dim_size(map2
->dim
, isl_dim_param
))
1677 isl_assert(map1
->ctx
,
1678 isl_dim_equal(map1
->dim
, map2
->dim
), goto error
);
1680 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
1681 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
1682 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
1684 result
= isl_map_alloc_dim(isl_dim_copy(map1
->dim
),
1685 map1
->n
* map2
->n
, flags
);
1688 for (i
= 0; i
< map1
->n
; ++i
)
1689 for (j
= 0; j
< map2
->n
; ++j
) {
1690 struct isl_basic_map
*part
;
1691 part
= isl_basic_map_intersect(
1692 isl_basic_map_copy(map1
->p
[i
]),
1693 isl_basic_map_copy(map2
->p
[j
]));
1694 if (isl_basic_map_is_empty(part
))
1695 isl_basic_map_free(part
);
1697 result
= isl_map_add(result
, part
);
1710 struct isl_set
*isl_set_intersect(struct isl_set
*set1
, struct isl_set
*set2
)
1712 return (struct isl_set
*)
1713 isl_map_intersect((struct isl_map
*)set1
,
1714 (struct isl_map
*)set2
);
1717 struct isl_basic_map
*isl_basic_map_reverse(struct isl_basic_map
*bmap
)
1719 struct isl_dim
*dim
;
1720 struct isl_basic_set
*bset
;
1725 bmap
= isl_basic_map_cow(bmap
);
1728 dim
= isl_dim_reverse(isl_dim_copy(bmap
->dim
));
1729 in
= isl_basic_map_n_in(bmap
);
1730 bset
= isl_basic_set_from_basic_map(bmap
);
1731 bset
= isl_basic_set_swap_vars(bset
, in
);
1732 return isl_basic_map_from_basic_set(bset
, dim
);
1735 /* Turn final n dimensions into existentially quantified variables.
1737 struct isl_basic_set
*isl_basic_set_project_out(
1738 struct isl_basic_set
*bset
, unsigned n
, unsigned flags
)
1748 isl_assert(bset
->ctx
, n
<= isl_basic_set_n_dim(bset
), goto error
);
1753 bset
= isl_basic_set_cow(bset
);
1755 row_size
= 1 + isl_dim_total(bset
->dim
) + bset
->extra
;
1756 old
= bset
->block2
.data
;
1757 bset
->block2
= isl_blk_extend(bset
->ctx
, bset
->block2
,
1758 (bset
->extra
+ n
) * (1 + row_size
));
1759 if (!bset
->block2
.data
)
1761 new_div
= isl_alloc_array(ctx
, isl_int
*, bset
->extra
+ n
);
1764 for (i
= 0; i
< n
; ++i
) {
1765 new_div
[i
] = bset
->block2
.data
+
1766 (bset
->extra
+ i
) * (1 + row_size
);
1767 isl_seq_clr(new_div
[i
], 1 + row_size
);
1769 for (i
= 0; i
< bset
->extra
; ++i
)
1770 new_div
[n
+ i
] = bset
->block2
.data
+ (bset
->div
[i
] - old
);
1772 bset
->div
= new_div
;
1775 bset
->dim
= isl_dim_drop_outputs(bset
->dim
,
1776 isl_basic_set_n_dim(bset
) - n
, n
);
1779 bset
= isl_basic_set_simplify(bset
);
1780 return isl_basic_set_finalize(bset
);
1782 isl_basic_set_free(bset
);
1786 struct isl_basic_map
*add_divs(struct isl_basic_map
*bmap
, unsigned n
)
1790 for (i
= 0; i
< n
; ++i
) {
1791 j
= isl_basic_map_alloc_div(bmap
);
1794 isl_seq_clr(bmap
->div
[j
], 1+1+isl_basic_map_total_dim(bmap
));
1798 isl_basic_map_free(bmap
);
1802 struct isl_basic_map
*isl_basic_map_apply_range(
1803 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
1805 struct isl_dim
*dim_result
= NULL
;
1806 struct isl_basic_map
*bmap
;
1807 unsigned n_in
, n_out
, n
, nparam
, total
, pos
;
1808 struct isl_dim_map
*dim_map1
, *dim_map2
;
1810 if (!bmap1
|| !bmap2
)
1813 dim_result
= isl_dim_join(isl_dim_copy(bmap1
->dim
),
1814 isl_dim_copy(bmap2
->dim
));
1816 n_in
= isl_basic_map_n_in(bmap1
);
1817 n_out
= isl_basic_map_n_out(bmap2
);
1818 n
= isl_basic_map_n_out(bmap1
);
1819 nparam
= isl_basic_map_n_param(bmap1
);
1821 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ n
;
1822 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
1823 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
1824 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
1825 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
1826 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
1827 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_in
);
1828 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_out
);
1829 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
1830 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
1831 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
1833 bmap
= isl_basic_map_alloc_dim(dim_result
,
1834 bmap1
->n_div
+ bmap2
->n_div
+ n
,
1835 bmap1
->n_eq
+ bmap2
->n_eq
,
1836 bmap1
->n_ineq
+ bmap2
->n_ineq
);
1837 bmap
= add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
1838 bmap
= add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
1839 bmap
= add_divs(bmap
, n
);
1840 bmap
= isl_basic_map_simplify(bmap
);
1841 return isl_basic_map_finalize(bmap
);
1843 isl_basic_map_free(bmap1
);
1844 isl_basic_map_free(bmap2
);
1848 struct isl_basic_set
*isl_basic_set_apply(
1849 struct isl_basic_set
*bset
, struct isl_basic_map
*bmap
)
1854 isl_assert(set
->ctx
, isl_basic_map_compatible_domain(bmap
, bset
),
1857 return (struct isl_basic_set
*)
1858 isl_basic_map_apply_range((struct isl_basic_map
*)bset
, bmap
);
1860 isl_basic_set_free(bset
);
1861 isl_basic_map_free(bmap
);
1865 struct isl_basic_map
*isl_basic_map_apply_domain(
1866 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
1868 if (!bmap1
|| !bmap2
)
1872 isl_basic_map_n_in(bmap1
) == isl_basic_map_n_in(bmap2
), goto error
);
1874 isl_basic_map_n_param(bmap1
) == isl_basic_map_n_param(bmap2
),
1877 bmap1
= isl_basic_map_reverse(bmap1
);
1878 bmap1
= isl_basic_map_apply_range(bmap1
, bmap2
);
1879 return isl_basic_map_reverse(bmap1
);
1881 isl_basic_map_free(bmap1
);
1882 isl_basic_map_free(bmap2
);
1886 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
1887 * A \cap B -> f(A) + f(B)
1889 struct isl_basic_map
*isl_basic_map_sum(
1890 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
1892 unsigned n_in
, n_out
, nparam
, total
, pos
;
1893 struct isl_basic_map
*bmap
= NULL
;
1894 struct isl_dim_map
*dim_map1
, *dim_map2
;
1897 if (!bmap1
|| !bmap2
)
1900 isl_assert(bmap1
->ctx
, isl_dim_equal(bmap1
->dim
, bmap2
->dim
),
1903 nparam
= isl_basic_map_n_param(bmap1
);
1904 n_in
= isl_basic_map_n_in(bmap1
);
1905 n_out
= isl_basic_map_n_out(bmap1
);
1907 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
;
1908 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
1909 dim_map2
= isl_dim_map_alloc(bmap2
->ctx
, total
);
1910 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
1911 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
);
1912 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
1913 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
1914 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_in
+ n_out
);
1915 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
1916 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
1917 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_out
);
1919 bmap
= isl_basic_map_alloc_dim(isl_dim_copy(bmap1
->dim
),
1920 bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
,
1921 bmap1
->n_eq
+ bmap2
->n_eq
+ n_out
,
1922 bmap1
->n_ineq
+ bmap2
->n_ineq
);
1923 for (i
= 0; i
< n_out
; ++i
) {
1924 int j
= isl_basic_map_alloc_equality(bmap
);
1927 isl_seq_clr(bmap
->eq
[j
], 1+total
);
1928 isl_int_set_si(bmap
->eq
[j
][1+nparam
+n_in
+i
], -1);
1929 isl_int_set_si(bmap
->eq
[j
][1+pos
+i
], 1);
1930 isl_int_set_si(bmap
->eq
[j
][1+pos
-n_out
+i
], 1);
1932 bmap
= add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
1933 bmap
= add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
1934 bmap
= add_divs(bmap
, 2 * n_out
);
1936 bmap
= isl_basic_map_simplify(bmap
);
1937 return isl_basic_map_finalize(bmap
);
1939 isl_basic_map_free(bmap
);
1940 isl_basic_map_free(bmap1
);
1941 isl_basic_map_free(bmap2
);
1945 /* Given a basic map A -> f(A), construct A -> -f(A).
1947 struct isl_basic_map
*isl_basic_map_neg(struct isl_basic_map
*bmap
)
1952 bmap
= isl_basic_map_cow(bmap
);
1956 n
= isl_basic_map_dim(bmap
, isl_dim_out
);
1957 off
= isl_basic_map_offset(bmap
, isl_dim_out
);
1958 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1959 for (j
= 0; j
< n
; ++j
)
1960 isl_int_neg(bmap
->eq
[i
][off
+j
], bmap
->eq
[i
][off
+j
]);
1961 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1962 for (j
= 0; j
< n
; ++j
)
1963 isl_int_neg(bmap
->ineq
[i
][off
+j
], bmap
->ineq
[i
][off
+j
]);
1964 for (i
= 0; i
< bmap
->n_div
; ++i
)
1965 for (j
= 0; j
< n
; ++j
)
1966 isl_int_neg(bmap
->div
[i
][1+off
+j
], bmap
->div
[i
][1+off
+j
]);
1967 return isl_basic_map_finalize(bmap
);
1970 /* Given a basic map A -> f(A) and an integer d, construct a basic map
1971 * A -> floor(f(A)/d).
1973 struct isl_basic_map
*isl_basic_map_floordiv(struct isl_basic_map
*bmap
,
1976 unsigned n_in
, n_out
, nparam
, total
, pos
;
1977 struct isl_basic_map
*result
= NULL
;
1978 struct isl_dim_map
*dim_map
;
1984 nparam
= isl_basic_map_n_param(bmap
);
1985 n_in
= isl_basic_map_n_in(bmap
);
1986 n_out
= isl_basic_map_n_out(bmap
);
1988 total
= nparam
+ n_in
+ n_out
+ bmap
->n_div
+ n_out
;
1989 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
1990 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_param
, pos
= 0);
1991 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_in
, pos
+= nparam
);
1992 isl_dim_map_div(dim_map
, bmap
, pos
+= n_in
+ n_out
);
1993 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_out
, pos
+= bmap
->n_div
);
1995 result
= isl_basic_map_alloc_dim(isl_dim_copy(bmap
->dim
),
1996 bmap
->n_div
+ n_out
,
1997 bmap
->n_eq
, bmap
->n_ineq
+ 2 * n_out
);
1998 result
= add_constraints_dim_map(result
, bmap
, dim_map
);
1999 result
= add_divs(result
, n_out
);
2000 for (i
= 0; i
< n_out
; ++i
) {
2002 j
= isl_basic_map_alloc_inequality(result
);
2005 isl_seq_clr(result
->ineq
[j
], 1+total
);
2006 isl_int_neg(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
2007 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], 1);
2008 j
= isl_basic_map_alloc_inequality(result
);
2011 isl_seq_clr(result
->ineq
[j
], 1+total
);
2012 isl_int_set(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
2013 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], -1);
2014 isl_int_sub_ui(result
->ineq
[j
][0], d
, 1);
2017 result
= isl_basic_map_simplify(result
);
2018 return isl_basic_map_finalize(result
);
2020 isl_basic_map_free(result
);
2024 static struct isl_basic_map
*var_equal(struct isl_basic_map
*bmap
, unsigned pos
)
2030 i
= isl_basic_map_alloc_equality(bmap
);
2033 nparam
= isl_basic_map_n_param(bmap
);
2034 n_in
= isl_basic_map_n_in(bmap
);
2035 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
2036 isl_int_set_si(bmap
->eq
[i
][1+nparam
+pos
], -1);
2037 isl_int_set_si(bmap
->eq
[i
][1+nparam
+n_in
+pos
], 1);
2038 return isl_basic_map_finalize(bmap
);
2040 isl_basic_map_free(bmap
);
2044 static struct isl_basic_map
*var_more(struct isl_basic_map
*bmap
, unsigned pos
)
2050 i
= isl_basic_map_alloc_inequality(bmap
);
2053 nparam
= isl_basic_map_n_param(bmap
);
2054 n_in
= isl_basic_map_n_in(bmap
);
2055 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
2056 isl_int_set_si(bmap
->ineq
[i
][0], -1);
2057 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
2058 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
2059 return isl_basic_map_finalize(bmap
);
2061 isl_basic_map_free(bmap
);
2065 static struct isl_basic_map
*var_less(struct isl_basic_map
*bmap
, unsigned pos
)
2071 i
= isl_basic_map_alloc_inequality(bmap
);
2074 nparam
= isl_basic_map_n_param(bmap
);
2075 n_in
= isl_basic_map_n_in(bmap
);
2076 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
2077 isl_int_set_si(bmap
->ineq
[i
][0], -1);
2078 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
2079 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
2080 return isl_basic_map_finalize(bmap
);
2082 isl_basic_map_free(bmap
);
2086 struct isl_basic_map
*isl_basic_map_equal(struct isl_dim
*dim
, unsigned n_equal
)
2089 struct isl_basic_map
*bmap
;
2090 bmap
= isl_basic_map_alloc_dim(dim
, 0, n_equal
, 0);
2093 for (i
= 0; i
< n_equal
&& bmap
; ++i
)
2094 bmap
= var_equal(bmap
, i
);
2095 return isl_basic_map_finalize(bmap
);
2098 struct isl_basic_map
*isl_basic_map_less_at(struct isl_dim
*dim
, unsigned pos
)
2101 struct isl_basic_map
*bmap
;
2102 bmap
= isl_basic_map_alloc_dim(dim
, 0, pos
, 1);
2105 for (i
= 0; i
< pos
&& bmap
; ++i
)
2106 bmap
= var_equal(bmap
, i
);
2108 bmap
= var_less(bmap
, pos
);
2109 return isl_basic_map_finalize(bmap
);
2112 struct isl_basic_map
*isl_basic_map_more_at(struct isl_dim
*dim
, unsigned pos
)
2115 struct isl_basic_map
*bmap
;
2116 bmap
= isl_basic_map_alloc_dim(dim
, 0, pos
, 1);
2119 for (i
= 0; i
< pos
&& bmap
; ++i
)
2120 bmap
= var_equal(bmap
, i
);
2122 bmap
= var_more(bmap
, pos
);
2123 return isl_basic_map_finalize(bmap
);
2126 struct isl_basic_map
*isl_basic_map_from_basic_set(
2127 struct isl_basic_set
*bset
, struct isl_dim
*dim
)
2129 struct isl_basic_map
*bmap
;
2131 bset
= isl_basic_set_cow(bset
);
2135 isl_assert(bset
->ctx
, isl_dim_compatible(bset
->dim
, dim
), goto error
);
2136 isl_dim_free(bset
->dim
);
2137 bmap
= (struct isl_basic_map
*) bset
;
2139 return isl_basic_map_finalize(bmap
);
2141 isl_basic_set_free(bset
);
2146 struct isl_basic_set
*isl_basic_set_from_basic_map(struct isl_basic_map
*bmap
)
2150 if (bmap
->dim
->n_in
== 0)
2151 return (struct isl_basic_set
*)bmap
;
2152 bmap
= isl_basic_map_cow(bmap
);
2155 bmap
->dim
= isl_dim_cow(bmap
->dim
);
2158 bmap
->dim
->n_out
+= bmap
->dim
->n_in
;
2159 bmap
->dim
->n_in
= 0;
2160 bmap
= isl_basic_map_finalize(bmap
);
2161 return (struct isl_basic_set
*)bmap
;
2163 isl_basic_map_free(bmap
);
2167 /* For a div d = floor(f/m), add the constraints
2170 * -(f-(n-1)) + m d >= 0
2172 * Note that the second constraint is the negation of
2176 static int add_div_constraints(struct isl_basic_map
*bmap
, unsigned div
)
2179 unsigned total
= isl_basic_map_total_dim(bmap
);
2180 unsigned div_pos
= 1 + total
- bmap
->n_div
+ div
;
2182 i
= isl_basic_map_alloc_inequality(bmap
);
2185 isl_seq_cpy(bmap
->ineq
[i
], bmap
->div
[div
]+1, 1+total
);
2186 isl_int_neg(bmap
->ineq
[i
][div_pos
], bmap
->div
[div
][0]);
2188 j
= isl_basic_map_alloc_inequality(bmap
);
2191 isl_seq_neg(bmap
->ineq
[j
], bmap
->ineq
[i
], 1 + total
);
2192 isl_int_add(bmap
->ineq
[j
][0], bmap
->ineq
[j
][0], bmap
->ineq
[j
][div_pos
]);
2193 isl_int_sub_ui(bmap
->ineq
[j
][0], bmap
->ineq
[j
][0], 1);
2197 struct isl_basic_set
*isl_basic_map_underlying_set(
2198 struct isl_basic_map
*bmap
)
2202 if (bmap
->dim
->nparam
== 0 && bmap
->dim
->n_in
== 0 && bmap
->n_div
== 0)
2203 return (struct isl_basic_set
*)bmap
;
2204 bmap
= isl_basic_map_cow(bmap
);
2207 bmap
->dim
= isl_dim_underlying(bmap
->dim
, bmap
->n_div
);
2210 bmap
->extra
-= bmap
->n_div
;
2212 bmap
= isl_basic_map_finalize(bmap
);
2213 return (struct isl_basic_set
*)bmap
;
2218 struct isl_basic_map
*isl_basic_map_overlying_set(
2219 struct isl_basic_set
*bset
, struct isl_basic_map
*like
)
2221 struct isl_basic_map
*bmap
;
2222 struct isl_ctx
*ctx
;
2229 isl_assert(ctx
, bset
->n_div
== 0, goto error
);
2230 isl_assert(ctx
, isl_basic_set_n_param(bset
) == 0, goto error
);
2231 isl_assert(ctx
, bset
->dim
->n_out
== isl_basic_map_total_dim(like
),
2233 if (isl_dim_equal(bset
->dim
, like
->dim
) && like
->n_div
== 0) {
2234 isl_basic_map_free(like
);
2235 return (struct isl_basic_map
*)bset
;
2237 bset
= isl_basic_set_cow(bset
);
2240 total
= bset
->dim
->n_out
+ bset
->extra
;
2241 bmap
= (struct isl_basic_map
*)bset
;
2242 isl_dim_free(bmap
->dim
);
2243 bmap
->dim
= isl_dim_copy(like
->dim
);
2246 bmap
->n_div
= like
->n_div
;
2247 bmap
->extra
+= like
->n_div
;
2250 ltotal
= total
- bmap
->extra
+ like
->extra
;
2253 bmap
->block2
= isl_blk_extend(ctx
, bmap
->block2
,
2254 bmap
->extra
* (1 + 1 + total
));
2255 if (isl_blk_is_error(bmap
->block2
))
2257 bmap
->div
= isl_realloc_array(ctx
, bmap
->div
, isl_int
*,
2261 for (i
= 0; i
< bmap
->extra
; ++i
)
2262 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + 1 + total
);
2263 for (i
= 0; i
< like
->n_div
; ++i
) {
2264 isl_seq_cpy(bmap
->div
[i
], like
->div
[i
], 1 + 1 + ltotal
);
2265 isl_seq_clr(bmap
->div
[i
]+1+1+ltotal
, total
- ltotal
);
2267 bmap
= isl_basic_map_extend_constraints(bmap
,
2268 0, 2 * like
->n_div
);
2269 for (i
= 0; i
< like
->n_div
; ++i
) {
2270 if (isl_int_is_zero(bmap
->div
[i
][0]))
2272 if (add_div_constraints(bmap
, i
) < 0)
2276 isl_basic_map_free(like
);
2277 bmap
= isl_basic_map_simplify(bmap
);
2278 bmap
= isl_basic_map_finalize(bmap
);
2281 isl_basic_map_free(like
);
2282 isl_basic_set_free(bset
);
2286 struct isl_basic_set
*isl_basic_set_from_underlying_set(
2287 struct isl_basic_set
*bset
, struct isl_basic_set
*like
)
2289 return (struct isl_basic_set
*)
2290 isl_basic_map_overlying_set(bset
, (struct isl_basic_map
*)like
);
2293 struct isl_set
*isl_set_from_underlying_set(
2294 struct isl_set
*set
, struct isl_basic_set
*like
)
2300 isl_assert(set
->ctx
, set
->dim
->n_out
== isl_basic_set_total_dim(like
),
2302 if (isl_dim_equal(set
->dim
, like
->dim
) && like
->n_div
== 0) {
2303 isl_basic_set_free(like
);
2306 set
= isl_set_cow(set
);
2309 for (i
= 0; i
< set
->n
; ++i
) {
2310 set
->p
[i
] = isl_basic_set_from_underlying_set(set
->p
[i
],
2311 isl_basic_set_copy(like
));
2315 isl_dim_free(set
->dim
);
2316 set
->dim
= isl_dim_copy(like
->dim
);
2319 isl_basic_set_free(like
);
2322 isl_basic_set_free(like
);
2327 struct isl_set
*isl_map_underlying_set(struct isl_map
*map
)
2331 map
= isl_map_cow(map
);
2334 map
->dim
= isl_dim_cow(map
->dim
);
2338 for (i
= 1; i
< map
->n
; ++i
)
2339 isl_assert(map
->ctx
, map
->p
[0]->n_div
== map
->p
[i
]->n_div
,
2341 for (i
= 0; i
< map
->n
; ++i
) {
2342 map
->p
[i
] = (struct isl_basic_map
*)
2343 isl_basic_map_underlying_set(map
->p
[i
]);
2348 map
->dim
= isl_dim_underlying(map
->dim
, 0);
2350 isl_dim_free(map
->dim
);
2351 map
->dim
= isl_dim_copy(map
->p
[0]->dim
);
2355 return (struct isl_set
*)map
;
2361 struct isl_set
*isl_set_to_underlying_set(struct isl_set
*set
)
2363 return (struct isl_set
*)isl_map_underlying_set((struct isl_map
*)set
);
2366 struct isl_basic_set
*isl_basic_map_domain(struct isl_basic_map
*bmap
)
2368 struct isl_basic_set
*domain
;
2372 n_out
= isl_basic_map_n_out(bmap
);
2373 domain
= isl_basic_set_from_basic_map(bmap
);
2374 return isl_basic_set_project_out(domain
, n_out
, 0);
2377 struct isl_basic_set
*isl_basic_map_range(struct isl_basic_map
*bmap
)
2379 return isl_basic_map_domain(isl_basic_map_reverse(bmap
));
2382 struct isl_set
*isl_map_range(struct isl_map
*map
)
2385 struct isl_set
*set
;
2389 map
= isl_map_cow(map
);
2393 set
= (struct isl_set
*) map
;
2394 if (set
->dim
->n_in
!= 0) {
2395 set
->dim
= isl_dim_drop_inputs(set
->dim
, 0, set
->dim
->n_in
);
2399 for (i
= 0; i
< map
->n
; ++i
) {
2400 set
->p
[i
] = isl_basic_map_range(map
->p
[i
]);
2404 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
2405 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
2412 struct isl_map
*isl_map_from_set(struct isl_set
*set
, struct isl_dim
*dim
)
2415 struct isl_map
*map
= NULL
;
2417 set
= isl_set_cow(set
);
2420 isl_assert(set
->ctx
, isl_dim_compatible(set
->dim
, dim
), goto error
);
2421 map
= (struct isl_map
*)set
;
2422 for (i
= 0; i
< set
->n
; ++i
) {
2423 map
->p
[i
] = isl_basic_map_from_basic_set(
2424 set
->p
[i
], isl_dim_copy(dim
));
2428 isl_dim_free(map
->dim
);
2437 struct isl_map
*isl_map_from_range(struct isl_set
*set
)
2439 return (struct isl_map
*)set
;
2442 struct isl_set
*isl_set_from_map(struct isl_map
*map
)
2445 struct isl_set
*set
= NULL
;
2449 map
= isl_map_cow(map
);
2452 map
->dim
= isl_dim_cow(map
->dim
);
2455 map
->dim
->n_out
+= map
->dim
->n_in
;
2457 set
= (struct isl_set
*)map
;
2458 for (i
= 0; i
< map
->n
; ++i
) {
2459 set
->p
[i
] = isl_basic_set_from_basic_map(map
->p
[i
]);
2469 struct isl_map
*isl_map_alloc_dim(struct isl_dim
*dim
, int n
, unsigned flags
)
2471 struct isl_map
*map
;
2475 isl_assert(dim
->ctx
, n
>= 0, return NULL
);
2476 map
= isl_alloc(dim
->ctx
, struct isl_map
,
2477 sizeof(struct isl_map
) +
2478 n
* sizeof(struct isl_basic_map
*));
2482 map
->ctx
= dim
->ctx
;
2483 isl_ctx_ref(map
->ctx
);
2495 struct isl_map
*isl_map_alloc(struct isl_ctx
*ctx
,
2496 unsigned nparam
, unsigned in
, unsigned out
, int n
,
2499 struct isl_map
*map
;
2500 struct isl_dim
*dims
;
2502 dims
= isl_dim_alloc(ctx
, nparam
, in
, out
);
2506 map
= isl_map_alloc_dim(dims
, n
, flags
);
2510 struct isl_basic_map
*isl_basic_map_empty(struct isl_ctx
*ctx
,
2511 unsigned nparam
, unsigned in
, unsigned out
)
2513 struct isl_basic_map
*bmap
;
2514 bmap
= isl_basic_map_alloc(ctx
, nparam
, in
, out
, 0, 1, 0);
2515 bmap
= isl_basic_map_set_to_empty(bmap
);
2519 struct isl_basic_set
*isl_basic_set_empty(struct isl_dim
*dim
)
2521 struct isl_basic_set
*bset
;
2522 bset
= isl_basic_set_alloc_dim(dim
, 0, 1, 0);
2523 bset
= isl_basic_set_set_to_empty(bset
);
2527 struct isl_basic_map
*isl_basic_map_empty_like(struct isl_basic_map
*model
)
2529 struct isl_basic_map
*bmap
;
2532 bmap
= isl_basic_map_alloc_dim(isl_dim_copy(model
->dim
), 0, 1, 0);
2533 bmap
= isl_basic_map_set_to_empty(bmap
);
2537 struct isl_basic_map
*isl_basic_map_empty_like_map(struct isl_map
*model
)
2539 struct isl_basic_map
*bmap
;
2542 bmap
= isl_basic_map_alloc_dim(isl_dim_copy(model
->dim
), 0, 1, 0);
2543 bmap
= isl_basic_map_set_to_empty(bmap
);
2547 struct isl_basic_set
*isl_basic_set_empty_like(struct isl_basic_set
*model
)
2549 struct isl_basic_set
*bset
;
2552 bset
= isl_basic_set_alloc_dim(isl_dim_copy(model
->dim
), 0, 1, 0);
2553 bset
= isl_basic_set_set_to_empty(bset
);
2557 struct isl_basic_map
*isl_basic_map_universe(struct isl_dim
*dim
)
2559 struct isl_basic_map
*bmap
;
2560 bmap
= isl_basic_map_alloc_dim(dim
, 0, 0, 0);
2564 struct isl_basic_set
*isl_basic_set_universe(struct isl_dim
*dim
)
2566 struct isl_basic_set
*bset
;
2567 bset
= isl_basic_set_alloc_dim(dim
, 0, 0, 0);
2571 struct isl_basic_set
*isl_basic_set_universe_like(struct isl_basic_set
*model
)
2575 return isl_basic_set_alloc_dim(isl_dim_copy(model
->dim
), 0, 0, 0);
2578 struct isl_map
*isl_map_empty(struct isl_dim
*dim
)
2580 return isl_map_alloc_dim(dim
, 0, ISL_MAP_DISJOINT
);
2583 struct isl_map
*isl_map_empty_like(struct isl_map
*model
)
2587 return isl_map_alloc_dim(isl_dim_copy(model
->dim
), 0, ISL_MAP_DISJOINT
);
2590 struct isl_map
*isl_map_empty_like_basic_map(struct isl_basic_map
*model
)
2594 return isl_map_alloc_dim(isl_dim_copy(model
->dim
), 0, ISL_MAP_DISJOINT
);
2597 struct isl_set
*isl_set_empty(struct isl_dim
*dim
)
2599 return isl_set_alloc_dim(dim
, 0, ISL_MAP_DISJOINT
);
2602 struct isl_set
*isl_set_empty_like(struct isl_set
*model
)
2606 return isl_set_empty(isl_dim_copy(model
->dim
));
2609 struct isl_set
*isl_set_universe(struct isl_dim
*dim
)
2611 struct isl_set
*set
;
2614 set
= isl_set_alloc_dim(isl_dim_copy(dim
), 1, ISL_MAP_DISJOINT
);
2615 set
= isl_set_add(set
, isl_basic_set_universe(dim
));
2619 struct isl_map
*isl_map_dup(struct isl_map
*map
)
2622 struct isl_map
*dup
;
2626 dup
= isl_map_alloc_dim(isl_dim_copy(map
->dim
), map
->n
, map
->flags
);
2627 for (i
= 0; i
< map
->n
; ++i
)
2628 dup
= isl_map_add(dup
, isl_basic_map_copy(map
->p
[i
]));
2632 struct isl_map
*isl_map_add(struct isl_map
*map
, struct isl_basic_map
*bmap
)
2636 isl_assert(map
->ctx
, isl_dim_equal(map
->dim
, bmap
->dim
), goto error
);
2637 isl_assert(map
->ctx
, map
->n
< map
->size
, goto error
);
2638 map
->p
[map
->n
] = bmap
;
2640 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
2646 isl_basic_map_free(bmap
);
2650 void isl_map_free(struct isl_map
*map
)
2660 isl_ctx_deref(map
->ctx
);
2661 for (i
= 0; i
< map
->n
; ++i
)
2662 isl_basic_map_free(map
->p
[i
]);
2663 isl_dim_free(map
->dim
);
2667 struct isl_map
*isl_map_extend(struct isl_map
*base
,
2668 unsigned nparam
, unsigned n_in
, unsigned n_out
)
2672 base
= isl_map_cow(base
);
2676 base
->dim
= isl_dim_extend(base
->dim
, nparam
, n_in
, n_out
);
2679 for (i
= 0; i
< base
->n
; ++i
) {
2680 base
->p
[i
] = isl_basic_map_extend_dim(base
->p
[i
],
2681 isl_dim_copy(base
->dim
), 0, 0, 0);
2691 struct isl_set
*isl_set_extend(struct isl_set
*base
,
2692 unsigned nparam
, unsigned dim
)
2694 return (struct isl_set
*)isl_map_extend((struct isl_map
*)base
,
2698 static struct isl_basic_map
*isl_basic_map_fix_pos(struct isl_basic_map
*bmap
,
2699 unsigned pos
, int value
)
2703 bmap
= isl_basic_map_cow(bmap
);
2704 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
2705 j
= isl_basic_map_alloc_equality(bmap
);
2708 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
2709 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
2710 isl_int_set_si(bmap
->eq
[j
][0], value
);
2711 bmap
= isl_basic_map_simplify(bmap
);
2712 return isl_basic_map_finalize(bmap
);
2714 isl_basic_map_free(bmap
);
2718 struct isl_basic_map
*isl_basic_map_fix_si(struct isl_basic_map
*bmap
,
2719 enum isl_dim_type type
, unsigned pos
, int value
)
2723 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
2724 return isl_basic_map_fix_pos(bmap
, isl_basic_map_offset(bmap
, type
) + pos
,
2727 isl_basic_map_free(bmap
);
2731 struct isl_basic_set
*isl_basic_set_fix_si(struct isl_basic_set
*bset
,
2732 enum isl_dim_type type
, unsigned pos
, int value
)
2734 return (struct isl_basic_set
*)
2735 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
2739 struct isl_basic_map
*isl_basic_map_fix_input_si(struct isl_basic_map
*bmap
,
2740 unsigned input
, int value
)
2742 return isl_basic_map_fix_si(bmap
, isl_dim_in
, input
, value
);
2745 struct isl_basic_set
*isl_basic_set_fix_dim_si(struct isl_basic_set
*bset
,
2746 unsigned dim
, int value
)
2748 return (struct isl_basic_set
*)
2749 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
2750 isl_dim_set
, dim
, value
);
2753 struct isl_map
*isl_map_fix_si(struct isl_map
*map
,
2754 enum isl_dim_type type
, unsigned pos
, int value
)
2758 map
= isl_map_cow(map
);
2762 isl_assert(ctx
, pos
< isl_map_dim(map
, type
), goto error
);
2763 for (i
= 0; i
< map
->n
; ++i
) {
2764 map
->p
[i
] = isl_basic_map_fix_si(map
->p
[i
], type
, pos
, value
);
2768 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
2775 struct isl_map
*isl_map_fix_input_si(struct isl_map
*map
,
2776 unsigned input
, int value
)
2778 return isl_map_fix_si(map
, isl_dim_in
, input
, value
);
2781 struct isl_set
*isl_set_fix_dim_si(struct isl_set
*set
, unsigned dim
, int value
)
2783 return (struct isl_set
*)
2784 isl_map_fix_si((struct isl_map
*)set
, isl_dim_set
, dim
, value
);
2787 struct isl_basic_set
*isl_basic_set_lower_bound_dim(struct isl_basic_set
*bset
,
2788 unsigned dim
, isl_int value
)
2793 bset
= isl_basic_set_cow(bset
);
2794 bset
= isl_basic_set_extend_constraints(bset
, 0, 1);
2795 j
= isl_basic_set_alloc_inequality(bset
);
2798 isl_seq_clr(bset
->ineq
[j
], 1 + isl_basic_set_total_dim(bset
));
2799 isl_int_set_si(bset
->ineq
[j
][1 + isl_basic_set_n_param(bset
) + dim
], 1);
2800 isl_int_neg(bset
->ineq
[j
][0], value
);
2801 bset
= isl_basic_set_simplify(bset
);
2802 return isl_basic_set_finalize(bset
);
2804 isl_basic_set_free(bset
);
2808 struct isl_set
*isl_set_lower_bound_dim(struct isl_set
*set
, unsigned dim
,
2813 set
= isl_set_cow(set
);
2817 isl_assert(set
->ctx
, dim
< isl_set_n_dim(set
), goto error
);
2818 for (i
= 0; i
< set
->n
; ++i
) {
2819 set
->p
[i
] = isl_basic_set_lower_bound_dim(set
->p
[i
], dim
, value
);
2829 struct isl_map
*isl_map_reverse(struct isl_map
*map
)
2834 map
= isl_map_cow(map
);
2838 map
->dim
= isl_dim_reverse(map
->dim
);
2841 for (i
= 0; i
< map
->n
; ++i
) {
2842 map
->p
[i
] = isl_basic_map_reverse(map
->p
[i
]);
2846 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
2853 struct isl_map
*isl_basic_map_lexmax(
2854 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
2855 struct isl_set
**empty
)
2857 return isl_pip_basic_map_lexmax(bmap
, dom
, empty
);
2860 struct isl_map
*isl_basic_map_lexmin(
2861 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
2862 struct isl_set
**empty
)
2864 return isl_pip_basic_map_lexmin(bmap
, dom
, empty
);
2867 struct isl_set
*isl_basic_set_lexmin(struct isl_basic_set
*bset
)
2869 struct isl_basic_map
*bmap
= NULL
;
2870 struct isl_basic_set
*dom
= NULL
;
2871 struct isl_map
*min
;
2872 struct isl_dim
*param_dim
;
2876 bmap
= isl_basic_map_from_basic_set(bset
, isl_dim_copy(bset
->dim
));
2879 param_dim
= isl_dim_domain(isl_dim_copy(bmap
->dim
));
2880 dom
= isl_basic_set_universe(param_dim
);
2883 min
= isl_basic_map_lexmin(bmap
, dom
, NULL
);
2884 return isl_map_range(min
);
2886 isl_basic_map_free(bmap
);
2890 struct isl_map
*isl_basic_map_compute_divs(struct isl_basic_map
*bmap
)
2897 off
= isl_dim_total(bmap
->dim
);
2898 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2899 if (isl_int_is_zero(bmap
->div
[i
][0]))
2900 return isl_pip_basic_map_compute_divs(bmap
);
2901 isl_assert(bmap
->ctx
, isl_int_is_zero(bmap
->div
[i
][1+1+off
+i
]),
2904 return isl_map_from_basic_map(bmap
);
2906 isl_basic_map_free(bmap
);
2910 struct isl_map
*isl_map_compute_divs(struct isl_map
*map
)
2913 struct isl_map
*res
;
2919 res
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[0]));
2920 for (i
= 1 ; i
< map
->n
; ++i
) {
2922 r2
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[i
]));
2923 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
2924 res
= isl_map_union_disjoint(res
, r2
);
2926 res
= isl_map_union(res
, r2
);
2933 struct isl_set
*isl_basic_set_compute_divs(struct isl_basic_set
*bset
)
2935 return (struct isl_set
*)
2936 isl_basic_map_compute_divs((struct isl_basic_map
*)bset
);
2939 struct isl_set
*isl_set_compute_divs(struct isl_set
*set
)
2941 return (struct isl_set
*)
2942 isl_map_compute_divs((struct isl_map
*)set
);
2945 struct isl_set
*isl_map_domain(struct isl_map
*map
)
2948 struct isl_set
*set
;
2953 map
= isl_map_cow(map
);
2957 set
= (struct isl_set
*)map
;
2958 set
->dim
= isl_dim_domain(set
->dim
);
2961 for (i
= 0; i
< map
->n
; ++i
) {
2962 set
->p
[i
] = isl_basic_map_domain(map
->p
[i
]);
2966 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
2967 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
2974 struct isl_map
*isl_map_union_disjoint(
2975 struct isl_map
*map1
, struct isl_map
*map2
)
2979 struct isl_map
*map
= NULL
;
2993 isl_assert(map1
->ctx
, isl_dim_equal(map1
->dim
, map2
->dim
), goto error
);
2995 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
2996 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
2997 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
2999 map
= isl_map_alloc_dim(isl_dim_copy(map1
->dim
),
3000 map1
->n
+ map2
->n
, flags
);
3003 for (i
= 0; i
< map1
->n
; ++i
) {
3004 map
= isl_map_add(map
,
3005 isl_basic_map_copy(map1
->p
[i
]));
3009 for (i
= 0; i
< map2
->n
; ++i
) {
3010 map
= isl_map_add(map
,
3011 isl_basic_map_copy(map2
->p
[i
]));
3025 struct isl_map
*isl_map_union(struct isl_map
*map1
, struct isl_map
*map2
)
3027 map1
= isl_map_union_disjoint(map1
, map2
);
3031 ISL_F_CLR(map1
, ISL_MAP_DISJOINT
);
3035 struct isl_set
*isl_set_union_disjoint(
3036 struct isl_set
*set1
, struct isl_set
*set2
)
3038 return (struct isl_set
*)
3039 isl_map_union_disjoint(
3040 (struct isl_map
*)set1
, (struct isl_map
*)set2
);
3043 struct isl_set
*isl_set_union(struct isl_set
*set1
, struct isl_set
*set2
)
3045 return (struct isl_set
*)
3046 isl_map_union((struct isl_map
*)set1
, (struct isl_map
*)set2
);
3049 struct isl_map
*isl_map_intersect_range(
3050 struct isl_map
*map
, struct isl_set
*set
)
3053 struct isl_map
*result
;
3059 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
) &&
3060 ISL_F_ISSET(set
, ISL_MAP_DISJOINT
))
3061 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
3063 result
= isl_map_alloc_dim(isl_dim_copy(map
->dim
),
3064 map
->n
* set
->n
, flags
);
3067 for (i
= 0; i
< map
->n
; ++i
)
3068 for (j
= 0; j
< set
->n
; ++j
) {
3069 result
= isl_map_add(result
,
3070 isl_basic_map_intersect_range(
3071 isl_basic_map_copy(map
->p
[i
]),
3072 isl_basic_set_copy(set
->p
[j
])));
3085 struct isl_map
*isl_map_intersect_domain(
3086 struct isl_map
*map
, struct isl_set
*set
)
3088 return isl_map_reverse(
3089 isl_map_intersect_range(isl_map_reverse(map
), set
));
3092 struct isl_map
*isl_map_apply_domain(
3093 struct isl_map
*map1
, struct isl_map
*map2
)
3097 map1
= isl_map_reverse(map1
);
3098 map1
= isl_map_apply_range(map1
, map2
);
3099 return isl_map_reverse(map1
);
3106 struct isl_map
*isl_map_apply_range(
3107 struct isl_map
*map1
, struct isl_map
*map2
)
3109 struct isl_dim
*dim_result
;
3110 struct isl_map
*result
;
3119 dim_result
= isl_dim_join(isl_dim_copy(map1
->dim
),
3120 isl_dim_copy(map2
->dim
));
3122 result
= isl_map_alloc_dim(dim_result
, map1
->n
* map2
->n
, 0);
3125 for (i
= 0; i
< map1
->n
; ++i
)
3126 for (j
= 0; j
< map2
->n
; ++j
) {
3127 result
= isl_map_add(result
,
3128 isl_basic_map_apply_range(
3129 isl_basic_map_copy(map1
->p
[i
]),
3130 isl_basic_map_copy(map2
->p
[j
])));
3136 if (result
&& result
->n
<= 1)
3137 ISL_F_SET(result
, ISL_MAP_DISJOINT
);
3146 * returns range - domain
3148 struct isl_basic_set
*isl_basic_map_deltas(struct isl_basic_map
*bmap
)
3150 struct isl_basic_set
*bset
;
3157 dim
= isl_basic_map_n_in(bmap
);
3158 nparam
= isl_basic_map_n_param(bmap
);
3159 isl_assert(bmap
->ctx
, dim
== isl_basic_map_n_out(bmap
), goto error
);
3160 bset
= isl_basic_set_from_basic_map(bmap
);
3161 bset
= isl_basic_set_extend(bset
, nparam
, 3*dim
, 0, dim
, 0);
3162 bset
= isl_basic_set_swap_vars(bset
, 2*dim
);
3163 for (i
= 0; i
< dim
; ++i
) {
3164 int j
= isl_basic_map_alloc_equality(
3165 (struct isl_basic_map
*)bset
);
3168 isl_seq_clr(bset
->eq
[j
], 1 + isl_basic_set_total_dim(bset
));
3169 isl_int_set_si(bset
->eq
[j
][1+nparam
+i
], 1);
3170 isl_int_set_si(bset
->eq
[j
][1+nparam
+dim
+i
], 1);
3171 isl_int_set_si(bset
->eq
[j
][1+nparam
+2*dim
+i
], -1);
3173 return isl_basic_set_project_out(bset
, 2*dim
, 0);
3175 isl_basic_map_free(bmap
);
3180 * returns range - domain
3182 struct isl_set
*isl_map_deltas(struct isl_map
*map
)
3185 struct isl_set
*result
;
3190 isl_assert(map
->ctx
, isl_map_n_in(map
) == isl_map_n_out(map
), goto error
);
3191 result
= isl_set_alloc(map
->ctx
, isl_map_n_param(map
),
3192 isl_map_n_in(map
), map
->n
, map
->flags
);
3195 for (i
= 0; i
< map
->n
; ++i
)
3196 result
= isl_set_add(result
,
3197 isl_basic_map_deltas(isl_basic_map_copy(map
->p
[i
])));
3205 static struct isl_basic_map
*basic_map_identity(struct isl_dim
*dims
)
3207 struct isl_basic_map
*bmap
;
3215 nparam
= dims
->nparam
;
3217 bmap
= isl_basic_map_alloc_dim(dims
, 0, dim
, 0);
3221 for (i
= 0; i
< dim
; ++i
) {
3222 int j
= isl_basic_map_alloc_equality(bmap
);
3225 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
3226 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
3227 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], -1);
3229 return isl_basic_map_finalize(bmap
);
3231 isl_basic_map_free(bmap
);
3235 struct isl_basic_map
*isl_basic_map_identity(struct isl_dim
*set_dim
)
3237 struct isl_dim
*dim
= isl_dim_map(set_dim
);
3240 return basic_map_identity(dim
);
3243 struct isl_basic_map
*isl_basic_map_identity_like(struct isl_basic_map
*model
)
3245 if (!model
|| !model
->dim
)
3247 isl_assert(model
->ctx
,
3248 model
->dim
->n_in
== model
->dim
->n_out
, return NULL
);
3249 return basic_map_identity(isl_dim_copy(model
->dim
));
3252 static struct isl_map
*map_identity(struct isl_dim
*dim
)
3254 struct isl_map
*map
= isl_map_alloc_dim(dim
, 1, ISL_MAP_DISJOINT
);
3255 return isl_map_add(map
, basic_map_identity(isl_dim_copy(dim
)));
3258 struct isl_map
*isl_map_identity(struct isl_dim
*set_dim
)
3260 struct isl_dim
*dim
= isl_dim_map(set_dim
);
3263 return map_identity(dim
);
3266 struct isl_map
*isl_map_identity_like(struct isl_basic_map
*model
)
3268 if (!model
|| !model
->dim
)
3270 isl_assert(model
->ctx
,
3271 model
->dim
->n_in
== model
->dim
->n_out
, return NULL
);
3272 return map_identity(isl_dim_copy(model
->dim
));
3275 int isl_set_is_equal(struct isl_set
*set1
, struct isl_set
*set2
)
3277 return isl_map_is_equal((struct isl_map
*)set1
, (struct isl_map
*)set2
);
3280 int isl_set_is_subset(struct isl_set
*set1
, struct isl_set
*set2
)
3282 return isl_map_is_subset(
3283 (struct isl_map
*)set1
, (struct isl_map
*)set2
);
3286 int isl_basic_map_is_subset(
3287 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3290 struct isl_map
*map1
;
3291 struct isl_map
*map2
;
3293 if (!bmap1
|| !bmap2
)
3296 map1
= isl_map_from_basic_map(isl_basic_map_copy(bmap1
));
3297 map2
= isl_map_from_basic_map(isl_basic_map_copy(bmap2
));
3299 is_subset
= isl_map_is_subset(map1
, map2
);
3307 int isl_basic_map_is_equal(
3308 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3312 if (!bmap1
|| !bmap2
)
3314 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
3317 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
3321 int isl_basic_set_is_equal(
3322 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
3324 return isl_basic_map_is_equal(
3325 (struct isl_basic_map
*)bset1
, (struct isl_basic_map
*)bset2
);
3328 int isl_map_is_empty(struct isl_map
*map
)
3335 for (i
= 0; i
< map
->n
; ++i
) {
3336 is_empty
= isl_basic_map_is_empty(map
->p
[i
]);
3345 int isl_map_fast_is_empty(struct isl_map
*map
)
3350 int isl_set_is_empty(struct isl_set
*set
)
3352 return isl_map_is_empty((struct isl_map
*)set
);
3355 int isl_map_is_subset(struct isl_map
*map1
, struct isl_map
*map2
)
3359 struct isl_map
*diff
;
3364 if (isl_map_is_empty(map1
))
3367 if (isl_map_is_empty(map2
))
3370 diff
= isl_map_subtract(isl_map_copy(map1
), isl_map_copy(map2
));
3374 is_subset
= isl_map_is_empty(diff
);
3380 int isl_map_is_equal(struct isl_map
*map1
, struct isl_map
*map2
)
3386 is_subset
= isl_map_is_subset(map1
, map2
);
3389 is_subset
= isl_map_is_subset(map2
, map1
);
3393 int isl_basic_map_is_strict_subset(
3394 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3398 if (!bmap1
|| !bmap2
)
3400 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
3403 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
3404 if (is_subset
== -1)
3409 int isl_basic_map_is_universe(struct isl_basic_map
*bmap
)
3413 return bmap
->n_eq
== 0 && bmap
->n_ineq
== 0;
3416 int isl_basic_map_is_empty(struct isl_basic_map
*bmap
)
3418 struct isl_basic_set
*bset
= NULL
;
3419 struct isl_vec
*sample
= NULL
;
3426 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
3429 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
3430 struct isl_basic_map
*copy
= isl_basic_map_copy(bmap
);
3431 copy
= isl_basic_map_convex_hull(copy
);
3432 empty
= ISL_F_ISSET(copy
, ISL_BASIC_MAP_EMPTY
);
3433 isl_basic_map_free(copy
);
3437 total
= 1 + isl_basic_map_total_dim(bmap
);
3438 if (bmap
->sample
&& bmap
->sample
->size
== total
) {
3439 int contains
= basic_map_contains(bmap
, bmap
->sample
);
3445 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
3448 sample
= isl_basic_set_sample(bset
);
3451 empty
= sample
->size
== 0;
3453 isl_vec_free(bmap
->ctx
, bmap
->sample
);
3454 bmap
->sample
= sample
;
3459 int isl_basic_set_is_empty(struct isl_basic_set
*bset
)
3461 return isl_basic_map_is_empty((struct isl_basic_map
*)bset
);
3464 struct isl_map
*isl_basic_map_union(
3465 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3467 struct isl_map
*map
;
3468 if (!bmap1
|| !bmap2
)
3471 isl_assert(map1
->ctx
, isl_dim_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
3473 map
= isl_map_alloc_dim(isl_dim_copy(bmap1
->dim
), 2, 0);
3476 map
= isl_map_add(map
, bmap1
);
3477 map
= isl_map_add(map
, bmap2
);
3480 isl_basic_map_free(bmap1
);
3481 isl_basic_map_free(bmap2
);
3485 struct isl_set
*isl_basic_set_union(
3486 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
3488 return (struct isl_set
*)isl_basic_map_union(
3489 (struct isl_basic_map
*)bset1
,
3490 (struct isl_basic_map
*)bset2
);
3493 /* Order divs such that any div only depends on previous divs */
3494 static struct isl_basic_map
*order_divs(struct isl_basic_map
*bmap
)
3497 unsigned off
= isl_dim_total(bmap
->dim
);
3499 for (i
= 0; i
< bmap
->n_div
; ++i
) {
3501 pos
= isl_seq_first_non_zero(bmap
->div
[i
]+1+1+off
+i
,
3505 swap_div(bmap
, i
, pos
);
3511 /* Look for a div in dst that corresponds to the div "div" in src.
3512 * The divs before "div" in src and dst are assumed to be the same.
3514 * Returns -1 if no corresponding div was found and the position
3515 * of the corresponding div in dst otherwise.
3517 static int find_div(struct isl_basic_map
*dst
,
3518 struct isl_basic_map
*src
, unsigned div
)
3522 unsigned total
= isl_dim_total(src
->dim
);
3524 isl_assert(dst
->ctx
, div
<= dst
->n_div
, return -1);
3525 for (i
= div
; i
< dst
->n_div
; ++i
)
3526 if (isl_seq_eq(dst
->div
[i
], src
->div
[div
], 1+1+total
+div
) &&
3527 isl_seq_first_non_zero(dst
->div
[i
]+1+1+total
+div
,
3528 dst
->n_div
- div
) == -1)
3533 struct isl_basic_map
*isl_basic_map_align_divs(
3534 struct isl_basic_map
*dst
, struct isl_basic_map
*src
)
3537 unsigned total
= isl_dim_total(src
->dim
);
3542 if (src
->n_div
== 0)
3545 for (i
= 0; i
< src
->n_div
; ++i
)
3546 isl_assert(src
->ctx
, !isl_int_is_zero(src
->div
[i
][0]), goto error
);
3548 src
= order_divs(src
);
3549 dst
= isl_basic_map_extend_dim(dst
, isl_dim_copy(dst
->dim
),
3550 src
->n_div
, 0, 2 * src
->n_div
);
3553 for (i
= 0; i
< src
->n_div
; ++i
) {
3554 int j
= find_div(dst
, src
, i
);
3556 j
= isl_basic_map_alloc_div(dst
);
3559 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
+i
);
3560 isl_seq_clr(dst
->div
[j
]+1+1+total
+i
, dst
->n_div
- i
);
3561 if (add_div_constraints(dst
, j
) < 0)
3565 swap_div(dst
, i
, j
);
3569 isl_basic_map_free(dst
);
3573 struct isl_basic_set
*isl_basic_set_align_divs(
3574 struct isl_basic_set
*dst
, struct isl_basic_set
*src
)
3576 return (struct isl_basic_set
*)isl_basic_map_align_divs(
3577 (struct isl_basic_map
*)dst
, (struct isl_basic_map
*)src
);
3580 struct isl_map
*isl_map_align_divs(struct isl_map
*map
)
3584 map
= isl_map_compute_divs(map
);
3585 map
= isl_map_cow(map
);
3589 for (i
= 1; i
< map
->n
; ++i
)
3590 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], map
->p
[i
]);
3591 for (i
= 1; i
< map
->n
; ++i
)
3592 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], map
->p
[0]);
3594 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3598 static struct isl_map
*add_cut_constraint(struct isl_map
*dst
,
3599 struct isl_basic_map
*src
, isl_int
*c
,
3600 unsigned len
, int oppose
)
3602 struct isl_basic_map
*copy
= NULL
;
3607 copy
= isl_basic_map_copy(src
);
3608 copy
= isl_basic_map_cow(copy
);
3611 copy
= isl_basic_map_extend_constraints(copy
, 0, 1);
3612 k
= isl_basic_map_alloc_inequality(copy
);
3616 isl_seq_neg(copy
->ineq
[k
], c
, len
);
3618 isl_seq_cpy(copy
->ineq
[k
], c
, len
);
3619 total
= 1 + isl_basic_map_total_dim(copy
);
3620 isl_seq_clr(copy
->ineq
[k
]+len
, total
- len
);
3621 isl_inequality_negate(copy
, k
);
3622 copy
= isl_basic_map_simplify(copy
);
3623 copy
= isl_basic_map_finalize(copy
);
3624 is_empty
= isl_basic_map_is_empty(copy
);
3628 dst
= isl_map_add(dst
, copy
);
3630 isl_basic_map_free(copy
);
3633 isl_basic_map_free(copy
);
3638 static struct isl_map
*subtract(struct isl_map
*map
, struct isl_basic_map
*bmap
)
3642 struct isl_map
*rest
= NULL
;
3644 unsigned total
= isl_basic_map_total_dim(bmap
);
3651 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
3652 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
3654 max
= map
->n
* (2 * bmap
->n_eq
+ bmap
->n_ineq
);
3655 rest
= isl_map_alloc_dim(isl_dim_copy(map
->dim
), max
, flags
);
3659 for (i
= 0; i
< map
->n
; ++i
) {
3660 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], bmap
);
3665 for (j
= 0; j
< map
->n
; ++j
)
3666 map
->p
[j
] = isl_basic_map_cow(map
->p
[j
]);
3668 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
3669 for (j
= 0; j
< map
->n
; ++j
) {
3670 rest
= add_cut_constraint(rest
,
3671 map
->p
[j
], bmap
->eq
[i
], 1+total
, 0);
3675 rest
= add_cut_constraint(rest
,
3676 map
->p
[j
], bmap
->eq
[i
], 1+total
, 1);
3680 map
->p
[j
] = isl_basic_map_extend_constraints(map
->p
[j
],
3684 k
= isl_basic_map_alloc_equality(map
->p
[j
]);
3687 isl_seq_cpy(map
->p
[j
]->eq
[k
], bmap
->eq
[i
], 1+total
);
3688 isl_seq_clr(map
->p
[j
]->eq
[k
]+1+total
,
3689 map
->p
[j
]->n_div
- bmap
->n_div
);
3693 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
3694 for (j
= 0; j
< map
->n
; ++j
) {
3695 rest
= add_cut_constraint(rest
,
3696 map
->p
[j
], bmap
->ineq
[i
], 1+total
, 0);
3700 map
->p
[j
] = isl_basic_map_extend_constraints(map
->p
[j
],
3704 k
= isl_basic_map_alloc_inequality(map
->p
[j
]);
3707 isl_seq_cpy(map
->p
[j
]->ineq
[k
], bmap
->ineq
[i
], 1+total
);
3708 isl_seq_clr(map
->p
[j
]->ineq
[k
]+1+total
,
3709 map
->p
[j
]->n_div
- bmap
->n_div
);
3721 struct isl_map
*isl_map_subtract(struct isl_map
*map1
, struct isl_map
*map2
)
3727 isl_assert(map1
->ctx
, isl_dim_equal(map1
->dim
, map2
->dim
), goto error
);
3729 if (isl_map_is_empty(map2
)) {
3734 map1
= isl_map_compute_divs(map1
);
3735 map2
= isl_map_compute_divs(map2
);
3739 for (i
= 0; map1
&& i
< map2
->n
; ++i
)
3740 map1
= subtract(map1
, map2
->p
[i
]);
3750 struct isl_set
*isl_set_subtract(struct isl_set
*set1
, struct isl_set
*set2
)
3752 return (struct isl_set
*)
3754 (struct isl_map
*)set1
, (struct isl_map
*)set2
);
3757 struct isl_set
*isl_set_apply(struct isl_set
*set
, struct isl_map
*map
)
3761 isl_assert(set
->ctx
, isl_map_compatible_domain(map
, set
), goto error
);
3762 map
= isl_map_intersect_domain(map
, set
);
3763 set
= isl_map_range(map
);
3771 /* There is no need to cow as removing empty parts doesn't change
3772 * the meaning of the set.
3774 struct isl_map
*isl_map_remove_empty_parts(struct isl_map
*map
)
3781 for (i
= map
->n
-1; i
>= 0; --i
) {
3782 if (!ISL_F_ISSET(map
->p
[i
], ISL_BASIC_MAP_EMPTY
))
3784 isl_basic_map_free(map
->p
[i
]);
3785 if (i
!= map
->n
-1) {
3786 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3787 map
->p
[i
] = map
->p
[map
->n
-1];
3795 struct isl_set
*isl_set_remove_empty_parts(struct isl_set
*set
)
3797 return (struct isl_set
*)
3798 isl_map_remove_empty_parts((struct isl_map
*)set
);
3801 struct isl_basic_map
*isl_map_copy_basic_map(struct isl_map
*map
)
3803 struct isl_basic_map
*bmap
;
3804 if (!map
|| map
->n
== 0)
3806 bmap
= map
->p
[map
->n
-1];
3807 isl_assert(map
->ctx
, ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
), return NULL
);
3808 return isl_basic_map_copy(bmap
);
3811 struct isl_basic_set
*isl_set_copy_basic_set(struct isl_set
*set
)
3813 (struct isl_basic_set
*)isl_map_copy_basic_map((struct isl_map
*)set
);
3816 struct isl_map
*isl_map_drop_basic_map(struct isl_map
*map
,
3817 struct isl_basic_map
*bmap
)
3823 for (i
= map
->n
-1; i
>= 0; --i
) {
3824 if (map
->p
[i
] != bmap
)
3826 map
= isl_map_cow(map
);
3829 isl_basic_map_free(map
->p
[i
]);
3830 if (i
!= map
->n
-1) {
3831 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
3832 map
->p
[i
] = map
->p
[map
->n
-1];
3837 isl_basic_map_free(bmap
);
3841 isl_basic_map_free(bmap
);
3845 struct isl_set
*isl_set_drop_basic_set(struct isl_set
*set
,
3846 struct isl_basic_set
*bset
)
3848 (struct isl_set
*)isl_map_drop_basic_map((struct isl_map
*)set
,
3849 (struct isl_basic_map
*)bset
);
3852 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
3853 * for any common value of the parameters and dimensions preceding dim
3854 * in both basic sets, the values of dimension pos in bset1 are
3855 * smaller or larger than those in bset2.
3858 * 1 if bset1 follows bset2
3859 * -1 if bset1 precedes bset2
3860 * 0 if bset1 and bset2 are incomparable
3861 * -2 if some error occurred.
3863 int isl_basic_set_compare_at(struct isl_basic_set
*bset1
,
3864 struct isl_basic_set
*bset2
, int pos
)
3866 struct isl_dim
*dims
;
3867 struct isl_basic_map
*bmap1
= NULL
;
3868 struct isl_basic_map
*bmap2
= NULL
;
3869 struct isl_ctx
*ctx
;
3870 struct isl_vec
*obj
;
3873 unsigned dim1
, dim2
;
3875 enum isl_lp_result res
;
3878 if (!bset1
|| !bset2
)
3881 nparam
= isl_basic_set_n_param(bset1
);
3882 dim1
= isl_basic_set_n_dim(bset1
);
3883 dim2
= isl_basic_set_n_dim(bset2
);
3884 dims
= isl_dim_alloc(bset1
->ctx
, nparam
, pos
, dim1
- pos
);
3885 bmap1
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset1
), dims
);
3886 dims
= isl_dim_alloc(bset2
->ctx
, nparam
, pos
, dim2
- pos
);
3887 bmap2
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset2
), dims
);
3888 if (!bmap1
|| !bmap2
)
3890 bmap1
= isl_basic_map_extend(bmap1
, nparam
,
3891 pos
, (dim1
- pos
) + (dim2
- pos
),
3892 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
3893 bmap1
= add_constraints(bmap1
, bmap2
, 0, dim1
- pos
);
3896 total
= isl_basic_map_total_dim(bmap1
);
3898 obj
= isl_vec_alloc(ctx
, total
);
3899 isl_seq_clr(obj
->block
.data
, total
);
3900 isl_int_set_si(obj
->block
.data
[nparam
+pos
], 1);
3901 isl_int_set_si(obj
->block
.data
[nparam
+pos
+(dim1
-pos
)], -1);
3906 res
= isl_solve_lp(bmap1
, 0, obj
->block
.data
, ctx
->one
, &num
, &den
);
3907 if (res
== isl_lp_empty
)
3909 else if (res
== isl_lp_ok
&& isl_int_is_pos(num
))
3911 else if ((res
== isl_lp_ok
&& isl_int_is_neg(num
)) ||
3912 res
== isl_lp_unbounded
)
3918 isl_basic_map_free(bmap1
);
3919 isl_vec_free(ctx
, obj
);
3922 isl_basic_map_free(bmap1
);
3923 isl_basic_map_free(bmap2
);
3927 static int isl_basic_map_fast_has_fixed_var(struct isl_basic_map
*bmap
,
3928 unsigned pos
, isl_int
*val
)
3936 total
= isl_basic_map_total_dim(bmap
);
3937 for (i
= 0, d
= total
-1; i
< bmap
->n_eq
&& d
+1 > pos
; ++i
) {
3938 for (; d
+1 > pos
; --d
)
3939 if (!isl_int_is_zero(bmap
->eq
[i
][1+d
]))
3943 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1, d
) != -1)
3945 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1+d
+1, total
-d
-1) != -1)
3947 if (!isl_int_is_one(bmap
->eq
[i
][1+d
]))
3950 isl_int_neg(*val
, bmap
->eq
[i
][0]);
3956 static int isl_map_fast_has_fixed_var(struct isl_map
*map
,
3957 unsigned pos
, isl_int
*val
)
3969 return isl_basic_map_fast_has_fixed_var(map
->p
[0], pos
, val
);
3972 fixed
= isl_basic_map_fast_has_fixed_var(map
->p
[0], pos
, &v
);
3973 for (i
= 1; fixed
== 1 && i
< map
->n
; ++i
) {
3974 fixed
= isl_basic_map_fast_has_fixed_var(map
->p
[i
], pos
, &tmp
);
3975 if (fixed
== 1 && isl_int_ne(tmp
, v
))
3979 isl_int_set(*val
, v
);
3985 static int isl_set_fast_has_fixed_var(struct isl_set
*set
, unsigned pos
,
3988 return isl_map_fast_has_fixed_var((struct isl_map
*)set
, pos
, val
);
3991 int isl_basic_map_fast_is_fixed(struct isl_basic_map
*bmap
,
3992 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
3994 if (pos
>= isl_basic_map_dim(bmap
, type
))
3996 return isl_basic_map_fast_has_fixed_var(bmap
,
3997 isl_basic_map_offset(bmap
, type
) - 1 + pos
, val
);
4000 /* Check if dimension dim has fixed value and if so and if val is not NULL,
4001 * then return this fixed value in *val.
4003 int isl_set_fast_dim_is_fixed(struct isl_set
*set
, unsigned dim
, isl_int
*val
)
4005 return isl_set_fast_has_fixed_var(set
, isl_set_n_param(set
) + dim
, val
);
4008 /* Check if input variable in has fixed value and if so and if val is not NULL,
4009 * then return this fixed value in *val.
4011 int isl_map_fast_input_is_fixed(struct isl_map
*map
, unsigned in
, isl_int
*val
)
4013 return isl_map_fast_has_fixed_var(map
, isl_map_n_param(map
) + in
, val
);
4016 /* Check if dimension dim has an (obvious) fixed lower bound and if so
4017 * and if val is not NULL, then return this lower bound in *val.
4019 int isl_basic_set_fast_dim_has_fixed_lower_bound(struct isl_basic_set
*bset
,
4020 unsigned dim
, isl_int
*val
)
4022 int i
, i_eq
= -1, i_ineq
= -1;
4029 total
= isl_basic_set_total_dim(bset
);
4030 nparam
= isl_basic_set_n_param(bset
);
4031 for (i
= 0; i
< bset
->n_eq
; ++i
) {
4032 if (isl_int_is_zero(bset
->eq
[i
][1+nparam
+dim
]))
4038 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
4039 if (!isl_int_is_pos(bset
->ineq
[i
][1+nparam
+dim
]))
4041 if (i_eq
!= -1 || i_ineq
!= -1)
4045 if (i_eq
== -1 && i_ineq
== -1)
4047 c
= i_eq
!= -1 ? bset
->eq
[i_eq
] : bset
->ineq
[i_ineq
];
4048 /* The coefficient should always be one due to normalization. */
4049 if (!isl_int_is_one(c
[1+nparam
+dim
]))
4051 if (isl_seq_first_non_zero(c
+1, nparam
+dim
) != -1)
4053 if (isl_seq_first_non_zero(c
+1+nparam
+dim
+1,
4054 total
- nparam
- dim
- 1) != -1)
4057 isl_int_neg(*val
, c
[0]);
4061 int isl_set_fast_dim_has_fixed_lower_bound(struct isl_set
*set
,
4062 unsigned dim
, isl_int
*val
)
4074 return isl_basic_set_fast_dim_has_fixed_lower_bound(set
->p
[0],
4078 fixed
= isl_basic_set_fast_dim_has_fixed_lower_bound(set
->p
[0],
4080 for (i
= 1; fixed
== 1 && i
< set
->n
; ++i
) {
4081 fixed
= isl_basic_set_fast_dim_has_fixed_lower_bound(set
->p
[i
],
4083 if (fixed
== 1 && isl_int_ne(tmp
, v
))
4087 isl_int_set(*val
, v
);
4098 static int qsort_constraint_cmp(const void *p1
, const void *p2
)
4100 const struct constraint
*c1
= (const struct constraint
*)p1
;
4101 const struct constraint
*c2
= (const struct constraint
*)p2
;
4102 unsigned size
= isl_min(c1
->size
, c2
->size
);
4103 return isl_seq_cmp(c1
->c
, c2
->c
, size
);
4106 static struct isl_basic_map
*isl_basic_map_sort_constraints(
4107 struct isl_basic_map
*bmap
)
4110 struct constraint
*c
;
4115 total
= isl_basic_map_total_dim(bmap
);
4116 c
= isl_alloc_array(bmap
->ctx
, struct constraint
, bmap
->n_ineq
);
4119 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
4121 c
[i
].c
= bmap
->ineq
[i
];
4123 qsort(c
, bmap
->n_ineq
, sizeof(struct constraint
), qsort_constraint_cmp
);
4124 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
4125 bmap
->ineq
[i
] = c
[i
].c
;
4129 isl_basic_map_free(bmap
);
4133 struct isl_basic_map
*isl_basic_map_normalize(struct isl_basic_map
*bmap
)
4137 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED
))
4139 bmap
= isl_basic_map_convex_hull(bmap
);
4140 bmap
= isl_basic_map_sort_constraints(bmap
);
4141 ISL_F_SET(bmap
, ISL_BASIC_MAP_NORMALIZED
);
4145 struct isl_basic_set
*isl_basic_set_normalize(struct isl_basic_set
*bset
)
4147 return (struct isl_basic_set
*)isl_basic_map_normalize(
4148 (struct isl_basic_map
*)bset
);
4151 static int isl_basic_map_fast_cmp(const struct isl_basic_map
*bmap1
,
4152 const struct isl_basic_map
*bmap2
)
4159 if (isl_basic_map_n_param(bmap1
) != isl_basic_map_n_param(bmap2
))
4160 return isl_basic_map_n_param(bmap1
) - isl_basic_map_n_param(bmap2
);
4161 if (isl_basic_map_n_in(bmap1
) != isl_basic_map_n_in(bmap2
))
4162 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
4163 if (isl_basic_map_n_out(bmap1
) != isl_basic_map_n_out(bmap2
))
4164 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
4165 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
) &&
4166 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
4168 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
))
4170 if (ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
4172 if (bmap1
->n_eq
!= bmap2
->n_eq
)
4173 return bmap1
->n_eq
- bmap2
->n_eq
;
4174 if (bmap1
->n_ineq
!= bmap2
->n_ineq
)
4175 return bmap1
->n_ineq
- bmap2
->n_ineq
;
4176 if (bmap1
->n_div
!= bmap2
->n_div
)
4177 return bmap1
->n_div
- bmap2
->n_div
;
4178 total
= isl_basic_map_total_dim(bmap1
);
4179 for (i
= 0; i
< bmap1
->n_eq
; ++i
) {
4180 cmp
= isl_seq_cmp(bmap1
->eq
[i
], bmap2
->eq
[i
], 1+total
);
4184 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
4185 cmp
= isl_seq_cmp(bmap1
->ineq
[i
], bmap2
->ineq
[i
], 1+total
);
4189 for (i
= 0; i
< bmap1
->n_div
; ++i
) {
4190 cmp
= isl_seq_cmp(bmap1
->div
[i
], bmap2
->div
[i
], 1+1+total
);
4197 static int isl_basic_map_fast_is_equal(struct isl_basic_map
*bmap1
,
4198 struct isl_basic_map
*bmap2
)
4200 return isl_basic_map_fast_cmp(bmap1
, bmap2
) == 0;
4203 static int qsort_bmap_cmp(const void *p1
, const void *p2
)
4205 const struct isl_basic_map
*bmap1
= *(const struct isl_basic_map
**)p1
;
4206 const struct isl_basic_map
*bmap2
= *(const struct isl_basic_map
**)p2
;
4208 return isl_basic_map_fast_cmp(bmap1
, bmap2
);
4211 /* We normalize in place, but if anything goes wrong we need
4212 * to return NULL, so we need to make sure we don't change the
4213 * meaning of any possible other copies of map.
4215 struct isl_map
*isl_map_normalize(struct isl_map
*map
)
4218 struct isl_basic_map
*bmap
;
4222 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
))
4224 for (i
= 0; i
< map
->n
; ++i
) {
4225 bmap
= isl_basic_map_normalize(isl_basic_map_copy(map
->p
[i
]));
4228 isl_basic_map_free(map
->p
[i
]);
4231 qsort(map
->p
, map
->n
, sizeof(struct isl_basic_map
*), qsort_bmap_cmp
);
4232 ISL_F_SET(map
, ISL_MAP_NORMALIZED
);
4233 map
= isl_map_remove_empty_parts(map
);
4236 for (i
= map
->n
- 1; i
>= 1; --i
) {
4237 if (!isl_basic_map_fast_is_equal(map
->p
[i
-1], map
->p
[i
]))
4239 isl_basic_map_free(map
->p
[i
-1]);
4240 for (j
= i
; j
< map
->n
; ++j
)
4241 map
->p
[j
-1] = map
->p
[j
];
4251 struct isl_set
*isl_set_normalize(struct isl_set
*set
)
4253 return (struct isl_set
*)isl_map_normalize((struct isl_map
*)set
);
4256 int isl_map_fast_is_equal(struct isl_map
*map1
, struct isl_map
*map2
)
4266 if (!isl_dim_equal(map1
->dim
, map2
->dim
))
4269 map1
= isl_map_copy(map1
);
4270 map2
= isl_map_copy(map2
);
4271 map1
= isl_map_normalize(map1
);
4272 map2
= isl_map_normalize(map2
);
4275 equal
= map1
->n
== map2
->n
;
4276 for (i
= 0; equal
&& i
< map1
->n
; ++i
) {
4277 equal
= isl_basic_map_fast_is_equal(map1
->p
[i
], map2
->p
[i
]);
4290 int isl_set_fast_is_equal(struct isl_set
*set1
, struct isl_set
*set2
)
4292 return isl_map_fast_is_equal((struct isl_map
*)set1
,
4293 (struct isl_map
*)set2
);
4296 /* Return an interval that ranges from min to max (inclusive)
4298 struct isl_basic_set
*isl_basic_set_interval(struct isl_ctx
*ctx
,
4299 isl_int min
, isl_int max
)
4302 struct isl_basic_set
*bset
= NULL
;
4304 bset
= isl_basic_set_alloc(ctx
, 0, 1, 0, 0, 2);
4308 k
= isl_basic_set_alloc_inequality(bset
);
4311 isl_int_set_si(bset
->ineq
[k
][1], 1);
4312 isl_int_neg(bset
->ineq
[k
][0], min
);
4314 k
= isl_basic_set_alloc_inequality(bset
);
4317 isl_int_set_si(bset
->ineq
[k
][1], -1);
4318 isl_int_set(bset
->ineq
[k
][0], max
);
4322 isl_basic_set_free(bset
);
4326 /* Return the Cartesian product of the basic sets in list (in the given order).
4328 struct isl_basic_set
*isl_basic_set_product(struct isl_basic_set_list
*list
)
4336 struct isl_basic_set
*product
= NULL
;
4340 isl_assert(list
->ctx
, list
->n
> 0, goto error
);
4341 isl_assert(list
->ctx
, list
->p
[0], goto error
);
4342 nparam
= isl_basic_set_n_param(list
->p
[0]);
4343 dim
= isl_basic_set_n_dim(list
->p
[0]);
4344 extra
= list
->p
[0]->n_div
;
4345 n_eq
= list
->p
[0]->n_eq
;
4346 n_ineq
= list
->p
[0]->n_ineq
;
4347 for (i
= 1; i
< list
->n
; ++i
) {
4348 isl_assert(list
->ctx
, list
->p
[i
], goto error
);
4349 isl_assert(list
->ctx
,
4350 nparam
== isl_basic_set_n_param(list
->p
[i
]), goto error
);
4351 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
4352 extra
+= list
->p
[i
]->n_div
;
4353 n_eq
+= list
->p
[i
]->n_eq
;
4354 n_ineq
+= list
->p
[i
]->n_ineq
;
4356 product
= isl_basic_set_alloc(list
->ctx
, nparam
, dim
, extra
,
4361 for (i
= 0; i
< list
->n
; ++i
) {
4362 isl_basic_set_add_constraints(product
,
4363 isl_basic_set_copy(list
->p
[i
]), dim
);
4364 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
4366 isl_basic_set_list_free(list
);
4369 isl_basic_set_free(product
);
4370 isl_basic_set_list_free(list
);
4374 struct isl_basic_map
*isl_basic_map_product(
4375 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
4377 struct isl_dim
*dim_result
= NULL
;
4378 struct isl_basic_map
*bmap
;
4379 unsigned in1
, in2
, out1
, out2
, nparam
, total
, pos
;
4380 struct isl_dim_map
*dim_map1
, *dim_map2
;
4382 if (!bmap1
|| !bmap2
)
4385 isl_assert(map1
->ctx
, isl_dim_match(bmap1
->dim
, isl_dim_param
,
4386 bmap2
->dim
, isl_dim_param
), goto error
);
4387 dim_result
= isl_dim_product(isl_dim_copy(bmap1
->dim
),
4388 isl_dim_copy(bmap2
->dim
));
4390 in1
= isl_basic_map_n_in(bmap1
);
4391 in2
= isl_basic_map_n_in(bmap2
);
4392 out1
= isl_basic_map_n_out(bmap1
);
4393 out2
= isl_basic_map_n_out(bmap2
);
4394 nparam
= isl_basic_map_n_param(bmap1
);
4396 total
= nparam
+ in1
+ in2
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
4397 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4398 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4399 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
4400 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
4401 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
4402 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
4403 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
4404 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
4405 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
4406 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
4408 bmap
= isl_basic_map_alloc_dim(dim_result
,
4409 bmap1
->n_div
+ bmap2
->n_div
,
4410 bmap1
->n_eq
+ bmap2
->n_eq
,
4411 bmap1
->n_ineq
+ bmap2
->n_ineq
);
4412 bmap
= add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
4413 bmap
= add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
4414 bmap
= isl_basic_map_simplify(bmap
);
4415 return isl_basic_map_finalize(bmap
);
4417 isl_basic_map_free(bmap1
);
4418 isl_basic_map_free(bmap2
);
4422 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
4424 struct isl_map
*isl_map_product(struct isl_map
*map1
, struct isl_map
*map2
)
4427 struct isl_map
*result
;
4433 isl_assert(map1
->ctx
, isl_dim_match(map1
->dim
, isl_dim_param
,
4434 map2
->dim
, isl_dim_param
), goto error
);
4436 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
4437 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
4438 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
4440 result
= isl_map_alloc_dim(isl_dim_product(isl_dim_copy(map1
->dim
),
4441 isl_dim_copy(map2
->dim
)),
4442 map1
->n
* map2
->n
, flags
);
4445 for (i
= 0; i
< map1
->n
; ++i
)
4446 for (j
= 0; j
< map2
->n
; ++j
) {
4447 struct isl_basic_map
*part
;
4448 part
= isl_basic_map_product(
4449 isl_basic_map_copy(map1
->p
[i
]),
4450 isl_basic_map_copy(map2
->p
[j
]));
4451 if (isl_basic_map_is_empty(part
))
4452 isl_basic_map_free(part
);
4454 result
= isl_map_add(result
, part
);
4467 uint32_t isl_basic_set_get_hash(struct isl_basic_set
*bset
)
4475 bset
= isl_basic_set_copy(bset
);
4476 bset
= isl_basic_set_normalize(bset
);
4479 total
= isl_basic_set_total_dim(bset
);
4480 isl_hash_byte(hash
, bset
->n_eq
& 0xFF);
4481 for (i
= 0; i
< bset
->n_eq
; ++i
) {
4483 c_hash
= isl_seq_get_hash(bset
->eq
[i
], 1 + total
);
4484 isl_hash_hash(hash
, c_hash
);
4486 isl_hash_byte(hash
, bset
->n_ineq
& 0xFF);
4487 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
4489 c_hash
= isl_seq_get_hash(bset
->ineq
[i
], 1 + total
);
4490 isl_hash_hash(hash
, c_hash
);
4492 isl_hash_byte(hash
, bset
->n_div
& 0xFF);
4493 for (i
= 0; i
< bset
->n_div
; ++i
) {
4495 if (isl_int_is_zero(bset
->div
[i
][0]))
4497 isl_hash_byte(hash
, i
& 0xFF);
4498 c_hash
= isl_seq_get_hash(bset
->div
[i
], 1 + 1 + total
);
4499 isl_hash_hash(hash
, c_hash
);
4501 isl_basic_set_free(bset
);
4505 uint32_t isl_set_get_hash(struct isl_set
*set
)
4512 set
= isl_set_copy(set
);
4513 set
= isl_set_normalize(set
);
4517 hash
= isl_hash_init();
4518 for (i
= 0; i
< set
->n
; ++i
) {
4520 bset_hash
= isl_basic_set_get_hash(set
->p
[i
]);
4521 isl_hash_hash(hash
, bset_hash
);
4529 /* Check if the value for dimension dim is completely determined
4530 * by the values of the other parameters and variables.
4531 * That is, check if dimension dim is involved in an equality.
4533 int isl_basic_set_dim_is_unique(struct isl_basic_set
*bset
, unsigned dim
)
4540 nparam
= isl_basic_set_n_param(bset
);
4541 for (i
= 0; i
< bset
->n_eq
; ++i
)
4542 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ dim
]))
4547 /* Check if the value for dimension dim is completely determined
4548 * by the values of the other parameters and variables.
4549 * That is, check if dimension dim is involved in an equality
4550 * for each of the subsets.
4552 int isl_set_dim_is_unique(struct isl_set
*set
, unsigned dim
)
4558 for (i
= 0; i
< set
->n
; ++i
) {
4560 unique
= isl_basic_set_dim_is_unique(set
->p
[i
], dim
);