2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
5 * Use of this software is governed by the GNU LGPLv2.1 license
7 * Written by Sven Verdoolaege, K.U.Leuven, Departement
8 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
9 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
10 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
17 #include "isl_dim_private.h"
18 #include "isl_equalities.h"
24 #include "isl_map_private.h"
25 #include "isl_map_piplib.h"
26 #include "isl_sample.h"
30 /* Maps dst positions to src positions */
36 static struct isl_dim_map
*isl_dim_map_alloc(struct isl_ctx
*ctx
, unsigned len
)
39 struct isl_dim_map
*dim_map
;
40 dim_map
= isl_alloc(ctx
, struct isl_dim_map
,
41 sizeof(struct isl_dim_map
) + len
* sizeof(int));
44 dim_map
->len
= 1 + len
;
46 for (i
= 0; i
< len
; ++i
)
47 dim_map
->pos
[1 + i
] = -1;
51 static unsigned n(struct isl_dim
*dim
, enum isl_dim_type type
)
54 case isl_dim_param
: return dim
->nparam
;
55 case isl_dim_in
: return dim
->n_in
;
56 case isl_dim_out
: return dim
->n_out
;
57 case isl_dim_all
: return dim
->nparam
+ dim
->n_in
+ dim
->n_out
;
62 static unsigned pos(struct isl_dim
*dim
, enum isl_dim_type type
)
65 case isl_dim_param
: return 1;
66 case isl_dim_in
: return 1 + dim
->nparam
;
67 case isl_dim_out
: return 1 + dim
->nparam
+ dim
->n_in
;
72 static void isl_dim_map_dim_range(struct isl_dim_map
*dim_map
,
73 struct isl_dim
*dim
, enum isl_dim_type type
,
74 unsigned first
, unsigned n
, unsigned dst_pos
)
82 src_pos
= pos(dim
, type
);
83 for (i
= 0; i
< n
; ++i
)
84 dim_map
->pos
[1 + dst_pos
+ i
] = src_pos
+ first
+ i
;
87 static void isl_dim_map_dim(struct isl_dim_map
*dim_map
, struct isl_dim
*dim
,
88 enum isl_dim_type type
, unsigned dst_pos
)
90 isl_dim_map_dim_range(dim_map
, dim
, type
, 0, n(dim
, type
), dst_pos
);
93 static void isl_dim_map_div(struct isl_dim_map
*dim_map
,
94 struct isl_basic_map
*bmap
, unsigned dst_pos
)
99 if (!dim_map
|| !bmap
)
102 src_pos
= 1 + isl_dim_total(bmap
->dim
);
103 for (i
= 0; i
< bmap
->n_div
; ++i
)
104 dim_map
->pos
[1 + dst_pos
+ i
] = src_pos
+ i
;
107 static void isl_dim_map_dump(struct isl_dim_map
*dim_map
)
111 for (i
= 0; i
< dim_map
->len
; ++i
)
112 fprintf(stderr
, "%d -> %d; ", i
, dim_map
->pos
[i
]);
113 fprintf(stderr
, "\n");
116 unsigned isl_basic_map_dim(const struct isl_basic_map
*bmap
,
117 enum isl_dim_type type
)
124 case isl_dim_out
: return isl_dim_size(bmap
->dim
, type
);
125 case isl_dim_div
: return bmap
->n_div
;
126 case isl_dim_all
: return isl_basic_map_total_dim(bmap
);
131 unsigned isl_map_dim(const struct isl_map
*map
, enum isl_dim_type type
)
133 return map
? n(map
->dim
, type
) : 0;
136 unsigned isl_set_dim(const struct isl_set
*set
, enum isl_dim_type type
)
138 return set
? n(set
->dim
, type
) : 0;
141 unsigned isl_basic_map_offset(struct isl_basic_map
*bmap
,
142 enum isl_dim_type type
)
144 struct isl_dim
*dim
= bmap
->dim
;
146 case isl_dim_param
: return 1;
147 case isl_dim_in
: return 1 + dim
->nparam
;
148 case isl_dim_out
: return 1 + dim
->nparam
+ dim
->n_in
;
149 case isl_dim_div
: return 1 + dim
->nparam
+ dim
->n_in
+ dim
->n_out
;
154 static unsigned map_offset(struct isl_map
*map
, enum isl_dim_type type
)
156 return pos(map
->dim
, type
);
159 unsigned isl_basic_set_dim(const struct isl_basic_set
*bset
,
160 enum isl_dim_type type
)
162 return isl_basic_map_dim((const struct isl_basic_map
*)bset
, type
);
165 unsigned isl_basic_set_n_dim(const struct isl_basic_set
*bset
)
167 return isl_basic_set_dim(bset
, isl_dim_set
);
170 unsigned isl_basic_set_n_param(const struct isl_basic_set
*bset
)
172 return isl_basic_set_dim(bset
, isl_dim_param
);
175 unsigned isl_basic_set_total_dim(const struct isl_basic_set
*bset
)
177 return isl_dim_total(bset
->dim
) + bset
->n_div
;
180 unsigned isl_set_n_dim(const struct isl_set
*set
)
182 return isl_set_dim(set
, isl_dim_set
);
185 unsigned isl_set_n_param(const struct isl_set
*set
)
187 return isl_set_dim(set
, isl_dim_param
);
190 unsigned isl_basic_map_n_in(const struct isl_basic_map
*bmap
)
192 return bmap
? bmap
->dim
->n_in
: 0;
195 unsigned isl_basic_map_n_out(const struct isl_basic_map
*bmap
)
197 return bmap
? bmap
->dim
->n_out
: 0;
200 unsigned isl_basic_map_n_param(const struct isl_basic_map
*bmap
)
202 return bmap
? bmap
->dim
->nparam
: 0;
205 unsigned isl_basic_map_n_div(const struct isl_basic_map
*bmap
)
207 return bmap
? bmap
->n_div
: 0;
210 unsigned isl_basic_map_total_dim(const struct isl_basic_map
*bmap
)
212 return bmap
? isl_dim_total(bmap
->dim
) + bmap
->n_div
: 0;
215 unsigned isl_map_n_in(const struct isl_map
*map
)
217 return map
->dim
->n_in
;
220 unsigned isl_map_n_out(const struct isl_map
*map
)
222 return map
->dim
->n_out
;
225 unsigned isl_map_n_param(const struct isl_map
*map
)
227 return map
->dim
->nparam
;
230 int isl_map_compatible_domain(struct isl_map
*map
, struct isl_set
*set
)
235 m
= isl_dim_match(map
->dim
, isl_dim_param
, set
->dim
, isl_dim_param
);
238 return isl_dim_tuple_match(map
->dim
, isl_dim_in
, set
->dim
, isl_dim_set
);
241 int isl_basic_map_compatible_domain(struct isl_basic_map
*bmap
,
242 struct isl_basic_set
*bset
)
247 m
= isl_dim_match(bmap
->dim
, isl_dim_param
, bset
->dim
, isl_dim_param
);
250 return isl_dim_tuple_match(bmap
->dim
, isl_dim_in
, bset
->dim
, isl_dim_set
);
253 int isl_basic_map_compatible_range(struct isl_basic_map
*bmap
,
254 struct isl_basic_set
*bset
)
259 m
= isl_dim_match(bmap
->dim
, isl_dim_param
, bset
->dim
, isl_dim_param
);
262 return isl_dim_tuple_match(bmap
->dim
, isl_dim_out
, bset
->dim
, isl_dim_set
);
265 isl_ctx
*isl_map_get_ctx(__isl_keep isl_map
*map
)
267 return map
? map
->ctx
: NULL
;
270 isl_ctx
*isl_set_get_ctx(__isl_keep isl_set
*set
)
272 return set
? set
->ctx
: NULL
;
275 struct isl_dim
*isl_basic_map_get_dim(struct isl_basic_map
*bmap
)
279 return isl_dim_copy(bmap
->dim
);
282 struct isl_dim
*isl_basic_set_get_dim(struct isl_basic_set
*bset
)
286 return isl_dim_copy(bset
->dim
);
289 struct isl_dim
*isl_map_get_dim(struct isl_map
*map
)
293 return isl_dim_copy(map
->dim
);
296 struct isl_dim
*isl_set_get_dim(struct isl_set
*set
)
300 return isl_dim_copy(set
->dim
);
303 __isl_give isl_basic_map
*isl_basic_map_set_tuple_name(
304 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
, const char *s
)
306 bmap
= isl_basic_map_cow(bmap
);
309 bmap
->dim
= isl_dim_set_tuple_name(bmap
->dim
, type
, s
);
314 isl_basic_map_free(bmap
);
318 __isl_give isl_map
*isl_map_set_tuple_name(__isl_take isl_map
*map
,
319 enum isl_dim_type type
, const char *s
)
323 map
= isl_map_cow(map
);
327 map
->dim
= isl_dim_set_tuple_name(map
->dim
, type
, s
);
331 for (i
= 0; i
< map
->n
; ++i
) {
332 map
->p
[i
] = isl_basic_map_set_tuple_name(map
->p
[i
], type
, s
);
343 __isl_give isl_basic_map
*isl_basic_map_set_dim_name(
344 __isl_take isl_basic_map
*bmap
,
345 enum isl_dim_type type
, unsigned pos
, const char *s
)
349 bmap
->dim
= isl_dim_set_name(bmap
->dim
, type
, pos
, s
);
354 isl_basic_map_free(bmap
);
358 __isl_give isl_map
*isl_map_set_dim_name(__isl_take isl_map
*map
,
359 enum isl_dim_type type
, unsigned pos
, const char *s
)
366 map
->dim
= isl_dim_set_name(map
->dim
, type
, pos
, s
);
370 for (i
= 0; i
< map
->n
; ++i
) {
371 map
->p
[i
] = isl_basic_map_set_dim_name(map
->p
[i
], type
, pos
, s
);
382 __isl_give isl_basic_set
*isl_basic_set_set_dim_name(
383 __isl_take isl_basic_set
*bset
,
384 enum isl_dim_type type
, unsigned pos
, const char *s
)
386 return (isl_basic_set
*)isl_basic_map_set_dim_name(
387 (isl_basic_map
*)bset
, type
, pos
, s
);
390 __isl_give isl_set
*isl_set_set_dim_name(__isl_take isl_set
*set
,
391 enum isl_dim_type type
, unsigned pos
, const char *s
)
393 return (isl_set
*)isl_map_set_dim_name((isl_map
*)set
, type
, pos
, s
);
396 int isl_basic_map_is_rational(__isl_keep isl_basic_map
*bmap
)
400 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
403 static struct isl_basic_map
*basic_map_init(struct isl_ctx
*ctx
,
404 struct isl_basic_map
*bmap
, unsigned extra
,
405 unsigned n_eq
, unsigned n_ineq
)
408 size_t row_size
= 1 + isl_dim_total(bmap
->dim
) + extra
;
413 bmap
->block
= isl_blk_alloc(ctx
, (n_ineq
+ n_eq
) * row_size
);
414 if (isl_blk_is_error(bmap
->block
))
417 bmap
->ineq
= isl_alloc_array(ctx
, isl_int
*, n_ineq
+ n_eq
);
422 bmap
->block2
= isl_blk_empty();
425 bmap
->block2
= isl_blk_alloc(ctx
, extra
* (1 + row_size
));
426 if (isl_blk_is_error(bmap
->block2
))
429 bmap
->div
= isl_alloc_array(ctx
, isl_int
*, extra
);
434 for (i
= 0; i
< n_ineq
+ n_eq
; ++i
)
435 bmap
->ineq
[i
] = bmap
->block
.data
+ i
* row_size
;
437 for (i
= 0; i
< extra
; ++i
)
438 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + row_size
);
442 bmap
->c_size
= n_eq
+ n_ineq
;
443 bmap
->eq
= bmap
->ineq
+ n_ineq
;
452 isl_basic_map_free(bmap
);
456 struct isl_basic_set
*isl_basic_set_alloc(struct isl_ctx
*ctx
,
457 unsigned nparam
, unsigned dim
, unsigned extra
,
458 unsigned n_eq
, unsigned n_ineq
)
460 struct isl_basic_map
*bmap
;
461 bmap
= isl_basic_map_alloc(ctx
, nparam
, 0, dim
, extra
, n_eq
, n_ineq
);
462 return (struct isl_basic_set
*)bmap
;
465 struct isl_basic_set
*isl_basic_set_alloc_dim(struct isl_dim
*dim
,
466 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
468 struct isl_basic_map
*bmap
;
471 isl_assert(dim
->ctx
, dim
->n_in
== 0, return NULL
);
472 bmap
= isl_basic_map_alloc_dim(dim
, extra
, n_eq
, n_ineq
);
473 return (struct isl_basic_set
*)bmap
;
476 struct isl_basic_map
*isl_basic_map_alloc_dim(struct isl_dim
*dim
,
477 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
479 struct isl_basic_map
*bmap
;
483 bmap
= isl_calloc_type(dim
->ctx
, struct isl_basic_map
);
488 return basic_map_init(dim
->ctx
, bmap
, extra
, n_eq
, n_ineq
);
494 struct isl_basic_map
*isl_basic_map_alloc(struct isl_ctx
*ctx
,
495 unsigned nparam
, unsigned in
, unsigned out
, unsigned extra
,
496 unsigned n_eq
, unsigned n_ineq
)
498 struct isl_basic_map
*bmap
;
501 dim
= isl_dim_alloc(ctx
, nparam
, in
, out
);
505 bmap
= isl_basic_map_alloc_dim(dim
, extra
, n_eq
, n_ineq
);
509 static void dup_constraints(
510 struct isl_basic_map
*dst
, struct isl_basic_map
*src
)
513 unsigned total
= isl_basic_map_total_dim(src
);
515 for (i
= 0; i
< src
->n_eq
; ++i
) {
516 int j
= isl_basic_map_alloc_equality(dst
);
517 isl_seq_cpy(dst
->eq
[j
], src
->eq
[i
], 1+total
);
520 for (i
= 0; i
< src
->n_ineq
; ++i
) {
521 int j
= isl_basic_map_alloc_inequality(dst
);
522 isl_seq_cpy(dst
->ineq
[j
], src
->ineq
[i
], 1+total
);
525 for (i
= 0; i
< src
->n_div
; ++i
) {
526 int j
= isl_basic_map_alloc_div(dst
);
527 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
);
529 ISL_F_SET(dst
, ISL_BASIC_SET_FINAL
);
532 struct isl_basic_map
*isl_basic_map_dup(struct isl_basic_map
*bmap
)
534 struct isl_basic_map
*dup
;
538 dup
= isl_basic_map_alloc_dim(isl_dim_copy(bmap
->dim
),
539 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
542 dup_constraints(dup
, bmap
);
543 dup
->flags
= bmap
->flags
;
544 dup
->sample
= isl_vec_copy(bmap
->sample
);
548 struct isl_basic_set
*isl_basic_set_dup(struct isl_basic_set
*bset
)
550 struct isl_basic_map
*dup
;
552 dup
= isl_basic_map_dup((struct isl_basic_map
*)bset
);
553 return (struct isl_basic_set
*)dup
;
556 struct isl_basic_set
*isl_basic_set_copy(struct isl_basic_set
*bset
)
561 if (ISL_F_ISSET(bset
, ISL_BASIC_SET_FINAL
)) {
565 return isl_basic_set_dup(bset
);
568 struct isl_set
*isl_set_copy(struct isl_set
*set
)
577 struct isl_basic_map
*isl_basic_map_copy(struct isl_basic_map
*bmap
)
582 if (ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
)) {
586 bmap
= isl_basic_map_dup(bmap
);
588 ISL_F_SET(bmap
, ISL_BASIC_SET_FINAL
);
592 struct isl_map
*isl_map_copy(struct isl_map
*map
)
601 void isl_basic_map_free(struct isl_basic_map
*bmap
)
609 isl_ctx_deref(bmap
->ctx
);
611 isl_blk_free(bmap
->ctx
, bmap
->block2
);
613 isl_blk_free(bmap
->ctx
, bmap
->block
);
614 isl_vec_free(bmap
->sample
);
615 isl_dim_free(bmap
->dim
);
619 void isl_basic_set_free(struct isl_basic_set
*bset
)
621 isl_basic_map_free((struct isl_basic_map
*)bset
);
624 static int room_for_con(struct isl_basic_map
*bmap
, unsigned n
)
626 return bmap
->n_eq
+ bmap
->n_ineq
+ n
<= bmap
->c_size
;
629 int isl_basic_map_alloc_equality(struct isl_basic_map
*bmap
)
635 isl_assert(ctx
, room_for_con(bmap
, 1), return -1);
636 isl_assert(ctx
, (bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
<= bmap
->c_size
,
638 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
639 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
640 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
641 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
642 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
643 if ((bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
== bmap
->c_size
) {
645 int j
= isl_basic_map_alloc_inequality(bmap
);
649 bmap
->ineq
[j
] = bmap
->ineq
[bmap
->n_ineq
- 1];
650 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
657 isl_seq_clr(bmap
->eq
[bmap
->n_eq
] + 1 + isl_basic_map_total_dim(bmap
),
658 bmap
->extra
- bmap
->n_div
);
662 int isl_basic_set_alloc_equality(struct isl_basic_set
*bset
)
664 return isl_basic_map_alloc_equality((struct isl_basic_map
*)bset
);
667 int isl_basic_map_free_equality(struct isl_basic_map
*bmap
, unsigned n
)
671 isl_assert(bmap
->ctx
, n
<= bmap
->n_eq
, return -1);
676 int isl_basic_set_free_equality(struct isl_basic_set
*bset
, unsigned n
)
678 return isl_basic_map_free_equality((struct isl_basic_map
*)bset
, n
);
681 int isl_basic_map_drop_equality(struct isl_basic_map
*bmap
, unsigned pos
)
686 isl_assert(bmap
->ctx
, pos
< bmap
->n_eq
, return -1);
688 if (pos
!= bmap
->n_eq
- 1) {
690 bmap
->eq
[pos
] = bmap
->eq
[bmap
->n_eq
- 1];
691 bmap
->eq
[bmap
->n_eq
- 1] = t
;
697 int isl_basic_set_drop_equality(struct isl_basic_set
*bset
, unsigned pos
)
699 return isl_basic_map_drop_equality((struct isl_basic_map
*)bset
, pos
);
702 void isl_basic_map_inequality_to_equality(
703 struct isl_basic_map
*bmap
, unsigned pos
)
708 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
709 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
714 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
715 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
716 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
717 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
720 static int room_for_ineq(struct isl_basic_map
*bmap
, unsigned n
)
722 return bmap
->n_ineq
+ n
<= bmap
->eq
- bmap
->ineq
;
725 int isl_basic_map_alloc_inequality(struct isl_basic_map
*bmap
)
731 isl_assert(ctx
, room_for_ineq(bmap
, 1), return -1);
732 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
733 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
734 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
735 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
736 isl_seq_clr(bmap
->ineq
[bmap
->n_ineq
] +
737 1 + isl_basic_map_total_dim(bmap
),
738 bmap
->extra
- bmap
->n_div
);
739 return bmap
->n_ineq
++;
742 int isl_basic_set_alloc_inequality(struct isl_basic_set
*bset
)
744 return isl_basic_map_alloc_inequality((struct isl_basic_map
*)bset
);
747 int isl_basic_map_free_inequality(struct isl_basic_map
*bmap
, unsigned n
)
751 isl_assert(bmap
->ctx
, n
<= bmap
->n_ineq
, return -1);
756 int isl_basic_set_free_inequality(struct isl_basic_set
*bset
, unsigned n
)
758 return isl_basic_map_free_inequality((struct isl_basic_map
*)bset
, n
);
761 int isl_basic_map_drop_inequality(struct isl_basic_map
*bmap
, unsigned pos
)
766 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
768 if (pos
!= bmap
->n_ineq
- 1) {
770 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
771 bmap
->ineq
[bmap
->n_ineq
- 1] = t
;
772 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
778 int isl_basic_set_drop_inequality(struct isl_basic_set
*bset
, unsigned pos
)
780 return isl_basic_map_drop_inequality((struct isl_basic_map
*)bset
, pos
);
783 __isl_give isl_basic_map
*isl_basic_map_add_eq(__isl_take isl_basic_map
*bmap
,
788 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
791 k
= isl_basic_map_alloc_equality(bmap
);
794 isl_seq_cpy(bmap
->eq
[k
], eq
, 1 + isl_basic_map_total_dim(bmap
));
797 isl_basic_map_free(bmap
);
801 __isl_give isl_basic_set
*isl_basic_set_add_eq(__isl_take isl_basic_set
*bset
,
804 return (isl_basic_set
*)
805 isl_basic_map_add_eq((isl_basic_map
*)bset
, eq
);
808 __isl_give isl_basic_map
*isl_basic_map_add_ineq(__isl_take isl_basic_map
*bmap
,
813 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
816 k
= isl_basic_map_alloc_inequality(bmap
);
819 isl_seq_cpy(bmap
->ineq
[k
], ineq
, 1 + isl_basic_map_total_dim(bmap
));
822 isl_basic_map_free(bmap
);
826 __isl_give isl_basic_set
*isl_basic_set_add_ineq(__isl_take isl_basic_set
*bset
,
829 return (isl_basic_set
*)
830 isl_basic_map_add_ineq((isl_basic_map
*)bset
, ineq
);
833 int isl_basic_map_alloc_div(struct isl_basic_map
*bmap
)
837 isl_assert(bmap
->ctx
, bmap
->n_div
< bmap
->extra
, return -1);
838 isl_seq_clr(bmap
->div
[bmap
->n_div
] +
839 1 + 1 + isl_basic_map_total_dim(bmap
),
840 bmap
->extra
- bmap
->n_div
);
841 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
842 return bmap
->n_div
++;
845 int isl_basic_set_alloc_div(struct isl_basic_set
*bset
)
847 return isl_basic_map_alloc_div((struct isl_basic_map
*)bset
);
850 int isl_basic_map_free_div(struct isl_basic_map
*bmap
, unsigned n
)
854 isl_assert(bmap
->ctx
, n
<= bmap
->n_div
, return -1);
859 int isl_basic_set_free_div(struct isl_basic_set
*bset
, unsigned n
)
861 return isl_basic_map_free_div((struct isl_basic_map
*)bset
, n
);
864 /* Copy constraint from src to dst, putting the vars of src at offset
865 * dim_off in dst and the divs of src at offset div_off in dst.
866 * If both sets are actually map, then dim_off applies to the input
869 static void copy_constraint(struct isl_basic_map
*dst_map
, isl_int
*dst
,
870 struct isl_basic_map
*src_map
, isl_int
*src
,
871 unsigned in_off
, unsigned out_off
, unsigned div_off
)
873 unsigned src_nparam
= isl_basic_map_n_param(src_map
);
874 unsigned dst_nparam
= isl_basic_map_n_param(dst_map
);
875 unsigned src_in
= isl_basic_map_n_in(src_map
);
876 unsigned dst_in
= isl_basic_map_n_in(dst_map
);
877 unsigned src_out
= isl_basic_map_n_out(src_map
);
878 unsigned dst_out
= isl_basic_map_n_out(dst_map
);
879 isl_int_set(dst
[0], src
[0]);
880 isl_seq_cpy(dst
+1, src
+1, isl_min(dst_nparam
, src_nparam
));
881 if (dst_nparam
> src_nparam
)
882 isl_seq_clr(dst
+1+src_nparam
,
883 dst_nparam
- src_nparam
);
884 isl_seq_clr(dst
+1+dst_nparam
, in_off
);
885 isl_seq_cpy(dst
+1+dst_nparam
+in_off
,
887 isl_min(dst_in
-in_off
, src_in
));
888 if (dst_in
-in_off
> src_in
)
889 isl_seq_clr(dst
+1+dst_nparam
+in_off
+src_in
,
890 dst_in
- in_off
- src_in
);
891 isl_seq_clr(dst
+1+dst_nparam
+dst_in
, out_off
);
892 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+out_off
,
893 src
+1+src_nparam
+src_in
,
894 isl_min(dst_out
-out_off
, src_out
));
895 if (dst_out
-out_off
> src_out
)
896 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+out_off
+src_out
,
897 dst_out
- out_off
- src_out
);
898 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
, div_off
);
899 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+dst_out
+div_off
,
900 src
+1+src_nparam
+src_in
+src_out
,
901 isl_min(dst_map
->extra
-div_off
, src_map
->n_div
));
902 if (dst_map
->n_div
-div_off
> src_map
->n_div
)
903 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
+
904 div_off
+src_map
->n_div
,
905 dst_map
->n_div
- div_off
- src_map
->n_div
);
908 static void copy_div(struct isl_basic_map
*dst_map
, isl_int
*dst
,
909 struct isl_basic_map
*src_map
, isl_int
*src
,
910 unsigned in_off
, unsigned out_off
, unsigned div_off
)
912 isl_int_set(dst
[0], src
[0]);
913 copy_constraint(dst_map
, dst
+1, src_map
, src
+1, in_off
, out_off
, div_off
);
916 static struct isl_basic_map
*add_constraints(struct isl_basic_map
*bmap1
,
917 struct isl_basic_map
*bmap2
, unsigned i_pos
, unsigned o_pos
)
922 if (!bmap1
|| !bmap2
)
925 div_off
= bmap1
->n_div
;
927 for (i
= 0; i
< bmap2
->n_eq
; ++i
) {
928 int i1
= isl_basic_map_alloc_equality(bmap1
);
931 copy_constraint(bmap1
, bmap1
->eq
[i1
], bmap2
, bmap2
->eq
[i
],
932 i_pos
, o_pos
, div_off
);
935 for (i
= 0; i
< bmap2
->n_ineq
; ++i
) {
936 int i1
= isl_basic_map_alloc_inequality(bmap1
);
939 copy_constraint(bmap1
, bmap1
->ineq
[i1
], bmap2
, bmap2
->ineq
[i
],
940 i_pos
, o_pos
, div_off
);
943 for (i
= 0; i
< bmap2
->n_div
; ++i
) {
944 int i1
= isl_basic_map_alloc_div(bmap1
);
947 copy_div(bmap1
, bmap1
->div
[i1
], bmap2
, bmap2
->div
[i
],
948 i_pos
, o_pos
, div_off
);
951 isl_basic_map_free(bmap2
);
956 isl_basic_map_free(bmap1
);
957 isl_basic_map_free(bmap2
);
961 static void copy_constraint_dim_map(isl_int
*dst
, isl_int
*src
,
962 struct isl_dim_map
*dim_map
)
966 for (i
= 0; i
< dim_map
->len
; ++i
) {
967 if (dim_map
->pos
[i
] < 0)
968 isl_int_set_si(dst
[i
], 0);
970 isl_int_set(dst
[i
], src
[dim_map
->pos
[i
]]);
974 static void copy_div_dim_map(isl_int
*dst
, isl_int
*src
,
975 struct isl_dim_map
*dim_map
)
977 isl_int_set(dst
[0], src
[0]);
978 copy_constraint_dim_map(dst
+1, src
+1, dim_map
);
981 static struct isl_basic_map
*add_constraints_dim_map(struct isl_basic_map
*dst
,
982 struct isl_basic_map
*src
, struct isl_dim_map
*dim_map
)
986 if (!src
|| !dst
|| !dim_map
)
989 for (i
= 0; i
< src
->n_eq
; ++i
) {
990 int i1
= isl_basic_map_alloc_equality(dst
);
993 copy_constraint_dim_map(dst
->eq
[i1
], src
->eq
[i
], dim_map
);
996 for (i
= 0; i
< src
->n_ineq
; ++i
) {
997 int i1
= isl_basic_map_alloc_inequality(dst
);
1000 copy_constraint_dim_map(dst
->ineq
[i1
], src
->ineq
[i
], dim_map
);
1003 for (i
= 0; i
< src
->n_div
; ++i
) {
1004 int i1
= isl_basic_map_alloc_div(dst
);
1007 copy_div_dim_map(dst
->div
[i1
], src
->div
[i
], dim_map
);
1011 isl_basic_map_free(src
);
1016 isl_basic_map_free(src
);
1017 isl_basic_map_free(dst
);
1021 struct isl_basic_set
*isl_basic_set_add_constraints(struct isl_basic_set
*bset1
,
1022 struct isl_basic_set
*bset2
, unsigned pos
)
1024 return (struct isl_basic_set
*)
1025 add_constraints((struct isl_basic_map
*)bset1
,
1026 (struct isl_basic_map
*)bset2
, 0, pos
);
1029 struct isl_basic_map
*isl_basic_map_extend_dim(struct isl_basic_map
*base
,
1030 struct isl_dim
*dim
, unsigned extra
,
1031 unsigned n_eq
, unsigned n_ineq
)
1033 struct isl_basic_map
*ext
;
1043 dims_ok
= isl_dim_equal(base
->dim
, dim
) &&
1044 base
->extra
>= base
->n_div
+ extra
;
1046 if (dims_ok
&& room_for_con(base
, n_eq
+ n_ineq
) &&
1047 room_for_ineq(base
, n_ineq
)) {
1052 isl_assert(base
->ctx
, base
->dim
->nparam
<= dim
->nparam
, goto error
);
1053 isl_assert(base
->ctx
, base
->dim
->n_in
<= dim
->n_in
, goto error
);
1054 isl_assert(base
->ctx
, base
->dim
->n_out
<= dim
->n_out
, goto error
);
1055 extra
+= base
->extra
;
1057 n_ineq
+= base
->n_ineq
;
1059 ext
= isl_basic_map_alloc_dim(dim
, extra
, n_eq
, n_ineq
);
1065 ext
->sample
= isl_vec_copy(base
->sample
);
1066 flags
= base
->flags
;
1067 ext
= add_constraints(ext
, base
, 0, 0);
1070 ISL_F_CLR(ext
, ISL_BASIC_SET_FINAL
);
1077 isl_basic_map_free(base
);
1081 struct isl_basic_set
*isl_basic_set_extend_dim(struct isl_basic_set
*base
,
1082 struct isl_dim
*dim
, unsigned extra
,
1083 unsigned n_eq
, unsigned n_ineq
)
1085 return (struct isl_basic_set
*)
1086 isl_basic_map_extend_dim((struct isl_basic_map
*)base
, dim
,
1087 extra
, n_eq
, n_ineq
);
1090 struct isl_basic_map
*isl_basic_map_extend_constraints(
1091 struct isl_basic_map
*base
, unsigned n_eq
, unsigned n_ineq
)
1095 return isl_basic_map_extend_dim(base
, isl_dim_copy(base
->dim
),
1099 struct isl_basic_map
*isl_basic_map_extend(struct isl_basic_map
*base
,
1100 unsigned nparam
, unsigned n_in
, unsigned n_out
, unsigned extra
,
1101 unsigned n_eq
, unsigned n_ineq
)
1103 struct isl_basic_map
*bmap
;
1104 struct isl_dim
*dim
;
1108 dim
= isl_dim_alloc(base
->ctx
, nparam
, n_in
, n_out
);
1112 bmap
= isl_basic_map_extend_dim(base
, dim
, extra
, n_eq
, n_ineq
);
1115 isl_basic_map_free(base
);
1119 struct isl_basic_set
*isl_basic_set_extend(struct isl_basic_set
*base
,
1120 unsigned nparam
, unsigned dim
, unsigned extra
,
1121 unsigned n_eq
, unsigned n_ineq
)
1123 return (struct isl_basic_set
*)
1124 isl_basic_map_extend((struct isl_basic_map
*)base
,
1125 nparam
, 0, dim
, extra
, n_eq
, n_ineq
);
1128 struct isl_basic_set
*isl_basic_set_extend_constraints(
1129 struct isl_basic_set
*base
, unsigned n_eq
, unsigned n_ineq
)
1131 return (struct isl_basic_set
*)
1132 isl_basic_map_extend_constraints((struct isl_basic_map
*)base
,
1136 struct isl_basic_set
*isl_basic_set_cow(struct isl_basic_set
*bset
)
1138 return (struct isl_basic_set
*)
1139 isl_basic_map_cow((struct isl_basic_map
*)bset
);
1142 struct isl_basic_map
*isl_basic_map_cow(struct isl_basic_map
*bmap
)
1147 if (bmap
->ref
> 1) {
1149 bmap
= isl_basic_map_dup(bmap
);
1152 ISL_F_CLR(bmap
, ISL_BASIC_SET_FINAL
);
1156 struct isl_set
*isl_set_cow(struct isl_set
*set
)
1164 return isl_set_dup(set
);
1167 struct isl_map
*isl_map_cow(struct isl_map
*map
)
1175 return isl_map_dup(map
);
1178 static void swap_vars(struct isl_blk blk
, isl_int
*a
,
1179 unsigned a_len
, unsigned b_len
)
1181 isl_seq_cpy(blk
.data
, a
+a_len
, b_len
);
1182 isl_seq_cpy(blk
.data
+b_len
, a
, a_len
);
1183 isl_seq_cpy(a
, blk
.data
, b_len
+a_len
);
1186 struct isl_basic_set
*isl_basic_set_swap_vars(
1187 struct isl_basic_set
*bset
, unsigned n
)
1197 nparam
= isl_basic_set_n_param(bset
);
1198 dim
= isl_basic_set_n_dim(bset
);
1199 isl_assert(bset
->ctx
, n
<= dim
, goto error
);
1204 bset
= isl_basic_set_cow(bset
);
1208 blk
= isl_blk_alloc(bset
->ctx
, dim
);
1209 if (isl_blk_is_error(blk
))
1212 for (i
= 0; i
< bset
->n_eq
; ++i
)
1214 bset
->eq
[i
]+1+nparam
, n
, dim
- n
);
1216 for (i
= 0; i
< bset
->n_ineq
; ++i
)
1218 bset
->ineq
[i
]+1+nparam
, n
, dim
- n
);
1220 for (i
= 0; i
< bset
->n_div
; ++i
)
1222 bset
->div
[i
]+1+1+nparam
, n
, dim
- n
);
1224 isl_blk_free(bset
->ctx
, blk
);
1226 ISL_F_CLR(bset
, ISL_BASIC_SET_NORMALIZED
);
1227 return isl_basic_set_gauss(bset
, NULL
);
1229 isl_basic_set_free(bset
);
1233 struct isl_set
*isl_set_swap_vars(struct isl_set
*set
, unsigned n
)
1236 set
= isl_set_cow(set
);
1240 for (i
= 0; i
< set
->n
; ++i
) {
1241 set
->p
[i
] = isl_basic_set_swap_vars(set
->p
[i
], n
);
1247 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
1251 struct isl_basic_map
*isl_basic_map_set_to_empty(struct isl_basic_map
*bmap
)
1257 total
= isl_basic_map_total_dim(bmap
);
1258 isl_basic_map_free_div(bmap
, bmap
->n_div
);
1259 isl_basic_map_free_inequality(bmap
, bmap
->n_ineq
);
1261 isl_basic_map_free_equality(bmap
, bmap
->n_eq
-1);
1263 i
= isl_basic_map_alloc_equality(bmap
);
1267 isl_int_set_si(bmap
->eq
[i
][0], 1);
1268 isl_seq_clr(bmap
->eq
[i
]+1, total
);
1269 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
1270 isl_vec_free(bmap
->sample
);
1271 bmap
->sample
= NULL
;
1272 return isl_basic_map_finalize(bmap
);
1274 isl_basic_map_free(bmap
);
1278 struct isl_basic_set
*isl_basic_set_set_to_empty(struct isl_basic_set
*bset
)
1280 return (struct isl_basic_set
*)
1281 isl_basic_map_set_to_empty((struct isl_basic_map
*)bset
);
1284 void isl_basic_map_swap_div(struct isl_basic_map
*bmap
, int a
, int b
)
1287 unsigned off
= isl_dim_total(bmap
->dim
);
1288 isl_int
*t
= bmap
->div
[a
];
1289 bmap
->div
[a
] = bmap
->div
[b
];
1292 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1293 isl_int_swap(bmap
->eq
[i
][1+off
+a
], bmap
->eq
[i
][1+off
+b
]);
1295 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1296 isl_int_swap(bmap
->ineq
[i
][1+off
+a
], bmap
->ineq
[i
][1+off
+b
]);
1298 for (i
= 0; i
< bmap
->n_div
; ++i
)
1299 isl_int_swap(bmap
->div
[i
][1+1+off
+a
], bmap
->div
[i
][1+1+off
+b
]);
1300 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1303 /* Eliminate the specified n dimensions starting at first from the
1304 * constraints using Fourier-Motzkin. The dimensions themselves
1307 __isl_give isl_map
*isl_map_eliminate(__isl_take isl_map
*map
,
1308 enum isl_dim_type type
, unsigned first
, unsigned n
)
1317 map
= isl_map_cow(map
);
1320 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
1321 first
+= pos(map
->dim
, type
) - 1;
1323 for (i
= 0; i
< map
->n
; ++i
) {
1324 map
->p
[i
] = isl_basic_map_eliminate_vars(map
->p
[i
], first
, n
);
1334 /* Eliminate the specified n dimensions starting at first from the
1335 * constraints using Fourier-Motzkin. The dimensions themselves
1338 __isl_give isl_set
*isl_set_eliminate(__isl_take isl_set
*set
,
1339 enum isl_dim_type type
, unsigned first
, unsigned n
)
1341 return (isl_set
*)isl_map_eliminate((isl_map
*)set
, type
, first
, n
);
1344 /* Eliminate the specified n dimensions starting at first from the
1345 * constraints using Fourier-Motzkin. The dimensions themselves
1348 __isl_give isl_set
*isl_set_eliminate_dims(__isl_take isl_set
*set
,
1349 unsigned first
, unsigned n
)
1351 return isl_set_eliminate(set
, isl_dim_set
, first
, n
);
1354 /* Project out n dimensions starting at first using Fourier-Motzkin */
1355 struct isl_set
*isl_set_remove_dims(struct isl_set
*set
,
1356 unsigned first
, unsigned n
)
1358 set
= isl_set_eliminate_dims(set
, first
, n
);
1359 set
= isl_set_drop_dims(set
, first
, n
);
1363 struct isl_basic_set
*isl_basic_set_remove_divs(struct isl_basic_set
*bset
)
1365 bset
= isl_basic_set_eliminate_vars(bset
, isl_dim_total(bset
->dim
),
1373 struct isl_set
*isl_set_remove_divs(struct isl_set
*set
)
1382 set
= isl_set_cow(set
);
1386 for (i
= 0; i
< set
->n
; ++i
) {
1387 set
->p
[i
] = isl_basic_set_remove_divs(set
->p
[i
]);
1397 struct isl_basic_map
*isl_basic_map_remove(struct isl_basic_map
*bmap
,
1398 enum isl_dim_type type
, unsigned first
, unsigned n
)
1402 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
1406 bmap
= isl_basic_map_eliminate_vars(bmap
,
1407 isl_basic_map_offset(bmap
, type
) - 1 + first
, n
);
1410 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
) && type
== isl_dim_div
)
1412 bmap
= isl_basic_map_drop(bmap
, type
, first
, n
);
1415 isl_basic_map_free(bmap
);
1419 __isl_give isl_basic_set
*isl_basic_set_remove(__isl_take isl_basic_set
*bset
,
1420 enum isl_dim_type type
, unsigned first
, unsigned n
)
1422 return (isl_basic_set
*)
1423 isl_basic_map_remove((isl_basic_map
*)bset
, type
, first
, n
);
1426 struct isl_map
*isl_map_remove(struct isl_map
*map
,
1427 enum isl_dim_type type
, unsigned first
, unsigned n
)
1434 map
= isl_map_cow(map
);
1437 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
1439 for (i
= 0; i
< map
->n
; ++i
) {
1440 map
->p
[i
] = isl_basic_map_eliminate_vars(map
->p
[i
],
1441 isl_basic_map_offset(map
->p
[i
], type
) - 1 + first
, n
);
1445 map
= isl_map_drop(map
, type
, first
, n
);
1452 __isl_give isl_set
*isl_set_remove(__isl_take isl_set
*bset
,
1453 enum isl_dim_type type
, unsigned first
, unsigned n
)
1455 return (isl_set
*)isl_map_remove((isl_map
*)bset
, type
, first
, n
);
1458 /* Project out n inputs starting at first using Fourier-Motzkin */
1459 struct isl_map
*isl_map_remove_inputs(struct isl_map
*map
,
1460 unsigned first
, unsigned n
)
1462 return isl_map_remove(map
, isl_dim_in
, first
, n
);
1465 /* Project out n dimensions starting at first using Fourier-Motzkin */
1466 struct isl_basic_set
*isl_basic_set_remove_dims(struct isl_basic_set
*bset
,
1467 unsigned first
, unsigned n
)
1469 unsigned nparam
= isl_basic_set_n_param(bset
);
1470 bset
= isl_basic_set_eliminate_vars(bset
, nparam
+ first
, n
);
1471 bset
= isl_basic_set_drop_dims(bset
, first
, n
);
1475 static void dump_term(struct isl_basic_map
*bmap
,
1476 isl_int c
, int pos
, FILE *out
)
1479 unsigned in
= isl_basic_map_n_in(bmap
);
1480 unsigned dim
= in
+ isl_basic_map_n_out(bmap
);
1481 unsigned nparam
= isl_basic_map_n_param(bmap
);
1483 isl_int_print(out
, c
, 0);
1485 if (!isl_int_is_one(c
))
1486 isl_int_print(out
, c
, 0);
1487 if (pos
< 1 + nparam
) {
1488 name
= isl_dim_get_name(bmap
->dim
,
1489 isl_dim_param
, pos
- 1);
1491 fprintf(out
, "%s", name
);
1493 fprintf(out
, "p%d", pos
- 1);
1494 } else if (pos
< 1 + nparam
+ in
)
1495 fprintf(out
, "i%d", pos
- 1 - nparam
);
1496 else if (pos
< 1 + nparam
+ dim
)
1497 fprintf(out
, "o%d", pos
- 1 - nparam
- in
);
1499 fprintf(out
, "e%d", pos
- 1 - nparam
- dim
);
1503 static void dump_constraint_sign(struct isl_basic_map
*bmap
, isl_int
*c
,
1504 int sign
, FILE *out
)
1508 unsigned len
= 1 + isl_basic_map_total_dim(bmap
);
1512 for (i
= 0, first
= 1; i
< len
; ++i
) {
1513 if (isl_int_sgn(c
[i
]) * sign
<= 0)
1516 fprintf(out
, " + ");
1518 isl_int_abs(v
, c
[i
]);
1519 dump_term(bmap
, v
, i
, out
);
1526 static void dump_constraint(struct isl_basic_map
*bmap
, isl_int
*c
,
1527 const char *op
, FILE *out
, int indent
)
1531 fprintf(out
, "%*s", indent
, "");
1533 dump_constraint_sign(bmap
, c
, 1, out
);
1534 fprintf(out
, " %s ", op
);
1535 dump_constraint_sign(bmap
, c
, -1, out
);
1539 for (i
= bmap
->n_div
; i
< bmap
->extra
; ++i
) {
1540 if (isl_int_is_zero(c
[1+isl_dim_total(bmap
->dim
)+i
]))
1542 fprintf(out
, "%*s", indent
, "");
1543 fprintf(out
, "ERROR: unused div coefficient not zero\n");
1548 static void dump_constraints(struct isl_basic_map
*bmap
,
1549 isl_int
**c
, unsigned n
,
1550 const char *op
, FILE *out
, int indent
)
1554 for (i
= 0; i
< n
; ++i
)
1555 dump_constraint(bmap
, c
[i
], op
, out
, indent
);
1558 static void dump_affine(struct isl_basic_map
*bmap
, isl_int
*exp
, FILE *out
)
1562 unsigned total
= isl_basic_map_total_dim(bmap
);
1564 for (j
= 0; j
< 1 + total
; ++j
) {
1565 if (isl_int_is_zero(exp
[j
]))
1567 if (!first
&& isl_int_is_pos(exp
[j
]))
1569 dump_term(bmap
, exp
[j
], j
, out
);
1574 static void dump(struct isl_basic_map
*bmap
, FILE *out
, int indent
)
1578 dump_constraints(bmap
, bmap
->eq
, bmap
->n_eq
, "=", out
, indent
);
1579 dump_constraints(bmap
, bmap
->ineq
, bmap
->n_ineq
, ">=", out
, indent
);
1581 for (i
= 0; i
< bmap
->n_div
; ++i
) {
1582 fprintf(out
, "%*s", indent
, "");
1583 fprintf(out
, "e%d = [(", i
);
1584 dump_affine(bmap
, bmap
->div
[i
]+1, out
);
1586 isl_int_print(out
, bmap
->div
[i
][0], 0);
1587 fprintf(out
, "]\n");
1591 void isl_basic_set_dump(struct isl_basic_set
*bset
, FILE *out
, int indent
)
1594 fprintf(out
, "null basic set\n");
1598 fprintf(out
, "%*s", indent
, "");
1599 fprintf(out
, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
1600 bset
->ref
, bset
->dim
->nparam
, bset
->dim
->n_out
,
1601 bset
->extra
, bset
->flags
);
1602 dump((struct isl_basic_map
*)bset
, out
, indent
);
1605 void isl_basic_map_dump(struct isl_basic_map
*bmap
, FILE *out
, int indent
)
1608 fprintf(out
, "null basic map\n");
1612 fprintf(out
, "%*s", indent
, "");
1613 fprintf(out
, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
1614 "flags: %x, n_name: %d\n",
1616 bmap
->dim
->nparam
, bmap
->dim
->n_in
, bmap
->dim
->n_out
,
1617 bmap
->extra
, bmap
->flags
, bmap
->dim
->n_name
);
1618 dump(bmap
, out
, indent
);
1621 int isl_inequality_negate(struct isl_basic_map
*bmap
, unsigned pos
)
1626 total
= isl_basic_map_total_dim(bmap
);
1627 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
1628 isl_seq_neg(bmap
->ineq
[pos
], bmap
->ineq
[pos
], 1 + total
);
1629 isl_int_sub_ui(bmap
->ineq
[pos
][0], bmap
->ineq
[pos
][0], 1);
1630 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1634 struct isl_set
*isl_set_alloc_dim(struct isl_dim
*dim
, int n
, unsigned flags
)
1636 struct isl_set
*set
;
1640 isl_assert(dim
->ctx
, dim
->n_in
== 0, return NULL
);
1641 isl_assert(dim
->ctx
, n
>= 0, return NULL
);
1642 set
= isl_alloc(dim
->ctx
, struct isl_set
,
1643 sizeof(struct isl_set
) +
1644 (n
- 1) * sizeof(struct isl_basic_set
*));
1648 set
->ctx
= dim
->ctx
;
1649 isl_ctx_ref(set
->ctx
);
1661 struct isl_set
*isl_set_alloc(struct isl_ctx
*ctx
,
1662 unsigned nparam
, unsigned dim
, int n
, unsigned flags
)
1664 struct isl_set
*set
;
1665 struct isl_dim
*dims
;
1667 dims
= isl_dim_alloc(ctx
, nparam
, 0, dim
);
1671 set
= isl_set_alloc_dim(dims
, n
, flags
);
1675 /* Make sure "map" has room for at least "n" more basic maps.
1677 struct isl_map
*isl_map_grow(struct isl_map
*map
, int n
)
1680 struct isl_map
*grown
= NULL
;
1684 isl_assert(map
->ctx
, n
>= 0, goto error
);
1685 if (map
->n
+ n
<= map
->size
)
1687 grown
= isl_map_alloc_dim(isl_map_get_dim(map
), map
->n
+ n
, map
->flags
);
1690 for (i
= 0; i
< map
->n
; ++i
) {
1691 grown
->p
[i
] = isl_basic_map_copy(map
->p
[i
]);
1699 isl_map_free(grown
);
1704 /* Make sure "set" has room for at least "n" more basic sets.
1706 struct isl_set
*isl_set_grow(struct isl_set
*set
, int n
)
1708 return (struct isl_set
*)isl_map_grow((struct isl_map
*)set
, n
);
1711 struct isl_set
*isl_set_dup(struct isl_set
*set
)
1714 struct isl_set
*dup
;
1719 dup
= isl_set_alloc_dim(isl_dim_copy(set
->dim
), set
->n
, set
->flags
);
1722 for (i
= 0; i
< set
->n
; ++i
)
1723 dup
= isl_set_add_basic_set(dup
, isl_basic_set_copy(set
->p
[i
]));
1727 struct isl_set
*isl_set_from_basic_set(struct isl_basic_set
*bset
)
1729 struct isl_set
*set
;
1734 set
= isl_set_alloc_dim(isl_dim_copy(bset
->dim
), 1, ISL_MAP_DISJOINT
);
1736 isl_basic_set_free(bset
);
1739 return isl_set_add_basic_set(set
, bset
);
1742 struct isl_map
*isl_map_from_basic_map(struct isl_basic_map
*bmap
)
1744 struct isl_map
*map
;
1749 map
= isl_map_alloc_dim(isl_dim_copy(bmap
->dim
), 1, ISL_MAP_DISJOINT
);
1751 isl_basic_map_free(bmap
);
1754 return isl_map_add_basic_map(map
, bmap
);
1757 __isl_give isl_set
*isl_set_add_basic_set(__isl_take isl_set
*set
,
1758 __isl_take isl_basic_set
*bset
)
1760 return (struct isl_set
*)isl_map_add_basic_map((struct isl_map
*)set
,
1761 (struct isl_basic_map
*)bset
);
1764 void isl_set_free(struct isl_set
*set
)
1774 isl_ctx_deref(set
->ctx
);
1775 for (i
= 0; i
< set
->n
; ++i
)
1776 isl_basic_set_free(set
->p
[i
]);
1777 isl_dim_free(set
->dim
);
1781 void isl_set_dump(struct isl_set
*set
, FILE *out
, int indent
)
1786 fprintf(out
, "null set\n");
1790 fprintf(out
, "%*s", indent
, "");
1791 fprintf(out
, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
1792 set
->ref
, set
->n
, set
->dim
->nparam
, set
->dim
->n_out
,
1794 for (i
= 0; i
< set
->n
; ++i
) {
1795 fprintf(out
, "%*s", indent
, "");
1796 fprintf(out
, "basic set %d:\n", i
);
1797 isl_basic_set_dump(set
->p
[i
], out
, indent
+4);
1801 void isl_map_dump(struct isl_map
*map
, FILE *out
, int indent
)
1806 fprintf(out
, "null map\n");
1810 fprintf(out
, "%*s", indent
, "");
1811 fprintf(out
, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
1812 "flags: %x, n_name: %d\n",
1813 map
->ref
, map
->n
, map
->dim
->nparam
, map
->dim
->n_in
,
1814 map
->dim
->n_out
, map
->flags
, map
->dim
->n_name
);
1815 for (i
= 0; i
< map
->n
; ++i
) {
1816 fprintf(out
, "%*s", indent
, "");
1817 fprintf(out
, "basic map %d:\n", i
);
1818 isl_basic_map_dump(map
->p
[i
], out
, indent
+4);
1822 struct isl_basic_map
*isl_basic_map_intersect_domain(
1823 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
1825 struct isl_basic_map
*bmap_domain
;
1826 struct isl_dim
*dim
;
1831 isl_assert(bset
->ctx
, isl_dim_match(bmap
->dim
, isl_dim_param
,
1832 bset
->dim
, isl_dim_param
), goto error
);
1834 if (isl_dim_size(bset
->dim
, isl_dim_set
) != 0)
1835 isl_assert(bset
->ctx
,
1836 isl_basic_map_compatible_domain(bmap
, bset
), goto error
);
1838 bmap
= isl_basic_map_cow(bmap
);
1841 bmap
= isl_basic_map_extend_dim(bmap
, isl_dim_copy(bmap
->dim
),
1842 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
1843 dim
= isl_dim_reverse(isl_dim_copy(bset
->dim
));
1844 bmap_domain
= isl_basic_map_from_basic_set(bset
, dim
);
1845 bmap
= add_constraints(bmap
, bmap_domain
, 0, 0);
1847 bmap
= isl_basic_map_simplify(bmap
);
1848 return isl_basic_map_finalize(bmap
);
1850 isl_basic_map_free(bmap
);
1851 isl_basic_set_free(bset
);
1855 struct isl_basic_map
*isl_basic_map_intersect_range(
1856 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
1858 struct isl_basic_map
*bmap_range
;
1863 isl_assert(bset
->ctx
, isl_dim_match(bmap
->dim
, isl_dim_param
,
1864 bset
->dim
, isl_dim_param
), goto error
);
1866 if (isl_dim_size(bset
->dim
, isl_dim_set
) != 0)
1867 isl_assert(bset
->ctx
,
1868 isl_basic_map_compatible_range(bmap
, bset
), goto error
);
1870 bmap
= isl_basic_map_cow(bmap
);
1873 bmap
= isl_basic_map_extend_dim(bmap
, isl_dim_copy(bmap
->dim
),
1874 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
1875 bmap_range
= isl_basic_map_from_basic_set(bset
, isl_dim_copy(bset
->dim
));
1876 bmap
= add_constraints(bmap
, bmap_range
, 0, 0);
1878 bmap
= isl_basic_map_simplify(bmap
);
1879 return isl_basic_map_finalize(bmap
);
1881 isl_basic_map_free(bmap
);
1882 isl_basic_set_free(bset
);
1886 int isl_basic_map_contains(struct isl_basic_map
*bmap
, struct isl_vec
*vec
)
1892 total
= 1 + isl_basic_map_total_dim(bmap
);
1893 if (total
!= vec
->size
)
1898 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
1899 isl_seq_inner_product(vec
->el
, bmap
->eq
[i
], total
, &s
);
1900 if (!isl_int_is_zero(s
)) {
1906 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
1907 isl_seq_inner_product(vec
->el
, bmap
->ineq
[i
], total
, &s
);
1908 if (isl_int_is_neg(s
)) {
1919 int isl_basic_set_contains(struct isl_basic_set
*bset
, struct isl_vec
*vec
)
1921 return isl_basic_map_contains((struct isl_basic_map
*)bset
, vec
);
1924 struct isl_basic_map
*isl_basic_map_intersect(
1925 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
1927 struct isl_vec
*sample
= NULL
;
1929 if (!bmap1
|| !bmap2
)
1932 isl_assert(bmap1
->ctx
, isl_dim_match(bmap1
->dim
, isl_dim_param
,
1933 bmap2
->dim
, isl_dim_param
), goto error
);
1934 if (isl_dim_total(bmap1
->dim
) ==
1935 isl_dim_size(bmap1
->dim
, isl_dim_param
) &&
1936 isl_dim_total(bmap2
->dim
) !=
1937 isl_dim_size(bmap2
->dim
, isl_dim_param
))
1938 return isl_basic_map_intersect(bmap2
, bmap1
);
1940 if (isl_dim_total(bmap2
->dim
) !=
1941 isl_dim_size(bmap2
->dim
, isl_dim_param
))
1942 isl_assert(bmap1
->ctx
,
1943 isl_dim_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
1945 if (bmap1
->sample
&&
1946 isl_basic_map_contains(bmap1
, bmap1
->sample
) > 0 &&
1947 isl_basic_map_contains(bmap2
, bmap1
->sample
) > 0)
1948 sample
= isl_vec_copy(bmap1
->sample
);
1949 else if (bmap2
->sample
&&
1950 isl_basic_map_contains(bmap1
, bmap2
->sample
) > 0 &&
1951 isl_basic_map_contains(bmap2
, bmap2
->sample
) > 0)
1952 sample
= isl_vec_copy(bmap2
->sample
);
1954 bmap1
= isl_basic_map_cow(bmap1
);
1957 bmap1
= isl_basic_map_extend_dim(bmap1
, isl_dim_copy(bmap1
->dim
),
1958 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
1959 bmap1
= add_constraints(bmap1
, bmap2
, 0, 0);
1962 isl_vec_free(sample
);
1964 isl_vec_free(bmap1
->sample
);
1965 bmap1
->sample
= sample
;
1968 bmap1
= isl_basic_map_simplify(bmap1
);
1969 return isl_basic_map_finalize(bmap1
);
1972 isl_vec_free(sample
);
1973 isl_basic_map_free(bmap1
);
1974 isl_basic_map_free(bmap2
);
1978 struct isl_basic_set
*isl_basic_set_intersect(
1979 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
1981 return (struct isl_basic_set
*)
1982 isl_basic_map_intersect(
1983 (struct isl_basic_map
*)bset1
,
1984 (struct isl_basic_map
*)bset2
);
1987 /* Special case of isl_map_intersect, where both map1 and map2
1988 * are convex, without any divs and such that either map1 or map2
1989 * contains a single constraint. This constraint is then simply
1990 * added to the other map.
1992 static __isl_give isl_map
*map_intersect_add_constraint(
1993 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
1995 struct isl_basic_map
*bmap1
;
1996 struct isl_basic_map
*bmap2
;
1998 isl_assert(map1
->ctx
, map1
->n
== 1, goto error
);
1999 isl_assert(map2
->ctx
, map1
->n
== 1, goto error
);
2000 isl_assert(map1
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
2001 isl_assert(map2
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
2003 if (map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
!= 1)
2004 return isl_map_intersect(map2
, map1
);
2006 isl_assert(map2
->ctx
,
2007 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1, goto error
);
2009 map1
= isl_map_cow(map1
);
2012 if (isl_map_fast_is_empty(map1
)) {
2016 map1
->p
[0] = isl_basic_map_cow(map1
->p
[0]);
2017 if (map2
->p
[0]->n_eq
== 1)
2018 map1
->p
[0] = isl_basic_map_add_eq(map1
->p
[0], map2
->p
[0]->eq
[0]);
2020 map1
->p
[0] = isl_basic_map_add_ineq(map1
->p
[0],
2021 map2
->p
[0]->ineq
[0]);
2023 map1
->p
[0] = isl_basic_map_simplify(map1
->p
[0]);
2024 map1
->p
[0] = isl_basic_map_finalize(map1
->p
[0]);
2028 if (isl_basic_map_fast_is_empty(map1
->p
[0])) {
2029 isl_basic_map_free(map1
->p
[0]);
2042 struct isl_map
*isl_map_intersect(struct isl_map
*map1
, struct isl_map
*map2
)
2045 struct isl_map
*result
;
2051 if (isl_map_fast_is_empty(map1
)) {
2055 if (isl_map_fast_is_empty(map2
)) {
2060 if (map1
->n
== 1 && map2
->n
== 1 &&
2061 map1
->p
[0]->n_div
== 0 && map2
->p
[0]->n_div
== 0 &&
2062 isl_dim_equal(map1
->dim
, map2
->dim
) &&
2063 (map1
->p
[0]->n_eq
+ map1
->p
[0]->n_ineq
== 1 ||
2064 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1))
2065 return map_intersect_add_constraint(map1
, map2
);
2066 isl_assert(map1
->ctx
, isl_dim_match(map1
->dim
, isl_dim_param
,
2067 map2
->dim
, isl_dim_param
), goto error
);
2068 if (isl_dim_total(map1
->dim
) ==
2069 isl_dim_size(map1
->dim
, isl_dim_param
) &&
2070 isl_dim_total(map2
->dim
) != isl_dim_size(map2
->dim
, isl_dim_param
))
2071 return isl_map_intersect(map2
, map1
);
2073 if (isl_dim_total(map2
->dim
) != isl_dim_size(map2
->dim
, isl_dim_param
))
2074 isl_assert(map1
->ctx
,
2075 isl_dim_equal(map1
->dim
, map2
->dim
), goto error
);
2077 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
2078 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
2079 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
2081 result
= isl_map_alloc_dim(isl_dim_copy(map1
->dim
),
2082 map1
->n
* map2
->n
, flags
);
2085 for (i
= 0; i
< map1
->n
; ++i
)
2086 for (j
= 0; j
< map2
->n
; ++j
) {
2087 struct isl_basic_map
*part
;
2088 part
= isl_basic_map_intersect(
2089 isl_basic_map_copy(map1
->p
[i
]),
2090 isl_basic_map_copy(map2
->p
[j
]));
2091 if (isl_basic_map_is_empty(part
))
2092 isl_basic_map_free(part
);
2094 result
= isl_map_add_basic_map(result
, part
);
2107 struct isl_set
*isl_set_intersect(struct isl_set
*set1
, struct isl_set
*set2
)
2109 return (struct isl_set
*)
2110 isl_map_intersect((struct isl_map
*)set1
,
2111 (struct isl_map
*)set2
);
2114 struct isl_basic_map
*isl_basic_map_reverse(struct isl_basic_map
*bmap
)
2116 struct isl_dim
*dim
;
2117 struct isl_basic_set
*bset
;
2122 bmap
= isl_basic_map_cow(bmap
);
2125 dim
= isl_dim_reverse(isl_dim_copy(bmap
->dim
));
2126 in
= isl_basic_map_n_in(bmap
);
2127 bset
= isl_basic_set_from_basic_map(bmap
);
2128 bset
= isl_basic_set_swap_vars(bset
, in
);
2129 return isl_basic_map_from_basic_set(bset
, dim
);
2132 __isl_give isl_basic_map
*isl_basic_map_insert(__isl_take isl_basic_map
*bmap
,
2133 enum isl_dim_type type
, unsigned pos
, unsigned n
)
2135 struct isl_dim
*res_dim
;
2136 struct isl_basic_map
*res
;
2137 struct isl_dim_map
*dim_map
;
2138 unsigned total
, off
;
2139 enum isl_dim_type t
;
2147 res_dim
= isl_dim_insert(isl_basic_map_get_dim(bmap
), type
, pos
, n
);
2149 total
= isl_basic_map_total_dim(bmap
) + n
;
2150 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
2152 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
2154 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
2156 unsigned size
= isl_basic_map_dim(bmap
, t
);
2157 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2159 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2160 pos
, size
- pos
, off
+ pos
+ n
);
2162 off
+= isl_dim_size(res_dim
, t
);
2164 isl_dim_map_div(dim_map
, bmap
, off
);
2166 res
= isl_basic_map_alloc_dim(res_dim
,
2167 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
2168 res
= add_constraints_dim_map(res
, bmap
, dim_map
);
2169 res
= isl_basic_map_simplify(res
);
2170 return isl_basic_map_finalize(res
);
2173 __isl_give isl_basic_map
*isl_basic_map_add(__isl_take isl_basic_map
*bmap
,
2174 enum isl_dim_type type
, unsigned n
)
2178 return isl_basic_map_insert(bmap
, type
,
2179 isl_basic_map_dim(bmap
, type
), n
);
2182 __isl_give isl_basic_set
*isl_basic_set_add(__isl_take isl_basic_set
*bset
,
2183 enum isl_dim_type type
, unsigned n
)
2187 isl_assert(bset
->ctx
, type
!= isl_dim_in
, goto error
);
2188 return (isl_basic_set
*)isl_basic_map_add((isl_basic_map
*)bset
, type
, n
);
2190 isl_basic_set_free(bset
);
2194 __isl_give isl_map
*isl_map_insert(__isl_take isl_map
*map
,
2195 enum isl_dim_type type
, unsigned pos
, unsigned n
)
2202 map
= isl_map_cow(map
);
2206 map
->dim
= isl_dim_insert(map
->dim
, type
, pos
, n
);
2210 for (i
= 0; i
< map
->n
; ++i
) {
2211 map
->p
[i
] = isl_basic_map_insert(map
->p
[i
], type
, pos
, n
);
2222 __isl_give isl_set
*isl_set_insert(__isl_take isl_set
*set
,
2223 enum isl_dim_type type
, unsigned pos
, unsigned n
)
2225 return (isl_set
*)isl_map_insert((isl_map
*)set
, type
, pos
, n
);
2228 __isl_give isl_map
*isl_map_add(__isl_take isl_map
*map
,
2229 enum isl_dim_type type
, unsigned n
)
2233 return isl_map_insert(map
, type
, isl_map_dim(map
, type
), n
);
2236 __isl_give isl_set
*isl_set_add(__isl_take isl_set
*set
,
2237 enum isl_dim_type type
, unsigned n
)
2241 isl_assert(set
->ctx
, type
!= isl_dim_in
, goto error
);
2242 return (isl_set
*)isl_map_add((isl_map
*)set
, type
, n
);
2248 __isl_give isl_basic_map
*isl_basic_map_move_dims(
2249 __isl_take isl_basic_map
*bmap
,
2250 enum isl_dim_type dst_type
, unsigned dst_pos
,
2251 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2254 struct isl_dim_map
*dim_map
;
2255 struct isl_basic_map
*res
;
2256 enum isl_dim_type t
;
2257 unsigned total
, off
;
2264 isl_assert(bmap
->ctx
, src_pos
+ n
<= isl_basic_map_dim(bmap
, src_type
),
2267 if (dst_type
== src_type
&& dst_pos
== src_pos
)
2270 isl_assert(bmap
->ctx
, dst_type
!= src_type
, goto error
);
2272 if (pos(bmap
->dim
, dst_type
) + dst_pos
==
2273 pos(bmap
->dim
, src_type
) + src_pos
+
2274 ((src_type
< dst_type
) ? n
: 0)) {
2275 bmap
= isl_basic_map_cow(bmap
);
2279 bmap
->dim
= isl_dim_move(bmap
->dim
, dst_type
, dst_pos
,
2280 src_type
, src_pos
, n
);
2284 bmap
= isl_basic_map_finalize(bmap
);
2289 total
= isl_basic_map_total_dim(bmap
);
2290 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
2293 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
2294 unsigned size
= isl_dim_size(bmap
->dim
, t
);
2295 if (t
== dst_type
) {
2296 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2299 isl_dim_map_dim_range(dim_map
, bmap
->dim
, src_type
,
2302 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2303 dst_pos
, size
- dst_pos
, off
);
2304 off
+= size
- dst_pos
;
2305 } else if (t
== src_type
) {
2306 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2309 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2310 src_pos
+ n
, size
- src_pos
- n
, off
);
2311 off
+= size
- src_pos
- n
;
2313 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
2317 isl_dim_map_div(dim_map
, bmap
, off
);
2319 res
= isl_basic_map_alloc_dim(isl_basic_map_get_dim(bmap
),
2320 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
2321 bmap
= add_constraints_dim_map(res
, bmap
, dim_map
);
2323 bmap
->dim
= isl_dim_move(bmap
->dim
, dst_type
, dst_pos
,
2324 src_type
, src_pos
, n
);
2328 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
2329 bmap
= isl_basic_map_gauss(bmap
, NULL
);
2330 bmap
= isl_basic_map_finalize(bmap
);
2334 isl_basic_map_free(bmap
);
2338 __isl_give isl_basic_set
*isl_basic_set_move_dims(__isl_take isl_basic_set
*bset
,
2339 enum isl_dim_type dst_type
, unsigned dst_pos
,
2340 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2342 return (isl_basic_set
*)isl_basic_map_move_dims(
2343 (isl_basic_map
*)bset
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
2346 __isl_give isl_set
*isl_set_move_dims(__isl_take isl_set
*set
,
2347 enum isl_dim_type dst_type
, unsigned dst_pos
,
2348 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2352 isl_assert(set
->ctx
, dst_type
!= isl_dim_in
, goto error
);
2353 return (isl_set
*)isl_map_move_dims((isl_map
*)set
, dst_type
, dst_pos
,
2354 src_type
, src_pos
, n
);
2360 __isl_give isl_map
*isl_map_move_dims(__isl_take isl_map
*map
,
2361 enum isl_dim_type dst_type
, unsigned dst_pos
,
2362 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2371 isl_assert(map
->ctx
, src_pos
+ n
<= isl_map_dim(map
, src_type
),
2374 if (dst_type
== src_type
&& dst_pos
== src_pos
)
2377 isl_assert(map
->ctx
, dst_type
!= src_type
, goto error
);
2379 map
= isl_map_cow(map
);
2383 map
->dim
= isl_dim_move(map
->dim
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
2387 for (i
= 0; i
< map
->n
; ++i
) {
2388 map
->p
[i
] = isl_basic_map_move_dims(map
->p
[i
],
2390 src_type
, src_pos
, n
);
2401 /* Move the specified dimensions to the last columns right before
2402 * the divs. Don't change the dimension specification of bmap.
2403 * That's the responsibility of the caller.
2405 static __isl_give isl_basic_map
*move_last(__isl_take isl_basic_map
*bmap
,
2406 enum isl_dim_type type
, unsigned first
, unsigned n
)
2408 struct isl_dim_map
*dim_map
;
2409 struct isl_basic_map
*res
;
2410 enum isl_dim_type t
;
2411 unsigned total
, off
;
2415 if (pos(bmap
->dim
, type
) + first
+ n
== 1 + isl_dim_total(bmap
->dim
))
2418 total
= isl_basic_map_total_dim(bmap
);
2419 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
2422 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
2423 unsigned size
= isl_dim_size(bmap
->dim
, t
);
2425 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2428 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2429 first
, n
, total
- bmap
->n_div
- n
);
2430 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2431 first
+ n
, size
- (first
+ n
), off
);
2432 off
+= size
- (first
+ n
);
2434 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
2438 isl_dim_map_div(dim_map
, bmap
, off
+ n
);
2440 res
= isl_basic_map_alloc_dim(isl_basic_map_get_dim(bmap
),
2441 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
2442 res
= add_constraints_dim_map(res
, bmap
, dim_map
);
2446 /* Turn the n dimensions of type type, starting at first
2447 * into existentially quantified variables.
2449 __isl_give isl_basic_map
*isl_basic_map_project_out(
2450 __isl_take isl_basic_map
*bmap
,
2451 enum isl_dim_type type
, unsigned first
, unsigned n
)
2464 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
2465 return isl_basic_map_remove(bmap
, type
, first
, n
);
2467 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
2470 bmap
= move_last(bmap
, type
, first
, n
);
2471 bmap
= isl_basic_map_cow(bmap
);
2475 row_size
= 1 + isl_dim_total(bmap
->dim
) + bmap
->extra
;
2476 old
= bmap
->block2
.data
;
2477 bmap
->block2
= isl_blk_extend(bmap
->ctx
, bmap
->block2
,
2478 (bmap
->extra
+ n
) * (1 + row_size
));
2479 if (!bmap
->block2
.data
)
2481 new_div
= isl_alloc_array(bmap
->ctx
, isl_int
*, bmap
->extra
+ n
);
2484 for (i
= 0; i
< n
; ++i
) {
2485 new_div
[i
] = bmap
->block2
.data
+
2486 (bmap
->extra
+ i
) * (1 + row_size
);
2487 isl_seq_clr(new_div
[i
], 1 + row_size
);
2489 for (i
= 0; i
< bmap
->extra
; ++i
)
2490 new_div
[n
+ i
] = bmap
->block2
.data
+ (bmap
->div
[i
] - old
);
2492 bmap
->div
= new_div
;
2496 bmap
->dim
= isl_dim_drop(bmap
->dim
, type
, first
, n
);
2499 bmap
= isl_basic_map_simplify(bmap
);
2500 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
2501 return isl_basic_map_finalize(bmap
);
2503 isl_basic_map_free(bmap
);
2507 /* Turn the n dimensions of type type, starting at first
2508 * into existentially quantified variables.
2510 struct isl_basic_set
*isl_basic_set_project_out(struct isl_basic_set
*bset
,
2511 enum isl_dim_type type
, unsigned first
, unsigned n
)
2513 return (isl_basic_set
*)isl_basic_map_project_out(
2514 (isl_basic_map
*)bset
, type
, first
, n
);
2517 /* Turn the n dimensions of type type, starting at first
2518 * into existentially quantified variables.
2520 __isl_give isl_map
*isl_map_project_out(__isl_take isl_map
*map
,
2521 enum isl_dim_type type
, unsigned first
, unsigned n
)
2531 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
2533 map
= isl_map_cow(map
);
2537 map
->dim
= isl_dim_drop(map
->dim
, type
, first
, n
);
2541 for (i
= 0; i
< map
->n
; ++i
) {
2542 map
->p
[i
] = isl_basic_map_project_out(map
->p
[i
], type
, first
, n
);
2553 /* Turn the n dimensions of type type, starting at first
2554 * into existentially quantified variables.
2556 __isl_give isl_set
*isl_set_project_out(__isl_take isl_set
*set
,
2557 enum isl_dim_type type
, unsigned first
, unsigned n
)
2559 return (isl_set
*)isl_map_project_out((isl_map
*)set
, type
, first
, n
);
2562 static struct isl_basic_map
*add_divs(struct isl_basic_map
*bmap
, unsigned n
)
2566 for (i
= 0; i
< n
; ++i
) {
2567 j
= isl_basic_map_alloc_div(bmap
);
2570 isl_seq_clr(bmap
->div
[j
], 1+1+isl_basic_map_total_dim(bmap
));
2574 isl_basic_map_free(bmap
);
2578 struct isl_basic_map
*isl_basic_map_apply_range(
2579 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
2581 struct isl_dim
*dim_result
= NULL
;
2582 struct isl_basic_map
*bmap
;
2583 unsigned n_in
, n_out
, n
, nparam
, total
, pos
;
2584 struct isl_dim_map
*dim_map1
, *dim_map2
;
2586 if (!bmap1
|| !bmap2
)
2589 dim_result
= isl_dim_join(isl_dim_copy(bmap1
->dim
),
2590 isl_dim_copy(bmap2
->dim
));
2592 n_in
= isl_basic_map_n_in(bmap1
);
2593 n_out
= isl_basic_map_n_out(bmap2
);
2594 n
= isl_basic_map_n_out(bmap1
);
2595 nparam
= isl_basic_map_n_param(bmap1
);
2597 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ n
;
2598 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
2599 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
2600 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
2601 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
2602 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
2603 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_in
);
2604 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_out
);
2605 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
2606 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
2607 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
2609 bmap
= isl_basic_map_alloc_dim(dim_result
,
2610 bmap1
->n_div
+ bmap2
->n_div
+ n
,
2611 bmap1
->n_eq
+ bmap2
->n_eq
,
2612 bmap1
->n_ineq
+ bmap2
->n_ineq
);
2613 bmap
= add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
2614 bmap
= add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
2615 bmap
= add_divs(bmap
, n
);
2616 bmap
= isl_basic_map_simplify(bmap
);
2617 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
2618 return isl_basic_map_finalize(bmap
);
2620 isl_basic_map_free(bmap1
);
2621 isl_basic_map_free(bmap2
);
2625 struct isl_basic_set
*isl_basic_set_apply(
2626 struct isl_basic_set
*bset
, struct isl_basic_map
*bmap
)
2631 isl_assert(bset
->ctx
, isl_basic_map_compatible_domain(bmap
, bset
),
2634 return (struct isl_basic_set
*)
2635 isl_basic_map_apply_range((struct isl_basic_map
*)bset
, bmap
);
2637 isl_basic_set_free(bset
);
2638 isl_basic_map_free(bmap
);
2642 struct isl_basic_map
*isl_basic_map_apply_domain(
2643 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
2645 if (!bmap1
|| !bmap2
)
2648 isl_assert(bmap1
->ctx
,
2649 isl_basic_map_n_in(bmap1
) == isl_basic_map_n_in(bmap2
), goto error
);
2650 isl_assert(bmap1
->ctx
,
2651 isl_basic_map_n_param(bmap1
) == isl_basic_map_n_param(bmap2
),
2654 bmap1
= isl_basic_map_reverse(bmap1
);
2655 bmap1
= isl_basic_map_apply_range(bmap1
, bmap2
);
2656 return isl_basic_map_reverse(bmap1
);
2658 isl_basic_map_free(bmap1
);
2659 isl_basic_map_free(bmap2
);
2663 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
2664 * A \cap B -> f(A) + f(B)
2666 struct isl_basic_map
*isl_basic_map_sum(
2667 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
2669 unsigned n_in
, n_out
, nparam
, total
, pos
;
2670 struct isl_basic_map
*bmap
= NULL
;
2671 struct isl_dim_map
*dim_map1
, *dim_map2
;
2674 if (!bmap1
|| !bmap2
)
2677 isl_assert(bmap1
->ctx
, isl_dim_equal(bmap1
->dim
, bmap2
->dim
),
2680 nparam
= isl_basic_map_n_param(bmap1
);
2681 n_in
= isl_basic_map_n_in(bmap1
);
2682 n_out
= isl_basic_map_n_out(bmap1
);
2684 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
;
2685 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
2686 dim_map2
= isl_dim_map_alloc(bmap2
->ctx
, total
);
2687 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
2688 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
);
2689 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
2690 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
2691 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_in
+ n_out
);
2692 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
2693 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
2694 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_out
);
2696 bmap
= isl_basic_map_alloc_dim(isl_dim_copy(bmap1
->dim
),
2697 bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
,
2698 bmap1
->n_eq
+ bmap2
->n_eq
+ n_out
,
2699 bmap1
->n_ineq
+ bmap2
->n_ineq
);
2700 for (i
= 0; i
< n_out
; ++i
) {
2701 int j
= isl_basic_map_alloc_equality(bmap
);
2704 isl_seq_clr(bmap
->eq
[j
], 1+total
);
2705 isl_int_set_si(bmap
->eq
[j
][1+nparam
+n_in
+i
], -1);
2706 isl_int_set_si(bmap
->eq
[j
][1+pos
+i
], 1);
2707 isl_int_set_si(bmap
->eq
[j
][1+pos
-n_out
+i
], 1);
2709 bmap
= add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
2710 bmap
= add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
2711 bmap
= add_divs(bmap
, 2 * n_out
);
2713 bmap
= isl_basic_map_simplify(bmap
);
2714 return isl_basic_map_finalize(bmap
);
2716 isl_basic_map_free(bmap
);
2717 isl_basic_map_free(bmap1
);
2718 isl_basic_map_free(bmap2
);
2722 /* Given two maps A -> f(A) and B -> g(B), construct a map
2723 * A \cap B -> f(A) + f(B)
2725 struct isl_map
*isl_map_sum(struct isl_map
*map1
, struct isl_map
*map2
)
2727 struct isl_map
*result
;
2733 isl_assert(map1
->ctx
, isl_dim_equal(map1
->dim
, map2
->dim
), goto error
);
2735 result
= isl_map_alloc_dim(isl_dim_copy(map1
->dim
),
2736 map1
->n
* map2
->n
, 0);
2739 for (i
= 0; i
< map1
->n
; ++i
)
2740 for (j
= 0; j
< map2
->n
; ++j
) {
2741 struct isl_basic_map
*part
;
2742 part
= isl_basic_map_sum(
2743 isl_basic_map_copy(map1
->p
[i
]),
2744 isl_basic_map_copy(map2
->p
[j
]));
2745 if (isl_basic_map_is_empty(part
))
2746 isl_basic_map_free(part
);
2748 result
= isl_map_add_basic_map(result
, part
);
2761 __isl_give isl_set
*isl_set_sum(__isl_take isl_set
*set1
,
2762 __isl_take isl_set
*set2
)
2764 return (isl_set
*)isl_map_sum((isl_map
*)set1
, (isl_map
*)set2
);
2767 /* Given a basic map A -> f(A), construct A -> -f(A).
2769 struct isl_basic_map
*isl_basic_map_neg(struct isl_basic_map
*bmap
)
2774 bmap
= isl_basic_map_cow(bmap
);
2778 n
= isl_basic_map_dim(bmap
, isl_dim_out
);
2779 off
= isl_basic_map_offset(bmap
, isl_dim_out
);
2780 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2781 for (j
= 0; j
< n
; ++j
)
2782 isl_int_neg(bmap
->eq
[i
][off
+j
], bmap
->eq
[i
][off
+j
]);
2783 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2784 for (j
= 0; j
< n
; ++j
)
2785 isl_int_neg(bmap
->ineq
[i
][off
+j
], bmap
->ineq
[i
][off
+j
]);
2786 for (i
= 0; i
< bmap
->n_div
; ++i
)
2787 for (j
= 0; j
< n
; ++j
)
2788 isl_int_neg(bmap
->div
[i
][1+off
+j
], bmap
->div
[i
][1+off
+j
]);
2789 return isl_basic_map_finalize(bmap
);
2792 /* Given a map A -> f(A), construct A -> -f(A).
2794 struct isl_map
*isl_map_neg(struct isl_map
*map
)
2798 map
= isl_map_cow(map
);
2802 for (i
= 0; i
< map
->n
; ++i
) {
2803 map
->p
[i
] = isl_basic_map_neg(map
->p
[i
]);
2814 __isl_give isl_set
*isl_set_neg(__isl_take isl_set
*set
)
2816 return (isl_set
*)isl_map_neg((isl_map
*)set
);
2819 /* Given a basic map A -> f(A) and an integer d, construct a basic map
2820 * A -> floor(f(A)/d).
2822 struct isl_basic_map
*isl_basic_map_floordiv(struct isl_basic_map
*bmap
,
2825 unsigned n_in
, n_out
, nparam
, total
, pos
;
2826 struct isl_basic_map
*result
= NULL
;
2827 struct isl_dim_map
*dim_map
;
2833 nparam
= isl_basic_map_n_param(bmap
);
2834 n_in
= isl_basic_map_n_in(bmap
);
2835 n_out
= isl_basic_map_n_out(bmap
);
2837 total
= nparam
+ n_in
+ n_out
+ bmap
->n_div
+ n_out
;
2838 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
2839 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_param
, pos
= 0);
2840 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_in
, pos
+= nparam
);
2841 isl_dim_map_div(dim_map
, bmap
, pos
+= n_in
+ n_out
);
2842 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_out
, pos
+= bmap
->n_div
);
2844 result
= isl_basic_map_alloc_dim(isl_dim_copy(bmap
->dim
),
2845 bmap
->n_div
+ n_out
,
2846 bmap
->n_eq
, bmap
->n_ineq
+ 2 * n_out
);
2847 result
= add_constraints_dim_map(result
, bmap
, dim_map
);
2848 result
= add_divs(result
, n_out
);
2849 for (i
= 0; i
< n_out
; ++i
) {
2851 j
= isl_basic_map_alloc_inequality(result
);
2854 isl_seq_clr(result
->ineq
[j
], 1+total
);
2855 isl_int_neg(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
2856 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], 1);
2857 j
= isl_basic_map_alloc_inequality(result
);
2860 isl_seq_clr(result
->ineq
[j
], 1+total
);
2861 isl_int_set(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
2862 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], -1);
2863 isl_int_sub_ui(result
->ineq
[j
][0], d
, 1);
2866 result
= isl_basic_map_simplify(result
);
2867 return isl_basic_map_finalize(result
);
2869 isl_basic_map_free(result
);
2873 /* Given a map A -> f(A) and an integer d, construct a map
2874 * A -> floor(f(A)/d).
2876 struct isl_map
*isl_map_floordiv(struct isl_map
*map
, isl_int d
)
2880 map
= isl_map_cow(map
);
2884 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
2885 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
2886 for (i
= 0; i
< map
->n
; ++i
) {
2887 map
->p
[i
] = isl_basic_map_floordiv(map
->p
[i
], d
);
2898 static struct isl_basic_map
*var_equal(struct isl_basic_map
*bmap
, unsigned pos
)
2904 i
= isl_basic_map_alloc_equality(bmap
);
2907 nparam
= isl_basic_map_n_param(bmap
);
2908 n_in
= isl_basic_map_n_in(bmap
);
2909 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
2910 isl_int_set_si(bmap
->eq
[i
][1+nparam
+pos
], -1);
2911 isl_int_set_si(bmap
->eq
[i
][1+nparam
+n_in
+pos
], 1);
2912 return isl_basic_map_finalize(bmap
);
2914 isl_basic_map_free(bmap
);
2918 /* Add a constraints to "bmap" expressing i_pos < o_pos
2920 static struct isl_basic_map
*var_less(struct isl_basic_map
*bmap
, unsigned pos
)
2926 i
= isl_basic_map_alloc_inequality(bmap
);
2929 nparam
= isl_basic_map_n_param(bmap
);
2930 n_in
= isl_basic_map_n_in(bmap
);
2931 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
2932 isl_int_set_si(bmap
->ineq
[i
][0], -1);
2933 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
2934 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
2935 return isl_basic_map_finalize(bmap
);
2937 isl_basic_map_free(bmap
);
2941 /* Add a constraint to "bmap" expressing i_pos <= o_pos
2943 static __isl_give isl_basic_map
*var_less_or_equal(
2944 __isl_take isl_basic_map
*bmap
, unsigned pos
)
2950 i
= isl_basic_map_alloc_inequality(bmap
);
2953 nparam
= isl_basic_map_n_param(bmap
);
2954 n_in
= isl_basic_map_n_in(bmap
);
2955 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
2956 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
2957 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
2958 return isl_basic_map_finalize(bmap
);
2960 isl_basic_map_free(bmap
);
2964 /* Add a constraints to "bmap" expressing i_pos > o_pos
2966 static struct isl_basic_map
*var_more(struct isl_basic_map
*bmap
, unsigned pos
)
2972 i
= isl_basic_map_alloc_inequality(bmap
);
2975 nparam
= isl_basic_map_n_param(bmap
);
2976 n_in
= isl_basic_map_n_in(bmap
);
2977 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
2978 isl_int_set_si(bmap
->ineq
[i
][0], -1);
2979 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
2980 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
2981 return isl_basic_map_finalize(bmap
);
2983 isl_basic_map_free(bmap
);
2987 /* Add a constraint to "bmap" expressing i_pos >= o_pos
2989 static __isl_give isl_basic_map
*var_more_or_equal(
2990 __isl_take isl_basic_map
*bmap
, unsigned pos
)
2996 i
= isl_basic_map_alloc_inequality(bmap
);
2999 nparam
= isl_basic_map_n_param(bmap
);
3000 n_in
= isl_basic_map_n_in(bmap
);
3001 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3002 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
3003 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
3004 return isl_basic_map_finalize(bmap
);
3006 isl_basic_map_free(bmap
);
3010 struct isl_basic_map
*isl_basic_map_equal(struct isl_dim
*dim
, unsigned n_equal
)
3013 struct isl_basic_map
*bmap
;
3014 bmap
= isl_basic_map_alloc_dim(dim
, 0, n_equal
, 0);
3017 for (i
= 0; i
< n_equal
&& bmap
; ++i
)
3018 bmap
= var_equal(bmap
, i
);
3019 return isl_basic_map_finalize(bmap
);
3022 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
3024 struct isl_basic_map
*isl_basic_map_less_at(struct isl_dim
*dim
, unsigned pos
)
3027 struct isl_basic_map
*bmap
;
3028 bmap
= isl_basic_map_alloc_dim(dim
, 0, pos
, 1);
3031 for (i
= 0; i
< pos
&& bmap
; ++i
)
3032 bmap
= var_equal(bmap
, i
);
3034 bmap
= var_less(bmap
, pos
);
3035 return isl_basic_map_finalize(bmap
);
3038 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
3040 __isl_give isl_basic_map
*isl_basic_map_less_or_equal_at(
3041 __isl_take isl_dim
*dim
, unsigned pos
)
3044 isl_basic_map
*bmap
;
3046 bmap
= isl_basic_map_alloc_dim(dim
, 0, pos
, 1);
3047 for (i
= 0; i
< pos
; ++i
)
3048 bmap
= var_equal(bmap
, i
);
3049 bmap
= var_less_or_equal(bmap
, pos
);
3050 return isl_basic_map_finalize(bmap
);
3053 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
3055 struct isl_basic_map
*isl_basic_map_more_at(struct isl_dim
*dim
, unsigned pos
)
3058 struct isl_basic_map
*bmap
;
3059 bmap
= isl_basic_map_alloc_dim(dim
, 0, pos
, 1);
3062 for (i
= 0; i
< pos
&& bmap
; ++i
)
3063 bmap
= var_equal(bmap
, i
);
3065 bmap
= var_more(bmap
, pos
);
3066 return isl_basic_map_finalize(bmap
);
3069 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
3071 __isl_give isl_basic_map
*isl_basic_map_more_or_equal_at(
3072 __isl_take isl_dim
*dim
, unsigned pos
)
3075 isl_basic_map
*bmap
;
3077 bmap
= isl_basic_map_alloc_dim(dim
, 0, pos
, 1);
3078 for (i
= 0; i
< pos
; ++i
)
3079 bmap
= var_equal(bmap
, i
);
3080 bmap
= var_more_or_equal(bmap
, pos
);
3081 return isl_basic_map_finalize(bmap
);
3084 static __isl_give isl_map
*map_lex_lte_first(__isl_take isl_dim
*dims
,
3085 unsigned n
, int equal
)
3087 struct isl_map
*map
;
3090 if (n
== 0 && equal
)
3091 return isl_map_universe(dims
);
3093 map
= isl_map_alloc_dim(isl_dim_copy(dims
), n
, ISL_MAP_DISJOINT
);
3095 for (i
= 0; i
+ 1 < n
; ++i
)
3096 map
= isl_map_add_basic_map(map
,
3097 isl_basic_map_less_at(isl_dim_copy(dims
), i
));
3100 map
= isl_map_add_basic_map(map
,
3101 isl_basic_map_less_or_equal_at(dims
, n
- 1));
3103 map
= isl_map_add_basic_map(map
,
3104 isl_basic_map_less_at(dims
, n
- 1));
3111 static __isl_give isl_map
*map_lex_lte(__isl_take isl_dim
*dims
, int equal
)
3115 return map_lex_lte_first(dims
, dims
->n_out
, equal
);
3118 __isl_give isl_map
*isl_map_lex_lt_first(__isl_take isl_dim
*dim
, unsigned n
)
3120 return map_lex_lte_first(dim
, n
, 0);
3123 __isl_give isl_map
*isl_map_lex_le_first(__isl_take isl_dim
*dim
, unsigned n
)
3125 return map_lex_lte_first(dim
, n
, 1);
3128 __isl_give isl_map
*isl_map_lex_lt(__isl_take isl_dim
*set_dim
)
3130 return map_lex_lte(isl_dim_map(set_dim
), 0);
3133 __isl_give isl_map
*isl_map_lex_le(__isl_take isl_dim
*set_dim
)
3135 return map_lex_lte(isl_dim_map(set_dim
), 1);
3138 static __isl_give isl_map
*map_lex_gte_first(__isl_take isl_dim
*dims
,
3139 unsigned n
, int equal
)
3141 struct isl_map
*map
;
3144 if (n
== 0 && equal
)
3145 return isl_map_universe(dims
);
3147 map
= isl_map_alloc_dim(isl_dim_copy(dims
), n
, ISL_MAP_DISJOINT
);
3149 for (i
= 0; i
+ 1 < n
; ++i
)
3150 map
= isl_map_add_basic_map(map
,
3151 isl_basic_map_more_at(isl_dim_copy(dims
), i
));
3154 map
= isl_map_add_basic_map(map
,
3155 isl_basic_map_more_or_equal_at(dims
, n
- 1));
3157 map
= isl_map_add_basic_map(map
,
3158 isl_basic_map_more_at(dims
, n
- 1));
3165 static __isl_give isl_map
*map_lex_gte(__isl_take isl_dim
*dims
, int equal
)
3169 return map_lex_gte_first(dims
, dims
->n_out
, equal
);
3172 __isl_give isl_map
*isl_map_lex_gt_first(__isl_take isl_dim
*dim
, unsigned n
)
3174 return map_lex_gte_first(dim
, n
, 0);
3177 __isl_give isl_map
*isl_map_lex_ge_first(__isl_take isl_dim
*dim
, unsigned n
)
3179 return map_lex_gte_first(dim
, n
, 1);
3182 __isl_give isl_map
*isl_map_lex_gt(__isl_take isl_dim
*set_dim
)
3184 return map_lex_gte(isl_dim_map(set_dim
), 0);
3187 __isl_give isl_map
*isl_map_lex_ge(__isl_take isl_dim
*set_dim
)
3189 return map_lex_gte(isl_dim_map(set_dim
), 1);
3192 struct isl_basic_map
*isl_basic_map_from_basic_set(
3193 struct isl_basic_set
*bset
, struct isl_dim
*dim
)
3195 struct isl_basic_map
*bmap
;
3197 bset
= isl_basic_set_cow(bset
);
3201 isl_assert(bset
->ctx
, isl_dim_compatible(bset
->dim
, dim
), goto error
);
3202 isl_dim_free(bset
->dim
);
3203 bmap
= (struct isl_basic_map
*) bset
;
3205 return isl_basic_map_finalize(bmap
);
3207 isl_basic_set_free(bset
);
3212 struct isl_basic_set
*isl_basic_set_from_basic_map(struct isl_basic_map
*bmap
)
3216 if (bmap
->dim
->n_in
== 0)
3217 return (struct isl_basic_set
*)bmap
;
3218 bmap
= isl_basic_map_cow(bmap
);
3221 bmap
->dim
= isl_dim_as_set_dim(bmap
->dim
);
3224 bmap
= isl_basic_map_finalize(bmap
);
3225 return (struct isl_basic_set
*)bmap
;
3227 isl_basic_map_free(bmap
);
3231 /* For a div d = floor(f/m), add the constraints
3234 * -(f-(n-1)) + m d >= 0
3236 * Note that the second constraint is the negation of
3240 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map
*bmap
,
3241 unsigned pos
, isl_int
*div
)
3244 unsigned total
= isl_basic_map_total_dim(bmap
);
3246 i
= isl_basic_map_alloc_inequality(bmap
);
3249 isl_seq_cpy(bmap
->ineq
[i
], div
+ 1, 1 + total
);
3250 isl_int_neg(bmap
->ineq
[i
][1 + pos
], div
[0]);
3252 j
= isl_basic_map_alloc_inequality(bmap
);
3255 isl_seq_neg(bmap
->ineq
[j
], bmap
->ineq
[i
], 1 + total
);
3256 isl_int_add(bmap
->ineq
[j
][0], bmap
->ineq
[j
][0], bmap
->ineq
[j
][1 + pos
]);
3257 isl_int_sub_ui(bmap
->ineq
[j
][0], bmap
->ineq
[j
][0], 1);
3261 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set
*bset
,
3262 unsigned pos
, isl_int
*div
)
3264 return isl_basic_map_add_div_constraints_var((isl_basic_map
*)bset
,
3268 int isl_basic_map_add_div_constraints(struct isl_basic_map
*bmap
, unsigned div
)
3270 unsigned total
= isl_basic_map_total_dim(bmap
);
3271 unsigned div_pos
= total
- bmap
->n_div
+ div
;
3273 return isl_basic_map_add_div_constraints_var(bmap
, div_pos
,
3277 struct isl_basic_set
*isl_basic_map_underlying_set(
3278 struct isl_basic_map
*bmap
)
3282 if (bmap
->dim
->nparam
== 0 && bmap
->dim
->n_in
== 0 &&
3283 bmap
->n_div
== 0 && !isl_dim_get_tuple_name(bmap
->dim
, isl_dim_out
))
3284 return (struct isl_basic_set
*)bmap
;
3285 bmap
= isl_basic_map_cow(bmap
);
3288 bmap
->dim
= isl_dim_underlying(bmap
->dim
, bmap
->n_div
);
3291 bmap
->extra
-= bmap
->n_div
;
3293 bmap
= isl_basic_map_finalize(bmap
);
3294 return (struct isl_basic_set
*)bmap
;
3299 __isl_give isl_basic_set
*isl_basic_set_underlying_set(
3300 __isl_take isl_basic_set
*bset
)
3302 return isl_basic_map_underlying_set((isl_basic_map
*)bset
);
3305 struct isl_basic_map
*isl_basic_map_overlying_set(
3306 struct isl_basic_set
*bset
, struct isl_basic_map
*like
)
3308 struct isl_basic_map
*bmap
;
3309 struct isl_ctx
*ctx
;
3316 isl_assert(ctx
, bset
->n_div
== 0, goto error
);
3317 isl_assert(ctx
, isl_basic_set_n_param(bset
) == 0, goto error
);
3318 isl_assert(ctx
, bset
->dim
->n_out
== isl_basic_map_total_dim(like
),
3320 if (isl_dim_equal(bset
->dim
, like
->dim
) && like
->n_div
== 0) {
3321 isl_basic_map_free(like
);
3322 return (struct isl_basic_map
*)bset
;
3324 bset
= isl_basic_set_cow(bset
);
3327 total
= bset
->dim
->n_out
+ bset
->extra
;
3328 bmap
= (struct isl_basic_map
*)bset
;
3329 isl_dim_free(bmap
->dim
);
3330 bmap
->dim
= isl_dim_copy(like
->dim
);
3333 bmap
->n_div
= like
->n_div
;
3334 bmap
->extra
+= like
->n_div
;
3337 ltotal
= total
- bmap
->extra
+ like
->extra
;
3340 bmap
->block2
= isl_blk_extend(ctx
, bmap
->block2
,
3341 bmap
->extra
* (1 + 1 + total
));
3342 if (isl_blk_is_error(bmap
->block2
))
3344 bmap
->div
= isl_realloc_array(ctx
, bmap
->div
, isl_int
*,
3348 for (i
= 0; i
< bmap
->extra
; ++i
)
3349 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + 1 + total
);
3350 for (i
= 0; i
< like
->n_div
; ++i
) {
3351 isl_seq_cpy(bmap
->div
[i
], like
->div
[i
], 1 + 1 + ltotal
);
3352 isl_seq_clr(bmap
->div
[i
]+1+1+ltotal
, total
- ltotal
);
3354 bmap
= isl_basic_map_extend_constraints(bmap
,
3355 0, 2 * like
->n_div
);
3356 for (i
= 0; i
< like
->n_div
; ++i
) {
3357 if (isl_int_is_zero(bmap
->div
[i
][0]))
3359 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
3363 isl_basic_map_free(like
);
3364 bmap
= isl_basic_map_simplify(bmap
);
3365 bmap
= isl_basic_map_finalize(bmap
);
3368 isl_basic_map_free(like
);
3369 isl_basic_set_free(bset
);
3373 struct isl_basic_set
*isl_basic_set_from_underlying_set(
3374 struct isl_basic_set
*bset
, struct isl_basic_set
*like
)
3376 return (struct isl_basic_set
*)
3377 isl_basic_map_overlying_set(bset
, (struct isl_basic_map
*)like
);
3380 struct isl_set
*isl_set_from_underlying_set(
3381 struct isl_set
*set
, struct isl_basic_set
*like
)
3387 isl_assert(set
->ctx
, set
->dim
->n_out
== isl_basic_set_total_dim(like
),
3389 if (isl_dim_equal(set
->dim
, like
->dim
) && like
->n_div
== 0) {
3390 isl_basic_set_free(like
);
3393 set
= isl_set_cow(set
);
3396 for (i
= 0; i
< set
->n
; ++i
) {
3397 set
->p
[i
] = isl_basic_set_from_underlying_set(set
->p
[i
],
3398 isl_basic_set_copy(like
));
3402 isl_dim_free(set
->dim
);
3403 set
->dim
= isl_dim_copy(like
->dim
);
3406 isl_basic_set_free(like
);
3409 isl_basic_set_free(like
);
3414 struct isl_set
*isl_map_underlying_set(struct isl_map
*map
)
3418 map
= isl_map_cow(map
);
3421 map
->dim
= isl_dim_cow(map
->dim
);
3425 for (i
= 1; i
< map
->n
; ++i
)
3426 isl_assert(map
->ctx
, map
->p
[0]->n_div
== map
->p
[i
]->n_div
,
3428 for (i
= 0; i
< map
->n
; ++i
) {
3429 map
->p
[i
] = (struct isl_basic_map
*)
3430 isl_basic_map_underlying_set(map
->p
[i
]);
3435 map
->dim
= isl_dim_underlying(map
->dim
, 0);
3437 isl_dim_free(map
->dim
);
3438 map
->dim
= isl_dim_copy(map
->p
[0]->dim
);
3442 return (struct isl_set
*)map
;
3448 struct isl_set
*isl_set_to_underlying_set(struct isl_set
*set
)
3450 return (struct isl_set
*)isl_map_underlying_set((struct isl_map
*)set
);
3453 static __isl_give isl_basic_map
*isl_basic_map_reset_dim(
3454 __isl_take isl_basic_map
*bmap
, __isl_take isl_dim
*dim
)
3456 bmap
= isl_basic_map_cow(bmap
);
3460 isl_dim_free(bmap
->dim
);
3463 bmap
= isl_basic_map_finalize(bmap
);
3467 isl_basic_map_free(bmap
);
3472 static __isl_give isl_basic_set
*isl_basic_set_reset_dim(
3473 __isl_take isl_basic_set
*bset
, __isl_take isl_dim
*dim
)
3475 return (isl_basic_set
*)isl_basic_map_reset_dim((isl_basic_map
*)bset
,
3479 __isl_give isl_map
*isl_map_reset_dim(__isl_take isl_map
*map
,
3480 __isl_take isl_dim
*dim
)
3484 map
= isl_map_cow(map
);
3488 for (i
= 0; i
< map
->n
; ++i
) {
3489 map
->p
[i
] = isl_basic_map_reset_dim(map
->p
[i
],
3494 isl_dim_free(map
->dim
);
3504 __isl_give isl_set
*isl_set_reset_dim(__isl_take isl_set
*set
,
3505 __isl_take isl_dim
*dim
)
3507 return (struct isl_set
*) isl_map_reset_dim((struct isl_map
*)set
, dim
);
3510 struct isl_basic_set
*isl_basic_map_domain(struct isl_basic_map
*bmap
)
3513 struct isl_basic_set
*domain
;
3519 dim
= isl_dim_domain(isl_basic_map_get_dim(bmap
));
3521 n_in
= isl_basic_map_n_in(bmap
);
3522 n_out
= isl_basic_map_n_out(bmap
);
3523 domain
= isl_basic_set_from_basic_map(bmap
);
3524 domain
= isl_basic_set_project_out(domain
, isl_dim_set
, n_in
, n_out
);
3526 domain
= isl_basic_set_reset_dim(domain
, dim
);
3531 struct isl_basic_set
*isl_basic_map_range(struct isl_basic_map
*bmap
)
3533 return isl_basic_map_domain(isl_basic_map_reverse(bmap
));
3536 struct isl_set
*isl_map_range(struct isl_map
*map
)
3539 struct isl_set
*set
;
3543 if (isl_map_dim(map
, isl_dim_in
) == 0)
3544 return (isl_set
*)map
;
3546 map
= isl_map_cow(map
);
3550 set
= (struct isl_set
*) map
;
3551 set
->dim
= isl_dim_drop_inputs(set
->dim
, 0, set
->dim
->n_in
);
3554 for (i
= 0; i
< map
->n
; ++i
) {
3555 set
->p
[i
] = isl_basic_map_range(map
->p
[i
]);
3559 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
3560 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
3567 struct isl_map
*isl_map_from_set(struct isl_set
*set
, struct isl_dim
*dim
)
3570 struct isl_map
*map
= NULL
;
3572 set
= isl_set_cow(set
);
3575 isl_assert(set
->ctx
, isl_dim_compatible(set
->dim
, dim
), goto error
);
3576 map
= (struct isl_map
*)set
;
3577 for (i
= 0; i
< set
->n
; ++i
) {
3578 map
->p
[i
] = isl_basic_map_from_basic_set(
3579 set
->p
[i
], isl_dim_copy(dim
));
3583 isl_dim_free(map
->dim
);
3592 struct isl_map
*isl_map_from_range(struct isl_set
*set
)
3594 return (struct isl_map
*)set
;
3597 __isl_give isl_map
*isl_map_from_domain(__isl_take isl_set
*set
)
3599 return isl_map_reverse(isl_map_from_range(set
));
3602 __isl_give isl_map
*isl_map_from_domain_and_range(__isl_take isl_set
*domain
,
3603 __isl_take isl_set
*range
)
3605 return isl_map_apply_range(isl_map_from_domain(domain
),
3606 isl_map_from_range(range
));
3609 struct isl_set
*isl_set_from_map(struct isl_map
*map
)
3612 struct isl_set
*set
= NULL
;
3616 map
= isl_map_cow(map
);
3619 map
->dim
= isl_dim_as_set_dim(map
->dim
);
3622 set
= (struct isl_set
*)map
;
3623 for (i
= 0; i
< map
->n
; ++i
) {
3624 set
->p
[i
] = isl_basic_set_from_basic_map(map
->p
[i
]);
3634 struct isl_map
*isl_map_alloc_dim(struct isl_dim
*dim
, int n
, unsigned flags
)
3636 struct isl_map
*map
;
3640 isl_assert(dim
->ctx
, n
>= 0, return NULL
);
3641 map
= isl_alloc(dim
->ctx
, struct isl_map
,
3642 sizeof(struct isl_map
) +
3643 (n
- 1) * sizeof(struct isl_basic_map
*));
3647 map
->ctx
= dim
->ctx
;
3648 isl_ctx_ref(map
->ctx
);
3660 struct isl_map
*isl_map_alloc(struct isl_ctx
*ctx
,
3661 unsigned nparam
, unsigned in
, unsigned out
, int n
,
3664 struct isl_map
*map
;
3665 struct isl_dim
*dims
;
3667 dims
= isl_dim_alloc(ctx
, nparam
, in
, out
);
3671 map
= isl_map_alloc_dim(dims
, n
, flags
);
3675 struct isl_basic_map
*isl_basic_map_empty(struct isl_dim
*dim
)
3677 struct isl_basic_map
*bmap
;
3678 bmap
= isl_basic_map_alloc_dim(dim
, 0, 1, 0);
3679 bmap
= isl_basic_map_set_to_empty(bmap
);
3683 struct isl_basic_set
*isl_basic_set_empty(struct isl_dim
*dim
)
3685 struct isl_basic_set
*bset
;
3686 bset
= isl_basic_set_alloc_dim(dim
, 0, 1, 0);
3687 bset
= isl_basic_set_set_to_empty(bset
);
3691 struct isl_basic_map
*isl_basic_map_empty_like(struct isl_basic_map
*model
)
3693 struct isl_basic_map
*bmap
;
3696 bmap
= isl_basic_map_alloc_dim(isl_dim_copy(model
->dim
), 0, 1, 0);
3697 bmap
= isl_basic_map_set_to_empty(bmap
);
3701 struct isl_basic_map
*isl_basic_map_empty_like_map(struct isl_map
*model
)
3703 struct isl_basic_map
*bmap
;
3706 bmap
= isl_basic_map_alloc_dim(isl_dim_copy(model
->dim
), 0, 1, 0);
3707 bmap
= isl_basic_map_set_to_empty(bmap
);
3711 struct isl_basic_set
*isl_basic_set_empty_like(struct isl_basic_set
*model
)
3713 struct isl_basic_set
*bset
;
3716 bset
= isl_basic_set_alloc_dim(isl_dim_copy(model
->dim
), 0, 1, 0);
3717 bset
= isl_basic_set_set_to_empty(bset
);
3721 struct isl_basic_map
*isl_basic_map_universe(struct isl_dim
*dim
)
3723 struct isl_basic_map
*bmap
;
3724 bmap
= isl_basic_map_alloc_dim(dim
, 0, 0, 0);
3728 struct isl_basic_set
*isl_basic_set_universe(struct isl_dim
*dim
)
3730 struct isl_basic_set
*bset
;
3731 bset
= isl_basic_set_alloc_dim(dim
, 0, 0, 0);
3735 __isl_give isl_basic_map
*isl_basic_map_universe_like(
3736 __isl_keep isl_basic_map
*model
)
3740 return isl_basic_map_alloc_dim(isl_dim_copy(model
->dim
), 0, 0, 0);
3743 struct isl_basic_set
*isl_basic_set_universe_like(struct isl_basic_set
*model
)
3747 return isl_basic_set_alloc_dim(isl_dim_copy(model
->dim
), 0, 0, 0);
3750 __isl_give isl_basic_set
*isl_basic_set_universe_like_set(
3751 __isl_keep isl_set
*model
)
3755 return isl_basic_set_alloc_dim(isl_dim_copy(model
->dim
), 0, 0, 0);
3758 struct isl_map
*isl_map_empty(struct isl_dim
*dim
)
3760 return isl_map_alloc_dim(dim
, 0, ISL_MAP_DISJOINT
);
3763 struct isl_map
*isl_map_empty_like(struct isl_map
*model
)
3767 return isl_map_alloc_dim(isl_dim_copy(model
->dim
), 0, ISL_MAP_DISJOINT
);
3770 struct isl_map
*isl_map_empty_like_basic_map(struct isl_basic_map
*model
)
3774 return isl_map_alloc_dim(isl_dim_copy(model
->dim
), 0, ISL_MAP_DISJOINT
);
3777 struct isl_set
*isl_set_empty(struct isl_dim
*dim
)
3779 return isl_set_alloc_dim(dim
, 0, ISL_MAP_DISJOINT
);
3782 struct isl_set
*isl_set_empty_like(struct isl_set
*model
)
3786 return isl_set_empty(isl_dim_copy(model
->dim
));
3789 struct isl_map
*isl_map_universe(struct isl_dim
*dim
)
3791 struct isl_map
*map
;
3794 map
= isl_map_alloc_dim(isl_dim_copy(dim
), 1, ISL_MAP_DISJOINT
);
3795 map
= isl_map_add_basic_map(map
, isl_basic_map_universe(dim
));
3799 struct isl_set
*isl_set_universe(struct isl_dim
*dim
)
3801 struct isl_set
*set
;
3804 set
= isl_set_alloc_dim(isl_dim_copy(dim
), 1, ISL_MAP_DISJOINT
);
3805 set
= isl_set_add_basic_set(set
, isl_basic_set_universe(dim
));
3809 __isl_give isl_set
*isl_set_universe_like(__isl_keep isl_set
*model
)
3813 return isl_set_universe(isl_dim_copy(model
->dim
));
3816 struct isl_map
*isl_map_dup(struct isl_map
*map
)
3819 struct isl_map
*dup
;
3823 dup
= isl_map_alloc_dim(isl_dim_copy(map
->dim
), map
->n
, map
->flags
);
3824 for (i
= 0; i
< map
->n
; ++i
)
3825 dup
= isl_map_add_basic_map(dup
, isl_basic_map_copy(map
->p
[i
]));
3829 __isl_give isl_map
*isl_map_add_basic_map(__isl_take isl_map
*map
,
3830 __isl_take isl_basic_map
*bmap
)
3834 if (isl_basic_map_fast_is_empty(bmap
)) {
3835 isl_basic_map_free(bmap
);
3838 isl_assert(map
->ctx
, isl_dim_equal(map
->dim
, bmap
->dim
), goto error
);
3839 isl_assert(map
->ctx
, map
->n
< map
->size
, goto error
);
3840 map
->p
[map
->n
] = bmap
;
3842 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3848 isl_basic_map_free(bmap
);
3852 void isl_map_free(struct isl_map
*map
)
3862 isl_ctx_deref(map
->ctx
);
3863 for (i
= 0; i
< map
->n
; ++i
)
3864 isl_basic_map_free(map
->p
[i
]);
3865 isl_dim_free(map
->dim
);
3869 struct isl_map
*isl_map_extend(struct isl_map
*base
,
3870 unsigned nparam
, unsigned n_in
, unsigned n_out
)
3874 base
= isl_map_cow(base
);
3878 base
->dim
= isl_dim_extend(base
->dim
, nparam
, n_in
, n_out
);
3881 for (i
= 0; i
< base
->n
; ++i
) {
3882 base
->p
[i
] = isl_basic_map_extend_dim(base
->p
[i
],
3883 isl_dim_copy(base
->dim
), 0, 0, 0);
3893 struct isl_set
*isl_set_extend(struct isl_set
*base
,
3894 unsigned nparam
, unsigned dim
)
3896 return (struct isl_set
*)isl_map_extend((struct isl_map
*)base
,
3900 static struct isl_basic_map
*isl_basic_map_fix_pos_si(
3901 struct isl_basic_map
*bmap
, unsigned pos
, int value
)
3905 bmap
= isl_basic_map_cow(bmap
);
3906 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
3907 j
= isl_basic_map_alloc_equality(bmap
);
3910 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
3911 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
3912 isl_int_set_si(bmap
->eq
[j
][0], value
);
3913 bmap
= isl_basic_map_simplify(bmap
);
3914 return isl_basic_map_finalize(bmap
);
3916 isl_basic_map_free(bmap
);
3920 static __isl_give isl_basic_map
*isl_basic_map_fix_pos(
3921 __isl_take isl_basic_map
*bmap
, unsigned pos
, isl_int value
)
3925 bmap
= isl_basic_map_cow(bmap
);
3926 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
3927 j
= isl_basic_map_alloc_equality(bmap
);
3930 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
3931 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
3932 isl_int_set(bmap
->eq
[j
][0], value
);
3933 bmap
= isl_basic_map_simplify(bmap
);
3934 return isl_basic_map_finalize(bmap
);
3936 isl_basic_map_free(bmap
);
3940 struct isl_basic_map
*isl_basic_map_fix_si(struct isl_basic_map
*bmap
,
3941 enum isl_dim_type type
, unsigned pos
, int value
)
3945 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
3946 return isl_basic_map_fix_pos_si(bmap
,
3947 isl_basic_map_offset(bmap
, type
) + pos
, value
);
3949 isl_basic_map_free(bmap
);
3953 __isl_give isl_basic_map
*isl_basic_map_fix(__isl_take isl_basic_map
*bmap
,
3954 enum isl_dim_type type
, unsigned pos
, isl_int value
)
3958 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
3959 return isl_basic_map_fix_pos(bmap
,
3960 isl_basic_map_offset(bmap
, type
) + pos
, value
);
3962 isl_basic_map_free(bmap
);
3966 struct isl_basic_set
*isl_basic_set_fix_si(struct isl_basic_set
*bset
,
3967 enum isl_dim_type type
, unsigned pos
, int value
)
3969 return (struct isl_basic_set
*)
3970 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
3974 __isl_give isl_basic_set
*isl_basic_set_fix(__isl_take isl_basic_set
*bset
,
3975 enum isl_dim_type type
, unsigned pos
, isl_int value
)
3977 return (struct isl_basic_set
*)
3978 isl_basic_map_fix((struct isl_basic_map
*)bset
,
3982 struct isl_basic_map
*isl_basic_map_fix_input_si(struct isl_basic_map
*bmap
,
3983 unsigned input
, int value
)
3985 return isl_basic_map_fix_si(bmap
, isl_dim_in
, input
, value
);
3988 struct isl_basic_set
*isl_basic_set_fix_dim_si(struct isl_basic_set
*bset
,
3989 unsigned dim
, int value
)
3991 return (struct isl_basic_set
*)
3992 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
3993 isl_dim_set
, dim
, value
);
3996 struct isl_map
*isl_map_fix_si(struct isl_map
*map
,
3997 enum isl_dim_type type
, unsigned pos
, int value
)
4001 map
= isl_map_cow(map
);
4005 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
4006 for (i
= 0; i
< map
->n
; ++i
) {
4007 map
->p
[i
] = isl_basic_map_fix_si(map
->p
[i
], type
, pos
, value
);
4011 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4018 __isl_give isl_set
*isl_set_fix_si(__isl_take isl_set
*set
,
4019 enum isl_dim_type type
, unsigned pos
, int value
)
4021 return (struct isl_set
*)
4022 isl_map_fix_si((struct isl_map
*)set
, type
, pos
, value
);
4025 __isl_give isl_map
*isl_map_fix(__isl_take isl_map
*map
,
4026 enum isl_dim_type type
, unsigned pos
, isl_int value
)
4030 map
= isl_map_cow(map
);
4034 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
4035 for (i
= 0; i
< map
->n
; ++i
) {
4036 map
->p
[i
] = isl_basic_map_fix(map
->p
[i
], type
, pos
, value
);
4040 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4047 __isl_give isl_set
*isl_set_fix(__isl_take isl_set
*set
,
4048 enum isl_dim_type type
, unsigned pos
, isl_int value
)
4050 return (struct isl_set
*)isl_map_fix((isl_map
*)set
, type
, pos
, value
);
4053 struct isl_map
*isl_map_fix_input_si(struct isl_map
*map
,
4054 unsigned input
, int value
)
4056 return isl_map_fix_si(map
, isl_dim_in
, input
, value
);
4059 struct isl_set
*isl_set_fix_dim_si(struct isl_set
*set
, unsigned dim
, int value
)
4061 return (struct isl_set
*)
4062 isl_map_fix_si((struct isl_map
*)set
, isl_dim_set
, dim
, value
);
4065 __isl_give isl_basic_map
*isl_basic_map_lower_bound_si(
4066 __isl_take isl_basic_map
*bmap
,
4067 enum isl_dim_type type
, unsigned pos
, int value
)
4073 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
4074 pos
+= isl_basic_map_offset(bmap
, type
);
4075 bmap
= isl_basic_map_cow(bmap
);
4076 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
4077 j
= isl_basic_map_alloc_inequality(bmap
);
4080 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
4081 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
4082 isl_int_set_si(bmap
->ineq
[j
][0], -value
);
4083 bmap
= isl_basic_map_simplify(bmap
);
4084 return isl_basic_map_finalize(bmap
);
4086 isl_basic_map_free(bmap
);
4090 struct isl_basic_set
*isl_basic_set_lower_bound_dim(struct isl_basic_set
*bset
,
4091 unsigned dim
, isl_int value
)
4095 bset
= isl_basic_set_cow(bset
);
4096 bset
= isl_basic_set_extend_constraints(bset
, 0, 1);
4097 j
= isl_basic_set_alloc_inequality(bset
);
4100 isl_seq_clr(bset
->ineq
[j
], 1 + isl_basic_set_total_dim(bset
));
4101 isl_int_set_si(bset
->ineq
[j
][1 + isl_basic_set_n_param(bset
) + dim
], 1);
4102 isl_int_neg(bset
->ineq
[j
][0], value
);
4103 bset
= isl_basic_set_simplify(bset
);
4104 return isl_basic_set_finalize(bset
);
4106 isl_basic_set_free(bset
);
4110 __isl_give isl_map
*isl_map_lower_bound_si(__isl_take isl_map
*map
,
4111 enum isl_dim_type type
, unsigned pos
, int value
)
4115 map
= isl_map_cow(map
);
4119 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
4120 for (i
= 0; i
< map
->n
; ++i
) {
4121 map
->p
[i
] = isl_basic_map_lower_bound_si(map
->p
[i
],
4126 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4133 __isl_give isl_set
*isl_set_lower_bound_si(__isl_take isl_set
*set
,
4134 enum isl_dim_type type
, unsigned pos
, int value
)
4136 return (struct isl_set
*)
4137 isl_map_lower_bound_si((struct isl_map
*)set
, type
, pos
, value
);
4140 struct isl_set
*isl_set_lower_bound_dim(struct isl_set
*set
, unsigned dim
,
4145 set
= isl_set_cow(set
);
4149 isl_assert(set
->ctx
, dim
< isl_set_n_dim(set
), goto error
);
4150 for (i
= 0; i
< set
->n
; ++i
) {
4151 set
->p
[i
] = isl_basic_set_lower_bound_dim(set
->p
[i
], dim
, value
);
4161 struct isl_map
*isl_map_reverse(struct isl_map
*map
)
4165 map
= isl_map_cow(map
);
4169 map
->dim
= isl_dim_reverse(map
->dim
);
4172 for (i
= 0; i
< map
->n
; ++i
) {
4173 map
->p
[i
] = isl_basic_map_reverse(map
->p
[i
]);
4177 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4184 static struct isl_map
*isl_basic_map_partial_lexopt(
4185 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
4186 struct isl_set
**empty
, int max
)
4190 if (bmap
->ctx
->opt
->pip
== ISL_PIP_PIP
)
4191 return isl_pip_basic_map_lexopt(bmap
, dom
, empty
, max
);
4193 return isl_tab_basic_map_partial_lexopt(bmap
, dom
, empty
, max
);
4195 isl_basic_map_free(bmap
);
4196 isl_basic_set_free(dom
);
4202 struct isl_map
*isl_basic_map_partial_lexmax(
4203 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
4204 struct isl_set
**empty
)
4206 return isl_basic_map_partial_lexopt(bmap
, dom
, empty
, 1);
4209 struct isl_map
*isl_basic_map_partial_lexmin(
4210 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
4211 struct isl_set
**empty
)
4213 return isl_basic_map_partial_lexopt(bmap
, dom
, empty
, 0);
4216 struct isl_set
*isl_basic_set_partial_lexmin(
4217 struct isl_basic_set
*bset
, struct isl_basic_set
*dom
,
4218 struct isl_set
**empty
)
4220 return (struct isl_set
*)
4221 isl_basic_map_partial_lexmin((struct isl_basic_map
*)bset
,
4225 struct isl_set
*isl_basic_set_partial_lexmax(
4226 struct isl_basic_set
*bset
, struct isl_basic_set
*dom
,
4227 struct isl_set
**empty
)
4229 return (struct isl_set
*)
4230 isl_basic_map_partial_lexmax((struct isl_basic_map
*)bset
,
4234 /* Given a basic map "bmap", compute the lexicograhically minimal
4235 * (or maximal) image element for each domain element in dom.
4236 * Set *empty to those elements in dom that do not have an image element.
4238 * We first make sure the basic sets in dom are disjoint and then
4239 * simply collect the results over each of the basic sets separately.
4240 * We could probably improve the efficiency a bit by moving the union
4241 * domain down into the parametric integer programming.
4243 static __isl_give isl_map
*basic_map_partial_lexopt(
4244 __isl_take isl_basic_map
*bmap
, __isl_take isl_set
*dom
,
4245 __isl_give isl_set
**empty
, int max
)
4248 struct isl_map
*res
;
4250 dom
= isl_set_make_disjoint(dom
);
4254 if (isl_set_fast_is_empty(dom
)) {
4255 res
= isl_map_empty_like_basic_map(bmap
);
4256 *empty
= isl_set_empty_like(dom
);
4258 isl_basic_map_free(bmap
);
4262 res
= isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap
),
4263 isl_basic_set_copy(dom
->p
[0]), empty
, max
);
4265 for (i
= 1; i
< dom
->n
; ++i
) {
4266 struct isl_map
*res_i
;
4267 struct isl_set
*empty_i
;
4269 res_i
= isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap
),
4270 isl_basic_set_copy(dom
->p
[i
]), &empty_i
, max
);
4272 res
= isl_map_union_disjoint(res
, res_i
);
4273 *empty
= isl_set_union_disjoint(*empty
, empty_i
);
4277 isl_basic_map_free(bmap
);
4282 isl_basic_map_free(bmap
);
4286 /* Given a map "map", compute the lexicograhically minimal
4287 * (or maximal) image element for each domain element in dom.
4288 * Set *empty to those elements in dom that do not have an image element.
4290 * We first compute the lexicographically minimal or maximal element
4291 * in the first basic map. This results in a partial solution "res"
4292 * and a subset "todo" of dom that still need to be handled.
4293 * We then consider each of the remaining maps in "map" and successively
4294 * improve both "res" and "todo".
4296 * Let res^k and todo^k be the results after k steps and let i = k + 1.
4297 * Assume we are computing the lexicographical maximum.
4298 * We first intersect basic map i with a relation that maps elements
4299 * to elements that are lexicographically larger than the image elements
4300 * in res^k and the compute the maximum image element of this intersection.
4301 * The result ("better") corresponds to those image elements in basic map i
4302 * that are better than what we had before. The remainder ("keep") are the
4303 * domain elements for which the image element in res_k was better.
4304 * We also compute the lexicographical maximum of basic map i in todo^k.
4305 * res^i is the result of the operation + better + those elements in
4306 * res^k that we should keep
4307 * todo^i is the remainder of the maximum operation on todo^k.
4309 static __isl_give isl_map
*isl_map_partial_lexopt(
4310 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
4311 __isl_give isl_set
**empty
, int max
)
4314 struct isl_map
*res
;
4315 struct isl_set
*todo
;
4320 if (isl_map_fast_is_empty(map
)) {
4328 res
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[0]),
4329 isl_set_copy(dom
), &todo
, max
);
4331 for (i
= 1; i
< map
->n
; ++i
) {
4333 struct isl_map
*better
;
4334 struct isl_set
*keep
;
4335 struct isl_map
*res_i
;
4336 struct isl_set
*todo_i
;
4337 struct isl_dim
*dim
= isl_map_get_dim(res
);
4339 dim
= isl_dim_range(dim
);
4341 lt
= isl_map_lex_lt(dim
);
4343 lt
= isl_map_lex_gt(dim
);
4344 lt
= isl_map_apply_range(isl_map_copy(res
), lt
);
4345 lt
= isl_map_intersect(lt
,
4346 isl_map_from_basic_map(isl_basic_map_copy(map
->p
[i
])));
4347 better
= isl_map_partial_lexopt(lt
,
4348 isl_map_domain(isl_map_copy(res
)),
4351 res_i
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[i
]),
4352 todo
, &todo_i
, max
);
4354 res
= isl_map_intersect_domain(res
, keep
);
4355 res
= isl_map_union_disjoint(res
, res_i
);
4356 res
= isl_map_union_disjoint(res
, better
);
4377 __isl_give isl_map
*isl_map_partial_lexmax(
4378 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
4379 __isl_give isl_set
**empty
)
4381 return isl_map_partial_lexopt(map
, dom
, empty
, 1);
4384 __isl_give isl_map
*isl_map_partial_lexmin(
4385 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
4386 __isl_give isl_set
**empty
)
4388 return isl_map_partial_lexopt(map
, dom
, empty
, 0);
4391 __isl_give isl_set
*isl_set_partial_lexmin(
4392 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
4393 __isl_give isl_set
**empty
)
4395 return (struct isl_set
*)
4396 isl_map_partial_lexmin((struct isl_map
*)set
,
4400 __isl_give isl_set
*isl_set_partial_lexmax(
4401 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
4402 __isl_give isl_set
**empty
)
4404 return (struct isl_set
*)
4405 isl_map_partial_lexmax((struct isl_map
*)set
,
4409 __isl_give isl_map
*isl_basic_map_lexopt(__isl_take isl_basic_map
*bmap
, int max
)
4411 struct isl_basic_set
*dom
= NULL
;
4412 struct isl_dim
*dom_dim
;
4416 dom_dim
= isl_dim_domain(isl_dim_copy(bmap
->dim
));
4417 dom
= isl_basic_set_universe(dom_dim
);
4418 return isl_basic_map_partial_lexopt(bmap
, dom
, NULL
, max
);
4420 isl_basic_map_free(bmap
);
4424 __isl_give isl_map
*isl_basic_map_lexmin(__isl_take isl_basic_map
*bmap
)
4426 return isl_basic_map_lexopt(bmap
, 0);
4429 __isl_give isl_map
*isl_basic_map_lexmax(__isl_take isl_basic_map
*bmap
)
4431 return isl_basic_map_lexopt(bmap
, 1);
4434 __isl_give isl_set
*isl_basic_set_lexmin(__isl_take isl_basic_set
*bset
)
4436 return (isl_set
*)isl_basic_map_lexmin((isl_basic_map
*)bset
);
4439 __isl_give isl_set
*isl_basic_set_lexmax(__isl_take isl_basic_set
*bset
)
4441 return (isl_set
*)isl_basic_map_lexmax((isl_basic_map
*)bset
);
4444 __isl_give isl_map
*isl_map_lexopt(__isl_take isl_map
*map
, int max
)
4446 struct isl_set
*dom
= NULL
;
4447 struct isl_dim
*dom_dim
;
4451 dom_dim
= isl_dim_domain(isl_dim_copy(map
->dim
));
4452 dom
= isl_set_universe(dom_dim
);
4453 return isl_map_partial_lexopt(map
, dom
, NULL
, max
);
4459 __isl_give isl_map
*isl_map_lexmin(__isl_take isl_map
*map
)
4461 return isl_map_lexopt(map
, 0);
4464 __isl_give isl_map
*isl_map_lexmax(__isl_take isl_map
*map
)
4466 return isl_map_lexopt(map
, 1);
4469 __isl_give isl_set
*isl_set_lexmin(__isl_take isl_set
*set
)
4471 return (isl_set
*)isl_map_lexmin((isl_map
*)set
);
4474 __isl_give isl_set
*isl_set_lexmax(__isl_take isl_set
*set
)
4476 return (isl_set
*)isl_map_lexmax((isl_map
*)set
);
4479 /* Apply a preimage specified by "mat" on the parameters of "bset".
4480 * bset is assumed to have only parameters and divs.
4482 static struct isl_basic_set
*basic_set_parameter_preimage(
4483 struct isl_basic_set
*bset
, struct isl_mat
*mat
)
4490 bset
->dim
= isl_dim_cow(bset
->dim
);
4494 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
4496 isl_assert(bset
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
4498 bset
->dim
->nparam
= 0;
4499 bset
->dim
->n_out
= nparam
;
4500 bset
= isl_basic_set_preimage(bset
, mat
);
4502 bset
->dim
->nparam
= bset
->dim
->n_out
;
4503 bset
->dim
->n_out
= 0;
4508 isl_basic_set_free(bset
);
4512 /* Apply a preimage specified by "mat" on the parameters of "set".
4513 * set is assumed to have only parameters and divs.
4515 static struct isl_set
*set_parameter_preimage(
4516 struct isl_set
*set
, struct isl_mat
*mat
)
4518 struct isl_dim
*dim
= NULL
;
4524 dim
= isl_dim_copy(set
->dim
);
4525 dim
= isl_dim_cow(dim
);
4529 nparam
= isl_set_dim(set
, isl_dim_param
);
4531 isl_assert(set
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
4534 dim
->n_out
= nparam
;
4535 isl_set_reset_dim(set
, dim
);
4536 set
= isl_set_preimage(set
, mat
);
4539 dim
= isl_dim_copy(set
->dim
);
4540 dim
= isl_dim_cow(dim
);
4543 dim
->nparam
= dim
->n_out
;
4545 isl_set_reset_dim(set
, dim
);
4555 /* Intersect the basic set "bset" with the affine space specified by the
4556 * equalities in "eq".
4558 static struct isl_basic_set
*basic_set_append_equalities(
4559 struct isl_basic_set
*bset
, struct isl_mat
*eq
)
4567 bset
= isl_basic_set_extend_dim(bset
, isl_dim_copy(bset
->dim
), 0,
4572 len
= 1 + isl_dim_total(bset
->dim
) + bset
->extra
;
4573 for (i
= 0; i
< eq
->n_row
; ++i
) {
4574 k
= isl_basic_set_alloc_equality(bset
);
4577 isl_seq_cpy(bset
->eq
[k
], eq
->row
[i
], eq
->n_col
);
4578 isl_seq_clr(bset
->eq
[k
] + eq
->n_col
, len
- eq
->n_col
);
4582 bset
= isl_basic_set_gauss(bset
, NULL
);
4583 bset
= isl_basic_set_finalize(bset
);
4588 isl_basic_set_free(bset
);
4592 /* Intersect the set "set" with the affine space specified by the
4593 * equalities in "eq".
4595 static struct isl_set
*set_append_equalities(struct isl_set
*set
,
4603 for (i
= 0; i
< set
->n
; ++i
) {
4604 set
->p
[i
] = basic_set_append_equalities(set
->p
[i
],
4617 /* Project the given basic set onto its parameter domain, possibly introducing
4618 * new, explicit, existential variables in the constraints.
4619 * The input has parameters and (possibly implicit) existential variables.
4620 * The output has the same parameters, but only
4621 * explicit existentially quantified variables.
4623 * The actual projection is performed by pip, but pip doesn't seem
4624 * to like equalities very much, so we first remove the equalities
4625 * among the parameters by performing a variable compression on
4626 * the parameters. Afterward, an inverse transformation is performed
4627 * and the equalities among the parameters are inserted back in.
4629 static struct isl_set
*parameter_compute_divs(struct isl_basic_set
*bset
)
4633 struct isl_mat
*T
, *T2
;
4634 struct isl_set
*set
;
4635 unsigned nparam
, n_div
;
4637 bset
= isl_basic_set_cow(bset
);
4641 if (bset
->n_eq
== 0)
4642 return isl_basic_set_lexmin(bset
);
4644 isl_basic_set_gauss(bset
, NULL
);
4646 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
4647 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
4649 for (i
= 0, j
= n_div
- 1; i
< bset
->n_eq
&& j
>= 0; --j
) {
4650 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ j
]))
4653 if (i
== bset
->n_eq
)
4654 return isl_basic_set_lexmin(bset
);
4656 eq
= isl_mat_sub_alloc(bset
->ctx
, bset
->eq
, i
, bset
->n_eq
- i
,
4658 eq
= isl_mat_cow(eq
);
4659 T
= isl_mat_variable_compression(isl_mat_copy(eq
), &T2
);
4660 if (T
&& T
->n_col
== 0) {
4664 bset
= isl_basic_set_set_to_empty(bset
);
4665 return isl_set_from_basic_set(bset
);
4667 bset
= basic_set_parameter_preimage(bset
, T
);
4669 set
= isl_basic_set_lexmin(bset
);
4670 set
= set_parameter_preimage(set
, T2
);
4671 set
= set_append_equalities(set
, eq
);
4675 /* Compute an explicit representation for all the existentially
4676 * quantified variables.
4677 * The input and output dimensions are first turned into parameters.
4678 * compute_divs then returns a map with the same parameters and
4679 * no input or output dimensions and the dimension specification
4680 * is reset to that of the input.
4682 static struct isl_map
*compute_divs(struct isl_basic_map
*bmap
)
4684 struct isl_basic_set
*bset
;
4685 struct isl_set
*set
;
4686 struct isl_map
*map
;
4687 struct isl_dim
*dim
, *orig_dim
= NULL
;
4692 bmap
= isl_basic_map_cow(bmap
);
4696 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4697 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4698 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4699 dim
= isl_dim_set_alloc(bmap
->ctx
, nparam
+ n_in
+ n_out
, 0);
4703 orig_dim
= bmap
->dim
;
4705 bset
= (struct isl_basic_set
*)bmap
;
4707 set
= parameter_compute_divs(bset
);
4708 map
= (struct isl_map
*)set
;
4709 map
= isl_map_reset_dim(map
, orig_dim
);
4713 isl_basic_map_free(bmap
);
4717 static int basic_map_divs_known(__isl_keep isl_basic_map
*bmap
)
4725 off
= isl_dim_total(bmap
->dim
);
4726 for (i
= 0; i
< bmap
->n_div
; ++i
) {
4727 if (isl_int_is_zero(bmap
->div
[i
][0]))
4729 isl_assert(bmap
->ctx
, isl_int_is_zero(bmap
->div
[i
][1+1+off
+i
]),
4735 static int map_divs_known(__isl_keep isl_map
*map
)
4742 for (i
= 0; i
< map
->n
; ++i
) {
4743 int known
= basic_map_divs_known(map
->p
[i
]);
4751 /* If bmap contains any unknown divs, then compute explicit
4752 * expressions for them. However, this computation may be
4753 * quite expensive, so first try to remove divs that aren't
4756 struct isl_map
*isl_basic_map_compute_divs(struct isl_basic_map
*bmap
)
4760 struct isl_map
*map
;
4762 known
= basic_map_divs_known(bmap
);
4766 return isl_map_from_basic_map(bmap
);
4768 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
4770 known
= basic_map_divs_known(bmap
);
4774 return isl_map_from_basic_map(bmap
);
4776 map
= compute_divs(bmap
);
4779 isl_basic_map_free(bmap
);
4783 struct isl_map
*isl_map_compute_divs(struct isl_map
*map
)
4787 struct isl_map
*res
;
4794 known
= map_divs_known(map
);
4802 res
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[0]));
4803 for (i
= 1 ; i
< map
->n
; ++i
) {
4805 r2
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[i
]));
4806 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
4807 res
= isl_map_union_disjoint(res
, r2
);
4809 res
= isl_map_union(res
, r2
);
4816 struct isl_set
*isl_basic_set_compute_divs(struct isl_basic_set
*bset
)
4818 return (struct isl_set
*)
4819 isl_basic_map_compute_divs((struct isl_basic_map
*)bset
);
4822 struct isl_set
*isl_set_compute_divs(struct isl_set
*set
)
4824 return (struct isl_set
*)
4825 isl_map_compute_divs((struct isl_map
*)set
);
4828 struct isl_set
*isl_map_domain(struct isl_map
*map
)
4831 struct isl_set
*set
;
4836 map
= isl_map_cow(map
);
4840 set
= (struct isl_set
*)map
;
4841 set
->dim
= isl_dim_domain(set
->dim
);
4844 for (i
= 0; i
< map
->n
; ++i
) {
4845 set
->p
[i
] = isl_basic_map_domain(map
->p
[i
]);
4849 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
4850 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
4857 struct isl_map
*isl_map_union_disjoint(
4858 struct isl_map
*map1
, struct isl_map
*map2
)
4862 struct isl_map
*map
= NULL
;
4876 isl_assert(map1
->ctx
, isl_dim_equal(map1
->dim
, map2
->dim
), goto error
);
4878 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
4879 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
4880 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
4882 map
= isl_map_alloc_dim(isl_dim_copy(map1
->dim
),
4883 map1
->n
+ map2
->n
, flags
);
4886 for (i
= 0; i
< map1
->n
; ++i
) {
4887 map
= isl_map_add_basic_map(map
,
4888 isl_basic_map_copy(map1
->p
[i
]));
4892 for (i
= 0; i
< map2
->n
; ++i
) {
4893 map
= isl_map_add_basic_map(map
,
4894 isl_basic_map_copy(map2
->p
[i
]));
4908 struct isl_map
*isl_map_union(struct isl_map
*map1
, struct isl_map
*map2
)
4910 map1
= isl_map_union_disjoint(map1
, map2
);
4914 ISL_F_CLR(map1
, ISL_MAP_DISJOINT
);
4918 struct isl_set
*isl_set_union_disjoint(
4919 struct isl_set
*set1
, struct isl_set
*set2
)
4921 return (struct isl_set
*)
4922 isl_map_union_disjoint(
4923 (struct isl_map
*)set1
, (struct isl_map
*)set2
);
4926 struct isl_set
*isl_set_union(struct isl_set
*set1
, struct isl_set
*set2
)
4928 return (struct isl_set
*)
4929 isl_map_union((struct isl_map
*)set1
, (struct isl_map
*)set2
);
4932 struct isl_map
*isl_map_intersect_range(
4933 struct isl_map
*map
, struct isl_set
*set
)
4936 struct isl_map
*result
;
4942 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
) &&
4943 ISL_F_ISSET(set
, ISL_MAP_DISJOINT
))
4944 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
4946 result
= isl_map_alloc_dim(isl_dim_copy(map
->dim
),
4947 map
->n
* set
->n
, flags
);
4950 for (i
= 0; i
< map
->n
; ++i
)
4951 for (j
= 0; j
< set
->n
; ++j
) {
4952 result
= isl_map_add_basic_map(result
,
4953 isl_basic_map_intersect_range(
4954 isl_basic_map_copy(map
->p
[i
]),
4955 isl_basic_set_copy(set
->p
[j
])));
4968 struct isl_map
*isl_map_intersect_domain(
4969 struct isl_map
*map
, struct isl_set
*set
)
4971 return isl_map_reverse(
4972 isl_map_intersect_range(isl_map_reverse(map
), set
));
4975 struct isl_map
*isl_map_apply_domain(
4976 struct isl_map
*map1
, struct isl_map
*map2
)
4980 map1
= isl_map_reverse(map1
);
4981 map1
= isl_map_apply_range(map1
, map2
);
4982 return isl_map_reverse(map1
);
4989 struct isl_map
*isl_map_apply_range(
4990 struct isl_map
*map1
, struct isl_map
*map2
)
4992 struct isl_dim
*dim_result
;
4993 struct isl_map
*result
;
4999 dim_result
= isl_dim_join(isl_dim_copy(map1
->dim
),
5000 isl_dim_copy(map2
->dim
));
5002 result
= isl_map_alloc_dim(dim_result
, map1
->n
* map2
->n
, 0);
5005 for (i
= 0; i
< map1
->n
; ++i
)
5006 for (j
= 0; j
< map2
->n
; ++j
) {
5007 result
= isl_map_add_basic_map(result
,
5008 isl_basic_map_apply_range(
5009 isl_basic_map_copy(map1
->p
[i
]),
5010 isl_basic_map_copy(map2
->p
[j
])));
5016 if (result
&& result
->n
<= 1)
5017 ISL_F_SET(result
, ISL_MAP_DISJOINT
);
5026 * returns range - domain
5028 struct isl_basic_set
*isl_basic_map_deltas(struct isl_basic_map
*bmap
)
5030 isl_dim
*dims
, *target_dim
;
5031 struct isl_basic_set
*bset
;
5038 isl_assert(bmap
->ctx
, isl_dim_tuple_match(bmap
->dim
, isl_dim_in
,
5039 bmap
->dim
, isl_dim_out
),
5041 target_dim
= isl_dim_domain(isl_basic_map_get_dim(bmap
));
5042 dim
= isl_basic_map_n_in(bmap
);
5043 nparam
= isl_basic_map_n_param(bmap
);
5044 bset
= isl_basic_set_from_basic_map(bmap
);
5045 bset
= isl_basic_set_cow(bset
);
5046 dims
= isl_basic_set_get_dim(bset
);
5047 dims
= isl_dim_add(dims
, isl_dim_set
, dim
);
5048 bset
= isl_basic_set_extend_dim(bset
, dims
, 0, dim
, 0);
5049 bset
= isl_basic_set_swap_vars(bset
, 2*dim
);
5050 for (i
= 0; i
< dim
; ++i
) {
5051 int j
= isl_basic_map_alloc_equality(
5052 (struct isl_basic_map
*)bset
);
5055 isl_seq_clr(bset
->eq
[j
], 1 + isl_basic_set_total_dim(bset
));
5056 isl_int_set_si(bset
->eq
[j
][1+nparam
+i
], 1);
5057 isl_int_set_si(bset
->eq
[j
][1+nparam
+dim
+i
], 1);
5058 isl_int_set_si(bset
->eq
[j
][1+nparam
+2*dim
+i
], -1);
5060 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, dim
, 2*dim
);
5061 bset
= isl_basic_set_reset_dim(bset
, target_dim
);
5064 isl_basic_map_free(bmap
);
5069 * returns range - domain
5071 struct isl_set
*isl_map_deltas(struct isl_map
*map
)
5075 struct isl_set
*result
;
5080 isl_assert(map
->ctx
, isl_dim_tuple_match(map
->dim
, isl_dim_in
,
5081 map
->dim
, isl_dim_out
),
5083 dim
= isl_map_get_dim(map
);
5084 dim
= isl_dim_domain(dim
);
5085 result
= isl_set_alloc_dim(dim
, map
->n
, map
->flags
);
5088 for (i
= 0; i
< map
->n
; ++i
)
5089 result
= isl_set_add_basic_set(result
,
5090 isl_basic_map_deltas(isl_basic_map_copy(map
->p
[i
])));
5098 static struct isl_basic_map
*basic_map_identity(struct isl_dim
*dims
)
5100 struct isl_basic_map
*bmap
;
5108 nparam
= dims
->nparam
;
5110 bmap
= isl_basic_map_alloc_dim(dims
, 0, dim
, 0);
5114 for (i
= 0; i
< dim
; ++i
) {
5115 int j
= isl_basic_map_alloc_equality(bmap
);
5118 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
5119 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
5120 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], -1);
5122 return isl_basic_map_finalize(bmap
);
5124 isl_basic_map_free(bmap
);
5128 struct isl_basic_map
*isl_basic_map_identity(struct isl_dim
*set_dim
)
5130 struct isl_dim
*dim
= isl_dim_map(set_dim
);
5133 return basic_map_identity(dim
);
5136 struct isl_basic_map
*isl_basic_map_identity_like(struct isl_basic_map
*model
)
5138 if (!model
|| !model
->dim
)
5140 isl_assert(model
->ctx
,
5141 model
->dim
->n_in
== model
->dim
->n_out
, return NULL
);
5142 return basic_map_identity(isl_dim_copy(model
->dim
));
5145 static struct isl_map
*map_identity(struct isl_dim
*dim
)
5147 struct isl_map
*map
= isl_map_alloc_dim(dim
, 1, ISL_MAP_DISJOINT
);
5148 return isl_map_add_basic_map(map
, basic_map_identity(isl_dim_copy(dim
)));
5151 struct isl_map
*isl_map_identity(struct isl_dim
*set_dim
)
5153 struct isl_dim
*dim
= isl_dim_map(set_dim
);
5156 return map_identity(dim
);
5159 struct isl_map
*isl_map_identity_like(struct isl_map
*model
)
5161 if (!model
|| !model
->dim
)
5163 isl_assert(model
->ctx
,
5164 model
->dim
->n_in
== model
->dim
->n_out
, return NULL
);
5165 return map_identity(isl_dim_copy(model
->dim
));
5168 struct isl_map
*isl_map_identity_like_basic_map(struct isl_basic_map
*model
)
5170 if (!model
|| !model
->dim
)
5172 isl_assert(model
->ctx
,
5173 model
->dim
->n_in
== model
->dim
->n_out
, return NULL
);
5174 return map_identity(isl_dim_copy(model
->dim
));
5177 /* Construct a basic set with all set dimensions having only non-negative
5180 struct isl_basic_set
*isl_basic_set_positive_orthant(struct isl_dim
*dims
)
5185 struct isl_basic_set
*bset
;
5189 nparam
= dims
->nparam
;
5191 bset
= isl_basic_set_alloc_dim(dims
, 0, 0, dim
);
5194 for (i
= 0; i
< dim
; ++i
) {
5195 int k
= isl_basic_set_alloc_inequality(bset
);
5198 isl_seq_clr(bset
->ineq
[k
], 1 + isl_basic_set_total_dim(bset
));
5199 isl_int_set_si(bset
->ineq
[k
][1 + nparam
+ i
], 1);
5203 isl_basic_set_free(bset
);
5207 __isl_give isl_set
*isl_set_split_dims(__isl_take isl_set
*set
,
5208 enum isl_dim_type type
, unsigned first
, unsigned n
)
5211 isl_basic_set
*nonneg
= NULL
;
5212 isl_basic_set
*neg
= NULL
;
5219 isl_assert(set
->ctx
, first
+ n
<= isl_set_dim(set
, type
), goto error
);
5221 for (i
= 0; i
< n
; ++i
) {
5225 nonneg
= isl_basic_set_alloc_dim(isl_set_get_dim(set
), 0, 0, 1);
5226 k
= isl_basic_set_alloc_inequality(nonneg
);
5229 isl_seq_clr(nonneg
->ineq
[k
], 1 + isl_basic_set_total_dim(nonneg
));
5230 isl_int_set_si(nonneg
->ineq
[k
][pos(set
->dim
, type
) + first
+ i
], 1);
5232 neg
= isl_basic_set_alloc_dim(isl_set_get_dim(set
), 0, 0, 1);
5233 k
= isl_basic_set_alloc_inequality(neg
);
5236 isl_seq_clr(neg
->ineq
[k
], 1 + isl_basic_set_total_dim(neg
));
5237 isl_int_set_si(neg
->ineq
[k
][0], -1);
5238 isl_int_set_si(neg
->ineq
[k
][pos(set
->dim
, type
) + first
+ i
], -1);
5240 set
= isl_set_intersect(set
, isl_basic_set_union(nonneg
, neg
));
5245 isl_basic_set_free(nonneg
);
5246 isl_basic_set_free(neg
);
5251 int isl_set_is_equal(struct isl_set
*set1
, struct isl_set
*set2
)
5253 return isl_map_is_equal((struct isl_map
*)set1
, (struct isl_map
*)set2
);
5256 int isl_basic_map_is_subset(
5257 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
5260 struct isl_map
*map1
;
5261 struct isl_map
*map2
;
5263 if (!bmap1
|| !bmap2
)
5266 map1
= isl_map_from_basic_map(isl_basic_map_copy(bmap1
));
5267 map2
= isl_map_from_basic_map(isl_basic_map_copy(bmap2
));
5269 is_subset
= isl_map_is_subset(map1
, map2
);
5277 int isl_basic_map_is_equal(
5278 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
5282 if (!bmap1
|| !bmap2
)
5284 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
5287 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
5291 int isl_basic_set_is_equal(
5292 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
5294 return isl_basic_map_is_equal(
5295 (struct isl_basic_map
*)bset1
, (struct isl_basic_map
*)bset2
);
5298 int isl_map_is_empty(struct isl_map
*map
)
5305 for (i
= 0; i
< map
->n
; ++i
) {
5306 is_empty
= isl_basic_map_is_empty(map
->p
[i
]);
5315 int isl_map_fast_is_empty(struct isl_map
*map
)
5317 return map
? map
->n
== 0 : -1;
5320 int isl_set_fast_is_empty(struct isl_set
*set
)
5322 return set
? set
->n
== 0 : -1;
5325 int isl_set_is_empty(struct isl_set
*set
)
5327 return isl_map_is_empty((struct isl_map
*)set
);
5330 int isl_map_has_equal_dim(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
5335 return isl_dim_equal(map1
->dim
, map2
->dim
);
5338 int isl_set_has_equal_dim(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
5343 return isl_dim_equal(set1
->dim
, set2
->dim
);
5346 int isl_map_is_equal(struct isl_map
*map1
, struct isl_map
*map2
)
5352 is_subset
= isl_map_is_subset(map1
, map2
);
5355 is_subset
= isl_map_is_subset(map2
, map1
);
5359 int isl_basic_map_is_strict_subset(
5360 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
5364 if (!bmap1
|| !bmap2
)
5366 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
5369 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
5370 if (is_subset
== -1)
5375 int isl_map_is_strict_subset(struct isl_map
*map1
, struct isl_map
*map2
)
5381 is_subset
= isl_map_is_subset(map1
, map2
);
5384 is_subset
= isl_map_is_subset(map2
, map1
);
5385 if (is_subset
== -1)
5390 int isl_set_is_strict_subset(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
5392 return isl_map_is_strict_subset((isl_map
*)set1
, (isl_map
*)set2
);
5395 int isl_basic_map_is_universe(struct isl_basic_map
*bmap
)
5399 return bmap
->n_eq
== 0 && bmap
->n_ineq
== 0;
5402 int isl_basic_set_is_universe(struct isl_basic_set
*bset
)
5406 return bset
->n_eq
== 0 && bset
->n_ineq
== 0;
5409 int isl_map_fast_is_universe(__isl_keep isl_map
*map
)
5416 for (i
= 0; i
< map
->n
; ++i
) {
5417 int r
= isl_basic_map_is_universe(map
->p
[i
]);
5425 int isl_set_fast_is_universe(__isl_keep isl_set
*set
)
5427 return isl_map_fast_is_universe((isl_map
*) set
);
5430 int isl_basic_map_is_empty(struct isl_basic_map
*bmap
)
5432 struct isl_basic_set
*bset
= NULL
;
5433 struct isl_vec
*sample
= NULL
;
5440 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
5443 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
5444 struct isl_basic_map
*copy
= isl_basic_map_copy(bmap
);
5445 copy
= isl_basic_map_remove_redundancies(copy
);
5446 empty
= ISL_F_ISSET(copy
, ISL_BASIC_MAP_EMPTY
);
5447 isl_basic_map_free(copy
);
5451 total
= 1 + isl_basic_map_total_dim(bmap
);
5452 if (bmap
->sample
&& bmap
->sample
->size
== total
) {
5453 int contains
= isl_basic_map_contains(bmap
, bmap
->sample
);
5459 isl_vec_free(bmap
->sample
);
5460 bmap
->sample
= NULL
;
5461 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
5464 sample
= isl_basic_set_sample_vec(bset
);
5467 empty
= sample
->size
== 0;
5468 isl_vec_free(bmap
->sample
);
5469 bmap
->sample
= sample
;
5471 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
5476 int isl_basic_map_fast_is_empty(struct isl_basic_map
*bmap
)
5480 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
);
5483 int isl_basic_set_fast_is_empty(struct isl_basic_set
*bset
)
5487 return ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
);
5490 int isl_basic_set_is_empty(struct isl_basic_set
*bset
)
5492 return isl_basic_map_is_empty((struct isl_basic_map
*)bset
);
5495 struct isl_map
*isl_basic_map_union(
5496 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
5498 struct isl_map
*map
;
5499 if (!bmap1
|| !bmap2
)
5502 isl_assert(bmap1
->ctx
, isl_dim_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
5504 map
= isl_map_alloc_dim(isl_dim_copy(bmap1
->dim
), 2, 0);
5507 map
= isl_map_add_basic_map(map
, bmap1
);
5508 map
= isl_map_add_basic_map(map
, bmap2
);
5511 isl_basic_map_free(bmap1
);
5512 isl_basic_map_free(bmap2
);
5516 struct isl_set
*isl_basic_set_union(
5517 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
5519 return (struct isl_set
*)isl_basic_map_union(
5520 (struct isl_basic_map
*)bset1
,
5521 (struct isl_basic_map
*)bset2
);
5524 /* Order divs such that any div only depends on previous divs */
5525 struct isl_basic_map
*isl_basic_map_order_divs(struct isl_basic_map
*bmap
)
5533 off
= isl_dim_total(bmap
->dim
);
5535 for (i
= 0; i
< bmap
->n_div
; ++i
) {
5537 if (isl_int_is_zero(bmap
->div
[i
][0]))
5539 pos
= isl_seq_first_non_zero(bmap
->div
[i
]+1+1+off
+i
,
5543 isl_basic_map_swap_div(bmap
, i
, i
+ pos
);
5549 struct isl_basic_set
*isl_basic_set_order_divs(struct isl_basic_set
*bset
)
5551 return (struct isl_basic_set
*)
5552 isl_basic_map_order_divs((struct isl_basic_map
*)bset
);
5555 __isl_give isl_map
*isl_map_order_divs(__isl_take isl_map
*map
)
5562 for (i
= 0; i
< map
->n
; ++i
) {
5563 map
->p
[i
] = isl_basic_map_order_divs(map
->p
[i
]);
5574 /* Look for a div in dst that corresponds to the div "div" in src.
5575 * The divs before "div" in src and dst are assumed to be the same.
5577 * Returns -1 if no corresponding div was found and the position
5578 * of the corresponding div in dst otherwise.
5580 static int find_div(struct isl_basic_map
*dst
,
5581 struct isl_basic_map
*src
, unsigned div
)
5585 unsigned total
= isl_dim_total(src
->dim
);
5587 isl_assert(dst
->ctx
, div
<= dst
->n_div
, return -1);
5588 for (i
= div
; i
< dst
->n_div
; ++i
)
5589 if (isl_seq_eq(dst
->div
[i
], src
->div
[div
], 1+1+total
+div
) &&
5590 isl_seq_first_non_zero(dst
->div
[i
]+1+1+total
+div
,
5591 dst
->n_div
- div
) == -1)
5596 struct isl_basic_map
*isl_basic_map_align_divs(
5597 struct isl_basic_map
*dst
, struct isl_basic_map
*src
)
5600 unsigned total
= isl_dim_total(src
->dim
);
5605 if (src
->n_div
== 0)
5608 for (i
= 0; i
< src
->n_div
; ++i
)
5609 isl_assert(src
->ctx
, !isl_int_is_zero(src
->div
[i
][0]), goto error
);
5611 src
= isl_basic_map_order_divs(src
);
5612 dst
= isl_basic_map_cow(dst
);
5613 dst
= isl_basic_map_extend_dim(dst
, isl_dim_copy(dst
->dim
),
5614 src
->n_div
, 0, 2 * src
->n_div
);
5617 for (i
= 0; i
< src
->n_div
; ++i
) {
5618 int j
= find_div(dst
, src
, i
);
5620 j
= isl_basic_map_alloc_div(dst
);
5623 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
+i
);
5624 isl_seq_clr(dst
->div
[j
]+1+1+total
+i
, dst
->n_div
- i
);
5625 if (isl_basic_map_add_div_constraints(dst
, j
) < 0)
5629 isl_basic_map_swap_div(dst
, i
, j
);
5633 isl_basic_map_free(dst
);
5637 struct isl_basic_set
*isl_basic_set_align_divs(
5638 struct isl_basic_set
*dst
, struct isl_basic_set
*src
)
5640 return (struct isl_basic_set
*)isl_basic_map_align_divs(
5641 (struct isl_basic_map
*)dst
, (struct isl_basic_map
*)src
);
5644 struct isl_map
*isl_map_align_divs(struct isl_map
*map
)
5652 map
= isl_map_compute_divs(map
);
5653 map
= isl_map_cow(map
);
5657 for (i
= 1; i
< map
->n
; ++i
)
5658 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], map
->p
[i
]);
5659 for (i
= 1; i
< map
->n
; ++i
)
5660 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], map
->p
[0]);
5662 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5666 struct isl_set
*isl_set_align_divs(struct isl_set
*set
)
5668 return (struct isl_set
*)isl_map_align_divs((struct isl_map
*)set
);
5671 struct isl_set
*isl_set_apply(struct isl_set
*set
, struct isl_map
*map
)
5675 isl_assert(set
->ctx
, isl_map_compatible_domain(map
, set
), goto error
);
5676 map
= isl_map_intersect_domain(map
, set
);
5677 set
= isl_map_range(map
);
5685 /* There is no need to cow as removing empty parts doesn't change
5686 * the meaning of the set.
5688 struct isl_map
*isl_map_remove_empty_parts(struct isl_map
*map
)
5695 for (i
= map
->n
-1; i
>= 0; --i
) {
5696 if (!ISL_F_ISSET(map
->p
[i
], ISL_BASIC_MAP_EMPTY
))
5698 isl_basic_map_free(map
->p
[i
]);
5699 if (i
!= map
->n
-1) {
5700 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5701 map
->p
[i
] = map
->p
[map
->n
-1];
5709 struct isl_set
*isl_set_remove_empty_parts(struct isl_set
*set
)
5711 return (struct isl_set
*)
5712 isl_map_remove_empty_parts((struct isl_map
*)set
);
5715 struct isl_basic_map
*isl_map_copy_basic_map(struct isl_map
*map
)
5717 struct isl_basic_map
*bmap
;
5718 if (!map
|| map
->n
== 0)
5720 bmap
= map
->p
[map
->n
-1];
5721 isl_assert(map
->ctx
, ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
), return NULL
);
5722 return isl_basic_map_copy(bmap
);
5725 struct isl_basic_set
*isl_set_copy_basic_set(struct isl_set
*set
)
5727 return (struct isl_basic_set
*)
5728 isl_map_copy_basic_map((struct isl_map
*)set
);
5731 __isl_give isl_map
*isl_map_drop_basic_map(__isl_take isl_map
*map
,
5732 __isl_keep isl_basic_map
*bmap
)
5738 for (i
= map
->n
-1; i
>= 0; --i
) {
5739 if (map
->p
[i
] != bmap
)
5741 map
= isl_map_cow(map
);
5744 isl_basic_map_free(map
->p
[i
]);
5745 if (i
!= map
->n
-1) {
5746 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
5747 map
->p
[i
] = map
->p
[map
->n
-1];
5758 struct isl_set
*isl_set_drop_basic_set(struct isl_set
*set
,
5759 struct isl_basic_set
*bset
)
5761 return (struct isl_set
*)isl_map_drop_basic_map((struct isl_map
*)set
,
5762 (struct isl_basic_map
*)bset
);
5765 /* Given two basic sets bset1 and bset2, compute the maximal difference
5766 * between the values of dimension pos in bset1 and those in bset2
5767 * for any common value of the parameters and dimensions preceding pos.
5769 static enum isl_lp_result
basic_set_maximal_difference_at(
5770 __isl_keep isl_basic_set
*bset1
, __isl_keep isl_basic_set
*bset2
,
5771 int pos
, isl_int
*opt
)
5773 struct isl_dim
*dims
;
5774 struct isl_basic_map
*bmap1
= NULL
;
5775 struct isl_basic_map
*bmap2
= NULL
;
5776 struct isl_ctx
*ctx
;
5777 struct isl_vec
*obj
;
5780 unsigned dim1
, dim2
;
5781 enum isl_lp_result res
;
5783 if (!bset1
|| !bset2
)
5784 return isl_lp_error
;
5786 nparam
= isl_basic_set_n_param(bset1
);
5787 dim1
= isl_basic_set_n_dim(bset1
);
5788 dim2
= isl_basic_set_n_dim(bset2
);
5789 dims
= isl_dim_alloc(bset1
->ctx
, nparam
, pos
, dim1
- pos
);
5790 bmap1
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset1
), dims
);
5791 dims
= isl_dim_alloc(bset2
->ctx
, nparam
, pos
, dim2
- pos
);
5792 bmap2
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset2
), dims
);
5793 if (!bmap1
|| !bmap2
)
5795 bmap1
= isl_basic_map_cow(bmap1
);
5796 bmap1
= isl_basic_map_extend(bmap1
, nparam
,
5797 pos
, (dim1
- pos
) + (dim2
- pos
),
5798 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
5799 bmap1
= add_constraints(bmap1
, bmap2
, 0, dim1
- pos
);
5802 total
= isl_basic_map_total_dim(bmap1
);
5804 obj
= isl_vec_alloc(ctx
, 1 + total
);
5805 isl_seq_clr(obj
->block
.data
, 1 + total
);
5806 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
], 1);
5807 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
+(dim1
-pos
)], -1);
5810 res
= isl_basic_map_solve_lp(bmap1
, 1, obj
->block
.data
, ctx
->one
,
5812 isl_basic_map_free(bmap1
);
5816 isl_basic_map_free(bmap1
);
5817 isl_basic_map_free(bmap2
);
5818 return isl_lp_error
;
5821 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
5822 * for any common value of the parameters and dimensions preceding pos
5823 * in both basic sets, the values of dimension pos in bset1 are
5824 * smaller or larger than those in bset2.
5827 * 1 if bset1 follows bset2
5828 * -1 if bset1 precedes bset2
5829 * 0 if bset1 and bset2 are incomparable
5830 * -2 if some error occurred.
5832 int isl_basic_set_compare_at(struct isl_basic_set
*bset1
,
5833 struct isl_basic_set
*bset2
, int pos
)
5836 enum isl_lp_result res
;
5841 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
5843 if (res
== isl_lp_empty
)
5845 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
5846 res
== isl_lp_unbounded
)
5848 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
5857 /* Given two basic sets bset1 and bset2, check whether
5858 * for any common value of the parameters and dimensions preceding pos
5859 * there is a value of dimension pos in bset1 that is larger
5860 * than a value of the same dimension in bset2.
5863 * 1 if there exists such a pair
5864 * 0 if there is no such pair, but there is a pair of equal values
5866 * -2 if some error occurred.
5868 int isl_basic_set_follows_at(__isl_keep isl_basic_set
*bset1
,
5869 __isl_keep isl_basic_set
*bset2
, int pos
)
5872 enum isl_lp_result res
;
5877 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
5879 if (res
== isl_lp_empty
)
5881 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
5882 res
== isl_lp_unbounded
)
5884 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
5886 else if (res
== isl_lp_ok
)
5895 /* Given two sets set1 and set2, check whether
5896 * for any common value of the parameters and dimensions preceding pos
5897 * there is a value of dimension pos in set1 that is larger
5898 * than a value of the same dimension in set2.
5901 * 1 if there exists such a pair
5902 * 0 if there is no such pair, but there is a pair of equal values
5904 * -2 if some error occurred.
5906 int isl_set_follows_at(__isl_keep isl_set
*set1
,
5907 __isl_keep isl_set
*set2
, int pos
)
5915 for (i
= 0; i
< set1
->n
; ++i
)
5916 for (j
= 0; j
< set2
->n
; ++j
) {
5918 f
= isl_basic_set_follows_at(set1
->p
[i
], set2
->p
[j
], pos
);
5919 if (f
== 1 || f
== -2)
5928 static int isl_basic_map_fast_has_fixed_var(struct isl_basic_map
*bmap
,
5929 unsigned pos
, isl_int
*val
)
5937 total
= isl_basic_map_total_dim(bmap
);
5938 for (i
= 0, d
= total
-1; i
< bmap
->n_eq
&& d
+1 > pos
; ++i
) {
5939 for (; d
+1 > pos
; --d
)
5940 if (!isl_int_is_zero(bmap
->eq
[i
][1+d
]))
5944 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1, d
) != -1)
5946 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1+d
+1, total
-d
-1) != -1)
5948 if (!isl_int_is_one(bmap
->eq
[i
][1+d
]))
5951 isl_int_neg(*val
, bmap
->eq
[i
][0]);
5957 static int isl_map_fast_has_fixed_var(struct isl_map
*map
,
5958 unsigned pos
, isl_int
*val
)
5970 return isl_basic_map_fast_has_fixed_var(map
->p
[0], pos
, val
);
5973 fixed
= isl_basic_map_fast_has_fixed_var(map
->p
[0], pos
, &v
);
5974 for (i
= 1; fixed
== 1 && i
< map
->n
; ++i
) {
5975 fixed
= isl_basic_map_fast_has_fixed_var(map
->p
[i
], pos
, &tmp
);
5976 if (fixed
== 1 && isl_int_ne(tmp
, v
))
5980 isl_int_set(*val
, v
);
5986 static int isl_basic_set_fast_has_fixed_var(struct isl_basic_set
*bset
,
5987 unsigned pos
, isl_int
*val
)
5989 return isl_basic_map_fast_has_fixed_var((struct isl_basic_map
*)bset
,
5993 static int isl_set_fast_has_fixed_var(struct isl_set
*set
, unsigned pos
,
5996 return isl_map_fast_has_fixed_var((struct isl_map
*)set
, pos
, val
);
5999 int isl_basic_map_fast_is_fixed(struct isl_basic_map
*bmap
,
6000 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
6002 if (pos
>= isl_basic_map_dim(bmap
, type
))
6004 return isl_basic_map_fast_has_fixed_var(bmap
,
6005 isl_basic_map_offset(bmap
, type
) - 1 + pos
, val
);
6008 int isl_map_fast_is_fixed(struct isl_map
*map
,
6009 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
6011 if (pos
>= isl_map_dim(map
, type
))
6013 return isl_map_fast_has_fixed_var(map
,
6014 map_offset(map
, type
) - 1 + pos
, val
);
6017 /* Check if dimension dim has fixed value and if so and if val is not NULL,
6018 * then return this fixed value in *val.
6020 int isl_basic_set_fast_dim_is_fixed(struct isl_basic_set
*bset
, unsigned dim
,
6023 return isl_basic_set_fast_has_fixed_var(bset
,
6024 isl_basic_set_n_param(bset
) + dim
, val
);
6027 /* Check if dimension dim has fixed value and if so and if val is not NULL,
6028 * then return this fixed value in *val.
6030 int isl_set_fast_dim_is_fixed(struct isl_set
*set
, unsigned dim
, isl_int
*val
)
6032 return isl_set_fast_has_fixed_var(set
, isl_set_n_param(set
) + dim
, val
);
6035 /* Check if input variable in has fixed value and if so and if val is not NULL,
6036 * then return this fixed value in *val.
6038 int isl_map_fast_input_is_fixed(struct isl_map
*map
, unsigned in
, isl_int
*val
)
6040 return isl_map_fast_has_fixed_var(map
, isl_map_n_param(map
) + in
, val
);
6043 /* Check if dimension dim has an (obvious) fixed lower bound and if so
6044 * and if val is not NULL, then return this lower bound in *val.
6046 int isl_basic_set_fast_dim_has_fixed_lower_bound(struct isl_basic_set
*bset
,
6047 unsigned dim
, isl_int
*val
)
6049 int i
, i_eq
= -1, i_ineq
= -1;
6056 total
= isl_basic_set_total_dim(bset
);
6057 nparam
= isl_basic_set_n_param(bset
);
6058 for (i
= 0; i
< bset
->n_eq
; ++i
) {
6059 if (isl_int_is_zero(bset
->eq
[i
][1+nparam
+dim
]))
6065 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
6066 if (!isl_int_is_pos(bset
->ineq
[i
][1+nparam
+dim
]))
6068 if (i_eq
!= -1 || i_ineq
!= -1)
6072 if (i_eq
== -1 && i_ineq
== -1)
6074 c
= i_eq
!= -1 ? bset
->eq
[i_eq
] : bset
->ineq
[i_ineq
];
6075 /* The coefficient should always be one due to normalization. */
6076 if (!isl_int_is_one(c
[1+nparam
+dim
]))
6078 if (isl_seq_first_non_zero(c
+1, nparam
+dim
) != -1)
6080 if (isl_seq_first_non_zero(c
+1+nparam
+dim
+1,
6081 total
- nparam
- dim
- 1) != -1)
6084 isl_int_neg(*val
, c
[0]);
6088 int isl_set_fast_dim_has_fixed_lower_bound(struct isl_set
*set
,
6089 unsigned dim
, isl_int
*val
)
6101 return isl_basic_set_fast_dim_has_fixed_lower_bound(set
->p
[0],
6105 fixed
= isl_basic_set_fast_dim_has_fixed_lower_bound(set
->p
[0],
6107 for (i
= 1; fixed
== 1 && i
< set
->n
; ++i
) {
6108 fixed
= isl_basic_set_fast_dim_has_fixed_lower_bound(set
->p
[i
],
6110 if (fixed
== 1 && isl_int_ne(tmp
, v
))
6114 isl_int_set(*val
, v
);
6125 static int qsort_constraint_cmp(const void *p1
, const void *p2
)
6127 const struct constraint
*c1
= (const struct constraint
*)p1
;
6128 const struct constraint
*c2
= (const struct constraint
*)p2
;
6129 unsigned size
= isl_min(c1
->size
, c2
->size
);
6130 return isl_seq_cmp(c1
->c
, c2
->c
, size
);
6133 static struct isl_basic_map
*isl_basic_map_sort_constraints(
6134 struct isl_basic_map
*bmap
)
6137 struct constraint
*c
;
6142 total
= isl_basic_map_total_dim(bmap
);
6143 c
= isl_alloc_array(bmap
->ctx
, struct constraint
, bmap
->n_ineq
);
6146 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
6148 c
[i
].c
= bmap
->ineq
[i
];
6150 qsort(c
, bmap
->n_ineq
, sizeof(struct constraint
), qsort_constraint_cmp
);
6151 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
6152 bmap
->ineq
[i
] = c
[i
].c
;
6156 isl_basic_map_free(bmap
);
6160 __isl_give isl_basic_set
*isl_basic_set_sort_constraints(
6161 __isl_take isl_basic_set
*bset
)
6163 return (struct isl_basic_set
*)isl_basic_map_sort_constraints(
6164 (struct isl_basic_map
*)bset
);
6167 struct isl_basic_map
*isl_basic_map_normalize(struct isl_basic_map
*bmap
)
6171 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED
))
6173 bmap
= isl_basic_map_remove_redundancies(bmap
);
6174 bmap
= isl_basic_map_sort_constraints(bmap
);
6175 ISL_F_SET(bmap
, ISL_BASIC_MAP_NORMALIZED
);
6179 struct isl_basic_set
*isl_basic_set_normalize(struct isl_basic_set
*bset
)
6181 return (struct isl_basic_set
*)isl_basic_map_normalize(
6182 (struct isl_basic_map
*)bset
);
6185 int isl_basic_map_fast_cmp(const __isl_keep isl_basic_map
*bmap1
,
6186 const __isl_keep isl_basic_map
*bmap2
)
6193 if (isl_basic_map_n_param(bmap1
) != isl_basic_map_n_param(bmap2
))
6194 return isl_basic_map_n_param(bmap1
) - isl_basic_map_n_param(bmap2
);
6195 if (isl_basic_map_n_in(bmap1
) != isl_basic_map_n_in(bmap2
))
6196 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
6197 if (isl_basic_map_n_out(bmap1
) != isl_basic_map_n_out(bmap2
))
6198 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
6199 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
) &&
6200 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
6202 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
))
6204 if (ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
6206 if (bmap1
->n_eq
!= bmap2
->n_eq
)
6207 return bmap1
->n_eq
- bmap2
->n_eq
;
6208 if (bmap1
->n_ineq
!= bmap2
->n_ineq
)
6209 return bmap1
->n_ineq
- bmap2
->n_ineq
;
6210 if (bmap1
->n_div
!= bmap2
->n_div
)
6211 return bmap1
->n_div
- bmap2
->n_div
;
6212 total
= isl_basic_map_total_dim(bmap1
);
6213 for (i
= 0; i
< bmap1
->n_eq
; ++i
) {
6214 cmp
= isl_seq_cmp(bmap1
->eq
[i
], bmap2
->eq
[i
], 1+total
);
6218 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
6219 cmp
= isl_seq_cmp(bmap1
->ineq
[i
], bmap2
->ineq
[i
], 1+total
);
6223 for (i
= 0; i
< bmap1
->n_div
; ++i
) {
6224 cmp
= isl_seq_cmp(bmap1
->div
[i
], bmap2
->div
[i
], 1+1+total
);
6231 int isl_basic_map_fast_is_equal(__isl_keep isl_basic_map
*bmap1
,
6232 __isl_keep isl_basic_map
*bmap2
)
6234 return isl_basic_map_fast_cmp(bmap1
, bmap2
) == 0;
6237 int isl_basic_set_fast_is_equal(__isl_keep isl_basic_set
*bset1
,
6238 __isl_keep isl_basic_set
*bset2
)
6240 return isl_basic_map_fast_is_equal((isl_basic_map
*)bset1
,
6241 (isl_basic_map
*)bset2
);
6244 static int qsort_bmap_cmp(const void *p1
, const void *p2
)
6246 const struct isl_basic_map
*bmap1
= *(const struct isl_basic_map
**)p1
;
6247 const struct isl_basic_map
*bmap2
= *(const struct isl_basic_map
**)p2
;
6249 return isl_basic_map_fast_cmp(bmap1
, bmap2
);
6252 /* We normalize in place, but if anything goes wrong we need
6253 * to return NULL, so we need to make sure we don't change the
6254 * meaning of any possible other copies of map.
6256 struct isl_map
*isl_map_normalize(struct isl_map
*map
)
6259 struct isl_basic_map
*bmap
;
6263 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
))
6265 for (i
= 0; i
< map
->n
; ++i
) {
6266 bmap
= isl_basic_map_normalize(isl_basic_map_copy(map
->p
[i
]));
6269 isl_basic_map_free(map
->p
[i
]);
6272 qsort(map
->p
, map
->n
, sizeof(struct isl_basic_map
*), qsort_bmap_cmp
);
6273 ISL_F_SET(map
, ISL_MAP_NORMALIZED
);
6274 map
= isl_map_remove_empty_parts(map
);
6277 for (i
= map
->n
- 1; i
>= 1; --i
) {
6278 if (!isl_basic_map_fast_is_equal(map
->p
[i
-1], map
->p
[i
]))
6280 isl_basic_map_free(map
->p
[i
-1]);
6281 for (j
= i
; j
< map
->n
; ++j
)
6282 map
->p
[j
-1] = map
->p
[j
];
6292 struct isl_set
*isl_set_normalize(struct isl_set
*set
)
6294 return (struct isl_set
*)isl_map_normalize((struct isl_map
*)set
);
6297 int isl_map_fast_is_equal(struct isl_map
*map1
, struct isl_map
*map2
)
6307 if (!isl_dim_equal(map1
->dim
, map2
->dim
))
6310 map1
= isl_map_copy(map1
);
6311 map2
= isl_map_copy(map2
);
6312 map1
= isl_map_normalize(map1
);
6313 map2
= isl_map_normalize(map2
);
6316 equal
= map1
->n
== map2
->n
;
6317 for (i
= 0; equal
&& i
< map1
->n
; ++i
) {
6318 equal
= isl_basic_map_fast_is_equal(map1
->p
[i
], map2
->p
[i
]);
6331 int isl_set_fast_is_equal(struct isl_set
*set1
, struct isl_set
*set2
)
6333 return isl_map_fast_is_equal((struct isl_map
*)set1
,
6334 (struct isl_map
*)set2
);
6337 /* Return an interval that ranges from min to max (inclusive)
6339 struct isl_basic_set
*isl_basic_set_interval(struct isl_ctx
*ctx
,
6340 isl_int min
, isl_int max
)
6343 struct isl_basic_set
*bset
= NULL
;
6345 bset
= isl_basic_set_alloc(ctx
, 0, 1, 0, 0, 2);
6349 k
= isl_basic_set_alloc_inequality(bset
);
6352 isl_int_set_si(bset
->ineq
[k
][1], 1);
6353 isl_int_neg(bset
->ineq
[k
][0], min
);
6355 k
= isl_basic_set_alloc_inequality(bset
);
6358 isl_int_set_si(bset
->ineq
[k
][1], -1);
6359 isl_int_set(bset
->ineq
[k
][0], max
);
6363 isl_basic_set_free(bset
);
6367 /* Return the Cartesian product of the basic sets in list (in the given order).
6369 struct isl_basic_set
*isl_basic_set_product(struct isl_basic_set_list
*list
)
6377 struct isl_basic_set
*product
= NULL
;
6381 isl_assert(list
->ctx
, list
->n
> 0, goto error
);
6382 isl_assert(list
->ctx
, list
->p
[0], goto error
);
6383 nparam
= isl_basic_set_n_param(list
->p
[0]);
6384 dim
= isl_basic_set_n_dim(list
->p
[0]);
6385 extra
= list
->p
[0]->n_div
;
6386 n_eq
= list
->p
[0]->n_eq
;
6387 n_ineq
= list
->p
[0]->n_ineq
;
6388 for (i
= 1; i
< list
->n
; ++i
) {
6389 isl_assert(list
->ctx
, list
->p
[i
], goto error
);
6390 isl_assert(list
->ctx
,
6391 nparam
== isl_basic_set_n_param(list
->p
[i
]), goto error
);
6392 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
6393 extra
+= list
->p
[i
]->n_div
;
6394 n_eq
+= list
->p
[i
]->n_eq
;
6395 n_ineq
+= list
->p
[i
]->n_ineq
;
6397 product
= isl_basic_set_alloc(list
->ctx
, nparam
, dim
, extra
,
6402 for (i
= 0; i
< list
->n
; ++i
) {
6403 isl_basic_set_add_constraints(product
,
6404 isl_basic_set_copy(list
->p
[i
]), dim
);
6405 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
6407 isl_basic_set_list_free(list
);
6410 isl_basic_set_free(product
);
6411 isl_basic_set_list_free(list
);
6415 struct isl_basic_map
*isl_basic_map_product(
6416 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
6418 struct isl_dim
*dim_result
= NULL
;
6419 struct isl_basic_map
*bmap
;
6420 unsigned in1
, in2
, out1
, out2
, nparam
, total
, pos
;
6421 struct isl_dim_map
*dim_map1
, *dim_map2
;
6423 if (!bmap1
|| !bmap2
)
6426 isl_assert(bmap1
->ctx
, isl_dim_match(bmap1
->dim
, isl_dim_param
,
6427 bmap2
->dim
, isl_dim_param
), goto error
);
6428 dim_result
= isl_dim_product(isl_dim_copy(bmap1
->dim
),
6429 isl_dim_copy(bmap2
->dim
));
6431 in1
= isl_basic_map_n_in(bmap1
);
6432 in2
= isl_basic_map_n_in(bmap2
);
6433 out1
= isl_basic_map_n_out(bmap1
);
6434 out2
= isl_basic_map_n_out(bmap2
);
6435 nparam
= isl_basic_map_n_param(bmap1
);
6437 total
= nparam
+ in1
+ in2
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
6438 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
6439 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
6440 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
6441 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
6442 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
6443 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
6444 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
6445 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
6446 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
6447 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
6449 bmap
= isl_basic_map_alloc_dim(dim_result
,
6450 bmap1
->n_div
+ bmap2
->n_div
,
6451 bmap1
->n_eq
+ bmap2
->n_eq
,
6452 bmap1
->n_ineq
+ bmap2
->n_ineq
);
6453 bmap
= add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
6454 bmap
= add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
6455 bmap
= isl_basic_map_simplify(bmap
);
6456 return isl_basic_map_finalize(bmap
);
6458 isl_basic_map_free(bmap1
);
6459 isl_basic_map_free(bmap2
);
6463 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
6465 struct isl_map
*isl_map_product(struct isl_map
*map1
, struct isl_map
*map2
)
6468 struct isl_map
*result
;
6474 isl_assert(map1
->ctx
, isl_dim_match(map1
->dim
, isl_dim_param
,
6475 map2
->dim
, isl_dim_param
), goto error
);
6477 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
6478 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
6479 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
6481 result
= isl_map_alloc_dim(isl_dim_product(isl_dim_copy(map1
->dim
),
6482 isl_dim_copy(map2
->dim
)),
6483 map1
->n
* map2
->n
, flags
);
6486 for (i
= 0; i
< map1
->n
; ++i
)
6487 for (j
= 0; j
< map2
->n
; ++j
) {
6488 struct isl_basic_map
*part
;
6489 part
= isl_basic_map_product(
6490 isl_basic_map_copy(map1
->p
[i
]),
6491 isl_basic_map_copy(map2
->p
[j
]));
6492 if (isl_basic_map_is_empty(part
))
6493 isl_basic_map_free(part
);
6495 result
= isl_map_add_basic_map(result
, part
);
6508 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
6510 __isl_give isl_map
*isl_map_flat_product(__isl_take isl_map
*map1
,
6511 __isl_take isl_map
*map2
)
6515 prod
= isl_map_product(map1
, map2
);
6516 prod
= isl_map_flatten(prod
);
6520 /* Given two set A and B, construct its Cartesian product A x B.
6522 struct isl_set
*isl_set_product(struct isl_set
*set1
, struct isl_set
*set2
)
6524 return (struct isl_set
*)isl_map_product((struct isl_map
*)set1
,
6525 (struct isl_map
*)set2
);
6528 __isl_give isl_set
*isl_set_flat_product(__isl_take isl_set
*set1
,
6529 __isl_take isl_set
*set2
)
6531 return (isl_set
*)isl_map_flat_product((isl_map
*)set1
, (isl_map
*)set2
);
6534 uint32_t isl_basic_set_get_hash(struct isl_basic_set
*bset
)
6537 uint32_t hash
= isl_hash_init();
6542 bset
= isl_basic_set_copy(bset
);
6543 bset
= isl_basic_set_normalize(bset
);
6546 total
= isl_basic_set_total_dim(bset
);
6547 isl_hash_byte(hash
, bset
->n_eq
& 0xFF);
6548 for (i
= 0; i
< bset
->n_eq
; ++i
) {
6550 c_hash
= isl_seq_get_hash(bset
->eq
[i
], 1 + total
);
6551 isl_hash_hash(hash
, c_hash
);
6553 isl_hash_byte(hash
, bset
->n_ineq
& 0xFF);
6554 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
6556 c_hash
= isl_seq_get_hash(bset
->ineq
[i
], 1 + total
);
6557 isl_hash_hash(hash
, c_hash
);
6559 isl_hash_byte(hash
, bset
->n_div
& 0xFF);
6560 for (i
= 0; i
< bset
->n_div
; ++i
) {
6562 if (isl_int_is_zero(bset
->div
[i
][0]))
6564 isl_hash_byte(hash
, i
& 0xFF);
6565 c_hash
= isl_seq_get_hash(bset
->div
[i
], 1 + 1 + total
);
6566 isl_hash_hash(hash
, c_hash
);
6568 isl_basic_set_free(bset
);
6572 uint32_t isl_set_get_hash(struct isl_set
*set
)
6579 set
= isl_set_copy(set
);
6580 set
= isl_set_normalize(set
);
6584 hash
= isl_hash_init();
6585 for (i
= 0; i
< set
->n
; ++i
) {
6587 bset_hash
= isl_basic_set_get_hash(set
->p
[i
]);
6588 isl_hash_hash(hash
, bset_hash
);
6596 /* Check if the value for dimension dim is completely determined
6597 * by the values of the other parameters and variables.
6598 * That is, check if dimension dim is involved in an equality.
6600 int isl_basic_set_dim_is_unique(struct isl_basic_set
*bset
, unsigned dim
)
6607 nparam
= isl_basic_set_n_param(bset
);
6608 for (i
= 0; i
< bset
->n_eq
; ++i
)
6609 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ dim
]))
6614 /* Check if the value for dimension dim is completely determined
6615 * by the values of the other parameters and variables.
6616 * That is, check if dimension dim is involved in an equality
6617 * for each of the subsets.
6619 int isl_set_dim_is_unique(struct isl_set
*set
, unsigned dim
)
6625 for (i
= 0; i
< set
->n
; ++i
) {
6627 unique
= isl_basic_set_dim_is_unique(set
->p
[i
], dim
);
6634 int isl_map_foreach_basic_map(__isl_keep isl_map
*map
,
6635 int (*fn
)(__isl_take isl_basic_map
*bmap
, void *user
), void *user
)
6642 for (i
= 0; i
< map
->n
; ++i
)
6643 if (fn(isl_basic_map_copy(map
->p
[i
]), user
) < 0)
6649 int isl_set_foreach_basic_set(__isl_keep isl_set
*set
,
6650 int (*fn
)(__isl_take isl_basic_set
*bset
, void *user
), void *user
)
6657 for (i
= 0; i
< set
->n
; ++i
)
6658 if (fn(isl_basic_set_copy(set
->p
[i
]), user
) < 0)
6664 __isl_give isl_basic_set
*isl_basic_set_lift(__isl_take isl_basic_set
*bset
)
6666 struct isl_dim
*dim
;
6671 if (bset
->n_div
== 0)
6674 bset
= isl_basic_set_cow(bset
);
6678 dim
= isl_basic_set_get_dim(bset
);
6679 dim
= isl_dim_add(dim
, isl_dim_set
, bset
->n_div
);
6682 isl_dim_free(bset
->dim
);
6686 bset
= isl_basic_set_finalize(bset
);
6690 isl_basic_set_free(bset
);
6694 __isl_give isl_set
*isl_set_lift(__isl_take isl_set
*set
)
6697 struct isl_dim
*dim
;
6700 set
= isl_set_align_divs(set
);
6704 if (set
->n
== 0 || set
->p
[0]->n_div
== 0)
6707 set
= isl_set_cow(set
);
6711 n_div
= set
->p
[0]->n_div
;
6712 dim
= isl_set_get_dim(set
);
6713 dim
= isl_dim_add(dim
, isl_dim_set
, n_div
);
6716 isl_dim_free(set
->dim
);
6719 for (i
= 0; i
< set
->n
; ++i
) {
6720 set
->p
[i
] = isl_basic_set_lift(set
->p
[i
]);
6731 __isl_give isl_map
*isl_set_lifting(__isl_take isl_set
*set
)
6733 struct isl_dim
*dim
;
6734 struct isl_basic_map
*bmap
;
6741 set
= isl_set_align_divs(set
);
6746 dim
= isl_set_get_dim(set
);
6747 if (set
->n
== 0 || set
->p
[0]->n_div
== 0) {
6749 return isl_map_identity(dim
);
6752 n_div
= set
->p
[0]->n_div
;
6753 dim
= isl_dim_map(dim
);
6754 n_param
= isl_dim_size(dim
, isl_dim_param
);
6755 n_set
= isl_dim_size(dim
, isl_dim_in
);
6756 dim
= isl_dim_extend(dim
, n_param
, n_set
, n_set
+ n_div
);
6757 bmap
= isl_basic_map_alloc_dim(dim
, 0, n_set
, 2 * n_div
);
6758 for (i
= 0; i
< n_set
; ++i
)
6759 bmap
= var_equal(bmap
, i
);
6761 total
= n_param
+ n_set
+ n_set
+ n_div
;
6762 for (i
= 0; i
< n_div
; ++i
) {
6763 k
= isl_basic_map_alloc_inequality(bmap
);
6766 isl_seq_cpy(bmap
->ineq
[k
], set
->p
[0]->div
[i
]+1, 1+n_param
);
6767 isl_seq_clr(bmap
->ineq
[k
]+1+n_param
, n_set
);
6768 isl_seq_cpy(bmap
->ineq
[k
]+1+n_param
+n_set
,
6769 set
->p
[0]->div
[i
]+1+1+n_param
, n_set
+ n_div
);
6770 isl_int_neg(bmap
->ineq
[k
][1+n_param
+n_set
+n_set
+i
],
6771 set
->p
[0]->div
[i
][0]);
6773 l
= isl_basic_map_alloc_inequality(bmap
);
6776 isl_seq_neg(bmap
->ineq
[l
], bmap
->ineq
[k
], 1 + total
);
6777 isl_int_add(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0],
6778 set
->p
[0]->div
[i
][0]);
6779 isl_int_sub_ui(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], 1);
6783 return isl_map_from_basic_map(bmap
);
6786 isl_basic_map_free(bmap
);
6790 int isl_basic_set_size(__isl_keep isl_basic_set
*bset
)
6798 dim
= isl_basic_set_total_dim(bset
);
6799 size
+= bset
->n_eq
* (1 + dim
);
6800 size
+= bset
->n_ineq
* (1 + dim
);
6801 size
+= bset
->n_div
* (2 + dim
);
6806 int isl_set_size(__isl_keep isl_set
*set
)
6814 for (i
= 0; i
< set
->n
; ++i
)
6815 size
+= isl_basic_set_size(set
->p
[i
]);
6820 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
6821 enum isl_dim_type type
, unsigned pos
)
6829 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), return -1);
6831 pos
+= isl_basic_map_offset(bmap
, type
);
6833 for (i
= 0; i
< bmap
->n_eq
; ++i
)
6834 if (!isl_int_is_zero(bmap
->eq
[i
][pos
]))
6838 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
6839 int sgn
= isl_int_sgn(bmap
->ineq
[i
][pos
]);
6846 return lower
&& upper
;
6849 int isl_map_dim_is_bounded(__isl_keep isl_map
*map
,
6850 enum isl_dim_type type
, unsigned pos
)
6857 for (i
= 0; i
< map
->n
; ++i
) {
6859 bounded
= isl_basic_map_dim_is_bounded(map
->p
[i
], type
, pos
);
6860 if (bounded
< 0 || !bounded
)
6867 /* Return 1 if the specified dim is involved in both an upper bound
6868 * and a lower bound.
6870 int isl_set_dim_is_bounded(__isl_keep isl_set
*set
,
6871 enum isl_dim_type type
, unsigned pos
)
6873 return isl_map_dim_is_bounded((isl_map
*)set
, type
, pos
);
6876 /* For each of the "n" variables starting at "first", determine
6877 * the sign of the variable and put the results in the first "n"
6878 * elements of the array "signs".
6880 * 1 means that the variable is non-negative
6881 * -1 means that the variable is non-positive
6882 * 0 means the variable attains both positive and negative values.
6884 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set
*bset
,
6885 unsigned first
, unsigned n
, int *signs
)
6887 isl_vec
*bound
= NULL
;
6888 struct isl_tab
*tab
= NULL
;
6889 struct isl_tab_undo
*snap
;
6892 if (!bset
|| !signs
)
6895 bound
= isl_vec_alloc(bset
->ctx
, 1 + isl_basic_set_total_dim(bset
));
6896 tab
= isl_tab_from_basic_set(bset
);
6900 isl_seq_clr(bound
->el
, bound
->size
);
6901 isl_int_set_si(bound
->el
[0], -1);
6903 snap
= isl_tab_snap(tab
);
6904 for (i
= 0; i
< n
; ++i
) {
6907 isl_int_set_si(bound
->el
[1 + first
+ i
], -1);
6908 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
6911 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
6912 if (isl_tab_rollback(tab
, snap
) < 0)
6920 isl_int_set_si(bound
->el
[1 + first
+ i
], 1);
6921 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
6924 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
6925 if (isl_tab_rollback(tab
, snap
) < 0)
6928 signs
[i
] = empty
? -1 : 0;
6932 isl_vec_free(bound
);
6936 isl_vec_free(bound
);
6940 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set
*bset
,
6941 enum isl_dim_type type
, unsigned first
, unsigned n
, int *signs
)
6943 if (!bset
|| !signs
)
6945 isl_assert(bset
->ctx
, first
+ n
<= isl_basic_set_dim(bset
, type
),
6948 first
+= pos(bset
->dim
, type
) - 1;
6949 return isl_basic_set_vars_get_sign(bset
, first
, n
, signs
);
6952 /* Check if the given map is single-valued.
6957 * and check if the result is a subset of the identity mapping.
6959 int isl_map_is_single_valued(__isl_keep isl_map
*map
)
6965 test
= isl_map_reverse(isl_map_copy(map
));
6966 test
= isl_map_apply_range(test
, isl_map_copy(map
));
6968 id
= isl_map_identity(isl_dim_range(isl_map_get_dim(map
)));
6970 sv
= isl_map_is_subset(test
, id
);
6978 int isl_map_is_bijective(__isl_keep isl_map
*map
)
6982 sv
= isl_map_is_single_valued(map
);
6986 map
= isl_map_copy(map
);
6987 map
= isl_map_reverse(map
);
6988 sv
= isl_map_is_single_valued(map
);
6994 int isl_set_is_singleton(__isl_keep isl_set
*set
)
6996 return isl_map_is_single_valued((isl_map
*)set
);
6999 int isl_map_is_translation(__isl_keep isl_map
*map
)
7004 delta
= isl_map_deltas(isl_map_copy(map
));
7005 ok
= isl_set_is_singleton(delta
);
7006 isl_set_free(delta
);
7011 static int unique(isl_int
*p
, unsigned pos
, unsigned len
)
7013 if (isl_seq_first_non_zero(p
, pos
) != -1)
7015 if (isl_seq_first_non_zero(p
+ pos
+ 1, len
- pos
- 1) != -1)
7020 int isl_basic_set_is_box(__isl_keep isl_basic_set
*bset
)
7029 if (isl_basic_set_dim(bset
, isl_dim_div
) != 0)
7032 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
7033 ovar
= isl_dim_offset(bset
->dim
, isl_dim_set
);
7034 for (j
= 0; j
< nvar
; ++j
) {
7035 int lower
= 0, upper
= 0;
7036 for (i
= 0; i
< bset
->n_eq
; ++i
) {
7037 if (isl_int_is_zero(bset
->eq
[i
][1 + ovar
+ j
]))
7039 if (!unique(bset
->eq
[i
] + 1 + ovar
, j
, nvar
))
7045 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
7046 if (isl_int_is_zero(bset
->ineq
[i
][1 + ovar
+ j
]))
7048 if (!unique(bset
->ineq
[i
] + 1 + ovar
, j
, nvar
))
7050 if (isl_int_is_pos(bset
->ineq
[i
][1 + ovar
+ j
]))
7055 if (!lower
|| !upper
)
7062 int isl_set_is_box(__isl_keep isl_set
*set
)
7069 return isl_basic_set_is_box(set
->p
[0]);
7072 int isl_basic_set_is_wrapping(__isl_keep isl_basic_set
*bset
)
7077 return isl_dim_is_wrapping(bset
->dim
);
7080 int isl_set_is_wrapping(__isl_keep isl_set
*set
)
7085 return isl_dim_is_wrapping(set
->dim
);
7088 __isl_give isl_basic_set
*isl_basic_map_wrap(__isl_take isl_basic_map
*bmap
)
7090 bmap
= isl_basic_map_cow(bmap
);
7094 bmap
->dim
= isl_dim_wrap(bmap
->dim
);
7098 bmap
= isl_basic_map_finalize(bmap
);
7100 return (isl_basic_set
*)bmap
;
7102 isl_basic_map_free(bmap
);
7106 __isl_give isl_set
*isl_map_wrap(__isl_take isl_map
*map
)
7110 map
= isl_map_cow(map
);
7114 for (i
= 0; i
< map
->n
; ++i
) {
7115 map
->p
[i
] = (isl_basic_map
*)isl_basic_map_wrap(map
->p
[i
]);
7119 map
->dim
= isl_dim_wrap(map
->dim
);
7123 return (isl_set
*)map
;
7129 __isl_give isl_basic_map
*isl_basic_set_unwrap(__isl_take isl_basic_set
*bset
)
7131 bset
= isl_basic_set_cow(bset
);
7135 bset
->dim
= isl_dim_unwrap(bset
->dim
);
7139 bset
= isl_basic_set_finalize(bset
);
7141 return (isl_basic_map
*)bset
;
7143 isl_basic_set_free(bset
);
7147 __isl_give isl_map
*isl_set_unwrap(__isl_take isl_set
*set
)
7154 if (!isl_set_is_wrapping(set
))
7155 isl_die(set
->ctx
, isl_error_invalid
, "not a wrapping set",
7158 set
= isl_set_cow(set
);
7162 for (i
= 0; i
< set
->n
; ++i
) {
7163 set
->p
[i
] = (isl_basic_set
*)isl_basic_set_unwrap(set
->p
[i
]);
7168 set
->dim
= isl_dim_unwrap(set
->dim
);
7172 return (isl_map
*)set
;
7178 __isl_give isl_basic_map
*isl_basic_map_reset(__isl_take isl_basic_map
*bmap
,
7179 enum isl_dim_type type
)
7184 if (!isl_dim_is_named_or_nested(bmap
->dim
, type
))
7187 bmap
= isl_basic_map_cow(bmap
);
7191 bmap
->dim
= isl_dim_reset(bmap
->dim
, type
);
7195 bmap
= isl_basic_map_finalize(bmap
);
7199 isl_basic_map_free(bmap
);
7203 __isl_give isl_map
*isl_map_reset(__isl_take isl_map
*map
,
7204 enum isl_dim_type type
)
7211 if (!isl_dim_is_named_or_nested(map
->dim
, type
))
7214 map
= isl_map_cow(map
);
7218 for (i
= 0; i
< map
->n
; ++i
) {
7219 map
->p
[i
] = isl_basic_map_reset(map
->p
[i
], type
);
7223 map
->dim
= isl_dim_reset(map
->dim
, type
);
7233 __isl_give isl_basic_map
*isl_basic_map_flatten(__isl_take isl_basic_map
*bmap
)
7238 if (!bmap
->dim
->nested
[0] && !bmap
->dim
->nested
[1])
7241 bmap
= isl_basic_map_cow(bmap
);
7245 bmap
->dim
= isl_dim_flatten(bmap
->dim
);
7249 bmap
= isl_basic_map_finalize(bmap
);
7253 isl_basic_map_free(bmap
);
7257 __isl_give isl_map
*isl_map_flatten(__isl_take isl_map
*map
)
7264 if (!map
->dim
->nested
[0] && !map
->dim
->nested
[1])
7267 map
= isl_map_cow(map
);
7271 for (i
= 0; i
< map
->n
; ++i
) {
7272 map
->p
[i
] = isl_basic_map_flatten(map
->p
[i
]);
7276 map
->dim
= isl_dim_flatten(map
->dim
);