2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
5 * Use of this software is governed by the GNU LGPLv2.1 license
7 * Written by Sven Verdoolaege, K.U.Leuven, Departement
8 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
9 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
10 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
15 #include <isl_ctx_private.h>
16 #include <isl_map_private.h>
18 #include "isl_dim_private.h"
19 #include "isl_equalities.h"
25 #include "isl_map_piplib.h"
26 #include <isl_reordering.h>
27 #include "isl_sample.h"
30 #include <isl_mat_private.h>
31 #include <isl_dim_map.h>
33 static unsigned n(struct isl_dim
*dim
, enum isl_dim_type type
)
36 case isl_dim_param
: return dim
->nparam
;
37 case isl_dim_in
: return dim
->n_in
;
38 case isl_dim_out
: return dim
->n_out
;
39 case isl_dim_all
: return dim
->nparam
+ dim
->n_in
+ dim
->n_out
;
44 static unsigned pos(struct isl_dim
*dim
, enum isl_dim_type type
)
47 case isl_dim_param
: return 1;
48 case isl_dim_in
: return 1 + dim
->nparam
;
49 case isl_dim_out
: return 1 + dim
->nparam
+ dim
->n_in
;
54 unsigned isl_basic_map_dim(const struct isl_basic_map
*bmap
,
55 enum isl_dim_type type
)
60 case isl_dim_cst
: return 1;
63 case isl_dim_out
: return isl_dim_size(bmap
->dim
, type
);
64 case isl_dim_div
: return bmap
->n_div
;
65 case isl_dim_all
: return isl_basic_map_total_dim(bmap
);
70 unsigned isl_map_dim(const struct isl_map
*map
, enum isl_dim_type type
)
72 return map
? n(map
->dim
, type
) : 0;
75 unsigned isl_set_dim(const struct isl_set
*set
, enum isl_dim_type type
)
77 return set
? n(set
->dim
, type
) : 0;
80 unsigned isl_basic_map_offset(struct isl_basic_map
*bmap
,
81 enum isl_dim_type type
)
83 struct isl_dim
*dim
= bmap
->dim
;
85 case isl_dim_cst
: return 0;
86 case isl_dim_param
: return 1;
87 case isl_dim_in
: return 1 + dim
->nparam
;
88 case isl_dim_out
: return 1 + dim
->nparam
+ dim
->n_in
;
89 case isl_dim_div
: return 1 + dim
->nparam
+ dim
->n_in
+ dim
->n_out
;
94 unsigned isl_basic_set_offset(struct isl_basic_set
*bset
,
95 enum isl_dim_type type
)
97 return isl_basic_map_offset(bset
, type
);
100 static unsigned map_offset(struct isl_map
*map
, enum isl_dim_type type
)
102 return pos(map
->dim
, type
);
105 unsigned isl_basic_set_dim(const struct isl_basic_set
*bset
,
106 enum isl_dim_type type
)
108 return isl_basic_map_dim((const struct isl_basic_map
*)bset
, type
);
111 unsigned isl_basic_set_n_dim(const struct isl_basic_set
*bset
)
113 return isl_basic_set_dim(bset
, isl_dim_set
);
116 unsigned isl_basic_set_n_param(const struct isl_basic_set
*bset
)
118 return isl_basic_set_dim(bset
, isl_dim_param
);
121 unsigned isl_basic_set_total_dim(const struct isl_basic_set
*bset
)
123 return isl_dim_total(bset
->dim
) + bset
->n_div
;
126 unsigned isl_set_n_dim(const struct isl_set
*set
)
128 return isl_set_dim(set
, isl_dim_set
);
131 unsigned isl_set_n_param(const struct isl_set
*set
)
133 return isl_set_dim(set
, isl_dim_param
);
136 unsigned isl_basic_map_n_in(const struct isl_basic_map
*bmap
)
138 return bmap
? bmap
->dim
->n_in
: 0;
141 unsigned isl_basic_map_n_out(const struct isl_basic_map
*bmap
)
143 return bmap
? bmap
->dim
->n_out
: 0;
146 unsigned isl_basic_map_n_param(const struct isl_basic_map
*bmap
)
148 return bmap
? bmap
->dim
->nparam
: 0;
151 unsigned isl_basic_map_n_div(const struct isl_basic_map
*bmap
)
153 return bmap
? bmap
->n_div
: 0;
156 unsigned isl_basic_map_total_dim(const struct isl_basic_map
*bmap
)
158 return bmap
? isl_dim_total(bmap
->dim
) + bmap
->n_div
: 0;
161 unsigned isl_map_n_in(const struct isl_map
*map
)
163 return map
->dim
->n_in
;
166 unsigned isl_map_n_out(const struct isl_map
*map
)
168 return map
->dim
->n_out
;
171 unsigned isl_map_n_param(const struct isl_map
*map
)
173 return map
->dim
->nparam
;
176 int isl_map_compatible_domain(struct isl_map
*map
, struct isl_set
*set
)
181 m
= isl_dim_match(map
->dim
, isl_dim_param
, set
->dim
, isl_dim_param
);
184 return isl_dim_tuple_match(map
->dim
, isl_dim_in
, set
->dim
, isl_dim_set
);
187 int isl_basic_map_compatible_domain(struct isl_basic_map
*bmap
,
188 struct isl_basic_set
*bset
)
193 m
= isl_dim_match(bmap
->dim
, isl_dim_param
, bset
->dim
, isl_dim_param
);
196 return isl_dim_tuple_match(bmap
->dim
, isl_dim_in
, bset
->dim
, isl_dim_set
);
199 int isl_basic_map_compatible_range(struct isl_basic_map
*bmap
,
200 struct isl_basic_set
*bset
)
205 m
= isl_dim_match(bmap
->dim
, isl_dim_param
, bset
->dim
, isl_dim_param
);
208 return isl_dim_tuple_match(bmap
->dim
, isl_dim_out
, bset
->dim
, isl_dim_set
);
211 isl_ctx
*isl_basic_map_get_ctx(__isl_keep isl_basic_map
*bmap
)
213 return bmap
? bmap
->ctx
: NULL
;
216 isl_ctx
*isl_basic_set_get_ctx(__isl_keep isl_basic_set
*bset
)
218 return bset
? bset
->ctx
: NULL
;
221 isl_ctx
*isl_map_get_ctx(__isl_keep isl_map
*map
)
223 return map
? map
->ctx
: NULL
;
226 isl_ctx
*isl_set_get_ctx(__isl_keep isl_set
*set
)
228 return set
? set
->ctx
: NULL
;
231 struct isl_dim
*isl_basic_map_get_dim(struct isl_basic_map
*bmap
)
235 return isl_dim_copy(bmap
->dim
);
238 struct isl_dim
*isl_basic_set_get_dim(struct isl_basic_set
*bset
)
242 return isl_dim_copy(bset
->dim
);
245 struct isl_dim
*isl_map_get_dim(struct isl_map
*map
)
249 return isl_dim_copy(map
->dim
);
252 struct isl_dim
*isl_set_get_dim(struct isl_set
*set
)
256 return isl_dim_copy(set
->dim
);
259 __isl_give isl_basic_map
*isl_basic_map_set_tuple_name(
260 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
, const char *s
)
262 bmap
= isl_basic_map_cow(bmap
);
265 bmap
->dim
= isl_dim_set_tuple_name(bmap
->dim
, type
, s
);
268 bmap
= isl_basic_map_finalize(bmap
);
271 isl_basic_map_free(bmap
);
275 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map
*bmap
,
276 enum isl_dim_type type
)
278 return bmap
? isl_dim_get_tuple_name(bmap
->dim
, type
) : NULL
;
281 __isl_give isl_map
*isl_map_set_tuple_name(__isl_take isl_map
*map
,
282 enum isl_dim_type type
, const char *s
)
286 map
= isl_map_cow(map
);
290 map
->dim
= isl_dim_set_tuple_name(map
->dim
, type
, s
);
294 for (i
= 0; i
< map
->n
; ++i
) {
295 map
->p
[i
] = isl_basic_map_set_tuple_name(map
->p
[i
], type
, s
);
306 const char *isl_map_get_tuple_name(__isl_keep isl_map
*map
,
307 enum isl_dim_type type
)
309 return map
? isl_dim_get_tuple_name(map
->dim
, type
) : NULL
;
312 __isl_give isl_set
*isl_set_set_tuple_name(__isl_take isl_set
*set
,
315 return (isl_set
*)isl_map_set_tuple_name((isl_map
*)set
, isl_dim_set
, s
);
318 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set
*bset
)
320 return bset
? isl_dim_get_tuple_name(bset
->dim
, isl_dim_set
) : NULL
;
323 const char *isl_set_get_tuple_name(__isl_keep isl_set
*set
)
325 return set
? isl_dim_get_tuple_name(set
->dim
, isl_dim_set
) : NULL
;
328 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map
*bmap
,
329 enum isl_dim_type type
, unsigned pos
)
331 return bmap
? isl_dim_get_name(bmap
->dim
, type
, pos
) : NULL
;
334 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set
*bset
,
335 enum isl_dim_type type
, unsigned pos
)
337 return bset
? isl_dim_get_name(bset
->dim
, type
, pos
) : NULL
;
340 const char *isl_map_get_dim_name(__isl_keep isl_map
*map
,
341 enum isl_dim_type type
, unsigned pos
)
343 return map
? isl_dim_get_name(map
->dim
, type
, pos
) : NULL
;
346 const char *isl_set_get_dim_name(__isl_keep isl_set
*set
,
347 enum isl_dim_type type
, unsigned pos
)
349 return set
? isl_dim_get_name(set
->dim
, type
, pos
) : NULL
;
352 __isl_give isl_basic_map
*isl_basic_map_set_dim_name(
353 __isl_take isl_basic_map
*bmap
,
354 enum isl_dim_type type
, unsigned pos
, const char *s
)
358 bmap
->dim
= isl_dim_set_name(bmap
->dim
, type
, pos
, s
);
363 isl_basic_map_free(bmap
);
367 __isl_give isl_map
*isl_map_set_dim_name(__isl_take isl_map
*map
,
368 enum isl_dim_type type
, unsigned pos
, const char *s
)
375 map
->dim
= isl_dim_set_name(map
->dim
, type
, pos
, s
);
379 for (i
= 0; i
< map
->n
; ++i
) {
380 map
->p
[i
] = isl_basic_map_set_dim_name(map
->p
[i
], type
, pos
, s
);
391 __isl_give isl_basic_set
*isl_basic_set_set_dim_name(
392 __isl_take isl_basic_set
*bset
,
393 enum isl_dim_type type
, unsigned pos
, const char *s
)
395 return (isl_basic_set
*)isl_basic_map_set_dim_name(
396 (isl_basic_map
*)bset
, type
, pos
, s
);
399 __isl_give isl_set
*isl_set_set_dim_name(__isl_take isl_set
*set
,
400 enum isl_dim_type type
, unsigned pos
, const char *s
)
402 return (isl_set
*)isl_map_set_dim_name((isl_map
*)set
, type
, pos
, s
);
405 int isl_basic_map_is_rational(__isl_keep isl_basic_map
*bmap
)
409 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
412 int isl_basic_set_is_rational(__isl_keep isl_basic_set
*bset
)
414 return isl_basic_map_is_rational(bset
);
417 static struct isl_basic_map
*basic_map_init(struct isl_ctx
*ctx
,
418 struct isl_basic_map
*bmap
, unsigned extra
,
419 unsigned n_eq
, unsigned n_ineq
)
422 size_t row_size
= 1 + isl_dim_total(bmap
->dim
) + extra
;
427 bmap
->block
= isl_blk_alloc(ctx
, (n_ineq
+ n_eq
) * row_size
);
428 if (isl_blk_is_error(bmap
->block
))
431 bmap
->ineq
= isl_alloc_array(ctx
, isl_int
*, n_ineq
+ n_eq
);
436 bmap
->block2
= isl_blk_empty();
439 bmap
->block2
= isl_blk_alloc(ctx
, extra
* (1 + row_size
));
440 if (isl_blk_is_error(bmap
->block2
))
443 bmap
->div
= isl_alloc_array(ctx
, isl_int
*, extra
);
448 for (i
= 0; i
< n_ineq
+ n_eq
; ++i
)
449 bmap
->ineq
[i
] = bmap
->block
.data
+ i
* row_size
;
451 for (i
= 0; i
< extra
; ++i
)
452 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + row_size
);
456 bmap
->c_size
= n_eq
+ n_ineq
;
457 bmap
->eq
= bmap
->ineq
+ n_ineq
;
466 isl_basic_map_free(bmap
);
470 struct isl_basic_set
*isl_basic_set_alloc(struct isl_ctx
*ctx
,
471 unsigned nparam
, unsigned dim
, unsigned extra
,
472 unsigned n_eq
, unsigned n_ineq
)
474 struct isl_basic_map
*bmap
;
475 bmap
= isl_basic_map_alloc(ctx
, nparam
, 0, dim
, extra
, n_eq
, n_ineq
);
476 return (struct isl_basic_set
*)bmap
;
479 struct isl_basic_set
*isl_basic_set_alloc_dim(struct isl_dim
*dim
,
480 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
482 struct isl_basic_map
*bmap
;
485 isl_assert(dim
->ctx
, dim
->n_in
== 0, goto error
);
486 bmap
= isl_basic_map_alloc_dim(dim
, extra
, n_eq
, n_ineq
);
487 return (struct isl_basic_set
*)bmap
;
493 struct isl_basic_map
*isl_basic_map_alloc_dim(struct isl_dim
*dim
,
494 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
496 struct isl_basic_map
*bmap
;
500 bmap
= isl_calloc_type(dim
->ctx
, struct isl_basic_map
);
505 return basic_map_init(dim
->ctx
, bmap
, extra
, n_eq
, n_ineq
);
511 struct isl_basic_map
*isl_basic_map_alloc(struct isl_ctx
*ctx
,
512 unsigned nparam
, unsigned in
, unsigned out
, unsigned extra
,
513 unsigned n_eq
, unsigned n_ineq
)
515 struct isl_basic_map
*bmap
;
518 dim
= isl_dim_alloc(ctx
, nparam
, in
, out
);
522 bmap
= isl_basic_map_alloc_dim(dim
, extra
, n_eq
, n_ineq
);
526 static void dup_constraints(
527 struct isl_basic_map
*dst
, struct isl_basic_map
*src
)
530 unsigned total
= isl_basic_map_total_dim(src
);
532 for (i
= 0; i
< src
->n_eq
; ++i
) {
533 int j
= isl_basic_map_alloc_equality(dst
);
534 isl_seq_cpy(dst
->eq
[j
], src
->eq
[i
], 1+total
);
537 for (i
= 0; i
< src
->n_ineq
; ++i
) {
538 int j
= isl_basic_map_alloc_inequality(dst
);
539 isl_seq_cpy(dst
->ineq
[j
], src
->ineq
[i
], 1+total
);
542 for (i
= 0; i
< src
->n_div
; ++i
) {
543 int j
= isl_basic_map_alloc_div(dst
);
544 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
);
546 ISL_F_SET(dst
, ISL_BASIC_SET_FINAL
);
549 struct isl_basic_map
*isl_basic_map_dup(struct isl_basic_map
*bmap
)
551 struct isl_basic_map
*dup
;
555 dup
= isl_basic_map_alloc_dim(isl_dim_copy(bmap
->dim
),
556 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
559 dup_constraints(dup
, bmap
);
560 dup
->flags
= bmap
->flags
;
561 dup
->sample
= isl_vec_copy(bmap
->sample
);
565 struct isl_basic_set
*isl_basic_set_dup(struct isl_basic_set
*bset
)
567 struct isl_basic_map
*dup
;
569 dup
= isl_basic_map_dup((struct isl_basic_map
*)bset
);
570 return (struct isl_basic_set
*)dup
;
573 struct isl_basic_set
*isl_basic_set_copy(struct isl_basic_set
*bset
)
578 if (ISL_F_ISSET(bset
, ISL_BASIC_SET_FINAL
)) {
582 return isl_basic_set_dup(bset
);
585 struct isl_set
*isl_set_copy(struct isl_set
*set
)
594 struct isl_basic_map
*isl_basic_map_copy(struct isl_basic_map
*bmap
)
599 if (ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
)) {
603 bmap
= isl_basic_map_dup(bmap
);
605 ISL_F_SET(bmap
, ISL_BASIC_SET_FINAL
);
609 struct isl_map
*isl_map_copy(struct isl_map
*map
)
618 void isl_basic_map_free(struct isl_basic_map
*bmap
)
626 isl_ctx_deref(bmap
->ctx
);
628 isl_blk_free(bmap
->ctx
, bmap
->block2
);
630 isl_blk_free(bmap
->ctx
, bmap
->block
);
631 isl_vec_free(bmap
->sample
);
632 isl_dim_free(bmap
->dim
);
636 void isl_basic_set_free(struct isl_basic_set
*bset
)
638 isl_basic_map_free((struct isl_basic_map
*)bset
);
641 static int room_for_con(struct isl_basic_map
*bmap
, unsigned n
)
643 return bmap
->n_eq
+ bmap
->n_ineq
+ n
<= bmap
->c_size
;
646 int isl_basic_map_alloc_equality(struct isl_basic_map
*bmap
)
652 isl_assert(ctx
, room_for_con(bmap
, 1), return -1);
653 isl_assert(ctx
, (bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
<= bmap
->c_size
,
655 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
656 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
657 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
658 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
659 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
660 if ((bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
== bmap
->c_size
) {
662 int j
= isl_basic_map_alloc_inequality(bmap
);
666 bmap
->ineq
[j
] = bmap
->ineq
[bmap
->n_ineq
- 1];
667 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
674 isl_seq_clr(bmap
->eq
[bmap
->n_eq
] + 1 + isl_basic_map_total_dim(bmap
),
675 bmap
->extra
- bmap
->n_div
);
679 int isl_basic_set_alloc_equality(struct isl_basic_set
*bset
)
681 return isl_basic_map_alloc_equality((struct isl_basic_map
*)bset
);
684 int isl_basic_map_free_equality(struct isl_basic_map
*bmap
, unsigned n
)
688 isl_assert(bmap
->ctx
, n
<= bmap
->n_eq
, return -1);
693 int isl_basic_set_free_equality(struct isl_basic_set
*bset
, unsigned n
)
695 return isl_basic_map_free_equality((struct isl_basic_map
*)bset
, n
);
698 int isl_basic_map_drop_equality(struct isl_basic_map
*bmap
, unsigned pos
)
703 isl_assert(bmap
->ctx
, pos
< bmap
->n_eq
, return -1);
705 if (pos
!= bmap
->n_eq
- 1) {
707 bmap
->eq
[pos
] = bmap
->eq
[bmap
->n_eq
- 1];
708 bmap
->eq
[bmap
->n_eq
- 1] = t
;
714 int isl_basic_set_drop_equality(struct isl_basic_set
*bset
, unsigned pos
)
716 return isl_basic_map_drop_equality((struct isl_basic_map
*)bset
, pos
);
719 void isl_basic_map_inequality_to_equality(
720 struct isl_basic_map
*bmap
, unsigned pos
)
725 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
726 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
731 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
732 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
733 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
734 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
737 static int room_for_ineq(struct isl_basic_map
*bmap
, unsigned n
)
739 return bmap
->n_ineq
+ n
<= bmap
->eq
- bmap
->ineq
;
742 int isl_basic_map_alloc_inequality(struct isl_basic_map
*bmap
)
748 isl_assert(ctx
, room_for_ineq(bmap
, 1), return -1);
749 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
750 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
751 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
752 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
753 isl_seq_clr(bmap
->ineq
[bmap
->n_ineq
] +
754 1 + isl_basic_map_total_dim(bmap
),
755 bmap
->extra
- bmap
->n_div
);
756 return bmap
->n_ineq
++;
759 int isl_basic_set_alloc_inequality(struct isl_basic_set
*bset
)
761 return isl_basic_map_alloc_inequality((struct isl_basic_map
*)bset
);
764 int isl_basic_map_free_inequality(struct isl_basic_map
*bmap
, unsigned n
)
768 isl_assert(bmap
->ctx
, n
<= bmap
->n_ineq
, return -1);
773 int isl_basic_set_free_inequality(struct isl_basic_set
*bset
, unsigned n
)
775 return isl_basic_map_free_inequality((struct isl_basic_map
*)bset
, n
);
778 int isl_basic_map_drop_inequality(struct isl_basic_map
*bmap
, unsigned pos
)
783 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
785 if (pos
!= bmap
->n_ineq
- 1) {
787 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
788 bmap
->ineq
[bmap
->n_ineq
- 1] = t
;
789 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
795 int isl_basic_set_drop_inequality(struct isl_basic_set
*bset
, unsigned pos
)
797 return isl_basic_map_drop_inequality((struct isl_basic_map
*)bset
, pos
);
800 __isl_give isl_basic_map
*isl_basic_map_add_eq(__isl_take isl_basic_map
*bmap
,
805 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
808 k
= isl_basic_map_alloc_equality(bmap
);
811 isl_seq_cpy(bmap
->eq
[k
], eq
, 1 + isl_basic_map_total_dim(bmap
));
814 isl_basic_map_free(bmap
);
818 __isl_give isl_basic_set
*isl_basic_set_add_eq(__isl_take isl_basic_set
*bset
,
821 return (isl_basic_set
*)
822 isl_basic_map_add_eq((isl_basic_map
*)bset
, eq
);
825 __isl_give isl_basic_map
*isl_basic_map_add_ineq(__isl_take isl_basic_map
*bmap
,
830 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
833 k
= isl_basic_map_alloc_inequality(bmap
);
836 isl_seq_cpy(bmap
->ineq
[k
], ineq
, 1 + isl_basic_map_total_dim(bmap
));
839 isl_basic_map_free(bmap
);
843 __isl_give isl_basic_set
*isl_basic_set_add_ineq(__isl_take isl_basic_set
*bset
,
846 return (isl_basic_set
*)
847 isl_basic_map_add_ineq((isl_basic_map
*)bset
, ineq
);
850 int isl_basic_map_alloc_div(struct isl_basic_map
*bmap
)
854 isl_assert(bmap
->ctx
, bmap
->n_div
< bmap
->extra
, return -1);
855 isl_seq_clr(bmap
->div
[bmap
->n_div
] +
856 1 + 1 + isl_basic_map_total_dim(bmap
),
857 bmap
->extra
- bmap
->n_div
);
858 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
859 return bmap
->n_div
++;
862 int isl_basic_set_alloc_div(struct isl_basic_set
*bset
)
864 return isl_basic_map_alloc_div((struct isl_basic_map
*)bset
);
867 int isl_basic_map_free_div(struct isl_basic_map
*bmap
, unsigned n
)
871 isl_assert(bmap
->ctx
, n
<= bmap
->n_div
, return -1);
876 int isl_basic_set_free_div(struct isl_basic_set
*bset
, unsigned n
)
878 return isl_basic_map_free_div((struct isl_basic_map
*)bset
, n
);
881 /* Copy constraint from src to dst, putting the vars of src at offset
882 * dim_off in dst and the divs of src at offset div_off in dst.
883 * If both sets are actually map, then dim_off applies to the input
886 static void copy_constraint(struct isl_basic_map
*dst_map
, isl_int
*dst
,
887 struct isl_basic_map
*src_map
, isl_int
*src
,
888 unsigned in_off
, unsigned out_off
, unsigned div_off
)
890 unsigned src_nparam
= isl_basic_map_n_param(src_map
);
891 unsigned dst_nparam
= isl_basic_map_n_param(dst_map
);
892 unsigned src_in
= isl_basic_map_n_in(src_map
);
893 unsigned dst_in
= isl_basic_map_n_in(dst_map
);
894 unsigned src_out
= isl_basic_map_n_out(src_map
);
895 unsigned dst_out
= isl_basic_map_n_out(dst_map
);
896 isl_int_set(dst
[0], src
[0]);
897 isl_seq_cpy(dst
+1, src
+1, isl_min(dst_nparam
, src_nparam
));
898 if (dst_nparam
> src_nparam
)
899 isl_seq_clr(dst
+1+src_nparam
,
900 dst_nparam
- src_nparam
);
901 isl_seq_clr(dst
+1+dst_nparam
, in_off
);
902 isl_seq_cpy(dst
+1+dst_nparam
+in_off
,
904 isl_min(dst_in
-in_off
, src_in
));
905 if (dst_in
-in_off
> src_in
)
906 isl_seq_clr(dst
+1+dst_nparam
+in_off
+src_in
,
907 dst_in
- in_off
- src_in
);
908 isl_seq_clr(dst
+1+dst_nparam
+dst_in
, out_off
);
909 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+out_off
,
910 src
+1+src_nparam
+src_in
,
911 isl_min(dst_out
-out_off
, src_out
));
912 if (dst_out
-out_off
> src_out
)
913 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+out_off
+src_out
,
914 dst_out
- out_off
- src_out
);
915 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
, div_off
);
916 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+dst_out
+div_off
,
917 src
+1+src_nparam
+src_in
+src_out
,
918 isl_min(dst_map
->extra
-div_off
, src_map
->n_div
));
919 if (dst_map
->n_div
-div_off
> src_map
->n_div
)
920 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
+
921 div_off
+src_map
->n_div
,
922 dst_map
->n_div
- div_off
- src_map
->n_div
);
925 static void copy_div(struct isl_basic_map
*dst_map
, isl_int
*dst
,
926 struct isl_basic_map
*src_map
, isl_int
*src
,
927 unsigned in_off
, unsigned out_off
, unsigned div_off
)
929 isl_int_set(dst
[0], src
[0]);
930 copy_constraint(dst_map
, dst
+1, src_map
, src
+1, in_off
, out_off
, div_off
);
933 static struct isl_basic_map
*add_constraints(struct isl_basic_map
*bmap1
,
934 struct isl_basic_map
*bmap2
, unsigned i_pos
, unsigned o_pos
)
939 if (!bmap1
|| !bmap2
)
942 div_off
= bmap1
->n_div
;
944 for (i
= 0; i
< bmap2
->n_eq
; ++i
) {
945 int i1
= isl_basic_map_alloc_equality(bmap1
);
948 copy_constraint(bmap1
, bmap1
->eq
[i1
], bmap2
, bmap2
->eq
[i
],
949 i_pos
, o_pos
, div_off
);
952 for (i
= 0; i
< bmap2
->n_ineq
; ++i
) {
953 int i1
= isl_basic_map_alloc_inequality(bmap1
);
956 copy_constraint(bmap1
, bmap1
->ineq
[i1
], bmap2
, bmap2
->ineq
[i
],
957 i_pos
, o_pos
, div_off
);
960 for (i
= 0; i
< bmap2
->n_div
; ++i
) {
961 int i1
= isl_basic_map_alloc_div(bmap1
);
964 copy_div(bmap1
, bmap1
->div
[i1
], bmap2
, bmap2
->div
[i
],
965 i_pos
, o_pos
, div_off
);
968 isl_basic_map_free(bmap2
);
973 isl_basic_map_free(bmap1
);
974 isl_basic_map_free(bmap2
);
978 struct isl_basic_set
*isl_basic_set_add_constraints(struct isl_basic_set
*bset1
,
979 struct isl_basic_set
*bset2
, unsigned pos
)
981 return (struct isl_basic_set
*)
982 add_constraints((struct isl_basic_map
*)bset1
,
983 (struct isl_basic_map
*)bset2
, 0, pos
);
986 struct isl_basic_map
*isl_basic_map_extend_dim(struct isl_basic_map
*base
,
987 struct isl_dim
*dim
, unsigned extra
,
988 unsigned n_eq
, unsigned n_ineq
)
990 struct isl_basic_map
*ext
;
1000 dims_ok
= isl_dim_equal(base
->dim
, dim
) &&
1001 base
->extra
>= base
->n_div
+ extra
;
1003 if (dims_ok
&& room_for_con(base
, n_eq
+ n_ineq
) &&
1004 room_for_ineq(base
, n_ineq
)) {
1009 isl_assert(base
->ctx
, base
->dim
->nparam
<= dim
->nparam
, goto error
);
1010 isl_assert(base
->ctx
, base
->dim
->n_in
<= dim
->n_in
, goto error
);
1011 isl_assert(base
->ctx
, base
->dim
->n_out
<= dim
->n_out
, goto error
);
1012 extra
+= base
->extra
;
1014 n_ineq
+= base
->n_ineq
;
1016 ext
= isl_basic_map_alloc_dim(dim
, extra
, n_eq
, n_ineq
);
1022 ext
->sample
= isl_vec_copy(base
->sample
);
1023 flags
= base
->flags
;
1024 ext
= add_constraints(ext
, base
, 0, 0);
1027 ISL_F_CLR(ext
, ISL_BASIC_SET_FINAL
);
1034 isl_basic_map_free(base
);
1038 struct isl_basic_set
*isl_basic_set_extend_dim(struct isl_basic_set
*base
,
1039 struct isl_dim
*dim
, unsigned extra
,
1040 unsigned n_eq
, unsigned n_ineq
)
1042 return (struct isl_basic_set
*)
1043 isl_basic_map_extend_dim((struct isl_basic_map
*)base
, dim
,
1044 extra
, n_eq
, n_ineq
);
1047 struct isl_basic_map
*isl_basic_map_extend_constraints(
1048 struct isl_basic_map
*base
, unsigned n_eq
, unsigned n_ineq
)
1052 return isl_basic_map_extend_dim(base
, isl_dim_copy(base
->dim
),
1056 struct isl_basic_map
*isl_basic_map_extend(struct isl_basic_map
*base
,
1057 unsigned nparam
, unsigned n_in
, unsigned n_out
, unsigned extra
,
1058 unsigned n_eq
, unsigned n_ineq
)
1060 struct isl_basic_map
*bmap
;
1061 struct isl_dim
*dim
;
1065 dim
= isl_dim_alloc(base
->ctx
, nparam
, n_in
, n_out
);
1069 bmap
= isl_basic_map_extend_dim(base
, dim
, extra
, n_eq
, n_ineq
);
1072 isl_basic_map_free(base
);
1076 struct isl_basic_set
*isl_basic_set_extend(struct isl_basic_set
*base
,
1077 unsigned nparam
, unsigned dim
, unsigned extra
,
1078 unsigned n_eq
, unsigned n_ineq
)
1080 return (struct isl_basic_set
*)
1081 isl_basic_map_extend((struct isl_basic_map
*)base
,
1082 nparam
, 0, dim
, extra
, n_eq
, n_ineq
);
1085 struct isl_basic_set
*isl_basic_set_extend_constraints(
1086 struct isl_basic_set
*base
, unsigned n_eq
, unsigned n_ineq
)
1088 return (struct isl_basic_set
*)
1089 isl_basic_map_extend_constraints((struct isl_basic_map
*)base
,
1093 struct isl_basic_set
*isl_basic_set_cow(struct isl_basic_set
*bset
)
1095 return (struct isl_basic_set
*)
1096 isl_basic_map_cow((struct isl_basic_map
*)bset
);
1099 struct isl_basic_map
*isl_basic_map_cow(struct isl_basic_map
*bmap
)
1104 if (bmap
->ref
> 1) {
1106 bmap
= isl_basic_map_dup(bmap
);
1109 ISL_F_CLR(bmap
, ISL_BASIC_SET_FINAL
);
1113 struct isl_set
*isl_set_cow(struct isl_set
*set
)
1121 return isl_set_dup(set
);
1124 struct isl_map
*isl_map_cow(struct isl_map
*map
)
1132 return isl_map_dup(map
);
1135 static void swap_vars(struct isl_blk blk
, isl_int
*a
,
1136 unsigned a_len
, unsigned b_len
)
1138 isl_seq_cpy(blk
.data
, a
+a_len
, b_len
);
1139 isl_seq_cpy(blk
.data
+b_len
, a
, a_len
);
1140 isl_seq_cpy(a
, blk
.data
, b_len
+a_len
);
1143 static __isl_give isl_basic_map
*isl_basic_map_swap_vars(
1144 __isl_take isl_basic_map
*bmap
, unsigned pos
, unsigned n1
, unsigned n2
)
1152 isl_assert(bmap
->ctx
,
1153 pos
+ n1
+ n2
<= 1 + isl_basic_map_total_dim(bmap
), goto error
);
1155 if (n1
== 0 || n2
== 0)
1158 bmap
= isl_basic_map_cow(bmap
);
1162 blk
= isl_blk_alloc(bmap
->ctx
, n1
+ n2
);
1163 if (isl_blk_is_error(blk
))
1166 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1168 bmap
->eq
[i
] + pos
, n1
, n2
);
1170 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1172 bmap
->ineq
[i
] + pos
, n1
, n2
);
1174 for (i
= 0; i
< bmap
->n_div
; ++i
)
1176 bmap
->div
[i
]+1 + pos
, n1
, n2
);
1178 isl_blk_free(bmap
->ctx
, blk
);
1180 ISL_F_CLR(bmap
, ISL_BASIC_SET_NORMALIZED
);
1181 bmap
= isl_basic_map_gauss(bmap
, NULL
);
1182 return isl_basic_map_finalize(bmap
);
1184 isl_basic_map_free(bmap
);
1188 static __isl_give isl_basic_set
*isl_basic_set_swap_vars(
1189 __isl_take isl_basic_set
*bset
, unsigned n
)
1194 nparam
= isl_basic_set_n_param(bset
);
1195 dim
= isl_basic_set_n_dim(bset
);
1196 isl_assert(bset
->ctx
, n
<= dim
, goto error
);
1198 return isl_basic_map_swap_vars(bset
, 1 + nparam
, n
, dim
- n
);
1200 isl_basic_set_free(bset
);
1204 static __isl_give isl_set
*isl_set_swap_vars(__isl_take isl_set
*set
,
1208 set
= isl_set_cow(set
);
1212 for (i
= 0; i
< set
->n
; ++i
) {
1213 set
->p
[i
] = isl_basic_set_swap_vars(set
->p
[i
], n
);
1219 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
1223 struct isl_basic_map
*isl_basic_map_set_to_empty(struct isl_basic_map
*bmap
)
1229 total
= isl_basic_map_total_dim(bmap
);
1230 isl_basic_map_free_div(bmap
, bmap
->n_div
);
1231 isl_basic_map_free_inequality(bmap
, bmap
->n_ineq
);
1233 isl_basic_map_free_equality(bmap
, bmap
->n_eq
-1);
1235 i
= isl_basic_map_alloc_equality(bmap
);
1239 isl_int_set_si(bmap
->eq
[i
][0], 1);
1240 isl_seq_clr(bmap
->eq
[i
]+1, total
);
1241 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
1242 isl_vec_free(bmap
->sample
);
1243 bmap
->sample
= NULL
;
1244 return isl_basic_map_finalize(bmap
);
1246 isl_basic_map_free(bmap
);
1250 struct isl_basic_set
*isl_basic_set_set_to_empty(struct isl_basic_set
*bset
)
1252 return (struct isl_basic_set
*)
1253 isl_basic_map_set_to_empty((struct isl_basic_map
*)bset
);
1256 void isl_basic_map_swap_div(struct isl_basic_map
*bmap
, int a
, int b
)
1259 unsigned off
= isl_dim_total(bmap
->dim
);
1260 isl_int
*t
= bmap
->div
[a
];
1261 bmap
->div
[a
] = bmap
->div
[b
];
1264 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1265 isl_int_swap(bmap
->eq
[i
][1+off
+a
], bmap
->eq
[i
][1+off
+b
]);
1267 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1268 isl_int_swap(bmap
->ineq
[i
][1+off
+a
], bmap
->ineq
[i
][1+off
+b
]);
1270 for (i
= 0; i
< bmap
->n_div
; ++i
)
1271 isl_int_swap(bmap
->div
[i
][1+1+off
+a
], bmap
->div
[i
][1+1+off
+b
]);
1272 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1275 /* Eliminate the specified n dimensions starting at first from the
1276 * constraints using Fourier-Motzkin. The dimensions themselves
1279 __isl_give isl_map
*isl_map_eliminate(__isl_take isl_map
*map
,
1280 enum isl_dim_type type
, unsigned first
, unsigned n
)
1289 map
= isl_map_cow(map
);
1292 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
1293 first
+= pos(map
->dim
, type
) - 1;
1295 for (i
= 0; i
< map
->n
; ++i
) {
1296 map
->p
[i
] = isl_basic_map_eliminate_vars(map
->p
[i
], first
, n
);
1306 /* Eliminate the specified n dimensions starting at first from the
1307 * constraints using Fourier-Motzkin. The dimensions themselves
1310 __isl_give isl_set
*isl_set_eliminate(__isl_take isl_set
*set
,
1311 enum isl_dim_type type
, unsigned first
, unsigned n
)
1313 return (isl_set
*)isl_map_eliminate((isl_map
*)set
, type
, first
, n
);
1316 /* Eliminate the specified n dimensions starting at first from the
1317 * constraints using Fourier-Motzkin. The dimensions themselves
1320 __isl_give isl_set
*isl_set_eliminate_dims(__isl_take isl_set
*set
,
1321 unsigned first
, unsigned n
)
1323 return isl_set_eliminate(set
, isl_dim_set
, first
, n
);
1326 __isl_give isl_basic_map
*isl_basic_map_remove_divs(
1327 __isl_take isl_basic_map
*bmap
)
1329 bmap
= isl_basic_map_eliminate_vars(bmap
, isl_dim_total(bmap
->dim
),
1334 return isl_basic_map_finalize(bmap
);
1337 __isl_give isl_basic_set
*isl_basic_set_remove_divs(
1338 __isl_take isl_basic_set
*bset
)
1340 return (struct isl_basic_set
*)isl_basic_map_remove_divs(
1341 (struct isl_basic_map
*)bset
);
1344 __isl_give isl_map
*isl_map_remove_divs(__isl_take isl_map
*map
)
1353 map
= isl_map_cow(map
);
1357 for (i
= 0; i
< map
->n
; ++i
) {
1358 map
->p
[i
] = isl_basic_map_remove_divs(map
->p
[i
]);
1368 __isl_give isl_set
*isl_set_remove_divs(__isl_take isl_set
*set
)
1370 return isl_map_remove_divs(set
);
1373 struct isl_basic_map
*isl_basic_map_remove_dims(struct isl_basic_map
*bmap
,
1374 enum isl_dim_type type
, unsigned first
, unsigned n
)
1378 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
1382 bmap
= isl_basic_map_eliminate_vars(bmap
,
1383 isl_basic_map_offset(bmap
, type
) - 1 + first
, n
);
1386 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
) && type
== isl_dim_div
)
1388 bmap
= isl_basic_map_drop(bmap
, type
, first
, n
);
1391 isl_basic_map_free(bmap
);
1395 /* Return true if the definition of the given div (recursively) involves
1396 * any of the given variables.
1398 static int div_involves_vars(__isl_keep isl_basic_map
*bmap
, int div
,
1399 unsigned first
, unsigned n
)
1402 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
1404 if (isl_int_is_zero(bmap
->div
[div
][0]))
1406 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + first
, n
) >= 0)
1409 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
1410 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
1412 if (div_involves_vars(bmap
, i
, first
, n
))
1419 /* Remove all divs (recursively) involving any of the given dimensions
1420 * in their definitions.
1422 __isl_give isl_basic_map
*isl_basic_map_remove_divs_involving_dims(
1423 __isl_take isl_basic_map
*bmap
,
1424 enum isl_dim_type type
, unsigned first
, unsigned n
)
1430 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
1432 first
+= isl_basic_map_offset(bmap
, type
);
1434 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
1435 if (!div_involves_vars(bmap
, i
, first
, n
))
1437 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
1442 isl_basic_map_free(bmap
);
1446 __isl_give isl_map
*isl_map_remove_divs_involving_dims(__isl_take isl_map
*map
,
1447 enum isl_dim_type type
, unsigned first
, unsigned n
)
1456 map
= isl_map_cow(map
);
1460 for (i
= 0; i
< map
->n
; ++i
) {
1461 map
->p
[i
] = isl_basic_map_remove_divs_involving_dims(map
->p
[i
],
1472 __isl_give isl_set
*isl_set_remove_divs_involving_dims(__isl_take isl_set
*set
,
1473 enum isl_dim_type type
, unsigned first
, unsigned n
)
1475 return (isl_set
*)isl_map_remove_divs_involving_dims((isl_map
*)set
,
1479 /* Return true if the definition of the given div is unknown or depends
1482 static int div_is_unknown(__isl_keep isl_basic_map
*bmap
, int div
)
1485 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
1487 if (isl_int_is_zero(bmap
->div
[div
][0]))
1490 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
1491 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
1493 if (div_is_unknown(bmap
, i
))
1500 /* Remove all divs that are unknown or defined in terms of unknown divs.
1502 __isl_give isl_basic_map
*isl_basic_map_remove_unknown_divs(
1503 __isl_take isl_basic_map
*bmap
)
1510 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
1511 if (!div_is_unknown(bmap
, i
))
1513 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
1519 __isl_give isl_map
*isl_map_remove_unknown_divs(__isl_take isl_map
*map
)
1528 map
= isl_map_cow(map
);
1532 for (i
= 0; i
< map
->n
; ++i
) {
1533 map
->p
[i
] = isl_basic_map_remove_unknown_divs(map
->p
[i
]);
1543 __isl_give isl_set
*isl_set_remove_unknown_divs(__isl_take isl_set
*set
)
1545 return (isl_set
*)isl_map_remove_unknown_divs((isl_map
*)set
);
1548 __isl_give isl_basic_set
*isl_basic_set_remove_dims(
1549 __isl_take isl_basic_set
*bset
,
1550 enum isl_dim_type type
, unsigned first
, unsigned n
)
1552 return (isl_basic_set
*)
1553 isl_basic_map_remove_dims((isl_basic_map
*)bset
, type
, first
, n
);
1556 struct isl_map
*isl_map_remove_dims(struct isl_map
*map
,
1557 enum isl_dim_type type
, unsigned first
, unsigned n
)
1564 map
= isl_map_cow(map
);
1567 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
1569 for (i
= 0; i
< map
->n
; ++i
) {
1570 map
->p
[i
] = isl_basic_map_eliminate_vars(map
->p
[i
],
1571 isl_basic_map_offset(map
->p
[i
], type
) - 1 + first
, n
);
1575 map
= isl_map_drop(map
, type
, first
, n
);
1582 __isl_give isl_set
*isl_set_remove_dims(__isl_take isl_set
*bset
,
1583 enum isl_dim_type type
, unsigned first
, unsigned n
)
1585 return (isl_set
*)isl_map_remove_dims((isl_map
*)bset
, type
, first
, n
);
1588 /* Project out n inputs starting at first using Fourier-Motzkin */
1589 struct isl_map
*isl_map_remove_inputs(struct isl_map
*map
,
1590 unsigned first
, unsigned n
)
1592 return isl_map_remove_dims(map
, isl_dim_in
, first
, n
);
1595 static void dump_term(struct isl_basic_map
*bmap
,
1596 isl_int c
, int pos
, FILE *out
)
1599 unsigned in
= isl_basic_map_n_in(bmap
);
1600 unsigned dim
= in
+ isl_basic_map_n_out(bmap
);
1601 unsigned nparam
= isl_basic_map_n_param(bmap
);
1603 isl_int_print(out
, c
, 0);
1605 if (!isl_int_is_one(c
))
1606 isl_int_print(out
, c
, 0);
1607 if (pos
< 1 + nparam
) {
1608 name
= isl_dim_get_name(bmap
->dim
,
1609 isl_dim_param
, pos
- 1);
1611 fprintf(out
, "%s", name
);
1613 fprintf(out
, "p%d", pos
- 1);
1614 } else if (pos
< 1 + nparam
+ in
)
1615 fprintf(out
, "i%d", pos
- 1 - nparam
);
1616 else if (pos
< 1 + nparam
+ dim
)
1617 fprintf(out
, "o%d", pos
- 1 - nparam
- in
);
1619 fprintf(out
, "e%d", pos
- 1 - nparam
- dim
);
1623 static void dump_constraint_sign(struct isl_basic_map
*bmap
, isl_int
*c
,
1624 int sign
, FILE *out
)
1628 unsigned len
= 1 + isl_basic_map_total_dim(bmap
);
1632 for (i
= 0, first
= 1; i
< len
; ++i
) {
1633 if (isl_int_sgn(c
[i
]) * sign
<= 0)
1636 fprintf(out
, " + ");
1638 isl_int_abs(v
, c
[i
]);
1639 dump_term(bmap
, v
, i
, out
);
1646 static void dump_constraint(struct isl_basic_map
*bmap
, isl_int
*c
,
1647 const char *op
, FILE *out
, int indent
)
1651 fprintf(out
, "%*s", indent
, "");
1653 dump_constraint_sign(bmap
, c
, 1, out
);
1654 fprintf(out
, " %s ", op
);
1655 dump_constraint_sign(bmap
, c
, -1, out
);
1659 for (i
= bmap
->n_div
; i
< bmap
->extra
; ++i
) {
1660 if (isl_int_is_zero(c
[1+isl_dim_total(bmap
->dim
)+i
]))
1662 fprintf(out
, "%*s", indent
, "");
1663 fprintf(out
, "ERROR: unused div coefficient not zero\n");
1668 static void dump_constraints(struct isl_basic_map
*bmap
,
1669 isl_int
**c
, unsigned n
,
1670 const char *op
, FILE *out
, int indent
)
1674 for (i
= 0; i
< n
; ++i
)
1675 dump_constraint(bmap
, c
[i
], op
, out
, indent
);
1678 static void dump_affine(struct isl_basic_map
*bmap
, isl_int
*exp
, FILE *out
)
1682 unsigned total
= isl_basic_map_total_dim(bmap
);
1684 for (j
= 0; j
< 1 + total
; ++j
) {
1685 if (isl_int_is_zero(exp
[j
]))
1687 if (!first
&& isl_int_is_pos(exp
[j
]))
1689 dump_term(bmap
, exp
[j
], j
, out
);
1694 static void dump(struct isl_basic_map
*bmap
, FILE *out
, int indent
)
1698 dump_constraints(bmap
, bmap
->eq
, bmap
->n_eq
, "=", out
, indent
);
1699 dump_constraints(bmap
, bmap
->ineq
, bmap
->n_ineq
, ">=", out
, indent
);
1701 for (i
= 0; i
< bmap
->n_div
; ++i
) {
1702 fprintf(out
, "%*s", indent
, "");
1703 fprintf(out
, "e%d = [(", i
);
1704 dump_affine(bmap
, bmap
->div
[i
]+1, out
);
1706 isl_int_print(out
, bmap
->div
[i
][0], 0);
1707 fprintf(out
, "]\n");
1711 void isl_basic_set_print_internal(struct isl_basic_set
*bset
,
1712 FILE *out
, int indent
)
1715 fprintf(out
, "null basic set\n");
1719 fprintf(out
, "%*s", indent
, "");
1720 fprintf(out
, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
1721 bset
->ref
, bset
->dim
->nparam
, bset
->dim
->n_out
,
1722 bset
->extra
, bset
->flags
);
1723 dump((struct isl_basic_map
*)bset
, out
, indent
);
1726 void isl_basic_map_print_internal(struct isl_basic_map
*bmap
,
1727 FILE *out
, int indent
)
1730 fprintf(out
, "null basic map\n");
1734 fprintf(out
, "%*s", indent
, "");
1735 fprintf(out
, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
1736 "flags: %x, n_name: %d\n",
1738 bmap
->dim
->nparam
, bmap
->dim
->n_in
, bmap
->dim
->n_out
,
1739 bmap
->extra
, bmap
->flags
, bmap
->dim
->n_name
);
1740 dump(bmap
, out
, indent
);
1743 int isl_inequality_negate(struct isl_basic_map
*bmap
, unsigned pos
)
1748 total
= isl_basic_map_total_dim(bmap
);
1749 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
1750 isl_seq_neg(bmap
->ineq
[pos
], bmap
->ineq
[pos
], 1 + total
);
1751 isl_int_sub_ui(bmap
->ineq
[pos
][0], bmap
->ineq
[pos
][0], 1);
1752 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1756 struct isl_set
*isl_set_alloc_dim(struct isl_dim
*dim
, int n
, unsigned flags
)
1758 struct isl_set
*set
;
1762 isl_assert(dim
->ctx
, dim
->n_in
== 0, goto error
);
1763 isl_assert(dim
->ctx
, n
>= 0, goto error
);
1764 set
= isl_alloc(dim
->ctx
, struct isl_set
,
1765 sizeof(struct isl_set
) +
1766 (n
- 1) * sizeof(struct isl_basic_set
*));
1770 set
->ctx
= dim
->ctx
;
1771 isl_ctx_ref(set
->ctx
);
1783 struct isl_set
*isl_set_alloc(struct isl_ctx
*ctx
,
1784 unsigned nparam
, unsigned dim
, int n
, unsigned flags
)
1786 struct isl_set
*set
;
1787 struct isl_dim
*dims
;
1789 dims
= isl_dim_alloc(ctx
, nparam
, 0, dim
);
1793 set
= isl_set_alloc_dim(dims
, n
, flags
);
1797 /* Make sure "map" has room for at least "n" more basic maps.
1799 struct isl_map
*isl_map_grow(struct isl_map
*map
, int n
)
1802 struct isl_map
*grown
= NULL
;
1806 isl_assert(map
->ctx
, n
>= 0, goto error
);
1807 if (map
->n
+ n
<= map
->size
)
1809 grown
= isl_map_alloc_dim(isl_map_get_dim(map
), map
->n
+ n
, map
->flags
);
1812 for (i
= 0; i
< map
->n
; ++i
) {
1813 grown
->p
[i
] = isl_basic_map_copy(map
->p
[i
]);
1821 isl_map_free(grown
);
1826 /* Make sure "set" has room for at least "n" more basic sets.
1828 struct isl_set
*isl_set_grow(struct isl_set
*set
, int n
)
1830 return (struct isl_set
*)isl_map_grow((struct isl_map
*)set
, n
);
1833 struct isl_set
*isl_set_dup(struct isl_set
*set
)
1836 struct isl_set
*dup
;
1841 dup
= isl_set_alloc_dim(isl_dim_copy(set
->dim
), set
->n
, set
->flags
);
1844 for (i
= 0; i
< set
->n
; ++i
)
1845 dup
= isl_set_add_basic_set(dup
, isl_basic_set_copy(set
->p
[i
]));
1849 struct isl_set
*isl_set_from_basic_set(struct isl_basic_set
*bset
)
1851 return isl_map_from_basic_map(bset
);
1854 struct isl_map
*isl_map_from_basic_map(struct isl_basic_map
*bmap
)
1856 struct isl_map
*map
;
1861 map
= isl_map_alloc_dim(isl_dim_copy(bmap
->dim
), 1, ISL_MAP_DISJOINT
);
1862 return isl_map_add_basic_map(map
, bmap
);
1865 __isl_give isl_set
*isl_set_add_basic_set(__isl_take isl_set
*set
,
1866 __isl_take isl_basic_set
*bset
)
1868 return (struct isl_set
*)isl_map_add_basic_map((struct isl_map
*)set
,
1869 (struct isl_basic_map
*)bset
);
1872 void isl_set_free(struct isl_set
*set
)
1882 isl_ctx_deref(set
->ctx
);
1883 for (i
= 0; i
< set
->n
; ++i
)
1884 isl_basic_set_free(set
->p
[i
]);
1885 isl_dim_free(set
->dim
);
1889 void isl_set_print_internal(struct isl_set
*set
, FILE *out
, int indent
)
1894 fprintf(out
, "null set\n");
1898 fprintf(out
, "%*s", indent
, "");
1899 fprintf(out
, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
1900 set
->ref
, set
->n
, set
->dim
->nparam
, set
->dim
->n_out
,
1902 for (i
= 0; i
< set
->n
; ++i
) {
1903 fprintf(out
, "%*s", indent
, "");
1904 fprintf(out
, "basic set %d:\n", i
);
1905 isl_basic_set_print_internal(set
->p
[i
], out
, indent
+4);
1909 void isl_map_print_internal(struct isl_map
*map
, FILE *out
, int indent
)
1914 fprintf(out
, "null map\n");
1918 fprintf(out
, "%*s", indent
, "");
1919 fprintf(out
, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
1920 "flags: %x, n_name: %d\n",
1921 map
->ref
, map
->n
, map
->dim
->nparam
, map
->dim
->n_in
,
1922 map
->dim
->n_out
, map
->flags
, map
->dim
->n_name
);
1923 for (i
= 0; i
< map
->n
; ++i
) {
1924 fprintf(out
, "%*s", indent
, "");
1925 fprintf(out
, "basic map %d:\n", i
);
1926 isl_basic_map_print_internal(map
->p
[i
], out
, indent
+4);
1930 struct isl_basic_map
*isl_basic_map_intersect_domain(
1931 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
1933 struct isl_basic_map
*bmap_domain
;
1938 isl_assert(bset
->ctx
, isl_dim_match(bmap
->dim
, isl_dim_param
,
1939 bset
->dim
, isl_dim_param
), goto error
);
1941 if (isl_dim_size(bset
->dim
, isl_dim_set
) != 0)
1942 isl_assert(bset
->ctx
,
1943 isl_basic_map_compatible_domain(bmap
, bset
), goto error
);
1945 bmap
= isl_basic_map_cow(bmap
);
1948 bmap
= isl_basic_map_extend_dim(bmap
, isl_dim_copy(bmap
->dim
),
1949 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
1950 bmap_domain
= isl_basic_map_from_domain(bset
);
1951 bmap
= add_constraints(bmap
, bmap_domain
, 0, 0);
1953 bmap
= isl_basic_map_simplify(bmap
);
1954 return isl_basic_map_finalize(bmap
);
1956 isl_basic_map_free(bmap
);
1957 isl_basic_set_free(bset
);
1961 struct isl_basic_map
*isl_basic_map_intersect_range(
1962 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
1964 struct isl_basic_map
*bmap_range
;
1969 isl_assert(bset
->ctx
, isl_dim_match(bmap
->dim
, isl_dim_param
,
1970 bset
->dim
, isl_dim_param
), goto error
);
1972 if (isl_dim_size(bset
->dim
, isl_dim_set
) != 0)
1973 isl_assert(bset
->ctx
,
1974 isl_basic_map_compatible_range(bmap
, bset
), goto error
);
1976 bmap
= isl_basic_map_cow(bmap
);
1979 bmap
= isl_basic_map_extend_dim(bmap
, isl_dim_copy(bmap
->dim
),
1980 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
1981 bmap_range
= isl_basic_map_from_basic_set(bset
, isl_dim_copy(bset
->dim
));
1982 bmap
= add_constraints(bmap
, bmap_range
, 0, 0);
1984 bmap
= isl_basic_map_simplify(bmap
);
1985 return isl_basic_map_finalize(bmap
);
1987 isl_basic_map_free(bmap
);
1988 isl_basic_set_free(bset
);
1992 int isl_basic_map_contains(struct isl_basic_map
*bmap
, struct isl_vec
*vec
)
1998 total
= 1 + isl_basic_map_total_dim(bmap
);
1999 if (total
!= vec
->size
)
2004 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
2005 isl_seq_inner_product(vec
->el
, bmap
->eq
[i
], total
, &s
);
2006 if (!isl_int_is_zero(s
)) {
2012 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
2013 isl_seq_inner_product(vec
->el
, bmap
->ineq
[i
], total
, &s
);
2014 if (isl_int_is_neg(s
)) {
2025 int isl_basic_set_contains(struct isl_basic_set
*bset
, struct isl_vec
*vec
)
2027 return isl_basic_map_contains((struct isl_basic_map
*)bset
, vec
);
2030 struct isl_basic_map
*isl_basic_map_intersect(
2031 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
2033 struct isl_vec
*sample
= NULL
;
2035 if (!bmap1
|| !bmap2
)
2038 isl_assert(bmap1
->ctx
, isl_dim_match(bmap1
->dim
, isl_dim_param
,
2039 bmap2
->dim
, isl_dim_param
), goto error
);
2040 if (isl_dim_total(bmap1
->dim
) ==
2041 isl_dim_size(bmap1
->dim
, isl_dim_param
) &&
2042 isl_dim_total(bmap2
->dim
) !=
2043 isl_dim_size(bmap2
->dim
, isl_dim_param
))
2044 return isl_basic_map_intersect(bmap2
, bmap1
);
2046 if (isl_dim_total(bmap2
->dim
) !=
2047 isl_dim_size(bmap2
->dim
, isl_dim_param
))
2048 isl_assert(bmap1
->ctx
,
2049 isl_dim_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
2051 if (bmap1
->sample
&&
2052 isl_basic_map_contains(bmap1
, bmap1
->sample
) > 0 &&
2053 isl_basic_map_contains(bmap2
, bmap1
->sample
) > 0)
2054 sample
= isl_vec_copy(bmap1
->sample
);
2055 else if (bmap2
->sample
&&
2056 isl_basic_map_contains(bmap1
, bmap2
->sample
) > 0 &&
2057 isl_basic_map_contains(bmap2
, bmap2
->sample
) > 0)
2058 sample
= isl_vec_copy(bmap2
->sample
);
2060 bmap1
= isl_basic_map_cow(bmap1
);
2063 bmap1
= isl_basic_map_extend_dim(bmap1
, isl_dim_copy(bmap1
->dim
),
2064 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
2065 bmap1
= add_constraints(bmap1
, bmap2
, 0, 0);
2068 isl_vec_free(sample
);
2070 isl_vec_free(bmap1
->sample
);
2071 bmap1
->sample
= sample
;
2074 bmap1
= isl_basic_map_simplify(bmap1
);
2075 return isl_basic_map_finalize(bmap1
);
2078 isl_vec_free(sample
);
2079 isl_basic_map_free(bmap1
);
2080 isl_basic_map_free(bmap2
);
2084 struct isl_basic_set
*isl_basic_set_intersect(
2085 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
2087 return (struct isl_basic_set
*)
2088 isl_basic_map_intersect(
2089 (struct isl_basic_map
*)bset1
,
2090 (struct isl_basic_map
*)bset2
);
2093 /* Special case of isl_map_intersect, where both map1 and map2
2094 * are convex, without any divs and such that either map1 or map2
2095 * contains a single constraint. This constraint is then simply
2096 * added to the other map.
2098 static __isl_give isl_map
*map_intersect_add_constraint(
2099 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
2101 struct isl_basic_map
*bmap1
;
2102 struct isl_basic_map
*bmap2
;
2104 isl_assert(map1
->ctx
, map1
->n
== 1, goto error
);
2105 isl_assert(map2
->ctx
, map1
->n
== 1, goto error
);
2106 isl_assert(map1
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
2107 isl_assert(map2
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
2109 if (map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
!= 1)
2110 return isl_map_intersect(map2
, map1
);
2112 isl_assert(map2
->ctx
,
2113 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1, goto error
);
2115 map1
= isl_map_cow(map1
);
2118 if (isl_map_fast_is_empty(map1
)) {
2122 map1
->p
[0] = isl_basic_map_cow(map1
->p
[0]);
2123 if (map2
->p
[0]->n_eq
== 1)
2124 map1
->p
[0] = isl_basic_map_add_eq(map1
->p
[0], map2
->p
[0]->eq
[0]);
2126 map1
->p
[0] = isl_basic_map_add_ineq(map1
->p
[0],
2127 map2
->p
[0]->ineq
[0]);
2129 map1
->p
[0] = isl_basic_map_simplify(map1
->p
[0]);
2130 map1
->p
[0] = isl_basic_map_finalize(map1
->p
[0]);
2134 if (isl_basic_map_fast_is_empty(map1
->p
[0])) {
2135 isl_basic_map_free(map1
->p
[0]);
2148 struct isl_map
*isl_map_intersect(struct isl_map
*map1
, struct isl_map
*map2
)
2151 struct isl_map
*result
;
2157 if (isl_map_fast_is_empty(map1
)) {
2161 if (isl_map_fast_is_empty(map2
)) {
2166 if (map1
->n
== 1 && map2
->n
== 1 &&
2167 map1
->p
[0]->n_div
== 0 && map2
->p
[0]->n_div
== 0 &&
2168 isl_dim_equal(map1
->dim
, map2
->dim
) &&
2169 (map1
->p
[0]->n_eq
+ map1
->p
[0]->n_ineq
== 1 ||
2170 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1))
2171 return map_intersect_add_constraint(map1
, map2
);
2172 isl_assert(map1
->ctx
, isl_dim_match(map1
->dim
, isl_dim_param
,
2173 map2
->dim
, isl_dim_param
), goto error
);
2174 if (isl_dim_total(map1
->dim
) ==
2175 isl_dim_size(map1
->dim
, isl_dim_param
) &&
2176 isl_dim_total(map2
->dim
) != isl_dim_size(map2
->dim
, isl_dim_param
))
2177 return isl_map_intersect(map2
, map1
);
2179 if (isl_dim_total(map2
->dim
) != isl_dim_size(map2
->dim
, isl_dim_param
))
2180 isl_assert(map1
->ctx
,
2181 isl_dim_equal(map1
->dim
, map2
->dim
), goto error
);
2183 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
2184 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
2185 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
2187 result
= isl_map_alloc_dim(isl_dim_copy(map1
->dim
),
2188 map1
->n
* map2
->n
, flags
);
2191 for (i
= 0; i
< map1
->n
; ++i
)
2192 for (j
= 0; j
< map2
->n
; ++j
) {
2193 struct isl_basic_map
*part
;
2194 part
= isl_basic_map_intersect(
2195 isl_basic_map_copy(map1
->p
[i
]),
2196 isl_basic_map_copy(map2
->p
[j
]));
2197 if (isl_basic_map_is_empty(part
))
2198 isl_basic_map_free(part
);
2200 result
= isl_map_add_basic_map(result
, part
);
2213 struct isl_set
*isl_set_intersect(struct isl_set
*set1
, struct isl_set
*set2
)
2215 return (struct isl_set
*)
2216 isl_map_intersect((struct isl_map
*)set1
,
2217 (struct isl_map
*)set2
);
2220 struct isl_basic_map
*isl_basic_map_reverse(struct isl_basic_map
*bmap
)
2222 struct isl_dim
*dim
;
2223 struct isl_basic_set
*bset
;
2228 bmap
= isl_basic_map_cow(bmap
);
2231 dim
= isl_dim_reverse(isl_dim_copy(bmap
->dim
));
2232 in
= isl_basic_map_n_in(bmap
);
2233 bset
= isl_basic_set_from_basic_map(bmap
);
2234 bset
= isl_basic_set_swap_vars(bset
, in
);
2235 return isl_basic_map_from_basic_set(bset
, dim
);
2238 __isl_give isl_basic_map
*isl_basic_map_insert(__isl_take isl_basic_map
*bmap
,
2239 enum isl_dim_type type
, unsigned pos
, unsigned n
)
2241 struct isl_dim
*res_dim
;
2242 struct isl_basic_map
*res
;
2243 struct isl_dim_map
*dim_map
;
2244 unsigned total
, off
;
2245 enum isl_dim_type t
;
2253 res_dim
= isl_dim_insert(isl_basic_map_get_dim(bmap
), type
, pos
, n
);
2255 total
= isl_basic_map_total_dim(bmap
) + n
;
2256 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
2258 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
2260 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
2262 unsigned size
= isl_basic_map_dim(bmap
, t
);
2263 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2265 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2266 pos
, size
- pos
, off
+ pos
+ n
);
2268 off
+= isl_dim_size(res_dim
, t
);
2270 isl_dim_map_div(dim_map
, bmap
, off
);
2272 res
= isl_basic_map_alloc_dim(res_dim
,
2273 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
2274 if (isl_basic_map_is_rational(bmap
))
2275 res
= isl_basic_map_set_rational(res
);
2276 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
2277 return isl_basic_map_finalize(res
);
2280 __isl_give isl_basic_map
*isl_basic_map_add(__isl_take isl_basic_map
*bmap
,
2281 enum isl_dim_type type
, unsigned n
)
2285 return isl_basic_map_insert(bmap
, type
,
2286 isl_basic_map_dim(bmap
, type
), n
);
2289 __isl_give isl_basic_set
*isl_basic_set_add(__isl_take isl_basic_set
*bset
,
2290 enum isl_dim_type type
, unsigned n
)
2294 isl_assert(bset
->ctx
, type
!= isl_dim_in
, goto error
);
2295 return (isl_basic_set
*)isl_basic_map_add((isl_basic_map
*)bset
, type
, n
);
2297 isl_basic_set_free(bset
);
2301 __isl_give isl_map
*isl_map_insert(__isl_take isl_map
*map
,
2302 enum isl_dim_type type
, unsigned pos
, unsigned n
)
2309 map
= isl_map_cow(map
);
2313 map
->dim
= isl_dim_insert(map
->dim
, type
, pos
, n
);
2317 for (i
= 0; i
< map
->n
; ++i
) {
2318 map
->p
[i
] = isl_basic_map_insert(map
->p
[i
], type
, pos
, n
);
2329 __isl_give isl_set
*isl_set_insert(__isl_take isl_set
*set
,
2330 enum isl_dim_type type
, unsigned pos
, unsigned n
)
2332 return (isl_set
*)isl_map_insert((isl_map
*)set
, type
, pos
, n
);
2335 __isl_give isl_map
*isl_map_add_dims(__isl_take isl_map
*map
,
2336 enum isl_dim_type type
, unsigned n
)
2340 return isl_map_insert(map
, type
, isl_map_dim(map
, type
), n
);
2343 __isl_give isl_set
*isl_set_add_dims(__isl_take isl_set
*set
,
2344 enum isl_dim_type type
, unsigned n
)
2348 isl_assert(set
->ctx
, type
!= isl_dim_in
, goto error
);
2349 return (isl_set
*)isl_map_add_dims((isl_map
*)set
, type
, n
);
2355 __isl_give isl_basic_map
*isl_basic_map_move_dims(
2356 __isl_take isl_basic_map
*bmap
,
2357 enum isl_dim_type dst_type
, unsigned dst_pos
,
2358 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2361 struct isl_dim_map
*dim_map
;
2362 struct isl_basic_map
*res
;
2363 enum isl_dim_type t
;
2364 unsigned total
, off
;
2371 isl_assert(bmap
->ctx
, src_pos
+ n
<= isl_basic_map_dim(bmap
, src_type
),
2374 if (dst_type
== src_type
&& dst_pos
== src_pos
)
2377 isl_assert(bmap
->ctx
, dst_type
!= src_type
, goto error
);
2379 if (pos(bmap
->dim
, dst_type
) + dst_pos
==
2380 pos(bmap
->dim
, src_type
) + src_pos
+
2381 ((src_type
< dst_type
) ? n
: 0)) {
2382 bmap
= isl_basic_map_cow(bmap
);
2386 bmap
->dim
= isl_dim_move(bmap
->dim
, dst_type
, dst_pos
,
2387 src_type
, src_pos
, n
);
2391 bmap
= isl_basic_map_finalize(bmap
);
2396 total
= isl_basic_map_total_dim(bmap
);
2397 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
2400 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
2401 unsigned size
= isl_dim_size(bmap
->dim
, t
);
2402 if (t
== dst_type
) {
2403 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2406 isl_dim_map_dim_range(dim_map
, bmap
->dim
, src_type
,
2409 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2410 dst_pos
, size
- dst_pos
, off
);
2411 off
+= size
- dst_pos
;
2412 } else if (t
== src_type
) {
2413 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2416 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2417 src_pos
+ n
, size
- src_pos
- n
, off
);
2418 off
+= size
- src_pos
- n
;
2420 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
2424 isl_dim_map_div(dim_map
, bmap
, off
);
2426 res
= isl_basic_map_alloc_dim(isl_basic_map_get_dim(bmap
),
2427 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
2428 bmap
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
2430 bmap
->dim
= isl_dim_move(bmap
->dim
, dst_type
, dst_pos
,
2431 src_type
, src_pos
, n
);
2435 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
2436 bmap
= isl_basic_map_gauss(bmap
, NULL
);
2437 bmap
= isl_basic_map_finalize(bmap
);
2441 isl_basic_map_free(bmap
);
2445 __isl_give isl_basic_set
*isl_basic_set_move_dims(__isl_take isl_basic_set
*bset
,
2446 enum isl_dim_type dst_type
, unsigned dst_pos
,
2447 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2449 return (isl_basic_set
*)isl_basic_map_move_dims(
2450 (isl_basic_map
*)bset
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
2453 __isl_give isl_set
*isl_set_move_dims(__isl_take isl_set
*set
,
2454 enum isl_dim_type dst_type
, unsigned dst_pos
,
2455 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2459 isl_assert(set
->ctx
, dst_type
!= isl_dim_in
, goto error
);
2460 return (isl_set
*)isl_map_move_dims((isl_map
*)set
, dst_type
, dst_pos
,
2461 src_type
, src_pos
, n
);
2467 __isl_give isl_map
*isl_map_move_dims(__isl_take isl_map
*map
,
2468 enum isl_dim_type dst_type
, unsigned dst_pos
,
2469 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2478 isl_assert(map
->ctx
, src_pos
+ n
<= isl_map_dim(map
, src_type
),
2481 if (dst_type
== src_type
&& dst_pos
== src_pos
)
2484 isl_assert(map
->ctx
, dst_type
!= src_type
, goto error
);
2486 map
= isl_map_cow(map
);
2490 map
->dim
= isl_dim_move(map
->dim
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
2494 for (i
= 0; i
< map
->n
; ++i
) {
2495 map
->p
[i
] = isl_basic_map_move_dims(map
->p
[i
],
2497 src_type
, src_pos
, n
);
2508 /* Move the specified dimensions to the last columns right before
2509 * the divs. Don't change the dimension specification of bmap.
2510 * That's the responsibility of the caller.
2512 static __isl_give isl_basic_map
*move_last(__isl_take isl_basic_map
*bmap
,
2513 enum isl_dim_type type
, unsigned first
, unsigned n
)
2515 struct isl_dim_map
*dim_map
;
2516 struct isl_basic_map
*res
;
2517 enum isl_dim_type t
;
2518 unsigned total
, off
;
2522 if (pos(bmap
->dim
, type
) + first
+ n
== 1 + isl_dim_total(bmap
->dim
))
2525 total
= isl_basic_map_total_dim(bmap
);
2526 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
2529 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
2530 unsigned size
= isl_dim_size(bmap
->dim
, t
);
2532 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2535 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2536 first
, n
, total
- bmap
->n_div
- n
);
2537 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2538 first
+ n
, size
- (first
+ n
), off
);
2539 off
+= size
- (first
+ n
);
2541 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
2545 isl_dim_map_div(dim_map
, bmap
, off
+ n
);
2547 res
= isl_basic_map_alloc_dim(isl_basic_map_get_dim(bmap
),
2548 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
2549 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
2553 /* Turn the n dimensions of type type, starting at first
2554 * into existentially quantified variables.
2556 __isl_give isl_basic_map
*isl_basic_map_project_out(
2557 __isl_take isl_basic_map
*bmap
,
2558 enum isl_dim_type type
, unsigned first
, unsigned n
)
2571 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
2572 return isl_basic_map_remove_dims(bmap
, type
, first
, n
);
2574 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
2577 bmap
= move_last(bmap
, type
, first
, n
);
2578 bmap
= isl_basic_map_cow(bmap
);
2582 row_size
= 1 + isl_dim_total(bmap
->dim
) + bmap
->extra
;
2583 old
= bmap
->block2
.data
;
2584 bmap
->block2
= isl_blk_extend(bmap
->ctx
, bmap
->block2
,
2585 (bmap
->extra
+ n
) * (1 + row_size
));
2586 if (!bmap
->block2
.data
)
2588 new_div
= isl_alloc_array(bmap
->ctx
, isl_int
*, bmap
->extra
+ n
);
2591 for (i
= 0; i
< n
; ++i
) {
2592 new_div
[i
] = bmap
->block2
.data
+
2593 (bmap
->extra
+ i
) * (1 + row_size
);
2594 isl_seq_clr(new_div
[i
], 1 + row_size
);
2596 for (i
= 0; i
< bmap
->extra
; ++i
)
2597 new_div
[n
+ i
] = bmap
->block2
.data
+ (bmap
->div
[i
] - old
);
2599 bmap
->div
= new_div
;
2603 bmap
->dim
= isl_dim_drop(bmap
->dim
, type
, first
, n
);
2606 bmap
= isl_basic_map_simplify(bmap
);
2607 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
2608 return isl_basic_map_finalize(bmap
);
2610 isl_basic_map_free(bmap
);
2614 /* Turn the n dimensions of type type, starting at first
2615 * into existentially quantified variables.
2617 struct isl_basic_set
*isl_basic_set_project_out(struct isl_basic_set
*bset
,
2618 enum isl_dim_type type
, unsigned first
, unsigned n
)
2620 return (isl_basic_set
*)isl_basic_map_project_out(
2621 (isl_basic_map
*)bset
, type
, first
, n
);
2624 /* Turn the n dimensions of type type, starting at first
2625 * into existentially quantified variables.
2627 __isl_give isl_map
*isl_map_project_out(__isl_take isl_map
*map
,
2628 enum isl_dim_type type
, unsigned first
, unsigned n
)
2638 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
2640 map
= isl_map_cow(map
);
2644 map
->dim
= isl_dim_drop(map
->dim
, type
, first
, n
);
2648 for (i
= 0; i
< map
->n
; ++i
) {
2649 map
->p
[i
] = isl_basic_map_project_out(map
->p
[i
], type
, first
, n
);
2660 /* Turn the n dimensions of type type, starting at first
2661 * into existentially quantified variables.
2663 __isl_give isl_set
*isl_set_project_out(__isl_take isl_set
*set
,
2664 enum isl_dim_type type
, unsigned first
, unsigned n
)
2666 return (isl_set
*)isl_map_project_out((isl_map
*)set
, type
, first
, n
);
2669 static struct isl_basic_map
*add_divs(struct isl_basic_map
*bmap
, unsigned n
)
2673 for (i
= 0; i
< n
; ++i
) {
2674 j
= isl_basic_map_alloc_div(bmap
);
2677 isl_seq_clr(bmap
->div
[j
], 1+1+isl_basic_map_total_dim(bmap
));
2681 isl_basic_map_free(bmap
);
2685 struct isl_basic_map
*isl_basic_map_apply_range(
2686 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
2688 struct isl_dim
*dim_result
= NULL
;
2689 struct isl_basic_map
*bmap
;
2690 unsigned n_in
, n_out
, n
, nparam
, total
, pos
;
2691 struct isl_dim_map
*dim_map1
, *dim_map2
;
2693 if (!bmap1
|| !bmap2
)
2696 dim_result
= isl_dim_join(isl_dim_copy(bmap1
->dim
),
2697 isl_dim_copy(bmap2
->dim
));
2699 n_in
= isl_basic_map_n_in(bmap1
);
2700 n_out
= isl_basic_map_n_out(bmap2
);
2701 n
= isl_basic_map_n_out(bmap1
);
2702 nparam
= isl_basic_map_n_param(bmap1
);
2704 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ n
;
2705 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
2706 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
2707 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
2708 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
2709 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
2710 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_in
);
2711 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_out
);
2712 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
2713 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
2714 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
2716 bmap
= isl_basic_map_alloc_dim(dim_result
,
2717 bmap1
->n_div
+ bmap2
->n_div
+ n
,
2718 bmap1
->n_eq
+ bmap2
->n_eq
,
2719 bmap1
->n_ineq
+ bmap2
->n_ineq
);
2720 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
2721 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
2722 bmap
= add_divs(bmap
, n
);
2723 bmap
= isl_basic_map_simplify(bmap
);
2724 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
2725 return isl_basic_map_finalize(bmap
);
2727 isl_basic_map_free(bmap1
);
2728 isl_basic_map_free(bmap2
);
2732 struct isl_basic_set
*isl_basic_set_apply(
2733 struct isl_basic_set
*bset
, struct isl_basic_map
*bmap
)
2738 isl_assert(bset
->ctx
, isl_basic_map_compatible_domain(bmap
, bset
),
2741 return (struct isl_basic_set
*)
2742 isl_basic_map_apply_range((struct isl_basic_map
*)bset
, bmap
);
2744 isl_basic_set_free(bset
);
2745 isl_basic_map_free(bmap
);
2749 struct isl_basic_map
*isl_basic_map_apply_domain(
2750 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
2752 if (!bmap1
|| !bmap2
)
2755 isl_assert(bmap1
->ctx
,
2756 isl_basic_map_n_in(bmap1
) == isl_basic_map_n_in(bmap2
), goto error
);
2757 isl_assert(bmap1
->ctx
,
2758 isl_basic_map_n_param(bmap1
) == isl_basic_map_n_param(bmap2
),
2761 bmap1
= isl_basic_map_reverse(bmap1
);
2762 bmap1
= isl_basic_map_apply_range(bmap1
, bmap2
);
2763 return isl_basic_map_reverse(bmap1
);
2765 isl_basic_map_free(bmap1
);
2766 isl_basic_map_free(bmap2
);
2770 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
2771 * A \cap B -> f(A) + f(B)
2773 struct isl_basic_map
*isl_basic_map_sum(
2774 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
2776 unsigned n_in
, n_out
, nparam
, total
, pos
;
2777 struct isl_basic_map
*bmap
= NULL
;
2778 struct isl_dim_map
*dim_map1
, *dim_map2
;
2781 if (!bmap1
|| !bmap2
)
2784 isl_assert(bmap1
->ctx
, isl_dim_equal(bmap1
->dim
, bmap2
->dim
),
2787 nparam
= isl_basic_map_n_param(bmap1
);
2788 n_in
= isl_basic_map_n_in(bmap1
);
2789 n_out
= isl_basic_map_n_out(bmap1
);
2791 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
;
2792 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
2793 dim_map2
= isl_dim_map_alloc(bmap2
->ctx
, total
);
2794 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
2795 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
);
2796 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
2797 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
2798 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_in
+ n_out
);
2799 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
2800 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
2801 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_out
);
2803 bmap
= isl_basic_map_alloc_dim(isl_dim_copy(bmap1
->dim
),
2804 bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
,
2805 bmap1
->n_eq
+ bmap2
->n_eq
+ n_out
,
2806 bmap1
->n_ineq
+ bmap2
->n_ineq
);
2807 for (i
= 0; i
< n_out
; ++i
) {
2808 int j
= isl_basic_map_alloc_equality(bmap
);
2811 isl_seq_clr(bmap
->eq
[j
], 1+total
);
2812 isl_int_set_si(bmap
->eq
[j
][1+nparam
+n_in
+i
], -1);
2813 isl_int_set_si(bmap
->eq
[j
][1+pos
+i
], 1);
2814 isl_int_set_si(bmap
->eq
[j
][1+pos
-n_out
+i
], 1);
2816 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
2817 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
2818 bmap
= add_divs(bmap
, 2 * n_out
);
2820 bmap
= isl_basic_map_simplify(bmap
);
2821 return isl_basic_map_finalize(bmap
);
2823 isl_basic_map_free(bmap
);
2824 isl_basic_map_free(bmap1
);
2825 isl_basic_map_free(bmap2
);
2829 /* Given two maps A -> f(A) and B -> g(B), construct a map
2830 * A \cap B -> f(A) + f(B)
2832 struct isl_map
*isl_map_sum(struct isl_map
*map1
, struct isl_map
*map2
)
2834 struct isl_map
*result
;
2840 isl_assert(map1
->ctx
, isl_dim_equal(map1
->dim
, map2
->dim
), goto error
);
2842 result
= isl_map_alloc_dim(isl_dim_copy(map1
->dim
),
2843 map1
->n
* map2
->n
, 0);
2846 for (i
= 0; i
< map1
->n
; ++i
)
2847 for (j
= 0; j
< map2
->n
; ++j
) {
2848 struct isl_basic_map
*part
;
2849 part
= isl_basic_map_sum(
2850 isl_basic_map_copy(map1
->p
[i
]),
2851 isl_basic_map_copy(map2
->p
[j
]));
2852 if (isl_basic_map_is_empty(part
))
2853 isl_basic_map_free(part
);
2855 result
= isl_map_add_basic_map(result
, part
);
2868 __isl_give isl_set
*isl_set_sum(__isl_take isl_set
*set1
,
2869 __isl_take isl_set
*set2
)
2871 return (isl_set
*)isl_map_sum((isl_map
*)set1
, (isl_map
*)set2
);
2874 /* Given a basic map A -> f(A), construct A -> -f(A).
2876 struct isl_basic_map
*isl_basic_map_neg(struct isl_basic_map
*bmap
)
2881 bmap
= isl_basic_map_cow(bmap
);
2885 n
= isl_basic_map_dim(bmap
, isl_dim_out
);
2886 off
= isl_basic_map_offset(bmap
, isl_dim_out
);
2887 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2888 for (j
= 0; j
< n
; ++j
)
2889 isl_int_neg(bmap
->eq
[i
][off
+j
], bmap
->eq
[i
][off
+j
]);
2890 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2891 for (j
= 0; j
< n
; ++j
)
2892 isl_int_neg(bmap
->ineq
[i
][off
+j
], bmap
->ineq
[i
][off
+j
]);
2893 for (i
= 0; i
< bmap
->n_div
; ++i
)
2894 for (j
= 0; j
< n
; ++j
)
2895 isl_int_neg(bmap
->div
[i
][1+off
+j
], bmap
->div
[i
][1+off
+j
]);
2896 return isl_basic_map_finalize(bmap
);
2899 __isl_give isl_basic_set
*isl_basic_set_neg(__isl_take isl_basic_set
*bset
)
2901 return isl_basic_map_neg(bset
);
2904 /* Given a map A -> f(A), construct A -> -f(A).
2906 struct isl_map
*isl_map_neg(struct isl_map
*map
)
2910 map
= isl_map_cow(map
);
2914 for (i
= 0; i
< map
->n
; ++i
) {
2915 map
->p
[i
] = isl_basic_map_neg(map
->p
[i
]);
2926 __isl_give isl_set
*isl_set_neg(__isl_take isl_set
*set
)
2928 return (isl_set
*)isl_map_neg((isl_map
*)set
);
2931 /* Given a basic map A -> f(A) and an integer d, construct a basic map
2932 * A -> floor(f(A)/d).
2934 struct isl_basic_map
*isl_basic_map_floordiv(struct isl_basic_map
*bmap
,
2937 unsigned n_in
, n_out
, nparam
, total
, pos
;
2938 struct isl_basic_map
*result
= NULL
;
2939 struct isl_dim_map
*dim_map
;
2945 nparam
= isl_basic_map_n_param(bmap
);
2946 n_in
= isl_basic_map_n_in(bmap
);
2947 n_out
= isl_basic_map_n_out(bmap
);
2949 total
= nparam
+ n_in
+ n_out
+ bmap
->n_div
+ n_out
;
2950 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
2951 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_param
, pos
= 0);
2952 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_in
, pos
+= nparam
);
2953 isl_dim_map_div(dim_map
, bmap
, pos
+= n_in
+ n_out
);
2954 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_out
, pos
+= bmap
->n_div
);
2956 result
= isl_basic_map_alloc_dim(isl_dim_copy(bmap
->dim
),
2957 bmap
->n_div
+ n_out
,
2958 bmap
->n_eq
, bmap
->n_ineq
+ 2 * n_out
);
2959 result
= isl_basic_map_add_constraints_dim_map(result
, bmap
, dim_map
);
2960 result
= add_divs(result
, n_out
);
2961 for (i
= 0; i
< n_out
; ++i
) {
2963 j
= isl_basic_map_alloc_inequality(result
);
2966 isl_seq_clr(result
->ineq
[j
], 1+total
);
2967 isl_int_neg(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
2968 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], 1);
2969 j
= isl_basic_map_alloc_inequality(result
);
2972 isl_seq_clr(result
->ineq
[j
], 1+total
);
2973 isl_int_set(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
2974 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], -1);
2975 isl_int_sub_ui(result
->ineq
[j
][0], d
, 1);
2978 result
= isl_basic_map_simplify(result
);
2979 return isl_basic_map_finalize(result
);
2981 isl_basic_map_free(result
);
2985 /* Given a map A -> f(A) and an integer d, construct a map
2986 * A -> floor(f(A)/d).
2988 struct isl_map
*isl_map_floordiv(struct isl_map
*map
, isl_int d
)
2992 map
= isl_map_cow(map
);
2996 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
2997 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
2998 for (i
= 0; i
< map
->n
; ++i
) {
2999 map
->p
[i
] = isl_basic_map_floordiv(map
->p
[i
], d
);
3010 static struct isl_basic_map
*var_equal(struct isl_basic_map
*bmap
, unsigned pos
)
3016 i
= isl_basic_map_alloc_equality(bmap
);
3019 nparam
= isl_basic_map_n_param(bmap
);
3020 n_in
= isl_basic_map_n_in(bmap
);
3021 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3022 isl_int_set_si(bmap
->eq
[i
][1+nparam
+pos
], -1);
3023 isl_int_set_si(bmap
->eq
[i
][1+nparam
+n_in
+pos
], 1);
3024 return isl_basic_map_finalize(bmap
);
3026 isl_basic_map_free(bmap
);
3030 /* Add a constraints to "bmap" expressing i_pos < o_pos
3032 static struct isl_basic_map
*var_less(struct isl_basic_map
*bmap
, unsigned pos
)
3038 i
= isl_basic_map_alloc_inequality(bmap
);
3041 nparam
= isl_basic_map_n_param(bmap
);
3042 n_in
= isl_basic_map_n_in(bmap
);
3043 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3044 isl_int_set_si(bmap
->ineq
[i
][0], -1);
3045 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
3046 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
3047 return isl_basic_map_finalize(bmap
);
3049 isl_basic_map_free(bmap
);
3053 /* Add a constraint to "bmap" expressing i_pos <= o_pos
3055 static __isl_give isl_basic_map
*var_less_or_equal(
3056 __isl_take isl_basic_map
*bmap
, unsigned pos
)
3062 i
= isl_basic_map_alloc_inequality(bmap
);
3065 nparam
= isl_basic_map_n_param(bmap
);
3066 n_in
= isl_basic_map_n_in(bmap
);
3067 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3068 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
3069 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
3070 return isl_basic_map_finalize(bmap
);
3072 isl_basic_map_free(bmap
);
3076 /* Add a constraints to "bmap" expressing i_pos > o_pos
3078 static struct isl_basic_map
*var_more(struct isl_basic_map
*bmap
, unsigned pos
)
3084 i
= isl_basic_map_alloc_inequality(bmap
);
3087 nparam
= isl_basic_map_n_param(bmap
);
3088 n_in
= isl_basic_map_n_in(bmap
);
3089 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3090 isl_int_set_si(bmap
->ineq
[i
][0], -1);
3091 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
3092 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
3093 return isl_basic_map_finalize(bmap
);
3095 isl_basic_map_free(bmap
);
3099 /* Add a constraint to "bmap" expressing i_pos >= o_pos
3101 static __isl_give isl_basic_map
*var_more_or_equal(
3102 __isl_take isl_basic_map
*bmap
, unsigned pos
)
3108 i
= isl_basic_map_alloc_inequality(bmap
);
3111 nparam
= isl_basic_map_n_param(bmap
);
3112 n_in
= isl_basic_map_n_in(bmap
);
3113 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3114 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
3115 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
3116 return isl_basic_map_finalize(bmap
);
3118 isl_basic_map_free(bmap
);
3122 struct isl_basic_map
*isl_basic_map_equal(struct isl_dim
*dim
, unsigned n_equal
)
3125 struct isl_basic_map
*bmap
;
3126 bmap
= isl_basic_map_alloc_dim(dim
, 0, n_equal
, 0);
3129 for (i
= 0; i
< n_equal
&& bmap
; ++i
)
3130 bmap
= var_equal(bmap
, i
);
3131 return isl_basic_map_finalize(bmap
);
3134 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
3136 struct isl_basic_map
*isl_basic_map_less_at(struct isl_dim
*dim
, unsigned pos
)
3139 struct isl_basic_map
*bmap
;
3140 bmap
= isl_basic_map_alloc_dim(dim
, 0, pos
, 1);
3143 for (i
= 0; i
< pos
&& bmap
; ++i
)
3144 bmap
= var_equal(bmap
, i
);
3146 bmap
= var_less(bmap
, pos
);
3147 return isl_basic_map_finalize(bmap
);
3150 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
3152 __isl_give isl_basic_map
*isl_basic_map_less_or_equal_at(
3153 __isl_take isl_dim
*dim
, unsigned pos
)
3156 isl_basic_map
*bmap
;
3158 bmap
= isl_basic_map_alloc_dim(dim
, 0, pos
, 1);
3159 for (i
= 0; i
< pos
; ++i
)
3160 bmap
= var_equal(bmap
, i
);
3161 bmap
= var_less_or_equal(bmap
, pos
);
3162 return isl_basic_map_finalize(bmap
);
3165 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
3167 struct isl_basic_map
*isl_basic_map_more_at(struct isl_dim
*dim
, unsigned pos
)
3170 struct isl_basic_map
*bmap
;
3171 bmap
= isl_basic_map_alloc_dim(dim
, 0, pos
, 1);
3174 for (i
= 0; i
< pos
&& bmap
; ++i
)
3175 bmap
= var_equal(bmap
, i
);
3177 bmap
= var_more(bmap
, pos
);
3178 return isl_basic_map_finalize(bmap
);
3181 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
3183 __isl_give isl_basic_map
*isl_basic_map_more_or_equal_at(
3184 __isl_take isl_dim
*dim
, unsigned pos
)
3187 isl_basic_map
*bmap
;
3189 bmap
= isl_basic_map_alloc_dim(dim
, 0, pos
, 1);
3190 for (i
= 0; i
< pos
; ++i
)
3191 bmap
= var_equal(bmap
, i
);
3192 bmap
= var_more_or_equal(bmap
, pos
);
3193 return isl_basic_map_finalize(bmap
);
3196 static __isl_give isl_map
*map_lex_lte_first(__isl_take isl_dim
*dims
,
3197 unsigned n
, int equal
)
3199 struct isl_map
*map
;
3202 if (n
== 0 && equal
)
3203 return isl_map_universe(dims
);
3205 map
= isl_map_alloc_dim(isl_dim_copy(dims
), n
, ISL_MAP_DISJOINT
);
3207 for (i
= 0; i
+ 1 < n
; ++i
)
3208 map
= isl_map_add_basic_map(map
,
3209 isl_basic_map_less_at(isl_dim_copy(dims
), i
));
3212 map
= isl_map_add_basic_map(map
,
3213 isl_basic_map_less_or_equal_at(dims
, n
- 1));
3215 map
= isl_map_add_basic_map(map
,
3216 isl_basic_map_less_at(dims
, n
- 1));
3223 static __isl_give isl_map
*map_lex_lte(__isl_take isl_dim
*dims
, int equal
)
3227 return map_lex_lte_first(dims
, dims
->n_out
, equal
);
3230 __isl_give isl_map
*isl_map_lex_lt_first(__isl_take isl_dim
*dim
, unsigned n
)
3232 return map_lex_lte_first(dim
, n
, 0);
3235 __isl_give isl_map
*isl_map_lex_le_first(__isl_take isl_dim
*dim
, unsigned n
)
3237 return map_lex_lte_first(dim
, n
, 1);
3240 __isl_give isl_map
*isl_map_lex_lt(__isl_take isl_dim
*set_dim
)
3242 return map_lex_lte(isl_dim_map_from_set(set_dim
), 0);
3245 __isl_give isl_map
*isl_map_lex_le(__isl_take isl_dim
*set_dim
)
3247 return map_lex_lte(isl_dim_map_from_set(set_dim
), 1);
3250 static __isl_give isl_map
*map_lex_gte_first(__isl_take isl_dim
*dims
,
3251 unsigned n
, int equal
)
3253 struct isl_map
*map
;
3256 if (n
== 0 && equal
)
3257 return isl_map_universe(dims
);
3259 map
= isl_map_alloc_dim(isl_dim_copy(dims
), n
, ISL_MAP_DISJOINT
);
3261 for (i
= 0; i
+ 1 < n
; ++i
)
3262 map
= isl_map_add_basic_map(map
,
3263 isl_basic_map_more_at(isl_dim_copy(dims
), i
));
3266 map
= isl_map_add_basic_map(map
,
3267 isl_basic_map_more_or_equal_at(dims
, n
- 1));
3269 map
= isl_map_add_basic_map(map
,
3270 isl_basic_map_more_at(dims
, n
- 1));
3277 static __isl_give isl_map
*map_lex_gte(__isl_take isl_dim
*dims
, int equal
)
3281 return map_lex_gte_first(dims
, dims
->n_out
, equal
);
3284 __isl_give isl_map
*isl_map_lex_gt_first(__isl_take isl_dim
*dim
, unsigned n
)
3286 return map_lex_gte_first(dim
, n
, 0);
3289 __isl_give isl_map
*isl_map_lex_ge_first(__isl_take isl_dim
*dim
, unsigned n
)
3291 return map_lex_gte_first(dim
, n
, 1);
3294 __isl_give isl_map
*isl_map_lex_gt(__isl_take isl_dim
*set_dim
)
3296 return map_lex_gte(isl_dim_map_from_set(set_dim
), 0);
3299 __isl_give isl_map
*isl_map_lex_ge(__isl_take isl_dim
*set_dim
)
3301 return map_lex_gte(isl_dim_map_from_set(set_dim
), 1);
3304 __isl_give isl_map
*isl_set_lex_le_set(__isl_take isl_set
*set1
,
3305 __isl_take isl_set
*set2
)
3308 map
= isl_map_lex_le(isl_set_get_dim(set1
));
3309 map
= isl_map_intersect_domain(map
, set1
);
3310 map
= isl_map_intersect_range(map
, set2
);
3314 __isl_give isl_map
*isl_set_lex_lt_set(__isl_take isl_set
*set1
,
3315 __isl_take isl_set
*set2
)
3318 map
= isl_map_lex_lt(isl_set_get_dim(set1
));
3319 map
= isl_map_intersect_domain(map
, set1
);
3320 map
= isl_map_intersect_range(map
, set2
);
3324 __isl_give isl_map
*isl_set_lex_ge_set(__isl_take isl_set
*set1
,
3325 __isl_take isl_set
*set2
)
3328 map
= isl_map_lex_ge(isl_set_get_dim(set1
));
3329 map
= isl_map_intersect_domain(map
, set1
);
3330 map
= isl_map_intersect_range(map
, set2
);
3334 __isl_give isl_map
*isl_set_lex_gt_set(__isl_take isl_set
*set1
,
3335 __isl_take isl_set
*set2
)
3338 map
= isl_map_lex_gt(isl_set_get_dim(set1
));
3339 map
= isl_map_intersect_domain(map
, set1
);
3340 map
= isl_map_intersect_range(map
, set2
);
3344 __isl_give isl_map
*isl_map_lex_le_map(__isl_take isl_map
*map1
,
3345 __isl_take isl_map
*map2
)
3348 map
= isl_map_lex_le(isl_dim_range(isl_map_get_dim(map1
)));
3349 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
3350 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
3354 __isl_give isl_map
*isl_map_lex_lt_map(__isl_take isl_map
*map1
,
3355 __isl_take isl_map
*map2
)
3358 map
= isl_map_lex_lt(isl_dim_range(isl_map_get_dim(map1
)));
3359 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
3360 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
3364 __isl_give isl_map
*isl_map_lex_ge_map(__isl_take isl_map
*map1
,
3365 __isl_take isl_map
*map2
)
3368 map
= isl_map_lex_ge(isl_dim_range(isl_map_get_dim(map1
)));
3369 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
3370 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
3374 __isl_give isl_map
*isl_map_lex_gt_map(__isl_take isl_map
*map1
,
3375 __isl_take isl_map
*map2
)
3378 map
= isl_map_lex_gt(isl_dim_range(isl_map_get_dim(map1
)));
3379 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
3380 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
3384 struct isl_basic_map
*isl_basic_map_from_basic_set(
3385 struct isl_basic_set
*bset
, struct isl_dim
*dim
)
3387 struct isl_basic_map
*bmap
;
3389 bset
= isl_basic_set_cow(bset
);
3393 isl_assert(bset
->ctx
, isl_dim_compatible(bset
->dim
, dim
), goto error
);
3394 isl_dim_free(bset
->dim
);
3395 bmap
= (struct isl_basic_map
*) bset
;
3397 return isl_basic_map_finalize(bmap
);
3399 isl_basic_set_free(bset
);
3404 struct isl_basic_set
*isl_basic_set_from_basic_map(struct isl_basic_map
*bmap
)
3408 if (bmap
->dim
->n_in
== 0)
3409 return (struct isl_basic_set
*)bmap
;
3410 bmap
= isl_basic_map_cow(bmap
);
3413 bmap
->dim
= isl_dim_as_set_dim(bmap
->dim
);
3416 bmap
= isl_basic_map_finalize(bmap
);
3417 return (struct isl_basic_set
*)bmap
;
3419 isl_basic_map_free(bmap
);
3423 /* For a div d = floor(f/m), add the constraints
3426 * -(f-(n-1)) + m d >= 0
3428 * Note that the second constraint is the negation of
3432 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map
*bmap
,
3433 unsigned pos
, isl_int
*div
)
3436 unsigned total
= isl_basic_map_total_dim(bmap
);
3438 i
= isl_basic_map_alloc_inequality(bmap
);
3441 isl_seq_cpy(bmap
->ineq
[i
], div
+ 1, 1 + total
);
3442 isl_int_neg(bmap
->ineq
[i
][1 + pos
], div
[0]);
3444 j
= isl_basic_map_alloc_inequality(bmap
);
3447 isl_seq_neg(bmap
->ineq
[j
], bmap
->ineq
[i
], 1 + total
);
3448 isl_int_add(bmap
->ineq
[j
][0], bmap
->ineq
[j
][0], bmap
->ineq
[j
][1 + pos
]);
3449 isl_int_sub_ui(bmap
->ineq
[j
][0], bmap
->ineq
[j
][0], 1);
3453 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set
*bset
,
3454 unsigned pos
, isl_int
*div
)
3456 return isl_basic_map_add_div_constraints_var((isl_basic_map
*)bset
,
3460 int isl_basic_map_add_div_constraints(struct isl_basic_map
*bmap
, unsigned div
)
3462 unsigned total
= isl_basic_map_total_dim(bmap
);
3463 unsigned div_pos
= total
- bmap
->n_div
+ div
;
3465 return isl_basic_map_add_div_constraints_var(bmap
, div_pos
,
3469 struct isl_basic_set
*isl_basic_map_underlying_set(
3470 struct isl_basic_map
*bmap
)
3474 if (bmap
->dim
->nparam
== 0 && bmap
->dim
->n_in
== 0 &&
3476 !isl_dim_is_named_or_nested(bmap
->dim
, isl_dim_in
) &&
3477 !isl_dim_is_named_or_nested(bmap
->dim
, isl_dim_out
))
3478 return (struct isl_basic_set
*)bmap
;
3479 bmap
= isl_basic_map_cow(bmap
);
3482 bmap
->dim
= isl_dim_underlying(bmap
->dim
, bmap
->n_div
);
3485 bmap
->extra
-= bmap
->n_div
;
3487 bmap
= isl_basic_map_finalize(bmap
);
3488 return (struct isl_basic_set
*)bmap
;
3493 __isl_give isl_basic_set
*isl_basic_set_underlying_set(
3494 __isl_take isl_basic_set
*bset
)
3496 return isl_basic_map_underlying_set((isl_basic_map
*)bset
);
3499 struct isl_basic_map
*isl_basic_map_overlying_set(
3500 struct isl_basic_set
*bset
, struct isl_basic_map
*like
)
3502 struct isl_basic_map
*bmap
;
3503 struct isl_ctx
*ctx
;
3510 isl_assert(ctx
, bset
->n_div
== 0, goto error
);
3511 isl_assert(ctx
, isl_basic_set_n_param(bset
) == 0, goto error
);
3512 isl_assert(ctx
, bset
->dim
->n_out
== isl_basic_map_total_dim(like
),
3514 if (isl_dim_equal(bset
->dim
, like
->dim
) && like
->n_div
== 0) {
3515 isl_basic_map_free(like
);
3516 return (struct isl_basic_map
*)bset
;
3518 bset
= isl_basic_set_cow(bset
);
3521 total
= bset
->dim
->n_out
+ bset
->extra
;
3522 bmap
= (struct isl_basic_map
*)bset
;
3523 isl_dim_free(bmap
->dim
);
3524 bmap
->dim
= isl_dim_copy(like
->dim
);
3527 bmap
->n_div
= like
->n_div
;
3528 bmap
->extra
+= like
->n_div
;
3532 ltotal
= total
- bmap
->extra
+ like
->extra
;
3535 bmap
->block2
= isl_blk_extend(ctx
, bmap
->block2
,
3536 bmap
->extra
* (1 + 1 + total
));
3537 if (isl_blk_is_error(bmap
->block2
))
3539 div
= isl_realloc_array(ctx
, bmap
->div
, isl_int
*, bmap
->extra
);
3543 for (i
= 0; i
< bmap
->extra
; ++i
)
3544 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + 1 + total
);
3545 for (i
= 0; i
< like
->n_div
; ++i
) {
3546 isl_seq_cpy(bmap
->div
[i
], like
->div
[i
], 1 + 1 + ltotal
);
3547 isl_seq_clr(bmap
->div
[i
]+1+1+ltotal
, total
- ltotal
);
3549 bmap
= isl_basic_map_extend_constraints(bmap
,
3550 0, 2 * like
->n_div
);
3551 for (i
= 0; i
< like
->n_div
; ++i
) {
3552 if (isl_int_is_zero(bmap
->div
[i
][0]))
3554 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
3558 isl_basic_map_free(like
);
3559 bmap
= isl_basic_map_simplify(bmap
);
3560 bmap
= isl_basic_map_finalize(bmap
);
3563 isl_basic_map_free(like
);
3564 isl_basic_set_free(bset
);
3568 struct isl_basic_set
*isl_basic_set_from_underlying_set(
3569 struct isl_basic_set
*bset
, struct isl_basic_set
*like
)
3571 return (struct isl_basic_set
*)
3572 isl_basic_map_overlying_set(bset
, (struct isl_basic_map
*)like
);
3575 struct isl_set
*isl_set_from_underlying_set(
3576 struct isl_set
*set
, struct isl_basic_set
*like
)
3582 isl_assert(set
->ctx
, set
->dim
->n_out
== isl_basic_set_total_dim(like
),
3584 if (isl_dim_equal(set
->dim
, like
->dim
) && like
->n_div
== 0) {
3585 isl_basic_set_free(like
);
3588 set
= isl_set_cow(set
);
3591 for (i
= 0; i
< set
->n
; ++i
) {
3592 set
->p
[i
] = isl_basic_set_from_underlying_set(set
->p
[i
],
3593 isl_basic_set_copy(like
));
3597 isl_dim_free(set
->dim
);
3598 set
->dim
= isl_dim_copy(like
->dim
);
3601 isl_basic_set_free(like
);
3604 isl_basic_set_free(like
);
3609 struct isl_set
*isl_map_underlying_set(struct isl_map
*map
)
3613 map
= isl_map_cow(map
);
3616 map
->dim
= isl_dim_cow(map
->dim
);
3620 for (i
= 1; i
< map
->n
; ++i
)
3621 isl_assert(map
->ctx
, map
->p
[0]->n_div
== map
->p
[i
]->n_div
,
3623 for (i
= 0; i
< map
->n
; ++i
) {
3624 map
->p
[i
] = (struct isl_basic_map
*)
3625 isl_basic_map_underlying_set(map
->p
[i
]);
3630 map
->dim
= isl_dim_underlying(map
->dim
, 0);
3632 isl_dim_free(map
->dim
);
3633 map
->dim
= isl_dim_copy(map
->p
[0]->dim
);
3637 return (struct isl_set
*)map
;
3643 struct isl_set
*isl_set_to_underlying_set(struct isl_set
*set
)
3645 return (struct isl_set
*)isl_map_underlying_set((struct isl_map
*)set
);
3648 __isl_give isl_basic_map
*isl_basic_map_reset_dim(
3649 __isl_take isl_basic_map
*bmap
, __isl_take isl_dim
*dim
)
3651 bmap
= isl_basic_map_cow(bmap
);
3655 isl_dim_free(bmap
->dim
);
3658 bmap
= isl_basic_map_finalize(bmap
);
3662 isl_basic_map_free(bmap
);
3667 __isl_give isl_basic_set
*isl_basic_set_reset_dim(
3668 __isl_take isl_basic_set
*bset
, __isl_take isl_dim
*dim
)
3670 return (isl_basic_set
*)isl_basic_map_reset_dim((isl_basic_map
*)bset
,
3674 __isl_give isl_map
*isl_map_reset_dim(__isl_take isl_map
*map
,
3675 __isl_take isl_dim
*dim
)
3679 map
= isl_map_cow(map
);
3683 for (i
= 0; i
< map
->n
; ++i
) {
3684 map
->p
[i
] = isl_basic_map_reset_dim(map
->p
[i
],
3689 isl_dim_free(map
->dim
);
3699 __isl_give isl_set
*isl_set_reset_dim(__isl_take isl_set
*set
,
3700 __isl_take isl_dim
*dim
)
3702 return (struct isl_set
*) isl_map_reset_dim((struct isl_map
*)set
, dim
);
3705 struct isl_basic_set
*isl_basic_map_domain(struct isl_basic_map
*bmap
)
3708 struct isl_basic_set
*domain
;
3714 dim
= isl_dim_domain(isl_basic_map_get_dim(bmap
));
3716 n_in
= isl_basic_map_n_in(bmap
);
3717 n_out
= isl_basic_map_n_out(bmap
);
3718 domain
= isl_basic_set_from_basic_map(bmap
);
3719 domain
= isl_basic_set_project_out(domain
, isl_dim_set
, n_in
, n_out
);
3721 domain
= isl_basic_set_reset_dim(domain
, dim
);
3726 struct isl_basic_set
*isl_basic_map_range(struct isl_basic_map
*bmap
)
3728 return isl_basic_map_domain(isl_basic_map_reverse(bmap
));
3731 __isl_give isl_basic_map
*isl_basic_map_domain_map(
3732 __isl_take isl_basic_map
*bmap
)
3736 isl_basic_map
*domain
;
3737 isl_basic_set
*bset
;
3738 int nparam
, n_in
, n_out
;
3741 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
3742 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
3743 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
3745 dim
= isl_dim_from_range(isl_dim_domain(isl_basic_map_get_dim(bmap
)));
3746 domain
= isl_basic_map_universe(dim
);
3748 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
3749 bmap
= isl_basic_map_apply_range(bmap
, domain
);
3750 bmap
= isl_basic_map_extend_constraints(bmap
, n_in
, 0);
3752 total
= isl_basic_map_total_dim(bmap
);
3754 for (i
= 0; i
< n_in
; ++i
) {
3755 k
= isl_basic_map_alloc_equality(bmap
);
3758 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
3759 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], -1);
3760 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ n_out
+ i
], 1);
3763 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3764 return isl_basic_map_finalize(bmap
);
3766 isl_basic_map_free(bmap
);
3770 __isl_give isl_basic_map
*isl_basic_map_range_map(
3771 __isl_take isl_basic_map
*bmap
)
3775 isl_basic_map
*range
;
3776 isl_basic_set
*bset
;
3777 int nparam
, n_in
, n_out
;
3780 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
3781 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
3782 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
3784 dim
= isl_dim_from_range(isl_dim_range(isl_basic_map_get_dim(bmap
)));
3785 range
= isl_basic_map_universe(dim
);
3787 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
3788 bmap
= isl_basic_map_apply_range(bmap
, range
);
3789 bmap
= isl_basic_map_extend_constraints(bmap
, n_out
, 0);
3791 total
= isl_basic_map_total_dim(bmap
);
3793 for (i
= 0; i
< n_out
; ++i
) {
3794 k
= isl_basic_map_alloc_equality(bmap
);
3797 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
3798 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ i
], -1);
3799 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ n_out
+ i
], 1);
3802 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3803 return isl_basic_map_finalize(bmap
);
3805 isl_basic_map_free(bmap
);
3809 struct isl_set
*isl_map_range(struct isl_map
*map
)
3812 struct isl_set
*set
;
3816 if (isl_map_dim(map
, isl_dim_in
) == 0)
3817 return (isl_set
*)map
;
3819 map
= isl_map_cow(map
);
3823 set
= (struct isl_set
*) map
;
3824 set
->dim
= isl_dim_drop_inputs(set
->dim
, 0, set
->dim
->n_in
);
3827 for (i
= 0; i
< map
->n
; ++i
) {
3828 set
->p
[i
] = isl_basic_map_range(map
->p
[i
]);
3832 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
3833 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
3840 __isl_give isl_map
*isl_map_domain_map(__isl_take isl_map
*map
)
3843 isl_dim
*domain_dim
;
3845 map
= isl_map_cow(map
);
3849 domain_dim
= isl_dim_from_range(isl_dim_domain(isl_map_get_dim(map
)));
3850 map
->dim
= isl_dim_from_domain(isl_dim_wrap(map
->dim
));
3851 map
->dim
= isl_dim_join(map
->dim
, domain_dim
);
3854 for (i
= 0; i
< map
->n
; ++i
) {
3855 map
->p
[i
] = isl_basic_map_domain_map(map
->p
[i
]);
3859 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
3860 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3867 __isl_give isl_map
*isl_map_range_map(__isl_take isl_map
*map
)
3872 map
= isl_map_cow(map
);
3876 range_dim
= isl_dim_range(isl_map_get_dim(map
));
3877 map
->dim
= isl_dim_from_domain(isl_dim_wrap(map
->dim
));
3878 map
->dim
= isl_dim_join(map
->dim
, range_dim
);
3881 for (i
= 0; i
< map
->n
; ++i
) {
3882 map
->p
[i
] = isl_basic_map_range_map(map
->p
[i
]);
3886 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
3887 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3894 struct isl_map
*isl_map_from_set(struct isl_set
*set
, struct isl_dim
*dim
)
3897 struct isl_map
*map
= NULL
;
3899 set
= isl_set_cow(set
);
3902 isl_assert(set
->ctx
, isl_dim_compatible(set
->dim
, dim
), goto error
);
3903 map
= (struct isl_map
*)set
;
3904 for (i
= 0; i
< set
->n
; ++i
) {
3905 map
->p
[i
] = isl_basic_map_from_basic_set(
3906 set
->p
[i
], isl_dim_copy(dim
));
3910 isl_dim_free(map
->dim
);
3919 __isl_give isl_basic_map
*isl_basic_map_from_domain(
3920 __isl_take isl_basic_set
*bset
)
3922 return isl_basic_map_reverse(isl_basic_map_from_range(bset
));
3925 __isl_give isl_basic_map
*isl_basic_map_from_range(
3926 __isl_take isl_basic_set
*bset
)
3928 return (isl_basic_map
*)bset
;
3931 struct isl_map
*isl_map_from_range(struct isl_set
*set
)
3933 return (struct isl_map
*)set
;
3936 __isl_give isl_map
*isl_map_from_domain(__isl_take isl_set
*set
)
3938 return isl_map_reverse(isl_map_from_range(set
));
3941 __isl_give isl_basic_map
*isl_basic_map_from_domain_and_range(
3942 __isl_take isl_basic_set
*domain
, __isl_take isl_basic_set
*range
)
3944 return isl_basic_map_apply_range(isl_basic_map_from_domain(domain
),
3945 isl_basic_map_from_range(range
));
3948 __isl_give isl_map
*isl_map_from_domain_and_range(__isl_take isl_set
*domain
,
3949 __isl_take isl_set
*range
)
3951 return isl_map_apply_range(isl_map_from_domain(domain
),
3952 isl_map_from_range(range
));
3955 struct isl_set
*isl_set_from_map(struct isl_map
*map
)
3958 struct isl_set
*set
= NULL
;
3962 map
= isl_map_cow(map
);
3965 map
->dim
= isl_dim_as_set_dim(map
->dim
);
3968 set
= (struct isl_set
*)map
;
3969 for (i
= 0; i
< map
->n
; ++i
) {
3970 set
->p
[i
] = isl_basic_set_from_basic_map(map
->p
[i
]);
3980 struct isl_map
*isl_map_alloc_dim(struct isl_dim
*dim
, int n
, unsigned flags
)
3982 struct isl_map
*map
;
3986 isl_assert(dim
->ctx
, n
>= 0, return NULL
);
3987 map
= isl_alloc(dim
->ctx
, struct isl_map
,
3988 sizeof(struct isl_map
) +
3989 (n
- 1) * sizeof(struct isl_basic_map
*));
3993 map
->ctx
= dim
->ctx
;
3994 isl_ctx_ref(map
->ctx
);
4006 struct isl_map
*isl_map_alloc(struct isl_ctx
*ctx
,
4007 unsigned nparam
, unsigned in
, unsigned out
, int n
,
4010 struct isl_map
*map
;
4011 struct isl_dim
*dims
;
4013 dims
= isl_dim_alloc(ctx
, nparam
, in
, out
);
4017 map
= isl_map_alloc_dim(dims
, n
, flags
);
4021 struct isl_basic_map
*isl_basic_map_empty(struct isl_dim
*dim
)
4023 struct isl_basic_map
*bmap
;
4024 bmap
= isl_basic_map_alloc_dim(dim
, 0, 1, 0);
4025 bmap
= isl_basic_map_set_to_empty(bmap
);
4029 struct isl_basic_set
*isl_basic_set_empty(struct isl_dim
*dim
)
4031 struct isl_basic_set
*bset
;
4032 bset
= isl_basic_set_alloc_dim(dim
, 0, 1, 0);
4033 bset
= isl_basic_set_set_to_empty(bset
);
4037 struct isl_basic_map
*isl_basic_map_empty_like(struct isl_basic_map
*model
)
4039 struct isl_basic_map
*bmap
;
4042 bmap
= isl_basic_map_alloc_dim(isl_dim_copy(model
->dim
), 0, 1, 0);
4043 bmap
= isl_basic_map_set_to_empty(bmap
);
4047 struct isl_basic_map
*isl_basic_map_empty_like_map(struct isl_map
*model
)
4049 struct isl_basic_map
*bmap
;
4052 bmap
= isl_basic_map_alloc_dim(isl_dim_copy(model
->dim
), 0, 1, 0);
4053 bmap
= isl_basic_map_set_to_empty(bmap
);
4057 struct isl_basic_set
*isl_basic_set_empty_like(struct isl_basic_set
*model
)
4059 struct isl_basic_set
*bset
;
4062 bset
= isl_basic_set_alloc_dim(isl_dim_copy(model
->dim
), 0, 1, 0);
4063 bset
= isl_basic_set_set_to_empty(bset
);
4067 struct isl_basic_map
*isl_basic_map_universe(struct isl_dim
*dim
)
4069 struct isl_basic_map
*bmap
;
4070 bmap
= isl_basic_map_alloc_dim(dim
, 0, 0, 0);
4071 bmap
= isl_basic_map_finalize(bmap
);
4075 struct isl_basic_set
*isl_basic_set_universe(struct isl_dim
*dim
)
4077 struct isl_basic_set
*bset
;
4078 bset
= isl_basic_set_alloc_dim(dim
, 0, 0, 0);
4079 bset
= isl_basic_set_finalize(bset
);
4083 __isl_give isl_basic_map
*isl_basic_map_nat_universe(__isl_take isl_dim
*dim
)
4086 unsigned total
= isl_dim_total(dim
);
4087 isl_basic_map
*bmap
;
4089 bmap
= isl_basic_map_alloc_dim(dim
, 0, 0, total
);
4090 for (i
= 0; i
< total
; ++i
) {
4091 int k
= isl_basic_map_alloc_inequality(bmap
);
4094 isl_seq_clr(bmap
->ineq
[k
], 1 + total
);
4095 isl_int_set_si(bmap
->ineq
[k
][1 + i
], 1);
4099 isl_basic_map_free(bmap
);
4103 __isl_give isl_basic_set
*isl_basic_set_nat_universe(__isl_take isl_dim
*dim
)
4105 return isl_basic_map_nat_universe(dim
);
4108 __isl_give isl_map
*isl_map_nat_universe(__isl_take isl_dim
*dim
)
4110 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim
));
4113 __isl_give isl_set
*isl_set_nat_universe(__isl_take isl_dim
*dim
)
4115 return isl_map_nat_universe(dim
);
4118 __isl_give isl_basic_map
*isl_basic_map_universe_like(
4119 __isl_keep isl_basic_map
*model
)
4123 return isl_basic_map_alloc_dim(isl_dim_copy(model
->dim
), 0, 0, 0);
4126 struct isl_basic_set
*isl_basic_set_universe_like(struct isl_basic_set
*model
)
4130 return isl_basic_set_alloc_dim(isl_dim_copy(model
->dim
), 0, 0, 0);
4133 __isl_give isl_basic_set
*isl_basic_set_universe_like_set(
4134 __isl_keep isl_set
*model
)
4138 return isl_basic_set_alloc_dim(isl_dim_copy(model
->dim
), 0, 0, 0);
4141 struct isl_map
*isl_map_empty(struct isl_dim
*dim
)
4143 return isl_map_alloc_dim(dim
, 0, ISL_MAP_DISJOINT
);
4146 struct isl_map
*isl_map_empty_like(struct isl_map
*model
)
4150 return isl_map_alloc_dim(isl_dim_copy(model
->dim
), 0, ISL_MAP_DISJOINT
);
4153 struct isl_map
*isl_map_empty_like_basic_map(struct isl_basic_map
*model
)
4157 return isl_map_alloc_dim(isl_dim_copy(model
->dim
), 0, ISL_MAP_DISJOINT
);
4160 struct isl_set
*isl_set_empty(struct isl_dim
*dim
)
4162 return isl_set_alloc_dim(dim
, 0, ISL_MAP_DISJOINT
);
4165 struct isl_set
*isl_set_empty_like(struct isl_set
*model
)
4169 return isl_set_empty(isl_dim_copy(model
->dim
));
4172 struct isl_map
*isl_map_universe(struct isl_dim
*dim
)
4174 struct isl_map
*map
;
4177 map
= isl_map_alloc_dim(isl_dim_copy(dim
), 1, ISL_MAP_DISJOINT
);
4178 map
= isl_map_add_basic_map(map
, isl_basic_map_universe(dim
));
4182 struct isl_set
*isl_set_universe(struct isl_dim
*dim
)
4184 struct isl_set
*set
;
4187 set
= isl_set_alloc_dim(isl_dim_copy(dim
), 1, ISL_MAP_DISJOINT
);
4188 set
= isl_set_add_basic_set(set
, isl_basic_set_universe(dim
));
4192 __isl_give isl_set
*isl_set_universe_like(__isl_keep isl_set
*model
)
4196 return isl_set_universe(isl_dim_copy(model
->dim
));
4199 struct isl_map
*isl_map_dup(struct isl_map
*map
)
4202 struct isl_map
*dup
;
4206 dup
= isl_map_alloc_dim(isl_dim_copy(map
->dim
), map
->n
, map
->flags
);
4207 for (i
= 0; i
< map
->n
; ++i
)
4208 dup
= isl_map_add_basic_map(dup
, isl_basic_map_copy(map
->p
[i
]));
4212 __isl_give isl_map
*isl_map_add_basic_map(__isl_take isl_map
*map
,
4213 __isl_take isl_basic_map
*bmap
)
4217 if (isl_basic_map_fast_is_empty(bmap
)) {
4218 isl_basic_map_free(bmap
);
4221 isl_assert(map
->ctx
, isl_dim_equal(map
->dim
, bmap
->dim
), goto error
);
4222 isl_assert(map
->ctx
, map
->n
< map
->size
, goto error
);
4223 map
->p
[map
->n
] = bmap
;
4225 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4231 isl_basic_map_free(bmap
);
4235 void isl_map_free(struct isl_map
*map
)
4245 isl_ctx_deref(map
->ctx
);
4246 for (i
= 0; i
< map
->n
; ++i
)
4247 isl_basic_map_free(map
->p
[i
]);
4248 isl_dim_free(map
->dim
);
4252 struct isl_map
*isl_map_extend(struct isl_map
*base
,
4253 unsigned nparam
, unsigned n_in
, unsigned n_out
)
4257 base
= isl_map_cow(base
);
4261 base
->dim
= isl_dim_extend(base
->dim
, nparam
, n_in
, n_out
);
4264 for (i
= 0; i
< base
->n
; ++i
) {
4265 base
->p
[i
] = isl_basic_map_extend_dim(base
->p
[i
],
4266 isl_dim_copy(base
->dim
), 0, 0, 0);
4276 struct isl_set
*isl_set_extend(struct isl_set
*base
,
4277 unsigned nparam
, unsigned dim
)
4279 return (struct isl_set
*)isl_map_extend((struct isl_map
*)base
,
4283 static struct isl_basic_map
*isl_basic_map_fix_pos_si(
4284 struct isl_basic_map
*bmap
, unsigned pos
, int value
)
4288 bmap
= isl_basic_map_cow(bmap
);
4289 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
4290 j
= isl_basic_map_alloc_equality(bmap
);
4293 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
4294 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
4295 isl_int_set_si(bmap
->eq
[j
][0], value
);
4296 bmap
= isl_basic_map_simplify(bmap
);
4297 return isl_basic_map_finalize(bmap
);
4299 isl_basic_map_free(bmap
);
4303 static __isl_give isl_basic_map
*isl_basic_map_fix_pos(
4304 __isl_take isl_basic_map
*bmap
, unsigned pos
, isl_int value
)
4308 bmap
= isl_basic_map_cow(bmap
);
4309 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
4310 j
= isl_basic_map_alloc_equality(bmap
);
4313 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
4314 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
4315 isl_int_set(bmap
->eq
[j
][0], value
);
4316 bmap
= isl_basic_map_simplify(bmap
);
4317 return isl_basic_map_finalize(bmap
);
4319 isl_basic_map_free(bmap
);
4323 struct isl_basic_map
*isl_basic_map_fix_si(struct isl_basic_map
*bmap
,
4324 enum isl_dim_type type
, unsigned pos
, int value
)
4328 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
4329 return isl_basic_map_fix_pos_si(bmap
,
4330 isl_basic_map_offset(bmap
, type
) + pos
, value
);
4332 isl_basic_map_free(bmap
);
4336 __isl_give isl_basic_map
*isl_basic_map_fix(__isl_take isl_basic_map
*bmap
,
4337 enum isl_dim_type type
, unsigned pos
, isl_int value
)
4341 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
4342 return isl_basic_map_fix_pos(bmap
,
4343 isl_basic_map_offset(bmap
, type
) + pos
, value
);
4345 isl_basic_map_free(bmap
);
4349 struct isl_basic_set
*isl_basic_set_fix_si(struct isl_basic_set
*bset
,
4350 enum isl_dim_type type
, unsigned pos
, int value
)
4352 return (struct isl_basic_set
*)
4353 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
4357 __isl_give isl_basic_set
*isl_basic_set_fix(__isl_take isl_basic_set
*bset
,
4358 enum isl_dim_type type
, unsigned pos
, isl_int value
)
4360 return (struct isl_basic_set
*)
4361 isl_basic_map_fix((struct isl_basic_map
*)bset
,
4365 struct isl_basic_map
*isl_basic_map_fix_input_si(struct isl_basic_map
*bmap
,
4366 unsigned input
, int value
)
4368 return isl_basic_map_fix_si(bmap
, isl_dim_in
, input
, value
);
4371 struct isl_basic_set
*isl_basic_set_fix_dim_si(struct isl_basic_set
*bset
,
4372 unsigned dim
, int value
)
4374 return (struct isl_basic_set
*)
4375 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
4376 isl_dim_set
, dim
, value
);
4379 struct isl_map
*isl_map_fix_si(struct isl_map
*map
,
4380 enum isl_dim_type type
, unsigned pos
, int value
)
4384 map
= isl_map_cow(map
);
4388 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
4389 for (i
= 0; i
< map
->n
; ++i
) {
4390 map
->p
[i
] = isl_basic_map_fix_si(map
->p
[i
], type
, pos
, value
);
4394 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4401 __isl_give isl_set
*isl_set_fix_si(__isl_take isl_set
*set
,
4402 enum isl_dim_type type
, unsigned pos
, int value
)
4404 return (struct isl_set
*)
4405 isl_map_fix_si((struct isl_map
*)set
, type
, pos
, value
);
4408 __isl_give isl_map
*isl_map_fix(__isl_take isl_map
*map
,
4409 enum isl_dim_type type
, unsigned pos
, isl_int value
)
4413 map
= isl_map_cow(map
);
4417 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
4418 for (i
= 0; i
< map
->n
; ++i
) {
4419 map
->p
[i
] = isl_basic_map_fix(map
->p
[i
], type
, pos
, value
);
4423 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4430 __isl_give isl_set
*isl_set_fix(__isl_take isl_set
*set
,
4431 enum isl_dim_type type
, unsigned pos
, isl_int value
)
4433 return (struct isl_set
*)isl_map_fix((isl_map
*)set
, type
, pos
, value
);
4436 struct isl_map
*isl_map_fix_input_si(struct isl_map
*map
,
4437 unsigned input
, int value
)
4439 return isl_map_fix_si(map
, isl_dim_in
, input
, value
);
4442 struct isl_set
*isl_set_fix_dim_si(struct isl_set
*set
, unsigned dim
, int value
)
4444 return (struct isl_set
*)
4445 isl_map_fix_si((struct isl_map
*)set
, isl_dim_set
, dim
, value
);
4448 __isl_give isl_basic_map
*isl_basic_map_lower_bound_si(
4449 __isl_take isl_basic_map
*bmap
,
4450 enum isl_dim_type type
, unsigned pos
, int value
)
4456 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
4457 pos
+= isl_basic_map_offset(bmap
, type
);
4458 bmap
= isl_basic_map_cow(bmap
);
4459 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
4460 j
= isl_basic_map_alloc_inequality(bmap
);
4463 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
4464 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
4465 isl_int_set_si(bmap
->ineq
[j
][0], -value
);
4466 bmap
= isl_basic_map_simplify(bmap
);
4467 return isl_basic_map_finalize(bmap
);
4469 isl_basic_map_free(bmap
);
4473 struct isl_basic_set
*isl_basic_set_lower_bound_dim(struct isl_basic_set
*bset
,
4474 unsigned dim
, isl_int value
)
4478 bset
= isl_basic_set_cow(bset
);
4479 bset
= isl_basic_set_extend_constraints(bset
, 0, 1);
4480 j
= isl_basic_set_alloc_inequality(bset
);
4483 isl_seq_clr(bset
->ineq
[j
], 1 + isl_basic_set_total_dim(bset
));
4484 isl_int_set_si(bset
->ineq
[j
][1 + isl_basic_set_n_param(bset
) + dim
], 1);
4485 isl_int_neg(bset
->ineq
[j
][0], value
);
4486 bset
= isl_basic_set_simplify(bset
);
4487 return isl_basic_set_finalize(bset
);
4489 isl_basic_set_free(bset
);
4493 __isl_give isl_map
*isl_map_lower_bound_si(__isl_take isl_map
*map
,
4494 enum isl_dim_type type
, unsigned pos
, int value
)
4498 map
= isl_map_cow(map
);
4502 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
4503 for (i
= 0; i
< map
->n
; ++i
) {
4504 map
->p
[i
] = isl_basic_map_lower_bound_si(map
->p
[i
],
4509 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4516 __isl_give isl_set
*isl_set_lower_bound_si(__isl_take isl_set
*set
,
4517 enum isl_dim_type type
, unsigned pos
, int value
)
4519 return (struct isl_set
*)
4520 isl_map_lower_bound_si((struct isl_map
*)set
, type
, pos
, value
);
4523 struct isl_set
*isl_set_lower_bound_dim(struct isl_set
*set
, unsigned dim
,
4528 set
= isl_set_cow(set
);
4532 isl_assert(set
->ctx
, dim
< isl_set_n_dim(set
), goto error
);
4533 for (i
= 0; i
< set
->n
; ++i
) {
4534 set
->p
[i
] = isl_basic_set_lower_bound_dim(set
->p
[i
], dim
, value
);
4544 struct isl_map
*isl_map_reverse(struct isl_map
*map
)
4548 map
= isl_map_cow(map
);
4552 map
->dim
= isl_dim_reverse(map
->dim
);
4555 for (i
= 0; i
< map
->n
; ++i
) {
4556 map
->p
[i
] = isl_basic_map_reverse(map
->p
[i
]);
4560 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4567 static struct isl_map
*isl_basic_map_partial_lexopt(
4568 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
4569 struct isl_set
**empty
, int max
)
4573 if (bmap
->ctx
->opt
->pip
== ISL_PIP_PIP
)
4574 return isl_pip_basic_map_lexopt(bmap
, dom
, empty
, max
);
4576 return isl_tab_basic_map_partial_lexopt(bmap
, dom
, empty
, max
);
4578 isl_basic_map_free(bmap
);
4579 isl_basic_set_free(dom
);
4585 struct isl_map
*isl_basic_map_partial_lexmax(
4586 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
4587 struct isl_set
**empty
)
4589 return isl_basic_map_partial_lexopt(bmap
, dom
, empty
, 1);
4592 struct isl_map
*isl_basic_map_partial_lexmin(
4593 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
4594 struct isl_set
**empty
)
4596 return isl_basic_map_partial_lexopt(bmap
, dom
, empty
, 0);
4599 struct isl_set
*isl_basic_set_partial_lexmin(
4600 struct isl_basic_set
*bset
, struct isl_basic_set
*dom
,
4601 struct isl_set
**empty
)
4603 return (struct isl_set
*)
4604 isl_basic_map_partial_lexmin((struct isl_basic_map
*)bset
,
4608 struct isl_set
*isl_basic_set_partial_lexmax(
4609 struct isl_basic_set
*bset
, struct isl_basic_set
*dom
,
4610 struct isl_set
**empty
)
4612 return (struct isl_set
*)
4613 isl_basic_map_partial_lexmax((struct isl_basic_map
*)bset
,
4617 /* Given a basic map "bmap", compute the lexicographically minimal
4618 * (or maximal) image element for each domain element in dom.
4619 * Set *empty to those elements in dom that do not have an image element.
4621 * We first make sure the basic sets in dom are disjoint and then
4622 * simply collect the results over each of the basic sets separately.
4623 * We could probably improve the efficiency a bit by moving the union
4624 * domain down into the parametric integer programming.
4626 static __isl_give isl_map
*basic_map_partial_lexopt(
4627 __isl_take isl_basic_map
*bmap
, __isl_take isl_set
*dom
,
4628 __isl_give isl_set
**empty
, int max
)
4631 struct isl_map
*res
;
4633 dom
= isl_set_make_disjoint(dom
);
4637 if (isl_set_fast_is_empty(dom
)) {
4638 res
= isl_map_empty_like_basic_map(bmap
);
4639 *empty
= isl_set_empty_like(dom
);
4641 isl_basic_map_free(bmap
);
4645 res
= isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap
),
4646 isl_basic_set_copy(dom
->p
[0]), empty
, max
);
4648 for (i
= 1; i
< dom
->n
; ++i
) {
4649 struct isl_map
*res_i
;
4650 struct isl_set
*empty_i
;
4652 res_i
= isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap
),
4653 isl_basic_set_copy(dom
->p
[i
]), &empty_i
, max
);
4655 res
= isl_map_union_disjoint(res
, res_i
);
4656 *empty
= isl_set_union_disjoint(*empty
, empty_i
);
4660 isl_basic_map_free(bmap
);
4665 isl_basic_map_free(bmap
);
4669 /* Given a map "map", compute the lexicographically minimal
4670 * (or maximal) image element for each domain element in dom.
4671 * Set *empty to those elements in dom that do not have an image element.
4673 * We first compute the lexicographically minimal or maximal element
4674 * in the first basic map. This results in a partial solution "res"
4675 * and a subset "todo" of dom that still need to be handled.
4676 * We then consider each of the remaining maps in "map" and successively
4677 * improve both "res" and "todo".
4679 * Let res^k and todo^k be the results after k steps and let i = k + 1.
4680 * Assume we are computing the lexicographical maximum.
4681 * We first compute the lexicographically maximal element in basic map i.
4682 * This results in a partial solution res_i and a subset todo_i.
4683 * Then we combine these results with those obtain for the first k basic maps
4684 * to obtain a result that is valid for the first k+1 basic maps.
4685 * In particular, the set where there is no solution is the set where
4686 * there is no solution for the first k basic maps and also no solution
4687 * for the ith basic map, i.e.,
4689 * todo^i = todo^k * todo_i
4691 * On dom(res^k) * dom(res_i), we need to pick the larger of the two
4692 * solutions, arbitrarily breaking ties in favor of res^k.
4693 * That is, when res^k(a) >= res_i(a), we pick res^k and
4694 * when res^k(a) < res_i(a), we pick res_i. (Here, ">=" and "<" denote
4695 * the lexicographic order.)
4696 * In practice, we compute
4698 * res^k * (res_i . "<=")
4702 * res_i * (res^k . "<")
4704 * Finally, we consider the symmetric difference of dom(res^k) and dom(res_i),
4705 * where only one of res^k and res_i provides a solution and we simply pick
4712 * Note that we only compute these intersections when dom(res^k) intersects
4713 * dom(res_i). Otherwise, the only effect of these intersections is to
4714 * potentially break up res^k and res_i into smaller pieces.
4715 * We want to avoid such splintering as much as possible.
4716 * In fact, an earlier implementation of this function would look for
4717 * better results in the domain of res^k and for extra results in todo^k,
4718 * but this would always result in a splintering according to todo^k,
4719 * even when the domain of basic map i is disjoint from the domains of
4720 * the previous basic maps.
4722 static __isl_give isl_map
*isl_map_partial_lexopt(
4723 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
4724 __isl_give isl_set
**empty
, int max
)
4727 struct isl_map
*res
;
4728 struct isl_set
*todo
;
4733 if (isl_map_fast_is_empty(map
)) {
4741 res
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[0]),
4742 isl_set_copy(dom
), &todo
, max
);
4744 for (i
= 1; i
< map
->n
; ++i
) {
4748 isl_dim
*dim
= isl_dim_range(isl_map_get_dim(res
));
4750 res_i
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[i
]),
4751 isl_set_copy(dom
), &todo_i
, max
);
4754 lt
= isl_map_lex_lt(isl_dim_copy(dim
));
4755 le
= isl_map_lex_le(dim
);
4757 lt
= isl_map_lex_gt(isl_dim_copy(dim
));
4758 le
= isl_map_lex_ge(dim
);
4760 lt
= isl_map_apply_range(isl_map_copy(res
), lt
);
4761 lt
= isl_map_intersect(lt
, isl_map_copy(res_i
));
4762 le
= isl_map_apply_range(isl_map_copy(res_i
), le
);
4763 le
= isl_map_intersect(le
, isl_map_copy(res
));
4765 if (!isl_map_is_empty(lt
) || !isl_map_is_empty(le
)) {
4766 res
= isl_map_intersect_domain(res
,
4767 isl_set_copy(todo_i
));
4768 res_i
= isl_map_intersect_domain(res_i
,
4769 isl_set_copy(todo
));
4772 res
= isl_map_union_disjoint(res
, res_i
);
4773 res
= isl_map_union_disjoint(res
, lt
);
4774 res
= isl_map_union_disjoint(res
, le
);
4776 todo
= isl_set_intersect(todo
, todo_i
);
4796 __isl_give isl_map
*isl_map_partial_lexmax(
4797 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
4798 __isl_give isl_set
**empty
)
4800 return isl_map_partial_lexopt(map
, dom
, empty
, 1);
4803 __isl_give isl_map
*isl_map_partial_lexmin(
4804 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
4805 __isl_give isl_set
**empty
)
4807 return isl_map_partial_lexopt(map
, dom
, empty
, 0);
4810 __isl_give isl_set
*isl_set_partial_lexmin(
4811 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
4812 __isl_give isl_set
**empty
)
4814 return (struct isl_set
*)
4815 isl_map_partial_lexmin((struct isl_map
*)set
,
4819 __isl_give isl_set
*isl_set_partial_lexmax(
4820 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
4821 __isl_give isl_set
**empty
)
4823 return (struct isl_set
*)
4824 isl_map_partial_lexmax((struct isl_map
*)set
,
4828 __isl_give isl_map
*isl_basic_map_lexopt(__isl_take isl_basic_map
*bmap
, int max
)
4830 struct isl_basic_set
*dom
= NULL
;
4831 struct isl_dim
*dom_dim
;
4835 dom_dim
= isl_dim_domain(isl_dim_copy(bmap
->dim
));
4836 dom
= isl_basic_set_universe(dom_dim
);
4837 return isl_basic_map_partial_lexopt(bmap
, dom
, NULL
, max
);
4839 isl_basic_map_free(bmap
);
4843 __isl_give isl_map
*isl_basic_map_lexmin(__isl_take isl_basic_map
*bmap
)
4845 return isl_basic_map_lexopt(bmap
, 0);
4848 __isl_give isl_map
*isl_basic_map_lexmax(__isl_take isl_basic_map
*bmap
)
4850 return isl_basic_map_lexopt(bmap
, 1);
4853 __isl_give isl_set
*isl_basic_set_lexmin(__isl_take isl_basic_set
*bset
)
4855 return (isl_set
*)isl_basic_map_lexmin((isl_basic_map
*)bset
);
4858 __isl_give isl_set
*isl_basic_set_lexmax(__isl_take isl_basic_set
*bset
)
4860 return (isl_set
*)isl_basic_map_lexmax((isl_basic_map
*)bset
);
4863 __isl_give isl_map
*isl_map_lexopt(__isl_take isl_map
*map
, int max
)
4865 struct isl_set
*dom
= NULL
;
4866 struct isl_dim
*dom_dim
;
4870 dom_dim
= isl_dim_domain(isl_dim_copy(map
->dim
));
4871 dom
= isl_set_universe(dom_dim
);
4872 return isl_map_partial_lexopt(map
, dom
, NULL
, max
);
4878 __isl_give isl_map
*isl_map_lexmin(__isl_take isl_map
*map
)
4880 return isl_map_lexopt(map
, 0);
4883 __isl_give isl_map
*isl_map_lexmax(__isl_take isl_map
*map
)
4885 return isl_map_lexopt(map
, 1);
4888 __isl_give isl_set
*isl_set_lexmin(__isl_take isl_set
*set
)
4890 return (isl_set
*)isl_map_lexmin((isl_map
*)set
);
4893 __isl_give isl_set
*isl_set_lexmax(__isl_take isl_set
*set
)
4895 return (isl_set
*)isl_map_lexmax((isl_map
*)set
);
4898 /* Apply a preimage specified by "mat" on the parameters of "bset".
4899 * bset is assumed to have only parameters and divs.
4901 static struct isl_basic_set
*basic_set_parameter_preimage(
4902 struct isl_basic_set
*bset
, struct isl_mat
*mat
)
4909 bset
->dim
= isl_dim_cow(bset
->dim
);
4913 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
4915 isl_assert(bset
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
4917 bset
->dim
->nparam
= 0;
4918 bset
->dim
->n_out
= nparam
;
4919 bset
= isl_basic_set_preimage(bset
, mat
);
4921 bset
->dim
->nparam
= bset
->dim
->n_out
;
4922 bset
->dim
->n_out
= 0;
4927 isl_basic_set_free(bset
);
4931 /* Apply a preimage specified by "mat" on the parameters of "set".
4932 * set is assumed to have only parameters and divs.
4934 static struct isl_set
*set_parameter_preimage(
4935 struct isl_set
*set
, struct isl_mat
*mat
)
4937 struct isl_dim
*dim
= NULL
;
4943 dim
= isl_dim_copy(set
->dim
);
4944 dim
= isl_dim_cow(dim
);
4948 nparam
= isl_set_dim(set
, isl_dim_param
);
4950 isl_assert(set
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
4953 dim
->n_out
= nparam
;
4954 isl_set_reset_dim(set
, dim
);
4955 set
= isl_set_preimage(set
, mat
);
4958 dim
= isl_dim_copy(set
->dim
);
4959 dim
= isl_dim_cow(dim
);
4962 dim
->nparam
= dim
->n_out
;
4964 isl_set_reset_dim(set
, dim
);
4974 /* Intersect the basic set "bset" with the affine space specified by the
4975 * equalities in "eq".
4977 static struct isl_basic_set
*basic_set_append_equalities(
4978 struct isl_basic_set
*bset
, struct isl_mat
*eq
)
4986 bset
= isl_basic_set_extend_dim(bset
, isl_dim_copy(bset
->dim
), 0,
4991 len
= 1 + isl_dim_total(bset
->dim
) + bset
->extra
;
4992 for (i
= 0; i
< eq
->n_row
; ++i
) {
4993 k
= isl_basic_set_alloc_equality(bset
);
4996 isl_seq_cpy(bset
->eq
[k
], eq
->row
[i
], eq
->n_col
);
4997 isl_seq_clr(bset
->eq
[k
] + eq
->n_col
, len
- eq
->n_col
);
5001 bset
= isl_basic_set_gauss(bset
, NULL
);
5002 bset
= isl_basic_set_finalize(bset
);
5007 isl_basic_set_free(bset
);
5011 /* Intersect the set "set" with the affine space specified by the
5012 * equalities in "eq".
5014 static struct isl_set
*set_append_equalities(struct isl_set
*set
,
5022 for (i
= 0; i
< set
->n
; ++i
) {
5023 set
->p
[i
] = basic_set_append_equalities(set
->p
[i
],
5036 /* Project the given basic set onto its parameter domain, possibly introducing
5037 * new, explicit, existential variables in the constraints.
5038 * The input has parameters and (possibly implicit) existential variables.
5039 * The output has the same parameters, but only
5040 * explicit existentially quantified variables.
5042 * The actual projection is performed by pip, but pip doesn't seem
5043 * to like equalities very much, so we first remove the equalities
5044 * among the parameters by performing a variable compression on
5045 * the parameters. Afterward, an inverse transformation is performed
5046 * and the equalities among the parameters are inserted back in.
5048 static struct isl_set
*parameter_compute_divs(struct isl_basic_set
*bset
)
5052 struct isl_mat
*T
, *T2
;
5053 struct isl_set
*set
;
5054 unsigned nparam
, n_div
;
5056 bset
= isl_basic_set_cow(bset
);
5060 if (bset
->n_eq
== 0)
5061 return isl_basic_set_lexmin(bset
);
5063 isl_basic_set_gauss(bset
, NULL
);
5065 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
5066 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
5068 for (i
= 0, j
= n_div
- 1; i
< bset
->n_eq
&& j
>= 0; --j
) {
5069 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ j
]))
5072 if (i
== bset
->n_eq
)
5073 return isl_basic_set_lexmin(bset
);
5075 eq
= isl_mat_sub_alloc6(bset
->ctx
, bset
->eq
, i
, bset
->n_eq
- i
,
5077 eq
= isl_mat_cow(eq
);
5078 T
= isl_mat_variable_compression(isl_mat_copy(eq
), &T2
);
5079 if (T
&& T
->n_col
== 0) {
5083 bset
= isl_basic_set_set_to_empty(bset
);
5084 return isl_set_from_basic_set(bset
);
5086 bset
= basic_set_parameter_preimage(bset
, T
);
5088 set
= isl_basic_set_lexmin(bset
);
5089 set
= set_parameter_preimage(set
, T2
);
5090 set
= set_append_equalities(set
, eq
);
5094 /* Compute an explicit representation for all the existentially
5095 * quantified variables.
5096 * The input and output dimensions are first turned into parameters.
5097 * compute_divs then returns a map with the same parameters and
5098 * no input or output dimensions and the dimension specification
5099 * is reset to that of the input.
5101 static struct isl_map
*compute_divs(struct isl_basic_map
*bmap
)
5103 struct isl_basic_set
*bset
;
5104 struct isl_set
*set
;
5105 struct isl_map
*map
;
5106 struct isl_dim
*dim
, *orig_dim
= NULL
;
5111 bmap
= isl_basic_map_cow(bmap
);
5115 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5116 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5117 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5118 dim
= isl_dim_set_alloc(bmap
->ctx
, nparam
+ n_in
+ n_out
, 0);
5122 orig_dim
= bmap
->dim
;
5124 bset
= (struct isl_basic_set
*)bmap
;
5126 set
= parameter_compute_divs(bset
);
5127 map
= (struct isl_map
*)set
;
5128 map
= isl_map_reset_dim(map
, orig_dim
);
5132 isl_basic_map_free(bmap
);
5136 static int basic_map_divs_known(__isl_keep isl_basic_map
*bmap
)
5144 off
= isl_dim_total(bmap
->dim
);
5145 for (i
= 0; i
< bmap
->n_div
; ++i
) {
5146 if (isl_int_is_zero(bmap
->div
[i
][0]))
5148 isl_assert(bmap
->ctx
, isl_int_is_zero(bmap
->div
[i
][1+1+off
+i
]),
5154 static int map_divs_known(__isl_keep isl_map
*map
)
5161 for (i
= 0; i
< map
->n
; ++i
) {
5162 int known
= basic_map_divs_known(map
->p
[i
]);
5170 /* If bmap contains any unknown divs, then compute explicit
5171 * expressions for them. However, this computation may be
5172 * quite expensive, so first try to remove divs that aren't
5175 struct isl_map
*isl_basic_map_compute_divs(struct isl_basic_map
*bmap
)
5179 struct isl_map
*map
;
5181 known
= basic_map_divs_known(bmap
);
5185 return isl_map_from_basic_map(bmap
);
5187 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
5189 known
= basic_map_divs_known(bmap
);
5193 return isl_map_from_basic_map(bmap
);
5195 map
= compute_divs(bmap
);
5198 isl_basic_map_free(bmap
);
5202 struct isl_map
*isl_map_compute_divs(struct isl_map
*map
)
5206 struct isl_map
*res
;
5213 known
= map_divs_known(map
);
5221 res
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[0]));
5222 for (i
= 1 ; i
< map
->n
; ++i
) {
5224 r2
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[i
]));
5225 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
5226 res
= isl_map_union_disjoint(res
, r2
);
5228 res
= isl_map_union(res
, r2
);
5235 struct isl_set
*isl_basic_set_compute_divs(struct isl_basic_set
*bset
)
5237 return (struct isl_set
*)
5238 isl_basic_map_compute_divs((struct isl_basic_map
*)bset
);
5241 struct isl_set
*isl_set_compute_divs(struct isl_set
*set
)
5243 return (struct isl_set
*)
5244 isl_map_compute_divs((struct isl_map
*)set
);
5247 struct isl_set
*isl_map_domain(struct isl_map
*map
)
5250 struct isl_set
*set
;
5255 map
= isl_map_cow(map
);
5259 set
= (struct isl_set
*)map
;
5260 set
->dim
= isl_dim_domain(set
->dim
);
5263 for (i
= 0; i
< map
->n
; ++i
) {
5264 set
->p
[i
] = isl_basic_map_domain(map
->p
[i
]);
5268 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
5269 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
5276 struct isl_map
*isl_map_union_disjoint(
5277 struct isl_map
*map1
, struct isl_map
*map2
)
5281 struct isl_map
*map
= NULL
;
5295 isl_assert(map1
->ctx
, isl_dim_equal(map1
->dim
, map2
->dim
), goto error
);
5297 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
5298 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
5299 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
5301 map
= isl_map_alloc_dim(isl_dim_copy(map1
->dim
),
5302 map1
->n
+ map2
->n
, flags
);
5305 for (i
= 0; i
< map1
->n
; ++i
) {
5306 map
= isl_map_add_basic_map(map
,
5307 isl_basic_map_copy(map1
->p
[i
]));
5311 for (i
= 0; i
< map2
->n
; ++i
) {
5312 map
= isl_map_add_basic_map(map
,
5313 isl_basic_map_copy(map2
->p
[i
]));
5327 struct isl_map
*isl_map_union(struct isl_map
*map1
, struct isl_map
*map2
)
5329 map1
= isl_map_union_disjoint(map1
, map2
);
5333 ISL_F_CLR(map1
, ISL_MAP_DISJOINT
);
5337 struct isl_set
*isl_set_union_disjoint(
5338 struct isl_set
*set1
, struct isl_set
*set2
)
5340 return (struct isl_set
*)
5341 isl_map_union_disjoint(
5342 (struct isl_map
*)set1
, (struct isl_map
*)set2
);
5345 struct isl_set
*isl_set_union(struct isl_set
*set1
, struct isl_set
*set2
)
5347 return (struct isl_set
*)
5348 isl_map_union((struct isl_map
*)set1
, (struct isl_map
*)set2
);
5351 struct isl_map
*isl_map_intersect_range(
5352 struct isl_map
*map
, struct isl_set
*set
)
5355 struct isl_map
*result
;
5361 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
) &&
5362 ISL_F_ISSET(set
, ISL_MAP_DISJOINT
))
5363 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
5365 result
= isl_map_alloc_dim(isl_dim_copy(map
->dim
),
5366 map
->n
* set
->n
, flags
);
5369 for (i
= 0; i
< map
->n
; ++i
)
5370 for (j
= 0; j
< set
->n
; ++j
) {
5371 result
= isl_map_add_basic_map(result
,
5372 isl_basic_map_intersect_range(
5373 isl_basic_map_copy(map
->p
[i
]),
5374 isl_basic_set_copy(set
->p
[j
])));
5387 struct isl_map
*isl_map_intersect_domain(
5388 struct isl_map
*map
, struct isl_set
*set
)
5390 return isl_map_reverse(
5391 isl_map_intersect_range(isl_map_reverse(map
), set
));
5394 struct isl_map
*isl_map_apply_domain(
5395 struct isl_map
*map1
, struct isl_map
*map2
)
5399 map1
= isl_map_reverse(map1
);
5400 map1
= isl_map_apply_range(map1
, map2
);
5401 return isl_map_reverse(map1
);
5408 struct isl_map
*isl_map_apply_range(
5409 struct isl_map
*map1
, struct isl_map
*map2
)
5411 struct isl_dim
*dim_result
;
5412 struct isl_map
*result
;
5418 dim_result
= isl_dim_join(isl_dim_copy(map1
->dim
),
5419 isl_dim_copy(map2
->dim
));
5421 result
= isl_map_alloc_dim(dim_result
, map1
->n
* map2
->n
, 0);
5424 for (i
= 0; i
< map1
->n
; ++i
)
5425 for (j
= 0; j
< map2
->n
; ++j
) {
5426 result
= isl_map_add_basic_map(result
,
5427 isl_basic_map_apply_range(
5428 isl_basic_map_copy(map1
->p
[i
]),
5429 isl_basic_map_copy(map2
->p
[j
])));
5435 if (result
&& result
->n
<= 1)
5436 ISL_F_SET(result
, ISL_MAP_DISJOINT
);
5445 * returns range - domain
5447 struct isl_basic_set
*isl_basic_map_deltas(struct isl_basic_map
*bmap
)
5449 isl_dim
*dims
, *target_dim
;
5450 struct isl_basic_set
*bset
;
5457 isl_assert(bmap
->ctx
, isl_dim_tuple_match(bmap
->dim
, isl_dim_in
,
5458 bmap
->dim
, isl_dim_out
),
5460 target_dim
= isl_dim_domain(isl_basic_map_get_dim(bmap
));
5461 dim
= isl_basic_map_n_in(bmap
);
5462 nparam
= isl_basic_map_n_param(bmap
);
5463 bset
= isl_basic_set_from_basic_map(bmap
);
5464 bset
= isl_basic_set_cow(bset
);
5465 dims
= isl_basic_set_get_dim(bset
);
5466 dims
= isl_dim_add(dims
, isl_dim_set
, dim
);
5467 bset
= isl_basic_set_extend_dim(bset
, dims
, 0, dim
, 0);
5468 bset
= isl_basic_set_swap_vars(bset
, 2*dim
);
5469 for (i
= 0; i
< dim
; ++i
) {
5470 int j
= isl_basic_map_alloc_equality(
5471 (struct isl_basic_map
*)bset
);
5474 isl_seq_clr(bset
->eq
[j
], 1 + isl_basic_set_total_dim(bset
));
5475 isl_int_set_si(bset
->eq
[j
][1+nparam
+i
], 1);
5476 isl_int_set_si(bset
->eq
[j
][1+nparam
+dim
+i
], 1);
5477 isl_int_set_si(bset
->eq
[j
][1+nparam
+2*dim
+i
], -1);
5479 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, dim
, 2*dim
);
5480 bset
= isl_basic_set_reset_dim(bset
, target_dim
);
5483 isl_basic_map_free(bmap
);
5488 * returns range - domain
5490 struct isl_set
*isl_map_deltas(struct isl_map
*map
)
5494 struct isl_set
*result
;
5499 isl_assert(map
->ctx
, isl_dim_tuple_match(map
->dim
, isl_dim_in
,
5500 map
->dim
, isl_dim_out
),
5502 dim
= isl_map_get_dim(map
);
5503 dim
= isl_dim_domain(dim
);
5504 result
= isl_set_alloc_dim(dim
, map
->n
, 0);
5507 for (i
= 0; i
< map
->n
; ++i
)
5508 result
= isl_set_add_basic_set(result
,
5509 isl_basic_map_deltas(isl_basic_map_copy(map
->p
[i
])));
5518 * returns [domain -> range] -> range - domain
5520 __isl_give isl_basic_map
*isl_basic_map_deltas_map(
5521 __isl_take isl_basic_map
*bmap
)
5525 isl_basic_map
*domain
;
5526 isl_basic_set
*bset
;
5530 if (!isl_dim_tuple_match(bmap
->dim
, isl_dim_in
, bmap
->dim
, isl_dim_out
))
5531 isl_die(bmap
->ctx
, isl_error_invalid
,
5532 "domain and range don't match", goto error
);
5534 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5535 n
= isl_basic_map_dim(bmap
, isl_dim_in
);
5537 dim
= isl_dim_from_range(isl_dim_domain(isl_basic_map_get_dim(bmap
)));
5538 domain
= isl_basic_map_universe(dim
);
5540 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
5541 bmap
= isl_basic_map_apply_range(bmap
, domain
);
5542 bmap
= isl_basic_map_extend_constraints(bmap
, n
, 0);
5544 total
= isl_basic_map_total_dim(bmap
);
5546 for (i
= 0; i
< n
; ++i
) {
5547 k
= isl_basic_map_alloc_equality(bmap
);
5550 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
5551 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], 1);
5552 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ i
], -1);
5553 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ n
+ i
], 1);
5556 bmap
= isl_basic_map_gauss(bmap
, NULL
);
5557 return isl_basic_map_finalize(bmap
);
5559 isl_basic_map_free(bmap
);
5564 * returns [domain -> range] -> range - domain
5566 __isl_give isl_map
*isl_map_deltas_map(__isl_take isl_map
*map
)
5569 isl_dim
*domain_dim
;
5574 if (!isl_dim_tuple_match(map
->dim
, isl_dim_in
, map
->dim
, isl_dim_out
))
5575 isl_die(map
->ctx
, isl_error_invalid
,
5576 "domain and range don't match", goto error
);
5578 map
= isl_map_cow(map
);
5582 domain_dim
= isl_dim_from_range(isl_dim_domain(isl_map_get_dim(map
)));
5583 map
->dim
= isl_dim_from_domain(isl_dim_wrap(map
->dim
));
5584 map
->dim
= isl_dim_join(map
->dim
, domain_dim
);
5587 for (i
= 0; i
< map
->n
; ++i
) {
5588 map
->p
[i
] = isl_basic_map_deltas_map(map
->p
[i
]);
5592 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5599 static struct isl_basic_map
*basic_map_identity(struct isl_dim
*dims
)
5601 struct isl_basic_map
*bmap
;
5609 nparam
= dims
->nparam
;
5611 bmap
= isl_basic_map_alloc_dim(dims
, 0, dim
, 0);
5615 for (i
= 0; i
< dim
; ++i
) {
5616 int j
= isl_basic_map_alloc_equality(bmap
);
5619 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
5620 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
5621 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], -1);
5623 return isl_basic_map_finalize(bmap
);
5625 isl_basic_map_free(bmap
);
5629 __isl_give isl_basic_map
*isl_basic_map_identity(__isl_take isl_dim
*dim
)
5633 if (dim
->n_in
!= dim
->n_out
)
5634 isl_die(dim
->ctx
, isl_error_invalid
,
5635 "number of input and output dimensions needs to be "
5636 "the same", goto error
);
5637 return basic_map_identity(dim
);
5643 struct isl_basic_map
*isl_basic_map_identity_like(struct isl_basic_map
*model
)
5645 if (!model
|| !model
->dim
)
5647 return isl_basic_map_identity(isl_dim_copy(model
->dim
));
5650 __isl_give isl_map
*isl_map_identity(__isl_take isl_dim
*dim
)
5652 return isl_map_from_basic_map(isl_basic_map_identity(dim
));
5655 struct isl_map
*isl_map_identity_like(struct isl_map
*model
)
5657 if (!model
|| !model
->dim
)
5659 return isl_map_identity(isl_dim_copy(model
->dim
));
5662 struct isl_map
*isl_map_identity_like_basic_map(struct isl_basic_map
*model
)
5664 if (!model
|| !model
->dim
)
5666 return isl_map_identity(isl_dim_copy(model
->dim
));
5669 __isl_give isl_map
*isl_set_identity(__isl_take isl_set
*set
)
5671 isl_dim
*dim
= isl_set_get_dim(set
);
5673 id
= isl_map_identity(isl_dim_map_from_set(dim
));
5674 return isl_map_intersect_range(id
, set
);
5677 /* Construct a basic set with all set dimensions having only non-negative
5680 struct isl_basic_set
*isl_basic_set_positive_orthant(struct isl_dim
*dims
)
5685 struct isl_basic_set
*bset
;
5689 nparam
= dims
->nparam
;
5691 bset
= isl_basic_set_alloc_dim(dims
, 0, 0, dim
);
5694 for (i
= 0; i
< dim
; ++i
) {
5695 int k
= isl_basic_set_alloc_inequality(bset
);
5698 isl_seq_clr(bset
->ineq
[k
], 1 + isl_basic_set_total_dim(bset
));
5699 isl_int_set_si(bset
->ineq
[k
][1 + nparam
+ i
], 1);
5703 isl_basic_set_free(bset
);
5707 /* Construct the half-space x_pos >= 0.
5709 static __isl_give isl_basic_set
*nonneg_halfspace(__isl_take isl_dim
*dim
,
5713 isl_basic_set
*nonneg
;
5715 nonneg
= isl_basic_set_alloc_dim(dim
, 0, 0, 1);
5716 k
= isl_basic_set_alloc_inequality(nonneg
);
5719 isl_seq_clr(nonneg
->ineq
[k
], 1 + isl_basic_set_total_dim(nonneg
));
5720 isl_int_set_si(nonneg
->ineq
[k
][pos
], 1);
5722 return isl_basic_set_finalize(nonneg
);
5724 isl_basic_set_free(nonneg
);
5728 /* Construct the half-space x_pos <= -1.
5730 static __isl_give isl_basic_set
*neg_halfspace(__isl_take isl_dim
*dim
, int pos
)
5735 neg
= isl_basic_set_alloc_dim(dim
, 0, 0, 1);
5736 k
= isl_basic_set_alloc_inequality(neg
);
5739 isl_seq_clr(neg
->ineq
[k
], 1 + isl_basic_set_total_dim(neg
));
5740 isl_int_set_si(neg
->ineq
[k
][0], -1);
5741 isl_int_set_si(neg
->ineq
[k
][pos
], -1);
5743 return isl_basic_set_finalize(neg
);
5745 isl_basic_set_free(neg
);
5749 __isl_give isl_set
*isl_set_split_dims(__isl_take isl_set
*set
,
5750 enum isl_dim_type type
, unsigned first
, unsigned n
)
5753 isl_basic_set
*nonneg
;
5761 isl_assert(set
->ctx
, first
+ n
<= isl_set_dim(set
, type
), goto error
);
5763 for (i
= 0; i
< n
; ++i
) {
5764 nonneg
= nonneg_halfspace(isl_set_get_dim(set
),
5765 pos(set
->dim
, type
) + first
+ i
);
5766 neg
= neg_halfspace(isl_set_get_dim(set
),
5767 pos(set
->dim
, type
) + first
+ i
);
5769 set
= isl_set_intersect(set
, isl_basic_set_union(nonneg
, neg
));
5778 static int foreach_orthant(__isl_take isl_set
*set
, int *signs
, int first
,
5779 int len
, int (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
5786 if (isl_set_fast_is_empty(set
)) {
5791 return fn(set
, signs
, user
);
5794 half
= isl_set_from_basic_set(nonneg_halfspace(isl_set_get_dim(set
),
5796 half
= isl_set_intersect(half
, isl_set_copy(set
));
5797 if (foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
) < 0)
5801 half
= isl_set_from_basic_set(neg_halfspace(isl_set_get_dim(set
),
5803 half
= isl_set_intersect(half
, set
);
5804 return foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
);
5810 /* Call "fn" on the intersections of "set" with each of the orthants
5811 * (except for obviously empty intersections). The orthant is identified
5812 * by the signs array, with each entry having value 1 or -1 according
5813 * to the sign of the corresponding variable.
5815 int isl_set_foreach_orthant(__isl_keep isl_set
*set
,
5816 int (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
5826 if (isl_set_fast_is_empty(set
))
5829 nparam
= isl_set_dim(set
, isl_dim_param
);
5830 nvar
= isl_set_dim(set
, isl_dim_set
);
5832 signs
= isl_alloc_array(set
->ctx
, int, nparam
+ nvar
);
5834 r
= foreach_orthant(isl_set_copy(set
), signs
, 0, nparam
+ nvar
,
5842 int isl_set_is_equal(struct isl_set
*set1
, struct isl_set
*set2
)
5844 return isl_map_is_equal((struct isl_map
*)set1
, (struct isl_map
*)set2
);
5847 int isl_basic_map_is_subset(
5848 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
5851 struct isl_map
*map1
;
5852 struct isl_map
*map2
;
5854 if (!bmap1
|| !bmap2
)
5857 map1
= isl_map_from_basic_map(isl_basic_map_copy(bmap1
));
5858 map2
= isl_map_from_basic_map(isl_basic_map_copy(bmap2
));
5860 is_subset
= isl_map_is_subset(map1
, map2
);
5868 int isl_basic_map_is_equal(
5869 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
5873 if (!bmap1
|| !bmap2
)
5875 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
5878 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
5882 int isl_basic_set_is_equal(
5883 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
5885 return isl_basic_map_is_equal(
5886 (struct isl_basic_map
*)bset1
, (struct isl_basic_map
*)bset2
);
5889 int isl_map_is_empty(struct isl_map
*map
)
5896 for (i
= 0; i
< map
->n
; ++i
) {
5897 is_empty
= isl_basic_map_is_empty(map
->p
[i
]);
5906 int isl_map_fast_is_empty(struct isl_map
*map
)
5908 return map
? map
->n
== 0 : -1;
5911 int isl_set_fast_is_empty(struct isl_set
*set
)
5913 return set
? set
->n
== 0 : -1;
5916 int isl_set_is_empty(struct isl_set
*set
)
5918 return isl_map_is_empty((struct isl_map
*)set
);
5921 int isl_map_has_equal_dim(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
5926 return isl_dim_equal(map1
->dim
, map2
->dim
);
5929 int isl_set_has_equal_dim(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
5934 return isl_dim_equal(set1
->dim
, set2
->dim
);
5937 int isl_map_is_equal(struct isl_map
*map1
, struct isl_map
*map2
)
5943 is_subset
= isl_map_is_subset(map1
, map2
);
5946 is_subset
= isl_map_is_subset(map2
, map1
);
5950 int isl_basic_map_is_strict_subset(
5951 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
5955 if (!bmap1
|| !bmap2
)
5957 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
5960 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
5961 if (is_subset
== -1)
5966 int isl_map_is_strict_subset(struct isl_map
*map1
, struct isl_map
*map2
)
5972 is_subset
= isl_map_is_subset(map1
, map2
);
5975 is_subset
= isl_map_is_subset(map2
, map1
);
5976 if (is_subset
== -1)
5981 int isl_set_is_strict_subset(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
5983 return isl_map_is_strict_subset((isl_map
*)set1
, (isl_map
*)set2
);
5986 int isl_basic_map_is_universe(struct isl_basic_map
*bmap
)
5990 return bmap
->n_eq
== 0 && bmap
->n_ineq
== 0;
5993 int isl_basic_set_is_universe(struct isl_basic_set
*bset
)
5997 return bset
->n_eq
== 0 && bset
->n_ineq
== 0;
6000 int isl_map_fast_is_universe(__isl_keep isl_map
*map
)
6007 for (i
= 0; i
< map
->n
; ++i
) {
6008 int r
= isl_basic_map_is_universe(map
->p
[i
]);
6016 int isl_set_fast_is_universe(__isl_keep isl_set
*set
)
6018 return isl_map_fast_is_universe((isl_map
*) set
);
6021 int isl_basic_map_is_empty(struct isl_basic_map
*bmap
)
6023 struct isl_basic_set
*bset
= NULL
;
6024 struct isl_vec
*sample
= NULL
;
6031 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
6034 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
6035 struct isl_basic_map
*copy
= isl_basic_map_copy(bmap
);
6036 copy
= isl_basic_map_remove_redundancies(copy
);
6037 empty
= ISL_F_ISSET(copy
, ISL_BASIC_MAP_EMPTY
);
6038 isl_basic_map_free(copy
);
6042 total
= 1 + isl_basic_map_total_dim(bmap
);
6043 if (bmap
->sample
&& bmap
->sample
->size
== total
) {
6044 int contains
= isl_basic_map_contains(bmap
, bmap
->sample
);
6050 isl_vec_free(bmap
->sample
);
6051 bmap
->sample
= NULL
;
6052 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
6055 sample
= isl_basic_set_sample_vec(bset
);
6058 empty
= sample
->size
== 0;
6059 isl_vec_free(bmap
->sample
);
6060 bmap
->sample
= sample
;
6062 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
6067 int isl_basic_map_fast_is_empty(struct isl_basic_map
*bmap
)
6071 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
);
6074 int isl_basic_set_fast_is_empty(struct isl_basic_set
*bset
)
6078 return ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
);
6081 int isl_basic_set_is_empty(struct isl_basic_set
*bset
)
6083 return isl_basic_map_is_empty((struct isl_basic_map
*)bset
);
6086 struct isl_map
*isl_basic_map_union(
6087 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
6089 struct isl_map
*map
;
6090 if (!bmap1
|| !bmap2
)
6093 isl_assert(bmap1
->ctx
, isl_dim_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
6095 map
= isl_map_alloc_dim(isl_dim_copy(bmap1
->dim
), 2, 0);
6098 map
= isl_map_add_basic_map(map
, bmap1
);
6099 map
= isl_map_add_basic_map(map
, bmap2
);
6102 isl_basic_map_free(bmap1
);
6103 isl_basic_map_free(bmap2
);
6107 struct isl_set
*isl_basic_set_union(
6108 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
6110 return (struct isl_set
*)isl_basic_map_union(
6111 (struct isl_basic_map
*)bset1
,
6112 (struct isl_basic_map
*)bset2
);
6115 /* Order divs such that any div only depends on previous divs */
6116 struct isl_basic_map
*isl_basic_map_order_divs(struct isl_basic_map
*bmap
)
6124 off
= isl_dim_total(bmap
->dim
);
6126 for (i
= 0; i
< bmap
->n_div
; ++i
) {
6128 if (isl_int_is_zero(bmap
->div
[i
][0]))
6130 pos
= isl_seq_first_non_zero(bmap
->div
[i
]+1+1+off
+i
,
6134 isl_basic_map_swap_div(bmap
, i
, i
+ pos
);
6140 struct isl_basic_set
*isl_basic_set_order_divs(struct isl_basic_set
*bset
)
6142 return (struct isl_basic_set
*)
6143 isl_basic_map_order_divs((struct isl_basic_map
*)bset
);
6146 __isl_give isl_map
*isl_map_order_divs(__isl_take isl_map
*map
)
6153 for (i
= 0; i
< map
->n
; ++i
) {
6154 map
->p
[i
] = isl_basic_map_order_divs(map
->p
[i
]);
6165 /* Look for a div in dst that corresponds to the div "div" in src.
6166 * The divs before "div" in src and dst are assumed to be the same.
6168 * Returns -1 if no corresponding div was found and the position
6169 * of the corresponding div in dst otherwise.
6171 static int find_div(struct isl_basic_map
*dst
,
6172 struct isl_basic_map
*src
, unsigned div
)
6176 unsigned total
= isl_dim_total(src
->dim
);
6178 isl_assert(dst
->ctx
, div
<= dst
->n_div
, return -1);
6179 for (i
= div
; i
< dst
->n_div
; ++i
)
6180 if (isl_seq_eq(dst
->div
[i
], src
->div
[div
], 1+1+total
+div
) &&
6181 isl_seq_first_non_zero(dst
->div
[i
]+1+1+total
+div
,
6182 dst
->n_div
- div
) == -1)
6187 struct isl_basic_map
*isl_basic_map_align_divs(
6188 struct isl_basic_map
*dst
, struct isl_basic_map
*src
)
6191 unsigned total
= isl_dim_total(src
->dim
);
6196 if (src
->n_div
== 0)
6199 for (i
= 0; i
< src
->n_div
; ++i
)
6200 isl_assert(src
->ctx
, !isl_int_is_zero(src
->div
[i
][0]), goto error
);
6202 src
= isl_basic_map_order_divs(src
);
6203 dst
= isl_basic_map_cow(dst
);
6204 dst
= isl_basic_map_extend_dim(dst
, isl_dim_copy(dst
->dim
),
6205 src
->n_div
, 0, 2 * src
->n_div
);
6208 for (i
= 0; i
< src
->n_div
; ++i
) {
6209 int j
= find_div(dst
, src
, i
);
6211 j
= isl_basic_map_alloc_div(dst
);
6214 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
+i
);
6215 isl_seq_clr(dst
->div
[j
]+1+1+total
+i
, dst
->n_div
- i
);
6216 if (isl_basic_map_add_div_constraints(dst
, j
) < 0)
6220 isl_basic_map_swap_div(dst
, i
, j
);
6224 isl_basic_map_free(dst
);
6228 struct isl_basic_set
*isl_basic_set_align_divs(
6229 struct isl_basic_set
*dst
, struct isl_basic_set
*src
)
6231 return (struct isl_basic_set
*)isl_basic_map_align_divs(
6232 (struct isl_basic_map
*)dst
, (struct isl_basic_map
*)src
);
6235 struct isl_map
*isl_map_align_divs(struct isl_map
*map
)
6243 map
= isl_map_compute_divs(map
);
6244 map
= isl_map_cow(map
);
6248 for (i
= 1; i
< map
->n
; ++i
)
6249 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], map
->p
[i
]);
6250 for (i
= 1; i
< map
->n
; ++i
)
6251 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], map
->p
[0]);
6253 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6257 struct isl_set
*isl_set_align_divs(struct isl_set
*set
)
6259 return (struct isl_set
*)isl_map_align_divs((struct isl_map
*)set
);
6262 struct isl_set
*isl_set_apply(struct isl_set
*set
, struct isl_map
*map
)
6266 isl_assert(set
->ctx
, isl_map_compatible_domain(map
, set
), goto error
);
6267 map
= isl_map_intersect_domain(map
, set
);
6268 set
= isl_map_range(map
);
6276 /* There is no need to cow as removing empty parts doesn't change
6277 * the meaning of the set.
6279 struct isl_map
*isl_map_remove_empty_parts(struct isl_map
*map
)
6286 for (i
= map
->n
-1; i
>= 0; --i
) {
6287 if (!ISL_F_ISSET(map
->p
[i
], ISL_BASIC_MAP_EMPTY
))
6289 isl_basic_map_free(map
->p
[i
]);
6290 if (i
!= map
->n
-1) {
6291 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6292 map
->p
[i
] = map
->p
[map
->n
-1];
6300 struct isl_set
*isl_set_remove_empty_parts(struct isl_set
*set
)
6302 return (struct isl_set
*)
6303 isl_map_remove_empty_parts((struct isl_map
*)set
);
6306 struct isl_basic_map
*isl_map_copy_basic_map(struct isl_map
*map
)
6308 struct isl_basic_map
*bmap
;
6309 if (!map
|| map
->n
== 0)
6311 bmap
= map
->p
[map
->n
-1];
6312 isl_assert(map
->ctx
, ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
), return NULL
);
6313 return isl_basic_map_copy(bmap
);
6316 struct isl_basic_set
*isl_set_copy_basic_set(struct isl_set
*set
)
6318 return (struct isl_basic_set
*)
6319 isl_map_copy_basic_map((struct isl_map
*)set
);
6322 __isl_give isl_map
*isl_map_drop_basic_map(__isl_take isl_map
*map
,
6323 __isl_keep isl_basic_map
*bmap
)
6329 for (i
= map
->n
-1; i
>= 0; --i
) {
6330 if (map
->p
[i
] != bmap
)
6332 map
= isl_map_cow(map
);
6335 isl_basic_map_free(map
->p
[i
]);
6336 if (i
!= map
->n
-1) {
6337 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
6338 map
->p
[i
] = map
->p
[map
->n
-1];
6349 struct isl_set
*isl_set_drop_basic_set(struct isl_set
*set
,
6350 struct isl_basic_set
*bset
)
6352 return (struct isl_set
*)isl_map_drop_basic_map((struct isl_map
*)set
,
6353 (struct isl_basic_map
*)bset
);
6356 /* Given two basic sets bset1 and bset2, compute the maximal difference
6357 * between the values of dimension pos in bset1 and those in bset2
6358 * for any common value of the parameters and dimensions preceding pos.
6360 static enum isl_lp_result
basic_set_maximal_difference_at(
6361 __isl_keep isl_basic_set
*bset1
, __isl_keep isl_basic_set
*bset2
,
6362 int pos
, isl_int
*opt
)
6364 struct isl_dim
*dims
;
6365 struct isl_basic_map
*bmap1
= NULL
;
6366 struct isl_basic_map
*bmap2
= NULL
;
6367 struct isl_ctx
*ctx
;
6368 struct isl_vec
*obj
;
6371 unsigned dim1
, dim2
;
6372 enum isl_lp_result res
;
6374 if (!bset1
|| !bset2
)
6375 return isl_lp_error
;
6377 nparam
= isl_basic_set_n_param(bset1
);
6378 dim1
= isl_basic_set_n_dim(bset1
);
6379 dim2
= isl_basic_set_n_dim(bset2
);
6380 dims
= isl_dim_alloc(bset1
->ctx
, nparam
, pos
, dim1
- pos
);
6381 bmap1
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset1
), dims
);
6382 dims
= isl_dim_alloc(bset2
->ctx
, nparam
, pos
, dim2
- pos
);
6383 bmap2
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset2
), dims
);
6384 if (!bmap1
|| !bmap2
)
6386 bmap1
= isl_basic_map_cow(bmap1
);
6387 bmap1
= isl_basic_map_extend(bmap1
, nparam
,
6388 pos
, (dim1
- pos
) + (dim2
- pos
),
6389 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
6390 bmap1
= add_constraints(bmap1
, bmap2
, 0, dim1
- pos
);
6393 total
= isl_basic_map_total_dim(bmap1
);
6395 obj
= isl_vec_alloc(ctx
, 1 + total
);
6396 isl_seq_clr(obj
->block
.data
, 1 + total
);
6397 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
], 1);
6398 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
+(dim1
-pos
)], -1);
6401 res
= isl_basic_map_solve_lp(bmap1
, 1, obj
->block
.data
, ctx
->one
,
6403 isl_basic_map_free(bmap1
);
6407 isl_basic_map_free(bmap1
);
6408 isl_basic_map_free(bmap2
);
6409 return isl_lp_error
;
6412 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
6413 * for any common value of the parameters and dimensions preceding pos
6414 * in both basic sets, the values of dimension pos in bset1 are
6415 * smaller or larger than those in bset2.
6418 * 1 if bset1 follows bset2
6419 * -1 if bset1 precedes bset2
6420 * 0 if bset1 and bset2 are incomparable
6421 * -2 if some error occurred.
6423 int isl_basic_set_compare_at(struct isl_basic_set
*bset1
,
6424 struct isl_basic_set
*bset2
, int pos
)
6427 enum isl_lp_result res
;
6432 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
6434 if (res
== isl_lp_empty
)
6436 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
6437 res
== isl_lp_unbounded
)
6439 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
6448 /* Given two basic sets bset1 and bset2, check whether
6449 * for any common value of the parameters and dimensions preceding pos
6450 * there is a value of dimension pos in bset1 that is larger
6451 * than a value of the same dimension in bset2.
6454 * 1 if there exists such a pair
6455 * 0 if there is no such pair, but there is a pair of equal values
6457 * -2 if some error occurred.
6459 int isl_basic_set_follows_at(__isl_keep isl_basic_set
*bset1
,
6460 __isl_keep isl_basic_set
*bset2
, int pos
)
6463 enum isl_lp_result res
;
6468 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
6470 if (res
== isl_lp_empty
)
6472 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
6473 res
== isl_lp_unbounded
)
6475 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
6477 else if (res
== isl_lp_ok
)
6486 /* Given two sets set1 and set2, check whether
6487 * for any common value of the parameters and dimensions preceding pos
6488 * there is a value of dimension pos in set1 that is larger
6489 * than a value of the same dimension in set2.
6492 * 1 if there exists such a pair
6493 * 0 if there is no such pair, but there is a pair of equal values
6495 * -2 if some error occurred.
6497 int isl_set_follows_at(__isl_keep isl_set
*set1
,
6498 __isl_keep isl_set
*set2
, int pos
)
6506 for (i
= 0; i
< set1
->n
; ++i
)
6507 for (j
= 0; j
< set2
->n
; ++j
) {
6509 f
= isl_basic_set_follows_at(set1
->p
[i
], set2
->p
[j
], pos
);
6510 if (f
== 1 || f
== -2)
6519 static int isl_basic_map_fast_has_fixed_var(struct isl_basic_map
*bmap
,
6520 unsigned pos
, isl_int
*val
)
6528 total
= isl_basic_map_total_dim(bmap
);
6529 for (i
= 0, d
= total
-1; i
< bmap
->n_eq
&& d
+1 > pos
; ++i
) {
6530 for (; d
+1 > pos
; --d
)
6531 if (!isl_int_is_zero(bmap
->eq
[i
][1+d
]))
6535 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1, d
) != -1)
6537 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1+d
+1, total
-d
-1) != -1)
6539 if (!isl_int_is_one(bmap
->eq
[i
][1+d
]))
6542 isl_int_neg(*val
, bmap
->eq
[i
][0]);
6548 static int isl_map_fast_has_fixed_var(struct isl_map
*map
,
6549 unsigned pos
, isl_int
*val
)
6561 return isl_basic_map_fast_has_fixed_var(map
->p
[0], pos
, val
);
6564 fixed
= isl_basic_map_fast_has_fixed_var(map
->p
[0], pos
, &v
);
6565 for (i
= 1; fixed
== 1 && i
< map
->n
; ++i
) {
6566 fixed
= isl_basic_map_fast_has_fixed_var(map
->p
[i
], pos
, &tmp
);
6567 if (fixed
== 1 && isl_int_ne(tmp
, v
))
6571 isl_int_set(*val
, v
);
6577 static int isl_basic_set_fast_has_fixed_var(struct isl_basic_set
*bset
,
6578 unsigned pos
, isl_int
*val
)
6580 return isl_basic_map_fast_has_fixed_var((struct isl_basic_map
*)bset
,
6584 static int isl_set_fast_has_fixed_var(struct isl_set
*set
, unsigned pos
,
6587 return isl_map_fast_has_fixed_var((struct isl_map
*)set
, pos
, val
);
6590 int isl_basic_map_fast_is_fixed(struct isl_basic_map
*bmap
,
6591 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
6593 if (pos
>= isl_basic_map_dim(bmap
, type
))
6595 return isl_basic_map_fast_has_fixed_var(bmap
,
6596 isl_basic_map_offset(bmap
, type
) - 1 + pos
, val
);
6599 int isl_map_fast_is_fixed(struct isl_map
*map
,
6600 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
6602 if (pos
>= isl_map_dim(map
, type
))
6604 return isl_map_fast_has_fixed_var(map
,
6605 map_offset(map
, type
) - 1 + pos
, val
);
6608 /* Check if dimension dim has fixed value and if so and if val is not NULL,
6609 * then return this fixed value in *val.
6611 int isl_basic_set_fast_dim_is_fixed(struct isl_basic_set
*bset
, unsigned dim
,
6614 return isl_basic_set_fast_has_fixed_var(bset
,
6615 isl_basic_set_n_param(bset
) + dim
, val
);
6618 /* Check if dimension dim has fixed value and if so and if val is not NULL,
6619 * then return this fixed value in *val.
6621 int isl_set_fast_dim_is_fixed(struct isl_set
*set
, unsigned dim
, isl_int
*val
)
6623 return isl_set_fast_has_fixed_var(set
, isl_set_n_param(set
) + dim
, val
);
6626 /* Check if input variable in has fixed value and if so and if val is not NULL,
6627 * then return this fixed value in *val.
6629 int isl_map_fast_input_is_fixed(struct isl_map
*map
, unsigned in
, isl_int
*val
)
6631 return isl_map_fast_has_fixed_var(map
, isl_map_n_param(map
) + in
, val
);
6634 /* Check if dimension dim has an (obvious) fixed lower bound and if so
6635 * and if val is not NULL, then return this lower bound in *val.
6637 int isl_basic_set_fast_dim_has_fixed_lower_bound(struct isl_basic_set
*bset
,
6638 unsigned dim
, isl_int
*val
)
6640 int i
, i_eq
= -1, i_ineq
= -1;
6647 total
= isl_basic_set_total_dim(bset
);
6648 nparam
= isl_basic_set_n_param(bset
);
6649 for (i
= 0; i
< bset
->n_eq
; ++i
) {
6650 if (isl_int_is_zero(bset
->eq
[i
][1+nparam
+dim
]))
6656 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
6657 if (!isl_int_is_pos(bset
->ineq
[i
][1+nparam
+dim
]))
6659 if (i_eq
!= -1 || i_ineq
!= -1)
6663 if (i_eq
== -1 && i_ineq
== -1)
6665 c
= i_eq
!= -1 ? bset
->eq
[i_eq
] : bset
->ineq
[i_ineq
];
6666 /* The coefficient should always be one due to normalization. */
6667 if (!isl_int_is_one(c
[1+nparam
+dim
]))
6669 if (isl_seq_first_non_zero(c
+1, nparam
+dim
) != -1)
6671 if (isl_seq_first_non_zero(c
+1+nparam
+dim
+1,
6672 total
- nparam
- dim
- 1) != -1)
6675 isl_int_neg(*val
, c
[0]);
6679 int isl_set_fast_dim_has_fixed_lower_bound(struct isl_set
*set
,
6680 unsigned dim
, isl_int
*val
)
6692 return isl_basic_set_fast_dim_has_fixed_lower_bound(set
->p
[0],
6696 fixed
= isl_basic_set_fast_dim_has_fixed_lower_bound(set
->p
[0],
6698 for (i
= 1; fixed
== 1 && i
< set
->n
; ++i
) {
6699 fixed
= isl_basic_set_fast_dim_has_fixed_lower_bound(set
->p
[i
],
6701 if (fixed
== 1 && isl_int_ne(tmp
, v
))
6705 isl_int_set(*val
, v
);
6716 /* uset_gist depends on constraints without existentially quantified
6717 * variables sorting first.
6719 static int qsort_constraint_cmp(const void *p1
, const void *p2
)
6721 const struct constraint
*c1
= (const struct constraint
*)p1
;
6722 const struct constraint
*c2
= (const struct constraint
*)p2
;
6724 unsigned size
= isl_min(c1
->size
, c2
->size
);
6726 l1
= isl_seq_last_non_zero(c1
->c
, size
);
6727 l2
= isl_seq_last_non_zero(c2
->c
, size
);
6732 return isl_seq_cmp(c1
->c
, c2
->c
, size
);
6735 static struct isl_basic_map
*isl_basic_map_sort_constraints(
6736 struct isl_basic_map
*bmap
)
6739 struct constraint
*c
;
6744 total
= isl_basic_map_total_dim(bmap
);
6745 c
= isl_alloc_array(bmap
->ctx
, struct constraint
, bmap
->n_ineq
);
6748 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
6750 c
[i
].c
= bmap
->ineq
[i
];
6752 qsort(c
, bmap
->n_ineq
, sizeof(struct constraint
), qsort_constraint_cmp
);
6753 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
6754 bmap
->ineq
[i
] = c
[i
].c
;
6758 isl_basic_map_free(bmap
);
6762 __isl_give isl_basic_set
*isl_basic_set_sort_constraints(
6763 __isl_take isl_basic_set
*bset
)
6765 return (struct isl_basic_set
*)isl_basic_map_sort_constraints(
6766 (struct isl_basic_map
*)bset
);
6769 struct isl_basic_map
*isl_basic_map_normalize(struct isl_basic_map
*bmap
)
6773 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED
))
6775 bmap
= isl_basic_map_remove_redundancies(bmap
);
6776 bmap
= isl_basic_map_sort_constraints(bmap
);
6777 ISL_F_SET(bmap
, ISL_BASIC_MAP_NORMALIZED
);
6781 struct isl_basic_set
*isl_basic_set_normalize(struct isl_basic_set
*bset
)
6783 return (struct isl_basic_set
*)isl_basic_map_normalize(
6784 (struct isl_basic_map
*)bset
);
6787 int isl_basic_map_fast_cmp(const __isl_keep isl_basic_map
*bmap1
,
6788 const __isl_keep isl_basic_map
*bmap2
)
6795 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) !=
6796 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_RATIONAL
))
6797 return ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) ? -1 : 1;
6798 if (isl_basic_map_n_param(bmap1
) != isl_basic_map_n_param(bmap2
))
6799 return isl_basic_map_n_param(bmap1
) - isl_basic_map_n_param(bmap2
);
6800 if (isl_basic_map_n_in(bmap1
) != isl_basic_map_n_in(bmap2
))
6801 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
6802 if (isl_basic_map_n_out(bmap1
) != isl_basic_map_n_out(bmap2
))
6803 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
6804 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
) &&
6805 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
6807 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
))
6809 if (ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
6811 if (bmap1
->n_eq
!= bmap2
->n_eq
)
6812 return bmap1
->n_eq
- bmap2
->n_eq
;
6813 if (bmap1
->n_ineq
!= bmap2
->n_ineq
)
6814 return bmap1
->n_ineq
- bmap2
->n_ineq
;
6815 if (bmap1
->n_div
!= bmap2
->n_div
)
6816 return bmap1
->n_div
- bmap2
->n_div
;
6817 total
= isl_basic_map_total_dim(bmap1
);
6818 for (i
= 0; i
< bmap1
->n_eq
; ++i
) {
6819 cmp
= isl_seq_cmp(bmap1
->eq
[i
], bmap2
->eq
[i
], 1+total
);
6823 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
6824 cmp
= isl_seq_cmp(bmap1
->ineq
[i
], bmap2
->ineq
[i
], 1+total
);
6828 for (i
= 0; i
< bmap1
->n_div
; ++i
) {
6829 cmp
= isl_seq_cmp(bmap1
->div
[i
], bmap2
->div
[i
], 1+1+total
);
6836 int isl_basic_map_fast_is_equal(__isl_keep isl_basic_map
*bmap1
,
6837 __isl_keep isl_basic_map
*bmap2
)
6839 return isl_basic_map_fast_cmp(bmap1
, bmap2
) == 0;
6842 int isl_basic_set_fast_is_equal(__isl_keep isl_basic_set
*bset1
,
6843 __isl_keep isl_basic_set
*bset2
)
6845 return isl_basic_map_fast_is_equal((isl_basic_map
*)bset1
,
6846 (isl_basic_map
*)bset2
);
6849 static int qsort_bmap_cmp(const void *p1
, const void *p2
)
6851 const struct isl_basic_map
*bmap1
= *(const struct isl_basic_map
**)p1
;
6852 const struct isl_basic_map
*bmap2
= *(const struct isl_basic_map
**)p2
;
6854 return isl_basic_map_fast_cmp(bmap1
, bmap2
);
6857 /* We normalize in place, but if anything goes wrong we need
6858 * to return NULL, so we need to make sure we don't change the
6859 * meaning of any possible other copies of map.
6861 struct isl_map
*isl_map_normalize(struct isl_map
*map
)
6864 struct isl_basic_map
*bmap
;
6868 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
))
6870 for (i
= 0; i
< map
->n
; ++i
) {
6871 bmap
= isl_basic_map_normalize(isl_basic_map_copy(map
->p
[i
]));
6874 isl_basic_map_free(map
->p
[i
]);
6877 qsort(map
->p
, map
->n
, sizeof(struct isl_basic_map
*), qsort_bmap_cmp
);
6878 ISL_F_SET(map
, ISL_MAP_NORMALIZED
);
6879 map
= isl_map_remove_empty_parts(map
);
6882 for (i
= map
->n
- 1; i
>= 1; --i
) {
6883 if (!isl_basic_map_fast_is_equal(map
->p
[i
-1], map
->p
[i
]))
6885 isl_basic_map_free(map
->p
[i
-1]);
6886 for (j
= i
; j
< map
->n
; ++j
)
6887 map
->p
[j
-1] = map
->p
[j
];
6897 struct isl_set
*isl_set_normalize(struct isl_set
*set
)
6899 return (struct isl_set
*)isl_map_normalize((struct isl_map
*)set
);
6902 int isl_map_fast_is_equal(struct isl_map
*map1
, struct isl_map
*map2
)
6912 if (!isl_dim_equal(map1
->dim
, map2
->dim
))
6915 map1
= isl_map_copy(map1
);
6916 map2
= isl_map_copy(map2
);
6917 map1
= isl_map_normalize(map1
);
6918 map2
= isl_map_normalize(map2
);
6921 equal
= map1
->n
== map2
->n
;
6922 for (i
= 0; equal
&& i
< map1
->n
; ++i
) {
6923 equal
= isl_basic_map_fast_is_equal(map1
->p
[i
], map2
->p
[i
]);
6936 int isl_set_fast_is_equal(struct isl_set
*set1
, struct isl_set
*set2
)
6938 return isl_map_fast_is_equal((struct isl_map
*)set1
,
6939 (struct isl_map
*)set2
);
6942 /* Return an interval that ranges from min to max (inclusive)
6944 struct isl_basic_set
*isl_basic_set_interval(struct isl_ctx
*ctx
,
6945 isl_int min
, isl_int max
)
6948 struct isl_basic_set
*bset
= NULL
;
6950 bset
= isl_basic_set_alloc(ctx
, 0, 1, 0, 0, 2);
6954 k
= isl_basic_set_alloc_inequality(bset
);
6957 isl_int_set_si(bset
->ineq
[k
][1], 1);
6958 isl_int_neg(bset
->ineq
[k
][0], min
);
6960 k
= isl_basic_set_alloc_inequality(bset
);
6963 isl_int_set_si(bset
->ineq
[k
][1], -1);
6964 isl_int_set(bset
->ineq
[k
][0], max
);
6968 isl_basic_set_free(bset
);
6972 /* Return the Cartesian product of the basic sets in list (in the given order).
6974 __isl_give isl_basic_set
*isl_basic_set_list_product(
6975 __isl_take
struct isl_basic_set_list
*list
)
6983 struct isl_basic_set
*product
= NULL
;
6987 isl_assert(list
->ctx
, list
->n
> 0, goto error
);
6988 isl_assert(list
->ctx
, list
->p
[0], goto error
);
6989 nparam
= isl_basic_set_n_param(list
->p
[0]);
6990 dim
= isl_basic_set_n_dim(list
->p
[0]);
6991 extra
= list
->p
[0]->n_div
;
6992 n_eq
= list
->p
[0]->n_eq
;
6993 n_ineq
= list
->p
[0]->n_ineq
;
6994 for (i
= 1; i
< list
->n
; ++i
) {
6995 isl_assert(list
->ctx
, list
->p
[i
], goto error
);
6996 isl_assert(list
->ctx
,
6997 nparam
== isl_basic_set_n_param(list
->p
[i
]), goto error
);
6998 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
6999 extra
+= list
->p
[i
]->n_div
;
7000 n_eq
+= list
->p
[i
]->n_eq
;
7001 n_ineq
+= list
->p
[i
]->n_ineq
;
7003 product
= isl_basic_set_alloc(list
->ctx
, nparam
, dim
, extra
,
7008 for (i
= 0; i
< list
->n
; ++i
) {
7009 isl_basic_set_add_constraints(product
,
7010 isl_basic_set_copy(list
->p
[i
]), dim
);
7011 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
7013 isl_basic_set_list_free(list
);
7016 isl_basic_set_free(product
);
7017 isl_basic_set_list_free(list
);
7021 struct isl_basic_map
*isl_basic_map_product(
7022 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
7024 struct isl_dim
*dim_result
= NULL
;
7025 struct isl_basic_map
*bmap
;
7026 unsigned in1
, in2
, out1
, out2
, nparam
, total
, pos
;
7027 struct isl_dim_map
*dim_map1
, *dim_map2
;
7029 if (!bmap1
|| !bmap2
)
7032 isl_assert(bmap1
->ctx
, isl_dim_match(bmap1
->dim
, isl_dim_param
,
7033 bmap2
->dim
, isl_dim_param
), goto error
);
7034 dim_result
= isl_dim_product(isl_dim_copy(bmap1
->dim
),
7035 isl_dim_copy(bmap2
->dim
));
7037 in1
= isl_basic_map_n_in(bmap1
);
7038 in2
= isl_basic_map_n_in(bmap2
);
7039 out1
= isl_basic_map_n_out(bmap1
);
7040 out2
= isl_basic_map_n_out(bmap2
);
7041 nparam
= isl_basic_map_n_param(bmap1
);
7043 total
= nparam
+ in1
+ in2
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
7044 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
7045 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
7046 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
7047 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
7048 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
7049 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
7050 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
7051 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
7052 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
7053 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
7055 bmap
= isl_basic_map_alloc_dim(dim_result
,
7056 bmap1
->n_div
+ bmap2
->n_div
,
7057 bmap1
->n_eq
+ bmap2
->n_eq
,
7058 bmap1
->n_ineq
+ bmap2
->n_ineq
);
7059 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
7060 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
7061 bmap
= isl_basic_map_simplify(bmap
);
7062 return isl_basic_map_finalize(bmap
);
7064 isl_basic_map_free(bmap1
);
7065 isl_basic_map_free(bmap2
);
7069 __isl_give isl_basic_map
*isl_basic_map_flat_product(
7070 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
7072 isl_basic_map
*prod
;
7074 prod
= isl_basic_map_product(bmap1
, bmap2
);
7075 prod
= isl_basic_map_flatten(prod
);
7079 __isl_give isl_basic_set
*isl_basic_set_flat_product(
7080 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
7082 return isl_basic_map_flat_product(bset1
, bset2
);
7085 __isl_give isl_basic_map
*isl_basic_map_range_product(
7086 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
7088 isl_dim
*dim_result
= NULL
;
7089 isl_basic_map
*bmap
;
7090 unsigned in
, out1
, out2
, nparam
, total
, pos
;
7091 struct isl_dim_map
*dim_map1
, *dim_map2
;
7093 if (!bmap1
|| !bmap2
)
7096 dim_result
= isl_dim_range_product(isl_dim_copy(bmap1
->dim
),
7097 isl_dim_copy(bmap2
->dim
));
7099 in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
7100 out1
= isl_basic_map_n_out(bmap1
);
7101 out2
= isl_basic_map_n_out(bmap2
);
7102 nparam
= isl_basic_map_n_param(bmap1
);
7104 total
= nparam
+ in
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
7105 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
7106 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
7107 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
7108 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
7109 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
7110 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
7111 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in
);
7112 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
7113 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
7114 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
7116 bmap
= isl_basic_map_alloc_dim(dim_result
,
7117 bmap1
->n_div
+ bmap2
->n_div
,
7118 bmap1
->n_eq
+ bmap2
->n_eq
,
7119 bmap1
->n_ineq
+ bmap2
->n_ineq
);
7120 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
7121 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
7122 bmap
= isl_basic_map_simplify(bmap
);
7123 return isl_basic_map_finalize(bmap
);
7125 isl_basic_map_free(bmap1
);
7126 isl_basic_map_free(bmap2
);
7130 static __isl_give isl_map
*map_product(__isl_take isl_map
*map1
,
7131 __isl_take isl_map
*map2
,
7132 __isl_give isl_dim
*(*dim_product
)(__isl_take isl_dim
*left
,
7133 __isl_take isl_dim
*right
),
7134 __isl_give isl_basic_map
*(*basic_map_product
)(
7135 __isl_take isl_basic_map
*left
, __isl_take isl_basic_map
*right
))
7138 struct isl_map
*result
;
7144 isl_assert(map1
->ctx
, isl_dim_match(map1
->dim
, isl_dim_param
,
7145 map2
->dim
, isl_dim_param
), goto error
);
7147 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
7148 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
7149 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
7151 result
= isl_map_alloc_dim(dim_product(isl_dim_copy(map1
->dim
),
7152 isl_dim_copy(map2
->dim
)),
7153 map1
->n
* map2
->n
, flags
);
7156 for (i
= 0; i
< map1
->n
; ++i
)
7157 for (j
= 0; j
< map2
->n
; ++j
) {
7158 struct isl_basic_map
*part
;
7159 part
= basic_map_product(isl_basic_map_copy(map1
->p
[i
]),
7160 isl_basic_map_copy(map2
->p
[j
]));
7161 if (isl_basic_map_is_empty(part
))
7162 isl_basic_map_free(part
);
7164 result
= isl_map_add_basic_map(result
, part
);
7177 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
7179 struct isl_map
*isl_map_product(struct isl_map
*map1
, struct isl_map
*map2
)
7181 return map_product(map1
, map2
, &isl_dim_product
, &isl_basic_map_product
);
7184 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
7186 __isl_give isl_map
*isl_map_flat_product(__isl_take isl_map
*map1
,
7187 __isl_take isl_map
*map2
)
7191 prod
= isl_map_product(map1
, map2
);
7192 prod
= isl_map_flatten(prod
);
7196 /* Given two set A and B, construct its Cartesian product A x B.
7198 struct isl_set
*isl_set_product(struct isl_set
*set1
, struct isl_set
*set2
)
7200 return (struct isl_set
*)isl_map_product((struct isl_map
*)set1
,
7201 (struct isl_map
*)set2
);
7204 __isl_give isl_set
*isl_set_flat_product(__isl_take isl_set
*set1
,
7205 __isl_take isl_set
*set2
)
7207 return (isl_set
*)isl_map_flat_product((isl_map
*)set1
, (isl_map
*)set2
);
7210 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
7212 __isl_give isl_map
*isl_map_range_product(__isl_take isl_map
*map1
,
7213 __isl_take isl_map
*map2
)
7215 return map_product(map1
, map2
, &isl_dim_range_product
,
7216 &isl_basic_map_range_product
);
7219 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map
*bmap
)
7222 uint32_t hash
= isl_hash_init();
7227 bmap
= isl_basic_map_copy(bmap
);
7228 bmap
= isl_basic_map_normalize(bmap
);
7231 total
= isl_basic_map_total_dim(bmap
);
7232 isl_hash_byte(hash
, bmap
->n_eq
& 0xFF);
7233 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
7235 c_hash
= isl_seq_get_hash(bmap
->eq
[i
], 1 + total
);
7236 isl_hash_hash(hash
, c_hash
);
7238 isl_hash_byte(hash
, bmap
->n_ineq
& 0xFF);
7239 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
7241 c_hash
= isl_seq_get_hash(bmap
->ineq
[i
], 1 + total
);
7242 isl_hash_hash(hash
, c_hash
);
7244 isl_hash_byte(hash
, bmap
->n_div
& 0xFF);
7245 for (i
= 0; i
< bmap
->n_div
; ++i
) {
7247 if (isl_int_is_zero(bmap
->div
[i
][0]))
7249 isl_hash_byte(hash
, i
& 0xFF);
7250 c_hash
= isl_seq_get_hash(bmap
->div
[i
], 1 + 1 + total
);
7251 isl_hash_hash(hash
, c_hash
);
7253 isl_basic_map_free(bmap
);
7257 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set
*bset
)
7259 return isl_basic_map_get_hash((isl_basic_map
*)bset
);
7262 uint32_t isl_map_get_hash(__isl_keep isl_map
*map
)
7269 map
= isl_map_copy(map
);
7270 map
= isl_map_normalize(map
);
7274 hash
= isl_hash_init();
7275 for (i
= 0; i
< map
->n
; ++i
) {
7277 bmap_hash
= isl_basic_map_get_hash(map
->p
[i
]);
7278 isl_hash_hash(hash
, bmap_hash
);
7286 uint32_t isl_set_get_hash(__isl_keep isl_set
*set
)
7288 return isl_map_get_hash((isl_map
*)set
);
7291 /* Check if the value for dimension dim is completely determined
7292 * by the values of the other parameters and variables.
7293 * That is, check if dimension dim is involved in an equality.
7295 int isl_basic_set_dim_is_unique(struct isl_basic_set
*bset
, unsigned dim
)
7302 nparam
= isl_basic_set_n_param(bset
);
7303 for (i
= 0; i
< bset
->n_eq
; ++i
)
7304 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ dim
]))
7309 /* Check if the value for dimension dim is completely determined
7310 * by the values of the other parameters and variables.
7311 * That is, check if dimension dim is involved in an equality
7312 * for each of the subsets.
7314 int isl_set_dim_is_unique(struct isl_set
*set
, unsigned dim
)
7320 for (i
= 0; i
< set
->n
; ++i
) {
7322 unique
= isl_basic_set_dim_is_unique(set
->p
[i
], dim
);
7329 int isl_set_n_basic_set(__isl_keep isl_set
*set
)
7331 return set
? set
->n
: 0;
7334 int isl_map_foreach_basic_map(__isl_keep isl_map
*map
,
7335 int (*fn
)(__isl_take isl_basic_map
*bmap
, void *user
), void *user
)
7342 for (i
= 0; i
< map
->n
; ++i
)
7343 if (fn(isl_basic_map_copy(map
->p
[i
]), user
) < 0)
7349 int isl_set_foreach_basic_set(__isl_keep isl_set
*set
,
7350 int (*fn
)(__isl_take isl_basic_set
*bset
, void *user
), void *user
)
7357 for (i
= 0; i
< set
->n
; ++i
)
7358 if (fn(isl_basic_set_copy(set
->p
[i
]), user
) < 0)
7364 __isl_give isl_basic_set
*isl_basic_set_lift(__isl_take isl_basic_set
*bset
)
7366 struct isl_dim
*dim
;
7371 bset
= isl_basic_set_cow(bset
);
7375 dim
= isl_basic_set_get_dim(bset
);
7376 dim
= isl_dim_lift(dim
, bset
->n_div
);
7379 isl_dim_free(bset
->dim
);
7381 bset
->extra
-= bset
->n_div
;
7384 bset
= isl_basic_set_finalize(bset
);
7388 isl_basic_set_free(bset
);
7392 __isl_give isl_set
*isl_set_lift(__isl_take isl_set
*set
)
7395 struct isl_dim
*dim
;
7398 set
= isl_set_align_divs(set
);
7403 set
= isl_set_cow(set
);
7407 n_div
= set
->p
[0]->n_div
;
7408 dim
= isl_set_get_dim(set
);
7409 dim
= isl_dim_lift(dim
, n_div
);
7412 isl_dim_free(set
->dim
);
7415 for (i
= 0; i
< set
->n
; ++i
) {
7416 set
->p
[i
] = isl_basic_set_lift(set
->p
[i
]);
7427 __isl_give isl_map
*isl_set_lifting(__isl_take isl_set
*set
)
7429 struct isl_dim
*dim
;
7430 struct isl_basic_map
*bmap
;
7437 set
= isl_set_align_divs(set
);
7442 dim
= isl_set_get_dim(set
);
7443 if (set
->n
== 0 || set
->p
[0]->n_div
== 0) {
7445 return isl_map_identity(isl_dim_map_from_set(dim
));
7448 n_div
= set
->p
[0]->n_div
;
7449 dim
= isl_dim_map_from_set(dim
);
7450 n_param
= isl_dim_size(dim
, isl_dim_param
);
7451 n_set
= isl_dim_size(dim
, isl_dim_in
);
7452 dim
= isl_dim_extend(dim
, n_param
, n_set
, n_set
+ n_div
);
7453 bmap
= isl_basic_map_alloc_dim(dim
, 0, n_set
, 2 * n_div
);
7454 for (i
= 0; i
< n_set
; ++i
)
7455 bmap
= var_equal(bmap
, i
);
7457 total
= n_param
+ n_set
+ n_set
+ n_div
;
7458 for (i
= 0; i
< n_div
; ++i
) {
7459 k
= isl_basic_map_alloc_inequality(bmap
);
7462 isl_seq_cpy(bmap
->ineq
[k
], set
->p
[0]->div
[i
]+1, 1+n_param
);
7463 isl_seq_clr(bmap
->ineq
[k
]+1+n_param
, n_set
);
7464 isl_seq_cpy(bmap
->ineq
[k
]+1+n_param
+n_set
,
7465 set
->p
[0]->div
[i
]+1+1+n_param
, n_set
+ n_div
);
7466 isl_int_neg(bmap
->ineq
[k
][1+n_param
+n_set
+n_set
+i
],
7467 set
->p
[0]->div
[i
][0]);
7469 l
= isl_basic_map_alloc_inequality(bmap
);
7472 isl_seq_neg(bmap
->ineq
[l
], bmap
->ineq
[k
], 1 + total
);
7473 isl_int_add(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0],
7474 set
->p
[0]->div
[i
][0]);
7475 isl_int_sub_ui(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], 1);
7479 bmap
= isl_basic_map_simplify(bmap
);
7480 bmap
= isl_basic_map_finalize(bmap
);
7481 return isl_map_from_basic_map(bmap
);
7484 isl_basic_map_free(bmap
);
7488 int isl_basic_set_size(__isl_keep isl_basic_set
*bset
)
7496 dim
= isl_basic_set_total_dim(bset
);
7497 size
+= bset
->n_eq
* (1 + dim
);
7498 size
+= bset
->n_ineq
* (1 + dim
);
7499 size
+= bset
->n_div
* (2 + dim
);
7504 int isl_set_size(__isl_keep isl_set
*set
)
7512 for (i
= 0; i
< set
->n
; ++i
)
7513 size
+= isl_basic_set_size(set
->p
[i
]);
7518 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
7519 enum isl_dim_type type
, unsigned pos
)
7527 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), return -1);
7529 pos
+= isl_basic_map_offset(bmap
, type
);
7531 for (i
= 0; i
< bmap
->n_eq
; ++i
)
7532 if (!isl_int_is_zero(bmap
->eq
[i
][pos
]))
7536 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
7537 int sgn
= isl_int_sgn(bmap
->ineq
[i
][pos
]);
7544 return lower
&& upper
;
7547 int isl_map_dim_is_bounded(__isl_keep isl_map
*map
,
7548 enum isl_dim_type type
, unsigned pos
)
7555 for (i
= 0; i
< map
->n
; ++i
) {
7557 bounded
= isl_basic_map_dim_is_bounded(map
->p
[i
], type
, pos
);
7558 if (bounded
< 0 || !bounded
)
7565 /* Return 1 if the specified dim is involved in both an upper bound
7566 * and a lower bound.
7568 int isl_set_dim_is_bounded(__isl_keep isl_set
*set
,
7569 enum isl_dim_type type
, unsigned pos
)
7571 return isl_map_dim_is_bounded((isl_map
*)set
, type
, pos
);
7574 /* For each of the "n" variables starting at "first", determine
7575 * the sign of the variable and put the results in the first "n"
7576 * elements of the array "signs".
7578 * 1 means that the variable is non-negative
7579 * -1 means that the variable is non-positive
7580 * 0 means the variable attains both positive and negative values.
7582 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set
*bset
,
7583 unsigned first
, unsigned n
, int *signs
)
7585 isl_vec
*bound
= NULL
;
7586 struct isl_tab
*tab
= NULL
;
7587 struct isl_tab_undo
*snap
;
7590 if (!bset
|| !signs
)
7593 bound
= isl_vec_alloc(bset
->ctx
, 1 + isl_basic_set_total_dim(bset
));
7594 tab
= isl_tab_from_basic_set(bset
);
7598 isl_seq_clr(bound
->el
, bound
->size
);
7599 isl_int_set_si(bound
->el
[0], -1);
7601 snap
= isl_tab_snap(tab
);
7602 for (i
= 0; i
< n
; ++i
) {
7605 isl_int_set_si(bound
->el
[1 + first
+ i
], -1);
7606 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
7609 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
7610 if (isl_tab_rollback(tab
, snap
) < 0)
7618 isl_int_set_si(bound
->el
[1 + first
+ i
], 1);
7619 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
7622 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
7623 if (isl_tab_rollback(tab
, snap
) < 0)
7626 signs
[i
] = empty
? -1 : 0;
7630 isl_vec_free(bound
);
7634 isl_vec_free(bound
);
7638 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set
*bset
,
7639 enum isl_dim_type type
, unsigned first
, unsigned n
, int *signs
)
7641 if (!bset
|| !signs
)
7643 isl_assert(bset
->ctx
, first
+ n
<= isl_basic_set_dim(bset
, type
),
7646 first
+= pos(bset
->dim
, type
) - 1;
7647 return isl_basic_set_vars_get_sign(bset
, first
, n
, signs
);
7650 /* Check if the given basic map is obviously single-valued.
7651 * In particular, for each output dimension, check that there is
7652 * an equality that defines the output dimension in terms of
7653 * earlier dimensions.
7655 int isl_basic_map_fast_is_single_valued(__isl_keep isl_basic_map
*bmap
)
7665 total
= 1 + isl_basic_map_total_dim(bmap
);
7666 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
7667 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
7669 for (i
= 0; i
< n_out
; ++i
) {
7670 for (j
= 0; j
< bmap
->n_eq
; ++j
) {
7671 if (isl_int_is_zero(bmap
->eq
[j
][o_out
+ i
]))
7673 if (isl_seq_first_non_zero(bmap
->eq
[j
] + o_out
+ i
+ 1,
7674 total
- (o_out
+ i
+ 1)) == -1)
7677 if (j
>= bmap
->n_eq
)
7684 /* Check if the given map is obviously single-valued.
7686 int isl_map_fast_is_single_valued(__isl_keep isl_map
*map
)
7697 return isl_basic_map_fast_is_single_valued(map
->p
[0]);
7700 /* Check if the given map is single-valued.
7705 * and check if the result is a subset of the identity mapping.
7707 int isl_map_is_single_valued(__isl_keep isl_map
*map
)
7714 sv
= isl_map_fast_is_single_valued(map
);
7718 test
= isl_map_reverse(isl_map_copy(map
));
7719 test
= isl_map_apply_range(test
, isl_map_copy(map
));
7721 dim
= isl_dim_map_from_set(isl_dim_range(isl_map_get_dim(map
)));
7722 id
= isl_map_identity(dim
);
7724 sv
= isl_map_is_subset(test
, id
);
7732 int isl_map_is_bijective(__isl_keep isl_map
*map
)
7736 sv
= isl_map_is_single_valued(map
);
7740 map
= isl_map_copy(map
);
7741 map
= isl_map_reverse(map
);
7742 sv
= isl_map_is_single_valued(map
);
7748 int isl_set_is_singleton(__isl_keep isl_set
*set
)
7750 return isl_map_is_single_valued((isl_map
*)set
);
7753 int isl_map_is_translation(__isl_keep isl_map
*map
)
7758 delta
= isl_map_deltas(isl_map_copy(map
));
7759 ok
= isl_set_is_singleton(delta
);
7760 isl_set_free(delta
);
7765 static int unique(isl_int
*p
, unsigned pos
, unsigned len
)
7767 if (isl_seq_first_non_zero(p
, pos
) != -1)
7769 if (isl_seq_first_non_zero(p
+ pos
+ 1, len
- pos
- 1) != -1)
7774 int isl_basic_set_is_box(__isl_keep isl_basic_set
*bset
)
7783 if (isl_basic_set_dim(bset
, isl_dim_div
) != 0)
7786 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
7787 ovar
= isl_dim_offset(bset
->dim
, isl_dim_set
);
7788 for (j
= 0; j
< nvar
; ++j
) {
7789 int lower
= 0, upper
= 0;
7790 for (i
= 0; i
< bset
->n_eq
; ++i
) {
7791 if (isl_int_is_zero(bset
->eq
[i
][1 + ovar
+ j
]))
7793 if (!unique(bset
->eq
[i
] + 1 + ovar
, j
, nvar
))
7799 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
7800 if (isl_int_is_zero(bset
->ineq
[i
][1 + ovar
+ j
]))
7802 if (!unique(bset
->ineq
[i
] + 1 + ovar
, j
, nvar
))
7804 if (isl_int_is_pos(bset
->ineq
[i
][1 + ovar
+ j
]))
7809 if (!lower
|| !upper
)
7816 int isl_set_is_box(__isl_keep isl_set
*set
)
7823 return isl_basic_set_is_box(set
->p
[0]);
7826 int isl_basic_set_is_wrapping(__isl_keep isl_basic_set
*bset
)
7831 return isl_dim_is_wrapping(bset
->dim
);
7834 int isl_set_is_wrapping(__isl_keep isl_set
*set
)
7839 return isl_dim_is_wrapping(set
->dim
);
7842 __isl_give isl_basic_set
*isl_basic_map_wrap(__isl_take isl_basic_map
*bmap
)
7844 bmap
= isl_basic_map_cow(bmap
);
7848 bmap
->dim
= isl_dim_wrap(bmap
->dim
);
7852 bmap
= isl_basic_map_finalize(bmap
);
7854 return (isl_basic_set
*)bmap
;
7856 isl_basic_map_free(bmap
);
7860 __isl_give isl_set
*isl_map_wrap(__isl_take isl_map
*map
)
7864 map
= isl_map_cow(map
);
7868 for (i
= 0; i
< map
->n
; ++i
) {
7869 map
->p
[i
] = (isl_basic_map
*)isl_basic_map_wrap(map
->p
[i
]);
7873 map
->dim
= isl_dim_wrap(map
->dim
);
7877 return (isl_set
*)map
;
7883 __isl_give isl_basic_map
*isl_basic_set_unwrap(__isl_take isl_basic_set
*bset
)
7885 bset
= isl_basic_set_cow(bset
);
7889 bset
->dim
= isl_dim_unwrap(bset
->dim
);
7893 bset
= isl_basic_set_finalize(bset
);
7895 return (isl_basic_map
*)bset
;
7897 isl_basic_set_free(bset
);
7901 __isl_give isl_map
*isl_set_unwrap(__isl_take isl_set
*set
)
7908 if (!isl_set_is_wrapping(set
))
7909 isl_die(set
->ctx
, isl_error_invalid
, "not a wrapping set",
7912 set
= isl_set_cow(set
);
7916 for (i
= 0; i
< set
->n
; ++i
) {
7917 set
->p
[i
] = (isl_basic_set
*)isl_basic_set_unwrap(set
->p
[i
]);
7922 set
->dim
= isl_dim_unwrap(set
->dim
);
7926 return (isl_map
*)set
;
7932 __isl_give isl_basic_map
*isl_basic_map_reset(__isl_take isl_basic_map
*bmap
,
7933 enum isl_dim_type type
)
7938 if (!isl_dim_is_named_or_nested(bmap
->dim
, type
))
7941 bmap
= isl_basic_map_cow(bmap
);
7945 bmap
->dim
= isl_dim_reset(bmap
->dim
, type
);
7949 bmap
= isl_basic_map_finalize(bmap
);
7953 isl_basic_map_free(bmap
);
7957 __isl_give isl_map
*isl_map_reset(__isl_take isl_map
*map
,
7958 enum isl_dim_type type
)
7965 if (!isl_dim_is_named_or_nested(map
->dim
, type
))
7968 map
= isl_map_cow(map
);
7972 for (i
= 0; i
< map
->n
; ++i
) {
7973 map
->p
[i
] = isl_basic_map_reset(map
->p
[i
], type
);
7977 map
->dim
= isl_dim_reset(map
->dim
, type
);
7987 __isl_give isl_basic_map
*isl_basic_map_flatten(__isl_take isl_basic_map
*bmap
)
7992 if (!bmap
->dim
->nested
[0] && !bmap
->dim
->nested
[1])
7995 bmap
= isl_basic_map_cow(bmap
);
7999 bmap
->dim
= isl_dim_flatten(bmap
->dim
);
8003 bmap
= isl_basic_map_finalize(bmap
);
8007 isl_basic_map_free(bmap
);
8011 __isl_give isl_basic_set
*isl_basic_set_flatten(__isl_take isl_basic_set
*bset
)
8013 return (isl_basic_set
*)isl_basic_map_flatten((isl_basic_map
*)bset
);
8016 __isl_give isl_map
*isl_map_flatten(__isl_take isl_map
*map
)
8023 if (!map
->dim
->nested
[0] && !map
->dim
->nested
[1])
8026 map
= isl_map_cow(map
);
8030 for (i
= 0; i
< map
->n
; ++i
) {
8031 map
->p
[i
] = isl_basic_map_flatten(map
->p
[i
]);
8035 map
->dim
= isl_dim_flatten(map
->dim
);
8045 __isl_give isl_set
*isl_set_flatten(__isl_take isl_set
*set
)
8047 return (isl_set
*)isl_map_flatten((isl_map
*)set
);
8050 __isl_give isl_map
*isl_set_flatten_map(__isl_take isl_set
*set
)
8052 isl_dim
*dim
, *flat_dim
;
8055 dim
= isl_set_get_dim(set
);
8056 flat_dim
= isl_dim_flatten(isl_dim_copy(dim
));
8057 map
= isl_map_identity(isl_dim_join(isl_dim_reverse(dim
), flat_dim
));
8058 map
= isl_map_intersect_domain(map
, set
);
8063 /* Reorder the dimensions of "bmap" according to the given dim_map
8064 * and set the dimension specification to "dim".
8066 __isl_give isl_basic_map
*isl_basic_map_realign(__isl_take isl_basic_map
*bmap
,
8067 __isl_take isl_dim
*dim
, __isl_take
struct isl_dim_map
*dim_map
)
8071 bmap
= isl_basic_map_cow(bmap
);
8072 if (!bmap
|| !dim
|| !dim_map
)
8075 res
= isl_basic_map_alloc_dim(dim
,
8076 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
8077 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
8078 res
= isl_basic_map_finalize(res
);
8082 isl_basic_map_free(bmap
);
8087 /* Reorder the dimensions of "map" according to given reordering.
8089 __isl_give isl_map
*isl_map_realign(__isl_take isl_map
*map
,
8090 __isl_take isl_reordering
*r
)
8093 struct isl_dim_map
*dim_map
;
8095 map
= isl_map_cow(map
);
8096 dim_map
= isl_dim_map_from_reordering(r
);
8097 if (!map
|| !r
|| !dim_map
)
8100 for (i
= 0; i
< map
->n
; ++i
) {
8101 struct isl_dim_map
*dim_map_i
;
8103 dim_map_i
= isl_dim_map_extend(dim_map
, map
->p
[i
]);
8105 map
->p
[i
] = isl_basic_map_realign(map
->p
[i
],
8106 isl_dim_copy(r
->dim
), dim_map_i
);
8112 map
= isl_map_reset_dim(map
, isl_dim_copy(r
->dim
));
8114 isl_reordering_free(r
);
8120 isl_reordering_free(r
);
8124 __isl_give isl_set
*isl_set_realign(__isl_take isl_set
*set
,
8125 __isl_take isl_reordering
*r
)
8127 return (isl_set
*)isl_map_realign((isl_map
*)set
, r
);
8130 __isl_give isl_mat
*isl_basic_map_equalities_matrix(
8131 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
8132 enum isl_dim_type c2
, enum isl_dim_type c3
,
8133 enum isl_dim_type c4
, enum isl_dim_type c5
)
8135 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
8136 struct isl_mat
*mat
;
8142 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_eq
,
8143 isl_basic_map_total_dim(bmap
) + 1);
8146 for (i
= 0; i
< bmap
->n_eq
; ++i
)
8147 for (j
= 0, pos
= 0; j
< 5; ++j
) {
8148 int off
= isl_basic_map_offset(bmap
, c
[j
]);
8149 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
8150 isl_int_set(mat
->row
[i
][pos
],
8151 bmap
->eq
[i
][off
+ k
]);
8159 __isl_give isl_mat
*isl_basic_map_inequalities_matrix(
8160 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
8161 enum isl_dim_type c2
, enum isl_dim_type c3
,
8162 enum isl_dim_type c4
, enum isl_dim_type c5
)
8164 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
8165 struct isl_mat
*mat
;
8171 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_ineq
,
8172 isl_basic_map_total_dim(bmap
) + 1);
8175 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
8176 for (j
= 0, pos
= 0; j
< 5; ++j
) {
8177 int off
= isl_basic_map_offset(bmap
, c
[j
]);
8178 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
8179 isl_int_set(mat
->row
[i
][pos
],
8180 bmap
->ineq
[i
][off
+ k
]);
8188 __isl_give isl_basic_map
*isl_basic_map_from_constraint_matrices(
8189 __isl_take isl_dim
*dim
,
8190 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
8191 enum isl_dim_type c2
, enum isl_dim_type c3
,
8192 enum isl_dim_type c4
, enum isl_dim_type c5
)
8194 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
8195 isl_basic_map
*bmap
;
8201 if (!dim
|| !eq
|| !ineq
)
8204 if (eq
->n_col
!= ineq
->n_col
)
8205 isl_die(dim
->ctx
, isl_error_invalid
,
8206 "equalities and inequalities matrices should have "
8207 "same number of columns", goto error
);
8209 total
= 1 + isl_dim_total(dim
);
8211 if (eq
->n_col
< total
)
8212 isl_die(dim
->ctx
, isl_error_invalid
,
8213 "number of columns too small", goto error
);
8215 extra
= eq
->n_col
- total
;
8217 bmap
= isl_basic_map_alloc_dim(isl_dim_copy(dim
), extra
,
8218 eq
->n_row
, ineq
->n_row
);
8221 for (i
= 0; i
< extra
; ++i
) {
8222 k
= isl_basic_map_alloc_div(bmap
);
8225 isl_int_set_si(bmap
->div
[k
][0], 0);
8227 for (i
= 0; i
< eq
->n_row
; ++i
) {
8228 l
= isl_basic_map_alloc_equality(bmap
);
8231 for (j
= 0, pos
= 0; j
< 5; ++j
) {
8232 int off
= isl_basic_map_offset(bmap
, c
[j
]);
8233 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
8234 isl_int_set(bmap
->eq
[l
][off
+ k
],
8240 for (i
= 0; i
< ineq
->n_row
; ++i
) {
8241 l
= isl_basic_map_alloc_inequality(bmap
);
8244 for (j
= 0, pos
= 0; j
< 5; ++j
) {
8245 int off
= isl_basic_map_offset(bmap
, c
[j
]);
8246 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
8247 isl_int_set(bmap
->ineq
[l
][off
+ k
],
8266 __isl_give isl_mat
*isl_basic_set_equalities_matrix(
8267 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
8268 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
8270 return isl_basic_map_equalities_matrix((isl_basic_map
*)bset
,
8271 c1
, c2
, c3
, c4
, isl_dim_in
);
8274 __isl_give isl_mat
*isl_basic_set_inequalities_matrix(
8275 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
8276 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
8278 return isl_basic_map_inequalities_matrix((isl_basic_map
*)bset
,
8279 c1
, c2
, c3
, c4
, isl_dim_in
);
8282 __isl_give isl_basic_set
*isl_basic_set_from_constraint_matrices(
8283 __isl_take isl_dim
*dim
,
8284 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
8285 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
8287 return (isl_basic_set
*)
8288 isl_basic_map_from_constraint_matrices(dim
, eq
, ineq
,
8289 c1
, c2
, c3
, c4
, isl_dim_in
);
8292 int isl_basic_map_can_zip(__isl_keep isl_basic_map
*bmap
)
8297 return isl_dim_can_zip(bmap
->dim
);
8300 int isl_map_can_zip(__isl_keep isl_map
*map
)
8305 return isl_dim_can_zip(map
->dim
);
8308 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
8309 * (A -> C) -> (B -> D).
8311 __isl_give isl_basic_map
*isl_basic_map_zip(__isl_take isl_basic_map
*bmap
)
8320 if (!isl_basic_map_can_zip(bmap
))
8321 isl_die(bmap
->ctx
, isl_error_invalid
,
8322 "basic map cannot be zipped", goto error
);
8323 pos
= isl_basic_map_offset(bmap
, isl_dim_in
) +
8324 isl_dim_size(bmap
->dim
->nested
[0], isl_dim_in
);
8325 n1
= isl_dim_size(bmap
->dim
->nested
[0], isl_dim_out
);
8326 n2
= isl_dim_size(bmap
->dim
->nested
[1], isl_dim_in
);
8327 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
8330 bmap
->dim
= isl_dim_zip(bmap
->dim
);
8335 isl_basic_map_free(bmap
);
8339 /* Given a map (A -> B) -> (C -> D), return the corresponding map
8340 * (A -> C) -> (B -> D).
8342 __isl_give isl_map
*isl_map_zip(__isl_take isl_map
*map
)
8349 if (!isl_map_can_zip(map
))
8350 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be zipped",
8353 map
= isl_map_cow(map
);
8357 for (i
= 0; i
< map
->n
; ++i
) {
8358 map
->p
[i
] = isl_basic_map_zip(map
->p
[i
]);
8363 map
->dim
= isl_dim_zip(map
->dim
);