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 struct isl_basic_map
*isl_basic_map_reverse(struct isl_basic_map
*bmap
)
2336 struct isl_dim
*dim
;
2337 struct isl_basic_set
*bset
;
2342 bmap
= isl_basic_map_cow(bmap
);
2345 dim
= isl_dim_reverse(isl_dim_copy(bmap
->dim
));
2346 in
= isl_basic_map_n_in(bmap
);
2347 bset
= isl_basic_set_from_basic_map(bmap
);
2348 bset
= isl_basic_set_swap_vars(bset
, in
);
2349 return isl_basic_map_from_basic_set(bset
, dim
);
2352 __isl_give isl_basic_map
*isl_basic_map_insert(__isl_take isl_basic_map
*bmap
,
2353 enum isl_dim_type type
, unsigned pos
, unsigned n
)
2355 struct isl_dim
*res_dim
;
2356 struct isl_basic_map
*res
;
2357 struct isl_dim_map
*dim_map
;
2358 unsigned total
, off
;
2359 enum isl_dim_type t
;
2367 res_dim
= isl_dim_insert(isl_basic_map_get_dim(bmap
), type
, pos
, n
);
2369 total
= isl_basic_map_total_dim(bmap
) + n
;
2370 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
2372 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
2374 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
2376 unsigned size
= isl_basic_map_dim(bmap
, t
);
2377 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2379 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2380 pos
, size
- pos
, off
+ pos
+ n
);
2382 off
+= isl_dim_size(res_dim
, t
);
2384 isl_dim_map_div(dim_map
, bmap
, off
);
2386 res
= isl_basic_map_alloc_dim(res_dim
,
2387 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
2388 if (isl_basic_map_is_rational(bmap
))
2389 res
= isl_basic_map_set_rational(res
);
2390 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
2391 return isl_basic_map_finalize(res
);
2394 __isl_give isl_basic_map
*isl_basic_map_add(__isl_take isl_basic_map
*bmap
,
2395 enum isl_dim_type type
, unsigned n
)
2399 return isl_basic_map_insert(bmap
, type
,
2400 isl_basic_map_dim(bmap
, type
), n
);
2403 __isl_give isl_basic_set
*isl_basic_set_add(__isl_take isl_basic_set
*bset
,
2404 enum isl_dim_type type
, unsigned n
)
2408 isl_assert(bset
->ctx
, type
!= isl_dim_in
, goto error
);
2409 return (isl_basic_set
*)isl_basic_map_add((isl_basic_map
*)bset
, type
, n
);
2411 isl_basic_set_free(bset
);
2415 __isl_give isl_map
*isl_map_insert(__isl_take isl_map
*map
,
2416 enum isl_dim_type type
, unsigned pos
, unsigned n
)
2423 map
= isl_map_cow(map
);
2427 map
->dim
= isl_dim_insert(map
->dim
, type
, pos
, n
);
2431 for (i
= 0; i
< map
->n
; ++i
) {
2432 map
->p
[i
] = isl_basic_map_insert(map
->p
[i
], type
, pos
, n
);
2443 __isl_give isl_set
*isl_set_insert(__isl_take isl_set
*set
,
2444 enum isl_dim_type type
, unsigned pos
, unsigned n
)
2446 return (isl_set
*)isl_map_insert((isl_map
*)set
, type
, pos
, n
);
2449 __isl_give isl_map
*isl_map_add_dims(__isl_take isl_map
*map
,
2450 enum isl_dim_type type
, unsigned n
)
2454 return isl_map_insert(map
, type
, isl_map_dim(map
, type
), n
);
2457 __isl_give isl_set
*isl_set_add_dims(__isl_take isl_set
*set
,
2458 enum isl_dim_type type
, unsigned n
)
2462 isl_assert(set
->ctx
, type
!= isl_dim_in
, goto error
);
2463 return (isl_set
*)isl_map_add_dims((isl_map
*)set
, type
, n
);
2469 __isl_give isl_basic_map
*isl_basic_map_move_dims(
2470 __isl_take isl_basic_map
*bmap
,
2471 enum isl_dim_type dst_type
, unsigned dst_pos
,
2472 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2474 struct isl_dim_map
*dim_map
;
2475 struct isl_basic_map
*res
;
2476 enum isl_dim_type t
;
2477 unsigned total
, off
;
2484 isl_assert(bmap
->ctx
, src_pos
+ n
<= isl_basic_map_dim(bmap
, src_type
),
2487 if (dst_type
== src_type
&& dst_pos
== src_pos
)
2490 isl_assert(bmap
->ctx
, dst_type
!= src_type
, goto error
);
2492 if (pos(bmap
->dim
, dst_type
) + dst_pos
==
2493 pos(bmap
->dim
, src_type
) + src_pos
+
2494 ((src_type
< dst_type
) ? n
: 0)) {
2495 bmap
= isl_basic_map_cow(bmap
);
2499 bmap
->dim
= isl_dim_move(bmap
->dim
, dst_type
, dst_pos
,
2500 src_type
, src_pos
, n
);
2504 bmap
= isl_basic_map_finalize(bmap
);
2509 total
= isl_basic_map_total_dim(bmap
);
2510 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
2513 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
2514 unsigned size
= isl_dim_size(bmap
->dim
, t
);
2515 if (t
== dst_type
) {
2516 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2519 isl_dim_map_dim_range(dim_map
, bmap
->dim
, src_type
,
2522 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2523 dst_pos
, size
- dst_pos
, off
);
2524 off
+= size
- dst_pos
;
2525 } else if (t
== src_type
) {
2526 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2529 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2530 src_pos
+ n
, size
- src_pos
- n
, off
);
2531 off
+= size
- src_pos
- n
;
2533 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
2537 isl_dim_map_div(dim_map
, bmap
, off
);
2539 res
= isl_basic_map_alloc_dim(isl_basic_map_get_dim(bmap
),
2540 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
2541 bmap
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
2543 bmap
->dim
= isl_dim_move(bmap
->dim
, dst_type
, dst_pos
,
2544 src_type
, src_pos
, n
);
2548 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
2549 bmap
= isl_basic_map_gauss(bmap
, NULL
);
2550 bmap
= isl_basic_map_finalize(bmap
);
2554 isl_basic_map_free(bmap
);
2558 __isl_give isl_basic_set
*isl_basic_set_move_dims(__isl_take isl_basic_set
*bset
,
2559 enum isl_dim_type dst_type
, unsigned dst_pos
,
2560 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2562 return (isl_basic_set
*)isl_basic_map_move_dims(
2563 (isl_basic_map
*)bset
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
2566 __isl_give isl_set
*isl_set_move_dims(__isl_take isl_set
*set
,
2567 enum isl_dim_type dst_type
, unsigned dst_pos
,
2568 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2572 isl_assert(set
->ctx
, dst_type
!= isl_dim_in
, goto error
);
2573 return (isl_set
*)isl_map_move_dims((isl_map
*)set
, dst_type
, dst_pos
,
2574 src_type
, src_pos
, n
);
2580 __isl_give isl_map
*isl_map_move_dims(__isl_take isl_map
*map
,
2581 enum isl_dim_type dst_type
, unsigned dst_pos
,
2582 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2591 isl_assert(map
->ctx
, src_pos
+ n
<= isl_map_dim(map
, src_type
),
2594 if (dst_type
== src_type
&& dst_pos
== src_pos
)
2597 isl_assert(map
->ctx
, dst_type
!= src_type
, goto error
);
2599 map
= isl_map_cow(map
);
2603 map
->dim
= isl_dim_move(map
->dim
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
2607 for (i
= 0; i
< map
->n
; ++i
) {
2608 map
->p
[i
] = isl_basic_map_move_dims(map
->p
[i
],
2610 src_type
, src_pos
, n
);
2621 /* Move the specified dimensions to the last columns right before
2622 * the divs. Don't change the dimension specification of bmap.
2623 * That's the responsibility of the caller.
2625 static __isl_give isl_basic_map
*move_last(__isl_take isl_basic_map
*bmap
,
2626 enum isl_dim_type type
, unsigned first
, unsigned n
)
2628 struct isl_dim_map
*dim_map
;
2629 struct isl_basic_map
*res
;
2630 enum isl_dim_type t
;
2631 unsigned total
, off
;
2635 if (pos(bmap
->dim
, type
) + first
+ n
== 1 + isl_dim_total(bmap
->dim
))
2638 total
= isl_basic_map_total_dim(bmap
);
2639 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
2642 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
2643 unsigned size
= isl_dim_size(bmap
->dim
, t
);
2645 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2648 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2649 first
, n
, total
- bmap
->n_div
- n
);
2650 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
2651 first
+ n
, size
- (first
+ n
), off
);
2652 off
+= size
- (first
+ n
);
2654 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
2658 isl_dim_map_div(dim_map
, bmap
, off
+ n
);
2660 res
= isl_basic_map_alloc_dim(isl_basic_map_get_dim(bmap
),
2661 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
2662 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
2666 /* Turn the n dimensions of type type, starting at first
2667 * into existentially quantified variables.
2669 __isl_give isl_basic_map
*isl_basic_map_project_out(
2670 __isl_take isl_basic_map
*bmap
,
2671 enum isl_dim_type type
, unsigned first
, unsigned n
)
2684 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
2685 return isl_basic_map_remove_dims(bmap
, type
, first
, n
);
2687 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
2690 bmap
= move_last(bmap
, type
, first
, n
);
2691 bmap
= isl_basic_map_cow(bmap
);
2695 row_size
= 1 + isl_dim_total(bmap
->dim
) + bmap
->extra
;
2696 old
= bmap
->block2
.data
;
2697 bmap
->block2
= isl_blk_extend(bmap
->ctx
, bmap
->block2
,
2698 (bmap
->extra
+ n
) * (1 + row_size
));
2699 if (!bmap
->block2
.data
)
2701 new_div
= isl_alloc_array(bmap
->ctx
, isl_int
*, bmap
->extra
+ n
);
2704 for (i
= 0; i
< n
; ++i
) {
2705 new_div
[i
] = bmap
->block2
.data
+
2706 (bmap
->extra
+ i
) * (1 + row_size
);
2707 isl_seq_clr(new_div
[i
], 1 + row_size
);
2709 for (i
= 0; i
< bmap
->extra
; ++i
)
2710 new_div
[n
+ i
] = bmap
->block2
.data
+ (bmap
->div
[i
] - old
);
2712 bmap
->div
= new_div
;
2716 bmap
->dim
= isl_dim_drop(bmap
->dim
, type
, first
, n
);
2719 bmap
= isl_basic_map_simplify(bmap
);
2720 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
2721 return isl_basic_map_finalize(bmap
);
2723 isl_basic_map_free(bmap
);
2727 /* Turn the n dimensions of type type, starting at first
2728 * into existentially quantified variables.
2730 struct isl_basic_set
*isl_basic_set_project_out(struct isl_basic_set
*bset
,
2731 enum isl_dim_type type
, unsigned first
, unsigned n
)
2733 return (isl_basic_set
*)isl_basic_map_project_out(
2734 (isl_basic_map
*)bset
, type
, first
, n
);
2737 /* Turn the n dimensions of type type, starting at first
2738 * into existentially quantified variables.
2740 __isl_give isl_map
*isl_map_project_out(__isl_take isl_map
*map
,
2741 enum isl_dim_type type
, unsigned first
, unsigned n
)
2751 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
2753 map
= isl_map_cow(map
);
2757 map
->dim
= isl_dim_drop(map
->dim
, type
, first
, n
);
2761 for (i
= 0; i
< map
->n
; ++i
) {
2762 map
->p
[i
] = isl_basic_map_project_out(map
->p
[i
], type
, first
, n
);
2773 /* Turn the n dimensions of type type, starting at first
2774 * into existentially quantified variables.
2776 __isl_give isl_set
*isl_set_project_out(__isl_take isl_set
*set
,
2777 enum isl_dim_type type
, unsigned first
, unsigned n
)
2779 return (isl_set
*)isl_map_project_out((isl_map
*)set
, type
, first
, n
);
2782 static struct isl_basic_map
*add_divs(struct isl_basic_map
*bmap
, unsigned n
)
2786 for (i
= 0; i
< n
; ++i
) {
2787 j
= isl_basic_map_alloc_div(bmap
);
2790 isl_seq_clr(bmap
->div
[j
], 1+1+isl_basic_map_total_dim(bmap
));
2794 isl_basic_map_free(bmap
);
2798 struct isl_basic_map
*isl_basic_map_apply_range(
2799 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
2801 struct isl_dim
*dim_result
= NULL
;
2802 struct isl_basic_map
*bmap
;
2803 unsigned n_in
, n_out
, n
, nparam
, total
, pos
;
2804 struct isl_dim_map
*dim_map1
, *dim_map2
;
2806 if (!bmap1
|| !bmap2
)
2809 dim_result
= isl_dim_join(isl_dim_copy(bmap1
->dim
),
2810 isl_dim_copy(bmap2
->dim
));
2812 n_in
= isl_basic_map_n_in(bmap1
);
2813 n_out
= isl_basic_map_n_out(bmap2
);
2814 n
= isl_basic_map_n_out(bmap1
);
2815 nparam
= isl_basic_map_n_param(bmap1
);
2817 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ n
;
2818 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
2819 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
2820 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
2821 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
2822 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
2823 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_in
);
2824 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_out
);
2825 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
2826 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
2827 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
2829 bmap
= isl_basic_map_alloc_dim(dim_result
,
2830 bmap1
->n_div
+ bmap2
->n_div
+ n
,
2831 bmap1
->n_eq
+ bmap2
->n_eq
,
2832 bmap1
->n_ineq
+ bmap2
->n_ineq
);
2833 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
2834 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
2835 bmap
= add_divs(bmap
, n
);
2836 bmap
= isl_basic_map_simplify(bmap
);
2837 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
2838 return isl_basic_map_finalize(bmap
);
2840 isl_basic_map_free(bmap1
);
2841 isl_basic_map_free(bmap2
);
2845 struct isl_basic_set
*isl_basic_set_apply(
2846 struct isl_basic_set
*bset
, struct isl_basic_map
*bmap
)
2851 isl_assert(bset
->ctx
, isl_basic_map_compatible_domain(bmap
, bset
),
2854 return (struct isl_basic_set
*)
2855 isl_basic_map_apply_range((struct isl_basic_map
*)bset
, bmap
);
2857 isl_basic_set_free(bset
);
2858 isl_basic_map_free(bmap
);
2862 struct isl_basic_map
*isl_basic_map_apply_domain(
2863 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
2865 if (!bmap1
|| !bmap2
)
2868 isl_assert(bmap1
->ctx
,
2869 isl_basic_map_n_in(bmap1
) == isl_basic_map_n_in(bmap2
), goto error
);
2870 isl_assert(bmap1
->ctx
,
2871 isl_basic_map_n_param(bmap1
) == isl_basic_map_n_param(bmap2
),
2874 bmap1
= isl_basic_map_reverse(bmap1
);
2875 bmap1
= isl_basic_map_apply_range(bmap1
, bmap2
);
2876 return isl_basic_map_reverse(bmap1
);
2878 isl_basic_map_free(bmap1
);
2879 isl_basic_map_free(bmap2
);
2883 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
2884 * A \cap B -> f(A) + f(B)
2886 struct isl_basic_map
*isl_basic_map_sum(
2887 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
2889 unsigned n_in
, n_out
, nparam
, total
, pos
;
2890 struct isl_basic_map
*bmap
= NULL
;
2891 struct isl_dim_map
*dim_map1
, *dim_map2
;
2894 if (!bmap1
|| !bmap2
)
2897 isl_assert(bmap1
->ctx
, isl_dim_equal(bmap1
->dim
, bmap2
->dim
),
2900 nparam
= isl_basic_map_n_param(bmap1
);
2901 n_in
= isl_basic_map_n_in(bmap1
);
2902 n_out
= isl_basic_map_n_out(bmap1
);
2904 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
;
2905 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
2906 dim_map2
= isl_dim_map_alloc(bmap2
->ctx
, total
);
2907 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
2908 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
);
2909 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
2910 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
2911 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_in
+ n_out
);
2912 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
2913 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
2914 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_out
);
2916 bmap
= isl_basic_map_alloc_dim(isl_dim_copy(bmap1
->dim
),
2917 bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
,
2918 bmap1
->n_eq
+ bmap2
->n_eq
+ n_out
,
2919 bmap1
->n_ineq
+ bmap2
->n_ineq
);
2920 for (i
= 0; i
< n_out
; ++i
) {
2921 int j
= isl_basic_map_alloc_equality(bmap
);
2924 isl_seq_clr(bmap
->eq
[j
], 1+total
);
2925 isl_int_set_si(bmap
->eq
[j
][1+nparam
+n_in
+i
], -1);
2926 isl_int_set_si(bmap
->eq
[j
][1+pos
+i
], 1);
2927 isl_int_set_si(bmap
->eq
[j
][1+pos
-n_out
+i
], 1);
2929 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
2930 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
2931 bmap
= add_divs(bmap
, 2 * n_out
);
2933 bmap
= isl_basic_map_simplify(bmap
);
2934 return isl_basic_map_finalize(bmap
);
2936 isl_basic_map_free(bmap
);
2937 isl_basic_map_free(bmap1
);
2938 isl_basic_map_free(bmap2
);
2942 /* Given two maps A -> f(A) and B -> g(B), construct a map
2943 * A \cap B -> f(A) + f(B)
2945 struct isl_map
*isl_map_sum(struct isl_map
*map1
, struct isl_map
*map2
)
2947 struct isl_map
*result
;
2953 isl_assert(map1
->ctx
, isl_dim_equal(map1
->dim
, map2
->dim
), goto error
);
2955 result
= isl_map_alloc_dim(isl_dim_copy(map1
->dim
),
2956 map1
->n
* map2
->n
, 0);
2959 for (i
= 0; i
< map1
->n
; ++i
)
2960 for (j
= 0; j
< map2
->n
; ++j
) {
2961 struct isl_basic_map
*part
;
2962 part
= isl_basic_map_sum(
2963 isl_basic_map_copy(map1
->p
[i
]),
2964 isl_basic_map_copy(map2
->p
[j
]));
2965 if (isl_basic_map_is_empty(part
))
2966 isl_basic_map_free(part
);
2968 result
= isl_map_add_basic_map(result
, part
);
2981 __isl_give isl_set
*isl_set_sum(__isl_take isl_set
*set1
,
2982 __isl_take isl_set
*set2
)
2984 return (isl_set
*)isl_map_sum((isl_map
*)set1
, (isl_map
*)set2
);
2987 /* Given a basic map A -> f(A), construct A -> -f(A).
2989 struct isl_basic_map
*isl_basic_map_neg(struct isl_basic_map
*bmap
)
2994 bmap
= isl_basic_map_cow(bmap
);
2998 n
= isl_basic_map_dim(bmap
, isl_dim_out
);
2999 off
= isl_basic_map_offset(bmap
, isl_dim_out
);
3000 for (i
= 0; i
< bmap
->n_eq
; ++i
)
3001 for (j
= 0; j
< n
; ++j
)
3002 isl_int_neg(bmap
->eq
[i
][off
+j
], bmap
->eq
[i
][off
+j
]);
3003 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
3004 for (j
= 0; j
< n
; ++j
)
3005 isl_int_neg(bmap
->ineq
[i
][off
+j
], bmap
->ineq
[i
][off
+j
]);
3006 for (i
= 0; i
< bmap
->n_div
; ++i
)
3007 for (j
= 0; j
< n
; ++j
)
3008 isl_int_neg(bmap
->div
[i
][1+off
+j
], bmap
->div
[i
][1+off
+j
]);
3009 return isl_basic_map_finalize(bmap
);
3012 __isl_give isl_basic_set
*isl_basic_set_neg(__isl_take isl_basic_set
*bset
)
3014 return isl_basic_map_neg(bset
);
3017 /* Given a map A -> f(A), construct A -> -f(A).
3019 struct isl_map
*isl_map_neg(struct isl_map
*map
)
3023 map
= isl_map_cow(map
);
3027 for (i
= 0; i
< map
->n
; ++i
) {
3028 map
->p
[i
] = isl_basic_map_neg(map
->p
[i
]);
3039 __isl_give isl_set
*isl_set_neg(__isl_take isl_set
*set
)
3041 return (isl_set
*)isl_map_neg((isl_map
*)set
);
3044 /* Given a basic map A -> f(A) and an integer d, construct a basic map
3045 * A -> floor(f(A)/d).
3047 struct isl_basic_map
*isl_basic_map_floordiv(struct isl_basic_map
*bmap
,
3050 unsigned n_in
, n_out
, nparam
, total
, pos
;
3051 struct isl_basic_map
*result
= NULL
;
3052 struct isl_dim_map
*dim_map
;
3058 nparam
= isl_basic_map_n_param(bmap
);
3059 n_in
= isl_basic_map_n_in(bmap
);
3060 n_out
= isl_basic_map_n_out(bmap
);
3062 total
= nparam
+ n_in
+ n_out
+ bmap
->n_div
+ n_out
;
3063 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3064 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_param
, pos
= 0);
3065 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_in
, pos
+= nparam
);
3066 isl_dim_map_div(dim_map
, bmap
, pos
+= n_in
+ n_out
);
3067 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_out
, pos
+= bmap
->n_div
);
3069 result
= isl_basic_map_alloc_dim(isl_dim_copy(bmap
->dim
),
3070 bmap
->n_div
+ n_out
,
3071 bmap
->n_eq
, bmap
->n_ineq
+ 2 * n_out
);
3072 result
= isl_basic_map_add_constraints_dim_map(result
, bmap
, dim_map
);
3073 result
= add_divs(result
, n_out
);
3074 for (i
= 0; i
< n_out
; ++i
) {
3076 j
= isl_basic_map_alloc_inequality(result
);
3079 isl_seq_clr(result
->ineq
[j
], 1+total
);
3080 isl_int_neg(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
3081 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], 1);
3082 j
= isl_basic_map_alloc_inequality(result
);
3085 isl_seq_clr(result
->ineq
[j
], 1+total
);
3086 isl_int_set(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
3087 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], -1);
3088 isl_int_sub_ui(result
->ineq
[j
][0], d
, 1);
3091 result
= isl_basic_map_simplify(result
);
3092 return isl_basic_map_finalize(result
);
3094 isl_basic_map_free(result
);
3098 /* Given a map A -> f(A) and an integer d, construct a map
3099 * A -> floor(f(A)/d).
3101 struct isl_map
*isl_map_floordiv(struct isl_map
*map
, isl_int d
)
3105 map
= isl_map_cow(map
);
3109 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
3110 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3111 for (i
= 0; i
< map
->n
; ++i
) {
3112 map
->p
[i
] = isl_basic_map_floordiv(map
->p
[i
], d
);
3123 static struct isl_basic_map
*var_equal(struct isl_basic_map
*bmap
, unsigned pos
)
3129 i
= isl_basic_map_alloc_equality(bmap
);
3132 nparam
= isl_basic_map_n_param(bmap
);
3133 n_in
= isl_basic_map_n_in(bmap
);
3134 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3135 isl_int_set_si(bmap
->eq
[i
][1+nparam
+pos
], -1);
3136 isl_int_set_si(bmap
->eq
[i
][1+nparam
+n_in
+pos
], 1);
3137 return isl_basic_map_finalize(bmap
);
3139 isl_basic_map_free(bmap
);
3143 /* Add a constraints to "bmap" expressing i_pos < o_pos
3145 static struct isl_basic_map
*var_less(struct isl_basic_map
*bmap
, unsigned pos
)
3151 i
= isl_basic_map_alloc_inequality(bmap
);
3154 nparam
= isl_basic_map_n_param(bmap
);
3155 n_in
= isl_basic_map_n_in(bmap
);
3156 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3157 isl_int_set_si(bmap
->ineq
[i
][0], -1);
3158 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
3159 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
3160 return isl_basic_map_finalize(bmap
);
3162 isl_basic_map_free(bmap
);
3166 /* Add a constraint to "bmap" expressing i_pos <= o_pos
3168 static __isl_give isl_basic_map
*var_less_or_equal(
3169 __isl_take isl_basic_map
*bmap
, unsigned pos
)
3175 i
= isl_basic_map_alloc_inequality(bmap
);
3178 nparam
= isl_basic_map_n_param(bmap
);
3179 n_in
= isl_basic_map_n_in(bmap
);
3180 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3181 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
3182 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
3183 return isl_basic_map_finalize(bmap
);
3185 isl_basic_map_free(bmap
);
3189 /* Add a constraints to "bmap" expressing i_pos > o_pos
3191 static struct isl_basic_map
*var_more(struct isl_basic_map
*bmap
, unsigned pos
)
3197 i
= isl_basic_map_alloc_inequality(bmap
);
3200 nparam
= isl_basic_map_n_param(bmap
);
3201 n_in
= isl_basic_map_n_in(bmap
);
3202 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3203 isl_int_set_si(bmap
->ineq
[i
][0], -1);
3204 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
3205 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
3206 return isl_basic_map_finalize(bmap
);
3208 isl_basic_map_free(bmap
);
3212 /* Add a constraint to "bmap" expressing i_pos >= o_pos
3214 static __isl_give isl_basic_map
*var_more_or_equal(
3215 __isl_take isl_basic_map
*bmap
, unsigned pos
)
3221 i
= isl_basic_map_alloc_inequality(bmap
);
3224 nparam
= isl_basic_map_n_param(bmap
);
3225 n_in
= isl_basic_map_n_in(bmap
);
3226 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3227 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
3228 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
3229 return isl_basic_map_finalize(bmap
);
3231 isl_basic_map_free(bmap
);
3235 struct isl_basic_map
*isl_basic_map_equal(struct isl_dim
*dim
, unsigned n_equal
)
3238 struct isl_basic_map
*bmap
;
3239 bmap
= isl_basic_map_alloc_dim(dim
, 0, n_equal
, 0);
3242 for (i
= 0; i
< n_equal
&& bmap
; ++i
)
3243 bmap
= var_equal(bmap
, i
);
3244 return isl_basic_map_finalize(bmap
);
3247 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
3249 struct isl_basic_map
*isl_basic_map_less_at(struct isl_dim
*dim
, unsigned pos
)
3252 struct isl_basic_map
*bmap
;
3253 bmap
= isl_basic_map_alloc_dim(dim
, 0, pos
, 1);
3256 for (i
= 0; i
< pos
&& bmap
; ++i
)
3257 bmap
= var_equal(bmap
, i
);
3259 bmap
= var_less(bmap
, pos
);
3260 return isl_basic_map_finalize(bmap
);
3263 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
3265 __isl_give isl_basic_map
*isl_basic_map_less_or_equal_at(
3266 __isl_take isl_dim
*dim
, unsigned pos
)
3269 isl_basic_map
*bmap
;
3271 bmap
= isl_basic_map_alloc_dim(dim
, 0, pos
, 1);
3272 for (i
= 0; i
< pos
; ++i
)
3273 bmap
= var_equal(bmap
, i
);
3274 bmap
= var_less_or_equal(bmap
, pos
);
3275 return isl_basic_map_finalize(bmap
);
3278 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
3280 struct isl_basic_map
*isl_basic_map_more_at(struct isl_dim
*dim
, unsigned pos
)
3283 struct isl_basic_map
*bmap
;
3284 bmap
= isl_basic_map_alloc_dim(dim
, 0, pos
, 1);
3287 for (i
= 0; i
< pos
&& bmap
; ++i
)
3288 bmap
= var_equal(bmap
, i
);
3290 bmap
= var_more(bmap
, pos
);
3291 return isl_basic_map_finalize(bmap
);
3294 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
3296 __isl_give isl_basic_map
*isl_basic_map_more_or_equal_at(
3297 __isl_take isl_dim
*dim
, unsigned pos
)
3300 isl_basic_map
*bmap
;
3302 bmap
= isl_basic_map_alloc_dim(dim
, 0, pos
, 1);
3303 for (i
= 0; i
< pos
; ++i
)
3304 bmap
= var_equal(bmap
, i
);
3305 bmap
= var_more_or_equal(bmap
, pos
);
3306 return isl_basic_map_finalize(bmap
);
3309 static __isl_give isl_map
*map_lex_lte_first(__isl_take isl_dim
*dims
,
3310 unsigned n
, int equal
)
3312 struct isl_map
*map
;
3315 if (n
== 0 && equal
)
3316 return isl_map_universe(dims
);
3318 map
= isl_map_alloc_dim(isl_dim_copy(dims
), n
, ISL_MAP_DISJOINT
);
3320 for (i
= 0; i
+ 1 < n
; ++i
)
3321 map
= isl_map_add_basic_map(map
,
3322 isl_basic_map_less_at(isl_dim_copy(dims
), i
));
3325 map
= isl_map_add_basic_map(map
,
3326 isl_basic_map_less_or_equal_at(dims
, n
- 1));
3328 map
= isl_map_add_basic_map(map
,
3329 isl_basic_map_less_at(dims
, n
- 1));
3336 static __isl_give isl_map
*map_lex_lte(__isl_take isl_dim
*dims
, int equal
)
3340 return map_lex_lte_first(dims
, dims
->n_out
, equal
);
3343 __isl_give isl_map
*isl_map_lex_lt_first(__isl_take isl_dim
*dim
, unsigned n
)
3345 return map_lex_lte_first(dim
, n
, 0);
3348 __isl_give isl_map
*isl_map_lex_le_first(__isl_take isl_dim
*dim
, unsigned n
)
3350 return map_lex_lte_first(dim
, n
, 1);
3353 __isl_give isl_map
*isl_map_lex_lt(__isl_take isl_dim
*set_dim
)
3355 return map_lex_lte(isl_dim_map_from_set(set_dim
), 0);
3358 __isl_give isl_map
*isl_map_lex_le(__isl_take isl_dim
*set_dim
)
3360 return map_lex_lte(isl_dim_map_from_set(set_dim
), 1);
3363 static __isl_give isl_map
*map_lex_gte_first(__isl_take isl_dim
*dims
,
3364 unsigned n
, int equal
)
3366 struct isl_map
*map
;
3369 if (n
== 0 && equal
)
3370 return isl_map_universe(dims
);
3372 map
= isl_map_alloc_dim(isl_dim_copy(dims
), n
, ISL_MAP_DISJOINT
);
3374 for (i
= 0; i
+ 1 < n
; ++i
)
3375 map
= isl_map_add_basic_map(map
,
3376 isl_basic_map_more_at(isl_dim_copy(dims
), i
));
3379 map
= isl_map_add_basic_map(map
,
3380 isl_basic_map_more_or_equal_at(dims
, n
- 1));
3382 map
= isl_map_add_basic_map(map
,
3383 isl_basic_map_more_at(dims
, n
- 1));
3390 static __isl_give isl_map
*map_lex_gte(__isl_take isl_dim
*dims
, int equal
)
3394 return map_lex_gte_first(dims
, dims
->n_out
, equal
);
3397 __isl_give isl_map
*isl_map_lex_gt_first(__isl_take isl_dim
*dim
, unsigned n
)
3399 return map_lex_gte_first(dim
, n
, 0);
3402 __isl_give isl_map
*isl_map_lex_ge_first(__isl_take isl_dim
*dim
, unsigned n
)
3404 return map_lex_gte_first(dim
, n
, 1);
3407 __isl_give isl_map
*isl_map_lex_gt(__isl_take isl_dim
*set_dim
)
3409 return map_lex_gte(isl_dim_map_from_set(set_dim
), 0);
3412 __isl_give isl_map
*isl_map_lex_ge(__isl_take isl_dim
*set_dim
)
3414 return map_lex_gte(isl_dim_map_from_set(set_dim
), 1);
3417 __isl_give isl_map
*isl_set_lex_le_set(__isl_take isl_set
*set1
,
3418 __isl_take isl_set
*set2
)
3421 map
= isl_map_lex_le(isl_set_get_dim(set1
));
3422 map
= isl_map_intersect_domain(map
, set1
);
3423 map
= isl_map_intersect_range(map
, set2
);
3427 __isl_give isl_map
*isl_set_lex_lt_set(__isl_take isl_set
*set1
,
3428 __isl_take isl_set
*set2
)
3431 map
= isl_map_lex_lt(isl_set_get_dim(set1
));
3432 map
= isl_map_intersect_domain(map
, set1
);
3433 map
= isl_map_intersect_range(map
, set2
);
3437 __isl_give isl_map
*isl_set_lex_ge_set(__isl_take isl_set
*set1
,
3438 __isl_take isl_set
*set2
)
3441 map
= isl_map_lex_ge(isl_set_get_dim(set1
));
3442 map
= isl_map_intersect_domain(map
, set1
);
3443 map
= isl_map_intersect_range(map
, set2
);
3447 __isl_give isl_map
*isl_set_lex_gt_set(__isl_take isl_set
*set1
,
3448 __isl_take isl_set
*set2
)
3451 map
= isl_map_lex_gt(isl_set_get_dim(set1
));
3452 map
= isl_map_intersect_domain(map
, set1
);
3453 map
= isl_map_intersect_range(map
, set2
);
3457 __isl_give isl_map
*isl_map_lex_le_map(__isl_take isl_map
*map1
,
3458 __isl_take isl_map
*map2
)
3461 map
= isl_map_lex_le(isl_dim_range(isl_map_get_dim(map1
)));
3462 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
3463 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
3467 __isl_give isl_map
*isl_map_lex_lt_map(__isl_take isl_map
*map1
,
3468 __isl_take isl_map
*map2
)
3471 map
= isl_map_lex_lt(isl_dim_range(isl_map_get_dim(map1
)));
3472 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
3473 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
3477 __isl_give isl_map
*isl_map_lex_ge_map(__isl_take isl_map
*map1
,
3478 __isl_take isl_map
*map2
)
3481 map
= isl_map_lex_ge(isl_dim_range(isl_map_get_dim(map1
)));
3482 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
3483 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
3487 __isl_give isl_map
*isl_map_lex_gt_map(__isl_take isl_map
*map1
,
3488 __isl_take isl_map
*map2
)
3491 map
= isl_map_lex_gt(isl_dim_range(isl_map_get_dim(map1
)));
3492 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
3493 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
3497 struct isl_basic_map
*isl_basic_map_from_basic_set(
3498 struct isl_basic_set
*bset
, struct isl_dim
*dim
)
3500 struct isl_basic_map
*bmap
;
3502 bset
= isl_basic_set_cow(bset
);
3506 isl_assert(bset
->ctx
, isl_dim_compatible(bset
->dim
, dim
), goto error
);
3507 isl_dim_free(bset
->dim
);
3508 bmap
= (struct isl_basic_map
*) bset
;
3510 return isl_basic_map_finalize(bmap
);
3512 isl_basic_set_free(bset
);
3517 struct isl_basic_set
*isl_basic_set_from_basic_map(struct isl_basic_map
*bmap
)
3521 if (bmap
->dim
->n_in
== 0)
3522 return (struct isl_basic_set
*)bmap
;
3523 bmap
= isl_basic_map_cow(bmap
);
3526 bmap
->dim
= isl_dim_as_set_dim(bmap
->dim
);
3529 bmap
= isl_basic_map_finalize(bmap
);
3530 return (struct isl_basic_set
*)bmap
;
3532 isl_basic_map_free(bmap
);
3536 /* For a div d = floor(f/m), add the constraints
3539 * -(f-(n-1)) + m d >= 0
3541 * Note that the second constraint is the negation of
3545 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map
*bmap
,
3546 unsigned pos
, isl_int
*div
)
3549 unsigned total
= isl_basic_map_total_dim(bmap
);
3551 i
= isl_basic_map_alloc_inequality(bmap
);
3554 isl_seq_cpy(bmap
->ineq
[i
], div
+ 1, 1 + total
);
3555 isl_int_neg(bmap
->ineq
[i
][1 + pos
], div
[0]);
3557 j
= isl_basic_map_alloc_inequality(bmap
);
3560 isl_seq_neg(bmap
->ineq
[j
], bmap
->ineq
[i
], 1 + total
);
3561 isl_int_add(bmap
->ineq
[j
][0], bmap
->ineq
[j
][0], bmap
->ineq
[j
][1 + pos
]);
3562 isl_int_sub_ui(bmap
->ineq
[j
][0], bmap
->ineq
[j
][0], 1);
3566 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set
*bset
,
3567 unsigned pos
, isl_int
*div
)
3569 return isl_basic_map_add_div_constraints_var((isl_basic_map
*)bset
,
3573 int isl_basic_map_add_div_constraints(struct isl_basic_map
*bmap
, unsigned div
)
3575 unsigned total
= isl_basic_map_total_dim(bmap
);
3576 unsigned div_pos
= total
- bmap
->n_div
+ div
;
3578 return isl_basic_map_add_div_constraints_var(bmap
, div_pos
,
3582 struct isl_basic_set
*isl_basic_map_underlying_set(
3583 struct isl_basic_map
*bmap
)
3587 if (bmap
->dim
->nparam
== 0 && bmap
->dim
->n_in
== 0 &&
3589 !isl_dim_is_named_or_nested(bmap
->dim
, isl_dim_in
) &&
3590 !isl_dim_is_named_or_nested(bmap
->dim
, isl_dim_out
))
3591 return (struct isl_basic_set
*)bmap
;
3592 bmap
= isl_basic_map_cow(bmap
);
3595 bmap
->dim
= isl_dim_underlying(bmap
->dim
, bmap
->n_div
);
3598 bmap
->extra
-= bmap
->n_div
;
3600 bmap
= isl_basic_map_finalize(bmap
);
3601 return (struct isl_basic_set
*)bmap
;
3606 __isl_give isl_basic_set
*isl_basic_set_underlying_set(
3607 __isl_take isl_basic_set
*bset
)
3609 return isl_basic_map_underlying_set((isl_basic_map
*)bset
);
3612 struct isl_basic_map
*isl_basic_map_overlying_set(
3613 struct isl_basic_set
*bset
, struct isl_basic_map
*like
)
3615 struct isl_basic_map
*bmap
;
3616 struct isl_ctx
*ctx
;
3623 isl_assert(ctx
, bset
->n_div
== 0, goto error
);
3624 isl_assert(ctx
, isl_basic_set_n_param(bset
) == 0, goto error
);
3625 isl_assert(ctx
, bset
->dim
->n_out
== isl_basic_map_total_dim(like
),
3627 if (isl_dim_equal(bset
->dim
, like
->dim
) && like
->n_div
== 0) {
3628 isl_basic_map_free(like
);
3629 return (struct isl_basic_map
*)bset
;
3631 bset
= isl_basic_set_cow(bset
);
3634 total
= bset
->dim
->n_out
+ bset
->extra
;
3635 bmap
= (struct isl_basic_map
*)bset
;
3636 isl_dim_free(bmap
->dim
);
3637 bmap
->dim
= isl_dim_copy(like
->dim
);
3640 bmap
->n_div
= like
->n_div
;
3641 bmap
->extra
+= like
->n_div
;
3645 ltotal
= total
- bmap
->extra
+ like
->extra
;
3648 bmap
->block2
= isl_blk_extend(ctx
, bmap
->block2
,
3649 bmap
->extra
* (1 + 1 + total
));
3650 if (isl_blk_is_error(bmap
->block2
))
3652 div
= isl_realloc_array(ctx
, bmap
->div
, isl_int
*, bmap
->extra
);
3656 for (i
= 0; i
< bmap
->extra
; ++i
)
3657 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + 1 + total
);
3658 for (i
= 0; i
< like
->n_div
; ++i
) {
3659 isl_seq_cpy(bmap
->div
[i
], like
->div
[i
], 1 + 1 + ltotal
);
3660 isl_seq_clr(bmap
->div
[i
]+1+1+ltotal
, total
- ltotal
);
3662 bmap
= isl_basic_map_extend_constraints(bmap
,
3663 0, 2 * like
->n_div
);
3664 for (i
= 0; i
< like
->n_div
; ++i
) {
3665 if (isl_int_is_zero(bmap
->div
[i
][0]))
3667 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
3671 isl_basic_map_free(like
);
3672 bmap
= isl_basic_map_simplify(bmap
);
3673 bmap
= isl_basic_map_finalize(bmap
);
3676 isl_basic_map_free(like
);
3677 isl_basic_set_free(bset
);
3681 struct isl_basic_set
*isl_basic_set_from_underlying_set(
3682 struct isl_basic_set
*bset
, struct isl_basic_set
*like
)
3684 return (struct isl_basic_set
*)
3685 isl_basic_map_overlying_set(bset
, (struct isl_basic_map
*)like
);
3688 struct isl_set
*isl_set_from_underlying_set(
3689 struct isl_set
*set
, struct isl_basic_set
*like
)
3695 isl_assert(set
->ctx
, set
->dim
->n_out
== isl_basic_set_total_dim(like
),
3697 if (isl_dim_equal(set
->dim
, like
->dim
) && like
->n_div
== 0) {
3698 isl_basic_set_free(like
);
3701 set
= isl_set_cow(set
);
3704 for (i
= 0; i
< set
->n
; ++i
) {
3705 set
->p
[i
] = isl_basic_set_from_underlying_set(set
->p
[i
],
3706 isl_basic_set_copy(like
));
3710 isl_dim_free(set
->dim
);
3711 set
->dim
= isl_dim_copy(like
->dim
);
3714 isl_basic_set_free(like
);
3717 isl_basic_set_free(like
);
3722 struct isl_set
*isl_map_underlying_set(struct isl_map
*map
)
3726 map
= isl_map_cow(map
);
3729 map
->dim
= isl_dim_cow(map
->dim
);
3733 for (i
= 1; i
< map
->n
; ++i
)
3734 isl_assert(map
->ctx
, map
->p
[0]->n_div
== map
->p
[i
]->n_div
,
3736 for (i
= 0; i
< map
->n
; ++i
) {
3737 map
->p
[i
] = (struct isl_basic_map
*)
3738 isl_basic_map_underlying_set(map
->p
[i
]);
3743 map
->dim
= isl_dim_underlying(map
->dim
, 0);
3745 isl_dim_free(map
->dim
);
3746 map
->dim
= isl_dim_copy(map
->p
[0]->dim
);
3750 return (struct isl_set
*)map
;
3756 struct isl_set
*isl_set_to_underlying_set(struct isl_set
*set
)
3758 return (struct isl_set
*)isl_map_underlying_set((struct isl_map
*)set
);
3761 __isl_give isl_basic_map
*isl_basic_map_reset_dim(
3762 __isl_take isl_basic_map
*bmap
, __isl_take isl_dim
*dim
)
3764 bmap
= isl_basic_map_cow(bmap
);
3768 isl_dim_free(bmap
->dim
);
3771 bmap
= isl_basic_map_finalize(bmap
);
3775 isl_basic_map_free(bmap
);
3780 __isl_give isl_basic_set
*isl_basic_set_reset_dim(
3781 __isl_take isl_basic_set
*bset
, __isl_take isl_dim
*dim
)
3783 return (isl_basic_set
*)isl_basic_map_reset_dim((isl_basic_map
*)bset
,
3787 __isl_give isl_map
*isl_map_reset_dim(__isl_take isl_map
*map
,
3788 __isl_take isl_dim
*dim
)
3792 map
= isl_map_cow(map
);
3796 for (i
= 0; i
< map
->n
; ++i
) {
3797 map
->p
[i
] = isl_basic_map_reset_dim(map
->p
[i
],
3802 isl_dim_free(map
->dim
);
3812 __isl_give isl_set
*isl_set_reset_dim(__isl_take isl_set
*set
,
3813 __isl_take isl_dim
*dim
)
3815 return (struct isl_set
*) isl_map_reset_dim((struct isl_map
*)set
, dim
);
3818 struct isl_basic_set
*isl_basic_map_domain(struct isl_basic_map
*bmap
)
3821 struct isl_basic_set
*domain
;
3827 dim
= isl_dim_domain(isl_basic_map_get_dim(bmap
));
3829 n_in
= isl_basic_map_n_in(bmap
);
3830 n_out
= isl_basic_map_n_out(bmap
);
3831 domain
= isl_basic_set_from_basic_map(bmap
);
3832 domain
= isl_basic_set_project_out(domain
, isl_dim_set
, n_in
, n_out
);
3834 domain
= isl_basic_set_reset_dim(domain
, dim
);
3839 int isl_basic_map_may_be_set(__isl_keep isl_basic_map
*bmap
)
3843 return isl_dim_may_be_set(bmap
->dim
);
3846 struct isl_basic_set
*isl_basic_map_range(struct isl_basic_map
*bmap
)
3850 if (isl_basic_map_may_be_set(bmap
))
3852 return isl_basic_map_domain(isl_basic_map_reverse(bmap
));
3855 __isl_give isl_basic_map
*isl_basic_map_domain_map(
3856 __isl_take isl_basic_map
*bmap
)
3860 isl_basic_map
*domain
;
3861 int nparam
, n_in
, n_out
;
3864 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
3865 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
3866 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
3868 dim
= isl_dim_from_range(isl_dim_domain(isl_basic_map_get_dim(bmap
)));
3869 domain
= isl_basic_map_universe(dim
);
3871 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
3872 bmap
= isl_basic_map_apply_range(bmap
, domain
);
3873 bmap
= isl_basic_map_extend_constraints(bmap
, n_in
, 0);
3875 total
= isl_basic_map_total_dim(bmap
);
3877 for (i
= 0; i
< n_in
; ++i
) {
3878 k
= isl_basic_map_alloc_equality(bmap
);
3881 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
3882 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], -1);
3883 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ n_out
+ i
], 1);
3886 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3887 return isl_basic_map_finalize(bmap
);
3889 isl_basic_map_free(bmap
);
3893 __isl_give isl_basic_map
*isl_basic_map_range_map(
3894 __isl_take isl_basic_map
*bmap
)
3898 isl_basic_map
*range
;
3899 int nparam
, n_in
, n_out
;
3902 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
3903 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
3904 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
3906 dim
= isl_dim_from_range(isl_dim_range(isl_basic_map_get_dim(bmap
)));
3907 range
= isl_basic_map_universe(dim
);
3909 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
3910 bmap
= isl_basic_map_apply_range(bmap
, range
);
3911 bmap
= isl_basic_map_extend_constraints(bmap
, n_out
, 0);
3913 total
= isl_basic_map_total_dim(bmap
);
3915 for (i
= 0; i
< n_out
; ++i
) {
3916 k
= isl_basic_map_alloc_equality(bmap
);
3919 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
3920 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ i
], -1);
3921 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ n_out
+ i
], 1);
3924 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3925 return isl_basic_map_finalize(bmap
);
3927 isl_basic_map_free(bmap
);
3931 int isl_map_may_be_set(__isl_keep isl_map
*map
)
3935 return isl_dim_may_be_set(map
->dim
);
3938 struct isl_set
*isl_map_range(struct isl_map
*map
)
3941 struct isl_set
*set
;
3945 if (isl_map_may_be_set(map
))
3946 return (isl_set
*)map
;
3948 map
= isl_map_cow(map
);
3952 set
= (struct isl_set
*) map
;
3953 set
->dim
= isl_dim_drop_inputs(set
->dim
, 0, set
->dim
->n_in
);
3956 for (i
= 0; i
< map
->n
; ++i
) {
3957 set
->p
[i
] = isl_basic_map_range(map
->p
[i
]);
3961 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
3962 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
3969 __isl_give isl_map
*isl_map_domain_map(__isl_take isl_map
*map
)
3972 isl_dim
*domain_dim
;
3974 map
= isl_map_cow(map
);
3978 domain_dim
= isl_dim_from_range(isl_dim_domain(isl_map_get_dim(map
)));
3979 map
->dim
= isl_dim_from_domain(isl_dim_wrap(map
->dim
));
3980 map
->dim
= isl_dim_join(map
->dim
, domain_dim
);
3983 for (i
= 0; i
< map
->n
; ++i
) {
3984 map
->p
[i
] = isl_basic_map_domain_map(map
->p
[i
]);
3988 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
3989 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3996 __isl_give isl_map
*isl_map_range_map(__isl_take isl_map
*map
)
4001 map
= isl_map_cow(map
);
4005 range_dim
= isl_dim_range(isl_map_get_dim(map
));
4006 map
->dim
= isl_dim_from_domain(isl_dim_wrap(map
->dim
));
4007 map
->dim
= isl_dim_join(map
->dim
, range_dim
);
4010 for (i
= 0; i
< map
->n
; ++i
) {
4011 map
->p
[i
] = isl_basic_map_range_map(map
->p
[i
]);
4015 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
4016 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4023 struct isl_map
*isl_map_from_set(struct isl_set
*set
, struct isl_dim
*dim
)
4026 struct isl_map
*map
= NULL
;
4028 set
= isl_set_cow(set
);
4031 isl_assert(set
->ctx
, isl_dim_compatible(set
->dim
, dim
), goto error
);
4032 map
= (struct isl_map
*)set
;
4033 for (i
= 0; i
< set
->n
; ++i
) {
4034 map
->p
[i
] = isl_basic_map_from_basic_set(
4035 set
->p
[i
], isl_dim_copy(dim
));
4039 isl_dim_free(map
->dim
);
4048 __isl_give isl_basic_map
*isl_basic_map_from_domain(
4049 __isl_take isl_basic_set
*bset
)
4051 return isl_basic_map_reverse(isl_basic_map_from_range(bset
));
4054 __isl_give isl_basic_map
*isl_basic_map_from_range(
4055 __isl_take isl_basic_set
*bset
)
4057 return (isl_basic_map
*)bset
;
4060 struct isl_map
*isl_map_from_range(struct isl_set
*set
)
4062 return (struct isl_map
*)set
;
4065 __isl_give isl_map
*isl_map_from_domain(__isl_take isl_set
*set
)
4067 return isl_map_reverse(isl_map_from_range(set
));
4070 __isl_give isl_basic_map
*isl_basic_map_from_domain_and_range(
4071 __isl_take isl_basic_set
*domain
, __isl_take isl_basic_set
*range
)
4073 return isl_basic_map_apply_range(isl_basic_map_from_domain(domain
),
4074 isl_basic_map_from_range(range
));
4077 __isl_give isl_map
*isl_map_from_domain_and_range(__isl_take isl_set
*domain
,
4078 __isl_take isl_set
*range
)
4080 return isl_map_apply_range(isl_map_from_domain(domain
),
4081 isl_map_from_range(range
));
4084 struct isl_set
*isl_set_from_map(struct isl_map
*map
)
4087 struct isl_set
*set
= NULL
;
4091 map
= isl_map_cow(map
);
4094 map
->dim
= isl_dim_as_set_dim(map
->dim
);
4097 set
= (struct isl_set
*)map
;
4098 for (i
= 0; i
< map
->n
; ++i
) {
4099 set
->p
[i
] = isl_basic_set_from_basic_map(map
->p
[i
]);
4109 struct isl_map
*isl_map_alloc_dim(struct isl_dim
*dim
, int n
, unsigned flags
)
4111 struct isl_map
*map
;
4115 isl_assert(dim
->ctx
, n
>= 0, return NULL
);
4116 map
= isl_alloc(dim
->ctx
, struct isl_map
,
4117 sizeof(struct isl_map
) +
4118 (n
- 1) * sizeof(struct isl_basic_map
*));
4122 map
->ctx
= dim
->ctx
;
4123 isl_ctx_ref(map
->ctx
);
4135 struct isl_map
*isl_map_alloc(struct isl_ctx
*ctx
,
4136 unsigned nparam
, unsigned in
, unsigned out
, int n
,
4139 struct isl_map
*map
;
4140 struct isl_dim
*dims
;
4142 dims
= isl_dim_alloc(ctx
, nparam
, in
, out
);
4146 map
= isl_map_alloc_dim(dims
, n
, flags
);
4150 struct isl_basic_map
*isl_basic_map_empty(struct isl_dim
*dim
)
4152 struct isl_basic_map
*bmap
;
4153 bmap
= isl_basic_map_alloc_dim(dim
, 0, 1, 0);
4154 bmap
= isl_basic_map_set_to_empty(bmap
);
4158 struct isl_basic_set
*isl_basic_set_empty(struct isl_dim
*dim
)
4160 struct isl_basic_set
*bset
;
4161 bset
= isl_basic_set_alloc_dim(dim
, 0, 1, 0);
4162 bset
= isl_basic_set_set_to_empty(bset
);
4166 struct isl_basic_map
*isl_basic_map_empty_like(struct isl_basic_map
*model
)
4168 struct isl_basic_map
*bmap
;
4171 bmap
= isl_basic_map_alloc_dim(isl_dim_copy(model
->dim
), 0, 1, 0);
4172 bmap
= isl_basic_map_set_to_empty(bmap
);
4176 struct isl_basic_map
*isl_basic_map_empty_like_map(struct isl_map
*model
)
4178 struct isl_basic_map
*bmap
;
4181 bmap
= isl_basic_map_alloc_dim(isl_dim_copy(model
->dim
), 0, 1, 0);
4182 bmap
= isl_basic_map_set_to_empty(bmap
);
4186 struct isl_basic_set
*isl_basic_set_empty_like(struct isl_basic_set
*model
)
4188 struct isl_basic_set
*bset
;
4191 bset
= isl_basic_set_alloc_dim(isl_dim_copy(model
->dim
), 0, 1, 0);
4192 bset
= isl_basic_set_set_to_empty(bset
);
4196 struct isl_basic_map
*isl_basic_map_universe(struct isl_dim
*dim
)
4198 struct isl_basic_map
*bmap
;
4199 bmap
= isl_basic_map_alloc_dim(dim
, 0, 0, 0);
4200 bmap
= isl_basic_map_finalize(bmap
);
4204 struct isl_basic_set
*isl_basic_set_universe(struct isl_dim
*dim
)
4206 struct isl_basic_set
*bset
;
4207 bset
= isl_basic_set_alloc_dim(dim
, 0, 0, 0);
4208 bset
= isl_basic_set_finalize(bset
);
4212 __isl_give isl_basic_map
*isl_basic_map_nat_universe(__isl_take isl_dim
*dim
)
4215 unsigned total
= isl_dim_total(dim
);
4216 isl_basic_map
*bmap
;
4218 bmap
= isl_basic_map_alloc_dim(dim
, 0, 0, total
);
4219 for (i
= 0; i
< total
; ++i
) {
4220 int k
= isl_basic_map_alloc_inequality(bmap
);
4223 isl_seq_clr(bmap
->ineq
[k
], 1 + total
);
4224 isl_int_set_si(bmap
->ineq
[k
][1 + i
], 1);
4228 isl_basic_map_free(bmap
);
4232 __isl_give isl_basic_set
*isl_basic_set_nat_universe(__isl_take isl_dim
*dim
)
4234 return isl_basic_map_nat_universe(dim
);
4237 __isl_give isl_map
*isl_map_nat_universe(__isl_take isl_dim
*dim
)
4239 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim
));
4242 __isl_give isl_set
*isl_set_nat_universe(__isl_take isl_dim
*dim
)
4244 return isl_map_nat_universe(dim
);
4247 __isl_give isl_basic_map
*isl_basic_map_universe_like(
4248 __isl_keep isl_basic_map
*model
)
4252 return isl_basic_map_alloc_dim(isl_dim_copy(model
->dim
), 0, 0, 0);
4255 struct isl_basic_set
*isl_basic_set_universe_like(struct isl_basic_set
*model
)
4259 return isl_basic_set_alloc_dim(isl_dim_copy(model
->dim
), 0, 0, 0);
4262 __isl_give isl_basic_set
*isl_basic_set_universe_like_set(
4263 __isl_keep isl_set
*model
)
4267 return isl_basic_set_alloc_dim(isl_dim_copy(model
->dim
), 0, 0, 0);
4270 struct isl_map
*isl_map_empty(struct isl_dim
*dim
)
4272 return isl_map_alloc_dim(dim
, 0, ISL_MAP_DISJOINT
);
4275 struct isl_map
*isl_map_empty_like(struct isl_map
*model
)
4279 return isl_map_alloc_dim(isl_dim_copy(model
->dim
), 0, ISL_MAP_DISJOINT
);
4282 struct isl_map
*isl_map_empty_like_basic_map(struct isl_basic_map
*model
)
4286 return isl_map_alloc_dim(isl_dim_copy(model
->dim
), 0, ISL_MAP_DISJOINT
);
4289 struct isl_set
*isl_set_empty(struct isl_dim
*dim
)
4291 return isl_set_alloc_dim(dim
, 0, ISL_MAP_DISJOINT
);
4294 struct isl_set
*isl_set_empty_like(struct isl_set
*model
)
4298 return isl_set_empty(isl_dim_copy(model
->dim
));
4301 struct isl_map
*isl_map_universe(struct isl_dim
*dim
)
4303 struct isl_map
*map
;
4306 map
= isl_map_alloc_dim(isl_dim_copy(dim
), 1, ISL_MAP_DISJOINT
);
4307 map
= isl_map_add_basic_map(map
, isl_basic_map_universe(dim
));
4311 struct isl_set
*isl_set_universe(struct isl_dim
*dim
)
4313 struct isl_set
*set
;
4316 set
= isl_set_alloc_dim(isl_dim_copy(dim
), 1, ISL_MAP_DISJOINT
);
4317 set
= isl_set_add_basic_set(set
, isl_basic_set_universe(dim
));
4321 __isl_give isl_set
*isl_set_universe_like(__isl_keep isl_set
*model
)
4325 return isl_set_universe(isl_dim_copy(model
->dim
));
4328 struct isl_map
*isl_map_dup(struct isl_map
*map
)
4331 struct isl_map
*dup
;
4335 dup
= isl_map_alloc_dim(isl_dim_copy(map
->dim
), map
->n
, map
->flags
);
4336 for (i
= 0; i
< map
->n
; ++i
)
4337 dup
= isl_map_add_basic_map(dup
, isl_basic_map_copy(map
->p
[i
]));
4341 __isl_give isl_map
*isl_map_add_basic_map(__isl_take isl_map
*map
,
4342 __isl_take isl_basic_map
*bmap
)
4346 if (isl_basic_map_plain_is_empty(bmap
)) {
4347 isl_basic_map_free(bmap
);
4350 isl_assert(map
->ctx
, isl_dim_equal(map
->dim
, bmap
->dim
), goto error
);
4351 isl_assert(map
->ctx
, map
->n
< map
->size
, goto error
);
4352 map
->p
[map
->n
] = bmap
;
4354 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4360 isl_basic_map_free(bmap
);
4364 void isl_map_free(struct isl_map
*map
)
4374 isl_ctx_deref(map
->ctx
);
4375 for (i
= 0; i
< map
->n
; ++i
)
4376 isl_basic_map_free(map
->p
[i
]);
4377 isl_dim_free(map
->dim
);
4381 struct isl_map
*isl_map_extend(struct isl_map
*base
,
4382 unsigned nparam
, unsigned n_in
, unsigned n_out
)
4386 base
= isl_map_cow(base
);
4390 base
->dim
= isl_dim_extend(base
->dim
, nparam
, n_in
, n_out
);
4393 for (i
= 0; i
< base
->n
; ++i
) {
4394 base
->p
[i
] = isl_basic_map_extend_dim(base
->p
[i
],
4395 isl_dim_copy(base
->dim
), 0, 0, 0);
4405 struct isl_set
*isl_set_extend(struct isl_set
*base
,
4406 unsigned nparam
, unsigned dim
)
4408 return (struct isl_set
*)isl_map_extend((struct isl_map
*)base
,
4412 static struct isl_basic_map
*isl_basic_map_fix_pos_si(
4413 struct isl_basic_map
*bmap
, unsigned pos
, int value
)
4417 bmap
= isl_basic_map_cow(bmap
);
4418 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
4419 j
= isl_basic_map_alloc_equality(bmap
);
4422 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
4423 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
4424 isl_int_set_si(bmap
->eq
[j
][0], value
);
4425 bmap
= isl_basic_map_simplify(bmap
);
4426 return isl_basic_map_finalize(bmap
);
4428 isl_basic_map_free(bmap
);
4432 static __isl_give isl_basic_map
*isl_basic_map_fix_pos(
4433 __isl_take isl_basic_map
*bmap
, unsigned pos
, isl_int value
)
4437 bmap
= isl_basic_map_cow(bmap
);
4438 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
4439 j
= isl_basic_map_alloc_equality(bmap
);
4442 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
4443 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
4444 isl_int_set(bmap
->eq
[j
][0], value
);
4445 bmap
= isl_basic_map_simplify(bmap
);
4446 return isl_basic_map_finalize(bmap
);
4448 isl_basic_map_free(bmap
);
4452 struct isl_basic_map
*isl_basic_map_fix_si(struct isl_basic_map
*bmap
,
4453 enum isl_dim_type type
, unsigned pos
, int value
)
4457 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
4458 return isl_basic_map_fix_pos_si(bmap
,
4459 isl_basic_map_offset(bmap
, type
) + pos
, value
);
4461 isl_basic_map_free(bmap
);
4465 __isl_give isl_basic_map
*isl_basic_map_fix(__isl_take isl_basic_map
*bmap
,
4466 enum isl_dim_type type
, unsigned pos
, isl_int value
)
4470 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
4471 return isl_basic_map_fix_pos(bmap
,
4472 isl_basic_map_offset(bmap
, type
) + pos
, value
);
4474 isl_basic_map_free(bmap
);
4478 struct isl_basic_set
*isl_basic_set_fix_si(struct isl_basic_set
*bset
,
4479 enum isl_dim_type type
, unsigned pos
, int value
)
4481 return (struct isl_basic_set
*)
4482 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
4486 __isl_give isl_basic_set
*isl_basic_set_fix(__isl_take isl_basic_set
*bset
,
4487 enum isl_dim_type type
, unsigned pos
, isl_int value
)
4489 return (struct isl_basic_set
*)
4490 isl_basic_map_fix((struct isl_basic_map
*)bset
,
4494 struct isl_basic_map
*isl_basic_map_fix_input_si(struct isl_basic_map
*bmap
,
4495 unsigned input
, int value
)
4497 return isl_basic_map_fix_si(bmap
, isl_dim_in
, input
, value
);
4500 struct isl_basic_set
*isl_basic_set_fix_dim_si(struct isl_basic_set
*bset
,
4501 unsigned dim
, int value
)
4503 return (struct isl_basic_set
*)
4504 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
4505 isl_dim_set
, dim
, value
);
4508 struct isl_map
*isl_map_fix_si(struct isl_map
*map
,
4509 enum isl_dim_type type
, unsigned pos
, int value
)
4513 map
= isl_map_cow(map
);
4517 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
4518 for (i
= 0; i
< map
->n
; ++i
) {
4519 map
->p
[i
] = isl_basic_map_fix_si(map
->p
[i
], type
, pos
, value
);
4523 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4530 __isl_give isl_set
*isl_set_fix_si(__isl_take isl_set
*set
,
4531 enum isl_dim_type type
, unsigned pos
, int value
)
4533 return (struct isl_set
*)
4534 isl_map_fix_si((struct isl_map
*)set
, type
, pos
, value
);
4537 __isl_give isl_map
*isl_map_fix(__isl_take isl_map
*map
,
4538 enum isl_dim_type type
, unsigned pos
, isl_int value
)
4542 map
= isl_map_cow(map
);
4546 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
4547 for (i
= 0; i
< map
->n
; ++i
) {
4548 map
->p
[i
] = isl_basic_map_fix(map
->p
[i
], type
, pos
, value
);
4552 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4559 __isl_give isl_set
*isl_set_fix(__isl_take isl_set
*set
,
4560 enum isl_dim_type type
, unsigned pos
, isl_int value
)
4562 return (struct isl_set
*)isl_map_fix((isl_map
*)set
, type
, pos
, value
);
4565 struct isl_map
*isl_map_fix_input_si(struct isl_map
*map
,
4566 unsigned input
, int value
)
4568 return isl_map_fix_si(map
, isl_dim_in
, input
, value
);
4571 struct isl_set
*isl_set_fix_dim_si(struct isl_set
*set
, unsigned dim
, int value
)
4573 return (struct isl_set
*)
4574 isl_map_fix_si((struct isl_map
*)set
, isl_dim_set
, dim
, value
);
4577 __isl_give isl_basic_map
*isl_basic_map_lower_bound_si(
4578 __isl_take isl_basic_map
*bmap
,
4579 enum isl_dim_type type
, unsigned pos
, int value
)
4585 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
4586 pos
+= isl_basic_map_offset(bmap
, type
);
4587 bmap
= isl_basic_map_cow(bmap
);
4588 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
4589 j
= isl_basic_map_alloc_inequality(bmap
);
4592 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
4593 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
4594 isl_int_set_si(bmap
->ineq
[j
][0], -value
);
4595 bmap
= isl_basic_map_simplify(bmap
);
4596 return isl_basic_map_finalize(bmap
);
4598 isl_basic_map_free(bmap
);
4602 struct isl_basic_set
*isl_basic_set_lower_bound_dim(struct isl_basic_set
*bset
,
4603 unsigned dim
, isl_int value
)
4607 bset
= isl_basic_set_cow(bset
);
4608 bset
= isl_basic_set_extend_constraints(bset
, 0, 1);
4609 j
= isl_basic_set_alloc_inequality(bset
);
4612 isl_seq_clr(bset
->ineq
[j
], 1 + isl_basic_set_total_dim(bset
));
4613 isl_int_set_si(bset
->ineq
[j
][1 + isl_basic_set_n_param(bset
) + dim
], 1);
4614 isl_int_neg(bset
->ineq
[j
][0], value
);
4615 bset
= isl_basic_set_simplify(bset
);
4616 return isl_basic_set_finalize(bset
);
4618 isl_basic_set_free(bset
);
4622 __isl_give isl_map
*isl_map_lower_bound_si(__isl_take isl_map
*map
,
4623 enum isl_dim_type type
, unsigned pos
, int value
)
4627 map
= isl_map_cow(map
);
4631 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
4632 for (i
= 0; i
< map
->n
; ++i
) {
4633 map
->p
[i
] = isl_basic_map_lower_bound_si(map
->p
[i
],
4638 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4645 __isl_give isl_set
*isl_set_lower_bound_si(__isl_take isl_set
*set
,
4646 enum isl_dim_type type
, unsigned pos
, int value
)
4648 return (struct isl_set
*)
4649 isl_map_lower_bound_si((struct isl_map
*)set
, type
, pos
, value
);
4652 struct isl_set
*isl_set_lower_bound_dim(struct isl_set
*set
, unsigned dim
,
4657 set
= isl_set_cow(set
);
4661 isl_assert(set
->ctx
, dim
< isl_set_n_dim(set
), goto error
);
4662 for (i
= 0; i
< set
->n
; ++i
) {
4663 set
->p
[i
] = isl_basic_set_lower_bound_dim(set
->p
[i
], dim
, value
);
4673 struct isl_map
*isl_map_reverse(struct isl_map
*map
)
4677 map
= isl_map_cow(map
);
4681 map
->dim
= isl_dim_reverse(map
->dim
);
4684 for (i
= 0; i
< map
->n
; ++i
) {
4685 map
->p
[i
] = isl_basic_map_reverse(map
->p
[i
]);
4689 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4696 static struct isl_map
*isl_basic_map_partial_lexopt(
4697 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
4698 struct isl_set
**empty
, int max
)
4702 if (bmap
->ctx
->opt
->pip
== ISL_PIP_PIP
)
4703 return isl_pip_basic_map_lexopt(bmap
, dom
, empty
, max
);
4705 return isl_tab_basic_map_partial_lexopt(bmap
, dom
, empty
, max
);
4707 isl_basic_map_free(bmap
);
4708 isl_basic_set_free(dom
);
4714 struct isl_map
*isl_basic_map_partial_lexmax(
4715 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
4716 struct isl_set
**empty
)
4718 return isl_basic_map_partial_lexopt(bmap
, dom
, empty
, 1);
4721 struct isl_map
*isl_basic_map_partial_lexmin(
4722 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
4723 struct isl_set
**empty
)
4725 return isl_basic_map_partial_lexopt(bmap
, dom
, empty
, 0);
4728 struct isl_set
*isl_basic_set_partial_lexmin(
4729 struct isl_basic_set
*bset
, struct isl_basic_set
*dom
,
4730 struct isl_set
**empty
)
4732 return (struct isl_set
*)
4733 isl_basic_map_partial_lexmin((struct isl_basic_map
*)bset
,
4737 struct isl_set
*isl_basic_set_partial_lexmax(
4738 struct isl_basic_set
*bset
, struct isl_basic_set
*dom
,
4739 struct isl_set
**empty
)
4741 return (struct isl_set
*)
4742 isl_basic_map_partial_lexmax((struct isl_basic_map
*)bset
,
4746 /* Given a basic map "bmap", compute the lexicographically minimal
4747 * (or maximal) image element for each domain element in dom.
4748 * Set *empty to those elements in dom that do not have an image element.
4750 * We first make sure the basic sets in dom are disjoint and then
4751 * simply collect the results over each of the basic sets separately.
4752 * We could probably improve the efficiency a bit by moving the union
4753 * domain down into the parametric integer programming.
4755 static __isl_give isl_map
*basic_map_partial_lexopt(
4756 __isl_take isl_basic_map
*bmap
, __isl_take isl_set
*dom
,
4757 __isl_give isl_set
**empty
, int max
)
4760 struct isl_map
*res
;
4762 dom
= isl_set_make_disjoint(dom
);
4766 if (isl_set_plain_is_empty(dom
)) {
4767 res
= isl_map_empty_like_basic_map(bmap
);
4768 *empty
= isl_set_empty_like(dom
);
4770 isl_basic_map_free(bmap
);
4774 res
= isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap
),
4775 isl_basic_set_copy(dom
->p
[0]), empty
, max
);
4777 for (i
= 1; i
< dom
->n
; ++i
) {
4778 struct isl_map
*res_i
;
4779 struct isl_set
*empty_i
;
4781 res_i
= isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap
),
4782 isl_basic_set_copy(dom
->p
[i
]), &empty_i
, max
);
4784 res
= isl_map_union_disjoint(res
, res_i
);
4785 *empty
= isl_set_union_disjoint(*empty
, empty_i
);
4789 isl_basic_map_free(bmap
);
4794 isl_basic_map_free(bmap
);
4798 /* Given a map "map", compute the lexicographically minimal
4799 * (or maximal) image element for each domain element in dom.
4800 * Set *empty to those elements in dom that do not have an image element.
4802 * We first compute the lexicographically minimal or maximal element
4803 * in the first basic map. This results in a partial solution "res"
4804 * and a subset "todo" of dom that still need to be handled.
4805 * We then consider each of the remaining maps in "map" and successively
4806 * improve both "res" and "todo".
4808 * Let res^k and todo^k be the results after k steps and let i = k + 1.
4809 * Assume we are computing the lexicographical maximum.
4810 * We first compute the lexicographically maximal element in basic map i.
4811 * This results in a partial solution res_i and a subset todo_i.
4812 * Then we combine these results with those obtain for the first k basic maps
4813 * to obtain a result that is valid for the first k+1 basic maps.
4814 * In particular, the set where there is no solution is the set where
4815 * there is no solution for the first k basic maps and also no solution
4816 * for the ith basic map, i.e.,
4818 * todo^i = todo^k * todo_i
4820 * On dom(res^k) * dom(res_i), we need to pick the larger of the two
4821 * solutions, arbitrarily breaking ties in favor of res^k.
4822 * That is, when res^k(a) >= res_i(a), we pick res^k and
4823 * when res^k(a) < res_i(a), we pick res_i. (Here, ">=" and "<" denote
4824 * the lexicographic order.)
4825 * In practice, we compute
4827 * res^k * (res_i . "<=")
4831 * res_i * (res^k . "<")
4833 * Finally, we consider the symmetric difference of dom(res^k) and dom(res_i),
4834 * where only one of res^k and res_i provides a solution and we simply pick
4841 * Note that we only compute these intersections when dom(res^k) intersects
4842 * dom(res_i). Otherwise, the only effect of these intersections is to
4843 * potentially break up res^k and res_i into smaller pieces.
4844 * We want to avoid such splintering as much as possible.
4845 * In fact, an earlier implementation of this function would look for
4846 * better results in the domain of res^k and for extra results in todo^k,
4847 * but this would always result in a splintering according to todo^k,
4848 * even when the domain of basic map i is disjoint from the domains of
4849 * the previous basic maps.
4851 static __isl_give isl_map
*isl_map_partial_lexopt(
4852 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
4853 __isl_give isl_set
**empty
, int max
)
4856 struct isl_map
*res
;
4857 struct isl_set
*todo
;
4862 if (isl_map_plain_is_empty(map
)) {
4870 res
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[0]),
4871 isl_set_copy(dom
), &todo
, max
);
4873 for (i
= 1; i
< map
->n
; ++i
) {
4877 isl_dim
*dim
= isl_dim_range(isl_map_get_dim(res
));
4879 res_i
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[i
]),
4880 isl_set_copy(dom
), &todo_i
, max
);
4883 lt
= isl_map_lex_lt(isl_dim_copy(dim
));
4884 le
= isl_map_lex_le(dim
);
4886 lt
= isl_map_lex_gt(isl_dim_copy(dim
));
4887 le
= isl_map_lex_ge(dim
);
4889 lt
= isl_map_apply_range(isl_map_copy(res
), lt
);
4890 lt
= isl_map_intersect(lt
, isl_map_copy(res_i
));
4891 le
= isl_map_apply_range(isl_map_copy(res_i
), le
);
4892 le
= isl_map_intersect(le
, isl_map_copy(res
));
4894 if (!isl_map_is_empty(lt
) || !isl_map_is_empty(le
)) {
4895 res
= isl_map_intersect_domain(res
,
4896 isl_set_copy(todo_i
));
4897 res_i
= isl_map_intersect_domain(res_i
,
4898 isl_set_copy(todo
));
4901 res
= isl_map_union_disjoint(res
, res_i
);
4902 res
= isl_map_union_disjoint(res
, lt
);
4903 res
= isl_map_union_disjoint(res
, le
);
4905 todo
= isl_set_intersect(todo
, todo_i
);
4925 __isl_give isl_map
*isl_map_partial_lexmax(
4926 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
4927 __isl_give isl_set
**empty
)
4929 return isl_map_partial_lexopt(map
, dom
, empty
, 1);
4932 __isl_give isl_map
*isl_map_partial_lexmin(
4933 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
4934 __isl_give isl_set
**empty
)
4936 return isl_map_partial_lexopt(map
, dom
, empty
, 0);
4939 __isl_give isl_set
*isl_set_partial_lexmin(
4940 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
4941 __isl_give isl_set
**empty
)
4943 return (struct isl_set
*)
4944 isl_map_partial_lexmin((struct isl_map
*)set
,
4948 __isl_give isl_set
*isl_set_partial_lexmax(
4949 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
4950 __isl_give isl_set
**empty
)
4952 return (struct isl_set
*)
4953 isl_map_partial_lexmax((struct isl_map
*)set
,
4957 __isl_give isl_map
*isl_basic_map_lexopt(__isl_take isl_basic_map
*bmap
, int max
)
4959 struct isl_basic_set
*dom
= NULL
;
4960 struct isl_dim
*dom_dim
;
4964 dom_dim
= isl_dim_domain(isl_dim_copy(bmap
->dim
));
4965 dom
= isl_basic_set_universe(dom_dim
);
4966 return isl_basic_map_partial_lexopt(bmap
, dom
, NULL
, max
);
4968 isl_basic_map_free(bmap
);
4972 __isl_give isl_map
*isl_basic_map_lexmin(__isl_take isl_basic_map
*bmap
)
4974 return isl_basic_map_lexopt(bmap
, 0);
4977 __isl_give isl_map
*isl_basic_map_lexmax(__isl_take isl_basic_map
*bmap
)
4979 return isl_basic_map_lexopt(bmap
, 1);
4982 __isl_give isl_set
*isl_basic_set_lexmin(__isl_take isl_basic_set
*bset
)
4984 return (isl_set
*)isl_basic_map_lexmin((isl_basic_map
*)bset
);
4987 __isl_give isl_set
*isl_basic_set_lexmax(__isl_take isl_basic_set
*bset
)
4989 return (isl_set
*)isl_basic_map_lexmax((isl_basic_map
*)bset
);
4992 __isl_give isl_map
*isl_map_lexopt(__isl_take isl_map
*map
, int max
)
4994 struct isl_set
*dom
= NULL
;
4995 struct isl_dim
*dom_dim
;
4999 dom_dim
= isl_dim_domain(isl_dim_copy(map
->dim
));
5000 dom
= isl_set_universe(dom_dim
);
5001 return isl_map_partial_lexopt(map
, dom
, NULL
, max
);
5007 __isl_give isl_map
*isl_map_lexmin(__isl_take isl_map
*map
)
5009 return isl_map_lexopt(map
, 0);
5012 __isl_give isl_map
*isl_map_lexmax(__isl_take isl_map
*map
)
5014 return isl_map_lexopt(map
, 1);
5017 __isl_give isl_set
*isl_set_lexmin(__isl_take isl_set
*set
)
5019 return (isl_set
*)isl_map_lexmin((isl_map
*)set
);
5022 __isl_give isl_set
*isl_set_lexmax(__isl_take isl_set
*set
)
5024 return (isl_set
*)isl_map_lexmax((isl_map
*)set
);
5027 /* Construct a map that equates the two given dimensions in the given space.
5029 static __isl_give isl_map
*equate(__isl_take isl_dim
*dim
,
5030 enum isl_dim_type src_type
, int src_pos
,
5031 enum isl_dim_type dst_type
, int dst_pos
)
5033 isl_basic_map
*bmap
;
5036 bmap
= isl_basic_map_alloc_dim(dim
, 0, 1, 0);
5037 k
= isl_basic_map_alloc_equality(bmap
);
5040 isl_seq_clr(bmap
->eq
[k
], 1 + isl_basic_map_total_dim(bmap
));
5041 src_pos
+= isl_basic_map_offset(bmap
, src_type
);
5042 dst_pos
+= isl_basic_map_offset(bmap
, dst_type
);
5043 isl_int_set_si(bmap
->eq
[k
][src_pos
], 1);
5044 isl_int_set_si(bmap
->eq
[k
][dst_pos
], -1);
5046 return isl_map_from_basic_map(bmap
);
5048 isl_basic_map_free(bmap
);
5052 /* Extract the first and only affine expression from list
5053 * and then add it to *pwaff with the given dom.
5054 * This domain is known to be disjoint from other domains
5055 * because of the way isl_basic_set_foreach_lexmax works.
5057 static int update_dim_max(__isl_take isl_basic_set
*dom
,
5058 __isl_take isl_aff_list
*list
, void *user
)
5060 isl_ctx
*ctx
= isl_basic_set_get_ctx(dom
);
5062 isl_pw_aff
**pwaff
= user
;
5063 isl_pw_aff
*pwaff_i
;
5065 if (isl_aff_list_n_aff(list
) != 1)
5066 isl_die(ctx
, isl_error_internal
,
5067 "expecting single element list", goto error
);
5069 aff
= isl_aff_list_get_aff(list
, 0);
5070 pwaff_i
= isl_pw_aff_alloc(isl_set_from_basic_set(dom
), aff
);
5072 *pwaff
= isl_pw_aff_add_disjoint(*pwaff
, pwaff_i
);
5074 isl_aff_list_free(list
);
5078 isl_basic_set_free(dom
);
5079 isl_aff_list_free(list
);
5083 /* Given a one-dimensional basic set, compute the maximum of that
5084 * dimension as an isl_pw_aff.
5086 * The isl_pw_aff is constructed by having isl_basic_set_foreach_lexmax
5087 * call update_dim_max on each leaf of the result.
5089 static __isl_give isl_pw_aff
*basic_set_dim_max(__isl_keep isl_basic_set
*bset
)
5091 isl_dim
*dim
= isl_basic_set_get_dim(bset
);
5095 dim
= isl_dim_domain(isl_dim_from_range(dim
));
5096 pwaff
= isl_pw_aff_empty(dim
);
5098 r
= isl_basic_set_foreach_lexmax(bset
, &update_dim_max
, &pwaff
);
5100 return isl_pw_aff_free(pwaff
);
5105 /* Compute the maximum of the given set dimension as a function of the
5106 * parameters, but independently of the other set dimensions.
5108 * We first project the set onto the given dimension and then compute
5109 * the "lexicographic" maximum in each basic set, combining the results
5110 * using isl_pw_aff_max.
5112 __isl_give isl_pw_aff
*isl_set_dim_max(__isl_take isl_set
*set
, int pos
)
5118 map
= isl_map_from_domain(set
);
5119 map
= isl_map_add_dims(map
, isl_dim_out
, 1);
5120 map
= isl_map_intersect(map
,
5121 equate(isl_map_get_dim(map
), isl_dim_in
, pos
,
5123 set
= isl_map_range(map
);
5128 isl_dim
*dim
= isl_set_get_dim(set
);
5129 dim
= isl_dim_domain(isl_dim_from_range(dim
));
5131 return isl_pw_aff_empty(dim
);
5134 pwaff
= basic_set_dim_max(set
->p
[0]);
5135 for (i
= 1; i
< set
->n
; ++i
) {
5136 isl_pw_aff
*pwaff_i
;
5138 pwaff_i
= basic_set_dim_max(set
->p
[i
]);
5139 pwaff
= isl_pw_aff_max(pwaff
, pwaff_i
);
5147 /* Apply a preimage specified by "mat" on the parameters of "bset".
5148 * bset is assumed to have only parameters and divs.
5150 static struct isl_basic_set
*basic_set_parameter_preimage(
5151 struct isl_basic_set
*bset
, struct isl_mat
*mat
)
5158 bset
->dim
= isl_dim_cow(bset
->dim
);
5162 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
5164 isl_assert(bset
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
5166 bset
->dim
->nparam
= 0;
5167 bset
->dim
->n_out
= nparam
;
5168 bset
= isl_basic_set_preimage(bset
, mat
);
5170 bset
->dim
->nparam
= bset
->dim
->n_out
;
5171 bset
->dim
->n_out
= 0;
5176 isl_basic_set_free(bset
);
5180 /* Apply a preimage specified by "mat" on the parameters of "set".
5181 * set is assumed to have only parameters and divs.
5183 static struct isl_set
*set_parameter_preimage(
5184 struct isl_set
*set
, struct isl_mat
*mat
)
5186 struct isl_dim
*dim
= NULL
;
5192 dim
= isl_dim_copy(set
->dim
);
5193 dim
= isl_dim_cow(dim
);
5197 nparam
= isl_set_dim(set
, isl_dim_param
);
5199 isl_assert(set
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
5202 dim
->n_out
= nparam
;
5203 isl_set_reset_dim(set
, dim
);
5204 set
= isl_set_preimage(set
, mat
);
5207 dim
= isl_dim_copy(set
->dim
);
5208 dim
= isl_dim_cow(dim
);
5211 dim
->nparam
= dim
->n_out
;
5213 isl_set_reset_dim(set
, dim
);
5223 /* Intersect the basic set "bset" with the affine space specified by the
5224 * equalities in "eq".
5226 static struct isl_basic_set
*basic_set_append_equalities(
5227 struct isl_basic_set
*bset
, struct isl_mat
*eq
)
5235 bset
= isl_basic_set_extend_dim(bset
, isl_dim_copy(bset
->dim
), 0,
5240 len
= 1 + isl_dim_total(bset
->dim
) + bset
->extra
;
5241 for (i
= 0; i
< eq
->n_row
; ++i
) {
5242 k
= isl_basic_set_alloc_equality(bset
);
5245 isl_seq_cpy(bset
->eq
[k
], eq
->row
[i
], eq
->n_col
);
5246 isl_seq_clr(bset
->eq
[k
] + eq
->n_col
, len
- eq
->n_col
);
5250 bset
= isl_basic_set_gauss(bset
, NULL
);
5251 bset
= isl_basic_set_finalize(bset
);
5256 isl_basic_set_free(bset
);
5260 /* Intersect the set "set" with the affine space specified by the
5261 * equalities in "eq".
5263 static struct isl_set
*set_append_equalities(struct isl_set
*set
,
5271 for (i
= 0; i
< set
->n
; ++i
) {
5272 set
->p
[i
] = basic_set_append_equalities(set
->p
[i
],
5285 /* Project the given basic set onto its parameter domain, possibly introducing
5286 * new, explicit, existential variables in the constraints.
5287 * The input has parameters and (possibly implicit) existential variables.
5288 * The output has the same parameters, but only
5289 * explicit existentially quantified variables.
5291 * The actual projection is performed by pip, but pip doesn't seem
5292 * to like equalities very much, so we first remove the equalities
5293 * among the parameters by performing a variable compression on
5294 * the parameters. Afterward, an inverse transformation is performed
5295 * and the equalities among the parameters are inserted back in.
5297 static struct isl_set
*parameter_compute_divs(struct isl_basic_set
*bset
)
5301 struct isl_mat
*T
, *T2
;
5302 struct isl_set
*set
;
5303 unsigned nparam
, n_div
;
5305 bset
= isl_basic_set_cow(bset
);
5309 if (bset
->n_eq
== 0)
5310 return isl_basic_set_lexmin(bset
);
5312 isl_basic_set_gauss(bset
, NULL
);
5314 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
5315 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
5317 for (i
= 0, j
= n_div
- 1; i
< bset
->n_eq
&& j
>= 0; --j
) {
5318 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ j
]))
5321 if (i
== bset
->n_eq
)
5322 return isl_basic_set_lexmin(bset
);
5324 eq
= isl_mat_sub_alloc6(bset
->ctx
, bset
->eq
, i
, bset
->n_eq
- i
,
5326 eq
= isl_mat_cow(eq
);
5327 T
= isl_mat_variable_compression(isl_mat_copy(eq
), &T2
);
5328 if (T
&& T
->n_col
== 0) {
5332 bset
= isl_basic_set_set_to_empty(bset
);
5333 return isl_set_from_basic_set(bset
);
5335 bset
= basic_set_parameter_preimage(bset
, T
);
5337 set
= isl_basic_set_lexmin(bset
);
5338 set
= set_parameter_preimage(set
, T2
);
5339 set
= set_append_equalities(set
, eq
);
5343 /* Compute an explicit representation for all the existentially
5344 * quantified variables.
5345 * The input and output dimensions are first turned into parameters.
5346 * compute_divs then returns a map with the same parameters and
5347 * no input or output dimensions and the dimension specification
5348 * is reset to that of the input.
5350 static struct isl_map
*compute_divs(struct isl_basic_map
*bmap
)
5352 struct isl_basic_set
*bset
;
5353 struct isl_set
*set
;
5354 struct isl_map
*map
;
5355 struct isl_dim
*dim
, *orig_dim
= NULL
;
5360 bmap
= isl_basic_map_cow(bmap
);
5364 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5365 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5366 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5367 dim
= isl_dim_set_alloc(bmap
->ctx
, nparam
+ n_in
+ n_out
, 0);
5371 orig_dim
= bmap
->dim
;
5373 bset
= (struct isl_basic_set
*)bmap
;
5375 set
= parameter_compute_divs(bset
);
5376 map
= (struct isl_map
*)set
;
5377 map
= isl_map_reset_dim(map
, orig_dim
);
5381 isl_basic_map_free(bmap
);
5385 int isl_basic_map_divs_known(__isl_keep isl_basic_map
*bmap
)
5393 off
= isl_dim_total(bmap
->dim
);
5394 for (i
= 0; i
< bmap
->n_div
; ++i
) {
5395 if (isl_int_is_zero(bmap
->div
[i
][0]))
5397 isl_assert(bmap
->ctx
, isl_int_is_zero(bmap
->div
[i
][1+1+off
+i
]),
5403 static int map_divs_known(__isl_keep isl_map
*map
)
5410 for (i
= 0; i
< map
->n
; ++i
) {
5411 int known
= isl_basic_map_divs_known(map
->p
[i
]);
5419 /* If bmap contains any unknown divs, then compute explicit
5420 * expressions for them. However, this computation may be
5421 * quite expensive, so first try to remove divs that aren't
5424 struct isl_map
*isl_basic_map_compute_divs(struct isl_basic_map
*bmap
)
5427 struct isl_map
*map
;
5429 known
= isl_basic_map_divs_known(bmap
);
5433 return isl_map_from_basic_map(bmap
);
5435 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
5437 known
= isl_basic_map_divs_known(bmap
);
5441 return isl_map_from_basic_map(bmap
);
5443 map
= compute_divs(bmap
);
5446 isl_basic_map_free(bmap
);
5450 struct isl_map
*isl_map_compute_divs(struct isl_map
*map
)
5454 struct isl_map
*res
;
5461 known
= map_divs_known(map
);
5469 res
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[0]));
5470 for (i
= 1 ; i
< map
->n
; ++i
) {
5472 r2
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[i
]));
5473 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
5474 res
= isl_map_union_disjoint(res
, r2
);
5476 res
= isl_map_union(res
, r2
);
5483 struct isl_set
*isl_basic_set_compute_divs(struct isl_basic_set
*bset
)
5485 return (struct isl_set
*)
5486 isl_basic_map_compute_divs((struct isl_basic_map
*)bset
);
5489 struct isl_set
*isl_set_compute_divs(struct isl_set
*set
)
5491 return (struct isl_set
*)
5492 isl_map_compute_divs((struct isl_map
*)set
);
5495 struct isl_set
*isl_map_domain(struct isl_map
*map
)
5498 struct isl_set
*set
;
5503 map
= isl_map_cow(map
);
5507 set
= (struct isl_set
*)map
;
5508 set
->dim
= isl_dim_domain(set
->dim
);
5511 for (i
= 0; i
< map
->n
; ++i
) {
5512 set
->p
[i
] = isl_basic_map_domain(map
->p
[i
]);
5516 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
5517 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
5524 struct isl_map
*isl_map_union_disjoint(
5525 struct isl_map
*map1
, struct isl_map
*map2
)
5529 struct isl_map
*map
= NULL
;
5543 isl_assert(map1
->ctx
, isl_dim_equal(map1
->dim
, map2
->dim
), goto error
);
5545 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
5546 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
5547 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
5549 map
= isl_map_alloc_dim(isl_dim_copy(map1
->dim
),
5550 map1
->n
+ map2
->n
, flags
);
5553 for (i
= 0; i
< map1
->n
; ++i
) {
5554 map
= isl_map_add_basic_map(map
,
5555 isl_basic_map_copy(map1
->p
[i
]));
5559 for (i
= 0; i
< map2
->n
; ++i
) {
5560 map
= isl_map_add_basic_map(map
,
5561 isl_basic_map_copy(map2
->p
[i
]));
5575 struct isl_map
*isl_map_union(struct isl_map
*map1
, struct isl_map
*map2
)
5577 map1
= isl_map_union_disjoint(map1
, map2
);
5581 ISL_F_CLR(map1
, ISL_MAP_DISJOINT
);
5585 struct isl_set
*isl_set_union_disjoint(
5586 struct isl_set
*set1
, struct isl_set
*set2
)
5588 return (struct isl_set
*)
5589 isl_map_union_disjoint(
5590 (struct isl_map
*)set1
, (struct isl_map
*)set2
);
5593 struct isl_set
*isl_set_union(struct isl_set
*set1
, struct isl_set
*set2
)
5595 return (struct isl_set
*)
5596 isl_map_union((struct isl_map
*)set1
, (struct isl_map
*)set2
);
5599 struct isl_map
*isl_map_intersect_range(
5600 struct isl_map
*map
, struct isl_set
*set
)
5603 struct isl_map
*result
;
5609 if (!isl_dim_match(map
->dim
, isl_dim_param
, set
->dim
, isl_dim_param
))
5610 isl_die(set
->ctx
, isl_error_invalid
,
5611 "parameters don't match", goto error
);
5613 if (isl_dim_size(set
->dim
, isl_dim_set
) != 0 &&
5614 !isl_map_compatible_range(map
, set
))
5615 isl_die(set
->ctx
, isl_error_invalid
,
5616 "incompatible spaces", goto error
);
5618 if (isl_set_plain_is_universe(set
)) {
5623 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
) &&
5624 ISL_F_ISSET(set
, ISL_MAP_DISJOINT
))
5625 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
5627 result
= isl_map_alloc_dim(isl_dim_copy(map
->dim
),
5628 map
->n
* set
->n
, flags
);
5631 for (i
= 0; i
< map
->n
; ++i
)
5632 for (j
= 0; j
< set
->n
; ++j
) {
5633 result
= isl_map_add_basic_map(result
,
5634 isl_basic_map_intersect_range(
5635 isl_basic_map_copy(map
->p
[i
]),
5636 isl_basic_set_copy(set
->p
[j
])));
5649 struct isl_map
*isl_map_intersect_domain(
5650 struct isl_map
*map
, struct isl_set
*set
)
5652 return isl_map_reverse(
5653 isl_map_intersect_range(isl_map_reverse(map
), set
));
5656 struct isl_map
*isl_map_apply_domain(
5657 struct isl_map
*map1
, struct isl_map
*map2
)
5661 map1
= isl_map_reverse(map1
);
5662 map1
= isl_map_apply_range(map1
, map2
);
5663 return isl_map_reverse(map1
);
5670 struct isl_map
*isl_map_apply_range(
5671 struct isl_map
*map1
, struct isl_map
*map2
)
5673 struct isl_dim
*dim_result
;
5674 struct isl_map
*result
;
5680 dim_result
= isl_dim_join(isl_dim_copy(map1
->dim
),
5681 isl_dim_copy(map2
->dim
));
5683 result
= isl_map_alloc_dim(dim_result
, map1
->n
* map2
->n
, 0);
5686 for (i
= 0; i
< map1
->n
; ++i
)
5687 for (j
= 0; j
< map2
->n
; ++j
) {
5688 result
= isl_map_add_basic_map(result
,
5689 isl_basic_map_apply_range(
5690 isl_basic_map_copy(map1
->p
[i
]),
5691 isl_basic_map_copy(map2
->p
[j
])));
5697 if (result
&& result
->n
<= 1)
5698 ISL_F_SET(result
, ISL_MAP_DISJOINT
);
5707 * returns range - domain
5709 struct isl_basic_set
*isl_basic_map_deltas(struct isl_basic_map
*bmap
)
5711 isl_dim
*dims
, *target_dim
;
5712 struct isl_basic_set
*bset
;
5719 isl_assert(bmap
->ctx
, isl_dim_tuple_match(bmap
->dim
, isl_dim_in
,
5720 bmap
->dim
, isl_dim_out
),
5722 target_dim
= isl_dim_domain(isl_basic_map_get_dim(bmap
));
5723 dim
= isl_basic_map_n_in(bmap
);
5724 nparam
= isl_basic_map_n_param(bmap
);
5725 bset
= isl_basic_set_from_basic_map(bmap
);
5726 bset
= isl_basic_set_cow(bset
);
5727 dims
= isl_basic_set_get_dim(bset
);
5728 dims
= isl_dim_add(dims
, isl_dim_set
, dim
);
5729 bset
= isl_basic_set_extend_dim(bset
, dims
, 0, dim
, 0);
5730 bset
= isl_basic_set_swap_vars(bset
, 2*dim
);
5731 for (i
= 0; i
< dim
; ++i
) {
5732 int j
= isl_basic_map_alloc_equality(
5733 (struct isl_basic_map
*)bset
);
5736 isl_seq_clr(bset
->eq
[j
], 1 + isl_basic_set_total_dim(bset
));
5737 isl_int_set_si(bset
->eq
[j
][1+nparam
+i
], 1);
5738 isl_int_set_si(bset
->eq
[j
][1+nparam
+dim
+i
], 1);
5739 isl_int_set_si(bset
->eq
[j
][1+nparam
+2*dim
+i
], -1);
5741 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, dim
, 2*dim
);
5742 bset
= isl_basic_set_reset_dim(bset
, target_dim
);
5745 isl_basic_map_free(bmap
);
5750 * returns range - domain
5752 struct isl_set
*isl_map_deltas(struct isl_map
*map
)
5756 struct isl_set
*result
;
5761 isl_assert(map
->ctx
, isl_dim_tuple_match(map
->dim
, isl_dim_in
,
5762 map
->dim
, isl_dim_out
),
5764 dim
= isl_map_get_dim(map
);
5765 dim
= isl_dim_domain(dim
);
5766 result
= isl_set_alloc_dim(dim
, map
->n
, 0);
5769 for (i
= 0; i
< map
->n
; ++i
)
5770 result
= isl_set_add_basic_set(result
,
5771 isl_basic_map_deltas(isl_basic_map_copy(map
->p
[i
])));
5780 * returns [domain -> range] -> range - domain
5782 __isl_give isl_basic_map
*isl_basic_map_deltas_map(
5783 __isl_take isl_basic_map
*bmap
)
5787 isl_basic_map
*domain
;
5791 if (!isl_dim_tuple_match(bmap
->dim
, isl_dim_in
, bmap
->dim
, isl_dim_out
))
5792 isl_die(bmap
->ctx
, isl_error_invalid
,
5793 "domain and range don't match", goto error
);
5795 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5796 n
= isl_basic_map_dim(bmap
, isl_dim_in
);
5798 dim
= isl_dim_from_range(isl_dim_domain(isl_basic_map_get_dim(bmap
)));
5799 domain
= isl_basic_map_universe(dim
);
5801 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
5802 bmap
= isl_basic_map_apply_range(bmap
, domain
);
5803 bmap
= isl_basic_map_extend_constraints(bmap
, n
, 0);
5805 total
= isl_basic_map_total_dim(bmap
);
5807 for (i
= 0; i
< n
; ++i
) {
5808 k
= isl_basic_map_alloc_equality(bmap
);
5811 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
5812 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], 1);
5813 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ i
], -1);
5814 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ n
+ i
], 1);
5817 bmap
= isl_basic_map_gauss(bmap
, NULL
);
5818 return isl_basic_map_finalize(bmap
);
5820 isl_basic_map_free(bmap
);
5825 * returns [domain -> range] -> range - domain
5827 __isl_give isl_map
*isl_map_deltas_map(__isl_take isl_map
*map
)
5830 isl_dim
*domain_dim
;
5835 if (!isl_dim_tuple_match(map
->dim
, isl_dim_in
, map
->dim
, isl_dim_out
))
5836 isl_die(map
->ctx
, isl_error_invalid
,
5837 "domain and range don't match", goto error
);
5839 map
= isl_map_cow(map
);
5843 domain_dim
= isl_dim_from_range(isl_dim_domain(isl_map_get_dim(map
)));
5844 map
->dim
= isl_dim_from_domain(isl_dim_wrap(map
->dim
));
5845 map
->dim
= isl_dim_join(map
->dim
, domain_dim
);
5848 for (i
= 0; i
< map
->n
; ++i
) {
5849 map
->p
[i
] = isl_basic_map_deltas_map(map
->p
[i
]);
5853 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5860 static struct isl_basic_map
*basic_map_identity(struct isl_dim
*dims
)
5862 struct isl_basic_map
*bmap
;
5870 nparam
= dims
->nparam
;
5872 bmap
= isl_basic_map_alloc_dim(dims
, 0, dim
, 0);
5876 for (i
= 0; i
< dim
; ++i
) {
5877 int j
= isl_basic_map_alloc_equality(bmap
);
5880 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
5881 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
5882 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], -1);
5884 return isl_basic_map_finalize(bmap
);
5886 isl_basic_map_free(bmap
);
5890 __isl_give isl_basic_map
*isl_basic_map_identity(__isl_take isl_dim
*dim
)
5894 if (dim
->n_in
!= dim
->n_out
)
5895 isl_die(dim
->ctx
, isl_error_invalid
,
5896 "number of input and output dimensions needs to be "
5897 "the same", goto error
);
5898 return basic_map_identity(dim
);
5904 struct isl_basic_map
*isl_basic_map_identity_like(struct isl_basic_map
*model
)
5906 if (!model
|| !model
->dim
)
5908 return isl_basic_map_identity(isl_dim_copy(model
->dim
));
5911 __isl_give isl_map
*isl_map_identity(__isl_take isl_dim
*dim
)
5913 return isl_map_from_basic_map(isl_basic_map_identity(dim
));
5916 struct isl_map
*isl_map_identity_like(struct isl_map
*model
)
5918 if (!model
|| !model
->dim
)
5920 return isl_map_identity(isl_dim_copy(model
->dim
));
5923 struct isl_map
*isl_map_identity_like_basic_map(struct isl_basic_map
*model
)
5925 if (!model
|| !model
->dim
)
5927 return isl_map_identity(isl_dim_copy(model
->dim
));
5930 __isl_give isl_map
*isl_set_identity(__isl_take isl_set
*set
)
5932 isl_dim
*dim
= isl_set_get_dim(set
);
5934 id
= isl_map_identity(isl_dim_map_from_set(dim
));
5935 return isl_map_intersect_range(id
, set
);
5938 /* Construct a basic set with all set dimensions having only non-negative
5941 struct isl_basic_set
*isl_basic_set_positive_orthant(struct isl_dim
*dims
)
5946 struct isl_basic_set
*bset
;
5950 nparam
= dims
->nparam
;
5952 bset
= isl_basic_set_alloc_dim(dims
, 0, 0, dim
);
5955 for (i
= 0; i
< dim
; ++i
) {
5956 int k
= isl_basic_set_alloc_inequality(bset
);
5959 isl_seq_clr(bset
->ineq
[k
], 1 + isl_basic_set_total_dim(bset
));
5960 isl_int_set_si(bset
->ineq
[k
][1 + nparam
+ i
], 1);
5964 isl_basic_set_free(bset
);
5968 /* Construct the half-space x_pos >= 0.
5970 static __isl_give isl_basic_set
*nonneg_halfspace(__isl_take isl_dim
*dim
,
5974 isl_basic_set
*nonneg
;
5976 nonneg
= isl_basic_set_alloc_dim(dim
, 0, 0, 1);
5977 k
= isl_basic_set_alloc_inequality(nonneg
);
5980 isl_seq_clr(nonneg
->ineq
[k
], 1 + isl_basic_set_total_dim(nonneg
));
5981 isl_int_set_si(nonneg
->ineq
[k
][pos
], 1);
5983 return isl_basic_set_finalize(nonneg
);
5985 isl_basic_set_free(nonneg
);
5989 /* Construct the half-space x_pos <= -1.
5991 static __isl_give isl_basic_set
*neg_halfspace(__isl_take isl_dim
*dim
, int pos
)
5996 neg
= isl_basic_set_alloc_dim(dim
, 0, 0, 1);
5997 k
= isl_basic_set_alloc_inequality(neg
);
6000 isl_seq_clr(neg
->ineq
[k
], 1 + isl_basic_set_total_dim(neg
));
6001 isl_int_set_si(neg
->ineq
[k
][0], -1);
6002 isl_int_set_si(neg
->ineq
[k
][pos
], -1);
6004 return isl_basic_set_finalize(neg
);
6006 isl_basic_set_free(neg
);
6010 __isl_give isl_set
*isl_set_split_dims(__isl_take isl_set
*set
,
6011 enum isl_dim_type type
, unsigned first
, unsigned n
)
6014 isl_basic_set
*nonneg
;
6022 isl_assert(set
->ctx
, first
+ n
<= isl_set_dim(set
, type
), goto error
);
6024 for (i
= 0; i
< n
; ++i
) {
6025 nonneg
= nonneg_halfspace(isl_set_get_dim(set
),
6026 pos(set
->dim
, type
) + first
+ i
);
6027 neg
= neg_halfspace(isl_set_get_dim(set
),
6028 pos(set
->dim
, type
) + first
+ i
);
6030 set
= isl_set_intersect(set
, isl_basic_set_union(nonneg
, neg
));
6039 static int foreach_orthant(__isl_take isl_set
*set
, int *signs
, int first
,
6040 int len
, int (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
6047 if (isl_set_plain_is_empty(set
)) {
6052 return fn(set
, signs
, user
);
6055 half
= isl_set_from_basic_set(nonneg_halfspace(isl_set_get_dim(set
),
6057 half
= isl_set_intersect(half
, isl_set_copy(set
));
6058 if (foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
) < 0)
6062 half
= isl_set_from_basic_set(neg_halfspace(isl_set_get_dim(set
),
6064 half
= isl_set_intersect(half
, set
);
6065 return foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
);
6071 /* Call "fn" on the intersections of "set" with each of the orthants
6072 * (except for obviously empty intersections). The orthant is identified
6073 * by the signs array, with each entry having value 1 or -1 according
6074 * to the sign of the corresponding variable.
6076 int isl_set_foreach_orthant(__isl_keep isl_set
*set
,
6077 int (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
6087 if (isl_set_plain_is_empty(set
))
6090 nparam
= isl_set_dim(set
, isl_dim_param
);
6091 nvar
= isl_set_dim(set
, isl_dim_set
);
6093 signs
= isl_alloc_array(set
->ctx
, int, nparam
+ nvar
);
6095 r
= foreach_orthant(isl_set_copy(set
), signs
, 0, nparam
+ nvar
,
6103 int isl_set_is_equal(struct isl_set
*set1
, struct isl_set
*set2
)
6105 return isl_map_is_equal((struct isl_map
*)set1
, (struct isl_map
*)set2
);
6108 int isl_basic_map_is_subset(
6109 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
6112 struct isl_map
*map1
;
6113 struct isl_map
*map2
;
6115 if (!bmap1
|| !bmap2
)
6118 map1
= isl_map_from_basic_map(isl_basic_map_copy(bmap1
));
6119 map2
= isl_map_from_basic_map(isl_basic_map_copy(bmap2
));
6121 is_subset
= isl_map_is_subset(map1
, map2
);
6129 int isl_basic_map_is_equal(
6130 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
6134 if (!bmap1
|| !bmap2
)
6136 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
6139 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
6143 int isl_basic_set_is_equal(
6144 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
6146 return isl_basic_map_is_equal(
6147 (struct isl_basic_map
*)bset1
, (struct isl_basic_map
*)bset2
);
6150 int isl_map_is_empty(struct isl_map
*map
)
6157 for (i
= 0; i
< map
->n
; ++i
) {
6158 is_empty
= isl_basic_map_is_empty(map
->p
[i
]);
6167 int isl_map_plain_is_empty(__isl_keep isl_map
*map
)
6169 return map
? map
->n
== 0 : -1;
6172 int isl_map_fast_is_empty(__isl_keep isl_map
*map
)
6174 return isl_map_plain_is_empty(map
);
6177 int isl_set_plain_is_empty(struct isl_set
*set
)
6179 return set
? set
->n
== 0 : -1;
6182 int isl_set_fast_is_empty(__isl_keep isl_set
*set
)
6184 return isl_set_plain_is_empty(set
);
6187 int isl_set_is_empty(struct isl_set
*set
)
6189 return isl_map_is_empty((struct isl_map
*)set
);
6192 int isl_map_has_equal_dim(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
6197 return isl_dim_equal(map1
->dim
, map2
->dim
);
6200 int isl_set_has_equal_dim(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
6205 return isl_dim_equal(set1
->dim
, set2
->dim
);
6208 int isl_map_is_equal(struct isl_map
*map1
, struct isl_map
*map2
)
6214 is_subset
= isl_map_is_subset(map1
, map2
);
6217 is_subset
= isl_map_is_subset(map2
, map1
);
6221 int isl_basic_map_is_strict_subset(
6222 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
6226 if (!bmap1
|| !bmap2
)
6228 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
6231 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
6232 if (is_subset
== -1)
6237 int isl_map_is_strict_subset(struct isl_map
*map1
, struct isl_map
*map2
)
6243 is_subset
= isl_map_is_subset(map1
, map2
);
6246 is_subset
= isl_map_is_subset(map2
, map1
);
6247 if (is_subset
== -1)
6252 int isl_set_is_strict_subset(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
6254 return isl_map_is_strict_subset((isl_map
*)set1
, (isl_map
*)set2
);
6257 int isl_basic_map_is_universe(struct isl_basic_map
*bmap
)
6261 return bmap
->n_eq
== 0 && bmap
->n_ineq
== 0;
6264 int isl_basic_set_is_universe(struct isl_basic_set
*bset
)
6268 return bset
->n_eq
== 0 && bset
->n_ineq
== 0;
6271 int isl_map_plain_is_universe(__isl_keep isl_map
*map
)
6278 for (i
= 0; i
< map
->n
; ++i
) {
6279 int r
= isl_basic_map_is_universe(map
->p
[i
]);
6287 int isl_set_plain_is_universe(__isl_keep isl_set
*set
)
6289 return isl_map_plain_is_universe((isl_map
*) set
);
6292 int isl_set_fast_is_universe(__isl_keep isl_set
*set
)
6294 return isl_set_plain_is_universe(set
);
6297 int isl_basic_map_is_empty(struct isl_basic_map
*bmap
)
6299 struct isl_basic_set
*bset
= NULL
;
6300 struct isl_vec
*sample
= NULL
;
6307 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
6310 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
6311 struct isl_basic_map
*copy
= isl_basic_map_copy(bmap
);
6312 copy
= isl_basic_map_remove_redundancies(copy
);
6313 empty
= ISL_F_ISSET(copy
, ISL_BASIC_MAP_EMPTY
);
6314 isl_basic_map_free(copy
);
6318 total
= 1 + isl_basic_map_total_dim(bmap
);
6319 if (bmap
->sample
&& bmap
->sample
->size
== total
) {
6320 int contains
= isl_basic_map_contains(bmap
, bmap
->sample
);
6326 isl_vec_free(bmap
->sample
);
6327 bmap
->sample
= NULL
;
6328 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
6331 sample
= isl_basic_set_sample_vec(bset
);
6334 empty
= sample
->size
== 0;
6335 isl_vec_free(bmap
->sample
);
6336 bmap
->sample
= sample
;
6338 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
6343 int isl_basic_map_plain_is_empty(__isl_keep isl_basic_map
*bmap
)
6347 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
);
6350 int isl_basic_map_fast_is_empty(__isl_keep isl_basic_map
*bmap
)
6352 return isl_basic_map_plain_is_empty(bmap
);
6355 int isl_basic_set_plain_is_empty(__isl_keep isl_basic_set
*bset
)
6359 return ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
);
6362 int isl_basic_set_fast_is_empty(__isl_keep isl_basic_set
*bset
)
6364 return isl_basic_set_plain_is_empty(bset
);
6367 int isl_basic_set_is_empty(struct isl_basic_set
*bset
)
6369 return isl_basic_map_is_empty((struct isl_basic_map
*)bset
);
6372 struct isl_map
*isl_basic_map_union(
6373 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
6375 struct isl_map
*map
;
6376 if (!bmap1
|| !bmap2
)
6379 isl_assert(bmap1
->ctx
, isl_dim_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
6381 map
= isl_map_alloc_dim(isl_dim_copy(bmap1
->dim
), 2, 0);
6384 map
= isl_map_add_basic_map(map
, bmap1
);
6385 map
= isl_map_add_basic_map(map
, bmap2
);
6388 isl_basic_map_free(bmap1
);
6389 isl_basic_map_free(bmap2
);
6393 struct isl_set
*isl_basic_set_union(
6394 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
6396 return (struct isl_set
*)isl_basic_map_union(
6397 (struct isl_basic_map
*)bset1
,
6398 (struct isl_basic_map
*)bset2
);
6401 /* Order divs such that any div only depends on previous divs */
6402 struct isl_basic_map
*isl_basic_map_order_divs(struct isl_basic_map
*bmap
)
6410 off
= isl_dim_total(bmap
->dim
);
6412 for (i
= 0; i
< bmap
->n_div
; ++i
) {
6414 if (isl_int_is_zero(bmap
->div
[i
][0]))
6416 pos
= isl_seq_first_non_zero(bmap
->div
[i
]+1+1+off
+i
,
6420 isl_basic_map_swap_div(bmap
, i
, i
+ pos
);
6426 struct isl_basic_set
*isl_basic_set_order_divs(struct isl_basic_set
*bset
)
6428 return (struct isl_basic_set
*)
6429 isl_basic_map_order_divs((struct isl_basic_map
*)bset
);
6432 __isl_give isl_map
*isl_map_order_divs(__isl_take isl_map
*map
)
6439 for (i
= 0; i
< map
->n
; ++i
) {
6440 map
->p
[i
] = isl_basic_map_order_divs(map
->p
[i
]);
6451 /* Apply the expansion computed by isl_merge_divs.
6452 * The expansion itself is given by "exp" while the resulting
6453 * list of divs is given by "div".
6455 __isl_give isl_basic_set
*isl_basic_set_expand_divs(
6456 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*div
, int *exp
)
6461 bset
= isl_basic_set_cow(bset
);
6465 if (div
->n_row
< bset
->n_div
)
6466 isl_die(isl_mat_get_ctx(div
), isl_error_invalid
,
6467 "not an expansion", goto error
);
6469 bset
= isl_basic_map_extend_dim(bset
, isl_dim_copy(bset
->dim
),
6470 div
->n_row
- bset
->n_div
, 0,
6471 2 * (div
->n_row
- bset
->n_div
));
6473 n_div
= bset
->n_div
;
6474 for (i
= n_div
; i
< div
->n_row
; ++i
)
6475 if (isl_basic_set_alloc_div(bset
) < 0)
6479 for (i
= div
->n_row
- 1; i
>= 0; --i
) {
6480 if (j
>= 0 && exp
[j
] == i
) {
6482 isl_basic_map_swap_div(bset
, i
, j
);
6485 isl_seq_cpy(bset
->div
[i
], div
->row
[i
], div
->n_col
);
6486 if (isl_basic_map_add_div_constraints(bset
, i
) < 0)
6494 isl_basic_set_free(bset
);
6499 /* Look for a div in dst that corresponds to the div "div" in src.
6500 * The divs before "div" in src and dst are assumed to be the same.
6502 * Returns -1 if no corresponding div was found and the position
6503 * of the corresponding div in dst otherwise.
6505 static int find_div(struct isl_basic_map
*dst
,
6506 struct isl_basic_map
*src
, unsigned div
)
6510 unsigned total
= isl_dim_total(src
->dim
);
6512 isl_assert(dst
->ctx
, div
<= dst
->n_div
, return -1);
6513 for (i
= div
; i
< dst
->n_div
; ++i
)
6514 if (isl_seq_eq(dst
->div
[i
], src
->div
[div
], 1+1+total
+div
) &&
6515 isl_seq_first_non_zero(dst
->div
[i
]+1+1+total
+div
,
6516 dst
->n_div
- div
) == -1)
6521 struct isl_basic_map
*isl_basic_map_align_divs(
6522 struct isl_basic_map
*dst
, struct isl_basic_map
*src
)
6525 unsigned total
= isl_dim_total(src
->dim
);
6530 if (src
->n_div
== 0)
6533 for (i
= 0; i
< src
->n_div
; ++i
)
6534 isl_assert(src
->ctx
, !isl_int_is_zero(src
->div
[i
][0]), goto error
);
6536 src
= isl_basic_map_order_divs(src
);
6537 dst
= isl_basic_map_cow(dst
);
6538 dst
= isl_basic_map_extend_dim(dst
, isl_dim_copy(dst
->dim
),
6539 src
->n_div
, 0, 2 * src
->n_div
);
6542 for (i
= 0; i
< src
->n_div
; ++i
) {
6543 int j
= find_div(dst
, src
, i
);
6545 j
= isl_basic_map_alloc_div(dst
);
6548 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
+i
);
6549 isl_seq_clr(dst
->div
[j
]+1+1+total
+i
, dst
->n_div
- i
);
6550 if (isl_basic_map_add_div_constraints(dst
, j
) < 0)
6554 isl_basic_map_swap_div(dst
, i
, j
);
6558 isl_basic_map_free(dst
);
6562 struct isl_basic_set
*isl_basic_set_align_divs(
6563 struct isl_basic_set
*dst
, struct isl_basic_set
*src
)
6565 return (struct isl_basic_set
*)isl_basic_map_align_divs(
6566 (struct isl_basic_map
*)dst
, (struct isl_basic_map
*)src
);
6569 struct isl_map
*isl_map_align_divs(struct isl_map
*map
)
6577 map
= isl_map_compute_divs(map
);
6578 map
= isl_map_cow(map
);
6582 for (i
= 1; i
< map
->n
; ++i
)
6583 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], map
->p
[i
]);
6584 for (i
= 1; i
< map
->n
; ++i
)
6585 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], map
->p
[0]);
6587 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6591 struct isl_set
*isl_set_align_divs(struct isl_set
*set
)
6593 return (struct isl_set
*)isl_map_align_divs((struct isl_map
*)set
);
6596 struct isl_set
*isl_set_apply(struct isl_set
*set
, struct isl_map
*map
)
6600 isl_assert(set
->ctx
, isl_map_compatible_domain(map
, set
), goto error
);
6601 map
= isl_map_intersect_domain(map
, set
);
6602 set
= isl_map_range(map
);
6610 /* There is no need to cow as removing empty parts doesn't change
6611 * the meaning of the set.
6613 struct isl_map
*isl_map_remove_empty_parts(struct isl_map
*map
)
6620 for (i
= map
->n
-1; i
>= 0; --i
) {
6621 if (!ISL_F_ISSET(map
->p
[i
], ISL_BASIC_MAP_EMPTY
))
6623 isl_basic_map_free(map
->p
[i
]);
6624 if (i
!= map
->n
-1) {
6625 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6626 map
->p
[i
] = map
->p
[map
->n
-1];
6634 struct isl_set
*isl_set_remove_empty_parts(struct isl_set
*set
)
6636 return (struct isl_set
*)
6637 isl_map_remove_empty_parts((struct isl_map
*)set
);
6640 struct isl_basic_map
*isl_map_copy_basic_map(struct isl_map
*map
)
6642 struct isl_basic_map
*bmap
;
6643 if (!map
|| map
->n
== 0)
6645 bmap
= map
->p
[map
->n
-1];
6646 isl_assert(map
->ctx
, ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
), return NULL
);
6647 return isl_basic_map_copy(bmap
);
6650 struct isl_basic_set
*isl_set_copy_basic_set(struct isl_set
*set
)
6652 return (struct isl_basic_set
*)
6653 isl_map_copy_basic_map((struct isl_map
*)set
);
6656 __isl_give isl_map
*isl_map_drop_basic_map(__isl_take isl_map
*map
,
6657 __isl_keep isl_basic_map
*bmap
)
6663 for (i
= map
->n
-1; i
>= 0; --i
) {
6664 if (map
->p
[i
] != bmap
)
6666 map
= isl_map_cow(map
);
6669 isl_basic_map_free(map
->p
[i
]);
6670 if (i
!= map
->n
-1) {
6671 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
6672 map
->p
[i
] = map
->p
[map
->n
-1];
6683 struct isl_set
*isl_set_drop_basic_set(struct isl_set
*set
,
6684 struct isl_basic_set
*bset
)
6686 return (struct isl_set
*)isl_map_drop_basic_map((struct isl_map
*)set
,
6687 (struct isl_basic_map
*)bset
);
6690 /* Given two basic sets bset1 and bset2, compute the maximal difference
6691 * between the values of dimension pos in bset1 and those in bset2
6692 * for any common value of the parameters and dimensions preceding pos.
6694 static enum isl_lp_result
basic_set_maximal_difference_at(
6695 __isl_keep isl_basic_set
*bset1
, __isl_keep isl_basic_set
*bset2
,
6696 int pos
, isl_int
*opt
)
6698 struct isl_dim
*dims
;
6699 struct isl_basic_map
*bmap1
= NULL
;
6700 struct isl_basic_map
*bmap2
= NULL
;
6701 struct isl_ctx
*ctx
;
6702 struct isl_vec
*obj
;
6705 unsigned dim1
, dim2
;
6706 enum isl_lp_result res
;
6708 if (!bset1
|| !bset2
)
6709 return isl_lp_error
;
6711 nparam
= isl_basic_set_n_param(bset1
);
6712 dim1
= isl_basic_set_n_dim(bset1
);
6713 dim2
= isl_basic_set_n_dim(bset2
);
6714 dims
= isl_dim_alloc(bset1
->ctx
, nparam
, pos
, dim1
- pos
);
6715 bmap1
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset1
), dims
);
6716 dims
= isl_dim_alloc(bset2
->ctx
, nparam
, pos
, dim2
- pos
);
6717 bmap2
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset2
), dims
);
6718 if (!bmap1
|| !bmap2
)
6720 bmap1
= isl_basic_map_cow(bmap1
);
6721 bmap1
= isl_basic_map_extend(bmap1
, nparam
,
6722 pos
, (dim1
- pos
) + (dim2
- pos
),
6723 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
6724 bmap1
= add_constraints(bmap1
, bmap2
, 0, dim1
- pos
);
6727 total
= isl_basic_map_total_dim(bmap1
);
6729 obj
= isl_vec_alloc(ctx
, 1 + total
);
6730 isl_seq_clr(obj
->block
.data
, 1 + total
);
6731 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
], 1);
6732 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
+(dim1
-pos
)], -1);
6735 res
= isl_basic_map_solve_lp(bmap1
, 1, obj
->block
.data
, ctx
->one
,
6737 isl_basic_map_free(bmap1
);
6741 isl_basic_map_free(bmap1
);
6742 isl_basic_map_free(bmap2
);
6743 return isl_lp_error
;
6746 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
6747 * for any common value of the parameters and dimensions preceding pos
6748 * in both basic sets, the values of dimension pos in bset1 are
6749 * smaller or larger than those in bset2.
6752 * 1 if bset1 follows bset2
6753 * -1 if bset1 precedes bset2
6754 * 0 if bset1 and bset2 are incomparable
6755 * -2 if some error occurred.
6757 int isl_basic_set_compare_at(struct isl_basic_set
*bset1
,
6758 struct isl_basic_set
*bset2
, int pos
)
6761 enum isl_lp_result res
;
6766 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
6768 if (res
== isl_lp_empty
)
6770 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
6771 res
== isl_lp_unbounded
)
6773 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
6782 /* Given two basic sets bset1 and bset2, check whether
6783 * for any common value of the parameters and dimensions preceding pos
6784 * there is a value of dimension pos in bset1 that is larger
6785 * than a value of the same dimension in bset2.
6788 * 1 if there exists such a pair
6789 * 0 if there is no such pair, but there is a pair of equal values
6791 * -2 if some error occurred.
6793 int isl_basic_set_follows_at(__isl_keep isl_basic_set
*bset1
,
6794 __isl_keep isl_basic_set
*bset2
, int pos
)
6797 enum isl_lp_result res
;
6802 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
6804 if (res
== isl_lp_empty
)
6806 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
6807 res
== isl_lp_unbounded
)
6809 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
6811 else if (res
== isl_lp_ok
)
6820 /* Given two sets set1 and set2, check whether
6821 * for any common value of the parameters and dimensions preceding pos
6822 * there is a value of dimension pos in set1 that is larger
6823 * than a value of the same dimension in set2.
6826 * 1 if there exists such a pair
6827 * 0 if there is no such pair, but there is a pair of equal values
6829 * -2 if some error occurred.
6831 int isl_set_follows_at(__isl_keep isl_set
*set1
,
6832 __isl_keep isl_set
*set2
, int pos
)
6840 for (i
= 0; i
< set1
->n
; ++i
)
6841 for (j
= 0; j
< set2
->n
; ++j
) {
6843 f
= isl_basic_set_follows_at(set1
->p
[i
], set2
->p
[j
], pos
);
6844 if (f
== 1 || f
== -2)
6853 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map
*bmap
,
6854 unsigned pos
, isl_int
*val
)
6862 total
= isl_basic_map_total_dim(bmap
);
6863 for (i
= 0, d
= total
-1; i
< bmap
->n_eq
&& d
+1 > pos
; ++i
) {
6864 for (; d
+1 > pos
; --d
)
6865 if (!isl_int_is_zero(bmap
->eq
[i
][1+d
]))
6869 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1, d
) != -1)
6871 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1+d
+1, total
-d
-1) != -1)
6873 if (!isl_int_is_one(bmap
->eq
[i
][1+d
]))
6876 isl_int_neg(*val
, bmap
->eq
[i
][0]);
6882 static int isl_map_plain_has_fixed_var(__isl_keep isl_map
*map
,
6883 unsigned pos
, isl_int
*val
)
6895 return isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, val
);
6898 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, &v
);
6899 for (i
= 1; fixed
== 1 && i
< map
->n
; ++i
) {
6900 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[i
], pos
, &tmp
);
6901 if (fixed
== 1 && isl_int_ne(tmp
, v
))
6905 isl_int_set(*val
, v
);
6911 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set
*bset
,
6912 unsigned pos
, isl_int
*val
)
6914 return isl_basic_map_plain_has_fixed_var((struct isl_basic_map
*)bset
,
6918 static int isl_set_plain_has_fixed_var(__isl_keep isl_set
*set
, unsigned pos
,
6921 return isl_map_plain_has_fixed_var((struct isl_map
*)set
, pos
, val
);
6924 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map
*bmap
,
6925 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
6927 if (pos
>= isl_basic_map_dim(bmap
, type
))
6929 return isl_basic_map_plain_has_fixed_var(bmap
,
6930 isl_basic_map_offset(bmap
, type
) - 1 + pos
, val
);
6933 int isl_map_plain_is_fixed(__isl_keep isl_map
*map
,
6934 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
6936 if (pos
>= isl_map_dim(map
, type
))
6938 return isl_map_plain_has_fixed_var(map
,
6939 map_offset(map
, type
) - 1 + pos
, val
);
6942 int isl_map_fast_is_fixed(__isl_keep isl_map
*map
,
6943 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
6945 return isl_map_plain_is_fixed(map
, type
, pos
, val
);
6948 /* Check if dimension dim has fixed value and if so and if val is not NULL,
6949 * then return this fixed value in *val.
6951 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set
*bset
,
6952 unsigned dim
, isl_int
*val
)
6954 return isl_basic_set_plain_has_fixed_var(bset
,
6955 isl_basic_set_n_param(bset
) + dim
, val
);
6958 /* Check if dimension dim has fixed value and if so and if val is not NULL,
6959 * then return this fixed value in *val.
6961 int isl_set_plain_dim_is_fixed(__isl_keep isl_set
*set
,
6962 unsigned dim
, isl_int
*val
)
6964 return isl_set_plain_has_fixed_var(set
, isl_set_n_param(set
) + dim
, val
);
6967 int isl_set_fast_dim_is_fixed(__isl_keep isl_set
*set
,
6968 unsigned dim
, isl_int
*val
)
6970 return isl_set_plain_dim_is_fixed(set
, dim
, val
);
6973 /* Check if input variable in has fixed value and if so and if val is not NULL,
6974 * then return this fixed value in *val.
6976 int isl_map_plain_input_is_fixed(__isl_keep isl_map
*map
,
6977 unsigned in
, isl_int
*val
)
6979 return isl_map_plain_has_fixed_var(map
, isl_map_n_param(map
) + in
, val
);
6982 /* Check if dimension dim has an (obvious) fixed lower bound and if so
6983 * and if val is not NULL, then return this lower bound in *val.
6985 int isl_basic_set_plain_dim_has_fixed_lower_bound(
6986 __isl_keep isl_basic_set
*bset
, unsigned dim
, isl_int
*val
)
6988 int i
, i_eq
= -1, i_ineq
= -1;
6995 total
= isl_basic_set_total_dim(bset
);
6996 nparam
= isl_basic_set_n_param(bset
);
6997 for (i
= 0; i
< bset
->n_eq
; ++i
) {
6998 if (isl_int_is_zero(bset
->eq
[i
][1+nparam
+dim
]))
7004 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
7005 if (!isl_int_is_pos(bset
->ineq
[i
][1+nparam
+dim
]))
7007 if (i_eq
!= -1 || i_ineq
!= -1)
7011 if (i_eq
== -1 && i_ineq
== -1)
7013 c
= i_eq
!= -1 ? bset
->eq
[i_eq
] : bset
->ineq
[i_ineq
];
7014 /* The coefficient should always be one due to normalization. */
7015 if (!isl_int_is_one(c
[1+nparam
+dim
]))
7017 if (isl_seq_first_non_zero(c
+1, nparam
+dim
) != -1)
7019 if (isl_seq_first_non_zero(c
+1+nparam
+dim
+1,
7020 total
- nparam
- dim
- 1) != -1)
7023 isl_int_neg(*val
, c
[0]);
7027 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set
*set
,
7028 unsigned dim
, isl_int
*val
)
7040 return isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[0],
7044 fixed
= isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[0],
7046 for (i
= 1; fixed
== 1 && i
< set
->n
; ++i
) {
7047 fixed
= isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[i
],
7049 if (fixed
== 1 && isl_int_ne(tmp
, v
))
7053 isl_int_set(*val
, v
);
7064 /* uset_gist depends on constraints without existentially quantified
7065 * variables sorting first.
7067 static int qsort_constraint_cmp(const void *p1
, const void *p2
)
7069 const struct constraint
*c1
= (const struct constraint
*)p1
;
7070 const struct constraint
*c2
= (const struct constraint
*)p2
;
7072 unsigned size
= isl_min(c1
->size
, c2
->size
);
7074 l1
= isl_seq_last_non_zero(c1
->c
, size
);
7075 l2
= isl_seq_last_non_zero(c2
->c
, size
);
7080 return isl_seq_cmp(c1
->c
, c2
->c
, size
);
7083 static struct isl_basic_map
*isl_basic_map_sort_constraints(
7084 struct isl_basic_map
*bmap
)
7087 struct constraint
*c
;
7092 total
= isl_basic_map_total_dim(bmap
);
7093 c
= isl_alloc_array(bmap
->ctx
, struct constraint
, bmap
->n_ineq
);
7096 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
7098 c
[i
].c
= bmap
->ineq
[i
];
7100 qsort(c
, bmap
->n_ineq
, sizeof(struct constraint
), qsort_constraint_cmp
);
7101 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
7102 bmap
->ineq
[i
] = c
[i
].c
;
7106 isl_basic_map_free(bmap
);
7110 __isl_give isl_basic_set
*isl_basic_set_sort_constraints(
7111 __isl_take isl_basic_set
*bset
)
7113 return (struct isl_basic_set
*)isl_basic_map_sort_constraints(
7114 (struct isl_basic_map
*)bset
);
7117 struct isl_basic_map
*isl_basic_map_normalize(struct isl_basic_map
*bmap
)
7121 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED
))
7123 bmap
= isl_basic_map_remove_redundancies(bmap
);
7124 bmap
= isl_basic_map_sort_constraints(bmap
);
7125 ISL_F_SET(bmap
, ISL_BASIC_MAP_NORMALIZED
);
7129 struct isl_basic_set
*isl_basic_set_normalize(struct isl_basic_set
*bset
)
7131 return (struct isl_basic_set
*)isl_basic_map_normalize(
7132 (struct isl_basic_map
*)bset
);
7135 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map
*bmap1
,
7136 const __isl_keep isl_basic_map
*bmap2
)
7143 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) !=
7144 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_RATIONAL
))
7145 return ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) ? -1 : 1;
7146 if (isl_basic_map_n_param(bmap1
) != isl_basic_map_n_param(bmap2
))
7147 return isl_basic_map_n_param(bmap1
) - isl_basic_map_n_param(bmap2
);
7148 if (isl_basic_map_n_in(bmap1
) != isl_basic_map_n_in(bmap2
))
7149 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
7150 if (isl_basic_map_n_out(bmap1
) != isl_basic_map_n_out(bmap2
))
7151 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
7152 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
) &&
7153 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
7155 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
))
7157 if (ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
7159 if (bmap1
->n_eq
!= bmap2
->n_eq
)
7160 return bmap1
->n_eq
- bmap2
->n_eq
;
7161 if (bmap1
->n_ineq
!= bmap2
->n_ineq
)
7162 return bmap1
->n_ineq
- bmap2
->n_ineq
;
7163 if (bmap1
->n_div
!= bmap2
->n_div
)
7164 return bmap1
->n_div
- bmap2
->n_div
;
7165 total
= isl_basic_map_total_dim(bmap1
);
7166 for (i
= 0; i
< bmap1
->n_eq
; ++i
) {
7167 cmp
= isl_seq_cmp(bmap1
->eq
[i
], bmap2
->eq
[i
], 1+total
);
7171 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
7172 cmp
= isl_seq_cmp(bmap1
->ineq
[i
], bmap2
->ineq
[i
], 1+total
);
7176 for (i
= 0; i
< bmap1
->n_div
; ++i
) {
7177 cmp
= isl_seq_cmp(bmap1
->div
[i
], bmap2
->div
[i
], 1+1+total
);
7184 int isl_basic_map_plain_is_equal(__isl_keep isl_basic_map
*bmap1
,
7185 __isl_keep isl_basic_map
*bmap2
)
7187 return isl_basic_map_plain_cmp(bmap1
, bmap2
) == 0;
7190 int isl_basic_set_plain_is_equal(__isl_keep isl_basic_set
*bset1
,
7191 __isl_keep isl_basic_set
*bset2
)
7193 return isl_basic_map_plain_is_equal((isl_basic_map
*)bset1
,
7194 (isl_basic_map
*)bset2
);
7197 static int qsort_bmap_cmp(const void *p1
, const void *p2
)
7199 const struct isl_basic_map
*bmap1
= *(const struct isl_basic_map
**)p1
;
7200 const struct isl_basic_map
*bmap2
= *(const struct isl_basic_map
**)p2
;
7202 return isl_basic_map_plain_cmp(bmap1
, bmap2
);
7205 /* We normalize in place, but if anything goes wrong we need
7206 * to return NULL, so we need to make sure we don't change the
7207 * meaning of any possible other copies of map.
7209 struct isl_map
*isl_map_normalize(struct isl_map
*map
)
7212 struct isl_basic_map
*bmap
;
7216 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
))
7218 for (i
= 0; i
< map
->n
; ++i
) {
7219 bmap
= isl_basic_map_normalize(isl_basic_map_copy(map
->p
[i
]));
7222 isl_basic_map_free(map
->p
[i
]);
7225 qsort(map
->p
, map
->n
, sizeof(struct isl_basic_map
*), qsort_bmap_cmp
);
7226 ISL_F_SET(map
, ISL_MAP_NORMALIZED
);
7227 map
= isl_map_remove_empty_parts(map
);
7230 for (i
= map
->n
- 1; i
>= 1; --i
) {
7231 if (!isl_basic_map_plain_is_equal(map
->p
[i
-1], map
->p
[i
]))
7233 isl_basic_map_free(map
->p
[i
-1]);
7234 for (j
= i
; j
< map
->n
; ++j
)
7235 map
->p
[j
-1] = map
->p
[j
];
7245 struct isl_set
*isl_set_normalize(struct isl_set
*set
)
7247 return (struct isl_set
*)isl_map_normalize((struct isl_map
*)set
);
7250 int isl_map_plain_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
7260 if (!isl_dim_equal(map1
->dim
, map2
->dim
))
7263 map1
= isl_map_copy(map1
);
7264 map2
= isl_map_copy(map2
);
7265 map1
= isl_map_normalize(map1
);
7266 map2
= isl_map_normalize(map2
);
7269 equal
= map1
->n
== map2
->n
;
7270 for (i
= 0; equal
&& i
< map1
->n
; ++i
) {
7271 equal
= isl_basic_map_plain_is_equal(map1
->p
[i
], map2
->p
[i
]);
7284 int isl_map_fast_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
7286 return isl_map_plain_is_equal(map1
, map2
);
7289 int isl_set_plain_is_equal(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
7291 return isl_map_plain_is_equal((struct isl_map
*)set1
,
7292 (struct isl_map
*)set2
);
7295 int isl_set_fast_is_equal(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
7297 return isl_set_plain_is_equal(set1
, set2
);
7300 /* Return an interval that ranges from min to max (inclusive)
7302 struct isl_basic_set
*isl_basic_set_interval(struct isl_ctx
*ctx
,
7303 isl_int min
, isl_int max
)
7306 struct isl_basic_set
*bset
= NULL
;
7308 bset
= isl_basic_set_alloc(ctx
, 0, 1, 0, 0, 2);
7312 k
= isl_basic_set_alloc_inequality(bset
);
7315 isl_int_set_si(bset
->ineq
[k
][1], 1);
7316 isl_int_neg(bset
->ineq
[k
][0], min
);
7318 k
= isl_basic_set_alloc_inequality(bset
);
7321 isl_int_set_si(bset
->ineq
[k
][1], -1);
7322 isl_int_set(bset
->ineq
[k
][0], max
);
7326 isl_basic_set_free(bset
);
7330 /* Return the Cartesian product of the basic sets in list (in the given order).
7332 __isl_give isl_basic_set
*isl_basic_set_list_product(
7333 __isl_take
struct isl_basic_set_list
*list
)
7341 struct isl_basic_set
*product
= NULL
;
7345 isl_assert(list
->ctx
, list
->n
> 0, goto error
);
7346 isl_assert(list
->ctx
, list
->p
[0], goto error
);
7347 nparam
= isl_basic_set_n_param(list
->p
[0]);
7348 dim
= isl_basic_set_n_dim(list
->p
[0]);
7349 extra
= list
->p
[0]->n_div
;
7350 n_eq
= list
->p
[0]->n_eq
;
7351 n_ineq
= list
->p
[0]->n_ineq
;
7352 for (i
= 1; i
< list
->n
; ++i
) {
7353 isl_assert(list
->ctx
, list
->p
[i
], goto error
);
7354 isl_assert(list
->ctx
,
7355 nparam
== isl_basic_set_n_param(list
->p
[i
]), goto error
);
7356 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
7357 extra
+= list
->p
[i
]->n_div
;
7358 n_eq
+= list
->p
[i
]->n_eq
;
7359 n_ineq
+= list
->p
[i
]->n_ineq
;
7361 product
= isl_basic_set_alloc(list
->ctx
, nparam
, dim
, extra
,
7366 for (i
= 0; i
< list
->n
; ++i
) {
7367 isl_basic_set_add_constraints(product
,
7368 isl_basic_set_copy(list
->p
[i
]), dim
);
7369 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
7371 isl_basic_set_list_free(list
);
7374 isl_basic_set_free(product
);
7375 isl_basic_set_list_free(list
);
7379 struct isl_basic_map
*isl_basic_map_product(
7380 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
7382 struct isl_dim
*dim_result
= NULL
;
7383 struct isl_basic_map
*bmap
;
7384 unsigned in1
, in2
, out1
, out2
, nparam
, total
, pos
;
7385 struct isl_dim_map
*dim_map1
, *dim_map2
;
7387 if (!bmap1
|| !bmap2
)
7390 isl_assert(bmap1
->ctx
, isl_dim_match(bmap1
->dim
, isl_dim_param
,
7391 bmap2
->dim
, isl_dim_param
), goto error
);
7392 dim_result
= isl_dim_product(isl_dim_copy(bmap1
->dim
),
7393 isl_dim_copy(bmap2
->dim
));
7395 in1
= isl_basic_map_n_in(bmap1
);
7396 in2
= isl_basic_map_n_in(bmap2
);
7397 out1
= isl_basic_map_n_out(bmap1
);
7398 out2
= isl_basic_map_n_out(bmap2
);
7399 nparam
= isl_basic_map_n_param(bmap1
);
7401 total
= nparam
+ in1
+ in2
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
7402 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
7403 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
7404 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
7405 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
7406 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
7407 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
7408 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
7409 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
7410 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
7411 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
7413 bmap
= isl_basic_map_alloc_dim(dim_result
,
7414 bmap1
->n_div
+ bmap2
->n_div
,
7415 bmap1
->n_eq
+ bmap2
->n_eq
,
7416 bmap1
->n_ineq
+ bmap2
->n_ineq
);
7417 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
7418 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
7419 bmap
= isl_basic_map_simplify(bmap
);
7420 return isl_basic_map_finalize(bmap
);
7422 isl_basic_map_free(bmap1
);
7423 isl_basic_map_free(bmap2
);
7427 __isl_give isl_basic_map
*isl_basic_map_flat_product(
7428 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
7430 isl_basic_map
*prod
;
7432 prod
= isl_basic_map_product(bmap1
, bmap2
);
7433 prod
= isl_basic_map_flatten(prod
);
7437 __isl_give isl_basic_set
*isl_basic_set_flat_product(
7438 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
7440 return isl_basic_map_flat_product(bset1
, bset2
);
7443 __isl_give isl_basic_map
*isl_basic_map_range_product(
7444 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
7446 isl_dim
*dim_result
= NULL
;
7447 isl_basic_map
*bmap
;
7448 unsigned in
, out1
, out2
, nparam
, total
, pos
;
7449 struct isl_dim_map
*dim_map1
, *dim_map2
;
7451 if (!bmap1
|| !bmap2
)
7454 dim_result
= isl_dim_range_product(isl_dim_copy(bmap1
->dim
),
7455 isl_dim_copy(bmap2
->dim
));
7457 in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
7458 out1
= isl_basic_map_n_out(bmap1
);
7459 out2
= isl_basic_map_n_out(bmap2
);
7460 nparam
= isl_basic_map_n_param(bmap1
);
7462 total
= nparam
+ in
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
7463 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
7464 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
7465 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
7466 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
7467 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
7468 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
7469 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in
);
7470 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
7471 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
7472 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
7474 bmap
= isl_basic_map_alloc_dim(dim_result
,
7475 bmap1
->n_div
+ bmap2
->n_div
,
7476 bmap1
->n_eq
+ bmap2
->n_eq
,
7477 bmap1
->n_ineq
+ bmap2
->n_ineq
);
7478 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
7479 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
7480 bmap
= isl_basic_map_simplify(bmap
);
7481 return isl_basic_map_finalize(bmap
);
7483 isl_basic_map_free(bmap1
);
7484 isl_basic_map_free(bmap2
);
7488 __isl_give isl_basic_map
*isl_basic_map_flat_range_product(
7489 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
7491 isl_basic_map
*prod
;
7493 prod
= isl_basic_map_range_product(bmap1
, bmap2
);
7494 prod
= isl_basic_map_flatten_range(prod
);
7498 static __isl_give isl_map
*map_product(__isl_take isl_map
*map1
,
7499 __isl_take isl_map
*map2
,
7500 __isl_give isl_dim
*(*dim_product
)(__isl_take isl_dim
*left
,
7501 __isl_take isl_dim
*right
),
7502 __isl_give isl_basic_map
*(*basic_map_product
)(
7503 __isl_take isl_basic_map
*left
, __isl_take isl_basic_map
*right
))
7506 struct isl_map
*result
;
7512 isl_assert(map1
->ctx
, isl_dim_match(map1
->dim
, isl_dim_param
,
7513 map2
->dim
, isl_dim_param
), goto error
);
7515 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
7516 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
7517 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
7519 result
= isl_map_alloc_dim(dim_product(isl_dim_copy(map1
->dim
),
7520 isl_dim_copy(map2
->dim
)),
7521 map1
->n
* map2
->n
, flags
);
7524 for (i
= 0; i
< map1
->n
; ++i
)
7525 for (j
= 0; j
< map2
->n
; ++j
) {
7526 struct isl_basic_map
*part
;
7527 part
= basic_map_product(isl_basic_map_copy(map1
->p
[i
]),
7528 isl_basic_map_copy(map2
->p
[j
]));
7529 if (isl_basic_map_is_empty(part
))
7530 isl_basic_map_free(part
);
7532 result
= isl_map_add_basic_map(result
, part
);
7545 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
7547 struct isl_map
*isl_map_product(struct isl_map
*map1
, struct isl_map
*map2
)
7549 return map_product(map1
, map2
, &isl_dim_product
, &isl_basic_map_product
);
7552 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
7554 __isl_give isl_map
*isl_map_flat_product(__isl_take isl_map
*map1
,
7555 __isl_take isl_map
*map2
)
7559 prod
= isl_map_product(map1
, map2
);
7560 prod
= isl_map_flatten(prod
);
7564 /* Given two set A and B, construct its Cartesian product A x B.
7566 struct isl_set
*isl_set_product(struct isl_set
*set1
, struct isl_set
*set2
)
7568 return (struct isl_set
*)isl_map_product((struct isl_map
*)set1
,
7569 (struct isl_map
*)set2
);
7572 __isl_give isl_set
*isl_set_flat_product(__isl_take isl_set
*set1
,
7573 __isl_take isl_set
*set2
)
7575 return (isl_set
*)isl_map_flat_product((isl_map
*)set1
, (isl_map
*)set2
);
7578 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
7580 __isl_give isl_map
*isl_map_range_product(__isl_take isl_map
*map1
,
7581 __isl_take isl_map
*map2
)
7583 return map_product(map1
, map2
, &isl_dim_range_product
,
7584 &isl_basic_map_range_product
);
7587 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
7589 __isl_give isl_map
*isl_map_flat_range_product(__isl_take isl_map
*map1
,
7590 __isl_take isl_map
*map2
)
7594 prod
= isl_map_range_product(map1
, map2
);
7595 prod
= isl_map_flatten_range(prod
);
7599 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map
*bmap
)
7602 uint32_t hash
= isl_hash_init();
7607 bmap
= isl_basic_map_copy(bmap
);
7608 bmap
= isl_basic_map_normalize(bmap
);
7611 total
= isl_basic_map_total_dim(bmap
);
7612 isl_hash_byte(hash
, bmap
->n_eq
& 0xFF);
7613 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
7615 c_hash
= isl_seq_get_hash(bmap
->eq
[i
], 1 + total
);
7616 isl_hash_hash(hash
, c_hash
);
7618 isl_hash_byte(hash
, bmap
->n_ineq
& 0xFF);
7619 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
7621 c_hash
= isl_seq_get_hash(bmap
->ineq
[i
], 1 + total
);
7622 isl_hash_hash(hash
, c_hash
);
7624 isl_hash_byte(hash
, bmap
->n_div
& 0xFF);
7625 for (i
= 0; i
< bmap
->n_div
; ++i
) {
7627 if (isl_int_is_zero(bmap
->div
[i
][0]))
7629 isl_hash_byte(hash
, i
& 0xFF);
7630 c_hash
= isl_seq_get_hash(bmap
->div
[i
], 1 + 1 + total
);
7631 isl_hash_hash(hash
, c_hash
);
7633 isl_basic_map_free(bmap
);
7637 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set
*bset
)
7639 return isl_basic_map_get_hash((isl_basic_map
*)bset
);
7642 uint32_t isl_map_get_hash(__isl_keep isl_map
*map
)
7649 map
= isl_map_copy(map
);
7650 map
= isl_map_normalize(map
);
7654 hash
= isl_hash_init();
7655 for (i
= 0; i
< map
->n
; ++i
) {
7657 bmap_hash
= isl_basic_map_get_hash(map
->p
[i
]);
7658 isl_hash_hash(hash
, bmap_hash
);
7666 uint32_t isl_set_get_hash(__isl_keep isl_set
*set
)
7668 return isl_map_get_hash((isl_map
*)set
);
7671 /* Check if the value for dimension dim is completely determined
7672 * by the values of the other parameters and variables.
7673 * That is, check if dimension dim is involved in an equality.
7675 int isl_basic_set_dim_is_unique(struct isl_basic_set
*bset
, unsigned dim
)
7682 nparam
= isl_basic_set_n_param(bset
);
7683 for (i
= 0; i
< bset
->n_eq
; ++i
)
7684 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ dim
]))
7689 /* Check if the value for dimension dim is completely determined
7690 * by the values of the other parameters and variables.
7691 * That is, check if dimension dim is involved in an equality
7692 * for each of the subsets.
7694 int isl_set_dim_is_unique(struct isl_set
*set
, unsigned dim
)
7700 for (i
= 0; i
< set
->n
; ++i
) {
7702 unique
= isl_basic_set_dim_is_unique(set
->p
[i
], dim
);
7709 int isl_set_n_basic_set(__isl_keep isl_set
*set
)
7711 return set
? set
->n
: 0;
7714 int isl_map_foreach_basic_map(__isl_keep isl_map
*map
,
7715 int (*fn
)(__isl_take isl_basic_map
*bmap
, void *user
), void *user
)
7722 for (i
= 0; i
< map
->n
; ++i
)
7723 if (fn(isl_basic_map_copy(map
->p
[i
]), user
) < 0)
7729 int isl_set_foreach_basic_set(__isl_keep isl_set
*set
,
7730 int (*fn
)(__isl_take isl_basic_set
*bset
, void *user
), void *user
)
7737 for (i
= 0; i
< set
->n
; ++i
)
7738 if (fn(isl_basic_set_copy(set
->p
[i
]), user
) < 0)
7744 __isl_give isl_basic_set
*isl_basic_set_lift(__isl_take isl_basic_set
*bset
)
7746 struct isl_dim
*dim
;
7751 bset
= isl_basic_set_cow(bset
);
7755 dim
= isl_basic_set_get_dim(bset
);
7756 dim
= isl_dim_lift(dim
, bset
->n_div
);
7759 isl_dim_free(bset
->dim
);
7761 bset
->extra
-= bset
->n_div
;
7764 bset
= isl_basic_set_finalize(bset
);
7768 isl_basic_set_free(bset
);
7772 __isl_give isl_set
*isl_set_lift(__isl_take isl_set
*set
)
7775 struct isl_dim
*dim
;
7778 set
= isl_set_align_divs(set
);
7783 set
= isl_set_cow(set
);
7787 n_div
= set
->p
[0]->n_div
;
7788 dim
= isl_set_get_dim(set
);
7789 dim
= isl_dim_lift(dim
, n_div
);
7792 isl_dim_free(set
->dim
);
7795 for (i
= 0; i
< set
->n
; ++i
) {
7796 set
->p
[i
] = isl_basic_set_lift(set
->p
[i
]);
7807 __isl_give isl_map
*isl_set_lifting(__isl_take isl_set
*set
)
7809 struct isl_dim
*dim
;
7810 struct isl_basic_map
*bmap
;
7817 set
= isl_set_align_divs(set
);
7822 dim
= isl_set_get_dim(set
);
7823 if (set
->n
== 0 || set
->p
[0]->n_div
== 0) {
7825 return isl_map_identity(isl_dim_map_from_set(dim
));
7828 n_div
= set
->p
[0]->n_div
;
7829 dim
= isl_dim_map_from_set(dim
);
7830 n_param
= isl_dim_size(dim
, isl_dim_param
);
7831 n_set
= isl_dim_size(dim
, isl_dim_in
);
7832 dim
= isl_dim_extend(dim
, n_param
, n_set
, n_set
+ n_div
);
7833 bmap
= isl_basic_map_alloc_dim(dim
, 0, n_set
, 2 * n_div
);
7834 for (i
= 0; i
< n_set
; ++i
)
7835 bmap
= var_equal(bmap
, i
);
7837 total
= n_param
+ n_set
+ n_set
+ n_div
;
7838 for (i
= 0; i
< n_div
; ++i
) {
7839 k
= isl_basic_map_alloc_inequality(bmap
);
7842 isl_seq_cpy(bmap
->ineq
[k
], set
->p
[0]->div
[i
]+1, 1+n_param
);
7843 isl_seq_clr(bmap
->ineq
[k
]+1+n_param
, n_set
);
7844 isl_seq_cpy(bmap
->ineq
[k
]+1+n_param
+n_set
,
7845 set
->p
[0]->div
[i
]+1+1+n_param
, n_set
+ n_div
);
7846 isl_int_neg(bmap
->ineq
[k
][1+n_param
+n_set
+n_set
+i
],
7847 set
->p
[0]->div
[i
][0]);
7849 l
= isl_basic_map_alloc_inequality(bmap
);
7852 isl_seq_neg(bmap
->ineq
[l
], bmap
->ineq
[k
], 1 + total
);
7853 isl_int_add(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0],
7854 set
->p
[0]->div
[i
][0]);
7855 isl_int_sub_ui(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], 1);
7859 bmap
= isl_basic_map_simplify(bmap
);
7860 bmap
= isl_basic_map_finalize(bmap
);
7861 return isl_map_from_basic_map(bmap
);
7864 isl_basic_map_free(bmap
);
7868 int isl_basic_set_size(__isl_keep isl_basic_set
*bset
)
7876 dim
= isl_basic_set_total_dim(bset
);
7877 size
+= bset
->n_eq
* (1 + dim
);
7878 size
+= bset
->n_ineq
* (1 + dim
);
7879 size
+= bset
->n_div
* (2 + dim
);
7884 int isl_set_size(__isl_keep isl_set
*set
)
7892 for (i
= 0; i
< set
->n
; ++i
)
7893 size
+= isl_basic_set_size(set
->p
[i
]);
7898 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
7899 enum isl_dim_type type
, unsigned pos
)
7907 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), return -1);
7909 pos
+= isl_basic_map_offset(bmap
, type
);
7911 for (i
= 0; i
< bmap
->n_eq
; ++i
)
7912 if (!isl_int_is_zero(bmap
->eq
[i
][pos
]))
7916 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
7917 int sgn
= isl_int_sgn(bmap
->ineq
[i
][pos
]);
7924 return lower
&& upper
;
7927 int isl_map_dim_is_bounded(__isl_keep isl_map
*map
,
7928 enum isl_dim_type type
, unsigned pos
)
7935 for (i
= 0; i
< map
->n
; ++i
) {
7937 bounded
= isl_basic_map_dim_is_bounded(map
->p
[i
], type
, pos
);
7938 if (bounded
< 0 || !bounded
)
7945 /* Return 1 if the specified dim is involved in both an upper bound
7946 * and a lower bound.
7948 int isl_set_dim_is_bounded(__isl_keep isl_set
*set
,
7949 enum isl_dim_type type
, unsigned pos
)
7951 return isl_map_dim_is_bounded((isl_map
*)set
, type
, pos
);
7954 /* For each of the "n" variables starting at "first", determine
7955 * the sign of the variable and put the results in the first "n"
7956 * elements of the array "signs".
7958 * 1 means that the variable is non-negative
7959 * -1 means that the variable is non-positive
7960 * 0 means the variable attains both positive and negative values.
7962 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set
*bset
,
7963 unsigned first
, unsigned n
, int *signs
)
7965 isl_vec
*bound
= NULL
;
7966 struct isl_tab
*tab
= NULL
;
7967 struct isl_tab_undo
*snap
;
7970 if (!bset
|| !signs
)
7973 bound
= isl_vec_alloc(bset
->ctx
, 1 + isl_basic_set_total_dim(bset
));
7974 tab
= isl_tab_from_basic_set(bset
);
7978 isl_seq_clr(bound
->el
, bound
->size
);
7979 isl_int_set_si(bound
->el
[0], -1);
7981 snap
= isl_tab_snap(tab
);
7982 for (i
= 0; i
< n
; ++i
) {
7985 isl_int_set_si(bound
->el
[1 + first
+ i
], -1);
7986 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
7989 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
7990 if (isl_tab_rollback(tab
, snap
) < 0)
7998 isl_int_set_si(bound
->el
[1 + first
+ i
], 1);
7999 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
8002 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
8003 if (isl_tab_rollback(tab
, snap
) < 0)
8006 signs
[i
] = empty
? -1 : 0;
8010 isl_vec_free(bound
);
8014 isl_vec_free(bound
);
8018 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set
*bset
,
8019 enum isl_dim_type type
, unsigned first
, unsigned n
, int *signs
)
8021 if (!bset
|| !signs
)
8023 isl_assert(bset
->ctx
, first
+ n
<= isl_basic_set_dim(bset
, type
),
8026 first
+= pos(bset
->dim
, type
) - 1;
8027 return isl_basic_set_vars_get_sign(bset
, first
, n
, signs
);
8030 /* Check if the given basic map is obviously single-valued.
8031 * In particular, for each output dimension, check that there is
8032 * an equality that defines the output dimension in terms of
8033 * earlier dimensions.
8035 int isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map
*bmap
)
8045 total
= 1 + isl_basic_map_total_dim(bmap
);
8046 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
8047 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
8049 for (i
= 0; i
< n_out
; ++i
) {
8050 for (j
= 0; j
< bmap
->n_eq
; ++j
) {
8051 if (isl_int_is_zero(bmap
->eq
[j
][o_out
+ i
]))
8053 if (isl_seq_first_non_zero(bmap
->eq
[j
] + o_out
+ i
+ 1,
8054 total
- (o_out
+ i
+ 1)) == -1)
8057 if (j
>= bmap
->n_eq
)
8064 /* Check if the given map is obviously single-valued.
8066 int isl_map_plain_is_single_valued(__isl_keep isl_map
*map
)
8075 return isl_basic_map_plain_is_single_valued(map
->p
[0]);
8078 /* Check if the given map is single-valued.
8083 * and check if the result is a subset of the identity mapping.
8085 int isl_map_is_single_valued(__isl_keep isl_map
*map
)
8092 sv
= isl_map_plain_is_single_valued(map
);
8096 test
= isl_map_reverse(isl_map_copy(map
));
8097 test
= isl_map_apply_range(test
, isl_map_copy(map
));
8099 dim
= isl_dim_map_from_set(isl_dim_range(isl_map_get_dim(map
)));
8100 id
= isl_map_identity(dim
);
8102 sv
= isl_map_is_subset(test
, id
);
8110 int isl_map_is_injective(__isl_keep isl_map
*map
)
8114 map
= isl_map_copy(map
);
8115 map
= isl_map_reverse(map
);
8116 in
= isl_map_is_single_valued(map
);
8122 /* Check if the given map is obviously injective.
8124 int isl_map_plain_is_injective(__isl_keep isl_map
*map
)
8128 map
= isl_map_copy(map
);
8129 map
= isl_map_reverse(map
);
8130 in
= isl_map_plain_is_single_valued(map
);
8136 int isl_map_is_bijective(__isl_keep isl_map
*map
)
8140 sv
= isl_map_is_single_valued(map
);
8144 return isl_map_is_injective(map
);
8147 int isl_set_is_singleton(__isl_keep isl_set
*set
)
8149 return isl_map_is_single_valued((isl_map
*)set
);
8152 int isl_map_is_translation(__isl_keep isl_map
*map
)
8157 delta
= isl_map_deltas(isl_map_copy(map
));
8158 ok
= isl_set_is_singleton(delta
);
8159 isl_set_free(delta
);
8164 static int unique(isl_int
*p
, unsigned pos
, unsigned len
)
8166 if (isl_seq_first_non_zero(p
, pos
) != -1)
8168 if (isl_seq_first_non_zero(p
+ pos
+ 1, len
- pos
- 1) != -1)
8173 int isl_basic_set_is_box(__isl_keep isl_basic_set
*bset
)
8182 if (isl_basic_set_dim(bset
, isl_dim_div
) != 0)
8185 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
8186 ovar
= isl_dim_offset(bset
->dim
, isl_dim_set
);
8187 for (j
= 0; j
< nvar
; ++j
) {
8188 int lower
= 0, upper
= 0;
8189 for (i
= 0; i
< bset
->n_eq
; ++i
) {
8190 if (isl_int_is_zero(bset
->eq
[i
][1 + ovar
+ j
]))
8192 if (!unique(bset
->eq
[i
] + 1 + ovar
, j
, nvar
))
8198 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
8199 if (isl_int_is_zero(bset
->ineq
[i
][1 + ovar
+ j
]))
8201 if (!unique(bset
->ineq
[i
] + 1 + ovar
, j
, nvar
))
8203 if (isl_int_is_pos(bset
->ineq
[i
][1 + ovar
+ j
]))
8208 if (!lower
|| !upper
)
8215 int isl_set_is_box(__isl_keep isl_set
*set
)
8222 return isl_basic_set_is_box(set
->p
[0]);
8225 int isl_basic_set_is_wrapping(__isl_keep isl_basic_set
*bset
)
8230 return isl_dim_is_wrapping(bset
->dim
);
8233 int isl_set_is_wrapping(__isl_keep isl_set
*set
)
8238 return isl_dim_is_wrapping(set
->dim
);
8241 __isl_give isl_basic_set
*isl_basic_map_wrap(__isl_take isl_basic_map
*bmap
)
8243 bmap
= isl_basic_map_cow(bmap
);
8247 bmap
->dim
= isl_dim_wrap(bmap
->dim
);
8251 bmap
= isl_basic_map_finalize(bmap
);
8253 return (isl_basic_set
*)bmap
;
8255 isl_basic_map_free(bmap
);
8259 __isl_give isl_set
*isl_map_wrap(__isl_take isl_map
*map
)
8263 map
= isl_map_cow(map
);
8267 for (i
= 0; i
< map
->n
; ++i
) {
8268 map
->p
[i
] = (isl_basic_map
*)isl_basic_map_wrap(map
->p
[i
]);
8272 map
->dim
= isl_dim_wrap(map
->dim
);
8276 return (isl_set
*)map
;
8282 __isl_give isl_basic_map
*isl_basic_set_unwrap(__isl_take isl_basic_set
*bset
)
8284 bset
= isl_basic_set_cow(bset
);
8288 bset
->dim
= isl_dim_unwrap(bset
->dim
);
8292 bset
= isl_basic_set_finalize(bset
);
8294 return (isl_basic_map
*)bset
;
8296 isl_basic_set_free(bset
);
8300 __isl_give isl_map
*isl_set_unwrap(__isl_take isl_set
*set
)
8307 if (!isl_set_is_wrapping(set
))
8308 isl_die(set
->ctx
, isl_error_invalid
, "not a wrapping set",
8311 set
= isl_set_cow(set
);
8315 for (i
= 0; i
< set
->n
; ++i
) {
8316 set
->p
[i
] = (isl_basic_set
*)isl_basic_set_unwrap(set
->p
[i
]);
8321 set
->dim
= isl_dim_unwrap(set
->dim
);
8325 return (isl_map
*)set
;
8331 __isl_give isl_basic_map
*isl_basic_map_reset(__isl_take isl_basic_map
*bmap
,
8332 enum isl_dim_type type
)
8337 if (!isl_dim_is_named_or_nested(bmap
->dim
, type
))
8340 bmap
= isl_basic_map_cow(bmap
);
8344 bmap
->dim
= isl_dim_reset(bmap
->dim
, type
);
8348 bmap
= isl_basic_map_finalize(bmap
);
8352 isl_basic_map_free(bmap
);
8356 __isl_give isl_map
*isl_map_reset(__isl_take isl_map
*map
,
8357 enum isl_dim_type type
)
8364 if (!isl_dim_is_named_or_nested(map
->dim
, type
))
8367 map
= isl_map_cow(map
);
8371 for (i
= 0; i
< map
->n
; ++i
) {
8372 map
->p
[i
] = isl_basic_map_reset(map
->p
[i
], type
);
8376 map
->dim
= isl_dim_reset(map
->dim
, type
);
8386 __isl_give isl_basic_map
*isl_basic_map_flatten(__isl_take isl_basic_map
*bmap
)
8391 if (!bmap
->dim
->nested
[0] && !bmap
->dim
->nested
[1])
8394 bmap
= isl_basic_map_cow(bmap
);
8398 bmap
->dim
= isl_dim_flatten(bmap
->dim
);
8402 bmap
= isl_basic_map_finalize(bmap
);
8406 isl_basic_map_free(bmap
);
8410 __isl_give isl_basic_set
*isl_basic_set_flatten(__isl_take isl_basic_set
*bset
)
8412 return (isl_basic_set
*)isl_basic_map_flatten((isl_basic_map
*)bset
);
8415 __isl_give isl_basic_map
*isl_basic_map_flatten_range(
8416 __isl_take isl_basic_map
*bmap
)
8421 if (!bmap
->dim
->nested
[1])
8424 bmap
= isl_basic_map_cow(bmap
);
8428 bmap
->dim
= isl_dim_flatten_range(bmap
->dim
);
8432 bmap
= isl_basic_map_finalize(bmap
);
8436 isl_basic_map_free(bmap
);
8440 __isl_give isl_map
*isl_map_flatten(__isl_take isl_map
*map
)
8447 if (!map
->dim
->nested
[0] && !map
->dim
->nested
[1])
8450 map
= isl_map_cow(map
);
8454 for (i
= 0; i
< map
->n
; ++i
) {
8455 map
->p
[i
] = isl_basic_map_flatten(map
->p
[i
]);
8459 map
->dim
= isl_dim_flatten(map
->dim
);
8469 __isl_give isl_set
*isl_set_flatten(__isl_take isl_set
*set
)
8471 return (isl_set
*)isl_map_flatten((isl_map
*)set
);
8474 __isl_give isl_map
*isl_set_flatten_map(__isl_take isl_set
*set
)
8476 isl_dim
*dim
, *flat_dim
;
8479 dim
= isl_set_get_dim(set
);
8480 flat_dim
= isl_dim_flatten(isl_dim_copy(dim
));
8481 map
= isl_map_identity(isl_dim_join(isl_dim_reverse(dim
), flat_dim
));
8482 map
= isl_map_intersect_domain(map
, set
);
8487 __isl_give isl_map
*isl_map_flatten_range(__isl_take isl_map
*map
)
8494 if (!map
->dim
->nested
[1])
8497 map
= isl_map_cow(map
);
8501 for (i
= 0; i
< map
->n
; ++i
) {
8502 map
->p
[i
] = isl_basic_map_flatten_range(map
->p
[i
]);
8506 map
->dim
= isl_dim_flatten_range(map
->dim
);
8516 /* Reorder the dimensions of "bmap" according to the given dim_map
8517 * and set the dimension specification to "dim".
8519 __isl_give isl_basic_map
*isl_basic_map_realign(__isl_take isl_basic_map
*bmap
,
8520 __isl_take isl_dim
*dim
, __isl_take
struct isl_dim_map
*dim_map
)
8524 bmap
= isl_basic_map_cow(bmap
);
8525 if (!bmap
|| !dim
|| !dim_map
)
8528 res
= isl_basic_map_alloc_dim(dim
,
8529 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
8530 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
8531 res
= isl_basic_map_finalize(res
);
8535 isl_basic_map_free(bmap
);
8540 /* Reorder the dimensions of "map" according to given reordering.
8542 __isl_give isl_map
*isl_map_realign(__isl_take isl_map
*map
,
8543 __isl_take isl_reordering
*r
)
8546 struct isl_dim_map
*dim_map
;
8548 map
= isl_map_cow(map
);
8549 dim_map
= isl_dim_map_from_reordering(r
);
8550 if (!map
|| !r
|| !dim_map
)
8553 for (i
= 0; i
< map
->n
; ++i
) {
8554 struct isl_dim_map
*dim_map_i
;
8556 dim_map_i
= isl_dim_map_extend(dim_map
, map
->p
[i
]);
8558 map
->p
[i
] = isl_basic_map_realign(map
->p
[i
],
8559 isl_dim_copy(r
->dim
), dim_map_i
);
8565 map
= isl_map_reset_dim(map
, isl_dim_copy(r
->dim
));
8567 isl_reordering_free(r
);
8573 isl_reordering_free(r
);
8577 __isl_give isl_set
*isl_set_realign(__isl_take isl_set
*set
,
8578 __isl_take isl_reordering
*r
)
8580 return (isl_set
*)isl_map_realign((isl_map
*)set
, r
);
8583 __isl_give isl_map
*isl_map_align_params(__isl_take isl_map
*map
,
8584 __isl_take isl_dim
*model
)
8591 ctx
= isl_dim_get_ctx(model
);
8592 if (!isl_dim_has_named_params(model
))
8593 isl_die(ctx
, isl_error_invalid
,
8594 "model has unnamed parameters", goto error
);
8595 if (!isl_dim_has_named_params(map
->dim
))
8596 isl_die(ctx
, isl_error_invalid
,
8597 "relation has unnamed parameters", goto error
);
8598 if (!isl_dim_match(map
->dim
, isl_dim_param
, model
, isl_dim_param
)) {
8599 isl_reordering
*exp
;
8601 model
= isl_dim_drop(model
, isl_dim_in
,
8602 0, isl_dim_size(model
, isl_dim_in
));
8603 model
= isl_dim_drop(model
, isl_dim_out
,
8604 0, isl_dim_size(model
, isl_dim_out
));
8605 exp
= isl_parameter_alignment_reordering(map
->dim
, model
);
8606 exp
= isl_reordering_extend_dim(exp
, isl_map_get_dim(map
));
8607 map
= isl_map_realign(map
, exp
);
8610 isl_dim_free(model
);
8613 isl_dim_free(model
);
8618 __isl_give isl_set
*isl_set_align_params(__isl_take isl_set
*set
,
8619 __isl_take isl_dim
*model
)
8621 return isl_map_align_params(set
, model
);
8624 __isl_give isl_mat
*isl_basic_map_equalities_matrix(
8625 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
8626 enum isl_dim_type c2
, enum isl_dim_type c3
,
8627 enum isl_dim_type c4
, enum isl_dim_type c5
)
8629 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
8630 struct isl_mat
*mat
;
8636 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_eq
,
8637 isl_basic_map_total_dim(bmap
) + 1);
8640 for (i
= 0; i
< bmap
->n_eq
; ++i
)
8641 for (j
= 0, pos
= 0; j
< 5; ++j
) {
8642 int off
= isl_basic_map_offset(bmap
, c
[j
]);
8643 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
8644 isl_int_set(mat
->row
[i
][pos
],
8645 bmap
->eq
[i
][off
+ k
]);
8653 __isl_give isl_mat
*isl_basic_map_inequalities_matrix(
8654 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
8655 enum isl_dim_type c2
, enum isl_dim_type c3
,
8656 enum isl_dim_type c4
, enum isl_dim_type c5
)
8658 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
8659 struct isl_mat
*mat
;
8665 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_ineq
,
8666 isl_basic_map_total_dim(bmap
) + 1);
8669 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
8670 for (j
= 0, pos
= 0; j
< 5; ++j
) {
8671 int off
= isl_basic_map_offset(bmap
, c
[j
]);
8672 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
8673 isl_int_set(mat
->row
[i
][pos
],
8674 bmap
->ineq
[i
][off
+ k
]);
8682 __isl_give isl_basic_map
*isl_basic_map_from_constraint_matrices(
8683 __isl_take isl_dim
*dim
,
8684 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
8685 enum isl_dim_type c2
, enum isl_dim_type c3
,
8686 enum isl_dim_type c4
, enum isl_dim_type c5
)
8688 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
8689 isl_basic_map
*bmap
;
8695 if (!dim
|| !eq
|| !ineq
)
8698 if (eq
->n_col
!= ineq
->n_col
)
8699 isl_die(dim
->ctx
, isl_error_invalid
,
8700 "equalities and inequalities matrices should have "
8701 "same number of columns", goto error
);
8703 total
= 1 + isl_dim_total(dim
);
8705 if (eq
->n_col
< total
)
8706 isl_die(dim
->ctx
, isl_error_invalid
,
8707 "number of columns too small", goto error
);
8709 extra
= eq
->n_col
- total
;
8711 bmap
= isl_basic_map_alloc_dim(isl_dim_copy(dim
), extra
,
8712 eq
->n_row
, ineq
->n_row
);
8715 for (i
= 0; i
< extra
; ++i
) {
8716 k
= isl_basic_map_alloc_div(bmap
);
8719 isl_int_set_si(bmap
->div
[k
][0], 0);
8721 for (i
= 0; i
< eq
->n_row
; ++i
) {
8722 l
= isl_basic_map_alloc_equality(bmap
);
8725 for (j
= 0, pos
= 0; j
< 5; ++j
) {
8726 int off
= isl_basic_map_offset(bmap
, c
[j
]);
8727 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
8728 isl_int_set(bmap
->eq
[l
][off
+ k
],
8734 for (i
= 0; i
< ineq
->n_row
; ++i
) {
8735 l
= isl_basic_map_alloc_inequality(bmap
);
8738 for (j
= 0, pos
= 0; j
< 5; ++j
) {
8739 int off
= isl_basic_map_offset(bmap
, c
[j
]);
8740 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
8741 isl_int_set(bmap
->ineq
[l
][off
+ k
],
8760 __isl_give isl_mat
*isl_basic_set_equalities_matrix(
8761 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
8762 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
8764 return isl_basic_map_equalities_matrix((isl_basic_map
*)bset
,
8765 c1
, c2
, c3
, c4
, isl_dim_in
);
8768 __isl_give isl_mat
*isl_basic_set_inequalities_matrix(
8769 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
8770 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
8772 return isl_basic_map_inequalities_matrix((isl_basic_map
*)bset
,
8773 c1
, c2
, c3
, c4
, isl_dim_in
);
8776 __isl_give isl_basic_set
*isl_basic_set_from_constraint_matrices(
8777 __isl_take isl_dim
*dim
,
8778 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
8779 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
8781 return (isl_basic_set
*)
8782 isl_basic_map_from_constraint_matrices(dim
, eq
, ineq
,
8783 c1
, c2
, c3
, c4
, isl_dim_in
);
8786 int isl_basic_map_can_zip(__isl_keep isl_basic_map
*bmap
)
8791 return isl_dim_can_zip(bmap
->dim
);
8794 int isl_map_can_zip(__isl_keep isl_map
*map
)
8799 return isl_dim_can_zip(map
->dim
);
8802 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
8803 * (A -> C) -> (B -> D).
8805 __isl_give isl_basic_map
*isl_basic_map_zip(__isl_take isl_basic_map
*bmap
)
8814 if (!isl_basic_map_can_zip(bmap
))
8815 isl_die(bmap
->ctx
, isl_error_invalid
,
8816 "basic map cannot be zipped", goto error
);
8817 pos
= isl_basic_map_offset(bmap
, isl_dim_in
) +
8818 isl_dim_size(bmap
->dim
->nested
[0], isl_dim_in
);
8819 n1
= isl_dim_size(bmap
->dim
->nested
[0], isl_dim_out
);
8820 n2
= isl_dim_size(bmap
->dim
->nested
[1], isl_dim_in
);
8821 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
8824 bmap
->dim
= isl_dim_zip(bmap
->dim
);
8829 isl_basic_map_free(bmap
);
8833 /* Given a map (A -> B) -> (C -> D), return the corresponding map
8834 * (A -> C) -> (B -> D).
8836 __isl_give isl_map
*isl_map_zip(__isl_take isl_map
*map
)
8843 if (!isl_map_can_zip(map
))
8844 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be zipped",
8847 map
= isl_map_cow(map
);
8851 for (i
= 0; i
< map
->n
; ++i
) {
8852 map
->p
[i
] = isl_basic_map_zip(map
->p
[i
]);
8857 map
->dim
= isl_dim_zip(map
->dim
);
8867 /* Construct a basic map mapping the domain of the affine expression
8868 * to a one-dimensional range prescribed by the affine expression.
8870 __isl_give isl_basic_map
*isl_basic_map_from_aff(__isl_take isl_aff
*aff
)
8874 isl_local_space
*ls
;
8875 isl_basic_map
*bmap
;
8880 ls
= isl_aff_get_local_space(aff
);
8881 ls
= isl_local_space_from_domain(ls
);
8882 ls
= isl_local_space_add_dims(ls
, isl_dim_out
, 1);
8883 bmap
= isl_basic_map_from_local_space(ls
);
8884 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
8885 k
= isl_basic_map_alloc_equality(bmap
);
8889 pos
= isl_basic_map_offset(bmap
, isl_dim_out
);
8890 isl_seq_cpy(bmap
->eq
[k
], aff
->v
->el
+ 1, pos
);
8891 isl_int_neg(bmap
->eq
[k
][pos
], aff
->v
->el
[0]);
8892 isl_seq_cpy(bmap
->eq
[k
] + pos
+ 1, aff
->v
->el
+ 1 + pos
,
8893 aff
->v
->size
- (pos
+ 1));
8896 bmap
= isl_basic_map_finalize(bmap
);
8900 isl_basic_map_free(bmap
);
8904 /* Construct a basic map mapping a domain in the given space to
8905 * to an n-dimensional range, with n the number of elements in the list,
8906 * where each coordinate in the range is prescribed by the
8907 * corresponding affine expression.
8908 * The domains of all affine expressions in the list are assumed to match
8911 __isl_give isl_basic_map
*isl_basic_map_from_aff_list(
8912 __isl_take isl_dim
*domain_dim
, __isl_take isl_aff_list
*list
)
8916 isl_basic_map
*bmap
;
8921 dim
= isl_dim_from_domain(domain_dim
);
8922 bmap
= isl_basic_map_universe(dim
);
8924 for (i
= 0; i
< list
->n
; ++i
) {
8926 isl_basic_map
*bmap_i
;
8928 aff
= isl_aff_copy(list
->p
[i
]);
8929 bmap_i
= isl_basic_map_from_aff(aff
);
8931 bmap
= isl_basic_map_flat_range_product(bmap
, bmap_i
);
8934 isl_aff_list_free(list
);