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
;
5332 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5333 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5334 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5336 dim
= isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap
)));
5337 domain
= isl_basic_map_universe(dim
);
5339 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
5340 bmap
= isl_basic_map_apply_range(bmap
, domain
);
5341 bmap
= isl_basic_map_extend_constraints(bmap
, n_in
, 0);
5343 for (i
= 0; i
< n_in
; ++i
)
5344 bmap
= isl_basic_map_equate(bmap
, isl_dim_in
, i
,
5347 bmap
= isl_basic_map_gauss(bmap
, NULL
);
5348 return isl_basic_map_finalize(bmap
);
5351 __isl_give isl_basic_map
*isl_basic_map_range_map(
5352 __isl_take isl_basic_map
*bmap
)
5356 isl_basic_map
*range
;
5357 int nparam
, n_in
, n_out
;
5359 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5360 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5361 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5363 dim
= isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap
)));
5364 range
= isl_basic_map_universe(dim
);
5366 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
5367 bmap
= isl_basic_map_apply_range(bmap
, range
);
5368 bmap
= isl_basic_map_extend_constraints(bmap
, n_out
, 0);
5370 for (i
= 0; i
< n_out
; ++i
)
5371 bmap
= isl_basic_map_equate(bmap
, isl_dim_in
, n_in
+ i
,
5374 bmap
= isl_basic_map_gauss(bmap
, NULL
);
5375 return isl_basic_map_finalize(bmap
);
5378 int isl_map_may_be_set(__isl_keep isl_map
*map
)
5382 return isl_space_may_be_set(map
->dim
);
5385 /* Is this map actually a set?
5386 * Users should never call this function. Outside of isl,
5387 * the type should indicate whether something is a set or a map.
5389 int isl_map_is_set(__isl_keep isl_map
*map
)
5393 return isl_space_is_set(map
->dim
);
5396 struct isl_set
*isl_map_range(struct isl_map
*map
)
5399 struct isl_set
*set
;
5403 if (isl_map_is_set(map
))
5404 return set_from_map(map
);
5406 map
= isl_map_cow(map
);
5410 set
= set_from_map(map
);
5411 set
->dim
= isl_space_range(set
->dim
);
5414 for (i
= 0; i
< map
->n
; ++i
) {
5415 set
->p
[i
] = isl_basic_map_range(map
->p
[i
]);
5419 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
5420 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
5427 __isl_give isl_map
*isl_map_domain_map(__isl_take isl_map
*map
)
5431 map
= isl_map_cow(map
);
5435 map
->dim
= isl_space_domain_map(map
->dim
);
5438 for (i
= 0; i
< map
->n
; ++i
) {
5439 map
->p
[i
] = isl_basic_map_domain_map(map
->p
[i
]);
5443 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
5444 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5451 __isl_give isl_map
*isl_map_range_map(__isl_take isl_map
*map
)
5454 isl_space
*range_dim
;
5456 map
= isl_map_cow(map
);
5460 range_dim
= isl_space_range(isl_map_get_space(map
));
5461 range_dim
= isl_space_from_range(range_dim
);
5462 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
5463 map
->dim
= isl_space_join(map
->dim
, range_dim
);
5466 for (i
= 0; i
< map
->n
; ++i
) {
5467 map
->p
[i
] = isl_basic_map_range_map(map
->p
[i
]);
5471 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
5472 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5479 /* Given a wrapped map of the form A[B -> C],
5480 * return the map A[B -> C] -> B.
5482 __isl_give isl_map
*isl_set_wrapped_domain_map(__isl_take isl_set
*set
)
5489 if (!isl_set_has_tuple_id(set
))
5490 return isl_map_domain_map(isl_set_unwrap(set
));
5492 id
= isl_set_get_tuple_id(set
);
5493 map
= isl_map_domain_map(isl_set_unwrap(set
));
5494 map
= isl_map_set_tuple_id(map
, isl_dim_in
, id
);
5499 __isl_give isl_map
*isl_map_from_set(__isl_take isl_set
*set
,
5500 __isl_take isl_space
*dim
)
5503 struct isl_map
*map
= NULL
;
5505 set
= isl_set_cow(set
);
5508 isl_assert(set
->ctx
, isl_space_compatible_internal(set
->dim
, dim
),
5510 map
= set_to_map(set
);
5511 for (i
= 0; i
< set
->n
; ++i
) {
5512 map
->p
[i
] = basic_map_from_basic_set(
5513 set
->p
[i
], isl_space_copy(dim
));
5517 isl_space_free(map
->dim
);
5521 isl_space_free(dim
);
5526 __isl_give isl_basic_map
*isl_basic_map_from_domain(
5527 __isl_take isl_basic_set
*bset
)
5529 return isl_basic_map_reverse(isl_basic_map_from_range(bset
));
5532 __isl_give isl_basic_map
*isl_basic_map_from_range(
5533 __isl_take isl_basic_set
*bset
)
5536 space
= isl_basic_set_get_space(bset
);
5537 space
= isl_space_from_range(space
);
5538 bset
= isl_basic_set_reset_space(bset
, space
);
5539 return bset_to_bmap(bset
);
5542 /* Create a relation with the given set as range.
5543 * The domain of the created relation is a zero-dimensional
5544 * flat anonymous space.
5546 __isl_give isl_map
*isl_map_from_range(__isl_take isl_set
*set
)
5549 space
= isl_set_get_space(set
);
5550 space
= isl_space_from_range(space
);
5551 set
= isl_set_reset_space(set
, space
);
5552 return set_to_map(set
);
5555 /* Create a relation with the given set as domain.
5556 * The range of the created relation is a zero-dimensional
5557 * flat anonymous space.
5559 __isl_give isl_map
*isl_map_from_domain(__isl_take isl_set
*set
)
5561 return isl_map_reverse(isl_map_from_range(set
));
5564 __isl_give isl_basic_map
*isl_basic_map_from_domain_and_range(
5565 __isl_take isl_basic_set
*domain
, __isl_take isl_basic_set
*range
)
5567 return isl_basic_map_apply_range(isl_basic_map_reverse(domain
), range
);
5570 __isl_give isl_map
*isl_map_from_domain_and_range(__isl_take isl_set
*domain
,
5571 __isl_take isl_set
*range
)
5573 return isl_map_apply_range(isl_map_reverse(domain
), range
);
5576 /* Return a newly allocated isl_map with given space and flags and
5577 * room for "n" basic maps.
5578 * Make sure that all cached information is cleared.
5580 __isl_give isl_map
*isl_map_alloc_space(__isl_take isl_space
*space
, int n
,
5583 struct isl_map
*map
;
5588 isl_die(space
->ctx
, isl_error_internal
,
5589 "negative number of basic maps", goto error
);
5590 map
= isl_calloc(space
->ctx
, struct isl_map
,
5591 sizeof(struct isl_map
) +
5592 (n
- 1) * sizeof(struct isl_basic_map
*));
5596 map
->ctx
= space
->ctx
;
5597 isl_ctx_ref(map
->ctx
);
5605 isl_space_free(space
);
5609 struct isl_map
*isl_map_alloc(struct isl_ctx
*ctx
,
5610 unsigned nparam
, unsigned in
, unsigned out
, int n
,
5613 struct isl_map
*map
;
5616 dims
= isl_space_alloc(ctx
, nparam
, in
, out
);
5620 map
= isl_map_alloc_space(dims
, n
, flags
);
5624 __isl_give isl_basic_map
*isl_basic_map_empty(__isl_take isl_space
*dim
)
5626 struct isl_basic_map
*bmap
;
5627 bmap
= isl_basic_map_alloc_space(dim
, 0, 1, 0);
5628 bmap
= isl_basic_map_set_to_empty(bmap
);
5632 __isl_give isl_basic_set
*isl_basic_set_empty(__isl_take isl_space
*dim
)
5634 struct isl_basic_set
*bset
;
5635 bset
= isl_basic_set_alloc_space(dim
, 0, 1, 0);
5636 bset
= isl_basic_set_set_to_empty(bset
);
5640 __isl_give isl_basic_map
*isl_basic_map_universe(__isl_take isl_space
*dim
)
5642 struct isl_basic_map
*bmap
;
5643 bmap
= isl_basic_map_alloc_space(dim
, 0, 0, 0);
5644 bmap
= isl_basic_map_finalize(bmap
);
5648 __isl_give isl_basic_set
*isl_basic_set_universe(__isl_take isl_space
*dim
)
5650 struct isl_basic_set
*bset
;
5651 bset
= isl_basic_set_alloc_space(dim
, 0, 0, 0);
5652 bset
= isl_basic_set_finalize(bset
);
5656 __isl_give isl_basic_map
*isl_basic_map_nat_universe(__isl_take isl_space
*dim
)
5659 unsigned total
= isl_space_dim(dim
, isl_dim_all
);
5660 isl_basic_map
*bmap
;
5662 bmap
= isl_basic_map_alloc_space(dim
, 0, 0, total
);
5663 for (i
= 0; i
< total
; ++i
) {
5664 int k
= isl_basic_map_alloc_inequality(bmap
);
5667 isl_seq_clr(bmap
->ineq
[k
], 1 + total
);
5668 isl_int_set_si(bmap
->ineq
[k
][1 + i
], 1);
5672 isl_basic_map_free(bmap
);
5676 __isl_give isl_basic_set
*isl_basic_set_nat_universe(__isl_take isl_space
*dim
)
5678 return isl_basic_map_nat_universe(dim
);
5681 __isl_give isl_map
*isl_map_nat_universe(__isl_take isl_space
*dim
)
5683 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim
));
5686 __isl_give isl_set
*isl_set_nat_universe(__isl_take isl_space
*dim
)
5688 return isl_map_nat_universe(dim
);
5691 __isl_give isl_map
*isl_map_empty(__isl_take isl_space
*dim
)
5693 return isl_map_alloc_space(dim
, 0, ISL_MAP_DISJOINT
);
5696 __isl_give isl_set
*isl_set_empty(__isl_take isl_space
*dim
)
5698 return isl_set_alloc_space(dim
, 0, ISL_MAP_DISJOINT
);
5701 __isl_give isl_map
*isl_map_universe(__isl_take isl_space
*dim
)
5703 struct isl_map
*map
;
5706 map
= isl_map_alloc_space(isl_space_copy(dim
), 1, ISL_MAP_DISJOINT
);
5707 map
= isl_map_add_basic_map(map
, isl_basic_map_universe(dim
));
5711 __isl_give isl_set
*isl_set_universe(__isl_take isl_space
*dim
)
5713 struct isl_set
*set
;
5716 set
= isl_set_alloc_space(isl_space_copy(dim
), 1, ISL_MAP_DISJOINT
);
5717 set
= isl_set_add_basic_set(set
, isl_basic_set_universe(dim
));
5721 struct isl_map
*isl_map_dup(struct isl_map
*map
)
5724 struct isl_map
*dup
;
5728 dup
= isl_map_alloc_space(isl_space_copy(map
->dim
), map
->n
, map
->flags
);
5729 for (i
= 0; i
< map
->n
; ++i
)
5730 dup
= isl_map_add_basic_map(dup
, isl_basic_map_copy(map
->p
[i
]));
5734 __isl_give isl_map
*isl_map_add_basic_map(__isl_take isl_map
*map
,
5735 __isl_take isl_basic_map
*bmap
)
5739 if (isl_basic_map_plain_is_empty(bmap
)) {
5740 isl_basic_map_free(bmap
);
5743 isl_assert(map
->ctx
, isl_space_is_equal(map
->dim
, bmap
->dim
), goto error
);
5744 isl_assert(map
->ctx
, map
->n
< map
->size
, goto error
);
5745 map
->p
[map
->n
] = bmap
;
5747 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5753 isl_basic_map_free(bmap
);
5757 __isl_null isl_map
*isl_map_free(__isl_take isl_map
*map
)
5768 isl_ctx_deref(map
->ctx
);
5769 for (i
= 0; i
< map
->n
; ++i
)
5770 isl_basic_map_free(map
->p
[i
]);
5771 isl_space_free(map
->dim
);
5777 static struct isl_basic_map
*isl_basic_map_fix_pos_si(
5778 struct isl_basic_map
*bmap
, unsigned pos
, int value
)
5782 bmap
= isl_basic_map_cow(bmap
);
5783 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
5784 j
= isl_basic_map_alloc_equality(bmap
);
5787 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
5788 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
5789 isl_int_set_si(bmap
->eq
[j
][0], value
);
5790 bmap
= isl_basic_map_simplify(bmap
);
5791 return isl_basic_map_finalize(bmap
);
5793 isl_basic_map_free(bmap
);
5797 static __isl_give isl_basic_map
*isl_basic_map_fix_pos(
5798 __isl_take isl_basic_map
*bmap
, unsigned pos
, isl_int value
)
5802 bmap
= isl_basic_map_cow(bmap
);
5803 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
5804 j
= isl_basic_map_alloc_equality(bmap
);
5807 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
5808 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
5809 isl_int_set(bmap
->eq
[j
][0], value
);
5810 bmap
= isl_basic_map_simplify(bmap
);
5811 return isl_basic_map_finalize(bmap
);
5813 isl_basic_map_free(bmap
);
5817 struct isl_basic_map
*isl_basic_map_fix_si(struct isl_basic_map
*bmap
,
5818 enum isl_dim_type type
, unsigned pos
, int value
)
5820 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
5821 return isl_basic_map_free(bmap
);
5822 return isl_basic_map_fix_pos_si(bmap
,
5823 isl_basic_map_offset(bmap
, type
) + pos
, value
);
5826 __isl_give isl_basic_map
*isl_basic_map_fix(__isl_take isl_basic_map
*bmap
,
5827 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5829 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
5830 return isl_basic_map_free(bmap
);
5831 return isl_basic_map_fix_pos(bmap
,
5832 isl_basic_map_offset(bmap
, type
) + pos
, value
);
5835 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
5836 * to be equal to "v".
5838 __isl_give isl_basic_map
*isl_basic_map_fix_val(__isl_take isl_basic_map
*bmap
,
5839 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5843 if (!isl_val_is_int(v
))
5844 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
5845 "expecting integer value", goto error
);
5846 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
5848 pos
+= isl_basic_map_offset(bmap
, type
);
5849 bmap
= isl_basic_map_fix_pos(bmap
, pos
, v
->n
);
5853 isl_basic_map_free(bmap
);
5858 /* Fix the value of the variable at position "pos" of type "type" of "bset"
5859 * to be equal to "v".
5861 __isl_give isl_basic_set
*isl_basic_set_fix_val(__isl_take isl_basic_set
*bset
,
5862 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5864 return isl_basic_map_fix_val(bset
, type
, pos
, v
);
5867 struct isl_basic_set
*isl_basic_set_fix_si(struct isl_basic_set
*bset
,
5868 enum isl_dim_type type
, unsigned pos
, int value
)
5870 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset
),
5874 __isl_give isl_basic_set
*isl_basic_set_fix(__isl_take isl_basic_set
*bset
,
5875 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5877 return bset_from_bmap(isl_basic_map_fix(bset_to_bmap(bset
),
5881 struct isl_basic_map
*isl_basic_map_fix_input_si(struct isl_basic_map
*bmap
,
5882 unsigned input
, int value
)
5884 return isl_basic_map_fix_si(bmap
, isl_dim_in
, input
, value
);
5887 struct isl_basic_set
*isl_basic_set_fix_dim_si(struct isl_basic_set
*bset
,
5888 unsigned dim
, int value
)
5890 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset
),
5891 isl_dim_set
, dim
, value
));
5894 static int remove_if_empty(__isl_keep isl_map
*map
, int i
)
5896 int empty
= isl_basic_map_plain_is_empty(map
->p
[i
]);
5903 isl_basic_map_free(map
->p
[i
]);
5904 if (i
!= map
->n
- 1) {
5905 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5906 map
->p
[i
] = map
->p
[map
->n
- 1];
5913 /* Perform "fn" on each basic map of "map", where we may not be holding
5914 * the only reference to "map".
5915 * In particular, "fn" should be a semantics preserving operation
5916 * that we want to apply to all copies of "map". We therefore need
5917 * to be careful not to modify "map" in a way that breaks "map"
5918 * in case anything goes wrong.
5920 __isl_give isl_map
*isl_map_inline_foreach_basic_map(__isl_take isl_map
*map
,
5921 __isl_give isl_basic_map
*(*fn
)(__isl_take isl_basic_map
*bmap
))
5923 struct isl_basic_map
*bmap
;
5929 for (i
= map
->n
- 1; i
>= 0; --i
) {
5930 bmap
= isl_basic_map_copy(map
->p
[i
]);
5934 isl_basic_map_free(map
->p
[i
]);
5936 if (remove_if_empty(map
, i
) < 0)
5946 struct isl_map
*isl_map_fix_si(struct isl_map
*map
,
5947 enum isl_dim_type type
, unsigned pos
, int value
)
5951 map
= isl_map_cow(map
);
5955 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
5956 for (i
= map
->n
- 1; i
>= 0; --i
) {
5957 map
->p
[i
] = isl_basic_map_fix_si(map
->p
[i
], type
, pos
, value
);
5958 if (remove_if_empty(map
, i
) < 0)
5961 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5968 __isl_give isl_set
*isl_set_fix_si(__isl_take isl_set
*set
,
5969 enum isl_dim_type type
, unsigned pos
, int value
)
5971 return set_from_map(isl_map_fix_si(set_to_map(set
), type
, pos
, value
));
5974 __isl_give isl_map
*isl_map_fix(__isl_take isl_map
*map
,
5975 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5979 map
= isl_map_cow(map
);
5983 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
5984 for (i
= 0; i
< map
->n
; ++i
) {
5985 map
->p
[i
] = isl_basic_map_fix(map
->p
[i
], type
, pos
, value
);
5989 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5996 __isl_give isl_set
*isl_set_fix(__isl_take isl_set
*set
,
5997 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5999 return set_from_map(isl_map_fix(set_to_map(set
), type
, pos
, value
));
6002 /* Fix the value of the variable at position "pos" of type "type" of "map"
6003 * to be equal to "v".
6005 __isl_give isl_map
*isl_map_fix_val(__isl_take isl_map
*map
,
6006 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6010 map
= isl_map_cow(map
);
6014 if (!isl_val_is_int(v
))
6015 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
6016 "expecting integer value", goto error
);
6017 if (pos
>= isl_map_dim(map
, type
))
6018 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
6019 "index out of bounds", goto error
);
6020 for (i
= map
->n
- 1; i
>= 0; --i
) {
6021 map
->p
[i
] = isl_basic_map_fix_val(map
->p
[i
], type
, pos
,
6023 if (remove_if_empty(map
, i
) < 0)
6026 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6035 /* Fix the value of the variable at position "pos" of type "type" of "set"
6036 * to be equal to "v".
6038 __isl_give isl_set
*isl_set_fix_val(__isl_take isl_set
*set
,
6039 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6041 return isl_map_fix_val(set
, type
, pos
, v
);
6044 struct isl_map
*isl_map_fix_input_si(struct isl_map
*map
,
6045 unsigned input
, int value
)
6047 return isl_map_fix_si(map
, isl_dim_in
, input
, value
);
6050 struct isl_set
*isl_set_fix_dim_si(struct isl_set
*set
, unsigned dim
, int value
)
6052 return set_from_map(isl_map_fix_si(set_to_map(set
),
6053 isl_dim_set
, dim
, value
));
6056 static __isl_give isl_basic_map
*basic_map_bound_si(
6057 __isl_take isl_basic_map
*bmap
,
6058 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
6062 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6063 return isl_basic_map_free(bmap
);
6064 pos
+= isl_basic_map_offset(bmap
, type
);
6065 bmap
= isl_basic_map_cow(bmap
);
6066 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
6067 j
= isl_basic_map_alloc_inequality(bmap
);
6070 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
6072 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
6073 isl_int_set_si(bmap
->ineq
[j
][0], value
);
6075 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
6076 isl_int_set_si(bmap
->ineq
[j
][0], -value
);
6078 bmap
= isl_basic_map_simplify(bmap
);
6079 return isl_basic_map_finalize(bmap
);
6081 isl_basic_map_free(bmap
);
6085 __isl_give isl_basic_map
*isl_basic_map_lower_bound_si(
6086 __isl_take isl_basic_map
*bmap
,
6087 enum isl_dim_type type
, unsigned pos
, int value
)
6089 return basic_map_bound_si(bmap
, type
, pos
, value
, 0);
6092 /* Constrain the values of the given dimension to be no greater than "value".
6094 __isl_give isl_basic_map
*isl_basic_map_upper_bound_si(
6095 __isl_take isl_basic_map
*bmap
,
6096 enum isl_dim_type type
, unsigned pos
, int value
)
6098 return basic_map_bound_si(bmap
, type
, pos
, value
, 1);
6101 struct isl_basic_set
*isl_basic_set_lower_bound_dim(struct isl_basic_set
*bset
,
6102 unsigned dim
, isl_int value
)
6106 bset
= isl_basic_set_cow(bset
);
6107 bset
= isl_basic_set_extend_constraints(bset
, 0, 1);
6108 j
= isl_basic_set_alloc_inequality(bset
);
6111 isl_seq_clr(bset
->ineq
[j
], 1 + isl_basic_set_total_dim(bset
));
6112 isl_int_set_si(bset
->ineq
[j
][1 + isl_basic_set_n_param(bset
) + dim
], 1);
6113 isl_int_neg(bset
->ineq
[j
][0], value
);
6114 bset
= isl_basic_set_simplify(bset
);
6115 return isl_basic_set_finalize(bset
);
6117 isl_basic_set_free(bset
);
6121 static __isl_give isl_map
*map_bound_si(__isl_take isl_map
*map
,
6122 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
6126 map
= isl_map_cow(map
);
6130 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
6131 for (i
= 0; i
< map
->n
; ++i
) {
6132 map
->p
[i
] = basic_map_bound_si(map
->p
[i
],
6133 type
, pos
, value
, upper
);
6137 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6144 __isl_give isl_map
*isl_map_lower_bound_si(__isl_take isl_map
*map
,
6145 enum isl_dim_type type
, unsigned pos
, int value
)
6147 return map_bound_si(map
, type
, pos
, value
, 0);
6150 __isl_give isl_map
*isl_map_upper_bound_si(__isl_take isl_map
*map
,
6151 enum isl_dim_type type
, unsigned pos
, int value
)
6153 return map_bound_si(map
, type
, pos
, value
, 1);
6156 __isl_give isl_set
*isl_set_lower_bound_si(__isl_take isl_set
*set
,
6157 enum isl_dim_type type
, unsigned pos
, int value
)
6159 return set_from_map(isl_map_lower_bound_si(set_to_map(set
),
6163 __isl_give isl_set
*isl_set_upper_bound_si(__isl_take isl_set
*set
,
6164 enum isl_dim_type type
, unsigned pos
, int value
)
6166 return isl_map_upper_bound_si(set
, type
, pos
, value
);
6169 /* Bound the given variable of "bmap" from below (or above is "upper"
6170 * is set) to "value".
6172 static __isl_give isl_basic_map
*basic_map_bound(
6173 __isl_take isl_basic_map
*bmap
,
6174 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
6178 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6179 return isl_basic_map_free(bmap
);
6180 pos
+= isl_basic_map_offset(bmap
, type
);
6181 bmap
= isl_basic_map_cow(bmap
);
6182 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
6183 j
= isl_basic_map_alloc_inequality(bmap
);
6186 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
6188 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
6189 isl_int_set(bmap
->ineq
[j
][0], value
);
6191 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
6192 isl_int_neg(bmap
->ineq
[j
][0], value
);
6194 bmap
= isl_basic_map_simplify(bmap
);
6195 return isl_basic_map_finalize(bmap
);
6197 isl_basic_map_free(bmap
);
6201 /* Bound the given variable of "map" from below (or above is "upper"
6202 * is set) to "value".
6204 static __isl_give isl_map
*map_bound(__isl_take isl_map
*map
,
6205 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
6209 map
= isl_map_cow(map
);
6213 if (pos
>= isl_map_dim(map
, type
))
6214 isl_die(map
->ctx
, isl_error_invalid
,
6215 "index out of bounds", goto error
);
6216 for (i
= map
->n
- 1; i
>= 0; --i
) {
6217 map
->p
[i
] = basic_map_bound(map
->p
[i
], type
, pos
, value
, upper
);
6218 if (remove_if_empty(map
, i
) < 0)
6221 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6228 __isl_give isl_map
*isl_map_lower_bound(__isl_take isl_map
*map
,
6229 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6231 return map_bound(map
, type
, pos
, value
, 0);
6234 __isl_give isl_map
*isl_map_upper_bound(__isl_take isl_map
*map
,
6235 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6237 return map_bound(map
, type
, pos
, value
, 1);
6240 __isl_give isl_set
*isl_set_lower_bound(__isl_take isl_set
*set
,
6241 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6243 return isl_map_lower_bound(set
, type
, pos
, value
);
6246 __isl_give isl_set
*isl_set_upper_bound(__isl_take isl_set
*set
,
6247 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6249 return isl_map_upper_bound(set
, type
, pos
, value
);
6252 /* Force the values of the variable at position "pos" of type "type" of "set"
6253 * to be no smaller than "value".
6255 __isl_give isl_set
*isl_set_lower_bound_val(__isl_take isl_set
*set
,
6256 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
6260 if (!isl_val_is_int(value
))
6261 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
6262 "expecting integer value", goto error
);
6263 set
= isl_set_lower_bound(set
, type
, pos
, value
->n
);
6264 isl_val_free(value
);
6267 isl_val_free(value
);
6272 /* Force the values of the variable at position "pos" of type "type" of "set"
6273 * to be no greater than "value".
6275 __isl_give isl_set
*isl_set_upper_bound_val(__isl_take isl_set
*set
,
6276 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
6280 if (!isl_val_is_int(value
))
6281 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
6282 "expecting integer value", goto error
);
6283 set
= isl_set_upper_bound(set
, type
, pos
, value
->n
);
6284 isl_val_free(value
);
6287 isl_val_free(value
);
6292 struct isl_set
*isl_set_lower_bound_dim(struct isl_set
*set
, unsigned dim
,
6297 set
= isl_set_cow(set
);
6301 isl_assert(set
->ctx
, dim
< isl_set_n_dim(set
), goto error
);
6302 for (i
= 0; i
< set
->n
; ++i
) {
6303 set
->p
[i
] = isl_basic_set_lower_bound_dim(set
->p
[i
], dim
, value
);
6313 struct isl_map
*isl_map_reverse(struct isl_map
*map
)
6317 map
= isl_map_cow(map
);
6321 map
->dim
= isl_space_reverse(map
->dim
);
6324 for (i
= 0; i
< map
->n
; ++i
) {
6325 map
->p
[i
] = isl_basic_map_reverse(map
->p
[i
]);
6329 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6337 #define TYPE isl_pw_multi_aff
6339 #define SUFFIX _pw_multi_aff
6341 #define EMPTY isl_pw_multi_aff_empty
6343 #define ADD isl_pw_multi_aff_union_add
6344 #include "isl_map_lexopt_templ.c"
6346 /* Given a map "map", compute the lexicographically minimal
6347 * (or maximal) image element for each domain element in dom,
6348 * in the form of an isl_pw_multi_aff.
6349 * If "empty" is not NULL, then set *empty to those elements in dom that
6350 * do not have an image element.
6351 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6352 * should be computed over the domain of "map". "empty" is also NULL
6355 * We first compute the lexicographically minimal or maximal element
6356 * in the first basic map. This results in a partial solution "res"
6357 * and a subset "todo" of dom that still need to be handled.
6358 * We then consider each of the remaining maps in "map" and successively
6359 * update both "res" and "todo".
6360 * If "empty" is NULL, then the todo sets are not needed and therefore
6361 * also not computed.
6363 static __isl_give isl_pw_multi_aff
*isl_map_partial_lexopt_aligned_pw_multi_aff(
6364 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6365 __isl_give isl_set
**empty
, unsigned flags
)
6369 isl_pw_multi_aff
*res
;
6372 full
= ISL_FL_ISSET(flags
, ISL_OPT_FULL
);
6373 if (!map
|| (!full
&& !dom
))
6376 if (isl_map_plain_is_empty(map
)) {
6381 return isl_pw_multi_aff_from_map(map
);
6384 res
= basic_map_partial_lexopt_pw_multi_aff(
6385 isl_basic_map_copy(map
->p
[0]),
6386 isl_set_copy(dom
), empty
, flags
);
6390 for (i
= 1; i
< map
->n
; ++i
) {
6391 isl_pw_multi_aff
*res_i
;
6393 res_i
= basic_map_partial_lexopt_pw_multi_aff(
6394 isl_basic_map_copy(map
->p
[i
]),
6395 isl_set_copy(dom
), empty
, flags
);
6397 if (ISL_FL_ISSET(flags
, ISL_OPT_MAX
))
6398 res
= isl_pw_multi_aff_union_lexmax(res
, res_i
);
6400 res
= isl_pw_multi_aff_union_lexmin(res
, res_i
);
6403 todo
= isl_set_intersect(todo
, *empty
);
6422 #define TYPE isl_map
6426 #define EMPTY isl_map_empty
6428 #define ADD isl_map_union_disjoint
6429 #include "isl_map_lexopt_templ.c"
6431 /* Given a map "map", compute the lexicographically minimal
6432 * (or maximal) image element for each domain element in "dom",
6433 * in the form of an isl_map.
6434 * If "empty" is not NULL, then set *empty to those elements in "dom" that
6435 * do not have an image element.
6436 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6437 * should be computed over the domain of "map". "empty" is also NULL
6440 * If the input consists of more than one disjunct, then first
6441 * compute the desired result in the form of an isl_pw_multi_aff and
6442 * then convert that into an isl_map.
6444 * This function used to have an explicit implementation in terms
6445 * of isl_maps, but it would continually intersect the domains of
6446 * partial results with the complement of the domain of the next
6447 * partial solution, potentially leading to an explosion in the number
6448 * of disjuncts if there are several disjuncts in the input.
6449 * An even earlier implementation of this function would look for
6450 * better results in the domain of the partial result and for extra
6451 * results in the complement of this domain, which would lead to
6452 * even more splintering.
6454 static __isl_give isl_map
*isl_map_partial_lexopt_aligned(
6455 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6456 __isl_give isl_set
**empty
, unsigned flags
)
6459 struct isl_map
*res
;
6460 isl_pw_multi_aff
*pma
;
6462 full
= ISL_FL_ISSET(flags
, ISL_OPT_FULL
);
6463 if (!map
|| (!full
&& !dom
))
6466 if (isl_map_plain_is_empty(map
)) {
6475 res
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[0]),
6481 pma
= isl_map_partial_lexopt_aligned_pw_multi_aff(map
, dom
, empty
,
6483 return isl_map_from_pw_multi_aff(pma
);
6492 __isl_give isl_map
*isl_map_partial_lexmax(
6493 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6494 __isl_give isl_set
**empty
)
6496 return isl_map_partial_lexopt(map
, dom
, empty
, ISL_OPT_MAX
);
6499 __isl_give isl_map
*isl_map_partial_lexmin(
6500 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6501 __isl_give isl_set
**empty
)
6503 return isl_map_partial_lexopt(map
, dom
, empty
, 0);
6506 __isl_give isl_set
*isl_set_partial_lexmin(
6507 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
6508 __isl_give isl_set
**empty
)
6510 return set_from_map(isl_map_partial_lexmin(set_to_map(set
),
6514 __isl_give isl_set
*isl_set_partial_lexmax(
6515 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
6516 __isl_give isl_set
**empty
)
6518 return set_from_map(isl_map_partial_lexmax(set_to_map(set
),
6522 /* Compute the lexicographic minimum (or maximum if "flags" includes
6523 * ISL_OPT_MAX) of "bset" over its parametric domain.
6525 __isl_give isl_set
*isl_basic_set_lexopt(__isl_take isl_basic_set
*bset
,
6528 return isl_basic_map_lexopt(bset
, flags
);
6531 __isl_give isl_map
*isl_basic_map_lexmax(__isl_take isl_basic_map
*bmap
)
6533 return isl_basic_map_lexopt(bmap
, ISL_OPT_MAX
);
6536 __isl_give isl_set
*isl_basic_set_lexmin(__isl_take isl_basic_set
*bset
)
6538 return set_from_map(isl_basic_map_lexmin(bset_to_bmap(bset
)));
6541 __isl_give isl_set
*isl_basic_set_lexmax(__isl_take isl_basic_set
*bset
)
6543 return set_from_map(isl_basic_map_lexmax(bset_to_bmap(bset
)));
6546 /* Compute the lexicographic minimum of "bset" over its parametric domain
6547 * for the purpose of quantifier elimination.
6548 * That is, find an explicit representation for all the existentially
6549 * quantified variables in "bset" by computing their lexicographic
6552 static __isl_give isl_set
*isl_basic_set_lexmin_compute_divs(
6553 __isl_take isl_basic_set
*bset
)
6555 return isl_basic_set_lexopt(bset
, ISL_OPT_QE
);
6558 /* Extract the first and only affine expression from list
6559 * and then add it to *pwaff with the given dom.
6560 * This domain is known to be disjoint from other domains
6561 * because of the way isl_basic_map_foreach_lexmax works.
6563 static int update_dim_opt(__isl_take isl_basic_set
*dom
,
6564 __isl_take isl_aff_list
*list
, void *user
)
6566 isl_ctx
*ctx
= isl_basic_set_get_ctx(dom
);
6568 isl_pw_aff
**pwaff
= user
;
6569 isl_pw_aff
*pwaff_i
;
6573 if (isl_aff_list_n_aff(list
) != 1)
6574 isl_die(ctx
, isl_error_internal
,
6575 "expecting single element list", goto error
);
6577 aff
= isl_aff_list_get_aff(list
, 0);
6578 pwaff_i
= isl_pw_aff_alloc(isl_set_from_basic_set(dom
), aff
);
6580 *pwaff
= isl_pw_aff_add_disjoint(*pwaff
, pwaff_i
);
6582 isl_aff_list_free(list
);
6586 isl_basic_set_free(dom
);
6587 isl_aff_list_free(list
);
6591 /* Given a basic map with one output dimension, compute the minimum or
6592 * maximum of that dimension as an isl_pw_aff.
6594 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
6595 * call update_dim_opt on each leaf of the result.
6597 static __isl_give isl_pw_aff
*basic_map_dim_opt(__isl_keep isl_basic_map
*bmap
,
6600 isl_space
*dim
= isl_basic_map_get_space(bmap
);
6604 dim
= isl_space_from_domain(isl_space_domain(dim
));
6605 dim
= isl_space_add_dims(dim
, isl_dim_out
, 1);
6606 pwaff
= isl_pw_aff_empty(dim
);
6608 r
= isl_basic_map_foreach_lexopt(bmap
, max
, &update_dim_opt
, &pwaff
);
6610 return isl_pw_aff_free(pwaff
);
6615 /* Compute the minimum or maximum of the given output dimension
6616 * as a function of the parameters and the input dimensions,
6617 * but independently of the other output dimensions.
6619 * We first project out the other output dimension and then compute
6620 * the "lexicographic" maximum in each basic map, combining the results
6621 * using isl_pw_aff_union_max.
6623 static __isl_give isl_pw_aff
*map_dim_opt(__isl_take isl_map
*map
, int pos
,
6630 n_out
= isl_map_dim(map
, isl_dim_out
);
6631 map
= isl_map_project_out(map
, isl_dim_out
, pos
+ 1, n_out
- (pos
+ 1));
6632 map
= isl_map_project_out(map
, isl_dim_out
, 0, pos
);
6637 isl_space
*dim
= isl_map_get_space(map
);
6639 return isl_pw_aff_empty(dim
);
6642 pwaff
= basic_map_dim_opt(map
->p
[0], max
);
6643 for (i
= 1; i
< map
->n
; ++i
) {
6644 isl_pw_aff
*pwaff_i
;
6646 pwaff_i
= basic_map_dim_opt(map
->p
[i
], max
);
6647 pwaff
= isl_pw_aff_union_opt(pwaff
, pwaff_i
, max
);
6655 /* Compute the minimum of the given output dimension as a function of the
6656 * parameters and input dimensions, but independently of
6657 * the other output dimensions.
6659 __isl_give isl_pw_aff
*isl_map_dim_min(__isl_take isl_map
*map
, int pos
)
6661 return map_dim_opt(map
, pos
, 0);
6664 /* Compute the maximum of the given output dimension as a function of the
6665 * parameters and input dimensions, but independently of
6666 * the other output dimensions.
6668 __isl_give isl_pw_aff
*isl_map_dim_max(__isl_take isl_map
*map
, int pos
)
6670 return map_dim_opt(map
, pos
, 1);
6673 /* Compute the minimum or maximum of the given set dimension
6674 * as a function of the parameters,
6675 * but independently of the other set dimensions.
6677 static __isl_give isl_pw_aff
*set_dim_opt(__isl_take isl_set
*set
, int pos
,
6680 return map_dim_opt(set
, pos
, max
);
6683 /* Compute the maximum of the given set dimension as a function of the
6684 * parameters, but independently of the other set dimensions.
6686 __isl_give isl_pw_aff
*isl_set_dim_max(__isl_take isl_set
*set
, int pos
)
6688 return set_dim_opt(set
, pos
, 1);
6691 /* Compute the minimum of the given set dimension as a function of the
6692 * parameters, but independently of the other set dimensions.
6694 __isl_give isl_pw_aff
*isl_set_dim_min(__isl_take isl_set
*set
, int pos
)
6696 return set_dim_opt(set
, pos
, 0);
6699 /* Apply a preimage specified by "mat" on the parameters of "bset".
6700 * bset is assumed to have only parameters and divs.
6702 static struct isl_basic_set
*basic_set_parameter_preimage(
6703 struct isl_basic_set
*bset
, struct isl_mat
*mat
)
6710 bset
->dim
= isl_space_cow(bset
->dim
);
6714 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
6716 isl_assert(bset
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
6718 bset
->dim
->nparam
= 0;
6719 bset
->dim
->n_out
= nparam
;
6720 bset
= isl_basic_set_preimage(bset
, mat
);
6722 bset
->dim
->nparam
= bset
->dim
->n_out
;
6723 bset
->dim
->n_out
= 0;
6728 isl_basic_set_free(bset
);
6732 /* Apply a preimage specified by "mat" on the parameters of "set".
6733 * set is assumed to have only parameters and divs.
6735 static __isl_give isl_set
*set_parameter_preimage(__isl_take isl_set
*set
,
6736 __isl_take isl_mat
*mat
)
6744 nparam
= isl_set_dim(set
, isl_dim_param
);
6746 if (mat
->n_row
!= 1 + nparam
)
6747 isl_die(isl_set_get_ctx(set
), isl_error_internal
,
6748 "unexpected number of rows", goto error
);
6750 space
= isl_set_get_space(set
);
6751 space
= isl_space_move_dims(space
, isl_dim_set
, 0,
6752 isl_dim_param
, 0, nparam
);
6753 set
= isl_set_reset_space(set
, space
);
6754 set
= isl_set_preimage(set
, mat
);
6755 nparam
= isl_set_dim(set
, isl_dim_out
);
6756 space
= isl_set_get_space(set
);
6757 space
= isl_space_move_dims(space
, isl_dim_param
, 0,
6758 isl_dim_out
, 0, nparam
);
6759 set
= isl_set_reset_space(set
, space
);
6767 /* Intersect the basic set "bset" with the affine space specified by the
6768 * equalities in "eq".
6770 static struct isl_basic_set
*basic_set_append_equalities(
6771 struct isl_basic_set
*bset
, struct isl_mat
*eq
)
6779 bset
= isl_basic_set_extend_space(bset
, isl_space_copy(bset
->dim
), 0,
6784 len
= 1 + isl_space_dim(bset
->dim
, isl_dim_all
) + bset
->extra
;
6785 for (i
= 0; i
< eq
->n_row
; ++i
) {
6786 k
= isl_basic_set_alloc_equality(bset
);
6789 isl_seq_cpy(bset
->eq
[k
], eq
->row
[i
], eq
->n_col
);
6790 isl_seq_clr(bset
->eq
[k
] + eq
->n_col
, len
- eq
->n_col
);
6794 bset
= isl_basic_set_gauss(bset
, NULL
);
6795 bset
= isl_basic_set_finalize(bset
);
6800 isl_basic_set_free(bset
);
6804 /* Intersect the set "set" with the affine space specified by the
6805 * equalities in "eq".
6807 static struct isl_set
*set_append_equalities(struct isl_set
*set
,
6815 for (i
= 0; i
< set
->n
; ++i
) {
6816 set
->p
[i
] = basic_set_append_equalities(set
->p
[i
],
6829 /* Given a basic set "bset" that only involves parameters and existentially
6830 * quantified variables, return the index of the first equality
6831 * that only involves parameters. If there is no such equality then
6832 * return bset->n_eq.
6834 * This function assumes that isl_basic_set_gauss has been called on "bset".
6836 static int first_parameter_equality(__isl_keep isl_basic_set
*bset
)
6839 unsigned nparam
, n_div
;
6844 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
6845 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
6847 for (i
= 0, j
= n_div
- 1; i
< bset
->n_eq
&& j
>= 0; --j
) {
6848 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ j
]))
6855 /* Compute an explicit representation for the existentially quantified
6856 * variables in "bset" by computing the "minimal value" of the set
6857 * variables. Since there are no set variables, the computation of
6858 * the minimal value essentially computes an explicit representation
6859 * of the non-empty part(s) of "bset".
6861 * The input only involves parameters and existentially quantified variables.
6862 * All equalities among parameters have been removed.
6864 * Since the existentially quantified variables in the result are in general
6865 * going to be different from those in the input, we first replace
6866 * them by the minimal number of variables based on their equalities.
6867 * This should simplify the parametric integer programming.
6869 static __isl_give isl_set
*base_compute_divs(__isl_take isl_basic_set
*bset
)
6871 isl_morph
*morph1
, *morph2
;
6877 if (bset
->n_eq
== 0)
6878 return isl_basic_set_lexmin_compute_divs(bset
);
6880 morph1
= isl_basic_set_parameter_compression(bset
);
6881 bset
= isl_morph_basic_set(isl_morph_copy(morph1
), bset
);
6882 bset
= isl_basic_set_lift(bset
);
6883 morph2
= isl_basic_set_variable_compression(bset
, isl_dim_set
);
6884 bset
= isl_morph_basic_set(morph2
, bset
);
6885 n
= isl_basic_set_dim(bset
, isl_dim_set
);
6886 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
6888 set
= isl_basic_set_lexmin_compute_divs(bset
);
6890 set
= isl_morph_set(isl_morph_inverse(morph1
), set
);
6895 /* Project the given basic set onto its parameter domain, possibly introducing
6896 * new, explicit, existential variables in the constraints.
6897 * The input has parameters and (possibly implicit) existential variables.
6898 * The output has the same parameters, but only
6899 * explicit existentially quantified variables.
6901 * The actual projection is performed by pip, but pip doesn't seem
6902 * to like equalities very much, so we first remove the equalities
6903 * among the parameters by performing a variable compression on
6904 * the parameters. Afterward, an inverse transformation is performed
6905 * and the equalities among the parameters are inserted back in.
6907 * The variable compression on the parameters may uncover additional
6908 * equalities that were only implicit before. We therefore check
6909 * if there are any new parameter equalities in the result and
6910 * if so recurse. The removal of parameter equalities is required
6911 * for the parameter compression performed by base_compute_divs.
6913 static struct isl_set
*parameter_compute_divs(struct isl_basic_set
*bset
)
6917 struct isl_mat
*T
, *T2
;
6918 struct isl_set
*set
;
6921 bset
= isl_basic_set_cow(bset
);
6925 if (bset
->n_eq
== 0)
6926 return base_compute_divs(bset
);
6928 bset
= isl_basic_set_gauss(bset
, NULL
);
6931 if (isl_basic_set_plain_is_empty(bset
))
6932 return isl_set_from_basic_set(bset
);
6934 i
= first_parameter_equality(bset
);
6935 if (i
== bset
->n_eq
)
6936 return base_compute_divs(bset
);
6938 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
6939 eq
= isl_mat_sub_alloc6(bset
->ctx
, bset
->eq
, i
, bset
->n_eq
- i
,
6941 eq
= isl_mat_cow(eq
);
6942 T
= isl_mat_variable_compression(isl_mat_copy(eq
), &T2
);
6943 if (T
&& T
->n_col
== 0) {
6947 bset
= isl_basic_set_set_to_empty(bset
);
6948 return isl_set_from_basic_set(bset
);
6950 bset
= basic_set_parameter_preimage(bset
, T
);
6952 i
= first_parameter_equality(bset
);
6955 else if (i
== bset
->n_eq
)
6956 set
= base_compute_divs(bset
);
6958 set
= parameter_compute_divs(bset
);
6959 set
= set_parameter_preimage(set
, T2
);
6960 set
= set_append_equalities(set
, eq
);
6964 /* Insert the divs from "ls" before those of "bmap".
6966 * The number of columns is not changed, which means that the last
6967 * dimensions of "bmap" are being reintepreted as the divs from "ls".
6968 * The caller is responsible for removing the same number of dimensions
6969 * from the space of "bmap".
6971 static __isl_give isl_basic_map
*insert_divs_from_local_space(
6972 __isl_take isl_basic_map
*bmap
, __isl_keep isl_local_space
*ls
)
6978 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
6982 old_n_div
= bmap
->n_div
;
6983 bmap
= insert_div_rows(bmap
, n_div
);
6987 for (i
= 0; i
< n_div
; ++i
) {
6988 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], ls
->div
->n_col
);
6989 isl_seq_clr(bmap
->div
[i
] + ls
->div
->n_col
, old_n_div
);
6995 /* Replace the space of "bmap" by the space and divs of "ls".
6997 * If "ls" has any divs, then we simplify the result since we may
6998 * have discovered some additional equalities that could simplify
6999 * the div expressions.
7001 static __isl_give isl_basic_map
*basic_replace_space_by_local_space(
7002 __isl_take isl_basic_map
*bmap
, __isl_take isl_local_space
*ls
)
7006 bmap
= isl_basic_map_cow(bmap
);
7010 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
7011 bmap
= insert_divs_from_local_space(bmap
, ls
);
7015 isl_space_free(bmap
->dim
);
7016 bmap
->dim
= isl_local_space_get_space(ls
);
7020 isl_local_space_free(ls
);
7022 bmap
= isl_basic_map_simplify(bmap
);
7023 bmap
= isl_basic_map_finalize(bmap
);
7026 isl_basic_map_free(bmap
);
7027 isl_local_space_free(ls
);
7031 /* Replace the space of "map" by the space and divs of "ls".
7033 static __isl_give isl_map
*replace_space_by_local_space(__isl_take isl_map
*map
,
7034 __isl_take isl_local_space
*ls
)
7038 map
= isl_map_cow(map
);
7042 for (i
= 0; i
< map
->n
; ++i
) {
7043 map
->p
[i
] = basic_replace_space_by_local_space(map
->p
[i
],
7044 isl_local_space_copy(ls
));
7048 isl_space_free(map
->dim
);
7049 map
->dim
= isl_local_space_get_space(ls
);
7053 isl_local_space_free(ls
);
7056 isl_local_space_free(ls
);
7061 /* Compute an explicit representation for the existentially
7062 * quantified variables for which do not know any explicit representation yet.
7064 * We first sort the existentially quantified variables so that the
7065 * existentially quantified variables for which we already have an explicit
7066 * representation are placed before those for which we do not.
7067 * The input dimensions, the output dimensions and the existentially
7068 * quantified variables for which we already have an explicit
7069 * representation are then turned into parameters.
7070 * compute_divs returns a map with the same parameters and
7071 * no input or output dimensions and the dimension specification
7072 * is reset to that of the input, including the existentially quantified
7073 * variables for which we already had an explicit representation.
7075 static struct isl_map
*compute_divs(struct isl_basic_map
*bmap
)
7077 struct isl_basic_set
*bset
;
7078 struct isl_set
*set
;
7079 struct isl_map
*map
;
7081 isl_local_space
*ls
;
7088 bmap
= isl_basic_map_sort_divs(bmap
);
7089 bmap
= isl_basic_map_cow(bmap
);
7093 n_known
= isl_basic_map_first_unknown_div(bmap
);
7095 return isl_map_from_basic_map(isl_basic_map_free(bmap
));
7097 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
7098 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
7099 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
7100 dim
= isl_space_set_alloc(bmap
->ctx
,
7101 nparam
+ n_in
+ n_out
+ n_known
, 0);
7105 ls
= isl_basic_map_get_local_space(bmap
);
7106 ls
= isl_local_space_drop_dims(ls
, isl_dim_div
,
7107 n_known
, bmap
->n_div
- n_known
);
7109 for (i
= n_known
; i
< bmap
->n_div
; ++i
)
7110 swap_div(bmap
, i
- n_known
, i
);
7111 bmap
->n_div
-= n_known
;
7112 bmap
->extra
-= n_known
;
7114 bmap
= isl_basic_map_reset_space(bmap
, dim
);
7115 bset
= bset_from_bmap(bmap
);
7117 set
= parameter_compute_divs(bset
);
7118 map
= set_to_map(set
);
7119 map
= replace_space_by_local_space(map
, ls
);
7123 isl_basic_map_free(bmap
);
7127 /* Remove the explicit representation of local variable "div",
7130 __isl_give isl_basic_map
*isl_basic_map_mark_div_unknown(
7131 __isl_take isl_basic_map
*bmap
, int div
)
7135 unknown
= isl_basic_map_div_is_marked_unknown(bmap
, div
);
7137 return isl_basic_map_free(bmap
);
7141 bmap
= isl_basic_map_cow(bmap
);
7144 isl_int_set_si(bmap
->div
[div
][0], 0);
7148 /* Is local variable "div" of "bmap" marked as not having an explicit
7150 * Note that even if "div" is not marked in this way and therefore
7151 * has an explicit representation, this representation may still
7152 * depend (indirectly) on other local variables that do not
7153 * have an explicit representation.
7155 isl_bool
isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map
*bmap
,
7158 if (isl_basic_map_check_range(bmap
, isl_dim_div
, div
, 1) < 0)
7159 return isl_bool_error
;
7160 return isl_int_is_zero(bmap
->div
[div
][0]);
7163 /* Return the position of the first local variable that does not
7164 * have an explicit representation.
7165 * Return the total number of local variables if they all have
7166 * an explicit representation.
7167 * Return -1 on error.
7169 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map
*bmap
)
7176 for (i
= 0; i
< bmap
->n_div
; ++i
) {
7177 if (!isl_basic_map_div_is_known(bmap
, i
))
7183 /* Return the position of the first local variable that does not
7184 * have an explicit representation.
7185 * Return the total number of local variables if they all have
7186 * an explicit representation.
7187 * Return -1 on error.
7189 int isl_basic_set_first_unknown_div(__isl_keep isl_basic_set
*bset
)
7191 return isl_basic_map_first_unknown_div(bset
);
7194 /* Does "bmap" have an explicit representation for all local variables?
7196 isl_bool
isl_basic_map_divs_known(__isl_keep isl_basic_map
*bmap
)
7200 n
= isl_basic_map_dim(bmap
, isl_dim_div
);
7201 first
= isl_basic_map_first_unknown_div(bmap
);
7203 return isl_bool_error
;
7207 /* Do all basic maps in "map" have an explicit representation
7208 * for all local variables?
7210 isl_bool
isl_map_divs_known(__isl_keep isl_map
*map
)
7215 return isl_bool_error
;
7217 for (i
= 0; i
< map
->n
; ++i
) {
7218 int known
= isl_basic_map_divs_known(map
->p
[i
]);
7223 return isl_bool_true
;
7226 /* If bmap contains any unknown divs, then compute explicit
7227 * expressions for them. However, this computation may be
7228 * quite expensive, so first try to remove divs that aren't
7231 struct isl_map
*isl_basic_map_compute_divs(struct isl_basic_map
*bmap
)
7234 struct isl_map
*map
;
7236 known
= isl_basic_map_divs_known(bmap
);
7240 return isl_map_from_basic_map(bmap
);
7242 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
7244 known
= isl_basic_map_divs_known(bmap
);
7248 return isl_map_from_basic_map(bmap
);
7250 map
= compute_divs(bmap
);
7253 isl_basic_map_free(bmap
);
7257 struct isl_map
*isl_map_compute_divs(struct isl_map
*map
)
7261 struct isl_map
*res
;
7268 known
= isl_map_divs_known(map
);
7276 res
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[0]));
7277 for (i
= 1 ; i
< map
->n
; ++i
) {
7279 r2
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[i
]));
7280 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
7281 res
= isl_map_union_disjoint(res
, r2
);
7283 res
= isl_map_union(res
, r2
);
7290 struct isl_set
*isl_basic_set_compute_divs(struct isl_basic_set
*bset
)
7292 return set_from_map(isl_basic_map_compute_divs(bset_to_bmap(bset
)));
7295 struct isl_set
*isl_set_compute_divs(struct isl_set
*set
)
7297 return set_from_map(isl_map_compute_divs(set_to_map(set
)));
7300 struct isl_set
*isl_map_domain(struct isl_map
*map
)
7303 struct isl_set
*set
;
7308 map
= isl_map_cow(map
);
7312 set
= set_from_map(map
);
7313 set
->dim
= isl_space_domain(set
->dim
);
7316 for (i
= 0; i
< map
->n
; ++i
) {
7317 set
->p
[i
] = isl_basic_map_domain(map
->p
[i
]);
7321 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
7322 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
7329 /* Return the union of "map1" and "map2", where we assume for now that
7330 * "map1" and "map2" are disjoint. Note that the basic maps inside
7331 * "map1" or "map2" may not be disjoint from each other.
7332 * Also note that this function is also called from isl_map_union,
7333 * which takes care of handling the situation where "map1" and "map2"
7334 * may not be disjoint.
7336 * If one of the inputs is empty, we can simply return the other input.
7337 * Similarly, if one of the inputs is universal, then it is equal to the union.
7339 static __isl_give isl_map
*map_union_disjoint(__isl_take isl_map
*map1
,
7340 __isl_take isl_map
*map2
)
7344 struct isl_map
*map
= NULL
;
7350 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
7351 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
7352 "spaces don't match", goto error
);
7363 is_universe
= isl_map_plain_is_universe(map1
);
7364 if (is_universe
< 0)
7371 is_universe
= isl_map_plain_is_universe(map2
);
7372 if (is_universe
< 0)
7379 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
7380 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
7381 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
7383 map
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
7384 map1
->n
+ map2
->n
, flags
);
7387 for (i
= 0; i
< map1
->n
; ++i
) {
7388 map
= isl_map_add_basic_map(map
,
7389 isl_basic_map_copy(map1
->p
[i
]));
7393 for (i
= 0; i
< map2
->n
; ++i
) {
7394 map
= isl_map_add_basic_map(map
,
7395 isl_basic_map_copy(map2
->p
[i
]));
7409 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7410 * guaranteed to be disjoint by the caller.
7412 * Note that this functions is called from within isl_map_make_disjoint,
7413 * so we have to be careful not to touch the constraints of the inputs
7416 __isl_give isl_map
*isl_map_union_disjoint(__isl_take isl_map
*map1
,
7417 __isl_take isl_map
*map2
)
7419 return isl_map_align_params_map_map_and(map1
, map2
, &map_union_disjoint
);
7422 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7423 * not be disjoint. The parameters are assumed to have been aligned.
7425 * We currently simply call map_union_disjoint, the internal operation
7426 * of which does not really depend on the inputs being disjoint.
7427 * If the result contains more than one basic map, then we clear
7428 * the disjoint flag since the result may contain basic maps from
7429 * both inputs and these are not guaranteed to be disjoint.
7431 * As a special case, if "map1" and "map2" are obviously equal,
7432 * then we simply return "map1".
7434 static __isl_give isl_map
*map_union_aligned(__isl_take isl_map
*map1
,
7435 __isl_take isl_map
*map2
)
7442 equal
= isl_map_plain_is_equal(map1
, map2
);
7450 map1
= map_union_disjoint(map1
, map2
);
7454 ISL_F_CLR(map1
, ISL_MAP_DISJOINT
);
7462 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7465 __isl_give isl_map
*isl_map_union(__isl_take isl_map
*map1
,
7466 __isl_take isl_map
*map2
)
7468 return isl_map_align_params_map_map_and(map1
, map2
, &map_union_aligned
);
7471 struct isl_set
*isl_set_union_disjoint(
7472 struct isl_set
*set1
, struct isl_set
*set2
)
7474 return set_from_map(isl_map_union_disjoint(set_to_map(set1
),
7478 struct isl_set
*isl_set_union(struct isl_set
*set1
, struct isl_set
*set2
)
7480 return set_from_map(isl_map_union(set_to_map(set1
), set_to_map(set2
)));
7483 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7486 * "map" and "set" are assumed to be compatible and non-NULL.
7488 static __isl_give isl_map
*map_intersect_set(__isl_take isl_map
*map
,
7489 __isl_take isl_set
*set
,
7490 __isl_give isl_basic_map
*fn(__isl_take isl_basic_map
*bmap
,
7491 __isl_take isl_basic_set
*bset
))
7494 struct isl_map
*result
;
7497 if (isl_set_plain_is_universe(set
)) {
7502 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
) &&
7503 ISL_F_ISSET(set
, ISL_MAP_DISJOINT
))
7504 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
7506 result
= isl_map_alloc_space(isl_space_copy(map
->dim
),
7507 map
->n
* set
->n
, flags
);
7508 for (i
= 0; result
&& i
< map
->n
; ++i
)
7509 for (j
= 0; j
< set
->n
; ++j
) {
7510 result
= isl_map_add_basic_map(result
,
7511 fn(isl_basic_map_copy(map
->p
[i
]),
7512 isl_basic_set_copy(set
->p
[j
])));
7522 static __isl_give isl_map
*map_intersect_range(__isl_take isl_map
*map
,
7523 __isl_take isl_set
*set
)
7528 if (!isl_map_compatible_range(map
, set
))
7529 isl_die(set
->ctx
, isl_error_invalid
,
7530 "incompatible spaces", goto error
);
7532 return map_intersect_set(map
, set
, &isl_basic_map_intersect_range
);
7539 __isl_give isl_map
*isl_map_intersect_range(__isl_take isl_map
*map
,
7540 __isl_take isl_set
*set
)
7542 return isl_map_align_params_map_map_and(map
, set
, &map_intersect_range
);
7545 static __isl_give isl_map
*map_intersect_domain(__isl_take isl_map
*map
,
7546 __isl_take isl_set
*set
)
7551 if (!isl_map_compatible_domain(map
, set
))
7552 isl_die(set
->ctx
, isl_error_invalid
,
7553 "incompatible spaces", goto error
);
7555 return map_intersect_set(map
, set
, &isl_basic_map_intersect_domain
);
7562 __isl_give isl_map
*isl_map_intersect_domain(__isl_take isl_map
*map
,
7563 __isl_take isl_set
*set
)
7565 return isl_map_align_params_map_map_and(map
, set
,
7566 &map_intersect_domain
);
7569 static __isl_give isl_map
*map_apply_domain(__isl_take isl_map
*map1
,
7570 __isl_take isl_map
*map2
)
7574 map1
= isl_map_reverse(map1
);
7575 map1
= isl_map_apply_range(map1
, map2
);
7576 return isl_map_reverse(map1
);
7583 __isl_give isl_map
*isl_map_apply_domain(__isl_take isl_map
*map1
,
7584 __isl_take isl_map
*map2
)
7586 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_domain
);
7589 static __isl_give isl_map
*map_apply_range(__isl_take isl_map
*map1
,
7590 __isl_take isl_map
*map2
)
7592 isl_space
*dim_result
;
7593 struct isl_map
*result
;
7599 dim_result
= isl_space_join(isl_space_copy(map1
->dim
),
7600 isl_space_copy(map2
->dim
));
7602 result
= isl_map_alloc_space(dim_result
, map1
->n
* map2
->n
, 0);
7605 for (i
= 0; i
< map1
->n
; ++i
)
7606 for (j
= 0; j
< map2
->n
; ++j
) {
7607 result
= isl_map_add_basic_map(result
,
7608 isl_basic_map_apply_range(
7609 isl_basic_map_copy(map1
->p
[i
]),
7610 isl_basic_map_copy(map2
->p
[j
])));
7616 if (result
&& result
->n
<= 1)
7617 ISL_F_SET(result
, ISL_MAP_DISJOINT
);
7625 __isl_give isl_map
*isl_map_apply_range(__isl_take isl_map
*map1
,
7626 __isl_take isl_map
*map2
)
7628 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_range
);
7632 * returns range - domain
7634 struct isl_basic_set
*isl_basic_map_deltas(struct isl_basic_map
*bmap
)
7636 isl_space
*target_space
;
7637 struct isl_basic_set
*bset
;
7644 isl_assert(bmap
->ctx
, isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
7645 bmap
->dim
, isl_dim_out
),
7647 target_space
= isl_space_domain(isl_basic_map_get_space(bmap
));
7648 dim
= isl_basic_map_n_in(bmap
);
7649 nparam
= isl_basic_map_n_param(bmap
);
7650 bmap
= isl_basic_map_from_range(isl_basic_map_wrap(bmap
));
7651 bmap
= isl_basic_map_add_dims(bmap
, isl_dim_in
, dim
);
7652 bmap
= isl_basic_map_extend_constraints(bmap
, dim
, 0);
7653 for (i
= 0; i
< dim
; ++i
) {
7654 int j
= isl_basic_map_alloc_equality(bmap
);
7656 bmap
= isl_basic_map_free(bmap
);
7659 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
7660 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
7661 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], 1);
7662 isl_int_set_si(bmap
->eq
[j
][1+nparam
+2*dim
+i
], -1);
7664 bset
= isl_basic_map_domain(bmap
);
7665 bset
= isl_basic_set_reset_space(bset
, target_space
);
7668 isl_basic_map_free(bmap
);
7673 * returns range - domain
7675 __isl_give isl_set
*isl_map_deltas(__isl_take isl_map
*map
)
7679 struct isl_set
*result
;
7684 isl_assert(map
->ctx
, isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
7685 map
->dim
, isl_dim_out
),
7687 dim
= isl_map_get_space(map
);
7688 dim
= isl_space_domain(dim
);
7689 result
= isl_set_alloc_space(dim
, map
->n
, 0);
7692 for (i
= 0; i
< map
->n
; ++i
)
7693 result
= isl_set_add_basic_set(result
,
7694 isl_basic_map_deltas(isl_basic_map_copy(map
->p
[i
])));
7703 * returns [domain -> range] -> range - domain
7705 __isl_give isl_basic_map
*isl_basic_map_deltas_map(
7706 __isl_take isl_basic_map
*bmap
)
7710 isl_basic_map
*domain
;
7714 if (!isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
7715 bmap
->dim
, isl_dim_out
))
7716 isl_die(bmap
->ctx
, isl_error_invalid
,
7717 "domain and range don't match", goto error
);
7719 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
7720 n
= isl_basic_map_dim(bmap
, isl_dim_in
);
7722 dim
= isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap
)));
7723 domain
= isl_basic_map_universe(dim
);
7725 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
7726 bmap
= isl_basic_map_apply_range(bmap
, domain
);
7727 bmap
= isl_basic_map_extend_constraints(bmap
, n
, 0);
7729 total
= isl_basic_map_total_dim(bmap
);
7731 for (i
= 0; i
< n
; ++i
) {
7732 k
= isl_basic_map_alloc_equality(bmap
);
7735 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
7736 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], 1);
7737 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ i
], -1);
7738 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ n
+ i
], 1);
7741 bmap
= isl_basic_map_gauss(bmap
, NULL
);
7742 return isl_basic_map_finalize(bmap
);
7744 isl_basic_map_free(bmap
);
7749 * returns [domain -> range] -> range - domain
7751 __isl_give isl_map
*isl_map_deltas_map(__isl_take isl_map
*map
)
7754 isl_space
*domain_dim
;
7759 if (!isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
7760 map
->dim
, isl_dim_out
))
7761 isl_die(map
->ctx
, isl_error_invalid
,
7762 "domain and range don't match", goto error
);
7764 map
= isl_map_cow(map
);
7768 domain_dim
= isl_space_from_range(isl_space_domain(isl_map_get_space(map
)));
7769 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
7770 map
->dim
= isl_space_join(map
->dim
, domain_dim
);
7773 for (i
= 0; i
< map
->n
; ++i
) {
7774 map
->p
[i
] = isl_basic_map_deltas_map(map
->p
[i
]);
7778 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
7785 static __isl_give isl_basic_map
*basic_map_identity(__isl_take isl_space
*dims
)
7787 struct isl_basic_map
*bmap
;
7795 nparam
= dims
->nparam
;
7797 bmap
= isl_basic_map_alloc_space(dims
, 0, dim
, 0);
7801 for (i
= 0; i
< dim
; ++i
) {
7802 int j
= isl_basic_map_alloc_equality(bmap
);
7805 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
7806 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
7807 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], -1);
7809 return isl_basic_map_finalize(bmap
);
7811 isl_basic_map_free(bmap
);
7815 __isl_give isl_basic_map
*isl_basic_map_identity(__isl_take isl_space
*dim
)
7819 if (dim
->n_in
!= dim
->n_out
)
7820 isl_die(dim
->ctx
, isl_error_invalid
,
7821 "number of input and output dimensions needs to be "
7822 "the same", goto error
);
7823 return basic_map_identity(dim
);
7825 isl_space_free(dim
);
7829 __isl_give isl_map
*isl_map_identity(__isl_take isl_space
*dim
)
7831 return isl_map_from_basic_map(isl_basic_map_identity(dim
));
7834 __isl_give isl_map
*isl_set_identity(__isl_take isl_set
*set
)
7836 isl_space
*dim
= isl_set_get_space(set
);
7838 id
= isl_map_identity(isl_space_map_from_set(dim
));
7839 return isl_map_intersect_range(id
, set
);
7842 /* Construct a basic set with all set dimensions having only non-negative
7845 __isl_give isl_basic_set
*isl_basic_set_positive_orthant(
7846 __isl_take isl_space
*space
)
7851 struct isl_basic_set
*bset
;
7855 nparam
= space
->nparam
;
7857 bset
= isl_basic_set_alloc_space(space
, 0, 0, dim
);
7860 for (i
= 0; i
< dim
; ++i
) {
7861 int k
= isl_basic_set_alloc_inequality(bset
);
7864 isl_seq_clr(bset
->ineq
[k
], 1 + isl_basic_set_total_dim(bset
));
7865 isl_int_set_si(bset
->ineq
[k
][1 + nparam
+ i
], 1);
7869 isl_basic_set_free(bset
);
7873 /* Construct the half-space x_pos >= 0.
7875 static __isl_give isl_basic_set
*nonneg_halfspace(__isl_take isl_space
*dim
,
7879 isl_basic_set
*nonneg
;
7881 nonneg
= isl_basic_set_alloc_space(dim
, 0, 0, 1);
7882 k
= isl_basic_set_alloc_inequality(nonneg
);
7885 isl_seq_clr(nonneg
->ineq
[k
], 1 + isl_basic_set_total_dim(nonneg
));
7886 isl_int_set_si(nonneg
->ineq
[k
][pos
], 1);
7888 return isl_basic_set_finalize(nonneg
);
7890 isl_basic_set_free(nonneg
);
7894 /* Construct the half-space x_pos <= -1.
7896 static __isl_give isl_basic_set
*neg_halfspace(__isl_take isl_space
*dim
, int pos
)
7901 neg
= isl_basic_set_alloc_space(dim
, 0, 0, 1);
7902 k
= isl_basic_set_alloc_inequality(neg
);
7905 isl_seq_clr(neg
->ineq
[k
], 1 + isl_basic_set_total_dim(neg
));
7906 isl_int_set_si(neg
->ineq
[k
][0], -1);
7907 isl_int_set_si(neg
->ineq
[k
][pos
], -1);
7909 return isl_basic_set_finalize(neg
);
7911 isl_basic_set_free(neg
);
7915 __isl_give isl_set
*isl_set_split_dims(__isl_take isl_set
*set
,
7916 enum isl_dim_type type
, unsigned first
, unsigned n
)
7920 isl_basic_set
*nonneg
;
7928 isl_assert(set
->ctx
, first
+ n
<= isl_set_dim(set
, type
), goto error
);
7930 offset
= pos(set
->dim
, type
);
7931 for (i
= 0; i
< n
; ++i
) {
7932 nonneg
= nonneg_halfspace(isl_set_get_space(set
),
7933 offset
+ first
+ i
);
7934 neg
= neg_halfspace(isl_set_get_space(set
), offset
+ first
+ i
);
7936 set
= isl_set_intersect(set
, isl_basic_set_union(nonneg
, neg
));
7945 static int foreach_orthant(__isl_take isl_set
*set
, int *signs
, int first
,
7946 int len
, int (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
7953 if (isl_set_plain_is_empty(set
)) {
7958 return fn(set
, signs
, user
);
7961 half
= isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set
),
7963 half
= isl_set_intersect(half
, isl_set_copy(set
));
7964 if (foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
) < 0)
7968 half
= isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set
),
7970 half
= isl_set_intersect(half
, set
);
7971 return foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
);
7977 /* Call "fn" on the intersections of "set" with each of the orthants
7978 * (except for obviously empty intersections). The orthant is identified
7979 * by the signs array, with each entry having value 1 or -1 according
7980 * to the sign of the corresponding variable.
7982 int isl_set_foreach_orthant(__isl_keep isl_set
*set
,
7983 int (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
7993 if (isl_set_plain_is_empty(set
))
7996 nparam
= isl_set_dim(set
, isl_dim_param
);
7997 nvar
= isl_set_dim(set
, isl_dim_set
);
7999 signs
= isl_alloc_array(set
->ctx
, int, nparam
+ nvar
);
8001 r
= foreach_orthant(isl_set_copy(set
), signs
, 0, nparam
+ nvar
,
8009 isl_bool
isl_set_is_equal(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
8011 return isl_map_is_equal(set_to_map(set1
), set_to_map(set2
));
8014 isl_bool
isl_basic_map_is_subset(__isl_keep isl_basic_map
*bmap1
,
8015 __isl_keep isl_basic_map
*bmap2
)
8018 struct isl_map
*map1
;
8019 struct isl_map
*map2
;
8021 if (!bmap1
|| !bmap2
)
8022 return isl_bool_error
;
8024 map1
= isl_map_from_basic_map(isl_basic_map_copy(bmap1
));
8025 map2
= isl_map_from_basic_map(isl_basic_map_copy(bmap2
));
8027 is_subset
= isl_map_is_subset(map1
, map2
);
8035 isl_bool
isl_basic_set_is_subset(__isl_keep isl_basic_set
*bset1
,
8036 __isl_keep isl_basic_set
*bset2
)
8038 return isl_basic_map_is_subset(bset1
, bset2
);
8041 isl_bool
isl_basic_map_is_equal(__isl_keep isl_basic_map
*bmap1
,
8042 __isl_keep isl_basic_map
*bmap2
)
8046 if (!bmap1
|| !bmap2
)
8047 return isl_bool_error
;
8048 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
8049 if (is_subset
!= isl_bool_true
)
8051 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
8055 isl_bool
isl_basic_set_is_equal(__isl_keep isl_basic_set
*bset1
,
8056 __isl_keep isl_basic_set
*bset2
)
8058 return isl_basic_map_is_equal(
8059 bset_to_bmap(bset1
), bset_to_bmap(bset2
));
8062 isl_bool
isl_map_is_empty(__isl_keep isl_map
*map
)
8068 return isl_bool_error
;
8069 for (i
= 0; i
< map
->n
; ++i
) {
8070 is_empty
= isl_basic_map_is_empty(map
->p
[i
]);
8072 return isl_bool_error
;
8074 return isl_bool_false
;
8076 return isl_bool_true
;
8079 isl_bool
isl_map_plain_is_empty(__isl_keep isl_map
*map
)
8081 return map
? map
->n
== 0 : isl_bool_error
;
8084 isl_bool
isl_set_plain_is_empty(__isl_keep isl_set
*set
)
8086 return set
? set
->n
== 0 : isl_bool_error
;
8089 isl_bool
isl_set_is_empty(__isl_keep isl_set
*set
)
8091 return isl_map_is_empty(set_to_map(set
));
8094 int isl_map_has_equal_space(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
8099 return isl_space_is_equal(map1
->dim
, map2
->dim
);
8102 int isl_set_has_equal_space(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
8107 return isl_space_is_equal(set1
->dim
, set2
->dim
);
8110 static isl_bool
map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
8115 return isl_bool_error
;
8116 is_subset
= isl_map_is_subset(map1
, map2
);
8117 if (is_subset
!= isl_bool_true
)
8119 is_subset
= isl_map_is_subset(map2
, map1
);
8123 /* Is "map1" equal to "map2"?
8125 * First check if they are obviously equal.
8126 * If not, then perform a more detailed analysis.
8128 isl_bool
isl_map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
8132 equal
= isl_map_plain_is_equal(map1
, map2
);
8133 if (equal
< 0 || equal
)
8135 return isl_map_align_params_map_map_and_test(map1
, map2
, &map_is_equal
);
8138 isl_bool
isl_basic_map_is_strict_subset(
8139 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
8143 if (!bmap1
|| !bmap2
)
8144 return isl_bool_error
;
8145 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
8146 if (is_subset
!= isl_bool_true
)
8148 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
8149 if (is_subset
== isl_bool_error
)
8154 isl_bool
isl_map_is_strict_subset(__isl_keep isl_map
*map1
,
8155 __isl_keep isl_map
*map2
)
8160 return isl_bool_error
;
8161 is_subset
= isl_map_is_subset(map1
, map2
);
8162 if (is_subset
!= isl_bool_true
)
8164 is_subset
= isl_map_is_subset(map2
, map1
);
8165 if (is_subset
== isl_bool_error
)
8170 isl_bool
isl_set_is_strict_subset(__isl_keep isl_set
*set1
,
8171 __isl_keep isl_set
*set2
)
8173 return isl_map_is_strict_subset(set_to_map(set1
), set_to_map(set2
));
8176 /* Is "bmap" obviously equal to the universe with the same space?
8178 * That is, does it not have any constraints?
8180 isl_bool
isl_basic_map_plain_is_universe(__isl_keep isl_basic_map
*bmap
)
8183 return isl_bool_error
;
8184 return bmap
->n_eq
== 0 && bmap
->n_ineq
== 0;
8187 /* Is "bset" obviously equal to the universe with the same space?
8189 isl_bool
isl_basic_set_plain_is_universe(__isl_keep isl_basic_set
*bset
)
8191 return isl_basic_map_plain_is_universe(bset
);
8194 /* If "c" does not involve any existentially quantified variables,
8195 * then set *univ to false and abort
8197 static isl_stat
involves_divs(__isl_take isl_constraint
*c
, void *user
)
8199 isl_bool
*univ
= user
;
8202 n
= isl_constraint_dim(c
, isl_dim_div
);
8203 *univ
= isl_constraint_involves_dims(c
, isl_dim_div
, 0, n
);
8204 isl_constraint_free(c
);
8205 if (*univ
< 0 || !*univ
)
8206 return isl_stat_error
;
8210 /* Is "bmap" equal to the universe with the same space?
8212 * First check if it is obviously equal to the universe.
8213 * If not and if there are any constraints not involving
8214 * existentially quantified variables, then it is certainly
8215 * not equal to the universe.
8216 * Otherwise, check if the universe is a subset of "bmap".
8218 isl_bool
isl_basic_map_is_universe(__isl_keep isl_basic_map
*bmap
)
8221 isl_basic_map
*test
;
8223 univ
= isl_basic_map_plain_is_universe(bmap
);
8224 if (univ
< 0 || univ
)
8226 if (isl_basic_map_dim(bmap
, isl_dim_div
) == 0)
8227 return isl_bool_false
;
8228 univ
= isl_bool_true
;
8229 if (isl_basic_map_foreach_constraint(bmap
, &involves_divs
, &univ
) < 0 &&
8231 return isl_bool_error
;
8232 if (univ
< 0 || !univ
)
8234 test
= isl_basic_map_universe(isl_basic_map_get_space(bmap
));
8235 univ
= isl_basic_map_is_subset(test
, bmap
);
8236 isl_basic_map_free(test
);
8240 /* Is "bset" equal to the universe with the same space?
8242 isl_bool
isl_basic_set_is_universe(__isl_keep isl_basic_set
*bset
)
8244 return isl_basic_map_is_universe(bset
);
8247 isl_bool
isl_map_plain_is_universe(__isl_keep isl_map
*map
)
8252 return isl_bool_error
;
8254 for (i
= 0; i
< map
->n
; ++i
) {
8255 isl_bool r
= isl_basic_map_plain_is_universe(map
->p
[i
]);
8260 return isl_bool_false
;
8263 isl_bool
isl_set_plain_is_universe(__isl_keep isl_set
*set
)
8265 return isl_map_plain_is_universe(set_to_map(set
));
8268 isl_bool
isl_basic_map_is_empty(__isl_keep isl_basic_map
*bmap
)
8270 struct isl_basic_set
*bset
= NULL
;
8271 struct isl_vec
*sample
= NULL
;
8272 isl_bool empty
, non_empty
;
8275 return isl_bool_error
;
8277 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
8278 return isl_bool_true
;
8280 if (isl_basic_map_plain_is_universe(bmap
))
8281 return isl_bool_false
;
8283 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
8284 struct isl_basic_map
*copy
= isl_basic_map_copy(bmap
);
8285 copy
= isl_basic_map_remove_redundancies(copy
);
8286 empty
= isl_basic_map_plain_is_empty(copy
);
8287 isl_basic_map_free(copy
);
8291 non_empty
= isl_basic_map_plain_is_non_empty(bmap
);
8293 return isl_bool_error
;
8295 return isl_bool_false
;
8296 isl_vec_free(bmap
->sample
);
8297 bmap
->sample
= NULL
;
8298 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
8300 return isl_bool_error
;
8301 sample
= isl_basic_set_sample_vec(bset
);
8303 return isl_bool_error
;
8304 empty
= sample
->size
== 0;
8305 isl_vec_free(bmap
->sample
);
8306 bmap
->sample
= sample
;
8308 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
8313 isl_bool
isl_basic_map_plain_is_empty(__isl_keep isl_basic_map
*bmap
)
8316 return isl_bool_error
;
8317 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
);
8320 isl_bool
isl_basic_set_plain_is_empty(__isl_keep isl_basic_set
*bset
)
8323 return isl_bool_error
;
8324 return ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
);
8327 /* Is "bmap" known to be non-empty?
8329 * That is, is the cached sample still valid?
8331 isl_bool
isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map
*bmap
)
8336 return isl_bool_error
;
8338 return isl_bool_false
;
8339 total
= 1 + isl_basic_map_total_dim(bmap
);
8340 if (bmap
->sample
->size
!= total
)
8341 return isl_bool_false
;
8342 return isl_basic_map_contains(bmap
, bmap
->sample
);
8345 isl_bool
isl_basic_set_is_empty(__isl_keep isl_basic_set
*bset
)
8347 return isl_basic_map_is_empty(bset_to_bmap(bset
));
8350 struct isl_map
*isl_basic_map_union(
8351 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
8353 struct isl_map
*map
;
8354 if (!bmap1
|| !bmap2
)
8357 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
8359 map
= isl_map_alloc_space(isl_space_copy(bmap1
->dim
), 2, 0);
8362 map
= isl_map_add_basic_map(map
, bmap1
);
8363 map
= isl_map_add_basic_map(map
, bmap2
);
8366 isl_basic_map_free(bmap1
);
8367 isl_basic_map_free(bmap2
);
8371 struct isl_set
*isl_basic_set_union(
8372 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
8374 return set_from_map(isl_basic_map_union(bset_to_bmap(bset1
),
8375 bset_to_bmap(bset2
)));
8378 /* Order divs such that any div only depends on previous divs */
8379 struct isl_basic_map
*isl_basic_map_order_divs(struct isl_basic_map
*bmap
)
8387 off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
8389 for (i
= 0; i
< bmap
->n_div
; ++i
) {
8391 if (isl_int_is_zero(bmap
->div
[i
][0]))
8393 pos
= isl_seq_first_non_zero(bmap
->div
[i
]+1+1+off
+i
,
8398 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_internal
,
8399 "integer division depends on itself",
8400 return isl_basic_map_free(bmap
));
8401 isl_basic_map_swap_div(bmap
, i
, i
+ pos
);
8407 struct isl_basic_set
*isl_basic_set_order_divs(struct isl_basic_set
*bset
)
8409 return bset_from_bmap(isl_basic_map_order_divs(bset_to_bmap(bset
)));
8412 __isl_give isl_map
*isl_map_order_divs(__isl_take isl_map
*map
)
8419 for (i
= 0; i
< map
->n
; ++i
) {
8420 map
->p
[i
] = isl_basic_map_order_divs(map
->p
[i
]);
8431 /* Sort the local variables of "bset".
8433 __isl_give isl_basic_set
*isl_basic_set_sort_divs(
8434 __isl_take isl_basic_set
*bset
)
8436 return bset_from_bmap(isl_basic_map_sort_divs(bset_to_bmap(bset
)));
8439 /* Apply the expansion computed by isl_merge_divs.
8440 * The expansion itself is given by "exp" while the resulting
8441 * list of divs is given by "div".
8443 * Move the integer divisions of "bmap" into the right position
8444 * according to "exp" and then introduce the additional integer
8445 * divisions, adding div constraints.
8446 * The moving should be done first to avoid moving coefficients
8447 * in the definitions of the extra integer divisions.
8449 __isl_give isl_basic_map
*isl_basic_map_expand_divs(
8450 __isl_take isl_basic_map
*bmap
, __isl_take isl_mat
*div
, int *exp
)
8455 bmap
= isl_basic_map_cow(bmap
);
8459 if (div
->n_row
< bmap
->n_div
)
8460 isl_die(isl_mat_get_ctx(div
), isl_error_invalid
,
8461 "not an expansion", goto error
);
8463 n_div
= bmap
->n_div
;
8464 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
8465 div
->n_row
- n_div
, 0,
8466 2 * (div
->n_row
- n_div
));
8468 for (i
= n_div
; i
< div
->n_row
; ++i
)
8469 if (isl_basic_map_alloc_div(bmap
) < 0)
8472 for (j
= n_div
- 1; j
>= 0; --j
) {
8475 isl_basic_map_swap_div(bmap
, j
, exp
[j
]);
8478 for (i
= 0; i
< div
->n_row
; ++i
) {
8479 if (j
< n_div
&& exp
[j
] == i
) {
8482 isl_seq_cpy(bmap
->div
[i
], div
->row
[i
], div
->n_col
);
8483 if (isl_basic_map_div_is_marked_unknown(bmap
, i
))
8485 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
8493 isl_basic_map_free(bmap
);
8498 /* Apply the expansion computed by isl_merge_divs.
8499 * The expansion itself is given by "exp" while the resulting
8500 * list of divs is given by "div".
8502 __isl_give isl_basic_set
*isl_basic_set_expand_divs(
8503 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*div
, int *exp
)
8505 return isl_basic_map_expand_divs(bset
, div
, exp
);
8508 /* Look for a div in dst that corresponds to the div "div" in src.
8509 * The divs before "div" in src and dst are assumed to be the same.
8511 * Returns -1 if no corresponding div was found and the position
8512 * of the corresponding div in dst otherwise.
8514 static int find_div(struct isl_basic_map
*dst
,
8515 struct isl_basic_map
*src
, unsigned div
)
8519 unsigned total
= isl_space_dim(src
->dim
, isl_dim_all
);
8521 isl_assert(dst
->ctx
, div
<= dst
->n_div
, return -1);
8522 for (i
= div
; i
< dst
->n_div
; ++i
)
8523 if (isl_seq_eq(dst
->div
[i
], src
->div
[div
], 1+1+total
+div
) &&
8524 isl_seq_first_non_zero(dst
->div
[i
]+1+1+total
+div
,
8525 dst
->n_div
- div
) == -1)
8530 /* Align the divs of "dst" to those of "src", adding divs from "src"
8531 * if needed. That is, make sure that the first src->n_div divs
8532 * of the result are equal to those of src.
8534 * The result is not finalized as by design it will have redundant
8535 * divs if any divs from "src" were copied.
8537 __isl_give isl_basic_map
*isl_basic_map_align_divs(
8538 __isl_take isl_basic_map
*dst
, __isl_keep isl_basic_map
*src
)
8541 int known
, extended
;
8545 return isl_basic_map_free(dst
);
8547 if (src
->n_div
== 0)
8550 known
= isl_basic_map_divs_known(src
);
8552 return isl_basic_map_free(dst
);
8554 isl_die(isl_basic_map_get_ctx(src
), isl_error_invalid
,
8555 "some src divs are unknown",
8556 return isl_basic_map_free(dst
));
8558 src
= isl_basic_map_order_divs(src
);
8561 total
= isl_space_dim(src
->dim
, isl_dim_all
);
8562 for (i
= 0; i
< src
->n_div
; ++i
) {
8563 int j
= find_div(dst
, src
, i
);
8566 int extra
= src
->n_div
- i
;
8567 dst
= isl_basic_map_cow(dst
);
8570 dst
= isl_basic_map_extend_space(dst
,
8571 isl_space_copy(dst
->dim
),
8572 extra
, 0, 2 * extra
);
8575 j
= isl_basic_map_alloc_div(dst
);
8577 return isl_basic_map_free(dst
);
8578 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
+i
);
8579 isl_seq_clr(dst
->div
[j
]+1+1+total
+i
, dst
->n_div
- i
);
8580 if (isl_basic_map_add_div_constraints(dst
, j
) < 0)
8581 return isl_basic_map_free(dst
);
8584 isl_basic_map_swap_div(dst
, i
, j
);
8589 struct isl_basic_set
*isl_basic_set_align_divs(
8590 struct isl_basic_set
*dst
, struct isl_basic_set
*src
)
8592 return bset_from_bmap(isl_basic_map_align_divs(bset_to_bmap(dst
),
8593 bset_to_bmap(src
)));
8596 struct isl_map
*isl_map_align_divs(struct isl_map
*map
)
8604 map
= isl_map_compute_divs(map
);
8605 map
= isl_map_cow(map
);
8609 for (i
= 1; i
< map
->n
; ++i
)
8610 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], map
->p
[i
]);
8611 for (i
= 1; i
< map
->n
; ++i
) {
8612 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], map
->p
[0]);
8614 return isl_map_free(map
);
8617 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
8621 struct isl_set
*isl_set_align_divs(struct isl_set
*set
)
8623 return set_from_map(isl_map_align_divs(set_to_map(set
)));
8626 /* Align the divs of the basic maps in "map" to those
8627 * of the basic maps in "list", as well as to the other basic maps in "map".
8628 * The elements in "list" are assumed to have known divs.
8630 __isl_give isl_map
*isl_map_align_divs_to_basic_map_list(
8631 __isl_take isl_map
*map
, __isl_keep isl_basic_map_list
*list
)
8635 map
= isl_map_compute_divs(map
);
8636 map
= isl_map_cow(map
);
8638 return isl_map_free(map
);
8642 n
= isl_basic_map_list_n_basic_map(list
);
8643 for (i
= 0; i
< n
; ++i
) {
8644 isl_basic_map
*bmap
;
8646 bmap
= isl_basic_map_list_get_basic_map(list
, i
);
8647 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], bmap
);
8648 isl_basic_map_free(bmap
);
8651 return isl_map_free(map
);
8653 return isl_map_align_divs(map
);
8656 /* Align the divs of each element of "list" to those of "bmap".
8657 * Both "bmap" and the elements of "list" are assumed to have known divs.
8659 __isl_give isl_basic_map_list
*isl_basic_map_list_align_divs_to_basic_map(
8660 __isl_take isl_basic_map_list
*list
, __isl_keep isl_basic_map
*bmap
)
8665 return isl_basic_map_list_free(list
);
8667 n
= isl_basic_map_list_n_basic_map(list
);
8668 for (i
= 0; i
< n
; ++i
) {
8669 isl_basic_map
*bmap_i
;
8671 bmap_i
= isl_basic_map_list_get_basic_map(list
, i
);
8672 bmap_i
= isl_basic_map_align_divs(bmap_i
, bmap
);
8673 list
= isl_basic_map_list_set_basic_map(list
, i
, bmap_i
);
8679 static __isl_give isl_set
*set_apply( __isl_take isl_set
*set
,
8680 __isl_take isl_map
*map
)
8684 isl_assert(set
->ctx
, isl_map_compatible_domain(map
, set
), goto error
);
8685 map
= isl_map_intersect_domain(map
, set
);
8686 set
= isl_map_range(map
);
8694 __isl_give isl_set
*isl_set_apply( __isl_take isl_set
*set
,
8695 __isl_take isl_map
*map
)
8697 return isl_map_align_params_map_map_and(set
, map
, &set_apply
);
8700 /* There is no need to cow as removing empty parts doesn't change
8701 * the meaning of the set.
8703 struct isl_map
*isl_map_remove_empty_parts(struct isl_map
*map
)
8710 for (i
= map
->n
- 1; i
>= 0; --i
)
8711 remove_if_empty(map
, i
);
8716 struct isl_set
*isl_set_remove_empty_parts(struct isl_set
*set
)
8718 return set_from_map(isl_map_remove_empty_parts(set_to_map(set
)));
8721 static __isl_give isl_basic_map
*map_copy_basic_map(__isl_keep isl_map
*map
)
8723 struct isl_basic_map
*bmap
;
8724 if (!map
|| map
->n
== 0)
8726 bmap
= map
->p
[map
->n
-1];
8727 isl_assert(map
->ctx
, ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
), return NULL
);
8728 return isl_basic_map_copy(bmap
);
8731 __isl_give isl_basic_map
*isl_map_copy_basic_map(__isl_keep isl_map
*map
)
8733 return map_copy_basic_map(map
);
8736 struct isl_basic_set
*isl_set_copy_basic_set(struct isl_set
*set
)
8738 return bset_from_bmap(map_copy_basic_map(set_to_map(set
)));
8741 static __isl_give isl_map
*map_drop_basic_map(__isl_take isl_map
*map
,
8742 __isl_keep isl_basic_map
*bmap
)
8748 for (i
= map
->n
-1; i
>= 0; --i
) {
8749 if (map
->p
[i
] != bmap
)
8751 map
= isl_map_cow(map
);
8754 isl_basic_map_free(map
->p
[i
]);
8755 if (i
!= map
->n
-1) {
8756 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
8757 map
->p
[i
] = map
->p
[map
->n
-1];
8768 __isl_give isl_map
*isl_map_drop_basic_map(__isl_take isl_map
*map
,
8769 __isl_keep isl_basic_map
*bmap
)
8771 return map_drop_basic_map(map
, bmap
);
8774 struct isl_set
*isl_set_drop_basic_set(struct isl_set
*set
,
8775 struct isl_basic_set
*bset
)
8777 return set_from_map(map_drop_basic_map(set_to_map(set
),
8778 bset_to_bmap(bset
)));
8781 /* Given two basic sets bset1 and bset2, compute the maximal difference
8782 * between the values of dimension pos in bset1 and those in bset2
8783 * for any common value of the parameters and dimensions preceding pos.
8785 static enum isl_lp_result
basic_set_maximal_difference_at(
8786 __isl_keep isl_basic_set
*bset1
, __isl_keep isl_basic_set
*bset2
,
8787 int pos
, isl_int
*opt
)
8789 isl_basic_map
*bmap1
;
8790 isl_basic_map
*bmap2
;
8791 struct isl_ctx
*ctx
;
8792 struct isl_vec
*obj
;
8796 enum isl_lp_result res
;
8798 if (!bset1
|| !bset2
)
8799 return isl_lp_error
;
8801 nparam
= isl_basic_set_n_param(bset1
);
8802 dim1
= isl_basic_set_n_dim(bset1
);
8804 bmap1
= isl_basic_map_from_range(isl_basic_set_copy(bset1
));
8805 bmap2
= isl_basic_map_from_range(isl_basic_set_copy(bset2
));
8806 bmap1
= isl_basic_map_move_dims(bmap1
, isl_dim_in
, 0,
8807 isl_dim_out
, 0, pos
);
8808 bmap2
= isl_basic_map_move_dims(bmap2
, isl_dim_in
, 0,
8809 isl_dim_out
, 0, pos
);
8810 bmap1
= isl_basic_map_range_product(bmap1
, bmap2
);
8812 return isl_lp_error
;
8814 total
= isl_basic_map_total_dim(bmap1
);
8816 obj
= isl_vec_alloc(ctx
, 1 + total
);
8819 isl_seq_clr(obj
->block
.data
, 1 + total
);
8820 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
], 1);
8821 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
+(dim1
-pos
)], -1);
8822 res
= isl_basic_map_solve_lp(bmap1
, 1, obj
->block
.data
, ctx
->one
,
8824 isl_basic_map_free(bmap1
);
8828 isl_basic_map_free(bmap1
);
8829 return isl_lp_error
;
8832 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
8833 * for any common value of the parameters and dimensions preceding pos
8834 * in both basic sets, the values of dimension pos in bset1 are
8835 * smaller or larger than those in bset2.
8838 * 1 if bset1 follows bset2
8839 * -1 if bset1 precedes bset2
8840 * 0 if bset1 and bset2 are incomparable
8841 * -2 if some error occurred.
8843 int isl_basic_set_compare_at(struct isl_basic_set
*bset1
,
8844 struct isl_basic_set
*bset2
, int pos
)
8847 enum isl_lp_result res
;
8852 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
8854 if (res
== isl_lp_empty
)
8856 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
8857 res
== isl_lp_unbounded
)
8859 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
8868 /* Given two basic sets bset1 and bset2, check whether
8869 * for any common value of the parameters and dimensions preceding pos
8870 * there is a value of dimension pos in bset1 that is larger
8871 * than a value of the same dimension in bset2.
8874 * 1 if there exists such a pair
8875 * 0 if there is no such pair, but there is a pair of equal values
8877 * -2 if some error occurred.
8879 int isl_basic_set_follows_at(__isl_keep isl_basic_set
*bset1
,
8880 __isl_keep isl_basic_set
*bset2
, int pos
)
8883 enum isl_lp_result res
;
8888 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
8890 if (res
== isl_lp_empty
)
8892 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
8893 res
== isl_lp_unbounded
)
8895 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
8897 else if (res
== isl_lp_ok
)
8906 /* Given two sets set1 and set2, check whether
8907 * for any common value of the parameters and dimensions preceding pos
8908 * there is a value of dimension pos in set1 that is larger
8909 * than a value of the same dimension in set2.
8912 * 1 if there exists such a pair
8913 * 0 if there is no such pair, but there is a pair of equal values
8915 * -2 if some error occurred.
8917 int isl_set_follows_at(__isl_keep isl_set
*set1
,
8918 __isl_keep isl_set
*set2
, int pos
)
8926 for (i
= 0; i
< set1
->n
; ++i
)
8927 for (j
= 0; j
< set2
->n
; ++j
) {
8929 f
= isl_basic_set_follows_at(set1
->p
[i
], set2
->p
[j
], pos
);
8930 if (f
== 1 || f
== -2)
8939 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map
*bmap
,
8940 unsigned pos
, isl_int
*val
)
8948 total
= isl_basic_map_total_dim(bmap
);
8949 for (i
= 0, d
= total
-1; i
< bmap
->n_eq
&& d
+1 > pos
; ++i
) {
8950 for (; d
+1 > pos
; --d
)
8951 if (!isl_int_is_zero(bmap
->eq
[i
][1+d
]))
8955 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1, d
) != -1)
8957 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1+d
+1, total
-d
-1) != -1)
8959 if (!isl_int_is_one(bmap
->eq
[i
][1+d
]))
8962 isl_int_neg(*val
, bmap
->eq
[i
][0]);
8968 static int isl_map_plain_has_fixed_var(__isl_keep isl_map
*map
,
8969 unsigned pos
, isl_int
*val
)
8981 return isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, val
);
8984 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, &v
);
8985 for (i
= 1; fixed
== 1 && i
< map
->n
; ++i
) {
8986 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[i
], pos
, &tmp
);
8987 if (fixed
== 1 && isl_int_ne(tmp
, v
))
8991 isl_int_set(*val
, v
);
8997 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set
*bset
,
8998 unsigned pos
, isl_int
*val
)
9000 return isl_basic_map_plain_has_fixed_var(bset_to_bmap(bset
),
9004 static int isl_set_plain_has_fixed_var(__isl_keep isl_set
*set
, unsigned pos
,
9007 return isl_map_plain_has_fixed_var(set_to_map(set
), pos
, val
);
9010 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map
*bmap
,
9011 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
9013 if (pos
>= isl_basic_map_dim(bmap
, type
))
9015 return isl_basic_map_plain_has_fixed_var(bmap
,
9016 isl_basic_map_offset(bmap
, type
) - 1 + pos
, val
);
9019 /* If "bmap" obviously lies on a hyperplane where the given dimension
9020 * has a fixed value, then return that value.
9021 * Otherwise return NaN.
9023 __isl_give isl_val
*isl_basic_map_plain_get_val_if_fixed(
9024 __isl_keep isl_basic_map
*bmap
,
9025 enum isl_dim_type type
, unsigned pos
)
9033 ctx
= isl_basic_map_get_ctx(bmap
);
9034 v
= isl_val_alloc(ctx
);
9037 fixed
= isl_basic_map_plain_is_fixed(bmap
, type
, pos
, &v
->n
);
9039 return isl_val_free(v
);
9041 isl_int_set_si(v
->d
, 1);
9045 return isl_val_nan(ctx
);
9048 int isl_map_plain_is_fixed(__isl_keep isl_map
*map
,
9049 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
9051 if (pos
>= isl_map_dim(map
, type
))
9053 return isl_map_plain_has_fixed_var(map
,
9054 map_offset(map
, type
) - 1 + pos
, val
);
9057 /* If "map" obviously lies on a hyperplane where the given dimension
9058 * has a fixed value, then return that value.
9059 * Otherwise return NaN.
9061 __isl_give isl_val
*isl_map_plain_get_val_if_fixed(__isl_keep isl_map
*map
,
9062 enum isl_dim_type type
, unsigned pos
)
9070 ctx
= isl_map_get_ctx(map
);
9071 v
= isl_val_alloc(ctx
);
9074 fixed
= isl_map_plain_is_fixed(map
, type
, pos
, &v
->n
);
9076 return isl_val_free(v
);
9078 isl_int_set_si(v
->d
, 1);
9082 return isl_val_nan(ctx
);
9085 /* If "set" obviously lies on a hyperplane where the given dimension
9086 * has a fixed value, then return that value.
9087 * Otherwise return NaN.
9089 __isl_give isl_val
*isl_set_plain_get_val_if_fixed(__isl_keep isl_set
*set
,
9090 enum isl_dim_type type
, unsigned pos
)
9092 return isl_map_plain_get_val_if_fixed(set
, type
, pos
);
9095 int isl_set_plain_is_fixed(__isl_keep isl_set
*set
,
9096 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
9098 return isl_map_plain_is_fixed(set
, type
, pos
, val
);
9101 /* Check if dimension dim has fixed value and if so and if val is not NULL,
9102 * then return this fixed value in *val.
9104 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set
*bset
,
9105 unsigned dim
, isl_int
*val
)
9107 return isl_basic_set_plain_has_fixed_var(bset
,
9108 isl_basic_set_n_param(bset
) + dim
, val
);
9111 /* Check if dimension dim has fixed value and if so and if val is not NULL,
9112 * then return this fixed value in *val.
9114 int isl_set_plain_dim_is_fixed(__isl_keep isl_set
*set
,
9115 unsigned dim
, isl_int
*val
)
9117 return isl_set_plain_has_fixed_var(set
, isl_set_n_param(set
) + dim
, val
);
9120 /* Check if input variable in has fixed value and if so and if val is not NULL,
9121 * then return this fixed value in *val.
9123 int isl_map_plain_input_is_fixed(__isl_keep isl_map
*map
,
9124 unsigned in
, isl_int
*val
)
9126 return isl_map_plain_has_fixed_var(map
, isl_map_n_param(map
) + in
, val
);
9129 /* Check if dimension dim has an (obvious) fixed lower bound and if so
9130 * and if val is not NULL, then return this lower bound in *val.
9132 int isl_basic_set_plain_dim_has_fixed_lower_bound(
9133 __isl_keep isl_basic_set
*bset
, unsigned dim
, isl_int
*val
)
9135 int i
, i_eq
= -1, i_ineq
= -1;
9142 total
= isl_basic_set_total_dim(bset
);
9143 nparam
= isl_basic_set_n_param(bset
);
9144 for (i
= 0; i
< bset
->n_eq
; ++i
) {
9145 if (isl_int_is_zero(bset
->eq
[i
][1+nparam
+dim
]))
9151 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
9152 if (!isl_int_is_pos(bset
->ineq
[i
][1+nparam
+dim
]))
9154 if (i_eq
!= -1 || i_ineq
!= -1)
9158 if (i_eq
== -1 && i_ineq
== -1)
9160 c
= i_eq
!= -1 ? bset
->eq
[i_eq
] : bset
->ineq
[i_ineq
];
9161 /* The coefficient should always be one due to normalization. */
9162 if (!isl_int_is_one(c
[1+nparam
+dim
]))
9164 if (isl_seq_first_non_zero(c
+1, nparam
+dim
) != -1)
9166 if (isl_seq_first_non_zero(c
+1+nparam
+dim
+1,
9167 total
- nparam
- dim
- 1) != -1)
9170 isl_int_neg(*val
, c
[0]);
9174 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set
*set
,
9175 unsigned dim
, isl_int
*val
)
9187 return isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[0],
9191 fixed
= isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[0],
9193 for (i
= 1; fixed
== 1 && i
< set
->n
; ++i
) {
9194 fixed
= isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[i
],
9196 if (fixed
== 1 && isl_int_ne(tmp
, v
))
9200 isl_int_set(*val
, v
);
9206 /* Return -1 if the constraint "c1" should be sorted before "c2"
9207 * and 1 if it should be sorted after "c2".
9208 * Return 0 if the two constraints are the same (up to the constant term).
9210 * In particular, if a constraint involves later variables than another
9211 * then it is sorted after this other constraint.
9212 * uset_gist depends on constraints without existentially quantified
9213 * variables sorting first.
9215 * For constraints that have the same latest variable, those
9216 * with the same coefficient for this latest variable (first in absolute value
9217 * and then in actual value) are grouped together.
9218 * This is useful for detecting pairs of constraints that can
9219 * be chained in their printed representation.
9221 * Finally, within a group, constraints are sorted according to
9222 * their coefficients (excluding the constant term).
9224 static int sort_constraint_cmp(const void *p1
, const void *p2
, void *arg
)
9226 isl_int
**c1
= (isl_int
**) p1
;
9227 isl_int
**c2
= (isl_int
**) p2
;
9229 unsigned size
= *(unsigned *) arg
;
9232 l1
= isl_seq_last_non_zero(*c1
+ 1, size
);
9233 l2
= isl_seq_last_non_zero(*c2
+ 1, size
);
9238 cmp
= isl_int_abs_cmp((*c1
)[1 + l1
], (*c2
)[1 + l1
]);
9241 cmp
= isl_int_cmp((*c1
)[1 + l1
], (*c2
)[1 + l1
]);
9245 return isl_seq_cmp(*c1
+ 1, *c2
+ 1, size
);
9248 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
9249 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
9250 * and 0 if the two constraints are the same (up to the constant term).
9252 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map
*bmap
,
9253 isl_int
*c1
, isl_int
*c2
)
9259 total
= isl_basic_map_total_dim(bmap
);
9260 return sort_constraint_cmp(&c1
, &c2
, &total
);
9263 __isl_give isl_basic_map
*isl_basic_map_sort_constraints(
9264 __isl_take isl_basic_map
*bmap
)
9270 if (bmap
->n_ineq
== 0)
9272 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED
))
9274 total
= isl_basic_map_total_dim(bmap
);
9275 if (isl_sort(bmap
->ineq
, bmap
->n_ineq
, sizeof(isl_int
*),
9276 &sort_constraint_cmp
, &total
) < 0)
9277 return isl_basic_map_free(bmap
);
9281 __isl_give isl_basic_set
*isl_basic_set_sort_constraints(
9282 __isl_take isl_basic_set
*bset
)
9284 isl_basic_map
*bmap
= bset_to_bmap(bset
);
9285 return bset_from_bmap(isl_basic_map_sort_constraints(bmap
));
9288 struct isl_basic_map
*isl_basic_map_normalize(struct isl_basic_map
*bmap
)
9292 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED
))
9294 bmap
= isl_basic_map_remove_redundancies(bmap
);
9295 bmap
= isl_basic_map_sort_constraints(bmap
);
9297 ISL_F_SET(bmap
, ISL_BASIC_MAP_NORMALIZED
);
9301 struct isl_basic_set
*isl_basic_set_normalize(struct isl_basic_set
*bset
)
9303 return bset_from_bmap(isl_basic_map_normalize(bset_to_bmap(bset
)));
9306 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map
*bmap1
,
9307 const __isl_keep isl_basic_map
*bmap2
)
9312 if (!bmap1
|| !bmap2
)
9317 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) !=
9318 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_RATIONAL
))
9319 return ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) ? -1 : 1;
9320 if (isl_basic_map_n_param(bmap1
) != isl_basic_map_n_param(bmap2
))
9321 return isl_basic_map_n_param(bmap1
) - isl_basic_map_n_param(bmap2
);
9322 if (isl_basic_map_n_in(bmap1
) != isl_basic_map_n_in(bmap2
))
9323 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
9324 if (isl_basic_map_n_out(bmap1
) != isl_basic_map_n_out(bmap2
))
9325 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
9326 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
) &&
9327 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
9329 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
))
9331 if (ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
9333 if (bmap1
->n_eq
!= bmap2
->n_eq
)
9334 return bmap1
->n_eq
- bmap2
->n_eq
;
9335 if (bmap1
->n_ineq
!= bmap2
->n_ineq
)
9336 return bmap1
->n_ineq
- bmap2
->n_ineq
;
9337 if (bmap1
->n_div
!= bmap2
->n_div
)
9338 return bmap1
->n_div
- bmap2
->n_div
;
9339 total
= isl_basic_map_total_dim(bmap1
);
9340 for (i
= 0; i
< bmap1
->n_eq
; ++i
) {
9341 cmp
= isl_seq_cmp(bmap1
->eq
[i
], bmap2
->eq
[i
], 1+total
);
9345 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
9346 cmp
= isl_seq_cmp(bmap1
->ineq
[i
], bmap2
->ineq
[i
], 1+total
);
9350 for (i
= 0; i
< bmap1
->n_div
; ++i
) {
9351 cmp
= isl_seq_cmp(bmap1
->div
[i
], bmap2
->div
[i
], 1+1+total
);
9358 int isl_basic_set_plain_cmp(const __isl_keep isl_basic_set
*bset1
,
9359 const __isl_keep isl_basic_set
*bset2
)
9361 return isl_basic_map_plain_cmp(bset1
, bset2
);
9364 int isl_set_plain_cmp(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
9370 if (set1
->n
!= set2
->n
)
9371 return set1
->n
- set2
->n
;
9373 for (i
= 0; i
< set1
->n
; ++i
) {
9374 cmp
= isl_basic_set_plain_cmp(set1
->p
[i
], set2
->p
[i
]);
9382 isl_bool
isl_basic_map_plain_is_equal(__isl_keep isl_basic_map
*bmap1
,
9383 __isl_keep isl_basic_map
*bmap2
)
9385 if (!bmap1
|| !bmap2
)
9386 return isl_bool_error
;
9387 return isl_basic_map_plain_cmp(bmap1
, bmap2
) == 0;
9390 isl_bool
isl_basic_set_plain_is_equal(__isl_keep isl_basic_set
*bset1
,
9391 __isl_keep isl_basic_set
*bset2
)
9393 return isl_basic_map_plain_is_equal(bset_to_bmap(bset1
),
9394 bset_to_bmap(bset2
));
9397 static int qsort_bmap_cmp(const void *p1
, const void *p2
)
9399 const struct isl_basic_map
*bmap1
= *(const struct isl_basic_map
**)p1
;
9400 const struct isl_basic_map
*bmap2
= *(const struct isl_basic_map
**)p2
;
9402 return isl_basic_map_plain_cmp(bmap1
, bmap2
);
9405 /* Sort the basic maps of "map" and remove duplicate basic maps.
9407 * While removing basic maps, we make sure that the basic maps remain
9408 * sorted because isl_map_normalize expects the basic maps of the result
9411 static __isl_give isl_map
*sort_and_remove_duplicates(__isl_take isl_map
*map
)
9415 map
= isl_map_remove_empty_parts(map
);
9418 qsort(map
->p
, map
->n
, sizeof(struct isl_basic_map
*), qsort_bmap_cmp
);
9419 for (i
= map
->n
- 1; i
>= 1; --i
) {
9420 if (!isl_basic_map_plain_is_equal(map
->p
[i
- 1], map
->p
[i
]))
9422 isl_basic_map_free(map
->p
[i
-1]);
9423 for (j
= i
; j
< map
->n
; ++j
)
9424 map
->p
[j
- 1] = map
->p
[j
];
9431 /* Remove obvious duplicates among the basic maps of "map".
9433 * Unlike isl_map_normalize, this function does not remove redundant
9434 * constraints and only removes duplicates that have exactly the same
9435 * constraints in the input. It does sort the constraints and
9436 * the basic maps to ease the detection of duplicates.
9438 * If "map" has already been normalized or if the basic maps are
9439 * disjoint, then there can be no duplicates.
9441 __isl_give isl_map
*isl_map_remove_obvious_duplicates(__isl_take isl_map
*map
)
9444 isl_basic_map
*bmap
;
9450 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
| ISL_MAP_DISJOINT
))
9452 for (i
= 0; i
< map
->n
; ++i
) {
9453 bmap
= isl_basic_map_copy(map
->p
[i
]);
9454 bmap
= isl_basic_map_sort_constraints(bmap
);
9456 return isl_map_free(map
);
9457 isl_basic_map_free(map
->p
[i
]);
9461 map
= sort_and_remove_duplicates(map
);
9465 /* We normalize in place, but if anything goes wrong we need
9466 * to return NULL, so we need to make sure we don't change the
9467 * meaning of any possible other copies of map.
9469 __isl_give isl_map
*isl_map_normalize(__isl_take isl_map
*map
)
9472 struct isl_basic_map
*bmap
;
9476 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
))
9478 for (i
= 0; i
< map
->n
; ++i
) {
9479 bmap
= isl_basic_map_normalize(isl_basic_map_copy(map
->p
[i
]));
9482 isl_basic_map_free(map
->p
[i
]);
9486 map
= sort_and_remove_duplicates(map
);
9488 ISL_F_SET(map
, ISL_MAP_NORMALIZED
);
9495 struct isl_set
*isl_set_normalize(struct isl_set
*set
)
9497 return set_from_map(isl_map_normalize(set_to_map(set
)));
9500 isl_bool
isl_map_plain_is_equal(__isl_keep isl_map
*map1
,
9501 __isl_keep isl_map
*map2
)
9507 return isl_bool_error
;
9510 return isl_bool_true
;
9511 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
9512 return isl_bool_false
;
9514 map1
= isl_map_copy(map1
);
9515 map2
= isl_map_copy(map2
);
9516 map1
= isl_map_normalize(map1
);
9517 map2
= isl_map_normalize(map2
);
9520 equal
= map1
->n
== map2
->n
;
9521 for (i
= 0; equal
&& i
< map1
->n
; ++i
) {
9522 equal
= isl_basic_map_plain_is_equal(map1
->p
[i
], map2
->p
[i
]);
9532 return isl_bool_error
;
9535 isl_bool
isl_set_plain_is_equal(__isl_keep isl_set
*set1
,
9536 __isl_keep isl_set
*set2
)
9538 return isl_map_plain_is_equal(set_to_map(set1
), set_to_map(set2
));
9541 /* Return an interval that ranges from min to max (inclusive)
9543 struct isl_basic_set
*isl_basic_set_interval(struct isl_ctx
*ctx
,
9544 isl_int min
, isl_int max
)
9547 struct isl_basic_set
*bset
= NULL
;
9549 bset
= isl_basic_set_alloc(ctx
, 0, 1, 0, 0, 2);
9553 k
= isl_basic_set_alloc_inequality(bset
);
9556 isl_int_set_si(bset
->ineq
[k
][1], 1);
9557 isl_int_neg(bset
->ineq
[k
][0], min
);
9559 k
= isl_basic_set_alloc_inequality(bset
);
9562 isl_int_set_si(bset
->ineq
[k
][1], -1);
9563 isl_int_set(bset
->ineq
[k
][0], max
);
9567 isl_basic_set_free(bset
);
9571 /* Return the basic maps in "map" as a list.
9573 __isl_give isl_basic_map_list
*isl_map_get_basic_map_list(
9574 __isl_keep isl_map
*map
)
9578 isl_basic_map_list
*list
;
9582 ctx
= isl_map_get_ctx(map
);
9583 list
= isl_basic_map_list_alloc(ctx
, map
->n
);
9585 for (i
= 0; i
< map
->n
; ++i
) {
9586 isl_basic_map
*bmap
;
9588 bmap
= isl_basic_map_copy(map
->p
[i
]);
9589 list
= isl_basic_map_list_add(list
, bmap
);
9595 /* Return the intersection of the elements in the non-empty list "list".
9596 * All elements are assumed to live in the same space.
9598 __isl_give isl_basic_map
*isl_basic_map_list_intersect(
9599 __isl_take isl_basic_map_list
*list
)
9602 isl_basic_map
*bmap
;
9606 n
= isl_basic_map_list_n_basic_map(list
);
9608 isl_die(isl_basic_map_list_get_ctx(list
), isl_error_invalid
,
9609 "expecting non-empty list", goto error
);
9611 bmap
= isl_basic_map_list_get_basic_map(list
, 0);
9612 for (i
= 1; i
< n
; ++i
) {
9613 isl_basic_map
*bmap_i
;
9615 bmap_i
= isl_basic_map_list_get_basic_map(list
, i
);
9616 bmap
= isl_basic_map_intersect(bmap
, bmap_i
);
9619 isl_basic_map_list_free(list
);
9622 isl_basic_map_list_free(list
);
9626 /* Return the intersection of the elements in the non-empty list "list".
9627 * All elements are assumed to live in the same space.
9629 __isl_give isl_basic_set
*isl_basic_set_list_intersect(
9630 __isl_take isl_basic_set_list
*list
)
9632 return isl_basic_map_list_intersect(list
);
9635 /* Return the union of the elements of "list".
9636 * The list is required to have at least one element.
9638 __isl_give isl_set
*isl_basic_set_list_union(
9639 __isl_take isl_basic_set_list
*list
)
9643 isl_basic_set
*bset
;
9648 n
= isl_basic_set_list_n_basic_set(list
);
9650 isl_die(isl_basic_set_list_get_ctx(list
), isl_error_invalid
,
9651 "expecting non-empty list", goto error
);
9653 bset
= isl_basic_set_list_get_basic_set(list
, 0);
9654 space
= isl_basic_set_get_space(bset
);
9655 isl_basic_set_free(bset
);
9657 set
= isl_set_alloc_space(space
, n
, 0);
9658 for (i
= 0; i
< n
; ++i
) {
9659 bset
= isl_basic_set_list_get_basic_set(list
, i
);
9660 set
= isl_set_add_basic_set(set
, bset
);
9663 isl_basic_set_list_free(list
);
9666 isl_basic_set_list_free(list
);
9670 /* Return the union of the elements in the non-empty list "list".
9671 * All elements are assumed to live in the same space.
9673 __isl_give isl_set
*isl_set_list_union(__isl_take isl_set_list
*list
)
9680 n
= isl_set_list_n_set(list
);
9682 isl_die(isl_set_list_get_ctx(list
), isl_error_invalid
,
9683 "expecting non-empty list", goto error
);
9685 set
= isl_set_list_get_set(list
, 0);
9686 for (i
= 1; i
< n
; ++i
) {
9689 set_i
= isl_set_list_get_set(list
, i
);
9690 set
= isl_set_union(set
, set_i
);
9693 isl_set_list_free(list
);
9696 isl_set_list_free(list
);
9700 /* Return the Cartesian product of the basic sets in list (in the given order).
9702 __isl_give isl_basic_set
*isl_basic_set_list_product(
9703 __isl_take
struct isl_basic_set_list
*list
)
9711 struct isl_basic_set
*product
= NULL
;
9715 isl_assert(list
->ctx
, list
->n
> 0, goto error
);
9716 isl_assert(list
->ctx
, list
->p
[0], goto error
);
9717 nparam
= isl_basic_set_n_param(list
->p
[0]);
9718 dim
= isl_basic_set_n_dim(list
->p
[0]);
9719 extra
= list
->p
[0]->n_div
;
9720 n_eq
= list
->p
[0]->n_eq
;
9721 n_ineq
= list
->p
[0]->n_ineq
;
9722 for (i
= 1; i
< list
->n
; ++i
) {
9723 isl_assert(list
->ctx
, list
->p
[i
], goto error
);
9724 isl_assert(list
->ctx
,
9725 nparam
== isl_basic_set_n_param(list
->p
[i
]), goto error
);
9726 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
9727 extra
+= list
->p
[i
]->n_div
;
9728 n_eq
+= list
->p
[i
]->n_eq
;
9729 n_ineq
+= list
->p
[i
]->n_ineq
;
9731 product
= isl_basic_set_alloc(list
->ctx
, nparam
, dim
, extra
,
9736 for (i
= 0; i
< list
->n
; ++i
) {
9737 isl_basic_set_add_constraints(product
,
9738 isl_basic_set_copy(list
->p
[i
]), dim
);
9739 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
9741 isl_basic_set_list_free(list
);
9744 isl_basic_set_free(product
);
9745 isl_basic_set_list_free(list
);
9749 struct isl_basic_map
*isl_basic_map_product(
9750 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
9752 isl_space
*dim_result
= NULL
;
9753 struct isl_basic_map
*bmap
;
9754 unsigned in1
, in2
, out1
, out2
, nparam
, total
, pos
;
9755 struct isl_dim_map
*dim_map1
, *dim_map2
;
9757 if (!bmap1
|| !bmap2
)
9760 isl_assert(bmap1
->ctx
, isl_space_match(bmap1
->dim
, isl_dim_param
,
9761 bmap2
->dim
, isl_dim_param
), goto error
);
9762 dim_result
= isl_space_product(isl_space_copy(bmap1
->dim
),
9763 isl_space_copy(bmap2
->dim
));
9765 in1
= isl_basic_map_n_in(bmap1
);
9766 in2
= isl_basic_map_n_in(bmap2
);
9767 out1
= isl_basic_map_n_out(bmap1
);
9768 out2
= isl_basic_map_n_out(bmap2
);
9769 nparam
= isl_basic_map_n_param(bmap1
);
9771 total
= nparam
+ in1
+ in2
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
9772 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9773 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9774 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9775 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9776 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9777 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
9778 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
9779 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
9780 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
9781 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9783 bmap
= isl_basic_map_alloc_space(dim_result
,
9784 bmap1
->n_div
+ bmap2
->n_div
,
9785 bmap1
->n_eq
+ bmap2
->n_eq
,
9786 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9787 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9788 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9789 bmap
= isl_basic_map_simplify(bmap
);
9790 return isl_basic_map_finalize(bmap
);
9792 isl_basic_map_free(bmap1
);
9793 isl_basic_map_free(bmap2
);
9797 __isl_give isl_basic_map
*isl_basic_map_flat_product(
9798 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9800 isl_basic_map
*prod
;
9802 prod
= isl_basic_map_product(bmap1
, bmap2
);
9803 prod
= isl_basic_map_flatten(prod
);
9807 __isl_give isl_basic_set
*isl_basic_set_flat_product(
9808 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
9810 return isl_basic_map_flat_range_product(bset1
, bset2
);
9813 __isl_give isl_basic_map
*isl_basic_map_domain_product(
9814 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9816 isl_space
*space_result
= NULL
;
9817 isl_basic_map
*bmap
;
9818 unsigned in1
, in2
, out
, nparam
, total
, pos
;
9819 struct isl_dim_map
*dim_map1
, *dim_map2
;
9821 if (!bmap1
|| !bmap2
)
9824 space_result
= isl_space_domain_product(isl_space_copy(bmap1
->dim
),
9825 isl_space_copy(bmap2
->dim
));
9827 in1
= isl_basic_map_dim(bmap1
, isl_dim_in
);
9828 in2
= isl_basic_map_dim(bmap2
, isl_dim_in
);
9829 out
= isl_basic_map_dim(bmap1
, isl_dim_out
);
9830 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
9832 total
= nparam
+ in1
+ in2
+ out
+ bmap1
->n_div
+ bmap2
->n_div
;
9833 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9834 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9835 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9836 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9837 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9838 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
9839 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
9840 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
);
9841 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out
);
9842 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9844 bmap
= isl_basic_map_alloc_space(space_result
,
9845 bmap1
->n_div
+ bmap2
->n_div
,
9846 bmap1
->n_eq
+ bmap2
->n_eq
,
9847 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9848 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9849 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9850 bmap
= isl_basic_map_simplify(bmap
);
9851 return isl_basic_map_finalize(bmap
);
9853 isl_basic_map_free(bmap1
);
9854 isl_basic_map_free(bmap2
);
9858 __isl_give isl_basic_map
*isl_basic_map_range_product(
9859 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9862 isl_space
*dim_result
= NULL
;
9863 isl_basic_map
*bmap
;
9864 unsigned in
, out1
, out2
, nparam
, total
, pos
;
9865 struct isl_dim_map
*dim_map1
, *dim_map2
;
9867 rational
= isl_basic_map_is_rational(bmap1
);
9868 if (rational
>= 0 && rational
)
9869 rational
= isl_basic_map_is_rational(bmap2
);
9870 if (!bmap1
|| !bmap2
|| rational
< 0)
9873 if (!isl_space_match(bmap1
->dim
, isl_dim_param
,
9874 bmap2
->dim
, isl_dim_param
))
9875 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
9876 "parameters don't match", goto error
);
9878 dim_result
= isl_space_range_product(isl_space_copy(bmap1
->dim
),
9879 isl_space_copy(bmap2
->dim
));
9881 in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
9882 out1
= isl_basic_map_n_out(bmap1
);
9883 out2
= isl_basic_map_n_out(bmap2
);
9884 nparam
= isl_basic_map_n_param(bmap1
);
9886 total
= nparam
+ in
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
9887 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9888 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9889 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9890 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9891 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9892 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
9893 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in
);
9894 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
9895 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
9896 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9898 bmap
= isl_basic_map_alloc_space(dim_result
,
9899 bmap1
->n_div
+ bmap2
->n_div
,
9900 bmap1
->n_eq
+ bmap2
->n_eq
,
9901 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9902 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9903 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9905 bmap
= isl_basic_map_set_rational(bmap
);
9906 bmap
= isl_basic_map_simplify(bmap
);
9907 return isl_basic_map_finalize(bmap
);
9909 isl_basic_map_free(bmap1
);
9910 isl_basic_map_free(bmap2
);
9914 __isl_give isl_basic_map
*isl_basic_map_flat_range_product(
9915 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9917 isl_basic_map
*prod
;
9919 prod
= isl_basic_map_range_product(bmap1
, bmap2
);
9920 prod
= isl_basic_map_flatten_range(prod
);
9924 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
9925 * and collect the results.
9926 * The result live in the space obtained by calling "space_product"
9927 * on the spaces of "map1" and "map2".
9928 * If "remove_duplicates" is set then the result may contain duplicates
9929 * (even if the inputs do not) and so we try and remove the obvious
9932 static __isl_give isl_map
*map_product(__isl_take isl_map
*map1
,
9933 __isl_take isl_map
*map2
,
9934 __isl_give isl_space
*(*space_product
)(__isl_take isl_space
*left
,
9935 __isl_take isl_space
*right
),
9936 __isl_give isl_basic_map
*(*basic_map_product
)(
9937 __isl_take isl_basic_map
*left
,
9938 __isl_take isl_basic_map
*right
),
9939 int remove_duplicates
)
9942 struct isl_map
*result
;
9948 isl_assert(map1
->ctx
, isl_space_match(map1
->dim
, isl_dim_param
,
9949 map2
->dim
, isl_dim_param
), goto error
);
9951 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
9952 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
9953 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
9955 result
= isl_map_alloc_space(space_product(isl_space_copy(map1
->dim
),
9956 isl_space_copy(map2
->dim
)),
9957 map1
->n
* map2
->n
, flags
);
9960 for (i
= 0; i
< map1
->n
; ++i
)
9961 for (j
= 0; j
< map2
->n
; ++j
) {
9962 struct isl_basic_map
*part
;
9963 part
= basic_map_product(isl_basic_map_copy(map1
->p
[i
]),
9964 isl_basic_map_copy(map2
->p
[j
]));
9965 if (isl_basic_map_is_empty(part
))
9966 isl_basic_map_free(part
);
9968 result
= isl_map_add_basic_map(result
, part
);
9972 if (remove_duplicates
)
9973 result
= isl_map_remove_obvious_duplicates(result
);
9983 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
9985 static __isl_give isl_map
*map_product_aligned(__isl_take isl_map
*map1
,
9986 __isl_take isl_map
*map2
)
9988 return map_product(map1
, map2
, &isl_space_product
,
9989 &isl_basic_map_product
, 0);
9992 __isl_give isl_map
*isl_map_product(__isl_take isl_map
*map1
,
9993 __isl_take isl_map
*map2
)
9995 return isl_map_align_params_map_map_and(map1
, map2
, &map_product_aligned
);
9998 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
10000 __isl_give isl_map
*isl_map_flat_product(__isl_take isl_map
*map1
,
10001 __isl_take isl_map
*map2
)
10005 prod
= isl_map_product(map1
, map2
);
10006 prod
= isl_map_flatten(prod
);
10010 /* Given two set A and B, construct its Cartesian product A x B.
10012 struct isl_set
*isl_set_product(struct isl_set
*set1
, struct isl_set
*set2
)
10014 return isl_map_range_product(set1
, set2
);
10017 __isl_give isl_set
*isl_set_flat_product(__isl_take isl_set
*set1
,
10018 __isl_take isl_set
*set2
)
10020 return isl_map_flat_range_product(set1
, set2
);
10023 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
10025 static __isl_give isl_map
*map_domain_product_aligned(__isl_take isl_map
*map1
,
10026 __isl_take isl_map
*map2
)
10028 return map_product(map1
, map2
, &isl_space_domain_product
,
10029 &isl_basic_map_domain_product
, 1);
10032 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
10034 static __isl_give isl_map
*map_range_product_aligned(__isl_take isl_map
*map1
,
10035 __isl_take isl_map
*map2
)
10037 return map_product(map1
, map2
, &isl_space_range_product
,
10038 &isl_basic_map_range_product
, 1);
10041 __isl_give isl_map
*isl_map_domain_product(__isl_take isl_map
*map1
,
10042 __isl_take isl_map
*map2
)
10044 return isl_map_align_params_map_map_and(map1
, map2
,
10045 &map_domain_product_aligned
);
10048 __isl_give isl_map
*isl_map_range_product(__isl_take isl_map
*map1
,
10049 __isl_take isl_map
*map2
)
10051 return isl_map_align_params_map_map_and(map1
, map2
,
10052 &map_range_product_aligned
);
10055 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
10057 __isl_give isl_map
*isl_map_factor_domain(__isl_take isl_map
*map
)
10060 int total1
, keep1
, total2
, keep2
;
10064 if (!isl_space_domain_is_wrapping(map
->dim
) ||
10065 !isl_space_range_is_wrapping(map
->dim
))
10066 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10067 "not a product", return isl_map_free(map
));
10069 space
= isl_map_get_space(map
);
10070 total1
= isl_space_dim(space
, isl_dim_in
);
10071 total2
= isl_space_dim(space
, isl_dim_out
);
10072 space
= isl_space_factor_domain(space
);
10073 keep1
= isl_space_dim(space
, isl_dim_in
);
10074 keep2
= isl_space_dim(space
, isl_dim_out
);
10075 map
= isl_map_project_out(map
, isl_dim_in
, keep1
, total1
- keep1
);
10076 map
= isl_map_project_out(map
, isl_dim_out
, keep2
, total2
- keep2
);
10077 map
= isl_map_reset_space(map
, space
);
10082 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
10084 __isl_give isl_map
*isl_map_factor_range(__isl_take isl_map
*map
)
10087 int total1
, keep1
, total2
, keep2
;
10091 if (!isl_space_domain_is_wrapping(map
->dim
) ||
10092 !isl_space_range_is_wrapping(map
->dim
))
10093 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10094 "not a product", return isl_map_free(map
));
10096 space
= isl_map_get_space(map
);
10097 total1
= isl_space_dim(space
, isl_dim_in
);
10098 total2
= isl_space_dim(space
, isl_dim_out
);
10099 space
= isl_space_factor_range(space
);
10100 keep1
= isl_space_dim(space
, isl_dim_in
);
10101 keep2
= isl_space_dim(space
, isl_dim_out
);
10102 map
= isl_map_project_out(map
, isl_dim_in
, 0, total1
- keep1
);
10103 map
= isl_map_project_out(map
, isl_dim_out
, 0, total2
- keep2
);
10104 map
= isl_map_reset_space(map
, space
);
10109 /* Given a map of the form [A -> B] -> C, return the map A -> C.
10111 __isl_give isl_map
*isl_map_domain_factor_domain(__isl_take isl_map
*map
)
10118 if (!isl_space_domain_is_wrapping(map
->dim
))
10119 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10120 "domain is not a product", return isl_map_free(map
));
10122 space
= isl_map_get_space(map
);
10123 total
= isl_space_dim(space
, isl_dim_in
);
10124 space
= isl_space_domain_factor_domain(space
);
10125 keep
= isl_space_dim(space
, isl_dim_in
);
10126 map
= isl_map_project_out(map
, isl_dim_in
, keep
, total
- keep
);
10127 map
= isl_map_reset_space(map
, space
);
10132 /* Given a map of the form [A -> B] -> C, return the map B -> C.
10134 __isl_give isl_map
*isl_map_domain_factor_range(__isl_take isl_map
*map
)
10141 if (!isl_space_domain_is_wrapping(map
->dim
))
10142 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10143 "domain is not a product", return isl_map_free(map
));
10145 space
= isl_map_get_space(map
);
10146 total
= isl_space_dim(space
, isl_dim_in
);
10147 space
= isl_space_domain_factor_range(space
);
10148 keep
= isl_space_dim(space
, isl_dim_in
);
10149 map
= isl_map_project_out(map
, isl_dim_in
, 0, total
- keep
);
10150 map
= isl_map_reset_space(map
, space
);
10155 /* Given a map A -> [B -> C], extract the map A -> B.
10157 __isl_give isl_map
*isl_map_range_factor_domain(__isl_take isl_map
*map
)
10164 if (!isl_space_range_is_wrapping(map
->dim
))
10165 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10166 "range is not a product", return isl_map_free(map
));
10168 space
= isl_map_get_space(map
);
10169 total
= isl_space_dim(space
, isl_dim_out
);
10170 space
= isl_space_range_factor_domain(space
);
10171 keep
= isl_space_dim(space
, isl_dim_out
);
10172 map
= isl_map_project_out(map
, isl_dim_out
, keep
, total
- keep
);
10173 map
= isl_map_reset_space(map
, space
);
10178 /* Given a map A -> [B -> C], extract the map A -> C.
10180 __isl_give isl_map
*isl_map_range_factor_range(__isl_take isl_map
*map
)
10187 if (!isl_space_range_is_wrapping(map
->dim
))
10188 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10189 "range is not a product", return isl_map_free(map
));
10191 space
= isl_map_get_space(map
);
10192 total
= isl_space_dim(space
, isl_dim_out
);
10193 space
= isl_space_range_factor_range(space
);
10194 keep
= isl_space_dim(space
, isl_dim_out
);
10195 map
= isl_map_project_out(map
, isl_dim_out
, 0, total
- keep
);
10196 map
= isl_map_reset_space(map
, space
);
10201 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
10203 __isl_give isl_map
*isl_map_flat_domain_product(__isl_take isl_map
*map1
,
10204 __isl_take isl_map
*map2
)
10208 prod
= isl_map_domain_product(map1
, map2
);
10209 prod
= isl_map_flatten_domain(prod
);
10213 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
10215 __isl_give isl_map
*isl_map_flat_range_product(__isl_take isl_map
*map1
,
10216 __isl_take isl_map
*map2
)
10220 prod
= isl_map_range_product(map1
, map2
);
10221 prod
= isl_map_flatten_range(prod
);
10225 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map
*bmap
)
10228 uint32_t hash
= isl_hash_init();
10233 bmap
= isl_basic_map_copy(bmap
);
10234 bmap
= isl_basic_map_normalize(bmap
);
10237 total
= isl_basic_map_total_dim(bmap
);
10238 isl_hash_byte(hash
, bmap
->n_eq
& 0xFF);
10239 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
10241 c_hash
= isl_seq_get_hash(bmap
->eq
[i
], 1 + total
);
10242 isl_hash_hash(hash
, c_hash
);
10244 isl_hash_byte(hash
, bmap
->n_ineq
& 0xFF);
10245 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
10247 c_hash
= isl_seq_get_hash(bmap
->ineq
[i
], 1 + total
);
10248 isl_hash_hash(hash
, c_hash
);
10250 isl_hash_byte(hash
, bmap
->n_div
& 0xFF);
10251 for (i
= 0; i
< bmap
->n_div
; ++i
) {
10253 if (isl_int_is_zero(bmap
->div
[i
][0]))
10255 isl_hash_byte(hash
, i
& 0xFF);
10256 c_hash
= isl_seq_get_hash(bmap
->div
[i
], 1 + 1 + total
);
10257 isl_hash_hash(hash
, c_hash
);
10259 isl_basic_map_free(bmap
);
10263 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set
*bset
)
10265 return isl_basic_map_get_hash(bset_to_bmap(bset
));
10268 uint32_t isl_map_get_hash(__isl_keep isl_map
*map
)
10275 map
= isl_map_copy(map
);
10276 map
= isl_map_normalize(map
);
10280 hash
= isl_hash_init();
10281 for (i
= 0; i
< map
->n
; ++i
) {
10282 uint32_t bmap_hash
;
10283 bmap_hash
= isl_basic_map_get_hash(map
->p
[i
]);
10284 isl_hash_hash(hash
, bmap_hash
);
10292 uint32_t isl_set_get_hash(__isl_keep isl_set
*set
)
10294 return isl_map_get_hash(set_to_map(set
));
10297 /* Check if the value for dimension dim is completely determined
10298 * by the values of the other parameters and variables.
10299 * That is, check if dimension dim is involved in an equality.
10301 int isl_basic_set_dim_is_unique(struct isl_basic_set
*bset
, unsigned dim
)
10308 nparam
= isl_basic_set_n_param(bset
);
10309 for (i
= 0; i
< bset
->n_eq
; ++i
)
10310 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ dim
]))
10315 /* Check if the value for dimension dim is completely determined
10316 * by the values of the other parameters and variables.
10317 * That is, check if dimension dim is involved in an equality
10318 * for each of the subsets.
10320 int isl_set_dim_is_unique(struct isl_set
*set
, unsigned dim
)
10326 for (i
= 0; i
< set
->n
; ++i
) {
10328 unique
= isl_basic_set_dim_is_unique(set
->p
[i
], dim
);
10335 /* Return the number of basic maps in the (current) representation of "map".
10337 int isl_map_n_basic_map(__isl_keep isl_map
*map
)
10339 return map
? map
->n
: 0;
10342 int isl_set_n_basic_set(__isl_keep isl_set
*set
)
10344 return set
? set
->n
: 0;
10347 isl_stat
isl_map_foreach_basic_map(__isl_keep isl_map
*map
,
10348 isl_stat (*fn
)(__isl_take isl_basic_map
*bmap
, void *user
), void *user
)
10353 return isl_stat_error
;
10355 for (i
= 0; i
< map
->n
; ++i
)
10356 if (fn(isl_basic_map_copy(map
->p
[i
]), user
) < 0)
10357 return isl_stat_error
;
10359 return isl_stat_ok
;
10362 isl_stat
isl_set_foreach_basic_set(__isl_keep isl_set
*set
,
10363 isl_stat (*fn
)(__isl_take isl_basic_set
*bset
, void *user
), void *user
)
10368 return isl_stat_error
;
10370 for (i
= 0; i
< set
->n
; ++i
)
10371 if (fn(isl_basic_set_copy(set
->p
[i
]), user
) < 0)
10372 return isl_stat_error
;
10374 return isl_stat_ok
;
10377 /* Return a list of basic sets, the union of which is equal to "set".
10379 __isl_give isl_basic_set_list
*isl_set_get_basic_set_list(
10380 __isl_keep isl_set
*set
)
10383 isl_basic_set_list
*list
;
10388 list
= isl_basic_set_list_alloc(isl_set_get_ctx(set
), set
->n
);
10389 for (i
= 0; i
< set
->n
; ++i
) {
10390 isl_basic_set
*bset
;
10392 bset
= isl_basic_set_copy(set
->p
[i
]);
10393 list
= isl_basic_set_list_add(list
, bset
);
10399 __isl_give isl_basic_set
*isl_basic_set_lift(__isl_take isl_basic_set
*bset
)
10406 bset
= isl_basic_set_cow(bset
);
10410 dim
= isl_basic_set_get_space(bset
);
10411 dim
= isl_space_lift(dim
, bset
->n_div
);
10414 isl_space_free(bset
->dim
);
10416 bset
->extra
-= bset
->n_div
;
10419 bset
= isl_basic_set_finalize(bset
);
10423 isl_basic_set_free(bset
);
10427 __isl_give isl_set
*isl_set_lift(__isl_take isl_set
*set
)
10433 set
= isl_set_align_divs(set
);
10438 set
= isl_set_cow(set
);
10442 n_div
= set
->p
[0]->n_div
;
10443 dim
= isl_set_get_space(set
);
10444 dim
= isl_space_lift(dim
, n_div
);
10447 isl_space_free(set
->dim
);
10450 for (i
= 0; i
< set
->n
; ++i
) {
10451 set
->p
[i
] = isl_basic_set_lift(set
->p
[i
]);
10462 __isl_give isl_map
*isl_set_lifting(__isl_take isl_set
*set
)
10465 struct isl_basic_map
*bmap
;
10472 set
= isl_set_align_divs(set
);
10477 dim
= isl_set_get_space(set
);
10478 if (set
->n
== 0 || set
->p
[0]->n_div
== 0) {
10480 return isl_map_identity(isl_space_map_from_set(dim
));
10483 n_div
= set
->p
[0]->n_div
;
10484 dim
= isl_space_map_from_set(dim
);
10485 n_param
= isl_space_dim(dim
, isl_dim_param
);
10486 n_set
= isl_space_dim(dim
, isl_dim_in
);
10487 dim
= isl_space_extend(dim
, n_param
, n_set
, n_set
+ n_div
);
10488 bmap
= isl_basic_map_alloc_space(dim
, 0, n_set
, 2 * n_div
);
10489 for (i
= 0; i
< n_set
; ++i
)
10490 bmap
= var_equal(bmap
, i
);
10492 total
= n_param
+ n_set
+ n_set
+ n_div
;
10493 for (i
= 0; i
< n_div
; ++i
) {
10494 k
= isl_basic_map_alloc_inequality(bmap
);
10497 isl_seq_cpy(bmap
->ineq
[k
], set
->p
[0]->div
[i
]+1, 1+n_param
);
10498 isl_seq_clr(bmap
->ineq
[k
]+1+n_param
, n_set
);
10499 isl_seq_cpy(bmap
->ineq
[k
]+1+n_param
+n_set
,
10500 set
->p
[0]->div
[i
]+1+1+n_param
, n_set
+ n_div
);
10501 isl_int_neg(bmap
->ineq
[k
][1+n_param
+n_set
+n_set
+i
],
10502 set
->p
[0]->div
[i
][0]);
10504 l
= isl_basic_map_alloc_inequality(bmap
);
10507 isl_seq_neg(bmap
->ineq
[l
], bmap
->ineq
[k
], 1 + total
);
10508 isl_int_add(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0],
10509 set
->p
[0]->div
[i
][0]);
10510 isl_int_sub_ui(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], 1);
10514 bmap
= isl_basic_map_simplify(bmap
);
10515 bmap
= isl_basic_map_finalize(bmap
);
10516 return isl_map_from_basic_map(bmap
);
10519 isl_basic_map_free(bmap
);
10523 int isl_basic_set_size(__isl_keep isl_basic_set
*bset
)
10531 dim
= isl_basic_set_total_dim(bset
);
10532 size
+= bset
->n_eq
* (1 + dim
);
10533 size
+= bset
->n_ineq
* (1 + dim
);
10534 size
+= bset
->n_div
* (2 + dim
);
10539 int isl_set_size(__isl_keep isl_set
*set
)
10547 for (i
= 0; i
< set
->n
; ++i
)
10548 size
+= isl_basic_set_size(set
->p
[i
]);
10553 /* Check if there is any lower bound (if lower == 0) and/or upper
10554 * bound (if upper == 0) on the specified dim.
10556 static isl_bool
basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
10557 enum isl_dim_type type
, unsigned pos
, int lower
, int upper
)
10561 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
10562 return isl_bool_error
;
10564 pos
+= isl_basic_map_offset(bmap
, type
);
10566 for (i
= 0; i
< bmap
->n_div
; ++i
) {
10567 if (isl_int_is_zero(bmap
->div
[i
][0]))
10569 if (!isl_int_is_zero(bmap
->div
[i
][1 + pos
]))
10570 return isl_bool_true
;
10573 for (i
= 0; i
< bmap
->n_eq
; ++i
)
10574 if (!isl_int_is_zero(bmap
->eq
[i
][pos
]))
10575 return isl_bool_true
;
10577 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
10578 int sgn
= isl_int_sgn(bmap
->ineq
[i
][pos
]);
10585 return lower
&& upper
;
10588 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
10589 enum isl_dim_type type
, unsigned pos
)
10591 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 0);
10594 isl_bool
isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map
*bmap
,
10595 enum isl_dim_type type
, unsigned pos
)
10597 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 1);
10600 isl_bool
isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map
*bmap
,
10601 enum isl_dim_type type
, unsigned pos
)
10603 return basic_map_dim_is_bounded(bmap
, type
, pos
, 1, 0);
10606 int isl_map_dim_is_bounded(__isl_keep isl_map
*map
,
10607 enum isl_dim_type type
, unsigned pos
)
10614 for (i
= 0; i
< map
->n
; ++i
) {
10616 bounded
= isl_basic_map_dim_is_bounded(map
->p
[i
], type
, pos
);
10617 if (bounded
< 0 || !bounded
)
10624 /* Return 1 if the specified dim is involved in both an upper bound
10625 * and a lower bound.
10627 int isl_set_dim_is_bounded(__isl_keep isl_set
*set
,
10628 enum isl_dim_type type
, unsigned pos
)
10630 return isl_map_dim_is_bounded(set_to_map(set
), type
, pos
);
10633 /* Does "map" have a bound (according to "fn") for any of its basic maps?
10635 static isl_bool
has_any_bound(__isl_keep isl_map
*map
,
10636 enum isl_dim_type type
, unsigned pos
,
10637 isl_bool (*fn
)(__isl_keep isl_basic_map
*bmap
,
10638 enum isl_dim_type type
, unsigned pos
))
10643 return isl_bool_error
;
10645 for (i
= 0; i
< map
->n
; ++i
) {
10647 bounded
= fn(map
->p
[i
], type
, pos
);
10648 if (bounded
< 0 || bounded
)
10652 return isl_bool_false
;
10655 /* Return 1 if the specified dim is involved in any lower bound.
10657 isl_bool
isl_set_dim_has_any_lower_bound(__isl_keep isl_set
*set
,
10658 enum isl_dim_type type
, unsigned pos
)
10660 return has_any_bound(set
, type
, pos
,
10661 &isl_basic_map_dim_has_lower_bound
);
10664 /* Return 1 if the specified dim is involved in any upper bound.
10666 isl_bool
isl_set_dim_has_any_upper_bound(__isl_keep isl_set
*set
,
10667 enum isl_dim_type type
, unsigned pos
)
10669 return has_any_bound(set
, type
, pos
,
10670 &isl_basic_map_dim_has_upper_bound
);
10673 /* Does "map" have a bound (according to "fn") for all of its basic maps?
10675 static isl_bool
has_bound(__isl_keep isl_map
*map
,
10676 enum isl_dim_type type
, unsigned pos
,
10677 isl_bool (*fn
)(__isl_keep isl_basic_map
*bmap
,
10678 enum isl_dim_type type
, unsigned pos
))
10683 return isl_bool_error
;
10685 for (i
= 0; i
< map
->n
; ++i
) {
10687 bounded
= fn(map
->p
[i
], type
, pos
);
10688 if (bounded
< 0 || !bounded
)
10692 return isl_bool_true
;
10695 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10697 isl_bool
isl_set_dim_has_lower_bound(__isl_keep isl_set
*set
,
10698 enum isl_dim_type type
, unsigned pos
)
10700 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_lower_bound
);
10703 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10705 isl_bool
isl_set_dim_has_upper_bound(__isl_keep isl_set
*set
,
10706 enum isl_dim_type type
, unsigned pos
)
10708 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_upper_bound
);
10711 /* For each of the "n" variables starting at "first", determine
10712 * the sign of the variable and put the results in the first "n"
10713 * elements of the array "signs".
10715 * 1 means that the variable is non-negative
10716 * -1 means that the variable is non-positive
10717 * 0 means the variable attains both positive and negative values.
10719 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set
*bset
,
10720 unsigned first
, unsigned n
, int *signs
)
10722 isl_vec
*bound
= NULL
;
10723 struct isl_tab
*tab
= NULL
;
10724 struct isl_tab_undo
*snap
;
10727 if (!bset
|| !signs
)
10730 bound
= isl_vec_alloc(bset
->ctx
, 1 + isl_basic_set_total_dim(bset
));
10731 tab
= isl_tab_from_basic_set(bset
, 0);
10732 if (!bound
|| !tab
)
10735 isl_seq_clr(bound
->el
, bound
->size
);
10736 isl_int_set_si(bound
->el
[0], -1);
10738 snap
= isl_tab_snap(tab
);
10739 for (i
= 0; i
< n
; ++i
) {
10742 isl_int_set_si(bound
->el
[1 + first
+ i
], -1);
10743 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
10745 empty
= tab
->empty
;
10746 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
10747 if (isl_tab_rollback(tab
, snap
) < 0)
10755 isl_int_set_si(bound
->el
[1 + first
+ i
], 1);
10756 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
10758 empty
= tab
->empty
;
10759 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
10760 if (isl_tab_rollback(tab
, snap
) < 0)
10763 signs
[i
] = empty
? -1 : 0;
10767 isl_vec_free(bound
);
10771 isl_vec_free(bound
);
10775 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set
*bset
,
10776 enum isl_dim_type type
, unsigned first
, unsigned n
, int *signs
)
10778 if (!bset
|| !signs
)
10780 isl_assert(bset
->ctx
, first
+ n
<= isl_basic_set_dim(bset
, type
),
10783 first
+= pos(bset
->dim
, type
) - 1;
10784 return isl_basic_set_vars_get_sign(bset
, first
, n
, signs
);
10787 /* Is it possible for the integer division "div" to depend (possibly
10788 * indirectly) on any output dimensions?
10790 * If the div is undefined, then we conservatively assume that it
10791 * may depend on them.
10792 * Otherwise, we check if it actually depends on them or on any integer
10793 * divisions that may depend on them.
10795 static int div_may_involve_output(__isl_keep isl_basic_map
*bmap
, int div
)
10798 unsigned n_out
, o_out
;
10799 unsigned n_div
, o_div
;
10801 if (isl_int_is_zero(bmap
->div
[div
][0]))
10804 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10805 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10807 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + o_out
, n_out
) != -1)
10810 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10811 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10813 for (i
= 0; i
< n_div
; ++i
) {
10814 if (isl_int_is_zero(bmap
->div
[div
][1 + o_div
+ i
]))
10816 if (div_may_involve_output(bmap
, i
))
10823 /* Return the first integer division of "bmap" in the range
10824 * [first, first + n[ that may depend on any output dimensions and
10825 * that has a non-zero coefficient in "c" (where the first coefficient
10826 * in "c" corresponds to integer division "first").
10828 static int first_div_may_involve_output(__isl_keep isl_basic_map
*bmap
,
10829 isl_int
*c
, int first
, int n
)
10836 for (k
= first
; k
< first
+ n
; ++k
) {
10837 if (isl_int_is_zero(c
[k
]))
10839 if (div_may_involve_output(bmap
, k
))
10846 /* Look for a pair of inequality constraints in "bmap" of the form
10848 * -l + i >= 0 or i >= l
10850 * n + l - i >= 0 or i <= l + n
10852 * with n < "m" and i the output dimension at position "pos".
10853 * (Note that n >= 0 as otherwise the two constraints would conflict.)
10854 * Furthermore, "l" is only allowed to involve parameters, input dimensions
10855 * and earlier output dimensions, as well as integer divisions that do
10856 * not involve any of the output dimensions.
10858 * Return the index of the first inequality constraint or bmap->n_ineq
10859 * if no such pair can be found.
10861 static int find_modulo_constraint_pair(__isl_keep isl_basic_map
*bmap
,
10862 int pos
, isl_int m
)
10867 unsigned n_div
, o_div
;
10868 unsigned n_out
, o_out
;
10874 ctx
= isl_basic_map_get_ctx(bmap
);
10875 total
= isl_basic_map_total_dim(bmap
);
10876 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10877 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10878 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10879 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10880 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
10881 if (!isl_int_abs_eq(bmap
->ineq
[i
][o_out
+ pos
], ctx
->one
))
10883 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + o_out
+ pos
+ 1,
10884 n_out
- (pos
+ 1)) != -1)
10886 if (first_div_may_involve_output(bmap
, bmap
->ineq
[i
] + o_div
,
10889 for (j
= i
+ 1; j
< bmap
->n_ineq
; ++j
) {
10890 if (!isl_int_abs_eq(bmap
->ineq
[j
][o_out
+ pos
],
10893 if (!isl_seq_is_neg(bmap
->ineq
[i
] + 1,
10894 bmap
->ineq
[j
] + 1, total
))
10898 if (j
>= bmap
->n_ineq
)
10900 isl_int_add(bmap
->ineq
[i
][0],
10901 bmap
->ineq
[i
][0], bmap
->ineq
[j
][0]);
10902 less
= isl_int_abs_lt(bmap
->ineq
[i
][0], m
);
10903 isl_int_sub(bmap
->ineq
[i
][0],
10904 bmap
->ineq
[i
][0], bmap
->ineq
[j
][0]);
10907 if (isl_int_is_one(bmap
->ineq
[i
][o_out
+ pos
]))
10913 return bmap
->n_ineq
;
10916 /* Return the index of the equality of "bmap" that defines
10917 * the output dimension "pos" in terms of earlier dimensions.
10918 * The equality may also involve integer divisions, as long
10919 * as those integer divisions are defined in terms of
10920 * parameters or input dimensions.
10921 * In this case, *div is set to the number of integer divisions and
10922 * *ineq is set to the number of inequality constraints (provided
10923 * div and ineq are not NULL).
10925 * The equality may also involve a single integer division involving
10926 * the output dimensions (typically only output dimension "pos") as
10927 * long as the coefficient of output dimension "pos" is 1 or -1 and
10928 * there is a pair of constraints i >= l and i <= l + n, with i referring
10929 * to output dimension "pos", l an expression involving only earlier
10930 * dimensions and n smaller than the coefficient of the integer division
10931 * in the equality. In this case, the output dimension can be defined
10932 * in terms of a modulo expression that does not involve the integer division.
10933 * *div is then set to this single integer division and
10934 * *ineq is set to the index of constraint i >= l.
10936 * Return bmap->n_eq if there is no such equality.
10937 * Return -1 on error.
10939 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map
*bmap
,
10940 int pos
, int *div
, int *ineq
)
10943 unsigned n_out
, o_out
;
10944 unsigned n_div
, o_div
;
10949 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10950 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10951 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10952 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10955 *ineq
= bmap
->n_ineq
;
10958 for (j
= 0; j
< bmap
->n_eq
; ++j
) {
10959 if (isl_int_is_zero(bmap
->eq
[j
][o_out
+ pos
]))
10961 if (isl_seq_first_non_zero(bmap
->eq
[j
] + o_out
+ pos
+ 1,
10962 n_out
- (pos
+ 1)) != -1)
10964 k
= first_div_may_involve_output(bmap
, bmap
->eq
[j
] + o_div
,
10968 if (!isl_int_is_one(bmap
->eq
[j
][o_out
+ pos
]) &&
10969 !isl_int_is_negone(bmap
->eq
[j
][o_out
+ pos
]))
10971 if (first_div_may_involve_output(bmap
, bmap
->eq
[j
] + o_div
,
10972 k
+ 1, n_div
- (k
+1)) < n_div
)
10974 l
= find_modulo_constraint_pair(bmap
, pos
,
10975 bmap
->eq
[j
][o_div
+ k
]);
10978 if (l
>= bmap
->n_ineq
)
10990 /* Check if the given basic map is obviously single-valued.
10991 * In particular, for each output dimension, check that there is
10992 * an equality that defines the output dimension in terms of
10993 * earlier dimensions.
10995 isl_bool
isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map
*bmap
)
11001 return isl_bool_error
;
11003 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
11005 for (i
= 0; i
< n_out
; ++i
) {
11008 eq
= isl_basic_map_output_defining_equality(bmap
, i
,
11011 return isl_bool_error
;
11012 if (eq
>= bmap
->n_eq
)
11013 return isl_bool_false
;
11016 return isl_bool_true
;
11019 /* Check if the given basic map is single-valued.
11020 * We simply compute
11024 * and check if the result is a subset of the identity mapping.
11026 isl_bool
isl_basic_map_is_single_valued(__isl_keep isl_basic_map
*bmap
)
11029 isl_basic_map
*test
;
11033 sv
= isl_basic_map_plain_is_single_valued(bmap
);
11037 test
= isl_basic_map_reverse(isl_basic_map_copy(bmap
));
11038 test
= isl_basic_map_apply_range(test
, isl_basic_map_copy(bmap
));
11040 space
= isl_basic_map_get_space(bmap
);
11041 space
= isl_space_map_from_set(isl_space_range(space
));
11042 id
= isl_basic_map_identity(space
);
11044 sv
= isl_basic_map_is_subset(test
, id
);
11046 isl_basic_map_free(test
);
11047 isl_basic_map_free(id
);
11052 /* Check if the given map is obviously single-valued.
11054 isl_bool
isl_map_plain_is_single_valued(__isl_keep isl_map
*map
)
11057 return isl_bool_error
;
11059 return isl_bool_true
;
11061 return isl_bool_false
;
11063 return isl_basic_map_plain_is_single_valued(map
->p
[0]);
11066 /* Check if the given map is single-valued.
11067 * We simply compute
11071 * and check if the result is a subset of the identity mapping.
11073 isl_bool
isl_map_is_single_valued(__isl_keep isl_map
*map
)
11080 sv
= isl_map_plain_is_single_valued(map
);
11084 test
= isl_map_reverse(isl_map_copy(map
));
11085 test
= isl_map_apply_range(test
, isl_map_copy(map
));
11087 dim
= isl_space_map_from_set(isl_space_range(isl_map_get_space(map
)));
11088 id
= isl_map_identity(dim
);
11090 sv
= isl_map_is_subset(test
, id
);
11092 isl_map_free(test
);
11098 isl_bool
isl_map_is_injective(__isl_keep isl_map
*map
)
11102 map
= isl_map_copy(map
);
11103 map
= isl_map_reverse(map
);
11104 in
= isl_map_is_single_valued(map
);
11110 /* Check if the given map is obviously injective.
11112 isl_bool
isl_map_plain_is_injective(__isl_keep isl_map
*map
)
11116 map
= isl_map_copy(map
);
11117 map
= isl_map_reverse(map
);
11118 in
= isl_map_plain_is_single_valued(map
);
11124 isl_bool
isl_map_is_bijective(__isl_keep isl_map
*map
)
11128 sv
= isl_map_is_single_valued(map
);
11132 return isl_map_is_injective(map
);
11135 isl_bool
isl_set_is_singleton(__isl_keep isl_set
*set
)
11137 return isl_map_is_single_valued(set_to_map(set
));
11140 /* Does "map" only map elements to themselves?
11142 * If the domain and range spaces are different, then "map"
11143 * is considered not to be an identity relation, even if it is empty.
11144 * Otherwise, construct the maximal identity relation and
11145 * check whether "map" is a subset of this relation.
11147 isl_bool
isl_map_is_identity(__isl_keep isl_map
*map
)
11151 isl_bool equal
, is_identity
;
11153 space
= isl_map_get_space(map
);
11154 equal
= isl_space_tuple_is_equal(space
, isl_dim_in
, space
, isl_dim_out
);
11155 isl_space_free(space
);
11156 if (equal
< 0 || !equal
)
11159 id
= isl_map_identity(isl_map_get_space(map
));
11160 is_identity
= isl_map_is_subset(map
, id
);
11163 return is_identity
;
11166 int isl_map_is_translation(__isl_keep isl_map
*map
)
11171 delta
= isl_map_deltas(isl_map_copy(map
));
11172 ok
= isl_set_is_singleton(delta
);
11173 isl_set_free(delta
);
11178 static int unique(isl_int
*p
, unsigned pos
, unsigned len
)
11180 if (isl_seq_first_non_zero(p
, pos
) != -1)
11182 if (isl_seq_first_non_zero(p
+ pos
+ 1, len
- pos
- 1) != -1)
11187 int isl_basic_set_is_box(__isl_keep isl_basic_set
*bset
)
11196 if (isl_basic_set_dim(bset
, isl_dim_div
) != 0)
11199 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
11200 ovar
= isl_space_offset(bset
->dim
, isl_dim_set
);
11201 for (j
= 0; j
< nvar
; ++j
) {
11202 int lower
= 0, upper
= 0;
11203 for (i
= 0; i
< bset
->n_eq
; ++i
) {
11204 if (isl_int_is_zero(bset
->eq
[i
][1 + ovar
+ j
]))
11206 if (!unique(bset
->eq
[i
] + 1 + ovar
, j
, nvar
))
11210 if (i
< bset
->n_eq
)
11212 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
11213 if (isl_int_is_zero(bset
->ineq
[i
][1 + ovar
+ j
]))
11215 if (!unique(bset
->ineq
[i
] + 1 + ovar
, j
, nvar
))
11217 if (isl_int_is_pos(bset
->ineq
[i
][1 + ovar
+ j
]))
11222 if (!lower
|| !upper
)
11229 int isl_set_is_box(__isl_keep isl_set
*set
)
11236 return isl_basic_set_is_box(set
->p
[0]);
11239 isl_bool
isl_basic_set_is_wrapping(__isl_keep isl_basic_set
*bset
)
11242 return isl_bool_error
;
11244 return isl_space_is_wrapping(bset
->dim
);
11247 isl_bool
isl_set_is_wrapping(__isl_keep isl_set
*set
)
11250 return isl_bool_error
;
11252 return isl_space_is_wrapping(set
->dim
);
11255 /* Modify the space of "map" through a call to "change".
11256 * If "can_change" is set (not NULL), then first call it to check
11257 * if the modification is allowed, printing the error message "cannot_change"
11260 static __isl_give isl_map
*isl_map_change_space(__isl_take isl_map
*map
,
11261 isl_bool (*can_change
)(__isl_keep isl_map
*map
),
11262 const char *cannot_change
,
11263 __isl_give isl_space
*(*change
)(__isl_take isl_space
*space
))
11271 ok
= can_change
? can_change(map
) : isl_bool_true
;
11273 return isl_map_free(map
);
11275 isl_die(isl_map_get_ctx(map
), isl_error_invalid
, cannot_change
,
11276 return isl_map_free(map
));
11278 space
= change(isl_map_get_space(map
));
11279 map
= isl_map_reset_space(map
, space
);
11284 /* Is the domain of "map" a wrapped relation?
11286 isl_bool
isl_map_domain_is_wrapping(__isl_keep isl_map
*map
)
11289 return isl_bool_error
;
11291 return isl_space_domain_is_wrapping(map
->dim
);
11294 /* Is the range of "map" a wrapped relation?
11296 isl_bool
isl_map_range_is_wrapping(__isl_keep isl_map
*map
)
11299 return isl_bool_error
;
11301 return isl_space_range_is_wrapping(map
->dim
);
11304 __isl_give isl_basic_set
*isl_basic_map_wrap(__isl_take isl_basic_map
*bmap
)
11306 bmap
= isl_basic_map_cow(bmap
);
11310 bmap
->dim
= isl_space_wrap(bmap
->dim
);
11314 bmap
= isl_basic_map_finalize(bmap
);
11316 return bset_from_bmap(bmap
);
11318 isl_basic_map_free(bmap
);
11322 /* Given a map A -> B, return the set (A -> B).
11324 __isl_give isl_set
*isl_map_wrap(__isl_take isl_map
*map
)
11326 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_wrap
);
11329 __isl_give isl_basic_map
*isl_basic_set_unwrap(__isl_take isl_basic_set
*bset
)
11331 bset
= isl_basic_set_cow(bset
);
11335 bset
->dim
= isl_space_unwrap(bset
->dim
);
11339 bset
= isl_basic_set_finalize(bset
);
11341 return bset_to_bmap(bset
);
11343 isl_basic_set_free(bset
);
11347 /* Given a set (A -> B), return the map A -> B.
11348 * Error out if "set" is not of the form (A -> B).
11350 __isl_give isl_map
*isl_set_unwrap(__isl_take isl_set
*set
)
11352 return isl_map_change_space(set
, &isl_set_is_wrapping
,
11353 "not a wrapping set", &isl_space_unwrap
);
11356 __isl_give isl_basic_map
*isl_basic_map_reset(__isl_take isl_basic_map
*bmap
,
11357 enum isl_dim_type type
)
11362 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
11365 bmap
= isl_basic_map_cow(bmap
);
11369 bmap
->dim
= isl_space_reset(bmap
->dim
, type
);
11373 bmap
= isl_basic_map_finalize(bmap
);
11377 isl_basic_map_free(bmap
);
11381 __isl_give isl_map
*isl_map_reset(__isl_take isl_map
*map
,
11382 enum isl_dim_type type
)
11389 if (!isl_space_is_named_or_nested(map
->dim
, type
))
11392 map
= isl_map_cow(map
);
11396 for (i
= 0; i
< map
->n
; ++i
) {
11397 map
->p
[i
] = isl_basic_map_reset(map
->p
[i
], type
);
11401 map
->dim
= isl_space_reset(map
->dim
, type
);
11411 __isl_give isl_basic_map
*isl_basic_map_flatten(__isl_take isl_basic_map
*bmap
)
11416 if (!bmap
->dim
->nested
[0] && !bmap
->dim
->nested
[1])
11419 bmap
= isl_basic_map_cow(bmap
);
11423 bmap
->dim
= isl_space_flatten(bmap
->dim
);
11427 bmap
= isl_basic_map_finalize(bmap
);
11431 isl_basic_map_free(bmap
);
11435 __isl_give isl_basic_set
*isl_basic_set_flatten(__isl_take isl_basic_set
*bset
)
11437 return bset_from_bmap(isl_basic_map_flatten(bset_to_bmap(bset
)));
11440 __isl_give isl_basic_map
*isl_basic_map_flatten_domain(
11441 __isl_take isl_basic_map
*bmap
)
11446 if (!bmap
->dim
->nested
[0])
11449 bmap
= isl_basic_map_cow(bmap
);
11453 bmap
->dim
= isl_space_flatten_domain(bmap
->dim
);
11457 bmap
= isl_basic_map_finalize(bmap
);
11461 isl_basic_map_free(bmap
);
11465 __isl_give isl_basic_map
*isl_basic_map_flatten_range(
11466 __isl_take isl_basic_map
*bmap
)
11471 if (!bmap
->dim
->nested
[1])
11474 bmap
= isl_basic_map_cow(bmap
);
11478 bmap
->dim
= isl_space_flatten_range(bmap
->dim
);
11482 bmap
= isl_basic_map_finalize(bmap
);
11486 isl_basic_map_free(bmap
);
11490 /* Remove any internal structure from the spaces of domain and range of "map".
11492 __isl_give isl_map
*isl_map_flatten(__isl_take isl_map
*map
)
11497 if (!map
->dim
->nested
[0] && !map
->dim
->nested
[1])
11500 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten
);
11503 __isl_give isl_set
*isl_set_flatten(__isl_take isl_set
*set
)
11505 return set_from_map(isl_map_flatten(set_to_map(set
)));
11508 __isl_give isl_map
*isl_set_flatten_map(__isl_take isl_set
*set
)
11510 isl_space
*dim
, *flat_dim
;
11513 dim
= isl_set_get_space(set
);
11514 flat_dim
= isl_space_flatten(isl_space_copy(dim
));
11515 map
= isl_map_identity(isl_space_join(isl_space_reverse(dim
), flat_dim
));
11516 map
= isl_map_intersect_domain(map
, set
);
11521 /* Remove any internal structure from the space of the domain of "map".
11523 __isl_give isl_map
*isl_map_flatten_domain(__isl_take isl_map
*map
)
11528 if (!map
->dim
->nested
[0])
11531 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten_domain
);
11534 /* Remove any internal structure from the space of the range of "map".
11536 __isl_give isl_map
*isl_map_flatten_range(__isl_take isl_map
*map
)
11541 if (!map
->dim
->nested
[1])
11544 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten_range
);
11547 /* Reorder the dimensions of "bmap" according to the given dim_map
11548 * and set the dimension specification to "dim" and
11549 * perform Gaussian elimination on the result.
11551 __isl_give isl_basic_map
*isl_basic_map_realign(__isl_take isl_basic_map
*bmap
,
11552 __isl_take isl_space
*dim
, __isl_take
struct isl_dim_map
*dim_map
)
11554 isl_basic_map
*res
;
11557 bmap
= isl_basic_map_cow(bmap
);
11558 if (!bmap
|| !dim
|| !dim_map
)
11561 flags
= bmap
->flags
;
11562 ISL_FL_CLR(flags
, ISL_BASIC_MAP_FINAL
);
11563 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED
);
11564 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
11565 res
= isl_basic_map_alloc_space(dim
,
11566 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
11567 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
11569 res
->flags
= flags
;
11570 res
= isl_basic_map_gauss(res
, NULL
);
11571 res
= isl_basic_map_finalize(res
);
11575 isl_basic_map_free(bmap
);
11576 isl_space_free(dim
);
11580 /* Reorder the dimensions of "map" according to given reordering.
11582 __isl_give isl_map
*isl_map_realign(__isl_take isl_map
*map
,
11583 __isl_take isl_reordering
*r
)
11586 struct isl_dim_map
*dim_map
;
11588 map
= isl_map_cow(map
);
11589 dim_map
= isl_dim_map_from_reordering(r
);
11590 if (!map
|| !r
|| !dim_map
)
11593 for (i
= 0; i
< map
->n
; ++i
) {
11594 struct isl_dim_map
*dim_map_i
;
11596 dim_map_i
= isl_dim_map_extend(dim_map
, map
->p
[i
]);
11598 map
->p
[i
] = isl_basic_map_realign(map
->p
[i
],
11599 isl_space_copy(r
->dim
), dim_map_i
);
11605 map
= isl_map_reset_space(map
, isl_space_copy(r
->dim
));
11607 isl_reordering_free(r
);
11613 isl_reordering_free(r
);
11617 __isl_give isl_set
*isl_set_realign(__isl_take isl_set
*set
,
11618 __isl_take isl_reordering
*r
)
11620 return set_from_map(isl_map_realign(set_to_map(set
), r
));
11623 __isl_give isl_map
*isl_map_align_params(__isl_take isl_map
*map
,
11624 __isl_take isl_space
*model
)
11628 if (!map
|| !model
)
11631 ctx
= isl_space_get_ctx(model
);
11632 if (!isl_space_has_named_params(model
))
11633 isl_die(ctx
, isl_error_invalid
,
11634 "model has unnamed parameters", goto error
);
11635 if (!isl_space_has_named_params(map
->dim
))
11636 isl_die(ctx
, isl_error_invalid
,
11637 "relation has unnamed parameters", goto error
);
11638 if (!isl_space_match(map
->dim
, isl_dim_param
, model
, isl_dim_param
)) {
11639 isl_reordering
*exp
;
11641 model
= isl_space_drop_dims(model
, isl_dim_in
,
11642 0, isl_space_dim(model
, isl_dim_in
));
11643 model
= isl_space_drop_dims(model
, isl_dim_out
,
11644 0, isl_space_dim(model
, isl_dim_out
));
11645 exp
= isl_parameter_alignment_reordering(map
->dim
, model
);
11646 exp
= isl_reordering_extend_space(exp
, isl_map_get_space(map
));
11647 map
= isl_map_realign(map
, exp
);
11650 isl_space_free(model
);
11653 isl_space_free(model
);
11658 __isl_give isl_set
*isl_set_align_params(__isl_take isl_set
*set
,
11659 __isl_take isl_space
*model
)
11661 return isl_map_align_params(set
, model
);
11664 /* Align the parameters of "bmap" to those of "model", introducing
11665 * additional parameters if needed.
11667 __isl_give isl_basic_map
*isl_basic_map_align_params(
11668 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*model
)
11672 if (!bmap
|| !model
)
11675 ctx
= isl_space_get_ctx(model
);
11676 if (!isl_space_has_named_params(model
))
11677 isl_die(ctx
, isl_error_invalid
,
11678 "model has unnamed parameters", goto error
);
11679 if (!isl_space_has_named_params(bmap
->dim
))
11680 isl_die(ctx
, isl_error_invalid
,
11681 "relation has unnamed parameters", goto error
);
11682 if (!isl_space_match(bmap
->dim
, isl_dim_param
, model
, isl_dim_param
)) {
11683 isl_reordering
*exp
;
11684 struct isl_dim_map
*dim_map
;
11686 model
= isl_space_drop_dims(model
, isl_dim_in
,
11687 0, isl_space_dim(model
, isl_dim_in
));
11688 model
= isl_space_drop_dims(model
, isl_dim_out
,
11689 0, isl_space_dim(model
, isl_dim_out
));
11690 exp
= isl_parameter_alignment_reordering(bmap
->dim
, model
);
11691 exp
= isl_reordering_extend_space(exp
,
11692 isl_basic_map_get_space(bmap
));
11693 dim_map
= isl_dim_map_from_reordering(exp
);
11694 bmap
= isl_basic_map_realign(bmap
,
11695 exp
? isl_space_copy(exp
->dim
) : NULL
,
11696 isl_dim_map_extend(dim_map
, bmap
));
11697 isl_reordering_free(exp
);
11701 isl_space_free(model
);
11704 isl_space_free(model
);
11705 isl_basic_map_free(bmap
);
11709 /* Align the parameters of "bset" to those of "model", introducing
11710 * additional parameters if needed.
11712 __isl_give isl_basic_set
*isl_basic_set_align_params(
11713 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*model
)
11715 return isl_basic_map_align_params(bset
, model
);
11718 __isl_give isl_mat
*isl_basic_map_equalities_matrix(
11719 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
11720 enum isl_dim_type c2
, enum isl_dim_type c3
,
11721 enum isl_dim_type c4
, enum isl_dim_type c5
)
11723 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11724 struct isl_mat
*mat
;
11730 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_eq
,
11731 isl_basic_map_total_dim(bmap
) + 1);
11734 for (i
= 0; i
< bmap
->n_eq
; ++i
)
11735 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11736 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11737 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11738 isl_int_set(mat
->row
[i
][pos
],
11739 bmap
->eq
[i
][off
+ k
]);
11747 __isl_give isl_mat
*isl_basic_map_inequalities_matrix(
11748 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
11749 enum isl_dim_type c2
, enum isl_dim_type c3
,
11750 enum isl_dim_type c4
, enum isl_dim_type c5
)
11752 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11753 struct isl_mat
*mat
;
11759 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_ineq
,
11760 isl_basic_map_total_dim(bmap
) + 1);
11763 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
11764 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11765 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11766 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11767 isl_int_set(mat
->row
[i
][pos
],
11768 bmap
->ineq
[i
][off
+ k
]);
11776 __isl_give isl_basic_map
*isl_basic_map_from_constraint_matrices(
11777 __isl_take isl_space
*dim
,
11778 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
11779 enum isl_dim_type c2
, enum isl_dim_type c3
,
11780 enum isl_dim_type c4
, enum isl_dim_type c5
)
11782 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11783 isl_basic_map
*bmap
;
11789 if (!dim
|| !eq
|| !ineq
)
11792 if (eq
->n_col
!= ineq
->n_col
)
11793 isl_die(dim
->ctx
, isl_error_invalid
,
11794 "equalities and inequalities matrices should have "
11795 "same number of columns", goto error
);
11797 total
= 1 + isl_space_dim(dim
, isl_dim_all
);
11799 if (eq
->n_col
< total
)
11800 isl_die(dim
->ctx
, isl_error_invalid
,
11801 "number of columns too small", goto error
);
11803 extra
= eq
->n_col
- total
;
11805 bmap
= isl_basic_map_alloc_space(isl_space_copy(dim
), extra
,
11806 eq
->n_row
, ineq
->n_row
);
11809 for (i
= 0; i
< extra
; ++i
) {
11810 k
= isl_basic_map_alloc_div(bmap
);
11813 isl_int_set_si(bmap
->div
[k
][0], 0);
11815 for (i
= 0; i
< eq
->n_row
; ++i
) {
11816 l
= isl_basic_map_alloc_equality(bmap
);
11819 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11820 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11821 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11822 isl_int_set(bmap
->eq
[l
][off
+ k
],
11828 for (i
= 0; i
< ineq
->n_row
; ++i
) {
11829 l
= isl_basic_map_alloc_inequality(bmap
);
11832 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11833 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11834 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11835 isl_int_set(bmap
->ineq
[l
][off
+ k
],
11836 ineq
->row
[i
][pos
]);
11842 isl_space_free(dim
);
11844 isl_mat_free(ineq
);
11846 bmap
= isl_basic_map_simplify(bmap
);
11847 return isl_basic_map_finalize(bmap
);
11849 isl_space_free(dim
);
11851 isl_mat_free(ineq
);
11855 __isl_give isl_mat
*isl_basic_set_equalities_matrix(
11856 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
11857 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
11859 return isl_basic_map_equalities_matrix(bset_to_bmap(bset
),
11860 c1
, c2
, c3
, c4
, isl_dim_in
);
11863 __isl_give isl_mat
*isl_basic_set_inequalities_matrix(
11864 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
11865 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
11867 return isl_basic_map_inequalities_matrix(bset_to_bmap(bset
),
11868 c1
, c2
, c3
, c4
, isl_dim_in
);
11871 __isl_give isl_basic_set
*isl_basic_set_from_constraint_matrices(
11872 __isl_take isl_space
*dim
,
11873 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
11874 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
11876 isl_basic_map
*bmap
;
11877 bmap
= isl_basic_map_from_constraint_matrices(dim
, eq
, ineq
,
11878 c1
, c2
, c3
, c4
, isl_dim_in
);
11879 return bset_from_bmap(bmap
);
11882 isl_bool
isl_basic_map_can_zip(__isl_keep isl_basic_map
*bmap
)
11885 return isl_bool_error
;
11887 return isl_space_can_zip(bmap
->dim
);
11890 isl_bool
isl_map_can_zip(__isl_keep isl_map
*map
)
11893 return isl_bool_error
;
11895 return isl_space_can_zip(map
->dim
);
11898 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
11899 * (A -> C) -> (B -> D).
11901 __isl_give isl_basic_map
*isl_basic_map_zip(__isl_take isl_basic_map
*bmap
)
11910 if (!isl_basic_map_can_zip(bmap
))
11911 isl_die(bmap
->ctx
, isl_error_invalid
,
11912 "basic map cannot be zipped", goto error
);
11913 pos
= isl_basic_map_offset(bmap
, isl_dim_in
) +
11914 isl_space_dim(bmap
->dim
->nested
[0], isl_dim_in
);
11915 n1
= isl_space_dim(bmap
->dim
->nested
[0], isl_dim_out
);
11916 n2
= isl_space_dim(bmap
->dim
->nested
[1], isl_dim_in
);
11917 bmap
= isl_basic_map_cow(bmap
);
11918 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
11921 bmap
->dim
= isl_space_zip(bmap
->dim
);
11924 bmap
= isl_basic_map_mark_final(bmap
);
11927 isl_basic_map_free(bmap
);
11931 /* Given a map (A -> B) -> (C -> D), return the corresponding map
11932 * (A -> C) -> (B -> D).
11934 __isl_give isl_map
*isl_map_zip(__isl_take isl_map
*map
)
11941 if (!isl_map_can_zip(map
))
11942 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be zipped",
11945 map
= isl_map_cow(map
);
11949 for (i
= 0; i
< map
->n
; ++i
) {
11950 map
->p
[i
] = isl_basic_map_zip(map
->p
[i
]);
11955 map
->dim
= isl_space_zip(map
->dim
);
11965 /* Can we apply isl_basic_map_curry to "bmap"?
11966 * That is, does it have a nested relation in its domain?
11968 isl_bool
isl_basic_map_can_curry(__isl_keep isl_basic_map
*bmap
)
11971 return isl_bool_error
;
11973 return isl_space_can_curry(bmap
->dim
);
11976 /* Can we apply isl_map_curry to "map"?
11977 * That is, does it have a nested relation in its domain?
11979 isl_bool
isl_map_can_curry(__isl_keep isl_map
*map
)
11982 return isl_bool_error
;
11984 return isl_space_can_curry(map
->dim
);
11987 /* Given a basic map (A -> B) -> C, return the corresponding basic map
11990 __isl_give isl_basic_map
*isl_basic_map_curry(__isl_take isl_basic_map
*bmap
)
11996 if (!isl_basic_map_can_curry(bmap
))
11997 isl_die(bmap
->ctx
, isl_error_invalid
,
11998 "basic map cannot be curried", goto error
);
11999 bmap
= isl_basic_map_cow(bmap
);
12002 bmap
->dim
= isl_space_curry(bmap
->dim
);
12005 bmap
= isl_basic_map_mark_final(bmap
);
12008 isl_basic_map_free(bmap
);
12012 /* Given a map (A -> B) -> C, return the corresponding map
12015 __isl_give isl_map
*isl_map_curry(__isl_take isl_map
*map
)
12017 return isl_map_change_space(map
, &isl_map_can_curry
,
12018 "map cannot be curried", &isl_space_curry
);
12021 /* Can isl_map_range_curry be applied to "map"?
12022 * That is, does it have a nested relation in its range,
12023 * the domain of which is itself a nested relation?
12025 isl_bool
isl_map_can_range_curry(__isl_keep isl_map
*map
)
12028 return isl_bool_error
;
12030 return isl_space_can_range_curry(map
->dim
);
12033 /* Given a map A -> ((B -> C) -> D), return the corresponding map
12034 * A -> (B -> (C -> D)).
12036 __isl_give isl_map
*isl_map_range_curry(__isl_take isl_map
*map
)
12038 return isl_map_change_space(map
, &isl_map_can_range_curry
,
12039 "map range cannot be curried",
12040 &isl_space_range_curry
);
12043 /* Can we apply isl_basic_map_uncurry to "bmap"?
12044 * That is, does it have a nested relation in its domain?
12046 isl_bool
isl_basic_map_can_uncurry(__isl_keep isl_basic_map
*bmap
)
12049 return isl_bool_error
;
12051 return isl_space_can_uncurry(bmap
->dim
);
12054 /* Can we apply isl_map_uncurry to "map"?
12055 * That is, does it have a nested relation in its domain?
12057 isl_bool
isl_map_can_uncurry(__isl_keep isl_map
*map
)
12060 return isl_bool_error
;
12062 return isl_space_can_uncurry(map
->dim
);
12065 /* Given a basic map A -> (B -> C), return the corresponding basic map
12068 __isl_give isl_basic_map
*isl_basic_map_uncurry(__isl_take isl_basic_map
*bmap
)
12074 if (!isl_basic_map_can_uncurry(bmap
))
12075 isl_die(bmap
->ctx
, isl_error_invalid
,
12076 "basic map cannot be uncurried",
12077 return isl_basic_map_free(bmap
));
12078 bmap
= isl_basic_map_cow(bmap
);
12081 bmap
->dim
= isl_space_uncurry(bmap
->dim
);
12083 return isl_basic_map_free(bmap
);
12084 bmap
= isl_basic_map_mark_final(bmap
);
12088 /* Given a map A -> (B -> C), return the corresponding map
12091 __isl_give isl_map
*isl_map_uncurry(__isl_take isl_map
*map
)
12093 return isl_map_change_space(map
, &isl_map_can_uncurry
,
12094 "map cannot be uncurried", &isl_space_uncurry
);
12097 /* Construct a basic map mapping the domain of the affine expression
12098 * to a one-dimensional range prescribed by the affine expression.
12099 * If "rational" is set, then construct a rational basic map.
12101 * A NaN affine expression cannot be converted to a basic map.
12103 static __isl_give isl_basic_map
*isl_basic_map_from_aff2(
12104 __isl_take isl_aff
*aff
, int rational
)
12109 isl_local_space
*ls
;
12110 isl_basic_map
*bmap
= NULL
;
12114 is_nan
= isl_aff_is_nan(aff
);
12118 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
12119 "cannot convert NaN", goto error
);
12121 ls
= isl_aff_get_local_space(aff
);
12122 bmap
= isl_basic_map_from_local_space(ls
);
12123 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
12124 k
= isl_basic_map_alloc_equality(bmap
);
12128 pos
= isl_basic_map_offset(bmap
, isl_dim_out
);
12129 isl_seq_cpy(bmap
->eq
[k
], aff
->v
->el
+ 1, pos
);
12130 isl_int_neg(bmap
->eq
[k
][pos
], aff
->v
->el
[0]);
12131 isl_seq_cpy(bmap
->eq
[k
] + pos
+ 1, aff
->v
->el
+ 1 + pos
,
12132 aff
->v
->size
- (pos
+ 1));
12136 bmap
= isl_basic_map_set_rational(bmap
);
12137 bmap
= isl_basic_map_finalize(bmap
);
12141 isl_basic_map_free(bmap
);
12145 /* Construct a basic map mapping the domain of the affine expression
12146 * to a one-dimensional range prescribed by the affine expression.
12148 __isl_give isl_basic_map
*isl_basic_map_from_aff(__isl_take isl_aff
*aff
)
12150 return isl_basic_map_from_aff2(aff
, 0);
12153 /* Construct a map mapping the domain of the affine expression
12154 * to a one-dimensional range prescribed by the affine expression.
12156 __isl_give isl_map
*isl_map_from_aff(__isl_take isl_aff
*aff
)
12158 isl_basic_map
*bmap
;
12160 bmap
= isl_basic_map_from_aff(aff
);
12161 return isl_map_from_basic_map(bmap
);
12164 /* Construct a basic map mapping the domain the multi-affine expression
12165 * to its range, with each dimension in the range equated to the
12166 * corresponding affine expression.
12167 * If "rational" is set, then construct a rational basic map.
12169 __isl_give isl_basic_map
*isl_basic_map_from_multi_aff2(
12170 __isl_take isl_multi_aff
*maff
, int rational
)
12174 isl_basic_map
*bmap
;
12179 if (isl_space_dim(maff
->space
, isl_dim_out
) != maff
->n
)
12180 isl_die(isl_multi_aff_get_ctx(maff
), isl_error_internal
,
12181 "invalid space", goto error
);
12183 space
= isl_space_domain(isl_multi_aff_get_space(maff
));
12184 bmap
= isl_basic_map_universe(isl_space_from_domain(space
));
12186 bmap
= isl_basic_map_set_rational(bmap
);
12188 for (i
= 0; i
< maff
->n
; ++i
) {
12190 isl_basic_map
*bmap_i
;
12192 aff
= isl_aff_copy(maff
->p
[i
]);
12193 bmap_i
= isl_basic_map_from_aff2(aff
, rational
);
12195 bmap
= isl_basic_map_flat_range_product(bmap
, bmap_i
);
12198 bmap
= isl_basic_map_reset_space(bmap
, isl_multi_aff_get_space(maff
));
12200 isl_multi_aff_free(maff
);
12203 isl_multi_aff_free(maff
);
12207 /* Construct a basic map mapping the domain the multi-affine expression
12208 * to its range, with each dimension in the range equated to the
12209 * corresponding affine expression.
12211 __isl_give isl_basic_map
*isl_basic_map_from_multi_aff(
12212 __isl_take isl_multi_aff
*ma
)
12214 return isl_basic_map_from_multi_aff2(ma
, 0);
12217 /* Construct a map mapping the domain the multi-affine expression
12218 * to its range, with each dimension in the range equated to the
12219 * corresponding affine expression.
12221 __isl_give isl_map
*isl_map_from_multi_aff(__isl_take isl_multi_aff
*maff
)
12223 isl_basic_map
*bmap
;
12225 bmap
= isl_basic_map_from_multi_aff(maff
);
12226 return isl_map_from_basic_map(bmap
);
12229 /* Construct a basic map mapping a domain in the given space to
12230 * to an n-dimensional range, with n the number of elements in the list,
12231 * where each coordinate in the range is prescribed by the
12232 * corresponding affine expression.
12233 * The domains of all affine expressions in the list are assumed to match
12236 __isl_give isl_basic_map
*isl_basic_map_from_aff_list(
12237 __isl_take isl_space
*domain_dim
, __isl_take isl_aff_list
*list
)
12241 isl_basic_map
*bmap
;
12246 dim
= isl_space_from_domain(domain_dim
);
12247 bmap
= isl_basic_map_universe(dim
);
12249 for (i
= 0; i
< list
->n
; ++i
) {
12251 isl_basic_map
*bmap_i
;
12253 aff
= isl_aff_copy(list
->p
[i
]);
12254 bmap_i
= isl_basic_map_from_aff(aff
);
12256 bmap
= isl_basic_map_flat_range_product(bmap
, bmap_i
);
12259 isl_aff_list_free(list
);
12263 __isl_give isl_set
*isl_set_equate(__isl_take isl_set
*set
,
12264 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12266 return isl_map_equate(set
, type1
, pos1
, type2
, pos2
);
12269 /* Construct a basic map where the given dimensions are equal to each other.
12271 static __isl_give isl_basic_map
*equator(__isl_take isl_space
*space
,
12272 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12274 isl_basic_map
*bmap
= NULL
;
12280 if (pos1
>= isl_space_dim(space
, type1
))
12281 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
12282 "index out of bounds", goto error
);
12283 if (pos2
>= isl_space_dim(space
, type2
))
12284 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
12285 "index out of bounds", goto error
);
12287 if (type1
== type2
&& pos1
== pos2
)
12288 return isl_basic_map_universe(space
);
12290 bmap
= isl_basic_map_alloc_space(isl_space_copy(space
), 0, 1, 0);
12291 i
= isl_basic_map_alloc_equality(bmap
);
12294 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
12295 pos1
+= isl_basic_map_offset(bmap
, type1
);
12296 pos2
+= isl_basic_map_offset(bmap
, type2
);
12297 isl_int_set_si(bmap
->eq
[i
][pos1
], -1);
12298 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
12299 bmap
= isl_basic_map_finalize(bmap
);
12300 isl_space_free(space
);
12303 isl_space_free(space
);
12304 isl_basic_map_free(bmap
);
12308 /* Add a constraint imposing that the given two dimensions are equal.
12310 __isl_give isl_basic_map
*isl_basic_map_equate(__isl_take isl_basic_map
*bmap
,
12311 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12315 eq
= equator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
12317 bmap
= isl_basic_map_intersect(bmap
, eq
);
12322 /* Add a constraint imposing that the given two dimensions are equal.
12324 __isl_give isl_map
*isl_map_equate(__isl_take isl_map
*map
,
12325 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12327 isl_basic_map
*bmap
;
12329 bmap
= equator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
12331 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
12336 /* Add a constraint imposing that the given two dimensions have opposite values.
12338 __isl_give isl_map
*isl_map_oppose(__isl_take isl_map
*map
,
12339 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12341 isl_basic_map
*bmap
= NULL
;
12347 if (pos1
>= isl_map_dim(map
, type1
))
12348 isl_die(map
->ctx
, isl_error_invalid
,
12349 "index out of bounds", goto error
);
12350 if (pos2
>= isl_map_dim(map
, type2
))
12351 isl_die(map
->ctx
, isl_error_invalid
,
12352 "index out of bounds", goto error
);
12354 bmap
= isl_basic_map_alloc_space(isl_map_get_space(map
), 0, 1, 0);
12355 i
= isl_basic_map_alloc_equality(bmap
);
12358 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
12359 pos1
+= isl_basic_map_offset(bmap
, type1
);
12360 pos2
+= isl_basic_map_offset(bmap
, type2
);
12361 isl_int_set_si(bmap
->eq
[i
][pos1
], 1);
12362 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
12363 bmap
= isl_basic_map_finalize(bmap
);
12365 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
12369 isl_basic_map_free(bmap
);
12374 /* Construct a constraint imposing that the value of the first dimension is
12375 * greater than or equal to that of the second.
12377 static __isl_give isl_constraint
*constraint_order_ge(
12378 __isl_take isl_space
*space
, enum isl_dim_type type1
, int pos1
,
12379 enum isl_dim_type type2
, int pos2
)
12386 c
= isl_constraint_alloc_inequality(isl_local_space_from_space(space
));
12388 if (pos1
>= isl_constraint_dim(c
, type1
))
12389 isl_die(isl_constraint_get_ctx(c
), isl_error_invalid
,
12390 "index out of bounds", return isl_constraint_free(c
));
12391 if (pos2
>= isl_constraint_dim(c
, type2
))
12392 isl_die(isl_constraint_get_ctx(c
), isl_error_invalid
,
12393 "index out of bounds", return isl_constraint_free(c
));
12395 if (type1
== type2
&& pos1
== pos2
)
12398 c
= isl_constraint_set_coefficient_si(c
, type1
, pos1
, 1);
12399 c
= isl_constraint_set_coefficient_si(c
, type2
, pos2
, -1);
12404 /* Add a constraint imposing that the value of the first dimension is
12405 * greater than or equal to that of the second.
12407 __isl_give isl_basic_map
*isl_basic_map_order_ge(__isl_take isl_basic_map
*bmap
,
12408 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12413 if (type1
== type2
&& pos1
== pos2
)
12415 space
= isl_basic_map_get_space(bmap
);
12416 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
12417 bmap
= isl_basic_map_add_constraint(bmap
, c
);
12422 /* Add a constraint imposing that the value of the first dimension is
12423 * greater than or equal to that of the second.
12425 __isl_give isl_map
*isl_map_order_ge(__isl_take isl_map
*map
,
12426 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12431 if (type1
== type2
&& pos1
== pos2
)
12433 space
= isl_map_get_space(map
);
12434 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
12435 map
= isl_map_add_constraint(map
, c
);
12440 /* Add a constraint imposing that the value of the first dimension is
12441 * less than or equal to that of the second.
12443 __isl_give isl_map
*isl_map_order_le(__isl_take isl_map
*map
,
12444 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12446 return isl_map_order_ge(map
, type2
, pos2
, type1
, pos1
);
12449 /* Construct a basic map where the value of the first dimension is
12450 * greater than that of the second.
12452 static __isl_give isl_basic_map
*greator(__isl_take isl_space
*space
,
12453 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12455 isl_basic_map
*bmap
= NULL
;
12461 if (pos1
>= isl_space_dim(space
, type1
))
12462 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
12463 "index out of bounds", goto error
);
12464 if (pos2
>= isl_space_dim(space
, type2
))
12465 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
12466 "index out of bounds", goto error
);
12468 if (type1
== type2
&& pos1
== pos2
)
12469 return isl_basic_map_empty(space
);
12471 bmap
= isl_basic_map_alloc_space(space
, 0, 0, 1);
12472 i
= isl_basic_map_alloc_inequality(bmap
);
12474 return isl_basic_map_free(bmap
);
12475 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
12476 pos1
+= isl_basic_map_offset(bmap
, type1
);
12477 pos2
+= isl_basic_map_offset(bmap
, type2
);
12478 isl_int_set_si(bmap
->ineq
[i
][pos1
], 1);
12479 isl_int_set_si(bmap
->ineq
[i
][pos2
], -1);
12480 isl_int_set_si(bmap
->ineq
[i
][0], -1);
12481 bmap
= isl_basic_map_finalize(bmap
);
12485 isl_space_free(space
);
12486 isl_basic_map_free(bmap
);
12490 /* Add a constraint imposing that the value of the first dimension is
12491 * greater than that of the second.
12493 __isl_give isl_basic_map
*isl_basic_map_order_gt(__isl_take isl_basic_map
*bmap
,
12494 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12498 gt
= greator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
12500 bmap
= isl_basic_map_intersect(bmap
, gt
);
12505 /* Add a constraint imposing that the value of the first dimension is
12506 * greater than that of the second.
12508 __isl_give isl_map
*isl_map_order_gt(__isl_take isl_map
*map
,
12509 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12511 isl_basic_map
*bmap
;
12513 bmap
= greator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
12515 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
12520 /* Add a constraint imposing that the value of the first dimension is
12521 * smaller than that of the second.
12523 __isl_give isl_map
*isl_map_order_lt(__isl_take isl_map
*map
,
12524 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12526 return isl_map_order_gt(map
, type2
, pos2
, type1
, pos1
);
12529 __isl_give isl_aff
*isl_basic_map_get_div(__isl_keep isl_basic_map
*bmap
,
12533 isl_local_space
*ls
;
12538 if (!isl_basic_map_divs_known(bmap
))
12539 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12540 "some divs are unknown", return NULL
);
12542 ls
= isl_basic_map_get_local_space(bmap
);
12543 div
= isl_local_space_get_div(ls
, pos
);
12544 isl_local_space_free(ls
);
12549 __isl_give isl_aff
*isl_basic_set_get_div(__isl_keep isl_basic_set
*bset
,
12552 return isl_basic_map_get_div(bset
, pos
);
12555 /* Plug in "subs" for dimension "type", "pos" of "bset".
12557 * Let i be the dimension to replace and let "subs" be of the form
12561 * Any integer division with a non-zero coefficient for i,
12563 * floor((a i + g)/m)
12567 * floor((a f + d g)/(m d))
12569 * Constraints of the form
12577 * We currently require that "subs" is an integral expression.
12578 * Handling rational expressions may require us to add stride constraints
12579 * as we do in isl_basic_set_preimage_multi_aff.
12581 __isl_give isl_basic_set
*isl_basic_set_substitute(
12582 __isl_take isl_basic_set
*bset
,
12583 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
12589 if (bset
&& isl_basic_set_plain_is_empty(bset
))
12592 bset
= isl_basic_set_cow(bset
);
12593 if (!bset
|| !subs
)
12596 ctx
= isl_basic_set_get_ctx(bset
);
12597 if (!isl_space_is_equal(bset
->dim
, subs
->ls
->dim
))
12598 isl_die(ctx
, isl_error_invalid
,
12599 "spaces don't match", goto error
);
12600 if (isl_local_space_dim(subs
->ls
, isl_dim_div
) != 0)
12601 isl_die(ctx
, isl_error_unsupported
,
12602 "cannot handle divs yet", goto error
);
12603 if (!isl_int_is_one(subs
->v
->el
[0]))
12604 isl_die(ctx
, isl_error_invalid
,
12605 "can only substitute integer expressions", goto error
);
12607 pos
+= isl_basic_set_offset(bset
, type
);
12611 for (i
= 0; i
< bset
->n_eq
; ++i
) {
12612 if (isl_int_is_zero(bset
->eq
[i
][pos
]))
12614 isl_int_set(v
, bset
->eq
[i
][pos
]);
12615 isl_int_set_si(bset
->eq
[i
][pos
], 0);
12616 isl_seq_combine(bset
->eq
[i
], subs
->v
->el
[0], bset
->eq
[i
],
12617 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12620 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
12621 if (isl_int_is_zero(bset
->ineq
[i
][pos
]))
12623 isl_int_set(v
, bset
->ineq
[i
][pos
]);
12624 isl_int_set_si(bset
->ineq
[i
][pos
], 0);
12625 isl_seq_combine(bset
->ineq
[i
], subs
->v
->el
[0], bset
->ineq
[i
],
12626 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12629 for (i
= 0; i
< bset
->n_div
; ++i
) {
12630 if (isl_int_is_zero(bset
->div
[i
][1 + pos
]))
12632 isl_int_set(v
, bset
->div
[i
][1 + pos
]);
12633 isl_int_set_si(bset
->div
[i
][1 + pos
], 0);
12634 isl_seq_combine(bset
->div
[i
] + 1,
12635 subs
->v
->el
[0], bset
->div
[i
] + 1,
12636 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12637 isl_int_mul(bset
->div
[i
][0], bset
->div
[i
][0], subs
->v
->el
[0]);
12642 bset
= isl_basic_set_simplify(bset
);
12643 return isl_basic_set_finalize(bset
);
12645 isl_basic_set_free(bset
);
12649 /* Plug in "subs" for dimension "type", "pos" of "set".
12651 __isl_give isl_set
*isl_set_substitute(__isl_take isl_set
*set
,
12652 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
12656 if (set
&& isl_set_plain_is_empty(set
))
12659 set
= isl_set_cow(set
);
12663 for (i
= set
->n
- 1; i
>= 0; --i
) {
12664 set
->p
[i
] = isl_basic_set_substitute(set
->p
[i
], type
, pos
, subs
);
12665 if (remove_if_empty(set
, i
) < 0)
12675 /* Check if the range of "ma" is compatible with the domain or range
12676 * (depending on "type") of "bmap".
12677 * Return -1 if anything is wrong.
12679 static int check_basic_map_compatible_range_multi_aff(
12680 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type type
,
12681 __isl_keep isl_multi_aff
*ma
)
12684 isl_space
*ma_space
;
12686 ma_space
= isl_multi_aff_get_space(ma
);
12688 m
= isl_space_match(bmap
->dim
, isl_dim_param
, ma_space
, isl_dim_param
);
12692 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12693 "parameters don't match", goto error
);
12694 m
= isl_space_tuple_is_equal(bmap
->dim
, type
, ma_space
, isl_dim_out
);
12698 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12699 "spaces don't match", goto error
);
12701 isl_space_free(ma_space
);
12704 isl_space_free(ma_space
);
12708 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12709 * coefficients before the transformed range of dimensions,
12710 * the "n_after" coefficients after the transformed range of dimensions
12711 * and the coefficients of the other divs in "bmap".
12713 static int set_ma_divs(__isl_keep isl_basic_map
*bmap
,
12714 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
, int n_div
)
12719 isl_local_space
*ls
;
12724 ls
= isl_aff_get_domain_local_space(ma
->p
[0]);
12728 n_param
= isl_local_space_dim(ls
, isl_dim_param
);
12729 n_set
= isl_local_space_dim(ls
, isl_dim_set
);
12730 for (i
= 0; i
< n_div
; ++i
) {
12731 int o_bmap
= 0, o_ls
= 0;
12733 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], 1 + 1 + n_param
);
12734 o_bmap
+= 1 + 1 + n_param
;
12735 o_ls
+= 1 + 1 + n_param
;
12736 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_before
);
12737 o_bmap
+= n_before
;
12738 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
12739 ls
->div
->row
[i
] + o_ls
, n_set
);
12742 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_after
);
12744 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
12745 ls
->div
->row
[i
] + o_ls
, n_div
);
12748 isl_seq_clr(bmap
->div
[i
] + o_bmap
, bmap
->n_div
- n_div
);
12749 if (isl_basic_set_add_div_constraints(bmap
, i
) < 0)
12753 isl_local_space_free(ls
);
12756 isl_local_space_free(ls
);
12760 /* How many stride constraints does "ma" enforce?
12761 * That is, how many of the affine expressions have a denominator
12762 * different from one?
12764 static int multi_aff_strides(__isl_keep isl_multi_aff
*ma
)
12769 for (i
= 0; i
< ma
->n
; ++i
)
12770 if (!isl_int_is_one(ma
->p
[i
]->v
->el
[0]))
12776 /* For each affine expression in ma of the form
12778 * x_i = (f_i y + h_i)/m_i
12780 * with m_i different from one, add a constraint to "bmap"
12783 * f_i y + h_i = m_i alpha_i
12785 * with alpha_i an additional existentially quantified variable.
12787 * The input variables of "ma" correspond to a subset of the variables
12788 * of "bmap". There are "n_before" variables in "bmap" before this
12789 * subset and "n_after" variables after this subset.
12790 * The integer divisions of the affine expressions in "ma" are assumed
12791 * to have been aligned. There are "n_div_ma" of them and
12792 * they appear first in "bmap", straight after the "n_after" variables.
12794 static __isl_give isl_basic_map
*add_ma_strides(
12795 __isl_take isl_basic_map
*bmap
, __isl_keep isl_multi_aff
*ma
,
12796 int n_before
, int n_after
, int n_div_ma
)
12804 total
= isl_basic_map_total_dim(bmap
);
12805 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
12806 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
12807 for (i
= 0; i
< ma
->n
; ++i
) {
12808 int o_bmap
= 0, o_ma
= 1;
12810 if (isl_int_is_one(ma
->p
[i
]->v
->el
[0]))
12812 div
= isl_basic_map_alloc_div(bmap
);
12813 k
= isl_basic_map_alloc_equality(bmap
);
12814 if (div
< 0 || k
< 0)
12816 isl_int_set_si(bmap
->div
[div
][0], 0);
12817 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12818 ma
->p
[i
]->v
->el
+ o_ma
, 1 + n_param
);
12819 o_bmap
+= 1 + n_param
;
12820 o_ma
+= 1 + n_param
;
12821 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_before
);
12822 o_bmap
+= n_before
;
12823 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12824 ma
->p
[i
]->v
->el
+ o_ma
, n_in
);
12827 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_after
);
12829 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12830 ma
->p
[i
]->v
->el
+ o_ma
, n_div_ma
);
12831 o_bmap
+= n_div_ma
;
12833 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, 1 + total
- o_bmap
);
12834 isl_int_neg(bmap
->eq
[k
][1 + total
], ma
->p
[i
]->v
->el
[0]);
12840 isl_basic_map_free(bmap
);
12844 /* Replace the domain or range space (depending on "type) of "space" by "set".
12846 static __isl_give isl_space
*isl_space_set(__isl_take isl_space
*space
,
12847 enum isl_dim_type type
, __isl_take isl_space
*set
)
12849 if (type
== isl_dim_in
) {
12850 space
= isl_space_range(space
);
12851 space
= isl_space_map_from_domain_and_range(set
, space
);
12853 space
= isl_space_domain(space
);
12854 space
= isl_space_map_from_domain_and_range(space
, set
);
12860 /* Compute the preimage of the domain or range (depending on "type")
12861 * of "bmap" under the function represented by "ma".
12862 * In other words, plug in "ma" in the domain or range of "bmap".
12863 * The result is a basic map that lives in the same space as "bmap"
12864 * except that the domain or range has been replaced by
12865 * the domain space of "ma".
12867 * If bmap is represented by
12869 * A(p) + S u + B x + T v + C(divs) >= 0,
12871 * where u and x are input and output dimensions if type == isl_dim_out
12872 * while x and v are input and output dimensions if type == isl_dim_in,
12873 * and ma is represented by
12875 * x = D(p) + F(y) + G(divs')
12877 * then the result is
12879 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12881 * The divs in the input set are similarly adjusted.
12884 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12888 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
12889 * B_i G(divs') + c_i(divs))/n_i)
12891 * If bmap is not a rational map and if F(y) involves any denominators
12893 * x_i = (f_i y + h_i)/m_i
12895 * then additional constraints are added to ensure that we only
12896 * map back integer points. That is we enforce
12898 * f_i y + h_i = m_i alpha_i
12900 * with alpha_i an additional existentially quantified variable.
12902 * We first copy over the divs from "ma".
12903 * Then we add the modified constraints and divs from "bmap".
12904 * Finally, we add the stride constraints, if needed.
12906 __isl_give isl_basic_map
*isl_basic_map_preimage_multi_aff(
12907 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
12908 __isl_take isl_multi_aff
*ma
)
12912 isl_basic_map
*res
= NULL
;
12913 int n_before
, n_after
, n_div_bmap
, n_div_ma
;
12914 isl_int f
, c1
, c2
, g
;
12915 int rational
, strides
;
12922 ma
= isl_multi_aff_align_divs(ma
);
12925 if (check_basic_map_compatible_range_multi_aff(bmap
, type
, ma
) < 0)
12928 if (type
== isl_dim_in
) {
12930 n_after
= isl_basic_map_dim(bmap
, isl_dim_out
);
12932 n_before
= isl_basic_map_dim(bmap
, isl_dim_in
);
12935 n_div_bmap
= isl_basic_map_dim(bmap
, isl_dim_div
);
12936 n_div_ma
= ma
->n
? isl_aff_dim(ma
->p
[0], isl_dim_div
) : 0;
12938 space
= isl_multi_aff_get_domain_space(ma
);
12939 space
= isl_space_set(isl_basic_map_get_space(bmap
), type
, space
);
12940 rational
= isl_basic_map_is_rational(bmap
);
12941 strides
= rational
? 0 : multi_aff_strides(ma
);
12942 res
= isl_basic_map_alloc_space(space
, n_div_ma
+ n_div_bmap
+ strides
,
12943 bmap
->n_eq
+ strides
, bmap
->n_ineq
+ 2 * n_div_ma
);
12945 res
= isl_basic_map_set_rational(res
);
12947 for (i
= 0; i
< n_div_ma
+ n_div_bmap
; ++i
)
12948 if (isl_basic_map_alloc_div(res
) < 0)
12951 if (set_ma_divs(res
, ma
, n_before
, n_after
, n_div_ma
) < 0)
12954 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
12955 k
= isl_basic_map_alloc_equality(res
);
12958 isl_seq_preimage(res
->eq
[k
], bmap
->eq
[i
], ma
, n_before
,
12959 n_after
, n_div_ma
, n_div_bmap
, f
, c1
, c2
, g
, 0);
12962 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
12963 k
= isl_basic_map_alloc_inequality(res
);
12966 isl_seq_preimage(res
->ineq
[k
], bmap
->ineq
[i
], ma
, n_before
,
12967 n_after
, n_div_ma
, n_div_bmap
, f
, c1
, c2
, g
, 0);
12970 for (i
= 0; i
< bmap
->n_div
; ++i
) {
12971 if (isl_int_is_zero(bmap
->div
[i
][0])) {
12972 isl_int_set_si(res
->div
[n_div_ma
+ i
][0], 0);
12975 isl_seq_preimage(res
->div
[n_div_ma
+ i
], bmap
->div
[i
], ma
,
12976 n_before
, n_after
, n_div_ma
, n_div_bmap
,
12981 res
= add_ma_strides(res
, ma
, n_before
, n_after
, n_div_ma
);
12987 isl_basic_map_free(bmap
);
12988 isl_multi_aff_free(ma
);
12989 res
= isl_basic_set_simplify(res
);
12990 return isl_basic_map_finalize(res
);
12996 isl_basic_map_free(bmap
);
12997 isl_multi_aff_free(ma
);
12998 isl_basic_map_free(res
);
13002 /* Compute the preimage of "bset" under the function represented by "ma".
13003 * In other words, plug in "ma" in "bset". The result is a basic set
13004 * that lives in the domain space of "ma".
13006 __isl_give isl_basic_set
*isl_basic_set_preimage_multi_aff(
13007 __isl_take isl_basic_set
*bset
, __isl_take isl_multi_aff
*ma
)
13009 return isl_basic_map_preimage_multi_aff(bset
, isl_dim_set
, ma
);
13012 /* Compute the preimage of the domain of "bmap" under the function
13013 * represented by "ma".
13014 * In other words, plug in "ma" in the domain of "bmap".
13015 * The result is a basic map that lives in the same space as "bmap"
13016 * except that the domain has been replaced by the domain space of "ma".
13018 __isl_give isl_basic_map
*isl_basic_map_preimage_domain_multi_aff(
13019 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
13021 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_in
, ma
);
13024 /* Compute the preimage of the range of "bmap" under the function
13025 * represented by "ma".
13026 * In other words, plug in "ma" in the range of "bmap".
13027 * The result is a basic map that lives in the same space as "bmap"
13028 * except that the range has been replaced by the domain space of "ma".
13030 __isl_give isl_basic_map
*isl_basic_map_preimage_range_multi_aff(
13031 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
13033 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_out
, ma
);
13036 /* Check if the range of "ma" is compatible with the domain or range
13037 * (depending on "type") of "map".
13038 * Return -1 if anything is wrong.
13040 static int check_map_compatible_range_multi_aff(
13041 __isl_keep isl_map
*map
, enum isl_dim_type type
,
13042 __isl_keep isl_multi_aff
*ma
)
13045 isl_space
*ma_space
;
13047 ma_space
= isl_multi_aff_get_space(ma
);
13048 m
= isl_space_tuple_is_equal(map
->dim
, type
, ma_space
, isl_dim_out
);
13049 isl_space_free(ma_space
);
13051 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
13052 "spaces don't match", return -1);
13056 /* Compute the preimage of the domain or range (depending on "type")
13057 * of "map" under the function represented by "ma".
13058 * In other words, plug in "ma" in the domain or range of "map".
13059 * The result is a map that lives in the same space as "map"
13060 * except that the domain or range has been replaced by
13061 * the domain space of "ma".
13063 * The parameters are assumed to have been aligned.
13065 static __isl_give isl_map
*map_preimage_multi_aff(__isl_take isl_map
*map
,
13066 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
13071 map
= isl_map_cow(map
);
13072 ma
= isl_multi_aff_align_divs(ma
);
13075 if (check_map_compatible_range_multi_aff(map
, type
, ma
) < 0)
13078 for (i
= 0; i
< map
->n
; ++i
) {
13079 map
->p
[i
] = isl_basic_map_preimage_multi_aff(map
->p
[i
], type
,
13080 isl_multi_aff_copy(ma
));
13085 space
= isl_multi_aff_get_domain_space(ma
);
13086 space
= isl_space_set(isl_map_get_space(map
), type
, space
);
13088 isl_space_free(map
->dim
);
13093 isl_multi_aff_free(ma
);
13095 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
13096 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
13099 isl_multi_aff_free(ma
);
13104 /* Compute the preimage of the domain or range (depending on "type")
13105 * of "map" under the function represented by "ma".
13106 * In other words, plug in "ma" in the domain or range of "map".
13107 * The result is a map that lives in the same space as "map"
13108 * except that the domain or range has been replaced by
13109 * the domain space of "ma".
13111 __isl_give isl_map
*isl_map_preimage_multi_aff(__isl_take isl_map
*map
,
13112 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
13117 if (isl_space_match(map
->dim
, isl_dim_param
, ma
->space
, isl_dim_param
))
13118 return map_preimage_multi_aff(map
, type
, ma
);
13120 if (!isl_space_has_named_params(map
->dim
) ||
13121 !isl_space_has_named_params(ma
->space
))
13122 isl_die(map
->ctx
, isl_error_invalid
,
13123 "unaligned unnamed parameters", goto error
);
13124 map
= isl_map_align_params(map
, isl_multi_aff_get_space(ma
));
13125 ma
= isl_multi_aff_align_params(ma
, isl_map_get_space(map
));
13127 return map_preimage_multi_aff(map
, type
, ma
);
13129 isl_multi_aff_free(ma
);
13130 return isl_map_free(map
);
13133 /* Compute the preimage of "set" under the function represented by "ma".
13134 * In other words, plug in "ma" in "set". The result is a set
13135 * that lives in the domain space of "ma".
13137 __isl_give isl_set
*isl_set_preimage_multi_aff(__isl_take isl_set
*set
,
13138 __isl_take isl_multi_aff
*ma
)
13140 return isl_map_preimage_multi_aff(set
, isl_dim_set
, ma
);
13143 /* Compute the preimage of the domain of "map" under the function
13144 * represented by "ma".
13145 * In other words, plug in "ma" in the domain of "map".
13146 * The result is a map that lives in the same space as "map"
13147 * except that the domain has been replaced by the domain space of "ma".
13149 __isl_give isl_map
*isl_map_preimage_domain_multi_aff(__isl_take isl_map
*map
,
13150 __isl_take isl_multi_aff
*ma
)
13152 return isl_map_preimage_multi_aff(map
, isl_dim_in
, ma
);
13155 /* Compute the preimage of the range of "map" under the function
13156 * represented by "ma".
13157 * In other words, plug in "ma" in the range of "map".
13158 * The result is a map that lives in the same space as "map"
13159 * except that the range has been replaced by the domain space of "ma".
13161 __isl_give isl_map
*isl_map_preimage_range_multi_aff(__isl_take isl_map
*map
,
13162 __isl_take isl_multi_aff
*ma
)
13164 return isl_map_preimage_multi_aff(map
, isl_dim_out
, ma
);
13167 /* Compute the preimage of "map" under the function represented by "pma".
13168 * In other words, plug in "pma" in the domain or range of "map".
13169 * The result is a map that lives in the same space as "map",
13170 * except that the space of type "type" has been replaced by
13171 * the domain space of "pma".
13173 * The parameters of "map" and "pma" are assumed to have been aligned.
13175 static __isl_give isl_map
*isl_map_preimage_pw_multi_aff_aligned(
13176 __isl_take isl_map
*map
, enum isl_dim_type type
,
13177 __isl_take isl_pw_multi_aff
*pma
)
13186 isl_pw_multi_aff_free(pma
);
13187 res
= isl_map_empty(isl_map_get_space(map
));
13192 res
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
13193 isl_multi_aff_copy(pma
->p
[0].maff
));
13194 if (type
== isl_dim_in
)
13195 res
= isl_map_intersect_domain(res
,
13196 isl_map_copy(pma
->p
[0].set
));
13198 res
= isl_map_intersect_range(res
,
13199 isl_map_copy(pma
->p
[0].set
));
13201 for (i
= 1; i
< pma
->n
; ++i
) {
13204 res_i
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
13205 isl_multi_aff_copy(pma
->p
[i
].maff
));
13206 if (type
== isl_dim_in
)
13207 res_i
= isl_map_intersect_domain(res_i
,
13208 isl_map_copy(pma
->p
[i
].set
));
13210 res_i
= isl_map_intersect_range(res_i
,
13211 isl_map_copy(pma
->p
[i
].set
));
13212 res
= isl_map_union(res
, res_i
);
13215 isl_pw_multi_aff_free(pma
);
13219 isl_pw_multi_aff_free(pma
);
13224 /* Compute the preimage of "map" under the function represented by "pma".
13225 * In other words, plug in "pma" in the domain or range of "map".
13226 * The result is a map that lives in the same space as "map",
13227 * except that the space of type "type" has been replaced by
13228 * the domain space of "pma".
13230 __isl_give isl_map
*isl_map_preimage_pw_multi_aff(__isl_take isl_map
*map
,
13231 enum isl_dim_type type
, __isl_take isl_pw_multi_aff
*pma
)
13236 if (isl_space_match(map
->dim
, isl_dim_param
, pma
->dim
, isl_dim_param
))
13237 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
13239 if (!isl_space_has_named_params(map
->dim
) ||
13240 !isl_space_has_named_params(pma
->dim
))
13241 isl_die(map
->ctx
, isl_error_invalid
,
13242 "unaligned unnamed parameters", goto error
);
13243 map
= isl_map_align_params(map
, isl_pw_multi_aff_get_space(pma
));
13244 pma
= isl_pw_multi_aff_align_params(pma
, isl_map_get_space(map
));
13246 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
13248 isl_pw_multi_aff_free(pma
);
13249 return isl_map_free(map
);
13252 /* Compute the preimage of "set" under the function represented by "pma".
13253 * In other words, plug in "pma" in "set". The result is a set
13254 * that lives in the domain space of "pma".
13256 __isl_give isl_set
*isl_set_preimage_pw_multi_aff(__isl_take isl_set
*set
,
13257 __isl_take isl_pw_multi_aff
*pma
)
13259 return isl_map_preimage_pw_multi_aff(set
, isl_dim_set
, pma
);
13262 /* Compute the preimage of the domain of "map" under the function
13263 * represented by "pma".
13264 * In other words, plug in "pma" in the domain of "map".
13265 * The result is a map that lives in the same space as "map",
13266 * except that domain space has been replaced by the domain space of "pma".
13268 __isl_give isl_map
*isl_map_preimage_domain_pw_multi_aff(
13269 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
13271 return isl_map_preimage_pw_multi_aff(map
, isl_dim_in
, pma
);
13274 /* Compute the preimage of the range of "map" under the function
13275 * represented by "pma".
13276 * In other words, plug in "pma" in the range of "map".
13277 * The result is a map that lives in the same space as "map",
13278 * except that range space has been replaced by the domain space of "pma".
13280 __isl_give isl_map
*isl_map_preimage_range_pw_multi_aff(
13281 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
13283 return isl_map_preimage_pw_multi_aff(map
, isl_dim_out
, pma
);
13286 /* Compute the preimage of "map" under the function represented by "mpa".
13287 * In other words, plug in "mpa" in the domain or range of "map".
13288 * The result is a map that lives in the same space as "map",
13289 * except that the space of type "type" has been replaced by
13290 * the domain space of "mpa".
13292 * If the map does not involve any constraints that refer to the
13293 * dimensions of the substituted space, then the only possible
13294 * effect of "mpa" on the map is to map the space to a different space.
13295 * We create a separate isl_multi_aff to effectuate this change
13296 * in order to avoid spurious splitting of the map along the pieces
13299 __isl_give isl_map
*isl_map_preimage_multi_pw_aff(__isl_take isl_map
*map
,
13300 enum isl_dim_type type
, __isl_take isl_multi_pw_aff
*mpa
)
13303 isl_pw_multi_aff
*pma
;
13308 n
= isl_map_dim(map
, type
);
13309 if (!isl_map_involves_dims(map
, type
, 0, n
)) {
13313 space
= isl_multi_pw_aff_get_space(mpa
);
13314 isl_multi_pw_aff_free(mpa
);
13315 ma
= isl_multi_aff_zero(space
);
13316 return isl_map_preimage_multi_aff(map
, type
, ma
);
13319 pma
= isl_pw_multi_aff_from_multi_pw_aff(mpa
);
13320 return isl_map_preimage_pw_multi_aff(map
, type
, pma
);
13323 isl_multi_pw_aff_free(mpa
);
13327 /* Compute the preimage of "map" under the function represented by "mpa".
13328 * In other words, plug in "mpa" in the domain "map".
13329 * The result is a map that lives in the same space as "map",
13330 * except that domain space has been replaced by the domain space of "mpa".
13332 __isl_give isl_map
*isl_map_preimage_domain_multi_pw_aff(
13333 __isl_take isl_map
*map
, __isl_take isl_multi_pw_aff
*mpa
)
13335 return isl_map_preimage_multi_pw_aff(map
, isl_dim_in
, mpa
);
13338 /* Compute the preimage of "set" by the function represented by "mpa".
13339 * In other words, plug in "mpa" in "set".
13341 __isl_give isl_set
*isl_set_preimage_multi_pw_aff(__isl_take isl_set
*set
,
13342 __isl_take isl_multi_pw_aff
*mpa
)
13344 return isl_map_preimage_multi_pw_aff(set
, isl_dim_set
, mpa
);
13347 /* Are the "n" "coefficients" starting at "first" of the integer division
13348 * expressions at position "pos1" in "bmap1" and "pos2" in "bmap2" equal
13350 * The "coefficient" at position 0 is the denominator.
13351 * The "coefficient" at position 1 is the constant term.
13353 isl_bool
isl_basic_map_equal_div_expr_part(__isl_keep isl_basic_map
*bmap1
,
13354 int pos1
, __isl_keep isl_basic_map
*bmap2
, int pos2
,
13355 unsigned first
, unsigned n
)
13357 if (isl_basic_map_check_range(bmap1
, isl_dim_div
, pos1
, 1) < 0)
13358 return isl_bool_error
;
13359 if (isl_basic_map_check_range(bmap2
, isl_dim_div
, pos2
, 1) < 0)
13360 return isl_bool_error
;
13361 return isl_seq_eq(bmap1
->div
[pos1
] + first
,
13362 bmap2
->div
[pos2
] + first
, n
);
13365 /* Are the integer division expressions at position "pos1" in "bmap1" and
13366 * "pos2" in "bmap2" equal to each other, except that the constant terms
13369 isl_bool
isl_basic_map_equal_div_expr_except_constant(
13370 __isl_keep isl_basic_map
*bmap1
, int pos1
,
13371 __isl_keep isl_basic_map
*bmap2
, int pos2
)
13376 if (!bmap1
|| !bmap2
)
13377 return isl_bool_error
;
13378 total
= isl_basic_map_total_dim(bmap1
);
13379 if (total
!= isl_basic_map_total_dim(bmap2
))
13380 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
13381 "incomparable div expressions", return isl_bool_error
);
13382 equal
= isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
13384 if (equal
< 0 || !equal
)
13386 equal
= isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
13388 if (equal
< 0 || equal
)
13389 return isl_bool_not(equal
);
13390 return isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
13394 /* Replace the numerator of the constant term of the integer division
13395 * expression at position "div" in "bmap" by "value".
13396 * The caller guarantees that this does not change the meaning
13399 __isl_give isl_basic_map
*isl_basic_map_set_div_expr_constant_num_si_inplace(
13400 __isl_take isl_basic_map
*bmap
, int div
, int value
)
13402 if (isl_basic_map_check_range(bmap
, isl_dim_div
, div
, 1) < 0)
13403 return isl_basic_map_free(bmap
);
13405 isl_int_set_si(bmap
->div
[div
][1], value
);
13410 /* Is the point "inner" internal to inequality constraint "ineq"
13412 * The point is considered to be internal to the inequality constraint,
13413 * if it strictly lies on the positive side of the inequality constraint,
13414 * or if it lies on the constraint and the constraint is lexico-positive.
13416 static isl_bool
is_internal(__isl_keep isl_vec
*inner
,
13417 __isl_keep isl_basic_set
*bset
, int ineq
)
13423 if (!inner
|| !bset
)
13424 return isl_bool_error
;
13426 ctx
= isl_basic_set_get_ctx(bset
);
13427 isl_seq_inner_product(inner
->el
, bset
->ineq
[ineq
], inner
->size
,
13428 &ctx
->normalize_gcd
);
13429 if (!isl_int_is_zero(ctx
->normalize_gcd
))
13430 return isl_int_is_nonneg(ctx
->normalize_gcd
);
13432 total
= isl_basic_set_dim(bset
, isl_dim_all
);
13433 pos
= isl_seq_first_non_zero(bset
->ineq
[ineq
] + 1, total
);
13434 return isl_int_is_pos(bset
->ineq
[ineq
][1 + pos
]);
13437 /* Tighten the inequality constraints of "bset" that are outward with respect
13438 * to the point "vec".
13439 * That is, tighten the constraints that are not satisfied by "vec".
13441 * "vec" is a point internal to some superset S of "bset" that is used
13442 * to make the subsets of S disjoint, by tightening one half of the constraints
13443 * that separate two subsets. In particular, the constraints of S
13444 * are all satisfied by "vec" and should not be tightened.
13445 * Of the internal constraints, those that have "vec" on the outside
13446 * are tightened. The shared facet is included in the adjacent subset
13447 * with the opposite constraint.
13448 * For constraints that saturate "vec", this criterion cannot be used
13449 * to determine which of the two sides should be tightened.
13450 * Instead, the sign of the first non-zero coefficient is used
13451 * to make this choice. Note that this second criterion is never used
13452 * on the constraints of S since "vec" is interior to "S".
13454 __isl_give isl_basic_set
*isl_basic_set_tighten_outward(
13455 __isl_take isl_basic_set
*bset
, __isl_keep isl_vec
*vec
)
13459 bset
= isl_basic_set_cow(bset
);
13462 for (j
= 0; j
< bset
->n_ineq
; ++j
) {
13465 internal
= is_internal(vec
, bset
, j
);
13467 return isl_basic_set_free(bset
);
13470 isl_int_sub_ui(bset
->ineq
[j
][0], bset
->ineq
[j
][0], 1);