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
14 #include <isl_ctx_private.h>
15 #include <isl_map_private.h>
17 #include "isl_dim_private.h"
18 #include "isl_equalities.h"
19 #include <isl_list_private.h>
24 #include "isl_map_piplib.h"
25 #include <isl_reordering.h>
26 #include "isl_sample.h"
29 #include <isl_mat_private.h>
30 #include <isl_dim_map.h>
31 #include <isl_local_space_private.h>
32 #include <isl_aff_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 struct isl_basic_map
*isl_basic_map_set_to_empty(struct isl_basic_map
*bmap
)
1287 total
= isl_basic_map_total_dim(bmap
);
1288 isl_basic_map_free_div(bmap
, bmap
->n_div
);
1289 isl_basic_map_free_inequality(bmap
, bmap
->n_ineq
);
1291 isl_basic_map_free_equality(bmap
, bmap
->n_eq
-1);
1293 i
= isl_basic_map_alloc_equality(bmap
);
1297 isl_int_set_si(bmap
->eq
[i
][0], 1);
1298 isl_seq_clr(bmap
->eq
[i
]+1, total
);
1299 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
1300 isl_vec_free(bmap
->sample
);
1301 bmap
->sample
= NULL
;
1302 return isl_basic_map_finalize(bmap
);
1304 isl_basic_map_free(bmap
);
1308 struct isl_basic_set
*isl_basic_set_set_to_empty(struct isl_basic_set
*bset
)
1310 return (struct isl_basic_set
*)
1311 isl_basic_map_set_to_empty((struct isl_basic_map
*)bset
);
1314 void isl_basic_map_swap_div(struct isl_basic_map
*bmap
, int a
, int b
)
1317 unsigned off
= isl_dim_total(bmap
->dim
);
1318 isl_int
*t
= bmap
->div
[a
];
1319 bmap
->div
[a
] = bmap
->div
[b
];
1322 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1323 isl_int_swap(bmap
->eq
[i
][1+off
+a
], bmap
->eq
[i
][1+off
+b
]);
1325 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1326 isl_int_swap(bmap
->ineq
[i
][1+off
+a
], bmap
->ineq
[i
][1+off
+b
]);
1328 for (i
= 0; i
< bmap
->n_div
; ++i
)
1329 isl_int_swap(bmap
->div
[i
][1+1+off
+a
], bmap
->div
[i
][1+1+off
+b
]);
1330 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1333 /* Eliminate the specified n dimensions starting at first from the
1334 * constraints using Fourier-Motzkin. The dimensions themselves
1337 __isl_give isl_map
*isl_map_eliminate(__isl_take isl_map
*map
,
1338 enum isl_dim_type type
, unsigned first
, unsigned n
)
1347 map
= isl_map_cow(map
);
1350 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
1351 first
+= pos(map
->dim
, type
) - 1;
1353 for (i
= 0; i
< map
->n
; ++i
) {
1354 map
->p
[i
] = isl_basic_map_eliminate_vars(map
->p
[i
], first
, n
);
1364 /* Eliminate the specified n dimensions starting at first from the
1365 * constraints using Fourier-Motzkin. The dimensions themselves
1368 __isl_give isl_set
*isl_set_eliminate(__isl_take isl_set
*set
,
1369 enum isl_dim_type type
, unsigned first
, unsigned n
)
1371 return (isl_set
*)isl_map_eliminate((isl_map
*)set
, type
, first
, n
);
1374 /* Eliminate the specified n dimensions starting at first from the
1375 * constraints using Fourier-Motzkin. The dimensions themselves
1378 __isl_give isl_set
*isl_set_eliminate_dims(__isl_take isl_set
*set
,
1379 unsigned first
, unsigned n
)
1381 return isl_set_eliminate(set
, isl_dim_set
, first
, n
);
1384 __isl_give isl_basic_map
*isl_basic_map_remove_divs(
1385 __isl_take isl_basic_map
*bmap
)
1387 bmap
= isl_basic_map_eliminate_vars(bmap
, isl_dim_total(bmap
->dim
),
1392 return isl_basic_map_finalize(bmap
);
1395 __isl_give isl_basic_set
*isl_basic_set_remove_divs(
1396 __isl_take isl_basic_set
*bset
)
1398 return (struct isl_basic_set
*)isl_basic_map_remove_divs(
1399 (struct isl_basic_map
*)bset
);
1402 __isl_give isl_map
*isl_map_remove_divs(__isl_take isl_map
*map
)
1411 map
= isl_map_cow(map
);
1415 for (i
= 0; i
< map
->n
; ++i
) {
1416 map
->p
[i
] = isl_basic_map_remove_divs(map
->p
[i
]);
1426 __isl_give isl_set
*isl_set_remove_divs(__isl_take isl_set
*set
)
1428 return isl_map_remove_divs(set
);
1431 struct isl_basic_map
*isl_basic_map_remove_dims(struct isl_basic_map
*bmap
,
1432 enum isl_dim_type type
, unsigned first
, unsigned n
)
1436 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
1440 bmap
= isl_basic_map_eliminate_vars(bmap
,
1441 isl_basic_map_offset(bmap
, type
) - 1 + first
, n
);
1444 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
) && type
== isl_dim_div
)
1446 bmap
= isl_basic_map_drop(bmap
, type
, first
, n
);
1449 isl_basic_map_free(bmap
);
1453 /* Return true if the definition of the given div (recursively) involves
1454 * any of the given variables.
1456 static int div_involves_vars(__isl_keep isl_basic_map
*bmap
, int div
,
1457 unsigned first
, unsigned n
)
1460 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
1462 if (isl_int_is_zero(bmap
->div
[div
][0]))
1464 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + first
, n
) >= 0)
1467 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
1468 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
1470 if (div_involves_vars(bmap
, i
, first
, n
))
1477 /* Remove all divs (recursively) involving any of the given dimensions
1478 * in their definitions.
1480 __isl_give isl_basic_map
*isl_basic_map_remove_divs_involving_dims(
1481 __isl_take isl_basic_map
*bmap
,
1482 enum isl_dim_type type
, unsigned first
, unsigned n
)
1488 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
1490 first
+= isl_basic_map_offset(bmap
, type
);
1492 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
1493 if (!div_involves_vars(bmap
, i
, first
, n
))
1495 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
1503 isl_basic_map_free(bmap
);
1507 __isl_give isl_map
*isl_map_remove_divs_involving_dims(__isl_take isl_map
*map
,
1508 enum isl_dim_type type
, unsigned first
, unsigned n
)
1517 map
= isl_map_cow(map
);
1521 for (i
= 0; i
< map
->n
; ++i
) {
1522 map
->p
[i
] = isl_basic_map_remove_divs_involving_dims(map
->p
[i
],
1533 __isl_give isl_set
*isl_set_remove_divs_involving_dims(__isl_take isl_set
*set
,
1534 enum isl_dim_type type
, unsigned first
, unsigned n
)
1536 return (isl_set
*)isl_map_remove_divs_involving_dims((isl_map
*)set
,
1540 int isl_basic_set_involves_dims(__isl_keep isl_basic_set
*bset
,
1541 enum isl_dim_type type
, unsigned first
, unsigned n
)
1548 if (first
+ n
> isl_basic_set_dim(bset
, type
))
1549 isl_die(bset
->ctx
, isl_error_invalid
,
1550 "index out of bounds", return -1);
1552 first
+= isl_basic_set_offset(bset
, type
);
1553 for (i
= 0; i
< bset
->n_eq
; ++i
)
1554 if (isl_seq_first_non_zero(bset
->eq
[i
] + first
, n
) >= 0)
1556 for (i
= 0; i
< bset
->n_ineq
; ++i
)
1557 if (isl_seq_first_non_zero(bset
->ineq
[i
] + first
, n
) >= 0)
1563 int isl_set_involves_dims(__isl_keep isl_set
*set
,
1564 enum isl_dim_type type
, unsigned first
, unsigned n
)
1571 if (first
+ n
> isl_set_dim(set
, type
))
1572 isl_die(set
->ctx
, isl_error_invalid
,
1573 "index out of bounds", return -1);
1575 for (i
= 0; i
< set
->n
; ++i
) {
1576 int involves
= isl_basic_set_involves_dims(set
->p
[i
],
1578 if (involves
< 0 || !involves
)
1585 /* Return true if the definition of the given div is unknown or depends
1588 static int div_is_unknown(__isl_keep isl_basic_map
*bmap
, int div
)
1591 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
1593 if (isl_int_is_zero(bmap
->div
[div
][0]))
1596 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
1597 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
1599 if (div_is_unknown(bmap
, i
))
1606 /* Remove all divs that are unknown or defined in terms of unknown divs.
1608 __isl_give isl_basic_map
*isl_basic_map_remove_unknown_divs(
1609 __isl_take isl_basic_map
*bmap
)
1616 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
1617 if (!div_is_unknown(bmap
, i
))
1619 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
1625 __isl_give isl_map
*isl_map_remove_unknown_divs(__isl_take isl_map
*map
)
1634 map
= isl_map_cow(map
);
1638 for (i
= 0; i
< map
->n
; ++i
) {
1639 map
->p
[i
] = isl_basic_map_remove_unknown_divs(map
->p
[i
]);
1649 __isl_give isl_set
*isl_set_remove_unknown_divs(__isl_take isl_set
*set
)
1651 return (isl_set
*)isl_map_remove_unknown_divs((isl_map
*)set
);
1654 __isl_give isl_basic_set
*isl_basic_set_remove_dims(
1655 __isl_take isl_basic_set
*bset
,
1656 enum isl_dim_type type
, unsigned first
, unsigned n
)
1658 return (isl_basic_set
*)
1659 isl_basic_map_remove_dims((isl_basic_map
*)bset
, type
, first
, n
);
1662 struct isl_map
*isl_map_remove_dims(struct isl_map
*map
,
1663 enum isl_dim_type type
, unsigned first
, unsigned n
)
1670 map
= isl_map_cow(map
);
1673 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
1675 for (i
= 0; i
< map
->n
; ++i
) {
1676 map
->p
[i
] = isl_basic_map_eliminate_vars(map
->p
[i
],
1677 isl_basic_map_offset(map
->p
[i
], type
) - 1 + first
, n
);
1681 map
= isl_map_drop(map
, type
, first
, n
);
1688 __isl_give isl_set
*isl_set_remove_dims(__isl_take isl_set
*bset
,
1689 enum isl_dim_type type
, unsigned first
, unsigned n
)
1691 return (isl_set
*)isl_map_remove_dims((isl_map
*)bset
, type
, first
, n
);
1694 /* Project out n inputs starting at first using Fourier-Motzkin */
1695 struct isl_map
*isl_map_remove_inputs(struct isl_map
*map
,
1696 unsigned first
, unsigned n
)
1698 return isl_map_remove_dims(map
, isl_dim_in
, first
, n
);
1701 static void dump_term(struct isl_basic_map
*bmap
,
1702 isl_int c
, int pos
, FILE *out
)
1705 unsigned in
= isl_basic_map_n_in(bmap
);
1706 unsigned dim
= in
+ isl_basic_map_n_out(bmap
);
1707 unsigned nparam
= isl_basic_map_n_param(bmap
);
1709 isl_int_print(out
, c
, 0);
1711 if (!isl_int_is_one(c
))
1712 isl_int_print(out
, c
, 0);
1713 if (pos
< 1 + nparam
) {
1714 name
= isl_dim_get_name(bmap
->dim
,
1715 isl_dim_param
, pos
- 1);
1717 fprintf(out
, "%s", name
);
1719 fprintf(out
, "p%d", pos
- 1);
1720 } else if (pos
< 1 + nparam
+ in
)
1721 fprintf(out
, "i%d", pos
- 1 - nparam
);
1722 else if (pos
< 1 + nparam
+ dim
)
1723 fprintf(out
, "o%d", pos
- 1 - nparam
- in
);
1725 fprintf(out
, "e%d", pos
- 1 - nparam
- dim
);
1729 static void dump_constraint_sign(struct isl_basic_map
*bmap
, isl_int
*c
,
1730 int sign
, FILE *out
)
1734 unsigned len
= 1 + isl_basic_map_total_dim(bmap
);
1738 for (i
= 0, first
= 1; i
< len
; ++i
) {
1739 if (isl_int_sgn(c
[i
]) * sign
<= 0)
1742 fprintf(out
, " + ");
1744 isl_int_abs(v
, c
[i
]);
1745 dump_term(bmap
, v
, i
, out
);
1752 static void dump_constraint(struct isl_basic_map
*bmap
, isl_int
*c
,
1753 const char *op
, FILE *out
, int indent
)
1757 fprintf(out
, "%*s", indent
, "");
1759 dump_constraint_sign(bmap
, c
, 1, out
);
1760 fprintf(out
, " %s ", op
);
1761 dump_constraint_sign(bmap
, c
, -1, out
);
1765 for (i
= bmap
->n_div
; i
< bmap
->extra
; ++i
) {
1766 if (isl_int_is_zero(c
[1+isl_dim_total(bmap
->dim
)+i
]))
1768 fprintf(out
, "%*s", indent
, "");
1769 fprintf(out
, "ERROR: unused div coefficient not zero\n");
1774 static void dump_constraints(struct isl_basic_map
*bmap
,
1775 isl_int
**c
, unsigned n
,
1776 const char *op
, FILE *out
, int indent
)
1780 for (i
= 0; i
< n
; ++i
)
1781 dump_constraint(bmap
, c
[i
], op
, out
, indent
);
1784 static void dump_affine(struct isl_basic_map
*bmap
, isl_int
*exp
, FILE *out
)
1788 unsigned total
= isl_basic_map_total_dim(bmap
);
1790 for (j
= 0; j
< 1 + total
; ++j
) {
1791 if (isl_int_is_zero(exp
[j
]))
1793 if (!first
&& isl_int_is_pos(exp
[j
]))
1795 dump_term(bmap
, exp
[j
], j
, out
);
1800 static void dump(struct isl_basic_map
*bmap
, FILE *out
, int indent
)
1804 dump_constraints(bmap
, bmap
->eq
, bmap
->n_eq
, "=", out
, indent
);
1805 dump_constraints(bmap
, bmap
->ineq
, bmap
->n_ineq
, ">=", out
, indent
);
1807 for (i
= 0; i
< bmap
->n_div
; ++i
) {
1808 fprintf(out
, "%*s", indent
, "");
1809 fprintf(out
, "e%d = [(", i
);
1810 dump_affine(bmap
, bmap
->div
[i
]+1, out
);
1812 isl_int_print(out
, bmap
->div
[i
][0], 0);
1813 fprintf(out
, "]\n");
1817 void isl_basic_set_print_internal(struct isl_basic_set
*bset
,
1818 FILE *out
, int indent
)
1821 fprintf(out
, "null basic set\n");
1825 fprintf(out
, "%*s", indent
, "");
1826 fprintf(out
, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
1827 bset
->ref
, bset
->dim
->nparam
, bset
->dim
->n_out
,
1828 bset
->extra
, bset
->flags
);
1829 dump((struct isl_basic_map
*)bset
, out
, indent
);
1832 void isl_basic_map_print_internal(struct isl_basic_map
*bmap
,
1833 FILE *out
, int indent
)
1836 fprintf(out
, "null basic map\n");
1840 fprintf(out
, "%*s", indent
, "");
1841 fprintf(out
, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
1842 "flags: %x, n_name: %d\n",
1844 bmap
->dim
->nparam
, bmap
->dim
->n_in
, bmap
->dim
->n_out
,
1845 bmap
->extra
, bmap
->flags
, bmap
->dim
->n_name
);
1846 dump(bmap
, out
, indent
);
1849 int isl_inequality_negate(struct isl_basic_map
*bmap
, unsigned pos
)
1854 total
= isl_basic_map_total_dim(bmap
);
1855 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
1856 isl_seq_neg(bmap
->ineq
[pos
], bmap
->ineq
[pos
], 1 + total
);
1857 isl_int_sub_ui(bmap
->ineq
[pos
][0], bmap
->ineq
[pos
][0], 1);
1858 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1862 struct isl_set
*isl_set_alloc_dim(struct isl_dim
*dim
, int n
, unsigned flags
)
1864 struct isl_set
*set
;
1868 isl_assert(dim
->ctx
, dim
->n_in
== 0, goto error
);
1869 isl_assert(dim
->ctx
, n
>= 0, goto error
);
1870 set
= isl_alloc(dim
->ctx
, struct isl_set
,
1871 sizeof(struct isl_set
) +
1872 (n
- 1) * sizeof(struct isl_basic_set
*));
1876 set
->ctx
= dim
->ctx
;
1877 isl_ctx_ref(set
->ctx
);
1889 struct isl_set
*isl_set_alloc(struct isl_ctx
*ctx
,
1890 unsigned nparam
, unsigned dim
, int n
, unsigned flags
)
1892 struct isl_set
*set
;
1893 struct isl_dim
*dims
;
1895 dims
= isl_dim_alloc(ctx
, nparam
, 0, dim
);
1899 set
= isl_set_alloc_dim(dims
, n
, flags
);
1903 /* Make sure "map" has room for at least "n" more basic maps.
1905 struct isl_map
*isl_map_grow(struct isl_map
*map
, int n
)
1908 struct isl_map
*grown
= NULL
;
1912 isl_assert(map
->ctx
, n
>= 0, goto error
);
1913 if (map
->n
+ n
<= map
->size
)
1915 grown
= isl_map_alloc_dim(isl_map_get_dim(map
), map
->n
+ n
, map
->flags
);
1918 for (i
= 0; i
< map
->n
; ++i
) {
1919 grown
->p
[i
] = isl_basic_map_copy(map
->p
[i
]);
1927 isl_map_free(grown
);
1932 /* Make sure "set" has room for at least "n" more basic sets.
1934 struct isl_set
*isl_set_grow(struct isl_set
*set
, int n
)
1936 return (struct isl_set
*)isl_map_grow((struct isl_map
*)set
, n
);
1939 struct isl_set
*isl_set_dup(struct isl_set
*set
)
1942 struct isl_set
*dup
;
1947 dup
= isl_set_alloc_dim(isl_dim_copy(set
->dim
), set
->n
, set
->flags
);
1950 for (i
= 0; i
< set
->n
; ++i
)
1951 dup
= isl_set_add_basic_set(dup
, isl_basic_set_copy(set
->p
[i
]));
1955 struct isl_set
*isl_set_from_basic_set(struct isl_basic_set
*bset
)
1957 return isl_map_from_basic_map(bset
);
1960 struct isl_map
*isl_map_from_basic_map(struct isl_basic_map
*bmap
)
1962 struct isl_map
*map
;
1967 map
= isl_map_alloc_dim(isl_dim_copy(bmap
->dim
), 1, ISL_MAP_DISJOINT
);
1968 return isl_map_add_basic_map(map
, bmap
);
1971 __isl_give isl_set
*isl_set_add_basic_set(__isl_take isl_set
*set
,
1972 __isl_take isl_basic_set
*bset
)
1974 return (struct isl_set
*)isl_map_add_basic_map((struct isl_map
*)set
,
1975 (struct isl_basic_map
*)bset
);
1978 void isl_set_free(struct isl_set
*set
)
1988 isl_ctx_deref(set
->ctx
);
1989 for (i
= 0; i
< set
->n
; ++i
)
1990 isl_basic_set_free(set
->p
[i
]);
1991 isl_dim_free(set
->dim
);
1995 void isl_set_print_internal(struct isl_set
*set
, FILE *out
, int indent
)
2000 fprintf(out
, "null set\n");
2004 fprintf(out
, "%*s", indent
, "");
2005 fprintf(out
, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
2006 set
->ref
, set
->n
, set
->dim
->nparam
, set
->dim
->n_out
,
2008 for (i
= 0; i
< set
->n
; ++i
) {
2009 fprintf(out
, "%*s", indent
, "");
2010 fprintf(out
, "basic set %d:\n", i
);
2011 isl_basic_set_print_internal(set
->p
[i
], out
, indent
+4);
2015 void isl_map_print_internal(struct isl_map
*map
, FILE *out
, int indent
)
2020 fprintf(out
, "null map\n");
2024 fprintf(out
, "%*s", indent
, "");
2025 fprintf(out
, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2026 "flags: %x, n_name: %d\n",
2027 map
->ref
, map
->n
, map
->dim
->nparam
, map
->dim
->n_in
,
2028 map
->dim
->n_out
, map
->flags
, map
->dim
->n_name
);
2029 for (i
= 0; i
< map
->n
; ++i
) {
2030 fprintf(out
, "%*s", indent
, "");
2031 fprintf(out
, "basic map %d:\n", i
);
2032 isl_basic_map_print_internal(map
->p
[i
], out
, indent
+4);
2036 struct isl_basic_map
*isl_basic_map_intersect_domain(
2037 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
2039 struct isl_basic_map
*bmap_domain
;
2044 isl_assert(bset
->ctx
, isl_dim_match(bmap
->dim
, isl_dim_param
,
2045 bset
->dim
, isl_dim_param
), goto error
);
2047 if (isl_dim_size(bset
->dim
, isl_dim_set
) != 0)
2048 isl_assert(bset
->ctx
,
2049 isl_basic_map_compatible_domain(bmap
, bset
), goto error
);
2051 bmap
= isl_basic_map_cow(bmap
);
2054 bmap
= isl_basic_map_extend_dim(bmap
, isl_dim_copy(bmap
->dim
),
2055 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
2056 bmap_domain
= isl_basic_map_from_domain(bset
);
2057 bmap
= add_constraints(bmap
, bmap_domain
, 0, 0);
2059 bmap
= isl_basic_map_simplify(bmap
);
2060 return isl_basic_map_finalize(bmap
);
2062 isl_basic_map_free(bmap
);
2063 isl_basic_set_free(bset
);
2067 struct isl_basic_map
*isl_basic_map_intersect_range(
2068 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
2070 struct isl_basic_map
*bmap_range
;
2075 isl_assert(bset
->ctx
, isl_dim_match(bmap
->dim
, isl_dim_param
,
2076 bset
->dim
, isl_dim_param
), goto error
);
2078 if (isl_dim_size(bset
->dim
, isl_dim_set
) != 0)
2079 isl_assert(bset
->ctx
,
2080 isl_basic_map_compatible_range(bmap
, bset
), goto error
);
2082 if (isl_basic_set_is_universe(bset
)) {
2083 isl_basic_set_free(bset
);
2087 bmap
= isl_basic_map_cow(bmap
);
2090 bmap
= isl_basic_map_extend_dim(bmap
, isl_dim_copy(bmap
->dim
),
2091 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
2092 bmap_range
= isl_basic_map_from_basic_set(bset
, isl_dim_copy(bset
->dim
));
2093 bmap
= add_constraints(bmap
, bmap_range
, 0, 0);
2095 bmap
= isl_basic_map_simplify(bmap
);
2096 return isl_basic_map_finalize(bmap
);
2098 isl_basic_map_free(bmap
);
2099 isl_basic_set_free(bset
);
2103 int isl_basic_map_contains(struct isl_basic_map
*bmap
, struct isl_vec
*vec
)
2109 total
= 1 + isl_basic_map_total_dim(bmap
);
2110 if (total
!= vec
->size
)
2115 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
2116 isl_seq_inner_product(vec
->el
, bmap
->eq
[i
], total
, &s
);
2117 if (!isl_int_is_zero(s
)) {
2123 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
2124 isl_seq_inner_product(vec
->el
, bmap
->ineq
[i
], total
, &s
);
2125 if (isl_int_is_neg(s
)) {
2136 int isl_basic_set_contains(struct isl_basic_set
*bset
, struct isl_vec
*vec
)
2138 return isl_basic_map_contains((struct isl_basic_map
*)bset
, vec
);
2141 struct isl_basic_map
*isl_basic_map_intersect(
2142 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
2144 struct isl_vec
*sample
= NULL
;
2146 if (!bmap1
|| !bmap2
)
2149 isl_assert(bmap1
->ctx
, isl_dim_match(bmap1
->dim
, isl_dim_param
,
2150 bmap2
->dim
, isl_dim_param
), goto error
);
2151 if (isl_dim_total(bmap1
->dim
) ==
2152 isl_dim_size(bmap1
->dim
, isl_dim_param
) &&
2153 isl_dim_total(bmap2
->dim
) !=
2154 isl_dim_size(bmap2
->dim
, isl_dim_param
))
2155 return isl_basic_map_intersect(bmap2
, bmap1
);
2157 if (isl_dim_total(bmap2
->dim
) !=
2158 isl_dim_size(bmap2
->dim
, isl_dim_param
))
2159 isl_assert(bmap1
->ctx
,
2160 isl_dim_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
2162 if (bmap1
->sample
&&
2163 isl_basic_map_contains(bmap1
, bmap1
->sample
) > 0 &&
2164 isl_basic_map_contains(bmap2
, bmap1
->sample
) > 0)
2165 sample
= isl_vec_copy(bmap1
->sample
);
2166 else if (bmap2
->sample
&&
2167 isl_basic_map_contains(bmap1
, bmap2
->sample
) > 0 &&
2168 isl_basic_map_contains(bmap2
, bmap2
->sample
) > 0)
2169 sample
= isl_vec_copy(bmap2
->sample
);
2171 bmap1
= isl_basic_map_cow(bmap1
);
2174 bmap1
= isl_basic_map_extend_dim(bmap1
, isl_dim_copy(bmap1
->dim
),
2175 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
2176 bmap1
= add_constraints(bmap1
, bmap2
, 0, 0);
2179 isl_vec_free(sample
);
2181 isl_vec_free(bmap1
->sample
);
2182 bmap1
->sample
= sample
;
2185 bmap1
= isl_basic_map_simplify(bmap1
);
2186 return isl_basic_map_finalize(bmap1
);
2189 isl_vec_free(sample
);
2190 isl_basic_map_free(bmap1
);
2191 isl_basic_map_free(bmap2
);
2195 struct isl_basic_set
*isl_basic_set_intersect(
2196 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
2198 return (struct isl_basic_set
*)
2199 isl_basic_map_intersect(
2200 (struct isl_basic_map
*)bset1
,
2201 (struct isl_basic_map
*)bset2
);
2204 /* Special case of isl_map_intersect, where both map1 and map2
2205 * are convex, without any divs and such that either map1 or map2
2206 * contains a single constraint. This constraint is then simply
2207 * added to the other map.
2209 static __isl_give isl_map
*map_intersect_add_constraint(
2210 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
2212 isl_assert(map1
->ctx
, map1
->n
== 1, goto error
);
2213 isl_assert(map2
->ctx
, map1
->n
== 1, goto error
);
2214 isl_assert(map1
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
2215 isl_assert(map2
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
2217 if (map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
!= 1)
2218 return isl_map_intersect(map2
, map1
);
2220 isl_assert(map2
->ctx
,
2221 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1, goto error
);
2223 map1
= isl_map_cow(map1
);
2226 if (isl_map_plain_is_empty(map1
)) {
2230 map1
->p
[0] = isl_basic_map_cow(map1
->p
[0]);
2231 if (map2
->p
[0]->n_eq
== 1)
2232 map1
->p
[0] = isl_basic_map_add_eq(map1
->p
[0], map2
->p
[0]->eq
[0]);
2234 map1
->p
[0] = isl_basic_map_add_ineq(map1
->p
[0],
2235 map2
->p
[0]->ineq
[0]);
2237 map1
->p
[0] = isl_basic_map_simplify(map1
->p
[0]);
2238 map1
->p
[0] = isl_basic_map_finalize(map1
->p
[0]);
2242 if (isl_basic_map_plain_is_empty(map1
->p
[0])) {
2243 isl_basic_map_free(map1
->p
[0]);
2256 struct isl_map
*isl_map_intersect(struct isl_map
*map1
, struct isl_map
*map2
)
2259 struct isl_map
*result
;
2265 if (isl_map_plain_is_empty(map1
)) {
2269 if (isl_map_plain_is_empty(map2
)) {
2274 if (map1
->n
== 1 && map2
->n
== 1 &&
2275 map1
->p
[0]->n_div
== 0 && map2
->p
[0]->n_div
== 0 &&
2276 isl_dim_equal(map1
->dim
, map2
->dim
) &&
2277 (map1
->p
[0]->n_eq
+ map1
->p
[0]->n_ineq
== 1 ||
2278 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1))
2279 return map_intersect_add_constraint(map1
, map2
);
2280 isl_assert(map1
->ctx
, isl_dim_match(map1
->dim
, isl_dim_param
,
2281 map2
->dim
, isl_dim_param
), goto error
);
2282 if (isl_dim_total(map1
->dim
) ==
2283 isl_dim_size(map1
->dim
, isl_dim_param
) &&
2284 isl_dim_total(map2
->dim
) != isl_dim_size(map2
->dim
, isl_dim_param
))
2285 return isl_map_intersect(map2
, map1
);
2287 if (isl_dim_total(map2
->dim
) != isl_dim_size(map2
->dim
, isl_dim_param
))
2288 isl_assert(map1
->ctx
,
2289 isl_dim_equal(map1
->dim
, map2
->dim
), goto error
);
2291 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
2292 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
2293 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
2295 result
= isl_map_alloc_dim(isl_dim_copy(map1
->dim
),
2296 map1
->n
* map2
->n
, flags
);
2299 for (i
= 0; i
< map1
->n
; ++i
)
2300 for (j
= 0; j
< map2
->n
; ++j
) {
2301 struct isl_basic_map
*part
;
2302 part
= isl_basic_map_intersect(
2303 isl_basic_map_copy(map1
->p
[i
]),
2304 isl_basic_map_copy(map2
->p
[j
]));
2305 if (isl_basic_map_is_empty(part
))
2306 isl_basic_map_free(part
);
2308 result
= isl_map_add_basic_map(result
, part
);
2321 struct isl_set
*isl_set_intersect(struct isl_set
*set1
, struct isl_set
*set2
)
2323 return (struct isl_set
*)
2324 isl_map_intersect((struct isl_map
*)set1
,
2325 (struct isl_map
*)set2
);
2328 struct isl_basic_map
*isl_basic_map_reverse(struct isl_basic_map
*bmap
)
2330 struct isl_dim
*dim
;
2331 struct isl_basic_set
*bset
;
2336 bmap
= isl_basic_map_cow(bmap
);
2339 dim
= isl_dim_reverse(isl_dim_copy(bmap
->dim
));
2340 in
= isl_basic_map_n_in(bmap
);
2341 bset
= isl_basic_set_from_basic_map(bmap
);
2342 bset
= isl_basic_set_swap_vars(bset
, in
);
2343 return isl_basic_map_from_basic_set(bset
, dim
);
2346 __isl_give isl_basic_map
*isl_basic_map_insert(__isl_take isl_basic_map
*bmap
,
2347 enum isl_dim_type type
, unsigned pos
, unsigned n
)
2349 struct isl_dim
*res_dim
;
2350 struct isl_basic_map
*res
;
2351 struct isl_dim_map
*dim_map
;
2352 unsigned total
, off
;
2353 enum isl_dim_type t
;
2361 res_dim
= isl_dim_insert(isl_basic_map_get_dim(bmap
), type
, pos
, n
);
2363 total
= isl_basic_map_total_dim(bmap
) + n
;
2364 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
2366 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
2368 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
2370 unsigned size
= isl_basic_map_dim(bmap
, t
);
2371 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2373 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2374 pos
, size
- pos
, off
+ pos
+ n
);
2376 off
+= isl_dim_size(res_dim
, t
);
2378 isl_dim_map_div(dim_map
, bmap
, off
);
2380 res
= isl_basic_map_alloc_dim(res_dim
,
2381 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
2382 if (isl_basic_map_is_rational(bmap
))
2383 res
= isl_basic_map_set_rational(res
);
2384 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
2385 return isl_basic_map_finalize(res
);
2388 __isl_give isl_basic_map
*isl_basic_map_add(__isl_take isl_basic_map
*bmap
,
2389 enum isl_dim_type type
, unsigned n
)
2393 return isl_basic_map_insert(bmap
, type
,
2394 isl_basic_map_dim(bmap
, type
), n
);
2397 __isl_give isl_basic_set
*isl_basic_set_add(__isl_take isl_basic_set
*bset
,
2398 enum isl_dim_type type
, unsigned n
)
2402 isl_assert(bset
->ctx
, type
!= isl_dim_in
, goto error
);
2403 return (isl_basic_set
*)isl_basic_map_add((isl_basic_map
*)bset
, type
, n
);
2405 isl_basic_set_free(bset
);
2409 __isl_give isl_map
*isl_map_insert(__isl_take isl_map
*map
,
2410 enum isl_dim_type type
, unsigned pos
, unsigned n
)
2417 map
= isl_map_cow(map
);
2421 map
->dim
= isl_dim_insert(map
->dim
, type
, pos
, n
);
2425 for (i
= 0; i
< map
->n
; ++i
) {
2426 map
->p
[i
] = isl_basic_map_insert(map
->p
[i
], type
, pos
, n
);
2437 __isl_give isl_set
*isl_set_insert(__isl_take isl_set
*set
,
2438 enum isl_dim_type type
, unsigned pos
, unsigned n
)
2440 return (isl_set
*)isl_map_insert((isl_map
*)set
, type
, pos
, n
);
2443 __isl_give isl_map
*isl_map_add_dims(__isl_take isl_map
*map
,
2444 enum isl_dim_type type
, unsigned n
)
2448 return isl_map_insert(map
, type
, isl_map_dim(map
, type
), n
);
2451 __isl_give isl_set
*isl_set_add_dims(__isl_take isl_set
*set
,
2452 enum isl_dim_type type
, unsigned n
)
2456 isl_assert(set
->ctx
, type
!= isl_dim_in
, goto error
);
2457 return (isl_set
*)isl_map_add_dims((isl_map
*)set
, type
, n
);
2463 __isl_give isl_basic_map
*isl_basic_map_move_dims(
2464 __isl_take isl_basic_map
*bmap
,
2465 enum isl_dim_type dst_type
, unsigned dst_pos
,
2466 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2468 struct isl_dim_map
*dim_map
;
2469 struct isl_basic_map
*res
;
2470 enum isl_dim_type t
;
2471 unsigned total
, off
;
2478 isl_assert(bmap
->ctx
, src_pos
+ n
<= isl_basic_map_dim(bmap
, src_type
),
2481 if (dst_type
== src_type
&& dst_pos
== src_pos
)
2484 isl_assert(bmap
->ctx
, dst_type
!= src_type
, goto error
);
2486 if (pos(bmap
->dim
, dst_type
) + dst_pos
==
2487 pos(bmap
->dim
, src_type
) + src_pos
+
2488 ((src_type
< dst_type
) ? n
: 0)) {
2489 bmap
= isl_basic_map_cow(bmap
);
2493 bmap
->dim
= isl_dim_move(bmap
->dim
, dst_type
, dst_pos
,
2494 src_type
, src_pos
, n
);
2498 bmap
= isl_basic_map_finalize(bmap
);
2503 total
= isl_basic_map_total_dim(bmap
);
2504 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
2507 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
2508 unsigned size
= isl_dim_size(bmap
->dim
, t
);
2509 if (t
== dst_type
) {
2510 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2513 isl_dim_map_dim_range(dim_map
, bmap
->dim
, src_type
,
2516 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2517 dst_pos
, size
- dst_pos
, off
);
2518 off
+= size
- dst_pos
;
2519 } else if (t
== src_type
) {
2520 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2523 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2524 src_pos
+ n
, size
- src_pos
- n
, off
);
2525 off
+= size
- src_pos
- n
;
2527 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
2531 isl_dim_map_div(dim_map
, bmap
, off
);
2533 res
= isl_basic_map_alloc_dim(isl_basic_map_get_dim(bmap
),
2534 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
2535 bmap
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
2537 bmap
->dim
= isl_dim_move(bmap
->dim
, dst_type
, dst_pos
,
2538 src_type
, src_pos
, n
);
2542 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
2543 bmap
= isl_basic_map_gauss(bmap
, NULL
);
2544 bmap
= isl_basic_map_finalize(bmap
);
2548 isl_basic_map_free(bmap
);
2552 __isl_give isl_basic_set
*isl_basic_set_move_dims(__isl_take isl_basic_set
*bset
,
2553 enum isl_dim_type dst_type
, unsigned dst_pos
,
2554 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2556 return (isl_basic_set
*)isl_basic_map_move_dims(
2557 (isl_basic_map
*)bset
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
2560 __isl_give isl_set
*isl_set_move_dims(__isl_take isl_set
*set
,
2561 enum isl_dim_type dst_type
, unsigned dst_pos
,
2562 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2566 isl_assert(set
->ctx
, dst_type
!= isl_dim_in
, goto error
);
2567 return (isl_set
*)isl_map_move_dims((isl_map
*)set
, dst_type
, dst_pos
,
2568 src_type
, src_pos
, n
);
2574 __isl_give isl_map
*isl_map_move_dims(__isl_take isl_map
*map
,
2575 enum isl_dim_type dst_type
, unsigned dst_pos
,
2576 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2585 isl_assert(map
->ctx
, src_pos
+ n
<= isl_map_dim(map
, src_type
),
2588 if (dst_type
== src_type
&& dst_pos
== src_pos
)
2591 isl_assert(map
->ctx
, dst_type
!= src_type
, goto error
);
2593 map
= isl_map_cow(map
);
2597 map
->dim
= isl_dim_move(map
->dim
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
2601 for (i
= 0; i
< map
->n
; ++i
) {
2602 map
->p
[i
] = isl_basic_map_move_dims(map
->p
[i
],
2604 src_type
, src_pos
, n
);
2615 /* Move the specified dimensions to the last columns right before
2616 * the divs. Don't change the dimension specification of bmap.
2617 * That's the responsibility of the caller.
2619 static __isl_give isl_basic_map
*move_last(__isl_take isl_basic_map
*bmap
,
2620 enum isl_dim_type type
, unsigned first
, unsigned n
)
2622 struct isl_dim_map
*dim_map
;
2623 struct isl_basic_map
*res
;
2624 enum isl_dim_type t
;
2625 unsigned total
, off
;
2629 if (pos(bmap
->dim
, type
) + first
+ n
== 1 + isl_dim_total(bmap
->dim
))
2632 total
= isl_basic_map_total_dim(bmap
);
2633 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
2636 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
2637 unsigned size
= isl_dim_size(bmap
->dim
, t
);
2639 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2642 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2643 first
, n
, total
- bmap
->n_div
- n
);
2644 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2645 first
+ n
, size
- (first
+ n
), off
);
2646 off
+= size
- (first
+ n
);
2648 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
2652 isl_dim_map_div(dim_map
, bmap
, off
+ n
);
2654 res
= isl_basic_map_alloc_dim(isl_basic_map_get_dim(bmap
),
2655 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
2656 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
2660 /* Turn the n dimensions of type type, starting at first
2661 * into existentially quantified variables.
2663 __isl_give isl_basic_map
*isl_basic_map_project_out(
2664 __isl_take isl_basic_map
*bmap
,
2665 enum isl_dim_type type
, unsigned first
, unsigned n
)
2678 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
2679 return isl_basic_map_remove_dims(bmap
, type
, first
, n
);
2681 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
2684 bmap
= move_last(bmap
, type
, first
, n
);
2685 bmap
= isl_basic_map_cow(bmap
);
2689 row_size
= 1 + isl_dim_total(bmap
->dim
) + bmap
->extra
;
2690 old
= bmap
->block2
.data
;
2691 bmap
->block2
= isl_blk_extend(bmap
->ctx
, bmap
->block2
,
2692 (bmap
->extra
+ n
) * (1 + row_size
));
2693 if (!bmap
->block2
.data
)
2695 new_div
= isl_alloc_array(bmap
->ctx
, isl_int
*, bmap
->extra
+ n
);
2698 for (i
= 0; i
< n
; ++i
) {
2699 new_div
[i
] = bmap
->block2
.data
+
2700 (bmap
->extra
+ i
) * (1 + row_size
);
2701 isl_seq_clr(new_div
[i
], 1 + row_size
);
2703 for (i
= 0; i
< bmap
->extra
; ++i
)
2704 new_div
[n
+ i
] = bmap
->block2
.data
+ (bmap
->div
[i
] - old
);
2706 bmap
->div
= new_div
;
2710 bmap
->dim
= isl_dim_drop(bmap
->dim
, type
, first
, n
);
2713 bmap
= isl_basic_map_simplify(bmap
);
2714 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
2715 return isl_basic_map_finalize(bmap
);
2717 isl_basic_map_free(bmap
);
2721 /* Turn the n dimensions of type type, starting at first
2722 * into existentially quantified variables.
2724 struct isl_basic_set
*isl_basic_set_project_out(struct isl_basic_set
*bset
,
2725 enum isl_dim_type type
, unsigned first
, unsigned n
)
2727 return (isl_basic_set
*)isl_basic_map_project_out(
2728 (isl_basic_map
*)bset
, type
, first
, n
);
2731 /* Turn the n dimensions of type type, starting at first
2732 * into existentially quantified variables.
2734 __isl_give isl_map
*isl_map_project_out(__isl_take isl_map
*map
,
2735 enum isl_dim_type type
, unsigned first
, unsigned n
)
2745 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
2747 map
= isl_map_cow(map
);
2751 map
->dim
= isl_dim_drop(map
->dim
, type
, first
, n
);
2755 for (i
= 0; i
< map
->n
; ++i
) {
2756 map
->p
[i
] = isl_basic_map_project_out(map
->p
[i
], type
, first
, n
);
2767 /* Turn the n dimensions of type type, starting at first
2768 * into existentially quantified variables.
2770 __isl_give isl_set
*isl_set_project_out(__isl_take isl_set
*set
,
2771 enum isl_dim_type type
, unsigned first
, unsigned n
)
2773 return (isl_set
*)isl_map_project_out((isl_map
*)set
, type
, first
, n
);
2776 static struct isl_basic_map
*add_divs(struct isl_basic_map
*bmap
, unsigned n
)
2780 for (i
= 0; i
< n
; ++i
) {
2781 j
= isl_basic_map_alloc_div(bmap
);
2784 isl_seq_clr(bmap
->div
[j
], 1+1+isl_basic_map_total_dim(bmap
));
2788 isl_basic_map_free(bmap
);
2792 struct isl_basic_map
*isl_basic_map_apply_range(
2793 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
2795 struct isl_dim
*dim_result
= NULL
;
2796 struct isl_basic_map
*bmap
;
2797 unsigned n_in
, n_out
, n
, nparam
, total
, pos
;
2798 struct isl_dim_map
*dim_map1
, *dim_map2
;
2800 if (!bmap1
|| !bmap2
)
2803 dim_result
= isl_dim_join(isl_dim_copy(bmap1
->dim
),
2804 isl_dim_copy(bmap2
->dim
));
2806 n_in
= isl_basic_map_n_in(bmap1
);
2807 n_out
= isl_basic_map_n_out(bmap2
);
2808 n
= isl_basic_map_n_out(bmap1
);
2809 nparam
= isl_basic_map_n_param(bmap1
);
2811 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ n
;
2812 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
2813 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
2814 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
2815 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
2816 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
2817 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_in
);
2818 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_out
);
2819 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
2820 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
2821 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
2823 bmap
= isl_basic_map_alloc_dim(dim_result
,
2824 bmap1
->n_div
+ bmap2
->n_div
+ n
,
2825 bmap1
->n_eq
+ bmap2
->n_eq
,
2826 bmap1
->n_ineq
+ bmap2
->n_ineq
);
2827 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
2828 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
2829 bmap
= add_divs(bmap
, n
);
2830 bmap
= isl_basic_map_simplify(bmap
);
2831 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
2832 return isl_basic_map_finalize(bmap
);
2834 isl_basic_map_free(bmap1
);
2835 isl_basic_map_free(bmap2
);
2839 struct isl_basic_set
*isl_basic_set_apply(
2840 struct isl_basic_set
*bset
, struct isl_basic_map
*bmap
)
2845 isl_assert(bset
->ctx
, isl_basic_map_compatible_domain(bmap
, bset
),
2848 return (struct isl_basic_set
*)
2849 isl_basic_map_apply_range((struct isl_basic_map
*)bset
, bmap
);
2851 isl_basic_set_free(bset
);
2852 isl_basic_map_free(bmap
);
2856 struct isl_basic_map
*isl_basic_map_apply_domain(
2857 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
2859 if (!bmap1
|| !bmap2
)
2862 isl_assert(bmap1
->ctx
,
2863 isl_basic_map_n_in(bmap1
) == isl_basic_map_n_in(bmap2
), goto error
);
2864 isl_assert(bmap1
->ctx
,
2865 isl_basic_map_n_param(bmap1
) == isl_basic_map_n_param(bmap2
),
2868 bmap1
= isl_basic_map_reverse(bmap1
);
2869 bmap1
= isl_basic_map_apply_range(bmap1
, bmap2
);
2870 return isl_basic_map_reverse(bmap1
);
2872 isl_basic_map_free(bmap1
);
2873 isl_basic_map_free(bmap2
);
2877 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
2878 * A \cap B -> f(A) + f(B)
2880 struct isl_basic_map
*isl_basic_map_sum(
2881 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
2883 unsigned n_in
, n_out
, nparam
, total
, pos
;
2884 struct isl_basic_map
*bmap
= NULL
;
2885 struct isl_dim_map
*dim_map1
, *dim_map2
;
2888 if (!bmap1
|| !bmap2
)
2891 isl_assert(bmap1
->ctx
, isl_dim_equal(bmap1
->dim
, bmap2
->dim
),
2894 nparam
= isl_basic_map_n_param(bmap1
);
2895 n_in
= isl_basic_map_n_in(bmap1
);
2896 n_out
= isl_basic_map_n_out(bmap1
);
2898 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
;
2899 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
2900 dim_map2
= isl_dim_map_alloc(bmap2
->ctx
, total
);
2901 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
2902 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
);
2903 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
2904 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
2905 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_in
+ n_out
);
2906 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
2907 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
2908 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_out
);
2910 bmap
= isl_basic_map_alloc_dim(isl_dim_copy(bmap1
->dim
),
2911 bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
,
2912 bmap1
->n_eq
+ bmap2
->n_eq
+ n_out
,
2913 bmap1
->n_ineq
+ bmap2
->n_ineq
);
2914 for (i
= 0; i
< n_out
; ++i
) {
2915 int j
= isl_basic_map_alloc_equality(bmap
);
2918 isl_seq_clr(bmap
->eq
[j
], 1+total
);
2919 isl_int_set_si(bmap
->eq
[j
][1+nparam
+n_in
+i
], -1);
2920 isl_int_set_si(bmap
->eq
[j
][1+pos
+i
], 1);
2921 isl_int_set_si(bmap
->eq
[j
][1+pos
-n_out
+i
], 1);
2923 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
2924 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
2925 bmap
= add_divs(bmap
, 2 * n_out
);
2927 bmap
= isl_basic_map_simplify(bmap
);
2928 return isl_basic_map_finalize(bmap
);
2930 isl_basic_map_free(bmap
);
2931 isl_basic_map_free(bmap1
);
2932 isl_basic_map_free(bmap2
);
2936 /* Given two maps A -> f(A) and B -> g(B), construct a map
2937 * A \cap B -> f(A) + f(B)
2939 struct isl_map
*isl_map_sum(struct isl_map
*map1
, struct isl_map
*map2
)
2941 struct isl_map
*result
;
2947 isl_assert(map1
->ctx
, isl_dim_equal(map1
->dim
, map2
->dim
), goto error
);
2949 result
= isl_map_alloc_dim(isl_dim_copy(map1
->dim
),
2950 map1
->n
* map2
->n
, 0);
2953 for (i
= 0; i
< map1
->n
; ++i
)
2954 for (j
= 0; j
< map2
->n
; ++j
) {
2955 struct isl_basic_map
*part
;
2956 part
= isl_basic_map_sum(
2957 isl_basic_map_copy(map1
->p
[i
]),
2958 isl_basic_map_copy(map2
->p
[j
]));
2959 if (isl_basic_map_is_empty(part
))
2960 isl_basic_map_free(part
);
2962 result
= isl_map_add_basic_map(result
, part
);
2975 __isl_give isl_set
*isl_set_sum(__isl_take isl_set
*set1
,
2976 __isl_take isl_set
*set2
)
2978 return (isl_set
*)isl_map_sum((isl_map
*)set1
, (isl_map
*)set2
);
2981 /* Given a basic map A -> f(A), construct A -> -f(A).
2983 struct isl_basic_map
*isl_basic_map_neg(struct isl_basic_map
*bmap
)
2988 bmap
= isl_basic_map_cow(bmap
);
2992 n
= isl_basic_map_dim(bmap
, isl_dim_out
);
2993 off
= isl_basic_map_offset(bmap
, isl_dim_out
);
2994 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2995 for (j
= 0; j
< n
; ++j
)
2996 isl_int_neg(bmap
->eq
[i
][off
+j
], bmap
->eq
[i
][off
+j
]);
2997 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2998 for (j
= 0; j
< n
; ++j
)
2999 isl_int_neg(bmap
->ineq
[i
][off
+j
], bmap
->ineq
[i
][off
+j
]);
3000 for (i
= 0; i
< bmap
->n_div
; ++i
)
3001 for (j
= 0; j
< n
; ++j
)
3002 isl_int_neg(bmap
->div
[i
][1+off
+j
], bmap
->div
[i
][1+off
+j
]);
3003 return isl_basic_map_finalize(bmap
);
3006 __isl_give isl_basic_set
*isl_basic_set_neg(__isl_take isl_basic_set
*bset
)
3008 return isl_basic_map_neg(bset
);
3011 /* Given a map A -> f(A), construct A -> -f(A).
3013 struct isl_map
*isl_map_neg(struct isl_map
*map
)
3017 map
= isl_map_cow(map
);
3021 for (i
= 0; i
< map
->n
; ++i
) {
3022 map
->p
[i
] = isl_basic_map_neg(map
->p
[i
]);
3033 __isl_give isl_set
*isl_set_neg(__isl_take isl_set
*set
)
3035 return (isl_set
*)isl_map_neg((isl_map
*)set
);
3038 /* Given a basic map A -> f(A) and an integer d, construct a basic map
3039 * A -> floor(f(A)/d).
3041 struct isl_basic_map
*isl_basic_map_floordiv(struct isl_basic_map
*bmap
,
3044 unsigned n_in
, n_out
, nparam
, total
, pos
;
3045 struct isl_basic_map
*result
= NULL
;
3046 struct isl_dim_map
*dim_map
;
3052 nparam
= isl_basic_map_n_param(bmap
);
3053 n_in
= isl_basic_map_n_in(bmap
);
3054 n_out
= isl_basic_map_n_out(bmap
);
3056 total
= nparam
+ n_in
+ n_out
+ bmap
->n_div
+ n_out
;
3057 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3058 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_param
, pos
= 0);
3059 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_in
, pos
+= nparam
);
3060 isl_dim_map_div(dim_map
, bmap
, pos
+= n_in
+ n_out
);
3061 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_out
, pos
+= bmap
->n_div
);
3063 result
= isl_basic_map_alloc_dim(isl_dim_copy(bmap
->dim
),
3064 bmap
->n_div
+ n_out
,
3065 bmap
->n_eq
, bmap
->n_ineq
+ 2 * n_out
);
3066 result
= isl_basic_map_add_constraints_dim_map(result
, bmap
, dim_map
);
3067 result
= add_divs(result
, n_out
);
3068 for (i
= 0; i
< n_out
; ++i
) {
3070 j
= isl_basic_map_alloc_inequality(result
);
3073 isl_seq_clr(result
->ineq
[j
], 1+total
);
3074 isl_int_neg(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
3075 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], 1);
3076 j
= isl_basic_map_alloc_inequality(result
);
3079 isl_seq_clr(result
->ineq
[j
], 1+total
);
3080 isl_int_set(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
3081 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], -1);
3082 isl_int_sub_ui(result
->ineq
[j
][0], d
, 1);
3085 result
= isl_basic_map_simplify(result
);
3086 return isl_basic_map_finalize(result
);
3088 isl_basic_map_free(result
);
3092 /* Given a map A -> f(A) and an integer d, construct a map
3093 * A -> floor(f(A)/d).
3095 struct isl_map
*isl_map_floordiv(struct isl_map
*map
, isl_int d
)
3099 map
= isl_map_cow(map
);
3103 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
3104 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3105 for (i
= 0; i
< map
->n
; ++i
) {
3106 map
->p
[i
] = isl_basic_map_floordiv(map
->p
[i
], d
);
3117 static struct isl_basic_map
*var_equal(struct isl_basic_map
*bmap
, unsigned pos
)
3123 i
= isl_basic_map_alloc_equality(bmap
);
3126 nparam
= isl_basic_map_n_param(bmap
);
3127 n_in
= isl_basic_map_n_in(bmap
);
3128 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3129 isl_int_set_si(bmap
->eq
[i
][1+nparam
+pos
], -1);
3130 isl_int_set_si(bmap
->eq
[i
][1+nparam
+n_in
+pos
], 1);
3131 return isl_basic_map_finalize(bmap
);
3133 isl_basic_map_free(bmap
);
3137 /* Add a constraints to "bmap" expressing i_pos < o_pos
3139 static struct isl_basic_map
*var_less(struct isl_basic_map
*bmap
, unsigned pos
)
3145 i
= isl_basic_map_alloc_inequality(bmap
);
3148 nparam
= isl_basic_map_n_param(bmap
);
3149 n_in
= isl_basic_map_n_in(bmap
);
3150 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3151 isl_int_set_si(bmap
->ineq
[i
][0], -1);
3152 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
3153 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
3154 return isl_basic_map_finalize(bmap
);
3156 isl_basic_map_free(bmap
);
3160 /* Add a constraint to "bmap" expressing i_pos <= o_pos
3162 static __isl_give isl_basic_map
*var_less_or_equal(
3163 __isl_take isl_basic_map
*bmap
, unsigned pos
)
3169 i
= isl_basic_map_alloc_inequality(bmap
);
3172 nparam
= isl_basic_map_n_param(bmap
);
3173 n_in
= isl_basic_map_n_in(bmap
);
3174 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3175 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
3176 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
3177 return isl_basic_map_finalize(bmap
);
3179 isl_basic_map_free(bmap
);
3183 /* Add a constraints to "bmap" expressing i_pos > o_pos
3185 static struct isl_basic_map
*var_more(struct isl_basic_map
*bmap
, unsigned pos
)
3191 i
= isl_basic_map_alloc_inequality(bmap
);
3194 nparam
= isl_basic_map_n_param(bmap
);
3195 n_in
= isl_basic_map_n_in(bmap
);
3196 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3197 isl_int_set_si(bmap
->ineq
[i
][0], -1);
3198 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
3199 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
3200 return isl_basic_map_finalize(bmap
);
3202 isl_basic_map_free(bmap
);
3206 /* Add a constraint to "bmap" expressing i_pos >= o_pos
3208 static __isl_give isl_basic_map
*var_more_or_equal(
3209 __isl_take isl_basic_map
*bmap
, unsigned pos
)
3215 i
= isl_basic_map_alloc_inequality(bmap
);
3218 nparam
= isl_basic_map_n_param(bmap
);
3219 n_in
= isl_basic_map_n_in(bmap
);
3220 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3221 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
3222 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
3223 return isl_basic_map_finalize(bmap
);
3225 isl_basic_map_free(bmap
);
3229 struct isl_basic_map
*isl_basic_map_equal(struct isl_dim
*dim
, unsigned n_equal
)
3232 struct isl_basic_map
*bmap
;
3233 bmap
= isl_basic_map_alloc_dim(dim
, 0, n_equal
, 0);
3236 for (i
= 0; i
< n_equal
&& bmap
; ++i
)
3237 bmap
= var_equal(bmap
, i
);
3238 return isl_basic_map_finalize(bmap
);
3241 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
3243 struct isl_basic_map
*isl_basic_map_less_at(struct isl_dim
*dim
, unsigned pos
)
3246 struct isl_basic_map
*bmap
;
3247 bmap
= isl_basic_map_alloc_dim(dim
, 0, pos
, 1);
3250 for (i
= 0; i
< pos
&& bmap
; ++i
)
3251 bmap
= var_equal(bmap
, i
);
3253 bmap
= var_less(bmap
, pos
);
3254 return isl_basic_map_finalize(bmap
);
3257 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
3259 __isl_give isl_basic_map
*isl_basic_map_less_or_equal_at(
3260 __isl_take isl_dim
*dim
, unsigned pos
)
3263 isl_basic_map
*bmap
;
3265 bmap
= isl_basic_map_alloc_dim(dim
, 0, pos
, 1);
3266 for (i
= 0; i
< pos
; ++i
)
3267 bmap
= var_equal(bmap
, i
);
3268 bmap
= var_less_or_equal(bmap
, pos
);
3269 return isl_basic_map_finalize(bmap
);
3272 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
3274 struct isl_basic_map
*isl_basic_map_more_at(struct isl_dim
*dim
, unsigned pos
)
3277 struct isl_basic_map
*bmap
;
3278 bmap
= isl_basic_map_alloc_dim(dim
, 0, pos
, 1);
3281 for (i
= 0; i
< pos
&& bmap
; ++i
)
3282 bmap
= var_equal(bmap
, i
);
3284 bmap
= var_more(bmap
, pos
);
3285 return isl_basic_map_finalize(bmap
);
3288 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
3290 __isl_give isl_basic_map
*isl_basic_map_more_or_equal_at(
3291 __isl_take isl_dim
*dim
, unsigned pos
)
3294 isl_basic_map
*bmap
;
3296 bmap
= isl_basic_map_alloc_dim(dim
, 0, pos
, 1);
3297 for (i
= 0; i
< pos
; ++i
)
3298 bmap
= var_equal(bmap
, i
);
3299 bmap
= var_more_or_equal(bmap
, pos
);
3300 return isl_basic_map_finalize(bmap
);
3303 static __isl_give isl_map
*map_lex_lte_first(__isl_take isl_dim
*dims
,
3304 unsigned n
, int equal
)
3306 struct isl_map
*map
;
3309 if (n
== 0 && equal
)
3310 return isl_map_universe(dims
);
3312 map
= isl_map_alloc_dim(isl_dim_copy(dims
), n
, ISL_MAP_DISJOINT
);
3314 for (i
= 0; i
+ 1 < n
; ++i
)
3315 map
= isl_map_add_basic_map(map
,
3316 isl_basic_map_less_at(isl_dim_copy(dims
), i
));
3319 map
= isl_map_add_basic_map(map
,
3320 isl_basic_map_less_or_equal_at(dims
, n
- 1));
3322 map
= isl_map_add_basic_map(map
,
3323 isl_basic_map_less_at(dims
, n
- 1));
3330 static __isl_give isl_map
*map_lex_lte(__isl_take isl_dim
*dims
, int equal
)
3334 return map_lex_lte_first(dims
, dims
->n_out
, equal
);
3337 __isl_give isl_map
*isl_map_lex_lt_first(__isl_take isl_dim
*dim
, unsigned n
)
3339 return map_lex_lte_first(dim
, n
, 0);
3342 __isl_give isl_map
*isl_map_lex_le_first(__isl_take isl_dim
*dim
, unsigned n
)
3344 return map_lex_lte_first(dim
, n
, 1);
3347 __isl_give isl_map
*isl_map_lex_lt(__isl_take isl_dim
*set_dim
)
3349 return map_lex_lte(isl_dim_map_from_set(set_dim
), 0);
3352 __isl_give isl_map
*isl_map_lex_le(__isl_take isl_dim
*set_dim
)
3354 return map_lex_lte(isl_dim_map_from_set(set_dim
), 1);
3357 static __isl_give isl_map
*map_lex_gte_first(__isl_take isl_dim
*dims
,
3358 unsigned n
, int equal
)
3360 struct isl_map
*map
;
3363 if (n
== 0 && equal
)
3364 return isl_map_universe(dims
);
3366 map
= isl_map_alloc_dim(isl_dim_copy(dims
), n
, ISL_MAP_DISJOINT
);
3368 for (i
= 0; i
+ 1 < n
; ++i
)
3369 map
= isl_map_add_basic_map(map
,
3370 isl_basic_map_more_at(isl_dim_copy(dims
), i
));
3373 map
= isl_map_add_basic_map(map
,
3374 isl_basic_map_more_or_equal_at(dims
, n
- 1));
3376 map
= isl_map_add_basic_map(map
,
3377 isl_basic_map_more_at(dims
, n
- 1));
3384 static __isl_give isl_map
*map_lex_gte(__isl_take isl_dim
*dims
, int equal
)
3388 return map_lex_gte_first(dims
, dims
->n_out
, equal
);
3391 __isl_give isl_map
*isl_map_lex_gt_first(__isl_take isl_dim
*dim
, unsigned n
)
3393 return map_lex_gte_first(dim
, n
, 0);
3396 __isl_give isl_map
*isl_map_lex_ge_first(__isl_take isl_dim
*dim
, unsigned n
)
3398 return map_lex_gte_first(dim
, n
, 1);
3401 __isl_give isl_map
*isl_map_lex_gt(__isl_take isl_dim
*set_dim
)
3403 return map_lex_gte(isl_dim_map_from_set(set_dim
), 0);
3406 __isl_give isl_map
*isl_map_lex_ge(__isl_take isl_dim
*set_dim
)
3408 return map_lex_gte(isl_dim_map_from_set(set_dim
), 1);
3411 __isl_give isl_map
*isl_set_lex_le_set(__isl_take isl_set
*set1
,
3412 __isl_take isl_set
*set2
)
3415 map
= isl_map_lex_le(isl_set_get_dim(set1
));
3416 map
= isl_map_intersect_domain(map
, set1
);
3417 map
= isl_map_intersect_range(map
, set2
);
3421 __isl_give isl_map
*isl_set_lex_lt_set(__isl_take isl_set
*set1
,
3422 __isl_take isl_set
*set2
)
3425 map
= isl_map_lex_lt(isl_set_get_dim(set1
));
3426 map
= isl_map_intersect_domain(map
, set1
);
3427 map
= isl_map_intersect_range(map
, set2
);
3431 __isl_give isl_map
*isl_set_lex_ge_set(__isl_take isl_set
*set1
,
3432 __isl_take isl_set
*set2
)
3435 map
= isl_map_lex_ge(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_gt_set(__isl_take isl_set
*set1
,
3442 __isl_take isl_set
*set2
)
3445 map
= isl_map_lex_gt(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_map_lex_le_map(__isl_take isl_map
*map1
,
3452 __isl_take isl_map
*map2
)
3455 map
= isl_map_lex_le(isl_dim_range(isl_map_get_dim(map1
)));
3456 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
3457 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
3461 __isl_give isl_map
*isl_map_lex_lt_map(__isl_take isl_map
*map1
,
3462 __isl_take isl_map
*map2
)
3465 map
= isl_map_lex_lt(isl_dim_range(isl_map_get_dim(map1
)));
3466 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
3467 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
3471 __isl_give isl_map
*isl_map_lex_ge_map(__isl_take isl_map
*map1
,
3472 __isl_take isl_map
*map2
)
3475 map
= isl_map_lex_ge(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_gt_map(__isl_take isl_map
*map1
,
3482 __isl_take isl_map
*map2
)
3485 map
= isl_map_lex_gt(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 struct isl_basic_map
*isl_basic_map_from_basic_set(
3492 struct isl_basic_set
*bset
, struct isl_dim
*dim
)
3494 struct isl_basic_map
*bmap
;
3496 bset
= isl_basic_set_cow(bset
);
3500 isl_assert(bset
->ctx
, isl_dim_compatible(bset
->dim
, dim
), goto error
);
3501 isl_dim_free(bset
->dim
);
3502 bmap
= (struct isl_basic_map
*) bset
;
3504 return isl_basic_map_finalize(bmap
);
3506 isl_basic_set_free(bset
);
3511 struct isl_basic_set
*isl_basic_set_from_basic_map(struct isl_basic_map
*bmap
)
3515 if (bmap
->dim
->n_in
== 0)
3516 return (struct isl_basic_set
*)bmap
;
3517 bmap
= isl_basic_map_cow(bmap
);
3520 bmap
->dim
= isl_dim_as_set_dim(bmap
->dim
);
3523 bmap
= isl_basic_map_finalize(bmap
);
3524 return (struct isl_basic_set
*)bmap
;
3526 isl_basic_map_free(bmap
);
3530 /* For a div d = floor(f/m), add the constraints
3533 * -(f-(n-1)) + m d >= 0
3535 * Note that the second constraint is the negation of
3539 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map
*bmap
,
3540 unsigned pos
, isl_int
*div
)
3543 unsigned total
= isl_basic_map_total_dim(bmap
);
3545 i
= isl_basic_map_alloc_inequality(bmap
);
3548 isl_seq_cpy(bmap
->ineq
[i
], div
+ 1, 1 + total
);
3549 isl_int_neg(bmap
->ineq
[i
][1 + pos
], div
[0]);
3551 j
= isl_basic_map_alloc_inequality(bmap
);
3554 isl_seq_neg(bmap
->ineq
[j
], bmap
->ineq
[i
], 1 + total
);
3555 isl_int_add(bmap
->ineq
[j
][0], bmap
->ineq
[j
][0], bmap
->ineq
[j
][1 + pos
]);
3556 isl_int_sub_ui(bmap
->ineq
[j
][0], bmap
->ineq
[j
][0], 1);
3560 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set
*bset
,
3561 unsigned pos
, isl_int
*div
)
3563 return isl_basic_map_add_div_constraints_var((isl_basic_map
*)bset
,
3567 int isl_basic_map_add_div_constraints(struct isl_basic_map
*bmap
, unsigned div
)
3569 unsigned total
= isl_basic_map_total_dim(bmap
);
3570 unsigned div_pos
= total
- bmap
->n_div
+ div
;
3572 return isl_basic_map_add_div_constraints_var(bmap
, div_pos
,
3576 struct isl_basic_set
*isl_basic_map_underlying_set(
3577 struct isl_basic_map
*bmap
)
3581 if (bmap
->dim
->nparam
== 0 && bmap
->dim
->n_in
== 0 &&
3583 !isl_dim_is_named_or_nested(bmap
->dim
, isl_dim_in
) &&
3584 !isl_dim_is_named_or_nested(bmap
->dim
, isl_dim_out
))
3585 return (struct isl_basic_set
*)bmap
;
3586 bmap
= isl_basic_map_cow(bmap
);
3589 bmap
->dim
= isl_dim_underlying(bmap
->dim
, bmap
->n_div
);
3592 bmap
->extra
-= bmap
->n_div
;
3594 bmap
= isl_basic_map_finalize(bmap
);
3595 return (struct isl_basic_set
*)bmap
;
3600 __isl_give isl_basic_set
*isl_basic_set_underlying_set(
3601 __isl_take isl_basic_set
*bset
)
3603 return isl_basic_map_underlying_set((isl_basic_map
*)bset
);
3606 struct isl_basic_map
*isl_basic_map_overlying_set(
3607 struct isl_basic_set
*bset
, struct isl_basic_map
*like
)
3609 struct isl_basic_map
*bmap
;
3610 struct isl_ctx
*ctx
;
3617 isl_assert(ctx
, bset
->n_div
== 0, goto error
);
3618 isl_assert(ctx
, isl_basic_set_n_param(bset
) == 0, goto error
);
3619 isl_assert(ctx
, bset
->dim
->n_out
== isl_basic_map_total_dim(like
),
3621 if (isl_dim_equal(bset
->dim
, like
->dim
) && like
->n_div
== 0) {
3622 isl_basic_map_free(like
);
3623 return (struct isl_basic_map
*)bset
;
3625 bset
= isl_basic_set_cow(bset
);
3628 total
= bset
->dim
->n_out
+ bset
->extra
;
3629 bmap
= (struct isl_basic_map
*)bset
;
3630 isl_dim_free(bmap
->dim
);
3631 bmap
->dim
= isl_dim_copy(like
->dim
);
3634 bmap
->n_div
= like
->n_div
;
3635 bmap
->extra
+= like
->n_div
;
3639 ltotal
= total
- bmap
->extra
+ like
->extra
;
3642 bmap
->block2
= isl_blk_extend(ctx
, bmap
->block2
,
3643 bmap
->extra
* (1 + 1 + total
));
3644 if (isl_blk_is_error(bmap
->block2
))
3646 div
= isl_realloc_array(ctx
, bmap
->div
, isl_int
*, bmap
->extra
);
3650 for (i
= 0; i
< bmap
->extra
; ++i
)
3651 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + 1 + total
);
3652 for (i
= 0; i
< like
->n_div
; ++i
) {
3653 isl_seq_cpy(bmap
->div
[i
], like
->div
[i
], 1 + 1 + ltotal
);
3654 isl_seq_clr(bmap
->div
[i
]+1+1+ltotal
, total
- ltotal
);
3656 bmap
= isl_basic_map_extend_constraints(bmap
,
3657 0, 2 * like
->n_div
);
3658 for (i
= 0; i
< like
->n_div
; ++i
) {
3659 if (isl_int_is_zero(bmap
->div
[i
][0]))
3661 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
3665 isl_basic_map_free(like
);
3666 bmap
= isl_basic_map_simplify(bmap
);
3667 bmap
= isl_basic_map_finalize(bmap
);
3670 isl_basic_map_free(like
);
3671 isl_basic_set_free(bset
);
3675 struct isl_basic_set
*isl_basic_set_from_underlying_set(
3676 struct isl_basic_set
*bset
, struct isl_basic_set
*like
)
3678 return (struct isl_basic_set
*)
3679 isl_basic_map_overlying_set(bset
, (struct isl_basic_map
*)like
);
3682 struct isl_set
*isl_set_from_underlying_set(
3683 struct isl_set
*set
, struct isl_basic_set
*like
)
3689 isl_assert(set
->ctx
, set
->dim
->n_out
== isl_basic_set_total_dim(like
),
3691 if (isl_dim_equal(set
->dim
, like
->dim
) && like
->n_div
== 0) {
3692 isl_basic_set_free(like
);
3695 set
= isl_set_cow(set
);
3698 for (i
= 0; i
< set
->n
; ++i
) {
3699 set
->p
[i
] = isl_basic_set_from_underlying_set(set
->p
[i
],
3700 isl_basic_set_copy(like
));
3704 isl_dim_free(set
->dim
);
3705 set
->dim
= isl_dim_copy(like
->dim
);
3708 isl_basic_set_free(like
);
3711 isl_basic_set_free(like
);
3716 struct isl_set
*isl_map_underlying_set(struct isl_map
*map
)
3720 map
= isl_map_cow(map
);
3723 map
->dim
= isl_dim_cow(map
->dim
);
3727 for (i
= 1; i
< map
->n
; ++i
)
3728 isl_assert(map
->ctx
, map
->p
[0]->n_div
== map
->p
[i
]->n_div
,
3730 for (i
= 0; i
< map
->n
; ++i
) {
3731 map
->p
[i
] = (struct isl_basic_map
*)
3732 isl_basic_map_underlying_set(map
->p
[i
]);
3737 map
->dim
= isl_dim_underlying(map
->dim
, 0);
3739 isl_dim_free(map
->dim
);
3740 map
->dim
= isl_dim_copy(map
->p
[0]->dim
);
3744 return (struct isl_set
*)map
;
3750 struct isl_set
*isl_set_to_underlying_set(struct isl_set
*set
)
3752 return (struct isl_set
*)isl_map_underlying_set((struct isl_map
*)set
);
3755 __isl_give isl_basic_map
*isl_basic_map_reset_dim(
3756 __isl_take isl_basic_map
*bmap
, __isl_take isl_dim
*dim
)
3758 bmap
= isl_basic_map_cow(bmap
);
3762 isl_dim_free(bmap
->dim
);
3765 bmap
= isl_basic_map_finalize(bmap
);
3769 isl_basic_map_free(bmap
);
3774 __isl_give isl_basic_set
*isl_basic_set_reset_dim(
3775 __isl_take isl_basic_set
*bset
, __isl_take isl_dim
*dim
)
3777 return (isl_basic_set
*)isl_basic_map_reset_dim((isl_basic_map
*)bset
,
3781 __isl_give isl_map
*isl_map_reset_dim(__isl_take isl_map
*map
,
3782 __isl_take isl_dim
*dim
)
3786 map
= isl_map_cow(map
);
3790 for (i
= 0; i
< map
->n
; ++i
) {
3791 map
->p
[i
] = isl_basic_map_reset_dim(map
->p
[i
],
3796 isl_dim_free(map
->dim
);
3806 __isl_give isl_set
*isl_set_reset_dim(__isl_take isl_set
*set
,
3807 __isl_take isl_dim
*dim
)
3809 return (struct isl_set
*) isl_map_reset_dim((struct isl_map
*)set
, dim
);
3812 struct isl_basic_set
*isl_basic_map_domain(struct isl_basic_map
*bmap
)
3815 struct isl_basic_set
*domain
;
3821 dim
= isl_dim_domain(isl_basic_map_get_dim(bmap
));
3823 n_in
= isl_basic_map_n_in(bmap
);
3824 n_out
= isl_basic_map_n_out(bmap
);
3825 domain
= isl_basic_set_from_basic_map(bmap
);
3826 domain
= isl_basic_set_project_out(domain
, isl_dim_set
, n_in
, n_out
);
3828 domain
= isl_basic_set_reset_dim(domain
, dim
);
3833 int isl_basic_map_may_be_set(__isl_keep isl_basic_map
*bmap
)
3837 return isl_dim_may_be_set(bmap
->dim
);
3840 struct isl_basic_set
*isl_basic_map_range(struct isl_basic_map
*bmap
)
3844 if (isl_basic_map_may_be_set(bmap
))
3846 return isl_basic_map_domain(isl_basic_map_reverse(bmap
));
3849 __isl_give isl_basic_map
*isl_basic_map_domain_map(
3850 __isl_take isl_basic_map
*bmap
)
3854 isl_basic_map
*domain
;
3855 int nparam
, n_in
, n_out
;
3858 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
3859 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
3860 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
3862 dim
= isl_dim_from_range(isl_dim_domain(isl_basic_map_get_dim(bmap
)));
3863 domain
= isl_basic_map_universe(dim
);
3865 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
3866 bmap
= isl_basic_map_apply_range(bmap
, domain
);
3867 bmap
= isl_basic_map_extend_constraints(bmap
, n_in
, 0);
3869 total
= isl_basic_map_total_dim(bmap
);
3871 for (i
= 0; i
< n_in
; ++i
) {
3872 k
= isl_basic_map_alloc_equality(bmap
);
3875 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
3876 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], -1);
3877 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ n_out
+ i
], 1);
3880 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3881 return isl_basic_map_finalize(bmap
);
3883 isl_basic_map_free(bmap
);
3887 __isl_give isl_basic_map
*isl_basic_map_range_map(
3888 __isl_take isl_basic_map
*bmap
)
3892 isl_basic_map
*range
;
3893 int nparam
, n_in
, n_out
;
3896 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
3897 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
3898 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
3900 dim
= isl_dim_from_range(isl_dim_range(isl_basic_map_get_dim(bmap
)));
3901 range
= isl_basic_map_universe(dim
);
3903 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
3904 bmap
= isl_basic_map_apply_range(bmap
, range
);
3905 bmap
= isl_basic_map_extend_constraints(bmap
, n_out
, 0);
3907 total
= isl_basic_map_total_dim(bmap
);
3909 for (i
= 0; i
< n_out
; ++i
) {
3910 k
= isl_basic_map_alloc_equality(bmap
);
3913 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
3914 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ i
], -1);
3915 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ n_out
+ i
], 1);
3918 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3919 return isl_basic_map_finalize(bmap
);
3921 isl_basic_map_free(bmap
);
3925 int isl_map_may_be_set(__isl_keep isl_map
*map
)
3929 return isl_dim_may_be_set(map
->dim
);
3932 struct isl_set
*isl_map_range(struct isl_map
*map
)
3935 struct isl_set
*set
;
3939 if (isl_map_may_be_set(map
))
3940 return (isl_set
*)map
;
3942 map
= isl_map_cow(map
);
3946 set
= (struct isl_set
*) map
;
3947 set
->dim
= isl_dim_drop_inputs(set
->dim
, 0, set
->dim
->n_in
);
3950 for (i
= 0; i
< map
->n
; ++i
) {
3951 set
->p
[i
] = isl_basic_map_range(map
->p
[i
]);
3955 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
3956 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
3963 __isl_give isl_map
*isl_map_domain_map(__isl_take isl_map
*map
)
3966 isl_dim
*domain_dim
;
3968 map
= isl_map_cow(map
);
3972 domain_dim
= isl_dim_from_range(isl_dim_domain(isl_map_get_dim(map
)));
3973 map
->dim
= isl_dim_from_domain(isl_dim_wrap(map
->dim
));
3974 map
->dim
= isl_dim_join(map
->dim
, domain_dim
);
3977 for (i
= 0; i
< map
->n
; ++i
) {
3978 map
->p
[i
] = isl_basic_map_domain_map(map
->p
[i
]);
3982 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
3983 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3990 __isl_give isl_map
*isl_map_range_map(__isl_take isl_map
*map
)
3995 map
= isl_map_cow(map
);
3999 range_dim
= isl_dim_range(isl_map_get_dim(map
));
4000 map
->dim
= isl_dim_from_domain(isl_dim_wrap(map
->dim
));
4001 map
->dim
= isl_dim_join(map
->dim
, range_dim
);
4004 for (i
= 0; i
< map
->n
; ++i
) {
4005 map
->p
[i
] = isl_basic_map_range_map(map
->p
[i
]);
4009 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
4010 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4017 struct isl_map
*isl_map_from_set(struct isl_set
*set
, struct isl_dim
*dim
)
4020 struct isl_map
*map
= NULL
;
4022 set
= isl_set_cow(set
);
4025 isl_assert(set
->ctx
, isl_dim_compatible(set
->dim
, dim
), goto error
);
4026 map
= (struct isl_map
*)set
;
4027 for (i
= 0; i
< set
->n
; ++i
) {
4028 map
->p
[i
] = isl_basic_map_from_basic_set(
4029 set
->p
[i
], isl_dim_copy(dim
));
4033 isl_dim_free(map
->dim
);
4042 __isl_give isl_basic_map
*isl_basic_map_from_domain(
4043 __isl_take isl_basic_set
*bset
)
4045 return isl_basic_map_reverse(isl_basic_map_from_range(bset
));
4048 __isl_give isl_basic_map
*isl_basic_map_from_range(
4049 __isl_take isl_basic_set
*bset
)
4051 return (isl_basic_map
*)bset
;
4054 struct isl_map
*isl_map_from_range(struct isl_set
*set
)
4056 return (struct isl_map
*)set
;
4059 __isl_give isl_map
*isl_map_from_domain(__isl_take isl_set
*set
)
4061 return isl_map_reverse(isl_map_from_range(set
));
4064 __isl_give isl_basic_map
*isl_basic_map_from_domain_and_range(
4065 __isl_take isl_basic_set
*domain
, __isl_take isl_basic_set
*range
)
4067 return isl_basic_map_apply_range(isl_basic_map_from_domain(domain
),
4068 isl_basic_map_from_range(range
));
4071 __isl_give isl_map
*isl_map_from_domain_and_range(__isl_take isl_set
*domain
,
4072 __isl_take isl_set
*range
)
4074 return isl_map_apply_range(isl_map_from_domain(domain
),
4075 isl_map_from_range(range
));
4078 struct isl_set
*isl_set_from_map(struct isl_map
*map
)
4081 struct isl_set
*set
= NULL
;
4085 map
= isl_map_cow(map
);
4088 map
->dim
= isl_dim_as_set_dim(map
->dim
);
4091 set
= (struct isl_set
*)map
;
4092 for (i
= 0; i
< map
->n
; ++i
) {
4093 set
->p
[i
] = isl_basic_set_from_basic_map(map
->p
[i
]);
4103 struct isl_map
*isl_map_alloc_dim(struct isl_dim
*dim
, int n
, unsigned flags
)
4105 struct isl_map
*map
;
4109 isl_assert(dim
->ctx
, n
>= 0, return NULL
);
4110 map
= isl_alloc(dim
->ctx
, struct isl_map
,
4111 sizeof(struct isl_map
) +
4112 (n
- 1) * sizeof(struct isl_basic_map
*));
4116 map
->ctx
= dim
->ctx
;
4117 isl_ctx_ref(map
->ctx
);
4129 struct isl_map
*isl_map_alloc(struct isl_ctx
*ctx
,
4130 unsigned nparam
, unsigned in
, unsigned out
, int n
,
4133 struct isl_map
*map
;
4134 struct isl_dim
*dims
;
4136 dims
= isl_dim_alloc(ctx
, nparam
, in
, out
);
4140 map
= isl_map_alloc_dim(dims
, n
, flags
);
4144 struct isl_basic_map
*isl_basic_map_empty(struct isl_dim
*dim
)
4146 struct isl_basic_map
*bmap
;
4147 bmap
= isl_basic_map_alloc_dim(dim
, 0, 1, 0);
4148 bmap
= isl_basic_map_set_to_empty(bmap
);
4152 struct isl_basic_set
*isl_basic_set_empty(struct isl_dim
*dim
)
4154 struct isl_basic_set
*bset
;
4155 bset
= isl_basic_set_alloc_dim(dim
, 0, 1, 0);
4156 bset
= isl_basic_set_set_to_empty(bset
);
4160 struct isl_basic_map
*isl_basic_map_empty_like(struct isl_basic_map
*model
)
4162 struct isl_basic_map
*bmap
;
4165 bmap
= isl_basic_map_alloc_dim(isl_dim_copy(model
->dim
), 0, 1, 0);
4166 bmap
= isl_basic_map_set_to_empty(bmap
);
4170 struct isl_basic_map
*isl_basic_map_empty_like_map(struct isl_map
*model
)
4172 struct isl_basic_map
*bmap
;
4175 bmap
= isl_basic_map_alloc_dim(isl_dim_copy(model
->dim
), 0, 1, 0);
4176 bmap
= isl_basic_map_set_to_empty(bmap
);
4180 struct isl_basic_set
*isl_basic_set_empty_like(struct isl_basic_set
*model
)
4182 struct isl_basic_set
*bset
;
4185 bset
= isl_basic_set_alloc_dim(isl_dim_copy(model
->dim
), 0, 1, 0);
4186 bset
= isl_basic_set_set_to_empty(bset
);
4190 struct isl_basic_map
*isl_basic_map_universe(struct isl_dim
*dim
)
4192 struct isl_basic_map
*bmap
;
4193 bmap
= isl_basic_map_alloc_dim(dim
, 0, 0, 0);
4194 bmap
= isl_basic_map_finalize(bmap
);
4198 struct isl_basic_set
*isl_basic_set_universe(struct isl_dim
*dim
)
4200 struct isl_basic_set
*bset
;
4201 bset
= isl_basic_set_alloc_dim(dim
, 0, 0, 0);
4202 bset
= isl_basic_set_finalize(bset
);
4206 __isl_give isl_basic_map
*isl_basic_map_nat_universe(__isl_take isl_dim
*dim
)
4209 unsigned total
= isl_dim_total(dim
);
4210 isl_basic_map
*bmap
;
4212 bmap
= isl_basic_map_alloc_dim(dim
, 0, 0, total
);
4213 for (i
= 0; i
< total
; ++i
) {
4214 int k
= isl_basic_map_alloc_inequality(bmap
);
4217 isl_seq_clr(bmap
->ineq
[k
], 1 + total
);
4218 isl_int_set_si(bmap
->ineq
[k
][1 + i
], 1);
4222 isl_basic_map_free(bmap
);
4226 __isl_give isl_basic_set
*isl_basic_set_nat_universe(__isl_take isl_dim
*dim
)
4228 return isl_basic_map_nat_universe(dim
);
4231 __isl_give isl_map
*isl_map_nat_universe(__isl_take isl_dim
*dim
)
4233 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim
));
4236 __isl_give isl_set
*isl_set_nat_universe(__isl_take isl_dim
*dim
)
4238 return isl_map_nat_universe(dim
);
4241 __isl_give isl_basic_map
*isl_basic_map_universe_like(
4242 __isl_keep isl_basic_map
*model
)
4246 return isl_basic_map_alloc_dim(isl_dim_copy(model
->dim
), 0, 0, 0);
4249 struct isl_basic_set
*isl_basic_set_universe_like(struct isl_basic_set
*model
)
4253 return isl_basic_set_alloc_dim(isl_dim_copy(model
->dim
), 0, 0, 0);
4256 __isl_give isl_basic_set
*isl_basic_set_universe_like_set(
4257 __isl_keep isl_set
*model
)
4261 return isl_basic_set_alloc_dim(isl_dim_copy(model
->dim
), 0, 0, 0);
4264 struct isl_map
*isl_map_empty(struct isl_dim
*dim
)
4266 return isl_map_alloc_dim(dim
, 0, ISL_MAP_DISJOINT
);
4269 struct isl_map
*isl_map_empty_like(struct isl_map
*model
)
4273 return isl_map_alloc_dim(isl_dim_copy(model
->dim
), 0, ISL_MAP_DISJOINT
);
4276 struct isl_map
*isl_map_empty_like_basic_map(struct isl_basic_map
*model
)
4280 return isl_map_alloc_dim(isl_dim_copy(model
->dim
), 0, ISL_MAP_DISJOINT
);
4283 struct isl_set
*isl_set_empty(struct isl_dim
*dim
)
4285 return isl_set_alloc_dim(dim
, 0, ISL_MAP_DISJOINT
);
4288 struct isl_set
*isl_set_empty_like(struct isl_set
*model
)
4292 return isl_set_empty(isl_dim_copy(model
->dim
));
4295 struct isl_map
*isl_map_universe(struct isl_dim
*dim
)
4297 struct isl_map
*map
;
4300 map
= isl_map_alloc_dim(isl_dim_copy(dim
), 1, ISL_MAP_DISJOINT
);
4301 map
= isl_map_add_basic_map(map
, isl_basic_map_universe(dim
));
4305 struct isl_set
*isl_set_universe(struct isl_dim
*dim
)
4307 struct isl_set
*set
;
4310 set
= isl_set_alloc_dim(isl_dim_copy(dim
), 1, ISL_MAP_DISJOINT
);
4311 set
= isl_set_add_basic_set(set
, isl_basic_set_universe(dim
));
4315 __isl_give isl_set
*isl_set_universe_like(__isl_keep isl_set
*model
)
4319 return isl_set_universe(isl_dim_copy(model
->dim
));
4322 struct isl_map
*isl_map_dup(struct isl_map
*map
)
4325 struct isl_map
*dup
;
4329 dup
= isl_map_alloc_dim(isl_dim_copy(map
->dim
), map
->n
, map
->flags
);
4330 for (i
= 0; i
< map
->n
; ++i
)
4331 dup
= isl_map_add_basic_map(dup
, isl_basic_map_copy(map
->p
[i
]));
4335 __isl_give isl_map
*isl_map_add_basic_map(__isl_take isl_map
*map
,
4336 __isl_take isl_basic_map
*bmap
)
4340 if (isl_basic_map_plain_is_empty(bmap
)) {
4341 isl_basic_map_free(bmap
);
4344 isl_assert(map
->ctx
, isl_dim_equal(map
->dim
, bmap
->dim
), goto error
);
4345 isl_assert(map
->ctx
, map
->n
< map
->size
, goto error
);
4346 map
->p
[map
->n
] = bmap
;
4348 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4354 isl_basic_map_free(bmap
);
4358 void isl_map_free(struct isl_map
*map
)
4368 isl_ctx_deref(map
->ctx
);
4369 for (i
= 0; i
< map
->n
; ++i
)
4370 isl_basic_map_free(map
->p
[i
]);
4371 isl_dim_free(map
->dim
);
4375 struct isl_map
*isl_map_extend(struct isl_map
*base
,
4376 unsigned nparam
, unsigned n_in
, unsigned n_out
)
4380 base
= isl_map_cow(base
);
4384 base
->dim
= isl_dim_extend(base
->dim
, nparam
, n_in
, n_out
);
4387 for (i
= 0; i
< base
->n
; ++i
) {
4388 base
->p
[i
] = isl_basic_map_extend_dim(base
->p
[i
],
4389 isl_dim_copy(base
->dim
), 0, 0, 0);
4399 struct isl_set
*isl_set_extend(struct isl_set
*base
,
4400 unsigned nparam
, unsigned dim
)
4402 return (struct isl_set
*)isl_map_extend((struct isl_map
*)base
,
4406 static struct isl_basic_map
*isl_basic_map_fix_pos_si(
4407 struct isl_basic_map
*bmap
, unsigned pos
, int value
)
4411 bmap
= isl_basic_map_cow(bmap
);
4412 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
4413 j
= isl_basic_map_alloc_equality(bmap
);
4416 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
4417 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
4418 isl_int_set_si(bmap
->eq
[j
][0], value
);
4419 bmap
= isl_basic_map_simplify(bmap
);
4420 return isl_basic_map_finalize(bmap
);
4422 isl_basic_map_free(bmap
);
4426 static __isl_give isl_basic_map
*isl_basic_map_fix_pos(
4427 __isl_take isl_basic_map
*bmap
, unsigned pos
, isl_int value
)
4431 bmap
= isl_basic_map_cow(bmap
);
4432 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
4433 j
= isl_basic_map_alloc_equality(bmap
);
4436 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
4437 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
4438 isl_int_set(bmap
->eq
[j
][0], value
);
4439 bmap
= isl_basic_map_simplify(bmap
);
4440 return isl_basic_map_finalize(bmap
);
4442 isl_basic_map_free(bmap
);
4446 struct isl_basic_map
*isl_basic_map_fix_si(struct isl_basic_map
*bmap
,
4447 enum isl_dim_type type
, unsigned pos
, int value
)
4451 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
4452 return isl_basic_map_fix_pos_si(bmap
,
4453 isl_basic_map_offset(bmap
, type
) + pos
, value
);
4455 isl_basic_map_free(bmap
);
4459 __isl_give isl_basic_map
*isl_basic_map_fix(__isl_take isl_basic_map
*bmap
,
4460 enum isl_dim_type type
, unsigned pos
, isl_int value
)
4464 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
4465 return isl_basic_map_fix_pos(bmap
,
4466 isl_basic_map_offset(bmap
, type
) + pos
, value
);
4468 isl_basic_map_free(bmap
);
4472 struct isl_basic_set
*isl_basic_set_fix_si(struct isl_basic_set
*bset
,
4473 enum isl_dim_type type
, unsigned pos
, int value
)
4475 return (struct isl_basic_set
*)
4476 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
4480 __isl_give isl_basic_set
*isl_basic_set_fix(__isl_take isl_basic_set
*bset
,
4481 enum isl_dim_type type
, unsigned pos
, isl_int value
)
4483 return (struct isl_basic_set
*)
4484 isl_basic_map_fix((struct isl_basic_map
*)bset
,
4488 struct isl_basic_map
*isl_basic_map_fix_input_si(struct isl_basic_map
*bmap
,
4489 unsigned input
, int value
)
4491 return isl_basic_map_fix_si(bmap
, isl_dim_in
, input
, value
);
4494 struct isl_basic_set
*isl_basic_set_fix_dim_si(struct isl_basic_set
*bset
,
4495 unsigned dim
, int value
)
4497 return (struct isl_basic_set
*)
4498 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
4499 isl_dim_set
, dim
, value
);
4502 struct isl_map
*isl_map_fix_si(struct isl_map
*map
,
4503 enum isl_dim_type type
, unsigned pos
, int value
)
4507 map
= isl_map_cow(map
);
4511 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
4512 for (i
= 0; i
< map
->n
; ++i
) {
4513 map
->p
[i
] = isl_basic_map_fix_si(map
->p
[i
], type
, pos
, value
);
4517 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4524 __isl_give isl_set
*isl_set_fix_si(__isl_take isl_set
*set
,
4525 enum isl_dim_type type
, unsigned pos
, int value
)
4527 return (struct isl_set
*)
4528 isl_map_fix_si((struct isl_map
*)set
, type
, pos
, value
);
4531 __isl_give isl_map
*isl_map_fix(__isl_take isl_map
*map
,
4532 enum isl_dim_type type
, unsigned pos
, isl_int value
)
4536 map
= isl_map_cow(map
);
4540 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
4541 for (i
= 0; i
< map
->n
; ++i
) {
4542 map
->p
[i
] = isl_basic_map_fix(map
->p
[i
], type
, pos
, value
);
4546 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4553 __isl_give isl_set
*isl_set_fix(__isl_take isl_set
*set
,
4554 enum isl_dim_type type
, unsigned pos
, isl_int value
)
4556 return (struct isl_set
*)isl_map_fix((isl_map
*)set
, type
, pos
, value
);
4559 struct isl_map
*isl_map_fix_input_si(struct isl_map
*map
,
4560 unsigned input
, int value
)
4562 return isl_map_fix_si(map
, isl_dim_in
, input
, value
);
4565 struct isl_set
*isl_set_fix_dim_si(struct isl_set
*set
, unsigned dim
, int value
)
4567 return (struct isl_set
*)
4568 isl_map_fix_si((struct isl_map
*)set
, isl_dim_set
, dim
, value
);
4571 __isl_give isl_basic_map
*isl_basic_map_lower_bound_si(
4572 __isl_take isl_basic_map
*bmap
,
4573 enum isl_dim_type type
, unsigned pos
, int value
)
4579 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
4580 pos
+= isl_basic_map_offset(bmap
, type
);
4581 bmap
= isl_basic_map_cow(bmap
);
4582 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
4583 j
= isl_basic_map_alloc_inequality(bmap
);
4586 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
4587 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
4588 isl_int_set_si(bmap
->ineq
[j
][0], -value
);
4589 bmap
= isl_basic_map_simplify(bmap
);
4590 return isl_basic_map_finalize(bmap
);
4592 isl_basic_map_free(bmap
);
4596 struct isl_basic_set
*isl_basic_set_lower_bound_dim(struct isl_basic_set
*bset
,
4597 unsigned dim
, isl_int value
)
4601 bset
= isl_basic_set_cow(bset
);
4602 bset
= isl_basic_set_extend_constraints(bset
, 0, 1);
4603 j
= isl_basic_set_alloc_inequality(bset
);
4606 isl_seq_clr(bset
->ineq
[j
], 1 + isl_basic_set_total_dim(bset
));
4607 isl_int_set_si(bset
->ineq
[j
][1 + isl_basic_set_n_param(bset
) + dim
], 1);
4608 isl_int_neg(bset
->ineq
[j
][0], value
);
4609 bset
= isl_basic_set_simplify(bset
);
4610 return isl_basic_set_finalize(bset
);
4612 isl_basic_set_free(bset
);
4616 __isl_give isl_map
*isl_map_lower_bound_si(__isl_take isl_map
*map
,
4617 enum isl_dim_type type
, unsigned pos
, int value
)
4621 map
= isl_map_cow(map
);
4625 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
4626 for (i
= 0; i
< map
->n
; ++i
) {
4627 map
->p
[i
] = isl_basic_map_lower_bound_si(map
->p
[i
],
4632 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4639 __isl_give isl_set
*isl_set_lower_bound_si(__isl_take isl_set
*set
,
4640 enum isl_dim_type type
, unsigned pos
, int value
)
4642 return (struct isl_set
*)
4643 isl_map_lower_bound_si((struct isl_map
*)set
, type
, pos
, value
);
4646 struct isl_set
*isl_set_lower_bound_dim(struct isl_set
*set
, unsigned dim
,
4651 set
= isl_set_cow(set
);
4655 isl_assert(set
->ctx
, dim
< isl_set_n_dim(set
), goto error
);
4656 for (i
= 0; i
< set
->n
; ++i
) {
4657 set
->p
[i
] = isl_basic_set_lower_bound_dim(set
->p
[i
], dim
, value
);
4667 struct isl_map
*isl_map_reverse(struct isl_map
*map
)
4671 map
= isl_map_cow(map
);
4675 map
->dim
= isl_dim_reverse(map
->dim
);
4678 for (i
= 0; i
< map
->n
; ++i
) {
4679 map
->p
[i
] = isl_basic_map_reverse(map
->p
[i
]);
4683 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4690 static struct isl_map
*isl_basic_map_partial_lexopt(
4691 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
4692 struct isl_set
**empty
, int max
)
4696 if (bmap
->ctx
->opt
->pip
== ISL_PIP_PIP
)
4697 return isl_pip_basic_map_lexopt(bmap
, dom
, empty
, max
);
4699 return isl_tab_basic_map_partial_lexopt(bmap
, dom
, empty
, max
);
4701 isl_basic_map_free(bmap
);
4702 isl_basic_set_free(dom
);
4708 struct isl_map
*isl_basic_map_partial_lexmax(
4709 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
4710 struct isl_set
**empty
)
4712 return isl_basic_map_partial_lexopt(bmap
, dom
, empty
, 1);
4715 struct isl_map
*isl_basic_map_partial_lexmin(
4716 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
4717 struct isl_set
**empty
)
4719 return isl_basic_map_partial_lexopt(bmap
, dom
, empty
, 0);
4722 struct isl_set
*isl_basic_set_partial_lexmin(
4723 struct isl_basic_set
*bset
, struct isl_basic_set
*dom
,
4724 struct isl_set
**empty
)
4726 return (struct isl_set
*)
4727 isl_basic_map_partial_lexmin((struct isl_basic_map
*)bset
,
4731 struct isl_set
*isl_basic_set_partial_lexmax(
4732 struct isl_basic_set
*bset
, struct isl_basic_set
*dom
,
4733 struct isl_set
**empty
)
4735 return (struct isl_set
*)
4736 isl_basic_map_partial_lexmax((struct isl_basic_map
*)bset
,
4740 /* Given a basic map "bmap", compute the lexicographically minimal
4741 * (or maximal) image element for each domain element in dom.
4742 * Set *empty to those elements in dom that do not have an image element.
4744 * We first make sure the basic sets in dom are disjoint and then
4745 * simply collect the results over each of the basic sets separately.
4746 * We could probably improve the efficiency a bit by moving the union
4747 * domain down into the parametric integer programming.
4749 static __isl_give isl_map
*basic_map_partial_lexopt(
4750 __isl_take isl_basic_map
*bmap
, __isl_take isl_set
*dom
,
4751 __isl_give isl_set
**empty
, int max
)
4754 struct isl_map
*res
;
4756 dom
= isl_set_make_disjoint(dom
);
4760 if (isl_set_plain_is_empty(dom
)) {
4761 res
= isl_map_empty_like_basic_map(bmap
);
4762 *empty
= isl_set_empty_like(dom
);
4764 isl_basic_map_free(bmap
);
4768 res
= isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap
),
4769 isl_basic_set_copy(dom
->p
[0]), empty
, max
);
4771 for (i
= 1; i
< dom
->n
; ++i
) {
4772 struct isl_map
*res_i
;
4773 struct isl_set
*empty_i
;
4775 res_i
= isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap
),
4776 isl_basic_set_copy(dom
->p
[i
]), &empty_i
, max
);
4778 res
= isl_map_union_disjoint(res
, res_i
);
4779 *empty
= isl_set_union_disjoint(*empty
, empty_i
);
4783 isl_basic_map_free(bmap
);
4788 isl_basic_map_free(bmap
);
4792 /* Given a map "map", compute the lexicographically minimal
4793 * (or maximal) image element for each domain element in dom.
4794 * Set *empty to those elements in dom that do not have an image element.
4796 * We first compute the lexicographically minimal or maximal element
4797 * in the first basic map. This results in a partial solution "res"
4798 * and a subset "todo" of dom that still need to be handled.
4799 * We then consider each of the remaining maps in "map" and successively
4800 * improve both "res" and "todo".
4802 * Let res^k and todo^k be the results after k steps and let i = k + 1.
4803 * Assume we are computing the lexicographical maximum.
4804 * We first compute the lexicographically maximal element in basic map i.
4805 * This results in a partial solution res_i and a subset todo_i.
4806 * Then we combine these results with those obtain for the first k basic maps
4807 * to obtain a result that is valid for the first k+1 basic maps.
4808 * In particular, the set where there is no solution is the set where
4809 * there is no solution for the first k basic maps and also no solution
4810 * for the ith basic map, i.e.,
4812 * todo^i = todo^k * todo_i
4814 * On dom(res^k) * dom(res_i), we need to pick the larger of the two
4815 * solutions, arbitrarily breaking ties in favor of res^k.
4816 * That is, when res^k(a) >= res_i(a), we pick res^k and
4817 * when res^k(a) < res_i(a), we pick res_i. (Here, ">=" and "<" denote
4818 * the lexicographic order.)
4819 * In practice, we compute
4821 * res^k * (res_i . "<=")
4825 * res_i * (res^k . "<")
4827 * Finally, we consider the symmetric difference of dom(res^k) and dom(res_i),
4828 * where only one of res^k and res_i provides a solution and we simply pick
4835 * Note that we only compute these intersections when dom(res^k) intersects
4836 * dom(res_i). Otherwise, the only effect of these intersections is to
4837 * potentially break up res^k and res_i into smaller pieces.
4838 * We want to avoid such splintering as much as possible.
4839 * In fact, an earlier implementation of this function would look for
4840 * better results in the domain of res^k and for extra results in todo^k,
4841 * but this would always result in a splintering according to todo^k,
4842 * even when the domain of basic map i is disjoint from the domains of
4843 * the previous basic maps.
4845 static __isl_give isl_map
*isl_map_partial_lexopt(
4846 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
4847 __isl_give isl_set
**empty
, int max
)
4850 struct isl_map
*res
;
4851 struct isl_set
*todo
;
4856 if (isl_map_plain_is_empty(map
)) {
4864 res
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[0]),
4865 isl_set_copy(dom
), &todo
, max
);
4867 for (i
= 1; i
< map
->n
; ++i
) {
4871 isl_dim
*dim
= isl_dim_range(isl_map_get_dim(res
));
4873 res_i
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[i
]),
4874 isl_set_copy(dom
), &todo_i
, max
);
4877 lt
= isl_map_lex_lt(isl_dim_copy(dim
));
4878 le
= isl_map_lex_le(dim
);
4880 lt
= isl_map_lex_gt(isl_dim_copy(dim
));
4881 le
= isl_map_lex_ge(dim
);
4883 lt
= isl_map_apply_range(isl_map_copy(res
), lt
);
4884 lt
= isl_map_intersect(lt
, isl_map_copy(res_i
));
4885 le
= isl_map_apply_range(isl_map_copy(res_i
), le
);
4886 le
= isl_map_intersect(le
, isl_map_copy(res
));
4888 if (!isl_map_is_empty(lt
) || !isl_map_is_empty(le
)) {
4889 res
= isl_map_intersect_domain(res
,
4890 isl_set_copy(todo_i
));
4891 res_i
= isl_map_intersect_domain(res_i
,
4892 isl_set_copy(todo
));
4895 res
= isl_map_union_disjoint(res
, res_i
);
4896 res
= isl_map_union_disjoint(res
, lt
);
4897 res
= isl_map_union_disjoint(res
, le
);
4899 todo
= isl_set_intersect(todo
, todo_i
);
4919 __isl_give isl_map
*isl_map_partial_lexmax(
4920 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
4921 __isl_give isl_set
**empty
)
4923 return isl_map_partial_lexopt(map
, dom
, empty
, 1);
4926 __isl_give isl_map
*isl_map_partial_lexmin(
4927 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
4928 __isl_give isl_set
**empty
)
4930 return isl_map_partial_lexopt(map
, dom
, empty
, 0);
4933 __isl_give isl_set
*isl_set_partial_lexmin(
4934 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
4935 __isl_give isl_set
**empty
)
4937 return (struct isl_set
*)
4938 isl_map_partial_lexmin((struct isl_map
*)set
,
4942 __isl_give isl_set
*isl_set_partial_lexmax(
4943 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
4944 __isl_give isl_set
**empty
)
4946 return (struct isl_set
*)
4947 isl_map_partial_lexmax((struct isl_map
*)set
,
4951 __isl_give isl_map
*isl_basic_map_lexopt(__isl_take isl_basic_map
*bmap
, int max
)
4953 struct isl_basic_set
*dom
= NULL
;
4954 struct isl_dim
*dom_dim
;
4958 dom_dim
= isl_dim_domain(isl_dim_copy(bmap
->dim
));
4959 dom
= isl_basic_set_universe(dom_dim
);
4960 return isl_basic_map_partial_lexopt(bmap
, dom
, NULL
, max
);
4962 isl_basic_map_free(bmap
);
4966 __isl_give isl_map
*isl_basic_map_lexmin(__isl_take isl_basic_map
*bmap
)
4968 return isl_basic_map_lexopt(bmap
, 0);
4971 __isl_give isl_map
*isl_basic_map_lexmax(__isl_take isl_basic_map
*bmap
)
4973 return isl_basic_map_lexopt(bmap
, 1);
4976 __isl_give isl_set
*isl_basic_set_lexmin(__isl_take isl_basic_set
*bset
)
4978 return (isl_set
*)isl_basic_map_lexmin((isl_basic_map
*)bset
);
4981 __isl_give isl_set
*isl_basic_set_lexmax(__isl_take isl_basic_set
*bset
)
4983 return (isl_set
*)isl_basic_map_lexmax((isl_basic_map
*)bset
);
4986 __isl_give isl_map
*isl_map_lexopt(__isl_take isl_map
*map
, int max
)
4988 struct isl_set
*dom
= NULL
;
4989 struct isl_dim
*dom_dim
;
4993 dom_dim
= isl_dim_domain(isl_dim_copy(map
->dim
));
4994 dom
= isl_set_universe(dom_dim
);
4995 return isl_map_partial_lexopt(map
, dom
, NULL
, max
);
5001 __isl_give isl_map
*isl_map_lexmin(__isl_take isl_map
*map
)
5003 return isl_map_lexopt(map
, 0);
5006 __isl_give isl_map
*isl_map_lexmax(__isl_take isl_map
*map
)
5008 return isl_map_lexopt(map
, 1);
5011 __isl_give isl_set
*isl_set_lexmin(__isl_take isl_set
*set
)
5013 return (isl_set
*)isl_map_lexmin((isl_map
*)set
);
5016 __isl_give isl_set
*isl_set_lexmax(__isl_take isl_set
*set
)
5018 return (isl_set
*)isl_map_lexmax((isl_map
*)set
);
5021 /* Construct a map that equates the two given dimensions in the given space.
5023 static __isl_give isl_map
*equate(__isl_take isl_dim
*dim
,
5024 enum isl_dim_type src_type
, int src_pos
,
5025 enum isl_dim_type dst_type
, int dst_pos
)
5027 isl_basic_map
*bmap
;
5030 bmap
= isl_basic_map_alloc_dim(dim
, 0, 1, 0);
5031 k
= isl_basic_map_alloc_equality(bmap
);
5034 isl_seq_clr(bmap
->eq
[k
], 1 + isl_basic_map_total_dim(bmap
));
5035 src_pos
+= isl_basic_map_offset(bmap
, src_type
);
5036 dst_pos
+= isl_basic_map_offset(bmap
, dst_type
);
5037 isl_int_set_si(bmap
->eq
[k
][src_pos
], 1);
5038 isl_int_set_si(bmap
->eq
[k
][dst_pos
], -1);
5040 return isl_map_from_basic_map(bmap
);
5042 isl_basic_map_free(bmap
);
5046 /* Extract the first and only affine expression from list
5047 * and then add it to *pwaff with the given dom.
5048 * This domain is known to be disjoint from other domains
5049 * because of the way isl_basic_set_foreach_lexmax works.
5051 static int update_dim_max(__isl_take isl_basic_set
*dom
,
5052 __isl_take isl_aff_list
*list
, void *user
)
5054 isl_ctx
*ctx
= isl_basic_set_get_ctx(dom
);
5056 isl_pw_aff
**pwaff
= user
;
5057 isl_pw_aff
*pwaff_i
;
5059 if (isl_aff_list_n_aff(list
) != 1)
5060 isl_die(ctx
, isl_error_internal
,
5061 "expecting single element list", goto error
);
5063 aff
= isl_aff_list_get_aff(list
, 0);
5064 pwaff_i
= isl_pw_aff_alloc(isl_set_from_basic_set(dom
), aff
);
5066 *pwaff
= isl_pw_aff_add_disjoint(*pwaff
, pwaff_i
);
5068 isl_aff_list_free(list
);
5072 isl_basic_set_free(dom
);
5073 isl_aff_list_free(list
);
5077 /* Given a one-dimensional basic set, compute the maximum of that
5078 * dimension as an isl_pw_aff.
5080 * The isl_pw_aff is constructed by having isl_basic_set_foreach_lexmax
5081 * call update_dim_max on each leaf of the result.
5083 static __isl_give isl_pw_aff
*basic_set_dim_max(__isl_keep isl_basic_set
*bset
)
5085 isl_dim
*dim
= isl_basic_set_get_dim(bset
);
5089 dim
= isl_dim_domain(isl_dim_from_range(dim
));
5090 pwaff
= isl_pw_aff_empty(dim
);
5092 r
= isl_basic_set_foreach_lexmax(bset
, &update_dim_max
, &pwaff
);
5094 return isl_pw_aff_free(pwaff
);
5099 /* Compute the maximum of the given set dimension as a function of the
5100 * parameters, but independently of the other set dimensions.
5102 * We first project the set onto the given dimension and then compute
5103 * the "lexicographic" maximum in each basic set, combining the results
5104 * using isl_pw_aff_max.
5106 __isl_give isl_pw_aff
*isl_set_dim_max(__isl_take isl_set
*set
, int pos
)
5112 map
= isl_map_from_domain(set
);
5113 map
= isl_map_add_dims(map
, isl_dim_out
, 1);
5114 map
= isl_map_intersect(map
,
5115 equate(isl_map_get_dim(map
), isl_dim_in
, pos
,
5117 set
= isl_map_range(map
);
5122 isl_dim
*dim
= isl_set_get_dim(set
);
5123 dim
= isl_dim_domain(isl_dim_from_range(dim
));
5125 return isl_pw_aff_empty(dim
);
5128 pwaff
= basic_set_dim_max(set
->p
[0]);
5129 for (i
= 1; i
< set
->n
; ++i
) {
5130 isl_pw_aff
*pwaff_i
;
5132 pwaff_i
= basic_set_dim_max(set
->p
[i
]);
5133 pwaff
= isl_pw_aff_max(pwaff
, pwaff_i
);
5141 /* Apply a preimage specified by "mat" on the parameters of "bset".
5142 * bset is assumed to have only parameters and divs.
5144 static struct isl_basic_set
*basic_set_parameter_preimage(
5145 struct isl_basic_set
*bset
, struct isl_mat
*mat
)
5152 bset
->dim
= isl_dim_cow(bset
->dim
);
5156 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
5158 isl_assert(bset
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
5160 bset
->dim
->nparam
= 0;
5161 bset
->dim
->n_out
= nparam
;
5162 bset
= isl_basic_set_preimage(bset
, mat
);
5164 bset
->dim
->nparam
= bset
->dim
->n_out
;
5165 bset
->dim
->n_out
= 0;
5170 isl_basic_set_free(bset
);
5174 /* Apply a preimage specified by "mat" on the parameters of "set".
5175 * set is assumed to have only parameters and divs.
5177 static struct isl_set
*set_parameter_preimage(
5178 struct isl_set
*set
, struct isl_mat
*mat
)
5180 struct isl_dim
*dim
= NULL
;
5186 dim
= isl_dim_copy(set
->dim
);
5187 dim
= isl_dim_cow(dim
);
5191 nparam
= isl_set_dim(set
, isl_dim_param
);
5193 isl_assert(set
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
5196 dim
->n_out
= nparam
;
5197 isl_set_reset_dim(set
, dim
);
5198 set
= isl_set_preimage(set
, mat
);
5201 dim
= isl_dim_copy(set
->dim
);
5202 dim
= isl_dim_cow(dim
);
5205 dim
->nparam
= dim
->n_out
;
5207 isl_set_reset_dim(set
, dim
);
5217 /* Intersect the basic set "bset" with the affine space specified by the
5218 * equalities in "eq".
5220 static struct isl_basic_set
*basic_set_append_equalities(
5221 struct isl_basic_set
*bset
, struct isl_mat
*eq
)
5229 bset
= isl_basic_set_extend_dim(bset
, isl_dim_copy(bset
->dim
), 0,
5234 len
= 1 + isl_dim_total(bset
->dim
) + bset
->extra
;
5235 for (i
= 0; i
< eq
->n_row
; ++i
) {
5236 k
= isl_basic_set_alloc_equality(bset
);
5239 isl_seq_cpy(bset
->eq
[k
], eq
->row
[i
], eq
->n_col
);
5240 isl_seq_clr(bset
->eq
[k
] + eq
->n_col
, len
- eq
->n_col
);
5244 bset
= isl_basic_set_gauss(bset
, NULL
);
5245 bset
= isl_basic_set_finalize(bset
);
5250 isl_basic_set_free(bset
);
5254 /* Intersect the set "set" with the affine space specified by the
5255 * equalities in "eq".
5257 static struct isl_set
*set_append_equalities(struct isl_set
*set
,
5265 for (i
= 0; i
< set
->n
; ++i
) {
5266 set
->p
[i
] = basic_set_append_equalities(set
->p
[i
],
5279 /* Project the given basic set onto its parameter domain, possibly introducing
5280 * new, explicit, existential variables in the constraints.
5281 * The input has parameters and (possibly implicit) existential variables.
5282 * The output has the same parameters, but only
5283 * explicit existentially quantified variables.
5285 * The actual projection is performed by pip, but pip doesn't seem
5286 * to like equalities very much, so we first remove the equalities
5287 * among the parameters by performing a variable compression on
5288 * the parameters. Afterward, an inverse transformation is performed
5289 * and the equalities among the parameters are inserted back in.
5291 static struct isl_set
*parameter_compute_divs(struct isl_basic_set
*bset
)
5295 struct isl_mat
*T
, *T2
;
5296 struct isl_set
*set
;
5297 unsigned nparam
, n_div
;
5299 bset
= isl_basic_set_cow(bset
);
5303 if (bset
->n_eq
== 0)
5304 return isl_basic_set_lexmin(bset
);
5306 isl_basic_set_gauss(bset
, NULL
);
5308 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
5309 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
5311 for (i
= 0, j
= n_div
- 1; i
< bset
->n_eq
&& j
>= 0; --j
) {
5312 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ j
]))
5315 if (i
== bset
->n_eq
)
5316 return isl_basic_set_lexmin(bset
);
5318 eq
= isl_mat_sub_alloc6(bset
->ctx
, bset
->eq
, i
, bset
->n_eq
- i
,
5320 eq
= isl_mat_cow(eq
);
5321 T
= isl_mat_variable_compression(isl_mat_copy(eq
), &T2
);
5322 if (T
&& T
->n_col
== 0) {
5326 bset
= isl_basic_set_set_to_empty(bset
);
5327 return isl_set_from_basic_set(bset
);
5329 bset
= basic_set_parameter_preimage(bset
, T
);
5331 set
= isl_basic_set_lexmin(bset
);
5332 set
= set_parameter_preimage(set
, T2
);
5333 set
= set_append_equalities(set
, eq
);
5337 /* Compute an explicit representation for all the existentially
5338 * quantified variables.
5339 * The input and output dimensions are first turned into parameters.
5340 * compute_divs then returns a map with the same parameters and
5341 * no input or output dimensions and the dimension specification
5342 * is reset to that of the input.
5344 static struct isl_map
*compute_divs(struct isl_basic_map
*bmap
)
5346 struct isl_basic_set
*bset
;
5347 struct isl_set
*set
;
5348 struct isl_map
*map
;
5349 struct isl_dim
*dim
, *orig_dim
= NULL
;
5354 bmap
= isl_basic_map_cow(bmap
);
5358 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5359 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5360 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5361 dim
= isl_dim_set_alloc(bmap
->ctx
, nparam
+ n_in
+ n_out
, 0);
5365 orig_dim
= bmap
->dim
;
5367 bset
= (struct isl_basic_set
*)bmap
;
5369 set
= parameter_compute_divs(bset
);
5370 map
= (struct isl_map
*)set
;
5371 map
= isl_map_reset_dim(map
, orig_dim
);
5375 isl_basic_map_free(bmap
);
5379 static int basic_map_divs_known(__isl_keep isl_basic_map
*bmap
)
5387 off
= isl_dim_total(bmap
->dim
);
5388 for (i
= 0; i
< bmap
->n_div
; ++i
) {
5389 if (isl_int_is_zero(bmap
->div
[i
][0]))
5391 isl_assert(bmap
->ctx
, isl_int_is_zero(bmap
->div
[i
][1+1+off
+i
]),
5397 static int map_divs_known(__isl_keep isl_map
*map
)
5404 for (i
= 0; i
< map
->n
; ++i
) {
5405 int known
= basic_map_divs_known(map
->p
[i
]);
5413 /* If bmap contains any unknown divs, then compute explicit
5414 * expressions for them. However, this computation may be
5415 * quite expensive, so first try to remove divs that aren't
5418 struct isl_map
*isl_basic_map_compute_divs(struct isl_basic_map
*bmap
)
5421 struct isl_map
*map
;
5423 known
= basic_map_divs_known(bmap
);
5427 return isl_map_from_basic_map(bmap
);
5429 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
5431 known
= basic_map_divs_known(bmap
);
5435 return isl_map_from_basic_map(bmap
);
5437 map
= compute_divs(bmap
);
5440 isl_basic_map_free(bmap
);
5444 struct isl_map
*isl_map_compute_divs(struct isl_map
*map
)
5448 struct isl_map
*res
;
5455 known
= map_divs_known(map
);
5463 res
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[0]));
5464 for (i
= 1 ; i
< map
->n
; ++i
) {
5466 r2
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[i
]));
5467 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
5468 res
= isl_map_union_disjoint(res
, r2
);
5470 res
= isl_map_union(res
, r2
);
5477 struct isl_set
*isl_basic_set_compute_divs(struct isl_basic_set
*bset
)
5479 return (struct isl_set
*)
5480 isl_basic_map_compute_divs((struct isl_basic_map
*)bset
);
5483 struct isl_set
*isl_set_compute_divs(struct isl_set
*set
)
5485 return (struct isl_set
*)
5486 isl_map_compute_divs((struct isl_map
*)set
);
5489 struct isl_set
*isl_map_domain(struct isl_map
*map
)
5492 struct isl_set
*set
;
5497 map
= isl_map_cow(map
);
5501 set
= (struct isl_set
*)map
;
5502 set
->dim
= isl_dim_domain(set
->dim
);
5505 for (i
= 0; i
< map
->n
; ++i
) {
5506 set
->p
[i
] = isl_basic_map_domain(map
->p
[i
]);
5510 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
5511 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
5518 struct isl_map
*isl_map_union_disjoint(
5519 struct isl_map
*map1
, struct isl_map
*map2
)
5523 struct isl_map
*map
= NULL
;
5537 isl_assert(map1
->ctx
, isl_dim_equal(map1
->dim
, map2
->dim
), goto error
);
5539 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
5540 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
5541 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
5543 map
= isl_map_alloc_dim(isl_dim_copy(map1
->dim
),
5544 map1
->n
+ map2
->n
, flags
);
5547 for (i
= 0; i
< map1
->n
; ++i
) {
5548 map
= isl_map_add_basic_map(map
,
5549 isl_basic_map_copy(map1
->p
[i
]));
5553 for (i
= 0; i
< map2
->n
; ++i
) {
5554 map
= isl_map_add_basic_map(map
,
5555 isl_basic_map_copy(map2
->p
[i
]));
5569 struct isl_map
*isl_map_union(struct isl_map
*map1
, struct isl_map
*map2
)
5571 map1
= isl_map_union_disjoint(map1
, map2
);
5575 ISL_F_CLR(map1
, ISL_MAP_DISJOINT
);
5579 struct isl_set
*isl_set_union_disjoint(
5580 struct isl_set
*set1
, struct isl_set
*set2
)
5582 return (struct isl_set
*)
5583 isl_map_union_disjoint(
5584 (struct isl_map
*)set1
, (struct isl_map
*)set2
);
5587 struct isl_set
*isl_set_union(struct isl_set
*set1
, struct isl_set
*set2
)
5589 return (struct isl_set
*)
5590 isl_map_union((struct isl_map
*)set1
, (struct isl_map
*)set2
);
5593 struct isl_map
*isl_map_intersect_range(
5594 struct isl_map
*map
, struct isl_set
*set
)
5597 struct isl_map
*result
;
5603 if (!isl_dim_match(map
->dim
, isl_dim_param
, set
->dim
, isl_dim_param
))
5604 isl_die(set
->ctx
, isl_error_invalid
,
5605 "parameters don't match", goto error
);
5607 if (isl_dim_size(set
->dim
, isl_dim_set
) != 0 &&
5608 !isl_map_compatible_range(map
, set
))
5609 isl_die(set
->ctx
, isl_error_invalid
,
5610 "incompatible spaces", goto error
);
5612 if (isl_set_plain_is_universe(set
)) {
5617 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
) &&
5618 ISL_F_ISSET(set
, ISL_MAP_DISJOINT
))
5619 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
5621 result
= isl_map_alloc_dim(isl_dim_copy(map
->dim
),
5622 map
->n
* set
->n
, flags
);
5625 for (i
= 0; i
< map
->n
; ++i
)
5626 for (j
= 0; j
< set
->n
; ++j
) {
5627 result
= isl_map_add_basic_map(result
,
5628 isl_basic_map_intersect_range(
5629 isl_basic_map_copy(map
->p
[i
]),
5630 isl_basic_set_copy(set
->p
[j
])));
5643 struct isl_map
*isl_map_intersect_domain(
5644 struct isl_map
*map
, struct isl_set
*set
)
5646 return isl_map_reverse(
5647 isl_map_intersect_range(isl_map_reverse(map
), set
));
5650 struct isl_map
*isl_map_apply_domain(
5651 struct isl_map
*map1
, struct isl_map
*map2
)
5655 map1
= isl_map_reverse(map1
);
5656 map1
= isl_map_apply_range(map1
, map2
);
5657 return isl_map_reverse(map1
);
5664 struct isl_map
*isl_map_apply_range(
5665 struct isl_map
*map1
, struct isl_map
*map2
)
5667 struct isl_dim
*dim_result
;
5668 struct isl_map
*result
;
5674 dim_result
= isl_dim_join(isl_dim_copy(map1
->dim
),
5675 isl_dim_copy(map2
->dim
));
5677 result
= isl_map_alloc_dim(dim_result
, map1
->n
* map2
->n
, 0);
5680 for (i
= 0; i
< map1
->n
; ++i
)
5681 for (j
= 0; j
< map2
->n
; ++j
) {
5682 result
= isl_map_add_basic_map(result
,
5683 isl_basic_map_apply_range(
5684 isl_basic_map_copy(map1
->p
[i
]),
5685 isl_basic_map_copy(map2
->p
[j
])));
5691 if (result
&& result
->n
<= 1)
5692 ISL_F_SET(result
, ISL_MAP_DISJOINT
);
5701 * returns range - domain
5703 struct isl_basic_set
*isl_basic_map_deltas(struct isl_basic_map
*bmap
)
5705 isl_dim
*dims
, *target_dim
;
5706 struct isl_basic_set
*bset
;
5713 isl_assert(bmap
->ctx
, isl_dim_tuple_match(bmap
->dim
, isl_dim_in
,
5714 bmap
->dim
, isl_dim_out
),
5716 target_dim
= isl_dim_domain(isl_basic_map_get_dim(bmap
));
5717 dim
= isl_basic_map_n_in(bmap
);
5718 nparam
= isl_basic_map_n_param(bmap
);
5719 bset
= isl_basic_set_from_basic_map(bmap
);
5720 bset
= isl_basic_set_cow(bset
);
5721 dims
= isl_basic_set_get_dim(bset
);
5722 dims
= isl_dim_add(dims
, isl_dim_set
, dim
);
5723 bset
= isl_basic_set_extend_dim(bset
, dims
, 0, dim
, 0);
5724 bset
= isl_basic_set_swap_vars(bset
, 2*dim
);
5725 for (i
= 0; i
< dim
; ++i
) {
5726 int j
= isl_basic_map_alloc_equality(
5727 (struct isl_basic_map
*)bset
);
5730 isl_seq_clr(bset
->eq
[j
], 1 + isl_basic_set_total_dim(bset
));
5731 isl_int_set_si(bset
->eq
[j
][1+nparam
+i
], 1);
5732 isl_int_set_si(bset
->eq
[j
][1+nparam
+dim
+i
], 1);
5733 isl_int_set_si(bset
->eq
[j
][1+nparam
+2*dim
+i
], -1);
5735 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, dim
, 2*dim
);
5736 bset
= isl_basic_set_reset_dim(bset
, target_dim
);
5739 isl_basic_map_free(bmap
);
5744 * returns range - domain
5746 struct isl_set
*isl_map_deltas(struct isl_map
*map
)
5750 struct isl_set
*result
;
5755 isl_assert(map
->ctx
, isl_dim_tuple_match(map
->dim
, isl_dim_in
,
5756 map
->dim
, isl_dim_out
),
5758 dim
= isl_map_get_dim(map
);
5759 dim
= isl_dim_domain(dim
);
5760 result
= isl_set_alloc_dim(dim
, map
->n
, 0);
5763 for (i
= 0; i
< map
->n
; ++i
)
5764 result
= isl_set_add_basic_set(result
,
5765 isl_basic_map_deltas(isl_basic_map_copy(map
->p
[i
])));
5774 * returns [domain -> range] -> range - domain
5776 __isl_give isl_basic_map
*isl_basic_map_deltas_map(
5777 __isl_take isl_basic_map
*bmap
)
5781 isl_basic_map
*domain
;
5785 if (!isl_dim_tuple_match(bmap
->dim
, isl_dim_in
, bmap
->dim
, isl_dim_out
))
5786 isl_die(bmap
->ctx
, isl_error_invalid
,
5787 "domain and range don't match", goto error
);
5789 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5790 n
= isl_basic_map_dim(bmap
, isl_dim_in
);
5792 dim
= isl_dim_from_range(isl_dim_domain(isl_basic_map_get_dim(bmap
)));
5793 domain
= isl_basic_map_universe(dim
);
5795 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
5796 bmap
= isl_basic_map_apply_range(bmap
, domain
);
5797 bmap
= isl_basic_map_extend_constraints(bmap
, n
, 0);
5799 total
= isl_basic_map_total_dim(bmap
);
5801 for (i
= 0; i
< n
; ++i
) {
5802 k
= isl_basic_map_alloc_equality(bmap
);
5805 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
5806 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], 1);
5807 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ i
], -1);
5808 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ n
+ i
], 1);
5811 bmap
= isl_basic_map_gauss(bmap
, NULL
);
5812 return isl_basic_map_finalize(bmap
);
5814 isl_basic_map_free(bmap
);
5819 * returns [domain -> range] -> range - domain
5821 __isl_give isl_map
*isl_map_deltas_map(__isl_take isl_map
*map
)
5824 isl_dim
*domain_dim
;
5829 if (!isl_dim_tuple_match(map
->dim
, isl_dim_in
, map
->dim
, isl_dim_out
))
5830 isl_die(map
->ctx
, isl_error_invalid
,
5831 "domain and range don't match", goto error
);
5833 map
= isl_map_cow(map
);
5837 domain_dim
= isl_dim_from_range(isl_dim_domain(isl_map_get_dim(map
)));
5838 map
->dim
= isl_dim_from_domain(isl_dim_wrap(map
->dim
));
5839 map
->dim
= isl_dim_join(map
->dim
, domain_dim
);
5842 for (i
= 0; i
< map
->n
; ++i
) {
5843 map
->p
[i
] = isl_basic_map_deltas_map(map
->p
[i
]);
5847 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5854 static struct isl_basic_map
*basic_map_identity(struct isl_dim
*dims
)
5856 struct isl_basic_map
*bmap
;
5864 nparam
= dims
->nparam
;
5866 bmap
= isl_basic_map_alloc_dim(dims
, 0, dim
, 0);
5870 for (i
= 0; i
< dim
; ++i
) {
5871 int j
= isl_basic_map_alloc_equality(bmap
);
5874 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
5875 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
5876 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], -1);
5878 return isl_basic_map_finalize(bmap
);
5880 isl_basic_map_free(bmap
);
5884 __isl_give isl_basic_map
*isl_basic_map_identity(__isl_take isl_dim
*dim
)
5888 if (dim
->n_in
!= dim
->n_out
)
5889 isl_die(dim
->ctx
, isl_error_invalid
,
5890 "number of input and output dimensions needs to be "
5891 "the same", goto error
);
5892 return basic_map_identity(dim
);
5898 struct isl_basic_map
*isl_basic_map_identity_like(struct isl_basic_map
*model
)
5900 if (!model
|| !model
->dim
)
5902 return isl_basic_map_identity(isl_dim_copy(model
->dim
));
5905 __isl_give isl_map
*isl_map_identity(__isl_take isl_dim
*dim
)
5907 return isl_map_from_basic_map(isl_basic_map_identity(dim
));
5910 struct isl_map
*isl_map_identity_like(struct isl_map
*model
)
5912 if (!model
|| !model
->dim
)
5914 return isl_map_identity(isl_dim_copy(model
->dim
));
5917 struct isl_map
*isl_map_identity_like_basic_map(struct isl_basic_map
*model
)
5919 if (!model
|| !model
->dim
)
5921 return isl_map_identity(isl_dim_copy(model
->dim
));
5924 __isl_give isl_map
*isl_set_identity(__isl_take isl_set
*set
)
5926 isl_dim
*dim
= isl_set_get_dim(set
);
5928 id
= isl_map_identity(isl_dim_map_from_set(dim
));
5929 return isl_map_intersect_range(id
, set
);
5932 /* Construct a basic set with all set dimensions having only non-negative
5935 struct isl_basic_set
*isl_basic_set_positive_orthant(struct isl_dim
*dims
)
5940 struct isl_basic_set
*bset
;
5944 nparam
= dims
->nparam
;
5946 bset
= isl_basic_set_alloc_dim(dims
, 0, 0, dim
);
5949 for (i
= 0; i
< dim
; ++i
) {
5950 int k
= isl_basic_set_alloc_inequality(bset
);
5953 isl_seq_clr(bset
->ineq
[k
], 1 + isl_basic_set_total_dim(bset
));
5954 isl_int_set_si(bset
->ineq
[k
][1 + nparam
+ i
], 1);
5958 isl_basic_set_free(bset
);
5962 /* Construct the half-space x_pos >= 0.
5964 static __isl_give isl_basic_set
*nonneg_halfspace(__isl_take isl_dim
*dim
,
5968 isl_basic_set
*nonneg
;
5970 nonneg
= isl_basic_set_alloc_dim(dim
, 0, 0, 1);
5971 k
= isl_basic_set_alloc_inequality(nonneg
);
5974 isl_seq_clr(nonneg
->ineq
[k
], 1 + isl_basic_set_total_dim(nonneg
));
5975 isl_int_set_si(nonneg
->ineq
[k
][pos
], 1);
5977 return isl_basic_set_finalize(nonneg
);
5979 isl_basic_set_free(nonneg
);
5983 /* Construct the half-space x_pos <= -1.
5985 static __isl_give isl_basic_set
*neg_halfspace(__isl_take isl_dim
*dim
, int pos
)
5990 neg
= isl_basic_set_alloc_dim(dim
, 0, 0, 1);
5991 k
= isl_basic_set_alloc_inequality(neg
);
5994 isl_seq_clr(neg
->ineq
[k
], 1 + isl_basic_set_total_dim(neg
));
5995 isl_int_set_si(neg
->ineq
[k
][0], -1);
5996 isl_int_set_si(neg
->ineq
[k
][pos
], -1);
5998 return isl_basic_set_finalize(neg
);
6000 isl_basic_set_free(neg
);
6004 __isl_give isl_set
*isl_set_split_dims(__isl_take isl_set
*set
,
6005 enum isl_dim_type type
, unsigned first
, unsigned n
)
6008 isl_basic_set
*nonneg
;
6016 isl_assert(set
->ctx
, first
+ n
<= isl_set_dim(set
, type
), goto error
);
6018 for (i
= 0; i
< n
; ++i
) {
6019 nonneg
= nonneg_halfspace(isl_set_get_dim(set
),
6020 pos(set
->dim
, type
) + first
+ i
);
6021 neg
= neg_halfspace(isl_set_get_dim(set
),
6022 pos(set
->dim
, type
) + first
+ i
);
6024 set
= isl_set_intersect(set
, isl_basic_set_union(nonneg
, neg
));
6033 static int foreach_orthant(__isl_take isl_set
*set
, int *signs
, int first
,
6034 int len
, int (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
6041 if (isl_set_plain_is_empty(set
)) {
6046 return fn(set
, signs
, user
);
6049 half
= isl_set_from_basic_set(nonneg_halfspace(isl_set_get_dim(set
),
6051 half
= isl_set_intersect(half
, isl_set_copy(set
));
6052 if (foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
) < 0)
6056 half
= isl_set_from_basic_set(neg_halfspace(isl_set_get_dim(set
),
6058 half
= isl_set_intersect(half
, set
);
6059 return foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
);
6065 /* Call "fn" on the intersections of "set" with each of the orthants
6066 * (except for obviously empty intersections). The orthant is identified
6067 * by the signs array, with each entry having value 1 or -1 according
6068 * to the sign of the corresponding variable.
6070 int isl_set_foreach_orthant(__isl_keep isl_set
*set
,
6071 int (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
6081 if (isl_set_plain_is_empty(set
))
6084 nparam
= isl_set_dim(set
, isl_dim_param
);
6085 nvar
= isl_set_dim(set
, isl_dim_set
);
6087 signs
= isl_alloc_array(set
->ctx
, int, nparam
+ nvar
);
6089 r
= foreach_orthant(isl_set_copy(set
), signs
, 0, nparam
+ nvar
,
6097 int isl_set_is_equal(struct isl_set
*set1
, struct isl_set
*set2
)
6099 return isl_map_is_equal((struct isl_map
*)set1
, (struct isl_map
*)set2
);
6102 int isl_basic_map_is_subset(
6103 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
6106 struct isl_map
*map1
;
6107 struct isl_map
*map2
;
6109 if (!bmap1
|| !bmap2
)
6112 map1
= isl_map_from_basic_map(isl_basic_map_copy(bmap1
));
6113 map2
= isl_map_from_basic_map(isl_basic_map_copy(bmap2
));
6115 is_subset
= isl_map_is_subset(map1
, map2
);
6123 int isl_basic_map_is_equal(
6124 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
6128 if (!bmap1
|| !bmap2
)
6130 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
6133 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
6137 int isl_basic_set_is_equal(
6138 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
6140 return isl_basic_map_is_equal(
6141 (struct isl_basic_map
*)bset1
, (struct isl_basic_map
*)bset2
);
6144 int isl_map_is_empty(struct isl_map
*map
)
6151 for (i
= 0; i
< map
->n
; ++i
) {
6152 is_empty
= isl_basic_map_is_empty(map
->p
[i
]);
6161 int isl_map_plain_is_empty(__isl_keep isl_map
*map
)
6163 return map
? map
->n
== 0 : -1;
6166 int isl_map_fast_is_empty(__isl_keep isl_map
*map
)
6168 return isl_map_plain_is_empty(map
);
6171 int isl_set_plain_is_empty(struct isl_set
*set
)
6173 return set
? set
->n
== 0 : -1;
6176 int isl_set_fast_is_empty(__isl_keep isl_set
*set
)
6178 return isl_set_plain_is_empty(set
);
6181 int isl_set_is_empty(struct isl_set
*set
)
6183 return isl_map_is_empty((struct isl_map
*)set
);
6186 int isl_map_has_equal_dim(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
6191 return isl_dim_equal(map1
->dim
, map2
->dim
);
6194 int isl_set_has_equal_dim(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
6199 return isl_dim_equal(set1
->dim
, set2
->dim
);
6202 int isl_map_is_equal(struct isl_map
*map1
, struct isl_map
*map2
)
6208 is_subset
= isl_map_is_subset(map1
, map2
);
6211 is_subset
= isl_map_is_subset(map2
, map1
);
6215 int isl_basic_map_is_strict_subset(
6216 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
6220 if (!bmap1
|| !bmap2
)
6222 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
6225 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
6226 if (is_subset
== -1)
6231 int isl_map_is_strict_subset(struct isl_map
*map1
, struct isl_map
*map2
)
6237 is_subset
= isl_map_is_subset(map1
, map2
);
6240 is_subset
= isl_map_is_subset(map2
, map1
);
6241 if (is_subset
== -1)
6246 int isl_set_is_strict_subset(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
6248 return isl_map_is_strict_subset((isl_map
*)set1
, (isl_map
*)set2
);
6251 int isl_basic_map_is_universe(struct isl_basic_map
*bmap
)
6255 return bmap
->n_eq
== 0 && bmap
->n_ineq
== 0;
6258 int isl_basic_set_is_universe(struct isl_basic_set
*bset
)
6262 return bset
->n_eq
== 0 && bset
->n_ineq
== 0;
6265 int isl_map_plain_is_universe(__isl_keep isl_map
*map
)
6272 for (i
= 0; i
< map
->n
; ++i
) {
6273 int r
= isl_basic_map_is_universe(map
->p
[i
]);
6281 int isl_set_plain_is_universe(__isl_keep isl_set
*set
)
6283 return isl_map_plain_is_universe((isl_map
*) set
);
6286 int isl_set_fast_is_universe(__isl_keep isl_set
*set
)
6288 return isl_set_plain_is_universe(set
);
6291 int isl_basic_map_is_empty(struct isl_basic_map
*bmap
)
6293 struct isl_basic_set
*bset
= NULL
;
6294 struct isl_vec
*sample
= NULL
;
6301 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
6304 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
6305 struct isl_basic_map
*copy
= isl_basic_map_copy(bmap
);
6306 copy
= isl_basic_map_remove_redundancies(copy
);
6307 empty
= ISL_F_ISSET(copy
, ISL_BASIC_MAP_EMPTY
);
6308 isl_basic_map_free(copy
);
6312 total
= 1 + isl_basic_map_total_dim(bmap
);
6313 if (bmap
->sample
&& bmap
->sample
->size
== total
) {
6314 int contains
= isl_basic_map_contains(bmap
, bmap
->sample
);
6320 isl_vec_free(bmap
->sample
);
6321 bmap
->sample
= NULL
;
6322 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
6325 sample
= isl_basic_set_sample_vec(bset
);
6328 empty
= sample
->size
== 0;
6329 isl_vec_free(bmap
->sample
);
6330 bmap
->sample
= sample
;
6332 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
6337 int isl_basic_map_plain_is_empty(__isl_keep isl_basic_map
*bmap
)
6341 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
);
6344 int isl_basic_map_fast_is_empty(__isl_keep isl_basic_map
*bmap
)
6346 return isl_basic_map_plain_is_empty(bmap
);
6349 int isl_basic_set_plain_is_empty(__isl_keep isl_basic_set
*bset
)
6353 return ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
);
6356 int isl_basic_set_fast_is_empty(__isl_keep isl_basic_set
*bset
)
6358 return isl_basic_set_plain_is_empty(bset
);
6361 int isl_basic_set_is_empty(struct isl_basic_set
*bset
)
6363 return isl_basic_map_is_empty((struct isl_basic_map
*)bset
);
6366 struct isl_map
*isl_basic_map_union(
6367 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
6369 struct isl_map
*map
;
6370 if (!bmap1
|| !bmap2
)
6373 isl_assert(bmap1
->ctx
, isl_dim_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
6375 map
= isl_map_alloc_dim(isl_dim_copy(bmap1
->dim
), 2, 0);
6378 map
= isl_map_add_basic_map(map
, bmap1
);
6379 map
= isl_map_add_basic_map(map
, bmap2
);
6382 isl_basic_map_free(bmap1
);
6383 isl_basic_map_free(bmap2
);
6387 struct isl_set
*isl_basic_set_union(
6388 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
6390 return (struct isl_set
*)isl_basic_map_union(
6391 (struct isl_basic_map
*)bset1
,
6392 (struct isl_basic_map
*)bset2
);
6395 /* Order divs such that any div only depends on previous divs */
6396 struct isl_basic_map
*isl_basic_map_order_divs(struct isl_basic_map
*bmap
)
6404 off
= isl_dim_total(bmap
->dim
);
6406 for (i
= 0; i
< bmap
->n_div
; ++i
) {
6408 if (isl_int_is_zero(bmap
->div
[i
][0]))
6410 pos
= isl_seq_first_non_zero(bmap
->div
[i
]+1+1+off
+i
,
6414 isl_basic_map_swap_div(bmap
, i
, i
+ pos
);
6420 struct isl_basic_set
*isl_basic_set_order_divs(struct isl_basic_set
*bset
)
6422 return (struct isl_basic_set
*)
6423 isl_basic_map_order_divs((struct isl_basic_map
*)bset
);
6426 __isl_give isl_map
*isl_map_order_divs(__isl_take isl_map
*map
)
6433 for (i
= 0; i
< map
->n
; ++i
) {
6434 map
->p
[i
] = isl_basic_map_order_divs(map
->p
[i
]);
6445 /* Apply the expansion computed by isl_merge_divs.
6446 * The expansion itself is given by "exp" while the resulting
6447 * list of divs is given by "div".
6449 __isl_give isl_basic_set
*isl_basic_set_expand_divs(
6450 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*div
, int *exp
)
6455 bset
= isl_basic_set_cow(bset
);
6459 if (div
->n_row
< bset
->n_div
)
6460 isl_die(isl_mat_get_ctx(div
), isl_error_invalid
,
6461 "not an expansion", goto error
);
6463 bset
= isl_basic_map_extend_dim(bset
, isl_dim_copy(bset
->dim
),
6464 div
->n_row
- bset
->n_div
, 0,
6465 2 * (div
->n_row
- bset
->n_div
));
6467 n_div
= bset
->n_div
;
6468 for (i
= n_div
; i
< div
->n_row
; ++i
)
6469 if (isl_basic_set_alloc_div(bset
) < 0)
6473 for (i
= div
->n_row
- 1; i
>= 0; --i
) {
6474 if (j
>= 0 && exp
[j
] == i
) {
6476 isl_basic_map_swap_div(bset
, i
, j
);
6479 isl_seq_cpy(bset
->div
[i
], div
->row
[i
], div
->n_col
);
6480 if (isl_basic_map_add_div_constraints(bset
, i
) < 0)
6488 isl_basic_set_free(bset
);
6493 /* Look for a div in dst that corresponds to the div "div" in src.
6494 * The divs before "div" in src and dst are assumed to be the same.
6496 * Returns -1 if no corresponding div was found and the position
6497 * of the corresponding div in dst otherwise.
6499 static int find_div(struct isl_basic_map
*dst
,
6500 struct isl_basic_map
*src
, unsigned div
)
6504 unsigned total
= isl_dim_total(src
->dim
);
6506 isl_assert(dst
->ctx
, div
<= dst
->n_div
, return -1);
6507 for (i
= div
; i
< dst
->n_div
; ++i
)
6508 if (isl_seq_eq(dst
->div
[i
], src
->div
[div
], 1+1+total
+div
) &&
6509 isl_seq_first_non_zero(dst
->div
[i
]+1+1+total
+div
,
6510 dst
->n_div
- div
) == -1)
6515 struct isl_basic_map
*isl_basic_map_align_divs(
6516 struct isl_basic_map
*dst
, struct isl_basic_map
*src
)
6519 unsigned total
= isl_dim_total(src
->dim
);
6524 if (src
->n_div
== 0)
6527 for (i
= 0; i
< src
->n_div
; ++i
)
6528 isl_assert(src
->ctx
, !isl_int_is_zero(src
->div
[i
][0]), goto error
);
6530 src
= isl_basic_map_order_divs(src
);
6531 dst
= isl_basic_map_cow(dst
);
6532 dst
= isl_basic_map_extend_dim(dst
, isl_dim_copy(dst
->dim
),
6533 src
->n_div
, 0, 2 * src
->n_div
);
6536 for (i
= 0; i
< src
->n_div
; ++i
) {
6537 int j
= find_div(dst
, src
, i
);
6539 j
= isl_basic_map_alloc_div(dst
);
6542 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
+i
);
6543 isl_seq_clr(dst
->div
[j
]+1+1+total
+i
, dst
->n_div
- i
);
6544 if (isl_basic_map_add_div_constraints(dst
, j
) < 0)
6548 isl_basic_map_swap_div(dst
, i
, j
);
6552 isl_basic_map_free(dst
);
6556 struct isl_basic_set
*isl_basic_set_align_divs(
6557 struct isl_basic_set
*dst
, struct isl_basic_set
*src
)
6559 return (struct isl_basic_set
*)isl_basic_map_align_divs(
6560 (struct isl_basic_map
*)dst
, (struct isl_basic_map
*)src
);
6563 struct isl_map
*isl_map_align_divs(struct isl_map
*map
)
6571 map
= isl_map_compute_divs(map
);
6572 map
= isl_map_cow(map
);
6576 for (i
= 1; i
< map
->n
; ++i
)
6577 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], map
->p
[i
]);
6578 for (i
= 1; i
< map
->n
; ++i
)
6579 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], map
->p
[0]);
6581 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6585 struct isl_set
*isl_set_align_divs(struct isl_set
*set
)
6587 return (struct isl_set
*)isl_map_align_divs((struct isl_map
*)set
);
6590 struct isl_set
*isl_set_apply(struct isl_set
*set
, struct isl_map
*map
)
6594 isl_assert(set
->ctx
, isl_map_compatible_domain(map
, set
), goto error
);
6595 map
= isl_map_intersect_domain(map
, set
);
6596 set
= isl_map_range(map
);
6604 /* There is no need to cow as removing empty parts doesn't change
6605 * the meaning of the set.
6607 struct isl_map
*isl_map_remove_empty_parts(struct isl_map
*map
)
6614 for (i
= map
->n
-1; i
>= 0; --i
) {
6615 if (!ISL_F_ISSET(map
->p
[i
], ISL_BASIC_MAP_EMPTY
))
6617 isl_basic_map_free(map
->p
[i
]);
6618 if (i
!= map
->n
-1) {
6619 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6620 map
->p
[i
] = map
->p
[map
->n
-1];
6628 struct isl_set
*isl_set_remove_empty_parts(struct isl_set
*set
)
6630 return (struct isl_set
*)
6631 isl_map_remove_empty_parts((struct isl_map
*)set
);
6634 struct isl_basic_map
*isl_map_copy_basic_map(struct isl_map
*map
)
6636 struct isl_basic_map
*bmap
;
6637 if (!map
|| map
->n
== 0)
6639 bmap
= map
->p
[map
->n
-1];
6640 isl_assert(map
->ctx
, ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
), return NULL
);
6641 return isl_basic_map_copy(bmap
);
6644 struct isl_basic_set
*isl_set_copy_basic_set(struct isl_set
*set
)
6646 return (struct isl_basic_set
*)
6647 isl_map_copy_basic_map((struct isl_map
*)set
);
6650 __isl_give isl_map
*isl_map_drop_basic_map(__isl_take isl_map
*map
,
6651 __isl_keep isl_basic_map
*bmap
)
6657 for (i
= map
->n
-1; i
>= 0; --i
) {
6658 if (map
->p
[i
] != bmap
)
6660 map
= isl_map_cow(map
);
6663 isl_basic_map_free(map
->p
[i
]);
6664 if (i
!= map
->n
-1) {
6665 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
6666 map
->p
[i
] = map
->p
[map
->n
-1];
6677 struct isl_set
*isl_set_drop_basic_set(struct isl_set
*set
,
6678 struct isl_basic_set
*bset
)
6680 return (struct isl_set
*)isl_map_drop_basic_map((struct isl_map
*)set
,
6681 (struct isl_basic_map
*)bset
);
6684 /* Given two basic sets bset1 and bset2, compute the maximal difference
6685 * between the values of dimension pos in bset1 and those in bset2
6686 * for any common value of the parameters and dimensions preceding pos.
6688 static enum isl_lp_result
basic_set_maximal_difference_at(
6689 __isl_keep isl_basic_set
*bset1
, __isl_keep isl_basic_set
*bset2
,
6690 int pos
, isl_int
*opt
)
6692 struct isl_dim
*dims
;
6693 struct isl_basic_map
*bmap1
= NULL
;
6694 struct isl_basic_map
*bmap2
= NULL
;
6695 struct isl_ctx
*ctx
;
6696 struct isl_vec
*obj
;
6699 unsigned dim1
, dim2
;
6700 enum isl_lp_result res
;
6702 if (!bset1
|| !bset2
)
6703 return isl_lp_error
;
6705 nparam
= isl_basic_set_n_param(bset1
);
6706 dim1
= isl_basic_set_n_dim(bset1
);
6707 dim2
= isl_basic_set_n_dim(bset2
);
6708 dims
= isl_dim_alloc(bset1
->ctx
, nparam
, pos
, dim1
- pos
);
6709 bmap1
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset1
), dims
);
6710 dims
= isl_dim_alloc(bset2
->ctx
, nparam
, pos
, dim2
- pos
);
6711 bmap2
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset2
), dims
);
6712 if (!bmap1
|| !bmap2
)
6714 bmap1
= isl_basic_map_cow(bmap1
);
6715 bmap1
= isl_basic_map_extend(bmap1
, nparam
,
6716 pos
, (dim1
- pos
) + (dim2
- pos
),
6717 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
6718 bmap1
= add_constraints(bmap1
, bmap2
, 0, dim1
- pos
);
6721 total
= isl_basic_map_total_dim(bmap1
);
6723 obj
= isl_vec_alloc(ctx
, 1 + total
);
6724 isl_seq_clr(obj
->block
.data
, 1 + total
);
6725 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
], 1);
6726 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
+(dim1
-pos
)], -1);
6729 res
= isl_basic_map_solve_lp(bmap1
, 1, obj
->block
.data
, ctx
->one
,
6731 isl_basic_map_free(bmap1
);
6735 isl_basic_map_free(bmap1
);
6736 isl_basic_map_free(bmap2
);
6737 return isl_lp_error
;
6740 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
6741 * for any common value of the parameters and dimensions preceding pos
6742 * in both basic sets, the values of dimension pos in bset1 are
6743 * smaller or larger than those in bset2.
6746 * 1 if bset1 follows bset2
6747 * -1 if bset1 precedes bset2
6748 * 0 if bset1 and bset2 are incomparable
6749 * -2 if some error occurred.
6751 int isl_basic_set_compare_at(struct isl_basic_set
*bset1
,
6752 struct isl_basic_set
*bset2
, int pos
)
6755 enum isl_lp_result res
;
6760 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
6762 if (res
== isl_lp_empty
)
6764 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
6765 res
== isl_lp_unbounded
)
6767 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
6776 /* Given two basic sets bset1 and bset2, check whether
6777 * for any common value of the parameters and dimensions preceding pos
6778 * there is a value of dimension pos in bset1 that is larger
6779 * than a value of the same dimension in bset2.
6782 * 1 if there exists such a pair
6783 * 0 if there is no such pair, but there is a pair of equal values
6785 * -2 if some error occurred.
6787 int isl_basic_set_follows_at(__isl_keep isl_basic_set
*bset1
,
6788 __isl_keep isl_basic_set
*bset2
, int pos
)
6791 enum isl_lp_result res
;
6796 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
6798 if (res
== isl_lp_empty
)
6800 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
6801 res
== isl_lp_unbounded
)
6803 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
6805 else if (res
== isl_lp_ok
)
6814 /* Given two sets set1 and set2, check whether
6815 * for any common value of the parameters and dimensions preceding pos
6816 * there is a value of dimension pos in set1 that is larger
6817 * than a value of the same dimension in set2.
6820 * 1 if there exists such a pair
6821 * 0 if there is no such pair, but there is a pair of equal values
6823 * -2 if some error occurred.
6825 int isl_set_follows_at(__isl_keep isl_set
*set1
,
6826 __isl_keep isl_set
*set2
, int pos
)
6834 for (i
= 0; i
< set1
->n
; ++i
)
6835 for (j
= 0; j
< set2
->n
; ++j
) {
6837 f
= isl_basic_set_follows_at(set1
->p
[i
], set2
->p
[j
], pos
);
6838 if (f
== 1 || f
== -2)
6847 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map
*bmap
,
6848 unsigned pos
, isl_int
*val
)
6856 total
= isl_basic_map_total_dim(bmap
);
6857 for (i
= 0, d
= total
-1; i
< bmap
->n_eq
&& d
+1 > pos
; ++i
) {
6858 for (; d
+1 > pos
; --d
)
6859 if (!isl_int_is_zero(bmap
->eq
[i
][1+d
]))
6863 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1, d
) != -1)
6865 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1+d
+1, total
-d
-1) != -1)
6867 if (!isl_int_is_one(bmap
->eq
[i
][1+d
]))
6870 isl_int_neg(*val
, bmap
->eq
[i
][0]);
6876 static int isl_map_plain_has_fixed_var(__isl_keep isl_map
*map
,
6877 unsigned pos
, isl_int
*val
)
6889 return isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, val
);
6892 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, &v
);
6893 for (i
= 1; fixed
== 1 && i
< map
->n
; ++i
) {
6894 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[i
], pos
, &tmp
);
6895 if (fixed
== 1 && isl_int_ne(tmp
, v
))
6899 isl_int_set(*val
, v
);
6905 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set
*bset
,
6906 unsigned pos
, isl_int
*val
)
6908 return isl_basic_map_plain_has_fixed_var((struct isl_basic_map
*)bset
,
6912 static int isl_set_plain_has_fixed_var(__isl_keep isl_set
*set
, unsigned pos
,
6915 return isl_map_plain_has_fixed_var((struct isl_map
*)set
, pos
, val
);
6918 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map
*bmap
,
6919 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
6921 if (pos
>= isl_basic_map_dim(bmap
, type
))
6923 return isl_basic_map_plain_has_fixed_var(bmap
,
6924 isl_basic_map_offset(bmap
, type
) - 1 + pos
, val
);
6927 int isl_map_plain_is_fixed(__isl_keep isl_map
*map
,
6928 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
6930 if (pos
>= isl_map_dim(map
, type
))
6932 return isl_map_plain_has_fixed_var(map
,
6933 map_offset(map
, type
) - 1 + pos
, val
);
6936 int isl_map_fast_is_fixed(__isl_keep isl_map
*map
,
6937 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
6939 return isl_map_plain_is_fixed(map
, type
, pos
, val
);
6942 /* Check if dimension dim has fixed value and if so and if val is not NULL,
6943 * then return this fixed value in *val.
6945 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set
*bset
,
6946 unsigned dim
, isl_int
*val
)
6948 return isl_basic_set_plain_has_fixed_var(bset
,
6949 isl_basic_set_n_param(bset
) + dim
, val
);
6952 /* Check if dimension dim has fixed value and if so and if val is not NULL,
6953 * then return this fixed value in *val.
6955 int isl_set_plain_dim_is_fixed(__isl_keep isl_set
*set
,
6956 unsigned dim
, isl_int
*val
)
6958 return isl_set_plain_has_fixed_var(set
, isl_set_n_param(set
) + dim
, val
);
6961 int isl_set_fast_dim_is_fixed(__isl_keep isl_set
*set
,
6962 unsigned dim
, isl_int
*val
)
6964 return isl_set_plain_dim_is_fixed(set
, dim
, val
);
6967 /* Check if input variable in has fixed value and if so and if val is not NULL,
6968 * then return this fixed value in *val.
6970 int isl_map_plain_input_is_fixed(__isl_keep isl_map
*map
,
6971 unsigned in
, isl_int
*val
)
6973 return isl_map_plain_has_fixed_var(map
, isl_map_n_param(map
) + in
, val
);
6976 /* Check if dimension dim has an (obvious) fixed lower bound and if so
6977 * and if val is not NULL, then return this lower bound in *val.
6979 int isl_basic_set_plain_dim_has_fixed_lower_bound(
6980 __isl_keep isl_basic_set
*bset
, unsigned dim
, isl_int
*val
)
6982 int i
, i_eq
= -1, i_ineq
= -1;
6989 total
= isl_basic_set_total_dim(bset
);
6990 nparam
= isl_basic_set_n_param(bset
);
6991 for (i
= 0; i
< bset
->n_eq
; ++i
) {
6992 if (isl_int_is_zero(bset
->eq
[i
][1+nparam
+dim
]))
6998 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
6999 if (!isl_int_is_pos(bset
->ineq
[i
][1+nparam
+dim
]))
7001 if (i_eq
!= -1 || i_ineq
!= -1)
7005 if (i_eq
== -1 && i_ineq
== -1)
7007 c
= i_eq
!= -1 ? bset
->eq
[i_eq
] : bset
->ineq
[i_ineq
];
7008 /* The coefficient should always be one due to normalization. */
7009 if (!isl_int_is_one(c
[1+nparam
+dim
]))
7011 if (isl_seq_first_non_zero(c
+1, nparam
+dim
) != -1)
7013 if (isl_seq_first_non_zero(c
+1+nparam
+dim
+1,
7014 total
- nparam
- dim
- 1) != -1)
7017 isl_int_neg(*val
, c
[0]);
7021 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set
*set
,
7022 unsigned dim
, isl_int
*val
)
7034 return isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[0],
7038 fixed
= isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[0],
7040 for (i
= 1; fixed
== 1 && i
< set
->n
; ++i
) {
7041 fixed
= isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[i
],
7043 if (fixed
== 1 && isl_int_ne(tmp
, v
))
7047 isl_int_set(*val
, v
);
7058 /* uset_gist depends on constraints without existentially quantified
7059 * variables sorting first.
7061 static int qsort_constraint_cmp(const void *p1
, const void *p2
)
7063 const struct constraint
*c1
= (const struct constraint
*)p1
;
7064 const struct constraint
*c2
= (const struct constraint
*)p2
;
7066 unsigned size
= isl_min(c1
->size
, c2
->size
);
7068 l1
= isl_seq_last_non_zero(c1
->c
, size
);
7069 l2
= isl_seq_last_non_zero(c2
->c
, size
);
7074 return isl_seq_cmp(c1
->c
, c2
->c
, size
);
7077 static struct isl_basic_map
*isl_basic_map_sort_constraints(
7078 struct isl_basic_map
*bmap
)
7081 struct constraint
*c
;
7086 total
= isl_basic_map_total_dim(bmap
);
7087 c
= isl_alloc_array(bmap
->ctx
, struct constraint
, bmap
->n_ineq
);
7090 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
7092 c
[i
].c
= bmap
->ineq
[i
];
7094 qsort(c
, bmap
->n_ineq
, sizeof(struct constraint
), qsort_constraint_cmp
);
7095 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
7096 bmap
->ineq
[i
] = c
[i
].c
;
7100 isl_basic_map_free(bmap
);
7104 __isl_give isl_basic_set
*isl_basic_set_sort_constraints(
7105 __isl_take isl_basic_set
*bset
)
7107 return (struct isl_basic_set
*)isl_basic_map_sort_constraints(
7108 (struct isl_basic_map
*)bset
);
7111 struct isl_basic_map
*isl_basic_map_normalize(struct isl_basic_map
*bmap
)
7115 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED
))
7117 bmap
= isl_basic_map_remove_redundancies(bmap
);
7118 bmap
= isl_basic_map_sort_constraints(bmap
);
7119 ISL_F_SET(bmap
, ISL_BASIC_MAP_NORMALIZED
);
7123 struct isl_basic_set
*isl_basic_set_normalize(struct isl_basic_set
*bset
)
7125 return (struct isl_basic_set
*)isl_basic_map_normalize(
7126 (struct isl_basic_map
*)bset
);
7129 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map
*bmap1
,
7130 const __isl_keep isl_basic_map
*bmap2
)
7137 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) !=
7138 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_RATIONAL
))
7139 return ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) ? -1 : 1;
7140 if (isl_basic_map_n_param(bmap1
) != isl_basic_map_n_param(bmap2
))
7141 return isl_basic_map_n_param(bmap1
) - isl_basic_map_n_param(bmap2
);
7142 if (isl_basic_map_n_in(bmap1
) != isl_basic_map_n_in(bmap2
))
7143 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
7144 if (isl_basic_map_n_out(bmap1
) != isl_basic_map_n_out(bmap2
))
7145 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
7146 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
) &&
7147 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
7149 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
))
7151 if (ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
7153 if (bmap1
->n_eq
!= bmap2
->n_eq
)
7154 return bmap1
->n_eq
- bmap2
->n_eq
;
7155 if (bmap1
->n_ineq
!= bmap2
->n_ineq
)
7156 return bmap1
->n_ineq
- bmap2
->n_ineq
;
7157 if (bmap1
->n_div
!= bmap2
->n_div
)
7158 return bmap1
->n_div
- bmap2
->n_div
;
7159 total
= isl_basic_map_total_dim(bmap1
);
7160 for (i
= 0; i
< bmap1
->n_eq
; ++i
) {
7161 cmp
= isl_seq_cmp(bmap1
->eq
[i
], bmap2
->eq
[i
], 1+total
);
7165 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
7166 cmp
= isl_seq_cmp(bmap1
->ineq
[i
], bmap2
->ineq
[i
], 1+total
);
7170 for (i
= 0; i
< bmap1
->n_div
; ++i
) {
7171 cmp
= isl_seq_cmp(bmap1
->div
[i
], bmap2
->div
[i
], 1+1+total
);
7178 int isl_basic_map_plain_is_equal(__isl_keep isl_basic_map
*bmap1
,
7179 __isl_keep isl_basic_map
*bmap2
)
7181 return isl_basic_map_plain_cmp(bmap1
, bmap2
) == 0;
7184 int isl_basic_set_plain_is_equal(__isl_keep isl_basic_set
*bset1
,
7185 __isl_keep isl_basic_set
*bset2
)
7187 return isl_basic_map_plain_is_equal((isl_basic_map
*)bset1
,
7188 (isl_basic_map
*)bset2
);
7191 static int qsort_bmap_cmp(const void *p1
, const void *p2
)
7193 const struct isl_basic_map
*bmap1
= *(const struct isl_basic_map
**)p1
;
7194 const struct isl_basic_map
*bmap2
= *(const struct isl_basic_map
**)p2
;
7196 return isl_basic_map_plain_cmp(bmap1
, bmap2
);
7199 /* We normalize in place, but if anything goes wrong we need
7200 * to return NULL, so we need to make sure we don't change the
7201 * meaning of any possible other copies of map.
7203 struct isl_map
*isl_map_normalize(struct isl_map
*map
)
7206 struct isl_basic_map
*bmap
;
7210 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
))
7212 for (i
= 0; i
< map
->n
; ++i
) {
7213 bmap
= isl_basic_map_normalize(isl_basic_map_copy(map
->p
[i
]));
7216 isl_basic_map_free(map
->p
[i
]);
7219 qsort(map
->p
, map
->n
, sizeof(struct isl_basic_map
*), qsort_bmap_cmp
);
7220 ISL_F_SET(map
, ISL_MAP_NORMALIZED
);
7221 map
= isl_map_remove_empty_parts(map
);
7224 for (i
= map
->n
- 1; i
>= 1; --i
) {
7225 if (!isl_basic_map_plain_is_equal(map
->p
[i
-1], map
->p
[i
]))
7227 isl_basic_map_free(map
->p
[i
-1]);
7228 for (j
= i
; j
< map
->n
; ++j
)
7229 map
->p
[j
-1] = map
->p
[j
];
7239 struct isl_set
*isl_set_normalize(struct isl_set
*set
)
7241 return (struct isl_set
*)isl_map_normalize((struct isl_map
*)set
);
7244 int isl_map_plain_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
7254 if (!isl_dim_equal(map1
->dim
, map2
->dim
))
7257 map1
= isl_map_copy(map1
);
7258 map2
= isl_map_copy(map2
);
7259 map1
= isl_map_normalize(map1
);
7260 map2
= isl_map_normalize(map2
);
7263 equal
= map1
->n
== map2
->n
;
7264 for (i
= 0; equal
&& i
< map1
->n
; ++i
) {
7265 equal
= isl_basic_map_plain_is_equal(map1
->p
[i
], map2
->p
[i
]);
7278 int isl_map_fast_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
7280 return isl_map_plain_is_equal(map1
, map2
);
7283 int isl_set_plain_is_equal(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
7285 return isl_map_plain_is_equal((struct isl_map
*)set1
,
7286 (struct isl_map
*)set2
);
7289 int isl_set_fast_is_equal(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
7291 return isl_set_plain_is_equal(set1
, set2
);
7294 /* Return an interval that ranges from min to max (inclusive)
7296 struct isl_basic_set
*isl_basic_set_interval(struct isl_ctx
*ctx
,
7297 isl_int min
, isl_int max
)
7300 struct isl_basic_set
*bset
= NULL
;
7302 bset
= isl_basic_set_alloc(ctx
, 0, 1, 0, 0, 2);
7306 k
= isl_basic_set_alloc_inequality(bset
);
7309 isl_int_set_si(bset
->ineq
[k
][1], 1);
7310 isl_int_neg(bset
->ineq
[k
][0], min
);
7312 k
= isl_basic_set_alloc_inequality(bset
);
7315 isl_int_set_si(bset
->ineq
[k
][1], -1);
7316 isl_int_set(bset
->ineq
[k
][0], max
);
7320 isl_basic_set_free(bset
);
7324 /* Return the Cartesian product of the basic sets in list (in the given order).
7326 __isl_give isl_basic_set
*isl_basic_set_list_product(
7327 __isl_take
struct isl_basic_set_list
*list
)
7335 struct isl_basic_set
*product
= NULL
;
7339 isl_assert(list
->ctx
, list
->n
> 0, goto error
);
7340 isl_assert(list
->ctx
, list
->p
[0], goto error
);
7341 nparam
= isl_basic_set_n_param(list
->p
[0]);
7342 dim
= isl_basic_set_n_dim(list
->p
[0]);
7343 extra
= list
->p
[0]->n_div
;
7344 n_eq
= list
->p
[0]->n_eq
;
7345 n_ineq
= list
->p
[0]->n_ineq
;
7346 for (i
= 1; i
< list
->n
; ++i
) {
7347 isl_assert(list
->ctx
, list
->p
[i
], goto error
);
7348 isl_assert(list
->ctx
,
7349 nparam
== isl_basic_set_n_param(list
->p
[i
]), goto error
);
7350 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
7351 extra
+= list
->p
[i
]->n_div
;
7352 n_eq
+= list
->p
[i
]->n_eq
;
7353 n_ineq
+= list
->p
[i
]->n_ineq
;
7355 product
= isl_basic_set_alloc(list
->ctx
, nparam
, dim
, extra
,
7360 for (i
= 0; i
< list
->n
; ++i
) {
7361 isl_basic_set_add_constraints(product
,
7362 isl_basic_set_copy(list
->p
[i
]), dim
);
7363 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
7365 isl_basic_set_list_free(list
);
7368 isl_basic_set_free(product
);
7369 isl_basic_set_list_free(list
);
7373 struct isl_basic_map
*isl_basic_map_product(
7374 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
7376 struct isl_dim
*dim_result
= NULL
;
7377 struct isl_basic_map
*bmap
;
7378 unsigned in1
, in2
, out1
, out2
, nparam
, total
, pos
;
7379 struct isl_dim_map
*dim_map1
, *dim_map2
;
7381 if (!bmap1
|| !bmap2
)
7384 isl_assert(bmap1
->ctx
, isl_dim_match(bmap1
->dim
, isl_dim_param
,
7385 bmap2
->dim
, isl_dim_param
), goto error
);
7386 dim_result
= isl_dim_product(isl_dim_copy(bmap1
->dim
),
7387 isl_dim_copy(bmap2
->dim
));
7389 in1
= isl_basic_map_n_in(bmap1
);
7390 in2
= isl_basic_map_n_in(bmap2
);
7391 out1
= isl_basic_map_n_out(bmap1
);
7392 out2
= isl_basic_map_n_out(bmap2
);
7393 nparam
= isl_basic_map_n_param(bmap1
);
7395 total
= nparam
+ in1
+ in2
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
7396 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
7397 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
7398 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
7399 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
7400 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
7401 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
7402 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
7403 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
7404 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
7405 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
7407 bmap
= isl_basic_map_alloc_dim(dim_result
,
7408 bmap1
->n_div
+ bmap2
->n_div
,
7409 bmap1
->n_eq
+ bmap2
->n_eq
,
7410 bmap1
->n_ineq
+ bmap2
->n_ineq
);
7411 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
7412 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
7413 bmap
= isl_basic_map_simplify(bmap
);
7414 return isl_basic_map_finalize(bmap
);
7416 isl_basic_map_free(bmap1
);
7417 isl_basic_map_free(bmap2
);
7421 __isl_give isl_basic_map
*isl_basic_map_flat_product(
7422 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
7424 isl_basic_map
*prod
;
7426 prod
= isl_basic_map_product(bmap1
, bmap2
);
7427 prod
= isl_basic_map_flatten(prod
);
7431 __isl_give isl_basic_set
*isl_basic_set_flat_product(
7432 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
7434 return isl_basic_map_flat_product(bset1
, bset2
);
7437 __isl_give isl_basic_map
*isl_basic_map_range_product(
7438 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
7440 isl_dim
*dim_result
= NULL
;
7441 isl_basic_map
*bmap
;
7442 unsigned in
, out1
, out2
, nparam
, total
, pos
;
7443 struct isl_dim_map
*dim_map1
, *dim_map2
;
7445 if (!bmap1
|| !bmap2
)
7448 dim_result
= isl_dim_range_product(isl_dim_copy(bmap1
->dim
),
7449 isl_dim_copy(bmap2
->dim
));
7451 in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
7452 out1
= isl_basic_map_n_out(bmap1
);
7453 out2
= isl_basic_map_n_out(bmap2
);
7454 nparam
= isl_basic_map_n_param(bmap1
);
7456 total
= nparam
+ in
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
7457 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
7458 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
7459 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
7460 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
7461 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
7462 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
7463 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in
);
7464 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
7465 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
7466 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
7468 bmap
= isl_basic_map_alloc_dim(dim_result
,
7469 bmap1
->n_div
+ bmap2
->n_div
,
7470 bmap1
->n_eq
+ bmap2
->n_eq
,
7471 bmap1
->n_ineq
+ bmap2
->n_ineq
);
7472 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
7473 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
7474 bmap
= isl_basic_map_simplify(bmap
);
7475 return isl_basic_map_finalize(bmap
);
7477 isl_basic_map_free(bmap1
);
7478 isl_basic_map_free(bmap2
);
7482 __isl_give isl_basic_map
*isl_basic_map_flat_range_product(
7483 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
7485 isl_basic_map
*prod
;
7487 prod
= isl_basic_map_range_product(bmap1
, bmap2
);
7488 prod
= isl_basic_map_flatten_range(prod
);
7492 static __isl_give isl_map
*map_product(__isl_take isl_map
*map1
,
7493 __isl_take isl_map
*map2
,
7494 __isl_give isl_dim
*(*dim_product
)(__isl_take isl_dim
*left
,
7495 __isl_take isl_dim
*right
),
7496 __isl_give isl_basic_map
*(*basic_map_product
)(
7497 __isl_take isl_basic_map
*left
, __isl_take isl_basic_map
*right
))
7500 struct isl_map
*result
;
7506 isl_assert(map1
->ctx
, isl_dim_match(map1
->dim
, isl_dim_param
,
7507 map2
->dim
, isl_dim_param
), goto error
);
7509 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
7510 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
7511 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
7513 result
= isl_map_alloc_dim(dim_product(isl_dim_copy(map1
->dim
),
7514 isl_dim_copy(map2
->dim
)),
7515 map1
->n
* map2
->n
, flags
);
7518 for (i
= 0; i
< map1
->n
; ++i
)
7519 for (j
= 0; j
< map2
->n
; ++j
) {
7520 struct isl_basic_map
*part
;
7521 part
= basic_map_product(isl_basic_map_copy(map1
->p
[i
]),
7522 isl_basic_map_copy(map2
->p
[j
]));
7523 if (isl_basic_map_is_empty(part
))
7524 isl_basic_map_free(part
);
7526 result
= isl_map_add_basic_map(result
, part
);
7539 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
7541 struct isl_map
*isl_map_product(struct isl_map
*map1
, struct isl_map
*map2
)
7543 return map_product(map1
, map2
, &isl_dim_product
, &isl_basic_map_product
);
7546 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
7548 __isl_give isl_map
*isl_map_flat_product(__isl_take isl_map
*map1
,
7549 __isl_take isl_map
*map2
)
7553 prod
= isl_map_product(map1
, map2
);
7554 prod
= isl_map_flatten(prod
);
7558 /* Given two set A and B, construct its Cartesian product A x B.
7560 struct isl_set
*isl_set_product(struct isl_set
*set1
, struct isl_set
*set2
)
7562 return (struct isl_set
*)isl_map_product((struct isl_map
*)set1
,
7563 (struct isl_map
*)set2
);
7566 __isl_give isl_set
*isl_set_flat_product(__isl_take isl_set
*set1
,
7567 __isl_take isl_set
*set2
)
7569 return (isl_set
*)isl_map_flat_product((isl_map
*)set1
, (isl_map
*)set2
);
7572 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
7574 __isl_give isl_map
*isl_map_range_product(__isl_take isl_map
*map1
,
7575 __isl_take isl_map
*map2
)
7577 return map_product(map1
, map2
, &isl_dim_range_product
,
7578 &isl_basic_map_range_product
);
7581 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
7583 __isl_give isl_map
*isl_map_flat_range_product(__isl_take isl_map
*map1
,
7584 __isl_take isl_map
*map2
)
7588 prod
= isl_map_range_product(map1
, map2
);
7589 prod
= isl_map_flatten_range(prod
);
7593 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map
*bmap
)
7596 uint32_t hash
= isl_hash_init();
7601 bmap
= isl_basic_map_copy(bmap
);
7602 bmap
= isl_basic_map_normalize(bmap
);
7605 total
= isl_basic_map_total_dim(bmap
);
7606 isl_hash_byte(hash
, bmap
->n_eq
& 0xFF);
7607 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
7609 c_hash
= isl_seq_get_hash(bmap
->eq
[i
], 1 + total
);
7610 isl_hash_hash(hash
, c_hash
);
7612 isl_hash_byte(hash
, bmap
->n_ineq
& 0xFF);
7613 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
7615 c_hash
= isl_seq_get_hash(bmap
->ineq
[i
], 1 + total
);
7616 isl_hash_hash(hash
, c_hash
);
7618 isl_hash_byte(hash
, bmap
->n_div
& 0xFF);
7619 for (i
= 0; i
< bmap
->n_div
; ++i
) {
7621 if (isl_int_is_zero(bmap
->div
[i
][0]))
7623 isl_hash_byte(hash
, i
& 0xFF);
7624 c_hash
= isl_seq_get_hash(bmap
->div
[i
], 1 + 1 + total
);
7625 isl_hash_hash(hash
, c_hash
);
7627 isl_basic_map_free(bmap
);
7631 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set
*bset
)
7633 return isl_basic_map_get_hash((isl_basic_map
*)bset
);
7636 uint32_t isl_map_get_hash(__isl_keep isl_map
*map
)
7643 map
= isl_map_copy(map
);
7644 map
= isl_map_normalize(map
);
7648 hash
= isl_hash_init();
7649 for (i
= 0; i
< map
->n
; ++i
) {
7651 bmap_hash
= isl_basic_map_get_hash(map
->p
[i
]);
7652 isl_hash_hash(hash
, bmap_hash
);
7660 uint32_t isl_set_get_hash(__isl_keep isl_set
*set
)
7662 return isl_map_get_hash((isl_map
*)set
);
7665 /* Check if the value for dimension dim is completely determined
7666 * by the values of the other parameters and variables.
7667 * That is, check if dimension dim is involved in an equality.
7669 int isl_basic_set_dim_is_unique(struct isl_basic_set
*bset
, unsigned dim
)
7676 nparam
= isl_basic_set_n_param(bset
);
7677 for (i
= 0; i
< bset
->n_eq
; ++i
)
7678 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ dim
]))
7683 /* Check if the value for dimension dim is completely determined
7684 * by the values of the other parameters and variables.
7685 * That is, check if dimension dim is involved in an equality
7686 * for each of the subsets.
7688 int isl_set_dim_is_unique(struct isl_set
*set
, unsigned dim
)
7694 for (i
= 0; i
< set
->n
; ++i
) {
7696 unique
= isl_basic_set_dim_is_unique(set
->p
[i
], dim
);
7703 int isl_set_n_basic_set(__isl_keep isl_set
*set
)
7705 return set
? set
->n
: 0;
7708 int isl_map_foreach_basic_map(__isl_keep isl_map
*map
,
7709 int (*fn
)(__isl_take isl_basic_map
*bmap
, void *user
), void *user
)
7716 for (i
= 0; i
< map
->n
; ++i
)
7717 if (fn(isl_basic_map_copy(map
->p
[i
]), user
) < 0)
7723 int isl_set_foreach_basic_set(__isl_keep isl_set
*set
,
7724 int (*fn
)(__isl_take isl_basic_set
*bset
, void *user
), void *user
)
7731 for (i
= 0; i
< set
->n
; ++i
)
7732 if (fn(isl_basic_set_copy(set
->p
[i
]), user
) < 0)
7738 __isl_give isl_basic_set
*isl_basic_set_lift(__isl_take isl_basic_set
*bset
)
7740 struct isl_dim
*dim
;
7745 bset
= isl_basic_set_cow(bset
);
7749 dim
= isl_basic_set_get_dim(bset
);
7750 dim
= isl_dim_lift(dim
, bset
->n_div
);
7753 isl_dim_free(bset
->dim
);
7755 bset
->extra
-= bset
->n_div
;
7758 bset
= isl_basic_set_finalize(bset
);
7762 isl_basic_set_free(bset
);
7766 __isl_give isl_set
*isl_set_lift(__isl_take isl_set
*set
)
7769 struct isl_dim
*dim
;
7772 set
= isl_set_align_divs(set
);
7777 set
= isl_set_cow(set
);
7781 n_div
= set
->p
[0]->n_div
;
7782 dim
= isl_set_get_dim(set
);
7783 dim
= isl_dim_lift(dim
, n_div
);
7786 isl_dim_free(set
->dim
);
7789 for (i
= 0; i
< set
->n
; ++i
) {
7790 set
->p
[i
] = isl_basic_set_lift(set
->p
[i
]);
7801 __isl_give isl_map
*isl_set_lifting(__isl_take isl_set
*set
)
7803 struct isl_dim
*dim
;
7804 struct isl_basic_map
*bmap
;
7811 set
= isl_set_align_divs(set
);
7816 dim
= isl_set_get_dim(set
);
7817 if (set
->n
== 0 || set
->p
[0]->n_div
== 0) {
7819 return isl_map_identity(isl_dim_map_from_set(dim
));
7822 n_div
= set
->p
[0]->n_div
;
7823 dim
= isl_dim_map_from_set(dim
);
7824 n_param
= isl_dim_size(dim
, isl_dim_param
);
7825 n_set
= isl_dim_size(dim
, isl_dim_in
);
7826 dim
= isl_dim_extend(dim
, n_param
, n_set
, n_set
+ n_div
);
7827 bmap
= isl_basic_map_alloc_dim(dim
, 0, n_set
, 2 * n_div
);
7828 for (i
= 0; i
< n_set
; ++i
)
7829 bmap
= var_equal(bmap
, i
);
7831 total
= n_param
+ n_set
+ n_set
+ n_div
;
7832 for (i
= 0; i
< n_div
; ++i
) {
7833 k
= isl_basic_map_alloc_inequality(bmap
);
7836 isl_seq_cpy(bmap
->ineq
[k
], set
->p
[0]->div
[i
]+1, 1+n_param
);
7837 isl_seq_clr(bmap
->ineq
[k
]+1+n_param
, n_set
);
7838 isl_seq_cpy(bmap
->ineq
[k
]+1+n_param
+n_set
,
7839 set
->p
[0]->div
[i
]+1+1+n_param
, n_set
+ n_div
);
7840 isl_int_neg(bmap
->ineq
[k
][1+n_param
+n_set
+n_set
+i
],
7841 set
->p
[0]->div
[i
][0]);
7843 l
= isl_basic_map_alloc_inequality(bmap
);
7846 isl_seq_neg(bmap
->ineq
[l
], bmap
->ineq
[k
], 1 + total
);
7847 isl_int_add(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0],
7848 set
->p
[0]->div
[i
][0]);
7849 isl_int_sub_ui(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], 1);
7853 bmap
= isl_basic_map_simplify(bmap
);
7854 bmap
= isl_basic_map_finalize(bmap
);
7855 return isl_map_from_basic_map(bmap
);
7858 isl_basic_map_free(bmap
);
7862 int isl_basic_set_size(__isl_keep isl_basic_set
*bset
)
7870 dim
= isl_basic_set_total_dim(bset
);
7871 size
+= bset
->n_eq
* (1 + dim
);
7872 size
+= bset
->n_ineq
* (1 + dim
);
7873 size
+= bset
->n_div
* (2 + dim
);
7878 int isl_set_size(__isl_keep isl_set
*set
)
7886 for (i
= 0; i
< set
->n
; ++i
)
7887 size
+= isl_basic_set_size(set
->p
[i
]);
7892 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
7893 enum isl_dim_type type
, unsigned pos
)
7901 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), return -1);
7903 pos
+= isl_basic_map_offset(bmap
, type
);
7905 for (i
= 0; i
< bmap
->n_eq
; ++i
)
7906 if (!isl_int_is_zero(bmap
->eq
[i
][pos
]))
7910 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
7911 int sgn
= isl_int_sgn(bmap
->ineq
[i
][pos
]);
7918 return lower
&& upper
;
7921 int isl_map_dim_is_bounded(__isl_keep isl_map
*map
,
7922 enum isl_dim_type type
, unsigned pos
)
7929 for (i
= 0; i
< map
->n
; ++i
) {
7931 bounded
= isl_basic_map_dim_is_bounded(map
->p
[i
], type
, pos
);
7932 if (bounded
< 0 || !bounded
)
7939 /* Return 1 if the specified dim is involved in both an upper bound
7940 * and a lower bound.
7942 int isl_set_dim_is_bounded(__isl_keep isl_set
*set
,
7943 enum isl_dim_type type
, unsigned pos
)
7945 return isl_map_dim_is_bounded((isl_map
*)set
, type
, pos
);
7948 /* For each of the "n" variables starting at "first", determine
7949 * the sign of the variable and put the results in the first "n"
7950 * elements of the array "signs".
7952 * 1 means that the variable is non-negative
7953 * -1 means that the variable is non-positive
7954 * 0 means the variable attains both positive and negative values.
7956 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set
*bset
,
7957 unsigned first
, unsigned n
, int *signs
)
7959 isl_vec
*bound
= NULL
;
7960 struct isl_tab
*tab
= NULL
;
7961 struct isl_tab_undo
*snap
;
7964 if (!bset
|| !signs
)
7967 bound
= isl_vec_alloc(bset
->ctx
, 1 + isl_basic_set_total_dim(bset
));
7968 tab
= isl_tab_from_basic_set(bset
);
7972 isl_seq_clr(bound
->el
, bound
->size
);
7973 isl_int_set_si(bound
->el
[0], -1);
7975 snap
= isl_tab_snap(tab
);
7976 for (i
= 0; i
< n
; ++i
) {
7979 isl_int_set_si(bound
->el
[1 + first
+ i
], -1);
7980 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
7983 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
7984 if (isl_tab_rollback(tab
, snap
) < 0)
7992 isl_int_set_si(bound
->el
[1 + first
+ i
], 1);
7993 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
7996 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
7997 if (isl_tab_rollback(tab
, snap
) < 0)
8000 signs
[i
] = empty
? -1 : 0;
8004 isl_vec_free(bound
);
8008 isl_vec_free(bound
);
8012 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set
*bset
,
8013 enum isl_dim_type type
, unsigned first
, unsigned n
, int *signs
)
8015 if (!bset
|| !signs
)
8017 isl_assert(bset
->ctx
, first
+ n
<= isl_basic_set_dim(bset
, type
),
8020 first
+= pos(bset
->dim
, type
) - 1;
8021 return isl_basic_set_vars_get_sign(bset
, first
, n
, signs
);
8024 /* Check if the given basic map is obviously single-valued.
8025 * In particular, for each output dimension, check that there is
8026 * an equality that defines the output dimension in terms of
8027 * earlier dimensions.
8029 int isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map
*bmap
)
8039 total
= 1 + isl_basic_map_total_dim(bmap
);
8040 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
8041 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
8043 for (i
= 0; i
< n_out
; ++i
) {
8044 for (j
= 0; j
< bmap
->n_eq
; ++j
) {
8045 if (isl_int_is_zero(bmap
->eq
[j
][o_out
+ i
]))
8047 if (isl_seq_first_non_zero(bmap
->eq
[j
] + o_out
+ i
+ 1,
8048 total
- (o_out
+ i
+ 1)) == -1)
8051 if (j
>= bmap
->n_eq
)
8058 /* Check if the given map is obviously single-valued.
8060 int isl_map_plain_is_single_valued(__isl_keep isl_map
*map
)
8069 return isl_basic_map_plain_is_single_valued(map
->p
[0]);
8072 /* Check if the given map is single-valued.
8077 * and check if the result is a subset of the identity mapping.
8079 int isl_map_is_single_valued(__isl_keep isl_map
*map
)
8086 sv
= isl_map_plain_is_single_valued(map
);
8090 test
= isl_map_reverse(isl_map_copy(map
));
8091 test
= isl_map_apply_range(test
, isl_map_copy(map
));
8093 dim
= isl_dim_map_from_set(isl_dim_range(isl_map_get_dim(map
)));
8094 id
= isl_map_identity(dim
);
8096 sv
= isl_map_is_subset(test
, id
);
8104 int isl_map_is_injective(__isl_keep isl_map
*map
)
8108 map
= isl_map_copy(map
);
8109 map
= isl_map_reverse(map
);
8110 in
= isl_map_is_single_valued(map
);
8116 /* Check if the given map is obviously injective.
8118 int isl_map_plain_is_injective(__isl_keep isl_map
*map
)
8122 map
= isl_map_copy(map
);
8123 map
= isl_map_reverse(map
);
8124 in
= isl_map_plain_is_single_valued(map
);
8130 int isl_map_is_bijective(__isl_keep isl_map
*map
)
8134 sv
= isl_map_is_single_valued(map
);
8138 return isl_map_is_injective(map
);
8141 int isl_set_is_singleton(__isl_keep isl_set
*set
)
8143 return isl_map_is_single_valued((isl_map
*)set
);
8146 int isl_map_is_translation(__isl_keep isl_map
*map
)
8151 delta
= isl_map_deltas(isl_map_copy(map
));
8152 ok
= isl_set_is_singleton(delta
);
8153 isl_set_free(delta
);
8158 static int unique(isl_int
*p
, unsigned pos
, unsigned len
)
8160 if (isl_seq_first_non_zero(p
, pos
) != -1)
8162 if (isl_seq_first_non_zero(p
+ pos
+ 1, len
- pos
- 1) != -1)
8167 int isl_basic_set_is_box(__isl_keep isl_basic_set
*bset
)
8176 if (isl_basic_set_dim(bset
, isl_dim_div
) != 0)
8179 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
8180 ovar
= isl_dim_offset(bset
->dim
, isl_dim_set
);
8181 for (j
= 0; j
< nvar
; ++j
) {
8182 int lower
= 0, upper
= 0;
8183 for (i
= 0; i
< bset
->n_eq
; ++i
) {
8184 if (isl_int_is_zero(bset
->eq
[i
][1 + ovar
+ j
]))
8186 if (!unique(bset
->eq
[i
] + 1 + ovar
, j
, nvar
))
8192 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
8193 if (isl_int_is_zero(bset
->ineq
[i
][1 + ovar
+ j
]))
8195 if (!unique(bset
->ineq
[i
] + 1 + ovar
, j
, nvar
))
8197 if (isl_int_is_pos(bset
->ineq
[i
][1 + ovar
+ j
]))
8202 if (!lower
|| !upper
)
8209 int isl_set_is_box(__isl_keep isl_set
*set
)
8216 return isl_basic_set_is_box(set
->p
[0]);
8219 int isl_basic_set_is_wrapping(__isl_keep isl_basic_set
*bset
)
8224 return isl_dim_is_wrapping(bset
->dim
);
8227 int isl_set_is_wrapping(__isl_keep isl_set
*set
)
8232 return isl_dim_is_wrapping(set
->dim
);
8235 __isl_give isl_basic_set
*isl_basic_map_wrap(__isl_take isl_basic_map
*bmap
)
8237 bmap
= isl_basic_map_cow(bmap
);
8241 bmap
->dim
= isl_dim_wrap(bmap
->dim
);
8245 bmap
= isl_basic_map_finalize(bmap
);
8247 return (isl_basic_set
*)bmap
;
8249 isl_basic_map_free(bmap
);
8253 __isl_give isl_set
*isl_map_wrap(__isl_take isl_map
*map
)
8257 map
= isl_map_cow(map
);
8261 for (i
= 0; i
< map
->n
; ++i
) {
8262 map
->p
[i
] = (isl_basic_map
*)isl_basic_map_wrap(map
->p
[i
]);
8266 map
->dim
= isl_dim_wrap(map
->dim
);
8270 return (isl_set
*)map
;
8276 __isl_give isl_basic_map
*isl_basic_set_unwrap(__isl_take isl_basic_set
*bset
)
8278 bset
= isl_basic_set_cow(bset
);
8282 bset
->dim
= isl_dim_unwrap(bset
->dim
);
8286 bset
= isl_basic_set_finalize(bset
);
8288 return (isl_basic_map
*)bset
;
8290 isl_basic_set_free(bset
);
8294 __isl_give isl_map
*isl_set_unwrap(__isl_take isl_set
*set
)
8301 if (!isl_set_is_wrapping(set
))
8302 isl_die(set
->ctx
, isl_error_invalid
, "not a wrapping set",
8305 set
= isl_set_cow(set
);
8309 for (i
= 0; i
< set
->n
; ++i
) {
8310 set
->p
[i
] = (isl_basic_set
*)isl_basic_set_unwrap(set
->p
[i
]);
8315 set
->dim
= isl_dim_unwrap(set
->dim
);
8319 return (isl_map
*)set
;
8325 __isl_give isl_basic_map
*isl_basic_map_reset(__isl_take isl_basic_map
*bmap
,
8326 enum isl_dim_type type
)
8331 if (!isl_dim_is_named_or_nested(bmap
->dim
, type
))
8334 bmap
= isl_basic_map_cow(bmap
);
8338 bmap
->dim
= isl_dim_reset(bmap
->dim
, type
);
8342 bmap
= isl_basic_map_finalize(bmap
);
8346 isl_basic_map_free(bmap
);
8350 __isl_give isl_map
*isl_map_reset(__isl_take isl_map
*map
,
8351 enum isl_dim_type type
)
8358 if (!isl_dim_is_named_or_nested(map
->dim
, type
))
8361 map
= isl_map_cow(map
);
8365 for (i
= 0; i
< map
->n
; ++i
) {
8366 map
->p
[i
] = isl_basic_map_reset(map
->p
[i
], type
);
8370 map
->dim
= isl_dim_reset(map
->dim
, type
);
8380 __isl_give isl_basic_map
*isl_basic_map_flatten(__isl_take isl_basic_map
*bmap
)
8385 if (!bmap
->dim
->nested
[0] && !bmap
->dim
->nested
[1])
8388 bmap
= isl_basic_map_cow(bmap
);
8392 bmap
->dim
= isl_dim_flatten(bmap
->dim
);
8396 bmap
= isl_basic_map_finalize(bmap
);
8400 isl_basic_map_free(bmap
);
8404 __isl_give isl_basic_set
*isl_basic_set_flatten(__isl_take isl_basic_set
*bset
)
8406 return (isl_basic_set
*)isl_basic_map_flatten((isl_basic_map
*)bset
);
8409 __isl_give isl_basic_map
*isl_basic_map_flatten_range(
8410 __isl_take isl_basic_map
*bmap
)
8415 if (!bmap
->dim
->nested
[1])
8418 bmap
= isl_basic_map_cow(bmap
);
8422 bmap
->dim
= isl_dim_flatten_range(bmap
->dim
);
8426 bmap
= isl_basic_map_finalize(bmap
);
8430 isl_basic_map_free(bmap
);
8434 __isl_give isl_map
*isl_map_flatten(__isl_take isl_map
*map
)
8441 if (!map
->dim
->nested
[0] && !map
->dim
->nested
[1])
8444 map
= isl_map_cow(map
);
8448 for (i
= 0; i
< map
->n
; ++i
) {
8449 map
->p
[i
] = isl_basic_map_flatten(map
->p
[i
]);
8453 map
->dim
= isl_dim_flatten(map
->dim
);
8463 __isl_give isl_set
*isl_set_flatten(__isl_take isl_set
*set
)
8465 return (isl_set
*)isl_map_flatten((isl_map
*)set
);
8468 __isl_give isl_map
*isl_set_flatten_map(__isl_take isl_set
*set
)
8470 isl_dim
*dim
, *flat_dim
;
8473 dim
= isl_set_get_dim(set
);
8474 flat_dim
= isl_dim_flatten(isl_dim_copy(dim
));
8475 map
= isl_map_identity(isl_dim_join(isl_dim_reverse(dim
), flat_dim
));
8476 map
= isl_map_intersect_domain(map
, set
);
8481 __isl_give isl_map
*isl_map_flatten_range(__isl_take isl_map
*map
)
8488 if (!map
->dim
->nested
[1])
8491 map
= isl_map_cow(map
);
8495 for (i
= 0; i
< map
->n
; ++i
) {
8496 map
->p
[i
] = isl_basic_map_flatten_range(map
->p
[i
]);
8500 map
->dim
= isl_dim_flatten_range(map
->dim
);
8510 /* Reorder the dimensions of "bmap" according to the given dim_map
8511 * and set the dimension specification to "dim".
8513 __isl_give isl_basic_map
*isl_basic_map_realign(__isl_take isl_basic_map
*bmap
,
8514 __isl_take isl_dim
*dim
, __isl_take
struct isl_dim_map
*dim_map
)
8518 bmap
= isl_basic_map_cow(bmap
);
8519 if (!bmap
|| !dim
|| !dim_map
)
8522 res
= isl_basic_map_alloc_dim(dim
,
8523 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
8524 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
8525 res
= isl_basic_map_finalize(res
);
8529 isl_basic_map_free(bmap
);
8534 /* Reorder the dimensions of "map" according to given reordering.
8536 __isl_give isl_map
*isl_map_realign(__isl_take isl_map
*map
,
8537 __isl_take isl_reordering
*r
)
8540 struct isl_dim_map
*dim_map
;
8542 map
= isl_map_cow(map
);
8543 dim_map
= isl_dim_map_from_reordering(r
);
8544 if (!map
|| !r
|| !dim_map
)
8547 for (i
= 0; i
< map
->n
; ++i
) {
8548 struct isl_dim_map
*dim_map_i
;
8550 dim_map_i
= isl_dim_map_extend(dim_map
, map
->p
[i
]);
8552 map
->p
[i
] = isl_basic_map_realign(map
->p
[i
],
8553 isl_dim_copy(r
->dim
), dim_map_i
);
8559 map
= isl_map_reset_dim(map
, isl_dim_copy(r
->dim
));
8561 isl_reordering_free(r
);
8567 isl_reordering_free(r
);
8571 __isl_give isl_set
*isl_set_realign(__isl_take isl_set
*set
,
8572 __isl_take isl_reordering
*r
)
8574 return (isl_set
*)isl_map_realign((isl_map
*)set
, r
);
8577 __isl_give isl_map
*isl_map_align_params(__isl_take isl_map
*map
,
8578 __isl_take isl_dim
*model
)
8585 ctx
= isl_dim_get_ctx(model
);
8586 if (!isl_dim_has_named_params(model
))
8587 isl_die(ctx
, isl_error_invalid
,
8588 "model has unnamed parameters", goto error
);
8589 if (!isl_dim_has_named_params(map
->dim
))
8590 isl_die(ctx
, isl_error_invalid
,
8591 "relation has unnamed parameters", goto error
);
8592 if (!isl_dim_match(map
->dim
, isl_dim_param
, model
, isl_dim_param
)) {
8593 isl_reordering
*exp
;
8595 model
= isl_dim_drop(model
, isl_dim_in
,
8596 0, isl_dim_size(model
, isl_dim_in
));
8597 model
= isl_dim_drop(model
, isl_dim_out
,
8598 0, isl_dim_size(model
, isl_dim_out
));
8599 exp
= isl_parameter_alignment_reordering(map
->dim
, model
);
8600 exp
= isl_reordering_extend_dim(exp
, isl_map_get_dim(map
));
8601 map
= isl_map_realign(map
, exp
);
8604 isl_dim_free(model
);
8607 isl_dim_free(model
);
8612 __isl_give isl_set
*isl_set_align_params(__isl_take isl_set
*set
,
8613 __isl_take isl_dim
*model
)
8615 return isl_map_align_params(set
, model
);
8618 __isl_give isl_mat
*isl_basic_map_equalities_matrix(
8619 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
8620 enum isl_dim_type c2
, enum isl_dim_type c3
,
8621 enum isl_dim_type c4
, enum isl_dim_type c5
)
8623 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
8624 struct isl_mat
*mat
;
8630 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_eq
,
8631 isl_basic_map_total_dim(bmap
) + 1);
8634 for (i
= 0; i
< bmap
->n_eq
; ++i
)
8635 for (j
= 0, pos
= 0; j
< 5; ++j
) {
8636 int off
= isl_basic_map_offset(bmap
, c
[j
]);
8637 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
8638 isl_int_set(mat
->row
[i
][pos
],
8639 bmap
->eq
[i
][off
+ k
]);
8647 __isl_give isl_mat
*isl_basic_map_inequalities_matrix(
8648 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
8649 enum isl_dim_type c2
, enum isl_dim_type c3
,
8650 enum isl_dim_type c4
, enum isl_dim_type c5
)
8652 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
8653 struct isl_mat
*mat
;
8659 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_ineq
,
8660 isl_basic_map_total_dim(bmap
) + 1);
8663 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
8664 for (j
= 0, pos
= 0; j
< 5; ++j
) {
8665 int off
= isl_basic_map_offset(bmap
, c
[j
]);
8666 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
8667 isl_int_set(mat
->row
[i
][pos
],
8668 bmap
->ineq
[i
][off
+ k
]);
8676 __isl_give isl_basic_map
*isl_basic_map_from_constraint_matrices(
8677 __isl_take isl_dim
*dim
,
8678 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
8679 enum isl_dim_type c2
, enum isl_dim_type c3
,
8680 enum isl_dim_type c4
, enum isl_dim_type c5
)
8682 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
8683 isl_basic_map
*bmap
;
8689 if (!dim
|| !eq
|| !ineq
)
8692 if (eq
->n_col
!= ineq
->n_col
)
8693 isl_die(dim
->ctx
, isl_error_invalid
,
8694 "equalities and inequalities matrices should have "
8695 "same number of columns", goto error
);
8697 total
= 1 + isl_dim_total(dim
);
8699 if (eq
->n_col
< total
)
8700 isl_die(dim
->ctx
, isl_error_invalid
,
8701 "number of columns too small", goto error
);
8703 extra
= eq
->n_col
- total
;
8705 bmap
= isl_basic_map_alloc_dim(isl_dim_copy(dim
), extra
,
8706 eq
->n_row
, ineq
->n_row
);
8709 for (i
= 0; i
< extra
; ++i
) {
8710 k
= isl_basic_map_alloc_div(bmap
);
8713 isl_int_set_si(bmap
->div
[k
][0], 0);
8715 for (i
= 0; i
< eq
->n_row
; ++i
) {
8716 l
= isl_basic_map_alloc_equality(bmap
);
8719 for (j
= 0, pos
= 0; j
< 5; ++j
) {
8720 int off
= isl_basic_map_offset(bmap
, c
[j
]);
8721 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
8722 isl_int_set(bmap
->eq
[l
][off
+ k
],
8728 for (i
= 0; i
< ineq
->n_row
; ++i
) {
8729 l
= isl_basic_map_alloc_inequality(bmap
);
8732 for (j
= 0, pos
= 0; j
< 5; ++j
) {
8733 int off
= isl_basic_map_offset(bmap
, c
[j
]);
8734 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
8735 isl_int_set(bmap
->ineq
[l
][off
+ k
],
8754 __isl_give isl_mat
*isl_basic_set_equalities_matrix(
8755 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
8756 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
8758 return isl_basic_map_equalities_matrix((isl_basic_map
*)bset
,
8759 c1
, c2
, c3
, c4
, isl_dim_in
);
8762 __isl_give isl_mat
*isl_basic_set_inequalities_matrix(
8763 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
8764 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
8766 return isl_basic_map_inequalities_matrix((isl_basic_map
*)bset
,
8767 c1
, c2
, c3
, c4
, isl_dim_in
);
8770 __isl_give isl_basic_set
*isl_basic_set_from_constraint_matrices(
8771 __isl_take isl_dim
*dim
,
8772 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
8773 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
8775 return (isl_basic_set
*)
8776 isl_basic_map_from_constraint_matrices(dim
, eq
, ineq
,
8777 c1
, c2
, c3
, c4
, isl_dim_in
);
8780 int isl_basic_map_can_zip(__isl_keep isl_basic_map
*bmap
)
8785 return isl_dim_can_zip(bmap
->dim
);
8788 int isl_map_can_zip(__isl_keep isl_map
*map
)
8793 return isl_dim_can_zip(map
->dim
);
8796 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
8797 * (A -> C) -> (B -> D).
8799 __isl_give isl_basic_map
*isl_basic_map_zip(__isl_take isl_basic_map
*bmap
)
8808 if (!isl_basic_map_can_zip(bmap
))
8809 isl_die(bmap
->ctx
, isl_error_invalid
,
8810 "basic map cannot be zipped", goto error
);
8811 pos
= isl_basic_map_offset(bmap
, isl_dim_in
) +
8812 isl_dim_size(bmap
->dim
->nested
[0], isl_dim_in
);
8813 n1
= isl_dim_size(bmap
->dim
->nested
[0], isl_dim_out
);
8814 n2
= isl_dim_size(bmap
->dim
->nested
[1], isl_dim_in
);
8815 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
8818 bmap
->dim
= isl_dim_zip(bmap
->dim
);
8823 isl_basic_map_free(bmap
);
8827 /* Given a map (A -> B) -> (C -> D), return the corresponding map
8828 * (A -> C) -> (B -> D).
8830 __isl_give isl_map
*isl_map_zip(__isl_take isl_map
*map
)
8837 if (!isl_map_can_zip(map
))
8838 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be zipped",
8841 map
= isl_map_cow(map
);
8845 for (i
= 0; i
< map
->n
; ++i
) {
8846 map
->p
[i
] = isl_basic_map_zip(map
->p
[i
]);
8851 map
->dim
= isl_dim_zip(map
->dim
);
8861 /* Construct a basic map mapping the domain of the affine expression
8862 * to a one-dimensional range prescribed by the affine expression.
8864 __isl_give isl_basic_map
*isl_basic_map_from_aff(__isl_take isl_aff
*aff
)
8868 isl_local_space
*ls
;
8869 isl_basic_map
*bmap
;
8874 ls
= isl_aff_get_local_space(aff
);
8875 ls
= isl_local_space_from_domain(ls
);
8876 ls
= isl_local_space_add_dim(ls
, isl_dim_out
, 1);
8877 bmap
= isl_basic_map_from_local_space(ls
);
8878 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
8879 k
= isl_basic_map_alloc_equality(bmap
);
8883 pos
= isl_basic_map_offset(bmap
, isl_dim_out
);
8884 isl_seq_cpy(bmap
->eq
[k
], aff
->v
->el
+ 1, pos
);
8885 isl_int_neg(bmap
->eq
[k
][pos
], aff
->v
->el
[0]);
8886 isl_seq_cpy(bmap
->eq
[k
] + pos
+ 1, aff
->v
->el
+ 1 + pos
,
8887 aff
->v
->size
- (pos
+ 1));
8890 bmap
= isl_basic_map_finalize(bmap
);
8894 isl_basic_map_free(bmap
);
8898 /* Construct a basic map mapping a domain in the given space to
8899 * to an n-dimensional range, with n the number of elements in the list,
8900 * where each coordinate in the range is prescribed by the
8901 * corresponding affine expression.
8902 * The domains of all affine expressions in the list are assumed to match
8905 __isl_give isl_basic_map
*isl_basic_map_from_aff_list(
8906 __isl_take isl_dim
*domain_dim
, __isl_take isl_aff_list
*list
)
8910 isl_basic_map
*bmap
;
8915 dim
= isl_dim_from_domain(domain_dim
);
8916 bmap
= isl_basic_map_universe(dim
);
8918 for (i
= 0; i
< list
->n
; ++i
) {
8920 isl_basic_map
*bmap_i
;
8922 aff
= isl_aff_copy(list
->p
[i
]);
8923 bmap_i
= isl_basic_map_from_aff(aff
);
8925 bmap
= isl_basic_map_flat_range_product(bmap
, bmap_i
);
8928 isl_aff_list_free(list
);