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 isl_bool
isl_basic_map_compatible_range(__isl_keep isl_basic_map
*bmap
,
243 __isl_keep isl_basic_set
*bset
)
247 return isl_bool_error
;
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 isl_bool
isl_basic_set_is_params(__isl_keep isl_basic_set
*bset
)
953 return isl_bool_error
;
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 if (isl_basic_map_free_div(bmap
, bmap
->n_div
) < 0)
1884 return isl_basic_map_free(bmap
);
1885 isl_basic_map_free_inequality(bmap
, bmap
->n_ineq
);
1887 isl_basic_map_free_equality(bmap
, bmap
->n_eq
-1);
1889 i
= isl_basic_map_alloc_equality(bmap
);
1893 isl_int_set_si(bmap
->eq
[i
][0], 1);
1894 isl_seq_clr(bmap
->eq
[i
]+1, total
);
1895 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
1896 isl_vec_free(bmap
->sample
);
1897 bmap
->sample
= NULL
;
1898 return isl_basic_map_finalize(bmap
);
1900 isl_basic_map_free(bmap
);
1904 struct isl_basic_set
*isl_basic_set_set_to_empty(struct isl_basic_set
*bset
)
1906 return bset_from_bmap(isl_basic_map_set_to_empty(bset_to_bmap(bset
)));
1909 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
1912 static void swap_div(__isl_keep isl_basic_map
*bmap
, int a
, int b
)
1914 isl_int
*t
= bmap
->div
[a
];
1915 bmap
->div
[a
] = bmap
->div
[b
];
1919 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
1920 * div definitions accordingly.
1922 void isl_basic_map_swap_div(struct isl_basic_map
*bmap
, int a
, int b
)
1925 unsigned off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
1927 swap_div(bmap
, a
, b
);
1929 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1930 isl_int_swap(bmap
->eq
[i
][1+off
+a
], bmap
->eq
[i
][1+off
+b
]);
1932 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1933 isl_int_swap(bmap
->ineq
[i
][1+off
+a
], bmap
->ineq
[i
][1+off
+b
]);
1935 for (i
= 0; i
< bmap
->n_div
; ++i
)
1936 isl_int_swap(bmap
->div
[i
][1+1+off
+a
], bmap
->div
[i
][1+1+off
+b
]);
1937 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1940 /* Swap divs "a" and "b" in "bset" and adjust the constraints and
1941 * div definitions accordingly.
1943 void isl_basic_set_swap_div(__isl_keep isl_basic_set
*bset
, int a
, int b
)
1945 isl_basic_map_swap_div(bset
, a
, b
);
1948 /* Eliminate the specified n dimensions starting at first from the
1949 * constraints, without removing the dimensions from the space.
1950 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1952 __isl_give isl_map
*isl_map_eliminate(__isl_take isl_map
*map
,
1953 enum isl_dim_type type
, unsigned first
, unsigned n
)
1962 if (first
+ n
> isl_map_dim(map
, type
) || first
+ n
< first
)
1963 isl_die(map
->ctx
, isl_error_invalid
,
1964 "index out of bounds", goto error
);
1966 map
= isl_map_cow(map
);
1970 for (i
= 0; i
< map
->n
; ++i
) {
1971 map
->p
[i
] = isl_basic_map_eliminate(map
->p
[i
], type
, first
, n
);
1981 /* Eliminate the specified n dimensions starting at first from the
1982 * constraints, without removing the dimensions from the space.
1983 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1985 __isl_give isl_set
*isl_set_eliminate(__isl_take isl_set
*set
,
1986 enum isl_dim_type type
, unsigned first
, unsigned n
)
1988 return set_from_map(isl_map_eliminate(set_to_map(set
), type
, first
, n
));
1991 /* Eliminate the specified n dimensions starting at first from the
1992 * constraints, without removing the dimensions from the space.
1993 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1995 __isl_give isl_set
*isl_set_eliminate_dims(__isl_take isl_set
*set
,
1996 unsigned first
, unsigned n
)
1998 return isl_set_eliminate(set
, isl_dim_set
, first
, n
);
2001 __isl_give isl_basic_map
*isl_basic_map_remove_divs(
2002 __isl_take isl_basic_map
*bmap
)
2006 bmap
= isl_basic_map_eliminate_vars(bmap
,
2007 isl_space_dim(bmap
->dim
, isl_dim_all
), bmap
->n_div
);
2011 return isl_basic_map_finalize(bmap
);
2014 __isl_give isl_basic_set
*isl_basic_set_remove_divs(
2015 __isl_take isl_basic_set
*bset
)
2017 return bset_from_bmap(isl_basic_map_remove_divs(bset_to_bmap(bset
)));
2020 __isl_give isl_map
*isl_map_remove_divs(__isl_take isl_map
*map
)
2029 map
= isl_map_cow(map
);
2033 for (i
= 0; i
< map
->n
; ++i
) {
2034 map
->p
[i
] = isl_basic_map_remove_divs(map
->p
[i
]);
2044 __isl_give isl_set
*isl_set_remove_divs(__isl_take isl_set
*set
)
2046 return isl_map_remove_divs(set
);
2049 struct isl_basic_map
*isl_basic_map_remove_dims(struct isl_basic_map
*bmap
,
2050 enum isl_dim_type type
, unsigned first
, unsigned n
)
2052 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2053 return isl_basic_map_free(bmap
);
2054 if (n
== 0 && !isl_space_is_named_or_nested(bmap
->dim
, type
))
2056 bmap
= isl_basic_map_eliminate_vars(bmap
,
2057 isl_basic_map_offset(bmap
, type
) - 1 + first
, n
);
2060 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
) && type
== isl_dim_div
)
2062 bmap
= isl_basic_map_drop(bmap
, type
, first
, n
);
2066 /* Return true if the definition of the given div (recursively) involves
2067 * any of the given variables.
2069 static isl_bool
div_involves_vars(__isl_keep isl_basic_map
*bmap
, int div
,
2070 unsigned first
, unsigned n
)
2073 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
2075 if (isl_int_is_zero(bmap
->div
[div
][0]))
2076 return isl_bool_false
;
2077 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + first
, n
) >= 0)
2078 return isl_bool_true
;
2080 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2083 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
2085 involves
= div_involves_vars(bmap
, i
, first
, n
);
2086 if (involves
< 0 || involves
)
2090 return isl_bool_false
;
2093 /* Try and add a lower and/or upper bound on "div" to "bmap"
2094 * based on inequality "i".
2095 * "total" is the total number of variables (excluding the divs).
2096 * "v" is a temporary object that can be used during the calculations.
2097 * If "lb" is set, then a lower bound should be constructed.
2098 * If "ub" is set, then an upper bound should be constructed.
2100 * The calling function has already checked that the inequality does not
2101 * reference "div", but we still need to check that the inequality is
2102 * of the right form. We'll consider the case where we want to construct
2103 * a lower bound. The construction of upper bounds is similar.
2105 * Let "div" be of the form
2107 * q = floor((a + f(x))/d)
2109 * We essentially check if constraint "i" is of the form
2113 * so that we can use it to derive a lower bound on "div".
2114 * However, we allow a slightly more general form
2118 * with the condition that the coefficients of g(x) - f(x) are all
2120 * Rewriting this constraint as
2124 * adding a + f(x) to both sides and dividing by d, we obtain
2126 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
2128 * Taking the floor on both sides, we obtain
2130 * q >= floor((a-b)/d) + (f(x)-g(x))/d
2134 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
2136 * In the case of an upper bound, we construct the constraint
2138 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
2141 static __isl_give isl_basic_map
*insert_bounds_on_div_from_ineq(
2142 __isl_take isl_basic_map
*bmap
, int div
, int i
,
2143 unsigned total
, isl_int v
, int lb
, int ub
)
2147 for (j
= 0; (lb
|| ub
) && j
< total
+ bmap
->n_div
; ++j
) {
2149 isl_int_sub(v
, bmap
->ineq
[i
][1 + j
],
2150 bmap
->div
[div
][1 + 1 + j
]);
2151 lb
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
2154 isl_int_add(v
, bmap
->ineq
[i
][1 + j
],
2155 bmap
->div
[div
][1 + 1 + j
]);
2156 ub
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
2162 bmap
= isl_basic_map_cow(bmap
);
2163 bmap
= isl_basic_map_extend_constraints(bmap
, 0, lb
+ ub
);
2165 int k
= isl_basic_map_alloc_inequality(bmap
);
2168 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
2169 isl_int_sub(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
2170 bmap
->div
[div
][1 + j
]);
2171 isl_int_cdiv_q(bmap
->ineq
[k
][j
],
2172 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
2174 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], 1);
2177 int k
= isl_basic_map_alloc_inequality(bmap
);
2180 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
2181 isl_int_add(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
2182 bmap
->div
[div
][1 + j
]);
2183 isl_int_fdiv_q(bmap
->ineq
[k
][j
],
2184 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
2186 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], -1);
2189 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
2192 isl_basic_map_free(bmap
);
2196 /* This function is called right before "div" is eliminated from "bmap"
2197 * using Fourier-Motzkin.
2198 * Look through the constraints of "bmap" for constraints on the argument
2199 * of the integer division and use them to construct constraints on the
2200 * integer division itself. These constraints can then be combined
2201 * during the Fourier-Motzkin elimination.
2202 * Note that it is only useful to introduce lower bounds on "div"
2203 * if "bmap" already contains upper bounds on "div" as the newly
2204 * introduce lower bounds can then be combined with the pre-existing
2205 * upper bounds. Similarly for upper bounds.
2206 * We therefore first check if "bmap" contains any lower and/or upper bounds
2209 * It is interesting to note that the introduction of these constraints
2210 * can indeed lead to more accurate results, even when compared to
2211 * deriving constraints on the argument of "div" from constraints on "div".
2212 * Consider, for example, the set
2214 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2216 * The second constraint can be rewritten as
2218 * 2 * [(-i-2j+3)/4] + k >= 0
2220 * from which we can derive
2222 * -i - 2j + 3 >= -2k
2228 * Combined with the first constraint, we obtain
2230 * -3 <= 3 + 2k or k >= -3
2232 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2233 * the first constraint, we obtain
2235 * [(i + 2j)/4] >= [-3/4] = -1
2237 * Combining this constraint with the second constraint, we obtain
2241 static __isl_give isl_basic_map
*insert_bounds_on_div(
2242 __isl_take isl_basic_map
*bmap
, int div
)
2245 int check_lb
, check_ub
;
2252 if (isl_int_is_zero(bmap
->div
[div
][0]))
2255 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
2259 for (i
= 0; (!check_lb
|| !check_ub
) && i
< bmap
->n_ineq
; ++i
) {
2260 int s
= isl_int_sgn(bmap
->ineq
[i
][1 + total
+ div
]);
2267 if (!check_lb
&& !check_ub
)
2272 for (i
= 0; bmap
&& i
< bmap
->n_ineq
; ++i
) {
2273 if (!isl_int_is_zero(bmap
->ineq
[i
][1 + total
+ div
]))
2276 bmap
= insert_bounds_on_div_from_ineq(bmap
, div
, i
, total
, v
,
2277 check_lb
, check_ub
);
2285 /* Remove all divs (recursively) involving any of the given dimensions
2286 * in their definitions.
2288 __isl_give isl_basic_map
*isl_basic_map_remove_divs_involving_dims(
2289 __isl_take isl_basic_map
*bmap
,
2290 enum isl_dim_type type
, unsigned first
, unsigned n
)
2294 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2295 return isl_basic_map_free(bmap
);
2296 first
+= isl_basic_map_offset(bmap
, type
);
2298 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2301 involves
= div_involves_vars(bmap
, i
, first
, n
);
2303 return isl_basic_map_free(bmap
);
2306 bmap
= insert_bounds_on_div(bmap
, i
);
2307 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
2316 __isl_give isl_basic_set
*isl_basic_set_remove_divs_involving_dims(
2317 __isl_take isl_basic_set
*bset
,
2318 enum isl_dim_type type
, unsigned first
, unsigned n
)
2320 return isl_basic_map_remove_divs_involving_dims(bset
, type
, first
, n
);
2323 __isl_give isl_map
*isl_map_remove_divs_involving_dims(__isl_take isl_map
*map
,
2324 enum isl_dim_type type
, unsigned first
, unsigned n
)
2333 map
= isl_map_cow(map
);
2337 for (i
= 0; i
< map
->n
; ++i
) {
2338 map
->p
[i
] = isl_basic_map_remove_divs_involving_dims(map
->p
[i
],
2349 __isl_give isl_set
*isl_set_remove_divs_involving_dims(__isl_take isl_set
*set
,
2350 enum isl_dim_type type
, unsigned first
, unsigned n
)
2352 return set_from_map(isl_map_remove_divs_involving_dims(set_to_map(set
),
2356 /* Does the description of "bmap" depend on the specified dimensions?
2357 * We also check whether the dimensions appear in any of the div definitions.
2358 * In principle there is no need for this check. If the dimensions appear
2359 * in a div definition, they also appear in the defining constraints of that
2362 isl_bool
isl_basic_map_involves_dims(__isl_keep isl_basic_map
*bmap
,
2363 enum isl_dim_type type
, unsigned first
, unsigned n
)
2367 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2368 return isl_bool_error
;
2370 first
+= isl_basic_map_offset(bmap
, type
);
2371 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2372 if (isl_seq_first_non_zero(bmap
->eq
[i
] + first
, n
) >= 0)
2373 return isl_bool_true
;
2374 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2375 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + first
, n
) >= 0)
2376 return isl_bool_true
;
2377 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2378 if (isl_int_is_zero(bmap
->div
[i
][0]))
2380 if (isl_seq_first_non_zero(bmap
->div
[i
] + 1 + first
, n
) >= 0)
2381 return isl_bool_true
;
2384 return isl_bool_false
;
2387 isl_bool
isl_map_involves_dims(__isl_keep isl_map
*map
,
2388 enum isl_dim_type type
, unsigned first
, unsigned n
)
2393 return isl_bool_error
;
2395 if (first
+ n
> isl_map_dim(map
, type
))
2396 isl_die(map
->ctx
, isl_error_invalid
,
2397 "index out of bounds", return isl_bool_error
);
2399 for (i
= 0; i
< map
->n
; ++i
) {
2400 isl_bool involves
= isl_basic_map_involves_dims(map
->p
[i
],
2402 if (involves
< 0 || involves
)
2406 return isl_bool_false
;
2409 isl_bool
isl_basic_set_involves_dims(__isl_keep isl_basic_set
*bset
,
2410 enum isl_dim_type type
, unsigned first
, unsigned n
)
2412 return isl_basic_map_involves_dims(bset
, type
, first
, n
);
2415 isl_bool
isl_set_involves_dims(__isl_keep isl_set
*set
,
2416 enum isl_dim_type type
, unsigned first
, unsigned n
)
2418 return isl_map_involves_dims(set
, type
, first
, n
);
2421 /* Drop all constraints in bmap that involve any of the dimensions
2422 * first to first+n-1.
2424 static __isl_give isl_basic_map
*isl_basic_map_drop_constraints_involving(
2425 __isl_take isl_basic_map
*bmap
, unsigned first
, unsigned n
)
2432 bmap
= isl_basic_map_cow(bmap
);
2437 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
) {
2438 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + first
, n
) == -1)
2440 isl_basic_map_drop_equality(bmap
, i
);
2443 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
2444 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + 1 + first
, n
) == -1)
2446 isl_basic_map_drop_inequality(bmap
, i
);
2449 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
2453 /* Drop all constraints in bset that involve any of the dimensions
2454 * first to first+n-1.
2456 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_involving(
2457 __isl_take isl_basic_set
*bset
, unsigned first
, unsigned n
)
2459 return isl_basic_map_drop_constraints_involving(bset
, first
, n
);
2462 /* Drop all constraints in bmap that do not involve any of the dimensions
2463 * first to first + n - 1 of the given type.
2465 __isl_give isl_basic_map
*isl_basic_map_drop_constraints_not_involving_dims(
2466 __isl_take isl_basic_map
*bmap
,
2467 enum isl_dim_type type
, unsigned first
, unsigned n
)
2472 isl_space
*space
= isl_basic_map_get_space(bmap
);
2473 isl_basic_map_free(bmap
);
2474 return isl_basic_map_universe(space
);
2476 bmap
= isl_basic_map_cow(bmap
);
2480 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2481 return isl_basic_map_free(bmap
);
2483 first
+= isl_basic_map_offset(bmap
, type
) - 1;
2485 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
) {
2486 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + first
, n
) != -1)
2488 isl_basic_map_drop_equality(bmap
, i
);
2491 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
2492 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + 1 + first
, n
) != -1)
2494 isl_basic_map_drop_inequality(bmap
, i
);
2497 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
2501 /* Drop all constraints in bset that do not involve any of the dimensions
2502 * first to first + n - 1 of the given type.
2504 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_not_involving_dims(
2505 __isl_take isl_basic_set
*bset
,
2506 enum isl_dim_type type
, unsigned first
, unsigned n
)
2508 return isl_basic_map_drop_constraints_not_involving_dims(bset
,
2512 /* Drop all constraints in bmap that involve any of the dimensions
2513 * first to first + n - 1 of the given type.
2515 __isl_give isl_basic_map
*isl_basic_map_drop_constraints_involving_dims(
2516 __isl_take isl_basic_map
*bmap
,
2517 enum isl_dim_type type
, unsigned first
, unsigned n
)
2524 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2525 return isl_basic_map_free(bmap
);
2527 bmap
= isl_basic_map_remove_divs_involving_dims(bmap
, type
, first
, n
);
2528 first
+= isl_basic_map_offset(bmap
, type
) - 1;
2529 return isl_basic_map_drop_constraints_involving(bmap
, first
, n
);
2532 /* Drop all constraints in bset that involve any of the dimensions
2533 * first to first + n - 1 of the given type.
2535 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_involving_dims(
2536 __isl_take isl_basic_set
*bset
,
2537 enum isl_dim_type type
, unsigned first
, unsigned n
)
2539 return isl_basic_map_drop_constraints_involving_dims(bset
,
2543 /* Drop constraints from "map" by applying "drop" to each basic map.
2545 static __isl_give isl_map
*drop_constraints(__isl_take isl_map
*map
,
2546 enum isl_dim_type type
, unsigned first
, unsigned n
,
2547 __isl_give isl_basic_map
*(*drop
)(__isl_take isl_basic_map
*bmap
,
2548 enum isl_dim_type type
, unsigned first
, unsigned n
))
2556 dim
= isl_map_dim(map
, type
);
2557 if (first
+ n
> dim
|| first
+ n
< first
)
2558 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
2559 "index out of bounds", return isl_map_free(map
));
2561 map
= isl_map_cow(map
);
2565 for (i
= 0; i
< map
->n
; ++i
) {
2566 map
->p
[i
] = drop(map
->p
[i
], type
, first
, n
);
2568 return isl_map_free(map
);
2572 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
2577 /* Drop all constraints in map that involve any of the dimensions
2578 * first to first + n - 1 of the given type.
2580 __isl_give isl_map
*isl_map_drop_constraints_involving_dims(
2581 __isl_take isl_map
*map
,
2582 enum isl_dim_type type
, unsigned first
, unsigned n
)
2586 return drop_constraints(map
, type
, first
, n
,
2587 &isl_basic_map_drop_constraints_involving_dims
);
2590 /* Drop all constraints in "map" that do not involve any of the dimensions
2591 * first to first + n - 1 of the given type.
2593 __isl_give isl_map
*isl_map_drop_constraints_not_involving_dims(
2594 __isl_take isl_map
*map
,
2595 enum isl_dim_type type
, unsigned first
, unsigned n
)
2598 isl_space
*space
= isl_map_get_space(map
);
2600 return isl_map_universe(space
);
2602 return drop_constraints(map
, type
, first
, n
,
2603 &isl_basic_map_drop_constraints_not_involving_dims
);
2606 /* Drop all constraints in set that involve any of the dimensions
2607 * first to first + n - 1 of the given type.
2609 __isl_give isl_set
*isl_set_drop_constraints_involving_dims(
2610 __isl_take isl_set
*set
,
2611 enum isl_dim_type type
, unsigned first
, unsigned n
)
2613 return isl_map_drop_constraints_involving_dims(set
, type
, first
, n
);
2616 /* Drop all constraints in "set" that do not involve any of the dimensions
2617 * first to first + n - 1 of the given type.
2619 __isl_give isl_set
*isl_set_drop_constraints_not_involving_dims(
2620 __isl_take isl_set
*set
,
2621 enum isl_dim_type type
, unsigned first
, unsigned n
)
2623 return isl_map_drop_constraints_not_involving_dims(set
, type
, first
, n
);
2626 /* Does local variable "div" of "bmap" have a complete explicit representation?
2627 * Having a complete explicit representation requires not only
2628 * an explicit representation, but also that all local variables
2629 * that appear in this explicit representation in turn have
2630 * a complete explicit representation.
2632 isl_bool
isl_basic_map_div_is_known(__isl_keep isl_basic_map
*bmap
, int div
)
2635 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
2638 marked
= isl_basic_map_div_is_marked_unknown(bmap
, div
);
2639 if (marked
< 0 || marked
)
2640 return isl_bool_not(marked
);
2642 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2645 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
2647 known
= isl_basic_map_div_is_known(bmap
, i
);
2648 if (known
< 0 || !known
)
2652 return isl_bool_true
;
2655 /* Does local variable "div" of "bset" have a complete explicit representation?
2657 isl_bool
isl_basic_set_div_is_known(__isl_keep isl_basic_set
*bset
, int div
)
2659 return isl_basic_map_div_is_known(bset
, div
);
2662 /* Remove all divs that are unknown or defined in terms of unknown divs.
2664 __isl_give isl_basic_map
*isl_basic_map_remove_unknown_divs(
2665 __isl_take isl_basic_map
*bmap
)
2672 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2673 if (isl_basic_map_div_is_known(bmap
, i
))
2675 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
2684 /* Remove all divs that are unknown or defined in terms of unknown divs.
2686 __isl_give isl_basic_set
*isl_basic_set_remove_unknown_divs(
2687 __isl_take isl_basic_set
*bset
)
2689 return isl_basic_map_remove_unknown_divs(bset
);
2692 __isl_give isl_map
*isl_map_remove_unknown_divs(__isl_take isl_map
*map
)
2701 map
= isl_map_cow(map
);
2705 for (i
= 0; i
< map
->n
; ++i
) {
2706 map
->p
[i
] = isl_basic_map_remove_unknown_divs(map
->p
[i
]);
2716 __isl_give isl_set
*isl_set_remove_unknown_divs(__isl_take isl_set
*set
)
2718 return set_from_map(isl_map_remove_unknown_divs(set_to_map(set
)));
2721 __isl_give isl_basic_set
*isl_basic_set_remove_dims(
2722 __isl_take isl_basic_set
*bset
,
2723 enum isl_dim_type type
, unsigned first
, unsigned n
)
2725 isl_basic_map
*bmap
= bset_to_bmap(bset
);
2726 bmap
= isl_basic_map_remove_dims(bmap
, type
, first
, n
);
2727 return bset_from_bmap(bmap
);
2730 struct isl_map
*isl_map_remove_dims(struct isl_map
*map
,
2731 enum isl_dim_type type
, unsigned first
, unsigned n
)
2738 map
= isl_map_cow(map
);
2741 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
2743 for (i
= 0; i
< map
->n
; ++i
) {
2744 map
->p
[i
] = isl_basic_map_eliminate_vars(map
->p
[i
],
2745 isl_basic_map_offset(map
->p
[i
], type
) - 1 + first
, n
);
2749 map
= isl_map_drop(map
, type
, first
, n
);
2756 __isl_give isl_set
*isl_set_remove_dims(__isl_take isl_set
*bset
,
2757 enum isl_dim_type type
, unsigned first
, unsigned n
)
2759 return set_from_map(isl_map_remove_dims(set_to_map(bset
),
2763 /* Project out n inputs starting at first using Fourier-Motzkin */
2764 struct isl_map
*isl_map_remove_inputs(struct isl_map
*map
,
2765 unsigned first
, unsigned n
)
2767 return isl_map_remove_dims(map
, isl_dim_in
, first
, n
);
2770 static void dump_term(struct isl_basic_map
*bmap
,
2771 isl_int c
, int pos
, FILE *out
)
2774 unsigned in
= isl_basic_map_n_in(bmap
);
2775 unsigned dim
= in
+ isl_basic_map_n_out(bmap
);
2776 unsigned nparam
= isl_basic_map_n_param(bmap
);
2778 isl_int_print(out
, c
, 0);
2780 if (!isl_int_is_one(c
))
2781 isl_int_print(out
, c
, 0);
2782 if (pos
< 1 + nparam
) {
2783 name
= isl_space_get_dim_name(bmap
->dim
,
2784 isl_dim_param
, pos
- 1);
2786 fprintf(out
, "%s", name
);
2788 fprintf(out
, "p%d", pos
- 1);
2789 } else if (pos
< 1 + nparam
+ in
)
2790 fprintf(out
, "i%d", pos
- 1 - nparam
);
2791 else if (pos
< 1 + nparam
+ dim
)
2792 fprintf(out
, "o%d", pos
- 1 - nparam
- in
);
2794 fprintf(out
, "e%d", pos
- 1 - nparam
- dim
);
2798 static void dump_constraint_sign(struct isl_basic_map
*bmap
, isl_int
*c
,
2799 int sign
, FILE *out
)
2803 unsigned len
= 1 + isl_basic_map_total_dim(bmap
);
2807 for (i
= 0, first
= 1; i
< len
; ++i
) {
2808 if (isl_int_sgn(c
[i
]) * sign
<= 0)
2811 fprintf(out
, " + ");
2813 isl_int_abs(v
, c
[i
]);
2814 dump_term(bmap
, v
, i
, out
);
2821 static void dump_constraint(struct isl_basic_map
*bmap
, isl_int
*c
,
2822 const char *op
, FILE *out
, int indent
)
2826 fprintf(out
, "%*s", indent
, "");
2828 dump_constraint_sign(bmap
, c
, 1, out
);
2829 fprintf(out
, " %s ", op
);
2830 dump_constraint_sign(bmap
, c
, -1, out
);
2834 for (i
= bmap
->n_div
; i
< bmap
->extra
; ++i
) {
2835 if (isl_int_is_zero(c
[1+isl_space_dim(bmap
->dim
, isl_dim_all
)+i
]))
2837 fprintf(out
, "%*s", indent
, "");
2838 fprintf(out
, "ERROR: unused div coefficient not zero\n");
2843 static void dump_constraints(struct isl_basic_map
*bmap
,
2844 isl_int
**c
, unsigned n
,
2845 const char *op
, FILE *out
, int indent
)
2849 for (i
= 0; i
< n
; ++i
)
2850 dump_constraint(bmap
, c
[i
], op
, out
, indent
);
2853 static void dump_affine(struct isl_basic_map
*bmap
, isl_int
*exp
, FILE *out
)
2857 unsigned total
= isl_basic_map_total_dim(bmap
);
2859 for (j
= 0; j
< 1 + total
; ++j
) {
2860 if (isl_int_is_zero(exp
[j
]))
2862 if (!first
&& isl_int_is_pos(exp
[j
]))
2864 dump_term(bmap
, exp
[j
], j
, out
);
2869 static void dump(struct isl_basic_map
*bmap
, FILE *out
, int indent
)
2873 dump_constraints(bmap
, bmap
->eq
, bmap
->n_eq
, "=", out
, indent
);
2874 dump_constraints(bmap
, bmap
->ineq
, bmap
->n_ineq
, ">=", out
, indent
);
2876 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2877 fprintf(out
, "%*s", indent
, "");
2878 fprintf(out
, "e%d = [(", i
);
2879 dump_affine(bmap
, bmap
->div
[i
]+1, out
);
2881 isl_int_print(out
, bmap
->div
[i
][0], 0);
2882 fprintf(out
, "]\n");
2886 void isl_basic_set_print_internal(struct isl_basic_set
*bset
,
2887 FILE *out
, int indent
)
2890 fprintf(out
, "null basic set\n");
2894 fprintf(out
, "%*s", indent
, "");
2895 fprintf(out
, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
2896 bset
->ref
, bset
->dim
->nparam
, bset
->dim
->n_out
,
2897 bset
->extra
, bset
->flags
);
2898 dump(bset_to_bmap(bset
), out
, indent
);
2901 void isl_basic_map_print_internal(struct isl_basic_map
*bmap
,
2902 FILE *out
, int indent
)
2905 fprintf(out
, "null basic map\n");
2909 fprintf(out
, "%*s", indent
, "");
2910 fprintf(out
, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
2911 "flags: %x, n_name: %d\n",
2913 bmap
->dim
->nparam
, bmap
->dim
->n_in
, bmap
->dim
->n_out
,
2914 bmap
->extra
, bmap
->flags
, bmap
->dim
->n_id
);
2915 dump(bmap
, out
, indent
);
2918 int isl_inequality_negate(struct isl_basic_map
*bmap
, unsigned pos
)
2923 total
= isl_basic_map_total_dim(bmap
);
2924 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
2925 isl_seq_neg(bmap
->ineq
[pos
], bmap
->ineq
[pos
], 1 + total
);
2926 isl_int_sub_ui(bmap
->ineq
[pos
][0], bmap
->ineq
[pos
][0], 1);
2927 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
2931 __isl_give isl_set
*isl_set_alloc_space(__isl_take isl_space
*space
, int n
,
2936 if (isl_space_dim(space
, isl_dim_in
) != 0)
2937 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
2938 "set cannot have input dimensions", goto error
);
2939 return isl_map_alloc_space(space
, n
, flags
);
2941 isl_space_free(space
);
2945 struct isl_set
*isl_set_alloc(struct isl_ctx
*ctx
,
2946 unsigned nparam
, unsigned dim
, int n
, unsigned flags
)
2948 struct isl_set
*set
;
2951 dims
= isl_space_alloc(ctx
, nparam
, 0, dim
);
2955 set
= isl_set_alloc_space(dims
, n
, flags
);
2959 /* Make sure "map" has room for at least "n" more basic maps.
2961 struct isl_map
*isl_map_grow(struct isl_map
*map
, int n
)
2964 struct isl_map
*grown
= NULL
;
2968 isl_assert(map
->ctx
, n
>= 0, goto error
);
2969 if (map
->n
+ n
<= map
->size
)
2971 grown
= isl_map_alloc_space(isl_map_get_space(map
), map
->n
+ n
, map
->flags
);
2974 for (i
= 0; i
< map
->n
; ++i
) {
2975 grown
->p
[i
] = isl_basic_map_copy(map
->p
[i
]);
2983 isl_map_free(grown
);
2988 /* Make sure "set" has room for at least "n" more basic sets.
2990 struct isl_set
*isl_set_grow(struct isl_set
*set
, int n
)
2992 return set_from_map(isl_map_grow(set_to_map(set
), n
));
2995 struct isl_set
*isl_set_dup(struct isl_set
*set
)
2998 struct isl_set
*dup
;
3003 dup
= isl_set_alloc_space(isl_space_copy(set
->dim
), set
->n
, set
->flags
);
3006 for (i
= 0; i
< set
->n
; ++i
)
3007 dup
= isl_set_add_basic_set(dup
, isl_basic_set_copy(set
->p
[i
]));
3011 struct isl_set
*isl_set_from_basic_set(struct isl_basic_set
*bset
)
3013 return isl_map_from_basic_map(bset
);
3016 struct isl_map
*isl_map_from_basic_map(struct isl_basic_map
*bmap
)
3018 struct isl_map
*map
;
3023 map
= isl_map_alloc_space(isl_space_copy(bmap
->dim
), 1, ISL_MAP_DISJOINT
);
3024 return isl_map_add_basic_map(map
, bmap
);
3027 __isl_give isl_set
*isl_set_add_basic_set(__isl_take isl_set
*set
,
3028 __isl_take isl_basic_set
*bset
)
3030 return set_from_map(isl_map_add_basic_map(set_to_map(set
),
3031 bset_to_bmap(bset
)));
3034 __isl_null isl_set
*isl_set_free(__isl_take isl_set
*set
)
3036 return isl_map_free(set
);
3039 void isl_set_print_internal(struct isl_set
*set
, FILE *out
, int indent
)
3044 fprintf(out
, "null set\n");
3048 fprintf(out
, "%*s", indent
, "");
3049 fprintf(out
, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
3050 set
->ref
, set
->n
, set
->dim
->nparam
, set
->dim
->n_out
,
3052 for (i
= 0; i
< set
->n
; ++i
) {
3053 fprintf(out
, "%*s", indent
, "");
3054 fprintf(out
, "basic set %d:\n", i
);
3055 isl_basic_set_print_internal(set
->p
[i
], out
, indent
+4);
3059 void isl_map_print_internal(struct isl_map
*map
, FILE *out
, int indent
)
3064 fprintf(out
, "null map\n");
3068 fprintf(out
, "%*s", indent
, "");
3069 fprintf(out
, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
3070 "flags: %x, n_name: %d\n",
3071 map
->ref
, map
->n
, map
->dim
->nparam
, map
->dim
->n_in
,
3072 map
->dim
->n_out
, map
->flags
, map
->dim
->n_id
);
3073 for (i
= 0; i
< map
->n
; ++i
) {
3074 fprintf(out
, "%*s", indent
, "");
3075 fprintf(out
, "basic map %d:\n", i
);
3076 isl_basic_map_print_internal(map
->p
[i
], out
, indent
+4);
3080 struct isl_basic_map
*isl_basic_map_intersect_domain(
3081 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
3083 struct isl_basic_map
*bmap_domain
;
3088 isl_assert(bset
->ctx
, isl_space_match(bmap
->dim
, isl_dim_param
,
3089 bset
->dim
, isl_dim_param
), goto error
);
3091 if (isl_space_dim(bset
->dim
, isl_dim_set
) != 0)
3092 isl_assert(bset
->ctx
,
3093 isl_basic_map_compatible_domain(bmap
, bset
), goto error
);
3095 bmap
= isl_basic_map_cow(bmap
);
3098 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
3099 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
3100 bmap_domain
= isl_basic_map_from_domain(bset
);
3101 bmap
= add_constraints(bmap
, bmap_domain
, 0, 0);
3103 bmap
= isl_basic_map_simplify(bmap
);
3104 return isl_basic_map_finalize(bmap
);
3106 isl_basic_map_free(bmap
);
3107 isl_basic_set_free(bset
);
3111 /* Check that the space of "bset" is the same as that of the range of "bmap".
3113 static isl_stat
isl_basic_map_check_compatible_range(
3114 __isl_keep isl_basic_map
*bmap
, __isl_keep isl_basic_set
*bset
)
3118 ok
= isl_basic_map_compatible_range(bmap
, bset
);
3120 return isl_stat_error
;
3122 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
3123 "incompatible spaces", return isl_stat_error
);
3128 struct isl_basic_map
*isl_basic_map_intersect_range(
3129 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
3131 struct isl_basic_map
*bmap_range
;
3136 isl_assert(bset
->ctx
, isl_space_match(bmap
->dim
, isl_dim_param
,
3137 bset
->dim
, isl_dim_param
), goto error
);
3139 if (isl_space_dim(bset
->dim
, isl_dim_set
) != 0 &&
3140 isl_basic_map_check_compatible_range(bmap
, bset
) < 0)
3143 if (isl_basic_set_plain_is_universe(bset
)) {
3144 isl_basic_set_free(bset
);
3148 bmap
= isl_basic_map_cow(bmap
);
3151 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
3152 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
3153 bmap_range
= bset_to_bmap(bset
);
3154 bmap
= add_constraints(bmap
, bmap_range
, 0, 0);
3156 bmap
= isl_basic_map_simplify(bmap
);
3157 return isl_basic_map_finalize(bmap
);
3159 isl_basic_map_free(bmap
);
3160 isl_basic_set_free(bset
);
3164 isl_bool
isl_basic_map_contains(__isl_keep isl_basic_map
*bmap
,
3165 __isl_keep isl_vec
*vec
)
3172 return isl_bool_error
;
3174 total
= 1 + isl_basic_map_total_dim(bmap
);
3175 if (total
!= vec
->size
)
3176 return isl_bool_false
;
3180 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
3181 isl_seq_inner_product(vec
->el
, bmap
->eq
[i
], total
, &s
);
3182 if (!isl_int_is_zero(s
)) {
3184 return isl_bool_false
;
3188 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
3189 isl_seq_inner_product(vec
->el
, bmap
->ineq
[i
], total
, &s
);
3190 if (isl_int_is_neg(s
)) {
3192 return isl_bool_false
;
3198 return isl_bool_true
;
3201 isl_bool
isl_basic_set_contains(__isl_keep isl_basic_set
*bset
,
3202 __isl_keep isl_vec
*vec
)
3204 return isl_basic_map_contains(bset_to_bmap(bset
), vec
);
3207 struct isl_basic_map
*isl_basic_map_intersect(
3208 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3210 struct isl_vec
*sample
= NULL
;
3212 if (!bmap1
|| !bmap2
)
3215 isl_assert(bmap1
->ctx
, isl_space_match(bmap1
->dim
, isl_dim_param
,
3216 bmap2
->dim
, isl_dim_param
), goto error
);
3217 if (isl_space_dim(bmap1
->dim
, isl_dim_all
) ==
3218 isl_space_dim(bmap1
->dim
, isl_dim_param
) &&
3219 isl_space_dim(bmap2
->dim
, isl_dim_all
) !=
3220 isl_space_dim(bmap2
->dim
, isl_dim_param
))
3221 return isl_basic_map_intersect(bmap2
, bmap1
);
3223 if (isl_space_dim(bmap2
->dim
, isl_dim_all
) !=
3224 isl_space_dim(bmap2
->dim
, isl_dim_param
))
3225 isl_assert(bmap1
->ctx
,
3226 isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
3228 if (isl_basic_map_plain_is_empty(bmap1
)) {
3229 isl_basic_map_free(bmap2
);
3232 if (isl_basic_map_plain_is_empty(bmap2
)) {
3233 isl_basic_map_free(bmap1
);
3237 if (bmap1
->sample
&&
3238 isl_basic_map_contains(bmap1
, bmap1
->sample
) > 0 &&
3239 isl_basic_map_contains(bmap2
, bmap1
->sample
) > 0)
3240 sample
= isl_vec_copy(bmap1
->sample
);
3241 else if (bmap2
->sample
&&
3242 isl_basic_map_contains(bmap1
, bmap2
->sample
) > 0 &&
3243 isl_basic_map_contains(bmap2
, bmap2
->sample
) > 0)
3244 sample
= isl_vec_copy(bmap2
->sample
);
3246 bmap1
= isl_basic_map_cow(bmap1
);
3249 bmap1
= isl_basic_map_extend_space(bmap1
, isl_space_copy(bmap1
->dim
),
3250 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
3251 bmap1
= add_constraints(bmap1
, bmap2
, 0, 0);
3254 isl_vec_free(sample
);
3256 isl_vec_free(bmap1
->sample
);
3257 bmap1
->sample
= sample
;
3260 bmap1
= isl_basic_map_simplify(bmap1
);
3261 return isl_basic_map_finalize(bmap1
);
3264 isl_vec_free(sample
);
3265 isl_basic_map_free(bmap1
);
3266 isl_basic_map_free(bmap2
);
3270 struct isl_basic_set
*isl_basic_set_intersect(
3271 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
3273 return bset_from_bmap(isl_basic_map_intersect(bset_to_bmap(bset1
),
3274 bset_to_bmap(bset2
)));
3277 __isl_give isl_basic_set
*isl_basic_set_intersect_params(
3278 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
3280 return isl_basic_set_intersect(bset1
, bset2
);
3283 /* Special case of isl_map_intersect, where both map1 and map2
3284 * are convex, without any divs and such that either map1 or map2
3285 * contains a single constraint. This constraint is then simply
3286 * added to the other map.
3288 static __isl_give isl_map
*map_intersect_add_constraint(
3289 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
3291 isl_assert(map1
->ctx
, map1
->n
== 1, goto error
);
3292 isl_assert(map2
->ctx
, map1
->n
== 1, goto error
);
3293 isl_assert(map1
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
3294 isl_assert(map2
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
3296 if (map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
!= 1)
3297 return isl_map_intersect(map2
, map1
);
3299 isl_assert(map2
->ctx
,
3300 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1, goto error
);
3302 map1
= isl_map_cow(map1
);
3305 if (isl_map_plain_is_empty(map1
)) {
3309 map1
->p
[0] = isl_basic_map_cow(map1
->p
[0]);
3310 if (map2
->p
[0]->n_eq
== 1)
3311 map1
->p
[0] = isl_basic_map_add_eq(map1
->p
[0], map2
->p
[0]->eq
[0]);
3313 map1
->p
[0] = isl_basic_map_add_ineq(map1
->p
[0],
3314 map2
->p
[0]->ineq
[0]);
3316 map1
->p
[0] = isl_basic_map_simplify(map1
->p
[0]);
3317 map1
->p
[0] = isl_basic_map_finalize(map1
->p
[0]);
3321 if (isl_basic_map_plain_is_empty(map1
->p
[0])) {
3322 isl_basic_map_free(map1
->p
[0]);
3335 /* map2 may be either a parameter domain or a map living in the same
3338 static __isl_give isl_map
*map_intersect_internal(__isl_take isl_map
*map1
,
3339 __isl_take isl_map
*map2
)
3348 if ((isl_map_plain_is_empty(map1
) ||
3349 isl_map_plain_is_universe(map2
)) &&
3350 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
3354 if ((isl_map_plain_is_empty(map2
) ||
3355 isl_map_plain_is_universe(map1
)) &&
3356 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
3361 if (map1
->n
== 1 && map2
->n
== 1 &&
3362 map1
->p
[0]->n_div
== 0 && map2
->p
[0]->n_div
== 0 &&
3363 isl_space_is_equal(map1
->dim
, map2
->dim
) &&
3364 (map1
->p
[0]->n_eq
+ map1
->p
[0]->n_ineq
== 1 ||
3365 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1))
3366 return map_intersect_add_constraint(map1
, map2
);
3368 if (isl_space_dim(map2
->dim
, isl_dim_all
) !=
3369 isl_space_dim(map2
->dim
, isl_dim_param
))
3370 isl_assert(map1
->ctx
,
3371 isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
3373 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
3374 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
3375 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
3377 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
3378 map1
->n
* map2
->n
, flags
);
3381 for (i
= 0; i
< map1
->n
; ++i
)
3382 for (j
= 0; j
< map2
->n
; ++j
) {
3383 struct isl_basic_map
*part
;
3384 part
= isl_basic_map_intersect(
3385 isl_basic_map_copy(map1
->p
[i
]),
3386 isl_basic_map_copy(map2
->p
[j
]));
3387 if (isl_basic_map_is_empty(part
) < 0)
3388 part
= isl_basic_map_free(part
);
3389 result
= isl_map_add_basic_map(result
, part
);
3402 static __isl_give isl_map
*map_intersect(__isl_take isl_map
*map1
,
3403 __isl_take isl_map
*map2
)
3407 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
3408 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
3409 "spaces don't match", goto error
);
3410 return map_intersect_internal(map1
, map2
);
3417 __isl_give isl_map
*isl_map_intersect(__isl_take isl_map
*map1
,
3418 __isl_take isl_map
*map2
)
3420 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect
);
3423 struct isl_set
*isl_set_intersect(struct isl_set
*set1
, struct isl_set
*set2
)
3425 return set_from_map(isl_map_intersect(set_to_map(set1
),
3429 /* map_intersect_internal accepts intersections
3430 * with parameter domains, so we can just call that function.
3432 static __isl_give isl_map
*map_intersect_params(__isl_take isl_map
*map
,
3433 __isl_take isl_set
*params
)
3435 return map_intersect_internal(map
, params
);
3438 __isl_give isl_map
*isl_map_intersect_params(__isl_take isl_map
*map1
,
3439 __isl_take isl_map
*map2
)
3441 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect_params
);
3444 __isl_give isl_set
*isl_set_intersect_params(__isl_take isl_set
*set
,
3445 __isl_take isl_set
*params
)
3447 return isl_map_intersect_params(set
, params
);
3450 struct isl_basic_map
*isl_basic_map_reverse(struct isl_basic_map
*bmap
)
3453 unsigned pos
, n1
, n2
;
3457 bmap
= isl_basic_map_cow(bmap
);
3460 space
= isl_space_reverse(isl_space_copy(bmap
->dim
));
3461 pos
= isl_basic_map_offset(bmap
, isl_dim_in
);
3462 n1
= isl_basic_map_dim(bmap
, isl_dim_in
);
3463 n2
= isl_basic_map_dim(bmap
, isl_dim_out
);
3464 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
3465 return isl_basic_map_reset_space(bmap
, space
);
3468 static __isl_give isl_basic_map
*basic_map_space_reset(
3469 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
)
3475 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
3478 space
= isl_basic_map_get_space(bmap
);
3479 space
= isl_space_reset(space
, type
);
3480 bmap
= isl_basic_map_reset_space(bmap
, space
);
3484 __isl_give isl_basic_map
*isl_basic_map_insert_dims(
3485 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
3486 unsigned pos
, unsigned n
)
3489 struct isl_basic_map
*res
;
3490 struct isl_dim_map
*dim_map
;
3491 unsigned total
, off
;
3492 enum isl_dim_type t
;
3495 return basic_map_space_reset(bmap
, type
);
3500 res_dim
= isl_space_insert_dims(isl_basic_map_get_space(bmap
), type
, pos
, n
);
3502 total
= isl_basic_map_total_dim(bmap
) + n
;
3503 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3505 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3507 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3509 unsigned size
= isl_basic_map_dim(bmap
, t
);
3510 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3512 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3513 pos
, size
- pos
, off
+ pos
+ n
);
3515 off
+= isl_space_dim(res_dim
, t
);
3517 isl_dim_map_div(dim_map
, bmap
, off
);
3519 res
= isl_basic_map_alloc_space(res_dim
,
3520 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3521 if (isl_basic_map_is_rational(bmap
))
3522 res
= isl_basic_map_set_rational(res
);
3523 if (isl_basic_map_plain_is_empty(bmap
)) {
3524 isl_basic_map_free(bmap
);
3526 return isl_basic_map_set_to_empty(res
);
3528 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3529 return isl_basic_map_finalize(res
);
3532 __isl_give isl_basic_set
*isl_basic_set_insert_dims(
3533 __isl_take isl_basic_set
*bset
,
3534 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3536 return isl_basic_map_insert_dims(bset
, type
, pos
, n
);
3539 __isl_give isl_basic_map
*isl_basic_map_add_dims(__isl_take isl_basic_map
*bmap
,
3540 enum isl_dim_type type
, unsigned n
)
3544 return isl_basic_map_insert_dims(bmap
, type
,
3545 isl_basic_map_dim(bmap
, type
), n
);
3548 __isl_give isl_basic_set
*isl_basic_set_add_dims(__isl_take isl_basic_set
*bset
,
3549 enum isl_dim_type type
, unsigned n
)
3553 isl_assert(bset
->ctx
, type
!= isl_dim_in
, goto error
);
3554 return isl_basic_map_add_dims(bset
, type
, n
);
3556 isl_basic_set_free(bset
);
3560 static __isl_give isl_map
*map_space_reset(__isl_take isl_map
*map
,
3561 enum isl_dim_type type
)
3565 if (!map
|| !isl_space_is_named_or_nested(map
->dim
, type
))
3568 space
= isl_map_get_space(map
);
3569 space
= isl_space_reset(space
, type
);
3570 map
= isl_map_reset_space(map
, space
);
3574 __isl_give isl_map
*isl_map_insert_dims(__isl_take isl_map
*map
,
3575 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3580 return map_space_reset(map
, type
);
3582 map
= isl_map_cow(map
);
3586 map
->dim
= isl_space_insert_dims(map
->dim
, type
, pos
, n
);
3590 for (i
= 0; i
< map
->n
; ++i
) {
3591 map
->p
[i
] = isl_basic_map_insert_dims(map
->p
[i
], type
, pos
, n
);
3602 __isl_give isl_set
*isl_set_insert_dims(__isl_take isl_set
*set
,
3603 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3605 return isl_map_insert_dims(set
, type
, pos
, n
);
3608 __isl_give isl_map
*isl_map_add_dims(__isl_take isl_map
*map
,
3609 enum isl_dim_type type
, unsigned n
)
3613 return isl_map_insert_dims(map
, type
, isl_map_dim(map
, type
), n
);
3616 __isl_give isl_set
*isl_set_add_dims(__isl_take isl_set
*set
,
3617 enum isl_dim_type type
, unsigned n
)
3621 isl_assert(set
->ctx
, type
!= isl_dim_in
, goto error
);
3622 return set_from_map(isl_map_add_dims(set_to_map(set
), type
, n
));
3628 __isl_give isl_basic_map
*isl_basic_map_move_dims(
3629 __isl_take isl_basic_map
*bmap
,
3630 enum isl_dim_type dst_type
, unsigned dst_pos
,
3631 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3633 struct isl_dim_map
*dim_map
;
3634 struct isl_basic_map
*res
;
3635 enum isl_dim_type t
;
3636 unsigned total
, off
;
3643 if (isl_basic_map_check_range(bmap
, src_type
, src_pos
, n
) < 0)
3644 return isl_basic_map_free(bmap
);
3646 if (dst_type
== src_type
&& dst_pos
== src_pos
)
3649 isl_assert(bmap
->ctx
, dst_type
!= src_type
, goto error
);
3651 if (pos(bmap
->dim
, dst_type
) + dst_pos
==
3652 pos(bmap
->dim
, src_type
) + src_pos
+
3653 ((src_type
< dst_type
) ? n
: 0)) {
3654 bmap
= isl_basic_map_cow(bmap
);
3658 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
3659 src_type
, src_pos
, n
);
3663 bmap
= isl_basic_map_finalize(bmap
);
3668 total
= isl_basic_map_total_dim(bmap
);
3669 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3672 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3673 unsigned size
= isl_space_dim(bmap
->dim
, t
);
3674 if (t
== dst_type
) {
3675 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3678 isl_dim_map_dim_range(dim_map
, bmap
->dim
, src_type
,
3681 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3682 dst_pos
, size
- dst_pos
, off
);
3683 off
+= size
- dst_pos
;
3684 } else if (t
== src_type
) {
3685 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3688 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3689 src_pos
+ n
, size
- src_pos
- n
, off
);
3690 off
+= size
- src_pos
- n
;
3692 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3696 isl_dim_map_div(dim_map
, bmap
, off
);
3698 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
3699 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3700 bmap
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3704 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
3705 src_type
, src_pos
, n
);
3709 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
3710 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3711 bmap
= isl_basic_map_finalize(bmap
);
3715 isl_basic_map_free(bmap
);
3719 __isl_give isl_basic_set
*isl_basic_set_move_dims(__isl_take isl_basic_set
*bset
,
3720 enum isl_dim_type dst_type
, unsigned dst_pos
,
3721 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3723 isl_basic_map
*bmap
= bset_to_bmap(bset
);
3724 bmap
= isl_basic_map_move_dims(bmap
, dst_type
, dst_pos
,
3725 src_type
, src_pos
, n
);
3726 return bset_from_bmap(bmap
);
3729 __isl_give isl_set
*isl_set_move_dims(__isl_take isl_set
*set
,
3730 enum isl_dim_type dst_type
, unsigned dst_pos
,
3731 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3735 isl_assert(set
->ctx
, dst_type
!= isl_dim_in
, goto error
);
3736 return set_from_map(isl_map_move_dims(set_to_map(set
),
3737 dst_type
, dst_pos
, src_type
, src_pos
, n
));
3743 __isl_give isl_map
*isl_map_move_dims(__isl_take isl_map
*map
,
3744 enum isl_dim_type dst_type
, unsigned dst_pos
,
3745 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3754 isl_assert(map
->ctx
, src_pos
+ n
<= isl_map_dim(map
, src_type
),
3757 if (dst_type
== src_type
&& dst_pos
== src_pos
)
3760 isl_assert(map
->ctx
, dst_type
!= src_type
, goto error
);
3762 map
= isl_map_cow(map
);
3766 map
->dim
= isl_space_move_dims(map
->dim
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
3770 for (i
= 0; i
< map
->n
; ++i
) {
3771 map
->p
[i
] = isl_basic_map_move_dims(map
->p
[i
],
3773 src_type
, src_pos
, n
);
3784 /* Move the specified dimensions to the last columns right before
3785 * the divs. Don't change the dimension specification of bmap.
3786 * That's the responsibility of the caller.
3788 static __isl_give isl_basic_map
*move_last(__isl_take isl_basic_map
*bmap
,
3789 enum isl_dim_type type
, unsigned first
, unsigned n
)
3791 struct isl_dim_map
*dim_map
;
3792 struct isl_basic_map
*res
;
3793 enum isl_dim_type t
;
3794 unsigned total
, off
;
3798 if (pos(bmap
->dim
, type
) + first
+ n
==
3799 1 + isl_space_dim(bmap
->dim
, isl_dim_all
))
3802 total
= isl_basic_map_total_dim(bmap
);
3803 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3806 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3807 unsigned size
= isl_space_dim(bmap
->dim
, t
);
3809 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3812 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3813 first
, n
, total
- bmap
->n_div
- n
);
3814 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3815 first
+ n
, size
- (first
+ n
), off
);
3816 off
+= size
- (first
+ n
);
3818 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3822 isl_dim_map_div(dim_map
, bmap
, off
+ n
);
3824 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
3825 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3826 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3830 /* Insert "n" rows in the divs of "bmap".
3832 * The number of columns is not changed, which means that the last
3833 * dimensions of "bmap" are being reintepreted as the new divs.
3834 * The space of "bmap" is not adjusted, however, which means
3835 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
3836 * from the space of "bmap" is the responsibility of the caller.
3838 static __isl_give isl_basic_map
*insert_div_rows(__isl_take isl_basic_map
*bmap
,
3846 bmap
= isl_basic_map_cow(bmap
);
3850 row_size
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + bmap
->extra
;
3851 old
= bmap
->block2
.data
;
3852 bmap
->block2
= isl_blk_extend(bmap
->ctx
, bmap
->block2
,
3853 (bmap
->extra
+ n
) * (1 + row_size
));
3854 if (!bmap
->block2
.data
)
3855 return isl_basic_map_free(bmap
);
3856 new_div
= isl_alloc_array(bmap
->ctx
, isl_int
*, bmap
->extra
+ n
);
3858 return isl_basic_map_free(bmap
);
3859 for (i
= 0; i
< n
; ++i
) {
3860 new_div
[i
] = bmap
->block2
.data
+
3861 (bmap
->extra
+ i
) * (1 + row_size
);
3862 isl_seq_clr(new_div
[i
], 1 + row_size
);
3864 for (i
= 0; i
< bmap
->extra
; ++i
)
3865 new_div
[n
+ i
] = bmap
->block2
.data
+ (bmap
->div
[i
] - old
);
3867 bmap
->div
= new_div
;
3874 /* Drop constraints from "bmap" that only involve the variables
3875 * of "type" in the range [first, first + n] that are not related
3876 * to any of the variables outside that interval.
3877 * These constraints cannot influence the values for the variables
3878 * outside the interval, except in case they cause "bmap" to be empty.
3879 * Only drop the constraints if "bmap" is known to be non-empty.
3881 static __isl_give isl_basic_map
*drop_irrelevant_constraints(
3882 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
3883 unsigned first
, unsigned n
)
3887 unsigned dim
, n_div
;
3890 non_empty
= isl_basic_map_plain_is_non_empty(bmap
);
3892 return isl_basic_map_free(bmap
);
3896 dim
= isl_basic_map_dim(bmap
, isl_dim_all
);
3897 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
3898 groups
= isl_calloc_array(isl_basic_map_get_ctx(bmap
), int, dim
);
3900 return isl_basic_map_free(bmap
);
3901 first
+= isl_basic_map_offset(bmap
, type
) - 1;
3902 for (i
= 0; i
< first
; ++i
)
3904 for (i
= first
+ n
; i
< dim
- n_div
; ++i
)
3907 bmap
= isl_basic_map_drop_unrelated_constraints(bmap
, groups
);
3912 /* Turn the n dimensions of type type, starting at first
3913 * into existentially quantified variables.
3915 * If a subset of the projected out variables are unrelated
3916 * to any of the variables that remain, then the constraints
3917 * involving this subset are simply dropped first.
3919 __isl_give isl_basic_map
*isl_basic_map_project_out(
3920 __isl_take isl_basic_map
*bmap
,
3921 enum isl_dim_type type
, unsigned first
, unsigned n
)
3924 return basic_map_space_reset(bmap
, type
);
3925 if (type
== isl_dim_div
)
3926 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
3927 "cannot project out existentially quantified variables",
3928 return isl_basic_map_free(bmap
));
3930 bmap
= drop_irrelevant_constraints(bmap
, type
, first
, n
);
3934 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
3935 return isl_basic_map_remove_dims(bmap
, type
, first
, n
);
3937 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
3938 return isl_basic_map_free(bmap
);
3940 bmap
= move_last(bmap
, type
, first
, n
);
3941 bmap
= isl_basic_map_cow(bmap
);
3942 bmap
= insert_div_rows(bmap
, n
);
3946 bmap
->dim
= isl_space_drop_dims(bmap
->dim
, type
, first
, n
);
3949 bmap
= isl_basic_map_simplify(bmap
);
3950 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
3951 return isl_basic_map_finalize(bmap
);
3953 isl_basic_map_free(bmap
);
3957 /* Turn the n dimensions of type type, starting at first
3958 * into existentially quantified variables.
3960 struct isl_basic_set
*isl_basic_set_project_out(struct isl_basic_set
*bset
,
3961 enum isl_dim_type type
, unsigned first
, unsigned n
)
3963 return bset_from_bmap(isl_basic_map_project_out(bset_to_bmap(bset
),
3967 /* Turn the n dimensions of type type, starting at first
3968 * into existentially quantified variables.
3970 __isl_give isl_map
*isl_map_project_out(__isl_take isl_map
*map
,
3971 enum isl_dim_type type
, unsigned first
, unsigned n
)
3979 return map_space_reset(map
, type
);
3981 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
3983 map
= isl_map_cow(map
);
3987 map
->dim
= isl_space_drop_dims(map
->dim
, type
, first
, n
);
3991 for (i
= 0; i
< map
->n
; ++i
) {
3992 map
->p
[i
] = isl_basic_map_project_out(map
->p
[i
], type
, first
, n
);
4003 /* Turn the n dimensions of type type, starting at first
4004 * into existentially quantified variables.
4006 __isl_give isl_set
*isl_set_project_out(__isl_take isl_set
*set
,
4007 enum isl_dim_type type
, unsigned first
, unsigned n
)
4009 return set_from_map(isl_map_project_out(set_to_map(set
),
4013 /* Return a map that projects the elements in "set" onto their
4014 * "n" set dimensions starting at "first".
4015 * "type" should be equal to isl_dim_set.
4017 __isl_give isl_map
*isl_set_project_onto_map(__isl_take isl_set
*set
,
4018 enum isl_dim_type type
, unsigned first
, unsigned n
)
4026 if (type
!= isl_dim_set
)
4027 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
4028 "only set dimensions can be projected out", goto error
);
4029 dim
= isl_set_dim(set
, isl_dim_set
);
4030 if (first
+ n
> dim
|| first
+ n
< first
)
4031 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
4032 "index out of bounds", goto error
);
4034 map
= isl_map_from_domain(set
);
4035 map
= isl_map_add_dims(map
, isl_dim_out
, n
);
4036 for (i
= 0; i
< n
; ++i
)
4037 map
= isl_map_equate(map
, isl_dim_in
, first
+ i
,
4045 static struct isl_basic_map
*add_divs(struct isl_basic_map
*bmap
, unsigned n
)
4049 for (i
= 0; i
< n
; ++i
) {
4050 j
= isl_basic_map_alloc_div(bmap
);
4053 isl_seq_clr(bmap
->div
[j
], 1+1+isl_basic_map_total_dim(bmap
));
4057 isl_basic_map_free(bmap
);
4061 struct isl_basic_map
*isl_basic_map_apply_range(
4062 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
4064 isl_space
*dim_result
= NULL
;
4065 struct isl_basic_map
*bmap
;
4066 unsigned n_in
, n_out
, n
, nparam
, total
, pos
;
4067 struct isl_dim_map
*dim_map1
, *dim_map2
;
4069 if (!bmap1
|| !bmap2
)
4071 if (!isl_space_match(bmap1
->dim
, isl_dim_param
,
4072 bmap2
->dim
, isl_dim_param
))
4073 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
4074 "parameters don't match", goto error
);
4075 if (!isl_space_tuple_is_equal(bmap1
->dim
, isl_dim_out
,
4076 bmap2
->dim
, isl_dim_in
))
4077 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
4078 "spaces don't match", goto error
);
4080 dim_result
= isl_space_join(isl_space_copy(bmap1
->dim
),
4081 isl_space_copy(bmap2
->dim
));
4083 n_in
= isl_basic_map_n_in(bmap1
);
4084 n_out
= isl_basic_map_n_out(bmap2
);
4085 n
= isl_basic_map_n_out(bmap1
);
4086 nparam
= isl_basic_map_n_param(bmap1
);
4088 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ n
;
4089 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4090 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4091 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
4092 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
4093 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
4094 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_in
);
4095 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_out
);
4096 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
4097 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
4098 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
4100 bmap
= isl_basic_map_alloc_space(dim_result
,
4101 bmap1
->n_div
+ bmap2
->n_div
+ n
,
4102 bmap1
->n_eq
+ bmap2
->n_eq
,
4103 bmap1
->n_ineq
+ bmap2
->n_ineq
);
4104 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
4105 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
4106 bmap
= add_divs(bmap
, n
);
4107 bmap
= isl_basic_map_simplify(bmap
);
4108 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
4109 return isl_basic_map_finalize(bmap
);
4111 isl_basic_map_free(bmap1
);
4112 isl_basic_map_free(bmap2
);
4116 struct isl_basic_set
*isl_basic_set_apply(
4117 struct isl_basic_set
*bset
, struct isl_basic_map
*bmap
)
4122 isl_assert(bset
->ctx
, isl_basic_map_compatible_domain(bmap
, bset
),
4125 return bset_from_bmap(isl_basic_map_apply_range(bset_to_bmap(bset
),
4128 isl_basic_set_free(bset
);
4129 isl_basic_map_free(bmap
);
4133 struct isl_basic_map
*isl_basic_map_apply_domain(
4134 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
4136 if (!bmap1
|| !bmap2
)
4139 isl_assert(bmap1
->ctx
,
4140 isl_basic_map_n_in(bmap1
) == isl_basic_map_n_in(bmap2
), goto error
);
4141 isl_assert(bmap1
->ctx
,
4142 isl_basic_map_n_param(bmap1
) == isl_basic_map_n_param(bmap2
),
4145 bmap1
= isl_basic_map_reverse(bmap1
);
4146 bmap1
= isl_basic_map_apply_range(bmap1
, bmap2
);
4147 return isl_basic_map_reverse(bmap1
);
4149 isl_basic_map_free(bmap1
);
4150 isl_basic_map_free(bmap2
);
4154 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
4155 * A \cap B -> f(A) + f(B)
4157 struct isl_basic_map
*isl_basic_map_sum(
4158 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
4160 unsigned n_in
, n_out
, nparam
, total
, pos
;
4161 struct isl_basic_map
*bmap
= NULL
;
4162 struct isl_dim_map
*dim_map1
, *dim_map2
;
4165 if (!bmap1
|| !bmap2
)
4168 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
),
4171 nparam
= isl_basic_map_n_param(bmap1
);
4172 n_in
= isl_basic_map_n_in(bmap1
);
4173 n_out
= isl_basic_map_n_out(bmap1
);
4175 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
;
4176 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4177 dim_map2
= isl_dim_map_alloc(bmap2
->ctx
, total
);
4178 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
4179 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
);
4180 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
4181 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
4182 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_in
+ n_out
);
4183 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
4184 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
4185 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_out
);
4187 bmap
= isl_basic_map_alloc_space(isl_space_copy(bmap1
->dim
),
4188 bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
,
4189 bmap1
->n_eq
+ bmap2
->n_eq
+ n_out
,
4190 bmap1
->n_ineq
+ bmap2
->n_ineq
);
4191 for (i
= 0; i
< n_out
; ++i
) {
4192 int j
= isl_basic_map_alloc_equality(bmap
);
4195 isl_seq_clr(bmap
->eq
[j
], 1+total
);
4196 isl_int_set_si(bmap
->eq
[j
][1+nparam
+n_in
+i
], -1);
4197 isl_int_set_si(bmap
->eq
[j
][1+pos
+i
], 1);
4198 isl_int_set_si(bmap
->eq
[j
][1+pos
-n_out
+i
], 1);
4200 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
4201 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
4202 bmap
= add_divs(bmap
, 2 * n_out
);
4204 bmap
= isl_basic_map_simplify(bmap
);
4205 return isl_basic_map_finalize(bmap
);
4207 isl_basic_map_free(bmap
);
4208 isl_basic_map_free(bmap1
);
4209 isl_basic_map_free(bmap2
);
4213 /* Given two maps A -> f(A) and B -> g(B), construct a map
4214 * A \cap B -> f(A) + f(B)
4216 struct isl_map
*isl_map_sum(struct isl_map
*map1
, struct isl_map
*map2
)
4218 struct isl_map
*result
;
4224 isl_assert(map1
->ctx
, isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
4226 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
4227 map1
->n
* map2
->n
, 0);
4230 for (i
= 0; i
< map1
->n
; ++i
)
4231 for (j
= 0; j
< map2
->n
; ++j
) {
4232 struct isl_basic_map
*part
;
4233 part
= isl_basic_map_sum(
4234 isl_basic_map_copy(map1
->p
[i
]),
4235 isl_basic_map_copy(map2
->p
[j
]));
4236 if (isl_basic_map_is_empty(part
))
4237 isl_basic_map_free(part
);
4239 result
= isl_map_add_basic_map(result
, part
);
4252 __isl_give isl_set
*isl_set_sum(__isl_take isl_set
*set1
,
4253 __isl_take isl_set
*set2
)
4255 return set_from_map(isl_map_sum(set_to_map(set1
), set_to_map(set2
)));
4258 /* Given a basic map A -> f(A), construct A -> -f(A).
4260 struct isl_basic_map
*isl_basic_map_neg(struct isl_basic_map
*bmap
)
4265 bmap
= isl_basic_map_cow(bmap
);
4269 n
= isl_basic_map_dim(bmap
, isl_dim_out
);
4270 off
= isl_basic_map_offset(bmap
, isl_dim_out
);
4271 for (i
= 0; i
< bmap
->n_eq
; ++i
)
4272 for (j
= 0; j
< n
; ++j
)
4273 isl_int_neg(bmap
->eq
[i
][off
+j
], bmap
->eq
[i
][off
+j
]);
4274 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
4275 for (j
= 0; j
< n
; ++j
)
4276 isl_int_neg(bmap
->ineq
[i
][off
+j
], bmap
->ineq
[i
][off
+j
]);
4277 for (i
= 0; i
< bmap
->n_div
; ++i
)
4278 for (j
= 0; j
< n
; ++j
)
4279 isl_int_neg(bmap
->div
[i
][1+off
+j
], bmap
->div
[i
][1+off
+j
]);
4280 bmap
= isl_basic_map_gauss(bmap
, NULL
);
4281 return isl_basic_map_finalize(bmap
);
4284 __isl_give isl_basic_set
*isl_basic_set_neg(__isl_take isl_basic_set
*bset
)
4286 return isl_basic_map_neg(bset
);
4289 /* Given a map A -> f(A), construct A -> -f(A).
4291 struct isl_map
*isl_map_neg(struct isl_map
*map
)
4295 map
= isl_map_cow(map
);
4299 for (i
= 0; i
< map
->n
; ++i
) {
4300 map
->p
[i
] = isl_basic_map_neg(map
->p
[i
]);
4311 __isl_give isl_set
*isl_set_neg(__isl_take isl_set
*set
)
4313 return set_from_map(isl_map_neg(set_to_map(set
)));
4316 /* Given a basic map A -> f(A) and an integer d, construct a basic map
4317 * A -> floor(f(A)/d).
4319 struct isl_basic_map
*isl_basic_map_floordiv(struct isl_basic_map
*bmap
,
4322 unsigned n_in
, n_out
, nparam
, total
, pos
;
4323 struct isl_basic_map
*result
= NULL
;
4324 struct isl_dim_map
*dim_map
;
4330 nparam
= isl_basic_map_n_param(bmap
);
4331 n_in
= isl_basic_map_n_in(bmap
);
4332 n_out
= isl_basic_map_n_out(bmap
);
4334 total
= nparam
+ n_in
+ n_out
+ bmap
->n_div
+ n_out
;
4335 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
4336 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_param
, pos
= 0);
4337 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_in
, pos
+= nparam
);
4338 isl_dim_map_div(dim_map
, bmap
, pos
+= n_in
+ n_out
);
4339 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_out
, pos
+= bmap
->n_div
);
4341 result
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
4342 bmap
->n_div
+ n_out
,
4343 bmap
->n_eq
, bmap
->n_ineq
+ 2 * n_out
);
4344 result
= isl_basic_map_add_constraints_dim_map(result
, bmap
, dim_map
);
4345 result
= add_divs(result
, n_out
);
4346 for (i
= 0; i
< n_out
; ++i
) {
4348 j
= isl_basic_map_alloc_inequality(result
);
4351 isl_seq_clr(result
->ineq
[j
], 1+total
);
4352 isl_int_neg(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
4353 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], 1);
4354 j
= isl_basic_map_alloc_inequality(result
);
4357 isl_seq_clr(result
->ineq
[j
], 1+total
);
4358 isl_int_set(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
4359 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], -1);
4360 isl_int_sub_ui(result
->ineq
[j
][0], d
, 1);
4363 result
= isl_basic_map_simplify(result
);
4364 return isl_basic_map_finalize(result
);
4366 isl_basic_map_free(result
);
4370 /* Given a map A -> f(A) and an integer d, construct a map
4371 * A -> floor(f(A)/d).
4373 struct isl_map
*isl_map_floordiv(struct isl_map
*map
, isl_int d
)
4377 map
= isl_map_cow(map
);
4381 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
4382 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4383 for (i
= 0; i
< map
->n
; ++i
) {
4384 map
->p
[i
] = isl_basic_map_floordiv(map
->p
[i
], d
);
4395 /* Given a map A -> f(A) and an integer d, construct a map
4396 * A -> floor(f(A)/d).
4398 __isl_give isl_map
*isl_map_floordiv_val(__isl_take isl_map
*map
,
4399 __isl_take isl_val
*d
)
4403 if (!isl_val_is_int(d
))
4404 isl_die(isl_val_get_ctx(d
), isl_error_invalid
,
4405 "expecting integer denominator", goto error
);
4406 map
= isl_map_floordiv(map
, d
->n
);
4415 static struct isl_basic_map
*var_equal(struct isl_basic_map
*bmap
, unsigned pos
)
4421 i
= isl_basic_map_alloc_equality(bmap
);
4424 nparam
= isl_basic_map_n_param(bmap
);
4425 n_in
= isl_basic_map_n_in(bmap
);
4426 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4427 isl_int_set_si(bmap
->eq
[i
][1+nparam
+pos
], -1);
4428 isl_int_set_si(bmap
->eq
[i
][1+nparam
+n_in
+pos
], 1);
4429 return isl_basic_map_finalize(bmap
);
4431 isl_basic_map_free(bmap
);
4435 /* Add a constraint to "bmap" expressing i_pos < o_pos
4437 static struct isl_basic_map
*var_less(struct isl_basic_map
*bmap
, unsigned pos
)
4443 i
= isl_basic_map_alloc_inequality(bmap
);
4446 nparam
= isl_basic_map_n_param(bmap
);
4447 n_in
= isl_basic_map_n_in(bmap
);
4448 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4449 isl_int_set_si(bmap
->ineq
[i
][0], -1);
4450 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
4451 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
4452 return isl_basic_map_finalize(bmap
);
4454 isl_basic_map_free(bmap
);
4458 /* Add a constraint to "bmap" expressing i_pos <= o_pos
4460 static __isl_give isl_basic_map
*var_less_or_equal(
4461 __isl_take isl_basic_map
*bmap
, unsigned pos
)
4467 i
= isl_basic_map_alloc_inequality(bmap
);
4470 nparam
= isl_basic_map_n_param(bmap
);
4471 n_in
= isl_basic_map_n_in(bmap
);
4472 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4473 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
4474 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
4475 return isl_basic_map_finalize(bmap
);
4477 isl_basic_map_free(bmap
);
4481 /* Add a constraint to "bmap" expressing i_pos > o_pos
4483 static struct isl_basic_map
*var_more(struct isl_basic_map
*bmap
, unsigned pos
)
4489 i
= isl_basic_map_alloc_inequality(bmap
);
4492 nparam
= isl_basic_map_n_param(bmap
);
4493 n_in
= isl_basic_map_n_in(bmap
);
4494 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4495 isl_int_set_si(bmap
->ineq
[i
][0], -1);
4496 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
4497 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
4498 return isl_basic_map_finalize(bmap
);
4500 isl_basic_map_free(bmap
);
4504 /* Add a constraint to "bmap" expressing i_pos >= o_pos
4506 static __isl_give isl_basic_map
*var_more_or_equal(
4507 __isl_take isl_basic_map
*bmap
, unsigned pos
)
4513 i
= isl_basic_map_alloc_inequality(bmap
);
4516 nparam
= isl_basic_map_n_param(bmap
);
4517 n_in
= isl_basic_map_n_in(bmap
);
4518 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4519 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
4520 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
4521 return isl_basic_map_finalize(bmap
);
4523 isl_basic_map_free(bmap
);
4527 __isl_give isl_basic_map
*isl_basic_map_equal(
4528 __isl_take isl_space
*dim
, unsigned n_equal
)
4531 struct isl_basic_map
*bmap
;
4532 bmap
= isl_basic_map_alloc_space(dim
, 0, n_equal
, 0);
4535 for (i
= 0; i
< n_equal
&& bmap
; ++i
)
4536 bmap
= var_equal(bmap
, i
);
4537 return isl_basic_map_finalize(bmap
);
4540 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
4542 __isl_give isl_basic_map
*isl_basic_map_less_at(__isl_take isl_space
*dim
,
4546 struct isl_basic_map
*bmap
;
4547 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4550 for (i
= 0; i
< pos
&& bmap
; ++i
)
4551 bmap
= var_equal(bmap
, i
);
4553 bmap
= var_less(bmap
, pos
);
4554 return isl_basic_map_finalize(bmap
);
4557 /* Return a relation on "dim" expressing i_[0..pos] <<= o_[0..pos]
4559 __isl_give isl_basic_map
*isl_basic_map_less_or_equal_at(
4560 __isl_take isl_space
*dim
, unsigned pos
)
4563 isl_basic_map
*bmap
;
4565 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4566 for (i
= 0; i
< pos
; ++i
)
4567 bmap
= var_equal(bmap
, i
);
4568 bmap
= var_less_or_equal(bmap
, pos
);
4569 return isl_basic_map_finalize(bmap
);
4572 /* Return a relation on "dim" expressing i_pos > o_pos
4574 __isl_give isl_basic_map
*isl_basic_map_more_at(__isl_take isl_space
*dim
,
4578 struct isl_basic_map
*bmap
;
4579 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4582 for (i
= 0; i
< pos
&& bmap
; ++i
)
4583 bmap
= var_equal(bmap
, i
);
4585 bmap
= var_more(bmap
, pos
);
4586 return isl_basic_map_finalize(bmap
);
4589 /* Return a relation on "dim" expressing i_[0..pos] >>= o_[0..pos]
4591 __isl_give isl_basic_map
*isl_basic_map_more_or_equal_at(
4592 __isl_take isl_space
*dim
, unsigned pos
)
4595 isl_basic_map
*bmap
;
4597 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4598 for (i
= 0; i
< pos
; ++i
)
4599 bmap
= var_equal(bmap
, i
);
4600 bmap
= var_more_or_equal(bmap
, pos
);
4601 return isl_basic_map_finalize(bmap
);
4604 static __isl_give isl_map
*map_lex_lte_first(__isl_take isl_space
*dims
,
4605 unsigned n
, int equal
)
4607 struct isl_map
*map
;
4610 if (n
== 0 && equal
)
4611 return isl_map_universe(dims
);
4613 map
= isl_map_alloc_space(isl_space_copy(dims
), n
, ISL_MAP_DISJOINT
);
4615 for (i
= 0; i
+ 1 < n
; ++i
)
4616 map
= isl_map_add_basic_map(map
,
4617 isl_basic_map_less_at(isl_space_copy(dims
), i
));
4620 map
= isl_map_add_basic_map(map
,
4621 isl_basic_map_less_or_equal_at(dims
, n
- 1));
4623 map
= isl_map_add_basic_map(map
,
4624 isl_basic_map_less_at(dims
, n
- 1));
4626 isl_space_free(dims
);
4631 static __isl_give isl_map
*map_lex_lte(__isl_take isl_space
*dims
, int equal
)
4635 return map_lex_lte_first(dims
, dims
->n_out
, equal
);
4638 __isl_give isl_map
*isl_map_lex_lt_first(__isl_take isl_space
*dim
, unsigned n
)
4640 return map_lex_lte_first(dim
, n
, 0);
4643 __isl_give isl_map
*isl_map_lex_le_first(__isl_take isl_space
*dim
, unsigned n
)
4645 return map_lex_lte_first(dim
, n
, 1);
4648 __isl_give isl_map
*isl_map_lex_lt(__isl_take isl_space
*set_dim
)
4650 return map_lex_lte(isl_space_map_from_set(set_dim
), 0);
4653 __isl_give isl_map
*isl_map_lex_le(__isl_take isl_space
*set_dim
)
4655 return map_lex_lte(isl_space_map_from_set(set_dim
), 1);
4658 static __isl_give isl_map
*map_lex_gte_first(__isl_take isl_space
*dims
,
4659 unsigned n
, int equal
)
4661 struct isl_map
*map
;
4664 if (n
== 0 && equal
)
4665 return isl_map_universe(dims
);
4667 map
= isl_map_alloc_space(isl_space_copy(dims
), n
, ISL_MAP_DISJOINT
);
4669 for (i
= 0; i
+ 1 < n
; ++i
)
4670 map
= isl_map_add_basic_map(map
,
4671 isl_basic_map_more_at(isl_space_copy(dims
), i
));
4674 map
= isl_map_add_basic_map(map
,
4675 isl_basic_map_more_or_equal_at(dims
, n
- 1));
4677 map
= isl_map_add_basic_map(map
,
4678 isl_basic_map_more_at(dims
, n
- 1));
4680 isl_space_free(dims
);
4685 static __isl_give isl_map
*map_lex_gte(__isl_take isl_space
*dims
, int equal
)
4689 return map_lex_gte_first(dims
, dims
->n_out
, equal
);
4692 __isl_give isl_map
*isl_map_lex_gt_first(__isl_take isl_space
*dim
, unsigned n
)
4694 return map_lex_gte_first(dim
, n
, 0);
4697 __isl_give isl_map
*isl_map_lex_ge_first(__isl_take isl_space
*dim
, unsigned n
)
4699 return map_lex_gte_first(dim
, n
, 1);
4702 __isl_give isl_map
*isl_map_lex_gt(__isl_take isl_space
*set_dim
)
4704 return map_lex_gte(isl_space_map_from_set(set_dim
), 0);
4707 __isl_give isl_map
*isl_map_lex_ge(__isl_take isl_space
*set_dim
)
4709 return map_lex_gte(isl_space_map_from_set(set_dim
), 1);
4712 __isl_give isl_map
*isl_set_lex_le_set(__isl_take isl_set
*set1
,
4713 __isl_take isl_set
*set2
)
4716 map
= isl_map_lex_le(isl_set_get_space(set1
));
4717 map
= isl_map_intersect_domain(map
, set1
);
4718 map
= isl_map_intersect_range(map
, set2
);
4722 __isl_give isl_map
*isl_set_lex_lt_set(__isl_take isl_set
*set1
,
4723 __isl_take isl_set
*set2
)
4726 map
= isl_map_lex_lt(isl_set_get_space(set1
));
4727 map
= isl_map_intersect_domain(map
, set1
);
4728 map
= isl_map_intersect_range(map
, set2
);
4732 __isl_give isl_map
*isl_set_lex_ge_set(__isl_take isl_set
*set1
,
4733 __isl_take isl_set
*set2
)
4736 map
= isl_map_lex_ge(isl_set_get_space(set1
));
4737 map
= isl_map_intersect_domain(map
, set1
);
4738 map
= isl_map_intersect_range(map
, set2
);
4742 __isl_give isl_map
*isl_set_lex_gt_set(__isl_take isl_set
*set1
,
4743 __isl_take isl_set
*set2
)
4746 map
= isl_map_lex_gt(isl_set_get_space(set1
));
4747 map
= isl_map_intersect_domain(map
, set1
);
4748 map
= isl_map_intersect_range(map
, set2
);
4752 __isl_give isl_map
*isl_map_lex_le_map(__isl_take isl_map
*map1
,
4753 __isl_take isl_map
*map2
)
4756 map
= isl_map_lex_le(isl_space_range(isl_map_get_space(map1
)));
4757 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4758 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4762 __isl_give isl_map
*isl_map_lex_lt_map(__isl_take isl_map
*map1
,
4763 __isl_take isl_map
*map2
)
4766 map
= isl_map_lex_lt(isl_space_range(isl_map_get_space(map1
)));
4767 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4768 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4772 __isl_give isl_map
*isl_map_lex_ge_map(__isl_take isl_map
*map1
,
4773 __isl_take isl_map
*map2
)
4776 map
= isl_map_lex_ge(isl_space_range(isl_map_get_space(map1
)));
4777 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4778 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4782 __isl_give isl_map
*isl_map_lex_gt_map(__isl_take isl_map
*map1
,
4783 __isl_take isl_map
*map2
)
4786 map
= isl_map_lex_gt(isl_space_range(isl_map_get_space(map1
)));
4787 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4788 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4792 static __isl_give isl_basic_map
*basic_map_from_basic_set(
4793 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*dim
)
4795 struct isl_basic_map
*bmap
;
4797 bset
= isl_basic_set_cow(bset
);
4801 isl_assert(bset
->ctx
, isl_space_compatible_internal(bset
->dim
, dim
),
4803 isl_space_free(bset
->dim
);
4804 bmap
= bset_to_bmap(bset
);
4806 return isl_basic_map_finalize(bmap
);
4808 isl_basic_set_free(bset
);
4809 isl_space_free(dim
);
4813 __isl_give isl_basic_map
*isl_basic_map_from_basic_set(
4814 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*space
)
4816 return basic_map_from_basic_set(bset
, space
);
4819 /* For a div d = floor(f/m), add the constraint
4823 static int add_upper_div_constraint(__isl_keep isl_basic_map
*bmap
,
4824 unsigned pos
, isl_int
*div
)
4827 unsigned total
= isl_basic_map_total_dim(bmap
);
4829 i
= isl_basic_map_alloc_inequality(bmap
);
4832 isl_seq_cpy(bmap
->ineq
[i
], div
+ 1, 1 + total
);
4833 isl_int_neg(bmap
->ineq
[i
][1 + pos
], div
[0]);
4838 /* For a div d = floor(f/m), add the constraint
4840 * -(f-(m-1)) + m d >= 0
4842 static int add_lower_div_constraint(__isl_keep isl_basic_map
*bmap
,
4843 unsigned pos
, isl_int
*div
)
4846 unsigned total
= isl_basic_map_total_dim(bmap
);
4848 i
= isl_basic_map_alloc_inequality(bmap
);
4851 isl_seq_neg(bmap
->ineq
[i
], div
+ 1, 1 + total
);
4852 isl_int_set(bmap
->ineq
[i
][1 + pos
], div
[0]);
4853 isl_int_add(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], bmap
->ineq
[i
][1 + pos
]);
4854 isl_int_sub_ui(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], 1);
4859 /* For a div d = floor(f/m), add the constraints
4862 * -(f-(m-1)) + m d >= 0
4864 * Note that the second constraint is the negation of
4868 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map
*bmap
,
4869 unsigned pos
, isl_int
*div
)
4871 if (add_upper_div_constraint(bmap
, pos
, div
) < 0)
4873 if (add_lower_div_constraint(bmap
, pos
, div
) < 0)
4878 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set
*bset
,
4879 unsigned pos
, isl_int
*div
)
4881 return isl_basic_map_add_div_constraints_var(bset_to_bmap(bset
),
4885 int isl_basic_map_add_div_constraints(struct isl_basic_map
*bmap
, unsigned div
)
4887 unsigned total
= isl_basic_map_total_dim(bmap
);
4888 unsigned div_pos
= total
- bmap
->n_div
+ div
;
4890 return isl_basic_map_add_div_constraints_var(bmap
, div_pos
,
4894 /* For each known div d = floor(f/m), add the constraints
4897 * -(f-(m-1)) + m d >= 0
4899 * Remove duplicate constraints in case of some these div constraints
4900 * already appear in "bmap".
4902 __isl_give isl_basic_map
*isl_basic_map_add_known_div_constraints(
4903 __isl_take isl_basic_map
*bmap
)
4909 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4913 bmap
= add_known_div_constraints(bmap
);
4914 bmap
= isl_basic_map_remove_duplicate_constraints(bmap
, NULL
, 0);
4915 bmap
= isl_basic_map_finalize(bmap
);
4919 /* Add the div constraint of sign "sign" for div "div" of "bmap".
4921 * In particular, if this div is of the form d = floor(f/m),
4922 * then add the constraint
4926 * if sign < 0 or the constraint
4928 * -(f-(m-1)) + m d >= 0
4932 int isl_basic_map_add_div_constraint(__isl_keep isl_basic_map
*bmap
,
4933 unsigned div
, int sign
)
4941 total
= isl_basic_map_total_dim(bmap
);
4942 div_pos
= total
- bmap
->n_div
+ div
;
4945 return add_upper_div_constraint(bmap
, div_pos
, bmap
->div
[div
]);
4947 return add_lower_div_constraint(bmap
, div_pos
, bmap
->div
[div
]);
4950 int isl_basic_set_add_div_constraints(struct isl_basic_set
*bset
, unsigned div
)
4952 return isl_basic_map_add_div_constraints(bset
, div
);
4955 struct isl_basic_set
*isl_basic_map_underlying_set(
4956 struct isl_basic_map
*bmap
)
4960 if (bmap
->dim
->nparam
== 0 && bmap
->dim
->n_in
== 0 &&
4962 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_in
) &&
4963 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_out
))
4964 return bset_from_bmap(bmap
);
4965 bmap
= isl_basic_map_cow(bmap
);
4968 bmap
->dim
= isl_space_underlying(bmap
->dim
, bmap
->n_div
);
4971 bmap
->extra
-= bmap
->n_div
;
4973 bmap
= isl_basic_map_finalize(bmap
);
4974 return bset_from_bmap(bmap
);
4976 isl_basic_map_free(bmap
);
4980 __isl_give isl_basic_set
*isl_basic_set_underlying_set(
4981 __isl_take isl_basic_set
*bset
)
4983 return isl_basic_map_underlying_set(bset_to_bmap(bset
));
4986 /* Replace each element in "list" by the result of applying
4987 * isl_basic_map_underlying_set to the element.
4989 __isl_give isl_basic_set_list
*isl_basic_map_list_underlying_set(
4990 __isl_take isl_basic_map_list
*list
)
4997 n
= isl_basic_map_list_n_basic_map(list
);
4998 for (i
= 0; i
< n
; ++i
) {
4999 isl_basic_map
*bmap
;
5000 isl_basic_set
*bset
;
5002 bmap
= isl_basic_map_list_get_basic_map(list
, i
);
5003 bset
= isl_basic_set_underlying_set(bmap
);
5004 list
= isl_basic_set_list_set_basic_set(list
, i
, bset
);
5010 struct isl_basic_map
*isl_basic_map_overlying_set(
5011 struct isl_basic_set
*bset
, struct isl_basic_map
*like
)
5013 struct isl_basic_map
*bmap
;
5014 struct isl_ctx
*ctx
;
5021 isl_assert(ctx
, bset
->n_div
== 0, goto error
);
5022 isl_assert(ctx
, isl_basic_set_n_param(bset
) == 0, goto error
);
5023 isl_assert(ctx
, bset
->dim
->n_out
== isl_basic_map_total_dim(like
),
5025 if (like
->n_div
== 0) {
5026 isl_space
*space
= isl_basic_map_get_space(like
);
5027 isl_basic_map_free(like
);
5028 return isl_basic_map_reset_space(bset
, space
);
5030 bset
= isl_basic_set_cow(bset
);
5033 total
= bset
->dim
->n_out
+ bset
->extra
;
5034 bmap
= bset_to_bmap(bset
);
5035 isl_space_free(bmap
->dim
);
5036 bmap
->dim
= isl_space_copy(like
->dim
);
5039 bmap
->n_div
= like
->n_div
;
5040 bmap
->extra
+= like
->n_div
;
5044 ltotal
= total
- bmap
->extra
+ like
->extra
;
5047 bmap
->block2
= isl_blk_extend(ctx
, bmap
->block2
,
5048 bmap
->extra
* (1 + 1 + total
));
5049 if (isl_blk_is_error(bmap
->block2
))
5051 div
= isl_realloc_array(ctx
, bmap
->div
, isl_int
*, bmap
->extra
);
5055 for (i
= 0; i
< bmap
->extra
; ++i
)
5056 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + 1 + total
);
5057 for (i
= 0; i
< like
->n_div
; ++i
) {
5058 isl_seq_cpy(bmap
->div
[i
], like
->div
[i
], 1 + 1 + ltotal
);
5059 isl_seq_clr(bmap
->div
[i
]+1+1+ltotal
, total
- ltotal
);
5061 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
5063 isl_basic_map_free(like
);
5064 bmap
= isl_basic_map_simplify(bmap
);
5065 bmap
= isl_basic_map_finalize(bmap
);
5068 isl_basic_map_free(like
);
5069 isl_basic_set_free(bset
);
5073 struct isl_basic_set
*isl_basic_set_from_underlying_set(
5074 struct isl_basic_set
*bset
, struct isl_basic_set
*like
)
5076 return bset_from_bmap(isl_basic_map_overlying_set(bset
,
5077 bset_to_bmap(like
)));
5080 struct isl_set
*isl_set_from_underlying_set(
5081 struct isl_set
*set
, struct isl_basic_set
*like
)
5087 isl_assert(set
->ctx
, set
->dim
->n_out
== isl_basic_set_total_dim(like
),
5089 if (isl_space_is_equal(set
->dim
, like
->dim
) && like
->n_div
== 0) {
5090 isl_basic_set_free(like
);
5093 set
= isl_set_cow(set
);
5096 for (i
= 0; i
< set
->n
; ++i
) {
5097 set
->p
[i
] = isl_basic_set_from_underlying_set(set
->p
[i
],
5098 isl_basic_set_copy(like
));
5102 isl_space_free(set
->dim
);
5103 set
->dim
= isl_space_copy(like
->dim
);
5106 isl_basic_set_free(like
);
5109 isl_basic_set_free(like
);
5114 struct isl_set
*isl_map_underlying_set(struct isl_map
*map
)
5118 map
= isl_map_cow(map
);
5121 map
->dim
= isl_space_cow(map
->dim
);
5125 for (i
= 1; i
< map
->n
; ++i
)
5126 isl_assert(map
->ctx
, map
->p
[0]->n_div
== map
->p
[i
]->n_div
,
5128 for (i
= 0; i
< map
->n
; ++i
) {
5129 map
->p
[i
] = bset_to_bmap(
5130 isl_basic_map_underlying_set(map
->p
[i
]));
5135 map
->dim
= isl_space_underlying(map
->dim
, 0);
5137 isl_space_free(map
->dim
);
5138 map
->dim
= isl_space_copy(map
->p
[0]->dim
);
5142 return set_from_map(map
);
5148 struct isl_set
*isl_set_to_underlying_set(struct isl_set
*set
)
5150 return set_from_map(isl_map_underlying_set(set_to_map(set
)));
5153 /* Replace the space of "bmap" by "space".
5155 * If the space of "bmap" is identical to "space" (including the identifiers
5156 * of the input and output dimensions), then simply return the original input.
5158 __isl_give isl_basic_map
*isl_basic_map_reset_space(
5159 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*space
)
5165 equal
= isl_space_is_equal(bmap
->dim
, space
);
5166 if (equal
>= 0 && equal
)
5167 equal
= isl_space_match(bmap
->dim
, isl_dim_in
,
5169 if (equal
>= 0 && equal
)
5170 equal
= isl_space_match(bmap
->dim
, isl_dim_out
,
5171 space
, isl_dim_out
);
5175 isl_space_free(space
);
5178 bmap
= isl_basic_map_cow(bmap
);
5179 if (!bmap
|| !space
)
5182 isl_space_free(bmap
->dim
);
5185 bmap
= isl_basic_map_finalize(bmap
);
5189 isl_basic_map_free(bmap
);
5190 isl_space_free(space
);
5194 __isl_give isl_basic_set
*isl_basic_set_reset_space(
5195 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*dim
)
5197 return bset_from_bmap(isl_basic_map_reset_space(bset_to_bmap(bset
),
5201 __isl_give isl_map
*isl_map_reset_space(__isl_take isl_map
*map
,
5202 __isl_take isl_space
*dim
)
5206 map
= isl_map_cow(map
);
5210 for (i
= 0; i
< map
->n
; ++i
) {
5211 map
->p
[i
] = isl_basic_map_reset_space(map
->p
[i
],
5212 isl_space_copy(dim
));
5216 isl_space_free(map
->dim
);
5222 isl_space_free(dim
);
5226 __isl_give isl_set
*isl_set_reset_space(__isl_take isl_set
*set
,
5227 __isl_take isl_space
*dim
)
5229 return set_from_map(isl_map_reset_space(set_to_map(set
), dim
));
5232 /* Compute the parameter domain of the given basic set.
5234 __isl_give isl_basic_set
*isl_basic_set_params(__isl_take isl_basic_set
*bset
)
5240 is_params
= isl_basic_set_is_params(bset
);
5242 return isl_basic_set_free(bset
);
5246 n
= isl_basic_set_dim(bset
, isl_dim_set
);
5247 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
5248 space
= isl_basic_set_get_space(bset
);
5249 space
= isl_space_params(space
);
5250 bset
= isl_basic_set_reset_space(bset
, space
);
5254 /* Construct a zero-dimensional basic set with the given parameter domain.
5256 __isl_give isl_basic_set
*isl_basic_set_from_params(
5257 __isl_take isl_basic_set
*bset
)
5260 space
= isl_basic_set_get_space(bset
);
5261 space
= isl_space_set_from_params(space
);
5262 bset
= isl_basic_set_reset_space(bset
, space
);
5266 /* Compute the parameter domain of the given set.
5268 __isl_give isl_set
*isl_set_params(__isl_take isl_set
*set
)
5273 if (isl_set_is_params(set
))
5276 n
= isl_set_dim(set
, isl_dim_set
);
5277 set
= isl_set_project_out(set
, isl_dim_set
, 0, n
);
5278 space
= isl_set_get_space(set
);
5279 space
= isl_space_params(space
);
5280 set
= isl_set_reset_space(set
, space
);
5284 /* Construct a zero-dimensional set with the given parameter domain.
5286 __isl_give isl_set
*isl_set_from_params(__isl_take isl_set
*set
)
5289 space
= isl_set_get_space(set
);
5290 space
= isl_space_set_from_params(space
);
5291 set
= isl_set_reset_space(set
, space
);
5295 /* Compute the parameter domain of the given map.
5297 __isl_give isl_set
*isl_map_params(__isl_take isl_map
*map
)
5302 n
= isl_map_dim(map
, isl_dim_in
);
5303 map
= isl_map_project_out(map
, isl_dim_in
, 0, n
);
5304 n
= isl_map_dim(map
, isl_dim_out
);
5305 map
= isl_map_project_out(map
, isl_dim_out
, 0, n
);
5306 space
= isl_map_get_space(map
);
5307 space
= isl_space_params(space
);
5308 map
= isl_map_reset_space(map
, space
);
5312 struct isl_basic_set
*isl_basic_map_domain(struct isl_basic_map
*bmap
)
5319 space
= isl_space_domain(isl_basic_map_get_space(bmap
));
5321 n_out
= isl_basic_map_n_out(bmap
);
5322 bmap
= isl_basic_map_project_out(bmap
, isl_dim_out
, 0, n_out
);
5324 return isl_basic_map_reset_space(bmap
, space
);
5327 isl_bool
isl_basic_map_may_be_set(__isl_keep isl_basic_map
*bmap
)
5330 return isl_bool_error
;
5331 return isl_space_may_be_set(bmap
->dim
);
5334 /* Is this basic map actually a set?
5335 * Users should never call this function. Outside of isl,
5336 * the type should indicate whether something is a set or a map.
5338 isl_bool
isl_basic_map_is_set(__isl_keep isl_basic_map
*bmap
)
5341 return isl_bool_error
;
5342 return isl_space_is_set(bmap
->dim
);
5345 struct isl_basic_set
*isl_basic_map_range(struct isl_basic_map
*bmap
)
5349 is_set
= isl_basic_map_is_set(bmap
);
5354 return isl_basic_map_domain(isl_basic_map_reverse(bmap
));
5356 isl_basic_map_free(bmap
);
5360 __isl_give isl_basic_map
*isl_basic_map_domain_map(
5361 __isl_take isl_basic_map
*bmap
)
5365 isl_basic_map
*domain
;
5366 int nparam
, n_in
, n_out
;
5368 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5369 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5370 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5372 dim
= isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap
)));
5373 domain
= isl_basic_map_universe(dim
);
5375 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
5376 bmap
= isl_basic_map_apply_range(bmap
, domain
);
5377 bmap
= isl_basic_map_extend_constraints(bmap
, n_in
, 0);
5379 for (i
= 0; i
< n_in
; ++i
)
5380 bmap
= isl_basic_map_equate(bmap
, isl_dim_in
, i
,
5383 bmap
= isl_basic_map_gauss(bmap
, NULL
);
5384 return isl_basic_map_finalize(bmap
);
5387 __isl_give isl_basic_map
*isl_basic_map_range_map(
5388 __isl_take isl_basic_map
*bmap
)
5392 isl_basic_map
*range
;
5393 int nparam
, n_in
, n_out
;
5395 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5396 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5397 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5399 dim
= isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap
)));
5400 range
= isl_basic_map_universe(dim
);
5402 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
5403 bmap
= isl_basic_map_apply_range(bmap
, range
);
5404 bmap
= isl_basic_map_extend_constraints(bmap
, n_out
, 0);
5406 for (i
= 0; i
< n_out
; ++i
)
5407 bmap
= isl_basic_map_equate(bmap
, isl_dim_in
, n_in
+ i
,
5410 bmap
= isl_basic_map_gauss(bmap
, NULL
);
5411 return isl_basic_map_finalize(bmap
);
5414 int isl_map_may_be_set(__isl_keep isl_map
*map
)
5418 return isl_space_may_be_set(map
->dim
);
5421 /* Is this map actually a set?
5422 * Users should never call this function. Outside of isl,
5423 * the type should indicate whether something is a set or a map.
5425 int isl_map_is_set(__isl_keep isl_map
*map
)
5429 return isl_space_is_set(map
->dim
);
5432 struct isl_set
*isl_map_range(struct isl_map
*map
)
5435 struct isl_set
*set
;
5439 if (isl_map_is_set(map
))
5440 return set_from_map(map
);
5442 map
= isl_map_cow(map
);
5446 set
= set_from_map(map
);
5447 set
->dim
= isl_space_range(set
->dim
);
5450 for (i
= 0; i
< map
->n
; ++i
) {
5451 set
->p
[i
] = isl_basic_map_range(map
->p
[i
]);
5455 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
5456 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
5463 __isl_give isl_map
*isl_map_domain_map(__isl_take isl_map
*map
)
5467 map
= isl_map_cow(map
);
5471 map
->dim
= isl_space_domain_map(map
->dim
);
5474 for (i
= 0; i
< map
->n
; ++i
) {
5475 map
->p
[i
] = isl_basic_map_domain_map(map
->p
[i
]);
5479 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
5480 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5487 __isl_give isl_map
*isl_map_range_map(__isl_take isl_map
*map
)
5490 isl_space
*range_dim
;
5492 map
= isl_map_cow(map
);
5496 range_dim
= isl_space_range(isl_map_get_space(map
));
5497 range_dim
= isl_space_from_range(range_dim
);
5498 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
5499 map
->dim
= isl_space_join(map
->dim
, range_dim
);
5502 for (i
= 0; i
< map
->n
; ++i
) {
5503 map
->p
[i
] = isl_basic_map_range_map(map
->p
[i
]);
5507 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
5508 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5515 /* Given a wrapped map of the form A[B -> C],
5516 * return the map A[B -> C] -> B.
5518 __isl_give isl_map
*isl_set_wrapped_domain_map(__isl_take isl_set
*set
)
5525 if (!isl_set_has_tuple_id(set
))
5526 return isl_map_domain_map(isl_set_unwrap(set
));
5528 id
= isl_set_get_tuple_id(set
);
5529 map
= isl_map_domain_map(isl_set_unwrap(set
));
5530 map
= isl_map_set_tuple_id(map
, isl_dim_in
, id
);
5535 __isl_give isl_map
*isl_map_from_set(__isl_take isl_set
*set
,
5536 __isl_take isl_space
*dim
)
5539 struct isl_map
*map
= NULL
;
5541 set
= isl_set_cow(set
);
5544 isl_assert(set
->ctx
, isl_space_compatible_internal(set
->dim
, dim
),
5546 map
= set_to_map(set
);
5547 for (i
= 0; i
< set
->n
; ++i
) {
5548 map
->p
[i
] = basic_map_from_basic_set(
5549 set
->p
[i
], isl_space_copy(dim
));
5553 isl_space_free(map
->dim
);
5557 isl_space_free(dim
);
5562 __isl_give isl_basic_map
*isl_basic_map_from_domain(
5563 __isl_take isl_basic_set
*bset
)
5565 return isl_basic_map_reverse(isl_basic_map_from_range(bset
));
5568 __isl_give isl_basic_map
*isl_basic_map_from_range(
5569 __isl_take isl_basic_set
*bset
)
5572 space
= isl_basic_set_get_space(bset
);
5573 space
= isl_space_from_range(space
);
5574 bset
= isl_basic_set_reset_space(bset
, space
);
5575 return bset_to_bmap(bset
);
5578 /* Create a relation with the given set as range.
5579 * The domain of the created relation is a zero-dimensional
5580 * flat anonymous space.
5582 __isl_give isl_map
*isl_map_from_range(__isl_take isl_set
*set
)
5585 space
= isl_set_get_space(set
);
5586 space
= isl_space_from_range(space
);
5587 set
= isl_set_reset_space(set
, space
);
5588 return set_to_map(set
);
5591 /* Create a relation with the given set as domain.
5592 * The range of the created relation is a zero-dimensional
5593 * flat anonymous space.
5595 __isl_give isl_map
*isl_map_from_domain(__isl_take isl_set
*set
)
5597 return isl_map_reverse(isl_map_from_range(set
));
5600 __isl_give isl_basic_map
*isl_basic_map_from_domain_and_range(
5601 __isl_take isl_basic_set
*domain
, __isl_take isl_basic_set
*range
)
5603 return isl_basic_map_apply_range(isl_basic_map_reverse(domain
), range
);
5606 __isl_give isl_map
*isl_map_from_domain_and_range(__isl_take isl_set
*domain
,
5607 __isl_take isl_set
*range
)
5609 return isl_map_apply_range(isl_map_reverse(domain
), range
);
5612 /* Return a newly allocated isl_map with given space and flags and
5613 * room for "n" basic maps.
5614 * Make sure that all cached information is cleared.
5616 __isl_give isl_map
*isl_map_alloc_space(__isl_take isl_space
*space
, int n
,
5619 struct isl_map
*map
;
5624 isl_die(space
->ctx
, isl_error_internal
,
5625 "negative number of basic maps", goto error
);
5626 map
= isl_calloc(space
->ctx
, struct isl_map
,
5627 sizeof(struct isl_map
) +
5628 (n
- 1) * sizeof(struct isl_basic_map
*));
5632 map
->ctx
= space
->ctx
;
5633 isl_ctx_ref(map
->ctx
);
5641 isl_space_free(space
);
5645 struct isl_map
*isl_map_alloc(struct isl_ctx
*ctx
,
5646 unsigned nparam
, unsigned in
, unsigned out
, int n
,
5649 struct isl_map
*map
;
5652 dims
= isl_space_alloc(ctx
, nparam
, in
, out
);
5656 map
= isl_map_alloc_space(dims
, n
, flags
);
5660 __isl_give isl_basic_map
*isl_basic_map_empty(__isl_take isl_space
*dim
)
5662 struct isl_basic_map
*bmap
;
5663 bmap
= isl_basic_map_alloc_space(dim
, 0, 1, 0);
5664 bmap
= isl_basic_map_set_to_empty(bmap
);
5668 __isl_give isl_basic_set
*isl_basic_set_empty(__isl_take isl_space
*dim
)
5670 struct isl_basic_set
*bset
;
5671 bset
= isl_basic_set_alloc_space(dim
, 0, 1, 0);
5672 bset
= isl_basic_set_set_to_empty(bset
);
5676 __isl_give isl_basic_map
*isl_basic_map_universe(__isl_take isl_space
*dim
)
5678 struct isl_basic_map
*bmap
;
5679 bmap
= isl_basic_map_alloc_space(dim
, 0, 0, 0);
5680 bmap
= isl_basic_map_finalize(bmap
);
5684 __isl_give isl_basic_set
*isl_basic_set_universe(__isl_take isl_space
*dim
)
5686 struct isl_basic_set
*bset
;
5687 bset
= isl_basic_set_alloc_space(dim
, 0, 0, 0);
5688 bset
= isl_basic_set_finalize(bset
);
5692 __isl_give isl_basic_map
*isl_basic_map_nat_universe(__isl_take isl_space
*dim
)
5695 unsigned total
= isl_space_dim(dim
, isl_dim_all
);
5696 isl_basic_map
*bmap
;
5698 bmap
= isl_basic_map_alloc_space(dim
, 0, 0, total
);
5699 for (i
= 0; i
< total
; ++i
) {
5700 int k
= isl_basic_map_alloc_inequality(bmap
);
5703 isl_seq_clr(bmap
->ineq
[k
], 1 + total
);
5704 isl_int_set_si(bmap
->ineq
[k
][1 + i
], 1);
5708 isl_basic_map_free(bmap
);
5712 __isl_give isl_basic_set
*isl_basic_set_nat_universe(__isl_take isl_space
*dim
)
5714 return isl_basic_map_nat_universe(dim
);
5717 __isl_give isl_map
*isl_map_nat_universe(__isl_take isl_space
*dim
)
5719 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim
));
5722 __isl_give isl_set
*isl_set_nat_universe(__isl_take isl_space
*dim
)
5724 return isl_map_nat_universe(dim
);
5727 __isl_give isl_map
*isl_map_empty(__isl_take isl_space
*dim
)
5729 return isl_map_alloc_space(dim
, 0, ISL_MAP_DISJOINT
);
5732 __isl_give isl_set
*isl_set_empty(__isl_take isl_space
*dim
)
5734 return isl_set_alloc_space(dim
, 0, ISL_MAP_DISJOINT
);
5737 __isl_give isl_map
*isl_map_universe(__isl_take isl_space
*dim
)
5739 struct isl_map
*map
;
5742 map
= isl_map_alloc_space(isl_space_copy(dim
), 1, ISL_MAP_DISJOINT
);
5743 map
= isl_map_add_basic_map(map
, isl_basic_map_universe(dim
));
5747 __isl_give isl_set
*isl_set_universe(__isl_take isl_space
*dim
)
5749 struct isl_set
*set
;
5752 set
= isl_set_alloc_space(isl_space_copy(dim
), 1, ISL_MAP_DISJOINT
);
5753 set
= isl_set_add_basic_set(set
, isl_basic_set_universe(dim
));
5757 struct isl_map
*isl_map_dup(struct isl_map
*map
)
5760 struct isl_map
*dup
;
5764 dup
= isl_map_alloc_space(isl_space_copy(map
->dim
), map
->n
, map
->flags
);
5765 for (i
= 0; i
< map
->n
; ++i
)
5766 dup
= isl_map_add_basic_map(dup
, isl_basic_map_copy(map
->p
[i
]));
5770 __isl_give isl_map
*isl_map_add_basic_map(__isl_take isl_map
*map
,
5771 __isl_take isl_basic_map
*bmap
)
5775 if (isl_basic_map_plain_is_empty(bmap
)) {
5776 isl_basic_map_free(bmap
);
5779 isl_assert(map
->ctx
, isl_space_is_equal(map
->dim
, bmap
->dim
), goto error
);
5780 isl_assert(map
->ctx
, map
->n
< map
->size
, goto error
);
5781 map
->p
[map
->n
] = bmap
;
5783 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5789 isl_basic_map_free(bmap
);
5793 __isl_null isl_map
*isl_map_free(__isl_take isl_map
*map
)
5804 isl_ctx_deref(map
->ctx
);
5805 for (i
= 0; i
< map
->n
; ++i
)
5806 isl_basic_map_free(map
->p
[i
]);
5807 isl_space_free(map
->dim
);
5813 static struct isl_basic_map
*isl_basic_map_fix_pos_si(
5814 struct isl_basic_map
*bmap
, unsigned pos
, int value
)
5818 bmap
= isl_basic_map_cow(bmap
);
5819 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
5820 j
= isl_basic_map_alloc_equality(bmap
);
5823 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
5824 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
5825 isl_int_set_si(bmap
->eq
[j
][0], value
);
5826 bmap
= isl_basic_map_simplify(bmap
);
5827 return isl_basic_map_finalize(bmap
);
5829 isl_basic_map_free(bmap
);
5833 static __isl_give isl_basic_map
*isl_basic_map_fix_pos(
5834 __isl_take isl_basic_map
*bmap
, unsigned pos
, isl_int value
)
5838 bmap
= isl_basic_map_cow(bmap
);
5839 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
5840 j
= isl_basic_map_alloc_equality(bmap
);
5843 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
5844 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
5845 isl_int_set(bmap
->eq
[j
][0], value
);
5846 bmap
= isl_basic_map_simplify(bmap
);
5847 return isl_basic_map_finalize(bmap
);
5849 isl_basic_map_free(bmap
);
5853 struct isl_basic_map
*isl_basic_map_fix_si(struct isl_basic_map
*bmap
,
5854 enum isl_dim_type type
, unsigned pos
, int value
)
5856 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
5857 return isl_basic_map_free(bmap
);
5858 return isl_basic_map_fix_pos_si(bmap
,
5859 isl_basic_map_offset(bmap
, type
) + pos
, value
);
5862 __isl_give isl_basic_map
*isl_basic_map_fix(__isl_take isl_basic_map
*bmap
,
5863 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5865 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
5866 return isl_basic_map_free(bmap
);
5867 return isl_basic_map_fix_pos(bmap
,
5868 isl_basic_map_offset(bmap
, type
) + pos
, value
);
5871 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
5872 * to be equal to "v".
5874 __isl_give isl_basic_map
*isl_basic_map_fix_val(__isl_take isl_basic_map
*bmap
,
5875 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5879 if (!isl_val_is_int(v
))
5880 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
5881 "expecting integer value", goto error
);
5882 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
5884 pos
+= isl_basic_map_offset(bmap
, type
);
5885 bmap
= isl_basic_map_fix_pos(bmap
, pos
, v
->n
);
5889 isl_basic_map_free(bmap
);
5894 /* Fix the value of the variable at position "pos" of type "type" of "bset"
5895 * to be equal to "v".
5897 __isl_give isl_basic_set
*isl_basic_set_fix_val(__isl_take isl_basic_set
*bset
,
5898 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5900 return isl_basic_map_fix_val(bset
, type
, pos
, v
);
5903 struct isl_basic_set
*isl_basic_set_fix_si(struct isl_basic_set
*bset
,
5904 enum isl_dim_type type
, unsigned pos
, int value
)
5906 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset
),
5910 __isl_give isl_basic_set
*isl_basic_set_fix(__isl_take isl_basic_set
*bset
,
5911 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5913 return bset_from_bmap(isl_basic_map_fix(bset_to_bmap(bset
),
5917 struct isl_basic_map
*isl_basic_map_fix_input_si(struct isl_basic_map
*bmap
,
5918 unsigned input
, int value
)
5920 return isl_basic_map_fix_si(bmap
, isl_dim_in
, input
, value
);
5923 struct isl_basic_set
*isl_basic_set_fix_dim_si(struct isl_basic_set
*bset
,
5924 unsigned dim
, int value
)
5926 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset
),
5927 isl_dim_set
, dim
, value
));
5930 static int remove_if_empty(__isl_keep isl_map
*map
, int i
)
5932 int empty
= isl_basic_map_plain_is_empty(map
->p
[i
]);
5939 isl_basic_map_free(map
->p
[i
]);
5940 if (i
!= map
->n
- 1) {
5941 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5942 map
->p
[i
] = map
->p
[map
->n
- 1];
5949 /* Perform "fn" on each basic map of "map", where we may not be holding
5950 * the only reference to "map".
5951 * In particular, "fn" should be a semantics preserving operation
5952 * that we want to apply to all copies of "map". We therefore need
5953 * to be careful not to modify "map" in a way that breaks "map"
5954 * in case anything goes wrong.
5956 __isl_give isl_map
*isl_map_inline_foreach_basic_map(__isl_take isl_map
*map
,
5957 __isl_give isl_basic_map
*(*fn
)(__isl_take isl_basic_map
*bmap
))
5959 struct isl_basic_map
*bmap
;
5965 for (i
= map
->n
- 1; i
>= 0; --i
) {
5966 bmap
= isl_basic_map_copy(map
->p
[i
]);
5970 isl_basic_map_free(map
->p
[i
]);
5972 if (remove_if_empty(map
, i
) < 0)
5982 struct isl_map
*isl_map_fix_si(struct isl_map
*map
,
5983 enum isl_dim_type type
, unsigned pos
, int value
)
5987 map
= isl_map_cow(map
);
5991 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
5992 for (i
= map
->n
- 1; i
>= 0; --i
) {
5993 map
->p
[i
] = isl_basic_map_fix_si(map
->p
[i
], type
, pos
, value
);
5994 if (remove_if_empty(map
, i
) < 0)
5997 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6004 __isl_give isl_set
*isl_set_fix_si(__isl_take isl_set
*set
,
6005 enum isl_dim_type type
, unsigned pos
, int value
)
6007 return set_from_map(isl_map_fix_si(set_to_map(set
), type
, pos
, value
));
6010 __isl_give isl_map
*isl_map_fix(__isl_take isl_map
*map
,
6011 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6015 map
= isl_map_cow(map
);
6019 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
6020 for (i
= 0; i
< map
->n
; ++i
) {
6021 map
->p
[i
] = isl_basic_map_fix(map
->p
[i
], type
, pos
, value
);
6025 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6032 __isl_give isl_set
*isl_set_fix(__isl_take isl_set
*set
,
6033 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6035 return set_from_map(isl_map_fix(set_to_map(set
), type
, pos
, value
));
6038 /* Fix the value of the variable at position "pos" of type "type" of "map"
6039 * to be equal to "v".
6041 __isl_give isl_map
*isl_map_fix_val(__isl_take isl_map
*map
,
6042 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6046 map
= isl_map_cow(map
);
6050 if (!isl_val_is_int(v
))
6051 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
6052 "expecting integer value", goto error
);
6053 if (pos
>= isl_map_dim(map
, type
))
6054 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
6055 "index out of bounds", goto error
);
6056 for (i
= map
->n
- 1; i
>= 0; --i
) {
6057 map
->p
[i
] = isl_basic_map_fix_val(map
->p
[i
], type
, pos
,
6059 if (remove_if_empty(map
, i
) < 0)
6062 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6071 /* Fix the value of the variable at position "pos" of type "type" of "set"
6072 * to be equal to "v".
6074 __isl_give isl_set
*isl_set_fix_val(__isl_take isl_set
*set
,
6075 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6077 return isl_map_fix_val(set
, type
, pos
, v
);
6080 struct isl_map
*isl_map_fix_input_si(struct isl_map
*map
,
6081 unsigned input
, int value
)
6083 return isl_map_fix_si(map
, isl_dim_in
, input
, value
);
6086 struct isl_set
*isl_set_fix_dim_si(struct isl_set
*set
, unsigned dim
, int value
)
6088 return set_from_map(isl_map_fix_si(set_to_map(set
),
6089 isl_dim_set
, dim
, value
));
6092 static __isl_give isl_basic_map
*basic_map_bound_si(
6093 __isl_take isl_basic_map
*bmap
,
6094 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
6098 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6099 return isl_basic_map_free(bmap
);
6100 pos
+= isl_basic_map_offset(bmap
, type
);
6101 bmap
= isl_basic_map_cow(bmap
);
6102 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
6103 j
= isl_basic_map_alloc_inequality(bmap
);
6106 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
6108 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
6109 isl_int_set_si(bmap
->ineq
[j
][0], value
);
6111 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
6112 isl_int_set_si(bmap
->ineq
[j
][0], -value
);
6114 bmap
= isl_basic_map_simplify(bmap
);
6115 return isl_basic_map_finalize(bmap
);
6117 isl_basic_map_free(bmap
);
6121 __isl_give isl_basic_map
*isl_basic_map_lower_bound_si(
6122 __isl_take isl_basic_map
*bmap
,
6123 enum isl_dim_type type
, unsigned pos
, int value
)
6125 return basic_map_bound_si(bmap
, type
, pos
, value
, 0);
6128 /* Constrain the values of the given dimension to be no greater than "value".
6130 __isl_give isl_basic_map
*isl_basic_map_upper_bound_si(
6131 __isl_take isl_basic_map
*bmap
,
6132 enum isl_dim_type type
, unsigned pos
, int value
)
6134 return basic_map_bound_si(bmap
, type
, pos
, value
, 1);
6137 struct isl_basic_set
*isl_basic_set_lower_bound_dim(struct isl_basic_set
*bset
,
6138 unsigned dim
, isl_int value
)
6142 bset
= isl_basic_set_cow(bset
);
6143 bset
= isl_basic_set_extend_constraints(bset
, 0, 1);
6144 j
= isl_basic_set_alloc_inequality(bset
);
6147 isl_seq_clr(bset
->ineq
[j
], 1 + isl_basic_set_total_dim(bset
));
6148 isl_int_set_si(bset
->ineq
[j
][1 + isl_basic_set_n_param(bset
) + dim
], 1);
6149 isl_int_neg(bset
->ineq
[j
][0], value
);
6150 bset
= isl_basic_set_simplify(bset
);
6151 return isl_basic_set_finalize(bset
);
6153 isl_basic_set_free(bset
);
6157 static __isl_give isl_map
*map_bound_si(__isl_take isl_map
*map
,
6158 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
6162 map
= isl_map_cow(map
);
6166 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
6167 for (i
= 0; i
< map
->n
; ++i
) {
6168 map
->p
[i
] = basic_map_bound_si(map
->p
[i
],
6169 type
, pos
, value
, upper
);
6173 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6180 __isl_give isl_map
*isl_map_lower_bound_si(__isl_take isl_map
*map
,
6181 enum isl_dim_type type
, unsigned pos
, int value
)
6183 return map_bound_si(map
, type
, pos
, value
, 0);
6186 __isl_give isl_map
*isl_map_upper_bound_si(__isl_take isl_map
*map
,
6187 enum isl_dim_type type
, unsigned pos
, int value
)
6189 return map_bound_si(map
, type
, pos
, value
, 1);
6192 __isl_give isl_set
*isl_set_lower_bound_si(__isl_take isl_set
*set
,
6193 enum isl_dim_type type
, unsigned pos
, int value
)
6195 return set_from_map(isl_map_lower_bound_si(set_to_map(set
),
6199 __isl_give isl_set
*isl_set_upper_bound_si(__isl_take isl_set
*set
,
6200 enum isl_dim_type type
, unsigned pos
, int value
)
6202 return isl_map_upper_bound_si(set
, type
, pos
, value
);
6205 /* Bound the given variable of "bmap" from below (or above is "upper"
6206 * is set) to "value".
6208 static __isl_give isl_basic_map
*basic_map_bound(
6209 __isl_take isl_basic_map
*bmap
,
6210 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
6214 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6215 return isl_basic_map_free(bmap
);
6216 pos
+= isl_basic_map_offset(bmap
, type
);
6217 bmap
= isl_basic_map_cow(bmap
);
6218 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
6219 j
= isl_basic_map_alloc_inequality(bmap
);
6222 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
6224 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
6225 isl_int_set(bmap
->ineq
[j
][0], value
);
6227 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
6228 isl_int_neg(bmap
->ineq
[j
][0], value
);
6230 bmap
= isl_basic_map_simplify(bmap
);
6231 return isl_basic_map_finalize(bmap
);
6233 isl_basic_map_free(bmap
);
6237 /* Bound the given variable of "map" from below (or above is "upper"
6238 * is set) to "value".
6240 static __isl_give isl_map
*map_bound(__isl_take isl_map
*map
,
6241 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
6245 map
= isl_map_cow(map
);
6249 if (pos
>= isl_map_dim(map
, type
))
6250 isl_die(map
->ctx
, isl_error_invalid
,
6251 "index out of bounds", goto error
);
6252 for (i
= map
->n
- 1; i
>= 0; --i
) {
6253 map
->p
[i
] = basic_map_bound(map
->p
[i
], type
, pos
, value
, upper
);
6254 if (remove_if_empty(map
, i
) < 0)
6257 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6264 __isl_give isl_map
*isl_map_lower_bound(__isl_take isl_map
*map
,
6265 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6267 return map_bound(map
, type
, pos
, value
, 0);
6270 __isl_give isl_map
*isl_map_upper_bound(__isl_take isl_map
*map
,
6271 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6273 return map_bound(map
, type
, pos
, value
, 1);
6276 __isl_give isl_set
*isl_set_lower_bound(__isl_take isl_set
*set
,
6277 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6279 return isl_map_lower_bound(set
, type
, pos
, value
);
6282 __isl_give isl_set
*isl_set_upper_bound(__isl_take isl_set
*set
,
6283 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6285 return isl_map_upper_bound(set
, type
, pos
, value
);
6288 /* Force the values of the variable at position "pos" of type "type" of "set"
6289 * to be no smaller than "value".
6291 __isl_give isl_set
*isl_set_lower_bound_val(__isl_take isl_set
*set
,
6292 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
6296 if (!isl_val_is_int(value
))
6297 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
6298 "expecting integer value", goto error
);
6299 set
= isl_set_lower_bound(set
, type
, pos
, value
->n
);
6300 isl_val_free(value
);
6303 isl_val_free(value
);
6308 /* Force the values of the variable at position "pos" of type "type" of "set"
6309 * to be no greater than "value".
6311 __isl_give isl_set
*isl_set_upper_bound_val(__isl_take isl_set
*set
,
6312 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
6316 if (!isl_val_is_int(value
))
6317 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
6318 "expecting integer value", goto error
);
6319 set
= isl_set_upper_bound(set
, type
, pos
, value
->n
);
6320 isl_val_free(value
);
6323 isl_val_free(value
);
6328 struct isl_set
*isl_set_lower_bound_dim(struct isl_set
*set
, unsigned dim
,
6333 set
= isl_set_cow(set
);
6337 isl_assert(set
->ctx
, dim
< isl_set_n_dim(set
), goto error
);
6338 for (i
= 0; i
< set
->n
; ++i
) {
6339 set
->p
[i
] = isl_basic_set_lower_bound_dim(set
->p
[i
], dim
, value
);
6349 struct isl_map
*isl_map_reverse(struct isl_map
*map
)
6353 map
= isl_map_cow(map
);
6357 map
->dim
= isl_space_reverse(map
->dim
);
6360 for (i
= 0; i
< map
->n
; ++i
) {
6361 map
->p
[i
] = isl_basic_map_reverse(map
->p
[i
]);
6365 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6373 #define TYPE isl_pw_multi_aff
6375 #define SUFFIX _pw_multi_aff
6377 #define EMPTY isl_pw_multi_aff_empty
6379 #define ADD isl_pw_multi_aff_union_add
6380 #include "isl_map_lexopt_templ.c"
6382 /* Given a map "map", compute the lexicographically minimal
6383 * (or maximal) image element for each domain element in dom,
6384 * in the form of an isl_pw_multi_aff.
6385 * If "empty" is not NULL, then set *empty to those elements in dom that
6386 * do not have an image element.
6387 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6388 * should be computed over the domain of "map". "empty" is also NULL
6391 * We first compute the lexicographically minimal or maximal element
6392 * in the first basic map. This results in a partial solution "res"
6393 * and a subset "todo" of dom that still need to be handled.
6394 * We then consider each of the remaining maps in "map" and successively
6395 * update both "res" and "todo".
6396 * If "empty" is NULL, then the todo sets are not needed and therefore
6397 * also not computed.
6399 static __isl_give isl_pw_multi_aff
*isl_map_partial_lexopt_aligned_pw_multi_aff(
6400 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6401 __isl_give isl_set
**empty
, unsigned flags
)
6405 isl_pw_multi_aff
*res
;
6408 full
= ISL_FL_ISSET(flags
, ISL_OPT_FULL
);
6409 if (!map
|| (!full
&& !dom
))
6412 if (isl_map_plain_is_empty(map
)) {
6417 return isl_pw_multi_aff_from_map(map
);
6420 res
= basic_map_partial_lexopt_pw_multi_aff(
6421 isl_basic_map_copy(map
->p
[0]),
6422 isl_set_copy(dom
), empty
, flags
);
6426 for (i
= 1; i
< map
->n
; ++i
) {
6427 isl_pw_multi_aff
*res_i
;
6429 res_i
= basic_map_partial_lexopt_pw_multi_aff(
6430 isl_basic_map_copy(map
->p
[i
]),
6431 isl_set_copy(dom
), empty
, flags
);
6433 if (ISL_FL_ISSET(flags
, ISL_OPT_MAX
))
6434 res
= isl_pw_multi_aff_union_lexmax(res
, res_i
);
6436 res
= isl_pw_multi_aff_union_lexmin(res
, res_i
);
6439 todo
= isl_set_intersect(todo
, *empty
);
6458 #define TYPE isl_map
6462 #define EMPTY isl_map_empty
6464 #define ADD isl_map_union_disjoint
6465 #include "isl_map_lexopt_templ.c"
6467 /* Given a map "map", compute the lexicographically minimal
6468 * (or maximal) image element for each domain element in "dom",
6469 * in the form of an isl_map.
6470 * If "empty" is not NULL, then set *empty to those elements in "dom" that
6471 * do not have an image element.
6472 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6473 * should be computed over the domain of "map". "empty" is also NULL
6476 * If the input consists of more than one disjunct, then first
6477 * compute the desired result in the form of an isl_pw_multi_aff and
6478 * then convert that into an isl_map.
6480 * This function used to have an explicit implementation in terms
6481 * of isl_maps, but it would continually intersect the domains of
6482 * partial results with the complement of the domain of the next
6483 * partial solution, potentially leading to an explosion in the number
6484 * of disjuncts if there are several disjuncts in the input.
6485 * An even earlier implementation of this function would look for
6486 * better results in the domain of the partial result and for extra
6487 * results in the complement of this domain, which would lead to
6488 * even more splintering.
6490 static __isl_give isl_map
*isl_map_partial_lexopt_aligned(
6491 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6492 __isl_give isl_set
**empty
, unsigned flags
)
6495 struct isl_map
*res
;
6496 isl_pw_multi_aff
*pma
;
6498 full
= ISL_FL_ISSET(flags
, ISL_OPT_FULL
);
6499 if (!map
|| (!full
&& !dom
))
6502 if (isl_map_plain_is_empty(map
)) {
6511 res
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[0]),
6517 pma
= isl_map_partial_lexopt_aligned_pw_multi_aff(map
, dom
, empty
,
6519 return isl_map_from_pw_multi_aff(pma
);
6528 __isl_give isl_map
*isl_map_partial_lexmax(
6529 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6530 __isl_give isl_set
**empty
)
6532 return isl_map_partial_lexopt(map
, dom
, empty
, ISL_OPT_MAX
);
6535 __isl_give isl_map
*isl_map_partial_lexmin(
6536 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6537 __isl_give isl_set
**empty
)
6539 return isl_map_partial_lexopt(map
, dom
, empty
, 0);
6542 __isl_give isl_set
*isl_set_partial_lexmin(
6543 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
6544 __isl_give isl_set
**empty
)
6546 return set_from_map(isl_map_partial_lexmin(set_to_map(set
),
6550 __isl_give isl_set
*isl_set_partial_lexmax(
6551 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
6552 __isl_give isl_set
**empty
)
6554 return set_from_map(isl_map_partial_lexmax(set_to_map(set
),
6558 /* Compute the lexicographic minimum (or maximum if "flags" includes
6559 * ISL_OPT_MAX) of "bset" over its parametric domain.
6561 __isl_give isl_set
*isl_basic_set_lexopt(__isl_take isl_basic_set
*bset
,
6564 return isl_basic_map_lexopt(bset
, flags
);
6567 __isl_give isl_map
*isl_basic_map_lexmax(__isl_take isl_basic_map
*bmap
)
6569 return isl_basic_map_lexopt(bmap
, ISL_OPT_MAX
);
6572 __isl_give isl_set
*isl_basic_set_lexmin(__isl_take isl_basic_set
*bset
)
6574 return set_from_map(isl_basic_map_lexmin(bset_to_bmap(bset
)));
6577 __isl_give isl_set
*isl_basic_set_lexmax(__isl_take isl_basic_set
*bset
)
6579 return set_from_map(isl_basic_map_lexmax(bset_to_bmap(bset
)));
6582 /* Compute the lexicographic minimum of "bset" over its parametric domain
6583 * for the purpose of quantifier elimination.
6584 * That is, find an explicit representation for all the existentially
6585 * quantified variables in "bset" by computing their lexicographic
6588 static __isl_give isl_set
*isl_basic_set_lexmin_compute_divs(
6589 __isl_take isl_basic_set
*bset
)
6591 return isl_basic_set_lexopt(bset
, ISL_OPT_QE
);
6594 /* Extract the first and only affine expression from list
6595 * and then add it to *pwaff with the given dom.
6596 * This domain is known to be disjoint from other domains
6597 * because of the way isl_basic_map_foreach_lexmax works.
6599 static int update_dim_opt(__isl_take isl_basic_set
*dom
,
6600 __isl_take isl_aff_list
*list
, void *user
)
6602 isl_ctx
*ctx
= isl_basic_set_get_ctx(dom
);
6604 isl_pw_aff
**pwaff
= user
;
6605 isl_pw_aff
*pwaff_i
;
6609 if (isl_aff_list_n_aff(list
) != 1)
6610 isl_die(ctx
, isl_error_internal
,
6611 "expecting single element list", goto error
);
6613 aff
= isl_aff_list_get_aff(list
, 0);
6614 pwaff_i
= isl_pw_aff_alloc(isl_set_from_basic_set(dom
), aff
);
6616 *pwaff
= isl_pw_aff_add_disjoint(*pwaff
, pwaff_i
);
6618 isl_aff_list_free(list
);
6622 isl_basic_set_free(dom
);
6623 isl_aff_list_free(list
);
6627 /* Given a basic map with one output dimension, compute the minimum or
6628 * maximum of that dimension as an isl_pw_aff.
6630 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
6631 * call update_dim_opt on each leaf of the result.
6633 static __isl_give isl_pw_aff
*basic_map_dim_opt(__isl_keep isl_basic_map
*bmap
,
6636 isl_space
*dim
= isl_basic_map_get_space(bmap
);
6640 dim
= isl_space_from_domain(isl_space_domain(dim
));
6641 dim
= isl_space_add_dims(dim
, isl_dim_out
, 1);
6642 pwaff
= isl_pw_aff_empty(dim
);
6644 r
= isl_basic_map_foreach_lexopt(bmap
, max
, &update_dim_opt
, &pwaff
);
6646 return isl_pw_aff_free(pwaff
);
6651 /* Compute the minimum or maximum of the given output dimension
6652 * as a function of the parameters and the input dimensions,
6653 * but independently of the other output dimensions.
6655 * We first project out the other output dimension and then compute
6656 * the "lexicographic" maximum in each basic map, combining the results
6657 * using isl_pw_aff_union_max.
6659 static __isl_give isl_pw_aff
*map_dim_opt(__isl_take isl_map
*map
, int pos
,
6666 n_out
= isl_map_dim(map
, isl_dim_out
);
6667 map
= isl_map_project_out(map
, isl_dim_out
, pos
+ 1, n_out
- (pos
+ 1));
6668 map
= isl_map_project_out(map
, isl_dim_out
, 0, pos
);
6673 isl_space
*dim
= isl_map_get_space(map
);
6675 return isl_pw_aff_empty(dim
);
6678 pwaff
= basic_map_dim_opt(map
->p
[0], max
);
6679 for (i
= 1; i
< map
->n
; ++i
) {
6680 isl_pw_aff
*pwaff_i
;
6682 pwaff_i
= basic_map_dim_opt(map
->p
[i
], max
);
6683 pwaff
= isl_pw_aff_union_opt(pwaff
, pwaff_i
, max
);
6691 /* Compute the minimum of the given output dimension as a function of the
6692 * parameters and input dimensions, but independently of
6693 * the other output dimensions.
6695 __isl_give isl_pw_aff
*isl_map_dim_min(__isl_take isl_map
*map
, int pos
)
6697 return map_dim_opt(map
, pos
, 0);
6700 /* Compute the maximum of the given output dimension as a function of the
6701 * parameters and input dimensions, but independently of
6702 * the other output dimensions.
6704 __isl_give isl_pw_aff
*isl_map_dim_max(__isl_take isl_map
*map
, int pos
)
6706 return map_dim_opt(map
, pos
, 1);
6709 /* Compute the minimum or maximum of the given set dimension
6710 * as a function of the parameters,
6711 * but independently of the other set dimensions.
6713 static __isl_give isl_pw_aff
*set_dim_opt(__isl_take isl_set
*set
, int pos
,
6716 return map_dim_opt(set
, pos
, max
);
6719 /* Compute the maximum of the given set dimension as a function of the
6720 * parameters, but independently of the other set dimensions.
6722 __isl_give isl_pw_aff
*isl_set_dim_max(__isl_take isl_set
*set
, int pos
)
6724 return set_dim_opt(set
, pos
, 1);
6727 /* Compute the minimum of the given set dimension as a function of the
6728 * parameters, but independently of the other set dimensions.
6730 __isl_give isl_pw_aff
*isl_set_dim_min(__isl_take isl_set
*set
, int pos
)
6732 return set_dim_opt(set
, pos
, 0);
6735 /* Apply a preimage specified by "mat" on the parameters of "bset".
6736 * bset is assumed to have only parameters and divs.
6738 static struct isl_basic_set
*basic_set_parameter_preimage(
6739 struct isl_basic_set
*bset
, struct isl_mat
*mat
)
6746 bset
->dim
= isl_space_cow(bset
->dim
);
6750 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
6752 isl_assert(bset
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
6754 bset
->dim
->nparam
= 0;
6755 bset
->dim
->n_out
= nparam
;
6756 bset
= isl_basic_set_preimage(bset
, mat
);
6758 bset
->dim
->nparam
= bset
->dim
->n_out
;
6759 bset
->dim
->n_out
= 0;
6764 isl_basic_set_free(bset
);
6768 /* Apply a preimage specified by "mat" on the parameters of "set".
6769 * set is assumed to have only parameters and divs.
6771 static __isl_give isl_set
*set_parameter_preimage(__isl_take isl_set
*set
,
6772 __isl_take isl_mat
*mat
)
6780 nparam
= isl_set_dim(set
, isl_dim_param
);
6782 if (mat
->n_row
!= 1 + nparam
)
6783 isl_die(isl_set_get_ctx(set
), isl_error_internal
,
6784 "unexpected number of rows", goto error
);
6786 space
= isl_set_get_space(set
);
6787 space
= isl_space_move_dims(space
, isl_dim_set
, 0,
6788 isl_dim_param
, 0, nparam
);
6789 set
= isl_set_reset_space(set
, space
);
6790 set
= isl_set_preimage(set
, mat
);
6791 nparam
= isl_set_dim(set
, isl_dim_out
);
6792 space
= isl_set_get_space(set
);
6793 space
= isl_space_move_dims(space
, isl_dim_param
, 0,
6794 isl_dim_out
, 0, nparam
);
6795 set
= isl_set_reset_space(set
, space
);
6803 /* Intersect the basic set "bset" with the affine space specified by the
6804 * equalities in "eq".
6806 static struct isl_basic_set
*basic_set_append_equalities(
6807 struct isl_basic_set
*bset
, struct isl_mat
*eq
)
6815 bset
= isl_basic_set_extend_space(bset
, isl_space_copy(bset
->dim
), 0,
6820 len
= 1 + isl_space_dim(bset
->dim
, isl_dim_all
) + bset
->extra
;
6821 for (i
= 0; i
< eq
->n_row
; ++i
) {
6822 k
= isl_basic_set_alloc_equality(bset
);
6825 isl_seq_cpy(bset
->eq
[k
], eq
->row
[i
], eq
->n_col
);
6826 isl_seq_clr(bset
->eq
[k
] + eq
->n_col
, len
- eq
->n_col
);
6830 bset
= isl_basic_set_gauss(bset
, NULL
);
6831 bset
= isl_basic_set_finalize(bset
);
6836 isl_basic_set_free(bset
);
6840 /* Intersect the set "set" with the affine space specified by the
6841 * equalities in "eq".
6843 static struct isl_set
*set_append_equalities(struct isl_set
*set
,
6851 for (i
= 0; i
< set
->n
; ++i
) {
6852 set
->p
[i
] = basic_set_append_equalities(set
->p
[i
],
6865 /* Given a basic set "bset" that only involves parameters and existentially
6866 * quantified variables, return the index of the first equality
6867 * that only involves parameters. If there is no such equality then
6868 * return bset->n_eq.
6870 * This function assumes that isl_basic_set_gauss has been called on "bset".
6872 static int first_parameter_equality(__isl_keep isl_basic_set
*bset
)
6875 unsigned nparam
, n_div
;
6880 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
6881 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
6883 for (i
= 0, j
= n_div
- 1; i
< bset
->n_eq
&& j
>= 0; --j
) {
6884 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ j
]))
6891 /* Compute an explicit representation for the existentially quantified
6892 * variables in "bset" by computing the "minimal value" of the set
6893 * variables. Since there are no set variables, the computation of
6894 * the minimal value essentially computes an explicit representation
6895 * of the non-empty part(s) of "bset".
6897 * The input only involves parameters and existentially quantified variables.
6898 * All equalities among parameters have been removed.
6900 * Since the existentially quantified variables in the result are in general
6901 * going to be different from those in the input, we first replace
6902 * them by the minimal number of variables based on their equalities.
6903 * This should simplify the parametric integer programming.
6905 static __isl_give isl_set
*base_compute_divs(__isl_take isl_basic_set
*bset
)
6907 isl_morph
*morph1
, *morph2
;
6913 if (bset
->n_eq
== 0)
6914 return isl_basic_set_lexmin_compute_divs(bset
);
6916 morph1
= isl_basic_set_parameter_compression(bset
);
6917 bset
= isl_morph_basic_set(isl_morph_copy(morph1
), bset
);
6918 bset
= isl_basic_set_lift(bset
);
6919 morph2
= isl_basic_set_variable_compression(bset
, isl_dim_set
);
6920 bset
= isl_morph_basic_set(morph2
, bset
);
6921 n
= isl_basic_set_dim(bset
, isl_dim_set
);
6922 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
6924 set
= isl_basic_set_lexmin_compute_divs(bset
);
6926 set
= isl_morph_set(isl_morph_inverse(morph1
), set
);
6931 /* Project the given basic set onto its parameter domain, possibly introducing
6932 * new, explicit, existential variables in the constraints.
6933 * The input has parameters and (possibly implicit) existential variables.
6934 * The output has the same parameters, but only
6935 * explicit existentially quantified variables.
6937 * The actual projection is performed by pip, but pip doesn't seem
6938 * to like equalities very much, so we first remove the equalities
6939 * among the parameters by performing a variable compression on
6940 * the parameters. Afterward, an inverse transformation is performed
6941 * and the equalities among the parameters are inserted back in.
6943 * The variable compression on the parameters may uncover additional
6944 * equalities that were only implicit before. We therefore check
6945 * if there are any new parameter equalities in the result and
6946 * if so recurse. The removal of parameter equalities is required
6947 * for the parameter compression performed by base_compute_divs.
6949 static struct isl_set
*parameter_compute_divs(struct isl_basic_set
*bset
)
6953 struct isl_mat
*T
, *T2
;
6954 struct isl_set
*set
;
6957 bset
= isl_basic_set_cow(bset
);
6961 if (bset
->n_eq
== 0)
6962 return base_compute_divs(bset
);
6964 bset
= isl_basic_set_gauss(bset
, NULL
);
6967 if (isl_basic_set_plain_is_empty(bset
))
6968 return isl_set_from_basic_set(bset
);
6970 i
= first_parameter_equality(bset
);
6971 if (i
== bset
->n_eq
)
6972 return base_compute_divs(bset
);
6974 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
6975 eq
= isl_mat_sub_alloc6(bset
->ctx
, bset
->eq
, i
, bset
->n_eq
- i
,
6977 eq
= isl_mat_cow(eq
);
6978 T
= isl_mat_variable_compression(isl_mat_copy(eq
), &T2
);
6979 if (T
&& T
->n_col
== 0) {
6983 bset
= isl_basic_set_set_to_empty(bset
);
6984 return isl_set_from_basic_set(bset
);
6986 bset
= basic_set_parameter_preimage(bset
, T
);
6988 i
= first_parameter_equality(bset
);
6991 else if (i
== bset
->n_eq
)
6992 set
= base_compute_divs(bset
);
6994 set
= parameter_compute_divs(bset
);
6995 set
= set_parameter_preimage(set
, T2
);
6996 set
= set_append_equalities(set
, eq
);
7000 /* Insert the divs from "ls" before those of "bmap".
7002 * The number of columns is not changed, which means that the last
7003 * dimensions of "bmap" are being reintepreted as the divs from "ls".
7004 * The caller is responsible for removing the same number of dimensions
7005 * from the space of "bmap".
7007 static __isl_give isl_basic_map
*insert_divs_from_local_space(
7008 __isl_take isl_basic_map
*bmap
, __isl_keep isl_local_space
*ls
)
7014 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
7018 old_n_div
= bmap
->n_div
;
7019 bmap
= insert_div_rows(bmap
, n_div
);
7023 for (i
= 0; i
< n_div
; ++i
) {
7024 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], ls
->div
->n_col
);
7025 isl_seq_clr(bmap
->div
[i
] + ls
->div
->n_col
, old_n_div
);
7031 /* Replace the space of "bmap" by the space and divs of "ls".
7033 * If "ls" has any divs, then we simplify the result since we may
7034 * have discovered some additional equalities that could simplify
7035 * the div expressions.
7037 static __isl_give isl_basic_map
*basic_replace_space_by_local_space(
7038 __isl_take isl_basic_map
*bmap
, __isl_take isl_local_space
*ls
)
7042 bmap
= isl_basic_map_cow(bmap
);
7046 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
7047 bmap
= insert_divs_from_local_space(bmap
, ls
);
7051 isl_space_free(bmap
->dim
);
7052 bmap
->dim
= isl_local_space_get_space(ls
);
7056 isl_local_space_free(ls
);
7058 bmap
= isl_basic_map_simplify(bmap
);
7059 bmap
= isl_basic_map_finalize(bmap
);
7062 isl_basic_map_free(bmap
);
7063 isl_local_space_free(ls
);
7067 /* Replace the space of "map" by the space and divs of "ls".
7069 static __isl_give isl_map
*replace_space_by_local_space(__isl_take isl_map
*map
,
7070 __isl_take isl_local_space
*ls
)
7074 map
= isl_map_cow(map
);
7078 for (i
= 0; i
< map
->n
; ++i
) {
7079 map
->p
[i
] = basic_replace_space_by_local_space(map
->p
[i
],
7080 isl_local_space_copy(ls
));
7084 isl_space_free(map
->dim
);
7085 map
->dim
= isl_local_space_get_space(ls
);
7089 isl_local_space_free(ls
);
7092 isl_local_space_free(ls
);
7097 /* Compute an explicit representation for the existentially
7098 * quantified variables for which do not know any explicit representation yet.
7100 * We first sort the existentially quantified variables so that the
7101 * existentially quantified variables for which we already have an explicit
7102 * representation are placed before those for which we do not.
7103 * The input dimensions, the output dimensions and the existentially
7104 * quantified variables for which we already have an explicit
7105 * representation are then turned into parameters.
7106 * compute_divs returns a map with the same parameters and
7107 * no input or output dimensions and the dimension specification
7108 * is reset to that of the input, including the existentially quantified
7109 * variables for which we already had an explicit representation.
7111 static struct isl_map
*compute_divs(struct isl_basic_map
*bmap
)
7113 struct isl_basic_set
*bset
;
7114 struct isl_set
*set
;
7115 struct isl_map
*map
;
7117 isl_local_space
*ls
;
7124 bmap
= isl_basic_map_sort_divs(bmap
);
7125 bmap
= isl_basic_map_cow(bmap
);
7129 n_known
= isl_basic_map_first_unknown_div(bmap
);
7131 return isl_map_from_basic_map(isl_basic_map_free(bmap
));
7133 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
7134 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
7135 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
7136 dim
= isl_space_set_alloc(bmap
->ctx
,
7137 nparam
+ n_in
+ n_out
+ n_known
, 0);
7141 ls
= isl_basic_map_get_local_space(bmap
);
7142 ls
= isl_local_space_drop_dims(ls
, isl_dim_div
,
7143 n_known
, bmap
->n_div
- n_known
);
7145 for (i
= n_known
; i
< bmap
->n_div
; ++i
)
7146 swap_div(bmap
, i
- n_known
, i
);
7147 bmap
->n_div
-= n_known
;
7148 bmap
->extra
-= n_known
;
7150 bmap
= isl_basic_map_reset_space(bmap
, dim
);
7151 bset
= bset_from_bmap(bmap
);
7153 set
= parameter_compute_divs(bset
);
7154 map
= set_to_map(set
);
7155 map
= replace_space_by_local_space(map
, ls
);
7159 isl_basic_map_free(bmap
);
7163 /* Remove the explicit representation of local variable "div",
7166 __isl_give isl_basic_map
*isl_basic_map_mark_div_unknown(
7167 __isl_take isl_basic_map
*bmap
, int div
)
7171 unknown
= isl_basic_map_div_is_marked_unknown(bmap
, div
);
7173 return isl_basic_map_free(bmap
);
7177 bmap
= isl_basic_map_cow(bmap
);
7180 isl_int_set_si(bmap
->div
[div
][0], 0);
7184 /* Is local variable "div" of "bmap" marked as not having an explicit
7186 * Note that even if "div" is not marked in this way and therefore
7187 * has an explicit representation, this representation may still
7188 * depend (indirectly) on other local variables that do not
7189 * have an explicit representation.
7191 isl_bool
isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map
*bmap
,
7194 if (isl_basic_map_check_range(bmap
, isl_dim_div
, div
, 1) < 0)
7195 return isl_bool_error
;
7196 return isl_int_is_zero(bmap
->div
[div
][0]);
7199 /* Return the position of the first local variable that does not
7200 * have an explicit representation.
7201 * Return the total number of local variables if they all have
7202 * an explicit representation.
7203 * Return -1 on error.
7205 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map
*bmap
)
7212 for (i
= 0; i
< bmap
->n_div
; ++i
) {
7213 if (!isl_basic_map_div_is_known(bmap
, i
))
7219 /* Return the position of the first local variable that does not
7220 * have an explicit representation.
7221 * Return the total number of local variables if they all have
7222 * an explicit representation.
7223 * Return -1 on error.
7225 int isl_basic_set_first_unknown_div(__isl_keep isl_basic_set
*bset
)
7227 return isl_basic_map_first_unknown_div(bset
);
7230 /* Does "bmap" have an explicit representation for all local variables?
7232 isl_bool
isl_basic_map_divs_known(__isl_keep isl_basic_map
*bmap
)
7236 n
= isl_basic_map_dim(bmap
, isl_dim_div
);
7237 first
= isl_basic_map_first_unknown_div(bmap
);
7239 return isl_bool_error
;
7243 /* Do all basic maps in "map" have an explicit representation
7244 * for all local variables?
7246 isl_bool
isl_map_divs_known(__isl_keep isl_map
*map
)
7251 return isl_bool_error
;
7253 for (i
= 0; i
< map
->n
; ++i
) {
7254 int known
= isl_basic_map_divs_known(map
->p
[i
]);
7259 return isl_bool_true
;
7262 /* If bmap contains any unknown divs, then compute explicit
7263 * expressions for them. However, this computation may be
7264 * quite expensive, so first try to remove divs that aren't
7267 struct isl_map
*isl_basic_map_compute_divs(struct isl_basic_map
*bmap
)
7270 struct isl_map
*map
;
7272 known
= isl_basic_map_divs_known(bmap
);
7276 return isl_map_from_basic_map(bmap
);
7278 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
7280 known
= isl_basic_map_divs_known(bmap
);
7284 return isl_map_from_basic_map(bmap
);
7286 map
= compute_divs(bmap
);
7289 isl_basic_map_free(bmap
);
7293 struct isl_map
*isl_map_compute_divs(struct isl_map
*map
)
7297 struct isl_map
*res
;
7304 known
= isl_map_divs_known(map
);
7312 res
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[0]));
7313 for (i
= 1 ; i
< map
->n
; ++i
) {
7315 r2
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[i
]));
7316 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
7317 res
= isl_map_union_disjoint(res
, r2
);
7319 res
= isl_map_union(res
, r2
);
7326 struct isl_set
*isl_basic_set_compute_divs(struct isl_basic_set
*bset
)
7328 return set_from_map(isl_basic_map_compute_divs(bset_to_bmap(bset
)));
7331 struct isl_set
*isl_set_compute_divs(struct isl_set
*set
)
7333 return set_from_map(isl_map_compute_divs(set_to_map(set
)));
7336 struct isl_set
*isl_map_domain(struct isl_map
*map
)
7339 struct isl_set
*set
;
7344 map
= isl_map_cow(map
);
7348 set
= set_from_map(map
);
7349 set
->dim
= isl_space_domain(set
->dim
);
7352 for (i
= 0; i
< map
->n
; ++i
) {
7353 set
->p
[i
] = isl_basic_map_domain(map
->p
[i
]);
7357 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
7358 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
7365 /* Return the union of "map1" and "map2", where we assume for now that
7366 * "map1" and "map2" are disjoint. Note that the basic maps inside
7367 * "map1" or "map2" may not be disjoint from each other.
7368 * Also note that this function is also called from isl_map_union,
7369 * which takes care of handling the situation where "map1" and "map2"
7370 * may not be disjoint.
7372 * If one of the inputs is empty, we can simply return the other input.
7373 * Similarly, if one of the inputs is universal, then it is equal to the union.
7375 static __isl_give isl_map
*map_union_disjoint(__isl_take isl_map
*map1
,
7376 __isl_take isl_map
*map2
)
7380 struct isl_map
*map
= NULL
;
7386 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
7387 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
7388 "spaces don't match", goto error
);
7399 is_universe
= isl_map_plain_is_universe(map1
);
7400 if (is_universe
< 0)
7407 is_universe
= isl_map_plain_is_universe(map2
);
7408 if (is_universe
< 0)
7415 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
7416 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
7417 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
7419 map
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
7420 map1
->n
+ map2
->n
, flags
);
7423 for (i
= 0; i
< map1
->n
; ++i
) {
7424 map
= isl_map_add_basic_map(map
,
7425 isl_basic_map_copy(map1
->p
[i
]));
7429 for (i
= 0; i
< map2
->n
; ++i
) {
7430 map
= isl_map_add_basic_map(map
,
7431 isl_basic_map_copy(map2
->p
[i
]));
7445 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7446 * guaranteed to be disjoint by the caller.
7448 * Note that this functions is called from within isl_map_make_disjoint,
7449 * so we have to be careful not to touch the constraints of the inputs
7452 __isl_give isl_map
*isl_map_union_disjoint(__isl_take isl_map
*map1
,
7453 __isl_take isl_map
*map2
)
7455 return isl_map_align_params_map_map_and(map1
, map2
, &map_union_disjoint
);
7458 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7459 * not be disjoint. The parameters are assumed to have been aligned.
7461 * We currently simply call map_union_disjoint, the internal operation
7462 * of which does not really depend on the inputs being disjoint.
7463 * If the result contains more than one basic map, then we clear
7464 * the disjoint flag since the result may contain basic maps from
7465 * both inputs and these are not guaranteed to be disjoint.
7467 * As a special case, if "map1" and "map2" are obviously equal,
7468 * then we simply return "map1".
7470 static __isl_give isl_map
*map_union_aligned(__isl_take isl_map
*map1
,
7471 __isl_take isl_map
*map2
)
7478 equal
= isl_map_plain_is_equal(map1
, map2
);
7486 map1
= map_union_disjoint(map1
, map2
);
7490 ISL_F_CLR(map1
, ISL_MAP_DISJOINT
);
7498 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7501 __isl_give isl_map
*isl_map_union(__isl_take isl_map
*map1
,
7502 __isl_take isl_map
*map2
)
7504 return isl_map_align_params_map_map_and(map1
, map2
, &map_union_aligned
);
7507 struct isl_set
*isl_set_union_disjoint(
7508 struct isl_set
*set1
, struct isl_set
*set2
)
7510 return set_from_map(isl_map_union_disjoint(set_to_map(set1
),
7514 struct isl_set
*isl_set_union(struct isl_set
*set1
, struct isl_set
*set2
)
7516 return set_from_map(isl_map_union(set_to_map(set1
), set_to_map(set2
)));
7519 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7522 * "map" and "set" are assumed to be compatible and non-NULL.
7524 static __isl_give isl_map
*map_intersect_set(__isl_take isl_map
*map
,
7525 __isl_take isl_set
*set
,
7526 __isl_give isl_basic_map
*fn(__isl_take isl_basic_map
*bmap
,
7527 __isl_take isl_basic_set
*bset
))
7530 struct isl_map
*result
;
7533 if (isl_set_plain_is_universe(set
)) {
7538 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
) &&
7539 ISL_F_ISSET(set
, ISL_MAP_DISJOINT
))
7540 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
7542 result
= isl_map_alloc_space(isl_space_copy(map
->dim
),
7543 map
->n
* set
->n
, flags
);
7544 for (i
= 0; result
&& i
< map
->n
; ++i
)
7545 for (j
= 0; j
< set
->n
; ++j
) {
7546 result
= isl_map_add_basic_map(result
,
7547 fn(isl_basic_map_copy(map
->p
[i
]),
7548 isl_basic_set_copy(set
->p
[j
])));
7558 static __isl_give isl_map
*map_intersect_range(__isl_take isl_map
*map
,
7559 __isl_take isl_set
*set
)
7564 if (!isl_map_compatible_range(map
, set
))
7565 isl_die(set
->ctx
, isl_error_invalid
,
7566 "incompatible spaces", goto error
);
7568 return map_intersect_set(map
, set
, &isl_basic_map_intersect_range
);
7575 __isl_give isl_map
*isl_map_intersect_range(__isl_take isl_map
*map
,
7576 __isl_take isl_set
*set
)
7578 return isl_map_align_params_map_map_and(map
, set
, &map_intersect_range
);
7581 static __isl_give isl_map
*map_intersect_domain(__isl_take isl_map
*map
,
7582 __isl_take isl_set
*set
)
7587 if (!isl_map_compatible_domain(map
, set
))
7588 isl_die(set
->ctx
, isl_error_invalid
,
7589 "incompatible spaces", goto error
);
7591 return map_intersect_set(map
, set
, &isl_basic_map_intersect_domain
);
7598 __isl_give isl_map
*isl_map_intersect_domain(__isl_take isl_map
*map
,
7599 __isl_take isl_set
*set
)
7601 return isl_map_align_params_map_map_and(map
, set
,
7602 &map_intersect_domain
);
7605 static __isl_give isl_map
*map_apply_domain(__isl_take isl_map
*map1
,
7606 __isl_take isl_map
*map2
)
7610 map1
= isl_map_reverse(map1
);
7611 map1
= isl_map_apply_range(map1
, map2
);
7612 return isl_map_reverse(map1
);
7619 __isl_give isl_map
*isl_map_apply_domain(__isl_take isl_map
*map1
,
7620 __isl_take isl_map
*map2
)
7622 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_domain
);
7625 static __isl_give isl_map
*map_apply_range(__isl_take isl_map
*map1
,
7626 __isl_take isl_map
*map2
)
7628 isl_space
*dim_result
;
7629 struct isl_map
*result
;
7635 dim_result
= isl_space_join(isl_space_copy(map1
->dim
),
7636 isl_space_copy(map2
->dim
));
7638 result
= isl_map_alloc_space(dim_result
, map1
->n
* map2
->n
, 0);
7641 for (i
= 0; i
< map1
->n
; ++i
)
7642 for (j
= 0; j
< map2
->n
; ++j
) {
7643 result
= isl_map_add_basic_map(result
,
7644 isl_basic_map_apply_range(
7645 isl_basic_map_copy(map1
->p
[i
]),
7646 isl_basic_map_copy(map2
->p
[j
])));
7652 if (result
&& result
->n
<= 1)
7653 ISL_F_SET(result
, ISL_MAP_DISJOINT
);
7661 __isl_give isl_map
*isl_map_apply_range(__isl_take isl_map
*map1
,
7662 __isl_take isl_map
*map2
)
7664 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_range
);
7668 * returns range - domain
7670 struct isl_basic_set
*isl_basic_map_deltas(struct isl_basic_map
*bmap
)
7672 isl_space
*target_space
;
7673 struct isl_basic_set
*bset
;
7680 isl_assert(bmap
->ctx
, isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
7681 bmap
->dim
, isl_dim_out
),
7683 target_space
= isl_space_domain(isl_basic_map_get_space(bmap
));
7684 dim
= isl_basic_map_n_in(bmap
);
7685 nparam
= isl_basic_map_n_param(bmap
);
7686 bmap
= isl_basic_map_from_range(isl_basic_map_wrap(bmap
));
7687 bmap
= isl_basic_map_add_dims(bmap
, isl_dim_in
, dim
);
7688 bmap
= isl_basic_map_extend_constraints(bmap
, dim
, 0);
7689 for (i
= 0; i
< dim
; ++i
) {
7690 int j
= isl_basic_map_alloc_equality(bmap
);
7692 bmap
= isl_basic_map_free(bmap
);
7695 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
7696 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
7697 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], 1);
7698 isl_int_set_si(bmap
->eq
[j
][1+nparam
+2*dim
+i
], -1);
7700 bset
= isl_basic_map_domain(bmap
);
7701 bset
= isl_basic_set_reset_space(bset
, target_space
);
7704 isl_basic_map_free(bmap
);
7709 * returns range - domain
7711 __isl_give isl_set
*isl_map_deltas(__isl_take isl_map
*map
)
7715 struct isl_set
*result
;
7720 isl_assert(map
->ctx
, isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
7721 map
->dim
, isl_dim_out
),
7723 dim
= isl_map_get_space(map
);
7724 dim
= isl_space_domain(dim
);
7725 result
= isl_set_alloc_space(dim
, map
->n
, 0);
7728 for (i
= 0; i
< map
->n
; ++i
)
7729 result
= isl_set_add_basic_set(result
,
7730 isl_basic_map_deltas(isl_basic_map_copy(map
->p
[i
])));
7739 * returns [domain -> range] -> range - domain
7741 __isl_give isl_basic_map
*isl_basic_map_deltas_map(
7742 __isl_take isl_basic_map
*bmap
)
7746 isl_basic_map
*domain
;
7750 if (!isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
7751 bmap
->dim
, isl_dim_out
))
7752 isl_die(bmap
->ctx
, isl_error_invalid
,
7753 "domain and range don't match", goto error
);
7755 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
7756 n
= isl_basic_map_dim(bmap
, isl_dim_in
);
7758 dim
= isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap
)));
7759 domain
= isl_basic_map_universe(dim
);
7761 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
7762 bmap
= isl_basic_map_apply_range(bmap
, domain
);
7763 bmap
= isl_basic_map_extend_constraints(bmap
, n
, 0);
7765 total
= isl_basic_map_total_dim(bmap
);
7767 for (i
= 0; i
< n
; ++i
) {
7768 k
= isl_basic_map_alloc_equality(bmap
);
7771 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
7772 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], 1);
7773 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ i
], -1);
7774 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ n
+ i
], 1);
7777 bmap
= isl_basic_map_gauss(bmap
, NULL
);
7778 return isl_basic_map_finalize(bmap
);
7780 isl_basic_map_free(bmap
);
7785 * returns [domain -> range] -> range - domain
7787 __isl_give isl_map
*isl_map_deltas_map(__isl_take isl_map
*map
)
7790 isl_space
*domain_dim
;
7795 if (!isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
7796 map
->dim
, isl_dim_out
))
7797 isl_die(map
->ctx
, isl_error_invalid
,
7798 "domain and range don't match", goto error
);
7800 map
= isl_map_cow(map
);
7804 domain_dim
= isl_space_from_range(isl_space_domain(isl_map_get_space(map
)));
7805 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
7806 map
->dim
= isl_space_join(map
->dim
, domain_dim
);
7809 for (i
= 0; i
< map
->n
; ++i
) {
7810 map
->p
[i
] = isl_basic_map_deltas_map(map
->p
[i
]);
7814 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
7821 static __isl_give isl_basic_map
*basic_map_identity(__isl_take isl_space
*dims
)
7823 struct isl_basic_map
*bmap
;
7831 nparam
= dims
->nparam
;
7833 bmap
= isl_basic_map_alloc_space(dims
, 0, dim
, 0);
7837 for (i
= 0; i
< dim
; ++i
) {
7838 int j
= isl_basic_map_alloc_equality(bmap
);
7841 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
7842 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
7843 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], -1);
7845 return isl_basic_map_finalize(bmap
);
7847 isl_basic_map_free(bmap
);
7851 __isl_give isl_basic_map
*isl_basic_map_identity(__isl_take isl_space
*dim
)
7855 if (dim
->n_in
!= dim
->n_out
)
7856 isl_die(dim
->ctx
, isl_error_invalid
,
7857 "number of input and output dimensions needs to be "
7858 "the same", goto error
);
7859 return basic_map_identity(dim
);
7861 isl_space_free(dim
);
7865 __isl_give isl_map
*isl_map_identity(__isl_take isl_space
*dim
)
7867 return isl_map_from_basic_map(isl_basic_map_identity(dim
));
7870 __isl_give isl_map
*isl_set_identity(__isl_take isl_set
*set
)
7872 isl_space
*dim
= isl_set_get_space(set
);
7874 id
= isl_map_identity(isl_space_map_from_set(dim
));
7875 return isl_map_intersect_range(id
, set
);
7878 /* Construct a basic set with all set dimensions having only non-negative
7881 __isl_give isl_basic_set
*isl_basic_set_positive_orthant(
7882 __isl_take isl_space
*space
)
7887 struct isl_basic_set
*bset
;
7891 nparam
= space
->nparam
;
7893 bset
= isl_basic_set_alloc_space(space
, 0, 0, dim
);
7896 for (i
= 0; i
< dim
; ++i
) {
7897 int k
= isl_basic_set_alloc_inequality(bset
);
7900 isl_seq_clr(bset
->ineq
[k
], 1 + isl_basic_set_total_dim(bset
));
7901 isl_int_set_si(bset
->ineq
[k
][1 + nparam
+ i
], 1);
7905 isl_basic_set_free(bset
);
7909 /* Construct the half-space x_pos >= 0.
7911 static __isl_give isl_basic_set
*nonneg_halfspace(__isl_take isl_space
*dim
,
7915 isl_basic_set
*nonneg
;
7917 nonneg
= isl_basic_set_alloc_space(dim
, 0, 0, 1);
7918 k
= isl_basic_set_alloc_inequality(nonneg
);
7921 isl_seq_clr(nonneg
->ineq
[k
], 1 + isl_basic_set_total_dim(nonneg
));
7922 isl_int_set_si(nonneg
->ineq
[k
][pos
], 1);
7924 return isl_basic_set_finalize(nonneg
);
7926 isl_basic_set_free(nonneg
);
7930 /* Construct the half-space x_pos <= -1.
7932 static __isl_give isl_basic_set
*neg_halfspace(__isl_take isl_space
*dim
, int pos
)
7937 neg
= isl_basic_set_alloc_space(dim
, 0, 0, 1);
7938 k
= isl_basic_set_alloc_inequality(neg
);
7941 isl_seq_clr(neg
->ineq
[k
], 1 + isl_basic_set_total_dim(neg
));
7942 isl_int_set_si(neg
->ineq
[k
][0], -1);
7943 isl_int_set_si(neg
->ineq
[k
][pos
], -1);
7945 return isl_basic_set_finalize(neg
);
7947 isl_basic_set_free(neg
);
7951 __isl_give isl_set
*isl_set_split_dims(__isl_take isl_set
*set
,
7952 enum isl_dim_type type
, unsigned first
, unsigned n
)
7956 isl_basic_set
*nonneg
;
7964 isl_assert(set
->ctx
, first
+ n
<= isl_set_dim(set
, type
), goto error
);
7966 offset
= pos(set
->dim
, type
);
7967 for (i
= 0; i
< n
; ++i
) {
7968 nonneg
= nonneg_halfspace(isl_set_get_space(set
),
7969 offset
+ first
+ i
);
7970 neg
= neg_halfspace(isl_set_get_space(set
), offset
+ first
+ i
);
7972 set
= isl_set_intersect(set
, isl_basic_set_union(nonneg
, neg
));
7981 static int foreach_orthant(__isl_take isl_set
*set
, int *signs
, int first
,
7982 int len
, int (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
7989 if (isl_set_plain_is_empty(set
)) {
7994 return fn(set
, signs
, user
);
7997 half
= isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set
),
7999 half
= isl_set_intersect(half
, isl_set_copy(set
));
8000 if (foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
) < 0)
8004 half
= isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set
),
8006 half
= isl_set_intersect(half
, set
);
8007 return foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
);
8013 /* Call "fn" on the intersections of "set" with each of the orthants
8014 * (except for obviously empty intersections). The orthant is identified
8015 * by the signs array, with each entry having value 1 or -1 according
8016 * to the sign of the corresponding variable.
8018 int isl_set_foreach_orthant(__isl_keep isl_set
*set
,
8019 int (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
8029 if (isl_set_plain_is_empty(set
))
8032 nparam
= isl_set_dim(set
, isl_dim_param
);
8033 nvar
= isl_set_dim(set
, isl_dim_set
);
8035 signs
= isl_alloc_array(set
->ctx
, int, nparam
+ nvar
);
8037 r
= foreach_orthant(isl_set_copy(set
), signs
, 0, nparam
+ nvar
,
8045 isl_bool
isl_set_is_equal(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
8047 return isl_map_is_equal(set_to_map(set1
), set_to_map(set2
));
8050 isl_bool
isl_basic_map_is_subset(__isl_keep isl_basic_map
*bmap1
,
8051 __isl_keep isl_basic_map
*bmap2
)
8054 struct isl_map
*map1
;
8055 struct isl_map
*map2
;
8057 if (!bmap1
|| !bmap2
)
8058 return isl_bool_error
;
8060 map1
= isl_map_from_basic_map(isl_basic_map_copy(bmap1
));
8061 map2
= isl_map_from_basic_map(isl_basic_map_copy(bmap2
));
8063 is_subset
= isl_map_is_subset(map1
, map2
);
8071 isl_bool
isl_basic_set_is_subset(__isl_keep isl_basic_set
*bset1
,
8072 __isl_keep isl_basic_set
*bset2
)
8074 return isl_basic_map_is_subset(bset1
, bset2
);
8077 isl_bool
isl_basic_map_is_equal(__isl_keep isl_basic_map
*bmap1
,
8078 __isl_keep isl_basic_map
*bmap2
)
8082 if (!bmap1
|| !bmap2
)
8083 return isl_bool_error
;
8084 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
8085 if (is_subset
!= isl_bool_true
)
8087 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
8091 isl_bool
isl_basic_set_is_equal(__isl_keep isl_basic_set
*bset1
,
8092 __isl_keep isl_basic_set
*bset2
)
8094 return isl_basic_map_is_equal(
8095 bset_to_bmap(bset1
), bset_to_bmap(bset2
));
8098 isl_bool
isl_map_is_empty(__isl_keep isl_map
*map
)
8104 return isl_bool_error
;
8105 for (i
= 0; i
< map
->n
; ++i
) {
8106 is_empty
= isl_basic_map_is_empty(map
->p
[i
]);
8108 return isl_bool_error
;
8110 return isl_bool_false
;
8112 return isl_bool_true
;
8115 isl_bool
isl_map_plain_is_empty(__isl_keep isl_map
*map
)
8117 return map
? map
->n
== 0 : isl_bool_error
;
8120 isl_bool
isl_set_plain_is_empty(__isl_keep isl_set
*set
)
8122 return set
? set
->n
== 0 : isl_bool_error
;
8125 isl_bool
isl_set_is_empty(__isl_keep isl_set
*set
)
8127 return isl_map_is_empty(set_to_map(set
));
8130 int isl_map_has_equal_space(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
8135 return isl_space_is_equal(map1
->dim
, map2
->dim
);
8138 int isl_set_has_equal_space(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
8143 return isl_space_is_equal(set1
->dim
, set2
->dim
);
8146 static isl_bool
map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
8151 return isl_bool_error
;
8152 is_subset
= isl_map_is_subset(map1
, map2
);
8153 if (is_subset
!= isl_bool_true
)
8155 is_subset
= isl_map_is_subset(map2
, map1
);
8159 /* Is "map1" equal to "map2"?
8161 * First check if they are obviously equal.
8162 * If not, then perform a more detailed analysis.
8164 isl_bool
isl_map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
8168 equal
= isl_map_plain_is_equal(map1
, map2
);
8169 if (equal
< 0 || equal
)
8171 return isl_map_align_params_map_map_and_test(map1
, map2
, &map_is_equal
);
8174 isl_bool
isl_basic_map_is_strict_subset(
8175 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
8179 if (!bmap1
|| !bmap2
)
8180 return isl_bool_error
;
8181 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
8182 if (is_subset
!= isl_bool_true
)
8184 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
8185 if (is_subset
== isl_bool_error
)
8190 isl_bool
isl_map_is_strict_subset(__isl_keep isl_map
*map1
,
8191 __isl_keep isl_map
*map2
)
8196 return isl_bool_error
;
8197 is_subset
= isl_map_is_subset(map1
, map2
);
8198 if (is_subset
!= isl_bool_true
)
8200 is_subset
= isl_map_is_subset(map2
, map1
);
8201 if (is_subset
== isl_bool_error
)
8206 isl_bool
isl_set_is_strict_subset(__isl_keep isl_set
*set1
,
8207 __isl_keep isl_set
*set2
)
8209 return isl_map_is_strict_subset(set_to_map(set1
), set_to_map(set2
));
8212 /* Is "bmap" obviously equal to the universe with the same space?
8214 * That is, does it not have any constraints?
8216 isl_bool
isl_basic_map_plain_is_universe(__isl_keep isl_basic_map
*bmap
)
8219 return isl_bool_error
;
8220 return bmap
->n_eq
== 0 && bmap
->n_ineq
== 0;
8223 /* Is "bset" obviously equal to the universe with the same space?
8225 isl_bool
isl_basic_set_plain_is_universe(__isl_keep isl_basic_set
*bset
)
8227 return isl_basic_map_plain_is_universe(bset
);
8230 /* If "c" does not involve any existentially quantified variables,
8231 * then set *univ to false and abort
8233 static isl_stat
involves_divs(__isl_take isl_constraint
*c
, void *user
)
8235 isl_bool
*univ
= user
;
8238 n
= isl_constraint_dim(c
, isl_dim_div
);
8239 *univ
= isl_constraint_involves_dims(c
, isl_dim_div
, 0, n
);
8240 isl_constraint_free(c
);
8241 if (*univ
< 0 || !*univ
)
8242 return isl_stat_error
;
8246 /* Is "bmap" equal to the universe with the same space?
8248 * First check if it is obviously equal to the universe.
8249 * If not and if there are any constraints not involving
8250 * existentially quantified variables, then it is certainly
8251 * not equal to the universe.
8252 * Otherwise, check if the universe is a subset of "bmap".
8254 isl_bool
isl_basic_map_is_universe(__isl_keep isl_basic_map
*bmap
)
8257 isl_basic_map
*test
;
8259 univ
= isl_basic_map_plain_is_universe(bmap
);
8260 if (univ
< 0 || univ
)
8262 if (isl_basic_map_dim(bmap
, isl_dim_div
) == 0)
8263 return isl_bool_false
;
8264 univ
= isl_bool_true
;
8265 if (isl_basic_map_foreach_constraint(bmap
, &involves_divs
, &univ
) < 0 &&
8267 return isl_bool_error
;
8268 if (univ
< 0 || !univ
)
8270 test
= isl_basic_map_universe(isl_basic_map_get_space(bmap
));
8271 univ
= isl_basic_map_is_subset(test
, bmap
);
8272 isl_basic_map_free(test
);
8276 /* Is "bset" equal to the universe with the same space?
8278 isl_bool
isl_basic_set_is_universe(__isl_keep isl_basic_set
*bset
)
8280 return isl_basic_map_is_universe(bset
);
8283 isl_bool
isl_map_plain_is_universe(__isl_keep isl_map
*map
)
8288 return isl_bool_error
;
8290 for (i
= 0; i
< map
->n
; ++i
) {
8291 isl_bool r
= isl_basic_map_plain_is_universe(map
->p
[i
]);
8296 return isl_bool_false
;
8299 isl_bool
isl_set_plain_is_universe(__isl_keep isl_set
*set
)
8301 return isl_map_plain_is_universe(set_to_map(set
));
8304 isl_bool
isl_basic_map_is_empty(__isl_keep isl_basic_map
*bmap
)
8306 struct isl_basic_set
*bset
= NULL
;
8307 struct isl_vec
*sample
= NULL
;
8308 isl_bool empty
, non_empty
;
8311 return isl_bool_error
;
8313 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
8314 return isl_bool_true
;
8316 if (isl_basic_map_plain_is_universe(bmap
))
8317 return isl_bool_false
;
8319 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
8320 struct isl_basic_map
*copy
= isl_basic_map_copy(bmap
);
8321 copy
= isl_basic_map_remove_redundancies(copy
);
8322 empty
= isl_basic_map_plain_is_empty(copy
);
8323 isl_basic_map_free(copy
);
8327 non_empty
= isl_basic_map_plain_is_non_empty(bmap
);
8329 return isl_bool_error
;
8331 return isl_bool_false
;
8332 isl_vec_free(bmap
->sample
);
8333 bmap
->sample
= NULL
;
8334 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
8336 return isl_bool_error
;
8337 sample
= isl_basic_set_sample_vec(bset
);
8339 return isl_bool_error
;
8340 empty
= sample
->size
== 0;
8341 isl_vec_free(bmap
->sample
);
8342 bmap
->sample
= sample
;
8344 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
8349 isl_bool
isl_basic_map_plain_is_empty(__isl_keep isl_basic_map
*bmap
)
8352 return isl_bool_error
;
8353 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
);
8356 isl_bool
isl_basic_set_plain_is_empty(__isl_keep isl_basic_set
*bset
)
8359 return isl_bool_error
;
8360 return ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
);
8363 /* Is "bmap" known to be non-empty?
8365 * That is, is the cached sample still valid?
8367 isl_bool
isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map
*bmap
)
8372 return isl_bool_error
;
8374 return isl_bool_false
;
8375 total
= 1 + isl_basic_map_total_dim(bmap
);
8376 if (bmap
->sample
->size
!= total
)
8377 return isl_bool_false
;
8378 return isl_basic_map_contains(bmap
, bmap
->sample
);
8381 isl_bool
isl_basic_set_is_empty(__isl_keep isl_basic_set
*bset
)
8383 return isl_basic_map_is_empty(bset_to_bmap(bset
));
8386 struct isl_map
*isl_basic_map_union(
8387 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
8389 struct isl_map
*map
;
8390 if (!bmap1
|| !bmap2
)
8393 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
8395 map
= isl_map_alloc_space(isl_space_copy(bmap1
->dim
), 2, 0);
8398 map
= isl_map_add_basic_map(map
, bmap1
);
8399 map
= isl_map_add_basic_map(map
, bmap2
);
8402 isl_basic_map_free(bmap1
);
8403 isl_basic_map_free(bmap2
);
8407 struct isl_set
*isl_basic_set_union(
8408 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
8410 return set_from_map(isl_basic_map_union(bset_to_bmap(bset1
),
8411 bset_to_bmap(bset2
)));
8414 /* Order divs such that any div only depends on previous divs */
8415 struct isl_basic_map
*isl_basic_map_order_divs(struct isl_basic_map
*bmap
)
8423 off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
8425 for (i
= 0; i
< bmap
->n_div
; ++i
) {
8427 if (isl_int_is_zero(bmap
->div
[i
][0]))
8429 pos
= isl_seq_first_non_zero(bmap
->div
[i
]+1+1+off
+i
,
8434 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_internal
,
8435 "integer division depends on itself",
8436 return isl_basic_map_free(bmap
));
8437 isl_basic_map_swap_div(bmap
, i
, i
+ pos
);
8443 struct isl_basic_set
*isl_basic_set_order_divs(struct isl_basic_set
*bset
)
8445 return bset_from_bmap(isl_basic_map_order_divs(bset_to_bmap(bset
)));
8448 __isl_give isl_map
*isl_map_order_divs(__isl_take isl_map
*map
)
8455 for (i
= 0; i
< map
->n
; ++i
) {
8456 map
->p
[i
] = isl_basic_map_order_divs(map
->p
[i
]);
8467 /* Sort the local variables of "bset".
8469 __isl_give isl_basic_set
*isl_basic_set_sort_divs(
8470 __isl_take isl_basic_set
*bset
)
8472 return bset_from_bmap(isl_basic_map_sort_divs(bset_to_bmap(bset
)));
8475 /* Apply the expansion computed by isl_merge_divs.
8476 * The expansion itself is given by "exp" while the resulting
8477 * list of divs is given by "div".
8479 * Move the integer divisions of "bmap" into the right position
8480 * according to "exp" and then introduce the additional integer
8481 * divisions, adding div constraints.
8482 * The moving should be done first to avoid moving coefficients
8483 * in the definitions of the extra integer divisions.
8485 __isl_give isl_basic_map
*isl_basic_map_expand_divs(
8486 __isl_take isl_basic_map
*bmap
, __isl_take isl_mat
*div
, int *exp
)
8491 bmap
= isl_basic_map_cow(bmap
);
8495 if (div
->n_row
< bmap
->n_div
)
8496 isl_die(isl_mat_get_ctx(div
), isl_error_invalid
,
8497 "not an expansion", goto error
);
8499 n_div
= bmap
->n_div
;
8500 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
8501 div
->n_row
- n_div
, 0,
8502 2 * (div
->n_row
- n_div
));
8504 for (i
= n_div
; i
< div
->n_row
; ++i
)
8505 if (isl_basic_map_alloc_div(bmap
) < 0)
8508 for (j
= n_div
- 1; j
>= 0; --j
) {
8511 isl_basic_map_swap_div(bmap
, j
, exp
[j
]);
8514 for (i
= 0; i
< div
->n_row
; ++i
) {
8515 if (j
< n_div
&& exp
[j
] == i
) {
8518 isl_seq_cpy(bmap
->div
[i
], div
->row
[i
], div
->n_col
);
8519 if (isl_basic_map_div_is_marked_unknown(bmap
, i
))
8521 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
8529 isl_basic_map_free(bmap
);
8534 /* Apply the expansion computed by isl_merge_divs.
8535 * The expansion itself is given by "exp" while the resulting
8536 * list of divs is given by "div".
8538 __isl_give isl_basic_set
*isl_basic_set_expand_divs(
8539 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*div
, int *exp
)
8541 return isl_basic_map_expand_divs(bset
, div
, exp
);
8544 /* Look for a div in dst that corresponds to the div "div" in src.
8545 * The divs before "div" in src and dst are assumed to be the same.
8547 * Returns -1 if no corresponding div was found and the position
8548 * of the corresponding div in dst otherwise.
8550 static int find_div(struct isl_basic_map
*dst
,
8551 struct isl_basic_map
*src
, unsigned div
)
8555 unsigned total
= isl_space_dim(src
->dim
, isl_dim_all
);
8557 isl_assert(dst
->ctx
, div
<= dst
->n_div
, return -1);
8558 for (i
= div
; i
< dst
->n_div
; ++i
)
8559 if (isl_seq_eq(dst
->div
[i
], src
->div
[div
], 1+1+total
+div
) &&
8560 isl_seq_first_non_zero(dst
->div
[i
]+1+1+total
+div
,
8561 dst
->n_div
- div
) == -1)
8566 /* Align the divs of "dst" to those of "src", adding divs from "src"
8567 * if needed. That is, make sure that the first src->n_div divs
8568 * of the result are equal to those of src.
8570 * The result is not finalized as by design it will have redundant
8571 * divs if any divs from "src" were copied.
8573 __isl_give isl_basic_map
*isl_basic_map_align_divs(
8574 __isl_take isl_basic_map
*dst
, __isl_keep isl_basic_map
*src
)
8577 int known
, extended
;
8581 return isl_basic_map_free(dst
);
8583 if (src
->n_div
== 0)
8586 known
= isl_basic_map_divs_known(src
);
8588 return isl_basic_map_free(dst
);
8590 isl_die(isl_basic_map_get_ctx(src
), isl_error_invalid
,
8591 "some src divs are unknown",
8592 return isl_basic_map_free(dst
));
8594 src
= isl_basic_map_order_divs(src
);
8597 total
= isl_space_dim(src
->dim
, isl_dim_all
);
8598 for (i
= 0; i
< src
->n_div
; ++i
) {
8599 int j
= find_div(dst
, src
, i
);
8602 int extra
= src
->n_div
- i
;
8603 dst
= isl_basic_map_cow(dst
);
8606 dst
= isl_basic_map_extend_space(dst
,
8607 isl_space_copy(dst
->dim
),
8608 extra
, 0, 2 * extra
);
8611 j
= isl_basic_map_alloc_div(dst
);
8613 return isl_basic_map_free(dst
);
8614 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
+i
);
8615 isl_seq_clr(dst
->div
[j
]+1+1+total
+i
, dst
->n_div
- i
);
8616 if (isl_basic_map_add_div_constraints(dst
, j
) < 0)
8617 return isl_basic_map_free(dst
);
8620 isl_basic_map_swap_div(dst
, i
, j
);
8625 struct isl_basic_set
*isl_basic_set_align_divs(
8626 struct isl_basic_set
*dst
, struct isl_basic_set
*src
)
8628 return bset_from_bmap(isl_basic_map_align_divs(bset_to_bmap(dst
),
8629 bset_to_bmap(src
)));
8632 struct isl_map
*isl_map_align_divs(struct isl_map
*map
)
8640 map
= isl_map_compute_divs(map
);
8641 map
= isl_map_cow(map
);
8645 for (i
= 1; i
< map
->n
; ++i
)
8646 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], map
->p
[i
]);
8647 for (i
= 1; i
< map
->n
; ++i
) {
8648 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], map
->p
[0]);
8650 return isl_map_free(map
);
8653 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
8657 struct isl_set
*isl_set_align_divs(struct isl_set
*set
)
8659 return set_from_map(isl_map_align_divs(set_to_map(set
)));
8662 /* Align the divs of the basic maps in "map" to those
8663 * of the basic maps in "list", as well as to the other basic maps in "map".
8664 * The elements in "list" are assumed to have known divs.
8666 __isl_give isl_map
*isl_map_align_divs_to_basic_map_list(
8667 __isl_take isl_map
*map
, __isl_keep isl_basic_map_list
*list
)
8671 map
= isl_map_compute_divs(map
);
8672 map
= isl_map_cow(map
);
8674 return isl_map_free(map
);
8678 n
= isl_basic_map_list_n_basic_map(list
);
8679 for (i
= 0; i
< n
; ++i
) {
8680 isl_basic_map
*bmap
;
8682 bmap
= isl_basic_map_list_get_basic_map(list
, i
);
8683 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], bmap
);
8684 isl_basic_map_free(bmap
);
8687 return isl_map_free(map
);
8689 return isl_map_align_divs(map
);
8692 /* Align the divs of each element of "list" to those of "bmap".
8693 * Both "bmap" and the elements of "list" are assumed to have known divs.
8695 __isl_give isl_basic_map_list
*isl_basic_map_list_align_divs_to_basic_map(
8696 __isl_take isl_basic_map_list
*list
, __isl_keep isl_basic_map
*bmap
)
8701 return isl_basic_map_list_free(list
);
8703 n
= isl_basic_map_list_n_basic_map(list
);
8704 for (i
= 0; i
< n
; ++i
) {
8705 isl_basic_map
*bmap_i
;
8707 bmap_i
= isl_basic_map_list_get_basic_map(list
, i
);
8708 bmap_i
= isl_basic_map_align_divs(bmap_i
, bmap
);
8709 list
= isl_basic_map_list_set_basic_map(list
, i
, bmap_i
);
8715 static __isl_give isl_set
*set_apply( __isl_take isl_set
*set
,
8716 __isl_take isl_map
*map
)
8720 isl_assert(set
->ctx
, isl_map_compatible_domain(map
, set
), goto error
);
8721 map
= isl_map_intersect_domain(map
, set
);
8722 set
= isl_map_range(map
);
8730 __isl_give isl_set
*isl_set_apply( __isl_take isl_set
*set
,
8731 __isl_take isl_map
*map
)
8733 return isl_map_align_params_map_map_and(set
, map
, &set_apply
);
8736 /* There is no need to cow as removing empty parts doesn't change
8737 * the meaning of the set.
8739 struct isl_map
*isl_map_remove_empty_parts(struct isl_map
*map
)
8746 for (i
= map
->n
- 1; i
>= 0; --i
)
8747 remove_if_empty(map
, i
);
8752 struct isl_set
*isl_set_remove_empty_parts(struct isl_set
*set
)
8754 return set_from_map(isl_map_remove_empty_parts(set_to_map(set
)));
8757 static __isl_give isl_basic_map
*map_copy_basic_map(__isl_keep isl_map
*map
)
8759 struct isl_basic_map
*bmap
;
8760 if (!map
|| map
->n
== 0)
8762 bmap
= map
->p
[map
->n
-1];
8763 isl_assert(map
->ctx
, ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
), return NULL
);
8764 return isl_basic_map_copy(bmap
);
8767 __isl_give isl_basic_map
*isl_map_copy_basic_map(__isl_keep isl_map
*map
)
8769 return map_copy_basic_map(map
);
8772 struct isl_basic_set
*isl_set_copy_basic_set(struct isl_set
*set
)
8774 return bset_from_bmap(map_copy_basic_map(set_to_map(set
)));
8777 static __isl_give isl_map
*map_drop_basic_map(__isl_take isl_map
*map
,
8778 __isl_keep isl_basic_map
*bmap
)
8784 for (i
= map
->n
-1; i
>= 0; --i
) {
8785 if (map
->p
[i
] != bmap
)
8787 map
= isl_map_cow(map
);
8790 isl_basic_map_free(map
->p
[i
]);
8791 if (i
!= map
->n
-1) {
8792 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
8793 map
->p
[i
] = map
->p
[map
->n
-1];
8804 __isl_give isl_map
*isl_map_drop_basic_map(__isl_take isl_map
*map
,
8805 __isl_keep isl_basic_map
*bmap
)
8807 return map_drop_basic_map(map
, bmap
);
8810 struct isl_set
*isl_set_drop_basic_set(struct isl_set
*set
,
8811 struct isl_basic_set
*bset
)
8813 return set_from_map(map_drop_basic_map(set_to_map(set
),
8814 bset_to_bmap(bset
)));
8817 /* Given two basic sets bset1 and bset2, compute the maximal difference
8818 * between the values of dimension pos in bset1 and those in bset2
8819 * for any common value of the parameters and dimensions preceding pos.
8821 static enum isl_lp_result
basic_set_maximal_difference_at(
8822 __isl_keep isl_basic_set
*bset1
, __isl_keep isl_basic_set
*bset2
,
8823 int pos
, isl_int
*opt
)
8825 isl_basic_map
*bmap1
;
8826 isl_basic_map
*bmap2
;
8827 struct isl_ctx
*ctx
;
8828 struct isl_vec
*obj
;
8832 enum isl_lp_result res
;
8834 if (!bset1
|| !bset2
)
8835 return isl_lp_error
;
8837 nparam
= isl_basic_set_n_param(bset1
);
8838 dim1
= isl_basic_set_n_dim(bset1
);
8840 bmap1
= isl_basic_map_from_range(isl_basic_set_copy(bset1
));
8841 bmap2
= isl_basic_map_from_range(isl_basic_set_copy(bset2
));
8842 bmap1
= isl_basic_map_move_dims(bmap1
, isl_dim_in
, 0,
8843 isl_dim_out
, 0, pos
);
8844 bmap2
= isl_basic_map_move_dims(bmap2
, isl_dim_in
, 0,
8845 isl_dim_out
, 0, pos
);
8846 bmap1
= isl_basic_map_range_product(bmap1
, bmap2
);
8848 return isl_lp_error
;
8850 total
= isl_basic_map_total_dim(bmap1
);
8852 obj
= isl_vec_alloc(ctx
, 1 + total
);
8855 isl_seq_clr(obj
->block
.data
, 1 + total
);
8856 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
], 1);
8857 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
+(dim1
-pos
)], -1);
8858 res
= isl_basic_map_solve_lp(bmap1
, 1, obj
->block
.data
, ctx
->one
,
8860 isl_basic_map_free(bmap1
);
8864 isl_basic_map_free(bmap1
);
8865 return isl_lp_error
;
8868 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
8869 * for any common value of the parameters and dimensions preceding pos
8870 * in both basic sets, the values of dimension pos in bset1 are
8871 * smaller or larger than those in bset2.
8874 * 1 if bset1 follows bset2
8875 * -1 if bset1 precedes bset2
8876 * 0 if bset1 and bset2 are incomparable
8877 * -2 if some error occurred.
8879 int isl_basic_set_compare_at(struct isl_basic_set
*bset1
,
8880 struct 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
))
8904 /* Given two basic sets bset1 and bset2, check whether
8905 * for any common value of the parameters and dimensions preceding pos
8906 * there is a value of dimension pos in bset1 that is larger
8907 * than a value of the same dimension in bset2.
8910 * 1 if there exists such a pair
8911 * 0 if there is no such pair, but there is a pair of equal values
8913 * -2 if some error occurred.
8915 int isl_basic_set_follows_at(__isl_keep isl_basic_set
*bset1
,
8916 __isl_keep isl_basic_set
*bset2
, int pos
)
8919 enum isl_lp_result res
;
8924 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
8926 if (res
== isl_lp_empty
)
8928 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
8929 res
== isl_lp_unbounded
)
8931 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
8933 else if (res
== isl_lp_ok
)
8942 /* Given two sets set1 and set2, check whether
8943 * for any common value of the parameters and dimensions preceding pos
8944 * there is a value of dimension pos in set1 that is larger
8945 * than a value of the same dimension in set2.
8948 * 1 if there exists such a pair
8949 * 0 if there is no such pair, but there is a pair of equal values
8951 * -2 if some error occurred.
8953 int isl_set_follows_at(__isl_keep isl_set
*set1
,
8954 __isl_keep isl_set
*set2
, int pos
)
8962 for (i
= 0; i
< set1
->n
; ++i
)
8963 for (j
= 0; j
< set2
->n
; ++j
) {
8965 f
= isl_basic_set_follows_at(set1
->p
[i
], set2
->p
[j
], pos
);
8966 if (f
== 1 || f
== -2)
8975 static isl_bool
isl_basic_map_plain_has_fixed_var(
8976 __isl_keep isl_basic_map
*bmap
, unsigned pos
, isl_int
*val
)
8983 return isl_bool_error
;
8984 total
= isl_basic_map_total_dim(bmap
);
8985 for (i
= 0, d
= total
-1; i
< bmap
->n_eq
&& d
+1 > pos
; ++i
) {
8986 for (; d
+1 > pos
; --d
)
8987 if (!isl_int_is_zero(bmap
->eq
[i
][1+d
]))
8991 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1, d
) != -1)
8992 return isl_bool_false
;
8993 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1+d
+1, total
-d
-1) != -1)
8994 return isl_bool_false
;
8995 if (!isl_int_is_one(bmap
->eq
[i
][1+d
]))
8996 return isl_bool_false
;
8998 isl_int_neg(*val
, bmap
->eq
[i
][0]);
8999 return isl_bool_true
;
9001 return isl_bool_false
;
9004 static int isl_map_plain_has_fixed_var(__isl_keep isl_map
*map
,
9005 unsigned pos
, isl_int
*val
)
9017 return isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, val
);
9020 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, &v
);
9021 for (i
= 1; fixed
== isl_bool_true
&& i
< map
->n
; ++i
) {
9022 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[i
], pos
, &tmp
);
9023 if (fixed
== isl_bool_true
&& isl_int_ne(tmp
, v
))
9024 fixed
= isl_bool_false
;
9027 isl_int_set(*val
, v
);
9033 static isl_bool
isl_basic_set_plain_has_fixed_var(
9034 __isl_keep isl_basic_set
*bset
, unsigned pos
, isl_int
*val
)
9036 return isl_basic_map_plain_has_fixed_var(bset_to_bmap(bset
),
9040 static int isl_set_plain_has_fixed_var(__isl_keep isl_set
*set
, unsigned pos
,
9043 return isl_map_plain_has_fixed_var(set_to_map(set
), pos
, val
);
9046 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map
*bmap
,
9047 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
9049 if (pos
>= isl_basic_map_dim(bmap
, type
))
9051 return isl_basic_map_plain_has_fixed_var(bmap
,
9052 isl_basic_map_offset(bmap
, type
) - 1 + pos
, val
);
9055 /* If "bmap" obviously lies on a hyperplane where the given dimension
9056 * has a fixed value, then return that value.
9057 * Otherwise return NaN.
9059 __isl_give isl_val
*isl_basic_map_plain_get_val_if_fixed(
9060 __isl_keep isl_basic_map
*bmap
,
9061 enum isl_dim_type type
, unsigned pos
)
9069 ctx
= isl_basic_map_get_ctx(bmap
);
9070 v
= isl_val_alloc(ctx
);
9073 fixed
= isl_basic_map_plain_is_fixed(bmap
, type
, pos
, &v
->n
);
9075 return isl_val_free(v
);
9077 isl_int_set_si(v
->d
, 1);
9081 return isl_val_nan(ctx
);
9084 int isl_map_plain_is_fixed(__isl_keep isl_map
*map
,
9085 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
9087 if (pos
>= isl_map_dim(map
, type
))
9089 return isl_map_plain_has_fixed_var(map
,
9090 map_offset(map
, type
) - 1 + pos
, val
);
9093 /* If "map" obviously lies on a hyperplane where the given dimension
9094 * has a fixed value, then return that value.
9095 * Otherwise return NaN.
9097 __isl_give isl_val
*isl_map_plain_get_val_if_fixed(__isl_keep isl_map
*map
,
9098 enum isl_dim_type type
, unsigned pos
)
9106 ctx
= isl_map_get_ctx(map
);
9107 v
= isl_val_alloc(ctx
);
9110 fixed
= isl_map_plain_is_fixed(map
, type
, pos
, &v
->n
);
9112 return isl_val_free(v
);
9114 isl_int_set_si(v
->d
, 1);
9118 return isl_val_nan(ctx
);
9121 /* If "set" obviously lies on a hyperplane where the given dimension
9122 * has a fixed value, then return that value.
9123 * Otherwise return NaN.
9125 __isl_give isl_val
*isl_set_plain_get_val_if_fixed(__isl_keep isl_set
*set
,
9126 enum isl_dim_type type
, unsigned pos
)
9128 return isl_map_plain_get_val_if_fixed(set
, type
, pos
);
9131 int isl_set_plain_is_fixed(__isl_keep isl_set
*set
,
9132 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
9134 return isl_map_plain_is_fixed(set
, type
, pos
, val
);
9137 /* Check if dimension dim has fixed value and if so and if val is not NULL,
9138 * then return this fixed value in *val.
9140 isl_bool
isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set
*bset
,
9141 unsigned dim
, isl_int
*val
)
9143 return isl_basic_set_plain_has_fixed_var(bset
,
9144 isl_basic_set_n_param(bset
) + dim
, val
);
9147 /* Check if dimension dim has fixed value and if so and if val is not NULL,
9148 * then return this fixed value in *val.
9150 int isl_set_plain_dim_is_fixed(__isl_keep isl_set
*set
,
9151 unsigned dim
, isl_int
*val
)
9153 return isl_set_plain_has_fixed_var(set
, isl_set_n_param(set
) + dim
, val
);
9156 /* Check if input variable in has fixed value and if so and if val is not NULL,
9157 * then return this fixed value in *val.
9159 int isl_map_plain_input_is_fixed(__isl_keep isl_map
*map
,
9160 unsigned in
, isl_int
*val
)
9162 return isl_map_plain_has_fixed_var(map
, isl_map_n_param(map
) + in
, val
);
9165 /* Check if dimension dim has an (obvious) fixed lower bound and if so
9166 * and if val is not NULL, then return this lower bound in *val.
9168 int isl_basic_set_plain_dim_has_fixed_lower_bound(
9169 __isl_keep isl_basic_set
*bset
, unsigned dim
, isl_int
*val
)
9171 int i
, i_eq
= -1, i_ineq
= -1;
9178 total
= isl_basic_set_total_dim(bset
);
9179 nparam
= isl_basic_set_n_param(bset
);
9180 for (i
= 0; i
< bset
->n_eq
; ++i
) {
9181 if (isl_int_is_zero(bset
->eq
[i
][1+nparam
+dim
]))
9187 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
9188 if (!isl_int_is_pos(bset
->ineq
[i
][1+nparam
+dim
]))
9190 if (i_eq
!= -1 || i_ineq
!= -1)
9194 if (i_eq
== -1 && i_ineq
== -1)
9196 c
= i_eq
!= -1 ? bset
->eq
[i_eq
] : bset
->ineq
[i_ineq
];
9197 /* The coefficient should always be one due to normalization. */
9198 if (!isl_int_is_one(c
[1+nparam
+dim
]))
9200 if (isl_seq_first_non_zero(c
+1, nparam
+dim
) != -1)
9202 if (isl_seq_first_non_zero(c
+1+nparam
+dim
+1,
9203 total
- nparam
- dim
- 1) != -1)
9206 isl_int_neg(*val
, c
[0]);
9210 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set
*set
,
9211 unsigned dim
, isl_int
*val
)
9223 return isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[0],
9227 fixed
= isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[0],
9229 for (i
= 1; fixed
== 1 && i
< set
->n
; ++i
) {
9230 fixed
= isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[i
],
9232 if (fixed
== 1 && isl_int_ne(tmp
, v
))
9236 isl_int_set(*val
, v
);
9242 /* Return -1 if the constraint "c1" should be sorted before "c2"
9243 * and 1 if it should be sorted after "c2".
9244 * Return 0 if the two constraints are the same (up to the constant term).
9246 * In particular, if a constraint involves later variables than another
9247 * then it is sorted after this other constraint.
9248 * uset_gist depends on constraints without existentially quantified
9249 * variables sorting first.
9251 * For constraints that have the same latest variable, those
9252 * with the same coefficient for this latest variable (first in absolute value
9253 * and then in actual value) are grouped together.
9254 * This is useful for detecting pairs of constraints that can
9255 * be chained in their printed representation.
9257 * Finally, within a group, constraints are sorted according to
9258 * their coefficients (excluding the constant term).
9260 static int sort_constraint_cmp(const void *p1
, const void *p2
, void *arg
)
9262 isl_int
**c1
= (isl_int
**) p1
;
9263 isl_int
**c2
= (isl_int
**) p2
;
9265 unsigned size
= *(unsigned *) arg
;
9268 l1
= isl_seq_last_non_zero(*c1
+ 1, size
);
9269 l2
= isl_seq_last_non_zero(*c2
+ 1, size
);
9274 cmp
= isl_int_abs_cmp((*c1
)[1 + l1
], (*c2
)[1 + l1
]);
9277 cmp
= isl_int_cmp((*c1
)[1 + l1
], (*c2
)[1 + l1
]);
9281 return isl_seq_cmp(*c1
+ 1, *c2
+ 1, size
);
9284 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
9285 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
9286 * and 0 if the two constraints are the same (up to the constant term).
9288 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map
*bmap
,
9289 isl_int
*c1
, isl_int
*c2
)
9295 total
= isl_basic_map_total_dim(bmap
);
9296 return sort_constraint_cmp(&c1
, &c2
, &total
);
9299 __isl_give isl_basic_map
*isl_basic_map_sort_constraints(
9300 __isl_take isl_basic_map
*bmap
)
9306 if (bmap
->n_ineq
== 0)
9308 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED
))
9310 total
= isl_basic_map_total_dim(bmap
);
9311 if (isl_sort(bmap
->ineq
, bmap
->n_ineq
, sizeof(isl_int
*),
9312 &sort_constraint_cmp
, &total
) < 0)
9313 return isl_basic_map_free(bmap
);
9317 __isl_give isl_basic_set
*isl_basic_set_sort_constraints(
9318 __isl_take isl_basic_set
*bset
)
9320 isl_basic_map
*bmap
= bset_to_bmap(bset
);
9321 return bset_from_bmap(isl_basic_map_sort_constraints(bmap
));
9324 struct isl_basic_map
*isl_basic_map_normalize(struct isl_basic_map
*bmap
)
9328 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED
))
9330 bmap
= isl_basic_map_remove_redundancies(bmap
);
9331 bmap
= isl_basic_map_sort_constraints(bmap
);
9333 ISL_F_SET(bmap
, ISL_BASIC_MAP_NORMALIZED
);
9337 struct isl_basic_set
*isl_basic_set_normalize(struct isl_basic_set
*bset
)
9339 return bset_from_bmap(isl_basic_map_normalize(bset_to_bmap(bset
)));
9342 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map
*bmap1
,
9343 const __isl_keep isl_basic_map
*bmap2
)
9348 if (!bmap1
|| !bmap2
)
9353 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) !=
9354 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_RATIONAL
))
9355 return ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) ? -1 : 1;
9356 if (isl_basic_map_n_param(bmap1
) != isl_basic_map_n_param(bmap2
))
9357 return isl_basic_map_n_param(bmap1
) - isl_basic_map_n_param(bmap2
);
9358 if (isl_basic_map_n_in(bmap1
) != isl_basic_map_n_in(bmap2
))
9359 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
9360 if (isl_basic_map_n_out(bmap1
) != isl_basic_map_n_out(bmap2
))
9361 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
9362 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
) &&
9363 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
9365 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
))
9367 if (ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
9369 if (bmap1
->n_eq
!= bmap2
->n_eq
)
9370 return bmap1
->n_eq
- bmap2
->n_eq
;
9371 if (bmap1
->n_ineq
!= bmap2
->n_ineq
)
9372 return bmap1
->n_ineq
- bmap2
->n_ineq
;
9373 if (bmap1
->n_div
!= bmap2
->n_div
)
9374 return bmap1
->n_div
- bmap2
->n_div
;
9375 total
= isl_basic_map_total_dim(bmap1
);
9376 for (i
= 0; i
< bmap1
->n_eq
; ++i
) {
9377 cmp
= isl_seq_cmp(bmap1
->eq
[i
], bmap2
->eq
[i
], 1+total
);
9381 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
9382 cmp
= isl_seq_cmp(bmap1
->ineq
[i
], bmap2
->ineq
[i
], 1+total
);
9386 for (i
= 0; i
< bmap1
->n_div
; ++i
) {
9387 cmp
= isl_seq_cmp(bmap1
->div
[i
], bmap2
->div
[i
], 1+1+total
);
9394 int isl_basic_set_plain_cmp(const __isl_keep isl_basic_set
*bset1
,
9395 const __isl_keep isl_basic_set
*bset2
)
9397 return isl_basic_map_plain_cmp(bset1
, bset2
);
9400 int isl_set_plain_cmp(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
9406 if (set1
->n
!= set2
->n
)
9407 return set1
->n
- set2
->n
;
9409 for (i
= 0; i
< set1
->n
; ++i
) {
9410 cmp
= isl_basic_set_plain_cmp(set1
->p
[i
], set2
->p
[i
]);
9418 isl_bool
isl_basic_map_plain_is_equal(__isl_keep isl_basic_map
*bmap1
,
9419 __isl_keep isl_basic_map
*bmap2
)
9421 if (!bmap1
|| !bmap2
)
9422 return isl_bool_error
;
9423 return isl_basic_map_plain_cmp(bmap1
, bmap2
) == 0;
9426 isl_bool
isl_basic_set_plain_is_equal(__isl_keep isl_basic_set
*bset1
,
9427 __isl_keep isl_basic_set
*bset2
)
9429 return isl_basic_map_plain_is_equal(bset_to_bmap(bset1
),
9430 bset_to_bmap(bset2
));
9433 static int qsort_bmap_cmp(const void *p1
, const void *p2
)
9435 const struct isl_basic_map
*bmap1
= *(const struct isl_basic_map
**)p1
;
9436 const struct isl_basic_map
*bmap2
= *(const struct isl_basic_map
**)p2
;
9438 return isl_basic_map_plain_cmp(bmap1
, bmap2
);
9441 /* Sort the basic maps of "map" and remove duplicate basic maps.
9443 * While removing basic maps, we make sure that the basic maps remain
9444 * sorted because isl_map_normalize expects the basic maps of the result
9447 static __isl_give isl_map
*sort_and_remove_duplicates(__isl_take isl_map
*map
)
9451 map
= isl_map_remove_empty_parts(map
);
9454 qsort(map
->p
, map
->n
, sizeof(struct isl_basic_map
*), qsort_bmap_cmp
);
9455 for (i
= map
->n
- 1; i
>= 1; --i
) {
9456 if (!isl_basic_map_plain_is_equal(map
->p
[i
- 1], map
->p
[i
]))
9458 isl_basic_map_free(map
->p
[i
-1]);
9459 for (j
= i
; j
< map
->n
; ++j
)
9460 map
->p
[j
- 1] = map
->p
[j
];
9467 /* Remove obvious duplicates among the basic maps of "map".
9469 * Unlike isl_map_normalize, this function does not remove redundant
9470 * constraints and only removes duplicates that have exactly the same
9471 * constraints in the input. It does sort the constraints and
9472 * the basic maps to ease the detection of duplicates.
9474 * If "map" has already been normalized or if the basic maps are
9475 * disjoint, then there can be no duplicates.
9477 __isl_give isl_map
*isl_map_remove_obvious_duplicates(__isl_take isl_map
*map
)
9480 isl_basic_map
*bmap
;
9486 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
| ISL_MAP_DISJOINT
))
9488 for (i
= 0; i
< map
->n
; ++i
) {
9489 bmap
= isl_basic_map_copy(map
->p
[i
]);
9490 bmap
= isl_basic_map_sort_constraints(bmap
);
9492 return isl_map_free(map
);
9493 isl_basic_map_free(map
->p
[i
]);
9497 map
= sort_and_remove_duplicates(map
);
9501 /* We normalize in place, but if anything goes wrong we need
9502 * to return NULL, so we need to make sure we don't change the
9503 * meaning of any possible other copies of map.
9505 __isl_give isl_map
*isl_map_normalize(__isl_take isl_map
*map
)
9508 struct isl_basic_map
*bmap
;
9512 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
))
9514 for (i
= 0; i
< map
->n
; ++i
) {
9515 bmap
= isl_basic_map_normalize(isl_basic_map_copy(map
->p
[i
]));
9518 isl_basic_map_free(map
->p
[i
]);
9522 map
= sort_and_remove_duplicates(map
);
9524 ISL_F_SET(map
, ISL_MAP_NORMALIZED
);
9531 struct isl_set
*isl_set_normalize(struct isl_set
*set
)
9533 return set_from_map(isl_map_normalize(set_to_map(set
)));
9536 isl_bool
isl_map_plain_is_equal(__isl_keep isl_map
*map1
,
9537 __isl_keep isl_map
*map2
)
9543 return isl_bool_error
;
9546 return isl_bool_true
;
9547 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
9548 return isl_bool_false
;
9550 map1
= isl_map_copy(map1
);
9551 map2
= isl_map_copy(map2
);
9552 map1
= isl_map_normalize(map1
);
9553 map2
= isl_map_normalize(map2
);
9556 equal
= map1
->n
== map2
->n
;
9557 for (i
= 0; equal
&& i
< map1
->n
; ++i
) {
9558 equal
= isl_basic_map_plain_is_equal(map1
->p
[i
], map2
->p
[i
]);
9568 return isl_bool_error
;
9571 isl_bool
isl_set_plain_is_equal(__isl_keep isl_set
*set1
,
9572 __isl_keep isl_set
*set2
)
9574 return isl_map_plain_is_equal(set_to_map(set1
), set_to_map(set2
));
9577 /* Return an interval that ranges from min to max (inclusive)
9579 struct isl_basic_set
*isl_basic_set_interval(struct isl_ctx
*ctx
,
9580 isl_int min
, isl_int max
)
9583 struct isl_basic_set
*bset
= NULL
;
9585 bset
= isl_basic_set_alloc(ctx
, 0, 1, 0, 0, 2);
9589 k
= isl_basic_set_alloc_inequality(bset
);
9592 isl_int_set_si(bset
->ineq
[k
][1], 1);
9593 isl_int_neg(bset
->ineq
[k
][0], min
);
9595 k
= isl_basic_set_alloc_inequality(bset
);
9598 isl_int_set_si(bset
->ineq
[k
][1], -1);
9599 isl_int_set(bset
->ineq
[k
][0], max
);
9603 isl_basic_set_free(bset
);
9607 /* Return the basic maps in "map" as a list.
9609 __isl_give isl_basic_map_list
*isl_map_get_basic_map_list(
9610 __isl_keep isl_map
*map
)
9614 isl_basic_map_list
*list
;
9618 ctx
= isl_map_get_ctx(map
);
9619 list
= isl_basic_map_list_alloc(ctx
, map
->n
);
9621 for (i
= 0; i
< map
->n
; ++i
) {
9622 isl_basic_map
*bmap
;
9624 bmap
= isl_basic_map_copy(map
->p
[i
]);
9625 list
= isl_basic_map_list_add(list
, bmap
);
9631 /* Return the intersection of the elements in the non-empty list "list".
9632 * All elements are assumed to live in the same space.
9634 __isl_give isl_basic_map
*isl_basic_map_list_intersect(
9635 __isl_take isl_basic_map_list
*list
)
9638 isl_basic_map
*bmap
;
9642 n
= isl_basic_map_list_n_basic_map(list
);
9644 isl_die(isl_basic_map_list_get_ctx(list
), isl_error_invalid
,
9645 "expecting non-empty list", goto error
);
9647 bmap
= isl_basic_map_list_get_basic_map(list
, 0);
9648 for (i
= 1; i
< n
; ++i
) {
9649 isl_basic_map
*bmap_i
;
9651 bmap_i
= isl_basic_map_list_get_basic_map(list
, i
);
9652 bmap
= isl_basic_map_intersect(bmap
, bmap_i
);
9655 isl_basic_map_list_free(list
);
9658 isl_basic_map_list_free(list
);
9662 /* Return the intersection of the elements in the non-empty list "list".
9663 * All elements are assumed to live in the same space.
9665 __isl_give isl_basic_set
*isl_basic_set_list_intersect(
9666 __isl_take isl_basic_set_list
*list
)
9668 return isl_basic_map_list_intersect(list
);
9671 /* Return the union of the elements of "list".
9672 * The list is required to have at least one element.
9674 __isl_give isl_set
*isl_basic_set_list_union(
9675 __isl_take isl_basic_set_list
*list
)
9679 isl_basic_set
*bset
;
9684 n
= isl_basic_set_list_n_basic_set(list
);
9686 isl_die(isl_basic_set_list_get_ctx(list
), isl_error_invalid
,
9687 "expecting non-empty list", goto error
);
9689 bset
= isl_basic_set_list_get_basic_set(list
, 0);
9690 space
= isl_basic_set_get_space(bset
);
9691 isl_basic_set_free(bset
);
9693 set
= isl_set_alloc_space(space
, n
, 0);
9694 for (i
= 0; i
< n
; ++i
) {
9695 bset
= isl_basic_set_list_get_basic_set(list
, i
);
9696 set
= isl_set_add_basic_set(set
, bset
);
9699 isl_basic_set_list_free(list
);
9702 isl_basic_set_list_free(list
);
9706 /* Return the union of the elements in the non-empty list "list".
9707 * All elements are assumed to live in the same space.
9709 __isl_give isl_set
*isl_set_list_union(__isl_take isl_set_list
*list
)
9716 n
= isl_set_list_n_set(list
);
9718 isl_die(isl_set_list_get_ctx(list
), isl_error_invalid
,
9719 "expecting non-empty list", goto error
);
9721 set
= isl_set_list_get_set(list
, 0);
9722 for (i
= 1; i
< n
; ++i
) {
9725 set_i
= isl_set_list_get_set(list
, i
);
9726 set
= isl_set_union(set
, set_i
);
9729 isl_set_list_free(list
);
9732 isl_set_list_free(list
);
9736 /* Return the Cartesian product of the basic sets in list (in the given order).
9738 __isl_give isl_basic_set
*isl_basic_set_list_product(
9739 __isl_take
struct isl_basic_set_list
*list
)
9747 struct isl_basic_set
*product
= NULL
;
9751 isl_assert(list
->ctx
, list
->n
> 0, goto error
);
9752 isl_assert(list
->ctx
, list
->p
[0], goto error
);
9753 nparam
= isl_basic_set_n_param(list
->p
[0]);
9754 dim
= isl_basic_set_n_dim(list
->p
[0]);
9755 extra
= list
->p
[0]->n_div
;
9756 n_eq
= list
->p
[0]->n_eq
;
9757 n_ineq
= list
->p
[0]->n_ineq
;
9758 for (i
= 1; i
< list
->n
; ++i
) {
9759 isl_assert(list
->ctx
, list
->p
[i
], goto error
);
9760 isl_assert(list
->ctx
,
9761 nparam
== isl_basic_set_n_param(list
->p
[i
]), goto error
);
9762 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
9763 extra
+= list
->p
[i
]->n_div
;
9764 n_eq
+= list
->p
[i
]->n_eq
;
9765 n_ineq
+= list
->p
[i
]->n_ineq
;
9767 product
= isl_basic_set_alloc(list
->ctx
, nparam
, dim
, extra
,
9772 for (i
= 0; i
< list
->n
; ++i
) {
9773 isl_basic_set_add_constraints(product
,
9774 isl_basic_set_copy(list
->p
[i
]), dim
);
9775 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
9777 isl_basic_set_list_free(list
);
9780 isl_basic_set_free(product
);
9781 isl_basic_set_list_free(list
);
9785 struct isl_basic_map
*isl_basic_map_product(
9786 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
9788 isl_space
*dim_result
= NULL
;
9789 struct isl_basic_map
*bmap
;
9790 unsigned in1
, in2
, out1
, out2
, nparam
, total
, pos
;
9791 struct isl_dim_map
*dim_map1
, *dim_map2
;
9793 if (!bmap1
|| !bmap2
)
9796 isl_assert(bmap1
->ctx
, isl_space_match(bmap1
->dim
, isl_dim_param
,
9797 bmap2
->dim
, isl_dim_param
), goto error
);
9798 dim_result
= isl_space_product(isl_space_copy(bmap1
->dim
),
9799 isl_space_copy(bmap2
->dim
));
9801 in1
= isl_basic_map_n_in(bmap1
);
9802 in2
= isl_basic_map_n_in(bmap2
);
9803 out1
= isl_basic_map_n_out(bmap1
);
9804 out2
= isl_basic_map_n_out(bmap2
);
9805 nparam
= isl_basic_map_n_param(bmap1
);
9807 total
= nparam
+ in1
+ in2
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
9808 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9809 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9810 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9811 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9812 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9813 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
9814 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
9815 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
9816 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
9817 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9819 bmap
= isl_basic_map_alloc_space(dim_result
,
9820 bmap1
->n_div
+ bmap2
->n_div
,
9821 bmap1
->n_eq
+ bmap2
->n_eq
,
9822 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9823 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9824 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9825 bmap
= isl_basic_map_simplify(bmap
);
9826 return isl_basic_map_finalize(bmap
);
9828 isl_basic_map_free(bmap1
);
9829 isl_basic_map_free(bmap2
);
9833 __isl_give isl_basic_map
*isl_basic_map_flat_product(
9834 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9836 isl_basic_map
*prod
;
9838 prod
= isl_basic_map_product(bmap1
, bmap2
);
9839 prod
= isl_basic_map_flatten(prod
);
9843 __isl_give isl_basic_set
*isl_basic_set_flat_product(
9844 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
9846 return isl_basic_map_flat_range_product(bset1
, bset2
);
9849 __isl_give isl_basic_map
*isl_basic_map_domain_product(
9850 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9852 isl_space
*space_result
= NULL
;
9853 isl_basic_map
*bmap
;
9854 unsigned in1
, in2
, out
, nparam
, total
, pos
;
9855 struct isl_dim_map
*dim_map1
, *dim_map2
;
9857 if (!bmap1
|| !bmap2
)
9860 space_result
= isl_space_domain_product(isl_space_copy(bmap1
->dim
),
9861 isl_space_copy(bmap2
->dim
));
9863 in1
= isl_basic_map_dim(bmap1
, isl_dim_in
);
9864 in2
= isl_basic_map_dim(bmap2
, isl_dim_in
);
9865 out
= isl_basic_map_dim(bmap1
, isl_dim_out
);
9866 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
9868 total
= nparam
+ in1
+ in2
+ out
+ bmap1
->n_div
+ bmap2
->n_div
;
9869 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9870 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9871 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9872 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9873 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9874 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
9875 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
9876 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
);
9877 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out
);
9878 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9880 bmap
= isl_basic_map_alloc_space(space_result
,
9881 bmap1
->n_div
+ bmap2
->n_div
,
9882 bmap1
->n_eq
+ bmap2
->n_eq
,
9883 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9884 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9885 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9886 bmap
= isl_basic_map_simplify(bmap
);
9887 return isl_basic_map_finalize(bmap
);
9889 isl_basic_map_free(bmap1
);
9890 isl_basic_map_free(bmap2
);
9894 __isl_give isl_basic_map
*isl_basic_map_range_product(
9895 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9898 isl_space
*dim_result
= NULL
;
9899 isl_basic_map
*bmap
;
9900 unsigned in
, out1
, out2
, nparam
, total
, pos
;
9901 struct isl_dim_map
*dim_map1
, *dim_map2
;
9903 rational
= isl_basic_map_is_rational(bmap1
);
9904 if (rational
>= 0 && rational
)
9905 rational
= isl_basic_map_is_rational(bmap2
);
9906 if (!bmap1
|| !bmap2
|| rational
< 0)
9909 if (!isl_space_match(bmap1
->dim
, isl_dim_param
,
9910 bmap2
->dim
, isl_dim_param
))
9911 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
9912 "parameters don't match", goto error
);
9914 dim_result
= isl_space_range_product(isl_space_copy(bmap1
->dim
),
9915 isl_space_copy(bmap2
->dim
));
9917 in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
9918 out1
= isl_basic_map_n_out(bmap1
);
9919 out2
= isl_basic_map_n_out(bmap2
);
9920 nparam
= isl_basic_map_n_param(bmap1
);
9922 total
= nparam
+ in
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
9923 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9924 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9925 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9926 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9927 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9928 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
9929 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in
);
9930 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
9931 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
9932 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9934 bmap
= isl_basic_map_alloc_space(dim_result
,
9935 bmap1
->n_div
+ bmap2
->n_div
,
9936 bmap1
->n_eq
+ bmap2
->n_eq
,
9937 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9938 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9939 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9941 bmap
= isl_basic_map_set_rational(bmap
);
9942 bmap
= isl_basic_map_simplify(bmap
);
9943 return isl_basic_map_finalize(bmap
);
9945 isl_basic_map_free(bmap1
);
9946 isl_basic_map_free(bmap2
);
9950 __isl_give isl_basic_map
*isl_basic_map_flat_range_product(
9951 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9953 isl_basic_map
*prod
;
9955 prod
= isl_basic_map_range_product(bmap1
, bmap2
);
9956 prod
= isl_basic_map_flatten_range(prod
);
9960 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
9961 * and collect the results.
9962 * The result live in the space obtained by calling "space_product"
9963 * on the spaces of "map1" and "map2".
9964 * If "remove_duplicates" is set then the result may contain duplicates
9965 * (even if the inputs do not) and so we try and remove the obvious
9968 static __isl_give isl_map
*map_product(__isl_take isl_map
*map1
,
9969 __isl_take isl_map
*map2
,
9970 __isl_give isl_space
*(*space_product
)(__isl_take isl_space
*left
,
9971 __isl_take isl_space
*right
),
9972 __isl_give isl_basic_map
*(*basic_map_product
)(
9973 __isl_take isl_basic_map
*left
,
9974 __isl_take isl_basic_map
*right
),
9975 int remove_duplicates
)
9978 struct isl_map
*result
;
9984 isl_assert(map1
->ctx
, isl_space_match(map1
->dim
, isl_dim_param
,
9985 map2
->dim
, isl_dim_param
), goto error
);
9987 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
9988 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
9989 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
9991 result
= isl_map_alloc_space(space_product(isl_space_copy(map1
->dim
),
9992 isl_space_copy(map2
->dim
)),
9993 map1
->n
* map2
->n
, flags
);
9996 for (i
= 0; i
< map1
->n
; ++i
)
9997 for (j
= 0; j
< map2
->n
; ++j
) {
9998 struct isl_basic_map
*part
;
9999 part
= basic_map_product(isl_basic_map_copy(map1
->p
[i
]),
10000 isl_basic_map_copy(map2
->p
[j
]));
10001 if (isl_basic_map_is_empty(part
))
10002 isl_basic_map_free(part
);
10004 result
= isl_map_add_basic_map(result
, part
);
10008 if (remove_duplicates
)
10009 result
= isl_map_remove_obvious_duplicates(result
);
10010 isl_map_free(map1
);
10011 isl_map_free(map2
);
10014 isl_map_free(map1
);
10015 isl_map_free(map2
);
10019 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
10021 static __isl_give isl_map
*map_product_aligned(__isl_take isl_map
*map1
,
10022 __isl_take isl_map
*map2
)
10024 return map_product(map1
, map2
, &isl_space_product
,
10025 &isl_basic_map_product
, 0);
10028 __isl_give isl_map
*isl_map_product(__isl_take isl_map
*map1
,
10029 __isl_take isl_map
*map2
)
10031 return isl_map_align_params_map_map_and(map1
, map2
, &map_product_aligned
);
10034 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
10036 __isl_give isl_map
*isl_map_flat_product(__isl_take isl_map
*map1
,
10037 __isl_take isl_map
*map2
)
10041 prod
= isl_map_product(map1
, map2
);
10042 prod
= isl_map_flatten(prod
);
10046 /* Given two set A and B, construct its Cartesian product A x B.
10048 struct isl_set
*isl_set_product(struct isl_set
*set1
, struct isl_set
*set2
)
10050 return isl_map_range_product(set1
, set2
);
10053 __isl_give isl_set
*isl_set_flat_product(__isl_take isl_set
*set1
,
10054 __isl_take isl_set
*set2
)
10056 return isl_map_flat_range_product(set1
, set2
);
10059 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
10061 static __isl_give isl_map
*map_domain_product_aligned(__isl_take isl_map
*map1
,
10062 __isl_take isl_map
*map2
)
10064 return map_product(map1
, map2
, &isl_space_domain_product
,
10065 &isl_basic_map_domain_product
, 1);
10068 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
10070 static __isl_give isl_map
*map_range_product_aligned(__isl_take isl_map
*map1
,
10071 __isl_take isl_map
*map2
)
10073 return map_product(map1
, map2
, &isl_space_range_product
,
10074 &isl_basic_map_range_product
, 1);
10077 __isl_give isl_map
*isl_map_domain_product(__isl_take isl_map
*map1
,
10078 __isl_take isl_map
*map2
)
10080 return isl_map_align_params_map_map_and(map1
, map2
,
10081 &map_domain_product_aligned
);
10084 __isl_give isl_map
*isl_map_range_product(__isl_take isl_map
*map1
,
10085 __isl_take isl_map
*map2
)
10087 return isl_map_align_params_map_map_and(map1
, map2
,
10088 &map_range_product_aligned
);
10091 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
10093 __isl_give isl_map
*isl_map_factor_domain(__isl_take isl_map
*map
)
10096 int total1
, keep1
, total2
, keep2
;
10100 if (!isl_space_domain_is_wrapping(map
->dim
) ||
10101 !isl_space_range_is_wrapping(map
->dim
))
10102 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10103 "not a product", return isl_map_free(map
));
10105 space
= isl_map_get_space(map
);
10106 total1
= isl_space_dim(space
, isl_dim_in
);
10107 total2
= isl_space_dim(space
, isl_dim_out
);
10108 space
= isl_space_factor_domain(space
);
10109 keep1
= isl_space_dim(space
, isl_dim_in
);
10110 keep2
= isl_space_dim(space
, isl_dim_out
);
10111 map
= isl_map_project_out(map
, isl_dim_in
, keep1
, total1
- keep1
);
10112 map
= isl_map_project_out(map
, isl_dim_out
, keep2
, total2
- keep2
);
10113 map
= isl_map_reset_space(map
, space
);
10118 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
10120 __isl_give isl_map
*isl_map_factor_range(__isl_take isl_map
*map
)
10123 int total1
, keep1
, total2
, keep2
;
10127 if (!isl_space_domain_is_wrapping(map
->dim
) ||
10128 !isl_space_range_is_wrapping(map
->dim
))
10129 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10130 "not a product", return isl_map_free(map
));
10132 space
= isl_map_get_space(map
);
10133 total1
= isl_space_dim(space
, isl_dim_in
);
10134 total2
= isl_space_dim(space
, isl_dim_out
);
10135 space
= isl_space_factor_range(space
);
10136 keep1
= isl_space_dim(space
, isl_dim_in
);
10137 keep2
= isl_space_dim(space
, isl_dim_out
);
10138 map
= isl_map_project_out(map
, isl_dim_in
, 0, total1
- keep1
);
10139 map
= isl_map_project_out(map
, isl_dim_out
, 0, total2
- keep2
);
10140 map
= isl_map_reset_space(map
, space
);
10145 /* Given a map of the form [A -> B] -> C, return the map A -> C.
10147 __isl_give isl_map
*isl_map_domain_factor_domain(__isl_take isl_map
*map
)
10154 if (!isl_space_domain_is_wrapping(map
->dim
))
10155 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10156 "domain is not a product", return isl_map_free(map
));
10158 space
= isl_map_get_space(map
);
10159 total
= isl_space_dim(space
, isl_dim_in
);
10160 space
= isl_space_domain_factor_domain(space
);
10161 keep
= isl_space_dim(space
, isl_dim_in
);
10162 map
= isl_map_project_out(map
, isl_dim_in
, keep
, total
- keep
);
10163 map
= isl_map_reset_space(map
, space
);
10168 /* Given a map of the form [A -> B] -> C, return the map B -> C.
10170 __isl_give isl_map
*isl_map_domain_factor_range(__isl_take isl_map
*map
)
10177 if (!isl_space_domain_is_wrapping(map
->dim
))
10178 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10179 "domain is not a product", return isl_map_free(map
));
10181 space
= isl_map_get_space(map
);
10182 total
= isl_space_dim(space
, isl_dim_in
);
10183 space
= isl_space_domain_factor_range(space
);
10184 keep
= isl_space_dim(space
, isl_dim_in
);
10185 map
= isl_map_project_out(map
, isl_dim_in
, 0, total
- keep
);
10186 map
= isl_map_reset_space(map
, space
);
10191 /* Given a map A -> [B -> C], extract the map A -> B.
10193 __isl_give isl_map
*isl_map_range_factor_domain(__isl_take isl_map
*map
)
10200 if (!isl_space_range_is_wrapping(map
->dim
))
10201 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10202 "range is not a product", return isl_map_free(map
));
10204 space
= isl_map_get_space(map
);
10205 total
= isl_space_dim(space
, isl_dim_out
);
10206 space
= isl_space_range_factor_domain(space
);
10207 keep
= isl_space_dim(space
, isl_dim_out
);
10208 map
= isl_map_project_out(map
, isl_dim_out
, keep
, total
- keep
);
10209 map
= isl_map_reset_space(map
, space
);
10214 /* Given a map A -> [B -> C], extract the map A -> C.
10216 __isl_give isl_map
*isl_map_range_factor_range(__isl_take isl_map
*map
)
10223 if (!isl_space_range_is_wrapping(map
->dim
))
10224 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10225 "range is not a product", return isl_map_free(map
));
10227 space
= isl_map_get_space(map
);
10228 total
= isl_space_dim(space
, isl_dim_out
);
10229 space
= isl_space_range_factor_range(space
);
10230 keep
= isl_space_dim(space
, isl_dim_out
);
10231 map
= isl_map_project_out(map
, isl_dim_out
, 0, total
- keep
);
10232 map
= isl_map_reset_space(map
, space
);
10237 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
10239 __isl_give isl_map
*isl_map_flat_domain_product(__isl_take isl_map
*map1
,
10240 __isl_take isl_map
*map2
)
10244 prod
= isl_map_domain_product(map1
, map2
);
10245 prod
= isl_map_flatten_domain(prod
);
10249 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
10251 __isl_give isl_map
*isl_map_flat_range_product(__isl_take isl_map
*map1
,
10252 __isl_take isl_map
*map2
)
10256 prod
= isl_map_range_product(map1
, map2
);
10257 prod
= isl_map_flatten_range(prod
);
10261 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map
*bmap
)
10264 uint32_t hash
= isl_hash_init();
10269 bmap
= isl_basic_map_copy(bmap
);
10270 bmap
= isl_basic_map_normalize(bmap
);
10273 total
= isl_basic_map_total_dim(bmap
);
10274 isl_hash_byte(hash
, bmap
->n_eq
& 0xFF);
10275 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
10277 c_hash
= isl_seq_get_hash(bmap
->eq
[i
], 1 + total
);
10278 isl_hash_hash(hash
, c_hash
);
10280 isl_hash_byte(hash
, bmap
->n_ineq
& 0xFF);
10281 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
10283 c_hash
= isl_seq_get_hash(bmap
->ineq
[i
], 1 + total
);
10284 isl_hash_hash(hash
, c_hash
);
10286 isl_hash_byte(hash
, bmap
->n_div
& 0xFF);
10287 for (i
= 0; i
< bmap
->n_div
; ++i
) {
10289 if (isl_int_is_zero(bmap
->div
[i
][0]))
10291 isl_hash_byte(hash
, i
& 0xFF);
10292 c_hash
= isl_seq_get_hash(bmap
->div
[i
], 1 + 1 + total
);
10293 isl_hash_hash(hash
, c_hash
);
10295 isl_basic_map_free(bmap
);
10299 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set
*bset
)
10301 return isl_basic_map_get_hash(bset_to_bmap(bset
));
10304 uint32_t isl_map_get_hash(__isl_keep isl_map
*map
)
10311 map
= isl_map_copy(map
);
10312 map
= isl_map_normalize(map
);
10316 hash
= isl_hash_init();
10317 for (i
= 0; i
< map
->n
; ++i
) {
10318 uint32_t bmap_hash
;
10319 bmap_hash
= isl_basic_map_get_hash(map
->p
[i
]);
10320 isl_hash_hash(hash
, bmap_hash
);
10328 uint32_t isl_set_get_hash(__isl_keep isl_set
*set
)
10330 return isl_map_get_hash(set_to_map(set
));
10333 /* Check if the value for dimension dim is completely determined
10334 * by the values of the other parameters and variables.
10335 * That is, check if dimension dim is involved in an equality.
10337 int isl_basic_set_dim_is_unique(struct isl_basic_set
*bset
, unsigned dim
)
10344 nparam
= isl_basic_set_n_param(bset
);
10345 for (i
= 0; i
< bset
->n_eq
; ++i
)
10346 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ dim
]))
10351 /* Check if the value for dimension dim is completely determined
10352 * by the values of the other parameters and variables.
10353 * That is, check if dimension dim is involved in an equality
10354 * for each of the subsets.
10356 int isl_set_dim_is_unique(struct isl_set
*set
, unsigned dim
)
10362 for (i
= 0; i
< set
->n
; ++i
) {
10364 unique
= isl_basic_set_dim_is_unique(set
->p
[i
], dim
);
10371 /* Return the number of basic maps in the (current) representation of "map".
10373 int isl_map_n_basic_map(__isl_keep isl_map
*map
)
10375 return map
? map
->n
: 0;
10378 int isl_set_n_basic_set(__isl_keep isl_set
*set
)
10380 return set
? set
->n
: 0;
10383 isl_stat
isl_map_foreach_basic_map(__isl_keep isl_map
*map
,
10384 isl_stat (*fn
)(__isl_take isl_basic_map
*bmap
, void *user
), void *user
)
10389 return isl_stat_error
;
10391 for (i
= 0; i
< map
->n
; ++i
)
10392 if (fn(isl_basic_map_copy(map
->p
[i
]), user
) < 0)
10393 return isl_stat_error
;
10395 return isl_stat_ok
;
10398 isl_stat
isl_set_foreach_basic_set(__isl_keep isl_set
*set
,
10399 isl_stat (*fn
)(__isl_take isl_basic_set
*bset
, void *user
), void *user
)
10404 return isl_stat_error
;
10406 for (i
= 0; i
< set
->n
; ++i
)
10407 if (fn(isl_basic_set_copy(set
->p
[i
]), user
) < 0)
10408 return isl_stat_error
;
10410 return isl_stat_ok
;
10413 /* Return a list of basic sets, the union of which is equal to "set".
10415 __isl_give isl_basic_set_list
*isl_set_get_basic_set_list(
10416 __isl_keep isl_set
*set
)
10419 isl_basic_set_list
*list
;
10424 list
= isl_basic_set_list_alloc(isl_set_get_ctx(set
), set
->n
);
10425 for (i
= 0; i
< set
->n
; ++i
) {
10426 isl_basic_set
*bset
;
10428 bset
= isl_basic_set_copy(set
->p
[i
]);
10429 list
= isl_basic_set_list_add(list
, bset
);
10435 __isl_give isl_basic_set
*isl_basic_set_lift(__isl_take isl_basic_set
*bset
)
10442 bset
= isl_basic_set_cow(bset
);
10446 dim
= isl_basic_set_get_space(bset
);
10447 dim
= isl_space_lift(dim
, bset
->n_div
);
10450 isl_space_free(bset
->dim
);
10452 bset
->extra
-= bset
->n_div
;
10455 bset
= isl_basic_set_finalize(bset
);
10459 isl_basic_set_free(bset
);
10463 __isl_give isl_set
*isl_set_lift(__isl_take isl_set
*set
)
10469 set
= isl_set_align_divs(set
);
10474 set
= isl_set_cow(set
);
10478 n_div
= set
->p
[0]->n_div
;
10479 dim
= isl_set_get_space(set
);
10480 dim
= isl_space_lift(dim
, n_div
);
10483 isl_space_free(set
->dim
);
10486 for (i
= 0; i
< set
->n
; ++i
) {
10487 set
->p
[i
] = isl_basic_set_lift(set
->p
[i
]);
10498 __isl_give isl_map
*isl_set_lifting(__isl_take isl_set
*set
)
10501 struct isl_basic_map
*bmap
;
10508 set
= isl_set_align_divs(set
);
10513 dim
= isl_set_get_space(set
);
10514 if (set
->n
== 0 || set
->p
[0]->n_div
== 0) {
10516 return isl_map_identity(isl_space_map_from_set(dim
));
10519 n_div
= set
->p
[0]->n_div
;
10520 dim
= isl_space_map_from_set(dim
);
10521 n_param
= isl_space_dim(dim
, isl_dim_param
);
10522 n_set
= isl_space_dim(dim
, isl_dim_in
);
10523 dim
= isl_space_extend(dim
, n_param
, n_set
, n_set
+ n_div
);
10524 bmap
= isl_basic_map_alloc_space(dim
, 0, n_set
, 2 * n_div
);
10525 for (i
= 0; i
< n_set
; ++i
)
10526 bmap
= var_equal(bmap
, i
);
10528 total
= n_param
+ n_set
+ n_set
+ n_div
;
10529 for (i
= 0; i
< n_div
; ++i
) {
10530 k
= isl_basic_map_alloc_inequality(bmap
);
10533 isl_seq_cpy(bmap
->ineq
[k
], set
->p
[0]->div
[i
]+1, 1+n_param
);
10534 isl_seq_clr(bmap
->ineq
[k
]+1+n_param
, n_set
);
10535 isl_seq_cpy(bmap
->ineq
[k
]+1+n_param
+n_set
,
10536 set
->p
[0]->div
[i
]+1+1+n_param
, n_set
+ n_div
);
10537 isl_int_neg(bmap
->ineq
[k
][1+n_param
+n_set
+n_set
+i
],
10538 set
->p
[0]->div
[i
][0]);
10540 l
= isl_basic_map_alloc_inequality(bmap
);
10543 isl_seq_neg(bmap
->ineq
[l
], bmap
->ineq
[k
], 1 + total
);
10544 isl_int_add(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0],
10545 set
->p
[0]->div
[i
][0]);
10546 isl_int_sub_ui(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], 1);
10550 bmap
= isl_basic_map_simplify(bmap
);
10551 bmap
= isl_basic_map_finalize(bmap
);
10552 return isl_map_from_basic_map(bmap
);
10555 isl_basic_map_free(bmap
);
10559 int isl_basic_set_size(__isl_keep isl_basic_set
*bset
)
10567 dim
= isl_basic_set_total_dim(bset
);
10568 size
+= bset
->n_eq
* (1 + dim
);
10569 size
+= bset
->n_ineq
* (1 + dim
);
10570 size
+= bset
->n_div
* (2 + dim
);
10575 int isl_set_size(__isl_keep isl_set
*set
)
10583 for (i
= 0; i
< set
->n
; ++i
)
10584 size
+= isl_basic_set_size(set
->p
[i
]);
10589 /* Check if there is any lower bound (if lower == 0) and/or upper
10590 * bound (if upper == 0) on the specified dim.
10592 static isl_bool
basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
10593 enum isl_dim_type type
, unsigned pos
, int lower
, int upper
)
10597 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
10598 return isl_bool_error
;
10600 pos
+= isl_basic_map_offset(bmap
, type
);
10602 for (i
= 0; i
< bmap
->n_div
; ++i
) {
10603 if (isl_int_is_zero(bmap
->div
[i
][0]))
10605 if (!isl_int_is_zero(bmap
->div
[i
][1 + pos
]))
10606 return isl_bool_true
;
10609 for (i
= 0; i
< bmap
->n_eq
; ++i
)
10610 if (!isl_int_is_zero(bmap
->eq
[i
][pos
]))
10611 return isl_bool_true
;
10613 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
10614 int sgn
= isl_int_sgn(bmap
->ineq
[i
][pos
]);
10621 return lower
&& upper
;
10624 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
10625 enum isl_dim_type type
, unsigned pos
)
10627 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 0);
10630 isl_bool
isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map
*bmap
,
10631 enum isl_dim_type type
, unsigned pos
)
10633 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 1);
10636 isl_bool
isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map
*bmap
,
10637 enum isl_dim_type type
, unsigned pos
)
10639 return basic_map_dim_is_bounded(bmap
, type
, pos
, 1, 0);
10642 int isl_map_dim_is_bounded(__isl_keep isl_map
*map
,
10643 enum isl_dim_type type
, unsigned pos
)
10650 for (i
= 0; i
< map
->n
; ++i
) {
10652 bounded
= isl_basic_map_dim_is_bounded(map
->p
[i
], type
, pos
);
10653 if (bounded
< 0 || !bounded
)
10660 /* Return 1 if the specified dim is involved in both an upper bound
10661 * and a lower bound.
10663 int isl_set_dim_is_bounded(__isl_keep isl_set
*set
,
10664 enum isl_dim_type type
, unsigned pos
)
10666 return isl_map_dim_is_bounded(set_to_map(set
), type
, pos
);
10669 /* Does "map" have a bound (according to "fn") for any of its basic maps?
10671 static isl_bool
has_any_bound(__isl_keep isl_map
*map
,
10672 enum isl_dim_type type
, unsigned pos
,
10673 isl_bool (*fn
)(__isl_keep isl_basic_map
*bmap
,
10674 enum isl_dim_type type
, unsigned pos
))
10679 return isl_bool_error
;
10681 for (i
= 0; i
< map
->n
; ++i
) {
10683 bounded
= fn(map
->p
[i
], type
, pos
);
10684 if (bounded
< 0 || bounded
)
10688 return isl_bool_false
;
10691 /* Return 1 if the specified dim is involved in any lower bound.
10693 isl_bool
isl_set_dim_has_any_lower_bound(__isl_keep isl_set
*set
,
10694 enum isl_dim_type type
, unsigned pos
)
10696 return has_any_bound(set
, type
, pos
,
10697 &isl_basic_map_dim_has_lower_bound
);
10700 /* Return 1 if the specified dim is involved in any upper bound.
10702 isl_bool
isl_set_dim_has_any_upper_bound(__isl_keep isl_set
*set
,
10703 enum isl_dim_type type
, unsigned pos
)
10705 return has_any_bound(set
, type
, pos
,
10706 &isl_basic_map_dim_has_upper_bound
);
10709 /* Does "map" have a bound (according to "fn") for all of its basic maps?
10711 static isl_bool
has_bound(__isl_keep isl_map
*map
,
10712 enum isl_dim_type type
, unsigned pos
,
10713 isl_bool (*fn
)(__isl_keep isl_basic_map
*bmap
,
10714 enum isl_dim_type type
, unsigned pos
))
10719 return isl_bool_error
;
10721 for (i
= 0; i
< map
->n
; ++i
) {
10723 bounded
= fn(map
->p
[i
], type
, pos
);
10724 if (bounded
< 0 || !bounded
)
10728 return isl_bool_true
;
10731 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10733 isl_bool
isl_set_dim_has_lower_bound(__isl_keep isl_set
*set
,
10734 enum isl_dim_type type
, unsigned pos
)
10736 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_lower_bound
);
10739 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10741 isl_bool
isl_set_dim_has_upper_bound(__isl_keep isl_set
*set
,
10742 enum isl_dim_type type
, unsigned pos
)
10744 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_upper_bound
);
10747 /* For each of the "n" variables starting at "first", determine
10748 * the sign of the variable and put the results in the first "n"
10749 * elements of the array "signs".
10751 * 1 means that the variable is non-negative
10752 * -1 means that the variable is non-positive
10753 * 0 means the variable attains both positive and negative values.
10755 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set
*bset
,
10756 unsigned first
, unsigned n
, int *signs
)
10758 isl_vec
*bound
= NULL
;
10759 struct isl_tab
*tab
= NULL
;
10760 struct isl_tab_undo
*snap
;
10763 if (!bset
|| !signs
)
10766 bound
= isl_vec_alloc(bset
->ctx
, 1 + isl_basic_set_total_dim(bset
));
10767 tab
= isl_tab_from_basic_set(bset
, 0);
10768 if (!bound
|| !tab
)
10771 isl_seq_clr(bound
->el
, bound
->size
);
10772 isl_int_set_si(bound
->el
[0], -1);
10774 snap
= isl_tab_snap(tab
);
10775 for (i
= 0; i
< n
; ++i
) {
10778 isl_int_set_si(bound
->el
[1 + first
+ i
], -1);
10779 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
10781 empty
= tab
->empty
;
10782 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
10783 if (isl_tab_rollback(tab
, snap
) < 0)
10791 isl_int_set_si(bound
->el
[1 + first
+ i
], 1);
10792 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
10794 empty
= tab
->empty
;
10795 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
10796 if (isl_tab_rollback(tab
, snap
) < 0)
10799 signs
[i
] = empty
? -1 : 0;
10803 isl_vec_free(bound
);
10807 isl_vec_free(bound
);
10811 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set
*bset
,
10812 enum isl_dim_type type
, unsigned first
, unsigned n
, int *signs
)
10814 if (!bset
|| !signs
)
10816 isl_assert(bset
->ctx
, first
+ n
<= isl_basic_set_dim(bset
, type
),
10819 first
+= pos(bset
->dim
, type
) - 1;
10820 return isl_basic_set_vars_get_sign(bset
, first
, n
, signs
);
10823 /* Is it possible for the integer division "div" to depend (possibly
10824 * indirectly) on any output dimensions?
10826 * If the div is undefined, then we conservatively assume that it
10827 * may depend on them.
10828 * Otherwise, we check if it actually depends on them or on any integer
10829 * divisions that may depend on them.
10831 static isl_bool
div_may_involve_output(__isl_keep isl_basic_map
*bmap
, int div
)
10834 unsigned n_out
, o_out
;
10835 unsigned n_div
, o_div
;
10837 if (isl_int_is_zero(bmap
->div
[div
][0]))
10838 return isl_bool_true
;
10840 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10841 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10843 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + o_out
, n_out
) != -1)
10844 return isl_bool_true
;
10846 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10847 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10849 for (i
= 0; i
< n_div
; ++i
) {
10850 isl_bool may_involve
;
10852 if (isl_int_is_zero(bmap
->div
[div
][1 + o_div
+ i
]))
10854 may_involve
= div_may_involve_output(bmap
, i
);
10855 if (may_involve
< 0 || may_involve
)
10856 return may_involve
;
10859 return isl_bool_false
;
10862 /* Return the first integer division of "bmap" in the range
10863 * [first, first + n[ that may depend on any output dimensions and
10864 * that has a non-zero coefficient in "c" (where the first coefficient
10865 * in "c" corresponds to integer division "first").
10867 static int first_div_may_involve_output(__isl_keep isl_basic_map
*bmap
,
10868 isl_int
*c
, int first
, int n
)
10875 for (k
= first
; k
< first
+ n
; ++k
) {
10876 isl_bool may_involve
;
10878 if (isl_int_is_zero(c
[k
]))
10880 may_involve
= div_may_involve_output(bmap
, k
);
10881 if (may_involve
< 0)
10890 /* Look for a pair of inequality constraints in "bmap" of the form
10892 * -l + i >= 0 or i >= l
10894 * n + l - i >= 0 or i <= l + n
10896 * with n < "m" and i the output dimension at position "pos".
10897 * (Note that n >= 0 as otherwise the two constraints would conflict.)
10898 * Furthermore, "l" is only allowed to involve parameters, input dimensions
10899 * and earlier output dimensions, as well as integer divisions that do
10900 * not involve any of the output dimensions.
10902 * Return the index of the first inequality constraint or bmap->n_ineq
10903 * if no such pair can be found.
10905 static int find_modulo_constraint_pair(__isl_keep isl_basic_map
*bmap
,
10906 int pos
, isl_int m
)
10911 unsigned n_div
, o_div
;
10912 unsigned n_out
, o_out
;
10918 ctx
= isl_basic_map_get_ctx(bmap
);
10919 total
= isl_basic_map_total_dim(bmap
);
10920 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10921 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10922 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10923 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10924 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
10925 if (!isl_int_abs_eq(bmap
->ineq
[i
][o_out
+ pos
], ctx
->one
))
10927 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + o_out
+ pos
+ 1,
10928 n_out
- (pos
+ 1)) != -1)
10930 if (first_div_may_involve_output(bmap
, bmap
->ineq
[i
] + o_div
,
10933 for (j
= i
+ 1; j
< bmap
->n_ineq
; ++j
) {
10934 if (!isl_int_abs_eq(bmap
->ineq
[j
][o_out
+ pos
],
10937 if (!isl_seq_is_neg(bmap
->ineq
[i
] + 1,
10938 bmap
->ineq
[j
] + 1, total
))
10942 if (j
>= bmap
->n_ineq
)
10944 isl_int_add(bmap
->ineq
[i
][0],
10945 bmap
->ineq
[i
][0], bmap
->ineq
[j
][0]);
10946 less
= isl_int_abs_lt(bmap
->ineq
[i
][0], m
);
10947 isl_int_sub(bmap
->ineq
[i
][0],
10948 bmap
->ineq
[i
][0], bmap
->ineq
[j
][0]);
10951 if (isl_int_is_one(bmap
->ineq
[i
][o_out
+ pos
]))
10957 return bmap
->n_ineq
;
10960 /* Return the index of the equality of "bmap" that defines
10961 * the output dimension "pos" in terms of earlier dimensions.
10962 * The equality may also involve integer divisions, as long
10963 * as those integer divisions are defined in terms of
10964 * parameters or input dimensions.
10965 * In this case, *div is set to the number of integer divisions and
10966 * *ineq is set to the number of inequality constraints (provided
10967 * div and ineq are not NULL).
10969 * The equality may also involve a single integer division involving
10970 * the output dimensions (typically only output dimension "pos") as
10971 * long as the coefficient of output dimension "pos" is 1 or -1 and
10972 * there is a pair of constraints i >= l and i <= l + n, with i referring
10973 * to output dimension "pos", l an expression involving only earlier
10974 * dimensions and n smaller than the coefficient of the integer division
10975 * in the equality. In this case, the output dimension can be defined
10976 * in terms of a modulo expression that does not involve the integer division.
10977 * *div is then set to this single integer division and
10978 * *ineq is set to the index of constraint i >= l.
10980 * Return bmap->n_eq if there is no such equality.
10981 * Return -1 on error.
10983 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map
*bmap
,
10984 int pos
, int *div
, int *ineq
)
10987 unsigned n_out
, o_out
;
10988 unsigned n_div
, o_div
;
10993 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10994 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10995 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10996 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10999 *ineq
= bmap
->n_ineq
;
11002 for (j
= 0; j
< bmap
->n_eq
; ++j
) {
11003 if (isl_int_is_zero(bmap
->eq
[j
][o_out
+ pos
]))
11005 if (isl_seq_first_non_zero(bmap
->eq
[j
] + o_out
+ pos
+ 1,
11006 n_out
- (pos
+ 1)) != -1)
11008 k
= first_div_may_involve_output(bmap
, bmap
->eq
[j
] + o_div
,
11012 if (!isl_int_is_one(bmap
->eq
[j
][o_out
+ pos
]) &&
11013 !isl_int_is_negone(bmap
->eq
[j
][o_out
+ pos
]))
11015 if (first_div_may_involve_output(bmap
, bmap
->eq
[j
] + o_div
,
11016 k
+ 1, n_div
- (k
+1)) < n_div
)
11018 l
= find_modulo_constraint_pair(bmap
, pos
,
11019 bmap
->eq
[j
][o_div
+ k
]);
11022 if (l
>= bmap
->n_ineq
)
11034 /* Check if the given basic map is obviously single-valued.
11035 * In particular, for each output dimension, check that there is
11036 * an equality that defines the output dimension in terms of
11037 * earlier dimensions.
11039 isl_bool
isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map
*bmap
)
11045 return isl_bool_error
;
11047 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
11049 for (i
= 0; i
< n_out
; ++i
) {
11052 eq
= isl_basic_map_output_defining_equality(bmap
, i
,
11055 return isl_bool_error
;
11056 if (eq
>= bmap
->n_eq
)
11057 return isl_bool_false
;
11060 return isl_bool_true
;
11063 /* Check if the given basic map is single-valued.
11064 * We simply compute
11068 * and check if the result is a subset of the identity mapping.
11070 isl_bool
isl_basic_map_is_single_valued(__isl_keep isl_basic_map
*bmap
)
11073 isl_basic_map
*test
;
11077 sv
= isl_basic_map_plain_is_single_valued(bmap
);
11081 test
= isl_basic_map_reverse(isl_basic_map_copy(bmap
));
11082 test
= isl_basic_map_apply_range(test
, isl_basic_map_copy(bmap
));
11084 space
= isl_basic_map_get_space(bmap
);
11085 space
= isl_space_map_from_set(isl_space_range(space
));
11086 id
= isl_basic_map_identity(space
);
11088 sv
= isl_basic_map_is_subset(test
, id
);
11090 isl_basic_map_free(test
);
11091 isl_basic_map_free(id
);
11096 /* Check if the given map is obviously single-valued.
11098 isl_bool
isl_map_plain_is_single_valued(__isl_keep isl_map
*map
)
11101 return isl_bool_error
;
11103 return isl_bool_true
;
11105 return isl_bool_false
;
11107 return isl_basic_map_plain_is_single_valued(map
->p
[0]);
11110 /* Check if the given map is single-valued.
11111 * We simply compute
11115 * and check if the result is a subset of the identity mapping.
11117 isl_bool
isl_map_is_single_valued(__isl_keep isl_map
*map
)
11124 sv
= isl_map_plain_is_single_valued(map
);
11128 test
= isl_map_reverse(isl_map_copy(map
));
11129 test
= isl_map_apply_range(test
, isl_map_copy(map
));
11131 dim
= isl_space_map_from_set(isl_space_range(isl_map_get_space(map
)));
11132 id
= isl_map_identity(dim
);
11134 sv
= isl_map_is_subset(test
, id
);
11136 isl_map_free(test
);
11142 isl_bool
isl_map_is_injective(__isl_keep isl_map
*map
)
11146 map
= isl_map_copy(map
);
11147 map
= isl_map_reverse(map
);
11148 in
= isl_map_is_single_valued(map
);
11154 /* Check if the given map is obviously injective.
11156 isl_bool
isl_map_plain_is_injective(__isl_keep isl_map
*map
)
11160 map
= isl_map_copy(map
);
11161 map
= isl_map_reverse(map
);
11162 in
= isl_map_plain_is_single_valued(map
);
11168 isl_bool
isl_map_is_bijective(__isl_keep isl_map
*map
)
11172 sv
= isl_map_is_single_valued(map
);
11176 return isl_map_is_injective(map
);
11179 isl_bool
isl_set_is_singleton(__isl_keep isl_set
*set
)
11181 return isl_map_is_single_valued(set_to_map(set
));
11184 /* Does "map" only map elements to themselves?
11186 * If the domain and range spaces are different, then "map"
11187 * is considered not to be an identity relation, even if it is empty.
11188 * Otherwise, construct the maximal identity relation and
11189 * check whether "map" is a subset of this relation.
11191 isl_bool
isl_map_is_identity(__isl_keep isl_map
*map
)
11195 isl_bool equal
, is_identity
;
11197 space
= isl_map_get_space(map
);
11198 equal
= isl_space_tuple_is_equal(space
, isl_dim_in
, space
, isl_dim_out
);
11199 isl_space_free(space
);
11200 if (equal
< 0 || !equal
)
11203 id
= isl_map_identity(isl_map_get_space(map
));
11204 is_identity
= isl_map_is_subset(map
, id
);
11207 return is_identity
;
11210 int isl_map_is_translation(__isl_keep isl_map
*map
)
11215 delta
= isl_map_deltas(isl_map_copy(map
));
11216 ok
= isl_set_is_singleton(delta
);
11217 isl_set_free(delta
);
11222 static int unique(isl_int
*p
, unsigned pos
, unsigned len
)
11224 if (isl_seq_first_non_zero(p
, pos
) != -1)
11226 if (isl_seq_first_non_zero(p
+ pos
+ 1, len
- pos
- 1) != -1)
11231 int isl_basic_set_is_box(__isl_keep isl_basic_set
*bset
)
11240 if (isl_basic_set_dim(bset
, isl_dim_div
) != 0)
11243 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
11244 ovar
= isl_space_offset(bset
->dim
, isl_dim_set
);
11245 for (j
= 0; j
< nvar
; ++j
) {
11246 int lower
= 0, upper
= 0;
11247 for (i
= 0; i
< bset
->n_eq
; ++i
) {
11248 if (isl_int_is_zero(bset
->eq
[i
][1 + ovar
+ j
]))
11250 if (!unique(bset
->eq
[i
] + 1 + ovar
, j
, nvar
))
11254 if (i
< bset
->n_eq
)
11256 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
11257 if (isl_int_is_zero(bset
->ineq
[i
][1 + ovar
+ j
]))
11259 if (!unique(bset
->ineq
[i
] + 1 + ovar
, j
, nvar
))
11261 if (isl_int_is_pos(bset
->ineq
[i
][1 + ovar
+ j
]))
11266 if (!lower
|| !upper
)
11273 int isl_set_is_box(__isl_keep isl_set
*set
)
11280 return isl_basic_set_is_box(set
->p
[0]);
11283 isl_bool
isl_basic_set_is_wrapping(__isl_keep isl_basic_set
*bset
)
11286 return isl_bool_error
;
11288 return isl_space_is_wrapping(bset
->dim
);
11291 isl_bool
isl_set_is_wrapping(__isl_keep isl_set
*set
)
11294 return isl_bool_error
;
11296 return isl_space_is_wrapping(set
->dim
);
11299 /* Modify the space of "map" through a call to "change".
11300 * If "can_change" is set (not NULL), then first call it to check
11301 * if the modification is allowed, printing the error message "cannot_change"
11304 static __isl_give isl_map
*isl_map_change_space(__isl_take isl_map
*map
,
11305 isl_bool (*can_change
)(__isl_keep isl_map
*map
),
11306 const char *cannot_change
,
11307 __isl_give isl_space
*(*change
)(__isl_take isl_space
*space
))
11315 ok
= can_change
? can_change(map
) : isl_bool_true
;
11317 return isl_map_free(map
);
11319 isl_die(isl_map_get_ctx(map
), isl_error_invalid
, cannot_change
,
11320 return isl_map_free(map
));
11322 space
= change(isl_map_get_space(map
));
11323 map
= isl_map_reset_space(map
, space
);
11328 /* Is the domain of "map" a wrapped relation?
11330 isl_bool
isl_map_domain_is_wrapping(__isl_keep isl_map
*map
)
11333 return isl_bool_error
;
11335 return isl_space_domain_is_wrapping(map
->dim
);
11338 /* Is the range of "map" a wrapped relation?
11340 isl_bool
isl_map_range_is_wrapping(__isl_keep isl_map
*map
)
11343 return isl_bool_error
;
11345 return isl_space_range_is_wrapping(map
->dim
);
11348 __isl_give isl_basic_set
*isl_basic_map_wrap(__isl_take isl_basic_map
*bmap
)
11350 bmap
= isl_basic_map_cow(bmap
);
11354 bmap
->dim
= isl_space_wrap(bmap
->dim
);
11358 bmap
= isl_basic_map_finalize(bmap
);
11360 return bset_from_bmap(bmap
);
11362 isl_basic_map_free(bmap
);
11366 /* Given a map A -> B, return the set (A -> B).
11368 __isl_give isl_set
*isl_map_wrap(__isl_take isl_map
*map
)
11370 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_wrap
);
11373 __isl_give isl_basic_map
*isl_basic_set_unwrap(__isl_take isl_basic_set
*bset
)
11375 bset
= isl_basic_set_cow(bset
);
11379 bset
->dim
= isl_space_unwrap(bset
->dim
);
11383 bset
= isl_basic_set_finalize(bset
);
11385 return bset_to_bmap(bset
);
11387 isl_basic_set_free(bset
);
11391 /* Given a set (A -> B), return the map A -> B.
11392 * Error out if "set" is not of the form (A -> B).
11394 __isl_give isl_map
*isl_set_unwrap(__isl_take isl_set
*set
)
11396 return isl_map_change_space(set
, &isl_set_is_wrapping
,
11397 "not a wrapping set", &isl_space_unwrap
);
11400 __isl_give isl_basic_map
*isl_basic_map_reset(__isl_take isl_basic_map
*bmap
,
11401 enum isl_dim_type type
)
11406 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
11409 bmap
= isl_basic_map_cow(bmap
);
11413 bmap
->dim
= isl_space_reset(bmap
->dim
, type
);
11417 bmap
= isl_basic_map_finalize(bmap
);
11421 isl_basic_map_free(bmap
);
11425 __isl_give isl_map
*isl_map_reset(__isl_take isl_map
*map
,
11426 enum isl_dim_type type
)
11433 if (!isl_space_is_named_or_nested(map
->dim
, type
))
11436 map
= isl_map_cow(map
);
11440 for (i
= 0; i
< map
->n
; ++i
) {
11441 map
->p
[i
] = isl_basic_map_reset(map
->p
[i
], type
);
11445 map
->dim
= isl_space_reset(map
->dim
, type
);
11455 __isl_give isl_basic_map
*isl_basic_map_flatten(__isl_take isl_basic_map
*bmap
)
11460 if (!bmap
->dim
->nested
[0] && !bmap
->dim
->nested
[1])
11463 bmap
= isl_basic_map_cow(bmap
);
11467 bmap
->dim
= isl_space_flatten(bmap
->dim
);
11471 bmap
= isl_basic_map_finalize(bmap
);
11475 isl_basic_map_free(bmap
);
11479 __isl_give isl_basic_set
*isl_basic_set_flatten(__isl_take isl_basic_set
*bset
)
11481 return bset_from_bmap(isl_basic_map_flatten(bset_to_bmap(bset
)));
11484 __isl_give isl_basic_map
*isl_basic_map_flatten_domain(
11485 __isl_take isl_basic_map
*bmap
)
11490 if (!bmap
->dim
->nested
[0])
11493 bmap
= isl_basic_map_cow(bmap
);
11497 bmap
->dim
= isl_space_flatten_domain(bmap
->dim
);
11501 bmap
= isl_basic_map_finalize(bmap
);
11505 isl_basic_map_free(bmap
);
11509 __isl_give isl_basic_map
*isl_basic_map_flatten_range(
11510 __isl_take isl_basic_map
*bmap
)
11515 if (!bmap
->dim
->nested
[1])
11518 bmap
= isl_basic_map_cow(bmap
);
11522 bmap
->dim
= isl_space_flatten_range(bmap
->dim
);
11526 bmap
= isl_basic_map_finalize(bmap
);
11530 isl_basic_map_free(bmap
);
11534 /* Remove any internal structure from the spaces of domain and range of "map".
11536 __isl_give isl_map
*isl_map_flatten(__isl_take isl_map
*map
)
11541 if (!map
->dim
->nested
[0] && !map
->dim
->nested
[1])
11544 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten
);
11547 __isl_give isl_set
*isl_set_flatten(__isl_take isl_set
*set
)
11549 return set_from_map(isl_map_flatten(set_to_map(set
)));
11552 __isl_give isl_map
*isl_set_flatten_map(__isl_take isl_set
*set
)
11554 isl_space
*dim
, *flat_dim
;
11557 dim
= isl_set_get_space(set
);
11558 flat_dim
= isl_space_flatten(isl_space_copy(dim
));
11559 map
= isl_map_identity(isl_space_join(isl_space_reverse(dim
), flat_dim
));
11560 map
= isl_map_intersect_domain(map
, set
);
11565 /* Remove any internal structure from the space of the domain of "map".
11567 __isl_give isl_map
*isl_map_flatten_domain(__isl_take isl_map
*map
)
11572 if (!map
->dim
->nested
[0])
11575 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten_domain
);
11578 /* Remove any internal structure from the space of the range of "map".
11580 __isl_give isl_map
*isl_map_flatten_range(__isl_take isl_map
*map
)
11585 if (!map
->dim
->nested
[1])
11588 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten_range
);
11591 /* Reorder the dimensions of "bmap" according to the given dim_map
11592 * and set the dimension specification to "dim" and
11593 * perform Gaussian elimination on the result.
11595 __isl_give isl_basic_map
*isl_basic_map_realign(__isl_take isl_basic_map
*bmap
,
11596 __isl_take isl_space
*dim
, __isl_take
struct isl_dim_map
*dim_map
)
11598 isl_basic_map
*res
;
11601 bmap
= isl_basic_map_cow(bmap
);
11602 if (!bmap
|| !dim
|| !dim_map
)
11605 flags
= bmap
->flags
;
11606 ISL_FL_CLR(flags
, ISL_BASIC_MAP_FINAL
);
11607 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED
);
11608 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
11609 res
= isl_basic_map_alloc_space(dim
,
11610 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
11611 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
11613 res
->flags
= flags
;
11614 res
= isl_basic_map_gauss(res
, NULL
);
11615 res
= isl_basic_map_finalize(res
);
11619 isl_basic_map_free(bmap
);
11620 isl_space_free(dim
);
11624 /* Reorder the dimensions of "map" according to given reordering.
11626 __isl_give isl_map
*isl_map_realign(__isl_take isl_map
*map
,
11627 __isl_take isl_reordering
*r
)
11630 struct isl_dim_map
*dim_map
;
11632 map
= isl_map_cow(map
);
11633 dim_map
= isl_dim_map_from_reordering(r
);
11634 if (!map
|| !r
|| !dim_map
)
11637 for (i
= 0; i
< map
->n
; ++i
) {
11638 struct isl_dim_map
*dim_map_i
;
11640 dim_map_i
= isl_dim_map_extend(dim_map
, map
->p
[i
]);
11642 map
->p
[i
] = isl_basic_map_realign(map
->p
[i
],
11643 isl_space_copy(r
->dim
), dim_map_i
);
11649 map
= isl_map_reset_space(map
, isl_space_copy(r
->dim
));
11651 isl_reordering_free(r
);
11657 isl_reordering_free(r
);
11661 __isl_give isl_set
*isl_set_realign(__isl_take isl_set
*set
,
11662 __isl_take isl_reordering
*r
)
11664 return set_from_map(isl_map_realign(set_to_map(set
), r
));
11667 __isl_give isl_map
*isl_map_align_params(__isl_take isl_map
*map
,
11668 __isl_take isl_space
*model
)
11672 if (!map
|| !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(map
->dim
))
11680 isl_die(ctx
, isl_error_invalid
,
11681 "relation has unnamed parameters", goto error
);
11682 if (!isl_space_match(map
->dim
, isl_dim_param
, model
, isl_dim_param
)) {
11683 isl_reordering
*exp
;
11685 model
= isl_space_drop_dims(model
, isl_dim_in
,
11686 0, isl_space_dim(model
, isl_dim_in
));
11687 model
= isl_space_drop_dims(model
, isl_dim_out
,
11688 0, isl_space_dim(model
, isl_dim_out
));
11689 exp
= isl_parameter_alignment_reordering(map
->dim
, model
);
11690 exp
= isl_reordering_extend_space(exp
, isl_map_get_space(map
));
11691 map
= isl_map_realign(map
, exp
);
11694 isl_space_free(model
);
11697 isl_space_free(model
);
11702 __isl_give isl_set
*isl_set_align_params(__isl_take isl_set
*set
,
11703 __isl_take isl_space
*model
)
11705 return isl_map_align_params(set
, model
);
11708 /* Align the parameters of "bmap" to those of "model", introducing
11709 * additional parameters if needed.
11711 __isl_give isl_basic_map
*isl_basic_map_align_params(
11712 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*model
)
11716 if (!bmap
|| !model
)
11719 ctx
= isl_space_get_ctx(model
);
11720 if (!isl_space_has_named_params(model
))
11721 isl_die(ctx
, isl_error_invalid
,
11722 "model has unnamed parameters", goto error
);
11723 if (!isl_space_has_named_params(bmap
->dim
))
11724 isl_die(ctx
, isl_error_invalid
,
11725 "relation has unnamed parameters", goto error
);
11726 if (!isl_space_match(bmap
->dim
, isl_dim_param
, model
, isl_dim_param
)) {
11727 isl_reordering
*exp
;
11728 struct isl_dim_map
*dim_map
;
11730 model
= isl_space_drop_dims(model
, isl_dim_in
,
11731 0, isl_space_dim(model
, isl_dim_in
));
11732 model
= isl_space_drop_dims(model
, isl_dim_out
,
11733 0, isl_space_dim(model
, isl_dim_out
));
11734 exp
= isl_parameter_alignment_reordering(bmap
->dim
, model
);
11735 exp
= isl_reordering_extend_space(exp
,
11736 isl_basic_map_get_space(bmap
));
11737 dim_map
= isl_dim_map_from_reordering(exp
);
11738 bmap
= isl_basic_map_realign(bmap
,
11739 exp
? isl_space_copy(exp
->dim
) : NULL
,
11740 isl_dim_map_extend(dim_map
, bmap
));
11741 isl_reordering_free(exp
);
11745 isl_space_free(model
);
11748 isl_space_free(model
);
11749 isl_basic_map_free(bmap
);
11753 /* Align the parameters of "bset" to those of "model", introducing
11754 * additional parameters if needed.
11756 __isl_give isl_basic_set
*isl_basic_set_align_params(
11757 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*model
)
11759 return isl_basic_map_align_params(bset
, model
);
11762 __isl_give isl_mat
*isl_basic_map_equalities_matrix(
11763 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
11764 enum isl_dim_type c2
, enum isl_dim_type c3
,
11765 enum isl_dim_type c4
, enum isl_dim_type c5
)
11767 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11768 struct isl_mat
*mat
;
11774 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_eq
,
11775 isl_basic_map_total_dim(bmap
) + 1);
11778 for (i
= 0; i
< bmap
->n_eq
; ++i
)
11779 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11780 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11781 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11782 isl_int_set(mat
->row
[i
][pos
],
11783 bmap
->eq
[i
][off
+ k
]);
11791 __isl_give isl_mat
*isl_basic_map_inequalities_matrix(
11792 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
11793 enum isl_dim_type c2
, enum isl_dim_type c3
,
11794 enum isl_dim_type c4
, enum isl_dim_type c5
)
11796 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11797 struct isl_mat
*mat
;
11803 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_ineq
,
11804 isl_basic_map_total_dim(bmap
) + 1);
11807 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
11808 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11809 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11810 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11811 isl_int_set(mat
->row
[i
][pos
],
11812 bmap
->ineq
[i
][off
+ k
]);
11820 __isl_give isl_basic_map
*isl_basic_map_from_constraint_matrices(
11821 __isl_take isl_space
*dim
,
11822 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
11823 enum isl_dim_type c2
, enum isl_dim_type c3
,
11824 enum isl_dim_type c4
, enum isl_dim_type c5
)
11826 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11827 isl_basic_map
*bmap
;
11833 if (!dim
|| !eq
|| !ineq
)
11836 if (eq
->n_col
!= ineq
->n_col
)
11837 isl_die(dim
->ctx
, isl_error_invalid
,
11838 "equalities and inequalities matrices should have "
11839 "same number of columns", goto error
);
11841 total
= 1 + isl_space_dim(dim
, isl_dim_all
);
11843 if (eq
->n_col
< total
)
11844 isl_die(dim
->ctx
, isl_error_invalid
,
11845 "number of columns too small", goto error
);
11847 extra
= eq
->n_col
- total
;
11849 bmap
= isl_basic_map_alloc_space(isl_space_copy(dim
), extra
,
11850 eq
->n_row
, ineq
->n_row
);
11853 for (i
= 0; i
< extra
; ++i
) {
11854 k
= isl_basic_map_alloc_div(bmap
);
11857 isl_int_set_si(bmap
->div
[k
][0], 0);
11859 for (i
= 0; i
< eq
->n_row
; ++i
) {
11860 l
= isl_basic_map_alloc_equality(bmap
);
11863 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11864 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11865 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11866 isl_int_set(bmap
->eq
[l
][off
+ k
],
11872 for (i
= 0; i
< ineq
->n_row
; ++i
) {
11873 l
= isl_basic_map_alloc_inequality(bmap
);
11876 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11877 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11878 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11879 isl_int_set(bmap
->ineq
[l
][off
+ k
],
11880 ineq
->row
[i
][pos
]);
11886 isl_space_free(dim
);
11888 isl_mat_free(ineq
);
11890 bmap
= isl_basic_map_simplify(bmap
);
11891 return isl_basic_map_finalize(bmap
);
11893 isl_space_free(dim
);
11895 isl_mat_free(ineq
);
11899 __isl_give isl_mat
*isl_basic_set_equalities_matrix(
11900 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
11901 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
11903 return isl_basic_map_equalities_matrix(bset_to_bmap(bset
),
11904 c1
, c2
, c3
, c4
, isl_dim_in
);
11907 __isl_give isl_mat
*isl_basic_set_inequalities_matrix(
11908 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
11909 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
11911 return isl_basic_map_inequalities_matrix(bset_to_bmap(bset
),
11912 c1
, c2
, c3
, c4
, isl_dim_in
);
11915 __isl_give isl_basic_set
*isl_basic_set_from_constraint_matrices(
11916 __isl_take isl_space
*dim
,
11917 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
11918 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
11920 isl_basic_map
*bmap
;
11921 bmap
= isl_basic_map_from_constraint_matrices(dim
, eq
, ineq
,
11922 c1
, c2
, c3
, c4
, isl_dim_in
);
11923 return bset_from_bmap(bmap
);
11926 isl_bool
isl_basic_map_can_zip(__isl_keep isl_basic_map
*bmap
)
11929 return isl_bool_error
;
11931 return isl_space_can_zip(bmap
->dim
);
11934 isl_bool
isl_map_can_zip(__isl_keep isl_map
*map
)
11937 return isl_bool_error
;
11939 return isl_space_can_zip(map
->dim
);
11942 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
11943 * (A -> C) -> (B -> D).
11945 __isl_give isl_basic_map
*isl_basic_map_zip(__isl_take isl_basic_map
*bmap
)
11954 if (!isl_basic_map_can_zip(bmap
))
11955 isl_die(bmap
->ctx
, isl_error_invalid
,
11956 "basic map cannot be zipped", goto error
);
11957 pos
= isl_basic_map_offset(bmap
, isl_dim_in
) +
11958 isl_space_dim(bmap
->dim
->nested
[0], isl_dim_in
);
11959 n1
= isl_space_dim(bmap
->dim
->nested
[0], isl_dim_out
);
11960 n2
= isl_space_dim(bmap
->dim
->nested
[1], isl_dim_in
);
11961 bmap
= isl_basic_map_cow(bmap
);
11962 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
11965 bmap
->dim
= isl_space_zip(bmap
->dim
);
11968 bmap
= isl_basic_map_mark_final(bmap
);
11971 isl_basic_map_free(bmap
);
11975 /* Given a map (A -> B) -> (C -> D), return the corresponding map
11976 * (A -> C) -> (B -> D).
11978 __isl_give isl_map
*isl_map_zip(__isl_take isl_map
*map
)
11985 if (!isl_map_can_zip(map
))
11986 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be zipped",
11989 map
= isl_map_cow(map
);
11993 for (i
= 0; i
< map
->n
; ++i
) {
11994 map
->p
[i
] = isl_basic_map_zip(map
->p
[i
]);
11999 map
->dim
= isl_space_zip(map
->dim
);
12009 /* Can we apply isl_basic_map_curry to "bmap"?
12010 * That is, does it have a nested relation in its domain?
12012 isl_bool
isl_basic_map_can_curry(__isl_keep isl_basic_map
*bmap
)
12015 return isl_bool_error
;
12017 return isl_space_can_curry(bmap
->dim
);
12020 /* Can we apply isl_map_curry to "map"?
12021 * That is, does it have a nested relation in its domain?
12023 isl_bool
isl_map_can_curry(__isl_keep isl_map
*map
)
12026 return isl_bool_error
;
12028 return isl_space_can_curry(map
->dim
);
12031 /* Given a basic map (A -> B) -> C, return the corresponding basic map
12034 __isl_give isl_basic_map
*isl_basic_map_curry(__isl_take isl_basic_map
*bmap
)
12040 if (!isl_basic_map_can_curry(bmap
))
12041 isl_die(bmap
->ctx
, isl_error_invalid
,
12042 "basic map cannot be curried", goto error
);
12043 bmap
= isl_basic_map_cow(bmap
);
12046 bmap
->dim
= isl_space_curry(bmap
->dim
);
12049 bmap
= isl_basic_map_mark_final(bmap
);
12052 isl_basic_map_free(bmap
);
12056 /* Given a map (A -> B) -> C, return the corresponding map
12059 __isl_give isl_map
*isl_map_curry(__isl_take isl_map
*map
)
12061 return isl_map_change_space(map
, &isl_map_can_curry
,
12062 "map cannot be curried", &isl_space_curry
);
12065 /* Can isl_map_range_curry be applied to "map"?
12066 * That is, does it have a nested relation in its range,
12067 * the domain of which is itself a nested relation?
12069 isl_bool
isl_map_can_range_curry(__isl_keep isl_map
*map
)
12072 return isl_bool_error
;
12074 return isl_space_can_range_curry(map
->dim
);
12077 /* Given a map A -> ((B -> C) -> D), return the corresponding map
12078 * A -> (B -> (C -> D)).
12080 __isl_give isl_map
*isl_map_range_curry(__isl_take isl_map
*map
)
12082 return isl_map_change_space(map
, &isl_map_can_range_curry
,
12083 "map range cannot be curried",
12084 &isl_space_range_curry
);
12087 /* Can we apply isl_basic_map_uncurry to "bmap"?
12088 * That is, does it have a nested relation in its domain?
12090 isl_bool
isl_basic_map_can_uncurry(__isl_keep isl_basic_map
*bmap
)
12093 return isl_bool_error
;
12095 return isl_space_can_uncurry(bmap
->dim
);
12098 /* Can we apply isl_map_uncurry to "map"?
12099 * That is, does it have a nested relation in its domain?
12101 isl_bool
isl_map_can_uncurry(__isl_keep isl_map
*map
)
12104 return isl_bool_error
;
12106 return isl_space_can_uncurry(map
->dim
);
12109 /* Given a basic map A -> (B -> C), return the corresponding basic map
12112 __isl_give isl_basic_map
*isl_basic_map_uncurry(__isl_take isl_basic_map
*bmap
)
12118 if (!isl_basic_map_can_uncurry(bmap
))
12119 isl_die(bmap
->ctx
, isl_error_invalid
,
12120 "basic map cannot be uncurried",
12121 return isl_basic_map_free(bmap
));
12122 bmap
= isl_basic_map_cow(bmap
);
12125 bmap
->dim
= isl_space_uncurry(bmap
->dim
);
12127 return isl_basic_map_free(bmap
);
12128 bmap
= isl_basic_map_mark_final(bmap
);
12132 /* Given a map A -> (B -> C), return the corresponding map
12135 __isl_give isl_map
*isl_map_uncurry(__isl_take isl_map
*map
)
12137 return isl_map_change_space(map
, &isl_map_can_uncurry
,
12138 "map cannot be uncurried", &isl_space_uncurry
);
12141 /* Construct a basic map mapping the domain of the affine expression
12142 * to a one-dimensional range prescribed by the affine expression.
12143 * If "rational" is set, then construct a rational basic map.
12145 * A NaN affine expression cannot be converted to a basic map.
12147 static __isl_give isl_basic_map
*isl_basic_map_from_aff2(
12148 __isl_take isl_aff
*aff
, int rational
)
12153 isl_local_space
*ls
;
12154 isl_basic_map
*bmap
= NULL
;
12158 is_nan
= isl_aff_is_nan(aff
);
12162 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
12163 "cannot convert NaN", goto error
);
12165 ls
= isl_aff_get_local_space(aff
);
12166 bmap
= isl_basic_map_from_local_space(ls
);
12167 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
12168 k
= isl_basic_map_alloc_equality(bmap
);
12172 pos
= isl_basic_map_offset(bmap
, isl_dim_out
);
12173 isl_seq_cpy(bmap
->eq
[k
], aff
->v
->el
+ 1, pos
);
12174 isl_int_neg(bmap
->eq
[k
][pos
], aff
->v
->el
[0]);
12175 isl_seq_cpy(bmap
->eq
[k
] + pos
+ 1, aff
->v
->el
+ 1 + pos
,
12176 aff
->v
->size
- (pos
+ 1));
12180 bmap
= isl_basic_map_set_rational(bmap
);
12181 bmap
= isl_basic_map_finalize(bmap
);
12185 isl_basic_map_free(bmap
);
12189 /* Construct a basic map mapping the domain of the affine expression
12190 * to a one-dimensional range prescribed by the affine expression.
12192 __isl_give isl_basic_map
*isl_basic_map_from_aff(__isl_take isl_aff
*aff
)
12194 return isl_basic_map_from_aff2(aff
, 0);
12197 /* Construct a map mapping the domain of the affine expression
12198 * to a one-dimensional range prescribed by the affine expression.
12200 __isl_give isl_map
*isl_map_from_aff(__isl_take isl_aff
*aff
)
12202 isl_basic_map
*bmap
;
12204 bmap
= isl_basic_map_from_aff(aff
);
12205 return isl_map_from_basic_map(bmap
);
12208 /* Construct a basic map mapping the domain the multi-affine expression
12209 * to its range, with each dimension in the range equated to the
12210 * corresponding affine expression.
12211 * If "rational" is set, then construct a rational basic map.
12213 __isl_give isl_basic_map
*isl_basic_map_from_multi_aff2(
12214 __isl_take isl_multi_aff
*maff
, int rational
)
12218 isl_basic_map
*bmap
;
12223 if (isl_space_dim(maff
->space
, isl_dim_out
) != maff
->n
)
12224 isl_die(isl_multi_aff_get_ctx(maff
), isl_error_internal
,
12225 "invalid space", goto error
);
12227 space
= isl_space_domain(isl_multi_aff_get_space(maff
));
12228 bmap
= isl_basic_map_universe(isl_space_from_domain(space
));
12230 bmap
= isl_basic_map_set_rational(bmap
);
12232 for (i
= 0; i
< maff
->n
; ++i
) {
12234 isl_basic_map
*bmap_i
;
12236 aff
= isl_aff_copy(maff
->p
[i
]);
12237 bmap_i
= isl_basic_map_from_aff2(aff
, rational
);
12239 bmap
= isl_basic_map_flat_range_product(bmap
, bmap_i
);
12242 bmap
= isl_basic_map_reset_space(bmap
, isl_multi_aff_get_space(maff
));
12244 isl_multi_aff_free(maff
);
12247 isl_multi_aff_free(maff
);
12251 /* Construct a basic map mapping the domain the multi-affine expression
12252 * to its range, with each dimension in the range equated to the
12253 * corresponding affine expression.
12255 __isl_give isl_basic_map
*isl_basic_map_from_multi_aff(
12256 __isl_take isl_multi_aff
*ma
)
12258 return isl_basic_map_from_multi_aff2(ma
, 0);
12261 /* Construct a map mapping the domain the multi-affine expression
12262 * to its range, with each dimension in the range equated to the
12263 * corresponding affine expression.
12265 __isl_give isl_map
*isl_map_from_multi_aff(__isl_take isl_multi_aff
*maff
)
12267 isl_basic_map
*bmap
;
12269 bmap
= isl_basic_map_from_multi_aff(maff
);
12270 return isl_map_from_basic_map(bmap
);
12273 /* Construct a basic map mapping a domain in the given space to
12274 * to an n-dimensional range, with n the number of elements in the list,
12275 * where each coordinate in the range is prescribed by the
12276 * corresponding affine expression.
12277 * The domains of all affine expressions in the list are assumed to match
12280 __isl_give isl_basic_map
*isl_basic_map_from_aff_list(
12281 __isl_take isl_space
*domain_dim
, __isl_take isl_aff_list
*list
)
12285 isl_basic_map
*bmap
;
12290 dim
= isl_space_from_domain(domain_dim
);
12291 bmap
= isl_basic_map_universe(dim
);
12293 for (i
= 0; i
< list
->n
; ++i
) {
12295 isl_basic_map
*bmap_i
;
12297 aff
= isl_aff_copy(list
->p
[i
]);
12298 bmap_i
= isl_basic_map_from_aff(aff
);
12300 bmap
= isl_basic_map_flat_range_product(bmap
, bmap_i
);
12303 isl_aff_list_free(list
);
12307 __isl_give isl_set
*isl_set_equate(__isl_take isl_set
*set
,
12308 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12310 return isl_map_equate(set
, type1
, pos1
, type2
, pos2
);
12313 /* Construct a basic map where the given dimensions are equal to each other.
12315 static __isl_give isl_basic_map
*equator(__isl_take isl_space
*space
,
12316 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12318 isl_basic_map
*bmap
= NULL
;
12324 if (pos1
>= isl_space_dim(space
, type1
))
12325 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
12326 "index out of bounds", goto error
);
12327 if (pos2
>= isl_space_dim(space
, type2
))
12328 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
12329 "index out of bounds", goto error
);
12331 if (type1
== type2
&& pos1
== pos2
)
12332 return isl_basic_map_universe(space
);
12334 bmap
= isl_basic_map_alloc_space(isl_space_copy(space
), 0, 1, 0);
12335 i
= isl_basic_map_alloc_equality(bmap
);
12338 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
12339 pos1
+= isl_basic_map_offset(bmap
, type1
);
12340 pos2
+= isl_basic_map_offset(bmap
, type2
);
12341 isl_int_set_si(bmap
->eq
[i
][pos1
], -1);
12342 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
12343 bmap
= isl_basic_map_finalize(bmap
);
12344 isl_space_free(space
);
12347 isl_space_free(space
);
12348 isl_basic_map_free(bmap
);
12352 /* Add a constraint imposing that the given two dimensions are equal.
12354 __isl_give isl_basic_map
*isl_basic_map_equate(__isl_take isl_basic_map
*bmap
,
12355 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12359 eq
= equator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
12361 bmap
= isl_basic_map_intersect(bmap
, eq
);
12366 /* Add a constraint imposing that the given two dimensions are equal.
12368 __isl_give isl_map
*isl_map_equate(__isl_take isl_map
*map
,
12369 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12371 isl_basic_map
*bmap
;
12373 bmap
= equator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
12375 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
12380 /* Add a constraint imposing that the given two dimensions have opposite values.
12382 __isl_give isl_map
*isl_map_oppose(__isl_take isl_map
*map
,
12383 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12385 isl_basic_map
*bmap
= NULL
;
12391 if (pos1
>= isl_map_dim(map
, type1
))
12392 isl_die(map
->ctx
, isl_error_invalid
,
12393 "index out of bounds", goto error
);
12394 if (pos2
>= isl_map_dim(map
, type2
))
12395 isl_die(map
->ctx
, isl_error_invalid
,
12396 "index out of bounds", goto error
);
12398 bmap
= isl_basic_map_alloc_space(isl_map_get_space(map
), 0, 1, 0);
12399 i
= isl_basic_map_alloc_equality(bmap
);
12402 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
12403 pos1
+= isl_basic_map_offset(bmap
, type1
);
12404 pos2
+= isl_basic_map_offset(bmap
, type2
);
12405 isl_int_set_si(bmap
->eq
[i
][pos1
], 1);
12406 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
12407 bmap
= isl_basic_map_finalize(bmap
);
12409 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
12413 isl_basic_map_free(bmap
);
12418 /* Construct a constraint imposing that the value of the first dimension is
12419 * greater than or equal to that of the second.
12421 static __isl_give isl_constraint
*constraint_order_ge(
12422 __isl_take isl_space
*space
, enum isl_dim_type type1
, int pos1
,
12423 enum isl_dim_type type2
, int pos2
)
12430 c
= isl_constraint_alloc_inequality(isl_local_space_from_space(space
));
12432 if (pos1
>= isl_constraint_dim(c
, type1
))
12433 isl_die(isl_constraint_get_ctx(c
), isl_error_invalid
,
12434 "index out of bounds", return isl_constraint_free(c
));
12435 if (pos2
>= isl_constraint_dim(c
, type2
))
12436 isl_die(isl_constraint_get_ctx(c
), isl_error_invalid
,
12437 "index out of bounds", return isl_constraint_free(c
));
12439 if (type1
== type2
&& pos1
== pos2
)
12442 c
= isl_constraint_set_coefficient_si(c
, type1
, pos1
, 1);
12443 c
= isl_constraint_set_coefficient_si(c
, type2
, pos2
, -1);
12448 /* Add a constraint imposing that the value of the first dimension is
12449 * greater than or equal to that of the second.
12451 __isl_give isl_basic_map
*isl_basic_map_order_ge(__isl_take isl_basic_map
*bmap
,
12452 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12457 if (type1
== type2
&& pos1
== pos2
)
12459 space
= isl_basic_map_get_space(bmap
);
12460 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
12461 bmap
= isl_basic_map_add_constraint(bmap
, c
);
12466 /* Add a constraint imposing that the value of the first dimension is
12467 * greater than or equal to that of the second.
12469 __isl_give isl_map
*isl_map_order_ge(__isl_take isl_map
*map
,
12470 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12475 if (type1
== type2
&& pos1
== pos2
)
12477 space
= isl_map_get_space(map
);
12478 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
12479 map
= isl_map_add_constraint(map
, c
);
12484 /* Add a constraint imposing that the value of the first dimension is
12485 * less than or equal to that of the second.
12487 __isl_give isl_map
*isl_map_order_le(__isl_take isl_map
*map
,
12488 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12490 return isl_map_order_ge(map
, type2
, pos2
, type1
, pos1
);
12493 /* Construct a basic map where the value of the first dimension is
12494 * greater than that of the second.
12496 static __isl_give isl_basic_map
*greator(__isl_take isl_space
*space
,
12497 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12499 isl_basic_map
*bmap
= NULL
;
12505 if (pos1
>= isl_space_dim(space
, type1
))
12506 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
12507 "index out of bounds", goto error
);
12508 if (pos2
>= isl_space_dim(space
, type2
))
12509 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
12510 "index out of bounds", goto error
);
12512 if (type1
== type2
&& pos1
== pos2
)
12513 return isl_basic_map_empty(space
);
12515 bmap
= isl_basic_map_alloc_space(space
, 0, 0, 1);
12516 i
= isl_basic_map_alloc_inequality(bmap
);
12518 return isl_basic_map_free(bmap
);
12519 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
12520 pos1
+= isl_basic_map_offset(bmap
, type1
);
12521 pos2
+= isl_basic_map_offset(bmap
, type2
);
12522 isl_int_set_si(bmap
->ineq
[i
][pos1
], 1);
12523 isl_int_set_si(bmap
->ineq
[i
][pos2
], -1);
12524 isl_int_set_si(bmap
->ineq
[i
][0], -1);
12525 bmap
= isl_basic_map_finalize(bmap
);
12529 isl_space_free(space
);
12530 isl_basic_map_free(bmap
);
12534 /* Add a constraint imposing that the value of the first dimension is
12535 * greater than that of the second.
12537 __isl_give isl_basic_map
*isl_basic_map_order_gt(__isl_take isl_basic_map
*bmap
,
12538 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12542 gt
= greator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
12544 bmap
= isl_basic_map_intersect(bmap
, gt
);
12549 /* Add a constraint imposing that the value of the first dimension is
12550 * greater than that of the second.
12552 __isl_give isl_map
*isl_map_order_gt(__isl_take isl_map
*map
,
12553 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12555 isl_basic_map
*bmap
;
12557 bmap
= greator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
12559 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
12564 /* Add a constraint imposing that the value of the first dimension is
12565 * smaller than that of the second.
12567 __isl_give isl_map
*isl_map_order_lt(__isl_take isl_map
*map
,
12568 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12570 return isl_map_order_gt(map
, type2
, pos2
, type1
, pos1
);
12573 __isl_give isl_aff
*isl_basic_map_get_div(__isl_keep isl_basic_map
*bmap
,
12577 isl_local_space
*ls
;
12582 if (!isl_basic_map_divs_known(bmap
))
12583 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12584 "some divs are unknown", return NULL
);
12586 ls
= isl_basic_map_get_local_space(bmap
);
12587 div
= isl_local_space_get_div(ls
, pos
);
12588 isl_local_space_free(ls
);
12593 __isl_give isl_aff
*isl_basic_set_get_div(__isl_keep isl_basic_set
*bset
,
12596 return isl_basic_map_get_div(bset
, pos
);
12599 /* Plug in "subs" for dimension "type", "pos" of "bset".
12601 * Let i be the dimension to replace and let "subs" be of the form
12605 * Any integer division with a non-zero coefficient for i,
12607 * floor((a i + g)/m)
12611 * floor((a f + d g)/(m d))
12613 * Constraints of the form
12621 * We currently require that "subs" is an integral expression.
12622 * Handling rational expressions may require us to add stride constraints
12623 * as we do in isl_basic_set_preimage_multi_aff.
12625 __isl_give isl_basic_set
*isl_basic_set_substitute(
12626 __isl_take isl_basic_set
*bset
,
12627 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
12633 if (bset
&& isl_basic_set_plain_is_empty(bset
))
12636 bset
= isl_basic_set_cow(bset
);
12637 if (!bset
|| !subs
)
12640 ctx
= isl_basic_set_get_ctx(bset
);
12641 if (!isl_space_is_equal(bset
->dim
, subs
->ls
->dim
))
12642 isl_die(ctx
, isl_error_invalid
,
12643 "spaces don't match", goto error
);
12644 if (isl_local_space_dim(subs
->ls
, isl_dim_div
) != 0)
12645 isl_die(ctx
, isl_error_unsupported
,
12646 "cannot handle divs yet", goto error
);
12647 if (!isl_int_is_one(subs
->v
->el
[0]))
12648 isl_die(ctx
, isl_error_invalid
,
12649 "can only substitute integer expressions", goto error
);
12651 pos
+= isl_basic_set_offset(bset
, type
);
12655 for (i
= 0; i
< bset
->n_eq
; ++i
) {
12656 if (isl_int_is_zero(bset
->eq
[i
][pos
]))
12658 isl_int_set(v
, bset
->eq
[i
][pos
]);
12659 isl_int_set_si(bset
->eq
[i
][pos
], 0);
12660 isl_seq_combine(bset
->eq
[i
], subs
->v
->el
[0], bset
->eq
[i
],
12661 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12664 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
12665 if (isl_int_is_zero(bset
->ineq
[i
][pos
]))
12667 isl_int_set(v
, bset
->ineq
[i
][pos
]);
12668 isl_int_set_si(bset
->ineq
[i
][pos
], 0);
12669 isl_seq_combine(bset
->ineq
[i
], subs
->v
->el
[0], bset
->ineq
[i
],
12670 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12673 for (i
= 0; i
< bset
->n_div
; ++i
) {
12674 if (isl_int_is_zero(bset
->div
[i
][1 + pos
]))
12676 isl_int_set(v
, bset
->div
[i
][1 + pos
]);
12677 isl_int_set_si(bset
->div
[i
][1 + pos
], 0);
12678 isl_seq_combine(bset
->div
[i
] + 1,
12679 subs
->v
->el
[0], bset
->div
[i
] + 1,
12680 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12681 isl_int_mul(bset
->div
[i
][0], bset
->div
[i
][0], subs
->v
->el
[0]);
12686 bset
= isl_basic_set_simplify(bset
);
12687 return isl_basic_set_finalize(bset
);
12689 isl_basic_set_free(bset
);
12693 /* Plug in "subs" for dimension "type", "pos" of "set".
12695 __isl_give isl_set
*isl_set_substitute(__isl_take isl_set
*set
,
12696 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
12700 if (set
&& isl_set_plain_is_empty(set
))
12703 set
= isl_set_cow(set
);
12707 for (i
= set
->n
- 1; i
>= 0; --i
) {
12708 set
->p
[i
] = isl_basic_set_substitute(set
->p
[i
], type
, pos
, subs
);
12709 if (remove_if_empty(set
, i
) < 0)
12719 /* Check if the range of "ma" is compatible with the domain or range
12720 * (depending on "type") of "bmap".
12721 * Return -1 if anything is wrong.
12723 static int check_basic_map_compatible_range_multi_aff(
12724 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type type
,
12725 __isl_keep isl_multi_aff
*ma
)
12728 isl_space
*ma_space
;
12730 ma_space
= isl_multi_aff_get_space(ma
);
12732 m
= isl_space_match(bmap
->dim
, isl_dim_param
, ma_space
, isl_dim_param
);
12736 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12737 "parameters don't match", goto error
);
12738 m
= isl_space_tuple_is_equal(bmap
->dim
, type
, ma_space
, isl_dim_out
);
12742 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12743 "spaces don't match", goto error
);
12745 isl_space_free(ma_space
);
12748 isl_space_free(ma_space
);
12752 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12753 * coefficients before the transformed range of dimensions,
12754 * the "n_after" coefficients after the transformed range of dimensions
12755 * and the coefficients of the other divs in "bmap".
12757 static int set_ma_divs(__isl_keep isl_basic_map
*bmap
,
12758 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
, int n_div
)
12763 isl_local_space
*ls
;
12768 ls
= isl_aff_get_domain_local_space(ma
->p
[0]);
12772 n_param
= isl_local_space_dim(ls
, isl_dim_param
);
12773 n_set
= isl_local_space_dim(ls
, isl_dim_set
);
12774 for (i
= 0; i
< n_div
; ++i
) {
12775 int o_bmap
= 0, o_ls
= 0;
12777 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], 1 + 1 + n_param
);
12778 o_bmap
+= 1 + 1 + n_param
;
12779 o_ls
+= 1 + 1 + n_param
;
12780 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_before
);
12781 o_bmap
+= n_before
;
12782 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
12783 ls
->div
->row
[i
] + o_ls
, n_set
);
12786 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_after
);
12788 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
12789 ls
->div
->row
[i
] + o_ls
, n_div
);
12792 isl_seq_clr(bmap
->div
[i
] + o_bmap
, bmap
->n_div
- n_div
);
12793 if (isl_basic_set_add_div_constraints(bmap
, i
) < 0)
12797 isl_local_space_free(ls
);
12800 isl_local_space_free(ls
);
12804 /* How many stride constraints does "ma" enforce?
12805 * That is, how many of the affine expressions have a denominator
12806 * different from one?
12808 static int multi_aff_strides(__isl_keep isl_multi_aff
*ma
)
12813 for (i
= 0; i
< ma
->n
; ++i
)
12814 if (!isl_int_is_one(ma
->p
[i
]->v
->el
[0]))
12820 /* For each affine expression in ma of the form
12822 * x_i = (f_i y + h_i)/m_i
12824 * with m_i different from one, add a constraint to "bmap"
12827 * f_i y + h_i = m_i alpha_i
12829 * with alpha_i an additional existentially quantified variable.
12831 * The input variables of "ma" correspond to a subset of the variables
12832 * of "bmap". There are "n_before" variables in "bmap" before this
12833 * subset and "n_after" variables after this subset.
12834 * The integer divisions of the affine expressions in "ma" are assumed
12835 * to have been aligned. There are "n_div_ma" of them and
12836 * they appear first in "bmap", straight after the "n_after" variables.
12838 static __isl_give isl_basic_map
*add_ma_strides(
12839 __isl_take isl_basic_map
*bmap
, __isl_keep isl_multi_aff
*ma
,
12840 int n_before
, int n_after
, int n_div_ma
)
12848 total
= isl_basic_map_total_dim(bmap
);
12849 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
12850 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
12851 for (i
= 0; i
< ma
->n
; ++i
) {
12852 int o_bmap
= 0, o_ma
= 1;
12854 if (isl_int_is_one(ma
->p
[i
]->v
->el
[0]))
12856 div
= isl_basic_map_alloc_div(bmap
);
12857 k
= isl_basic_map_alloc_equality(bmap
);
12858 if (div
< 0 || k
< 0)
12860 isl_int_set_si(bmap
->div
[div
][0], 0);
12861 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12862 ma
->p
[i
]->v
->el
+ o_ma
, 1 + n_param
);
12863 o_bmap
+= 1 + n_param
;
12864 o_ma
+= 1 + n_param
;
12865 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_before
);
12866 o_bmap
+= n_before
;
12867 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12868 ma
->p
[i
]->v
->el
+ o_ma
, n_in
);
12871 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_after
);
12873 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12874 ma
->p
[i
]->v
->el
+ o_ma
, n_div_ma
);
12875 o_bmap
+= n_div_ma
;
12877 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, 1 + total
- o_bmap
);
12878 isl_int_neg(bmap
->eq
[k
][1 + total
], ma
->p
[i
]->v
->el
[0]);
12884 isl_basic_map_free(bmap
);
12888 /* Replace the domain or range space (depending on "type) of "space" by "set".
12890 static __isl_give isl_space
*isl_space_set(__isl_take isl_space
*space
,
12891 enum isl_dim_type type
, __isl_take isl_space
*set
)
12893 if (type
== isl_dim_in
) {
12894 space
= isl_space_range(space
);
12895 space
= isl_space_map_from_domain_and_range(set
, space
);
12897 space
= isl_space_domain(space
);
12898 space
= isl_space_map_from_domain_and_range(space
, set
);
12904 /* Compute the preimage of the domain or range (depending on "type")
12905 * of "bmap" under the function represented by "ma".
12906 * In other words, plug in "ma" in the domain or range of "bmap".
12907 * The result is a basic map that lives in the same space as "bmap"
12908 * except that the domain or range has been replaced by
12909 * the domain space of "ma".
12911 * If bmap is represented by
12913 * A(p) + S u + B x + T v + C(divs) >= 0,
12915 * where u and x are input and output dimensions if type == isl_dim_out
12916 * while x and v are input and output dimensions if type == isl_dim_in,
12917 * and ma is represented by
12919 * x = D(p) + F(y) + G(divs')
12921 * then the result is
12923 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12925 * The divs in the input set are similarly adjusted.
12928 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12932 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
12933 * B_i G(divs') + c_i(divs))/n_i)
12935 * If bmap is not a rational map and if F(y) involves any denominators
12937 * x_i = (f_i y + h_i)/m_i
12939 * then additional constraints are added to ensure that we only
12940 * map back integer points. That is we enforce
12942 * f_i y + h_i = m_i alpha_i
12944 * with alpha_i an additional existentially quantified variable.
12946 * We first copy over the divs from "ma".
12947 * Then we add the modified constraints and divs from "bmap".
12948 * Finally, we add the stride constraints, if needed.
12950 __isl_give isl_basic_map
*isl_basic_map_preimage_multi_aff(
12951 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
12952 __isl_take isl_multi_aff
*ma
)
12956 isl_basic_map
*res
= NULL
;
12957 int n_before
, n_after
, n_div_bmap
, n_div_ma
;
12958 isl_int f
, c1
, c2
, g
;
12959 int rational
, strides
;
12966 ma
= isl_multi_aff_align_divs(ma
);
12969 if (check_basic_map_compatible_range_multi_aff(bmap
, type
, ma
) < 0)
12972 if (type
== isl_dim_in
) {
12974 n_after
= isl_basic_map_dim(bmap
, isl_dim_out
);
12976 n_before
= isl_basic_map_dim(bmap
, isl_dim_in
);
12979 n_div_bmap
= isl_basic_map_dim(bmap
, isl_dim_div
);
12980 n_div_ma
= ma
->n
? isl_aff_dim(ma
->p
[0], isl_dim_div
) : 0;
12982 space
= isl_multi_aff_get_domain_space(ma
);
12983 space
= isl_space_set(isl_basic_map_get_space(bmap
), type
, space
);
12984 rational
= isl_basic_map_is_rational(bmap
);
12985 strides
= rational
? 0 : multi_aff_strides(ma
);
12986 res
= isl_basic_map_alloc_space(space
, n_div_ma
+ n_div_bmap
+ strides
,
12987 bmap
->n_eq
+ strides
, bmap
->n_ineq
+ 2 * n_div_ma
);
12989 res
= isl_basic_map_set_rational(res
);
12991 for (i
= 0; i
< n_div_ma
+ n_div_bmap
; ++i
)
12992 if (isl_basic_map_alloc_div(res
) < 0)
12995 if (set_ma_divs(res
, ma
, n_before
, n_after
, n_div_ma
) < 0)
12998 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
12999 k
= isl_basic_map_alloc_equality(res
);
13002 isl_seq_preimage(res
->eq
[k
], bmap
->eq
[i
], ma
, n_before
,
13003 n_after
, n_div_ma
, n_div_bmap
, f
, c1
, c2
, g
, 0);
13006 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
13007 k
= isl_basic_map_alloc_inequality(res
);
13010 isl_seq_preimage(res
->ineq
[k
], bmap
->ineq
[i
], ma
, n_before
,
13011 n_after
, n_div_ma
, n_div_bmap
, f
, c1
, c2
, g
, 0);
13014 for (i
= 0; i
< bmap
->n_div
; ++i
) {
13015 if (isl_int_is_zero(bmap
->div
[i
][0])) {
13016 isl_int_set_si(res
->div
[n_div_ma
+ i
][0], 0);
13019 isl_seq_preimage(res
->div
[n_div_ma
+ i
], bmap
->div
[i
], ma
,
13020 n_before
, n_after
, n_div_ma
, n_div_bmap
,
13025 res
= add_ma_strides(res
, ma
, n_before
, n_after
, n_div_ma
);
13031 isl_basic_map_free(bmap
);
13032 isl_multi_aff_free(ma
);
13033 res
= isl_basic_set_simplify(res
);
13034 return isl_basic_map_finalize(res
);
13040 isl_basic_map_free(bmap
);
13041 isl_multi_aff_free(ma
);
13042 isl_basic_map_free(res
);
13046 /* Compute the preimage of "bset" under the function represented by "ma".
13047 * In other words, plug in "ma" in "bset". The result is a basic set
13048 * that lives in the domain space of "ma".
13050 __isl_give isl_basic_set
*isl_basic_set_preimage_multi_aff(
13051 __isl_take isl_basic_set
*bset
, __isl_take isl_multi_aff
*ma
)
13053 return isl_basic_map_preimage_multi_aff(bset
, isl_dim_set
, ma
);
13056 /* Compute the preimage of the domain of "bmap" under the function
13057 * represented by "ma".
13058 * In other words, plug in "ma" in the domain of "bmap".
13059 * The result is a basic map that lives in the same space as "bmap"
13060 * except that the domain has been replaced by the domain space of "ma".
13062 __isl_give isl_basic_map
*isl_basic_map_preimage_domain_multi_aff(
13063 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
13065 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_in
, ma
);
13068 /* Compute the preimage of the range of "bmap" under the function
13069 * represented by "ma".
13070 * In other words, plug in "ma" in the range of "bmap".
13071 * The result is a basic map that lives in the same space as "bmap"
13072 * except that the range has been replaced by the domain space of "ma".
13074 __isl_give isl_basic_map
*isl_basic_map_preimage_range_multi_aff(
13075 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
13077 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_out
, ma
);
13080 /* Check if the range of "ma" is compatible with the domain or range
13081 * (depending on "type") of "map".
13082 * Return -1 if anything is wrong.
13084 static int check_map_compatible_range_multi_aff(
13085 __isl_keep isl_map
*map
, enum isl_dim_type type
,
13086 __isl_keep isl_multi_aff
*ma
)
13089 isl_space
*ma_space
;
13091 ma_space
= isl_multi_aff_get_space(ma
);
13092 m
= isl_space_tuple_is_equal(map
->dim
, type
, ma_space
, isl_dim_out
);
13093 isl_space_free(ma_space
);
13095 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
13096 "spaces don't match", return -1);
13100 /* Compute the preimage of the domain or range (depending on "type")
13101 * of "map" under the function represented by "ma".
13102 * In other words, plug in "ma" in the domain or range of "map".
13103 * The result is a map that lives in the same space as "map"
13104 * except that the domain or range has been replaced by
13105 * the domain space of "ma".
13107 * The parameters are assumed to have been aligned.
13109 static __isl_give isl_map
*map_preimage_multi_aff(__isl_take isl_map
*map
,
13110 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
13115 map
= isl_map_cow(map
);
13116 ma
= isl_multi_aff_align_divs(ma
);
13119 if (check_map_compatible_range_multi_aff(map
, type
, ma
) < 0)
13122 for (i
= 0; i
< map
->n
; ++i
) {
13123 map
->p
[i
] = isl_basic_map_preimage_multi_aff(map
->p
[i
], type
,
13124 isl_multi_aff_copy(ma
));
13129 space
= isl_multi_aff_get_domain_space(ma
);
13130 space
= isl_space_set(isl_map_get_space(map
), type
, space
);
13132 isl_space_free(map
->dim
);
13137 isl_multi_aff_free(ma
);
13139 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
13140 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
13143 isl_multi_aff_free(ma
);
13148 /* Compute the preimage of the domain or range (depending on "type")
13149 * of "map" under the function represented by "ma".
13150 * In other words, plug in "ma" in the domain or range of "map".
13151 * The result is a map that lives in the same space as "map"
13152 * except that the domain or range has been replaced by
13153 * the domain space of "ma".
13155 __isl_give isl_map
*isl_map_preimage_multi_aff(__isl_take isl_map
*map
,
13156 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
13161 if (isl_space_match(map
->dim
, isl_dim_param
, ma
->space
, isl_dim_param
))
13162 return map_preimage_multi_aff(map
, type
, ma
);
13164 if (!isl_space_has_named_params(map
->dim
) ||
13165 !isl_space_has_named_params(ma
->space
))
13166 isl_die(map
->ctx
, isl_error_invalid
,
13167 "unaligned unnamed parameters", goto error
);
13168 map
= isl_map_align_params(map
, isl_multi_aff_get_space(ma
));
13169 ma
= isl_multi_aff_align_params(ma
, isl_map_get_space(map
));
13171 return map_preimage_multi_aff(map
, type
, ma
);
13173 isl_multi_aff_free(ma
);
13174 return isl_map_free(map
);
13177 /* Compute the preimage of "set" under the function represented by "ma".
13178 * In other words, plug in "ma" in "set". The result is a set
13179 * that lives in the domain space of "ma".
13181 __isl_give isl_set
*isl_set_preimage_multi_aff(__isl_take isl_set
*set
,
13182 __isl_take isl_multi_aff
*ma
)
13184 return isl_map_preimage_multi_aff(set
, isl_dim_set
, ma
);
13187 /* Compute the preimage of the domain of "map" under the function
13188 * represented by "ma".
13189 * In other words, plug in "ma" in the domain of "map".
13190 * The result is a map that lives in the same space as "map"
13191 * except that the domain has been replaced by the domain space of "ma".
13193 __isl_give isl_map
*isl_map_preimage_domain_multi_aff(__isl_take isl_map
*map
,
13194 __isl_take isl_multi_aff
*ma
)
13196 return isl_map_preimage_multi_aff(map
, isl_dim_in
, ma
);
13199 /* Compute the preimage of the range of "map" under the function
13200 * represented by "ma".
13201 * In other words, plug in "ma" in the range of "map".
13202 * The result is a map that lives in the same space as "map"
13203 * except that the range has been replaced by the domain space of "ma".
13205 __isl_give isl_map
*isl_map_preimage_range_multi_aff(__isl_take isl_map
*map
,
13206 __isl_take isl_multi_aff
*ma
)
13208 return isl_map_preimage_multi_aff(map
, isl_dim_out
, ma
);
13211 /* Compute the preimage of "map" under the function represented by "pma".
13212 * In other words, plug in "pma" in the domain or range of "map".
13213 * The result is a map that lives in the same space as "map",
13214 * except that the space of type "type" has been replaced by
13215 * the domain space of "pma".
13217 * The parameters of "map" and "pma" are assumed to have been aligned.
13219 static __isl_give isl_map
*isl_map_preimage_pw_multi_aff_aligned(
13220 __isl_take isl_map
*map
, enum isl_dim_type type
,
13221 __isl_take isl_pw_multi_aff
*pma
)
13230 isl_pw_multi_aff_free(pma
);
13231 res
= isl_map_empty(isl_map_get_space(map
));
13236 res
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
13237 isl_multi_aff_copy(pma
->p
[0].maff
));
13238 if (type
== isl_dim_in
)
13239 res
= isl_map_intersect_domain(res
,
13240 isl_map_copy(pma
->p
[0].set
));
13242 res
= isl_map_intersect_range(res
,
13243 isl_map_copy(pma
->p
[0].set
));
13245 for (i
= 1; i
< pma
->n
; ++i
) {
13248 res_i
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
13249 isl_multi_aff_copy(pma
->p
[i
].maff
));
13250 if (type
== isl_dim_in
)
13251 res_i
= isl_map_intersect_domain(res_i
,
13252 isl_map_copy(pma
->p
[i
].set
));
13254 res_i
= isl_map_intersect_range(res_i
,
13255 isl_map_copy(pma
->p
[i
].set
));
13256 res
= isl_map_union(res
, res_i
);
13259 isl_pw_multi_aff_free(pma
);
13263 isl_pw_multi_aff_free(pma
);
13268 /* Compute the preimage of "map" under the function represented by "pma".
13269 * In other words, plug in "pma" in the domain or range of "map".
13270 * The result is a map that lives in the same space as "map",
13271 * except that the space of type "type" has been replaced by
13272 * the domain space of "pma".
13274 __isl_give isl_map
*isl_map_preimage_pw_multi_aff(__isl_take isl_map
*map
,
13275 enum isl_dim_type type
, __isl_take isl_pw_multi_aff
*pma
)
13280 if (isl_space_match(map
->dim
, isl_dim_param
, pma
->dim
, isl_dim_param
))
13281 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
13283 if (!isl_space_has_named_params(map
->dim
) ||
13284 !isl_space_has_named_params(pma
->dim
))
13285 isl_die(map
->ctx
, isl_error_invalid
,
13286 "unaligned unnamed parameters", goto error
);
13287 map
= isl_map_align_params(map
, isl_pw_multi_aff_get_space(pma
));
13288 pma
= isl_pw_multi_aff_align_params(pma
, isl_map_get_space(map
));
13290 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
13292 isl_pw_multi_aff_free(pma
);
13293 return isl_map_free(map
);
13296 /* Compute the preimage of "set" under the function represented by "pma".
13297 * In other words, plug in "pma" in "set". The result is a set
13298 * that lives in the domain space of "pma".
13300 __isl_give isl_set
*isl_set_preimage_pw_multi_aff(__isl_take isl_set
*set
,
13301 __isl_take isl_pw_multi_aff
*pma
)
13303 return isl_map_preimage_pw_multi_aff(set
, isl_dim_set
, pma
);
13306 /* Compute the preimage of the domain of "map" under the function
13307 * represented by "pma".
13308 * In other words, plug in "pma" in the domain of "map".
13309 * The result is a map that lives in the same space as "map",
13310 * except that domain space has been replaced by the domain space of "pma".
13312 __isl_give isl_map
*isl_map_preimage_domain_pw_multi_aff(
13313 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
13315 return isl_map_preimage_pw_multi_aff(map
, isl_dim_in
, pma
);
13318 /* Compute the preimage of the range of "map" under the function
13319 * represented by "pma".
13320 * In other words, plug in "pma" in the range of "map".
13321 * The result is a map that lives in the same space as "map",
13322 * except that range space has been replaced by the domain space of "pma".
13324 __isl_give isl_map
*isl_map_preimage_range_pw_multi_aff(
13325 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
13327 return isl_map_preimage_pw_multi_aff(map
, isl_dim_out
, pma
);
13330 /* Compute the preimage of "map" under the function represented by "mpa".
13331 * In other words, plug in "mpa" in the domain or range of "map".
13332 * The result is a map that lives in the same space as "map",
13333 * except that the space of type "type" has been replaced by
13334 * the domain space of "mpa".
13336 * If the map does not involve any constraints that refer to the
13337 * dimensions of the substituted space, then the only possible
13338 * effect of "mpa" on the map is to map the space to a different space.
13339 * We create a separate isl_multi_aff to effectuate this change
13340 * in order to avoid spurious splitting of the map along the pieces
13343 __isl_give isl_map
*isl_map_preimage_multi_pw_aff(__isl_take isl_map
*map
,
13344 enum isl_dim_type type
, __isl_take isl_multi_pw_aff
*mpa
)
13347 isl_pw_multi_aff
*pma
;
13352 n
= isl_map_dim(map
, type
);
13353 if (!isl_map_involves_dims(map
, type
, 0, n
)) {
13357 space
= isl_multi_pw_aff_get_space(mpa
);
13358 isl_multi_pw_aff_free(mpa
);
13359 ma
= isl_multi_aff_zero(space
);
13360 return isl_map_preimage_multi_aff(map
, type
, ma
);
13363 pma
= isl_pw_multi_aff_from_multi_pw_aff(mpa
);
13364 return isl_map_preimage_pw_multi_aff(map
, type
, pma
);
13367 isl_multi_pw_aff_free(mpa
);
13371 /* Compute the preimage of "map" under the function represented by "mpa".
13372 * In other words, plug in "mpa" in the domain "map".
13373 * The result is a map that lives in the same space as "map",
13374 * except that domain space has been replaced by the domain space of "mpa".
13376 __isl_give isl_map
*isl_map_preimage_domain_multi_pw_aff(
13377 __isl_take isl_map
*map
, __isl_take isl_multi_pw_aff
*mpa
)
13379 return isl_map_preimage_multi_pw_aff(map
, isl_dim_in
, mpa
);
13382 /* Compute the preimage of "set" by the function represented by "mpa".
13383 * In other words, plug in "mpa" in "set".
13385 __isl_give isl_set
*isl_set_preimage_multi_pw_aff(__isl_take isl_set
*set
,
13386 __isl_take isl_multi_pw_aff
*mpa
)
13388 return isl_map_preimage_multi_pw_aff(set
, isl_dim_set
, mpa
);
13391 /* Are the "n" "coefficients" starting at "first" of the integer division
13392 * expressions at position "pos1" in "bmap1" and "pos2" in "bmap2" equal
13394 * The "coefficient" at position 0 is the denominator.
13395 * The "coefficient" at position 1 is the constant term.
13397 isl_bool
isl_basic_map_equal_div_expr_part(__isl_keep isl_basic_map
*bmap1
,
13398 int pos1
, __isl_keep isl_basic_map
*bmap2
, int pos2
,
13399 unsigned first
, unsigned n
)
13401 if (isl_basic_map_check_range(bmap1
, isl_dim_div
, pos1
, 1) < 0)
13402 return isl_bool_error
;
13403 if (isl_basic_map_check_range(bmap2
, isl_dim_div
, pos2
, 1) < 0)
13404 return isl_bool_error
;
13405 return isl_seq_eq(bmap1
->div
[pos1
] + first
,
13406 bmap2
->div
[pos2
] + first
, n
);
13409 /* Are the integer division expressions at position "pos1" in "bmap1" and
13410 * "pos2" in "bmap2" equal to each other, except that the constant terms
13413 isl_bool
isl_basic_map_equal_div_expr_except_constant(
13414 __isl_keep isl_basic_map
*bmap1
, int pos1
,
13415 __isl_keep isl_basic_map
*bmap2
, int pos2
)
13420 if (!bmap1
|| !bmap2
)
13421 return isl_bool_error
;
13422 total
= isl_basic_map_total_dim(bmap1
);
13423 if (total
!= isl_basic_map_total_dim(bmap2
))
13424 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
13425 "incomparable div expressions", return isl_bool_error
);
13426 equal
= isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
13428 if (equal
< 0 || !equal
)
13430 equal
= isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
13432 if (equal
< 0 || equal
)
13433 return isl_bool_not(equal
);
13434 return isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
13438 /* Replace the numerator of the constant term of the integer division
13439 * expression at position "div" in "bmap" by "value".
13440 * The caller guarantees that this does not change the meaning
13443 __isl_give isl_basic_map
*isl_basic_map_set_div_expr_constant_num_si_inplace(
13444 __isl_take isl_basic_map
*bmap
, int div
, int value
)
13446 if (isl_basic_map_check_range(bmap
, isl_dim_div
, div
, 1) < 0)
13447 return isl_basic_map_free(bmap
);
13449 isl_int_set_si(bmap
->div
[div
][1], value
);
13454 /* Is the point "inner" internal to inequality constraint "ineq"
13456 * The point is considered to be internal to the inequality constraint,
13457 * if it strictly lies on the positive side of the inequality constraint,
13458 * or if it lies on the constraint and the constraint is lexico-positive.
13460 static isl_bool
is_internal(__isl_keep isl_vec
*inner
,
13461 __isl_keep isl_basic_set
*bset
, int ineq
)
13467 if (!inner
|| !bset
)
13468 return isl_bool_error
;
13470 ctx
= isl_basic_set_get_ctx(bset
);
13471 isl_seq_inner_product(inner
->el
, bset
->ineq
[ineq
], inner
->size
,
13472 &ctx
->normalize_gcd
);
13473 if (!isl_int_is_zero(ctx
->normalize_gcd
))
13474 return isl_int_is_nonneg(ctx
->normalize_gcd
);
13476 total
= isl_basic_set_dim(bset
, isl_dim_all
);
13477 pos
= isl_seq_first_non_zero(bset
->ineq
[ineq
] + 1, total
);
13478 return isl_int_is_pos(bset
->ineq
[ineq
][1 + pos
]);
13481 /* Tighten the inequality constraints of "bset" that are outward with respect
13482 * to the point "vec".
13483 * That is, tighten the constraints that are not satisfied by "vec".
13485 * "vec" is a point internal to some superset S of "bset" that is used
13486 * to make the subsets of S disjoint, by tightening one half of the constraints
13487 * that separate two subsets. In particular, the constraints of S
13488 * are all satisfied by "vec" and should not be tightened.
13489 * Of the internal constraints, those that have "vec" on the outside
13490 * are tightened. The shared facet is included in the adjacent subset
13491 * with the opposite constraint.
13492 * For constraints that saturate "vec", this criterion cannot be used
13493 * to determine which of the two sides should be tightened.
13494 * Instead, the sign of the first non-zero coefficient is used
13495 * to make this choice. Note that this second criterion is never used
13496 * on the constraints of S since "vec" is interior to "S".
13498 __isl_give isl_basic_set
*isl_basic_set_tighten_outward(
13499 __isl_take isl_basic_set
*bset
, __isl_keep isl_vec
*vec
)
13503 bset
= isl_basic_set_cow(bset
);
13506 for (j
= 0; j
< bset
->n_ineq
; ++j
) {
13509 internal
= is_internal(vec
, bset
, j
);
13511 return isl_basic_set_free(bset
);
13514 isl_int_sub_ui(bset
->ineq
[j
][0], bset
->ineq
[j
][0], 1);