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
15 #include <isl_ctx_private.h>
16 #include <isl_map_private.h>
18 #include "isl_dim_private.h"
19 #include "isl_equalities.h"
25 #include "isl_map_piplib.h"
26 #include <isl_reordering.h>
27 #include "isl_sample.h"
30 #include <isl_mat_private.h>
31 #include <isl_dim_map.h>
32 #include <isl_local_space_private.h>
34 static unsigned n(struct isl_dim
*dim
, enum isl_dim_type type
)
37 case isl_dim_param
: return dim
->nparam
;
38 case isl_dim_in
: return dim
->n_in
;
39 case isl_dim_out
: return dim
->n_out
;
40 case isl_dim_all
: return dim
->nparam
+ dim
->n_in
+ dim
->n_out
;
45 static unsigned pos(struct isl_dim
*dim
, enum isl_dim_type type
)
48 case isl_dim_param
: return 1;
49 case isl_dim_in
: return 1 + dim
->nparam
;
50 case isl_dim_out
: return 1 + dim
->nparam
+ dim
->n_in
;
55 unsigned isl_basic_map_dim(const struct isl_basic_map
*bmap
,
56 enum isl_dim_type type
)
61 case isl_dim_cst
: return 1;
64 case isl_dim_out
: return isl_dim_size(bmap
->dim
, type
);
65 case isl_dim_div
: return bmap
->n_div
;
66 case isl_dim_all
: return isl_basic_map_total_dim(bmap
);
71 unsigned isl_map_dim(const struct isl_map
*map
, enum isl_dim_type type
)
73 return map
? n(map
->dim
, type
) : 0;
76 unsigned isl_set_dim(const struct isl_set
*set
, enum isl_dim_type type
)
78 return set
? n(set
->dim
, type
) : 0;
81 unsigned isl_basic_map_offset(struct isl_basic_map
*bmap
,
82 enum isl_dim_type type
)
84 struct isl_dim
*dim
= bmap
->dim
;
86 case isl_dim_cst
: return 0;
87 case isl_dim_param
: return 1;
88 case isl_dim_in
: return 1 + dim
->nparam
;
89 case isl_dim_out
: return 1 + dim
->nparam
+ dim
->n_in
;
90 case isl_dim_div
: return 1 + dim
->nparam
+ dim
->n_in
+ dim
->n_out
;
95 unsigned isl_basic_set_offset(struct isl_basic_set
*bset
,
96 enum isl_dim_type type
)
98 return isl_basic_map_offset(bset
, type
);
101 static unsigned map_offset(struct isl_map
*map
, enum isl_dim_type type
)
103 return pos(map
->dim
, type
);
106 unsigned isl_basic_set_dim(const struct isl_basic_set
*bset
,
107 enum isl_dim_type type
)
109 return isl_basic_map_dim((const struct isl_basic_map
*)bset
, type
);
112 unsigned isl_basic_set_n_dim(const struct isl_basic_set
*bset
)
114 return isl_basic_set_dim(bset
, isl_dim_set
);
117 unsigned isl_basic_set_n_param(const struct isl_basic_set
*bset
)
119 return isl_basic_set_dim(bset
, isl_dim_param
);
122 unsigned isl_basic_set_total_dim(const struct isl_basic_set
*bset
)
124 return isl_dim_total(bset
->dim
) + bset
->n_div
;
127 unsigned isl_set_n_dim(const struct isl_set
*set
)
129 return isl_set_dim(set
, isl_dim_set
);
132 unsigned isl_set_n_param(const struct isl_set
*set
)
134 return isl_set_dim(set
, isl_dim_param
);
137 unsigned isl_basic_map_n_in(const struct isl_basic_map
*bmap
)
139 return bmap
? bmap
->dim
->n_in
: 0;
142 unsigned isl_basic_map_n_out(const struct isl_basic_map
*bmap
)
144 return bmap
? bmap
->dim
->n_out
: 0;
147 unsigned isl_basic_map_n_param(const struct isl_basic_map
*bmap
)
149 return bmap
? bmap
->dim
->nparam
: 0;
152 unsigned isl_basic_map_n_div(const struct isl_basic_map
*bmap
)
154 return bmap
? bmap
->n_div
: 0;
157 unsigned isl_basic_map_total_dim(const struct isl_basic_map
*bmap
)
159 return bmap
? isl_dim_total(bmap
->dim
) + bmap
->n_div
: 0;
162 unsigned isl_map_n_in(const struct isl_map
*map
)
164 return map
->dim
->n_in
;
167 unsigned isl_map_n_out(const struct isl_map
*map
)
169 return map
->dim
->n_out
;
172 unsigned isl_map_n_param(const struct isl_map
*map
)
174 return map
->dim
->nparam
;
177 int isl_map_compatible_domain(struct isl_map
*map
, struct isl_set
*set
)
182 m
= isl_dim_match(map
->dim
, isl_dim_param
, set
->dim
, isl_dim_param
);
185 return isl_dim_tuple_match(map
->dim
, isl_dim_in
, set
->dim
, isl_dim_set
);
188 int isl_basic_map_compatible_domain(struct isl_basic_map
*bmap
,
189 struct isl_basic_set
*bset
)
194 m
= isl_dim_match(bmap
->dim
, isl_dim_param
, bset
->dim
, isl_dim_param
);
197 return isl_dim_tuple_match(bmap
->dim
, isl_dim_in
, bset
->dim
, isl_dim_set
);
200 int isl_map_compatible_range(__isl_keep isl_map
*map
, __isl_keep isl_set
*set
)
205 m
= isl_dim_match(map
->dim
, isl_dim_param
, set
->dim
, isl_dim_param
);
208 return isl_dim_tuple_match(map
->dim
, isl_dim_out
, set
->dim
, isl_dim_set
);
211 int isl_basic_map_compatible_range(struct isl_basic_map
*bmap
,
212 struct isl_basic_set
*bset
)
217 m
= isl_dim_match(bmap
->dim
, isl_dim_param
, bset
->dim
, isl_dim_param
);
220 return isl_dim_tuple_match(bmap
->dim
, isl_dim_out
, bset
->dim
, isl_dim_set
);
223 isl_ctx
*isl_basic_map_get_ctx(__isl_keep isl_basic_map
*bmap
)
225 return bmap
? bmap
->ctx
: NULL
;
228 isl_ctx
*isl_basic_set_get_ctx(__isl_keep isl_basic_set
*bset
)
230 return bset
? bset
->ctx
: NULL
;
233 isl_ctx
*isl_map_get_ctx(__isl_keep isl_map
*map
)
235 return map
? map
->ctx
: NULL
;
238 isl_ctx
*isl_set_get_ctx(__isl_keep isl_set
*set
)
240 return set
? set
->ctx
: NULL
;
243 struct isl_dim
*isl_basic_map_get_dim(struct isl_basic_map
*bmap
)
247 return isl_dim_copy(bmap
->dim
);
250 struct isl_dim
*isl_basic_set_get_dim(struct isl_basic_set
*bset
)
254 return isl_dim_copy(bset
->dim
);
257 __isl_give isl_local_space
*isl_basic_map_get_local_space(
258 __isl_keep isl_basic_map
*bmap
)
267 total
= isl_basic_map_total_dim(bmap
);
268 ls
= isl_local_space_alloc(isl_dim_copy(bmap
->dim
), bmap
->n_div
);
272 for (i
= 0; i
< bmap
->n_div
; ++i
)
273 isl_seq_cpy(ls
->div
->row
[i
], bmap
->div
[i
], 2 + total
);
278 __isl_give isl_local_space
*isl_basic_set_get_local_space(
279 __isl_keep isl_basic_set
*bset
)
281 return isl_basic_map_get_local_space(bset
);
284 __isl_give isl_basic_map
*isl_basic_map_from_local_space(
285 __isl_take isl_local_space
*ls
)
294 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
295 bmap
= isl_basic_map_alloc_dim(isl_local_space_get_dim(ls
),
296 n_div
, 0, 2 * n_div
);
298 for (i
= 0; i
< n_div
; ++i
)
299 if (isl_basic_map_alloc_div(bmap
) < 0)
302 for (i
= 0; i
< n_div
; ++i
) {
303 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], ls
->div
->n_col
);
304 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
308 isl_local_space_free(ls
);
311 isl_local_space_free(ls
);
312 isl_basic_map_free(bmap
);
316 __isl_give isl_basic_set
*isl_basic_set_from_local_space(
317 __isl_take isl_local_space
*ls
)
319 return isl_basic_map_from_local_space(ls
);
322 struct isl_dim
*isl_map_get_dim(struct isl_map
*map
)
326 return isl_dim_copy(map
->dim
);
329 struct isl_dim
*isl_set_get_dim(struct isl_set
*set
)
333 return isl_dim_copy(set
->dim
);
336 __isl_give isl_basic_map
*isl_basic_map_set_tuple_name(
337 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
, const char *s
)
339 bmap
= isl_basic_map_cow(bmap
);
342 bmap
->dim
= isl_dim_set_tuple_name(bmap
->dim
, type
, s
);
345 bmap
= isl_basic_map_finalize(bmap
);
348 isl_basic_map_free(bmap
);
352 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map
*bmap
,
353 enum isl_dim_type type
)
355 return bmap
? isl_dim_get_tuple_name(bmap
->dim
, type
) : NULL
;
358 __isl_give isl_map
*isl_map_set_tuple_name(__isl_take isl_map
*map
,
359 enum isl_dim_type type
, const char *s
)
363 map
= isl_map_cow(map
);
367 map
->dim
= isl_dim_set_tuple_name(map
->dim
, type
, s
);
371 for (i
= 0; i
< map
->n
; ++i
) {
372 map
->p
[i
] = isl_basic_map_set_tuple_name(map
->p
[i
], type
, s
);
383 const char *isl_map_get_tuple_name(__isl_keep isl_map
*map
,
384 enum isl_dim_type type
)
386 return map
? isl_dim_get_tuple_name(map
->dim
, type
) : NULL
;
389 __isl_give isl_set
*isl_set_set_tuple_name(__isl_take isl_set
*set
,
392 return (isl_set
*)isl_map_set_tuple_name((isl_map
*)set
, isl_dim_set
, s
);
395 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set
*bset
)
397 return bset
? isl_dim_get_tuple_name(bset
->dim
, isl_dim_set
) : NULL
;
400 const char *isl_set_get_tuple_name(__isl_keep isl_set
*set
)
402 return set
? isl_dim_get_tuple_name(set
->dim
, isl_dim_set
) : NULL
;
405 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map
*bmap
,
406 enum isl_dim_type type
, unsigned pos
)
408 return bmap
? isl_dim_get_name(bmap
->dim
, type
, pos
) : NULL
;
411 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set
*bset
,
412 enum isl_dim_type type
, unsigned pos
)
414 return bset
? isl_dim_get_name(bset
->dim
, type
, pos
) : NULL
;
417 const char *isl_map_get_dim_name(__isl_keep isl_map
*map
,
418 enum isl_dim_type type
, unsigned pos
)
420 return map
? isl_dim_get_name(map
->dim
, type
, pos
) : NULL
;
423 const char *isl_set_get_dim_name(__isl_keep isl_set
*set
,
424 enum isl_dim_type type
, unsigned pos
)
426 return set
? isl_dim_get_name(set
->dim
, type
, pos
) : NULL
;
429 __isl_give isl_basic_map
*isl_basic_map_set_dim_name(
430 __isl_take isl_basic_map
*bmap
,
431 enum isl_dim_type type
, unsigned pos
, const char *s
)
435 bmap
->dim
= isl_dim_set_name(bmap
->dim
, type
, pos
, s
);
440 isl_basic_map_free(bmap
);
444 __isl_give isl_map
*isl_map_set_dim_name(__isl_take isl_map
*map
,
445 enum isl_dim_type type
, unsigned pos
, const char *s
)
452 map
->dim
= isl_dim_set_name(map
->dim
, type
, pos
, s
);
456 for (i
= 0; i
< map
->n
; ++i
) {
457 map
->p
[i
] = isl_basic_map_set_dim_name(map
->p
[i
], type
, pos
, s
);
468 __isl_give isl_basic_set
*isl_basic_set_set_dim_name(
469 __isl_take isl_basic_set
*bset
,
470 enum isl_dim_type type
, unsigned pos
, const char *s
)
472 return (isl_basic_set
*)isl_basic_map_set_dim_name(
473 (isl_basic_map
*)bset
, type
, pos
, s
);
476 __isl_give isl_set
*isl_set_set_dim_name(__isl_take isl_set
*set
,
477 enum isl_dim_type type
, unsigned pos
, const char *s
)
479 return (isl_set
*)isl_map_set_dim_name((isl_map
*)set
, type
, pos
, s
);
482 int isl_basic_map_is_rational(__isl_keep isl_basic_map
*bmap
)
486 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
489 int isl_basic_set_is_rational(__isl_keep isl_basic_set
*bset
)
491 return isl_basic_map_is_rational(bset
);
494 static struct isl_basic_map
*basic_map_init(struct isl_ctx
*ctx
,
495 struct isl_basic_map
*bmap
, unsigned extra
,
496 unsigned n_eq
, unsigned n_ineq
)
499 size_t row_size
= 1 + isl_dim_total(bmap
->dim
) + extra
;
504 bmap
->block
= isl_blk_alloc(ctx
, (n_ineq
+ n_eq
) * row_size
);
505 if (isl_blk_is_error(bmap
->block
))
508 bmap
->ineq
= isl_alloc_array(ctx
, isl_int
*, n_ineq
+ n_eq
);
513 bmap
->block2
= isl_blk_empty();
516 bmap
->block2
= isl_blk_alloc(ctx
, extra
* (1 + row_size
));
517 if (isl_blk_is_error(bmap
->block2
))
520 bmap
->div
= isl_alloc_array(ctx
, isl_int
*, extra
);
525 for (i
= 0; i
< n_ineq
+ n_eq
; ++i
)
526 bmap
->ineq
[i
] = bmap
->block
.data
+ i
* row_size
;
528 for (i
= 0; i
< extra
; ++i
)
529 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + row_size
);
533 bmap
->c_size
= n_eq
+ n_ineq
;
534 bmap
->eq
= bmap
->ineq
+ n_ineq
;
543 isl_basic_map_free(bmap
);
547 struct isl_basic_set
*isl_basic_set_alloc(struct isl_ctx
*ctx
,
548 unsigned nparam
, unsigned dim
, unsigned extra
,
549 unsigned n_eq
, unsigned n_ineq
)
551 struct isl_basic_map
*bmap
;
552 bmap
= isl_basic_map_alloc(ctx
, nparam
, 0, dim
, extra
, n_eq
, n_ineq
);
553 return (struct isl_basic_set
*)bmap
;
556 struct isl_basic_set
*isl_basic_set_alloc_dim(struct isl_dim
*dim
,
557 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
559 struct isl_basic_map
*bmap
;
562 isl_assert(dim
->ctx
, dim
->n_in
== 0, goto error
);
563 bmap
= isl_basic_map_alloc_dim(dim
, extra
, n_eq
, n_ineq
);
564 return (struct isl_basic_set
*)bmap
;
570 struct isl_basic_map
*isl_basic_map_alloc_dim(struct isl_dim
*dim
,
571 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
573 struct isl_basic_map
*bmap
;
577 bmap
= isl_calloc_type(dim
->ctx
, struct isl_basic_map
);
582 return basic_map_init(dim
->ctx
, bmap
, extra
, n_eq
, n_ineq
);
588 struct isl_basic_map
*isl_basic_map_alloc(struct isl_ctx
*ctx
,
589 unsigned nparam
, unsigned in
, unsigned out
, unsigned extra
,
590 unsigned n_eq
, unsigned n_ineq
)
592 struct isl_basic_map
*bmap
;
595 dim
= isl_dim_alloc(ctx
, nparam
, in
, out
);
599 bmap
= isl_basic_map_alloc_dim(dim
, extra
, n_eq
, n_ineq
);
603 static void dup_constraints(
604 struct isl_basic_map
*dst
, struct isl_basic_map
*src
)
607 unsigned total
= isl_basic_map_total_dim(src
);
609 for (i
= 0; i
< src
->n_eq
; ++i
) {
610 int j
= isl_basic_map_alloc_equality(dst
);
611 isl_seq_cpy(dst
->eq
[j
], src
->eq
[i
], 1+total
);
614 for (i
= 0; i
< src
->n_ineq
; ++i
) {
615 int j
= isl_basic_map_alloc_inequality(dst
);
616 isl_seq_cpy(dst
->ineq
[j
], src
->ineq
[i
], 1+total
);
619 for (i
= 0; i
< src
->n_div
; ++i
) {
620 int j
= isl_basic_map_alloc_div(dst
);
621 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
);
623 ISL_F_SET(dst
, ISL_BASIC_SET_FINAL
);
626 struct isl_basic_map
*isl_basic_map_dup(struct isl_basic_map
*bmap
)
628 struct isl_basic_map
*dup
;
632 dup
= isl_basic_map_alloc_dim(isl_dim_copy(bmap
->dim
),
633 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
636 dup_constraints(dup
, bmap
);
637 dup
->flags
= bmap
->flags
;
638 dup
->sample
= isl_vec_copy(bmap
->sample
);
642 struct isl_basic_set
*isl_basic_set_dup(struct isl_basic_set
*bset
)
644 struct isl_basic_map
*dup
;
646 dup
= isl_basic_map_dup((struct isl_basic_map
*)bset
);
647 return (struct isl_basic_set
*)dup
;
650 struct isl_basic_set
*isl_basic_set_copy(struct isl_basic_set
*bset
)
655 if (ISL_F_ISSET(bset
, ISL_BASIC_SET_FINAL
)) {
659 return isl_basic_set_dup(bset
);
662 struct isl_set
*isl_set_copy(struct isl_set
*set
)
671 struct isl_basic_map
*isl_basic_map_copy(struct isl_basic_map
*bmap
)
676 if (ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
)) {
680 bmap
= isl_basic_map_dup(bmap
);
682 ISL_F_SET(bmap
, ISL_BASIC_SET_FINAL
);
686 struct isl_map
*isl_map_copy(struct isl_map
*map
)
695 void isl_basic_map_free(struct isl_basic_map
*bmap
)
703 isl_ctx_deref(bmap
->ctx
);
705 isl_blk_free(bmap
->ctx
, bmap
->block2
);
707 isl_blk_free(bmap
->ctx
, bmap
->block
);
708 isl_vec_free(bmap
->sample
);
709 isl_dim_free(bmap
->dim
);
713 void isl_basic_set_free(struct isl_basic_set
*bset
)
715 isl_basic_map_free((struct isl_basic_map
*)bset
);
718 static int room_for_con(struct isl_basic_map
*bmap
, unsigned n
)
720 return bmap
->n_eq
+ bmap
->n_ineq
+ n
<= bmap
->c_size
;
723 int isl_basic_map_alloc_equality(struct isl_basic_map
*bmap
)
729 isl_assert(ctx
, room_for_con(bmap
, 1), return -1);
730 isl_assert(ctx
, (bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
<= bmap
->c_size
,
732 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
733 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
734 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
735 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
736 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
737 if ((bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
== bmap
->c_size
) {
739 int j
= isl_basic_map_alloc_inequality(bmap
);
743 bmap
->ineq
[j
] = bmap
->ineq
[bmap
->n_ineq
- 1];
744 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
751 isl_seq_clr(bmap
->eq
[bmap
->n_eq
] + 1 + isl_basic_map_total_dim(bmap
),
752 bmap
->extra
- bmap
->n_div
);
756 int isl_basic_set_alloc_equality(struct isl_basic_set
*bset
)
758 return isl_basic_map_alloc_equality((struct isl_basic_map
*)bset
);
761 int isl_basic_map_free_equality(struct isl_basic_map
*bmap
, unsigned n
)
765 isl_assert(bmap
->ctx
, n
<= bmap
->n_eq
, return -1);
770 int isl_basic_set_free_equality(struct isl_basic_set
*bset
, unsigned n
)
772 return isl_basic_map_free_equality((struct isl_basic_map
*)bset
, n
);
775 int isl_basic_map_drop_equality(struct isl_basic_map
*bmap
, unsigned pos
)
780 isl_assert(bmap
->ctx
, pos
< bmap
->n_eq
, return -1);
782 if (pos
!= bmap
->n_eq
- 1) {
784 bmap
->eq
[pos
] = bmap
->eq
[bmap
->n_eq
- 1];
785 bmap
->eq
[bmap
->n_eq
- 1] = t
;
791 int isl_basic_set_drop_equality(struct isl_basic_set
*bset
, unsigned pos
)
793 return isl_basic_map_drop_equality((struct isl_basic_map
*)bset
, pos
);
796 void isl_basic_map_inequality_to_equality(
797 struct isl_basic_map
*bmap
, unsigned pos
)
802 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
803 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
808 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
809 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
810 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
811 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
814 static int room_for_ineq(struct isl_basic_map
*bmap
, unsigned n
)
816 return bmap
->n_ineq
+ n
<= bmap
->eq
- bmap
->ineq
;
819 int isl_basic_map_alloc_inequality(struct isl_basic_map
*bmap
)
825 isl_assert(ctx
, room_for_ineq(bmap
, 1), return -1);
826 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
827 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
828 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
829 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
830 isl_seq_clr(bmap
->ineq
[bmap
->n_ineq
] +
831 1 + isl_basic_map_total_dim(bmap
),
832 bmap
->extra
- bmap
->n_div
);
833 return bmap
->n_ineq
++;
836 int isl_basic_set_alloc_inequality(struct isl_basic_set
*bset
)
838 return isl_basic_map_alloc_inequality((struct isl_basic_map
*)bset
);
841 int isl_basic_map_free_inequality(struct isl_basic_map
*bmap
, unsigned n
)
845 isl_assert(bmap
->ctx
, n
<= bmap
->n_ineq
, return -1);
850 int isl_basic_set_free_inequality(struct isl_basic_set
*bset
, unsigned n
)
852 return isl_basic_map_free_inequality((struct isl_basic_map
*)bset
, n
);
855 int isl_basic_map_drop_inequality(struct isl_basic_map
*bmap
, unsigned pos
)
860 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
862 if (pos
!= bmap
->n_ineq
- 1) {
864 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
865 bmap
->ineq
[bmap
->n_ineq
- 1] = t
;
866 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
872 int isl_basic_set_drop_inequality(struct isl_basic_set
*bset
, unsigned pos
)
874 return isl_basic_map_drop_inequality((struct isl_basic_map
*)bset
, pos
);
877 __isl_give isl_basic_map
*isl_basic_map_add_eq(__isl_take isl_basic_map
*bmap
,
882 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
885 k
= isl_basic_map_alloc_equality(bmap
);
888 isl_seq_cpy(bmap
->eq
[k
], eq
, 1 + isl_basic_map_total_dim(bmap
));
891 isl_basic_map_free(bmap
);
895 __isl_give isl_basic_set
*isl_basic_set_add_eq(__isl_take isl_basic_set
*bset
,
898 return (isl_basic_set
*)
899 isl_basic_map_add_eq((isl_basic_map
*)bset
, eq
);
902 __isl_give isl_basic_map
*isl_basic_map_add_ineq(__isl_take isl_basic_map
*bmap
,
907 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
910 k
= isl_basic_map_alloc_inequality(bmap
);
913 isl_seq_cpy(bmap
->ineq
[k
], ineq
, 1 + isl_basic_map_total_dim(bmap
));
916 isl_basic_map_free(bmap
);
920 __isl_give isl_basic_set
*isl_basic_set_add_ineq(__isl_take isl_basic_set
*bset
,
923 return (isl_basic_set
*)
924 isl_basic_map_add_ineq((isl_basic_map
*)bset
, ineq
);
927 int isl_basic_map_alloc_div(struct isl_basic_map
*bmap
)
931 isl_assert(bmap
->ctx
, bmap
->n_div
< bmap
->extra
, return -1);
932 isl_seq_clr(bmap
->div
[bmap
->n_div
] +
933 1 + 1 + isl_basic_map_total_dim(bmap
),
934 bmap
->extra
- bmap
->n_div
);
935 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
936 return bmap
->n_div
++;
939 int isl_basic_set_alloc_div(struct isl_basic_set
*bset
)
941 return isl_basic_map_alloc_div((struct isl_basic_map
*)bset
);
944 int isl_basic_map_free_div(struct isl_basic_map
*bmap
, unsigned n
)
948 isl_assert(bmap
->ctx
, n
<= bmap
->n_div
, return -1);
953 int isl_basic_set_free_div(struct isl_basic_set
*bset
, unsigned n
)
955 return isl_basic_map_free_div((struct isl_basic_map
*)bset
, n
);
958 /* Copy constraint from src to dst, putting the vars of src at offset
959 * dim_off in dst and the divs of src at offset div_off in dst.
960 * If both sets are actually map, then dim_off applies to the input
963 static void copy_constraint(struct isl_basic_map
*dst_map
, isl_int
*dst
,
964 struct isl_basic_map
*src_map
, isl_int
*src
,
965 unsigned in_off
, unsigned out_off
, unsigned div_off
)
967 unsigned src_nparam
= isl_basic_map_n_param(src_map
);
968 unsigned dst_nparam
= isl_basic_map_n_param(dst_map
);
969 unsigned src_in
= isl_basic_map_n_in(src_map
);
970 unsigned dst_in
= isl_basic_map_n_in(dst_map
);
971 unsigned src_out
= isl_basic_map_n_out(src_map
);
972 unsigned dst_out
= isl_basic_map_n_out(dst_map
);
973 isl_int_set(dst
[0], src
[0]);
974 isl_seq_cpy(dst
+1, src
+1, isl_min(dst_nparam
, src_nparam
));
975 if (dst_nparam
> src_nparam
)
976 isl_seq_clr(dst
+1+src_nparam
,
977 dst_nparam
- src_nparam
);
978 isl_seq_clr(dst
+1+dst_nparam
, in_off
);
979 isl_seq_cpy(dst
+1+dst_nparam
+in_off
,
981 isl_min(dst_in
-in_off
, src_in
));
982 if (dst_in
-in_off
> src_in
)
983 isl_seq_clr(dst
+1+dst_nparam
+in_off
+src_in
,
984 dst_in
- in_off
- src_in
);
985 isl_seq_clr(dst
+1+dst_nparam
+dst_in
, out_off
);
986 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+out_off
,
987 src
+1+src_nparam
+src_in
,
988 isl_min(dst_out
-out_off
, src_out
));
989 if (dst_out
-out_off
> src_out
)
990 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+out_off
+src_out
,
991 dst_out
- out_off
- src_out
);
992 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
, div_off
);
993 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+dst_out
+div_off
,
994 src
+1+src_nparam
+src_in
+src_out
,
995 isl_min(dst_map
->extra
-div_off
, src_map
->n_div
));
996 if (dst_map
->n_div
-div_off
> src_map
->n_div
)
997 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
+
998 div_off
+src_map
->n_div
,
999 dst_map
->n_div
- div_off
- src_map
->n_div
);
1002 static void copy_div(struct isl_basic_map
*dst_map
, isl_int
*dst
,
1003 struct isl_basic_map
*src_map
, isl_int
*src
,
1004 unsigned in_off
, unsigned out_off
, unsigned div_off
)
1006 isl_int_set(dst
[0], src
[0]);
1007 copy_constraint(dst_map
, dst
+1, src_map
, src
+1, in_off
, out_off
, div_off
);
1010 static struct isl_basic_map
*add_constraints(struct isl_basic_map
*bmap1
,
1011 struct isl_basic_map
*bmap2
, unsigned i_pos
, unsigned o_pos
)
1016 if (!bmap1
|| !bmap2
)
1019 div_off
= bmap1
->n_div
;
1021 for (i
= 0; i
< bmap2
->n_eq
; ++i
) {
1022 int i1
= isl_basic_map_alloc_equality(bmap1
);
1025 copy_constraint(bmap1
, bmap1
->eq
[i1
], bmap2
, bmap2
->eq
[i
],
1026 i_pos
, o_pos
, div_off
);
1029 for (i
= 0; i
< bmap2
->n_ineq
; ++i
) {
1030 int i1
= isl_basic_map_alloc_inequality(bmap1
);
1033 copy_constraint(bmap1
, bmap1
->ineq
[i1
], bmap2
, bmap2
->ineq
[i
],
1034 i_pos
, o_pos
, div_off
);
1037 for (i
= 0; i
< bmap2
->n_div
; ++i
) {
1038 int i1
= isl_basic_map_alloc_div(bmap1
);
1041 copy_div(bmap1
, bmap1
->div
[i1
], bmap2
, bmap2
->div
[i
],
1042 i_pos
, o_pos
, div_off
);
1045 isl_basic_map_free(bmap2
);
1050 isl_basic_map_free(bmap1
);
1051 isl_basic_map_free(bmap2
);
1055 struct isl_basic_set
*isl_basic_set_add_constraints(struct isl_basic_set
*bset1
,
1056 struct isl_basic_set
*bset2
, unsigned pos
)
1058 return (struct isl_basic_set
*)
1059 add_constraints((struct isl_basic_map
*)bset1
,
1060 (struct isl_basic_map
*)bset2
, 0, pos
);
1063 struct isl_basic_map
*isl_basic_map_extend_dim(struct isl_basic_map
*base
,
1064 struct isl_dim
*dim
, unsigned extra
,
1065 unsigned n_eq
, unsigned n_ineq
)
1067 struct isl_basic_map
*ext
;
1077 dims_ok
= isl_dim_equal(base
->dim
, dim
) &&
1078 base
->extra
>= base
->n_div
+ extra
;
1080 if (dims_ok
&& room_for_con(base
, n_eq
+ n_ineq
) &&
1081 room_for_ineq(base
, n_ineq
)) {
1086 isl_assert(base
->ctx
, base
->dim
->nparam
<= dim
->nparam
, goto error
);
1087 isl_assert(base
->ctx
, base
->dim
->n_in
<= dim
->n_in
, goto error
);
1088 isl_assert(base
->ctx
, base
->dim
->n_out
<= dim
->n_out
, goto error
);
1089 extra
+= base
->extra
;
1091 n_ineq
+= base
->n_ineq
;
1093 ext
= isl_basic_map_alloc_dim(dim
, extra
, n_eq
, n_ineq
);
1099 ext
->sample
= isl_vec_copy(base
->sample
);
1100 flags
= base
->flags
;
1101 ext
= add_constraints(ext
, base
, 0, 0);
1104 ISL_F_CLR(ext
, ISL_BASIC_SET_FINAL
);
1111 isl_basic_map_free(base
);
1115 struct isl_basic_set
*isl_basic_set_extend_dim(struct isl_basic_set
*base
,
1116 struct isl_dim
*dim
, unsigned extra
,
1117 unsigned n_eq
, unsigned n_ineq
)
1119 return (struct isl_basic_set
*)
1120 isl_basic_map_extend_dim((struct isl_basic_map
*)base
, dim
,
1121 extra
, n_eq
, n_ineq
);
1124 struct isl_basic_map
*isl_basic_map_extend_constraints(
1125 struct isl_basic_map
*base
, unsigned n_eq
, unsigned n_ineq
)
1129 return isl_basic_map_extend_dim(base
, isl_dim_copy(base
->dim
),
1133 struct isl_basic_map
*isl_basic_map_extend(struct isl_basic_map
*base
,
1134 unsigned nparam
, unsigned n_in
, unsigned n_out
, unsigned extra
,
1135 unsigned n_eq
, unsigned n_ineq
)
1137 struct isl_basic_map
*bmap
;
1138 struct isl_dim
*dim
;
1142 dim
= isl_dim_alloc(base
->ctx
, nparam
, n_in
, n_out
);
1146 bmap
= isl_basic_map_extend_dim(base
, dim
, extra
, n_eq
, n_ineq
);
1149 isl_basic_map_free(base
);
1153 struct isl_basic_set
*isl_basic_set_extend(struct isl_basic_set
*base
,
1154 unsigned nparam
, unsigned dim
, unsigned extra
,
1155 unsigned n_eq
, unsigned n_ineq
)
1157 return (struct isl_basic_set
*)
1158 isl_basic_map_extend((struct isl_basic_map
*)base
,
1159 nparam
, 0, dim
, extra
, n_eq
, n_ineq
);
1162 struct isl_basic_set
*isl_basic_set_extend_constraints(
1163 struct isl_basic_set
*base
, unsigned n_eq
, unsigned n_ineq
)
1165 return (struct isl_basic_set
*)
1166 isl_basic_map_extend_constraints((struct isl_basic_map
*)base
,
1170 struct isl_basic_set
*isl_basic_set_cow(struct isl_basic_set
*bset
)
1172 return (struct isl_basic_set
*)
1173 isl_basic_map_cow((struct isl_basic_map
*)bset
);
1176 struct isl_basic_map
*isl_basic_map_cow(struct isl_basic_map
*bmap
)
1181 if (bmap
->ref
> 1) {
1183 bmap
= isl_basic_map_dup(bmap
);
1186 ISL_F_CLR(bmap
, ISL_BASIC_SET_FINAL
);
1190 struct isl_set
*isl_set_cow(struct isl_set
*set
)
1198 return isl_set_dup(set
);
1201 struct isl_map
*isl_map_cow(struct isl_map
*map
)
1209 return isl_map_dup(map
);
1212 static void swap_vars(struct isl_blk blk
, isl_int
*a
,
1213 unsigned a_len
, unsigned b_len
)
1215 isl_seq_cpy(blk
.data
, a
+a_len
, b_len
);
1216 isl_seq_cpy(blk
.data
+b_len
, a
, a_len
);
1217 isl_seq_cpy(a
, blk
.data
, b_len
+a_len
);
1220 static __isl_give isl_basic_map
*isl_basic_map_swap_vars(
1221 __isl_take isl_basic_map
*bmap
, unsigned pos
, unsigned n1
, unsigned n2
)
1229 isl_assert(bmap
->ctx
,
1230 pos
+ n1
+ n2
<= 1 + isl_basic_map_total_dim(bmap
), goto error
);
1232 if (n1
== 0 || n2
== 0)
1235 bmap
= isl_basic_map_cow(bmap
);
1239 blk
= isl_blk_alloc(bmap
->ctx
, n1
+ n2
);
1240 if (isl_blk_is_error(blk
))
1243 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1245 bmap
->eq
[i
] + pos
, n1
, n2
);
1247 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1249 bmap
->ineq
[i
] + pos
, n1
, n2
);
1251 for (i
= 0; i
< bmap
->n_div
; ++i
)
1253 bmap
->div
[i
]+1 + pos
, n1
, n2
);
1255 isl_blk_free(bmap
->ctx
, blk
);
1257 ISL_F_CLR(bmap
, ISL_BASIC_SET_NORMALIZED
);
1258 bmap
= isl_basic_map_gauss(bmap
, NULL
);
1259 return isl_basic_map_finalize(bmap
);
1261 isl_basic_map_free(bmap
);
1265 static __isl_give isl_basic_set
*isl_basic_set_swap_vars(
1266 __isl_take isl_basic_set
*bset
, unsigned n
)
1271 nparam
= isl_basic_set_n_param(bset
);
1272 dim
= isl_basic_set_n_dim(bset
);
1273 isl_assert(bset
->ctx
, n
<= dim
, goto error
);
1275 return isl_basic_map_swap_vars(bset
, 1 + nparam
, n
, dim
- n
);
1277 isl_basic_set_free(bset
);
1281 static __isl_give isl_set
*isl_set_swap_vars(__isl_take isl_set
*set
,
1285 set
= isl_set_cow(set
);
1289 for (i
= 0; i
< set
->n
; ++i
) {
1290 set
->p
[i
] = isl_basic_set_swap_vars(set
->p
[i
], n
);
1296 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
1300 struct isl_basic_map
*isl_basic_map_set_to_empty(struct isl_basic_map
*bmap
)
1306 total
= isl_basic_map_total_dim(bmap
);
1307 isl_basic_map_free_div(bmap
, bmap
->n_div
);
1308 isl_basic_map_free_inequality(bmap
, bmap
->n_ineq
);
1310 isl_basic_map_free_equality(bmap
, bmap
->n_eq
-1);
1312 i
= isl_basic_map_alloc_equality(bmap
);
1316 isl_int_set_si(bmap
->eq
[i
][0], 1);
1317 isl_seq_clr(bmap
->eq
[i
]+1, total
);
1318 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
1319 isl_vec_free(bmap
->sample
);
1320 bmap
->sample
= NULL
;
1321 return isl_basic_map_finalize(bmap
);
1323 isl_basic_map_free(bmap
);
1327 struct isl_basic_set
*isl_basic_set_set_to_empty(struct isl_basic_set
*bset
)
1329 return (struct isl_basic_set
*)
1330 isl_basic_map_set_to_empty((struct isl_basic_map
*)bset
);
1333 void isl_basic_map_swap_div(struct isl_basic_map
*bmap
, int a
, int b
)
1336 unsigned off
= isl_dim_total(bmap
->dim
);
1337 isl_int
*t
= bmap
->div
[a
];
1338 bmap
->div
[a
] = bmap
->div
[b
];
1341 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1342 isl_int_swap(bmap
->eq
[i
][1+off
+a
], bmap
->eq
[i
][1+off
+b
]);
1344 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1345 isl_int_swap(bmap
->ineq
[i
][1+off
+a
], bmap
->ineq
[i
][1+off
+b
]);
1347 for (i
= 0; i
< bmap
->n_div
; ++i
)
1348 isl_int_swap(bmap
->div
[i
][1+1+off
+a
], bmap
->div
[i
][1+1+off
+b
]);
1349 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1352 /* Eliminate the specified n dimensions starting at first from the
1353 * constraints using Fourier-Motzkin. The dimensions themselves
1356 __isl_give isl_map
*isl_map_eliminate(__isl_take isl_map
*map
,
1357 enum isl_dim_type type
, unsigned first
, unsigned n
)
1366 map
= isl_map_cow(map
);
1369 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
1370 first
+= pos(map
->dim
, type
) - 1;
1372 for (i
= 0; i
< map
->n
; ++i
) {
1373 map
->p
[i
] = isl_basic_map_eliminate_vars(map
->p
[i
], first
, n
);
1383 /* Eliminate the specified n dimensions starting at first from the
1384 * constraints using Fourier-Motzkin. The dimensions themselves
1387 __isl_give isl_set
*isl_set_eliminate(__isl_take isl_set
*set
,
1388 enum isl_dim_type type
, unsigned first
, unsigned n
)
1390 return (isl_set
*)isl_map_eliminate((isl_map
*)set
, type
, first
, n
);
1393 /* Eliminate the specified n dimensions starting at first from the
1394 * constraints using Fourier-Motzkin. The dimensions themselves
1397 __isl_give isl_set
*isl_set_eliminate_dims(__isl_take isl_set
*set
,
1398 unsigned first
, unsigned n
)
1400 return isl_set_eliminate(set
, isl_dim_set
, first
, n
);
1403 __isl_give isl_basic_map
*isl_basic_map_remove_divs(
1404 __isl_take isl_basic_map
*bmap
)
1406 bmap
= isl_basic_map_eliminate_vars(bmap
, isl_dim_total(bmap
->dim
),
1411 return isl_basic_map_finalize(bmap
);
1414 __isl_give isl_basic_set
*isl_basic_set_remove_divs(
1415 __isl_take isl_basic_set
*bset
)
1417 return (struct isl_basic_set
*)isl_basic_map_remove_divs(
1418 (struct isl_basic_map
*)bset
);
1421 __isl_give isl_map
*isl_map_remove_divs(__isl_take isl_map
*map
)
1430 map
= isl_map_cow(map
);
1434 for (i
= 0; i
< map
->n
; ++i
) {
1435 map
->p
[i
] = isl_basic_map_remove_divs(map
->p
[i
]);
1445 __isl_give isl_set
*isl_set_remove_divs(__isl_take isl_set
*set
)
1447 return isl_map_remove_divs(set
);
1450 struct isl_basic_map
*isl_basic_map_remove_dims(struct isl_basic_map
*bmap
,
1451 enum isl_dim_type type
, unsigned first
, unsigned n
)
1455 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
1459 bmap
= isl_basic_map_eliminate_vars(bmap
,
1460 isl_basic_map_offset(bmap
, type
) - 1 + first
, n
);
1463 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
) && type
== isl_dim_div
)
1465 bmap
= isl_basic_map_drop(bmap
, type
, first
, n
);
1468 isl_basic_map_free(bmap
);
1472 /* Return true if the definition of the given div (recursively) involves
1473 * any of the given variables.
1475 static int div_involves_vars(__isl_keep isl_basic_map
*bmap
, int div
,
1476 unsigned first
, unsigned n
)
1479 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
1481 if (isl_int_is_zero(bmap
->div
[div
][0]))
1483 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + first
, n
) >= 0)
1486 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
1487 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
1489 if (div_involves_vars(bmap
, i
, first
, n
))
1496 /* Remove all divs (recursively) involving any of the given dimensions
1497 * in their definitions.
1499 __isl_give isl_basic_map
*isl_basic_map_remove_divs_involving_dims(
1500 __isl_take isl_basic_map
*bmap
,
1501 enum isl_dim_type type
, unsigned first
, unsigned n
)
1507 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
1509 first
+= isl_basic_map_offset(bmap
, type
);
1511 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
1512 if (!div_involves_vars(bmap
, i
, first
, n
))
1514 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
1519 isl_basic_map_free(bmap
);
1523 __isl_give isl_map
*isl_map_remove_divs_involving_dims(__isl_take isl_map
*map
,
1524 enum isl_dim_type type
, unsigned first
, unsigned n
)
1533 map
= isl_map_cow(map
);
1537 for (i
= 0; i
< map
->n
; ++i
) {
1538 map
->p
[i
] = isl_basic_map_remove_divs_involving_dims(map
->p
[i
],
1549 __isl_give isl_set
*isl_set_remove_divs_involving_dims(__isl_take isl_set
*set
,
1550 enum isl_dim_type type
, unsigned first
, unsigned n
)
1552 return (isl_set
*)isl_map_remove_divs_involving_dims((isl_map
*)set
,
1556 int isl_basic_set_involves_dims(__isl_keep isl_basic_set
*bset
,
1557 enum isl_dim_type type
, unsigned first
, unsigned n
)
1564 if (first
+ n
> isl_basic_set_dim(bset
, type
))
1565 isl_die(bset
->ctx
, isl_error_invalid
,
1566 "index out of bounds", return -1);
1568 first
+= isl_basic_set_offset(bset
, type
);
1569 for (i
= 0; i
< bset
->n_eq
; ++i
)
1570 if (isl_seq_first_non_zero(bset
->eq
[i
] + first
, n
) >= 0)
1572 for (i
= 0; i
< bset
->n_ineq
; ++i
)
1573 if (isl_seq_first_non_zero(bset
->ineq
[i
] + first
, n
) >= 0)
1579 int isl_set_involves_dims(__isl_keep isl_set
*set
,
1580 enum isl_dim_type type
, unsigned first
, unsigned n
)
1587 if (first
+ n
> isl_set_dim(set
, type
))
1588 isl_die(set
->ctx
, isl_error_invalid
,
1589 "index out of bounds", return -1);
1591 for (i
= 0; i
< set
->n
; ++i
) {
1592 int involves
= isl_basic_set_involves_dims(set
->p
[i
],
1594 if (involves
< 0 || !involves
)
1601 /* Return true if the definition of the given div is unknown or depends
1604 static int div_is_unknown(__isl_keep isl_basic_map
*bmap
, int div
)
1607 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
1609 if (isl_int_is_zero(bmap
->div
[div
][0]))
1612 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
1613 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
1615 if (div_is_unknown(bmap
, i
))
1622 /* Remove all divs that are unknown or defined in terms of unknown divs.
1624 __isl_give isl_basic_map
*isl_basic_map_remove_unknown_divs(
1625 __isl_take isl_basic_map
*bmap
)
1632 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
1633 if (!div_is_unknown(bmap
, i
))
1635 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
1641 __isl_give isl_map
*isl_map_remove_unknown_divs(__isl_take isl_map
*map
)
1650 map
= isl_map_cow(map
);
1654 for (i
= 0; i
< map
->n
; ++i
) {
1655 map
->p
[i
] = isl_basic_map_remove_unknown_divs(map
->p
[i
]);
1665 __isl_give isl_set
*isl_set_remove_unknown_divs(__isl_take isl_set
*set
)
1667 return (isl_set
*)isl_map_remove_unknown_divs((isl_map
*)set
);
1670 __isl_give isl_basic_set
*isl_basic_set_remove_dims(
1671 __isl_take isl_basic_set
*bset
,
1672 enum isl_dim_type type
, unsigned first
, unsigned n
)
1674 return (isl_basic_set
*)
1675 isl_basic_map_remove_dims((isl_basic_map
*)bset
, type
, first
, n
);
1678 struct isl_map
*isl_map_remove_dims(struct isl_map
*map
,
1679 enum isl_dim_type type
, unsigned first
, unsigned n
)
1686 map
= isl_map_cow(map
);
1689 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
1691 for (i
= 0; i
< map
->n
; ++i
) {
1692 map
->p
[i
] = isl_basic_map_eliminate_vars(map
->p
[i
],
1693 isl_basic_map_offset(map
->p
[i
], type
) - 1 + first
, n
);
1697 map
= isl_map_drop(map
, type
, first
, n
);
1704 __isl_give isl_set
*isl_set_remove_dims(__isl_take isl_set
*bset
,
1705 enum isl_dim_type type
, unsigned first
, unsigned n
)
1707 return (isl_set
*)isl_map_remove_dims((isl_map
*)bset
, type
, first
, n
);
1710 /* Project out n inputs starting at first using Fourier-Motzkin */
1711 struct isl_map
*isl_map_remove_inputs(struct isl_map
*map
,
1712 unsigned first
, unsigned n
)
1714 return isl_map_remove_dims(map
, isl_dim_in
, first
, n
);
1717 static void dump_term(struct isl_basic_map
*bmap
,
1718 isl_int c
, int pos
, FILE *out
)
1721 unsigned in
= isl_basic_map_n_in(bmap
);
1722 unsigned dim
= in
+ isl_basic_map_n_out(bmap
);
1723 unsigned nparam
= isl_basic_map_n_param(bmap
);
1725 isl_int_print(out
, c
, 0);
1727 if (!isl_int_is_one(c
))
1728 isl_int_print(out
, c
, 0);
1729 if (pos
< 1 + nparam
) {
1730 name
= isl_dim_get_name(bmap
->dim
,
1731 isl_dim_param
, pos
- 1);
1733 fprintf(out
, "%s", name
);
1735 fprintf(out
, "p%d", pos
- 1);
1736 } else if (pos
< 1 + nparam
+ in
)
1737 fprintf(out
, "i%d", pos
- 1 - nparam
);
1738 else if (pos
< 1 + nparam
+ dim
)
1739 fprintf(out
, "o%d", pos
- 1 - nparam
- in
);
1741 fprintf(out
, "e%d", pos
- 1 - nparam
- dim
);
1745 static void dump_constraint_sign(struct isl_basic_map
*bmap
, isl_int
*c
,
1746 int sign
, FILE *out
)
1750 unsigned len
= 1 + isl_basic_map_total_dim(bmap
);
1754 for (i
= 0, first
= 1; i
< len
; ++i
) {
1755 if (isl_int_sgn(c
[i
]) * sign
<= 0)
1758 fprintf(out
, " + ");
1760 isl_int_abs(v
, c
[i
]);
1761 dump_term(bmap
, v
, i
, out
);
1768 static void dump_constraint(struct isl_basic_map
*bmap
, isl_int
*c
,
1769 const char *op
, FILE *out
, int indent
)
1773 fprintf(out
, "%*s", indent
, "");
1775 dump_constraint_sign(bmap
, c
, 1, out
);
1776 fprintf(out
, " %s ", op
);
1777 dump_constraint_sign(bmap
, c
, -1, out
);
1781 for (i
= bmap
->n_div
; i
< bmap
->extra
; ++i
) {
1782 if (isl_int_is_zero(c
[1+isl_dim_total(bmap
->dim
)+i
]))
1784 fprintf(out
, "%*s", indent
, "");
1785 fprintf(out
, "ERROR: unused div coefficient not zero\n");
1790 static void dump_constraints(struct isl_basic_map
*bmap
,
1791 isl_int
**c
, unsigned n
,
1792 const char *op
, FILE *out
, int indent
)
1796 for (i
= 0; i
< n
; ++i
)
1797 dump_constraint(bmap
, c
[i
], op
, out
, indent
);
1800 static void dump_affine(struct isl_basic_map
*bmap
, isl_int
*exp
, FILE *out
)
1804 unsigned total
= isl_basic_map_total_dim(bmap
);
1806 for (j
= 0; j
< 1 + total
; ++j
) {
1807 if (isl_int_is_zero(exp
[j
]))
1809 if (!first
&& isl_int_is_pos(exp
[j
]))
1811 dump_term(bmap
, exp
[j
], j
, out
);
1816 static void dump(struct isl_basic_map
*bmap
, FILE *out
, int indent
)
1820 dump_constraints(bmap
, bmap
->eq
, bmap
->n_eq
, "=", out
, indent
);
1821 dump_constraints(bmap
, bmap
->ineq
, bmap
->n_ineq
, ">=", out
, indent
);
1823 for (i
= 0; i
< bmap
->n_div
; ++i
) {
1824 fprintf(out
, "%*s", indent
, "");
1825 fprintf(out
, "e%d = [(", i
);
1826 dump_affine(bmap
, bmap
->div
[i
]+1, out
);
1828 isl_int_print(out
, bmap
->div
[i
][0], 0);
1829 fprintf(out
, "]\n");
1833 void isl_basic_set_print_internal(struct isl_basic_set
*bset
,
1834 FILE *out
, int indent
)
1837 fprintf(out
, "null basic set\n");
1841 fprintf(out
, "%*s", indent
, "");
1842 fprintf(out
, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
1843 bset
->ref
, bset
->dim
->nparam
, bset
->dim
->n_out
,
1844 bset
->extra
, bset
->flags
);
1845 dump((struct isl_basic_map
*)bset
, out
, indent
);
1848 void isl_basic_map_print_internal(struct isl_basic_map
*bmap
,
1849 FILE *out
, int indent
)
1852 fprintf(out
, "null basic map\n");
1856 fprintf(out
, "%*s", indent
, "");
1857 fprintf(out
, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
1858 "flags: %x, n_name: %d\n",
1860 bmap
->dim
->nparam
, bmap
->dim
->n_in
, bmap
->dim
->n_out
,
1861 bmap
->extra
, bmap
->flags
, bmap
->dim
->n_name
);
1862 dump(bmap
, out
, indent
);
1865 int isl_inequality_negate(struct isl_basic_map
*bmap
, unsigned pos
)
1870 total
= isl_basic_map_total_dim(bmap
);
1871 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
1872 isl_seq_neg(bmap
->ineq
[pos
], bmap
->ineq
[pos
], 1 + total
);
1873 isl_int_sub_ui(bmap
->ineq
[pos
][0], bmap
->ineq
[pos
][0], 1);
1874 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1878 struct isl_set
*isl_set_alloc_dim(struct isl_dim
*dim
, int n
, unsigned flags
)
1880 struct isl_set
*set
;
1884 isl_assert(dim
->ctx
, dim
->n_in
== 0, goto error
);
1885 isl_assert(dim
->ctx
, n
>= 0, goto error
);
1886 set
= isl_alloc(dim
->ctx
, struct isl_set
,
1887 sizeof(struct isl_set
) +
1888 (n
- 1) * sizeof(struct isl_basic_set
*));
1892 set
->ctx
= dim
->ctx
;
1893 isl_ctx_ref(set
->ctx
);
1905 struct isl_set
*isl_set_alloc(struct isl_ctx
*ctx
,
1906 unsigned nparam
, unsigned dim
, int n
, unsigned flags
)
1908 struct isl_set
*set
;
1909 struct isl_dim
*dims
;
1911 dims
= isl_dim_alloc(ctx
, nparam
, 0, dim
);
1915 set
= isl_set_alloc_dim(dims
, n
, flags
);
1919 /* Make sure "map" has room for at least "n" more basic maps.
1921 struct isl_map
*isl_map_grow(struct isl_map
*map
, int n
)
1924 struct isl_map
*grown
= NULL
;
1928 isl_assert(map
->ctx
, n
>= 0, goto error
);
1929 if (map
->n
+ n
<= map
->size
)
1931 grown
= isl_map_alloc_dim(isl_map_get_dim(map
), map
->n
+ n
, map
->flags
);
1934 for (i
= 0; i
< map
->n
; ++i
) {
1935 grown
->p
[i
] = isl_basic_map_copy(map
->p
[i
]);
1943 isl_map_free(grown
);
1948 /* Make sure "set" has room for at least "n" more basic sets.
1950 struct isl_set
*isl_set_grow(struct isl_set
*set
, int n
)
1952 return (struct isl_set
*)isl_map_grow((struct isl_map
*)set
, n
);
1955 struct isl_set
*isl_set_dup(struct isl_set
*set
)
1958 struct isl_set
*dup
;
1963 dup
= isl_set_alloc_dim(isl_dim_copy(set
->dim
), set
->n
, set
->flags
);
1966 for (i
= 0; i
< set
->n
; ++i
)
1967 dup
= isl_set_add_basic_set(dup
, isl_basic_set_copy(set
->p
[i
]));
1971 struct isl_set
*isl_set_from_basic_set(struct isl_basic_set
*bset
)
1973 return isl_map_from_basic_map(bset
);
1976 struct isl_map
*isl_map_from_basic_map(struct isl_basic_map
*bmap
)
1978 struct isl_map
*map
;
1983 map
= isl_map_alloc_dim(isl_dim_copy(bmap
->dim
), 1, ISL_MAP_DISJOINT
);
1984 return isl_map_add_basic_map(map
, bmap
);
1987 __isl_give isl_set
*isl_set_add_basic_set(__isl_take isl_set
*set
,
1988 __isl_take isl_basic_set
*bset
)
1990 return (struct isl_set
*)isl_map_add_basic_map((struct isl_map
*)set
,
1991 (struct isl_basic_map
*)bset
);
1994 void isl_set_free(struct isl_set
*set
)
2004 isl_ctx_deref(set
->ctx
);
2005 for (i
= 0; i
< set
->n
; ++i
)
2006 isl_basic_set_free(set
->p
[i
]);
2007 isl_dim_free(set
->dim
);
2011 void isl_set_print_internal(struct isl_set
*set
, FILE *out
, int indent
)
2016 fprintf(out
, "null set\n");
2020 fprintf(out
, "%*s", indent
, "");
2021 fprintf(out
, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
2022 set
->ref
, set
->n
, set
->dim
->nparam
, set
->dim
->n_out
,
2024 for (i
= 0; i
< set
->n
; ++i
) {
2025 fprintf(out
, "%*s", indent
, "");
2026 fprintf(out
, "basic set %d:\n", i
);
2027 isl_basic_set_print_internal(set
->p
[i
], out
, indent
+4);
2031 void isl_map_print_internal(struct isl_map
*map
, FILE *out
, int indent
)
2036 fprintf(out
, "null map\n");
2040 fprintf(out
, "%*s", indent
, "");
2041 fprintf(out
, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2042 "flags: %x, n_name: %d\n",
2043 map
->ref
, map
->n
, map
->dim
->nparam
, map
->dim
->n_in
,
2044 map
->dim
->n_out
, map
->flags
, map
->dim
->n_name
);
2045 for (i
= 0; i
< map
->n
; ++i
) {
2046 fprintf(out
, "%*s", indent
, "");
2047 fprintf(out
, "basic map %d:\n", i
);
2048 isl_basic_map_print_internal(map
->p
[i
], out
, indent
+4);
2052 struct isl_basic_map
*isl_basic_map_intersect_domain(
2053 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
2055 struct isl_basic_map
*bmap_domain
;
2060 isl_assert(bset
->ctx
, isl_dim_match(bmap
->dim
, isl_dim_param
,
2061 bset
->dim
, isl_dim_param
), goto error
);
2063 if (isl_dim_size(bset
->dim
, isl_dim_set
) != 0)
2064 isl_assert(bset
->ctx
,
2065 isl_basic_map_compatible_domain(bmap
, bset
), goto error
);
2067 bmap
= isl_basic_map_cow(bmap
);
2070 bmap
= isl_basic_map_extend_dim(bmap
, isl_dim_copy(bmap
->dim
),
2071 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
2072 bmap_domain
= isl_basic_map_from_domain(bset
);
2073 bmap
= add_constraints(bmap
, bmap_domain
, 0, 0);
2075 bmap
= isl_basic_map_simplify(bmap
);
2076 return isl_basic_map_finalize(bmap
);
2078 isl_basic_map_free(bmap
);
2079 isl_basic_set_free(bset
);
2083 struct isl_basic_map
*isl_basic_map_intersect_range(
2084 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
2086 struct isl_basic_map
*bmap_range
;
2091 isl_assert(bset
->ctx
, isl_dim_match(bmap
->dim
, isl_dim_param
,
2092 bset
->dim
, isl_dim_param
), goto error
);
2094 if (isl_dim_size(bset
->dim
, isl_dim_set
) != 0)
2095 isl_assert(bset
->ctx
,
2096 isl_basic_map_compatible_range(bmap
, bset
), goto error
);
2098 if (isl_basic_set_is_universe(bset
)) {
2099 isl_basic_set_free(bset
);
2103 bmap
= isl_basic_map_cow(bmap
);
2106 bmap
= isl_basic_map_extend_dim(bmap
, isl_dim_copy(bmap
->dim
),
2107 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
2108 bmap_range
= isl_basic_map_from_basic_set(bset
, isl_dim_copy(bset
->dim
));
2109 bmap
= add_constraints(bmap
, bmap_range
, 0, 0);
2111 bmap
= isl_basic_map_simplify(bmap
);
2112 return isl_basic_map_finalize(bmap
);
2114 isl_basic_map_free(bmap
);
2115 isl_basic_set_free(bset
);
2119 int isl_basic_map_contains(struct isl_basic_map
*bmap
, struct isl_vec
*vec
)
2125 total
= 1 + isl_basic_map_total_dim(bmap
);
2126 if (total
!= vec
->size
)
2131 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
2132 isl_seq_inner_product(vec
->el
, bmap
->eq
[i
], total
, &s
);
2133 if (!isl_int_is_zero(s
)) {
2139 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
2140 isl_seq_inner_product(vec
->el
, bmap
->ineq
[i
], total
, &s
);
2141 if (isl_int_is_neg(s
)) {
2152 int isl_basic_set_contains(struct isl_basic_set
*bset
, struct isl_vec
*vec
)
2154 return isl_basic_map_contains((struct isl_basic_map
*)bset
, vec
);
2157 struct isl_basic_map
*isl_basic_map_intersect(
2158 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
2160 struct isl_vec
*sample
= NULL
;
2162 if (!bmap1
|| !bmap2
)
2165 isl_assert(bmap1
->ctx
, isl_dim_match(bmap1
->dim
, isl_dim_param
,
2166 bmap2
->dim
, isl_dim_param
), goto error
);
2167 if (isl_dim_total(bmap1
->dim
) ==
2168 isl_dim_size(bmap1
->dim
, isl_dim_param
) &&
2169 isl_dim_total(bmap2
->dim
) !=
2170 isl_dim_size(bmap2
->dim
, isl_dim_param
))
2171 return isl_basic_map_intersect(bmap2
, bmap1
);
2173 if (isl_dim_total(bmap2
->dim
) !=
2174 isl_dim_size(bmap2
->dim
, isl_dim_param
))
2175 isl_assert(bmap1
->ctx
,
2176 isl_dim_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
2178 if (bmap1
->sample
&&
2179 isl_basic_map_contains(bmap1
, bmap1
->sample
) > 0 &&
2180 isl_basic_map_contains(bmap2
, bmap1
->sample
) > 0)
2181 sample
= isl_vec_copy(bmap1
->sample
);
2182 else if (bmap2
->sample
&&
2183 isl_basic_map_contains(bmap1
, bmap2
->sample
) > 0 &&
2184 isl_basic_map_contains(bmap2
, bmap2
->sample
) > 0)
2185 sample
= isl_vec_copy(bmap2
->sample
);
2187 bmap1
= isl_basic_map_cow(bmap1
);
2190 bmap1
= isl_basic_map_extend_dim(bmap1
, isl_dim_copy(bmap1
->dim
),
2191 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
2192 bmap1
= add_constraints(bmap1
, bmap2
, 0, 0);
2195 isl_vec_free(sample
);
2197 isl_vec_free(bmap1
->sample
);
2198 bmap1
->sample
= sample
;
2201 bmap1
= isl_basic_map_simplify(bmap1
);
2202 return isl_basic_map_finalize(bmap1
);
2205 isl_vec_free(sample
);
2206 isl_basic_map_free(bmap1
);
2207 isl_basic_map_free(bmap2
);
2211 struct isl_basic_set
*isl_basic_set_intersect(
2212 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
2214 return (struct isl_basic_set
*)
2215 isl_basic_map_intersect(
2216 (struct isl_basic_map
*)bset1
,
2217 (struct isl_basic_map
*)bset2
);
2220 /* Special case of isl_map_intersect, where both map1 and map2
2221 * are convex, without any divs and such that either map1 or map2
2222 * contains a single constraint. This constraint is then simply
2223 * added to the other map.
2225 static __isl_give isl_map
*map_intersect_add_constraint(
2226 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
2228 struct isl_basic_map
*bmap1
;
2229 struct isl_basic_map
*bmap2
;
2231 isl_assert(map1
->ctx
, map1
->n
== 1, goto error
);
2232 isl_assert(map2
->ctx
, map1
->n
== 1, goto error
);
2233 isl_assert(map1
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
2234 isl_assert(map2
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
2236 if (map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
!= 1)
2237 return isl_map_intersect(map2
, map1
);
2239 isl_assert(map2
->ctx
,
2240 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1, goto error
);
2242 map1
= isl_map_cow(map1
);
2245 if (isl_map_plain_is_empty(map1
)) {
2249 map1
->p
[0] = isl_basic_map_cow(map1
->p
[0]);
2250 if (map2
->p
[0]->n_eq
== 1)
2251 map1
->p
[0] = isl_basic_map_add_eq(map1
->p
[0], map2
->p
[0]->eq
[0]);
2253 map1
->p
[0] = isl_basic_map_add_ineq(map1
->p
[0],
2254 map2
->p
[0]->ineq
[0]);
2256 map1
->p
[0] = isl_basic_map_simplify(map1
->p
[0]);
2257 map1
->p
[0] = isl_basic_map_finalize(map1
->p
[0]);
2261 if (isl_basic_map_plain_is_empty(map1
->p
[0])) {
2262 isl_basic_map_free(map1
->p
[0]);
2275 struct isl_map
*isl_map_intersect(struct isl_map
*map1
, struct isl_map
*map2
)
2278 struct isl_map
*result
;
2284 if (isl_map_plain_is_empty(map1
)) {
2288 if (isl_map_plain_is_empty(map2
)) {
2293 if (map1
->n
== 1 && map2
->n
== 1 &&
2294 map1
->p
[0]->n_div
== 0 && map2
->p
[0]->n_div
== 0 &&
2295 isl_dim_equal(map1
->dim
, map2
->dim
) &&
2296 (map1
->p
[0]->n_eq
+ map1
->p
[0]->n_ineq
== 1 ||
2297 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1))
2298 return map_intersect_add_constraint(map1
, map2
);
2299 isl_assert(map1
->ctx
, isl_dim_match(map1
->dim
, isl_dim_param
,
2300 map2
->dim
, isl_dim_param
), goto error
);
2301 if (isl_dim_total(map1
->dim
) ==
2302 isl_dim_size(map1
->dim
, isl_dim_param
) &&
2303 isl_dim_total(map2
->dim
) != isl_dim_size(map2
->dim
, isl_dim_param
))
2304 return isl_map_intersect(map2
, map1
);
2306 if (isl_dim_total(map2
->dim
) != isl_dim_size(map2
->dim
, isl_dim_param
))
2307 isl_assert(map1
->ctx
,
2308 isl_dim_equal(map1
->dim
, map2
->dim
), goto error
);
2310 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
2311 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
2312 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
2314 result
= isl_map_alloc_dim(isl_dim_copy(map1
->dim
),
2315 map1
->n
* map2
->n
, flags
);
2318 for (i
= 0; i
< map1
->n
; ++i
)
2319 for (j
= 0; j
< map2
->n
; ++j
) {
2320 struct isl_basic_map
*part
;
2321 part
= isl_basic_map_intersect(
2322 isl_basic_map_copy(map1
->p
[i
]),
2323 isl_basic_map_copy(map2
->p
[j
]));
2324 if (isl_basic_map_is_empty(part
))
2325 isl_basic_map_free(part
);
2327 result
= isl_map_add_basic_map(result
, part
);
2340 struct isl_set
*isl_set_intersect(struct isl_set
*set1
, struct isl_set
*set2
)
2342 return (struct isl_set
*)
2343 isl_map_intersect((struct isl_map
*)set1
,
2344 (struct isl_map
*)set2
);
2347 struct isl_basic_map
*isl_basic_map_reverse(struct isl_basic_map
*bmap
)
2349 struct isl_dim
*dim
;
2350 struct isl_basic_set
*bset
;
2355 bmap
= isl_basic_map_cow(bmap
);
2358 dim
= isl_dim_reverse(isl_dim_copy(bmap
->dim
));
2359 in
= isl_basic_map_n_in(bmap
);
2360 bset
= isl_basic_set_from_basic_map(bmap
);
2361 bset
= isl_basic_set_swap_vars(bset
, in
);
2362 return isl_basic_map_from_basic_set(bset
, dim
);
2365 __isl_give isl_basic_map
*isl_basic_map_insert(__isl_take isl_basic_map
*bmap
,
2366 enum isl_dim_type type
, unsigned pos
, unsigned n
)
2368 struct isl_dim
*res_dim
;
2369 struct isl_basic_map
*res
;
2370 struct isl_dim_map
*dim_map
;
2371 unsigned total
, off
;
2372 enum isl_dim_type t
;
2380 res_dim
= isl_dim_insert(isl_basic_map_get_dim(bmap
), type
, pos
, n
);
2382 total
= isl_basic_map_total_dim(bmap
) + n
;
2383 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
2385 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
2387 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
2389 unsigned size
= isl_basic_map_dim(bmap
, t
);
2390 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2392 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2393 pos
, size
- pos
, off
+ pos
+ n
);
2395 off
+= isl_dim_size(res_dim
, t
);
2397 isl_dim_map_div(dim_map
, bmap
, off
);
2399 res
= isl_basic_map_alloc_dim(res_dim
,
2400 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
2401 if (isl_basic_map_is_rational(bmap
))
2402 res
= isl_basic_map_set_rational(res
);
2403 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
2404 return isl_basic_map_finalize(res
);
2407 __isl_give isl_basic_map
*isl_basic_map_add(__isl_take isl_basic_map
*bmap
,
2408 enum isl_dim_type type
, unsigned n
)
2412 return isl_basic_map_insert(bmap
, type
,
2413 isl_basic_map_dim(bmap
, type
), n
);
2416 __isl_give isl_basic_set
*isl_basic_set_add(__isl_take isl_basic_set
*bset
,
2417 enum isl_dim_type type
, unsigned n
)
2421 isl_assert(bset
->ctx
, type
!= isl_dim_in
, goto error
);
2422 return (isl_basic_set
*)isl_basic_map_add((isl_basic_map
*)bset
, type
, n
);
2424 isl_basic_set_free(bset
);
2428 __isl_give isl_map
*isl_map_insert(__isl_take isl_map
*map
,
2429 enum isl_dim_type type
, unsigned pos
, unsigned n
)
2436 map
= isl_map_cow(map
);
2440 map
->dim
= isl_dim_insert(map
->dim
, type
, pos
, n
);
2444 for (i
= 0; i
< map
->n
; ++i
) {
2445 map
->p
[i
] = isl_basic_map_insert(map
->p
[i
], type
, pos
, n
);
2456 __isl_give isl_set
*isl_set_insert(__isl_take isl_set
*set
,
2457 enum isl_dim_type type
, unsigned pos
, unsigned n
)
2459 return (isl_set
*)isl_map_insert((isl_map
*)set
, type
, pos
, n
);
2462 __isl_give isl_map
*isl_map_add_dims(__isl_take isl_map
*map
,
2463 enum isl_dim_type type
, unsigned n
)
2467 return isl_map_insert(map
, type
, isl_map_dim(map
, type
), n
);
2470 __isl_give isl_set
*isl_set_add_dims(__isl_take isl_set
*set
,
2471 enum isl_dim_type type
, unsigned n
)
2475 isl_assert(set
->ctx
, type
!= isl_dim_in
, goto error
);
2476 return (isl_set
*)isl_map_add_dims((isl_map
*)set
, type
, n
);
2482 __isl_give isl_basic_map
*isl_basic_map_move_dims(
2483 __isl_take isl_basic_map
*bmap
,
2484 enum isl_dim_type dst_type
, unsigned dst_pos
,
2485 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2488 struct isl_dim_map
*dim_map
;
2489 struct isl_basic_map
*res
;
2490 enum isl_dim_type t
;
2491 unsigned total
, off
;
2498 isl_assert(bmap
->ctx
, src_pos
+ n
<= isl_basic_map_dim(bmap
, src_type
),
2501 if (dst_type
== src_type
&& dst_pos
== src_pos
)
2504 isl_assert(bmap
->ctx
, dst_type
!= src_type
, goto error
);
2506 if (pos(bmap
->dim
, dst_type
) + dst_pos
==
2507 pos(bmap
->dim
, src_type
) + src_pos
+
2508 ((src_type
< dst_type
) ? n
: 0)) {
2509 bmap
= isl_basic_map_cow(bmap
);
2513 bmap
->dim
= isl_dim_move(bmap
->dim
, dst_type
, dst_pos
,
2514 src_type
, src_pos
, n
);
2518 bmap
= isl_basic_map_finalize(bmap
);
2523 total
= isl_basic_map_total_dim(bmap
);
2524 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
2527 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
2528 unsigned size
= isl_dim_size(bmap
->dim
, t
);
2529 if (t
== dst_type
) {
2530 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2533 isl_dim_map_dim_range(dim_map
, bmap
->dim
, src_type
,
2536 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2537 dst_pos
, size
- dst_pos
, off
);
2538 off
+= size
- dst_pos
;
2539 } else if (t
== src_type
) {
2540 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2543 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2544 src_pos
+ n
, size
- src_pos
- n
, off
);
2545 off
+= size
- src_pos
- n
;
2547 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
2551 isl_dim_map_div(dim_map
, bmap
, off
);
2553 res
= isl_basic_map_alloc_dim(isl_basic_map_get_dim(bmap
),
2554 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
2555 bmap
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
2557 bmap
->dim
= isl_dim_move(bmap
->dim
, dst_type
, dst_pos
,
2558 src_type
, src_pos
, n
);
2562 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
2563 bmap
= isl_basic_map_gauss(bmap
, NULL
);
2564 bmap
= isl_basic_map_finalize(bmap
);
2568 isl_basic_map_free(bmap
);
2572 __isl_give isl_basic_set
*isl_basic_set_move_dims(__isl_take isl_basic_set
*bset
,
2573 enum isl_dim_type dst_type
, unsigned dst_pos
,
2574 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2576 return (isl_basic_set
*)isl_basic_map_move_dims(
2577 (isl_basic_map
*)bset
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
2580 __isl_give isl_set
*isl_set_move_dims(__isl_take isl_set
*set
,
2581 enum isl_dim_type dst_type
, unsigned dst_pos
,
2582 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2586 isl_assert(set
->ctx
, dst_type
!= isl_dim_in
, goto error
);
2587 return (isl_set
*)isl_map_move_dims((isl_map
*)set
, dst_type
, dst_pos
,
2588 src_type
, src_pos
, n
);
2594 __isl_give isl_map
*isl_map_move_dims(__isl_take isl_map
*map
,
2595 enum isl_dim_type dst_type
, unsigned dst_pos
,
2596 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2605 isl_assert(map
->ctx
, src_pos
+ n
<= isl_map_dim(map
, src_type
),
2608 if (dst_type
== src_type
&& dst_pos
== src_pos
)
2611 isl_assert(map
->ctx
, dst_type
!= src_type
, goto error
);
2613 map
= isl_map_cow(map
);
2617 map
->dim
= isl_dim_move(map
->dim
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
2621 for (i
= 0; i
< map
->n
; ++i
) {
2622 map
->p
[i
] = isl_basic_map_move_dims(map
->p
[i
],
2624 src_type
, src_pos
, n
);
2635 /* Move the specified dimensions to the last columns right before
2636 * the divs. Don't change the dimension specification of bmap.
2637 * That's the responsibility of the caller.
2639 static __isl_give isl_basic_map
*move_last(__isl_take isl_basic_map
*bmap
,
2640 enum isl_dim_type type
, unsigned first
, unsigned n
)
2642 struct isl_dim_map
*dim_map
;
2643 struct isl_basic_map
*res
;
2644 enum isl_dim_type t
;
2645 unsigned total
, off
;
2649 if (pos(bmap
->dim
, type
) + first
+ n
== 1 + isl_dim_total(bmap
->dim
))
2652 total
= isl_basic_map_total_dim(bmap
);
2653 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
2656 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
2657 unsigned size
= isl_dim_size(bmap
->dim
, t
);
2659 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2662 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2663 first
, n
, total
- bmap
->n_div
- n
);
2664 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2665 first
+ n
, size
- (first
+ n
), off
);
2666 off
+= size
- (first
+ n
);
2668 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
2672 isl_dim_map_div(dim_map
, bmap
, off
+ n
);
2674 res
= isl_basic_map_alloc_dim(isl_basic_map_get_dim(bmap
),
2675 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
2676 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
2680 /* Turn the n dimensions of type type, starting at first
2681 * into existentially quantified variables.
2683 __isl_give isl_basic_map
*isl_basic_map_project_out(
2684 __isl_take isl_basic_map
*bmap
,
2685 enum isl_dim_type type
, unsigned first
, unsigned n
)
2698 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
2699 return isl_basic_map_remove_dims(bmap
, type
, first
, n
);
2701 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
2704 bmap
= move_last(bmap
, type
, first
, n
);
2705 bmap
= isl_basic_map_cow(bmap
);
2709 row_size
= 1 + isl_dim_total(bmap
->dim
) + bmap
->extra
;
2710 old
= bmap
->block2
.data
;
2711 bmap
->block2
= isl_blk_extend(bmap
->ctx
, bmap
->block2
,
2712 (bmap
->extra
+ n
) * (1 + row_size
));
2713 if (!bmap
->block2
.data
)
2715 new_div
= isl_alloc_array(bmap
->ctx
, isl_int
*, bmap
->extra
+ n
);
2718 for (i
= 0; i
< n
; ++i
) {
2719 new_div
[i
] = bmap
->block2
.data
+
2720 (bmap
->extra
+ i
) * (1 + row_size
);
2721 isl_seq_clr(new_div
[i
], 1 + row_size
);
2723 for (i
= 0; i
< bmap
->extra
; ++i
)
2724 new_div
[n
+ i
] = bmap
->block2
.data
+ (bmap
->div
[i
] - old
);
2726 bmap
->div
= new_div
;
2730 bmap
->dim
= isl_dim_drop(bmap
->dim
, type
, first
, n
);
2733 bmap
= isl_basic_map_simplify(bmap
);
2734 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
2735 return isl_basic_map_finalize(bmap
);
2737 isl_basic_map_free(bmap
);
2741 /* Turn the n dimensions of type type, starting at first
2742 * into existentially quantified variables.
2744 struct isl_basic_set
*isl_basic_set_project_out(struct isl_basic_set
*bset
,
2745 enum isl_dim_type type
, unsigned first
, unsigned n
)
2747 return (isl_basic_set
*)isl_basic_map_project_out(
2748 (isl_basic_map
*)bset
, type
, first
, n
);
2751 /* Turn the n dimensions of type type, starting at first
2752 * into existentially quantified variables.
2754 __isl_give isl_map
*isl_map_project_out(__isl_take isl_map
*map
,
2755 enum isl_dim_type type
, unsigned first
, unsigned n
)
2765 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
2767 map
= isl_map_cow(map
);
2771 map
->dim
= isl_dim_drop(map
->dim
, type
, first
, n
);
2775 for (i
= 0; i
< map
->n
; ++i
) {
2776 map
->p
[i
] = isl_basic_map_project_out(map
->p
[i
], type
, first
, n
);
2787 /* Turn the n dimensions of type type, starting at first
2788 * into existentially quantified variables.
2790 __isl_give isl_set
*isl_set_project_out(__isl_take isl_set
*set
,
2791 enum isl_dim_type type
, unsigned first
, unsigned n
)
2793 return (isl_set
*)isl_map_project_out((isl_map
*)set
, type
, first
, n
);
2796 static struct isl_basic_map
*add_divs(struct isl_basic_map
*bmap
, unsigned n
)
2800 for (i
= 0; i
< n
; ++i
) {
2801 j
= isl_basic_map_alloc_div(bmap
);
2804 isl_seq_clr(bmap
->div
[j
], 1+1+isl_basic_map_total_dim(bmap
));
2808 isl_basic_map_free(bmap
);
2812 struct isl_basic_map
*isl_basic_map_apply_range(
2813 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
2815 struct isl_dim
*dim_result
= NULL
;
2816 struct isl_basic_map
*bmap
;
2817 unsigned n_in
, n_out
, n
, nparam
, total
, pos
;
2818 struct isl_dim_map
*dim_map1
, *dim_map2
;
2820 if (!bmap1
|| !bmap2
)
2823 dim_result
= isl_dim_join(isl_dim_copy(bmap1
->dim
),
2824 isl_dim_copy(bmap2
->dim
));
2826 n_in
= isl_basic_map_n_in(bmap1
);
2827 n_out
= isl_basic_map_n_out(bmap2
);
2828 n
= isl_basic_map_n_out(bmap1
);
2829 nparam
= isl_basic_map_n_param(bmap1
);
2831 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ n
;
2832 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
2833 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
2834 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
2835 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
2836 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
2837 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_in
);
2838 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_out
);
2839 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
2840 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
2841 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
2843 bmap
= isl_basic_map_alloc_dim(dim_result
,
2844 bmap1
->n_div
+ bmap2
->n_div
+ n
,
2845 bmap1
->n_eq
+ bmap2
->n_eq
,
2846 bmap1
->n_ineq
+ bmap2
->n_ineq
);
2847 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
2848 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
2849 bmap
= add_divs(bmap
, n
);
2850 bmap
= isl_basic_map_simplify(bmap
);
2851 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
2852 return isl_basic_map_finalize(bmap
);
2854 isl_basic_map_free(bmap1
);
2855 isl_basic_map_free(bmap2
);
2859 struct isl_basic_set
*isl_basic_set_apply(
2860 struct isl_basic_set
*bset
, struct isl_basic_map
*bmap
)
2865 isl_assert(bset
->ctx
, isl_basic_map_compatible_domain(bmap
, bset
),
2868 return (struct isl_basic_set
*)
2869 isl_basic_map_apply_range((struct isl_basic_map
*)bset
, bmap
);
2871 isl_basic_set_free(bset
);
2872 isl_basic_map_free(bmap
);
2876 struct isl_basic_map
*isl_basic_map_apply_domain(
2877 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
2879 if (!bmap1
|| !bmap2
)
2882 isl_assert(bmap1
->ctx
,
2883 isl_basic_map_n_in(bmap1
) == isl_basic_map_n_in(bmap2
), goto error
);
2884 isl_assert(bmap1
->ctx
,
2885 isl_basic_map_n_param(bmap1
) == isl_basic_map_n_param(bmap2
),
2888 bmap1
= isl_basic_map_reverse(bmap1
);
2889 bmap1
= isl_basic_map_apply_range(bmap1
, bmap2
);
2890 return isl_basic_map_reverse(bmap1
);
2892 isl_basic_map_free(bmap1
);
2893 isl_basic_map_free(bmap2
);
2897 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
2898 * A \cap B -> f(A) + f(B)
2900 struct isl_basic_map
*isl_basic_map_sum(
2901 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
2903 unsigned n_in
, n_out
, nparam
, total
, pos
;
2904 struct isl_basic_map
*bmap
= NULL
;
2905 struct isl_dim_map
*dim_map1
, *dim_map2
;
2908 if (!bmap1
|| !bmap2
)
2911 isl_assert(bmap1
->ctx
, isl_dim_equal(bmap1
->dim
, bmap2
->dim
),
2914 nparam
= isl_basic_map_n_param(bmap1
);
2915 n_in
= isl_basic_map_n_in(bmap1
);
2916 n_out
= isl_basic_map_n_out(bmap1
);
2918 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
;
2919 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
2920 dim_map2
= isl_dim_map_alloc(bmap2
->ctx
, total
);
2921 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
2922 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
);
2923 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
2924 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
2925 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_in
+ n_out
);
2926 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
2927 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
2928 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_out
);
2930 bmap
= isl_basic_map_alloc_dim(isl_dim_copy(bmap1
->dim
),
2931 bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
,
2932 bmap1
->n_eq
+ bmap2
->n_eq
+ n_out
,
2933 bmap1
->n_ineq
+ bmap2
->n_ineq
);
2934 for (i
= 0; i
< n_out
; ++i
) {
2935 int j
= isl_basic_map_alloc_equality(bmap
);
2938 isl_seq_clr(bmap
->eq
[j
], 1+total
);
2939 isl_int_set_si(bmap
->eq
[j
][1+nparam
+n_in
+i
], -1);
2940 isl_int_set_si(bmap
->eq
[j
][1+pos
+i
], 1);
2941 isl_int_set_si(bmap
->eq
[j
][1+pos
-n_out
+i
], 1);
2943 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
2944 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
2945 bmap
= add_divs(bmap
, 2 * n_out
);
2947 bmap
= isl_basic_map_simplify(bmap
);
2948 return isl_basic_map_finalize(bmap
);
2950 isl_basic_map_free(bmap
);
2951 isl_basic_map_free(bmap1
);
2952 isl_basic_map_free(bmap2
);
2956 /* Given two maps A -> f(A) and B -> g(B), construct a map
2957 * A \cap B -> f(A) + f(B)
2959 struct isl_map
*isl_map_sum(struct isl_map
*map1
, struct isl_map
*map2
)
2961 struct isl_map
*result
;
2967 isl_assert(map1
->ctx
, isl_dim_equal(map1
->dim
, map2
->dim
), goto error
);
2969 result
= isl_map_alloc_dim(isl_dim_copy(map1
->dim
),
2970 map1
->n
* map2
->n
, 0);
2973 for (i
= 0; i
< map1
->n
; ++i
)
2974 for (j
= 0; j
< map2
->n
; ++j
) {
2975 struct isl_basic_map
*part
;
2976 part
= isl_basic_map_sum(
2977 isl_basic_map_copy(map1
->p
[i
]),
2978 isl_basic_map_copy(map2
->p
[j
]));
2979 if (isl_basic_map_is_empty(part
))
2980 isl_basic_map_free(part
);
2982 result
= isl_map_add_basic_map(result
, part
);
2995 __isl_give isl_set
*isl_set_sum(__isl_take isl_set
*set1
,
2996 __isl_take isl_set
*set2
)
2998 return (isl_set
*)isl_map_sum((isl_map
*)set1
, (isl_map
*)set2
);
3001 /* Given a basic map A -> f(A), construct A -> -f(A).
3003 struct isl_basic_map
*isl_basic_map_neg(struct isl_basic_map
*bmap
)
3008 bmap
= isl_basic_map_cow(bmap
);
3012 n
= isl_basic_map_dim(bmap
, isl_dim_out
);
3013 off
= isl_basic_map_offset(bmap
, isl_dim_out
);
3014 for (i
= 0; i
< bmap
->n_eq
; ++i
)
3015 for (j
= 0; j
< n
; ++j
)
3016 isl_int_neg(bmap
->eq
[i
][off
+j
], bmap
->eq
[i
][off
+j
]);
3017 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
3018 for (j
= 0; j
< n
; ++j
)
3019 isl_int_neg(bmap
->ineq
[i
][off
+j
], bmap
->ineq
[i
][off
+j
]);
3020 for (i
= 0; i
< bmap
->n_div
; ++i
)
3021 for (j
= 0; j
< n
; ++j
)
3022 isl_int_neg(bmap
->div
[i
][1+off
+j
], bmap
->div
[i
][1+off
+j
]);
3023 return isl_basic_map_finalize(bmap
);
3026 __isl_give isl_basic_set
*isl_basic_set_neg(__isl_take isl_basic_set
*bset
)
3028 return isl_basic_map_neg(bset
);
3031 /* Given a map A -> f(A), construct A -> -f(A).
3033 struct isl_map
*isl_map_neg(struct isl_map
*map
)
3037 map
= isl_map_cow(map
);
3041 for (i
= 0; i
< map
->n
; ++i
) {
3042 map
->p
[i
] = isl_basic_map_neg(map
->p
[i
]);
3053 __isl_give isl_set
*isl_set_neg(__isl_take isl_set
*set
)
3055 return (isl_set
*)isl_map_neg((isl_map
*)set
);
3058 /* Given a basic map A -> f(A) and an integer d, construct a basic map
3059 * A -> floor(f(A)/d).
3061 struct isl_basic_map
*isl_basic_map_floordiv(struct isl_basic_map
*bmap
,
3064 unsigned n_in
, n_out
, nparam
, total
, pos
;
3065 struct isl_basic_map
*result
= NULL
;
3066 struct isl_dim_map
*dim_map
;
3072 nparam
= isl_basic_map_n_param(bmap
);
3073 n_in
= isl_basic_map_n_in(bmap
);
3074 n_out
= isl_basic_map_n_out(bmap
);
3076 total
= nparam
+ n_in
+ n_out
+ bmap
->n_div
+ n_out
;
3077 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3078 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_param
, pos
= 0);
3079 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_in
, pos
+= nparam
);
3080 isl_dim_map_div(dim_map
, bmap
, pos
+= n_in
+ n_out
);
3081 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_out
, pos
+= bmap
->n_div
);
3083 result
= isl_basic_map_alloc_dim(isl_dim_copy(bmap
->dim
),
3084 bmap
->n_div
+ n_out
,
3085 bmap
->n_eq
, bmap
->n_ineq
+ 2 * n_out
);
3086 result
= isl_basic_map_add_constraints_dim_map(result
, bmap
, dim_map
);
3087 result
= add_divs(result
, n_out
);
3088 for (i
= 0; i
< n_out
; ++i
) {
3090 j
= isl_basic_map_alloc_inequality(result
);
3093 isl_seq_clr(result
->ineq
[j
], 1+total
);
3094 isl_int_neg(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
3095 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], 1);
3096 j
= isl_basic_map_alloc_inequality(result
);
3099 isl_seq_clr(result
->ineq
[j
], 1+total
);
3100 isl_int_set(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
3101 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], -1);
3102 isl_int_sub_ui(result
->ineq
[j
][0], d
, 1);
3105 result
= isl_basic_map_simplify(result
);
3106 return isl_basic_map_finalize(result
);
3108 isl_basic_map_free(result
);
3112 /* Given a map A -> f(A) and an integer d, construct a map
3113 * A -> floor(f(A)/d).
3115 struct isl_map
*isl_map_floordiv(struct isl_map
*map
, isl_int d
)
3119 map
= isl_map_cow(map
);
3123 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
3124 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3125 for (i
= 0; i
< map
->n
; ++i
) {
3126 map
->p
[i
] = isl_basic_map_floordiv(map
->p
[i
], d
);
3137 static struct isl_basic_map
*var_equal(struct isl_basic_map
*bmap
, unsigned pos
)
3143 i
= isl_basic_map_alloc_equality(bmap
);
3146 nparam
= isl_basic_map_n_param(bmap
);
3147 n_in
= isl_basic_map_n_in(bmap
);
3148 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3149 isl_int_set_si(bmap
->eq
[i
][1+nparam
+pos
], -1);
3150 isl_int_set_si(bmap
->eq
[i
][1+nparam
+n_in
+pos
], 1);
3151 return isl_basic_map_finalize(bmap
);
3153 isl_basic_map_free(bmap
);
3157 /* Add a constraints to "bmap" expressing i_pos < o_pos
3159 static struct isl_basic_map
*var_less(struct isl_basic_map
*bmap
, unsigned pos
)
3165 i
= isl_basic_map_alloc_inequality(bmap
);
3168 nparam
= isl_basic_map_n_param(bmap
);
3169 n_in
= isl_basic_map_n_in(bmap
);
3170 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3171 isl_int_set_si(bmap
->ineq
[i
][0], -1);
3172 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
3173 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
3174 return isl_basic_map_finalize(bmap
);
3176 isl_basic_map_free(bmap
);
3180 /* Add a constraint to "bmap" expressing i_pos <= o_pos
3182 static __isl_give isl_basic_map
*var_less_or_equal(
3183 __isl_take isl_basic_map
*bmap
, unsigned pos
)
3189 i
= isl_basic_map_alloc_inequality(bmap
);
3192 nparam
= isl_basic_map_n_param(bmap
);
3193 n_in
= isl_basic_map_n_in(bmap
);
3194 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3195 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
3196 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
3197 return isl_basic_map_finalize(bmap
);
3199 isl_basic_map_free(bmap
);
3203 /* Add a constraints to "bmap" expressing i_pos > o_pos
3205 static struct isl_basic_map
*var_more(struct isl_basic_map
*bmap
, unsigned pos
)
3211 i
= isl_basic_map_alloc_inequality(bmap
);
3214 nparam
= isl_basic_map_n_param(bmap
);
3215 n_in
= isl_basic_map_n_in(bmap
);
3216 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3217 isl_int_set_si(bmap
->ineq
[i
][0], -1);
3218 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
3219 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
3220 return isl_basic_map_finalize(bmap
);
3222 isl_basic_map_free(bmap
);
3226 /* Add a constraint to "bmap" expressing i_pos >= o_pos
3228 static __isl_give isl_basic_map
*var_more_or_equal(
3229 __isl_take isl_basic_map
*bmap
, unsigned pos
)
3235 i
= isl_basic_map_alloc_inequality(bmap
);
3238 nparam
= isl_basic_map_n_param(bmap
);
3239 n_in
= isl_basic_map_n_in(bmap
);
3240 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3241 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
3242 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
3243 return isl_basic_map_finalize(bmap
);
3245 isl_basic_map_free(bmap
);
3249 struct isl_basic_map
*isl_basic_map_equal(struct isl_dim
*dim
, unsigned n_equal
)
3252 struct isl_basic_map
*bmap
;
3253 bmap
= isl_basic_map_alloc_dim(dim
, 0, n_equal
, 0);
3256 for (i
= 0; i
< n_equal
&& bmap
; ++i
)
3257 bmap
= var_equal(bmap
, i
);
3258 return isl_basic_map_finalize(bmap
);
3261 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
3263 struct isl_basic_map
*isl_basic_map_less_at(struct isl_dim
*dim
, unsigned pos
)
3266 struct isl_basic_map
*bmap
;
3267 bmap
= isl_basic_map_alloc_dim(dim
, 0, pos
, 1);
3270 for (i
= 0; i
< pos
&& bmap
; ++i
)
3271 bmap
= var_equal(bmap
, i
);
3273 bmap
= var_less(bmap
, pos
);
3274 return isl_basic_map_finalize(bmap
);
3277 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
3279 __isl_give isl_basic_map
*isl_basic_map_less_or_equal_at(
3280 __isl_take isl_dim
*dim
, unsigned pos
)
3283 isl_basic_map
*bmap
;
3285 bmap
= isl_basic_map_alloc_dim(dim
, 0, pos
, 1);
3286 for (i
= 0; i
< pos
; ++i
)
3287 bmap
= var_equal(bmap
, i
);
3288 bmap
= var_less_or_equal(bmap
, pos
);
3289 return isl_basic_map_finalize(bmap
);
3292 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
3294 struct isl_basic_map
*isl_basic_map_more_at(struct isl_dim
*dim
, unsigned pos
)
3297 struct isl_basic_map
*bmap
;
3298 bmap
= isl_basic_map_alloc_dim(dim
, 0, pos
, 1);
3301 for (i
= 0; i
< pos
&& bmap
; ++i
)
3302 bmap
= var_equal(bmap
, i
);
3304 bmap
= var_more(bmap
, pos
);
3305 return isl_basic_map_finalize(bmap
);
3308 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
3310 __isl_give isl_basic_map
*isl_basic_map_more_or_equal_at(
3311 __isl_take isl_dim
*dim
, unsigned pos
)
3314 isl_basic_map
*bmap
;
3316 bmap
= isl_basic_map_alloc_dim(dim
, 0, pos
, 1);
3317 for (i
= 0; i
< pos
; ++i
)
3318 bmap
= var_equal(bmap
, i
);
3319 bmap
= var_more_or_equal(bmap
, pos
);
3320 return isl_basic_map_finalize(bmap
);
3323 static __isl_give isl_map
*map_lex_lte_first(__isl_take isl_dim
*dims
,
3324 unsigned n
, int equal
)
3326 struct isl_map
*map
;
3329 if (n
== 0 && equal
)
3330 return isl_map_universe(dims
);
3332 map
= isl_map_alloc_dim(isl_dim_copy(dims
), n
, ISL_MAP_DISJOINT
);
3334 for (i
= 0; i
+ 1 < n
; ++i
)
3335 map
= isl_map_add_basic_map(map
,
3336 isl_basic_map_less_at(isl_dim_copy(dims
), i
));
3339 map
= isl_map_add_basic_map(map
,
3340 isl_basic_map_less_or_equal_at(dims
, n
- 1));
3342 map
= isl_map_add_basic_map(map
,
3343 isl_basic_map_less_at(dims
, n
- 1));
3350 static __isl_give isl_map
*map_lex_lte(__isl_take isl_dim
*dims
, int equal
)
3354 return map_lex_lte_first(dims
, dims
->n_out
, equal
);
3357 __isl_give isl_map
*isl_map_lex_lt_first(__isl_take isl_dim
*dim
, unsigned n
)
3359 return map_lex_lte_first(dim
, n
, 0);
3362 __isl_give isl_map
*isl_map_lex_le_first(__isl_take isl_dim
*dim
, unsigned n
)
3364 return map_lex_lte_first(dim
, n
, 1);
3367 __isl_give isl_map
*isl_map_lex_lt(__isl_take isl_dim
*set_dim
)
3369 return map_lex_lte(isl_dim_map_from_set(set_dim
), 0);
3372 __isl_give isl_map
*isl_map_lex_le(__isl_take isl_dim
*set_dim
)
3374 return map_lex_lte(isl_dim_map_from_set(set_dim
), 1);
3377 static __isl_give isl_map
*map_lex_gte_first(__isl_take isl_dim
*dims
,
3378 unsigned n
, int equal
)
3380 struct isl_map
*map
;
3383 if (n
== 0 && equal
)
3384 return isl_map_universe(dims
);
3386 map
= isl_map_alloc_dim(isl_dim_copy(dims
), n
, ISL_MAP_DISJOINT
);
3388 for (i
= 0; i
+ 1 < n
; ++i
)
3389 map
= isl_map_add_basic_map(map
,
3390 isl_basic_map_more_at(isl_dim_copy(dims
), i
));
3393 map
= isl_map_add_basic_map(map
,
3394 isl_basic_map_more_or_equal_at(dims
, n
- 1));
3396 map
= isl_map_add_basic_map(map
,
3397 isl_basic_map_more_at(dims
, n
- 1));
3404 static __isl_give isl_map
*map_lex_gte(__isl_take isl_dim
*dims
, int equal
)
3408 return map_lex_gte_first(dims
, dims
->n_out
, equal
);
3411 __isl_give isl_map
*isl_map_lex_gt_first(__isl_take isl_dim
*dim
, unsigned n
)
3413 return map_lex_gte_first(dim
, n
, 0);
3416 __isl_give isl_map
*isl_map_lex_ge_first(__isl_take isl_dim
*dim
, unsigned n
)
3418 return map_lex_gte_first(dim
, n
, 1);
3421 __isl_give isl_map
*isl_map_lex_gt(__isl_take isl_dim
*set_dim
)
3423 return map_lex_gte(isl_dim_map_from_set(set_dim
), 0);
3426 __isl_give isl_map
*isl_map_lex_ge(__isl_take isl_dim
*set_dim
)
3428 return map_lex_gte(isl_dim_map_from_set(set_dim
), 1);
3431 __isl_give isl_map
*isl_set_lex_le_set(__isl_take isl_set
*set1
,
3432 __isl_take isl_set
*set2
)
3435 map
= isl_map_lex_le(isl_set_get_dim(set1
));
3436 map
= isl_map_intersect_domain(map
, set1
);
3437 map
= isl_map_intersect_range(map
, set2
);
3441 __isl_give isl_map
*isl_set_lex_lt_set(__isl_take isl_set
*set1
,
3442 __isl_take isl_set
*set2
)
3445 map
= isl_map_lex_lt(isl_set_get_dim(set1
));
3446 map
= isl_map_intersect_domain(map
, set1
);
3447 map
= isl_map_intersect_range(map
, set2
);
3451 __isl_give isl_map
*isl_set_lex_ge_set(__isl_take isl_set
*set1
,
3452 __isl_take isl_set
*set2
)
3455 map
= isl_map_lex_ge(isl_set_get_dim(set1
));
3456 map
= isl_map_intersect_domain(map
, set1
);
3457 map
= isl_map_intersect_range(map
, set2
);
3461 __isl_give isl_map
*isl_set_lex_gt_set(__isl_take isl_set
*set1
,
3462 __isl_take isl_set
*set2
)
3465 map
= isl_map_lex_gt(isl_set_get_dim(set1
));
3466 map
= isl_map_intersect_domain(map
, set1
);
3467 map
= isl_map_intersect_range(map
, set2
);
3471 __isl_give isl_map
*isl_map_lex_le_map(__isl_take isl_map
*map1
,
3472 __isl_take isl_map
*map2
)
3475 map
= isl_map_lex_le(isl_dim_range(isl_map_get_dim(map1
)));
3476 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
3477 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
3481 __isl_give isl_map
*isl_map_lex_lt_map(__isl_take isl_map
*map1
,
3482 __isl_take isl_map
*map2
)
3485 map
= isl_map_lex_lt(isl_dim_range(isl_map_get_dim(map1
)));
3486 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
3487 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
3491 __isl_give isl_map
*isl_map_lex_ge_map(__isl_take isl_map
*map1
,
3492 __isl_take isl_map
*map2
)
3495 map
= isl_map_lex_ge(isl_dim_range(isl_map_get_dim(map1
)));
3496 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
3497 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
3501 __isl_give isl_map
*isl_map_lex_gt_map(__isl_take isl_map
*map1
,
3502 __isl_take isl_map
*map2
)
3505 map
= isl_map_lex_gt(isl_dim_range(isl_map_get_dim(map1
)));
3506 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
3507 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
3511 struct isl_basic_map
*isl_basic_map_from_basic_set(
3512 struct isl_basic_set
*bset
, struct isl_dim
*dim
)
3514 struct isl_basic_map
*bmap
;
3516 bset
= isl_basic_set_cow(bset
);
3520 isl_assert(bset
->ctx
, isl_dim_compatible(bset
->dim
, dim
), goto error
);
3521 isl_dim_free(bset
->dim
);
3522 bmap
= (struct isl_basic_map
*) bset
;
3524 return isl_basic_map_finalize(bmap
);
3526 isl_basic_set_free(bset
);
3531 struct isl_basic_set
*isl_basic_set_from_basic_map(struct isl_basic_map
*bmap
)
3535 if (bmap
->dim
->n_in
== 0)
3536 return (struct isl_basic_set
*)bmap
;
3537 bmap
= isl_basic_map_cow(bmap
);
3540 bmap
->dim
= isl_dim_as_set_dim(bmap
->dim
);
3543 bmap
= isl_basic_map_finalize(bmap
);
3544 return (struct isl_basic_set
*)bmap
;
3546 isl_basic_map_free(bmap
);
3550 /* For a div d = floor(f/m), add the constraints
3553 * -(f-(n-1)) + m d >= 0
3555 * Note that the second constraint is the negation of
3559 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map
*bmap
,
3560 unsigned pos
, isl_int
*div
)
3563 unsigned total
= isl_basic_map_total_dim(bmap
);
3565 i
= isl_basic_map_alloc_inequality(bmap
);
3568 isl_seq_cpy(bmap
->ineq
[i
], div
+ 1, 1 + total
);
3569 isl_int_neg(bmap
->ineq
[i
][1 + pos
], div
[0]);
3571 j
= isl_basic_map_alloc_inequality(bmap
);
3574 isl_seq_neg(bmap
->ineq
[j
], bmap
->ineq
[i
], 1 + total
);
3575 isl_int_add(bmap
->ineq
[j
][0], bmap
->ineq
[j
][0], bmap
->ineq
[j
][1 + pos
]);
3576 isl_int_sub_ui(bmap
->ineq
[j
][0], bmap
->ineq
[j
][0], 1);
3580 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set
*bset
,
3581 unsigned pos
, isl_int
*div
)
3583 return isl_basic_map_add_div_constraints_var((isl_basic_map
*)bset
,
3587 int isl_basic_map_add_div_constraints(struct isl_basic_map
*bmap
, unsigned div
)
3589 unsigned total
= isl_basic_map_total_dim(bmap
);
3590 unsigned div_pos
= total
- bmap
->n_div
+ div
;
3592 return isl_basic_map_add_div_constraints_var(bmap
, div_pos
,
3596 struct isl_basic_set
*isl_basic_map_underlying_set(
3597 struct isl_basic_map
*bmap
)
3601 if (bmap
->dim
->nparam
== 0 && bmap
->dim
->n_in
== 0 &&
3603 !isl_dim_is_named_or_nested(bmap
->dim
, isl_dim_in
) &&
3604 !isl_dim_is_named_or_nested(bmap
->dim
, isl_dim_out
))
3605 return (struct isl_basic_set
*)bmap
;
3606 bmap
= isl_basic_map_cow(bmap
);
3609 bmap
->dim
= isl_dim_underlying(bmap
->dim
, bmap
->n_div
);
3612 bmap
->extra
-= bmap
->n_div
;
3614 bmap
= isl_basic_map_finalize(bmap
);
3615 return (struct isl_basic_set
*)bmap
;
3620 __isl_give isl_basic_set
*isl_basic_set_underlying_set(
3621 __isl_take isl_basic_set
*bset
)
3623 return isl_basic_map_underlying_set((isl_basic_map
*)bset
);
3626 struct isl_basic_map
*isl_basic_map_overlying_set(
3627 struct isl_basic_set
*bset
, struct isl_basic_map
*like
)
3629 struct isl_basic_map
*bmap
;
3630 struct isl_ctx
*ctx
;
3637 isl_assert(ctx
, bset
->n_div
== 0, goto error
);
3638 isl_assert(ctx
, isl_basic_set_n_param(bset
) == 0, goto error
);
3639 isl_assert(ctx
, bset
->dim
->n_out
== isl_basic_map_total_dim(like
),
3641 if (isl_dim_equal(bset
->dim
, like
->dim
) && like
->n_div
== 0) {
3642 isl_basic_map_free(like
);
3643 return (struct isl_basic_map
*)bset
;
3645 bset
= isl_basic_set_cow(bset
);
3648 total
= bset
->dim
->n_out
+ bset
->extra
;
3649 bmap
= (struct isl_basic_map
*)bset
;
3650 isl_dim_free(bmap
->dim
);
3651 bmap
->dim
= isl_dim_copy(like
->dim
);
3654 bmap
->n_div
= like
->n_div
;
3655 bmap
->extra
+= like
->n_div
;
3659 ltotal
= total
- bmap
->extra
+ like
->extra
;
3662 bmap
->block2
= isl_blk_extend(ctx
, bmap
->block2
,
3663 bmap
->extra
* (1 + 1 + total
));
3664 if (isl_blk_is_error(bmap
->block2
))
3666 div
= isl_realloc_array(ctx
, bmap
->div
, isl_int
*, bmap
->extra
);
3670 for (i
= 0; i
< bmap
->extra
; ++i
)
3671 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + 1 + total
);
3672 for (i
= 0; i
< like
->n_div
; ++i
) {
3673 isl_seq_cpy(bmap
->div
[i
], like
->div
[i
], 1 + 1 + ltotal
);
3674 isl_seq_clr(bmap
->div
[i
]+1+1+ltotal
, total
- ltotal
);
3676 bmap
= isl_basic_map_extend_constraints(bmap
,
3677 0, 2 * like
->n_div
);
3678 for (i
= 0; i
< like
->n_div
; ++i
) {
3679 if (isl_int_is_zero(bmap
->div
[i
][0]))
3681 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
3685 isl_basic_map_free(like
);
3686 bmap
= isl_basic_map_simplify(bmap
);
3687 bmap
= isl_basic_map_finalize(bmap
);
3690 isl_basic_map_free(like
);
3691 isl_basic_set_free(bset
);
3695 struct isl_basic_set
*isl_basic_set_from_underlying_set(
3696 struct isl_basic_set
*bset
, struct isl_basic_set
*like
)
3698 return (struct isl_basic_set
*)
3699 isl_basic_map_overlying_set(bset
, (struct isl_basic_map
*)like
);
3702 struct isl_set
*isl_set_from_underlying_set(
3703 struct isl_set
*set
, struct isl_basic_set
*like
)
3709 isl_assert(set
->ctx
, set
->dim
->n_out
== isl_basic_set_total_dim(like
),
3711 if (isl_dim_equal(set
->dim
, like
->dim
) && like
->n_div
== 0) {
3712 isl_basic_set_free(like
);
3715 set
= isl_set_cow(set
);
3718 for (i
= 0; i
< set
->n
; ++i
) {
3719 set
->p
[i
] = isl_basic_set_from_underlying_set(set
->p
[i
],
3720 isl_basic_set_copy(like
));
3724 isl_dim_free(set
->dim
);
3725 set
->dim
= isl_dim_copy(like
->dim
);
3728 isl_basic_set_free(like
);
3731 isl_basic_set_free(like
);
3736 struct isl_set
*isl_map_underlying_set(struct isl_map
*map
)
3740 map
= isl_map_cow(map
);
3743 map
->dim
= isl_dim_cow(map
->dim
);
3747 for (i
= 1; i
< map
->n
; ++i
)
3748 isl_assert(map
->ctx
, map
->p
[0]->n_div
== map
->p
[i
]->n_div
,
3750 for (i
= 0; i
< map
->n
; ++i
) {
3751 map
->p
[i
] = (struct isl_basic_map
*)
3752 isl_basic_map_underlying_set(map
->p
[i
]);
3757 map
->dim
= isl_dim_underlying(map
->dim
, 0);
3759 isl_dim_free(map
->dim
);
3760 map
->dim
= isl_dim_copy(map
->p
[0]->dim
);
3764 return (struct isl_set
*)map
;
3770 struct isl_set
*isl_set_to_underlying_set(struct isl_set
*set
)
3772 return (struct isl_set
*)isl_map_underlying_set((struct isl_map
*)set
);
3775 __isl_give isl_basic_map
*isl_basic_map_reset_dim(
3776 __isl_take isl_basic_map
*bmap
, __isl_take isl_dim
*dim
)
3778 bmap
= isl_basic_map_cow(bmap
);
3782 isl_dim_free(bmap
->dim
);
3785 bmap
= isl_basic_map_finalize(bmap
);
3789 isl_basic_map_free(bmap
);
3794 __isl_give isl_basic_set
*isl_basic_set_reset_dim(
3795 __isl_take isl_basic_set
*bset
, __isl_take isl_dim
*dim
)
3797 return (isl_basic_set
*)isl_basic_map_reset_dim((isl_basic_map
*)bset
,
3801 __isl_give isl_map
*isl_map_reset_dim(__isl_take isl_map
*map
,
3802 __isl_take isl_dim
*dim
)
3806 map
= isl_map_cow(map
);
3810 for (i
= 0; i
< map
->n
; ++i
) {
3811 map
->p
[i
] = isl_basic_map_reset_dim(map
->p
[i
],
3816 isl_dim_free(map
->dim
);
3826 __isl_give isl_set
*isl_set_reset_dim(__isl_take isl_set
*set
,
3827 __isl_take isl_dim
*dim
)
3829 return (struct isl_set
*) isl_map_reset_dim((struct isl_map
*)set
, dim
);
3832 struct isl_basic_set
*isl_basic_map_domain(struct isl_basic_map
*bmap
)
3835 struct isl_basic_set
*domain
;
3841 dim
= isl_dim_domain(isl_basic_map_get_dim(bmap
));
3843 n_in
= isl_basic_map_n_in(bmap
);
3844 n_out
= isl_basic_map_n_out(bmap
);
3845 domain
= isl_basic_set_from_basic_map(bmap
);
3846 domain
= isl_basic_set_project_out(domain
, isl_dim_set
, n_in
, n_out
);
3848 domain
= isl_basic_set_reset_dim(domain
, dim
);
3853 int isl_basic_map_may_be_set(__isl_keep isl_basic_map
*bmap
)
3857 return isl_dim_may_be_set(bmap
->dim
);
3860 struct isl_basic_set
*isl_basic_map_range(struct isl_basic_map
*bmap
)
3864 if (isl_basic_map_may_be_set(bmap
))
3866 return isl_basic_map_domain(isl_basic_map_reverse(bmap
));
3869 __isl_give isl_basic_map
*isl_basic_map_domain_map(
3870 __isl_take isl_basic_map
*bmap
)
3874 isl_basic_map
*domain
;
3875 isl_basic_set
*bset
;
3876 int nparam
, n_in
, n_out
;
3879 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
3880 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
3881 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
3883 dim
= isl_dim_from_range(isl_dim_domain(isl_basic_map_get_dim(bmap
)));
3884 domain
= isl_basic_map_universe(dim
);
3886 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
3887 bmap
= isl_basic_map_apply_range(bmap
, domain
);
3888 bmap
= isl_basic_map_extend_constraints(bmap
, n_in
, 0);
3890 total
= isl_basic_map_total_dim(bmap
);
3892 for (i
= 0; i
< n_in
; ++i
) {
3893 k
= isl_basic_map_alloc_equality(bmap
);
3896 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
3897 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], -1);
3898 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ n_out
+ i
], 1);
3901 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3902 return isl_basic_map_finalize(bmap
);
3904 isl_basic_map_free(bmap
);
3908 __isl_give isl_basic_map
*isl_basic_map_range_map(
3909 __isl_take isl_basic_map
*bmap
)
3913 isl_basic_map
*range
;
3914 isl_basic_set
*bset
;
3915 int nparam
, n_in
, n_out
;
3918 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
3919 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
3920 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
3922 dim
= isl_dim_from_range(isl_dim_range(isl_basic_map_get_dim(bmap
)));
3923 range
= isl_basic_map_universe(dim
);
3925 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
3926 bmap
= isl_basic_map_apply_range(bmap
, range
);
3927 bmap
= isl_basic_map_extend_constraints(bmap
, n_out
, 0);
3929 total
= isl_basic_map_total_dim(bmap
);
3931 for (i
= 0; i
< n_out
; ++i
) {
3932 k
= isl_basic_map_alloc_equality(bmap
);
3935 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
3936 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ i
], -1);
3937 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ n_out
+ i
], 1);
3940 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3941 return isl_basic_map_finalize(bmap
);
3943 isl_basic_map_free(bmap
);
3947 int isl_map_may_be_set(__isl_keep isl_map
*map
)
3951 return isl_dim_may_be_set(map
->dim
);
3954 struct isl_set
*isl_map_range(struct isl_map
*map
)
3957 struct isl_set
*set
;
3961 if (isl_map_may_be_set(map
))
3962 return (isl_set
*)map
;
3964 map
= isl_map_cow(map
);
3968 set
= (struct isl_set
*) map
;
3969 set
->dim
= isl_dim_drop_inputs(set
->dim
, 0, set
->dim
->n_in
);
3972 for (i
= 0; i
< map
->n
; ++i
) {
3973 set
->p
[i
] = isl_basic_map_range(map
->p
[i
]);
3977 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
3978 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
3985 __isl_give isl_map
*isl_map_domain_map(__isl_take isl_map
*map
)
3988 isl_dim
*domain_dim
;
3990 map
= isl_map_cow(map
);
3994 domain_dim
= isl_dim_from_range(isl_dim_domain(isl_map_get_dim(map
)));
3995 map
->dim
= isl_dim_from_domain(isl_dim_wrap(map
->dim
));
3996 map
->dim
= isl_dim_join(map
->dim
, domain_dim
);
3999 for (i
= 0; i
< map
->n
; ++i
) {
4000 map
->p
[i
] = isl_basic_map_domain_map(map
->p
[i
]);
4004 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
4005 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4012 __isl_give isl_map
*isl_map_range_map(__isl_take isl_map
*map
)
4017 map
= isl_map_cow(map
);
4021 range_dim
= isl_dim_range(isl_map_get_dim(map
));
4022 map
->dim
= isl_dim_from_domain(isl_dim_wrap(map
->dim
));
4023 map
->dim
= isl_dim_join(map
->dim
, range_dim
);
4026 for (i
= 0; i
< map
->n
; ++i
) {
4027 map
->p
[i
] = isl_basic_map_range_map(map
->p
[i
]);
4031 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
4032 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4039 struct isl_map
*isl_map_from_set(struct isl_set
*set
, struct isl_dim
*dim
)
4042 struct isl_map
*map
= NULL
;
4044 set
= isl_set_cow(set
);
4047 isl_assert(set
->ctx
, isl_dim_compatible(set
->dim
, dim
), goto error
);
4048 map
= (struct isl_map
*)set
;
4049 for (i
= 0; i
< set
->n
; ++i
) {
4050 map
->p
[i
] = isl_basic_map_from_basic_set(
4051 set
->p
[i
], isl_dim_copy(dim
));
4055 isl_dim_free(map
->dim
);
4064 __isl_give isl_basic_map
*isl_basic_map_from_domain(
4065 __isl_take isl_basic_set
*bset
)
4067 return isl_basic_map_reverse(isl_basic_map_from_range(bset
));
4070 __isl_give isl_basic_map
*isl_basic_map_from_range(
4071 __isl_take isl_basic_set
*bset
)
4073 return (isl_basic_map
*)bset
;
4076 struct isl_map
*isl_map_from_range(struct isl_set
*set
)
4078 return (struct isl_map
*)set
;
4081 __isl_give isl_map
*isl_map_from_domain(__isl_take isl_set
*set
)
4083 return isl_map_reverse(isl_map_from_range(set
));
4086 __isl_give isl_basic_map
*isl_basic_map_from_domain_and_range(
4087 __isl_take isl_basic_set
*domain
, __isl_take isl_basic_set
*range
)
4089 return isl_basic_map_apply_range(isl_basic_map_from_domain(domain
),
4090 isl_basic_map_from_range(range
));
4093 __isl_give isl_map
*isl_map_from_domain_and_range(__isl_take isl_set
*domain
,
4094 __isl_take isl_set
*range
)
4096 return isl_map_apply_range(isl_map_from_domain(domain
),
4097 isl_map_from_range(range
));
4100 struct isl_set
*isl_set_from_map(struct isl_map
*map
)
4103 struct isl_set
*set
= NULL
;
4107 map
= isl_map_cow(map
);
4110 map
->dim
= isl_dim_as_set_dim(map
->dim
);
4113 set
= (struct isl_set
*)map
;
4114 for (i
= 0; i
< map
->n
; ++i
) {
4115 set
->p
[i
] = isl_basic_set_from_basic_map(map
->p
[i
]);
4125 struct isl_map
*isl_map_alloc_dim(struct isl_dim
*dim
, int n
, unsigned flags
)
4127 struct isl_map
*map
;
4131 isl_assert(dim
->ctx
, n
>= 0, return NULL
);
4132 map
= isl_alloc(dim
->ctx
, struct isl_map
,
4133 sizeof(struct isl_map
) +
4134 (n
- 1) * sizeof(struct isl_basic_map
*));
4138 map
->ctx
= dim
->ctx
;
4139 isl_ctx_ref(map
->ctx
);
4151 struct isl_map
*isl_map_alloc(struct isl_ctx
*ctx
,
4152 unsigned nparam
, unsigned in
, unsigned out
, int n
,
4155 struct isl_map
*map
;
4156 struct isl_dim
*dims
;
4158 dims
= isl_dim_alloc(ctx
, nparam
, in
, out
);
4162 map
= isl_map_alloc_dim(dims
, n
, flags
);
4166 struct isl_basic_map
*isl_basic_map_empty(struct isl_dim
*dim
)
4168 struct isl_basic_map
*bmap
;
4169 bmap
= isl_basic_map_alloc_dim(dim
, 0, 1, 0);
4170 bmap
= isl_basic_map_set_to_empty(bmap
);
4174 struct isl_basic_set
*isl_basic_set_empty(struct isl_dim
*dim
)
4176 struct isl_basic_set
*bset
;
4177 bset
= isl_basic_set_alloc_dim(dim
, 0, 1, 0);
4178 bset
= isl_basic_set_set_to_empty(bset
);
4182 struct isl_basic_map
*isl_basic_map_empty_like(struct isl_basic_map
*model
)
4184 struct isl_basic_map
*bmap
;
4187 bmap
= isl_basic_map_alloc_dim(isl_dim_copy(model
->dim
), 0, 1, 0);
4188 bmap
= isl_basic_map_set_to_empty(bmap
);
4192 struct isl_basic_map
*isl_basic_map_empty_like_map(struct isl_map
*model
)
4194 struct isl_basic_map
*bmap
;
4197 bmap
= isl_basic_map_alloc_dim(isl_dim_copy(model
->dim
), 0, 1, 0);
4198 bmap
= isl_basic_map_set_to_empty(bmap
);
4202 struct isl_basic_set
*isl_basic_set_empty_like(struct isl_basic_set
*model
)
4204 struct isl_basic_set
*bset
;
4207 bset
= isl_basic_set_alloc_dim(isl_dim_copy(model
->dim
), 0, 1, 0);
4208 bset
= isl_basic_set_set_to_empty(bset
);
4212 struct isl_basic_map
*isl_basic_map_universe(struct isl_dim
*dim
)
4214 struct isl_basic_map
*bmap
;
4215 bmap
= isl_basic_map_alloc_dim(dim
, 0, 0, 0);
4216 bmap
= isl_basic_map_finalize(bmap
);
4220 struct isl_basic_set
*isl_basic_set_universe(struct isl_dim
*dim
)
4222 struct isl_basic_set
*bset
;
4223 bset
= isl_basic_set_alloc_dim(dim
, 0, 0, 0);
4224 bset
= isl_basic_set_finalize(bset
);
4228 __isl_give isl_basic_map
*isl_basic_map_nat_universe(__isl_take isl_dim
*dim
)
4231 unsigned total
= isl_dim_total(dim
);
4232 isl_basic_map
*bmap
;
4234 bmap
= isl_basic_map_alloc_dim(dim
, 0, 0, total
);
4235 for (i
= 0; i
< total
; ++i
) {
4236 int k
= isl_basic_map_alloc_inequality(bmap
);
4239 isl_seq_clr(bmap
->ineq
[k
], 1 + total
);
4240 isl_int_set_si(bmap
->ineq
[k
][1 + i
], 1);
4244 isl_basic_map_free(bmap
);
4248 __isl_give isl_basic_set
*isl_basic_set_nat_universe(__isl_take isl_dim
*dim
)
4250 return isl_basic_map_nat_universe(dim
);
4253 __isl_give isl_map
*isl_map_nat_universe(__isl_take isl_dim
*dim
)
4255 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim
));
4258 __isl_give isl_set
*isl_set_nat_universe(__isl_take isl_dim
*dim
)
4260 return isl_map_nat_universe(dim
);
4263 __isl_give isl_basic_map
*isl_basic_map_universe_like(
4264 __isl_keep isl_basic_map
*model
)
4268 return isl_basic_map_alloc_dim(isl_dim_copy(model
->dim
), 0, 0, 0);
4271 struct isl_basic_set
*isl_basic_set_universe_like(struct isl_basic_set
*model
)
4275 return isl_basic_set_alloc_dim(isl_dim_copy(model
->dim
), 0, 0, 0);
4278 __isl_give isl_basic_set
*isl_basic_set_universe_like_set(
4279 __isl_keep isl_set
*model
)
4283 return isl_basic_set_alloc_dim(isl_dim_copy(model
->dim
), 0, 0, 0);
4286 struct isl_map
*isl_map_empty(struct isl_dim
*dim
)
4288 return isl_map_alloc_dim(dim
, 0, ISL_MAP_DISJOINT
);
4291 struct isl_map
*isl_map_empty_like(struct isl_map
*model
)
4295 return isl_map_alloc_dim(isl_dim_copy(model
->dim
), 0, ISL_MAP_DISJOINT
);
4298 struct isl_map
*isl_map_empty_like_basic_map(struct isl_basic_map
*model
)
4302 return isl_map_alloc_dim(isl_dim_copy(model
->dim
), 0, ISL_MAP_DISJOINT
);
4305 struct isl_set
*isl_set_empty(struct isl_dim
*dim
)
4307 return isl_set_alloc_dim(dim
, 0, ISL_MAP_DISJOINT
);
4310 struct isl_set
*isl_set_empty_like(struct isl_set
*model
)
4314 return isl_set_empty(isl_dim_copy(model
->dim
));
4317 struct isl_map
*isl_map_universe(struct isl_dim
*dim
)
4319 struct isl_map
*map
;
4322 map
= isl_map_alloc_dim(isl_dim_copy(dim
), 1, ISL_MAP_DISJOINT
);
4323 map
= isl_map_add_basic_map(map
, isl_basic_map_universe(dim
));
4327 struct isl_set
*isl_set_universe(struct isl_dim
*dim
)
4329 struct isl_set
*set
;
4332 set
= isl_set_alloc_dim(isl_dim_copy(dim
), 1, ISL_MAP_DISJOINT
);
4333 set
= isl_set_add_basic_set(set
, isl_basic_set_universe(dim
));
4337 __isl_give isl_set
*isl_set_universe_like(__isl_keep isl_set
*model
)
4341 return isl_set_universe(isl_dim_copy(model
->dim
));
4344 struct isl_map
*isl_map_dup(struct isl_map
*map
)
4347 struct isl_map
*dup
;
4351 dup
= isl_map_alloc_dim(isl_dim_copy(map
->dim
), map
->n
, map
->flags
);
4352 for (i
= 0; i
< map
->n
; ++i
)
4353 dup
= isl_map_add_basic_map(dup
, isl_basic_map_copy(map
->p
[i
]));
4357 __isl_give isl_map
*isl_map_add_basic_map(__isl_take isl_map
*map
,
4358 __isl_take isl_basic_map
*bmap
)
4362 if (isl_basic_map_plain_is_empty(bmap
)) {
4363 isl_basic_map_free(bmap
);
4366 isl_assert(map
->ctx
, isl_dim_equal(map
->dim
, bmap
->dim
), goto error
);
4367 isl_assert(map
->ctx
, map
->n
< map
->size
, goto error
);
4368 map
->p
[map
->n
] = bmap
;
4370 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4376 isl_basic_map_free(bmap
);
4380 void isl_map_free(struct isl_map
*map
)
4390 isl_ctx_deref(map
->ctx
);
4391 for (i
= 0; i
< map
->n
; ++i
)
4392 isl_basic_map_free(map
->p
[i
]);
4393 isl_dim_free(map
->dim
);
4397 struct isl_map
*isl_map_extend(struct isl_map
*base
,
4398 unsigned nparam
, unsigned n_in
, unsigned n_out
)
4402 base
= isl_map_cow(base
);
4406 base
->dim
= isl_dim_extend(base
->dim
, nparam
, n_in
, n_out
);
4409 for (i
= 0; i
< base
->n
; ++i
) {
4410 base
->p
[i
] = isl_basic_map_extend_dim(base
->p
[i
],
4411 isl_dim_copy(base
->dim
), 0, 0, 0);
4421 struct isl_set
*isl_set_extend(struct isl_set
*base
,
4422 unsigned nparam
, unsigned dim
)
4424 return (struct isl_set
*)isl_map_extend((struct isl_map
*)base
,
4428 static struct isl_basic_map
*isl_basic_map_fix_pos_si(
4429 struct isl_basic_map
*bmap
, unsigned pos
, int value
)
4433 bmap
= isl_basic_map_cow(bmap
);
4434 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
4435 j
= isl_basic_map_alloc_equality(bmap
);
4438 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
4439 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
4440 isl_int_set_si(bmap
->eq
[j
][0], value
);
4441 bmap
= isl_basic_map_simplify(bmap
);
4442 return isl_basic_map_finalize(bmap
);
4444 isl_basic_map_free(bmap
);
4448 static __isl_give isl_basic_map
*isl_basic_map_fix_pos(
4449 __isl_take isl_basic_map
*bmap
, unsigned pos
, isl_int value
)
4453 bmap
= isl_basic_map_cow(bmap
);
4454 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
4455 j
= isl_basic_map_alloc_equality(bmap
);
4458 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
4459 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
4460 isl_int_set(bmap
->eq
[j
][0], value
);
4461 bmap
= isl_basic_map_simplify(bmap
);
4462 return isl_basic_map_finalize(bmap
);
4464 isl_basic_map_free(bmap
);
4468 struct isl_basic_map
*isl_basic_map_fix_si(struct isl_basic_map
*bmap
,
4469 enum isl_dim_type type
, unsigned pos
, int value
)
4473 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
4474 return isl_basic_map_fix_pos_si(bmap
,
4475 isl_basic_map_offset(bmap
, type
) + pos
, value
);
4477 isl_basic_map_free(bmap
);
4481 __isl_give isl_basic_map
*isl_basic_map_fix(__isl_take isl_basic_map
*bmap
,
4482 enum isl_dim_type type
, unsigned pos
, isl_int value
)
4486 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
4487 return isl_basic_map_fix_pos(bmap
,
4488 isl_basic_map_offset(bmap
, type
) + pos
, value
);
4490 isl_basic_map_free(bmap
);
4494 struct isl_basic_set
*isl_basic_set_fix_si(struct isl_basic_set
*bset
,
4495 enum isl_dim_type type
, unsigned pos
, int value
)
4497 return (struct isl_basic_set
*)
4498 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
4502 __isl_give isl_basic_set
*isl_basic_set_fix(__isl_take isl_basic_set
*bset
,
4503 enum isl_dim_type type
, unsigned pos
, isl_int value
)
4505 return (struct isl_basic_set
*)
4506 isl_basic_map_fix((struct isl_basic_map
*)bset
,
4510 struct isl_basic_map
*isl_basic_map_fix_input_si(struct isl_basic_map
*bmap
,
4511 unsigned input
, int value
)
4513 return isl_basic_map_fix_si(bmap
, isl_dim_in
, input
, value
);
4516 struct isl_basic_set
*isl_basic_set_fix_dim_si(struct isl_basic_set
*bset
,
4517 unsigned dim
, int value
)
4519 return (struct isl_basic_set
*)
4520 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
4521 isl_dim_set
, dim
, value
);
4524 struct isl_map
*isl_map_fix_si(struct isl_map
*map
,
4525 enum isl_dim_type type
, unsigned pos
, int value
)
4529 map
= isl_map_cow(map
);
4533 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
4534 for (i
= 0; i
< map
->n
; ++i
) {
4535 map
->p
[i
] = isl_basic_map_fix_si(map
->p
[i
], type
, pos
, value
);
4539 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4546 __isl_give isl_set
*isl_set_fix_si(__isl_take isl_set
*set
,
4547 enum isl_dim_type type
, unsigned pos
, int value
)
4549 return (struct isl_set
*)
4550 isl_map_fix_si((struct isl_map
*)set
, type
, pos
, value
);
4553 __isl_give isl_map
*isl_map_fix(__isl_take isl_map
*map
,
4554 enum isl_dim_type type
, unsigned pos
, isl_int value
)
4558 map
= isl_map_cow(map
);
4562 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
4563 for (i
= 0; i
< map
->n
; ++i
) {
4564 map
->p
[i
] = isl_basic_map_fix(map
->p
[i
], type
, pos
, value
);
4568 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4575 __isl_give isl_set
*isl_set_fix(__isl_take isl_set
*set
,
4576 enum isl_dim_type type
, unsigned pos
, isl_int value
)
4578 return (struct isl_set
*)isl_map_fix((isl_map
*)set
, type
, pos
, value
);
4581 struct isl_map
*isl_map_fix_input_si(struct isl_map
*map
,
4582 unsigned input
, int value
)
4584 return isl_map_fix_si(map
, isl_dim_in
, input
, value
);
4587 struct isl_set
*isl_set_fix_dim_si(struct isl_set
*set
, unsigned dim
, int value
)
4589 return (struct isl_set
*)
4590 isl_map_fix_si((struct isl_map
*)set
, isl_dim_set
, dim
, value
);
4593 __isl_give isl_basic_map
*isl_basic_map_lower_bound_si(
4594 __isl_take isl_basic_map
*bmap
,
4595 enum isl_dim_type type
, unsigned pos
, int value
)
4601 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
4602 pos
+= isl_basic_map_offset(bmap
, type
);
4603 bmap
= isl_basic_map_cow(bmap
);
4604 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
4605 j
= isl_basic_map_alloc_inequality(bmap
);
4608 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
4609 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
4610 isl_int_set_si(bmap
->ineq
[j
][0], -value
);
4611 bmap
= isl_basic_map_simplify(bmap
);
4612 return isl_basic_map_finalize(bmap
);
4614 isl_basic_map_free(bmap
);
4618 struct isl_basic_set
*isl_basic_set_lower_bound_dim(struct isl_basic_set
*bset
,
4619 unsigned dim
, isl_int value
)
4623 bset
= isl_basic_set_cow(bset
);
4624 bset
= isl_basic_set_extend_constraints(bset
, 0, 1);
4625 j
= isl_basic_set_alloc_inequality(bset
);
4628 isl_seq_clr(bset
->ineq
[j
], 1 + isl_basic_set_total_dim(bset
));
4629 isl_int_set_si(bset
->ineq
[j
][1 + isl_basic_set_n_param(bset
) + dim
], 1);
4630 isl_int_neg(bset
->ineq
[j
][0], value
);
4631 bset
= isl_basic_set_simplify(bset
);
4632 return isl_basic_set_finalize(bset
);
4634 isl_basic_set_free(bset
);
4638 __isl_give isl_map
*isl_map_lower_bound_si(__isl_take isl_map
*map
,
4639 enum isl_dim_type type
, unsigned pos
, int value
)
4643 map
= isl_map_cow(map
);
4647 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
4648 for (i
= 0; i
< map
->n
; ++i
) {
4649 map
->p
[i
] = isl_basic_map_lower_bound_si(map
->p
[i
],
4654 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4661 __isl_give isl_set
*isl_set_lower_bound_si(__isl_take isl_set
*set
,
4662 enum isl_dim_type type
, unsigned pos
, int value
)
4664 return (struct isl_set
*)
4665 isl_map_lower_bound_si((struct isl_map
*)set
, type
, pos
, value
);
4668 struct isl_set
*isl_set_lower_bound_dim(struct isl_set
*set
, unsigned dim
,
4673 set
= isl_set_cow(set
);
4677 isl_assert(set
->ctx
, dim
< isl_set_n_dim(set
), goto error
);
4678 for (i
= 0; i
< set
->n
; ++i
) {
4679 set
->p
[i
] = isl_basic_set_lower_bound_dim(set
->p
[i
], dim
, value
);
4689 struct isl_map
*isl_map_reverse(struct isl_map
*map
)
4693 map
= isl_map_cow(map
);
4697 map
->dim
= isl_dim_reverse(map
->dim
);
4700 for (i
= 0; i
< map
->n
; ++i
) {
4701 map
->p
[i
] = isl_basic_map_reverse(map
->p
[i
]);
4705 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4712 static struct isl_map
*isl_basic_map_partial_lexopt(
4713 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
4714 struct isl_set
**empty
, int max
)
4718 if (bmap
->ctx
->opt
->pip
== ISL_PIP_PIP
)
4719 return isl_pip_basic_map_lexopt(bmap
, dom
, empty
, max
);
4721 return isl_tab_basic_map_partial_lexopt(bmap
, dom
, empty
, max
);
4723 isl_basic_map_free(bmap
);
4724 isl_basic_set_free(dom
);
4730 struct isl_map
*isl_basic_map_partial_lexmax(
4731 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
4732 struct isl_set
**empty
)
4734 return isl_basic_map_partial_lexopt(bmap
, dom
, empty
, 1);
4737 struct isl_map
*isl_basic_map_partial_lexmin(
4738 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
4739 struct isl_set
**empty
)
4741 return isl_basic_map_partial_lexopt(bmap
, dom
, empty
, 0);
4744 struct isl_set
*isl_basic_set_partial_lexmin(
4745 struct isl_basic_set
*bset
, struct isl_basic_set
*dom
,
4746 struct isl_set
**empty
)
4748 return (struct isl_set
*)
4749 isl_basic_map_partial_lexmin((struct isl_basic_map
*)bset
,
4753 struct isl_set
*isl_basic_set_partial_lexmax(
4754 struct isl_basic_set
*bset
, struct isl_basic_set
*dom
,
4755 struct isl_set
**empty
)
4757 return (struct isl_set
*)
4758 isl_basic_map_partial_lexmax((struct isl_basic_map
*)bset
,
4762 /* Given a basic map "bmap", compute the lexicographically minimal
4763 * (or maximal) image element for each domain element in dom.
4764 * Set *empty to those elements in dom that do not have an image element.
4766 * We first make sure the basic sets in dom are disjoint and then
4767 * simply collect the results over each of the basic sets separately.
4768 * We could probably improve the efficiency a bit by moving the union
4769 * domain down into the parametric integer programming.
4771 static __isl_give isl_map
*basic_map_partial_lexopt(
4772 __isl_take isl_basic_map
*bmap
, __isl_take isl_set
*dom
,
4773 __isl_give isl_set
**empty
, int max
)
4776 struct isl_map
*res
;
4778 dom
= isl_set_make_disjoint(dom
);
4782 if (isl_set_plain_is_empty(dom
)) {
4783 res
= isl_map_empty_like_basic_map(bmap
);
4784 *empty
= isl_set_empty_like(dom
);
4786 isl_basic_map_free(bmap
);
4790 res
= isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap
),
4791 isl_basic_set_copy(dom
->p
[0]), empty
, max
);
4793 for (i
= 1; i
< dom
->n
; ++i
) {
4794 struct isl_map
*res_i
;
4795 struct isl_set
*empty_i
;
4797 res_i
= isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap
),
4798 isl_basic_set_copy(dom
->p
[i
]), &empty_i
, max
);
4800 res
= isl_map_union_disjoint(res
, res_i
);
4801 *empty
= isl_set_union_disjoint(*empty
, empty_i
);
4805 isl_basic_map_free(bmap
);
4810 isl_basic_map_free(bmap
);
4814 /* Given a map "map", compute the lexicographically minimal
4815 * (or maximal) image element for each domain element in dom.
4816 * Set *empty to those elements in dom that do not have an image element.
4818 * We first compute the lexicographically minimal or maximal element
4819 * in the first basic map. This results in a partial solution "res"
4820 * and a subset "todo" of dom that still need to be handled.
4821 * We then consider each of the remaining maps in "map" and successively
4822 * improve both "res" and "todo".
4824 * Let res^k and todo^k be the results after k steps and let i = k + 1.
4825 * Assume we are computing the lexicographical maximum.
4826 * We first compute the lexicographically maximal element in basic map i.
4827 * This results in a partial solution res_i and a subset todo_i.
4828 * Then we combine these results with those obtain for the first k basic maps
4829 * to obtain a result that is valid for the first k+1 basic maps.
4830 * In particular, the set where there is no solution is the set where
4831 * there is no solution for the first k basic maps and also no solution
4832 * for the ith basic map, i.e.,
4834 * todo^i = todo^k * todo_i
4836 * On dom(res^k) * dom(res_i), we need to pick the larger of the two
4837 * solutions, arbitrarily breaking ties in favor of res^k.
4838 * That is, when res^k(a) >= res_i(a), we pick res^k and
4839 * when res^k(a) < res_i(a), we pick res_i. (Here, ">=" and "<" denote
4840 * the lexicographic order.)
4841 * In practice, we compute
4843 * res^k * (res_i . "<=")
4847 * res_i * (res^k . "<")
4849 * Finally, we consider the symmetric difference of dom(res^k) and dom(res_i),
4850 * where only one of res^k and res_i provides a solution and we simply pick
4857 * Note that we only compute these intersections when dom(res^k) intersects
4858 * dom(res_i). Otherwise, the only effect of these intersections is to
4859 * potentially break up res^k and res_i into smaller pieces.
4860 * We want to avoid such splintering as much as possible.
4861 * In fact, an earlier implementation of this function would look for
4862 * better results in the domain of res^k and for extra results in todo^k,
4863 * but this would always result in a splintering according to todo^k,
4864 * even when the domain of basic map i is disjoint from the domains of
4865 * the previous basic maps.
4867 static __isl_give isl_map
*isl_map_partial_lexopt(
4868 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
4869 __isl_give isl_set
**empty
, int max
)
4872 struct isl_map
*res
;
4873 struct isl_set
*todo
;
4878 if (isl_map_plain_is_empty(map
)) {
4886 res
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[0]),
4887 isl_set_copy(dom
), &todo
, max
);
4889 for (i
= 1; i
< map
->n
; ++i
) {
4893 isl_dim
*dim
= isl_dim_range(isl_map_get_dim(res
));
4895 res_i
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[i
]),
4896 isl_set_copy(dom
), &todo_i
, max
);
4899 lt
= isl_map_lex_lt(isl_dim_copy(dim
));
4900 le
= isl_map_lex_le(dim
);
4902 lt
= isl_map_lex_gt(isl_dim_copy(dim
));
4903 le
= isl_map_lex_ge(dim
);
4905 lt
= isl_map_apply_range(isl_map_copy(res
), lt
);
4906 lt
= isl_map_intersect(lt
, isl_map_copy(res_i
));
4907 le
= isl_map_apply_range(isl_map_copy(res_i
), le
);
4908 le
= isl_map_intersect(le
, isl_map_copy(res
));
4910 if (!isl_map_is_empty(lt
) || !isl_map_is_empty(le
)) {
4911 res
= isl_map_intersect_domain(res
,
4912 isl_set_copy(todo_i
));
4913 res_i
= isl_map_intersect_domain(res_i
,
4914 isl_set_copy(todo
));
4917 res
= isl_map_union_disjoint(res
, res_i
);
4918 res
= isl_map_union_disjoint(res
, lt
);
4919 res
= isl_map_union_disjoint(res
, le
);
4921 todo
= isl_set_intersect(todo
, todo_i
);
4941 __isl_give isl_map
*isl_map_partial_lexmax(
4942 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
4943 __isl_give isl_set
**empty
)
4945 return isl_map_partial_lexopt(map
, dom
, empty
, 1);
4948 __isl_give isl_map
*isl_map_partial_lexmin(
4949 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
4950 __isl_give isl_set
**empty
)
4952 return isl_map_partial_lexopt(map
, dom
, empty
, 0);
4955 __isl_give isl_set
*isl_set_partial_lexmin(
4956 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
4957 __isl_give isl_set
**empty
)
4959 return (struct isl_set
*)
4960 isl_map_partial_lexmin((struct isl_map
*)set
,
4964 __isl_give isl_set
*isl_set_partial_lexmax(
4965 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
4966 __isl_give isl_set
**empty
)
4968 return (struct isl_set
*)
4969 isl_map_partial_lexmax((struct isl_map
*)set
,
4973 __isl_give isl_map
*isl_basic_map_lexopt(__isl_take isl_basic_map
*bmap
, int max
)
4975 struct isl_basic_set
*dom
= NULL
;
4976 struct isl_dim
*dom_dim
;
4980 dom_dim
= isl_dim_domain(isl_dim_copy(bmap
->dim
));
4981 dom
= isl_basic_set_universe(dom_dim
);
4982 return isl_basic_map_partial_lexopt(bmap
, dom
, NULL
, max
);
4984 isl_basic_map_free(bmap
);
4988 __isl_give isl_map
*isl_basic_map_lexmin(__isl_take isl_basic_map
*bmap
)
4990 return isl_basic_map_lexopt(bmap
, 0);
4993 __isl_give isl_map
*isl_basic_map_lexmax(__isl_take isl_basic_map
*bmap
)
4995 return isl_basic_map_lexopt(bmap
, 1);
4998 __isl_give isl_set
*isl_basic_set_lexmin(__isl_take isl_basic_set
*bset
)
5000 return (isl_set
*)isl_basic_map_lexmin((isl_basic_map
*)bset
);
5003 __isl_give isl_set
*isl_basic_set_lexmax(__isl_take isl_basic_set
*bset
)
5005 return (isl_set
*)isl_basic_map_lexmax((isl_basic_map
*)bset
);
5008 __isl_give isl_map
*isl_map_lexopt(__isl_take isl_map
*map
, int max
)
5010 struct isl_set
*dom
= NULL
;
5011 struct isl_dim
*dom_dim
;
5015 dom_dim
= isl_dim_domain(isl_dim_copy(map
->dim
));
5016 dom
= isl_set_universe(dom_dim
);
5017 return isl_map_partial_lexopt(map
, dom
, NULL
, max
);
5023 __isl_give isl_map
*isl_map_lexmin(__isl_take isl_map
*map
)
5025 return isl_map_lexopt(map
, 0);
5028 __isl_give isl_map
*isl_map_lexmax(__isl_take isl_map
*map
)
5030 return isl_map_lexopt(map
, 1);
5033 __isl_give isl_set
*isl_set_lexmin(__isl_take isl_set
*set
)
5035 return (isl_set
*)isl_map_lexmin((isl_map
*)set
);
5038 __isl_give isl_set
*isl_set_lexmax(__isl_take isl_set
*set
)
5040 return (isl_set
*)isl_map_lexmax((isl_map
*)set
);
5043 /* Apply a preimage specified by "mat" on the parameters of "bset".
5044 * bset is assumed to have only parameters and divs.
5046 static struct isl_basic_set
*basic_set_parameter_preimage(
5047 struct isl_basic_set
*bset
, struct isl_mat
*mat
)
5054 bset
->dim
= isl_dim_cow(bset
->dim
);
5058 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
5060 isl_assert(bset
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
5062 bset
->dim
->nparam
= 0;
5063 bset
->dim
->n_out
= nparam
;
5064 bset
= isl_basic_set_preimage(bset
, mat
);
5066 bset
->dim
->nparam
= bset
->dim
->n_out
;
5067 bset
->dim
->n_out
= 0;
5072 isl_basic_set_free(bset
);
5076 /* Apply a preimage specified by "mat" on the parameters of "set".
5077 * set is assumed to have only parameters and divs.
5079 static struct isl_set
*set_parameter_preimage(
5080 struct isl_set
*set
, struct isl_mat
*mat
)
5082 struct isl_dim
*dim
= NULL
;
5088 dim
= isl_dim_copy(set
->dim
);
5089 dim
= isl_dim_cow(dim
);
5093 nparam
= isl_set_dim(set
, isl_dim_param
);
5095 isl_assert(set
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
5098 dim
->n_out
= nparam
;
5099 isl_set_reset_dim(set
, dim
);
5100 set
= isl_set_preimage(set
, mat
);
5103 dim
= isl_dim_copy(set
->dim
);
5104 dim
= isl_dim_cow(dim
);
5107 dim
->nparam
= dim
->n_out
;
5109 isl_set_reset_dim(set
, dim
);
5119 /* Intersect the basic set "bset" with the affine space specified by the
5120 * equalities in "eq".
5122 static struct isl_basic_set
*basic_set_append_equalities(
5123 struct isl_basic_set
*bset
, struct isl_mat
*eq
)
5131 bset
= isl_basic_set_extend_dim(bset
, isl_dim_copy(bset
->dim
), 0,
5136 len
= 1 + isl_dim_total(bset
->dim
) + bset
->extra
;
5137 for (i
= 0; i
< eq
->n_row
; ++i
) {
5138 k
= isl_basic_set_alloc_equality(bset
);
5141 isl_seq_cpy(bset
->eq
[k
], eq
->row
[i
], eq
->n_col
);
5142 isl_seq_clr(bset
->eq
[k
] + eq
->n_col
, len
- eq
->n_col
);
5146 bset
= isl_basic_set_gauss(bset
, NULL
);
5147 bset
= isl_basic_set_finalize(bset
);
5152 isl_basic_set_free(bset
);
5156 /* Intersect the set "set" with the affine space specified by the
5157 * equalities in "eq".
5159 static struct isl_set
*set_append_equalities(struct isl_set
*set
,
5167 for (i
= 0; i
< set
->n
; ++i
) {
5168 set
->p
[i
] = basic_set_append_equalities(set
->p
[i
],
5181 /* Project the given basic set onto its parameter domain, possibly introducing
5182 * new, explicit, existential variables in the constraints.
5183 * The input has parameters and (possibly implicit) existential variables.
5184 * The output has the same parameters, but only
5185 * explicit existentially quantified variables.
5187 * The actual projection is performed by pip, but pip doesn't seem
5188 * to like equalities very much, so we first remove the equalities
5189 * among the parameters by performing a variable compression on
5190 * the parameters. Afterward, an inverse transformation is performed
5191 * and the equalities among the parameters are inserted back in.
5193 static struct isl_set
*parameter_compute_divs(struct isl_basic_set
*bset
)
5197 struct isl_mat
*T
, *T2
;
5198 struct isl_set
*set
;
5199 unsigned nparam
, n_div
;
5201 bset
= isl_basic_set_cow(bset
);
5205 if (bset
->n_eq
== 0)
5206 return isl_basic_set_lexmin(bset
);
5208 isl_basic_set_gauss(bset
, NULL
);
5210 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
5211 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
5213 for (i
= 0, j
= n_div
- 1; i
< bset
->n_eq
&& j
>= 0; --j
) {
5214 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ j
]))
5217 if (i
== bset
->n_eq
)
5218 return isl_basic_set_lexmin(bset
);
5220 eq
= isl_mat_sub_alloc6(bset
->ctx
, bset
->eq
, i
, bset
->n_eq
- i
,
5222 eq
= isl_mat_cow(eq
);
5223 T
= isl_mat_variable_compression(isl_mat_copy(eq
), &T2
);
5224 if (T
&& T
->n_col
== 0) {
5228 bset
= isl_basic_set_set_to_empty(bset
);
5229 return isl_set_from_basic_set(bset
);
5231 bset
= basic_set_parameter_preimage(bset
, T
);
5233 set
= isl_basic_set_lexmin(bset
);
5234 set
= set_parameter_preimage(set
, T2
);
5235 set
= set_append_equalities(set
, eq
);
5239 /* Compute an explicit representation for all the existentially
5240 * quantified variables.
5241 * The input and output dimensions are first turned into parameters.
5242 * compute_divs then returns a map with the same parameters and
5243 * no input or output dimensions and the dimension specification
5244 * is reset to that of the input.
5246 static struct isl_map
*compute_divs(struct isl_basic_map
*bmap
)
5248 struct isl_basic_set
*bset
;
5249 struct isl_set
*set
;
5250 struct isl_map
*map
;
5251 struct isl_dim
*dim
, *orig_dim
= NULL
;
5256 bmap
= isl_basic_map_cow(bmap
);
5260 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5261 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5262 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5263 dim
= isl_dim_set_alloc(bmap
->ctx
, nparam
+ n_in
+ n_out
, 0);
5267 orig_dim
= bmap
->dim
;
5269 bset
= (struct isl_basic_set
*)bmap
;
5271 set
= parameter_compute_divs(bset
);
5272 map
= (struct isl_map
*)set
;
5273 map
= isl_map_reset_dim(map
, orig_dim
);
5277 isl_basic_map_free(bmap
);
5281 static int basic_map_divs_known(__isl_keep isl_basic_map
*bmap
)
5289 off
= isl_dim_total(bmap
->dim
);
5290 for (i
= 0; i
< bmap
->n_div
; ++i
) {
5291 if (isl_int_is_zero(bmap
->div
[i
][0]))
5293 isl_assert(bmap
->ctx
, isl_int_is_zero(bmap
->div
[i
][1+1+off
+i
]),
5299 static int map_divs_known(__isl_keep isl_map
*map
)
5306 for (i
= 0; i
< map
->n
; ++i
) {
5307 int known
= basic_map_divs_known(map
->p
[i
]);
5315 /* If bmap contains any unknown divs, then compute explicit
5316 * expressions for them. However, this computation may be
5317 * quite expensive, so first try to remove divs that aren't
5320 struct isl_map
*isl_basic_map_compute_divs(struct isl_basic_map
*bmap
)
5324 struct isl_map
*map
;
5326 known
= basic_map_divs_known(bmap
);
5330 return isl_map_from_basic_map(bmap
);
5332 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
5334 known
= basic_map_divs_known(bmap
);
5338 return isl_map_from_basic_map(bmap
);
5340 map
= compute_divs(bmap
);
5343 isl_basic_map_free(bmap
);
5347 struct isl_map
*isl_map_compute_divs(struct isl_map
*map
)
5351 struct isl_map
*res
;
5358 known
= map_divs_known(map
);
5366 res
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[0]));
5367 for (i
= 1 ; i
< map
->n
; ++i
) {
5369 r2
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[i
]));
5370 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
5371 res
= isl_map_union_disjoint(res
, r2
);
5373 res
= isl_map_union(res
, r2
);
5380 struct isl_set
*isl_basic_set_compute_divs(struct isl_basic_set
*bset
)
5382 return (struct isl_set
*)
5383 isl_basic_map_compute_divs((struct isl_basic_map
*)bset
);
5386 struct isl_set
*isl_set_compute_divs(struct isl_set
*set
)
5388 return (struct isl_set
*)
5389 isl_map_compute_divs((struct isl_map
*)set
);
5392 struct isl_set
*isl_map_domain(struct isl_map
*map
)
5395 struct isl_set
*set
;
5400 map
= isl_map_cow(map
);
5404 set
= (struct isl_set
*)map
;
5405 set
->dim
= isl_dim_domain(set
->dim
);
5408 for (i
= 0; i
< map
->n
; ++i
) {
5409 set
->p
[i
] = isl_basic_map_domain(map
->p
[i
]);
5413 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
5414 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
5421 struct isl_map
*isl_map_union_disjoint(
5422 struct isl_map
*map1
, struct isl_map
*map2
)
5426 struct isl_map
*map
= NULL
;
5440 isl_assert(map1
->ctx
, isl_dim_equal(map1
->dim
, map2
->dim
), goto error
);
5442 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
5443 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
5444 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
5446 map
= isl_map_alloc_dim(isl_dim_copy(map1
->dim
),
5447 map1
->n
+ map2
->n
, flags
);
5450 for (i
= 0; i
< map1
->n
; ++i
) {
5451 map
= isl_map_add_basic_map(map
,
5452 isl_basic_map_copy(map1
->p
[i
]));
5456 for (i
= 0; i
< map2
->n
; ++i
) {
5457 map
= isl_map_add_basic_map(map
,
5458 isl_basic_map_copy(map2
->p
[i
]));
5472 struct isl_map
*isl_map_union(struct isl_map
*map1
, struct isl_map
*map2
)
5474 map1
= isl_map_union_disjoint(map1
, map2
);
5478 ISL_F_CLR(map1
, ISL_MAP_DISJOINT
);
5482 struct isl_set
*isl_set_union_disjoint(
5483 struct isl_set
*set1
, struct isl_set
*set2
)
5485 return (struct isl_set
*)
5486 isl_map_union_disjoint(
5487 (struct isl_map
*)set1
, (struct isl_map
*)set2
);
5490 struct isl_set
*isl_set_union(struct isl_set
*set1
, struct isl_set
*set2
)
5492 return (struct isl_set
*)
5493 isl_map_union((struct isl_map
*)set1
, (struct isl_map
*)set2
);
5496 struct isl_map
*isl_map_intersect_range(
5497 struct isl_map
*map
, struct isl_set
*set
)
5500 struct isl_map
*result
;
5506 if (!isl_dim_match(map
->dim
, isl_dim_param
, set
->dim
, isl_dim_param
))
5507 isl_die(set
->ctx
, isl_error_invalid
,
5508 "parameters don't match", goto error
);
5510 if (isl_dim_size(set
->dim
, isl_dim_set
) != 0 &&
5511 !isl_map_compatible_range(map
, set
))
5512 isl_die(set
->ctx
, isl_error_invalid
,
5513 "incompatible spaces", goto error
);
5515 if (isl_set_plain_is_universe(set
)) {
5520 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
) &&
5521 ISL_F_ISSET(set
, ISL_MAP_DISJOINT
))
5522 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
5524 result
= isl_map_alloc_dim(isl_dim_copy(map
->dim
),
5525 map
->n
* set
->n
, flags
);
5528 for (i
= 0; i
< map
->n
; ++i
)
5529 for (j
= 0; j
< set
->n
; ++j
) {
5530 result
= isl_map_add_basic_map(result
,
5531 isl_basic_map_intersect_range(
5532 isl_basic_map_copy(map
->p
[i
]),
5533 isl_basic_set_copy(set
->p
[j
])));
5546 struct isl_map
*isl_map_intersect_domain(
5547 struct isl_map
*map
, struct isl_set
*set
)
5549 return isl_map_reverse(
5550 isl_map_intersect_range(isl_map_reverse(map
), set
));
5553 struct isl_map
*isl_map_apply_domain(
5554 struct isl_map
*map1
, struct isl_map
*map2
)
5558 map1
= isl_map_reverse(map1
);
5559 map1
= isl_map_apply_range(map1
, map2
);
5560 return isl_map_reverse(map1
);
5567 struct isl_map
*isl_map_apply_range(
5568 struct isl_map
*map1
, struct isl_map
*map2
)
5570 struct isl_dim
*dim_result
;
5571 struct isl_map
*result
;
5577 dim_result
= isl_dim_join(isl_dim_copy(map1
->dim
),
5578 isl_dim_copy(map2
->dim
));
5580 result
= isl_map_alloc_dim(dim_result
, map1
->n
* map2
->n
, 0);
5583 for (i
= 0; i
< map1
->n
; ++i
)
5584 for (j
= 0; j
< map2
->n
; ++j
) {
5585 result
= isl_map_add_basic_map(result
,
5586 isl_basic_map_apply_range(
5587 isl_basic_map_copy(map1
->p
[i
]),
5588 isl_basic_map_copy(map2
->p
[j
])));
5594 if (result
&& result
->n
<= 1)
5595 ISL_F_SET(result
, ISL_MAP_DISJOINT
);
5604 * returns range - domain
5606 struct isl_basic_set
*isl_basic_map_deltas(struct isl_basic_map
*bmap
)
5608 isl_dim
*dims
, *target_dim
;
5609 struct isl_basic_set
*bset
;
5616 isl_assert(bmap
->ctx
, isl_dim_tuple_match(bmap
->dim
, isl_dim_in
,
5617 bmap
->dim
, isl_dim_out
),
5619 target_dim
= isl_dim_domain(isl_basic_map_get_dim(bmap
));
5620 dim
= isl_basic_map_n_in(bmap
);
5621 nparam
= isl_basic_map_n_param(bmap
);
5622 bset
= isl_basic_set_from_basic_map(bmap
);
5623 bset
= isl_basic_set_cow(bset
);
5624 dims
= isl_basic_set_get_dim(bset
);
5625 dims
= isl_dim_add(dims
, isl_dim_set
, dim
);
5626 bset
= isl_basic_set_extend_dim(bset
, dims
, 0, dim
, 0);
5627 bset
= isl_basic_set_swap_vars(bset
, 2*dim
);
5628 for (i
= 0; i
< dim
; ++i
) {
5629 int j
= isl_basic_map_alloc_equality(
5630 (struct isl_basic_map
*)bset
);
5633 isl_seq_clr(bset
->eq
[j
], 1 + isl_basic_set_total_dim(bset
));
5634 isl_int_set_si(bset
->eq
[j
][1+nparam
+i
], 1);
5635 isl_int_set_si(bset
->eq
[j
][1+nparam
+dim
+i
], 1);
5636 isl_int_set_si(bset
->eq
[j
][1+nparam
+2*dim
+i
], -1);
5638 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, dim
, 2*dim
);
5639 bset
= isl_basic_set_reset_dim(bset
, target_dim
);
5642 isl_basic_map_free(bmap
);
5647 * returns range - domain
5649 struct isl_set
*isl_map_deltas(struct isl_map
*map
)
5653 struct isl_set
*result
;
5658 isl_assert(map
->ctx
, isl_dim_tuple_match(map
->dim
, isl_dim_in
,
5659 map
->dim
, isl_dim_out
),
5661 dim
= isl_map_get_dim(map
);
5662 dim
= isl_dim_domain(dim
);
5663 result
= isl_set_alloc_dim(dim
, map
->n
, 0);
5666 for (i
= 0; i
< map
->n
; ++i
)
5667 result
= isl_set_add_basic_set(result
,
5668 isl_basic_map_deltas(isl_basic_map_copy(map
->p
[i
])));
5677 * returns [domain -> range] -> range - domain
5679 __isl_give isl_basic_map
*isl_basic_map_deltas_map(
5680 __isl_take isl_basic_map
*bmap
)
5684 isl_basic_map
*domain
;
5685 isl_basic_set
*bset
;
5689 if (!isl_dim_tuple_match(bmap
->dim
, isl_dim_in
, bmap
->dim
, isl_dim_out
))
5690 isl_die(bmap
->ctx
, isl_error_invalid
,
5691 "domain and range don't match", goto error
);
5693 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5694 n
= isl_basic_map_dim(bmap
, isl_dim_in
);
5696 dim
= isl_dim_from_range(isl_dim_domain(isl_basic_map_get_dim(bmap
)));
5697 domain
= isl_basic_map_universe(dim
);
5699 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
5700 bmap
= isl_basic_map_apply_range(bmap
, domain
);
5701 bmap
= isl_basic_map_extend_constraints(bmap
, n
, 0);
5703 total
= isl_basic_map_total_dim(bmap
);
5705 for (i
= 0; i
< n
; ++i
) {
5706 k
= isl_basic_map_alloc_equality(bmap
);
5709 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
5710 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], 1);
5711 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ i
], -1);
5712 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ n
+ i
], 1);
5715 bmap
= isl_basic_map_gauss(bmap
, NULL
);
5716 return isl_basic_map_finalize(bmap
);
5718 isl_basic_map_free(bmap
);
5723 * returns [domain -> range] -> range - domain
5725 __isl_give isl_map
*isl_map_deltas_map(__isl_take isl_map
*map
)
5728 isl_dim
*domain_dim
;
5733 if (!isl_dim_tuple_match(map
->dim
, isl_dim_in
, map
->dim
, isl_dim_out
))
5734 isl_die(map
->ctx
, isl_error_invalid
,
5735 "domain and range don't match", goto error
);
5737 map
= isl_map_cow(map
);
5741 domain_dim
= isl_dim_from_range(isl_dim_domain(isl_map_get_dim(map
)));
5742 map
->dim
= isl_dim_from_domain(isl_dim_wrap(map
->dim
));
5743 map
->dim
= isl_dim_join(map
->dim
, domain_dim
);
5746 for (i
= 0; i
< map
->n
; ++i
) {
5747 map
->p
[i
] = isl_basic_map_deltas_map(map
->p
[i
]);
5751 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5758 static struct isl_basic_map
*basic_map_identity(struct isl_dim
*dims
)
5760 struct isl_basic_map
*bmap
;
5768 nparam
= dims
->nparam
;
5770 bmap
= isl_basic_map_alloc_dim(dims
, 0, dim
, 0);
5774 for (i
= 0; i
< dim
; ++i
) {
5775 int j
= isl_basic_map_alloc_equality(bmap
);
5778 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
5779 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
5780 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], -1);
5782 return isl_basic_map_finalize(bmap
);
5784 isl_basic_map_free(bmap
);
5788 __isl_give isl_basic_map
*isl_basic_map_identity(__isl_take isl_dim
*dim
)
5792 if (dim
->n_in
!= dim
->n_out
)
5793 isl_die(dim
->ctx
, isl_error_invalid
,
5794 "number of input and output dimensions needs to be "
5795 "the same", goto error
);
5796 return basic_map_identity(dim
);
5802 struct isl_basic_map
*isl_basic_map_identity_like(struct isl_basic_map
*model
)
5804 if (!model
|| !model
->dim
)
5806 return isl_basic_map_identity(isl_dim_copy(model
->dim
));
5809 __isl_give isl_map
*isl_map_identity(__isl_take isl_dim
*dim
)
5811 return isl_map_from_basic_map(isl_basic_map_identity(dim
));
5814 struct isl_map
*isl_map_identity_like(struct isl_map
*model
)
5816 if (!model
|| !model
->dim
)
5818 return isl_map_identity(isl_dim_copy(model
->dim
));
5821 struct isl_map
*isl_map_identity_like_basic_map(struct isl_basic_map
*model
)
5823 if (!model
|| !model
->dim
)
5825 return isl_map_identity(isl_dim_copy(model
->dim
));
5828 __isl_give isl_map
*isl_set_identity(__isl_take isl_set
*set
)
5830 isl_dim
*dim
= isl_set_get_dim(set
);
5832 id
= isl_map_identity(isl_dim_map_from_set(dim
));
5833 return isl_map_intersect_range(id
, set
);
5836 /* Construct a basic set with all set dimensions having only non-negative
5839 struct isl_basic_set
*isl_basic_set_positive_orthant(struct isl_dim
*dims
)
5844 struct isl_basic_set
*bset
;
5848 nparam
= dims
->nparam
;
5850 bset
= isl_basic_set_alloc_dim(dims
, 0, 0, dim
);
5853 for (i
= 0; i
< dim
; ++i
) {
5854 int k
= isl_basic_set_alloc_inequality(bset
);
5857 isl_seq_clr(bset
->ineq
[k
], 1 + isl_basic_set_total_dim(bset
));
5858 isl_int_set_si(bset
->ineq
[k
][1 + nparam
+ i
], 1);
5862 isl_basic_set_free(bset
);
5866 /* Construct the half-space x_pos >= 0.
5868 static __isl_give isl_basic_set
*nonneg_halfspace(__isl_take isl_dim
*dim
,
5872 isl_basic_set
*nonneg
;
5874 nonneg
= isl_basic_set_alloc_dim(dim
, 0, 0, 1);
5875 k
= isl_basic_set_alloc_inequality(nonneg
);
5878 isl_seq_clr(nonneg
->ineq
[k
], 1 + isl_basic_set_total_dim(nonneg
));
5879 isl_int_set_si(nonneg
->ineq
[k
][pos
], 1);
5881 return isl_basic_set_finalize(nonneg
);
5883 isl_basic_set_free(nonneg
);
5887 /* Construct the half-space x_pos <= -1.
5889 static __isl_give isl_basic_set
*neg_halfspace(__isl_take isl_dim
*dim
, int pos
)
5894 neg
= isl_basic_set_alloc_dim(dim
, 0, 0, 1);
5895 k
= isl_basic_set_alloc_inequality(neg
);
5898 isl_seq_clr(neg
->ineq
[k
], 1 + isl_basic_set_total_dim(neg
));
5899 isl_int_set_si(neg
->ineq
[k
][0], -1);
5900 isl_int_set_si(neg
->ineq
[k
][pos
], -1);
5902 return isl_basic_set_finalize(neg
);
5904 isl_basic_set_free(neg
);
5908 __isl_give isl_set
*isl_set_split_dims(__isl_take isl_set
*set
,
5909 enum isl_dim_type type
, unsigned first
, unsigned n
)
5912 isl_basic_set
*nonneg
;
5920 isl_assert(set
->ctx
, first
+ n
<= isl_set_dim(set
, type
), goto error
);
5922 for (i
= 0; i
< n
; ++i
) {
5923 nonneg
= nonneg_halfspace(isl_set_get_dim(set
),
5924 pos(set
->dim
, type
) + first
+ i
);
5925 neg
= neg_halfspace(isl_set_get_dim(set
),
5926 pos(set
->dim
, type
) + first
+ i
);
5928 set
= isl_set_intersect(set
, isl_basic_set_union(nonneg
, neg
));
5937 static int foreach_orthant(__isl_take isl_set
*set
, int *signs
, int first
,
5938 int len
, int (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
5945 if (isl_set_plain_is_empty(set
)) {
5950 return fn(set
, signs
, user
);
5953 half
= isl_set_from_basic_set(nonneg_halfspace(isl_set_get_dim(set
),
5955 half
= isl_set_intersect(half
, isl_set_copy(set
));
5956 if (foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
) < 0)
5960 half
= isl_set_from_basic_set(neg_halfspace(isl_set_get_dim(set
),
5962 half
= isl_set_intersect(half
, set
);
5963 return foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
);
5969 /* Call "fn" on the intersections of "set" with each of the orthants
5970 * (except for obviously empty intersections). The orthant is identified
5971 * by the signs array, with each entry having value 1 or -1 according
5972 * to the sign of the corresponding variable.
5974 int isl_set_foreach_orthant(__isl_keep isl_set
*set
,
5975 int (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
5985 if (isl_set_plain_is_empty(set
))
5988 nparam
= isl_set_dim(set
, isl_dim_param
);
5989 nvar
= isl_set_dim(set
, isl_dim_set
);
5991 signs
= isl_alloc_array(set
->ctx
, int, nparam
+ nvar
);
5993 r
= foreach_orthant(isl_set_copy(set
), signs
, 0, nparam
+ nvar
,
6001 int isl_set_is_equal(struct isl_set
*set1
, struct isl_set
*set2
)
6003 return isl_map_is_equal((struct isl_map
*)set1
, (struct isl_map
*)set2
);
6006 int isl_basic_map_is_subset(
6007 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
6010 struct isl_map
*map1
;
6011 struct isl_map
*map2
;
6013 if (!bmap1
|| !bmap2
)
6016 map1
= isl_map_from_basic_map(isl_basic_map_copy(bmap1
));
6017 map2
= isl_map_from_basic_map(isl_basic_map_copy(bmap2
));
6019 is_subset
= isl_map_is_subset(map1
, map2
);
6027 int isl_basic_map_is_equal(
6028 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
6032 if (!bmap1
|| !bmap2
)
6034 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
6037 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
6041 int isl_basic_set_is_equal(
6042 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
6044 return isl_basic_map_is_equal(
6045 (struct isl_basic_map
*)bset1
, (struct isl_basic_map
*)bset2
);
6048 int isl_map_is_empty(struct isl_map
*map
)
6055 for (i
= 0; i
< map
->n
; ++i
) {
6056 is_empty
= isl_basic_map_is_empty(map
->p
[i
]);
6065 int isl_map_plain_is_empty(__isl_keep isl_map
*map
)
6067 return map
? map
->n
== 0 : -1;
6070 int isl_map_fast_is_empty(__isl_keep isl_map
*map
)
6072 return isl_map_plain_is_empty(map
);
6075 int isl_set_plain_is_empty(struct isl_set
*set
)
6077 return set
? set
->n
== 0 : -1;
6080 int isl_set_fast_is_empty(__isl_keep isl_set
*set
)
6082 return isl_set_plain_is_empty(set
);
6085 int isl_set_is_empty(struct isl_set
*set
)
6087 return isl_map_is_empty((struct isl_map
*)set
);
6090 int isl_map_has_equal_dim(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
6095 return isl_dim_equal(map1
->dim
, map2
->dim
);
6098 int isl_set_has_equal_dim(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
6103 return isl_dim_equal(set1
->dim
, set2
->dim
);
6106 int isl_map_is_equal(struct isl_map
*map1
, struct isl_map
*map2
)
6112 is_subset
= isl_map_is_subset(map1
, map2
);
6115 is_subset
= isl_map_is_subset(map2
, map1
);
6119 int isl_basic_map_is_strict_subset(
6120 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
6124 if (!bmap1
|| !bmap2
)
6126 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
6129 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
6130 if (is_subset
== -1)
6135 int isl_map_is_strict_subset(struct isl_map
*map1
, struct isl_map
*map2
)
6141 is_subset
= isl_map_is_subset(map1
, map2
);
6144 is_subset
= isl_map_is_subset(map2
, map1
);
6145 if (is_subset
== -1)
6150 int isl_set_is_strict_subset(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
6152 return isl_map_is_strict_subset((isl_map
*)set1
, (isl_map
*)set2
);
6155 int isl_basic_map_is_universe(struct isl_basic_map
*bmap
)
6159 return bmap
->n_eq
== 0 && bmap
->n_ineq
== 0;
6162 int isl_basic_set_is_universe(struct isl_basic_set
*bset
)
6166 return bset
->n_eq
== 0 && bset
->n_ineq
== 0;
6169 int isl_map_plain_is_universe(__isl_keep isl_map
*map
)
6176 for (i
= 0; i
< map
->n
; ++i
) {
6177 int r
= isl_basic_map_is_universe(map
->p
[i
]);
6185 int isl_set_plain_is_universe(__isl_keep isl_set
*set
)
6187 return isl_map_plain_is_universe((isl_map
*) set
);
6190 int isl_set_fast_is_universe(__isl_keep isl_set
*set
)
6192 return isl_set_plain_is_universe(set
);
6195 int isl_basic_map_is_empty(struct isl_basic_map
*bmap
)
6197 struct isl_basic_set
*bset
= NULL
;
6198 struct isl_vec
*sample
= NULL
;
6205 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
6208 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
6209 struct isl_basic_map
*copy
= isl_basic_map_copy(bmap
);
6210 copy
= isl_basic_map_remove_redundancies(copy
);
6211 empty
= ISL_F_ISSET(copy
, ISL_BASIC_MAP_EMPTY
);
6212 isl_basic_map_free(copy
);
6216 total
= 1 + isl_basic_map_total_dim(bmap
);
6217 if (bmap
->sample
&& bmap
->sample
->size
== total
) {
6218 int contains
= isl_basic_map_contains(bmap
, bmap
->sample
);
6224 isl_vec_free(bmap
->sample
);
6225 bmap
->sample
= NULL
;
6226 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
6229 sample
= isl_basic_set_sample_vec(bset
);
6232 empty
= sample
->size
== 0;
6233 isl_vec_free(bmap
->sample
);
6234 bmap
->sample
= sample
;
6236 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
6241 int isl_basic_map_plain_is_empty(__isl_keep isl_basic_map
*bmap
)
6245 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
);
6248 int isl_basic_map_fast_is_empty(__isl_keep isl_basic_map
*bmap
)
6250 return isl_basic_map_plain_is_empty(bmap
);
6253 int isl_basic_set_plain_is_empty(__isl_keep isl_basic_set
*bset
)
6257 return ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
);
6260 int isl_basic_set_fast_is_empty(__isl_keep isl_basic_set
*bset
)
6262 return isl_basic_set_plain_is_empty(bset
);
6265 int isl_basic_set_is_empty(struct isl_basic_set
*bset
)
6267 return isl_basic_map_is_empty((struct isl_basic_map
*)bset
);
6270 struct isl_map
*isl_basic_map_union(
6271 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
6273 struct isl_map
*map
;
6274 if (!bmap1
|| !bmap2
)
6277 isl_assert(bmap1
->ctx
, isl_dim_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
6279 map
= isl_map_alloc_dim(isl_dim_copy(bmap1
->dim
), 2, 0);
6282 map
= isl_map_add_basic_map(map
, bmap1
);
6283 map
= isl_map_add_basic_map(map
, bmap2
);
6286 isl_basic_map_free(bmap1
);
6287 isl_basic_map_free(bmap2
);
6291 struct isl_set
*isl_basic_set_union(
6292 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
6294 return (struct isl_set
*)isl_basic_map_union(
6295 (struct isl_basic_map
*)bset1
,
6296 (struct isl_basic_map
*)bset2
);
6299 /* Order divs such that any div only depends on previous divs */
6300 struct isl_basic_map
*isl_basic_map_order_divs(struct isl_basic_map
*bmap
)
6308 off
= isl_dim_total(bmap
->dim
);
6310 for (i
= 0; i
< bmap
->n_div
; ++i
) {
6312 if (isl_int_is_zero(bmap
->div
[i
][0]))
6314 pos
= isl_seq_first_non_zero(bmap
->div
[i
]+1+1+off
+i
,
6318 isl_basic_map_swap_div(bmap
, i
, i
+ pos
);
6324 struct isl_basic_set
*isl_basic_set_order_divs(struct isl_basic_set
*bset
)
6326 return (struct isl_basic_set
*)
6327 isl_basic_map_order_divs((struct isl_basic_map
*)bset
);
6330 __isl_give isl_map
*isl_map_order_divs(__isl_take isl_map
*map
)
6337 for (i
= 0; i
< map
->n
; ++i
) {
6338 map
->p
[i
] = isl_basic_map_order_divs(map
->p
[i
]);
6349 /* Apply the expansion computed by isl_merge_divs.
6350 * The expansion itself is given by "exp" while the resulting
6351 * list of divs is given by "div".
6353 __isl_give isl_basic_set
*isl_basic_set_expand_divs(
6354 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*div
, int *exp
)
6359 bset
= isl_basic_set_cow(bset
);
6363 if (div
->n_row
< bset
->n_div
)
6364 isl_die(isl_mat_get_ctx(div
), isl_error_invalid
,
6365 "not an expansion", goto error
);
6367 bset
= isl_basic_map_extend_dim(bset
, isl_dim_copy(bset
->dim
),
6368 div
->n_row
- bset
->n_div
, 0,
6369 2 * (div
->n_row
- bset
->n_div
));
6371 n_div
= bset
->n_div
;
6372 for (i
= n_div
; i
< div
->n_row
; ++i
)
6373 if (isl_basic_set_alloc_div(bset
) < 0)
6377 for (i
= div
->n_row
- 1; i
>= 0; --i
) {
6378 if (j
>= 0 && exp
[j
] == i
) {
6380 isl_basic_map_swap_div(bset
, i
, j
);
6383 isl_seq_cpy(bset
->div
[i
], div
->row
[i
], div
->n_col
);
6384 if (isl_basic_map_add_div_constraints(bset
, i
) < 0)
6392 isl_basic_set_free(bset
);
6397 /* Look for a div in dst that corresponds to the div "div" in src.
6398 * The divs before "div" in src and dst are assumed to be the same.
6400 * Returns -1 if no corresponding div was found and the position
6401 * of the corresponding div in dst otherwise.
6403 static int find_div(struct isl_basic_map
*dst
,
6404 struct isl_basic_map
*src
, unsigned div
)
6408 unsigned total
= isl_dim_total(src
->dim
);
6410 isl_assert(dst
->ctx
, div
<= dst
->n_div
, return -1);
6411 for (i
= div
; i
< dst
->n_div
; ++i
)
6412 if (isl_seq_eq(dst
->div
[i
], src
->div
[div
], 1+1+total
+div
) &&
6413 isl_seq_first_non_zero(dst
->div
[i
]+1+1+total
+div
,
6414 dst
->n_div
- div
) == -1)
6419 struct isl_basic_map
*isl_basic_map_align_divs(
6420 struct isl_basic_map
*dst
, struct isl_basic_map
*src
)
6423 unsigned total
= isl_dim_total(src
->dim
);
6428 if (src
->n_div
== 0)
6431 for (i
= 0; i
< src
->n_div
; ++i
)
6432 isl_assert(src
->ctx
, !isl_int_is_zero(src
->div
[i
][0]), goto error
);
6434 src
= isl_basic_map_order_divs(src
);
6435 dst
= isl_basic_map_cow(dst
);
6436 dst
= isl_basic_map_extend_dim(dst
, isl_dim_copy(dst
->dim
),
6437 src
->n_div
, 0, 2 * src
->n_div
);
6440 for (i
= 0; i
< src
->n_div
; ++i
) {
6441 int j
= find_div(dst
, src
, i
);
6443 j
= isl_basic_map_alloc_div(dst
);
6446 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
+i
);
6447 isl_seq_clr(dst
->div
[j
]+1+1+total
+i
, dst
->n_div
- i
);
6448 if (isl_basic_map_add_div_constraints(dst
, j
) < 0)
6452 isl_basic_map_swap_div(dst
, i
, j
);
6456 isl_basic_map_free(dst
);
6460 struct isl_basic_set
*isl_basic_set_align_divs(
6461 struct isl_basic_set
*dst
, struct isl_basic_set
*src
)
6463 return (struct isl_basic_set
*)isl_basic_map_align_divs(
6464 (struct isl_basic_map
*)dst
, (struct isl_basic_map
*)src
);
6467 struct isl_map
*isl_map_align_divs(struct isl_map
*map
)
6475 map
= isl_map_compute_divs(map
);
6476 map
= isl_map_cow(map
);
6480 for (i
= 1; i
< map
->n
; ++i
)
6481 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], map
->p
[i
]);
6482 for (i
= 1; i
< map
->n
; ++i
)
6483 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], map
->p
[0]);
6485 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6489 struct isl_set
*isl_set_align_divs(struct isl_set
*set
)
6491 return (struct isl_set
*)isl_map_align_divs((struct isl_map
*)set
);
6494 struct isl_set
*isl_set_apply(struct isl_set
*set
, struct isl_map
*map
)
6498 isl_assert(set
->ctx
, isl_map_compatible_domain(map
, set
), goto error
);
6499 map
= isl_map_intersect_domain(map
, set
);
6500 set
= isl_map_range(map
);
6508 /* There is no need to cow as removing empty parts doesn't change
6509 * the meaning of the set.
6511 struct isl_map
*isl_map_remove_empty_parts(struct isl_map
*map
)
6518 for (i
= map
->n
-1; i
>= 0; --i
) {
6519 if (!ISL_F_ISSET(map
->p
[i
], ISL_BASIC_MAP_EMPTY
))
6521 isl_basic_map_free(map
->p
[i
]);
6522 if (i
!= map
->n
-1) {
6523 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6524 map
->p
[i
] = map
->p
[map
->n
-1];
6532 struct isl_set
*isl_set_remove_empty_parts(struct isl_set
*set
)
6534 return (struct isl_set
*)
6535 isl_map_remove_empty_parts((struct isl_map
*)set
);
6538 struct isl_basic_map
*isl_map_copy_basic_map(struct isl_map
*map
)
6540 struct isl_basic_map
*bmap
;
6541 if (!map
|| map
->n
== 0)
6543 bmap
= map
->p
[map
->n
-1];
6544 isl_assert(map
->ctx
, ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
), return NULL
);
6545 return isl_basic_map_copy(bmap
);
6548 struct isl_basic_set
*isl_set_copy_basic_set(struct isl_set
*set
)
6550 return (struct isl_basic_set
*)
6551 isl_map_copy_basic_map((struct isl_map
*)set
);
6554 __isl_give isl_map
*isl_map_drop_basic_map(__isl_take isl_map
*map
,
6555 __isl_keep isl_basic_map
*bmap
)
6561 for (i
= map
->n
-1; i
>= 0; --i
) {
6562 if (map
->p
[i
] != bmap
)
6564 map
= isl_map_cow(map
);
6567 isl_basic_map_free(map
->p
[i
]);
6568 if (i
!= map
->n
-1) {
6569 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
6570 map
->p
[i
] = map
->p
[map
->n
-1];
6581 struct isl_set
*isl_set_drop_basic_set(struct isl_set
*set
,
6582 struct isl_basic_set
*bset
)
6584 return (struct isl_set
*)isl_map_drop_basic_map((struct isl_map
*)set
,
6585 (struct isl_basic_map
*)bset
);
6588 /* Given two basic sets bset1 and bset2, compute the maximal difference
6589 * between the values of dimension pos in bset1 and those in bset2
6590 * for any common value of the parameters and dimensions preceding pos.
6592 static enum isl_lp_result
basic_set_maximal_difference_at(
6593 __isl_keep isl_basic_set
*bset1
, __isl_keep isl_basic_set
*bset2
,
6594 int pos
, isl_int
*opt
)
6596 struct isl_dim
*dims
;
6597 struct isl_basic_map
*bmap1
= NULL
;
6598 struct isl_basic_map
*bmap2
= NULL
;
6599 struct isl_ctx
*ctx
;
6600 struct isl_vec
*obj
;
6603 unsigned dim1
, dim2
;
6604 enum isl_lp_result res
;
6606 if (!bset1
|| !bset2
)
6607 return isl_lp_error
;
6609 nparam
= isl_basic_set_n_param(bset1
);
6610 dim1
= isl_basic_set_n_dim(bset1
);
6611 dim2
= isl_basic_set_n_dim(bset2
);
6612 dims
= isl_dim_alloc(bset1
->ctx
, nparam
, pos
, dim1
- pos
);
6613 bmap1
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset1
), dims
);
6614 dims
= isl_dim_alloc(bset2
->ctx
, nparam
, pos
, dim2
- pos
);
6615 bmap2
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset2
), dims
);
6616 if (!bmap1
|| !bmap2
)
6618 bmap1
= isl_basic_map_cow(bmap1
);
6619 bmap1
= isl_basic_map_extend(bmap1
, nparam
,
6620 pos
, (dim1
- pos
) + (dim2
- pos
),
6621 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
6622 bmap1
= add_constraints(bmap1
, bmap2
, 0, dim1
- pos
);
6625 total
= isl_basic_map_total_dim(bmap1
);
6627 obj
= isl_vec_alloc(ctx
, 1 + total
);
6628 isl_seq_clr(obj
->block
.data
, 1 + total
);
6629 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
], 1);
6630 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
+(dim1
-pos
)], -1);
6633 res
= isl_basic_map_solve_lp(bmap1
, 1, obj
->block
.data
, ctx
->one
,
6635 isl_basic_map_free(bmap1
);
6639 isl_basic_map_free(bmap1
);
6640 isl_basic_map_free(bmap2
);
6641 return isl_lp_error
;
6644 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
6645 * for any common value of the parameters and dimensions preceding pos
6646 * in both basic sets, the values of dimension pos in bset1 are
6647 * smaller or larger than those in bset2.
6650 * 1 if bset1 follows bset2
6651 * -1 if bset1 precedes bset2
6652 * 0 if bset1 and bset2 are incomparable
6653 * -2 if some error occurred.
6655 int isl_basic_set_compare_at(struct isl_basic_set
*bset1
,
6656 struct isl_basic_set
*bset2
, int pos
)
6659 enum isl_lp_result res
;
6664 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
6666 if (res
== isl_lp_empty
)
6668 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
6669 res
== isl_lp_unbounded
)
6671 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
6680 /* Given two basic sets bset1 and bset2, check whether
6681 * for any common value of the parameters and dimensions preceding pos
6682 * there is a value of dimension pos in bset1 that is larger
6683 * than a value of the same dimension in bset2.
6686 * 1 if there exists such a pair
6687 * 0 if there is no such pair, but there is a pair of equal values
6689 * -2 if some error occurred.
6691 int isl_basic_set_follows_at(__isl_keep isl_basic_set
*bset1
,
6692 __isl_keep isl_basic_set
*bset2
, int pos
)
6695 enum isl_lp_result res
;
6700 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
6702 if (res
== isl_lp_empty
)
6704 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
6705 res
== isl_lp_unbounded
)
6707 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
6709 else if (res
== isl_lp_ok
)
6718 /* Given two sets set1 and set2, check whether
6719 * for any common value of the parameters and dimensions preceding pos
6720 * there is a value of dimension pos in set1 that is larger
6721 * than a value of the same dimension in set2.
6724 * 1 if there exists such a pair
6725 * 0 if there is no such pair, but there is a pair of equal values
6727 * -2 if some error occurred.
6729 int isl_set_follows_at(__isl_keep isl_set
*set1
,
6730 __isl_keep isl_set
*set2
, int pos
)
6738 for (i
= 0; i
< set1
->n
; ++i
)
6739 for (j
= 0; j
< set2
->n
; ++j
) {
6741 f
= isl_basic_set_follows_at(set1
->p
[i
], set2
->p
[j
], pos
);
6742 if (f
== 1 || f
== -2)
6751 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map
*bmap
,
6752 unsigned pos
, isl_int
*val
)
6760 total
= isl_basic_map_total_dim(bmap
);
6761 for (i
= 0, d
= total
-1; i
< bmap
->n_eq
&& d
+1 > pos
; ++i
) {
6762 for (; d
+1 > pos
; --d
)
6763 if (!isl_int_is_zero(bmap
->eq
[i
][1+d
]))
6767 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1, d
) != -1)
6769 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1+d
+1, total
-d
-1) != -1)
6771 if (!isl_int_is_one(bmap
->eq
[i
][1+d
]))
6774 isl_int_neg(*val
, bmap
->eq
[i
][0]);
6780 static int isl_map_plain_has_fixed_var(__isl_keep isl_map
*map
,
6781 unsigned pos
, isl_int
*val
)
6793 return isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, val
);
6796 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, &v
);
6797 for (i
= 1; fixed
== 1 && i
< map
->n
; ++i
) {
6798 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[i
], pos
, &tmp
);
6799 if (fixed
== 1 && isl_int_ne(tmp
, v
))
6803 isl_int_set(*val
, v
);
6809 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set
*bset
,
6810 unsigned pos
, isl_int
*val
)
6812 return isl_basic_map_plain_has_fixed_var((struct isl_basic_map
*)bset
,
6816 static int isl_set_plain_has_fixed_var(__isl_keep isl_set
*set
, unsigned pos
,
6819 return isl_map_plain_has_fixed_var((struct isl_map
*)set
, pos
, val
);
6822 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map
*bmap
,
6823 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
6825 if (pos
>= isl_basic_map_dim(bmap
, type
))
6827 return isl_basic_map_plain_has_fixed_var(bmap
,
6828 isl_basic_map_offset(bmap
, type
) - 1 + pos
, val
);
6831 int isl_map_plain_is_fixed(__isl_keep isl_map
*map
,
6832 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
6834 if (pos
>= isl_map_dim(map
, type
))
6836 return isl_map_plain_has_fixed_var(map
,
6837 map_offset(map
, type
) - 1 + pos
, val
);
6840 int isl_map_fast_is_fixed(__isl_keep isl_map
*map
,
6841 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
6843 return isl_map_plain_is_fixed(map
, type
, pos
, val
);
6846 /* Check if dimension dim has fixed value and if so and if val is not NULL,
6847 * then return this fixed value in *val.
6849 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set
*bset
,
6850 unsigned dim
, isl_int
*val
)
6852 return isl_basic_set_plain_has_fixed_var(bset
,
6853 isl_basic_set_n_param(bset
) + dim
, val
);
6856 /* Check if dimension dim has fixed value and if so and if val is not NULL,
6857 * then return this fixed value in *val.
6859 int isl_set_plain_dim_is_fixed(__isl_keep isl_set
*set
,
6860 unsigned dim
, isl_int
*val
)
6862 return isl_set_plain_has_fixed_var(set
, isl_set_n_param(set
) + dim
, val
);
6865 int isl_set_fast_dim_is_fixed(__isl_keep isl_set
*set
,
6866 unsigned dim
, isl_int
*val
)
6868 return isl_set_plain_dim_is_fixed(set
, dim
, val
);
6871 /* Check if input variable in has fixed value and if so and if val is not NULL,
6872 * then return this fixed value in *val.
6874 int isl_map_plain_input_is_fixed(__isl_keep isl_map
*map
,
6875 unsigned in
, isl_int
*val
)
6877 return isl_map_plain_has_fixed_var(map
, isl_map_n_param(map
) + in
, val
);
6880 /* Check if dimension dim has an (obvious) fixed lower bound and if so
6881 * and if val is not NULL, then return this lower bound in *val.
6883 int isl_basic_set_plain_dim_has_fixed_lower_bound(
6884 __isl_keep isl_basic_set
*bset
, unsigned dim
, isl_int
*val
)
6886 int i
, i_eq
= -1, i_ineq
= -1;
6893 total
= isl_basic_set_total_dim(bset
);
6894 nparam
= isl_basic_set_n_param(bset
);
6895 for (i
= 0; i
< bset
->n_eq
; ++i
) {
6896 if (isl_int_is_zero(bset
->eq
[i
][1+nparam
+dim
]))
6902 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
6903 if (!isl_int_is_pos(bset
->ineq
[i
][1+nparam
+dim
]))
6905 if (i_eq
!= -1 || i_ineq
!= -1)
6909 if (i_eq
== -1 && i_ineq
== -1)
6911 c
= i_eq
!= -1 ? bset
->eq
[i_eq
] : bset
->ineq
[i_ineq
];
6912 /* The coefficient should always be one due to normalization. */
6913 if (!isl_int_is_one(c
[1+nparam
+dim
]))
6915 if (isl_seq_first_non_zero(c
+1, nparam
+dim
) != -1)
6917 if (isl_seq_first_non_zero(c
+1+nparam
+dim
+1,
6918 total
- nparam
- dim
- 1) != -1)
6921 isl_int_neg(*val
, c
[0]);
6925 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set
*set
,
6926 unsigned dim
, isl_int
*val
)
6938 return isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[0],
6942 fixed
= isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[0],
6944 for (i
= 1; fixed
== 1 && i
< set
->n
; ++i
) {
6945 fixed
= isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[i
],
6947 if (fixed
== 1 && isl_int_ne(tmp
, v
))
6951 isl_int_set(*val
, v
);
6962 /* uset_gist depends on constraints without existentially quantified
6963 * variables sorting first.
6965 static int qsort_constraint_cmp(const void *p1
, const void *p2
)
6967 const struct constraint
*c1
= (const struct constraint
*)p1
;
6968 const struct constraint
*c2
= (const struct constraint
*)p2
;
6970 unsigned size
= isl_min(c1
->size
, c2
->size
);
6972 l1
= isl_seq_last_non_zero(c1
->c
, size
);
6973 l2
= isl_seq_last_non_zero(c2
->c
, size
);
6978 return isl_seq_cmp(c1
->c
, c2
->c
, size
);
6981 static struct isl_basic_map
*isl_basic_map_sort_constraints(
6982 struct isl_basic_map
*bmap
)
6985 struct constraint
*c
;
6990 total
= isl_basic_map_total_dim(bmap
);
6991 c
= isl_alloc_array(bmap
->ctx
, struct constraint
, bmap
->n_ineq
);
6994 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
6996 c
[i
].c
= bmap
->ineq
[i
];
6998 qsort(c
, bmap
->n_ineq
, sizeof(struct constraint
), qsort_constraint_cmp
);
6999 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
7000 bmap
->ineq
[i
] = c
[i
].c
;
7004 isl_basic_map_free(bmap
);
7008 __isl_give isl_basic_set
*isl_basic_set_sort_constraints(
7009 __isl_take isl_basic_set
*bset
)
7011 return (struct isl_basic_set
*)isl_basic_map_sort_constraints(
7012 (struct isl_basic_map
*)bset
);
7015 struct isl_basic_map
*isl_basic_map_normalize(struct isl_basic_map
*bmap
)
7019 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED
))
7021 bmap
= isl_basic_map_remove_redundancies(bmap
);
7022 bmap
= isl_basic_map_sort_constraints(bmap
);
7023 ISL_F_SET(bmap
, ISL_BASIC_MAP_NORMALIZED
);
7027 struct isl_basic_set
*isl_basic_set_normalize(struct isl_basic_set
*bset
)
7029 return (struct isl_basic_set
*)isl_basic_map_normalize(
7030 (struct isl_basic_map
*)bset
);
7033 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map
*bmap1
,
7034 const __isl_keep isl_basic_map
*bmap2
)
7041 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) !=
7042 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_RATIONAL
))
7043 return ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) ? -1 : 1;
7044 if (isl_basic_map_n_param(bmap1
) != isl_basic_map_n_param(bmap2
))
7045 return isl_basic_map_n_param(bmap1
) - isl_basic_map_n_param(bmap2
);
7046 if (isl_basic_map_n_in(bmap1
) != isl_basic_map_n_in(bmap2
))
7047 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
7048 if (isl_basic_map_n_out(bmap1
) != isl_basic_map_n_out(bmap2
))
7049 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
7050 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
) &&
7051 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
7053 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
))
7055 if (ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
7057 if (bmap1
->n_eq
!= bmap2
->n_eq
)
7058 return bmap1
->n_eq
- bmap2
->n_eq
;
7059 if (bmap1
->n_ineq
!= bmap2
->n_ineq
)
7060 return bmap1
->n_ineq
- bmap2
->n_ineq
;
7061 if (bmap1
->n_div
!= bmap2
->n_div
)
7062 return bmap1
->n_div
- bmap2
->n_div
;
7063 total
= isl_basic_map_total_dim(bmap1
);
7064 for (i
= 0; i
< bmap1
->n_eq
; ++i
) {
7065 cmp
= isl_seq_cmp(bmap1
->eq
[i
], bmap2
->eq
[i
], 1+total
);
7069 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
7070 cmp
= isl_seq_cmp(bmap1
->ineq
[i
], bmap2
->ineq
[i
], 1+total
);
7074 for (i
= 0; i
< bmap1
->n_div
; ++i
) {
7075 cmp
= isl_seq_cmp(bmap1
->div
[i
], bmap2
->div
[i
], 1+1+total
);
7082 int isl_basic_map_plain_is_equal(__isl_keep isl_basic_map
*bmap1
,
7083 __isl_keep isl_basic_map
*bmap2
)
7085 return isl_basic_map_plain_cmp(bmap1
, bmap2
) == 0;
7088 int isl_basic_set_plain_is_equal(__isl_keep isl_basic_set
*bset1
,
7089 __isl_keep isl_basic_set
*bset2
)
7091 return isl_basic_map_plain_is_equal((isl_basic_map
*)bset1
,
7092 (isl_basic_map
*)bset2
);
7095 static int qsort_bmap_cmp(const void *p1
, const void *p2
)
7097 const struct isl_basic_map
*bmap1
= *(const struct isl_basic_map
**)p1
;
7098 const struct isl_basic_map
*bmap2
= *(const struct isl_basic_map
**)p2
;
7100 return isl_basic_map_plain_cmp(bmap1
, bmap2
);
7103 /* We normalize in place, but if anything goes wrong we need
7104 * to return NULL, so we need to make sure we don't change the
7105 * meaning of any possible other copies of map.
7107 struct isl_map
*isl_map_normalize(struct isl_map
*map
)
7110 struct isl_basic_map
*bmap
;
7114 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
))
7116 for (i
= 0; i
< map
->n
; ++i
) {
7117 bmap
= isl_basic_map_normalize(isl_basic_map_copy(map
->p
[i
]));
7120 isl_basic_map_free(map
->p
[i
]);
7123 qsort(map
->p
, map
->n
, sizeof(struct isl_basic_map
*), qsort_bmap_cmp
);
7124 ISL_F_SET(map
, ISL_MAP_NORMALIZED
);
7125 map
= isl_map_remove_empty_parts(map
);
7128 for (i
= map
->n
- 1; i
>= 1; --i
) {
7129 if (!isl_basic_map_plain_is_equal(map
->p
[i
-1], map
->p
[i
]))
7131 isl_basic_map_free(map
->p
[i
-1]);
7132 for (j
= i
; j
< map
->n
; ++j
)
7133 map
->p
[j
-1] = map
->p
[j
];
7143 struct isl_set
*isl_set_normalize(struct isl_set
*set
)
7145 return (struct isl_set
*)isl_map_normalize((struct isl_map
*)set
);
7148 int isl_map_plain_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
7158 if (!isl_dim_equal(map1
->dim
, map2
->dim
))
7161 map1
= isl_map_copy(map1
);
7162 map2
= isl_map_copy(map2
);
7163 map1
= isl_map_normalize(map1
);
7164 map2
= isl_map_normalize(map2
);
7167 equal
= map1
->n
== map2
->n
;
7168 for (i
= 0; equal
&& i
< map1
->n
; ++i
) {
7169 equal
= isl_basic_map_plain_is_equal(map1
->p
[i
], map2
->p
[i
]);
7182 int isl_map_fast_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
7184 return isl_map_plain_is_equal(map1
, map2
);
7187 int isl_set_plain_is_equal(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
7189 return isl_map_plain_is_equal((struct isl_map
*)set1
,
7190 (struct isl_map
*)set2
);
7193 int isl_set_fast_is_equal(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
7195 return isl_set_plain_is_equal(set1
, set2
);
7198 /* Return an interval that ranges from min to max (inclusive)
7200 struct isl_basic_set
*isl_basic_set_interval(struct isl_ctx
*ctx
,
7201 isl_int min
, isl_int max
)
7204 struct isl_basic_set
*bset
= NULL
;
7206 bset
= isl_basic_set_alloc(ctx
, 0, 1, 0, 0, 2);
7210 k
= isl_basic_set_alloc_inequality(bset
);
7213 isl_int_set_si(bset
->ineq
[k
][1], 1);
7214 isl_int_neg(bset
->ineq
[k
][0], min
);
7216 k
= isl_basic_set_alloc_inequality(bset
);
7219 isl_int_set_si(bset
->ineq
[k
][1], -1);
7220 isl_int_set(bset
->ineq
[k
][0], max
);
7224 isl_basic_set_free(bset
);
7228 /* Return the Cartesian product of the basic sets in list (in the given order).
7230 __isl_give isl_basic_set
*isl_basic_set_list_product(
7231 __isl_take
struct isl_basic_set_list
*list
)
7239 struct isl_basic_set
*product
= NULL
;
7243 isl_assert(list
->ctx
, list
->n
> 0, goto error
);
7244 isl_assert(list
->ctx
, list
->p
[0], goto error
);
7245 nparam
= isl_basic_set_n_param(list
->p
[0]);
7246 dim
= isl_basic_set_n_dim(list
->p
[0]);
7247 extra
= list
->p
[0]->n_div
;
7248 n_eq
= list
->p
[0]->n_eq
;
7249 n_ineq
= list
->p
[0]->n_ineq
;
7250 for (i
= 1; i
< list
->n
; ++i
) {
7251 isl_assert(list
->ctx
, list
->p
[i
], goto error
);
7252 isl_assert(list
->ctx
,
7253 nparam
== isl_basic_set_n_param(list
->p
[i
]), goto error
);
7254 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
7255 extra
+= list
->p
[i
]->n_div
;
7256 n_eq
+= list
->p
[i
]->n_eq
;
7257 n_ineq
+= list
->p
[i
]->n_ineq
;
7259 product
= isl_basic_set_alloc(list
->ctx
, nparam
, dim
, extra
,
7264 for (i
= 0; i
< list
->n
; ++i
) {
7265 isl_basic_set_add_constraints(product
,
7266 isl_basic_set_copy(list
->p
[i
]), dim
);
7267 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
7269 isl_basic_set_list_free(list
);
7272 isl_basic_set_free(product
);
7273 isl_basic_set_list_free(list
);
7277 struct isl_basic_map
*isl_basic_map_product(
7278 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
7280 struct isl_dim
*dim_result
= NULL
;
7281 struct isl_basic_map
*bmap
;
7282 unsigned in1
, in2
, out1
, out2
, nparam
, total
, pos
;
7283 struct isl_dim_map
*dim_map1
, *dim_map2
;
7285 if (!bmap1
|| !bmap2
)
7288 isl_assert(bmap1
->ctx
, isl_dim_match(bmap1
->dim
, isl_dim_param
,
7289 bmap2
->dim
, isl_dim_param
), goto error
);
7290 dim_result
= isl_dim_product(isl_dim_copy(bmap1
->dim
),
7291 isl_dim_copy(bmap2
->dim
));
7293 in1
= isl_basic_map_n_in(bmap1
);
7294 in2
= isl_basic_map_n_in(bmap2
);
7295 out1
= isl_basic_map_n_out(bmap1
);
7296 out2
= isl_basic_map_n_out(bmap2
);
7297 nparam
= isl_basic_map_n_param(bmap1
);
7299 total
= nparam
+ in1
+ in2
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
7300 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
7301 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
7302 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
7303 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
7304 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
7305 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
7306 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
7307 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
7308 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
7309 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
7311 bmap
= isl_basic_map_alloc_dim(dim_result
,
7312 bmap1
->n_div
+ bmap2
->n_div
,
7313 bmap1
->n_eq
+ bmap2
->n_eq
,
7314 bmap1
->n_ineq
+ bmap2
->n_ineq
);
7315 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
7316 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
7317 bmap
= isl_basic_map_simplify(bmap
);
7318 return isl_basic_map_finalize(bmap
);
7320 isl_basic_map_free(bmap1
);
7321 isl_basic_map_free(bmap2
);
7325 __isl_give isl_basic_map
*isl_basic_map_flat_product(
7326 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
7328 isl_basic_map
*prod
;
7330 prod
= isl_basic_map_product(bmap1
, bmap2
);
7331 prod
= isl_basic_map_flatten(prod
);
7335 __isl_give isl_basic_set
*isl_basic_set_flat_product(
7336 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
7338 return isl_basic_map_flat_product(bset1
, bset2
);
7341 __isl_give isl_basic_map
*isl_basic_map_range_product(
7342 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
7344 isl_dim
*dim_result
= NULL
;
7345 isl_basic_map
*bmap
;
7346 unsigned in
, out1
, out2
, nparam
, total
, pos
;
7347 struct isl_dim_map
*dim_map1
, *dim_map2
;
7349 if (!bmap1
|| !bmap2
)
7352 dim_result
= isl_dim_range_product(isl_dim_copy(bmap1
->dim
),
7353 isl_dim_copy(bmap2
->dim
));
7355 in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
7356 out1
= isl_basic_map_n_out(bmap1
);
7357 out2
= isl_basic_map_n_out(bmap2
);
7358 nparam
= isl_basic_map_n_param(bmap1
);
7360 total
= nparam
+ in
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
7361 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
7362 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
7363 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
7364 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
7365 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
7366 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
7367 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in
);
7368 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
7369 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
7370 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
7372 bmap
= isl_basic_map_alloc_dim(dim_result
,
7373 bmap1
->n_div
+ bmap2
->n_div
,
7374 bmap1
->n_eq
+ bmap2
->n_eq
,
7375 bmap1
->n_ineq
+ bmap2
->n_ineq
);
7376 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
7377 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
7378 bmap
= isl_basic_map_simplify(bmap
);
7379 return isl_basic_map_finalize(bmap
);
7381 isl_basic_map_free(bmap1
);
7382 isl_basic_map_free(bmap2
);
7386 static __isl_give isl_map
*map_product(__isl_take isl_map
*map1
,
7387 __isl_take isl_map
*map2
,
7388 __isl_give isl_dim
*(*dim_product
)(__isl_take isl_dim
*left
,
7389 __isl_take isl_dim
*right
),
7390 __isl_give isl_basic_map
*(*basic_map_product
)(
7391 __isl_take isl_basic_map
*left
, __isl_take isl_basic_map
*right
))
7394 struct isl_map
*result
;
7400 isl_assert(map1
->ctx
, isl_dim_match(map1
->dim
, isl_dim_param
,
7401 map2
->dim
, isl_dim_param
), goto error
);
7403 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
7404 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
7405 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
7407 result
= isl_map_alloc_dim(dim_product(isl_dim_copy(map1
->dim
),
7408 isl_dim_copy(map2
->dim
)),
7409 map1
->n
* map2
->n
, flags
);
7412 for (i
= 0; i
< map1
->n
; ++i
)
7413 for (j
= 0; j
< map2
->n
; ++j
) {
7414 struct isl_basic_map
*part
;
7415 part
= basic_map_product(isl_basic_map_copy(map1
->p
[i
]),
7416 isl_basic_map_copy(map2
->p
[j
]));
7417 if (isl_basic_map_is_empty(part
))
7418 isl_basic_map_free(part
);
7420 result
= isl_map_add_basic_map(result
, part
);
7433 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
7435 struct isl_map
*isl_map_product(struct isl_map
*map1
, struct isl_map
*map2
)
7437 return map_product(map1
, map2
, &isl_dim_product
, &isl_basic_map_product
);
7440 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
7442 __isl_give isl_map
*isl_map_flat_product(__isl_take isl_map
*map1
,
7443 __isl_take isl_map
*map2
)
7447 prod
= isl_map_product(map1
, map2
);
7448 prod
= isl_map_flatten(prod
);
7452 /* Given two set A and B, construct its Cartesian product A x B.
7454 struct isl_set
*isl_set_product(struct isl_set
*set1
, struct isl_set
*set2
)
7456 return (struct isl_set
*)isl_map_product((struct isl_map
*)set1
,
7457 (struct isl_map
*)set2
);
7460 __isl_give isl_set
*isl_set_flat_product(__isl_take isl_set
*set1
,
7461 __isl_take isl_set
*set2
)
7463 return (isl_set
*)isl_map_flat_product((isl_map
*)set1
, (isl_map
*)set2
);
7466 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
7468 __isl_give isl_map
*isl_map_range_product(__isl_take isl_map
*map1
,
7469 __isl_take isl_map
*map2
)
7471 return map_product(map1
, map2
, &isl_dim_range_product
,
7472 &isl_basic_map_range_product
);
7475 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map
*bmap
)
7478 uint32_t hash
= isl_hash_init();
7483 bmap
= isl_basic_map_copy(bmap
);
7484 bmap
= isl_basic_map_normalize(bmap
);
7487 total
= isl_basic_map_total_dim(bmap
);
7488 isl_hash_byte(hash
, bmap
->n_eq
& 0xFF);
7489 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
7491 c_hash
= isl_seq_get_hash(bmap
->eq
[i
], 1 + total
);
7492 isl_hash_hash(hash
, c_hash
);
7494 isl_hash_byte(hash
, bmap
->n_ineq
& 0xFF);
7495 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
7497 c_hash
= isl_seq_get_hash(bmap
->ineq
[i
], 1 + total
);
7498 isl_hash_hash(hash
, c_hash
);
7500 isl_hash_byte(hash
, bmap
->n_div
& 0xFF);
7501 for (i
= 0; i
< bmap
->n_div
; ++i
) {
7503 if (isl_int_is_zero(bmap
->div
[i
][0]))
7505 isl_hash_byte(hash
, i
& 0xFF);
7506 c_hash
= isl_seq_get_hash(bmap
->div
[i
], 1 + 1 + total
);
7507 isl_hash_hash(hash
, c_hash
);
7509 isl_basic_map_free(bmap
);
7513 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set
*bset
)
7515 return isl_basic_map_get_hash((isl_basic_map
*)bset
);
7518 uint32_t isl_map_get_hash(__isl_keep isl_map
*map
)
7525 map
= isl_map_copy(map
);
7526 map
= isl_map_normalize(map
);
7530 hash
= isl_hash_init();
7531 for (i
= 0; i
< map
->n
; ++i
) {
7533 bmap_hash
= isl_basic_map_get_hash(map
->p
[i
]);
7534 isl_hash_hash(hash
, bmap_hash
);
7542 uint32_t isl_set_get_hash(__isl_keep isl_set
*set
)
7544 return isl_map_get_hash((isl_map
*)set
);
7547 /* Check if the value for dimension dim is completely determined
7548 * by the values of the other parameters and variables.
7549 * That is, check if dimension dim is involved in an equality.
7551 int isl_basic_set_dim_is_unique(struct isl_basic_set
*bset
, unsigned dim
)
7558 nparam
= isl_basic_set_n_param(bset
);
7559 for (i
= 0; i
< bset
->n_eq
; ++i
)
7560 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ dim
]))
7565 /* Check if the value for dimension dim is completely determined
7566 * by the values of the other parameters and variables.
7567 * That is, check if dimension dim is involved in an equality
7568 * for each of the subsets.
7570 int isl_set_dim_is_unique(struct isl_set
*set
, unsigned dim
)
7576 for (i
= 0; i
< set
->n
; ++i
) {
7578 unique
= isl_basic_set_dim_is_unique(set
->p
[i
], dim
);
7585 int isl_set_n_basic_set(__isl_keep isl_set
*set
)
7587 return set
? set
->n
: 0;
7590 int isl_map_foreach_basic_map(__isl_keep isl_map
*map
,
7591 int (*fn
)(__isl_take isl_basic_map
*bmap
, void *user
), void *user
)
7598 for (i
= 0; i
< map
->n
; ++i
)
7599 if (fn(isl_basic_map_copy(map
->p
[i
]), user
) < 0)
7605 int isl_set_foreach_basic_set(__isl_keep isl_set
*set
,
7606 int (*fn
)(__isl_take isl_basic_set
*bset
, void *user
), void *user
)
7613 for (i
= 0; i
< set
->n
; ++i
)
7614 if (fn(isl_basic_set_copy(set
->p
[i
]), user
) < 0)
7620 __isl_give isl_basic_set
*isl_basic_set_lift(__isl_take isl_basic_set
*bset
)
7622 struct isl_dim
*dim
;
7627 bset
= isl_basic_set_cow(bset
);
7631 dim
= isl_basic_set_get_dim(bset
);
7632 dim
= isl_dim_lift(dim
, bset
->n_div
);
7635 isl_dim_free(bset
->dim
);
7637 bset
->extra
-= bset
->n_div
;
7640 bset
= isl_basic_set_finalize(bset
);
7644 isl_basic_set_free(bset
);
7648 __isl_give isl_set
*isl_set_lift(__isl_take isl_set
*set
)
7651 struct isl_dim
*dim
;
7654 set
= isl_set_align_divs(set
);
7659 set
= isl_set_cow(set
);
7663 n_div
= set
->p
[0]->n_div
;
7664 dim
= isl_set_get_dim(set
);
7665 dim
= isl_dim_lift(dim
, n_div
);
7668 isl_dim_free(set
->dim
);
7671 for (i
= 0; i
< set
->n
; ++i
) {
7672 set
->p
[i
] = isl_basic_set_lift(set
->p
[i
]);
7683 __isl_give isl_map
*isl_set_lifting(__isl_take isl_set
*set
)
7685 struct isl_dim
*dim
;
7686 struct isl_basic_map
*bmap
;
7693 set
= isl_set_align_divs(set
);
7698 dim
= isl_set_get_dim(set
);
7699 if (set
->n
== 0 || set
->p
[0]->n_div
== 0) {
7701 return isl_map_identity(isl_dim_map_from_set(dim
));
7704 n_div
= set
->p
[0]->n_div
;
7705 dim
= isl_dim_map_from_set(dim
);
7706 n_param
= isl_dim_size(dim
, isl_dim_param
);
7707 n_set
= isl_dim_size(dim
, isl_dim_in
);
7708 dim
= isl_dim_extend(dim
, n_param
, n_set
, n_set
+ n_div
);
7709 bmap
= isl_basic_map_alloc_dim(dim
, 0, n_set
, 2 * n_div
);
7710 for (i
= 0; i
< n_set
; ++i
)
7711 bmap
= var_equal(bmap
, i
);
7713 total
= n_param
+ n_set
+ n_set
+ n_div
;
7714 for (i
= 0; i
< n_div
; ++i
) {
7715 k
= isl_basic_map_alloc_inequality(bmap
);
7718 isl_seq_cpy(bmap
->ineq
[k
], set
->p
[0]->div
[i
]+1, 1+n_param
);
7719 isl_seq_clr(bmap
->ineq
[k
]+1+n_param
, n_set
);
7720 isl_seq_cpy(bmap
->ineq
[k
]+1+n_param
+n_set
,
7721 set
->p
[0]->div
[i
]+1+1+n_param
, n_set
+ n_div
);
7722 isl_int_neg(bmap
->ineq
[k
][1+n_param
+n_set
+n_set
+i
],
7723 set
->p
[0]->div
[i
][0]);
7725 l
= isl_basic_map_alloc_inequality(bmap
);
7728 isl_seq_neg(bmap
->ineq
[l
], bmap
->ineq
[k
], 1 + total
);
7729 isl_int_add(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0],
7730 set
->p
[0]->div
[i
][0]);
7731 isl_int_sub_ui(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], 1);
7735 bmap
= isl_basic_map_simplify(bmap
);
7736 bmap
= isl_basic_map_finalize(bmap
);
7737 return isl_map_from_basic_map(bmap
);
7740 isl_basic_map_free(bmap
);
7744 int isl_basic_set_size(__isl_keep isl_basic_set
*bset
)
7752 dim
= isl_basic_set_total_dim(bset
);
7753 size
+= bset
->n_eq
* (1 + dim
);
7754 size
+= bset
->n_ineq
* (1 + dim
);
7755 size
+= bset
->n_div
* (2 + dim
);
7760 int isl_set_size(__isl_keep isl_set
*set
)
7768 for (i
= 0; i
< set
->n
; ++i
)
7769 size
+= isl_basic_set_size(set
->p
[i
]);
7774 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
7775 enum isl_dim_type type
, unsigned pos
)
7783 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), return -1);
7785 pos
+= isl_basic_map_offset(bmap
, type
);
7787 for (i
= 0; i
< bmap
->n_eq
; ++i
)
7788 if (!isl_int_is_zero(bmap
->eq
[i
][pos
]))
7792 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
7793 int sgn
= isl_int_sgn(bmap
->ineq
[i
][pos
]);
7800 return lower
&& upper
;
7803 int isl_map_dim_is_bounded(__isl_keep isl_map
*map
,
7804 enum isl_dim_type type
, unsigned pos
)
7811 for (i
= 0; i
< map
->n
; ++i
) {
7813 bounded
= isl_basic_map_dim_is_bounded(map
->p
[i
], type
, pos
);
7814 if (bounded
< 0 || !bounded
)
7821 /* Return 1 if the specified dim is involved in both an upper bound
7822 * and a lower bound.
7824 int isl_set_dim_is_bounded(__isl_keep isl_set
*set
,
7825 enum isl_dim_type type
, unsigned pos
)
7827 return isl_map_dim_is_bounded((isl_map
*)set
, type
, pos
);
7830 /* For each of the "n" variables starting at "first", determine
7831 * the sign of the variable and put the results in the first "n"
7832 * elements of the array "signs".
7834 * 1 means that the variable is non-negative
7835 * -1 means that the variable is non-positive
7836 * 0 means the variable attains both positive and negative values.
7838 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set
*bset
,
7839 unsigned first
, unsigned n
, int *signs
)
7841 isl_vec
*bound
= NULL
;
7842 struct isl_tab
*tab
= NULL
;
7843 struct isl_tab_undo
*snap
;
7846 if (!bset
|| !signs
)
7849 bound
= isl_vec_alloc(bset
->ctx
, 1 + isl_basic_set_total_dim(bset
));
7850 tab
= isl_tab_from_basic_set(bset
);
7854 isl_seq_clr(bound
->el
, bound
->size
);
7855 isl_int_set_si(bound
->el
[0], -1);
7857 snap
= isl_tab_snap(tab
);
7858 for (i
= 0; i
< n
; ++i
) {
7861 isl_int_set_si(bound
->el
[1 + first
+ i
], -1);
7862 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
7865 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
7866 if (isl_tab_rollback(tab
, snap
) < 0)
7874 isl_int_set_si(bound
->el
[1 + first
+ i
], 1);
7875 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
7878 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
7879 if (isl_tab_rollback(tab
, snap
) < 0)
7882 signs
[i
] = empty
? -1 : 0;
7886 isl_vec_free(bound
);
7890 isl_vec_free(bound
);
7894 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set
*bset
,
7895 enum isl_dim_type type
, unsigned first
, unsigned n
, int *signs
)
7897 if (!bset
|| !signs
)
7899 isl_assert(bset
->ctx
, first
+ n
<= isl_basic_set_dim(bset
, type
),
7902 first
+= pos(bset
->dim
, type
) - 1;
7903 return isl_basic_set_vars_get_sign(bset
, first
, n
, signs
);
7906 /* Check if the given basic map is obviously single-valued.
7907 * In particular, for each output dimension, check that there is
7908 * an equality that defines the output dimension in terms of
7909 * earlier dimensions.
7911 int isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map
*bmap
)
7921 total
= 1 + isl_basic_map_total_dim(bmap
);
7922 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
7923 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
7925 for (i
= 0; i
< n_out
; ++i
) {
7926 for (j
= 0; j
< bmap
->n_eq
; ++j
) {
7927 if (isl_int_is_zero(bmap
->eq
[j
][o_out
+ i
]))
7929 if (isl_seq_first_non_zero(bmap
->eq
[j
] + o_out
+ i
+ 1,
7930 total
- (o_out
+ i
+ 1)) == -1)
7933 if (j
>= bmap
->n_eq
)
7940 /* Check if the given map is obviously single-valued.
7942 int isl_map_plain_is_single_valued(__isl_keep isl_map
*map
)
7953 return isl_basic_map_plain_is_single_valued(map
->p
[0]);
7956 /* Check if the given map is single-valued.
7961 * and check if the result is a subset of the identity mapping.
7963 int isl_map_is_single_valued(__isl_keep isl_map
*map
)
7970 sv
= isl_map_plain_is_single_valued(map
);
7974 test
= isl_map_reverse(isl_map_copy(map
));
7975 test
= isl_map_apply_range(test
, isl_map_copy(map
));
7977 dim
= isl_dim_map_from_set(isl_dim_range(isl_map_get_dim(map
)));
7978 id
= isl_map_identity(dim
);
7980 sv
= isl_map_is_subset(test
, id
);
7988 int isl_map_is_injective(__isl_keep isl_map
*map
)
7992 map
= isl_map_copy(map
);
7993 map
= isl_map_reverse(map
);
7994 in
= isl_map_is_single_valued(map
);
8000 /* Check if the given map is obviously injective.
8002 int isl_map_plain_is_injective(__isl_keep isl_map
*map
)
8006 map
= isl_map_copy(map
);
8007 map
= isl_map_reverse(map
);
8008 in
= isl_map_plain_is_single_valued(map
);
8014 int isl_map_is_bijective(__isl_keep isl_map
*map
)
8018 sv
= isl_map_is_single_valued(map
);
8022 return isl_map_is_injective(map
);
8025 int isl_set_is_singleton(__isl_keep isl_set
*set
)
8027 return isl_map_is_single_valued((isl_map
*)set
);
8030 int isl_map_is_translation(__isl_keep isl_map
*map
)
8035 delta
= isl_map_deltas(isl_map_copy(map
));
8036 ok
= isl_set_is_singleton(delta
);
8037 isl_set_free(delta
);
8042 static int unique(isl_int
*p
, unsigned pos
, unsigned len
)
8044 if (isl_seq_first_non_zero(p
, pos
) != -1)
8046 if (isl_seq_first_non_zero(p
+ pos
+ 1, len
- pos
- 1) != -1)
8051 int isl_basic_set_is_box(__isl_keep isl_basic_set
*bset
)
8060 if (isl_basic_set_dim(bset
, isl_dim_div
) != 0)
8063 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
8064 ovar
= isl_dim_offset(bset
->dim
, isl_dim_set
);
8065 for (j
= 0; j
< nvar
; ++j
) {
8066 int lower
= 0, upper
= 0;
8067 for (i
= 0; i
< bset
->n_eq
; ++i
) {
8068 if (isl_int_is_zero(bset
->eq
[i
][1 + ovar
+ j
]))
8070 if (!unique(bset
->eq
[i
] + 1 + ovar
, j
, nvar
))
8076 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
8077 if (isl_int_is_zero(bset
->ineq
[i
][1 + ovar
+ j
]))
8079 if (!unique(bset
->ineq
[i
] + 1 + ovar
, j
, nvar
))
8081 if (isl_int_is_pos(bset
->ineq
[i
][1 + ovar
+ j
]))
8086 if (!lower
|| !upper
)
8093 int isl_set_is_box(__isl_keep isl_set
*set
)
8100 return isl_basic_set_is_box(set
->p
[0]);
8103 int isl_basic_set_is_wrapping(__isl_keep isl_basic_set
*bset
)
8108 return isl_dim_is_wrapping(bset
->dim
);
8111 int isl_set_is_wrapping(__isl_keep isl_set
*set
)
8116 return isl_dim_is_wrapping(set
->dim
);
8119 __isl_give isl_basic_set
*isl_basic_map_wrap(__isl_take isl_basic_map
*bmap
)
8121 bmap
= isl_basic_map_cow(bmap
);
8125 bmap
->dim
= isl_dim_wrap(bmap
->dim
);
8129 bmap
= isl_basic_map_finalize(bmap
);
8131 return (isl_basic_set
*)bmap
;
8133 isl_basic_map_free(bmap
);
8137 __isl_give isl_set
*isl_map_wrap(__isl_take isl_map
*map
)
8141 map
= isl_map_cow(map
);
8145 for (i
= 0; i
< map
->n
; ++i
) {
8146 map
->p
[i
] = (isl_basic_map
*)isl_basic_map_wrap(map
->p
[i
]);
8150 map
->dim
= isl_dim_wrap(map
->dim
);
8154 return (isl_set
*)map
;
8160 __isl_give isl_basic_map
*isl_basic_set_unwrap(__isl_take isl_basic_set
*bset
)
8162 bset
= isl_basic_set_cow(bset
);
8166 bset
->dim
= isl_dim_unwrap(bset
->dim
);
8170 bset
= isl_basic_set_finalize(bset
);
8172 return (isl_basic_map
*)bset
;
8174 isl_basic_set_free(bset
);
8178 __isl_give isl_map
*isl_set_unwrap(__isl_take isl_set
*set
)
8185 if (!isl_set_is_wrapping(set
))
8186 isl_die(set
->ctx
, isl_error_invalid
, "not a wrapping set",
8189 set
= isl_set_cow(set
);
8193 for (i
= 0; i
< set
->n
; ++i
) {
8194 set
->p
[i
] = (isl_basic_set
*)isl_basic_set_unwrap(set
->p
[i
]);
8199 set
->dim
= isl_dim_unwrap(set
->dim
);
8203 return (isl_map
*)set
;
8209 __isl_give isl_basic_map
*isl_basic_map_reset(__isl_take isl_basic_map
*bmap
,
8210 enum isl_dim_type type
)
8215 if (!isl_dim_is_named_or_nested(bmap
->dim
, type
))
8218 bmap
= isl_basic_map_cow(bmap
);
8222 bmap
->dim
= isl_dim_reset(bmap
->dim
, type
);
8226 bmap
= isl_basic_map_finalize(bmap
);
8230 isl_basic_map_free(bmap
);
8234 __isl_give isl_map
*isl_map_reset(__isl_take isl_map
*map
,
8235 enum isl_dim_type type
)
8242 if (!isl_dim_is_named_or_nested(map
->dim
, type
))
8245 map
= isl_map_cow(map
);
8249 for (i
= 0; i
< map
->n
; ++i
) {
8250 map
->p
[i
] = isl_basic_map_reset(map
->p
[i
], type
);
8254 map
->dim
= isl_dim_reset(map
->dim
, type
);
8264 __isl_give isl_basic_map
*isl_basic_map_flatten(__isl_take isl_basic_map
*bmap
)
8269 if (!bmap
->dim
->nested
[0] && !bmap
->dim
->nested
[1])
8272 bmap
= isl_basic_map_cow(bmap
);
8276 bmap
->dim
= isl_dim_flatten(bmap
->dim
);
8280 bmap
= isl_basic_map_finalize(bmap
);
8284 isl_basic_map_free(bmap
);
8288 __isl_give isl_basic_set
*isl_basic_set_flatten(__isl_take isl_basic_set
*bset
)
8290 return (isl_basic_set
*)isl_basic_map_flatten((isl_basic_map
*)bset
);
8293 __isl_give isl_map
*isl_map_flatten(__isl_take isl_map
*map
)
8300 if (!map
->dim
->nested
[0] && !map
->dim
->nested
[1])
8303 map
= isl_map_cow(map
);
8307 for (i
= 0; i
< map
->n
; ++i
) {
8308 map
->p
[i
] = isl_basic_map_flatten(map
->p
[i
]);
8312 map
->dim
= isl_dim_flatten(map
->dim
);
8322 __isl_give isl_set
*isl_set_flatten(__isl_take isl_set
*set
)
8324 return (isl_set
*)isl_map_flatten((isl_map
*)set
);
8327 __isl_give isl_map
*isl_set_flatten_map(__isl_take isl_set
*set
)
8329 isl_dim
*dim
, *flat_dim
;
8332 dim
= isl_set_get_dim(set
);
8333 flat_dim
= isl_dim_flatten(isl_dim_copy(dim
));
8334 map
= isl_map_identity(isl_dim_join(isl_dim_reverse(dim
), flat_dim
));
8335 map
= isl_map_intersect_domain(map
, set
);
8340 /* Reorder the dimensions of "bmap" according to the given dim_map
8341 * and set the dimension specification to "dim".
8343 __isl_give isl_basic_map
*isl_basic_map_realign(__isl_take isl_basic_map
*bmap
,
8344 __isl_take isl_dim
*dim
, __isl_take
struct isl_dim_map
*dim_map
)
8348 bmap
= isl_basic_map_cow(bmap
);
8349 if (!bmap
|| !dim
|| !dim_map
)
8352 res
= isl_basic_map_alloc_dim(dim
,
8353 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
8354 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
8355 res
= isl_basic_map_finalize(res
);
8359 isl_basic_map_free(bmap
);
8364 /* Reorder the dimensions of "map" according to given reordering.
8366 __isl_give isl_map
*isl_map_realign(__isl_take isl_map
*map
,
8367 __isl_take isl_reordering
*r
)
8370 struct isl_dim_map
*dim_map
;
8372 map
= isl_map_cow(map
);
8373 dim_map
= isl_dim_map_from_reordering(r
);
8374 if (!map
|| !r
|| !dim_map
)
8377 for (i
= 0; i
< map
->n
; ++i
) {
8378 struct isl_dim_map
*dim_map_i
;
8380 dim_map_i
= isl_dim_map_extend(dim_map
, map
->p
[i
]);
8382 map
->p
[i
] = isl_basic_map_realign(map
->p
[i
],
8383 isl_dim_copy(r
->dim
), dim_map_i
);
8389 map
= isl_map_reset_dim(map
, isl_dim_copy(r
->dim
));
8391 isl_reordering_free(r
);
8397 isl_reordering_free(r
);
8401 __isl_give isl_set
*isl_set_realign(__isl_take isl_set
*set
,
8402 __isl_take isl_reordering
*r
)
8404 return (isl_set
*)isl_map_realign((isl_map
*)set
, r
);
8407 __isl_give isl_map
*isl_map_align_params(__isl_take isl_map
*map
,
8408 __isl_take isl_dim
*model
)
8415 ctx
= isl_dim_get_ctx(model
);
8416 if (!isl_dim_has_named_params(model
))
8417 isl_die(ctx
, isl_error_invalid
,
8418 "model has unnamed parameters", goto error
);
8419 if (!isl_dim_has_named_params(map
->dim
))
8420 isl_die(ctx
, isl_error_invalid
,
8421 "relation has unnamed parameters", goto error
);
8422 if (!isl_dim_match(map
->dim
, isl_dim_param
, model
, isl_dim_param
)) {
8423 isl_reordering
*exp
;
8425 model
= isl_dim_drop(model
, isl_dim_in
,
8426 0, isl_dim_size(model
, isl_dim_in
));
8427 model
= isl_dim_drop(model
, isl_dim_out
,
8428 0, isl_dim_size(model
, isl_dim_out
));
8429 exp
= isl_parameter_alignment_reordering(map
->dim
, model
);
8430 exp
= isl_reordering_extend_dim(exp
, isl_map_get_dim(map
));
8431 map
= isl_map_realign(map
, exp
);
8434 isl_dim_free(model
);
8437 isl_dim_free(model
);
8442 __isl_give isl_set
*isl_set_align_params(__isl_take isl_set
*set
,
8443 __isl_take isl_dim
*model
)
8445 return isl_map_align_params(set
, model
);
8448 __isl_give isl_mat
*isl_basic_map_equalities_matrix(
8449 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
8450 enum isl_dim_type c2
, enum isl_dim_type c3
,
8451 enum isl_dim_type c4
, enum isl_dim_type c5
)
8453 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
8454 struct isl_mat
*mat
;
8460 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_eq
,
8461 isl_basic_map_total_dim(bmap
) + 1);
8464 for (i
= 0; i
< bmap
->n_eq
; ++i
)
8465 for (j
= 0, pos
= 0; j
< 5; ++j
) {
8466 int off
= isl_basic_map_offset(bmap
, c
[j
]);
8467 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
8468 isl_int_set(mat
->row
[i
][pos
],
8469 bmap
->eq
[i
][off
+ k
]);
8477 __isl_give isl_mat
*isl_basic_map_inequalities_matrix(
8478 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
8479 enum isl_dim_type c2
, enum isl_dim_type c3
,
8480 enum isl_dim_type c4
, enum isl_dim_type c5
)
8482 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
8483 struct isl_mat
*mat
;
8489 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_ineq
,
8490 isl_basic_map_total_dim(bmap
) + 1);
8493 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
8494 for (j
= 0, pos
= 0; j
< 5; ++j
) {
8495 int off
= isl_basic_map_offset(bmap
, c
[j
]);
8496 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
8497 isl_int_set(mat
->row
[i
][pos
],
8498 bmap
->ineq
[i
][off
+ k
]);
8506 __isl_give isl_basic_map
*isl_basic_map_from_constraint_matrices(
8507 __isl_take isl_dim
*dim
,
8508 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
8509 enum isl_dim_type c2
, enum isl_dim_type c3
,
8510 enum isl_dim_type c4
, enum isl_dim_type c5
)
8512 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
8513 isl_basic_map
*bmap
;
8519 if (!dim
|| !eq
|| !ineq
)
8522 if (eq
->n_col
!= ineq
->n_col
)
8523 isl_die(dim
->ctx
, isl_error_invalid
,
8524 "equalities and inequalities matrices should have "
8525 "same number of columns", goto error
);
8527 total
= 1 + isl_dim_total(dim
);
8529 if (eq
->n_col
< total
)
8530 isl_die(dim
->ctx
, isl_error_invalid
,
8531 "number of columns too small", goto error
);
8533 extra
= eq
->n_col
- total
;
8535 bmap
= isl_basic_map_alloc_dim(isl_dim_copy(dim
), extra
,
8536 eq
->n_row
, ineq
->n_row
);
8539 for (i
= 0; i
< extra
; ++i
) {
8540 k
= isl_basic_map_alloc_div(bmap
);
8543 isl_int_set_si(bmap
->div
[k
][0], 0);
8545 for (i
= 0; i
< eq
->n_row
; ++i
) {
8546 l
= isl_basic_map_alloc_equality(bmap
);
8549 for (j
= 0, pos
= 0; j
< 5; ++j
) {
8550 int off
= isl_basic_map_offset(bmap
, c
[j
]);
8551 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
8552 isl_int_set(bmap
->eq
[l
][off
+ k
],
8558 for (i
= 0; i
< ineq
->n_row
; ++i
) {
8559 l
= isl_basic_map_alloc_inequality(bmap
);
8562 for (j
= 0, pos
= 0; j
< 5; ++j
) {
8563 int off
= isl_basic_map_offset(bmap
, c
[j
]);
8564 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
8565 isl_int_set(bmap
->ineq
[l
][off
+ k
],
8584 __isl_give isl_mat
*isl_basic_set_equalities_matrix(
8585 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
8586 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
8588 return isl_basic_map_equalities_matrix((isl_basic_map
*)bset
,
8589 c1
, c2
, c3
, c4
, isl_dim_in
);
8592 __isl_give isl_mat
*isl_basic_set_inequalities_matrix(
8593 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
8594 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
8596 return isl_basic_map_inequalities_matrix((isl_basic_map
*)bset
,
8597 c1
, c2
, c3
, c4
, isl_dim_in
);
8600 __isl_give isl_basic_set
*isl_basic_set_from_constraint_matrices(
8601 __isl_take isl_dim
*dim
,
8602 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
8603 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
8605 return (isl_basic_set
*)
8606 isl_basic_map_from_constraint_matrices(dim
, eq
, ineq
,
8607 c1
, c2
, c3
, c4
, isl_dim_in
);
8610 int isl_basic_map_can_zip(__isl_keep isl_basic_map
*bmap
)
8615 return isl_dim_can_zip(bmap
->dim
);
8618 int isl_map_can_zip(__isl_keep isl_map
*map
)
8623 return isl_dim_can_zip(map
->dim
);
8626 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
8627 * (A -> C) -> (B -> D).
8629 __isl_give isl_basic_map
*isl_basic_map_zip(__isl_take isl_basic_map
*bmap
)
8638 if (!isl_basic_map_can_zip(bmap
))
8639 isl_die(bmap
->ctx
, isl_error_invalid
,
8640 "basic map cannot be zipped", goto error
);
8641 pos
= isl_basic_map_offset(bmap
, isl_dim_in
) +
8642 isl_dim_size(bmap
->dim
->nested
[0], isl_dim_in
);
8643 n1
= isl_dim_size(bmap
->dim
->nested
[0], isl_dim_out
);
8644 n2
= isl_dim_size(bmap
->dim
->nested
[1], isl_dim_in
);
8645 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
8648 bmap
->dim
= isl_dim_zip(bmap
->dim
);
8653 isl_basic_map_free(bmap
);
8657 /* Given a map (A -> B) -> (C -> D), return the corresponding map
8658 * (A -> C) -> (B -> D).
8660 __isl_give isl_map
*isl_map_zip(__isl_take isl_map
*map
)
8667 if (!isl_map_can_zip(map
))
8668 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be zipped",
8671 map
= isl_map_cow(map
);
8675 for (i
= 0; i
< map
->n
; ++i
) {
8676 map
->p
[i
] = isl_basic_map_zip(map
->p
[i
]);
8681 map
->dim
= isl_dim_zip(map
->dim
);