2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
5 * Use of this software is governed by the GNU LGPLv2.1 license
7 * Written by Sven Verdoolaege, K.U.Leuven, Departement
8 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
9 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
10 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
14 #include <isl_ctx_private.h>
15 #include <isl_map_private.h>
17 #include "isl_dim_private.h"
18 #include "isl_equalities.h"
19 #include <isl_list_private.h>
24 #include "isl_map_piplib.h"
25 #include <isl_reordering.h>
26 #include "isl_sample.h"
29 #include <isl_mat_private.h>
30 #include <isl_dim_map.h>
31 #include <isl_local_space_private.h>
32 #include <isl_aff_private.h>
34 static unsigned n(struct isl_dim
*dim
, enum isl_dim_type type
)
37 case isl_dim_param
: return dim
->nparam
;
38 case isl_dim_in
: return dim
->n_in
;
39 case isl_dim_out
: return dim
->n_out
;
40 case isl_dim_all
: return dim
->nparam
+ dim
->n_in
+ dim
->n_out
;
45 static unsigned pos(struct isl_dim
*dim
, enum isl_dim_type type
)
48 case isl_dim_param
: return 1;
49 case isl_dim_in
: return 1 + dim
->nparam
;
50 case isl_dim_out
: return 1 + dim
->nparam
+ dim
->n_in
;
55 unsigned isl_basic_map_dim(const struct isl_basic_map
*bmap
,
56 enum isl_dim_type type
)
61 case isl_dim_cst
: return 1;
64 case isl_dim_out
: return isl_dim_size(bmap
->dim
, type
);
65 case isl_dim_div
: return bmap
->n_div
;
66 case isl_dim_all
: return isl_basic_map_total_dim(bmap
);
71 unsigned isl_map_dim(const struct isl_map
*map
, enum isl_dim_type type
)
73 return map
? n(map
->dim
, type
) : 0;
76 unsigned isl_set_dim(const struct isl_set
*set
, enum isl_dim_type type
)
78 return set
? n(set
->dim
, type
) : 0;
81 unsigned isl_basic_map_offset(struct isl_basic_map
*bmap
,
82 enum isl_dim_type type
)
84 struct isl_dim
*dim
= bmap
->dim
;
86 case isl_dim_cst
: return 0;
87 case isl_dim_param
: return 1;
88 case isl_dim_in
: return 1 + dim
->nparam
;
89 case isl_dim_out
: return 1 + dim
->nparam
+ dim
->n_in
;
90 case isl_dim_div
: return 1 + dim
->nparam
+ dim
->n_in
+ dim
->n_out
;
95 unsigned isl_basic_set_offset(struct isl_basic_set
*bset
,
96 enum isl_dim_type type
)
98 return isl_basic_map_offset(bset
, type
);
101 static unsigned map_offset(struct isl_map
*map
, enum isl_dim_type type
)
103 return pos(map
->dim
, type
);
106 unsigned isl_basic_set_dim(const struct isl_basic_set
*bset
,
107 enum isl_dim_type type
)
109 return isl_basic_map_dim((const struct isl_basic_map
*)bset
, type
);
112 unsigned isl_basic_set_n_dim(const struct isl_basic_set
*bset
)
114 return isl_basic_set_dim(bset
, isl_dim_set
);
117 unsigned isl_basic_set_n_param(const struct isl_basic_set
*bset
)
119 return isl_basic_set_dim(bset
, isl_dim_param
);
122 unsigned isl_basic_set_total_dim(const struct isl_basic_set
*bset
)
124 return isl_dim_total(bset
->dim
) + bset
->n_div
;
127 unsigned isl_set_n_dim(const struct isl_set
*set
)
129 return isl_set_dim(set
, isl_dim_set
);
132 unsigned isl_set_n_param(const struct isl_set
*set
)
134 return isl_set_dim(set
, isl_dim_param
);
137 unsigned isl_basic_map_n_in(const struct isl_basic_map
*bmap
)
139 return bmap
? bmap
->dim
->n_in
: 0;
142 unsigned isl_basic_map_n_out(const struct isl_basic_map
*bmap
)
144 return bmap
? bmap
->dim
->n_out
: 0;
147 unsigned isl_basic_map_n_param(const struct isl_basic_map
*bmap
)
149 return bmap
? bmap
->dim
->nparam
: 0;
152 unsigned isl_basic_map_n_div(const struct isl_basic_map
*bmap
)
154 return bmap
? bmap
->n_div
: 0;
157 unsigned isl_basic_map_total_dim(const struct isl_basic_map
*bmap
)
159 return bmap
? isl_dim_total(bmap
->dim
) + bmap
->n_div
: 0;
162 unsigned isl_map_n_in(const struct isl_map
*map
)
164 return map
->dim
->n_in
;
167 unsigned isl_map_n_out(const struct isl_map
*map
)
169 return map
->dim
->n_out
;
172 unsigned isl_map_n_param(const struct isl_map
*map
)
174 return map
->dim
->nparam
;
177 int isl_map_compatible_domain(struct isl_map
*map
, struct isl_set
*set
)
182 m
= isl_dim_match(map
->dim
, isl_dim_param
, set
->dim
, isl_dim_param
);
185 return isl_dim_tuple_match(map
->dim
, isl_dim_in
, set
->dim
, isl_dim_set
);
188 int isl_basic_map_compatible_domain(struct isl_basic_map
*bmap
,
189 struct isl_basic_set
*bset
)
194 m
= isl_dim_match(bmap
->dim
, isl_dim_param
, bset
->dim
, isl_dim_param
);
197 return isl_dim_tuple_match(bmap
->dim
, isl_dim_in
, bset
->dim
, isl_dim_set
);
200 int isl_map_compatible_range(__isl_keep isl_map
*map
, __isl_keep isl_set
*set
)
205 m
= isl_dim_match(map
->dim
, isl_dim_param
, set
->dim
, isl_dim_param
);
208 return isl_dim_tuple_match(map
->dim
, isl_dim_out
, set
->dim
, isl_dim_set
);
211 int isl_basic_map_compatible_range(struct isl_basic_map
*bmap
,
212 struct isl_basic_set
*bset
)
217 m
= isl_dim_match(bmap
->dim
, isl_dim_param
, bset
->dim
, isl_dim_param
);
220 return isl_dim_tuple_match(bmap
->dim
, isl_dim_out
, bset
->dim
, isl_dim_set
);
223 isl_ctx
*isl_basic_map_get_ctx(__isl_keep isl_basic_map
*bmap
)
225 return bmap
? bmap
->ctx
: NULL
;
228 isl_ctx
*isl_basic_set_get_ctx(__isl_keep isl_basic_set
*bset
)
230 return bset
? bset
->ctx
: NULL
;
233 isl_ctx
*isl_map_get_ctx(__isl_keep isl_map
*map
)
235 return map
? map
->ctx
: NULL
;
238 isl_ctx
*isl_set_get_ctx(__isl_keep isl_set
*set
)
240 return set
? set
->ctx
: NULL
;
243 struct isl_dim
*isl_basic_map_get_dim(struct isl_basic_map
*bmap
)
247 return isl_dim_copy(bmap
->dim
);
250 struct isl_dim
*isl_basic_set_get_dim(struct isl_basic_set
*bset
)
254 return isl_dim_copy(bset
->dim
);
257 __isl_give isl_local_space
*isl_basic_map_get_local_space(
258 __isl_keep isl_basic_map
*bmap
)
267 total
= isl_basic_map_total_dim(bmap
);
268 ls
= isl_local_space_alloc(isl_dim_copy(bmap
->dim
), bmap
->n_div
);
272 for (i
= 0; i
< bmap
->n_div
; ++i
)
273 isl_seq_cpy(ls
->div
->row
[i
], bmap
->div
[i
], 2 + total
);
278 __isl_give isl_local_space
*isl_basic_set_get_local_space(
279 __isl_keep isl_basic_set
*bset
)
281 return isl_basic_map_get_local_space(bset
);
284 __isl_give isl_basic_map
*isl_basic_map_from_local_space(
285 __isl_take isl_local_space
*ls
)
294 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
295 bmap
= isl_basic_map_alloc_dim(isl_local_space_get_dim(ls
),
296 n_div
, 0, 2 * n_div
);
298 for (i
= 0; i
< n_div
; ++i
)
299 if (isl_basic_map_alloc_div(bmap
) < 0)
302 for (i
= 0; i
< n_div
; ++i
) {
303 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], ls
->div
->n_col
);
304 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
308 isl_local_space_free(ls
);
311 isl_local_space_free(ls
);
312 isl_basic_map_free(bmap
);
316 __isl_give isl_basic_set
*isl_basic_set_from_local_space(
317 __isl_take isl_local_space
*ls
)
319 return isl_basic_map_from_local_space(ls
);
322 struct isl_dim
*isl_map_get_dim(struct isl_map
*map
)
326 return isl_dim_copy(map
->dim
);
329 struct isl_dim
*isl_set_get_dim(struct isl_set
*set
)
333 return isl_dim_copy(set
->dim
);
336 __isl_give isl_basic_map
*isl_basic_map_set_tuple_name(
337 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
, const char *s
)
339 bmap
= isl_basic_map_cow(bmap
);
342 bmap
->dim
= isl_dim_set_tuple_name(bmap
->dim
, type
, s
);
345 bmap
= isl_basic_map_finalize(bmap
);
348 isl_basic_map_free(bmap
);
352 __isl_give isl_basic_set
*isl_basic_set_set_tuple_name(
353 __isl_take isl_basic_set
*bset
, const char *s
)
355 return isl_basic_map_set_tuple_name(bset
, isl_dim_set
, s
);
358 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map
*bmap
,
359 enum isl_dim_type type
)
361 return bmap
? isl_dim_get_tuple_name(bmap
->dim
, type
) : NULL
;
364 __isl_give isl_map
*isl_map_set_tuple_name(__isl_take isl_map
*map
,
365 enum isl_dim_type type
, const char *s
)
369 map
= isl_map_cow(map
);
373 map
->dim
= isl_dim_set_tuple_name(map
->dim
, type
, s
);
377 for (i
= 0; i
< map
->n
; ++i
) {
378 map
->p
[i
] = isl_basic_map_set_tuple_name(map
->p
[i
], type
, s
);
389 const char *isl_map_get_tuple_name(__isl_keep isl_map
*map
,
390 enum isl_dim_type type
)
392 return map
? isl_dim_get_tuple_name(map
->dim
, type
) : NULL
;
395 __isl_give isl_set
*isl_set_set_tuple_name(__isl_take isl_set
*set
,
398 return (isl_set
*)isl_map_set_tuple_name((isl_map
*)set
, isl_dim_set
, s
);
401 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set
*bset
)
403 return bset
? isl_dim_get_tuple_name(bset
->dim
, isl_dim_set
) : NULL
;
406 const char *isl_set_get_tuple_name(__isl_keep isl_set
*set
)
408 return set
? isl_dim_get_tuple_name(set
->dim
, isl_dim_set
) : NULL
;
411 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map
*bmap
,
412 enum isl_dim_type type
, unsigned pos
)
414 return bmap
? isl_dim_get_name(bmap
->dim
, type
, pos
) : NULL
;
417 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set
*bset
,
418 enum isl_dim_type type
, unsigned pos
)
420 return bset
? isl_dim_get_name(bset
->dim
, type
, pos
) : NULL
;
423 const char *isl_map_get_dim_name(__isl_keep isl_map
*map
,
424 enum isl_dim_type type
, unsigned pos
)
426 return map
? isl_dim_get_name(map
->dim
, type
, pos
) : NULL
;
429 const char *isl_set_get_dim_name(__isl_keep isl_set
*set
,
430 enum isl_dim_type type
, unsigned pos
)
432 return set
? isl_dim_get_name(set
->dim
, type
, pos
) : NULL
;
435 __isl_give isl_basic_map
*isl_basic_map_set_dim_name(
436 __isl_take isl_basic_map
*bmap
,
437 enum isl_dim_type type
, unsigned pos
, const char *s
)
441 bmap
->dim
= isl_dim_set_name(bmap
->dim
, type
, pos
, s
);
446 isl_basic_map_free(bmap
);
450 __isl_give isl_map
*isl_map_set_dim_name(__isl_take isl_map
*map
,
451 enum isl_dim_type type
, unsigned pos
, const char *s
)
458 map
->dim
= isl_dim_set_name(map
->dim
, type
, pos
, s
);
462 for (i
= 0; i
< map
->n
; ++i
) {
463 map
->p
[i
] = isl_basic_map_set_dim_name(map
->p
[i
], type
, pos
, s
);
474 __isl_give isl_basic_set
*isl_basic_set_set_dim_name(
475 __isl_take isl_basic_set
*bset
,
476 enum isl_dim_type type
, unsigned pos
, const char *s
)
478 return (isl_basic_set
*)isl_basic_map_set_dim_name(
479 (isl_basic_map
*)bset
, type
, pos
, s
);
482 __isl_give isl_set
*isl_set_set_dim_name(__isl_take isl_set
*set
,
483 enum isl_dim_type type
, unsigned pos
, const char *s
)
485 return (isl_set
*)isl_map_set_dim_name((isl_map
*)set
, type
, pos
, s
);
488 int isl_basic_map_is_rational(__isl_keep isl_basic_map
*bmap
)
492 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
495 int isl_basic_set_is_rational(__isl_keep isl_basic_set
*bset
)
497 return isl_basic_map_is_rational(bset
);
500 static struct isl_basic_map
*basic_map_init(struct isl_ctx
*ctx
,
501 struct isl_basic_map
*bmap
, unsigned extra
,
502 unsigned n_eq
, unsigned n_ineq
)
505 size_t row_size
= 1 + isl_dim_total(bmap
->dim
) + extra
;
510 bmap
->block
= isl_blk_alloc(ctx
, (n_ineq
+ n_eq
) * row_size
);
511 if (isl_blk_is_error(bmap
->block
))
514 bmap
->ineq
= isl_alloc_array(ctx
, isl_int
*, n_ineq
+ n_eq
);
519 bmap
->block2
= isl_blk_empty();
522 bmap
->block2
= isl_blk_alloc(ctx
, extra
* (1 + row_size
));
523 if (isl_blk_is_error(bmap
->block2
))
526 bmap
->div
= isl_alloc_array(ctx
, isl_int
*, extra
);
531 for (i
= 0; i
< n_ineq
+ n_eq
; ++i
)
532 bmap
->ineq
[i
] = bmap
->block
.data
+ i
* row_size
;
534 for (i
= 0; i
< extra
; ++i
)
535 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + row_size
);
539 bmap
->c_size
= n_eq
+ n_ineq
;
540 bmap
->eq
= bmap
->ineq
+ n_ineq
;
549 isl_basic_map_free(bmap
);
553 struct isl_basic_set
*isl_basic_set_alloc(struct isl_ctx
*ctx
,
554 unsigned nparam
, unsigned dim
, unsigned extra
,
555 unsigned n_eq
, unsigned n_ineq
)
557 struct isl_basic_map
*bmap
;
558 bmap
= isl_basic_map_alloc(ctx
, nparam
, 0, dim
, extra
, n_eq
, n_ineq
);
559 return (struct isl_basic_set
*)bmap
;
562 struct isl_basic_set
*isl_basic_set_alloc_dim(struct isl_dim
*dim
,
563 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
565 struct isl_basic_map
*bmap
;
568 isl_assert(dim
->ctx
, dim
->n_in
== 0, goto error
);
569 bmap
= isl_basic_map_alloc_dim(dim
, extra
, n_eq
, n_ineq
);
570 return (struct isl_basic_set
*)bmap
;
576 struct isl_basic_map
*isl_basic_map_alloc_dim(struct isl_dim
*dim
,
577 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
579 struct isl_basic_map
*bmap
;
583 bmap
= isl_calloc_type(dim
->ctx
, struct isl_basic_map
);
588 return basic_map_init(dim
->ctx
, bmap
, extra
, n_eq
, n_ineq
);
594 struct isl_basic_map
*isl_basic_map_alloc(struct isl_ctx
*ctx
,
595 unsigned nparam
, unsigned in
, unsigned out
, unsigned extra
,
596 unsigned n_eq
, unsigned n_ineq
)
598 struct isl_basic_map
*bmap
;
601 dim
= isl_dim_alloc(ctx
, nparam
, in
, out
);
605 bmap
= isl_basic_map_alloc_dim(dim
, extra
, n_eq
, n_ineq
);
609 static void dup_constraints(
610 struct isl_basic_map
*dst
, struct isl_basic_map
*src
)
613 unsigned total
= isl_basic_map_total_dim(src
);
615 for (i
= 0; i
< src
->n_eq
; ++i
) {
616 int j
= isl_basic_map_alloc_equality(dst
);
617 isl_seq_cpy(dst
->eq
[j
], src
->eq
[i
], 1+total
);
620 for (i
= 0; i
< src
->n_ineq
; ++i
) {
621 int j
= isl_basic_map_alloc_inequality(dst
);
622 isl_seq_cpy(dst
->ineq
[j
], src
->ineq
[i
], 1+total
);
625 for (i
= 0; i
< src
->n_div
; ++i
) {
626 int j
= isl_basic_map_alloc_div(dst
);
627 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
);
629 ISL_F_SET(dst
, ISL_BASIC_SET_FINAL
);
632 struct isl_basic_map
*isl_basic_map_dup(struct isl_basic_map
*bmap
)
634 struct isl_basic_map
*dup
;
638 dup
= isl_basic_map_alloc_dim(isl_dim_copy(bmap
->dim
),
639 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
642 dup_constraints(dup
, bmap
);
643 dup
->flags
= bmap
->flags
;
644 dup
->sample
= isl_vec_copy(bmap
->sample
);
648 struct isl_basic_set
*isl_basic_set_dup(struct isl_basic_set
*bset
)
650 struct isl_basic_map
*dup
;
652 dup
= isl_basic_map_dup((struct isl_basic_map
*)bset
);
653 return (struct isl_basic_set
*)dup
;
656 struct isl_basic_set
*isl_basic_set_copy(struct isl_basic_set
*bset
)
661 if (ISL_F_ISSET(bset
, ISL_BASIC_SET_FINAL
)) {
665 return isl_basic_set_dup(bset
);
668 struct isl_set
*isl_set_copy(struct isl_set
*set
)
677 struct isl_basic_map
*isl_basic_map_copy(struct isl_basic_map
*bmap
)
682 if (ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
)) {
686 bmap
= isl_basic_map_dup(bmap
);
688 ISL_F_SET(bmap
, ISL_BASIC_SET_FINAL
);
692 struct isl_map
*isl_map_copy(struct isl_map
*map
)
701 void isl_basic_map_free(struct isl_basic_map
*bmap
)
709 isl_ctx_deref(bmap
->ctx
);
711 isl_blk_free(bmap
->ctx
, bmap
->block2
);
713 isl_blk_free(bmap
->ctx
, bmap
->block
);
714 isl_vec_free(bmap
->sample
);
715 isl_dim_free(bmap
->dim
);
719 void isl_basic_set_free(struct isl_basic_set
*bset
)
721 isl_basic_map_free((struct isl_basic_map
*)bset
);
724 static int room_for_con(struct isl_basic_map
*bmap
, unsigned n
)
726 return bmap
->n_eq
+ bmap
->n_ineq
+ n
<= bmap
->c_size
;
729 int isl_basic_map_alloc_equality(struct isl_basic_map
*bmap
)
735 isl_assert(ctx
, room_for_con(bmap
, 1), return -1);
736 isl_assert(ctx
, (bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
<= bmap
->c_size
,
738 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
739 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
740 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
741 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
742 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
743 if ((bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
== bmap
->c_size
) {
745 int j
= isl_basic_map_alloc_inequality(bmap
);
749 bmap
->ineq
[j
] = bmap
->ineq
[bmap
->n_ineq
- 1];
750 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
757 isl_seq_clr(bmap
->eq
[bmap
->n_eq
] + 1 + isl_basic_map_total_dim(bmap
),
758 bmap
->extra
- bmap
->n_div
);
762 int isl_basic_set_alloc_equality(struct isl_basic_set
*bset
)
764 return isl_basic_map_alloc_equality((struct isl_basic_map
*)bset
);
767 int isl_basic_map_free_equality(struct isl_basic_map
*bmap
, unsigned n
)
771 isl_assert(bmap
->ctx
, n
<= bmap
->n_eq
, return -1);
776 int isl_basic_set_free_equality(struct isl_basic_set
*bset
, unsigned n
)
778 return isl_basic_map_free_equality((struct isl_basic_map
*)bset
, n
);
781 int isl_basic_map_drop_equality(struct isl_basic_map
*bmap
, unsigned pos
)
786 isl_assert(bmap
->ctx
, pos
< bmap
->n_eq
, return -1);
788 if (pos
!= bmap
->n_eq
- 1) {
790 bmap
->eq
[pos
] = bmap
->eq
[bmap
->n_eq
- 1];
791 bmap
->eq
[bmap
->n_eq
- 1] = t
;
797 int isl_basic_set_drop_equality(struct isl_basic_set
*bset
, unsigned pos
)
799 return isl_basic_map_drop_equality((struct isl_basic_map
*)bset
, pos
);
802 void isl_basic_map_inequality_to_equality(
803 struct isl_basic_map
*bmap
, unsigned pos
)
808 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
809 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
814 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
815 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
816 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
817 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
820 static int room_for_ineq(struct isl_basic_map
*bmap
, unsigned n
)
822 return bmap
->n_ineq
+ n
<= bmap
->eq
- bmap
->ineq
;
825 int isl_basic_map_alloc_inequality(struct isl_basic_map
*bmap
)
831 isl_assert(ctx
, room_for_ineq(bmap
, 1), return -1);
832 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
833 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
834 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
835 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
836 isl_seq_clr(bmap
->ineq
[bmap
->n_ineq
] +
837 1 + isl_basic_map_total_dim(bmap
),
838 bmap
->extra
- bmap
->n_div
);
839 return bmap
->n_ineq
++;
842 int isl_basic_set_alloc_inequality(struct isl_basic_set
*bset
)
844 return isl_basic_map_alloc_inequality((struct isl_basic_map
*)bset
);
847 int isl_basic_map_free_inequality(struct isl_basic_map
*bmap
, unsigned n
)
851 isl_assert(bmap
->ctx
, n
<= bmap
->n_ineq
, return -1);
856 int isl_basic_set_free_inequality(struct isl_basic_set
*bset
, unsigned n
)
858 return isl_basic_map_free_inequality((struct isl_basic_map
*)bset
, n
);
861 int isl_basic_map_drop_inequality(struct isl_basic_map
*bmap
, unsigned pos
)
866 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
868 if (pos
!= bmap
->n_ineq
- 1) {
870 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
871 bmap
->ineq
[bmap
->n_ineq
- 1] = t
;
872 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
878 int isl_basic_set_drop_inequality(struct isl_basic_set
*bset
, unsigned pos
)
880 return isl_basic_map_drop_inequality((struct isl_basic_map
*)bset
, pos
);
883 __isl_give isl_basic_map
*isl_basic_map_add_eq(__isl_take isl_basic_map
*bmap
,
888 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
891 k
= isl_basic_map_alloc_equality(bmap
);
894 isl_seq_cpy(bmap
->eq
[k
], eq
, 1 + isl_basic_map_total_dim(bmap
));
897 isl_basic_map_free(bmap
);
901 __isl_give isl_basic_set
*isl_basic_set_add_eq(__isl_take isl_basic_set
*bset
,
904 return (isl_basic_set
*)
905 isl_basic_map_add_eq((isl_basic_map
*)bset
, eq
);
908 __isl_give isl_basic_map
*isl_basic_map_add_ineq(__isl_take isl_basic_map
*bmap
,
913 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
916 k
= isl_basic_map_alloc_inequality(bmap
);
919 isl_seq_cpy(bmap
->ineq
[k
], ineq
, 1 + isl_basic_map_total_dim(bmap
));
922 isl_basic_map_free(bmap
);
926 __isl_give isl_basic_set
*isl_basic_set_add_ineq(__isl_take isl_basic_set
*bset
,
929 return (isl_basic_set
*)
930 isl_basic_map_add_ineq((isl_basic_map
*)bset
, ineq
);
933 int isl_basic_map_alloc_div(struct isl_basic_map
*bmap
)
937 isl_assert(bmap
->ctx
, bmap
->n_div
< bmap
->extra
, return -1);
938 isl_seq_clr(bmap
->div
[bmap
->n_div
] +
939 1 + 1 + isl_basic_map_total_dim(bmap
),
940 bmap
->extra
- bmap
->n_div
);
941 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
942 return bmap
->n_div
++;
945 int isl_basic_set_alloc_div(struct isl_basic_set
*bset
)
947 return isl_basic_map_alloc_div((struct isl_basic_map
*)bset
);
950 int isl_basic_map_free_div(struct isl_basic_map
*bmap
, unsigned n
)
954 isl_assert(bmap
->ctx
, n
<= bmap
->n_div
, return -1);
959 int isl_basic_set_free_div(struct isl_basic_set
*bset
, unsigned n
)
961 return isl_basic_map_free_div((struct isl_basic_map
*)bset
, n
);
964 /* Copy constraint from src to dst, putting the vars of src at offset
965 * dim_off in dst and the divs of src at offset div_off in dst.
966 * If both sets are actually map, then dim_off applies to the input
969 static void copy_constraint(struct isl_basic_map
*dst_map
, isl_int
*dst
,
970 struct isl_basic_map
*src_map
, isl_int
*src
,
971 unsigned in_off
, unsigned out_off
, unsigned div_off
)
973 unsigned src_nparam
= isl_basic_map_n_param(src_map
);
974 unsigned dst_nparam
= isl_basic_map_n_param(dst_map
);
975 unsigned src_in
= isl_basic_map_n_in(src_map
);
976 unsigned dst_in
= isl_basic_map_n_in(dst_map
);
977 unsigned src_out
= isl_basic_map_n_out(src_map
);
978 unsigned dst_out
= isl_basic_map_n_out(dst_map
);
979 isl_int_set(dst
[0], src
[0]);
980 isl_seq_cpy(dst
+1, src
+1, isl_min(dst_nparam
, src_nparam
));
981 if (dst_nparam
> src_nparam
)
982 isl_seq_clr(dst
+1+src_nparam
,
983 dst_nparam
- src_nparam
);
984 isl_seq_clr(dst
+1+dst_nparam
, in_off
);
985 isl_seq_cpy(dst
+1+dst_nparam
+in_off
,
987 isl_min(dst_in
-in_off
, src_in
));
988 if (dst_in
-in_off
> src_in
)
989 isl_seq_clr(dst
+1+dst_nparam
+in_off
+src_in
,
990 dst_in
- in_off
- src_in
);
991 isl_seq_clr(dst
+1+dst_nparam
+dst_in
, out_off
);
992 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+out_off
,
993 src
+1+src_nparam
+src_in
,
994 isl_min(dst_out
-out_off
, src_out
));
995 if (dst_out
-out_off
> src_out
)
996 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+out_off
+src_out
,
997 dst_out
- out_off
- src_out
);
998 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
, div_off
);
999 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+dst_out
+div_off
,
1000 src
+1+src_nparam
+src_in
+src_out
,
1001 isl_min(dst_map
->extra
-div_off
, src_map
->n_div
));
1002 if (dst_map
->n_div
-div_off
> src_map
->n_div
)
1003 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
+
1004 div_off
+src_map
->n_div
,
1005 dst_map
->n_div
- div_off
- src_map
->n_div
);
1008 static void copy_div(struct isl_basic_map
*dst_map
, isl_int
*dst
,
1009 struct isl_basic_map
*src_map
, isl_int
*src
,
1010 unsigned in_off
, unsigned out_off
, unsigned div_off
)
1012 isl_int_set(dst
[0], src
[0]);
1013 copy_constraint(dst_map
, dst
+1, src_map
, src
+1, in_off
, out_off
, div_off
);
1016 static struct isl_basic_map
*add_constraints(struct isl_basic_map
*bmap1
,
1017 struct isl_basic_map
*bmap2
, unsigned i_pos
, unsigned o_pos
)
1022 if (!bmap1
|| !bmap2
)
1025 div_off
= bmap1
->n_div
;
1027 for (i
= 0; i
< bmap2
->n_eq
; ++i
) {
1028 int i1
= isl_basic_map_alloc_equality(bmap1
);
1031 copy_constraint(bmap1
, bmap1
->eq
[i1
], bmap2
, bmap2
->eq
[i
],
1032 i_pos
, o_pos
, div_off
);
1035 for (i
= 0; i
< bmap2
->n_ineq
; ++i
) {
1036 int i1
= isl_basic_map_alloc_inequality(bmap1
);
1039 copy_constraint(bmap1
, bmap1
->ineq
[i1
], bmap2
, bmap2
->ineq
[i
],
1040 i_pos
, o_pos
, div_off
);
1043 for (i
= 0; i
< bmap2
->n_div
; ++i
) {
1044 int i1
= isl_basic_map_alloc_div(bmap1
);
1047 copy_div(bmap1
, bmap1
->div
[i1
], bmap2
, bmap2
->div
[i
],
1048 i_pos
, o_pos
, div_off
);
1051 isl_basic_map_free(bmap2
);
1056 isl_basic_map_free(bmap1
);
1057 isl_basic_map_free(bmap2
);
1061 struct isl_basic_set
*isl_basic_set_add_constraints(struct isl_basic_set
*bset1
,
1062 struct isl_basic_set
*bset2
, unsigned pos
)
1064 return (struct isl_basic_set
*)
1065 add_constraints((struct isl_basic_map
*)bset1
,
1066 (struct isl_basic_map
*)bset2
, 0, pos
);
1069 struct isl_basic_map
*isl_basic_map_extend_dim(struct isl_basic_map
*base
,
1070 struct isl_dim
*dim
, unsigned extra
,
1071 unsigned n_eq
, unsigned n_ineq
)
1073 struct isl_basic_map
*ext
;
1083 dims_ok
= isl_dim_equal(base
->dim
, dim
) &&
1084 base
->extra
>= base
->n_div
+ extra
;
1086 if (dims_ok
&& room_for_con(base
, n_eq
+ n_ineq
) &&
1087 room_for_ineq(base
, n_ineq
)) {
1092 isl_assert(base
->ctx
, base
->dim
->nparam
<= dim
->nparam
, goto error
);
1093 isl_assert(base
->ctx
, base
->dim
->n_in
<= dim
->n_in
, goto error
);
1094 isl_assert(base
->ctx
, base
->dim
->n_out
<= dim
->n_out
, goto error
);
1095 extra
+= base
->extra
;
1097 n_ineq
+= base
->n_ineq
;
1099 ext
= isl_basic_map_alloc_dim(dim
, extra
, n_eq
, n_ineq
);
1105 ext
->sample
= isl_vec_copy(base
->sample
);
1106 flags
= base
->flags
;
1107 ext
= add_constraints(ext
, base
, 0, 0);
1110 ISL_F_CLR(ext
, ISL_BASIC_SET_FINAL
);
1117 isl_basic_map_free(base
);
1121 struct isl_basic_set
*isl_basic_set_extend_dim(struct isl_basic_set
*base
,
1122 struct isl_dim
*dim
, unsigned extra
,
1123 unsigned n_eq
, unsigned n_ineq
)
1125 return (struct isl_basic_set
*)
1126 isl_basic_map_extend_dim((struct isl_basic_map
*)base
, dim
,
1127 extra
, n_eq
, n_ineq
);
1130 struct isl_basic_map
*isl_basic_map_extend_constraints(
1131 struct isl_basic_map
*base
, unsigned n_eq
, unsigned n_ineq
)
1135 return isl_basic_map_extend_dim(base
, isl_dim_copy(base
->dim
),
1139 struct isl_basic_map
*isl_basic_map_extend(struct isl_basic_map
*base
,
1140 unsigned nparam
, unsigned n_in
, unsigned n_out
, unsigned extra
,
1141 unsigned n_eq
, unsigned n_ineq
)
1143 struct isl_basic_map
*bmap
;
1144 struct isl_dim
*dim
;
1148 dim
= isl_dim_alloc(base
->ctx
, nparam
, n_in
, n_out
);
1152 bmap
= isl_basic_map_extend_dim(base
, dim
, extra
, n_eq
, n_ineq
);
1155 isl_basic_map_free(base
);
1159 struct isl_basic_set
*isl_basic_set_extend(struct isl_basic_set
*base
,
1160 unsigned nparam
, unsigned dim
, unsigned extra
,
1161 unsigned n_eq
, unsigned n_ineq
)
1163 return (struct isl_basic_set
*)
1164 isl_basic_map_extend((struct isl_basic_map
*)base
,
1165 nparam
, 0, dim
, extra
, n_eq
, n_ineq
);
1168 struct isl_basic_set
*isl_basic_set_extend_constraints(
1169 struct isl_basic_set
*base
, unsigned n_eq
, unsigned n_ineq
)
1171 return (struct isl_basic_set
*)
1172 isl_basic_map_extend_constraints((struct isl_basic_map
*)base
,
1176 struct isl_basic_set
*isl_basic_set_cow(struct isl_basic_set
*bset
)
1178 return (struct isl_basic_set
*)
1179 isl_basic_map_cow((struct isl_basic_map
*)bset
);
1182 struct isl_basic_map
*isl_basic_map_cow(struct isl_basic_map
*bmap
)
1187 if (bmap
->ref
> 1) {
1189 bmap
= isl_basic_map_dup(bmap
);
1192 ISL_F_CLR(bmap
, ISL_BASIC_SET_FINAL
);
1196 struct isl_set
*isl_set_cow(struct isl_set
*set
)
1204 return isl_set_dup(set
);
1207 struct isl_map
*isl_map_cow(struct isl_map
*map
)
1215 return isl_map_dup(map
);
1218 static void swap_vars(struct isl_blk blk
, isl_int
*a
,
1219 unsigned a_len
, unsigned b_len
)
1221 isl_seq_cpy(blk
.data
, a
+a_len
, b_len
);
1222 isl_seq_cpy(blk
.data
+b_len
, a
, a_len
);
1223 isl_seq_cpy(a
, blk
.data
, b_len
+a_len
);
1226 static __isl_give isl_basic_map
*isl_basic_map_swap_vars(
1227 __isl_take isl_basic_map
*bmap
, unsigned pos
, unsigned n1
, unsigned n2
)
1235 isl_assert(bmap
->ctx
,
1236 pos
+ n1
+ n2
<= 1 + isl_basic_map_total_dim(bmap
), goto error
);
1238 if (n1
== 0 || n2
== 0)
1241 bmap
= isl_basic_map_cow(bmap
);
1245 blk
= isl_blk_alloc(bmap
->ctx
, n1
+ n2
);
1246 if (isl_blk_is_error(blk
))
1249 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1251 bmap
->eq
[i
] + pos
, n1
, n2
);
1253 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1255 bmap
->ineq
[i
] + pos
, n1
, n2
);
1257 for (i
= 0; i
< bmap
->n_div
; ++i
)
1259 bmap
->div
[i
]+1 + pos
, n1
, n2
);
1261 isl_blk_free(bmap
->ctx
, blk
);
1263 ISL_F_CLR(bmap
, ISL_BASIC_SET_NORMALIZED
);
1264 bmap
= isl_basic_map_gauss(bmap
, NULL
);
1265 return isl_basic_map_finalize(bmap
);
1267 isl_basic_map_free(bmap
);
1271 static __isl_give isl_basic_set
*isl_basic_set_swap_vars(
1272 __isl_take isl_basic_set
*bset
, unsigned n
)
1277 nparam
= isl_basic_set_n_param(bset
);
1278 dim
= isl_basic_set_n_dim(bset
);
1279 isl_assert(bset
->ctx
, n
<= dim
, goto error
);
1281 return isl_basic_map_swap_vars(bset
, 1 + nparam
, n
, dim
- n
);
1283 isl_basic_set_free(bset
);
1287 struct isl_basic_map
*isl_basic_map_set_to_empty(struct isl_basic_map
*bmap
)
1293 total
= isl_basic_map_total_dim(bmap
);
1294 isl_basic_map_free_div(bmap
, bmap
->n_div
);
1295 isl_basic_map_free_inequality(bmap
, bmap
->n_ineq
);
1297 isl_basic_map_free_equality(bmap
, bmap
->n_eq
-1);
1299 i
= isl_basic_map_alloc_equality(bmap
);
1303 isl_int_set_si(bmap
->eq
[i
][0], 1);
1304 isl_seq_clr(bmap
->eq
[i
]+1, total
);
1305 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
1306 isl_vec_free(bmap
->sample
);
1307 bmap
->sample
= NULL
;
1308 return isl_basic_map_finalize(bmap
);
1310 isl_basic_map_free(bmap
);
1314 struct isl_basic_set
*isl_basic_set_set_to_empty(struct isl_basic_set
*bset
)
1316 return (struct isl_basic_set
*)
1317 isl_basic_map_set_to_empty((struct isl_basic_map
*)bset
);
1320 void isl_basic_map_swap_div(struct isl_basic_map
*bmap
, int a
, int b
)
1323 unsigned off
= isl_dim_total(bmap
->dim
);
1324 isl_int
*t
= bmap
->div
[a
];
1325 bmap
->div
[a
] = bmap
->div
[b
];
1328 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1329 isl_int_swap(bmap
->eq
[i
][1+off
+a
], bmap
->eq
[i
][1+off
+b
]);
1331 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1332 isl_int_swap(bmap
->ineq
[i
][1+off
+a
], bmap
->ineq
[i
][1+off
+b
]);
1334 for (i
= 0; i
< bmap
->n_div
; ++i
)
1335 isl_int_swap(bmap
->div
[i
][1+1+off
+a
], bmap
->div
[i
][1+1+off
+b
]);
1336 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1339 /* Eliminate the specified n dimensions starting at first from the
1340 * constraints using Fourier-Motzkin. The dimensions themselves
1343 __isl_give isl_map
*isl_map_eliminate(__isl_take isl_map
*map
,
1344 enum isl_dim_type type
, unsigned first
, unsigned n
)
1353 map
= isl_map_cow(map
);
1356 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
1357 first
+= pos(map
->dim
, type
) - 1;
1359 for (i
= 0; i
< map
->n
; ++i
) {
1360 map
->p
[i
] = isl_basic_map_eliminate_vars(map
->p
[i
], first
, n
);
1370 /* Eliminate the specified n dimensions starting at first from the
1371 * constraints using Fourier-Motzkin. The dimensions themselves
1374 __isl_give isl_set
*isl_set_eliminate(__isl_take isl_set
*set
,
1375 enum isl_dim_type type
, unsigned first
, unsigned n
)
1377 return (isl_set
*)isl_map_eliminate((isl_map
*)set
, type
, first
, n
);
1380 /* Eliminate the specified n dimensions starting at first from the
1381 * constraints using Fourier-Motzkin. The dimensions themselves
1384 __isl_give isl_set
*isl_set_eliminate_dims(__isl_take isl_set
*set
,
1385 unsigned first
, unsigned n
)
1387 return isl_set_eliminate(set
, isl_dim_set
, first
, n
);
1390 __isl_give isl_basic_map
*isl_basic_map_remove_divs(
1391 __isl_take isl_basic_map
*bmap
)
1393 bmap
= isl_basic_map_eliminate_vars(bmap
, isl_dim_total(bmap
->dim
),
1398 return isl_basic_map_finalize(bmap
);
1401 __isl_give isl_basic_set
*isl_basic_set_remove_divs(
1402 __isl_take isl_basic_set
*bset
)
1404 return (struct isl_basic_set
*)isl_basic_map_remove_divs(
1405 (struct isl_basic_map
*)bset
);
1408 __isl_give isl_map
*isl_map_remove_divs(__isl_take isl_map
*map
)
1417 map
= isl_map_cow(map
);
1421 for (i
= 0; i
< map
->n
; ++i
) {
1422 map
->p
[i
] = isl_basic_map_remove_divs(map
->p
[i
]);
1432 __isl_give isl_set
*isl_set_remove_divs(__isl_take isl_set
*set
)
1434 return isl_map_remove_divs(set
);
1437 struct isl_basic_map
*isl_basic_map_remove_dims(struct isl_basic_map
*bmap
,
1438 enum isl_dim_type type
, unsigned first
, unsigned n
)
1442 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
1446 bmap
= isl_basic_map_eliminate_vars(bmap
,
1447 isl_basic_map_offset(bmap
, type
) - 1 + first
, n
);
1450 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
) && type
== isl_dim_div
)
1452 bmap
= isl_basic_map_drop(bmap
, type
, first
, n
);
1455 isl_basic_map_free(bmap
);
1459 /* Return true if the definition of the given div (recursively) involves
1460 * any of the given variables.
1462 static int div_involves_vars(__isl_keep isl_basic_map
*bmap
, int div
,
1463 unsigned first
, unsigned n
)
1466 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
1468 if (isl_int_is_zero(bmap
->div
[div
][0]))
1470 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + first
, n
) >= 0)
1473 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
1474 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
1476 if (div_involves_vars(bmap
, i
, first
, n
))
1483 /* Remove all divs (recursively) involving any of the given dimensions
1484 * in their definitions.
1486 __isl_give isl_basic_map
*isl_basic_map_remove_divs_involving_dims(
1487 __isl_take isl_basic_map
*bmap
,
1488 enum isl_dim_type type
, unsigned first
, unsigned n
)
1494 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
1496 first
+= isl_basic_map_offset(bmap
, type
);
1498 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
1499 if (!div_involves_vars(bmap
, i
, first
, n
))
1501 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
1509 isl_basic_map_free(bmap
);
1513 __isl_give isl_map
*isl_map_remove_divs_involving_dims(__isl_take isl_map
*map
,
1514 enum isl_dim_type type
, unsigned first
, unsigned n
)
1523 map
= isl_map_cow(map
);
1527 for (i
= 0; i
< map
->n
; ++i
) {
1528 map
->p
[i
] = isl_basic_map_remove_divs_involving_dims(map
->p
[i
],
1539 __isl_give isl_set
*isl_set_remove_divs_involving_dims(__isl_take isl_set
*set
,
1540 enum isl_dim_type type
, unsigned first
, unsigned n
)
1542 return (isl_set
*)isl_map_remove_divs_involving_dims((isl_map
*)set
,
1546 int isl_basic_set_involves_dims(__isl_keep isl_basic_set
*bset
,
1547 enum isl_dim_type type
, unsigned first
, unsigned n
)
1554 if (first
+ n
> isl_basic_set_dim(bset
, type
))
1555 isl_die(bset
->ctx
, isl_error_invalid
,
1556 "index out of bounds", return -1);
1558 first
+= isl_basic_set_offset(bset
, type
);
1559 for (i
= 0; i
< bset
->n_eq
; ++i
)
1560 if (isl_seq_first_non_zero(bset
->eq
[i
] + first
, n
) >= 0)
1562 for (i
= 0; i
< bset
->n_ineq
; ++i
)
1563 if (isl_seq_first_non_zero(bset
->ineq
[i
] + first
, n
) >= 0)
1569 int isl_set_involves_dims(__isl_keep isl_set
*set
,
1570 enum isl_dim_type type
, unsigned first
, unsigned n
)
1577 if (first
+ n
> isl_set_dim(set
, type
))
1578 isl_die(set
->ctx
, isl_error_invalid
,
1579 "index out of bounds", return -1);
1581 for (i
= 0; i
< set
->n
; ++i
) {
1582 int involves
= isl_basic_set_involves_dims(set
->p
[i
],
1584 if (involves
< 0 || !involves
)
1591 /* Return true if the definition of the given div is unknown or depends
1594 static int div_is_unknown(__isl_keep isl_basic_map
*bmap
, int div
)
1597 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
1599 if (isl_int_is_zero(bmap
->div
[div
][0]))
1602 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
1603 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
1605 if (div_is_unknown(bmap
, i
))
1612 /* Remove all divs that are unknown or defined in terms of unknown divs.
1614 __isl_give isl_basic_map
*isl_basic_map_remove_unknown_divs(
1615 __isl_take isl_basic_map
*bmap
)
1622 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
1623 if (!div_is_unknown(bmap
, i
))
1625 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
1631 __isl_give isl_map
*isl_map_remove_unknown_divs(__isl_take isl_map
*map
)
1640 map
= isl_map_cow(map
);
1644 for (i
= 0; i
< map
->n
; ++i
) {
1645 map
->p
[i
] = isl_basic_map_remove_unknown_divs(map
->p
[i
]);
1655 __isl_give isl_set
*isl_set_remove_unknown_divs(__isl_take isl_set
*set
)
1657 return (isl_set
*)isl_map_remove_unknown_divs((isl_map
*)set
);
1660 __isl_give isl_basic_set
*isl_basic_set_remove_dims(
1661 __isl_take isl_basic_set
*bset
,
1662 enum isl_dim_type type
, unsigned first
, unsigned n
)
1664 return (isl_basic_set
*)
1665 isl_basic_map_remove_dims((isl_basic_map
*)bset
, type
, first
, n
);
1668 struct isl_map
*isl_map_remove_dims(struct isl_map
*map
,
1669 enum isl_dim_type type
, unsigned first
, unsigned n
)
1676 map
= isl_map_cow(map
);
1679 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
1681 for (i
= 0; i
< map
->n
; ++i
) {
1682 map
->p
[i
] = isl_basic_map_eliminate_vars(map
->p
[i
],
1683 isl_basic_map_offset(map
->p
[i
], type
) - 1 + first
, n
);
1687 map
= isl_map_drop(map
, type
, first
, n
);
1694 __isl_give isl_set
*isl_set_remove_dims(__isl_take isl_set
*bset
,
1695 enum isl_dim_type type
, unsigned first
, unsigned n
)
1697 return (isl_set
*)isl_map_remove_dims((isl_map
*)bset
, type
, first
, n
);
1700 /* Project out n inputs starting at first using Fourier-Motzkin */
1701 struct isl_map
*isl_map_remove_inputs(struct isl_map
*map
,
1702 unsigned first
, unsigned n
)
1704 return isl_map_remove_dims(map
, isl_dim_in
, first
, n
);
1707 static void dump_term(struct isl_basic_map
*bmap
,
1708 isl_int c
, int pos
, FILE *out
)
1711 unsigned in
= isl_basic_map_n_in(bmap
);
1712 unsigned dim
= in
+ isl_basic_map_n_out(bmap
);
1713 unsigned nparam
= isl_basic_map_n_param(bmap
);
1715 isl_int_print(out
, c
, 0);
1717 if (!isl_int_is_one(c
))
1718 isl_int_print(out
, c
, 0);
1719 if (pos
< 1 + nparam
) {
1720 name
= isl_dim_get_name(bmap
->dim
,
1721 isl_dim_param
, pos
- 1);
1723 fprintf(out
, "%s", name
);
1725 fprintf(out
, "p%d", pos
- 1);
1726 } else if (pos
< 1 + nparam
+ in
)
1727 fprintf(out
, "i%d", pos
- 1 - nparam
);
1728 else if (pos
< 1 + nparam
+ dim
)
1729 fprintf(out
, "o%d", pos
- 1 - nparam
- in
);
1731 fprintf(out
, "e%d", pos
- 1 - nparam
- dim
);
1735 static void dump_constraint_sign(struct isl_basic_map
*bmap
, isl_int
*c
,
1736 int sign
, FILE *out
)
1740 unsigned len
= 1 + isl_basic_map_total_dim(bmap
);
1744 for (i
= 0, first
= 1; i
< len
; ++i
) {
1745 if (isl_int_sgn(c
[i
]) * sign
<= 0)
1748 fprintf(out
, " + ");
1750 isl_int_abs(v
, c
[i
]);
1751 dump_term(bmap
, v
, i
, out
);
1758 static void dump_constraint(struct isl_basic_map
*bmap
, isl_int
*c
,
1759 const char *op
, FILE *out
, int indent
)
1763 fprintf(out
, "%*s", indent
, "");
1765 dump_constraint_sign(bmap
, c
, 1, out
);
1766 fprintf(out
, " %s ", op
);
1767 dump_constraint_sign(bmap
, c
, -1, out
);
1771 for (i
= bmap
->n_div
; i
< bmap
->extra
; ++i
) {
1772 if (isl_int_is_zero(c
[1+isl_dim_total(bmap
->dim
)+i
]))
1774 fprintf(out
, "%*s", indent
, "");
1775 fprintf(out
, "ERROR: unused div coefficient not zero\n");
1780 static void dump_constraints(struct isl_basic_map
*bmap
,
1781 isl_int
**c
, unsigned n
,
1782 const char *op
, FILE *out
, int indent
)
1786 for (i
= 0; i
< n
; ++i
)
1787 dump_constraint(bmap
, c
[i
], op
, out
, indent
);
1790 static void dump_affine(struct isl_basic_map
*bmap
, isl_int
*exp
, FILE *out
)
1794 unsigned total
= isl_basic_map_total_dim(bmap
);
1796 for (j
= 0; j
< 1 + total
; ++j
) {
1797 if (isl_int_is_zero(exp
[j
]))
1799 if (!first
&& isl_int_is_pos(exp
[j
]))
1801 dump_term(bmap
, exp
[j
], j
, out
);
1806 static void dump(struct isl_basic_map
*bmap
, FILE *out
, int indent
)
1810 dump_constraints(bmap
, bmap
->eq
, bmap
->n_eq
, "=", out
, indent
);
1811 dump_constraints(bmap
, bmap
->ineq
, bmap
->n_ineq
, ">=", out
, indent
);
1813 for (i
= 0; i
< bmap
->n_div
; ++i
) {
1814 fprintf(out
, "%*s", indent
, "");
1815 fprintf(out
, "e%d = [(", i
);
1816 dump_affine(bmap
, bmap
->div
[i
]+1, out
);
1818 isl_int_print(out
, bmap
->div
[i
][0], 0);
1819 fprintf(out
, "]\n");
1823 void isl_basic_set_print_internal(struct isl_basic_set
*bset
,
1824 FILE *out
, int indent
)
1827 fprintf(out
, "null basic set\n");
1831 fprintf(out
, "%*s", indent
, "");
1832 fprintf(out
, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
1833 bset
->ref
, bset
->dim
->nparam
, bset
->dim
->n_out
,
1834 bset
->extra
, bset
->flags
);
1835 dump((struct isl_basic_map
*)bset
, out
, indent
);
1838 void isl_basic_map_print_internal(struct isl_basic_map
*bmap
,
1839 FILE *out
, int indent
)
1842 fprintf(out
, "null basic map\n");
1846 fprintf(out
, "%*s", indent
, "");
1847 fprintf(out
, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
1848 "flags: %x, n_name: %d\n",
1850 bmap
->dim
->nparam
, bmap
->dim
->n_in
, bmap
->dim
->n_out
,
1851 bmap
->extra
, bmap
->flags
, bmap
->dim
->n_name
);
1852 dump(bmap
, out
, indent
);
1855 int isl_inequality_negate(struct isl_basic_map
*bmap
, unsigned pos
)
1860 total
= isl_basic_map_total_dim(bmap
);
1861 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
1862 isl_seq_neg(bmap
->ineq
[pos
], bmap
->ineq
[pos
], 1 + total
);
1863 isl_int_sub_ui(bmap
->ineq
[pos
][0], bmap
->ineq
[pos
][0], 1);
1864 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1868 struct isl_set
*isl_set_alloc_dim(struct isl_dim
*dim
, int n
, unsigned flags
)
1870 struct isl_set
*set
;
1874 isl_assert(dim
->ctx
, dim
->n_in
== 0, goto error
);
1875 isl_assert(dim
->ctx
, n
>= 0, goto error
);
1876 set
= isl_alloc(dim
->ctx
, struct isl_set
,
1877 sizeof(struct isl_set
) +
1878 (n
- 1) * sizeof(struct isl_basic_set
*));
1882 set
->ctx
= dim
->ctx
;
1883 isl_ctx_ref(set
->ctx
);
1895 struct isl_set
*isl_set_alloc(struct isl_ctx
*ctx
,
1896 unsigned nparam
, unsigned dim
, int n
, unsigned flags
)
1898 struct isl_set
*set
;
1899 struct isl_dim
*dims
;
1901 dims
= isl_dim_alloc(ctx
, nparam
, 0, dim
);
1905 set
= isl_set_alloc_dim(dims
, n
, flags
);
1909 /* Make sure "map" has room for at least "n" more basic maps.
1911 struct isl_map
*isl_map_grow(struct isl_map
*map
, int n
)
1914 struct isl_map
*grown
= NULL
;
1918 isl_assert(map
->ctx
, n
>= 0, goto error
);
1919 if (map
->n
+ n
<= map
->size
)
1921 grown
= isl_map_alloc_dim(isl_map_get_dim(map
), map
->n
+ n
, map
->flags
);
1924 for (i
= 0; i
< map
->n
; ++i
) {
1925 grown
->p
[i
] = isl_basic_map_copy(map
->p
[i
]);
1933 isl_map_free(grown
);
1938 /* Make sure "set" has room for at least "n" more basic sets.
1940 struct isl_set
*isl_set_grow(struct isl_set
*set
, int n
)
1942 return (struct isl_set
*)isl_map_grow((struct isl_map
*)set
, n
);
1945 struct isl_set
*isl_set_dup(struct isl_set
*set
)
1948 struct isl_set
*dup
;
1953 dup
= isl_set_alloc_dim(isl_dim_copy(set
->dim
), set
->n
, set
->flags
);
1956 for (i
= 0; i
< set
->n
; ++i
)
1957 dup
= isl_set_add_basic_set(dup
, isl_basic_set_copy(set
->p
[i
]));
1961 struct isl_set
*isl_set_from_basic_set(struct isl_basic_set
*bset
)
1963 return isl_map_from_basic_map(bset
);
1966 struct isl_map
*isl_map_from_basic_map(struct isl_basic_map
*bmap
)
1968 struct isl_map
*map
;
1973 map
= isl_map_alloc_dim(isl_dim_copy(bmap
->dim
), 1, ISL_MAP_DISJOINT
);
1974 return isl_map_add_basic_map(map
, bmap
);
1977 __isl_give isl_set
*isl_set_add_basic_set(__isl_take isl_set
*set
,
1978 __isl_take isl_basic_set
*bset
)
1980 return (struct isl_set
*)isl_map_add_basic_map((struct isl_map
*)set
,
1981 (struct isl_basic_map
*)bset
);
1984 void isl_set_free(struct isl_set
*set
)
1994 isl_ctx_deref(set
->ctx
);
1995 for (i
= 0; i
< set
->n
; ++i
)
1996 isl_basic_set_free(set
->p
[i
]);
1997 isl_dim_free(set
->dim
);
2001 void isl_set_print_internal(struct isl_set
*set
, FILE *out
, int indent
)
2006 fprintf(out
, "null set\n");
2010 fprintf(out
, "%*s", indent
, "");
2011 fprintf(out
, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
2012 set
->ref
, set
->n
, set
->dim
->nparam
, set
->dim
->n_out
,
2014 for (i
= 0; i
< set
->n
; ++i
) {
2015 fprintf(out
, "%*s", indent
, "");
2016 fprintf(out
, "basic set %d:\n", i
);
2017 isl_basic_set_print_internal(set
->p
[i
], out
, indent
+4);
2021 void isl_map_print_internal(struct isl_map
*map
, FILE *out
, int indent
)
2026 fprintf(out
, "null map\n");
2030 fprintf(out
, "%*s", indent
, "");
2031 fprintf(out
, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2032 "flags: %x, n_name: %d\n",
2033 map
->ref
, map
->n
, map
->dim
->nparam
, map
->dim
->n_in
,
2034 map
->dim
->n_out
, map
->flags
, map
->dim
->n_name
);
2035 for (i
= 0; i
< map
->n
; ++i
) {
2036 fprintf(out
, "%*s", indent
, "");
2037 fprintf(out
, "basic map %d:\n", i
);
2038 isl_basic_map_print_internal(map
->p
[i
], out
, indent
+4);
2042 struct isl_basic_map
*isl_basic_map_intersect_domain(
2043 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
2045 struct isl_basic_map
*bmap_domain
;
2050 isl_assert(bset
->ctx
, isl_dim_match(bmap
->dim
, isl_dim_param
,
2051 bset
->dim
, isl_dim_param
), goto error
);
2053 if (isl_dim_size(bset
->dim
, isl_dim_set
) != 0)
2054 isl_assert(bset
->ctx
,
2055 isl_basic_map_compatible_domain(bmap
, bset
), goto error
);
2057 bmap
= isl_basic_map_cow(bmap
);
2060 bmap
= isl_basic_map_extend_dim(bmap
, isl_dim_copy(bmap
->dim
),
2061 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
2062 bmap_domain
= isl_basic_map_from_domain(bset
);
2063 bmap
= add_constraints(bmap
, bmap_domain
, 0, 0);
2065 bmap
= isl_basic_map_simplify(bmap
);
2066 return isl_basic_map_finalize(bmap
);
2068 isl_basic_map_free(bmap
);
2069 isl_basic_set_free(bset
);
2073 struct isl_basic_map
*isl_basic_map_intersect_range(
2074 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
2076 struct isl_basic_map
*bmap_range
;
2081 isl_assert(bset
->ctx
, isl_dim_match(bmap
->dim
, isl_dim_param
,
2082 bset
->dim
, isl_dim_param
), goto error
);
2084 if (isl_dim_size(bset
->dim
, isl_dim_set
) != 0)
2085 isl_assert(bset
->ctx
,
2086 isl_basic_map_compatible_range(bmap
, bset
), goto error
);
2088 if (isl_basic_set_is_universe(bset
)) {
2089 isl_basic_set_free(bset
);
2093 bmap
= isl_basic_map_cow(bmap
);
2096 bmap
= isl_basic_map_extend_dim(bmap
, isl_dim_copy(bmap
->dim
),
2097 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
2098 bmap_range
= isl_basic_map_from_basic_set(bset
, isl_dim_copy(bset
->dim
));
2099 bmap
= add_constraints(bmap
, bmap_range
, 0, 0);
2101 bmap
= isl_basic_map_simplify(bmap
);
2102 return isl_basic_map_finalize(bmap
);
2104 isl_basic_map_free(bmap
);
2105 isl_basic_set_free(bset
);
2109 int isl_basic_map_contains(struct isl_basic_map
*bmap
, struct isl_vec
*vec
)
2115 total
= 1 + isl_basic_map_total_dim(bmap
);
2116 if (total
!= vec
->size
)
2121 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
2122 isl_seq_inner_product(vec
->el
, bmap
->eq
[i
], total
, &s
);
2123 if (!isl_int_is_zero(s
)) {
2129 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
2130 isl_seq_inner_product(vec
->el
, bmap
->ineq
[i
], total
, &s
);
2131 if (isl_int_is_neg(s
)) {
2142 int isl_basic_set_contains(struct isl_basic_set
*bset
, struct isl_vec
*vec
)
2144 return isl_basic_map_contains((struct isl_basic_map
*)bset
, vec
);
2147 struct isl_basic_map
*isl_basic_map_intersect(
2148 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
2150 struct isl_vec
*sample
= NULL
;
2152 if (!bmap1
|| !bmap2
)
2155 isl_assert(bmap1
->ctx
, isl_dim_match(bmap1
->dim
, isl_dim_param
,
2156 bmap2
->dim
, isl_dim_param
), goto error
);
2157 if (isl_dim_total(bmap1
->dim
) ==
2158 isl_dim_size(bmap1
->dim
, isl_dim_param
) &&
2159 isl_dim_total(bmap2
->dim
) !=
2160 isl_dim_size(bmap2
->dim
, isl_dim_param
))
2161 return isl_basic_map_intersect(bmap2
, bmap1
);
2163 if (isl_dim_total(bmap2
->dim
) !=
2164 isl_dim_size(bmap2
->dim
, isl_dim_param
))
2165 isl_assert(bmap1
->ctx
,
2166 isl_dim_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
2168 if (bmap1
->sample
&&
2169 isl_basic_map_contains(bmap1
, bmap1
->sample
) > 0 &&
2170 isl_basic_map_contains(bmap2
, bmap1
->sample
) > 0)
2171 sample
= isl_vec_copy(bmap1
->sample
);
2172 else if (bmap2
->sample
&&
2173 isl_basic_map_contains(bmap1
, bmap2
->sample
) > 0 &&
2174 isl_basic_map_contains(bmap2
, bmap2
->sample
) > 0)
2175 sample
= isl_vec_copy(bmap2
->sample
);
2177 bmap1
= isl_basic_map_cow(bmap1
);
2180 bmap1
= isl_basic_map_extend_dim(bmap1
, isl_dim_copy(bmap1
->dim
),
2181 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
2182 bmap1
= add_constraints(bmap1
, bmap2
, 0, 0);
2185 isl_vec_free(sample
);
2187 isl_vec_free(bmap1
->sample
);
2188 bmap1
->sample
= sample
;
2191 bmap1
= isl_basic_map_simplify(bmap1
);
2192 return isl_basic_map_finalize(bmap1
);
2195 isl_vec_free(sample
);
2196 isl_basic_map_free(bmap1
);
2197 isl_basic_map_free(bmap2
);
2201 struct isl_basic_set
*isl_basic_set_intersect(
2202 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
2204 return (struct isl_basic_set
*)
2205 isl_basic_map_intersect(
2206 (struct isl_basic_map
*)bset1
,
2207 (struct isl_basic_map
*)bset2
);
2210 /* Special case of isl_map_intersect, where both map1 and map2
2211 * are convex, without any divs and such that either map1 or map2
2212 * contains a single constraint. This constraint is then simply
2213 * added to the other map.
2215 static __isl_give isl_map
*map_intersect_add_constraint(
2216 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
2218 isl_assert(map1
->ctx
, map1
->n
== 1, goto error
);
2219 isl_assert(map2
->ctx
, map1
->n
== 1, goto error
);
2220 isl_assert(map1
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
2221 isl_assert(map2
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
2223 if (map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
!= 1)
2224 return isl_map_intersect(map2
, map1
);
2226 isl_assert(map2
->ctx
,
2227 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1, goto error
);
2229 map1
= isl_map_cow(map1
);
2232 if (isl_map_plain_is_empty(map1
)) {
2236 map1
->p
[0] = isl_basic_map_cow(map1
->p
[0]);
2237 if (map2
->p
[0]->n_eq
== 1)
2238 map1
->p
[0] = isl_basic_map_add_eq(map1
->p
[0], map2
->p
[0]->eq
[0]);
2240 map1
->p
[0] = isl_basic_map_add_ineq(map1
->p
[0],
2241 map2
->p
[0]->ineq
[0]);
2243 map1
->p
[0] = isl_basic_map_simplify(map1
->p
[0]);
2244 map1
->p
[0] = isl_basic_map_finalize(map1
->p
[0]);
2248 if (isl_basic_map_plain_is_empty(map1
->p
[0])) {
2249 isl_basic_map_free(map1
->p
[0]);
2262 struct isl_map
*isl_map_intersect(struct isl_map
*map1
, struct isl_map
*map2
)
2265 struct isl_map
*result
;
2271 if (isl_map_plain_is_empty(map1
)) {
2275 if (isl_map_plain_is_empty(map2
)) {
2280 if (map1
->n
== 1 && map2
->n
== 1 &&
2281 map1
->p
[0]->n_div
== 0 && map2
->p
[0]->n_div
== 0 &&
2282 isl_dim_equal(map1
->dim
, map2
->dim
) &&
2283 (map1
->p
[0]->n_eq
+ map1
->p
[0]->n_ineq
== 1 ||
2284 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1))
2285 return map_intersect_add_constraint(map1
, map2
);
2286 isl_assert(map1
->ctx
, isl_dim_match(map1
->dim
, isl_dim_param
,
2287 map2
->dim
, isl_dim_param
), goto error
);
2288 if (isl_dim_total(map1
->dim
) ==
2289 isl_dim_size(map1
->dim
, isl_dim_param
) &&
2290 isl_dim_total(map2
->dim
) != isl_dim_size(map2
->dim
, isl_dim_param
))
2291 return isl_map_intersect(map2
, map1
);
2293 if (isl_dim_total(map2
->dim
) != isl_dim_size(map2
->dim
, isl_dim_param
))
2294 isl_assert(map1
->ctx
,
2295 isl_dim_equal(map1
->dim
, map2
->dim
), goto error
);
2297 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
2298 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
2299 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
2301 result
= isl_map_alloc_dim(isl_dim_copy(map1
->dim
),
2302 map1
->n
* map2
->n
, flags
);
2305 for (i
= 0; i
< map1
->n
; ++i
)
2306 for (j
= 0; j
< map2
->n
; ++j
) {
2307 struct isl_basic_map
*part
;
2308 part
= isl_basic_map_intersect(
2309 isl_basic_map_copy(map1
->p
[i
]),
2310 isl_basic_map_copy(map2
->p
[j
]));
2311 if (isl_basic_map_is_empty(part
))
2312 isl_basic_map_free(part
);
2314 result
= isl_map_add_basic_map(result
, part
);
2327 struct isl_set
*isl_set_intersect(struct isl_set
*set1
, struct isl_set
*set2
)
2329 return (struct isl_set
*)
2330 isl_map_intersect((struct isl_map
*)set1
,
2331 (struct isl_map
*)set2
);
2334 /* The current implementation of isl_map_intersect accepts intersections
2335 * with parameter domains, so we can just call that for now.
2337 __isl_give isl_map
*isl_map_intersect_params(__isl_take isl_map
*map
,
2338 __isl_take isl_set
*params
)
2340 return isl_map_intersect(map
, params
);
2343 __isl_give isl_set
*isl_set_intersect_params(__isl_take isl_set
*set
,
2344 __isl_take isl_set
*params
)
2346 return isl_map_intersect_params(set
, params
);
2349 struct isl_basic_map
*isl_basic_map_reverse(struct isl_basic_map
*bmap
)
2351 struct isl_dim
*dim
;
2352 struct isl_basic_set
*bset
;
2357 bmap
= isl_basic_map_cow(bmap
);
2360 dim
= isl_dim_reverse(isl_dim_copy(bmap
->dim
));
2361 in
= isl_basic_map_n_in(bmap
);
2362 bset
= isl_basic_set_from_basic_map(bmap
);
2363 bset
= isl_basic_set_swap_vars(bset
, in
);
2364 return isl_basic_map_from_basic_set(bset
, dim
);
2367 __isl_give isl_basic_map
*isl_basic_map_insert(__isl_take isl_basic_map
*bmap
,
2368 enum isl_dim_type type
, unsigned pos
, unsigned n
)
2370 struct isl_dim
*res_dim
;
2371 struct isl_basic_map
*res
;
2372 struct isl_dim_map
*dim_map
;
2373 unsigned total
, off
;
2374 enum isl_dim_type t
;
2382 res_dim
= isl_dim_insert(isl_basic_map_get_dim(bmap
), type
, pos
, n
);
2384 total
= isl_basic_map_total_dim(bmap
) + n
;
2385 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
2387 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
2389 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
2391 unsigned size
= isl_basic_map_dim(bmap
, t
);
2392 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2394 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2395 pos
, size
- pos
, off
+ pos
+ n
);
2397 off
+= isl_dim_size(res_dim
, t
);
2399 isl_dim_map_div(dim_map
, bmap
, off
);
2401 res
= isl_basic_map_alloc_dim(res_dim
,
2402 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
2403 if (isl_basic_map_is_rational(bmap
))
2404 res
= isl_basic_map_set_rational(res
);
2405 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
2406 return isl_basic_map_finalize(res
);
2409 __isl_give isl_basic_map
*isl_basic_map_add(__isl_take isl_basic_map
*bmap
,
2410 enum isl_dim_type type
, unsigned n
)
2414 return isl_basic_map_insert(bmap
, type
,
2415 isl_basic_map_dim(bmap
, type
), n
);
2418 __isl_give isl_basic_set
*isl_basic_set_add(__isl_take isl_basic_set
*bset
,
2419 enum isl_dim_type type
, unsigned n
)
2423 isl_assert(bset
->ctx
, type
!= isl_dim_in
, goto error
);
2424 return (isl_basic_set
*)isl_basic_map_add((isl_basic_map
*)bset
, type
, n
);
2426 isl_basic_set_free(bset
);
2430 __isl_give isl_map
*isl_map_insert(__isl_take isl_map
*map
,
2431 enum isl_dim_type type
, unsigned pos
, unsigned n
)
2438 map
= isl_map_cow(map
);
2442 map
->dim
= isl_dim_insert(map
->dim
, type
, pos
, n
);
2446 for (i
= 0; i
< map
->n
; ++i
) {
2447 map
->p
[i
] = isl_basic_map_insert(map
->p
[i
], type
, pos
, n
);
2458 __isl_give isl_set
*isl_set_insert(__isl_take isl_set
*set
,
2459 enum isl_dim_type type
, unsigned pos
, unsigned n
)
2461 return (isl_set
*)isl_map_insert((isl_map
*)set
, type
, pos
, n
);
2464 __isl_give isl_map
*isl_map_add_dims(__isl_take isl_map
*map
,
2465 enum isl_dim_type type
, unsigned n
)
2469 return isl_map_insert(map
, type
, isl_map_dim(map
, type
), n
);
2472 __isl_give isl_set
*isl_set_add_dims(__isl_take isl_set
*set
,
2473 enum isl_dim_type type
, unsigned n
)
2477 isl_assert(set
->ctx
, type
!= isl_dim_in
, goto error
);
2478 return (isl_set
*)isl_map_add_dims((isl_map
*)set
, type
, n
);
2484 __isl_give isl_basic_map
*isl_basic_map_move_dims(
2485 __isl_take isl_basic_map
*bmap
,
2486 enum isl_dim_type dst_type
, unsigned dst_pos
,
2487 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2489 struct isl_dim_map
*dim_map
;
2490 struct isl_basic_map
*res
;
2491 enum isl_dim_type t
;
2492 unsigned total
, off
;
2499 isl_assert(bmap
->ctx
, src_pos
+ n
<= isl_basic_map_dim(bmap
, src_type
),
2502 if (dst_type
== src_type
&& dst_pos
== src_pos
)
2505 isl_assert(bmap
->ctx
, dst_type
!= src_type
, goto error
);
2507 if (pos(bmap
->dim
, dst_type
) + dst_pos
==
2508 pos(bmap
->dim
, src_type
) + src_pos
+
2509 ((src_type
< dst_type
) ? n
: 0)) {
2510 bmap
= isl_basic_map_cow(bmap
);
2514 bmap
->dim
= isl_dim_move(bmap
->dim
, dst_type
, dst_pos
,
2515 src_type
, src_pos
, n
);
2519 bmap
= isl_basic_map_finalize(bmap
);
2524 total
= isl_basic_map_total_dim(bmap
);
2525 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
2528 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
2529 unsigned size
= isl_dim_size(bmap
->dim
, t
);
2530 if (t
== dst_type
) {
2531 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2534 isl_dim_map_dim_range(dim_map
, bmap
->dim
, src_type
,
2537 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2538 dst_pos
, size
- dst_pos
, off
);
2539 off
+= size
- dst_pos
;
2540 } else if (t
== src_type
) {
2541 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2544 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2545 src_pos
+ n
, size
- src_pos
- n
, off
);
2546 off
+= size
- src_pos
- n
;
2548 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
2552 isl_dim_map_div(dim_map
, bmap
, off
);
2554 res
= isl_basic_map_alloc_dim(isl_basic_map_get_dim(bmap
),
2555 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
2556 bmap
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
2558 bmap
->dim
= isl_dim_move(bmap
->dim
, dst_type
, dst_pos
,
2559 src_type
, src_pos
, n
);
2563 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
2564 bmap
= isl_basic_map_gauss(bmap
, NULL
);
2565 bmap
= isl_basic_map_finalize(bmap
);
2569 isl_basic_map_free(bmap
);
2573 __isl_give isl_basic_set
*isl_basic_set_move_dims(__isl_take isl_basic_set
*bset
,
2574 enum isl_dim_type dst_type
, unsigned dst_pos
,
2575 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2577 return (isl_basic_set
*)isl_basic_map_move_dims(
2578 (isl_basic_map
*)bset
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
2581 __isl_give isl_set
*isl_set_move_dims(__isl_take isl_set
*set
,
2582 enum isl_dim_type dst_type
, unsigned dst_pos
,
2583 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2587 isl_assert(set
->ctx
, dst_type
!= isl_dim_in
, goto error
);
2588 return (isl_set
*)isl_map_move_dims((isl_map
*)set
, dst_type
, dst_pos
,
2589 src_type
, src_pos
, n
);
2595 __isl_give isl_map
*isl_map_move_dims(__isl_take isl_map
*map
,
2596 enum isl_dim_type dst_type
, unsigned dst_pos
,
2597 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2606 isl_assert(map
->ctx
, src_pos
+ n
<= isl_map_dim(map
, src_type
),
2609 if (dst_type
== src_type
&& dst_pos
== src_pos
)
2612 isl_assert(map
->ctx
, dst_type
!= src_type
, goto error
);
2614 map
= isl_map_cow(map
);
2618 map
->dim
= isl_dim_move(map
->dim
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
2622 for (i
= 0; i
< map
->n
; ++i
) {
2623 map
->p
[i
] = isl_basic_map_move_dims(map
->p
[i
],
2625 src_type
, src_pos
, n
);
2636 /* Move the specified dimensions to the last columns right before
2637 * the divs. Don't change the dimension specification of bmap.
2638 * That's the responsibility of the caller.
2640 static __isl_give isl_basic_map
*move_last(__isl_take isl_basic_map
*bmap
,
2641 enum isl_dim_type type
, unsigned first
, unsigned n
)
2643 struct isl_dim_map
*dim_map
;
2644 struct isl_basic_map
*res
;
2645 enum isl_dim_type t
;
2646 unsigned total
, off
;
2650 if (pos(bmap
->dim
, type
) + first
+ n
== 1 + isl_dim_total(bmap
->dim
))
2653 total
= isl_basic_map_total_dim(bmap
);
2654 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
2657 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
2658 unsigned size
= isl_dim_size(bmap
->dim
, t
);
2660 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2663 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2664 first
, n
, total
- bmap
->n_div
- n
);
2665 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2666 first
+ n
, size
- (first
+ n
), off
);
2667 off
+= size
- (first
+ n
);
2669 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
2673 isl_dim_map_div(dim_map
, bmap
, off
+ n
);
2675 res
= isl_basic_map_alloc_dim(isl_basic_map_get_dim(bmap
),
2676 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
2677 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
2681 /* Turn the n dimensions of type type, starting at first
2682 * into existentially quantified variables.
2684 __isl_give isl_basic_map
*isl_basic_map_project_out(
2685 __isl_take isl_basic_map
*bmap
,
2686 enum isl_dim_type type
, unsigned first
, unsigned n
)
2699 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
2700 return isl_basic_map_remove_dims(bmap
, type
, first
, n
);
2702 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
2705 bmap
= move_last(bmap
, type
, first
, n
);
2706 bmap
= isl_basic_map_cow(bmap
);
2710 row_size
= 1 + isl_dim_total(bmap
->dim
) + bmap
->extra
;
2711 old
= bmap
->block2
.data
;
2712 bmap
->block2
= isl_blk_extend(bmap
->ctx
, bmap
->block2
,
2713 (bmap
->extra
+ n
) * (1 + row_size
));
2714 if (!bmap
->block2
.data
)
2716 new_div
= isl_alloc_array(bmap
->ctx
, isl_int
*, bmap
->extra
+ n
);
2719 for (i
= 0; i
< n
; ++i
) {
2720 new_div
[i
] = bmap
->block2
.data
+
2721 (bmap
->extra
+ i
) * (1 + row_size
);
2722 isl_seq_clr(new_div
[i
], 1 + row_size
);
2724 for (i
= 0; i
< bmap
->extra
; ++i
)
2725 new_div
[n
+ i
] = bmap
->block2
.data
+ (bmap
->div
[i
] - old
);
2727 bmap
->div
= new_div
;
2731 bmap
->dim
= isl_dim_drop(bmap
->dim
, type
, first
, n
);
2734 bmap
= isl_basic_map_simplify(bmap
);
2735 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
2736 return isl_basic_map_finalize(bmap
);
2738 isl_basic_map_free(bmap
);
2742 /* Turn the n dimensions of type type, starting at first
2743 * into existentially quantified variables.
2745 struct isl_basic_set
*isl_basic_set_project_out(struct isl_basic_set
*bset
,
2746 enum isl_dim_type type
, unsigned first
, unsigned n
)
2748 return (isl_basic_set
*)isl_basic_map_project_out(
2749 (isl_basic_map
*)bset
, type
, first
, n
);
2752 /* Turn the n dimensions of type type, starting at first
2753 * into existentially quantified variables.
2755 __isl_give isl_map
*isl_map_project_out(__isl_take isl_map
*map
,
2756 enum isl_dim_type type
, unsigned first
, unsigned n
)
2766 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
2768 map
= isl_map_cow(map
);
2772 map
->dim
= isl_dim_drop(map
->dim
, type
, first
, n
);
2776 for (i
= 0; i
< map
->n
; ++i
) {
2777 map
->p
[i
] = isl_basic_map_project_out(map
->p
[i
], type
, first
, n
);
2788 /* Turn the n dimensions of type type, starting at first
2789 * into existentially quantified variables.
2791 __isl_give isl_set
*isl_set_project_out(__isl_take isl_set
*set
,
2792 enum isl_dim_type type
, unsigned first
, unsigned n
)
2794 return (isl_set
*)isl_map_project_out((isl_map
*)set
, type
, first
, n
);
2797 static struct isl_basic_map
*add_divs(struct isl_basic_map
*bmap
, unsigned n
)
2801 for (i
= 0; i
< n
; ++i
) {
2802 j
= isl_basic_map_alloc_div(bmap
);
2805 isl_seq_clr(bmap
->div
[j
], 1+1+isl_basic_map_total_dim(bmap
));
2809 isl_basic_map_free(bmap
);
2813 struct isl_basic_map
*isl_basic_map_apply_range(
2814 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
2816 struct isl_dim
*dim_result
= NULL
;
2817 struct isl_basic_map
*bmap
;
2818 unsigned n_in
, n_out
, n
, nparam
, total
, pos
;
2819 struct isl_dim_map
*dim_map1
, *dim_map2
;
2821 if (!bmap1
|| !bmap2
)
2824 dim_result
= isl_dim_join(isl_dim_copy(bmap1
->dim
),
2825 isl_dim_copy(bmap2
->dim
));
2827 n_in
= isl_basic_map_n_in(bmap1
);
2828 n_out
= isl_basic_map_n_out(bmap2
);
2829 n
= isl_basic_map_n_out(bmap1
);
2830 nparam
= isl_basic_map_n_param(bmap1
);
2832 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ n
;
2833 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
2834 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
2835 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
2836 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
2837 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
2838 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_in
);
2839 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_out
);
2840 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
2841 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
2842 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
2844 bmap
= isl_basic_map_alloc_dim(dim_result
,
2845 bmap1
->n_div
+ bmap2
->n_div
+ n
,
2846 bmap1
->n_eq
+ bmap2
->n_eq
,
2847 bmap1
->n_ineq
+ bmap2
->n_ineq
);
2848 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
2849 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
2850 bmap
= add_divs(bmap
, n
);
2851 bmap
= isl_basic_map_simplify(bmap
);
2852 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
2853 return isl_basic_map_finalize(bmap
);
2855 isl_basic_map_free(bmap1
);
2856 isl_basic_map_free(bmap2
);
2860 struct isl_basic_set
*isl_basic_set_apply(
2861 struct isl_basic_set
*bset
, struct isl_basic_map
*bmap
)
2866 isl_assert(bset
->ctx
, isl_basic_map_compatible_domain(bmap
, bset
),
2869 return (struct isl_basic_set
*)
2870 isl_basic_map_apply_range((struct isl_basic_map
*)bset
, bmap
);
2872 isl_basic_set_free(bset
);
2873 isl_basic_map_free(bmap
);
2877 struct isl_basic_map
*isl_basic_map_apply_domain(
2878 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
2880 if (!bmap1
|| !bmap2
)
2883 isl_assert(bmap1
->ctx
,
2884 isl_basic_map_n_in(bmap1
) == isl_basic_map_n_in(bmap2
), goto error
);
2885 isl_assert(bmap1
->ctx
,
2886 isl_basic_map_n_param(bmap1
) == isl_basic_map_n_param(bmap2
),
2889 bmap1
= isl_basic_map_reverse(bmap1
);
2890 bmap1
= isl_basic_map_apply_range(bmap1
, bmap2
);
2891 return isl_basic_map_reverse(bmap1
);
2893 isl_basic_map_free(bmap1
);
2894 isl_basic_map_free(bmap2
);
2898 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
2899 * A \cap B -> f(A) + f(B)
2901 struct isl_basic_map
*isl_basic_map_sum(
2902 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
2904 unsigned n_in
, n_out
, nparam
, total
, pos
;
2905 struct isl_basic_map
*bmap
= NULL
;
2906 struct isl_dim_map
*dim_map1
, *dim_map2
;
2909 if (!bmap1
|| !bmap2
)
2912 isl_assert(bmap1
->ctx
, isl_dim_equal(bmap1
->dim
, bmap2
->dim
),
2915 nparam
= isl_basic_map_n_param(bmap1
);
2916 n_in
= isl_basic_map_n_in(bmap1
);
2917 n_out
= isl_basic_map_n_out(bmap1
);
2919 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
;
2920 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
2921 dim_map2
= isl_dim_map_alloc(bmap2
->ctx
, total
);
2922 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
2923 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
);
2924 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
2925 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
2926 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_in
+ n_out
);
2927 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
2928 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
2929 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_out
);
2931 bmap
= isl_basic_map_alloc_dim(isl_dim_copy(bmap1
->dim
),
2932 bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
,
2933 bmap1
->n_eq
+ bmap2
->n_eq
+ n_out
,
2934 bmap1
->n_ineq
+ bmap2
->n_ineq
);
2935 for (i
= 0; i
< n_out
; ++i
) {
2936 int j
= isl_basic_map_alloc_equality(bmap
);
2939 isl_seq_clr(bmap
->eq
[j
], 1+total
);
2940 isl_int_set_si(bmap
->eq
[j
][1+nparam
+n_in
+i
], -1);
2941 isl_int_set_si(bmap
->eq
[j
][1+pos
+i
], 1);
2942 isl_int_set_si(bmap
->eq
[j
][1+pos
-n_out
+i
], 1);
2944 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
2945 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
2946 bmap
= add_divs(bmap
, 2 * n_out
);
2948 bmap
= isl_basic_map_simplify(bmap
);
2949 return isl_basic_map_finalize(bmap
);
2951 isl_basic_map_free(bmap
);
2952 isl_basic_map_free(bmap1
);
2953 isl_basic_map_free(bmap2
);
2957 /* Given two maps A -> f(A) and B -> g(B), construct a map
2958 * A \cap B -> f(A) + f(B)
2960 struct isl_map
*isl_map_sum(struct isl_map
*map1
, struct isl_map
*map2
)
2962 struct isl_map
*result
;
2968 isl_assert(map1
->ctx
, isl_dim_equal(map1
->dim
, map2
->dim
), goto error
);
2970 result
= isl_map_alloc_dim(isl_dim_copy(map1
->dim
),
2971 map1
->n
* map2
->n
, 0);
2974 for (i
= 0; i
< map1
->n
; ++i
)
2975 for (j
= 0; j
< map2
->n
; ++j
) {
2976 struct isl_basic_map
*part
;
2977 part
= isl_basic_map_sum(
2978 isl_basic_map_copy(map1
->p
[i
]),
2979 isl_basic_map_copy(map2
->p
[j
]));
2980 if (isl_basic_map_is_empty(part
))
2981 isl_basic_map_free(part
);
2983 result
= isl_map_add_basic_map(result
, part
);
2996 __isl_give isl_set
*isl_set_sum(__isl_take isl_set
*set1
,
2997 __isl_take isl_set
*set2
)
2999 return (isl_set
*)isl_map_sum((isl_map
*)set1
, (isl_map
*)set2
);
3002 /* Given a basic map A -> f(A), construct A -> -f(A).
3004 struct isl_basic_map
*isl_basic_map_neg(struct isl_basic_map
*bmap
)
3009 bmap
= isl_basic_map_cow(bmap
);
3013 n
= isl_basic_map_dim(bmap
, isl_dim_out
);
3014 off
= isl_basic_map_offset(bmap
, isl_dim_out
);
3015 for (i
= 0; i
< bmap
->n_eq
; ++i
)
3016 for (j
= 0; j
< n
; ++j
)
3017 isl_int_neg(bmap
->eq
[i
][off
+j
], bmap
->eq
[i
][off
+j
]);
3018 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
3019 for (j
= 0; j
< n
; ++j
)
3020 isl_int_neg(bmap
->ineq
[i
][off
+j
], bmap
->ineq
[i
][off
+j
]);
3021 for (i
= 0; i
< bmap
->n_div
; ++i
)
3022 for (j
= 0; j
< n
; ++j
)
3023 isl_int_neg(bmap
->div
[i
][1+off
+j
], bmap
->div
[i
][1+off
+j
]);
3024 return isl_basic_map_finalize(bmap
);
3027 __isl_give isl_basic_set
*isl_basic_set_neg(__isl_take isl_basic_set
*bset
)
3029 return isl_basic_map_neg(bset
);
3032 /* Given a map A -> f(A), construct A -> -f(A).
3034 struct isl_map
*isl_map_neg(struct isl_map
*map
)
3038 map
= isl_map_cow(map
);
3042 for (i
= 0; i
< map
->n
; ++i
) {
3043 map
->p
[i
] = isl_basic_map_neg(map
->p
[i
]);
3054 __isl_give isl_set
*isl_set_neg(__isl_take isl_set
*set
)
3056 return (isl_set
*)isl_map_neg((isl_map
*)set
);
3059 /* Given a basic map A -> f(A) and an integer d, construct a basic map
3060 * A -> floor(f(A)/d).
3062 struct isl_basic_map
*isl_basic_map_floordiv(struct isl_basic_map
*bmap
,
3065 unsigned n_in
, n_out
, nparam
, total
, pos
;
3066 struct isl_basic_map
*result
= NULL
;
3067 struct isl_dim_map
*dim_map
;
3073 nparam
= isl_basic_map_n_param(bmap
);
3074 n_in
= isl_basic_map_n_in(bmap
);
3075 n_out
= isl_basic_map_n_out(bmap
);
3077 total
= nparam
+ n_in
+ n_out
+ bmap
->n_div
+ n_out
;
3078 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3079 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_param
, pos
= 0);
3080 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_in
, pos
+= nparam
);
3081 isl_dim_map_div(dim_map
, bmap
, pos
+= n_in
+ n_out
);
3082 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_out
, pos
+= bmap
->n_div
);
3084 result
= isl_basic_map_alloc_dim(isl_dim_copy(bmap
->dim
),
3085 bmap
->n_div
+ n_out
,
3086 bmap
->n_eq
, bmap
->n_ineq
+ 2 * n_out
);
3087 result
= isl_basic_map_add_constraints_dim_map(result
, bmap
, dim_map
);
3088 result
= add_divs(result
, n_out
);
3089 for (i
= 0; i
< n_out
; ++i
) {
3091 j
= isl_basic_map_alloc_inequality(result
);
3094 isl_seq_clr(result
->ineq
[j
], 1+total
);
3095 isl_int_neg(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
3096 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], 1);
3097 j
= isl_basic_map_alloc_inequality(result
);
3100 isl_seq_clr(result
->ineq
[j
], 1+total
);
3101 isl_int_set(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
3102 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], -1);
3103 isl_int_sub_ui(result
->ineq
[j
][0], d
, 1);
3106 result
= isl_basic_map_simplify(result
);
3107 return isl_basic_map_finalize(result
);
3109 isl_basic_map_free(result
);
3113 /* Given a map A -> f(A) and an integer d, construct a map
3114 * A -> floor(f(A)/d).
3116 struct isl_map
*isl_map_floordiv(struct isl_map
*map
, isl_int d
)
3120 map
= isl_map_cow(map
);
3124 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
3125 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3126 for (i
= 0; i
< map
->n
; ++i
) {
3127 map
->p
[i
] = isl_basic_map_floordiv(map
->p
[i
], d
);
3138 static struct isl_basic_map
*var_equal(struct isl_basic_map
*bmap
, unsigned pos
)
3144 i
= isl_basic_map_alloc_equality(bmap
);
3147 nparam
= isl_basic_map_n_param(bmap
);
3148 n_in
= isl_basic_map_n_in(bmap
);
3149 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3150 isl_int_set_si(bmap
->eq
[i
][1+nparam
+pos
], -1);
3151 isl_int_set_si(bmap
->eq
[i
][1+nparam
+n_in
+pos
], 1);
3152 return isl_basic_map_finalize(bmap
);
3154 isl_basic_map_free(bmap
);
3158 /* Add a constraints to "bmap" expressing i_pos < o_pos
3160 static struct isl_basic_map
*var_less(struct isl_basic_map
*bmap
, unsigned pos
)
3166 i
= isl_basic_map_alloc_inequality(bmap
);
3169 nparam
= isl_basic_map_n_param(bmap
);
3170 n_in
= isl_basic_map_n_in(bmap
);
3171 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3172 isl_int_set_si(bmap
->ineq
[i
][0], -1);
3173 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
3174 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
3175 return isl_basic_map_finalize(bmap
);
3177 isl_basic_map_free(bmap
);
3181 /* Add a constraint to "bmap" expressing i_pos <= o_pos
3183 static __isl_give isl_basic_map
*var_less_or_equal(
3184 __isl_take isl_basic_map
*bmap
, unsigned pos
)
3190 i
= isl_basic_map_alloc_inequality(bmap
);
3193 nparam
= isl_basic_map_n_param(bmap
);
3194 n_in
= isl_basic_map_n_in(bmap
);
3195 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3196 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
3197 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
3198 return isl_basic_map_finalize(bmap
);
3200 isl_basic_map_free(bmap
);
3204 /* Add a constraints to "bmap" expressing i_pos > o_pos
3206 static struct isl_basic_map
*var_more(struct isl_basic_map
*bmap
, unsigned pos
)
3212 i
= isl_basic_map_alloc_inequality(bmap
);
3215 nparam
= isl_basic_map_n_param(bmap
);
3216 n_in
= isl_basic_map_n_in(bmap
);
3217 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3218 isl_int_set_si(bmap
->ineq
[i
][0], -1);
3219 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
3220 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
3221 return isl_basic_map_finalize(bmap
);
3223 isl_basic_map_free(bmap
);
3227 /* Add a constraint to "bmap" expressing i_pos >= o_pos
3229 static __isl_give isl_basic_map
*var_more_or_equal(
3230 __isl_take isl_basic_map
*bmap
, unsigned pos
)
3236 i
= isl_basic_map_alloc_inequality(bmap
);
3239 nparam
= isl_basic_map_n_param(bmap
);
3240 n_in
= isl_basic_map_n_in(bmap
);
3241 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3242 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
3243 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
3244 return isl_basic_map_finalize(bmap
);
3246 isl_basic_map_free(bmap
);
3250 struct isl_basic_map
*isl_basic_map_equal(struct isl_dim
*dim
, unsigned n_equal
)
3253 struct isl_basic_map
*bmap
;
3254 bmap
= isl_basic_map_alloc_dim(dim
, 0, n_equal
, 0);
3257 for (i
= 0; i
< n_equal
&& bmap
; ++i
)
3258 bmap
= var_equal(bmap
, i
);
3259 return isl_basic_map_finalize(bmap
);
3262 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
3264 struct isl_basic_map
*isl_basic_map_less_at(struct isl_dim
*dim
, unsigned pos
)
3267 struct isl_basic_map
*bmap
;
3268 bmap
= isl_basic_map_alloc_dim(dim
, 0, pos
, 1);
3271 for (i
= 0; i
< pos
&& bmap
; ++i
)
3272 bmap
= var_equal(bmap
, i
);
3274 bmap
= var_less(bmap
, pos
);
3275 return isl_basic_map_finalize(bmap
);
3278 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
3280 __isl_give isl_basic_map
*isl_basic_map_less_or_equal_at(
3281 __isl_take isl_dim
*dim
, unsigned pos
)
3284 isl_basic_map
*bmap
;
3286 bmap
= isl_basic_map_alloc_dim(dim
, 0, pos
, 1);
3287 for (i
= 0; i
< pos
; ++i
)
3288 bmap
= var_equal(bmap
, i
);
3289 bmap
= var_less_or_equal(bmap
, pos
);
3290 return isl_basic_map_finalize(bmap
);
3293 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
3295 struct isl_basic_map
*isl_basic_map_more_at(struct isl_dim
*dim
, unsigned pos
)
3298 struct isl_basic_map
*bmap
;
3299 bmap
= isl_basic_map_alloc_dim(dim
, 0, pos
, 1);
3302 for (i
= 0; i
< pos
&& bmap
; ++i
)
3303 bmap
= var_equal(bmap
, i
);
3305 bmap
= var_more(bmap
, pos
);
3306 return isl_basic_map_finalize(bmap
);
3309 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
3311 __isl_give isl_basic_map
*isl_basic_map_more_or_equal_at(
3312 __isl_take isl_dim
*dim
, unsigned pos
)
3315 isl_basic_map
*bmap
;
3317 bmap
= isl_basic_map_alloc_dim(dim
, 0, pos
, 1);
3318 for (i
= 0; i
< pos
; ++i
)
3319 bmap
= var_equal(bmap
, i
);
3320 bmap
= var_more_or_equal(bmap
, pos
);
3321 return isl_basic_map_finalize(bmap
);
3324 static __isl_give isl_map
*map_lex_lte_first(__isl_take isl_dim
*dims
,
3325 unsigned n
, int equal
)
3327 struct isl_map
*map
;
3330 if (n
== 0 && equal
)
3331 return isl_map_universe(dims
);
3333 map
= isl_map_alloc_dim(isl_dim_copy(dims
), n
, ISL_MAP_DISJOINT
);
3335 for (i
= 0; i
+ 1 < n
; ++i
)
3336 map
= isl_map_add_basic_map(map
,
3337 isl_basic_map_less_at(isl_dim_copy(dims
), i
));
3340 map
= isl_map_add_basic_map(map
,
3341 isl_basic_map_less_or_equal_at(dims
, n
- 1));
3343 map
= isl_map_add_basic_map(map
,
3344 isl_basic_map_less_at(dims
, n
- 1));
3351 static __isl_give isl_map
*map_lex_lte(__isl_take isl_dim
*dims
, int equal
)
3355 return map_lex_lte_first(dims
, dims
->n_out
, equal
);
3358 __isl_give isl_map
*isl_map_lex_lt_first(__isl_take isl_dim
*dim
, unsigned n
)
3360 return map_lex_lte_first(dim
, n
, 0);
3363 __isl_give isl_map
*isl_map_lex_le_first(__isl_take isl_dim
*dim
, unsigned n
)
3365 return map_lex_lte_first(dim
, n
, 1);
3368 __isl_give isl_map
*isl_map_lex_lt(__isl_take isl_dim
*set_dim
)
3370 return map_lex_lte(isl_dim_map_from_set(set_dim
), 0);
3373 __isl_give isl_map
*isl_map_lex_le(__isl_take isl_dim
*set_dim
)
3375 return map_lex_lte(isl_dim_map_from_set(set_dim
), 1);
3378 static __isl_give isl_map
*map_lex_gte_first(__isl_take isl_dim
*dims
,
3379 unsigned n
, int equal
)
3381 struct isl_map
*map
;
3384 if (n
== 0 && equal
)
3385 return isl_map_universe(dims
);
3387 map
= isl_map_alloc_dim(isl_dim_copy(dims
), n
, ISL_MAP_DISJOINT
);
3389 for (i
= 0; i
+ 1 < n
; ++i
)
3390 map
= isl_map_add_basic_map(map
,
3391 isl_basic_map_more_at(isl_dim_copy(dims
), i
));
3394 map
= isl_map_add_basic_map(map
,
3395 isl_basic_map_more_or_equal_at(dims
, n
- 1));
3397 map
= isl_map_add_basic_map(map
,
3398 isl_basic_map_more_at(dims
, n
- 1));
3405 static __isl_give isl_map
*map_lex_gte(__isl_take isl_dim
*dims
, int equal
)
3409 return map_lex_gte_first(dims
, dims
->n_out
, equal
);
3412 __isl_give isl_map
*isl_map_lex_gt_first(__isl_take isl_dim
*dim
, unsigned n
)
3414 return map_lex_gte_first(dim
, n
, 0);
3417 __isl_give isl_map
*isl_map_lex_ge_first(__isl_take isl_dim
*dim
, unsigned n
)
3419 return map_lex_gte_first(dim
, n
, 1);
3422 __isl_give isl_map
*isl_map_lex_gt(__isl_take isl_dim
*set_dim
)
3424 return map_lex_gte(isl_dim_map_from_set(set_dim
), 0);
3427 __isl_give isl_map
*isl_map_lex_ge(__isl_take isl_dim
*set_dim
)
3429 return map_lex_gte(isl_dim_map_from_set(set_dim
), 1);
3432 __isl_give isl_map
*isl_set_lex_le_set(__isl_take isl_set
*set1
,
3433 __isl_take isl_set
*set2
)
3436 map
= isl_map_lex_le(isl_set_get_dim(set1
));
3437 map
= isl_map_intersect_domain(map
, set1
);
3438 map
= isl_map_intersect_range(map
, set2
);
3442 __isl_give isl_map
*isl_set_lex_lt_set(__isl_take isl_set
*set1
,
3443 __isl_take isl_set
*set2
)
3446 map
= isl_map_lex_lt(isl_set_get_dim(set1
));
3447 map
= isl_map_intersect_domain(map
, set1
);
3448 map
= isl_map_intersect_range(map
, set2
);
3452 __isl_give isl_map
*isl_set_lex_ge_set(__isl_take isl_set
*set1
,
3453 __isl_take isl_set
*set2
)
3456 map
= isl_map_lex_ge(isl_set_get_dim(set1
));
3457 map
= isl_map_intersect_domain(map
, set1
);
3458 map
= isl_map_intersect_range(map
, set2
);
3462 __isl_give isl_map
*isl_set_lex_gt_set(__isl_take isl_set
*set1
,
3463 __isl_take isl_set
*set2
)
3466 map
= isl_map_lex_gt(isl_set_get_dim(set1
));
3467 map
= isl_map_intersect_domain(map
, set1
);
3468 map
= isl_map_intersect_range(map
, set2
);
3472 __isl_give isl_map
*isl_map_lex_le_map(__isl_take isl_map
*map1
,
3473 __isl_take isl_map
*map2
)
3476 map
= isl_map_lex_le(isl_dim_range(isl_map_get_dim(map1
)));
3477 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
3478 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
3482 __isl_give isl_map
*isl_map_lex_lt_map(__isl_take isl_map
*map1
,
3483 __isl_take isl_map
*map2
)
3486 map
= isl_map_lex_lt(isl_dim_range(isl_map_get_dim(map1
)));
3487 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
3488 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
3492 __isl_give isl_map
*isl_map_lex_ge_map(__isl_take isl_map
*map1
,
3493 __isl_take isl_map
*map2
)
3496 map
= isl_map_lex_ge(isl_dim_range(isl_map_get_dim(map1
)));
3497 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
3498 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
3502 __isl_give isl_map
*isl_map_lex_gt_map(__isl_take isl_map
*map1
,
3503 __isl_take isl_map
*map2
)
3506 map
= isl_map_lex_gt(isl_dim_range(isl_map_get_dim(map1
)));
3507 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
3508 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
3512 struct isl_basic_map
*isl_basic_map_from_basic_set(
3513 struct isl_basic_set
*bset
, struct isl_dim
*dim
)
3515 struct isl_basic_map
*bmap
;
3517 bset
= isl_basic_set_cow(bset
);
3521 isl_assert(bset
->ctx
, isl_dim_compatible(bset
->dim
, dim
), goto error
);
3522 isl_dim_free(bset
->dim
);
3523 bmap
= (struct isl_basic_map
*) bset
;
3525 return isl_basic_map_finalize(bmap
);
3527 isl_basic_set_free(bset
);
3532 struct isl_basic_set
*isl_basic_set_from_basic_map(struct isl_basic_map
*bmap
)
3536 if (bmap
->dim
->n_in
== 0)
3537 return (struct isl_basic_set
*)bmap
;
3538 bmap
= isl_basic_map_cow(bmap
);
3541 bmap
->dim
= isl_dim_as_set_dim(bmap
->dim
);
3544 bmap
= isl_basic_map_finalize(bmap
);
3545 return (struct isl_basic_set
*)bmap
;
3547 isl_basic_map_free(bmap
);
3551 /* For a div d = floor(f/m), add the constraints
3554 * -(f-(n-1)) + m d >= 0
3556 * Note that the second constraint is the negation of
3560 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map
*bmap
,
3561 unsigned pos
, isl_int
*div
)
3564 unsigned total
= isl_basic_map_total_dim(bmap
);
3566 i
= isl_basic_map_alloc_inequality(bmap
);
3569 isl_seq_cpy(bmap
->ineq
[i
], div
+ 1, 1 + total
);
3570 isl_int_neg(bmap
->ineq
[i
][1 + pos
], div
[0]);
3572 j
= isl_basic_map_alloc_inequality(bmap
);
3575 isl_seq_neg(bmap
->ineq
[j
], bmap
->ineq
[i
], 1 + total
);
3576 isl_int_add(bmap
->ineq
[j
][0], bmap
->ineq
[j
][0], bmap
->ineq
[j
][1 + pos
]);
3577 isl_int_sub_ui(bmap
->ineq
[j
][0], bmap
->ineq
[j
][0], 1);
3581 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set
*bset
,
3582 unsigned pos
, isl_int
*div
)
3584 return isl_basic_map_add_div_constraints_var((isl_basic_map
*)bset
,
3588 int isl_basic_map_add_div_constraints(struct isl_basic_map
*bmap
, unsigned div
)
3590 unsigned total
= isl_basic_map_total_dim(bmap
);
3591 unsigned div_pos
= total
- bmap
->n_div
+ div
;
3593 return isl_basic_map_add_div_constraints_var(bmap
, div_pos
,
3597 struct isl_basic_set
*isl_basic_map_underlying_set(
3598 struct isl_basic_map
*bmap
)
3602 if (bmap
->dim
->nparam
== 0 && bmap
->dim
->n_in
== 0 &&
3604 !isl_dim_is_named_or_nested(bmap
->dim
, isl_dim_in
) &&
3605 !isl_dim_is_named_or_nested(bmap
->dim
, isl_dim_out
))
3606 return (struct isl_basic_set
*)bmap
;
3607 bmap
= isl_basic_map_cow(bmap
);
3610 bmap
->dim
= isl_dim_underlying(bmap
->dim
, bmap
->n_div
);
3613 bmap
->extra
-= bmap
->n_div
;
3615 bmap
= isl_basic_map_finalize(bmap
);
3616 return (struct isl_basic_set
*)bmap
;
3621 __isl_give isl_basic_set
*isl_basic_set_underlying_set(
3622 __isl_take isl_basic_set
*bset
)
3624 return isl_basic_map_underlying_set((isl_basic_map
*)bset
);
3627 struct isl_basic_map
*isl_basic_map_overlying_set(
3628 struct isl_basic_set
*bset
, struct isl_basic_map
*like
)
3630 struct isl_basic_map
*bmap
;
3631 struct isl_ctx
*ctx
;
3638 isl_assert(ctx
, bset
->n_div
== 0, goto error
);
3639 isl_assert(ctx
, isl_basic_set_n_param(bset
) == 0, goto error
);
3640 isl_assert(ctx
, bset
->dim
->n_out
== isl_basic_map_total_dim(like
),
3642 if (isl_dim_equal(bset
->dim
, like
->dim
) && like
->n_div
== 0) {
3643 isl_basic_map_free(like
);
3644 return (struct isl_basic_map
*)bset
;
3646 bset
= isl_basic_set_cow(bset
);
3649 total
= bset
->dim
->n_out
+ bset
->extra
;
3650 bmap
= (struct isl_basic_map
*)bset
;
3651 isl_dim_free(bmap
->dim
);
3652 bmap
->dim
= isl_dim_copy(like
->dim
);
3655 bmap
->n_div
= like
->n_div
;
3656 bmap
->extra
+= like
->n_div
;
3660 ltotal
= total
- bmap
->extra
+ like
->extra
;
3663 bmap
->block2
= isl_blk_extend(ctx
, bmap
->block2
,
3664 bmap
->extra
* (1 + 1 + total
));
3665 if (isl_blk_is_error(bmap
->block2
))
3667 div
= isl_realloc_array(ctx
, bmap
->div
, isl_int
*, bmap
->extra
);
3671 for (i
= 0; i
< bmap
->extra
; ++i
)
3672 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + 1 + total
);
3673 for (i
= 0; i
< like
->n_div
; ++i
) {
3674 isl_seq_cpy(bmap
->div
[i
], like
->div
[i
], 1 + 1 + ltotal
);
3675 isl_seq_clr(bmap
->div
[i
]+1+1+ltotal
, total
- ltotal
);
3677 bmap
= isl_basic_map_extend_constraints(bmap
,
3678 0, 2 * like
->n_div
);
3679 for (i
= 0; i
< like
->n_div
; ++i
) {
3680 if (isl_int_is_zero(bmap
->div
[i
][0]))
3682 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
3686 isl_basic_map_free(like
);
3687 bmap
= isl_basic_map_simplify(bmap
);
3688 bmap
= isl_basic_map_finalize(bmap
);
3691 isl_basic_map_free(like
);
3692 isl_basic_set_free(bset
);
3696 struct isl_basic_set
*isl_basic_set_from_underlying_set(
3697 struct isl_basic_set
*bset
, struct isl_basic_set
*like
)
3699 return (struct isl_basic_set
*)
3700 isl_basic_map_overlying_set(bset
, (struct isl_basic_map
*)like
);
3703 struct isl_set
*isl_set_from_underlying_set(
3704 struct isl_set
*set
, struct isl_basic_set
*like
)
3710 isl_assert(set
->ctx
, set
->dim
->n_out
== isl_basic_set_total_dim(like
),
3712 if (isl_dim_equal(set
->dim
, like
->dim
) && like
->n_div
== 0) {
3713 isl_basic_set_free(like
);
3716 set
= isl_set_cow(set
);
3719 for (i
= 0; i
< set
->n
; ++i
) {
3720 set
->p
[i
] = isl_basic_set_from_underlying_set(set
->p
[i
],
3721 isl_basic_set_copy(like
));
3725 isl_dim_free(set
->dim
);
3726 set
->dim
= isl_dim_copy(like
->dim
);
3729 isl_basic_set_free(like
);
3732 isl_basic_set_free(like
);
3737 struct isl_set
*isl_map_underlying_set(struct isl_map
*map
)
3741 map
= isl_map_cow(map
);
3744 map
->dim
= isl_dim_cow(map
->dim
);
3748 for (i
= 1; i
< map
->n
; ++i
)
3749 isl_assert(map
->ctx
, map
->p
[0]->n_div
== map
->p
[i
]->n_div
,
3751 for (i
= 0; i
< map
->n
; ++i
) {
3752 map
->p
[i
] = (struct isl_basic_map
*)
3753 isl_basic_map_underlying_set(map
->p
[i
]);
3758 map
->dim
= isl_dim_underlying(map
->dim
, 0);
3760 isl_dim_free(map
->dim
);
3761 map
->dim
= isl_dim_copy(map
->p
[0]->dim
);
3765 return (struct isl_set
*)map
;
3771 struct isl_set
*isl_set_to_underlying_set(struct isl_set
*set
)
3773 return (struct isl_set
*)isl_map_underlying_set((struct isl_map
*)set
);
3776 __isl_give isl_basic_map
*isl_basic_map_reset_dim(
3777 __isl_take isl_basic_map
*bmap
, __isl_take isl_dim
*dim
)
3779 bmap
= isl_basic_map_cow(bmap
);
3783 isl_dim_free(bmap
->dim
);
3786 bmap
= isl_basic_map_finalize(bmap
);
3790 isl_basic_map_free(bmap
);
3795 __isl_give isl_basic_set
*isl_basic_set_reset_dim(
3796 __isl_take isl_basic_set
*bset
, __isl_take isl_dim
*dim
)
3798 return (isl_basic_set
*)isl_basic_map_reset_dim((isl_basic_map
*)bset
,
3802 __isl_give isl_map
*isl_map_reset_dim(__isl_take isl_map
*map
,
3803 __isl_take isl_dim
*dim
)
3807 map
= isl_map_cow(map
);
3811 for (i
= 0; i
< map
->n
; ++i
) {
3812 map
->p
[i
] = isl_basic_map_reset_dim(map
->p
[i
],
3817 isl_dim_free(map
->dim
);
3827 __isl_give isl_set
*isl_set_reset_dim(__isl_take isl_set
*set
,
3828 __isl_take isl_dim
*dim
)
3830 return (struct isl_set
*) isl_map_reset_dim((struct isl_map
*)set
, dim
);
3833 struct isl_basic_set
*isl_basic_map_domain(struct isl_basic_map
*bmap
)
3836 struct isl_basic_set
*domain
;
3842 dim
= isl_dim_domain(isl_basic_map_get_dim(bmap
));
3844 n_in
= isl_basic_map_n_in(bmap
);
3845 n_out
= isl_basic_map_n_out(bmap
);
3846 domain
= isl_basic_set_from_basic_map(bmap
);
3847 domain
= isl_basic_set_project_out(domain
, isl_dim_set
, n_in
, n_out
);
3849 domain
= isl_basic_set_reset_dim(domain
, dim
);
3854 int isl_basic_map_may_be_set(__isl_keep isl_basic_map
*bmap
)
3858 return isl_dim_may_be_set(bmap
->dim
);
3861 struct isl_basic_set
*isl_basic_map_range(struct isl_basic_map
*bmap
)
3865 if (isl_basic_map_may_be_set(bmap
))
3867 return isl_basic_map_domain(isl_basic_map_reverse(bmap
));
3870 __isl_give isl_basic_map
*isl_basic_map_domain_map(
3871 __isl_take isl_basic_map
*bmap
)
3875 isl_basic_map
*domain
;
3876 int nparam
, n_in
, n_out
;
3879 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
3880 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
3881 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
3883 dim
= isl_dim_from_range(isl_dim_domain(isl_basic_map_get_dim(bmap
)));
3884 domain
= isl_basic_map_universe(dim
);
3886 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
3887 bmap
= isl_basic_map_apply_range(bmap
, domain
);
3888 bmap
= isl_basic_map_extend_constraints(bmap
, n_in
, 0);
3890 total
= isl_basic_map_total_dim(bmap
);
3892 for (i
= 0; i
< n_in
; ++i
) {
3893 k
= isl_basic_map_alloc_equality(bmap
);
3896 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
3897 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], -1);
3898 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ n_out
+ i
], 1);
3901 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3902 return isl_basic_map_finalize(bmap
);
3904 isl_basic_map_free(bmap
);
3908 __isl_give isl_basic_map
*isl_basic_map_range_map(
3909 __isl_take isl_basic_map
*bmap
)
3913 isl_basic_map
*range
;
3914 int nparam
, n_in
, n_out
;
3917 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
3918 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
3919 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
3921 dim
= isl_dim_from_range(isl_dim_range(isl_basic_map_get_dim(bmap
)));
3922 range
= isl_basic_map_universe(dim
);
3924 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
3925 bmap
= isl_basic_map_apply_range(bmap
, range
);
3926 bmap
= isl_basic_map_extend_constraints(bmap
, n_out
, 0);
3928 total
= isl_basic_map_total_dim(bmap
);
3930 for (i
= 0; i
< n_out
; ++i
) {
3931 k
= isl_basic_map_alloc_equality(bmap
);
3934 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
3935 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ i
], -1);
3936 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ n_out
+ i
], 1);
3939 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3940 return isl_basic_map_finalize(bmap
);
3942 isl_basic_map_free(bmap
);
3946 int isl_map_may_be_set(__isl_keep isl_map
*map
)
3950 return isl_dim_may_be_set(map
->dim
);
3953 struct isl_set
*isl_map_range(struct isl_map
*map
)
3956 struct isl_set
*set
;
3960 if (isl_map_may_be_set(map
))
3961 return (isl_set
*)map
;
3963 map
= isl_map_cow(map
);
3967 set
= (struct isl_set
*) map
;
3968 set
->dim
= isl_dim_drop_inputs(set
->dim
, 0, set
->dim
->n_in
);
3971 for (i
= 0; i
< map
->n
; ++i
) {
3972 set
->p
[i
] = isl_basic_map_range(map
->p
[i
]);
3976 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
3977 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
3984 __isl_give isl_map
*isl_map_domain_map(__isl_take isl_map
*map
)
3987 isl_dim
*domain_dim
;
3989 map
= isl_map_cow(map
);
3993 domain_dim
= isl_dim_from_range(isl_dim_domain(isl_map_get_dim(map
)));
3994 map
->dim
= isl_dim_from_domain(isl_dim_wrap(map
->dim
));
3995 map
->dim
= isl_dim_join(map
->dim
, domain_dim
);
3998 for (i
= 0; i
< map
->n
; ++i
) {
3999 map
->p
[i
] = isl_basic_map_domain_map(map
->p
[i
]);
4003 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
4004 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4011 __isl_give isl_map
*isl_map_range_map(__isl_take isl_map
*map
)
4016 map
= isl_map_cow(map
);
4020 range_dim
= isl_dim_range(isl_map_get_dim(map
));
4021 map
->dim
= isl_dim_from_domain(isl_dim_wrap(map
->dim
));
4022 map
->dim
= isl_dim_join(map
->dim
, range_dim
);
4025 for (i
= 0; i
< map
->n
; ++i
) {
4026 map
->p
[i
] = isl_basic_map_range_map(map
->p
[i
]);
4030 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
4031 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4038 struct isl_map
*isl_map_from_set(struct isl_set
*set
, struct isl_dim
*dim
)
4041 struct isl_map
*map
= NULL
;
4043 set
= isl_set_cow(set
);
4046 isl_assert(set
->ctx
, isl_dim_compatible(set
->dim
, dim
), goto error
);
4047 map
= (struct isl_map
*)set
;
4048 for (i
= 0; i
< set
->n
; ++i
) {
4049 map
->p
[i
] = isl_basic_map_from_basic_set(
4050 set
->p
[i
], isl_dim_copy(dim
));
4054 isl_dim_free(map
->dim
);
4063 __isl_give isl_basic_map
*isl_basic_map_from_domain(
4064 __isl_take isl_basic_set
*bset
)
4066 return isl_basic_map_reverse(isl_basic_map_from_range(bset
));
4069 __isl_give isl_basic_map
*isl_basic_map_from_range(
4070 __isl_take isl_basic_set
*bset
)
4072 return (isl_basic_map
*)bset
;
4075 struct isl_map
*isl_map_from_range(struct isl_set
*set
)
4077 return (struct isl_map
*)set
;
4080 __isl_give isl_map
*isl_map_from_domain(__isl_take isl_set
*set
)
4082 return isl_map_reverse(isl_map_from_range(set
));
4085 __isl_give isl_basic_map
*isl_basic_map_from_domain_and_range(
4086 __isl_take isl_basic_set
*domain
, __isl_take isl_basic_set
*range
)
4088 return isl_basic_map_apply_range(isl_basic_map_from_domain(domain
),
4089 isl_basic_map_from_range(range
));
4092 __isl_give isl_map
*isl_map_from_domain_and_range(__isl_take isl_set
*domain
,
4093 __isl_take isl_set
*range
)
4095 return isl_map_apply_range(isl_map_from_domain(domain
),
4096 isl_map_from_range(range
));
4099 struct isl_set
*isl_set_from_map(struct isl_map
*map
)
4102 struct isl_set
*set
= NULL
;
4106 map
= isl_map_cow(map
);
4109 map
->dim
= isl_dim_as_set_dim(map
->dim
);
4112 set
= (struct isl_set
*)map
;
4113 for (i
= 0; i
< map
->n
; ++i
) {
4114 set
->p
[i
] = isl_basic_set_from_basic_map(map
->p
[i
]);
4124 struct isl_map
*isl_map_alloc_dim(struct isl_dim
*dim
, int n
, unsigned flags
)
4126 struct isl_map
*map
;
4130 isl_assert(dim
->ctx
, n
>= 0, return NULL
);
4131 map
= isl_alloc(dim
->ctx
, struct isl_map
,
4132 sizeof(struct isl_map
) +
4133 (n
- 1) * sizeof(struct isl_basic_map
*));
4137 map
->ctx
= dim
->ctx
;
4138 isl_ctx_ref(map
->ctx
);
4150 struct isl_map
*isl_map_alloc(struct isl_ctx
*ctx
,
4151 unsigned nparam
, unsigned in
, unsigned out
, int n
,
4154 struct isl_map
*map
;
4155 struct isl_dim
*dims
;
4157 dims
= isl_dim_alloc(ctx
, nparam
, in
, out
);
4161 map
= isl_map_alloc_dim(dims
, n
, flags
);
4165 struct isl_basic_map
*isl_basic_map_empty(struct isl_dim
*dim
)
4167 struct isl_basic_map
*bmap
;
4168 bmap
= isl_basic_map_alloc_dim(dim
, 0, 1, 0);
4169 bmap
= isl_basic_map_set_to_empty(bmap
);
4173 struct isl_basic_set
*isl_basic_set_empty(struct isl_dim
*dim
)
4175 struct isl_basic_set
*bset
;
4176 bset
= isl_basic_set_alloc_dim(dim
, 0, 1, 0);
4177 bset
= isl_basic_set_set_to_empty(bset
);
4181 struct isl_basic_map
*isl_basic_map_empty_like(struct isl_basic_map
*model
)
4183 struct isl_basic_map
*bmap
;
4186 bmap
= isl_basic_map_alloc_dim(isl_dim_copy(model
->dim
), 0, 1, 0);
4187 bmap
= isl_basic_map_set_to_empty(bmap
);
4191 struct isl_basic_map
*isl_basic_map_empty_like_map(struct isl_map
*model
)
4193 struct isl_basic_map
*bmap
;
4196 bmap
= isl_basic_map_alloc_dim(isl_dim_copy(model
->dim
), 0, 1, 0);
4197 bmap
= isl_basic_map_set_to_empty(bmap
);
4201 struct isl_basic_set
*isl_basic_set_empty_like(struct isl_basic_set
*model
)
4203 struct isl_basic_set
*bset
;
4206 bset
= isl_basic_set_alloc_dim(isl_dim_copy(model
->dim
), 0, 1, 0);
4207 bset
= isl_basic_set_set_to_empty(bset
);
4211 struct isl_basic_map
*isl_basic_map_universe(struct isl_dim
*dim
)
4213 struct isl_basic_map
*bmap
;
4214 bmap
= isl_basic_map_alloc_dim(dim
, 0, 0, 0);
4215 bmap
= isl_basic_map_finalize(bmap
);
4219 struct isl_basic_set
*isl_basic_set_universe(struct isl_dim
*dim
)
4221 struct isl_basic_set
*bset
;
4222 bset
= isl_basic_set_alloc_dim(dim
, 0, 0, 0);
4223 bset
= isl_basic_set_finalize(bset
);
4227 __isl_give isl_basic_map
*isl_basic_map_nat_universe(__isl_take isl_dim
*dim
)
4230 unsigned total
= isl_dim_total(dim
);
4231 isl_basic_map
*bmap
;
4233 bmap
= isl_basic_map_alloc_dim(dim
, 0, 0, total
);
4234 for (i
= 0; i
< total
; ++i
) {
4235 int k
= isl_basic_map_alloc_inequality(bmap
);
4238 isl_seq_clr(bmap
->ineq
[k
], 1 + total
);
4239 isl_int_set_si(bmap
->ineq
[k
][1 + i
], 1);
4243 isl_basic_map_free(bmap
);
4247 __isl_give isl_basic_set
*isl_basic_set_nat_universe(__isl_take isl_dim
*dim
)
4249 return isl_basic_map_nat_universe(dim
);
4252 __isl_give isl_map
*isl_map_nat_universe(__isl_take isl_dim
*dim
)
4254 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim
));
4257 __isl_give isl_set
*isl_set_nat_universe(__isl_take isl_dim
*dim
)
4259 return isl_map_nat_universe(dim
);
4262 __isl_give isl_basic_map
*isl_basic_map_universe_like(
4263 __isl_keep isl_basic_map
*model
)
4267 return isl_basic_map_alloc_dim(isl_dim_copy(model
->dim
), 0, 0, 0);
4270 struct isl_basic_set
*isl_basic_set_universe_like(struct isl_basic_set
*model
)
4274 return isl_basic_set_alloc_dim(isl_dim_copy(model
->dim
), 0, 0, 0);
4277 __isl_give isl_basic_set
*isl_basic_set_universe_like_set(
4278 __isl_keep isl_set
*model
)
4282 return isl_basic_set_alloc_dim(isl_dim_copy(model
->dim
), 0, 0, 0);
4285 struct isl_map
*isl_map_empty(struct isl_dim
*dim
)
4287 return isl_map_alloc_dim(dim
, 0, ISL_MAP_DISJOINT
);
4290 struct isl_map
*isl_map_empty_like(struct isl_map
*model
)
4294 return isl_map_alloc_dim(isl_dim_copy(model
->dim
), 0, ISL_MAP_DISJOINT
);
4297 struct isl_map
*isl_map_empty_like_basic_map(struct isl_basic_map
*model
)
4301 return isl_map_alloc_dim(isl_dim_copy(model
->dim
), 0, ISL_MAP_DISJOINT
);
4304 struct isl_set
*isl_set_empty(struct isl_dim
*dim
)
4306 return isl_set_alloc_dim(dim
, 0, ISL_MAP_DISJOINT
);
4309 struct isl_set
*isl_set_empty_like(struct isl_set
*model
)
4313 return isl_set_empty(isl_dim_copy(model
->dim
));
4316 struct isl_map
*isl_map_universe(struct isl_dim
*dim
)
4318 struct isl_map
*map
;
4321 map
= isl_map_alloc_dim(isl_dim_copy(dim
), 1, ISL_MAP_DISJOINT
);
4322 map
= isl_map_add_basic_map(map
, isl_basic_map_universe(dim
));
4326 struct isl_set
*isl_set_universe(struct isl_dim
*dim
)
4328 struct isl_set
*set
;
4331 set
= isl_set_alloc_dim(isl_dim_copy(dim
), 1, ISL_MAP_DISJOINT
);
4332 set
= isl_set_add_basic_set(set
, isl_basic_set_universe(dim
));
4336 __isl_give isl_set
*isl_set_universe_like(__isl_keep isl_set
*model
)
4340 return isl_set_universe(isl_dim_copy(model
->dim
));
4343 struct isl_map
*isl_map_dup(struct isl_map
*map
)
4346 struct isl_map
*dup
;
4350 dup
= isl_map_alloc_dim(isl_dim_copy(map
->dim
), map
->n
, map
->flags
);
4351 for (i
= 0; i
< map
->n
; ++i
)
4352 dup
= isl_map_add_basic_map(dup
, isl_basic_map_copy(map
->p
[i
]));
4356 __isl_give isl_map
*isl_map_add_basic_map(__isl_take isl_map
*map
,
4357 __isl_take isl_basic_map
*bmap
)
4361 if (isl_basic_map_plain_is_empty(bmap
)) {
4362 isl_basic_map_free(bmap
);
4365 isl_assert(map
->ctx
, isl_dim_equal(map
->dim
, bmap
->dim
), goto error
);
4366 isl_assert(map
->ctx
, map
->n
< map
->size
, goto error
);
4367 map
->p
[map
->n
] = bmap
;
4369 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4375 isl_basic_map_free(bmap
);
4379 void isl_map_free(struct isl_map
*map
)
4389 isl_ctx_deref(map
->ctx
);
4390 for (i
= 0; i
< map
->n
; ++i
)
4391 isl_basic_map_free(map
->p
[i
]);
4392 isl_dim_free(map
->dim
);
4396 struct isl_map
*isl_map_extend(struct isl_map
*base
,
4397 unsigned nparam
, unsigned n_in
, unsigned n_out
)
4401 base
= isl_map_cow(base
);
4405 base
->dim
= isl_dim_extend(base
->dim
, nparam
, n_in
, n_out
);
4408 for (i
= 0; i
< base
->n
; ++i
) {
4409 base
->p
[i
] = isl_basic_map_extend_dim(base
->p
[i
],
4410 isl_dim_copy(base
->dim
), 0, 0, 0);
4420 struct isl_set
*isl_set_extend(struct isl_set
*base
,
4421 unsigned nparam
, unsigned dim
)
4423 return (struct isl_set
*)isl_map_extend((struct isl_map
*)base
,
4427 static struct isl_basic_map
*isl_basic_map_fix_pos_si(
4428 struct isl_basic_map
*bmap
, unsigned pos
, int value
)
4432 bmap
= isl_basic_map_cow(bmap
);
4433 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
4434 j
= isl_basic_map_alloc_equality(bmap
);
4437 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
4438 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
4439 isl_int_set_si(bmap
->eq
[j
][0], value
);
4440 bmap
= isl_basic_map_simplify(bmap
);
4441 return isl_basic_map_finalize(bmap
);
4443 isl_basic_map_free(bmap
);
4447 static __isl_give isl_basic_map
*isl_basic_map_fix_pos(
4448 __isl_take isl_basic_map
*bmap
, unsigned pos
, isl_int value
)
4452 bmap
= isl_basic_map_cow(bmap
);
4453 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
4454 j
= isl_basic_map_alloc_equality(bmap
);
4457 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
4458 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
4459 isl_int_set(bmap
->eq
[j
][0], value
);
4460 bmap
= isl_basic_map_simplify(bmap
);
4461 return isl_basic_map_finalize(bmap
);
4463 isl_basic_map_free(bmap
);
4467 struct isl_basic_map
*isl_basic_map_fix_si(struct isl_basic_map
*bmap
,
4468 enum isl_dim_type type
, unsigned pos
, int value
)
4472 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
4473 return isl_basic_map_fix_pos_si(bmap
,
4474 isl_basic_map_offset(bmap
, type
) + pos
, value
);
4476 isl_basic_map_free(bmap
);
4480 __isl_give isl_basic_map
*isl_basic_map_fix(__isl_take isl_basic_map
*bmap
,
4481 enum isl_dim_type type
, unsigned pos
, isl_int value
)
4485 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
4486 return isl_basic_map_fix_pos(bmap
,
4487 isl_basic_map_offset(bmap
, type
) + pos
, value
);
4489 isl_basic_map_free(bmap
);
4493 struct isl_basic_set
*isl_basic_set_fix_si(struct isl_basic_set
*bset
,
4494 enum isl_dim_type type
, unsigned pos
, int value
)
4496 return (struct isl_basic_set
*)
4497 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
4501 __isl_give isl_basic_set
*isl_basic_set_fix(__isl_take isl_basic_set
*bset
,
4502 enum isl_dim_type type
, unsigned pos
, isl_int value
)
4504 return (struct isl_basic_set
*)
4505 isl_basic_map_fix((struct isl_basic_map
*)bset
,
4509 struct isl_basic_map
*isl_basic_map_fix_input_si(struct isl_basic_map
*bmap
,
4510 unsigned input
, int value
)
4512 return isl_basic_map_fix_si(bmap
, isl_dim_in
, input
, value
);
4515 struct isl_basic_set
*isl_basic_set_fix_dim_si(struct isl_basic_set
*bset
,
4516 unsigned dim
, int value
)
4518 return (struct isl_basic_set
*)
4519 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
4520 isl_dim_set
, dim
, value
);
4523 struct isl_map
*isl_map_fix_si(struct isl_map
*map
,
4524 enum isl_dim_type type
, unsigned pos
, int value
)
4528 map
= isl_map_cow(map
);
4532 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
4533 for (i
= 0; i
< map
->n
; ++i
) {
4534 map
->p
[i
] = isl_basic_map_fix_si(map
->p
[i
], type
, pos
, value
);
4538 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4545 __isl_give isl_set
*isl_set_fix_si(__isl_take isl_set
*set
,
4546 enum isl_dim_type type
, unsigned pos
, int value
)
4548 return (struct isl_set
*)
4549 isl_map_fix_si((struct isl_map
*)set
, type
, pos
, value
);
4552 __isl_give isl_map
*isl_map_fix(__isl_take isl_map
*map
,
4553 enum isl_dim_type type
, unsigned pos
, isl_int value
)
4557 map
= isl_map_cow(map
);
4561 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
4562 for (i
= 0; i
< map
->n
; ++i
) {
4563 map
->p
[i
] = isl_basic_map_fix(map
->p
[i
], type
, pos
, value
);
4567 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4574 __isl_give isl_set
*isl_set_fix(__isl_take isl_set
*set
,
4575 enum isl_dim_type type
, unsigned pos
, isl_int value
)
4577 return (struct isl_set
*)isl_map_fix((isl_map
*)set
, type
, pos
, value
);
4580 struct isl_map
*isl_map_fix_input_si(struct isl_map
*map
,
4581 unsigned input
, int value
)
4583 return isl_map_fix_si(map
, isl_dim_in
, input
, value
);
4586 struct isl_set
*isl_set_fix_dim_si(struct isl_set
*set
, unsigned dim
, int value
)
4588 return (struct isl_set
*)
4589 isl_map_fix_si((struct isl_map
*)set
, isl_dim_set
, dim
, value
);
4592 __isl_give isl_basic_map
*isl_basic_map_lower_bound_si(
4593 __isl_take isl_basic_map
*bmap
,
4594 enum isl_dim_type type
, unsigned pos
, int value
)
4600 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
4601 pos
+= isl_basic_map_offset(bmap
, type
);
4602 bmap
= isl_basic_map_cow(bmap
);
4603 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
4604 j
= isl_basic_map_alloc_inequality(bmap
);
4607 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
4608 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
4609 isl_int_set_si(bmap
->ineq
[j
][0], -value
);
4610 bmap
= isl_basic_map_simplify(bmap
);
4611 return isl_basic_map_finalize(bmap
);
4613 isl_basic_map_free(bmap
);
4617 struct isl_basic_set
*isl_basic_set_lower_bound_dim(struct isl_basic_set
*bset
,
4618 unsigned dim
, isl_int value
)
4622 bset
= isl_basic_set_cow(bset
);
4623 bset
= isl_basic_set_extend_constraints(bset
, 0, 1);
4624 j
= isl_basic_set_alloc_inequality(bset
);
4627 isl_seq_clr(bset
->ineq
[j
], 1 + isl_basic_set_total_dim(bset
));
4628 isl_int_set_si(bset
->ineq
[j
][1 + isl_basic_set_n_param(bset
) + dim
], 1);
4629 isl_int_neg(bset
->ineq
[j
][0], value
);
4630 bset
= isl_basic_set_simplify(bset
);
4631 return isl_basic_set_finalize(bset
);
4633 isl_basic_set_free(bset
);
4637 __isl_give isl_map
*isl_map_lower_bound_si(__isl_take isl_map
*map
,
4638 enum isl_dim_type type
, unsigned pos
, int value
)
4642 map
= isl_map_cow(map
);
4646 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
4647 for (i
= 0; i
< map
->n
; ++i
) {
4648 map
->p
[i
] = isl_basic_map_lower_bound_si(map
->p
[i
],
4653 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4660 __isl_give isl_set
*isl_set_lower_bound_si(__isl_take isl_set
*set
,
4661 enum isl_dim_type type
, unsigned pos
, int value
)
4663 return (struct isl_set
*)
4664 isl_map_lower_bound_si((struct isl_map
*)set
, type
, pos
, value
);
4667 struct isl_set
*isl_set_lower_bound_dim(struct isl_set
*set
, unsigned dim
,
4672 set
= isl_set_cow(set
);
4676 isl_assert(set
->ctx
, dim
< isl_set_n_dim(set
), goto error
);
4677 for (i
= 0; i
< set
->n
; ++i
) {
4678 set
->p
[i
] = isl_basic_set_lower_bound_dim(set
->p
[i
], dim
, value
);
4688 struct isl_map
*isl_map_reverse(struct isl_map
*map
)
4692 map
= isl_map_cow(map
);
4696 map
->dim
= isl_dim_reverse(map
->dim
);
4699 for (i
= 0; i
< map
->n
; ++i
) {
4700 map
->p
[i
] = isl_basic_map_reverse(map
->p
[i
]);
4704 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4711 static struct isl_map
*isl_basic_map_partial_lexopt(
4712 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
4713 struct isl_set
**empty
, int max
)
4717 if (bmap
->ctx
->opt
->pip
== ISL_PIP_PIP
)
4718 return isl_pip_basic_map_lexopt(bmap
, dom
, empty
, max
);
4720 return isl_tab_basic_map_partial_lexopt(bmap
, dom
, empty
, max
);
4722 isl_basic_map_free(bmap
);
4723 isl_basic_set_free(dom
);
4729 struct isl_map
*isl_basic_map_partial_lexmax(
4730 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
4731 struct isl_set
**empty
)
4733 return isl_basic_map_partial_lexopt(bmap
, dom
, empty
, 1);
4736 struct isl_map
*isl_basic_map_partial_lexmin(
4737 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
4738 struct isl_set
**empty
)
4740 return isl_basic_map_partial_lexopt(bmap
, dom
, empty
, 0);
4743 struct isl_set
*isl_basic_set_partial_lexmin(
4744 struct isl_basic_set
*bset
, struct isl_basic_set
*dom
,
4745 struct isl_set
**empty
)
4747 return (struct isl_set
*)
4748 isl_basic_map_partial_lexmin((struct isl_basic_map
*)bset
,
4752 struct isl_set
*isl_basic_set_partial_lexmax(
4753 struct isl_basic_set
*bset
, struct isl_basic_set
*dom
,
4754 struct isl_set
**empty
)
4756 return (struct isl_set
*)
4757 isl_basic_map_partial_lexmax((struct isl_basic_map
*)bset
,
4761 /* Given a basic map "bmap", compute the lexicographically minimal
4762 * (or maximal) image element for each domain element in dom.
4763 * Set *empty to those elements in dom that do not have an image element.
4765 * We first make sure the basic sets in dom are disjoint and then
4766 * simply collect the results over each of the basic sets separately.
4767 * We could probably improve the efficiency a bit by moving the union
4768 * domain down into the parametric integer programming.
4770 static __isl_give isl_map
*basic_map_partial_lexopt(
4771 __isl_take isl_basic_map
*bmap
, __isl_take isl_set
*dom
,
4772 __isl_give isl_set
**empty
, int max
)
4775 struct isl_map
*res
;
4777 dom
= isl_set_make_disjoint(dom
);
4781 if (isl_set_plain_is_empty(dom
)) {
4782 res
= isl_map_empty_like_basic_map(bmap
);
4783 *empty
= isl_set_empty_like(dom
);
4785 isl_basic_map_free(bmap
);
4789 res
= isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap
),
4790 isl_basic_set_copy(dom
->p
[0]), empty
, max
);
4792 for (i
= 1; i
< dom
->n
; ++i
) {
4793 struct isl_map
*res_i
;
4794 struct isl_set
*empty_i
;
4796 res_i
= isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap
),
4797 isl_basic_set_copy(dom
->p
[i
]), &empty_i
, max
);
4799 res
= isl_map_union_disjoint(res
, res_i
);
4800 *empty
= isl_set_union_disjoint(*empty
, empty_i
);
4804 isl_basic_map_free(bmap
);
4809 isl_basic_map_free(bmap
);
4813 /* Given a map "map", compute the lexicographically minimal
4814 * (or maximal) image element for each domain element in dom.
4815 * Set *empty to those elements in dom that do not have an image element.
4817 * We first compute the lexicographically minimal or maximal element
4818 * in the first basic map. This results in a partial solution "res"
4819 * and a subset "todo" of dom that still need to be handled.
4820 * We then consider each of the remaining maps in "map" and successively
4821 * improve both "res" and "todo".
4823 * Let res^k and todo^k be the results after k steps and let i = k + 1.
4824 * Assume we are computing the lexicographical maximum.
4825 * We first compute the lexicographically maximal element in basic map i.
4826 * This results in a partial solution res_i and a subset todo_i.
4827 * Then we combine these results with those obtain for the first k basic maps
4828 * to obtain a result that is valid for the first k+1 basic maps.
4829 * In particular, the set where there is no solution is the set where
4830 * there is no solution for the first k basic maps and also no solution
4831 * for the ith basic map, i.e.,
4833 * todo^i = todo^k * todo_i
4835 * On dom(res^k) * dom(res_i), we need to pick the larger of the two
4836 * solutions, arbitrarily breaking ties in favor of res^k.
4837 * That is, when res^k(a) >= res_i(a), we pick res^k and
4838 * when res^k(a) < res_i(a), we pick res_i. (Here, ">=" and "<" denote
4839 * the lexicographic order.)
4840 * In practice, we compute
4842 * res^k * (res_i . "<=")
4846 * res_i * (res^k . "<")
4848 * Finally, we consider the symmetric difference of dom(res^k) and dom(res_i),
4849 * where only one of res^k and res_i provides a solution and we simply pick
4856 * Note that we only compute these intersections when dom(res^k) intersects
4857 * dom(res_i). Otherwise, the only effect of these intersections is to
4858 * potentially break up res^k and res_i into smaller pieces.
4859 * We want to avoid such splintering as much as possible.
4860 * In fact, an earlier implementation of this function would look for
4861 * better results in the domain of res^k and for extra results in todo^k,
4862 * but this would always result in a splintering according to todo^k,
4863 * even when the domain of basic map i is disjoint from the domains of
4864 * the previous basic maps.
4866 static __isl_give isl_map
*isl_map_partial_lexopt(
4867 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
4868 __isl_give isl_set
**empty
, int max
)
4871 struct isl_map
*res
;
4872 struct isl_set
*todo
;
4877 if (isl_map_plain_is_empty(map
)) {
4885 res
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[0]),
4886 isl_set_copy(dom
), &todo
, max
);
4888 for (i
= 1; i
< map
->n
; ++i
) {
4892 isl_dim
*dim
= isl_dim_range(isl_map_get_dim(res
));
4894 res_i
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[i
]),
4895 isl_set_copy(dom
), &todo_i
, max
);
4898 lt
= isl_map_lex_lt(isl_dim_copy(dim
));
4899 le
= isl_map_lex_le(dim
);
4901 lt
= isl_map_lex_gt(isl_dim_copy(dim
));
4902 le
= isl_map_lex_ge(dim
);
4904 lt
= isl_map_apply_range(isl_map_copy(res
), lt
);
4905 lt
= isl_map_intersect(lt
, isl_map_copy(res_i
));
4906 le
= isl_map_apply_range(isl_map_copy(res_i
), le
);
4907 le
= isl_map_intersect(le
, isl_map_copy(res
));
4909 if (!isl_map_is_empty(lt
) || !isl_map_is_empty(le
)) {
4910 res
= isl_map_intersect_domain(res
,
4911 isl_set_copy(todo_i
));
4912 res_i
= isl_map_intersect_domain(res_i
,
4913 isl_set_copy(todo
));
4916 res
= isl_map_union_disjoint(res
, res_i
);
4917 res
= isl_map_union_disjoint(res
, lt
);
4918 res
= isl_map_union_disjoint(res
, le
);
4920 todo
= isl_set_intersect(todo
, todo_i
);
4940 __isl_give isl_map
*isl_map_partial_lexmax(
4941 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
4942 __isl_give isl_set
**empty
)
4944 return isl_map_partial_lexopt(map
, dom
, empty
, 1);
4947 __isl_give isl_map
*isl_map_partial_lexmin(
4948 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
4949 __isl_give isl_set
**empty
)
4951 return isl_map_partial_lexopt(map
, dom
, empty
, 0);
4954 __isl_give isl_set
*isl_set_partial_lexmin(
4955 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
4956 __isl_give isl_set
**empty
)
4958 return (struct isl_set
*)
4959 isl_map_partial_lexmin((struct isl_map
*)set
,
4963 __isl_give isl_set
*isl_set_partial_lexmax(
4964 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
4965 __isl_give isl_set
**empty
)
4967 return (struct isl_set
*)
4968 isl_map_partial_lexmax((struct isl_map
*)set
,
4972 __isl_give isl_map
*isl_basic_map_lexopt(__isl_take isl_basic_map
*bmap
, int max
)
4974 struct isl_basic_set
*dom
= NULL
;
4975 struct isl_dim
*dom_dim
;
4979 dom_dim
= isl_dim_domain(isl_dim_copy(bmap
->dim
));
4980 dom
= isl_basic_set_universe(dom_dim
);
4981 return isl_basic_map_partial_lexopt(bmap
, dom
, NULL
, max
);
4983 isl_basic_map_free(bmap
);
4987 __isl_give isl_map
*isl_basic_map_lexmin(__isl_take isl_basic_map
*bmap
)
4989 return isl_basic_map_lexopt(bmap
, 0);
4992 __isl_give isl_map
*isl_basic_map_lexmax(__isl_take isl_basic_map
*bmap
)
4994 return isl_basic_map_lexopt(bmap
, 1);
4997 __isl_give isl_set
*isl_basic_set_lexmin(__isl_take isl_basic_set
*bset
)
4999 return (isl_set
*)isl_basic_map_lexmin((isl_basic_map
*)bset
);
5002 __isl_give isl_set
*isl_basic_set_lexmax(__isl_take isl_basic_set
*bset
)
5004 return (isl_set
*)isl_basic_map_lexmax((isl_basic_map
*)bset
);
5007 __isl_give isl_map
*isl_map_lexopt(__isl_take isl_map
*map
, int max
)
5009 struct isl_set
*dom
= NULL
;
5010 struct isl_dim
*dom_dim
;
5014 dom_dim
= isl_dim_domain(isl_dim_copy(map
->dim
));
5015 dom
= isl_set_universe(dom_dim
);
5016 return isl_map_partial_lexopt(map
, dom
, NULL
, max
);
5022 __isl_give isl_map
*isl_map_lexmin(__isl_take isl_map
*map
)
5024 return isl_map_lexopt(map
, 0);
5027 __isl_give isl_map
*isl_map_lexmax(__isl_take isl_map
*map
)
5029 return isl_map_lexopt(map
, 1);
5032 __isl_give isl_set
*isl_set_lexmin(__isl_take isl_set
*set
)
5034 return (isl_set
*)isl_map_lexmin((isl_map
*)set
);
5037 __isl_give isl_set
*isl_set_lexmax(__isl_take isl_set
*set
)
5039 return (isl_set
*)isl_map_lexmax((isl_map
*)set
);
5042 /* Construct a map that equates the two given dimensions in the given space.
5044 static __isl_give isl_map
*equate(__isl_take isl_dim
*dim
,
5045 enum isl_dim_type src_type
, int src_pos
,
5046 enum isl_dim_type dst_type
, int dst_pos
)
5048 isl_basic_map
*bmap
;
5051 bmap
= isl_basic_map_alloc_dim(dim
, 0, 1, 0);
5052 k
= isl_basic_map_alloc_equality(bmap
);
5055 isl_seq_clr(bmap
->eq
[k
], 1 + isl_basic_map_total_dim(bmap
));
5056 src_pos
+= isl_basic_map_offset(bmap
, src_type
);
5057 dst_pos
+= isl_basic_map_offset(bmap
, dst_type
);
5058 isl_int_set_si(bmap
->eq
[k
][src_pos
], 1);
5059 isl_int_set_si(bmap
->eq
[k
][dst_pos
], -1);
5061 return isl_map_from_basic_map(bmap
);
5063 isl_basic_map_free(bmap
);
5067 /* Extract the first and only affine expression from list
5068 * and then add it to *pwaff with the given dom.
5069 * This domain is known to be disjoint from other domains
5070 * because of the way isl_basic_set_foreach_lexmax works.
5072 static int update_dim_max(__isl_take isl_basic_set
*dom
,
5073 __isl_take isl_aff_list
*list
, void *user
)
5075 isl_ctx
*ctx
= isl_basic_set_get_ctx(dom
);
5077 isl_pw_aff
**pwaff
= user
;
5078 isl_pw_aff
*pwaff_i
;
5080 if (isl_aff_list_n_aff(list
) != 1)
5081 isl_die(ctx
, isl_error_internal
,
5082 "expecting single element list", goto error
);
5084 aff
= isl_aff_list_get_aff(list
, 0);
5085 pwaff_i
= isl_pw_aff_alloc(isl_set_from_basic_set(dom
), aff
);
5087 *pwaff
= isl_pw_aff_add_disjoint(*pwaff
, pwaff_i
);
5089 isl_aff_list_free(list
);
5093 isl_basic_set_free(dom
);
5094 isl_aff_list_free(list
);
5098 /* Given a one-dimensional basic set, compute the maximum of that
5099 * dimension as an isl_pw_aff.
5101 * The isl_pw_aff is constructed by having isl_basic_set_foreach_lexmax
5102 * call update_dim_max on each leaf of the result.
5104 static __isl_give isl_pw_aff
*basic_set_dim_max(__isl_keep isl_basic_set
*bset
)
5106 isl_dim
*dim
= isl_basic_set_get_dim(bset
);
5110 dim
= isl_dim_domain(isl_dim_from_range(dim
));
5111 pwaff
= isl_pw_aff_empty(dim
);
5113 r
= isl_basic_set_foreach_lexmax(bset
, &update_dim_max
, &pwaff
);
5115 return isl_pw_aff_free(pwaff
);
5120 /* Compute the maximum of the given set dimension as a function of the
5121 * parameters, but independently of the other set dimensions.
5123 * We first project the set onto the given dimension and then compute
5124 * the "lexicographic" maximum in each basic set, combining the results
5125 * using isl_pw_aff_max.
5127 __isl_give isl_pw_aff
*isl_set_dim_max(__isl_take isl_set
*set
, int pos
)
5133 map
= isl_map_from_domain(set
);
5134 map
= isl_map_add_dims(map
, isl_dim_out
, 1);
5135 map
= isl_map_intersect(map
,
5136 equate(isl_map_get_dim(map
), isl_dim_in
, pos
,
5138 set
= isl_map_range(map
);
5143 isl_dim
*dim
= isl_set_get_dim(set
);
5144 dim
= isl_dim_domain(isl_dim_from_range(dim
));
5146 return isl_pw_aff_empty(dim
);
5149 pwaff
= basic_set_dim_max(set
->p
[0]);
5150 for (i
= 1; i
< set
->n
; ++i
) {
5151 isl_pw_aff
*pwaff_i
;
5153 pwaff_i
= basic_set_dim_max(set
->p
[i
]);
5154 pwaff
= isl_pw_aff_max(pwaff
, pwaff_i
);
5162 /* Apply a preimage specified by "mat" on the parameters of "bset".
5163 * bset is assumed to have only parameters and divs.
5165 static struct isl_basic_set
*basic_set_parameter_preimage(
5166 struct isl_basic_set
*bset
, struct isl_mat
*mat
)
5173 bset
->dim
= isl_dim_cow(bset
->dim
);
5177 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
5179 isl_assert(bset
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
5181 bset
->dim
->nparam
= 0;
5182 bset
->dim
->n_out
= nparam
;
5183 bset
= isl_basic_set_preimage(bset
, mat
);
5185 bset
->dim
->nparam
= bset
->dim
->n_out
;
5186 bset
->dim
->n_out
= 0;
5191 isl_basic_set_free(bset
);
5195 /* Apply a preimage specified by "mat" on the parameters of "set".
5196 * set is assumed to have only parameters and divs.
5198 static struct isl_set
*set_parameter_preimage(
5199 struct isl_set
*set
, struct isl_mat
*mat
)
5201 struct isl_dim
*dim
= NULL
;
5207 dim
= isl_dim_copy(set
->dim
);
5208 dim
= isl_dim_cow(dim
);
5212 nparam
= isl_set_dim(set
, isl_dim_param
);
5214 isl_assert(set
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
5217 dim
->n_out
= nparam
;
5218 isl_set_reset_dim(set
, dim
);
5219 set
= isl_set_preimage(set
, mat
);
5222 dim
= isl_dim_copy(set
->dim
);
5223 dim
= isl_dim_cow(dim
);
5226 dim
->nparam
= dim
->n_out
;
5228 isl_set_reset_dim(set
, dim
);
5238 /* Intersect the basic set "bset" with the affine space specified by the
5239 * equalities in "eq".
5241 static struct isl_basic_set
*basic_set_append_equalities(
5242 struct isl_basic_set
*bset
, struct isl_mat
*eq
)
5250 bset
= isl_basic_set_extend_dim(bset
, isl_dim_copy(bset
->dim
), 0,
5255 len
= 1 + isl_dim_total(bset
->dim
) + bset
->extra
;
5256 for (i
= 0; i
< eq
->n_row
; ++i
) {
5257 k
= isl_basic_set_alloc_equality(bset
);
5260 isl_seq_cpy(bset
->eq
[k
], eq
->row
[i
], eq
->n_col
);
5261 isl_seq_clr(bset
->eq
[k
] + eq
->n_col
, len
- eq
->n_col
);
5265 bset
= isl_basic_set_gauss(bset
, NULL
);
5266 bset
= isl_basic_set_finalize(bset
);
5271 isl_basic_set_free(bset
);
5275 /* Intersect the set "set" with the affine space specified by the
5276 * equalities in "eq".
5278 static struct isl_set
*set_append_equalities(struct isl_set
*set
,
5286 for (i
= 0; i
< set
->n
; ++i
) {
5287 set
->p
[i
] = basic_set_append_equalities(set
->p
[i
],
5300 /* Project the given basic set onto its parameter domain, possibly introducing
5301 * new, explicit, existential variables in the constraints.
5302 * The input has parameters and (possibly implicit) existential variables.
5303 * The output has the same parameters, but only
5304 * explicit existentially quantified variables.
5306 * The actual projection is performed by pip, but pip doesn't seem
5307 * to like equalities very much, so we first remove the equalities
5308 * among the parameters by performing a variable compression on
5309 * the parameters. Afterward, an inverse transformation is performed
5310 * and the equalities among the parameters are inserted back in.
5312 static struct isl_set
*parameter_compute_divs(struct isl_basic_set
*bset
)
5316 struct isl_mat
*T
, *T2
;
5317 struct isl_set
*set
;
5318 unsigned nparam
, n_div
;
5320 bset
= isl_basic_set_cow(bset
);
5324 if (bset
->n_eq
== 0)
5325 return isl_basic_set_lexmin(bset
);
5327 isl_basic_set_gauss(bset
, NULL
);
5329 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
5330 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
5332 for (i
= 0, j
= n_div
- 1; i
< bset
->n_eq
&& j
>= 0; --j
) {
5333 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ j
]))
5336 if (i
== bset
->n_eq
)
5337 return isl_basic_set_lexmin(bset
);
5339 eq
= isl_mat_sub_alloc6(bset
->ctx
, bset
->eq
, i
, bset
->n_eq
- i
,
5341 eq
= isl_mat_cow(eq
);
5342 T
= isl_mat_variable_compression(isl_mat_copy(eq
), &T2
);
5343 if (T
&& T
->n_col
== 0) {
5347 bset
= isl_basic_set_set_to_empty(bset
);
5348 return isl_set_from_basic_set(bset
);
5350 bset
= basic_set_parameter_preimage(bset
, T
);
5352 set
= isl_basic_set_lexmin(bset
);
5353 set
= set_parameter_preimage(set
, T2
);
5354 set
= set_append_equalities(set
, eq
);
5358 /* Compute an explicit representation for all the existentially
5359 * quantified variables.
5360 * The input and output dimensions are first turned into parameters.
5361 * compute_divs then returns a map with the same parameters and
5362 * no input or output dimensions and the dimension specification
5363 * is reset to that of the input.
5365 static struct isl_map
*compute_divs(struct isl_basic_map
*bmap
)
5367 struct isl_basic_set
*bset
;
5368 struct isl_set
*set
;
5369 struct isl_map
*map
;
5370 struct isl_dim
*dim
, *orig_dim
= NULL
;
5375 bmap
= isl_basic_map_cow(bmap
);
5379 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5380 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5381 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5382 dim
= isl_dim_set_alloc(bmap
->ctx
, nparam
+ n_in
+ n_out
, 0);
5386 orig_dim
= bmap
->dim
;
5388 bset
= (struct isl_basic_set
*)bmap
;
5390 set
= parameter_compute_divs(bset
);
5391 map
= (struct isl_map
*)set
;
5392 map
= isl_map_reset_dim(map
, orig_dim
);
5396 isl_basic_map_free(bmap
);
5400 int isl_basic_map_divs_known(__isl_keep isl_basic_map
*bmap
)
5408 off
= isl_dim_total(bmap
->dim
);
5409 for (i
= 0; i
< bmap
->n_div
; ++i
) {
5410 if (isl_int_is_zero(bmap
->div
[i
][0]))
5412 isl_assert(bmap
->ctx
, isl_int_is_zero(bmap
->div
[i
][1+1+off
+i
]),
5418 static int map_divs_known(__isl_keep isl_map
*map
)
5425 for (i
= 0; i
< map
->n
; ++i
) {
5426 int known
= isl_basic_map_divs_known(map
->p
[i
]);
5434 /* If bmap contains any unknown divs, then compute explicit
5435 * expressions for them. However, this computation may be
5436 * quite expensive, so first try to remove divs that aren't
5439 struct isl_map
*isl_basic_map_compute_divs(struct isl_basic_map
*bmap
)
5442 struct isl_map
*map
;
5444 known
= isl_basic_map_divs_known(bmap
);
5448 return isl_map_from_basic_map(bmap
);
5450 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
5452 known
= isl_basic_map_divs_known(bmap
);
5456 return isl_map_from_basic_map(bmap
);
5458 map
= compute_divs(bmap
);
5461 isl_basic_map_free(bmap
);
5465 struct isl_map
*isl_map_compute_divs(struct isl_map
*map
)
5469 struct isl_map
*res
;
5476 known
= map_divs_known(map
);
5484 res
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[0]));
5485 for (i
= 1 ; i
< map
->n
; ++i
) {
5487 r2
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[i
]));
5488 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
5489 res
= isl_map_union_disjoint(res
, r2
);
5491 res
= isl_map_union(res
, r2
);
5498 struct isl_set
*isl_basic_set_compute_divs(struct isl_basic_set
*bset
)
5500 return (struct isl_set
*)
5501 isl_basic_map_compute_divs((struct isl_basic_map
*)bset
);
5504 struct isl_set
*isl_set_compute_divs(struct isl_set
*set
)
5506 return (struct isl_set
*)
5507 isl_map_compute_divs((struct isl_map
*)set
);
5510 struct isl_set
*isl_map_domain(struct isl_map
*map
)
5513 struct isl_set
*set
;
5518 map
= isl_map_cow(map
);
5522 set
= (struct isl_set
*)map
;
5523 set
->dim
= isl_dim_domain(set
->dim
);
5526 for (i
= 0; i
< map
->n
; ++i
) {
5527 set
->p
[i
] = isl_basic_map_domain(map
->p
[i
]);
5531 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
5532 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
5539 struct isl_map
*isl_map_union_disjoint(
5540 struct isl_map
*map1
, struct isl_map
*map2
)
5544 struct isl_map
*map
= NULL
;
5558 isl_assert(map1
->ctx
, isl_dim_equal(map1
->dim
, map2
->dim
), goto error
);
5560 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
5561 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
5562 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
5564 map
= isl_map_alloc_dim(isl_dim_copy(map1
->dim
),
5565 map1
->n
+ map2
->n
, flags
);
5568 for (i
= 0; i
< map1
->n
; ++i
) {
5569 map
= isl_map_add_basic_map(map
,
5570 isl_basic_map_copy(map1
->p
[i
]));
5574 for (i
= 0; i
< map2
->n
; ++i
) {
5575 map
= isl_map_add_basic_map(map
,
5576 isl_basic_map_copy(map2
->p
[i
]));
5590 struct isl_map
*isl_map_union(struct isl_map
*map1
, struct isl_map
*map2
)
5592 map1
= isl_map_union_disjoint(map1
, map2
);
5596 ISL_F_CLR(map1
, ISL_MAP_DISJOINT
);
5600 struct isl_set
*isl_set_union_disjoint(
5601 struct isl_set
*set1
, struct isl_set
*set2
)
5603 return (struct isl_set
*)
5604 isl_map_union_disjoint(
5605 (struct isl_map
*)set1
, (struct isl_map
*)set2
);
5608 struct isl_set
*isl_set_union(struct isl_set
*set1
, struct isl_set
*set2
)
5610 return (struct isl_set
*)
5611 isl_map_union((struct isl_map
*)set1
, (struct isl_map
*)set2
);
5614 struct isl_map
*isl_map_intersect_range(
5615 struct isl_map
*map
, struct isl_set
*set
)
5618 struct isl_map
*result
;
5624 if (!isl_dim_match(map
->dim
, isl_dim_param
, set
->dim
, isl_dim_param
))
5625 isl_die(set
->ctx
, isl_error_invalid
,
5626 "parameters don't match", goto error
);
5628 if (isl_dim_size(set
->dim
, isl_dim_set
) != 0 &&
5629 !isl_map_compatible_range(map
, set
))
5630 isl_die(set
->ctx
, isl_error_invalid
,
5631 "incompatible spaces", goto error
);
5633 if (isl_set_plain_is_universe(set
)) {
5638 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
) &&
5639 ISL_F_ISSET(set
, ISL_MAP_DISJOINT
))
5640 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
5642 result
= isl_map_alloc_dim(isl_dim_copy(map
->dim
),
5643 map
->n
* set
->n
, flags
);
5646 for (i
= 0; i
< map
->n
; ++i
)
5647 for (j
= 0; j
< set
->n
; ++j
) {
5648 result
= isl_map_add_basic_map(result
,
5649 isl_basic_map_intersect_range(
5650 isl_basic_map_copy(map
->p
[i
]),
5651 isl_basic_set_copy(set
->p
[j
])));
5664 struct isl_map
*isl_map_intersect_domain(
5665 struct isl_map
*map
, struct isl_set
*set
)
5667 return isl_map_reverse(
5668 isl_map_intersect_range(isl_map_reverse(map
), set
));
5671 struct isl_map
*isl_map_apply_domain(
5672 struct isl_map
*map1
, struct isl_map
*map2
)
5676 map1
= isl_map_reverse(map1
);
5677 map1
= isl_map_apply_range(map1
, map2
);
5678 return isl_map_reverse(map1
);
5685 struct isl_map
*isl_map_apply_range(
5686 struct isl_map
*map1
, struct isl_map
*map2
)
5688 struct isl_dim
*dim_result
;
5689 struct isl_map
*result
;
5695 dim_result
= isl_dim_join(isl_dim_copy(map1
->dim
),
5696 isl_dim_copy(map2
->dim
));
5698 result
= isl_map_alloc_dim(dim_result
, map1
->n
* map2
->n
, 0);
5701 for (i
= 0; i
< map1
->n
; ++i
)
5702 for (j
= 0; j
< map2
->n
; ++j
) {
5703 result
= isl_map_add_basic_map(result
,
5704 isl_basic_map_apply_range(
5705 isl_basic_map_copy(map1
->p
[i
]),
5706 isl_basic_map_copy(map2
->p
[j
])));
5712 if (result
&& result
->n
<= 1)
5713 ISL_F_SET(result
, ISL_MAP_DISJOINT
);
5722 * returns range - domain
5724 struct isl_basic_set
*isl_basic_map_deltas(struct isl_basic_map
*bmap
)
5726 isl_dim
*dims
, *target_dim
;
5727 struct isl_basic_set
*bset
;
5734 isl_assert(bmap
->ctx
, isl_dim_tuple_match(bmap
->dim
, isl_dim_in
,
5735 bmap
->dim
, isl_dim_out
),
5737 target_dim
= isl_dim_domain(isl_basic_map_get_dim(bmap
));
5738 dim
= isl_basic_map_n_in(bmap
);
5739 nparam
= isl_basic_map_n_param(bmap
);
5740 bset
= isl_basic_set_from_basic_map(bmap
);
5741 bset
= isl_basic_set_cow(bset
);
5742 dims
= isl_basic_set_get_dim(bset
);
5743 dims
= isl_dim_add(dims
, isl_dim_set
, dim
);
5744 bset
= isl_basic_set_extend_dim(bset
, dims
, 0, dim
, 0);
5745 bset
= isl_basic_set_swap_vars(bset
, 2*dim
);
5746 for (i
= 0; i
< dim
; ++i
) {
5747 int j
= isl_basic_map_alloc_equality(
5748 (struct isl_basic_map
*)bset
);
5751 isl_seq_clr(bset
->eq
[j
], 1 + isl_basic_set_total_dim(bset
));
5752 isl_int_set_si(bset
->eq
[j
][1+nparam
+i
], 1);
5753 isl_int_set_si(bset
->eq
[j
][1+nparam
+dim
+i
], 1);
5754 isl_int_set_si(bset
->eq
[j
][1+nparam
+2*dim
+i
], -1);
5756 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, dim
, 2*dim
);
5757 bset
= isl_basic_set_reset_dim(bset
, target_dim
);
5760 isl_basic_map_free(bmap
);
5765 * returns range - domain
5767 struct isl_set
*isl_map_deltas(struct isl_map
*map
)
5771 struct isl_set
*result
;
5776 isl_assert(map
->ctx
, isl_dim_tuple_match(map
->dim
, isl_dim_in
,
5777 map
->dim
, isl_dim_out
),
5779 dim
= isl_map_get_dim(map
);
5780 dim
= isl_dim_domain(dim
);
5781 result
= isl_set_alloc_dim(dim
, map
->n
, 0);
5784 for (i
= 0; i
< map
->n
; ++i
)
5785 result
= isl_set_add_basic_set(result
,
5786 isl_basic_map_deltas(isl_basic_map_copy(map
->p
[i
])));
5795 * returns [domain -> range] -> range - domain
5797 __isl_give isl_basic_map
*isl_basic_map_deltas_map(
5798 __isl_take isl_basic_map
*bmap
)
5802 isl_basic_map
*domain
;
5806 if (!isl_dim_tuple_match(bmap
->dim
, isl_dim_in
, bmap
->dim
, isl_dim_out
))
5807 isl_die(bmap
->ctx
, isl_error_invalid
,
5808 "domain and range don't match", goto error
);
5810 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5811 n
= isl_basic_map_dim(bmap
, isl_dim_in
);
5813 dim
= isl_dim_from_range(isl_dim_domain(isl_basic_map_get_dim(bmap
)));
5814 domain
= isl_basic_map_universe(dim
);
5816 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
5817 bmap
= isl_basic_map_apply_range(bmap
, domain
);
5818 bmap
= isl_basic_map_extend_constraints(bmap
, n
, 0);
5820 total
= isl_basic_map_total_dim(bmap
);
5822 for (i
= 0; i
< n
; ++i
) {
5823 k
= isl_basic_map_alloc_equality(bmap
);
5826 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
5827 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], 1);
5828 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ i
], -1);
5829 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ n
+ i
], 1);
5832 bmap
= isl_basic_map_gauss(bmap
, NULL
);
5833 return isl_basic_map_finalize(bmap
);
5835 isl_basic_map_free(bmap
);
5840 * returns [domain -> range] -> range - domain
5842 __isl_give isl_map
*isl_map_deltas_map(__isl_take isl_map
*map
)
5845 isl_dim
*domain_dim
;
5850 if (!isl_dim_tuple_match(map
->dim
, isl_dim_in
, map
->dim
, isl_dim_out
))
5851 isl_die(map
->ctx
, isl_error_invalid
,
5852 "domain and range don't match", goto error
);
5854 map
= isl_map_cow(map
);
5858 domain_dim
= isl_dim_from_range(isl_dim_domain(isl_map_get_dim(map
)));
5859 map
->dim
= isl_dim_from_domain(isl_dim_wrap(map
->dim
));
5860 map
->dim
= isl_dim_join(map
->dim
, domain_dim
);
5863 for (i
= 0; i
< map
->n
; ++i
) {
5864 map
->p
[i
] = isl_basic_map_deltas_map(map
->p
[i
]);
5868 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5875 static struct isl_basic_map
*basic_map_identity(struct isl_dim
*dims
)
5877 struct isl_basic_map
*bmap
;
5885 nparam
= dims
->nparam
;
5887 bmap
= isl_basic_map_alloc_dim(dims
, 0, dim
, 0);
5891 for (i
= 0; i
< dim
; ++i
) {
5892 int j
= isl_basic_map_alloc_equality(bmap
);
5895 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
5896 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
5897 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], -1);
5899 return isl_basic_map_finalize(bmap
);
5901 isl_basic_map_free(bmap
);
5905 __isl_give isl_basic_map
*isl_basic_map_identity(__isl_take isl_dim
*dim
)
5909 if (dim
->n_in
!= dim
->n_out
)
5910 isl_die(dim
->ctx
, isl_error_invalid
,
5911 "number of input and output dimensions needs to be "
5912 "the same", goto error
);
5913 return basic_map_identity(dim
);
5919 struct isl_basic_map
*isl_basic_map_identity_like(struct isl_basic_map
*model
)
5921 if (!model
|| !model
->dim
)
5923 return isl_basic_map_identity(isl_dim_copy(model
->dim
));
5926 __isl_give isl_map
*isl_map_identity(__isl_take isl_dim
*dim
)
5928 return isl_map_from_basic_map(isl_basic_map_identity(dim
));
5931 struct isl_map
*isl_map_identity_like(struct isl_map
*model
)
5933 if (!model
|| !model
->dim
)
5935 return isl_map_identity(isl_dim_copy(model
->dim
));
5938 struct isl_map
*isl_map_identity_like_basic_map(struct isl_basic_map
*model
)
5940 if (!model
|| !model
->dim
)
5942 return isl_map_identity(isl_dim_copy(model
->dim
));
5945 __isl_give isl_map
*isl_set_identity(__isl_take isl_set
*set
)
5947 isl_dim
*dim
= isl_set_get_dim(set
);
5949 id
= isl_map_identity(isl_dim_map_from_set(dim
));
5950 return isl_map_intersect_range(id
, set
);
5953 /* Construct a basic set with all set dimensions having only non-negative
5956 struct isl_basic_set
*isl_basic_set_positive_orthant(struct isl_dim
*dims
)
5961 struct isl_basic_set
*bset
;
5965 nparam
= dims
->nparam
;
5967 bset
= isl_basic_set_alloc_dim(dims
, 0, 0, dim
);
5970 for (i
= 0; i
< dim
; ++i
) {
5971 int k
= isl_basic_set_alloc_inequality(bset
);
5974 isl_seq_clr(bset
->ineq
[k
], 1 + isl_basic_set_total_dim(bset
));
5975 isl_int_set_si(bset
->ineq
[k
][1 + nparam
+ i
], 1);
5979 isl_basic_set_free(bset
);
5983 /* Construct the half-space x_pos >= 0.
5985 static __isl_give isl_basic_set
*nonneg_halfspace(__isl_take isl_dim
*dim
,
5989 isl_basic_set
*nonneg
;
5991 nonneg
= isl_basic_set_alloc_dim(dim
, 0, 0, 1);
5992 k
= isl_basic_set_alloc_inequality(nonneg
);
5995 isl_seq_clr(nonneg
->ineq
[k
], 1 + isl_basic_set_total_dim(nonneg
));
5996 isl_int_set_si(nonneg
->ineq
[k
][pos
], 1);
5998 return isl_basic_set_finalize(nonneg
);
6000 isl_basic_set_free(nonneg
);
6004 /* Construct the half-space x_pos <= -1.
6006 static __isl_give isl_basic_set
*neg_halfspace(__isl_take isl_dim
*dim
, int pos
)
6011 neg
= isl_basic_set_alloc_dim(dim
, 0, 0, 1);
6012 k
= isl_basic_set_alloc_inequality(neg
);
6015 isl_seq_clr(neg
->ineq
[k
], 1 + isl_basic_set_total_dim(neg
));
6016 isl_int_set_si(neg
->ineq
[k
][0], -1);
6017 isl_int_set_si(neg
->ineq
[k
][pos
], -1);
6019 return isl_basic_set_finalize(neg
);
6021 isl_basic_set_free(neg
);
6025 __isl_give isl_set
*isl_set_split_dims(__isl_take isl_set
*set
,
6026 enum isl_dim_type type
, unsigned first
, unsigned n
)
6029 isl_basic_set
*nonneg
;
6037 isl_assert(set
->ctx
, first
+ n
<= isl_set_dim(set
, type
), goto error
);
6039 for (i
= 0; i
< n
; ++i
) {
6040 nonneg
= nonneg_halfspace(isl_set_get_dim(set
),
6041 pos(set
->dim
, type
) + first
+ i
);
6042 neg
= neg_halfspace(isl_set_get_dim(set
),
6043 pos(set
->dim
, type
) + first
+ i
);
6045 set
= isl_set_intersect(set
, isl_basic_set_union(nonneg
, neg
));
6054 static int foreach_orthant(__isl_take isl_set
*set
, int *signs
, int first
,
6055 int len
, int (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
6062 if (isl_set_plain_is_empty(set
)) {
6067 return fn(set
, signs
, user
);
6070 half
= isl_set_from_basic_set(nonneg_halfspace(isl_set_get_dim(set
),
6072 half
= isl_set_intersect(half
, isl_set_copy(set
));
6073 if (foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
) < 0)
6077 half
= isl_set_from_basic_set(neg_halfspace(isl_set_get_dim(set
),
6079 half
= isl_set_intersect(half
, set
);
6080 return foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
);
6086 /* Call "fn" on the intersections of "set" with each of the orthants
6087 * (except for obviously empty intersections). The orthant is identified
6088 * by the signs array, with each entry having value 1 or -1 according
6089 * to the sign of the corresponding variable.
6091 int isl_set_foreach_orthant(__isl_keep isl_set
*set
,
6092 int (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
6102 if (isl_set_plain_is_empty(set
))
6105 nparam
= isl_set_dim(set
, isl_dim_param
);
6106 nvar
= isl_set_dim(set
, isl_dim_set
);
6108 signs
= isl_alloc_array(set
->ctx
, int, nparam
+ nvar
);
6110 r
= foreach_orthant(isl_set_copy(set
), signs
, 0, nparam
+ nvar
,
6118 int isl_set_is_equal(struct isl_set
*set1
, struct isl_set
*set2
)
6120 return isl_map_is_equal((struct isl_map
*)set1
, (struct isl_map
*)set2
);
6123 int isl_basic_map_is_subset(
6124 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
6127 struct isl_map
*map1
;
6128 struct isl_map
*map2
;
6130 if (!bmap1
|| !bmap2
)
6133 map1
= isl_map_from_basic_map(isl_basic_map_copy(bmap1
));
6134 map2
= isl_map_from_basic_map(isl_basic_map_copy(bmap2
));
6136 is_subset
= isl_map_is_subset(map1
, map2
);
6144 int isl_basic_map_is_equal(
6145 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
6149 if (!bmap1
|| !bmap2
)
6151 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
6154 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
6158 int isl_basic_set_is_equal(
6159 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
6161 return isl_basic_map_is_equal(
6162 (struct isl_basic_map
*)bset1
, (struct isl_basic_map
*)bset2
);
6165 int isl_map_is_empty(struct isl_map
*map
)
6172 for (i
= 0; i
< map
->n
; ++i
) {
6173 is_empty
= isl_basic_map_is_empty(map
->p
[i
]);
6182 int isl_map_plain_is_empty(__isl_keep isl_map
*map
)
6184 return map
? map
->n
== 0 : -1;
6187 int isl_map_fast_is_empty(__isl_keep isl_map
*map
)
6189 return isl_map_plain_is_empty(map
);
6192 int isl_set_plain_is_empty(struct isl_set
*set
)
6194 return set
? set
->n
== 0 : -1;
6197 int isl_set_fast_is_empty(__isl_keep isl_set
*set
)
6199 return isl_set_plain_is_empty(set
);
6202 int isl_set_is_empty(struct isl_set
*set
)
6204 return isl_map_is_empty((struct isl_map
*)set
);
6207 int isl_map_has_equal_dim(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
6212 return isl_dim_equal(map1
->dim
, map2
->dim
);
6215 int isl_set_has_equal_dim(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
6220 return isl_dim_equal(set1
->dim
, set2
->dim
);
6223 int isl_map_is_equal(struct isl_map
*map1
, struct isl_map
*map2
)
6229 is_subset
= isl_map_is_subset(map1
, map2
);
6232 is_subset
= isl_map_is_subset(map2
, map1
);
6236 int isl_basic_map_is_strict_subset(
6237 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
6241 if (!bmap1
|| !bmap2
)
6243 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
6246 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
6247 if (is_subset
== -1)
6252 int isl_map_is_strict_subset(struct isl_map
*map1
, struct isl_map
*map2
)
6258 is_subset
= isl_map_is_subset(map1
, map2
);
6261 is_subset
= isl_map_is_subset(map2
, map1
);
6262 if (is_subset
== -1)
6267 int isl_set_is_strict_subset(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
6269 return isl_map_is_strict_subset((isl_map
*)set1
, (isl_map
*)set2
);
6272 int isl_basic_map_is_universe(struct isl_basic_map
*bmap
)
6276 return bmap
->n_eq
== 0 && bmap
->n_ineq
== 0;
6279 int isl_basic_set_is_universe(struct isl_basic_set
*bset
)
6283 return bset
->n_eq
== 0 && bset
->n_ineq
== 0;
6286 int isl_map_plain_is_universe(__isl_keep isl_map
*map
)
6293 for (i
= 0; i
< map
->n
; ++i
) {
6294 int r
= isl_basic_map_is_universe(map
->p
[i
]);
6302 int isl_set_plain_is_universe(__isl_keep isl_set
*set
)
6304 return isl_map_plain_is_universe((isl_map
*) set
);
6307 int isl_set_fast_is_universe(__isl_keep isl_set
*set
)
6309 return isl_set_plain_is_universe(set
);
6312 int isl_basic_map_is_empty(struct isl_basic_map
*bmap
)
6314 struct isl_basic_set
*bset
= NULL
;
6315 struct isl_vec
*sample
= NULL
;
6322 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
6325 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
6326 struct isl_basic_map
*copy
= isl_basic_map_copy(bmap
);
6327 copy
= isl_basic_map_remove_redundancies(copy
);
6328 empty
= ISL_F_ISSET(copy
, ISL_BASIC_MAP_EMPTY
);
6329 isl_basic_map_free(copy
);
6333 total
= 1 + isl_basic_map_total_dim(bmap
);
6334 if (bmap
->sample
&& bmap
->sample
->size
== total
) {
6335 int contains
= isl_basic_map_contains(bmap
, bmap
->sample
);
6341 isl_vec_free(bmap
->sample
);
6342 bmap
->sample
= NULL
;
6343 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
6346 sample
= isl_basic_set_sample_vec(bset
);
6349 empty
= sample
->size
== 0;
6350 isl_vec_free(bmap
->sample
);
6351 bmap
->sample
= sample
;
6353 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
6358 int isl_basic_map_plain_is_empty(__isl_keep isl_basic_map
*bmap
)
6362 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
);
6365 int isl_basic_map_fast_is_empty(__isl_keep isl_basic_map
*bmap
)
6367 return isl_basic_map_plain_is_empty(bmap
);
6370 int isl_basic_set_plain_is_empty(__isl_keep isl_basic_set
*bset
)
6374 return ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
);
6377 int isl_basic_set_fast_is_empty(__isl_keep isl_basic_set
*bset
)
6379 return isl_basic_set_plain_is_empty(bset
);
6382 int isl_basic_set_is_empty(struct isl_basic_set
*bset
)
6384 return isl_basic_map_is_empty((struct isl_basic_map
*)bset
);
6387 struct isl_map
*isl_basic_map_union(
6388 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
6390 struct isl_map
*map
;
6391 if (!bmap1
|| !bmap2
)
6394 isl_assert(bmap1
->ctx
, isl_dim_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
6396 map
= isl_map_alloc_dim(isl_dim_copy(bmap1
->dim
), 2, 0);
6399 map
= isl_map_add_basic_map(map
, bmap1
);
6400 map
= isl_map_add_basic_map(map
, bmap2
);
6403 isl_basic_map_free(bmap1
);
6404 isl_basic_map_free(bmap2
);
6408 struct isl_set
*isl_basic_set_union(
6409 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
6411 return (struct isl_set
*)isl_basic_map_union(
6412 (struct isl_basic_map
*)bset1
,
6413 (struct isl_basic_map
*)bset2
);
6416 /* Order divs such that any div only depends on previous divs */
6417 struct isl_basic_map
*isl_basic_map_order_divs(struct isl_basic_map
*bmap
)
6425 off
= isl_dim_total(bmap
->dim
);
6427 for (i
= 0; i
< bmap
->n_div
; ++i
) {
6429 if (isl_int_is_zero(bmap
->div
[i
][0]))
6431 pos
= isl_seq_first_non_zero(bmap
->div
[i
]+1+1+off
+i
,
6435 isl_basic_map_swap_div(bmap
, i
, i
+ pos
);
6441 struct isl_basic_set
*isl_basic_set_order_divs(struct isl_basic_set
*bset
)
6443 return (struct isl_basic_set
*)
6444 isl_basic_map_order_divs((struct isl_basic_map
*)bset
);
6447 __isl_give isl_map
*isl_map_order_divs(__isl_take isl_map
*map
)
6454 for (i
= 0; i
< map
->n
; ++i
) {
6455 map
->p
[i
] = isl_basic_map_order_divs(map
->p
[i
]);
6466 /* Apply the expansion computed by isl_merge_divs.
6467 * The expansion itself is given by "exp" while the resulting
6468 * list of divs is given by "div".
6470 __isl_give isl_basic_set
*isl_basic_set_expand_divs(
6471 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*div
, int *exp
)
6476 bset
= isl_basic_set_cow(bset
);
6480 if (div
->n_row
< bset
->n_div
)
6481 isl_die(isl_mat_get_ctx(div
), isl_error_invalid
,
6482 "not an expansion", goto error
);
6484 bset
= isl_basic_map_extend_dim(bset
, isl_dim_copy(bset
->dim
),
6485 div
->n_row
- bset
->n_div
, 0,
6486 2 * (div
->n_row
- bset
->n_div
));
6488 n_div
= bset
->n_div
;
6489 for (i
= n_div
; i
< div
->n_row
; ++i
)
6490 if (isl_basic_set_alloc_div(bset
) < 0)
6494 for (i
= div
->n_row
- 1; i
>= 0; --i
) {
6495 if (j
>= 0 && exp
[j
] == i
) {
6497 isl_basic_map_swap_div(bset
, i
, j
);
6500 isl_seq_cpy(bset
->div
[i
], div
->row
[i
], div
->n_col
);
6501 if (isl_basic_map_add_div_constraints(bset
, i
) < 0)
6509 isl_basic_set_free(bset
);
6514 /* Look for a div in dst that corresponds to the div "div" in src.
6515 * The divs before "div" in src and dst are assumed to be the same.
6517 * Returns -1 if no corresponding div was found and the position
6518 * of the corresponding div in dst otherwise.
6520 static int find_div(struct isl_basic_map
*dst
,
6521 struct isl_basic_map
*src
, unsigned div
)
6525 unsigned total
= isl_dim_total(src
->dim
);
6527 isl_assert(dst
->ctx
, div
<= dst
->n_div
, return -1);
6528 for (i
= div
; i
< dst
->n_div
; ++i
)
6529 if (isl_seq_eq(dst
->div
[i
], src
->div
[div
], 1+1+total
+div
) &&
6530 isl_seq_first_non_zero(dst
->div
[i
]+1+1+total
+div
,
6531 dst
->n_div
- div
) == -1)
6536 struct isl_basic_map
*isl_basic_map_align_divs(
6537 struct isl_basic_map
*dst
, struct isl_basic_map
*src
)
6540 unsigned total
= isl_dim_total(src
->dim
);
6545 if (src
->n_div
== 0)
6548 for (i
= 0; i
< src
->n_div
; ++i
)
6549 isl_assert(src
->ctx
, !isl_int_is_zero(src
->div
[i
][0]), goto error
);
6551 src
= isl_basic_map_order_divs(src
);
6552 dst
= isl_basic_map_cow(dst
);
6553 dst
= isl_basic_map_extend_dim(dst
, isl_dim_copy(dst
->dim
),
6554 src
->n_div
, 0, 2 * src
->n_div
);
6557 for (i
= 0; i
< src
->n_div
; ++i
) {
6558 int j
= find_div(dst
, src
, i
);
6560 j
= isl_basic_map_alloc_div(dst
);
6563 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
+i
);
6564 isl_seq_clr(dst
->div
[j
]+1+1+total
+i
, dst
->n_div
- i
);
6565 if (isl_basic_map_add_div_constraints(dst
, j
) < 0)
6569 isl_basic_map_swap_div(dst
, i
, j
);
6573 isl_basic_map_free(dst
);
6577 struct isl_basic_set
*isl_basic_set_align_divs(
6578 struct isl_basic_set
*dst
, struct isl_basic_set
*src
)
6580 return (struct isl_basic_set
*)isl_basic_map_align_divs(
6581 (struct isl_basic_map
*)dst
, (struct isl_basic_map
*)src
);
6584 struct isl_map
*isl_map_align_divs(struct isl_map
*map
)
6592 map
= isl_map_compute_divs(map
);
6593 map
= isl_map_cow(map
);
6597 for (i
= 1; i
< map
->n
; ++i
)
6598 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], map
->p
[i
]);
6599 for (i
= 1; i
< map
->n
; ++i
)
6600 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], map
->p
[0]);
6602 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6606 struct isl_set
*isl_set_align_divs(struct isl_set
*set
)
6608 return (struct isl_set
*)isl_map_align_divs((struct isl_map
*)set
);
6611 struct isl_set
*isl_set_apply(struct isl_set
*set
, struct isl_map
*map
)
6615 isl_assert(set
->ctx
, isl_map_compatible_domain(map
, set
), goto error
);
6616 map
= isl_map_intersect_domain(map
, set
);
6617 set
= isl_map_range(map
);
6625 /* There is no need to cow as removing empty parts doesn't change
6626 * the meaning of the set.
6628 struct isl_map
*isl_map_remove_empty_parts(struct isl_map
*map
)
6635 for (i
= map
->n
-1; i
>= 0; --i
) {
6636 if (!ISL_F_ISSET(map
->p
[i
], ISL_BASIC_MAP_EMPTY
))
6638 isl_basic_map_free(map
->p
[i
]);
6639 if (i
!= map
->n
-1) {
6640 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6641 map
->p
[i
] = map
->p
[map
->n
-1];
6649 struct isl_set
*isl_set_remove_empty_parts(struct isl_set
*set
)
6651 return (struct isl_set
*)
6652 isl_map_remove_empty_parts((struct isl_map
*)set
);
6655 struct isl_basic_map
*isl_map_copy_basic_map(struct isl_map
*map
)
6657 struct isl_basic_map
*bmap
;
6658 if (!map
|| map
->n
== 0)
6660 bmap
= map
->p
[map
->n
-1];
6661 isl_assert(map
->ctx
, ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
), return NULL
);
6662 return isl_basic_map_copy(bmap
);
6665 struct isl_basic_set
*isl_set_copy_basic_set(struct isl_set
*set
)
6667 return (struct isl_basic_set
*)
6668 isl_map_copy_basic_map((struct isl_map
*)set
);
6671 __isl_give isl_map
*isl_map_drop_basic_map(__isl_take isl_map
*map
,
6672 __isl_keep isl_basic_map
*bmap
)
6678 for (i
= map
->n
-1; i
>= 0; --i
) {
6679 if (map
->p
[i
] != bmap
)
6681 map
= isl_map_cow(map
);
6684 isl_basic_map_free(map
->p
[i
]);
6685 if (i
!= map
->n
-1) {
6686 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
6687 map
->p
[i
] = map
->p
[map
->n
-1];
6698 struct isl_set
*isl_set_drop_basic_set(struct isl_set
*set
,
6699 struct isl_basic_set
*bset
)
6701 return (struct isl_set
*)isl_map_drop_basic_map((struct isl_map
*)set
,
6702 (struct isl_basic_map
*)bset
);
6705 /* Given two basic sets bset1 and bset2, compute the maximal difference
6706 * between the values of dimension pos in bset1 and those in bset2
6707 * for any common value of the parameters and dimensions preceding pos.
6709 static enum isl_lp_result
basic_set_maximal_difference_at(
6710 __isl_keep isl_basic_set
*bset1
, __isl_keep isl_basic_set
*bset2
,
6711 int pos
, isl_int
*opt
)
6713 struct isl_dim
*dims
;
6714 struct isl_basic_map
*bmap1
= NULL
;
6715 struct isl_basic_map
*bmap2
= NULL
;
6716 struct isl_ctx
*ctx
;
6717 struct isl_vec
*obj
;
6720 unsigned dim1
, dim2
;
6721 enum isl_lp_result res
;
6723 if (!bset1
|| !bset2
)
6724 return isl_lp_error
;
6726 nparam
= isl_basic_set_n_param(bset1
);
6727 dim1
= isl_basic_set_n_dim(bset1
);
6728 dim2
= isl_basic_set_n_dim(bset2
);
6729 dims
= isl_dim_alloc(bset1
->ctx
, nparam
, pos
, dim1
- pos
);
6730 bmap1
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset1
), dims
);
6731 dims
= isl_dim_alloc(bset2
->ctx
, nparam
, pos
, dim2
- pos
);
6732 bmap2
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset2
), dims
);
6733 if (!bmap1
|| !bmap2
)
6735 bmap1
= isl_basic_map_cow(bmap1
);
6736 bmap1
= isl_basic_map_extend(bmap1
, nparam
,
6737 pos
, (dim1
- pos
) + (dim2
- pos
),
6738 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
6739 bmap1
= add_constraints(bmap1
, bmap2
, 0, dim1
- pos
);
6742 total
= isl_basic_map_total_dim(bmap1
);
6744 obj
= isl_vec_alloc(ctx
, 1 + total
);
6745 isl_seq_clr(obj
->block
.data
, 1 + total
);
6746 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
], 1);
6747 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
+(dim1
-pos
)], -1);
6750 res
= isl_basic_map_solve_lp(bmap1
, 1, obj
->block
.data
, ctx
->one
,
6752 isl_basic_map_free(bmap1
);
6756 isl_basic_map_free(bmap1
);
6757 isl_basic_map_free(bmap2
);
6758 return isl_lp_error
;
6761 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
6762 * for any common value of the parameters and dimensions preceding pos
6763 * in both basic sets, the values of dimension pos in bset1 are
6764 * smaller or larger than those in bset2.
6767 * 1 if bset1 follows bset2
6768 * -1 if bset1 precedes bset2
6769 * 0 if bset1 and bset2 are incomparable
6770 * -2 if some error occurred.
6772 int isl_basic_set_compare_at(struct isl_basic_set
*bset1
,
6773 struct isl_basic_set
*bset2
, int pos
)
6776 enum isl_lp_result res
;
6781 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
6783 if (res
== isl_lp_empty
)
6785 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
6786 res
== isl_lp_unbounded
)
6788 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
6797 /* Given two basic sets bset1 and bset2, check whether
6798 * for any common value of the parameters and dimensions preceding pos
6799 * there is a value of dimension pos in bset1 that is larger
6800 * than a value of the same dimension in bset2.
6803 * 1 if there exists such a pair
6804 * 0 if there is no such pair, but there is a pair of equal values
6806 * -2 if some error occurred.
6808 int isl_basic_set_follows_at(__isl_keep isl_basic_set
*bset1
,
6809 __isl_keep isl_basic_set
*bset2
, int pos
)
6812 enum isl_lp_result res
;
6817 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
6819 if (res
== isl_lp_empty
)
6821 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
6822 res
== isl_lp_unbounded
)
6824 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
6826 else if (res
== isl_lp_ok
)
6835 /* Given two sets set1 and set2, check whether
6836 * for any common value of the parameters and dimensions preceding pos
6837 * there is a value of dimension pos in set1 that is larger
6838 * than a value of the same dimension in set2.
6841 * 1 if there exists such a pair
6842 * 0 if there is no such pair, but there is a pair of equal values
6844 * -2 if some error occurred.
6846 int isl_set_follows_at(__isl_keep isl_set
*set1
,
6847 __isl_keep isl_set
*set2
, int pos
)
6855 for (i
= 0; i
< set1
->n
; ++i
)
6856 for (j
= 0; j
< set2
->n
; ++j
) {
6858 f
= isl_basic_set_follows_at(set1
->p
[i
], set2
->p
[j
], pos
);
6859 if (f
== 1 || f
== -2)
6868 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map
*bmap
,
6869 unsigned pos
, isl_int
*val
)
6877 total
= isl_basic_map_total_dim(bmap
);
6878 for (i
= 0, d
= total
-1; i
< bmap
->n_eq
&& d
+1 > pos
; ++i
) {
6879 for (; d
+1 > pos
; --d
)
6880 if (!isl_int_is_zero(bmap
->eq
[i
][1+d
]))
6884 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1, d
) != -1)
6886 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1+d
+1, total
-d
-1) != -1)
6888 if (!isl_int_is_one(bmap
->eq
[i
][1+d
]))
6891 isl_int_neg(*val
, bmap
->eq
[i
][0]);
6897 static int isl_map_plain_has_fixed_var(__isl_keep isl_map
*map
,
6898 unsigned pos
, isl_int
*val
)
6910 return isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, val
);
6913 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, &v
);
6914 for (i
= 1; fixed
== 1 && i
< map
->n
; ++i
) {
6915 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[i
], pos
, &tmp
);
6916 if (fixed
== 1 && isl_int_ne(tmp
, v
))
6920 isl_int_set(*val
, v
);
6926 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set
*bset
,
6927 unsigned pos
, isl_int
*val
)
6929 return isl_basic_map_plain_has_fixed_var((struct isl_basic_map
*)bset
,
6933 static int isl_set_plain_has_fixed_var(__isl_keep isl_set
*set
, unsigned pos
,
6936 return isl_map_plain_has_fixed_var((struct isl_map
*)set
, pos
, val
);
6939 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map
*bmap
,
6940 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
6942 if (pos
>= isl_basic_map_dim(bmap
, type
))
6944 return isl_basic_map_plain_has_fixed_var(bmap
,
6945 isl_basic_map_offset(bmap
, type
) - 1 + pos
, val
);
6948 int isl_map_plain_is_fixed(__isl_keep isl_map
*map
,
6949 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
6951 if (pos
>= isl_map_dim(map
, type
))
6953 return isl_map_plain_has_fixed_var(map
,
6954 map_offset(map
, type
) - 1 + pos
, val
);
6957 int isl_map_fast_is_fixed(__isl_keep isl_map
*map
,
6958 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
6960 return isl_map_plain_is_fixed(map
, type
, pos
, val
);
6963 /* Check if dimension dim has fixed value and if so and if val is not NULL,
6964 * then return this fixed value in *val.
6966 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set
*bset
,
6967 unsigned dim
, isl_int
*val
)
6969 return isl_basic_set_plain_has_fixed_var(bset
,
6970 isl_basic_set_n_param(bset
) + dim
, val
);
6973 /* Check if dimension dim has fixed value and if so and if val is not NULL,
6974 * then return this fixed value in *val.
6976 int isl_set_plain_dim_is_fixed(__isl_keep isl_set
*set
,
6977 unsigned dim
, isl_int
*val
)
6979 return isl_set_plain_has_fixed_var(set
, isl_set_n_param(set
) + dim
, val
);
6982 int isl_set_fast_dim_is_fixed(__isl_keep isl_set
*set
,
6983 unsigned dim
, isl_int
*val
)
6985 return isl_set_plain_dim_is_fixed(set
, dim
, val
);
6988 /* Check if input variable in has fixed value and if so and if val is not NULL,
6989 * then return this fixed value in *val.
6991 int isl_map_plain_input_is_fixed(__isl_keep isl_map
*map
,
6992 unsigned in
, isl_int
*val
)
6994 return isl_map_plain_has_fixed_var(map
, isl_map_n_param(map
) + in
, val
);
6997 /* Check if dimension dim has an (obvious) fixed lower bound and if so
6998 * and if val is not NULL, then return this lower bound in *val.
7000 int isl_basic_set_plain_dim_has_fixed_lower_bound(
7001 __isl_keep isl_basic_set
*bset
, unsigned dim
, isl_int
*val
)
7003 int i
, i_eq
= -1, i_ineq
= -1;
7010 total
= isl_basic_set_total_dim(bset
);
7011 nparam
= isl_basic_set_n_param(bset
);
7012 for (i
= 0; i
< bset
->n_eq
; ++i
) {
7013 if (isl_int_is_zero(bset
->eq
[i
][1+nparam
+dim
]))
7019 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
7020 if (!isl_int_is_pos(bset
->ineq
[i
][1+nparam
+dim
]))
7022 if (i_eq
!= -1 || i_ineq
!= -1)
7026 if (i_eq
== -1 && i_ineq
== -1)
7028 c
= i_eq
!= -1 ? bset
->eq
[i_eq
] : bset
->ineq
[i_ineq
];
7029 /* The coefficient should always be one due to normalization. */
7030 if (!isl_int_is_one(c
[1+nparam
+dim
]))
7032 if (isl_seq_first_non_zero(c
+1, nparam
+dim
) != -1)
7034 if (isl_seq_first_non_zero(c
+1+nparam
+dim
+1,
7035 total
- nparam
- dim
- 1) != -1)
7038 isl_int_neg(*val
, c
[0]);
7042 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set
*set
,
7043 unsigned dim
, isl_int
*val
)
7055 return isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[0],
7059 fixed
= isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[0],
7061 for (i
= 1; fixed
== 1 && i
< set
->n
; ++i
) {
7062 fixed
= isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[i
],
7064 if (fixed
== 1 && isl_int_ne(tmp
, v
))
7068 isl_int_set(*val
, v
);
7079 /* uset_gist depends on constraints without existentially quantified
7080 * variables sorting first.
7082 static int qsort_constraint_cmp(const void *p1
, const void *p2
)
7084 const struct constraint
*c1
= (const struct constraint
*)p1
;
7085 const struct constraint
*c2
= (const struct constraint
*)p2
;
7087 unsigned size
= isl_min(c1
->size
, c2
->size
);
7089 l1
= isl_seq_last_non_zero(c1
->c
, size
);
7090 l2
= isl_seq_last_non_zero(c2
->c
, size
);
7095 return isl_seq_cmp(c1
->c
, c2
->c
, size
);
7098 static struct isl_basic_map
*isl_basic_map_sort_constraints(
7099 struct isl_basic_map
*bmap
)
7102 struct constraint
*c
;
7107 total
= isl_basic_map_total_dim(bmap
);
7108 c
= isl_alloc_array(bmap
->ctx
, struct constraint
, bmap
->n_ineq
);
7111 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
7113 c
[i
].c
= bmap
->ineq
[i
];
7115 qsort(c
, bmap
->n_ineq
, sizeof(struct constraint
), qsort_constraint_cmp
);
7116 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
7117 bmap
->ineq
[i
] = c
[i
].c
;
7121 isl_basic_map_free(bmap
);
7125 __isl_give isl_basic_set
*isl_basic_set_sort_constraints(
7126 __isl_take isl_basic_set
*bset
)
7128 return (struct isl_basic_set
*)isl_basic_map_sort_constraints(
7129 (struct isl_basic_map
*)bset
);
7132 struct isl_basic_map
*isl_basic_map_normalize(struct isl_basic_map
*bmap
)
7136 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED
))
7138 bmap
= isl_basic_map_remove_redundancies(bmap
);
7139 bmap
= isl_basic_map_sort_constraints(bmap
);
7140 ISL_F_SET(bmap
, ISL_BASIC_MAP_NORMALIZED
);
7144 struct isl_basic_set
*isl_basic_set_normalize(struct isl_basic_set
*bset
)
7146 return (struct isl_basic_set
*)isl_basic_map_normalize(
7147 (struct isl_basic_map
*)bset
);
7150 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map
*bmap1
,
7151 const __isl_keep isl_basic_map
*bmap2
)
7158 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) !=
7159 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_RATIONAL
))
7160 return ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) ? -1 : 1;
7161 if (isl_basic_map_n_param(bmap1
) != isl_basic_map_n_param(bmap2
))
7162 return isl_basic_map_n_param(bmap1
) - isl_basic_map_n_param(bmap2
);
7163 if (isl_basic_map_n_in(bmap1
) != isl_basic_map_n_in(bmap2
))
7164 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
7165 if (isl_basic_map_n_out(bmap1
) != isl_basic_map_n_out(bmap2
))
7166 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
7167 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
) &&
7168 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
7170 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
))
7172 if (ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
7174 if (bmap1
->n_eq
!= bmap2
->n_eq
)
7175 return bmap1
->n_eq
- bmap2
->n_eq
;
7176 if (bmap1
->n_ineq
!= bmap2
->n_ineq
)
7177 return bmap1
->n_ineq
- bmap2
->n_ineq
;
7178 if (bmap1
->n_div
!= bmap2
->n_div
)
7179 return bmap1
->n_div
- bmap2
->n_div
;
7180 total
= isl_basic_map_total_dim(bmap1
);
7181 for (i
= 0; i
< bmap1
->n_eq
; ++i
) {
7182 cmp
= isl_seq_cmp(bmap1
->eq
[i
], bmap2
->eq
[i
], 1+total
);
7186 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
7187 cmp
= isl_seq_cmp(bmap1
->ineq
[i
], bmap2
->ineq
[i
], 1+total
);
7191 for (i
= 0; i
< bmap1
->n_div
; ++i
) {
7192 cmp
= isl_seq_cmp(bmap1
->div
[i
], bmap2
->div
[i
], 1+1+total
);
7199 int isl_basic_map_plain_is_equal(__isl_keep isl_basic_map
*bmap1
,
7200 __isl_keep isl_basic_map
*bmap2
)
7202 return isl_basic_map_plain_cmp(bmap1
, bmap2
) == 0;
7205 int isl_basic_set_plain_is_equal(__isl_keep isl_basic_set
*bset1
,
7206 __isl_keep isl_basic_set
*bset2
)
7208 return isl_basic_map_plain_is_equal((isl_basic_map
*)bset1
,
7209 (isl_basic_map
*)bset2
);
7212 static int qsort_bmap_cmp(const void *p1
, const void *p2
)
7214 const struct isl_basic_map
*bmap1
= *(const struct isl_basic_map
**)p1
;
7215 const struct isl_basic_map
*bmap2
= *(const struct isl_basic_map
**)p2
;
7217 return isl_basic_map_plain_cmp(bmap1
, bmap2
);
7220 /* We normalize in place, but if anything goes wrong we need
7221 * to return NULL, so we need to make sure we don't change the
7222 * meaning of any possible other copies of map.
7224 struct isl_map
*isl_map_normalize(struct isl_map
*map
)
7227 struct isl_basic_map
*bmap
;
7231 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
))
7233 for (i
= 0; i
< map
->n
; ++i
) {
7234 bmap
= isl_basic_map_normalize(isl_basic_map_copy(map
->p
[i
]));
7237 isl_basic_map_free(map
->p
[i
]);
7240 qsort(map
->p
, map
->n
, sizeof(struct isl_basic_map
*), qsort_bmap_cmp
);
7241 ISL_F_SET(map
, ISL_MAP_NORMALIZED
);
7242 map
= isl_map_remove_empty_parts(map
);
7245 for (i
= map
->n
- 1; i
>= 1; --i
) {
7246 if (!isl_basic_map_plain_is_equal(map
->p
[i
-1], map
->p
[i
]))
7248 isl_basic_map_free(map
->p
[i
-1]);
7249 for (j
= i
; j
< map
->n
; ++j
)
7250 map
->p
[j
-1] = map
->p
[j
];
7260 struct isl_set
*isl_set_normalize(struct isl_set
*set
)
7262 return (struct isl_set
*)isl_map_normalize((struct isl_map
*)set
);
7265 int isl_map_plain_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
7275 if (!isl_dim_equal(map1
->dim
, map2
->dim
))
7278 map1
= isl_map_copy(map1
);
7279 map2
= isl_map_copy(map2
);
7280 map1
= isl_map_normalize(map1
);
7281 map2
= isl_map_normalize(map2
);
7284 equal
= map1
->n
== map2
->n
;
7285 for (i
= 0; equal
&& i
< map1
->n
; ++i
) {
7286 equal
= isl_basic_map_plain_is_equal(map1
->p
[i
], map2
->p
[i
]);
7299 int isl_map_fast_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
7301 return isl_map_plain_is_equal(map1
, map2
);
7304 int isl_set_plain_is_equal(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
7306 return isl_map_plain_is_equal((struct isl_map
*)set1
,
7307 (struct isl_map
*)set2
);
7310 int isl_set_fast_is_equal(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
7312 return isl_set_plain_is_equal(set1
, set2
);
7315 /* Return an interval that ranges from min to max (inclusive)
7317 struct isl_basic_set
*isl_basic_set_interval(struct isl_ctx
*ctx
,
7318 isl_int min
, isl_int max
)
7321 struct isl_basic_set
*bset
= NULL
;
7323 bset
= isl_basic_set_alloc(ctx
, 0, 1, 0, 0, 2);
7327 k
= isl_basic_set_alloc_inequality(bset
);
7330 isl_int_set_si(bset
->ineq
[k
][1], 1);
7331 isl_int_neg(bset
->ineq
[k
][0], min
);
7333 k
= isl_basic_set_alloc_inequality(bset
);
7336 isl_int_set_si(bset
->ineq
[k
][1], -1);
7337 isl_int_set(bset
->ineq
[k
][0], max
);
7341 isl_basic_set_free(bset
);
7345 /* Return the Cartesian product of the basic sets in list (in the given order).
7347 __isl_give isl_basic_set
*isl_basic_set_list_product(
7348 __isl_take
struct isl_basic_set_list
*list
)
7356 struct isl_basic_set
*product
= NULL
;
7360 isl_assert(list
->ctx
, list
->n
> 0, goto error
);
7361 isl_assert(list
->ctx
, list
->p
[0], goto error
);
7362 nparam
= isl_basic_set_n_param(list
->p
[0]);
7363 dim
= isl_basic_set_n_dim(list
->p
[0]);
7364 extra
= list
->p
[0]->n_div
;
7365 n_eq
= list
->p
[0]->n_eq
;
7366 n_ineq
= list
->p
[0]->n_ineq
;
7367 for (i
= 1; i
< list
->n
; ++i
) {
7368 isl_assert(list
->ctx
, list
->p
[i
], goto error
);
7369 isl_assert(list
->ctx
,
7370 nparam
== isl_basic_set_n_param(list
->p
[i
]), goto error
);
7371 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
7372 extra
+= list
->p
[i
]->n_div
;
7373 n_eq
+= list
->p
[i
]->n_eq
;
7374 n_ineq
+= list
->p
[i
]->n_ineq
;
7376 product
= isl_basic_set_alloc(list
->ctx
, nparam
, dim
, extra
,
7381 for (i
= 0; i
< list
->n
; ++i
) {
7382 isl_basic_set_add_constraints(product
,
7383 isl_basic_set_copy(list
->p
[i
]), dim
);
7384 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
7386 isl_basic_set_list_free(list
);
7389 isl_basic_set_free(product
);
7390 isl_basic_set_list_free(list
);
7394 struct isl_basic_map
*isl_basic_map_product(
7395 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
7397 struct isl_dim
*dim_result
= NULL
;
7398 struct isl_basic_map
*bmap
;
7399 unsigned in1
, in2
, out1
, out2
, nparam
, total
, pos
;
7400 struct isl_dim_map
*dim_map1
, *dim_map2
;
7402 if (!bmap1
|| !bmap2
)
7405 isl_assert(bmap1
->ctx
, isl_dim_match(bmap1
->dim
, isl_dim_param
,
7406 bmap2
->dim
, isl_dim_param
), goto error
);
7407 dim_result
= isl_dim_product(isl_dim_copy(bmap1
->dim
),
7408 isl_dim_copy(bmap2
->dim
));
7410 in1
= isl_basic_map_n_in(bmap1
);
7411 in2
= isl_basic_map_n_in(bmap2
);
7412 out1
= isl_basic_map_n_out(bmap1
);
7413 out2
= isl_basic_map_n_out(bmap2
);
7414 nparam
= isl_basic_map_n_param(bmap1
);
7416 total
= nparam
+ in1
+ in2
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
7417 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
7418 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
7419 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
7420 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
7421 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
7422 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
7423 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
7424 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
7425 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
7426 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
7428 bmap
= isl_basic_map_alloc_dim(dim_result
,
7429 bmap1
->n_div
+ bmap2
->n_div
,
7430 bmap1
->n_eq
+ bmap2
->n_eq
,
7431 bmap1
->n_ineq
+ bmap2
->n_ineq
);
7432 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
7433 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
7434 bmap
= isl_basic_map_simplify(bmap
);
7435 return isl_basic_map_finalize(bmap
);
7437 isl_basic_map_free(bmap1
);
7438 isl_basic_map_free(bmap2
);
7442 __isl_give isl_basic_map
*isl_basic_map_flat_product(
7443 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
7445 isl_basic_map
*prod
;
7447 prod
= isl_basic_map_product(bmap1
, bmap2
);
7448 prod
= isl_basic_map_flatten(prod
);
7452 __isl_give isl_basic_set
*isl_basic_set_flat_product(
7453 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
7455 return isl_basic_map_flat_product(bset1
, bset2
);
7458 __isl_give isl_basic_map
*isl_basic_map_range_product(
7459 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
7461 isl_dim
*dim_result
= NULL
;
7462 isl_basic_map
*bmap
;
7463 unsigned in
, out1
, out2
, nparam
, total
, pos
;
7464 struct isl_dim_map
*dim_map1
, *dim_map2
;
7466 if (!bmap1
|| !bmap2
)
7469 dim_result
= isl_dim_range_product(isl_dim_copy(bmap1
->dim
),
7470 isl_dim_copy(bmap2
->dim
));
7472 in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
7473 out1
= isl_basic_map_n_out(bmap1
);
7474 out2
= isl_basic_map_n_out(bmap2
);
7475 nparam
= isl_basic_map_n_param(bmap1
);
7477 total
= nparam
+ in
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
7478 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
7479 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
7480 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
7481 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
7482 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
7483 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
7484 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in
);
7485 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
7486 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
7487 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
7489 bmap
= isl_basic_map_alloc_dim(dim_result
,
7490 bmap1
->n_div
+ bmap2
->n_div
,
7491 bmap1
->n_eq
+ bmap2
->n_eq
,
7492 bmap1
->n_ineq
+ bmap2
->n_ineq
);
7493 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
7494 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
7495 bmap
= isl_basic_map_simplify(bmap
);
7496 return isl_basic_map_finalize(bmap
);
7498 isl_basic_map_free(bmap1
);
7499 isl_basic_map_free(bmap2
);
7503 __isl_give isl_basic_map
*isl_basic_map_flat_range_product(
7504 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
7506 isl_basic_map
*prod
;
7508 prod
= isl_basic_map_range_product(bmap1
, bmap2
);
7509 prod
= isl_basic_map_flatten_range(prod
);
7513 static __isl_give isl_map
*map_product(__isl_take isl_map
*map1
,
7514 __isl_take isl_map
*map2
,
7515 __isl_give isl_dim
*(*dim_product
)(__isl_take isl_dim
*left
,
7516 __isl_take isl_dim
*right
),
7517 __isl_give isl_basic_map
*(*basic_map_product
)(
7518 __isl_take isl_basic_map
*left
, __isl_take isl_basic_map
*right
))
7521 struct isl_map
*result
;
7527 isl_assert(map1
->ctx
, isl_dim_match(map1
->dim
, isl_dim_param
,
7528 map2
->dim
, isl_dim_param
), goto error
);
7530 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
7531 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
7532 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
7534 result
= isl_map_alloc_dim(dim_product(isl_dim_copy(map1
->dim
),
7535 isl_dim_copy(map2
->dim
)),
7536 map1
->n
* map2
->n
, flags
);
7539 for (i
= 0; i
< map1
->n
; ++i
)
7540 for (j
= 0; j
< map2
->n
; ++j
) {
7541 struct isl_basic_map
*part
;
7542 part
= basic_map_product(isl_basic_map_copy(map1
->p
[i
]),
7543 isl_basic_map_copy(map2
->p
[j
]));
7544 if (isl_basic_map_is_empty(part
))
7545 isl_basic_map_free(part
);
7547 result
= isl_map_add_basic_map(result
, part
);
7560 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
7562 struct isl_map
*isl_map_product(struct isl_map
*map1
, struct isl_map
*map2
)
7564 return map_product(map1
, map2
, &isl_dim_product
, &isl_basic_map_product
);
7567 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
7569 __isl_give isl_map
*isl_map_flat_product(__isl_take isl_map
*map1
,
7570 __isl_take isl_map
*map2
)
7574 prod
= isl_map_product(map1
, map2
);
7575 prod
= isl_map_flatten(prod
);
7579 /* Given two set A and B, construct its Cartesian product A x B.
7581 struct isl_set
*isl_set_product(struct isl_set
*set1
, struct isl_set
*set2
)
7583 return (struct isl_set
*)isl_map_product((struct isl_map
*)set1
,
7584 (struct isl_map
*)set2
);
7587 __isl_give isl_set
*isl_set_flat_product(__isl_take isl_set
*set1
,
7588 __isl_take isl_set
*set2
)
7590 return (isl_set
*)isl_map_flat_product((isl_map
*)set1
, (isl_map
*)set2
);
7593 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
7595 __isl_give isl_map
*isl_map_range_product(__isl_take isl_map
*map1
,
7596 __isl_take isl_map
*map2
)
7598 return map_product(map1
, map2
, &isl_dim_range_product
,
7599 &isl_basic_map_range_product
);
7602 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
7604 __isl_give isl_map
*isl_map_flat_range_product(__isl_take isl_map
*map1
,
7605 __isl_take isl_map
*map2
)
7609 prod
= isl_map_range_product(map1
, map2
);
7610 prod
= isl_map_flatten_range(prod
);
7614 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map
*bmap
)
7617 uint32_t hash
= isl_hash_init();
7622 bmap
= isl_basic_map_copy(bmap
);
7623 bmap
= isl_basic_map_normalize(bmap
);
7626 total
= isl_basic_map_total_dim(bmap
);
7627 isl_hash_byte(hash
, bmap
->n_eq
& 0xFF);
7628 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
7630 c_hash
= isl_seq_get_hash(bmap
->eq
[i
], 1 + total
);
7631 isl_hash_hash(hash
, c_hash
);
7633 isl_hash_byte(hash
, bmap
->n_ineq
& 0xFF);
7634 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
7636 c_hash
= isl_seq_get_hash(bmap
->ineq
[i
], 1 + total
);
7637 isl_hash_hash(hash
, c_hash
);
7639 isl_hash_byte(hash
, bmap
->n_div
& 0xFF);
7640 for (i
= 0; i
< bmap
->n_div
; ++i
) {
7642 if (isl_int_is_zero(bmap
->div
[i
][0]))
7644 isl_hash_byte(hash
, i
& 0xFF);
7645 c_hash
= isl_seq_get_hash(bmap
->div
[i
], 1 + 1 + total
);
7646 isl_hash_hash(hash
, c_hash
);
7648 isl_basic_map_free(bmap
);
7652 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set
*bset
)
7654 return isl_basic_map_get_hash((isl_basic_map
*)bset
);
7657 uint32_t isl_map_get_hash(__isl_keep isl_map
*map
)
7664 map
= isl_map_copy(map
);
7665 map
= isl_map_normalize(map
);
7669 hash
= isl_hash_init();
7670 for (i
= 0; i
< map
->n
; ++i
) {
7672 bmap_hash
= isl_basic_map_get_hash(map
->p
[i
]);
7673 isl_hash_hash(hash
, bmap_hash
);
7681 uint32_t isl_set_get_hash(__isl_keep isl_set
*set
)
7683 return isl_map_get_hash((isl_map
*)set
);
7686 /* Check if the value for dimension dim is completely determined
7687 * by the values of the other parameters and variables.
7688 * That is, check if dimension dim is involved in an equality.
7690 int isl_basic_set_dim_is_unique(struct isl_basic_set
*bset
, unsigned dim
)
7697 nparam
= isl_basic_set_n_param(bset
);
7698 for (i
= 0; i
< bset
->n_eq
; ++i
)
7699 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ dim
]))
7704 /* Check if the value for dimension dim is completely determined
7705 * by the values of the other parameters and variables.
7706 * That is, check if dimension dim is involved in an equality
7707 * for each of the subsets.
7709 int isl_set_dim_is_unique(struct isl_set
*set
, unsigned dim
)
7715 for (i
= 0; i
< set
->n
; ++i
) {
7717 unique
= isl_basic_set_dim_is_unique(set
->p
[i
], dim
);
7724 int isl_set_n_basic_set(__isl_keep isl_set
*set
)
7726 return set
? set
->n
: 0;
7729 int isl_map_foreach_basic_map(__isl_keep isl_map
*map
,
7730 int (*fn
)(__isl_take isl_basic_map
*bmap
, void *user
), void *user
)
7737 for (i
= 0; i
< map
->n
; ++i
)
7738 if (fn(isl_basic_map_copy(map
->p
[i
]), user
) < 0)
7744 int isl_set_foreach_basic_set(__isl_keep isl_set
*set
,
7745 int (*fn
)(__isl_take isl_basic_set
*bset
, void *user
), void *user
)
7752 for (i
= 0; i
< set
->n
; ++i
)
7753 if (fn(isl_basic_set_copy(set
->p
[i
]), user
) < 0)
7759 __isl_give isl_basic_set
*isl_basic_set_lift(__isl_take isl_basic_set
*bset
)
7761 struct isl_dim
*dim
;
7766 bset
= isl_basic_set_cow(bset
);
7770 dim
= isl_basic_set_get_dim(bset
);
7771 dim
= isl_dim_lift(dim
, bset
->n_div
);
7774 isl_dim_free(bset
->dim
);
7776 bset
->extra
-= bset
->n_div
;
7779 bset
= isl_basic_set_finalize(bset
);
7783 isl_basic_set_free(bset
);
7787 __isl_give isl_set
*isl_set_lift(__isl_take isl_set
*set
)
7790 struct isl_dim
*dim
;
7793 set
= isl_set_align_divs(set
);
7798 set
= isl_set_cow(set
);
7802 n_div
= set
->p
[0]->n_div
;
7803 dim
= isl_set_get_dim(set
);
7804 dim
= isl_dim_lift(dim
, n_div
);
7807 isl_dim_free(set
->dim
);
7810 for (i
= 0; i
< set
->n
; ++i
) {
7811 set
->p
[i
] = isl_basic_set_lift(set
->p
[i
]);
7822 __isl_give isl_map
*isl_set_lifting(__isl_take isl_set
*set
)
7824 struct isl_dim
*dim
;
7825 struct isl_basic_map
*bmap
;
7832 set
= isl_set_align_divs(set
);
7837 dim
= isl_set_get_dim(set
);
7838 if (set
->n
== 0 || set
->p
[0]->n_div
== 0) {
7840 return isl_map_identity(isl_dim_map_from_set(dim
));
7843 n_div
= set
->p
[0]->n_div
;
7844 dim
= isl_dim_map_from_set(dim
);
7845 n_param
= isl_dim_size(dim
, isl_dim_param
);
7846 n_set
= isl_dim_size(dim
, isl_dim_in
);
7847 dim
= isl_dim_extend(dim
, n_param
, n_set
, n_set
+ n_div
);
7848 bmap
= isl_basic_map_alloc_dim(dim
, 0, n_set
, 2 * n_div
);
7849 for (i
= 0; i
< n_set
; ++i
)
7850 bmap
= var_equal(bmap
, i
);
7852 total
= n_param
+ n_set
+ n_set
+ n_div
;
7853 for (i
= 0; i
< n_div
; ++i
) {
7854 k
= isl_basic_map_alloc_inequality(bmap
);
7857 isl_seq_cpy(bmap
->ineq
[k
], set
->p
[0]->div
[i
]+1, 1+n_param
);
7858 isl_seq_clr(bmap
->ineq
[k
]+1+n_param
, n_set
);
7859 isl_seq_cpy(bmap
->ineq
[k
]+1+n_param
+n_set
,
7860 set
->p
[0]->div
[i
]+1+1+n_param
, n_set
+ n_div
);
7861 isl_int_neg(bmap
->ineq
[k
][1+n_param
+n_set
+n_set
+i
],
7862 set
->p
[0]->div
[i
][0]);
7864 l
= isl_basic_map_alloc_inequality(bmap
);
7867 isl_seq_neg(bmap
->ineq
[l
], bmap
->ineq
[k
], 1 + total
);
7868 isl_int_add(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0],
7869 set
->p
[0]->div
[i
][0]);
7870 isl_int_sub_ui(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], 1);
7874 bmap
= isl_basic_map_simplify(bmap
);
7875 bmap
= isl_basic_map_finalize(bmap
);
7876 return isl_map_from_basic_map(bmap
);
7879 isl_basic_map_free(bmap
);
7883 int isl_basic_set_size(__isl_keep isl_basic_set
*bset
)
7891 dim
= isl_basic_set_total_dim(bset
);
7892 size
+= bset
->n_eq
* (1 + dim
);
7893 size
+= bset
->n_ineq
* (1 + dim
);
7894 size
+= bset
->n_div
* (2 + dim
);
7899 int isl_set_size(__isl_keep isl_set
*set
)
7907 for (i
= 0; i
< set
->n
; ++i
)
7908 size
+= isl_basic_set_size(set
->p
[i
]);
7913 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
7914 enum isl_dim_type type
, unsigned pos
)
7922 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), return -1);
7924 pos
+= isl_basic_map_offset(bmap
, type
);
7926 for (i
= 0; i
< bmap
->n_eq
; ++i
)
7927 if (!isl_int_is_zero(bmap
->eq
[i
][pos
]))
7931 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
7932 int sgn
= isl_int_sgn(bmap
->ineq
[i
][pos
]);
7939 return lower
&& upper
;
7942 int isl_map_dim_is_bounded(__isl_keep isl_map
*map
,
7943 enum isl_dim_type type
, unsigned pos
)
7950 for (i
= 0; i
< map
->n
; ++i
) {
7952 bounded
= isl_basic_map_dim_is_bounded(map
->p
[i
], type
, pos
);
7953 if (bounded
< 0 || !bounded
)
7960 /* Return 1 if the specified dim is involved in both an upper bound
7961 * and a lower bound.
7963 int isl_set_dim_is_bounded(__isl_keep isl_set
*set
,
7964 enum isl_dim_type type
, unsigned pos
)
7966 return isl_map_dim_is_bounded((isl_map
*)set
, type
, pos
);
7969 /* For each of the "n" variables starting at "first", determine
7970 * the sign of the variable and put the results in the first "n"
7971 * elements of the array "signs".
7973 * 1 means that the variable is non-negative
7974 * -1 means that the variable is non-positive
7975 * 0 means the variable attains both positive and negative values.
7977 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set
*bset
,
7978 unsigned first
, unsigned n
, int *signs
)
7980 isl_vec
*bound
= NULL
;
7981 struct isl_tab
*tab
= NULL
;
7982 struct isl_tab_undo
*snap
;
7985 if (!bset
|| !signs
)
7988 bound
= isl_vec_alloc(bset
->ctx
, 1 + isl_basic_set_total_dim(bset
));
7989 tab
= isl_tab_from_basic_set(bset
);
7993 isl_seq_clr(bound
->el
, bound
->size
);
7994 isl_int_set_si(bound
->el
[0], -1);
7996 snap
= isl_tab_snap(tab
);
7997 for (i
= 0; i
< n
; ++i
) {
8000 isl_int_set_si(bound
->el
[1 + first
+ i
], -1);
8001 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
8004 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
8005 if (isl_tab_rollback(tab
, snap
) < 0)
8013 isl_int_set_si(bound
->el
[1 + first
+ i
], 1);
8014 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
8017 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
8018 if (isl_tab_rollback(tab
, snap
) < 0)
8021 signs
[i
] = empty
? -1 : 0;
8025 isl_vec_free(bound
);
8029 isl_vec_free(bound
);
8033 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set
*bset
,
8034 enum isl_dim_type type
, unsigned first
, unsigned n
, int *signs
)
8036 if (!bset
|| !signs
)
8038 isl_assert(bset
->ctx
, first
+ n
<= isl_basic_set_dim(bset
, type
),
8041 first
+= pos(bset
->dim
, type
) - 1;
8042 return isl_basic_set_vars_get_sign(bset
, first
, n
, signs
);
8045 /* Check if the given basic map is obviously single-valued.
8046 * In particular, for each output dimension, check that there is
8047 * an equality that defines the output dimension in terms of
8048 * earlier dimensions.
8050 int isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map
*bmap
)
8060 total
= 1 + isl_basic_map_total_dim(bmap
);
8061 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
8062 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
8064 for (i
= 0; i
< n_out
; ++i
) {
8065 for (j
= 0; j
< bmap
->n_eq
; ++j
) {
8066 if (isl_int_is_zero(bmap
->eq
[j
][o_out
+ i
]))
8068 if (isl_seq_first_non_zero(bmap
->eq
[j
] + o_out
+ i
+ 1,
8069 total
- (o_out
+ i
+ 1)) == -1)
8072 if (j
>= bmap
->n_eq
)
8079 /* Check if the given map is obviously single-valued.
8081 int isl_map_plain_is_single_valued(__isl_keep isl_map
*map
)
8090 return isl_basic_map_plain_is_single_valued(map
->p
[0]);
8093 /* Check if the given map is single-valued.
8098 * and check if the result is a subset of the identity mapping.
8100 int isl_map_is_single_valued(__isl_keep isl_map
*map
)
8107 sv
= isl_map_plain_is_single_valued(map
);
8111 test
= isl_map_reverse(isl_map_copy(map
));
8112 test
= isl_map_apply_range(test
, isl_map_copy(map
));
8114 dim
= isl_dim_map_from_set(isl_dim_range(isl_map_get_dim(map
)));
8115 id
= isl_map_identity(dim
);
8117 sv
= isl_map_is_subset(test
, id
);
8125 int isl_map_is_injective(__isl_keep isl_map
*map
)
8129 map
= isl_map_copy(map
);
8130 map
= isl_map_reverse(map
);
8131 in
= isl_map_is_single_valued(map
);
8137 /* Check if the given map is obviously injective.
8139 int isl_map_plain_is_injective(__isl_keep isl_map
*map
)
8143 map
= isl_map_copy(map
);
8144 map
= isl_map_reverse(map
);
8145 in
= isl_map_plain_is_single_valued(map
);
8151 int isl_map_is_bijective(__isl_keep isl_map
*map
)
8155 sv
= isl_map_is_single_valued(map
);
8159 return isl_map_is_injective(map
);
8162 int isl_set_is_singleton(__isl_keep isl_set
*set
)
8164 return isl_map_is_single_valued((isl_map
*)set
);
8167 int isl_map_is_translation(__isl_keep isl_map
*map
)
8172 delta
= isl_map_deltas(isl_map_copy(map
));
8173 ok
= isl_set_is_singleton(delta
);
8174 isl_set_free(delta
);
8179 static int unique(isl_int
*p
, unsigned pos
, unsigned len
)
8181 if (isl_seq_first_non_zero(p
, pos
) != -1)
8183 if (isl_seq_first_non_zero(p
+ pos
+ 1, len
- pos
- 1) != -1)
8188 int isl_basic_set_is_box(__isl_keep isl_basic_set
*bset
)
8197 if (isl_basic_set_dim(bset
, isl_dim_div
) != 0)
8200 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
8201 ovar
= isl_dim_offset(bset
->dim
, isl_dim_set
);
8202 for (j
= 0; j
< nvar
; ++j
) {
8203 int lower
= 0, upper
= 0;
8204 for (i
= 0; i
< bset
->n_eq
; ++i
) {
8205 if (isl_int_is_zero(bset
->eq
[i
][1 + ovar
+ j
]))
8207 if (!unique(bset
->eq
[i
] + 1 + ovar
, j
, nvar
))
8213 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
8214 if (isl_int_is_zero(bset
->ineq
[i
][1 + ovar
+ j
]))
8216 if (!unique(bset
->ineq
[i
] + 1 + ovar
, j
, nvar
))
8218 if (isl_int_is_pos(bset
->ineq
[i
][1 + ovar
+ j
]))
8223 if (!lower
|| !upper
)
8230 int isl_set_is_box(__isl_keep isl_set
*set
)
8237 return isl_basic_set_is_box(set
->p
[0]);
8240 int isl_basic_set_is_wrapping(__isl_keep isl_basic_set
*bset
)
8245 return isl_dim_is_wrapping(bset
->dim
);
8248 int isl_set_is_wrapping(__isl_keep isl_set
*set
)
8253 return isl_dim_is_wrapping(set
->dim
);
8256 __isl_give isl_basic_set
*isl_basic_map_wrap(__isl_take isl_basic_map
*bmap
)
8258 bmap
= isl_basic_map_cow(bmap
);
8262 bmap
->dim
= isl_dim_wrap(bmap
->dim
);
8266 bmap
= isl_basic_map_finalize(bmap
);
8268 return (isl_basic_set
*)bmap
;
8270 isl_basic_map_free(bmap
);
8274 __isl_give isl_set
*isl_map_wrap(__isl_take isl_map
*map
)
8278 map
= isl_map_cow(map
);
8282 for (i
= 0; i
< map
->n
; ++i
) {
8283 map
->p
[i
] = (isl_basic_map
*)isl_basic_map_wrap(map
->p
[i
]);
8287 map
->dim
= isl_dim_wrap(map
->dim
);
8291 return (isl_set
*)map
;
8297 __isl_give isl_basic_map
*isl_basic_set_unwrap(__isl_take isl_basic_set
*bset
)
8299 bset
= isl_basic_set_cow(bset
);
8303 bset
->dim
= isl_dim_unwrap(bset
->dim
);
8307 bset
= isl_basic_set_finalize(bset
);
8309 return (isl_basic_map
*)bset
;
8311 isl_basic_set_free(bset
);
8315 __isl_give isl_map
*isl_set_unwrap(__isl_take isl_set
*set
)
8322 if (!isl_set_is_wrapping(set
))
8323 isl_die(set
->ctx
, isl_error_invalid
, "not a wrapping set",
8326 set
= isl_set_cow(set
);
8330 for (i
= 0; i
< set
->n
; ++i
) {
8331 set
->p
[i
] = (isl_basic_set
*)isl_basic_set_unwrap(set
->p
[i
]);
8336 set
->dim
= isl_dim_unwrap(set
->dim
);
8340 return (isl_map
*)set
;
8346 __isl_give isl_basic_map
*isl_basic_map_reset(__isl_take isl_basic_map
*bmap
,
8347 enum isl_dim_type type
)
8352 if (!isl_dim_is_named_or_nested(bmap
->dim
, type
))
8355 bmap
= isl_basic_map_cow(bmap
);
8359 bmap
->dim
= isl_dim_reset(bmap
->dim
, type
);
8363 bmap
= isl_basic_map_finalize(bmap
);
8367 isl_basic_map_free(bmap
);
8371 __isl_give isl_map
*isl_map_reset(__isl_take isl_map
*map
,
8372 enum isl_dim_type type
)
8379 if (!isl_dim_is_named_or_nested(map
->dim
, type
))
8382 map
= isl_map_cow(map
);
8386 for (i
= 0; i
< map
->n
; ++i
) {
8387 map
->p
[i
] = isl_basic_map_reset(map
->p
[i
], type
);
8391 map
->dim
= isl_dim_reset(map
->dim
, type
);
8401 __isl_give isl_basic_map
*isl_basic_map_flatten(__isl_take isl_basic_map
*bmap
)
8406 if (!bmap
->dim
->nested
[0] && !bmap
->dim
->nested
[1])
8409 bmap
= isl_basic_map_cow(bmap
);
8413 bmap
->dim
= isl_dim_flatten(bmap
->dim
);
8417 bmap
= isl_basic_map_finalize(bmap
);
8421 isl_basic_map_free(bmap
);
8425 __isl_give isl_basic_set
*isl_basic_set_flatten(__isl_take isl_basic_set
*bset
)
8427 return (isl_basic_set
*)isl_basic_map_flatten((isl_basic_map
*)bset
);
8430 __isl_give isl_basic_map
*isl_basic_map_flatten_range(
8431 __isl_take isl_basic_map
*bmap
)
8436 if (!bmap
->dim
->nested
[1])
8439 bmap
= isl_basic_map_cow(bmap
);
8443 bmap
->dim
= isl_dim_flatten_range(bmap
->dim
);
8447 bmap
= isl_basic_map_finalize(bmap
);
8451 isl_basic_map_free(bmap
);
8455 __isl_give isl_map
*isl_map_flatten(__isl_take isl_map
*map
)
8462 if (!map
->dim
->nested
[0] && !map
->dim
->nested
[1])
8465 map
= isl_map_cow(map
);
8469 for (i
= 0; i
< map
->n
; ++i
) {
8470 map
->p
[i
] = isl_basic_map_flatten(map
->p
[i
]);
8474 map
->dim
= isl_dim_flatten(map
->dim
);
8484 __isl_give isl_set
*isl_set_flatten(__isl_take isl_set
*set
)
8486 return (isl_set
*)isl_map_flatten((isl_map
*)set
);
8489 __isl_give isl_map
*isl_set_flatten_map(__isl_take isl_set
*set
)
8491 isl_dim
*dim
, *flat_dim
;
8494 dim
= isl_set_get_dim(set
);
8495 flat_dim
= isl_dim_flatten(isl_dim_copy(dim
));
8496 map
= isl_map_identity(isl_dim_join(isl_dim_reverse(dim
), flat_dim
));
8497 map
= isl_map_intersect_domain(map
, set
);
8502 __isl_give isl_map
*isl_map_flatten_range(__isl_take isl_map
*map
)
8509 if (!map
->dim
->nested
[1])
8512 map
= isl_map_cow(map
);
8516 for (i
= 0; i
< map
->n
; ++i
) {
8517 map
->p
[i
] = isl_basic_map_flatten_range(map
->p
[i
]);
8521 map
->dim
= isl_dim_flatten_range(map
->dim
);
8531 /* Reorder the dimensions of "bmap" according to the given dim_map
8532 * and set the dimension specification to "dim".
8534 __isl_give isl_basic_map
*isl_basic_map_realign(__isl_take isl_basic_map
*bmap
,
8535 __isl_take isl_dim
*dim
, __isl_take
struct isl_dim_map
*dim_map
)
8539 bmap
= isl_basic_map_cow(bmap
);
8540 if (!bmap
|| !dim
|| !dim_map
)
8543 res
= isl_basic_map_alloc_dim(dim
,
8544 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
8545 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
8546 res
= isl_basic_map_finalize(res
);
8550 isl_basic_map_free(bmap
);
8555 /* Reorder the dimensions of "map" according to given reordering.
8557 __isl_give isl_map
*isl_map_realign(__isl_take isl_map
*map
,
8558 __isl_take isl_reordering
*r
)
8561 struct isl_dim_map
*dim_map
;
8563 map
= isl_map_cow(map
);
8564 dim_map
= isl_dim_map_from_reordering(r
);
8565 if (!map
|| !r
|| !dim_map
)
8568 for (i
= 0; i
< map
->n
; ++i
) {
8569 struct isl_dim_map
*dim_map_i
;
8571 dim_map_i
= isl_dim_map_extend(dim_map
, map
->p
[i
]);
8573 map
->p
[i
] = isl_basic_map_realign(map
->p
[i
],
8574 isl_dim_copy(r
->dim
), dim_map_i
);
8580 map
= isl_map_reset_dim(map
, isl_dim_copy(r
->dim
));
8582 isl_reordering_free(r
);
8588 isl_reordering_free(r
);
8592 __isl_give isl_set
*isl_set_realign(__isl_take isl_set
*set
,
8593 __isl_take isl_reordering
*r
)
8595 return (isl_set
*)isl_map_realign((isl_map
*)set
, r
);
8598 __isl_give isl_map
*isl_map_align_params(__isl_take isl_map
*map
,
8599 __isl_take isl_dim
*model
)
8606 ctx
= isl_dim_get_ctx(model
);
8607 if (!isl_dim_has_named_params(model
))
8608 isl_die(ctx
, isl_error_invalid
,
8609 "model has unnamed parameters", goto error
);
8610 if (!isl_dim_has_named_params(map
->dim
))
8611 isl_die(ctx
, isl_error_invalid
,
8612 "relation has unnamed parameters", goto error
);
8613 if (!isl_dim_match(map
->dim
, isl_dim_param
, model
, isl_dim_param
)) {
8614 isl_reordering
*exp
;
8616 model
= isl_dim_drop(model
, isl_dim_in
,
8617 0, isl_dim_size(model
, isl_dim_in
));
8618 model
= isl_dim_drop(model
, isl_dim_out
,
8619 0, isl_dim_size(model
, isl_dim_out
));
8620 exp
= isl_parameter_alignment_reordering(map
->dim
, model
);
8621 exp
= isl_reordering_extend_dim(exp
, isl_map_get_dim(map
));
8622 map
= isl_map_realign(map
, exp
);
8625 isl_dim_free(model
);
8628 isl_dim_free(model
);
8633 __isl_give isl_set
*isl_set_align_params(__isl_take isl_set
*set
,
8634 __isl_take isl_dim
*model
)
8636 return isl_map_align_params(set
, model
);
8639 __isl_give isl_mat
*isl_basic_map_equalities_matrix(
8640 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
8641 enum isl_dim_type c2
, enum isl_dim_type c3
,
8642 enum isl_dim_type c4
, enum isl_dim_type c5
)
8644 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
8645 struct isl_mat
*mat
;
8651 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_eq
,
8652 isl_basic_map_total_dim(bmap
) + 1);
8655 for (i
= 0; i
< bmap
->n_eq
; ++i
)
8656 for (j
= 0, pos
= 0; j
< 5; ++j
) {
8657 int off
= isl_basic_map_offset(bmap
, c
[j
]);
8658 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
8659 isl_int_set(mat
->row
[i
][pos
],
8660 bmap
->eq
[i
][off
+ k
]);
8668 __isl_give isl_mat
*isl_basic_map_inequalities_matrix(
8669 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
8670 enum isl_dim_type c2
, enum isl_dim_type c3
,
8671 enum isl_dim_type c4
, enum isl_dim_type c5
)
8673 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
8674 struct isl_mat
*mat
;
8680 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_ineq
,
8681 isl_basic_map_total_dim(bmap
) + 1);
8684 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
8685 for (j
= 0, pos
= 0; j
< 5; ++j
) {
8686 int off
= isl_basic_map_offset(bmap
, c
[j
]);
8687 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
8688 isl_int_set(mat
->row
[i
][pos
],
8689 bmap
->ineq
[i
][off
+ k
]);
8697 __isl_give isl_basic_map
*isl_basic_map_from_constraint_matrices(
8698 __isl_take isl_dim
*dim
,
8699 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
8700 enum isl_dim_type c2
, enum isl_dim_type c3
,
8701 enum isl_dim_type c4
, enum isl_dim_type c5
)
8703 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
8704 isl_basic_map
*bmap
;
8710 if (!dim
|| !eq
|| !ineq
)
8713 if (eq
->n_col
!= ineq
->n_col
)
8714 isl_die(dim
->ctx
, isl_error_invalid
,
8715 "equalities and inequalities matrices should have "
8716 "same number of columns", goto error
);
8718 total
= 1 + isl_dim_total(dim
);
8720 if (eq
->n_col
< total
)
8721 isl_die(dim
->ctx
, isl_error_invalid
,
8722 "number of columns too small", goto error
);
8724 extra
= eq
->n_col
- total
;
8726 bmap
= isl_basic_map_alloc_dim(isl_dim_copy(dim
), extra
,
8727 eq
->n_row
, ineq
->n_row
);
8730 for (i
= 0; i
< extra
; ++i
) {
8731 k
= isl_basic_map_alloc_div(bmap
);
8734 isl_int_set_si(bmap
->div
[k
][0], 0);
8736 for (i
= 0; i
< eq
->n_row
; ++i
) {
8737 l
= isl_basic_map_alloc_equality(bmap
);
8740 for (j
= 0, pos
= 0; j
< 5; ++j
) {
8741 int off
= isl_basic_map_offset(bmap
, c
[j
]);
8742 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
8743 isl_int_set(bmap
->eq
[l
][off
+ k
],
8749 for (i
= 0; i
< ineq
->n_row
; ++i
) {
8750 l
= isl_basic_map_alloc_inequality(bmap
);
8753 for (j
= 0, pos
= 0; j
< 5; ++j
) {
8754 int off
= isl_basic_map_offset(bmap
, c
[j
]);
8755 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
8756 isl_int_set(bmap
->ineq
[l
][off
+ k
],
8775 __isl_give isl_mat
*isl_basic_set_equalities_matrix(
8776 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
8777 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
8779 return isl_basic_map_equalities_matrix((isl_basic_map
*)bset
,
8780 c1
, c2
, c3
, c4
, isl_dim_in
);
8783 __isl_give isl_mat
*isl_basic_set_inequalities_matrix(
8784 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
8785 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
8787 return isl_basic_map_inequalities_matrix((isl_basic_map
*)bset
,
8788 c1
, c2
, c3
, c4
, isl_dim_in
);
8791 __isl_give isl_basic_set
*isl_basic_set_from_constraint_matrices(
8792 __isl_take isl_dim
*dim
,
8793 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
8794 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
8796 return (isl_basic_set
*)
8797 isl_basic_map_from_constraint_matrices(dim
, eq
, ineq
,
8798 c1
, c2
, c3
, c4
, isl_dim_in
);
8801 int isl_basic_map_can_zip(__isl_keep isl_basic_map
*bmap
)
8806 return isl_dim_can_zip(bmap
->dim
);
8809 int isl_map_can_zip(__isl_keep isl_map
*map
)
8814 return isl_dim_can_zip(map
->dim
);
8817 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
8818 * (A -> C) -> (B -> D).
8820 __isl_give isl_basic_map
*isl_basic_map_zip(__isl_take isl_basic_map
*bmap
)
8829 if (!isl_basic_map_can_zip(bmap
))
8830 isl_die(bmap
->ctx
, isl_error_invalid
,
8831 "basic map cannot be zipped", goto error
);
8832 pos
= isl_basic_map_offset(bmap
, isl_dim_in
) +
8833 isl_dim_size(bmap
->dim
->nested
[0], isl_dim_in
);
8834 n1
= isl_dim_size(bmap
->dim
->nested
[0], isl_dim_out
);
8835 n2
= isl_dim_size(bmap
->dim
->nested
[1], isl_dim_in
);
8836 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
8839 bmap
->dim
= isl_dim_zip(bmap
->dim
);
8844 isl_basic_map_free(bmap
);
8848 /* Given a map (A -> B) -> (C -> D), return the corresponding map
8849 * (A -> C) -> (B -> D).
8851 __isl_give isl_map
*isl_map_zip(__isl_take isl_map
*map
)
8858 if (!isl_map_can_zip(map
))
8859 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be zipped",
8862 map
= isl_map_cow(map
);
8866 for (i
= 0; i
< map
->n
; ++i
) {
8867 map
->p
[i
] = isl_basic_map_zip(map
->p
[i
]);
8872 map
->dim
= isl_dim_zip(map
->dim
);
8882 /* Construct a basic map mapping the domain of the affine expression
8883 * to a one-dimensional range prescribed by the affine expression.
8885 __isl_give isl_basic_map
*isl_basic_map_from_aff(__isl_take isl_aff
*aff
)
8889 isl_local_space
*ls
;
8890 isl_basic_map
*bmap
;
8895 ls
= isl_aff_get_local_space(aff
);
8896 ls
= isl_local_space_from_domain(ls
);
8897 ls
= isl_local_space_add_dims(ls
, isl_dim_out
, 1);
8898 bmap
= isl_basic_map_from_local_space(ls
);
8899 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
8900 k
= isl_basic_map_alloc_equality(bmap
);
8904 pos
= isl_basic_map_offset(bmap
, isl_dim_out
);
8905 isl_seq_cpy(bmap
->eq
[k
], aff
->v
->el
+ 1, pos
);
8906 isl_int_neg(bmap
->eq
[k
][pos
], aff
->v
->el
[0]);
8907 isl_seq_cpy(bmap
->eq
[k
] + pos
+ 1, aff
->v
->el
+ 1 + pos
,
8908 aff
->v
->size
- (pos
+ 1));
8911 bmap
= isl_basic_map_finalize(bmap
);
8915 isl_basic_map_free(bmap
);
8919 /* Construct a basic map mapping a domain in the given space to
8920 * to an n-dimensional range, with n the number of elements in the list,
8921 * where each coordinate in the range is prescribed by the
8922 * corresponding affine expression.
8923 * The domains of all affine expressions in the list are assumed to match
8926 __isl_give isl_basic_map
*isl_basic_map_from_aff_list(
8927 __isl_take isl_dim
*domain_dim
, __isl_take isl_aff_list
*list
)
8931 isl_basic_map
*bmap
;
8936 dim
= isl_dim_from_domain(domain_dim
);
8937 bmap
= isl_basic_map_universe(dim
);
8939 for (i
= 0; i
< list
->n
; ++i
) {
8941 isl_basic_map
*bmap_i
;
8943 aff
= isl_aff_copy(list
->p
[i
]);
8944 bmap_i
= isl_basic_map_from_aff(aff
);
8946 bmap
= isl_basic_map_flat_range_product(bmap
, bmap_i
);
8949 isl_aff_list_free(list
);