2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012-2014 Ecole Normale Superieure
5 * Copyright 2014 INRIA Rocquencourt
6 * Copyright 2016 INRIA Paris
7 * Copyright 2016 Sven Verdoolaege
9 * Use of this software is governed by the MIT license
11 * Written by Sven Verdoolaege, K.U.Leuven, Departement
12 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
13 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
14 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
15 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
16 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
17 * B.P. 105 - 78153 Le Chesnay, France
18 * and Centre de Recherche Inria de Paris, 2 rue Simone Iff - Voie DQ12,
19 * CS 42112, 75589 Paris Cedex 12, France
23 #include <isl_ctx_private.h>
24 #include <isl_map_private.h>
26 #include <isl/constraint.h>
27 #include "isl_space_private.h"
28 #include "isl_equalities.h"
29 #include <isl_lp_private.h>
33 #include <isl_reordering.h>
34 #include "isl_sample.h"
38 #include <isl_mat_private.h>
39 #include <isl_vec_private.h>
40 #include <isl_dim_map.h>
41 #include <isl_local_space_private.h>
42 #include <isl_aff_private.h>
43 #include <isl_options_private.h>
44 #include <isl_morph.h>
45 #include <isl_val_private.h>
46 #include <isl/deprecated/map_int.h>
47 #include <isl/deprecated/set_int.h>
49 #include <bset_to_bmap.c>
50 #include <bset_from_bmap.c>
51 #include <set_to_map.c>
52 #include <set_from_map.c>
54 static unsigned n(__isl_keep isl_space
*dim
, enum isl_dim_type type
)
57 case isl_dim_param
: return dim
->nparam
;
58 case isl_dim_in
: return dim
->n_in
;
59 case isl_dim_out
: return dim
->n_out
;
60 case isl_dim_all
: return dim
->nparam
+ dim
->n_in
+ dim
->n_out
;
65 static unsigned pos(__isl_keep isl_space
*dim
, enum isl_dim_type type
)
68 case isl_dim_param
: return 1;
69 case isl_dim_in
: return 1 + dim
->nparam
;
70 case isl_dim_out
: return 1 + dim
->nparam
+ dim
->n_in
;
75 unsigned isl_basic_map_dim(__isl_keep isl_basic_map
*bmap
,
76 enum isl_dim_type type
)
81 case isl_dim_cst
: return 1;
84 case isl_dim_out
: return isl_space_dim(bmap
->dim
, type
);
85 case isl_dim_div
: return bmap
->n_div
;
86 case isl_dim_all
: return isl_basic_map_total_dim(bmap
);
91 unsigned isl_map_dim(__isl_keep isl_map
*map
, enum isl_dim_type type
)
93 return map
? n(map
->dim
, type
) : 0;
96 unsigned isl_set_dim(__isl_keep isl_set
*set
, enum isl_dim_type type
)
98 return set
? n(set
->dim
, type
) : 0;
101 unsigned isl_basic_map_offset(struct isl_basic_map
*bmap
,
102 enum isl_dim_type type
)
111 case isl_dim_cst
: return 0;
112 case isl_dim_param
: return 1;
113 case isl_dim_in
: return 1 + space
->nparam
;
114 case isl_dim_out
: return 1 + space
->nparam
+ space
->n_in
;
115 case isl_dim_div
: return 1 + space
->nparam
+ space
->n_in
+
121 unsigned isl_basic_set_offset(struct isl_basic_set
*bset
,
122 enum isl_dim_type type
)
124 return isl_basic_map_offset(bset
, type
);
127 static unsigned map_offset(struct isl_map
*map
, enum isl_dim_type type
)
129 return pos(map
->dim
, type
);
132 unsigned isl_basic_set_dim(__isl_keep isl_basic_set
*bset
,
133 enum isl_dim_type type
)
135 return isl_basic_map_dim(bset
, type
);
138 unsigned isl_basic_set_n_dim(__isl_keep isl_basic_set
*bset
)
140 return isl_basic_set_dim(bset
, isl_dim_set
);
143 unsigned isl_basic_set_n_param(__isl_keep isl_basic_set
*bset
)
145 return isl_basic_set_dim(bset
, isl_dim_param
);
148 unsigned isl_basic_set_total_dim(const struct isl_basic_set
*bset
)
152 return isl_space_dim(bset
->dim
, isl_dim_all
) + bset
->n_div
;
155 unsigned isl_set_n_dim(__isl_keep isl_set
*set
)
157 return isl_set_dim(set
, isl_dim_set
);
160 unsigned isl_set_n_param(__isl_keep isl_set
*set
)
162 return isl_set_dim(set
, isl_dim_param
);
165 unsigned isl_basic_map_n_in(const struct isl_basic_map
*bmap
)
167 return bmap
? bmap
->dim
->n_in
: 0;
170 unsigned isl_basic_map_n_out(const struct isl_basic_map
*bmap
)
172 return bmap
? bmap
->dim
->n_out
: 0;
175 unsigned isl_basic_map_n_param(const struct isl_basic_map
*bmap
)
177 return bmap
? bmap
->dim
->nparam
: 0;
180 unsigned isl_basic_map_n_div(const struct isl_basic_map
*bmap
)
182 return bmap
? bmap
->n_div
: 0;
185 unsigned isl_basic_map_total_dim(const struct isl_basic_map
*bmap
)
187 return bmap
? isl_space_dim(bmap
->dim
, isl_dim_all
) + bmap
->n_div
: 0;
190 unsigned isl_map_n_in(const struct isl_map
*map
)
192 return map
? map
->dim
->n_in
: 0;
195 unsigned isl_map_n_out(const struct isl_map
*map
)
197 return map
? map
->dim
->n_out
: 0;
200 unsigned isl_map_n_param(const struct isl_map
*map
)
202 return map
? map
->dim
->nparam
: 0;
205 isl_bool
isl_map_compatible_domain(__isl_keep isl_map
*map
,
206 __isl_keep isl_set
*set
)
210 return isl_bool_error
;
211 m
= isl_space_match(map
->dim
, isl_dim_param
, set
->dim
, isl_dim_param
);
214 return isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
215 set
->dim
, isl_dim_set
);
218 isl_bool
isl_basic_map_compatible_domain(__isl_keep isl_basic_map
*bmap
,
219 __isl_keep isl_basic_set
*bset
)
223 return isl_bool_error
;
224 m
= isl_space_match(bmap
->dim
, isl_dim_param
, bset
->dim
, isl_dim_param
);
227 return isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
228 bset
->dim
, isl_dim_set
);
231 isl_bool
isl_map_compatible_range(__isl_keep isl_map
*map
,
232 __isl_keep isl_set
*set
)
236 return isl_bool_error
;
237 m
= isl_space_match(map
->dim
, isl_dim_param
, set
->dim
, isl_dim_param
);
240 return isl_space_tuple_is_equal(map
->dim
, isl_dim_out
,
241 set
->dim
, isl_dim_set
);
244 isl_bool
isl_basic_map_compatible_range(__isl_keep isl_basic_map
*bmap
,
245 __isl_keep isl_basic_set
*bset
)
249 return isl_bool_error
;
250 m
= isl_space_match(bmap
->dim
, isl_dim_param
, bset
->dim
, isl_dim_param
);
253 return isl_space_tuple_is_equal(bmap
->dim
, isl_dim_out
,
254 bset
->dim
, isl_dim_set
);
257 isl_ctx
*isl_basic_map_get_ctx(__isl_keep isl_basic_map
*bmap
)
259 return bmap
? bmap
->ctx
: NULL
;
262 isl_ctx
*isl_basic_set_get_ctx(__isl_keep isl_basic_set
*bset
)
264 return bset
? bset
->ctx
: NULL
;
267 isl_ctx
*isl_map_get_ctx(__isl_keep isl_map
*map
)
269 return map
? map
->ctx
: NULL
;
272 isl_ctx
*isl_set_get_ctx(__isl_keep isl_set
*set
)
274 return set
? set
->ctx
: NULL
;
277 /* Return the space of "bmap".
279 __isl_keep isl_space
*isl_basic_map_peek_space(
280 __isl_keep
const isl_basic_map
*bmap
)
282 return bmap
? bmap
->dim
: NULL
;
285 __isl_give isl_space
*isl_basic_map_get_space(__isl_keep isl_basic_map
*bmap
)
287 return isl_space_copy(isl_basic_map_peek_space(bmap
));
290 __isl_give isl_space
*isl_basic_set_get_space(__isl_keep isl_basic_set
*bset
)
292 return isl_basic_map_get_space(bset_to_bmap(bset
));
295 /* Extract the divs in "bmap" as a matrix.
297 __isl_give isl_mat
*isl_basic_map_get_divs(__isl_keep isl_basic_map
*bmap
)
308 ctx
= isl_basic_map_get_ctx(bmap
);
309 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
310 cols
= 1 + 1 + total
+ bmap
->n_div
;
311 div
= isl_mat_alloc(ctx
, bmap
->n_div
, cols
);
315 for (i
= 0; i
< bmap
->n_div
; ++i
)
316 isl_seq_cpy(div
->row
[i
], bmap
->div
[i
], cols
);
321 /* Extract the divs in "bset" as a matrix.
323 __isl_give isl_mat
*isl_basic_set_get_divs(__isl_keep isl_basic_set
*bset
)
325 return isl_basic_map_get_divs(bset
);
328 __isl_give isl_local_space
*isl_basic_map_get_local_space(
329 __isl_keep isl_basic_map
*bmap
)
336 div
= isl_basic_map_get_divs(bmap
);
337 return isl_local_space_alloc_div(isl_space_copy(bmap
->dim
), div
);
340 __isl_give isl_local_space
*isl_basic_set_get_local_space(
341 __isl_keep isl_basic_set
*bset
)
343 return isl_basic_map_get_local_space(bset
);
346 /* For each known div d = floor(f/m), add the constraints
349 * -(f-(m-1)) + m d >= 0
351 * Do not finalize the result.
353 static __isl_give isl_basic_map
*add_known_div_constraints(
354 __isl_take isl_basic_map
*bmap
)
361 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
364 bmap
= isl_basic_map_cow(bmap
);
365 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 2 * n_div
);
368 for (i
= 0; i
< n_div
; ++i
) {
369 if (isl_int_is_zero(bmap
->div
[i
][0]))
371 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
372 return isl_basic_map_free(bmap
);
378 __isl_give isl_basic_map
*isl_basic_map_from_local_space(
379 __isl_take isl_local_space
*ls
)
388 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
389 bmap
= isl_basic_map_alloc_space(isl_local_space_get_space(ls
),
390 n_div
, 0, 2 * n_div
);
392 for (i
= 0; i
< n_div
; ++i
)
393 if (isl_basic_map_alloc_div(bmap
) < 0)
396 for (i
= 0; i
< n_div
; ++i
)
397 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], ls
->div
->n_col
);
398 bmap
= add_known_div_constraints(bmap
);
400 isl_local_space_free(ls
);
403 isl_local_space_free(ls
);
404 isl_basic_map_free(bmap
);
408 __isl_give isl_basic_set
*isl_basic_set_from_local_space(
409 __isl_take isl_local_space
*ls
)
411 return isl_basic_map_from_local_space(ls
);
414 __isl_give isl_space
*isl_map_get_space(__isl_keep isl_map
*map
)
418 return isl_space_copy(map
->dim
);
421 __isl_give isl_space
*isl_set_get_space(__isl_keep isl_set
*set
)
425 return isl_space_copy(set
->dim
);
428 __isl_give isl_basic_map
*isl_basic_map_set_tuple_name(
429 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
, const char *s
)
431 bmap
= isl_basic_map_cow(bmap
);
434 bmap
->dim
= isl_space_set_tuple_name(bmap
->dim
, type
, s
);
437 bmap
= isl_basic_map_finalize(bmap
);
440 isl_basic_map_free(bmap
);
444 __isl_give isl_basic_set
*isl_basic_set_set_tuple_name(
445 __isl_take isl_basic_set
*bset
, const char *s
)
447 return isl_basic_map_set_tuple_name(bset
, isl_dim_set
, s
);
450 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map
*bmap
,
451 enum isl_dim_type type
)
453 return bmap
? isl_space_get_tuple_name(bmap
->dim
, type
) : NULL
;
456 __isl_give isl_map
*isl_map_set_tuple_name(__isl_take isl_map
*map
,
457 enum isl_dim_type type
, const char *s
)
461 map
= isl_map_cow(map
);
465 map
->dim
= isl_space_set_tuple_name(map
->dim
, type
, s
);
469 for (i
= 0; i
< map
->n
; ++i
) {
470 map
->p
[i
] = isl_basic_map_set_tuple_name(map
->p
[i
], type
, s
);
481 /* Replace the identifier of the tuple of type "type" by "id".
483 __isl_give isl_basic_map
*isl_basic_map_set_tuple_id(
484 __isl_take isl_basic_map
*bmap
,
485 enum isl_dim_type type
, __isl_take isl_id
*id
)
487 bmap
= isl_basic_map_cow(bmap
);
490 bmap
->dim
= isl_space_set_tuple_id(bmap
->dim
, type
, id
);
492 return isl_basic_map_free(bmap
);
493 bmap
= isl_basic_map_finalize(bmap
);
500 /* Replace the identifier of the tuple by "id".
502 __isl_give isl_basic_set
*isl_basic_set_set_tuple_id(
503 __isl_take isl_basic_set
*bset
, __isl_take isl_id
*id
)
505 return isl_basic_map_set_tuple_id(bset
, isl_dim_set
, id
);
508 /* Does the input or output tuple have a name?
510 isl_bool
isl_map_has_tuple_name(__isl_keep isl_map
*map
, enum isl_dim_type type
)
512 return map
? isl_space_has_tuple_name(map
->dim
, type
) : isl_bool_error
;
515 const char *isl_map_get_tuple_name(__isl_keep isl_map
*map
,
516 enum isl_dim_type type
)
518 return map
? isl_space_get_tuple_name(map
->dim
, type
) : NULL
;
521 __isl_give isl_set
*isl_set_set_tuple_name(__isl_take isl_set
*set
,
524 return set_from_map(isl_map_set_tuple_name(set_to_map(set
),
528 __isl_give isl_map
*isl_map_set_tuple_id(__isl_take isl_map
*map
,
529 enum isl_dim_type type
, __isl_take isl_id
*id
)
531 map
= isl_map_cow(map
);
535 map
->dim
= isl_space_set_tuple_id(map
->dim
, type
, id
);
537 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
543 __isl_give isl_set
*isl_set_set_tuple_id(__isl_take isl_set
*set
,
544 __isl_take isl_id
*id
)
546 return isl_map_set_tuple_id(set
, isl_dim_set
, id
);
549 __isl_give isl_map
*isl_map_reset_tuple_id(__isl_take isl_map
*map
,
550 enum isl_dim_type type
)
552 map
= isl_map_cow(map
);
556 map
->dim
= isl_space_reset_tuple_id(map
->dim
, type
);
558 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
561 __isl_give isl_set
*isl_set_reset_tuple_id(__isl_take isl_set
*set
)
563 return isl_map_reset_tuple_id(set
, isl_dim_set
);
566 isl_bool
isl_map_has_tuple_id(__isl_keep isl_map
*map
, enum isl_dim_type type
)
568 return map
? isl_space_has_tuple_id(map
->dim
, type
) : isl_bool_error
;
571 __isl_give isl_id
*isl_map_get_tuple_id(__isl_keep isl_map
*map
,
572 enum isl_dim_type type
)
574 return map
? isl_space_get_tuple_id(map
->dim
, type
) : NULL
;
577 isl_bool
isl_set_has_tuple_id(__isl_keep isl_set
*set
)
579 return isl_map_has_tuple_id(set
, isl_dim_set
);
582 __isl_give isl_id
*isl_set_get_tuple_id(__isl_keep isl_set
*set
)
584 return isl_map_get_tuple_id(set
, isl_dim_set
);
587 /* Does the set tuple have a name?
589 isl_bool
isl_set_has_tuple_name(__isl_keep isl_set
*set
)
592 return isl_bool_error
;
593 return isl_space_has_tuple_name(set
->dim
, isl_dim_set
);
597 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set
*bset
)
599 return bset
? isl_space_get_tuple_name(bset
->dim
, isl_dim_set
) : NULL
;
602 const char *isl_set_get_tuple_name(__isl_keep isl_set
*set
)
604 return set
? isl_space_get_tuple_name(set
->dim
, isl_dim_set
) : NULL
;
607 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map
*bmap
,
608 enum isl_dim_type type
, unsigned pos
)
610 return bmap
? isl_space_get_dim_name(bmap
->dim
, type
, pos
) : NULL
;
613 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set
*bset
,
614 enum isl_dim_type type
, unsigned pos
)
616 return bset
? isl_space_get_dim_name(bset
->dim
, type
, pos
) : NULL
;
619 /* Does the given dimension have a name?
621 isl_bool
isl_map_has_dim_name(__isl_keep isl_map
*map
,
622 enum isl_dim_type type
, unsigned pos
)
625 return isl_bool_error
;
626 return isl_space_has_dim_name(map
->dim
, type
, pos
);
629 const char *isl_map_get_dim_name(__isl_keep isl_map
*map
,
630 enum isl_dim_type type
, unsigned pos
)
632 return map
? isl_space_get_dim_name(map
->dim
, type
, pos
) : NULL
;
635 const char *isl_set_get_dim_name(__isl_keep isl_set
*set
,
636 enum isl_dim_type type
, unsigned pos
)
638 return set
? isl_space_get_dim_name(set
->dim
, type
, pos
) : NULL
;
641 /* Does the given dimension have a name?
643 isl_bool
isl_set_has_dim_name(__isl_keep isl_set
*set
,
644 enum isl_dim_type type
, unsigned pos
)
647 return isl_bool_error
;
648 return isl_space_has_dim_name(set
->dim
, type
, pos
);
651 __isl_give isl_basic_map
*isl_basic_map_set_dim_name(
652 __isl_take isl_basic_map
*bmap
,
653 enum isl_dim_type type
, unsigned pos
, const char *s
)
655 bmap
= isl_basic_map_cow(bmap
);
658 bmap
->dim
= isl_space_set_dim_name(bmap
->dim
, type
, pos
, s
);
661 return isl_basic_map_finalize(bmap
);
663 isl_basic_map_free(bmap
);
667 __isl_give isl_map
*isl_map_set_dim_name(__isl_take isl_map
*map
,
668 enum isl_dim_type type
, unsigned pos
, const char *s
)
672 map
= isl_map_cow(map
);
676 map
->dim
= isl_space_set_dim_name(map
->dim
, type
, pos
, s
);
680 for (i
= 0; i
< map
->n
; ++i
) {
681 map
->p
[i
] = isl_basic_map_set_dim_name(map
->p
[i
], type
, pos
, s
);
692 __isl_give isl_basic_set
*isl_basic_set_set_dim_name(
693 __isl_take isl_basic_set
*bset
,
694 enum isl_dim_type type
, unsigned pos
, const char *s
)
696 return bset_from_bmap(isl_basic_map_set_dim_name(bset_to_bmap(bset
),
700 __isl_give isl_set
*isl_set_set_dim_name(__isl_take isl_set
*set
,
701 enum isl_dim_type type
, unsigned pos
, const char *s
)
703 return set_from_map(isl_map_set_dim_name(set_to_map(set
),
707 isl_bool
isl_basic_map_has_dim_id(__isl_keep isl_basic_map
*bmap
,
708 enum isl_dim_type type
, unsigned pos
)
711 return isl_bool_error
;
712 return isl_space_has_dim_id(bmap
->dim
, type
, pos
);
715 __isl_give isl_id
*isl_basic_set_get_dim_id(__isl_keep isl_basic_set
*bset
,
716 enum isl_dim_type type
, unsigned pos
)
718 return bset
? isl_space_get_dim_id(bset
->dim
, type
, pos
) : NULL
;
721 isl_bool
isl_map_has_dim_id(__isl_keep isl_map
*map
,
722 enum isl_dim_type type
, unsigned pos
)
724 return map
? isl_space_has_dim_id(map
->dim
, type
, pos
) : isl_bool_error
;
727 __isl_give isl_id
*isl_map_get_dim_id(__isl_keep isl_map
*map
,
728 enum isl_dim_type type
, unsigned pos
)
730 return map
? isl_space_get_dim_id(map
->dim
, type
, pos
) : NULL
;
733 isl_bool
isl_set_has_dim_id(__isl_keep isl_set
*set
,
734 enum isl_dim_type type
, unsigned pos
)
736 return isl_map_has_dim_id(set
, type
, pos
);
739 __isl_give isl_id
*isl_set_get_dim_id(__isl_keep isl_set
*set
,
740 enum isl_dim_type type
, unsigned pos
)
742 return isl_map_get_dim_id(set
, type
, pos
);
745 __isl_give isl_map
*isl_map_set_dim_id(__isl_take isl_map
*map
,
746 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
748 map
= isl_map_cow(map
);
752 map
->dim
= isl_space_set_dim_id(map
->dim
, type
, pos
, id
);
754 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
760 __isl_give isl_set
*isl_set_set_dim_id(__isl_take isl_set
*set
,
761 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
763 return isl_map_set_dim_id(set
, type
, pos
, id
);
766 int isl_map_find_dim_by_id(__isl_keep isl_map
*map
, enum isl_dim_type type
,
767 __isl_keep isl_id
*id
)
771 return isl_space_find_dim_by_id(map
->dim
, type
, id
);
774 int isl_set_find_dim_by_id(__isl_keep isl_set
*set
, enum isl_dim_type type
,
775 __isl_keep isl_id
*id
)
777 return isl_map_find_dim_by_id(set
, type
, id
);
780 /* Return the position of the dimension of the given type and name
782 * Return -1 if no such dimension can be found.
784 int isl_basic_map_find_dim_by_name(__isl_keep isl_basic_map
*bmap
,
785 enum isl_dim_type type
, const char *name
)
789 return isl_space_find_dim_by_name(bmap
->dim
, type
, name
);
792 int isl_map_find_dim_by_name(__isl_keep isl_map
*map
, enum isl_dim_type type
,
797 return isl_space_find_dim_by_name(map
->dim
, type
, name
);
800 int isl_set_find_dim_by_name(__isl_keep isl_set
*set
, enum isl_dim_type type
,
803 return isl_map_find_dim_by_name(set
, type
, name
);
806 /* Reset the user pointer on all identifiers of parameters and tuples
807 * of the space of "map".
809 __isl_give isl_map
*isl_map_reset_user(__isl_take isl_map
*map
)
813 space
= isl_map_get_space(map
);
814 space
= isl_space_reset_user(space
);
815 map
= isl_map_reset_space(map
, space
);
820 /* Reset the user pointer on all identifiers of parameters and tuples
821 * of the space of "set".
823 __isl_give isl_set
*isl_set_reset_user(__isl_take isl_set
*set
)
825 return isl_map_reset_user(set
);
828 isl_bool
isl_basic_map_is_rational(__isl_keep isl_basic_map
*bmap
)
831 return isl_bool_error
;
832 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
835 /* Has "map" been marked as a rational map?
836 * In particular, have all basic maps in "map" been marked this way?
837 * An empty map is not considered to be rational.
838 * Maps where only some of the basic maps are marked rational
841 isl_bool
isl_map_is_rational(__isl_keep isl_map
*map
)
847 return isl_bool_error
;
849 return isl_bool_false
;
850 rational
= isl_basic_map_is_rational(map
->p
[0]);
853 for (i
= 1; i
< map
->n
; ++i
) {
856 rational_i
= isl_basic_map_is_rational(map
->p
[i
]);
859 if (rational
!= rational_i
)
860 isl_die(isl_map_get_ctx(map
), isl_error_unsupported
,
861 "mixed rational and integer basic maps "
862 "not supported", return isl_bool_error
);
868 /* Has "set" been marked as a rational set?
869 * In particular, have all basic set in "set" been marked this way?
870 * An empty set is not considered to be rational.
871 * Sets where only some of the basic sets are marked rational
874 isl_bool
isl_set_is_rational(__isl_keep isl_set
*set
)
876 return isl_map_is_rational(set
);
879 int isl_basic_set_is_rational(__isl_keep isl_basic_set
*bset
)
881 return isl_basic_map_is_rational(bset
);
884 /* Does "bmap" contain any rational points?
886 * If "bmap" has an equality for each dimension, equating the dimension
887 * to an integer constant, then it has no rational points, even if it
888 * is marked as rational.
890 isl_bool
isl_basic_map_has_rational(__isl_keep isl_basic_map
*bmap
)
892 isl_bool has_rational
= isl_bool_true
;
896 return isl_bool_error
;
897 if (isl_basic_map_plain_is_empty(bmap
))
898 return isl_bool_false
;
899 if (!isl_basic_map_is_rational(bmap
))
900 return isl_bool_false
;
901 bmap
= isl_basic_map_copy(bmap
);
902 bmap
= isl_basic_map_implicit_equalities(bmap
);
904 return isl_bool_error
;
905 total
= isl_basic_map_total_dim(bmap
);
906 if (bmap
->n_eq
== total
) {
908 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
909 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1, total
);
912 if (!isl_int_is_one(bmap
->eq
[i
][1 + j
]) &&
913 !isl_int_is_negone(bmap
->eq
[i
][1 + j
]))
915 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + j
+ 1,
921 has_rational
= isl_bool_false
;
923 isl_basic_map_free(bmap
);
928 /* Does "map" contain any rational points?
930 isl_bool
isl_map_has_rational(__isl_keep isl_map
*map
)
933 isl_bool has_rational
;
936 return isl_bool_error
;
937 for (i
= 0; i
< map
->n
; ++i
) {
938 has_rational
= isl_basic_map_has_rational(map
->p
[i
]);
939 if (has_rational
< 0 || has_rational
)
942 return isl_bool_false
;
945 /* Does "set" contain any rational points?
947 isl_bool
isl_set_has_rational(__isl_keep isl_set
*set
)
949 return isl_map_has_rational(set
);
952 /* Is this basic set a parameter domain?
954 isl_bool
isl_basic_set_is_params(__isl_keep isl_basic_set
*bset
)
957 return isl_bool_error
;
958 return isl_space_is_params(bset
->dim
);
961 /* Is this set a parameter domain?
963 isl_bool
isl_set_is_params(__isl_keep isl_set
*set
)
966 return isl_bool_error
;
967 return isl_space_is_params(set
->dim
);
970 /* Is this map actually a parameter domain?
971 * Users should never call this function. Outside of isl,
972 * a map can never be a parameter domain.
974 isl_bool
isl_map_is_params(__isl_keep isl_map
*map
)
977 return isl_bool_error
;
978 return isl_space_is_params(map
->dim
);
981 static struct isl_basic_map
*basic_map_init(struct isl_ctx
*ctx
,
982 struct isl_basic_map
*bmap
, unsigned extra
,
983 unsigned n_eq
, unsigned n_ineq
)
986 size_t row_size
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + extra
;
991 bmap
->block
= isl_blk_alloc(ctx
, (n_ineq
+ n_eq
) * row_size
);
992 if (isl_blk_is_error(bmap
->block
))
995 bmap
->ineq
= isl_alloc_array(ctx
, isl_int
*, n_ineq
+ n_eq
);
996 if ((n_ineq
+ n_eq
) && !bmap
->ineq
)
1000 bmap
->block2
= isl_blk_empty();
1003 bmap
->block2
= isl_blk_alloc(ctx
, extra
* (1 + row_size
));
1004 if (isl_blk_is_error(bmap
->block2
))
1007 bmap
->div
= isl_alloc_array(ctx
, isl_int
*, extra
);
1012 for (i
= 0; i
< n_ineq
+ n_eq
; ++i
)
1013 bmap
->ineq
[i
] = bmap
->block
.data
+ i
* row_size
;
1015 for (i
= 0; i
< extra
; ++i
)
1016 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + row_size
);
1020 bmap
->c_size
= n_eq
+ n_ineq
;
1021 bmap
->eq
= bmap
->ineq
+ n_ineq
;
1022 bmap
->extra
= extra
;
1026 bmap
->sample
= NULL
;
1030 isl_basic_map_free(bmap
);
1034 struct isl_basic_set
*isl_basic_set_alloc(struct isl_ctx
*ctx
,
1035 unsigned nparam
, unsigned dim
, unsigned extra
,
1036 unsigned n_eq
, unsigned n_ineq
)
1038 struct isl_basic_map
*bmap
;
1041 space
= isl_space_set_alloc(ctx
, nparam
, dim
);
1045 bmap
= isl_basic_map_alloc_space(space
, extra
, n_eq
, n_ineq
);
1046 return bset_from_bmap(bmap
);
1049 struct isl_basic_set
*isl_basic_set_alloc_space(__isl_take isl_space
*dim
,
1050 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
1052 struct isl_basic_map
*bmap
;
1055 isl_assert(dim
->ctx
, dim
->n_in
== 0, goto error
);
1056 bmap
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
1057 return bset_from_bmap(bmap
);
1059 isl_space_free(dim
);
1063 struct isl_basic_map
*isl_basic_map_alloc_space(__isl_take isl_space
*dim
,
1064 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
1066 struct isl_basic_map
*bmap
;
1070 bmap
= isl_calloc_type(dim
->ctx
, struct isl_basic_map
);
1075 return basic_map_init(dim
->ctx
, bmap
, extra
, n_eq
, n_ineq
);
1077 isl_space_free(dim
);
1081 struct isl_basic_map
*isl_basic_map_alloc(struct isl_ctx
*ctx
,
1082 unsigned nparam
, unsigned in
, unsigned out
, unsigned extra
,
1083 unsigned n_eq
, unsigned n_ineq
)
1085 struct isl_basic_map
*bmap
;
1088 dim
= isl_space_alloc(ctx
, nparam
, in
, out
);
1092 bmap
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
1096 static void dup_constraints(
1097 struct isl_basic_map
*dst
, struct isl_basic_map
*src
)
1100 unsigned total
= isl_basic_map_total_dim(src
);
1102 for (i
= 0; i
< src
->n_eq
; ++i
) {
1103 int j
= isl_basic_map_alloc_equality(dst
);
1104 isl_seq_cpy(dst
->eq
[j
], src
->eq
[i
], 1+total
);
1107 for (i
= 0; i
< src
->n_ineq
; ++i
) {
1108 int j
= isl_basic_map_alloc_inequality(dst
);
1109 isl_seq_cpy(dst
->ineq
[j
], src
->ineq
[i
], 1+total
);
1112 for (i
= 0; i
< src
->n_div
; ++i
) {
1113 int j
= isl_basic_map_alloc_div(dst
);
1114 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
);
1116 ISL_F_SET(dst
, ISL_BASIC_SET_FINAL
);
1119 struct isl_basic_map
*isl_basic_map_dup(struct isl_basic_map
*bmap
)
1121 struct isl_basic_map
*dup
;
1125 dup
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
1126 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
1129 dup_constraints(dup
, bmap
);
1130 dup
->flags
= bmap
->flags
;
1131 dup
->sample
= isl_vec_copy(bmap
->sample
);
1135 struct isl_basic_set
*isl_basic_set_dup(struct isl_basic_set
*bset
)
1137 struct isl_basic_map
*dup
;
1139 dup
= isl_basic_map_dup(bset_to_bmap(bset
));
1140 return bset_from_bmap(dup
);
1143 struct isl_basic_set
*isl_basic_set_copy(struct isl_basic_set
*bset
)
1148 if (ISL_F_ISSET(bset
, ISL_BASIC_SET_FINAL
)) {
1152 return isl_basic_set_dup(bset
);
1155 struct isl_set
*isl_set_copy(struct isl_set
*set
)
1164 struct isl_basic_map
*isl_basic_map_copy(struct isl_basic_map
*bmap
)
1169 if (ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
)) {
1173 bmap
= isl_basic_map_dup(bmap
);
1175 ISL_F_SET(bmap
, ISL_BASIC_SET_FINAL
);
1179 struct isl_map
*isl_map_copy(struct isl_map
*map
)
1188 __isl_null isl_basic_map
*isl_basic_map_free(__isl_take isl_basic_map
*bmap
)
1193 if (--bmap
->ref
> 0)
1196 isl_ctx_deref(bmap
->ctx
);
1198 isl_blk_free(bmap
->ctx
, bmap
->block2
);
1200 isl_blk_free(bmap
->ctx
, bmap
->block
);
1201 isl_vec_free(bmap
->sample
);
1202 isl_space_free(bmap
->dim
);
1208 __isl_null isl_basic_set
*isl_basic_set_free(__isl_take isl_basic_set
*bset
)
1210 return isl_basic_map_free(bset_to_bmap(bset
));
1213 static int room_for_con(struct isl_basic_map
*bmap
, unsigned n
)
1215 return bmap
->n_eq
+ bmap
->n_ineq
+ n
<= bmap
->c_size
;
1218 __isl_give isl_map
*isl_map_align_params_map_map_and(
1219 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
,
1220 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*map1
,
1221 __isl_take isl_map
*map2
))
1225 if (isl_space_match(map1
->dim
, isl_dim_param
, map2
->dim
, isl_dim_param
))
1226 return fn(map1
, map2
);
1227 if (!isl_space_has_named_params(map1
->dim
) ||
1228 !isl_space_has_named_params(map2
->dim
))
1229 isl_die(map1
->ctx
, isl_error_invalid
,
1230 "unaligned unnamed parameters", goto error
);
1231 map1
= isl_map_align_params(map1
, isl_map_get_space(map2
));
1232 map2
= isl_map_align_params(map2
, isl_map_get_space(map1
));
1233 return fn(map1
, map2
);
1240 isl_bool
isl_map_align_params_map_map_and_test(__isl_keep isl_map
*map1
,
1241 __isl_keep isl_map
*map2
,
1242 isl_bool (*fn
)(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
))
1247 return isl_bool_error
;
1248 if (isl_space_match(map1
->dim
, isl_dim_param
, map2
->dim
, isl_dim_param
))
1249 return fn(map1
, map2
);
1250 if (!isl_space_has_named_params(map1
->dim
) ||
1251 !isl_space_has_named_params(map2
->dim
))
1252 isl_die(map1
->ctx
, isl_error_invalid
,
1253 "unaligned unnamed parameters", return isl_bool_error
);
1254 map1
= isl_map_copy(map1
);
1255 map2
= isl_map_copy(map2
);
1256 map1
= isl_map_align_params(map1
, isl_map_get_space(map2
));
1257 map2
= isl_map_align_params(map2
, isl_map_get_space(map1
));
1264 int isl_basic_map_alloc_equality(struct isl_basic_map
*bmap
)
1266 struct isl_ctx
*ctx
;
1270 isl_assert(ctx
, room_for_con(bmap
, 1), return -1);
1271 isl_assert(ctx
, (bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
<= bmap
->c_size
,
1273 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1274 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1275 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1276 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1277 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1278 if ((bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
== bmap
->c_size
) {
1280 int j
= isl_basic_map_alloc_inequality(bmap
);
1284 bmap
->ineq
[j
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1285 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
1292 isl_seq_clr(bmap
->eq
[bmap
->n_eq
] + 1 + isl_basic_map_total_dim(bmap
),
1293 bmap
->extra
- bmap
->n_div
);
1294 return bmap
->n_eq
++;
1297 int isl_basic_set_alloc_equality(struct isl_basic_set
*bset
)
1299 return isl_basic_map_alloc_equality(bset_to_bmap(bset
));
1302 int isl_basic_map_free_equality(struct isl_basic_map
*bmap
, unsigned n
)
1306 isl_assert(bmap
->ctx
, n
<= bmap
->n_eq
, return -1);
1311 int isl_basic_set_free_equality(struct isl_basic_set
*bset
, unsigned n
)
1313 return isl_basic_map_free_equality(bset_to_bmap(bset
), n
);
1316 int isl_basic_map_drop_equality(struct isl_basic_map
*bmap
, unsigned pos
)
1321 isl_assert(bmap
->ctx
, pos
< bmap
->n_eq
, return -1);
1323 if (pos
!= bmap
->n_eq
- 1) {
1325 bmap
->eq
[pos
] = bmap
->eq
[bmap
->n_eq
- 1];
1326 bmap
->eq
[bmap
->n_eq
- 1] = t
;
1332 /* Turn inequality "pos" of "bmap" into an equality.
1334 * In particular, we move the inequality in front of the equalities
1335 * and move the last inequality in the position of the moved inequality.
1336 * Note that isl_tab_make_equalities_explicit depends on this particular
1337 * change in the ordering of the constraints.
1339 void isl_basic_map_inequality_to_equality(
1340 struct isl_basic_map
*bmap
, unsigned pos
)
1344 t
= bmap
->ineq
[pos
];
1345 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1346 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
1351 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1352 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1353 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1354 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1357 static int room_for_ineq(struct isl_basic_map
*bmap
, unsigned n
)
1359 return bmap
->n_ineq
+ n
<= bmap
->eq
- bmap
->ineq
;
1362 int isl_basic_map_alloc_inequality(struct isl_basic_map
*bmap
)
1364 struct isl_ctx
*ctx
;
1368 isl_assert(ctx
, room_for_ineq(bmap
, 1), return -1);
1369 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1370 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1371 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1372 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1373 isl_seq_clr(bmap
->ineq
[bmap
->n_ineq
] +
1374 1 + isl_basic_map_total_dim(bmap
),
1375 bmap
->extra
- bmap
->n_div
);
1376 return bmap
->n_ineq
++;
1379 int isl_basic_set_alloc_inequality(struct isl_basic_set
*bset
)
1381 return isl_basic_map_alloc_inequality(bset_to_bmap(bset
));
1384 int isl_basic_map_free_inequality(struct isl_basic_map
*bmap
, unsigned n
)
1388 isl_assert(bmap
->ctx
, n
<= bmap
->n_ineq
, return -1);
1393 int isl_basic_set_free_inequality(struct isl_basic_set
*bset
, unsigned n
)
1395 return isl_basic_map_free_inequality(bset_to_bmap(bset
), n
);
1398 int isl_basic_map_drop_inequality(struct isl_basic_map
*bmap
, unsigned pos
)
1403 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
1405 if (pos
!= bmap
->n_ineq
- 1) {
1406 t
= bmap
->ineq
[pos
];
1407 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1408 bmap
->ineq
[bmap
->n_ineq
- 1] = t
;
1409 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1415 int isl_basic_set_drop_inequality(struct isl_basic_set
*bset
, unsigned pos
)
1417 return isl_basic_map_drop_inequality(bset_to_bmap(bset
), pos
);
1420 __isl_give isl_basic_map
*isl_basic_map_add_eq(__isl_take isl_basic_map
*bmap
,
1425 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
1428 k
= isl_basic_map_alloc_equality(bmap
);
1431 isl_seq_cpy(bmap
->eq
[k
], eq
, 1 + isl_basic_map_total_dim(bmap
));
1434 isl_basic_map_free(bmap
);
1438 __isl_give isl_basic_set
*isl_basic_set_add_eq(__isl_take isl_basic_set
*bset
,
1441 return bset_from_bmap(isl_basic_map_add_eq(bset_to_bmap(bset
), eq
));
1444 __isl_give isl_basic_map
*isl_basic_map_add_ineq(__isl_take isl_basic_map
*bmap
,
1449 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
1452 k
= isl_basic_map_alloc_inequality(bmap
);
1455 isl_seq_cpy(bmap
->ineq
[k
], ineq
, 1 + isl_basic_map_total_dim(bmap
));
1458 isl_basic_map_free(bmap
);
1462 __isl_give isl_basic_set
*isl_basic_set_add_ineq(__isl_take isl_basic_set
*bset
,
1465 return bset_from_bmap(isl_basic_map_add_ineq(bset_to_bmap(bset
), ineq
));
1468 int isl_basic_map_alloc_div(struct isl_basic_map
*bmap
)
1472 isl_assert(bmap
->ctx
, bmap
->n_div
< bmap
->extra
, return -1);
1473 isl_seq_clr(bmap
->div
[bmap
->n_div
] +
1474 1 + 1 + isl_basic_map_total_dim(bmap
),
1475 bmap
->extra
- bmap
->n_div
);
1476 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1477 return bmap
->n_div
++;
1480 int isl_basic_set_alloc_div(struct isl_basic_set
*bset
)
1482 return isl_basic_map_alloc_div(bset_to_bmap(bset
));
1485 /* Check that there are "n" dimensions of type "type" starting at "first"
1488 static isl_stat
isl_basic_map_check_range(__isl_keep isl_basic_map
*bmap
,
1489 enum isl_dim_type type
, unsigned first
, unsigned n
)
1494 return isl_stat_error
;
1495 dim
= isl_basic_map_dim(bmap
, type
);
1496 if (first
+ n
> dim
|| first
+ n
< first
)
1497 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
1498 "position or range out of bounds",
1499 return isl_stat_error
);
1503 /* Insert an extra integer division, prescribed by "div", to "bmap"
1504 * at (integer division) position "pos".
1506 * The integer division is first added at the end and then moved
1507 * into the right position.
1509 __isl_give isl_basic_map
*isl_basic_map_insert_div(
1510 __isl_take isl_basic_map
*bmap
, int pos
, __isl_keep isl_vec
*div
)
1514 bmap
= isl_basic_map_cow(bmap
);
1516 return isl_basic_map_free(bmap
);
1518 if (div
->size
!= 1 + 1 + isl_basic_map_dim(bmap
, isl_dim_all
))
1519 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
1520 "unexpected size", return isl_basic_map_free(bmap
));
1521 if (isl_basic_map_check_range(bmap
, isl_dim_div
, pos
, 0) < 0)
1522 return isl_basic_map_free(bmap
);
1524 bmap
= isl_basic_map_extend_space(bmap
,
1525 isl_basic_map_get_space(bmap
), 1, 0, 2);
1526 k
= isl_basic_map_alloc_div(bmap
);
1528 return isl_basic_map_free(bmap
);
1529 isl_seq_cpy(bmap
->div
[k
], div
->el
, div
->size
);
1530 isl_int_set_si(bmap
->div
[k
][div
->size
], 0);
1532 for (i
= k
; i
> pos
; --i
)
1533 isl_basic_map_swap_div(bmap
, i
, i
- 1);
1538 isl_stat
isl_basic_map_free_div(struct isl_basic_map
*bmap
, unsigned n
)
1541 return isl_stat_error
;
1542 isl_assert(bmap
->ctx
, n
<= bmap
->n_div
, return isl_stat_error
);
1547 /* Copy constraint from src to dst, putting the vars of src at offset
1548 * dim_off in dst and the divs of src at offset div_off in dst.
1549 * If both sets are actually map, then dim_off applies to the input
1552 static void copy_constraint(struct isl_basic_map
*dst_map
, isl_int
*dst
,
1553 struct isl_basic_map
*src_map
, isl_int
*src
,
1554 unsigned in_off
, unsigned out_off
, unsigned div_off
)
1556 unsigned src_nparam
= isl_basic_map_n_param(src_map
);
1557 unsigned dst_nparam
= isl_basic_map_n_param(dst_map
);
1558 unsigned src_in
= isl_basic_map_n_in(src_map
);
1559 unsigned dst_in
= isl_basic_map_n_in(dst_map
);
1560 unsigned src_out
= isl_basic_map_n_out(src_map
);
1561 unsigned dst_out
= isl_basic_map_n_out(dst_map
);
1562 isl_int_set(dst
[0], src
[0]);
1563 isl_seq_cpy(dst
+1, src
+1, isl_min(dst_nparam
, src_nparam
));
1564 if (dst_nparam
> src_nparam
)
1565 isl_seq_clr(dst
+1+src_nparam
,
1566 dst_nparam
- src_nparam
);
1567 isl_seq_clr(dst
+1+dst_nparam
, in_off
);
1568 isl_seq_cpy(dst
+1+dst_nparam
+in_off
,
1570 isl_min(dst_in
-in_off
, src_in
));
1571 if (dst_in
-in_off
> src_in
)
1572 isl_seq_clr(dst
+1+dst_nparam
+in_off
+src_in
,
1573 dst_in
- in_off
- src_in
);
1574 isl_seq_clr(dst
+1+dst_nparam
+dst_in
, out_off
);
1575 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+out_off
,
1576 src
+1+src_nparam
+src_in
,
1577 isl_min(dst_out
-out_off
, src_out
));
1578 if (dst_out
-out_off
> src_out
)
1579 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+out_off
+src_out
,
1580 dst_out
- out_off
- src_out
);
1581 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
, div_off
);
1582 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+dst_out
+div_off
,
1583 src
+1+src_nparam
+src_in
+src_out
,
1584 isl_min(dst_map
->extra
-div_off
, src_map
->n_div
));
1585 if (dst_map
->n_div
-div_off
> src_map
->n_div
)
1586 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
+
1587 div_off
+src_map
->n_div
,
1588 dst_map
->n_div
- div_off
- src_map
->n_div
);
1591 static void copy_div(struct isl_basic_map
*dst_map
, isl_int
*dst
,
1592 struct isl_basic_map
*src_map
, isl_int
*src
,
1593 unsigned in_off
, unsigned out_off
, unsigned div_off
)
1595 isl_int_set(dst
[0], src
[0]);
1596 copy_constraint(dst_map
, dst
+1, src_map
, src
+1, in_off
, out_off
, div_off
);
1599 static struct isl_basic_map
*add_constraints(struct isl_basic_map
*bmap1
,
1600 struct isl_basic_map
*bmap2
, unsigned i_pos
, unsigned o_pos
)
1605 if (!bmap1
|| !bmap2
)
1608 div_off
= bmap1
->n_div
;
1610 for (i
= 0; i
< bmap2
->n_eq
; ++i
) {
1611 int i1
= isl_basic_map_alloc_equality(bmap1
);
1614 copy_constraint(bmap1
, bmap1
->eq
[i1
], bmap2
, bmap2
->eq
[i
],
1615 i_pos
, o_pos
, div_off
);
1618 for (i
= 0; i
< bmap2
->n_ineq
; ++i
) {
1619 int i1
= isl_basic_map_alloc_inequality(bmap1
);
1622 copy_constraint(bmap1
, bmap1
->ineq
[i1
], bmap2
, bmap2
->ineq
[i
],
1623 i_pos
, o_pos
, div_off
);
1626 for (i
= 0; i
< bmap2
->n_div
; ++i
) {
1627 int i1
= isl_basic_map_alloc_div(bmap1
);
1630 copy_div(bmap1
, bmap1
->div
[i1
], bmap2
, bmap2
->div
[i
],
1631 i_pos
, o_pos
, div_off
);
1634 isl_basic_map_free(bmap2
);
1639 isl_basic_map_free(bmap1
);
1640 isl_basic_map_free(bmap2
);
1644 struct isl_basic_set
*isl_basic_set_add_constraints(struct isl_basic_set
*bset1
,
1645 struct isl_basic_set
*bset2
, unsigned pos
)
1647 return bset_from_bmap(add_constraints(bset_to_bmap(bset1
),
1648 bset_to_bmap(bset2
), 0, pos
));
1651 struct isl_basic_map
*isl_basic_map_extend_space(struct isl_basic_map
*base
,
1652 __isl_take isl_space
*dim
, unsigned extra
,
1653 unsigned n_eq
, unsigned n_ineq
)
1655 struct isl_basic_map
*ext
;
1665 dims_ok
= isl_space_is_equal(base
->dim
, dim
) &&
1666 base
->extra
>= base
->n_div
+ extra
;
1668 if (dims_ok
&& room_for_con(base
, n_eq
+ n_ineq
) &&
1669 room_for_ineq(base
, n_ineq
)) {
1670 isl_space_free(dim
);
1674 isl_assert(base
->ctx
, base
->dim
->nparam
<= dim
->nparam
, goto error
);
1675 isl_assert(base
->ctx
, base
->dim
->n_in
<= dim
->n_in
, goto error
);
1676 isl_assert(base
->ctx
, base
->dim
->n_out
<= dim
->n_out
, goto error
);
1677 extra
+= base
->extra
;
1679 n_ineq
+= base
->n_ineq
;
1681 ext
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
1687 ext
->sample
= isl_vec_copy(base
->sample
);
1688 flags
= base
->flags
;
1689 ext
= add_constraints(ext
, base
, 0, 0);
1692 ISL_F_CLR(ext
, ISL_BASIC_SET_FINAL
);
1698 isl_space_free(dim
);
1699 isl_basic_map_free(base
);
1703 struct isl_basic_set
*isl_basic_set_extend_space(struct isl_basic_set
*base
,
1704 __isl_take isl_space
*dim
, unsigned extra
,
1705 unsigned n_eq
, unsigned n_ineq
)
1707 return bset_from_bmap(isl_basic_map_extend_space(bset_to_bmap(base
),
1708 dim
, extra
, n_eq
, n_ineq
));
1711 struct isl_basic_map
*isl_basic_map_extend_constraints(
1712 struct isl_basic_map
*base
, unsigned n_eq
, unsigned n_ineq
)
1716 return isl_basic_map_extend_space(base
, isl_space_copy(base
->dim
),
1720 struct isl_basic_map
*isl_basic_map_extend(struct isl_basic_map
*base
,
1721 unsigned nparam
, unsigned n_in
, unsigned n_out
, unsigned extra
,
1722 unsigned n_eq
, unsigned n_ineq
)
1724 struct isl_basic_map
*bmap
;
1729 dim
= isl_space_alloc(base
->ctx
, nparam
, n_in
, n_out
);
1733 bmap
= isl_basic_map_extend_space(base
, dim
, extra
, n_eq
, n_ineq
);
1736 isl_basic_map_free(base
);
1740 struct isl_basic_set
*isl_basic_set_extend(struct isl_basic_set
*base
,
1741 unsigned nparam
, unsigned dim
, unsigned extra
,
1742 unsigned n_eq
, unsigned n_ineq
)
1744 return bset_from_bmap(isl_basic_map_extend(bset_to_bmap(base
),
1745 nparam
, 0, dim
, extra
, n_eq
, n_ineq
));
1748 struct isl_basic_set
*isl_basic_set_extend_constraints(
1749 struct isl_basic_set
*base
, unsigned n_eq
, unsigned n_ineq
)
1751 isl_basic_map
*bmap
= bset_to_bmap(base
);
1752 bmap
= isl_basic_map_extend_constraints(bmap
, n_eq
, n_ineq
);
1753 return bset_from_bmap(bmap
);
1756 struct isl_basic_set
*isl_basic_set_cow(struct isl_basic_set
*bset
)
1758 return bset_from_bmap(isl_basic_map_cow(bset_to_bmap(bset
)));
1761 struct isl_basic_map
*isl_basic_map_cow(struct isl_basic_map
*bmap
)
1766 if (bmap
->ref
> 1) {
1768 bmap
= isl_basic_map_dup(bmap
);
1771 ISL_F_CLR(bmap
, ISL_BASIC_SET_FINAL
);
1772 ISL_F_CLR(bmap
, ISL_BASIC_MAP_REDUCED_COEFFICIENTS
);
1777 /* Clear all cached information in "map", either because it is about
1778 * to be modified or because it is being freed.
1779 * Always return the same pointer that is passed in.
1780 * This is needed for the use in isl_map_free.
1782 static __isl_give isl_map
*clear_caches(__isl_take isl_map
*map
)
1784 isl_basic_map_free(map
->cached_simple_hull
[0]);
1785 isl_basic_map_free(map
->cached_simple_hull
[1]);
1786 map
->cached_simple_hull
[0] = NULL
;
1787 map
->cached_simple_hull
[1] = NULL
;
1791 struct isl_set
*isl_set_cow(struct isl_set
*set
)
1793 return isl_map_cow(set
);
1796 /* Return an isl_map that is equal to "map" and that has only
1797 * a single reference.
1799 * If the original input already has only one reference, then
1800 * simply return it, but clear all cached information, since
1801 * it may be rendered invalid by the operations that will be
1802 * performed on the result.
1804 * Otherwise, create a duplicate (without any cached information).
1806 struct isl_map
*isl_map_cow(struct isl_map
*map
)
1812 return clear_caches(map
);
1814 return isl_map_dup(map
);
1817 static void swap_vars(struct isl_blk blk
, isl_int
*a
,
1818 unsigned a_len
, unsigned b_len
)
1820 isl_seq_cpy(blk
.data
, a
+a_len
, b_len
);
1821 isl_seq_cpy(blk
.data
+b_len
, a
, a_len
);
1822 isl_seq_cpy(a
, blk
.data
, b_len
+a_len
);
1825 static __isl_give isl_basic_map
*isl_basic_map_swap_vars(
1826 __isl_take isl_basic_map
*bmap
, unsigned pos
, unsigned n1
, unsigned n2
)
1834 isl_assert(bmap
->ctx
,
1835 pos
+ n1
+ n2
<= 1 + isl_basic_map_total_dim(bmap
), goto error
);
1837 if (n1
== 0 || n2
== 0)
1840 bmap
= isl_basic_map_cow(bmap
);
1844 blk
= isl_blk_alloc(bmap
->ctx
, n1
+ n2
);
1845 if (isl_blk_is_error(blk
))
1848 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1850 bmap
->eq
[i
] + pos
, n1
, n2
);
1852 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1854 bmap
->ineq
[i
] + pos
, n1
, n2
);
1856 for (i
= 0; i
< bmap
->n_div
; ++i
)
1858 bmap
->div
[i
]+1 + pos
, n1
, n2
);
1860 isl_blk_free(bmap
->ctx
, blk
);
1862 ISL_F_CLR(bmap
, ISL_BASIC_SET_NORMALIZED
);
1863 bmap
= isl_basic_map_gauss(bmap
, NULL
);
1864 return isl_basic_map_finalize(bmap
);
1866 isl_basic_map_free(bmap
);
1870 struct isl_basic_map
*isl_basic_map_set_to_empty(struct isl_basic_map
*bmap
)
1876 total
= isl_basic_map_total_dim(bmap
);
1877 if (isl_basic_map_free_div(bmap
, bmap
->n_div
) < 0)
1878 return isl_basic_map_free(bmap
);
1879 isl_basic_map_free_inequality(bmap
, bmap
->n_ineq
);
1881 isl_basic_map_free_equality(bmap
, bmap
->n_eq
-1);
1883 i
= isl_basic_map_alloc_equality(bmap
);
1887 isl_int_set_si(bmap
->eq
[i
][0], 1);
1888 isl_seq_clr(bmap
->eq
[i
]+1, total
);
1889 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
1890 isl_vec_free(bmap
->sample
);
1891 bmap
->sample
= NULL
;
1892 return isl_basic_map_finalize(bmap
);
1894 isl_basic_map_free(bmap
);
1898 struct isl_basic_set
*isl_basic_set_set_to_empty(struct isl_basic_set
*bset
)
1900 return bset_from_bmap(isl_basic_map_set_to_empty(bset_to_bmap(bset
)));
1903 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
1906 static void swap_div(__isl_keep isl_basic_map
*bmap
, int a
, int b
)
1908 isl_int
*t
= bmap
->div
[a
];
1909 bmap
->div
[a
] = bmap
->div
[b
];
1913 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
1914 * div definitions accordingly.
1916 void isl_basic_map_swap_div(struct isl_basic_map
*bmap
, int a
, int b
)
1919 unsigned off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
1921 swap_div(bmap
, a
, b
);
1923 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1924 isl_int_swap(bmap
->eq
[i
][1+off
+a
], bmap
->eq
[i
][1+off
+b
]);
1926 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1927 isl_int_swap(bmap
->ineq
[i
][1+off
+a
], bmap
->ineq
[i
][1+off
+b
]);
1929 for (i
= 0; i
< bmap
->n_div
; ++i
)
1930 isl_int_swap(bmap
->div
[i
][1+1+off
+a
], bmap
->div
[i
][1+1+off
+b
]);
1931 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1934 /* Swap divs "a" and "b" in "bset" and adjust the constraints and
1935 * div definitions accordingly.
1937 void isl_basic_set_swap_div(__isl_keep isl_basic_set
*bset
, int a
, int b
)
1939 isl_basic_map_swap_div(bset
, a
, b
);
1942 /* Eliminate the specified n dimensions starting at first from the
1943 * constraints, without removing the dimensions from the space.
1944 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1946 __isl_give isl_map
*isl_map_eliminate(__isl_take isl_map
*map
,
1947 enum isl_dim_type type
, unsigned first
, unsigned n
)
1956 if (first
+ n
> isl_map_dim(map
, type
) || first
+ n
< first
)
1957 isl_die(map
->ctx
, isl_error_invalid
,
1958 "index out of bounds", goto error
);
1960 map
= isl_map_cow(map
);
1964 for (i
= 0; i
< map
->n
; ++i
) {
1965 map
->p
[i
] = isl_basic_map_eliminate(map
->p
[i
], type
, first
, n
);
1975 /* Eliminate the specified n dimensions starting at first from the
1976 * constraints, without removing the dimensions from the space.
1977 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1979 __isl_give isl_set
*isl_set_eliminate(__isl_take isl_set
*set
,
1980 enum isl_dim_type type
, unsigned first
, unsigned n
)
1982 return set_from_map(isl_map_eliminate(set_to_map(set
), type
, first
, n
));
1985 /* Eliminate the specified n dimensions starting at first from the
1986 * constraints, without removing the dimensions from the space.
1987 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1989 __isl_give isl_set
*isl_set_eliminate_dims(__isl_take isl_set
*set
,
1990 unsigned first
, unsigned n
)
1992 return isl_set_eliminate(set
, isl_dim_set
, first
, n
);
1995 __isl_give isl_basic_map
*isl_basic_map_remove_divs(
1996 __isl_take isl_basic_map
*bmap
)
2000 bmap
= isl_basic_map_eliminate_vars(bmap
,
2001 isl_space_dim(bmap
->dim
, isl_dim_all
), bmap
->n_div
);
2005 return isl_basic_map_finalize(bmap
);
2008 __isl_give isl_basic_set
*isl_basic_set_remove_divs(
2009 __isl_take isl_basic_set
*bset
)
2011 return bset_from_bmap(isl_basic_map_remove_divs(bset_to_bmap(bset
)));
2014 __isl_give isl_map
*isl_map_remove_divs(__isl_take isl_map
*map
)
2023 map
= isl_map_cow(map
);
2027 for (i
= 0; i
< map
->n
; ++i
) {
2028 map
->p
[i
] = isl_basic_map_remove_divs(map
->p
[i
]);
2038 __isl_give isl_set
*isl_set_remove_divs(__isl_take isl_set
*set
)
2040 return isl_map_remove_divs(set
);
2043 struct isl_basic_map
*isl_basic_map_remove_dims(struct isl_basic_map
*bmap
,
2044 enum isl_dim_type type
, unsigned first
, unsigned n
)
2046 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2047 return isl_basic_map_free(bmap
);
2048 if (n
== 0 && !isl_space_is_named_or_nested(bmap
->dim
, type
))
2050 bmap
= isl_basic_map_eliminate_vars(bmap
,
2051 isl_basic_map_offset(bmap
, type
) - 1 + first
, n
);
2054 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
) && type
== isl_dim_div
)
2056 bmap
= isl_basic_map_drop(bmap
, type
, first
, n
);
2060 /* Return true if the definition of the given div (recursively) involves
2061 * any of the given variables.
2063 static isl_bool
div_involves_vars(__isl_keep isl_basic_map
*bmap
, int div
,
2064 unsigned first
, unsigned n
)
2067 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
2069 if (isl_int_is_zero(bmap
->div
[div
][0]))
2070 return isl_bool_false
;
2071 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + first
, n
) >= 0)
2072 return isl_bool_true
;
2074 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2077 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
2079 involves
= div_involves_vars(bmap
, i
, first
, n
);
2080 if (involves
< 0 || involves
)
2084 return isl_bool_false
;
2087 /* Try and add a lower and/or upper bound on "div" to "bmap"
2088 * based on inequality "i".
2089 * "total" is the total number of variables (excluding the divs).
2090 * "v" is a temporary object that can be used during the calculations.
2091 * If "lb" is set, then a lower bound should be constructed.
2092 * If "ub" is set, then an upper bound should be constructed.
2094 * The calling function has already checked that the inequality does not
2095 * reference "div", but we still need to check that the inequality is
2096 * of the right form. We'll consider the case where we want to construct
2097 * a lower bound. The construction of upper bounds is similar.
2099 * Let "div" be of the form
2101 * q = floor((a + f(x))/d)
2103 * We essentially check if constraint "i" is of the form
2107 * so that we can use it to derive a lower bound on "div".
2108 * However, we allow a slightly more general form
2112 * with the condition that the coefficients of g(x) - f(x) are all
2114 * Rewriting this constraint as
2118 * adding a + f(x) to both sides and dividing by d, we obtain
2120 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
2122 * Taking the floor on both sides, we obtain
2124 * q >= floor((a-b)/d) + (f(x)-g(x))/d
2128 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
2130 * In the case of an upper bound, we construct the constraint
2132 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
2135 static __isl_give isl_basic_map
*insert_bounds_on_div_from_ineq(
2136 __isl_take isl_basic_map
*bmap
, int div
, int i
,
2137 unsigned total
, isl_int v
, int lb
, int ub
)
2141 for (j
= 0; (lb
|| ub
) && j
< total
+ bmap
->n_div
; ++j
) {
2143 isl_int_sub(v
, bmap
->ineq
[i
][1 + j
],
2144 bmap
->div
[div
][1 + 1 + j
]);
2145 lb
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
2148 isl_int_add(v
, bmap
->ineq
[i
][1 + j
],
2149 bmap
->div
[div
][1 + 1 + j
]);
2150 ub
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
2156 bmap
= isl_basic_map_cow(bmap
);
2157 bmap
= isl_basic_map_extend_constraints(bmap
, 0, lb
+ ub
);
2159 int k
= isl_basic_map_alloc_inequality(bmap
);
2162 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
2163 isl_int_sub(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
2164 bmap
->div
[div
][1 + j
]);
2165 isl_int_cdiv_q(bmap
->ineq
[k
][j
],
2166 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
2168 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], 1);
2171 int k
= isl_basic_map_alloc_inequality(bmap
);
2174 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
2175 isl_int_add(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
2176 bmap
->div
[div
][1 + j
]);
2177 isl_int_fdiv_q(bmap
->ineq
[k
][j
],
2178 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
2180 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], -1);
2183 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
2186 isl_basic_map_free(bmap
);
2190 /* This function is called right before "div" is eliminated from "bmap"
2191 * using Fourier-Motzkin.
2192 * Look through the constraints of "bmap" for constraints on the argument
2193 * of the integer division and use them to construct constraints on the
2194 * integer division itself. These constraints can then be combined
2195 * during the Fourier-Motzkin elimination.
2196 * Note that it is only useful to introduce lower bounds on "div"
2197 * if "bmap" already contains upper bounds on "div" as the newly
2198 * introduce lower bounds can then be combined with the pre-existing
2199 * upper bounds. Similarly for upper bounds.
2200 * We therefore first check if "bmap" contains any lower and/or upper bounds
2203 * It is interesting to note that the introduction of these constraints
2204 * can indeed lead to more accurate results, even when compared to
2205 * deriving constraints on the argument of "div" from constraints on "div".
2206 * Consider, for example, the set
2208 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2210 * The second constraint can be rewritten as
2212 * 2 * [(-i-2j+3)/4] + k >= 0
2214 * from which we can derive
2216 * -i - 2j + 3 >= -2k
2222 * Combined with the first constraint, we obtain
2224 * -3 <= 3 + 2k or k >= -3
2226 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2227 * the first constraint, we obtain
2229 * [(i + 2j)/4] >= [-3/4] = -1
2231 * Combining this constraint with the second constraint, we obtain
2235 static __isl_give isl_basic_map
*insert_bounds_on_div(
2236 __isl_take isl_basic_map
*bmap
, int div
)
2239 int check_lb
, check_ub
;
2246 if (isl_int_is_zero(bmap
->div
[div
][0]))
2249 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
2253 for (i
= 0; (!check_lb
|| !check_ub
) && i
< bmap
->n_ineq
; ++i
) {
2254 int s
= isl_int_sgn(bmap
->ineq
[i
][1 + total
+ div
]);
2261 if (!check_lb
&& !check_ub
)
2266 for (i
= 0; bmap
&& i
< bmap
->n_ineq
; ++i
) {
2267 if (!isl_int_is_zero(bmap
->ineq
[i
][1 + total
+ div
]))
2270 bmap
= insert_bounds_on_div_from_ineq(bmap
, div
, i
, total
, v
,
2271 check_lb
, check_ub
);
2279 /* Remove all divs (recursively) involving any of the given dimensions
2280 * in their definitions.
2282 __isl_give isl_basic_map
*isl_basic_map_remove_divs_involving_dims(
2283 __isl_take isl_basic_map
*bmap
,
2284 enum isl_dim_type type
, unsigned first
, unsigned n
)
2288 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2289 return isl_basic_map_free(bmap
);
2290 first
+= isl_basic_map_offset(bmap
, type
);
2292 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2295 involves
= div_involves_vars(bmap
, i
, first
, n
);
2297 return isl_basic_map_free(bmap
);
2300 bmap
= insert_bounds_on_div(bmap
, i
);
2301 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
2310 __isl_give isl_basic_set
*isl_basic_set_remove_divs_involving_dims(
2311 __isl_take isl_basic_set
*bset
,
2312 enum isl_dim_type type
, unsigned first
, unsigned n
)
2314 return isl_basic_map_remove_divs_involving_dims(bset
, type
, first
, n
);
2317 __isl_give isl_map
*isl_map_remove_divs_involving_dims(__isl_take isl_map
*map
,
2318 enum isl_dim_type type
, unsigned first
, unsigned n
)
2327 map
= isl_map_cow(map
);
2331 for (i
= 0; i
< map
->n
; ++i
) {
2332 map
->p
[i
] = isl_basic_map_remove_divs_involving_dims(map
->p
[i
],
2343 __isl_give isl_set
*isl_set_remove_divs_involving_dims(__isl_take isl_set
*set
,
2344 enum isl_dim_type type
, unsigned first
, unsigned n
)
2346 return set_from_map(isl_map_remove_divs_involving_dims(set_to_map(set
),
2350 /* Does the description of "bmap" depend on the specified dimensions?
2351 * We also check whether the dimensions appear in any of the div definitions.
2352 * In principle there is no need for this check. If the dimensions appear
2353 * in a div definition, they also appear in the defining constraints of that
2356 isl_bool
isl_basic_map_involves_dims(__isl_keep isl_basic_map
*bmap
,
2357 enum isl_dim_type type
, unsigned first
, unsigned n
)
2361 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2362 return isl_bool_error
;
2364 first
+= isl_basic_map_offset(bmap
, type
);
2365 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2366 if (isl_seq_first_non_zero(bmap
->eq
[i
] + first
, n
) >= 0)
2367 return isl_bool_true
;
2368 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2369 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + first
, n
) >= 0)
2370 return isl_bool_true
;
2371 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2372 if (isl_int_is_zero(bmap
->div
[i
][0]))
2374 if (isl_seq_first_non_zero(bmap
->div
[i
] + 1 + first
, n
) >= 0)
2375 return isl_bool_true
;
2378 return isl_bool_false
;
2381 isl_bool
isl_map_involves_dims(__isl_keep isl_map
*map
,
2382 enum isl_dim_type type
, unsigned first
, unsigned n
)
2387 return isl_bool_error
;
2389 if (first
+ n
> isl_map_dim(map
, type
))
2390 isl_die(map
->ctx
, isl_error_invalid
,
2391 "index out of bounds", return isl_bool_error
);
2393 for (i
= 0; i
< map
->n
; ++i
) {
2394 isl_bool involves
= isl_basic_map_involves_dims(map
->p
[i
],
2396 if (involves
< 0 || involves
)
2400 return isl_bool_false
;
2403 isl_bool
isl_basic_set_involves_dims(__isl_keep isl_basic_set
*bset
,
2404 enum isl_dim_type type
, unsigned first
, unsigned n
)
2406 return isl_basic_map_involves_dims(bset
, type
, first
, n
);
2409 isl_bool
isl_set_involves_dims(__isl_keep isl_set
*set
,
2410 enum isl_dim_type type
, unsigned first
, unsigned n
)
2412 return isl_map_involves_dims(set
, type
, first
, n
);
2415 /* Drop all constraints in bmap that involve any of the dimensions
2416 * first to first+n-1.
2418 static __isl_give isl_basic_map
*isl_basic_map_drop_constraints_involving(
2419 __isl_take isl_basic_map
*bmap
, unsigned first
, unsigned n
)
2426 bmap
= isl_basic_map_cow(bmap
);
2431 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
) {
2432 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + first
, n
) == -1)
2434 isl_basic_map_drop_equality(bmap
, i
);
2437 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
2438 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + 1 + first
, n
) == -1)
2440 isl_basic_map_drop_inequality(bmap
, i
);
2443 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
2447 /* Drop all constraints in bset that involve any of the dimensions
2448 * first to first+n-1.
2450 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_involving(
2451 __isl_take isl_basic_set
*bset
, unsigned first
, unsigned n
)
2453 return isl_basic_map_drop_constraints_involving(bset
, first
, n
);
2456 /* Drop all constraints in bmap that do not involve any of the dimensions
2457 * first to first + n - 1 of the given type.
2459 __isl_give isl_basic_map
*isl_basic_map_drop_constraints_not_involving_dims(
2460 __isl_take isl_basic_map
*bmap
,
2461 enum isl_dim_type type
, unsigned first
, unsigned n
)
2466 isl_space
*space
= isl_basic_map_get_space(bmap
);
2467 isl_basic_map_free(bmap
);
2468 return isl_basic_map_universe(space
);
2470 bmap
= isl_basic_map_cow(bmap
);
2474 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2475 return isl_basic_map_free(bmap
);
2477 first
+= isl_basic_map_offset(bmap
, type
) - 1;
2479 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
) {
2480 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + first
, n
) != -1)
2482 isl_basic_map_drop_equality(bmap
, i
);
2485 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
2486 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + 1 + first
, n
) != -1)
2488 isl_basic_map_drop_inequality(bmap
, i
);
2491 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
2495 /* Drop all constraints in bset that do not involve any of the dimensions
2496 * first to first + n - 1 of the given type.
2498 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_not_involving_dims(
2499 __isl_take isl_basic_set
*bset
,
2500 enum isl_dim_type type
, unsigned first
, unsigned n
)
2502 return isl_basic_map_drop_constraints_not_involving_dims(bset
,
2506 /* Drop all constraints in bmap that involve any of the dimensions
2507 * first to first + n - 1 of the given type.
2509 __isl_give isl_basic_map
*isl_basic_map_drop_constraints_involving_dims(
2510 __isl_take isl_basic_map
*bmap
,
2511 enum isl_dim_type type
, unsigned first
, unsigned n
)
2518 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2519 return isl_basic_map_free(bmap
);
2521 bmap
= isl_basic_map_remove_divs_involving_dims(bmap
, type
, first
, n
);
2522 first
+= isl_basic_map_offset(bmap
, type
) - 1;
2523 return isl_basic_map_drop_constraints_involving(bmap
, first
, n
);
2526 /* Drop all constraints in bset that involve any of the dimensions
2527 * first to first + n - 1 of the given type.
2529 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_involving_dims(
2530 __isl_take isl_basic_set
*bset
,
2531 enum isl_dim_type type
, unsigned first
, unsigned n
)
2533 return isl_basic_map_drop_constraints_involving_dims(bset
,
2537 /* Drop constraints from "map" by applying "drop" to each basic map.
2539 static __isl_give isl_map
*drop_constraints(__isl_take isl_map
*map
,
2540 enum isl_dim_type type
, unsigned first
, unsigned n
,
2541 __isl_give isl_basic_map
*(*drop
)(__isl_take isl_basic_map
*bmap
,
2542 enum isl_dim_type type
, unsigned first
, unsigned n
))
2550 dim
= isl_map_dim(map
, type
);
2551 if (first
+ n
> dim
|| first
+ n
< first
)
2552 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
2553 "index out of bounds", return isl_map_free(map
));
2555 map
= isl_map_cow(map
);
2559 for (i
= 0; i
< map
->n
; ++i
) {
2560 map
->p
[i
] = drop(map
->p
[i
], type
, first
, n
);
2562 return isl_map_free(map
);
2566 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
2571 /* Drop all constraints in map that involve any of the dimensions
2572 * first to first + n - 1 of the given type.
2574 __isl_give isl_map
*isl_map_drop_constraints_involving_dims(
2575 __isl_take isl_map
*map
,
2576 enum isl_dim_type type
, unsigned first
, unsigned n
)
2580 return drop_constraints(map
, type
, first
, n
,
2581 &isl_basic_map_drop_constraints_involving_dims
);
2584 /* Drop all constraints in "map" that do not involve any of the dimensions
2585 * first to first + n - 1 of the given type.
2587 __isl_give isl_map
*isl_map_drop_constraints_not_involving_dims(
2588 __isl_take isl_map
*map
,
2589 enum isl_dim_type type
, unsigned first
, unsigned n
)
2592 isl_space
*space
= isl_map_get_space(map
);
2594 return isl_map_universe(space
);
2596 return drop_constraints(map
, type
, first
, n
,
2597 &isl_basic_map_drop_constraints_not_involving_dims
);
2600 /* Drop all constraints in set that involve any of the dimensions
2601 * first to first + n - 1 of the given type.
2603 __isl_give isl_set
*isl_set_drop_constraints_involving_dims(
2604 __isl_take isl_set
*set
,
2605 enum isl_dim_type type
, unsigned first
, unsigned n
)
2607 return isl_map_drop_constraints_involving_dims(set
, type
, first
, n
);
2610 /* Drop all constraints in "set" that do not involve any of the dimensions
2611 * first to first + n - 1 of the given type.
2613 __isl_give isl_set
*isl_set_drop_constraints_not_involving_dims(
2614 __isl_take isl_set
*set
,
2615 enum isl_dim_type type
, unsigned first
, unsigned n
)
2617 return isl_map_drop_constraints_not_involving_dims(set
, type
, first
, n
);
2620 /* Does local variable "div" of "bmap" have a complete explicit representation?
2621 * Having a complete explicit representation requires not only
2622 * an explicit representation, but also that all local variables
2623 * that appear in this explicit representation in turn have
2624 * a complete explicit representation.
2626 isl_bool
isl_basic_map_div_is_known(__isl_keep isl_basic_map
*bmap
, int div
)
2629 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
2632 marked
= isl_basic_map_div_is_marked_unknown(bmap
, div
);
2633 if (marked
< 0 || marked
)
2634 return isl_bool_not(marked
);
2636 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2639 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
2641 known
= isl_basic_map_div_is_known(bmap
, i
);
2642 if (known
< 0 || !known
)
2646 return isl_bool_true
;
2649 /* Remove all divs that are unknown or defined in terms of unknown divs.
2651 __isl_give isl_basic_map
*isl_basic_map_remove_unknown_divs(
2652 __isl_take isl_basic_map
*bmap
)
2659 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2660 if (isl_basic_map_div_is_known(bmap
, i
))
2662 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
2671 /* Remove all divs that are unknown or defined in terms of unknown divs.
2673 __isl_give isl_basic_set
*isl_basic_set_remove_unknown_divs(
2674 __isl_take isl_basic_set
*bset
)
2676 return isl_basic_map_remove_unknown_divs(bset
);
2679 __isl_give isl_map
*isl_map_remove_unknown_divs(__isl_take isl_map
*map
)
2688 map
= isl_map_cow(map
);
2692 for (i
= 0; i
< map
->n
; ++i
) {
2693 map
->p
[i
] = isl_basic_map_remove_unknown_divs(map
->p
[i
]);
2703 __isl_give isl_set
*isl_set_remove_unknown_divs(__isl_take isl_set
*set
)
2705 return set_from_map(isl_map_remove_unknown_divs(set_to_map(set
)));
2708 __isl_give isl_basic_set
*isl_basic_set_remove_dims(
2709 __isl_take isl_basic_set
*bset
,
2710 enum isl_dim_type type
, unsigned first
, unsigned n
)
2712 isl_basic_map
*bmap
= bset_to_bmap(bset
);
2713 bmap
= isl_basic_map_remove_dims(bmap
, type
, first
, n
);
2714 return bset_from_bmap(bmap
);
2717 struct isl_map
*isl_map_remove_dims(struct isl_map
*map
,
2718 enum isl_dim_type type
, unsigned first
, unsigned n
)
2725 map
= isl_map_cow(map
);
2728 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
2730 for (i
= 0; i
< map
->n
; ++i
) {
2731 map
->p
[i
] = isl_basic_map_eliminate_vars(map
->p
[i
],
2732 isl_basic_map_offset(map
->p
[i
], type
) - 1 + first
, n
);
2736 map
= isl_map_drop(map
, type
, first
, n
);
2743 __isl_give isl_set
*isl_set_remove_dims(__isl_take isl_set
*bset
,
2744 enum isl_dim_type type
, unsigned first
, unsigned n
)
2746 return set_from_map(isl_map_remove_dims(set_to_map(bset
),
2750 /* Project out n inputs starting at first using Fourier-Motzkin */
2751 struct isl_map
*isl_map_remove_inputs(struct isl_map
*map
,
2752 unsigned first
, unsigned n
)
2754 return isl_map_remove_dims(map
, isl_dim_in
, first
, n
);
2757 static void dump_term(struct isl_basic_map
*bmap
,
2758 isl_int c
, int pos
, FILE *out
)
2761 unsigned in
= isl_basic_map_n_in(bmap
);
2762 unsigned dim
= in
+ isl_basic_map_n_out(bmap
);
2763 unsigned nparam
= isl_basic_map_n_param(bmap
);
2765 isl_int_print(out
, c
, 0);
2767 if (!isl_int_is_one(c
))
2768 isl_int_print(out
, c
, 0);
2769 if (pos
< 1 + nparam
) {
2770 name
= isl_space_get_dim_name(bmap
->dim
,
2771 isl_dim_param
, pos
- 1);
2773 fprintf(out
, "%s", name
);
2775 fprintf(out
, "p%d", pos
- 1);
2776 } else if (pos
< 1 + nparam
+ in
)
2777 fprintf(out
, "i%d", pos
- 1 - nparam
);
2778 else if (pos
< 1 + nparam
+ dim
)
2779 fprintf(out
, "o%d", pos
- 1 - nparam
- in
);
2781 fprintf(out
, "e%d", pos
- 1 - nparam
- dim
);
2785 static void dump_constraint_sign(struct isl_basic_map
*bmap
, isl_int
*c
,
2786 int sign
, FILE *out
)
2790 unsigned len
= 1 + isl_basic_map_total_dim(bmap
);
2794 for (i
= 0, first
= 1; i
< len
; ++i
) {
2795 if (isl_int_sgn(c
[i
]) * sign
<= 0)
2798 fprintf(out
, " + ");
2800 isl_int_abs(v
, c
[i
]);
2801 dump_term(bmap
, v
, i
, out
);
2808 static void dump_constraint(struct isl_basic_map
*bmap
, isl_int
*c
,
2809 const char *op
, FILE *out
, int indent
)
2813 fprintf(out
, "%*s", indent
, "");
2815 dump_constraint_sign(bmap
, c
, 1, out
);
2816 fprintf(out
, " %s ", op
);
2817 dump_constraint_sign(bmap
, c
, -1, out
);
2821 for (i
= bmap
->n_div
; i
< bmap
->extra
; ++i
) {
2822 if (isl_int_is_zero(c
[1+isl_space_dim(bmap
->dim
, isl_dim_all
)+i
]))
2824 fprintf(out
, "%*s", indent
, "");
2825 fprintf(out
, "ERROR: unused div coefficient not zero\n");
2830 static void dump_constraints(struct isl_basic_map
*bmap
,
2831 isl_int
**c
, unsigned n
,
2832 const char *op
, FILE *out
, int indent
)
2836 for (i
= 0; i
< n
; ++i
)
2837 dump_constraint(bmap
, c
[i
], op
, out
, indent
);
2840 static void dump_affine(struct isl_basic_map
*bmap
, isl_int
*exp
, FILE *out
)
2844 unsigned total
= isl_basic_map_total_dim(bmap
);
2846 for (j
= 0; j
< 1 + total
; ++j
) {
2847 if (isl_int_is_zero(exp
[j
]))
2849 if (!first
&& isl_int_is_pos(exp
[j
]))
2851 dump_term(bmap
, exp
[j
], j
, out
);
2856 static void dump(struct isl_basic_map
*bmap
, FILE *out
, int indent
)
2860 dump_constraints(bmap
, bmap
->eq
, bmap
->n_eq
, "=", out
, indent
);
2861 dump_constraints(bmap
, bmap
->ineq
, bmap
->n_ineq
, ">=", out
, indent
);
2863 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2864 fprintf(out
, "%*s", indent
, "");
2865 fprintf(out
, "e%d = [(", i
);
2866 dump_affine(bmap
, bmap
->div
[i
]+1, out
);
2868 isl_int_print(out
, bmap
->div
[i
][0], 0);
2869 fprintf(out
, "]\n");
2873 void isl_basic_set_print_internal(struct isl_basic_set
*bset
,
2874 FILE *out
, int indent
)
2877 fprintf(out
, "null basic set\n");
2881 fprintf(out
, "%*s", indent
, "");
2882 fprintf(out
, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
2883 bset
->ref
, bset
->dim
->nparam
, bset
->dim
->n_out
,
2884 bset
->extra
, bset
->flags
);
2885 dump(bset_to_bmap(bset
), out
, indent
);
2888 void isl_basic_map_print_internal(struct isl_basic_map
*bmap
,
2889 FILE *out
, int indent
)
2892 fprintf(out
, "null basic map\n");
2896 fprintf(out
, "%*s", indent
, "");
2897 fprintf(out
, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
2898 "flags: %x, n_name: %d\n",
2900 bmap
->dim
->nparam
, bmap
->dim
->n_in
, bmap
->dim
->n_out
,
2901 bmap
->extra
, bmap
->flags
, bmap
->dim
->n_id
);
2902 dump(bmap
, out
, indent
);
2905 int isl_inequality_negate(struct isl_basic_map
*bmap
, unsigned pos
)
2910 total
= isl_basic_map_total_dim(bmap
);
2911 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
2912 isl_seq_neg(bmap
->ineq
[pos
], bmap
->ineq
[pos
], 1 + total
);
2913 isl_int_sub_ui(bmap
->ineq
[pos
][0], bmap
->ineq
[pos
][0], 1);
2914 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
2918 __isl_give isl_set
*isl_set_alloc_space(__isl_take isl_space
*space
, int n
,
2923 if (isl_space_dim(space
, isl_dim_in
) != 0)
2924 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
2925 "set cannot have input dimensions", goto error
);
2926 return isl_map_alloc_space(space
, n
, flags
);
2928 isl_space_free(space
);
2932 /* Make sure "map" has room for at least "n" more basic maps.
2934 struct isl_map
*isl_map_grow(struct isl_map
*map
, int n
)
2937 struct isl_map
*grown
= NULL
;
2941 isl_assert(map
->ctx
, n
>= 0, goto error
);
2942 if (map
->n
+ n
<= map
->size
)
2944 grown
= isl_map_alloc_space(isl_map_get_space(map
), map
->n
+ n
, map
->flags
);
2947 for (i
= 0; i
< map
->n
; ++i
) {
2948 grown
->p
[i
] = isl_basic_map_copy(map
->p
[i
]);
2956 isl_map_free(grown
);
2961 /* Make sure "set" has room for at least "n" more basic sets.
2963 struct isl_set
*isl_set_grow(struct isl_set
*set
, int n
)
2965 return set_from_map(isl_map_grow(set_to_map(set
), n
));
2968 struct isl_set
*isl_set_from_basic_set(struct isl_basic_set
*bset
)
2970 return isl_map_from_basic_map(bset
);
2973 struct isl_map
*isl_map_from_basic_map(struct isl_basic_map
*bmap
)
2975 struct isl_map
*map
;
2980 map
= isl_map_alloc_space(isl_space_copy(bmap
->dim
), 1, ISL_MAP_DISJOINT
);
2981 return isl_map_add_basic_map(map
, bmap
);
2984 __isl_give isl_set
*isl_set_add_basic_set(__isl_take isl_set
*set
,
2985 __isl_take isl_basic_set
*bset
)
2987 return set_from_map(isl_map_add_basic_map(set_to_map(set
),
2988 bset_to_bmap(bset
)));
2991 __isl_null isl_set
*isl_set_free(__isl_take isl_set
*set
)
2993 return isl_map_free(set
);
2996 void isl_set_print_internal(struct isl_set
*set
, FILE *out
, int indent
)
3001 fprintf(out
, "null set\n");
3005 fprintf(out
, "%*s", indent
, "");
3006 fprintf(out
, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
3007 set
->ref
, set
->n
, set
->dim
->nparam
, set
->dim
->n_out
,
3009 for (i
= 0; i
< set
->n
; ++i
) {
3010 fprintf(out
, "%*s", indent
, "");
3011 fprintf(out
, "basic set %d:\n", i
);
3012 isl_basic_set_print_internal(set
->p
[i
], out
, indent
+4);
3016 void isl_map_print_internal(struct isl_map
*map
, FILE *out
, int indent
)
3021 fprintf(out
, "null map\n");
3025 fprintf(out
, "%*s", indent
, "");
3026 fprintf(out
, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
3027 "flags: %x, n_name: %d\n",
3028 map
->ref
, map
->n
, map
->dim
->nparam
, map
->dim
->n_in
,
3029 map
->dim
->n_out
, map
->flags
, map
->dim
->n_id
);
3030 for (i
= 0; i
< map
->n
; ++i
) {
3031 fprintf(out
, "%*s", indent
, "");
3032 fprintf(out
, "basic map %d:\n", i
);
3033 isl_basic_map_print_internal(map
->p
[i
], out
, indent
+4);
3037 struct isl_basic_map
*isl_basic_map_intersect_domain(
3038 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
3040 struct isl_basic_map
*bmap_domain
;
3045 isl_assert(bset
->ctx
, isl_space_match(bmap
->dim
, isl_dim_param
,
3046 bset
->dim
, isl_dim_param
), goto error
);
3048 if (isl_space_dim(bset
->dim
, isl_dim_set
) != 0)
3049 isl_assert(bset
->ctx
,
3050 isl_basic_map_compatible_domain(bmap
, bset
), goto error
);
3052 bmap
= isl_basic_map_cow(bmap
);
3055 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
3056 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
3057 bmap_domain
= isl_basic_map_from_domain(bset
);
3058 bmap
= add_constraints(bmap
, bmap_domain
, 0, 0);
3060 bmap
= isl_basic_map_simplify(bmap
);
3061 return isl_basic_map_finalize(bmap
);
3063 isl_basic_map_free(bmap
);
3064 isl_basic_set_free(bset
);
3068 /* Check that the space of "bset" is the same as that of the range of "bmap".
3070 static isl_stat
isl_basic_map_check_compatible_range(
3071 __isl_keep isl_basic_map
*bmap
, __isl_keep isl_basic_set
*bset
)
3075 ok
= isl_basic_map_compatible_range(bmap
, bset
);
3077 return isl_stat_error
;
3079 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
3080 "incompatible spaces", return isl_stat_error
);
3085 struct isl_basic_map
*isl_basic_map_intersect_range(
3086 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
3088 struct isl_basic_map
*bmap_range
;
3093 isl_assert(bset
->ctx
, isl_space_match(bmap
->dim
, isl_dim_param
,
3094 bset
->dim
, isl_dim_param
), goto error
);
3096 if (isl_space_dim(bset
->dim
, isl_dim_set
) != 0 &&
3097 isl_basic_map_check_compatible_range(bmap
, bset
) < 0)
3100 if (isl_basic_set_plain_is_universe(bset
)) {
3101 isl_basic_set_free(bset
);
3105 bmap
= isl_basic_map_cow(bmap
);
3108 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
3109 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
3110 bmap_range
= bset_to_bmap(bset
);
3111 bmap
= add_constraints(bmap
, bmap_range
, 0, 0);
3113 bmap
= isl_basic_map_simplify(bmap
);
3114 return isl_basic_map_finalize(bmap
);
3116 isl_basic_map_free(bmap
);
3117 isl_basic_set_free(bset
);
3121 isl_bool
isl_basic_map_contains(__isl_keep isl_basic_map
*bmap
,
3122 __isl_keep isl_vec
*vec
)
3129 return isl_bool_error
;
3131 total
= 1 + isl_basic_map_total_dim(bmap
);
3132 if (total
!= vec
->size
)
3133 return isl_bool_false
;
3137 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
3138 isl_seq_inner_product(vec
->el
, bmap
->eq
[i
], total
, &s
);
3139 if (!isl_int_is_zero(s
)) {
3141 return isl_bool_false
;
3145 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
3146 isl_seq_inner_product(vec
->el
, bmap
->ineq
[i
], total
, &s
);
3147 if (isl_int_is_neg(s
)) {
3149 return isl_bool_false
;
3155 return isl_bool_true
;
3158 isl_bool
isl_basic_set_contains(__isl_keep isl_basic_set
*bset
,
3159 __isl_keep isl_vec
*vec
)
3161 return isl_basic_map_contains(bset_to_bmap(bset
), vec
);
3164 struct isl_basic_map
*isl_basic_map_intersect(
3165 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3167 struct isl_vec
*sample
= NULL
;
3169 if (!bmap1
|| !bmap2
)
3172 isl_assert(bmap1
->ctx
, isl_space_match(bmap1
->dim
, isl_dim_param
,
3173 bmap2
->dim
, isl_dim_param
), goto error
);
3174 if (isl_space_dim(bmap1
->dim
, isl_dim_all
) ==
3175 isl_space_dim(bmap1
->dim
, isl_dim_param
) &&
3176 isl_space_dim(bmap2
->dim
, isl_dim_all
) !=
3177 isl_space_dim(bmap2
->dim
, isl_dim_param
))
3178 return isl_basic_map_intersect(bmap2
, bmap1
);
3180 if (isl_space_dim(bmap2
->dim
, isl_dim_all
) !=
3181 isl_space_dim(bmap2
->dim
, isl_dim_param
))
3182 isl_assert(bmap1
->ctx
,
3183 isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
3185 if (isl_basic_map_plain_is_empty(bmap1
)) {
3186 isl_basic_map_free(bmap2
);
3189 if (isl_basic_map_plain_is_empty(bmap2
)) {
3190 isl_basic_map_free(bmap1
);
3194 if (bmap1
->sample
&&
3195 isl_basic_map_contains(bmap1
, bmap1
->sample
) > 0 &&
3196 isl_basic_map_contains(bmap2
, bmap1
->sample
) > 0)
3197 sample
= isl_vec_copy(bmap1
->sample
);
3198 else if (bmap2
->sample
&&
3199 isl_basic_map_contains(bmap1
, bmap2
->sample
) > 0 &&
3200 isl_basic_map_contains(bmap2
, bmap2
->sample
) > 0)
3201 sample
= isl_vec_copy(bmap2
->sample
);
3203 bmap1
= isl_basic_map_cow(bmap1
);
3206 bmap1
= isl_basic_map_extend_space(bmap1
, isl_space_copy(bmap1
->dim
),
3207 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
3208 bmap1
= add_constraints(bmap1
, bmap2
, 0, 0);
3211 isl_vec_free(sample
);
3213 isl_vec_free(bmap1
->sample
);
3214 bmap1
->sample
= sample
;
3217 bmap1
= isl_basic_map_simplify(bmap1
);
3218 return isl_basic_map_finalize(bmap1
);
3221 isl_vec_free(sample
);
3222 isl_basic_map_free(bmap1
);
3223 isl_basic_map_free(bmap2
);
3227 struct isl_basic_set
*isl_basic_set_intersect(
3228 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
3230 return bset_from_bmap(isl_basic_map_intersect(bset_to_bmap(bset1
),
3231 bset_to_bmap(bset2
)));
3234 __isl_give isl_basic_set
*isl_basic_set_intersect_params(
3235 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
3237 return isl_basic_set_intersect(bset1
, bset2
);
3240 /* Special case of isl_map_intersect, where both map1 and map2
3241 * are convex, without any divs and such that either map1 or map2
3242 * contains a single constraint. This constraint is then simply
3243 * added to the other map.
3245 static __isl_give isl_map
*map_intersect_add_constraint(
3246 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
3248 isl_assert(map1
->ctx
, map1
->n
== 1, goto error
);
3249 isl_assert(map2
->ctx
, map1
->n
== 1, goto error
);
3250 isl_assert(map1
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
3251 isl_assert(map2
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
3253 if (map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
!= 1)
3254 return isl_map_intersect(map2
, map1
);
3256 map1
= isl_map_cow(map1
);
3259 if (isl_map_plain_is_empty(map1
)) {
3263 map1
->p
[0] = isl_basic_map_cow(map1
->p
[0]);
3264 if (map2
->p
[0]->n_eq
== 1)
3265 map1
->p
[0] = isl_basic_map_add_eq(map1
->p
[0], map2
->p
[0]->eq
[0]);
3267 map1
->p
[0] = isl_basic_map_add_ineq(map1
->p
[0],
3268 map2
->p
[0]->ineq
[0]);
3270 map1
->p
[0] = isl_basic_map_simplify(map1
->p
[0]);
3271 map1
->p
[0] = isl_basic_map_finalize(map1
->p
[0]);
3275 if (isl_basic_map_plain_is_empty(map1
->p
[0])) {
3276 isl_basic_map_free(map1
->p
[0]);
3289 /* map2 may be either a parameter domain or a map living in the same
3292 static __isl_give isl_map
*map_intersect_internal(__isl_take isl_map
*map1
,
3293 __isl_take isl_map
*map2
)
3302 if ((isl_map_plain_is_empty(map1
) ||
3303 isl_map_plain_is_universe(map2
)) &&
3304 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
3308 if ((isl_map_plain_is_empty(map2
) ||
3309 isl_map_plain_is_universe(map1
)) &&
3310 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
3315 if (map1
->n
== 1 && map2
->n
== 1 &&
3316 map1
->p
[0]->n_div
== 0 && map2
->p
[0]->n_div
== 0 &&
3317 isl_space_is_equal(map1
->dim
, map2
->dim
) &&
3318 (map1
->p
[0]->n_eq
+ map1
->p
[0]->n_ineq
== 1 ||
3319 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1))
3320 return map_intersect_add_constraint(map1
, map2
);
3322 if (isl_space_dim(map2
->dim
, isl_dim_all
) !=
3323 isl_space_dim(map2
->dim
, isl_dim_param
))
3324 isl_assert(map1
->ctx
,
3325 isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
3327 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
3328 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
3329 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
3331 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
3332 map1
->n
* map2
->n
, flags
);
3335 for (i
= 0; i
< map1
->n
; ++i
)
3336 for (j
= 0; j
< map2
->n
; ++j
) {
3337 struct isl_basic_map
*part
;
3338 part
= isl_basic_map_intersect(
3339 isl_basic_map_copy(map1
->p
[i
]),
3340 isl_basic_map_copy(map2
->p
[j
]));
3341 if (isl_basic_map_is_empty(part
) < 0)
3342 part
= isl_basic_map_free(part
);
3343 result
= isl_map_add_basic_map(result
, part
);
3356 static __isl_give isl_map
*map_intersect(__isl_take isl_map
*map1
,
3357 __isl_take isl_map
*map2
)
3361 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
3362 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
3363 "spaces don't match", goto error
);
3364 return map_intersect_internal(map1
, map2
);
3371 __isl_give isl_map
*isl_map_intersect(__isl_take isl_map
*map1
,
3372 __isl_take isl_map
*map2
)
3374 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect
);
3377 struct isl_set
*isl_set_intersect(struct isl_set
*set1
, struct isl_set
*set2
)
3379 return set_from_map(isl_map_intersect(set_to_map(set1
),
3383 /* map_intersect_internal accepts intersections
3384 * with parameter domains, so we can just call that function.
3386 static __isl_give isl_map
*map_intersect_params(__isl_take isl_map
*map
,
3387 __isl_take isl_set
*params
)
3389 return map_intersect_internal(map
, params
);
3392 __isl_give isl_map
*isl_map_intersect_params(__isl_take isl_map
*map1
,
3393 __isl_take isl_map
*map2
)
3395 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect_params
);
3398 __isl_give isl_set
*isl_set_intersect_params(__isl_take isl_set
*set
,
3399 __isl_take isl_set
*params
)
3401 return isl_map_intersect_params(set
, params
);
3404 struct isl_basic_map
*isl_basic_map_reverse(struct isl_basic_map
*bmap
)
3407 unsigned pos
, n1
, n2
;
3411 bmap
= isl_basic_map_cow(bmap
);
3414 space
= isl_space_reverse(isl_space_copy(bmap
->dim
));
3415 pos
= isl_basic_map_offset(bmap
, isl_dim_in
);
3416 n1
= isl_basic_map_dim(bmap
, isl_dim_in
);
3417 n2
= isl_basic_map_dim(bmap
, isl_dim_out
);
3418 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
3419 return isl_basic_map_reset_space(bmap
, space
);
3422 static __isl_give isl_basic_map
*basic_map_space_reset(
3423 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
)
3429 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
3432 space
= isl_basic_map_get_space(bmap
);
3433 space
= isl_space_reset(space
, type
);
3434 bmap
= isl_basic_map_reset_space(bmap
, space
);
3438 __isl_give isl_basic_map
*isl_basic_map_insert_dims(
3439 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
3440 unsigned pos
, unsigned n
)
3444 struct isl_basic_map
*res
;
3445 struct isl_dim_map
*dim_map
;
3446 unsigned total
, off
;
3447 enum isl_dim_type t
;
3450 return basic_map_space_reset(bmap
, type
);
3455 res_dim
= isl_space_insert_dims(isl_basic_map_get_space(bmap
), type
, pos
, n
);
3457 total
= isl_basic_map_total_dim(bmap
) + n
;
3458 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3460 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3462 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3464 unsigned size
= isl_basic_map_dim(bmap
, t
);
3465 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3467 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3468 pos
, size
- pos
, off
+ pos
+ n
);
3470 off
+= isl_space_dim(res_dim
, t
);
3472 isl_dim_map_div(dim_map
, bmap
, off
);
3474 res
= isl_basic_map_alloc_space(res_dim
,
3475 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3476 rational
= isl_basic_map_is_rational(bmap
);
3478 res
= isl_basic_map_free(res
);
3480 res
= isl_basic_map_set_rational(res
);
3481 if (isl_basic_map_plain_is_empty(bmap
)) {
3482 isl_basic_map_free(bmap
);
3484 return isl_basic_map_set_to_empty(res
);
3486 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3487 return isl_basic_map_finalize(res
);
3490 __isl_give isl_basic_set
*isl_basic_set_insert_dims(
3491 __isl_take isl_basic_set
*bset
,
3492 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3494 return isl_basic_map_insert_dims(bset
, type
, pos
, n
);
3497 __isl_give isl_basic_map
*isl_basic_map_add_dims(__isl_take isl_basic_map
*bmap
,
3498 enum isl_dim_type type
, unsigned n
)
3502 return isl_basic_map_insert_dims(bmap
, type
,
3503 isl_basic_map_dim(bmap
, type
), n
);
3506 __isl_give isl_basic_set
*isl_basic_set_add_dims(__isl_take isl_basic_set
*bset
,
3507 enum isl_dim_type type
, unsigned n
)
3511 isl_assert(bset
->ctx
, type
!= isl_dim_in
, goto error
);
3512 return isl_basic_map_add_dims(bset
, type
, n
);
3514 isl_basic_set_free(bset
);
3518 static __isl_give isl_map
*map_space_reset(__isl_take isl_map
*map
,
3519 enum isl_dim_type type
)
3523 if (!map
|| !isl_space_is_named_or_nested(map
->dim
, type
))
3526 space
= isl_map_get_space(map
);
3527 space
= isl_space_reset(space
, type
);
3528 map
= isl_map_reset_space(map
, space
);
3532 __isl_give isl_map
*isl_map_insert_dims(__isl_take isl_map
*map
,
3533 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3538 return map_space_reset(map
, type
);
3540 map
= isl_map_cow(map
);
3544 map
->dim
= isl_space_insert_dims(map
->dim
, type
, pos
, n
);
3548 for (i
= 0; i
< map
->n
; ++i
) {
3549 map
->p
[i
] = isl_basic_map_insert_dims(map
->p
[i
], type
, pos
, n
);
3560 __isl_give isl_set
*isl_set_insert_dims(__isl_take isl_set
*set
,
3561 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3563 return isl_map_insert_dims(set
, type
, pos
, n
);
3566 __isl_give isl_map
*isl_map_add_dims(__isl_take isl_map
*map
,
3567 enum isl_dim_type type
, unsigned n
)
3571 return isl_map_insert_dims(map
, type
, isl_map_dim(map
, type
), n
);
3574 __isl_give isl_set
*isl_set_add_dims(__isl_take isl_set
*set
,
3575 enum isl_dim_type type
, unsigned n
)
3579 isl_assert(set
->ctx
, type
!= isl_dim_in
, goto error
);
3580 return set_from_map(isl_map_add_dims(set_to_map(set
), type
, n
));
3586 __isl_give isl_basic_map
*isl_basic_map_move_dims(
3587 __isl_take isl_basic_map
*bmap
,
3588 enum isl_dim_type dst_type
, unsigned dst_pos
,
3589 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3591 struct isl_dim_map
*dim_map
;
3592 struct isl_basic_map
*res
;
3593 enum isl_dim_type t
;
3594 unsigned total
, off
;
3601 if (isl_basic_map_check_range(bmap
, src_type
, src_pos
, n
) < 0)
3602 return isl_basic_map_free(bmap
);
3604 if (dst_type
== src_type
&& dst_pos
== src_pos
)
3607 isl_assert(bmap
->ctx
, dst_type
!= src_type
, goto error
);
3609 if (pos(bmap
->dim
, dst_type
) + dst_pos
==
3610 pos(bmap
->dim
, src_type
) + src_pos
+
3611 ((src_type
< dst_type
) ? n
: 0)) {
3612 bmap
= isl_basic_map_cow(bmap
);
3616 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
3617 src_type
, src_pos
, n
);
3621 bmap
= isl_basic_map_finalize(bmap
);
3626 total
= isl_basic_map_total_dim(bmap
);
3627 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3630 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3631 unsigned size
= isl_space_dim(bmap
->dim
, t
);
3632 if (t
== dst_type
) {
3633 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3636 isl_dim_map_dim_range(dim_map
, bmap
->dim
, src_type
,
3639 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3640 dst_pos
, size
- dst_pos
, off
);
3641 off
+= size
- dst_pos
;
3642 } else if (t
== src_type
) {
3643 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3646 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3647 src_pos
+ n
, size
- src_pos
- n
, off
);
3648 off
+= size
- src_pos
- n
;
3650 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3654 isl_dim_map_div(dim_map
, bmap
, off
);
3656 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
3657 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3658 bmap
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3662 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
3663 src_type
, src_pos
, n
);
3667 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
3668 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3669 bmap
= isl_basic_map_finalize(bmap
);
3673 isl_basic_map_free(bmap
);
3677 __isl_give isl_basic_set
*isl_basic_set_move_dims(__isl_take isl_basic_set
*bset
,
3678 enum isl_dim_type dst_type
, unsigned dst_pos
,
3679 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3681 isl_basic_map
*bmap
= bset_to_bmap(bset
);
3682 bmap
= isl_basic_map_move_dims(bmap
, dst_type
, dst_pos
,
3683 src_type
, src_pos
, n
);
3684 return bset_from_bmap(bmap
);
3687 __isl_give isl_set
*isl_set_move_dims(__isl_take isl_set
*set
,
3688 enum isl_dim_type dst_type
, unsigned dst_pos
,
3689 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3693 isl_assert(set
->ctx
, dst_type
!= isl_dim_in
, goto error
);
3694 return set_from_map(isl_map_move_dims(set_to_map(set
),
3695 dst_type
, dst_pos
, src_type
, src_pos
, n
));
3701 __isl_give isl_map
*isl_map_move_dims(__isl_take isl_map
*map
,
3702 enum isl_dim_type dst_type
, unsigned dst_pos
,
3703 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3712 isl_assert(map
->ctx
, src_pos
+ n
<= isl_map_dim(map
, src_type
),
3715 if (dst_type
== src_type
&& dst_pos
== src_pos
)
3718 isl_assert(map
->ctx
, dst_type
!= src_type
, goto error
);
3720 map
= isl_map_cow(map
);
3724 map
->dim
= isl_space_move_dims(map
->dim
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
3728 for (i
= 0; i
< map
->n
; ++i
) {
3729 map
->p
[i
] = isl_basic_map_move_dims(map
->p
[i
],
3731 src_type
, src_pos
, n
);
3742 /* Move the specified dimensions to the last columns right before
3743 * the divs. Don't change the dimension specification of bmap.
3744 * That's the responsibility of the caller.
3746 static __isl_give isl_basic_map
*move_last(__isl_take isl_basic_map
*bmap
,
3747 enum isl_dim_type type
, unsigned first
, unsigned n
)
3749 struct isl_dim_map
*dim_map
;
3750 struct isl_basic_map
*res
;
3751 enum isl_dim_type t
;
3752 unsigned total
, off
;
3756 if (pos(bmap
->dim
, type
) + first
+ n
==
3757 1 + isl_space_dim(bmap
->dim
, isl_dim_all
))
3760 total
= isl_basic_map_total_dim(bmap
);
3761 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3764 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3765 unsigned size
= isl_space_dim(bmap
->dim
, t
);
3767 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3770 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3771 first
, n
, total
- bmap
->n_div
- n
);
3772 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3773 first
+ n
, size
- (first
+ n
), off
);
3774 off
+= size
- (first
+ n
);
3776 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3780 isl_dim_map_div(dim_map
, bmap
, off
+ n
);
3782 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
3783 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3784 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3788 /* Insert "n" rows in the divs of "bmap".
3790 * The number of columns is not changed, which means that the last
3791 * dimensions of "bmap" are being reintepreted as the new divs.
3792 * The space of "bmap" is not adjusted, however, which means
3793 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
3794 * from the space of "bmap" is the responsibility of the caller.
3796 static __isl_give isl_basic_map
*insert_div_rows(__isl_take isl_basic_map
*bmap
,
3804 bmap
= isl_basic_map_cow(bmap
);
3808 row_size
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + bmap
->extra
;
3809 old
= bmap
->block2
.data
;
3810 bmap
->block2
= isl_blk_extend(bmap
->ctx
, bmap
->block2
,
3811 (bmap
->extra
+ n
) * (1 + row_size
));
3812 if (!bmap
->block2
.data
)
3813 return isl_basic_map_free(bmap
);
3814 new_div
= isl_alloc_array(bmap
->ctx
, isl_int
*, bmap
->extra
+ n
);
3816 return isl_basic_map_free(bmap
);
3817 for (i
= 0; i
< n
; ++i
) {
3818 new_div
[i
] = bmap
->block2
.data
+
3819 (bmap
->extra
+ i
) * (1 + row_size
);
3820 isl_seq_clr(new_div
[i
], 1 + row_size
);
3822 for (i
= 0; i
< bmap
->extra
; ++i
)
3823 new_div
[n
+ i
] = bmap
->block2
.data
+ (bmap
->div
[i
] - old
);
3825 bmap
->div
= new_div
;
3832 /* Drop constraints from "bmap" that only involve the variables
3833 * of "type" in the range [first, first + n] that are not related
3834 * to any of the variables outside that interval.
3835 * These constraints cannot influence the values for the variables
3836 * outside the interval, except in case they cause "bmap" to be empty.
3837 * Only drop the constraints if "bmap" is known to be non-empty.
3839 static __isl_give isl_basic_map
*drop_irrelevant_constraints(
3840 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
3841 unsigned first
, unsigned n
)
3845 unsigned dim
, n_div
;
3848 non_empty
= isl_basic_map_plain_is_non_empty(bmap
);
3850 return isl_basic_map_free(bmap
);
3854 dim
= isl_basic_map_dim(bmap
, isl_dim_all
);
3855 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
3856 groups
= isl_calloc_array(isl_basic_map_get_ctx(bmap
), int, dim
);
3858 return isl_basic_map_free(bmap
);
3859 first
+= isl_basic_map_offset(bmap
, type
) - 1;
3860 for (i
= 0; i
< first
; ++i
)
3862 for (i
= first
+ n
; i
< dim
- n_div
; ++i
)
3865 bmap
= isl_basic_map_drop_unrelated_constraints(bmap
, groups
);
3870 /* Turn the n dimensions of type type, starting at first
3871 * into existentially quantified variables.
3873 * If a subset of the projected out variables are unrelated
3874 * to any of the variables that remain, then the constraints
3875 * involving this subset are simply dropped first.
3877 __isl_give isl_basic_map
*isl_basic_map_project_out(
3878 __isl_take isl_basic_map
*bmap
,
3879 enum isl_dim_type type
, unsigned first
, unsigned n
)
3882 return basic_map_space_reset(bmap
, type
);
3883 if (type
== isl_dim_div
)
3884 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
3885 "cannot project out existentially quantified variables",
3886 return isl_basic_map_free(bmap
));
3888 bmap
= drop_irrelevant_constraints(bmap
, type
, first
, n
);
3892 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
3893 return isl_basic_map_remove_dims(bmap
, type
, first
, n
);
3895 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
3896 return isl_basic_map_free(bmap
);
3898 bmap
= move_last(bmap
, type
, first
, n
);
3899 bmap
= isl_basic_map_cow(bmap
);
3900 bmap
= insert_div_rows(bmap
, n
);
3904 bmap
->dim
= isl_space_drop_dims(bmap
->dim
, type
, first
, n
);
3907 bmap
= isl_basic_map_simplify(bmap
);
3908 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
3909 return isl_basic_map_finalize(bmap
);
3911 isl_basic_map_free(bmap
);
3915 /* Turn the n dimensions of type type, starting at first
3916 * into existentially quantified variables.
3918 struct isl_basic_set
*isl_basic_set_project_out(struct isl_basic_set
*bset
,
3919 enum isl_dim_type type
, unsigned first
, unsigned n
)
3921 return bset_from_bmap(isl_basic_map_project_out(bset_to_bmap(bset
),
3925 /* Turn the n dimensions of type type, starting at first
3926 * into existentially quantified variables.
3928 __isl_give isl_map
*isl_map_project_out(__isl_take isl_map
*map
,
3929 enum isl_dim_type type
, unsigned first
, unsigned n
)
3937 return map_space_reset(map
, type
);
3939 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
3941 map
= isl_map_cow(map
);
3945 map
->dim
= isl_space_drop_dims(map
->dim
, type
, first
, n
);
3949 for (i
= 0; i
< map
->n
; ++i
) {
3950 map
->p
[i
] = isl_basic_map_project_out(map
->p
[i
], type
, first
, n
);
3961 /* Turn the n dimensions of type type, starting at first
3962 * into existentially quantified variables.
3964 __isl_give isl_set
*isl_set_project_out(__isl_take isl_set
*set
,
3965 enum isl_dim_type type
, unsigned first
, unsigned n
)
3967 return set_from_map(isl_map_project_out(set_to_map(set
),
3971 /* Return a map that projects the elements in "set" onto their
3972 * "n" set dimensions starting at "first".
3973 * "type" should be equal to isl_dim_set.
3975 __isl_give isl_map
*isl_set_project_onto_map(__isl_take isl_set
*set
,
3976 enum isl_dim_type type
, unsigned first
, unsigned n
)
3984 if (type
!= isl_dim_set
)
3985 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
3986 "only set dimensions can be projected out", goto error
);
3987 dim
= isl_set_dim(set
, isl_dim_set
);
3988 if (first
+ n
> dim
|| first
+ n
< first
)
3989 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
3990 "index out of bounds", goto error
);
3992 map
= isl_map_from_domain(set
);
3993 map
= isl_map_add_dims(map
, isl_dim_out
, n
);
3994 for (i
= 0; i
< n
; ++i
)
3995 map
= isl_map_equate(map
, isl_dim_in
, first
+ i
,
4003 static struct isl_basic_map
*add_divs(struct isl_basic_map
*bmap
, unsigned n
)
4007 for (i
= 0; i
< n
; ++i
) {
4008 j
= isl_basic_map_alloc_div(bmap
);
4011 isl_seq_clr(bmap
->div
[j
], 1+1+isl_basic_map_total_dim(bmap
));
4015 isl_basic_map_free(bmap
);
4019 struct isl_basic_map
*isl_basic_map_apply_range(
4020 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
4022 isl_space
*dim_result
= NULL
;
4023 struct isl_basic_map
*bmap
;
4024 unsigned n_in
, n_out
, n
, nparam
, total
, pos
;
4025 struct isl_dim_map
*dim_map1
, *dim_map2
;
4027 if (!bmap1
|| !bmap2
)
4029 if (!isl_space_match(bmap1
->dim
, isl_dim_param
,
4030 bmap2
->dim
, isl_dim_param
))
4031 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
4032 "parameters don't match", goto error
);
4033 if (!isl_space_tuple_is_equal(bmap1
->dim
, isl_dim_out
,
4034 bmap2
->dim
, isl_dim_in
))
4035 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
4036 "spaces don't match", goto error
);
4038 dim_result
= isl_space_join(isl_space_copy(bmap1
->dim
),
4039 isl_space_copy(bmap2
->dim
));
4041 n_in
= isl_basic_map_n_in(bmap1
);
4042 n_out
= isl_basic_map_n_out(bmap2
);
4043 n
= isl_basic_map_n_out(bmap1
);
4044 nparam
= isl_basic_map_n_param(bmap1
);
4046 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ n
;
4047 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4048 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4049 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
4050 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
4051 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
4052 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_in
);
4053 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_out
);
4054 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
4055 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
4056 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
4058 bmap
= isl_basic_map_alloc_space(dim_result
,
4059 bmap1
->n_div
+ bmap2
->n_div
+ n
,
4060 bmap1
->n_eq
+ bmap2
->n_eq
,
4061 bmap1
->n_ineq
+ bmap2
->n_ineq
);
4062 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
4063 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
4064 bmap
= add_divs(bmap
, n
);
4065 bmap
= isl_basic_map_simplify(bmap
);
4066 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
4067 return isl_basic_map_finalize(bmap
);
4069 isl_basic_map_free(bmap1
);
4070 isl_basic_map_free(bmap2
);
4074 struct isl_basic_set
*isl_basic_set_apply(
4075 struct isl_basic_set
*bset
, struct isl_basic_map
*bmap
)
4080 isl_assert(bset
->ctx
, isl_basic_map_compatible_domain(bmap
, bset
),
4083 return bset_from_bmap(isl_basic_map_apply_range(bset_to_bmap(bset
),
4086 isl_basic_set_free(bset
);
4087 isl_basic_map_free(bmap
);
4091 struct isl_basic_map
*isl_basic_map_apply_domain(
4092 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
4094 if (!bmap1
|| !bmap2
)
4097 if (!isl_space_match(bmap1
->dim
, isl_dim_param
,
4098 bmap2
->dim
, isl_dim_param
))
4099 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
4100 "parameters don't match", goto error
);
4101 if (!isl_space_tuple_is_equal(bmap1
->dim
, isl_dim_in
,
4102 bmap2
->dim
, isl_dim_in
))
4103 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
4104 "spaces don't match", goto error
);
4106 bmap1
= isl_basic_map_reverse(bmap1
);
4107 bmap1
= isl_basic_map_apply_range(bmap1
, bmap2
);
4108 return isl_basic_map_reverse(bmap1
);
4110 isl_basic_map_free(bmap1
);
4111 isl_basic_map_free(bmap2
);
4115 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
4116 * A \cap B -> f(A) + f(B)
4118 struct isl_basic_map
*isl_basic_map_sum(
4119 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
4121 unsigned n_in
, n_out
, nparam
, total
, pos
;
4122 struct isl_basic_map
*bmap
= NULL
;
4123 struct isl_dim_map
*dim_map1
, *dim_map2
;
4126 if (!bmap1
|| !bmap2
)
4129 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
),
4132 nparam
= isl_basic_map_n_param(bmap1
);
4133 n_in
= isl_basic_map_n_in(bmap1
);
4134 n_out
= isl_basic_map_n_out(bmap1
);
4136 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
;
4137 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4138 dim_map2
= isl_dim_map_alloc(bmap2
->ctx
, total
);
4139 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
4140 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
);
4141 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
4142 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
4143 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_in
+ n_out
);
4144 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
4145 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
4146 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_out
);
4148 bmap
= isl_basic_map_alloc_space(isl_space_copy(bmap1
->dim
),
4149 bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
,
4150 bmap1
->n_eq
+ bmap2
->n_eq
+ n_out
,
4151 bmap1
->n_ineq
+ bmap2
->n_ineq
);
4152 for (i
= 0; i
< n_out
; ++i
) {
4153 int j
= isl_basic_map_alloc_equality(bmap
);
4156 isl_seq_clr(bmap
->eq
[j
], 1+total
);
4157 isl_int_set_si(bmap
->eq
[j
][1+nparam
+n_in
+i
], -1);
4158 isl_int_set_si(bmap
->eq
[j
][1+pos
+i
], 1);
4159 isl_int_set_si(bmap
->eq
[j
][1+pos
-n_out
+i
], 1);
4161 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
4162 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
4163 bmap
= add_divs(bmap
, 2 * n_out
);
4165 bmap
= isl_basic_map_simplify(bmap
);
4166 return isl_basic_map_finalize(bmap
);
4168 isl_basic_map_free(bmap
);
4169 isl_basic_map_free(bmap1
);
4170 isl_basic_map_free(bmap2
);
4174 /* Given two maps A -> f(A) and B -> g(B), construct a map
4175 * A \cap B -> f(A) + f(B)
4177 struct isl_map
*isl_map_sum(struct isl_map
*map1
, struct isl_map
*map2
)
4179 struct isl_map
*result
;
4185 isl_assert(map1
->ctx
, isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
4187 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
4188 map1
->n
* map2
->n
, 0);
4191 for (i
= 0; i
< map1
->n
; ++i
)
4192 for (j
= 0; j
< map2
->n
; ++j
) {
4193 struct isl_basic_map
*part
;
4194 part
= isl_basic_map_sum(
4195 isl_basic_map_copy(map1
->p
[i
]),
4196 isl_basic_map_copy(map2
->p
[j
]));
4197 if (isl_basic_map_is_empty(part
))
4198 isl_basic_map_free(part
);
4200 result
= isl_map_add_basic_map(result
, part
);
4213 __isl_give isl_set
*isl_set_sum(__isl_take isl_set
*set1
,
4214 __isl_take isl_set
*set2
)
4216 return set_from_map(isl_map_sum(set_to_map(set1
), set_to_map(set2
)));
4219 /* Given a basic map A -> f(A), construct A -> -f(A).
4221 struct isl_basic_map
*isl_basic_map_neg(struct isl_basic_map
*bmap
)
4226 bmap
= isl_basic_map_cow(bmap
);
4230 n
= isl_basic_map_dim(bmap
, isl_dim_out
);
4231 off
= isl_basic_map_offset(bmap
, isl_dim_out
);
4232 for (i
= 0; i
< bmap
->n_eq
; ++i
)
4233 for (j
= 0; j
< n
; ++j
)
4234 isl_int_neg(bmap
->eq
[i
][off
+j
], bmap
->eq
[i
][off
+j
]);
4235 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
4236 for (j
= 0; j
< n
; ++j
)
4237 isl_int_neg(bmap
->ineq
[i
][off
+j
], bmap
->ineq
[i
][off
+j
]);
4238 for (i
= 0; i
< bmap
->n_div
; ++i
)
4239 for (j
= 0; j
< n
; ++j
)
4240 isl_int_neg(bmap
->div
[i
][1+off
+j
], bmap
->div
[i
][1+off
+j
]);
4241 bmap
= isl_basic_map_gauss(bmap
, NULL
);
4242 return isl_basic_map_finalize(bmap
);
4245 __isl_give isl_basic_set
*isl_basic_set_neg(__isl_take isl_basic_set
*bset
)
4247 return isl_basic_map_neg(bset
);
4250 /* Given a map A -> f(A), construct A -> -f(A).
4252 struct isl_map
*isl_map_neg(struct isl_map
*map
)
4256 map
= isl_map_cow(map
);
4260 for (i
= 0; i
< map
->n
; ++i
) {
4261 map
->p
[i
] = isl_basic_map_neg(map
->p
[i
]);
4272 __isl_give isl_set
*isl_set_neg(__isl_take isl_set
*set
)
4274 return set_from_map(isl_map_neg(set_to_map(set
)));
4277 /* Given a basic map A -> f(A) and an integer d, construct a basic map
4278 * A -> floor(f(A)/d).
4280 struct isl_basic_map
*isl_basic_map_floordiv(struct isl_basic_map
*bmap
,
4283 unsigned n_in
, n_out
, nparam
, total
, pos
;
4284 struct isl_basic_map
*result
= NULL
;
4285 struct isl_dim_map
*dim_map
;
4291 nparam
= isl_basic_map_n_param(bmap
);
4292 n_in
= isl_basic_map_n_in(bmap
);
4293 n_out
= isl_basic_map_n_out(bmap
);
4295 total
= nparam
+ n_in
+ n_out
+ bmap
->n_div
+ n_out
;
4296 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
4297 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_param
, pos
= 0);
4298 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_in
, pos
+= nparam
);
4299 isl_dim_map_div(dim_map
, bmap
, pos
+= n_in
+ n_out
);
4300 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_out
, pos
+= bmap
->n_div
);
4302 result
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
4303 bmap
->n_div
+ n_out
,
4304 bmap
->n_eq
, bmap
->n_ineq
+ 2 * n_out
);
4305 result
= isl_basic_map_add_constraints_dim_map(result
, bmap
, dim_map
);
4306 result
= add_divs(result
, n_out
);
4307 for (i
= 0; i
< n_out
; ++i
) {
4309 j
= isl_basic_map_alloc_inequality(result
);
4312 isl_seq_clr(result
->ineq
[j
], 1+total
);
4313 isl_int_neg(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
4314 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], 1);
4315 j
= isl_basic_map_alloc_inequality(result
);
4318 isl_seq_clr(result
->ineq
[j
], 1+total
);
4319 isl_int_set(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
4320 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], -1);
4321 isl_int_sub_ui(result
->ineq
[j
][0], d
, 1);
4324 result
= isl_basic_map_simplify(result
);
4325 return isl_basic_map_finalize(result
);
4327 isl_basic_map_free(result
);
4331 /* Given a map A -> f(A) and an integer d, construct a map
4332 * A -> floor(f(A)/d).
4334 struct isl_map
*isl_map_floordiv(struct isl_map
*map
, isl_int d
)
4338 map
= isl_map_cow(map
);
4342 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
4343 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4344 for (i
= 0; i
< map
->n
; ++i
) {
4345 map
->p
[i
] = isl_basic_map_floordiv(map
->p
[i
], d
);
4356 /* Given a map A -> f(A) and an integer d, construct a map
4357 * A -> floor(f(A)/d).
4359 __isl_give isl_map
*isl_map_floordiv_val(__isl_take isl_map
*map
,
4360 __isl_take isl_val
*d
)
4364 if (!isl_val_is_int(d
))
4365 isl_die(isl_val_get_ctx(d
), isl_error_invalid
,
4366 "expecting integer denominator", goto error
);
4367 map
= isl_map_floordiv(map
, d
->n
);
4376 static struct isl_basic_map
*var_equal(struct isl_basic_map
*bmap
, unsigned pos
)
4382 i
= isl_basic_map_alloc_equality(bmap
);
4385 nparam
= isl_basic_map_n_param(bmap
);
4386 n_in
= isl_basic_map_n_in(bmap
);
4387 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4388 isl_int_set_si(bmap
->eq
[i
][1+nparam
+pos
], -1);
4389 isl_int_set_si(bmap
->eq
[i
][1+nparam
+n_in
+pos
], 1);
4390 return isl_basic_map_finalize(bmap
);
4392 isl_basic_map_free(bmap
);
4396 /* Add a constraint to "bmap" expressing i_pos < o_pos
4398 static struct isl_basic_map
*var_less(struct isl_basic_map
*bmap
, unsigned pos
)
4404 i
= isl_basic_map_alloc_inequality(bmap
);
4407 nparam
= isl_basic_map_n_param(bmap
);
4408 n_in
= isl_basic_map_n_in(bmap
);
4409 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4410 isl_int_set_si(bmap
->ineq
[i
][0], -1);
4411 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
4412 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
4413 return isl_basic_map_finalize(bmap
);
4415 isl_basic_map_free(bmap
);
4419 /* Add a constraint to "bmap" expressing i_pos <= o_pos
4421 static __isl_give isl_basic_map
*var_less_or_equal(
4422 __isl_take isl_basic_map
*bmap
, unsigned pos
)
4428 i
= isl_basic_map_alloc_inequality(bmap
);
4431 nparam
= isl_basic_map_n_param(bmap
);
4432 n_in
= isl_basic_map_n_in(bmap
);
4433 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4434 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
4435 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
4436 return isl_basic_map_finalize(bmap
);
4438 isl_basic_map_free(bmap
);
4442 /* Add a constraint to "bmap" expressing i_pos > o_pos
4444 static struct isl_basic_map
*var_more(struct isl_basic_map
*bmap
, unsigned pos
)
4450 i
= isl_basic_map_alloc_inequality(bmap
);
4453 nparam
= isl_basic_map_n_param(bmap
);
4454 n_in
= isl_basic_map_n_in(bmap
);
4455 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4456 isl_int_set_si(bmap
->ineq
[i
][0], -1);
4457 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
4458 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
4459 return isl_basic_map_finalize(bmap
);
4461 isl_basic_map_free(bmap
);
4465 /* Add a constraint to "bmap" expressing i_pos >= o_pos
4467 static __isl_give isl_basic_map
*var_more_or_equal(
4468 __isl_take isl_basic_map
*bmap
, unsigned pos
)
4474 i
= isl_basic_map_alloc_inequality(bmap
);
4477 nparam
= isl_basic_map_n_param(bmap
);
4478 n_in
= isl_basic_map_n_in(bmap
);
4479 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4480 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
4481 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
4482 return isl_basic_map_finalize(bmap
);
4484 isl_basic_map_free(bmap
);
4488 __isl_give isl_basic_map
*isl_basic_map_equal(
4489 __isl_take isl_space
*dim
, unsigned n_equal
)
4492 struct isl_basic_map
*bmap
;
4493 bmap
= isl_basic_map_alloc_space(dim
, 0, n_equal
, 0);
4496 for (i
= 0; i
< n_equal
&& bmap
; ++i
)
4497 bmap
= var_equal(bmap
, i
);
4498 return isl_basic_map_finalize(bmap
);
4501 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
4503 __isl_give isl_basic_map
*isl_basic_map_less_at(__isl_take isl_space
*dim
,
4507 struct isl_basic_map
*bmap
;
4508 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4511 for (i
= 0; i
< pos
&& bmap
; ++i
)
4512 bmap
= var_equal(bmap
, i
);
4514 bmap
= var_less(bmap
, pos
);
4515 return isl_basic_map_finalize(bmap
);
4518 /* Return a relation on "dim" expressing i_[0..pos] <<= o_[0..pos]
4520 __isl_give isl_basic_map
*isl_basic_map_less_or_equal_at(
4521 __isl_take isl_space
*dim
, unsigned pos
)
4524 isl_basic_map
*bmap
;
4526 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4527 for (i
= 0; i
< pos
; ++i
)
4528 bmap
= var_equal(bmap
, i
);
4529 bmap
= var_less_or_equal(bmap
, pos
);
4530 return isl_basic_map_finalize(bmap
);
4533 /* Return a relation on "dim" expressing i_pos > o_pos
4535 __isl_give isl_basic_map
*isl_basic_map_more_at(__isl_take isl_space
*dim
,
4539 struct isl_basic_map
*bmap
;
4540 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4543 for (i
= 0; i
< pos
&& bmap
; ++i
)
4544 bmap
= var_equal(bmap
, i
);
4546 bmap
= var_more(bmap
, pos
);
4547 return isl_basic_map_finalize(bmap
);
4550 /* Return a relation on "dim" expressing i_[0..pos] >>= o_[0..pos]
4552 __isl_give isl_basic_map
*isl_basic_map_more_or_equal_at(
4553 __isl_take isl_space
*dim
, unsigned pos
)
4556 isl_basic_map
*bmap
;
4558 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4559 for (i
= 0; i
< pos
; ++i
)
4560 bmap
= var_equal(bmap
, i
);
4561 bmap
= var_more_or_equal(bmap
, pos
);
4562 return isl_basic_map_finalize(bmap
);
4565 static __isl_give isl_map
*map_lex_lte_first(__isl_take isl_space
*dims
,
4566 unsigned n
, int equal
)
4568 struct isl_map
*map
;
4571 if (n
== 0 && equal
)
4572 return isl_map_universe(dims
);
4574 map
= isl_map_alloc_space(isl_space_copy(dims
), n
, ISL_MAP_DISJOINT
);
4576 for (i
= 0; i
+ 1 < n
; ++i
)
4577 map
= isl_map_add_basic_map(map
,
4578 isl_basic_map_less_at(isl_space_copy(dims
), i
));
4581 map
= isl_map_add_basic_map(map
,
4582 isl_basic_map_less_or_equal_at(dims
, n
- 1));
4584 map
= isl_map_add_basic_map(map
,
4585 isl_basic_map_less_at(dims
, n
- 1));
4587 isl_space_free(dims
);
4592 static __isl_give isl_map
*map_lex_lte(__isl_take isl_space
*dims
, int equal
)
4596 return map_lex_lte_first(dims
, dims
->n_out
, equal
);
4599 __isl_give isl_map
*isl_map_lex_lt_first(__isl_take isl_space
*dim
, unsigned n
)
4601 return map_lex_lte_first(dim
, n
, 0);
4604 __isl_give isl_map
*isl_map_lex_le_first(__isl_take isl_space
*dim
, unsigned n
)
4606 return map_lex_lte_first(dim
, n
, 1);
4609 __isl_give isl_map
*isl_map_lex_lt(__isl_take isl_space
*set_dim
)
4611 return map_lex_lte(isl_space_map_from_set(set_dim
), 0);
4614 __isl_give isl_map
*isl_map_lex_le(__isl_take isl_space
*set_dim
)
4616 return map_lex_lte(isl_space_map_from_set(set_dim
), 1);
4619 static __isl_give isl_map
*map_lex_gte_first(__isl_take isl_space
*dims
,
4620 unsigned n
, int equal
)
4622 struct isl_map
*map
;
4625 if (n
== 0 && equal
)
4626 return isl_map_universe(dims
);
4628 map
= isl_map_alloc_space(isl_space_copy(dims
), n
, ISL_MAP_DISJOINT
);
4630 for (i
= 0; i
+ 1 < n
; ++i
)
4631 map
= isl_map_add_basic_map(map
,
4632 isl_basic_map_more_at(isl_space_copy(dims
), i
));
4635 map
= isl_map_add_basic_map(map
,
4636 isl_basic_map_more_or_equal_at(dims
, n
- 1));
4638 map
= isl_map_add_basic_map(map
,
4639 isl_basic_map_more_at(dims
, n
- 1));
4641 isl_space_free(dims
);
4646 static __isl_give isl_map
*map_lex_gte(__isl_take isl_space
*dims
, int equal
)
4650 return map_lex_gte_first(dims
, dims
->n_out
, equal
);
4653 __isl_give isl_map
*isl_map_lex_gt_first(__isl_take isl_space
*dim
, unsigned n
)
4655 return map_lex_gte_first(dim
, n
, 0);
4658 __isl_give isl_map
*isl_map_lex_ge_first(__isl_take isl_space
*dim
, unsigned n
)
4660 return map_lex_gte_first(dim
, n
, 1);
4663 __isl_give isl_map
*isl_map_lex_gt(__isl_take isl_space
*set_dim
)
4665 return map_lex_gte(isl_space_map_from_set(set_dim
), 0);
4668 __isl_give isl_map
*isl_map_lex_ge(__isl_take isl_space
*set_dim
)
4670 return map_lex_gte(isl_space_map_from_set(set_dim
), 1);
4673 __isl_give isl_map
*isl_set_lex_le_set(__isl_take isl_set
*set1
,
4674 __isl_take isl_set
*set2
)
4677 map
= isl_map_lex_le(isl_set_get_space(set1
));
4678 map
= isl_map_intersect_domain(map
, set1
);
4679 map
= isl_map_intersect_range(map
, set2
);
4683 __isl_give isl_map
*isl_set_lex_lt_set(__isl_take isl_set
*set1
,
4684 __isl_take isl_set
*set2
)
4687 map
= isl_map_lex_lt(isl_set_get_space(set1
));
4688 map
= isl_map_intersect_domain(map
, set1
);
4689 map
= isl_map_intersect_range(map
, set2
);
4693 __isl_give isl_map
*isl_set_lex_ge_set(__isl_take isl_set
*set1
,
4694 __isl_take isl_set
*set2
)
4697 map
= isl_map_lex_ge(isl_set_get_space(set1
));
4698 map
= isl_map_intersect_domain(map
, set1
);
4699 map
= isl_map_intersect_range(map
, set2
);
4703 __isl_give isl_map
*isl_set_lex_gt_set(__isl_take isl_set
*set1
,
4704 __isl_take isl_set
*set2
)
4707 map
= isl_map_lex_gt(isl_set_get_space(set1
));
4708 map
= isl_map_intersect_domain(map
, set1
);
4709 map
= isl_map_intersect_range(map
, set2
);
4713 __isl_give isl_map
*isl_map_lex_le_map(__isl_take isl_map
*map1
,
4714 __isl_take isl_map
*map2
)
4717 map
= isl_map_lex_le(isl_space_range(isl_map_get_space(map1
)));
4718 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4719 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4723 __isl_give isl_map
*isl_map_lex_lt_map(__isl_take isl_map
*map1
,
4724 __isl_take isl_map
*map2
)
4727 map
= isl_map_lex_lt(isl_space_range(isl_map_get_space(map1
)));
4728 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4729 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4733 __isl_give isl_map
*isl_map_lex_ge_map(__isl_take isl_map
*map1
,
4734 __isl_take isl_map
*map2
)
4737 map
= isl_map_lex_ge(isl_space_range(isl_map_get_space(map1
)));
4738 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4739 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4743 __isl_give isl_map
*isl_map_lex_gt_map(__isl_take isl_map
*map1
,
4744 __isl_take isl_map
*map2
)
4747 map
= isl_map_lex_gt(isl_space_range(isl_map_get_space(map1
)));
4748 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4749 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4753 /* For a div d = floor(f/m), add the constraint
4757 static isl_stat
add_upper_div_constraint(__isl_keep isl_basic_map
*bmap
,
4758 unsigned pos
, isl_int
*div
)
4761 unsigned total
= isl_basic_map_total_dim(bmap
);
4763 i
= isl_basic_map_alloc_inequality(bmap
);
4765 return isl_stat_error
;
4766 isl_seq_cpy(bmap
->ineq
[i
], div
+ 1, 1 + total
);
4767 isl_int_neg(bmap
->ineq
[i
][1 + pos
], div
[0]);
4772 /* For a div d = floor(f/m), add the constraint
4774 * -(f-(m-1)) + m d >= 0
4776 static isl_stat
add_lower_div_constraint(__isl_keep isl_basic_map
*bmap
,
4777 unsigned pos
, isl_int
*div
)
4780 unsigned total
= isl_basic_map_total_dim(bmap
);
4782 i
= isl_basic_map_alloc_inequality(bmap
);
4784 return isl_stat_error
;
4785 isl_seq_neg(bmap
->ineq
[i
], div
+ 1, 1 + total
);
4786 isl_int_set(bmap
->ineq
[i
][1 + pos
], div
[0]);
4787 isl_int_add(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], bmap
->ineq
[i
][1 + pos
]);
4788 isl_int_sub_ui(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], 1);
4793 /* For a div d = floor(f/m), add the constraints
4796 * -(f-(m-1)) + m d >= 0
4798 * Note that the second constraint is the negation of
4802 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map
*bmap
,
4803 unsigned pos
, isl_int
*div
)
4805 if (add_upper_div_constraint(bmap
, pos
, div
) < 0)
4807 if (add_lower_div_constraint(bmap
, pos
, div
) < 0)
4812 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set
*bset
,
4813 unsigned pos
, isl_int
*div
)
4815 return isl_basic_map_add_div_constraints_var(bset_to_bmap(bset
),
4819 int isl_basic_map_add_div_constraints(struct isl_basic_map
*bmap
, unsigned div
)
4821 unsigned total
= isl_basic_map_total_dim(bmap
);
4822 unsigned div_pos
= total
- bmap
->n_div
+ div
;
4824 return isl_basic_map_add_div_constraints_var(bmap
, div_pos
,
4828 /* For each known div d = floor(f/m), add the constraints
4831 * -(f-(m-1)) + m d >= 0
4833 * Remove duplicate constraints in case of some these div constraints
4834 * already appear in "bmap".
4836 __isl_give isl_basic_map
*isl_basic_map_add_known_div_constraints(
4837 __isl_take isl_basic_map
*bmap
)
4843 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4847 bmap
= add_known_div_constraints(bmap
);
4848 bmap
= isl_basic_map_remove_duplicate_constraints(bmap
, NULL
, 0);
4849 bmap
= isl_basic_map_finalize(bmap
);
4853 /* Add the div constraint of sign "sign" for div "div" of "bmap".
4855 * In particular, if this div is of the form d = floor(f/m),
4856 * then add the constraint
4860 * if sign < 0 or the constraint
4862 * -(f-(m-1)) + m d >= 0
4866 int isl_basic_map_add_div_constraint(__isl_keep isl_basic_map
*bmap
,
4867 unsigned div
, int sign
)
4875 total
= isl_basic_map_total_dim(bmap
);
4876 div_pos
= total
- bmap
->n_div
+ div
;
4879 return add_upper_div_constraint(bmap
, div_pos
, bmap
->div
[div
]);
4881 return add_lower_div_constraint(bmap
, div_pos
, bmap
->div
[div
]);
4884 struct isl_basic_set
*isl_basic_map_underlying_set(
4885 struct isl_basic_map
*bmap
)
4889 if (bmap
->dim
->nparam
== 0 && bmap
->dim
->n_in
== 0 &&
4891 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_in
) &&
4892 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_out
))
4893 return bset_from_bmap(bmap
);
4894 bmap
= isl_basic_map_cow(bmap
);
4897 bmap
->dim
= isl_space_underlying(bmap
->dim
, bmap
->n_div
);
4900 bmap
->extra
-= bmap
->n_div
;
4902 bmap
= isl_basic_map_finalize(bmap
);
4903 return bset_from_bmap(bmap
);
4905 isl_basic_map_free(bmap
);
4909 __isl_give isl_basic_set
*isl_basic_set_underlying_set(
4910 __isl_take isl_basic_set
*bset
)
4912 return isl_basic_map_underlying_set(bset_to_bmap(bset
));
4915 /* Replace each element in "list" by the result of applying
4916 * isl_basic_map_underlying_set to the element.
4918 __isl_give isl_basic_set_list
*isl_basic_map_list_underlying_set(
4919 __isl_take isl_basic_map_list
*list
)
4926 n
= isl_basic_map_list_n_basic_map(list
);
4927 for (i
= 0; i
< n
; ++i
) {
4928 isl_basic_map
*bmap
;
4929 isl_basic_set
*bset
;
4931 bmap
= isl_basic_map_list_get_basic_map(list
, i
);
4932 bset
= isl_basic_set_underlying_set(bmap
);
4933 list
= isl_basic_set_list_set_basic_set(list
, i
, bset
);
4939 struct isl_basic_map
*isl_basic_map_overlying_set(
4940 struct isl_basic_set
*bset
, struct isl_basic_map
*like
)
4942 struct isl_basic_map
*bmap
;
4943 struct isl_ctx
*ctx
;
4950 isl_assert(ctx
, bset
->n_div
== 0, goto error
);
4951 isl_assert(ctx
, isl_basic_set_n_param(bset
) == 0, goto error
);
4952 isl_assert(ctx
, bset
->dim
->n_out
== isl_basic_map_total_dim(like
),
4954 if (like
->n_div
== 0) {
4955 isl_space
*space
= isl_basic_map_get_space(like
);
4956 isl_basic_map_free(like
);
4957 return isl_basic_map_reset_space(bset
, space
);
4959 bset
= isl_basic_set_cow(bset
);
4962 total
= bset
->dim
->n_out
+ bset
->extra
;
4963 bmap
= bset_to_bmap(bset
);
4964 isl_space_free(bmap
->dim
);
4965 bmap
->dim
= isl_space_copy(like
->dim
);
4968 bmap
->n_div
= like
->n_div
;
4969 bmap
->extra
+= like
->n_div
;
4973 ltotal
= total
- bmap
->extra
+ like
->extra
;
4976 bmap
->block2
= isl_blk_extend(ctx
, bmap
->block2
,
4977 bmap
->extra
* (1 + 1 + total
));
4978 if (isl_blk_is_error(bmap
->block2
))
4980 div
= isl_realloc_array(ctx
, bmap
->div
, isl_int
*, bmap
->extra
);
4984 for (i
= 0; i
< bmap
->extra
; ++i
)
4985 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + 1 + total
);
4986 for (i
= 0; i
< like
->n_div
; ++i
) {
4987 isl_seq_cpy(bmap
->div
[i
], like
->div
[i
], 1 + 1 + ltotal
);
4988 isl_seq_clr(bmap
->div
[i
]+1+1+ltotal
, total
- ltotal
);
4990 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
4992 isl_basic_map_free(like
);
4993 bmap
= isl_basic_map_simplify(bmap
);
4994 bmap
= isl_basic_map_finalize(bmap
);
4997 isl_basic_map_free(like
);
4998 isl_basic_set_free(bset
);
5002 struct isl_basic_set
*isl_basic_set_from_underlying_set(
5003 struct isl_basic_set
*bset
, struct isl_basic_set
*like
)
5005 return bset_from_bmap(isl_basic_map_overlying_set(bset
,
5006 bset_to_bmap(like
)));
5009 struct isl_set
*isl_map_underlying_set(struct isl_map
*map
)
5013 map
= isl_map_cow(map
);
5016 map
->dim
= isl_space_cow(map
->dim
);
5020 for (i
= 1; i
< map
->n
; ++i
)
5021 isl_assert(map
->ctx
, map
->p
[0]->n_div
== map
->p
[i
]->n_div
,
5023 for (i
= 0; i
< map
->n
; ++i
) {
5024 map
->p
[i
] = bset_to_bmap(
5025 isl_basic_map_underlying_set(map
->p
[i
]));
5030 map
->dim
= isl_space_underlying(map
->dim
, 0);
5032 isl_space_free(map
->dim
);
5033 map
->dim
= isl_space_copy(map
->p
[0]->dim
);
5037 return set_from_map(map
);
5043 /* Replace the space of "bmap" by "space".
5045 * If the space of "bmap" is identical to "space" (including the identifiers
5046 * of the input and output dimensions), then simply return the original input.
5048 __isl_give isl_basic_map
*isl_basic_map_reset_space(
5049 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*space
)
5055 equal
= isl_space_is_equal(bmap
->dim
, space
);
5056 if (equal
>= 0 && equal
)
5057 equal
= isl_space_match(bmap
->dim
, isl_dim_in
,
5059 if (equal
>= 0 && equal
)
5060 equal
= isl_space_match(bmap
->dim
, isl_dim_out
,
5061 space
, isl_dim_out
);
5065 isl_space_free(space
);
5068 bmap
= isl_basic_map_cow(bmap
);
5069 if (!bmap
|| !space
)
5072 isl_space_free(bmap
->dim
);
5075 bmap
= isl_basic_map_finalize(bmap
);
5079 isl_basic_map_free(bmap
);
5080 isl_space_free(space
);
5084 __isl_give isl_basic_set
*isl_basic_set_reset_space(
5085 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*dim
)
5087 return bset_from_bmap(isl_basic_map_reset_space(bset_to_bmap(bset
),
5091 __isl_give isl_map
*isl_map_reset_space(__isl_take isl_map
*map
,
5092 __isl_take isl_space
*dim
)
5096 map
= isl_map_cow(map
);
5100 for (i
= 0; i
< map
->n
; ++i
) {
5101 map
->p
[i
] = isl_basic_map_reset_space(map
->p
[i
],
5102 isl_space_copy(dim
));
5106 isl_space_free(map
->dim
);
5112 isl_space_free(dim
);
5116 __isl_give isl_set
*isl_set_reset_space(__isl_take isl_set
*set
,
5117 __isl_take isl_space
*dim
)
5119 return set_from_map(isl_map_reset_space(set_to_map(set
), dim
));
5122 /* Compute the parameter domain of the given basic set.
5124 __isl_give isl_basic_set
*isl_basic_set_params(__isl_take isl_basic_set
*bset
)
5130 is_params
= isl_basic_set_is_params(bset
);
5132 return isl_basic_set_free(bset
);
5136 n
= isl_basic_set_dim(bset
, isl_dim_set
);
5137 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
5138 space
= isl_basic_set_get_space(bset
);
5139 space
= isl_space_params(space
);
5140 bset
= isl_basic_set_reset_space(bset
, space
);
5144 /* Construct a zero-dimensional basic set with the given parameter domain.
5146 __isl_give isl_basic_set
*isl_basic_set_from_params(
5147 __isl_take isl_basic_set
*bset
)
5150 space
= isl_basic_set_get_space(bset
);
5151 space
= isl_space_set_from_params(space
);
5152 bset
= isl_basic_set_reset_space(bset
, space
);
5156 /* Compute the parameter domain of the given set.
5158 __isl_give isl_set
*isl_set_params(__isl_take isl_set
*set
)
5163 if (isl_set_is_params(set
))
5166 n
= isl_set_dim(set
, isl_dim_set
);
5167 set
= isl_set_project_out(set
, isl_dim_set
, 0, n
);
5168 space
= isl_set_get_space(set
);
5169 space
= isl_space_params(space
);
5170 set
= isl_set_reset_space(set
, space
);
5174 /* Construct a zero-dimensional set with the given parameter domain.
5176 __isl_give isl_set
*isl_set_from_params(__isl_take isl_set
*set
)
5179 space
= isl_set_get_space(set
);
5180 space
= isl_space_set_from_params(space
);
5181 set
= isl_set_reset_space(set
, space
);
5185 /* Compute the parameter domain of the given map.
5187 __isl_give isl_set
*isl_map_params(__isl_take isl_map
*map
)
5192 n
= isl_map_dim(map
, isl_dim_in
);
5193 map
= isl_map_project_out(map
, isl_dim_in
, 0, n
);
5194 n
= isl_map_dim(map
, isl_dim_out
);
5195 map
= isl_map_project_out(map
, isl_dim_out
, 0, n
);
5196 space
= isl_map_get_space(map
);
5197 space
= isl_space_params(space
);
5198 map
= isl_map_reset_space(map
, space
);
5202 struct isl_basic_set
*isl_basic_map_domain(struct isl_basic_map
*bmap
)
5209 space
= isl_space_domain(isl_basic_map_get_space(bmap
));
5211 n_out
= isl_basic_map_n_out(bmap
);
5212 bmap
= isl_basic_map_project_out(bmap
, isl_dim_out
, 0, n_out
);
5214 return isl_basic_map_reset_space(bmap
, space
);
5217 isl_bool
isl_basic_map_may_be_set(__isl_keep isl_basic_map
*bmap
)
5220 return isl_bool_error
;
5221 return isl_space_may_be_set(bmap
->dim
);
5224 /* Is this basic map actually a set?
5225 * Users should never call this function. Outside of isl,
5226 * the type should indicate whether something is a set or a map.
5228 isl_bool
isl_basic_map_is_set(__isl_keep isl_basic_map
*bmap
)
5231 return isl_bool_error
;
5232 return isl_space_is_set(bmap
->dim
);
5235 struct isl_basic_set
*isl_basic_map_range(struct isl_basic_map
*bmap
)
5239 is_set
= isl_basic_map_is_set(bmap
);
5244 return isl_basic_map_domain(isl_basic_map_reverse(bmap
));
5246 isl_basic_map_free(bmap
);
5250 __isl_give isl_basic_map
*isl_basic_map_domain_map(
5251 __isl_take isl_basic_map
*bmap
)
5255 isl_basic_map
*domain
;
5256 int nparam
, n_in
, n_out
;
5258 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5259 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5260 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5262 dim
= isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap
)));
5263 domain
= isl_basic_map_universe(dim
);
5265 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
5266 bmap
= isl_basic_map_apply_range(bmap
, domain
);
5267 bmap
= isl_basic_map_extend_constraints(bmap
, n_in
, 0);
5269 for (i
= 0; i
< n_in
; ++i
)
5270 bmap
= isl_basic_map_equate(bmap
, isl_dim_in
, i
,
5273 bmap
= isl_basic_map_gauss(bmap
, NULL
);
5274 return isl_basic_map_finalize(bmap
);
5277 __isl_give isl_basic_map
*isl_basic_map_range_map(
5278 __isl_take isl_basic_map
*bmap
)
5282 isl_basic_map
*range
;
5283 int nparam
, n_in
, n_out
;
5285 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5286 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5287 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5289 dim
= isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap
)));
5290 range
= isl_basic_map_universe(dim
);
5292 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
5293 bmap
= isl_basic_map_apply_range(bmap
, range
);
5294 bmap
= isl_basic_map_extend_constraints(bmap
, n_out
, 0);
5296 for (i
= 0; i
< n_out
; ++i
)
5297 bmap
= isl_basic_map_equate(bmap
, isl_dim_in
, n_in
+ i
,
5300 bmap
= isl_basic_map_gauss(bmap
, NULL
);
5301 return isl_basic_map_finalize(bmap
);
5304 int isl_map_may_be_set(__isl_keep isl_map
*map
)
5308 return isl_space_may_be_set(map
->dim
);
5311 /* Is this map actually a set?
5312 * Users should never call this function. Outside of isl,
5313 * the type should indicate whether something is a set or a map.
5315 isl_bool
isl_map_is_set(__isl_keep isl_map
*map
)
5318 return isl_bool_error
;
5319 return isl_space_is_set(map
->dim
);
5322 struct isl_set
*isl_map_range(struct isl_map
*map
)
5326 struct isl_set
*set
;
5328 is_set
= isl_map_is_set(map
);
5332 return set_from_map(map
);
5334 map
= isl_map_cow(map
);
5338 set
= set_from_map(map
);
5339 set
->dim
= isl_space_range(set
->dim
);
5342 for (i
= 0; i
< map
->n
; ++i
) {
5343 set
->p
[i
] = isl_basic_map_range(map
->p
[i
]);
5347 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
5348 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
5355 __isl_give isl_map
*isl_map_domain_map(__isl_take isl_map
*map
)
5359 map
= isl_map_cow(map
);
5363 map
->dim
= isl_space_domain_map(map
->dim
);
5366 for (i
= 0; i
< map
->n
; ++i
) {
5367 map
->p
[i
] = isl_basic_map_domain_map(map
->p
[i
]);
5371 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
5372 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5379 __isl_give isl_map
*isl_map_range_map(__isl_take isl_map
*map
)
5382 isl_space
*range_dim
;
5384 map
= isl_map_cow(map
);
5388 range_dim
= isl_space_range(isl_map_get_space(map
));
5389 range_dim
= isl_space_from_range(range_dim
);
5390 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
5391 map
->dim
= isl_space_join(map
->dim
, range_dim
);
5394 for (i
= 0; i
< map
->n
; ++i
) {
5395 map
->p
[i
] = isl_basic_map_range_map(map
->p
[i
]);
5399 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
5400 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5407 /* Given a wrapped map of the form A[B -> C],
5408 * return the map A[B -> C] -> B.
5410 __isl_give isl_map
*isl_set_wrapped_domain_map(__isl_take isl_set
*set
)
5417 if (!isl_set_has_tuple_id(set
))
5418 return isl_map_domain_map(isl_set_unwrap(set
));
5420 id
= isl_set_get_tuple_id(set
);
5421 map
= isl_map_domain_map(isl_set_unwrap(set
));
5422 map
= isl_map_set_tuple_id(map
, isl_dim_in
, id
);
5427 __isl_give isl_basic_map
*isl_basic_map_from_domain(
5428 __isl_take isl_basic_set
*bset
)
5430 return isl_basic_map_reverse(isl_basic_map_from_range(bset
));
5433 __isl_give isl_basic_map
*isl_basic_map_from_range(
5434 __isl_take isl_basic_set
*bset
)
5437 space
= isl_basic_set_get_space(bset
);
5438 space
= isl_space_from_range(space
);
5439 bset
= isl_basic_set_reset_space(bset
, space
);
5440 return bset_to_bmap(bset
);
5443 /* Create a relation with the given set as range.
5444 * The domain of the created relation is a zero-dimensional
5445 * flat anonymous space.
5447 __isl_give isl_map
*isl_map_from_range(__isl_take isl_set
*set
)
5450 space
= isl_set_get_space(set
);
5451 space
= isl_space_from_range(space
);
5452 set
= isl_set_reset_space(set
, space
);
5453 return set_to_map(set
);
5456 /* Create a relation with the given set as domain.
5457 * The range of the created relation is a zero-dimensional
5458 * flat anonymous space.
5460 __isl_give isl_map
*isl_map_from_domain(__isl_take isl_set
*set
)
5462 return isl_map_reverse(isl_map_from_range(set
));
5465 __isl_give isl_basic_map
*isl_basic_map_from_domain_and_range(
5466 __isl_take isl_basic_set
*domain
, __isl_take isl_basic_set
*range
)
5468 return isl_basic_map_apply_range(isl_basic_map_reverse(domain
), range
);
5471 __isl_give isl_map
*isl_map_from_domain_and_range(__isl_take isl_set
*domain
,
5472 __isl_take isl_set
*range
)
5474 return isl_map_apply_range(isl_map_reverse(domain
), range
);
5477 /* Return a newly allocated isl_map with given space and flags and
5478 * room for "n" basic maps.
5479 * Make sure that all cached information is cleared.
5481 __isl_give isl_map
*isl_map_alloc_space(__isl_take isl_space
*space
, int n
,
5484 struct isl_map
*map
;
5489 isl_die(space
->ctx
, isl_error_internal
,
5490 "negative number of basic maps", goto error
);
5491 map
= isl_calloc(space
->ctx
, struct isl_map
,
5492 sizeof(struct isl_map
) +
5493 (n
- 1) * sizeof(struct isl_basic_map
*));
5497 map
->ctx
= space
->ctx
;
5498 isl_ctx_ref(map
->ctx
);
5506 isl_space_free(space
);
5510 __isl_give isl_basic_map
*isl_basic_map_empty(__isl_take isl_space
*dim
)
5512 struct isl_basic_map
*bmap
;
5513 bmap
= isl_basic_map_alloc_space(dim
, 0, 1, 0);
5514 bmap
= isl_basic_map_set_to_empty(bmap
);
5518 __isl_give isl_basic_set
*isl_basic_set_empty(__isl_take isl_space
*dim
)
5520 struct isl_basic_set
*bset
;
5521 bset
= isl_basic_set_alloc_space(dim
, 0, 1, 0);
5522 bset
= isl_basic_set_set_to_empty(bset
);
5526 __isl_give isl_basic_map
*isl_basic_map_universe(__isl_take isl_space
*dim
)
5528 struct isl_basic_map
*bmap
;
5529 bmap
= isl_basic_map_alloc_space(dim
, 0, 0, 0);
5530 bmap
= isl_basic_map_finalize(bmap
);
5534 __isl_give isl_basic_set
*isl_basic_set_universe(__isl_take isl_space
*dim
)
5536 struct isl_basic_set
*bset
;
5537 bset
= isl_basic_set_alloc_space(dim
, 0, 0, 0);
5538 bset
= isl_basic_set_finalize(bset
);
5542 __isl_give isl_basic_map
*isl_basic_map_nat_universe(__isl_take isl_space
*dim
)
5545 unsigned total
= isl_space_dim(dim
, isl_dim_all
);
5546 isl_basic_map
*bmap
;
5548 bmap
= isl_basic_map_alloc_space(dim
, 0, 0, total
);
5549 for (i
= 0; i
< total
; ++i
) {
5550 int k
= isl_basic_map_alloc_inequality(bmap
);
5553 isl_seq_clr(bmap
->ineq
[k
], 1 + total
);
5554 isl_int_set_si(bmap
->ineq
[k
][1 + i
], 1);
5558 isl_basic_map_free(bmap
);
5562 __isl_give isl_basic_set
*isl_basic_set_nat_universe(__isl_take isl_space
*dim
)
5564 return isl_basic_map_nat_universe(dim
);
5567 __isl_give isl_map
*isl_map_nat_universe(__isl_take isl_space
*dim
)
5569 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim
));
5572 __isl_give isl_set
*isl_set_nat_universe(__isl_take isl_space
*dim
)
5574 return isl_map_nat_universe(dim
);
5577 __isl_give isl_map
*isl_map_empty(__isl_take isl_space
*dim
)
5579 return isl_map_alloc_space(dim
, 0, ISL_MAP_DISJOINT
);
5582 __isl_give isl_set
*isl_set_empty(__isl_take isl_space
*dim
)
5584 return isl_set_alloc_space(dim
, 0, ISL_MAP_DISJOINT
);
5587 __isl_give isl_map
*isl_map_universe(__isl_take isl_space
*dim
)
5589 struct isl_map
*map
;
5592 map
= isl_map_alloc_space(isl_space_copy(dim
), 1, ISL_MAP_DISJOINT
);
5593 map
= isl_map_add_basic_map(map
, isl_basic_map_universe(dim
));
5597 __isl_give isl_set
*isl_set_universe(__isl_take isl_space
*dim
)
5599 struct isl_set
*set
;
5602 set
= isl_set_alloc_space(isl_space_copy(dim
), 1, ISL_MAP_DISJOINT
);
5603 set
= isl_set_add_basic_set(set
, isl_basic_set_universe(dim
));
5607 struct isl_map
*isl_map_dup(struct isl_map
*map
)
5610 struct isl_map
*dup
;
5614 dup
= isl_map_alloc_space(isl_space_copy(map
->dim
), map
->n
, map
->flags
);
5615 for (i
= 0; i
< map
->n
; ++i
)
5616 dup
= isl_map_add_basic_map(dup
, isl_basic_map_copy(map
->p
[i
]));
5620 __isl_give isl_map
*isl_map_add_basic_map(__isl_take isl_map
*map
,
5621 __isl_take isl_basic_map
*bmap
)
5625 if (isl_basic_map_plain_is_empty(bmap
)) {
5626 isl_basic_map_free(bmap
);
5629 isl_assert(map
->ctx
, isl_space_is_equal(map
->dim
, bmap
->dim
), goto error
);
5630 isl_assert(map
->ctx
, map
->n
< map
->size
, goto error
);
5631 map
->p
[map
->n
] = bmap
;
5633 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5639 isl_basic_map_free(bmap
);
5643 __isl_null isl_map
*isl_map_free(__isl_take isl_map
*map
)
5654 isl_ctx_deref(map
->ctx
);
5655 for (i
= 0; i
< map
->n
; ++i
)
5656 isl_basic_map_free(map
->p
[i
]);
5657 isl_space_free(map
->dim
);
5663 static struct isl_basic_map
*isl_basic_map_fix_pos_si(
5664 struct isl_basic_map
*bmap
, unsigned pos
, int value
)
5668 bmap
= isl_basic_map_cow(bmap
);
5669 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
5670 j
= isl_basic_map_alloc_equality(bmap
);
5673 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
5674 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
5675 isl_int_set_si(bmap
->eq
[j
][0], value
);
5676 bmap
= isl_basic_map_simplify(bmap
);
5677 return isl_basic_map_finalize(bmap
);
5679 isl_basic_map_free(bmap
);
5683 static __isl_give isl_basic_map
*isl_basic_map_fix_pos(
5684 __isl_take isl_basic_map
*bmap
, unsigned pos
, isl_int value
)
5688 bmap
= isl_basic_map_cow(bmap
);
5689 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
5690 j
= isl_basic_map_alloc_equality(bmap
);
5693 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
5694 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
5695 isl_int_set(bmap
->eq
[j
][0], value
);
5696 bmap
= isl_basic_map_simplify(bmap
);
5697 return isl_basic_map_finalize(bmap
);
5699 isl_basic_map_free(bmap
);
5703 struct isl_basic_map
*isl_basic_map_fix_si(struct isl_basic_map
*bmap
,
5704 enum isl_dim_type type
, unsigned pos
, int value
)
5706 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
5707 return isl_basic_map_free(bmap
);
5708 return isl_basic_map_fix_pos_si(bmap
,
5709 isl_basic_map_offset(bmap
, type
) + pos
, value
);
5712 __isl_give isl_basic_map
*isl_basic_map_fix(__isl_take isl_basic_map
*bmap
,
5713 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5715 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
5716 return isl_basic_map_free(bmap
);
5717 return isl_basic_map_fix_pos(bmap
,
5718 isl_basic_map_offset(bmap
, type
) + pos
, value
);
5721 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
5722 * to be equal to "v".
5724 __isl_give isl_basic_map
*isl_basic_map_fix_val(__isl_take isl_basic_map
*bmap
,
5725 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5729 if (!isl_val_is_int(v
))
5730 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
5731 "expecting integer value", goto error
);
5732 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
5734 pos
+= isl_basic_map_offset(bmap
, type
);
5735 bmap
= isl_basic_map_fix_pos(bmap
, pos
, v
->n
);
5739 isl_basic_map_free(bmap
);
5744 /* Fix the value of the variable at position "pos" of type "type" of "bset"
5745 * to be equal to "v".
5747 __isl_give isl_basic_set
*isl_basic_set_fix_val(__isl_take isl_basic_set
*bset
,
5748 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5750 return isl_basic_map_fix_val(bset
, type
, pos
, v
);
5753 struct isl_basic_set
*isl_basic_set_fix_si(struct isl_basic_set
*bset
,
5754 enum isl_dim_type type
, unsigned pos
, int value
)
5756 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset
),
5760 __isl_give isl_basic_set
*isl_basic_set_fix(__isl_take isl_basic_set
*bset
,
5761 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5763 return bset_from_bmap(isl_basic_map_fix(bset_to_bmap(bset
),
5767 struct isl_basic_map
*isl_basic_map_fix_input_si(struct isl_basic_map
*bmap
,
5768 unsigned input
, int value
)
5770 return isl_basic_map_fix_si(bmap
, isl_dim_in
, input
, value
);
5773 struct isl_basic_set
*isl_basic_set_fix_dim_si(struct isl_basic_set
*bset
,
5774 unsigned dim
, int value
)
5776 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset
),
5777 isl_dim_set
, dim
, value
));
5780 static int remove_if_empty(__isl_keep isl_map
*map
, int i
)
5782 int empty
= isl_basic_map_plain_is_empty(map
->p
[i
]);
5789 isl_basic_map_free(map
->p
[i
]);
5790 if (i
!= map
->n
- 1) {
5791 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5792 map
->p
[i
] = map
->p
[map
->n
- 1];
5799 /* Perform "fn" on each basic map of "map", where we may not be holding
5800 * the only reference to "map".
5801 * In particular, "fn" should be a semantics preserving operation
5802 * that we want to apply to all copies of "map". We therefore need
5803 * to be careful not to modify "map" in a way that breaks "map"
5804 * in case anything goes wrong.
5806 __isl_give isl_map
*isl_map_inline_foreach_basic_map(__isl_take isl_map
*map
,
5807 __isl_give isl_basic_map
*(*fn
)(__isl_take isl_basic_map
*bmap
))
5809 struct isl_basic_map
*bmap
;
5815 for (i
= map
->n
- 1; i
>= 0; --i
) {
5816 bmap
= isl_basic_map_copy(map
->p
[i
]);
5820 isl_basic_map_free(map
->p
[i
]);
5822 if (remove_if_empty(map
, i
) < 0)
5832 struct isl_map
*isl_map_fix_si(struct isl_map
*map
,
5833 enum isl_dim_type type
, unsigned pos
, int value
)
5837 map
= isl_map_cow(map
);
5841 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
5842 for (i
= map
->n
- 1; i
>= 0; --i
) {
5843 map
->p
[i
] = isl_basic_map_fix_si(map
->p
[i
], type
, pos
, value
);
5844 if (remove_if_empty(map
, i
) < 0)
5847 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5854 __isl_give isl_set
*isl_set_fix_si(__isl_take isl_set
*set
,
5855 enum isl_dim_type type
, unsigned pos
, int value
)
5857 return set_from_map(isl_map_fix_si(set_to_map(set
), type
, pos
, value
));
5860 __isl_give isl_map
*isl_map_fix(__isl_take isl_map
*map
,
5861 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5865 map
= isl_map_cow(map
);
5869 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
5870 for (i
= 0; i
< map
->n
; ++i
) {
5871 map
->p
[i
] = isl_basic_map_fix(map
->p
[i
], type
, pos
, value
);
5875 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5882 __isl_give isl_set
*isl_set_fix(__isl_take isl_set
*set
,
5883 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5885 return set_from_map(isl_map_fix(set_to_map(set
), type
, pos
, value
));
5888 /* Fix the value of the variable at position "pos" of type "type" of "map"
5889 * to be equal to "v".
5891 __isl_give isl_map
*isl_map_fix_val(__isl_take isl_map
*map
,
5892 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5896 map
= isl_map_cow(map
);
5900 if (!isl_val_is_int(v
))
5901 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
5902 "expecting integer value", goto error
);
5903 if (pos
>= isl_map_dim(map
, type
))
5904 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
5905 "index out of bounds", goto error
);
5906 for (i
= map
->n
- 1; i
>= 0; --i
) {
5907 map
->p
[i
] = isl_basic_map_fix_val(map
->p
[i
], type
, pos
,
5909 if (remove_if_empty(map
, i
) < 0)
5912 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5921 /* Fix the value of the variable at position "pos" of type "type" of "set"
5922 * to be equal to "v".
5924 __isl_give isl_set
*isl_set_fix_val(__isl_take isl_set
*set
,
5925 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5927 return isl_map_fix_val(set
, type
, pos
, v
);
5930 struct isl_map
*isl_map_fix_input_si(struct isl_map
*map
,
5931 unsigned input
, int value
)
5933 return isl_map_fix_si(map
, isl_dim_in
, input
, value
);
5936 struct isl_set
*isl_set_fix_dim_si(struct isl_set
*set
, unsigned dim
, int value
)
5938 return set_from_map(isl_map_fix_si(set_to_map(set
),
5939 isl_dim_set
, dim
, value
));
5942 static __isl_give isl_basic_map
*basic_map_bound_si(
5943 __isl_take isl_basic_map
*bmap
,
5944 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
5948 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
5949 return isl_basic_map_free(bmap
);
5950 pos
+= isl_basic_map_offset(bmap
, type
);
5951 bmap
= isl_basic_map_cow(bmap
);
5952 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
5953 j
= isl_basic_map_alloc_inequality(bmap
);
5956 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
5958 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
5959 isl_int_set_si(bmap
->ineq
[j
][0], value
);
5961 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
5962 isl_int_set_si(bmap
->ineq
[j
][0], -value
);
5964 bmap
= isl_basic_map_simplify(bmap
);
5965 return isl_basic_map_finalize(bmap
);
5967 isl_basic_map_free(bmap
);
5971 __isl_give isl_basic_map
*isl_basic_map_lower_bound_si(
5972 __isl_take isl_basic_map
*bmap
,
5973 enum isl_dim_type type
, unsigned pos
, int value
)
5975 return basic_map_bound_si(bmap
, type
, pos
, value
, 0);
5978 /* Constrain the values of the given dimension to be no greater than "value".
5980 __isl_give isl_basic_map
*isl_basic_map_upper_bound_si(
5981 __isl_take isl_basic_map
*bmap
,
5982 enum isl_dim_type type
, unsigned pos
, int value
)
5984 return basic_map_bound_si(bmap
, type
, pos
, value
, 1);
5987 static __isl_give isl_map
*map_bound_si(__isl_take isl_map
*map
,
5988 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
5992 map
= isl_map_cow(map
);
5996 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
5997 for (i
= 0; i
< map
->n
; ++i
) {
5998 map
->p
[i
] = basic_map_bound_si(map
->p
[i
],
5999 type
, pos
, value
, upper
);
6003 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6010 __isl_give isl_map
*isl_map_lower_bound_si(__isl_take isl_map
*map
,
6011 enum isl_dim_type type
, unsigned pos
, int value
)
6013 return map_bound_si(map
, type
, pos
, value
, 0);
6016 __isl_give isl_map
*isl_map_upper_bound_si(__isl_take isl_map
*map
,
6017 enum isl_dim_type type
, unsigned pos
, int value
)
6019 return map_bound_si(map
, type
, pos
, value
, 1);
6022 __isl_give isl_set
*isl_set_lower_bound_si(__isl_take isl_set
*set
,
6023 enum isl_dim_type type
, unsigned pos
, int value
)
6025 return set_from_map(isl_map_lower_bound_si(set_to_map(set
),
6029 __isl_give isl_set
*isl_set_upper_bound_si(__isl_take isl_set
*set
,
6030 enum isl_dim_type type
, unsigned pos
, int value
)
6032 return isl_map_upper_bound_si(set
, type
, pos
, value
);
6035 /* Bound the given variable of "bmap" from below (or above is "upper"
6036 * is set) to "value".
6038 static __isl_give isl_basic_map
*basic_map_bound(
6039 __isl_take isl_basic_map
*bmap
,
6040 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
6044 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6045 return isl_basic_map_free(bmap
);
6046 pos
+= isl_basic_map_offset(bmap
, type
);
6047 bmap
= isl_basic_map_cow(bmap
);
6048 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
6049 j
= isl_basic_map_alloc_inequality(bmap
);
6052 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
6054 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
6055 isl_int_set(bmap
->ineq
[j
][0], value
);
6057 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
6058 isl_int_neg(bmap
->ineq
[j
][0], value
);
6060 bmap
= isl_basic_map_simplify(bmap
);
6061 return isl_basic_map_finalize(bmap
);
6063 isl_basic_map_free(bmap
);
6067 /* Bound the given variable of "map" from below (or above is "upper"
6068 * is set) to "value".
6070 static __isl_give isl_map
*map_bound(__isl_take isl_map
*map
,
6071 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
6075 map
= isl_map_cow(map
);
6079 if (pos
>= isl_map_dim(map
, type
))
6080 isl_die(map
->ctx
, isl_error_invalid
,
6081 "index out of bounds", goto error
);
6082 for (i
= map
->n
- 1; i
>= 0; --i
) {
6083 map
->p
[i
] = basic_map_bound(map
->p
[i
], type
, pos
, value
, upper
);
6084 if (remove_if_empty(map
, i
) < 0)
6087 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6094 __isl_give isl_map
*isl_map_lower_bound(__isl_take isl_map
*map
,
6095 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6097 return map_bound(map
, type
, pos
, value
, 0);
6100 __isl_give isl_map
*isl_map_upper_bound(__isl_take isl_map
*map
,
6101 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6103 return map_bound(map
, type
, pos
, value
, 1);
6106 __isl_give isl_set
*isl_set_lower_bound(__isl_take isl_set
*set
,
6107 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6109 return isl_map_lower_bound(set
, type
, pos
, value
);
6112 __isl_give isl_set
*isl_set_upper_bound(__isl_take isl_set
*set
,
6113 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6115 return isl_map_upper_bound(set
, type
, pos
, value
);
6118 /* Force the values of the variable at position "pos" of type "type" of "set"
6119 * to be no smaller than "value".
6121 __isl_give isl_set
*isl_set_lower_bound_val(__isl_take isl_set
*set
,
6122 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
6126 if (!isl_val_is_int(value
))
6127 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
6128 "expecting integer value", goto error
);
6129 set
= isl_set_lower_bound(set
, type
, pos
, value
->n
);
6130 isl_val_free(value
);
6133 isl_val_free(value
);
6138 /* Force the values of the variable at position "pos" of type "type" of "set"
6139 * to be no greater than "value".
6141 __isl_give isl_set
*isl_set_upper_bound_val(__isl_take isl_set
*set
,
6142 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
6146 if (!isl_val_is_int(value
))
6147 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
6148 "expecting integer value", goto error
);
6149 set
= isl_set_upper_bound(set
, type
, pos
, value
->n
);
6150 isl_val_free(value
);
6153 isl_val_free(value
);
6158 struct isl_map
*isl_map_reverse(struct isl_map
*map
)
6162 map
= isl_map_cow(map
);
6166 map
->dim
= isl_space_reverse(map
->dim
);
6169 for (i
= 0; i
< map
->n
; ++i
) {
6170 map
->p
[i
] = isl_basic_map_reverse(map
->p
[i
]);
6174 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6182 #define TYPE isl_pw_multi_aff
6184 #define SUFFIX _pw_multi_aff
6186 #define EMPTY isl_pw_multi_aff_empty
6188 #define ADD isl_pw_multi_aff_union_add
6189 #include "isl_map_lexopt_templ.c"
6191 /* Given a map "map", compute the lexicographically minimal
6192 * (or maximal) image element for each domain element in dom,
6193 * in the form of an isl_pw_multi_aff.
6194 * If "empty" is not NULL, then set *empty to those elements in dom that
6195 * do not have an image element.
6196 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6197 * should be computed over the domain of "map". "empty" is also NULL
6200 * We first compute the lexicographically minimal or maximal element
6201 * in the first basic map. This results in a partial solution "res"
6202 * and a subset "todo" of dom that still need to be handled.
6203 * We then consider each of the remaining maps in "map" and successively
6204 * update both "res" and "todo".
6205 * If "empty" is NULL, then the todo sets are not needed and therefore
6206 * also not computed.
6208 static __isl_give isl_pw_multi_aff
*isl_map_partial_lexopt_aligned_pw_multi_aff(
6209 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6210 __isl_give isl_set
**empty
, unsigned flags
)
6214 isl_pw_multi_aff
*res
;
6217 full
= ISL_FL_ISSET(flags
, ISL_OPT_FULL
);
6218 if (!map
|| (!full
&& !dom
))
6221 if (isl_map_plain_is_empty(map
)) {
6226 return isl_pw_multi_aff_from_map(map
);
6229 res
= basic_map_partial_lexopt_pw_multi_aff(
6230 isl_basic_map_copy(map
->p
[0]),
6231 isl_set_copy(dom
), empty
, flags
);
6235 for (i
= 1; i
< map
->n
; ++i
) {
6236 isl_pw_multi_aff
*res_i
;
6238 res_i
= basic_map_partial_lexopt_pw_multi_aff(
6239 isl_basic_map_copy(map
->p
[i
]),
6240 isl_set_copy(dom
), empty
, flags
);
6242 if (ISL_FL_ISSET(flags
, ISL_OPT_MAX
))
6243 res
= isl_pw_multi_aff_union_lexmax(res
, res_i
);
6245 res
= isl_pw_multi_aff_union_lexmin(res
, res_i
);
6248 todo
= isl_set_intersect(todo
, *empty
);
6267 #define TYPE isl_map
6271 #define EMPTY isl_map_empty
6273 #define ADD isl_map_union_disjoint
6274 #include "isl_map_lexopt_templ.c"
6276 /* Given a map "map", compute the lexicographically minimal
6277 * (or maximal) image element for each domain element in "dom",
6278 * in the form of an isl_map.
6279 * If "empty" is not NULL, then set *empty to those elements in "dom" that
6280 * do not have an image element.
6281 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6282 * should be computed over the domain of "map". "empty" is also NULL
6285 * If the input consists of more than one disjunct, then first
6286 * compute the desired result in the form of an isl_pw_multi_aff and
6287 * then convert that into an isl_map.
6289 * This function used to have an explicit implementation in terms
6290 * of isl_maps, but it would continually intersect the domains of
6291 * partial results with the complement of the domain of the next
6292 * partial solution, potentially leading to an explosion in the number
6293 * of disjuncts if there are several disjuncts in the input.
6294 * An even earlier implementation of this function would look for
6295 * better results in the domain of the partial result and for extra
6296 * results in the complement of this domain, which would lead to
6297 * even more splintering.
6299 static __isl_give isl_map
*isl_map_partial_lexopt_aligned(
6300 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6301 __isl_give isl_set
**empty
, unsigned flags
)
6304 struct isl_map
*res
;
6305 isl_pw_multi_aff
*pma
;
6307 full
= ISL_FL_ISSET(flags
, ISL_OPT_FULL
);
6308 if (!map
|| (!full
&& !dom
))
6311 if (isl_map_plain_is_empty(map
)) {
6320 res
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[0]),
6326 pma
= isl_map_partial_lexopt_aligned_pw_multi_aff(map
, dom
, empty
,
6328 return isl_map_from_pw_multi_aff(pma
);
6337 __isl_give isl_map
*isl_map_partial_lexmax(
6338 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6339 __isl_give isl_set
**empty
)
6341 return isl_map_partial_lexopt(map
, dom
, empty
, ISL_OPT_MAX
);
6344 __isl_give isl_map
*isl_map_partial_lexmin(
6345 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6346 __isl_give isl_set
**empty
)
6348 return isl_map_partial_lexopt(map
, dom
, empty
, 0);
6351 __isl_give isl_set
*isl_set_partial_lexmin(
6352 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
6353 __isl_give isl_set
**empty
)
6355 return set_from_map(isl_map_partial_lexmin(set_to_map(set
),
6359 __isl_give isl_set
*isl_set_partial_lexmax(
6360 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
6361 __isl_give isl_set
**empty
)
6363 return set_from_map(isl_map_partial_lexmax(set_to_map(set
),
6367 /* Compute the lexicographic minimum (or maximum if "flags" includes
6368 * ISL_OPT_MAX) of "bset" over its parametric domain.
6370 __isl_give isl_set
*isl_basic_set_lexopt(__isl_take isl_basic_set
*bset
,
6373 return isl_basic_map_lexopt(bset
, flags
);
6376 __isl_give isl_map
*isl_basic_map_lexmax(__isl_take isl_basic_map
*bmap
)
6378 return isl_basic_map_lexopt(bmap
, ISL_OPT_MAX
);
6381 __isl_give isl_set
*isl_basic_set_lexmin(__isl_take isl_basic_set
*bset
)
6383 return set_from_map(isl_basic_map_lexmin(bset_to_bmap(bset
)));
6386 __isl_give isl_set
*isl_basic_set_lexmax(__isl_take isl_basic_set
*bset
)
6388 return set_from_map(isl_basic_map_lexmax(bset_to_bmap(bset
)));
6391 /* Compute the lexicographic minimum of "bset" over its parametric domain
6392 * for the purpose of quantifier elimination.
6393 * That is, find an explicit representation for all the existentially
6394 * quantified variables in "bset" by computing their lexicographic
6397 static __isl_give isl_set
*isl_basic_set_lexmin_compute_divs(
6398 __isl_take isl_basic_set
*bset
)
6400 return isl_basic_set_lexopt(bset
, ISL_OPT_QE
);
6403 /* Extract the first and only affine expression from list
6404 * and then add it to *pwaff with the given dom.
6405 * This domain is known to be disjoint from other domains
6406 * because of the way isl_basic_map_foreach_lexmax works.
6408 static isl_stat
update_dim_opt(__isl_take isl_basic_set
*dom
,
6409 __isl_take isl_aff_list
*list
, void *user
)
6411 isl_ctx
*ctx
= isl_basic_set_get_ctx(dom
);
6413 isl_pw_aff
**pwaff
= user
;
6414 isl_pw_aff
*pwaff_i
;
6418 if (isl_aff_list_n_aff(list
) != 1)
6419 isl_die(ctx
, isl_error_internal
,
6420 "expecting single element list", goto error
);
6422 aff
= isl_aff_list_get_aff(list
, 0);
6423 pwaff_i
= isl_pw_aff_alloc(isl_set_from_basic_set(dom
), aff
);
6425 *pwaff
= isl_pw_aff_add_disjoint(*pwaff
, pwaff_i
);
6427 isl_aff_list_free(list
);
6431 isl_basic_set_free(dom
);
6432 isl_aff_list_free(list
);
6433 return isl_stat_error
;
6436 /* Given a basic map with one output dimension, compute the minimum or
6437 * maximum of that dimension as an isl_pw_aff.
6439 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
6440 * call update_dim_opt on each leaf of the result.
6442 static __isl_give isl_pw_aff
*basic_map_dim_opt(__isl_keep isl_basic_map
*bmap
,
6445 isl_space
*dim
= isl_basic_map_get_space(bmap
);
6449 dim
= isl_space_from_domain(isl_space_domain(dim
));
6450 dim
= isl_space_add_dims(dim
, isl_dim_out
, 1);
6451 pwaff
= isl_pw_aff_empty(dim
);
6453 r
= isl_basic_map_foreach_lexopt(bmap
, max
, &update_dim_opt
, &pwaff
);
6455 return isl_pw_aff_free(pwaff
);
6460 /* Compute the minimum or maximum of the given output dimension
6461 * as a function of the parameters and the input dimensions,
6462 * but independently of the other output dimensions.
6464 * We first project out the other output dimension and then compute
6465 * the "lexicographic" maximum in each basic map, combining the results
6466 * using isl_pw_aff_union_max.
6468 static __isl_give isl_pw_aff
*map_dim_opt(__isl_take isl_map
*map
, int pos
,
6475 n_out
= isl_map_dim(map
, isl_dim_out
);
6476 map
= isl_map_project_out(map
, isl_dim_out
, pos
+ 1, n_out
- (pos
+ 1));
6477 map
= isl_map_project_out(map
, isl_dim_out
, 0, pos
);
6482 isl_space
*dim
= isl_map_get_space(map
);
6484 return isl_pw_aff_empty(dim
);
6487 pwaff
= basic_map_dim_opt(map
->p
[0], max
);
6488 for (i
= 1; i
< map
->n
; ++i
) {
6489 isl_pw_aff
*pwaff_i
;
6491 pwaff_i
= basic_map_dim_opt(map
->p
[i
], max
);
6492 pwaff
= isl_pw_aff_union_opt(pwaff
, pwaff_i
, max
);
6500 /* Compute the minimum of the given output dimension as a function of the
6501 * parameters and input dimensions, but independently of
6502 * the other output dimensions.
6504 __isl_give isl_pw_aff
*isl_map_dim_min(__isl_take isl_map
*map
, int pos
)
6506 return map_dim_opt(map
, pos
, 0);
6509 /* Compute the maximum of the given output dimension as a function of the
6510 * parameters and input dimensions, but independently of
6511 * the other output dimensions.
6513 __isl_give isl_pw_aff
*isl_map_dim_max(__isl_take isl_map
*map
, int pos
)
6515 return map_dim_opt(map
, pos
, 1);
6518 /* Compute the minimum or maximum of the given set dimension
6519 * as a function of the parameters,
6520 * but independently of the other set dimensions.
6522 static __isl_give isl_pw_aff
*set_dim_opt(__isl_take isl_set
*set
, int pos
,
6525 return map_dim_opt(set
, pos
, max
);
6528 /* Compute the maximum of the given set dimension as a function of the
6529 * parameters, but independently of the other set dimensions.
6531 __isl_give isl_pw_aff
*isl_set_dim_max(__isl_take isl_set
*set
, int pos
)
6533 return set_dim_opt(set
, pos
, 1);
6536 /* Compute the minimum of the given set dimension as a function of the
6537 * parameters, but independently of the other set dimensions.
6539 __isl_give isl_pw_aff
*isl_set_dim_min(__isl_take isl_set
*set
, int pos
)
6541 return set_dim_opt(set
, pos
, 0);
6544 /* Apply a preimage specified by "mat" on the parameters of "bset".
6545 * bset is assumed to have only parameters and divs.
6547 static struct isl_basic_set
*basic_set_parameter_preimage(
6548 struct isl_basic_set
*bset
, struct isl_mat
*mat
)
6555 bset
->dim
= isl_space_cow(bset
->dim
);
6559 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
6561 isl_assert(bset
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
6563 bset
->dim
->nparam
= 0;
6564 bset
->dim
->n_out
= nparam
;
6565 bset
= isl_basic_set_preimage(bset
, mat
);
6567 bset
->dim
->nparam
= bset
->dim
->n_out
;
6568 bset
->dim
->n_out
= 0;
6573 isl_basic_set_free(bset
);
6577 /* Apply a preimage specified by "mat" on the parameters of "set".
6578 * set is assumed to have only parameters and divs.
6580 static __isl_give isl_set
*set_parameter_preimage(__isl_take isl_set
*set
,
6581 __isl_take isl_mat
*mat
)
6589 nparam
= isl_set_dim(set
, isl_dim_param
);
6591 if (mat
->n_row
!= 1 + nparam
)
6592 isl_die(isl_set_get_ctx(set
), isl_error_internal
,
6593 "unexpected number of rows", goto error
);
6595 space
= isl_set_get_space(set
);
6596 space
= isl_space_move_dims(space
, isl_dim_set
, 0,
6597 isl_dim_param
, 0, nparam
);
6598 set
= isl_set_reset_space(set
, space
);
6599 set
= isl_set_preimage(set
, mat
);
6600 nparam
= isl_set_dim(set
, isl_dim_out
);
6601 space
= isl_set_get_space(set
);
6602 space
= isl_space_move_dims(space
, isl_dim_param
, 0,
6603 isl_dim_out
, 0, nparam
);
6604 set
= isl_set_reset_space(set
, space
);
6612 /* Intersect the basic set "bset" with the affine space specified by the
6613 * equalities in "eq".
6615 static struct isl_basic_set
*basic_set_append_equalities(
6616 struct isl_basic_set
*bset
, struct isl_mat
*eq
)
6624 bset
= isl_basic_set_extend_space(bset
, isl_space_copy(bset
->dim
), 0,
6629 len
= 1 + isl_space_dim(bset
->dim
, isl_dim_all
) + bset
->extra
;
6630 for (i
= 0; i
< eq
->n_row
; ++i
) {
6631 k
= isl_basic_set_alloc_equality(bset
);
6634 isl_seq_cpy(bset
->eq
[k
], eq
->row
[i
], eq
->n_col
);
6635 isl_seq_clr(bset
->eq
[k
] + eq
->n_col
, len
- eq
->n_col
);
6639 bset
= isl_basic_set_gauss(bset
, NULL
);
6640 bset
= isl_basic_set_finalize(bset
);
6645 isl_basic_set_free(bset
);
6649 /* Intersect the set "set" with the affine space specified by the
6650 * equalities in "eq".
6652 static struct isl_set
*set_append_equalities(struct isl_set
*set
,
6660 for (i
= 0; i
< set
->n
; ++i
) {
6661 set
->p
[i
] = basic_set_append_equalities(set
->p
[i
],
6674 /* Given a basic set "bset" that only involves parameters and existentially
6675 * quantified variables, return the index of the first equality
6676 * that only involves parameters. If there is no such equality then
6677 * return bset->n_eq.
6679 * This function assumes that isl_basic_set_gauss has been called on "bset".
6681 static int first_parameter_equality(__isl_keep isl_basic_set
*bset
)
6684 unsigned nparam
, n_div
;
6689 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
6690 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
6692 for (i
= 0, j
= n_div
- 1; i
< bset
->n_eq
&& j
>= 0; --j
) {
6693 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ j
]))
6700 /* Compute an explicit representation for the existentially quantified
6701 * variables in "bset" by computing the "minimal value" of the set
6702 * variables. Since there are no set variables, the computation of
6703 * the minimal value essentially computes an explicit representation
6704 * of the non-empty part(s) of "bset".
6706 * The input only involves parameters and existentially quantified variables.
6707 * All equalities among parameters have been removed.
6709 * Since the existentially quantified variables in the result are in general
6710 * going to be different from those in the input, we first replace
6711 * them by the minimal number of variables based on their equalities.
6712 * This should simplify the parametric integer programming.
6714 static __isl_give isl_set
*base_compute_divs(__isl_take isl_basic_set
*bset
)
6716 isl_morph
*morph1
, *morph2
;
6722 if (bset
->n_eq
== 0)
6723 return isl_basic_set_lexmin_compute_divs(bset
);
6725 morph1
= isl_basic_set_parameter_compression(bset
);
6726 bset
= isl_morph_basic_set(isl_morph_copy(morph1
), bset
);
6727 bset
= isl_basic_set_lift(bset
);
6728 morph2
= isl_basic_set_variable_compression(bset
, isl_dim_set
);
6729 bset
= isl_morph_basic_set(morph2
, bset
);
6730 n
= isl_basic_set_dim(bset
, isl_dim_set
);
6731 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
6733 set
= isl_basic_set_lexmin_compute_divs(bset
);
6735 set
= isl_morph_set(isl_morph_inverse(morph1
), set
);
6740 /* Project the given basic set onto its parameter domain, possibly introducing
6741 * new, explicit, existential variables in the constraints.
6742 * The input has parameters and (possibly implicit) existential variables.
6743 * The output has the same parameters, but only
6744 * explicit existentially quantified variables.
6746 * The actual projection is performed by pip, but pip doesn't seem
6747 * to like equalities very much, so we first remove the equalities
6748 * among the parameters by performing a variable compression on
6749 * the parameters. Afterward, an inverse transformation is performed
6750 * and the equalities among the parameters are inserted back in.
6752 * The variable compression on the parameters may uncover additional
6753 * equalities that were only implicit before. We therefore check
6754 * if there are any new parameter equalities in the result and
6755 * if so recurse. The removal of parameter equalities is required
6756 * for the parameter compression performed by base_compute_divs.
6758 static struct isl_set
*parameter_compute_divs(struct isl_basic_set
*bset
)
6762 struct isl_mat
*T
, *T2
;
6763 struct isl_set
*set
;
6766 bset
= isl_basic_set_cow(bset
);
6770 if (bset
->n_eq
== 0)
6771 return base_compute_divs(bset
);
6773 bset
= isl_basic_set_gauss(bset
, NULL
);
6776 if (isl_basic_set_plain_is_empty(bset
))
6777 return isl_set_from_basic_set(bset
);
6779 i
= first_parameter_equality(bset
);
6780 if (i
== bset
->n_eq
)
6781 return base_compute_divs(bset
);
6783 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
6784 eq
= isl_mat_sub_alloc6(bset
->ctx
, bset
->eq
, i
, bset
->n_eq
- i
,
6786 eq
= isl_mat_cow(eq
);
6787 T
= isl_mat_variable_compression(isl_mat_copy(eq
), &T2
);
6788 if (T
&& T
->n_col
== 0) {
6792 bset
= isl_basic_set_set_to_empty(bset
);
6793 return isl_set_from_basic_set(bset
);
6795 bset
= basic_set_parameter_preimage(bset
, T
);
6797 i
= first_parameter_equality(bset
);
6800 else if (i
== bset
->n_eq
)
6801 set
= base_compute_divs(bset
);
6803 set
= parameter_compute_divs(bset
);
6804 set
= set_parameter_preimage(set
, T2
);
6805 set
= set_append_equalities(set
, eq
);
6809 /* Insert the divs from "ls" before those of "bmap".
6811 * The number of columns is not changed, which means that the last
6812 * dimensions of "bmap" are being reintepreted as the divs from "ls".
6813 * The caller is responsible for removing the same number of dimensions
6814 * from the space of "bmap".
6816 static __isl_give isl_basic_map
*insert_divs_from_local_space(
6817 __isl_take isl_basic_map
*bmap
, __isl_keep isl_local_space
*ls
)
6823 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
6827 old_n_div
= bmap
->n_div
;
6828 bmap
= insert_div_rows(bmap
, n_div
);
6832 for (i
= 0; i
< n_div
; ++i
) {
6833 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], ls
->div
->n_col
);
6834 isl_seq_clr(bmap
->div
[i
] + ls
->div
->n_col
, old_n_div
);
6840 /* Replace the space of "bmap" by the space and divs of "ls".
6842 * If "ls" has any divs, then we simplify the result since we may
6843 * have discovered some additional equalities that could simplify
6844 * the div expressions.
6846 static __isl_give isl_basic_map
*basic_replace_space_by_local_space(
6847 __isl_take isl_basic_map
*bmap
, __isl_take isl_local_space
*ls
)
6851 bmap
= isl_basic_map_cow(bmap
);
6855 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
6856 bmap
= insert_divs_from_local_space(bmap
, ls
);
6860 isl_space_free(bmap
->dim
);
6861 bmap
->dim
= isl_local_space_get_space(ls
);
6865 isl_local_space_free(ls
);
6867 bmap
= isl_basic_map_simplify(bmap
);
6868 bmap
= isl_basic_map_finalize(bmap
);
6871 isl_basic_map_free(bmap
);
6872 isl_local_space_free(ls
);
6876 /* Replace the space of "map" by the space and divs of "ls".
6878 static __isl_give isl_map
*replace_space_by_local_space(__isl_take isl_map
*map
,
6879 __isl_take isl_local_space
*ls
)
6883 map
= isl_map_cow(map
);
6887 for (i
= 0; i
< map
->n
; ++i
) {
6888 map
->p
[i
] = basic_replace_space_by_local_space(map
->p
[i
],
6889 isl_local_space_copy(ls
));
6893 isl_space_free(map
->dim
);
6894 map
->dim
= isl_local_space_get_space(ls
);
6898 isl_local_space_free(ls
);
6901 isl_local_space_free(ls
);
6906 /* Compute an explicit representation for the existentially
6907 * quantified variables for which do not know any explicit representation yet.
6909 * We first sort the existentially quantified variables so that the
6910 * existentially quantified variables for which we already have an explicit
6911 * representation are placed before those for which we do not.
6912 * The input dimensions, the output dimensions and the existentially
6913 * quantified variables for which we already have an explicit
6914 * representation are then turned into parameters.
6915 * compute_divs returns a map with the same parameters and
6916 * no input or output dimensions and the dimension specification
6917 * is reset to that of the input, including the existentially quantified
6918 * variables for which we already had an explicit representation.
6920 static struct isl_map
*compute_divs(struct isl_basic_map
*bmap
)
6922 struct isl_basic_set
*bset
;
6923 struct isl_set
*set
;
6924 struct isl_map
*map
;
6926 isl_local_space
*ls
;
6933 bmap
= isl_basic_map_sort_divs(bmap
);
6934 bmap
= isl_basic_map_cow(bmap
);
6938 n_known
= isl_basic_map_first_unknown_div(bmap
);
6940 return isl_map_from_basic_map(isl_basic_map_free(bmap
));
6942 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
6943 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
6944 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
6945 dim
= isl_space_set_alloc(bmap
->ctx
,
6946 nparam
+ n_in
+ n_out
+ n_known
, 0);
6950 ls
= isl_basic_map_get_local_space(bmap
);
6951 ls
= isl_local_space_drop_dims(ls
, isl_dim_div
,
6952 n_known
, bmap
->n_div
- n_known
);
6954 for (i
= n_known
; i
< bmap
->n_div
; ++i
)
6955 swap_div(bmap
, i
- n_known
, i
);
6956 bmap
->n_div
-= n_known
;
6957 bmap
->extra
-= n_known
;
6959 bmap
= isl_basic_map_reset_space(bmap
, dim
);
6960 bset
= bset_from_bmap(bmap
);
6962 set
= parameter_compute_divs(bset
);
6963 map
= set_to_map(set
);
6964 map
= replace_space_by_local_space(map
, ls
);
6968 isl_basic_map_free(bmap
);
6972 /* Remove the explicit representation of local variable "div",
6975 __isl_give isl_basic_map
*isl_basic_map_mark_div_unknown(
6976 __isl_take isl_basic_map
*bmap
, int div
)
6980 unknown
= isl_basic_map_div_is_marked_unknown(bmap
, div
);
6982 return isl_basic_map_free(bmap
);
6986 bmap
= isl_basic_map_cow(bmap
);
6989 isl_int_set_si(bmap
->div
[div
][0], 0);
6993 /* Is local variable "div" of "bmap" marked as not having an explicit
6995 * Note that even if "div" is not marked in this way and therefore
6996 * has an explicit representation, this representation may still
6997 * depend (indirectly) on other local variables that do not
6998 * have an explicit representation.
7000 isl_bool
isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map
*bmap
,
7003 if (isl_basic_map_check_range(bmap
, isl_dim_div
, div
, 1) < 0)
7004 return isl_bool_error
;
7005 return isl_int_is_zero(bmap
->div
[div
][0]);
7008 /* Return the position of the first local variable that does not
7009 * have an explicit representation.
7010 * Return the total number of local variables if they all have
7011 * an explicit representation.
7012 * Return -1 on error.
7014 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map
*bmap
)
7021 for (i
= 0; i
< bmap
->n_div
; ++i
) {
7022 if (!isl_basic_map_div_is_known(bmap
, i
))
7028 /* Return the position of the first local variable that does not
7029 * have an explicit representation.
7030 * Return the total number of local variables if they all have
7031 * an explicit representation.
7032 * Return -1 on error.
7034 int isl_basic_set_first_unknown_div(__isl_keep isl_basic_set
*bset
)
7036 return isl_basic_map_first_unknown_div(bset
);
7039 /* Does "bmap" have an explicit representation for all local variables?
7041 isl_bool
isl_basic_map_divs_known(__isl_keep isl_basic_map
*bmap
)
7045 n
= isl_basic_map_dim(bmap
, isl_dim_div
);
7046 first
= isl_basic_map_first_unknown_div(bmap
);
7048 return isl_bool_error
;
7052 /* Do all basic maps in "map" have an explicit representation
7053 * for all local variables?
7055 isl_bool
isl_map_divs_known(__isl_keep isl_map
*map
)
7060 return isl_bool_error
;
7062 for (i
= 0; i
< map
->n
; ++i
) {
7063 int known
= isl_basic_map_divs_known(map
->p
[i
]);
7068 return isl_bool_true
;
7071 /* If bmap contains any unknown divs, then compute explicit
7072 * expressions for them. However, this computation may be
7073 * quite expensive, so first try to remove divs that aren't
7076 struct isl_map
*isl_basic_map_compute_divs(struct isl_basic_map
*bmap
)
7079 struct isl_map
*map
;
7081 known
= isl_basic_map_divs_known(bmap
);
7085 return isl_map_from_basic_map(bmap
);
7087 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
7089 known
= isl_basic_map_divs_known(bmap
);
7093 return isl_map_from_basic_map(bmap
);
7095 map
= compute_divs(bmap
);
7098 isl_basic_map_free(bmap
);
7102 struct isl_map
*isl_map_compute_divs(struct isl_map
*map
)
7106 struct isl_map
*res
;
7113 known
= isl_map_divs_known(map
);
7121 res
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[0]));
7122 for (i
= 1 ; i
< map
->n
; ++i
) {
7124 r2
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[i
]));
7125 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
7126 res
= isl_map_union_disjoint(res
, r2
);
7128 res
= isl_map_union(res
, r2
);
7135 struct isl_set
*isl_basic_set_compute_divs(struct isl_basic_set
*bset
)
7137 return set_from_map(isl_basic_map_compute_divs(bset_to_bmap(bset
)));
7140 struct isl_set
*isl_set_compute_divs(struct isl_set
*set
)
7142 return set_from_map(isl_map_compute_divs(set_to_map(set
)));
7145 struct isl_set
*isl_map_domain(struct isl_map
*map
)
7148 struct isl_set
*set
;
7153 map
= isl_map_cow(map
);
7157 set
= set_from_map(map
);
7158 set
->dim
= isl_space_domain(set
->dim
);
7161 for (i
= 0; i
< map
->n
; ++i
) {
7162 set
->p
[i
] = isl_basic_map_domain(map
->p
[i
]);
7166 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
7167 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
7174 /* Return the union of "map1" and "map2", where we assume for now that
7175 * "map1" and "map2" are disjoint. Note that the basic maps inside
7176 * "map1" or "map2" may not be disjoint from each other.
7177 * Also note that this function is also called from isl_map_union,
7178 * which takes care of handling the situation where "map1" and "map2"
7179 * may not be disjoint.
7181 * If one of the inputs is empty, we can simply return the other input.
7182 * Similarly, if one of the inputs is universal, then it is equal to the union.
7184 static __isl_give isl_map
*map_union_disjoint(__isl_take isl_map
*map1
,
7185 __isl_take isl_map
*map2
)
7189 struct isl_map
*map
= NULL
;
7195 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
7196 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
7197 "spaces don't match", goto error
);
7208 is_universe
= isl_map_plain_is_universe(map1
);
7209 if (is_universe
< 0)
7216 is_universe
= isl_map_plain_is_universe(map2
);
7217 if (is_universe
< 0)
7224 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
7225 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
7226 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
7228 map
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
7229 map1
->n
+ map2
->n
, flags
);
7232 for (i
= 0; i
< map1
->n
; ++i
) {
7233 map
= isl_map_add_basic_map(map
,
7234 isl_basic_map_copy(map1
->p
[i
]));
7238 for (i
= 0; i
< map2
->n
; ++i
) {
7239 map
= isl_map_add_basic_map(map
,
7240 isl_basic_map_copy(map2
->p
[i
]));
7254 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7255 * guaranteed to be disjoint by the caller.
7257 * Note that this functions is called from within isl_map_make_disjoint,
7258 * so we have to be careful not to touch the constraints of the inputs
7261 __isl_give isl_map
*isl_map_union_disjoint(__isl_take isl_map
*map1
,
7262 __isl_take isl_map
*map2
)
7264 return isl_map_align_params_map_map_and(map1
, map2
, &map_union_disjoint
);
7267 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7268 * not be disjoint. The parameters are assumed to have been aligned.
7270 * We currently simply call map_union_disjoint, the internal operation
7271 * of which does not really depend on the inputs being disjoint.
7272 * If the result contains more than one basic map, then we clear
7273 * the disjoint flag since the result may contain basic maps from
7274 * both inputs and these are not guaranteed to be disjoint.
7276 * As a special case, if "map1" and "map2" are obviously equal,
7277 * then we simply return "map1".
7279 static __isl_give isl_map
*map_union_aligned(__isl_take isl_map
*map1
,
7280 __isl_take isl_map
*map2
)
7287 equal
= isl_map_plain_is_equal(map1
, map2
);
7295 map1
= map_union_disjoint(map1
, map2
);
7299 ISL_F_CLR(map1
, ISL_MAP_DISJOINT
);
7307 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7310 __isl_give isl_map
*isl_map_union(__isl_take isl_map
*map1
,
7311 __isl_take isl_map
*map2
)
7313 return isl_map_align_params_map_map_and(map1
, map2
, &map_union_aligned
);
7316 struct isl_set
*isl_set_union_disjoint(
7317 struct isl_set
*set1
, struct isl_set
*set2
)
7319 return set_from_map(isl_map_union_disjoint(set_to_map(set1
),
7323 struct isl_set
*isl_set_union(struct isl_set
*set1
, struct isl_set
*set2
)
7325 return set_from_map(isl_map_union(set_to_map(set1
), set_to_map(set2
)));
7328 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7331 * "map" and "set" are assumed to be compatible and non-NULL.
7333 static __isl_give isl_map
*map_intersect_set(__isl_take isl_map
*map
,
7334 __isl_take isl_set
*set
,
7335 __isl_give isl_basic_map
*fn(__isl_take isl_basic_map
*bmap
,
7336 __isl_take isl_basic_set
*bset
))
7339 struct isl_map
*result
;
7342 if (isl_set_plain_is_universe(set
)) {
7347 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
) &&
7348 ISL_F_ISSET(set
, ISL_MAP_DISJOINT
))
7349 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
7351 result
= isl_map_alloc_space(isl_space_copy(map
->dim
),
7352 map
->n
* set
->n
, flags
);
7353 for (i
= 0; result
&& i
< map
->n
; ++i
)
7354 for (j
= 0; j
< set
->n
; ++j
) {
7355 result
= isl_map_add_basic_map(result
,
7356 fn(isl_basic_map_copy(map
->p
[i
]),
7357 isl_basic_set_copy(set
->p
[j
])));
7367 static __isl_give isl_map
*map_intersect_range(__isl_take isl_map
*map
,
7368 __isl_take isl_set
*set
)
7372 ok
= isl_map_compatible_range(map
, set
);
7376 isl_die(set
->ctx
, isl_error_invalid
,
7377 "incompatible spaces", goto error
);
7379 return map_intersect_set(map
, set
, &isl_basic_map_intersect_range
);
7386 __isl_give isl_map
*isl_map_intersect_range(__isl_take isl_map
*map
,
7387 __isl_take isl_set
*set
)
7389 return isl_map_align_params_map_map_and(map
, set
, &map_intersect_range
);
7392 static __isl_give isl_map
*map_intersect_domain(__isl_take isl_map
*map
,
7393 __isl_take isl_set
*set
)
7397 ok
= isl_map_compatible_domain(map
, set
);
7401 isl_die(set
->ctx
, isl_error_invalid
,
7402 "incompatible spaces", goto error
);
7404 return map_intersect_set(map
, set
, &isl_basic_map_intersect_domain
);
7411 __isl_give isl_map
*isl_map_intersect_domain(__isl_take isl_map
*map
,
7412 __isl_take isl_set
*set
)
7414 return isl_map_align_params_map_map_and(map
, set
,
7415 &map_intersect_domain
);
7418 static __isl_give isl_map
*map_apply_domain(__isl_take isl_map
*map1
,
7419 __isl_take isl_map
*map2
)
7423 map1
= isl_map_reverse(map1
);
7424 map1
= isl_map_apply_range(map1
, map2
);
7425 return isl_map_reverse(map1
);
7432 __isl_give isl_map
*isl_map_apply_domain(__isl_take isl_map
*map1
,
7433 __isl_take isl_map
*map2
)
7435 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_domain
);
7438 static __isl_give isl_map
*map_apply_range(__isl_take isl_map
*map1
,
7439 __isl_take isl_map
*map2
)
7441 isl_space
*dim_result
;
7442 struct isl_map
*result
;
7448 dim_result
= isl_space_join(isl_space_copy(map1
->dim
),
7449 isl_space_copy(map2
->dim
));
7451 result
= isl_map_alloc_space(dim_result
, map1
->n
* map2
->n
, 0);
7454 for (i
= 0; i
< map1
->n
; ++i
)
7455 for (j
= 0; j
< map2
->n
; ++j
) {
7456 result
= isl_map_add_basic_map(result
,
7457 isl_basic_map_apply_range(
7458 isl_basic_map_copy(map1
->p
[i
]),
7459 isl_basic_map_copy(map2
->p
[j
])));
7465 if (result
&& result
->n
<= 1)
7466 ISL_F_SET(result
, ISL_MAP_DISJOINT
);
7474 __isl_give isl_map
*isl_map_apply_range(__isl_take isl_map
*map1
,
7475 __isl_take isl_map
*map2
)
7477 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_range
);
7481 * returns range - domain
7483 struct isl_basic_set
*isl_basic_map_deltas(struct isl_basic_map
*bmap
)
7485 isl_space
*target_space
;
7486 struct isl_basic_set
*bset
;
7493 isl_assert(bmap
->ctx
, isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
7494 bmap
->dim
, isl_dim_out
),
7496 target_space
= isl_space_domain(isl_basic_map_get_space(bmap
));
7497 dim
= isl_basic_map_n_in(bmap
);
7498 nparam
= isl_basic_map_n_param(bmap
);
7499 bmap
= isl_basic_map_from_range(isl_basic_map_wrap(bmap
));
7500 bmap
= isl_basic_map_add_dims(bmap
, isl_dim_in
, dim
);
7501 bmap
= isl_basic_map_extend_constraints(bmap
, dim
, 0);
7502 for (i
= 0; i
< dim
; ++i
) {
7503 int j
= isl_basic_map_alloc_equality(bmap
);
7505 bmap
= isl_basic_map_free(bmap
);
7508 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
7509 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
7510 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], 1);
7511 isl_int_set_si(bmap
->eq
[j
][1+nparam
+2*dim
+i
], -1);
7513 bset
= isl_basic_map_domain(bmap
);
7514 bset
= isl_basic_set_reset_space(bset
, target_space
);
7517 isl_basic_map_free(bmap
);
7522 * returns range - domain
7524 __isl_give isl_set
*isl_map_deltas(__isl_take isl_map
*map
)
7528 struct isl_set
*result
;
7533 isl_assert(map
->ctx
, isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
7534 map
->dim
, isl_dim_out
),
7536 dim
= isl_map_get_space(map
);
7537 dim
= isl_space_domain(dim
);
7538 result
= isl_set_alloc_space(dim
, map
->n
, 0);
7541 for (i
= 0; i
< map
->n
; ++i
)
7542 result
= isl_set_add_basic_set(result
,
7543 isl_basic_map_deltas(isl_basic_map_copy(map
->p
[i
])));
7552 * returns [domain -> range] -> range - domain
7554 __isl_give isl_basic_map
*isl_basic_map_deltas_map(
7555 __isl_take isl_basic_map
*bmap
)
7559 isl_basic_map
*domain
;
7563 if (!isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
7564 bmap
->dim
, isl_dim_out
))
7565 isl_die(bmap
->ctx
, isl_error_invalid
,
7566 "domain and range don't match", goto error
);
7568 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
7569 n
= isl_basic_map_dim(bmap
, isl_dim_in
);
7571 dim
= isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap
)));
7572 domain
= isl_basic_map_universe(dim
);
7574 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
7575 bmap
= isl_basic_map_apply_range(bmap
, domain
);
7576 bmap
= isl_basic_map_extend_constraints(bmap
, n
, 0);
7578 total
= isl_basic_map_total_dim(bmap
);
7580 for (i
= 0; i
< n
; ++i
) {
7581 k
= isl_basic_map_alloc_equality(bmap
);
7584 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
7585 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], 1);
7586 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ i
], -1);
7587 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ n
+ i
], 1);
7590 bmap
= isl_basic_map_gauss(bmap
, NULL
);
7591 return isl_basic_map_finalize(bmap
);
7593 isl_basic_map_free(bmap
);
7598 * returns [domain -> range] -> range - domain
7600 __isl_give isl_map
*isl_map_deltas_map(__isl_take isl_map
*map
)
7603 isl_space
*domain_dim
;
7608 if (!isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
7609 map
->dim
, isl_dim_out
))
7610 isl_die(map
->ctx
, isl_error_invalid
,
7611 "domain and range don't match", goto error
);
7613 map
= isl_map_cow(map
);
7617 domain_dim
= isl_space_from_range(isl_space_domain(isl_map_get_space(map
)));
7618 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
7619 map
->dim
= isl_space_join(map
->dim
, domain_dim
);
7622 for (i
= 0; i
< map
->n
; ++i
) {
7623 map
->p
[i
] = isl_basic_map_deltas_map(map
->p
[i
]);
7627 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
7634 static __isl_give isl_basic_map
*basic_map_identity(__isl_take isl_space
*dims
)
7636 struct isl_basic_map
*bmap
;
7644 nparam
= dims
->nparam
;
7646 bmap
= isl_basic_map_alloc_space(dims
, 0, dim
, 0);
7650 for (i
= 0; i
< dim
; ++i
) {
7651 int j
= isl_basic_map_alloc_equality(bmap
);
7654 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
7655 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
7656 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], -1);
7658 return isl_basic_map_finalize(bmap
);
7660 isl_basic_map_free(bmap
);
7664 __isl_give isl_basic_map
*isl_basic_map_identity(__isl_take isl_space
*dim
)
7668 if (dim
->n_in
!= dim
->n_out
)
7669 isl_die(dim
->ctx
, isl_error_invalid
,
7670 "number of input and output dimensions needs to be "
7671 "the same", goto error
);
7672 return basic_map_identity(dim
);
7674 isl_space_free(dim
);
7678 __isl_give isl_map
*isl_map_identity(__isl_take isl_space
*dim
)
7680 return isl_map_from_basic_map(isl_basic_map_identity(dim
));
7683 __isl_give isl_map
*isl_set_identity(__isl_take isl_set
*set
)
7685 isl_space
*dim
= isl_set_get_space(set
);
7687 id
= isl_map_identity(isl_space_map_from_set(dim
));
7688 return isl_map_intersect_range(id
, set
);
7691 /* Construct a basic set with all set dimensions having only non-negative
7694 __isl_give isl_basic_set
*isl_basic_set_positive_orthant(
7695 __isl_take isl_space
*space
)
7700 struct isl_basic_set
*bset
;
7704 nparam
= space
->nparam
;
7706 bset
= isl_basic_set_alloc_space(space
, 0, 0, dim
);
7709 for (i
= 0; i
< dim
; ++i
) {
7710 int k
= isl_basic_set_alloc_inequality(bset
);
7713 isl_seq_clr(bset
->ineq
[k
], 1 + isl_basic_set_total_dim(bset
));
7714 isl_int_set_si(bset
->ineq
[k
][1 + nparam
+ i
], 1);
7718 isl_basic_set_free(bset
);
7722 /* Construct the half-space x_pos >= 0.
7724 static __isl_give isl_basic_set
*nonneg_halfspace(__isl_take isl_space
*dim
,
7728 isl_basic_set
*nonneg
;
7730 nonneg
= isl_basic_set_alloc_space(dim
, 0, 0, 1);
7731 k
= isl_basic_set_alloc_inequality(nonneg
);
7734 isl_seq_clr(nonneg
->ineq
[k
], 1 + isl_basic_set_total_dim(nonneg
));
7735 isl_int_set_si(nonneg
->ineq
[k
][pos
], 1);
7737 return isl_basic_set_finalize(nonneg
);
7739 isl_basic_set_free(nonneg
);
7743 /* Construct the half-space x_pos <= -1.
7745 static __isl_give isl_basic_set
*neg_halfspace(__isl_take isl_space
*dim
, int pos
)
7750 neg
= isl_basic_set_alloc_space(dim
, 0, 0, 1);
7751 k
= isl_basic_set_alloc_inequality(neg
);
7754 isl_seq_clr(neg
->ineq
[k
], 1 + isl_basic_set_total_dim(neg
));
7755 isl_int_set_si(neg
->ineq
[k
][0], -1);
7756 isl_int_set_si(neg
->ineq
[k
][pos
], -1);
7758 return isl_basic_set_finalize(neg
);
7760 isl_basic_set_free(neg
);
7764 __isl_give isl_set
*isl_set_split_dims(__isl_take isl_set
*set
,
7765 enum isl_dim_type type
, unsigned first
, unsigned n
)
7769 isl_basic_set
*nonneg
;
7777 isl_assert(set
->ctx
, first
+ n
<= isl_set_dim(set
, type
), goto error
);
7779 offset
= pos(set
->dim
, type
);
7780 for (i
= 0; i
< n
; ++i
) {
7781 nonneg
= nonneg_halfspace(isl_set_get_space(set
),
7782 offset
+ first
+ i
);
7783 neg
= neg_halfspace(isl_set_get_space(set
), offset
+ first
+ i
);
7785 set
= isl_set_intersect(set
, isl_basic_set_union(nonneg
, neg
));
7794 static isl_stat
foreach_orthant(__isl_take isl_set
*set
, int *signs
, int first
,
7796 isl_stat (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
7802 return isl_stat_error
;
7803 if (isl_set_plain_is_empty(set
)) {
7808 return fn(set
, signs
, user
);
7811 half
= isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set
),
7813 half
= isl_set_intersect(half
, isl_set_copy(set
));
7814 if (foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
) < 0)
7818 half
= isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set
),
7820 half
= isl_set_intersect(half
, set
);
7821 return foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
);
7824 return isl_stat_error
;
7827 /* Call "fn" on the intersections of "set" with each of the orthants
7828 * (except for obviously empty intersections). The orthant is identified
7829 * by the signs array, with each entry having value 1 or -1 according
7830 * to the sign of the corresponding variable.
7832 isl_stat
isl_set_foreach_orthant(__isl_keep isl_set
*set
,
7833 isl_stat (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
7842 return isl_stat_error
;
7843 if (isl_set_plain_is_empty(set
))
7846 nparam
= isl_set_dim(set
, isl_dim_param
);
7847 nvar
= isl_set_dim(set
, isl_dim_set
);
7849 signs
= isl_alloc_array(set
->ctx
, int, nparam
+ nvar
);
7851 r
= foreach_orthant(isl_set_copy(set
), signs
, 0, nparam
+ nvar
,
7859 isl_bool
isl_set_is_equal(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
7861 return isl_map_is_equal(set_to_map(set1
), set_to_map(set2
));
7864 isl_bool
isl_basic_map_is_subset(__isl_keep isl_basic_map
*bmap1
,
7865 __isl_keep isl_basic_map
*bmap2
)
7868 struct isl_map
*map1
;
7869 struct isl_map
*map2
;
7871 if (!bmap1
|| !bmap2
)
7872 return isl_bool_error
;
7874 map1
= isl_map_from_basic_map(isl_basic_map_copy(bmap1
));
7875 map2
= isl_map_from_basic_map(isl_basic_map_copy(bmap2
));
7877 is_subset
= isl_map_is_subset(map1
, map2
);
7885 isl_bool
isl_basic_set_is_subset(__isl_keep isl_basic_set
*bset1
,
7886 __isl_keep isl_basic_set
*bset2
)
7888 return isl_basic_map_is_subset(bset1
, bset2
);
7891 isl_bool
isl_basic_map_is_equal(__isl_keep isl_basic_map
*bmap1
,
7892 __isl_keep isl_basic_map
*bmap2
)
7896 if (!bmap1
|| !bmap2
)
7897 return isl_bool_error
;
7898 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
7899 if (is_subset
!= isl_bool_true
)
7901 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
7905 isl_bool
isl_basic_set_is_equal(__isl_keep isl_basic_set
*bset1
,
7906 __isl_keep isl_basic_set
*bset2
)
7908 return isl_basic_map_is_equal(
7909 bset_to_bmap(bset1
), bset_to_bmap(bset2
));
7912 isl_bool
isl_map_is_empty(__isl_keep isl_map
*map
)
7918 return isl_bool_error
;
7919 for (i
= 0; i
< map
->n
; ++i
) {
7920 is_empty
= isl_basic_map_is_empty(map
->p
[i
]);
7922 return isl_bool_error
;
7924 return isl_bool_false
;
7926 return isl_bool_true
;
7929 isl_bool
isl_map_plain_is_empty(__isl_keep isl_map
*map
)
7931 return map
? map
->n
== 0 : isl_bool_error
;
7934 isl_bool
isl_set_plain_is_empty(__isl_keep isl_set
*set
)
7936 return set
? set
->n
== 0 : isl_bool_error
;
7939 isl_bool
isl_set_is_empty(__isl_keep isl_set
*set
)
7941 return isl_map_is_empty(set_to_map(set
));
7944 isl_bool
isl_map_has_equal_space(__isl_keep isl_map
*map1
,
7945 __isl_keep isl_map
*map2
)
7948 return isl_bool_error
;
7950 return isl_space_is_equal(map1
->dim
, map2
->dim
);
7953 isl_bool
isl_set_has_equal_space(__isl_keep isl_set
*set1
,
7954 __isl_keep isl_set
*set2
)
7957 return isl_bool_error
;
7959 return isl_space_is_equal(set1
->dim
, set2
->dim
);
7962 static isl_bool
map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
7967 return isl_bool_error
;
7968 is_subset
= isl_map_is_subset(map1
, map2
);
7969 if (is_subset
!= isl_bool_true
)
7971 is_subset
= isl_map_is_subset(map2
, map1
);
7975 /* Is "map1" equal to "map2"?
7977 * First check if they are obviously equal.
7978 * If not, then perform a more detailed analysis.
7980 isl_bool
isl_map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
7984 equal
= isl_map_plain_is_equal(map1
, map2
);
7985 if (equal
< 0 || equal
)
7987 return isl_map_align_params_map_map_and_test(map1
, map2
, &map_is_equal
);
7990 isl_bool
isl_basic_map_is_strict_subset(
7991 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
7995 if (!bmap1
|| !bmap2
)
7996 return isl_bool_error
;
7997 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
7998 if (is_subset
!= isl_bool_true
)
8000 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
8001 if (is_subset
== isl_bool_error
)
8006 isl_bool
isl_map_is_strict_subset(__isl_keep isl_map
*map1
,
8007 __isl_keep isl_map
*map2
)
8012 return isl_bool_error
;
8013 is_subset
= isl_map_is_subset(map1
, map2
);
8014 if (is_subset
!= isl_bool_true
)
8016 is_subset
= isl_map_is_subset(map2
, map1
);
8017 if (is_subset
== isl_bool_error
)
8022 isl_bool
isl_set_is_strict_subset(__isl_keep isl_set
*set1
,
8023 __isl_keep isl_set
*set2
)
8025 return isl_map_is_strict_subset(set_to_map(set1
), set_to_map(set2
));
8028 /* Is "bmap" obviously equal to the universe with the same space?
8030 * That is, does it not have any constraints?
8032 isl_bool
isl_basic_map_plain_is_universe(__isl_keep isl_basic_map
*bmap
)
8035 return isl_bool_error
;
8036 return bmap
->n_eq
== 0 && bmap
->n_ineq
== 0;
8039 /* Is "bset" obviously equal to the universe with the same space?
8041 isl_bool
isl_basic_set_plain_is_universe(__isl_keep isl_basic_set
*bset
)
8043 return isl_basic_map_plain_is_universe(bset
);
8046 /* If "c" does not involve any existentially quantified variables,
8047 * then set *univ to false and abort
8049 static isl_stat
involves_divs(__isl_take isl_constraint
*c
, void *user
)
8051 isl_bool
*univ
= user
;
8054 n
= isl_constraint_dim(c
, isl_dim_div
);
8055 *univ
= isl_constraint_involves_dims(c
, isl_dim_div
, 0, n
);
8056 isl_constraint_free(c
);
8057 if (*univ
< 0 || !*univ
)
8058 return isl_stat_error
;
8062 /* Is "bmap" equal to the universe with the same space?
8064 * First check if it is obviously equal to the universe.
8065 * If not and if there are any constraints not involving
8066 * existentially quantified variables, then it is certainly
8067 * not equal to the universe.
8068 * Otherwise, check if the universe is a subset of "bmap".
8070 isl_bool
isl_basic_map_is_universe(__isl_keep isl_basic_map
*bmap
)
8073 isl_basic_map
*test
;
8075 univ
= isl_basic_map_plain_is_universe(bmap
);
8076 if (univ
< 0 || univ
)
8078 if (isl_basic_map_dim(bmap
, isl_dim_div
) == 0)
8079 return isl_bool_false
;
8080 univ
= isl_bool_true
;
8081 if (isl_basic_map_foreach_constraint(bmap
, &involves_divs
, &univ
) < 0 &&
8083 return isl_bool_error
;
8084 if (univ
< 0 || !univ
)
8086 test
= isl_basic_map_universe(isl_basic_map_get_space(bmap
));
8087 univ
= isl_basic_map_is_subset(test
, bmap
);
8088 isl_basic_map_free(test
);
8092 /* Is "bset" equal to the universe with the same space?
8094 isl_bool
isl_basic_set_is_universe(__isl_keep isl_basic_set
*bset
)
8096 return isl_basic_map_is_universe(bset
);
8099 isl_bool
isl_map_plain_is_universe(__isl_keep isl_map
*map
)
8104 return isl_bool_error
;
8106 for (i
= 0; i
< map
->n
; ++i
) {
8107 isl_bool r
= isl_basic_map_plain_is_universe(map
->p
[i
]);
8112 return isl_bool_false
;
8115 isl_bool
isl_set_plain_is_universe(__isl_keep isl_set
*set
)
8117 return isl_map_plain_is_universe(set_to_map(set
));
8120 isl_bool
isl_basic_map_is_empty(__isl_keep isl_basic_map
*bmap
)
8122 struct isl_basic_set
*bset
= NULL
;
8123 struct isl_vec
*sample
= NULL
;
8124 isl_bool empty
, non_empty
;
8127 return isl_bool_error
;
8129 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
8130 return isl_bool_true
;
8132 if (isl_basic_map_plain_is_universe(bmap
))
8133 return isl_bool_false
;
8135 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
8136 struct isl_basic_map
*copy
= isl_basic_map_copy(bmap
);
8137 copy
= isl_basic_map_remove_redundancies(copy
);
8138 empty
= isl_basic_map_plain_is_empty(copy
);
8139 isl_basic_map_free(copy
);
8143 non_empty
= isl_basic_map_plain_is_non_empty(bmap
);
8145 return isl_bool_error
;
8147 return isl_bool_false
;
8148 isl_vec_free(bmap
->sample
);
8149 bmap
->sample
= NULL
;
8150 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
8152 return isl_bool_error
;
8153 sample
= isl_basic_set_sample_vec(bset
);
8155 return isl_bool_error
;
8156 empty
= sample
->size
== 0;
8157 isl_vec_free(bmap
->sample
);
8158 bmap
->sample
= sample
;
8160 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
8165 isl_bool
isl_basic_map_plain_is_empty(__isl_keep isl_basic_map
*bmap
)
8168 return isl_bool_error
;
8169 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
);
8172 isl_bool
isl_basic_set_plain_is_empty(__isl_keep isl_basic_set
*bset
)
8175 return isl_bool_error
;
8176 return ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
);
8179 /* Is "bmap" known to be non-empty?
8181 * That is, is the cached sample still valid?
8183 isl_bool
isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map
*bmap
)
8188 return isl_bool_error
;
8190 return isl_bool_false
;
8191 total
= 1 + isl_basic_map_total_dim(bmap
);
8192 if (bmap
->sample
->size
!= total
)
8193 return isl_bool_false
;
8194 return isl_basic_map_contains(bmap
, bmap
->sample
);
8197 isl_bool
isl_basic_set_is_empty(__isl_keep isl_basic_set
*bset
)
8199 return isl_basic_map_is_empty(bset_to_bmap(bset
));
8202 struct isl_map
*isl_basic_map_union(
8203 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
8205 struct isl_map
*map
;
8206 if (!bmap1
|| !bmap2
)
8209 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
8211 map
= isl_map_alloc_space(isl_space_copy(bmap1
->dim
), 2, 0);
8214 map
= isl_map_add_basic_map(map
, bmap1
);
8215 map
= isl_map_add_basic_map(map
, bmap2
);
8218 isl_basic_map_free(bmap1
);
8219 isl_basic_map_free(bmap2
);
8223 struct isl_set
*isl_basic_set_union(
8224 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
8226 return set_from_map(isl_basic_map_union(bset_to_bmap(bset1
),
8227 bset_to_bmap(bset2
)));
8230 /* Order divs such that any div only depends on previous divs */
8231 struct isl_basic_map
*isl_basic_map_order_divs(struct isl_basic_map
*bmap
)
8239 off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
8241 for (i
= 0; i
< bmap
->n_div
; ++i
) {
8243 if (isl_int_is_zero(bmap
->div
[i
][0]))
8245 pos
= isl_seq_first_non_zero(bmap
->div
[i
]+1+1+off
+i
,
8250 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_internal
,
8251 "integer division depends on itself",
8252 return isl_basic_map_free(bmap
));
8253 isl_basic_map_swap_div(bmap
, i
, i
+ pos
);
8259 struct isl_basic_set
*isl_basic_set_order_divs(struct isl_basic_set
*bset
)
8261 return bset_from_bmap(isl_basic_map_order_divs(bset_to_bmap(bset
)));
8264 __isl_give isl_map
*isl_map_order_divs(__isl_take isl_map
*map
)
8271 for (i
= 0; i
< map
->n
; ++i
) {
8272 map
->p
[i
] = isl_basic_map_order_divs(map
->p
[i
]);
8283 /* Sort the local variables of "bset".
8285 __isl_give isl_basic_set
*isl_basic_set_sort_divs(
8286 __isl_take isl_basic_set
*bset
)
8288 return bset_from_bmap(isl_basic_map_sort_divs(bset_to_bmap(bset
)));
8291 /* Apply the expansion computed by isl_merge_divs.
8292 * The expansion itself is given by "exp" while the resulting
8293 * list of divs is given by "div".
8295 * Move the integer divisions of "bmap" into the right position
8296 * according to "exp" and then introduce the additional integer
8297 * divisions, adding div constraints.
8298 * The moving should be done first to avoid moving coefficients
8299 * in the definitions of the extra integer divisions.
8301 __isl_give isl_basic_map
*isl_basic_map_expand_divs(
8302 __isl_take isl_basic_map
*bmap
, __isl_take isl_mat
*div
, int *exp
)
8307 bmap
= isl_basic_map_cow(bmap
);
8311 if (div
->n_row
< bmap
->n_div
)
8312 isl_die(isl_mat_get_ctx(div
), isl_error_invalid
,
8313 "not an expansion", goto error
);
8315 n_div
= bmap
->n_div
;
8316 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
8317 div
->n_row
- n_div
, 0,
8318 2 * (div
->n_row
- n_div
));
8320 for (i
= n_div
; i
< div
->n_row
; ++i
)
8321 if (isl_basic_map_alloc_div(bmap
) < 0)
8324 for (j
= n_div
- 1; j
>= 0; --j
) {
8327 isl_basic_map_swap_div(bmap
, j
, exp
[j
]);
8330 for (i
= 0; i
< div
->n_row
; ++i
) {
8331 if (j
< n_div
&& exp
[j
] == i
) {
8334 isl_seq_cpy(bmap
->div
[i
], div
->row
[i
], div
->n_col
);
8335 if (isl_basic_map_div_is_marked_unknown(bmap
, i
))
8337 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
8345 isl_basic_map_free(bmap
);
8350 /* Apply the expansion computed by isl_merge_divs.
8351 * The expansion itself is given by "exp" while the resulting
8352 * list of divs is given by "div".
8354 __isl_give isl_basic_set
*isl_basic_set_expand_divs(
8355 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*div
, int *exp
)
8357 return isl_basic_map_expand_divs(bset
, div
, exp
);
8360 /* Look for a div in dst that corresponds to the div "div" in src.
8361 * The divs before "div" in src and dst are assumed to be the same.
8363 * Returns -1 if no corresponding div was found and the position
8364 * of the corresponding div in dst otherwise.
8366 static int find_div(struct isl_basic_map
*dst
,
8367 struct isl_basic_map
*src
, unsigned div
)
8371 unsigned total
= isl_space_dim(src
->dim
, isl_dim_all
);
8373 isl_assert(dst
->ctx
, div
<= dst
->n_div
, return -1);
8374 for (i
= div
; i
< dst
->n_div
; ++i
)
8375 if (isl_seq_eq(dst
->div
[i
], src
->div
[div
], 1+1+total
+div
) &&
8376 isl_seq_first_non_zero(dst
->div
[i
]+1+1+total
+div
,
8377 dst
->n_div
- div
) == -1)
8382 /* Align the divs of "dst" to those of "src", adding divs from "src"
8383 * if needed. That is, make sure that the first src->n_div divs
8384 * of the result are equal to those of src.
8386 * The result is not finalized as by design it will have redundant
8387 * divs if any divs from "src" were copied.
8389 __isl_give isl_basic_map
*isl_basic_map_align_divs(
8390 __isl_take isl_basic_map
*dst
, __isl_keep isl_basic_map
*src
)
8393 int known
, extended
;
8397 return isl_basic_map_free(dst
);
8399 if (src
->n_div
== 0)
8402 known
= isl_basic_map_divs_known(src
);
8404 return isl_basic_map_free(dst
);
8406 isl_die(isl_basic_map_get_ctx(src
), isl_error_invalid
,
8407 "some src divs are unknown",
8408 return isl_basic_map_free(dst
));
8410 src
= isl_basic_map_order_divs(src
);
8413 total
= isl_space_dim(src
->dim
, isl_dim_all
);
8414 for (i
= 0; i
< src
->n_div
; ++i
) {
8415 int j
= find_div(dst
, src
, i
);
8418 int extra
= src
->n_div
- i
;
8419 dst
= isl_basic_map_cow(dst
);
8422 dst
= isl_basic_map_extend_space(dst
,
8423 isl_space_copy(dst
->dim
),
8424 extra
, 0, 2 * extra
);
8427 j
= isl_basic_map_alloc_div(dst
);
8429 return isl_basic_map_free(dst
);
8430 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
+i
);
8431 isl_seq_clr(dst
->div
[j
]+1+1+total
+i
, dst
->n_div
- i
);
8432 if (isl_basic_map_add_div_constraints(dst
, j
) < 0)
8433 return isl_basic_map_free(dst
);
8436 isl_basic_map_swap_div(dst
, i
, j
);
8441 struct isl_map
*isl_map_align_divs(struct isl_map
*map
)
8449 map
= isl_map_compute_divs(map
);
8450 map
= isl_map_cow(map
);
8454 for (i
= 1; i
< map
->n
; ++i
)
8455 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], map
->p
[i
]);
8456 for (i
= 1; i
< map
->n
; ++i
) {
8457 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], map
->p
[0]);
8459 return isl_map_free(map
);
8462 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
8466 struct isl_set
*isl_set_align_divs(struct isl_set
*set
)
8468 return set_from_map(isl_map_align_divs(set_to_map(set
)));
8471 /* Align the divs of the basic maps in "map" to those
8472 * of the basic maps in "list", as well as to the other basic maps in "map".
8473 * The elements in "list" are assumed to have known divs.
8475 __isl_give isl_map
*isl_map_align_divs_to_basic_map_list(
8476 __isl_take isl_map
*map
, __isl_keep isl_basic_map_list
*list
)
8480 map
= isl_map_compute_divs(map
);
8481 map
= isl_map_cow(map
);
8483 return isl_map_free(map
);
8487 n
= isl_basic_map_list_n_basic_map(list
);
8488 for (i
= 0; i
< n
; ++i
) {
8489 isl_basic_map
*bmap
;
8491 bmap
= isl_basic_map_list_get_basic_map(list
, i
);
8492 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], bmap
);
8493 isl_basic_map_free(bmap
);
8496 return isl_map_free(map
);
8498 return isl_map_align_divs(map
);
8501 /* Align the divs of each element of "list" to those of "bmap".
8502 * Both "bmap" and the elements of "list" are assumed to have known divs.
8504 __isl_give isl_basic_map_list
*isl_basic_map_list_align_divs_to_basic_map(
8505 __isl_take isl_basic_map_list
*list
, __isl_keep isl_basic_map
*bmap
)
8510 return isl_basic_map_list_free(list
);
8512 n
= isl_basic_map_list_n_basic_map(list
);
8513 for (i
= 0; i
< n
; ++i
) {
8514 isl_basic_map
*bmap_i
;
8516 bmap_i
= isl_basic_map_list_get_basic_map(list
, i
);
8517 bmap_i
= isl_basic_map_align_divs(bmap_i
, bmap
);
8518 list
= isl_basic_map_list_set_basic_map(list
, i
, bmap_i
);
8524 static __isl_give isl_set
*set_apply( __isl_take isl_set
*set
,
8525 __isl_take isl_map
*map
)
8529 ok
= isl_map_compatible_domain(map
, set
);
8533 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
8534 "incompatible spaces", goto error
);
8535 map
= isl_map_intersect_domain(map
, set
);
8536 set
= isl_map_range(map
);
8544 __isl_give isl_set
*isl_set_apply( __isl_take isl_set
*set
,
8545 __isl_take isl_map
*map
)
8547 return isl_map_align_params_map_map_and(set
, map
, &set_apply
);
8550 /* There is no need to cow as removing empty parts doesn't change
8551 * the meaning of the set.
8553 struct isl_map
*isl_map_remove_empty_parts(struct isl_map
*map
)
8560 for (i
= map
->n
- 1; i
>= 0; --i
)
8561 remove_if_empty(map
, i
);
8566 struct isl_set
*isl_set_remove_empty_parts(struct isl_set
*set
)
8568 return set_from_map(isl_map_remove_empty_parts(set_to_map(set
)));
8571 /* Given two basic sets bset1 and bset2, compute the maximal difference
8572 * between the values of dimension pos in bset1 and those in bset2
8573 * for any common value of the parameters and dimensions preceding pos.
8575 static enum isl_lp_result
basic_set_maximal_difference_at(
8576 __isl_keep isl_basic_set
*bset1
, __isl_keep isl_basic_set
*bset2
,
8577 int pos
, isl_int
*opt
)
8579 isl_basic_map
*bmap1
;
8580 isl_basic_map
*bmap2
;
8581 struct isl_ctx
*ctx
;
8582 struct isl_vec
*obj
;
8586 enum isl_lp_result res
;
8588 if (!bset1
|| !bset2
)
8589 return isl_lp_error
;
8591 nparam
= isl_basic_set_n_param(bset1
);
8592 dim1
= isl_basic_set_n_dim(bset1
);
8594 bmap1
= isl_basic_map_from_range(isl_basic_set_copy(bset1
));
8595 bmap2
= isl_basic_map_from_range(isl_basic_set_copy(bset2
));
8596 bmap1
= isl_basic_map_move_dims(bmap1
, isl_dim_in
, 0,
8597 isl_dim_out
, 0, pos
);
8598 bmap2
= isl_basic_map_move_dims(bmap2
, isl_dim_in
, 0,
8599 isl_dim_out
, 0, pos
);
8600 bmap1
= isl_basic_map_range_product(bmap1
, bmap2
);
8602 return isl_lp_error
;
8604 total
= isl_basic_map_total_dim(bmap1
);
8606 obj
= isl_vec_alloc(ctx
, 1 + total
);
8609 isl_seq_clr(obj
->block
.data
, 1 + total
);
8610 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
], 1);
8611 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
+(dim1
-pos
)], -1);
8612 res
= isl_basic_map_solve_lp(bmap1
, 1, obj
->block
.data
, ctx
->one
,
8614 isl_basic_map_free(bmap1
);
8618 isl_basic_map_free(bmap1
);
8619 return isl_lp_error
;
8622 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
8623 * for any common value of the parameters and dimensions preceding pos
8624 * in both basic sets, the values of dimension pos in bset1 are
8625 * smaller or larger than those in bset2.
8628 * 1 if bset1 follows bset2
8629 * -1 if bset1 precedes bset2
8630 * 0 if bset1 and bset2 are incomparable
8631 * -2 if some error occurred.
8633 int isl_basic_set_compare_at(struct isl_basic_set
*bset1
,
8634 struct isl_basic_set
*bset2
, int pos
)
8637 enum isl_lp_result res
;
8642 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
8644 if (res
== isl_lp_empty
)
8646 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
8647 res
== isl_lp_unbounded
)
8649 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
8658 /* Given two basic sets bset1 and bset2, check whether
8659 * for any common value of the parameters and dimensions preceding pos
8660 * there is a value of dimension pos in bset1 that is larger
8661 * than a value of the same dimension in bset2.
8664 * 1 if there exists such a pair
8665 * 0 if there is no such pair, but there is a pair of equal values
8667 * -2 if some error occurred.
8669 int isl_basic_set_follows_at(__isl_keep isl_basic_set
*bset1
,
8670 __isl_keep isl_basic_set
*bset2
, int pos
)
8673 enum isl_lp_result res
;
8678 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
8680 if (res
== isl_lp_empty
)
8682 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
8683 res
== isl_lp_unbounded
)
8685 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
8687 else if (res
== isl_lp_ok
)
8696 /* Given two sets set1 and set2, check whether
8697 * for any common value of the parameters and dimensions preceding pos
8698 * there is a value of dimension pos in set1 that is larger
8699 * than a value of the same dimension in set2.
8702 * 1 if there exists such a pair
8703 * 0 if there is no such pair, but there is a pair of equal values
8705 * -2 if some error occurred.
8707 int isl_set_follows_at(__isl_keep isl_set
*set1
,
8708 __isl_keep isl_set
*set2
, int pos
)
8716 for (i
= 0; i
< set1
->n
; ++i
)
8717 for (j
= 0; j
< set2
->n
; ++j
) {
8719 f
= isl_basic_set_follows_at(set1
->p
[i
], set2
->p
[j
], pos
);
8720 if (f
== 1 || f
== -2)
8729 static isl_bool
isl_basic_map_plain_has_fixed_var(
8730 __isl_keep isl_basic_map
*bmap
, unsigned pos
, isl_int
*val
)
8737 return isl_bool_error
;
8738 total
= isl_basic_map_total_dim(bmap
);
8739 for (i
= 0, d
= total
-1; i
< bmap
->n_eq
&& d
+1 > pos
; ++i
) {
8740 for (; d
+1 > pos
; --d
)
8741 if (!isl_int_is_zero(bmap
->eq
[i
][1+d
]))
8745 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1, d
) != -1)
8746 return isl_bool_false
;
8747 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1+d
+1, total
-d
-1) != -1)
8748 return isl_bool_false
;
8749 if (!isl_int_is_one(bmap
->eq
[i
][1+d
]))
8750 return isl_bool_false
;
8752 isl_int_neg(*val
, bmap
->eq
[i
][0]);
8753 return isl_bool_true
;
8755 return isl_bool_false
;
8758 static isl_bool
isl_map_plain_has_fixed_var(__isl_keep isl_map
*map
,
8759 unsigned pos
, isl_int
*val
)
8767 return isl_bool_error
;
8769 return isl_bool_false
;
8771 return isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, val
);
8774 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, &v
);
8775 for (i
= 1; fixed
== isl_bool_true
&& i
< map
->n
; ++i
) {
8776 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[i
], pos
, &tmp
);
8777 if (fixed
== isl_bool_true
&& isl_int_ne(tmp
, v
))
8778 fixed
= isl_bool_false
;
8781 isl_int_set(*val
, v
);
8787 static isl_bool
isl_basic_set_plain_has_fixed_var(
8788 __isl_keep isl_basic_set
*bset
, unsigned pos
, isl_int
*val
)
8790 return isl_basic_map_plain_has_fixed_var(bset_to_bmap(bset
),
8794 isl_bool
isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map
*bmap
,
8795 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
8797 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
8798 return isl_bool_error
;
8799 return isl_basic_map_plain_has_fixed_var(bmap
,
8800 isl_basic_map_offset(bmap
, type
) - 1 + pos
, val
);
8803 /* If "bmap" obviously lies on a hyperplane where the given dimension
8804 * has a fixed value, then return that value.
8805 * Otherwise return NaN.
8807 __isl_give isl_val
*isl_basic_map_plain_get_val_if_fixed(
8808 __isl_keep isl_basic_map
*bmap
,
8809 enum isl_dim_type type
, unsigned pos
)
8817 ctx
= isl_basic_map_get_ctx(bmap
);
8818 v
= isl_val_alloc(ctx
);
8821 fixed
= isl_basic_map_plain_is_fixed(bmap
, type
, pos
, &v
->n
);
8823 return isl_val_free(v
);
8825 isl_int_set_si(v
->d
, 1);
8829 return isl_val_nan(ctx
);
8832 isl_bool
isl_map_plain_is_fixed(__isl_keep isl_map
*map
,
8833 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
8835 if (pos
>= isl_map_dim(map
, type
))
8836 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
8837 "position out of bounds", return isl_bool_error
);
8838 return isl_map_plain_has_fixed_var(map
,
8839 map_offset(map
, type
) - 1 + pos
, val
);
8842 /* If "map" obviously lies on a hyperplane where the given dimension
8843 * has a fixed value, then return that value.
8844 * Otherwise return NaN.
8846 __isl_give isl_val
*isl_map_plain_get_val_if_fixed(__isl_keep isl_map
*map
,
8847 enum isl_dim_type type
, unsigned pos
)
8855 ctx
= isl_map_get_ctx(map
);
8856 v
= isl_val_alloc(ctx
);
8859 fixed
= isl_map_plain_is_fixed(map
, type
, pos
, &v
->n
);
8861 return isl_val_free(v
);
8863 isl_int_set_si(v
->d
, 1);
8867 return isl_val_nan(ctx
);
8870 /* If "set" obviously lies on a hyperplane where the given dimension
8871 * has a fixed value, then return that value.
8872 * Otherwise return NaN.
8874 __isl_give isl_val
*isl_set_plain_get_val_if_fixed(__isl_keep isl_set
*set
,
8875 enum isl_dim_type type
, unsigned pos
)
8877 return isl_map_plain_get_val_if_fixed(set
, type
, pos
);
8880 isl_bool
isl_set_plain_is_fixed(__isl_keep isl_set
*set
,
8881 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
8883 return isl_map_plain_is_fixed(set
, type
, pos
, val
);
8886 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8887 * then return this fixed value in *val.
8889 isl_bool
isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set
*bset
,
8890 unsigned dim
, isl_int
*val
)
8892 return isl_basic_set_plain_has_fixed_var(bset
,
8893 isl_basic_set_n_param(bset
) + dim
, val
);
8896 /* Return -1 if the constraint "c1" should be sorted before "c2"
8897 * and 1 if it should be sorted after "c2".
8898 * Return 0 if the two constraints are the same (up to the constant term).
8900 * In particular, if a constraint involves later variables than another
8901 * then it is sorted after this other constraint.
8902 * uset_gist depends on constraints without existentially quantified
8903 * variables sorting first.
8905 * For constraints that have the same latest variable, those
8906 * with the same coefficient for this latest variable (first in absolute value
8907 * and then in actual value) are grouped together.
8908 * This is useful for detecting pairs of constraints that can
8909 * be chained in their printed representation.
8911 * Finally, within a group, constraints are sorted according to
8912 * their coefficients (excluding the constant term).
8914 static int sort_constraint_cmp(const void *p1
, const void *p2
, void *arg
)
8916 isl_int
**c1
= (isl_int
**) p1
;
8917 isl_int
**c2
= (isl_int
**) p2
;
8919 unsigned size
= *(unsigned *) arg
;
8922 l1
= isl_seq_last_non_zero(*c1
+ 1, size
);
8923 l2
= isl_seq_last_non_zero(*c2
+ 1, size
);
8928 cmp
= isl_int_abs_cmp((*c1
)[1 + l1
], (*c2
)[1 + l1
]);
8931 cmp
= isl_int_cmp((*c1
)[1 + l1
], (*c2
)[1 + l1
]);
8935 return isl_seq_cmp(*c1
+ 1, *c2
+ 1, size
);
8938 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
8939 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
8940 * and 0 if the two constraints are the same (up to the constant term).
8942 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map
*bmap
,
8943 isl_int
*c1
, isl_int
*c2
)
8949 total
= isl_basic_map_total_dim(bmap
);
8950 return sort_constraint_cmp(&c1
, &c2
, &total
);
8953 __isl_give isl_basic_map
*isl_basic_map_sort_constraints(
8954 __isl_take isl_basic_map
*bmap
)
8960 if (bmap
->n_ineq
== 0)
8962 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED
))
8964 total
= isl_basic_map_total_dim(bmap
);
8965 if (isl_sort(bmap
->ineq
, bmap
->n_ineq
, sizeof(isl_int
*),
8966 &sort_constraint_cmp
, &total
) < 0)
8967 return isl_basic_map_free(bmap
);
8971 __isl_give isl_basic_set
*isl_basic_set_sort_constraints(
8972 __isl_take isl_basic_set
*bset
)
8974 isl_basic_map
*bmap
= bset_to_bmap(bset
);
8975 return bset_from_bmap(isl_basic_map_sort_constraints(bmap
));
8978 struct isl_basic_map
*isl_basic_map_normalize(struct isl_basic_map
*bmap
)
8982 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED
))
8984 bmap
= isl_basic_map_remove_redundancies(bmap
);
8985 bmap
= isl_basic_map_sort_constraints(bmap
);
8987 ISL_F_SET(bmap
, ISL_BASIC_MAP_NORMALIZED
);
8990 int isl_basic_map_plain_cmp(__isl_keep isl_basic_map
*bmap1
,
8991 __isl_keep isl_basic_map
*bmap2
)
8996 if (!bmap1
|| !bmap2
)
9001 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) !=
9002 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_RATIONAL
))
9003 return ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) ? -1 : 1;
9004 if (isl_basic_map_n_param(bmap1
) != isl_basic_map_n_param(bmap2
))
9005 return isl_basic_map_n_param(bmap1
) - isl_basic_map_n_param(bmap2
);
9006 if (isl_basic_map_n_in(bmap1
) != isl_basic_map_n_in(bmap2
))
9007 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
9008 if (isl_basic_map_n_out(bmap1
) != isl_basic_map_n_out(bmap2
))
9009 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
9010 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
) &&
9011 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
9013 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
))
9015 if (ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
9017 if (bmap1
->n_eq
!= bmap2
->n_eq
)
9018 return bmap1
->n_eq
- bmap2
->n_eq
;
9019 if (bmap1
->n_ineq
!= bmap2
->n_ineq
)
9020 return bmap1
->n_ineq
- bmap2
->n_ineq
;
9021 if (bmap1
->n_div
!= bmap2
->n_div
)
9022 return bmap1
->n_div
- bmap2
->n_div
;
9023 total
= isl_basic_map_total_dim(bmap1
);
9024 for (i
= 0; i
< bmap1
->n_eq
; ++i
) {
9025 cmp
= isl_seq_cmp(bmap1
->eq
[i
], bmap2
->eq
[i
], 1+total
);
9029 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
9030 cmp
= isl_seq_cmp(bmap1
->ineq
[i
], bmap2
->ineq
[i
], 1+total
);
9034 for (i
= 0; i
< bmap1
->n_div
; ++i
) {
9035 cmp
= isl_seq_cmp(bmap1
->div
[i
], bmap2
->div
[i
], 1+1+total
);
9042 int isl_basic_set_plain_cmp(__isl_keep isl_basic_set
*bset1
,
9043 __isl_keep isl_basic_set
*bset2
)
9045 return isl_basic_map_plain_cmp(bset1
, bset2
);
9048 int isl_set_plain_cmp(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
9054 if (set1
->n
!= set2
->n
)
9055 return set1
->n
- set2
->n
;
9057 for (i
= 0; i
< set1
->n
; ++i
) {
9058 cmp
= isl_basic_set_plain_cmp(set1
->p
[i
], set2
->p
[i
]);
9066 isl_bool
isl_basic_map_plain_is_equal(__isl_keep isl_basic_map
*bmap1
,
9067 __isl_keep isl_basic_map
*bmap2
)
9069 if (!bmap1
|| !bmap2
)
9070 return isl_bool_error
;
9071 return isl_basic_map_plain_cmp(bmap1
, bmap2
) == 0;
9074 isl_bool
isl_basic_set_plain_is_equal(__isl_keep isl_basic_set
*bset1
,
9075 __isl_keep isl_basic_set
*bset2
)
9077 return isl_basic_map_plain_is_equal(bset_to_bmap(bset1
),
9078 bset_to_bmap(bset2
));
9081 static int qsort_bmap_cmp(const void *p1
, const void *p2
)
9083 isl_basic_map
*bmap1
= *(isl_basic_map
**) p1
;
9084 isl_basic_map
*bmap2
= *(isl_basic_map
**) p2
;
9086 return isl_basic_map_plain_cmp(bmap1
, bmap2
);
9089 /* Sort the basic maps of "map" and remove duplicate basic maps.
9091 * While removing basic maps, we make sure that the basic maps remain
9092 * sorted because isl_map_normalize expects the basic maps of the result
9095 static __isl_give isl_map
*sort_and_remove_duplicates(__isl_take isl_map
*map
)
9099 map
= isl_map_remove_empty_parts(map
);
9102 qsort(map
->p
, map
->n
, sizeof(struct isl_basic_map
*), qsort_bmap_cmp
);
9103 for (i
= map
->n
- 1; i
>= 1; --i
) {
9104 if (!isl_basic_map_plain_is_equal(map
->p
[i
- 1], map
->p
[i
]))
9106 isl_basic_map_free(map
->p
[i
-1]);
9107 for (j
= i
; j
< map
->n
; ++j
)
9108 map
->p
[j
- 1] = map
->p
[j
];
9115 /* Remove obvious duplicates among the basic maps of "map".
9117 * Unlike isl_map_normalize, this function does not remove redundant
9118 * constraints and only removes duplicates that have exactly the same
9119 * constraints in the input. It does sort the constraints and
9120 * the basic maps to ease the detection of duplicates.
9122 * If "map" has already been normalized or if the basic maps are
9123 * disjoint, then there can be no duplicates.
9125 __isl_give isl_map
*isl_map_remove_obvious_duplicates(__isl_take isl_map
*map
)
9128 isl_basic_map
*bmap
;
9134 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
| ISL_MAP_DISJOINT
))
9136 for (i
= 0; i
< map
->n
; ++i
) {
9137 bmap
= isl_basic_map_copy(map
->p
[i
]);
9138 bmap
= isl_basic_map_sort_constraints(bmap
);
9140 return isl_map_free(map
);
9141 isl_basic_map_free(map
->p
[i
]);
9145 map
= sort_and_remove_duplicates(map
);
9149 /* We normalize in place, but if anything goes wrong we need
9150 * to return NULL, so we need to make sure we don't change the
9151 * meaning of any possible other copies of map.
9153 __isl_give isl_map
*isl_map_normalize(__isl_take isl_map
*map
)
9156 struct isl_basic_map
*bmap
;
9160 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
))
9162 for (i
= 0; i
< map
->n
; ++i
) {
9163 bmap
= isl_basic_map_normalize(isl_basic_map_copy(map
->p
[i
]));
9166 isl_basic_map_free(map
->p
[i
]);
9170 map
= sort_and_remove_duplicates(map
);
9172 ISL_F_SET(map
, ISL_MAP_NORMALIZED
);
9179 struct isl_set
*isl_set_normalize(struct isl_set
*set
)
9181 return set_from_map(isl_map_normalize(set_to_map(set
)));
9184 isl_bool
isl_map_plain_is_equal(__isl_keep isl_map
*map1
,
9185 __isl_keep isl_map
*map2
)
9191 return isl_bool_error
;
9194 return isl_bool_true
;
9195 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
9196 return isl_bool_false
;
9198 map1
= isl_map_copy(map1
);
9199 map2
= isl_map_copy(map2
);
9200 map1
= isl_map_normalize(map1
);
9201 map2
= isl_map_normalize(map2
);
9204 equal
= map1
->n
== map2
->n
;
9205 for (i
= 0; equal
&& i
< map1
->n
; ++i
) {
9206 equal
= isl_basic_map_plain_is_equal(map1
->p
[i
], map2
->p
[i
]);
9216 return isl_bool_error
;
9219 isl_bool
isl_set_plain_is_equal(__isl_keep isl_set
*set1
,
9220 __isl_keep isl_set
*set2
)
9222 return isl_map_plain_is_equal(set_to_map(set1
), set_to_map(set2
));
9225 /* Return the basic maps in "map" as a list.
9227 __isl_give isl_basic_map_list
*isl_map_get_basic_map_list(
9228 __isl_keep isl_map
*map
)
9232 isl_basic_map_list
*list
;
9236 ctx
= isl_map_get_ctx(map
);
9237 list
= isl_basic_map_list_alloc(ctx
, map
->n
);
9239 for (i
= 0; i
< map
->n
; ++i
) {
9240 isl_basic_map
*bmap
;
9242 bmap
= isl_basic_map_copy(map
->p
[i
]);
9243 list
= isl_basic_map_list_add(list
, bmap
);
9249 /* Return the intersection of the elements in the non-empty list "list".
9250 * All elements are assumed to live in the same space.
9252 __isl_give isl_basic_map
*isl_basic_map_list_intersect(
9253 __isl_take isl_basic_map_list
*list
)
9256 isl_basic_map
*bmap
;
9260 n
= isl_basic_map_list_n_basic_map(list
);
9262 isl_die(isl_basic_map_list_get_ctx(list
), isl_error_invalid
,
9263 "expecting non-empty list", goto error
);
9265 bmap
= isl_basic_map_list_get_basic_map(list
, 0);
9266 for (i
= 1; i
< n
; ++i
) {
9267 isl_basic_map
*bmap_i
;
9269 bmap_i
= isl_basic_map_list_get_basic_map(list
, i
);
9270 bmap
= isl_basic_map_intersect(bmap
, bmap_i
);
9273 isl_basic_map_list_free(list
);
9276 isl_basic_map_list_free(list
);
9280 /* Return the intersection of the elements in the non-empty list "list".
9281 * All elements are assumed to live in the same space.
9283 __isl_give isl_basic_set
*isl_basic_set_list_intersect(
9284 __isl_take isl_basic_set_list
*list
)
9286 return isl_basic_map_list_intersect(list
);
9289 /* Return the union of the elements of "list".
9290 * The list is required to have at least one element.
9292 __isl_give isl_set
*isl_basic_set_list_union(
9293 __isl_take isl_basic_set_list
*list
)
9297 isl_basic_set
*bset
;
9302 n
= isl_basic_set_list_n_basic_set(list
);
9304 isl_die(isl_basic_set_list_get_ctx(list
), isl_error_invalid
,
9305 "expecting non-empty list", goto error
);
9307 bset
= isl_basic_set_list_get_basic_set(list
, 0);
9308 space
= isl_basic_set_get_space(bset
);
9309 isl_basic_set_free(bset
);
9311 set
= isl_set_alloc_space(space
, n
, 0);
9312 for (i
= 0; i
< n
; ++i
) {
9313 bset
= isl_basic_set_list_get_basic_set(list
, i
);
9314 set
= isl_set_add_basic_set(set
, bset
);
9317 isl_basic_set_list_free(list
);
9320 isl_basic_set_list_free(list
);
9324 /* Return the union of the elements in the non-empty list "list".
9325 * All elements are assumed to live in the same space.
9327 __isl_give isl_set
*isl_set_list_union(__isl_take isl_set_list
*list
)
9334 n
= isl_set_list_n_set(list
);
9336 isl_die(isl_set_list_get_ctx(list
), isl_error_invalid
,
9337 "expecting non-empty list", goto error
);
9339 set
= isl_set_list_get_set(list
, 0);
9340 for (i
= 1; i
< n
; ++i
) {
9343 set_i
= isl_set_list_get_set(list
, i
);
9344 set
= isl_set_union(set
, set_i
);
9347 isl_set_list_free(list
);
9350 isl_set_list_free(list
);
9354 struct isl_basic_map
*isl_basic_map_product(
9355 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
9357 isl_space
*dim_result
= NULL
;
9358 struct isl_basic_map
*bmap
;
9359 unsigned in1
, in2
, out1
, out2
, nparam
, total
, pos
;
9360 struct isl_dim_map
*dim_map1
, *dim_map2
;
9362 if (!bmap1
|| !bmap2
)
9365 isl_assert(bmap1
->ctx
, isl_space_match(bmap1
->dim
, isl_dim_param
,
9366 bmap2
->dim
, isl_dim_param
), goto error
);
9367 dim_result
= isl_space_product(isl_space_copy(bmap1
->dim
),
9368 isl_space_copy(bmap2
->dim
));
9370 in1
= isl_basic_map_n_in(bmap1
);
9371 in2
= isl_basic_map_n_in(bmap2
);
9372 out1
= isl_basic_map_n_out(bmap1
);
9373 out2
= isl_basic_map_n_out(bmap2
);
9374 nparam
= isl_basic_map_n_param(bmap1
);
9376 total
= nparam
+ in1
+ in2
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
9377 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9378 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9379 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9380 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9381 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9382 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
9383 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
9384 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
9385 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
9386 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9388 bmap
= isl_basic_map_alloc_space(dim_result
,
9389 bmap1
->n_div
+ bmap2
->n_div
,
9390 bmap1
->n_eq
+ bmap2
->n_eq
,
9391 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9392 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9393 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9394 bmap
= isl_basic_map_simplify(bmap
);
9395 return isl_basic_map_finalize(bmap
);
9397 isl_basic_map_free(bmap1
);
9398 isl_basic_map_free(bmap2
);
9402 __isl_give isl_basic_map
*isl_basic_map_flat_product(
9403 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9405 isl_basic_map
*prod
;
9407 prod
= isl_basic_map_product(bmap1
, bmap2
);
9408 prod
= isl_basic_map_flatten(prod
);
9412 __isl_give isl_basic_set
*isl_basic_set_flat_product(
9413 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
9415 return isl_basic_map_flat_range_product(bset1
, bset2
);
9418 __isl_give isl_basic_map
*isl_basic_map_domain_product(
9419 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9421 isl_space
*space_result
= NULL
;
9422 isl_basic_map
*bmap
;
9423 unsigned in1
, in2
, out
, nparam
, total
, pos
;
9424 struct isl_dim_map
*dim_map1
, *dim_map2
;
9426 if (!bmap1
|| !bmap2
)
9429 space_result
= isl_space_domain_product(isl_space_copy(bmap1
->dim
),
9430 isl_space_copy(bmap2
->dim
));
9432 in1
= isl_basic_map_dim(bmap1
, isl_dim_in
);
9433 in2
= isl_basic_map_dim(bmap2
, isl_dim_in
);
9434 out
= isl_basic_map_dim(bmap1
, isl_dim_out
);
9435 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
9437 total
= nparam
+ in1
+ in2
+ out
+ bmap1
->n_div
+ bmap2
->n_div
;
9438 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9439 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9440 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9441 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9442 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9443 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
9444 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
9445 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
);
9446 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out
);
9447 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9449 bmap
= isl_basic_map_alloc_space(space_result
,
9450 bmap1
->n_div
+ bmap2
->n_div
,
9451 bmap1
->n_eq
+ bmap2
->n_eq
,
9452 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9453 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9454 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9455 bmap
= isl_basic_map_simplify(bmap
);
9456 return isl_basic_map_finalize(bmap
);
9458 isl_basic_map_free(bmap1
);
9459 isl_basic_map_free(bmap2
);
9463 __isl_give isl_basic_map
*isl_basic_map_range_product(
9464 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9467 isl_space
*dim_result
= NULL
;
9468 isl_basic_map
*bmap
;
9469 unsigned in
, out1
, out2
, nparam
, total
, pos
;
9470 struct isl_dim_map
*dim_map1
, *dim_map2
;
9472 rational
= isl_basic_map_is_rational(bmap1
);
9473 if (rational
>= 0 && rational
)
9474 rational
= isl_basic_map_is_rational(bmap2
);
9475 if (!bmap1
|| !bmap2
|| rational
< 0)
9478 if (!isl_space_match(bmap1
->dim
, isl_dim_param
,
9479 bmap2
->dim
, isl_dim_param
))
9480 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
9481 "parameters don't match", goto error
);
9483 dim_result
= isl_space_range_product(isl_space_copy(bmap1
->dim
),
9484 isl_space_copy(bmap2
->dim
));
9486 in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
9487 out1
= isl_basic_map_n_out(bmap1
);
9488 out2
= isl_basic_map_n_out(bmap2
);
9489 nparam
= isl_basic_map_n_param(bmap1
);
9491 total
= nparam
+ in
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
9492 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9493 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9494 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9495 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9496 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9497 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
9498 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in
);
9499 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
9500 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
9501 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9503 bmap
= isl_basic_map_alloc_space(dim_result
,
9504 bmap1
->n_div
+ bmap2
->n_div
,
9505 bmap1
->n_eq
+ bmap2
->n_eq
,
9506 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9507 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9508 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9510 bmap
= isl_basic_map_set_rational(bmap
);
9511 bmap
= isl_basic_map_simplify(bmap
);
9512 return isl_basic_map_finalize(bmap
);
9514 isl_basic_map_free(bmap1
);
9515 isl_basic_map_free(bmap2
);
9519 __isl_give isl_basic_map
*isl_basic_map_flat_range_product(
9520 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9522 isl_basic_map
*prod
;
9524 prod
= isl_basic_map_range_product(bmap1
, bmap2
);
9525 prod
= isl_basic_map_flatten_range(prod
);
9529 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
9530 * and collect the results.
9531 * The result live in the space obtained by calling "space_product"
9532 * on the spaces of "map1" and "map2".
9533 * If "remove_duplicates" is set then the result may contain duplicates
9534 * (even if the inputs do not) and so we try and remove the obvious
9537 static __isl_give isl_map
*map_product(__isl_take isl_map
*map1
,
9538 __isl_take isl_map
*map2
,
9539 __isl_give isl_space
*(*space_product
)(__isl_take isl_space
*left
,
9540 __isl_take isl_space
*right
),
9541 __isl_give isl_basic_map
*(*basic_map_product
)(
9542 __isl_take isl_basic_map
*left
,
9543 __isl_take isl_basic_map
*right
),
9544 int remove_duplicates
)
9547 struct isl_map
*result
;
9553 isl_assert(map1
->ctx
, isl_space_match(map1
->dim
, isl_dim_param
,
9554 map2
->dim
, isl_dim_param
), goto error
);
9556 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
9557 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
9558 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
9560 result
= isl_map_alloc_space(space_product(isl_space_copy(map1
->dim
),
9561 isl_space_copy(map2
->dim
)),
9562 map1
->n
* map2
->n
, flags
);
9565 for (i
= 0; i
< map1
->n
; ++i
)
9566 for (j
= 0; j
< map2
->n
; ++j
) {
9567 struct isl_basic_map
*part
;
9568 part
= basic_map_product(isl_basic_map_copy(map1
->p
[i
]),
9569 isl_basic_map_copy(map2
->p
[j
]));
9570 if (isl_basic_map_is_empty(part
))
9571 isl_basic_map_free(part
);
9573 result
= isl_map_add_basic_map(result
, part
);
9577 if (remove_duplicates
)
9578 result
= isl_map_remove_obvious_duplicates(result
);
9588 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
9590 static __isl_give isl_map
*map_product_aligned(__isl_take isl_map
*map1
,
9591 __isl_take isl_map
*map2
)
9593 return map_product(map1
, map2
, &isl_space_product
,
9594 &isl_basic_map_product
, 0);
9597 __isl_give isl_map
*isl_map_product(__isl_take isl_map
*map1
,
9598 __isl_take isl_map
*map2
)
9600 return isl_map_align_params_map_map_and(map1
, map2
, &map_product_aligned
);
9603 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
9605 __isl_give isl_map
*isl_map_flat_product(__isl_take isl_map
*map1
,
9606 __isl_take isl_map
*map2
)
9610 prod
= isl_map_product(map1
, map2
);
9611 prod
= isl_map_flatten(prod
);
9615 /* Given two set A and B, construct its Cartesian product A x B.
9617 struct isl_set
*isl_set_product(struct isl_set
*set1
, struct isl_set
*set2
)
9619 return isl_map_range_product(set1
, set2
);
9622 __isl_give isl_set
*isl_set_flat_product(__isl_take isl_set
*set1
,
9623 __isl_take isl_set
*set2
)
9625 return isl_map_flat_range_product(set1
, set2
);
9628 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
9630 static __isl_give isl_map
*map_domain_product_aligned(__isl_take isl_map
*map1
,
9631 __isl_take isl_map
*map2
)
9633 return map_product(map1
, map2
, &isl_space_domain_product
,
9634 &isl_basic_map_domain_product
, 1);
9637 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
9639 static __isl_give isl_map
*map_range_product_aligned(__isl_take isl_map
*map1
,
9640 __isl_take isl_map
*map2
)
9642 return map_product(map1
, map2
, &isl_space_range_product
,
9643 &isl_basic_map_range_product
, 1);
9646 __isl_give isl_map
*isl_map_domain_product(__isl_take isl_map
*map1
,
9647 __isl_take isl_map
*map2
)
9649 return isl_map_align_params_map_map_and(map1
, map2
,
9650 &map_domain_product_aligned
);
9653 __isl_give isl_map
*isl_map_range_product(__isl_take isl_map
*map1
,
9654 __isl_take isl_map
*map2
)
9656 return isl_map_align_params_map_map_and(map1
, map2
,
9657 &map_range_product_aligned
);
9660 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
9662 __isl_give isl_map
*isl_map_factor_domain(__isl_take isl_map
*map
)
9665 int total1
, keep1
, total2
, keep2
;
9669 if (!isl_space_domain_is_wrapping(map
->dim
) ||
9670 !isl_space_range_is_wrapping(map
->dim
))
9671 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9672 "not a product", return isl_map_free(map
));
9674 space
= isl_map_get_space(map
);
9675 total1
= isl_space_dim(space
, isl_dim_in
);
9676 total2
= isl_space_dim(space
, isl_dim_out
);
9677 space
= isl_space_factor_domain(space
);
9678 keep1
= isl_space_dim(space
, isl_dim_in
);
9679 keep2
= isl_space_dim(space
, isl_dim_out
);
9680 map
= isl_map_project_out(map
, isl_dim_in
, keep1
, total1
- keep1
);
9681 map
= isl_map_project_out(map
, isl_dim_out
, keep2
, total2
- keep2
);
9682 map
= isl_map_reset_space(map
, space
);
9687 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
9689 __isl_give isl_map
*isl_map_factor_range(__isl_take isl_map
*map
)
9692 int total1
, keep1
, total2
, keep2
;
9696 if (!isl_space_domain_is_wrapping(map
->dim
) ||
9697 !isl_space_range_is_wrapping(map
->dim
))
9698 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9699 "not a product", return isl_map_free(map
));
9701 space
= isl_map_get_space(map
);
9702 total1
= isl_space_dim(space
, isl_dim_in
);
9703 total2
= isl_space_dim(space
, isl_dim_out
);
9704 space
= isl_space_factor_range(space
);
9705 keep1
= isl_space_dim(space
, isl_dim_in
);
9706 keep2
= isl_space_dim(space
, isl_dim_out
);
9707 map
= isl_map_project_out(map
, isl_dim_in
, 0, total1
- keep1
);
9708 map
= isl_map_project_out(map
, isl_dim_out
, 0, total2
- keep2
);
9709 map
= isl_map_reset_space(map
, space
);
9714 /* Given a map of the form [A -> B] -> C, return the map A -> C.
9716 __isl_give isl_map
*isl_map_domain_factor_domain(__isl_take isl_map
*map
)
9723 if (!isl_space_domain_is_wrapping(map
->dim
))
9724 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9725 "domain is not a product", return isl_map_free(map
));
9727 space
= isl_map_get_space(map
);
9728 total
= isl_space_dim(space
, isl_dim_in
);
9729 space
= isl_space_domain_factor_domain(space
);
9730 keep
= isl_space_dim(space
, isl_dim_in
);
9731 map
= isl_map_project_out(map
, isl_dim_in
, keep
, total
- keep
);
9732 map
= isl_map_reset_space(map
, space
);
9737 /* Given a map of the form [A -> B] -> C, return the map B -> C.
9739 __isl_give isl_map
*isl_map_domain_factor_range(__isl_take isl_map
*map
)
9746 if (!isl_space_domain_is_wrapping(map
->dim
))
9747 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9748 "domain is not a product", return isl_map_free(map
));
9750 space
= isl_map_get_space(map
);
9751 total
= isl_space_dim(space
, isl_dim_in
);
9752 space
= isl_space_domain_factor_range(space
);
9753 keep
= isl_space_dim(space
, isl_dim_in
);
9754 map
= isl_map_project_out(map
, isl_dim_in
, 0, total
- keep
);
9755 map
= isl_map_reset_space(map
, space
);
9760 /* Given a map A -> [B -> C], extract the map A -> B.
9762 __isl_give isl_map
*isl_map_range_factor_domain(__isl_take isl_map
*map
)
9769 if (!isl_space_range_is_wrapping(map
->dim
))
9770 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9771 "range is not a product", return isl_map_free(map
));
9773 space
= isl_map_get_space(map
);
9774 total
= isl_space_dim(space
, isl_dim_out
);
9775 space
= isl_space_range_factor_domain(space
);
9776 keep
= isl_space_dim(space
, isl_dim_out
);
9777 map
= isl_map_project_out(map
, isl_dim_out
, keep
, total
- keep
);
9778 map
= isl_map_reset_space(map
, space
);
9783 /* Given a map A -> [B -> C], extract the map A -> C.
9785 __isl_give isl_map
*isl_map_range_factor_range(__isl_take isl_map
*map
)
9792 if (!isl_space_range_is_wrapping(map
->dim
))
9793 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9794 "range is not a product", return isl_map_free(map
));
9796 space
= isl_map_get_space(map
);
9797 total
= isl_space_dim(space
, isl_dim_out
);
9798 space
= isl_space_range_factor_range(space
);
9799 keep
= isl_space_dim(space
, isl_dim_out
);
9800 map
= isl_map_project_out(map
, isl_dim_out
, 0, total
- keep
);
9801 map
= isl_map_reset_space(map
, space
);
9806 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
9808 __isl_give isl_map
*isl_map_flat_domain_product(__isl_take isl_map
*map1
,
9809 __isl_take isl_map
*map2
)
9813 prod
= isl_map_domain_product(map1
, map2
);
9814 prod
= isl_map_flatten_domain(prod
);
9818 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
9820 __isl_give isl_map
*isl_map_flat_range_product(__isl_take isl_map
*map1
,
9821 __isl_take isl_map
*map2
)
9825 prod
= isl_map_range_product(map1
, map2
);
9826 prod
= isl_map_flatten_range(prod
);
9830 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map
*bmap
)
9833 uint32_t hash
= isl_hash_init();
9838 bmap
= isl_basic_map_copy(bmap
);
9839 bmap
= isl_basic_map_normalize(bmap
);
9842 total
= isl_basic_map_total_dim(bmap
);
9843 isl_hash_byte(hash
, bmap
->n_eq
& 0xFF);
9844 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
9846 c_hash
= isl_seq_get_hash(bmap
->eq
[i
], 1 + total
);
9847 isl_hash_hash(hash
, c_hash
);
9849 isl_hash_byte(hash
, bmap
->n_ineq
& 0xFF);
9850 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
9852 c_hash
= isl_seq_get_hash(bmap
->ineq
[i
], 1 + total
);
9853 isl_hash_hash(hash
, c_hash
);
9855 isl_hash_byte(hash
, bmap
->n_div
& 0xFF);
9856 for (i
= 0; i
< bmap
->n_div
; ++i
) {
9858 if (isl_int_is_zero(bmap
->div
[i
][0]))
9860 isl_hash_byte(hash
, i
& 0xFF);
9861 c_hash
= isl_seq_get_hash(bmap
->div
[i
], 1 + 1 + total
);
9862 isl_hash_hash(hash
, c_hash
);
9864 isl_basic_map_free(bmap
);
9868 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set
*bset
)
9870 return isl_basic_map_get_hash(bset_to_bmap(bset
));
9873 uint32_t isl_map_get_hash(__isl_keep isl_map
*map
)
9880 map
= isl_map_copy(map
);
9881 map
= isl_map_normalize(map
);
9885 hash
= isl_hash_init();
9886 for (i
= 0; i
< map
->n
; ++i
) {
9888 bmap_hash
= isl_basic_map_get_hash(map
->p
[i
]);
9889 isl_hash_hash(hash
, bmap_hash
);
9897 uint32_t isl_set_get_hash(__isl_keep isl_set
*set
)
9899 return isl_map_get_hash(set_to_map(set
));
9902 /* Return the number of basic maps in the (current) representation of "map".
9904 int isl_map_n_basic_map(__isl_keep isl_map
*map
)
9906 return map
? map
->n
: 0;
9909 int isl_set_n_basic_set(__isl_keep isl_set
*set
)
9911 return set
? set
->n
: 0;
9914 isl_stat
isl_map_foreach_basic_map(__isl_keep isl_map
*map
,
9915 isl_stat (*fn
)(__isl_take isl_basic_map
*bmap
, void *user
), void *user
)
9920 return isl_stat_error
;
9922 for (i
= 0; i
< map
->n
; ++i
)
9923 if (fn(isl_basic_map_copy(map
->p
[i
]), user
) < 0)
9924 return isl_stat_error
;
9929 isl_stat
isl_set_foreach_basic_set(__isl_keep isl_set
*set
,
9930 isl_stat (*fn
)(__isl_take isl_basic_set
*bset
, void *user
), void *user
)
9935 return isl_stat_error
;
9937 for (i
= 0; i
< set
->n
; ++i
)
9938 if (fn(isl_basic_set_copy(set
->p
[i
]), user
) < 0)
9939 return isl_stat_error
;
9944 /* Return a list of basic sets, the union of which is equal to "set".
9946 __isl_give isl_basic_set_list
*isl_set_get_basic_set_list(
9947 __isl_keep isl_set
*set
)
9950 isl_basic_set_list
*list
;
9955 list
= isl_basic_set_list_alloc(isl_set_get_ctx(set
), set
->n
);
9956 for (i
= 0; i
< set
->n
; ++i
) {
9957 isl_basic_set
*bset
;
9959 bset
= isl_basic_set_copy(set
->p
[i
]);
9960 list
= isl_basic_set_list_add(list
, bset
);
9966 __isl_give isl_basic_set
*isl_basic_set_lift(__isl_take isl_basic_set
*bset
)
9973 bset
= isl_basic_set_cow(bset
);
9977 dim
= isl_basic_set_get_space(bset
);
9978 dim
= isl_space_lift(dim
, bset
->n_div
);
9981 isl_space_free(bset
->dim
);
9983 bset
->extra
-= bset
->n_div
;
9986 bset
= isl_basic_set_finalize(bset
);
9990 isl_basic_set_free(bset
);
9994 __isl_give isl_set
*isl_set_lift(__isl_take isl_set
*set
)
10000 set
= isl_set_align_divs(set
);
10005 set
= isl_set_cow(set
);
10009 n_div
= set
->p
[0]->n_div
;
10010 dim
= isl_set_get_space(set
);
10011 dim
= isl_space_lift(dim
, n_div
);
10014 isl_space_free(set
->dim
);
10017 for (i
= 0; i
< set
->n
; ++i
) {
10018 set
->p
[i
] = isl_basic_set_lift(set
->p
[i
]);
10029 int isl_basic_set_size(__isl_keep isl_basic_set
*bset
)
10037 dim
= isl_basic_set_total_dim(bset
);
10038 size
+= bset
->n_eq
* (1 + dim
);
10039 size
+= bset
->n_ineq
* (1 + dim
);
10040 size
+= bset
->n_div
* (2 + dim
);
10045 int isl_set_size(__isl_keep isl_set
*set
)
10053 for (i
= 0; i
< set
->n
; ++i
)
10054 size
+= isl_basic_set_size(set
->p
[i
]);
10059 /* Check if there is any lower bound (if lower == 0) and/or upper
10060 * bound (if upper == 0) on the specified dim.
10062 static isl_bool
basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
10063 enum isl_dim_type type
, unsigned pos
, int lower
, int upper
)
10067 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
10068 return isl_bool_error
;
10070 pos
+= isl_basic_map_offset(bmap
, type
);
10072 for (i
= 0; i
< bmap
->n_div
; ++i
) {
10073 if (isl_int_is_zero(bmap
->div
[i
][0]))
10075 if (!isl_int_is_zero(bmap
->div
[i
][1 + pos
]))
10076 return isl_bool_true
;
10079 for (i
= 0; i
< bmap
->n_eq
; ++i
)
10080 if (!isl_int_is_zero(bmap
->eq
[i
][pos
]))
10081 return isl_bool_true
;
10083 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
10084 int sgn
= isl_int_sgn(bmap
->ineq
[i
][pos
]);
10091 return lower
&& upper
;
10094 isl_bool
isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
10095 enum isl_dim_type type
, unsigned pos
)
10097 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 0);
10100 isl_bool
isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map
*bmap
,
10101 enum isl_dim_type type
, unsigned pos
)
10103 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 1);
10106 isl_bool
isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map
*bmap
,
10107 enum isl_dim_type type
, unsigned pos
)
10109 return basic_map_dim_is_bounded(bmap
, type
, pos
, 1, 0);
10112 isl_bool
isl_map_dim_is_bounded(__isl_keep isl_map
*map
,
10113 enum isl_dim_type type
, unsigned pos
)
10118 return isl_bool_error
;
10120 for (i
= 0; i
< map
->n
; ++i
) {
10122 bounded
= isl_basic_map_dim_is_bounded(map
->p
[i
], type
, pos
);
10123 if (bounded
< 0 || !bounded
)
10127 return isl_bool_true
;
10130 /* Return true if the specified dim is involved in both an upper bound
10131 * and a lower bound.
10133 isl_bool
isl_set_dim_is_bounded(__isl_keep isl_set
*set
,
10134 enum isl_dim_type type
, unsigned pos
)
10136 return isl_map_dim_is_bounded(set_to_map(set
), type
, pos
);
10139 /* Does "map" have a bound (according to "fn") for any of its basic maps?
10141 static isl_bool
has_any_bound(__isl_keep isl_map
*map
,
10142 enum isl_dim_type type
, unsigned pos
,
10143 isl_bool (*fn
)(__isl_keep isl_basic_map
*bmap
,
10144 enum isl_dim_type type
, unsigned pos
))
10149 return isl_bool_error
;
10151 for (i
= 0; i
< map
->n
; ++i
) {
10153 bounded
= fn(map
->p
[i
], type
, pos
);
10154 if (bounded
< 0 || bounded
)
10158 return isl_bool_false
;
10161 /* Return 1 if the specified dim is involved in any lower bound.
10163 isl_bool
isl_set_dim_has_any_lower_bound(__isl_keep isl_set
*set
,
10164 enum isl_dim_type type
, unsigned pos
)
10166 return has_any_bound(set
, type
, pos
,
10167 &isl_basic_map_dim_has_lower_bound
);
10170 /* Return 1 if the specified dim is involved in any upper bound.
10172 isl_bool
isl_set_dim_has_any_upper_bound(__isl_keep isl_set
*set
,
10173 enum isl_dim_type type
, unsigned pos
)
10175 return has_any_bound(set
, type
, pos
,
10176 &isl_basic_map_dim_has_upper_bound
);
10179 /* Does "map" have a bound (according to "fn") for all of its basic maps?
10181 static isl_bool
has_bound(__isl_keep isl_map
*map
,
10182 enum isl_dim_type type
, unsigned pos
,
10183 isl_bool (*fn
)(__isl_keep isl_basic_map
*bmap
,
10184 enum isl_dim_type type
, unsigned pos
))
10189 return isl_bool_error
;
10191 for (i
= 0; i
< map
->n
; ++i
) {
10193 bounded
= fn(map
->p
[i
], type
, pos
);
10194 if (bounded
< 0 || !bounded
)
10198 return isl_bool_true
;
10201 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10203 isl_bool
isl_set_dim_has_lower_bound(__isl_keep isl_set
*set
,
10204 enum isl_dim_type type
, unsigned pos
)
10206 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_lower_bound
);
10209 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10211 isl_bool
isl_set_dim_has_upper_bound(__isl_keep isl_set
*set
,
10212 enum isl_dim_type type
, unsigned pos
)
10214 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_upper_bound
);
10217 /* For each of the "n" variables starting at "first", determine
10218 * the sign of the variable and put the results in the first "n"
10219 * elements of the array "signs".
10221 * 1 means that the variable is non-negative
10222 * -1 means that the variable is non-positive
10223 * 0 means the variable attains both positive and negative values.
10225 isl_stat
isl_basic_set_vars_get_sign(__isl_keep isl_basic_set
*bset
,
10226 unsigned first
, unsigned n
, int *signs
)
10228 isl_vec
*bound
= NULL
;
10229 struct isl_tab
*tab
= NULL
;
10230 struct isl_tab_undo
*snap
;
10233 if (!bset
|| !signs
)
10234 return isl_stat_error
;
10236 bound
= isl_vec_alloc(bset
->ctx
, 1 + isl_basic_set_total_dim(bset
));
10237 tab
= isl_tab_from_basic_set(bset
, 0);
10238 if (!bound
|| !tab
)
10241 isl_seq_clr(bound
->el
, bound
->size
);
10242 isl_int_set_si(bound
->el
[0], -1);
10244 snap
= isl_tab_snap(tab
);
10245 for (i
= 0; i
< n
; ++i
) {
10248 isl_int_set_si(bound
->el
[1 + first
+ i
], -1);
10249 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
10251 empty
= tab
->empty
;
10252 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
10253 if (isl_tab_rollback(tab
, snap
) < 0)
10261 isl_int_set_si(bound
->el
[1 + first
+ i
], 1);
10262 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
10264 empty
= tab
->empty
;
10265 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
10266 if (isl_tab_rollback(tab
, snap
) < 0)
10269 signs
[i
] = empty
? -1 : 0;
10273 isl_vec_free(bound
);
10274 return isl_stat_ok
;
10277 isl_vec_free(bound
);
10278 return isl_stat_error
;
10281 isl_stat
isl_basic_set_dims_get_sign(__isl_keep isl_basic_set
*bset
,
10282 enum isl_dim_type type
, unsigned first
, unsigned n
, int *signs
)
10284 if (!bset
|| !signs
)
10285 return isl_stat_error
;
10286 isl_assert(bset
->ctx
, first
+ n
<= isl_basic_set_dim(bset
, type
),
10287 return isl_stat_error
);
10289 first
+= pos(bset
->dim
, type
) - 1;
10290 return isl_basic_set_vars_get_sign(bset
, first
, n
, signs
);
10293 /* Is it possible for the integer division "div" to depend (possibly
10294 * indirectly) on any output dimensions?
10296 * If the div is undefined, then we conservatively assume that it
10297 * may depend on them.
10298 * Otherwise, we check if it actually depends on them or on any integer
10299 * divisions that may depend on them.
10301 static isl_bool
div_may_involve_output(__isl_keep isl_basic_map
*bmap
, int div
)
10304 unsigned n_out
, o_out
;
10305 unsigned n_div
, o_div
;
10307 if (isl_int_is_zero(bmap
->div
[div
][0]))
10308 return isl_bool_true
;
10310 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10311 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10313 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + o_out
, n_out
) != -1)
10314 return isl_bool_true
;
10316 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10317 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10319 for (i
= 0; i
< n_div
; ++i
) {
10320 isl_bool may_involve
;
10322 if (isl_int_is_zero(bmap
->div
[div
][1 + o_div
+ i
]))
10324 may_involve
= div_may_involve_output(bmap
, i
);
10325 if (may_involve
< 0 || may_involve
)
10326 return may_involve
;
10329 return isl_bool_false
;
10332 /* Return the first integer division of "bmap" in the range
10333 * [first, first + n[ that may depend on any output dimensions and
10334 * that has a non-zero coefficient in "c" (where the first coefficient
10335 * in "c" corresponds to integer division "first").
10337 static int first_div_may_involve_output(__isl_keep isl_basic_map
*bmap
,
10338 isl_int
*c
, int first
, int n
)
10345 for (k
= first
; k
< first
+ n
; ++k
) {
10346 isl_bool may_involve
;
10348 if (isl_int_is_zero(c
[k
]))
10350 may_involve
= div_may_involve_output(bmap
, k
);
10351 if (may_involve
< 0)
10360 /* Look for a pair of inequality constraints in "bmap" of the form
10362 * -l + i >= 0 or i >= l
10364 * n + l - i >= 0 or i <= l + n
10366 * with n < "m" and i the output dimension at position "pos".
10367 * (Note that n >= 0 as otherwise the two constraints would conflict.)
10368 * Furthermore, "l" is only allowed to involve parameters, input dimensions
10369 * and earlier output dimensions, as well as integer divisions that do
10370 * not involve any of the output dimensions.
10372 * Return the index of the first inequality constraint or bmap->n_ineq
10373 * if no such pair can be found.
10375 static int find_modulo_constraint_pair(__isl_keep isl_basic_map
*bmap
,
10376 int pos
, isl_int m
)
10381 unsigned n_div
, o_div
;
10382 unsigned n_out
, o_out
;
10388 ctx
= isl_basic_map_get_ctx(bmap
);
10389 total
= isl_basic_map_total_dim(bmap
);
10390 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10391 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10392 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10393 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10394 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
10395 if (!isl_int_abs_eq(bmap
->ineq
[i
][o_out
+ pos
], ctx
->one
))
10397 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + o_out
+ pos
+ 1,
10398 n_out
- (pos
+ 1)) != -1)
10400 if (first_div_may_involve_output(bmap
, bmap
->ineq
[i
] + o_div
,
10403 for (j
= i
+ 1; j
< bmap
->n_ineq
; ++j
) {
10404 if (!isl_int_abs_eq(bmap
->ineq
[j
][o_out
+ pos
],
10407 if (!isl_seq_is_neg(bmap
->ineq
[i
] + 1,
10408 bmap
->ineq
[j
] + 1, total
))
10412 if (j
>= bmap
->n_ineq
)
10414 isl_int_add(bmap
->ineq
[i
][0],
10415 bmap
->ineq
[i
][0], bmap
->ineq
[j
][0]);
10416 less
= isl_int_abs_lt(bmap
->ineq
[i
][0], m
);
10417 isl_int_sub(bmap
->ineq
[i
][0],
10418 bmap
->ineq
[i
][0], bmap
->ineq
[j
][0]);
10421 if (isl_int_is_one(bmap
->ineq
[i
][o_out
+ pos
]))
10427 return bmap
->n_ineq
;
10430 /* Return the index of the equality of "bmap" that defines
10431 * the output dimension "pos" in terms of earlier dimensions.
10432 * The equality may also involve integer divisions, as long
10433 * as those integer divisions are defined in terms of
10434 * parameters or input dimensions.
10435 * In this case, *div is set to the number of integer divisions and
10436 * *ineq is set to the number of inequality constraints (provided
10437 * div and ineq are not NULL).
10439 * The equality may also involve a single integer division involving
10440 * the output dimensions (typically only output dimension "pos") as
10441 * long as the coefficient of output dimension "pos" is 1 or -1 and
10442 * there is a pair of constraints i >= l and i <= l + n, with i referring
10443 * to output dimension "pos", l an expression involving only earlier
10444 * dimensions and n smaller than the coefficient of the integer division
10445 * in the equality. In this case, the output dimension can be defined
10446 * in terms of a modulo expression that does not involve the integer division.
10447 * *div is then set to this single integer division and
10448 * *ineq is set to the index of constraint i >= l.
10450 * Return bmap->n_eq if there is no such equality.
10451 * Return -1 on error.
10453 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map
*bmap
,
10454 int pos
, int *div
, int *ineq
)
10457 unsigned n_out
, o_out
;
10458 unsigned n_div
, o_div
;
10463 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10464 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10465 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10466 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10469 *ineq
= bmap
->n_ineq
;
10472 for (j
= 0; j
< bmap
->n_eq
; ++j
) {
10473 if (isl_int_is_zero(bmap
->eq
[j
][o_out
+ pos
]))
10475 if (isl_seq_first_non_zero(bmap
->eq
[j
] + o_out
+ pos
+ 1,
10476 n_out
- (pos
+ 1)) != -1)
10478 k
= first_div_may_involve_output(bmap
, bmap
->eq
[j
] + o_div
,
10482 if (!isl_int_is_one(bmap
->eq
[j
][o_out
+ pos
]) &&
10483 !isl_int_is_negone(bmap
->eq
[j
][o_out
+ pos
]))
10485 if (first_div_may_involve_output(bmap
, bmap
->eq
[j
] + o_div
,
10486 k
+ 1, n_div
- (k
+1)) < n_div
)
10488 l
= find_modulo_constraint_pair(bmap
, pos
,
10489 bmap
->eq
[j
][o_div
+ k
]);
10492 if (l
>= bmap
->n_ineq
)
10504 /* Check if the given basic map is obviously single-valued.
10505 * In particular, for each output dimension, check that there is
10506 * an equality that defines the output dimension in terms of
10507 * earlier dimensions.
10509 isl_bool
isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map
*bmap
)
10515 return isl_bool_error
;
10517 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10519 for (i
= 0; i
< n_out
; ++i
) {
10522 eq
= isl_basic_map_output_defining_equality(bmap
, i
,
10525 return isl_bool_error
;
10526 if (eq
>= bmap
->n_eq
)
10527 return isl_bool_false
;
10530 return isl_bool_true
;
10533 /* Check if the given basic map is single-valued.
10534 * We simply compute
10538 * and check if the result is a subset of the identity mapping.
10540 isl_bool
isl_basic_map_is_single_valued(__isl_keep isl_basic_map
*bmap
)
10543 isl_basic_map
*test
;
10547 sv
= isl_basic_map_plain_is_single_valued(bmap
);
10551 test
= isl_basic_map_reverse(isl_basic_map_copy(bmap
));
10552 test
= isl_basic_map_apply_range(test
, isl_basic_map_copy(bmap
));
10554 space
= isl_basic_map_get_space(bmap
);
10555 space
= isl_space_map_from_set(isl_space_range(space
));
10556 id
= isl_basic_map_identity(space
);
10558 sv
= isl_basic_map_is_subset(test
, id
);
10560 isl_basic_map_free(test
);
10561 isl_basic_map_free(id
);
10566 /* Check if the given map is obviously single-valued.
10568 isl_bool
isl_map_plain_is_single_valued(__isl_keep isl_map
*map
)
10571 return isl_bool_error
;
10573 return isl_bool_true
;
10575 return isl_bool_false
;
10577 return isl_basic_map_plain_is_single_valued(map
->p
[0]);
10580 /* Check if the given map is single-valued.
10581 * We simply compute
10585 * and check if the result is a subset of the identity mapping.
10587 isl_bool
isl_map_is_single_valued(__isl_keep isl_map
*map
)
10594 sv
= isl_map_plain_is_single_valued(map
);
10598 test
= isl_map_reverse(isl_map_copy(map
));
10599 test
= isl_map_apply_range(test
, isl_map_copy(map
));
10601 dim
= isl_space_map_from_set(isl_space_range(isl_map_get_space(map
)));
10602 id
= isl_map_identity(dim
);
10604 sv
= isl_map_is_subset(test
, id
);
10606 isl_map_free(test
);
10612 isl_bool
isl_map_is_injective(__isl_keep isl_map
*map
)
10616 map
= isl_map_copy(map
);
10617 map
= isl_map_reverse(map
);
10618 in
= isl_map_is_single_valued(map
);
10624 /* Check if the given map is obviously injective.
10626 isl_bool
isl_map_plain_is_injective(__isl_keep isl_map
*map
)
10630 map
= isl_map_copy(map
);
10631 map
= isl_map_reverse(map
);
10632 in
= isl_map_plain_is_single_valued(map
);
10638 isl_bool
isl_map_is_bijective(__isl_keep isl_map
*map
)
10642 sv
= isl_map_is_single_valued(map
);
10646 return isl_map_is_injective(map
);
10649 isl_bool
isl_set_is_singleton(__isl_keep isl_set
*set
)
10651 return isl_map_is_single_valued(set_to_map(set
));
10654 /* Does "map" only map elements to themselves?
10656 * If the domain and range spaces are different, then "map"
10657 * is considered not to be an identity relation, even if it is empty.
10658 * Otherwise, construct the maximal identity relation and
10659 * check whether "map" is a subset of this relation.
10661 isl_bool
isl_map_is_identity(__isl_keep isl_map
*map
)
10665 isl_bool equal
, is_identity
;
10667 space
= isl_map_get_space(map
);
10668 equal
= isl_space_tuple_is_equal(space
, isl_dim_in
, space
, isl_dim_out
);
10669 isl_space_free(space
);
10670 if (equal
< 0 || !equal
)
10673 id
= isl_map_identity(isl_map_get_space(map
));
10674 is_identity
= isl_map_is_subset(map
, id
);
10677 return is_identity
;
10680 int isl_map_is_translation(__isl_keep isl_map
*map
)
10685 delta
= isl_map_deltas(isl_map_copy(map
));
10686 ok
= isl_set_is_singleton(delta
);
10687 isl_set_free(delta
);
10692 static int unique(isl_int
*p
, unsigned pos
, unsigned len
)
10694 if (isl_seq_first_non_zero(p
, pos
) != -1)
10696 if (isl_seq_first_non_zero(p
+ pos
+ 1, len
- pos
- 1) != -1)
10701 isl_bool
isl_basic_set_is_box(__isl_keep isl_basic_set
*bset
)
10708 return isl_bool_error
;
10710 if (isl_basic_set_dim(bset
, isl_dim_div
) != 0)
10711 return isl_bool_false
;
10713 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
10714 ovar
= isl_space_offset(bset
->dim
, isl_dim_set
);
10715 for (j
= 0; j
< nvar
; ++j
) {
10716 int lower
= 0, upper
= 0;
10717 for (i
= 0; i
< bset
->n_eq
; ++i
) {
10718 if (isl_int_is_zero(bset
->eq
[i
][1 + ovar
+ j
]))
10720 if (!unique(bset
->eq
[i
] + 1 + ovar
, j
, nvar
))
10721 return isl_bool_false
;
10724 if (i
< bset
->n_eq
)
10726 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
10727 if (isl_int_is_zero(bset
->ineq
[i
][1 + ovar
+ j
]))
10729 if (!unique(bset
->ineq
[i
] + 1 + ovar
, j
, nvar
))
10730 return isl_bool_false
;
10731 if (isl_int_is_pos(bset
->ineq
[i
][1 + ovar
+ j
]))
10736 if (!lower
|| !upper
)
10737 return isl_bool_false
;
10740 return isl_bool_true
;
10743 isl_bool
isl_set_is_box(__isl_keep isl_set
*set
)
10746 return isl_bool_error
;
10748 return isl_bool_false
;
10750 return isl_basic_set_is_box(set
->p
[0]);
10753 isl_bool
isl_basic_set_is_wrapping(__isl_keep isl_basic_set
*bset
)
10756 return isl_bool_error
;
10758 return isl_space_is_wrapping(bset
->dim
);
10761 isl_bool
isl_set_is_wrapping(__isl_keep isl_set
*set
)
10764 return isl_bool_error
;
10766 return isl_space_is_wrapping(set
->dim
);
10769 /* Modify the space of "map" through a call to "change".
10770 * If "can_change" is set (not NULL), then first call it to check
10771 * if the modification is allowed, printing the error message "cannot_change"
10774 static __isl_give isl_map
*isl_map_change_space(__isl_take isl_map
*map
,
10775 isl_bool (*can_change
)(__isl_keep isl_map
*map
),
10776 const char *cannot_change
,
10777 __isl_give isl_space
*(*change
)(__isl_take isl_space
*space
))
10785 ok
= can_change
? can_change(map
) : isl_bool_true
;
10787 return isl_map_free(map
);
10789 isl_die(isl_map_get_ctx(map
), isl_error_invalid
, cannot_change
,
10790 return isl_map_free(map
));
10792 space
= change(isl_map_get_space(map
));
10793 map
= isl_map_reset_space(map
, space
);
10798 /* Is the domain of "map" a wrapped relation?
10800 isl_bool
isl_map_domain_is_wrapping(__isl_keep isl_map
*map
)
10803 return isl_bool_error
;
10805 return isl_space_domain_is_wrapping(map
->dim
);
10808 /* Is the range of "map" a wrapped relation?
10810 isl_bool
isl_map_range_is_wrapping(__isl_keep isl_map
*map
)
10813 return isl_bool_error
;
10815 return isl_space_range_is_wrapping(map
->dim
);
10818 __isl_give isl_basic_set
*isl_basic_map_wrap(__isl_take isl_basic_map
*bmap
)
10820 bmap
= isl_basic_map_cow(bmap
);
10824 bmap
->dim
= isl_space_wrap(bmap
->dim
);
10828 bmap
= isl_basic_map_finalize(bmap
);
10830 return bset_from_bmap(bmap
);
10832 isl_basic_map_free(bmap
);
10836 /* Given a map A -> B, return the set (A -> B).
10838 __isl_give isl_set
*isl_map_wrap(__isl_take isl_map
*map
)
10840 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_wrap
);
10843 __isl_give isl_basic_map
*isl_basic_set_unwrap(__isl_take isl_basic_set
*bset
)
10845 bset
= isl_basic_set_cow(bset
);
10849 bset
->dim
= isl_space_unwrap(bset
->dim
);
10853 bset
= isl_basic_set_finalize(bset
);
10855 return bset_to_bmap(bset
);
10857 isl_basic_set_free(bset
);
10861 /* Given a set (A -> B), return the map A -> B.
10862 * Error out if "set" is not of the form (A -> B).
10864 __isl_give isl_map
*isl_set_unwrap(__isl_take isl_set
*set
)
10866 return isl_map_change_space(set
, &isl_set_is_wrapping
,
10867 "not a wrapping set", &isl_space_unwrap
);
10870 __isl_give isl_basic_map
*isl_basic_map_reset(__isl_take isl_basic_map
*bmap
,
10871 enum isl_dim_type type
)
10876 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
10879 bmap
= isl_basic_map_cow(bmap
);
10883 bmap
->dim
= isl_space_reset(bmap
->dim
, type
);
10887 bmap
= isl_basic_map_finalize(bmap
);
10891 isl_basic_map_free(bmap
);
10895 __isl_give isl_map
*isl_map_reset(__isl_take isl_map
*map
,
10896 enum isl_dim_type type
)
10903 if (!isl_space_is_named_or_nested(map
->dim
, type
))
10906 map
= isl_map_cow(map
);
10910 for (i
= 0; i
< map
->n
; ++i
) {
10911 map
->p
[i
] = isl_basic_map_reset(map
->p
[i
], type
);
10915 map
->dim
= isl_space_reset(map
->dim
, type
);
10925 __isl_give isl_basic_map
*isl_basic_map_flatten(__isl_take isl_basic_map
*bmap
)
10930 if (!bmap
->dim
->nested
[0] && !bmap
->dim
->nested
[1])
10933 bmap
= isl_basic_map_cow(bmap
);
10937 bmap
->dim
= isl_space_flatten(bmap
->dim
);
10941 bmap
= isl_basic_map_finalize(bmap
);
10945 isl_basic_map_free(bmap
);
10949 __isl_give isl_basic_set
*isl_basic_set_flatten(__isl_take isl_basic_set
*bset
)
10951 return bset_from_bmap(isl_basic_map_flatten(bset_to_bmap(bset
)));
10954 __isl_give isl_basic_map
*isl_basic_map_flatten_domain(
10955 __isl_take isl_basic_map
*bmap
)
10960 if (!bmap
->dim
->nested
[0])
10963 bmap
= isl_basic_map_cow(bmap
);
10967 bmap
->dim
= isl_space_flatten_domain(bmap
->dim
);
10971 bmap
= isl_basic_map_finalize(bmap
);
10975 isl_basic_map_free(bmap
);
10979 __isl_give isl_basic_map
*isl_basic_map_flatten_range(
10980 __isl_take isl_basic_map
*bmap
)
10985 if (!bmap
->dim
->nested
[1])
10988 bmap
= isl_basic_map_cow(bmap
);
10992 bmap
->dim
= isl_space_flatten_range(bmap
->dim
);
10996 bmap
= isl_basic_map_finalize(bmap
);
11000 isl_basic_map_free(bmap
);
11004 /* Remove any internal structure from the spaces of domain and range of "map".
11006 __isl_give isl_map
*isl_map_flatten(__isl_take isl_map
*map
)
11011 if (!map
->dim
->nested
[0] && !map
->dim
->nested
[1])
11014 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten
);
11017 __isl_give isl_set
*isl_set_flatten(__isl_take isl_set
*set
)
11019 return set_from_map(isl_map_flatten(set_to_map(set
)));
11022 __isl_give isl_map
*isl_set_flatten_map(__isl_take isl_set
*set
)
11024 isl_space
*dim
, *flat_dim
;
11027 dim
= isl_set_get_space(set
);
11028 flat_dim
= isl_space_flatten(isl_space_copy(dim
));
11029 map
= isl_map_identity(isl_space_join(isl_space_reverse(dim
), flat_dim
));
11030 map
= isl_map_intersect_domain(map
, set
);
11035 /* Remove any internal structure from the space of the domain of "map".
11037 __isl_give isl_map
*isl_map_flatten_domain(__isl_take isl_map
*map
)
11042 if (!map
->dim
->nested
[0])
11045 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten_domain
);
11048 /* Remove any internal structure from the space of the range of "map".
11050 __isl_give isl_map
*isl_map_flatten_range(__isl_take isl_map
*map
)
11055 if (!map
->dim
->nested
[1])
11058 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten_range
);
11061 /* Reorder the dimensions of "bmap" according to the given dim_map
11062 * and set the dimension specification to "dim" and
11063 * perform Gaussian elimination on the result.
11065 __isl_give isl_basic_map
*isl_basic_map_realign(__isl_take isl_basic_map
*bmap
,
11066 __isl_take isl_space
*dim
, __isl_take
struct isl_dim_map
*dim_map
)
11068 isl_basic_map
*res
;
11071 bmap
= isl_basic_map_cow(bmap
);
11072 if (!bmap
|| !dim
|| !dim_map
)
11075 flags
= bmap
->flags
;
11076 ISL_FL_CLR(flags
, ISL_BASIC_MAP_FINAL
);
11077 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED
);
11078 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
11079 res
= isl_basic_map_alloc_space(dim
,
11080 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
11081 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
11083 res
->flags
= flags
;
11084 res
= isl_basic_map_gauss(res
, NULL
);
11085 res
= isl_basic_map_finalize(res
);
11089 isl_basic_map_free(bmap
);
11090 isl_space_free(dim
);
11094 /* Reorder the dimensions of "map" according to given reordering.
11096 __isl_give isl_map
*isl_map_realign(__isl_take isl_map
*map
,
11097 __isl_take isl_reordering
*r
)
11100 struct isl_dim_map
*dim_map
;
11102 map
= isl_map_cow(map
);
11103 dim_map
= isl_dim_map_from_reordering(r
);
11104 if (!map
|| !r
|| !dim_map
)
11107 for (i
= 0; i
< map
->n
; ++i
) {
11108 struct isl_dim_map
*dim_map_i
;
11110 dim_map_i
= isl_dim_map_extend(dim_map
, map
->p
[i
]);
11112 map
->p
[i
] = isl_basic_map_realign(map
->p
[i
],
11113 isl_space_copy(r
->dim
), dim_map_i
);
11119 map
= isl_map_reset_space(map
, isl_space_copy(r
->dim
));
11121 isl_reordering_free(r
);
11127 isl_reordering_free(r
);
11131 __isl_give isl_set
*isl_set_realign(__isl_take isl_set
*set
,
11132 __isl_take isl_reordering
*r
)
11134 return set_from_map(isl_map_realign(set_to_map(set
), r
));
11137 __isl_give isl_map
*isl_map_align_params(__isl_take isl_map
*map
,
11138 __isl_take isl_space
*model
)
11142 if (!map
|| !model
)
11145 ctx
= isl_space_get_ctx(model
);
11146 if (!isl_space_has_named_params(model
))
11147 isl_die(ctx
, isl_error_invalid
,
11148 "model has unnamed parameters", goto error
);
11149 if (!isl_space_has_named_params(map
->dim
))
11150 isl_die(ctx
, isl_error_invalid
,
11151 "relation has unnamed parameters", goto error
);
11152 if (!isl_space_match(map
->dim
, isl_dim_param
, model
, isl_dim_param
)) {
11153 isl_reordering
*exp
;
11155 model
= isl_space_drop_dims(model
, isl_dim_in
,
11156 0, isl_space_dim(model
, isl_dim_in
));
11157 model
= isl_space_drop_dims(model
, isl_dim_out
,
11158 0, isl_space_dim(model
, isl_dim_out
));
11159 exp
= isl_parameter_alignment_reordering(map
->dim
, model
);
11160 exp
= isl_reordering_extend_space(exp
, isl_map_get_space(map
));
11161 map
= isl_map_realign(map
, exp
);
11164 isl_space_free(model
);
11167 isl_space_free(model
);
11172 __isl_give isl_set
*isl_set_align_params(__isl_take isl_set
*set
,
11173 __isl_take isl_space
*model
)
11175 return isl_map_align_params(set
, model
);
11178 /* Align the parameters of "bmap" to those of "model", introducing
11179 * additional parameters if needed.
11181 __isl_give isl_basic_map
*isl_basic_map_align_params(
11182 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*model
)
11186 if (!bmap
|| !model
)
11189 ctx
= isl_space_get_ctx(model
);
11190 if (!isl_space_has_named_params(model
))
11191 isl_die(ctx
, isl_error_invalid
,
11192 "model has unnamed parameters", goto error
);
11193 if (!isl_space_has_named_params(bmap
->dim
))
11194 isl_die(ctx
, isl_error_invalid
,
11195 "relation has unnamed parameters", goto error
);
11196 if (!isl_space_match(bmap
->dim
, isl_dim_param
, model
, isl_dim_param
)) {
11197 isl_reordering
*exp
;
11198 struct isl_dim_map
*dim_map
;
11200 model
= isl_space_drop_dims(model
, isl_dim_in
,
11201 0, isl_space_dim(model
, isl_dim_in
));
11202 model
= isl_space_drop_dims(model
, isl_dim_out
,
11203 0, isl_space_dim(model
, isl_dim_out
));
11204 exp
= isl_parameter_alignment_reordering(bmap
->dim
, model
);
11205 exp
= isl_reordering_extend_space(exp
,
11206 isl_basic_map_get_space(bmap
));
11207 dim_map
= isl_dim_map_from_reordering(exp
);
11208 bmap
= isl_basic_map_realign(bmap
,
11209 exp
? isl_space_copy(exp
->dim
) : NULL
,
11210 isl_dim_map_extend(dim_map
, bmap
));
11211 isl_reordering_free(exp
);
11215 isl_space_free(model
);
11218 isl_space_free(model
);
11219 isl_basic_map_free(bmap
);
11223 /* Align the parameters of "bset" to those of "model", introducing
11224 * additional parameters if needed.
11226 __isl_give isl_basic_set
*isl_basic_set_align_params(
11227 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*model
)
11229 return isl_basic_map_align_params(bset
, model
);
11232 __isl_give isl_mat
*isl_basic_map_equalities_matrix(
11233 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
11234 enum isl_dim_type c2
, enum isl_dim_type c3
,
11235 enum isl_dim_type c4
, enum isl_dim_type c5
)
11237 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11238 struct isl_mat
*mat
;
11244 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_eq
,
11245 isl_basic_map_total_dim(bmap
) + 1);
11248 for (i
= 0; i
< bmap
->n_eq
; ++i
)
11249 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11250 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11251 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11252 isl_int_set(mat
->row
[i
][pos
],
11253 bmap
->eq
[i
][off
+ k
]);
11261 __isl_give isl_mat
*isl_basic_map_inequalities_matrix(
11262 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
11263 enum isl_dim_type c2
, enum isl_dim_type c3
,
11264 enum isl_dim_type c4
, enum isl_dim_type c5
)
11266 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11267 struct isl_mat
*mat
;
11273 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_ineq
,
11274 isl_basic_map_total_dim(bmap
) + 1);
11277 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
11278 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11279 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11280 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11281 isl_int_set(mat
->row
[i
][pos
],
11282 bmap
->ineq
[i
][off
+ k
]);
11290 __isl_give isl_basic_map
*isl_basic_map_from_constraint_matrices(
11291 __isl_take isl_space
*dim
,
11292 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
11293 enum isl_dim_type c2
, enum isl_dim_type c3
,
11294 enum isl_dim_type c4
, enum isl_dim_type c5
)
11296 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11297 isl_basic_map
*bmap
;
11303 if (!dim
|| !eq
|| !ineq
)
11306 if (eq
->n_col
!= ineq
->n_col
)
11307 isl_die(dim
->ctx
, isl_error_invalid
,
11308 "equalities and inequalities matrices should have "
11309 "same number of columns", goto error
);
11311 total
= 1 + isl_space_dim(dim
, isl_dim_all
);
11313 if (eq
->n_col
< total
)
11314 isl_die(dim
->ctx
, isl_error_invalid
,
11315 "number of columns too small", goto error
);
11317 extra
= eq
->n_col
- total
;
11319 bmap
= isl_basic_map_alloc_space(isl_space_copy(dim
), extra
,
11320 eq
->n_row
, ineq
->n_row
);
11323 for (i
= 0; i
< extra
; ++i
) {
11324 k
= isl_basic_map_alloc_div(bmap
);
11327 isl_int_set_si(bmap
->div
[k
][0], 0);
11329 for (i
= 0; i
< eq
->n_row
; ++i
) {
11330 l
= isl_basic_map_alloc_equality(bmap
);
11333 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11334 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11335 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11336 isl_int_set(bmap
->eq
[l
][off
+ k
],
11342 for (i
= 0; i
< ineq
->n_row
; ++i
) {
11343 l
= isl_basic_map_alloc_inequality(bmap
);
11346 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11347 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11348 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11349 isl_int_set(bmap
->ineq
[l
][off
+ k
],
11350 ineq
->row
[i
][pos
]);
11356 isl_space_free(dim
);
11358 isl_mat_free(ineq
);
11360 bmap
= isl_basic_map_simplify(bmap
);
11361 return isl_basic_map_finalize(bmap
);
11363 isl_space_free(dim
);
11365 isl_mat_free(ineq
);
11369 __isl_give isl_mat
*isl_basic_set_equalities_matrix(
11370 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
11371 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
11373 return isl_basic_map_equalities_matrix(bset_to_bmap(bset
),
11374 c1
, c2
, c3
, c4
, isl_dim_in
);
11377 __isl_give isl_mat
*isl_basic_set_inequalities_matrix(
11378 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
11379 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
11381 return isl_basic_map_inequalities_matrix(bset_to_bmap(bset
),
11382 c1
, c2
, c3
, c4
, isl_dim_in
);
11385 __isl_give isl_basic_set
*isl_basic_set_from_constraint_matrices(
11386 __isl_take isl_space
*dim
,
11387 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
11388 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
11390 isl_basic_map
*bmap
;
11391 bmap
= isl_basic_map_from_constraint_matrices(dim
, eq
, ineq
,
11392 c1
, c2
, c3
, c4
, isl_dim_in
);
11393 return bset_from_bmap(bmap
);
11396 isl_bool
isl_basic_map_can_zip(__isl_keep isl_basic_map
*bmap
)
11399 return isl_bool_error
;
11401 return isl_space_can_zip(bmap
->dim
);
11404 isl_bool
isl_map_can_zip(__isl_keep isl_map
*map
)
11407 return isl_bool_error
;
11409 return isl_space_can_zip(map
->dim
);
11412 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
11413 * (A -> C) -> (B -> D).
11415 __isl_give isl_basic_map
*isl_basic_map_zip(__isl_take isl_basic_map
*bmap
)
11424 if (!isl_basic_map_can_zip(bmap
))
11425 isl_die(bmap
->ctx
, isl_error_invalid
,
11426 "basic map cannot be zipped", goto error
);
11427 pos
= isl_basic_map_offset(bmap
, isl_dim_in
) +
11428 isl_space_dim(bmap
->dim
->nested
[0], isl_dim_in
);
11429 n1
= isl_space_dim(bmap
->dim
->nested
[0], isl_dim_out
);
11430 n2
= isl_space_dim(bmap
->dim
->nested
[1], isl_dim_in
);
11431 bmap
= isl_basic_map_cow(bmap
);
11432 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
11435 bmap
->dim
= isl_space_zip(bmap
->dim
);
11438 bmap
= isl_basic_map_mark_final(bmap
);
11441 isl_basic_map_free(bmap
);
11445 /* Given a map (A -> B) -> (C -> D), return the corresponding map
11446 * (A -> C) -> (B -> D).
11448 __isl_give isl_map
*isl_map_zip(__isl_take isl_map
*map
)
11455 if (!isl_map_can_zip(map
))
11456 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be zipped",
11459 map
= isl_map_cow(map
);
11463 for (i
= 0; i
< map
->n
; ++i
) {
11464 map
->p
[i
] = isl_basic_map_zip(map
->p
[i
]);
11469 map
->dim
= isl_space_zip(map
->dim
);
11479 /* Can we apply isl_basic_map_curry to "bmap"?
11480 * That is, does it have a nested relation in its domain?
11482 isl_bool
isl_basic_map_can_curry(__isl_keep isl_basic_map
*bmap
)
11485 return isl_bool_error
;
11487 return isl_space_can_curry(bmap
->dim
);
11490 /* Can we apply isl_map_curry to "map"?
11491 * That is, does it have a nested relation in its domain?
11493 isl_bool
isl_map_can_curry(__isl_keep isl_map
*map
)
11496 return isl_bool_error
;
11498 return isl_space_can_curry(map
->dim
);
11501 /* Given a basic map (A -> B) -> C, return the corresponding basic map
11504 __isl_give isl_basic_map
*isl_basic_map_curry(__isl_take isl_basic_map
*bmap
)
11510 if (!isl_basic_map_can_curry(bmap
))
11511 isl_die(bmap
->ctx
, isl_error_invalid
,
11512 "basic map cannot be curried", goto error
);
11513 bmap
= isl_basic_map_cow(bmap
);
11516 bmap
->dim
= isl_space_curry(bmap
->dim
);
11519 bmap
= isl_basic_map_mark_final(bmap
);
11522 isl_basic_map_free(bmap
);
11526 /* Given a map (A -> B) -> C, return the corresponding map
11529 __isl_give isl_map
*isl_map_curry(__isl_take isl_map
*map
)
11531 return isl_map_change_space(map
, &isl_map_can_curry
,
11532 "map cannot be curried", &isl_space_curry
);
11535 /* Can isl_map_range_curry be applied to "map"?
11536 * That is, does it have a nested relation in its range,
11537 * the domain of which is itself a nested relation?
11539 isl_bool
isl_map_can_range_curry(__isl_keep isl_map
*map
)
11542 return isl_bool_error
;
11544 return isl_space_can_range_curry(map
->dim
);
11547 /* Given a map A -> ((B -> C) -> D), return the corresponding map
11548 * A -> (B -> (C -> D)).
11550 __isl_give isl_map
*isl_map_range_curry(__isl_take isl_map
*map
)
11552 return isl_map_change_space(map
, &isl_map_can_range_curry
,
11553 "map range cannot be curried",
11554 &isl_space_range_curry
);
11557 /* Can we apply isl_basic_map_uncurry to "bmap"?
11558 * That is, does it have a nested relation in its domain?
11560 isl_bool
isl_basic_map_can_uncurry(__isl_keep isl_basic_map
*bmap
)
11563 return isl_bool_error
;
11565 return isl_space_can_uncurry(bmap
->dim
);
11568 /* Can we apply isl_map_uncurry to "map"?
11569 * That is, does it have a nested relation in its domain?
11571 isl_bool
isl_map_can_uncurry(__isl_keep isl_map
*map
)
11574 return isl_bool_error
;
11576 return isl_space_can_uncurry(map
->dim
);
11579 /* Given a basic map A -> (B -> C), return the corresponding basic map
11582 __isl_give isl_basic_map
*isl_basic_map_uncurry(__isl_take isl_basic_map
*bmap
)
11588 if (!isl_basic_map_can_uncurry(bmap
))
11589 isl_die(bmap
->ctx
, isl_error_invalid
,
11590 "basic map cannot be uncurried",
11591 return isl_basic_map_free(bmap
));
11592 bmap
= isl_basic_map_cow(bmap
);
11595 bmap
->dim
= isl_space_uncurry(bmap
->dim
);
11597 return isl_basic_map_free(bmap
);
11598 bmap
= isl_basic_map_mark_final(bmap
);
11602 /* Given a map A -> (B -> C), return the corresponding map
11605 __isl_give isl_map
*isl_map_uncurry(__isl_take isl_map
*map
)
11607 return isl_map_change_space(map
, &isl_map_can_uncurry
,
11608 "map cannot be uncurried", &isl_space_uncurry
);
11611 /* Construct a basic map mapping the domain of the affine expression
11612 * to a one-dimensional range prescribed by the affine expression.
11613 * If "rational" is set, then construct a rational basic map.
11615 * A NaN affine expression cannot be converted to a basic map.
11617 static __isl_give isl_basic_map
*isl_basic_map_from_aff2(
11618 __isl_take isl_aff
*aff
, int rational
)
11623 isl_local_space
*ls
;
11624 isl_basic_map
*bmap
= NULL
;
11628 is_nan
= isl_aff_is_nan(aff
);
11632 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
11633 "cannot convert NaN", goto error
);
11635 ls
= isl_aff_get_local_space(aff
);
11636 bmap
= isl_basic_map_from_local_space(ls
);
11637 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
11638 k
= isl_basic_map_alloc_equality(bmap
);
11642 pos
= isl_basic_map_offset(bmap
, isl_dim_out
);
11643 isl_seq_cpy(bmap
->eq
[k
], aff
->v
->el
+ 1, pos
);
11644 isl_int_neg(bmap
->eq
[k
][pos
], aff
->v
->el
[0]);
11645 isl_seq_cpy(bmap
->eq
[k
] + pos
+ 1, aff
->v
->el
+ 1 + pos
,
11646 aff
->v
->size
- (pos
+ 1));
11650 bmap
= isl_basic_map_set_rational(bmap
);
11651 bmap
= isl_basic_map_finalize(bmap
);
11655 isl_basic_map_free(bmap
);
11659 /* Construct a basic map mapping the domain of the affine expression
11660 * to a one-dimensional range prescribed by the affine expression.
11662 __isl_give isl_basic_map
*isl_basic_map_from_aff(__isl_take isl_aff
*aff
)
11664 return isl_basic_map_from_aff2(aff
, 0);
11667 /* Construct a map mapping the domain of the affine expression
11668 * to a one-dimensional range prescribed by the affine expression.
11670 __isl_give isl_map
*isl_map_from_aff(__isl_take isl_aff
*aff
)
11672 isl_basic_map
*bmap
;
11674 bmap
= isl_basic_map_from_aff(aff
);
11675 return isl_map_from_basic_map(bmap
);
11678 /* Construct a basic map mapping the domain the multi-affine expression
11679 * to its range, with each dimension in the range equated to the
11680 * corresponding affine expression.
11681 * If "rational" is set, then construct a rational basic map.
11683 __isl_give isl_basic_map
*isl_basic_map_from_multi_aff2(
11684 __isl_take isl_multi_aff
*maff
, int rational
)
11688 isl_basic_map
*bmap
;
11693 if (isl_space_dim(maff
->space
, isl_dim_out
) != maff
->n
)
11694 isl_die(isl_multi_aff_get_ctx(maff
), isl_error_internal
,
11695 "invalid space", goto error
);
11697 space
= isl_space_domain(isl_multi_aff_get_space(maff
));
11698 bmap
= isl_basic_map_universe(isl_space_from_domain(space
));
11700 bmap
= isl_basic_map_set_rational(bmap
);
11702 for (i
= 0; i
< maff
->n
; ++i
) {
11704 isl_basic_map
*bmap_i
;
11706 aff
= isl_aff_copy(maff
->p
[i
]);
11707 bmap_i
= isl_basic_map_from_aff2(aff
, rational
);
11709 bmap
= isl_basic_map_flat_range_product(bmap
, bmap_i
);
11712 bmap
= isl_basic_map_reset_space(bmap
, isl_multi_aff_get_space(maff
));
11714 isl_multi_aff_free(maff
);
11717 isl_multi_aff_free(maff
);
11721 /* Construct a basic map mapping the domain the multi-affine expression
11722 * to its range, with each dimension in the range equated to the
11723 * corresponding affine expression.
11725 __isl_give isl_basic_map
*isl_basic_map_from_multi_aff(
11726 __isl_take isl_multi_aff
*ma
)
11728 return isl_basic_map_from_multi_aff2(ma
, 0);
11731 /* Construct a map mapping the domain the multi-affine expression
11732 * to its range, with each dimension in the range equated to the
11733 * corresponding affine expression.
11735 __isl_give isl_map
*isl_map_from_multi_aff(__isl_take isl_multi_aff
*maff
)
11737 isl_basic_map
*bmap
;
11739 bmap
= isl_basic_map_from_multi_aff(maff
);
11740 return isl_map_from_basic_map(bmap
);
11743 /* Construct a basic map mapping a domain in the given space to
11744 * to an n-dimensional range, with n the number of elements in the list,
11745 * where each coordinate in the range is prescribed by the
11746 * corresponding affine expression.
11747 * The domains of all affine expressions in the list are assumed to match
11750 __isl_give isl_basic_map
*isl_basic_map_from_aff_list(
11751 __isl_take isl_space
*domain_dim
, __isl_take isl_aff_list
*list
)
11755 isl_basic_map
*bmap
;
11760 dim
= isl_space_from_domain(domain_dim
);
11761 bmap
= isl_basic_map_universe(dim
);
11763 for (i
= 0; i
< list
->n
; ++i
) {
11765 isl_basic_map
*bmap_i
;
11767 aff
= isl_aff_copy(list
->p
[i
]);
11768 bmap_i
= isl_basic_map_from_aff(aff
);
11770 bmap
= isl_basic_map_flat_range_product(bmap
, bmap_i
);
11773 isl_aff_list_free(list
);
11777 __isl_give isl_set
*isl_set_equate(__isl_take isl_set
*set
,
11778 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11780 return isl_map_equate(set
, type1
, pos1
, type2
, pos2
);
11783 /* Construct a basic map where the given dimensions are equal to each other.
11785 static __isl_give isl_basic_map
*equator(__isl_take isl_space
*space
,
11786 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11788 isl_basic_map
*bmap
= NULL
;
11794 if (pos1
>= isl_space_dim(space
, type1
))
11795 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
11796 "index out of bounds", goto error
);
11797 if (pos2
>= isl_space_dim(space
, type2
))
11798 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
11799 "index out of bounds", goto error
);
11801 if (type1
== type2
&& pos1
== pos2
)
11802 return isl_basic_map_universe(space
);
11804 bmap
= isl_basic_map_alloc_space(isl_space_copy(space
), 0, 1, 0);
11805 i
= isl_basic_map_alloc_equality(bmap
);
11808 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
11809 pos1
+= isl_basic_map_offset(bmap
, type1
);
11810 pos2
+= isl_basic_map_offset(bmap
, type2
);
11811 isl_int_set_si(bmap
->eq
[i
][pos1
], -1);
11812 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
11813 bmap
= isl_basic_map_finalize(bmap
);
11814 isl_space_free(space
);
11817 isl_space_free(space
);
11818 isl_basic_map_free(bmap
);
11822 /* Add a constraint imposing that the given two dimensions are equal.
11824 __isl_give isl_basic_map
*isl_basic_map_equate(__isl_take isl_basic_map
*bmap
,
11825 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11829 eq
= equator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
11831 bmap
= isl_basic_map_intersect(bmap
, eq
);
11836 /* Add a constraint imposing that the given two dimensions are equal.
11838 __isl_give isl_map
*isl_map_equate(__isl_take isl_map
*map
,
11839 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11841 isl_basic_map
*bmap
;
11843 bmap
= equator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
11845 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
11850 /* Add a constraint imposing that the given two dimensions have opposite values.
11852 __isl_give isl_map
*isl_map_oppose(__isl_take isl_map
*map
,
11853 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11855 isl_basic_map
*bmap
= NULL
;
11861 if (pos1
>= isl_map_dim(map
, type1
))
11862 isl_die(map
->ctx
, isl_error_invalid
,
11863 "index out of bounds", goto error
);
11864 if (pos2
>= isl_map_dim(map
, type2
))
11865 isl_die(map
->ctx
, isl_error_invalid
,
11866 "index out of bounds", goto error
);
11868 bmap
= isl_basic_map_alloc_space(isl_map_get_space(map
), 0, 1, 0);
11869 i
= isl_basic_map_alloc_equality(bmap
);
11872 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
11873 pos1
+= isl_basic_map_offset(bmap
, type1
);
11874 pos2
+= isl_basic_map_offset(bmap
, type2
);
11875 isl_int_set_si(bmap
->eq
[i
][pos1
], 1);
11876 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
11877 bmap
= isl_basic_map_finalize(bmap
);
11879 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
11883 isl_basic_map_free(bmap
);
11888 /* Construct a constraint imposing that the value of the first dimension is
11889 * greater than or equal to that of the second.
11891 static __isl_give isl_constraint
*constraint_order_ge(
11892 __isl_take isl_space
*space
, enum isl_dim_type type1
, int pos1
,
11893 enum isl_dim_type type2
, int pos2
)
11900 c
= isl_constraint_alloc_inequality(isl_local_space_from_space(space
));
11902 if (pos1
>= isl_constraint_dim(c
, type1
))
11903 isl_die(isl_constraint_get_ctx(c
), isl_error_invalid
,
11904 "index out of bounds", return isl_constraint_free(c
));
11905 if (pos2
>= isl_constraint_dim(c
, type2
))
11906 isl_die(isl_constraint_get_ctx(c
), isl_error_invalid
,
11907 "index out of bounds", return isl_constraint_free(c
));
11909 if (type1
== type2
&& pos1
== pos2
)
11912 c
= isl_constraint_set_coefficient_si(c
, type1
, pos1
, 1);
11913 c
= isl_constraint_set_coefficient_si(c
, type2
, pos2
, -1);
11918 /* Add a constraint imposing that the value of the first dimension is
11919 * greater than or equal to that of the second.
11921 __isl_give isl_basic_map
*isl_basic_map_order_ge(__isl_take isl_basic_map
*bmap
,
11922 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11927 if (type1
== type2
&& pos1
== pos2
)
11929 space
= isl_basic_map_get_space(bmap
);
11930 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
11931 bmap
= isl_basic_map_add_constraint(bmap
, c
);
11936 /* Add a constraint imposing that the value of the first dimension is
11937 * greater than or equal to that of the second.
11939 __isl_give isl_map
*isl_map_order_ge(__isl_take isl_map
*map
,
11940 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11945 if (type1
== type2
&& pos1
== pos2
)
11947 space
= isl_map_get_space(map
);
11948 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
11949 map
= isl_map_add_constraint(map
, c
);
11954 /* Add a constraint imposing that the value of the first dimension is
11955 * less than or equal to that of the second.
11957 __isl_give isl_map
*isl_map_order_le(__isl_take isl_map
*map
,
11958 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11960 return isl_map_order_ge(map
, type2
, pos2
, type1
, pos1
);
11963 /* Construct a basic map where the value of the first dimension is
11964 * greater than that of the second.
11966 static __isl_give isl_basic_map
*greator(__isl_take isl_space
*space
,
11967 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11969 isl_basic_map
*bmap
= NULL
;
11975 if (pos1
>= isl_space_dim(space
, type1
))
11976 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
11977 "index out of bounds", goto error
);
11978 if (pos2
>= isl_space_dim(space
, type2
))
11979 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
11980 "index out of bounds", goto error
);
11982 if (type1
== type2
&& pos1
== pos2
)
11983 return isl_basic_map_empty(space
);
11985 bmap
= isl_basic_map_alloc_space(space
, 0, 0, 1);
11986 i
= isl_basic_map_alloc_inequality(bmap
);
11988 return isl_basic_map_free(bmap
);
11989 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
11990 pos1
+= isl_basic_map_offset(bmap
, type1
);
11991 pos2
+= isl_basic_map_offset(bmap
, type2
);
11992 isl_int_set_si(bmap
->ineq
[i
][pos1
], 1);
11993 isl_int_set_si(bmap
->ineq
[i
][pos2
], -1);
11994 isl_int_set_si(bmap
->ineq
[i
][0], -1);
11995 bmap
= isl_basic_map_finalize(bmap
);
11999 isl_space_free(space
);
12000 isl_basic_map_free(bmap
);
12004 /* Add a constraint imposing that the value of the first dimension is
12005 * greater than that of the second.
12007 __isl_give isl_basic_map
*isl_basic_map_order_gt(__isl_take isl_basic_map
*bmap
,
12008 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12012 gt
= greator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
12014 bmap
= isl_basic_map_intersect(bmap
, gt
);
12019 /* Add a constraint imposing that the value of the first dimension is
12020 * greater than that of the second.
12022 __isl_give isl_map
*isl_map_order_gt(__isl_take isl_map
*map
,
12023 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12025 isl_basic_map
*bmap
;
12027 bmap
= greator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
12029 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
12034 /* Add a constraint imposing that the value of the first dimension is
12035 * smaller than that of the second.
12037 __isl_give isl_map
*isl_map_order_lt(__isl_take isl_map
*map
,
12038 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12040 return isl_map_order_gt(map
, type2
, pos2
, type1
, pos1
);
12043 __isl_give isl_aff
*isl_basic_map_get_div(__isl_keep isl_basic_map
*bmap
,
12047 isl_local_space
*ls
;
12052 if (!isl_basic_map_divs_known(bmap
))
12053 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12054 "some divs are unknown", return NULL
);
12056 ls
= isl_basic_map_get_local_space(bmap
);
12057 div
= isl_local_space_get_div(ls
, pos
);
12058 isl_local_space_free(ls
);
12063 __isl_give isl_aff
*isl_basic_set_get_div(__isl_keep isl_basic_set
*bset
,
12066 return isl_basic_map_get_div(bset
, pos
);
12069 /* Plug in "subs" for dimension "type", "pos" of "bset".
12071 * Let i be the dimension to replace and let "subs" be of the form
12075 * Any integer division with a non-zero coefficient for i,
12077 * floor((a i + g)/m)
12081 * floor((a f + d g)/(m d))
12083 * Constraints of the form
12091 * We currently require that "subs" is an integral expression.
12092 * Handling rational expressions may require us to add stride constraints
12093 * as we do in isl_basic_set_preimage_multi_aff.
12095 __isl_give isl_basic_set
*isl_basic_set_substitute(
12096 __isl_take isl_basic_set
*bset
,
12097 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
12103 if (bset
&& isl_basic_set_plain_is_empty(bset
))
12106 bset
= isl_basic_set_cow(bset
);
12107 if (!bset
|| !subs
)
12110 ctx
= isl_basic_set_get_ctx(bset
);
12111 if (!isl_space_is_equal(bset
->dim
, subs
->ls
->dim
))
12112 isl_die(ctx
, isl_error_invalid
,
12113 "spaces don't match", goto error
);
12114 if (isl_local_space_dim(subs
->ls
, isl_dim_div
) != 0)
12115 isl_die(ctx
, isl_error_unsupported
,
12116 "cannot handle divs yet", goto error
);
12117 if (!isl_int_is_one(subs
->v
->el
[0]))
12118 isl_die(ctx
, isl_error_invalid
,
12119 "can only substitute integer expressions", goto error
);
12121 pos
+= isl_basic_set_offset(bset
, type
);
12125 for (i
= 0; i
< bset
->n_eq
; ++i
) {
12126 if (isl_int_is_zero(bset
->eq
[i
][pos
]))
12128 isl_int_set(v
, bset
->eq
[i
][pos
]);
12129 isl_int_set_si(bset
->eq
[i
][pos
], 0);
12130 isl_seq_combine(bset
->eq
[i
], subs
->v
->el
[0], bset
->eq
[i
],
12131 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12134 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
12135 if (isl_int_is_zero(bset
->ineq
[i
][pos
]))
12137 isl_int_set(v
, bset
->ineq
[i
][pos
]);
12138 isl_int_set_si(bset
->ineq
[i
][pos
], 0);
12139 isl_seq_combine(bset
->ineq
[i
], subs
->v
->el
[0], bset
->ineq
[i
],
12140 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12143 for (i
= 0; i
< bset
->n_div
; ++i
) {
12144 if (isl_int_is_zero(bset
->div
[i
][1 + pos
]))
12146 isl_int_set(v
, bset
->div
[i
][1 + pos
]);
12147 isl_int_set_si(bset
->div
[i
][1 + pos
], 0);
12148 isl_seq_combine(bset
->div
[i
] + 1,
12149 subs
->v
->el
[0], bset
->div
[i
] + 1,
12150 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12151 isl_int_mul(bset
->div
[i
][0], bset
->div
[i
][0], subs
->v
->el
[0]);
12156 bset
= isl_basic_set_simplify(bset
);
12157 return isl_basic_set_finalize(bset
);
12159 isl_basic_set_free(bset
);
12163 /* Plug in "subs" for dimension "type", "pos" of "set".
12165 __isl_give isl_set
*isl_set_substitute(__isl_take isl_set
*set
,
12166 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
12170 if (set
&& isl_set_plain_is_empty(set
))
12173 set
= isl_set_cow(set
);
12177 for (i
= set
->n
- 1; i
>= 0; --i
) {
12178 set
->p
[i
] = isl_basic_set_substitute(set
->p
[i
], type
, pos
, subs
);
12179 if (remove_if_empty(set
, i
) < 0)
12189 /* Check if the range of "ma" is compatible with the domain or range
12190 * (depending on "type") of "bmap".
12192 static isl_stat
check_basic_map_compatible_range_multi_aff(
12193 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type type
,
12194 __isl_keep isl_multi_aff
*ma
)
12197 isl_space
*ma_space
;
12199 ma_space
= isl_multi_aff_get_space(ma
);
12201 m
= isl_space_match(bmap
->dim
, isl_dim_param
, ma_space
, isl_dim_param
);
12205 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12206 "parameters don't match", goto error
);
12207 m
= isl_space_tuple_is_equal(bmap
->dim
, type
, ma_space
, isl_dim_out
);
12211 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12212 "spaces don't match", goto error
);
12214 isl_space_free(ma_space
);
12215 return isl_stat_ok
;
12217 isl_space_free(ma_space
);
12218 return isl_stat_error
;
12221 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12222 * coefficients before the transformed range of dimensions,
12223 * the "n_after" coefficients after the transformed range of dimensions
12224 * and the coefficients of the other divs in "bmap".
12226 static int set_ma_divs(__isl_keep isl_basic_map
*bmap
,
12227 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
, int n_div
)
12232 isl_local_space
*ls
;
12237 ls
= isl_aff_get_domain_local_space(ma
->p
[0]);
12241 n_param
= isl_local_space_dim(ls
, isl_dim_param
);
12242 n_set
= isl_local_space_dim(ls
, isl_dim_set
);
12243 for (i
= 0; i
< n_div
; ++i
) {
12244 int o_bmap
= 0, o_ls
= 0;
12246 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], 1 + 1 + n_param
);
12247 o_bmap
+= 1 + 1 + n_param
;
12248 o_ls
+= 1 + 1 + n_param
;
12249 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_before
);
12250 o_bmap
+= n_before
;
12251 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
12252 ls
->div
->row
[i
] + o_ls
, n_set
);
12255 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_after
);
12257 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
12258 ls
->div
->row
[i
] + o_ls
, n_div
);
12261 isl_seq_clr(bmap
->div
[i
] + o_bmap
, bmap
->n_div
- n_div
);
12262 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
12266 isl_local_space_free(ls
);
12269 isl_local_space_free(ls
);
12273 /* How many stride constraints does "ma" enforce?
12274 * That is, how many of the affine expressions have a denominator
12275 * different from one?
12277 static int multi_aff_strides(__isl_keep isl_multi_aff
*ma
)
12282 for (i
= 0; i
< ma
->n
; ++i
)
12283 if (!isl_int_is_one(ma
->p
[i
]->v
->el
[0]))
12289 /* For each affine expression in ma of the form
12291 * x_i = (f_i y + h_i)/m_i
12293 * with m_i different from one, add a constraint to "bmap"
12296 * f_i y + h_i = m_i alpha_i
12298 * with alpha_i an additional existentially quantified variable.
12300 * The input variables of "ma" correspond to a subset of the variables
12301 * of "bmap". There are "n_before" variables in "bmap" before this
12302 * subset and "n_after" variables after this subset.
12303 * The integer divisions of the affine expressions in "ma" are assumed
12304 * to have been aligned. There are "n_div_ma" of them and
12305 * they appear first in "bmap", straight after the "n_after" variables.
12307 static __isl_give isl_basic_map
*add_ma_strides(
12308 __isl_take isl_basic_map
*bmap
, __isl_keep isl_multi_aff
*ma
,
12309 int n_before
, int n_after
, int n_div_ma
)
12317 total
= isl_basic_map_total_dim(bmap
);
12318 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
12319 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
12320 for (i
= 0; i
< ma
->n
; ++i
) {
12321 int o_bmap
= 0, o_ma
= 1;
12323 if (isl_int_is_one(ma
->p
[i
]->v
->el
[0]))
12325 div
= isl_basic_map_alloc_div(bmap
);
12326 k
= isl_basic_map_alloc_equality(bmap
);
12327 if (div
< 0 || k
< 0)
12329 isl_int_set_si(bmap
->div
[div
][0], 0);
12330 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12331 ma
->p
[i
]->v
->el
+ o_ma
, 1 + n_param
);
12332 o_bmap
+= 1 + n_param
;
12333 o_ma
+= 1 + n_param
;
12334 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_before
);
12335 o_bmap
+= n_before
;
12336 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12337 ma
->p
[i
]->v
->el
+ o_ma
, n_in
);
12340 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_after
);
12342 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12343 ma
->p
[i
]->v
->el
+ o_ma
, n_div_ma
);
12344 o_bmap
+= n_div_ma
;
12346 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, 1 + total
- o_bmap
);
12347 isl_int_neg(bmap
->eq
[k
][1 + total
], ma
->p
[i
]->v
->el
[0]);
12353 isl_basic_map_free(bmap
);
12357 /* Replace the domain or range space (depending on "type) of "space" by "set".
12359 static __isl_give isl_space
*isl_space_set(__isl_take isl_space
*space
,
12360 enum isl_dim_type type
, __isl_take isl_space
*set
)
12362 if (type
== isl_dim_in
) {
12363 space
= isl_space_range(space
);
12364 space
= isl_space_map_from_domain_and_range(set
, space
);
12366 space
= isl_space_domain(space
);
12367 space
= isl_space_map_from_domain_and_range(space
, set
);
12373 /* Compute the preimage of the domain or range (depending on "type")
12374 * of "bmap" under the function represented by "ma".
12375 * In other words, plug in "ma" in the domain or range of "bmap".
12376 * The result is a basic map that lives in the same space as "bmap"
12377 * except that the domain or range has been replaced by
12378 * the domain space of "ma".
12380 * If bmap is represented by
12382 * A(p) + S u + B x + T v + C(divs) >= 0,
12384 * where u and x are input and output dimensions if type == isl_dim_out
12385 * while x and v are input and output dimensions if type == isl_dim_in,
12386 * and ma is represented by
12388 * x = D(p) + F(y) + G(divs')
12390 * then the result is
12392 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12394 * The divs in the input set are similarly adjusted.
12397 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12401 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
12402 * B_i G(divs') + c_i(divs))/n_i)
12404 * If bmap is not a rational map and if F(y) involves any denominators
12406 * x_i = (f_i y + h_i)/m_i
12408 * then additional constraints are added to ensure that we only
12409 * map back integer points. That is we enforce
12411 * f_i y + h_i = m_i alpha_i
12413 * with alpha_i an additional existentially quantified variable.
12415 * We first copy over the divs from "ma".
12416 * Then we add the modified constraints and divs from "bmap".
12417 * Finally, we add the stride constraints, if needed.
12419 __isl_give isl_basic_map
*isl_basic_map_preimage_multi_aff(
12420 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
12421 __isl_take isl_multi_aff
*ma
)
12425 isl_basic_map
*res
= NULL
;
12426 int n_before
, n_after
, n_div_bmap
, n_div_ma
;
12427 isl_int f
, c1
, c2
, g
;
12436 ma
= isl_multi_aff_align_divs(ma
);
12439 if (check_basic_map_compatible_range_multi_aff(bmap
, type
, ma
) < 0)
12442 if (type
== isl_dim_in
) {
12444 n_after
= isl_basic_map_dim(bmap
, isl_dim_out
);
12446 n_before
= isl_basic_map_dim(bmap
, isl_dim_in
);
12449 n_div_bmap
= isl_basic_map_dim(bmap
, isl_dim_div
);
12450 n_div_ma
= ma
->n
? isl_aff_dim(ma
->p
[0], isl_dim_div
) : 0;
12452 space
= isl_multi_aff_get_domain_space(ma
);
12453 space
= isl_space_set(isl_basic_map_get_space(bmap
), type
, space
);
12454 rational
= isl_basic_map_is_rational(bmap
);
12455 strides
= rational
? 0 : multi_aff_strides(ma
);
12456 res
= isl_basic_map_alloc_space(space
, n_div_ma
+ n_div_bmap
+ strides
,
12457 bmap
->n_eq
+ strides
, bmap
->n_ineq
+ 2 * n_div_ma
);
12459 res
= isl_basic_map_set_rational(res
);
12461 for (i
= 0; i
< n_div_ma
+ n_div_bmap
; ++i
)
12462 if (isl_basic_map_alloc_div(res
) < 0)
12465 if (set_ma_divs(res
, ma
, n_before
, n_after
, n_div_ma
) < 0)
12468 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
12469 k
= isl_basic_map_alloc_equality(res
);
12472 isl_seq_preimage(res
->eq
[k
], bmap
->eq
[i
], ma
, n_before
,
12473 n_after
, n_div_ma
, n_div_bmap
, f
, c1
, c2
, g
, 0);
12476 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
12477 k
= isl_basic_map_alloc_inequality(res
);
12480 isl_seq_preimage(res
->ineq
[k
], bmap
->ineq
[i
], ma
, n_before
,
12481 n_after
, n_div_ma
, n_div_bmap
, f
, c1
, c2
, g
, 0);
12484 for (i
= 0; i
< bmap
->n_div
; ++i
) {
12485 if (isl_int_is_zero(bmap
->div
[i
][0])) {
12486 isl_int_set_si(res
->div
[n_div_ma
+ i
][0], 0);
12489 isl_seq_preimage(res
->div
[n_div_ma
+ i
], bmap
->div
[i
], ma
,
12490 n_before
, n_after
, n_div_ma
, n_div_bmap
,
12495 res
= add_ma_strides(res
, ma
, n_before
, n_after
, n_div_ma
);
12501 isl_basic_map_free(bmap
);
12502 isl_multi_aff_free(ma
);
12503 res
= isl_basic_map_simplify(res
);
12504 return isl_basic_map_finalize(res
);
12510 isl_basic_map_free(bmap
);
12511 isl_multi_aff_free(ma
);
12512 isl_basic_map_free(res
);
12516 /* Compute the preimage of "bset" under the function represented by "ma".
12517 * In other words, plug in "ma" in "bset". The result is a basic set
12518 * that lives in the domain space of "ma".
12520 __isl_give isl_basic_set
*isl_basic_set_preimage_multi_aff(
12521 __isl_take isl_basic_set
*bset
, __isl_take isl_multi_aff
*ma
)
12523 return isl_basic_map_preimage_multi_aff(bset
, isl_dim_set
, ma
);
12526 /* Compute the preimage of the domain of "bmap" under the function
12527 * represented by "ma".
12528 * In other words, plug in "ma" in the domain of "bmap".
12529 * The result is a basic map that lives in the same space as "bmap"
12530 * except that the domain has been replaced by the domain space of "ma".
12532 __isl_give isl_basic_map
*isl_basic_map_preimage_domain_multi_aff(
12533 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
12535 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_in
, ma
);
12538 /* Compute the preimage of the range of "bmap" under the function
12539 * represented by "ma".
12540 * In other words, plug in "ma" in the range of "bmap".
12541 * The result is a basic map that lives in the same space as "bmap"
12542 * except that the range has been replaced by the domain space of "ma".
12544 __isl_give isl_basic_map
*isl_basic_map_preimage_range_multi_aff(
12545 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
12547 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_out
, ma
);
12550 /* Check if the range of "ma" is compatible with the domain or range
12551 * (depending on "type") of "map".
12552 * Return isl_stat_error if anything is wrong.
12554 static isl_stat
check_map_compatible_range_multi_aff(
12555 __isl_keep isl_map
*map
, enum isl_dim_type type
,
12556 __isl_keep isl_multi_aff
*ma
)
12559 isl_space
*ma_space
;
12561 ma_space
= isl_multi_aff_get_space(ma
);
12562 m
= isl_space_tuple_is_equal(map
->dim
, type
, ma_space
, isl_dim_out
);
12563 isl_space_free(ma_space
);
12565 return isl_stat_error
;
12567 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
12568 "spaces don't match", return isl_stat_error
);
12569 return isl_stat_ok
;
12572 /* Compute the preimage of the domain or range (depending on "type")
12573 * of "map" under the function represented by "ma".
12574 * In other words, plug in "ma" in the domain or range of "map".
12575 * The result is a map that lives in the same space as "map"
12576 * except that the domain or range has been replaced by
12577 * the domain space of "ma".
12579 * The parameters are assumed to have been aligned.
12581 static __isl_give isl_map
*map_preimage_multi_aff(__isl_take isl_map
*map
,
12582 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
12587 map
= isl_map_cow(map
);
12588 ma
= isl_multi_aff_align_divs(ma
);
12591 if (check_map_compatible_range_multi_aff(map
, type
, ma
) < 0)
12594 for (i
= 0; i
< map
->n
; ++i
) {
12595 map
->p
[i
] = isl_basic_map_preimage_multi_aff(map
->p
[i
], type
,
12596 isl_multi_aff_copy(ma
));
12601 space
= isl_multi_aff_get_domain_space(ma
);
12602 space
= isl_space_set(isl_map_get_space(map
), type
, space
);
12604 isl_space_free(map
->dim
);
12609 isl_multi_aff_free(ma
);
12611 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
12612 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
12615 isl_multi_aff_free(ma
);
12620 /* Compute the preimage of the domain or range (depending on "type")
12621 * of "map" under the function represented by "ma".
12622 * In other words, plug in "ma" in the domain or range of "map".
12623 * The result is a map that lives in the same space as "map"
12624 * except that the domain or range has been replaced by
12625 * the domain space of "ma".
12627 __isl_give isl_map
*isl_map_preimage_multi_aff(__isl_take isl_map
*map
,
12628 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
12633 if (isl_space_match(map
->dim
, isl_dim_param
, ma
->space
, isl_dim_param
))
12634 return map_preimage_multi_aff(map
, type
, ma
);
12636 if (!isl_space_has_named_params(map
->dim
) ||
12637 !isl_space_has_named_params(ma
->space
))
12638 isl_die(map
->ctx
, isl_error_invalid
,
12639 "unaligned unnamed parameters", goto error
);
12640 map
= isl_map_align_params(map
, isl_multi_aff_get_space(ma
));
12641 ma
= isl_multi_aff_align_params(ma
, isl_map_get_space(map
));
12643 return map_preimage_multi_aff(map
, type
, ma
);
12645 isl_multi_aff_free(ma
);
12646 return isl_map_free(map
);
12649 /* Compute the preimage of "set" under the function represented by "ma".
12650 * In other words, plug in "ma" in "set". The result is a set
12651 * that lives in the domain space of "ma".
12653 __isl_give isl_set
*isl_set_preimage_multi_aff(__isl_take isl_set
*set
,
12654 __isl_take isl_multi_aff
*ma
)
12656 return isl_map_preimage_multi_aff(set
, isl_dim_set
, ma
);
12659 /* Compute the preimage of the domain of "map" under the function
12660 * represented by "ma".
12661 * In other words, plug in "ma" in the domain of "map".
12662 * The result is a map that lives in the same space as "map"
12663 * except that the domain has been replaced by the domain space of "ma".
12665 __isl_give isl_map
*isl_map_preimage_domain_multi_aff(__isl_take isl_map
*map
,
12666 __isl_take isl_multi_aff
*ma
)
12668 return isl_map_preimage_multi_aff(map
, isl_dim_in
, ma
);
12671 /* Compute the preimage of the range of "map" under the function
12672 * represented by "ma".
12673 * In other words, plug in "ma" in the range of "map".
12674 * The result is a map that lives in the same space as "map"
12675 * except that the range has been replaced by the domain space of "ma".
12677 __isl_give isl_map
*isl_map_preimage_range_multi_aff(__isl_take isl_map
*map
,
12678 __isl_take isl_multi_aff
*ma
)
12680 return isl_map_preimage_multi_aff(map
, isl_dim_out
, ma
);
12683 /* Compute the preimage of "map" under the function represented by "pma".
12684 * In other words, plug in "pma" in the domain or range of "map".
12685 * The result is a map that lives in the same space as "map",
12686 * except that the space of type "type" has been replaced by
12687 * the domain space of "pma".
12689 * The parameters of "map" and "pma" are assumed to have been aligned.
12691 static __isl_give isl_map
*isl_map_preimage_pw_multi_aff_aligned(
12692 __isl_take isl_map
*map
, enum isl_dim_type type
,
12693 __isl_take isl_pw_multi_aff
*pma
)
12702 isl_pw_multi_aff_free(pma
);
12703 res
= isl_map_empty(isl_map_get_space(map
));
12708 res
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
12709 isl_multi_aff_copy(pma
->p
[0].maff
));
12710 if (type
== isl_dim_in
)
12711 res
= isl_map_intersect_domain(res
,
12712 isl_map_copy(pma
->p
[0].set
));
12714 res
= isl_map_intersect_range(res
,
12715 isl_map_copy(pma
->p
[0].set
));
12717 for (i
= 1; i
< pma
->n
; ++i
) {
12720 res_i
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
12721 isl_multi_aff_copy(pma
->p
[i
].maff
));
12722 if (type
== isl_dim_in
)
12723 res_i
= isl_map_intersect_domain(res_i
,
12724 isl_map_copy(pma
->p
[i
].set
));
12726 res_i
= isl_map_intersect_range(res_i
,
12727 isl_map_copy(pma
->p
[i
].set
));
12728 res
= isl_map_union(res
, res_i
);
12731 isl_pw_multi_aff_free(pma
);
12735 isl_pw_multi_aff_free(pma
);
12740 /* Compute the preimage of "map" under the function represented by "pma".
12741 * In other words, plug in "pma" in the domain or range of "map".
12742 * The result is a map that lives in the same space as "map",
12743 * except that the space of type "type" has been replaced by
12744 * the domain space of "pma".
12746 __isl_give isl_map
*isl_map_preimage_pw_multi_aff(__isl_take isl_map
*map
,
12747 enum isl_dim_type type
, __isl_take isl_pw_multi_aff
*pma
)
12752 if (isl_space_match(map
->dim
, isl_dim_param
, pma
->dim
, isl_dim_param
))
12753 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
12755 if (!isl_space_has_named_params(map
->dim
) ||
12756 !isl_space_has_named_params(pma
->dim
))
12757 isl_die(map
->ctx
, isl_error_invalid
,
12758 "unaligned unnamed parameters", goto error
);
12759 map
= isl_map_align_params(map
, isl_pw_multi_aff_get_space(pma
));
12760 pma
= isl_pw_multi_aff_align_params(pma
, isl_map_get_space(map
));
12762 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
12764 isl_pw_multi_aff_free(pma
);
12765 return isl_map_free(map
);
12768 /* Compute the preimage of "set" under the function represented by "pma".
12769 * In other words, plug in "pma" in "set". The result is a set
12770 * that lives in the domain space of "pma".
12772 __isl_give isl_set
*isl_set_preimage_pw_multi_aff(__isl_take isl_set
*set
,
12773 __isl_take isl_pw_multi_aff
*pma
)
12775 return isl_map_preimage_pw_multi_aff(set
, isl_dim_set
, pma
);
12778 /* Compute the preimage of the domain of "map" under the function
12779 * represented by "pma".
12780 * In other words, plug in "pma" in the domain of "map".
12781 * The result is a map that lives in the same space as "map",
12782 * except that domain space has been replaced by the domain space of "pma".
12784 __isl_give isl_map
*isl_map_preimage_domain_pw_multi_aff(
12785 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
12787 return isl_map_preimage_pw_multi_aff(map
, isl_dim_in
, pma
);
12790 /* Compute the preimage of the range of "map" under the function
12791 * represented by "pma".
12792 * In other words, plug in "pma" in the range of "map".
12793 * The result is a map that lives in the same space as "map",
12794 * except that range space has been replaced by the domain space of "pma".
12796 __isl_give isl_map
*isl_map_preimage_range_pw_multi_aff(
12797 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
12799 return isl_map_preimage_pw_multi_aff(map
, isl_dim_out
, pma
);
12802 /* Compute the preimage of "map" under the function represented by "mpa".
12803 * In other words, plug in "mpa" in the domain or range of "map".
12804 * The result is a map that lives in the same space as "map",
12805 * except that the space of type "type" has been replaced by
12806 * the domain space of "mpa".
12808 * If the map does not involve any constraints that refer to the
12809 * dimensions of the substituted space, then the only possible
12810 * effect of "mpa" on the map is to map the space to a different space.
12811 * We create a separate isl_multi_aff to effectuate this change
12812 * in order to avoid spurious splitting of the map along the pieces
12815 __isl_give isl_map
*isl_map_preimage_multi_pw_aff(__isl_take isl_map
*map
,
12816 enum isl_dim_type type
, __isl_take isl_multi_pw_aff
*mpa
)
12819 isl_pw_multi_aff
*pma
;
12824 n
= isl_map_dim(map
, type
);
12825 if (!isl_map_involves_dims(map
, type
, 0, n
)) {
12829 space
= isl_multi_pw_aff_get_space(mpa
);
12830 isl_multi_pw_aff_free(mpa
);
12831 ma
= isl_multi_aff_zero(space
);
12832 return isl_map_preimage_multi_aff(map
, type
, ma
);
12835 pma
= isl_pw_multi_aff_from_multi_pw_aff(mpa
);
12836 return isl_map_preimage_pw_multi_aff(map
, type
, pma
);
12839 isl_multi_pw_aff_free(mpa
);
12843 /* Compute the preimage of "map" under the function represented by "mpa".
12844 * In other words, plug in "mpa" in the domain "map".
12845 * The result is a map that lives in the same space as "map",
12846 * except that domain space has been replaced by the domain space of "mpa".
12848 __isl_give isl_map
*isl_map_preimage_domain_multi_pw_aff(
12849 __isl_take isl_map
*map
, __isl_take isl_multi_pw_aff
*mpa
)
12851 return isl_map_preimage_multi_pw_aff(map
, isl_dim_in
, mpa
);
12854 /* Compute the preimage of "set" by the function represented by "mpa".
12855 * In other words, plug in "mpa" in "set".
12857 __isl_give isl_set
*isl_set_preimage_multi_pw_aff(__isl_take isl_set
*set
,
12858 __isl_take isl_multi_pw_aff
*mpa
)
12860 return isl_map_preimage_multi_pw_aff(set
, isl_dim_set
, mpa
);
12863 /* Are the "n" "coefficients" starting at "first" of the integer division
12864 * expressions at position "pos1" in "bmap1" and "pos2" in "bmap2" equal
12866 * The "coefficient" at position 0 is the denominator.
12867 * The "coefficient" at position 1 is the constant term.
12869 isl_bool
isl_basic_map_equal_div_expr_part(__isl_keep isl_basic_map
*bmap1
,
12870 int pos1
, __isl_keep isl_basic_map
*bmap2
, int pos2
,
12871 unsigned first
, unsigned n
)
12873 if (isl_basic_map_check_range(bmap1
, isl_dim_div
, pos1
, 1) < 0)
12874 return isl_bool_error
;
12875 if (isl_basic_map_check_range(bmap2
, isl_dim_div
, pos2
, 1) < 0)
12876 return isl_bool_error
;
12877 return isl_seq_eq(bmap1
->div
[pos1
] + first
,
12878 bmap2
->div
[pos2
] + first
, n
);
12881 /* Are the integer division expressions at position "pos1" in "bmap1" and
12882 * "pos2" in "bmap2" equal to each other, except that the constant terms
12885 isl_bool
isl_basic_map_equal_div_expr_except_constant(
12886 __isl_keep isl_basic_map
*bmap1
, int pos1
,
12887 __isl_keep isl_basic_map
*bmap2
, int pos2
)
12892 if (!bmap1
|| !bmap2
)
12893 return isl_bool_error
;
12894 total
= isl_basic_map_total_dim(bmap1
);
12895 if (total
!= isl_basic_map_total_dim(bmap2
))
12896 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
12897 "incomparable div expressions", return isl_bool_error
);
12898 equal
= isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
12900 if (equal
< 0 || !equal
)
12902 equal
= isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
12904 if (equal
< 0 || equal
)
12905 return isl_bool_not(equal
);
12906 return isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
12910 /* Replace the numerator of the constant term of the integer division
12911 * expression at position "div" in "bmap" by "value".
12912 * The caller guarantees that this does not change the meaning
12915 __isl_give isl_basic_map
*isl_basic_map_set_div_expr_constant_num_si_inplace(
12916 __isl_take isl_basic_map
*bmap
, int div
, int value
)
12918 if (isl_basic_map_check_range(bmap
, isl_dim_div
, div
, 1) < 0)
12919 return isl_basic_map_free(bmap
);
12921 isl_int_set_si(bmap
->div
[div
][1], value
);
12926 /* Is the point "inner" internal to inequality constraint "ineq"
12928 * The point is considered to be internal to the inequality constraint,
12929 * if it strictly lies on the positive side of the inequality constraint,
12930 * or if it lies on the constraint and the constraint is lexico-positive.
12932 static isl_bool
is_internal(__isl_keep isl_vec
*inner
,
12933 __isl_keep isl_basic_set
*bset
, int ineq
)
12939 if (!inner
|| !bset
)
12940 return isl_bool_error
;
12942 ctx
= isl_basic_set_get_ctx(bset
);
12943 isl_seq_inner_product(inner
->el
, bset
->ineq
[ineq
], inner
->size
,
12944 &ctx
->normalize_gcd
);
12945 if (!isl_int_is_zero(ctx
->normalize_gcd
))
12946 return isl_int_is_nonneg(ctx
->normalize_gcd
);
12948 total
= isl_basic_set_dim(bset
, isl_dim_all
);
12949 pos
= isl_seq_first_non_zero(bset
->ineq
[ineq
] + 1, total
);
12950 return isl_int_is_pos(bset
->ineq
[ineq
][1 + pos
]);
12953 /* Tighten the inequality constraints of "bset" that are outward with respect
12954 * to the point "vec".
12955 * That is, tighten the constraints that are not satisfied by "vec".
12957 * "vec" is a point internal to some superset S of "bset" that is used
12958 * to make the subsets of S disjoint, by tightening one half of the constraints
12959 * that separate two subsets. In particular, the constraints of S
12960 * are all satisfied by "vec" and should not be tightened.
12961 * Of the internal constraints, those that have "vec" on the outside
12962 * are tightened. The shared facet is included in the adjacent subset
12963 * with the opposite constraint.
12964 * For constraints that saturate "vec", this criterion cannot be used
12965 * to determine which of the two sides should be tightened.
12966 * Instead, the sign of the first non-zero coefficient is used
12967 * to make this choice. Note that this second criterion is never used
12968 * on the constraints of S since "vec" is interior to "S".
12970 __isl_give isl_basic_set
*isl_basic_set_tighten_outward(
12971 __isl_take isl_basic_set
*bset
, __isl_keep isl_vec
*vec
)
12975 bset
= isl_basic_set_cow(bset
);
12978 for (j
= 0; j
< bset
->n_ineq
; ++j
) {
12981 internal
= is_internal(vec
, bset
, j
);
12983 return isl_basic_set_free(bset
);
12986 isl_int_sub_ui(bset
->ineq
[j
][0], bset
->ineq
[j
][0], 1);