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_reordering.h>
27 #include "isl_sample.h"
31 /* Maps dst positions to src positions */
37 static struct isl_dim_map
*isl_dim_map_alloc(struct isl_ctx
*ctx
, unsigned len
)
40 struct isl_dim_map
*dim_map
;
41 dim_map
= isl_alloc(ctx
, struct isl_dim_map
,
42 sizeof(struct isl_dim_map
) + len
* sizeof(int));
45 dim_map
->len
= 1 + len
;
47 for (i
= 0; i
< len
; ++i
)
48 dim_map
->pos
[1 + i
] = -1;
52 static unsigned n(struct isl_dim
*dim
, enum isl_dim_type type
)
55 case isl_dim_param
: return dim
->nparam
;
56 case isl_dim_in
: return dim
->n_in
;
57 case isl_dim_out
: return dim
->n_out
;
58 case isl_dim_all
: return dim
->nparam
+ dim
->n_in
+ dim
->n_out
;
63 static unsigned pos(struct isl_dim
*dim
, enum isl_dim_type type
)
66 case isl_dim_param
: return 1;
67 case isl_dim_in
: return 1 + dim
->nparam
;
68 case isl_dim_out
: return 1 + dim
->nparam
+ dim
->n_in
;
73 static void isl_dim_map_dim_range(struct isl_dim_map
*dim_map
,
74 struct isl_dim
*dim
, enum isl_dim_type type
,
75 unsigned first
, unsigned n
, unsigned dst_pos
)
83 src_pos
= pos(dim
, type
);
84 for (i
= 0; i
< n
; ++i
)
85 dim_map
->pos
[1 + dst_pos
+ i
] = src_pos
+ first
+ i
;
88 static void isl_dim_map_dim(struct isl_dim_map
*dim_map
, struct isl_dim
*dim
,
89 enum isl_dim_type type
, unsigned dst_pos
)
91 isl_dim_map_dim_range(dim_map
, dim
, type
, 0, n(dim
, type
), dst_pos
);
94 static void isl_dim_map_div(struct isl_dim_map
*dim_map
,
95 struct isl_basic_map
*bmap
, unsigned dst_pos
)
100 if (!dim_map
|| !bmap
)
103 src_pos
= 1 + isl_dim_total(bmap
->dim
);
104 for (i
= 0; i
< bmap
->n_div
; ++i
)
105 dim_map
->pos
[1 + dst_pos
+ i
] = src_pos
+ i
;
108 static void isl_dim_map_dump(struct isl_dim_map
*dim_map
)
112 for (i
= 0; i
< dim_map
->len
; ++i
)
113 fprintf(stderr
, "%d -> %d; ", i
, dim_map
->pos
[i
]);
114 fprintf(stderr
, "\n");
117 unsigned isl_basic_map_dim(const struct isl_basic_map
*bmap
,
118 enum isl_dim_type type
)
125 case isl_dim_out
: return isl_dim_size(bmap
->dim
, type
);
126 case isl_dim_div
: return bmap
->n_div
;
127 case isl_dim_all
: return isl_basic_map_total_dim(bmap
);
132 unsigned isl_map_dim(const struct isl_map
*map
, enum isl_dim_type type
)
134 return map
? n(map
->dim
, type
) : 0;
137 unsigned isl_set_dim(const struct isl_set
*set
, enum isl_dim_type type
)
139 return set
? n(set
->dim
, type
) : 0;
142 unsigned isl_basic_map_offset(struct isl_basic_map
*bmap
,
143 enum isl_dim_type type
)
145 struct isl_dim
*dim
= bmap
->dim
;
147 case isl_dim_param
: return 1;
148 case isl_dim_in
: return 1 + dim
->nparam
;
149 case isl_dim_out
: return 1 + dim
->nparam
+ dim
->n_in
;
150 case isl_dim_div
: return 1 + dim
->nparam
+ dim
->n_in
+ dim
->n_out
;
155 static unsigned map_offset(struct isl_map
*map
, enum isl_dim_type type
)
157 return pos(map
->dim
, type
);
160 unsigned isl_basic_set_dim(const struct isl_basic_set
*bset
,
161 enum isl_dim_type type
)
163 return isl_basic_map_dim((const struct isl_basic_map
*)bset
, type
);
166 unsigned isl_basic_set_n_dim(const struct isl_basic_set
*bset
)
168 return isl_basic_set_dim(bset
, isl_dim_set
);
171 unsigned isl_basic_set_n_param(const struct isl_basic_set
*bset
)
173 return isl_basic_set_dim(bset
, isl_dim_param
);
176 unsigned isl_basic_set_total_dim(const struct isl_basic_set
*bset
)
178 return isl_dim_total(bset
->dim
) + bset
->n_div
;
181 unsigned isl_set_n_dim(const struct isl_set
*set
)
183 return isl_set_dim(set
, isl_dim_set
);
186 unsigned isl_set_n_param(const struct isl_set
*set
)
188 return isl_set_dim(set
, isl_dim_param
);
191 unsigned isl_basic_map_n_in(const struct isl_basic_map
*bmap
)
193 return bmap
? bmap
->dim
->n_in
: 0;
196 unsigned isl_basic_map_n_out(const struct isl_basic_map
*bmap
)
198 return bmap
? bmap
->dim
->n_out
: 0;
201 unsigned isl_basic_map_n_param(const struct isl_basic_map
*bmap
)
203 return bmap
? bmap
->dim
->nparam
: 0;
206 unsigned isl_basic_map_n_div(const struct isl_basic_map
*bmap
)
208 return bmap
? bmap
->n_div
: 0;
211 unsigned isl_basic_map_total_dim(const struct isl_basic_map
*bmap
)
213 return bmap
? isl_dim_total(bmap
->dim
) + bmap
->n_div
: 0;
216 unsigned isl_map_n_in(const struct isl_map
*map
)
218 return map
->dim
->n_in
;
221 unsigned isl_map_n_out(const struct isl_map
*map
)
223 return map
->dim
->n_out
;
226 unsigned isl_map_n_param(const struct isl_map
*map
)
228 return map
->dim
->nparam
;
231 int isl_map_compatible_domain(struct isl_map
*map
, struct isl_set
*set
)
236 m
= isl_dim_match(map
->dim
, isl_dim_param
, set
->dim
, isl_dim_param
);
239 return isl_dim_tuple_match(map
->dim
, isl_dim_in
, set
->dim
, isl_dim_set
);
242 int isl_basic_map_compatible_domain(struct isl_basic_map
*bmap
,
243 struct isl_basic_set
*bset
)
248 m
= isl_dim_match(bmap
->dim
, isl_dim_param
, bset
->dim
, isl_dim_param
);
251 return isl_dim_tuple_match(bmap
->dim
, isl_dim_in
, bset
->dim
, isl_dim_set
);
254 int isl_basic_map_compatible_range(struct isl_basic_map
*bmap
,
255 struct isl_basic_set
*bset
)
260 m
= isl_dim_match(bmap
->dim
, isl_dim_param
, bset
->dim
, isl_dim_param
);
263 return isl_dim_tuple_match(bmap
->dim
, isl_dim_out
, bset
->dim
, isl_dim_set
);
266 isl_ctx
*isl_basic_map_get_ctx(__isl_keep isl_basic_map
*bmap
)
268 return bmap
? bmap
->ctx
: NULL
;
271 isl_ctx
*isl_basic_set_get_ctx(__isl_keep isl_basic_set
*bset
)
273 return bset
? bset
->ctx
: NULL
;
276 isl_ctx
*isl_map_get_ctx(__isl_keep isl_map
*map
)
278 return map
? map
->ctx
: NULL
;
281 isl_ctx
*isl_set_get_ctx(__isl_keep isl_set
*set
)
283 return set
? set
->ctx
: NULL
;
286 struct isl_dim
*isl_basic_map_get_dim(struct isl_basic_map
*bmap
)
290 return isl_dim_copy(bmap
->dim
);
293 struct isl_dim
*isl_basic_set_get_dim(struct isl_basic_set
*bset
)
297 return isl_dim_copy(bset
->dim
);
300 struct isl_dim
*isl_map_get_dim(struct isl_map
*map
)
304 return isl_dim_copy(map
->dim
);
307 struct isl_dim
*isl_set_get_dim(struct isl_set
*set
)
311 return isl_dim_copy(set
->dim
);
314 __isl_give isl_basic_map
*isl_basic_map_set_tuple_name(
315 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
, const char *s
)
317 bmap
= isl_basic_map_cow(bmap
);
320 bmap
->dim
= isl_dim_set_tuple_name(bmap
->dim
, type
, s
);
325 isl_basic_map_free(bmap
);
329 __isl_give isl_map
*isl_map_set_tuple_name(__isl_take isl_map
*map
,
330 enum isl_dim_type type
, const char *s
)
334 map
= isl_map_cow(map
);
338 map
->dim
= isl_dim_set_tuple_name(map
->dim
, type
, s
);
342 for (i
= 0; i
< map
->n
; ++i
) {
343 map
->p
[i
] = isl_basic_map_set_tuple_name(map
->p
[i
], type
, s
);
354 __isl_give isl_basic_map
*isl_basic_map_set_dim_name(
355 __isl_take isl_basic_map
*bmap
,
356 enum isl_dim_type type
, unsigned pos
, const char *s
)
360 bmap
->dim
= isl_dim_set_name(bmap
->dim
, type
, pos
, s
);
365 isl_basic_map_free(bmap
);
369 __isl_give isl_map
*isl_map_set_dim_name(__isl_take isl_map
*map
,
370 enum isl_dim_type type
, unsigned pos
, const char *s
)
377 map
->dim
= isl_dim_set_name(map
->dim
, type
, pos
, s
);
381 for (i
= 0; i
< map
->n
; ++i
) {
382 map
->p
[i
] = isl_basic_map_set_dim_name(map
->p
[i
], type
, pos
, s
);
393 __isl_give isl_basic_set
*isl_basic_set_set_dim_name(
394 __isl_take isl_basic_set
*bset
,
395 enum isl_dim_type type
, unsigned pos
, const char *s
)
397 return (isl_basic_set
*)isl_basic_map_set_dim_name(
398 (isl_basic_map
*)bset
, type
, pos
, s
);
401 __isl_give isl_set
*isl_set_set_dim_name(__isl_take isl_set
*set
,
402 enum isl_dim_type type
, unsigned pos
, const char *s
)
404 return (isl_set
*)isl_map_set_dim_name((isl_map
*)set
, type
, pos
, s
);
407 int isl_basic_map_is_rational(__isl_keep isl_basic_map
*bmap
)
411 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
414 static struct isl_basic_map
*basic_map_init(struct isl_ctx
*ctx
,
415 struct isl_basic_map
*bmap
, unsigned extra
,
416 unsigned n_eq
, unsigned n_ineq
)
419 size_t row_size
= 1 + isl_dim_total(bmap
->dim
) + extra
;
424 bmap
->block
= isl_blk_alloc(ctx
, (n_ineq
+ n_eq
) * row_size
);
425 if (isl_blk_is_error(bmap
->block
))
428 bmap
->ineq
= isl_alloc_array(ctx
, isl_int
*, n_ineq
+ n_eq
);
433 bmap
->block2
= isl_blk_empty();
436 bmap
->block2
= isl_blk_alloc(ctx
, extra
* (1 + row_size
));
437 if (isl_blk_is_error(bmap
->block2
))
440 bmap
->div
= isl_alloc_array(ctx
, isl_int
*, extra
);
445 for (i
= 0; i
< n_ineq
+ n_eq
; ++i
)
446 bmap
->ineq
[i
] = bmap
->block
.data
+ i
* row_size
;
448 for (i
= 0; i
< extra
; ++i
)
449 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + row_size
);
453 bmap
->c_size
= n_eq
+ n_ineq
;
454 bmap
->eq
= bmap
->ineq
+ n_ineq
;
463 isl_basic_map_free(bmap
);
467 struct isl_basic_set
*isl_basic_set_alloc(struct isl_ctx
*ctx
,
468 unsigned nparam
, unsigned dim
, unsigned extra
,
469 unsigned n_eq
, unsigned n_ineq
)
471 struct isl_basic_map
*bmap
;
472 bmap
= isl_basic_map_alloc(ctx
, nparam
, 0, dim
, extra
, n_eq
, n_ineq
);
473 return (struct isl_basic_set
*)bmap
;
476 struct isl_basic_set
*isl_basic_set_alloc_dim(struct isl_dim
*dim
,
477 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
479 struct isl_basic_map
*bmap
;
482 isl_assert(dim
->ctx
, dim
->n_in
== 0, return NULL
);
483 bmap
= isl_basic_map_alloc_dim(dim
, extra
, n_eq
, n_ineq
);
484 return (struct isl_basic_set
*)bmap
;
487 struct isl_basic_map
*isl_basic_map_alloc_dim(struct isl_dim
*dim
,
488 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
490 struct isl_basic_map
*bmap
;
494 bmap
= isl_calloc_type(dim
->ctx
, struct isl_basic_map
);
499 return basic_map_init(dim
->ctx
, bmap
, extra
, n_eq
, n_ineq
);
505 struct isl_basic_map
*isl_basic_map_alloc(struct isl_ctx
*ctx
,
506 unsigned nparam
, unsigned in
, unsigned out
, unsigned extra
,
507 unsigned n_eq
, unsigned n_ineq
)
509 struct isl_basic_map
*bmap
;
512 dim
= isl_dim_alloc(ctx
, nparam
, in
, out
);
516 bmap
= isl_basic_map_alloc_dim(dim
, extra
, n_eq
, n_ineq
);
520 static void dup_constraints(
521 struct isl_basic_map
*dst
, struct isl_basic_map
*src
)
524 unsigned total
= isl_basic_map_total_dim(src
);
526 for (i
= 0; i
< src
->n_eq
; ++i
) {
527 int j
= isl_basic_map_alloc_equality(dst
);
528 isl_seq_cpy(dst
->eq
[j
], src
->eq
[i
], 1+total
);
531 for (i
= 0; i
< src
->n_ineq
; ++i
) {
532 int j
= isl_basic_map_alloc_inequality(dst
);
533 isl_seq_cpy(dst
->ineq
[j
], src
->ineq
[i
], 1+total
);
536 for (i
= 0; i
< src
->n_div
; ++i
) {
537 int j
= isl_basic_map_alloc_div(dst
);
538 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
);
540 ISL_F_SET(dst
, ISL_BASIC_SET_FINAL
);
543 struct isl_basic_map
*isl_basic_map_dup(struct isl_basic_map
*bmap
)
545 struct isl_basic_map
*dup
;
549 dup
= isl_basic_map_alloc_dim(isl_dim_copy(bmap
->dim
),
550 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
553 dup_constraints(dup
, bmap
);
554 dup
->flags
= bmap
->flags
;
555 dup
->sample
= isl_vec_copy(bmap
->sample
);
559 struct isl_basic_set
*isl_basic_set_dup(struct isl_basic_set
*bset
)
561 struct isl_basic_map
*dup
;
563 dup
= isl_basic_map_dup((struct isl_basic_map
*)bset
);
564 return (struct isl_basic_set
*)dup
;
567 struct isl_basic_set
*isl_basic_set_copy(struct isl_basic_set
*bset
)
572 if (ISL_F_ISSET(bset
, ISL_BASIC_SET_FINAL
)) {
576 return isl_basic_set_dup(bset
);
579 struct isl_set
*isl_set_copy(struct isl_set
*set
)
588 struct isl_basic_map
*isl_basic_map_copy(struct isl_basic_map
*bmap
)
593 if (ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
)) {
597 bmap
= isl_basic_map_dup(bmap
);
599 ISL_F_SET(bmap
, ISL_BASIC_SET_FINAL
);
603 struct isl_map
*isl_map_copy(struct isl_map
*map
)
612 void isl_basic_map_free(struct isl_basic_map
*bmap
)
620 isl_ctx_deref(bmap
->ctx
);
622 isl_blk_free(bmap
->ctx
, bmap
->block2
);
624 isl_blk_free(bmap
->ctx
, bmap
->block
);
625 isl_vec_free(bmap
->sample
);
626 isl_dim_free(bmap
->dim
);
630 void isl_basic_set_free(struct isl_basic_set
*bset
)
632 isl_basic_map_free((struct isl_basic_map
*)bset
);
635 static int room_for_con(struct isl_basic_map
*bmap
, unsigned n
)
637 return bmap
->n_eq
+ bmap
->n_ineq
+ n
<= bmap
->c_size
;
640 int isl_basic_map_alloc_equality(struct isl_basic_map
*bmap
)
646 isl_assert(ctx
, room_for_con(bmap
, 1), return -1);
647 isl_assert(ctx
, (bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
<= bmap
->c_size
,
649 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
650 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
651 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
652 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
653 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
654 if ((bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
== bmap
->c_size
) {
656 int j
= isl_basic_map_alloc_inequality(bmap
);
660 bmap
->ineq
[j
] = bmap
->ineq
[bmap
->n_ineq
- 1];
661 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
668 isl_seq_clr(bmap
->eq
[bmap
->n_eq
] + 1 + isl_basic_map_total_dim(bmap
),
669 bmap
->extra
- bmap
->n_div
);
673 int isl_basic_set_alloc_equality(struct isl_basic_set
*bset
)
675 return isl_basic_map_alloc_equality((struct isl_basic_map
*)bset
);
678 int isl_basic_map_free_equality(struct isl_basic_map
*bmap
, unsigned n
)
682 isl_assert(bmap
->ctx
, n
<= bmap
->n_eq
, return -1);
687 int isl_basic_set_free_equality(struct isl_basic_set
*bset
, unsigned n
)
689 return isl_basic_map_free_equality((struct isl_basic_map
*)bset
, n
);
692 int isl_basic_map_drop_equality(struct isl_basic_map
*bmap
, unsigned pos
)
697 isl_assert(bmap
->ctx
, pos
< bmap
->n_eq
, return -1);
699 if (pos
!= bmap
->n_eq
- 1) {
701 bmap
->eq
[pos
] = bmap
->eq
[bmap
->n_eq
- 1];
702 bmap
->eq
[bmap
->n_eq
- 1] = t
;
708 int isl_basic_set_drop_equality(struct isl_basic_set
*bset
, unsigned pos
)
710 return isl_basic_map_drop_equality((struct isl_basic_map
*)bset
, pos
);
713 void isl_basic_map_inequality_to_equality(
714 struct isl_basic_map
*bmap
, unsigned pos
)
719 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
720 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
725 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
726 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
727 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
728 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
731 static int room_for_ineq(struct isl_basic_map
*bmap
, unsigned n
)
733 return bmap
->n_ineq
+ n
<= bmap
->eq
- bmap
->ineq
;
736 int isl_basic_map_alloc_inequality(struct isl_basic_map
*bmap
)
742 isl_assert(ctx
, room_for_ineq(bmap
, 1), return -1);
743 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
744 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
745 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
746 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
747 isl_seq_clr(bmap
->ineq
[bmap
->n_ineq
] +
748 1 + isl_basic_map_total_dim(bmap
),
749 bmap
->extra
- bmap
->n_div
);
750 return bmap
->n_ineq
++;
753 int isl_basic_set_alloc_inequality(struct isl_basic_set
*bset
)
755 return isl_basic_map_alloc_inequality((struct isl_basic_map
*)bset
);
758 int isl_basic_map_free_inequality(struct isl_basic_map
*bmap
, unsigned n
)
762 isl_assert(bmap
->ctx
, n
<= bmap
->n_ineq
, return -1);
767 int isl_basic_set_free_inequality(struct isl_basic_set
*bset
, unsigned n
)
769 return isl_basic_map_free_inequality((struct isl_basic_map
*)bset
, n
);
772 int isl_basic_map_drop_inequality(struct isl_basic_map
*bmap
, unsigned pos
)
777 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
779 if (pos
!= bmap
->n_ineq
- 1) {
781 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
782 bmap
->ineq
[bmap
->n_ineq
- 1] = t
;
783 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
789 int isl_basic_set_drop_inequality(struct isl_basic_set
*bset
, unsigned pos
)
791 return isl_basic_map_drop_inequality((struct isl_basic_map
*)bset
, pos
);
794 __isl_give isl_basic_map
*isl_basic_map_add_eq(__isl_take isl_basic_map
*bmap
,
799 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
802 k
= isl_basic_map_alloc_equality(bmap
);
805 isl_seq_cpy(bmap
->eq
[k
], eq
, 1 + isl_basic_map_total_dim(bmap
));
808 isl_basic_map_free(bmap
);
812 __isl_give isl_basic_set
*isl_basic_set_add_eq(__isl_take isl_basic_set
*bset
,
815 return (isl_basic_set
*)
816 isl_basic_map_add_eq((isl_basic_map
*)bset
, eq
);
819 __isl_give isl_basic_map
*isl_basic_map_add_ineq(__isl_take isl_basic_map
*bmap
,
824 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
827 k
= isl_basic_map_alloc_inequality(bmap
);
830 isl_seq_cpy(bmap
->ineq
[k
], ineq
, 1 + isl_basic_map_total_dim(bmap
));
833 isl_basic_map_free(bmap
);
837 __isl_give isl_basic_set
*isl_basic_set_add_ineq(__isl_take isl_basic_set
*bset
,
840 return (isl_basic_set
*)
841 isl_basic_map_add_ineq((isl_basic_map
*)bset
, ineq
);
844 int isl_basic_map_alloc_div(struct isl_basic_map
*bmap
)
848 isl_assert(bmap
->ctx
, bmap
->n_div
< bmap
->extra
, return -1);
849 isl_seq_clr(bmap
->div
[bmap
->n_div
] +
850 1 + 1 + isl_basic_map_total_dim(bmap
),
851 bmap
->extra
- bmap
->n_div
);
852 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
853 return bmap
->n_div
++;
856 int isl_basic_set_alloc_div(struct isl_basic_set
*bset
)
858 return isl_basic_map_alloc_div((struct isl_basic_map
*)bset
);
861 int isl_basic_map_free_div(struct isl_basic_map
*bmap
, unsigned n
)
865 isl_assert(bmap
->ctx
, n
<= bmap
->n_div
, return -1);
870 int isl_basic_set_free_div(struct isl_basic_set
*bset
, unsigned n
)
872 return isl_basic_map_free_div((struct isl_basic_map
*)bset
, n
);
875 /* Copy constraint from src to dst, putting the vars of src at offset
876 * dim_off in dst and the divs of src at offset div_off in dst.
877 * If both sets are actually map, then dim_off applies to the input
880 static void copy_constraint(struct isl_basic_map
*dst_map
, isl_int
*dst
,
881 struct isl_basic_map
*src_map
, isl_int
*src
,
882 unsigned in_off
, unsigned out_off
, unsigned div_off
)
884 unsigned src_nparam
= isl_basic_map_n_param(src_map
);
885 unsigned dst_nparam
= isl_basic_map_n_param(dst_map
);
886 unsigned src_in
= isl_basic_map_n_in(src_map
);
887 unsigned dst_in
= isl_basic_map_n_in(dst_map
);
888 unsigned src_out
= isl_basic_map_n_out(src_map
);
889 unsigned dst_out
= isl_basic_map_n_out(dst_map
);
890 isl_int_set(dst
[0], src
[0]);
891 isl_seq_cpy(dst
+1, src
+1, isl_min(dst_nparam
, src_nparam
));
892 if (dst_nparam
> src_nparam
)
893 isl_seq_clr(dst
+1+src_nparam
,
894 dst_nparam
- src_nparam
);
895 isl_seq_clr(dst
+1+dst_nparam
, in_off
);
896 isl_seq_cpy(dst
+1+dst_nparam
+in_off
,
898 isl_min(dst_in
-in_off
, src_in
));
899 if (dst_in
-in_off
> src_in
)
900 isl_seq_clr(dst
+1+dst_nparam
+in_off
+src_in
,
901 dst_in
- in_off
- src_in
);
902 isl_seq_clr(dst
+1+dst_nparam
+dst_in
, out_off
);
903 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+out_off
,
904 src
+1+src_nparam
+src_in
,
905 isl_min(dst_out
-out_off
, src_out
));
906 if (dst_out
-out_off
> src_out
)
907 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+out_off
+src_out
,
908 dst_out
- out_off
- src_out
);
909 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
, div_off
);
910 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+dst_out
+div_off
,
911 src
+1+src_nparam
+src_in
+src_out
,
912 isl_min(dst_map
->extra
-div_off
, src_map
->n_div
));
913 if (dst_map
->n_div
-div_off
> src_map
->n_div
)
914 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
+
915 div_off
+src_map
->n_div
,
916 dst_map
->n_div
- div_off
- src_map
->n_div
);
919 static void copy_div(struct isl_basic_map
*dst_map
, isl_int
*dst
,
920 struct isl_basic_map
*src_map
, isl_int
*src
,
921 unsigned in_off
, unsigned out_off
, unsigned div_off
)
923 isl_int_set(dst
[0], src
[0]);
924 copy_constraint(dst_map
, dst
+1, src_map
, src
+1, in_off
, out_off
, div_off
);
927 static struct isl_basic_map
*add_constraints(struct isl_basic_map
*bmap1
,
928 struct isl_basic_map
*bmap2
, unsigned i_pos
, unsigned o_pos
)
933 if (!bmap1
|| !bmap2
)
936 div_off
= bmap1
->n_div
;
938 for (i
= 0; i
< bmap2
->n_eq
; ++i
) {
939 int i1
= isl_basic_map_alloc_equality(bmap1
);
942 copy_constraint(bmap1
, bmap1
->eq
[i1
], bmap2
, bmap2
->eq
[i
],
943 i_pos
, o_pos
, div_off
);
946 for (i
= 0; i
< bmap2
->n_ineq
; ++i
) {
947 int i1
= isl_basic_map_alloc_inequality(bmap1
);
950 copy_constraint(bmap1
, bmap1
->ineq
[i1
], bmap2
, bmap2
->ineq
[i
],
951 i_pos
, o_pos
, div_off
);
954 for (i
= 0; i
< bmap2
->n_div
; ++i
) {
955 int i1
= isl_basic_map_alloc_div(bmap1
);
958 copy_div(bmap1
, bmap1
->div
[i1
], bmap2
, bmap2
->div
[i
],
959 i_pos
, o_pos
, div_off
);
962 isl_basic_map_free(bmap2
);
967 isl_basic_map_free(bmap1
);
968 isl_basic_map_free(bmap2
);
972 static void copy_constraint_dim_map(isl_int
*dst
, isl_int
*src
,
973 struct isl_dim_map
*dim_map
)
977 for (i
= 0; i
< dim_map
->len
; ++i
) {
978 if (dim_map
->pos
[i
] < 0)
979 isl_int_set_si(dst
[i
], 0);
981 isl_int_set(dst
[i
], src
[dim_map
->pos
[i
]]);
985 static void copy_div_dim_map(isl_int
*dst
, isl_int
*src
,
986 struct isl_dim_map
*dim_map
)
988 isl_int_set(dst
[0], src
[0]);
989 copy_constraint_dim_map(dst
+1, src
+1, dim_map
);
992 static struct isl_basic_map
*add_constraints_dim_map(struct isl_basic_map
*dst
,
993 struct isl_basic_map
*src
, struct isl_dim_map
*dim_map
)
997 if (!src
|| !dst
|| !dim_map
)
1000 for (i
= 0; i
< src
->n_eq
; ++i
) {
1001 int i1
= isl_basic_map_alloc_equality(dst
);
1004 copy_constraint_dim_map(dst
->eq
[i1
], src
->eq
[i
], dim_map
);
1007 for (i
= 0; i
< src
->n_ineq
; ++i
) {
1008 int i1
= isl_basic_map_alloc_inequality(dst
);
1011 copy_constraint_dim_map(dst
->ineq
[i1
], src
->ineq
[i
], dim_map
);
1014 for (i
= 0; i
< src
->n_div
; ++i
) {
1015 int i1
= isl_basic_map_alloc_div(dst
);
1018 copy_div_dim_map(dst
->div
[i1
], src
->div
[i
], dim_map
);
1022 isl_basic_map_free(src
);
1027 isl_basic_map_free(src
);
1028 isl_basic_map_free(dst
);
1032 struct isl_basic_set
*isl_basic_set_add_constraints(struct isl_basic_set
*bset1
,
1033 struct isl_basic_set
*bset2
, unsigned pos
)
1035 return (struct isl_basic_set
*)
1036 add_constraints((struct isl_basic_map
*)bset1
,
1037 (struct isl_basic_map
*)bset2
, 0, pos
);
1040 struct isl_basic_map
*isl_basic_map_extend_dim(struct isl_basic_map
*base
,
1041 struct isl_dim
*dim
, unsigned extra
,
1042 unsigned n_eq
, unsigned n_ineq
)
1044 struct isl_basic_map
*ext
;
1054 dims_ok
= isl_dim_equal(base
->dim
, dim
) &&
1055 base
->extra
>= base
->n_div
+ extra
;
1057 if (dims_ok
&& room_for_con(base
, n_eq
+ n_ineq
) &&
1058 room_for_ineq(base
, n_ineq
)) {
1063 isl_assert(base
->ctx
, base
->dim
->nparam
<= dim
->nparam
, goto error
);
1064 isl_assert(base
->ctx
, base
->dim
->n_in
<= dim
->n_in
, goto error
);
1065 isl_assert(base
->ctx
, base
->dim
->n_out
<= dim
->n_out
, goto error
);
1066 extra
+= base
->extra
;
1068 n_ineq
+= base
->n_ineq
;
1070 ext
= isl_basic_map_alloc_dim(dim
, extra
, n_eq
, n_ineq
);
1076 ext
->sample
= isl_vec_copy(base
->sample
);
1077 flags
= base
->flags
;
1078 ext
= add_constraints(ext
, base
, 0, 0);
1081 ISL_F_CLR(ext
, ISL_BASIC_SET_FINAL
);
1088 isl_basic_map_free(base
);
1092 struct isl_basic_set
*isl_basic_set_extend_dim(struct isl_basic_set
*base
,
1093 struct isl_dim
*dim
, unsigned extra
,
1094 unsigned n_eq
, unsigned n_ineq
)
1096 return (struct isl_basic_set
*)
1097 isl_basic_map_extend_dim((struct isl_basic_map
*)base
, dim
,
1098 extra
, n_eq
, n_ineq
);
1101 struct isl_basic_map
*isl_basic_map_extend_constraints(
1102 struct isl_basic_map
*base
, unsigned n_eq
, unsigned n_ineq
)
1106 return isl_basic_map_extend_dim(base
, isl_dim_copy(base
->dim
),
1110 struct isl_basic_map
*isl_basic_map_extend(struct isl_basic_map
*base
,
1111 unsigned nparam
, unsigned n_in
, unsigned n_out
, unsigned extra
,
1112 unsigned n_eq
, unsigned n_ineq
)
1114 struct isl_basic_map
*bmap
;
1115 struct isl_dim
*dim
;
1119 dim
= isl_dim_alloc(base
->ctx
, nparam
, n_in
, n_out
);
1123 bmap
= isl_basic_map_extend_dim(base
, dim
, extra
, n_eq
, n_ineq
);
1126 isl_basic_map_free(base
);
1130 struct isl_basic_set
*isl_basic_set_extend(struct isl_basic_set
*base
,
1131 unsigned nparam
, unsigned dim
, unsigned extra
,
1132 unsigned n_eq
, unsigned n_ineq
)
1134 return (struct isl_basic_set
*)
1135 isl_basic_map_extend((struct isl_basic_map
*)base
,
1136 nparam
, 0, dim
, extra
, n_eq
, n_ineq
);
1139 struct isl_basic_set
*isl_basic_set_extend_constraints(
1140 struct isl_basic_set
*base
, unsigned n_eq
, unsigned n_ineq
)
1142 return (struct isl_basic_set
*)
1143 isl_basic_map_extend_constraints((struct isl_basic_map
*)base
,
1147 struct isl_basic_set
*isl_basic_set_cow(struct isl_basic_set
*bset
)
1149 return (struct isl_basic_set
*)
1150 isl_basic_map_cow((struct isl_basic_map
*)bset
);
1153 struct isl_basic_map
*isl_basic_map_cow(struct isl_basic_map
*bmap
)
1158 if (bmap
->ref
> 1) {
1160 bmap
= isl_basic_map_dup(bmap
);
1163 ISL_F_CLR(bmap
, ISL_BASIC_SET_FINAL
);
1167 struct isl_set
*isl_set_cow(struct isl_set
*set
)
1175 return isl_set_dup(set
);
1178 struct isl_map
*isl_map_cow(struct isl_map
*map
)
1186 return isl_map_dup(map
);
1189 static void swap_vars(struct isl_blk blk
, isl_int
*a
,
1190 unsigned a_len
, unsigned b_len
)
1192 isl_seq_cpy(blk
.data
, a
+a_len
, b_len
);
1193 isl_seq_cpy(blk
.data
+b_len
, a
, a_len
);
1194 isl_seq_cpy(a
, blk
.data
, b_len
+a_len
);
1197 struct isl_basic_set
*isl_basic_set_swap_vars(
1198 struct isl_basic_set
*bset
, unsigned n
)
1208 nparam
= isl_basic_set_n_param(bset
);
1209 dim
= isl_basic_set_n_dim(bset
);
1210 isl_assert(bset
->ctx
, n
<= dim
, goto error
);
1215 bset
= isl_basic_set_cow(bset
);
1219 blk
= isl_blk_alloc(bset
->ctx
, dim
);
1220 if (isl_blk_is_error(blk
))
1223 for (i
= 0; i
< bset
->n_eq
; ++i
)
1225 bset
->eq
[i
]+1+nparam
, n
, dim
- n
);
1227 for (i
= 0; i
< bset
->n_ineq
; ++i
)
1229 bset
->ineq
[i
]+1+nparam
, n
, dim
- n
);
1231 for (i
= 0; i
< bset
->n_div
; ++i
)
1233 bset
->div
[i
]+1+1+nparam
, n
, dim
- n
);
1235 isl_blk_free(bset
->ctx
, blk
);
1237 ISL_F_CLR(bset
, ISL_BASIC_SET_NORMALIZED
);
1238 return isl_basic_set_gauss(bset
, NULL
);
1240 isl_basic_set_free(bset
);
1244 struct isl_set
*isl_set_swap_vars(struct isl_set
*set
, unsigned n
)
1247 set
= isl_set_cow(set
);
1251 for (i
= 0; i
< set
->n
; ++i
) {
1252 set
->p
[i
] = isl_basic_set_swap_vars(set
->p
[i
], n
);
1258 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
1262 struct isl_basic_map
*isl_basic_map_set_to_empty(struct isl_basic_map
*bmap
)
1268 total
= isl_basic_map_total_dim(bmap
);
1269 isl_basic_map_free_div(bmap
, bmap
->n_div
);
1270 isl_basic_map_free_inequality(bmap
, bmap
->n_ineq
);
1272 isl_basic_map_free_equality(bmap
, bmap
->n_eq
-1);
1274 i
= isl_basic_map_alloc_equality(bmap
);
1278 isl_int_set_si(bmap
->eq
[i
][0], 1);
1279 isl_seq_clr(bmap
->eq
[i
]+1, total
);
1280 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
1281 isl_vec_free(bmap
->sample
);
1282 bmap
->sample
= NULL
;
1283 return isl_basic_map_finalize(bmap
);
1285 isl_basic_map_free(bmap
);
1289 struct isl_basic_set
*isl_basic_set_set_to_empty(struct isl_basic_set
*bset
)
1291 return (struct isl_basic_set
*)
1292 isl_basic_map_set_to_empty((struct isl_basic_map
*)bset
);
1295 void isl_basic_map_swap_div(struct isl_basic_map
*bmap
, int a
, int b
)
1298 unsigned off
= isl_dim_total(bmap
->dim
);
1299 isl_int
*t
= bmap
->div
[a
];
1300 bmap
->div
[a
] = bmap
->div
[b
];
1303 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1304 isl_int_swap(bmap
->eq
[i
][1+off
+a
], bmap
->eq
[i
][1+off
+b
]);
1306 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1307 isl_int_swap(bmap
->ineq
[i
][1+off
+a
], bmap
->ineq
[i
][1+off
+b
]);
1309 for (i
= 0; i
< bmap
->n_div
; ++i
)
1310 isl_int_swap(bmap
->div
[i
][1+1+off
+a
], bmap
->div
[i
][1+1+off
+b
]);
1311 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1314 /* Eliminate the specified n dimensions starting at first from the
1315 * constraints using Fourier-Motzkin. The dimensions themselves
1318 __isl_give isl_map
*isl_map_eliminate(__isl_take isl_map
*map
,
1319 enum isl_dim_type type
, unsigned first
, unsigned n
)
1328 map
= isl_map_cow(map
);
1331 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
1332 first
+= pos(map
->dim
, type
) - 1;
1334 for (i
= 0; i
< map
->n
; ++i
) {
1335 map
->p
[i
] = isl_basic_map_eliminate_vars(map
->p
[i
], first
, n
);
1345 /* Eliminate the specified n dimensions starting at first from the
1346 * constraints using Fourier-Motzkin. The dimensions themselves
1349 __isl_give isl_set
*isl_set_eliminate(__isl_take isl_set
*set
,
1350 enum isl_dim_type type
, unsigned first
, unsigned n
)
1352 return (isl_set
*)isl_map_eliminate((isl_map
*)set
, type
, first
, n
);
1355 /* Eliminate the specified n dimensions starting at first from the
1356 * constraints using Fourier-Motzkin. The dimensions themselves
1359 __isl_give isl_set
*isl_set_eliminate_dims(__isl_take isl_set
*set
,
1360 unsigned first
, unsigned n
)
1362 return isl_set_eliminate(set
, isl_dim_set
, first
, n
);
1365 /* Project out n dimensions starting at first using Fourier-Motzkin */
1366 struct isl_set
*isl_set_remove_dims(struct isl_set
*set
,
1367 unsigned first
, unsigned n
)
1369 set
= isl_set_eliminate_dims(set
, first
, n
);
1370 set
= isl_set_drop_dims(set
, first
, n
);
1374 struct isl_basic_set
*isl_basic_set_remove_divs(struct isl_basic_set
*bset
)
1376 bset
= isl_basic_set_eliminate_vars(bset
, isl_dim_total(bset
->dim
),
1384 struct isl_set
*isl_set_remove_divs(struct isl_set
*set
)
1393 set
= isl_set_cow(set
);
1397 for (i
= 0; i
< set
->n
; ++i
) {
1398 set
->p
[i
] = isl_basic_set_remove_divs(set
->p
[i
]);
1408 struct isl_basic_map
*isl_basic_map_remove(struct isl_basic_map
*bmap
,
1409 enum isl_dim_type type
, unsigned first
, unsigned n
)
1413 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
1417 bmap
= isl_basic_map_eliminate_vars(bmap
,
1418 isl_basic_map_offset(bmap
, type
) - 1 + first
, n
);
1421 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
) && type
== isl_dim_div
)
1423 bmap
= isl_basic_map_drop(bmap
, type
, first
, n
);
1426 isl_basic_map_free(bmap
);
1430 __isl_give isl_basic_set
*isl_basic_set_remove(__isl_take isl_basic_set
*bset
,
1431 enum isl_dim_type type
, unsigned first
, unsigned n
)
1433 return (isl_basic_set
*)
1434 isl_basic_map_remove((isl_basic_map
*)bset
, type
, first
, n
);
1437 struct isl_map
*isl_map_remove(struct isl_map
*map
,
1438 enum isl_dim_type type
, unsigned first
, unsigned n
)
1445 map
= isl_map_cow(map
);
1448 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
1450 for (i
= 0; i
< map
->n
; ++i
) {
1451 map
->p
[i
] = isl_basic_map_eliminate_vars(map
->p
[i
],
1452 isl_basic_map_offset(map
->p
[i
], type
) - 1 + first
, n
);
1456 map
= isl_map_drop(map
, type
, first
, n
);
1463 __isl_give isl_set
*isl_set_remove(__isl_take isl_set
*bset
,
1464 enum isl_dim_type type
, unsigned first
, unsigned n
)
1466 return (isl_set
*)isl_map_remove((isl_map
*)bset
, type
, first
, n
);
1469 /* Project out n inputs starting at first using Fourier-Motzkin */
1470 struct isl_map
*isl_map_remove_inputs(struct isl_map
*map
,
1471 unsigned first
, unsigned n
)
1473 return isl_map_remove(map
, isl_dim_in
, first
, n
);
1476 /* Project out n dimensions starting at first using Fourier-Motzkin */
1477 struct isl_basic_set
*isl_basic_set_remove_dims(struct isl_basic_set
*bset
,
1478 unsigned first
, unsigned n
)
1480 unsigned nparam
= isl_basic_set_n_param(bset
);
1481 bset
= isl_basic_set_eliminate_vars(bset
, nparam
+ first
, n
);
1482 bset
= isl_basic_set_drop_dims(bset
, first
, n
);
1486 static void dump_term(struct isl_basic_map
*bmap
,
1487 isl_int c
, int pos
, FILE *out
)
1490 unsigned in
= isl_basic_map_n_in(bmap
);
1491 unsigned dim
= in
+ isl_basic_map_n_out(bmap
);
1492 unsigned nparam
= isl_basic_map_n_param(bmap
);
1494 isl_int_print(out
, c
, 0);
1496 if (!isl_int_is_one(c
))
1497 isl_int_print(out
, c
, 0);
1498 if (pos
< 1 + nparam
) {
1499 name
= isl_dim_get_name(bmap
->dim
,
1500 isl_dim_param
, pos
- 1);
1502 fprintf(out
, "%s", name
);
1504 fprintf(out
, "p%d", pos
- 1);
1505 } else if (pos
< 1 + nparam
+ in
)
1506 fprintf(out
, "i%d", pos
- 1 - nparam
);
1507 else if (pos
< 1 + nparam
+ dim
)
1508 fprintf(out
, "o%d", pos
- 1 - nparam
- in
);
1510 fprintf(out
, "e%d", pos
- 1 - nparam
- dim
);
1514 static void dump_constraint_sign(struct isl_basic_map
*bmap
, isl_int
*c
,
1515 int sign
, FILE *out
)
1519 unsigned len
= 1 + isl_basic_map_total_dim(bmap
);
1523 for (i
= 0, first
= 1; i
< len
; ++i
) {
1524 if (isl_int_sgn(c
[i
]) * sign
<= 0)
1527 fprintf(out
, " + ");
1529 isl_int_abs(v
, c
[i
]);
1530 dump_term(bmap
, v
, i
, out
);
1537 static void dump_constraint(struct isl_basic_map
*bmap
, isl_int
*c
,
1538 const char *op
, FILE *out
, int indent
)
1542 fprintf(out
, "%*s", indent
, "");
1544 dump_constraint_sign(bmap
, c
, 1, out
);
1545 fprintf(out
, " %s ", op
);
1546 dump_constraint_sign(bmap
, c
, -1, out
);
1550 for (i
= bmap
->n_div
; i
< bmap
->extra
; ++i
) {
1551 if (isl_int_is_zero(c
[1+isl_dim_total(bmap
->dim
)+i
]))
1553 fprintf(out
, "%*s", indent
, "");
1554 fprintf(out
, "ERROR: unused div coefficient not zero\n");
1559 static void dump_constraints(struct isl_basic_map
*bmap
,
1560 isl_int
**c
, unsigned n
,
1561 const char *op
, FILE *out
, int indent
)
1565 for (i
= 0; i
< n
; ++i
)
1566 dump_constraint(bmap
, c
[i
], op
, out
, indent
);
1569 static void dump_affine(struct isl_basic_map
*bmap
, isl_int
*exp
, FILE *out
)
1573 unsigned total
= isl_basic_map_total_dim(bmap
);
1575 for (j
= 0; j
< 1 + total
; ++j
) {
1576 if (isl_int_is_zero(exp
[j
]))
1578 if (!first
&& isl_int_is_pos(exp
[j
]))
1580 dump_term(bmap
, exp
[j
], j
, out
);
1585 static void dump(struct isl_basic_map
*bmap
, FILE *out
, int indent
)
1589 dump_constraints(bmap
, bmap
->eq
, bmap
->n_eq
, "=", out
, indent
);
1590 dump_constraints(bmap
, bmap
->ineq
, bmap
->n_ineq
, ">=", out
, indent
);
1592 for (i
= 0; i
< bmap
->n_div
; ++i
) {
1593 fprintf(out
, "%*s", indent
, "");
1594 fprintf(out
, "e%d = [(", i
);
1595 dump_affine(bmap
, bmap
->div
[i
]+1, out
);
1597 isl_int_print(out
, bmap
->div
[i
][0], 0);
1598 fprintf(out
, "]\n");
1602 void isl_basic_set_dump(struct isl_basic_set
*bset
, FILE *out
, int indent
)
1605 fprintf(out
, "null basic set\n");
1609 fprintf(out
, "%*s", indent
, "");
1610 fprintf(out
, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
1611 bset
->ref
, bset
->dim
->nparam
, bset
->dim
->n_out
,
1612 bset
->extra
, bset
->flags
);
1613 dump((struct isl_basic_map
*)bset
, out
, indent
);
1616 void isl_basic_map_dump(struct isl_basic_map
*bmap
, FILE *out
, int indent
)
1619 fprintf(out
, "null basic map\n");
1623 fprintf(out
, "%*s", indent
, "");
1624 fprintf(out
, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
1625 "flags: %x, n_name: %d\n",
1627 bmap
->dim
->nparam
, bmap
->dim
->n_in
, bmap
->dim
->n_out
,
1628 bmap
->extra
, bmap
->flags
, bmap
->dim
->n_name
);
1629 dump(bmap
, out
, indent
);
1632 int isl_inequality_negate(struct isl_basic_map
*bmap
, unsigned pos
)
1637 total
= isl_basic_map_total_dim(bmap
);
1638 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
1639 isl_seq_neg(bmap
->ineq
[pos
], bmap
->ineq
[pos
], 1 + total
);
1640 isl_int_sub_ui(bmap
->ineq
[pos
][0], bmap
->ineq
[pos
][0], 1);
1641 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1645 struct isl_set
*isl_set_alloc_dim(struct isl_dim
*dim
, int n
, unsigned flags
)
1647 struct isl_set
*set
;
1651 isl_assert(dim
->ctx
, dim
->n_in
== 0, return NULL
);
1652 isl_assert(dim
->ctx
, n
>= 0, return NULL
);
1653 set
= isl_alloc(dim
->ctx
, struct isl_set
,
1654 sizeof(struct isl_set
) +
1655 (n
- 1) * sizeof(struct isl_basic_set
*));
1659 set
->ctx
= dim
->ctx
;
1660 isl_ctx_ref(set
->ctx
);
1672 struct isl_set
*isl_set_alloc(struct isl_ctx
*ctx
,
1673 unsigned nparam
, unsigned dim
, int n
, unsigned flags
)
1675 struct isl_set
*set
;
1676 struct isl_dim
*dims
;
1678 dims
= isl_dim_alloc(ctx
, nparam
, 0, dim
);
1682 set
= isl_set_alloc_dim(dims
, n
, flags
);
1686 /* Make sure "map" has room for at least "n" more basic maps.
1688 struct isl_map
*isl_map_grow(struct isl_map
*map
, int n
)
1691 struct isl_map
*grown
= NULL
;
1695 isl_assert(map
->ctx
, n
>= 0, goto error
);
1696 if (map
->n
+ n
<= map
->size
)
1698 grown
= isl_map_alloc_dim(isl_map_get_dim(map
), map
->n
+ n
, map
->flags
);
1701 for (i
= 0; i
< map
->n
; ++i
) {
1702 grown
->p
[i
] = isl_basic_map_copy(map
->p
[i
]);
1710 isl_map_free(grown
);
1715 /* Make sure "set" has room for at least "n" more basic sets.
1717 struct isl_set
*isl_set_grow(struct isl_set
*set
, int n
)
1719 return (struct isl_set
*)isl_map_grow((struct isl_map
*)set
, n
);
1722 struct isl_set
*isl_set_dup(struct isl_set
*set
)
1725 struct isl_set
*dup
;
1730 dup
= isl_set_alloc_dim(isl_dim_copy(set
->dim
), set
->n
, set
->flags
);
1733 for (i
= 0; i
< set
->n
; ++i
)
1734 dup
= isl_set_add_basic_set(dup
, isl_basic_set_copy(set
->p
[i
]));
1738 struct isl_set
*isl_set_from_basic_set(struct isl_basic_set
*bset
)
1740 struct isl_set
*set
;
1745 set
= isl_set_alloc_dim(isl_dim_copy(bset
->dim
), 1, ISL_MAP_DISJOINT
);
1747 isl_basic_set_free(bset
);
1750 return isl_set_add_basic_set(set
, bset
);
1753 struct isl_map
*isl_map_from_basic_map(struct isl_basic_map
*bmap
)
1755 struct isl_map
*map
;
1760 map
= isl_map_alloc_dim(isl_dim_copy(bmap
->dim
), 1, ISL_MAP_DISJOINT
);
1762 isl_basic_map_free(bmap
);
1765 return isl_map_add_basic_map(map
, bmap
);
1768 __isl_give isl_set
*isl_set_add_basic_set(__isl_take isl_set
*set
,
1769 __isl_take isl_basic_set
*bset
)
1771 return (struct isl_set
*)isl_map_add_basic_map((struct isl_map
*)set
,
1772 (struct isl_basic_map
*)bset
);
1775 void isl_set_free(struct isl_set
*set
)
1785 isl_ctx_deref(set
->ctx
);
1786 for (i
= 0; i
< set
->n
; ++i
)
1787 isl_basic_set_free(set
->p
[i
]);
1788 isl_dim_free(set
->dim
);
1792 void isl_set_dump(struct isl_set
*set
, FILE *out
, int indent
)
1797 fprintf(out
, "null set\n");
1801 fprintf(out
, "%*s", indent
, "");
1802 fprintf(out
, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
1803 set
->ref
, set
->n
, set
->dim
->nparam
, set
->dim
->n_out
,
1805 for (i
= 0; i
< set
->n
; ++i
) {
1806 fprintf(out
, "%*s", indent
, "");
1807 fprintf(out
, "basic set %d:\n", i
);
1808 isl_basic_set_dump(set
->p
[i
], out
, indent
+4);
1812 void isl_map_dump(struct isl_map
*map
, FILE *out
, int indent
)
1817 fprintf(out
, "null map\n");
1821 fprintf(out
, "%*s", indent
, "");
1822 fprintf(out
, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
1823 "flags: %x, n_name: %d\n",
1824 map
->ref
, map
->n
, map
->dim
->nparam
, map
->dim
->n_in
,
1825 map
->dim
->n_out
, map
->flags
, map
->dim
->n_name
);
1826 for (i
= 0; i
< map
->n
; ++i
) {
1827 fprintf(out
, "%*s", indent
, "");
1828 fprintf(out
, "basic map %d:\n", i
);
1829 isl_basic_map_dump(map
->p
[i
], out
, indent
+4);
1833 struct isl_basic_map
*isl_basic_map_intersect_domain(
1834 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
1836 struct isl_basic_map
*bmap_domain
;
1837 struct isl_dim
*dim
;
1842 isl_assert(bset
->ctx
, isl_dim_match(bmap
->dim
, isl_dim_param
,
1843 bset
->dim
, isl_dim_param
), goto error
);
1845 if (isl_dim_size(bset
->dim
, isl_dim_set
) != 0)
1846 isl_assert(bset
->ctx
,
1847 isl_basic_map_compatible_domain(bmap
, bset
), goto error
);
1849 bmap
= isl_basic_map_cow(bmap
);
1852 bmap
= isl_basic_map_extend_dim(bmap
, isl_dim_copy(bmap
->dim
),
1853 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
1854 dim
= isl_dim_reverse(isl_dim_copy(bset
->dim
));
1855 bmap_domain
= isl_basic_map_from_basic_set(bset
, dim
);
1856 bmap
= add_constraints(bmap
, bmap_domain
, 0, 0);
1858 bmap
= isl_basic_map_simplify(bmap
);
1859 return isl_basic_map_finalize(bmap
);
1861 isl_basic_map_free(bmap
);
1862 isl_basic_set_free(bset
);
1866 struct isl_basic_map
*isl_basic_map_intersect_range(
1867 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
1869 struct isl_basic_map
*bmap_range
;
1874 isl_assert(bset
->ctx
, isl_dim_match(bmap
->dim
, isl_dim_param
,
1875 bset
->dim
, isl_dim_param
), goto error
);
1877 if (isl_dim_size(bset
->dim
, isl_dim_set
) != 0)
1878 isl_assert(bset
->ctx
,
1879 isl_basic_map_compatible_range(bmap
, bset
), goto error
);
1881 bmap
= isl_basic_map_cow(bmap
);
1884 bmap
= isl_basic_map_extend_dim(bmap
, isl_dim_copy(bmap
->dim
),
1885 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
1886 bmap_range
= isl_basic_map_from_basic_set(bset
, isl_dim_copy(bset
->dim
));
1887 bmap
= add_constraints(bmap
, bmap_range
, 0, 0);
1889 bmap
= isl_basic_map_simplify(bmap
);
1890 return isl_basic_map_finalize(bmap
);
1892 isl_basic_map_free(bmap
);
1893 isl_basic_set_free(bset
);
1897 int isl_basic_map_contains(struct isl_basic_map
*bmap
, struct isl_vec
*vec
)
1903 total
= 1 + isl_basic_map_total_dim(bmap
);
1904 if (total
!= vec
->size
)
1909 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
1910 isl_seq_inner_product(vec
->el
, bmap
->eq
[i
], total
, &s
);
1911 if (!isl_int_is_zero(s
)) {
1917 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
1918 isl_seq_inner_product(vec
->el
, bmap
->ineq
[i
], total
, &s
);
1919 if (isl_int_is_neg(s
)) {
1930 int isl_basic_set_contains(struct isl_basic_set
*bset
, struct isl_vec
*vec
)
1932 return isl_basic_map_contains((struct isl_basic_map
*)bset
, vec
);
1935 struct isl_basic_map
*isl_basic_map_intersect(
1936 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
1938 struct isl_vec
*sample
= NULL
;
1940 if (!bmap1
|| !bmap2
)
1943 isl_assert(bmap1
->ctx
, isl_dim_match(bmap1
->dim
, isl_dim_param
,
1944 bmap2
->dim
, isl_dim_param
), goto error
);
1945 if (isl_dim_total(bmap1
->dim
) ==
1946 isl_dim_size(bmap1
->dim
, isl_dim_param
) &&
1947 isl_dim_total(bmap2
->dim
) !=
1948 isl_dim_size(bmap2
->dim
, isl_dim_param
))
1949 return isl_basic_map_intersect(bmap2
, bmap1
);
1951 if (isl_dim_total(bmap2
->dim
) !=
1952 isl_dim_size(bmap2
->dim
, isl_dim_param
))
1953 isl_assert(bmap1
->ctx
,
1954 isl_dim_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
1956 if (bmap1
->sample
&&
1957 isl_basic_map_contains(bmap1
, bmap1
->sample
) > 0 &&
1958 isl_basic_map_contains(bmap2
, bmap1
->sample
) > 0)
1959 sample
= isl_vec_copy(bmap1
->sample
);
1960 else if (bmap2
->sample
&&
1961 isl_basic_map_contains(bmap1
, bmap2
->sample
) > 0 &&
1962 isl_basic_map_contains(bmap2
, bmap2
->sample
) > 0)
1963 sample
= isl_vec_copy(bmap2
->sample
);
1965 bmap1
= isl_basic_map_cow(bmap1
);
1968 bmap1
= isl_basic_map_extend_dim(bmap1
, isl_dim_copy(bmap1
->dim
),
1969 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
1970 bmap1
= add_constraints(bmap1
, bmap2
, 0, 0);
1973 isl_vec_free(sample
);
1975 isl_vec_free(bmap1
->sample
);
1976 bmap1
->sample
= sample
;
1979 bmap1
= isl_basic_map_simplify(bmap1
);
1980 return isl_basic_map_finalize(bmap1
);
1983 isl_vec_free(sample
);
1984 isl_basic_map_free(bmap1
);
1985 isl_basic_map_free(bmap2
);
1989 struct isl_basic_set
*isl_basic_set_intersect(
1990 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
1992 return (struct isl_basic_set
*)
1993 isl_basic_map_intersect(
1994 (struct isl_basic_map
*)bset1
,
1995 (struct isl_basic_map
*)bset2
);
1998 /* Special case of isl_map_intersect, where both map1 and map2
1999 * are convex, without any divs and such that either map1 or map2
2000 * contains a single constraint. This constraint is then simply
2001 * added to the other map.
2003 static __isl_give isl_map
*map_intersect_add_constraint(
2004 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
2006 struct isl_basic_map
*bmap1
;
2007 struct isl_basic_map
*bmap2
;
2009 isl_assert(map1
->ctx
, map1
->n
== 1, goto error
);
2010 isl_assert(map2
->ctx
, map1
->n
== 1, goto error
);
2011 isl_assert(map1
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
2012 isl_assert(map2
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
2014 if (map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
!= 1)
2015 return isl_map_intersect(map2
, map1
);
2017 isl_assert(map2
->ctx
,
2018 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1, goto error
);
2020 map1
= isl_map_cow(map1
);
2023 if (isl_map_fast_is_empty(map1
)) {
2027 map1
->p
[0] = isl_basic_map_cow(map1
->p
[0]);
2028 if (map2
->p
[0]->n_eq
== 1)
2029 map1
->p
[0] = isl_basic_map_add_eq(map1
->p
[0], map2
->p
[0]->eq
[0]);
2031 map1
->p
[0] = isl_basic_map_add_ineq(map1
->p
[0],
2032 map2
->p
[0]->ineq
[0]);
2034 map1
->p
[0] = isl_basic_map_simplify(map1
->p
[0]);
2035 map1
->p
[0] = isl_basic_map_finalize(map1
->p
[0]);
2039 if (isl_basic_map_fast_is_empty(map1
->p
[0])) {
2040 isl_basic_map_free(map1
->p
[0]);
2053 struct isl_map
*isl_map_intersect(struct isl_map
*map1
, struct isl_map
*map2
)
2056 struct isl_map
*result
;
2062 if (isl_map_fast_is_empty(map1
)) {
2066 if (isl_map_fast_is_empty(map2
)) {
2071 if (map1
->n
== 1 && map2
->n
== 1 &&
2072 map1
->p
[0]->n_div
== 0 && map2
->p
[0]->n_div
== 0 &&
2073 isl_dim_equal(map1
->dim
, map2
->dim
) &&
2074 (map1
->p
[0]->n_eq
+ map1
->p
[0]->n_ineq
== 1 ||
2075 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1))
2076 return map_intersect_add_constraint(map1
, map2
);
2077 isl_assert(map1
->ctx
, isl_dim_match(map1
->dim
, isl_dim_param
,
2078 map2
->dim
, isl_dim_param
), goto error
);
2079 if (isl_dim_total(map1
->dim
) ==
2080 isl_dim_size(map1
->dim
, isl_dim_param
) &&
2081 isl_dim_total(map2
->dim
) != isl_dim_size(map2
->dim
, isl_dim_param
))
2082 return isl_map_intersect(map2
, map1
);
2084 if (isl_dim_total(map2
->dim
) != isl_dim_size(map2
->dim
, isl_dim_param
))
2085 isl_assert(map1
->ctx
,
2086 isl_dim_equal(map1
->dim
, map2
->dim
), goto error
);
2088 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
2089 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
2090 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
2092 result
= isl_map_alloc_dim(isl_dim_copy(map1
->dim
),
2093 map1
->n
* map2
->n
, flags
);
2096 for (i
= 0; i
< map1
->n
; ++i
)
2097 for (j
= 0; j
< map2
->n
; ++j
) {
2098 struct isl_basic_map
*part
;
2099 part
= isl_basic_map_intersect(
2100 isl_basic_map_copy(map1
->p
[i
]),
2101 isl_basic_map_copy(map2
->p
[j
]));
2102 if (isl_basic_map_is_empty(part
))
2103 isl_basic_map_free(part
);
2105 result
= isl_map_add_basic_map(result
, part
);
2118 struct isl_set
*isl_set_intersect(struct isl_set
*set1
, struct isl_set
*set2
)
2120 return (struct isl_set
*)
2121 isl_map_intersect((struct isl_map
*)set1
,
2122 (struct isl_map
*)set2
);
2125 struct isl_basic_map
*isl_basic_map_reverse(struct isl_basic_map
*bmap
)
2127 struct isl_dim
*dim
;
2128 struct isl_basic_set
*bset
;
2133 bmap
= isl_basic_map_cow(bmap
);
2136 dim
= isl_dim_reverse(isl_dim_copy(bmap
->dim
));
2137 in
= isl_basic_map_n_in(bmap
);
2138 bset
= isl_basic_set_from_basic_map(bmap
);
2139 bset
= isl_basic_set_swap_vars(bset
, in
);
2140 return isl_basic_map_from_basic_set(bset
, dim
);
2143 __isl_give isl_basic_map
*isl_basic_map_insert(__isl_take isl_basic_map
*bmap
,
2144 enum isl_dim_type type
, unsigned pos
, unsigned n
)
2146 struct isl_dim
*res_dim
;
2147 struct isl_basic_map
*res
;
2148 struct isl_dim_map
*dim_map
;
2149 unsigned total
, off
;
2150 enum isl_dim_type t
;
2158 res_dim
= isl_dim_insert(isl_basic_map_get_dim(bmap
), type
, pos
, n
);
2160 total
= isl_basic_map_total_dim(bmap
) + n
;
2161 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
2163 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
2165 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
2167 unsigned size
= isl_basic_map_dim(bmap
, t
);
2168 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2170 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2171 pos
, size
- pos
, off
+ pos
+ n
);
2173 off
+= isl_dim_size(res_dim
, t
);
2175 isl_dim_map_div(dim_map
, bmap
, off
);
2177 res
= isl_basic_map_alloc_dim(res_dim
,
2178 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
2179 res
= add_constraints_dim_map(res
, bmap
, dim_map
);
2180 res
= isl_basic_map_simplify(res
);
2181 return isl_basic_map_finalize(res
);
2184 __isl_give isl_basic_map
*isl_basic_map_add(__isl_take isl_basic_map
*bmap
,
2185 enum isl_dim_type type
, unsigned n
)
2189 return isl_basic_map_insert(bmap
, type
,
2190 isl_basic_map_dim(bmap
, type
), n
);
2193 __isl_give isl_basic_set
*isl_basic_set_add(__isl_take isl_basic_set
*bset
,
2194 enum isl_dim_type type
, unsigned n
)
2198 isl_assert(bset
->ctx
, type
!= isl_dim_in
, goto error
);
2199 return (isl_basic_set
*)isl_basic_map_add((isl_basic_map
*)bset
, type
, n
);
2201 isl_basic_set_free(bset
);
2205 __isl_give isl_map
*isl_map_insert(__isl_take isl_map
*map
,
2206 enum isl_dim_type type
, unsigned pos
, unsigned n
)
2213 map
= isl_map_cow(map
);
2217 map
->dim
= isl_dim_insert(map
->dim
, type
, pos
, n
);
2221 for (i
= 0; i
< map
->n
; ++i
) {
2222 map
->p
[i
] = isl_basic_map_insert(map
->p
[i
], type
, pos
, n
);
2233 __isl_give isl_set
*isl_set_insert(__isl_take isl_set
*set
,
2234 enum isl_dim_type type
, unsigned pos
, unsigned n
)
2236 return (isl_set
*)isl_map_insert((isl_map
*)set
, type
, pos
, n
);
2239 __isl_give isl_map
*isl_map_add(__isl_take isl_map
*map
,
2240 enum isl_dim_type type
, unsigned n
)
2244 return isl_map_insert(map
, type
, isl_map_dim(map
, type
), n
);
2247 __isl_give isl_set
*isl_set_add(__isl_take isl_set
*set
,
2248 enum isl_dim_type type
, unsigned n
)
2252 isl_assert(set
->ctx
, type
!= isl_dim_in
, goto error
);
2253 return (isl_set
*)isl_map_add((isl_map
*)set
, type
, n
);
2259 __isl_give isl_basic_map
*isl_basic_map_move_dims(
2260 __isl_take isl_basic_map
*bmap
,
2261 enum isl_dim_type dst_type
, unsigned dst_pos
,
2262 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2265 struct isl_dim_map
*dim_map
;
2266 struct isl_basic_map
*res
;
2267 enum isl_dim_type t
;
2268 unsigned total
, off
;
2275 isl_assert(bmap
->ctx
, src_pos
+ n
<= isl_basic_map_dim(bmap
, src_type
),
2278 if (dst_type
== src_type
&& dst_pos
== src_pos
)
2281 isl_assert(bmap
->ctx
, dst_type
!= src_type
, goto error
);
2283 if (pos(bmap
->dim
, dst_type
) + dst_pos
==
2284 pos(bmap
->dim
, src_type
) + src_pos
+
2285 ((src_type
< dst_type
) ? n
: 0)) {
2286 bmap
= isl_basic_map_cow(bmap
);
2290 bmap
->dim
= isl_dim_move(bmap
->dim
, dst_type
, dst_pos
,
2291 src_type
, src_pos
, n
);
2295 bmap
= isl_basic_map_finalize(bmap
);
2300 total
= isl_basic_map_total_dim(bmap
);
2301 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
2304 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
2305 unsigned size
= isl_dim_size(bmap
->dim
, t
);
2306 if (t
== dst_type
) {
2307 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2310 isl_dim_map_dim_range(dim_map
, bmap
->dim
, src_type
,
2313 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2314 dst_pos
, size
- dst_pos
, off
);
2315 off
+= size
- dst_pos
;
2316 } else if (t
== src_type
) {
2317 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2320 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2321 src_pos
+ n
, size
- src_pos
- n
, off
);
2322 off
+= size
- src_pos
- n
;
2324 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
2328 isl_dim_map_div(dim_map
, bmap
, off
);
2330 res
= isl_basic_map_alloc_dim(isl_basic_map_get_dim(bmap
),
2331 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
2332 bmap
= add_constraints_dim_map(res
, bmap
, dim_map
);
2334 bmap
->dim
= isl_dim_move(bmap
->dim
, dst_type
, dst_pos
,
2335 src_type
, src_pos
, n
);
2339 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
2340 bmap
= isl_basic_map_gauss(bmap
, NULL
);
2341 bmap
= isl_basic_map_finalize(bmap
);
2345 isl_basic_map_free(bmap
);
2349 __isl_give isl_basic_set
*isl_basic_set_move_dims(__isl_take isl_basic_set
*bset
,
2350 enum isl_dim_type dst_type
, unsigned dst_pos
,
2351 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2353 return (isl_basic_set
*)isl_basic_map_move_dims(
2354 (isl_basic_map
*)bset
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
2357 __isl_give isl_set
*isl_set_move_dims(__isl_take isl_set
*set
,
2358 enum isl_dim_type dst_type
, unsigned dst_pos
,
2359 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2363 isl_assert(set
->ctx
, dst_type
!= isl_dim_in
, goto error
);
2364 return (isl_set
*)isl_map_move_dims((isl_map
*)set
, dst_type
, dst_pos
,
2365 src_type
, src_pos
, n
);
2371 __isl_give isl_map
*isl_map_move_dims(__isl_take isl_map
*map
,
2372 enum isl_dim_type dst_type
, unsigned dst_pos
,
2373 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2382 isl_assert(map
->ctx
, src_pos
+ n
<= isl_map_dim(map
, src_type
),
2385 if (dst_type
== src_type
&& dst_pos
== src_pos
)
2388 isl_assert(map
->ctx
, dst_type
!= src_type
, goto error
);
2390 map
= isl_map_cow(map
);
2394 map
->dim
= isl_dim_move(map
->dim
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
2398 for (i
= 0; i
< map
->n
; ++i
) {
2399 map
->p
[i
] = isl_basic_map_move_dims(map
->p
[i
],
2401 src_type
, src_pos
, n
);
2412 /* Move the specified dimensions to the last columns right before
2413 * the divs. Don't change the dimension specification of bmap.
2414 * That's the responsibility of the caller.
2416 static __isl_give isl_basic_map
*move_last(__isl_take isl_basic_map
*bmap
,
2417 enum isl_dim_type type
, unsigned first
, unsigned n
)
2419 struct isl_dim_map
*dim_map
;
2420 struct isl_basic_map
*res
;
2421 enum isl_dim_type t
;
2422 unsigned total
, off
;
2426 if (pos(bmap
->dim
, type
) + first
+ n
== 1 + isl_dim_total(bmap
->dim
))
2429 total
= isl_basic_map_total_dim(bmap
);
2430 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
2433 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
2434 unsigned size
= isl_dim_size(bmap
->dim
, t
);
2436 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2439 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2440 first
, n
, total
- bmap
->n_div
- n
);
2441 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2442 first
+ n
, size
- (first
+ n
), off
);
2443 off
+= size
- (first
+ n
);
2445 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
2449 isl_dim_map_div(dim_map
, bmap
, off
+ n
);
2451 res
= isl_basic_map_alloc_dim(isl_basic_map_get_dim(bmap
),
2452 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
2453 res
= add_constraints_dim_map(res
, bmap
, dim_map
);
2457 /* Turn the n dimensions of type type, starting at first
2458 * into existentially quantified variables.
2460 __isl_give isl_basic_map
*isl_basic_map_project_out(
2461 __isl_take isl_basic_map
*bmap
,
2462 enum isl_dim_type type
, unsigned first
, unsigned n
)
2475 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
2476 return isl_basic_map_remove(bmap
, type
, first
, n
);
2478 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
2481 bmap
= move_last(bmap
, type
, first
, n
);
2482 bmap
= isl_basic_map_cow(bmap
);
2486 row_size
= 1 + isl_dim_total(bmap
->dim
) + bmap
->extra
;
2487 old
= bmap
->block2
.data
;
2488 bmap
->block2
= isl_blk_extend(bmap
->ctx
, bmap
->block2
,
2489 (bmap
->extra
+ n
) * (1 + row_size
));
2490 if (!bmap
->block2
.data
)
2492 new_div
= isl_alloc_array(bmap
->ctx
, isl_int
*, bmap
->extra
+ n
);
2495 for (i
= 0; i
< n
; ++i
) {
2496 new_div
[i
] = bmap
->block2
.data
+
2497 (bmap
->extra
+ i
) * (1 + row_size
);
2498 isl_seq_clr(new_div
[i
], 1 + row_size
);
2500 for (i
= 0; i
< bmap
->extra
; ++i
)
2501 new_div
[n
+ i
] = bmap
->block2
.data
+ (bmap
->div
[i
] - old
);
2503 bmap
->div
= new_div
;
2507 bmap
->dim
= isl_dim_drop(bmap
->dim
, type
, first
, n
);
2510 bmap
= isl_basic_map_simplify(bmap
);
2511 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
2512 return isl_basic_map_finalize(bmap
);
2514 isl_basic_map_free(bmap
);
2518 /* Turn the n dimensions of type type, starting at first
2519 * into existentially quantified variables.
2521 struct isl_basic_set
*isl_basic_set_project_out(struct isl_basic_set
*bset
,
2522 enum isl_dim_type type
, unsigned first
, unsigned n
)
2524 return (isl_basic_set
*)isl_basic_map_project_out(
2525 (isl_basic_map
*)bset
, type
, first
, n
);
2528 /* Turn the n dimensions of type type, starting at first
2529 * into existentially quantified variables.
2531 __isl_give isl_map
*isl_map_project_out(__isl_take isl_map
*map
,
2532 enum isl_dim_type type
, unsigned first
, unsigned n
)
2542 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
2544 map
= isl_map_cow(map
);
2548 map
->dim
= isl_dim_drop(map
->dim
, type
, first
, n
);
2552 for (i
= 0; i
< map
->n
; ++i
) {
2553 map
->p
[i
] = isl_basic_map_project_out(map
->p
[i
], type
, first
, n
);
2564 /* Turn the n dimensions of type type, starting at first
2565 * into existentially quantified variables.
2567 __isl_give isl_set
*isl_set_project_out(__isl_take isl_set
*set
,
2568 enum isl_dim_type type
, unsigned first
, unsigned n
)
2570 return (isl_set
*)isl_map_project_out((isl_map
*)set
, type
, first
, n
);
2573 static struct isl_basic_map
*add_divs(struct isl_basic_map
*bmap
, unsigned n
)
2577 for (i
= 0; i
< n
; ++i
) {
2578 j
= isl_basic_map_alloc_div(bmap
);
2581 isl_seq_clr(bmap
->div
[j
], 1+1+isl_basic_map_total_dim(bmap
));
2585 isl_basic_map_free(bmap
);
2589 struct isl_basic_map
*isl_basic_map_apply_range(
2590 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
2592 struct isl_dim
*dim_result
= NULL
;
2593 struct isl_basic_map
*bmap
;
2594 unsigned n_in
, n_out
, n
, nparam
, total
, pos
;
2595 struct isl_dim_map
*dim_map1
, *dim_map2
;
2597 if (!bmap1
|| !bmap2
)
2600 dim_result
= isl_dim_join(isl_dim_copy(bmap1
->dim
),
2601 isl_dim_copy(bmap2
->dim
));
2603 n_in
= isl_basic_map_n_in(bmap1
);
2604 n_out
= isl_basic_map_n_out(bmap2
);
2605 n
= isl_basic_map_n_out(bmap1
);
2606 nparam
= isl_basic_map_n_param(bmap1
);
2608 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ n
;
2609 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
2610 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
2611 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
2612 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
2613 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
2614 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_in
);
2615 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_out
);
2616 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
2617 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
2618 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
2620 bmap
= isl_basic_map_alloc_dim(dim_result
,
2621 bmap1
->n_div
+ bmap2
->n_div
+ n
,
2622 bmap1
->n_eq
+ bmap2
->n_eq
,
2623 bmap1
->n_ineq
+ bmap2
->n_ineq
);
2624 bmap
= add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
2625 bmap
= add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
2626 bmap
= add_divs(bmap
, n
);
2627 bmap
= isl_basic_map_simplify(bmap
);
2628 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
2629 return isl_basic_map_finalize(bmap
);
2631 isl_basic_map_free(bmap1
);
2632 isl_basic_map_free(bmap2
);
2636 struct isl_basic_set
*isl_basic_set_apply(
2637 struct isl_basic_set
*bset
, struct isl_basic_map
*bmap
)
2642 isl_assert(bset
->ctx
, isl_basic_map_compatible_domain(bmap
, bset
),
2645 return (struct isl_basic_set
*)
2646 isl_basic_map_apply_range((struct isl_basic_map
*)bset
, bmap
);
2648 isl_basic_set_free(bset
);
2649 isl_basic_map_free(bmap
);
2653 struct isl_basic_map
*isl_basic_map_apply_domain(
2654 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
2656 if (!bmap1
|| !bmap2
)
2659 isl_assert(bmap1
->ctx
,
2660 isl_basic_map_n_in(bmap1
) == isl_basic_map_n_in(bmap2
), goto error
);
2661 isl_assert(bmap1
->ctx
,
2662 isl_basic_map_n_param(bmap1
) == isl_basic_map_n_param(bmap2
),
2665 bmap1
= isl_basic_map_reverse(bmap1
);
2666 bmap1
= isl_basic_map_apply_range(bmap1
, bmap2
);
2667 return isl_basic_map_reverse(bmap1
);
2669 isl_basic_map_free(bmap1
);
2670 isl_basic_map_free(bmap2
);
2674 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
2675 * A \cap B -> f(A) + f(B)
2677 struct isl_basic_map
*isl_basic_map_sum(
2678 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
2680 unsigned n_in
, n_out
, nparam
, total
, pos
;
2681 struct isl_basic_map
*bmap
= NULL
;
2682 struct isl_dim_map
*dim_map1
, *dim_map2
;
2685 if (!bmap1
|| !bmap2
)
2688 isl_assert(bmap1
->ctx
, isl_dim_equal(bmap1
->dim
, bmap2
->dim
),
2691 nparam
= isl_basic_map_n_param(bmap1
);
2692 n_in
= isl_basic_map_n_in(bmap1
);
2693 n_out
= isl_basic_map_n_out(bmap1
);
2695 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
;
2696 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
2697 dim_map2
= isl_dim_map_alloc(bmap2
->ctx
, total
);
2698 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
2699 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
);
2700 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
2701 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
2702 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_in
+ n_out
);
2703 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
2704 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
2705 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_out
);
2707 bmap
= isl_basic_map_alloc_dim(isl_dim_copy(bmap1
->dim
),
2708 bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
,
2709 bmap1
->n_eq
+ bmap2
->n_eq
+ n_out
,
2710 bmap1
->n_ineq
+ bmap2
->n_ineq
);
2711 for (i
= 0; i
< n_out
; ++i
) {
2712 int j
= isl_basic_map_alloc_equality(bmap
);
2715 isl_seq_clr(bmap
->eq
[j
], 1+total
);
2716 isl_int_set_si(bmap
->eq
[j
][1+nparam
+n_in
+i
], -1);
2717 isl_int_set_si(bmap
->eq
[j
][1+pos
+i
], 1);
2718 isl_int_set_si(bmap
->eq
[j
][1+pos
-n_out
+i
], 1);
2720 bmap
= add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
2721 bmap
= add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
2722 bmap
= add_divs(bmap
, 2 * n_out
);
2724 bmap
= isl_basic_map_simplify(bmap
);
2725 return isl_basic_map_finalize(bmap
);
2727 isl_basic_map_free(bmap
);
2728 isl_basic_map_free(bmap1
);
2729 isl_basic_map_free(bmap2
);
2733 /* Given two maps A -> f(A) and B -> g(B), construct a map
2734 * A \cap B -> f(A) + f(B)
2736 struct isl_map
*isl_map_sum(struct isl_map
*map1
, struct isl_map
*map2
)
2738 struct isl_map
*result
;
2744 isl_assert(map1
->ctx
, isl_dim_equal(map1
->dim
, map2
->dim
), goto error
);
2746 result
= isl_map_alloc_dim(isl_dim_copy(map1
->dim
),
2747 map1
->n
* map2
->n
, 0);
2750 for (i
= 0; i
< map1
->n
; ++i
)
2751 for (j
= 0; j
< map2
->n
; ++j
) {
2752 struct isl_basic_map
*part
;
2753 part
= isl_basic_map_sum(
2754 isl_basic_map_copy(map1
->p
[i
]),
2755 isl_basic_map_copy(map2
->p
[j
]));
2756 if (isl_basic_map_is_empty(part
))
2757 isl_basic_map_free(part
);
2759 result
= isl_map_add_basic_map(result
, part
);
2772 __isl_give isl_set
*isl_set_sum(__isl_take isl_set
*set1
,
2773 __isl_take isl_set
*set2
)
2775 return (isl_set
*)isl_map_sum((isl_map
*)set1
, (isl_map
*)set2
);
2778 /* Given a basic map A -> f(A), construct A -> -f(A).
2780 struct isl_basic_map
*isl_basic_map_neg(struct isl_basic_map
*bmap
)
2785 bmap
= isl_basic_map_cow(bmap
);
2789 n
= isl_basic_map_dim(bmap
, isl_dim_out
);
2790 off
= isl_basic_map_offset(bmap
, isl_dim_out
);
2791 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2792 for (j
= 0; j
< n
; ++j
)
2793 isl_int_neg(bmap
->eq
[i
][off
+j
], bmap
->eq
[i
][off
+j
]);
2794 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2795 for (j
= 0; j
< n
; ++j
)
2796 isl_int_neg(bmap
->ineq
[i
][off
+j
], bmap
->ineq
[i
][off
+j
]);
2797 for (i
= 0; i
< bmap
->n_div
; ++i
)
2798 for (j
= 0; j
< n
; ++j
)
2799 isl_int_neg(bmap
->div
[i
][1+off
+j
], bmap
->div
[i
][1+off
+j
]);
2800 return isl_basic_map_finalize(bmap
);
2803 /* Given a map A -> f(A), construct A -> -f(A).
2805 struct isl_map
*isl_map_neg(struct isl_map
*map
)
2809 map
= isl_map_cow(map
);
2813 for (i
= 0; i
< map
->n
; ++i
) {
2814 map
->p
[i
] = isl_basic_map_neg(map
->p
[i
]);
2825 __isl_give isl_set
*isl_set_neg(__isl_take isl_set
*set
)
2827 return (isl_set
*)isl_map_neg((isl_map
*)set
);
2830 /* Given a basic map A -> f(A) and an integer d, construct a basic map
2831 * A -> floor(f(A)/d).
2833 struct isl_basic_map
*isl_basic_map_floordiv(struct isl_basic_map
*bmap
,
2836 unsigned n_in
, n_out
, nparam
, total
, pos
;
2837 struct isl_basic_map
*result
= NULL
;
2838 struct isl_dim_map
*dim_map
;
2844 nparam
= isl_basic_map_n_param(bmap
);
2845 n_in
= isl_basic_map_n_in(bmap
);
2846 n_out
= isl_basic_map_n_out(bmap
);
2848 total
= nparam
+ n_in
+ n_out
+ bmap
->n_div
+ n_out
;
2849 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
2850 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_param
, pos
= 0);
2851 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_in
, pos
+= nparam
);
2852 isl_dim_map_div(dim_map
, bmap
, pos
+= n_in
+ n_out
);
2853 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_out
, pos
+= bmap
->n_div
);
2855 result
= isl_basic_map_alloc_dim(isl_dim_copy(bmap
->dim
),
2856 bmap
->n_div
+ n_out
,
2857 bmap
->n_eq
, bmap
->n_ineq
+ 2 * n_out
);
2858 result
= add_constraints_dim_map(result
, bmap
, dim_map
);
2859 result
= add_divs(result
, n_out
);
2860 for (i
= 0; i
< n_out
; ++i
) {
2862 j
= isl_basic_map_alloc_inequality(result
);
2865 isl_seq_clr(result
->ineq
[j
], 1+total
);
2866 isl_int_neg(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
2867 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], 1);
2868 j
= isl_basic_map_alloc_inequality(result
);
2871 isl_seq_clr(result
->ineq
[j
], 1+total
);
2872 isl_int_set(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
2873 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], -1);
2874 isl_int_sub_ui(result
->ineq
[j
][0], d
, 1);
2877 result
= isl_basic_map_simplify(result
);
2878 return isl_basic_map_finalize(result
);
2880 isl_basic_map_free(result
);
2884 /* Given a map A -> f(A) and an integer d, construct a map
2885 * A -> floor(f(A)/d).
2887 struct isl_map
*isl_map_floordiv(struct isl_map
*map
, isl_int d
)
2891 map
= isl_map_cow(map
);
2895 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
2896 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
2897 for (i
= 0; i
< map
->n
; ++i
) {
2898 map
->p
[i
] = isl_basic_map_floordiv(map
->p
[i
], d
);
2909 static struct isl_basic_map
*var_equal(struct isl_basic_map
*bmap
, unsigned pos
)
2915 i
= isl_basic_map_alloc_equality(bmap
);
2918 nparam
= isl_basic_map_n_param(bmap
);
2919 n_in
= isl_basic_map_n_in(bmap
);
2920 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
2921 isl_int_set_si(bmap
->eq
[i
][1+nparam
+pos
], -1);
2922 isl_int_set_si(bmap
->eq
[i
][1+nparam
+n_in
+pos
], 1);
2923 return isl_basic_map_finalize(bmap
);
2925 isl_basic_map_free(bmap
);
2929 /* Add a constraints to "bmap" expressing i_pos < o_pos
2931 static struct isl_basic_map
*var_less(struct isl_basic_map
*bmap
, unsigned pos
)
2937 i
= isl_basic_map_alloc_inequality(bmap
);
2940 nparam
= isl_basic_map_n_param(bmap
);
2941 n_in
= isl_basic_map_n_in(bmap
);
2942 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
2943 isl_int_set_si(bmap
->ineq
[i
][0], -1);
2944 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
2945 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
2946 return isl_basic_map_finalize(bmap
);
2948 isl_basic_map_free(bmap
);
2952 /* Add a constraint to "bmap" expressing i_pos <= o_pos
2954 static __isl_give isl_basic_map
*var_less_or_equal(
2955 __isl_take isl_basic_map
*bmap
, unsigned pos
)
2961 i
= isl_basic_map_alloc_inequality(bmap
);
2964 nparam
= isl_basic_map_n_param(bmap
);
2965 n_in
= isl_basic_map_n_in(bmap
);
2966 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
2967 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
2968 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
2969 return isl_basic_map_finalize(bmap
);
2971 isl_basic_map_free(bmap
);
2975 /* Add a constraints to "bmap" expressing i_pos > o_pos
2977 static struct isl_basic_map
*var_more(struct isl_basic_map
*bmap
, unsigned pos
)
2983 i
= isl_basic_map_alloc_inequality(bmap
);
2986 nparam
= isl_basic_map_n_param(bmap
);
2987 n_in
= isl_basic_map_n_in(bmap
);
2988 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
2989 isl_int_set_si(bmap
->ineq
[i
][0], -1);
2990 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
2991 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
2992 return isl_basic_map_finalize(bmap
);
2994 isl_basic_map_free(bmap
);
2998 /* Add a constraint to "bmap" expressing i_pos >= o_pos
3000 static __isl_give isl_basic_map
*var_more_or_equal(
3001 __isl_take isl_basic_map
*bmap
, unsigned pos
)
3007 i
= isl_basic_map_alloc_inequality(bmap
);
3010 nparam
= isl_basic_map_n_param(bmap
);
3011 n_in
= isl_basic_map_n_in(bmap
);
3012 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3013 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
3014 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
3015 return isl_basic_map_finalize(bmap
);
3017 isl_basic_map_free(bmap
);
3021 struct isl_basic_map
*isl_basic_map_equal(struct isl_dim
*dim
, unsigned n_equal
)
3024 struct isl_basic_map
*bmap
;
3025 bmap
= isl_basic_map_alloc_dim(dim
, 0, n_equal
, 0);
3028 for (i
= 0; i
< n_equal
&& bmap
; ++i
)
3029 bmap
= var_equal(bmap
, i
);
3030 return isl_basic_map_finalize(bmap
);
3033 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
3035 struct isl_basic_map
*isl_basic_map_less_at(struct isl_dim
*dim
, unsigned pos
)
3038 struct isl_basic_map
*bmap
;
3039 bmap
= isl_basic_map_alloc_dim(dim
, 0, pos
, 1);
3042 for (i
= 0; i
< pos
&& bmap
; ++i
)
3043 bmap
= var_equal(bmap
, i
);
3045 bmap
= var_less(bmap
, pos
);
3046 return isl_basic_map_finalize(bmap
);
3049 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
3051 __isl_give isl_basic_map
*isl_basic_map_less_or_equal_at(
3052 __isl_take isl_dim
*dim
, unsigned pos
)
3055 isl_basic_map
*bmap
;
3057 bmap
= isl_basic_map_alloc_dim(dim
, 0, pos
, 1);
3058 for (i
= 0; i
< pos
; ++i
)
3059 bmap
= var_equal(bmap
, i
);
3060 bmap
= var_less_or_equal(bmap
, pos
);
3061 return isl_basic_map_finalize(bmap
);
3064 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
3066 struct isl_basic_map
*isl_basic_map_more_at(struct isl_dim
*dim
, unsigned pos
)
3069 struct isl_basic_map
*bmap
;
3070 bmap
= isl_basic_map_alloc_dim(dim
, 0, pos
, 1);
3073 for (i
= 0; i
< pos
&& bmap
; ++i
)
3074 bmap
= var_equal(bmap
, i
);
3076 bmap
= var_more(bmap
, pos
);
3077 return isl_basic_map_finalize(bmap
);
3080 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
3082 __isl_give isl_basic_map
*isl_basic_map_more_or_equal_at(
3083 __isl_take isl_dim
*dim
, unsigned pos
)
3086 isl_basic_map
*bmap
;
3088 bmap
= isl_basic_map_alloc_dim(dim
, 0, pos
, 1);
3089 for (i
= 0; i
< pos
; ++i
)
3090 bmap
= var_equal(bmap
, i
);
3091 bmap
= var_more_or_equal(bmap
, pos
);
3092 return isl_basic_map_finalize(bmap
);
3095 static __isl_give isl_map
*map_lex_lte_first(__isl_take isl_dim
*dims
,
3096 unsigned n
, int equal
)
3098 struct isl_map
*map
;
3101 if (n
== 0 && equal
)
3102 return isl_map_universe(dims
);
3104 map
= isl_map_alloc_dim(isl_dim_copy(dims
), n
, ISL_MAP_DISJOINT
);
3106 for (i
= 0; i
+ 1 < n
; ++i
)
3107 map
= isl_map_add_basic_map(map
,
3108 isl_basic_map_less_at(isl_dim_copy(dims
), i
));
3111 map
= isl_map_add_basic_map(map
,
3112 isl_basic_map_less_or_equal_at(dims
, n
- 1));
3114 map
= isl_map_add_basic_map(map
,
3115 isl_basic_map_less_at(dims
, n
- 1));
3122 static __isl_give isl_map
*map_lex_lte(__isl_take isl_dim
*dims
, int equal
)
3126 return map_lex_lte_first(dims
, dims
->n_out
, equal
);
3129 __isl_give isl_map
*isl_map_lex_lt_first(__isl_take isl_dim
*dim
, unsigned n
)
3131 return map_lex_lte_first(dim
, n
, 0);
3134 __isl_give isl_map
*isl_map_lex_le_first(__isl_take isl_dim
*dim
, unsigned n
)
3136 return map_lex_lte_first(dim
, n
, 1);
3139 __isl_give isl_map
*isl_map_lex_lt(__isl_take isl_dim
*set_dim
)
3141 return map_lex_lte(isl_dim_map(set_dim
), 0);
3144 __isl_give isl_map
*isl_map_lex_le(__isl_take isl_dim
*set_dim
)
3146 return map_lex_lte(isl_dim_map(set_dim
), 1);
3149 static __isl_give isl_map
*map_lex_gte_first(__isl_take isl_dim
*dims
,
3150 unsigned n
, int equal
)
3152 struct isl_map
*map
;
3155 if (n
== 0 && equal
)
3156 return isl_map_universe(dims
);
3158 map
= isl_map_alloc_dim(isl_dim_copy(dims
), n
, ISL_MAP_DISJOINT
);
3160 for (i
= 0; i
+ 1 < n
; ++i
)
3161 map
= isl_map_add_basic_map(map
,
3162 isl_basic_map_more_at(isl_dim_copy(dims
), i
));
3165 map
= isl_map_add_basic_map(map
,
3166 isl_basic_map_more_or_equal_at(dims
, n
- 1));
3168 map
= isl_map_add_basic_map(map
,
3169 isl_basic_map_more_at(dims
, n
- 1));
3176 static __isl_give isl_map
*map_lex_gte(__isl_take isl_dim
*dims
, int equal
)
3180 return map_lex_gte_first(dims
, dims
->n_out
, equal
);
3183 __isl_give isl_map
*isl_map_lex_gt_first(__isl_take isl_dim
*dim
, unsigned n
)
3185 return map_lex_gte_first(dim
, n
, 0);
3188 __isl_give isl_map
*isl_map_lex_ge_first(__isl_take isl_dim
*dim
, unsigned n
)
3190 return map_lex_gte_first(dim
, n
, 1);
3193 __isl_give isl_map
*isl_map_lex_gt(__isl_take isl_dim
*set_dim
)
3195 return map_lex_gte(isl_dim_map(set_dim
), 0);
3198 __isl_give isl_map
*isl_map_lex_ge(__isl_take isl_dim
*set_dim
)
3200 return map_lex_gte(isl_dim_map(set_dim
), 1);
3203 struct isl_basic_map
*isl_basic_map_from_basic_set(
3204 struct isl_basic_set
*bset
, struct isl_dim
*dim
)
3206 struct isl_basic_map
*bmap
;
3208 bset
= isl_basic_set_cow(bset
);
3212 isl_assert(bset
->ctx
, isl_dim_compatible(bset
->dim
, dim
), goto error
);
3213 isl_dim_free(bset
->dim
);
3214 bmap
= (struct isl_basic_map
*) bset
;
3216 return isl_basic_map_finalize(bmap
);
3218 isl_basic_set_free(bset
);
3223 struct isl_basic_set
*isl_basic_set_from_basic_map(struct isl_basic_map
*bmap
)
3227 if (bmap
->dim
->n_in
== 0)
3228 return (struct isl_basic_set
*)bmap
;
3229 bmap
= isl_basic_map_cow(bmap
);
3232 bmap
->dim
= isl_dim_as_set_dim(bmap
->dim
);
3235 bmap
= isl_basic_map_finalize(bmap
);
3236 return (struct isl_basic_set
*)bmap
;
3238 isl_basic_map_free(bmap
);
3242 /* For a div d = floor(f/m), add the constraints
3245 * -(f-(n-1)) + m d >= 0
3247 * Note that the second constraint is the negation of
3251 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map
*bmap
,
3252 unsigned pos
, isl_int
*div
)
3255 unsigned total
= isl_basic_map_total_dim(bmap
);
3257 i
= isl_basic_map_alloc_inequality(bmap
);
3260 isl_seq_cpy(bmap
->ineq
[i
], div
+ 1, 1 + total
);
3261 isl_int_neg(bmap
->ineq
[i
][1 + pos
], div
[0]);
3263 j
= isl_basic_map_alloc_inequality(bmap
);
3266 isl_seq_neg(bmap
->ineq
[j
], bmap
->ineq
[i
], 1 + total
);
3267 isl_int_add(bmap
->ineq
[j
][0], bmap
->ineq
[j
][0], bmap
->ineq
[j
][1 + pos
]);
3268 isl_int_sub_ui(bmap
->ineq
[j
][0], bmap
->ineq
[j
][0], 1);
3272 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set
*bset
,
3273 unsigned pos
, isl_int
*div
)
3275 return isl_basic_map_add_div_constraints_var((isl_basic_map
*)bset
,
3279 int isl_basic_map_add_div_constraints(struct isl_basic_map
*bmap
, unsigned div
)
3281 unsigned total
= isl_basic_map_total_dim(bmap
);
3282 unsigned div_pos
= total
- bmap
->n_div
+ div
;
3284 return isl_basic_map_add_div_constraints_var(bmap
, div_pos
,
3288 struct isl_basic_set
*isl_basic_map_underlying_set(
3289 struct isl_basic_map
*bmap
)
3293 if (bmap
->dim
->nparam
== 0 && bmap
->dim
->n_in
== 0 &&
3294 bmap
->n_div
== 0 && !isl_dim_get_tuple_name(bmap
->dim
, isl_dim_out
))
3295 return (struct isl_basic_set
*)bmap
;
3296 bmap
= isl_basic_map_cow(bmap
);
3299 bmap
->dim
= isl_dim_underlying(bmap
->dim
, bmap
->n_div
);
3302 bmap
->extra
-= bmap
->n_div
;
3304 bmap
= isl_basic_map_finalize(bmap
);
3305 return (struct isl_basic_set
*)bmap
;
3310 __isl_give isl_basic_set
*isl_basic_set_underlying_set(
3311 __isl_take isl_basic_set
*bset
)
3313 return isl_basic_map_underlying_set((isl_basic_map
*)bset
);
3316 struct isl_basic_map
*isl_basic_map_overlying_set(
3317 struct isl_basic_set
*bset
, struct isl_basic_map
*like
)
3319 struct isl_basic_map
*bmap
;
3320 struct isl_ctx
*ctx
;
3327 isl_assert(ctx
, bset
->n_div
== 0, goto error
);
3328 isl_assert(ctx
, isl_basic_set_n_param(bset
) == 0, goto error
);
3329 isl_assert(ctx
, bset
->dim
->n_out
== isl_basic_map_total_dim(like
),
3331 if (isl_dim_equal(bset
->dim
, like
->dim
) && like
->n_div
== 0) {
3332 isl_basic_map_free(like
);
3333 return (struct isl_basic_map
*)bset
;
3335 bset
= isl_basic_set_cow(bset
);
3338 total
= bset
->dim
->n_out
+ bset
->extra
;
3339 bmap
= (struct isl_basic_map
*)bset
;
3340 isl_dim_free(bmap
->dim
);
3341 bmap
->dim
= isl_dim_copy(like
->dim
);
3344 bmap
->n_div
= like
->n_div
;
3345 bmap
->extra
+= like
->n_div
;
3348 ltotal
= total
- bmap
->extra
+ like
->extra
;
3351 bmap
->block2
= isl_blk_extend(ctx
, bmap
->block2
,
3352 bmap
->extra
* (1 + 1 + total
));
3353 if (isl_blk_is_error(bmap
->block2
))
3355 bmap
->div
= isl_realloc_array(ctx
, bmap
->div
, isl_int
*,
3359 for (i
= 0; i
< bmap
->extra
; ++i
)
3360 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + 1 + total
);
3361 for (i
= 0; i
< like
->n_div
; ++i
) {
3362 isl_seq_cpy(bmap
->div
[i
], like
->div
[i
], 1 + 1 + ltotal
);
3363 isl_seq_clr(bmap
->div
[i
]+1+1+ltotal
, total
- ltotal
);
3365 bmap
= isl_basic_map_extend_constraints(bmap
,
3366 0, 2 * like
->n_div
);
3367 for (i
= 0; i
< like
->n_div
; ++i
) {
3368 if (isl_int_is_zero(bmap
->div
[i
][0]))
3370 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
3374 isl_basic_map_free(like
);
3375 bmap
= isl_basic_map_simplify(bmap
);
3376 bmap
= isl_basic_map_finalize(bmap
);
3379 isl_basic_map_free(like
);
3380 isl_basic_set_free(bset
);
3384 struct isl_basic_set
*isl_basic_set_from_underlying_set(
3385 struct isl_basic_set
*bset
, struct isl_basic_set
*like
)
3387 return (struct isl_basic_set
*)
3388 isl_basic_map_overlying_set(bset
, (struct isl_basic_map
*)like
);
3391 struct isl_set
*isl_set_from_underlying_set(
3392 struct isl_set
*set
, struct isl_basic_set
*like
)
3398 isl_assert(set
->ctx
, set
->dim
->n_out
== isl_basic_set_total_dim(like
),
3400 if (isl_dim_equal(set
->dim
, like
->dim
) && like
->n_div
== 0) {
3401 isl_basic_set_free(like
);
3404 set
= isl_set_cow(set
);
3407 for (i
= 0; i
< set
->n
; ++i
) {
3408 set
->p
[i
] = isl_basic_set_from_underlying_set(set
->p
[i
],
3409 isl_basic_set_copy(like
));
3413 isl_dim_free(set
->dim
);
3414 set
->dim
= isl_dim_copy(like
->dim
);
3417 isl_basic_set_free(like
);
3420 isl_basic_set_free(like
);
3425 struct isl_set
*isl_map_underlying_set(struct isl_map
*map
)
3429 map
= isl_map_cow(map
);
3432 map
->dim
= isl_dim_cow(map
->dim
);
3436 for (i
= 1; i
< map
->n
; ++i
)
3437 isl_assert(map
->ctx
, map
->p
[0]->n_div
== map
->p
[i
]->n_div
,
3439 for (i
= 0; i
< map
->n
; ++i
) {
3440 map
->p
[i
] = (struct isl_basic_map
*)
3441 isl_basic_map_underlying_set(map
->p
[i
]);
3446 map
->dim
= isl_dim_underlying(map
->dim
, 0);
3448 isl_dim_free(map
->dim
);
3449 map
->dim
= isl_dim_copy(map
->p
[0]->dim
);
3453 return (struct isl_set
*)map
;
3459 struct isl_set
*isl_set_to_underlying_set(struct isl_set
*set
)
3461 return (struct isl_set
*)isl_map_underlying_set((struct isl_map
*)set
);
3464 static __isl_give isl_basic_map
*isl_basic_map_reset_dim(
3465 __isl_take isl_basic_map
*bmap
, __isl_take isl_dim
*dim
)
3467 bmap
= isl_basic_map_cow(bmap
);
3471 isl_dim_free(bmap
->dim
);
3474 bmap
= isl_basic_map_finalize(bmap
);
3478 isl_basic_map_free(bmap
);
3483 static __isl_give isl_basic_set
*isl_basic_set_reset_dim(
3484 __isl_take isl_basic_set
*bset
, __isl_take isl_dim
*dim
)
3486 return (isl_basic_set
*)isl_basic_map_reset_dim((isl_basic_map
*)bset
,
3490 __isl_give isl_map
*isl_map_reset_dim(__isl_take isl_map
*map
,
3491 __isl_take isl_dim
*dim
)
3495 map
= isl_map_cow(map
);
3499 for (i
= 0; i
< map
->n
; ++i
) {
3500 map
->p
[i
] = isl_basic_map_reset_dim(map
->p
[i
],
3505 isl_dim_free(map
->dim
);
3515 __isl_give isl_set
*isl_set_reset_dim(__isl_take isl_set
*set
,
3516 __isl_take isl_dim
*dim
)
3518 return (struct isl_set
*) isl_map_reset_dim((struct isl_map
*)set
, dim
);
3521 struct isl_basic_set
*isl_basic_map_domain(struct isl_basic_map
*bmap
)
3524 struct isl_basic_set
*domain
;
3530 dim
= isl_dim_domain(isl_basic_map_get_dim(bmap
));
3532 n_in
= isl_basic_map_n_in(bmap
);
3533 n_out
= isl_basic_map_n_out(bmap
);
3534 domain
= isl_basic_set_from_basic_map(bmap
);
3535 domain
= isl_basic_set_project_out(domain
, isl_dim_set
, n_in
, n_out
);
3537 domain
= isl_basic_set_reset_dim(domain
, dim
);
3542 struct isl_basic_set
*isl_basic_map_range(struct isl_basic_map
*bmap
)
3544 return isl_basic_map_domain(isl_basic_map_reverse(bmap
));
3547 struct isl_set
*isl_map_range(struct isl_map
*map
)
3550 struct isl_set
*set
;
3554 if (isl_map_dim(map
, isl_dim_in
) == 0)
3555 return (isl_set
*)map
;
3557 map
= isl_map_cow(map
);
3561 set
= (struct isl_set
*) map
;
3562 set
->dim
= isl_dim_drop_inputs(set
->dim
, 0, set
->dim
->n_in
);
3565 for (i
= 0; i
< map
->n
; ++i
) {
3566 set
->p
[i
] = isl_basic_map_range(map
->p
[i
]);
3570 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
3571 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
3578 struct isl_map
*isl_map_from_set(struct isl_set
*set
, struct isl_dim
*dim
)
3581 struct isl_map
*map
= NULL
;
3583 set
= isl_set_cow(set
);
3586 isl_assert(set
->ctx
, isl_dim_compatible(set
->dim
, dim
), goto error
);
3587 map
= (struct isl_map
*)set
;
3588 for (i
= 0; i
< set
->n
; ++i
) {
3589 map
->p
[i
] = isl_basic_map_from_basic_set(
3590 set
->p
[i
], isl_dim_copy(dim
));
3594 isl_dim_free(map
->dim
);
3603 struct isl_map
*isl_map_from_range(struct isl_set
*set
)
3605 return (struct isl_map
*)set
;
3608 __isl_give isl_map
*isl_map_from_domain(__isl_take isl_set
*set
)
3610 return isl_map_reverse(isl_map_from_range(set
));
3613 __isl_give isl_map
*isl_map_from_domain_and_range(__isl_take isl_set
*domain
,
3614 __isl_take isl_set
*range
)
3616 return isl_map_apply_range(isl_map_from_domain(domain
),
3617 isl_map_from_range(range
));
3620 struct isl_set
*isl_set_from_map(struct isl_map
*map
)
3623 struct isl_set
*set
= NULL
;
3627 map
= isl_map_cow(map
);
3630 map
->dim
= isl_dim_as_set_dim(map
->dim
);
3633 set
= (struct isl_set
*)map
;
3634 for (i
= 0; i
< map
->n
; ++i
) {
3635 set
->p
[i
] = isl_basic_set_from_basic_map(map
->p
[i
]);
3645 struct isl_map
*isl_map_alloc_dim(struct isl_dim
*dim
, int n
, unsigned flags
)
3647 struct isl_map
*map
;
3651 isl_assert(dim
->ctx
, n
>= 0, return NULL
);
3652 map
= isl_alloc(dim
->ctx
, struct isl_map
,
3653 sizeof(struct isl_map
) +
3654 (n
- 1) * sizeof(struct isl_basic_map
*));
3658 map
->ctx
= dim
->ctx
;
3659 isl_ctx_ref(map
->ctx
);
3671 struct isl_map
*isl_map_alloc(struct isl_ctx
*ctx
,
3672 unsigned nparam
, unsigned in
, unsigned out
, int n
,
3675 struct isl_map
*map
;
3676 struct isl_dim
*dims
;
3678 dims
= isl_dim_alloc(ctx
, nparam
, in
, out
);
3682 map
= isl_map_alloc_dim(dims
, n
, flags
);
3686 struct isl_basic_map
*isl_basic_map_empty(struct isl_dim
*dim
)
3688 struct isl_basic_map
*bmap
;
3689 bmap
= isl_basic_map_alloc_dim(dim
, 0, 1, 0);
3690 bmap
= isl_basic_map_set_to_empty(bmap
);
3694 struct isl_basic_set
*isl_basic_set_empty(struct isl_dim
*dim
)
3696 struct isl_basic_set
*bset
;
3697 bset
= isl_basic_set_alloc_dim(dim
, 0, 1, 0);
3698 bset
= isl_basic_set_set_to_empty(bset
);
3702 struct isl_basic_map
*isl_basic_map_empty_like(struct isl_basic_map
*model
)
3704 struct isl_basic_map
*bmap
;
3707 bmap
= isl_basic_map_alloc_dim(isl_dim_copy(model
->dim
), 0, 1, 0);
3708 bmap
= isl_basic_map_set_to_empty(bmap
);
3712 struct isl_basic_map
*isl_basic_map_empty_like_map(struct isl_map
*model
)
3714 struct isl_basic_map
*bmap
;
3717 bmap
= isl_basic_map_alloc_dim(isl_dim_copy(model
->dim
), 0, 1, 0);
3718 bmap
= isl_basic_map_set_to_empty(bmap
);
3722 struct isl_basic_set
*isl_basic_set_empty_like(struct isl_basic_set
*model
)
3724 struct isl_basic_set
*bset
;
3727 bset
= isl_basic_set_alloc_dim(isl_dim_copy(model
->dim
), 0, 1, 0);
3728 bset
= isl_basic_set_set_to_empty(bset
);
3732 struct isl_basic_map
*isl_basic_map_universe(struct isl_dim
*dim
)
3734 struct isl_basic_map
*bmap
;
3735 bmap
= isl_basic_map_alloc_dim(dim
, 0, 0, 0);
3739 struct isl_basic_set
*isl_basic_set_universe(struct isl_dim
*dim
)
3741 struct isl_basic_set
*bset
;
3742 bset
= isl_basic_set_alloc_dim(dim
, 0, 0, 0);
3746 __isl_give isl_basic_map
*isl_basic_map_universe_like(
3747 __isl_keep isl_basic_map
*model
)
3751 return isl_basic_map_alloc_dim(isl_dim_copy(model
->dim
), 0, 0, 0);
3754 struct isl_basic_set
*isl_basic_set_universe_like(struct isl_basic_set
*model
)
3758 return isl_basic_set_alloc_dim(isl_dim_copy(model
->dim
), 0, 0, 0);
3761 __isl_give isl_basic_set
*isl_basic_set_universe_like_set(
3762 __isl_keep isl_set
*model
)
3766 return isl_basic_set_alloc_dim(isl_dim_copy(model
->dim
), 0, 0, 0);
3769 struct isl_map
*isl_map_empty(struct isl_dim
*dim
)
3771 return isl_map_alloc_dim(dim
, 0, ISL_MAP_DISJOINT
);
3774 struct isl_map
*isl_map_empty_like(struct isl_map
*model
)
3778 return isl_map_alloc_dim(isl_dim_copy(model
->dim
), 0, ISL_MAP_DISJOINT
);
3781 struct isl_map
*isl_map_empty_like_basic_map(struct isl_basic_map
*model
)
3785 return isl_map_alloc_dim(isl_dim_copy(model
->dim
), 0, ISL_MAP_DISJOINT
);
3788 struct isl_set
*isl_set_empty(struct isl_dim
*dim
)
3790 return isl_set_alloc_dim(dim
, 0, ISL_MAP_DISJOINT
);
3793 struct isl_set
*isl_set_empty_like(struct isl_set
*model
)
3797 return isl_set_empty(isl_dim_copy(model
->dim
));
3800 struct isl_map
*isl_map_universe(struct isl_dim
*dim
)
3802 struct isl_map
*map
;
3805 map
= isl_map_alloc_dim(isl_dim_copy(dim
), 1, ISL_MAP_DISJOINT
);
3806 map
= isl_map_add_basic_map(map
, isl_basic_map_universe(dim
));
3810 struct isl_set
*isl_set_universe(struct isl_dim
*dim
)
3812 struct isl_set
*set
;
3815 set
= isl_set_alloc_dim(isl_dim_copy(dim
), 1, ISL_MAP_DISJOINT
);
3816 set
= isl_set_add_basic_set(set
, isl_basic_set_universe(dim
));
3820 __isl_give isl_set
*isl_set_universe_like(__isl_keep isl_set
*model
)
3824 return isl_set_universe(isl_dim_copy(model
->dim
));
3827 struct isl_map
*isl_map_dup(struct isl_map
*map
)
3830 struct isl_map
*dup
;
3834 dup
= isl_map_alloc_dim(isl_dim_copy(map
->dim
), map
->n
, map
->flags
);
3835 for (i
= 0; i
< map
->n
; ++i
)
3836 dup
= isl_map_add_basic_map(dup
, isl_basic_map_copy(map
->p
[i
]));
3840 __isl_give isl_map
*isl_map_add_basic_map(__isl_take isl_map
*map
,
3841 __isl_take isl_basic_map
*bmap
)
3845 if (isl_basic_map_fast_is_empty(bmap
)) {
3846 isl_basic_map_free(bmap
);
3849 isl_assert(map
->ctx
, isl_dim_equal(map
->dim
, bmap
->dim
), goto error
);
3850 isl_assert(map
->ctx
, map
->n
< map
->size
, goto error
);
3851 map
->p
[map
->n
] = bmap
;
3853 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3859 isl_basic_map_free(bmap
);
3863 void isl_map_free(struct isl_map
*map
)
3873 isl_ctx_deref(map
->ctx
);
3874 for (i
= 0; i
< map
->n
; ++i
)
3875 isl_basic_map_free(map
->p
[i
]);
3876 isl_dim_free(map
->dim
);
3880 struct isl_map
*isl_map_extend(struct isl_map
*base
,
3881 unsigned nparam
, unsigned n_in
, unsigned n_out
)
3885 base
= isl_map_cow(base
);
3889 base
->dim
= isl_dim_extend(base
->dim
, nparam
, n_in
, n_out
);
3892 for (i
= 0; i
< base
->n
; ++i
) {
3893 base
->p
[i
] = isl_basic_map_extend_dim(base
->p
[i
],
3894 isl_dim_copy(base
->dim
), 0, 0, 0);
3904 struct isl_set
*isl_set_extend(struct isl_set
*base
,
3905 unsigned nparam
, unsigned dim
)
3907 return (struct isl_set
*)isl_map_extend((struct isl_map
*)base
,
3911 static struct isl_basic_map
*isl_basic_map_fix_pos_si(
3912 struct isl_basic_map
*bmap
, unsigned pos
, int value
)
3916 bmap
= isl_basic_map_cow(bmap
);
3917 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
3918 j
= isl_basic_map_alloc_equality(bmap
);
3921 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
3922 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
3923 isl_int_set_si(bmap
->eq
[j
][0], value
);
3924 bmap
= isl_basic_map_simplify(bmap
);
3925 return isl_basic_map_finalize(bmap
);
3927 isl_basic_map_free(bmap
);
3931 static __isl_give isl_basic_map
*isl_basic_map_fix_pos(
3932 __isl_take isl_basic_map
*bmap
, unsigned pos
, isl_int value
)
3936 bmap
= isl_basic_map_cow(bmap
);
3937 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
3938 j
= isl_basic_map_alloc_equality(bmap
);
3941 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
3942 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
3943 isl_int_set(bmap
->eq
[j
][0], value
);
3944 bmap
= isl_basic_map_simplify(bmap
);
3945 return isl_basic_map_finalize(bmap
);
3947 isl_basic_map_free(bmap
);
3951 struct isl_basic_map
*isl_basic_map_fix_si(struct isl_basic_map
*bmap
,
3952 enum isl_dim_type type
, unsigned pos
, int value
)
3956 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
3957 return isl_basic_map_fix_pos_si(bmap
,
3958 isl_basic_map_offset(bmap
, type
) + pos
, value
);
3960 isl_basic_map_free(bmap
);
3964 __isl_give isl_basic_map
*isl_basic_map_fix(__isl_take isl_basic_map
*bmap
,
3965 enum isl_dim_type type
, unsigned pos
, isl_int value
)
3969 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
3970 return isl_basic_map_fix_pos(bmap
,
3971 isl_basic_map_offset(bmap
, type
) + pos
, value
);
3973 isl_basic_map_free(bmap
);
3977 struct isl_basic_set
*isl_basic_set_fix_si(struct isl_basic_set
*bset
,
3978 enum isl_dim_type type
, unsigned pos
, int value
)
3980 return (struct isl_basic_set
*)
3981 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
3985 __isl_give isl_basic_set
*isl_basic_set_fix(__isl_take isl_basic_set
*bset
,
3986 enum isl_dim_type type
, unsigned pos
, isl_int value
)
3988 return (struct isl_basic_set
*)
3989 isl_basic_map_fix((struct isl_basic_map
*)bset
,
3993 struct isl_basic_map
*isl_basic_map_fix_input_si(struct isl_basic_map
*bmap
,
3994 unsigned input
, int value
)
3996 return isl_basic_map_fix_si(bmap
, isl_dim_in
, input
, value
);
3999 struct isl_basic_set
*isl_basic_set_fix_dim_si(struct isl_basic_set
*bset
,
4000 unsigned dim
, int value
)
4002 return (struct isl_basic_set
*)
4003 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
4004 isl_dim_set
, dim
, value
);
4007 struct isl_map
*isl_map_fix_si(struct isl_map
*map
,
4008 enum isl_dim_type type
, unsigned pos
, int value
)
4012 map
= isl_map_cow(map
);
4016 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
4017 for (i
= 0; i
< map
->n
; ++i
) {
4018 map
->p
[i
] = isl_basic_map_fix_si(map
->p
[i
], type
, pos
, value
);
4022 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4029 __isl_give isl_set
*isl_set_fix_si(__isl_take isl_set
*set
,
4030 enum isl_dim_type type
, unsigned pos
, int value
)
4032 return (struct isl_set
*)
4033 isl_map_fix_si((struct isl_map
*)set
, type
, pos
, value
);
4036 __isl_give isl_map
*isl_map_fix(__isl_take isl_map
*map
,
4037 enum isl_dim_type type
, unsigned pos
, isl_int value
)
4041 map
= isl_map_cow(map
);
4045 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
4046 for (i
= 0; i
< map
->n
; ++i
) {
4047 map
->p
[i
] = isl_basic_map_fix(map
->p
[i
], type
, pos
, value
);
4051 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4058 __isl_give isl_set
*isl_set_fix(__isl_take isl_set
*set
,
4059 enum isl_dim_type type
, unsigned pos
, isl_int value
)
4061 return (struct isl_set
*)isl_map_fix((isl_map
*)set
, type
, pos
, value
);
4064 struct isl_map
*isl_map_fix_input_si(struct isl_map
*map
,
4065 unsigned input
, int value
)
4067 return isl_map_fix_si(map
, isl_dim_in
, input
, value
);
4070 struct isl_set
*isl_set_fix_dim_si(struct isl_set
*set
, unsigned dim
, int value
)
4072 return (struct isl_set
*)
4073 isl_map_fix_si((struct isl_map
*)set
, isl_dim_set
, dim
, value
);
4076 __isl_give isl_basic_map
*isl_basic_map_lower_bound_si(
4077 __isl_take isl_basic_map
*bmap
,
4078 enum isl_dim_type type
, unsigned pos
, int value
)
4084 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
4085 pos
+= isl_basic_map_offset(bmap
, type
);
4086 bmap
= isl_basic_map_cow(bmap
);
4087 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
4088 j
= isl_basic_map_alloc_inequality(bmap
);
4091 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
4092 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
4093 isl_int_set_si(bmap
->ineq
[j
][0], -value
);
4094 bmap
= isl_basic_map_simplify(bmap
);
4095 return isl_basic_map_finalize(bmap
);
4097 isl_basic_map_free(bmap
);
4101 struct isl_basic_set
*isl_basic_set_lower_bound_dim(struct isl_basic_set
*bset
,
4102 unsigned dim
, isl_int value
)
4106 bset
= isl_basic_set_cow(bset
);
4107 bset
= isl_basic_set_extend_constraints(bset
, 0, 1);
4108 j
= isl_basic_set_alloc_inequality(bset
);
4111 isl_seq_clr(bset
->ineq
[j
], 1 + isl_basic_set_total_dim(bset
));
4112 isl_int_set_si(bset
->ineq
[j
][1 + isl_basic_set_n_param(bset
) + dim
], 1);
4113 isl_int_neg(bset
->ineq
[j
][0], value
);
4114 bset
= isl_basic_set_simplify(bset
);
4115 return isl_basic_set_finalize(bset
);
4117 isl_basic_set_free(bset
);
4121 __isl_give isl_map
*isl_map_lower_bound_si(__isl_take isl_map
*map
,
4122 enum isl_dim_type type
, unsigned pos
, int value
)
4126 map
= isl_map_cow(map
);
4130 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
4131 for (i
= 0; i
< map
->n
; ++i
) {
4132 map
->p
[i
] = isl_basic_map_lower_bound_si(map
->p
[i
],
4137 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4144 __isl_give isl_set
*isl_set_lower_bound_si(__isl_take isl_set
*set
,
4145 enum isl_dim_type type
, unsigned pos
, int value
)
4147 return (struct isl_set
*)
4148 isl_map_lower_bound_si((struct isl_map
*)set
, type
, pos
, value
);
4151 struct isl_set
*isl_set_lower_bound_dim(struct isl_set
*set
, unsigned dim
,
4156 set
= isl_set_cow(set
);
4160 isl_assert(set
->ctx
, dim
< isl_set_n_dim(set
), goto error
);
4161 for (i
= 0; i
< set
->n
; ++i
) {
4162 set
->p
[i
] = isl_basic_set_lower_bound_dim(set
->p
[i
], dim
, value
);
4172 struct isl_map
*isl_map_reverse(struct isl_map
*map
)
4176 map
= isl_map_cow(map
);
4180 map
->dim
= isl_dim_reverse(map
->dim
);
4183 for (i
= 0; i
< map
->n
; ++i
) {
4184 map
->p
[i
] = isl_basic_map_reverse(map
->p
[i
]);
4188 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4195 static struct isl_map
*isl_basic_map_partial_lexopt(
4196 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
4197 struct isl_set
**empty
, int max
)
4201 if (bmap
->ctx
->opt
->pip
== ISL_PIP_PIP
)
4202 return isl_pip_basic_map_lexopt(bmap
, dom
, empty
, max
);
4204 return isl_tab_basic_map_partial_lexopt(bmap
, dom
, empty
, max
);
4206 isl_basic_map_free(bmap
);
4207 isl_basic_set_free(dom
);
4213 struct isl_map
*isl_basic_map_partial_lexmax(
4214 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
4215 struct isl_set
**empty
)
4217 return isl_basic_map_partial_lexopt(bmap
, dom
, empty
, 1);
4220 struct isl_map
*isl_basic_map_partial_lexmin(
4221 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
4222 struct isl_set
**empty
)
4224 return isl_basic_map_partial_lexopt(bmap
, dom
, empty
, 0);
4227 struct isl_set
*isl_basic_set_partial_lexmin(
4228 struct isl_basic_set
*bset
, struct isl_basic_set
*dom
,
4229 struct isl_set
**empty
)
4231 return (struct isl_set
*)
4232 isl_basic_map_partial_lexmin((struct isl_basic_map
*)bset
,
4236 struct isl_set
*isl_basic_set_partial_lexmax(
4237 struct isl_basic_set
*bset
, struct isl_basic_set
*dom
,
4238 struct isl_set
**empty
)
4240 return (struct isl_set
*)
4241 isl_basic_map_partial_lexmax((struct isl_basic_map
*)bset
,
4245 /* Given a basic map "bmap", compute the lexicograhically minimal
4246 * (or maximal) image element for each domain element in dom.
4247 * Set *empty to those elements in dom that do not have an image element.
4249 * We first make sure the basic sets in dom are disjoint and then
4250 * simply collect the results over each of the basic sets separately.
4251 * We could probably improve the efficiency a bit by moving the union
4252 * domain down into the parametric integer programming.
4254 static __isl_give isl_map
*basic_map_partial_lexopt(
4255 __isl_take isl_basic_map
*bmap
, __isl_take isl_set
*dom
,
4256 __isl_give isl_set
**empty
, int max
)
4259 struct isl_map
*res
;
4261 dom
= isl_set_make_disjoint(dom
);
4265 if (isl_set_fast_is_empty(dom
)) {
4266 res
= isl_map_empty_like_basic_map(bmap
);
4267 *empty
= isl_set_empty_like(dom
);
4269 isl_basic_map_free(bmap
);
4273 res
= isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap
),
4274 isl_basic_set_copy(dom
->p
[0]), empty
, max
);
4276 for (i
= 1; i
< dom
->n
; ++i
) {
4277 struct isl_map
*res_i
;
4278 struct isl_set
*empty_i
;
4280 res_i
= isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap
),
4281 isl_basic_set_copy(dom
->p
[i
]), &empty_i
, max
);
4283 res
= isl_map_union_disjoint(res
, res_i
);
4284 *empty
= isl_set_union_disjoint(*empty
, empty_i
);
4288 isl_basic_map_free(bmap
);
4293 isl_basic_map_free(bmap
);
4297 /* Given a map "map", compute the lexicograhically minimal
4298 * (or maximal) image element for each domain element in dom.
4299 * Set *empty to those elements in dom that do not have an image element.
4301 * We first compute the lexicographically minimal or maximal element
4302 * in the first basic map. This results in a partial solution "res"
4303 * and a subset "todo" of dom that still need to be handled.
4304 * We then consider each of the remaining maps in "map" and successively
4305 * improve both "res" and "todo".
4307 * Let res^k and todo^k be the results after k steps and let i = k + 1.
4308 * Assume we are computing the lexicographical maximum.
4309 * We first intersect basic map i with a relation that maps elements
4310 * to elements that are lexicographically larger than the image elements
4311 * in res^k and the compute the maximum image element of this intersection.
4312 * The result ("better") corresponds to those image elements in basic map i
4313 * that are better than what we had before. The remainder ("keep") are the
4314 * domain elements for which the image element in res_k was better.
4315 * We also compute the lexicographical maximum of basic map i in todo^k.
4316 * res^i is the result of the operation + better + those elements in
4317 * res^k that we should keep
4318 * todo^i is the remainder of the maximum operation on todo^k.
4320 static __isl_give isl_map
*isl_map_partial_lexopt(
4321 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
4322 __isl_give isl_set
**empty
, int max
)
4325 struct isl_map
*res
;
4326 struct isl_set
*todo
;
4331 if (isl_map_fast_is_empty(map
)) {
4339 res
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[0]),
4340 isl_set_copy(dom
), &todo
, max
);
4342 for (i
= 1; i
< map
->n
; ++i
) {
4344 struct isl_map
*better
;
4345 struct isl_set
*keep
;
4346 struct isl_map
*res_i
;
4347 struct isl_set
*todo_i
;
4348 struct isl_dim
*dim
= isl_map_get_dim(res
);
4350 dim
= isl_dim_range(dim
);
4352 lt
= isl_map_lex_lt(dim
);
4354 lt
= isl_map_lex_gt(dim
);
4355 lt
= isl_map_apply_range(isl_map_copy(res
), lt
);
4356 lt
= isl_map_intersect(lt
,
4357 isl_map_from_basic_map(isl_basic_map_copy(map
->p
[i
])));
4358 better
= isl_map_partial_lexopt(lt
,
4359 isl_map_domain(isl_map_copy(res
)),
4362 res_i
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[i
]),
4363 todo
, &todo_i
, max
);
4365 res
= isl_map_intersect_domain(res
, keep
);
4366 res
= isl_map_union_disjoint(res
, res_i
);
4367 res
= isl_map_union_disjoint(res
, better
);
4388 __isl_give isl_map
*isl_map_partial_lexmax(
4389 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
4390 __isl_give isl_set
**empty
)
4392 return isl_map_partial_lexopt(map
, dom
, empty
, 1);
4395 __isl_give isl_map
*isl_map_partial_lexmin(
4396 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
4397 __isl_give isl_set
**empty
)
4399 return isl_map_partial_lexopt(map
, dom
, empty
, 0);
4402 __isl_give isl_set
*isl_set_partial_lexmin(
4403 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
4404 __isl_give isl_set
**empty
)
4406 return (struct isl_set
*)
4407 isl_map_partial_lexmin((struct isl_map
*)set
,
4411 __isl_give isl_set
*isl_set_partial_lexmax(
4412 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
4413 __isl_give isl_set
**empty
)
4415 return (struct isl_set
*)
4416 isl_map_partial_lexmax((struct isl_map
*)set
,
4420 __isl_give isl_map
*isl_basic_map_lexopt(__isl_take isl_basic_map
*bmap
, int max
)
4422 struct isl_basic_set
*dom
= NULL
;
4423 struct isl_dim
*dom_dim
;
4427 dom_dim
= isl_dim_domain(isl_dim_copy(bmap
->dim
));
4428 dom
= isl_basic_set_universe(dom_dim
);
4429 return isl_basic_map_partial_lexopt(bmap
, dom
, NULL
, max
);
4431 isl_basic_map_free(bmap
);
4435 __isl_give isl_map
*isl_basic_map_lexmin(__isl_take isl_basic_map
*bmap
)
4437 return isl_basic_map_lexopt(bmap
, 0);
4440 __isl_give isl_map
*isl_basic_map_lexmax(__isl_take isl_basic_map
*bmap
)
4442 return isl_basic_map_lexopt(bmap
, 1);
4445 __isl_give isl_set
*isl_basic_set_lexmin(__isl_take isl_basic_set
*bset
)
4447 return (isl_set
*)isl_basic_map_lexmin((isl_basic_map
*)bset
);
4450 __isl_give isl_set
*isl_basic_set_lexmax(__isl_take isl_basic_set
*bset
)
4452 return (isl_set
*)isl_basic_map_lexmax((isl_basic_map
*)bset
);
4455 __isl_give isl_map
*isl_map_lexopt(__isl_take isl_map
*map
, int max
)
4457 struct isl_set
*dom
= NULL
;
4458 struct isl_dim
*dom_dim
;
4462 dom_dim
= isl_dim_domain(isl_dim_copy(map
->dim
));
4463 dom
= isl_set_universe(dom_dim
);
4464 return isl_map_partial_lexopt(map
, dom
, NULL
, max
);
4470 __isl_give isl_map
*isl_map_lexmin(__isl_take isl_map
*map
)
4472 return isl_map_lexopt(map
, 0);
4475 __isl_give isl_map
*isl_map_lexmax(__isl_take isl_map
*map
)
4477 return isl_map_lexopt(map
, 1);
4480 __isl_give isl_set
*isl_set_lexmin(__isl_take isl_set
*set
)
4482 return (isl_set
*)isl_map_lexmin((isl_map
*)set
);
4485 __isl_give isl_set
*isl_set_lexmax(__isl_take isl_set
*set
)
4487 return (isl_set
*)isl_map_lexmax((isl_map
*)set
);
4490 /* Apply a preimage specified by "mat" on the parameters of "bset".
4491 * bset is assumed to have only parameters and divs.
4493 static struct isl_basic_set
*basic_set_parameter_preimage(
4494 struct isl_basic_set
*bset
, struct isl_mat
*mat
)
4501 bset
->dim
= isl_dim_cow(bset
->dim
);
4505 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
4507 isl_assert(bset
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
4509 bset
->dim
->nparam
= 0;
4510 bset
->dim
->n_out
= nparam
;
4511 bset
= isl_basic_set_preimage(bset
, mat
);
4513 bset
->dim
->nparam
= bset
->dim
->n_out
;
4514 bset
->dim
->n_out
= 0;
4519 isl_basic_set_free(bset
);
4523 /* Apply a preimage specified by "mat" on the parameters of "set".
4524 * set is assumed to have only parameters and divs.
4526 static struct isl_set
*set_parameter_preimage(
4527 struct isl_set
*set
, struct isl_mat
*mat
)
4529 struct isl_dim
*dim
= NULL
;
4535 dim
= isl_dim_copy(set
->dim
);
4536 dim
= isl_dim_cow(dim
);
4540 nparam
= isl_set_dim(set
, isl_dim_param
);
4542 isl_assert(set
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
4545 dim
->n_out
= nparam
;
4546 isl_set_reset_dim(set
, dim
);
4547 set
= isl_set_preimage(set
, mat
);
4550 dim
= isl_dim_copy(set
->dim
);
4551 dim
= isl_dim_cow(dim
);
4554 dim
->nparam
= dim
->n_out
;
4556 isl_set_reset_dim(set
, dim
);
4566 /* Intersect the basic set "bset" with the affine space specified by the
4567 * equalities in "eq".
4569 static struct isl_basic_set
*basic_set_append_equalities(
4570 struct isl_basic_set
*bset
, struct isl_mat
*eq
)
4578 bset
= isl_basic_set_extend_dim(bset
, isl_dim_copy(bset
->dim
), 0,
4583 len
= 1 + isl_dim_total(bset
->dim
) + bset
->extra
;
4584 for (i
= 0; i
< eq
->n_row
; ++i
) {
4585 k
= isl_basic_set_alloc_equality(bset
);
4588 isl_seq_cpy(bset
->eq
[k
], eq
->row
[i
], eq
->n_col
);
4589 isl_seq_clr(bset
->eq
[k
] + eq
->n_col
, len
- eq
->n_col
);
4593 bset
= isl_basic_set_gauss(bset
, NULL
);
4594 bset
= isl_basic_set_finalize(bset
);
4599 isl_basic_set_free(bset
);
4603 /* Intersect the set "set" with the affine space specified by the
4604 * equalities in "eq".
4606 static struct isl_set
*set_append_equalities(struct isl_set
*set
,
4614 for (i
= 0; i
< set
->n
; ++i
) {
4615 set
->p
[i
] = basic_set_append_equalities(set
->p
[i
],
4628 /* Project the given basic set onto its parameter domain, possibly introducing
4629 * new, explicit, existential variables in the constraints.
4630 * The input has parameters and (possibly implicit) existential variables.
4631 * The output has the same parameters, but only
4632 * explicit existentially quantified variables.
4634 * The actual projection is performed by pip, but pip doesn't seem
4635 * to like equalities very much, so we first remove the equalities
4636 * among the parameters by performing a variable compression on
4637 * the parameters. Afterward, an inverse transformation is performed
4638 * and the equalities among the parameters are inserted back in.
4640 static struct isl_set
*parameter_compute_divs(struct isl_basic_set
*bset
)
4644 struct isl_mat
*T
, *T2
;
4645 struct isl_set
*set
;
4646 unsigned nparam
, n_div
;
4648 bset
= isl_basic_set_cow(bset
);
4652 if (bset
->n_eq
== 0)
4653 return isl_basic_set_lexmin(bset
);
4655 isl_basic_set_gauss(bset
, NULL
);
4657 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
4658 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
4660 for (i
= 0, j
= n_div
- 1; i
< bset
->n_eq
&& j
>= 0; --j
) {
4661 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ j
]))
4664 if (i
== bset
->n_eq
)
4665 return isl_basic_set_lexmin(bset
);
4667 eq
= isl_mat_sub_alloc(bset
->ctx
, bset
->eq
, i
, bset
->n_eq
- i
,
4669 eq
= isl_mat_cow(eq
);
4670 T
= isl_mat_variable_compression(isl_mat_copy(eq
), &T2
);
4671 if (T
&& T
->n_col
== 0) {
4675 bset
= isl_basic_set_set_to_empty(bset
);
4676 return isl_set_from_basic_set(bset
);
4678 bset
= basic_set_parameter_preimage(bset
, T
);
4680 set
= isl_basic_set_lexmin(bset
);
4681 set
= set_parameter_preimage(set
, T2
);
4682 set
= set_append_equalities(set
, eq
);
4686 /* Compute an explicit representation for all the existentially
4687 * quantified variables.
4688 * The input and output dimensions are first turned into parameters.
4689 * compute_divs then returns a map with the same parameters and
4690 * no input or output dimensions and the dimension specification
4691 * is reset to that of the input.
4693 static struct isl_map
*compute_divs(struct isl_basic_map
*bmap
)
4695 struct isl_basic_set
*bset
;
4696 struct isl_set
*set
;
4697 struct isl_map
*map
;
4698 struct isl_dim
*dim
, *orig_dim
= NULL
;
4703 bmap
= isl_basic_map_cow(bmap
);
4707 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4708 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4709 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4710 dim
= isl_dim_set_alloc(bmap
->ctx
, nparam
+ n_in
+ n_out
, 0);
4714 orig_dim
= bmap
->dim
;
4716 bset
= (struct isl_basic_set
*)bmap
;
4718 set
= parameter_compute_divs(bset
);
4719 map
= (struct isl_map
*)set
;
4720 map
= isl_map_reset_dim(map
, orig_dim
);
4724 isl_basic_map_free(bmap
);
4728 static int basic_map_divs_known(__isl_keep isl_basic_map
*bmap
)
4736 off
= isl_dim_total(bmap
->dim
);
4737 for (i
= 0; i
< bmap
->n_div
; ++i
) {
4738 if (isl_int_is_zero(bmap
->div
[i
][0]))
4740 isl_assert(bmap
->ctx
, isl_int_is_zero(bmap
->div
[i
][1+1+off
+i
]),
4746 static int map_divs_known(__isl_keep isl_map
*map
)
4753 for (i
= 0; i
< map
->n
; ++i
) {
4754 int known
= basic_map_divs_known(map
->p
[i
]);
4762 /* If bmap contains any unknown divs, then compute explicit
4763 * expressions for them. However, this computation may be
4764 * quite expensive, so first try to remove divs that aren't
4767 struct isl_map
*isl_basic_map_compute_divs(struct isl_basic_map
*bmap
)
4771 struct isl_map
*map
;
4773 known
= basic_map_divs_known(bmap
);
4777 return isl_map_from_basic_map(bmap
);
4779 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
4781 known
= basic_map_divs_known(bmap
);
4785 return isl_map_from_basic_map(bmap
);
4787 map
= compute_divs(bmap
);
4790 isl_basic_map_free(bmap
);
4794 struct isl_map
*isl_map_compute_divs(struct isl_map
*map
)
4798 struct isl_map
*res
;
4805 known
= map_divs_known(map
);
4813 res
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[0]));
4814 for (i
= 1 ; i
< map
->n
; ++i
) {
4816 r2
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[i
]));
4817 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
4818 res
= isl_map_union_disjoint(res
, r2
);
4820 res
= isl_map_union(res
, r2
);
4827 struct isl_set
*isl_basic_set_compute_divs(struct isl_basic_set
*bset
)
4829 return (struct isl_set
*)
4830 isl_basic_map_compute_divs((struct isl_basic_map
*)bset
);
4833 struct isl_set
*isl_set_compute_divs(struct isl_set
*set
)
4835 return (struct isl_set
*)
4836 isl_map_compute_divs((struct isl_map
*)set
);
4839 struct isl_set
*isl_map_domain(struct isl_map
*map
)
4842 struct isl_set
*set
;
4847 map
= isl_map_cow(map
);
4851 set
= (struct isl_set
*)map
;
4852 set
->dim
= isl_dim_domain(set
->dim
);
4855 for (i
= 0; i
< map
->n
; ++i
) {
4856 set
->p
[i
] = isl_basic_map_domain(map
->p
[i
]);
4860 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
4861 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
4868 struct isl_map
*isl_map_union_disjoint(
4869 struct isl_map
*map1
, struct isl_map
*map2
)
4873 struct isl_map
*map
= NULL
;
4887 isl_assert(map1
->ctx
, isl_dim_equal(map1
->dim
, map2
->dim
), goto error
);
4889 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
4890 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
4891 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
4893 map
= isl_map_alloc_dim(isl_dim_copy(map1
->dim
),
4894 map1
->n
+ map2
->n
, flags
);
4897 for (i
= 0; i
< map1
->n
; ++i
) {
4898 map
= isl_map_add_basic_map(map
,
4899 isl_basic_map_copy(map1
->p
[i
]));
4903 for (i
= 0; i
< map2
->n
; ++i
) {
4904 map
= isl_map_add_basic_map(map
,
4905 isl_basic_map_copy(map2
->p
[i
]));
4919 struct isl_map
*isl_map_union(struct isl_map
*map1
, struct isl_map
*map2
)
4921 map1
= isl_map_union_disjoint(map1
, map2
);
4925 ISL_F_CLR(map1
, ISL_MAP_DISJOINT
);
4929 struct isl_set
*isl_set_union_disjoint(
4930 struct isl_set
*set1
, struct isl_set
*set2
)
4932 return (struct isl_set
*)
4933 isl_map_union_disjoint(
4934 (struct isl_map
*)set1
, (struct isl_map
*)set2
);
4937 struct isl_set
*isl_set_union(struct isl_set
*set1
, struct isl_set
*set2
)
4939 return (struct isl_set
*)
4940 isl_map_union((struct isl_map
*)set1
, (struct isl_map
*)set2
);
4943 struct isl_map
*isl_map_intersect_range(
4944 struct isl_map
*map
, struct isl_set
*set
)
4947 struct isl_map
*result
;
4953 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
) &&
4954 ISL_F_ISSET(set
, ISL_MAP_DISJOINT
))
4955 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
4957 result
= isl_map_alloc_dim(isl_dim_copy(map
->dim
),
4958 map
->n
* set
->n
, flags
);
4961 for (i
= 0; i
< map
->n
; ++i
)
4962 for (j
= 0; j
< set
->n
; ++j
) {
4963 result
= isl_map_add_basic_map(result
,
4964 isl_basic_map_intersect_range(
4965 isl_basic_map_copy(map
->p
[i
]),
4966 isl_basic_set_copy(set
->p
[j
])));
4979 struct isl_map
*isl_map_intersect_domain(
4980 struct isl_map
*map
, struct isl_set
*set
)
4982 return isl_map_reverse(
4983 isl_map_intersect_range(isl_map_reverse(map
), set
));
4986 struct isl_map
*isl_map_apply_domain(
4987 struct isl_map
*map1
, struct isl_map
*map2
)
4991 map1
= isl_map_reverse(map1
);
4992 map1
= isl_map_apply_range(map1
, map2
);
4993 return isl_map_reverse(map1
);
5000 struct isl_map
*isl_map_apply_range(
5001 struct isl_map
*map1
, struct isl_map
*map2
)
5003 struct isl_dim
*dim_result
;
5004 struct isl_map
*result
;
5010 dim_result
= isl_dim_join(isl_dim_copy(map1
->dim
),
5011 isl_dim_copy(map2
->dim
));
5013 result
= isl_map_alloc_dim(dim_result
, map1
->n
* map2
->n
, 0);
5016 for (i
= 0; i
< map1
->n
; ++i
)
5017 for (j
= 0; j
< map2
->n
; ++j
) {
5018 result
= isl_map_add_basic_map(result
,
5019 isl_basic_map_apply_range(
5020 isl_basic_map_copy(map1
->p
[i
]),
5021 isl_basic_map_copy(map2
->p
[j
])));
5027 if (result
&& result
->n
<= 1)
5028 ISL_F_SET(result
, ISL_MAP_DISJOINT
);
5037 * returns range - domain
5039 struct isl_basic_set
*isl_basic_map_deltas(struct isl_basic_map
*bmap
)
5041 isl_dim
*dims
, *target_dim
;
5042 struct isl_basic_set
*bset
;
5049 isl_assert(bmap
->ctx
, isl_dim_tuple_match(bmap
->dim
, isl_dim_in
,
5050 bmap
->dim
, isl_dim_out
),
5052 target_dim
= isl_dim_domain(isl_basic_map_get_dim(bmap
));
5053 dim
= isl_basic_map_n_in(bmap
);
5054 nparam
= isl_basic_map_n_param(bmap
);
5055 bset
= isl_basic_set_from_basic_map(bmap
);
5056 bset
= isl_basic_set_cow(bset
);
5057 dims
= isl_basic_set_get_dim(bset
);
5058 dims
= isl_dim_add(dims
, isl_dim_set
, dim
);
5059 bset
= isl_basic_set_extend_dim(bset
, dims
, 0, dim
, 0);
5060 bset
= isl_basic_set_swap_vars(bset
, 2*dim
);
5061 for (i
= 0; i
< dim
; ++i
) {
5062 int j
= isl_basic_map_alloc_equality(
5063 (struct isl_basic_map
*)bset
);
5066 isl_seq_clr(bset
->eq
[j
], 1 + isl_basic_set_total_dim(bset
));
5067 isl_int_set_si(bset
->eq
[j
][1+nparam
+i
], 1);
5068 isl_int_set_si(bset
->eq
[j
][1+nparam
+dim
+i
], 1);
5069 isl_int_set_si(bset
->eq
[j
][1+nparam
+2*dim
+i
], -1);
5071 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, dim
, 2*dim
);
5072 bset
= isl_basic_set_reset_dim(bset
, target_dim
);
5075 isl_basic_map_free(bmap
);
5080 * returns range - domain
5082 struct isl_set
*isl_map_deltas(struct isl_map
*map
)
5086 struct isl_set
*result
;
5091 isl_assert(map
->ctx
, isl_dim_tuple_match(map
->dim
, isl_dim_in
,
5092 map
->dim
, isl_dim_out
),
5094 dim
= isl_map_get_dim(map
);
5095 dim
= isl_dim_domain(dim
);
5096 result
= isl_set_alloc_dim(dim
, map
->n
, map
->flags
);
5099 for (i
= 0; i
< map
->n
; ++i
)
5100 result
= isl_set_add_basic_set(result
,
5101 isl_basic_map_deltas(isl_basic_map_copy(map
->p
[i
])));
5109 static struct isl_basic_map
*basic_map_identity(struct isl_dim
*dims
)
5111 struct isl_basic_map
*bmap
;
5119 nparam
= dims
->nparam
;
5121 bmap
= isl_basic_map_alloc_dim(dims
, 0, dim
, 0);
5125 for (i
= 0; i
< dim
; ++i
) {
5126 int j
= isl_basic_map_alloc_equality(bmap
);
5129 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
5130 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
5131 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], -1);
5133 return isl_basic_map_finalize(bmap
);
5135 isl_basic_map_free(bmap
);
5139 struct isl_basic_map
*isl_basic_map_identity(struct isl_dim
*set_dim
)
5141 struct isl_dim
*dim
= isl_dim_map(set_dim
);
5144 return basic_map_identity(dim
);
5147 struct isl_basic_map
*isl_basic_map_identity_like(struct isl_basic_map
*model
)
5149 if (!model
|| !model
->dim
)
5151 isl_assert(model
->ctx
,
5152 model
->dim
->n_in
== model
->dim
->n_out
, return NULL
);
5153 return basic_map_identity(isl_dim_copy(model
->dim
));
5156 static struct isl_map
*map_identity(struct isl_dim
*dim
)
5158 struct isl_map
*map
= isl_map_alloc_dim(dim
, 1, ISL_MAP_DISJOINT
);
5159 return isl_map_add_basic_map(map
, basic_map_identity(isl_dim_copy(dim
)));
5162 struct isl_map
*isl_map_identity(struct isl_dim
*set_dim
)
5164 struct isl_dim
*dim
= isl_dim_map(set_dim
);
5167 return map_identity(dim
);
5170 struct isl_map
*isl_map_identity_like(struct isl_map
*model
)
5172 if (!model
|| !model
->dim
)
5174 isl_assert(model
->ctx
,
5175 model
->dim
->n_in
== model
->dim
->n_out
, return NULL
);
5176 return map_identity(isl_dim_copy(model
->dim
));
5179 struct isl_map
*isl_map_identity_like_basic_map(struct isl_basic_map
*model
)
5181 if (!model
|| !model
->dim
)
5183 isl_assert(model
->ctx
,
5184 model
->dim
->n_in
== model
->dim
->n_out
, return NULL
);
5185 return map_identity(isl_dim_copy(model
->dim
));
5188 /* Construct a basic set with all set dimensions having only non-negative
5191 struct isl_basic_set
*isl_basic_set_positive_orthant(struct isl_dim
*dims
)
5196 struct isl_basic_set
*bset
;
5200 nparam
= dims
->nparam
;
5202 bset
= isl_basic_set_alloc_dim(dims
, 0, 0, dim
);
5205 for (i
= 0; i
< dim
; ++i
) {
5206 int k
= isl_basic_set_alloc_inequality(bset
);
5209 isl_seq_clr(bset
->ineq
[k
], 1 + isl_basic_set_total_dim(bset
));
5210 isl_int_set_si(bset
->ineq
[k
][1 + nparam
+ i
], 1);
5214 isl_basic_set_free(bset
);
5218 __isl_give isl_set
*isl_set_split_dims(__isl_take isl_set
*set
,
5219 enum isl_dim_type type
, unsigned first
, unsigned n
)
5222 isl_basic_set
*nonneg
= NULL
;
5223 isl_basic_set
*neg
= NULL
;
5230 isl_assert(set
->ctx
, first
+ n
<= isl_set_dim(set
, type
), goto error
);
5232 for (i
= 0; i
< n
; ++i
) {
5236 nonneg
= isl_basic_set_alloc_dim(isl_set_get_dim(set
), 0, 0, 1);
5237 k
= isl_basic_set_alloc_inequality(nonneg
);
5240 isl_seq_clr(nonneg
->ineq
[k
], 1 + isl_basic_set_total_dim(nonneg
));
5241 isl_int_set_si(nonneg
->ineq
[k
][pos(set
->dim
, type
) + first
+ i
], 1);
5243 neg
= isl_basic_set_alloc_dim(isl_set_get_dim(set
), 0, 0, 1);
5244 k
= isl_basic_set_alloc_inequality(neg
);
5247 isl_seq_clr(neg
->ineq
[k
], 1 + isl_basic_set_total_dim(neg
));
5248 isl_int_set_si(neg
->ineq
[k
][0], -1);
5249 isl_int_set_si(neg
->ineq
[k
][pos(set
->dim
, type
) + first
+ i
], -1);
5251 set
= isl_set_intersect(set
, isl_basic_set_union(nonneg
, neg
));
5256 isl_basic_set_free(nonneg
);
5257 isl_basic_set_free(neg
);
5262 int isl_set_is_equal(struct isl_set
*set1
, struct isl_set
*set2
)
5264 return isl_map_is_equal((struct isl_map
*)set1
, (struct isl_map
*)set2
);
5267 int isl_basic_map_is_subset(
5268 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
5271 struct isl_map
*map1
;
5272 struct isl_map
*map2
;
5274 if (!bmap1
|| !bmap2
)
5277 map1
= isl_map_from_basic_map(isl_basic_map_copy(bmap1
));
5278 map2
= isl_map_from_basic_map(isl_basic_map_copy(bmap2
));
5280 is_subset
= isl_map_is_subset(map1
, map2
);
5288 int isl_basic_map_is_equal(
5289 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
5293 if (!bmap1
|| !bmap2
)
5295 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
5298 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
5302 int isl_basic_set_is_equal(
5303 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
5305 return isl_basic_map_is_equal(
5306 (struct isl_basic_map
*)bset1
, (struct isl_basic_map
*)bset2
);
5309 int isl_map_is_empty(struct isl_map
*map
)
5316 for (i
= 0; i
< map
->n
; ++i
) {
5317 is_empty
= isl_basic_map_is_empty(map
->p
[i
]);
5326 int isl_map_fast_is_empty(struct isl_map
*map
)
5328 return map
? map
->n
== 0 : -1;
5331 int isl_set_fast_is_empty(struct isl_set
*set
)
5333 return set
? set
->n
== 0 : -1;
5336 int isl_set_is_empty(struct isl_set
*set
)
5338 return isl_map_is_empty((struct isl_map
*)set
);
5341 int isl_map_has_equal_dim(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
5346 return isl_dim_equal(map1
->dim
, map2
->dim
);
5349 int isl_set_has_equal_dim(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
5354 return isl_dim_equal(set1
->dim
, set2
->dim
);
5357 int isl_map_is_equal(struct isl_map
*map1
, struct isl_map
*map2
)
5363 is_subset
= isl_map_is_subset(map1
, map2
);
5366 is_subset
= isl_map_is_subset(map2
, map1
);
5370 int isl_basic_map_is_strict_subset(
5371 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
5375 if (!bmap1
|| !bmap2
)
5377 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
5380 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
5381 if (is_subset
== -1)
5386 int isl_map_is_strict_subset(struct isl_map
*map1
, struct isl_map
*map2
)
5392 is_subset
= isl_map_is_subset(map1
, map2
);
5395 is_subset
= isl_map_is_subset(map2
, map1
);
5396 if (is_subset
== -1)
5401 int isl_set_is_strict_subset(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
5403 return isl_map_is_strict_subset((isl_map
*)set1
, (isl_map
*)set2
);
5406 int isl_basic_map_is_universe(struct isl_basic_map
*bmap
)
5410 return bmap
->n_eq
== 0 && bmap
->n_ineq
== 0;
5413 int isl_basic_set_is_universe(struct isl_basic_set
*bset
)
5417 return bset
->n_eq
== 0 && bset
->n_ineq
== 0;
5420 int isl_map_fast_is_universe(__isl_keep isl_map
*map
)
5427 for (i
= 0; i
< map
->n
; ++i
) {
5428 int r
= isl_basic_map_is_universe(map
->p
[i
]);
5436 int isl_set_fast_is_universe(__isl_keep isl_set
*set
)
5438 return isl_map_fast_is_universe((isl_map
*) set
);
5441 int isl_basic_map_is_empty(struct isl_basic_map
*bmap
)
5443 struct isl_basic_set
*bset
= NULL
;
5444 struct isl_vec
*sample
= NULL
;
5451 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
5454 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
5455 struct isl_basic_map
*copy
= isl_basic_map_copy(bmap
);
5456 copy
= isl_basic_map_remove_redundancies(copy
);
5457 empty
= ISL_F_ISSET(copy
, ISL_BASIC_MAP_EMPTY
);
5458 isl_basic_map_free(copy
);
5462 total
= 1 + isl_basic_map_total_dim(bmap
);
5463 if (bmap
->sample
&& bmap
->sample
->size
== total
) {
5464 int contains
= isl_basic_map_contains(bmap
, bmap
->sample
);
5470 isl_vec_free(bmap
->sample
);
5471 bmap
->sample
= NULL
;
5472 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
5475 sample
= isl_basic_set_sample_vec(bset
);
5478 empty
= sample
->size
== 0;
5479 isl_vec_free(bmap
->sample
);
5480 bmap
->sample
= sample
;
5482 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
5487 int isl_basic_map_fast_is_empty(struct isl_basic_map
*bmap
)
5491 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
);
5494 int isl_basic_set_fast_is_empty(struct isl_basic_set
*bset
)
5498 return ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
);
5501 int isl_basic_set_is_empty(struct isl_basic_set
*bset
)
5503 return isl_basic_map_is_empty((struct isl_basic_map
*)bset
);
5506 struct isl_map
*isl_basic_map_union(
5507 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
5509 struct isl_map
*map
;
5510 if (!bmap1
|| !bmap2
)
5513 isl_assert(bmap1
->ctx
, isl_dim_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
5515 map
= isl_map_alloc_dim(isl_dim_copy(bmap1
->dim
), 2, 0);
5518 map
= isl_map_add_basic_map(map
, bmap1
);
5519 map
= isl_map_add_basic_map(map
, bmap2
);
5522 isl_basic_map_free(bmap1
);
5523 isl_basic_map_free(bmap2
);
5527 struct isl_set
*isl_basic_set_union(
5528 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
5530 return (struct isl_set
*)isl_basic_map_union(
5531 (struct isl_basic_map
*)bset1
,
5532 (struct isl_basic_map
*)bset2
);
5535 /* Order divs such that any div only depends on previous divs */
5536 struct isl_basic_map
*isl_basic_map_order_divs(struct isl_basic_map
*bmap
)
5544 off
= isl_dim_total(bmap
->dim
);
5546 for (i
= 0; i
< bmap
->n_div
; ++i
) {
5548 if (isl_int_is_zero(bmap
->div
[i
][0]))
5550 pos
= isl_seq_first_non_zero(bmap
->div
[i
]+1+1+off
+i
,
5554 isl_basic_map_swap_div(bmap
, i
, i
+ pos
);
5560 struct isl_basic_set
*isl_basic_set_order_divs(struct isl_basic_set
*bset
)
5562 return (struct isl_basic_set
*)
5563 isl_basic_map_order_divs((struct isl_basic_map
*)bset
);
5566 __isl_give isl_map
*isl_map_order_divs(__isl_take isl_map
*map
)
5573 for (i
= 0; i
< map
->n
; ++i
) {
5574 map
->p
[i
] = isl_basic_map_order_divs(map
->p
[i
]);
5585 /* Look for a div in dst that corresponds to the div "div" in src.
5586 * The divs before "div" in src and dst are assumed to be the same.
5588 * Returns -1 if no corresponding div was found and the position
5589 * of the corresponding div in dst otherwise.
5591 static int find_div(struct isl_basic_map
*dst
,
5592 struct isl_basic_map
*src
, unsigned div
)
5596 unsigned total
= isl_dim_total(src
->dim
);
5598 isl_assert(dst
->ctx
, div
<= dst
->n_div
, return -1);
5599 for (i
= div
; i
< dst
->n_div
; ++i
)
5600 if (isl_seq_eq(dst
->div
[i
], src
->div
[div
], 1+1+total
+div
) &&
5601 isl_seq_first_non_zero(dst
->div
[i
]+1+1+total
+div
,
5602 dst
->n_div
- div
) == -1)
5607 struct isl_basic_map
*isl_basic_map_align_divs(
5608 struct isl_basic_map
*dst
, struct isl_basic_map
*src
)
5611 unsigned total
= isl_dim_total(src
->dim
);
5616 if (src
->n_div
== 0)
5619 for (i
= 0; i
< src
->n_div
; ++i
)
5620 isl_assert(src
->ctx
, !isl_int_is_zero(src
->div
[i
][0]), goto error
);
5622 src
= isl_basic_map_order_divs(src
);
5623 dst
= isl_basic_map_cow(dst
);
5624 dst
= isl_basic_map_extend_dim(dst
, isl_dim_copy(dst
->dim
),
5625 src
->n_div
, 0, 2 * src
->n_div
);
5628 for (i
= 0; i
< src
->n_div
; ++i
) {
5629 int j
= find_div(dst
, src
, i
);
5631 j
= isl_basic_map_alloc_div(dst
);
5634 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
+i
);
5635 isl_seq_clr(dst
->div
[j
]+1+1+total
+i
, dst
->n_div
- i
);
5636 if (isl_basic_map_add_div_constraints(dst
, j
) < 0)
5640 isl_basic_map_swap_div(dst
, i
, j
);
5644 isl_basic_map_free(dst
);
5648 struct isl_basic_set
*isl_basic_set_align_divs(
5649 struct isl_basic_set
*dst
, struct isl_basic_set
*src
)
5651 return (struct isl_basic_set
*)isl_basic_map_align_divs(
5652 (struct isl_basic_map
*)dst
, (struct isl_basic_map
*)src
);
5655 struct isl_map
*isl_map_align_divs(struct isl_map
*map
)
5663 map
= isl_map_compute_divs(map
);
5664 map
= isl_map_cow(map
);
5668 for (i
= 1; i
< map
->n
; ++i
)
5669 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], map
->p
[i
]);
5670 for (i
= 1; i
< map
->n
; ++i
)
5671 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], map
->p
[0]);
5673 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5677 struct isl_set
*isl_set_align_divs(struct isl_set
*set
)
5679 return (struct isl_set
*)isl_map_align_divs((struct isl_map
*)set
);
5682 struct isl_set
*isl_set_apply(struct isl_set
*set
, struct isl_map
*map
)
5686 isl_assert(set
->ctx
, isl_map_compatible_domain(map
, set
), goto error
);
5687 map
= isl_map_intersect_domain(map
, set
);
5688 set
= isl_map_range(map
);
5696 /* There is no need to cow as removing empty parts doesn't change
5697 * the meaning of the set.
5699 struct isl_map
*isl_map_remove_empty_parts(struct isl_map
*map
)
5706 for (i
= map
->n
-1; i
>= 0; --i
) {
5707 if (!ISL_F_ISSET(map
->p
[i
], ISL_BASIC_MAP_EMPTY
))
5709 isl_basic_map_free(map
->p
[i
]);
5710 if (i
!= map
->n
-1) {
5711 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5712 map
->p
[i
] = map
->p
[map
->n
-1];
5720 struct isl_set
*isl_set_remove_empty_parts(struct isl_set
*set
)
5722 return (struct isl_set
*)
5723 isl_map_remove_empty_parts((struct isl_map
*)set
);
5726 struct isl_basic_map
*isl_map_copy_basic_map(struct isl_map
*map
)
5728 struct isl_basic_map
*bmap
;
5729 if (!map
|| map
->n
== 0)
5731 bmap
= map
->p
[map
->n
-1];
5732 isl_assert(map
->ctx
, ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
), return NULL
);
5733 return isl_basic_map_copy(bmap
);
5736 struct isl_basic_set
*isl_set_copy_basic_set(struct isl_set
*set
)
5738 return (struct isl_basic_set
*)
5739 isl_map_copy_basic_map((struct isl_map
*)set
);
5742 __isl_give isl_map
*isl_map_drop_basic_map(__isl_take isl_map
*map
,
5743 __isl_keep isl_basic_map
*bmap
)
5749 for (i
= map
->n
-1; i
>= 0; --i
) {
5750 if (map
->p
[i
] != bmap
)
5752 map
= isl_map_cow(map
);
5755 isl_basic_map_free(map
->p
[i
]);
5756 if (i
!= map
->n
-1) {
5757 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
5758 map
->p
[i
] = map
->p
[map
->n
-1];
5769 struct isl_set
*isl_set_drop_basic_set(struct isl_set
*set
,
5770 struct isl_basic_set
*bset
)
5772 return (struct isl_set
*)isl_map_drop_basic_map((struct isl_map
*)set
,
5773 (struct isl_basic_map
*)bset
);
5776 /* Given two basic sets bset1 and bset2, compute the maximal difference
5777 * between the values of dimension pos in bset1 and those in bset2
5778 * for any common value of the parameters and dimensions preceding pos.
5780 static enum isl_lp_result
basic_set_maximal_difference_at(
5781 __isl_keep isl_basic_set
*bset1
, __isl_keep isl_basic_set
*bset2
,
5782 int pos
, isl_int
*opt
)
5784 struct isl_dim
*dims
;
5785 struct isl_basic_map
*bmap1
= NULL
;
5786 struct isl_basic_map
*bmap2
= NULL
;
5787 struct isl_ctx
*ctx
;
5788 struct isl_vec
*obj
;
5791 unsigned dim1
, dim2
;
5792 enum isl_lp_result res
;
5794 if (!bset1
|| !bset2
)
5795 return isl_lp_error
;
5797 nparam
= isl_basic_set_n_param(bset1
);
5798 dim1
= isl_basic_set_n_dim(bset1
);
5799 dim2
= isl_basic_set_n_dim(bset2
);
5800 dims
= isl_dim_alloc(bset1
->ctx
, nparam
, pos
, dim1
- pos
);
5801 bmap1
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset1
), dims
);
5802 dims
= isl_dim_alloc(bset2
->ctx
, nparam
, pos
, dim2
- pos
);
5803 bmap2
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset2
), dims
);
5804 if (!bmap1
|| !bmap2
)
5806 bmap1
= isl_basic_map_cow(bmap1
);
5807 bmap1
= isl_basic_map_extend(bmap1
, nparam
,
5808 pos
, (dim1
- pos
) + (dim2
- pos
),
5809 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
5810 bmap1
= add_constraints(bmap1
, bmap2
, 0, dim1
- pos
);
5813 total
= isl_basic_map_total_dim(bmap1
);
5815 obj
= isl_vec_alloc(ctx
, 1 + total
);
5816 isl_seq_clr(obj
->block
.data
, 1 + total
);
5817 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
], 1);
5818 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
+(dim1
-pos
)], -1);
5821 res
= isl_basic_map_solve_lp(bmap1
, 1, obj
->block
.data
, ctx
->one
,
5823 isl_basic_map_free(bmap1
);
5827 isl_basic_map_free(bmap1
);
5828 isl_basic_map_free(bmap2
);
5829 return isl_lp_error
;
5832 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
5833 * for any common value of the parameters and dimensions preceding pos
5834 * in both basic sets, the values of dimension pos in bset1 are
5835 * smaller or larger than those in bset2.
5838 * 1 if bset1 follows bset2
5839 * -1 if bset1 precedes bset2
5840 * 0 if bset1 and bset2 are incomparable
5841 * -2 if some error occurred.
5843 int isl_basic_set_compare_at(struct isl_basic_set
*bset1
,
5844 struct isl_basic_set
*bset2
, int pos
)
5847 enum isl_lp_result res
;
5852 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
5854 if (res
== isl_lp_empty
)
5856 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
5857 res
== isl_lp_unbounded
)
5859 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
5868 /* Given two basic sets bset1 and bset2, check whether
5869 * for any common value of the parameters and dimensions preceding pos
5870 * there is a value of dimension pos in bset1 that is larger
5871 * than a value of the same dimension in bset2.
5874 * 1 if there exists such a pair
5875 * 0 if there is no such pair, but there is a pair of equal values
5877 * -2 if some error occurred.
5879 int isl_basic_set_follows_at(__isl_keep isl_basic_set
*bset1
,
5880 __isl_keep isl_basic_set
*bset2
, int pos
)
5883 enum isl_lp_result res
;
5888 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
5890 if (res
== isl_lp_empty
)
5892 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
5893 res
== isl_lp_unbounded
)
5895 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
5897 else if (res
== isl_lp_ok
)
5906 /* Given two sets set1 and set2, check whether
5907 * for any common value of the parameters and dimensions preceding pos
5908 * there is a value of dimension pos in set1 that is larger
5909 * than a value of the same dimension in set2.
5912 * 1 if there exists such a pair
5913 * 0 if there is no such pair, but there is a pair of equal values
5915 * -2 if some error occurred.
5917 int isl_set_follows_at(__isl_keep isl_set
*set1
,
5918 __isl_keep isl_set
*set2
, int pos
)
5926 for (i
= 0; i
< set1
->n
; ++i
)
5927 for (j
= 0; j
< set2
->n
; ++j
) {
5929 f
= isl_basic_set_follows_at(set1
->p
[i
], set2
->p
[j
], pos
);
5930 if (f
== 1 || f
== -2)
5939 static int isl_basic_map_fast_has_fixed_var(struct isl_basic_map
*bmap
,
5940 unsigned pos
, isl_int
*val
)
5948 total
= isl_basic_map_total_dim(bmap
);
5949 for (i
= 0, d
= total
-1; i
< bmap
->n_eq
&& d
+1 > pos
; ++i
) {
5950 for (; d
+1 > pos
; --d
)
5951 if (!isl_int_is_zero(bmap
->eq
[i
][1+d
]))
5955 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1, d
) != -1)
5957 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1+d
+1, total
-d
-1) != -1)
5959 if (!isl_int_is_one(bmap
->eq
[i
][1+d
]))
5962 isl_int_neg(*val
, bmap
->eq
[i
][0]);
5968 static int isl_map_fast_has_fixed_var(struct isl_map
*map
,
5969 unsigned pos
, isl_int
*val
)
5981 return isl_basic_map_fast_has_fixed_var(map
->p
[0], pos
, val
);
5984 fixed
= isl_basic_map_fast_has_fixed_var(map
->p
[0], pos
, &v
);
5985 for (i
= 1; fixed
== 1 && i
< map
->n
; ++i
) {
5986 fixed
= isl_basic_map_fast_has_fixed_var(map
->p
[i
], pos
, &tmp
);
5987 if (fixed
== 1 && isl_int_ne(tmp
, v
))
5991 isl_int_set(*val
, v
);
5997 static int isl_basic_set_fast_has_fixed_var(struct isl_basic_set
*bset
,
5998 unsigned pos
, isl_int
*val
)
6000 return isl_basic_map_fast_has_fixed_var((struct isl_basic_map
*)bset
,
6004 static int isl_set_fast_has_fixed_var(struct isl_set
*set
, unsigned pos
,
6007 return isl_map_fast_has_fixed_var((struct isl_map
*)set
, pos
, val
);
6010 int isl_basic_map_fast_is_fixed(struct isl_basic_map
*bmap
,
6011 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
6013 if (pos
>= isl_basic_map_dim(bmap
, type
))
6015 return isl_basic_map_fast_has_fixed_var(bmap
,
6016 isl_basic_map_offset(bmap
, type
) - 1 + pos
, val
);
6019 int isl_map_fast_is_fixed(struct isl_map
*map
,
6020 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
6022 if (pos
>= isl_map_dim(map
, type
))
6024 return isl_map_fast_has_fixed_var(map
,
6025 map_offset(map
, type
) - 1 + pos
, val
);
6028 /* Check if dimension dim has fixed value and if so and if val is not NULL,
6029 * then return this fixed value in *val.
6031 int isl_basic_set_fast_dim_is_fixed(struct isl_basic_set
*bset
, unsigned dim
,
6034 return isl_basic_set_fast_has_fixed_var(bset
,
6035 isl_basic_set_n_param(bset
) + dim
, val
);
6038 /* Check if dimension dim has fixed value and if so and if val is not NULL,
6039 * then return this fixed value in *val.
6041 int isl_set_fast_dim_is_fixed(struct isl_set
*set
, unsigned dim
, isl_int
*val
)
6043 return isl_set_fast_has_fixed_var(set
, isl_set_n_param(set
) + dim
, val
);
6046 /* Check if input variable in has fixed value and if so and if val is not NULL,
6047 * then return this fixed value in *val.
6049 int isl_map_fast_input_is_fixed(struct isl_map
*map
, unsigned in
, isl_int
*val
)
6051 return isl_map_fast_has_fixed_var(map
, isl_map_n_param(map
) + in
, val
);
6054 /* Check if dimension dim has an (obvious) fixed lower bound and if so
6055 * and if val is not NULL, then return this lower bound in *val.
6057 int isl_basic_set_fast_dim_has_fixed_lower_bound(struct isl_basic_set
*bset
,
6058 unsigned dim
, isl_int
*val
)
6060 int i
, i_eq
= -1, i_ineq
= -1;
6067 total
= isl_basic_set_total_dim(bset
);
6068 nparam
= isl_basic_set_n_param(bset
);
6069 for (i
= 0; i
< bset
->n_eq
; ++i
) {
6070 if (isl_int_is_zero(bset
->eq
[i
][1+nparam
+dim
]))
6076 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
6077 if (!isl_int_is_pos(bset
->ineq
[i
][1+nparam
+dim
]))
6079 if (i_eq
!= -1 || i_ineq
!= -1)
6083 if (i_eq
== -1 && i_ineq
== -1)
6085 c
= i_eq
!= -1 ? bset
->eq
[i_eq
] : bset
->ineq
[i_ineq
];
6086 /* The coefficient should always be one due to normalization. */
6087 if (!isl_int_is_one(c
[1+nparam
+dim
]))
6089 if (isl_seq_first_non_zero(c
+1, nparam
+dim
) != -1)
6091 if (isl_seq_first_non_zero(c
+1+nparam
+dim
+1,
6092 total
- nparam
- dim
- 1) != -1)
6095 isl_int_neg(*val
, c
[0]);
6099 int isl_set_fast_dim_has_fixed_lower_bound(struct isl_set
*set
,
6100 unsigned dim
, isl_int
*val
)
6112 return isl_basic_set_fast_dim_has_fixed_lower_bound(set
->p
[0],
6116 fixed
= isl_basic_set_fast_dim_has_fixed_lower_bound(set
->p
[0],
6118 for (i
= 1; fixed
== 1 && i
< set
->n
; ++i
) {
6119 fixed
= isl_basic_set_fast_dim_has_fixed_lower_bound(set
->p
[i
],
6121 if (fixed
== 1 && isl_int_ne(tmp
, v
))
6125 isl_int_set(*val
, v
);
6136 static int qsort_constraint_cmp(const void *p1
, const void *p2
)
6138 const struct constraint
*c1
= (const struct constraint
*)p1
;
6139 const struct constraint
*c2
= (const struct constraint
*)p2
;
6140 unsigned size
= isl_min(c1
->size
, c2
->size
);
6141 return isl_seq_cmp(c1
->c
, c2
->c
, size
);
6144 static struct isl_basic_map
*isl_basic_map_sort_constraints(
6145 struct isl_basic_map
*bmap
)
6148 struct constraint
*c
;
6153 total
= isl_basic_map_total_dim(bmap
);
6154 c
= isl_alloc_array(bmap
->ctx
, struct constraint
, bmap
->n_ineq
);
6157 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
6159 c
[i
].c
= bmap
->ineq
[i
];
6161 qsort(c
, bmap
->n_ineq
, sizeof(struct constraint
), qsort_constraint_cmp
);
6162 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
6163 bmap
->ineq
[i
] = c
[i
].c
;
6167 isl_basic_map_free(bmap
);
6171 __isl_give isl_basic_set
*isl_basic_set_sort_constraints(
6172 __isl_take isl_basic_set
*bset
)
6174 return (struct isl_basic_set
*)isl_basic_map_sort_constraints(
6175 (struct isl_basic_map
*)bset
);
6178 struct isl_basic_map
*isl_basic_map_normalize(struct isl_basic_map
*bmap
)
6182 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED
))
6184 bmap
= isl_basic_map_remove_redundancies(bmap
);
6185 bmap
= isl_basic_map_sort_constraints(bmap
);
6186 ISL_F_SET(bmap
, ISL_BASIC_MAP_NORMALIZED
);
6190 struct isl_basic_set
*isl_basic_set_normalize(struct isl_basic_set
*bset
)
6192 return (struct isl_basic_set
*)isl_basic_map_normalize(
6193 (struct isl_basic_map
*)bset
);
6196 int isl_basic_map_fast_cmp(const __isl_keep isl_basic_map
*bmap1
,
6197 const __isl_keep isl_basic_map
*bmap2
)
6204 if (isl_basic_map_n_param(bmap1
) != isl_basic_map_n_param(bmap2
))
6205 return isl_basic_map_n_param(bmap1
) - isl_basic_map_n_param(bmap2
);
6206 if (isl_basic_map_n_in(bmap1
) != isl_basic_map_n_in(bmap2
))
6207 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
6208 if (isl_basic_map_n_out(bmap1
) != isl_basic_map_n_out(bmap2
))
6209 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
6210 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
) &&
6211 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
6213 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
))
6215 if (ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
6217 if (bmap1
->n_eq
!= bmap2
->n_eq
)
6218 return bmap1
->n_eq
- bmap2
->n_eq
;
6219 if (bmap1
->n_ineq
!= bmap2
->n_ineq
)
6220 return bmap1
->n_ineq
- bmap2
->n_ineq
;
6221 if (bmap1
->n_div
!= bmap2
->n_div
)
6222 return bmap1
->n_div
- bmap2
->n_div
;
6223 total
= isl_basic_map_total_dim(bmap1
);
6224 for (i
= 0; i
< bmap1
->n_eq
; ++i
) {
6225 cmp
= isl_seq_cmp(bmap1
->eq
[i
], bmap2
->eq
[i
], 1+total
);
6229 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
6230 cmp
= isl_seq_cmp(bmap1
->ineq
[i
], bmap2
->ineq
[i
], 1+total
);
6234 for (i
= 0; i
< bmap1
->n_div
; ++i
) {
6235 cmp
= isl_seq_cmp(bmap1
->div
[i
], bmap2
->div
[i
], 1+1+total
);
6242 int isl_basic_map_fast_is_equal(__isl_keep isl_basic_map
*bmap1
,
6243 __isl_keep isl_basic_map
*bmap2
)
6245 return isl_basic_map_fast_cmp(bmap1
, bmap2
) == 0;
6248 int isl_basic_set_fast_is_equal(__isl_keep isl_basic_set
*bset1
,
6249 __isl_keep isl_basic_set
*bset2
)
6251 return isl_basic_map_fast_is_equal((isl_basic_map
*)bset1
,
6252 (isl_basic_map
*)bset2
);
6255 static int qsort_bmap_cmp(const void *p1
, const void *p2
)
6257 const struct isl_basic_map
*bmap1
= *(const struct isl_basic_map
**)p1
;
6258 const struct isl_basic_map
*bmap2
= *(const struct isl_basic_map
**)p2
;
6260 return isl_basic_map_fast_cmp(bmap1
, bmap2
);
6263 /* We normalize in place, but if anything goes wrong we need
6264 * to return NULL, so we need to make sure we don't change the
6265 * meaning of any possible other copies of map.
6267 struct isl_map
*isl_map_normalize(struct isl_map
*map
)
6270 struct isl_basic_map
*bmap
;
6274 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
))
6276 for (i
= 0; i
< map
->n
; ++i
) {
6277 bmap
= isl_basic_map_normalize(isl_basic_map_copy(map
->p
[i
]));
6280 isl_basic_map_free(map
->p
[i
]);
6283 qsort(map
->p
, map
->n
, sizeof(struct isl_basic_map
*), qsort_bmap_cmp
);
6284 ISL_F_SET(map
, ISL_MAP_NORMALIZED
);
6285 map
= isl_map_remove_empty_parts(map
);
6288 for (i
= map
->n
- 1; i
>= 1; --i
) {
6289 if (!isl_basic_map_fast_is_equal(map
->p
[i
-1], map
->p
[i
]))
6291 isl_basic_map_free(map
->p
[i
-1]);
6292 for (j
= i
; j
< map
->n
; ++j
)
6293 map
->p
[j
-1] = map
->p
[j
];
6303 struct isl_set
*isl_set_normalize(struct isl_set
*set
)
6305 return (struct isl_set
*)isl_map_normalize((struct isl_map
*)set
);
6308 int isl_map_fast_is_equal(struct isl_map
*map1
, struct isl_map
*map2
)
6318 if (!isl_dim_equal(map1
->dim
, map2
->dim
))
6321 map1
= isl_map_copy(map1
);
6322 map2
= isl_map_copy(map2
);
6323 map1
= isl_map_normalize(map1
);
6324 map2
= isl_map_normalize(map2
);
6327 equal
= map1
->n
== map2
->n
;
6328 for (i
= 0; equal
&& i
< map1
->n
; ++i
) {
6329 equal
= isl_basic_map_fast_is_equal(map1
->p
[i
], map2
->p
[i
]);
6342 int isl_set_fast_is_equal(struct isl_set
*set1
, struct isl_set
*set2
)
6344 return isl_map_fast_is_equal((struct isl_map
*)set1
,
6345 (struct isl_map
*)set2
);
6348 /* Return an interval that ranges from min to max (inclusive)
6350 struct isl_basic_set
*isl_basic_set_interval(struct isl_ctx
*ctx
,
6351 isl_int min
, isl_int max
)
6354 struct isl_basic_set
*bset
= NULL
;
6356 bset
= isl_basic_set_alloc(ctx
, 0, 1, 0, 0, 2);
6360 k
= isl_basic_set_alloc_inequality(bset
);
6363 isl_int_set_si(bset
->ineq
[k
][1], 1);
6364 isl_int_neg(bset
->ineq
[k
][0], min
);
6366 k
= isl_basic_set_alloc_inequality(bset
);
6369 isl_int_set_si(bset
->ineq
[k
][1], -1);
6370 isl_int_set(bset
->ineq
[k
][0], max
);
6374 isl_basic_set_free(bset
);
6378 /* Return the Cartesian product of the basic sets in list (in the given order).
6380 struct isl_basic_set
*isl_basic_set_product(struct isl_basic_set_list
*list
)
6388 struct isl_basic_set
*product
= NULL
;
6392 isl_assert(list
->ctx
, list
->n
> 0, goto error
);
6393 isl_assert(list
->ctx
, list
->p
[0], goto error
);
6394 nparam
= isl_basic_set_n_param(list
->p
[0]);
6395 dim
= isl_basic_set_n_dim(list
->p
[0]);
6396 extra
= list
->p
[0]->n_div
;
6397 n_eq
= list
->p
[0]->n_eq
;
6398 n_ineq
= list
->p
[0]->n_ineq
;
6399 for (i
= 1; i
< list
->n
; ++i
) {
6400 isl_assert(list
->ctx
, list
->p
[i
], goto error
);
6401 isl_assert(list
->ctx
,
6402 nparam
== isl_basic_set_n_param(list
->p
[i
]), goto error
);
6403 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
6404 extra
+= list
->p
[i
]->n_div
;
6405 n_eq
+= list
->p
[i
]->n_eq
;
6406 n_ineq
+= list
->p
[i
]->n_ineq
;
6408 product
= isl_basic_set_alloc(list
->ctx
, nparam
, dim
, extra
,
6413 for (i
= 0; i
< list
->n
; ++i
) {
6414 isl_basic_set_add_constraints(product
,
6415 isl_basic_set_copy(list
->p
[i
]), dim
);
6416 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
6418 isl_basic_set_list_free(list
);
6421 isl_basic_set_free(product
);
6422 isl_basic_set_list_free(list
);
6426 struct isl_basic_map
*isl_basic_map_product(
6427 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
6429 struct isl_dim
*dim_result
= NULL
;
6430 struct isl_basic_map
*bmap
;
6431 unsigned in1
, in2
, out1
, out2
, nparam
, total
, pos
;
6432 struct isl_dim_map
*dim_map1
, *dim_map2
;
6434 if (!bmap1
|| !bmap2
)
6437 isl_assert(bmap1
->ctx
, isl_dim_match(bmap1
->dim
, isl_dim_param
,
6438 bmap2
->dim
, isl_dim_param
), goto error
);
6439 dim_result
= isl_dim_product(isl_dim_copy(bmap1
->dim
),
6440 isl_dim_copy(bmap2
->dim
));
6442 in1
= isl_basic_map_n_in(bmap1
);
6443 in2
= isl_basic_map_n_in(bmap2
);
6444 out1
= isl_basic_map_n_out(bmap1
);
6445 out2
= isl_basic_map_n_out(bmap2
);
6446 nparam
= isl_basic_map_n_param(bmap1
);
6448 total
= nparam
+ in1
+ in2
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
6449 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
6450 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
6451 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
6452 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
6453 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
6454 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
6455 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
6456 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
6457 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
6458 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
6460 bmap
= isl_basic_map_alloc_dim(dim_result
,
6461 bmap1
->n_div
+ bmap2
->n_div
,
6462 bmap1
->n_eq
+ bmap2
->n_eq
,
6463 bmap1
->n_ineq
+ bmap2
->n_ineq
);
6464 bmap
= add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
6465 bmap
= add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
6466 bmap
= isl_basic_map_simplify(bmap
);
6467 return isl_basic_map_finalize(bmap
);
6469 isl_basic_map_free(bmap1
);
6470 isl_basic_map_free(bmap2
);
6474 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
6476 struct isl_map
*isl_map_product(struct isl_map
*map1
, struct isl_map
*map2
)
6479 struct isl_map
*result
;
6485 isl_assert(map1
->ctx
, isl_dim_match(map1
->dim
, isl_dim_param
,
6486 map2
->dim
, isl_dim_param
), goto error
);
6488 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
6489 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
6490 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
6492 result
= isl_map_alloc_dim(isl_dim_product(isl_dim_copy(map1
->dim
),
6493 isl_dim_copy(map2
->dim
)),
6494 map1
->n
* map2
->n
, flags
);
6497 for (i
= 0; i
< map1
->n
; ++i
)
6498 for (j
= 0; j
< map2
->n
; ++j
) {
6499 struct isl_basic_map
*part
;
6500 part
= isl_basic_map_product(
6501 isl_basic_map_copy(map1
->p
[i
]),
6502 isl_basic_map_copy(map2
->p
[j
]));
6503 if (isl_basic_map_is_empty(part
))
6504 isl_basic_map_free(part
);
6506 result
= isl_map_add_basic_map(result
, part
);
6519 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
6521 __isl_give isl_map
*isl_map_flat_product(__isl_take isl_map
*map1
,
6522 __isl_take isl_map
*map2
)
6526 prod
= isl_map_product(map1
, map2
);
6527 prod
= isl_map_flatten(prod
);
6531 /* Given two set A and B, construct its Cartesian product A x B.
6533 struct isl_set
*isl_set_product(struct isl_set
*set1
, struct isl_set
*set2
)
6535 return (struct isl_set
*)isl_map_product((struct isl_map
*)set1
,
6536 (struct isl_map
*)set2
);
6539 __isl_give isl_set
*isl_set_flat_product(__isl_take isl_set
*set1
,
6540 __isl_take isl_set
*set2
)
6542 return (isl_set
*)isl_map_flat_product((isl_map
*)set1
, (isl_map
*)set2
);
6545 uint32_t isl_basic_set_get_hash(struct isl_basic_set
*bset
)
6548 uint32_t hash
= isl_hash_init();
6553 bset
= isl_basic_set_copy(bset
);
6554 bset
= isl_basic_set_normalize(bset
);
6557 total
= isl_basic_set_total_dim(bset
);
6558 isl_hash_byte(hash
, bset
->n_eq
& 0xFF);
6559 for (i
= 0; i
< bset
->n_eq
; ++i
) {
6561 c_hash
= isl_seq_get_hash(bset
->eq
[i
], 1 + total
);
6562 isl_hash_hash(hash
, c_hash
);
6564 isl_hash_byte(hash
, bset
->n_ineq
& 0xFF);
6565 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
6567 c_hash
= isl_seq_get_hash(bset
->ineq
[i
], 1 + total
);
6568 isl_hash_hash(hash
, c_hash
);
6570 isl_hash_byte(hash
, bset
->n_div
& 0xFF);
6571 for (i
= 0; i
< bset
->n_div
; ++i
) {
6573 if (isl_int_is_zero(bset
->div
[i
][0]))
6575 isl_hash_byte(hash
, i
& 0xFF);
6576 c_hash
= isl_seq_get_hash(bset
->div
[i
], 1 + 1 + total
);
6577 isl_hash_hash(hash
, c_hash
);
6579 isl_basic_set_free(bset
);
6583 uint32_t isl_set_get_hash(struct isl_set
*set
)
6590 set
= isl_set_copy(set
);
6591 set
= isl_set_normalize(set
);
6595 hash
= isl_hash_init();
6596 for (i
= 0; i
< set
->n
; ++i
) {
6598 bset_hash
= isl_basic_set_get_hash(set
->p
[i
]);
6599 isl_hash_hash(hash
, bset_hash
);
6607 /* Check if the value for dimension dim is completely determined
6608 * by the values of the other parameters and variables.
6609 * That is, check if dimension dim is involved in an equality.
6611 int isl_basic_set_dim_is_unique(struct isl_basic_set
*bset
, unsigned dim
)
6618 nparam
= isl_basic_set_n_param(bset
);
6619 for (i
= 0; i
< bset
->n_eq
; ++i
)
6620 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ dim
]))
6625 /* Check if the value for dimension dim is completely determined
6626 * by the values of the other parameters and variables.
6627 * That is, check if dimension dim is involved in an equality
6628 * for each of the subsets.
6630 int isl_set_dim_is_unique(struct isl_set
*set
, unsigned dim
)
6636 for (i
= 0; i
< set
->n
; ++i
) {
6638 unique
= isl_basic_set_dim_is_unique(set
->p
[i
], dim
);
6645 int isl_map_foreach_basic_map(__isl_keep isl_map
*map
,
6646 int (*fn
)(__isl_take isl_basic_map
*bmap
, void *user
), void *user
)
6653 for (i
= 0; i
< map
->n
; ++i
)
6654 if (fn(isl_basic_map_copy(map
->p
[i
]), user
) < 0)
6660 int isl_set_foreach_basic_set(__isl_keep isl_set
*set
,
6661 int (*fn
)(__isl_take isl_basic_set
*bset
, void *user
), void *user
)
6668 for (i
= 0; i
< set
->n
; ++i
)
6669 if (fn(isl_basic_set_copy(set
->p
[i
]), user
) < 0)
6675 __isl_give isl_basic_set
*isl_basic_set_lift(__isl_take isl_basic_set
*bset
)
6677 struct isl_dim
*dim
;
6682 if (bset
->n_div
== 0)
6685 bset
= isl_basic_set_cow(bset
);
6689 dim
= isl_basic_set_get_dim(bset
);
6690 dim
= isl_dim_add(dim
, isl_dim_set
, bset
->n_div
);
6693 isl_dim_free(bset
->dim
);
6697 bset
= isl_basic_set_finalize(bset
);
6701 isl_basic_set_free(bset
);
6705 __isl_give isl_set
*isl_set_lift(__isl_take isl_set
*set
)
6708 struct isl_dim
*dim
;
6711 set
= isl_set_align_divs(set
);
6715 if (set
->n
== 0 || set
->p
[0]->n_div
== 0)
6718 set
= isl_set_cow(set
);
6722 n_div
= set
->p
[0]->n_div
;
6723 dim
= isl_set_get_dim(set
);
6724 dim
= isl_dim_add(dim
, isl_dim_set
, n_div
);
6727 isl_dim_free(set
->dim
);
6730 for (i
= 0; i
< set
->n
; ++i
) {
6731 set
->p
[i
] = isl_basic_set_lift(set
->p
[i
]);
6742 __isl_give isl_map
*isl_set_lifting(__isl_take isl_set
*set
)
6744 struct isl_dim
*dim
;
6745 struct isl_basic_map
*bmap
;
6752 set
= isl_set_align_divs(set
);
6757 dim
= isl_set_get_dim(set
);
6758 if (set
->n
== 0 || set
->p
[0]->n_div
== 0) {
6760 return isl_map_identity(dim
);
6763 n_div
= set
->p
[0]->n_div
;
6764 dim
= isl_dim_map(dim
);
6765 n_param
= isl_dim_size(dim
, isl_dim_param
);
6766 n_set
= isl_dim_size(dim
, isl_dim_in
);
6767 dim
= isl_dim_extend(dim
, n_param
, n_set
, n_set
+ n_div
);
6768 bmap
= isl_basic_map_alloc_dim(dim
, 0, n_set
, 2 * n_div
);
6769 for (i
= 0; i
< n_set
; ++i
)
6770 bmap
= var_equal(bmap
, i
);
6772 total
= n_param
+ n_set
+ n_set
+ n_div
;
6773 for (i
= 0; i
< n_div
; ++i
) {
6774 k
= isl_basic_map_alloc_inequality(bmap
);
6777 isl_seq_cpy(bmap
->ineq
[k
], set
->p
[0]->div
[i
]+1, 1+n_param
);
6778 isl_seq_clr(bmap
->ineq
[k
]+1+n_param
, n_set
);
6779 isl_seq_cpy(bmap
->ineq
[k
]+1+n_param
+n_set
,
6780 set
->p
[0]->div
[i
]+1+1+n_param
, n_set
+ n_div
);
6781 isl_int_neg(bmap
->ineq
[k
][1+n_param
+n_set
+n_set
+i
],
6782 set
->p
[0]->div
[i
][0]);
6784 l
= isl_basic_map_alloc_inequality(bmap
);
6787 isl_seq_neg(bmap
->ineq
[l
], bmap
->ineq
[k
], 1 + total
);
6788 isl_int_add(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0],
6789 set
->p
[0]->div
[i
][0]);
6790 isl_int_sub_ui(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], 1);
6794 return isl_map_from_basic_map(bmap
);
6797 isl_basic_map_free(bmap
);
6801 int isl_basic_set_size(__isl_keep isl_basic_set
*bset
)
6809 dim
= isl_basic_set_total_dim(bset
);
6810 size
+= bset
->n_eq
* (1 + dim
);
6811 size
+= bset
->n_ineq
* (1 + dim
);
6812 size
+= bset
->n_div
* (2 + dim
);
6817 int isl_set_size(__isl_keep isl_set
*set
)
6825 for (i
= 0; i
< set
->n
; ++i
)
6826 size
+= isl_basic_set_size(set
->p
[i
]);
6831 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
6832 enum isl_dim_type type
, unsigned pos
)
6840 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), return -1);
6842 pos
+= isl_basic_map_offset(bmap
, type
);
6844 for (i
= 0; i
< bmap
->n_eq
; ++i
)
6845 if (!isl_int_is_zero(bmap
->eq
[i
][pos
]))
6849 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
6850 int sgn
= isl_int_sgn(bmap
->ineq
[i
][pos
]);
6857 return lower
&& upper
;
6860 int isl_map_dim_is_bounded(__isl_keep isl_map
*map
,
6861 enum isl_dim_type type
, unsigned pos
)
6868 for (i
= 0; i
< map
->n
; ++i
) {
6870 bounded
= isl_basic_map_dim_is_bounded(map
->p
[i
], type
, pos
);
6871 if (bounded
< 0 || !bounded
)
6878 /* Return 1 if the specified dim is involved in both an upper bound
6879 * and a lower bound.
6881 int isl_set_dim_is_bounded(__isl_keep isl_set
*set
,
6882 enum isl_dim_type type
, unsigned pos
)
6884 return isl_map_dim_is_bounded((isl_map
*)set
, type
, pos
);
6887 /* For each of the "n" variables starting at "first", determine
6888 * the sign of the variable and put the results in the first "n"
6889 * elements of the array "signs".
6891 * 1 means that the variable is non-negative
6892 * -1 means that the variable is non-positive
6893 * 0 means the variable attains both positive and negative values.
6895 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set
*bset
,
6896 unsigned first
, unsigned n
, int *signs
)
6898 isl_vec
*bound
= NULL
;
6899 struct isl_tab
*tab
= NULL
;
6900 struct isl_tab_undo
*snap
;
6903 if (!bset
|| !signs
)
6906 bound
= isl_vec_alloc(bset
->ctx
, 1 + isl_basic_set_total_dim(bset
));
6907 tab
= isl_tab_from_basic_set(bset
);
6911 isl_seq_clr(bound
->el
, bound
->size
);
6912 isl_int_set_si(bound
->el
[0], -1);
6914 snap
= isl_tab_snap(tab
);
6915 for (i
= 0; i
< n
; ++i
) {
6918 isl_int_set_si(bound
->el
[1 + first
+ i
], -1);
6919 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
6922 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
6923 if (isl_tab_rollback(tab
, snap
) < 0)
6931 isl_int_set_si(bound
->el
[1 + first
+ i
], 1);
6932 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
6935 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
6936 if (isl_tab_rollback(tab
, snap
) < 0)
6939 signs
[i
] = empty
? -1 : 0;
6943 isl_vec_free(bound
);
6947 isl_vec_free(bound
);
6951 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set
*bset
,
6952 enum isl_dim_type type
, unsigned first
, unsigned n
, int *signs
)
6954 if (!bset
|| !signs
)
6956 isl_assert(bset
->ctx
, first
+ n
<= isl_basic_set_dim(bset
, type
),
6959 first
+= pos(bset
->dim
, type
) - 1;
6960 return isl_basic_set_vars_get_sign(bset
, first
, n
, signs
);
6963 /* Check if the given map is single-valued.
6968 * and check if the result is a subset of the identity mapping.
6970 int isl_map_is_single_valued(__isl_keep isl_map
*map
)
6976 test
= isl_map_reverse(isl_map_copy(map
));
6977 test
= isl_map_apply_range(test
, isl_map_copy(map
));
6979 id
= isl_map_identity(isl_dim_range(isl_map_get_dim(map
)));
6981 sv
= isl_map_is_subset(test
, id
);
6989 int isl_map_is_bijective(__isl_keep isl_map
*map
)
6993 sv
= isl_map_is_single_valued(map
);
6997 map
= isl_map_copy(map
);
6998 map
= isl_map_reverse(map
);
6999 sv
= isl_map_is_single_valued(map
);
7005 int isl_set_is_singleton(__isl_keep isl_set
*set
)
7007 return isl_map_is_single_valued((isl_map
*)set
);
7010 int isl_map_is_translation(__isl_keep isl_map
*map
)
7015 delta
= isl_map_deltas(isl_map_copy(map
));
7016 ok
= isl_set_is_singleton(delta
);
7017 isl_set_free(delta
);
7022 static int unique(isl_int
*p
, unsigned pos
, unsigned len
)
7024 if (isl_seq_first_non_zero(p
, pos
) != -1)
7026 if (isl_seq_first_non_zero(p
+ pos
+ 1, len
- pos
- 1) != -1)
7031 int isl_basic_set_is_box(__isl_keep isl_basic_set
*bset
)
7040 if (isl_basic_set_dim(bset
, isl_dim_div
) != 0)
7043 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
7044 ovar
= isl_dim_offset(bset
->dim
, isl_dim_set
);
7045 for (j
= 0; j
< nvar
; ++j
) {
7046 int lower
= 0, upper
= 0;
7047 for (i
= 0; i
< bset
->n_eq
; ++i
) {
7048 if (isl_int_is_zero(bset
->eq
[i
][1 + ovar
+ j
]))
7050 if (!unique(bset
->eq
[i
] + 1 + ovar
, j
, nvar
))
7056 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
7057 if (isl_int_is_zero(bset
->ineq
[i
][1 + ovar
+ j
]))
7059 if (!unique(bset
->ineq
[i
] + 1 + ovar
, j
, nvar
))
7061 if (isl_int_is_pos(bset
->ineq
[i
][1 + ovar
+ j
]))
7066 if (!lower
|| !upper
)
7073 int isl_set_is_box(__isl_keep isl_set
*set
)
7080 return isl_basic_set_is_box(set
->p
[0]);
7083 int isl_basic_set_is_wrapping(__isl_keep isl_basic_set
*bset
)
7088 return isl_dim_is_wrapping(bset
->dim
);
7091 int isl_set_is_wrapping(__isl_keep isl_set
*set
)
7096 return isl_dim_is_wrapping(set
->dim
);
7099 __isl_give isl_basic_set
*isl_basic_map_wrap(__isl_take isl_basic_map
*bmap
)
7101 bmap
= isl_basic_map_cow(bmap
);
7105 bmap
->dim
= isl_dim_wrap(bmap
->dim
);
7109 bmap
= isl_basic_map_finalize(bmap
);
7111 return (isl_basic_set
*)bmap
;
7113 isl_basic_map_free(bmap
);
7117 __isl_give isl_set
*isl_map_wrap(__isl_take isl_map
*map
)
7121 map
= isl_map_cow(map
);
7125 for (i
= 0; i
< map
->n
; ++i
) {
7126 map
->p
[i
] = (isl_basic_map
*)isl_basic_map_wrap(map
->p
[i
]);
7130 map
->dim
= isl_dim_wrap(map
->dim
);
7134 return (isl_set
*)map
;
7140 __isl_give isl_basic_map
*isl_basic_set_unwrap(__isl_take isl_basic_set
*bset
)
7142 bset
= isl_basic_set_cow(bset
);
7146 bset
->dim
= isl_dim_unwrap(bset
->dim
);
7150 bset
= isl_basic_set_finalize(bset
);
7152 return (isl_basic_map
*)bset
;
7154 isl_basic_set_free(bset
);
7158 __isl_give isl_map
*isl_set_unwrap(__isl_take isl_set
*set
)
7165 if (!isl_set_is_wrapping(set
))
7166 isl_die(set
->ctx
, isl_error_invalid
, "not a wrapping set",
7169 set
= isl_set_cow(set
);
7173 for (i
= 0; i
< set
->n
; ++i
) {
7174 set
->p
[i
] = (isl_basic_set
*)isl_basic_set_unwrap(set
->p
[i
]);
7179 set
->dim
= isl_dim_unwrap(set
->dim
);
7183 return (isl_map
*)set
;
7189 __isl_give isl_basic_map
*isl_basic_map_reset(__isl_take isl_basic_map
*bmap
,
7190 enum isl_dim_type type
)
7195 if (!isl_dim_is_named_or_nested(bmap
->dim
, type
))
7198 bmap
= isl_basic_map_cow(bmap
);
7202 bmap
->dim
= isl_dim_reset(bmap
->dim
, type
);
7206 bmap
= isl_basic_map_finalize(bmap
);
7210 isl_basic_map_free(bmap
);
7214 __isl_give isl_map
*isl_map_reset(__isl_take isl_map
*map
,
7215 enum isl_dim_type type
)
7222 if (!isl_dim_is_named_or_nested(map
->dim
, type
))
7225 map
= isl_map_cow(map
);
7229 for (i
= 0; i
< map
->n
; ++i
) {
7230 map
->p
[i
] = isl_basic_map_reset(map
->p
[i
], type
);
7234 map
->dim
= isl_dim_reset(map
->dim
, type
);
7244 __isl_give isl_basic_map
*isl_basic_map_flatten(__isl_take isl_basic_map
*bmap
)
7249 if (!bmap
->dim
->nested
[0] && !bmap
->dim
->nested
[1])
7252 bmap
= isl_basic_map_cow(bmap
);
7256 bmap
->dim
= isl_dim_flatten(bmap
->dim
);
7260 bmap
= isl_basic_map_finalize(bmap
);
7264 isl_basic_map_free(bmap
);
7268 __isl_give isl_map
*isl_map_flatten(__isl_take isl_map
*map
)
7275 if (!map
->dim
->nested
[0] && !map
->dim
->nested
[1])
7278 map
= isl_map_cow(map
);
7282 for (i
= 0; i
< map
->n
; ++i
) {
7283 map
->p
[i
] = isl_basic_map_flatten(map
->p
[i
]);
7287 map
->dim
= isl_dim_flatten(map
->dim
);
7297 /* Extend the given dim_map with mappings for the divs in bmap.
7299 static __isl_give
struct isl_dim_map
*extend_dim_map(
7300 __isl_keep
struct isl_dim_map
*dim_map
,
7301 __isl_keep isl_basic_map
*bmap
)
7304 struct isl_dim_map
*res
;
7307 offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
7309 res
= isl_dim_map_alloc(bmap
->ctx
, dim_map
->len
- 1 + bmap
->n_div
);
7313 for (i
= 0; i
< dim_map
->len
; ++i
)
7314 res
->pos
[i
] = dim_map
->pos
[i
];
7315 for (i
= 0; i
< bmap
->n_div
; ++i
)
7316 res
->pos
[dim_map
->len
+ i
] = offset
+ i
;
7321 /* Extract a dim_map from a reordering.
7322 * We essentially need to reverse the mapping, and add an offset
7323 * of 1 for the constant term.
7325 __isl_give
struct isl_dim_map
*isl_dim_map_from_reordering(
7326 __isl_keep isl_reordering
*exp
)
7329 struct isl_dim_map
*dim_map
;
7334 dim_map
= isl_dim_map_alloc(exp
->dim
->ctx
, isl_dim_total(exp
->dim
));
7338 for (i
= 0; i
< exp
->len
; ++i
)
7339 dim_map
->pos
[1 + exp
->pos
[i
]] = 1 + i
;
7344 /* Reorder the dimensions of "bmap" according to the given dim_map
7345 * and set the dimension specification to "dim".
7347 __isl_give isl_basic_map
*isl_basic_map_realign(__isl_take isl_basic_map
*bmap
,
7348 __isl_take isl_dim
*dim
, __isl_take
struct isl_dim_map
*dim_map
)
7352 bmap
= isl_basic_map_cow(bmap
);
7353 if (!bmap
|| !dim
|| !dim_map
)
7356 res
= isl_basic_map_alloc_dim(dim
,
7357 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
7358 res
= add_constraints_dim_map(res
, bmap
, dim_map
);
7359 res
= isl_basic_map_finalize(res
);
7363 isl_basic_map_free(bmap
);
7368 /* Reorder the dimensions of "map" according to given reordering.
7370 __isl_give isl_map
*isl_map_realign(__isl_take isl_map
*map
,
7371 __isl_take isl_reordering
*r
)
7374 struct isl_dim_map
*dim_map
;
7376 map
= isl_map_cow(map
);
7377 dim_map
= isl_dim_map_from_reordering(r
);
7378 if (!map
|| !r
|| !dim_map
)
7381 for (i
= 0; i
< map
->n
; ++i
) {
7382 struct isl_dim_map
*dim_map_i
;
7384 dim_map_i
= extend_dim_map(dim_map
, map
->p
[i
]);
7386 map
->p
[i
] = isl_basic_map_realign(map
->p
[i
],
7387 isl_dim_copy(r
->dim
), dim_map_i
);
7393 map
= isl_map_reset_dim(map
, isl_dim_copy(r
->dim
));
7395 isl_reordering_free(r
);
7401 isl_reordering_free(r
);
7405 __isl_give isl_set
*isl_set_realign(__isl_take isl_set
*set
,
7406 __isl_take isl_reordering
*r
)
7408 return (isl_set
*)isl_map_realign((isl_map
*)set
, r
);