2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012-2013 Ecole Normale Superieure
6 * Use of this software is governed by the MIT license
8 * Written by Sven Verdoolaege, K.U.Leuven, Departement
9 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
10 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
11 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
12 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
16 #include <isl_ctx_private.h>
17 #include <isl_map_private.h>
19 #include "isl_space_private.h"
20 #include "isl_equalities.h"
21 #include <isl_lp_private.h>
25 #include "isl_map_piplib.h"
26 #include <isl_reordering.h>
27 #include "isl_sample.h"
30 #include <isl_mat_private.h>
31 #include <isl_vec_private.h>
32 #include <isl_dim_map.h>
33 #include <isl_local_space_private.h>
34 #include <isl_aff_private.h>
35 #include <isl_options_private.h>
36 #include <isl_morph.h>
37 #include <isl_val_private.h>
38 #include <isl/deprecated/map_int.h>
39 #include <isl/deprecated/set_int.h>
41 static unsigned n(__isl_keep isl_space
*dim
, enum isl_dim_type type
)
44 case isl_dim_param
: return dim
->nparam
;
45 case isl_dim_in
: return dim
->n_in
;
46 case isl_dim_out
: return dim
->n_out
;
47 case isl_dim_all
: return dim
->nparam
+ dim
->n_in
+ dim
->n_out
;
52 static unsigned pos(__isl_keep isl_space
*dim
, enum isl_dim_type type
)
55 case isl_dim_param
: return 1;
56 case isl_dim_in
: return 1 + dim
->nparam
;
57 case isl_dim_out
: return 1 + dim
->nparam
+ dim
->n_in
;
62 unsigned isl_basic_map_dim(__isl_keep isl_basic_map
*bmap
,
63 enum isl_dim_type type
)
68 case isl_dim_cst
: return 1;
71 case isl_dim_out
: return isl_space_dim(bmap
->dim
, type
);
72 case isl_dim_div
: return bmap
->n_div
;
73 case isl_dim_all
: return isl_basic_map_total_dim(bmap
);
78 unsigned isl_map_dim(__isl_keep isl_map
*map
, enum isl_dim_type type
)
80 return map
? n(map
->dim
, type
) : 0;
83 unsigned isl_set_dim(__isl_keep isl_set
*set
, enum isl_dim_type type
)
85 return set
? n(set
->dim
, type
) : 0;
88 unsigned isl_basic_map_offset(struct isl_basic_map
*bmap
,
89 enum isl_dim_type type
)
91 isl_space
*dim
= bmap
->dim
;
93 case isl_dim_cst
: return 0;
94 case isl_dim_param
: return 1;
95 case isl_dim_in
: return 1 + dim
->nparam
;
96 case isl_dim_out
: return 1 + dim
->nparam
+ dim
->n_in
;
97 case isl_dim_div
: return 1 + dim
->nparam
+ dim
->n_in
+ dim
->n_out
;
102 unsigned isl_basic_set_offset(struct isl_basic_set
*bset
,
103 enum isl_dim_type type
)
105 return isl_basic_map_offset(bset
, type
);
108 static unsigned map_offset(struct isl_map
*map
, enum isl_dim_type type
)
110 return pos(map
->dim
, type
);
113 unsigned isl_basic_set_dim(__isl_keep isl_basic_set
*bset
,
114 enum isl_dim_type type
)
116 return isl_basic_map_dim(bset
, type
);
119 unsigned isl_basic_set_n_dim(__isl_keep isl_basic_set
*bset
)
121 return isl_basic_set_dim(bset
, isl_dim_set
);
124 unsigned isl_basic_set_n_param(__isl_keep isl_basic_set
*bset
)
126 return isl_basic_set_dim(bset
, isl_dim_param
);
129 unsigned isl_basic_set_total_dim(const struct isl_basic_set
*bset
)
133 return isl_space_dim(bset
->dim
, isl_dim_all
) + bset
->n_div
;
136 unsigned isl_set_n_dim(__isl_keep isl_set
*set
)
138 return isl_set_dim(set
, isl_dim_set
);
141 unsigned isl_set_n_param(__isl_keep isl_set
*set
)
143 return isl_set_dim(set
, isl_dim_param
);
146 unsigned isl_basic_map_n_in(const struct isl_basic_map
*bmap
)
148 return bmap
? bmap
->dim
->n_in
: 0;
151 unsigned isl_basic_map_n_out(const struct isl_basic_map
*bmap
)
153 return bmap
? bmap
->dim
->n_out
: 0;
156 unsigned isl_basic_map_n_param(const struct isl_basic_map
*bmap
)
158 return bmap
? bmap
->dim
->nparam
: 0;
161 unsigned isl_basic_map_n_div(const struct isl_basic_map
*bmap
)
163 return bmap
? bmap
->n_div
: 0;
166 unsigned isl_basic_map_total_dim(const struct isl_basic_map
*bmap
)
168 return bmap
? isl_space_dim(bmap
->dim
, isl_dim_all
) + bmap
->n_div
: 0;
171 unsigned isl_map_n_in(const struct isl_map
*map
)
173 return map
? map
->dim
->n_in
: 0;
176 unsigned isl_map_n_out(const struct isl_map
*map
)
178 return map
? map
->dim
->n_out
: 0;
181 unsigned isl_map_n_param(const struct isl_map
*map
)
183 return map
? map
->dim
->nparam
: 0;
186 int isl_map_compatible_domain(struct isl_map
*map
, struct isl_set
*set
)
191 m
= isl_space_match(map
->dim
, isl_dim_param
, set
->dim
, isl_dim_param
);
194 return isl_space_tuple_match(map
->dim
, isl_dim_in
, set
->dim
, isl_dim_set
);
197 int isl_basic_map_compatible_domain(struct isl_basic_map
*bmap
,
198 struct isl_basic_set
*bset
)
203 m
= isl_space_match(bmap
->dim
, isl_dim_param
, bset
->dim
, isl_dim_param
);
206 return isl_space_tuple_match(bmap
->dim
, isl_dim_in
, bset
->dim
, isl_dim_set
);
209 int isl_map_compatible_range(__isl_keep isl_map
*map
, __isl_keep isl_set
*set
)
214 m
= isl_space_match(map
->dim
, isl_dim_param
, set
->dim
, isl_dim_param
);
217 return isl_space_tuple_match(map
->dim
, isl_dim_out
, set
->dim
, isl_dim_set
);
220 int isl_basic_map_compatible_range(struct isl_basic_map
*bmap
,
221 struct isl_basic_set
*bset
)
226 m
= isl_space_match(bmap
->dim
, isl_dim_param
, bset
->dim
, isl_dim_param
);
229 return isl_space_tuple_match(bmap
->dim
, isl_dim_out
, bset
->dim
, isl_dim_set
);
232 isl_ctx
*isl_basic_map_get_ctx(__isl_keep isl_basic_map
*bmap
)
234 return bmap
? bmap
->ctx
: NULL
;
237 isl_ctx
*isl_basic_set_get_ctx(__isl_keep isl_basic_set
*bset
)
239 return bset
? bset
->ctx
: NULL
;
242 isl_ctx
*isl_map_get_ctx(__isl_keep isl_map
*map
)
244 return map
? map
->ctx
: NULL
;
247 isl_ctx
*isl_set_get_ctx(__isl_keep isl_set
*set
)
249 return set
? set
->ctx
: NULL
;
252 __isl_give isl_space
*isl_basic_map_get_space(__isl_keep isl_basic_map
*bmap
)
256 return isl_space_copy(bmap
->dim
);
259 __isl_give isl_space
*isl_basic_set_get_space(__isl_keep isl_basic_set
*bset
)
263 return isl_space_copy(bset
->dim
);
266 /* Extract the divs in "bmap" as a matrix.
268 __isl_give isl_mat
*isl_basic_map_get_divs(__isl_keep isl_basic_map
*bmap
)
279 ctx
= isl_basic_map_get_ctx(bmap
);
280 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
281 cols
= 1 + 1 + total
+ bmap
->n_div
;
282 div
= isl_mat_alloc(ctx
, bmap
->n_div
, cols
);
286 for (i
= 0; i
< bmap
->n_div
; ++i
)
287 isl_seq_cpy(div
->row
[i
], bmap
->div
[i
], cols
);
292 /* Extract the divs in "bset" as a matrix.
294 __isl_give isl_mat
*isl_basic_set_get_divs(__isl_keep isl_basic_set
*bset
)
296 return isl_basic_map_get_divs(bset
);
299 __isl_give isl_local_space
*isl_basic_map_get_local_space(
300 __isl_keep isl_basic_map
*bmap
)
307 div
= isl_basic_map_get_divs(bmap
);
308 return isl_local_space_alloc_div(isl_space_copy(bmap
->dim
), div
);
311 __isl_give isl_local_space
*isl_basic_set_get_local_space(
312 __isl_keep isl_basic_set
*bset
)
314 return isl_basic_map_get_local_space(bset
);
317 __isl_give isl_basic_map
*isl_basic_map_from_local_space(
318 __isl_take isl_local_space
*ls
)
327 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
328 bmap
= isl_basic_map_alloc_space(isl_local_space_get_space(ls
),
329 n_div
, 0, 2 * n_div
);
331 for (i
= 0; i
< n_div
; ++i
)
332 if (isl_basic_map_alloc_div(bmap
) < 0)
335 for (i
= 0; i
< n_div
; ++i
) {
336 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], ls
->div
->n_col
);
337 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
341 isl_local_space_free(ls
);
344 isl_local_space_free(ls
);
345 isl_basic_map_free(bmap
);
349 __isl_give isl_basic_set
*isl_basic_set_from_local_space(
350 __isl_take isl_local_space
*ls
)
352 return isl_basic_map_from_local_space(ls
);
355 __isl_give isl_space
*isl_map_get_space(__isl_keep isl_map
*map
)
359 return isl_space_copy(map
->dim
);
362 __isl_give isl_space
*isl_set_get_space(__isl_keep isl_set
*set
)
366 return isl_space_copy(set
->dim
);
369 __isl_give isl_basic_map
*isl_basic_map_set_tuple_name(
370 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
, const char *s
)
372 bmap
= isl_basic_map_cow(bmap
);
375 bmap
->dim
= isl_space_set_tuple_name(bmap
->dim
, type
, s
);
378 bmap
= isl_basic_map_finalize(bmap
);
381 isl_basic_map_free(bmap
);
385 __isl_give isl_basic_set
*isl_basic_set_set_tuple_name(
386 __isl_take isl_basic_set
*bset
, const char *s
)
388 return isl_basic_map_set_tuple_name(bset
, isl_dim_set
, s
);
391 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map
*bmap
,
392 enum isl_dim_type type
)
394 return bmap
? isl_space_get_tuple_name(bmap
->dim
, type
) : NULL
;
397 __isl_give isl_map
*isl_map_set_tuple_name(__isl_take isl_map
*map
,
398 enum isl_dim_type type
, const char *s
)
402 map
= isl_map_cow(map
);
406 map
->dim
= isl_space_set_tuple_name(map
->dim
, type
, s
);
410 for (i
= 0; i
< map
->n
; ++i
) {
411 map
->p
[i
] = isl_basic_map_set_tuple_name(map
->p
[i
], type
, s
);
422 /* Does the input or output tuple have a name?
424 int isl_map_has_tuple_name(__isl_keep isl_map
*map
, enum isl_dim_type type
)
426 return map
? isl_space_has_tuple_name(map
->dim
, type
) : -1;
429 const char *isl_map_get_tuple_name(__isl_keep isl_map
*map
,
430 enum isl_dim_type type
)
432 return map
? isl_space_get_tuple_name(map
->dim
, type
) : NULL
;
435 __isl_give isl_set
*isl_set_set_tuple_name(__isl_take isl_set
*set
,
438 return (isl_set
*)isl_map_set_tuple_name((isl_map
*)set
, isl_dim_set
, s
);
441 __isl_give isl_map
*isl_map_set_tuple_id(__isl_take isl_map
*map
,
442 enum isl_dim_type type
, __isl_take isl_id
*id
)
444 map
= isl_map_cow(map
);
446 return isl_id_free(id
);
448 map
->dim
= isl_space_set_tuple_id(map
->dim
, type
, id
);
450 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
453 __isl_give isl_set
*isl_set_set_tuple_id(__isl_take isl_set
*set
,
454 __isl_take isl_id
*id
)
456 return isl_map_set_tuple_id(set
, isl_dim_set
, id
);
459 __isl_give isl_map
*isl_map_reset_tuple_id(__isl_take isl_map
*map
,
460 enum isl_dim_type type
)
462 map
= isl_map_cow(map
);
466 map
->dim
= isl_space_reset_tuple_id(map
->dim
, type
);
468 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
471 __isl_give isl_set
*isl_set_reset_tuple_id(__isl_take isl_set
*set
)
473 return isl_map_reset_tuple_id(set
, isl_dim_set
);
476 int isl_map_has_tuple_id(__isl_keep isl_map
*map
, enum isl_dim_type type
)
478 return map
? isl_space_has_tuple_id(map
->dim
, type
) : -1;
481 __isl_give isl_id
*isl_map_get_tuple_id(__isl_keep isl_map
*map
,
482 enum isl_dim_type type
)
484 return map
? isl_space_get_tuple_id(map
->dim
, type
) : NULL
;
487 int isl_set_has_tuple_id(__isl_keep isl_set
*set
)
489 return isl_map_has_tuple_id(set
, isl_dim_set
);
492 __isl_give isl_id
*isl_set_get_tuple_id(__isl_keep isl_set
*set
)
494 return isl_map_get_tuple_id(set
, isl_dim_set
);
497 /* Does the set tuple have a name?
499 int isl_set_has_tuple_name(__isl_keep isl_set
*set
)
501 return set
? isl_space_has_tuple_name(set
->dim
, isl_dim_set
) : -1;
505 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set
*bset
)
507 return bset
? isl_space_get_tuple_name(bset
->dim
, isl_dim_set
) : NULL
;
510 const char *isl_set_get_tuple_name(__isl_keep isl_set
*set
)
512 return set
? isl_space_get_tuple_name(set
->dim
, isl_dim_set
) : NULL
;
515 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map
*bmap
,
516 enum isl_dim_type type
, unsigned pos
)
518 return bmap
? isl_space_get_dim_name(bmap
->dim
, type
, pos
) : NULL
;
521 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set
*bset
,
522 enum isl_dim_type type
, unsigned pos
)
524 return bset
? isl_space_get_dim_name(bset
->dim
, type
, pos
) : NULL
;
527 /* Does the given dimension have a name?
529 int isl_map_has_dim_name(__isl_keep isl_map
*map
,
530 enum isl_dim_type type
, unsigned pos
)
532 return map
? isl_space_has_dim_name(map
->dim
, type
, pos
) : -1;
535 const char *isl_map_get_dim_name(__isl_keep isl_map
*map
,
536 enum isl_dim_type type
, unsigned pos
)
538 return map
? isl_space_get_dim_name(map
->dim
, type
, pos
) : NULL
;
541 const char *isl_set_get_dim_name(__isl_keep isl_set
*set
,
542 enum isl_dim_type type
, unsigned pos
)
544 return set
? isl_space_get_dim_name(set
->dim
, type
, pos
) : NULL
;
547 /* Does the given dimension have a name?
549 int isl_set_has_dim_name(__isl_keep isl_set
*set
,
550 enum isl_dim_type type
, unsigned pos
)
552 return set
? isl_space_has_dim_name(set
->dim
, type
, pos
) : -1;
555 __isl_give isl_basic_map
*isl_basic_map_set_dim_name(
556 __isl_take isl_basic_map
*bmap
,
557 enum isl_dim_type type
, unsigned pos
, const char *s
)
559 bmap
= isl_basic_map_cow(bmap
);
562 bmap
->dim
= isl_space_set_dim_name(bmap
->dim
, type
, pos
, s
);
565 return isl_basic_map_finalize(bmap
);
567 isl_basic_map_free(bmap
);
571 __isl_give isl_map
*isl_map_set_dim_name(__isl_take isl_map
*map
,
572 enum isl_dim_type type
, unsigned pos
, const char *s
)
576 map
= isl_map_cow(map
);
580 map
->dim
= isl_space_set_dim_name(map
->dim
, type
, pos
, s
);
584 for (i
= 0; i
< map
->n
; ++i
) {
585 map
->p
[i
] = isl_basic_map_set_dim_name(map
->p
[i
], type
, pos
, s
);
596 __isl_give isl_basic_set
*isl_basic_set_set_dim_name(
597 __isl_take isl_basic_set
*bset
,
598 enum isl_dim_type type
, unsigned pos
, const char *s
)
600 return (isl_basic_set
*)isl_basic_map_set_dim_name(
601 (isl_basic_map
*)bset
, type
, pos
, s
);
604 __isl_give isl_set
*isl_set_set_dim_name(__isl_take isl_set
*set
,
605 enum isl_dim_type type
, unsigned pos
, const char *s
)
607 return (isl_set
*)isl_map_set_dim_name((isl_map
*)set
, type
, pos
, s
);
610 int isl_basic_map_has_dim_id(__isl_keep isl_basic_map
*bmap
,
611 enum isl_dim_type type
, unsigned pos
)
613 return bmap
? isl_space_has_dim_id(bmap
->dim
, type
, pos
) : -1;
616 __isl_give isl_id
*isl_basic_set_get_dim_id(__isl_keep isl_basic_set
*bset
,
617 enum isl_dim_type type
, unsigned pos
)
619 return bset
? isl_space_get_dim_id(bset
->dim
, type
, pos
) : NULL
;
622 int isl_map_has_dim_id(__isl_keep isl_map
*map
,
623 enum isl_dim_type type
, unsigned pos
)
625 return map
? isl_space_has_dim_id(map
->dim
, type
, pos
) : -1;
628 __isl_give isl_id
*isl_map_get_dim_id(__isl_keep isl_map
*map
,
629 enum isl_dim_type type
, unsigned pos
)
631 return map
? isl_space_get_dim_id(map
->dim
, type
, pos
) : NULL
;
634 int isl_set_has_dim_id(__isl_keep isl_set
*set
,
635 enum isl_dim_type type
, unsigned pos
)
637 return isl_map_has_dim_id(set
, type
, pos
);
640 __isl_give isl_id
*isl_set_get_dim_id(__isl_keep isl_set
*set
,
641 enum isl_dim_type type
, unsigned pos
)
643 return isl_map_get_dim_id(set
, type
, pos
);
646 __isl_give isl_map
*isl_map_set_dim_id(__isl_take isl_map
*map
,
647 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
649 map
= isl_map_cow(map
);
651 return isl_id_free(id
);
653 map
->dim
= isl_space_set_dim_id(map
->dim
, type
, pos
, id
);
655 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
658 __isl_give isl_set
*isl_set_set_dim_id(__isl_take isl_set
*set
,
659 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
661 return isl_map_set_dim_id(set
, type
, pos
, id
);
664 int isl_map_find_dim_by_id(__isl_keep isl_map
*map
, enum isl_dim_type type
,
665 __isl_keep isl_id
*id
)
669 return isl_space_find_dim_by_id(map
->dim
, type
, id
);
672 int isl_set_find_dim_by_id(__isl_keep isl_set
*set
, enum isl_dim_type type
,
673 __isl_keep isl_id
*id
)
675 return isl_map_find_dim_by_id(set
, type
, id
);
678 int isl_map_find_dim_by_name(__isl_keep isl_map
*map
, enum isl_dim_type type
,
683 return isl_space_find_dim_by_name(map
->dim
, type
, name
);
686 int isl_set_find_dim_by_name(__isl_keep isl_set
*set
, enum isl_dim_type type
,
689 return isl_map_find_dim_by_name(set
, type
, name
);
692 int isl_basic_map_is_rational(__isl_keep isl_basic_map
*bmap
)
696 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
699 int isl_basic_set_is_rational(__isl_keep isl_basic_set
*bset
)
701 return isl_basic_map_is_rational(bset
);
704 /* Does "bmap" contain any rational points?
706 * If "bmap" has an equality for each dimension, equating the dimension
707 * to an integer constant, then it has no rational points, even if it
708 * is marked as rational.
710 int isl_basic_map_has_rational(__isl_keep isl_basic_map
*bmap
)
712 int has_rational
= 1;
717 if (isl_basic_map_plain_is_empty(bmap
))
719 if (!isl_basic_map_is_rational(bmap
))
721 bmap
= isl_basic_map_copy(bmap
);
722 bmap
= isl_basic_map_implicit_equalities(bmap
);
725 total
= isl_basic_map_total_dim(bmap
);
726 if (bmap
->n_eq
== total
) {
728 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
729 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1, total
);
732 if (!isl_int_is_one(bmap
->eq
[i
][1 + j
]) &&
733 !isl_int_is_negone(bmap
->eq
[i
][1 + j
]))
735 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + j
+ 1,
743 isl_basic_map_free(bmap
);
748 /* Does "map" contain any rational points?
750 int isl_map_has_rational(__isl_keep isl_map
*map
)
757 for (i
= 0; i
< map
->n
; ++i
) {
758 has_rational
= isl_basic_map_has_rational(map
->p
[i
]);
759 if (has_rational
< 0)
767 /* Does "set" contain any rational points?
769 int isl_set_has_rational(__isl_keep isl_set
*set
)
771 return isl_map_has_rational(set
);
774 /* Is this basic set a parameter domain?
776 int isl_basic_set_is_params(__isl_keep isl_basic_set
*bset
)
780 return isl_space_is_params(bset
->dim
);
783 /* Is this set a parameter domain?
785 int isl_set_is_params(__isl_keep isl_set
*set
)
789 return isl_space_is_params(set
->dim
);
792 /* Is this map actually a parameter domain?
793 * Users should never call this function. Outside of isl,
794 * a map can never be a parameter domain.
796 int isl_map_is_params(__isl_keep isl_map
*map
)
800 return isl_space_is_params(map
->dim
);
803 static struct isl_basic_map
*basic_map_init(struct isl_ctx
*ctx
,
804 struct isl_basic_map
*bmap
, unsigned extra
,
805 unsigned n_eq
, unsigned n_ineq
)
808 size_t row_size
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + extra
;
813 bmap
->block
= isl_blk_alloc(ctx
, (n_ineq
+ n_eq
) * row_size
);
814 if (isl_blk_is_error(bmap
->block
))
817 bmap
->ineq
= isl_alloc_array(ctx
, isl_int
*, n_ineq
+ n_eq
);
822 bmap
->block2
= isl_blk_empty();
825 bmap
->block2
= isl_blk_alloc(ctx
, extra
* (1 + row_size
));
826 if (isl_blk_is_error(bmap
->block2
))
829 bmap
->div
= isl_alloc_array(ctx
, isl_int
*, extra
);
834 for (i
= 0; i
< n_ineq
+ n_eq
; ++i
)
835 bmap
->ineq
[i
] = bmap
->block
.data
+ i
* row_size
;
837 for (i
= 0; i
< extra
; ++i
)
838 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + row_size
);
842 bmap
->c_size
= n_eq
+ n_ineq
;
843 bmap
->eq
= bmap
->ineq
+ n_ineq
;
852 isl_basic_map_free(bmap
);
856 struct isl_basic_set
*isl_basic_set_alloc(struct isl_ctx
*ctx
,
857 unsigned nparam
, unsigned dim
, unsigned extra
,
858 unsigned n_eq
, unsigned n_ineq
)
860 struct isl_basic_map
*bmap
;
863 space
= isl_space_set_alloc(ctx
, nparam
, dim
);
867 bmap
= isl_basic_map_alloc_space(space
, extra
, n_eq
, n_ineq
);
868 return (struct isl_basic_set
*)bmap
;
871 struct isl_basic_set
*isl_basic_set_alloc_space(__isl_take isl_space
*dim
,
872 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
874 struct isl_basic_map
*bmap
;
877 isl_assert(dim
->ctx
, dim
->n_in
== 0, goto error
);
878 bmap
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
879 return (struct isl_basic_set
*)bmap
;
885 struct isl_basic_map
*isl_basic_map_alloc_space(__isl_take isl_space
*dim
,
886 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
888 struct isl_basic_map
*bmap
;
892 bmap
= isl_calloc_type(dim
->ctx
, struct isl_basic_map
);
897 return basic_map_init(dim
->ctx
, bmap
, extra
, n_eq
, n_ineq
);
903 struct isl_basic_map
*isl_basic_map_alloc(struct isl_ctx
*ctx
,
904 unsigned nparam
, unsigned in
, unsigned out
, unsigned extra
,
905 unsigned n_eq
, unsigned n_ineq
)
907 struct isl_basic_map
*bmap
;
910 dim
= isl_space_alloc(ctx
, nparam
, in
, out
);
914 bmap
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
918 static void dup_constraints(
919 struct isl_basic_map
*dst
, struct isl_basic_map
*src
)
922 unsigned total
= isl_basic_map_total_dim(src
);
924 for (i
= 0; i
< src
->n_eq
; ++i
) {
925 int j
= isl_basic_map_alloc_equality(dst
);
926 isl_seq_cpy(dst
->eq
[j
], src
->eq
[i
], 1+total
);
929 for (i
= 0; i
< src
->n_ineq
; ++i
) {
930 int j
= isl_basic_map_alloc_inequality(dst
);
931 isl_seq_cpy(dst
->ineq
[j
], src
->ineq
[i
], 1+total
);
934 for (i
= 0; i
< src
->n_div
; ++i
) {
935 int j
= isl_basic_map_alloc_div(dst
);
936 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
);
938 ISL_F_SET(dst
, ISL_BASIC_SET_FINAL
);
941 struct isl_basic_map
*isl_basic_map_dup(struct isl_basic_map
*bmap
)
943 struct isl_basic_map
*dup
;
947 dup
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
948 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
951 dup_constraints(dup
, bmap
);
952 dup
->flags
= bmap
->flags
;
953 dup
->sample
= isl_vec_copy(bmap
->sample
);
957 struct isl_basic_set
*isl_basic_set_dup(struct isl_basic_set
*bset
)
959 struct isl_basic_map
*dup
;
961 dup
= isl_basic_map_dup((struct isl_basic_map
*)bset
);
962 return (struct isl_basic_set
*)dup
;
965 struct isl_basic_set
*isl_basic_set_copy(struct isl_basic_set
*bset
)
970 if (ISL_F_ISSET(bset
, ISL_BASIC_SET_FINAL
)) {
974 return isl_basic_set_dup(bset
);
977 struct isl_set
*isl_set_copy(struct isl_set
*set
)
986 struct isl_basic_map
*isl_basic_map_copy(struct isl_basic_map
*bmap
)
991 if (ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
)) {
995 bmap
= isl_basic_map_dup(bmap
);
997 ISL_F_SET(bmap
, ISL_BASIC_SET_FINAL
);
1001 struct isl_map
*isl_map_copy(struct isl_map
*map
)
1010 void *isl_basic_map_free(__isl_take isl_basic_map
*bmap
)
1015 if (--bmap
->ref
> 0)
1018 isl_ctx_deref(bmap
->ctx
);
1020 isl_blk_free(bmap
->ctx
, bmap
->block2
);
1022 isl_blk_free(bmap
->ctx
, bmap
->block
);
1023 isl_vec_free(bmap
->sample
);
1024 isl_space_free(bmap
->dim
);
1030 void *isl_basic_set_free(struct isl_basic_set
*bset
)
1032 return isl_basic_map_free((struct isl_basic_map
*)bset
);
1035 static int room_for_con(struct isl_basic_map
*bmap
, unsigned n
)
1037 return bmap
->n_eq
+ bmap
->n_ineq
+ n
<= bmap
->c_size
;
1040 __isl_give isl_map
*isl_map_align_params_map_map_and(
1041 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
,
1042 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*map1
,
1043 __isl_take isl_map
*map2
))
1047 if (isl_space_match(map1
->dim
, isl_dim_param
, map2
->dim
, isl_dim_param
))
1048 return fn(map1
, map2
);
1049 if (!isl_space_has_named_params(map1
->dim
) ||
1050 !isl_space_has_named_params(map2
->dim
))
1051 isl_die(map1
->ctx
, isl_error_invalid
,
1052 "unaligned unnamed parameters", goto error
);
1053 map1
= isl_map_align_params(map1
, isl_map_get_space(map2
));
1054 map2
= isl_map_align_params(map2
, isl_map_get_space(map1
));
1055 return fn(map1
, map2
);
1062 int isl_map_align_params_map_map_and_test(__isl_keep isl_map
*map1
,
1063 __isl_keep isl_map
*map2
,
1064 int (*fn
)(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
))
1070 if (isl_space_match(map1
->dim
, isl_dim_param
, map2
->dim
, isl_dim_param
))
1071 return fn(map1
, map2
);
1072 if (!isl_space_has_named_params(map1
->dim
) ||
1073 !isl_space_has_named_params(map2
->dim
))
1074 isl_die(map1
->ctx
, isl_error_invalid
,
1075 "unaligned unnamed parameters", return -1);
1076 map1
= isl_map_copy(map1
);
1077 map2
= isl_map_copy(map2
);
1078 map1
= isl_map_align_params(map1
, isl_map_get_space(map2
));
1079 map2
= isl_map_align_params(map2
, isl_map_get_space(map1
));
1086 int isl_basic_map_alloc_equality(struct isl_basic_map
*bmap
)
1088 struct isl_ctx
*ctx
;
1092 isl_assert(ctx
, room_for_con(bmap
, 1), return -1);
1093 isl_assert(ctx
, (bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
<= bmap
->c_size
,
1095 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1096 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1097 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1098 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1099 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1100 if ((bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
== bmap
->c_size
) {
1102 int j
= isl_basic_map_alloc_inequality(bmap
);
1106 bmap
->ineq
[j
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1107 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
1114 isl_seq_clr(bmap
->eq
[bmap
->n_eq
] + 1 + isl_basic_map_total_dim(bmap
),
1115 bmap
->extra
- bmap
->n_div
);
1116 return bmap
->n_eq
++;
1119 int isl_basic_set_alloc_equality(struct isl_basic_set
*bset
)
1121 return isl_basic_map_alloc_equality((struct isl_basic_map
*)bset
);
1124 int isl_basic_map_free_equality(struct isl_basic_map
*bmap
, unsigned n
)
1128 isl_assert(bmap
->ctx
, n
<= bmap
->n_eq
, return -1);
1133 int isl_basic_set_free_equality(struct isl_basic_set
*bset
, unsigned n
)
1135 return isl_basic_map_free_equality((struct isl_basic_map
*)bset
, n
);
1138 int isl_basic_map_drop_equality(struct isl_basic_map
*bmap
, unsigned pos
)
1143 isl_assert(bmap
->ctx
, pos
< bmap
->n_eq
, return -1);
1145 if (pos
!= bmap
->n_eq
- 1) {
1147 bmap
->eq
[pos
] = bmap
->eq
[bmap
->n_eq
- 1];
1148 bmap
->eq
[bmap
->n_eq
- 1] = t
;
1154 int isl_basic_set_drop_equality(struct isl_basic_set
*bset
, unsigned pos
)
1156 return isl_basic_map_drop_equality((struct isl_basic_map
*)bset
, pos
);
1159 /* Turn inequality "pos" of "bmap" into an equality.
1161 * In particular, we move the inequality in front of the equalities
1162 * and move the last inequality in the position of the moved inequality.
1163 * Note that isl_tab_make_equalities_explicit depends on this particular
1164 * change in the ordering of the constraints.
1166 void isl_basic_map_inequality_to_equality(
1167 struct isl_basic_map
*bmap
, unsigned pos
)
1171 t
= bmap
->ineq
[pos
];
1172 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1173 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
1178 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1179 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1180 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1181 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1184 static int room_for_ineq(struct isl_basic_map
*bmap
, unsigned n
)
1186 return bmap
->n_ineq
+ n
<= bmap
->eq
- bmap
->ineq
;
1189 int isl_basic_map_alloc_inequality(struct isl_basic_map
*bmap
)
1191 struct isl_ctx
*ctx
;
1195 isl_assert(ctx
, room_for_ineq(bmap
, 1), return -1);
1196 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1197 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1198 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1199 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1200 isl_seq_clr(bmap
->ineq
[bmap
->n_ineq
] +
1201 1 + isl_basic_map_total_dim(bmap
),
1202 bmap
->extra
- bmap
->n_div
);
1203 return bmap
->n_ineq
++;
1206 int isl_basic_set_alloc_inequality(struct isl_basic_set
*bset
)
1208 return isl_basic_map_alloc_inequality((struct isl_basic_map
*)bset
);
1211 int isl_basic_map_free_inequality(struct isl_basic_map
*bmap
, unsigned n
)
1215 isl_assert(bmap
->ctx
, n
<= bmap
->n_ineq
, return -1);
1220 int isl_basic_set_free_inequality(struct isl_basic_set
*bset
, unsigned n
)
1222 return isl_basic_map_free_inequality((struct isl_basic_map
*)bset
, n
);
1225 int isl_basic_map_drop_inequality(struct isl_basic_map
*bmap
, unsigned pos
)
1230 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
1232 if (pos
!= bmap
->n_ineq
- 1) {
1233 t
= bmap
->ineq
[pos
];
1234 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1235 bmap
->ineq
[bmap
->n_ineq
- 1] = t
;
1236 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1242 int isl_basic_set_drop_inequality(struct isl_basic_set
*bset
, unsigned pos
)
1244 return isl_basic_map_drop_inequality((struct isl_basic_map
*)bset
, pos
);
1247 __isl_give isl_basic_map
*isl_basic_map_add_eq(__isl_take isl_basic_map
*bmap
,
1252 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
1255 k
= isl_basic_map_alloc_equality(bmap
);
1258 isl_seq_cpy(bmap
->eq
[k
], eq
, 1 + isl_basic_map_total_dim(bmap
));
1261 isl_basic_map_free(bmap
);
1265 __isl_give isl_basic_set
*isl_basic_set_add_eq(__isl_take isl_basic_set
*bset
,
1268 return (isl_basic_set
*)
1269 isl_basic_map_add_eq((isl_basic_map
*)bset
, eq
);
1272 __isl_give isl_basic_map
*isl_basic_map_add_ineq(__isl_take isl_basic_map
*bmap
,
1277 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
1280 k
= isl_basic_map_alloc_inequality(bmap
);
1283 isl_seq_cpy(bmap
->ineq
[k
], ineq
, 1 + isl_basic_map_total_dim(bmap
));
1286 isl_basic_map_free(bmap
);
1290 __isl_give isl_basic_set
*isl_basic_set_add_ineq(__isl_take isl_basic_set
*bset
,
1293 return (isl_basic_set
*)
1294 isl_basic_map_add_ineq((isl_basic_map
*)bset
, ineq
);
1297 int isl_basic_map_alloc_div(struct isl_basic_map
*bmap
)
1301 isl_assert(bmap
->ctx
, bmap
->n_div
< bmap
->extra
, return -1);
1302 isl_seq_clr(bmap
->div
[bmap
->n_div
] +
1303 1 + 1 + isl_basic_map_total_dim(bmap
),
1304 bmap
->extra
- bmap
->n_div
);
1305 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1306 return bmap
->n_div
++;
1309 int isl_basic_set_alloc_div(struct isl_basic_set
*bset
)
1311 return isl_basic_map_alloc_div((struct isl_basic_map
*)bset
);
1314 int isl_basic_map_free_div(struct isl_basic_map
*bmap
, unsigned n
)
1318 isl_assert(bmap
->ctx
, n
<= bmap
->n_div
, return -1);
1323 int isl_basic_set_free_div(struct isl_basic_set
*bset
, unsigned n
)
1325 return isl_basic_map_free_div((struct isl_basic_map
*)bset
, n
);
1328 /* Copy constraint from src to dst, putting the vars of src at offset
1329 * dim_off in dst and the divs of src at offset div_off in dst.
1330 * If both sets are actually map, then dim_off applies to the input
1333 static void copy_constraint(struct isl_basic_map
*dst_map
, isl_int
*dst
,
1334 struct isl_basic_map
*src_map
, isl_int
*src
,
1335 unsigned in_off
, unsigned out_off
, unsigned div_off
)
1337 unsigned src_nparam
= isl_basic_map_n_param(src_map
);
1338 unsigned dst_nparam
= isl_basic_map_n_param(dst_map
);
1339 unsigned src_in
= isl_basic_map_n_in(src_map
);
1340 unsigned dst_in
= isl_basic_map_n_in(dst_map
);
1341 unsigned src_out
= isl_basic_map_n_out(src_map
);
1342 unsigned dst_out
= isl_basic_map_n_out(dst_map
);
1343 isl_int_set(dst
[0], src
[0]);
1344 isl_seq_cpy(dst
+1, src
+1, isl_min(dst_nparam
, src_nparam
));
1345 if (dst_nparam
> src_nparam
)
1346 isl_seq_clr(dst
+1+src_nparam
,
1347 dst_nparam
- src_nparam
);
1348 isl_seq_clr(dst
+1+dst_nparam
, in_off
);
1349 isl_seq_cpy(dst
+1+dst_nparam
+in_off
,
1351 isl_min(dst_in
-in_off
, src_in
));
1352 if (dst_in
-in_off
> src_in
)
1353 isl_seq_clr(dst
+1+dst_nparam
+in_off
+src_in
,
1354 dst_in
- in_off
- src_in
);
1355 isl_seq_clr(dst
+1+dst_nparam
+dst_in
, out_off
);
1356 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+out_off
,
1357 src
+1+src_nparam
+src_in
,
1358 isl_min(dst_out
-out_off
, src_out
));
1359 if (dst_out
-out_off
> src_out
)
1360 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+out_off
+src_out
,
1361 dst_out
- out_off
- src_out
);
1362 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
, div_off
);
1363 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+dst_out
+div_off
,
1364 src
+1+src_nparam
+src_in
+src_out
,
1365 isl_min(dst_map
->extra
-div_off
, src_map
->n_div
));
1366 if (dst_map
->n_div
-div_off
> src_map
->n_div
)
1367 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
+
1368 div_off
+src_map
->n_div
,
1369 dst_map
->n_div
- div_off
- src_map
->n_div
);
1372 static void copy_div(struct isl_basic_map
*dst_map
, isl_int
*dst
,
1373 struct isl_basic_map
*src_map
, isl_int
*src
,
1374 unsigned in_off
, unsigned out_off
, unsigned div_off
)
1376 isl_int_set(dst
[0], src
[0]);
1377 copy_constraint(dst_map
, dst
+1, src_map
, src
+1, in_off
, out_off
, div_off
);
1380 static struct isl_basic_map
*add_constraints(struct isl_basic_map
*bmap1
,
1381 struct isl_basic_map
*bmap2
, unsigned i_pos
, unsigned o_pos
)
1386 if (!bmap1
|| !bmap2
)
1389 div_off
= bmap1
->n_div
;
1391 for (i
= 0; i
< bmap2
->n_eq
; ++i
) {
1392 int i1
= isl_basic_map_alloc_equality(bmap1
);
1395 copy_constraint(bmap1
, bmap1
->eq
[i1
], bmap2
, bmap2
->eq
[i
],
1396 i_pos
, o_pos
, div_off
);
1399 for (i
= 0; i
< bmap2
->n_ineq
; ++i
) {
1400 int i1
= isl_basic_map_alloc_inequality(bmap1
);
1403 copy_constraint(bmap1
, bmap1
->ineq
[i1
], bmap2
, bmap2
->ineq
[i
],
1404 i_pos
, o_pos
, div_off
);
1407 for (i
= 0; i
< bmap2
->n_div
; ++i
) {
1408 int i1
= isl_basic_map_alloc_div(bmap1
);
1411 copy_div(bmap1
, bmap1
->div
[i1
], bmap2
, bmap2
->div
[i
],
1412 i_pos
, o_pos
, div_off
);
1415 isl_basic_map_free(bmap2
);
1420 isl_basic_map_free(bmap1
);
1421 isl_basic_map_free(bmap2
);
1425 struct isl_basic_set
*isl_basic_set_add_constraints(struct isl_basic_set
*bset1
,
1426 struct isl_basic_set
*bset2
, unsigned pos
)
1428 return (struct isl_basic_set
*)
1429 add_constraints((struct isl_basic_map
*)bset1
,
1430 (struct isl_basic_map
*)bset2
, 0, pos
);
1433 struct isl_basic_map
*isl_basic_map_extend_space(struct isl_basic_map
*base
,
1434 __isl_take isl_space
*dim
, unsigned extra
,
1435 unsigned n_eq
, unsigned n_ineq
)
1437 struct isl_basic_map
*ext
;
1447 dims_ok
= isl_space_is_equal(base
->dim
, dim
) &&
1448 base
->extra
>= base
->n_div
+ extra
;
1450 if (dims_ok
&& room_for_con(base
, n_eq
+ n_ineq
) &&
1451 room_for_ineq(base
, n_ineq
)) {
1452 isl_space_free(dim
);
1456 isl_assert(base
->ctx
, base
->dim
->nparam
<= dim
->nparam
, goto error
);
1457 isl_assert(base
->ctx
, base
->dim
->n_in
<= dim
->n_in
, goto error
);
1458 isl_assert(base
->ctx
, base
->dim
->n_out
<= dim
->n_out
, goto error
);
1459 extra
+= base
->extra
;
1461 n_ineq
+= base
->n_ineq
;
1463 ext
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
1469 ext
->sample
= isl_vec_copy(base
->sample
);
1470 flags
= base
->flags
;
1471 ext
= add_constraints(ext
, base
, 0, 0);
1474 ISL_F_CLR(ext
, ISL_BASIC_SET_FINAL
);
1480 isl_space_free(dim
);
1481 isl_basic_map_free(base
);
1485 struct isl_basic_set
*isl_basic_set_extend_space(struct isl_basic_set
*base
,
1486 __isl_take isl_space
*dim
, unsigned extra
,
1487 unsigned n_eq
, unsigned n_ineq
)
1489 return (struct isl_basic_set
*)
1490 isl_basic_map_extend_space((struct isl_basic_map
*)base
, dim
,
1491 extra
, n_eq
, n_ineq
);
1494 struct isl_basic_map
*isl_basic_map_extend_constraints(
1495 struct isl_basic_map
*base
, unsigned n_eq
, unsigned n_ineq
)
1499 return isl_basic_map_extend_space(base
, isl_space_copy(base
->dim
),
1503 struct isl_basic_map
*isl_basic_map_extend(struct isl_basic_map
*base
,
1504 unsigned nparam
, unsigned n_in
, unsigned n_out
, unsigned extra
,
1505 unsigned n_eq
, unsigned n_ineq
)
1507 struct isl_basic_map
*bmap
;
1512 dim
= isl_space_alloc(base
->ctx
, nparam
, n_in
, n_out
);
1516 bmap
= isl_basic_map_extend_space(base
, dim
, extra
, n_eq
, n_ineq
);
1519 isl_basic_map_free(base
);
1523 struct isl_basic_set
*isl_basic_set_extend(struct isl_basic_set
*base
,
1524 unsigned nparam
, unsigned dim
, unsigned extra
,
1525 unsigned n_eq
, unsigned n_ineq
)
1527 return (struct isl_basic_set
*)
1528 isl_basic_map_extend((struct isl_basic_map
*)base
,
1529 nparam
, 0, dim
, extra
, n_eq
, n_ineq
);
1532 struct isl_basic_set
*isl_basic_set_extend_constraints(
1533 struct isl_basic_set
*base
, unsigned n_eq
, unsigned n_ineq
)
1535 return (struct isl_basic_set
*)
1536 isl_basic_map_extend_constraints((struct isl_basic_map
*)base
,
1540 struct isl_basic_set
*isl_basic_set_cow(struct isl_basic_set
*bset
)
1542 return (struct isl_basic_set
*)
1543 isl_basic_map_cow((struct isl_basic_map
*)bset
);
1546 struct isl_basic_map
*isl_basic_map_cow(struct isl_basic_map
*bmap
)
1551 if (bmap
->ref
> 1) {
1553 bmap
= isl_basic_map_dup(bmap
);
1556 ISL_F_CLR(bmap
, ISL_BASIC_SET_FINAL
);
1560 struct isl_set
*isl_set_cow(struct isl_set
*set
)
1568 return isl_set_dup(set
);
1571 struct isl_map
*isl_map_cow(struct isl_map
*map
)
1579 return isl_map_dup(map
);
1582 static void swap_vars(struct isl_blk blk
, isl_int
*a
,
1583 unsigned a_len
, unsigned b_len
)
1585 isl_seq_cpy(blk
.data
, a
+a_len
, b_len
);
1586 isl_seq_cpy(blk
.data
+b_len
, a
, a_len
);
1587 isl_seq_cpy(a
, blk
.data
, b_len
+a_len
);
1590 static __isl_give isl_basic_map
*isl_basic_map_swap_vars(
1591 __isl_take isl_basic_map
*bmap
, unsigned pos
, unsigned n1
, unsigned n2
)
1599 isl_assert(bmap
->ctx
,
1600 pos
+ n1
+ n2
<= 1 + isl_basic_map_total_dim(bmap
), goto error
);
1602 if (n1
== 0 || n2
== 0)
1605 bmap
= isl_basic_map_cow(bmap
);
1609 blk
= isl_blk_alloc(bmap
->ctx
, n1
+ n2
);
1610 if (isl_blk_is_error(blk
))
1613 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1615 bmap
->eq
[i
] + pos
, n1
, n2
);
1617 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1619 bmap
->ineq
[i
] + pos
, n1
, n2
);
1621 for (i
= 0; i
< bmap
->n_div
; ++i
)
1623 bmap
->div
[i
]+1 + pos
, n1
, n2
);
1625 isl_blk_free(bmap
->ctx
, blk
);
1627 ISL_F_CLR(bmap
, ISL_BASIC_SET_NORMALIZED
);
1628 bmap
= isl_basic_map_gauss(bmap
, NULL
);
1629 return isl_basic_map_finalize(bmap
);
1631 isl_basic_map_free(bmap
);
1635 static __isl_give isl_basic_set
*isl_basic_set_swap_vars(
1636 __isl_take isl_basic_set
*bset
, unsigned n
)
1644 nparam
= isl_basic_set_n_param(bset
);
1645 dim
= isl_basic_set_n_dim(bset
);
1646 isl_assert(bset
->ctx
, n
<= dim
, goto error
);
1648 return isl_basic_map_swap_vars(bset
, 1 + nparam
, n
, dim
- n
);
1650 isl_basic_set_free(bset
);
1654 struct isl_basic_map
*isl_basic_map_set_to_empty(struct isl_basic_map
*bmap
)
1660 total
= isl_basic_map_total_dim(bmap
);
1661 isl_basic_map_free_div(bmap
, bmap
->n_div
);
1662 isl_basic_map_free_inequality(bmap
, bmap
->n_ineq
);
1664 isl_basic_map_free_equality(bmap
, bmap
->n_eq
-1);
1666 i
= isl_basic_map_alloc_equality(bmap
);
1670 isl_int_set_si(bmap
->eq
[i
][0], 1);
1671 isl_seq_clr(bmap
->eq
[i
]+1, total
);
1672 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
1673 isl_vec_free(bmap
->sample
);
1674 bmap
->sample
= NULL
;
1675 return isl_basic_map_finalize(bmap
);
1677 isl_basic_map_free(bmap
);
1681 struct isl_basic_set
*isl_basic_set_set_to_empty(struct isl_basic_set
*bset
)
1683 return (struct isl_basic_set
*)
1684 isl_basic_map_set_to_empty((struct isl_basic_map
*)bset
);
1687 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
1690 static void swap_div(__isl_keep isl_basic_map
*bmap
, int a
, int b
)
1692 isl_int
*t
= bmap
->div
[a
];
1693 bmap
->div
[a
] = bmap
->div
[b
];
1697 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
1698 * div definitions accordingly.
1700 void isl_basic_map_swap_div(struct isl_basic_map
*bmap
, int a
, int b
)
1703 unsigned off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
1705 swap_div(bmap
, a
, b
);
1707 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1708 isl_int_swap(bmap
->eq
[i
][1+off
+a
], bmap
->eq
[i
][1+off
+b
]);
1710 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1711 isl_int_swap(bmap
->ineq
[i
][1+off
+a
], bmap
->ineq
[i
][1+off
+b
]);
1713 for (i
= 0; i
< bmap
->n_div
; ++i
)
1714 isl_int_swap(bmap
->div
[i
][1+1+off
+a
], bmap
->div
[i
][1+1+off
+b
]);
1715 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1718 /* Eliminate the specified n dimensions starting at first from the
1719 * constraints, without removing the dimensions from the space.
1720 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1722 __isl_give isl_map
*isl_map_eliminate(__isl_take isl_map
*map
,
1723 enum isl_dim_type type
, unsigned first
, unsigned n
)
1732 if (first
+ n
> isl_map_dim(map
, type
) || first
+ n
< first
)
1733 isl_die(map
->ctx
, isl_error_invalid
,
1734 "index out of bounds", goto error
);
1736 map
= isl_map_cow(map
);
1740 for (i
= 0; i
< map
->n
; ++i
) {
1741 map
->p
[i
] = isl_basic_map_eliminate(map
->p
[i
], type
, first
, n
);
1751 /* Eliminate the specified n dimensions starting at first from the
1752 * constraints, without removing the dimensions from the space.
1753 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1755 __isl_give isl_set
*isl_set_eliminate(__isl_take isl_set
*set
,
1756 enum isl_dim_type type
, unsigned first
, unsigned n
)
1758 return (isl_set
*)isl_map_eliminate((isl_map
*)set
, type
, first
, n
);
1761 /* Eliminate the specified n dimensions starting at first from the
1762 * constraints, without removing the dimensions from the space.
1763 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1765 __isl_give isl_set
*isl_set_eliminate_dims(__isl_take isl_set
*set
,
1766 unsigned first
, unsigned n
)
1768 return isl_set_eliminate(set
, isl_dim_set
, first
, n
);
1771 __isl_give isl_basic_map
*isl_basic_map_remove_divs(
1772 __isl_take isl_basic_map
*bmap
)
1776 bmap
= isl_basic_map_eliminate_vars(bmap
,
1777 isl_space_dim(bmap
->dim
, isl_dim_all
), bmap
->n_div
);
1781 return isl_basic_map_finalize(bmap
);
1784 __isl_give isl_basic_set
*isl_basic_set_remove_divs(
1785 __isl_take isl_basic_set
*bset
)
1787 return (struct isl_basic_set
*)isl_basic_map_remove_divs(
1788 (struct isl_basic_map
*)bset
);
1791 __isl_give isl_map
*isl_map_remove_divs(__isl_take isl_map
*map
)
1800 map
= isl_map_cow(map
);
1804 for (i
= 0; i
< map
->n
; ++i
) {
1805 map
->p
[i
] = isl_basic_map_remove_divs(map
->p
[i
]);
1815 __isl_give isl_set
*isl_set_remove_divs(__isl_take isl_set
*set
)
1817 return isl_map_remove_divs(set
);
1820 struct isl_basic_map
*isl_basic_map_remove_dims(struct isl_basic_map
*bmap
,
1821 enum isl_dim_type type
, unsigned first
, unsigned n
)
1825 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
1827 if (n
== 0 && !isl_space_is_named_or_nested(bmap
->dim
, type
))
1829 bmap
= isl_basic_map_eliminate_vars(bmap
,
1830 isl_basic_map_offset(bmap
, type
) - 1 + first
, n
);
1833 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
) && type
== isl_dim_div
)
1835 bmap
= isl_basic_map_drop(bmap
, type
, first
, n
);
1838 isl_basic_map_free(bmap
);
1842 /* Return true if the definition of the given div (recursively) involves
1843 * any of the given variables.
1845 static int div_involves_vars(__isl_keep isl_basic_map
*bmap
, int div
,
1846 unsigned first
, unsigned n
)
1849 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
1851 if (isl_int_is_zero(bmap
->div
[div
][0]))
1853 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + first
, n
) >= 0)
1856 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
1857 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
1859 if (div_involves_vars(bmap
, i
, first
, n
))
1866 /* Try and add a lower and/or upper bound on "div" to "bmap"
1867 * based on inequality "i".
1868 * "total" is the total number of variables (excluding the divs).
1869 * "v" is a temporary object that can be used during the calculations.
1870 * If "lb" is set, then a lower bound should be constructed.
1871 * If "ub" is set, then an upper bound should be constructed.
1873 * The calling function has already checked that the inequality does not
1874 * reference "div", but we still need to check that the inequality is
1875 * of the right form. We'll consider the case where we want to construct
1876 * a lower bound. The construction of upper bounds is similar.
1878 * Let "div" be of the form
1880 * q = floor((a + f(x))/d)
1882 * We essentially check if constraint "i" is of the form
1886 * so that we can use it to derive a lower bound on "div".
1887 * However, we allow a slightly more general form
1891 * with the condition that the coefficients of g(x) - f(x) are all
1893 * Rewriting this constraint as
1897 * adding a + f(x) to both sides and dividing by d, we obtain
1899 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
1901 * Taking the floor on both sides, we obtain
1903 * q >= floor((a-b)/d) + (f(x)-g(x))/d
1907 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
1909 * In the case of an upper bound, we construct the constraint
1911 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
1914 static __isl_give isl_basic_map
*insert_bounds_on_div_from_ineq(
1915 __isl_take isl_basic_map
*bmap
, int div
, int i
,
1916 unsigned total
, isl_int v
, int lb
, int ub
)
1920 for (j
= 0; (lb
|| ub
) && j
< total
+ bmap
->n_div
; ++j
) {
1922 isl_int_sub(v
, bmap
->ineq
[i
][1 + j
],
1923 bmap
->div
[div
][1 + 1 + j
]);
1924 lb
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
1927 isl_int_add(v
, bmap
->ineq
[i
][1 + j
],
1928 bmap
->div
[div
][1 + 1 + j
]);
1929 ub
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
1935 bmap
= isl_basic_map_extend_constraints(bmap
, 0, lb
+ ub
);
1937 int k
= isl_basic_map_alloc_inequality(bmap
);
1940 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
1941 isl_int_sub(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
1942 bmap
->div
[div
][1 + j
]);
1943 isl_int_cdiv_q(bmap
->ineq
[k
][j
],
1944 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
1946 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], 1);
1949 int k
= isl_basic_map_alloc_inequality(bmap
);
1952 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
1953 isl_int_add(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
1954 bmap
->div
[div
][1 + j
]);
1955 isl_int_fdiv_q(bmap
->ineq
[k
][j
],
1956 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
1958 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], -1);
1963 isl_basic_map_free(bmap
);
1967 /* This function is called right before "div" is eliminated from "bmap"
1968 * using Fourier-Motzkin.
1969 * Look through the constraints of "bmap" for constraints on the argument
1970 * of the integer division and use them to construct constraints on the
1971 * integer division itself. These constraints can then be combined
1972 * during the Fourier-Motzkin elimination.
1973 * Note that it is only useful to introduce lower bounds on "div"
1974 * if "bmap" already contains upper bounds on "div" as the newly
1975 * introduce lower bounds can then be combined with the pre-existing
1976 * upper bounds. Similarly for upper bounds.
1977 * We therefore first check if "bmap" contains any lower and/or upper bounds
1980 * It is interesting to note that the introduction of these constraints
1981 * can indeed lead to more accurate results, even when compared to
1982 * deriving constraints on the argument of "div" from constraints on "div".
1983 * Consider, for example, the set
1985 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
1987 * The second constraint can be rewritten as
1989 * 2 * [(-i-2j+3)/4] + k >= 0
1991 * from which we can derive
1993 * -i - 2j + 3 >= -2k
1999 * Combined with the first constraint, we obtain
2001 * -3 <= 3 + 2k or k >= -3
2003 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2004 * the first constraint, we obtain
2006 * [(i + 2j)/4] >= [-3/4] = -1
2008 * Combining this constraint with the second constraint, we obtain
2012 static __isl_give isl_basic_map
*insert_bounds_on_div(
2013 __isl_take isl_basic_map
*bmap
, int div
)
2016 int check_lb
, check_ub
;
2023 if (isl_int_is_zero(bmap
->div
[div
][0]))
2026 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
2030 for (i
= 0; (!check_lb
|| !check_ub
) && i
< bmap
->n_ineq
; ++i
) {
2031 int s
= isl_int_sgn(bmap
->ineq
[i
][1 + total
+ div
]);
2038 if (!check_lb
&& !check_ub
)
2043 for (i
= 0; bmap
&& i
< bmap
->n_ineq
; ++i
) {
2044 if (!isl_int_is_zero(bmap
->ineq
[i
][1 + total
+ div
]))
2047 bmap
= insert_bounds_on_div_from_ineq(bmap
, div
, i
, total
, v
,
2048 check_lb
, check_ub
);
2056 /* Remove all divs (recursively) involving any of the given dimensions
2057 * in their definitions.
2059 __isl_give isl_basic_map
*isl_basic_map_remove_divs_involving_dims(
2060 __isl_take isl_basic_map
*bmap
,
2061 enum isl_dim_type type
, unsigned first
, unsigned n
)
2067 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
2069 first
+= isl_basic_map_offset(bmap
, type
);
2071 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2072 if (!div_involves_vars(bmap
, i
, first
, n
))
2074 bmap
= insert_bounds_on_div(bmap
, i
);
2075 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
2083 isl_basic_map_free(bmap
);
2087 __isl_give isl_basic_set
*isl_basic_set_remove_divs_involving_dims(
2088 __isl_take isl_basic_set
*bset
,
2089 enum isl_dim_type type
, unsigned first
, unsigned n
)
2091 return isl_basic_map_remove_divs_involving_dims(bset
, type
, first
, n
);
2094 __isl_give isl_map
*isl_map_remove_divs_involving_dims(__isl_take isl_map
*map
,
2095 enum isl_dim_type type
, unsigned first
, unsigned n
)
2104 map
= isl_map_cow(map
);
2108 for (i
= 0; i
< map
->n
; ++i
) {
2109 map
->p
[i
] = isl_basic_map_remove_divs_involving_dims(map
->p
[i
],
2120 __isl_give isl_set
*isl_set_remove_divs_involving_dims(__isl_take isl_set
*set
,
2121 enum isl_dim_type type
, unsigned first
, unsigned n
)
2123 return (isl_set
*)isl_map_remove_divs_involving_dims((isl_map
*)set
,
2127 /* Does the desciption of "bmap" depend on the specified dimensions?
2128 * We also check whether the dimensions appear in any of the div definitions.
2129 * In principle there is no need for this check. If the dimensions appear
2130 * in a div definition, they also appear in the defining constraints of that
2133 int isl_basic_map_involves_dims(__isl_keep isl_basic_map
*bmap
,
2134 enum isl_dim_type type
, unsigned first
, unsigned n
)
2141 if (first
+ n
> isl_basic_map_dim(bmap
, type
))
2142 isl_die(bmap
->ctx
, isl_error_invalid
,
2143 "index out of bounds", return -1);
2145 first
+= isl_basic_map_offset(bmap
, type
);
2146 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2147 if (isl_seq_first_non_zero(bmap
->eq
[i
] + first
, n
) >= 0)
2149 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2150 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + first
, n
) >= 0)
2152 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2153 if (isl_int_is_zero(bmap
->div
[i
][0]))
2155 if (isl_seq_first_non_zero(bmap
->div
[i
] + 1 + first
, n
) >= 0)
2162 int isl_map_involves_dims(__isl_keep isl_map
*map
,
2163 enum isl_dim_type type
, unsigned first
, unsigned n
)
2170 if (first
+ n
> isl_map_dim(map
, type
))
2171 isl_die(map
->ctx
, isl_error_invalid
,
2172 "index out of bounds", return -1);
2174 for (i
= 0; i
< map
->n
; ++i
) {
2175 int involves
= isl_basic_map_involves_dims(map
->p
[i
],
2177 if (involves
< 0 || involves
)
2184 int isl_basic_set_involves_dims(__isl_keep isl_basic_set
*bset
,
2185 enum isl_dim_type type
, unsigned first
, unsigned n
)
2187 return isl_basic_map_involves_dims(bset
, type
, first
, n
);
2190 int isl_set_involves_dims(__isl_keep isl_set
*set
,
2191 enum isl_dim_type type
, unsigned first
, unsigned n
)
2193 return isl_map_involves_dims(set
, type
, first
, n
);
2196 /* Return true if the definition of the given div is unknown or depends
2199 static int div_is_unknown(__isl_keep isl_basic_map
*bmap
, int div
)
2202 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
2204 if (isl_int_is_zero(bmap
->div
[div
][0]))
2207 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2208 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
2210 if (div_is_unknown(bmap
, i
))
2217 /* Remove all divs that are unknown or defined in terms of unknown divs.
2219 __isl_give isl_basic_map
*isl_basic_map_remove_unknown_divs(
2220 __isl_take isl_basic_map
*bmap
)
2227 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2228 if (!div_is_unknown(bmap
, i
))
2230 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
2239 /* Remove all divs that are unknown or defined in terms of unknown divs.
2241 __isl_give isl_basic_set
*isl_basic_set_remove_unknown_divs(
2242 __isl_take isl_basic_set
*bset
)
2244 return isl_basic_map_remove_unknown_divs(bset
);
2247 __isl_give isl_map
*isl_map_remove_unknown_divs(__isl_take isl_map
*map
)
2256 map
= isl_map_cow(map
);
2260 for (i
= 0; i
< map
->n
; ++i
) {
2261 map
->p
[i
] = isl_basic_map_remove_unknown_divs(map
->p
[i
]);
2271 __isl_give isl_set
*isl_set_remove_unknown_divs(__isl_take isl_set
*set
)
2273 return (isl_set
*)isl_map_remove_unknown_divs((isl_map
*)set
);
2276 __isl_give isl_basic_set
*isl_basic_set_remove_dims(
2277 __isl_take isl_basic_set
*bset
,
2278 enum isl_dim_type type
, unsigned first
, unsigned n
)
2280 return (isl_basic_set
*)
2281 isl_basic_map_remove_dims((isl_basic_map
*)bset
, type
, first
, n
);
2284 struct isl_map
*isl_map_remove_dims(struct isl_map
*map
,
2285 enum isl_dim_type type
, unsigned first
, unsigned n
)
2292 map
= isl_map_cow(map
);
2295 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
2297 for (i
= 0; i
< map
->n
; ++i
) {
2298 map
->p
[i
] = isl_basic_map_eliminate_vars(map
->p
[i
],
2299 isl_basic_map_offset(map
->p
[i
], type
) - 1 + first
, n
);
2303 map
= isl_map_drop(map
, type
, first
, n
);
2310 __isl_give isl_set
*isl_set_remove_dims(__isl_take isl_set
*bset
,
2311 enum isl_dim_type type
, unsigned first
, unsigned n
)
2313 return (isl_set
*)isl_map_remove_dims((isl_map
*)bset
, type
, first
, n
);
2316 /* Project out n inputs starting at first using Fourier-Motzkin */
2317 struct isl_map
*isl_map_remove_inputs(struct isl_map
*map
,
2318 unsigned first
, unsigned n
)
2320 return isl_map_remove_dims(map
, isl_dim_in
, first
, n
);
2323 static void dump_term(struct isl_basic_map
*bmap
,
2324 isl_int c
, int pos
, FILE *out
)
2327 unsigned in
= isl_basic_map_n_in(bmap
);
2328 unsigned dim
= in
+ isl_basic_map_n_out(bmap
);
2329 unsigned nparam
= isl_basic_map_n_param(bmap
);
2331 isl_int_print(out
, c
, 0);
2333 if (!isl_int_is_one(c
))
2334 isl_int_print(out
, c
, 0);
2335 if (pos
< 1 + nparam
) {
2336 name
= isl_space_get_dim_name(bmap
->dim
,
2337 isl_dim_param
, pos
- 1);
2339 fprintf(out
, "%s", name
);
2341 fprintf(out
, "p%d", pos
- 1);
2342 } else if (pos
< 1 + nparam
+ in
)
2343 fprintf(out
, "i%d", pos
- 1 - nparam
);
2344 else if (pos
< 1 + nparam
+ dim
)
2345 fprintf(out
, "o%d", pos
- 1 - nparam
- in
);
2347 fprintf(out
, "e%d", pos
- 1 - nparam
- dim
);
2351 static void dump_constraint_sign(struct isl_basic_map
*bmap
, isl_int
*c
,
2352 int sign
, FILE *out
)
2356 unsigned len
= 1 + isl_basic_map_total_dim(bmap
);
2360 for (i
= 0, first
= 1; i
< len
; ++i
) {
2361 if (isl_int_sgn(c
[i
]) * sign
<= 0)
2364 fprintf(out
, " + ");
2366 isl_int_abs(v
, c
[i
]);
2367 dump_term(bmap
, v
, i
, out
);
2374 static void dump_constraint(struct isl_basic_map
*bmap
, isl_int
*c
,
2375 const char *op
, FILE *out
, int indent
)
2379 fprintf(out
, "%*s", indent
, "");
2381 dump_constraint_sign(bmap
, c
, 1, out
);
2382 fprintf(out
, " %s ", op
);
2383 dump_constraint_sign(bmap
, c
, -1, out
);
2387 for (i
= bmap
->n_div
; i
< bmap
->extra
; ++i
) {
2388 if (isl_int_is_zero(c
[1+isl_space_dim(bmap
->dim
, isl_dim_all
)+i
]))
2390 fprintf(out
, "%*s", indent
, "");
2391 fprintf(out
, "ERROR: unused div coefficient not zero\n");
2396 static void dump_constraints(struct isl_basic_map
*bmap
,
2397 isl_int
**c
, unsigned n
,
2398 const char *op
, FILE *out
, int indent
)
2402 for (i
= 0; i
< n
; ++i
)
2403 dump_constraint(bmap
, c
[i
], op
, out
, indent
);
2406 static void dump_affine(struct isl_basic_map
*bmap
, isl_int
*exp
, FILE *out
)
2410 unsigned total
= isl_basic_map_total_dim(bmap
);
2412 for (j
= 0; j
< 1 + total
; ++j
) {
2413 if (isl_int_is_zero(exp
[j
]))
2415 if (!first
&& isl_int_is_pos(exp
[j
]))
2417 dump_term(bmap
, exp
[j
], j
, out
);
2422 static void dump(struct isl_basic_map
*bmap
, FILE *out
, int indent
)
2426 dump_constraints(bmap
, bmap
->eq
, bmap
->n_eq
, "=", out
, indent
);
2427 dump_constraints(bmap
, bmap
->ineq
, bmap
->n_ineq
, ">=", out
, indent
);
2429 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2430 fprintf(out
, "%*s", indent
, "");
2431 fprintf(out
, "e%d = [(", i
);
2432 dump_affine(bmap
, bmap
->div
[i
]+1, out
);
2434 isl_int_print(out
, bmap
->div
[i
][0], 0);
2435 fprintf(out
, "]\n");
2439 void isl_basic_set_print_internal(struct isl_basic_set
*bset
,
2440 FILE *out
, int indent
)
2443 fprintf(out
, "null basic set\n");
2447 fprintf(out
, "%*s", indent
, "");
2448 fprintf(out
, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
2449 bset
->ref
, bset
->dim
->nparam
, bset
->dim
->n_out
,
2450 bset
->extra
, bset
->flags
);
2451 dump((struct isl_basic_map
*)bset
, out
, indent
);
2454 void isl_basic_map_print_internal(struct isl_basic_map
*bmap
,
2455 FILE *out
, int indent
)
2458 fprintf(out
, "null basic map\n");
2462 fprintf(out
, "%*s", indent
, "");
2463 fprintf(out
, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
2464 "flags: %x, n_name: %d\n",
2466 bmap
->dim
->nparam
, bmap
->dim
->n_in
, bmap
->dim
->n_out
,
2467 bmap
->extra
, bmap
->flags
, bmap
->dim
->n_id
);
2468 dump(bmap
, out
, indent
);
2471 int isl_inequality_negate(struct isl_basic_map
*bmap
, unsigned pos
)
2476 total
= isl_basic_map_total_dim(bmap
);
2477 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
2478 isl_seq_neg(bmap
->ineq
[pos
], bmap
->ineq
[pos
], 1 + total
);
2479 isl_int_sub_ui(bmap
->ineq
[pos
][0], bmap
->ineq
[pos
][0], 1);
2480 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
2484 __isl_give isl_set
*isl_set_alloc_space(__isl_take isl_space
*dim
, int n
,
2487 struct isl_set
*set
;
2491 isl_assert(dim
->ctx
, dim
->n_in
== 0, goto error
);
2492 isl_assert(dim
->ctx
, n
>= 0, goto error
);
2493 set
= isl_alloc(dim
->ctx
, struct isl_set
,
2494 sizeof(struct isl_set
) +
2495 (n
- 1) * sizeof(struct isl_basic_set
*));
2499 set
->ctx
= dim
->ctx
;
2500 isl_ctx_ref(set
->ctx
);
2508 isl_space_free(dim
);
2512 struct isl_set
*isl_set_alloc(struct isl_ctx
*ctx
,
2513 unsigned nparam
, unsigned dim
, int n
, unsigned flags
)
2515 struct isl_set
*set
;
2518 dims
= isl_space_alloc(ctx
, nparam
, 0, dim
);
2522 set
= isl_set_alloc_space(dims
, n
, flags
);
2526 /* Make sure "map" has room for at least "n" more basic maps.
2528 struct isl_map
*isl_map_grow(struct isl_map
*map
, int n
)
2531 struct isl_map
*grown
= NULL
;
2535 isl_assert(map
->ctx
, n
>= 0, goto error
);
2536 if (map
->n
+ n
<= map
->size
)
2538 grown
= isl_map_alloc_space(isl_map_get_space(map
), map
->n
+ n
, map
->flags
);
2541 for (i
= 0; i
< map
->n
; ++i
) {
2542 grown
->p
[i
] = isl_basic_map_copy(map
->p
[i
]);
2550 isl_map_free(grown
);
2555 /* Make sure "set" has room for at least "n" more basic sets.
2557 struct isl_set
*isl_set_grow(struct isl_set
*set
, int n
)
2559 return (struct isl_set
*)isl_map_grow((struct isl_map
*)set
, n
);
2562 struct isl_set
*isl_set_dup(struct isl_set
*set
)
2565 struct isl_set
*dup
;
2570 dup
= isl_set_alloc_space(isl_space_copy(set
->dim
), set
->n
, set
->flags
);
2573 for (i
= 0; i
< set
->n
; ++i
)
2574 dup
= isl_set_add_basic_set(dup
, isl_basic_set_copy(set
->p
[i
]));
2578 struct isl_set
*isl_set_from_basic_set(struct isl_basic_set
*bset
)
2580 return isl_map_from_basic_map(bset
);
2583 struct isl_map
*isl_map_from_basic_map(struct isl_basic_map
*bmap
)
2585 struct isl_map
*map
;
2590 map
= isl_map_alloc_space(isl_space_copy(bmap
->dim
), 1, ISL_MAP_DISJOINT
);
2591 return isl_map_add_basic_map(map
, bmap
);
2594 __isl_give isl_set
*isl_set_add_basic_set(__isl_take isl_set
*set
,
2595 __isl_take isl_basic_set
*bset
)
2597 return (struct isl_set
*)isl_map_add_basic_map((struct isl_map
*)set
,
2598 (struct isl_basic_map
*)bset
);
2601 void *isl_set_free(__isl_take isl_set
*set
)
2611 isl_ctx_deref(set
->ctx
);
2612 for (i
= 0; i
< set
->n
; ++i
)
2613 isl_basic_set_free(set
->p
[i
]);
2614 isl_space_free(set
->dim
);
2620 void isl_set_print_internal(struct isl_set
*set
, FILE *out
, int indent
)
2625 fprintf(out
, "null set\n");
2629 fprintf(out
, "%*s", indent
, "");
2630 fprintf(out
, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
2631 set
->ref
, set
->n
, set
->dim
->nparam
, set
->dim
->n_out
,
2633 for (i
= 0; i
< set
->n
; ++i
) {
2634 fprintf(out
, "%*s", indent
, "");
2635 fprintf(out
, "basic set %d:\n", i
);
2636 isl_basic_set_print_internal(set
->p
[i
], out
, indent
+4);
2640 void isl_map_print_internal(struct isl_map
*map
, FILE *out
, int indent
)
2645 fprintf(out
, "null map\n");
2649 fprintf(out
, "%*s", indent
, "");
2650 fprintf(out
, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2651 "flags: %x, n_name: %d\n",
2652 map
->ref
, map
->n
, map
->dim
->nparam
, map
->dim
->n_in
,
2653 map
->dim
->n_out
, map
->flags
, map
->dim
->n_id
);
2654 for (i
= 0; i
< map
->n
; ++i
) {
2655 fprintf(out
, "%*s", indent
, "");
2656 fprintf(out
, "basic map %d:\n", i
);
2657 isl_basic_map_print_internal(map
->p
[i
], out
, indent
+4);
2661 struct isl_basic_map
*isl_basic_map_intersect_domain(
2662 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
2664 struct isl_basic_map
*bmap_domain
;
2669 isl_assert(bset
->ctx
, isl_space_match(bmap
->dim
, isl_dim_param
,
2670 bset
->dim
, isl_dim_param
), goto error
);
2672 if (isl_space_dim(bset
->dim
, isl_dim_set
) != 0)
2673 isl_assert(bset
->ctx
,
2674 isl_basic_map_compatible_domain(bmap
, bset
), goto error
);
2676 bmap
= isl_basic_map_cow(bmap
);
2679 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
2680 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
2681 bmap_domain
= isl_basic_map_from_domain(bset
);
2682 bmap
= add_constraints(bmap
, bmap_domain
, 0, 0);
2684 bmap
= isl_basic_map_simplify(bmap
);
2685 return isl_basic_map_finalize(bmap
);
2687 isl_basic_map_free(bmap
);
2688 isl_basic_set_free(bset
);
2692 struct isl_basic_map
*isl_basic_map_intersect_range(
2693 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
2695 struct isl_basic_map
*bmap_range
;
2700 isl_assert(bset
->ctx
, isl_space_match(bmap
->dim
, isl_dim_param
,
2701 bset
->dim
, isl_dim_param
), goto error
);
2703 if (isl_space_dim(bset
->dim
, isl_dim_set
) != 0)
2704 isl_assert(bset
->ctx
,
2705 isl_basic_map_compatible_range(bmap
, bset
), goto error
);
2707 if (isl_basic_set_is_universe(bset
)) {
2708 isl_basic_set_free(bset
);
2712 bmap
= isl_basic_map_cow(bmap
);
2715 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
2716 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
2717 bmap_range
= isl_basic_map_from_basic_set(bset
, isl_space_copy(bset
->dim
));
2718 bmap
= add_constraints(bmap
, bmap_range
, 0, 0);
2720 bmap
= isl_basic_map_simplify(bmap
);
2721 return isl_basic_map_finalize(bmap
);
2723 isl_basic_map_free(bmap
);
2724 isl_basic_set_free(bset
);
2728 int isl_basic_map_contains(struct isl_basic_map
*bmap
, struct isl_vec
*vec
)
2737 total
= 1 + isl_basic_map_total_dim(bmap
);
2738 if (total
!= vec
->size
)
2743 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
2744 isl_seq_inner_product(vec
->el
, bmap
->eq
[i
], total
, &s
);
2745 if (!isl_int_is_zero(s
)) {
2751 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
2752 isl_seq_inner_product(vec
->el
, bmap
->ineq
[i
], total
, &s
);
2753 if (isl_int_is_neg(s
)) {
2764 int isl_basic_set_contains(struct isl_basic_set
*bset
, struct isl_vec
*vec
)
2766 return isl_basic_map_contains((struct isl_basic_map
*)bset
, vec
);
2769 struct isl_basic_map
*isl_basic_map_intersect(
2770 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
2772 struct isl_vec
*sample
= NULL
;
2774 if (!bmap1
|| !bmap2
)
2777 isl_assert(bmap1
->ctx
, isl_space_match(bmap1
->dim
, isl_dim_param
,
2778 bmap2
->dim
, isl_dim_param
), goto error
);
2779 if (isl_space_dim(bmap1
->dim
, isl_dim_all
) ==
2780 isl_space_dim(bmap1
->dim
, isl_dim_param
) &&
2781 isl_space_dim(bmap2
->dim
, isl_dim_all
) !=
2782 isl_space_dim(bmap2
->dim
, isl_dim_param
))
2783 return isl_basic_map_intersect(bmap2
, bmap1
);
2785 if (isl_space_dim(bmap2
->dim
, isl_dim_all
) !=
2786 isl_space_dim(bmap2
->dim
, isl_dim_param
))
2787 isl_assert(bmap1
->ctx
,
2788 isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
2790 if (bmap1
->sample
&&
2791 isl_basic_map_contains(bmap1
, bmap1
->sample
) > 0 &&
2792 isl_basic_map_contains(bmap2
, bmap1
->sample
) > 0)
2793 sample
= isl_vec_copy(bmap1
->sample
);
2794 else if (bmap2
->sample
&&
2795 isl_basic_map_contains(bmap1
, bmap2
->sample
) > 0 &&
2796 isl_basic_map_contains(bmap2
, bmap2
->sample
) > 0)
2797 sample
= isl_vec_copy(bmap2
->sample
);
2799 bmap1
= isl_basic_map_cow(bmap1
);
2802 bmap1
= isl_basic_map_extend_space(bmap1
, isl_space_copy(bmap1
->dim
),
2803 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
2804 bmap1
= add_constraints(bmap1
, bmap2
, 0, 0);
2807 isl_vec_free(sample
);
2809 isl_vec_free(bmap1
->sample
);
2810 bmap1
->sample
= sample
;
2813 bmap1
= isl_basic_map_simplify(bmap1
);
2814 return isl_basic_map_finalize(bmap1
);
2817 isl_vec_free(sample
);
2818 isl_basic_map_free(bmap1
);
2819 isl_basic_map_free(bmap2
);
2823 struct isl_basic_set
*isl_basic_set_intersect(
2824 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
2826 return (struct isl_basic_set
*)
2827 isl_basic_map_intersect(
2828 (struct isl_basic_map
*)bset1
,
2829 (struct isl_basic_map
*)bset2
);
2832 __isl_give isl_basic_set
*isl_basic_set_intersect_params(
2833 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
2835 return isl_basic_set_intersect(bset1
, bset2
);
2838 /* Special case of isl_map_intersect, where both map1 and map2
2839 * are convex, without any divs and such that either map1 or map2
2840 * contains a single constraint. This constraint is then simply
2841 * added to the other map.
2843 static __isl_give isl_map
*map_intersect_add_constraint(
2844 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
2846 isl_assert(map1
->ctx
, map1
->n
== 1, goto error
);
2847 isl_assert(map2
->ctx
, map1
->n
== 1, goto error
);
2848 isl_assert(map1
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
2849 isl_assert(map2
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
2851 if (map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
!= 1)
2852 return isl_map_intersect(map2
, map1
);
2854 isl_assert(map2
->ctx
,
2855 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1, goto error
);
2857 map1
= isl_map_cow(map1
);
2860 if (isl_map_plain_is_empty(map1
)) {
2864 map1
->p
[0] = isl_basic_map_cow(map1
->p
[0]);
2865 if (map2
->p
[0]->n_eq
== 1)
2866 map1
->p
[0] = isl_basic_map_add_eq(map1
->p
[0], map2
->p
[0]->eq
[0]);
2868 map1
->p
[0] = isl_basic_map_add_ineq(map1
->p
[0],
2869 map2
->p
[0]->ineq
[0]);
2871 map1
->p
[0] = isl_basic_map_simplify(map1
->p
[0]);
2872 map1
->p
[0] = isl_basic_map_finalize(map1
->p
[0]);
2876 if (isl_basic_map_plain_is_empty(map1
->p
[0])) {
2877 isl_basic_map_free(map1
->p
[0]);
2890 /* map2 may be either a parameter domain or a map living in the same
2893 static __isl_give isl_map
*map_intersect_internal(__isl_take isl_map
*map1
,
2894 __isl_take isl_map
*map2
)
2903 if ((isl_map_plain_is_empty(map1
) ||
2904 isl_map_plain_is_universe(map2
)) &&
2905 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
2909 if ((isl_map_plain_is_empty(map2
) ||
2910 isl_map_plain_is_universe(map1
)) &&
2911 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
2916 if (map1
->n
== 1 && map2
->n
== 1 &&
2917 map1
->p
[0]->n_div
== 0 && map2
->p
[0]->n_div
== 0 &&
2918 isl_space_is_equal(map1
->dim
, map2
->dim
) &&
2919 (map1
->p
[0]->n_eq
+ map1
->p
[0]->n_ineq
== 1 ||
2920 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1))
2921 return map_intersect_add_constraint(map1
, map2
);
2923 if (isl_space_dim(map2
->dim
, isl_dim_all
) !=
2924 isl_space_dim(map2
->dim
, isl_dim_param
))
2925 isl_assert(map1
->ctx
,
2926 isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
2928 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
2929 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
2930 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
2932 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
2933 map1
->n
* map2
->n
, flags
);
2936 for (i
= 0; i
< map1
->n
; ++i
)
2937 for (j
= 0; j
< map2
->n
; ++j
) {
2938 struct isl_basic_map
*part
;
2939 part
= isl_basic_map_intersect(
2940 isl_basic_map_copy(map1
->p
[i
]),
2941 isl_basic_map_copy(map2
->p
[j
]));
2942 if (isl_basic_map_is_empty(part
) < 0)
2943 part
= isl_basic_map_free(part
);
2944 result
= isl_map_add_basic_map(result
, part
);
2957 static __isl_give isl_map
*map_intersect(__isl_take isl_map
*map1
,
2958 __isl_take isl_map
*map2
)
2962 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
2963 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
2964 "spaces don't match", goto error
);
2965 return map_intersect_internal(map1
, map2
);
2972 __isl_give isl_map
*isl_map_intersect(__isl_take isl_map
*map1
,
2973 __isl_take isl_map
*map2
)
2975 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect
);
2978 struct isl_set
*isl_set_intersect(struct isl_set
*set1
, struct isl_set
*set2
)
2980 return (struct isl_set
*)
2981 isl_map_intersect((struct isl_map
*)set1
,
2982 (struct isl_map
*)set2
);
2985 /* map_intersect_internal accepts intersections
2986 * with parameter domains, so we can just call that function.
2988 static __isl_give isl_map
*map_intersect_params(__isl_take isl_map
*map
,
2989 __isl_take isl_set
*params
)
2991 return map_intersect_internal(map
, params
);
2994 __isl_give isl_map
*isl_map_intersect_params(__isl_take isl_map
*map1
,
2995 __isl_take isl_map
*map2
)
2997 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect_params
);
3000 __isl_give isl_set
*isl_set_intersect_params(__isl_take isl_set
*set
,
3001 __isl_take isl_set
*params
)
3003 return isl_map_intersect_params(set
, params
);
3006 struct isl_basic_map
*isl_basic_map_reverse(struct isl_basic_map
*bmap
)
3009 struct isl_basic_set
*bset
;
3014 bmap
= isl_basic_map_cow(bmap
);
3017 dim
= isl_space_reverse(isl_space_copy(bmap
->dim
));
3018 in
= isl_basic_map_n_in(bmap
);
3019 bset
= isl_basic_set_from_basic_map(bmap
);
3020 bset
= isl_basic_set_swap_vars(bset
, in
);
3021 return isl_basic_map_from_basic_set(bset
, dim
);
3024 static __isl_give isl_basic_map
*basic_map_space_reset(
3025 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
)
3031 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
3034 space
= isl_basic_map_get_space(bmap
);
3035 space
= isl_space_reset(space
, type
);
3036 bmap
= isl_basic_map_reset_space(bmap
, space
);
3040 __isl_give isl_basic_map
*isl_basic_map_insert_dims(
3041 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
3042 unsigned pos
, unsigned n
)
3045 struct isl_basic_map
*res
;
3046 struct isl_dim_map
*dim_map
;
3047 unsigned total
, off
;
3048 enum isl_dim_type t
;
3051 return basic_map_space_reset(bmap
, type
);
3056 res_dim
= isl_space_insert_dims(isl_basic_map_get_space(bmap
), type
, pos
, n
);
3058 total
= isl_basic_map_total_dim(bmap
) + n
;
3059 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3061 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3063 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3065 unsigned size
= isl_basic_map_dim(bmap
, t
);
3066 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3068 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3069 pos
, size
- pos
, off
+ pos
+ n
);
3071 off
+= isl_space_dim(res_dim
, t
);
3073 isl_dim_map_div(dim_map
, bmap
, off
);
3075 res
= isl_basic_map_alloc_space(res_dim
,
3076 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3077 if (isl_basic_map_is_rational(bmap
))
3078 res
= isl_basic_map_set_rational(res
);
3079 if (isl_basic_map_plain_is_empty(bmap
)) {
3080 isl_basic_map_free(bmap
);
3082 return isl_basic_map_set_to_empty(res
);
3084 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3085 return isl_basic_map_finalize(res
);
3088 __isl_give isl_basic_set
*isl_basic_set_insert_dims(
3089 __isl_take isl_basic_set
*bset
,
3090 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3092 return isl_basic_map_insert_dims(bset
, type
, pos
, n
);
3095 __isl_give isl_basic_map
*isl_basic_map_add(__isl_take isl_basic_map
*bmap
,
3096 enum isl_dim_type type
, unsigned n
)
3100 return isl_basic_map_insert_dims(bmap
, type
,
3101 isl_basic_map_dim(bmap
, type
), n
);
3104 __isl_give isl_basic_set
*isl_basic_set_add_dims(__isl_take isl_basic_set
*bset
,
3105 enum isl_dim_type type
, unsigned n
)
3109 isl_assert(bset
->ctx
, type
!= isl_dim_in
, goto error
);
3110 return (isl_basic_set
*)isl_basic_map_add((isl_basic_map
*)bset
, type
, n
);
3112 isl_basic_set_free(bset
);
3116 static __isl_give isl_map
*map_space_reset(__isl_take isl_map
*map
,
3117 enum isl_dim_type type
)
3121 if (!map
|| !isl_space_is_named_or_nested(map
->dim
, type
))
3124 space
= isl_map_get_space(map
);
3125 space
= isl_space_reset(space
, type
);
3126 map
= isl_map_reset_space(map
, space
);
3130 __isl_give isl_map
*isl_map_insert_dims(__isl_take isl_map
*map
,
3131 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3136 return map_space_reset(map
, type
);
3138 map
= isl_map_cow(map
);
3142 map
->dim
= isl_space_insert_dims(map
->dim
, type
, pos
, n
);
3146 for (i
= 0; i
< map
->n
; ++i
) {
3147 map
->p
[i
] = isl_basic_map_insert_dims(map
->p
[i
], type
, pos
, n
);
3158 __isl_give isl_set
*isl_set_insert_dims(__isl_take isl_set
*set
,
3159 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3161 return isl_map_insert_dims(set
, type
, pos
, n
);
3164 __isl_give isl_map
*isl_map_add_dims(__isl_take isl_map
*map
,
3165 enum isl_dim_type type
, unsigned n
)
3169 return isl_map_insert_dims(map
, type
, isl_map_dim(map
, type
), n
);
3172 __isl_give isl_set
*isl_set_add_dims(__isl_take isl_set
*set
,
3173 enum isl_dim_type type
, unsigned n
)
3177 isl_assert(set
->ctx
, type
!= isl_dim_in
, goto error
);
3178 return (isl_set
*)isl_map_add_dims((isl_map
*)set
, type
, n
);
3184 __isl_give isl_basic_map
*isl_basic_map_move_dims(
3185 __isl_take isl_basic_map
*bmap
,
3186 enum isl_dim_type dst_type
, unsigned dst_pos
,
3187 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3189 struct isl_dim_map
*dim_map
;
3190 struct isl_basic_map
*res
;
3191 enum isl_dim_type t
;
3192 unsigned total
, off
;
3199 isl_assert(bmap
->ctx
, src_pos
+ n
<= isl_basic_map_dim(bmap
, src_type
),
3202 if (dst_type
== src_type
&& dst_pos
== src_pos
)
3205 isl_assert(bmap
->ctx
, dst_type
!= src_type
, goto error
);
3207 if (pos(bmap
->dim
, dst_type
) + dst_pos
==
3208 pos(bmap
->dim
, src_type
) + src_pos
+
3209 ((src_type
< dst_type
) ? n
: 0)) {
3210 bmap
= isl_basic_map_cow(bmap
);
3214 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
3215 src_type
, src_pos
, n
);
3219 bmap
= isl_basic_map_finalize(bmap
);
3224 total
= isl_basic_map_total_dim(bmap
);
3225 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3228 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3229 unsigned size
= isl_space_dim(bmap
->dim
, t
);
3230 if (t
== dst_type
) {
3231 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3234 isl_dim_map_dim_range(dim_map
, bmap
->dim
, src_type
,
3237 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3238 dst_pos
, size
- dst_pos
, off
);
3239 off
+= size
- dst_pos
;
3240 } else if (t
== src_type
) {
3241 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3244 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3245 src_pos
+ n
, size
- src_pos
- n
, off
);
3246 off
+= size
- src_pos
- n
;
3248 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3252 isl_dim_map_div(dim_map
, bmap
, off
);
3254 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
3255 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3256 bmap
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3260 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
3261 src_type
, src_pos
, n
);
3265 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
3266 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3267 bmap
= isl_basic_map_finalize(bmap
);
3271 isl_basic_map_free(bmap
);
3275 __isl_give isl_basic_set
*isl_basic_set_move_dims(__isl_take isl_basic_set
*bset
,
3276 enum isl_dim_type dst_type
, unsigned dst_pos
,
3277 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3279 return (isl_basic_set
*)isl_basic_map_move_dims(
3280 (isl_basic_map
*)bset
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
3283 __isl_give isl_set
*isl_set_move_dims(__isl_take isl_set
*set
,
3284 enum isl_dim_type dst_type
, unsigned dst_pos
,
3285 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3289 isl_assert(set
->ctx
, dst_type
!= isl_dim_in
, goto error
);
3290 return (isl_set
*)isl_map_move_dims((isl_map
*)set
, dst_type
, dst_pos
,
3291 src_type
, src_pos
, n
);
3297 __isl_give isl_map
*isl_map_move_dims(__isl_take isl_map
*map
,
3298 enum isl_dim_type dst_type
, unsigned dst_pos
,
3299 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3308 isl_assert(map
->ctx
, src_pos
+ n
<= isl_map_dim(map
, src_type
),
3311 if (dst_type
== src_type
&& dst_pos
== src_pos
)
3314 isl_assert(map
->ctx
, dst_type
!= src_type
, goto error
);
3316 map
= isl_map_cow(map
);
3320 map
->dim
= isl_space_move_dims(map
->dim
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
3324 for (i
= 0; i
< map
->n
; ++i
) {
3325 map
->p
[i
] = isl_basic_map_move_dims(map
->p
[i
],
3327 src_type
, src_pos
, n
);
3338 /* Move the specified dimensions to the last columns right before
3339 * the divs. Don't change the dimension specification of bmap.
3340 * That's the responsibility of the caller.
3342 static __isl_give isl_basic_map
*move_last(__isl_take isl_basic_map
*bmap
,
3343 enum isl_dim_type type
, unsigned first
, unsigned n
)
3345 struct isl_dim_map
*dim_map
;
3346 struct isl_basic_map
*res
;
3347 enum isl_dim_type t
;
3348 unsigned total
, off
;
3352 if (pos(bmap
->dim
, type
) + first
+ n
==
3353 1 + isl_space_dim(bmap
->dim
, isl_dim_all
))
3356 total
= isl_basic_map_total_dim(bmap
);
3357 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3360 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3361 unsigned size
= isl_space_dim(bmap
->dim
, t
);
3363 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3366 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3367 first
, n
, total
- bmap
->n_div
- n
);
3368 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3369 first
+ n
, size
- (first
+ n
), off
);
3370 off
+= size
- (first
+ n
);
3372 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3376 isl_dim_map_div(dim_map
, bmap
, off
+ n
);
3378 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
3379 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3380 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3384 /* Insert "n" rows in the divs of "bmap".
3386 * The number of columns is not changed, which means that the last
3387 * dimensions of "bmap" are being reintepreted as the new divs.
3388 * The space of "bmap" is not adjusted, however, which means
3389 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
3390 * from the space of "bmap" is the responsibility of the caller.
3392 static __isl_give isl_basic_map
*insert_div_rows(__isl_take isl_basic_map
*bmap
,
3400 bmap
= isl_basic_map_cow(bmap
);
3404 row_size
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + bmap
->extra
;
3405 old
= bmap
->block2
.data
;
3406 bmap
->block2
= isl_blk_extend(bmap
->ctx
, bmap
->block2
,
3407 (bmap
->extra
+ n
) * (1 + row_size
));
3408 if (!bmap
->block2
.data
)
3409 return isl_basic_map_free(bmap
);
3410 new_div
= isl_alloc_array(bmap
->ctx
, isl_int
*, bmap
->extra
+ n
);
3412 return isl_basic_map_free(bmap
);
3413 for (i
= 0; i
< n
; ++i
) {
3414 new_div
[i
] = bmap
->block2
.data
+
3415 (bmap
->extra
+ i
) * (1 + row_size
);
3416 isl_seq_clr(new_div
[i
], 1 + row_size
);
3418 for (i
= 0; i
< bmap
->extra
; ++i
)
3419 new_div
[n
+ i
] = bmap
->block2
.data
+ (bmap
->div
[i
] - old
);
3421 bmap
->div
= new_div
;
3428 /* Turn the n dimensions of type type, starting at first
3429 * into existentially quantified variables.
3431 __isl_give isl_basic_map
*isl_basic_map_project_out(
3432 __isl_take isl_basic_map
*bmap
,
3433 enum isl_dim_type type
, unsigned first
, unsigned n
)
3436 return basic_map_space_reset(bmap
, type
);
3441 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
3442 return isl_basic_map_remove_dims(bmap
, type
, first
, n
);
3444 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
3447 bmap
= move_last(bmap
, type
, first
, n
);
3448 bmap
= isl_basic_map_cow(bmap
);
3449 bmap
= insert_div_rows(bmap
, n
);
3453 bmap
->dim
= isl_space_drop_dims(bmap
->dim
, type
, first
, n
);
3456 bmap
= isl_basic_map_simplify(bmap
);
3457 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
3458 return isl_basic_map_finalize(bmap
);
3460 isl_basic_map_free(bmap
);
3464 /* Turn the n dimensions of type type, starting at first
3465 * into existentially quantified variables.
3467 struct isl_basic_set
*isl_basic_set_project_out(struct isl_basic_set
*bset
,
3468 enum isl_dim_type type
, unsigned first
, unsigned n
)
3470 return (isl_basic_set
*)isl_basic_map_project_out(
3471 (isl_basic_map
*)bset
, type
, first
, n
);
3474 /* Turn the n dimensions of type type, starting at first
3475 * into existentially quantified variables.
3477 __isl_give isl_map
*isl_map_project_out(__isl_take isl_map
*map
,
3478 enum isl_dim_type type
, unsigned first
, unsigned n
)
3486 return map_space_reset(map
, type
);
3488 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
3490 map
= isl_map_cow(map
);
3494 map
->dim
= isl_space_drop_dims(map
->dim
, type
, first
, n
);
3498 for (i
= 0; i
< map
->n
; ++i
) {
3499 map
->p
[i
] = isl_basic_map_project_out(map
->p
[i
], type
, first
, n
);
3510 /* Turn the n dimensions of type type, starting at first
3511 * into existentially quantified variables.
3513 __isl_give isl_set
*isl_set_project_out(__isl_take isl_set
*set
,
3514 enum isl_dim_type type
, unsigned first
, unsigned n
)
3516 return (isl_set
*)isl_map_project_out((isl_map
*)set
, type
, first
, n
);
3519 static struct isl_basic_map
*add_divs(struct isl_basic_map
*bmap
, unsigned n
)
3523 for (i
= 0; i
< n
; ++i
) {
3524 j
= isl_basic_map_alloc_div(bmap
);
3527 isl_seq_clr(bmap
->div
[j
], 1+1+isl_basic_map_total_dim(bmap
));
3531 isl_basic_map_free(bmap
);
3535 struct isl_basic_map
*isl_basic_map_apply_range(
3536 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3538 isl_space
*dim_result
= NULL
;
3539 struct isl_basic_map
*bmap
;
3540 unsigned n_in
, n_out
, n
, nparam
, total
, pos
;
3541 struct isl_dim_map
*dim_map1
, *dim_map2
;
3543 if (!bmap1
|| !bmap2
)
3546 dim_result
= isl_space_join(isl_space_copy(bmap1
->dim
),
3547 isl_space_copy(bmap2
->dim
));
3549 n_in
= isl_basic_map_n_in(bmap1
);
3550 n_out
= isl_basic_map_n_out(bmap2
);
3551 n
= isl_basic_map_n_out(bmap1
);
3552 nparam
= isl_basic_map_n_param(bmap1
);
3554 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ n
;
3555 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
3556 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
3557 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
3558 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
3559 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
3560 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_in
);
3561 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_out
);
3562 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
3563 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
3564 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
3566 bmap
= isl_basic_map_alloc_space(dim_result
,
3567 bmap1
->n_div
+ bmap2
->n_div
+ n
,
3568 bmap1
->n_eq
+ bmap2
->n_eq
,
3569 bmap1
->n_ineq
+ bmap2
->n_ineq
);
3570 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
3571 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
3572 bmap
= add_divs(bmap
, n
);
3573 bmap
= isl_basic_map_simplify(bmap
);
3574 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
3575 return isl_basic_map_finalize(bmap
);
3577 isl_basic_map_free(bmap1
);
3578 isl_basic_map_free(bmap2
);
3582 struct isl_basic_set
*isl_basic_set_apply(
3583 struct isl_basic_set
*bset
, struct isl_basic_map
*bmap
)
3588 isl_assert(bset
->ctx
, isl_basic_map_compatible_domain(bmap
, bset
),
3591 return (struct isl_basic_set
*)
3592 isl_basic_map_apply_range((struct isl_basic_map
*)bset
, bmap
);
3594 isl_basic_set_free(bset
);
3595 isl_basic_map_free(bmap
);
3599 struct isl_basic_map
*isl_basic_map_apply_domain(
3600 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3602 if (!bmap1
|| !bmap2
)
3605 isl_assert(bmap1
->ctx
,
3606 isl_basic_map_n_in(bmap1
) == isl_basic_map_n_in(bmap2
), goto error
);
3607 isl_assert(bmap1
->ctx
,
3608 isl_basic_map_n_param(bmap1
) == isl_basic_map_n_param(bmap2
),
3611 bmap1
= isl_basic_map_reverse(bmap1
);
3612 bmap1
= isl_basic_map_apply_range(bmap1
, bmap2
);
3613 return isl_basic_map_reverse(bmap1
);
3615 isl_basic_map_free(bmap1
);
3616 isl_basic_map_free(bmap2
);
3620 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
3621 * A \cap B -> f(A) + f(B)
3623 struct isl_basic_map
*isl_basic_map_sum(
3624 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3626 unsigned n_in
, n_out
, nparam
, total
, pos
;
3627 struct isl_basic_map
*bmap
= NULL
;
3628 struct isl_dim_map
*dim_map1
, *dim_map2
;
3631 if (!bmap1
|| !bmap2
)
3634 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
),
3637 nparam
= isl_basic_map_n_param(bmap1
);
3638 n_in
= isl_basic_map_n_in(bmap1
);
3639 n_out
= isl_basic_map_n_out(bmap1
);
3641 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
;
3642 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
3643 dim_map2
= isl_dim_map_alloc(bmap2
->ctx
, total
);
3644 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
3645 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
);
3646 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
3647 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
3648 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_in
+ n_out
);
3649 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
3650 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
3651 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_out
);
3653 bmap
= isl_basic_map_alloc_space(isl_space_copy(bmap1
->dim
),
3654 bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
,
3655 bmap1
->n_eq
+ bmap2
->n_eq
+ n_out
,
3656 bmap1
->n_ineq
+ bmap2
->n_ineq
);
3657 for (i
= 0; i
< n_out
; ++i
) {
3658 int j
= isl_basic_map_alloc_equality(bmap
);
3661 isl_seq_clr(bmap
->eq
[j
], 1+total
);
3662 isl_int_set_si(bmap
->eq
[j
][1+nparam
+n_in
+i
], -1);
3663 isl_int_set_si(bmap
->eq
[j
][1+pos
+i
], 1);
3664 isl_int_set_si(bmap
->eq
[j
][1+pos
-n_out
+i
], 1);
3666 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
3667 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
3668 bmap
= add_divs(bmap
, 2 * n_out
);
3670 bmap
= isl_basic_map_simplify(bmap
);
3671 return isl_basic_map_finalize(bmap
);
3673 isl_basic_map_free(bmap
);
3674 isl_basic_map_free(bmap1
);
3675 isl_basic_map_free(bmap2
);
3679 /* Given two maps A -> f(A) and B -> g(B), construct a map
3680 * A \cap B -> f(A) + f(B)
3682 struct isl_map
*isl_map_sum(struct isl_map
*map1
, struct isl_map
*map2
)
3684 struct isl_map
*result
;
3690 isl_assert(map1
->ctx
, isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
3692 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
3693 map1
->n
* map2
->n
, 0);
3696 for (i
= 0; i
< map1
->n
; ++i
)
3697 for (j
= 0; j
< map2
->n
; ++j
) {
3698 struct isl_basic_map
*part
;
3699 part
= isl_basic_map_sum(
3700 isl_basic_map_copy(map1
->p
[i
]),
3701 isl_basic_map_copy(map2
->p
[j
]));
3702 if (isl_basic_map_is_empty(part
))
3703 isl_basic_map_free(part
);
3705 result
= isl_map_add_basic_map(result
, part
);
3718 __isl_give isl_set
*isl_set_sum(__isl_take isl_set
*set1
,
3719 __isl_take isl_set
*set2
)
3721 return (isl_set
*)isl_map_sum((isl_map
*)set1
, (isl_map
*)set2
);
3724 /* Given a basic map A -> f(A), construct A -> -f(A).
3726 struct isl_basic_map
*isl_basic_map_neg(struct isl_basic_map
*bmap
)
3731 bmap
= isl_basic_map_cow(bmap
);
3735 n
= isl_basic_map_dim(bmap
, isl_dim_out
);
3736 off
= isl_basic_map_offset(bmap
, isl_dim_out
);
3737 for (i
= 0; i
< bmap
->n_eq
; ++i
)
3738 for (j
= 0; j
< n
; ++j
)
3739 isl_int_neg(bmap
->eq
[i
][off
+j
], bmap
->eq
[i
][off
+j
]);
3740 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
3741 for (j
= 0; j
< n
; ++j
)
3742 isl_int_neg(bmap
->ineq
[i
][off
+j
], bmap
->ineq
[i
][off
+j
]);
3743 for (i
= 0; i
< bmap
->n_div
; ++i
)
3744 for (j
= 0; j
< n
; ++j
)
3745 isl_int_neg(bmap
->div
[i
][1+off
+j
], bmap
->div
[i
][1+off
+j
]);
3746 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3747 return isl_basic_map_finalize(bmap
);
3750 __isl_give isl_basic_set
*isl_basic_set_neg(__isl_take isl_basic_set
*bset
)
3752 return isl_basic_map_neg(bset
);
3755 /* Given a map A -> f(A), construct A -> -f(A).
3757 struct isl_map
*isl_map_neg(struct isl_map
*map
)
3761 map
= isl_map_cow(map
);
3765 for (i
= 0; i
< map
->n
; ++i
) {
3766 map
->p
[i
] = isl_basic_map_neg(map
->p
[i
]);
3777 __isl_give isl_set
*isl_set_neg(__isl_take isl_set
*set
)
3779 return (isl_set
*)isl_map_neg((isl_map
*)set
);
3782 /* Given a basic map A -> f(A) and an integer d, construct a basic map
3783 * A -> floor(f(A)/d).
3785 struct isl_basic_map
*isl_basic_map_floordiv(struct isl_basic_map
*bmap
,
3788 unsigned n_in
, n_out
, nparam
, total
, pos
;
3789 struct isl_basic_map
*result
= NULL
;
3790 struct isl_dim_map
*dim_map
;
3796 nparam
= isl_basic_map_n_param(bmap
);
3797 n_in
= isl_basic_map_n_in(bmap
);
3798 n_out
= isl_basic_map_n_out(bmap
);
3800 total
= nparam
+ n_in
+ n_out
+ bmap
->n_div
+ n_out
;
3801 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3802 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_param
, pos
= 0);
3803 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_in
, pos
+= nparam
);
3804 isl_dim_map_div(dim_map
, bmap
, pos
+= n_in
+ n_out
);
3805 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_out
, pos
+= bmap
->n_div
);
3807 result
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
3808 bmap
->n_div
+ n_out
,
3809 bmap
->n_eq
, bmap
->n_ineq
+ 2 * n_out
);
3810 result
= isl_basic_map_add_constraints_dim_map(result
, bmap
, dim_map
);
3811 result
= add_divs(result
, n_out
);
3812 for (i
= 0; i
< n_out
; ++i
) {
3814 j
= isl_basic_map_alloc_inequality(result
);
3817 isl_seq_clr(result
->ineq
[j
], 1+total
);
3818 isl_int_neg(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
3819 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], 1);
3820 j
= isl_basic_map_alloc_inequality(result
);
3823 isl_seq_clr(result
->ineq
[j
], 1+total
);
3824 isl_int_set(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
3825 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], -1);
3826 isl_int_sub_ui(result
->ineq
[j
][0], d
, 1);
3829 result
= isl_basic_map_simplify(result
);
3830 return isl_basic_map_finalize(result
);
3832 isl_basic_map_free(result
);
3836 /* Given a map A -> f(A) and an integer d, construct a map
3837 * A -> floor(f(A)/d).
3839 struct isl_map
*isl_map_floordiv(struct isl_map
*map
, isl_int d
)
3843 map
= isl_map_cow(map
);
3847 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
3848 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3849 for (i
= 0; i
< map
->n
; ++i
) {
3850 map
->p
[i
] = isl_basic_map_floordiv(map
->p
[i
], d
);
3861 /* Given a map A -> f(A) and an integer d, construct a map
3862 * A -> floor(f(A)/d).
3864 __isl_give isl_map
*isl_map_floordiv_val(__isl_take isl_map
*map
,
3865 __isl_take isl_val
*d
)
3869 if (!isl_val_is_int(d
))
3870 isl_die(isl_val_get_ctx(d
), isl_error_invalid
,
3871 "expecting integer denominator", goto error
);
3872 map
= isl_map_floordiv(map
, d
->n
);
3881 static struct isl_basic_map
*var_equal(struct isl_basic_map
*bmap
, unsigned pos
)
3887 i
= isl_basic_map_alloc_equality(bmap
);
3890 nparam
= isl_basic_map_n_param(bmap
);
3891 n_in
= isl_basic_map_n_in(bmap
);
3892 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3893 isl_int_set_si(bmap
->eq
[i
][1+nparam
+pos
], -1);
3894 isl_int_set_si(bmap
->eq
[i
][1+nparam
+n_in
+pos
], 1);
3895 return isl_basic_map_finalize(bmap
);
3897 isl_basic_map_free(bmap
);
3901 /* Add a constraints to "bmap" expressing i_pos < o_pos
3903 static struct isl_basic_map
*var_less(struct isl_basic_map
*bmap
, unsigned pos
)
3909 i
= isl_basic_map_alloc_inequality(bmap
);
3912 nparam
= isl_basic_map_n_param(bmap
);
3913 n_in
= isl_basic_map_n_in(bmap
);
3914 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3915 isl_int_set_si(bmap
->ineq
[i
][0], -1);
3916 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
3917 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
3918 return isl_basic_map_finalize(bmap
);
3920 isl_basic_map_free(bmap
);
3924 /* Add a constraint to "bmap" expressing i_pos <= o_pos
3926 static __isl_give isl_basic_map
*var_less_or_equal(
3927 __isl_take isl_basic_map
*bmap
, unsigned pos
)
3933 i
= isl_basic_map_alloc_inequality(bmap
);
3936 nparam
= isl_basic_map_n_param(bmap
);
3937 n_in
= isl_basic_map_n_in(bmap
);
3938 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3939 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
3940 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
3941 return isl_basic_map_finalize(bmap
);
3943 isl_basic_map_free(bmap
);
3947 /* Add a constraints to "bmap" expressing i_pos > o_pos
3949 static struct isl_basic_map
*var_more(struct isl_basic_map
*bmap
, unsigned pos
)
3955 i
= isl_basic_map_alloc_inequality(bmap
);
3958 nparam
= isl_basic_map_n_param(bmap
);
3959 n_in
= isl_basic_map_n_in(bmap
);
3960 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3961 isl_int_set_si(bmap
->ineq
[i
][0], -1);
3962 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
3963 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
3964 return isl_basic_map_finalize(bmap
);
3966 isl_basic_map_free(bmap
);
3970 /* Add a constraint to "bmap" expressing i_pos >= o_pos
3972 static __isl_give isl_basic_map
*var_more_or_equal(
3973 __isl_take isl_basic_map
*bmap
, unsigned pos
)
3979 i
= isl_basic_map_alloc_inequality(bmap
);
3982 nparam
= isl_basic_map_n_param(bmap
);
3983 n_in
= isl_basic_map_n_in(bmap
);
3984 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
3985 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
3986 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
3987 return isl_basic_map_finalize(bmap
);
3989 isl_basic_map_free(bmap
);
3993 __isl_give isl_basic_map
*isl_basic_map_equal(
3994 __isl_take isl_space
*dim
, unsigned n_equal
)
3997 struct isl_basic_map
*bmap
;
3998 bmap
= isl_basic_map_alloc_space(dim
, 0, n_equal
, 0);
4001 for (i
= 0; i
< n_equal
&& bmap
; ++i
)
4002 bmap
= var_equal(bmap
, i
);
4003 return isl_basic_map_finalize(bmap
);
4006 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
4008 __isl_give isl_basic_map
*isl_basic_map_less_at(__isl_take isl_space
*dim
,
4012 struct isl_basic_map
*bmap
;
4013 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4016 for (i
= 0; i
< pos
&& bmap
; ++i
)
4017 bmap
= var_equal(bmap
, i
);
4019 bmap
= var_less(bmap
, pos
);
4020 return isl_basic_map_finalize(bmap
);
4023 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
4025 __isl_give isl_basic_map
*isl_basic_map_less_or_equal_at(
4026 __isl_take isl_space
*dim
, unsigned pos
)
4029 isl_basic_map
*bmap
;
4031 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4032 for (i
= 0; i
< pos
; ++i
)
4033 bmap
= var_equal(bmap
, i
);
4034 bmap
= var_less_or_equal(bmap
, pos
);
4035 return isl_basic_map_finalize(bmap
);
4038 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
4040 __isl_give isl_basic_map
*isl_basic_map_more_at(__isl_take isl_space
*dim
,
4044 struct isl_basic_map
*bmap
;
4045 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4048 for (i
= 0; i
< pos
&& bmap
; ++i
)
4049 bmap
= var_equal(bmap
, i
);
4051 bmap
= var_more(bmap
, pos
);
4052 return isl_basic_map_finalize(bmap
);
4055 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
4057 __isl_give isl_basic_map
*isl_basic_map_more_or_equal_at(
4058 __isl_take isl_space
*dim
, unsigned pos
)
4061 isl_basic_map
*bmap
;
4063 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4064 for (i
= 0; i
< pos
; ++i
)
4065 bmap
= var_equal(bmap
, i
);
4066 bmap
= var_more_or_equal(bmap
, pos
);
4067 return isl_basic_map_finalize(bmap
);
4070 static __isl_give isl_map
*map_lex_lte_first(__isl_take isl_space
*dims
,
4071 unsigned n
, int equal
)
4073 struct isl_map
*map
;
4076 if (n
== 0 && equal
)
4077 return isl_map_universe(dims
);
4079 map
= isl_map_alloc_space(isl_space_copy(dims
), n
, ISL_MAP_DISJOINT
);
4081 for (i
= 0; i
+ 1 < n
; ++i
)
4082 map
= isl_map_add_basic_map(map
,
4083 isl_basic_map_less_at(isl_space_copy(dims
), i
));
4086 map
= isl_map_add_basic_map(map
,
4087 isl_basic_map_less_or_equal_at(dims
, n
- 1));
4089 map
= isl_map_add_basic_map(map
,
4090 isl_basic_map_less_at(dims
, n
- 1));
4092 isl_space_free(dims
);
4097 static __isl_give isl_map
*map_lex_lte(__isl_take isl_space
*dims
, int equal
)
4101 return map_lex_lte_first(dims
, dims
->n_out
, equal
);
4104 __isl_give isl_map
*isl_map_lex_lt_first(__isl_take isl_space
*dim
, unsigned n
)
4106 return map_lex_lte_first(dim
, n
, 0);
4109 __isl_give isl_map
*isl_map_lex_le_first(__isl_take isl_space
*dim
, unsigned n
)
4111 return map_lex_lte_first(dim
, n
, 1);
4114 __isl_give isl_map
*isl_map_lex_lt(__isl_take isl_space
*set_dim
)
4116 return map_lex_lte(isl_space_map_from_set(set_dim
), 0);
4119 __isl_give isl_map
*isl_map_lex_le(__isl_take isl_space
*set_dim
)
4121 return map_lex_lte(isl_space_map_from_set(set_dim
), 1);
4124 static __isl_give isl_map
*map_lex_gte_first(__isl_take isl_space
*dims
,
4125 unsigned n
, int equal
)
4127 struct isl_map
*map
;
4130 if (n
== 0 && equal
)
4131 return isl_map_universe(dims
);
4133 map
= isl_map_alloc_space(isl_space_copy(dims
), n
, ISL_MAP_DISJOINT
);
4135 for (i
= 0; i
+ 1 < n
; ++i
)
4136 map
= isl_map_add_basic_map(map
,
4137 isl_basic_map_more_at(isl_space_copy(dims
), i
));
4140 map
= isl_map_add_basic_map(map
,
4141 isl_basic_map_more_or_equal_at(dims
, n
- 1));
4143 map
= isl_map_add_basic_map(map
,
4144 isl_basic_map_more_at(dims
, n
- 1));
4146 isl_space_free(dims
);
4151 static __isl_give isl_map
*map_lex_gte(__isl_take isl_space
*dims
, int equal
)
4155 return map_lex_gte_first(dims
, dims
->n_out
, equal
);
4158 __isl_give isl_map
*isl_map_lex_gt_first(__isl_take isl_space
*dim
, unsigned n
)
4160 return map_lex_gte_first(dim
, n
, 0);
4163 __isl_give isl_map
*isl_map_lex_ge_first(__isl_take isl_space
*dim
, unsigned n
)
4165 return map_lex_gte_first(dim
, n
, 1);
4168 __isl_give isl_map
*isl_map_lex_gt(__isl_take isl_space
*set_dim
)
4170 return map_lex_gte(isl_space_map_from_set(set_dim
), 0);
4173 __isl_give isl_map
*isl_map_lex_ge(__isl_take isl_space
*set_dim
)
4175 return map_lex_gte(isl_space_map_from_set(set_dim
), 1);
4178 __isl_give isl_map
*isl_set_lex_le_set(__isl_take isl_set
*set1
,
4179 __isl_take isl_set
*set2
)
4182 map
= isl_map_lex_le(isl_set_get_space(set1
));
4183 map
= isl_map_intersect_domain(map
, set1
);
4184 map
= isl_map_intersect_range(map
, set2
);
4188 __isl_give isl_map
*isl_set_lex_lt_set(__isl_take isl_set
*set1
,
4189 __isl_take isl_set
*set2
)
4192 map
= isl_map_lex_lt(isl_set_get_space(set1
));
4193 map
= isl_map_intersect_domain(map
, set1
);
4194 map
= isl_map_intersect_range(map
, set2
);
4198 __isl_give isl_map
*isl_set_lex_ge_set(__isl_take isl_set
*set1
,
4199 __isl_take isl_set
*set2
)
4202 map
= isl_map_lex_ge(isl_set_get_space(set1
));
4203 map
= isl_map_intersect_domain(map
, set1
);
4204 map
= isl_map_intersect_range(map
, set2
);
4208 __isl_give isl_map
*isl_set_lex_gt_set(__isl_take isl_set
*set1
,
4209 __isl_take isl_set
*set2
)
4212 map
= isl_map_lex_gt(isl_set_get_space(set1
));
4213 map
= isl_map_intersect_domain(map
, set1
);
4214 map
= isl_map_intersect_range(map
, set2
);
4218 __isl_give isl_map
*isl_map_lex_le_map(__isl_take isl_map
*map1
,
4219 __isl_take isl_map
*map2
)
4222 map
= isl_map_lex_le(isl_space_range(isl_map_get_space(map1
)));
4223 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4224 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4228 __isl_give isl_map
*isl_map_lex_lt_map(__isl_take isl_map
*map1
,
4229 __isl_take isl_map
*map2
)
4232 map
= isl_map_lex_lt(isl_space_range(isl_map_get_space(map1
)));
4233 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4234 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4238 __isl_give isl_map
*isl_map_lex_ge_map(__isl_take isl_map
*map1
,
4239 __isl_take isl_map
*map2
)
4242 map
= isl_map_lex_ge(isl_space_range(isl_map_get_space(map1
)));
4243 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4244 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4248 __isl_give isl_map
*isl_map_lex_gt_map(__isl_take isl_map
*map1
,
4249 __isl_take isl_map
*map2
)
4252 map
= isl_map_lex_gt(isl_space_range(isl_map_get_space(map1
)));
4253 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4254 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4258 __isl_give isl_basic_map
*isl_basic_map_from_basic_set(
4259 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*dim
)
4261 struct isl_basic_map
*bmap
;
4263 bset
= isl_basic_set_cow(bset
);
4267 isl_assert(bset
->ctx
, isl_space_compatible(bset
->dim
, dim
), goto error
);
4268 isl_space_free(bset
->dim
);
4269 bmap
= (struct isl_basic_map
*) bset
;
4271 return isl_basic_map_finalize(bmap
);
4273 isl_basic_set_free(bset
);
4274 isl_space_free(dim
);
4278 struct isl_basic_set
*isl_basic_set_from_basic_map(struct isl_basic_map
*bmap
)
4282 if (bmap
->dim
->n_in
== 0)
4283 return (struct isl_basic_set
*)bmap
;
4284 bmap
= isl_basic_map_cow(bmap
);
4287 bmap
->dim
= isl_space_as_set_space(bmap
->dim
);
4290 bmap
= isl_basic_map_finalize(bmap
);
4291 return (struct isl_basic_set
*)bmap
;
4293 isl_basic_map_free(bmap
);
4297 /* For a div d = floor(f/m), add the constraints
4300 * -(f-(n-1)) + m d >= 0
4302 * Note that the second constraint is the negation of
4306 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map
*bmap
,
4307 unsigned pos
, isl_int
*div
)
4310 unsigned total
= isl_basic_map_total_dim(bmap
);
4312 i
= isl_basic_map_alloc_inequality(bmap
);
4315 isl_seq_cpy(bmap
->ineq
[i
], div
+ 1, 1 + total
);
4316 isl_int_neg(bmap
->ineq
[i
][1 + pos
], div
[0]);
4318 j
= isl_basic_map_alloc_inequality(bmap
);
4321 isl_seq_neg(bmap
->ineq
[j
], bmap
->ineq
[i
], 1 + total
);
4322 isl_int_add(bmap
->ineq
[j
][0], bmap
->ineq
[j
][0], bmap
->ineq
[j
][1 + pos
]);
4323 isl_int_sub_ui(bmap
->ineq
[j
][0], bmap
->ineq
[j
][0], 1);
4327 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set
*bset
,
4328 unsigned pos
, isl_int
*div
)
4330 return isl_basic_map_add_div_constraints_var((isl_basic_map
*)bset
,
4334 int isl_basic_map_add_div_constraints(struct isl_basic_map
*bmap
, unsigned div
)
4336 unsigned total
= isl_basic_map_total_dim(bmap
);
4337 unsigned div_pos
= total
- bmap
->n_div
+ div
;
4339 return isl_basic_map_add_div_constraints_var(bmap
, div_pos
,
4343 int isl_basic_set_add_div_constraints(struct isl_basic_set
*bset
, unsigned div
)
4345 return isl_basic_map_add_div_constraints(bset
, div
);
4348 struct isl_basic_set
*isl_basic_map_underlying_set(
4349 struct isl_basic_map
*bmap
)
4353 if (bmap
->dim
->nparam
== 0 && bmap
->dim
->n_in
== 0 &&
4355 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_in
) &&
4356 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_out
))
4357 return (struct isl_basic_set
*)bmap
;
4358 bmap
= isl_basic_map_cow(bmap
);
4361 bmap
->dim
= isl_space_underlying(bmap
->dim
, bmap
->n_div
);
4364 bmap
->extra
-= bmap
->n_div
;
4366 bmap
= isl_basic_map_finalize(bmap
);
4367 return (struct isl_basic_set
*)bmap
;
4369 isl_basic_map_free(bmap
);
4373 __isl_give isl_basic_set
*isl_basic_set_underlying_set(
4374 __isl_take isl_basic_set
*bset
)
4376 return isl_basic_map_underlying_set((isl_basic_map
*)bset
);
4379 struct isl_basic_map
*isl_basic_map_overlying_set(
4380 struct isl_basic_set
*bset
, struct isl_basic_map
*like
)
4382 struct isl_basic_map
*bmap
;
4383 struct isl_ctx
*ctx
;
4390 isl_assert(ctx
, bset
->n_div
== 0, goto error
);
4391 isl_assert(ctx
, isl_basic_set_n_param(bset
) == 0, goto error
);
4392 isl_assert(ctx
, bset
->dim
->n_out
== isl_basic_map_total_dim(like
),
4394 if (isl_space_is_equal(bset
->dim
, like
->dim
) && like
->n_div
== 0) {
4395 isl_basic_map_free(like
);
4396 return (struct isl_basic_map
*)bset
;
4398 bset
= isl_basic_set_cow(bset
);
4401 total
= bset
->dim
->n_out
+ bset
->extra
;
4402 bmap
= (struct isl_basic_map
*)bset
;
4403 isl_space_free(bmap
->dim
);
4404 bmap
->dim
= isl_space_copy(like
->dim
);
4407 bmap
->n_div
= like
->n_div
;
4408 bmap
->extra
+= like
->n_div
;
4412 ltotal
= total
- bmap
->extra
+ like
->extra
;
4415 bmap
->block2
= isl_blk_extend(ctx
, bmap
->block2
,
4416 bmap
->extra
* (1 + 1 + total
));
4417 if (isl_blk_is_error(bmap
->block2
))
4419 div
= isl_realloc_array(ctx
, bmap
->div
, isl_int
*, bmap
->extra
);
4423 for (i
= 0; i
< bmap
->extra
; ++i
)
4424 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + 1 + total
);
4425 for (i
= 0; i
< like
->n_div
; ++i
) {
4426 isl_seq_cpy(bmap
->div
[i
], like
->div
[i
], 1 + 1 + ltotal
);
4427 isl_seq_clr(bmap
->div
[i
]+1+1+ltotal
, total
- ltotal
);
4429 bmap
= isl_basic_map_extend_constraints(bmap
,
4430 0, 2 * like
->n_div
);
4431 for (i
= 0; i
< like
->n_div
; ++i
) {
4434 if (isl_int_is_zero(bmap
->div
[i
][0]))
4436 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
4437 bmap
= isl_basic_map_free(bmap
);
4440 isl_basic_map_free(like
);
4441 bmap
= isl_basic_map_simplify(bmap
);
4442 bmap
= isl_basic_map_finalize(bmap
);
4445 isl_basic_map_free(like
);
4446 isl_basic_set_free(bset
);
4450 struct isl_basic_set
*isl_basic_set_from_underlying_set(
4451 struct isl_basic_set
*bset
, struct isl_basic_set
*like
)
4453 return (struct isl_basic_set
*)
4454 isl_basic_map_overlying_set(bset
, (struct isl_basic_map
*)like
);
4457 struct isl_set
*isl_set_from_underlying_set(
4458 struct isl_set
*set
, struct isl_basic_set
*like
)
4464 isl_assert(set
->ctx
, set
->dim
->n_out
== isl_basic_set_total_dim(like
),
4466 if (isl_space_is_equal(set
->dim
, like
->dim
) && like
->n_div
== 0) {
4467 isl_basic_set_free(like
);
4470 set
= isl_set_cow(set
);
4473 for (i
= 0; i
< set
->n
; ++i
) {
4474 set
->p
[i
] = isl_basic_set_from_underlying_set(set
->p
[i
],
4475 isl_basic_set_copy(like
));
4479 isl_space_free(set
->dim
);
4480 set
->dim
= isl_space_copy(like
->dim
);
4483 isl_basic_set_free(like
);
4486 isl_basic_set_free(like
);
4491 struct isl_set
*isl_map_underlying_set(struct isl_map
*map
)
4495 map
= isl_map_cow(map
);
4498 map
->dim
= isl_space_cow(map
->dim
);
4502 for (i
= 1; i
< map
->n
; ++i
)
4503 isl_assert(map
->ctx
, map
->p
[0]->n_div
== map
->p
[i
]->n_div
,
4505 for (i
= 0; i
< map
->n
; ++i
) {
4506 map
->p
[i
] = (struct isl_basic_map
*)
4507 isl_basic_map_underlying_set(map
->p
[i
]);
4512 map
->dim
= isl_space_underlying(map
->dim
, 0);
4514 isl_space_free(map
->dim
);
4515 map
->dim
= isl_space_copy(map
->p
[0]->dim
);
4519 return (struct isl_set
*)map
;
4525 struct isl_set
*isl_set_to_underlying_set(struct isl_set
*set
)
4527 return (struct isl_set
*)isl_map_underlying_set((struct isl_map
*)set
);
4530 __isl_give isl_basic_map
*isl_basic_map_reset_space(
4531 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*dim
)
4533 bmap
= isl_basic_map_cow(bmap
);
4537 isl_space_free(bmap
->dim
);
4540 bmap
= isl_basic_map_finalize(bmap
);
4544 isl_basic_map_free(bmap
);
4545 isl_space_free(dim
);
4549 __isl_give isl_basic_set
*isl_basic_set_reset_space(
4550 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*dim
)
4552 return (isl_basic_set
*)isl_basic_map_reset_space((isl_basic_map
*)bset
,
4556 __isl_give isl_map
*isl_map_reset_space(__isl_take isl_map
*map
,
4557 __isl_take isl_space
*dim
)
4561 map
= isl_map_cow(map
);
4565 for (i
= 0; i
< map
->n
; ++i
) {
4566 map
->p
[i
] = isl_basic_map_reset_space(map
->p
[i
],
4567 isl_space_copy(dim
));
4571 isl_space_free(map
->dim
);
4577 isl_space_free(dim
);
4581 __isl_give isl_set
*isl_set_reset_space(__isl_take isl_set
*set
,
4582 __isl_take isl_space
*dim
)
4584 return (struct isl_set
*) isl_map_reset_space((struct isl_map
*)set
, dim
);
4587 /* Compute the parameter domain of the given basic set.
4589 __isl_give isl_basic_set
*isl_basic_set_params(__isl_take isl_basic_set
*bset
)
4594 if (isl_basic_set_is_params(bset
))
4597 n
= isl_basic_set_dim(bset
, isl_dim_set
);
4598 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
4599 space
= isl_basic_set_get_space(bset
);
4600 space
= isl_space_params(space
);
4601 bset
= isl_basic_set_reset_space(bset
, space
);
4605 /* Construct a zero-dimensional basic set with the given parameter domain.
4607 __isl_give isl_basic_set
*isl_basic_set_from_params(
4608 __isl_take isl_basic_set
*bset
)
4611 space
= isl_basic_set_get_space(bset
);
4612 space
= isl_space_set_from_params(space
);
4613 bset
= isl_basic_set_reset_space(bset
, space
);
4617 /* Compute the parameter domain of the given set.
4619 __isl_give isl_set
*isl_set_params(__isl_take isl_set
*set
)
4624 if (isl_set_is_params(set
))
4627 n
= isl_set_dim(set
, isl_dim_set
);
4628 set
= isl_set_project_out(set
, isl_dim_set
, 0, n
);
4629 space
= isl_set_get_space(set
);
4630 space
= isl_space_params(space
);
4631 set
= isl_set_reset_space(set
, space
);
4635 /* Construct a zero-dimensional set with the given parameter domain.
4637 __isl_give isl_set
*isl_set_from_params(__isl_take isl_set
*set
)
4640 space
= isl_set_get_space(set
);
4641 space
= isl_space_set_from_params(space
);
4642 set
= isl_set_reset_space(set
, space
);
4646 /* Compute the parameter domain of the given map.
4648 __isl_give isl_set
*isl_map_params(__isl_take isl_map
*map
)
4653 n
= isl_map_dim(map
, isl_dim_in
);
4654 map
= isl_map_project_out(map
, isl_dim_in
, 0, n
);
4655 n
= isl_map_dim(map
, isl_dim_out
);
4656 map
= isl_map_project_out(map
, isl_dim_out
, 0, n
);
4657 space
= isl_map_get_space(map
);
4658 space
= isl_space_params(space
);
4659 map
= isl_map_reset_space(map
, space
);
4663 struct isl_basic_set
*isl_basic_map_domain(struct isl_basic_map
*bmap
)
4666 struct isl_basic_set
*domain
;
4672 dim
= isl_space_domain(isl_basic_map_get_space(bmap
));
4674 n_in
= isl_basic_map_n_in(bmap
);
4675 n_out
= isl_basic_map_n_out(bmap
);
4676 domain
= isl_basic_set_from_basic_map(bmap
);
4677 domain
= isl_basic_set_project_out(domain
, isl_dim_set
, n_in
, n_out
);
4679 domain
= isl_basic_set_reset_space(domain
, dim
);
4684 int isl_basic_map_may_be_set(__isl_keep isl_basic_map
*bmap
)
4688 return isl_space_may_be_set(bmap
->dim
);
4691 /* Is this basic map actually a set?
4692 * Users should never call this function. Outside of isl,
4693 * the type should indicate whether something is a set or a map.
4695 int isl_basic_map_is_set(__isl_keep isl_basic_map
*bmap
)
4699 return isl_space_is_set(bmap
->dim
);
4702 struct isl_basic_set
*isl_basic_map_range(struct isl_basic_map
*bmap
)
4706 if (isl_basic_map_is_set(bmap
))
4708 return isl_basic_map_domain(isl_basic_map_reverse(bmap
));
4711 __isl_give isl_basic_map
*isl_basic_map_domain_map(
4712 __isl_take isl_basic_map
*bmap
)
4716 isl_basic_map
*domain
;
4717 int nparam
, n_in
, n_out
;
4720 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4721 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4722 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4724 dim
= isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap
)));
4725 domain
= isl_basic_map_universe(dim
);
4727 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
4728 bmap
= isl_basic_map_apply_range(bmap
, domain
);
4729 bmap
= isl_basic_map_extend_constraints(bmap
, n_in
, 0);
4731 total
= isl_basic_map_total_dim(bmap
);
4733 for (i
= 0; i
< n_in
; ++i
) {
4734 k
= isl_basic_map_alloc_equality(bmap
);
4737 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
4738 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], -1);
4739 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ n_out
+ i
], 1);
4742 bmap
= isl_basic_map_gauss(bmap
, NULL
);
4743 return isl_basic_map_finalize(bmap
);
4745 isl_basic_map_free(bmap
);
4749 __isl_give isl_basic_map
*isl_basic_map_range_map(
4750 __isl_take isl_basic_map
*bmap
)
4754 isl_basic_map
*range
;
4755 int nparam
, n_in
, n_out
;
4758 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4759 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4760 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4762 dim
= isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap
)));
4763 range
= isl_basic_map_universe(dim
);
4765 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
4766 bmap
= isl_basic_map_apply_range(bmap
, range
);
4767 bmap
= isl_basic_map_extend_constraints(bmap
, n_out
, 0);
4769 total
= isl_basic_map_total_dim(bmap
);
4771 for (i
= 0; i
< n_out
; ++i
) {
4772 k
= isl_basic_map_alloc_equality(bmap
);
4775 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
4776 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ i
], -1);
4777 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ n_out
+ i
], 1);
4780 bmap
= isl_basic_map_gauss(bmap
, NULL
);
4781 return isl_basic_map_finalize(bmap
);
4783 isl_basic_map_free(bmap
);
4787 int isl_map_may_be_set(__isl_keep isl_map
*map
)
4791 return isl_space_may_be_set(map
->dim
);
4794 /* Is this map actually a set?
4795 * Users should never call this function. Outside of isl,
4796 * the type should indicate whether something is a set or a map.
4798 int isl_map_is_set(__isl_keep isl_map
*map
)
4802 return isl_space_is_set(map
->dim
);
4805 struct isl_set
*isl_map_range(struct isl_map
*map
)
4808 struct isl_set
*set
;
4812 if (isl_map_is_set(map
))
4813 return (isl_set
*)map
;
4815 map
= isl_map_cow(map
);
4819 set
= (struct isl_set
*) map
;
4820 set
->dim
= isl_space_range(set
->dim
);
4823 for (i
= 0; i
< map
->n
; ++i
) {
4824 set
->p
[i
] = isl_basic_map_range(map
->p
[i
]);
4828 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
4829 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
4836 __isl_give isl_map
*isl_map_domain_map(__isl_take isl_map
*map
)
4839 isl_space
*domain_dim
;
4841 map
= isl_map_cow(map
);
4845 domain_dim
= isl_space_from_range(isl_space_domain(isl_map_get_space(map
)));
4846 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
4847 map
->dim
= isl_space_join(map
->dim
, domain_dim
);
4850 for (i
= 0; i
< map
->n
; ++i
) {
4851 map
->p
[i
] = isl_basic_map_domain_map(map
->p
[i
]);
4855 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
4856 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4863 __isl_give isl_map
*isl_map_range_map(__isl_take isl_map
*map
)
4866 isl_space
*range_dim
;
4868 map
= isl_map_cow(map
);
4872 range_dim
= isl_space_range(isl_map_get_space(map
));
4873 range_dim
= isl_space_from_range(range_dim
);
4874 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
4875 map
->dim
= isl_space_join(map
->dim
, range_dim
);
4878 for (i
= 0; i
< map
->n
; ++i
) {
4879 map
->p
[i
] = isl_basic_map_range_map(map
->p
[i
]);
4883 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
4884 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4891 __isl_give isl_map
*isl_map_from_set(__isl_take isl_set
*set
,
4892 __isl_take isl_space
*dim
)
4895 struct isl_map
*map
= NULL
;
4897 set
= isl_set_cow(set
);
4900 isl_assert(set
->ctx
, isl_space_compatible(set
->dim
, dim
), goto error
);
4901 map
= (struct isl_map
*)set
;
4902 for (i
= 0; i
< set
->n
; ++i
) {
4903 map
->p
[i
] = isl_basic_map_from_basic_set(
4904 set
->p
[i
], isl_space_copy(dim
));
4908 isl_space_free(map
->dim
);
4912 isl_space_free(dim
);
4917 __isl_give isl_basic_map
*isl_basic_map_from_domain(
4918 __isl_take isl_basic_set
*bset
)
4920 return isl_basic_map_reverse(isl_basic_map_from_range(bset
));
4923 __isl_give isl_basic_map
*isl_basic_map_from_range(
4924 __isl_take isl_basic_set
*bset
)
4927 space
= isl_basic_set_get_space(bset
);
4928 space
= isl_space_from_range(space
);
4929 bset
= isl_basic_set_reset_space(bset
, space
);
4930 return (isl_basic_map
*)bset
;
4933 struct isl_map
*isl_map_from_range(struct isl_set
*set
)
4936 space
= isl_set_get_space(set
);
4937 space
= isl_space_from_range(space
);
4938 set
= isl_set_reset_space(set
, space
);
4939 return (struct isl_map
*)set
;
4942 __isl_give isl_map
*isl_map_from_domain(__isl_take isl_set
*set
)
4944 return isl_map_reverse(isl_map_from_range(set
));
4947 __isl_give isl_basic_map
*isl_basic_map_from_domain_and_range(
4948 __isl_take isl_basic_set
*domain
, __isl_take isl_basic_set
*range
)
4950 return isl_basic_map_apply_range(isl_basic_map_reverse(domain
), range
);
4953 __isl_give isl_map
*isl_map_from_domain_and_range(__isl_take isl_set
*domain
,
4954 __isl_take isl_set
*range
)
4956 return isl_map_apply_range(isl_map_reverse(domain
), range
);
4959 struct isl_set
*isl_set_from_map(struct isl_map
*map
)
4962 struct isl_set
*set
= NULL
;
4966 map
= isl_map_cow(map
);
4969 map
->dim
= isl_space_as_set_space(map
->dim
);
4972 set
= (struct isl_set
*)map
;
4973 for (i
= 0; i
< map
->n
; ++i
) {
4974 set
->p
[i
] = isl_basic_set_from_basic_map(map
->p
[i
]);
4984 __isl_give isl_map
*isl_map_alloc_space(__isl_take isl_space
*dim
, int n
,
4987 struct isl_map
*map
;
4992 isl_die(dim
->ctx
, isl_error_internal
,
4993 "negative number of basic maps", goto error
);
4994 map
= isl_alloc(dim
->ctx
, struct isl_map
,
4995 sizeof(struct isl_map
) +
4996 (n
- 1) * sizeof(struct isl_basic_map
*));
5000 map
->ctx
= dim
->ctx
;
5001 isl_ctx_ref(map
->ctx
);
5009 isl_space_free(dim
);
5013 struct isl_map
*isl_map_alloc(struct isl_ctx
*ctx
,
5014 unsigned nparam
, unsigned in
, unsigned out
, int n
,
5017 struct isl_map
*map
;
5020 dims
= isl_space_alloc(ctx
, nparam
, in
, out
);
5024 map
= isl_map_alloc_space(dims
, n
, flags
);
5028 __isl_give isl_basic_map
*isl_basic_map_empty(__isl_take isl_space
*dim
)
5030 struct isl_basic_map
*bmap
;
5031 bmap
= isl_basic_map_alloc_space(dim
, 0, 1, 0);
5032 bmap
= isl_basic_map_set_to_empty(bmap
);
5036 __isl_give isl_basic_set
*isl_basic_set_empty(__isl_take isl_space
*dim
)
5038 struct isl_basic_set
*bset
;
5039 bset
= isl_basic_set_alloc_space(dim
, 0, 1, 0);
5040 bset
= isl_basic_set_set_to_empty(bset
);
5044 struct isl_basic_map
*isl_basic_map_empty_like(struct isl_basic_map
*model
)
5046 struct isl_basic_map
*bmap
;
5049 bmap
= isl_basic_map_alloc_space(isl_space_copy(model
->dim
), 0, 1, 0);
5050 bmap
= isl_basic_map_set_to_empty(bmap
);
5054 struct isl_basic_map
*isl_basic_map_empty_like_map(struct isl_map
*model
)
5056 struct isl_basic_map
*bmap
;
5059 bmap
= isl_basic_map_alloc_space(isl_space_copy(model
->dim
), 0, 1, 0);
5060 bmap
= isl_basic_map_set_to_empty(bmap
);
5064 struct isl_basic_set
*isl_basic_set_empty_like(struct isl_basic_set
*model
)
5066 struct isl_basic_set
*bset
;
5069 bset
= isl_basic_set_alloc_space(isl_space_copy(model
->dim
), 0, 1, 0);
5070 bset
= isl_basic_set_set_to_empty(bset
);
5074 __isl_give isl_basic_map
*isl_basic_map_universe(__isl_take isl_space
*dim
)
5076 struct isl_basic_map
*bmap
;
5077 bmap
= isl_basic_map_alloc_space(dim
, 0, 0, 0);
5078 bmap
= isl_basic_map_finalize(bmap
);
5082 __isl_give isl_basic_set
*isl_basic_set_universe(__isl_take isl_space
*dim
)
5084 struct isl_basic_set
*bset
;
5085 bset
= isl_basic_set_alloc_space(dim
, 0, 0, 0);
5086 bset
= isl_basic_set_finalize(bset
);
5090 __isl_give isl_basic_map
*isl_basic_map_nat_universe(__isl_take isl_space
*dim
)
5093 unsigned total
= isl_space_dim(dim
, isl_dim_all
);
5094 isl_basic_map
*bmap
;
5096 bmap
= isl_basic_map_alloc_space(dim
, 0, 0, total
);
5097 for (i
= 0; i
< total
; ++i
) {
5098 int k
= isl_basic_map_alloc_inequality(bmap
);
5101 isl_seq_clr(bmap
->ineq
[k
], 1 + total
);
5102 isl_int_set_si(bmap
->ineq
[k
][1 + i
], 1);
5106 isl_basic_map_free(bmap
);
5110 __isl_give isl_basic_set
*isl_basic_set_nat_universe(__isl_take isl_space
*dim
)
5112 return isl_basic_map_nat_universe(dim
);
5115 __isl_give isl_map
*isl_map_nat_universe(__isl_take isl_space
*dim
)
5117 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim
));
5120 __isl_give isl_set
*isl_set_nat_universe(__isl_take isl_space
*dim
)
5122 return isl_map_nat_universe(dim
);
5125 __isl_give isl_basic_map
*isl_basic_map_universe_like(
5126 __isl_keep isl_basic_map
*model
)
5130 return isl_basic_map_alloc_space(isl_space_copy(model
->dim
), 0, 0, 0);
5133 struct isl_basic_set
*isl_basic_set_universe_like(struct isl_basic_set
*model
)
5137 return isl_basic_set_alloc_space(isl_space_copy(model
->dim
), 0, 0, 0);
5140 __isl_give isl_basic_set
*isl_basic_set_universe_like_set(
5141 __isl_keep isl_set
*model
)
5145 return isl_basic_set_alloc_space(isl_space_copy(model
->dim
), 0, 0, 0);
5148 __isl_give isl_map
*isl_map_empty(__isl_take isl_space
*dim
)
5150 return isl_map_alloc_space(dim
, 0, ISL_MAP_DISJOINT
);
5153 struct isl_map
*isl_map_empty_like(struct isl_map
*model
)
5157 return isl_map_alloc_space(isl_space_copy(model
->dim
), 0, ISL_MAP_DISJOINT
);
5160 struct isl_map
*isl_map_empty_like_basic_map(struct isl_basic_map
*model
)
5164 return isl_map_alloc_space(isl_space_copy(model
->dim
), 0, ISL_MAP_DISJOINT
);
5167 __isl_give isl_set
*isl_set_empty(__isl_take isl_space
*dim
)
5169 return isl_set_alloc_space(dim
, 0, ISL_MAP_DISJOINT
);
5172 struct isl_set
*isl_set_empty_like(struct isl_set
*model
)
5176 return isl_set_empty(isl_space_copy(model
->dim
));
5179 __isl_give isl_map
*isl_map_universe(__isl_take isl_space
*dim
)
5181 struct isl_map
*map
;
5184 map
= isl_map_alloc_space(isl_space_copy(dim
), 1, ISL_MAP_DISJOINT
);
5185 map
= isl_map_add_basic_map(map
, isl_basic_map_universe(dim
));
5189 __isl_give isl_set
*isl_set_universe(__isl_take isl_space
*dim
)
5191 struct isl_set
*set
;
5194 set
= isl_set_alloc_space(isl_space_copy(dim
), 1, ISL_MAP_DISJOINT
);
5195 set
= isl_set_add_basic_set(set
, isl_basic_set_universe(dim
));
5199 __isl_give isl_set
*isl_set_universe_like(__isl_keep isl_set
*model
)
5203 return isl_set_universe(isl_space_copy(model
->dim
));
5206 struct isl_map
*isl_map_dup(struct isl_map
*map
)
5209 struct isl_map
*dup
;
5213 dup
= isl_map_alloc_space(isl_space_copy(map
->dim
), map
->n
, map
->flags
);
5214 for (i
= 0; i
< map
->n
; ++i
)
5215 dup
= isl_map_add_basic_map(dup
, isl_basic_map_copy(map
->p
[i
]));
5219 __isl_give isl_map
*isl_map_add_basic_map(__isl_take isl_map
*map
,
5220 __isl_take isl_basic_map
*bmap
)
5224 if (isl_basic_map_plain_is_empty(bmap
)) {
5225 isl_basic_map_free(bmap
);
5228 isl_assert(map
->ctx
, isl_space_is_equal(map
->dim
, bmap
->dim
), goto error
);
5229 isl_assert(map
->ctx
, map
->n
< map
->size
, goto error
);
5230 map
->p
[map
->n
] = bmap
;
5232 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5238 isl_basic_map_free(bmap
);
5242 void *isl_map_free(struct isl_map
*map
)
5252 isl_ctx_deref(map
->ctx
);
5253 for (i
= 0; i
< map
->n
; ++i
)
5254 isl_basic_map_free(map
->p
[i
]);
5255 isl_space_free(map
->dim
);
5261 struct isl_map
*isl_map_extend(struct isl_map
*base
,
5262 unsigned nparam
, unsigned n_in
, unsigned n_out
)
5266 base
= isl_map_cow(base
);
5270 base
->dim
= isl_space_extend(base
->dim
, nparam
, n_in
, n_out
);
5273 for (i
= 0; i
< base
->n
; ++i
) {
5274 base
->p
[i
] = isl_basic_map_extend_space(base
->p
[i
],
5275 isl_space_copy(base
->dim
), 0, 0, 0);
5285 struct isl_set
*isl_set_extend(struct isl_set
*base
,
5286 unsigned nparam
, unsigned dim
)
5288 return (struct isl_set
*)isl_map_extend((struct isl_map
*)base
,
5292 static struct isl_basic_map
*isl_basic_map_fix_pos_si(
5293 struct isl_basic_map
*bmap
, unsigned pos
, int value
)
5297 bmap
= isl_basic_map_cow(bmap
);
5298 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
5299 j
= isl_basic_map_alloc_equality(bmap
);
5302 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
5303 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
5304 isl_int_set_si(bmap
->eq
[j
][0], value
);
5305 bmap
= isl_basic_map_simplify(bmap
);
5306 return isl_basic_map_finalize(bmap
);
5308 isl_basic_map_free(bmap
);
5312 static __isl_give isl_basic_map
*isl_basic_map_fix_pos(
5313 __isl_take isl_basic_map
*bmap
, unsigned pos
, isl_int value
)
5317 bmap
= isl_basic_map_cow(bmap
);
5318 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
5319 j
= isl_basic_map_alloc_equality(bmap
);
5322 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
5323 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
5324 isl_int_set(bmap
->eq
[j
][0], value
);
5325 bmap
= isl_basic_map_simplify(bmap
);
5326 return isl_basic_map_finalize(bmap
);
5328 isl_basic_map_free(bmap
);
5332 struct isl_basic_map
*isl_basic_map_fix_si(struct isl_basic_map
*bmap
,
5333 enum isl_dim_type type
, unsigned pos
, int value
)
5337 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
5338 return isl_basic_map_fix_pos_si(bmap
,
5339 isl_basic_map_offset(bmap
, type
) + pos
, value
);
5341 isl_basic_map_free(bmap
);
5345 __isl_give isl_basic_map
*isl_basic_map_fix(__isl_take isl_basic_map
*bmap
,
5346 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5350 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
5351 return isl_basic_map_fix_pos(bmap
,
5352 isl_basic_map_offset(bmap
, type
) + pos
, value
);
5354 isl_basic_map_free(bmap
);
5358 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
5359 * to be equal to "v".
5361 __isl_give isl_basic_map
*isl_basic_map_fix_val(__isl_take isl_basic_map
*bmap
,
5362 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5366 if (!isl_val_is_int(v
))
5367 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
5368 "expecting integer value", goto error
);
5369 if (pos
>= isl_basic_map_dim(bmap
, type
))
5370 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
5371 "index out of bounds", goto error
);
5372 pos
+= isl_basic_map_offset(bmap
, type
);
5373 bmap
= isl_basic_map_fix_pos(bmap
, pos
, v
->n
);
5377 isl_basic_map_free(bmap
);
5382 /* Fix the value of the variable at position "pos" of type "type" of "bset"
5383 * to be equal to "v".
5385 __isl_give isl_basic_set
*isl_basic_set_fix_val(__isl_take isl_basic_set
*bset
,
5386 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5388 return isl_basic_map_fix_val(bset
, type
, pos
, v
);
5391 struct isl_basic_set
*isl_basic_set_fix_si(struct isl_basic_set
*bset
,
5392 enum isl_dim_type type
, unsigned pos
, int value
)
5394 return (struct isl_basic_set
*)
5395 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
5399 __isl_give isl_basic_set
*isl_basic_set_fix(__isl_take isl_basic_set
*bset
,
5400 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5402 return (struct isl_basic_set
*)
5403 isl_basic_map_fix((struct isl_basic_map
*)bset
,
5407 struct isl_basic_map
*isl_basic_map_fix_input_si(struct isl_basic_map
*bmap
,
5408 unsigned input
, int value
)
5410 return isl_basic_map_fix_si(bmap
, isl_dim_in
, input
, value
);
5413 struct isl_basic_set
*isl_basic_set_fix_dim_si(struct isl_basic_set
*bset
,
5414 unsigned dim
, int value
)
5416 return (struct isl_basic_set
*)
5417 isl_basic_map_fix_si((struct isl_basic_map
*)bset
,
5418 isl_dim_set
, dim
, value
);
5421 static int remove_if_empty(__isl_keep isl_map
*map
, int i
)
5423 int empty
= isl_basic_map_plain_is_empty(map
->p
[i
]);
5430 isl_basic_map_free(map
->p
[i
]);
5431 if (i
!= map
->n
- 1) {
5432 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5433 map
->p
[i
] = map
->p
[map
->n
- 1];
5440 /* Perform "fn" on each basic map of "map", where we may not be holding
5441 * the only reference to "map".
5442 * In particular, "fn" should be a semantics preserving operation
5443 * that we want to apply to all copies of "map". We therefore need
5444 * to be careful not to modify "map" in a way that breaks "map"
5445 * in case anything goes wrong.
5447 __isl_give isl_map
*isl_map_inline_foreach_basic_map(__isl_take isl_map
*map
,
5448 __isl_give isl_basic_map
*(*fn
)(__isl_take isl_basic_map
*bmap
))
5450 struct isl_basic_map
*bmap
;
5456 for (i
= map
->n
- 1; i
>= 0; --i
) {
5457 bmap
= isl_basic_map_copy(map
->p
[i
]);
5461 isl_basic_map_free(map
->p
[i
]);
5463 if (remove_if_empty(map
, i
) < 0)
5473 struct isl_map
*isl_map_fix_si(struct isl_map
*map
,
5474 enum isl_dim_type type
, unsigned pos
, int value
)
5478 map
= isl_map_cow(map
);
5482 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
5483 for (i
= map
->n
- 1; i
>= 0; --i
) {
5484 map
->p
[i
] = isl_basic_map_fix_si(map
->p
[i
], type
, pos
, value
);
5485 if (remove_if_empty(map
, i
) < 0)
5488 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5495 __isl_give isl_set
*isl_set_fix_si(__isl_take isl_set
*set
,
5496 enum isl_dim_type type
, unsigned pos
, int value
)
5498 return (struct isl_set
*)
5499 isl_map_fix_si((struct isl_map
*)set
, type
, pos
, value
);
5502 __isl_give isl_map
*isl_map_fix(__isl_take isl_map
*map
,
5503 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5507 map
= isl_map_cow(map
);
5511 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
5512 for (i
= 0; i
< map
->n
; ++i
) {
5513 map
->p
[i
] = isl_basic_map_fix(map
->p
[i
], type
, pos
, value
);
5517 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5524 __isl_give isl_set
*isl_set_fix(__isl_take isl_set
*set
,
5525 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5527 return (struct isl_set
*)isl_map_fix((isl_map
*)set
, type
, pos
, value
);
5530 /* Fix the value of the variable at position "pos" of type "type" of "map"
5531 * to be equal to "v".
5533 __isl_give isl_map
*isl_map_fix_val(__isl_take isl_map
*map
,
5534 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5538 map
= isl_map_cow(map
);
5542 if (!isl_val_is_int(v
))
5543 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
5544 "expecting integer value", goto error
);
5545 if (pos
>= isl_map_dim(map
, type
))
5546 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
5547 "index out of bounds", goto error
);
5548 for (i
= map
->n
- 1; i
>= 0; --i
) {
5549 map
->p
[i
] = isl_basic_map_fix_val(map
->p
[i
], type
, pos
,
5551 if (remove_if_empty(map
, i
) < 0)
5554 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5563 /* Fix the value of the variable at position "pos" of type "type" of "set"
5564 * to be equal to "v".
5566 __isl_give isl_set
*isl_set_fix_val(__isl_take isl_set
*set
,
5567 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5569 return isl_map_fix_val(set
, type
, pos
, v
);
5572 struct isl_map
*isl_map_fix_input_si(struct isl_map
*map
,
5573 unsigned input
, int value
)
5575 return isl_map_fix_si(map
, isl_dim_in
, input
, value
);
5578 struct isl_set
*isl_set_fix_dim_si(struct isl_set
*set
, unsigned dim
, int value
)
5580 return (struct isl_set
*)
5581 isl_map_fix_si((struct isl_map
*)set
, isl_dim_set
, dim
, value
);
5584 static __isl_give isl_basic_map
*basic_map_bound_si(
5585 __isl_take isl_basic_map
*bmap
,
5586 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
5592 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
5593 pos
+= isl_basic_map_offset(bmap
, type
);
5594 bmap
= isl_basic_map_cow(bmap
);
5595 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
5596 j
= isl_basic_map_alloc_inequality(bmap
);
5599 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
5601 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
5602 isl_int_set_si(bmap
->ineq
[j
][0], value
);
5604 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
5605 isl_int_set_si(bmap
->ineq
[j
][0], -value
);
5607 bmap
= isl_basic_map_simplify(bmap
);
5608 return isl_basic_map_finalize(bmap
);
5610 isl_basic_map_free(bmap
);
5614 __isl_give isl_basic_map
*isl_basic_map_lower_bound_si(
5615 __isl_take isl_basic_map
*bmap
,
5616 enum isl_dim_type type
, unsigned pos
, int value
)
5618 return basic_map_bound_si(bmap
, type
, pos
, value
, 0);
5621 /* Constrain the values of the given dimension to be no greater than "value".
5623 __isl_give isl_basic_map
*isl_basic_map_upper_bound_si(
5624 __isl_take isl_basic_map
*bmap
,
5625 enum isl_dim_type type
, unsigned pos
, int value
)
5627 return basic_map_bound_si(bmap
, type
, pos
, value
, 1);
5630 struct isl_basic_set
*isl_basic_set_lower_bound_dim(struct isl_basic_set
*bset
,
5631 unsigned dim
, isl_int value
)
5635 bset
= isl_basic_set_cow(bset
);
5636 bset
= isl_basic_set_extend_constraints(bset
, 0, 1);
5637 j
= isl_basic_set_alloc_inequality(bset
);
5640 isl_seq_clr(bset
->ineq
[j
], 1 + isl_basic_set_total_dim(bset
));
5641 isl_int_set_si(bset
->ineq
[j
][1 + isl_basic_set_n_param(bset
) + dim
], 1);
5642 isl_int_neg(bset
->ineq
[j
][0], value
);
5643 bset
= isl_basic_set_simplify(bset
);
5644 return isl_basic_set_finalize(bset
);
5646 isl_basic_set_free(bset
);
5650 static __isl_give isl_map
*map_bound_si(__isl_take isl_map
*map
,
5651 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
5655 map
= isl_map_cow(map
);
5659 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
5660 for (i
= 0; i
< map
->n
; ++i
) {
5661 map
->p
[i
] = basic_map_bound_si(map
->p
[i
],
5662 type
, pos
, value
, upper
);
5666 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5673 __isl_give isl_map
*isl_map_lower_bound_si(__isl_take isl_map
*map
,
5674 enum isl_dim_type type
, unsigned pos
, int value
)
5676 return map_bound_si(map
, type
, pos
, value
, 0);
5679 __isl_give isl_map
*isl_map_upper_bound_si(__isl_take isl_map
*map
,
5680 enum isl_dim_type type
, unsigned pos
, int value
)
5682 return map_bound_si(map
, type
, pos
, value
, 1);
5685 __isl_give isl_set
*isl_set_lower_bound_si(__isl_take isl_set
*set
,
5686 enum isl_dim_type type
, unsigned pos
, int value
)
5688 return (struct isl_set
*)
5689 isl_map_lower_bound_si((struct isl_map
*)set
, type
, pos
, value
);
5692 __isl_give isl_set
*isl_set_upper_bound_si(__isl_take isl_set
*set
,
5693 enum isl_dim_type type
, unsigned pos
, int value
)
5695 return isl_map_upper_bound_si(set
, type
, pos
, value
);
5698 /* Bound the given variable of "bmap" from below (or above is "upper"
5699 * is set) to "value".
5701 static __isl_give isl_basic_map
*basic_map_bound(
5702 __isl_take isl_basic_map
*bmap
,
5703 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
5709 if (pos
>= isl_basic_map_dim(bmap
, type
))
5710 isl_die(bmap
->ctx
, isl_error_invalid
,
5711 "index out of bounds", goto error
);
5712 pos
+= isl_basic_map_offset(bmap
, type
);
5713 bmap
= isl_basic_map_cow(bmap
);
5714 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
5715 j
= isl_basic_map_alloc_inequality(bmap
);
5718 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
5720 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
5721 isl_int_set(bmap
->ineq
[j
][0], value
);
5723 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
5724 isl_int_neg(bmap
->ineq
[j
][0], value
);
5726 bmap
= isl_basic_map_simplify(bmap
);
5727 return isl_basic_map_finalize(bmap
);
5729 isl_basic_map_free(bmap
);
5733 /* Bound the given variable of "map" from below (or above is "upper"
5734 * is set) to "value".
5736 static __isl_give isl_map
*map_bound(__isl_take isl_map
*map
,
5737 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
5741 map
= isl_map_cow(map
);
5745 if (pos
>= isl_map_dim(map
, type
))
5746 isl_die(map
->ctx
, isl_error_invalid
,
5747 "index out of bounds", goto error
);
5748 for (i
= map
->n
- 1; i
>= 0; --i
) {
5749 map
->p
[i
] = basic_map_bound(map
->p
[i
], type
, pos
, value
, upper
);
5750 if (remove_if_empty(map
, i
) < 0)
5753 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5760 __isl_give isl_map
*isl_map_lower_bound(__isl_take isl_map
*map
,
5761 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5763 return map_bound(map
, type
, pos
, value
, 0);
5766 __isl_give isl_map
*isl_map_upper_bound(__isl_take isl_map
*map
,
5767 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5769 return map_bound(map
, type
, pos
, value
, 1);
5772 __isl_give isl_set
*isl_set_lower_bound(__isl_take isl_set
*set
,
5773 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5775 return isl_map_lower_bound(set
, type
, pos
, value
);
5778 __isl_give isl_set
*isl_set_upper_bound(__isl_take isl_set
*set
,
5779 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5781 return isl_map_upper_bound(set
, type
, pos
, value
);
5784 /* Force the values of the variable at position "pos" of type "type" of "set"
5785 * to be no smaller than "value".
5787 __isl_give isl_set
*isl_set_lower_bound_val(__isl_take isl_set
*set
,
5788 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
5792 if (!isl_val_is_int(value
))
5793 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
5794 "expecting integer value", goto error
);
5795 set
= isl_set_lower_bound(set
, type
, pos
, value
->n
);
5796 isl_val_free(value
);
5799 isl_val_free(value
);
5804 /* Force the values of the variable at position "pos" of type "type" of "set"
5805 * to be no greater than "value".
5807 __isl_give isl_set
*isl_set_upper_bound_val(__isl_take isl_set
*set
,
5808 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
5812 if (!isl_val_is_int(value
))
5813 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
5814 "expecting integer value", goto error
);
5815 set
= isl_set_upper_bound(set
, type
, pos
, value
->n
);
5816 isl_val_free(value
);
5819 isl_val_free(value
);
5824 struct isl_set
*isl_set_lower_bound_dim(struct isl_set
*set
, unsigned dim
,
5829 set
= isl_set_cow(set
);
5833 isl_assert(set
->ctx
, dim
< isl_set_n_dim(set
), goto error
);
5834 for (i
= 0; i
< set
->n
; ++i
) {
5835 set
->p
[i
] = isl_basic_set_lower_bound_dim(set
->p
[i
], dim
, value
);
5845 struct isl_map
*isl_map_reverse(struct isl_map
*map
)
5849 map
= isl_map_cow(map
);
5853 map
->dim
= isl_space_reverse(map
->dim
);
5856 for (i
= 0; i
< map
->n
; ++i
) {
5857 map
->p
[i
] = isl_basic_map_reverse(map
->p
[i
]);
5861 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5868 static struct isl_map
*isl_basic_map_partial_lexopt(
5869 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
5870 struct isl_set
**empty
, int max
)
5874 if (bmap
->ctx
->opt
->pip
== ISL_PIP_PIP
)
5875 return isl_pip_basic_map_lexopt(bmap
, dom
, empty
, max
);
5877 return isl_tab_basic_map_partial_lexopt(bmap
, dom
, empty
, max
);
5879 isl_basic_map_free(bmap
);
5880 isl_basic_set_free(dom
);
5886 struct isl_map
*isl_basic_map_partial_lexmax(
5887 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
5888 struct isl_set
**empty
)
5890 return isl_basic_map_partial_lexopt(bmap
, dom
, empty
, 1);
5893 struct isl_map
*isl_basic_map_partial_lexmin(
5894 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
5895 struct isl_set
**empty
)
5897 return isl_basic_map_partial_lexopt(bmap
, dom
, empty
, 0);
5900 struct isl_set
*isl_basic_set_partial_lexmin(
5901 struct isl_basic_set
*bset
, struct isl_basic_set
*dom
,
5902 struct isl_set
**empty
)
5904 return (struct isl_set
*)
5905 isl_basic_map_partial_lexmin((struct isl_basic_map
*)bset
,
5909 struct isl_set
*isl_basic_set_partial_lexmax(
5910 struct isl_basic_set
*bset
, struct isl_basic_set
*dom
,
5911 struct isl_set
**empty
)
5913 return (struct isl_set
*)
5914 isl_basic_map_partial_lexmax((struct isl_basic_map
*)bset
,
5918 __isl_give isl_pw_multi_aff
*isl_basic_map_partial_lexmin_pw_multi_aff(
5919 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
5920 __isl_give isl_set
**empty
)
5922 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap
, dom
, empty
, 0);
5925 __isl_give isl_pw_multi_aff
*isl_basic_map_partial_lexmax_pw_multi_aff(
5926 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*dom
,
5927 __isl_give isl_set
**empty
)
5929 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap
, dom
, empty
, 1);
5932 __isl_give isl_pw_multi_aff
*isl_basic_set_partial_lexmin_pw_multi_aff(
5933 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*dom
,
5934 __isl_give isl_set
**empty
)
5936 return isl_basic_map_partial_lexmin_pw_multi_aff(bset
, dom
, empty
);
5939 __isl_give isl_pw_multi_aff
*isl_basic_set_partial_lexmax_pw_multi_aff(
5940 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*dom
,
5941 __isl_give isl_set
**empty
)
5943 return isl_basic_map_partial_lexmax_pw_multi_aff(bset
, dom
, empty
);
5946 __isl_give isl_pw_multi_aff
*isl_basic_map_lexopt_pw_multi_aff(
5947 __isl_take isl_basic_map
*bmap
, int max
)
5949 isl_basic_set
*dom
= NULL
;
5950 isl_space
*dom_space
;
5954 dom_space
= isl_space_domain(isl_space_copy(bmap
->dim
));
5955 dom
= isl_basic_set_universe(dom_space
);
5956 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap
, dom
, NULL
, max
);
5958 isl_basic_map_free(bmap
);
5962 __isl_give isl_pw_multi_aff
*isl_basic_map_lexmin_pw_multi_aff(
5963 __isl_take isl_basic_map
*bmap
)
5965 return isl_basic_map_lexopt_pw_multi_aff(bmap
, 0);
5969 #define TYPE isl_pw_multi_aff
5971 #define SUFFIX _pw_multi_aff
5973 #define EMPTY isl_pw_multi_aff_empty
5975 #define ADD isl_pw_multi_aff_union_add
5976 #include "isl_map_lexopt_templ.c"
5978 /* Given a map "map", compute the lexicographically minimal
5979 * (or maximal) image element for each domain element in dom,
5980 * in the form of an isl_pw_multi_aff.
5981 * Set *empty to those elements in dom that do not have an image element.
5983 * We first compute the lexicographically minimal or maximal element
5984 * in the first basic map. This results in a partial solution "res"
5985 * and a subset "todo" of dom that still need to be handled.
5986 * We then consider each of the remaining maps in "map" and successively
5987 * update both "res" and "todo".
5989 static __isl_give isl_pw_multi_aff
*isl_map_partial_lexopt_aligned_pw_multi_aff(
5990 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
5991 __isl_give isl_set
**empty
, int max
)
5994 isl_pw_multi_aff
*res
;
6000 if (isl_map_plain_is_empty(map
)) {
6005 return isl_pw_multi_aff_from_map(map
);
6008 res
= basic_map_partial_lexopt_pw_multi_aff(
6009 isl_basic_map_copy(map
->p
[0]),
6010 isl_set_copy(dom
), &todo
, max
);
6012 for (i
= 1; i
< map
->n
; ++i
) {
6013 isl_pw_multi_aff
*res_i
;
6016 res_i
= basic_map_partial_lexopt_pw_multi_aff(
6017 isl_basic_map_copy(map
->p
[i
]),
6018 isl_set_copy(dom
), &todo_i
, max
);
6021 res
= isl_pw_multi_aff_union_lexmax(res
, res_i
);
6023 res
= isl_pw_multi_aff_union_lexmin(res
, res_i
);
6025 todo
= isl_set_intersect(todo
, todo_i
);
6046 #define TYPE isl_map
6050 #define EMPTY isl_map_empty
6052 #define ADD isl_map_union_disjoint
6053 #include "isl_map_lexopt_templ.c"
6055 /* Given a map "map", compute the lexicographically minimal
6056 * (or maximal) image element for each domain element in dom.
6057 * Set *empty to those elements in dom that do not have an image element.
6059 * We first compute the lexicographically minimal or maximal element
6060 * in the first basic map. This results in a partial solution "res"
6061 * and a subset "todo" of dom that still need to be handled.
6062 * We then consider each of the remaining maps in "map" and successively
6063 * update both "res" and "todo".
6065 * Let res^k and todo^k be the results after k steps and let i = k + 1.
6066 * Assume we are computing the lexicographical maximum.
6067 * We first compute the lexicographically maximal element in basic map i.
6068 * This results in a partial solution res_i and a subset todo_i.
6069 * Then we combine these results with those obtain for the first k basic maps
6070 * to obtain a result that is valid for the first k+1 basic maps.
6071 * In particular, the set where there is no solution is the set where
6072 * there is no solution for the first k basic maps and also no solution
6073 * for the ith basic map, i.e.,
6075 * todo^i = todo^k * todo_i
6077 * On dom(res^k) * dom(res_i), we need to pick the larger of the two
6078 * solutions, arbitrarily breaking ties in favor of res^k.
6079 * That is, when res^k(a) >= res_i(a), we pick res^k and
6080 * when res^k(a) < res_i(a), we pick res_i. (Here, ">=" and "<" denote
6081 * the lexicographic order.)
6082 * In practice, we compute
6084 * res^k * (res_i . "<=")
6088 * res_i * (res^k . "<")
6090 * Finally, we consider the symmetric difference of dom(res^k) and dom(res_i),
6091 * where only one of res^k and res_i provides a solution and we simply pick
6098 * Note that we only compute these intersections when dom(res^k) intersects
6099 * dom(res_i). Otherwise, the only effect of these intersections is to
6100 * potentially break up res^k and res_i into smaller pieces.
6101 * We want to avoid such splintering as much as possible.
6102 * In fact, an earlier implementation of this function would look for
6103 * better results in the domain of res^k and for extra results in todo^k,
6104 * but this would always result in a splintering according to todo^k,
6105 * even when the domain of basic map i is disjoint from the domains of
6106 * the previous basic maps.
6108 static __isl_give isl_map
*isl_map_partial_lexopt_aligned(
6109 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6110 __isl_give isl_set
**empty
, int max
)
6113 struct isl_map
*res
;
6114 struct isl_set
*todo
;
6119 if (isl_map_plain_is_empty(map
)) {
6127 res
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[0]),
6128 isl_set_copy(dom
), &todo
, max
);
6130 for (i
= 1; i
< map
->n
; ++i
) {
6134 isl_space
*dim
= isl_space_range(isl_map_get_space(res
));
6136 res_i
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[i
]),
6137 isl_set_copy(dom
), &todo_i
, max
);
6140 lt
= isl_map_lex_lt(isl_space_copy(dim
));
6141 le
= isl_map_lex_le(dim
);
6143 lt
= isl_map_lex_gt(isl_space_copy(dim
));
6144 le
= isl_map_lex_ge(dim
);
6146 lt
= isl_map_apply_range(isl_map_copy(res
), lt
);
6147 lt
= isl_map_intersect(lt
, isl_map_copy(res_i
));
6148 le
= isl_map_apply_range(isl_map_copy(res_i
), le
);
6149 le
= isl_map_intersect(le
, isl_map_copy(res
));
6151 if (!isl_map_is_empty(lt
) || !isl_map_is_empty(le
)) {
6152 res
= isl_map_intersect_domain(res
,
6153 isl_set_copy(todo_i
));
6154 res_i
= isl_map_intersect_domain(res_i
,
6155 isl_set_copy(todo
));
6158 res
= isl_map_union_disjoint(res
, res_i
);
6159 res
= isl_map_union_disjoint(res
, lt
);
6160 res
= isl_map_union_disjoint(res
, le
);
6162 todo
= isl_set_intersect(todo
, todo_i
);
6182 __isl_give isl_map
*isl_map_partial_lexmax(
6183 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6184 __isl_give isl_set
**empty
)
6186 return isl_map_partial_lexopt(map
, dom
, empty
, 1);
6189 __isl_give isl_map
*isl_map_partial_lexmin(
6190 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6191 __isl_give isl_set
**empty
)
6193 return isl_map_partial_lexopt(map
, dom
, empty
, 0);
6196 __isl_give isl_set
*isl_set_partial_lexmin(
6197 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
6198 __isl_give isl_set
**empty
)
6200 return (struct isl_set
*)
6201 isl_map_partial_lexmin((struct isl_map
*)set
,
6205 __isl_give isl_set
*isl_set_partial_lexmax(
6206 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
6207 __isl_give isl_set
**empty
)
6209 return (struct isl_set
*)
6210 isl_map_partial_lexmax((struct isl_map
*)set
,
6214 /* Compute the lexicographic minimum (or maximum if "max" is set)
6215 * of "bmap" over its domain.
6217 * Since we are not interested in the part of the domain space where
6218 * there is no solution, we initialize the domain to those constraints
6219 * of "bmap" that only involve the parameters and the input dimensions.
6220 * This relieves the parametric programming engine from detecting those
6221 * inequalities and transferring them to the context. More importantly,
6222 * it ensures that those inequalities are transferred first and not
6223 * intermixed with inequalities that actually split the domain.
6225 __isl_give isl_map
*isl_basic_map_lexopt(__isl_take isl_basic_map
*bmap
, int max
)
6229 isl_basic_map
*copy
;
6232 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
6233 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
6234 copy
= isl_basic_map_copy(bmap
);
6235 copy
= isl_basic_map_drop_constraints_involving_dims(copy
,
6236 isl_dim_div
, 0, n_div
);
6237 copy
= isl_basic_map_drop_constraints_involving_dims(copy
,
6238 isl_dim_out
, 0, n_out
);
6239 dom
= isl_basic_map_domain(copy
);
6240 return isl_basic_map_partial_lexopt(bmap
, dom
, NULL
, max
);
6243 __isl_give isl_map
*isl_basic_map_lexmin(__isl_take isl_basic_map
*bmap
)
6245 return isl_basic_map_lexopt(bmap
, 0);
6248 __isl_give isl_map
*isl_basic_map_lexmax(__isl_take isl_basic_map
*bmap
)
6250 return isl_basic_map_lexopt(bmap
, 1);
6253 __isl_give isl_set
*isl_basic_set_lexmin(__isl_take isl_basic_set
*bset
)
6255 return (isl_set
*)isl_basic_map_lexmin((isl_basic_map
*)bset
);
6258 __isl_give isl_set
*isl_basic_set_lexmax(__isl_take isl_basic_set
*bset
)
6260 return (isl_set
*)isl_basic_map_lexmax((isl_basic_map
*)bset
);
6263 /* Extract the first and only affine expression from list
6264 * and then add it to *pwaff with the given dom.
6265 * This domain is known to be disjoint from other domains
6266 * because of the way isl_basic_map_foreach_lexmax works.
6268 static int update_dim_opt(__isl_take isl_basic_set
*dom
,
6269 __isl_take isl_aff_list
*list
, void *user
)
6271 isl_ctx
*ctx
= isl_basic_set_get_ctx(dom
);
6273 isl_pw_aff
**pwaff
= user
;
6274 isl_pw_aff
*pwaff_i
;
6278 if (isl_aff_list_n_aff(list
) != 1)
6279 isl_die(ctx
, isl_error_internal
,
6280 "expecting single element list", goto error
);
6282 aff
= isl_aff_list_get_aff(list
, 0);
6283 pwaff_i
= isl_pw_aff_alloc(isl_set_from_basic_set(dom
), aff
);
6285 *pwaff
= isl_pw_aff_add_disjoint(*pwaff
, pwaff_i
);
6287 isl_aff_list_free(list
);
6291 isl_basic_set_free(dom
);
6292 isl_aff_list_free(list
);
6296 /* Given a basic map with one output dimension, compute the minimum or
6297 * maximum of that dimension as an isl_pw_aff.
6299 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
6300 * call update_dim_opt on each leaf of the result.
6302 static __isl_give isl_pw_aff
*basic_map_dim_opt(__isl_keep isl_basic_map
*bmap
,
6305 isl_space
*dim
= isl_basic_map_get_space(bmap
);
6309 dim
= isl_space_from_domain(isl_space_domain(dim
));
6310 dim
= isl_space_add_dims(dim
, isl_dim_out
, 1);
6311 pwaff
= isl_pw_aff_empty(dim
);
6313 r
= isl_basic_map_foreach_lexopt(bmap
, max
, &update_dim_opt
, &pwaff
);
6315 return isl_pw_aff_free(pwaff
);
6320 /* Compute the minimum or maximum of the given output dimension
6321 * as a function of the parameters and the input dimensions,
6322 * but independently of the other output dimensions.
6324 * We first project out the other output dimension and then compute
6325 * the "lexicographic" maximum in each basic map, combining the results
6326 * using isl_pw_aff_union_max.
6328 static __isl_give isl_pw_aff
*map_dim_opt(__isl_take isl_map
*map
, int pos
,
6335 n_out
= isl_map_dim(map
, isl_dim_out
);
6336 map
= isl_map_project_out(map
, isl_dim_out
, pos
+ 1, n_out
- (pos
+ 1));
6337 map
= isl_map_project_out(map
, isl_dim_out
, 0, pos
);
6342 isl_space
*dim
= isl_map_get_space(map
);
6343 dim
= isl_space_domain(isl_space_from_range(dim
));
6345 return isl_pw_aff_empty(dim
);
6348 pwaff
= basic_map_dim_opt(map
->p
[0], max
);
6349 for (i
= 1; i
< map
->n
; ++i
) {
6350 isl_pw_aff
*pwaff_i
;
6352 pwaff_i
= basic_map_dim_opt(map
->p
[i
], max
);
6353 pwaff
= isl_pw_aff_union_opt(pwaff
, pwaff_i
, max
);
6361 /* Compute the maximum of the given output dimension as a function of the
6362 * parameters and input dimensions, but independently of
6363 * the other output dimensions.
6365 __isl_give isl_pw_aff
*isl_map_dim_max(__isl_take isl_map
*map
, int pos
)
6367 return map_dim_opt(map
, pos
, 1);
6370 /* Compute the minimum or maximum of the given set dimension
6371 * as a function of the parameters,
6372 * but independently of the other set dimensions.
6374 static __isl_give isl_pw_aff
*set_dim_opt(__isl_take isl_set
*set
, int pos
,
6377 return map_dim_opt(set
, pos
, max
);
6380 /* Compute the maximum of the given set dimension as a function of the
6381 * parameters, but independently of the other set dimensions.
6383 __isl_give isl_pw_aff
*isl_set_dim_max(__isl_take isl_set
*set
, int pos
)
6385 return set_dim_opt(set
, pos
, 1);
6388 /* Compute the minimum of the given set dimension as a function of the
6389 * parameters, but independently of the other set dimensions.
6391 __isl_give isl_pw_aff
*isl_set_dim_min(__isl_take isl_set
*set
, int pos
)
6393 return set_dim_opt(set
, pos
, 0);
6396 /* Apply a preimage specified by "mat" on the parameters of "bset".
6397 * bset is assumed to have only parameters and divs.
6399 static struct isl_basic_set
*basic_set_parameter_preimage(
6400 struct isl_basic_set
*bset
, struct isl_mat
*mat
)
6407 bset
->dim
= isl_space_cow(bset
->dim
);
6411 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
6413 isl_assert(bset
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
6415 bset
->dim
->nparam
= 0;
6416 bset
->dim
->n_out
= nparam
;
6417 bset
= isl_basic_set_preimage(bset
, mat
);
6419 bset
->dim
->nparam
= bset
->dim
->n_out
;
6420 bset
->dim
->n_out
= 0;
6425 isl_basic_set_free(bset
);
6429 /* Apply a preimage specified by "mat" on the parameters of "set".
6430 * set is assumed to have only parameters and divs.
6432 static struct isl_set
*set_parameter_preimage(
6433 struct isl_set
*set
, struct isl_mat
*mat
)
6435 isl_space
*dim
= NULL
;
6441 dim
= isl_space_copy(set
->dim
);
6442 dim
= isl_space_cow(dim
);
6446 nparam
= isl_set_dim(set
, isl_dim_param
);
6448 isl_assert(set
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
6451 dim
->n_out
= nparam
;
6452 isl_set_reset_space(set
, dim
);
6453 set
= isl_set_preimage(set
, mat
);
6456 dim
= isl_space_copy(set
->dim
);
6457 dim
= isl_space_cow(dim
);
6460 dim
->nparam
= dim
->n_out
;
6462 isl_set_reset_space(set
, dim
);
6465 isl_space_free(dim
);
6472 /* Intersect the basic set "bset" with the affine space specified by the
6473 * equalities in "eq".
6475 static struct isl_basic_set
*basic_set_append_equalities(
6476 struct isl_basic_set
*bset
, struct isl_mat
*eq
)
6484 bset
= isl_basic_set_extend_space(bset
, isl_space_copy(bset
->dim
), 0,
6489 len
= 1 + isl_space_dim(bset
->dim
, isl_dim_all
) + bset
->extra
;
6490 for (i
= 0; i
< eq
->n_row
; ++i
) {
6491 k
= isl_basic_set_alloc_equality(bset
);
6494 isl_seq_cpy(bset
->eq
[k
], eq
->row
[i
], eq
->n_col
);
6495 isl_seq_clr(bset
->eq
[k
] + eq
->n_col
, len
- eq
->n_col
);
6499 bset
= isl_basic_set_gauss(bset
, NULL
);
6500 bset
= isl_basic_set_finalize(bset
);
6505 isl_basic_set_free(bset
);
6509 /* Intersect the set "set" with the affine space specified by the
6510 * equalities in "eq".
6512 static struct isl_set
*set_append_equalities(struct isl_set
*set
,
6520 for (i
= 0; i
< set
->n
; ++i
) {
6521 set
->p
[i
] = basic_set_append_equalities(set
->p
[i
],
6534 /* Given a basic set "bset" that only involves parameters and existentially
6535 * quantified variables, return the index of the first equality
6536 * that only involves parameters. If there is no such equality then
6537 * return bset->n_eq.
6539 * This function assumes that isl_basic_set_gauss has been called on "bset".
6541 static int first_parameter_equality(__isl_keep isl_basic_set
*bset
)
6544 unsigned nparam
, n_div
;
6549 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
6550 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
6552 for (i
= 0, j
= n_div
- 1; i
< bset
->n_eq
&& j
>= 0; --j
) {
6553 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ j
]))
6560 /* Compute an explicit representation for the existentially quantified
6561 * variables in "bset" by computing the "minimal value" of the set
6562 * variables. Since there are no set variables, the computation of
6563 * the minimal value essentially computes an explicit representation
6564 * of the non-empty part(s) of "bset".
6566 * The input only involves parameters and existentially quantified variables.
6567 * All equalities among parameters have been removed.
6569 * Since the existentially quantified variables in the result are in general
6570 * going to be different from those in the input, we first replace
6571 * them by the minimal number of variables based on their equalities.
6572 * This should simplify the parametric integer programming.
6574 static __isl_give isl_set
*base_compute_divs(__isl_take isl_basic_set
*bset
)
6576 isl_morph
*morph1
, *morph2
;
6582 if (bset
->n_eq
== 0)
6583 return isl_basic_set_lexmin(bset
);
6585 morph1
= isl_basic_set_parameter_compression(bset
);
6586 bset
= isl_morph_basic_set(isl_morph_copy(morph1
), bset
);
6587 bset
= isl_basic_set_lift(bset
);
6588 morph2
= isl_basic_set_variable_compression(bset
, isl_dim_set
);
6589 bset
= isl_morph_basic_set(morph2
, bset
);
6590 n
= isl_basic_set_dim(bset
, isl_dim_set
);
6591 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
6593 set
= isl_basic_set_lexmin(bset
);
6595 set
= isl_morph_set(isl_morph_inverse(morph1
), set
);
6600 /* Project the given basic set onto its parameter domain, possibly introducing
6601 * new, explicit, existential variables in the constraints.
6602 * The input has parameters and (possibly implicit) existential variables.
6603 * The output has the same parameters, but only
6604 * explicit existentially quantified variables.
6606 * The actual projection is performed by pip, but pip doesn't seem
6607 * to like equalities very much, so we first remove the equalities
6608 * among the parameters by performing a variable compression on
6609 * the parameters. Afterward, an inverse transformation is performed
6610 * and the equalities among the parameters are inserted back in.
6612 * The variable compression on the parameters may uncover additional
6613 * equalities that were only implicit before. We therefore check
6614 * if there are any new parameter equalities in the result and
6615 * if so recurse. The removal of parameter equalities is required
6616 * for the parameter compression performed by base_compute_divs.
6618 static struct isl_set
*parameter_compute_divs(struct isl_basic_set
*bset
)
6622 struct isl_mat
*T
, *T2
;
6623 struct isl_set
*set
;
6626 bset
= isl_basic_set_cow(bset
);
6630 if (bset
->n_eq
== 0)
6631 return base_compute_divs(bset
);
6633 bset
= isl_basic_set_gauss(bset
, NULL
);
6636 if (isl_basic_set_plain_is_empty(bset
))
6637 return isl_set_from_basic_set(bset
);
6639 i
= first_parameter_equality(bset
);
6640 if (i
== bset
->n_eq
)
6641 return base_compute_divs(bset
);
6643 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
6644 eq
= isl_mat_sub_alloc6(bset
->ctx
, bset
->eq
, i
, bset
->n_eq
- i
,
6646 eq
= isl_mat_cow(eq
);
6647 T
= isl_mat_variable_compression(isl_mat_copy(eq
), &T2
);
6648 if (T
&& T
->n_col
== 0) {
6652 bset
= isl_basic_set_set_to_empty(bset
);
6653 return isl_set_from_basic_set(bset
);
6655 bset
= basic_set_parameter_preimage(bset
, T
);
6657 i
= first_parameter_equality(bset
);
6660 else if (i
== bset
->n_eq
)
6661 set
= base_compute_divs(bset
);
6663 set
= parameter_compute_divs(bset
);
6664 set
= set_parameter_preimage(set
, T2
);
6665 set
= set_append_equalities(set
, eq
);
6669 /* Insert the divs from "ls" before those of "bmap".
6671 * The number of columns is not changed, which means that the last
6672 * dimensions of "bmap" are being reintepreted as the divs from "ls".
6673 * The caller is responsible for removing the same number of dimensions
6674 * from the space of "bmap".
6676 static __isl_give isl_basic_map
*insert_divs_from_local_space(
6677 __isl_take isl_basic_map
*bmap
, __isl_keep isl_local_space
*ls
)
6683 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
6687 old_n_div
= bmap
->n_div
;
6688 bmap
= insert_div_rows(bmap
, n_div
);
6692 for (i
= 0; i
< n_div
; ++i
) {
6693 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], ls
->div
->n_col
);
6694 isl_seq_clr(bmap
->div
[i
] + ls
->div
->n_col
, old_n_div
);
6700 /* Replace the space of "bmap" by the space and divs of "ls".
6702 * If "ls" has any divs, then we simplify the result since we may
6703 * have discovered some additional equalities that could simplify
6704 * the div expressions.
6706 static __isl_give isl_basic_map
*basic_replace_space_by_local_space(
6707 __isl_take isl_basic_map
*bmap
, __isl_take isl_local_space
*ls
)
6711 bmap
= isl_basic_map_cow(bmap
);
6715 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
6716 bmap
= insert_divs_from_local_space(bmap
, ls
);
6720 isl_space_free(bmap
->dim
);
6721 bmap
->dim
= isl_local_space_get_space(ls
);
6725 isl_local_space_free(ls
);
6727 bmap
= isl_basic_map_simplify(bmap
);
6728 bmap
= isl_basic_map_finalize(bmap
);
6731 isl_basic_map_free(bmap
);
6732 isl_local_space_free(ls
);
6736 /* Replace the space of "map" by the space and divs of "ls".
6738 static __isl_give isl_map
*replace_space_by_local_space(__isl_take isl_map
*map
,
6739 __isl_take isl_local_space
*ls
)
6743 map
= isl_map_cow(map
);
6747 for (i
= 0; i
< map
->n
; ++i
) {
6748 map
->p
[i
] = basic_replace_space_by_local_space(map
->p
[i
],
6749 isl_local_space_copy(ls
));
6753 isl_space_free(map
->dim
);
6754 map
->dim
= isl_local_space_get_space(ls
);
6758 isl_local_space_free(ls
);
6761 isl_local_space_free(ls
);
6766 /* Compute an explicit representation for the existentially
6767 * quantified variables for which do not know any explicit representation yet.
6769 * We first sort the existentially quantified variables so that the
6770 * existentially quantified variables for which we already have an explicit
6771 * representation are placed before those for which we do not.
6772 * The input dimensions, the output dimensions and the existentially
6773 * quantified variables for which we already have an explicit
6774 * representation are then turned into parameters.
6775 * compute_divs returns a map with the same parameters and
6776 * no input or output dimensions and the dimension specification
6777 * is reset to that of the input, including the existentially quantified
6778 * variables for which we already had an explicit representation.
6780 static struct isl_map
*compute_divs(struct isl_basic_map
*bmap
)
6782 struct isl_basic_set
*bset
;
6783 struct isl_set
*set
;
6784 struct isl_map
*map
;
6786 isl_local_space
*ls
;
6793 bmap
= isl_basic_map_sort_divs(bmap
);
6794 bmap
= isl_basic_map_cow(bmap
);
6798 for (n_known
= 0; n_known
< bmap
->n_div
; ++n_known
)
6799 if (isl_int_is_zero(bmap
->div
[n_known
][0]))
6802 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
6803 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
6804 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
6805 dim
= isl_space_set_alloc(bmap
->ctx
,
6806 nparam
+ n_in
+ n_out
+ n_known
, 0);
6810 ls
= isl_basic_map_get_local_space(bmap
);
6811 ls
= isl_local_space_drop_dims(ls
, isl_dim_div
,
6812 n_known
, bmap
->n_div
- n_known
);
6814 for (i
= n_known
; i
< bmap
->n_div
; ++i
)
6815 swap_div(bmap
, i
- n_known
, i
);
6816 bmap
->n_div
-= n_known
;
6817 bmap
->extra
-= n_known
;
6819 bmap
= isl_basic_map_reset_space(bmap
, dim
);
6820 bset
= (struct isl_basic_set
*)bmap
;
6822 set
= parameter_compute_divs(bset
);
6823 map
= (struct isl_map
*)set
;
6824 map
= replace_space_by_local_space(map
, ls
);
6828 isl_basic_map_free(bmap
);
6832 int isl_basic_map_divs_known(__isl_keep isl_basic_map
*bmap
)
6840 off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
6841 for (i
= 0; i
< bmap
->n_div
; ++i
) {
6842 if (isl_int_is_zero(bmap
->div
[i
][0]))
6844 isl_assert(bmap
->ctx
, isl_int_is_zero(bmap
->div
[i
][1+1+off
+i
]),
6850 static int map_divs_known(__isl_keep isl_map
*map
)
6857 for (i
= 0; i
< map
->n
; ++i
) {
6858 int known
= isl_basic_map_divs_known(map
->p
[i
]);
6866 /* If bmap contains any unknown divs, then compute explicit
6867 * expressions for them. However, this computation may be
6868 * quite expensive, so first try to remove divs that aren't
6871 struct isl_map
*isl_basic_map_compute_divs(struct isl_basic_map
*bmap
)
6874 struct isl_map
*map
;
6876 known
= isl_basic_map_divs_known(bmap
);
6880 return isl_map_from_basic_map(bmap
);
6882 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
6884 known
= isl_basic_map_divs_known(bmap
);
6888 return isl_map_from_basic_map(bmap
);
6890 map
= compute_divs(bmap
);
6893 isl_basic_map_free(bmap
);
6897 struct isl_map
*isl_map_compute_divs(struct isl_map
*map
)
6901 struct isl_map
*res
;
6908 known
= map_divs_known(map
);
6916 res
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[0]));
6917 for (i
= 1 ; i
< map
->n
; ++i
) {
6919 r2
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[i
]));
6920 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
6921 res
= isl_map_union_disjoint(res
, r2
);
6923 res
= isl_map_union(res
, r2
);
6930 struct isl_set
*isl_basic_set_compute_divs(struct isl_basic_set
*bset
)
6932 return (struct isl_set
*)
6933 isl_basic_map_compute_divs((struct isl_basic_map
*)bset
);
6936 struct isl_set
*isl_set_compute_divs(struct isl_set
*set
)
6938 return (struct isl_set
*)
6939 isl_map_compute_divs((struct isl_map
*)set
);
6942 struct isl_set
*isl_map_domain(struct isl_map
*map
)
6945 struct isl_set
*set
;
6950 map
= isl_map_cow(map
);
6954 set
= (struct isl_set
*)map
;
6955 set
->dim
= isl_space_domain(set
->dim
);
6958 for (i
= 0; i
< map
->n
; ++i
) {
6959 set
->p
[i
] = isl_basic_map_domain(map
->p
[i
]);
6963 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
6964 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
6971 /* Return the union of "map1" and "map2", where we assume for now that
6972 * "map1" and "map2" are disjoint. Note that the basic maps inside
6973 * "map1" or "map2" may not be disjoint from each other.
6974 * Also note that this function is also called from isl_map_union,
6975 * which takes care of handling the situation where "map1" and "map2"
6976 * may not be disjoint.
6978 * If one of the inputs is empty, we can simply return the other input.
6979 * Similarly, if one of the inputs is universal, then it is equal to the union.
6981 static __isl_give isl_map
*map_union_disjoint(__isl_take isl_map
*map1
,
6982 __isl_take isl_map
*map2
)
6986 struct isl_map
*map
= NULL
;
7001 is_universe
= isl_map_plain_is_universe(map1
);
7002 if (is_universe
< 0)
7009 is_universe
= isl_map_plain_is_universe(map2
);
7010 if (is_universe
< 0)
7017 isl_assert(map1
->ctx
, isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
7019 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
7020 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
7021 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
7023 map
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
7024 map1
->n
+ map2
->n
, flags
);
7027 for (i
= 0; i
< map1
->n
; ++i
) {
7028 map
= isl_map_add_basic_map(map
,
7029 isl_basic_map_copy(map1
->p
[i
]));
7033 for (i
= 0; i
< map2
->n
; ++i
) {
7034 map
= isl_map_add_basic_map(map
,
7035 isl_basic_map_copy(map2
->p
[i
]));
7049 __isl_give isl_map
*isl_map_union_disjoint(__isl_take isl_map
*map1
,
7050 __isl_take isl_map
*map2
)
7052 return isl_map_align_params_map_map_and(map1
, map2
, &map_union_disjoint
);
7055 struct isl_map
*isl_map_union(struct isl_map
*map1
, struct isl_map
*map2
)
7057 map1
= isl_map_union_disjoint(map1
, map2
);
7061 ISL_F_CLR(map1
, ISL_MAP_DISJOINT
);
7065 struct isl_set
*isl_set_union_disjoint(
7066 struct isl_set
*set1
, struct isl_set
*set2
)
7068 return (struct isl_set
*)
7069 isl_map_union_disjoint(
7070 (struct isl_map
*)set1
, (struct isl_map
*)set2
);
7073 struct isl_set
*isl_set_union(struct isl_set
*set1
, struct isl_set
*set2
)
7075 return (struct isl_set
*)
7076 isl_map_union((struct isl_map
*)set1
, (struct isl_map
*)set2
);
7079 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7082 * "map" and "set" are assumed to be compatible and non-NULL.
7084 static __isl_give isl_map
*map_intersect_set(__isl_take isl_map
*map
,
7085 __isl_take isl_set
*set
,
7086 __isl_give isl_basic_map
*fn(__isl_take isl_basic_map
*bmap
,
7087 __isl_take isl_basic_set
*bset
))
7090 struct isl_map
*result
;
7093 if (isl_set_plain_is_universe(set
)) {
7098 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
) &&
7099 ISL_F_ISSET(set
, ISL_MAP_DISJOINT
))
7100 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
7102 result
= isl_map_alloc_space(isl_space_copy(map
->dim
),
7103 map
->n
* set
->n
, flags
);
7104 for (i
= 0; result
&& i
< map
->n
; ++i
)
7105 for (j
= 0; j
< set
->n
; ++j
) {
7106 result
= isl_map_add_basic_map(result
,
7107 fn(isl_basic_map_copy(map
->p
[i
]),
7108 isl_basic_set_copy(set
->p
[j
])));
7118 static __isl_give isl_map
*map_intersect_range(__isl_take isl_map
*map
,
7119 __isl_take isl_set
*set
)
7124 if (!isl_map_compatible_range(map
, set
))
7125 isl_die(set
->ctx
, isl_error_invalid
,
7126 "incompatible spaces", goto error
);
7128 return map_intersect_set(map
, set
, &isl_basic_map_intersect_range
);
7135 __isl_give isl_map
*isl_map_intersect_range(__isl_take isl_map
*map
,
7136 __isl_take isl_set
*set
)
7138 return isl_map_align_params_map_map_and(map
, set
, &map_intersect_range
);
7141 static __isl_give isl_map
*map_intersect_domain(__isl_take isl_map
*map
,
7142 __isl_take isl_set
*set
)
7147 if (!isl_map_compatible_domain(map
, set
))
7148 isl_die(set
->ctx
, isl_error_invalid
,
7149 "incompatible spaces", goto error
);
7151 return map_intersect_set(map
, set
, &isl_basic_map_intersect_domain
);
7158 __isl_give isl_map
*isl_map_intersect_domain(__isl_take isl_map
*map
,
7159 __isl_take isl_set
*set
)
7161 return isl_map_align_params_map_map_and(map
, set
,
7162 &map_intersect_domain
);
7165 static __isl_give isl_map
*map_apply_domain(__isl_take isl_map
*map1
,
7166 __isl_take isl_map
*map2
)
7170 map1
= isl_map_reverse(map1
);
7171 map1
= isl_map_apply_range(map1
, map2
);
7172 return isl_map_reverse(map1
);
7179 __isl_give isl_map
*isl_map_apply_domain(__isl_take isl_map
*map1
,
7180 __isl_take isl_map
*map2
)
7182 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_domain
);
7185 static __isl_give isl_map
*map_apply_range(__isl_take isl_map
*map1
,
7186 __isl_take isl_map
*map2
)
7188 isl_space
*dim_result
;
7189 struct isl_map
*result
;
7195 dim_result
= isl_space_join(isl_space_copy(map1
->dim
),
7196 isl_space_copy(map2
->dim
));
7198 result
= isl_map_alloc_space(dim_result
, map1
->n
* map2
->n
, 0);
7201 for (i
= 0; i
< map1
->n
; ++i
)
7202 for (j
= 0; j
< map2
->n
; ++j
) {
7203 result
= isl_map_add_basic_map(result
,
7204 isl_basic_map_apply_range(
7205 isl_basic_map_copy(map1
->p
[i
]),
7206 isl_basic_map_copy(map2
->p
[j
])));
7212 if (result
&& result
->n
<= 1)
7213 ISL_F_SET(result
, ISL_MAP_DISJOINT
);
7221 __isl_give isl_map
*isl_map_apply_range(__isl_take isl_map
*map1
,
7222 __isl_take isl_map
*map2
)
7224 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_range
);
7228 * returns range - domain
7230 struct isl_basic_set
*isl_basic_map_deltas(struct isl_basic_map
*bmap
)
7232 isl_space
*dims
, *target_dim
;
7233 struct isl_basic_set
*bset
;
7240 isl_assert(bmap
->ctx
, isl_space_tuple_match(bmap
->dim
, isl_dim_in
,
7241 bmap
->dim
, isl_dim_out
),
7243 target_dim
= isl_space_domain(isl_basic_map_get_space(bmap
));
7244 dim
= isl_basic_map_n_in(bmap
);
7245 nparam
= isl_basic_map_n_param(bmap
);
7246 bset
= isl_basic_set_from_basic_map(bmap
);
7247 bset
= isl_basic_set_cow(bset
);
7248 dims
= isl_basic_set_get_space(bset
);
7249 dims
= isl_space_add_dims(dims
, isl_dim_set
, dim
);
7250 bset
= isl_basic_set_extend_space(bset
, dims
, 0, dim
, 0);
7251 bset
= isl_basic_set_swap_vars(bset
, 2*dim
);
7252 for (i
= 0; i
< dim
; ++i
) {
7253 int j
= isl_basic_map_alloc_equality(
7254 (struct isl_basic_map
*)bset
);
7256 bset
= isl_basic_set_free(bset
);
7259 isl_seq_clr(bset
->eq
[j
], 1 + isl_basic_set_total_dim(bset
));
7260 isl_int_set_si(bset
->eq
[j
][1+nparam
+i
], 1);
7261 isl_int_set_si(bset
->eq
[j
][1+nparam
+dim
+i
], 1);
7262 isl_int_set_si(bset
->eq
[j
][1+nparam
+2*dim
+i
], -1);
7264 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, dim
, 2*dim
);
7265 bset
= isl_basic_set_reset_space(bset
, target_dim
);
7268 isl_basic_map_free(bmap
);
7273 * returns range - domain
7275 __isl_give isl_set
*isl_map_deltas(__isl_take isl_map
*map
)
7279 struct isl_set
*result
;
7284 isl_assert(map
->ctx
, isl_space_tuple_match(map
->dim
, isl_dim_in
,
7285 map
->dim
, isl_dim_out
),
7287 dim
= isl_map_get_space(map
);
7288 dim
= isl_space_domain(dim
);
7289 result
= isl_set_alloc_space(dim
, map
->n
, 0);
7292 for (i
= 0; i
< map
->n
; ++i
)
7293 result
= isl_set_add_basic_set(result
,
7294 isl_basic_map_deltas(isl_basic_map_copy(map
->p
[i
])));
7303 * returns [domain -> range] -> range - domain
7305 __isl_give isl_basic_map
*isl_basic_map_deltas_map(
7306 __isl_take isl_basic_map
*bmap
)
7310 isl_basic_map
*domain
;
7314 if (!isl_space_tuple_match(bmap
->dim
, isl_dim_in
, bmap
->dim
, isl_dim_out
))
7315 isl_die(bmap
->ctx
, isl_error_invalid
,
7316 "domain and range don't match", goto error
);
7318 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
7319 n
= isl_basic_map_dim(bmap
, isl_dim_in
);
7321 dim
= isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap
)));
7322 domain
= isl_basic_map_universe(dim
);
7324 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
7325 bmap
= isl_basic_map_apply_range(bmap
, domain
);
7326 bmap
= isl_basic_map_extend_constraints(bmap
, n
, 0);
7328 total
= isl_basic_map_total_dim(bmap
);
7330 for (i
= 0; i
< n
; ++i
) {
7331 k
= isl_basic_map_alloc_equality(bmap
);
7334 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
7335 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], 1);
7336 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ i
], -1);
7337 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ n
+ i
], 1);
7340 bmap
= isl_basic_map_gauss(bmap
, NULL
);
7341 return isl_basic_map_finalize(bmap
);
7343 isl_basic_map_free(bmap
);
7348 * returns [domain -> range] -> range - domain
7350 __isl_give isl_map
*isl_map_deltas_map(__isl_take isl_map
*map
)
7353 isl_space
*domain_dim
;
7358 if (!isl_space_tuple_match(map
->dim
, isl_dim_in
, map
->dim
, isl_dim_out
))
7359 isl_die(map
->ctx
, isl_error_invalid
,
7360 "domain and range don't match", goto error
);
7362 map
= isl_map_cow(map
);
7366 domain_dim
= isl_space_from_range(isl_space_domain(isl_map_get_space(map
)));
7367 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
7368 map
->dim
= isl_space_join(map
->dim
, domain_dim
);
7371 for (i
= 0; i
< map
->n
; ++i
) {
7372 map
->p
[i
] = isl_basic_map_deltas_map(map
->p
[i
]);
7376 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
7383 static __isl_give isl_basic_map
*basic_map_identity(__isl_take isl_space
*dims
)
7385 struct isl_basic_map
*bmap
;
7393 nparam
= dims
->nparam
;
7395 bmap
= isl_basic_map_alloc_space(dims
, 0, dim
, 0);
7399 for (i
= 0; i
< dim
; ++i
) {
7400 int j
= isl_basic_map_alloc_equality(bmap
);
7403 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
7404 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
7405 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], -1);
7407 return isl_basic_map_finalize(bmap
);
7409 isl_basic_map_free(bmap
);
7413 __isl_give isl_basic_map
*isl_basic_map_identity(__isl_take isl_space
*dim
)
7417 if (dim
->n_in
!= dim
->n_out
)
7418 isl_die(dim
->ctx
, isl_error_invalid
,
7419 "number of input and output dimensions needs to be "
7420 "the same", goto error
);
7421 return basic_map_identity(dim
);
7423 isl_space_free(dim
);
7427 struct isl_basic_map
*isl_basic_map_identity_like(struct isl_basic_map
*model
)
7429 if (!model
|| !model
->dim
)
7431 return isl_basic_map_identity(isl_space_copy(model
->dim
));
7434 __isl_give isl_map
*isl_map_identity(__isl_take isl_space
*dim
)
7436 return isl_map_from_basic_map(isl_basic_map_identity(dim
));
7439 struct isl_map
*isl_map_identity_like(struct isl_map
*model
)
7441 if (!model
|| !model
->dim
)
7443 return isl_map_identity(isl_space_copy(model
->dim
));
7446 struct isl_map
*isl_map_identity_like_basic_map(struct isl_basic_map
*model
)
7448 if (!model
|| !model
->dim
)
7450 return isl_map_identity(isl_space_copy(model
->dim
));
7453 __isl_give isl_map
*isl_set_identity(__isl_take isl_set
*set
)
7455 isl_space
*dim
= isl_set_get_space(set
);
7457 id
= isl_map_identity(isl_space_map_from_set(dim
));
7458 return isl_map_intersect_range(id
, set
);
7461 /* Construct a basic set with all set dimensions having only non-negative
7464 __isl_give isl_basic_set
*isl_basic_set_positive_orthant(
7465 __isl_take isl_space
*space
)
7470 struct isl_basic_set
*bset
;
7474 nparam
= space
->nparam
;
7476 bset
= isl_basic_set_alloc_space(space
, 0, 0, dim
);
7479 for (i
= 0; i
< dim
; ++i
) {
7480 int k
= isl_basic_set_alloc_inequality(bset
);
7483 isl_seq_clr(bset
->ineq
[k
], 1 + isl_basic_set_total_dim(bset
));
7484 isl_int_set_si(bset
->ineq
[k
][1 + nparam
+ i
], 1);
7488 isl_basic_set_free(bset
);
7492 /* Construct the half-space x_pos >= 0.
7494 static __isl_give isl_basic_set
*nonneg_halfspace(__isl_take isl_space
*dim
,
7498 isl_basic_set
*nonneg
;
7500 nonneg
= isl_basic_set_alloc_space(dim
, 0, 0, 1);
7501 k
= isl_basic_set_alloc_inequality(nonneg
);
7504 isl_seq_clr(nonneg
->ineq
[k
], 1 + isl_basic_set_total_dim(nonneg
));
7505 isl_int_set_si(nonneg
->ineq
[k
][pos
], 1);
7507 return isl_basic_set_finalize(nonneg
);
7509 isl_basic_set_free(nonneg
);
7513 /* Construct the half-space x_pos <= -1.
7515 static __isl_give isl_basic_set
*neg_halfspace(__isl_take isl_space
*dim
, int pos
)
7520 neg
= isl_basic_set_alloc_space(dim
, 0, 0, 1);
7521 k
= isl_basic_set_alloc_inequality(neg
);
7524 isl_seq_clr(neg
->ineq
[k
], 1 + isl_basic_set_total_dim(neg
));
7525 isl_int_set_si(neg
->ineq
[k
][0], -1);
7526 isl_int_set_si(neg
->ineq
[k
][pos
], -1);
7528 return isl_basic_set_finalize(neg
);
7530 isl_basic_set_free(neg
);
7534 __isl_give isl_set
*isl_set_split_dims(__isl_take isl_set
*set
,
7535 enum isl_dim_type type
, unsigned first
, unsigned n
)
7538 isl_basic_set
*nonneg
;
7546 isl_assert(set
->ctx
, first
+ n
<= isl_set_dim(set
, type
), goto error
);
7548 for (i
= 0; i
< n
; ++i
) {
7549 nonneg
= nonneg_halfspace(isl_set_get_space(set
),
7550 pos(set
->dim
, type
) + first
+ i
);
7551 neg
= neg_halfspace(isl_set_get_space(set
),
7552 pos(set
->dim
, type
) + first
+ i
);
7554 set
= isl_set_intersect(set
, isl_basic_set_union(nonneg
, neg
));
7563 static int foreach_orthant(__isl_take isl_set
*set
, int *signs
, int first
,
7564 int len
, int (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
7571 if (isl_set_plain_is_empty(set
)) {
7576 return fn(set
, signs
, user
);
7579 half
= isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set
),
7581 half
= isl_set_intersect(half
, isl_set_copy(set
));
7582 if (foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
) < 0)
7586 half
= isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set
),
7588 half
= isl_set_intersect(half
, set
);
7589 return foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
);
7595 /* Call "fn" on the intersections of "set" with each of the orthants
7596 * (except for obviously empty intersections). The orthant is identified
7597 * by the signs array, with each entry having value 1 or -1 according
7598 * to the sign of the corresponding variable.
7600 int isl_set_foreach_orthant(__isl_keep isl_set
*set
,
7601 int (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
7611 if (isl_set_plain_is_empty(set
))
7614 nparam
= isl_set_dim(set
, isl_dim_param
);
7615 nvar
= isl_set_dim(set
, isl_dim_set
);
7617 signs
= isl_alloc_array(set
->ctx
, int, nparam
+ nvar
);
7619 r
= foreach_orthant(isl_set_copy(set
), signs
, 0, nparam
+ nvar
,
7627 int isl_set_is_equal(struct isl_set
*set1
, struct isl_set
*set2
)
7629 return isl_map_is_equal((struct isl_map
*)set1
, (struct isl_map
*)set2
);
7632 int isl_basic_map_is_subset(
7633 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
7636 struct isl_map
*map1
;
7637 struct isl_map
*map2
;
7639 if (!bmap1
|| !bmap2
)
7642 map1
= isl_map_from_basic_map(isl_basic_map_copy(bmap1
));
7643 map2
= isl_map_from_basic_map(isl_basic_map_copy(bmap2
));
7645 is_subset
= isl_map_is_subset(map1
, map2
);
7653 int isl_basic_set_is_subset(__isl_keep isl_basic_set
*bset1
,
7654 __isl_keep isl_basic_set
*bset2
)
7656 return isl_basic_map_is_subset(bset1
, bset2
);
7659 int isl_basic_map_is_equal(
7660 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
7664 if (!bmap1
|| !bmap2
)
7666 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
7669 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
7673 int isl_basic_set_is_equal(
7674 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
7676 return isl_basic_map_is_equal(
7677 (struct isl_basic_map
*)bset1
, (struct isl_basic_map
*)bset2
);
7680 int isl_map_is_empty(struct isl_map
*map
)
7687 for (i
= 0; i
< map
->n
; ++i
) {
7688 is_empty
= isl_basic_map_is_empty(map
->p
[i
]);
7697 int isl_map_plain_is_empty(__isl_keep isl_map
*map
)
7699 return map
? map
->n
== 0 : -1;
7702 int isl_map_fast_is_empty(__isl_keep isl_map
*map
)
7704 return isl_map_plain_is_empty(map
);
7707 int isl_set_plain_is_empty(struct isl_set
*set
)
7709 return set
? set
->n
== 0 : -1;
7712 int isl_set_fast_is_empty(__isl_keep isl_set
*set
)
7714 return isl_set_plain_is_empty(set
);
7717 int isl_set_is_empty(struct isl_set
*set
)
7719 return isl_map_is_empty((struct isl_map
*)set
);
7722 int isl_map_has_equal_space(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
7727 return isl_space_is_equal(map1
->dim
, map2
->dim
);
7730 int isl_set_has_equal_space(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
7735 return isl_space_is_equal(set1
->dim
, set2
->dim
);
7738 static int map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
7744 is_subset
= isl_map_is_subset(map1
, map2
);
7747 is_subset
= isl_map_is_subset(map2
, map1
);
7751 int isl_map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
7753 return isl_map_align_params_map_map_and_test(map1
, map2
, &map_is_equal
);
7756 int isl_basic_map_is_strict_subset(
7757 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
7761 if (!bmap1
|| !bmap2
)
7763 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
7766 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
7767 if (is_subset
== -1)
7772 int isl_map_is_strict_subset(struct isl_map
*map1
, struct isl_map
*map2
)
7778 is_subset
= isl_map_is_subset(map1
, map2
);
7781 is_subset
= isl_map_is_subset(map2
, map1
);
7782 if (is_subset
== -1)
7787 int isl_set_is_strict_subset(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
7789 return isl_map_is_strict_subset((isl_map
*)set1
, (isl_map
*)set2
);
7792 int isl_basic_map_is_universe(struct isl_basic_map
*bmap
)
7796 return bmap
->n_eq
== 0 && bmap
->n_ineq
== 0;
7799 int isl_basic_set_is_universe(struct isl_basic_set
*bset
)
7803 return bset
->n_eq
== 0 && bset
->n_ineq
== 0;
7806 int isl_map_plain_is_universe(__isl_keep isl_map
*map
)
7813 for (i
= 0; i
< map
->n
; ++i
) {
7814 int r
= isl_basic_map_is_universe(map
->p
[i
]);
7822 int isl_set_plain_is_universe(__isl_keep isl_set
*set
)
7824 return isl_map_plain_is_universe((isl_map
*) set
);
7827 int isl_set_fast_is_universe(__isl_keep isl_set
*set
)
7829 return isl_set_plain_is_universe(set
);
7832 int isl_basic_map_is_empty(struct isl_basic_map
*bmap
)
7834 struct isl_basic_set
*bset
= NULL
;
7835 struct isl_vec
*sample
= NULL
;
7842 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
7845 if (isl_basic_map_is_universe(bmap
))
7848 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
7849 struct isl_basic_map
*copy
= isl_basic_map_copy(bmap
);
7850 copy
= isl_basic_map_remove_redundancies(copy
);
7851 empty
= isl_basic_map_plain_is_empty(copy
);
7852 isl_basic_map_free(copy
);
7856 total
= 1 + isl_basic_map_total_dim(bmap
);
7857 if (bmap
->sample
&& bmap
->sample
->size
== total
) {
7858 int contains
= isl_basic_map_contains(bmap
, bmap
->sample
);
7864 isl_vec_free(bmap
->sample
);
7865 bmap
->sample
= NULL
;
7866 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
7869 sample
= isl_basic_set_sample_vec(bset
);
7872 empty
= sample
->size
== 0;
7873 isl_vec_free(bmap
->sample
);
7874 bmap
->sample
= sample
;
7876 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
7881 int isl_basic_map_plain_is_empty(__isl_keep isl_basic_map
*bmap
)
7885 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
);
7888 int isl_basic_map_fast_is_empty(__isl_keep isl_basic_map
*bmap
)
7890 return isl_basic_map_plain_is_empty(bmap
);
7893 int isl_basic_set_plain_is_empty(__isl_keep isl_basic_set
*bset
)
7897 return ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
);
7900 int isl_basic_set_fast_is_empty(__isl_keep isl_basic_set
*bset
)
7902 return isl_basic_set_plain_is_empty(bset
);
7905 int isl_basic_set_is_empty(struct isl_basic_set
*bset
)
7907 return isl_basic_map_is_empty((struct isl_basic_map
*)bset
);
7910 struct isl_map
*isl_basic_map_union(
7911 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
7913 struct isl_map
*map
;
7914 if (!bmap1
|| !bmap2
)
7917 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
7919 map
= isl_map_alloc_space(isl_space_copy(bmap1
->dim
), 2, 0);
7922 map
= isl_map_add_basic_map(map
, bmap1
);
7923 map
= isl_map_add_basic_map(map
, bmap2
);
7926 isl_basic_map_free(bmap1
);
7927 isl_basic_map_free(bmap2
);
7931 struct isl_set
*isl_basic_set_union(
7932 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
7934 return (struct isl_set
*)isl_basic_map_union(
7935 (struct isl_basic_map
*)bset1
,
7936 (struct isl_basic_map
*)bset2
);
7939 /* Order divs such that any div only depends on previous divs */
7940 struct isl_basic_map
*isl_basic_map_order_divs(struct isl_basic_map
*bmap
)
7948 off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
7950 for (i
= 0; i
< bmap
->n_div
; ++i
) {
7952 if (isl_int_is_zero(bmap
->div
[i
][0]))
7954 pos
= isl_seq_first_non_zero(bmap
->div
[i
]+1+1+off
+i
,
7958 isl_basic_map_swap_div(bmap
, i
, i
+ pos
);
7964 struct isl_basic_set
*isl_basic_set_order_divs(struct isl_basic_set
*bset
)
7966 return (struct isl_basic_set
*)
7967 isl_basic_map_order_divs((struct isl_basic_map
*)bset
);
7970 __isl_give isl_map
*isl_map_order_divs(__isl_take isl_map
*map
)
7977 for (i
= 0; i
< map
->n
; ++i
) {
7978 map
->p
[i
] = isl_basic_map_order_divs(map
->p
[i
]);
7989 /* Apply the expansion computed by isl_merge_divs.
7990 * The expansion itself is given by "exp" while the resulting
7991 * list of divs is given by "div".
7993 __isl_give isl_basic_set
*isl_basic_set_expand_divs(
7994 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*div
, int *exp
)
7999 bset
= isl_basic_set_cow(bset
);
8003 if (div
->n_row
< bset
->n_div
)
8004 isl_die(isl_mat_get_ctx(div
), isl_error_invalid
,
8005 "not an expansion", goto error
);
8007 n_div
= bset
->n_div
;
8008 bset
= isl_basic_map_extend_space(bset
, isl_space_copy(bset
->dim
),
8009 div
->n_row
- n_div
, 0,
8010 2 * (div
->n_row
- n_div
));
8012 for (i
= n_div
; i
< div
->n_row
; ++i
)
8013 if (isl_basic_set_alloc_div(bset
) < 0)
8017 for (i
= div
->n_row
- 1; i
>= 0; --i
) {
8018 if (j
>= 0 && exp
[j
] == i
) {
8020 isl_basic_map_swap_div(bset
, i
, j
);
8023 isl_seq_cpy(bset
->div
[i
], div
->row
[i
], div
->n_col
);
8024 if (isl_basic_map_add_div_constraints(bset
, i
) < 0)
8032 isl_basic_set_free(bset
);
8037 /* Look for a div in dst that corresponds to the div "div" in src.
8038 * The divs before "div" in src and dst are assumed to be the same.
8040 * Returns -1 if no corresponding div was found and the position
8041 * of the corresponding div in dst otherwise.
8043 static int find_div(struct isl_basic_map
*dst
,
8044 struct isl_basic_map
*src
, unsigned div
)
8048 unsigned total
= isl_space_dim(src
->dim
, isl_dim_all
);
8050 isl_assert(dst
->ctx
, div
<= dst
->n_div
, return -1);
8051 for (i
= div
; i
< dst
->n_div
; ++i
)
8052 if (isl_seq_eq(dst
->div
[i
], src
->div
[div
], 1+1+total
+div
) &&
8053 isl_seq_first_non_zero(dst
->div
[i
]+1+1+total
+div
,
8054 dst
->n_div
- div
) == -1)
8059 struct isl_basic_map
*isl_basic_map_align_divs(
8060 struct isl_basic_map
*dst
, struct isl_basic_map
*src
)
8068 if (src
->n_div
== 0)
8071 for (i
= 0; i
< src
->n_div
; ++i
)
8072 isl_assert(src
->ctx
, !isl_int_is_zero(src
->div
[i
][0]), goto error
);
8074 src
= isl_basic_map_order_divs(src
);
8075 dst
= isl_basic_map_cow(dst
);
8078 dst
= isl_basic_map_extend_space(dst
, isl_space_copy(dst
->dim
),
8079 src
->n_div
, 0, 2 * src
->n_div
);
8082 total
= isl_space_dim(src
->dim
, isl_dim_all
);
8083 for (i
= 0; i
< src
->n_div
; ++i
) {
8084 int j
= find_div(dst
, src
, i
);
8086 j
= isl_basic_map_alloc_div(dst
);
8089 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
+i
);
8090 isl_seq_clr(dst
->div
[j
]+1+1+total
+i
, dst
->n_div
- i
);
8091 if (isl_basic_map_add_div_constraints(dst
, j
) < 0)
8095 isl_basic_map_swap_div(dst
, i
, j
);
8099 isl_basic_map_free(dst
);
8103 struct isl_basic_set
*isl_basic_set_align_divs(
8104 struct isl_basic_set
*dst
, struct isl_basic_set
*src
)
8106 return (struct isl_basic_set
*)isl_basic_map_align_divs(
8107 (struct isl_basic_map
*)dst
, (struct isl_basic_map
*)src
);
8110 struct isl_map
*isl_map_align_divs(struct isl_map
*map
)
8118 map
= isl_map_compute_divs(map
);
8119 map
= isl_map_cow(map
);
8123 for (i
= 1; i
< map
->n
; ++i
)
8124 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], map
->p
[i
]);
8125 for (i
= 1; i
< map
->n
; ++i
) {
8126 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], map
->p
[0]);
8128 return isl_map_free(map
);
8131 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
8135 struct isl_set
*isl_set_align_divs(struct isl_set
*set
)
8137 return (struct isl_set
*)isl_map_align_divs((struct isl_map
*)set
);
8140 static __isl_give isl_set
*set_apply( __isl_take isl_set
*set
,
8141 __isl_take isl_map
*map
)
8145 isl_assert(set
->ctx
, isl_map_compatible_domain(map
, set
), goto error
);
8146 map
= isl_map_intersect_domain(map
, set
);
8147 set
= isl_map_range(map
);
8155 __isl_give isl_set
*isl_set_apply( __isl_take isl_set
*set
,
8156 __isl_take isl_map
*map
)
8158 return isl_map_align_params_map_map_and(set
, map
, &set_apply
);
8161 /* There is no need to cow as removing empty parts doesn't change
8162 * the meaning of the set.
8164 struct isl_map
*isl_map_remove_empty_parts(struct isl_map
*map
)
8171 for (i
= map
->n
- 1; i
>= 0; --i
)
8172 remove_if_empty(map
, i
);
8177 struct isl_set
*isl_set_remove_empty_parts(struct isl_set
*set
)
8179 return (struct isl_set
*)
8180 isl_map_remove_empty_parts((struct isl_map
*)set
);
8183 struct isl_basic_map
*isl_map_copy_basic_map(struct isl_map
*map
)
8185 struct isl_basic_map
*bmap
;
8186 if (!map
|| map
->n
== 0)
8188 bmap
= map
->p
[map
->n
-1];
8189 isl_assert(map
->ctx
, ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
), return NULL
);
8190 return isl_basic_map_copy(bmap
);
8193 struct isl_basic_set
*isl_set_copy_basic_set(struct isl_set
*set
)
8195 return (struct isl_basic_set
*)
8196 isl_map_copy_basic_map((struct isl_map
*)set
);
8199 __isl_give isl_map
*isl_map_drop_basic_map(__isl_take isl_map
*map
,
8200 __isl_keep isl_basic_map
*bmap
)
8206 for (i
= map
->n
-1; i
>= 0; --i
) {
8207 if (map
->p
[i
] != bmap
)
8209 map
= isl_map_cow(map
);
8212 isl_basic_map_free(map
->p
[i
]);
8213 if (i
!= map
->n
-1) {
8214 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
8215 map
->p
[i
] = map
->p
[map
->n
-1];
8226 struct isl_set
*isl_set_drop_basic_set(struct isl_set
*set
,
8227 struct isl_basic_set
*bset
)
8229 return (struct isl_set
*)isl_map_drop_basic_map((struct isl_map
*)set
,
8230 (struct isl_basic_map
*)bset
);
8233 /* Given two basic sets bset1 and bset2, compute the maximal difference
8234 * between the values of dimension pos in bset1 and those in bset2
8235 * for any common value of the parameters and dimensions preceding pos.
8237 static enum isl_lp_result
basic_set_maximal_difference_at(
8238 __isl_keep isl_basic_set
*bset1
, __isl_keep isl_basic_set
*bset2
,
8239 int pos
, isl_int
*opt
)
8242 struct isl_basic_map
*bmap1
= NULL
;
8243 struct isl_basic_map
*bmap2
= NULL
;
8244 struct isl_ctx
*ctx
;
8245 struct isl_vec
*obj
;
8248 unsigned dim1
, dim2
;
8249 enum isl_lp_result res
;
8251 if (!bset1
|| !bset2
)
8252 return isl_lp_error
;
8254 nparam
= isl_basic_set_n_param(bset1
);
8255 dim1
= isl_basic_set_n_dim(bset1
);
8256 dim2
= isl_basic_set_n_dim(bset2
);
8257 dims
= isl_space_alloc(bset1
->ctx
, nparam
, pos
, dim1
- pos
);
8258 bmap1
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset1
), dims
);
8259 dims
= isl_space_alloc(bset2
->ctx
, nparam
, pos
, dim2
- pos
);
8260 bmap2
= isl_basic_map_from_basic_set(isl_basic_set_copy(bset2
), dims
);
8261 if (!bmap1
|| !bmap2
)
8263 bmap1
= isl_basic_map_cow(bmap1
);
8264 bmap1
= isl_basic_map_extend(bmap1
, nparam
,
8265 pos
, (dim1
- pos
) + (dim2
- pos
),
8266 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
8267 bmap1
= add_constraints(bmap1
, bmap2
, 0, dim1
- pos
);
8270 total
= isl_basic_map_total_dim(bmap1
);
8272 obj
= isl_vec_alloc(ctx
, 1 + total
);
8275 isl_seq_clr(obj
->block
.data
, 1 + total
);
8276 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
], 1);
8277 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
+(dim1
-pos
)], -1);
8278 res
= isl_basic_map_solve_lp(bmap1
, 1, obj
->block
.data
, ctx
->one
,
8280 isl_basic_map_free(bmap1
);
8284 isl_basic_map_free(bmap2
);
8286 isl_basic_map_free(bmap1
);
8287 return isl_lp_error
;
8290 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
8291 * for any common value of the parameters and dimensions preceding pos
8292 * in both basic sets, the values of dimension pos in bset1 are
8293 * smaller or larger than those in bset2.
8296 * 1 if bset1 follows bset2
8297 * -1 if bset1 precedes bset2
8298 * 0 if bset1 and bset2 are incomparable
8299 * -2 if some error occurred.
8301 int isl_basic_set_compare_at(struct isl_basic_set
*bset1
,
8302 struct isl_basic_set
*bset2
, int pos
)
8305 enum isl_lp_result res
;
8310 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
8312 if (res
== isl_lp_empty
)
8314 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
8315 res
== isl_lp_unbounded
)
8317 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
8326 /* Given two basic sets bset1 and bset2, check whether
8327 * for any common value of the parameters and dimensions preceding pos
8328 * there is a value of dimension pos in bset1 that is larger
8329 * than a value of the same dimension in bset2.
8332 * 1 if there exists such a pair
8333 * 0 if there is no such pair, but there is a pair of equal values
8335 * -2 if some error occurred.
8337 int isl_basic_set_follows_at(__isl_keep isl_basic_set
*bset1
,
8338 __isl_keep isl_basic_set
*bset2
, int pos
)
8341 enum isl_lp_result res
;
8346 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
8348 if (res
== isl_lp_empty
)
8350 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
8351 res
== isl_lp_unbounded
)
8353 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
8355 else if (res
== isl_lp_ok
)
8364 /* Given two sets set1 and set2, check whether
8365 * for any common value of the parameters and dimensions preceding pos
8366 * there is a value of dimension pos in set1 that is larger
8367 * than a value of the same dimension in set2.
8370 * 1 if there exists such a pair
8371 * 0 if there is no such pair, but there is a pair of equal values
8373 * -2 if some error occurred.
8375 int isl_set_follows_at(__isl_keep isl_set
*set1
,
8376 __isl_keep isl_set
*set2
, int pos
)
8384 for (i
= 0; i
< set1
->n
; ++i
)
8385 for (j
= 0; j
< set2
->n
; ++j
) {
8387 f
= isl_basic_set_follows_at(set1
->p
[i
], set2
->p
[j
], pos
);
8388 if (f
== 1 || f
== -2)
8397 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map
*bmap
,
8398 unsigned pos
, isl_int
*val
)
8406 total
= isl_basic_map_total_dim(bmap
);
8407 for (i
= 0, d
= total
-1; i
< bmap
->n_eq
&& d
+1 > pos
; ++i
) {
8408 for (; d
+1 > pos
; --d
)
8409 if (!isl_int_is_zero(bmap
->eq
[i
][1+d
]))
8413 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1, d
) != -1)
8415 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1+d
+1, total
-d
-1) != -1)
8417 if (!isl_int_is_one(bmap
->eq
[i
][1+d
]))
8420 isl_int_neg(*val
, bmap
->eq
[i
][0]);
8426 static int isl_map_plain_has_fixed_var(__isl_keep isl_map
*map
,
8427 unsigned pos
, isl_int
*val
)
8439 return isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, val
);
8442 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, &v
);
8443 for (i
= 1; fixed
== 1 && i
< map
->n
; ++i
) {
8444 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[i
], pos
, &tmp
);
8445 if (fixed
== 1 && isl_int_ne(tmp
, v
))
8449 isl_int_set(*val
, v
);
8455 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set
*bset
,
8456 unsigned pos
, isl_int
*val
)
8458 return isl_basic_map_plain_has_fixed_var((struct isl_basic_map
*)bset
,
8462 static int isl_set_plain_has_fixed_var(__isl_keep isl_set
*set
, unsigned pos
,
8465 return isl_map_plain_has_fixed_var((struct isl_map
*)set
, pos
, val
);
8468 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map
*bmap
,
8469 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
8471 if (pos
>= isl_basic_map_dim(bmap
, type
))
8473 return isl_basic_map_plain_has_fixed_var(bmap
,
8474 isl_basic_map_offset(bmap
, type
) - 1 + pos
, val
);
8477 /* If "bmap" obviously lies on a hyperplane where the given dimension
8478 * has a fixed value, then return that value.
8479 * Otherwise return NaN.
8481 __isl_give isl_val
*isl_basic_map_plain_get_val_if_fixed(
8482 __isl_keep isl_basic_map
*bmap
,
8483 enum isl_dim_type type
, unsigned pos
)
8491 ctx
= isl_basic_map_get_ctx(bmap
);
8492 v
= isl_val_alloc(ctx
);
8495 fixed
= isl_basic_map_plain_is_fixed(bmap
, type
, pos
, &v
->n
);
8497 return isl_val_free(v
);
8499 isl_int_set_si(v
->d
, 1);
8503 return isl_val_nan(ctx
);
8506 int isl_map_plain_is_fixed(__isl_keep isl_map
*map
,
8507 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
8509 if (pos
>= isl_map_dim(map
, type
))
8511 return isl_map_plain_has_fixed_var(map
,
8512 map_offset(map
, type
) - 1 + pos
, val
);
8515 /* If "map" obviously lies on a hyperplane where the given dimension
8516 * has a fixed value, then return that value.
8517 * Otherwise return NaN.
8519 __isl_give isl_val
*isl_map_plain_get_val_if_fixed(__isl_keep isl_map
*map
,
8520 enum isl_dim_type type
, unsigned pos
)
8528 ctx
= isl_map_get_ctx(map
);
8529 v
= isl_val_alloc(ctx
);
8532 fixed
= isl_map_plain_is_fixed(map
, type
, pos
, &v
->n
);
8534 return isl_val_free(v
);
8536 isl_int_set_si(v
->d
, 1);
8540 return isl_val_nan(ctx
);
8543 /* If "set" obviously lies on a hyperplane where the given dimension
8544 * has a fixed value, then return that value.
8545 * Otherwise return NaN.
8547 __isl_give isl_val
*isl_set_plain_get_val_if_fixed(__isl_keep isl_set
*set
,
8548 enum isl_dim_type type
, unsigned pos
)
8550 return isl_map_plain_get_val_if_fixed(set
, type
, pos
);
8553 int isl_set_plain_is_fixed(__isl_keep isl_set
*set
,
8554 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
8556 return isl_map_plain_is_fixed(set
, type
, pos
, val
);
8559 int isl_map_fast_is_fixed(__isl_keep isl_map
*map
,
8560 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
8562 return isl_map_plain_is_fixed(map
, type
, pos
, val
);
8565 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8566 * then return this fixed value in *val.
8568 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set
*bset
,
8569 unsigned dim
, isl_int
*val
)
8571 return isl_basic_set_plain_has_fixed_var(bset
,
8572 isl_basic_set_n_param(bset
) + dim
, val
);
8575 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8576 * then return this fixed value in *val.
8578 int isl_set_plain_dim_is_fixed(__isl_keep isl_set
*set
,
8579 unsigned dim
, isl_int
*val
)
8581 return isl_set_plain_has_fixed_var(set
, isl_set_n_param(set
) + dim
, val
);
8584 int isl_set_fast_dim_is_fixed(__isl_keep isl_set
*set
,
8585 unsigned dim
, isl_int
*val
)
8587 return isl_set_plain_dim_is_fixed(set
, dim
, val
);
8590 /* Check if input variable in has fixed value and if so and if val is not NULL,
8591 * then return this fixed value in *val.
8593 int isl_map_plain_input_is_fixed(__isl_keep isl_map
*map
,
8594 unsigned in
, isl_int
*val
)
8596 return isl_map_plain_has_fixed_var(map
, isl_map_n_param(map
) + in
, val
);
8599 /* Check if dimension dim has an (obvious) fixed lower bound and if so
8600 * and if val is not NULL, then return this lower bound in *val.
8602 int isl_basic_set_plain_dim_has_fixed_lower_bound(
8603 __isl_keep isl_basic_set
*bset
, unsigned dim
, isl_int
*val
)
8605 int i
, i_eq
= -1, i_ineq
= -1;
8612 total
= isl_basic_set_total_dim(bset
);
8613 nparam
= isl_basic_set_n_param(bset
);
8614 for (i
= 0; i
< bset
->n_eq
; ++i
) {
8615 if (isl_int_is_zero(bset
->eq
[i
][1+nparam
+dim
]))
8621 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
8622 if (!isl_int_is_pos(bset
->ineq
[i
][1+nparam
+dim
]))
8624 if (i_eq
!= -1 || i_ineq
!= -1)
8628 if (i_eq
== -1 && i_ineq
== -1)
8630 c
= i_eq
!= -1 ? bset
->eq
[i_eq
] : bset
->ineq
[i_ineq
];
8631 /* The coefficient should always be one due to normalization. */
8632 if (!isl_int_is_one(c
[1+nparam
+dim
]))
8634 if (isl_seq_first_non_zero(c
+1, nparam
+dim
) != -1)
8636 if (isl_seq_first_non_zero(c
+1+nparam
+dim
+1,
8637 total
- nparam
- dim
- 1) != -1)
8640 isl_int_neg(*val
, c
[0]);
8644 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set
*set
,
8645 unsigned dim
, isl_int
*val
)
8657 return isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[0],
8661 fixed
= isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[0],
8663 for (i
= 1; fixed
== 1 && i
< set
->n
; ++i
) {
8664 fixed
= isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[i
],
8666 if (fixed
== 1 && isl_int_ne(tmp
, v
))
8670 isl_int_set(*val
, v
);
8681 /* uset_gist depends on constraints without existentially quantified
8682 * variables sorting first.
8684 static int qsort_constraint_cmp(const void *p1
, const void *p2
)
8686 const struct constraint
*c1
= (const struct constraint
*)p1
;
8687 const struct constraint
*c2
= (const struct constraint
*)p2
;
8689 unsigned size
= isl_min(c1
->size
, c2
->size
);
8691 l1
= isl_seq_last_non_zero(c1
->c
+ 1, size
);
8692 l2
= isl_seq_last_non_zero(c2
->c
+ 1, size
);
8697 return isl_seq_cmp(c1
->c
+ 1, c2
->c
+ 1, size
);
8700 static struct isl_basic_map
*isl_basic_map_sort_constraints(
8701 struct isl_basic_map
*bmap
)
8704 struct constraint
*c
;
8709 total
= isl_basic_map_total_dim(bmap
);
8710 c
= isl_alloc_array(bmap
->ctx
, struct constraint
, bmap
->n_ineq
);
8713 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
8715 c
[i
].c
= bmap
->ineq
[i
];
8717 qsort(c
, bmap
->n_ineq
, sizeof(struct constraint
), qsort_constraint_cmp
);
8718 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
8719 bmap
->ineq
[i
] = c
[i
].c
;
8723 isl_basic_map_free(bmap
);
8727 __isl_give isl_basic_set
*isl_basic_set_sort_constraints(
8728 __isl_take isl_basic_set
*bset
)
8730 return (struct isl_basic_set
*)isl_basic_map_sort_constraints(
8731 (struct isl_basic_map
*)bset
);
8734 struct isl_basic_map
*isl_basic_map_normalize(struct isl_basic_map
*bmap
)
8738 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED
))
8740 bmap
= isl_basic_map_remove_redundancies(bmap
);
8741 bmap
= isl_basic_map_sort_constraints(bmap
);
8743 ISL_F_SET(bmap
, ISL_BASIC_MAP_NORMALIZED
);
8747 struct isl_basic_set
*isl_basic_set_normalize(struct isl_basic_set
*bset
)
8749 return (struct isl_basic_set
*)isl_basic_map_normalize(
8750 (struct isl_basic_map
*)bset
);
8753 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map
*bmap1
,
8754 const __isl_keep isl_basic_map
*bmap2
)
8761 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) !=
8762 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_RATIONAL
))
8763 return ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) ? -1 : 1;
8764 if (isl_basic_map_n_param(bmap1
) != isl_basic_map_n_param(bmap2
))
8765 return isl_basic_map_n_param(bmap1
) - isl_basic_map_n_param(bmap2
);
8766 if (isl_basic_map_n_in(bmap1
) != isl_basic_map_n_in(bmap2
))
8767 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
8768 if (isl_basic_map_n_out(bmap1
) != isl_basic_map_n_out(bmap2
))
8769 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
8770 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
) &&
8771 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
8773 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
))
8775 if (ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
8777 if (bmap1
->n_eq
!= bmap2
->n_eq
)
8778 return bmap1
->n_eq
- bmap2
->n_eq
;
8779 if (bmap1
->n_ineq
!= bmap2
->n_ineq
)
8780 return bmap1
->n_ineq
- bmap2
->n_ineq
;
8781 if (bmap1
->n_div
!= bmap2
->n_div
)
8782 return bmap1
->n_div
- bmap2
->n_div
;
8783 total
= isl_basic_map_total_dim(bmap1
);
8784 for (i
= 0; i
< bmap1
->n_eq
; ++i
) {
8785 cmp
= isl_seq_cmp(bmap1
->eq
[i
], bmap2
->eq
[i
], 1+total
);
8789 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
8790 cmp
= isl_seq_cmp(bmap1
->ineq
[i
], bmap2
->ineq
[i
], 1+total
);
8794 for (i
= 0; i
< bmap1
->n_div
; ++i
) {
8795 cmp
= isl_seq_cmp(bmap1
->div
[i
], bmap2
->div
[i
], 1+1+total
);
8802 int isl_basic_set_plain_cmp(const __isl_keep isl_basic_set
*bset1
,
8803 const __isl_keep isl_basic_set
*bset2
)
8805 return isl_basic_map_plain_cmp(bset1
, bset2
);
8808 int isl_set_plain_cmp(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
8814 if (set1
->n
!= set2
->n
)
8815 return set1
->n
- set2
->n
;
8817 for (i
= 0; i
< set1
->n
; ++i
) {
8818 cmp
= isl_basic_set_plain_cmp(set1
->p
[i
], set2
->p
[i
]);
8826 int isl_basic_map_plain_is_equal(__isl_keep isl_basic_map
*bmap1
,
8827 __isl_keep isl_basic_map
*bmap2
)
8829 return isl_basic_map_plain_cmp(bmap1
, bmap2
) == 0;
8832 int isl_basic_set_plain_is_equal(__isl_keep isl_basic_set
*bset1
,
8833 __isl_keep isl_basic_set
*bset2
)
8835 return isl_basic_map_plain_is_equal((isl_basic_map
*)bset1
,
8836 (isl_basic_map
*)bset2
);
8839 static int qsort_bmap_cmp(const void *p1
, const void *p2
)
8841 const struct isl_basic_map
*bmap1
= *(const struct isl_basic_map
**)p1
;
8842 const struct isl_basic_map
*bmap2
= *(const struct isl_basic_map
**)p2
;
8844 return isl_basic_map_plain_cmp(bmap1
, bmap2
);
8847 /* We normalize in place, but if anything goes wrong we need
8848 * to return NULL, so we need to make sure we don't change the
8849 * meaning of any possible other copies of map.
8851 struct isl_map
*isl_map_normalize(struct isl_map
*map
)
8854 struct isl_basic_map
*bmap
;
8858 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
))
8860 for (i
= 0; i
< map
->n
; ++i
) {
8861 bmap
= isl_basic_map_normalize(isl_basic_map_copy(map
->p
[i
]));
8864 isl_basic_map_free(map
->p
[i
]);
8867 qsort(map
->p
, map
->n
, sizeof(struct isl_basic_map
*), qsort_bmap_cmp
);
8868 ISL_F_SET(map
, ISL_MAP_NORMALIZED
);
8869 map
= isl_map_remove_empty_parts(map
);
8872 for (i
= map
->n
- 1; i
>= 1; --i
) {
8873 if (!isl_basic_map_plain_is_equal(map
->p
[i
-1], map
->p
[i
]))
8875 isl_basic_map_free(map
->p
[i
-1]);
8876 for (j
= i
; j
< map
->n
; ++j
)
8877 map
->p
[j
-1] = map
->p
[j
];
8887 struct isl_set
*isl_set_normalize(struct isl_set
*set
)
8889 return (struct isl_set
*)isl_map_normalize((struct isl_map
*)set
);
8892 int isl_map_plain_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
8902 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
8905 map1
= isl_map_copy(map1
);
8906 map2
= isl_map_copy(map2
);
8907 map1
= isl_map_normalize(map1
);
8908 map2
= isl_map_normalize(map2
);
8911 equal
= map1
->n
== map2
->n
;
8912 for (i
= 0; equal
&& i
< map1
->n
; ++i
) {
8913 equal
= isl_basic_map_plain_is_equal(map1
->p
[i
], map2
->p
[i
]);
8926 int isl_map_fast_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
8928 return isl_map_plain_is_equal(map1
, map2
);
8931 int isl_set_plain_is_equal(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
8933 return isl_map_plain_is_equal((struct isl_map
*)set1
,
8934 (struct isl_map
*)set2
);
8937 int isl_set_fast_is_equal(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
8939 return isl_set_plain_is_equal(set1
, set2
);
8942 /* Return an interval that ranges from min to max (inclusive)
8944 struct isl_basic_set
*isl_basic_set_interval(struct isl_ctx
*ctx
,
8945 isl_int min
, isl_int max
)
8948 struct isl_basic_set
*bset
= NULL
;
8950 bset
= isl_basic_set_alloc(ctx
, 0, 1, 0, 0, 2);
8954 k
= isl_basic_set_alloc_inequality(bset
);
8957 isl_int_set_si(bset
->ineq
[k
][1], 1);
8958 isl_int_neg(bset
->ineq
[k
][0], min
);
8960 k
= isl_basic_set_alloc_inequality(bset
);
8963 isl_int_set_si(bset
->ineq
[k
][1], -1);
8964 isl_int_set(bset
->ineq
[k
][0], max
);
8968 isl_basic_set_free(bset
);
8972 /* Return the Cartesian product of the basic sets in list (in the given order).
8974 __isl_give isl_basic_set
*isl_basic_set_list_product(
8975 __isl_take
struct isl_basic_set_list
*list
)
8983 struct isl_basic_set
*product
= NULL
;
8987 isl_assert(list
->ctx
, list
->n
> 0, goto error
);
8988 isl_assert(list
->ctx
, list
->p
[0], goto error
);
8989 nparam
= isl_basic_set_n_param(list
->p
[0]);
8990 dim
= isl_basic_set_n_dim(list
->p
[0]);
8991 extra
= list
->p
[0]->n_div
;
8992 n_eq
= list
->p
[0]->n_eq
;
8993 n_ineq
= list
->p
[0]->n_ineq
;
8994 for (i
= 1; i
< list
->n
; ++i
) {
8995 isl_assert(list
->ctx
, list
->p
[i
], goto error
);
8996 isl_assert(list
->ctx
,
8997 nparam
== isl_basic_set_n_param(list
->p
[i
]), goto error
);
8998 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
8999 extra
+= list
->p
[i
]->n_div
;
9000 n_eq
+= list
->p
[i
]->n_eq
;
9001 n_ineq
+= list
->p
[i
]->n_ineq
;
9003 product
= isl_basic_set_alloc(list
->ctx
, nparam
, dim
, extra
,
9008 for (i
= 0; i
< list
->n
; ++i
) {
9009 isl_basic_set_add_constraints(product
,
9010 isl_basic_set_copy(list
->p
[i
]), dim
);
9011 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
9013 isl_basic_set_list_free(list
);
9016 isl_basic_set_free(product
);
9017 isl_basic_set_list_free(list
);
9021 struct isl_basic_map
*isl_basic_map_product(
9022 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
9024 isl_space
*dim_result
= NULL
;
9025 struct isl_basic_map
*bmap
;
9026 unsigned in1
, in2
, out1
, out2
, nparam
, total
, pos
;
9027 struct isl_dim_map
*dim_map1
, *dim_map2
;
9029 if (!bmap1
|| !bmap2
)
9032 isl_assert(bmap1
->ctx
, isl_space_match(bmap1
->dim
, isl_dim_param
,
9033 bmap2
->dim
, isl_dim_param
), goto error
);
9034 dim_result
= isl_space_product(isl_space_copy(bmap1
->dim
),
9035 isl_space_copy(bmap2
->dim
));
9037 in1
= isl_basic_map_n_in(bmap1
);
9038 in2
= isl_basic_map_n_in(bmap2
);
9039 out1
= isl_basic_map_n_out(bmap1
);
9040 out2
= isl_basic_map_n_out(bmap2
);
9041 nparam
= isl_basic_map_n_param(bmap1
);
9043 total
= nparam
+ in1
+ in2
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
9044 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9045 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9046 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9047 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9048 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9049 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
9050 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
9051 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
9052 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
9053 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9055 bmap
= isl_basic_map_alloc_space(dim_result
,
9056 bmap1
->n_div
+ bmap2
->n_div
,
9057 bmap1
->n_eq
+ bmap2
->n_eq
,
9058 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9059 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9060 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9061 bmap
= isl_basic_map_simplify(bmap
);
9062 return isl_basic_map_finalize(bmap
);
9064 isl_basic_map_free(bmap1
);
9065 isl_basic_map_free(bmap2
);
9069 __isl_give isl_basic_map
*isl_basic_map_flat_product(
9070 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9072 isl_basic_map
*prod
;
9074 prod
= isl_basic_map_product(bmap1
, bmap2
);
9075 prod
= isl_basic_map_flatten(prod
);
9079 __isl_give isl_basic_set
*isl_basic_set_flat_product(
9080 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
9082 return isl_basic_map_flat_range_product(bset1
, bset2
);
9085 __isl_give isl_basic_map
*isl_basic_map_domain_product(
9086 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9088 isl_space
*space_result
= NULL
;
9089 isl_basic_map
*bmap
;
9090 unsigned in1
, in2
, out
, nparam
, total
, pos
;
9091 struct isl_dim_map
*dim_map1
, *dim_map2
;
9093 if (!bmap1
|| !bmap2
)
9096 space_result
= isl_space_domain_product(isl_space_copy(bmap1
->dim
),
9097 isl_space_copy(bmap2
->dim
));
9099 in1
= isl_basic_map_dim(bmap1
, isl_dim_in
);
9100 in2
= isl_basic_map_dim(bmap2
, isl_dim_in
);
9101 out
= isl_basic_map_dim(bmap1
, isl_dim_out
);
9102 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
9104 total
= nparam
+ in1
+ in2
+ out
+ bmap1
->n_div
+ bmap2
->n_div
;
9105 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9106 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9107 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9108 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9109 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9110 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
9111 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
9112 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
);
9113 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out
);
9114 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9116 bmap
= isl_basic_map_alloc_space(space_result
,
9117 bmap1
->n_div
+ bmap2
->n_div
,
9118 bmap1
->n_eq
+ bmap2
->n_eq
,
9119 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9120 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9121 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9122 bmap
= isl_basic_map_simplify(bmap
);
9123 return isl_basic_map_finalize(bmap
);
9125 isl_basic_map_free(bmap1
);
9126 isl_basic_map_free(bmap2
);
9130 __isl_give isl_basic_map
*isl_basic_map_range_product(
9131 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9133 isl_space
*dim_result
= NULL
;
9134 isl_basic_map
*bmap
;
9135 unsigned in
, out1
, out2
, nparam
, total
, pos
;
9136 struct isl_dim_map
*dim_map1
, *dim_map2
;
9138 if (!bmap1
|| !bmap2
)
9141 if (!isl_space_match(bmap1
->dim
, isl_dim_param
,
9142 bmap2
->dim
, isl_dim_param
))
9143 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
9144 "parameters don't match", goto error
);
9146 dim_result
= isl_space_range_product(isl_space_copy(bmap1
->dim
),
9147 isl_space_copy(bmap2
->dim
));
9149 in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
9150 out1
= isl_basic_map_n_out(bmap1
);
9151 out2
= isl_basic_map_n_out(bmap2
);
9152 nparam
= isl_basic_map_n_param(bmap1
);
9154 total
= nparam
+ in
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
9155 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9156 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9157 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9158 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9159 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9160 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
9161 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in
);
9162 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
9163 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
9164 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9166 bmap
= isl_basic_map_alloc_space(dim_result
,
9167 bmap1
->n_div
+ bmap2
->n_div
,
9168 bmap1
->n_eq
+ bmap2
->n_eq
,
9169 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9170 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9171 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9172 bmap
= isl_basic_map_simplify(bmap
);
9173 return isl_basic_map_finalize(bmap
);
9175 isl_basic_map_free(bmap1
);
9176 isl_basic_map_free(bmap2
);
9180 __isl_give isl_basic_map
*isl_basic_map_flat_range_product(
9181 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9183 isl_basic_map
*prod
;
9185 prod
= isl_basic_map_range_product(bmap1
, bmap2
);
9186 prod
= isl_basic_map_flatten_range(prod
);
9190 static __isl_give isl_map
*map_product(__isl_take isl_map
*map1
,
9191 __isl_take isl_map
*map2
,
9192 __isl_give isl_space
*(*dim_product
)(__isl_take isl_space
*left
,
9193 __isl_take isl_space
*right
),
9194 __isl_give isl_basic_map
*(*basic_map_product
)(
9195 __isl_take isl_basic_map
*left
, __isl_take isl_basic_map
*right
))
9198 struct isl_map
*result
;
9204 isl_assert(map1
->ctx
, isl_space_match(map1
->dim
, isl_dim_param
,
9205 map2
->dim
, isl_dim_param
), goto error
);
9207 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
9208 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
9209 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
9211 result
= isl_map_alloc_space(dim_product(isl_space_copy(map1
->dim
),
9212 isl_space_copy(map2
->dim
)),
9213 map1
->n
* map2
->n
, flags
);
9216 for (i
= 0; i
< map1
->n
; ++i
)
9217 for (j
= 0; j
< map2
->n
; ++j
) {
9218 struct isl_basic_map
*part
;
9219 part
= basic_map_product(isl_basic_map_copy(map1
->p
[i
]),
9220 isl_basic_map_copy(map2
->p
[j
]));
9221 if (isl_basic_map_is_empty(part
))
9222 isl_basic_map_free(part
);
9224 result
= isl_map_add_basic_map(result
, part
);
9237 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
9239 static __isl_give isl_map
*map_product_aligned(__isl_take isl_map
*map1
,
9240 __isl_take isl_map
*map2
)
9242 return map_product(map1
, map2
, &isl_space_product
, &isl_basic_map_product
);
9245 __isl_give isl_map
*isl_map_product(__isl_take isl_map
*map1
,
9246 __isl_take isl_map
*map2
)
9248 return isl_map_align_params_map_map_and(map1
, map2
, &map_product_aligned
);
9251 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
9253 __isl_give isl_map
*isl_map_flat_product(__isl_take isl_map
*map1
,
9254 __isl_take isl_map
*map2
)
9258 prod
= isl_map_product(map1
, map2
);
9259 prod
= isl_map_flatten(prod
);
9263 /* Given two set A and B, construct its Cartesian product A x B.
9265 struct isl_set
*isl_set_product(struct isl_set
*set1
, struct isl_set
*set2
)
9267 return isl_map_range_product(set1
, set2
);
9270 __isl_give isl_set
*isl_set_flat_product(__isl_take isl_set
*set1
,
9271 __isl_take isl_set
*set2
)
9273 return isl_map_flat_range_product(set1
, set2
);
9276 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
9278 static __isl_give isl_map
*map_domain_product_aligned(__isl_take isl_map
*map1
,
9279 __isl_take isl_map
*map2
)
9281 return map_product(map1
, map2
, &isl_space_domain_product
,
9282 &isl_basic_map_domain_product
);
9285 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
9287 static __isl_give isl_map
*map_range_product_aligned(__isl_take isl_map
*map1
,
9288 __isl_take isl_map
*map2
)
9290 return map_product(map1
, map2
, &isl_space_range_product
,
9291 &isl_basic_map_range_product
);
9294 __isl_give isl_map
*isl_map_domain_product(__isl_take isl_map
*map1
,
9295 __isl_take isl_map
*map2
)
9297 return isl_map_align_params_map_map_and(map1
, map2
,
9298 &map_domain_product_aligned
);
9301 __isl_give isl_map
*isl_map_range_product(__isl_take isl_map
*map1
,
9302 __isl_take isl_map
*map2
)
9304 return isl_map_align_params_map_map_and(map1
, map2
,
9305 &map_range_product_aligned
);
9308 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
9310 __isl_give isl_map
*isl_map_flat_domain_product(__isl_take isl_map
*map1
,
9311 __isl_take isl_map
*map2
)
9315 prod
= isl_map_domain_product(map1
, map2
);
9316 prod
= isl_map_flatten_domain(prod
);
9320 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
9322 __isl_give isl_map
*isl_map_flat_range_product(__isl_take isl_map
*map1
,
9323 __isl_take isl_map
*map2
)
9327 prod
= isl_map_range_product(map1
, map2
);
9328 prod
= isl_map_flatten_range(prod
);
9332 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map
*bmap
)
9335 uint32_t hash
= isl_hash_init();
9340 bmap
= isl_basic_map_copy(bmap
);
9341 bmap
= isl_basic_map_normalize(bmap
);
9344 total
= isl_basic_map_total_dim(bmap
);
9345 isl_hash_byte(hash
, bmap
->n_eq
& 0xFF);
9346 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
9348 c_hash
= isl_seq_get_hash(bmap
->eq
[i
], 1 + total
);
9349 isl_hash_hash(hash
, c_hash
);
9351 isl_hash_byte(hash
, bmap
->n_ineq
& 0xFF);
9352 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
9354 c_hash
= isl_seq_get_hash(bmap
->ineq
[i
], 1 + total
);
9355 isl_hash_hash(hash
, c_hash
);
9357 isl_hash_byte(hash
, bmap
->n_div
& 0xFF);
9358 for (i
= 0; i
< bmap
->n_div
; ++i
) {
9360 if (isl_int_is_zero(bmap
->div
[i
][0]))
9362 isl_hash_byte(hash
, i
& 0xFF);
9363 c_hash
= isl_seq_get_hash(bmap
->div
[i
], 1 + 1 + total
);
9364 isl_hash_hash(hash
, c_hash
);
9366 isl_basic_map_free(bmap
);
9370 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set
*bset
)
9372 return isl_basic_map_get_hash((isl_basic_map
*)bset
);
9375 uint32_t isl_map_get_hash(__isl_keep isl_map
*map
)
9382 map
= isl_map_copy(map
);
9383 map
= isl_map_normalize(map
);
9387 hash
= isl_hash_init();
9388 for (i
= 0; i
< map
->n
; ++i
) {
9390 bmap_hash
= isl_basic_map_get_hash(map
->p
[i
]);
9391 isl_hash_hash(hash
, bmap_hash
);
9399 uint32_t isl_set_get_hash(__isl_keep isl_set
*set
)
9401 return isl_map_get_hash((isl_map
*)set
);
9404 /* Check if the value for dimension dim is completely determined
9405 * by the values of the other parameters and variables.
9406 * That is, check if dimension dim is involved in an equality.
9408 int isl_basic_set_dim_is_unique(struct isl_basic_set
*bset
, unsigned dim
)
9415 nparam
= isl_basic_set_n_param(bset
);
9416 for (i
= 0; i
< bset
->n_eq
; ++i
)
9417 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ dim
]))
9422 /* Check if the value for dimension dim is completely determined
9423 * by the values of the other parameters and variables.
9424 * That is, check if dimension dim is involved in an equality
9425 * for each of the subsets.
9427 int isl_set_dim_is_unique(struct isl_set
*set
, unsigned dim
)
9433 for (i
= 0; i
< set
->n
; ++i
) {
9435 unique
= isl_basic_set_dim_is_unique(set
->p
[i
], dim
);
9442 int isl_set_n_basic_set(__isl_keep isl_set
*set
)
9444 return set
? set
->n
: 0;
9447 int isl_map_foreach_basic_map(__isl_keep isl_map
*map
,
9448 int (*fn
)(__isl_take isl_basic_map
*bmap
, void *user
), void *user
)
9455 for (i
= 0; i
< map
->n
; ++i
)
9456 if (fn(isl_basic_map_copy(map
->p
[i
]), user
) < 0)
9462 int isl_set_foreach_basic_set(__isl_keep isl_set
*set
,
9463 int (*fn
)(__isl_take isl_basic_set
*bset
, void *user
), void *user
)
9470 for (i
= 0; i
< set
->n
; ++i
)
9471 if (fn(isl_basic_set_copy(set
->p
[i
]), user
) < 0)
9477 __isl_give isl_basic_set
*isl_basic_set_lift(__isl_take isl_basic_set
*bset
)
9484 bset
= isl_basic_set_cow(bset
);
9488 dim
= isl_basic_set_get_space(bset
);
9489 dim
= isl_space_lift(dim
, bset
->n_div
);
9492 isl_space_free(bset
->dim
);
9494 bset
->extra
-= bset
->n_div
;
9497 bset
= isl_basic_set_finalize(bset
);
9501 isl_basic_set_free(bset
);
9505 __isl_give isl_set
*isl_set_lift(__isl_take isl_set
*set
)
9511 set
= isl_set_align_divs(set
);
9516 set
= isl_set_cow(set
);
9520 n_div
= set
->p
[0]->n_div
;
9521 dim
= isl_set_get_space(set
);
9522 dim
= isl_space_lift(dim
, n_div
);
9525 isl_space_free(set
->dim
);
9528 for (i
= 0; i
< set
->n
; ++i
) {
9529 set
->p
[i
] = isl_basic_set_lift(set
->p
[i
]);
9540 __isl_give isl_map
*isl_set_lifting(__isl_take isl_set
*set
)
9543 struct isl_basic_map
*bmap
;
9550 set
= isl_set_align_divs(set
);
9555 dim
= isl_set_get_space(set
);
9556 if (set
->n
== 0 || set
->p
[0]->n_div
== 0) {
9558 return isl_map_identity(isl_space_map_from_set(dim
));
9561 n_div
= set
->p
[0]->n_div
;
9562 dim
= isl_space_map_from_set(dim
);
9563 n_param
= isl_space_dim(dim
, isl_dim_param
);
9564 n_set
= isl_space_dim(dim
, isl_dim_in
);
9565 dim
= isl_space_extend(dim
, n_param
, n_set
, n_set
+ n_div
);
9566 bmap
= isl_basic_map_alloc_space(dim
, 0, n_set
, 2 * n_div
);
9567 for (i
= 0; i
< n_set
; ++i
)
9568 bmap
= var_equal(bmap
, i
);
9570 total
= n_param
+ n_set
+ n_set
+ n_div
;
9571 for (i
= 0; i
< n_div
; ++i
) {
9572 k
= isl_basic_map_alloc_inequality(bmap
);
9575 isl_seq_cpy(bmap
->ineq
[k
], set
->p
[0]->div
[i
]+1, 1+n_param
);
9576 isl_seq_clr(bmap
->ineq
[k
]+1+n_param
, n_set
);
9577 isl_seq_cpy(bmap
->ineq
[k
]+1+n_param
+n_set
,
9578 set
->p
[0]->div
[i
]+1+1+n_param
, n_set
+ n_div
);
9579 isl_int_neg(bmap
->ineq
[k
][1+n_param
+n_set
+n_set
+i
],
9580 set
->p
[0]->div
[i
][0]);
9582 l
= isl_basic_map_alloc_inequality(bmap
);
9585 isl_seq_neg(bmap
->ineq
[l
], bmap
->ineq
[k
], 1 + total
);
9586 isl_int_add(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0],
9587 set
->p
[0]->div
[i
][0]);
9588 isl_int_sub_ui(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], 1);
9592 bmap
= isl_basic_map_simplify(bmap
);
9593 bmap
= isl_basic_map_finalize(bmap
);
9594 return isl_map_from_basic_map(bmap
);
9597 isl_basic_map_free(bmap
);
9601 int isl_basic_set_size(__isl_keep isl_basic_set
*bset
)
9609 dim
= isl_basic_set_total_dim(bset
);
9610 size
+= bset
->n_eq
* (1 + dim
);
9611 size
+= bset
->n_ineq
* (1 + dim
);
9612 size
+= bset
->n_div
* (2 + dim
);
9617 int isl_set_size(__isl_keep isl_set
*set
)
9625 for (i
= 0; i
< set
->n
; ++i
)
9626 size
+= isl_basic_set_size(set
->p
[i
]);
9631 /* Check if there is any lower bound (if lower == 0) and/or upper
9632 * bound (if upper == 0) on the specified dim.
9634 static int basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
9635 enum isl_dim_type type
, unsigned pos
, int lower
, int upper
)
9642 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), return -1);
9644 pos
+= isl_basic_map_offset(bmap
, type
);
9646 for (i
= 0; i
< bmap
->n_div
; ++i
) {
9647 if (isl_int_is_zero(bmap
->div
[i
][0]))
9649 if (!isl_int_is_zero(bmap
->div
[i
][1 + pos
]))
9653 for (i
= 0; i
< bmap
->n_eq
; ++i
)
9654 if (!isl_int_is_zero(bmap
->eq
[i
][pos
]))
9657 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
9658 int sgn
= isl_int_sgn(bmap
->ineq
[i
][pos
]);
9665 return lower
&& upper
;
9668 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
9669 enum isl_dim_type type
, unsigned pos
)
9671 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 0);
9674 int isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map
*bmap
,
9675 enum isl_dim_type type
, unsigned pos
)
9677 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 1);
9680 int isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map
*bmap
,
9681 enum isl_dim_type type
, unsigned pos
)
9683 return basic_map_dim_is_bounded(bmap
, type
, pos
, 1, 0);
9686 int isl_map_dim_is_bounded(__isl_keep isl_map
*map
,
9687 enum isl_dim_type type
, unsigned pos
)
9694 for (i
= 0; i
< map
->n
; ++i
) {
9696 bounded
= isl_basic_map_dim_is_bounded(map
->p
[i
], type
, pos
);
9697 if (bounded
< 0 || !bounded
)
9704 /* Return 1 if the specified dim is involved in both an upper bound
9705 * and a lower bound.
9707 int isl_set_dim_is_bounded(__isl_keep isl_set
*set
,
9708 enum isl_dim_type type
, unsigned pos
)
9710 return isl_map_dim_is_bounded((isl_map
*)set
, type
, pos
);
9713 /* Does "map" have a bound (according to "fn") for any of its basic maps?
9715 static int has_any_bound(__isl_keep isl_map
*map
,
9716 enum isl_dim_type type
, unsigned pos
,
9717 int (*fn
)(__isl_keep isl_basic_map
*bmap
,
9718 enum isl_dim_type type
, unsigned pos
))
9725 for (i
= 0; i
< map
->n
; ++i
) {
9727 bounded
= fn(map
->p
[i
], type
, pos
);
9728 if (bounded
< 0 || bounded
)
9735 /* Return 1 if the specified dim is involved in any lower bound.
9737 int isl_set_dim_has_any_lower_bound(__isl_keep isl_set
*set
,
9738 enum isl_dim_type type
, unsigned pos
)
9740 return has_any_bound(set
, type
, pos
,
9741 &isl_basic_map_dim_has_lower_bound
);
9744 /* Return 1 if the specified dim is involved in any upper bound.
9746 int isl_set_dim_has_any_upper_bound(__isl_keep isl_set
*set
,
9747 enum isl_dim_type type
, unsigned pos
)
9749 return has_any_bound(set
, type
, pos
,
9750 &isl_basic_map_dim_has_upper_bound
);
9753 /* Does "map" have a bound (according to "fn") for all of its basic maps?
9755 static int has_bound(__isl_keep isl_map
*map
,
9756 enum isl_dim_type type
, unsigned pos
,
9757 int (*fn
)(__isl_keep isl_basic_map
*bmap
,
9758 enum isl_dim_type type
, unsigned pos
))
9765 for (i
= 0; i
< map
->n
; ++i
) {
9767 bounded
= fn(map
->p
[i
], type
, pos
);
9768 if (bounded
< 0 || !bounded
)
9775 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
9777 int isl_set_dim_has_lower_bound(__isl_keep isl_set
*set
,
9778 enum isl_dim_type type
, unsigned pos
)
9780 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_lower_bound
);
9783 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
9785 int isl_set_dim_has_upper_bound(__isl_keep isl_set
*set
,
9786 enum isl_dim_type type
, unsigned pos
)
9788 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_upper_bound
);
9791 /* For each of the "n" variables starting at "first", determine
9792 * the sign of the variable and put the results in the first "n"
9793 * elements of the array "signs".
9795 * 1 means that the variable is non-negative
9796 * -1 means that the variable is non-positive
9797 * 0 means the variable attains both positive and negative values.
9799 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set
*bset
,
9800 unsigned first
, unsigned n
, int *signs
)
9802 isl_vec
*bound
= NULL
;
9803 struct isl_tab
*tab
= NULL
;
9804 struct isl_tab_undo
*snap
;
9807 if (!bset
|| !signs
)
9810 bound
= isl_vec_alloc(bset
->ctx
, 1 + isl_basic_set_total_dim(bset
));
9811 tab
= isl_tab_from_basic_set(bset
, 0);
9815 isl_seq_clr(bound
->el
, bound
->size
);
9816 isl_int_set_si(bound
->el
[0], -1);
9818 snap
= isl_tab_snap(tab
);
9819 for (i
= 0; i
< n
; ++i
) {
9822 isl_int_set_si(bound
->el
[1 + first
+ i
], -1);
9823 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
9826 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
9827 if (isl_tab_rollback(tab
, snap
) < 0)
9835 isl_int_set_si(bound
->el
[1 + first
+ i
], 1);
9836 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
9839 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
9840 if (isl_tab_rollback(tab
, snap
) < 0)
9843 signs
[i
] = empty
? -1 : 0;
9847 isl_vec_free(bound
);
9851 isl_vec_free(bound
);
9855 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set
*bset
,
9856 enum isl_dim_type type
, unsigned first
, unsigned n
, int *signs
)
9858 if (!bset
|| !signs
)
9860 isl_assert(bset
->ctx
, first
+ n
<= isl_basic_set_dim(bset
, type
),
9863 first
+= pos(bset
->dim
, type
) - 1;
9864 return isl_basic_set_vars_get_sign(bset
, first
, n
, signs
);
9867 /* Check if the given basic map is obviously single-valued.
9868 * In particular, for each output dimension, check that there is
9869 * an equality that defines the output dimension in terms of
9870 * earlier dimensions.
9872 int isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map
*bmap
)
9882 total
= 1 + isl_basic_map_total_dim(bmap
);
9883 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
9884 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
9886 for (i
= 0; i
< n_out
; ++i
) {
9887 for (j
= 0; j
< bmap
->n_eq
; ++j
) {
9888 if (isl_int_is_zero(bmap
->eq
[j
][o_out
+ i
]))
9890 if (isl_seq_first_non_zero(bmap
->eq
[j
] + o_out
+ i
+ 1,
9891 total
- (o_out
+ i
+ 1)) == -1)
9894 if (j
>= bmap
->n_eq
)
9901 /* Check if the given basic map is single-valued.
9906 * and check if the result is a subset of the identity mapping.
9908 int isl_basic_map_is_single_valued(__isl_keep isl_basic_map
*bmap
)
9911 isl_basic_map
*test
;
9915 sv
= isl_basic_map_plain_is_single_valued(bmap
);
9919 test
= isl_basic_map_reverse(isl_basic_map_copy(bmap
));
9920 test
= isl_basic_map_apply_range(test
, isl_basic_map_copy(bmap
));
9922 space
= isl_basic_map_get_space(bmap
);
9923 space
= isl_space_map_from_set(isl_space_range(space
));
9924 id
= isl_basic_map_identity(space
);
9926 sv
= isl_basic_map_is_subset(test
, id
);
9928 isl_basic_map_free(test
);
9929 isl_basic_map_free(id
);
9934 /* Check if the given map is obviously single-valued.
9936 int isl_map_plain_is_single_valued(__isl_keep isl_map
*map
)
9945 return isl_basic_map_plain_is_single_valued(map
->p
[0]);
9948 /* Check if the given map is single-valued.
9953 * and check if the result is a subset of the identity mapping.
9955 int isl_map_is_single_valued(__isl_keep isl_map
*map
)
9962 sv
= isl_map_plain_is_single_valued(map
);
9966 test
= isl_map_reverse(isl_map_copy(map
));
9967 test
= isl_map_apply_range(test
, isl_map_copy(map
));
9969 dim
= isl_space_map_from_set(isl_space_range(isl_map_get_space(map
)));
9970 id
= isl_map_identity(dim
);
9972 sv
= isl_map_is_subset(test
, id
);
9980 int isl_map_is_injective(__isl_keep isl_map
*map
)
9984 map
= isl_map_copy(map
);
9985 map
= isl_map_reverse(map
);
9986 in
= isl_map_is_single_valued(map
);
9992 /* Check if the given map is obviously injective.
9994 int isl_map_plain_is_injective(__isl_keep isl_map
*map
)
9998 map
= isl_map_copy(map
);
9999 map
= isl_map_reverse(map
);
10000 in
= isl_map_plain_is_single_valued(map
);
10006 int isl_map_is_bijective(__isl_keep isl_map
*map
)
10010 sv
= isl_map_is_single_valued(map
);
10014 return isl_map_is_injective(map
);
10017 int isl_set_is_singleton(__isl_keep isl_set
*set
)
10019 return isl_map_is_single_valued((isl_map
*)set
);
10022 int isl_map_is_translation(__isl_keep isl_map
*map
)
10027 delta
= isl_map_deltas(isl_map_copy(map
));
10028 ok
= isl_set_is_singleton(delta
);
10029 isl_set_free(delta
);
10034 static int unique(isl_int
*p
, unsigned pos
, unsigned len
)
10036 if (isl_seq_first_non_zero(p
, pos
) != -1)
10038 if (isl_seq_first_non_zero(p
+ pos
+ 1, len
- pos
- 1) != -1)
10043 int isl_basic_set_is_box(__isl_keep isl_basic_set
*bset
)
10052 if (isl_basic_set_dim(bset
, isl_dim_div
) != 0)
10055 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
10056 ovar
= isl_space_offset(bset
->dim
, isl_dim_set
);
10057 for (j
= 0; j
< nvar
; ++j
) {
10058 int lower
= 0, upper
= 0;
10059 for (i
= 0; i
< bset
->n_eq
; ++i
) {
10060 if (isl_int_is_zero(bset
->eq
[i
][1 + ovar
+ j
]))
10062 if (!unique(bset
->eq
[i
] + 1 + ovar
, j
, nvar
))
10066 if (i
< bset
->n_eq
)
10068 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
10069 if (isl_int_is_zero(bset
->ineq
[i
][1 + ovar
+ j
]))
10071 if (!unique(bset
->ineq
[i
] + 1 + ovar
, j
, nvar
))
10073 if (isl_int_is_pos(bset
->ineq
[i
][1 + ovar
+ j
]))
10078 if (!lower
|| !upper
)
10085 int isl_set_is_box(__isl_keep isl_set
*set
)
10092 return isl_basic_set_is_box(set
->p
[0]);
10095 int isl_basic_set_is_wrapping(__isl_keep isl_basic_set
*bset
)
10100 return isl_space_is_wrapping(bset
->dim
);
10103 int isl_set_is_wrapping(__isl_keep isl_set
*set
)
10108 return isl_space_is_wrapping(set
->dim
);
10111 __isl_give isl_basic_set
*isl_basic_map_wrap(__isl_take isl_basic_map
*bmap
)
10113 bmap
= isl_basic_map_cow(bmap
);
10117 bmap
->dim
= isl_space_wrap(bmap
->dim
);
10121 bmap
= isl_basic_map_finalize(bmap
);
10123 return (isl_basic_set
*)bmap
;
10125 isl_basic_map_free(bmap
);
10129 __isl_give isl_set
*isl_map_wrap(__isl_take isl_map
*map
)
10133 map
= isl_map_cow(map
);
10137 for (i
= 0; i
< map
->n
; ++i
) {
10138 map
->p
[i
] = (isl_basic_map
*)isl_basic_map_wrap(map
->p
[i
]);
10142 map
->dim
= isl_space_wrap(map
->dim
);
10146 return (isl_set
*)map
;
10152 __isl_give isl_basic_map
*isl_basic_set_unwrap(__isl_take isl_basic_set
*bset
)
10154 bset
= isl_basic_set_cow(bset
);
10158 bset
->dim
= isl_space_unwrap(bset
->dim
);
10162 bset
= isl_basic_set_finalize(bset
);
10164 return (isl_basic_map
*)bset
;
10166 isl_basic_set_free(bset
);
10170 __isl_give isl_map
*isl_set_unwrap(__isl_take isl_set
*set
)
10177 if (!isl_set_is_wrapping(set
))
10178 isl_die(set
->ctx
, isl_error_invalid
, "not a wrapping set",
10181 set
= isl_set_cow(set
);
10185 for (i
= 0; i
< set
->n
; ++i
) {
10186 set
->p
[i
] = (isl_basic_set
*)isl_basic_set_unwrap(set
->p
[i
]);
10191 set
->dim
= isl_space_unwrap(set
->dim
);
10195 return (isl_map
*)set
;
10201 __isl_give isl_basic_map
*isl_basic_map_reset(__isl_take isl_basic_map
*bmap
,
10202 enum isl_dim_type type
)
10207 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
10210 bmap
= isl_basic_map_cow(bmap
);
10214 bmap
->dim
= isl_space_reset(bmap
->dim
, type
);
10218 bmap
= isl_basic_map_finalize(bmap
);
10222 isl_basic_map_free(bmap
);
10226 __isl_give isl_map
*isl_map_reset(__isl_take isl_map
*map
,
10227 enum isl_dim_type type
)
10234 if (!isl_space_is_named_or_nested(map
->dim
, type
))
10237 map
= isl_map_cow(map
);
10241 for (i
= 0; i
< map
->n
; ++i
) {
10242 map
->p
[i
] = isl_basic_map_reset(map
->p
[i
], type
);
10246 map
->dim
= isl_space_reset(map
->dim
, type
);
10256 __isl_give isl_basic_map
*isl_basic_map_flatten(__isl_take isl_basic_map
*bmap
)
10261 if (!bmap
->dim
->nested
[0] && !bmap
->dim
->nested
[1])
10264 bmap
= isl_basic_map_cow(bmap
);
10268 bmap
->dim
= isl_space_flatten(bmap
->dim
);
10272 bmap
= isl_basic_map_finalize(bmap
);
10276 isl_basic_map_free(bmap
);
10280 __isl_give isl_basic_set
*isl_basic_set_flatten(__isl_take isl_basic_set
*bset
)
10282 return (isl_basic_set
*)isl_basic_map_flatten((isl_basic_map
*)bset
);
10285 __isl_give isl_basic_map
*isl_basic_map_flatten_domain(
10286 __isl_take isl_basic_map
*bmap
)
10291 if (!bmap
->dim
->nested
[0])
10294 bmap
= isl_basic_map_cow(bmap
);
10298 bmap
->dim
= isl_space_flatten_domain(bmap
->dim
);
10302 bmap
= isl_basic_map_finalize(bmap
);
10306 isl_basic_map_free(bmap
);
10310 __isl_give isl_basic_map
*isl_basic_map_flatten_range(
10311 __isl_take isl_basic_map
*bmap
)
10316 if (!bmap
->dim
->nested
[1])
10319 bmap
= isl_basic_map_cow(bmap
);
10323 bmap
->dim
= isl_space_flatten_range(bmap
->dim
);
10327 bmap
= isl_basic_map_finalize(bmap
);
10331 isl_basic_map_free(bmap
);
10335 __isl_give isl_map
*isl_map_flatten(__isl_take isl_map
*map
)
10342 if (!map
->dim
->nested
[0] && !map
->dim
->nested
[1])
10345 map
= isl_map_cow(map
);
10349 for (i
= 0; i
< map
->n
; ++i
) {
10350 map
->p
[i
] = isl_basic_map_flatten(map
->p
[i
]);
10354 map
->dim
= isl_space_flatten(map
->dim
);
10364 __isl_give isl_set
*isl_set_flatten(__isl_take isl_set
*set
)
10366 return (isl_set
*)isl_map_flatten((isl_map
*)set
);
10369 __isl_give isl_map
*isl_set_flatten_map(__isl_take isl_set
*set
)
10371 isl_space
*dim
, *flat_dim
;
10374 dim
= isl_set_get_space(set
);
10375 flat_dim
= isl_space_flatten(isl_space_copy(dim
));
10376 map
= isl_map_identity(isl_space_join(isl_space_reverse(dim
), flat_dim
));
10377 map
= isl_map_intersect_domain(map
, set
);
10382 __isl_give isl_map
*isl_map_flatten_domain(__isl_take isl_map
*map
)
10389 if (!map
->dim
->nested
[0])
10392 map
= isl_map_cow(map
);
10396 for (i
= 0; i
< map
->n
; ++i
) {
10397 map
->p
[i
] = isl_basic_map_flatten_domain(map
->p
[i
]);
10401 map
->dim
= isl_space_flatten_domain(map
->dim
);
10411 __isl_give isl_map
*isl_map_flatten_range(__isl_take isl_map
*map
)
10418 if (!map
->dim
->nested
[1])
10421 map
= isl_map_cow(map
);
10425 for (i
= 0; i
< map
->n
; ++i
) {
10426 map
->p
[i
] = isl_basic_map_flatten_range(map
->p
[i
]);
10430 map
->dim
= isl_space_flatten_range(map
->dim
);
10440 /* Reorder the dimensions of "bmap" according to the given dim_map
10441 * and set the dimension specification to "dim".
10443 __isl_give isl_basic_map
*isl_basic_map_realign(__isl_take isl_basic_map
*bmap
,
10444 __isl_take isl_space
*dim
, __isl_take
struct isl_dim_map
*dim_map
)
10446 isl_basic_map
*res
;
10449 bmap
= isl_basic_map_cow(bmap
);
10450 if (!bmap
|| !dim
|| !dim_map
)
10453 flags
= bmap
->flags
;
10454 ISL_FL_CLR(flags
, ISL_BASIC_MAP_FINAL
);
10455 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED
);
10456 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
10457 res
= isl_basic_map_alloc_space(dim
,
10458 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
10459 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
10461 res
->flags
= flags
;
10462 res
= isl_basic_map_finalize(res
);
10466 isl_basic_map_free(bmap
);
10467 isl_space_free(dim
);
10471 /* Reorder the dimensions of "map" according to given reordering.
10473 __isl_give isl_map
*isl_map_realign(__isl_take isl_map
*map
,
10474 __isl_take isl_reordering
*r
)
10477 struct isl_dim_map
*dim_map
;
10479 map
= isl_map_cow(map
);
10480 dim_map
= isl_dim_map_from_reordering(r
);
10481 if (!map
|| !r
|| !dim_map
)
10484 for (i
= 0; i
< map
->n
; ++i
) {
10485 struct isl_dim_map
*dim_map_i
;
10487 dim_map_i
= isl_dim_map_extend(dim_map
, map
->p
[i
]);
10489 map
->p
[i
] = isl_basic_map_realign(map
->p
[i
],
10490 isl_space_copy(r
->dim
), dim_map_i
);
10496 map
= isl_map_reset_space(map
, isl_space_copy(r
->dim
));
10498 isl_reordering_free(r
);
10504 isl_reordering_free(r
);
10508 __isl_give isl_set
*isl_set_realign(__isl_take isl_set
*set
,
10509 __isl_take isl_reordering
*r
)
10511 return (isl_set
*)isl_map_realign((isl_map
*)set
, r
);
10514 __isl_give isl_map
*isl_map_align_params(__isl_take isl_map
*map
,
10515 __isl_take isl_space
*model
)
10519 if (!map
|| !model
)
10522 ctx
= isl_space_get_ctx(model
);
10523 if (!isl_space_has_named_params(model
))
10524 isl_die(ctx
, isl_error_invalid
,
10525 "model has unnamed parameters", goto error
);
10526 if (!isl_space_has_named_params(map
->dim
))
10527 isl_die(ctx
, isl_error_invalid
,
10528 "relation has unnamed parameters", goto error
);
10529 if (!isl_space_match(map
->dim
, isl_dim_param
, model
, isl_dim_param
)) {
10530 isl_reordering
*exp
;
10532 model
= isl_space_drop_dims(model
, isl_dim_in
,
10533 0, isl_space_dim(model
, isl_dim_in
));
10534 model
= isl_space_drop_dims(model
, isl_dim_out
,
10535 0, isl_space_dim(model
, isl_dim_out
));
10536 exp
= isl_parameter_alignment_reordering(map
->dim
, model
);
10537 exp
= isl_reordering_extend_space(exp
, isl_map_get_space(map
));
10538 map
= isl_map_realign(map
, exp
);
10541 isl_space_free(model
);
10544 isl_space_free(model
);
10549 __isl_give isl_set
*isl_set_align_params(__isl_take isl_set
*set
,
10550 __isl_take isl_space
*model
)
10552 return isl_map_align_params(set
, model
);
10555 /* Align the parameters of "bmap" to those of "model", introducing
10556 * additional parameters if needed.
10558 __isl_give isl_basic_map
*isl_basic_map_align_params(
10559 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*model
)
10563 if (!bmap
|| !model
)
10566 ctx
= isl_space_get_ctx(model
);
10567 if (!isl_space_has_named_params(model
))
10568 isl_die(ctx
, isl_error_invalid
,
10569 "model has unnamed parameters", goto error
);
10570 if (!isl_space_has_named_params(bmap
->dim
))
10571 isl_die(ctx
, isl_error_invalid
,
10572 "relation has unnamed parameters", goto error
);
10573 if (!isl_space_match(bmap
->dim
, isl_dim_param
, model
, isl_dim_param
)) {
10574 isl_reordering
*exp
;
10575 struct isl_dim_map
*dim_map
;
10577 model
= isl_space_drop_dims(model
, isl_dim_in
,
10578 0, isl_space_dim(model
, isl_dim_in
));
10579 model
= isl_space_drop_dims(model
, isl_dim_out
,
10580 0, isl_space_dim(model
, isl_dim_out
));
10581 exp
= isl_parameter_alignment_reordering(bmap
->dim
, model
);
10582 exp
= isl_reordering_extend_space(exp
,
10583 isl_basic_map_get_space(bmap
));
10584 dim_map
= isl_dim_map_from_reordering(exp
);
10585 bmap
= isl_basic_map_realign(bmap
,
10586 exp
? isl_space_copy(exp
->dim
) : NULL
,
10587 isl_dim_map_extend(dim_map
, bmap
));
10588 isl_reordering_free(exp
);
10592 isl_space_free(model
);
10595 isl_space_free(model
);
10596 isl_basic_map_free(bmap
);
10600 /* Align the parameters of "bset" to those of "model", introducing
10601 * additional parameters if needed.
10603 __isl_give isl_basic_set
*isl_basic_set_align_params(
10604 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*model
)
10606 return isl_basic_map_align_params(bset
, model
);
10609 __isl_give isl_mat
*isl_basic_map_equalities_matrix(
10610 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
10611 enum isl_dim_type c2
, enum isl_dim_type c3
,
10612 enum isl_dim_type c4
, enum isl_dim_type c5
)
10614 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
10615 struct isl_mat
*mat
;
10621 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_eq
,
10622 isl_basic_map_total_dim(bmap
) + 1);
10625 for (i
= 0; i
< bmap
->n_eq
; ++i
)
10626 for (j
= 0, pos
= 0; j
< 5; ++j
) {
10627 int off
= isl_basic_map_offset(bmap
, c
[j
]);
10628 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
10629 isl_int_set(mat
->row
[i
][pos
],
10630 bmap
->eq
[i
][off
+ k
]);
10638 __isl_give isl_mat
*isl_basic_map_inequalities_matrix(
10639 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
10640 enum isl_dim_type c2
, enum isl_dim_type c3
,
10641 enum isl_dim_type c4
, enum isl_dim_type c5
)
10643 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
10644 struct isl_mat
*mat
;
10650 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_ineq
,
10651 isl_basic_map_total_dim(bmap
) + 1);
10654 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
10655 for (j
= 0, pos
= 0; j
< 5; ++j
) {
10656 int off
= isl_basic_map_offset(bmap
, c
[j
]);
10657 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
10658 isl_int_set(mat
->row
[i
][pos
],
10659 bmap
->ineq
[i
][off
+ k
]);
10667 __isl_give isl_basic_map
*isl_basic_map_from_constraint_matrices(
10668 __isl_take isl_space
*dim
,
10669 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
10670 enum isl_dim_type c2
, enum isl_dim_type c3
,
10671 enum isl_dim_type c4
, enum isl_dim_type c5
)
10673 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
10674 isl_basic_map
*bmap
;
10680 if (!dim
|| !eq
|| !ineq
)
10683 if (eq
->n_col
!= ineq
->n_col
)
10684 isl_die(dim
->ctx
, isl_error_invalid
,
10685 "equalities and inequalities matrices should have "
10686 "same number of columns", goto error
);
10688 total
= 1 + isl_space_dim(dim
, isl_dim_all
);
10690 if (eq
->n_col
< total
)
10691 isl_die(dim
->ctx
, isl_error_invalid
,
10692 "number of columns too small", goto error
);
10694 extra
= eq
->n_col
- total
;
10696 bmap
= isl_basic_map_alloc_space(isl_space_copy(dim
), extra
,
10697 eq
->n_row
, ineq
->n_row
);
10700 for (i
= 0; i
< extra
; ++i
) {
10701 k
= isl_basic_map_alloc_div(bmap
);
10704 isl_int_set_si(bmap
->div
[k
][0], 0);
10706 for (i
= 0; i
< eq
->n_row
; ++i
) {
10707 l
= isl_basic_map_alloc_equality(bmap
);
10710 for (j
= 0, pos
= 0; j
< 5; ++j
) {
10711 int off
= isl_basic_map_offset(bmap
, c
[j
]);
10712 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
10713 isl_int_set(bmap
->eq
[l
][off
+ k
],
10719 for (i
= 0; i
< ineq
->n_row
; ++i
) {
10720 l
= isl_basic_map_alloc_inequality(bmap
);
10723 for (j
= 0, pos
= 0; j
< 5; ++j
) {
10724 int off
= isl_basic_map_offset(bmap
, c
[j
]);
10725 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
10726 isl_int_set(bmap
->ineq
[l
][off
+ k
],
10727 ineq
->row
[i
][pos
]);
10733 isl_space_free(dim
);
10735 isl_mat_free(ineq
);
10737 bmap
= isl_basic_map_simplify(bmap
);
10738 return isl_basic_map_finalize(bmap
);
10740 isl_space_free(dim
);
10742 isl_mat_free(ineq
);
10746 __isl_give isl_mat
*isl_basic_set_equalities_matrix(
10747 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
10748 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
10750 return isl_basic_map_equalities_matrix((isl_basic_map
*)bset
,
10751 c1
, c2
, c3
, c4
, isl_dim_in
);
10754 __isl_give isl_mat
*isl_basic_set_inequalities_matrix(
10755 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
10756 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
10758 return isl_basic_map_inequalities_matrix((isl_basic_map
*)bset
,
10759 c1
, c2
, c3
, c4
, isl_dim_in
);
10762 __isl_give isl_basic_set
*isl_basic_set_from_constraint_matrices(
10763 __isl_take isl_space
*dim
,
10764 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
10765 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
10767 return (isl_basic_set
*)
10768 isl_basic_map_from_constraint_matrices(dim
, eq
, ineq
,
10769 c1
, c2
, c3
, c4
, isl_dim_in
);
10772 int isl_basic_map_can_zip(__isl_keep isl_basic_map
*bmap
)
10777 return isl_space_can_zip(bmap
->dim
);
10780 int isl_map_can_zip(__isl_keep isl_map
*map
)
10785 return isl_space_can_zip(map
->dim
);
10788 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
10789 * (A -> C) -> (B -> D).
10791 __isl_give isl_basic_map
*isl_basic_map_zip(__isl_take isl_basic_map
*bmap
)
10800 if (!isl_basic_map_can_zip(bmap
))
10801 isl_die(bmap
->ctx
, isl_error_invalid
,
10802 "basic map cannot be zipped", goto error
);
10803 pos
= isl_basic_map_offset(bmap
, isl_dim_in
) +
10804 isl_space_dim(bmap
->dim
->nested
[0], isl_dim_in
);
10805 n1
= isl_space_dim(bmap
->dim
->nested
[0], isl_dim_out
);
10806 n2
= isl_space_dim(bmap
->dim
->nested
[1], isl_dim_in
);
10807 bmap
= isl_basic_map_cow(bmap
);
10808 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
10811 bmap
->dim
= isl_space_zip(bmap
->dim
);
10816 isl_basic_map_free(bmap
);
10820 /* Given a map (A -> B) -> (C -> D), return the corresponding map
10821 * (A -> C) -> (B -> D).
10823 __isl_give isl_map
*isl_map_zip(__isl_take isl_map
*map
)
10830 if (!isl_map_can_zip(map
))
10831 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be zipped",
10834 map
= isl_map_cow(map
);
10838 for (i
= 0; i
< map
->n
; ++i
) {
10839 map
->p
[i
] = isl_basic_map_zip(map
->p
[i
]);
10844 map
->dim
= isl_space_zip(map
->dim
);
10854 /* Can we apply isl_basic_map_curry to "bmap"?
10855 * That is, does it have a nested relation in its domain?
10857 int isl_basic_map_can_curry(__isl_keep isl_basic_map
*bmap
)
10862 return isl_space_can_curry(bmap
->dim
);
10865 /* Can we apply isl_map_curry to "map"?
10866 * That is, does it have a nested relation in its domain?
10868 int isl_map_can_curry(__isl_keep isl_map
*map
)
10873 return isl_space_can_curry(map
->dim
);
10876 /* Given a basic map (A -> B) -> C, return the corresponding basic map
10879 __isl_give isl_basic_map
*isl_basic_map_curry(__isl_take isl_basic_map
*bmap
)
10885 if (!isl_basic_map_can_curry(bmap
))
10886 isl_die(bmap
->ctx
, isl_error_invalid
,
10887 "basic map cannot be curried", goto error
);
10888 bmap
= isl_basic_map_cow(bmap
);
10891 bmap
->dim
= isl_space_curry(bmap
->dim
);
10896 isl_basic_map_free(bmap
);
10900 /* Given a map (A -> B) -> C, return the corresponding map
10903 __isl_give isl_map
*isl_map_curry(__isl_take isl_map
*map
)
10910 if (!isl_map_can_curry(map
))
10911 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be curried",
10914 map
= isl_map_cow(map
);
10918 for (i
= 0; i
< map
->n
; ++i
) {
10919 map
->p
[i
] = isl_basic_map_curry(map
->p
[i
]);
10924 map
->dim
= isl_space_curry(map
->dim
);
10934 /* Can we apply isl_basic_map_uncurry to "bmap"?
10935 * That is, does it have a nested relation in its domain?
10937 int isl_basic_map_can_uncurry(__isl_keep isl_basic_map
*bmap
)
10942 return isl_space_can_uncurry(bmap
->dim
);
10945 /* Can we apply isl_map_uncurry to "map"?
10946 * That is, does it have a nested relation in its domain?
10948 int isl_map_can_uncurry(__isl_keep isl_map
*map
)
10953 return isl_space_can_uncurry(map
->dim
);
10956 /* Given a basic map A -> (B -> C), return the corresponding basic map
10959 __isl_give isl_basic_map
*isl_basic_map_uncurry(__isl_take isl_basic_map
*bmap
)
10965 if (!isl_basic_map_can_uncurry(bmap
))
10966 isl_die(bmap
->ctx
, isl_error_invalid
,
10967 "basic map cannot be uncurried",
10968 return isl_basic_map_free(bmap
));
10969 bmap
= isl_basic_map_cow(bmap
);
10972 bmap
->dim
= isl_space_uncurry(bmap
->dim
);
10974 return isl_basic_map_free(bmap
);
10978 /* Given a map A -> (B -> C), return the corresponding map
10981 __isl_give isl_map
*isl_map_uncurry(__isl_take isl_map
*map
)
10988 if (!isl_map_can_uncurry(map
))
10989 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be uncurried",
10990 return isl_map_free(map
));
10992 map
= isl_map_cow(map
);
10996 for (i
= 0; i
< map
->n
; ++i
) {
10997 map
->p
[i
] = isl_basic_map_uncurry(map
->p
[i
]);
10999 return isl_map_free(map
);
11002 map
->dim
= isl_space_uncurry(map
->dim
);
11004 return isl_map_free(map
);
11009 /* Construct a basic map mapping the domain of the affine expression
11010 * to a one-dimensional range prescribed by the affine expression.
11012 __isl_give isl_basic_map
*isl_basic_map_from_aff(__isl_take isl_aff
*aff
)
11016 isl_local_space
*ls
;
11017 isl_basic_map
*bmap
;
11022 ls
= isl_aff_get_local_space(aff
);
11023 bmap
= isl_basic_map_from_local_space(ls
);
11024 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
11025 k
= isl_basic_map_alloc_equality(bmap
);
11029 pos
= isl_basic_map_offset(bmap
, isl_dim_out
);
11030 isl_seq_cpy(bmap
->eq
[k
], aff
->v
->el
+ 1, pos
);
11031 isl_int_neg(bmap
->eq
[k
][pos
], aff
->v
->el
[0]);
11032 isl_seq_cpy(bmap
->eq
[k
] + pos
+ 1, aff
->v
->el
+ 1 + pos
,
11033 aff
->v
->size
- (pos
+ 1));
11036 bmap
= isl_basic_map_finalize(bmap
);
11040 isl_basic_map_free(bmap
);
11044 /* Construct a map mapping the domain of the affine expression
11045 * to a one-dimensional range prescribed by the affine expression.
11047 __isl_give isl_map
*isl_map_from_aff(__isl_take isl_aff
*aff
)
11049 isl_basic_map
*bmap
;
11051 bmap
= isl_basic_map_from_aff(aff
);
11052 return isl_map_from_basic_map(bmap
);
11055 /* Construct a basic map mapping the domain the multi-affine expression
11056 * to its range, with each dimension in the range equated to the
11057 * corresponding affine expression.
11059 __isl_give isl_basic_map
*isl_basic_map_from_multi_aff(
11060 __isl_take isl_multi_aff
*maff
)
11064 isl_basic_map
*bmap
;
11069 if (isl_space_dim(maff
->space
, isl_dim_out
) != maff
->n
)
11070 isl_die(isl_multi_aff_get_ctx(maff
), isl_error_internal
,
11071 "invalid space", return isl_multi_aff_free(maff
));
11073 space
= isl_space_domain(isl_multi_aff_get_space(maff
));
11074 bmap
= isl_basic_map_universe(isl_space_from_domain(space
));
11076 for (i
= 0; i
< maff
->n
; ++i
) {
11078 isl_basic_map
*bmap_i
;
11080 aff
= isl_aff_copy(maff
->p
[i
]);
11081 bmap_i
= isl_basic_map_from_aff(aff
);
11083 bmap
= isl_basic_map_flat_range_product(bmap
, bmap_i
);
11086 bmap
= isl_basic_map_reset_space(bmap
, isl_multi_aff_get_space(maff
));
11088 isl_multi_aff_free(maff
);
11092 /* Construct a map mapping the domain the multi-affine expression
11093 * to its range, with each dimension in the range equated to the
11094 * corresponding affine expression.
11096 __isl_give isl_map
*isl_map_from_multi_aff(__isl_take isl_multi_aff
*maff
)
11098 isl_basic_map
*bmap
;
11100 bmap
= isl_basic_map_from_multi_aff(maff
);
11101 return isl_map_from_basic_map(bmap
);
11104 /* Construct a basic map mapping a domain in the given space to
11105 * to an n-dimensional range, with n the number of elements in the list,
11106 * where each coordinate in the range is prescribed by the
11107 * corresponding affine expression.
11108 * The domains of all affine expressions in the list are assumed to match
11111 __isl_give isl_basic_map
*isl_basic_map_from_aff_list(
11112 __isl_take isl_space
*domain_dim
, __isl_take isl_aff_list
*list
)
11116 isl_basic_map
*bmap
;
11121 dim
= isl_space_from_domain(domain_dim
);
11122 bmap
= isl_basic_map_universe(dim
);
11124 for (i
= 0; i
< list
->n
; ++i
) {
11126 isl_basic_map
*bmap_i
;
11128 aff
= isl_aff_copy(list
->p
[i
]);
11129 bmap_i
= isl_basic_map_from_aff(aff
);
11131 bmap
= isl_basic_map_flat_range_product(bmap
, bmap_i
);
11134 isl_aff_list_free(list
);
11138 __isl_give isl_set
*isl_set_equate(__isl_take isl_set
*set
,
11139 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11141 return isl_map_equate(set
, type1
, pos1
, type2
, pos2
);
11144 /* Construct a basic map where the given dimensions are equal to each other.
11146 static __isl_give isl_basic_map
*equator(__isl_take isl_space
*space
,
11147 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11149 isl_basic_map
*bmap
= NULL
;
11155 if (pos1
>= isl_space_dim(space
, type1
))
11156 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
11157 "index out of bounds", goto error
);
11158 if (pos2
>= isl_space_dim(space
, type2
))
11159 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
11160 "index out of bounds", goto error
);
11162 if (type1
== type2
&& pos1
== pos2
)
11163 return isl_basic_map_universe(space
);
11165 bmap
= isl_basic_map_alloc_space(isl_space_copy(space
), 0, 1, 0);
11166 i
= isl_basic_map_alloc_equality(bmap
);
11169 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
11170 pos1
+= isl_basic_map_offset(bmap
, type1
);
11171 pos2
+= isl_basic_map_offset(bmap
, type2
);
11172 isl_int_set_si(bmap
->eq
[i
][pos1
], -1);
11173 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
11174 bmap
= isl_basic_map_finalize(bmap
);
11175 isl_space_free(space
);
11178 isl_space_free(space
);
11179 isl_basic_map_free(bmap
);
11183 /* Add a constraint imposing that the given two dimensions are equal.
11185 __isl_give isl_basic_map
*isl_basic_map_equate(__isl_take isl_basic_map
*bmap
,
11186 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11190 eq
= equator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
11192 bmap
= isl_basic_map_intersect(bmap
, eq
);
11197 /* Add a constraint imposing that the given two dimensions are equal.
11199 __isl_give isl_map
*isl_map_equate(__isl_take isl_map
*map
,
11200 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11202 isl_basic_map
*bmap
;
11204 bmap
= equator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
11206 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
11211 /* Add a constraint imposing that the given two dimensions have opposite values.
11213 __isl_give isl_map
*isl_map_oppose(__isl_take isl_map
*map
,
11214 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11216 isl_basic_map
*bmap
= NULL
;
11222 if (pos1
>= isl_map_dim(map
, type1
))
11223 isl_die(map
->ctx
, isl_error_invalid
,
11224 "index out of bounds", goto error
);
11225 if (pos2
>= isl_map_dim(map
, type2
))
11226 isl_die(map
->ctx
, isl_error_invalid
,
11227 "index out of bounds", goto error
);
11229 bmap
= isl_basic_map_alloc_space(isl_map_get_space(map
), 0, 1, 0);
11230 i
= isl_basic_map_alloc_equality(bmap
);
11233 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
11234 pos1
+= isl_basic_map_offset(bmap
, type1
);
11235 pos2
+= isl_basic_map_offset(bmap
, type2
);
11236 isl_int_set_si(bmap
->eq
[i
][pos1
], 1);
11237 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
11238 bmap
= isl_basic_map_finalize(bmap
);
11240 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
11244 isl_basic_map_free(bmap
);
11249 /* Add a constraint imposing that the value of the first dimension is
11250 * greater than or equal to that of the second.
11252 __isl_give isl_basic_map
*isl_basic_map_order_ge(__isl_take isl_basic_map
*bmap
,
11253 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11256 isl_local_space
*ls
;
11261 if (pos1
>= isl_basic_map_dim(bmap
, type1
))
11262 isl_die(bmap
->ctx
, isl_error_invalid
,
11263 "index out of bounds", return isl_basic_map_free(bmap
));
11264 if (pos2
>= isl_basic_map_dim(bmap
, type2
))
11265 isl_die(bmap
->ctx
, isl_error_invalid
,
11266 "index out of bounds", return isl_basic_map_free(bmap
));
11268 if (type1
== type2
&& pos1
== pos2
)
11271 ls
= isl_local_space_from_space(isl_basic_map_get_space(bmap
));
11272 c
= isl_inequality_alloc(ls
);
11273 c
= isl_constraint_set_coefficient_si(c
, type1
, pos1
, 1);
11274 c
= isl_constraint_set_coefficient_si(c
, type2
, pos2
, -1);
11275 bmap
= isl_basic_map_add_constraint(bmap
, c
);
11280 /* Construct a basic map where the value of the first dimension is
11281 * greater than that of the second.
11283 static __isl_give isl_basic_map
*greator(__isl_take isl_space
*space
,
11284 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11286 isl_basic_map
*bmap
= NULL
;
11292 if (pos1
>= isl_space_dim(space
, type1
))
11293 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
11294 "index out of bounds", goto error
);
11295 if (pos2
>= isl_space_dim(space
, type2
))
11296 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
11297 "index out of bounds", goto error
);
11299 if (type1
== type2
&& pos1
== pos2
)
11300 return isl_basic_map_empty(space
);
11302 bmap
= isl_basic_map_alloc_space(space
, 0, 0, 1);
11303 i
= isl_basic_map_alloc_inequality(bmap
);
11306 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
11307 pos1
+= isl_basic_map_offset(bmap
, type1
);
11308 pos2
+= isl_basic_map_offset(bmap
, type2
);
11309 isl_int_set_si(bmap
->ineq
[i
][pos1
], 1);
11310 isl_int_set_si(bmap
->ineq
[i
][pos2
], -1);
11311 isl_int_set_si(bmap
->ineq
[i
][0], -1);
11312 bmap
= isl_basic_map_finalize(bmap
);
11316 isl_space_free(space
);
11317 isl_basic_map_free(bmap
);
11321 /* Add a constraint imposing that the value of the first dimension is
11322 * greater than that of the second.
11324 __isl_give isl_basic_map
*isl_basic_map_order_gt(__isl_take isl_basic_map
*bmap
,
11325 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11329 gt
= greator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
11331 bmap
= isl_basic_map_intersect(bmap
, gt
);
11336 /* Add a constraint imposing that the value of the first dimension is
11337 * greater than that of the second.
11339 __isl_give isl_map
*isl_map_order_gt(__isl_take isl_map
*map
,
11340 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11342 isl_basic_map
*bmap
;
11344 bmap
= greator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
11346 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
11351 /* Add a constraint imposing that the value of the first dimension is
11352 * smaller than that of the second.
11354 __isl_give isl_map
*isl_map_order_lt(__isl_take isl_map
*map
,
11355 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11357 return isl_map_order_gt(map
, type2
, pos2
, type1
, pos1
);
11360 __isl_give isl_aff
*isl_basic_map_get_div(__isl_keep isl_basic_map
*bmap
,
11364 isl_local_space
*ls
;
11369 if (!isl_basic_map_divs_known(bmap
))
11370 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
11371 "some divs are unknown", return NULL
);
11373 ls
= isl_basic_map_get_local_space(bmap
);
11374 div
= isl_local_space_get_div(ls
, pos
);
11375 isl_local_space_free(ls
);
11380 __isl_give isl_aff
*isl_basic_set_get_div(__isl_keep isl_basic_set
*bset
,
11383 return isl_basic_map_get_div(bset
, pos
);
11386 /* Plug in "subs" for dimension "type", "pos" of "bset".
11388 * Let i be the dimension to replace and let "subs" be of the form
11392 * Any integer division with a non-zero coefficient for i,
11394 * floor((a i + g)/m)
11398 * floor((a f + d g)/(m d))
11400 * Constraints of the form
11408 * We currently require that "subs" is an integral expression.
11409 * Handling rational expressions may require us to add stride constraints
11410 * as we do in isl_basic_set_preimage_multi_aff.
11412 __isl_give isl_basic_set
*isl_basic_set_substitute(
11413 __isl_take isl_basic_set
*bset
,
11414 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
11420 if (bset
&& isl_basic_set_plain_is_empty(bset
))
11423 bset
= isl_basic_set_cow(bset
);
11424 if (!bset
|| !subs
)
11427 ctx
= isl_basic_set_get_ctx(bset
);
11428 if (!isl_space_is_equal(bset
->dim
, subs
->ls
->dim
))
11429 isl_die(ctx
, isl_error_invalid
,
11430 "spaces don't match", goto error
);
11431 if (isl_local_space_dim(subs
->ls
, isl_dim_div
) != 0)
11432 isl_die(ctx
, isl_error_unsupported
,
11433 "cannot handle divs yet", goto error
);
11434 if (!isl_int_is_one(subs
->v
->el
[0]))
11435 isl_die(ctx
, isl_error_invalid
,
11436 "can only substitute integer expressions", goto error
);
11438 pos
+= isl_basic_set_offset(bset
, type
);
11442 for (i
= 0; i
< bset
->n_eq
; ++i
) {
11443 if (isl_int_is_zero(bset
->eq
[i
][pos
]))
11445 isl_int_set(v
, bset
->eq
[i
][pos
]);
11446 isl_int_set_si(bset
->eq
[i
][pos
], 0);
11447 isl_seq_combine(bset
->eq
[i
], subs
->v
->el
[0], bset
->eq
[i
],
11448 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
11451 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
11452 if (isl_int_is_zero(bset
->ineq
[i
][pos
]))
11454 isl_int_set(v
, bset
->ineq
[i
][pos
]);
11455 isl_int_set_si(bset
->ineq
[i
][pos
], 0);
11456 isl_seq_combine(bset
->ineq
[i
], subs
->v
->el
[0], bset
->ineq
[i
],
11457 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
11460 for (i
= 0; i
< bset
->n_div
; ++i
) {
11461 if (isl_int_is_zero(bset
->div
[i
][1 + pos
]))
11463 isl_int_set(v
, bset
->div
[i
][1 + pos
]);
11464 isl_int_set_si(bset
->div
[i
][1 + pos
], 0);
11465 isl_seq_combine(bset
->div
[i
] + 1,
11466 subs
->v
->el
[0], bset
->div
[i
] + 1,
11467 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
11468 isl_int_mul(bset
->div
[i
][0], bset
->div
[i
][0], subs
->v
->el
[0]);
11473 bset
= isl_basic_set_simplify(bset
);
11474 return isl_basic_set_finalize(bset
);
11476 isl_basic_set_free(bset
);
11480 /* Plug in "subs" for dimension "type", "pos" of "set".
11482 __isl_give isl_set
*isl_set_substitute(__isl_take isl_set
*set
,
11483 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
11487 if (set
&& isl_set_plain_is_empty(set
))
11490 set
= isl_set_cow(set
);
11494 for (i
= set
->n
- 1; i
>= 0; --i
) {
11495 set
->p
[i
] = isl_basic_set_substitute(set
->p
[i
], type
, pos
, subs
);
11496 if (remove_if_empty(set
, i
) < 0)
11506 /* Check if the range of "ma" is compatible with the domain or range
11507 * (depending on "type") of "bmap".
11508 * Return -1 if anything is wrong.
11510 static int check_basic_map_compatible_range_multi_aff(
11511 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type type
,
11512 __isl_keep isl_multi_aff
*ma
)
11515 isl_space
*ma_space
;
11517 ma_space
= isl_multi_aff_get_space(ma
);
11518 m
= isl_space_tuple_match(bmap
->dim
, type
, ma_space
, isl_dim_out
);
11519 isl_space_free(ma_space
);
11521 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
11522 "spaces don't match", return -1);
11526 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
11527 * coefficients before the transformed range of dimensions,
11528 * the "n_after" coefficients after the transformed range of dimensions
11529 * and the coefficients of the other divs in "bmap".
11531 static int set_ma_divs(__isl_keep isl_basic_map
*bmap
,
11532 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
, int n_div
)
11537 isl_local_space
*ls
;
11542 ls
= isl_aff_get_domain_local_space(ma
->p
[0]);
11546 n_param
= isl_local_space_dim(ls
, isl_dim_param
);
11547 n_set
= isl_local_space_dim(ls
, isl_dim_set
);
11548 for (i
= 0; i
< n_div
; ++i
) {
11549 int o_bmap
= 0, o_ls
= 0;
11551 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], 1 + 1 + n_param
);
11552 o_bmap
+= 1 + 1 + n_param
;
11553 o_ls
+= 1 + 1 + n_param
;
11554 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_before
);
11555 o_bmap
+= n_before
;
11556 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
11557 ls
->div
->row
[i
] + o_ls
, n_set
);
11560 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_after
);
11562 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
11563 ls
->div
->row
[i
] + o_ls
, n_div
);
11566 isl_seq_clr(bmap
->div
[i
] + o_bmap
, bmap
->n_div
- n_div
);
11567 if (isl_basic_set_add_div_constraints(bmap
, i
) < 0)
11571 isl_local_space_free(ls
);
11574 isl_local_space_free(ls
);
11578 /* How many stride constraints does "ma" enforce?
11579 * That is, how many of the affine expressions have a denominator
11580 * different from one?
11582 static int multi_aff_strides(__isl_keep isl_multi_aff
*ma
)
11587 for (i
= 0; i
< ma
->n
; ++i
)
11588 if (!isl_int_is_one(ma
->p
[i
]->v
->el
[0]))
11594 /* For each affine expression in ma of the form
11596 * x_i = (f_i y + h_i)/m_i
11598 * with m_i different from one, add a constraint to "bmap"
11601 * f_i y + h_i = m_i alpha_i
11603 * with alpha_i an additional existentially quantified variable.
11605 static __isl_give isl_basic_map
*add_ma_strides(
11606 __isl_take isl_basic_map
*bmap
, __isl_keep isl_multi_aff
*ma
,
11607 int n_before
, int n_after
)
11616 total
= isl_basic_map_total_dim(bmap
);
11617 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
11618 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
11619 n_div
= isl_multi_aff_dim(ma
, isl_dim_div
);
11620 for (i
= 0; i
< ma
->n
; ++i
) {
11621 int o_bmap
= 0, o_ma
= 1;
11623 if (isl_int_is_one(ma
->p
[i
]->v
->el
[0]))
11625 div
= isl_basic_map_alloc_div(bmap
);
11626 k
= isl_basic_map_alloc_equality(bmap
);
11627 if (div
< 0 || k
< 0)
11629 isl_int_set_si(bmap
->div
[div
][0], 0);
11630 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
11631 ma
->p
[i
]->v
->el
+ o_ma
, 1 + n_param
);
11632 o_bmap
+= 1 + n_param
;
11633 o_ma
+= 1 + n_param
;
11634 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_before
);
11635 o_bmap
+= n_before
;
11636 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
11637 ma
->p
[i
]->v
->el
+ o_ma
, n_in
);
11640 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_after
);
11642 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
11643 ma
->p
[i
]->v
->el
+ o_ma
, n_div
);
11646 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, 1 + total
- o_bmap
);
11647 isl_int_neg(bmap
->eq
[k
][1 + total
], ma
->p
[i
]->v
->el
[0]);
11653 isl_basic_map_free(bmap
);
11657 /* Replace the domain or range space (depending on "type) of "space" by "set".
11659 static __isl_give isl_space
*isl_space_set(__isl_take isl_space
*space
,
11660 enum isl_dim_type type
, __isl_take isl_space
*set
)
11662 if (type
== isl_dim_in
) {
11663 space
= isl_space_range(space
);
11664 space
= isl_space_map_from_domain_and_range(set
, space
);
11666 space
= isl_space_domain(space
);
11667 space
= isl_space_map_from_domain_and_range(space
, set
);
11673 /* Compute the preimage of the domain or range (depending on "type")
11674 * of "bmap" under the function represented by "ma".
11675 * In other words, plug in "ma" in the domain or range of "bmap".
11676 * The result is a basic map that lives in the same space as "bmap"
11677 * except that the domain or range has been replaced by
11678 * the domain space of "ma".
11680 * If bmap is represented by
11682 * A(p) + S u + B x + T v + C(divs) >= 0,
11684 * where u and x are input and output dimensions if type == isl_dim_out
11685 * while x and v are input and output dimensions if type == isl_dim_in,
11686 * and ma is represented by
11688 * x = D(p) + F(y) + G(divs')
11690 * then the result is
11692 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
11694 * The divs in the input set are similarly adjusted.
11697 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
11701 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
11702 * B_i G(divs') + c_i(divs))/n_i)
11704 * If bmap is not a rational map and if F(y) involves any denominators
11706 * x_i = (f_i y + h_i)/m_i
11708 * then additional constraints are added to ensure that we only
11709 * map back integer points. That is we enforce
11711 * f_i y + h_i = m_i alpha_i
11713 * with alpha_i an additional existentially quantified variable.
11715 * We first copy over the divs from "ma".
11716 * Then we add the modified constraints and divs from "bmap".
11717 * Finally, we add the stride constraints, if needed.
11719 __isl_give isl_basic_map
*isl_basic_map_preimage_multi_aff(
11720 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
11721 __isl_take isl_multi_aff
*ma
)
11725 isl_basic_map
*res
= NULL
;
11726 int n_before
, n_after
, n_div_bmap
, n_div_ma
;
11727 isl_int f
, c1
, c2
, g
;
11728 int rational
, strides
;
11735 ma
= isl_multi_aff_align_divs(ma
);
11738 if (check_basic_map_compatible_range_multi_aff(bmap
, type
, ma
) < 0)
11741 if (type
== isl_dim_in
) {
11743 n_after
= isl_basic_map_dim(bmap
, isl_dim_out
);
11745 n_before
= isl_basic_map_dim(bmap
, isl_dim_in
);
11748 n_div_bmap
= isl_basic_map_dim(bmap
, isl_dim_div
);
11749 n_div_ma
= ma
->n
? isl_aff_dim(ma
->p
[0], isl_dim_div
) : 0;
11751 space
= isl_multi_aff_get_domain_space(ma
);
11752 space
= isl_space_set(isl_basic_map_get_space(bmap
), type
, space
);
11753 rational
= isl_basic_map_is_rational(bmap
);
11754 strides
= rational
? 0 : multi_aff_strides(ma
);
11755 res
= isl_basic_map_alloc_space(space
, n_div_ma
+ n_div_bmap
+ strides
,
11756 bmap
->n_eq
+ strides
, bmap
->n_ineq
+ 2 * n_div_ma
);
11758 res
= isl_basic_map_set_rational(res
);
11760 for (i
= 0; i
< n_div_ma
+ n_div_bmap
; ++i
)
11761 if (isl_basic_map_alloc_div(res
) < 0)
11764 if (set_ma_divs(res
, ma
, n_before
, n_after
, n_div_ma
) < 0)
11767 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
11768 k
= isl_basic_map_alloc_equality(res
);
11771 isl_seq_preimage(res
->eq
[k
], bmap
->eq
[i
], ma
, n_before
,
11772 n_after
, n_div_ma
, n_div_bmap
, f
, c1
, c2
, g
, 0);
11775 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
11776 k
= isl_basic_map_alloc_inequality(res
);
11779 isl_seq_preimage(res
->ineq
[k
], bmap
->ineq
[i
], ma
, n_before
,
11780 n_after
, n_div_ma
, n_div_bmap
, f
, c1
, c2
, g
, 0);
11783 for (i
= 0; i
< bmap
->n_div
; ++i
) {
11784 if (isl_int_is_zero(bmap
->div
[i
][0])) {
11785 isl_int_set_si(res
->div
[n_div_ma
+ i
][0], 0);
11788 isl_seq_preimage(res
->div
[n_div_ma
+ i
], bmap
->div
[i
], ma
,
11789 n_before
, n_after
, n_div_ma
, n_div_bmap
,
11794 res
= add_ma_strides(res
, ma
, n_before
, n_after
);
11800 isl_basic_map_free(bmap
);
11801 isl_multi_aff_free(ma
);
11802 res
= isl_basic_set_simplify(res
);
11803 return isl_basic_map_finalize(res
);
11809 isl_basic_map_free(bmap
);
11810 isl_multi_aff_free(ma
);
11811 isl_basic_map_free(res
);
11815 /* Compute the preimage of "bset" under the function represented by "ma".
11816 * In other words, plug in "ma" in "bset". The result is a basic set
11817 * that lives in the domain space of "ma".
11819 __isl_give isl_basic_set
*isl_basic_set_preimage_multi_aff(
11820 __isl_take isl_basic_set
*bset
, __isl_take isl_multi_aff
*ma
)
11822 return isl_basic_map_preimage_multi_aff(bset
, isl_dim_set
, ma
);
11825 /* Check if the range of "ma" is compatible with the domain or range
11826 * (depending on "type") of "map".
11827 * Return -1 if anything is wrong.
11829 static int check_map_compatible_range_multi_aff(
11830 __isl_keep isl_map
*map
, enum isl_dim_type type
,
11831 __isl_keep isl_multi_aff
*ma
)
11834 isl_space
*ma_space
;
11836 ma_space
= isl_multi_aff_get_space(ma
);
11837 m
= isl_space_tuple_match(map
->dim
, type
, ma_space
, isl_dim_out
);
11838 isl_space_free(ma_space
);
11840 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
11841 "spaces don't match", return -1);
11845 /* Compute the preimage of the domain or range (depending on "type")
11846 * of "map" under the function represented by "ma".
11847 * In other words, plug in "ma" in the domain or range of "map".
11848 * The result is a map that lives in the same space as "map"
11849 * except that the domain or range has been replaced by
11850 * the domain space of "ma".
11852 * The parameters are assumed to have been aligned.
11854 static __isl_give isl_map
*map_preimage_multi_aff(__isl_take isl_map
*map
,
11855 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
11860 map
= isl_map_cow(map
);
11861 ma
= isl_multi_aff_align_divs(ma
);
11864 if (check_map_compatible_range_multi_aff(map
, type
, ma
) < 0)
11867 for (i
= 0; i
< map
->n
; ++i
) {
11868 map
->p
[i
] = isl_basic_map_preimage_multi_aff(map
->p
[i
], type
,
11869 isl_multi_aff_copy(ma
));
11874 space
= isl_multi_aff_get_domain_space(ma
);
11875 space
= isl_space_set(isl_map_get_space(map
), type
, space
);
11877 isl_space_free(map
->dim
);
11882 isl_multi_aff_free(ma
);
11884 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
11885 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
11888 isl_multi_aff_free(ma
);
11893 /* Compute the preimage of the domain or range (depending on "type")
11894 * of "map" under the function represented by "ma".
11895 * In other words, plug in "ma" in the domain or range of "map".
11896 * The result is a map that lives in the same space as "map"
11897 * except that the domain or range has been replaced by
11898 * the domain space of "ma".
11900 __isl_give isl_map
*isl_map_preimage_multi_aff(__isl_take isl_map
*map
,
11901 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
11906 if (isl_space_match(map
->dim
, isl_dim_param
, ma
->space
, isl_dim_param
))
11907 return map_preimage_multi_aff(map
, type
, ma
);
11909 if (!isl_space_has_named_params(map
->dim
) ||
11910 !isl_space_has_named_params(ma
->space
))
11911 isl_die(map
->ctx
, isl_error_invalid
,
11912 "unaligned unnamed parameters", goto error
);
11913 map
= isl_map_align_params(map
, isl_multi_aff_get_space(ma
));
11914 ma
= isl_multi_aff_align_params(ma
, isl_map_get_space(map
));
11916 return map_preimage_multi_aff(map
, type
, ma
);
11918 isl_multi_aff_free(ma
);
11919 return isl_map_free(map
);
11922 /* Compute the preimage of "set" under the function represented by "ma".
11923 * In other words, plug in "ma" "set". The result is a set
11924 * that lives in the domain space of "ma".
11926 __isl_give isl_set
*isl_set_preimage_multi_aff(__isl_take isl_set
*set
,
11927 __isl_take isl_multi_aff
*ma
)
11929 return isl_map_preimage_multi_aff(set
, isl_dim_set
, ma
);
11932 /* Compute the preimage of the domain of "map" under the function
11933 * represented by "ma".
11934 * In other words, plug in "ma" in the domain of "map".
11935 * The result is a map that lives in the same space as "map"
11936 * except that the domain has been replaced by the domain space of "ma".
11938 __isl_give isl_map
*isl_map_preimage_domain_multi_aff(__isl_take isl_map
*map
,
11939 __isl_take isl_multi_aff
*ma
)
11941 return isl_map_preimage_multi_aff(map
, isl_dim_in
, ma
);
11944 /* Compute the preimage of "set" under the function represented by "pma".
11945 * In other words, plug in "pma" in "set. The result is a set
11946 * that lives in the domain space of "pma".
11948 static __isl_give isl_set
*set_preimage_pw_multi_aff(__isl_take isl_set
*set
,
11949 __isl_take isl_pw_multi_aff
*pma
)
11958 isl_pw_multi_aff_free(pma
);
11959 res
= isl_set_empty(isl_set_get_space(set
));
11964 res
= isl_set_preimage_multi_aff(isl_set_copy(set
),
11965 isl_multi_aff_copy(pma
->p
[0].maff
));
11966 res
= isl_set_intersect(res
, isl_set_copy(pma
->p
[0].set
));
11968 for (i
= 1; i
< pma
->n
; ++i
) {
11971 res_i
= isl_set_preimage_multi_aff(isl_set_copy(set
),
11972 isl_multi_aff_copy(pma
->p
[i
].maff
));
11973 res_i
= isl_set_intersect(res_i
, isl_set_copy(pma
->p
[i
].set
));
11974 res
= isl_set_union(res
, res_i
);
11977 isl_pw_multi_aff_free(pma
);
11981 isl_pw_multi_aff_free(pma
);
11986 __isl_give isl_set
*isl_set_preimage_pw_multi_aff(__isl_take isl_set
*set
,
11987 __isl_take isl_pw_multi_aff
*pma
)
11992 if (isl_space_match(set
->dim
, isl_dim_param
, pma
->dim
, isl_dim_param
))
11993 return set_preimage_pw_multi_aff(set
, pma
);
11995 if (!isl_space_has_named_params(set
->dim
) ||
11996 !isl_space_has_named_params(pma
->dim
))
11997 isl_die(set
->ctx
, isl_error_invalid
,
11998 "unaligned unnamed parameters", goto error
);
11999 set
= isl_set_align_params(set
, isl_pw_multi_aff_get_space(pma
));
12000 pma
= isl_pw_multi_aff_align_params(pma
, isl_set_get_space(set
));
12002 return set_preimage_pw_multi_aff(set
, pma
);
12004 isl_pw_multi_aff_free(pma
);
12005 return isl_set_free(set
);