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 int isl_map_compatible_domain(struct isl_map
*map
, struct isl_set
*set
)
210 m
= isl_space_match(map
->dim
, isl_dim_param
, set
->dim
, isl_dim_param
);
213 return isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
214 set
->dim
, isl_dim_set
);
217 isl_bool
isl_basic_map_compatible_domain(__isl_keep isl_basic_map
*bmap
,
218 __isl_keep isl_basic_set
*bset
)
222 return isl_bool_error
;
223 m
= isl_space_match(bmap
->dim
, isl_dim_param
, bset
->dim
, isl_dim_param
);
226 return isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
227 bset
->dim
, isl_dim_set
);
230 int isl_map_compatible_range(__isl_keep isl_map
*map
, __isl_keep isl_set
*set
)
235 m
= isl_space_match(map
->dim
, isl_dim_param
, set
->dim
, isl_dim_param
);
238 return isl_space_tuple_is_equal(map
->dim
, isl_dim_out
,
239 set
->dim
, isl_dim_set
);
242 int isl_basic_map_compatible_range(struct isl_basic_map
*bmap
,
243 struct isl_basic_set
*bset
)
248 m
= isl_space_match(bmap
->dim
, isl_dim_param
, bset
->dim
, isl_dim_param
);
251 return isl_space_tuple_is_equal(bmap
->dim
, isl_dim_out
,
252 bset
->dim
, isl_dim_set
);
255 isl_ctx
*isl_basic_map_get_ctx(__isl_keep isl_basic_map
*bmap
)
257 return bmap
? bmap
->ctx
: NULL
;
260 isl_ctx
*isl_basic_set_get_ctx(__isl_keep isl_basic_set
*bset
)
262 return bset
? bset
->ctx
: NULL
;
265 isl_ctx
*isl_map_get_ctx(__isl_keep isl_map
*map
)
267 return map
? map
->ctx
: NULL
;
270 isl_ctx
*isl_set_get_ctx(__isl_keep isl_set
*set
)
272 return set
? set
->ctx
: NULL
;
275 __isl_give isl_space
*isl_basic_map_get_space(__isl_keep isl_basic_map
*bmap
)
279 return isl_space_copy(bmap
->dim
);
282 __isl_give isl_space
*isl_basic_set_get_space(__isl_keep isl_basic_set
*bset
)
286 return isl_space_copy(bset
->dim
);
289 /* Extract the divs in "bmap" as a matrix.
291 __isl_give isl_mat
*isl_basic_map_get_divs(__isl_keep isl_basic_map
*bmap
)
302 ctx
= isl_basic_map_get_ctx(bmap
);
303 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
304 cols
= 1 + 1 + total
+ bmap
->n_div
;
305 div
= isl_mat_alloc(ctx
, bmap
->n_div
, cols
);
309 for (i
= 0; i
< bmap
->n_div
; ++i
)
310 isl_seq_cpy(div
->row
[i
], bmap
->div
[i
], cols
);
315 /* Extract the divs in "bset" as a matrix.
317 __isl_give isl_mat
*isl_basic_set_get_divs(__isl_keep isl_basic_set
*bset
)
319 return isl_basic_map_get_divs(bset
);
322 __isl_give isl_local_space
*isl_basic_map_get_local_space(
323 __isl_keep isl_basic_map
*bmap
)
330 div
= isl_basic_map_get_divs(bmap
);
331 return isl_local_space_alloc_div(isl_space_copy(bmap
->dim
), div
);
334 __isl_give isl_local_space
*isl_basic_set_get_local_space(
335 __isl_keep isl_basic_set
*bset
)
337 return isl_basic_map_get_local_space(bset
);
340 /* For each known div d = floor(f/m), add the constraints
343 * -(f-(m-1)) + m d >= 0
345 * Do not finalize the result.
347 static __isl_give isl_basic_map
*add_known_div_constraints(
348 __isl_take isl_basic_map
*bmap
)
355 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
358 bmap
= isl_basic_map_cow(bmap
);
359 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 2 * n_div
);
362 for (i
= 0; i
< n_div
; ++i
) {
363 if (isl_int_is_zero(bmap
->div
[i
][0]))
365 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
366 return isl_basic_map_free(bmap
);
372 __isl_give isl_basic_map
*isl_basic_map_from_local_space(
373 __isl_take isl_local_space
*ls
)
382 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
383 bmap
= isl_basic_map_alloc_space(isl_local_space_get_space(ls
),
384 n_div
, 0, 2 * n_div
);
386 for (i
= 0; i
< n_div
; ++i
)
387 if (isl_basic_map_alloc_div(bmap
) < 0)
390 for (i
= 0; i
< n_div
; ++i
)
391 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], ls
->div
->n_col
);
392 bmap
= add_known_div_constraints(bmap
);
394 isl_local_space_free(ls
);
397 isl_local_space_free(ls
);
398 isl_basic_map_free(bmap
);
402 __isl_give isl_basic_set
*isl_basic_set_from_local_space(
403 __isl_take isl_local_space
*ls
)
405 return isl_basic_map_from_local_space(ls
);
408 __isl_give isl_space
*isl_map_get_space(__isl_keep isl_map
*map
)
412 return isl_space_copy(map
->dim
);
415 __isl_give isl_space
*isl_set_get_space(__isl_keep isl_set
*set
)
419 return isl_space_copy(set
->dim
);
422 __isl_give isl_basic_map
*isl_basic_map_set_tuple_name(
423 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
, const char *s
)
425 bmap
= isl_basic_map_cow(bmap
);
428 bmap
->dim
= isl_space_set_tuple_name(bmap
->dim
, type
, s
);
431 bmap
= isl_basic_map_finalize(bmap
);
434 isl_basic_map_free(bmap
);
438 __isl_give isl_basic_set
*isl_basic_set_set_tuple_name(
439 __isl_take isl_basic_set
*bset
, const char *s
)
441 return isl_basic_map_set_tuple_name(bset
, isl_dim_set
, s
);
444 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map
*bmap
,
445 enum isl_dim_type type
)
447 return bmap
? isl_space_get_tuple_name(bmap
->dim
, type
) : NULL
;
450 __isl_give isl_map
*isl_map_set_tuple_name(__isl_take isl_map
*map
,
451 enum isl_dim_type type
, const char *s
)
455 map
= isl_map_cow(map
);
459 map
->dim
= isl_space_set_tuple_name(map
->dim
, type
, s
);
463 for (i
= 0; i
< map
->n
; ++i
) {
464 map
->p
[i
] = isl_basic_map_set_tuple_name(map
->p
[i
], type
, s
);
475 /* Replace the identifier of the tuple of type "type" by "id".
477 __isl_give isl_basic_map
*isl_basic_map_set_tuple_id(
478 __isl_take isl_basic_map
*bmap
,
479 enum isl_dim_type type
, __isl_take isl_id
*id
)
481 bmap
= isl_basic_map_cow(bmap
);
484 bmap
->dim
= isl_space_set_tuple_id(bmap
->dim
, type
, id
);
486 return isl_basic_map_free(bmap
);
487 bmap
= isl_basic_map_finalize(bmap
);
494 /* Replace the identifier of the tuple by "id".
496 __isl_give isl_basic_set
*isl_basic_set_set_tuple_id(
497 __isl_take isl_basic_set
*bset
, __isl_take isl_id
*id
)
499 return isl_basic_map_set_tuple_id(bset
, isl_dim_set
, id
);
502 /* Does the input or output tuple have a name?
504 isl_bool
isl_map_has_tuple_name(__isl_keep isl_map
*map
, enum isl_dim_type type
)
506 return map
? isl_space_has_tuple_name(map
->dim
, type
) : isl_bool_error
;
509 const char *isl_map_get_tuple_name(__isl_keep isl_map
*map
,
510 enum isl_dim_type type
)
512 return map
? isl_space_get_tuple_name(map
->dim
, type
) : NULL
;
515 __isl_give isl_set
*isl_set_set_tuple_name(__isl_take isl_set
*set
,
518 return set_from_map(isl_map_set_tuple_name(set_to_map(set
),
522 __isl_give isl_map
*isl_map_set_tuple_id(__isl_take isl_map
*map
,
523 enum isl_dim_type type
, __isl_take isl_id
*id
)
525 map
= isl_map_cow(map
);
529 map
->dim
= isl_space_set_tuple_id(map
->dim
, type
, id
);
531 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
537 __isl_give isl_set
*isl_set_set_tuple_id(__isl_take isl_set
*set
,
538 __isl_take isl_id
*id
)
540 return isl_map_set_tuple_id(set
, isl_dim_set
, id
);
543 __isl_give isl_map
*isl_map_reset_tuple_id(__isl_take isl_map
*map
,
544 enum isl_dim_type type
)
546 map
= isl_map_cow(map
);
550 map
->dim
= isl_space_reset_tuple_id(map
->dim
, type
);
552 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
555 __isl_give isl_set
*isl_set_reset_tuple_id(__isl_take isl_set
*set
)
557 return isl_map_reset_tuple_id(set
, isl_dim_set
);
560 isl_bool
isl_map_has_tuple_id(__isl_keep isl_map
*map
, enum isl_dim_type type
)
562 return map
? isl_space_has_tuple_id(map
->dim
, type
) : isl_bool_error
;
565 __isl_give isl_id
*isl_map_get_tuple_id(__isl_keep isl_map
*map
,
566 enum isl_dim_type type
)
568 return map
? isl_space_get_tuple_id(map
->dim
, type
) : NULL
;
571 isl_bool
isl_set_has_tuple_id(__isl_keep isl_set
*set
)
573 return isl_map_has_tuple_id(set
, isl_dim_set
);
576 __isl_give isl_id
*isl_set_get_tuple_id(__isl_keep isl_set
*set
)
578 return isl_map_get_tuple_id(set
, isl_dim_set
);
581 /* Does the set tuple have a name?
583 isl_bool
isl_set_has_tuple_name(__isl_keep isl_set
*set
)
586 return isl_bool_error
;
587 return isl_space_has_tuple_name(set
->dim
, isl_dim_set
);
591 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set
*bset
)
593 return bset
? isl_space_get_tuple_name(bset
->dim
, isl_dim_set
) : NULL
;
596 const char *isl_set_get_tuple_name(__isl_keep isl_set
*set
)
598 return set
? isl_space_get_tuple_name(set
->dim
, isl_dim_set
) : NULL
;
601 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map
*bmap
,
602 enum isl_dim_type type
, unsigned pos
)
604 return bmap
? isl_space_get_dim_name(bmap
->dim
, type
, pos
) : NULL
;
607 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set
*bset
,
608 enum isl_dim_type type
, unsigned pos
)
610 return bset
? isl_space_get_dim_name(bset
->dim
, type
, pos
) : NULL
;
613 /* Does the given dimension have a name?
615 isl_bool
isl_map_has_dim_name(__isl_keep isl_map
*map
,
616 enum isl_dim_type type
, unsigned pos
)
619 return isl_bool_error
;
620 return isl_space_has_dim_name(map
->dim
, type
, pos
);
623 const char *isl_map_get_dim_name(__isl_keep isl_map
*map
,
624 enum isl_dim_type type
, unsigned pos
)
626 return map
? isl_space_get_dim_name(map
->dim
, type
, pos
) : NULL
;
629 const char *isl_set_get_dim_name(__isl_keep isl_set
*set
,
630 enum isl_dim_type type
, unsigned pos
)
632 return set
? isl_space_get_dim_name(set
->dim
, type
, pos
) : NULL
;
635 /* Does the given dimension have a name?
637 isl_bool
isl_set_has_dim_name(__isl_keep isl_set
*set
,
638 enum isl_dim_type type
, unsigned pos
)
641 return isl_bool_error
;
642 return isl_space_has_dim_name(set
->dim
, type
, pos
);
645 __isl_give isl_basic_map
*isl_basic_map_set_dim_name(
646 __isl_take isl_basic_map
*bmap
,
647 enum isl_dim_type type
, unsigned pos
, const char *s
)
649 bmap
= isl_basic_map_cow(bmap
);
652 bmap
->dim
= isl_space_set_dim_name(bmap
->dim
, type
, pos
, s
);
655 return isl_basic_map_finalize(bmap
);
657 isl_basic_map_free(bmap
);
661 __isl_give isl_map
*isl_map_set_dim_name(__isl_take isl_map
*map
,
662 enum isl_dim_type type
, unsigned pos
, const char *s
)
666 map
= isl_map_cow(map
);
670 map
->dim
= isl_space_set_dim_name(map
->dim
, type
, pos
, s
);
674 for (i
= 0; i
< map
->n
; ++i
) {
675 map
->p
[i
] = isl_basic_map_set_dim_name(map
->p
[i
], type
, pos
, s
);
686 __isl_give isl_basic_set
*isl_basic_set_set_dim_name(
687 __isl_take isl_basic_set
*bset
,
688 enum isl_dim_type type
, unsigned pos
, const char *s
)
690 return bset_from_bmap(isl_basic_map_set_dim_name(bset_to_bmap(bset
),
694 __isl_give isl_set
*isl_set_set_dim_name(__isl_take isl_set
*set
,
695 enum isl_dim_type type
, unsigned pos
, const char *s
)
697 return set_from_map(isl_map_set_dim_name(set_to_map(set
),
701 isl_bool
isl_basic_map_has_dim_id(__isl_keep isl_basic_map
*bmap
,
702 enum isl_dim_type type
, unsigned pos
)
705 return isl_bool_error
;
706 return isl_space_has_dim_id(bmap
->dim
, type
, pos
);
709 __isl_give isl_id
*isl_basic_set_get_dim_id(__isl_keep isl_basic_set
*bset
,
710 enum isl_dim_type type
, unsigned pos
)
712 return bset
? isl_space_get_dim_id(bset
->dim
, type
, pos
) : NULL
;
715 isl_bool
isl_map_has_dim_id(__isl_keep isl_map
*map
,
716 enum isl_dim_type type
, unsigned pos
)
718 return map
? isl_space_has_dim_id(map
->dim
, type
, pos
) : isl_bool_error
;
721 __isl_give isl_id
*isl_map_get_dim_id(__isl_keep isl_map
*map
,
722 enum isl_dim_type type
, unsigned pos
)
724 return map
? isl_space_get_dim_id(map
->dim
, type
, pos
) : NULL
;
727 isl_bool
isl_set_has_dim_id(__isl_keep isl_set
*set
,
728 enum isl_dim_type type
, unsigned pos
)
730 return isl_map_has_dim_id(set
, type
, pos
);
733 __isl_give isl_id
*isl_set_get_dim_id(__isl_keep isl_set
*set
,
734 enum isl_dim_type type
, unsigned pos
)
736 return isl_map_get_dim_id(set
, type
, pos
);
739 __isl_give isl_map
*isl_map_set_dim_id(__isl_take isl_map
*map
,
740 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
742 map
= isl_map_cow(map
);
746 map
->dim
= isl_space_set_dim_id(map
->dim
, type
, pos
, id
);
748 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
754 __isl_give isl_set
*isl_set_set_dim_id(__isl_take isl_set
*set
,
755 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
757 return isl_map_set_dim_id(set
, type
, pos
, id
);
760 int isl_map_find_dim_by_id(__isl_keep isl_map
*map
, enum isl_dim_type type
,
761 __isl_keep isl_id
*id
)
765 return isl_space_find_dim_by_id(map
->dim
, type
, id
);
768 int isl_set_find_dim_by_id(__isl_keep isl_set
*set
, enum isl_dim_type type
,
769 __isl_keep isl_id
*id
)
771 return isl_map_find_dim_by_id(set
, type
, id
);
774 /* Return the position of the dimension of the given type and name
776 * Return -1 if no such dimension can be found.
778 int isl_basic_map_find_dim_by_name(__isl_keep isl_basic_map
*bmap
,
779 enum isl_dim_type type
, const char *name
)
783 return isl_space_find_dim_by_name(bmap
->dim
, type
, name
);
786 int isl_map_find_dim_by_name(__isl_keep isl_map
*map
, enum isl_dim_type type
,
791 return isl_space_find_dim_by_name(map
->dim
, type
, name
);
794 int isl_set_find_dim_by_name(__isl_keep isl_set
*set
, enum isl_dim_type type
,
797 return isl_map_find_dim_by_name(set
, type
, name
);
800 /* Reset the user pointer on all identifiers of parameters and tuples
801 * of the space of "map".
803 __isl_give isl_map
*isl_map_reset_user(__isl_take isl_map
*map
)
807 space
= isl_map_get_space(map
);
808 space
= isl_space_reset_user(space
);
809 map
= isl_map_reset_space(map
, space
);
814 /* Reset the user pointer on all identifiers of parameters and tuples
815 * of the space of "set".
817 __isl_give isl_set
*isl_set_reset_user(__isl_take isl_set
*set
)
819 return isl_map_reset_user(set
);
822 int isl_basic_map_is_rational(__isl_keep isl_basic_map
*bmap
)
826 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
829 /* Has "map" been marked as a rational map?
830 * In particular, have all basic maps in "map" been marked this way?
831 * An empty map is not considered to be rational.
832 * Maps where only some of the basic maps are marked rational
835 isl_bool
isl_map_is_rational(__isl_keep isl_map
*map
)
841 return isl_bool_error
;
843 return isl_bool_false
;
844 rational
= isl_basic_map_is_rational(map
->p
[0]);
847 for (i
= 1; i
< map
->n
; ++i
) {
850 rational_i
= isl_basic_map_is_rational(map
->p
[i
]);
853 if (rational
!= rational_i
)
854 isl_die(isl_map_get_ctx(map
), isl_error_unsupported
,
855 "mixed rational and integer basic maps "
856 "not supported", return isl_bool_error
);
862 /* Has "set" been marked as a rational set?
863 * In particular, have all basic set in "set" been marked this way?
864 * An empty set is not considered to be rational.
865 * Sets where only some of the basic sets are marked rational
868 isl_bool
isl_set_is_rational(__isl_keep isl_set
*set
)
870 return isl_map_is_rational(set
);
873 int isl_basic_set_is_rational(__isl_keep isl_basic_set
*bset
)
875 return isl_basic_map_is_rational(bset
);
878 /* Does "bmap" contain any rational points?
880 * If "bmap" has an equality for each dimension, equating the dimension
881 * to an integer constant, then it has no rational points, even if it
882 * is marked as rational.
884 int isl_basic_map_has_rational(__isl_keep isl_basic_map
*bmap
)
886 int has_rational
= 1;
891 if (isl_basic_map_plain_is_empty(bmap
))
893 if (!isl_basic_map_is_rational(bmap
))
895 bmap
= isl_basic_map_copy(bmap
);
896 bmap
= isl_basic_map_implicit_equalities(bmap
);
899 total
= isl_basic_map_total_dim(bmap
);
900 if (bmap
->n_eq
== total
) {
902 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
903 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1, total
);
906 if (!isl_int_is_one(bmap
->eq
[i
][1 + j
]) &&
907 !isl_int_is_negone(bmap
->eq
[i
][1 + j
]))
909 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + j
+ 1,
917 isl_basic_map_free(bmap
);
922 /* Does "map" contain any rational points?
924 int isl_map_has_rational(__isl_keep isl_map
*map
)
931 for (i
= 0; i
< map
->n
; ++i
) {
932 has_rational
= isl_basic_map_has_rational(map
->p
[i
]);
933 if (has_rational
< 0)
941 /* Does "set" contain any rational points?
943 int isl_set_has_rational(__isl_keep isl_set
*set
)
945 return isl_map_has_rational(set
);
948 /* Is this basic set a parameter domain?
950 int isl_basic_set_is_params(__isl_keep isl_basic_set
*bset
)
954 return isl_space_is_params(bset
->dim
);
957 /* Is this set a parameter domain?
959 isl_bool
isl_set_is_params(__isl_keep isl_set
*set
)
962 return isl_bool_error
;
963 return isl_space_is_params(set
->dim
);
966 /* Is this map actually a parameter domain?
967 * Users should never call this function. Outside of isl,
968 * a map can never be a parameter domain.
970 int isl_map_is_params(__isl_keep isl_map
*map
)
974 return isl_space_is_params(map
->dim
);
977 static struct isl_basic_map
*basic_map_init(struct isl_ctx
*ctx
,
978 struct isl_basic_map
*bmap
, unsigned extra
,
979 unsigned n_eq
, unsigned n_ineq
)
982 size_t row_size
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + extra
;
987 bmap
->block
= isl_blk_alloc(ctx
, (n_ineq
+ n_eq
) * row_size
);
988 if (isl_blk_is_error(bmap
->block
))
991 bmap
->ineq
= isl_alloc_array(ctx
, isl_int
*, n_ineq
+ n_eq
);
992 if ((n_ineq
+ n_eq
) && !bmap
->ineq
)
996 bmap
->block2
= isl_blk_empty();
999 bmap
->block2
= isl_blk_alloc(ctx
, extra
* (1 + row_size
));
1000 if (isl_blk_is_error(bmap
->block2
))
1003 bmap
->div
= isl_alloc_array(ctx
, isl_int
*, extra
);
1008 for (i
= 0; i
< n_ineq
+ n_eq
; ++i
)
1009 bmap
->ineq
[i
] = bmap
->block
.data
+ i
* row_size
;
1011 for (i
= 0; i
< extra
; ++i
)
1012 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + row_size
);
1016 bmap
->c_size
= n_eq
+ n_ineq
;
1017 bmap
->eq
= bmap
->ineq
+ n_ineq
;
1018 bmap
->extra
= extra
;
1022 bmap
->sample
= NULL
;
1026 isl_basic_map_free(bmap
);
1030 struct isl_basic_set
*isl_basic_set_alloc(struct isl_ctx
*ctx
,
1031 unsigned nparam
, unsigned dim
, unsigned extra
,
1032 unsigned n_eq
, unsigned n_ineq
)
1034 struct isl_basic_map
*bmap
;
1037 space
= isl_space_set_alloc(ctx
, nparam
, dim
);
1041 bmap
= isl_basic_map_alloc_space(space
, extra
, n_eq
, n_ineq
);
1042 return bset_from_bmap(bmap
);
1045 struct isl_basic_set
*isl_basic_set_alloc_space(__isl_take isl_space
*dim
,
1046 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
1048 struct isl_basic_map
*bmap
;
1051 isl_assert(dim
->ctx
, dim
->n_in
== 0, goto error
);
1052 bmap
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
1053 return bset_from_bmap(bmap
);
1055 isl_space_free(dim
);
1059 struct isl_basic_map
*isl_basic_map_alloc_space(__isl_take isl_space
*dim
,
1060 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
1062 struct isl_basic_map
*bmap
;
1066 bmap
= isl_calloc_type(dim
->ctx
, struct isl_basic_map
);
1071 return basic_map_init(dim
->ctx
, bmap
, extra
, n_eq
, n_ineq
);
1073 isl_space_free(dim
);
1077 struct isl_basic_map
*isl_basic_map_alloc(struct isl_ctx
*ctx
,
1078 unsigned nparam
, unsigned in
, unsigned out
, unsigned extra
,
1079 unsigned n_eq
, unsigned n_ineq
)
1081 struct isl_basic_map
*bmap
;
1084 dim
= isl_space_alloc(ctx
, nparam
, in
, out
);
1088 bmap
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
1092 static void dup_constraints(
1093 struct isl_basic_map
*dst
, struct isl_basic_map
*src
)
1096 unsigned total
= isl_basic_map_total_dim(src
);
1098 for (i
= 0; i
< src
->n_eq
; ++i
) {
1099 int j
= isl_basic_map_alloc_equality(dst
);
1100 isl_seq_cpy(dst
->eq
[j
], src
->eq
[i
], 1+total
);
1103 for (i
= 0; i
< src
->n_ineq
; ++i
) {
1104 int j
= isl_basic_map_alloc_inequality(dst
);
1105 isl_seq_cpy(dst
->ineq
[j
], src
->ineq
[i
], 1+total
);
1108 for (i
= 0; i
< src
->n_div
; ++i
) {
1109 int j
= isl_basic_map_alloc_div(dst
);
1110 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
);
1112 ISL_F_SET(dst
, ISL_BASIC_SET_FINAL
);
1115 struct isl_basic_map
*isl_basic_map_dup(struct isl_basic_map
*bmap
)
1117 struct isl_basic_map
*dup
;
1121 dup
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
1122 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
1125 dup_constraints(dup
, bmap
);
1126 dup
->flags
= bmap
->flags
;
1127 dup
->sample
= isl_vec_copy(bmap
->sample
);
1131 struct isl_basic_set
*isl_basic_set_dup(struct isl_basic_set
*bset
)
1133 struct isl_basic_map
*dup
;
1135 dup
= isl_basic_map_dup(bset_to_bmap(bset
));
1136 return bset_from_bmap(dup
);
1139 struct isl_basic_set
*isl_basic_set_copy(struct isl_basic_set
*bset
)
1144 if (ISL_F_ISSET(bset
, ISL_BASIC_SET_FINAL
)) {
1148 return isl_basic_set_dup(bset
);
1151 struct isl_set
*isl_set_copy(struct isl_set
*set
)
1160 struct isl_basic_map
*isl_basic_map_copy(struct isl_basic_map
*bmap
)
1165 if (ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
)) {
1169 bmap
= isl_basic_map_dup(bmap
);
1171 ISL_F_SET(bmap
, ISL_BASIC_SET_FINAL
);
1175 struct isl_map
*isl_map_copy(struct isl_map
*map
)
1184 __isl_null isl_basic_map
*isl_basic_map_free(__isl_take isl_basic_map
*bmap
)
1189 if (--bmap
->ref
> 0)
1192 isl_ctx_deref(bmap
->ctx
);
1194 isl_blk_free(bmap
->ctx
, bmap
->block2
);
1196 isl_blk_free(bmap
->ctx
, bmap
->block
);
1197 isl_vec_free(bmap
->sample
);
1198 isl_space_free(bmap
->dim
);
1204 __isl_null isl_basic_set
*isl_basic_set_free(__isl_take isl_basic_set
*bset
)
1206 return isl_basic_map_free(bset_to_bmap(bset
));
1209 static int room_for_con(struct isl_basic_map
*bmap
, unsigned n
)
1211 return bmap
->n_eq
+ bmap
->n_ineq
+ n
<= bmap
->c_size
;
1214 __isl_give isl_map
*isl_map_align_params_map_map_and(
1215 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
,
1216 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*map1
,
1217 __isl_take isl_map
*map2
))
1221 if (isl_space_match(map1
->dim
, isl_dim_param
, map2
->dim
, isl_dim_param
))
1222 return fn(map1
, map2
);
1223 if (!isl_space_has_named_params(map1
->dim
) ||
1224 !isl_space_has_named_params(map2
->dim
))
1225 isl_die(map1
->ctx
, isl_error_invalid
,
1226 "unaligned unnamed parameters", goto error
);
1227 map1
= isl_map_align_params(map1
, isl_map_get_space(map2
));
1228 map2
= isl_map_align_params(map2
, isl_map_get_space(map1
));
1229 return fn(map1
, map2
);
1236 isl_bool
isl_map_align_params_map_map_and_test(__isl_keep isl_map
*map1
,
1237 __isl_keep isl_map
*map2
,
1238 isl_bool (*fn
)(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
))
1243 return isl_bool_error
;
1244 if (isl_space_match(map1
->dim
, isl_dim_param
, map2
->dim
, isl_dim_param
))
1245 return fn(map1
, map2
);
1246 if (!isl_space_has_named_params(map1
->dim
) ||
1247 !isl_space_has_named_params(map2
->dim
))
1248 isl_die(map1
->ctx
, isl_error_invalid
,
1249 "unaligned unnamed parameters", return isl_bool_error
);
1250 map1
= isl_map_copy(map1
);
1251 map2
= isl_map_copy(map2
);
1252 map1
= isl_map_align_params(map1
, isl_map_get_space(map2
));
1253 map2
= isl_map_align_params(map2
, isl_map_get_space(map1
));
1260 int isl_basic_map_alloc_equality(struct isl_basic_map
*bmap
)
1262 struct isl_ctx
*ctx
;
1266 isl_assert(ctx
, room_for_con(bmap
, 1), return -1);
1267 isl_assert(ctx
, (bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
<= bmap
->c_size
,
1269 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1270 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1271 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1272 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1273 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1274 if ((bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
== bmap
->c_size
) {
1276 int j
= isl_basic_map_alloc_inequality(bmap
);
1280 bmap
->ineq
[j
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1281 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
1288 isl_seq_clr(bmap
->eq
[bmap
->n_eq
] + 1 + isl_basic_map_total_dim(bmap
),
1289 bmap
->extra
- bmap
->n_div
);
1290 return bmap
->n_eq
++;
1293 int isl_basic_set_alloc_equality(struct isl_basic_set
*bset
)
1295 return isl_basic_map_alloc_equality(bset_to_bmap(bset
));
1298 int isl_basic_map_free_equality(struct isl_basic_map
*bmap
, unsigned n
)
1302 isl_assert(bmap
->ctx
, n
<= bmap
->n_eq
, return -1);
1307 int isl_basic_set_free_equality(struct isl_basic_set
*bset
, unsigned n
)
1309 return isl_basic_map_free_equality(bset_to_bmap(bset
), n
);
1312 int isl_basic_map_drop_equality(struct isl_basic_map
*bmap
, unsigned pos
)
1317 isl_assert(bmap
->ctx
, pos
< bmap
->n_eq
, return -1);
1319 if (pos
!= bmap
->n_eq
- 1) {
1321 bmap
->eq
[pos
] = bmap
->eq
[bmap
->n_eq
- 1];
1322 bmap
->eq
[bmap
->n_eq
- 1] = t
;
1328 int isl_basic_set_drop_equality(struct isl_basic_set
*bset
, unsigned pos
)
1330 return isl_basic_map_drop_equality(bset_to_bmap(bset
), pos
);
1333 /* Turn inequality "pos" of "bmap" into an equality.
1335 * In particular, we move the inequality in front of the equalities
1336 * and move the last inequality in the position of the moved inequality.
1337 * Note that isl_tab_make_equalities_explicit depends on this particular
1338 * change in the ordering of the constraints.
1340 void isl_basic_map_inequality_to_equality(
1341 struct isl_basic_map
*bmap
, unsigned pos
)
1345 t
= bmap
->ineq
[pos
];
1346 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1347 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
1352 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1353 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1354 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1355 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1358 static int room_for_ineq(struct isl_basic_map
*bmap
, unsigned n
)
1360 return bmap
->n_ineq
+ n
<= bmap
->eq
- bmap
->ineq
;
1363 int isl_basic_map_alloc_inequality(struct isl_basic_map
*bmap
)
1365 struct isl_ctx
*ctx
;
1369 isl_assert(ctx
, room_for_ineq(bmap
, 1), return -1);
1370 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1371 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1372 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1373 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1374 isl_seq_clr(bmap
->ineq
[bmap
->n_ineq
] +
1375 1 + isl_basic_map_total_dim(bmap
),
1376 bmap
->extra
- bmap
->n_div
);
1377 return bmap
->n_ineq
++;
1380 int isl_basic_set_alloc_inequality(struct isl_basic_set
*bset
)
1382 return isl_basic_map_alloc_inequality(bset_to_bmap(bset
));
1385 int isl_basic_map_free_inequality(struct isl_basic_map
*bmap
, unsigned n
)
1389 isl_assert(bmap
->ctx
, n
<= bmap
->n_ineq
, return -1);
1394 int isl_basic_set_free_inequality(struct isl_basic_set
*bset
, unsigned n
)
1396 return isl_basic_map_free_inequality(bset_to_bmap(bset
), n
);
1399 int isl_basic_map_drop_inequality(struct isl_basic_map
*bmap
, unsigned pos
)
1404 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
1406 if (pos
!= bmap
->n_ineq
- 1) {
1407 t
= bmap
->ineq
[pos
];
1408 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1409 bmap
->ineq
[bmap
->n_ineq
- 1] = t
;
1410 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1416 int isl_basic_set_drop_inequality(struct isl_basic_set
*bset
, unsigned pos
)
1418 return isl_basic_map_drop_inequality(bset_to_bmap(bset
), pos
);
1421 __isl_give isl_basic_map
*isl_basic_map_add_eq(__isl_take isl_basic_map
*bmap
,
1426 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
1429 k
= isl_basic_map_alloc_equality(bmap
);
1432 isl_seq_cpy(bmap
->eq
[k
], eq
, 1 + isl_basic_map_total_dim(bmap
));
1435 isl_basic_map_free(bmap
);
1439 __isl_give isl_basic_set
*isl_basic_set_add_eq(__isl_take isl_basic_set
*bset
,
1442 return bset_from_bmap(isl_basic_map_add_eq(bset_to_bmap(bset
), eq
));
1445 __isl_give isl_basic_map
*isl_basic_map_add_ineq(__isl_take isl_basic_map
*bmap
,
1450 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
1453 k
= isl_basic_map_alloc_inequality(bmap
);
1456 isl_seq_cpy(bmap
->ineq
[k
], ineq
, 1 + isl_basic_map_total_dim(bmap
));
1459 isl_basic_map_free(bmap
);
1463 __isl_give isl_basic_set
*isl_basic_set_add_ineq(__isl_take isl_basic_set
*bset
,
1466 return bset_from_bmap(isl_basic_map_add_ineq(bset_to_bmap(bset
), ineq
));
1469 int isl_basic_map_alloc_div(struct isl_basic_map
*bmap
)
1473 isl_assert(bmap
->ctx
, bmap
->n_div
< bmap
->extra
, return -1);
1474 isl_seq_clr(bmap
->div
[bmap
->n_div
] +
1475 1 + 1 + isl_basic_map_total_dim(bmap
),
1476 bmap
->extra
- bmap
->n_div
);
1477 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1478 return bmap
->n_div
++;
1481 int isl_basic_set_alloc_div(struct isl_basic_set
*bset
)
1483 return isl_basic_map_alloc_div(bset_to_bmap(bset
));
1486 /* Check that there are "n" dimensions of type "type" starting at "first"
1489 static isl_stat
isl_basic_map_check_range(__isl_keep isl_basic_map
*bmap
,
1490 enum isl_dim_type type
, unsigned first
, unsigned n
)
1495 return isl_stat_error
;
1496 dim
= isl_basic_map_dim(bmap
, type
);
1497 if (first
+ n
> dim
|| first
+ n
< first
)
1498 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
1499 "position or range out of bounds",
1500 return isl_stat_error
);
1504 /* Insert an extra integer division, prescribed by "div", to "bmap"
1505 * at (integer division) position "pos".
1507 * The integer division is first added at the end and then moved
1508 * into the right position.
1510 __isl_give isl_basic_map
*isl_basic_map_insert_div(
1511 __isl_take isl_basic_map
*bmap
, int pos
, __isl_keep isl_vec
*div
)
1515 bmap
= isl_basic_map_cow(bmap
);
1517 return isl_basic_map_free(bmap
);
1519 if (div
->size
!= 1 + 1 + isl_basic_map_dim(bmap
, isl_dim_all
))
1520 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
1521 "unexpected size", return isl_basic_map_free(bmap
));
1522 if (isl_basic_map_check_range(bmap
, isl_dim_div
, pos
, 0) < 0)
1523 return isl_basic_map_free(bmap
);
1525 bmap
= isl_basic_map_extend_space(bmap
,
1526 isl_basic_map_get_space(bmap
), 1, 0, 2);
1527 k
= isl_basic_map_alloc_div(bmap
);
1529 return isl_basic_map_free(bmap
);
1530 isl_seq_cpy(bmap
->div
[k
], div
->el
, div
->size
);
1531 isl_int_set_si(bmap
->div
[k
][div
->size
], 0);
1533 for (i
= k
; i
> pos
; --i
)
1534 isl_basic_map_swap_div(bmap
, i
, i
- 1);
1539 int isl_basic_map_free_div(struct isl_basic_map
*bmap
, unsigned n
)
1543 isl_assert(bmap
->ctx
, n
<= bmap
->n_div
, return -1);
1548 int isl_basic_set_free_div(struct isl_basic_set
*bset
, unsigned n
)
1550 return isl_basic_map_free_div(bset_to_bmap(bset
), n
);
1553 /* Copy constraint from src to dst, putting the vars of src at offset
1554 * dim_off in dst and the divs of src at offset div_off in dst.
1555 * If both sets are actually map, then dim_off applies to the input
1558 static void copy_constraint(struct isl_basic_map
*dst_map
, isl_int
*dst
,
1559 struct isl_basic_map
*src_map
, isl_int
*src
,
1560 unsigned in_off
, unsigned out_off
, unsigned div_off
)
1562 unsigned src_nparam
= isl_basic_map_n_param(src_map
);
1563 unsigned dst_nparam
= isl_basic_map_n_param(dst_map
);
1564 unsigned src_in
= isl_basic_map_n_in(src_map
);
1565 unsigned dst_in
= isl_basic_map_n_in(dst_map
);
1566 unsigned src_out
= isl_basic_map_n_out(src_map
);
1567 unsigned dst_out
= isl_basic_map_n_out(dst_map
);
1568 isl_int_set(dst
[0], src
[0]);
1569 isl_seq_cpy(dst
+1, src
+1, isl_min(dst_nparam
, src_nparam
));
1570 if (dst_nparam
> src_nparam
)
1571 isl_seq_clr(dst
+1+src_nparam
,
1572 dst_nparam
- src_nparam
);
1573 isl_seq_clr(dst
+1+dst_nparam
, in_off
);
1574 isl_seq_cpy(dst
+1+dst_nparam
+in_off
,
1576 isl_min(dst_in
-in_off
, src_in
));
1577 if (dst_in
-in_off
> src_in
)
1578 isl_seq_clr(dst
+1+dst_nparam
+in_off
+src_in
,
1579 dst_in
- in_off
- src_in
);
1580 isl_seq_clr(dst
+1+dst_nparam
+dst_in
, out_off
);
1581 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+out_off
,
1582 src
+1+src_nparam
+src_in
,
1583 isl_min(dst_out
-out_off
, src_out
));
1584 if (dst_out
-out_off
> src_out
)
1585 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+out_off
+src_out
,
1586 dst_out
- out_off
- src_out
);
1587 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
, div_off
);
1588 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+dst_out
+div_off
,
1589 src
+1+src_nparam
+src_in
+src_out
,
1590 isl_min(dst_map
->extra
-div_off
, src_map
->n_div
));
1591 if (dst_map
->n_div
-div_off
> src_map
->n_div
)
1592 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
+
1593 div_off
+src_map
->n_div
,
1594 dst_map
->n_div
- div_off
- src_map
->n_div
);
1597 static void copy_div(struct isl_basic_map
*dst_map
, isl_int
*dst
,
1598 struct isl_basic_map
*src_map
, isl_int
*src
,
1599 unsigned in_off
, unsigned out_off
, unsigned div_off
)
1601 isl_int_set(dst
[0], src
[0]);
1602 copy_constraint(dst_map
, dst
+1, src_map
, src
+1, in_off
, out_off
, div_off
);
1605 static struct isl_basic_map
*add_constraints(struct isl_basic_map
*bmap1
,
1606 struct isl_basic_map
*bmap2
, unsigned i_pos
, unsigned o_pos
)
1611 if (!bmap1
|| !bmap2
)
1614 div_off
= bmap1
->n_div
;
1616 for (i
= 0; i
< bmap2
->n_eq
; ++i
) {
1617 int i1
= isl_basic_map_alloc_equality(bmap1
);
1620 copy_constraint(bmap1
, bmap1
->eq
[i1
], bmap2
, bmap2
->eq
[i
],
1621 i_pos
, o_pos
, div_off
);
1624 for (i
= 0; i
< bmap2
->n_ineq
; ++i
) {
1625 int i1
= isl_basic_map_alloc_inequality(bmap1
);
1628 copy_constraint(bmap1
, bmap1
->ineq
[i1
], bmap2
, bmap2
->ineq
[i
],
1629 i_pos
, o_pos
, div_off
);
1632 for (i
= 0; i
< bmap2
->n_div
; ++i
) {
1633 int i1
= isl_basic_map_alloc_div(bmap1
);
1636 copy_div(bmap1
, bmap1
->div
[i1
], bmap2
, bmap2
->div
[i
],
1637 i_pos
, o_pos
, div_off
);
1640 isl_basic_map_free(bmap2
);
1645 isl_basic_map_free(bmap1
);
1646 isl_basic_map_free(bmap2
);
1650 struct isl_basic_set
*isl_basic_set_add_constraints(struct isl_basic_set
*bset1
,
1651 struct isl_basic_set
*bset2
, unsigned pos
)
1653 return bset_from_bmap(add_constraints(bset_to_bmap(bset1
),
1654 bset_to_bmap(bset2
), 0, pos
));
1657 struct isl_basic_map
*isl_basic_map_extend_space(struct isl_basic_map
*base
,
1658 __isl_take isl_space
*dim
, unsigned extra
,
1659 unsigned n_eq
, unsigned n_ineq
)
1661 struct isl_basic_map
*ext
;
1671 dims_ok
= isl_space_is_equal(base
->dim
, dim
) &&
1672 base
->extra
>= base
->n_div
+ extra
;
1674 if (dims_ok
&& room_for_con(base
, n_eq
+ n_ineq
) &&
1675 room_for_ineq(base
, n_ineq
)) {
1676 isl_space_free(dim
);
1680 isl_assert(base
->ctx
, base
->dim
->nparam
<= dim
->nparam
, goto error
);
1681 isl_assert(base
->ctx
, base
->dim
->n_in
<= dim
->n_in
, goto error
);
1682 isl_assert(base
->ctx
, base
->dim
->n_out
<= dim
->n_out
, goto error
);
1683 extra
+= base
->extra
;
1685 n_ineq
+= base
->n_ineq
;
1687 ext
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
1693 ext
->sample
= isl_vec_copy(base
->sample
);
1694 flags
= base
->flags
;
1695 ext
= add_constraints(ext
, base
, 0, 0);
1698 ISL_F_CLR(ext
, ISL_BASIC_SET_FINAL
);
1704 isl_space_free(dim
);
1705 isl_basic_map_free(base
);
1709 struct isl_basic_set
*isl_basic_set_extend_space(struct isl_basic_set
*base
,
1710 __isl_take isl_space
*dim
, unsigned extra
,
1711 unsigned n_eq
, unsigned n_ineq
)
1713 return bset_from_bmap(isl_basic_map_extend_space(bset_to_bmap(base
),
1714 dim
, extra
, n_eq
, n_ineq
));
1717 struct isl_basic_map
*isl_basic_map_extend_constraints(
1718 struct isl_basic_map
*base
, unsigned n_eq
, unsigned n_ineq
)
1722 return isl_basic_map_extend_space(base
, isl_space_copy(base
->dim
),
1726 struct isl_basic_map
*isl_basic_map_extend(struct isl_basic_map
*base
,
1727 unsigned nparam
, unsigned n_in
, unsigned n_out
, unsigned extra
,
1728 unsigned n_eq
, unsigned n_ineq
)
1730 struct isl_basic_map
*bmap
;
1735 dim
= isl_space_alloc(base
->ctx
, nparam
, n_in
, n_out
);
1739 bmap
= isl_basic_map_extend_space(base
, dim
, extra
, n_eq
, n_ineq
);
1742 isl_basic_map_free(base
);
1746 struct isl_basic_set
*isl_basic_set_extend(struct isl_basic_set
*base
,
1747 unsigned nparam
, unsigned dim
, unsigned extra
,
1748 unsigned n_eq
, unsigned n_ineq
)
1750 return bset_from_bmap(isl_basic_map_extend(bset_to_bmap(base
),
1751 nparam
, 0, dim
, extra
, n_eq
, n_ineq
));
1754 struct isl_basic_set
*isl_basic_set_extend_constraints(
1755 struct isl_basic_set
*base
, unsigned n_eq
, unsigned n_ineq
)
1757 isl_basic_map
*bmap
= bset_to_bmap(base
);
1758 bmap
= isl_basic_map_extend_constraints(bmap
, n_eq
, n_ineq
);
1759 return bset_from_bmap(bmap
);
1762 struct isl_basic_set
*isl_basic_set_cow(struct isl_basic_set
*bset
)
1764 return bset_from_bmap(isl_basic_map_cow(bset_to_bmap(bset
)));
1767 struct isl_basic_map
*isl_basic_map_cow(struct isl_basic_map
*bmap
)
1772 if (bmap
->ref
> 1) {
1774 bmap
= isl_basic_map_dup(bmap
);
1777 ISL_F_CLR(bmap
, ISL_BASIC_SET_FINAL
);
1778 ISL_F_CLR(bmap
, ISL_BASIC_MAP_REDUCED_COEFFICIENTS
);
1783 /* Clear all cached information in "map", either because it is about
1784 * to be modified or because it is being freed.
1785 * Always return the same pointer that is passed in.
1786 * This is needed for the use in isl_map_free.
1788 static __isl_give isl_map
*clear_caches(__isl_take isl_map
*map
)
1790 isl_basic_map_free(map
->cached_simple_hull
[0]);
1791 isl_basic_map_free(map
->cached_simple_hull
[1]);
1792 map
->cached_simple_hull
[0] = NULL
;
1793 map
->cached_simple_hull
[1] = NULL
;
1797 struct isl_set
*isl_set_cow(struct isl_set
*set
)
1799 return isl_map_cow(set
);
1802 /* Return an isl_map that is equal to "map" and that has only
1803 * a single reference.
1805 * If the original input already has only one reference, then
1806 * simply return it, but clear all cached information, since
1807 * it may be rendered invalid by the operations that will be
1808 * performed on the result.
1810 * Otherwise, create a duplicate (without any cached information).
1812 struct isl_map
*isl_map_cow(struct isl_map
*map
)
1818 return clear_caches(map
);
1820 return isl_map_dup(map
);
1823 static void swap_vars(struct isl_blk blk
, isl_int
*a
,
1824 unsigned a_len
, unsigned b_len
)
1826 isl_seq_cpy(blk
.data
, a
+a_len
, b_len
);
1827 isl_seq_cpy(blk
.data
+b_len
, a
, a_len
);
1828 isl_seq_cpy(a
, blk
.data
, b_len
+a_len
);
1831 static __isl_give isl_basic_map
*isl_basic_map_swap_vars(
1832 __isl_take isl_basic_map
*bmap
, unsigned pos
, unsigned n1
, unsigned n2
)
1840 isl_assert(bmap
->ctx
,
1841 pos
+ n1
+ n2
<= 1 + isl_basic_map_total_dim(bmap
), goto error
);
1843 if (n1
== 0 || n2
== 0)
1846 bmap
= isl_basic_map_cow(bmap
);
1850 blk
= isl_blk_alloc(bmap
->ctx
, n1
+ n2
);
1851 if (isl_blk_is_error(blk
))
1854 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1856 bmap
->eq
[i
] + pos
, n1
, n2
);
1858 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1860 bmap
->ineq
[i
] + pos
, n1
, n2
);
1862 for (i
= 0; i
< bmap
->n_div
; ++i
)
1864 bmap
->div
[i
]+1 + pos
, n1
, n2
);
1866 isl_blk_free(bmap
->ctx
, blk
);
1868 ISL_F_CLR(bmap
, ISL_BASIC_SET_NORMALIZED
);
1869 bmap
= isl_basic_map_gauss(bmap
, NULL
);
1870 return isl_basic_map_finalize(bmap
);
1872 isl_basic_map_free(bmap
);
1876 struct isl_basic_map
*isl_basic_map_set_to_empty(struct isl_basic_map
*bmap
)
1882 total
= isl_basic_map_total_dim(bmap
);
1883 isl_basic_map_free_div(bmap
, bmap
->n_div
);
1884 isl_basic_map_free_inequality(bmap
, bmap
->n_ineq
);
1886 isl_basic_map_free_equality(bmap
, bmap
->n_eq
-1);
1888 i
= isl_basic_map_alloc_equality(bmap
);
1892 isl_int_set_si(bmap
->eq
[i
][0], 1);
1893 isl_seq_clr(bmap
->eq
[i
]+1, total
);
1894 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
1895 isl_vec_free(bmap
->sample
);
1896 bmap
->sample
= NULL
;
1897 return isl_basic_map_finalize(bmap
);
1899 isl_basic_map_free(bmap
);
1903 struct isl_basic_set
*isl_basic_set_set_to_empty(struct isl_basic_set
*bset
)
1905 return bset_from_bmap(isl_basic_map_set_to_empty(bset_to_bmap(bset
)));
1908 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
1911 static void swap_div(__isl_keep isl_basic_map
*bmap
, int a
, int b
)
1913 isl_int
*t
= bmap
->div
[a
];
1914 bmap
->div
[a
] = bmap
->div
[b
];
1918 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
1919 * div definitions accordingly.
1921 void isl_basic_map_swap_div(struct isl_basic_map
*bmap
, int a
, int b
)
1924 unsigned off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
1926 swap_div(bmap
, a
, b
);
1928 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1929 isl_int_swap(bmap
->eq
[i
][1+off
+a
], bmap
->eq
[i
][1+off
+b
]);
1931 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1932 isl_int_swap(bmap
->ineq
[i
][1+off
+a
], bmap
->ineq
[i
][1+off
+b
]);
1934 for (i
= 0; i
< bmap
->n_div
; ++i
)
1935 isl_int_swap(bmap
->div
[i
][1+1+off
+a
], bmap
->div
[i
][1+1+off
+b
]);
1936 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1939 /* Swap divs "a" and "b" in "bset" and adjust the constraints and
1940 * div definitions accordingly.
1942 void isl_basic_set_swap_div(__isl_keep isl_basic_set
*bset
, int a
, int b
)
1944 isl_basic_map_swap_div(bset
, a
, b
);
1947 /* Eliminate the specified n dimensions starting at first from the
1948 * constraints, without removing the dimensions from the space.
1949 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1951 __isl_give isl_map
*isl_map_eliminate(__isl_take isl_map
*map
,
1952 enum isl_dim_type type
, unsigned first
, unsigned n
)
1961 if (first
+ n
> isl_map_dim(map
, type
) || first
+ n
< first
)
1962 isl_die(map
->ctx
, isl_error_invalid
,
1963 "index out of bounds", goto error
);
1965 map
= isl_map_cow(map
);
1969 for (i
= 0; i
< map
->n
; ++i
) {
1970 map
->p
[i
] = isl_basic_map_eliminate(map
->p
[i
], type
, first
, n
);
1980 /* Eliminate the specified n dimensions starting at first from the
1981 * constraints, without removing the dimensions from the space.
1982 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1984 __isl_give isl_set
*isl_set_eliminate(__isl_take isl_set
*set
,
1985 enum isl_dim_type type
, unsigned first
, unsigned n
)
1987 return set_from_map(isl_map_eliminate(set_to_map(set
), type
, first
, n
));
1990 /* Eliminate the specified n dimensions starting at first from the
1991 * constraints, without removing the dimensions from the space.
1992 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1994 __isl_give isl_set
*isl_set_eliminate_dims(__isl_take isl_set
*set
,
1995 unsigned first
, unsigned n
)
1997 return isl_set_eliminate(set
, isl_dim_set
, first
, n
);
2000 __isl_give isl_basic_map
*isl_basic_map_remove_divs(
2001 __isl_take isl_basic_map
*bmap
)
2005 bmap
= isl_basic_map_eliminate_vars(bmap
,
2006 isl_space_dim(bmap
->dim
, isl_dim_all
), bmap
->n_div
);
2010 return isl_basic_map_finalize(bmap
);
2013 __isl_give isl_basic_set
*isl_basic_set_remove_divs(
2014 __isl_take isl_basic_set
*bset
)
2016 return bset_from_bmap(isl_basic_map_remove_divs(bset_to_bmap(bset
)));
2019 __isl_give isl_map
*isl_map_remove_divs(__isl_take isl_map
*map
)
2028 map
= isl_map_cow(map
);
2032 for (i
= 0; i
< map
->n
; ++i
) {
2033 map
->p
[i
] = isl_basic_map_remove_divs(map
->p
[i
]);
2043 __isl_give isl_set
*isl_set_remove_divs(__isl_take isl_set
*set
)
2045 return isl_map_remove_divs(set
);
2048 struct isl_basic_map
*isl_basic_map_remove_dims(struct isl_basic_map
*bmap
,
2049 enum isl_dim_type type
, unsigned first
, unsigned n
)
2051 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2052 return isl_basic_map_free(bmap
);
2053 if (n
== 0 && !isl_space_is_named_or_nested(bmap
->dim
, type
))
2055 bmap
= isl_basic_map_eliminate_vars(bmap
,
2056 isl_basic_map_offset(bmap
, type
) - 1 + first
, n
);
2059 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
) && type
== isl_dim_div
)
2061 bmap
= isl_basic_map_drop(bmap
, type
, first
, n
);
2065 /* Return true if the definition of the given div (recursively) involves
2066 * any of the given variables.
2068 static int div_involves_vars(__isl_keep isl_basic_map
*bmap
, int div
,
2069 unsigned first
, unsigned n
)
2072 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
2074 if (isl_int_is_zero(bmap
->div
[div
][0]))
2076 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + first
, n
) >= 0)
2079 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2080 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
2082 if (div_involves_vars(bmap
, i
, first
, n
))
2089 /* Try and add a lower and/or upper bound on "div" to "bmap"
2090 * based on inequality "i".
2091 * "total" is the total number of variables (excluding the divs).
2092 * "v" is a temporary object that can be used during the calculations.
2093 * If "lb" is set, then a lower bound should be constructed.
2094 * If "ub" is set, then an upper bound should be constructed.
2096 * The calling function has already checked that the inequality does not
2097 * reference "div", but we still need to check that the inequality is
2098 * of the right form. We'll consider the case where we want to construct
2099 * a lower bound. The construction of upper bounds is similar.
2101 * Let "div" be of the form
2103 * q = floor((a + f(x))/d)
2105 * We essentially check if constraint "i" is of the form
2109 * so that we can use it to derive a lower bound on "div".
2110 * However, we allow a slightly more general form
2114 * with the condition that the coefficients of g(x) - f(x) are all
2116 * Rewriting this constraint as
2120 * adding a + f(x) to both sides and dividing by d, we obtain
2122 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
2124 * Taking the floor on both sides, we obtain
2126 * q >= floor((a-b)/d) + (f(x)-g(x))/d
2130 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
2132 * In the case of an upper bound, we construct the constraint
2134 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
2137 static __isl_give isl_basic_map
*insert_bounds_on_div_from_ineq(
2138 __isl_take isl_basic_map
*bmap
, int div
, int i
,
2139 unsigned total
, isl_int v
, int lb
, int ub
)
2143 for (j
= 0; (lb
|| ub
) && j
< total
+ bmap
->n_div
; ++j
) {
2145 isl_int_sub(v
, bmap
->ineq
[i
][1 + j
],
2146 bmap
->div
[div
][1 + 1 + j
]);
2147 lb
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
2150 isl_int_add(v
, bmap
->ineq
[i
][1 + j
],
2151 bmap
->div
[div
][1 + 1 + j
]);
2152 ub
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
2158 bmap
= isl_basic_map_cow(bmap
);
2159 bmap
= isl_basic_map_extend_constraints(bmap
, 0, lb
+ ub
);
2161 int k
= isl_basic_map_alloc_inequality(bmap
);
2164 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
2165 isl_int_sub(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
2166 bmap
->div
[div
][1 + j
]);
2167 isl_int_cdiv_q(bmap
->ineq
[k
][j
],
2168 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
2170 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], 1);
2173 int k
= isl_basic_map_alloc_inequality(bmap
);
2176 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
2177 isl_int_add(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
2178 bmap
->div
[div
][1 + j
]);
2179 isl_int_fdiv_q(bmap
->ineq
[k
][j
],
2180 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
2182 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], -1);
2185 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
2188 isl_basic_map_free(bmap
);
2192 /* This function is called right before "div" is eliminated from "bmap"
2193 * using Fourier-Motzkin.
2194 * Look through the constraints of "bmap" for constraints on the argument
2195 * of the integer division and use them to construct constraints on the
2196 * integer division itself. These constraints can then be combined
2197 * during the Fourier-Motzkin elimination.
2198 * Note that it is only useful to introduce lower bounds on "div"
2199 * if "bmap" already contains upper bounds on "div" as the newly
2200 * introduce lower bounds can then be combined with the pre-existing
2201 * upper bounds. Similarly for upper bounds.
2202 * We therefore first check if "bmap" contains any lower and/or upper bounds
2205 * It is interesting to note that the introduction of these constraints
2206 * can indeed lead to more accurate results, even when compared to
2207 * deriving constraints on the argument of "div" from constraints on "div".
2208 * Consider, for example, the set
2210 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2212 * The second constraint can be rewritten as
2214 * 2 * [(-i-2j+3)/4] + k >= 0
2216 * from which we can derive
2218 * -i - 2j + 3 >= -2k
2224 * Combined with the first constraint, we obtain
2226 * -3 <= 3 + 2k or k >= -3
2228 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2229 * the first constraint, we obtain
2231 * [(i + 2j)/4] >= [-3/4] = -1
2233 * Combining this constraint with the second constraint, we obtain
2237 static __isl_give isl_basic_map
*insert_bounds_on_div(
2238 __isl_take isl_basic_map
*bmap
, int div
)
2241 int check_lb
, check_ub
;
2248 if (isl_int_is_zero(bmap
->div
[div
][0]))
2251 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
2255 for (i
= 0; (!check_lb
|| !check_ub
) && i
< bmap
->n_ineq
; ++i
) {
2256 int s
= isl_int_sgn(bmap
->ineq
[i
][1 + total
+ div
]);
2263 if (!check_lb
&& !check_ub
)
2268 for (i
= 0; bmap
&& i
< bmap
->n_ineq
; ++i
) {
2269 if (!isl_int_is_zero(bmap
->ineq
[i
][1 + total
+ div
]))
2272 bmap
= insert_bounds_on_div_from_ineq(bmap
, div
, i
, total
, v
,
2273 check_lb
, check_ub
);
2281 /* Remove all divs (recursively) involving any of the given dimensions
2282 * in their definitions.
2284 __isl_give isl_basic_map
*isl_basic_map_remove_divs_involving_dims(
2285 __isl_take isl_basic_map
*bmap
,
2286 enum isl_dim_type type
, unsigned first
, unsigned n
)
2290 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2291 return isl_basic_map_free(bmap
);
2292 first
+= isl_basic_map_offset(bmap
, type
);
2294 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2295 if (!div_involves_vars(bmap
, i
, first
, n
))
2297 bmap
= insert_bounds_on_div(bmap
, i
);
2298 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
2307 __isl_give isl_basic_set
*isl_basic_set_remove_divs_involving_dims(
2308 __isl_take isl_basic_set
*bset
,
2309 enum isl_dim_type type
, unsigned first
, unsigned n
)
2311 return isl_basic_map_remove_divs_involving_dims(bset
, type
, first
, n
);
2314 __isl_give isl_map
*isl_map_remove_divs_involving_dims(__isl_take isl_map
*map
,
2315 enum isl_dim_type type
, unsigned first
, unsigned n
)
2324 map
= isl_map_cow(map
);
2328 for (i
= 0; i
< map
->n
; ++i
) {
2329 map
->p
[i
] = isl_basic_map_remove_divs_involving_dims(map
->p
[i
],
2340 __isl_give isl_set
*isl_set_remove_divs_involving_dims(__isl_take isl_set
*set
,
2341 enum isl_dim_type type
, unsigned first
, unsigned n
)
2343 return set_from_map(isl_map_remove_divs_involving_dims(set_to_map(set
),
2347 /* Does the description of "bmap" depend on the specified dimensions?
2348 * We also check whether the dimensions appear in any of the div definitions.
2349 * In principle there is no need for this check. If the dimensions appear
2350 * in a div definition, they also appear in the defining constraints of that
2353 isl_bool
isl_basic_map_involves_dims(__isl_keep isl_basic_map
*bmap
,
2354 enum isl_dim_type type
, unsigned first
, unsigned n
)
2358 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2359 return isl_bool_error
;
2361 first
+= isl_basic_map_offset(bmap
, type
);
2362 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2363 if (isl_seq_first_non_zero(bmap
->eq
[i
] + first
, n
) >= 0)
2364 return isl_bool_true
;
2365 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2366 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + first
, n
) >= 0)
2367 return isl_bool_true
;
2368 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2369 if (isl_int_is_zero(bmap
->div
[i
][0]))
2371 if (isl_seq_first_non_zero(bmap
->div
[i
] + 1 + first
, n
) >= 0)
2372 return isl_bool_true
;
2375 return isl_bool_false
;
2378 isl_bool
isl_map_involves_dims(__isl_keep isl_map
*map
,
2379 enum isl_dim_type type
, unsigned first
, unsigned n
)
2384 return isl_bool_error
;
2386 if (first
+ n
> isl_map_dim(map
, type
))
2387 isl_die(map
->ctx
, isl_error_invalid
,
2388 "index out of bounds", return isl_bool_error
);
2390 for (i
= 0; i
< map
->n
; ++i
) {
2391 isl_bool involves
= isl_basic_map_involves_dims(map
->p
[i
],
2393 if (involves
< 0 || involves
)
2397 return isl_bool_false
;
2400 isl_bool
isl_basic_set_involves_dims(__isl_keep isl_basic_set
*bset
,
2401 enum isl_dim_type type
, unsigned first
, unsigned n
)
2403 return isl_basic_map_involves_dims(bset
, type
, first
, n
);
2406 isl_bool
isl_set_involves_dims(__isl_keep isl_set
*set
,
2407 enum isl_dim_type type
, unsigned first
, unsigned n
)
2409 return isl_map_involves_dims(set
, type
, first
, n
);
2412 /* Drop all constraints in bmap that involve any of the dimensions
2413 * first to first+n-1.
2415 static __isl_give isl_basic_map
*isl_basic_map_drop_constraints_involving(
2416 __isl_take isl_basic_map
*bmap
, unsigned first
, unsigned n
)
2423 bmap
= isl_basic_map_cow(bmap
);
2428 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
) {
2429 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + first
, n
) == -1)
2431 isl_basic_map_drop_equality(bmap
, i
);
2434 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
2435 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + 1 + first
, n
) == -1)
2437 isl_basic_map_drop_inequality(bmap
, i
);
2440 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
2444 /* Drop all constraints in bset that involve any of the dimensions
2445 * first to first+n-1.
2447 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_involving(
2448 __isl_take isl_basic_set
*bset
, unsigned first
, unsigned n
)
2450 return isl_basic_map_drop_constraints_involving(bset
, first
, n
);
2453 /* Drop all constraints in bmap that do not involve any of the dimensions
2454 * first to first + n - 1 of the given type.
2456 __isl_give isl_basic_map
*isl_basic_map_drop_constraints_not_involving_dims(
2457 __isl_take isl_basic_map
*bmap
,
2458 enum isl_dim_type type
, unsigned first
, unsigned n
)
2463 isl_space
*space
= isl_basic_map_get_space(bmap
);
2464 isl_basic_map_free(bmap
);
2465 return isl_basic_map_universe(space
);
2467 bmap
= isl_basic_map_cow(bmap
);
2471 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2472 return isl_basic_map_free(bmap
);
2474 first
+= isl_basic_map_offset(bmap
, type
) - 1;
2476 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
) {
2477 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + first
, n
) != -1)
2479 isl_basic_map_drop_equality(bmap
, i
);
2482 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
2483 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + 1 + first
, n
) != -1)
2485 isl_basic_map_drop_inequality(bmap
, i
);
2488 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
2492 /* Drop all constraints in bset that do not involve any of the dimensions
2493 * first to first + n - 1 of the given type.
2495 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_not_involving_dims(
2496 __isl_take isl_basic_set
*bset
,
2497 enum isl_dim_type type
, unsigned first
, unsigned n
)
2499 return isl_basic_map_drop_constraints_not_involving_dims(bset
,
2503 /* Drop all constraints in bmap that involve any of the dimensions
2504 * first to first + n - 1 of the given type.
2506 __isl_give isl_basic_map
*isl_basic_map_drop_constraints_involving_dims(
2507 __isl_take isl_basic_map
*bmap
,
2508 enum isl_dim_type type
, unsigned first
, unsigned n
)
2515 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2516 return isl_basic_map_free(bmap
);
2518 bmap
= isl_basic_map_remove_divs_involving_dims(bmap
, type
, first
, n
);
2519 first
+= isl_basic_map_offset(bmap
, type
) - 1;
2520 return isl_basic_map_drop_constraints_involving(bmap
, first
, n
);
2523 /* Drop all constraints in bset that involve any of the dimensions
2524 * first to first + n - 1 of the given type.
2526 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_involving_dims(
2527 __isl_take isl_basic_set
*bset
,
2528 enum isl_dim_type type
, unsigned first
, unsigned n
)
2530 return isl_basic_map_drop_constraints_involving_dims(bset
,
2534 /* Drop constraints from "map" by applying "drop" to each basic map.
2536 static __isl_give isl_map
*drop_constraints(__isl_take isl_map
*map
,
2537 enum isl_dim_type type
, unsigned first
, unsigned n
,
2538 __isl_give isl_basic_map
*(*drop
)(__isl_take isl_basic_map
*bmap
,
2539 enum isl_dim_type type
, unsigned first
, unsigned n
))
2547 dim
= isl_map_dim(map
, type
);
2548 if (first
+ n
> dim
|| first
+ n
< first
)
2549 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
2550 "index out of bounds", return isl_map_free(map
));
2552 map
= isl_map_cow(map
);
2556 for (i
= 0; i
< map
->n
; ++i
) {
2557 map
->p
[i
] = drop(map
->p
[i
], type
, first
, n
);
2559 return isl_map_free(map
);
2563 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
2568 /* Drop all constraints in map that involve any of the dimensions
2569 * first to first + n - 1 of the given type.
2571 __isl_give isl_map
*isl_map_drop_constraints_involving_dims(
2572 __isl_take isl_map
*map
,
2573 enum isl_dim_type type
, unsigned first
, unsigned n
)
2577 return drop_constraints(map
, type
, first
, n
,
2578 &isl_basic_map_drop_constraints_involving_dims
);
2581 /* Drop all constraints in "map" that do not involve any of the dimensions
2582 * first to first + n - 1 of the given type.
2584 __isl_give isl_map
*isl_map_drop_constraints_not_involving_dims(
2585 __isl_take isl_map
*map
,
2586 enum isl_dim_type type
, unsigned first
, unsigned n
)
2589 isl_space
*space
= isl_map_get_space(map
);
2591 return isl_map_universe(space
);
2593 return drop_constraints(map
, type
, first
, n
,
2594 &isl_basic_map_drop_constraints_not_involving_dims
);
2597 /* Drop all constraints in set that involve any of the dimensions
2598 * first to first + n - 1 of the given type.
2600 __isl_give isl_set
*isl_set_drop_constraints_involving_dims(
2601 __isl_take isl_set
*set
,
2602 enum isl_dim_type type
, unsigned first
, unsigned n
)
2604 return isl_map_drop_constraints_involving_dims(set
, type
, first
, n
);
2607 /* Drop all constraints in "set" that do not involve any of the dimensions
2608 * first to first + n - 1 of the given type.
2610 __isl_give isl_set
*isl_set_drop_constraints_not_involving_dims(
2611 __isl_take isl_set
*set
,
2612 enum isl_dim_type type
, unsigned first
, unsigned n
)
2614 return isl_map_drop_constraints_not_involving_dims(set
, type
, first
, n
);
2617 /* Does local variable "div" of "bmap" have a complete explicit representation?
2618 * Having a complete explicit representation requires not only
2619 * an explicit representation, but also that all local variables
2620 * that appear in this explicit representation in turn have
2621 * a complete explicit representation.
2623 isl_bool
isl_basic_map_div_is_known(__isl_keep isl_basic_map
*bmap
, int div
)
2626 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
2629 marked
= isl_basic_map_div_is_marked_unknown(bmap
, div
);
2630 if (marked
< 0 || marked
)
2631 return isl_bool_not(marked
);
2633 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2636 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
2638 known
= isl_basic_map_div_is_known(bmap
, i
);
2639 if (known
< 0 || !known
)
2643 return isl_bool_true
;
2646 /* Does local variable "div" of "bset" have a complete explicit representation?
2648 isl_bool
isl_basic_set_div_is_known(__isl_keep isl_basic_set
*bset
, int div
)
2650 return isl_basic_map_div_is_known(bset
, div
);
2653 /* Remove all divs that are unknown or defined in terms of unknown divs.
2655 __isl_give isl_basic_map
*isl_basic_map_remove_unknown_divs(
2656 __isl_take isl_basic_map
*bmap
)
2663 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2664 if (isl_basic_map_div_is_known(bmap
, i
))
2666 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
2675 /* Remove all divs that are unknown or defined in terms of unknown divs.
2677 __isl_give isl_basic_set
*isl_basic_set_remove_unknown_divs(
2678 __isl_take isl_basic_set
*bset
)
2680 return isl_basic_map_remove_unknown_divs(bset
);
2683 __isl_give isl_map
*isl_map_remove_unknown_divs(__isl_take isl_map
*map
)
2692 map
= isl_map_cow(map
);
2696 for (i
= 0; i
< map
->n
; ++i
) {
2697 map
->p
[i
] = isl_basic_map_remove_unknown_divs(map
->p
[i
]);
2707 __isl_give isl_set
*isl_set_remove_unknown_divs(__isl_take isl_set
*set
)
2709 return set_from_map(isl_map_remove_unknown_divs(set_to_map(set
)));
2712 __isl_give isl_basic_set
*isl_basic_set_remove_dims(
2713 __isl_take isl_basic_set
*bset
,
2714 enum isl_dim_type type
, unsigned first
, unsigned n
)
2716 isl_basic_map
*bmap
= bset_to_bmap(bset
);
2717 bmap
= isl_basic_map_remove_dims(bmap
, type
, first
, n
);
2718 return bset_from_bmap(bmap
);
2721 struct isl_map
*isl_map_remove_dims(struct isl_map
*map
,
2722 enum isl_dim_type type
, unsigned first
, unsigned n
)
2729 map
= isl_map_cow(map
);
2732 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
2734 for (i
= 0; i
< map
->n
; ++i
) {
2735 map
->p
[i
] = isl_basic_map_eliminate_vars(map
->p
[i
],
2736 isl_basic_map_offset(map
->p
[i
], type
) - 1 + first
, n
);
2740 map
= isl_map_drop(map
, type
, first
, n
);
2747 __isl_give isl_set
*isl_set_remove_dims(__isl_take isl_set
*bset
,
2748 enum isl_dim_type type
, unsigned first
, unsigned n
)
2750 return set_from_map(isl_map_remove_dims(set_to_map(bset
),
2754 /* Project out n inputs starting at first using Fourier-Motzkin */
2755 struct isl_map
*isl_map_remove_inputs(struct isl_map
*map
,
2756 unsigned first
, unsigned n
)
2758 return isl_map_remove_dims(map
, isl_dim_in
, first
, n
);
2761 static void dump_term(struct isl_basic_map
*bmap
,
2762 isl_int c
, int pos
, FILE *out
)
2765 unsigned in
= isl_basic_map_n_in(bmap
);
2766 unsigned dim
= in
+ isl_basic_map_n_out(bmap
);
2767 unsigned nparam
= isl_basic_map_n_param(bmap
);
2769 isl_int_print(out
, c
, 0);
2771 if (!isl_int_is_one(c
))
2772 isl_int_print(out
, c
, 0);
2773 if (pos
< 1 + nparam
) {
2774 name
= isl_space_get_dim_name(bmap
->dim
,
2775 isl_dim_param
, pos
- 1);
2777 fprintf(out
, "%s", name
);
2779 fprintf(out
, "p%d", pos
- 1);
2780 } else if (pos
< 1 + nparam
+ in
)
2781 fprintf(out
, "i%d", pos
- 1 - nparam
);
2782 else if (pos
< 1 + nparam
+ dim
)
2783 fprintf(out
, "o%d", pos
- 1 - nparam
- in
);
2785 fprintf(out
, "e%d", pos
- 1 - nparam
- dim
);
2789 static void dump_constraint_sign(struct isl_basic_map
*bmap
, isl_int
*c
,
2790 int sign
, FILE *out
)
2794 unsigned len
= 1 + isl_basic_map_total_dim(bmap
);
2798 for (i
= 0, first
= 1; i
< len
; ++i
) {
2799 if (isl_int_sgn(c
[i
]) * sign
<= 0)
2802 fprintf(out
, " + ");
2804 isl_int_abs(v
, c
[i
]);
2805 dump_term(bmap
, v
, i
, out
);
2812 static void dump_constraint(struct isl_basic_map
*bmap
, isl_int
*c
,
2813 const char *op
, FILE *out
, int indent
)
2817 fprintf(out
, "%*s", indent
, "");
2819 dump_constraint_sign(bmap
, c
, 1, out
);
2820 fprintf(out
, " %s ", op
);
2821 dump_constraint_sign(bmap
, c
, -1, out
);
2825 for (i
= bmap
->n_div
; i
< bmap
->extra
; ++i
) {
2826 if (isl_int_is_zero(c
[1+isl_space_dim(bmap
->dim
, isl_dim_all
)+i
]))
2828 fprintf(out
, "%*s", indent
, "");
2829 fprintf(out
, "ERROR: unused div coefficient not zero\n");
2834 static void dump_constraints(struct isl_basic_map
*bmap
,
2835 isl_int
**c
, unsigned n
,
2836 const char *op
, FILE *out
, int indent
)
2840 for (i
= 0; i
< n
; ++i
)
2841 dump_constraint(bmap
, c
[i
], op
, out
, indent
);
2844 static void dump_affine(struct isl_basic_map
*bmap
, isl_int
*exp
, FILE *out
)
2848 unsigned total
= isl_basic_map_total_dim(bmap
);
2850 for (j
= 0; j
< 1 + total
; ++j
) {
2851 if (isl_int_is_zero(exp
[j
]))
2853 if (!first
&& isl_int_is_pos(exp
[j
]))
2855 dump_term(bmap
, exp
[j
], j
, out
);
2860 static void dump(struct isl_basic_map
*bmap
, FILE *out
, int indent
)
2864 dump_constraints(bmap
, bmap
->eq
, bmap
->n_eq
, "=", out
, indent
);
2865 dump_constraints(bmap
, bmap
->ineq
, bmap
->n_ineq
, ">=", out
, indent
);
2867 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2868 fprintf(out
, "%*s", indent
, "");
2869 fprintf(out
, "e%d = [(", i
);
2870 dump_affine(bmap
, bmap
->div
[i
]+1, out
);
2872 isl_int_print(out
, bmap
->div
[i
][0], 0);
2873 fprintf(out
, "]\n");
2877 void isl_basic_set_print_internal(struct isl_basic_set
*bset
,
2878 FILE *out
, int indent
)
2881 fprintf(out
, "null basic set\n");
2885 fprintf(out
, "%*s", indent
, "");
2886 fprintf(out
, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
2887 bset
->ref
, bset
->dim
->nparam
, bset
->dim
->n_out
,
2888 bset
->extra
, bset
->flags
);
2889 dump(bset_to_bmap(bset
), out
, indent
);
2892 void isl_basic_map_print_internal(struct isl_basic_map
*bmap
,
2893 FILE *out
, int indent
)
2896 fprintf(out
, "null basic map\n");
2900 fprintf(out
, "%*s", indent
, "");
2901 fprintf(out
, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
2902 "flags: %x, n_name: %d\n",
2904 bmap
->dim
->nparam
, bmap
->dim
->n_in
, bmap
->dim
->n_out
,
2905 bmap
->extra
, bmap
->flags
, bmap
->dim
->n_id
);
2906 dump(bmap
, out
, indent
);
2909 int isl_inequality_negate(struct isl_basic_map
*bmap
, unsigned pos
)
2914 total
= isl_basic_map_total_dim(bmap
);
2915 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
2916 isl_seq_neg(bmap
->ineq
[pos
], bmap
->ineq
[pos
], 1 + total
);
2917 isl_int_sub_ui(bmap
->ineq
[pos
][0], bmap
->ineq
[pos
][0], 1);
2918 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
2922 __isl_give isl_set
*isl_set_alloc_space(__isl_take isl_space
*space
, int n
,
2927 if (isl_space_dim(space
, isl_dim_in
) != 0)
2928 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
2929 "set cannot have input dimensions", goto error
);
2930 return isl_map_alloc_space(space
, n
, flags
);
2932 isl_space_free(space
);
2936 struct isl_set
*isl_set_alloc(struct isl_ctx
*ctx
,
2937 unsigned nparam
, unsigned dim
, int n
, unsigned flags
)
2939 struct isl_set
*set
;
2942 dims
= isl_space_alloc(ctx
, nparam
, 0, dim
);
2946 set
= isl_set_alloc_space(dims
, n
, flags
);
2950 /* Make sure "map" has room for at least "n" more basic maps.
2952 struct isl_map
*isl_map_grow(struct isl_map
*map
, int n
)
2955 struct isl_map
*grown
= NULL
;
2959 isl_assert(map
->ctx
, n
>= 0, goto error
);
2960 if (map
->n
+ n
<= map
->size
)
2962 grown
= isl_map_alloc_space(isl_map_get_space(map
), map
->n
+ n
, map
->flags
);
2965 for (i
= 0; i
< map
->n
; ++i
) {
2966 grown
->p
[i
] = isl_basic_map_copy(map
->p
[i
]);
2974 isl_map_free(grown
);
2979 /* Make sure "set" has room for at least "n" more basic sets.
2981 struct isl_set
*isl_set_grow(struct isl_set
*set
, int n
)
2983 return set_from_map(isl_map_grow(set_to_map(set
), n
));
2986 struct isl_set
*isl_set_dup(struct isl_set
*set
)
2989 struct isl_set
*dup
;
2994 dup
= isl_set_alloc_space(isl_space_copy(set
->dim
), set
->n
, set
->flags
);
2997 for (i
= 0; i
< set
->n
; ++i
)
2998 dup
= isl_set_add_basic_set(dup
, isl_basic_set_copy(set
->p
[i
]));
3002 struct isl_set
*isl_set_from_basic_set(struct isl_basic_set
*bset
)
3004 return isl_map_from_basic_map(bset
);
3007 struct isl_map
*isl_map_from_basic_map(struct isl_basic_map
*bmap
)
3009 struct isl_map
*map
;
3014 map
= isl_map_alloc_space(isl_space_copy(bmap
->dim
), 1, ISL_MAP_DISJOINT
);
3015 return isl_map_add_basic_map(map
, bmap
);
3018 __isl_give isl_set
*isl_set_add_basic_set(__isl_take isl_set
*set
,
3019 __isl_take isl_basic_set
*bset
)
3021 return set_from_map(isl_map_add_basic_map(set_to_map(set
),
3022 bset_to_bmap(bset
)));
3025 __isl_null isl_set
*isl_set_free(__isl_take isl_set
*set
)
3027 return isl_map_free(set
);
3030 void isl_set_print_internal(struct isl_set
*set
, FILE *out
, int indent
)
3035 fprintf(out
, "null set\n");
3039 fprintf(out
, "%*s", indent
, "");
3040 fprintf(out
, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
3041 set
->ref
, set
->n
, set
->dim
->nparam
, set
->dim
->n_out
,
3043 for (i
= 0; i
< set
->n
; ++i
) {
3044 fprintf(out
, "%*s", indent
, "");
3045 fprintf(out
, "basic set %d:\n", i
);
3046 isl_basic_set_print_internal(set
->p
[i
], out
, indent
+4);
3050 void isl_map_print_internal(struct isl_map
*map
, FILE *out
, int indent
)
3055 fprintf(out
, "null map\n");
3059 fprintf(out
, "%*s", indent
, "");
3060 fprintf(out
, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
3061 "flags: %x, n_name: %d\n",
3062 map
->ref
, map
->n
, map
->dim
->nparam
, map
->dim
->n_in
,
3063 map
->dim
->n_out
, map
->flags
, map
->dim
->n_id
);
3064 for (i
= 0; i
< map
->n
; ++i
) {
3065 fprintf(out
, "%*s", indent
, "");
3066 fprintf(out
, "basic map %d:\n", i
);
3067 isl_basic_map_print_internal(map
->p
[i
], out
, indent
+4);
3071 struct isl_basic_map
*isl_basic_map_intersect_domain(
3072 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
3074 struct isl_basic_map
*bmap_domain
;
3079 isl_assert(bset
->ctx
, isl_space_match(bmap
->dim
, isl_dim_param
,
3080 bset
->dim
, isl_dim_param
), goto error
);
3082 if (isl_space_dim(bset
->dim
, isl_dim_set
) != 0)
3083 isl_assert(bset
->ctx
,
3084 isl_basic_map_compatible_domain(bmap
, bset
), goto error
);
3086 bmap
= isl_basic_map_cow(bmap
);
3089 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
3090 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
3091 bmap_domain
= isl_basic_map_from_domain(bset
);
3092 bmap
= add_constraints(bmap
, bmap_domain
, 0, 0);
3094 bmap
= isl_basic_map_simplify(bmap
);
3095 return isl_basic_map_finalize(bmap
);
3097 isl_basic_map_free(bmap
);
3098 isl_basic_set_free(bset
);
3102 struct isl_basic_map
*isl_basic_map_intersect_range(
3103 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
3105 struct isl_basic_map
*bmap_range
;
3110 isl_assert(bset
->ctx
, isl_space_match(bmap
->dim
, isl_dim_param
,
3111 bset
->dim
, isl_dim_param
), goto error
);
3113 if (isl_space_dim(bset
->dim
, isl_dim_set
) != 0)
3114 isl_assert(bset
->ctx
,
3115 isl_basic_map_compatible_range(bmap
, bset
), goto error
);
3117 if (isl_basic_set_plain_is_universe(bset
)) {
3118 isl_basic_set_free(bset
);
3122 bmap
= isl_basic_map_cow(bmap
);
3125 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
3126 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
3127 bmap_range
= bset_to_bmap(bset
);
3128 bmap
= add_constraints(bmap
, bmap_range
, 0, 0);
3130 bmap
= isl_basic_map_simplify(bmap
);
3131 return isl_basic_map_finalize(bmap
);
3133 isl_basic_map_free(bmap
);
3134 isl_basic_set_free(bset
);
3138 isl_bool
isl_basic_map_contains(__isl_keep isl_basic_map
*bmap
,
3139 __isl_keep isl_vec
*vec
)
3146 return isl_bool_error
;
3148 total
= 1 + isl_basic_map_total_dim(bmap
);
3149 if (total
!= vec
->size
)
3150 return isl_bool_error
;
3154 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
3155 isl_seq_inner_product(vec
->el
, bmap
->eq
[i
], total
, &s
);
3156 if (!isl_int_is_zero(s
)) {
3158 return isl_bool_false
;
3162 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
3163 isl_seq_inner_product(vec
->el
, bmap
->ineq
[i
], total
, &s
);
3164 if (isl_int_is_neg(s
)) {
3166 return isl_bool_false
;
3172 return isl_bool_true
;
3175 isl_bool
isl_basic_set_contains(__isl_keep isl_basic_set
*bset
,
3176 __isl_keep isl_vec
*vec
)
3178 return isl_basic_map_contains(bset_to_bmap(bset
), vec
);
3181 struct isl_basic_map
*isl_basic_map_intersect(
3182 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3184 struct isl_vec
*sample
= NULL
;
3186 if (!bmap1
|| !bmap2
)
3189 isl_assert(bmap1
->ctx
, isl_space_match(bmap1
->dim
, isl_dim_param
,
3190 bmap2
->dim
, isl_dim_param
), goto error
);
3191 if (isl_space_dim(bmap1
->dim
, isl_dim_all
) ==
3192 isl_space_dim(bmap1
->dim
, isl_dim_param
) &&
3193 isl_space_dim(bmap2
->dim
, isl_dim_all
) !=
3194 isl_space_dim(bmap2
->dim
, isl_dim_param
))
3195 return isl_basic_map_intersect(bmap2
, bmap1
);
3197 if (isl_space_dim(bmap2
->dim
, isl_dim_all
) !=
3198 isl_space_dim(bmap2
->dim
, isl_dim_param
))
3199 isl_assert(bmap1
->ctx
,
3200 isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
3202 if (isl_basic_map_plain_is_empty(bmap1
)) {
3203 isl_basic_map_free(bmap2
);
3206 if (isl_basic_map_plain_is_empty(bmap2
)) {
3207 isl_basic_map_free(bmap1
);
3211 if (bmap1
->sample
&&
3212 isl_basic_map_contains(bmap1
, bmap1
->sample
) > 0 &&
3213 isl_basic_map_contains(bmap2
, bmap1
->sample
) > 0)
3214 sample
= isl_vec_copy(bmap1
->sample
);
3215 else if (bmap2
->sample
&&
3216 isl_basic_map_contains(bmap1
, bmap2
->sample
) > 0 &&
3217 isl_basic_map_contains(bmap2
, bmap2
->sample
) > 0)
3218 sample
= isl_vec_copy(bmap2
->sample
);
3220 bmap1
= isl_basic_map_cow(bmap1
);
3223 bmap1
= isl_basic_map_extend_space(bmap1
, isl_space_copy(bmap1
->dim
),
3224 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
3225 bmap1
= add_constraints(bmap1
, bmap2
, 0, 0);
3228 isl_vec_free(sample
);
3230 isl_vec_free(bmap1
->sample
);
3231 bmap1
->sample
= sample
;
3234 bmap1
= isl_basic_map_simplify(bmap1
);
3235 return isl_basic_map_finalize(bmap1
);
3238 isl_vec_free(sample
);
3239 isl_basic_map_free(bmap1
);
3240 isl_basic_map_free(bmap2
);
3244 struct isl_basic_set
*isl_basic_set_intersect(
3245 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
3247 return bset_from_bmap(isl_basic_map_intersect(bset_to_bmap(bset1
),
3248 bset_to_bmap(bset2
)));
3251 __isl_give isl_basic_set
*isl_basic_set_intersect_params(
3252 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
3254 return isl_basic_set_intersect(bset1
, bset2
);
3257 /* Special case of isl_map_intersect, where both map1 and map2
3258 * are convex, without any divs and such that either map1 or map2
3259 * contains a single constraint. This constraint is then simply
3260 * added to the other map.
3262 static __isl_give isl_map
*map_intersect_add_constraint(
3263 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
3265 isl_assert(map1
->ctx
, map1
->n
== 1, goto error
);
3266 isl_assert(map2
->ctx
, map1
->n
== 1, goto error
);
3267 isl_assert(map1
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
3268 isl_assert(map2
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
3270 if (map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
!= 1)
3271 return isl_map_intersect(map2
, map1
);
3273 isl_assert(map2
->ctx
,
3274 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1, goto error
);
3276 map1
= isl_map_cow(map1
);
3279 if (isl_map_plain_is_empty(map1
)) {
3283 map1
->p
[0] = isl_basic_map_cow(map1
->p
[0]);
3284 if (map2
->p
[0]->n_eq
== 1)
3285 map1
->p
[0] = isl_basic_map_add_eq(map1
->p
[0], map2
->p
[0]->eq
[0]);
3287 map1
->p
[0] = isl_basic_map_add_ineq(map1
->p
[0],
3288 map2
->p
[0]->ineq
[0]);
3290 map1
->p
[0] = isl_basic_map_simplify(map1
->p
[0]);
3291 map1
->p
[0] = isl_basic_map_finalize(map1
->p
[0]);
3295 if (isl_basic_map_plain_is_empty(map1
->p
[0])) {
3296 isl_basic_map_free(map1
->p
[0]);
3309 /* map2 may be either a parameter domain or a map living in the same
3312 static __isl_give isl_map
*map_intersect_internal(__isl_take isl_map
*map1
,
3313 __isl_take isl_map
*map2
)
3322 if ((isl_map_plain_is_empty(map1
) ||
3323 isl_map_plain_is_universe(map2
)) &&
3324 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
3328 if ((isl_map_plain_is_empty(map2
) ||
3329 isl_map_plain_is_universe(map1
)) &&
3330 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
3335 if (map1
->n
== 1 && map2
->n
== 1 &&
3336 map1
->p
[0]->n_div
== 0 && map2
->p
[0]->n_div
== 0 &&
3337 isl_space_is_equal(map1
->dim
, map2
->dim
) &&
3338 (map1
->p
[0]->n_eq
+ map1
->p
[0]->n_ineq
== 1 ||
3339 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1))
3340 return map_intersect_add_constraint(map1
, map2
);
3342 if (isl_space_dim(map2
->dim
, isl_dim_all
) !=
3343 isl_space_dim(map2
->dim
, isl_dim_param
))
3344 isl_assert(map1
->ctx
,
3345 isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
3347 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
3348 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
3349 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
3351 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
3352 map1
->n
* map2
->n
, flags
);
3355 for (i
= 0; i
< map1
->n
; ++i
)
3356 for (j
= 0; j
< map2
->n
; ++j
) {
3357 struct isl_basic_map
*part
;
3358 part
= isl_basic_map_intersect(
3359 isl_basic_map_copy(map1
->p
[i
]),
3360 isl_basic_map_copy(map2
->p
[j
]));
3361 if (isl_basic_map_is_empty(part
) < 0)
3362 part
= isl_basic_map_free(part
);
3363 result
= isl_map_add_basic_map(result
, part
);
3376 static __isl_give isl_map
*map_intersect(__isl_take isl_map
*map1
,
3377 __isl_take isl_map
*map2
)
3381 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
3382 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
3383 "spaces don't match", goto error
);
3384 return map_intersect_internal(map1
, map2
);
3391 __isl_give isl_map
*isl_map_intersect(__isl_take isl_map
*map1
,
3392 __isl_take isl_map
*map2
)
3394 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect
);
3397 struct isl_set
*isl_set_intersect(struct isl_set
*set1
, struct isl_set
*set2
)
3399 return set_from_map(isl_map_intersect(set_to_map(set1
),
3403 /* map_intersect_internal accepts intersections
3404 * with parameter domains, so we can just call that function.
3406 static __isl_give isl_map
*map_intersect_params(__isl_take isl_map
*map
,
3407 __isl_take isl_set
*params
)
3409 return map_intersect_internal(map
, params
);
3412 __isl_give isl_map
*isl_map_intersect_params(__isl_take isl_map
*map1
,
3413 __isl_take isl_map
*map2
)
3415 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect_params
);
3418 __isl_give isl_set
*isl_set_intersect_params(__isl_take isl_set
*set
,
3419 __isl_take isl_set
*params
)
3421 return isl_map_intersect_params(set
, params
);
3424 struct isl_basic_map
*isl_basic_map_reverse(struct isl_basic_map
*bmap
)
3427 unsigned pos
, n1
, n2
;
3431 bmap
= isl_basic_map_cow(bmap
);
3434 space
= isl_space_reverse(isl_space_copy(bmap
->dim
));
3435 pos
= isl_basic_map_offset(bmap
, isl_dim_in
);
3436 n1
= isl_basic_map_dim(bmap
, isl_dim_in
);
3437 n2
= isl_basic_map_dim(bmap
, isl_dim_out
);
3438 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
3439 return isl_basic_map_reset_space(bmap
, space
);
3442 static __isl_give isl_basic_map
*basic_map_space_reset(
3443 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
)
3449 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
3452 space
= isl_basic_map_get_space(bmap
);
3453 space
= isl_space_reset(space
, type
);
3454 bmap
= isl_basic_map_reset_space(bmap
, space
);
3458 __isl_give isl_basic_map
*isl_basic_map_insert_dims(
3459 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
3460 unsigned pos
, unsigned n
)
3463 struct isl_basic_map
*res
;
3464 struct isl_dim_map
*dim_map
;
3465 unsigned total
, off
;
3466 enum isl_dim_type t
;
3469 return basic_map_space_reset(bmap
, type
);
3474 res_dim
= isl_space_insert_dims(isl_basic_map_get_space(bmap
), type
, pos
, n
);
3476 total
= isl_basic_map_total_dim(bmap
) + n
;
3477 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3479 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3481 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3483 unsigned size
= isl_basic_map_dim(bmap
, t
);
3484 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3486 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3487 pos
, size
- pos
, off
+ pos
+ n
);
3489 off
+= isl_space_dim(res_dim
, t
);
3491 isl_dim_map_div(dim_map
, bmap
, off
);
3493 res
= isl_basic_map_alloc_space(res_dim
,
3494 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3495 if (isl_basic_map_is_rational(bmap
))
3496 res
= isl_basic_map_set_rational(res
);
3497 if (isl_basic_map_plain_is_empty(bmap
)) {
3498 isl_basic_map_free(bmap
);
3500 return isl_basic_map_set_to_empty(res
);
3502 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3503 return isl_basic_map_finalize(res
);
3506 __isl_give isl_basic_set
*isl_basic_set_insert_dims(
3507 __isl_take isl_basic_set
*bset
,
3508 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3510 return isl_basic_map_insert_dims(bset
, type
, pos
, n
);
3513 __isl_give isl_basic_map
*isl_basic_map_add_dims(__isl_take isl_basic_map
*bmap
,
3514 enum isl_dim_type type
, unsigned n
)
3518 return isl_basic_map_insert_dims(bmap
, type
,
3519 isl_basic_map_dim(bmap
, type
), n
);
3522 __isl_give isl_basic_set
*isl_basic_set_add_dims(__isl_take isl_basic_set
*bset
,
3523 enum isl_dim_type type
, unsigned n
)
3527 isl_assert(bset
->ctx
, type
!= isl_dim_in
, goto error
);
3528 return isl_basic_map_add_dims(bset
, type
, n
);
3530 isl_basic_set_free(bset
);
3534 static __isl_give isl_map
*map_space_reset(__isl_take isl_map
*map
,
3535 enum isl_dim_type type
)
3539 if (!map
|| !isl_space_is_named_or_nested(map
->dim
, type
))
3542 space
= isl_map_get_space(map
);
3543 space
= isl_space_reset(space
, type
);
3544 map
= isl_map_reset_space(map
, space
);
3548 __isl_give isl_map
*isl_map_insert_dims(__isl_take isl_map
*map
,
3549 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3554 return map_space_reset(map
, type
);
3556 map
= isl_map_cow(map
);
3560 map
->dim
= isl_space_insert_dims(map
->dim
, type
, pos
, n
);
3564 for (i
= 0; i
< map
->n
; ++i
) {
3565 map
->p
[i
] = isl_basic_map_insert_dims(map
->p
[i
], type
, pos
, n
);
3576 __isl_give isl_set
*isl_set_insert_dims(__isl_take isl_set
*set
,
3577 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3579 return isl_map_insert_dims(set
, type
, pos
, n
);
3582 __isl_give isl_map
*isl_map_add_dims(__isl_take isl_map
*map
,
3583 enum isl_dim_type type
, unsigned n
)
3587 return isl_map_insert_dims(map
, type
, isl_map_dim(map
, type
), n
);
3590 __isl_give isl_set
*isl_set_add_dims(__isl_take isl_set
*set
,
3591 enum isl_dim_type type
, unsigned n
)
3595 isl_assert(set
->ctx
, type
!= isl_dim_in
, goto error
);
3596 return set_from_map(isl_map_add_dims(set_to_map(set
), type
, n
));
3602 __isl_give isl_basic_map
*isl_basic_map_move_dims(
3603 __isl_take isl_basic_map
*bmap
,
3604 enum isl_dim_type dst_type
, unsigned dst_pos
,
3605 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3607 struct isl_dim_map
*dim_map
;
3608 struct isl_basic_map
*res
;
3609 enum isl_dim_type t
;
3610 unsigned total
, off
;
3617 if (isl_basic_map_check_range(bmap
, src_type
, src_pos
, n
) < 0)
3618 return isl_basic_map_free(bmap
);
3620 if (dst_type
== src_type
&& dst_pos
== src_pos
)
3623 isl_assert(bmap
->ctx
, dst_type
!= src_type
, goto error
);
3625 if (pos(bmap
->dim
, dst_type
) + dst_pos
==
3626 pos(bmap
->dim
, src_type
) + src_pos
+
3627 ((src_type
< dst_type
) ? n
: 0)) {
3628 bmap
= isl_basic_map_cow(bmap
);
3632 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
3633 src_type
, src_pos
, n
);
3637 bmap
= isl_basic_map_finalize(bmap
);
3642 total
= isl_basic_map_total_dim(bmap
);
3643 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3646 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3647 unsigned size
= isl_space_dim(bmap
->dim
, t
);
3648 if (t
== dst_type
) {
3649 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3652 isl_dim_map_dim_range(dim_map
, bmap
->dim
, src_type
,
3655 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3656 dst_pos
, size
- dst_pos
, off
);
3657 off
+= size
- dst_pos
;
3658 } else if (t
== src_type
) {
3659 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3662 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3663 src_pos
+ n
, size
- src_pos
- n
, off
);
3664 off
+= size
- src_pos
- n
;
3666 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3670 isl_dim_map_div(dim_map
, bmap
, off
);
3672 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
3673 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3674 bmap
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3678 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
3679 src_type
, src_pos
, n
);
3683 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
3684 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3685 bmap
= isl_basic_map_finalize(bmap
);
3689 isl_basic_map_free(bmap
);
3693 __isl_give isl_basic_set
*isl_basic_set_move_dims(__isl_take isl_basic_set
*bset
,
3694 enum isl_dim_type dst_type
, unsigned dst_pos
,
3695 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3697 isl_basic_map
*bmap
= bset_to_bmap(bset
);
3698 bmap
= isl_basic_map_move_dims(bmap
, dst_type
, dst_pos
,
3699 src_type
, src_pos
, n
);
3700 return bset_from_bmap(bmap
);
3703 __isl_give isl_set
*isl_set_move_dims(__isl_take isl_set
*set
,
3704 enum isl_dim_type dst_type
, unsigned dst_pos
,
3705 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3709 isl_assert(set
->ctx
, dst_type
!= isl_dim_in
, goto error
);
3710 return set_from_map(isl_map_move_dims(set_to_map(set
),
3711 dst_type
, dst_pos
, src_type
, src_pos
, n
));
3717 __isl_give isl_map
*isl_map_move_dims(__isl_take isl_map
*map
,
3718 enum isl_dim_type dst_type
, unsigned dst_pos
,
3719 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3728 isl_assert(map
->ctx
, src_pos
+ n
<= isl_map_dim(map
, src_type
),
3731 if (dst_type
== src_type
&& dst_pos
== src_pos
)
3734 isl_assert(map
->ctx
, dst_type
!= src_type
, goto error
);
3736 map
= isl_map_cow(map
);
3740 map
->dim
= isl_space_move_dims(map
->dim
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
3744 for (i
= 0; i
< map
->n
; ++i
) {
3745 map
->p
[i
] = isl_basic_map_move_dims(map
->p
[i
],
3747 src_type
, src_pos
, n
);
3758 /* Move the specified dimensions to the last columns right before
3759 * the divs. Don't change the dimension specification of bmap.
3760 * That's the responsibility of the caller.
3762 static __isl_give isl_basic_map
*move_last(__isl_take isl_basic_map
*bmap
,
3763 enum isl_dim_type type
, unsigned first
, unsigned n
)
3765 struct isl_dim_map
*dim_map
;
3766 struct isl_basic_map
*res
;
3767 enum isl_dim_type t
;
3768 unsigned total
, off
;
3772 if (pos(bmap
->dim
, type
) + first
+ n
==
3773 1 + isl_space_dim(bmap
->dim
, isl_dim_all
))
3776 total
= isl_basic_map_total_dim(bmap
);
3777 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3780 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3781 unsigned size
= isl_space_dim(bmap
->dim
, t
);
3783 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3786 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3787 first
, n
, total
- bmap
->n_div
- n
);
3788 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3789 first
+ n
, size
- (first
+ n
), off
);
3790 off
+= size
- (first
+ n
);
3792 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3796 isl_dim_map_div(dim_map
, bmap
, off
+ n
);
3798 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
3799 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3800 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3804 /* Insert "n" rows in the divs of "bmap".
3806 * The number of columns is not changed, which means that the last
3807 * dimensions of "bmap" are being reintepreted as the new divs.
3808 * The space of "bmap" is not adjusted, however, which means
3809 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
3810 * from the space of "bmap" is the responsibility of the caller.
3812 static __isl_give isl_basic_map
*insert_div_rows(__isl_take isl_basic_map
*bmap
,
3820 bmap
= isl_basic_map_cow(bmap
);
3824 row_size
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + bmap
->extra
;
3825 old
= bmap
->block2
.data
;
3826 bmap
->block2
= isl_blk_extend(bmap
->ctx
, bmap
->block2
,
3827 (bmap
->extra
+ n
) * (1 + row_size
));
3828 if (!bmap
->block2
.data
)
3829 return isl_basic_map_free(bmap
);
3830 new_div
= isl_alloc_array(bmap
->ctx
, isl_int
*, bmap
->extra
+ n
);
3832 return isl_basic_map_free(bmap
);
3833 for (i
= 0; i
< n
; ++i
) {
3834 new_div
[i
] = bmap
->block2
.data
+
3835 (bmap
->extra
+ i
) * (1 + row_size
);
3836 isl_seq_clr(new_div
[i
], 1 + row_size
);
3838 for (i
= 0; i
< bmap
->extra
; ++i
)
3839 new_div
[n
+ i
] = bmap
->block2
.data
+ (bmap
->div
[i
] - old
);
3841 bmap
->div
= new_div
;
3848 /* Drop constraints from "bmap" that only involve the variables
3849 * of "type" in the range [first, first + n] that are not related
3850 * to any of the variables outside that interval.
3851 * These constraints cannot influence the values for the variables
3852 * outside the interval, except in case they cause "bmap" to be empty.
3853 * Only drop the constraints if "bmap" is known to be non-empty.
3855 static __isl_give isl_basic_map
*drop_irrelevant_constraints(
3856 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
3857 unsigned first
, unsigned n
)
3861 unsigned dim
, n_div
;
3864 non_empty
= isl_basic_map_plain_is_non_empty(bmap
);
3866 return isl_basic_map_free(bmap
);
3870 dim
= isl_basic_map_dim(bmap
, isl_dim_all
);
3871 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
3872 groups
= isl_calloc_array(isl_basic_map_get_ctx(bmap
), int, dim
);
3874 return isl_basic_map_free(bmap
);
3875 first
+= isl_basic_map_offset(bmap
, type
) - 1;
3876 for (i
= 0; i
< first
; ++i
)
3878 for (i
= first
+ n
; i
< dim
- n_div
; ++i
)
3881 bmap
= isl_basic_map_drop_unrelated_constraints(bmap
, groups
);
3886 /* Turn the n dimensions of type type, starting at first
3887 * into existentially quantified variables.
3889 * If a subset of the projected out variables are unrelated
3890 * to any of the variables that remain, then the constraints
3891 * involving this subset are simply dropped first.
3893 __isl_give isl_basic_map
*isl_basic_map_project_out(
3894 __isl_take isl_basic_map
*bmap
,
3895 enum isl_dim_type type
, unsigned first
, unsigned n
)
3898 return basic_map_space_reset(bmap
, type
);
3899 if (type
== isl_dim_div
)
3900 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
3901 "cannot project out existentially quantified variables",
3902 return isl_basic_map_free(bmap
));
3904 bmap
= drop_irrelevant_constraints(bmap
, type
, first
, n
);
3908 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
3909 return isl_basic_map_remove_dims(bmap
, type
, first
, n
);
3911 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
3912 return isl_basic_map_free(bmap
);
3914 bmap
= move_last(bmap
, type
, first
, n
);
3915 bmap
= isl_basic_map_cow(bmap
);
3916 bmap
= insert_div_rows(bmap
, n
);
3920 bmap
->dim
= isl_space_drop_dims(bmap
->dim
, type
, first
, n
);
3923 bmap
= isl_basic_map_simplify(bmap
);
3924 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
3925 return isl_basic_map_finalize(bmap
);
3927 isl_basic_map_free(bmap
);
3931 /* Turn the n dimensions of type type, starting at first
3932 * into existentially quantified variables.
3934 struct isl_basic_set
*isl_basic_set_project_out(struct isl_basic_set
*bset
,
3935 enum isl_dim_type type
, unsigned first
, unsigned n
)
3937 return bset_from_bmap(isl_basic_map_project_out(bset_to_bmap(bset
),
3941 /* Turn the n dimensions of type type, starting at first
3942 * into existentially quantified variables.
3944 __isl_give isl_map
*isl_map_project_out(__isl_take isl_map
*map
,
3945 enum isl_dim_type type
, unsigned first
, unsigned n
)
3953 return map_space_reset(map
, type
);
3955 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
3957 map
= isl_map_cow(map
);
3961 map
->dim
= isl_space_drop_dims(map
->dim
, type
, first
, n
);
3965 for (i
= 0; i
< map
->n
; ++i
) {
3966 map
->p
[i
] = isl_basic_map_project_out(map
->p
[i
], type
, first
, n
);
3977 /* Turn the n dimensions of type type, starting at first
3978 * into existentially quantified variables.
3980 __isl_give isl_set
*isl_set_project_out(__isl_take isl_set
*set
,
3981 enum isl_dim_type type
, unsigned first
, unsigned n
)
3983 return set_from_map(isl_map_project_out(set_to_map(set
),
3987 /* Return a map that projects the elements in "set" onto their
3988 * "n" set dimensions starting at "first".
3989 * "type" should be equal to isl_dim_set.
3991 __isl_give isl_map
*isl_set_project_onto_map(__isl_take isl_set
*set
,
3992 enum isl_dim_type type
, unsigned first
, unsigned n
)
4000 if (type
!= isl_dim_set
)
4001 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
4002 "only set dimensions can be projected out", goto error
);
4003 dim
= isl_set_dim(set
, isl_dim_set
);
4004 if (first
+ n
> dim
|| first
+ n
< first
)
4005 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
4006 "index out of bounds", goto error
);
4008 map
= isl_map_from_domain(set
);
4009 map
= isl_map_add_dims(map
, isl_dim_out
, n
);
4010 for (i
= 0; i
< n
; ++i
)
4011 map
= isl_map_equate(map
, isl_dim_in
, first
+ i
,
4019 static struct isl_basic_map
*add_divs(struct isl_basic_map
*bmap
, unsigned n
)
4023 for (i
= 0; i
< n
; ++i
) {
4024 j
= isl_basic_map_alloc_div(bmap
);
4027 isl_seq_clr(bmap
->div
[j
], 1+1+isl_basic_map_total_dim(bmap
));
4031 isl_basic_map_free(bmap
);
4035 struct isl_basic_map
*isl_basic_map_apply_range(
4036 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
4038 isl_space
*dim_result
= NULL
;
4039 struct isl_basic_map
*bmap
;
4040 unsigned n_in
, n_out
, n
, nparam
, total
, pos
;
4041 struct isl_dim_map
*dim_map1
, *dim_map2
;
4043 if (!bmap1
|| !bmap2
)
4045 if (!isl_space_match(bmap1
->dim
, isl_dim_param
,
4046 bmap2
->dim
, isl_dim_param
))
4047 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
4048 "parameters don't match", goto error
);
4049 if (!isl_space_tuple_is_equal(bmap1
->dim
, isl_dim_out
,
4050 bmap2
->dim
, isl_dim_in
))
4051 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
4052 "spaces don't match", goto error
);
4054 dim_result
= isl_space_join(isl_space_copy(bmap1
->dim
),
4055 isl_space_copy(bmap2
->dim
));
4057 n_in
= isl_basic_map_n_in(bmap1
);
4058 n_out
= isl_basic_map_n_out(bmap2
);
4059 n
= isl_basic_map_n_out(bmap1
);
4060 nparam
= isl_basic_map_n_param(bmap1
);
4062 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ n
;
4063 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4064 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4065 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
4066 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
4067 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
4068 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_in
);
4069 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_out
);
4070 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
4071 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
4072 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
4074 bmap
= isl_basic_map_alloc_space(dim_result
,
4075 bmap1
->n_div
+ bmap2
->n_div
+ n
,
4076 bmap1
->n_eq
+ bmap2
->n_eq
,
4077 bmap1
->n_ineq
+ bmap2
->n_ineq
);
4078 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
4079 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
4080 bmap
= add_divs(bmap
, n
);
4081 bmap
= isl_basic_map_simplify(bmap
);
4082 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
4083 return isl_basic_map_finalize(bmap
);
4085 isl_basic_map_free(bmap1
);
4086 isl_basic_map_free(bmap2
);
4090 struct isl_basic_set
*isl_basic_set_apply(
4091 struct isl_basic_set
*bset
, struct isl_basic_map
*bmap
)
4096 isl_assert(bset
->ctx
, isl_basic_map_compatible_domain(bmap
, bset
),
4099 return bset_from_bmap(isl_basic_map_apply_range(bset_to_bmap(bset
),
4102 isl_basic_set_free(bset
);
4103 isl_basic_map_free(bmap
);
4107 struct isl_basic_map
*isl_basic_map_apply_domain(
4108 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
4110 if (!bmap1
|| !bmap2
)
4113 isl_assert(bmap1
->ctx
,
4114 isl_basic_map_n_in(bmap1
) == isl_basic_map_n_in(bmap2
), goto error
);
4115 isl_assert(bmap1
->ctx
,
4116 isl_basic_map_n_param(bmap1
) == isl_basic_map_n_param(bmap2
),
4119 bmap1
= isl_basic_map_reverse(bmap1
);
4120 bmap1
= isl_basic_map_apply_range(bmap1
, bmap2
);
4121 return isl_basic_map_reverse(bmap1
);
4123 isl_basic_map_free(bmap1
);
4124 isl_basic_map_free(bmap2
);
4128 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
4129 * A \cap B -> f(A) + f(B)
4131 struct isl_basic_map
*isl_basic_map_sum(
4132 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
4134 unsigned n_in
, n_out
, nparam
, total
, pos
;
4135 struct isl_basic_map
*bmap
= NULL
;
4136 struct isl_dim_map
*dim_map1
, *dim_map2
;
4139 if (!bmap1
|| !bmap2
)
4142 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
),
4145 nparam
= isl_basic_map_n_param(bmap1
);
4146 n_in
= isl_basic_map_n_in(bmap1
);
4147 n_out
= isl_basic_map_n_out(bmap1
);
4149 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
;
4150 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4151 dim_map2
= isl_dim_map_alloc(bmap2
->ctx
, total
);
4152 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
4153 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
);
4154 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
4155 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
4156 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_in
+ n_out
);
4157 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
4158 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
4159 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_out
);
4161 bmap
= isl_basic_map_alloc_space(isl_space_copy(bmap1
->dim
),
4162 bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
,
4163 bmap1
->n_eq
+ bmap2
->n_eq
+ n_out
,
4164 bmap1
->n_ineq
+ bmap2
->n_ineq
);
4165 for (i
= 0; i
< n_out
; ++i
) {
4166 int j
= isl_basic_map_alloc_equality(bmap
);
4169 isl_seq_clr(bmap
->eq
[j
], 1+total
);
4170 isl_int_set_si(bmap
->eq
[j
][1+nparam
+n_in
+i
], -1);
4171 isl_int_set_si(bmap
->eq
[j
][1+pos
+i
], 1);
4172 isl_int_set_si(bmap
->eq
[j
][1+pos
-n_out
+i
], 1);
4174 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
4175 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
4176 bmap
= add_divs(bmap
, 2 * n_out
);
4178 bmap
= isl_basic_map_simplify(bmap
);
4179 return isl_basic_map_finalize(bmap
);
4181 isl_basic_map_free(bmap
);
4182 isl_basic_map_free(bmap1
);
4183 isl_basic_map_free(bmap2
);
4187 /* Given two maps A -> f(A) and B -> g(B), construct a map
4188 * A \cap B -> f(A) + f(B)
4190 struct isl_map
*isl_map_sum(struct isl_map
*map1
, struct isl_map
*map2
)
4192 struct isl_map
*result
;
4198 isl_assert(map1
->ctx
, isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
4200 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
4201 map1
->n
* map2
->n
, 0);
4204 for (i
= 0; i
< map1
->n
; ++i
)
4205 for (j
= 0; j
< map2
->n
; ++j
) {
4206 struct isl_basic_map
*part
;
4207 part
= isl_basic_map_sum(
4208 isl_basic_map_copy(map1
->p
[i
]),
4209 isl_basic_map_copy(map2
->p
[j
]));
4210 if (isl_basic_map_is_empty(part
))
4211 isl_basic_map_free(part
);
4213 result
= isl_map_add_basic_map(result
, part
);
4226 __isl_give isl_set
*isl_set_sum(__isl_take isl_set
*set1
,
4227 __isl_take isl_set
*set2
)
4229 return set_from_map(isl_map_sum(set_to_map(set1
), set_to_map(set2
)));
4232 /* Given a basic map A -> f(A), construct A -> -f(A).
4234 struct isl_basic_map
*isl_basic_map_neg(struct isl_basic_map
*bmap
)
4239 bmap
= isl_basic_map_cow(bmap
);
4243 n
= isl_basic_map_dim(bmap
, isl_dim_out
);
4244 off
= isl_basic_map_offset(bmap
, isl_dim_out
);
4245 for (i
= 0; i
< bmap
->n_eq
; ++i
)
4246 for (j
= 0; j
< n
; ++j
)
4247 isl_int_neg(bmap
->eq
[i
][off
+j
], bmap
->eq
[i
][off
+j
]);
4248 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
4249 for (j
= 0; j
< n
; ++j
)
4250 isl_int_neg(bmap
->ineq
[i
][off
+j
], bmap
->ineq
[i
][off
+j
]);
4251 for (i
= 0; i
< bmap
->n_div
; ++i
)
4252 for (j
= 0; j
< n
; ++j
)
4253 isl_int_neg(bmap
->div
[i
][1+off
+j
], bmap
->div
[i
][1+off
+j
]);
4254 bmap
= isl_basic_map_gauss(bmap
, NULL
);
4255 return isl_basic_map_finalize(bmap
);
4258 __isl_give isl_basic_set
*isl_basic_set_neg(__isl_take isl_basic_set
*bset
)
4260 return isl_basic_map_neg(bset
);
4263 /* Given a map A -> f(A), construct A -> -f(A).
4265 struct isl_map
*isl_map_neg(struct isl_map
*map
)
4269 map
= isl_map_cow(map
);
4273 for (i
= 0; i
< map
->n
; ++i
) {
4274 map
->p
[i
] = isl_basic_map_neg(map
->p
[i
]);
4285 __isl_give isl_set
*isl_set_neg(__isl_take isl_set
*set
)
4287 return set_from_map(isl_map_neg(set_to_map(set
)));
4290 /* Given a basic map A -> f(A) and an integer d, construct a basic map
4291 * A -> floor(f(A)/d).
4293 struct isl_basic_map
*isl_basic_map_floordiv(struct isl_basic_map
*bmap
,
4296 unsigned n_in
, n_out
, nparam
, total
, pos
;
4297 struct isl_basic_map
*result
= NULL
;
4298 struct isl_dim_map
*dim_map
;
4304 nparam
= isl_basic_map_n_param(bmap
);
4305 n_in
= isl_basic_map_n_in(bmap
);
4306 n_out
= isl_basic_map_n_out(bmap
);
4308 total
= nparam
+ n_in
+ n_out
+ bmap
->n_div
+ n_out
;
4309 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
4310 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_param
, pos
= 0);
4311 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_in
, pos
+= nparam
);
4312 isl_dim_map_div(dim_map
, bmap
, pos
+= n_in
+ n_out
);
4313 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_out
, pos
+= bmap
->n_div
);
4315 result
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
4316 bmap
->n_div
+ n_out
,
4317 bmap
->n_eq
, bmap
->n_ineq
+ 2 * n_out
);
4318 result
= isl_basic_map_add_constraints_dim_map(result
, bmap
, dim_map
);
4319 result
= add_divs(result
, n_out
);
4320 for (i
= 0; i
< n_out
; ++i
) {
4322 j
= isl_basic_map_alloc_inequality(result
);
4325 isl_seq_clr(result
->ineq
[j
], 1+total
);
4326 isl_int_neg(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
4327 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], 1);
4328 j
= isl_basic_map_alloc_inequality(result
);
4331 isl_seq_clr(result
->ineq
[j
], 1+total
);
4332 isl_int_set(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
4333 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], -1);
4334 isl_int_sub_ui(result
->ineq
[j
][0], d
, 1);
4337 result
= isl_basic_map_simplify(result
);
4338 return isl_basic_map_finalize(result
);
4340 isl_basic_map_free(result
);
4344 /* Given a map A -> f(A) and an integer d, construct a map
4345 * A -> floor(f(A)/d).
4347 struct isl_map
*isl_map_floordiv(struct isl_map
*map
, isl_int d
)
4351 map
= isl_map_cow(map
);
4355 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
4356 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4357 for (i
= 0; i
< map
->n
; ++i
) {
4358 map
->p
[i
] = isl_basic_map_floordiv(map
->p
[i
], d
);
4369 /* Given a map A -> f(A) and an integer d, construct a map
4370 * A -> floor(f(A)/d).
4372 __isl_give isl_map
*isl_map_floordiv_val(__isl_take isl_map
*map
,
4373 __isl_take isl_val
*d
)
4377 if (!isl_val_is_int(d
))
4378 isl_die(isl_val_get_ctx(d
), isl_error_invalid
,
4379 "expecting integer denominator", goto error
);
4380 map
= isl_map_floordiv(map
, d
->n
);
4389 static struct isl_basic_map
*var_equal(struct isl_basic_map
*bmap
, unsigned pos
)
4395 i
= isl_basic_map_alloc_equality(bmap
);
4398 nparam
= isl_basic_map_n_param(bmap
);
4399 n_in
= isl_basic_map_n_in(bmap
);
4400 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4401 isl_int_set_si(bmap
->eq
[i
][1+nparam
+pos
], -1);
4402 isl_int_set_si(bmap
->eq
[i
][1+nparam
+n_in
+pos
], 1);
4403 return isl_basic_map_finalize(bmap
);
4405 isl_basic_map_free(bmap
);
4409 /* Add a constraint to "bmap" expressing i_pos < o_pos
4411 static struct isl_basic_map
*var_less(struct isl_basic_map
*bmap
, unsigned pos
)
4417 i
= isl_basic_map_alloc_inequality(bmap
);
4420 nparam
= isl_basic_map_n_param(bmap
);
4421 n_in
= isl_basic_map_n_in(bmap
);
4422 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4423 isl_int_set_si(bmap
->ineq
[i
][0], -1);
4424 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
4425 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
4426 return isl_basic_map_finalize(bmap
);
4428 isl_basic_map_free(bmap
);
4432 /* Add a constraint to "bmap" expressing i_pos <= o_pos
4434 static __isl_give isl_basic_map
*var_less_or_equal(
4435 __isl_take isl_basic_map
*bmap
, unsigned pos
)
4441 i
= isl_basic_map_alloc_inequality(bmap
);
4444 nparam
= isl_basic_map_n_param(bmap
);
4445 n_in
= isl_basic_map_n_in(bmap
);
4446 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4447 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
4448 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
4449 return isl_basic_map_finalize(bmap
);
4451 isl_basic_map_free(bmap
);
4455 /* Add a constraint to "bmap" expressing i_pos > o_pos
4457 static struct isl_basic_map
*var_more(struct isl_basic_map
*bmap
, unsigned pos
)
4463 i
= isl_basic_map_alloc_inequality(bmap
);
4466 nparam
= isl_basic_map_n_param(bmap
);
4467 n_in
= isl_basic_map_n_in(bmap
);
4468 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4469 isl_int_set_si(bmap
->ineq
[i
][0], -1);
4470 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
4471 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
4472 return isl_basic_map_finalize(bmap
);
4474 isl_basic_map_free(bmap
);
4478 /* Add a constraint to "bmap" expressing i_pos >= o_pos
4480 static __isl_give isl_basic_map
*var_more_or_equal(
4481 __isl_take isl_basic_map
*bmap
, unsigned pos
)
4487 i
= isl_basic_map_alloc_inequality(bmap
);
4490 nparam
= isl_basic_map_n_param(bmap
);
4491 n_in
= isl_basic_map_n_in(bmap
);
4492 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4493 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
4494 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
4495 return isl_basic_map_finalize(bmap
);
4497 isl_basic_map_free(bmap
);
4501 __isl_give isl_basic_map
*isl_basic_map_equal(
4502 __isl_take isl_space
*dim
, unsigned n_equal
)
4505 struct isl_basic_map
*bmap
;
4506 bmap
= isl_basic_map_alloc_space(dim
, 0, n_equal
, 0);
4509 for (i
= 0; i
< n_equal
&& bmap
; ++i
)
4510 bmap
= var_equal(bmap
, i
);
4511 return isl_basic_map_finalize(bmap
);
4514 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
4516 __isl_give isl_basic_map
*isl_basic_map_less_at(__isl_take isl_space
*dim
,
4520 struct isl_basic_map
*bmap
;
4521 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4524 for (i
= 0; i
< pos
&& bmap
; ++i
)
4525 bmap
= var_equal(bmap
, i
);
4527 bmap
= var_less(bmap
, pos
);
4528 return isl_basic_map_finalize(bmap
);
4531 /* Return a relation on "dim" expressing i_[0..pos] <<= o_[0..pos]
4533 __isl_give isl_basic_map
*isl_basic_map_less_or_equal_at(
4534 __isl_take isl_space
*dim
, unsigned pos
)
4537 isl_basic_map
*bmap
;
4539 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4540 for (i
= 0; i
< pos
; ++i
)
4541 bmap
= var_equal(bmap
, i
);
4542 bmap
= var_less_or_equal(bmap
, pos
);
4543 return isl_basic_map_finalize(bmap
);
4546 /* Return a relation on "dim" expressing i_pos > o_pos
4548 __isl_give isl_basic_map
*isl_basic_map_more_at(__isl_take isl_space
*dim
,
4552 struct isl_basic_map
*bmap
;
4553 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4556 for (i
= 0; i
< pos
&& bmap
; ++i
)
4557 bmap
= var_equal(bmap
, i
);
4559 bmap
= var_more(bmap
, pos
);
4560 return isl_basic_map_finalize(bmap
);
4563 /* Return a relation on "dim" expressing i_[0..pos] >>= o_[0..pos]
4565 __isl_give isl_basic_map
*isl_basic_map_more_or_equal_at(
4566 __isl_take isl_space
*dim
, unsigned pos
)
4569 isl_basic_map
*bmap
;
4571 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4572 for (i
= 0; i
< pos
; ++i
)
4573 bmap
= var_equal(bmap
, i
);
4574 bmap
= var_more_or_equal(bmap
, pos
);
4575 return isl_basic_map_finalize(bmap
);
4578 static __isl_give isl_map
*map_lex_lte_first(__isl_take isl_space
*dims
,
4579 unsigned n
, int equal
)
4581 struct isl_map
*map
;
4584 if (n
== 0 && equal
)
4585 return isl_map_universe(dims
);
4587 map
= isl_map_alloc_space(isl_space_copy(dims
), n
, ISL_MAP_DISJOINT
);
4589 for (i
= 0; i
+ 1 < n
; ++i
)
4590 map
= isl_map_add_basic_map(map
,
4591 isl_basic_map_less_at(isl_space_copy(dims
), i
));
4594 map
= isl_map_add_basic_map(map
,
4595 isl_basic_map_less_or_equal_at(dims
, n
- 1));
4597 map
= isl_map_add_basic_map(map
,
4598 isl_basic_map_less_at(dims
, n
- 1));
4600 isl_space_free(dims
);
4605 static __isl_give isl_map
*map_lex_lte(__isl_take isl_space
*dims
, int equal
)
4609 return map_lex_lte_first(dims
, dims
->n_out
, equal
);
4612 __isl_give isl_map
*isl_map_lex_lt_first(__isl_take isl_space
*dim
, unsigned n
)
4614 return map_lex_lte_first(dim
, n
, 0);
4617 __isl_give isl_map
*isl_map_lex_le_first(__isl_take isl_space
*dim
, unsigned n
)
4619 return map_lex_lte_first(dim
, n
, 1);
4622 __isl_give isl_map
*isl_map_lex_lt(__isl_take isl_space
*set_dim
)
4624 return map_lex_lte(isl_space_map_from_set(set_dim
), 0);
4627 __isl_give isl_map
*isl_map_lex_le(__isl_take isl_space
*set_dim
)
4629 return map_lex_lte(isl_space_map_from_set(set_dim
), 1);
4632 static __isl_give isl_map
*map_lex_gte_first(__isl_take isl_space
*dims
,
4633 unsigned n
, int equal
)
4635 struct isl_map
*map
;
4638 if (n
== 0 && equal
)
4639 return isl_map_universe(dims
);
4641 map
= isl_map_alloc_space(isl_space_copy(dims
), n
, ISL_MAP_DISJOINT
);
4643 for (i
= 0; i
+ 1 < n
; ++i
)
4644 map
= isl_map_add_basic_map(map
,
4645 isl_basic_map_more_at(isl_space_copy(dims
), i
));
4648 map
= isl_map_add_basic_map(map
,
4649 isl_basic_map_more_or_equal_at(dims
, n
- 1));
4651 map
= isl_map_add_basic_map(map
,
4652 isl_basic_map_more_at(dims
, n
- 1));
4654 isl_space_free(dims
);
4659 static __isl_give isl_map
*map_lex_gte(__isl_take isl_space
*dims
, int equal
)
4663 return map_lex_gte_first(dims
, dims
->n_out
, equal
);
4666 __isl_give isl_map
*isl_map_lex_gt_first(__isl_take isl_space
*dim
, unsigned n
)
4668 return map_lex_gte_first(dim
, n
, 0);
4671 __isl_give isl_map
*isl_map_lex_ge_first(__isl_take isl_space
*dim
, unsigned n
)
4673 return map_lex_gte_first(dim
, n
, 1);
4676 __isl_give isl_map
*isl_map_lex_gt(__isl_take isl_space
*set_dim
)
4678 return map_lex_gte(isl_space_map_from_set(set_dim
), 0);
4681 __isl_give isl_map
*isl_map_lex_ge(__isl_take isl_space
*set_dim
)
4683 return map_lex_gte(isl_space_map_from_set(set_dim
), 1);
4686 __isl_give isl_map
*isl_set_lex_le_set(__isl_take isl_set
*set1
,
4687 __isl_take isl_set
*set2
)
4690 map
= isl_map_lex_le(isl_set_get_space(set1
));
4691 map
= isl_map_intersect_domain(map
, set1
);
4692 map
= isl_map_intersect_range(map
, set2
);
4696 __isl_give isl_map
*isl_set_lex_lt_set(__isl_take isl_set
*set1
,
4697 __isl_take isl_set
*set2
)
4700 map
= isl_map_lex_lt(isl_set_get_space(set1
));
4701 map
= isl_map_intersect_domain(map
, set1
);
4702 map
= isl_map_intersect_range(map
, set2
);
4706 __isl_give isl_map
*isl_set_lex_ge_set(__isl_take isl_set
*set1
,
4707 __isl_take isl_set
*set2
)
4710 map
= isl_map_lex_ge(isl_set_get_space(set1
));
4711 map
= isl_map_intersect_domain(map
, set1
);
4712 map
= isl_map_intersect_range(map
, set2
);
4716 __isl_give isl_map
*isl_set_lex_gt_set(__isl_take isl_set
*set1
,
4717 __isl_take isl_set
*set2
)
4720 map
= isl_map_lex_gt(isl_set_get_space(set1
));
4721 map
= isl_map_intersect_domain(map
, set1
);
4722 map
= isl_map_intersect_range(map
, set2
);
4726 __isl_give isl_map
*isl_map_lex_le_map(__isl_take isl_map
*map1
,
4727 __isl_take isl_map
*map2
)
4730 map
= isl_map_lex_le(isl_space_range(isl_map_get_space(map1
)));
4731 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4732 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4736 __isl_give isl_map
*isl_map_lex_lt_map(__isl_take isl_map
*map1
,
4737 __isl_take isl_map
*map2
)
4740 map
= isl_map_lex_lt(isl_space_range(isl_map_get_space(map1
)));
4741 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4742 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4746 __isl_give isl_map
*isl_map_lex_ge_map(__isl_take isl_map
*map1
,
4747 __isl_take isl_map
*map2
)
4750 map
= isl_map_lex_ge(isl_space_range(isl_map_get_space(map1
)));
4751 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4752 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4756 __isl_give isl_map
*isl_map_lex_gt_map(__isl_take isl_map
*map1
,
4757 __isl_take isl_map
*map2
)
4760 map
= isl_map_lex_gt(isl_space_range(isl_map_get_space(map1
)));
4761 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4762 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4766 static __isl_give isl_basic_map
*basic_map_from_basic_set(
4767 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*dim
)
4769 struct isl_basic_map
*bmap
;
4771 bset
= isl_basic_set_cow(bset
);
4775 isl_assert(bset
->ctx
, isl_space_compatible_internal(bset
->dim
, dim
),
4777 isl_space_free(bset
->dim
);
4778 bmap
= bset_to_bmap(bset
);
4780 return isl_basic_map_finalize(bmap
);
4782 isl_basic_set_free(bset
);
4783 isl_space_free(dim
);
4787 __isl_give isl_basic_map
*isl_basic_map_from_basic_set(
4788 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*space
)
4790 return basic_map_from_basic_set(bset
, space
);
4793 /* For a div d = floor(f/m), add the constraint
4797 static int add_upper_div_constraint(__isl_keep isl_basic_map
*bmap
,
4798 unsigned pos
, isl_int
*div
)
4801 unsigned total
= isl_basic_map_total_dim(bmap
);
4803 i
= isl_basic_map_alloc_inequality(bmap
);
4806 isl_seq_cpy(bmap
->ineq
[i
], div
+ 1, 1 + total
);
4807 isl_int_neg(bmap
->ineq
[i
][1 + pos
], div
[0]);
4812 /* For a div d = floor(f/m), add the constraint
4814 * -(f-(m-1)) + m d >= 0
4816 static int add_lower_div_constraint(__isl_keep isl_basic_map
*bmap
,
4817 unsigned pos
, isl_int
*div
)
4820 unsigned total
= isl_basic_map_total_dim(bmap
);
4822 i
= isl_basic_map_alloc_inequality(bmap
);
4825 isl_seq_neg(bmap
->ineq
[i
], div
+ 1, 1 + total
);
4826 isl_int_set(bmap
->ineq
[i
][1 + pos
], div
[0]);
4827 isl_int_add(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], bmap
->ineq
[i
][1 + pos
]);
4828 isl_int_sub_ui(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], 1);
4833 /* For a div d = floor(f/m), add the constraints
4836 * -(f-(m-1)) + m d >= 0
4838 * Note that the second constraint is the negation of
4842 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map
*bmap
,
4843 unsigned pos
, isl_int
*div
)
4845 if (add_upper_div_constraint(bmap
, pos
, div
) < 0)
4847 if (add_lower_div_constraint(bmap
, pos
, div
) < 0)
4852 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set
*bset
,
4853 unsigned pos
, isl_int
*div
)
4855 return isl_basic_map_add_div_constraints_var(bset_to_bmap(bset
),
4859 int isl_basic_map_add_div_constraints(struct isl_basic_map
*bmap
, unsigned div
)
4861 unsigned total
= isl_basic_map_total_dim(bmap
);
4862 unsigned div_pos
= total
- bmap
->n_div
+ div
;
4864 return isl_basic_map_add_div_constraints_var(bmap
, div_pos
,
4868 /* For each known div d = floor(f/m), add the constraints
4871 * -(f-(m-1)) + m d >= 0
4873 * Remove duplicate constraints in case of some these div constraints
4874 * already appear in "bmap".
4876 __isl_give isl_basic_map
*isl_basic_map_add_known_div_constraints(
4877 __isl_take isl_basic_map
*bmap
)
4883 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4887 bmap
= add_known_div_constraints(bmap
);
4888 bmap
= isl_basic_map_remove_duplicate_constraints(bmap
, NULL
, 0);
4889 bmap
= isl_basic_map_finalize(bmap
);
4893 /* Add the div constraint of sign "sign" for div "div" of "bmap".
4895 * In particular, if this div is of the form d = floor(f/m),
4896 * then add the constraint
4900 * if sign < 0 or the constraint
4902 * -(f-(m-1)) + m d >= 0
4906 int isl_basic_map_add_div_constraint(__isl_keep isl_basic_map
*bmap
,
4907 unsigned div
, int sign
)
4915 total
= isl_basic_map_total_dim(bmap
);
4916 div_pos
= total
- bmap
->n_div
+ div
;
4919 return add_upper_div_constraint(bmap
, div_pos
, bmap
->div
[div
]);
4921 return add_lower_div_constraint(bmap
, div_pos
, bmap
->div
[div
]);
4924 int isl_basic_set_add_div_constraints(struct isl_basic_set
*bset
, unsigned div
)
4926 return isl_basic_map_add_div_constraints(bset
, div
);
4929 struct isl_basic_set
*isl_basic_map_underlying_set(
4930 struct isl_basic_map
*bmap
)
4934 if (bmap
->dim
->nparam
== 0 && bmap
->dim
->n_in
== 0 &&
4936 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_in
) &&
4937 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_out
))
4938 return bset_from_bmap(bmap
);
4939 bmap
= isl_basic_map_cow(bmap
);
4942 bmap
->dim
= isl_space_underlying(bmap
->dim
, bmap
->n_div
);
4945 bmap
->extra
-= bmap
->n_div
;
4947 bmap
= isl_basic_map_finalize(bmap
);
4948 return bset_from_bmap(bmap
);
4950 isl_basic_map_free(bmap
);
4954 __isl_give isl_basic_set
*isl_basic_set_underlying_set(
4955 __isl_take isl_basic_set
*bset
)
4957 return isl_basic_map_underlying_set(bset_to_bmap(bset
));
4960 /* Replace each element in "list" by the result of applying
4961 * isl_basic_map_underlying_set to the element.
4963 __isl_give isl_basic_set_list
*isl_basic_map_list_underlying_set(
4964 __isl_take isl_basic_map_list
*list
)
4971 n
= isl_basic_map_list_n_basic_map(list
);
4972 for (i
= 0; i
< n
; ++i
) {
4973 isl_basic_map
*bmap
;
4974 isl_basic_set
*bset
;
4976 bmap
= isl_basic_map_list_get_basic_map(list
, i
);
4977 bset
= isl_basic_set_underlying_set(bmap
);
4978 list
= isl_basic_set_list_set_basic_set(list
, i
, bset
);
4984 struct isl_basic_map
*isl_basic_map_overlying_set(
4985 struct isl_basic_set
*bset
, struct isl_basic_map
*like
)
4987 struct isl_basic_map
*bmap
;
4988 struct isl_ctx
*ctx
;
4995 isl_assert(ctx
, bset
->n_div
== 0, goto error
);
4996 isl_assert(ctx
, isl_basic_set_n_param(bset
) == 0, goto error
);
4997 isl_assert(ctx
, bset
->dim
->n_out
== isl_basic_map_total_dim(like
),
4999 if (like
->n_div
== 0) {
5000 isl_space
*space
= isl_basic_map_get_space(like
);
5001 isl_basic_map_free(like
);
5002 return isl_basic_map_reset_space(bset
, space
);
5004 bset
= isl_basic_set_cow(bset
);
5007 total
= bset
->dim
->n_out
+ bset
->extra
;
5008 bmap
= bset_to_bmap(bset
);
5009 isl_space_free(bmap
->dim
);
5010 bmap
->dim
= isl_space_copy(like
->dim
);
5013 bmap
->n_div
= like
->n_div
;
5014 bmap
->extra
+= like
->n_div
;
5018 ltotal
= total
- bmap
->extra
+ like
->extra
;
5021 bmap
->block2
= isl_blk_extend(ctx
, bmap
->block2
,
5022 bmap
->extra
* (1 + 1 + total
));
5023 if (isl_blk_is_error(bmap
->block2
))
5025 div
= isl_realloc_array(ctx
, bmap
->div
, isl_int
*, bmap
->extra
);
5029 for (i
= 0; i
< bmap
->extra
; ++i
)
5030 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + 1 + total
);
5031 for (i
= 0; i
< like
->n_div
; ++i
) {
5032 isl_seq_cpy(bmap
->div
[i
], like
->div
[i
], 1 + 1 + ltotal
);
5033 isl_seq_clr(bmap
->div
[i
]+1+1+ltotal
, total
- ltotal
);
5035 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
5037 isl_basic_map_free(like
);
5038 bmap
= isl_basic_map_simplify(bmap
);
5039 bmap
= isl_basic_map_finalize(bmap
);
5042 isl_basic_map_free(like
);
5043 isl_basic_set_free(bset
);
5047 struct isl_basic_set
*isl_basic_set_from_underlying_set(
5048 struct isl_basic_set
*bset
, struct isl_basic_set
*like
)
5050 return bset_from_bmap(isl_basic_map_overlying_set(bset
,
5051 bset_to_bmap(like
)));
5054 struct isl_set
*isl_set_from_underlying_set(
5055 struct isl_set
*set
, struct isl_basic_set
*like
)
5061 isl_assert(set
->ctx
, set
->dim
->n_out
== isl_basic_set_total_dim(like
),
5063 if (isl_space_is_equal(set
->dim
, like
->dim
) && like
->n_div
== 0) {
5064 isl_basic_set_free(like
);
5067 set
= isl_set_cow(set
);
5070 for (i
= 0; i
< set
->n
; ++i
) {
5071 set
->p
[i
] = isl_basic_set_from_underlying_set(set
->p
[i
],
5072 isl_basic_set_copy(like
));
5076 isl_space_free(set
->dim
);
5077 set
->dim
= isl_space_copy(like
->dim
);
5080 isl_basic_set_free(like
);
5083 isl_basic_set_free(like
);
5088 struct isl_set
*isl_map_underlying_set(struct isl_map
*map
)
5092 map
= isl_map_cow(map
);
5095 map
->dim
= isl_space_cow(map
->dim
);
5099 for (i
= 1; i
< map
->n
; ++i
)
5100 isl_assert(map
->ctx
, map
->p
[0]->n_div
== map
->p
[i
]->n_div
,
5102 for (i
= 0; i
< map
->n
; ++i
) {
5103 map
->p
[i
] = bset_to_bmap(
5104 isl_basic_map_underlying_set(map
->p
[i
]));
5109 map
->dim
= isl_space_underlying(map
->dim
, 0);
5111 isl_space_free(map
->dim
);
5112 map
->dim
= isl_space_copy(map
->p
[0]->dim
);
5116 return set_from_map(map
);
5122 struct isl_set
*isl_set_to_underlying_set(struct isl_set
*set
)
5124 return set_from_map(isl_map_underlying_set(set_to_map(set
)));
5127 /* Replace the space of "bmap" by "space".
5129 * If the space of "bmap" is identical to "space" (including the identifiers
5130 * of the input and output dimensions), then simply return the original input.
5132 __isl_give isl_basic_map
*isl_basic_map_reset_space(
5133 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*space
)
5139 equal
= isl_space_is_equal(bmap
->dim
, space
);
5140 if (equal
>= 0 && equal
)
5141 equal
= isl_space_match(bmap
->dim
, isl_dim_in
,
5143 if (equal
>= 0 && equal
)
5144 equal
= isl_space_match(bmap
->dim
, isl_dim_out
,
5145 space
, isl_dim_out
);
5149 isl_space_free(space
);
5152 bmap
= isl_basic_map_cow(bmap
);
5153 if (!bmap
|| !space
)
5156 isl_space_free(bmap
->dim
);
5159 bmap
= isl_basic_map_finalize(bmap
);
5163 isl_basic_map_free(bmap
);
5164 isl_space_free(space
);
5168 __isl_give isl_basic_set
*isl_basic_set_reset_space(
5169 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*dim
)
5171 return bset_from_bmap(isl_basic_map_reset_space(bset_to_bmap(bset
),
5175 __isl_give isl_map
*isl_map_reset_space(__isl_take isl_map
*map
,
5176 __isl_take isl_space
*dim
)
5180 map
= isl_map_cow(map
);
5184 for (i
= 0; i
< map
->n
; ++i
) {
5185 map
->p
[i
] = isl_basic_map_reset_space(map
->p
[i
],
5186 isl_space_copy(dim
));
5190 isl_space_free(map
->dim
);
5196 isl_space_free(dim
);
5200 __isl_give isl_set
*isl_set_reset_space(__isl_take isl_set
*set
,
5201 __isl_take isl_space
*dim
)
5203 return set_from_map(isl_map_reset_space(set_to_map(set
), dim
));
5206 /* Compute the parameter domain of the given basic set.
5208 __isl_give isl_basic_set
*isl_basic_set_params(__isl_take isl_basic_set
*bset
)
5213 if (isl_basic_set_is_params(bset
))
5216 n
= isl_basic_set_dim(bset
, isl_dim_set
);
5217 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
5218 space
= isl_basic_set_get_space(bset
);
5219 space
= isl_space_params(space
);
5220 bset
= isl_basic_set_reset_space(bset
, space
);
5224 /* Construct a zero-dimensional basic set with the given parameter domain.
5226 __isl_give isl_basic_set
*isl_basic_set_from_params(
5227 __isl_take isl_basic_set
*bset
)
5230 space
= isl_basic_set_get_space(bset
);
5231 space
= isl_space_set_from_params(space
);
5232 bset
= isl_basic_set_reset_space(bset
, space
);
5236 /* Compute the parameter domain of the given set.
5238 __isl_give isl_set
*isl_set_params(__isl_take isl_set
*set
)
5243 if (isl_set_is_params(set
))
5246 n
= isl_set_dim(set
, isl_dim_set
);
5247 set
= isl_set_project_out(set
, isl_dim_set
, 0, n
);
5248 space
= isl_set_get_space(set
);
5249 space
= isl_space_params(space
);
5250 set
= isl_set_reset_space(set
, space
);
5254 /* Construct a zero-dimensional set with the given parameter domain.
5256 __isl_give isl_set
*isl_set_from_params(__isl_take isl_set
*set
)
5259 space
= isl_set_get_space(set
);
5260 space
= isl_space_set_from_params(space
);
5261 set
= isl_set_reset_space(set
, space
);
5265 /* Compute the parameter domain of the given map.
5267 __isl_give isl_set
*isl_map_params(__isl_take isl_map
*map
)
5272 n
= isl_map_dim(map
, isl_dim_in
);
5273 map
= isl_map_project_out(map
, isl_dim_in
, 0, n
);
5274 n
= isl_map_dim(map
, isl_dim_out
);
5275 map
= isl_map_project_out(map
, isl_dim_out
, 0, n
);
5276 space
= isl_map_get_space(map
);
5277 space
= isl_space_params(space
);
5278 map
= isl_map_reset_space(map
, space
);
5282 struct isl_basic_set
*isl_basic_map_domain(struct isl_basic_map
*bmap
)
5289 space
= isl_space_domain(isl_basic_map_get_space(bmap
));
5291 n_out
= isl_basic_map_n_out(bmap
);
5292 bmap
= isl_basic_map_project_out(bmap
, isl_dim_out
, 0, n_out
);
5294 return isl_basic_map_reset_space(bmap
, space
);
5297 int isl_basic_map_may_be_set(__isl_keep isl_basic_map
*bmap
)
5301 return isl_space_may_be_set(bmap
->dim
);
5304 /* Is this basic map actually a set?
5305 * Users should never call this function. Outside of isl,
5306 * the type should indicate whether something is a set or a map.
5308 int isl_basic_map_is_set(__isl_keep isl_basic_map
*bmap
)
5312 return isl_space_is_set(bmap
->dim
);
5315 struct isl_basic_set
*isl_basic_map_range(struct isl_basic_map
*bmap
)
5319 if (isl_basic_map_is_set(bmap
))
5321 return isl_basic_map_domain(isl_basic_map_reverse(bmap
));
5324 __isl_give isl_basic_map
*isl_basic_map_domain_map(
5325 __isl_take isl_basic_map
*bmap
)
5329 isl_basic_map
*domain
;
5330 int nparam
, n_in
, n_out
;
5333 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5334 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5335 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5337 dim
= isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap
)));
5338 domain
= isl_basic_map_universe(dim
);
5340 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
5341 bmap
= isl_basic_map_apply_range(bmap
, domain
);
5342 bmap
= isl_basic_map_extend_constraints(bmap
, n_in
, 0);
5344 total
= isl_basic_map_total_dim(bmap
);
5346 for (i
= 0; i
< n_in
; ++i
) {
5347 k
= isl_basic_map_alloc_equality(bmap
);
5350 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
5351 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], -1);
5352 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ n_out
+ i
], 1);
5355 bmap
= isl_basic_map_gauss(bmap
, NULL
);
5356 return isl_basic_map_finalize(bmap
);
5358 isl_basic_map_free(bmap
);
5362 __isl_give isl_basic_map
*isl_basic_map_range_map(
5363 __isl_take isl_basic_map
*bmap
)
5367 isl_basic_map
*range
;
5368 int nparam
, n_in
, n_out
;
5371 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5372 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5373 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5375 dim
= isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap
)));
5376 range
= isl_basic_map_universe(dim
);
5378 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
5379 bmap
= isl_basic_map_apply_range(bmap
, range
);
5380 bmap
= isl_basic_map_extend_constraints(bmap
, n_out
, 0);
5382 total
= isl_basic_map_total_dim(bmap
);
5384 for (i
= 0; i
< n_out
; ++i
) {
5385 k
= isl_basic_map_alloc_equality(bmap
);
5388 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
5389 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ i
], -1);
5390 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ n_out
+ i
], 1);
5393 bmap
= isl_basic_map_gauss(bmap
, NULL
);
5394 return isl_basic_map_finalize(bmap
);
5396 isl_basic_map_free(bmap
);
5400 int isl_map_may_be_set(__isl_keep isl_map
*map
)
5404 return isl_space_may_be_set(map
->dim
);
5407 /* Is this map actually a set?
5408 * Users should never call this function. Outside of isl,
5409 * the type should indicate whether something is a set or a map.
5411 int isl_map_is_set(__isl_keep isl_map
*map
)
5415 return isl_space_is_set(map
->dim
);
5418 struct isl_set
*isl_map_range(struct isl_map
*map
)
5421 struct isl_set
*set
;
5425 if (isl_map_is_set(map
))
5426 return set_from_map(map
);
5428 map
= isl_map_cow(map
);
5432 set
= set_from_map(map
);
5433 set
->dim
= isl_space_range(set
->dim
);
5436 for (i
= 0; i
< map
->n
; ++i
) {
5437 set
->p
[i
] = isl_basic_map_range(map
->p
[i
]);
5441 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
5442 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
5449 __isl_give isl_map
*isl_map_domain_map(__isl_take isl_map
*map
)
5453 map
= isl_map_cow(map
);
5457 map
->dim
= isl_space_domain_map(map
->dim
);
5460 for (i
= 0; i
< map
->n
; ++i
) {
5461 map
->p
[i
] = isl_basic_map_domain_map(map
->p
[i
]);
5465 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
5466 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5473 __isl_give isl_map
*isl_map_range_map(__isl_take isl_map
*map
)
5476 isl_space
*range_dim
;
5478 map
= isl_map_cow(map
);
5482 range_dim
= isl_space_range(isl_map_get_space(map
));
5483 range_dim
= isl_space_from_range(range_dim
);
5484 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
5485 map
->dim
= isl_space_join(map
->dim
, range_dim
);
5488 for (i
= 0; i
< map
->n
; ++i
) {
5489 map
->p
[i
] = isl_basic_map_range_map(map
->p
[i
]);
5493 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
5494 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5501 /* Given a wrapped map of the form A[B -> C],
5502 * return the map A[B -> C] -> B.
5504 __isl_give isl_map
*isl_set_wrapped_domain_map(__isl_take isl_set
*set
)
5511 if (!isl_set_has_tuple_id(set
))
5512 return isl_map_domain_map(isl_set_unwrap(set
));
5514 id
= isl_set_get_tuple_id(set
);
5515 map
= isl_map_domain_map(isl_set_unwrap(set
));
5516 map
= isl_map_set_tuple_id(map
, isl_dim_in
, id
);
5521 __isl_give isl_map
*isl_map_from_set(__isl_take isl_set
*set
,
5522 __isl_take isl_space
*dim
)
5525 struct isl_map
*map
= NULL
;
5527 set
= isl_set_cow(set
);
5530 isl_assert(set
->ctx
, isl_space_compatible_internal(set
->dim
, dim
),
5532 map
= set_to_map(set
);
5533 for (i
= 0; i
< set
->n
; ++i
) {
5534 map
->p
[i
] = basic_map_from_basic_set(
5535 set
->p
[i
], isl_space_copy(dim
));
5539 isl_space_free(map
->dim
);
5543 isl_space_free(dim
);
5548 __isl_give isl_basic_map
*isl_basic_map_from_domain(
5549 __isl_take isl_basic_set
*bset
)
5551 return isl_basic_map_reverse(isl_basic_map_from_range(bset
));
5554 __isl_give isl_basic_map
*isl_basic_map_from_range(
5555 __isl_take isl_basic_set
*bset
)
5558 space
= isl_basic_set_get_space(bset
);
5559 space
= isl_space_from_range(space
);
5560 bset
= isl_basic_set_reset_space(bset
, space
);
5561 return bset_to_bmap(bset
);
5564 /* Create a relation with the given set as range.
5565 * The domain of the created relation is a zero-dimensional
5566 * flat anonymous space.
5568 __isl_give isl_map
*isl_map_from_range(__isl_take isl_set
*set
)
5571 space
= isl_set_get_space(set
);
5572 space
= isl_space_from_range(space
);
5573 set
= isl_set_reset_space(set
, space
);
5574 return set_to_map(set
);
5577 /* Create a relation with the given set as domain.
5578 * The range of the created relation is a zero-dimensional
5579 * flat anonymous space.
5581 __isl_give isl_map
*isl_map_from_domain(__isl_take isl_set
*set
)
5583 return isl_map_reverse(isl_map_from_range(set
));
5586 __isl_give isl_basic_map
*isl_basic_map_from_domain_and_range(
5587 __isl_take isl_basic_set
*domain
, __isl_take isl_basic_set
*range
)
5589 return isl_basic_map_apply_range(isl_basic_map_reverse(domain
), range
);
5592 __isl_give isl_map
*isl_map_from_domain_and_range(__isl_take isl_set
*domain
,
5593 __isl_take isl_set
*range
)
5595 return isl_map_apply_range(isl_map_reverse(domain
), range
);
5598 /* Return a newly allocated isl_map with given space and flags and
5599 * room for "n" basic maps.
5600 * Make sure that all cached information is cleared.
5602 __isl_give isl_map
*isl_map_alloc_space(__isl_take isl_space
*space
, int n
,
5605 struct isl_map
*map
;
5610 isl_die(space
->ctx
, isl_error_internal
,
5611 "negative number of basic maps", goto error
);
5612 map
= isl_calloc(space
->ctx
, struct isl_map
,
5613 sizeof(struct isl_map
) +
5614 (n
- 1) * sizeof(struct isl_basic_map
*));
5618 map
->ctx
= space
->ctx
;
5619 isl_ctx_ref(map
->ctx
);
5627 isl_space_free(space
);
5631 struct isl_map
*isl_map_alloc(struct isl_ctx
*ctx
,
5632 unsigned nparam
, unsigned in
, unsigned out
, int n
,
5635 struct isl_map
*map
;
5638 dims
= isl_space_alloc(ctx
, nparam
, in
, out
);
5642 map
= isl_map_alloc_space(dims
, n
, flags
);
5646 __isl_give isl_basic_map
*isl_basic_map_empty(__isl_take isl_space
*dim
)
5648 struct isl_basic_map
*bmap
;
5649 bmap
= isl_basic_map_alloc_space(dim
, 0, 1, 0);
5650 bmap
= isl_basic_map_set_to_empty(bmap
);
5654 __isl_give isl_basic_set
*isl_basic_set_empty(__isl_take isl_space
*dim
)
5656 struct isl_basic_set
*bset
;
5657 bset
= isl_basic_set_alloc_space(dim
, 0, 1, 0);
5658 bset
= isl_basic_set_set_to_empty(bset
);
5662 __isl_give isl_basic_map
*isl_basic_map_universe(__isl_take isl_space
*dim
)
5664 struct isl_basic_map
*bmap
;
5665 bmap
= isl_basic_map_alloc_space(dim
, 0, 0, 0);
5666 bmap
= isl_basic_map_finalize(bmap
);
5670 __isl_give isl_basic_set
*isl_basic_set_universe(__isl_take isl_space
*dim
)
5672 struct isl_basic_set
*bset
;
5673 bset
= isl_basic_set_alloc_space(dim
, 0, 0, 0);
5674 bset
= isl_basic_set_finalize(bset
);
5678 __isl_give isl_basic_map
*isl_basic_map_nat_universe(__isl_take isl_space
*dim
)
5681 unsigned total
= isl_space_dim(dim
, isl_dim_all
);
5682 isl_basic_map
*bmap
;
5684 bmap
= isl_basic_map_alloc_space(dim
, 0, 0, total
);
5685 for (i
= 0; i
< total
; ++i
) {
5686 int k
= isl_basic_map_alloc_inequality(bmap
);
5689 isl_seq_clr(bmap
->ineq
[k
], 1 + total
);
5690 isl_int_set_si(bmap
->ineq
[k
][1 + i
], 1);
5694 isl_basic_map_free(bmap
);
5698 __isl_give isl_basic_set
*isl_basic_set_nat_universe(__isl_take isl_space
*dim
)
5700 return isl_basic_map_nat_universe(dim
);
5703 __isl_give isl_map
*isl_map_nat_universe(__isl_take isl_space
*dim
)
5705 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim
));
5708 __isl_give isl_set
*isl_set_nat_universe(__isl_take isl_space
*dim
)
5710 return isl_map_nat_universe(dim
);
5713 __isl_give isl_map
*isl_map_empty(__isl_take isl_space
*dim
)
5715 return isl_map_alloc_space(dim
, 0, ISL_MAP_DISJOINT
);
5718 __isl_give isl_set
*isl_set_empty(__isl_take isl_space
*dim
)
5720 return isl_set_alloc_space(dim
, 0, ISL_MAP_DISJOINT
);
5723 __isl_give isl_map
*isl_map_universe(__isl_take isl_space
*dim
)
5725 struct isl_map
*map
;
5728 map
= isl_map_alloc_space(isl_space_copy(dim
), 1, ISL_MAP_DISJOINT
);
5729 map
= isl_map_add_basic_map(map
, isl_basic_map_universe(dim
));
5733 __isl_give isl_set
*isl_set_universe(__isl_take isl_space
*dim
)
5735 struct isl_set
*set
;
5738 set
= isl_set_alloc_space(isl_space_copy(dim
), 1, ISL_MAP_DISJOINT
);
5739 set
= isl_set_add_basic_set(set
, isl_basic_set_universe(dim
));
5743 struct isl_map
*isl_map_dup(struct isl_map
*map
)
5746 struct isl_map
*dup
;
5750 dup
= isl_map_alloc_space(isl_space_copy(map
->dim
), map
->n
, map
->flags
);
5751 for (i
= 0; i
< map
->n
; ++i
)
5752 dup
= isl_map_add_basic_map(dup
, isl_basic_map_copy(map
->p
[i
]));
5756 __isl_give isl_map
*isl_map_add_basic_map(__isl_take isl_map
*map
,
5757 __isl_take isl_basic_map
*bmap
)
5761 if (isl_basic_map_plain_is_empty(bmap
)) {
5762 isl_basic_map_free(bmap
);
5765 isl_assert(map
->ctx
, isl_space_is_equal(map
->dim
, bmap
->dim
), goto error
);
5766 isl_assert(map
->ctx
, map
->n
< map
->size
, goto error
);
5767 map
->p
[map
->n
] = bmap
;
5769 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5775 isl_basic_map_free(bmap
);
5779 __isl_null isl_map
*isl_map_free(__isl_take isl_map
*map
)
5790 isl_ctx_deref(map
->ctx
);
5791 for (i
= 0; i
< map
->n
; ++i
)
5792 isl_basic_map_free(map
->p
[i
]);
5793 isl_space_free(map
->dim
);
5799 static struct isl_basic_map
*isl_basic_map_fix_pos_si(
5800 struct isl_basic_map
*bmap
, unsigned pos
, int value
)
5804 bmap
= isl_basic_map_cow(bmap
);
5805 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
5806 j
= isl_basic_map_alloc_equality(bmap
);
5809 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
5810 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
5811 isl_int_set_si(bmap
->eq
[j
][0], value
);
5812 bmap
= isl_basic_map_simplify(bmap
);
5813 return isl_basic_map_finalize(bmap
);
5815 isl_basic_map_free(bmap
);
5819 static __isl_give isl_basic_map
*isl_basic_map_fix_pos(
5820 __isl_take isl_basic_map
*bmap
, unsigned pos
, isl_int value
)
5824 bmap
= isl_basic_map_cow(bmap
);
5825 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
5826 j
= isl_basic_map_alloc_equality(bmap
);
5829 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
5830 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
5831 isl_int_set(bmap
->eq
[j
][0], value
);
5832 bmap
= isl_basic_map_simplify(bmap
);
5833 return isl_basic_map_finalize(bmap
);
5835 isl_basic_map_free(bmap
);
5839 struct isl_basic_map
*isl_basic_map_fix_si(struct isl_basic_map
*bmap
,
5840 enum isl_dim_type type
, unsigned pos
, int value
)
5842 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
5843 return isl_basic_map_free(bmap
);
5844 return isl_basic_map_fix_pos_si(bmap
,
5845 isl_basic_map_offset(bmap
, type
) + pos
, value
);
5848 __isl_give isl_basic_map
*isl_basic_map_fix(__isl_take isl_basic_map
*bmap
,
5849 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5851 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
5852 return isl_basic_map_free(bmap
);
5853 return isl_basic_map_fix_pos(bmap
,
5854 isl_basic_map_offset(bmap
, type
) + pos
, value
);
5857 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
5858 * to be equal to "v".
5860 __isl_give isl_basic_map
*isl_basic_map_fix_val(__isl_take isl_basic_map
*bmap
,
5861 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5865 if (!isl_val_is_int(v
))
5866 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
5867 "expecting integer value", goto error
);
5868 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
5870 pos
+= isl_basic_map_offset(bmap
, type
);
5871 bmap
= isl_basic_map_fix_pos(bmap
, pos
, v
->n
);
5875 isl_basic_map_free(bmap
);
5880 /* Fix the value of the variable at position "pos" of type "type" of "bset"
5881 * to be equal to "v".
5883 __isl_give isl_basic_set
*isl_basic_set_fix_val(__isl_take isl_basic_set
*bset
,
5884 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5886 return isl_basic_map_fix_val(bset
, type
, pos
, v
);
5889 struct isl_basic_set
*isl_basic_set_fix_si(struct isl_basic_set
*bset
,
5890 enum isl_dim_type type
, unsigned pos
, int value
)
5892 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset
),
5896 __isl_give isl_basic_set
*isl_basic_set_fix(__isl_take isl_basic_set
*bset
,
5897 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5899 return bset_from_bmap(isl_basic_map_fix(bset_to_bmap(bset
),
5903 struct isl_basic_map
*isl_basic_map_fix_input_si(struct isl_basic_map
*bmap
,
5904 unsigned input
, int value
)
5906 return isl_basic_map_fix_si(bmap
, isl_dim_in
, input
, value
);
5909 struct isl_basic_set
*isl_basic_set_fix_dim_si(struct isl_basic_set
*bset
,
5910 unsigned dim
, int value
)
5912 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset
),
5913 isl_dim_set
, dim
, value
));
5916 static int remove_if_empty(__isl_keep isl_map
*map
, int i
)
5918 int empty
= isl_basic_map_plain_is_empty(map
->p
[i
]);
5925 isl_basic_map_free(map
->p
[i
]);
5926 if (i
!= map
->n
- 1) {
5927 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5928 map
->p
[i
] = map
->p
[map
->n
- 1];
5935 /* Perform "fn" on each basic map of "map", where we may not be holding
5936 * the only reference to "map".
5937 * In particular, "fn" should be a semantics preserving operation
5938 * that we want to apply to all copies of "map". We therefore need
5939 * to be careful not to modify "map" in a way that breaks "map"
5940 * in case anything goes wrong.
5942 __isl_give isl_map
*isl_map_inline_foreach_basic_map(__isl_take isl_map
*map
,
5943 __isl_give isl_basic_map
*(*fn
)(__isl_take isl_basic_map
*bmap
))
5945 struct isl_basic_map
*bmap
;
5951 for (i
= map
->n
- 1; i
>= 0; --i
) {
5952 bmap
= isl_basic_map_copy(map
->p
[i
]);
5956 isl_basic_map_free(map
->p
[i
]);
5958 if (remove_if_empty(map
, i
) < 0)
5968 struct isl_map
*isl_map_fix_si(struct isl_map
*map
,
5969 enum isl_dim_type type
, unsigned pos
, int value
)
5973 map
= isl_map_cow(map
);
5977 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
5978 for (i
= map
->n
- 1; i
>= 0; --i
) {
5979 map
->p
[i
] = isl_basic_map_fix_si(map
->p
[i
], type
, pos
, value
);
5980 if (remove_if_empty(map
, i
) < 0)
5983 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5990 __isl_give isl_set
*isl_set_fix_si(__isl_take isl_set
*set
,
5991 enum isl_dim_type type
, unsigned pos
, int value
)
5993 return set_from_map(isl_map_fix_si(set_to_map(set
), type
, pos
, value
));
5996 __isl_give isl_map
*isl_map_fix(__isl_take isl_map
*map
,
5997 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6001 map
= isl_map_cow(map
);
6005 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
6006 for (i
= 0; i
< map
->n
; ++i
) {
6007 map
->p
[i
] = isl_basic_map_fix(map
->p
[i
], type
, pos
, value
);
6011 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6018 __isl_give isl_set
*isl_set_fix(__isl_take isl_set
*set
,
6019 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6021 return set_from_map(isl_map_fix(set_to_map(set
), type
, pos
, value
));
6024 /* Fix the value of the variable at position "pos" of type "type" of "map"
6025 * to be equal to "v".
6027 __isl_give isl_map
*isl_map_fix_val(__isl_take isl_map
*map
,
6028 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6032 map
= isl_map_cow(map
);
6036 if (!isl_val_is_int(v
))
6037 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
6038 "expecting integer value", goto error
);
6039 if (pos
>= isl_map_dim(map
, type
))
6040 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
6041 "index out of bounds", goto error
);
6042 for (i
= map
->n
- 1; i
>= 0; --i
) {
6043 map
->p
[i
] = isl_basic_map_fix_val(map
->p
[i
], type
, pos
,
6045 if (remove_if_empty(map
, i
) < 0)
6048 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6057 /* Fix the value of the variable at position "pos" of type "type" of "set"
6058 * to be equal to "v".
6060 __isl_give isl_set
*isl_set_fix_val(__isl_take isl_set
*set
,
6061 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6063 return isl_map_fix_val(set
, type
, pos
, v
);
6066 struct isl_map
*isl_map_fix_input_si(struct isl_map
*map
,
6067 unsigned input
, int value
)
6069 return isl_map_fix_si(map
, isl_dim_in
, input
, value
);
6072 struct isl_set
*isl_set_fix_dim_si(struct isl_set
*set
, unsigned dim
, int value
)
6074 return set_from_map(isl_map_fix_si(set_to_map(set
),
6075 isl_dim_set
, dim
, value
));
6078 static __isl_give isl_basic_map
*basic_map_bound_si(
6079 __isl_take isl_basic_map
*bmap
,
6080 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
6084 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6085 return isl_basic_map_free(bmap
);
6086 pos
+= isl_basic_map_offset(bmap
, type
);
6087 bmap
= isl_basic_map_cow(bmap
);
6088 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
6089 j
= isl_basic_map_alloc_inequality(bmap
);
6092 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
6094 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
6095 isl_int_set_si(bmap
->ineq
[j
][0], value
);
6097 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
6098 isl_int_set_si(bmap
->ineq
[j
][0], -value
);
6100 bmap
= isl_basic_map_simplify(bmap
);
6101 return isl_basic_map_finalize(bmap
);
6103 isl_basic_map_free(bmap
);
6107 __isl_give isl_basic_map
*isl_basic_map_lower_bound_si(
6108 __isl_take isl_basic_map
*bmap
,
6109 enum isl_dim_type type
, unsigned pos
, int value
)
6111 return basic_map_bound_si(bmap
, type
, pos
, value
, 0);
6114 /* Constrain the values of the given dimension to be no greater than "value".
6116 __isl_give isl_basic_map
*isl_basic_map_upper_bound_si(
6117 __isl_take isl_basic_map
*bmap
,
6118 enum isl_dim_type type
, unsigned pos
, int value
)
6120 return basic_map_bound_si(bmap
, type
, pos
, value
, 1);
6123 struct isl_basic_set
*isl_basic_set_lower_bound_dim(struct isl_basic_set
*bset
,
6124 unsigned dim
, isl_int value
)
6128 bset
= isl_basic_set_cow(bset
);
6129 bset
= isl_basic_set_extend_constraints(bset
, 0, 1);
6130 j
= isl_basic_set_alloc_inequality(bset
);
6133 isl_seq_clr(bset
->ineq
[j
], 1 + isl_basic_set_total_dim(bset
));
6134 isl_int_set_si(bset
->ineq
[j
][1 + isl_basic_set_n_param(bset
) + dim
], 1);
6135 isl_int_neg(bset
->ineq
[j
][0], value
);
6136 bset
= isl_basic_set_simplify(bset
);
6137 return isl_basic_set_finalize(bset
);
6139 isl_basic_set_free(bset
);
6143 static __isl_give isl_map
*map_bound_si(__isl_take isl_map
*map
,
6144 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
6148 map
= isl_map_cow(map
);
6152 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
6153 for (i
= 0; i
< map
->n
; ++i
) {
6154 map
->p
[i
] = basic_map_bound_si(map
->p
[i
],
6155 type
, pos
, value
, upper
);
6159 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6166 __isl_give isl_map
*isl_map_lower_bound_si(__isl_take isl_map
*map
,
6167 enum isl_dim_type type
, unsigned pos
, int value
)
6169 return map_bound_si(map
, type
, pos
, value
, 0);
6172 __isl_give isl_map
*isl_map_upper_bound_si(__isl_take isl_map
*map
,
6173 enum isl_dim_type type
, unsigned pos
, int value
)
6175 return map_bound_si(map
, type
, pos
, value
, 1);
6178 __isl_give isl_set
*isl_set_lower_bound_si(__isl_take isl_set
*set
,
6179 enum isl_dim_type type
, unsigned pos
, int value
)
6181 return set_from_map(isl_map_lower_bound_si(set_to_map(set
),
6185 __isl_give isl_set
*isl_set_upper_bound_si(__isl_take isl_set
*set
,
6186 enum isl_dim_type type
, unsigned pos
, int value
)
6188 return isl_map_upper_bound_si(set
, type
, pos
, value
);
6191 /* Bound the given variable of "bmap" from below (or above is "upper"
6192 * is set) to "value".
6194 static __isl_give isl_basic_map
*basic_map_bound(
6195 __isl_take isl_basic_map
*bmap
,
6196 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
6200 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6201 return isl_basic_map_free(bmap
);
6202 pos
+= isl_basic_map_offset(bmap
, type
);
6203 bmap
= isl_basic_map_cow(bmap
);
6204 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
6205 j
= isl_basic_map_alloc_inequality(bmap
);
6208 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
6210 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
6211 isl_int_set(bmap
->ineq
[j
][0], value
);
6213 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
6214 isl_int_neg(bmap
->ineq
[j
][0], value
);
6216 bmap
= isl_basic_map_simplify(bmap
);
6217 return isl_basic_map_finalize(bmap
);
6219 isl_basic_map_free(bmap
);
6223 /* Bound the given variable of "map" from below (or above is "upper"
6224 * is set) to "value".
6226 static __isl_give isl_map
*map_bound(__isl_take isl_map
*map
,
6227 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
6231 map
= isl_map_cow(map
);
6235 if (pos
>= isl_map_dim(map
, type
))
6236 isl_die(map
->ctx
, isl_error_invalid
,
6237 "index out of bounds", goto error
);
6238 for (i
= map
->n
- 1; i
>= 0; --i
) {
6239 map
->p
[i
] = basic_map_bound(map
->p
[i
], type
, pos
, value
, upper
);
6240 if (remove_if_empty(map
, i
) < 0)
6243 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6250 __isl_give isl_map
*isl_map_lower_bound(__isl_take isl_map
*map
,
6251 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6253 return map_bound(map
, type
, pos
, value
, 0);
6256 __isl_give isl_map
*isl_map_upper_bound(__isl_take isl_map
*map
,
6257 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6259 return map_bound(map
, type
, pos
, value
, 1);
6262 __isl_give isl_set
*isl_set_lower_bound(__isl_take isl_set
*set
,
6263 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6265 return isl_map_lower_bound(set
, type
, pos
, value
);
6268 __isl_give isl_set
*isl_set_upper_bound(__isl_take isl_set
*set
,
6269 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6271 return isl_map_upper_bound(set
, type
, pos
, value
);
6274 /* Force the values of the variable at position "pos" of type "type" of "set"
6275 * to be no smaller than "value".
6277 __isl_give isl_set
*isl_set_lower_bound_val(__isl_take isl_set
*set
,
6278 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
6282 if (!isl_val_is_int(value
))
6283 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
6284 "expecting integer value", goto error
);
6285 set
= isl_set_lower_bound(set
, type
, pos
, value
->n
);
6286 isl_val_free(value
);
6289 isl_val_free(value
);
6294 /* Force the values of the variable at position "pos" of type "type" of "set"
6295 * to be no greater than "value".
6297 __isl_give isl_set
*isl_set_upper_bound_val(__isl_take isl_set
*set
,
6298 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
6302 if (!isl_val_is_int(value
))
6303 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
6304 "expecting integer value", goto error
);
6305 set
= isl_set_upper_bound(set
, type
, pos
, value
->n
);
6306 isl_val_free(value
);
6309 isl_val_free(value
);
6314 struct isl_set
*isl_set_lower_bound_dim(struct isl_set
*set
, unsigned dim
,
6319 set
= isl_set_cow(set
);
6323 isl_assert(set
->ctx
, dim
< isl_set_n_dim(set
), goto error
);
6324 for (i
= 0; i
< set
->n
; ++i
) {
6325 set
->p
[i
] = isl_basic_set_lower_bound_dim(set
->p
[i
], dim
, value
);
6335 struct isl_map
*isl_map_reverse(struct isl_map
*map
)
6339 map
= isl_map_cow(map
);
6343 map
->dim
= isl_space_reverse(map
->dim
);
6346 for (i
= 0; i
< map
->n
; ++i
) {
6347 map
->p
[i
] = isl_basic_map_reverse(map
->p
[i
]);
6351 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6359 #define TYPE isl_pw_multi_aff
6361 #define SUFFIX _pw_multi_aff
6363 #define EMPTY isl_pw_multi_aff_empty
6365 #define ADD isl_pw_multi_aff_union_add
6366 #include "isl_map_lexopt_templ.c"
6368 /* Given a map "map", compute the lexicographically minimal
6369 * (or maximal) image element for each domain element in dom,
6370 * in the form of an isl_pw_multi_aff.
6371 * If "empty" is not NULL, then set *empty to those elements in dom that
6372 * do not have an image element.
6373 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6374 * should be computed over the domain of "map". "empty" is also NULL
6377 * We first compute the lexicographically minimal or maximal element
6378 * in the first basic map. This results in a partial solution "res"
6379 * and a subset "todo" of dom that still need to be handled.
6380 * We then consider each of the remaining maps in "map" and successively
6381 * update both "res" and "todo".
6382 * If "empty" is NULL, then the todo sets are not needed and therefore
6383 * also not computed.
6385 static __isl_give isl_pw_multi_aff
*isl_map_partial_lexopt_aligned_pw_multi_aff(
6386 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6387 __isl_give isl_set
**empty
, unsigned flags
)
6391 isl_pw_multi_aff
*res
;
6394 full
= ISL_FL_ISSET(flags
, ISL_OPT_FULL
);
6395 if (!map
|| (!full
&& !dom
))
6398 if (isl_map_plain_is_empty(map
)) {
6403 return isl_pw_multi_aff_from_map(map
);
6406 res
= basic_map_partial_lexopt_pw_multi_aff(
6407 isl_basic_map_copy(map
->p
[0]),
6408 isl_set_copy(dom
), empty
, flags
);
6412 for (i
= 1; i
< map
->n
; ++i
) {
6413 isl_pw_multi_aff
*res_i
;
6415 res_i
= basic_map_partial_lexopt_pw_multi_aff(
6416 isl_basic_map_copy(map
->p
[i
]),
6417 isl_set_copy(dom
), empty
, flags
);
6419 if (ISL_FL_ISSET(flags
, ISL_OPT_MAX
))
6420 res
= isl_pw_multi_aff_union_lexmax(res
, res_i
);
6422 res
= isl_pw_multi_aff_union_lexmin(res
, res_i
);
6425 todo
= isl_set_intersect(todo
, *empty
);
6444 #define TYPE isl_map
6448 #define EMPTY isl_map_empty
6450 #define ADD isl_map_union_disjoint
6451 #include "isl_map_lexopt_templ.c"
6453 /* Given a map "map", compute the lexicographically minimal
6454 * (or maximal) image element for each domain element in "dom",
6455 * in the form of an isl_map.
6456 * If "empty" is not NULL, then set *empty to those elements in "dom" that
6457 * do not have an image element.
6458 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6459 * should be computed over the domain of "map". "empty" is also NULL
6462 * If the input consists of more than one disjunct, then first
6463 * compute the desired result in the form of an isl_pw_multi_aff and
6464 * then convert that into an isl_map.
6466 * This function used to have an explicit implementation in terms
6467 * of isl_maps, but it would continually intersect the domains of
6468 * partial results with the complement of the domain of the next
6469 * partial solution, potentially leading to an explosion in the number
6470 * of disjuncts if there are several disjuncts in the input.
6471 * An even earlier implementation of this function would look for
6472 * better results in the domain of the partial result and for extra
6473 * results in the complement of this domain, which would lead to
6474 * even more splintering.
6476 static __isl_give isl_map
*isl_map_partial_lexopt_aligned(
6477 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6478 __isl_give isl_set
**empty
, unsigned flags
)
6481 struct isl_map
*res
;
6482 isl_pw_multi_aff
*pma
;
6484 full
= ISL_FL_ISSET(flags
, ISL_OPT_FULL
);
6485 if (!map
|| (!full
&& !dom
))
6488 if (isl_map_plain_is_empty(map
)) {
6497 res
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[0]),
6503 pma
= isl_map_partial_lexopt_aligned_pw_multi_aff(map
, dom
, empty
,
6505 return isl_map_from_pw_multi_aff(pma
);
6514 __isl_give isl_map
*isl_map_partial_lexmax(
6515 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6516 __isl_give isl_set
**empty
)
6518 return isl_map_partial_lexopt(map
, dom
, empty
, ISL_OPT_MAX
);
6521 __isl_give isl_map
*isl_map_partial_lexmin(
6522 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6523 __isl_give isl_set
**empty
)
6525 return isl_map_partial_lexopt(map
, dom
, empty
, 0);
6528 __isl_give isl_set
*isl_set_partial_lexmin(
6529 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
6530 __isl_give isl_set
**empty
)
6532 return set_from_map(isl_map_partial_lexmin(set_to_map(set
),
6536 __isl_give isl_set
*isl_set_partial_lexmax(
6537 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
6538 __isl_give isl_set
**empty
)
6540 return set_from_map(isl_map_partial_lexmax(set_to_map(set
),
6544 /* Compute the lexicographic minimum (or maximum if "flags" includes
6545 * ISL_OPT_MAX) of "bset" over its parametric domain.
6547 __isl_give isl_set
*isl_basic_set_lexopt(__isl_take isl_basic_set
*bset
,
6550 return isl_basic_map_lexopt(bset
, flags
);
6553 __isl_give isl_map
*isl_basic_map_lexmax(__isl_take isl_basic_map
*bmap
)
6555 return isl_basic_map_lexopt(bmap
, ISL_OPT_MAX
);
6558 __isl_give isl_set
*isl_basic_set_lexmin(__isl_take isl_basic_set
*bset
)
6560 return set_from_map(isl_basic_map_lexmin(bset_to_bmap(bset
)));
6563 __isl_give isl_set
*isl_basic_set_lexmax(__isl_take isl_basic_set
*bset
)
6565 return set_from_map(isl_basic_map_lexmax(bset_to_bmap(bset
)));
6568 /* Compute the lexicographic minimum of "bset" over its parametric domain
6569 * for the purpose of quantifier elimination.
6570 * That is, find an explicit representation for all the existentially
6571 * quantified variables in "bset" by computing their lexicographic
6574 static __isl_give isl_set
*isl_basic_set_lexmin_compute_divs(
6575 __isl_take isl_basic_set
*bset
)
6577 return isl_basic_set_lexopt(bset
, ISL_OPT_QE
);
6580 /* Extract the first and only affine expression from list
6581 * and then add it to *pwaff with the given dom.
6582 * This domain is known to be disjoint from other domains
6583 * because of the way isl_basic_map_foreach_lexmax works.
6585 static int update_dim_opt(__isl_take isl_basic_set
*dom
,
6586 __isl_take isl_aff_list
*list
, void *user
)
6588 isl_ctx
*ctx
= isl_basic_set_get_ctx(dom
);
6590 isl_pw_aff
**pwaff
= user
;
6591 isl_pw_aff
*pwaff_i
;
6595 if (isl_aff_list_n_aff(list
) != 1)
6596 isl_die(ctx
, isl_error_internal
,
6597 "expecting single element list", goto error
);
6599 aff
= isl_aff_list_get_aff(list
, 0);
6600 pwaff_i
= isl_pw_aff_alloc(isl_set_from_basic_set(dom
), aff
);
6602 *pwaff
= isl_pw_aff_add_disjoint(*pwaff
, pwaff_i
);
6604 isl_aff_list_free(list
);
6608 isl_basic_set_free(dom
);
6609 isl_aff_list_free(list
);
6613 /* Given a basic map with one output dimension, compute the minimum or
6614 * maximum of that dimension as an isl_pw_aff.
6616 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
6617 * call update_dim_opt on each leaf of the result.
6619 static __isl_give isl_pw_aff
*basic_map_dim_opt(__isl_keep isl_basic_map
*bmap
,
6622 isl_space
*dim
= isl_basic_map_get_space(bmap
);
6626 dim
= isl_space_from_domain(isl_space_domain(dim
));
6627 dim
= isl_space_add_dims(dim
, isl_dim_out
, 1);
6628 pwaff
= isl_pw_aff_empty(dim
);
6630 r
= isl_basic_map_foreach_lexopt(bmap
, max
, &update_dim_opt
, &pwaff
);
6632 return isl_pw_aff_free(pwaff
);
6637 /* Compute the minimum or maximum of the given output dimension
6638 * as a function of the parameters and the input dimensions,
6639 * but independently of the other output dimensions.
6641 * We first project out the other output dimension and then compute
6642 * the "lexicographic" maximum in each basic map, combining the results
6643 * using isl_pw_aff_union_max.
6645 static __isl_give isl_pw_aff
*map_dim_opt(__isl_take isl_map
*map
, int pos
,
6652 n_out
= isl_map_dim(map
, isl_dim_out
);
6653 map
= isl_map_project_out(map
, isl_dim_out
, pos
+ 1, n_out
- (pos
+ 1));
6654 map
= isl_map_project_out(map
, isl_dim_out
, 0, pos
);
6659 isl_space
*dim
= isl_map_get_space(map
);
6661 return isl_pw_aff_empty(dim
);
6664 pwaff
= basic_map_dim_opt(map
->p
[0], max
);
6665 for (i
= 1; i
< map
->n
; ++i
) {
6666 isl_pw_aff
*pwaff_i
;
6668 pwaff_i
= basic_map_dim_opt(map
->p
[i
], max
);
6669 pwaff
= isl_pw_aff_union_opt(pwaff
, pwaff_i
, max
);
6677 /* Compute the minimum of the given output dimension as a function of the
6678 * parameters and input dimensions, but independently of
6679 * the other output dimensions.
6681 __isl_give isl_pw_aff
*isl_map_dim_min(__isl_take isl_map
*map
, int pos
)
6683 return map_dim_opt(map
, pos
, 0);
6686 /* Compute the maximum of the given output dimension as a function of the
6687 * parameters and input dimensions, but independently of
6688 * the other output dimensions.
6690 __isl_give isl_pw_aff
*isl_map_dim_max(__isl_take isl_map
*map
, int pos
)
6692 return map_dim_opt(map
, pos
, 1);
6695 /* Compute the minimum or maximum of the given set dimension
6696 * as a function of the parameters,
6697 * but independently of the other set dimensions.
6699 static __isl_give isl_pw_aff
*set_dim_opt(__isl_take isl_set
*set
, int pos
,
6702 return map_dim_opt(set
, pos
, max
);
6705 /* Compute the maximum of the given set dimension as a function of the
6706 * parameters, but independently of the other set dimensions.
6708 __isl_give isl_pw_aff
*isl_set_dim_max(__isl_take isl_set
*set
, int pos
)
6710 return set_dim_opt(set
, pos
, 1);
6713 /* Compute the minimum of the given set dimension as a function of the
6714 * parameters, but independently of the other set dimensions.
6716 __isl_give isl_pw_aff
*isl_set_dim_min(__isl_take isl_set
*set
, int pos
)
6718 return set_dim_opt(set
, pos
, 0);
6721 /* Apply a preimage specified by "mat" on the parameters of "bset".
6722 * bset is assumed to have only parameters and divs.
6724 static struct isl_basic_set
*basic_set_parameter_preimage(
6725 struct isl_basic_set
*bset
, struct isl_mat
*mat
)
6732 bset
->dim
= isl_space_cow(bset
->dim
);
6736 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
6738 isl_assert(bset
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
6740 bset
->dim
->nparam
= 0;
6741 bset
->dim
->n_out
= nparam
;
6742 bset
= isl_basic_set_preimage(bset
, mat
);
6744 bset
->dim
->nparam
= bset
->dim
->n_out
;
6745 bset
->dim
->n_out
= 0;
6750 isl_basic_set_free(bset
);
6754 /* Apply a preimage specified by "mat" on the parameters of "set".
6755 * set is assumed to have only parameters and divs.
6757 static __isl_give isl_set
*set_parameter_preimage(__isl_take isl_set
*set
,
6758 __isl_take isl_mat
*mat
)
6766 nparam
= isl_set_dim(set
, isl_dim_param
);
6768 if (mat
->n_row
!= 1 + nparam
)
6769 isl_die(isl_set_get_ctx(set
), isl_error_internal
,
6770 "unexpected number of rows", goto error
);
6772 space
= isl_set_get_space(set
);
6773 space
= isl_space_move_dims(space
, isl_dim_set
, 0,
6774 isl_dim_param
, 0, nparam
);
6775 set
= isl_set_reset_space(set
, space
);
6776 set
= isl_set_preimage(set
, mat
);
6777 nparam
= isl_set_dim(set
, isl_dim_out
);
6778 space
= isl_set_get_space(set
);
6779 space
= isl_space_move_dims(space
, isl_dim_param
, 0,
6780 isl_dim_out
, 0, nparam
);
6781 set
= isl_set_reset_space(set
, space
);
6789 /* Intersect the basic set "bset" with the affine space specified by the
6790 * equalities in "eq".
6792 static struct isl_basic_set
*basic_set_append_equalities(
6793 struct isl_basic_set
*bset
, struct isl_mat
*eq
)
6801 bset
= isl_basic_set_extend_space(bset
, isl_space_copy(bset
->dim
), 0,
6806 len
= 1 + isl_space_dim(bset
->dim
, isl_dim_all
) + bset
->extra
;
6807 for (i
= 0; i
< eq
->n_row
; ++i
) {
6808 k
= isl_basic_set_alloc_equality(bset
);
6811 isl_seq_cpy(bset
->eq
[k
], eq
->row
[i
], eq
->n_col
);
6812 isl_seq_clr(bset
->eq
[k
] + eq
->n_col
, len
- eq
->n_col
);
6816 bset
= isl_basic_set_gauss(bset
, NULL
);
6817 bset
= isl_basic_set_finalize(bset
);
6822 isl_basic_set_free(bset
);
6826 /* Intersect the set "set" with the affine space specified by the
6827 * equalities in "eq".
6829 static struct isl_set
*set_append_equalities(struct isl_set
*set
,
6837 for (i
= 0; i
< set
->n
; ++i
) {
6838 set
->p
[i
] = basic_set_append_equalities(set
->p
[i
],
6851 /* Given a basic set "bset" that only involves parameters and existentially
6852 * quantified variables, return the index of the first equality
6853 * that only involves parameters. If there is no such equality then
6854 * return bset->n_eq.
6856 * This function assumes that isl_basic_set_gauss has been called on "bset".
6858 static int first_parameter_equality(__isl_keep isl_basic_set
*bset
)
6861 unsigned nparam
, n_div
;
6866 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
6867 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
6869 for (i
= 0, j
= n_div
- 1; i
< bset
->n_eq
&& j
>= 0; --j
) {
6870 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ j
]))
6877 /* Compute an explicit representation for the existentially quantified
6878 * variables in "bset" by computing the "minimal value" of the set
6879 * variables. Since there are no set variables, the computation of
6880 * the minimal value essentially computes an explicit representation
6881 * of the non-empty part(s) of "bset".
6883 * The input only involves parameters and existentially quantified variables.
6884 * All equalities among parameters have been removed.
6886 * Since the existentially quantified variables in the result are in general
6887 * going to be different from those in the input, we first replace
6888 * them by the minimal number of variables based on their equalities.
6889 * This should simplify the parametric integer programming.
6891 static __isl_give isl_set
*base_compute_divs(__isl_take isl_basic_set
*bset
)
6893 isl_morph
*morph1
, *morph2
;
6899 if (bset
->n_eq
== 0)
6900 return isl_basic_set_lexmin_compute_divs(bset
);
6902 morph1
= isl_basic_set_parameter_compression(bset
);
6903 bset
= isl_morph_basic_set(isl_morph_copy(morph1
), bset
);
6904 bset
= isl_basic_set_lift(bset
);
6905 morph2
= isl_basic_set_variable_compression(bset
, isl_dim_set
);
6906 bset
= isl_morph_basic_set(morph2
, bset
);
6907 n
= isl_basic_set_dim(bset
, isl_dim_set
);
6908 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
6910 set
= isl_basic_set_lexmin_compute_divs(bset
);
6912 set
= isl_morph_set(isl_morph_inverse(morph1
), set
);
6917 /* Project the given basic set onto its parameter domain, possibly introducing
6918 * new, explicit, existential variables in the constraints.
6919 * The input has parameters and (possibly implicit) existential variables.
6920 * The output has the same parameters, but only
6921 * explicit existentially quantified variables.
6923 * The actual projection is performed by pip, but pip doesn't seem
6924 * to like equalities very much, so we first remove the equalities
6925 * among the parameters by performing a variable compression on
6926 * the parameters. Afterward, an inverse transformation is performed
6927 * and the equalities among the parameters are inserted back in.
6929 * The variable compression on the parameters may uncover additional
6930 * equalities that were only implicit before. We therefore check
6931 * if there are any new parameter equalities in the result and
6932 * if so recurse. The removal of parameter equalities is required
6933 * for the parameter compression performed by base_compute_divs.
6935 static struct isl_set
*parameter_compute_divs(struct isl_basic_set
*bset
)
6939 struct isl_mat
*T
, *T2
;
6940 struct isl_set
*set
;
6943 bset
= isl_basic_set_cow(bset
);
6947 if (bset
->n_eq
== 0)
6948 return base_compute_divs(bset
);
6950 bset
= isl_basic_set_gauss(bset
, NULL
);
6953 if (isl_basic_set_plain_is_empty(bset
))
6954 return isl_set_from_basic_set(bset
);
6956 i
= first_parameter_equality(bset
);
6957 if (i
== bset
->n_eq
)
6958 return base_compute_divs(bset
);
6960 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
6961 eq
= isl_mat_sub_alloc6(bset
->ctx
, bset
->eq
, i
, bset
->n_eq
- i
,
6963 eq
= isl_mat_cow(eq
);
6964 T
= isl_mat_variable_compression(isl_mat_copy(eq
), &T2
);
6965 if (T
&& T
->n_col
== 0) {
6969 bset
= isl_basic_set_set_to_empty(bset
);
6970 return isl_set_from_basic_set(bset
);
6972 bset
= basic_set_parameter_preimage(bset
, T
);
6974 i
= first_parameter_equality(bset
);
6977 else if (i
== bset
->n_eq
)
6978 set
= base_compute_divs(bset
);
6980 set
= parameter_compute_divs(bset
);
6981 set
= set_parameter_preimage(set
, T2
);
6982 set
= set_append_equalities(set
, eq
);
6986 /* Insert the divs from "ls" before those of "bmap".
6988 * The number of columns is not changed, which means that the last
6989 * dimensions of "bmap" are being reintepreted as the divs from "ls".
6990 * The caller is responsible for removing the same number of dimensions
6991 * from the space of "bmap".
6993 static __isl_give isl_basic_map
*insert_divs_from_local_space(
6994 __isl_take isl_basic_map
*bmap
, __isl_keep isl_local_space
*ls
)
7000 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
7004 old_n_div
= bmap
->n_div
;
7005 bmap
= insert_div_rows(bmap
, n_div
);
7009 for (i
= 0; i
< n_div
; ++i
) {
7010 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], ls
->div
->n_col
);
7011 isl_seq_clr(bmap
->div
[i
] + ls
->div
->n_col
, old_n_div
);
7017 /* Replace the space of "bmap" by the space and divs of "ls".
7019 * If "ls" has any divs, then we simplify the result since we may
7020 * have discovered some additional equalities that could simplify
7021 * the div expressions.
7023 static __isl_give isl_basic_map
*basic_replace_space_by_local_space(
7024 __isl_take isl_basic_map
*bmap
, __isl_take isl_local_space
*ls
)
7028 bmap
= isl_basic_map_cow(bmap
);
7032 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
7033 bmap
= insert_divs_from_local_space(bmap
, ls
);
7037 isl_space_free(bmap
->dim
);
7038 bmap
->dim
= isl_local_space_get_space(ls
);
7042 isl_local_space_free(ls
);
7044 bmap
= isl_basic_map_simplify(bmap
);
7045 bmap
= isl_basic_map_finalize(bmap
);
7048 isl_basic_map_free(bmap
);
7049 isl_local_space_free(ls
);
7053 /* Replace the space of "map" by the space and divs of "ls".
7055 static __isl_give isl_map
*replace_space_by_local_space(__isl_take isl_map
*map
,
7056 __isl_take isl_local_space
*ls
)
7060 map
= isl_map_cow(map
);
7064 for (i
= 0; i
< map
->n
; ++i
) {
7065 map
->p
[i
] = basic_replace_space_by_local_space(map
->p
[i
],
7066 isl_local_space_copy(ls
));
7070 isl_space_free(map
->dim
);
7071 map
->dim
= isl_local_space_get_space(ls
);
7075 isl_local_space_free(ls
);
7078 isl_local_space_free(ls
);
7083 /* Compute an explicit representation for the existentially
7084 * quantified variables for which do not know any explicit representation yet.
7086 * We first sort the existentially quantified variables so that the
7087 * existentially quantified variables for which we already have an explicit
7088 * representation are placed before those for which we do not.
7089 * The input dimensions, the output dimensions and the existentially
7090 * quantified variables for which we already have an explicit
7091 * representation are then turned into parameters.
7092 * compute_divs returns a map with the same parameters and
7093 * no input or output dimensions and the dimension specification
7094 * is reset to that of the input, including the existentially quantified
7095 * variables for which we already had an explicit representation.
7097 static struct isl_map
*compute_divs(struct isl_basic_map
*bmap
)
7099 struct isl_basic_set
*bset
;
7100 struct isl_set
*set
;
7101 struct isl_map
*map
;
7103 isl_local_space
*ls
;
7110 bmap
= isl_basic_map_sort_divs(bmap
);
7111 bmap
= isl_basic_map_cow(bmap
);
7115 n_known
= isl_basic_map_first_unknown_div(bmap
);
7117 return isl_map_from_basic_map(isl_basic_map_free(bmap
));
7119 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
7120 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
7121 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
7122 dim
= isl_space_set_alloc(bmap
->ctx
,
7123 nparam
+ n_in
+ n_out
+ n_known
, 0);
7127 ls
= isl_basic_map_get_local_space(bmap
);
7128 ls
= isl_local_space_drop_dims(ls
, isl_dim_div
,
7129 n_known
, bmap
->n_div
- n_known
);
7131 for (i
= n_known
; i
< bmap
->n_div
; ++i
)
7132 swap_div(bmap
, i
- n_known
, i
);
7133 bmap
->n_div
-= n_known
;
7134 bmap
->extra
-= n_known
;
7136 bmap
= isl_basic_map_reset_space(bmap
, dim
);
7137 bset
= bset_from_bmap(bmap
);
7139 set
= parameter_compute_divs(bset
);
7140 map
= set_to_map(set
);
7141 map
= replace_space_by_local_space(map
, ls
);
7145 isl_basic_map_free(bmap
);
7149 /* Remove the explicit representation of local variable "div",
7152 __isl_give isl_basic_map
*isl_basic_map_mark_div_unknown(
7153 __isl_take isl_basic_map
*bmap
, int div
)
7157 unknown
= isl_basic_map_div_is_marked_unknown(bmap
, div
);
7159 return isl_basic_map_free(bmap
);
7163 bmap
= isl_basic_map_cow(bmap
);
7166 isl_int_set_si(bmap
->div
[div
][0], 0);
7170 /* Is local variable "div" of "bmap" marked as not having an explicit
7172 * Note that even if "div" is not marked in this way and therefore
7173 * has an explicit representation, this representation may still
7174 * depend (indirectly) on other local variables that do not
7175 * have an explicit representation.
7177 isl_bool
isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map
*bmap
,
7180 if (isl_basic_map_check_range(bmap
, isl_dim_div
, div
, 1) < 0)
7181 return isl_bool_error
;
7182 return isl_int_is_zero(bmap
->div
[div
][0]);
7185 /* Return the position of the first local variable that does not
7186 * have an explicit representation.
7187 * Return the total number of local variables if they all have
7188 * an explicit representation.
7189 * Return -1 on error.
7191 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map
*bmap
)
7198 for (i
= 0; i
< bmap
->n_div
; ++i
) {
7199 if (!isl_basic_map_div_is_known(bmap
, i
))
7205 /* Return the position of the first local variable that does not
7206 * have an explicit representation.
7207 * Return the total number of local variables if they all have
7208 * an explicit representation.
7209 * Return -1 on error.
7211 int isl_basic_set_first_unknown_div(__isl_keep isl_basic_set
*bset
)
7213 return isl_basic_map_first_unknown_div(bset
);
7216 /* Does "bmap" have an explicit representation for all local variables?
7218 isl_bool
isl_basic_map_divs_known(__isl_keep isl_basic_map
*bmap
)
7222 n
= isl_basic_map_dim(bmap
, isl_dim_div
);
7223 first
= isl_basic_map_first_unknown_div(bmap
);
7225 return isl_bool_error
;
7229 /* Do all basic maps in "map" have an explicit representation
7230 * for all local variables?
7232 isl_bool
isl_map_divs_known(__isl_keep isl_map
*map
)
7237 return isl_bool_error
;
7239 for (i
= 0; i
< map
->n
; ++i
) {
7240 int known
= isl_basic_map_divs_known(map
->p
[i
]);
7245 return isl_bool_true
;
7248 /* If bmap contains any unknown divs, then compute explicit
7249 * expressions for them. However, this computation may be
7250 * quite expensive, so first try to remove divs that aren't
7253 struct isl_map
*isl_basic_map_compute_divs(struct isl_basic_map
*bmap
)
7256 struct isl_map
*map
;
7258 known
= isl_basic_map_divs_known(bmap
);
7262 return isl_map_from_basic_map(bmap
);
7264 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
7266 known
= isl_basic_map_divs_known(bmap
);
7270 return isl_map_from_basic_map(bmap
);
7272 map
= compute_divs(bmap
);
7275 isl_basic_map_free(bmap
);
7279 struct isl_map
*isl_map_compute_divs(struct isl_map
*map
)
7283 struct isl_map
*res
;
7290 known
= isl_map_divs_known(map
);
7298 res
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[0]));
7299 for (i
= 1 ; i
< map
->n
; ++i
) {
7301 r2
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[i
]));
7302 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
7303 res
= isl_map_union_disjoint(res
, r2
);
7305 res
= isl_map_union(res
, r2
);
7312 struct isl_set
*isl_basic_set_compute_divs(struct isl_basic_set
*bset
)
7314 return set_from_map(isl_basic_map_compute_divs(bset_to_bmap(bset
)));
7317 struct isl_set
*isl_set_compute_divs(struct isl_set
*set
)
7319 return set_from_map(isl_map_compute_divs(set_to_map(set
)));
7322 struct isl_set
*isl_map_domain(struct isl_map
*map
)
7325 struct isl_set
*set
;
7330 map
= isl_map_cow(map
);
7334 set
= set_from_map(map
);
7335 set
->dim
= isl_space_domain(set
->dim
);
7338 for (i
= 0; i
< map
->n
; ++i
) {
7339 set
->p
[i
] = isl_basic_map_domain(map
->p
[i
]);
7343 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
7344 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
7351 /* Return the union of "map1" and "map2", where we assume for now that
7352 * "map1" and "map2" are disjoint. Note that the basic maps inside
7353 * "map1" or "map2" may not be disjoint from each other.
7354 * Also note that this function is also called from isl_map_union,
7355 * which takes care of handling the situation where "map1" and "map2"
7356 * may not be disjoint.
7358 * If one of the inputs is empty, we can simply return the other input.
7359 * Similarly, if one of the inputs is universal, then it is equal to the union.
7361 static __isl_give isl_map
*map_union_disjoint(__isl_take isl_map
*map1
,
7362 __isl_take isl_map
*map2
)
7366 struct isl_map
*map
= NULL
;
7372 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
7373 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
7374 "spaces don't match", goto error
);
7385 is_universe
= isl_map_plain_is_universe(map1
);
7386 if (is_universe
< 0)
7393 is_universe
= isl_map_plain_is_universe(map2
);
7394 if (is_universe
< 0)
7401 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
7402 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
7403 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
7405 map
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
7406 map1
->n
+ map2
->n
, flags
);
7409 for (i
= 0; i
< map1
->n
; ++i
) {
7410 map
= isl_map_add_basic_map(map
,
7411 isl_basic_map_copy(map1
->p
[i
]));
7415 for (i
= 0; i
< map2
->n
; ++i
) {
7416 map
= isl_map_add_basic_map(map
,
7417 isl_basic_map_copy(map2
->p
[i
]));
7431 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7432 * guaranteed to be disjoint by the caller.
7434 * Note that this functions is called from within isl_map_make_disjoint,
7435 * so we have to be careful not to touch the constraints of the inputs
7438 __isl_give isl_map
*isl_map_union_disjoint(__isl_take isl_map
*map1
,
7439 __isl_take isl_map
*map2
)
7441 return isl_map_align_params_map_map_and(map1
, map2
, &map_union_disjoint
);
7444 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7445 * not be disjoint. The parameters are assumed to have been aligned.
7447 * We currently simply call map_union_disjoint, the internal operation
7448 * of which does not really depend on the inputs being disjoint.
7449 * If the result contains more than one basic map, then we clear
7450 * the disjoint flag since the result may contain basic maps from
7451 * both inputs and these are not guaranteed to be disjoint.
7453 * As a special case, if "map1" and "map2" are obviously equal,
7454 * then we simply return "map1".
7456 static __isl_give isl_map
*map_union_aligned(__isl_take isl_map
*map1
,
7457 __isl_take isl_map
*map2
)
7464 equal
= isl_map_plain_is_equal(map1
, map2
);
7472 map1
= map_union_disjoint(map1
, map2
);
7476 ISL_F_CLR(map1
, ISL_MAP_DISJOINT
);
7484 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7487 __isl_give isl_map
*isl_map_union(__isl_take isl_map
*map1
,
7488 __isl_take isl_map
*map2
)
7490 return isl_map_align_params_map_map_and(map1
, map2
, &map_union_aligned
);
7493 struct isl_set
*isl_set_union_disjoint(
7494 struct isl_set
*set1
, struct isl_set
*set2
)
7496 return set_from_map(isl_map_union_disjoint(set_to_map(set1
),
7500 struct isl_set
*isl_set_union(struct isl_set
*set1
, struct isl_set
*set2
)
7502 return set_from_map(isl_map_union(set_to_map(set1
), set_to_map(set2
)));
7505 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7508 * "map" and "set" are assumed to be compatible and non-NULL.
7510 static __isl_give isl_map
*map_intersect_set(__isl_take isl_map
*map
,
7511 __isl_take isl_set
*set
,
7512 __isl_give isl_basic_map
*fn(__isl_take isl_basic_map
*bmap
,
7513 __isl_take isl_basic_set
*bset
))
7516 struct isl_map
*result
;
7519 if (isl_set_plain_is_universe(set
)) {
7524 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
) &&
7525 ISL_F_ISSET(set
, ISL_MAP_DISJOINT
))
7526 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
7528 result
= isl_map_alloc_space(isl_space_copy(map
->dim
),
7529 map
->n
* set
->n
, flags
);
7530 for (i
= 0; result
&& i
< map
->n
; ++i
)
7531 for (j
= 0; j
< set
->n
; ++j
) {
7532 result
= isl_map_add_basic_map(result
,
7533 fn(isl_basic_map_copy(map
->p
[i
]),
7534 isl_basic_set_copy(set
->p
[j
])));
7544 static __isl_give isl_map
*map_intersect_range(__isl_take isl_map
*map
,
7545 __isl_take isl_set
*set
)
7550 if (!isl_map_compatible_range(map
, set
))
7551 isl_die(set
->ctx
, isl_error_invalid
,
7552 "incompatible spaces", goto error
);
7554 return map_intersect_set(map
, set
, &isl_basic_map_intersect_range
);
7561 __isl_give isl_map
*isl_map_intersect_range(__isl_take isl_map
*map
,
7562 __isl_take isl_set
*set
)
7564 return isl_map_align_params_map_map_and(map
, set
, &map_intersect_range
);
7567 static __isl_give isl_map
*map_intersect_domain(__isl_take isl_map
*map
,
7568 __isl_take isl_set
*set
)
7573 if (!isl_map_compatible_domain(map
, set
))
7574 isl_die(set
->ctx
, isl_error_invalid
,
7575 "incompatible spaces", goto error
);
7577 return map_intersect_set(map
, set
, &isl_basic_map_intersect_domain
);
7584 __isl_give isl_map
*isl_map_intersect_domain(__isl_take isl_map
*map
,
7585 __isl_take isl_set
*set
)
7587 return isl_map_align_params_map_map_and(map
, set
,
7588 &map_intersect_domain
);
7591 static __isl_give isl_map
*map_apply_domain(__isl_take isl_map
*map1
,
7592 __isl_take isl_map
*map2
)
7596 map1
= isl_map_reverse(map1
);
7597 map1
= isl_map_apply_range(map1
, map2
);
7598 return isl_map_reverse(map1
);
7605 __isl_give isl_map
*isl_map_apply_domain(__isl_take isl_map
*map1
,
7606 __isl_take isl_map
*map2
)
7608 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_domain
);
7611 static __isl_give isl_map
*map_apply_range(__isl_take isl_map
*map1
,
7612 __isl_take isl_map
*map2
)
7614 isl_space
*dim_result
;
7615 struct isl_map
*result
;
7621 dim_result
= isl_space_join(isl_space_copy(map1
->dim
),
7622 isl_space_copy(map2
->dim
));
7624 result
= isl_map_alloc_space(dim_result
, map1
->n
* map2
->n
, 0);
7627 for (i
= 0; i
< map1
->n
; ++i
)
7628 for (j
= 0; j
< map2
->n
; ++j
) {
7629 result
= isl_map_add_basic_map(result
,
7630 isl_basic_map_apply_range(
7631 isl_basic_map_copy(map1
->p
[i
]),
7632 isl_basic_map_copy(map2
->p
[j
])));
7638 if (result
&& result
->n
<= 1)
7639 ISL_F_SET(result
, ISL_MAP_DISJOINT
);
7647 __isl_give isl_map
*isl_map_apply_range(__isl_take isl_map
*map1
,
7648 __isl_take isl_map
*map2
)
7650 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_range
);
7654 * returns range - domain
7656 struct isl_basic_set
*isl_basic_map_deltas(struct isl_basic_map
*bmap
)
7658 isl_space
*target_space
;
7659 struct isl_basic_set
*bset
;
7666 isl_assert(bmap
->ctx
, isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
7667 bmap
->dim
, isl_dim_out
),
7669 target_space
= isl_space_domain(isl_basic_map_get_space(bmap
));
7670 dim
= isl_basic_map_n_in(bmap
);
7671 nparam
= isl_basic_map_n_param(bmap
);
7672 bmap
= isl_basic_map_from_range(isl_basic_map_wrap(bmap
));
7673 bmap
= isl_basic_map_add_dims(bmap
, isl_dim_in
, dim
);
7674 bmap
= isl_basic_map_extend_constraints(bmap
, dim
, 0);
7675 for (i
= 0; i
< dim
; ++i
) {
7676 int j
= isl_basic_map_alloc_equality(bmap
);
7678 bmap
= isl_basic_map_free(bmap
);
7681 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
7682 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
7683 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], 1);
7684 isl_int_set_si(bmap
->eq
[j
][1+nparam
+2*dim
+i
], -1);
7686 bset
= isl_basic_map_domain(bmap
);
7687 bset
= isl_basic_set_reset_space(bset
, target_space
);
7690 isl_basic_map_free(bmap
);
7695 * returns range - domain
7697 __isl_give isl_set
*isl_map_deltas(__isl_take isl_map
*map
)
7701 struct isl_set
*result
;
7706 isl_assert(map
->ctx
, isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
7707 map
->dim
, isl_dim_out
),
7709 dim
= isl_map_get_space(map
);
7710 dim
= isl_space_domain(dim
);
7711 result
= isl_set_alloc_space(dim
, map
->n
, 0);
7714 for (i
= 0; i
< map
->n
; ++i
)
7715 result
= isl_set_add_basic_set(result
,
7716 isl_basic_map_deltas(isl_basic_map_copy(map
->p
[i
])));
7725 * returns [domain -> range] -> range - domain
7727 __isl_give isl_basic_map
*isl_basic_map_deltas_map(
7728 __isl_take isl_basic_map
*bmap
)
7732 isl_basic_map
*domain
;
7736 if (!isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
7737 bmap
->dim
, isl_dim_out
))
7738 isl_die(bmap
->ctx
, isl_error_invalid
,
7739 "domain and range don't match", goto error
);
7741 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
7742 n
= isl_basic_map_dim(bmap
, isl_dim_in
);
7744 dim
= isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap
)));
7745 domain
= isl_basic_map_universe(dim
);
7747 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
7748 bmap
= isl_basic_map_apply_range(bmap
, domain
);
7749 bmap
= isl_basic_map_extend_constraints(bmap
, n
, 0);
7751 total
= isl_basic_map_total_dim(bmap
);
7753 for (i
= 0; i
< n
; ++i
) {
7754 k
= isl_basic_map_alloc_equality(bmap
);
7757 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
7758 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], 1);
7759 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ i
], -1);
7760 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ n
+ i
], 1);
7763 bmap
= isl_basic_map_gauss(bmap
, NULL
);
7764 return isl_basic_map_finalize(bmap
);
7766 isl_basic_map_free(bmap
);
7771 * returns [domain -> range] -> range - domain
7773 __isl_give isl_map
*isl_map_deltas_map(__isl_take isl_map
*map
)
7776 isl_space
*domain_dim
;
7781 if (!isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
7782 map
->dim
, isl_dim_out
))
7783 isl_die(map
->ctx
, isl_error_invalid
,
7784 "domain and range don't match", goto error
);
7786 map
= isl_map_cow(map
);
7790 domain_dim
= isl_space_from_range(isl_space_domain(isl_map_get_space(map
)));
7791 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
7792 map
->dim
= isl_space_join(map
->dim
, domain_dim
);
7795 for (i
= 0; i
< map
->n
; ++i
) {
7796 map
->p
[i
] = isl_basic_map_deltas_map(map
->p
[i
]);
7800 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
7807 static __isl_give isl_basic_map
*basic_map_identity(__isl_take isl_space
*dims
)
7809 struct isl_basic_map
*bmap
;
7817 nparam
= dims
->nparam
;
7819 bmap
= isl_basic_map_alloc_space(dims
, 0, dim
, 0);
7823 for (i
= 0; i
< dim
; ++i
) {
7824 int j
= isl_basic_map_alloc_equality(bmap
);
7827 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
7828 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
7829 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], -1);
7831 return isl_basic_map_finalize(bmap
);
7833 isl_basic_map_free(bmap
);
7837 __isl_give isl_basic_map
*isl_basic_map_identity(__isl_take isl_space
*dim
)
7841 if (dim
->n_in
!= dim
->n_out
)
7842 isl_die(dim
->ctx
, isl_error_invalid
,
7843 "number of input and output dimensions needs to be "
7844 "the same", goto error
);
7845 return basic_map_identity(dim
);
7847 isl_space_free(dim
);
7851 __isl_give isl_map
*isl_map_identity(__isl_take isl_space
*dim
)
7853 return isl_map_from_basic_map(isl_basic_map_identity(dim
));
7856 __isl_give isl_map
*isl_set_identity(__isl_take isl_set
*set
)
7858 isl_space
*dim
= isl_set_get_space(set
);
7860 id
= isl_map_identity(isl_space_map_from_set(dim
));
7861 return isl_map_intersect_range(id
, set
);
7864 /* Construct a basic set with all set dimensions having only non-negative
7867 __isl_give isl_basic_set
*isl_basic_set_positive_orthant(
7868 __isl_take isl_space
*space
)
7873 struct isl_basic_set
*bset
;
7877 nparam
= space
->nparam
;
7879 bset
= isl_basic_set_alloc_space(space
, 0, 0, dim
);
7882 for (i
= 0; i
< dim
; ++i
) {
7883 int k
= isl_basic_set_alloc_inequality(bset
);
7886 isl_seq_clr(bset
->ineq
[k
], 1 + isl_basic_set_total_dim(bset
));
7887 isl_int_set_si(bset
->ineq
[k
][1 + nparam
+ i
], 1);
7891 isl_basic_set_free(bset
);
7895 /* Construct the half-space x_pos >= 0.
7897 static __isl_give isl_basic_set
*nonneg_halfspace(__isl_take isl_space
*dim
,
7901 isl_basic_set
*nonneg
;
7903 nonneg
= isl_basic_set_alloc_space(dim
, 0, 0, 1);
7904 k
= isl_basic_set_alloc_inequality(nonneg
);
7907 isl_seq_clr(nonneg
->ineq
[k
], 1 + isl_basic_set_total_dim(nonneg
));
7908 isl_int_set_si(nonneg
->ineq
[k
][pos
], 1);
7910 return isl_basic_set_finalize(nonneg
);
7912 isl_basic_set_free(nonneg
);
7916 /* Construct the half-space x_pos <= -1.
7918 static __isl_give isl_basic_set
*neg_halfspace(__isl_take isl_space
*dim
, int pos
)
7923 neg
= isl_basic_set_alloc_space(dim
, 0, 0, 1);
7924 k
= isl_basic_set_alloc_inequality(neg
);
7927 isl_seq_clr(neg
->ineq
[k
], 1 + isl_basic_set_total_dim(neg
));
7928 isl_int_set_si(neg
->ineq
[k
][0], -1);
7929 isl_int_set_si(neg
->ineq
[k
][pos
], -1);
7931 return isl_basic_set_finalize(neg
);
7933 isl_basic_set_free(neg
);
7937 __isl_give isl_set
*isl_set_split_dims(__isl_take isl_set
*set
,
7938 enum isl_dim_type type
, unsigned first
, unsigned n
)
7942 isl_basic_set
*nonneg
;
7950 isl_assert(set
->ctx
, first
+ n
<= isl_set_dim(set
, type
), goto error
);
7952 offset
= pos(set
->dim
, type
);
7953 for (i
= 0; i
< n
; ++i
) {
7954 nonneg
= nonneg_halfspace(isl_set_get_space(set
),
7955 offset
+ first
+ i
);
7956 neg
= neg_halfspace(isl_set_get_space(set
), offset
+ first
+ i
);
7958 set
= isl_set_intersect(set
, isl_basic_set_union(nonneg
, neg
));
7967 static int foreach_orthant(__isl_take isl_set
*set
, int *signs
, int first
,
7968 int len
, int (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
7975 if (isl_set_plain_is_empty(set
)) {
7980 return fn(set
, signs
, user
);
7983 half
= isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set
),
7985 half
= isl_set_intersect(half
, isl_set_copy(set
));
7986 if (foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
) < 0)
7990 half
= isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set
),
7992 half
= isl_set_intersect(half
, set
);
7993 return foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
);
7999 /* Call "fn" on the intersections of "set" with each of the orthants
8000 * (except for obviously empty intersections). The orthant is identified
8001 * by the signs array, with each entry having value 1 or -1 according
8002 * to the sign of the corresponding variable.
8004 int isl_set_foreach_orthant(__isl_keep isl_set
*set
,
8005 int (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
8015 if (isl_set_plain_is_empty(set
))
8018 nparam
= isl_set_dim(set
, isl_dim_param
);
8019 nvar
= isl_set_dim(set
, isl_dim_set
);
8021 signs
= isl_alloc_array(set
->ctx
, int, nparam
+ nvar
);
8023 r
= foreach_orthant(isl_set_copy(set
), signs
, 0, nparam
+ nvar
,
8031 isl_bool
isl_set_is_equal(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
8033 return isl_map_is_equal(set_to_map(set1
), set_to_map(set2
));
8036 isl_bool
isl_basic_map_is_subset(__isl_keep isl_basic_map
*bmap1
,
8037 __isl_keep isl_basic_map
*bmap2
)
8040 struct isl_map
*map1
;
8041 struct isl_map
*map2
;
8043 if (!bmap1
|| !bmap2
)
8044 return isl_bool_error
;
8046 map1
= isl_map_from_basic_map(isl_basic_map_copy(bmap1
));
8047 map2
= isl_map_from_basic_map(isl_basic_map_copy(bmap2
));
8049 is_subset
= isl_map_is_subset(map1
, map2
);
8057 isl_bool
isl_basic_set_is_subset(__isl_keep isl_basic_set
*bset1
,
8058 __isl_keep isl_basic_set
*bset2
)
8060 return isl_basic_map_is_subset(bset1
, bset2
);
8063 isl_bool
isl_basic_map_is_equal(__isl_keep isl_basic_map
*bmap1
,
8064 __isl_keep isl_basic_map
*bmap2
)
8068 if (!bmap1
|| !bmap2
)
8069 return isl_bool_error
;
8070 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
8071 if (is_subset
!= isl_bool_true
)
8073 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
8077 isl_bool
isl_basic_set_is_equal(__isl_keep isl_basic_set
*bset1
,
8078 __isl_keep isl_basic_set
*bset2
)
8080 return isl_basic_map_is_equal(
8081 bset_to_bmap(bset1
), bset_to_bmap(bset2
));
8084 isl_bool
isl_map_is_empty(__isl_keep isl_map
*map
)
8090 return isl_bool_error
;
8091 for (i
= 0; i
< map
->n
; ++i
) {
8092 is_empty
= isl_basic_map_is_empty(map
->p
[i
]);
8094 return isl_bool_error
;
8096 return isl_bool_false
;
8098 return isl_bool_true
;
8101 isl_bool
isl_map_plain_is_empty(__isl_keep isl_map
*map
)
8103 return map
? map
->n
== 0 : isl_bool_error
;
8106 isl_bool
isl_set_plain_is_empty(__isl_keep isl_set
*set
)
8108 return set
? set
->n
== 0 : isl_bool_error
;
8111 isl_bool
isl_set_is_empty(__isl_keep isl_set
*set
)
8113 return isl_map_is_empty(set_to_map(set
));
8116 int isl_map_has_equal_space(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
8121 return isl_space_is_equal(map1
->dim
, map2
->dim
);
8124 int isl_set_has_equal_space(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
8129 return isl_space_is_equal(set1
->dim
, set2
->dim
);
8132 static isl_bool
map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
8137 return isl_bool_error
;
8138 is_subset
= isl_map_is_subset(map1
, map2
);
8139 if (is_subset
!= isl_bool_true
)
8141 is_subset
= isl_map_is_subset(map2
, map1
);
8145 /* Is "map1" equal to "map2"?
8147 * First check if they are obviously equal.
8148 * If not, then perform a more detailed analysis.
8150 isl_bool
isl_map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
8154 equal
= isl_map_plain_is_equal(map1
, map2
);
8155 if (equal
< 0 || equal
)
8157 return isl_map_align_params_map_map_and_test(map1
, map2
, &map_is_equal
);
8160 isl_bool
isl_basic_map_is_strict_subset(
8161 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
8165 if (!bmap1
|| !bmap2
)
8166 return isl_bool_error
;
8167 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
8168 if (is_subset
!= isl_bool_true
)
8170 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
8171 if (is_subset
== isl_bool_error
)
8176 isl_bool
isl_map_is_strict_subset(__isl_keep isl_map
*map1
,
8177 __isl_keep isl_map
*map2
)
8182 return isl_bool_error
;
8183 is_subset
= isl_map_is_subset(map1
, map2
);
8184 if (is_subset
!= isl_bool_true
)
8186 is_subset
= isl_map_is_subset(map2
, map1
);
8187 if (is_subset
== isl_bool_error
)
8192 isl_bool
isl_set_is_strict_subset(__isl_keep isl_set
*set1
,
8193 __isl_keep isl_set
*set2
)
8195 return isl_map_is_strict_subset(set_to_map(set1
), set_to_map(set2
));
8198 /* Is "bmap" obviously equal to the universe with the same space?
8200 * That is, does it not have any constraints?
8202 isl_bool
isl_basic_map_plain_is_universe(__isl_keep isl_basic_map
*bmap
)
8205 return isl_bool_error
;
8206 return bmap
->n_eq
== 0 && bmap
->n_ineq
== 0;
8209 /* Is "bset" obviously equal to the universe with the same space?
8211 isl_bool
isl_basic_set_plain_is_universe(__isl_keep isl_basic_set
*bset
)
8213 return isl_basic_map_plain_is_universe(bset
);
8216 /* If "c" does not involve any existentially quantified variables,
8217 * then set *univ to false and abort
8219 static isl_stat
involves_divs(__isl_take isl_constraint
*c
, void *user
)
8221 isl_bool
*univ
= user
;
8224 n
= isl_constraint_dim(c
, isl_dim_div
);
8225 *univ
= isl_constraint_involves_dims(c
, isl_dim_div
, 0, n
);
8226 isl_constraint_free(c
);
8227 if (*univ
< 0 || !*univ
)
8228 return isl_stat_error
;
8232 /* Is "bmap" equal to the universe with the same space?
8234 * First check if it is obviously equal to the universe.
8235 * If not and if there are any constraints not involving
8236 * existentially quantified variables, then it is certainly
8237 * not equal to the universe.
8238 * Otherwise, check if the universe is a subset of "bmap".
8240 isl_bool
isl_basic_map_is_universe(__isl_keep isl_basic_map
*bmap
)
8243 isl_basic_map
*test
;
8245 univ
= isl_basic_map_plain_is_universe(bmap
);
8246 if (univ
< 0 || univ
)
8248 if (isl_basic_map_dim(bmap
, isl_dim_div
) == 0)
8249 return isl_bool_false
;
8250 univ
= isl_bool_true
;
8251 if (isl_basic_map_foreach_constraint(bmap
, &involves_divs
, &univ
) < 0 &&
8253 return isl_bool_error
;
8254 if (univ
< 0 || !univ
)
8256 test
= isl_basic_map_universe(isl_basic_map_get_space(bmap
));
8257 univ
= isl_basic_map_is_subset(test
, bmap
);
8258 isl_basic_map_free(test
);
8262 /* Is "bset" equal to the universe with the same space?
8264 isl_bool
isl_basic_set_is_universe(__isl_keep isl_basic_set
*bset
)
8266 return isl_basic_map_is_universe(bset
);
8269 isl_bool
isl_map_plain_is_universe(__isl_keep isl_map
*map
)
8274 return isl_bool_error
;
8276 for (i
= 0; i
< map
->n
; ++i
) {
8277 isl_bool r
= isl_basic_map_plain_is_universe(map
->p
[i
]);
8282 return isl_bool_false
;
8285 isl_bool
isl_set_plain_is_universe(__isl_keep isl_set
*set
)
8287 return isl_map_plain_is_universe(set_to_map(set
));
8290 isl_bool
isl_basic_map_is_empty(__isl_keep isl_basic_map
*bmap
)
8292 struct isl_basic_set
*bset
= NULL
;
8293 struct isl_vec
*sample
= NULL
;
8294 isl_bool empty
, non_empty
;
8297 return isl_bool_error
;
8299 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
8300 return isl_bool_true
;
8302 if (isl_basic_map_plain_is_universe(bmap
))
8303 return isl_bool_false
;
8305 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
8306 struct isl_basic_map
*copy
= isl_basic_map_copy(bmap
);
8307 copy
= isl_basic_map_remove_redundancies(copy
);
8308 empty
= isl_basic_map_plain_is_empty(copy
);
8309 isl_basic_map_free(copy
);
8313 non_empty
= isl_basic_map_plain_is_non_empty(bmap
);
8315 return isl_bool_error
;
8317 return isl_bool_false
;
8318 isl_vec_free(bmap
->sample
);
8319 bmap
->sample
= NULL
;
8320 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
8322 return isl_bool_error
;
8323 sample
= isl_basic_set_sample_vec(bset
);
8325 return isl_bool_error
;
8326 empty
= sample
->size
== 0;
8327 isl_vec_free(bmap
->sample
);
8328 bmap
->sample
= sample
;
8330 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
8335 isl_bool
isl_basic_map_plain_is_empty(__isl_keep isl_basic_map
*bmap
)
8338 return isl_bool_error
;
8339 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
);
8342 isl_bool
isl_basic_set_plain_is_empty(__isl_keep isl_basic_set
*bset
)
8345 return isl_bool_error
;
8346 return ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
);
8349 /* Is "bmap" known to be non-empty?
8351 * That is, is the cached sample still valid?
8353 isl_bool
isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map
*bmap
)
8358 return isl_bool_error
;
8360 return isl_bool_false
;
8361 total
= 1 + isl_basic_map_total_dim(bmap
);
8362 if (bmap
->sample
->size
!= total
)
8363 return isl_bool_false
;
8364 return isl_basic_map_contains(bmap
, bmap
->sample
);
8367 isl_bool
isl_basic_set_is_empty(__isl_keep isl_basic_set
*bset
)
8369 return isl_basic_map_is_empty(bset_to_bmap(bset
));
8372 struct isl_map
*isl_basic_map_union(
8373 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
8375 struct isl_map
*map
;
8376 if (!bmap1
|| !bmap2
)
8379 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
8381 map
= isl_map_alloc_space(isl_space_copy(bmap1
->dim
), 2, 0);
8384 map
= isl_map_add_basic_map(map
, bmap1
);
8385 map
= isl_map_add_basic_map(map
, bmap2
);
8388 isl_basic_map_free(bmap1
);
8389 isl_basic_map_free(bmap2
);
8393 struct isl_set
*isl_basic_set_union(
8394 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
8396 return set_from_map(isl_basic_map_union(bset_to_bmap(bset1
),
8397 bset_to_bmap(bset2
)));
8400 /* Order divs such that any div only depends on previous divs */
8401 struct isl_basic_map
*isl_basic_map_order_divs(struct isl_basic_map
*bmap
)
8409 off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
8411 for (i
= 0; i
< bmap
->n_div
; ++i
) {
8413 if (isl_int_is_zero(bmap
->div
[i
][0]))
8415 pos
= isl_seq_first_non_zero(bmap
->div
[i
]+1+1+off
+i
,
8420 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_internal
,
8421 "integer division depends on itself",
8422 return isl_basic_map_free(bmap
));
8423 isl_basic_map_swap_div(bmap
, i
, i
+ pos
);
8429 struct isl_basic_set
*isl_basic_set_order_divs(struct isl_basic_set
*bset
)
8431 return bset_from_bmap(isl_basic_map_order_divs(bset_to_bmap(bset
)));
8434 __isl_give isl_map
*isl_map_order_divs(__isl_take isl_map
*map
)
8441 for (i
= 0; i
< map
->n
; ++i
) {
8442 map
->p
[i
] = isl_basic_map_order_divs(map
->p
[i
]);
8453 /* Sort the local variables of "bset".
8455 __isl_give isl_basic_set
*isl_basic_set_sort_divs(
8456 __isl_take isl_basic_set
*bset
)
8458 return bset_from_bmap(isl_basic_map_sort_divs(bset_to_bmap(bset
)));
8461 /* Apply the expansion computed by isl_merge_divs.
8462 * The expansion itself is given by "exp" while the resulting
8463 * list of divs is given by "div".
8465 * Move the integer divisions of "bmap" into the right position
8466 * according to "exp" and then introduce the additional integer
8467 * divisions, adding div constraints.
8468 * The moving should be done first to avoid moving coefficients
8469 * in the definitions of the extra integer divisions.
8471 __isl_give isl_basic_map
*isl_basic_map_expand_divs(
8472 __isl_take isl_basic_map
*bmap
, __isl_take isl_mat
*div
, int *exp
)
8477 bmap
= isl_basic_map_cow(bmap
);
8481 if (div
->n_row
< bmap
->n_div
)
8482 isl_die(isl_mat_get_ctx(div
), isl_error_invalid
,
8483 "not an expansion", goto error
);
8485 n_div
= bmap
->n_div
;
8486 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
8487 div
->n_row
- n_div
, 0,
8488 2 * (div
->n_row
- n_div
));
8490 for (i
= n_div
; i
< div
->n_row
; ++i
)
8491 if (isl_basic_map_alloc_div(bmap
) < 0)
8494 for (j
= n_div
- 1; j
>= 0; --j
) {
8497 isl_basic_map_swap_div(bmap
, j
, exp
[j
]);
8500 for (i
= 0; i
< div
->n_row
; ++i
) {
8501 if (j
< n_div
&& exp
[j
] == i
) {
8504 isl_seq_cpy(bmap
->div
[i
], div
->row
[i
], div
->n_col
);
8505 if (isl_basic_map_div_is_marked_unknown(bmap
, i
))
8507 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
8515 isl_basic_map_free(bmap
);
8520 /* Apply the expansion computed by isl_merge_divs.
8521 * The expansion itself is given by "exp" while the resulting
8522 * list of divs is given by "div".
8524 __isl_give isl_basic_set
*isl_basic_set_expand_divs(
8525 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*div
, int *exp
)
8527 return isl_basic_map_expand_divs(bset
, div
, exp
);
8530 /* Look for a div in dst that corresponds to the div "div" in src.
8531 * The divs before "div" in src and dst are assumed to be the same.
8533 * Returns -1 if no corresponding div was found and the position
8534 * of the corresponding div in dst otherwise.
8536 static int find_div(struct isl_basic_map
*dst
,
8537 struct isl_basic_map
*src
, unsigned div
)
8541 unsigned total
= isl_space_dim(src
->dim
, isl_dim_all
);
8543 isl_assert(dst
->ctx
, div
<= dst
->n_div
, return -1);
8544 for (i
= div
; i
< dst
->n_div
; ++i
)
8545 if (isl_seq_eq(dst
->div
[i
], src
->div
[div
], 1+1+total
+div
) &&
8546 isl_seq_first_non_zero(dst
->div
[i
]+1+1+total
+div
,
8547 dst
->n_div
- div
) == -1)
8552 /* Align the divs of "dst" to those of "src", adding divs from "src"
8553 * if needed. That is, make sure that the first src->n_div divs
8554 * of the result are equal to those of src.
8556 * The result is not finalized as by design it will have redundant
8557 * divs if any divs from "src" were copied.
8559 __isl_give isl_basic_map
*isl_basic_map_align_divs(
8560 __isl_take isl_basic_map
*dst
, __isl_keep isl_basic_map
*src
)
8563 int known
, extended
;
8567 return isl_basic_map_free(dst
);
8569 if (src
->n_div
== 0)
8572 known
= isl_basic_map_divs_known(src
);
8574 return isl_basic_map_free(dst
);
8576 isl_die(isl_basic_map_get_ctx(src
), isl_error_invalid
,
8577 "some src divs are unknown",
8578 return isl_basic_map_free(dst
));
8580 src
= isl_basic_map_order_divs(src
);
8583 total
= isl_space_dim(src
->dim
, isl_dim_all
);
8584 for (i
= 0; i
< src
->n_div
; ++i
) {
8585 int j
= find_div(dst
, src
, i
);
8588 int extra
= src
->n_div
- i
;
8589 dst
= isl_basic_map_cow(dst
);
8592 dst
= isl_basic_map_extend_space(dst
,
8593 isl_space_copy(dst
->dim
),
8594 extra
, 0, 2 * extra
);
8597 j
= isl_basic_map_alloc_div(dst
);
8599 return isl_basic_map_free(dst
);
8600 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
+i
);
8601 isl_seq_clr(dst
->div
[j
]+1+1+total
+i
, dst
->n_div
- i
);
8602 if (isl_basic_map_add_div_constraints(dst
, j
) < 0)
8603 return isl_basic_map_free(dst
);
8606 isl_basic_map_swap_div(dst
, i
, j
);
8611 struct isl_basic_set
*isl_basic_set_align_divs(
8612 struct isl_basic_set
*dst
, struct isl_basic_set
*src
)
8614 return bset_from_bmap(isl_basic_map_align_divs(bset_to_bmap(dst
),
8615 bset_to_bmap(src
)));
8618 struct isl_map
*isl_map_align_divs(struct isl_map
*map
)
8626 map
= isl_map_compute_divs(map
);
8627 map
= isl_map_cow(map
);
8631 for (i
= 1; i
< map
->n
; ++i
)
8632 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], map
->p
[i
]);
8633 for (i
= 1; i
< map
->n
; ++i
) {
8634 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], map
->p
[0]);
8636 return isl_map_free(map
);
8639 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
8643 struct isl_set
*isl_set_align_divs(struct isl_set
*set
)
8645 return set_from_map(isl_map_align_divs(set_to_map(set
)));
8648 /* Align the divs of the basic maps in "map" to those
8649 * of the basic maps in "list", as well as to the other basic maps in "map".
8650 * The elements in "list" are assumed to have known divs.
8652 __isl_give isl_map
*isl_map_align_divs_to_basic_map_list(
8653 __isl_take isl_map
*map
, __isl_keep isl_basic_map_list
*list
)
8657 map
= isl_map_compute_divs(map
);
8658 map
= isl_map_cow(map
);
8660 return isl_map_free(map
);
8664 n
= isl_basic_map_list_n_basic_map(list
);
8665 for (i
= 0; i
< n
; ++i
) {
8666 isl_basic_map
*bmap
;
8668 bmap
= isl_basic_map_list_get_basic_map(list
, i
);
8669 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], bmap
);
8670 isl_basic_map_free(bmap
);
8673 return isl_map_free(map
);
8675 return isl_map_align_divs(map
);
8678 /* Align the divs of each element of "list" to those of "bmap".
8679 * Both "bmap" and the elements of "list" are assumed to have known divs.
8681 __isl_give isl_basic_map_list
*isl_basic_map_list_align_divs_to_basic_map(
8682 __isl_take isl_basic_map_list
*list
, __isl_keep isl_basic_map
*bmap
)
8687 return isl_basic_map_list_free(list
);
8689 n
= isl_basic_map_list_n_basic_map(list
);
8690 for (i
= 0; i
< n
; ++i
) {
8691 isl_basic_map
*bmap_i
;
8693 bmap_i
= isl_basic_map_list_get_basic_map(list
, i
);
8694 bmap_i
= isl_basic_map_align_divs(bmap_i
, bmap
);
8695 list
= isl_basic_map_list_set_basic_map(list
, i
, bmap_i
);
8701 static __isl_give isl_set
*set_apply( __isl_take isl_set
*set
,
8702 __isl_take isl_map
*map
)
8706 isl_assert(set
->ctx
, isl_map_compatible_domain(map
, set
), goto error
);
8707 map
= isl_map_intersect_domain(map
, set
);
8708 set
= isl_map_range(map
);
8716 __isl_give isl_set
*isl_set_apply( __isl_take isl_set
*set
,
8717 __isl_take isl_map
*map
)
8719 return isl_map_align_params_map_map_and(set
, map
, &set_apply
);
8722 /* There is no need to cow as removing empty parts doesn't change
8723 * the meaning of the set.
8725 struct isl_map
*isl_map_remove_empty_parts(struct isl_map
*map
)
8732 for (i
= map
->n
- 1; i
>= 0; --i
)
8733 remove_if_empty(map
, i
);
8738 struct isl_set
*isl_set_remove_empty_parts(struct isl_set
*set
)
8740 return set_from_map(isl_map_remove_empty_parts(set_to_map(set
)));
8743 static __isl_give isl_basic_map
*map_copy_basic_map(__isl_keep isl_map
*map
)
8745 struct isl_basic_map
*bmap
;
8746 if (!map
|| map
->n
== 0)
8748 bmap
= map
->p
[map
->n
-1];
8749 isl_assert(map
->ctx
, ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
), return NULL
);
8750 return isl_basic_map_copy(bmap
);
8753 __isl_give isl_basic_map
*isl_map_copy_basic_map(__isl_keep isl_map
*map
)
8755 return map_copy_basic_map(map
);
8758 struct isl_basic_set
*isl_set_copy_basic_set(struct isl_set
*set
)
8760 return bset_from_bmap(map_copy_basic_map(set_to_map(set
)));
8763 static __isl_give isl_map
*map_drop_basic_map(__isl_take isl_map
*map
,
8764 __isl_keep isl_basic_map
*bmap
)
8770 for (i
= map
->n
-1; i
>= 0; --i
) {
8771 if (map
->p
[i
] != bmap
)
8773 map
= isl_map_cow(map
);
8776 isl_basic_map_free(map
->p
[i
]);
8777 if (i
!= map
->n
-1) {
8778 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
8779 map
->p
[i
] = map
->p
[map
->n
-1];
8790 __isl_give isl_map
*isl_map_drop_basic_map(__isl_take isl_map
*map
,
8791 __isl_keep isl_basic_map
*bmap
)
8793 return map_drop_basic_map(map
, bmap
);
8796 struct isl_set
*isl_set_drop_basic_set(struct isl_set
*set
,
8797 struct isl_basic_set
*bset
)
8799 return set_from_map(map_drop_basic_map(set_to_map(set
),
8800 bset_to_bmap(bset
)));
8803 /* Given two basic sets bset1 and bset2, compute the maximal difference
8804 * between the values of dimension pos in bset1 and those in bset2
8805 * for any common value of the parameters and dimensions preceding pos.
8807 static enum isl_lp_result
basic_set_maximal_difference_at(
8808 __isl_keep isl_basic_set
*bset1
, __isl_keep isl_basic_set
*bset2
,
8809 int pos
, isl_int
*opt
)
8811 isl_basic_map
*bmap1
;
8812 isl_basic_map
*bmap2
;
8813 struct isl_ctx
*ctx
;
8814 struct isl_vec
*obj
;
8818 enum isl_lp_result res
;
8820 if (!bset1
|| !bset2
)
8821 return isl_lp_error
;
8823 nparam
= isl_basic_set_n_param(bset1
);
8824 dim1
= isl_basic_set_n_dim(bset1
);
8826 bmap1
= isl_basic_map_from_range(isl_basic_set_copy(bset1
));
8827 bmap2
= isl_basic_map_from_range(isl_basic_set_copy(bset2
));
8828 bmap1
= isl_basic_map_move_dims(bmap1
, isl_dim_in
, 0,
8829 isl_dim_out
, 0, pos
);
8830 bmap2
= isl_basic_map_move_dims(bmap2
, isl_dim_in
, 0,
8831 isl_dim_out
, 0, pos
);
8832 bmap1
= isl_basic_map_range_product(bmap1
, bmap2
);
8834 return isl_lp_error
;
8836 total
= isl_basic_map_total_dim(bmap1
);
8838 obj
= isl_vec_alloc(ctx
, 1 + total
);
8841 isl_seq_clr(obj
->block
.data
, 1 + total
);
8842 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
], 1);
8843 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
+(dim1
-pos
)], -1);
8844 res
= isl_basic_map_solve_lp(bmap1
, 1, obj
->block
.data
, ctx
->one
,
8846 isl_basic_map_free(bmap1
);
8850 isl_basic_map_free(bmap1
);
8851 return isl_lp_error
;
8854 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
8855 * for any common value of the parameters and dimensions preceding pos
8856 * in both basic sets, the values of dimension pos in bset1 are
8857 * smaller or larger than those in bset2.
8860 * 1 if bset1 follows bset2
8861 * -1 if bset1 precedes bset2
8862 * 0 if bset1 and bset2 are incomparable
8863 * -2 if some error occurred.
8865 int isl_basic_set_compare_at(struct isl_basic_set
*bset1
,
8866 struct isl_basic_set
*bset2
, int pos
)
8869 enum isl_lp_result res
;
8874 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
8876 if (res
== isl_lp_empty
)
8878 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
8879 res
== isl_lp_unbounded
)
8881 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
8890 /* Given two basic sets bset1 and bset2, check whether
8891 * for any common value of the parameters and dimensions preceding pos
8892 * there is a value of dimension pos in bset1 that is larger
8893 * than a value of the same dimension in bset2.
8896 * 1 if there exists such a pair
8897 * 0 if there is no such pair, but there is a pair of equal values
8899 * -2 if some error occurred.
8901 int isl_basic_set_follows_at(__isl_keep isl_basic_set
*bset1
,
8902 __isl_keep isl_basic_set
*bset2
, int pos
)
8905 enum isl_lp_result res
;
8910 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
8912 if (res
== isl_lp_empty
)
8914 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
8915 res
== isl_lp_unbounded
)
8917 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
8919 else if (res
== isl_lp_ok
)
8928 /* Given two sets set1 and set2, check whether
8929 * for any common value of the parameters and dimensions preceding pos
8930 * there is a value of dimension pos in set1 that is larger
8931 * than a value of the same dimension in set2.
8934 * 1 if there exists such a pair
8935 * 0 if there is no such pair, but there is a pair of equal values
8937 * -2 if some error occurred.
8939 int isl_set_follows_at(__isl_keep isl_set
*set1
,
8940 __isl_keep isl_set
*set2
, int pos
)
8948 for (i
= 0; i
< set1
->n
; ++i
)
8949 for (j
= 0; j
< set2
->n
; ++j
) {
8951 f
= isl_basic_set_follows_at(set1
->p
[i
], set2
->p
[j
], pos
);
8952 if (f
== 1 || f
== -2)
8961 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map
*bmap
,
8962 unsigned pos
, isl_int
*val
)
8970 total
= isl_basic_map_total_dim(bmap
);
8971 for (i
= 0, d
= total
-1; i
< bmap
->n_eq
&& d
+1 > pos
; ++i
) {
8972 for (; d
+1 > pos
; --d
)
8973 if (!isl_int_is_zero(bmap
->eq
[i
][1+d
]))
8977 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1, d
) != -1)
8979 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1+d
+1, total
-d
-1) != -1)
8981 if (!isl_int_is_one(bmap
->eq
[i
][1+d
]))
8984 isl_int_neg(*val
, bmap
->eq
[i
][0]);
8990 static int isl_map_plain_has_fixed_var(__isl_keep isl_map
*map
,
8991 unsigned pos
, isl_int
*val
)
9003 return isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, val
);
9006 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, &v
);
9007 for (i
= 1; fixed
== 1 && i
< map
->n
; ++i
) {
9008 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[i
], pos
, &tmp
);
9009 if (fixed
== 1 && isl_int_ne(tmp
, v
))
9013 isl_int_set(*val
, v
);
9019 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set
*bset
,
9020 unsigned pos
, isl_int
*val
)
9022 return isl_basic_map_plain_has_fixed_var(bset_to_bmap(bset
),
9026 static int isl_set_plain_has_fixed_var(__isl_keep isl_set
*set
, unsigned pos
,
9029 return isl_map_plain_has_fixed_var(set_to_map(set
), pos
, val
);
9032 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map
*bmap
,
9033 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
9035 if (pos
>= isl_basic_map_dim(bmap
, type
))
9037 return isl_basic_map_plain_has_fixed_var(bmap
,
9038 isl_basic_map_offset(bmap
, type
) - 1 + pos
, val
);
9041 /* If "bmap" obviously lies on a hyperplane where the given dimension
9042 * has a fixed value, then return that value.
9043 * Otherwise return NaN.
9045 __isl_give isl_val
*isl_basic_map_plain_get_val_if_fixed(
9046 __isl_keep isl_basic_map
*bmap
,
9047 enum isl_dim_type type
, unsigned pos
)
9055 ctx
= isl_basic_map_get_ctx(bmap
);
9056 v
= isl_val_alloc(ctx
);
9059 fixed
= isl_basic_map_plain_is_fixed(bmap
, type
, pos
, &v
->n
);
9061 return isl_val_free(v
);
9063 isl_int_set_si(v
->d
, 1);
9067 return isl_val_nan(ctx
);
9070 int isl_map_plain_is_fixed(__isl_keep isl_map
*map
,
9071 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
9073 if (pos
>= isl_map_dim(map
, type
))
9075 return isl_map_plain_has_fixed_var(map
,
9076 map_offset(map
, type
) - 1 + pos
, val
);
9079 /* If "map" obviously lies on a hyperplane where the given dimension
9080 * has a fixed value, then return that value.
9081 * Otherwise return NaN.
9083 __isl_give isl_val
*isl_map_plain_get_val_if_fixed(__isl_keep isl_map
*map
,
9084 enum isl_dim_type type
, unsigned pos
)
9092 ctx
= isl_map_get_ctx(map
);
9093 v
= isl_val_alloc(ctx
);
9096 fixed
= isl_map_plain_is_fixed(map
, type
, pos
, &v
->n
);
9098 return isl_val_free(v
);
9100 isl_int_set_si(v
->d
, 1);
9104 return isl_val_nan(ctx
);
9107 /* If "set" obviously lies on a hyperplane where the given dimension
9108 * has a fixed value, then return that value.
9109 * Otherwise return NaN.
9111 __isl_give isl_val
*isl_set_plain_get_val_if_fixed(__isl_keep isl_set
*set
,
9112 enum isl_dim_type type
, unsigned pos
)
9114 return isl_map_plain_get_val_if_fixed(set
, type
, pos
);
9117 int isl_set_plain_is_fixed(__isl_keep isl_set
*set
,
9118 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
9120 return isl_map_plain_is_fixed(set
, type
, pos
, val
);
9123 /* Check if dimension dim has fixed value and if so and if val is not NULL,
9124 * then return this fixed value in *val.
9126 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set
*bset
,
9127 unsigned dim
, isl_int
*val
)
9129 return isl_basic_set_plain_has_fixed_var(bset
,
9130 isl_basic_set_n_param(bset
) + dim
, val
);
9133 /* Check if dimension dim has fixed value and if so and if val is not NULL,
9134 * then return this fixed value in *val.
9136 int isl_set_plain_dim_is_fixed(__isl_keep isl_set
*set
,
9137 unsigned dim
, isl_int
*val
)
9139 return isl_set_plain_has_fixed_var(set
, isl_set_n_param(set
) + dim
, val
);
9142 /* Check if input variable in has fixed value and if so and if val is not NULL,
9143 * then return this fixed value in *val.
9145 int isl_map_plain_input_is_fixed(__isl_keep isl_map
*map
,
9146 unsigned in
, isl_int
*val
)
9148 return isl_map_plain_has_fixed_var(map
, isl_map_n_param(map
) + in
, val
);
9151 /* Check if dimension dim has an (obvious) fixed lower bound and if so
9152 * and if val is not NULL, then return this lower bound in *val.
9154 int isl_basic_set_plain_dim_has_fixed_lower_bound(
9155 __isl_keep isl_basic_set
*bset
, unsigned dim
, isl_int
*val
)
9157 int i
, i_eq
= -1, i_ineq
= -1;
9164 total
= isl_basic_set_total_dim(bset
);
9165 nparam
= isl_basic_set_n_param(bset
);
9166 for (i
= 0; i
< bset
->n_eq
; ++i
) {
9167 if (isl_int_is_zero(bset
->eq
[i
][1+nparam
+dim
]))
9173 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
9174 if (!isl_int_is_pos(bset
->ineq
[i
][1+nparam
+dim
]))
9176 if (i_eq
!= -1 || i_ineq
!= -1)
9180 if (i_eq
== -1 && i_ineq
== -1)
9182 c
= i_eq
!= -1 ? bset
->eq
[i_eq
] : bset
->ineq
[i_ineq
];
9183 /* The coefficient should always be one due to normalization. */
9184 if (!isl_int_is_one(c
[1+nparam
+dim
]))
9186 if (isl_seq_first_non_zero(c
+1, nparam
+dim
) != -1)
9188 if (isl_seq_first_non_zero(c
+1+nparam
+dim
+1,
9189 total
- nparam
- dim
- 1) != -1)
9192 isl_int_neg(*val
, c
[0]);
9196 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set
*set
,
9197 unsigned dim
, isl_int
*val
)
9209 return isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[0],
9213 fixed
= isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[0],
9215 for (i
= 1; fixed
== 1 && i
< set
->n
; ++i
) {
9216 fixed
= isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[i
],
9218 if (fixed
== 1 && isl_int_ne(tmp
, v
))
9222 isl_int_set(*val
, v
);
9228 /* Return -1 if the constraint "c1" should be sorted before "c2"
9229 * and 1 if it should be sorted after "c2".
9230 * Return 0 if the two constraints are the same (up to the constant term).
9232 * In particular, if a constraint involves later variables than another
9233 * then it is sorted after this other constraint.
9234 * uset_gist depends on constraints without existentially quantified
9235 * variables sorting first.
9237 * For constraints that have the same latest variable, those
9238 * with the same coefficient for this latest variable (first in absolute value
9239 * and then in actual value) are grouped together.
9240 * This is useful for detecting pairs of constraints that can
9241 * be chained in their printed representation.
9243 * Finally, within a group, constraints are sorted according to
9244 * their coefficients (excluding the constant term).
9246 static int sort_constraint_cmp(const void *p1
, const void *p2
, void *arg
)
9248 isl_int
**c1
= (isl_int
**) p1
;
9249 isl_int
**c2
= (isl_int
**) p2
;
9251 unsigned size
= *(unsigned *) arg
;
9254 l1
= isl_seq_last_non_zero(*c1
+ 1, size
);
9255 l2
= isl_seq_last_non_zero(*c2
+ 1, size
);
9260 cmp
= isl_int_abs_cmp((*c1
)[1 + l1
], (*c2
)[1 + l1
]);
9263 cmp
= isl_int_cmp((*c1
)[1 + l1
], (*c2
)[1 + l1
]);
9267 return isl_seq_cmp(*c1
+ 1, *c2
+ 1, size
);
9270 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
9271 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
9272 * and 0 if the two constraints are the same (up to the constant term).
9274 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map
*bmap
,
9275 isl_int
*c1
, isl_int
*c2
)
9281 total
= isl_basic_map_total_dim(bmap
);
9282 return sort_constraint_cmp(&c1
, &c2
, &total
);
9285 __isl_give isl_basic_map
*isl_basic_map_sort_constraints(
9286 __isl_take isl_basic_map
*bmap
)
9292 if (bmap
->n_ineq
== 0)
9294 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED
))
9296 total
= isl_basic_map_total_dim(bmap
);
9297 if (isl_sort(bmap
->ineq
, bmap
->n_ineq
, sizeof(isl_int
*),
9298 &sort_constraint_cmp
, &total
) < 0)
9299 return isl_basic_map_free(bmap
);
9303 __isl_give isl_basic_set
*isl_basic_set_sort_constraints(
9304 __isl_take isl_basic_set
*bset
)
9306 isl_basic_map
*bmap
= bset_to_bmap(bset
);
9307 return bset_from_bmap(isl_basic_map_sort_constraints(bmap
));
9310 struct isl_basic_map
*isl_basic_map_normalize(struct isl_basic_map
*bmap
)
9314 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED
))
9316 bmap
= isl_basic_map_remove_redundancies(bmap
);
9317 bmap
= isl_basic_map_sort_constraints(bmap
);
9319 ISL_F_SET(bmap
, ISL_BASIC_MAP_NORMALIZED
);
9323 struct isl_basic_set
*isl_basic_set_normalize(struct isl_basic_set
*bset
)
9325 return bset_from_bmap(isl_basic_map_normalize(bset_to_bmap(bset
)));
9328 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map
*bmap1
,
9329 const __isl_keep isl_basic_map
*bmap2
)
9334 if (!bmap1
|| !bmap2
)
9339 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) !=
9340 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_RATIONAL
))
9341 return ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) ? -1 : 1;
9342 if (isl_basic_map_n_param(bmap1
) != isl_basic_map_n_param(bmap2
))
9343 return isl_basic_map_n_param(bmap1
) - isl_basic_map_n_param(bmap2
);
9344 if (isl_basic_map_n_in(bmap1
) != isl_basic_map_n_in(bmap2
))
9345 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
9346 if (isl_basic_map_n_out(bmap1
) != isl_basic_map_n_out(bmap2
))
9347 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
9348 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
) &&
9349 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
9351 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
))
9353 if (ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
9355 if (bmap1
->n_eq
!= bmap2
->n_eq
)
9356 return bmap1
->n_eq
- bmap2
->n_eq
;
9357 if (bmap1
->n_ineq
!= bmap2
->n_ineq
)
9358 return bmap1
->n_ineq
- bmap2
->n_ineq
;
9359 if (bmap1
->n_div
!= bmap2
->n_div
)
9360 return bmap1
->n_div
- bmap2
->n_div
;
9361 total
= isl_basic_map_total_dim(bmap1
);
9362 for (i
= 0; i
< bmap1
->n_eq
; ++i
) {
9363 cmp
= isl_seq_cmp(bmap1
->eq
[i
], bmap2
->eq
[i
], 1+total
);
9367 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
9368 cmp
= isl_seq_cmp(bmap1
->ineq
[i
], bmap2
->ineq
[i
], 1+total
);
9372 for (i
= 0; i
< bmap1
->n_div
; ++i
) {
9373 cmp
= isl_seq_cmp(bmap1
->div
[i
], bmap2
->div
[i
], 1+1+total
);
9380 int isl_basic_set_plain_cmp(const __isl_keep isl_basic_set
*bset1
,
9381 const __isl_keep isl_basic_set
*bset2
)
9383 return isl_basic_map_plain_cmp(bset1
, bset2
);
9386 int isl_set_plain_cmp(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
9392 if (set1
->n
!= set2
->n
)
9393 return set1
->n
- set2
->n
;
9395 for (i
= 0; i
< set1
->n
; ++i
) {
9396 cmp
= isl_basic_set_plain_cmp(set1
->p
[i
], set2
->p
[i
]);
9404 isl_bool
isl_basic_map_plain_is_equal(__isl_keep isl_basic_map
*bmap1
,
9405 __isl_keep isl_basic_map
*bmap2
)
9407 if (!bmap1
|| !bmap2
)
9408 return isl_bool_error
;
9409 return isl_basic_map_plain_cmp(bmap1
, bmap2
) == 0;
9412 isl_bool
isl_basic_set_plain_is_equal(__isl_keep isl_basic_set
*bset1
,
9413 __isl_keep isl_basic_set
*bset2
)
9415 return isl_basic_map_plain_is_equal(bset_to_bmap(bset1
),
9416 bset_to_bmap(bset2
));
9419 static int qsort_bmap_cmp(const void *p1
, const void *p2
)
9421 const struct isl_basic_map
*bmap1
= *(const struct isl_basic_map
**)p1
;
9422 const struct isl_basic_map
*bmap2
= *(const struct isl_basic_map
**)p2
;
9424 return isl_basic_map_plain_cmp(bmap1
, bmap2
);
9427 /* Sort the basic maps of "map" and remove duplicate basic maps.
9429 * While removing basic maps, we make sure that the basic maps remain
9430 * sorted because isl_map_normalize expects the basic maps of the result
9433 static __isl_give isl_map
*sort_and_remove_duplicates(__isl_take isl_map
*map
)
9437 map
= isl_map_remove_empty_parts(map
);
9440 qsort(map
->p
, map
->n
, sizeof(struct isl_basic_map
*), qsort_bmap_cmp
);
9441 for (i
= map
->n
- 1; i
>= 1; --i
) {
9442 if (!isl_basic_map_plain_is_equal(map
->p
[i
- 1], map
->p
[i
]))
9444 isl_basic_map_free(map
->p
[i
-1]);
9445 for (j
= i
; j
< map
->n
; ++j
)
9446 map
->p
[j
- 1] = map
->p
[j
];
9453 /* Remove obvious duplicates among the basic maps of "map".
9455 * Unlike isl_map_normalize, this function does not remove redundant
9456 * constraints and only removes duplicates that have exactly the same
9457 * constraints in the input. It does sort the constraints and
9458 * the basic maps to ease the detection of duplicates.
9460 * If "map" has already been normalized or if the basic maps are
9461 * disjoint, then there can be no duplicates.
9463 __isl_give isl_map
*isl_map_remove_obvious_duplicates(__isl_take isl_map
*map
)
9466 isl_basic_map
*bmap
;
9472 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
| ISL_MAP_DISJOINT
))
9474 for (i
= 0; i
< map
->n
; ++i
) {
9475 bmap
= isl_basic_map_copy(map
->p
[i
]);
9476 bmap
= isl_basic_map_sort_constraints(bmap
);
9478 return isl_map_free(map
);
9479 isl_basic_map_free(map
->p
[i
]);
9483 map
= sort_and_remove_duplicates(map
);
9487 /* We normalize in place, but if anything goes wrong we need
9488 * to return NULL, so we need to make sure we don't change the
9489 * meaning of any possible other copies of map.
9491 __isl_give isl_map
*isl_map_normalize(__isl_take isl_map
*map
)
9494 struct isl_basic_map
*bmap
;
9498 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
))
9500 for (i
= 0; i
< map
->n
; ++i
) {
9501 bmap
= isl_basic_map_normalize(isl_basic_map_copy(map
->p
[i
]));
9504 isl_basic_map_free(map
->p
[i
]);
9508 map
= sort_and_remove_duplicates(map
);
9510 ISL_F_SET(map
, ISL_MAP_NORMALIZED
);
9517 struct isl_set
*isl_set_normalize(struct isl_set
*set
)
9519 return set_from_map(isl_map_normalize(set_to_map(set
)));
9522 isl_bool
isl_map_plain_is_equal(__isl_keep isl_map
*map1
,
9523 __isl_keep isl_map
*map2
)
9529 return isl_bool_error
;
9532 return isl_bool_true
;
9533 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
9534 return isl_bool_false
;
9536 map1
= isl_map_copy(map1
);
9537 map2
= isl_map_copy(map2
);
9538 map1
= isl_map_normalize(map1
);
9539 map2
= isl_map_normalize(map2
);
9542 equal
= map1
->n
== map2
->n
;
9543 for (i
= 0; equal
&& i
< map1
->n
; ++i
) {
9544 equal
= isl_basic_map_plain_is_equal(map1
->p
[i
], map2
->p
[i
]);
9554 return isl_bool_error
;
9557 isl_bool
isl_set_plain_is_equal(__isl_keep isl_set
*set1
,
9558 __isl_keep isl_set
*set2
)
9560 return isl_map_plain_is_equal(set_to_map(set1
), set_to_map(set2
));
9563 /* Return an interval that ranges from min to max (inclusive)
9565 struct isl_basic_set
*isl_basic_set_interval(struct isl_ctx
*ctx
,
9566 isl_int min
, isl_int max
)
9569 struct isl_basic_set
*bset
= NULL
;
9571 bset
= isl_basic_set_alloc(ctx
, 0, 1, 0, 0, 2);
9575 k
= isl_basic_set_alloc_inequality(bset
);
9578 isl_int_set_si(bset
->ineq
[k
][1], 1);
9579 isl_int_neg(bset
->ineq
[k
][0], min
);
9581 k
= isl_basic_set_alloc_inequality(bset
);
9584 isl_int_set_si(bset
->ineq
[k
][1], -1);
9585 isl_int_set(bset
->ineq
[k
][0], max
);
9589 isl_basic_set_free(bset
);
9593 /* Return the basic maps in "map" as a list.
9595 __isl_give isl_basic_map_list
*isl_map_get_basic_map_list(
9596 __isl_keep isl_map
*map
)
9600 isl_basic_map_list
*list
;
9604 ctx
= isl_map_get_ctx(map
);
9605 list
= isl_basic_map_list_alloc(ctx
, map
->n
);
9607 for (i
= 0; i
< map
->n
; ++i
) {
9608 isl_basic_map
*bmap
;
9610 bmap
= isl_basic_map_copy(map
->p
[i
]);
9611 list
= isl_basic_map_list_add(list
, bmap
);
9617 /* Return the intersection of the elements in the non-empty list "list".
9618 * All elements are assumed to live in the same space.
9620 __isl_give isl_basic_map
*isl_basic_map_list_intersect(
9621 __isl_take isl_basic_map_list
*list
)
9624 isl_basic_map
*bmap
;
9628 n
= isl_basic_map_list_n_basic_map(list
);
9630 isl_die(isl_basic_map_list_get_ctx(list
), isl_error_invalid
,
9631 "expecting non-empty list", goto error
);
9633 bmap
= isl_basic_map_list_get_basic_map(list
, 0);
9634 for (i
= 1; i
< n
; ++i
) {
9635 isl_basic_map
*bmap_i
;
9637 bmap_i
= isl_basic_map_list_get_basic_map(list
, i
);
9638 bmap
= isl_basic_map_intersect(bmap
, bmap_i
);
9641 isl_basic_map_list_free(list
);
9644 isl_basic_map_list_free(list
);
9648 /* Return the intersection of the elements in the non-empty list "list".
9649 * All elements are assumed to live in the same space.
9651 __isl_give isl_basic_set
*isl_basic_set_list_intersect(
9652 __isl_take isl_basic_set_list
*list
)
9654 return isl_basic_map_list_intersect(list
);
9657 /* Return the union of the elements of "list".
9658 * The list is required to have at least one element.
9660 __isl_give isl_set
*isl_basic_set_list_union(
9661 __isl_take isl_basic_set_list
*list
)
9665 isl_basic_set
*bset
;
9670 n
= isl_basic_set_list_n_basic_set(list
);
9672 isl_die(isl_basic_set_list_get_ctx(list
), isl_error_invalid
,
9673 "expecting non-empty list", goto error
);
9675 bset
= isl_basic_set_list_get_basic_set(list
, 0);
9676 space
= isl_basic_set_get_space(bset
);
9677 isl_basic_set_free(bset
);
9679 set
= isl_set_alloc_space(space
, n
, 0);
9680 for (i
= 0; i
< n
; ++i
) {
9681 bset
= isl_basic_set_list_get_basic_set(list
, i
);
9682 set
= isl_set_add_basic_set(set
, bset
);
9685 isl_basic_set_list_free(list
);
9688 isl_basic_set_list_free(list
);
9692 /* Return the union of the elements in the non-empty list "list".
9693 * All elements are assumed to live in the same space.
9695 __isl_give isl_set
*isl_set_list_union(__isl_take isl_set_list
*list
)
9702 n
= isl_set_list_n_set(list
);
9704 isl_die(isl_set_list_get_ctx(list
), isl_error_invalid
,
9705 "expecting non-empty list", goto error
);
9707 set
= isl_set_list_get_set(list
, 0);
9708 for (i
= 1; i
< n
; ++i
) {
9711 set_i
= isl_set_list_get_set(list
, i
);
9712 set
= isl_set_union(set
, set_i
);
9715 isl_set_list_free(list
);
9718 isl_set_list_free(list
);
9722 /* Return the Cartesian product of the basic sets in list (in the given order).
9724 __isl_give isl_basic_set
*isl_basic_set_list_product(
9725 __isl_take
struct isl_basic_set_list
*list
)
9733 struct isl_basic_set
*product
= NULL
;
9737 isl_assert(list
->ctx
, list
->n
> 0, goto error
);
9738 isl_assert(list
->ctx
, list
->p
[0], goto error
);
9739 nparam
= isl_basic_set_n_param(list
->p
[0]);
9740 dim
= isl_basic_set_n_dim(list
->p
[0]);
9741 extra
= list
->p
[0]->n_div
;
9742 n_eq
= list
->p
[0]->n_eq
;
9743 n_ineq
= list
->p
[0]->n_ineq
;
9744 for (i
= 1; i
< list
->n
; ++i
) {
9745 isl_assert(list
->ctx
, list
->p
[i
], goto error
);
9746 isl_assert(list
->ctx
,
9747 nparam
== isl_basic_set_n_param(list
->p
[i
]), goto error
);
9748 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
9749 extra
+= list
->p
[i
]->n_div
;
9750 n_eq
+= list
->p
[i
]->n_eq
;
9751 n_ineq
+= list
->p
[i
]->n_ineq
;
9753 product
= isl_basic_set_alloc(list
->ctx
, nparam
, dim
, extra
,
9758 for (i
= 0; i
< list
->n
; ++i
) {
9759 isl_basic_set_add_constraints(product
,
9760 isl_basic_set_copy(list
->p
[i
]), dim
);
9761 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
9763 isl_basic_set_list_free(list
);
9766 isl_basic_set_free(product
);
9767 isl_basic_set_list_free(list
);
9771 struct isl_basic_map
*isl_basic_map_product(
9772 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
9774 isl_space
*dim_result
= NULL
;
9775 struct isl_basic_map
*bmap
;
9776 unsigned in1
, in2
, out1
, out2
, nparam
, total
, pos
;
9777 struct isl_dim_map
*dim_map1
, *dim_map2
;
9779 if (!bmap1
|| !bmap2
)
9782 isl_assert(bmap1
->ctx
, isl_space_match(bmap1
->dim
, isl_dim_param
,
9783 bmap2
->dim
, isl_dim_param
), goto error
);
9784 dim_result
= isl_space_product(isl_space_copy(bmap1
->dim
),
9785 isl_space_copy(bmap2
->dim
));
9787 in1
= isl_basic_map_n_in(bmap1
);
9788 in2
= isl_basic_map_n_in(bmap2
);
9789 out1
= isl_basic_map_n_out(bmap1
);
9790 out2
= isl_basic_map_n_out(bmap2
);
9791 nparam
= isl_basic_map_n_param(bmap1
);
9793 total
= nparam
+ in1
+ in2
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
9794 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9795 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9796 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9797 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9798 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9799 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
9800 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
9801 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
9802 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
9803 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9805 bmap
= isl_basic_map_alloc_space(dim_result
,
9806 bmap1
->n_div
+ bmap2
->n_div
,
9807 bmap1
->n_eq
+ bmap2
->n_eq
,
9808 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9809 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9810 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9811 bmap
= isl_basic_map_simplify(bmap
);
9812 return isl_basic_map_finalize(bmap
);
9814 isl_basic_map_free(bmap1
);
9815 isl_basic_map_free(bmap2
);
9819 __isl_give isl_basic_map
*isl_basic_map_flat_product(
9820 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9822 isl_basic_map
*prod
;
9824 prod
= isl_basic_map_product(bmap1
, bmap2
);
9825 prod
= isl_basic_map_flatten(prod
);
9829 __isl_give isl_basic_set
*isl_basic_set_flat_product(
9830 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
9832 return isl_basic_map_flat_range_product(bset1
, bset2
);
9835 __isl_give isl_basic_map
*isl_basic_map_domain_product(
9836 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9838 isl_space
*space_result
= NULL
;
9839 isl_basic_map
*bmap
;
9840 unsigned in1
, in2
, out
, nparam
, total
, pos
;
9841 struct isl_dim_map
*dim_map1
, *dim_map2
;
9843 if (!bmap1
|| !bmap2
)
9846 space_result
= isl_space_domain_product(isl_space_copy(bmap1
->dim
),
9847 isl_space_copy(bmap2
->dim
));
9849 in1
= isl_basic_map_dim(bmap1
, isl_dim_in
);
9850 in2
= isl_basic_map_dim(bmap2
, isl_dim_in
);
9851 out
= isl_basic_map_dim(bmap1
, isl_dim_out
);
9852 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
9854 total
= nparam
+ in1
+ in2
+ out
+ bmap1
->n_div
+ bmap2
->n_div
;
9855 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9856 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9857 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9858 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9859 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9860 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
9861 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
9862 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
);
9863 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out
);
9864 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9866 bmap
= isl_basic_map_alloc_space(space_result
,
9867 bmap1
->n_div
+ bmap2
->n_div
,
9868 bmap1
->n_eq
+ bmap2
->n_eq
,
9869 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9870 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9871 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9872 bmap
= isl_basic_map_simplify(bmap
);
9873 return isl_basic_map_finalize(bmap
);
9875 isl_basic_map_free(bmap1
);
9876 isl_basic_map_free(bmap2
);
9880 __isl_give isl_basic_map
*isl_basic_map_range_product(
9881 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9884 isl_space
*dim_result
= NULL
;
9885 isl_basic_map
*bmap
;
9886 unsigned in
, out1
, out2
, nparam
, total
, pos
;
9887 struct isl_dim_map
*dim_map1
, *dim_map2
;
9889 rational
= isl_basic_map_is_rational(bmap1
);
9890 if (rational
>= 0 && rational
)
9891 rational
= isl_basic_map_is_rational(bmap2
);
9892 if (!bmap1
|| !bmap2
|| rational
< 0)
9895 if (!isl_space_match(bmap1
->dim
, isl_dim_param
,
9896 bmap2
->dim
, isl_dim_param
))
9897 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
9898 "parameters don't match", goto error
);
9900 dim_result
= isl_space_range_product(isl_space_copy(bmap1
->dim
),
9901 isl_space_copy(bmap2
->dim
));
9903 in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
9904 out1
= isl_basic_map_n_out(bmap1
);
9905 out2
= isl_basic_map_n_out(bmap2
);
9906 nparam
= isl_basic_map_n_param(bmap1
);
9908 total
= nparam
+ in
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
9909 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9910 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9911 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9912 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9913 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9914 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
9915 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in
);
9916 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
9917 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
9918 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9920 bmap
= isl_basic_map_alloc_space(dim_result
,
9921 bmap1
->n_div
+ bmap2
->n_div
,
9922 bmap1
->n_eq
+ bmap2
->n_eq
,
9923 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9924 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9925 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9927 bmap
= isl_basic_map_set_rational(bmap
);
9928 bmap
= isl_basic_map_simplify(bmap
);
9929 return isl_basic_map_finalize(bmap
);
9931 isl_basic_map_free(bmap1
);
9932 isl_basic_map_free(bmap2
);
9936 __isl_give isl_basic_map
*isl_basic_map_flat_range_product(
9937 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9939 isl_basic_map
*prod
;
9941 prod
= isl_basic_map_range_product(bmap1
, bmap2
);
9942 prod
= isl_basic_map_flatten_range(prod
);
9946 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
9947 * and collect the results.
9948 * The result live in the space obtained by calling "space_product"
9949 * on the spaces of "map1" and "map2".
9950 * If "remove_duplicates" is set then the result may contain duplicates
9951 * (even if the inputs do not) and so we try and remove the obvious
9954 static __isl_give isl_map
*map_product(__isl_take isl_map
*map1
,
9955 __isl_take isl_map
*map2
,
9956 __isl_give isl_space
*(*space_product
)(__isl_take isl_space
*left
,
9957 __isl_take isl_space
*right
),
9958 __isl_give isl_basic_map
*(*basic_map_product
)(
9959 __isl_take isl_basic_map
*left
,
9960 __isl_take isl_basic_map
*right
),
9961 int remove_duplicates
)
9964 struct isl_map
*result
;
9970 isl_assert(map1
->ctx
, isl_space_match(map1
->dim
, isl_dim_param
,
9971 map2
->dim
, isl_dim_param
), goto error
);
9973 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
9974 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
9975 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
9977 result
= isl_map_alloc_space(space_product(isl_space_copy(map1
->dim
),
9978 isl_space_copy(map2
->dim
)),
9979 map1
->n
* map2
->n
, flags
);
9982 for (i
= 0; i
< map1
->n
; ++i
)
9983 for (j
= 0; j
< map2
->n
; ++j
) {
9984 struct isl_basic_map
*part
;
9985 part
= basic_map_product(isl_basic_map_copy(map1
->p
[i
]),
9986 isl_basic_map_copy(map2
->p
[j
]));
9987 if (isl_basic_map_is_empty(part
))
9988 isl_basic_map_free(part
);
9990 result
= isl_map_add_basic_map(result
, part
);
9994 if (remove_duplicates
)
9995 result
= isl_map_remove_obvious_duplicates(result
);
10000 isl_map_free(map1
);
10001 isl_map_free(map2
);
10005 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
10007 static __isl_give isl_map
*map_product_aligned(__isl_take isl_map
*map1
,
10008 __isl_take isl_map
*map2
)
10010 return map_product(map1
, map2
, &isl_space_product
,
10011 &isl_basic_map_product
, 0);
10014 __isl_give isl_map
*isl_map_product(__isl_take isl_map
*map1
,
10015 __isl_take isl_map
*map2
)
10017 return isl_map_align_params_map_map_and(map1
, map2
, &map_product_aligned
);
10020 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
10022 __isl_give isl_map
*isl_map_flat_product(__isl_take isl_map
*map1
,
10023 __isl_take isl_map
*map2
)
10027 prod
= isl_map_product(map1
, map2
);
10028 prod
= isl_map_flatten(prod
);
10032 /* Given two set A and B, construct its Cartesian product A x B.
10034 struct isl_set
*isl_set_product(struct isl_set
*set1
, struct isl_set
*set2
)
10036 return isl_map_range_product(set1
, set2
);
10039 __isl_give isl_set
*isl_set_flat_product(__isl_take isl_set
*set1
,
10040 __isl_take isl_set
*set2
)
10042 return isl_map_flat_range_product(set1
, set2
);
10045 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
10047 static __isl_give isl_map
*map_domain_product_aligned(__isl_take isl_map
*map1
,
10048 __isl_take isl_map
*map2
)
10050 return map_product(map1
, map2
, &isl_space_domain_product
,
10051 &isl_basic_map_domain_product
, 1);
10054 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
10056 static __isl_give isl_map
*map_range_product_aligned(__isl_take isl_map
*map1
,
10057 __isl_take isl_map
*map2
)
10059 return map_product(map1
, map2
, &isl_space_range_product
,
10060 &isl_basic_map_range_product
, 1);
10063 __isl_give isl_map
*isl_map_domain_product(__isl_take isl_map
*map1
,
10064 __isl_take isl_map
*map2
)
10066 return isl_map_align_params_map_map_and(map1
, map2
,
10067 &map_domain_product_aligned
);
10070 __isl_give isl_map
*isl_map_range_product(__isl_take isl_map
*map1
,
10071 __isl_take isl_map
*map2
)
10073 return isl_map_align_params_map_map_and(map1
, map2
,
10074 &map_range_product_aligned
);
10077 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
10079 __isl_give isl_map
*isl_map_factor_domain(__isl_take isl_map
*map
)
10082 int total1
, keep1
, total2
, keep2
;
10086 if (!isl_space_domain_is_wrapping(map
->dim
) ||
10087 !isl_space_range_is_wrapping(map
->dim
))
10088 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10089 "not a product", return isl_map_free(map
));
10091 space
= isl_map_get_space(map
);
10092 total1
= isl_space_dim(space
, isl_dim_in
);
10093 total2
= isl_space_dim(space
, isl_dim_out
);
10094 space
= isl_space_factor_domain(space
);
10095 keep1
= isl_space_dim(space
, isl_dim_in
);
10096 keep2
= isl_space_dim(space
, isl_dim_out
);
10097 map
= isl_map_project_out(map
, isl_dim_in
, keep1
, total1
- keep1
);
10098 map
= isl_map_project_out(map
, isl_dim_out
, keep2
, total2
- keep2
);
10099 map
= isl_map_reset_space(map
, space
);
10104 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
10106 __isl_give isl_map
*isl_map_factor_range(__isl_take isl_map
*map
)
10109 int total1
, keep1
, total2
, keep2
;
10113 if (!isl_space_domain_is_wrapping(map
->dim
) ||
10114 !isl_space_range_is_wrapping(map
->dim
))
10115 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10116 "not a product", return isl_map_free(map
));
10118 space
= isl_map_get_space(map
);
10119 total1
= isl_space_dim(space
, isl_dim_in
);
10120 total2
= isl_space_dim(space
, isl_dim_out
);
10121 space
= isl_space_factor_range(space
);
10122 keep1
= isl_space_dim(space
, isl_dim_in
);
10123 keep2
= isl_space_dim(space
, isl_dim_out
);
10124 map
= isl_map_project_out(map
, isl_dim_in
, 0, total1
- keep1
);
10125 map
= isl_map_project_out(map
, isl_dim_out
, 0, total2
- keep2
);
10126 map
= isl_map_reset_space(map
, space
);
10131 /* Given a map of the form [A -> B] -> C, return the map A -> C.
10133 __isl_give isl_map
*isl_map_domain_factor_domain(__isl_take isl_map
*map
)
10140 if (!isl_space_domain_is_wrapping(map
->dim
))
10141 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10142 "domain is not a product", return isl_map_free(map
));
10144 space
= isl_map_get_space(map
);
10145 total
= isl_space_dim(space
, isl_dim_in
);
10146 space
= isl_space_domain_factor_domain(space
);
10147 keep
= isl_space_dim(space
, isl_dim_in
);
10148 map
= isl_map_project_out(map
, isl_dim_in
, keep
, total
- keep
);
10149 map
= isl_map_reset_space(map
, space
);
10154 /* Given a map of the form [A -> B] -> C, return the map B -> C.
10156 __isl_give isl_map
*isl_map_domain_factor_range(__isl_take isl_map
*map
)
10163 if (!isl_space_domain_is_wrapping(map
->dim
))
10164 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10165 "domain is not a product", return isl_map_free(map
));
10167 space
= isl_map_get_space(map
);
10168 total
= isl_space_dim(space
, isl_dim_in
);
10169 space
= isl_space_domain_factor_range(space
);
10170 keep
= isl_space_dim(space
, isl_dim_in
);
10171 map
= isl_map_project_out(map
, isl_dim_in
, 0, total
- keep
);
10172 map
= isl_map_reset_space(map
, space
);
10177 /* Given a map A -> [B -> C], extract the map A -> B.
10179 __isl_give isl_map
*isl_map_range_factor_domain(__isl_take isl_map
*map
)
10186 if (!isl_space_range_is_wrapping(map
->dim
))
10187 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10188 "range is not a product", return isl_map_free(map
));
10190 space
= isl_map_get_space(map
);
10191 total
= isl_space_dim(space
, isl_dim_out
);
10192 space
= isl_space_range_factor_domain(space
);
10193 keep
= isl_space_dim(space
, isl_dim_out
);
10194 map
= isl_map_project_out(map
, isl_dim_out
, keep
, total
- keep
);
10195 map
= isl_map_reset_space(map
, space
);
10200 /* Given a map A -> [B -> C], extract the map A -> C.
10202 __isl_give isl_map
*isl_map_range_factor_range(__isl_take isl_map
*map
)
10209 if (!isl_space_range_is_wrapping(map
->dim
))
10210 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10211 "range is not a product", return isl_map_free(map
));
10213 space
= isl_map_get_space(map
);
10214 total
= isl_space_dim(space
, isl_dim_out
);
10215 space
= isl_space_range_factor_range(space
);
10216 keep
= isl_space_dim(space
, isl_dim_out
);
10217 map
= isl_map_project_out(map
, isl_dim_out
, 0, total
- keep
);
10218 map
= isl_map_reset_space(map
, space
);
10223 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
10225 __isl_give isl_map
*isl_map_flat_domain_product(__isl_take isl_map
*map1
,
10226 __isl_take isl_map
*map2
)
10230 prod
= isl_map_domain_product(map1
, map2
);
10231 prod
= isl_map_flatten_domain(prod
);
10235 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
10237 __isl_give isl_map
*isl_map_flat_range_product(__isl_take isl_map
*map1
,
10238 __isl_take isl_map
*map2
)
10242 prod
= isl_map_range_product(map1
, map2
);
10243 prod
= isl_map_flatten_range(prod
);
10247 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map
*bmap
)
10250 uint32_t hash
= isl_hash_init();
10255 bmap
= isl_basic_map_copy(bmap
);
10256 bmap
= isl_basic_map_normalize(bmap
);
10259 total
= isl_basic_map_total_dim(bmap
);
10260 isl_hash_byte(hash
, bmap
->n_eq
& 0xFF);
10261 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
10263 c_hash
= isl_seq_get_hash(bmap
->eq
[i
], 1 + total
);
10264 isl_hash_hash(hash
, c_hash
);
10266 isl_hash_byte(hash
, bmap
->n_ineq
& 0xFF);
10267 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
10269 c_hash
= isl_seq_get_hash(bmap
->ineq
[i
], 1 + total
);
10270 isl_hash_hash(hash
, c_hash
);
10272 isl_hash_byte(hash
, bmap
->n_div
& 0xFF);
10273 for (i
= 0; i
< bmap
->n_div
; ++i
) {
10275 if (isl_int_is_zero(bmap
->div
[i
][0]))
10277 isl_hash_byte(hash
, i
& 0xFF);
10278 c_hash
= isl_seq_get_hash(bmap
->div
[i
], 1 + 1 + total
);
10279 isl_hash_hash(hash
, c_hash
);
10281 isl_basic_map_free(bmap
);
10285 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set
*bset
)
10287 return isl_basic_map_get_hash(bset_to_bmap(bset
));
10290 uint32_t isl_map_get_hash(__isl_keep isl_map
*map
)
10297 map
= isl_map_copy(map
);
10298 map
= isl_map_normalize(map
);
10302 hash
= isl_hash_init();
10303 for (i
= 0; i
< map
->n
; ++i
) {
10304 uint32_t bmap_hash
;
10305 bmap_hash
= isl_basic_map_get_hash(map
->p
[i
]);
10306 isl_hash_hash(hash
, bmap_hash
);
10314 uint32_t isl_set_get_hash(__isl_keep isl_set
*set
)
10316 return isl_map_get_hash(set_to_map(set
));
10319 /* Check if the value for dimension dim is completely determined
10320 * by the values of the other parameters and variables.
10321 * That is, check if dimension dim is involved in an equality.
10323 int isl_basic_set_dim_is_unique(struct isl_basic_set
*bset
, unsigned dim
)
10330 nparam
= isl_basic_set_n_param(bset
);
10331 for (i
= 0; i
< bset
->n_eq
; ++i
)
10332 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ dim
]))
10337 /* Check if the value for dimension dim is completely determined
10338 * by the values of the other parameters and variables.
10339 * That is, check if dimension dim is involved in an equality
10340 * for each of the subsets.
10342 int isl_set_dim_is_unique(struct isl_set
*set
, unsigned dim
)
10348 for (i
= 0; i
< set
->n
; ++i
) {
10350 unique
= isl_basic_set_dim_is_unique(set
->p
[i
], dim
);
10357 /* Return the number of basic maps in the (current) representation of "map".
10359 int isl_map_n_basic_map(__isl_keep isl_map
*map
)
10361 return map
? map
->n
: 0;
10364 int isl_set_n_basic_set(__isl_keep isl_set
*set
)
10366 return set
? set
->n
: 0;
10369 isl_stat
isl_map_foreach_basic_map(__isl_keep isl_map
*map
,
10370 isl_stat (*fn
)(__isl_take isl_basic_map
*bmap
, void *user
), void *user
)
10375 return isl_stat_error
;
10377 for (i
= 0; i
< map
->n
; ++i
)
10378 if (fn(isl_basic_map_copy(map
->p
[i
]), user
) < 0)
10379 return isl_stat_error
;
10381 return isl_stat_ok
;
10384 isl_stat
isl_set_foreach_basic_set(__isl_keep isl_set
*set
,
10385 isl_stat (*fn
)(__isl_take isl_basic_set
*bset
, void *user
), void *user
)
10390 return isl_stat_error
;
10392 for (i
= 0; i
< set
->n
; ++i
)
10393 if (fn(isl_basic_set_copy(set
->p
[i
]), user
) < 0)
10394 return isl_stat_error
;
10396 return isl_stat_ok
;
10399 /* Return a list of basic sets, the union of which is equal to "set".
10401 __isl_give isl_basic_set_list
*isl_set_get_basic_set_list(
10402 __isl_keep isl_set
*set
)
10405 isl_basic_set_list
*list
;
10410 list
= isl_basic_set_list_alloc(isl_set_get_ctx(set
), set
->n
);
10411 for (i
= 0; i
< set
->n
; ++i
) {
10412 isl_basic_set
*bset
;
10414 bset
= isl_basic_set_copy(set
->p
[i
]);
10415 list
= isl_basic_set_list_add(list
, bset
);
10421 __isl_give isl_basic_set
*isl_basic_set_lift(__isl_take isl_basic_set
*bset
)
10428 bset
= isl_basic_set_cow(bset
);
10432 dim
= isl_basic_set_get_space(bset
);
10433 dim
= isl_space_lift(dim
, bset
->n_div
);
10436 isl_space_free(bset
->dim
);
10438 bset
->extra
-= bset
->n_div
;
10441 bset
= isl_basic_set_finalize(bset
);
10445 isl_basic_set_free(bset
);
10449 __isl_give isl_set
*isl_set_lift(__isl_take isl_set
*set
)
10455 set
= isl_set_align_divs(set
);
10460 set
= isl_set_cow(set
);
10464 n_div
= set
->p
[0]->n_div
;
10465 dim
= isl_set_get_space(set
);
10466 dim
= isl_space_lift(dim
, n_div
);
10469 isl_space_free(set
->dim
);
10472 for (i
= 0; i
< set
->n
; ++i
) {
10473 set
->p
[i
] = isl_basic_set_lift(set
->p
[i
]);
10484 __isl_give isl_map
*isl_set_lifting(__isl_take isl_set
*set
)
10487 struct isl_basic_map
*bmap
;
10494 set
= isl_set_align_divs(set
);
10499 dim
= isl_set_get_space(set
);
10500 if (set
->n
== 0 || set
->p
[0]->n_div
== 0) {
10502 return isl_map_identity(isl_space_map_from_set(dim
));
10505 n_div
= set
->p
[0]->n_div
;
10506 dim
= isl_space_map_from_set(dim
);
10507 n_param
= isl_space_dim(dim
, isl_dim_param
);
10508 n_set
= isl_space_dim(dim
, isl_dim_in
);
10509 dim
= isl_space_extend(dim
, n_param
, n_set
, n_set
+ n_div
);
10510 bmap
= isl_basic_map_alloc_space(dim
, 0, n_set
, 2 * n_div
);
10511 for (i
= 0; i
< n_set
; ++i
)
10512 bmap
= var_equal(bmap
, i
);
10514 total
= n_param
+ n_set
+ n_set
+ n_div
;
10515 for (i
= 0; i
< n_div
; ++i
) {
10516 k
= isl_basic_map_alloc_inequality(bmap
);
10519 isl_seq_cpy(bmap
->ineq
[k
], set
->p
[0]->div
[i
]+1, 1+n_param
);
10520 isl_seq_clr(bmap
->ineq
[k
]+1+n_param
, n_set
);
10521 isl_seq_cpy(bmap
->ineq
[k
]+1+n_param
+n_set
,
10522 set
->p
[0]->div
[i
]+1+1+n_param
, n_set
+ n_div
);
10523 isl_int_neg(bmap
->ineq
[k
][1+n_param
+n_set
+n_set
+i
],
10524 set
->p
[0]->div
[i
][0]);
10526 l
= isl_basic_map_alloc_inequality(bmap
);
10529 isl_seq_neg(bmap
->ineq
[l
], bmap
->ineq
[k
], 1 + total
);
10530 isl_int_add(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0],
10531 set
->p
[0]->div
[i
][0]);
10532 isl_int_sub_ui(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], 1);
10536 bmap
= isl_basic_map_simplify(bmap
);
10537 bmap
= isl_basic_map_finalize(bmap
);
10538 return isl_map_from_basic_map(bmap
);
10541 isl_basic_map_free(bmap
);
10545 int isl_basic_set_size(__isl_keep isl_basic_set
*bset
)
10553 dim
= isl_basic_set_total_dim(bset
);
10554 size
+= bset
->n_eq
* (1 + dim
);
10555 size
+= bset
->n_ineq
* (1 + dim
);
10556 size
+= bset
->n_div
* (2 + dim
);
10561 int isl_set_size(__isl_keep isl_set
*set
)
10569 for (i
= 0; i
< set
->n
; ++i
)
10570 size
+= isl_basic_set_size(set
->p
[i
]);
10575 /* Check if there is any lower bound (if lower == 0) and/or upper
10576 * bound (if upper == 0) on the specified dim.
10578 static isl_bool
basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
10579 enum isl_dim_type type
, unsigned pos
, int lower
, int upper
)
10583 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
10584 return isl_bool_error
;
10586 pos
+= isl_basic_map_offset(bmap
, type
);
10588 for (i
= 0; i
< bmap
->n_div
; ++i
) {
10589 if (isl_int_is_zero(bmap
->div
[i
][0]))
10591 if (!isl_int_is_zero(bmap
->div
[i
][1 + pos
]))
10592 return isl_bool_true
;
10595 for (i
= 0; i
< bmap
->n_eq
; ++i
)
10596 if (!isl_int_is_zero(bmap
->eq
[i
][pos
]))
10597 return isl_bool_true
;
10599 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
10600 int sgn
= isl_int_sgn(bmap
->ineq
[i
][pos
]);
10607 return lower
&& upper
;
10610 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
10611 enum isl_dim_type type
, unsigned pos
)
10613 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 0);
10616 isl_bool
isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map
*bmap
,
10617 enum isl_dim_type type
, unsigned pos
)
10619 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 1);
10622 isl_bool
isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map
*bmap
,
10623 enum isl_dim_type type
, unsigned pos
)
10625 return basic_map_dim_is_bounded(bmap
, type
, pos
, 1, 0);
10628 int isl_map_dim_is_bounded(__isl_keep isl_map
*map
,
10629 enum isl_dim_type type
, unsigned pos
)
10636 for (i
= 0; i
< map
->n
; ++i
) {
10638 bounded
= isl_basic_map_dim_is_bounded(map
->p
[i
], type
, pos
);
10639 if (bounded
< 0 || !bounded
)
10646 /* Return 1 if the specified dim is involved in both an upper bound
10647 * and a lower bound.
10649 int isl_set_dim_is_bounded(__isl_keep isl_set
*set
,
10650 enum isl_dim_type type
, unsigned pos
)
10652 return isl_map_dim_is_bounded(set_to_map(set
), type
, pos
);
10655 /* Does "map" have a bound (according to "fn") for any of its basic maps?
10657 static isl_bool
has_any_bound(__isl_keep isl_map
*map
,
10658 enum isl_dim_type type
, unsigned pos
,
10659 isl_bool (*fn
)(__isl_keep isl_basic_map
*bmap
,
10660 enum isl_dim_type type
, unsigned pos
))
10665 return isl_bool_error
;
10667 for (i
= 0; i
< map
->n
; ++i
) {
10669 bounded
= fn(map
->p
[i
], type
, pos
);
10670 if (bounded
< 0 || bounded
)
10674 return isl_bool_false
;
10677 /* Return 1 if the specified dim is involved in any lower bound.
10679 isl_bool
isl_set_dim_has_any_lower_bound(__isl_keep isl_set
*set
,
10680 enum isl_dim_type type
, unsigned pos
)
10682 return has_any_bound(set
, type
, pos
,
10683 &isl_basic_map_dim_has_lower_bound
);
10686 /* Return 1 if the specified dim is involved in any upper bound.
10688 isl_bool
isl_set_dim_has_any_upper_bound(__isl_keep isl_set
*set
,
10689 enum isl_dim_type type
, unsigned pos
)
10691 return has_any_bound(set
, type
, pos
,
10692 &isl_basic_map_dim_has_upper_bound
);
10695 /* Does "map" have a bound (according to "fn") for all of its basic maps?
10697 static isl_bool
has_bound(__isl_keep isl_map
*map
,
10698 enum isl_dim_type type
, unsigned pos
,
10699 isl_bool (*fn
)(__isl_keep isl_basic_map
*bmap
,
10700 enum isl_dim_type type
, unsigned pos
))
10705 return isl_bool_error
;
10707 for (i
= 0; i
< map
->n
; ++i
) {
10709 bounded
= fn(map
->p
[i
], type
, pos
);
10710 if (bounded
< 0 || !bounded
)
10714 return isl_bool_true
;
10717 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10719 isl_bool
isl_set_dim_has_lower_bound(__isl_keep isl_set
*set
,
10720 enum isl_dim_type type
, unsigned pos
)
10722 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_lower_bound
);
10725 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10727 isl_bool
isl_set_dim_has_upper_bound(__isl_keep isl_set
*set
,
10728 enum isl_dim_type type
, unsigned pos
)
10730 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_upper_bound
);
10733 /* For each of the "n" variables starting at "first", determine
10734 * the sign of the variable and put the results in the first "n"
10735 * elements of the array "signs".
10737 * 1 means that the variable is non-negative
10738 * -1 means that the variable is non-positive
10739 * 0 means the variable attains both positive and negative values.
10741 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set
*bset
,
10742 unsigned first
, unsigned n
, int *signs
)
10744 isl_vec
*bound
= NULL
;
10745 struct isl_tab
*tab
= NULL
;
10746 struct isl_tab_undo
*snap
;
10749 if (!bset
|| !signs
)
10752 bound
= isl_vec_alloc(bset
->ctx
, 1 + isl_basic_set_total_dim(bset
));
10753 tab
= isl_tab_from_basic_set(bset
, 0);
10754 if (!bound
|| !tab
)
10757 isl_seq_clr(bound
->el
, bound
->size
);
10758 isl_int_set_si(bound
->el
[0], -1);
10760 snap
= isl_tab_snap(tab
);
10761 for (i
= 0; i
< n
; ++i
) {
10764 isl_int_set_si(bound
->el
[1 + first
+ i
], -1);
10765 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
10767 empty
= tab
->empty
;
10768 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
10769 if (isl_tab_rollback(tab
, snap
) < 0)
10777 isl_int_set_si(bound
->el
[1 + first
+ i
], 1);
10778 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
10780 empty
= tab
->empty
;
10781 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
10782 if (isl_tab_rollback(tab
, snap
) < 0)
10785 signs
[i
] = empty
? -1 : 0;
10789 isl_vec_free(bound
);
10793 isl_vec_free(bound
);
10797 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set
*bset
,
10798 enum isl_dim_type type
, unsigned first
, unsigned n
, int *signs
)
10800 if (!bset
|| !signs
)
10802 isl_assert(bset
->ctx
, first
+ n
<= isl_basic_set_dim(bset
, type
),
10805 first
+= pos(bset
->dim
, type
) - 1;
10806 return isl_basic_set_vars_get_sign(bset
, first
, n
, signs
);
10809 /* Is it possible for the integer division "div" to depend (possibly
10810 * indirectly) on any output dimensions?
10812 * If the div is undefined, then we conservatively assume that it
10813 * may depend on them.
10814 * Otherwise, we check if it actually depends on them or on any integer
10815 * divisions that may depend on them.
10817 static int div_may_involve_output(__isl_keep isl_basic_map
*bmap
, int div
)
10820 unsigned n_out
, o_out
;
10821 unsigned n_div
, o_div
;
10823 if (isl_int_is_zero(bmap
->div
[div
][0]))
10826 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10827 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10829 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + o_out
, n_out
) != -1)
10832 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10833 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10835 for (i
= 0; i
< n_div
; ++i
) {
10836 if (isl_int_is_zero(bmap
->div
[div
][1 + o_div
+ i
]))
10838 if (div_may_involve_output(bmap
, i
))
10845 /* Return the first integer division of "bmap" in the range
10846 * [first, first + n[ that may depend on any output dimensions and
10847 * that has a non-zero coefficient in "c" (where the first coefficient
10848 * in "c" corresponds to integer division "first").
10850 static int first_div_may_involve_output(__isl_keep isl_basic_map
*bmap
,
10851 isl_int
*c
, int first
, int n
)
10858 for (k
= first
; k
< first
+ n
; ++k
) {
10859 if (isl_int_is_zero(c
[k
]))
10861 if (div_may_involve_output(bmap
, k
))
10868 /* Look for a pair of inequality constraints in "bmap" of the form
10870 * -l + i >= 0 or i >= l
10872 * n + l - i >= 0 or i <= l + n
10874 * with n < "m" and i the output dimension at position "pos".
10875 * (Note that n >= 0 as otherwise the two constraints would conflict.)
10876 * Furthermore, "l" is only allowed to involve parameters, input dimensions
10877 * and earlier output dimensions, as well as integer divisions that do
10878 * not involve any of the output dimensions.
10880 * Return the index of the first inequality constraint or bmap->n_ineq
10881 * if no such pair can be found.
10883 static int find_modulo_constraint_pair(__isl_keep isl_basic_map
*bmap
,
10884 int pos
, isl_int m
)
10889 unsigned n_div
, o_div
;
10890 unsigned n_out
, o_out
;
10896 ctx
= isl_basic_map_get_ctx(bmap
);
10897 total
= isl_basic_map_total_dim(bmap
);
10898 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10899 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10900 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10901 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10902 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
10903 if (!isl_int_abs_eq(bmap
->ineq
[i
][o_out
+ pos
], ctx
->one
))
10905 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + o_out
+ pos
+ 1,
10906 n_out
- (pos
+ 1)) != -1)
10908 if (first_div_may_involve_output(bmap
, bmap
->ineq
[i
] + o_div
,
10911 for (j
= i
+ 1; j
< bmap
->n_ineq
; ++j
) {
10912 if (!isl_int_abs_eq(bmap
->ineq
[j
][o_out
+ pos
],
10915 if (!isl_seq_is_neg(bmap
->ineq
[i
] + 1,
10916 bmap
->ineq
[j
] + 1, total
))
10920 if (j
>= bmap
->n_ineq
)
10922 isl_int_add(bmap
->ineq
[i
][0],
10923 bmap
->ineq
[i
][0], bmap
->ineq
[j
][0]);
10924 less
= isl_int_abs_lt(bmap
->ineq
[i
][0], m
);
10925 isl_int_sub(bmap
->ineq
[i
][0],
10926 bmap
->ineq
[i
][0], bmap
->ineq
[j
][0]);
10929 if (isl_int_is_one(bmap
->ineq
[i
][o_out
+ pos
]))
10935 return bmap
->n_ineq
;
10938 /* Return the index of the equality of "bmap" that defines
10939 * the output dimension "pos" in terms of earlier dimensions.
10940 * The equality may also involve integer divisions, as long
10941 * as those integer divisions are defined in terms of
10942 * parameters or input dimensions.
10943 * In this case, *div is set to the number of integer divisions and
10944 * *ineq is set to the number of inequality constraints (provided
10945 * div and ineq are not NULL).
10947 * The equality may also involve a single integer division involving
10948 * the output dimensions (typically only output dimension "pos") as
10949 * long as the coefficient of output dimension "pos" is 1 or -1 and
10950 * there is a pair of constraints i >= l and i <= l + n, with i referring
10951 * to output dimension "pos", l an expression involving only earlier
10952 * dimensions and n smaller than the coefficient of the integer division
10953 * in the equality. In this case, the output dimension can be defined
10954 * in terms of a modulo expression that does not involve the integer division.
10955 * *div is then set to this single integer division and
10956 * *ineq is set to the index of constraint i >= l.
10958 * Return bmap->n_eq if there is no such equality.
10959 * Return -1 on error.
10961 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map
*bmap
,
10962 int pos
, int *div
, int *ineq
)
10965 unsigned n_out
, o_out
;
10966 unsigned n_div
, o_div
;
10971 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10972 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10973 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10974 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10977 *ineq
= bmap
->n_ineq
;
10980 for (j
= 0; j
< bmap
->n_eq
; ++j
) {
10981 if (isl_int_is_zero(bmap
->eq
[j
][o_out
+ pos
]))
10983 if (isl_seq_first_non_zero(bmap
->eq
[j
] + o_out
+ pos
+ 1,
10984 n_out
- (pos
+ 1)) != -1)
10986 k
= first_div_may_involve_output(bmap
, bmap
->eq
[j
] + o_div
,
10990 if (!isl_int_is_one(bmap
->eq
[j
][o_out
+ pos
]) &&
10991 !isl_int_is_negone(bmap
->eq
[j
][o_out
+ pos
]))
10993 if (first_div_may_involve_output(bmap
, bmap
->eq
[j
] + o_div
,
10994 k
+ 1, n_div
- (k
+1)) < n_div
)
10996 l
= find_modulo_constraint_pair(bmap
, pos
,
10997 bmap
->eq
[j
][o_div
+ k
]);
11000 if (l
>= bmap
->n_ineq
)
11012 /* Check if the given basic map is obviously single-valued.
11013 * In particular, for each output dimension, check that there is
11014 * an equality that defines the output dimension in terms of
11015 * earlier dimensions.
11017 isl_bool
isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map
*bmap
)
11023 return isl_bool_error
;
11025 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
11027 for (i
= 0; i
< n_out
; ++i
) {
11030 eq
= isl_basic_map_output_defining_equality(bmap
, i
,
11033 return isl_bool_error
;
11034 if (eq
>= bmap
->n_eq
)
11035 return isl_bool_false
;
11038 return isl_bool_true
;
11041 /* Check if the given basic map is single-valued.
11042 * We simply compute
11046 * and check if the result is a subset of the identity mapping.
11048 isl_bool
isl_basic_map_is_single_valued(__isl_keep isl_basic_map
*bmap
)
11051 isl_basic_map
*test
;
11055 sv
= isl_basic_map_plain_is_single_valued(bmap
);
11059 test
= isl_basic_map_reverse(isl_basic_map_copy(bmap
));
11060 test
= isl_basic_map_apply_range(test
, isl_basic_map_copy(bmap
));
11062 space
= isl_basic_map_get_space(bmap
);
11063 space
= isl_space_map_from_set(isl_space_range(space
));
11064 id
= isl_basic_map_identity(space
);
11066 sv
= isl_basic_map_is_subset(test
, id
);
11068 isl_basic_map_free(test
);
11069 isl_basic_map_free(id
);
11074 /* Check if the given map is obviously single-valued.
11076 isl_bool
isl_map_plain_is_single_valued(__isl_keep isl_map
*map
)
11079 return isl_bool_error
;
11081 return isl_bool_true
;
11083 return isl_bool_false
;
11085 return isl_basic_map_plain_is_single_valued(map
->p
[0]);
11088 /* Check if the given map is single-valued.
11089 * We simply compute
11093 * and check if the result is a subset of the identity mapping.
11095 isl_bool
isl_map_is_single_valued(__isl_keep isl_map
*map
)
11102 sv
= isl_map_plain_is_single_valued(map
);
11106 test
= isl_map_reverse(isl_map_copy(map
));
11107 test
= isl_map_apply_range(test
, isl_map_copy(map
));
11109 dim
= isl_space_map_from_set(isl_space_range(isl_map_get_space(map
)));
11110 id
= isl_map_identity(dim
);
11112 sv
= isl_map_is_subset(test
, id
);
11114 isl_map_free(test
);
11120 isl_bool
isl_map_is_injective(__isl_keep isl_map
*map
)
11124 map
= isl_map_copy(map
);
11125 map
= isl_map_reverse(map
);
11126 in
= isl_map_is_single_valued(map
);
11132 /* Check if the given map is obviously injective.
11134 isl_bool
isl_map_plain_is_injective(__isl_keep isl_map
*map
)
11138 map
= isl_map_copy(map
);
11139 map
= isl_map_reverse(map
);
11140 in
= isl_map_plain_is_single_valued(map
);
11146 isl_bool
isl_map_is_bijective(__isl_keep isl_map
*map
)
11150 sv
= isl_map_is_single_valued(map
);
11154 return isl_map_is_injective(map
);
11157 isl_bool
isl_set_is_singleton(__isl_keep isl_set
*set
)
11159 return isl_map_is_single_valued(set_to_map(set
));
11162 /* Does "map" only map elements to themselves?
11164 * If the domain and range spaces are different, then "map"
11165 * is considered not to be an identity relation, even if it is empty.
11166 * Otherwise, construct the maximal identity relation and
11167 * check whether "map" is a subset of this relation.
11169 isl_bool
isl_map_is_identity(__isl_keep isl_map
*map
)
11173 isl_bool equal
, is_identity
;
11175 space
= isl_map_get_space(map
);
11176 equal
= isl_space_tuple_is_equal(space
, isl_dim_in
, space
, isl_dim_out
);
11177 isl_space_free(space
);
11178 if (equal
< 0 || !equal
)
11181 id
= isl_map_identity(isl_map_get_space(map
));
11182 is_identity
= isl_map_is_subset(map
, id
);
11185 return is_identity
;
11188 int isl_map_is_translation(__isl_keep isl_map
*map
)
11193 delta
= isl_map_deltas(isl_map_copy(map
));
11194 ok
= isl_set_is_singleton(delta
);
11195 isl_set_free(delta
);
11200 static int unique(isl_int
*p
, unsigned pos
, unsigned len
)
11202 if (isl_seq_first_non_zero(p
, pos
) != -1)
11204 if (isl_seq_first_non_zero(p
+ pos
+ 1, len
- pos
- 1) != -1)
11209 int isl_basic_set_is_box(__isl_keep isl_basic_set
*bset
)
11218 if (isl_basic_set_dim(bset
, isl_dim_div
) != 0)
11221 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
11222 ovar
= isl_space_offset(bset
->dim
, isl_dim_set
);
11223 for (j
= 0; j
< nvar
; ++j
) {
11224 int lower
= 0, upper
= 0;
11225 for (i
= 0; i
< bset
->n_eq
; ++i
) {
11226 if (isl_int_is_zero(bset
->eq
[i
][1 + ovar
+ j
]))
11228 if (!unique(bset
->eq
[i
] + 1 + ovar
, j
, nvar
))
11232 if (i
< bset
->n_eq
)
11234 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
11235 if (isl_int_is_zero(bset
->ineq
[i
][1 + ovar
+ j
]))
11237 if (!unique(bset
->ineq
[i
] + 1 + ovar
, j
, nvar
))
11239 if (isl_int_is_pos(bset
->ineq
[i
][1 + ovar
+ j
]))
11244 if (!lower
|| !upper
)
11251 int isl_set_is_box(__isl_keep isl_set
*set
)
11258 return isl_basic_set_is_box(set
->p
[0]);
11261 isl_bool
isl_basic_set_is_wrapping(__isl_keep isl_basic_set
*bset
)
11264 return isl_bool_error
;
11266 return isl_space_is_wrapping(bset
->dim
);
11269 isl_bool
isl_set_is_wrapping(__isl_keep isl_set
*set
)
11272 return isl_bool_error
;
11274 return isl_space_is_wrapping(set
->dim
);
11277 /* Modify the space of "map" through a call to "change".
11278 * If "can_change" is set (not NULL), then first call it to check
11279 * if the modification is allowed, printing the error message "cannot_change"
11282 static __isl_give isl_map
*isl_map_change_space(__isl_take isl_map
*map
,
11283 isl_bool (*can_change
)(__isl_keep isl_map
*map
),
11284 const char *cannot_change
,
11285 __isl_give isl_space
*(*change
)(__isl_take isl_space
*space
))
11293 ok
= can_change
? can_change(map
) : isl_bool_true
;
11295 return isl_map_free(map
);
11297 isl_die(isl_map_get_ctx(map
), isl_error_invalid
, cannot_change
,
11298 return isl_map_free(map
));
11300 space
= change(isl_map_get_space(map
));
11301 map
= isl_map_reset_space(map
, space
);
11306 /* Is the domain of "map" a wrapped relation?
11308 isl_bool
isl_map_domain_is_wrapping(__isl_keep isl_map
*map
)
11311 return isl_bool_error
;
11313 return isl_space_domain_is_wrapping(map
->dim
);
11316 /* Is the range of "map" a wrapped relation?
11318 isl_bool
isl_map_range_is_wrapping(__isl_keep isl_map
*map
)
11321 return isl_bool_error
;
11323 return isl_space_range_is_wrapping(map
->dim
);
11326 __isl_give isl_basic_set
*isl_basic_map_wrap(__isl_take isl_basic_map
*bmap
)
11328 bmap
= isl_basic_map_cow(bmap
);
11332 bmap
->dim
= isl_space_wrap(bmap
->dim
);
11336 bmap
= isl_basic_map_finalize(bmap
);
11338 return bset_from_bmap(bmap
);
11340 isl_basic_map_free(bmap
);
11344 /* Given a map A -> B, return the set (A -> B).
11346 __isl_give isl_set
*isl_map_wrap(__isl_take isl_map
*map
)
11348 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_wrap
);
11351 __isl_give isl_basic_map
*isl_basic_set_unwrap(__isl_take isl_basic_set
*bset
)
11353 bset
= isl_basic_set_cow(bset
);
11357 bset
->dim
= isl_space_unwrap(bset
->dim
);
11361 bset
= isl_basic_set_finalize(bset
);
11363 return bset_to_bmap(bset
);
11365 isl_basic_set_free(bset
);
11369 /* Given a set (A -> B), return the map A -> B.
11370 * Error out if "set" is not of the form (A -> B).
11372 __isl_give isl_map
*isl_set_unwrap(__isl_take isl_set
*set
)
11374 return isl_map_change_space(set
, &isl_set_is_wrapping
,
11375 "not a wrapping set", &isl_space_unwrap
);
11378 __isl_give isl_basic_map
*isl_basic_map_reset(__isl_take isl_basic_map
*bmap
,
11379 enum isl_dim_type type
)
11384 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
11387 bmap
= isl_basic_map_cow(bmap
);
11391 bmap
->dim
= isl_space_reset(bmap
->dim
, type
);
11395 bmap
= isl_basic_map_finalize(bmap
);
11399 isl_basic_map_free(bmap
);
11403 __isl_give isl_map
*isl_map_reset(__isl_take isl_map
*map
,
11404 enum isl_dim_type type
)
11411 if (!isl_space_is_named_or_nested(map
->dim
, type
))
11414 map
= isl_map_cow(map
);
11418 for (i
= 0; i
< map
->n
; ++i
) {
11419 map
->p
[i
] = isl_basic_map_reset(map
->p
[i
], type
);
11423 map
->dim
= isl_space_reset(map
->dim
, type
);
11433 __isl_give isl_basic_map
*isl_basic_map_flatten(__isl_take isl_basic_map
*bmap
)
11438 if (!bmap
->dim
->nested
[0] && !bmap
->dim
->nested
[1])
11441 bmap
= isl_basic_map_cow(bmap
);
11445 bmap
->dim
= isl_space_flatten(bmap
->dim
);
11449 bmap
= isl_basic_map_finalize(bmap
);
11453 isl_basic_map_free(bmap
);
11457 __isl_give isl_basic_set
*isl_basic_set_flatten(__isl_take isl_basic_set
*bset
)
11459 return bset_from_bmap(isl_basic_map_flatten(bset_to_bmap(bset
)));
11462 __isl_give isl_basic_map
*isl_basic_map_flatten_domain(
11463 __isl_take isl_basic_map
*bmap
)
11468 if (!bmap
->dim
->nested
[0])
11471 bmap
= isl_basic_map_cow(bmap
);
11475 bmap
->dim
= isl_space_flatten_domain(bmap
->dim
);
11479 bmap
= isl_basic_map_finalize(bmap
);
11483 isl_basic_map_free(bmap
);
11487 __isl_give isl_basic_map
*isl_basic_map_flatten_range(
11488 __isl_take isl_basic_map
*bmap
)
11493 if (!bmap
->dim
->nested
[1])
11496 bmap
= isl_basic_map_cow(bmap
);
11500 bmap
->dim
= isl_space_flatten_range(bmap
->dim
);
11504 bmap
= isl_basic_map_finalize(bmap
);
11508 isl_basic_map_free(bmap
);
11512 /* Remove any internal structure from the spaces of domain and range of "map".
11514 __isl_give isl_map
*isl_map_flatten(__isl_take isl_map
*map
)
11519 if (!map
->dim
->nested
[0] && !map
->dim
->nested
[1])
11522 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten
);
11525 __isl_give isl_set
*isl_set_flatten(__isl_take isl_set
*set
)
11527 return set_from_map(isl_map_flatten(set_to_map(set
)));
11530 __isl_give isl_map
*isl_set_flatten_map(__isl_take isl_set
*set
)
11532 isl_space
*dim
, *flat_dim
;
11535 dim
= isl_set_get_space(set
);
11536 flat_dim
= isl_space_flatten(isl_space_copy(dim
));
11537 map
= isl_map_identity(isl_space_join(isl_space_reverse(dim
), flat_dim
));
11538 map
= isl_map_intersect_domain(map
, set
);
11543 /* Remove any internal structure from the space of the domain of "map".
11545 __isl_give isl_map
*isl_map_flatten_domain(__isl_take isl_map
*map
)
11550 if (!map
->dim
->nested
[0])
11553 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten_domain
);
11556 /* Remove any internal structure from the space of the range of "map".
11558 __isl_give isl_map
*isl_map_flatten_range(__isl_take isl_map
*map
)
11563 if (!map
->dim
->nested
[1])
11566 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten_range
);
11569 /* Reorder the dimensions of "bmap" according to the given dim_map
11570 * and set the dimension specification to "dim" and
11571 * perform Gaussian elimination on the result.
11573 __isl_give isl_basic_map
*isl_basic_map_realign(__isl_take isl_basic_map
*bmap
,
11574 __isl_take isl_space
*dim
, __isl_take
struct isl_dim_map
*dim_map
)
11576 isl_basic_map
*res
;
11579 bmap
= isl_basic_map_cow(bmap
);
11580 if (!bmap
|| !dim
|| !dim_map
)
11583 flags
= bmap
->flags
;
11584 ISL_FL_CLR(flags
, ISL_BASIC_MAP_FINAL
);
11585 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED
);
11586 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
11587 res
= isl_basic_map_alloc_space(dim
,
11588 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
11589 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
11591 res
->flags
= flags
;
11592 res
= isl_basic_map_gauss(res
, NULL
);
11593 res
= isl_basic_map_finalize(res
);
11597 isl_basic_map_free(bmap
);
11598 isl_space_free(dim
);
11602 /* Reorder the dimensions of "map" according to given reordering.
11604 __isl_give isl_map
*isl_map_realign(__isl_take isl_map
*map
,
11605 __isl_take isl_reordering
*r
)
11608 struct isl_dim_map
*dim_map
;
11610 map
= isl_map_cow(map
);
11611 dim_map
= isl_dim_map_from_reordering(r
);
11612 if (!map
|| !r
|| !dim_map
)
11615 for (i
= 0; i
< map
->n
; ++i
) {
11616 struct isl_dim_map
*dim_map_i
;
11618 dim_map_i
= isl_dim_map_extend(dim_map
, map
->p
[i
]);
11620 map
->p
[i
] = isl_basic_map_realign(map
->p
[i
],
11621 isl_space_copy(r
->dim
), dim_map_i
);
11627 map
= isl_map_reset_space(map
, isl_space_copy(r
->dim
));
11629 isl_reordering_free(r
);
11635 isl_reordering_free(r
);
11639 __isl_give isl_set
*isl_set_realign(__isl_take isl_set
*set
,
11640 __isl_take isl_reordering
*r
)
11642 return set_from_map(isl_map_realign(set_to_map(set
), r
));
11645 __isl_give isl_map
*isl_map_align_params(__isl_take isl_map
*map
,
11646 __isl_take isl_space
*model
)
11650 if (!map
|| !model
)
11653 ctx
= isl_space_get_ctx(model
);
11654 if (!isl_space_has_named_params(model
))
11655 isl_die(ctx
, isl_error_invalid
,
11656 "model has unnamed parameters", goto error
);
11657 if (!isl_space_has_named_params(map
->dim
))
11658 isl_die(ctx
, isl_error_invalid
,
11659 "relation has unnamed parameters", goto error
);
11660 if (!isl_space_match(map
->dim
, isl_dim_param
, model
, isl_dim_param
)) {
11661 isl_reordering
*exp
;
11663 model
= isl_space_drop_dims(model
, isl_dim_in
,
11664 0, isl_space_dim(model
, isl_dim_in
));
11665 model
= isl_space_drop_dims(model
, isl_dim_out
,
11666 0, isl_space_dim(model
, isl_dim_out
));
11667 exp
= isl_parameter_alignment_reordering(map
->dim
, model
);
11668 exp
= isl_reordering_extend_space(exp
, isl_map_get_space(map
));
11669 map
= isl_map_realign(map
, exp
);
11672 isl_space_free(model
);
11675 isl_space_free(model
);
11680 __isl_give isl_set
*isl_set_align_params(__isl_take isl_set
*set
,
11681 __isl_take isl_space
*model
)
11683 return isl_map_align_params(set
, model
);
11686 /* Align the parameters of "bmap" to those of "model", introducing
11687 * additional parameters if needed.
11689 __isl_give isl_basic_map
*isl_basic_map_align_params(
11690 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*model
)
11694 if (!bmap
|| !model
)
11697 ctx
= isl_space_get_ctx(model
);
11698 if (!isl_space_has_named_params(model
))
11699 isl_die(ctx
, isl_error_invalid
,
11700 "model has unnamed parameters", goto error
);
11701 if (!isl_space_has_named_params(bmap
->dim
))
11702 isl_die(ctx
, isl_error_invalid
,
11703 "relation has unnamed parameters", goto error
);
11704 if (!isl_space_match(bmap
->dim
, isl_dim_param
, model
, isl_dim_param
)) {
11705 isl_reordering
*exp
;
11706 struct isl_dim_map
*dim_map
;
11708 model
= isl_space_drop_dims(model
, isl_dim_in
,
11709 0, isl_space_dim(model
, isl_dim_in
));
11710 model
= isl_space_drop_dims(model
, isl_dim_out
,
11711 0, isl_space_dim(model
, isl_dim_out
));
11712 exp
= isl_parameter_alignment_reordering(bmap
->dim
, model
);
11713 exp
= isl_reordering_extend_space(exp
,
11714 isl_basic_map_get_space(bmap
));
11715 dim_map
= isl_dim_map_from_reordering(exp
);
11716 bmap
= isl_basic_map_realign(bmap
,
11717 exp
? isl_space_copy(exp
->dim
) : NULL
,
11718 isl_dim_map_extend(dim_map
, bmap
));
11719 isl_reordering_free(exp
);
11723 isl_space_free(model
);
11726 isl_space_free(model
);
11727 isl_basic_map_free(bmap
);
11731 /* Align the parameters of "bset" to those of "model", introducing
11732 * additional parameters if needed.
11734 __isl_give isl_basic_set
*isl_basic_set_align_params(
11735 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*model
)
11737 return isl_basic_map_align_params(bset
, model
);
11740 __isl_give isl_mat
*isl_basic_map_equalities_matrix(
11741 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
11742 enum isl_dim_type c2
, enum isl_dim_type c3
,
11743 enum isl_dim_type c4
, enum isl_dim_type c5
)
11745 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11746 struct isl_mat
*mat
;
11752 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_eq
,
11753 isl_basic_map_total_dim(bmap
) + 1);
11756 for (i
= 0; i
< bmap
->n_eq
; ++i
)
11757 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11758 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11759 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11760 isl_int_set(mat
->row
[i
][pos
],
11761 bmap
->eq
[i
][off
+ k
]);
11769 __isl_give isl_mat
*isl_basic_map_inequalities_matrix(
11770 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
11771 enum isl_dim_type c2
, enum isl_dim_type c3
,
11772 enum isl_dim_type c4
, enum isl_dim_type c5
)
11774 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11775 struct isl_mat
*mat
;
11781 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_ineq
,
11782 isl_basic_map_total_dim(bmap
) + 1);
11785 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
11786 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11787 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11788 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11789 isl_int_set(mat
->row
[i
][pos
],
11790 bmap
->ineq
[i
][off
+ k
]);
11798 __isl_give isl_basic_map
*isl_basic_map_from_constraint_matrices(
11799 __isl_take isl_space
*dim
,
11800 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
11801 enum isl_dim_type c2
, enum isl_dim_type c3
,
11802 enum isl_dim_type c4
, enum isl_dim_type c5
)
11804 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11805 isl_basic_map
*bmap
;
11811 if (!dim
|| !eq
|| !ineq
)
11814 if (eq
->n_col
!= ineq
->n_col
)
11815 isl_die(dim
->ctx
, isl_error_invalid
,
11816 "equalities and inequalities matrices should have "
11817 "same number of columns", goto error
);
11819 total
= 1 + isl_space_dim(dim
, isl_dim_all
);
11821 if (eq
->n_col
< total
)
11822 isl_die(dim
->ctx
, isl_error_invalid
,
11823 "number of columns too small", goto error
);
11825 extra
= eq
->n_col
- total
;
11827 bmap
= isl_basic_map_alloc_space(isl_space_copy(dim
), extra
,
11828 eq
->n_row
, ineq
->n_row
);
11831 for (i
= 0; i
< extra
; ++i
) {
11832 k
= isl_basic_map_alloc_div(bmap
);
11835 isl_int_set_si(bmap
->div
[k
][0], 0);
11837 for (i
= 0; i
< eq
->n_row
; ++i
) {
11838 l
= isl_basic_map_alloc_equality(bmap
);
11841 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11842 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11843 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11844 isl_int_set(bmap
->eq
[l
][off
+ k
],
11850 for (i
= 0; i
< ineq
->n_row
; ++i
) {
11851 l
= isl_basic_map_alloc_inequality(bmap
);
11854 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11855 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11856 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11857 isl_int_set(bmap
->ineq
[l
][off
+ k
],
11858 ineq
->row
[i
][pos
]);
11864 isl_space_free(dim
);
11866 isl_mat_free(ineq
);
11868 bmap
= isl_basic_map_simplify(bmap
);
11869 return isl_basic_map_finalize(bmap
);
11871 isl_space_free(dim
);
11873 isl_mat_free(ineq
);
11877 __isl_give isl_mat
*isl_basic_set_equalities_matrix(
11878 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
11879 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
11881 return isl_basic_map_equalities_matrix(bset_to_bmap(bset
),
11882 c1
, c2
, c3
, c4
, isl_dim_in
);
11885 __isl_give isl_mat
*isl_basic_set_inequalities_matrix(
11886 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
11887 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
11889 return isl_basic_map_inequalities_matrix(bset_to_bmap(bset
),
11890 c1
, c2
, c3
, c4
, isl_dim_in
);
11893 __isl_give isl_basic_set
*isl_basic_set_from_constraint_matrices(
11894 __isl_take isl_space
*dim
,
11895 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
11896 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
11898 isl_basic_map
*bmap
;
11899 bmap
= isl_basic_map_from_constraint_matrices(dim
, eq
, ineq
,
11900 c1
, c2
, c3
, c4
, isl_dim_in
);
11901 return bset_from_bmap(bmap
);
11904 isl_bool
isl_basic_map_can_zip(__isl_keep isl_basic_map
*bmap
)
11907 return isl_bool_error
;
11909 return isl_space_can_zip(bmap
->dim
);
11912 isl_bool
isl_map_can_zip(__isl_keep isl_map
*map
)
11915 return isl_bool_error
;
11917 return isl_space_can_zip(map
->dim
);
11920 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
11921 * (A -> C) -> (B -> D).
11923 __isl_give isl_basic_map
*isl_basic_map_zip(__isl_take isl_basic_map
*bmap
)
11932 if (!isl_basic_map_can_zip(bmap
))
11933 isl_die(bmap
->ctx
, isl_error_invalid
,
11934 "basic map cannot be zipped", goto error
);
11935 pos
= isl_basic_map_offset(bmap
, isl_dim_in
) +
11936 isl_space_dim(bmap
->dim
->nested
[0], isl_dim_in
);
11937 n1
= isl_space_dim(bmap
->dim
->nested
[0], isl_dim_out
);
11938 n2
= isl_space_dim(bmap
->dim
->nested
[1], isl_dim_in
);
11939 bmap
= isl_basic_map_cow(bmap
);
11940 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
11943 bmap
->dim
= isl_space_zip(bmap
->dim
);
11946 bmap
= isl_basic_map_mark_final(bmap
);
11949 isl_basic_map_free(bmap
);
11953 /* Given a map (A -> B) -> (C -> D), return the corresponding map
11954 * (A -> C) -> (B -> D).
11956 __isl_give isl_map
*isl_map_zip(__isl_take isl_map
*map
)
11963 if (!isl_map_can_zip(map
))
11964 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be zipped",
11967 map
= isl_map_cow(map
);
11971 for (i
= 0; i
< map
->n
; ++i
) {
11972 map
->p
[i
] = isl_basic_map_zip(map
->p
[i
]);
11977 map
->dim
= isl_space_zip(map
->dim
);
11987 /* Can we apply isl_basic_map_curry to "bmap"?
11988 * That is, does it have a nested relation in its domain?
11990 isl_bool
isl_basic_map_can_curry(__isl_keep isl_basic_map
*bmap
)
11993 return isl_bool_error
;
11995 return isl_space_can_curry(bmap
->dim
);
11998 /* Can we apply isl_map_curry to "map"?
11999 * That is, does it have a nested relation in its domain?
12001 isl_bool
isl_map_can_curry(__isl_keep isl_map
*map
)
12004 return isl_bool_error
;
12006 return isl_space_can_curry(map
->dim
);
12009 /* Given a basic map (A -> B) -> C, return the corresponding basic map
12012 __isl_give isl_basic_map
*isl_basic_map_curry(__isl_take isl_basic_map
*bmap
)
12018 if (!isl_basic_map_can_curry(bmap
))
12019 isl_die(bmap
->ctx
, isl_error_invalid
,
12020 "basic map cannot be curried", goto error
);
12021 bmap
= isl_basic_map_cow(bmap
);
12024 bmap
->dim
= isl_space_curry(bmap
->dim
);
12027 bmap
= isl_basic_map_mark_final(bmap
);
12030 isl_basic_map_free(bmap
);
12034 /* Given a map (A -> B) -> C, return the corresponding map
12037 __isl_give isl_map
*isl_map_curry(__isl_take isl_map
*map
)
12039 return isl_map_change_space(map
, &isl_map_can_curry
,
12040 "map cannot be curried", &isl_space_curry
);
12043 /* Can isl_map_range_curry be applied to "map"?
12044 * That is, does it have a nested relation in its range,
12045 * the domain of which is itself a nested relation?
12047 isl_bool
isl_map_can_range_curry(__isl_keep isl_map
*map
)
12050 return isl_bool_error
;
12052 return isl_space_can_range_curry(map
->dim
);
12055 /* Given a map A -> ((B -> C) -> D), return the corresponding map
12056 * A -> (B -> (C -> D)).
12058 __isl_give isl_map
*isl_map_range_curry(__isl_take isl_map
*map
)
12060 return isl_map_change_space(map
, &isl_map_can_range_curry
,
12061 "map range cannot be curried",
12062 &isl_space_range_curry
);
12065 /* Can we apply isl_basic_map_uncurry to "bmap"?
12066 * That is, does it have a nested relation in its domain?
12068 isl_bool
isl_basic_map_can_uncurry(__isl_keep isl_basic_map
*bmap
)
12071 return isl_bool_error
;
12073 return isl_space_can_uncurry(bmap
->dim
);
12076 /* Can we apply isl_map_uncurry to "map"?
12077 * That is, does it have a nested relation in its domain?
12079 isl_bool
isl_map_can_uncurry(__isl_keep isl_map
*map
)
12082 return isl_bool_error
;
12084 return isl_space_can_uncurry(map
->dim
);
12087 /* Given a basic map A -> (B -> C), return the corresponding basic map
12090 __isl_give isl_basic_map
*isl_basic_map_uncurry(__isl_take isl_basic_map
*bmap
)
12096 if (!isl_basic_map_can_uncurry(bmap
))
12097 isl_die(bmap
->ctx
, isl_error_invalid
,
12098 "basic map cannot be uncurried",
12099 return isl_basic_map_free(bmap
));
12100 bmap
= isl_basic_map_cow(bmap
);
12103 bmap
->dim
= isl_space_uncurry(bmap
->dim
);
12105 return isl_basic_map_free(bmap
);
12106 bmap
= isl_basic_map_mark_final(bmap
);
12110 /* Given a map A -> (B -> C), return the corresponding map
12113 __isl_give isl_map
*isl_map_uncurry(__isl_take isl_map
*map
)
12115 return isl_map_change_space(map
, &isl_map_can_uncurry
,
12116 "map cannot be uncurried", &isl_space_uncurry
);
12119 /* Construct a basic map mapping the domain of the affine expression
12120 * to a one-dimensional range prescribed by the affine expression.
12121 * If "rational" is set, then construct a rational basic map.
12123 * A NaN affine expression cannot be converted to a basic map.
12125 static __isl_give isl_basic_map
*isl_basic_map_from_aff2(
12126 __isl_take isl_aff
*aff
, int rational
)
12131 isl_local_space
*ls
;
12132 isl_basic_map
*bmap
= NULL
;
12136 is_nan
= isl_aff_is_nan(aff
);
12140 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
12141 "cannot convert NaN", goto error
);
12143 ls
= isl_aff_get_local_space(aff
);
12144 bmap
= isl_basic_map_from_local_space(ls
);
12145 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
12146 k
= isl_basic_map_alloc_equality(bmap
);
12150 pos
= isl_basic_map_offset(bmap
, isl_dim_out
);
12151 isl_seq_cpy(bmap
->eq
[k
], aff
->v
->el
+ 1, pos
);
12152 isl_int_neg(bmap
->eq
[k
][pos
], aff
->v
->el
[0]);
12153 isl_seq_cpy(bmap
->eq
[k
] + pos
+ 1, aff
->v
->el
+ 1 + pos
,
12154 aff
->v
->size
- (pos
+ 1));
12158 bmap
= isl_basic_map_set_rational(bmap
);
12159 bmap
= isl_basic_map_finalize(bmap
);
12163 isl_basic_map_free(bmap
);
12167 /* Construct a basic map mapping the domain of the affine expression
12168 * to a one-dimensional range prescribed by the affine expression.
12170 __isl_give isl_basic_map
*isl_basic_map_from_aff(__isl_take isl_aff
*aff
)
12172 return isl_basic_map_from_aff2(aff
, 0);
12175 /* Construct a map mapping the domain of the affine expression
12176 * to a one-dimensional range prescribed by the affine expression.
12178 __isl_give isl_map
*isl_map_from_aff(__isl_take isl_aff
*aff
)
12180 isl_basic_map
*bmap
;
12182 bmap
= isl_basic_map_from_aff(aff
);
12183 return isl_map_from_basic_map(bmap
);
12186 /* Construct a basic map mapping the domain the multi-affine expression
12187 * to its range, with each dimension in the range equated to the
12188 * corresponding affine expression.
12189 * If "rational" is set, then construct a rational basic map.
12191 __isl_give isl_basic_map
*isl_basic_map_from_multi_aff2(
12192 __isl_take isl_multi_aff
*maff
, int rational
)
12196 isl_basic_map
*bmap
;
12201 if (isl_space_dim(maff
->space
, isl_dim_out
) != maff
->n
)
12202 isl_die(isl_multi_aff_get_ctx(maff
), isl_error_internal
,
12203 "invalid space", goto error
);
12205 space
= isl_space_domain(isl_multi_aff_get_space(maff
));
12206 bmap
= isl_basic_map_universe(isl_space_from_domain(space
));
12208 bmap
= isl_basic_map_set_rational(bmap
);
12210 for (i
= 0; i
< maff
->n
; ++i
) {
12212 isl_basic_map
*bmap_i
;
12214 aff
= isl_aff_copy(maff
->p
[i
]);
12215 bmap_i
= isl_basic_map_from_aff2(aff
, rational
);
12217 bmap
= isl_basic_map_flat_range_product(bmap
, bmap_i
);
12220 bmap
= isl_basic_map_reset_space(bmap
, isl_multi_aff_get_space(maff
));
12222 isl_multi_aff_free(maff
);
12225 isl_multi_aff_free(maff
);
12229 /* Construct a basic map mapping the domain the multi-affine expression
12230 * to its range, with each dimension in the range equated to the
12231 * corresponding affine expression.
12233 __isl_give isl_basic_map
*isl_basic_map_from_multi_aff(
12234 __isl_take isl_multi_aff
*ma
)
12236 return isl_basic_map_from_multi_aff2(ma
, 0);
12239 /* Construct a map mapping the domain the multi-affine expression
12240 * to its range, with each dimension in the range equated to the
12241 * corresponding affine expression.
12243 __isl_give isl_map
*isl_map_from_multi_aff(__isl_take isl_multi_aff
*maff
)
12245 isl_basic_map
*bmap
;
12247 bmap
= isl_basic_map_from_multi_aff(maff
);
12248 return isl_map_from_basic_map(bmap
);
12251 /* Construct a basic map mapping a domain in the given space to
12252 * to an n-dimensional range, with n the number of elements in the list,
12253 * where each coordinate in the range is prescribed by the
12254 * corresponding affine expression.
12255 * The domains of all affine expressions in the list are assumed to match
12258 __isl_give isl_basic_map
*isl_basic_map_from_aff_list(
12259 __isl_take isl_space
*domain_dim
, __isl_take isl_aff_list
*list
)
12263 isl_basic_map
*bmap
;
12268 dim
= isl_space_from_domain(domain_dim
);
12269 bmap
= isl_basic_map_universe(dim
);
12271 for (i
= 0; i
< list
->n
; ++i
) {
12273 isl_basic_map
*bmap_i
;
12275 aff
= isl_aff_copy(list
->p
[i
]);
12276 bmap_i
= isl_basic_map_from_aff(aff
);
12278 bmap
= isl_basic_map_flat_range_product(bmap
, bmap_i
);
12281 isl_aff_list_free(list
);
12285 __isl_give isl_set
*isl_set_equate(__isl_take isl_set
*set
,
12286 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12288 return isl_map_equate(set
, type1
, pos1
, type2
, pos2
);
12291 /* Construct a basic map where the given dimensions are equal to each other.
12293 static __isl_give isl_basic_map
*equator(__isl_take isl_space
*space
,
12294 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12296 isl_basic_map
*bmap
= NULL
;
12302 if (pos1
>= isl_space_dim(space
, type1
))
12303 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
12304 "index out of bounds", goto error
);
12305 if (pos2
>= isl_space_dim(space
, type2
))
12306 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
12307 "index out of bounds", goto error
);
12309 if (type1
== type2
&& pos1
== pos2
)
12310 return isl_basic_map_universe(space
);
12312 bmap
= isl_basic_map_alloc_space(isl_space_copy(space
), 0, 1, 0);
12313 i
= isl_basic_map_alloc_equality(bmap
);
12316 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
12317 pos1
+= isl_basic_map_offset(bmap
, type1
);
12318 pos2
+= isl_basic_map_offset(bmap
, type2
);
12319 isl_int_set_si(bmap
->eq
[i
][pos1
], -1);
12320 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
12321 bmap
= isl_basic_map_finalize(bmap
);
12322 isl_space_free(space
);
12325 isl_space_free(space
);
12326 isl_basic_map_free(bmap
);
12330 /* Add a constraint imposing that the given two dimensions are equal.
12332 __isl_give isl_basic_map
*isl_basic_map_equate(__isl_take isl_basic_map
*bmap
,
12333 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12337 eq
= equator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
12339 bmap
= isl_basic_map_intersect(bmap
, eq
);
12344 /* Add a constraint imposing that the given two dimensions are equal.
12346 __isl_give isl_map
*isl_map_equate(__isl_take isl_map
*map
,
12347 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12349 isl_basic_map
*bmap
;
12351 bmap
= equator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
12353 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
12358 /* Add a constraint imposing that the given two dimensions have opposite values.
12360 __isl_give isl_map
*isl_map_oppose(__isl_take isl_map
*map
,
12361 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12363 isl_basic_map
*bmap
= NULL
;
12369 if (pos1
>= isl_map_dim(map
, type1
))
12370 isl_die(map
->ctx
, isl_error_invalid
,
12371 "index out of bounds", goto error
);
12372 if (pos2
>= isl_map_dim(map
, type2
))
12373 isl_die(map
->ctx
, isl_error_invalid
,
12374 "index out of bounds", goto error
);
12376 bmap
= isl_basic_map_alloc_space(isl_map_get_space(map
), 0, 1, 0);
12377 i
= isl_basic_map_alloc_equality(bmap
);
12380 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
12381 pos1
+= isl_basic_map_offset(bmap
, type1
);
12382 pos2
+= isl_basic_map_offset(bmap
, type2
);
12383 isl_int_set_si(bmap
->eq
[i
][pos1
], 1);
12384 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
12385 bmap
= isl_basic_map_finalize(bmap
);
12387 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
12391 isl_basic_map_free(bmap
);
12396 /* Construct a constraint imposing that the value of the first dimension is
12397 * greater than or equal to that of the second.
12399 static __isl_give isl_constraint
*constraint_order_ge(
12400 __isl_take isl_space
*space
, enum isl_dim_type type1
, int pos1
,
12401 enum isl_dim_type type2
, int pos2
)
12408 c
= isl_constraint_alloc_inequality(isl_local_space_from_space(space
));
12410 if (pos1
>= isl_constraint_dim(c
, type1
))
12411 isl_die(isl_constraint_get_ctx(c
), isl_error_invalid
,
12412 "index out of bounds", return isl_constraint_free(c
));
12413 if (pos2
>= isl_constraint_dim(c
, type2
))
12414 isl_die(isl_constraint_get_ctx(c
), isl_error_invalid
,
12415 "index out of bounds", return isl_constraint_free(c
));
12417 if (type1
== type2
&& pos1
== pos2
)
12420 c
= isl_constraint_set_coefficient_si(c
, type1
, pos1
, 1);
12421 c
= isl_constraint_set_coefficient_si(c
, type2
, pos2
, -1);
12426 /* Add a constraint imposing that the value of the first dimension is
12427 * greater than or equal to that of the second.
12429 __isl_give isl_basic_map
*isl_basic_map_order_ge(__isl_take isl_basic_map
*bmap
,
12430 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12435 if (type1
== type2
&& pos1
== pos2
)
12437 space
= isl_basic_map_get_space(bmap
);
12438 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
12439 bmap
= isl_basic_map_add_constraint(bmap
, c
);
12444 /* Add a constraint imposing that the value of the first dimension is
12445 * greater than or equal to that of the second.
12447 __isl_give isl_map
*isl_map_order_ge(__isl_take isl_map
*map
,
12448 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12453 if (type1
== type2
&& pos1
== pos2
)
12455 space
= isl_map_get_space(map
);
12456 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
12457 map
= isl_map_add_constraint(map
, c
);
12462 /* Add a constraint imposing that the value of the first dimension is
12463 * less than or equal to that of the second.
12465 __isl_give isl_map
*isl_map_order_le(__isl_take isl_map
*map
,
12466 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12468 return isl_map_order_ge(map
, type2
, pos2
, type1
, pos1
);
12471 /* Construct a basic map where the value of the first dimension is
12472 * greater than that of the second.
12474 static __isl_give isl_basic_map
*greator(__isl_take isl_space
*space
,
12475 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12477 isl_basic_map
*bmap
= NULL
;
12483 if (pos1
>= isl_space_dim(space
, type1
))
12484 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
12485 "index out of bounds", goto error
);
12486 if (pos2
>= isl_space_dim(space
, type2
))
12487 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
12488 "index out of bounds", goto error
);
12490 if (type1
== type2
&& pos1
== pos2
)
12491 return isl_basic_map_empty(space
);
12493 bmap
= isl_basic_map_alloc_space(space
, 0, 0, 1);
12494 i
= isl_basic_map_alloc_inequality(bmap
);
12496 return isl_basic_map_free(bmap
);
12497 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
12498 pos1
+= isl_basic_map_offset(bmap
, type1
);
12499 pos2
+= isl_basic_map_offset(bmap
, type2
);
12500 isl_int_set_si(bmap
->ineq
[i
][pos1
], 1);
12501 isl_int_set_si(bmap
->ineq
[i
][pos2
], -1);
12502 isl_int_set_si(bmap
->ineq
[i
][0], -1);
12503 bmap
= isl_basic_map_finalize(bmap
);
12507 isl_space_free(space
);
12508 isl_basic_map_free(bmap
);
12512 /* Add a constraint imposing that the value of the first dimension is
12513 * greater than that of the second.
12515 __isl_give isl_basic_map
*isl_basic_map_order_gt(__isl_take isl_basic_map
*bmap
,
12516 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12520 gt
= greator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
12522 bmap
= isl_basic_map_intersect(bmap
, gt
);
12527 /* Add a constraint imposing that the value of the first dimension is
12528 * greater than that of the second.
12530 __isl_give isl_map
*isl_map_order_gt(__isl_take isl_map
*map
,
12531 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12533 isl_basic_map
*bmap
;
12535 bmap
= greator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
12537 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
12542 /* Add a constraint imposing that the value of the first dimension is
12543 * smaller than that of the second.
12545 __isl_give isl_map
*isl_map_order_lt(__isl_take isl_map
*map
,
12546 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12548 return isl_map_order_gt(map
, type2
, pos2
, type1
, pos1
);
12551 __isl_give isl_aff
*isl_basic_map_get_div(__isl_keep isl_basic_map
*bmap
,
12555 isl_local_space
*ls
;
12560 if (!isl_basic_map_divs_known(bmap
))
12561 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12562 "some divs are unknown", return NULL
);
12564 ls
= isl_basic_map_get_local_space(bmap
);
12565 div
= isl_local_space_get_div(ls
, pos
);
12566 isl_local_space_free(ls
);
12571 __isl_give isl_aff
*isl_basic_set_get_div(__isl_keep isl_basic_set
*bset
,
12574 return isl_basic_map_get_div(bset
, pos
);
12577 /* Plug in "subs" for dimension "type", "pos" of "bset".
12579 * Let i be the dimension to replace and let "subs" be of the form
12583 * Any integer division with a non-zero coefficient for i,
12585 * floor((a i + g)/m)
12589 * floor((a f + d g)/(m d))
12591 * Constraints of the form
12599 * We currently require that "subs" is an integral expression.
12600 * Handling rational expressions may require us to add stride constraints
12601 * as we do in isl_basic_set_preimage_multi_aff.
12603 __isl_give isl_basic_set
*isl_basic_set_substitute(
12604 __isl_take isl_basic_set
*bset
,
12605 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
12611 if (bset
&& isl_basic_set_plain_is_empty(bset
))
12614 bset
= isl_basic_set_cow(bset
);
12615 if (!bset
|| !subs
)
12618 ctx
= isl_basic_set_get_ctx(bset
);
12619 if (!isl_space_is_equal(bset
->dim
, subs
->ls
->dim
))
12620 isl_die(ctx
, isl_error_invalid
,
12621 "spaces don't match", goto error
);
12622 if (isl_local_space_dim(subs
->ls
, isl_dim_div
) != 0)
12623 isl_die(ctx
, isl_error_unsupported
,
12624 "cannot handle divs yet", goto error
);
12625 if (!isl_int_is_one(subs
->v
->el
[0]))
12626 isl_die(ctx
, isl_error_invalid
,
12627 "can only substitute integer expressions", goto error
);
12629 pos
+= isl_basic_set_offset(bset
, type
);
12633 for (i
= 0; i
< bset
->n_eq
; ++i
) {
12634 if (isl_int_is_zero(bset
->eq
[i
][pos
]))
12636 isl_int_set(v
, bset
->eq
[i
][pos
]);
12637 isl_int_set_si(bset
->eq
[i
][pos
], 0);
12638 isl_seq_combine(bset
->eq
[i
], subs
->v
->el
[0], bset
->eq
[i
],
12639 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12642 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
12643 if (isl_int_is_zero(bset
->ineq
[i
][pos
]))
12645 isl_int_set(v
, bset
->ineq
[i
][pos
]);
12646 isl_int_set_si(bset
->ineq
[i
][pos
], 0);
12647 isl_seq_combine(bset
->ineq
[i
], subs
->v
->el
[0], bset
->ineq
[i
],
12648 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12651 for (i
= 0; i
< bset
->n_div
; ++i
) {
12652 if (isl_int_is_zero(bset
->div
[i
][1 + pos
]))
12654 isl_int_set(v
, bset
->div
[i
][1 + pos
]);
12655 isl_int_set_si(bset
->div
[i
][1 + pos
], 0);
12656 isl_seq_combine(bset
->div
[i
] + 1,
12657 subs
->v
->el
[0], bset
->div
[i
] + 1,
12658 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12659 isl_int_mul(bset
->div
[i
][0], bset
->div
[i
][0], subs
->v
->el
[0]);
12664 bset
= isl_basic_set_simplify(bset
);
12665 return isl_basic_set_finalize(bset
);
12667 isl_basic_set_free(bset
);
12671 /* Plug in "subs" for dimension "type", "pos" of "set".
12673 __isl_give isl_set
*isl_set_substitute(__isl_take isl_set
*set
,
12674 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
12678 if (set
&& isl_set_plain_is_empty(set
))
12681 set
= isl_set_cow(set
);
12685 for (i
= set
->n
- 1; i
>= 0; --i
) {
12686 set
->p
[i
] = isl_basic_set_substitute(set
->p
[i
], type
, pos
, subs
);
12687 if (remove_if_empty(set
, i
) < 0)
12697 /* Check if the range of "ma" is compatible with the domain or range
12698 * (depending on "type") of "bmap".
12699 * Return -1 if anything is wrong.
12701 static int check_basic_map_compatible_range_multi_aff(
12702 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type type
,
12703 __isl_keep isl_multi_aff
*ma
)
12706 isl_space
*ma_space
;
12708 ma_space
= isl_multi_aff_get_space(ma
);
12710 m
= isl_space_match(bmap
->dim
, isl_dim_param
, ma_space
, isl_dim_param
);
12714 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12715 "parameters don't match", goto error
);
12716 m
= isl_space_tuple_is_equal(bmap
->dim
, type
, ma_space
, isl_dim_out
);
12720 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12721 "spaces don't match", goto error
);
12723 isl_space_free(ma_space
);
12726 isl_space_free(ma_space
);
12730 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12731 * coefficients before the transformed range of dimensions,
12732 * the "n_after" coefficients after the transformed range of dimensions
12733 * and the coefficients of the other divs in "bmap".
12735 static int set_ma_divs(__isl_keep isl_basic_map
*bmap
,
12736 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
, int n_div
)
12741 isl_local_space
*ls
;
12746 ls
= isl_aff_get_domain_local_space(ma
->p
[0]);
12750 n_param
= isl_local_space_dim(ls
, isl_dim_param
);
12751 n_set
= isl_local_space_dim(ls
, isl_dim_set
);
12752 for (i
= 0; i
< n_div
; ++i
) {
12753 int o_bmap
= 0, o_ls
= 0;
12755 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], 1 + 1 + n_param
);
12756 o_bmap
+= 1 + 1 + n_param
;
12757 o_ls
+= 1 + 1 + n_param
;
12758 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_before
);
12759 o_bmap
+= n_before
;
12760 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
12761 ls
->div
->row
[i
] + o_ls
, n_set
);
12764 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_after
);
12766 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
12767 ls
->div
->row
[i
] + o_ls
, n_div
);
12770 isl_seq_clr(bmap
->div
[i
] + o_bmap
, bmap
->n_div
- n_div
);
12771 if (isl_basic_set_add_div_constraints(bmap
, i
) < 0)
12775 isl_local_space_free(ls
);
12778 isl_local_space_free(ls
);
12782 /* How many stride constraints does "ma" enforce?
12783 * That is, how many of the affine expressions have a denominator
12784 * different from one?
12786 static int multi_aff_strides(__isl_keep isl_multi_aff
*ma
)
12791 for (i
= 0; i
< ma
->n
; ++i
)
12792 if (!isl_int_is_one(ma
->p
[i
]->v
->el
[0]))
12798 /* For each affine expression in ma of the form
12800 * x_i = (f_i y + h_i)/m_i
12802 * with m_i different from one, add a constraint to "bmap"
12805 * f_i y + h_i = m_i alpha_i
12807 * with alpha_i an additional existentially quantified variable.
12809 * The input variables of "ma" correspond to a subset of the variables
12810 * of "bmap". There are "n_before" variables in "bmap" before this
12811 * subset and "n_after" variables after this subset.
12812 * The integer divisions of the affine expressions in "ma" are assumed
12813 * to have been aligned. There are "n_div_ma" of them and
12814 * they appear first in "bmap", straight after the "n_after" variables.
12816 static __isl_give isl_basic_map
*add_ma_strides(
12817 __isl_take isl_basic_map
*bmap
, __isl_keep isl_multi_aff
*ma
,
12818 int n_before
, int n_after
, int n_div_ma
)
12826 total
= isl_basic_map_total_dim(bmap
);
12827 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
12828 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
12829 for (i
= 0; i
< ma
->n
; ++i
) {
12830 int o_bmap
= 0, o_ma
= 1;
12832 if (isl_int_is_one(ma
->p
[i
]->v
->el
[0]))
12834 div
= isl_basic_map_alloc_div(bmap
);
12835 k
= isl_basic_map_alloc_equality(bmap
);
12836 if (div
< 0 || k
< 0)
12838 isl_int_set_si(bmap
->div
[div
][0], 0);
12839 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12840 ma
->p
[i
]->v
->el
+ o_ma
, 1 + n_param
);
12841 o_bmap
+= 1 + n_param
;
12842 o_ma
+= 1 + n_param
;
12843 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_before
);
12844 o_bmap
+= n_before
;
12845 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12846 ma
->p
[i
]->v
->el
+ o_ma
, n_in
);
12849 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_after
);
12851 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12852 ma
->p
[i
]->v
->el
+ o_ma
, n_div_ma
);
12853 o_bmap
+= n_div_ma
;
12855 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, 1 + total
- o_bmap
);
12856 isl_int_neg(bmap
->eq
[k
][1 + total
], ma
->p
[i
]->v
->el
[0]);
12862 isl_basic_map_free(bmap
);
12866 /* Replace the domain or range space (depending on "type) of "space" by "set".
12868 static __isl_give isl_space
*isl_space_set(__isl_take isl_space
*space
,
12869 enum isl_dim_type type
, __isl_take isl_space
*set
)
12871 if (type
== isl_dim_in
) {
12872 space
= isl_space_range(space
);
12873 space
= isl_space_map_from_domain_and_range(set
, space
);
12875 space
= isl_space_domain(space
);
12876 space
= isl_space_map_from_domain_and_range(space
, set
);
12882 /* Compute the preimage of the domain or range (depending on "type")
12883 * of "bmap" under the function represented by "ma".
12884 * In other words, plug in "ma" in the domain or range of "bmap".
12885 * The result is a basic map that lives in the same space as "bmap"
12886 * except that the domain or range has been replaced by
12887 * the domain space of "ma".
12889 * If bmap is represented by
12891 * A(p) + S u + B x + T v + C(divs) >= 0,
12893 * where u and x are input and output dimensions if type == isl_dim_out
12894 * while x and v are input and output dimensions if type == isl_dim_in,
12895 * and ma is represented by
12897 * x = D(p) + F(y) + G(divs')
12899 * then the result is
12901 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12903 * The divs in the input set are similarly adjusted.
12906 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12910 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
12911 * B_i G(divs') + c_i(divs))/n_i)
12913 * If bmap is not a rational map and if F(y) involves any denominators
12915 * x_i = (f_i y + h_i)/m_i
12917 * then additional constraints are added to ensure that we only
12918 * map back integer points. That is we enforce
12920 * f_i y + h_i = m_i alpha_i
12922 * with alpha_i an additional existentially quantified variable.
12924 * We first copy over the divs from "ma".
12925 * Then we add the modified constraints and divs from "bmap".
12926 * Finally, we add the stride constraints, if needed.
12928 __isl_give isl_basic_map
*isl_basic_map_preimage_multi_aff(
12929 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
12930 __isl_take isl_multi_aff
*ma
)
12934 isl_basic_map
*res
= NULL
;
12935 int n_before
, n_after
, n_div_bmap
, n_div_ma
;
12936 isl_int f
, c1
, c2
, g
;
12937 int rational
, strides
;
12944 ma
= isl_multi_aff_align_divs(ma
);
12947 if (check_basic_map_compatible_range_multi_aff(bmap
, type
, ma
) < 0)
12950 if (type
== isl_dim_in
) {
12952 n_after
= isl_basic_map_dim(bmap
, isl_dim_out
);
12954 n_before
= isl_basic_map_dim(bmap
, isl_dim_in
);
12957 n_div_bmap
= isl_basic_map_dim(bmap
, isl_dim_div
);
12958 n_div_ma
= ma
->n
? isl_aff_dim(ma
->p
[0], isl_dim_div
) : 0;
12960 space
= isl_multi_aff_get_domain_space(ma
);
12961 space
= isl_space_set(isl_basic_map_get_space(bmap
), type
, space
);
12962 rational
= isl_basic_map_is_rational(bmap
);
12963 strides
= rational
? 0 : multi_aff_strides(ma
);
12964 res
= isl_basic_map_alloc_space(space
, n_div_ma
+ n_div_bmap
+ strides
,
12965 bmap
->n_eq
+ strides
, bmap
->n_ineq
+ 2 * n_div_ma
);
12967 res
= isl_basic_map_set_rational(res
);
12969 for (i
= 0; i
< n_div_ma
+ n_div_bmap
; ++i
)
12970 if (isl_basic_map_alloc_div(res
) < 0)
12973 if (set_ma_divs(res
, ma
, n_before
, n_after
, n_div_ma
) < 0)
12976 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
12977 k
= isl_basic_map_alloc_equality(res
);
12980 isl_seq_preimage(res
->eq
[k
], bmap
->eq
[i
], ma
, n_before
,
12981 n_after
, n_div_ma
, n_div_bmap
, f
, c1
, c2
, g
, 0);
12984 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
12985 k
= isl_basic_map_alloc_inequality(res
);
12988 isl_seq_preimage(res
->ineq
[k
], bmap
->ineq
[i
], ma
, n_before
,
12989 n_after
, n_div_ma
, n_div_bmap
, f
, c1
, c2
, g
, 0);
12992 for (i
= 0; i
< bmap
->n_div
; ++i
) {
12993 if (isl_int_is_zero(bmap
->div
[i
][0])) {
12994 isl_int_set_si(res
->div
[n_div_ma
+ i
][0], 0);
12997 isl_seq_preimage(res
->div
[n_div_ma
+ i
], bmap
->div
[i
], ma
,
12998 n_before
, n_after
, n_div_ma
, n_div_bmap
,
13003 res
= add_ma_strides(res
, ma
, n_before
, n_after
, n_div_ma
);
13009 isl_basic_map_free(bmap
);
13010 isl_multi_aff_free(ma
);
13011 res
= isl_basic_set_simplify(res
);
13012 return isl_basic_map_finalize(res
);
13018 isl_basic_map_free(bmap
);
13019 isl_multi_aff_free(ma
);
13020 isl_basic_map_free(res
);
13024 /* Compute the preimage of "bset" under the function represented by "ma".
13025 * In other words, plug in "ma" in "bset". The result is a basic set
13026 * that lives in the domain space of "ma".
13028 __isl_give isl_basic_set
*isl_basic_set_preimage_multi_aff(
13029 __isl_take isl_basic_set
*bset
, __isl_take isl_multi_aff
*ma
)
13031 return isl_basic_map_preimage_multi_aff(bset
, isl_dim_set
, ma
);
13034 /* Compute the preimage of the domain of "bmap" under the function
13035 * represented by "ma".
13036 * In other words, plug in "ma" in the domain of "bmap".
13037 * The result is a basic map that lives in the same space as "bmap"
13038 * except that the domain has been replaced by the domain space of "ma".
13040 __isl_give isl_basic_map
*isl_basic_map_preimage_domain_multi_aff(
13041 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
13043 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_in
, ma
);
13046 /* Compute the preimage of the range of "bmap" under the function
13047 * represented by "ma".
13048 * In other words, plug in "ma" in the range of "bmap".
13049 * The result is a basic map that lives in the same space as "bmap"
13050 * except that the range has been replaced by the domain space of "ma".
13052 __isl_give isl_basic_map
*isl_basic_map_preimage_range_multi_aff(
13053 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
13055 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_out
, ma
);
13058 /* Check if the range of "ma" is compatible with the domain or range
13059 * (depending on "type") of "map".
13060 * Return -1 if anything is wrong.
13062 static int check_map_compatible_range_multi_aff(
13063 __isl_keep isl_map
*map
, enum isl_dim_type type
,
13064 __isl_keep isl_multi_aff
*ma
)
13067 isl_space
*ma_space
;
13069 ma_space
= isl_multi_aff_get_space(ma
);
13070 m
= isl_space_tuple_is_equal(map
->dim
, type
, ma_space
, isl_dim_out
);
13071 isl_space_free(ma_space
);
13073 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
13074 "spaces don't match", return -1);
13078 /* Compute the preimage of the domain or range (depending on "type")
13079 * of "map" under the function represented by "ma".
13080 * In other words, plug in "ma" in the domain or range of "map".
13081 * The result is a map that lives in the same space as "map"
13082 * except that the domain or range has been replaced by
13083 * the domain space of "ma".
13085 * The parameters are assumed to have been aligned.
13087 static __isl_give isl_map
*map_preimage_multi_aff(__isl_take isl_map
*map
,
13088 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
13093 map
= isl_map_cow(map
);
13094 ma
= isl_multi_aff_align_divs(ma
);
13097 if (check_map_compatible_range_multi_aff(map
, type
, ma
) < 0)
13100 for (i
= 0; i
< map
->n
; ++i
) {
13101 map
->p
[i
] = isl_basic_map_preimage_multi_aff(map
->p
[i
], type
,
13102 isl_multi_aff_copy(ma
));
13107 space
= isl_multi_aff_get_domain_space(ma
);
13108 space
= isl_space_set(isl_map_get_space(map
), type
, space
);
13110 isl_space_free(map
->dim
);
13115 isl_multi_aff_free(ma
);
13117 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
13118 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
13121 isl_multi_aff_free(ma
);
13126 /* Compute the preimage of the domain or range (depending on "type")
13127 * of "map" under the function represented by "ma".
13128 * In other words, plug in "ma" in the domain or range of "map".
13129 * The result is a map that lives in the same space as "map"
13130 * except that the domain or range has been replaced by
13131 * the domain space of "ma".
13133 __isl_give isl_map
*isl_map_preimage_multi_aff(__isl_take isl_map
*map
,
13134 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
13139 if (isl_space_match(map
->dim
, isl_dim_param
, ma
->space
, isl_dim_param
))
13140 return map_preimage_multi_aff(map
, type
, ma
);
13142 if (!isl_space_has_named_params(map
->dim
) ||
13143 !isl_space_has_named_params(ma
->space
))
13144 isl_die(map
->ctx
, isl_error_invalid
,
13145 "unaligned unnamed parameters", goto error
);
13146 map
= isl_map_align_params(map
, isl_multi_aff_get_space(ma
));
13147 ma
= isl_multi_aff_align_params(ma
, isl_map_get_space(map
));
13149 return map_preimage_multi_aff(map
, type
, ma
);
13151 isl_multi_aff_free(ma
);
13152 return isl_map_free(map
);
13155 /* Compute the preimage of "set" under the function represented by "ma".
13156 * In other words, plug in "ma" in "set". The result is a set
13157 * that lives in the domain space of "ma".
13159 __isl_give isl_set
*isl_set_preimage_multi_aff(__isl_take isl_set
*set
,
13160 __isl_take isl_multi_aff
*ma
)
13162 return isl_map_preimage_multi_aff(set
, isl_dim_set
, ma
);
13165 /* Compute the preimage of the domain of "map" under the function
13166 * represented by "ma".
13167 * In other words, plug in "ma" in the domain of "map".
13168 * The result is a map that lives in the same space as "map"
13169 * except that the domain has been replaced by the domain space of "ma".
13171 __isl_give isl_map
*isl_map_preimage_domain_multi_aff(__isl_take isl_map
*map
,
13172 __isl_take isl_multi_aff
*ma
)
13174 return isl_map_preimage_multi_aff(map
, isl_dim_in
, ma
);
13177 /* Compute the preimage of the range of "map" under the function
13178 * represented by "ma".
13179 * In other words, plug in "ma" in the range of "map".
13180 * The result is a map that lives in the same space as "map"
13181 * except that the range has been replaced by the domain space of "ma".
13183 __isl_give isl_map
*isl_map_preimage_range_multi_aff(__isl_take isl_map
*map
,
13184 __isl_take isl_multi_aff
*ma
)
13186 return isl_map_preimage_multi_aff(map
, isl_dim_out
, ma
);
13189 /* Compute the preimage of "map" under the function represented by "pma".
13190 * In other words, plug in "pma" in the domain or range of "map".
13191 * The result is a map that lives in the same space as "map",
13192 * except that the space of type "type" has been replaced by
13193 * the domain space of "pma".
13195 * The parameters of "map" and "pma" are assumed to have been aligned.
13197 static __isl_give isl_map
*isl_map_preimage_pw_multi_aff_aligned(
13198 __isl_take isl_map
*map
, enum isl_dim_type type
,
13199 __isl_take isl_pw_multi_aff
*pma
)
13208 isl_pw_multi_aff_free(pma
);
13209 res
= isl_map_empty(isl_map_get_space(map
));
13214 res
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
13215 isl_multi_aff_copy(pma
->p
[0].maff
));
13216 if (type
== isl_dim_in
)
13217 res
= isl_map_intersect_domain(res
,
13218 isl_map_copy(pma
->p
[0].set
));
13220 res
= isl_map_intersect_range(res
,
13221 isl_map_copy(pma
->p
[0].set
));
13223 for (i
= 1; i
< pma
->n
; ++i
) {
13226 res_i
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
13227 isl_multi_aff_copy(pma
->p
[i
].maff
));
13228 if (type
== isl_dim_in
)
13229 res_i
= isl_map_intersect_domain(res_i
,
13230 isl_map_copy(pma
->p
[i
].set
));
13232 res_i
= isl_map_intersect_range(res_i
,
13233 isl_map_copy(pma
->p
[i
].set
));
13234 res
= isl_map_union(res
, res_i
);
13237 isl_pw_multi_aff_free(pma
);
13241 isl_pw_multi_aff_free(pma
);
13246 /* Compute the preimage of "map" under the function represented by "pma".
13247 * In other words, plug in "pma" in the domain or range of "map".
13248 * The result is a map that lives in the same space as "map",
13249 * except that the space of type "type" has been replaced by
13250 * the domain space of "pma".
13252 __isl_give isl_map
*isl_map_preimage_pw_multi_aff(__isl_take isl_map
*map
,
13253 enum isl_dim_type type
, __isl_take isl_pw_multi_aff
*pma
)
13258 if (isl_space_match(map
->dim
, isl_dim_param
, pma
->dim
, isl_dim_param
))
13259 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
13261 if (!isl_space_has_named_params(map
->dim
) ||
13262 !isl_space_has_named_params(pma
->dim
))
13263 isl_die(map
->ctx
, isl_error_invalid
,
13264 "unaligned unnamed parameters", goto error
);
13265 map
= isl_map_align_params(map
, isl_pw_multi_aff_get_space(pma
));
13266 pma
= isl_pw_multi_aff_align_params(pma
, isl_map_get_space(map
));
13268 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
13270 isl_pw_multi_aff_free(pma
);
13271 return isl_map_free(map
);
13274 /* Compute the preimage of "set" under the function represented by "pma".
13275 * In other words, plug in "pma" in "set". The result is a set
13276 * that lives in the domain space of "pma".
13278 __isl_give isl_set
*isl_set_preimage_pw_multi_aff(__isl_take isl_set
*set
,
13279 __isl_take isl_pw_multi_aff
*pma
)
13281 return isl_map_preimage_pw_multi_aff(set
, isl_dim_set
, pma
);
13284 /* Compute the preimage of the domain of "map" under the function
13285 * represented by "pma".
13286 * In other words, plug in "pma" in the domain of "map".
13287 * The result is a map that lives in the same space as "map",
13288 * except that domain space has been replaced by the domain space of "pma".
13290 __isl_give isl_map
*isl_map_preimage_domain_pw_multi_aff(
13291 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
13293 return isl_map_preimage_pw_multi_aff(map
, isl_dim_in
, pma
);
13296 /* Compute the preimage of the range of "map" under the function
13297 * represented by "pma".
13298 * In other words, plug in "pma" in the range of "map".
13299 * The result is a map that lives in the same space as "map",
13300 * except that range space has been replaced by the domain space of "pma".
13302 __isl_give isl_map
*isl_map_preimage_range_pw_multi_aff(
13303 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
13305 return isl_map_preimage_pw_multi_aff(map
, isl_dim_out
, pma
);
13308 /* Compute the preimage of "map" under the function represented by "mpa".
13309 * In other words, plug in "mpa" in the domain or range of "map".
13310 * The result is a map that lives in the same space as "map",
13311 * except that the space of type "type" has been replaced by
13312 * the domain space of "mpa".
13314 * If the map does not involve any constraints that refer to the
13315 * dimensions of the substituted space, then the only possible
13316 * effect of "mpa" on the map is to map the space to a different space.
13317 * We create a separate isl_multi_aff to effectuate this change
13318 * in order to avoid spurious splitting of the map along the pieces
13321 __isl_give isl_map
*isl_map_preimage_multi_pw_aff(__isl_take isl_map
*map
,
13322 enum isl_dim_type type
, __isl_take isl_multi_pw_aff
*mpa
)
13325 isl_pw_multi_aff
*pma
;
13330 n
= isl_map_dim(map
, type
);
13331 if (!isl_map_involves_dims(map
, type
, 0, n
)) {
13335 space
= isl_multi_pw_aff_get_space(mpa
);
13336 isl_multi_pw_aff_free(mpa
);
13337 ma
= isl_multi_aff_zero(space
);
13338 return isl_map_preimage_multi_aff(map
, type
, ma
);
13341 pma
= isl_pw_multi_aff_from_multi_pw_aff(mpa
);
13342 return isl_map_preimage_pw_multi_aff(map
, type
, pma
);
13345 isl_multi_pw_aff_free(mpa
);
13349 /* Compute the preimage of "map" under the function represented by "mpa".
13350 * In other words, plug in "mpa" in the domain "map".
13351 * The result is a map that lives in the same space as "map",
13352 * except that domain space has been replaced by the domain space of "mpa".
13354 __isl_give isl_map
*isl_map_preimage_domain_multi_pw_aff(
13355 __isl_take isl_map
*map
, __isl_take isl_multi_pw_aff
*mpa
)
13357 return isl_map_preimage_multi_pw_aff(map
, isl_dim_in
, mpa
);
13360 /* Compute the preimage of "set" by the function represented by "mpa".
13361 * In other words, plug in "mpa" in "set".
13363 __isl_give isl_set
*isl_set_preimage_multi_pw_aff(__isl_take isl_set
*set
,
13364 __isl_take isl_multi_pw_aff
*mpa
)
13366 return isl_map_preimage_multi_pw_aff(set
, isl_dim_set
, mpa
);
13369 /* Are the "n" "coefficients" starting at "first" of the integer division
13370 * expressions at position "pos1" in "bmap1" and "pos2" in "bmap2" equal
13372 * The "coefficient" at position 0 is the denominator.
13373 * The "coefficient" at position 1 is the constant term.
13375 isl_bool
isl_basic_map_equal_div_expr_part(__isl_keep isl_basic_map
*bmap1
,
13376 int pos1
, __isl_keep isl_basic_map
*bmap2
, int pos2
,
13377 unsigned first
, unsigned n
)
13379 if (isl_basic_map_check_range(bmap1
, isl_dim_div
, pos1
, 1) < 0)
13380 return isl_bool_error
;
13381 if (isl_basic_map_check_range(bmap2
, isl_dim_div
, pos2
, 1) < 0)
13382 return isl_bool_error
;
13383 return isl_seq_eq(bmap1
->div
[pos1
] + first
,
13384 bmap2
->div
[pos2
] + first
, n
);
13387 /* Are the integer division expressions at position "pos1" in "bmap1" and
13388 * "pos2" in "bmap2" equal to each other, except that the constant terms
13391 isl_bool
isl_basic_map_equal_div_expr_except_constant(
13392 __isl_keep isl_basic_map
*bmap1
, int pos1
,
13393 __isl_keep isl_basic_map
*bmap2
, int pos2
)
13398 if (!bmap1
|| !bmap2
)
13399 return isl_bool_error
;
13400 total
= isl_basic_map_total_dim(bmap1
);
13401 if (total
!= isl_basic_map_total_dim(bmap2
))
13402 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
13403 "incomparable div expressions", return isl_bool_error
);
13404 equal
= isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
13406 if (equal
< 0 || !equal
)
13408 equal
= isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
13410 if (equal
< 0 || equal
)
13411 return isl_bool_not(equal
);
13412 return isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
13416 /* Replace the numerator of the constant term of the integer division
13417 * expression at position "div" in "bmap" by "value".
13418 * The caller guarantees that this does not change the meaning
13421 __isl_give isl_basic_map
*isl_basic_map_set_div_expr_constant_num_si_inplace(
13422 __isl_take isl_basic_map
*bmap
, int div
, int value
)
13424 if (isl_basic_map_check_range(bmap
, isl_dim_div
, div
, 1) < 0)
13425 return isl_basic_map_free(bmap
);
13427 isl_int_set_si(bmap
->div
[div
][1], value
);
13432 /* Is the point "inner" internal to inequality constraint "ineq"
13434 * The point is considered to be internal to the inequality constraint,
13435 * if it strictly lies on the positive side of the inequality constraint,
13436 * or if it lies on the constraint and the constraint is lexico-positive.
13438 static isl_bool
is_internal(__isl_keep isl_vec
*inner
,
13439 __isl_keep isl_basic_set
*bset
, int ineq
)
13445 if (!inner
|| !bset
)
13446 return isl_bool_error
;
13448 ctx
= isl_basic_set_get_ctx(bset
);
13449 isl_seq_inner_product(inner
->el
, bset
->ineq
[ineq
], inner
->size
,
13450 &ctx
->normalize_gcd
);
13451 if (!isl_int_is_zero(ctx
->normalize_gcd
))
13452 return isl_int_is_nonneg(ctx
->normalize_gcd
);
13454 total
= isl_basic_set_dim(bset
, isl_dim_all
);
13455 pos
= isl_seq_first_non_zero(bset
->ineq
[ineq
] + 1, total
);
13456 return isl_int_is_pos(bset
->ineq
[ineq
][1 + pos
]);
13459 /* Tighten the inequality constraints of "bset" that are outward with respect
13460 * to the point "vec".
13461 * That is, tighten the constraints that are not satisfied by "vec".
13463 * "vec" is a point internal to some superset S of "bset" that is used
13464 * to make the subsets of S disjoint, by tightening one half of the constraints
13465 * that separate two subsets. In particular, the constraints of S
13466 * are all satisfied by "vec" and should not be tightened.
13467 * Of the internal constraints, those that have "vec" on the outside
13468 * are tightened. The shared facet is included in the adjacent subset
13469 * with the opposite constraint.
13470 * For constraints that saturate "vec", this criterion cannot be used
13471 * to determine which of the two sides should be tightened.
13472 * Instead, the sign of the first non-zero coefficient is used
13473 * to make this choice. Note that this second criterion is never used
13474 * on the constraints of S since "vec" is interior to "S".
13476 __isl_give isl_basic_set
*isl_basic_set_tighten_outward(
13477 __isl_take isl_basic_set
*bset
, __isl_keep isl_vec
*vec
)
13481 bset
= isl_basic_set_cow(bset
);
13484 for (j
= 0; j
< bset
->n_ineq
; ++j
) {
13487 internal
= is_internal(vec
, bset
, j
);
13489 return isl_basic_set_free(bset
);
13492 isl_int_sub_ui(bset
->ineq
[j
][0], bset
->ineq
[j
][0], 1);