2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012-2014 Ecole Normale Superieure
5 * Copyright 2014 INRIA Rocquencourt
6 * Copyright 2016 INRIA Paris
7 * Copyright 2016 Sven Verdoolaege
9 * Use of this software is governed by the MIT license
11 * Written by Sven Verdoolaege, K.U.Leuven, Departement
12 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
13 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
14 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
15 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
16 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
17 * B.P. 105 - 78153 Le Chesnay, France
18 * and Centre de Recherche Inria de Paris, 2 rue Simone Iff - Voie DQ12,
19 * CS 42112, 75589 Paris Cedex 12, France
23 #include <isl_ctx_private.h>
24 #include <isl_map_private.h>
26 #include <isl/constraint.h>
27 #include "isl_space_private.h"
28 #include "isl_equalities.h"
29 #include <isl_lp_private.h>
33 #include <isl_reordering.h>
34 #include "isl_sample.h"
38 #include <isl_mat_private.h>
39 #include <isl_vec_private.h>
40 #include <isl_dim_map.h>
41 #include <isl_local_space_private.h>
42 #include <isl_aff_private.h>
43 #include <isl_options_private.h>
44 #include <isl_morph.h>
45 #include <isl_val_private.h>
46 #include <isl/deprecated/map_int.h>
47 #include <isl/deprecated/set_int.h>
49 #include <bset_to_bmap.c>
50 #include <bset_from_bmap.c>
51 #include <set_to_map.c>
52 #include <set_from_map.c>
54 static unsigned n(__isl_keep isl_space
*dim
, enum isl_dim_type type
)
57 case isl_dim_param
: return dim
->nparam
;
58 case isl_dim_in
: return dim
->n_in
;
59 case isl_dim_out
: return dim
->n_out
;
60 case isl_dim_all
: return dim
->nparam
+ dim
->n_in
+ dim
->n_out
;
65 static unsigned pos(__isl_keep isl_space
*dim
, enum isl_dim_type type
)
68 case isl_dim_param
: return 1;
69 case isl_dim_in
: return 1 + dim
->nparam
;
70 case isl_dim_out
: return 1 + dim
->nparam
+ dim
->n_in
;
75 unsigned isl_basic_map_dim(__isl_keep isl_basic_map
*bmap
,
76 enum isl_dim_type type
)
81 case isl_dim_cst
: return 1;
84 case isl_dim_out
: return isl_space_dim(bmap
->dim
, type
);
85 case isl_dim_div
: return bmap
->n_div
;
86 case isl_dim_all
: return isl_basic_map_total_dim(bmap
);
91 unsigned isl_map_dim(__isl_keep isl_map
*map
, enum isl_dim_type type
)
93 return map
? n(map
->dim
, type
) : 0;
96 unsigned isl_set_dim(__isl_keep isl_set
*set
, enum isl_dim_type type
)
98 return set
? n(set
->dim
, type
) : 0;
101 unsigned isl_basic_map_offset(struct isl_basic_map
*bmap
,
102 enum isl_dim_type type
)
111 case isl_dim_cst
: return 0;
112 case isl_dim_param
: return 1;
113 case isl_dim_in
: return 1 + space
->nparam
;
114 case isl_dim_out
: return 1 + space
->nparam
+ space
->n_in
;
115 case isl_dim_div
: return 1 + space
->nparam
+ space
->n_in
+
121 unsigned isl_basic_set_offset(struct isl_basic_set
*bset
,
122 enum isl_dim_type type
)
124 return isl_basic_map_offset(bset
, type
);
127 static unsigned map_offset(struct isl_map
*map
, enum isl_dim_type type
)
129 return pos(map
->dim
, type
);
132 unsigned isl_basic_set_dim(__isl_keep isl_basic_set
*bset
,
133 enum isl_dim_type type
)
135 return isl_basic_map_dim(bset
, type
);
138 unsigned isl_basic_set_n_dim(__isl_keep isl_basic_set
*bset
)
140 return isl_basic_set_dim(bset
, isl_dim_set
);
143 unsigned isl_basic_set_n_param(__isl_keep isl_basic_set
*bset
)
145 return isl_basic_set_dim(bset
, isl_dim_param
);
148 unsigned isl_basic_set_total_dim(const struct isl_basic_set
*bset
)
152 return isl_space_dim(bset
->dim
, isl_dim_all
) + bset
->n_div
;
155 unsigned isl_set_n_dim(__isl_keep isl_set
*set
)
157 return isl_set_dim(set
, isl_dim_set
);
160 unsigned isl_set_n_param(__isl_keep isl_set
*set
)
162 return isl_set_dim(set
, isl_dim_param
);
165 unsigned isl_basic_map_n_in(const struct isl_basic_map
*bmap
)
167 return bmap
? bmap
->dim
->n_in
: 0;
170 unsigned isl_basic_map_n_out(const struct isl_basic_map
*bmap
)
172 return bmap
? bmap
->dim
->n_out
: 0;
175 unsigned isl_basic_map_n_param(const struct isl_basic_map
*bmap
)
177 return bmap
? bmap
->dim
->nparam
: 0;
180 unsigned isl_basic_map_n_div(const struct isl_basic_map
*bmap
)
182 return bmap
? bmap
->n_div
: 0;
185 unsigned isl_basic_map_total_dim(const struct isl_basic_map
*bmap
)
187 return bmap
? isl_space_dim(bmap
->dim
, isl_dim_all
) + bmap
->n_div
: 0;
190 unsigned isl_map_n_in(const struct isl_map
*map
)
192 return map
? map
->dim
->n_in
: 0;
195 unsigned isl_map_n_out(const struct isl_map
*map
)
197 return map
? map
->dim
->n_out
: 0;
200 unsigned isl_map_n_param(const struct isl_map
*map
)
202 return map
? map
->dim
->nparam
: 0;
205 int isl_map_compatible_domain(struct isl_map
*map
, struct isl_set
*set
)
210 m
= isl_space_match(map
->dim
, isl_dim_param
, set
->dim
, isl_dim_param
);
213 return isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
214 set
->dim
, isl_dim_set
);
217 isl_bool
isl_basic_map_compatible_domain(__isl_keep isl_basic_map
*bmap
,
218 __isl_keep isl_basic_set
*bset
)
222 return isl_bool_error
;
223 m
= isl_space_match(bmap
->dim
, isl_dim_param
, bset
->dim
, isl_dim_param
);
226 return isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
227 bset
->dim
, isl_dim_set
);
230 int isl_map_compatible_range(__isl_keep isl_map
*map
, __isl_keep isl_set
*set
)
235 m
= isl_space_match(map
->dim
, isl_dim_param
, set
->dim
, isl_dim_param
);
238 return isl_space_tuple_is_equal(map
->dim
, isl_dim_out
,
239 set
->dim
, isl_dim_set
);
242 int isl_basic_map_compatible_range(struct isl_basic_map
*bmap
,
243 struct isl_basic_set
*bset
)
248 m
= isl_space_match(bmap
->dim
, isl_dim_param
, bset
->dim
, isl_dim_param
);
251 return isl_space_tuple_is_equal(bmap
->dim
, isl_dim_out
,
252 bset
->dim
, isl_dim_set
);
255 isl_ctx
*isl_basic_map_get_ctx(__isl_keep isl_basic_map
*bmap
)
257 return bmap
? bmap
->ctx
: NULL
;
260 isl_ctx
*isl_basic_set_get_ctx(__isl_keep isl_basic_set
*bset
)
262 return bset
? bset
->ctx
: NULL
;
265 isl_ctx
*isl_map_get_ctx(__isl_keep isl_map
*map
)
267 return map
? map
->ctx
: NULL
;
270 isl_ctx
*isl_set_get_ctx(__isl_keep isl_set
*set
)
272 return set
? set
->ctx
: NULL
;
275 __isl_give isl_space
*isl_basic_map_get_space(__isl_keep isl_basic_map
*bmap
)
279 return isl_space_copy(bmap
->dim
);
282 __isl_give isl_space
*isl_basic_set_get_space(__isl_keep isl_basic_set
*bset
)
286 return isl_space_copy(bset
->dim
);
289 /* Extract the divs in "bmap" as a matrix.
291 __isl_give isl_mat
*isl_basic_map_get_divs(__isl_keep isl_basic_map
*bmap
)
302 ctx
= isl_basic_map_get_ctx(bmap
);
303 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
304 cols
= 1 + 1 + total
+ bmap
->n_div
;
305 div
= isl_mat_alloc(ctx
, bmap
->n_div
, cols
);
309 for (i
= 0; i
< bmap
->n_div
; ++i
)
310 isl_seq_cpy(div
->row
[i
], bmap
->div
[i
], cols
);
315 /* Extract the divs in "bset" as a matrix.
317 __isl_give isl_mat
*isl_basic_set_get_divs(__isl_keep isl_basic_set
*bset
)
319 return isl_basic_map_get_divs(bset
);
322 __isl_give isl_local_space
*isl_basic_map_get_local_space(
323 __isl_keep isl_basic_map
*bmap
)
330 div
= isl_basic_map_get_divs(bmap
);
331 return isl_local_space_alloc_div(isl_space_copy(bmap
->dim
), div
);
334 __isl_give isl_local_space
*isl_basic_set_get_local_space(
335 __isl_keep isl_basic_set
*bset
)
337 return isl_basic_map_get_local_space(bset
);
340 /* For each known div d = floor(f/m), add the constraints
343 * -(f-(m-1)) + m d >= 0
345 * Do not finalize the result.
347 static __isl_give isl_basic_map
*add_known_div_constraints(
348 __isl_take isl_basic_map
*bmap
)
355 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
358 bmap
= isl_basic_map_cow(bmap
);
359 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 2 * n_div
);
362 for (i
= 0; i
< n_div
; ++i
) {
363 if (isl_int_is_zero(bmap
->div
[i
][0]))
365 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
366 return isl_basic_map_free(bmap
);
372 __isl_give isl_basic_map
*isl_basic_map_from_local_space(
373 __isl_take isl_local_space
*ls
)
382 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
383 bmap
= isl_basic_map_alloc_space(isl_local_space_get_space(ls
),
384 n_div
, 0, 2 * n_div
);
386 for (i
= 0; i
< n_div
; ++i
)
387 if (isl_basic_map_alloc_div(bmap
) < 0)
390 for (i
= 0; i
< n_div
; ++i
)
391 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], ls
->div
->n_col
);
392 bmap
= add_known_div_constraints(bmap
);
394 isl_local_space_free(ls
);
397 isl_local_space_free(ls
);
398 isl_basic_map_free(bmap
);
402 __isl_give isl_basic_set
*isl_basic_set_from_local_space(
403 __isl_take isl_local_space
*ls
)
405 return isl_basic_map_from_local_space(ls
);
408 __isl_give isl_space
*isl_map_get_space(__isl_keep isl_map
*map
)
412 return isl_space_copy(map
->dim
);
415 __isl_give isl_space
*isl_set_get_space(__isl_keep isl_set
*set
)
419 return isl_space_copy(set
->dim
);
422 __isl_give isl_basic_map
*isl_basic_map_set_tuple_name(
423 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
, const char *s
)
425 bmap
= isl_basic_map_cow(bmap
);
428 bmap
->dim
= isl_space_set_tuple_name(bmap
->dim
, type
, s
);
431 bmap
= isl_basic_map_finalize(bmap
);
434 isl_basic_map_free(bmap
);
438 __isl_give isl_basic_set
*isl_basic_set_set_tuple_name(
439 __isl_take isl_basic_set
*bset
, const char *s
)
441 return isl_basic_map_set_tuple_name(bset
, isl_dim_set
, s
);
444 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map
*bmap
,
445 enum isl_dim_type type
)
447 return bmap
? isl_space_get_tuple_name(bmap
->dim
, type
) : NULL
;
450 __isl_give isl_map
*isl_map_set_tuple_name(__isl_take isl_map
*map
,
451 enum isl_dim_type type
, const char *s
)
455 map
= isl_map_cow(map
);
459 map
->dim
= isl_space_set_tuple_name(map
->dim
, type
, s
);
463 for (i
= 0; i
< map
->n
; ++i
) {
464 map
->p
[i
] = isl_basic_map_set_tuple_name(map
->p
[i
], type
, s
);
475 /* Replace the identifier of the tuple of type "type" by "id".
477 __isl_give isl_basic_map
*isl_basic_map_set_tuple_id(
478 __isl_take isl_basic_map
*bmap
,
479 enum isl_dim_type type
, __isl_take isl_id
*id
)
481 bmap
= isl_basic_map_cow(bmap
);
484 bmap
->dim
= isl_space_set_tuple_id(bmap
->dim
, type
, id
);
486 return isl_basic_map_free(bmap
);
487 bmap
= isl_basic_map_finalize(bmap
);
494 /* Replace the identifier of the tuple by "id".
496 __isl_give isl_basic_set
*isl_basic_set_set_tuple_id(
497 __isl_take isl_basic_set
*bset
, __isl_take isl_id
*id
)
499 return isl_basic_map_set_tuple_id(bset
, isl_dim_set
, id
);
502 /* Does the input or output tuple have a name?
504 isl_bool
isl_map_has_tuple_name(__isl_keep isl_map
*map
, enum isl_dim_type type
)
506 return map
? isl_space_has_tuple_name(map
->dim
, type
) : isl_bool_error
;
509 const char *isl_map_get_tuple_name(__isl_keep isl_map
*map
,
510 enum isl_dim_type type
)
512 return map
? isl_space_get_tuple_name(map
->dim
, type
) : NULL
;
515 __isl_give isl_set
*isl_set_set_tuple_name(__isl_take isl_set
*set
,
518 return set_from_map(isl_map_set_tuple_name(set_to_map(set
),
522 __isl_give isl_map
*isl_map_set_tuple_id(__isl_take isl_map
*map
,
523 enum isl_dim_type type
, __isl_take isl_id
*id
)
525 map
= isl_map_cow(map
);
529 map
->dim
= isl_space_set_tuple_id(map
->dim
, type
, id
);
531 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
537 __isl_give isl_set
*isl_set_set_tuple_id(__isl_take isl_set
*set
,
538 __isl_take isl_id
*id
)
540 return isl_map_set_tuple_id(set
, isl_dim_set
, id
);
543 __isl_give isl_map
*isl_map_reset_tuple_id(__isl_take isl_map
*map
,
544 enum isl_dim_type type
)
546 map
= isl_map_cow(map
);
550 map
->dim
= isl_space_reset_tuple_id(map
->dim
, type
);
552 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
555 __isl_give isl_set
*isl_set_reset_tuple_id(__isl_take isl_set
*set
)
557 return isl_map_reset_tuple_id(set
, isl_dim_set
);
560 isl_bool
isl_map_has_tuple_id(__isl_keep isl_map
*map
, enum isl_dim_type type
)
562 return map
? isl_space_has_tuple_id(map
->dim
, type
) : isl_bool_error
;
565 __isl_give isl_id
*isl_map_get_tuple_id(__isl_keep isl_map
*map
,
566 enum isl_dim_type type
)
568 return map
? isl_space_get_tuple_id(map
->dim
, type
) : NULL
;
571 isl_bool
isl_set_has_tuple_id(__isl_keep isl_set
*set
)
573 return isl_map_has_tuple_id(set
, isl_dim_set
);
576 __isl_give isl_id
*isl_set_get_tuple_id(__isl_keep isl_set
*set
)
578 return isl_map_get_tuple_id(set
, isl_dim_set
);
581 /* Does the set tuple have a name?
583 isl_bool
isl_set_has_tuple_name(__isl_keep isl_set
*set
)
586 return isl_bool_error
;
587 return isl_space_has_tuple_name(set
->dim
, isl_dim_set
);
591 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set
*bset
)
593 return bset
? isl_space_get_tuple_name(bset
->dim
, isl_dim_set
) : NULL
;
596 const char *isl_set_get_tuple_name(__isl_keep isl_set
*set
)
598 return set
? isl_space_get_tuple_name(set
->dim
, isl_dim_set
) : NULL
;
601 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map
*bmap
,
602 enum isl_dim_type type
, unsigned pos
)
604 return bmap
? isl_space_get_dim_name(bmap
->dim
, type
, pos
) : NULL
;
607 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set
*bset
,
608 enum isl_dim_type type
, unsigned pos
)
610 return bset
? isl_space_get_dim_name(bset
->dim
, type
, pos
) : NULL
;
613 /* Does the given dimension have a name?
615 isl_bool
isl_map_has_dim_name(__isl_keep isl_map
*map
,
616 enum isl_dim_type type
, unsigned pos
)
619 return isl_bool_error
;
620 return isl_space_has_dim_name(map
->dim
, type
, pos
);
623 const char *isl_map_get_dim_name(__isl_keep isl_map
*map
,
624 enum isl_dim_type type
, unsigned pos
)
626 return map
? isl_space_get_dim_name(map
->dim
, type
, pos
) : NULL
;
629 const char *isl_set_get_dim_name(__isl_keep isl_set
*set
,
630 enum isl_dim_type type
, unsigned pos
)
632 return set
? isl_space_get_dim_name(set
->dim
, type
, pos
) : NULL
;
635 /* Does the given dimension have a name?
637 isl_bool
isl_set_has_dim_name(__isl_keep isl_set
*set
,
638 enum isl_dim_type type
, unsigned pos
)
641 return isl_bool_error
;
642 return isl_space_has_dim_name(set
->dim
, type
, pos
);
645 __isl_give isl_basic_map
*isl_basic_map_set_dim_name(
646 __isl_take isl_basic_map
*bmap
,
647 enum isl_dim_type type
, unsigned pos
, const char *s
)
649 bmap
= isl_basic_map_cow(bmap
);
652 bmap
->dim
= isl_space_set_dim_name(bmap
->dim
, type
, pos
, s
);
655 return isl_basic_map_finalize(bmap
);
657 isl_basic_map_free(bmap
);
661 __isl_give isl_map
*isl_map_set_dim_name(__isl_take isl_map
*map
,
662 enum isl_dim_type type
, unsigned pos
, const char *s
)
666 map
= isl_map_cow(map
);
670 map
->dim
= isl_space_set_dim_name(map
->dim
, type
, pos
, s
);
674 for (i
= 0; i
< map
->n
; ++i
) {
675 map
->p
[i
] = isl_basic_map_set_dim_name(map
->p
[i
], type
, pos
, s
);
686 __isl_give isl_basic_set
*isl_basic_set_set_dim_name(
687 __isl_take isl_basic_set
*bset
,
688 enum isl_dim_type type
, unsigned pos
, const char *s
)
690 return bset_from_bmap(isl_basic_map_set_dim_name(bset_to_bmap(bset
),
694 __isl_give isl_set
*isl_set_set_dim_name(__isl_take isl_set
*set
,
695 enum isl_dim_type type
, unsigned pos
, const char *s
)
697 return set_from_map(isl_map_set_dim_name(set_to_map(set
),
701 isl_bool
isl_basic_map_has_dim_id(__isl_keep isl_basic_map
*bmap
,
702 enum isl_dim_type type
, unsigned pos
)
705 return isl_bool_error
;
706 return isl_space_has_dim_id(bmap
->dim
, type
, pos
);
709 __isl_give isl_id
*isl_basic_set_get_dim_id(__isl_keep isl_basic_set
*bset
,
710 enum isl_dim_type type
, unsigned pos
)
712 return bset
? isl_space_get_dim_id(bset
->dim
, type
, pos
) : NULL
;
715 isl_bool
isl_map_has_dim_id(__isl_keep isl_map
*map
,
716 enum isl_dim_type type
, unsigned pos
)
718 return map
? isl_space_has_dim_id(map
->dim
, type
, pos
) : isl_bool_error
;
721 __isl_give isl_id
*isl_map_get_dim_id(__isl_keep isl_map
*map
,
722 enum isl_dim_type type
, unsigned pos
)
724 return map
? isl_space_get_dim_id(map
->dim
, type
, pos
) : NULL
;
727 isl_bool
isl_set_has_dim_id(__isl_keep isl_set
*set
,
728 enum isl_dim_type type
, unsigned pos
)
730 return isl_map_has_dim_id(set
, type
, pos
);
733 __isl_give isl_id
*isl_set_get_dim_id(__isl_keep isl_set
*set
,
734 enum isl_dim_type type
, unsigned pos
)
736 return isl_map_get_dim_id(set
, type
, pos
);
739 __isl_give isl_map
*isl_map_set_dim_id(__isl_take isl_map
*map
,
740 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
742 map
= isl_map_cow(map
);
746 map
->dim
= isl_space_set_dim_id(map
->dim
, type
, pos
, id
);
748 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
754 __isl_give isl_set
*isl_set_set_dim_id(__isl_take isl_set
*set
,
755 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
757 return isl_map_set_dim_id(set
, type
, pos
, id
);
760 int isl_map_find_dim_by_id(__isl_keep isl_map
*map
, enum isl_dim_type type
,
761 __isl_keep isl_id
*id
)
765 return isl_space_find_dim_by_id(map
->dim
, type
, id
);
768 int isl_set_find_dim_by_id(__isl_keep isl_set
*set
, enum isl_dim_type type
,
769 __isl_keep isl_id
*id
)
771 return isl_map_find_dim_by_id(set
, type
, id
);
774 /* Return the position of the dimension of the given type and name
776 * Return -1 if no such dimension can be found.
778 int isl_basic_map_find_dim_by_name(__isl_keep isl_basic_map
*bmap
,
779 enum isl_dim_type type
, const char *name
)
783 return isl_space_find_dim_by_name(bmap
->dim
, type
, name
);
786 int isl_map_find_dim_by_name(__isl_keep isl_map
*map
, enum isl_dim_type type
,
791 return isl_space_find_dim_by_name(map
->dim
, type
, name
);
794 int isl_set_find_dim_by_name(__isl_keep isl_set
*set
, enum isl_dim_type type
,
797 return isl_map_find_dim_by_name(set
, type
, name
);
800 /* Reset the user pointer on all identifiers of parameters and tuples
801 * of the space of "map".
803 __isl_give isl_map
*isl_map_reset_user(__isl_take isl_map
*map
)
807 space
= isl_map_get_space(map
);
808 space
= isl_space_reset_user(space
);
809 map
= isl_map_reset_space(map
, space
);
814 /* Reset the user pointer on all identifiers of parameters and tuples
815 * of the space of "set".
817 __isl_give isl_set
*isl_set_reset_user(__isl_take isl_set
*set
)
819 return isl_map_reset_user(set
);
822 int isl_basic_map_is_rational(__isl_keep isl_basic_map
*bmap
)
826 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
829 /* Has "map" been marked as a rational map?
830 * In particular, have all basic maps in "map" been marked this way?
831 * An empty map is not considered to be rational.
832 * Maps where only some of the basic maps are marked rational
835 isl_bool
isl_map_is_rational(__isl_keep isl_map
*map
)
841 return isl_bool_error
;
843 return isl_bool_false
;
844 rational
= isl_basic_map_is_rational(map
->p
[0]);
847 for (i
= 1; i
< map
->n
; ++i
) {
850 rational_i
= isl_basic_map_is_rational(map
->p
[i
]);
853 if (rational
!= rational_i
)
854 isl_die(isl_map_get_ctx(map
), isl_error_unsupported
,
855 "mixed rational and integer basic maps "
856 "not supported", return isl_bool_error
);
862 /* Has "set" been marked as a rational set?
863 * In particular, have all basic set in "set" been marked this way?
864 * An empty set is not considered to be rational.
865 * Sets where only some of the basic sets are marked rational
868 isl_bool
isl_set_is_rational(__isl_keep isl_set
*set
)
870 return isl_map_is_rational(set
);
873 int isl_basic_set_is_rational(__isl_keep isl_basic_set
*bset
)
875 return isl_basic_map_is_rational(bset
);
878 /* Does "bmap" contain any rational points?
880 * If "bmap" has an equality for each dimension, equating the dimension
881 * to an integer constant, then it has no rational points, even if it
882 * is marked as rational.
884 int isl_basic_map_has_rational(__isl_keep isl_basic_map
*bmap
)
886 int has_rational
= 1;
891 if (isl_basic_map_plain_is_empty(bmap
))
893 if (!isl_basic_map_is_rational(bmap
))
895 bmap
= isl_basic_map_copy(bmap
);
896 bmap
= isl_basic_map_implicit_equalities(bmap
);
899 total
= isl_basic_map_total_dim(bmap
);
900 if (bmap
->n_eq
== total
) {
902 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
903 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1, total
);
906 if (!isl_int_is_one(bmap
->eq
[i
][1 + j
]) &&
907 !isl_int_is_negone(bmap
->eq
[i
][1 + j
]))
909 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + j
+ 1,
917 isl_basic_map_free(bmap
);
922 /* Does "map" contain any rational points?
924 int isl_map_has_rational(__isl_keep isl_map
*map
)
931 for (i
= 0; i
< map
->n
; ++i
) {
932 has_rational
= isl_basic_map_has_rational(map
->p
[i
]);
933 if (has_rational
< 0)
941 /* Does "set" contain any rational points?
943 int isl_set_has_rational(__isl_keep isl_set
*set
)
945 return isl_map_has_rational(set
);
948 /* Is this basic set a parameter domain?
950 int isl_basic_set_is_params(__isl_keep isl_basic_set
*bset
)
954 return isl_space_is_params(bset
->dim
);
957 /* Is this set a parameter domain?
959 isl_bool
isl_set_is_params(__isl_keep isl_set
*set
)
962 return isl_bool_error
;
963 return isl_space_is_params(set
->dim
);
966 /* Is this map actually a parameter domain?
967 * Users should never call this function. Outside of isl,
968 * a map can never be a parameter domain.
970 int isl_map_is_params(__isl_keep isl_map
*map
)
974 return isl_space_is_params(map
->dim
);
977 static struct isl_basic_map
*basic_map_init(struct isl_ctx
*ctx
,
978 struct isl_basic_map
*bmap
, unsigned extra
,
979 unsigned n_eq
, unsigned n_ineq
)
982 size_t row_size
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + extra
;
987 bmap
->block
= isl_blk_alloc(ctx
, (n_ineq
+ n_eq
) * row_size
);
988 if (isl_blk_is_error(bmap
->block
))
991 bmap
->ineq
= isl_alloc_array(ctx
, isl_int
*, n_ineq
+ n_eq
);
992 if ((n_ineq
+ n_eq
) && !bmap
->ineq
)
996 bmap
->block2
= isl_blk_empty();
999 bmap
->block2
= isl_blk_alloc(ctx
, extra
* (1 + row_size
));
1000 if (isl_blk_is_error(bmap
->block2
))
1003 bmap
->div
= isl_alloc_array(ctx
, isl_int
*, extra
);
1008 for (i
= 0; i
< n_ineq
+ n_eq
; ++i
)
1009 bmap
->ineq
[i
] = bmap
->block
.data
+ i
* row_size
;
1011 for (i
= 0; i
< extra
; ++i
)
1012 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + row_size
);
1016 bmap
->c_size
= n_eq
+ n_ineq
;
1017 bmap
->eq
= bmap
->ineq
+ n_ineq
;
1018 bmap
->extra
= extra
;
1022 bmap
->sample
= NULL
;
1026 isl_basic_map_free(bmap
);
1030 struct isl_basic_set
*isl_basic_set_alloc(struct isl_ctx
*ctx
,
1031 unsigned nparam
, unsigned dim
, unsigned extra
,
1032 unsigned n_eq
, unsigned n_ineq
)
1034 struct isl_basic_map
*bmap
;
1037 space
= isl_space_set_alloc(ctx
, nparam
, dim
);
1041 bmap
= isl_basic_map_alloc_space(space
, extra
, n_eq
, n_ineq
);
1042 return bset_from_bmap(bmap
);
1045 struct isl_basic_set
*isl_basic_set_alloc_space(__isl_take isl_space
*dim
,
1046 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
1048 struct isl_basic_map
*bmap
;
1051 isl_assert(dim
->ctx
, dim
->n_in
== 0, goto error
);
1052 bmap
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
1053 return bset_from_bmap(bmap
);
1055 isl_space_free(dim
);
1059 struct isl_basic_map
*isl_basic_map_alloc_space(__isl_take isl_space
*dim
,
1060 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
1062 struct isl_basic_map
*bmap
;
1066 bmap
= isl_calloc_type(dim
->ctx
, struct isl_basic_map
);
1071 return basic_map_init(dim
->ctx
, bmap
, extra
, n_eq
, n_ineq
);
1073 isl_space_free(dim
);
1077 struct isl_basic_map
*isl_basic_map_alloc(struct isl_ctx
*ctx
,
1078 unsigned nparam
, unsigned in
, unsigned out
, unsigned extra
,
1079 unsigned n_eq
, unsigned n_ineq
)
1081 struct isl_basic_map
*bmap
;
1084 dim
= isl_space_alloc(ctx
, nparam
, in
, out
);
1088 bmap
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
1092 static void dup_constraints(
1093 struct isl_basic_map
*dst
, struct isl_basic_map
*src
)
1096 unsigned total
= isl_basic_map_total_dim(src
);
1098 for (i
= 0; i
< src
->n_eq
; ++i
) {
1099 int j
= isl_basic_map_alloc_equality(dst
);
1100 isl_seq_cpy(dst
->eq
[j
], src
->eq
[i
], 1+total
);
1103 for (i
= 0; i
< src
->n_ineq
; ++i
) {
1104 int j
= isl_basic_map_alloc_inequality(dst
);
1105 isl_seq_cpy(dst
->ineq
[j
], src
->ineq
[i
], 1+total
);
1108 for (i
= 0; i
< src
->n_div
; ++i
) {
1109 int j
= isl_basic_map_alloc_div(dst
);
1110 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
);
1112 ISL_F_SET(dst
, ISL_BASIC_SET_FINAL
);
1115 struct isl_basic_map
*isl_basic_map_dup(struct isl_basic_map
*bmap
)
1117 struct isl_basic_map
*dup
;
1121 dup
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
1122 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
1125 dup_constraints(dup
, bmap
);
1126 dup
->flags
= bmap
->flags
;
1127 dup
->sample
= isl_vec_copy(bmap
->sample
);
1131 struct isl_basic_set
*isl_basic_set_dup(struct isl_basic_set
*bset
)
1133 struct isl_basic_map
*dup
;
1135 dup
= isl_basic_map_dup(bset_to_bmap(bset
));
1136 return bset_from_bmap(dup
);
1139 struct isl_basic_set
*isl_basic_set_copy(struct isl_basic_set
*bset
)
1144 if (ISL_F_ISSET(bset
, ISL_BASIC_SET_FINAL
)) {
1148 return isl_basic_set_dup(bset
);
1151 struct isl_set
*isl_set_copy(struct isl_set
*set
)
1160 struct isl_basic_map
*isl_basic_map_copy(struct isl_basic_map
*bmap
)
1165 if (ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
)) {
1169 bmap
= isl_basic_map_dup(bmap
);
1171 ISL_F_SET(bmap
, ISL_BASIC_SET_FINAL
);
1175 struct isl_map
*isl_map_copy(struct isl_map
*map
)
1184 __isl_null isl_basic_map
*isl_basic_map_free(__isl_take isl_basic_map
*bmap
)
1189 if (--bmap
->ref
> 0)
1192 isl_ctx_deref(bmap
->ctx
);
1194 isl_blk_free(bmap
->ctx
, bmap
->block2
);
1196 isl_blk_free(bmap
->ctx
, bmap
->block
);
1197 isl_vec_free(bmap
->sample
);
1198 isl_space_free(bmap
->dim
);
1204 __isl_null isl_basic_set
*isl_basic_set_free(__isl_take isl_basic_set
*bset
)
1206 return isl_basic_map_free(bset_to_bmap(bset
));
1209 static int room_for_con(struct isl_basic_map
*bmap
, unsigned n
)
1211 return bmap
->n_eq
+ bmap
->n_ineq
+ n
<= bmap
->c_size
;
1214 __isl_give isl_map
*isl_map_align_params_map_map_and(
1215 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
,
1216 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*map1
,
1217 __isl_take isl_map
*map2
))
1221 if (isl_space_match(map1
->dim
, isl_dim_param
, map2
->dim
, isl_dim_param
))
1222 return fn(map1
, map2
);
1223 if (!isl_space_has_named_params(map1
->dim
) ||
1224 !isl_space_has_named_params(map2
->dim
))
1225 isl_die(map1
->ctx
, isl_error_invalid
,
1226 "unaligned unnamed parameters", goto error
);
1227 map1
= isl_map_align_params(map1
, isl_map_get_space(map2
));
1228 map2
= isl_map_align_params(map2
, isl_map_get_space(map1
));
1229 return fn(map1
, map2
);
1236 isl_bool
isl_map_align_params_map_map_and_test(__isl_keep isl_map
*map1
,
1237 __isl_keep isl_map
*map2
,
1238 isl_bool (*fn
)(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
))
1243 return isl_bool_error
;
1244 if (isl_space_match(map1
->dim
, isl_dim_param
, map2
->dim
, isl_dim_param
))
1245 return fn(map1
, map2
);
1246 if (!isl_space_has_named_params(map1
->dim
) ||
1247 !isl_space_has_named_params(map2
->dim
))
1248 isl_die(map1
->ctx
, isl_error_invalid
,
1249 "unaligned unnamed parameters", return isl_bool_error
);
1250 map1
= isl_map_copy(map1
);
1251 map2
= isl_map_copy(map2
);
1252 map1
= isl_map_align_params(map1
, isl_map_get_space(map2
));
1253 map2
= isl_map_align_params(map2
, isl_map_get_space(map1
));
1260 int isl_basic_map_alloc_equality(struct isl_basic_map
*bmap
)
1262 struct isl_ctx
*ctx
;
1266 isl_assert(ctx
, room_for_con(bmap
, 1), return -1);
1267 isl_assert(ctx
, (bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
<= bmap
->c_size
,
1269 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1270 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1271 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1272 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1273 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1274 if ((bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
== bmap
->c_size
) {
1276 int j
= isl_basic_map_alloc_inequality(bmap
);
1280 bmap
->ineq
[j
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1281 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
1288 isl_seq_clr(bmap
->eq
[bmap
->n_eq
] + 1 + isl_basic_map_total_dim(bmap
),
1289 bmap
->extra
- bmap
->n_div
);
1290 return bmap
->n_eq
++;
1293 int isl_basic_set_alloc_equality(struct isl_basic_set
*bset
)
1295 return isl_basic_map_alloc_equality(bset_to_bmap(bset
));
1298 int isl_basic_map_free_equality(struct isl_basic_map
*bmap
, unsigned n
)
1302 isl_assert(bmap
->ctx
, n
<= bmap
->n_eq
, return -1);
1307 int isl_basic_set_free_equality(struct isl_basic_set
*bset
, unsigned n
)
1309 return isl_basic_map_free_equality(bset_to_bmap(bset
), n
);
1312 int isl_basic_map_drop_equality(struct isl_basic_map
*bmap
, unsigned pos
)
1317 isl_assert(bmap
->ctx
, pos
< bmap
->n_eq
, return -1);
1319 if (pos
!= bmap
->n_eq
- 1) {
1321 bmap
->eq
[pos
] = bmap
->eq
[bmap
->n_eq
- 1];
1322 bmap
->eq
[bmap
->n_eq
- 1] = t
;
1328 int isl_basic_set_drop_equality(struct isl_basic_set
*bset
, unsigned pos
)
1330 return isl_basic_map_drop_equality(bset_to_bmap(bset
), pos
);
1333 /* Turn inequality "pos" of "bmap" into an equality.
1335 * In particular, we move the inequality in front of the equalities
1336 * and move the last inequality in the position of the moved inequality.
1337 * Note that isl_tab_make_equalities_explicit depends on this particular
1338 * change in the ordering of the constraints.
1340 void isl_basic_map_inequality_to_equality(
1341 struct isl_basic_map
*bmap
, unsigned pos
)
1345 t
= bmap
->ineq
[pos
];
1346 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1347 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
1352 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1353 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1354 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1355 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1358 static int room_for_ineq(struct isl_basic_map
*bmap
, unsigned n
)
1360 return bmap
->n_ineq
+ n
<= bmap
->eq
- bmap
->ineq
;
1363 int isl_basic_map_alloc_inequality(struct isl_basic_map
*bmap
)
1365 struct isl_ctx
*ctx
;
1369 isl_assert(ctx
, room_for_ineq(bmap
, 1), return -1);
1370 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1371 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1372 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1373 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1374 isl_seq_clr(bmap
->ineq
[bmap
->n_ineq
] +
1375 1 + isl_basic_map_total_dim(bmap
),
1376 bmap
->extra
- bmap
->n_div
);
1377 return bmap
->n_ineq
++;
1380 int isl_basic_set_alloc_inequality(struct isl_basic_set
*bset
)
1382 return isl_basic_map_alloc_inequality(bset_to_bmap(bset
));
1385 int isl_basic_map_free_inequality(struct isl_basic_map
*bmap
, unsigned n
)
1389 isl_assert(bmap
->ctx
, n
<= bmap
->n_ineq
, return -1);
1394 int isl_basic_set_free_inequality(struct isl_basic_set
*bset
, unsigned n
)
1396 return isl_basic_map_free_inequality(bset_to_bmap(bset
), n
);
1399 int isl_basic_map_drop_inequality(struct isl_basic_map
*bmap
, unsigned pos
)
1404 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
1406 if (pos
!= bmap
->n_ineq
- 1) {
1407 t
= bmap
->ineq
[pos
];
1408 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1409 bmap
->ineq
[bmap
->n_ineq
- 1] = t
;
1410 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1416 int isl_basic_set_drop_inequality(struct isl_basic_set
*bset
, unsigned pos
)
1418 return isl_basic_map_drop_inequality(bset_to_bmap(bset
), pos
);
1421 __isl_give isl_basic_map
*isl_basic_map_add_eq(__isl_take isl_basic_map
*bmap
,
1426 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
1429 k
= isl_basic_map_alloc_equality(bmap
);
1432 isl_seq_cpy(bmap
->eq
[k
], eq
, 1 + isl_basic_map_total_dim(bmap
));
1435 isl_basic_map_free(bmap
);
1439 __isl_give isl_basic_set
*isl_basic_set_add_eq(__isl_take isl_basic_set
*bset
,
1442 return bset_from_bmap(isl_basic_map_add_eq(bset_to_bmap(bset
), eq
));
1445 __isl_give isl_basic_map
*isl_basic_map_add_ineq(__isl_take isl_basic_map
*bmap
,
1450 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
1453 k
= isl_basic_map_alloc_inequality(bmap
);
1456 isl_seq_cpy(bmap
->ineq
[k
], ineq
, 1 + isl_basic_map_total_dim(bmap
));
1459 isl_basic_map_free(bmap
);
1463 __isl_give isl_basic_set
*isl_basic_set_add_ineq(__isl_take isl_basic_set
*bset
,
1466 return bset_from_bmap(isl_basic_map_add_ineq(bset_to_bmap(bset
), ineq
));
1469 int isl_basic_map_alloc_div(struct isl_basic_map
*bmap
)
1473 isl_assert(bmap
->ctx
, bmap
->n_div
< bmap
->extra
, return -1);
1474 isl_seq_clr(bmap
->div
[bmap
->n_div
] +
1475 1 + 1 + isl_basic_map_total_dim(bmap
),
1476 bmap
->extra
- bmap
->n_div
);
1477 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1478 return bmap
->n_div
++;
1481 int isl_basic_set_alloc_div(struct isl_basic_set
*bset
)
1483 return isl_basic_map_alloc_div(bset_to_bmap(bset
));
1486 /* Insert an extra integer division, prescribed by "div", to "bmap"
1487 * at (integer division) position "pos".
1489 * The integer division is first added at the end and then moved
1490 * into the right position.
1492 __isl_give isl_basic_map
*isl_basic_map_insert_div(
1493 __isl_take isl_basic_map
*bmap
, int pos
, __isl_keep isl_vec
*div
)
1497 bmap
= isl_basic_map_cow(bmap
);
1499 return isl_basic_map_free(bmap
);
1501 if (div
->size
!= 1 + 1 + isl_basic_map_dim(bmap
, isl_dim_all
))
1502 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
1503 "unexpected size", return isl_basic_map_free(bmap
));
1504 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
1505 if (pos
< 0 || pos
> n_div
)
1506 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
1507 "invalid position", return isl_basic_map_free(bmap
));
1509 bmap
= isl_basic_map_extend_space(bmap
,
1510 isl_basic_map_get_space(bmap
), 1, 0, 2);
1511 k
= isl_basic_map_alloc_div(bmap
);
1513 return isl_basic_map_free(bmap
);
1514 isl_seq_cpy(bmap
->div
[k
], div
->el
, div
->size
);
1515 isl_int_set_si(bmap
->div
[k
][div
->size
], 0);
1517 for (i
= k
; i
> pos
; --i
)
1518 isl_basic_map_swap_div(bmap
, i
, i
- 1);
1523 int isl_basic_map_free_div(struct isl_basic_map
*bmap
, unsigned n
)
1527 isl_assert(bmap
->ctx
, n
<= bmap
->n_div
, return -1);
1532 int isl_basic_set_free_div(struct isl_basic_set
*bset
, unsigned n
)
1534 return isl_basic_map_free_div(bset_to_bmap(bset
), n
);
1537 /* Copy constraint from src to dst, putting the vars of src at offset
1538 * dim_off in dst and the divs of src at offset div_off in dst.
1539 * If both sets are actually map, then dim_off applies to the input
1542 static void copy_constraint(struct isl_basic_map
*dst_map
, isl_int
*dst
,
1543 struct isl_basic_map
*src_map
, isl_int
*src
,
1544 unsigned in_off
, unsigned out_off
, unsigned div_off
)
1546 unsigned src_nparam
= isl_basic_map_n_param(src_map
);
1547 unsigned dst_nparam
= isl_basic_map_n_param(dst_map
);
1548 unsigned src_in
= isl_basic_map_n_in(src_map
);
1549 unsigned dst_in
= isl_basic_map_n_in(dst_map
);
1550 unsigned src_out
= isl_basic_map_n_out(src_map
);
1551 unsigned dst_out
= isl_basic_map_n_out(dst_map
);
1552 isl_int_set(dst
[0], src
[0]);
1553 isl_seq_cpy(dst
+1, src
+1, isl_min(dst_nparam
, src_nparam
));
1554 if (dst_nparam
> src_nparam
)
1555 isl_seq_clr(dst
+1+src_nparam
,
1556 dst_nparam
- src_nparam
);
1557 isl_seq_clr(dst
+1+dst_nparam
, in_off
);
1558 isl_seq_cpy(dst
+1+dst_nparam
+in_off
,
1560 isl_min(dst_in
-in_off
, src_in
));
1561 if (dst_in
-in_off
> src_in
)
1562 isl_seq_clr(dst
+1+dst_nparam
+in_off
+src_in
,
1563 dst_in
- in_off
- src_in
);
1564 isl_seq_clr(dst
+1+dst_nparam
+dst_in
, out_off
);
1565 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+out_off
,
1566 src
+1+src_nparam
+src_in
,
1567 isl_min(dst_out
-out_off
, src_out
));
1568 if (dst_out
-out_off
> src_out
)
1569 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+out_off
+src_out
,
1570 dst_out
- out_off
- src_out
);
1571 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
, div_off
);
1572 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+dst_out
+div_off
,
1573 src
+1+src_nparam
+src_in
+src_out
,
1574 isl_min(dst_map
->extra
-div_off
, src_map
->n_div
));
1575 if (dst_map
->n_div
-div_off
> src_map
->n_div
)
1576 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
+
1577 div_off
+src_map
->n_div
,
1578 dst_map
->n_div
- div_off
- src_map
->n_div
);
1581 static void copy_div(struct isl_basic_map
*dst_map
, isl_int
*dst
,
1582 struct isl_basic_map
*src_map
, isl_int
*src
,
1583 unsigned in_off
, unsigned out_off
, unsigned div_off
)
1585 isl_int_set(dst
[0], src
[0]);
1586 copy_constraint(dst_map
, dst
+1, src_map
, src
+1, in_off
, out_off
, div_off
);
1589 static struct isl_basic_map
*add_constraints(struct isl_basic_map
*bmap1
,
1590 struct isl_basic_map
*bmap2
, unsigned i_pos
, unsigned o_pos
)
1595 if (!bmap1
|| !bmap2
)
1598 div_off
= bmap1
->n_div
;
1600 for (i
= 0; i
< bmap2
->n_eq
; ++i
) {
1601 int i1
= isl_basic_map_alloc_equality(bmap1
);
1604 copy_constraint(bmap1
, bmap1
->eq
[i1
], bmap2
, bmap2
->eq
[i
],
1605 i_pos
, o_pos
, div_off
);
1608 for (i
= 0; i
< bmap2
->n_ineq
; ++i
) {
1609 int i1
= isl_basic_map_alloc_inequality(bmap1
);
1612 copy_constraint(bmap1
, bmap1
->ineq
[i1
], bmap2
, bmap2
->ineq
[i
],
1613 i_pos
, o_pos
, div_off
);
1616 for (i
= 0; i
< bmap2
->n_div
; ++i
) {
1617 int i1
= isl_basic_map_alloc_div(bmap1
);
1620 copy_div(bmap1
, bmap1
->div
[i1
], bmap2
, bmap2
->div
[i
],
1621 i_pos
, o_pos
, div_off
);
1624 isl_basic_map_free(bmap2
);
1629 isl_basic_map_free(bmap1
);
1630 isl_basic_map_free(bmap2
);
1634 struct isl_basic_set
*isl_basic_set_add_constraints(struct isl_basic_set
*bset1
,
1635 struct isl_basic_set
*bset2
, unsigned pos
)
1637 return bset_from_bmap(add_constraints(bset_to_bmap(bset1
),
1638 bset_to_bmap(bset2
), 0, pos
));
1641 struct isl_basic_map
*isl_basic_map_extend_space(struct isl_basic_map
*base
,
1642 __isl_take isl_space
*dim
, unsigned extra
,
1643 unsigned n_eq
, unsigned n_ineq
)
1645 struct isl_basic_map
*ext
;
1655 dims_ok
= isl_space_is_equal(base
->dim
, dim
) &&
1656 base
->extra
>= base
->n_div
+ extra
;
1658 if (dims_ok
&& room_for_con(base
, n_eq
+ n_ineq
) &&
1659 room_for_ineq(base
, n_ineq
)) {
1660 isl_space_free(dim
);
1664 isl_assert(base
->ctx
, base
->dim
->nparam
<= dim
->nparam
, goto error
);
1665 isl_assert(base
->ctx
, base
->dim
->n_in
<= dim
->n_in
, goto error
);
1666 isl_assert(base
->ctx
, base
->dim
->n_out
<= dim
->n_out
, goto error
);
1667 extra
+= base
->extra
;
1669 n_ineq
+= base
->n_ineq
;
1671 ext
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
1677 ext
->sample
= isl_vec_copy(base
->sample
);
1678 flags
= base
->flags
;
1679 ext
= add_constraints(ext
, base
, 0, 0);
1682 ISL_F_CLR(ext
, ISL_BASIC_SET_FINAL
);
1688 isl_space_free(dim
);
1689 isl_basic_map_free(base
);
1693 struct isl_basic_set
*isl_basic_set_extend_space(struct isl_basic_set
*base
,
1694 __isl_take isl_space
*dim
, unsigned extra
,
1695 unsigned n_eq
, unsigned n_ineq
)
1697 return bset_from_bmap(isl_basic_map_extend_space(bset_to_bmap(base
),
1698 dim
, extra
, n_eq
, n_ineq
));
1701 struct isl_basic_map
*isl_basic_map_extend_constraints(
1702 struct isl_basic_map
*base
, unsigned n_eq
, unsigned n_ineq
)
1706 return isl_basic_map_extend_space(base
, isl_space_copy(base
->dim
),
1710 struct isl_basic_map
*isl_basic_map_extend(struct isl_basic_map
*base
,
1711 unsigned nparam
, unsigned n_in
, unsigned n_out
, unsigned extra
,
1712 unsigned n_eq
, unsigned n_ineq
)
1714 struct isl_basic_map
*bmap
;
1719 dim
= isl_space_alloc(base
->ctx
, nparam
, n_in
, n_out
);
1723 bmap
= isl_basic_map_extend_space(base
, dim
, extra
, n_eq
, n_ineq
);
1726 isl_basic_map_free(base
);
1730 struct isl_basic_set
*isl_basic_set_extend(struct isl_basic_set
*base
,
1731 unsigned nparam
, unsigned dim
, unsigned extra
,
1732 unsigned n_eq
, unsigned n_ineq
)
1734 return bset_from_bmap(isl_basic_map_extend(bset_to_bmap(base
),
1735 nparam
, 0, dim
, extra
, n_eq
, n_ineq
));
1738 struct isl_basic_set
*isl_basic_set_extend_constraints(
1739 struct isl_basic_set
*base
, unsigned n_eq
, unsigned n_ineq
)
1741 isl_basic_map
*bmap
= bset_to_bmap(base
);
1742 bmap
= isl_basic_map_extend_constraints(bmap
, n_eq
, n_ineq
);
1743 return bset_from_bmap(bmap
);
1746 struct isl_basic_set
*isl_basic_set_cow(struct isl_basic_set
*bset
)
1748 return bset_from_bmap(isl_basic_map_cow(bset_to_bmap(bset
)));
1751 struct isl_basic_map
*isl_basic_map_cow(struct isl_basic_map
*bmap
)
1756 if (bmap
->ref
> 1) {
1758 bmap
= isl_basic_map_dup(bmap
);
1761 ISL_F_CLR(bmap
, ISL_BASIC_SET_FINAL
);
1762 ISL_F_CLR(bmap
, ISL_BASIC_MAP_REDUCED_COEFFICIENTS
);
1767 /* Clear all cached information in "map", either because it is about
1768 * to be modified or because it is being freed.
1769 * Always return the same pointer that is passed in.
1770 * This is needed for the use in isl_map_free.
1772 static __isl_give isl_map
*clear_caches(__isl_take isl_map
*map
)
1774 isl_basic_map_free(map
->cached_simple_hull
[0]);
1775 isl_basic_map_free(map
->cached_simple_hull
[1]);
1776 map
->cached_simple_hull
[0] = NULL
;
1777 map
->cached_simple_hull
[1] = NULL
;
1781 struct isl_set
*isl_set_cow(struct isl_set
*set
)
1783 return isl_map_cow(set
);
1786 /* Return an isl_map that is equal to "map" and that has only
1787 * a single reference.
1789 * If the original input already has only one reference, then
1790 * simply return it, but clear all cached information, since
1791 * it may be rendered invalid by the operations that will be
1792 * performed on the result.
1794 * Otherwise, create a duplicate (without any cached information).
1796 struct isl_map
*isl_map_cow(struct isl_map
*map
)
1802 return clear_caches(map
);
1804 return isl_map_dup(map
);
1807 static void swap_vars(struct isl_blk blk
, isl_int
*a
,
1808 unsigned a_len
, unsigned b_len
)
1810 isl_seq_cpy(blk
.data
, a
+a_len
, b_len
);
1811 isl_seq_cpy(blk
.data
+b_len
, a
, a_len
);
1812 isl_seq_cpy(a
, blk
.data
, b_len
+a_len
);
1815 static __isl_give isl_basic_map
*isl_basic_map_swap_vars(
1816 __isl_take isl_basic_map
*bmap
, unsigned pos
, unsigned n1
, unsigned n2
)
1824 isl_assert(bmap
->ctx
,
1825 pos
+ n1
+ n2
<= 1 + isl_basic_map_total_dim(bmap
), goto error
);
1827 if (n1
== 0 || n2
== 0)
1830 bmap
= isl_basic_map_cow(bmap
);
1834 blk
= isl_blk_alloc(bmap
->ctx
, n1
+ n2
);
1835 if (isl_blk_is_error(blk
))
1838 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1840 bmap
->eq
[i
] + pos
, n1
, n2
);
1842 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1844 bmap
->ineq
[i
] + pos
, n1
, n2
);
1846 for (i
= 0; i
< bmap
->n_div
; ++i
)
1848 bmap
->div
[i
]+1 + pos
, n1
, n2
);
1850 isl_blk_free(bmap
->ctx
, blk
);
1852 ISL_F_CLR(bmap
, ISL_BASIC_SET_NORMALIZED
);
1853 bmap
= isl_basic_map_gauss(bmap
, NULL
);
1854 return isl_basic_map_finalize(bmap
);
1856 isl_basic_map_free(bmap
);
1860 struct isl_basic_map
*isl_basic_map_set_to_empty(struct isl_basic_map
*bmap
)
1866 total
= isl_basic_map_total_dim(bmap
);
1867 isl_basic_map_free_div(bmap
, bmap
->n_div
);
1868 isl_basic_map_free_inequality(bmap
, bmap
->n_ineq
);
1870 isl_basic_map_free_equality(bmap
, bmap
->n_eq
-1);
1872 i
= isl_basic_map_alloc_equality(bmap
);
1876 isl_int_set_si(bmap
->eq
[i
][0], 1);
1877 isl_seq_clr(bmap
->eq
[i
]+1, total
);
1878 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
1879 isl_vec_free(bmap
->sample
);
1880 bmap
->sample
= NULL
;
1881 return isl_basic_map_finalize(bmap
);
1883 isl_basic_map_free(bmap
);
1887 struct isl_basic_set
*isl_basic_set_set_to_empty(struct isl_basic_set
*bset
)
1889 return bset_from_bmap(isl_basic_map_set_to_empty(bset_to_bmap(bset
)));
1892 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
1895 static void swap_div(__isl_keep isl_basic_map
*bmap
, int a
, int b
)
1897 isl_int
*t
= bmap
->div
[a
];
1898 bmap
->div
[a
] = bmap
->div
[b
];
1902 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
1903 * div definitions accordingly.
1905 void isl_basic_map_swap_div(struct isl_basic_map
*bmap
, int a
, int b
)
1908 unsigned off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
1910 swap_div(bmap
, a
, b
);
1912 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1913 isl_int_swap(bmap
->eq
[i
][1+off
+a
], bmap
->eq
[i
][1+off
+b
]);
1915 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1916 isl_int_swap(bmap
->ineq
[i
][1+off
+a
], bmap
->ineq
[i
][1+off
+b
]);
1918 for (i
= 0; i
< bmap
->n_div
; ++i
)
1919 isl_int_swap(bmap
->div
[i
][1+1+off
+a
], bmap
->div
[i
][1+1+off
+b
]);
1920 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1923 /* Swap divs "a" and "b" in "bset" and adjust the constraints and
1924 * div definitions accordingly.
1926 void isl_basic_set_swap_div(__isl_keep isl_basic_set
*bset
, int a
, int b
)
1928 isl_basic_map_swap_div(bset
, a
, b
);
1931 /* Eliminate the specified n dimensions starting at first from the
1932 * constraints, without removing the dimensions from the space.
1933 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1935 __isl_give isl_map
*isl_map_eliminate(__isl_take isl_map
*map
,
1936 enum isl_dim_type type
, unsigned first
, unsigned n
)
1945 if (first
+ n
> isl_map_dim(map
, type
) || first
+ n
< first
)
1946 isl_die(map
->ctx
, isl_error_invalid
,
1947 "index out of bounds", goto error
);
1949 map
= isl_map_cow(map
);
1953 for (i
= 0; i
< map
->n
; ++i
) {
1954 map
->p
[i
] = isl_basic_map_eliminate(map
->p
[i
], type
, first
, n
);
1964 /* Eliminate the specified n dimensions starting at first from the
1965 * constraints, without removing the dimensions from the space.
1966 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1968 __isl_give isl_set
*isl_set_eliminate(__isl_take isl_set
*set
,
1969 enum isl_dim_type type
, unsigned first
, unsigned n
)
1971 return set_from_map(isl_map_eliminate(set_to_map(set
), type
, first
, n
));
1974 /* Eliminate the specified n dimensions starting at first from the
1975 * constraints, without removing the dimensions from the space.
1976 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1978 __isl_give isl_set
*isl_set_eliminate_dims(__isl_take isl_set
*set
,
1979 unsigned first
, unsigned n
)
1981 return isl_set_eliminate(set
, isl_dim_set
, first
, n
);
1984 __isl_give isl_basic_map
*isl_basic_map_remove_divs(
1985 __isl_take isl_basic_map
*bmap
)
1989 bmap
= isl_basic_map_eliminate_vars(bmap
,
1990 isl_space_dim(bmap
->dim
, isl_dim_all
), bmap
->n_div
);
1994 return isl_basic_map_finalize(bmap
);
1997 __isl_give isl_basic_set
*isl_basic_set_remove_divs(
1998 __isl_take isl_basic_set
*bset
)
2000 return bset_from_bmap(isl_basic_map_remove_divs(bset_to_bmap(bset
)));
2003 __isl_give isl_map
*isl_map_remove_divs(__isl_take isl_map
*map
)
2012 map
= isl_map_cow(map
);
2016 for (i
= 0; i
< map
->n
; ++i
) {
2017 map
->p
[i
] = isl_basic_map_remove_divs(map
->p
[i
]);
2027 __isl_give isl_set
*isl_set_remove_divs(__isl_take isl_set
*set
)
2029 return isl_map_remove_divs(set
);
2032 struct isl_basic_map
*isl_basic_map_remove_dims(struct isl_basic_map
*bmap
,
2033 enum isl_dim_type type
, unsigned first
, unsigned n
)
2037 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
2039 if (n
== 0 && !isl_space_is_named_or_nested(bmap
->dim
, type
))
2041 bmap
= isl_basic_map_eliminate_vars(bmap
,
2042 isl_basic_map_offset(bmap
, type
) - 1 + first
, n
);
2045 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
) && type
== isl_dim_div
)
2047 bmap
= isl_basic_map_drop(bmap
, type
, first
, n
);
2050 isl_basic_map_free(bmap
);
2054 /* Return true if the definition of the given div (recursively) involves
2055 * any of the given variables.
2057 static int div_involves_vars(__isl_keep isl_basic_map
*bmap
, int div
,
2058 unsigned first
, unsigned n
)
2061 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
2063 if (isl_int_is_zero(bmap
->div
[div
][0]))
2065 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + first
, n
) >= 0)
2068 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2069 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
2071 if (div_involves_vars(bmap
, i
, first
, n
))
2078 /* Try and add a lower and/or upper bound on "div" to "bmap"
2079 * based on inequality "i".
2080 * "total" is the total number of variables (excluding the divs).
2081 * "v" is a temporary object that can be used during the calculations.
2082 * If "lb" is set, then a lower bound should be constructed.
2083 * If "ub" is set, then an upper bound should be constructed.
2085 * The calling function has already checked that the inequality does not
2086 * reference "div", but we still need to check that the inequality is
2087 * of the right form. We'll consider the case where we want to construct
2088 * a lower bound. The construction of upper bounds is similar.
2090 * Let "div" be of the form
2092 * q = floor((a + f(x))/d)
2094 * We essentially check if constraint "i" is of the form
2098 * so that we can use it to derive a lower bound on "div".
2099 * However, we allow a slightly more general form
2103 * with the condition that the coefficients of g(x) - f(x) are all
2105 * Rewriting this constraint as
2109 * adding a + f(x) to both sides and dividing by d, we obtain
2111 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
2113 * Taking the floor on both sides, we obtain
2115 * q >= floor((a-b)/d) + (f(x)-g(x))/d
2119 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
2121 * In the case of an upper bound, we construct the constraint
2123 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
2126 static __isl_give isl_basic_map
*insert_bounds_on_div_from_ineq(
2127 __isl_take isl_basic_map
*bmap
, int div
, int i
,
2128 unsigned total
, isl_int v
, int lb
, int ub
)
2132 for (j
= 0; (lb
|| ub
) && j
< total
+ bmap
->n_div
; ++j
) {
2134 isl_int_sub(v
, bmap
->ineq
[i
][1 + j
],
2135 bmap
->div
[div
][1 + 1 + j
]);
2136 lb
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
2139 isl_int_add(v
, bmap
->ineq
[i
][1 + j
],
2140 bmap
->div
[div
][1 + 1 + j
]);
2141 ub
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
2147 bmap
= isl_basic_map_cow(bmap
);
2148 bmap
= isl_basic_map_extend_constraints(bmap
, 0, lb
+ ub
);
2150 int k
= isl_basic_map_alloc_inequality(bmap
);
2153 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
2154 isl_int_sub(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
2155 bmap
->div
[div
][1 + j
]);
2156 isl_int_cdiv_q(bmap
->ineq
[k
][j
],
2157 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
2159 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], 1);
2162 int k
= isl_basic_map_alloc_inequality(bmap
);
2165 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
2166 isl_int_add(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
2167 bmap
->div
[div
][1 + j
]);
2168 isl_int_fdiv_q(bmap
->ineq
[k
][j
],
2169 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
2171 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], -1);
2174 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
2177 isl_basic_map_free(bmap
);
2181 /* This function is called right before "div" is eliminated from "bmap"
2182 * using Fourier-Motzkin.
2183 * Look through the constraints of "bmap" for constraints on the argument
2184 * of the integer division and use them to construct constraints on the
2185 * integer division itself. These constraints can then be combined
2186 * during the Fourier-Motzkin elimination.
2187 * Note that it is only useful to introduce lower bounds on "div"
2188 * if "bmap" already contains upper bounds on "div" as the newly
2189 * introduce lower bounds can then be combined with the pre-existing
2190 * upper bounds. Similarly for upper bounds.
2191 * We therefore first check if "bmap" contains any lower and/or upper bounds
2194 * It is interesting to note that the introduction of these constraints
2195 * can indeed lead to more accurate results, even when compared to
2196 * deriving constraints on the argument of "div" from constraints on "div".
2197 * Consider, for example, the set
2199 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2201 * The second constraint can be rewritten as
2203 * 2 * [(-i-2j+3)/4] + k >= 0
2205 * from which we can derive
2207 * -i - 2j + 3 >= -2k
2213 * Combined with the first constraint, we obtain
2215 * -3 <= 3 + 2k or k >= -3
2217 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2218 * the first constraint, we obtain
2220 * [(i + 2j)/4] >= [-3/4] = -1
2222 * Combining this constraint with the second constraint, we obtain
2226 static __isl_give isl_basic_map
*insert_bounds_on_div(
2227 __isl_take isl_basic_map
*bmap
, int div
)
2230 int check_lb
, check_ub
;
2237 if (isl_int_is_zero(bmap
->div
[div
][0]))
2240 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
2244 for (i
= 0; (!check_lb
|| !check_ub
) && i
< bmap
->n_ineq
; ++i
) {
2245 int s
= isl_int_sgn(bmap
->ineq
[i
][1 + total
+ div
]);
2252 if (!check_lb
&& !check_ub
)
2257 for (i
= 0; bmap
&& i
< bmap
->n_ineq
; ++i
) {
2258 if (!isl_int_is_zero(bmap
->ineq
[i
][1 + total
+ div
]))
2261 bmap
= insert_bounds_on_div_from_ineq(bmap
, div
, i
, total
, v
,
2262 check_lb
, check_ub
);
2270 /* Remove all divs (recursively) involving any of the given dimensions
2271 * in their definitions.
2273 __isl_give isl_basic_map
*isl_basic_map_remove_divs_involving_dims(
2274 __isl_take isl_basic_map
*bmap
,
2275 enum isl_dim_type type
, unsigned first
, unsigned n
)
2281 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
2283 first
+= isl_basic_map_offset(bmap
, type
);
2285 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2286 if (!div_involves_vars(bmap
, i
, first
, n
))
2288 bmap
= insert_bounds_on_div(bmap
, i
);
2289 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
2297 isl_basic_map_free(bmap
);
2301 __isl_give isl_basic_set
*isl_basic_set_remove_divs_involving_dims(
2302 __isl_take isl_basic_set
*bset
,
2303 enum isl_dim_type type
, unsigned first
, unsigned n
)
2305 return isl_basic_map_remove_divs_involving_dims(bset
, type
, first
, n
);
2308 __isl_give isl_map
*isl_map_remove_divs_involving_dims(__isl_take isl_map
*map
,
2309 enum isl_dim_type type
, unsigned first
, unsigned n
)
2318 map
= isl_map_cow(map
);
2322 for (i
= 0; i
< map
->n
; ++i
) {
2323 map
->p
[i
] = isl_basic_map_remove_divs_involving_dims(map
->p
[i
],
2334 __isl_give isl_set
*isl_set_remove_divs_involving_dims(__isl_take isl_set
*set
,
2335 enum isl_dim_type type
, unsigned first
, unsigned n
)
2337 return set_from_map(isl_map_remove_divs_involving_dims(set_to_map(set
),
2341 /* Does the description of "bmap" depend on the specified dimensions?
2342 * We also check whether the dimensions appear in any of the div definitions.
2343 * In principle there is no need for this check. If the dimensions appear
2344 * in a div definition, they also appear in the defining constraints of that
2347 isl_bool
isl_basic_map_involves_dims(__isl_keep isl_basic_map
*bmap
,
2348 enum isl_dim_type type
, unsigned first
, unsigned n
)
2353 return isl_bool_error
;
2355 if (first
+ n
> isl_basic_map_dim(bmap
, type
))
2356 isl_die(bmap
->ctx
, isl_error_invalid
,
2357 "index out of bounds", return isl_bool_error
);
2359 first
+= isl_basic_map_offset(bmap
, type
);
2360 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2361 if (isl_seq_first_non_zero(bmap
->eq
[i
] + first
, n
) >= 0)
2362 return isl_bool_true
;
2363 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2364 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + first
, n
) >= 0)
2365 return isl_bool_true
;
2366 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2367 if (isl_int_is_zero(bmap
->div
[i
][0]))
2369 if (isl_seq_first_non_zero(bmap
->div
[i
] + 1 + first
, n
) >= 0)
2370 return isl_bool_true
;
2373 return isl_bool_false
;
2376 isl_bool
isl_map_involves_dims(__isl_keep isl_map
*map
,
2377 enum isl_dim_type type
, unsigned first
, unsigned n
)
2382 return isl_bool_error
;
2384 if (first
+ n
> isl_map_dim(map
, type
))
2385 isl_die(map
->ctx
, isl_error_invalid
,
2386 "index out of bounds", return isl_bool_error
);
2388 for (i
= 0; i
< map
->n
; ++i
) {
2389 isl_bool involves
= isl_basic_map_involves_dims(map
->p
[i
],
2391 if (involves
< 0 || involves
)
2395 return isl_bool_false
;
2398 isl_bool
isl_basic_set_involves_dims(__isl_keep isl_basic_set
*bset
,
2399 enum isl_dim_type type
, unsigned first
, unsigned n
)
2401 return isl_basic_map_involves_dims(bset
, type
, first
, n
);
2404 isl_bool
isl_set_involves_dims(__isl_keep isl_set
*set
,
2405 enum isl_dim_type type
, unsigned first
, unsigned n
)
2407 return isl_map_involves_dims(set
, type
, first
, n
);
2410 /* Does local variable "div" of "bmap" have a complete explicit representation?
2411 * Having a complete explicit representation requires not only
2412 * an explicit representation, but also that all local variables
2413 * that appear in this explicit representation in turn have
2414 * a complete explicit representation.
2416 isl_bool
isl_basic_map_div_is_known(__isl_keep isl_basic_map
*bmap
, int div
)
2419 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
2422 marked
= isl_basic_map_div_is_marked_unknown(bmap
, div
);
2423 if (marked
< 0 || marked
)
2424 return isl_bool_not(marked
);
2426 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2429 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
2431 known
= isl_basic_map_div_is_known(bmap
, i
);
2432 if (known
< 0 || !known
)
2436 return isl_bool_true
;
2439 /* Does local variable "div" of "bset" have a complete explicit representation?
2441 isl_bool
isl_basic_set_div_is_known(__isl_keep isl_basic_set
*bset
, int div
)
2443 return isl_basic_map_div_is_known(bset
, div
);
2446 /* Remove all divs that are unknown or defined in terms of unknown divs.
2448 __isl_give isl_basic_map
*isl_basic_map_remove_unknown_divs(
2449 __isl_take isl_basic_map
*bmap
)
2456 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2457 if (isl_basic_map_div_is_known(bmap
, i
))
2459 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
2468 /* Remove all divs that are unknown or defined in terms of unknown divs.
2470 __isl_give isl_basic_set
*isl_basic_set_remove_unknown_divs(
2471 __isl_take isl_basic_set
*bset
)
2473 return isl_basic_map_remove_unknown_divs(bset
);
2476 __isl_give isl_map
*isl_map_remove_unknown_divs(__isl_take isl_map
*map
)
2485 map
= isl_map_cow(map
);
2489 for (i
= 0; i
< map
->n
; ++i
) {
2490 map
->p
[i
] = isl_basic_map_remove_unknown_divs(map
->p
[i
]);
2500 __isl_give isl_set
*isl_set_remove_unknown_divs(__isl_take isl_set
*set
)
2502 return set_from_map(isl_map_remove_unknown_divs(set_to_map(set
)));
2505 __isl_give isl_basic_set
*isl_basic_set_remove_dims(
2506 __isl_take isl_basic_set
*bset
,
2507 enum isl_dim_type type
, unsigned first
, unsigned n
)
2509 isl_basic_map
*bmap
= bset_to_bmap(bset
);
2510 bmap
= isl_basic_map_remove_dims(bmap
, type
, first
, n
);
2511 return bset_from_bmap(bmap
);
2514 struct isl_map
*isl_map_remove_dims(struct isl_map
*map
,
2515 enum isl_dim_type type
, unsigned first
, unsigned n
)
2522 map
= isl_map_cow(map
);
2525 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
2527 for (i
= 0; i
< map
->n
; ++i
) {
2528 map
->p
[i
] = isl_basic_map_eliminate_vars(map
->p
[i
],
2529 isl_basic_map_offset(map
->p
[i
], type
) - 1 + first
, n
);
2533 map
= isl_map_drop(map
, type
, first
, n
);
2540 __isl_give isl_set
*isl_set_remove_dims(__isl_take isl_set
*bset
,
2541 enum isl_dim_type type
, unsigned first
, unsigned n
)
2543 return set_from_map(isl_map_remove_dims(set_to_map(bset
),
2547 /* Project out n inputs starting at first using Fourier-Motzkin */
2548 struct isl_map
*isl_map_remove_inputs(struct isl_map
*map
,
2549 unsigned first
, unsigned n
)
2551 return isl_map_remove_dims(map
, isl_dim_in
, first
, n
);
2554 static void dump_term(struct isl_basic_map
*bmap
,
2555 isl_int c
, int pos
, FILE *out
)
2558 unsigned in
= isl_basic_map_n_in(bmap
);
2559 unsigned dim
= in
+ isl_basic_map_n_out(bmap
);
2560 unsigned nparam
= isl_basic_map_n_param(bmap
);
2562 isl_int_print(out
, c
, 0);
2564 if (!isl_int_is_one(c
))
2565 isl_int_print(out
, c
, 0);
2566 if (pos
< 1 + nparam
) {
2567 name
= isl_space_get_dim_name(bmap
->dim
,
2568 isl_dim_param
, pos
- 1);
2570 fprintf(out
, "%s", name
);
2572 fprintf(out
, "p%d", pos
- 1);
2573 } else if (pos
< 1 + nparam
+ in
)
2574 fprintf(out
, "i%d", pos
- 1 - nparam
);
2575 else if (pos
< 1 + nparam
+ dim
)
2576 fprintf(out
, "o%d", pos
- 1 - nparam
- in
);
2578 fprintf(out
, "e%d", pos
- 1 - nparam
- dim
);
2582 static void dump_constraint_sign(struct isl_basic_map
*bmap
, isl_int
*c
,
2583 int sign
, FILE *out
)
2587 unsigned len
= 1 + isl_basic_map_total_dim(bmap
);
2591 for (i
= 0, first
= 1; i
< len
; ++i
) {
2592 if (isl_int_sgn(c
[i
]) * sign
<= 0)
2595 fprintf(out
, " + ");
2597 isl_int_abs(v
, c
[i
]);
2598 dump_term(bmap
, v
, i
, out
);
2605 static void dump_constraint(struct isl_basic_map
*bmap
, isl_int
*c
,
2606 const char *op
, FILE *out
, int indent
)
2610 fprintf(out
, "%*s", indent
, "");
2612 dump_constraint_sign(bmap
, c
, 1, out
);
2613 fprintf(out
, " %s ", op
);
2614 dump_constraint_sign(bmap
, c
, -1, out
);
2618 for (i
= bmap
->n_div
; i
< bmap
->extra
; ++i
) {
2619 if (isl_int_is_zero(c
[1+isl_space_dim(bmap
->dim
, isl_dim_all
)+i
]))
2621 fprintf(out
, "%*s", indent
, "");
2622 fprintf(out
, "ERROR: unused div coefficient not zero\n");
2627 static void dump_constraints(struct isl_basic_map
*bmap
,
2628 isl_int
**c
, unsigned n
,
2629 const char *op
, FILE *out
, int indent
)
2633 for (i
= 0; i
< n
; ++i
)
2634 dump_constraint(bmap
, c
[i
], op
, out
, indent
);
2637 static void dump_affine(struct isl_basic_map
*bmap
, isl_int
*exp
, FILE *out
)
2641 unsigned total
= isl_basic_map_total_dim(bmap
);
2643 for (j
= 0; j
< 1 + total
; ++j
) {
2644 if (isl_int_is_zero(exp
[j
]))
2646 if (!first
&& isl_int_is_pos(exp
[j
]))
2648 dump_term(bmap
, exp
[j
], j
, out
);
2653 static void dump(struct isl_basic_map
*bmap
, FILE *out
, int indent
)
2657 dump_constraints(bmap
, bmap
->eq
, bmap
->n_eq
, "=", out
, indent
);
2658 dump_constraints(bmap
, bmap
->ineq
, bmap
->n_ineq
, ">=", out
, indent
);
2660 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2661 fprintf(out
, "%*s", indent
, "");
2662 fprintf(out
, "e%d = [(", i
);
2663 dump_affine(bmap
, bmap
->div
[i
]+1, out
);
2665 isl_int_print(out
, bmap
->div
[i
][0], 0);
2666 fprintf(out
, "]\n");
2670 void isl_basic_set_print_internal(struct isl_basic_set
*bset
,
2671 FILE *out
, int indent
)
2674 fprintf(out
, "null basic set\n");
2678 fprintf(out
, "%*s", indent
, "");
2679 fprintf(out
, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
2680 bset
->ref
, bset
->dim
->nparam
, bset
->dim
->n_out
,
2681 bset
->extra
, bset
->flags
);
2682 dump(bset_to_bmap(bset
), out
, indent
);
2685 void isl_basic_map_print_internal(struct isl_basic_map
*bmap
,
2686 FILE *out
, int indent
)
2689 fprintf(out
, "null basic map\n");
2693 fprintf(out
, "%*s", indent
, "");
2694 fprintf(out
, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
2695 "flags: %x, n_name: %d\n",
2697 bmap
->dim
->nparam
, bmap
->dim
->n_in
, bmap
->dim
->n_out
,
2698 bmap
->extra
, bmap
->flags
, bmap
->dim
->n_id
);
2699 dump(bmap
, out
, indent
);
2702 int isl_inequality_negate(struct isl_basic_map
*bmap
, unsigned pos
)
2707 total
= isl_basic_map_total_dim(bmap
);
2708 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
2709 isl_seq_neg(bmap
->ineq
[pos
], bmap
->ineq
[pos
], 1 + total
);
2710 isl_int_sub_ui(bmap
->ineq
[pos
][0], bmap
->ineq
[pos
][0], 1);
2711 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
2715 __isl_give isl_set
*isl_set_alloc_space(__isl_take isl_space
*space
, int n
,
2720 if (isl_space_dim(space
, isl_dim_in
) != 0)
2721 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
2722 "set cannot have input dimensions", goto error
);
2723 return isl_map_alloc_space(space
, n
, flags
);
2725 isl_space_free(space
);
2729 struct isl_set
*isl_set_alloc(struct isl_ctx
*ctx
,
2730 unsigned nparam
, unsigned dim
, int n
, unsigned flags
)
2732 struct isl_set
*set
;
2735 dims
= isl_space_alloc(ctx
, nparam
, 0, dim
);
2739 set
= isl_set_alloc_space(dims
, n
, flags
);
2743 /* Make sure "map" has room for at least "n" more basic maps.
2745 struct isl_map
*isl_map_grow(struct isl_map
*map
, int n
)
2748 struct isl_map
*grown
= NULL
;
2752 isl_assert(map
->ctx
, n
>= 0, goto error
);
2753 if (map
->n
+ n
<= map
->size
)
2755 grown
= isl_map_alloc_space(isl_map_get_space(map
), map
->n
+ n
, map
->flags
);
2758 for (i
= 0; i
< map
->n
; ++i
) {
2759 grown
->p
[i
] = isl_basic_map_copy(map
->p
[i
]);
2767 isl_map_free(grown
);
2772 /* Make sure "set" has room for at least "n" more basic sets.
2774 struct isl_set
*isl_set_grow(struct isl_set
*set
, int n
)
2776 return set_from_map(isl_map_grow(set_to_map(set
), n
));
2779 struct isl_set
*isl_set_dup(struct isl_set
*set
)
2782 struct isl_set
*dup
;
2787 dup
= isl_set_alloc_space(isl_space_copy(set
->dim
), set
->n
, set
->flags
);
2790 for (i
= 0; i
< set
->n
; ++i
)
2791 dup
= isl_set_add_basic_set(dup
, isl_basic_set_copy(set
->p
[i
]));
2795 struct isl_set
*isl_set_from_basic_set(struct isl_basic_set
*bset
)
2797 return isl_map_from_basic_map(bset
);
2800 struct isl_map
*isl_map_from_basic_map(struct isl_basic_map
*bmap
)
2802 struct isl_map
*map
;
2807 map
= isl_map_alloc_space(isl_space_copy(bmap
->dim
), 1, ISL_MAP_DISJOINT
);
2808 return isl_map_add_basic_map(map
, bmap
);
2811 __isl_give isl_set
*isl_set_add_basic_set(__isl_take isl_set
*set
,
2812 __isl_take isl_basic_set
*bset
)
2814 return set_from_map(isl_map_add_basic_map(set_to_map(set
),
2815 bset_to_bmap(bset
)));
2818 __isl_null isl_set
*isl_set_free(__isl_take isl_set
*set
)
2820 return isl_map_free(set
);
2823 void isl_set_print_internal(struct isl_set
*set
, FILE *out
, int indent
)
2828 fprintf(out
, "null set\n");
2832 fprintf(out
, "%*s", indent
, "");
2833 fprintf(out
, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
2834 set
->ref
, set
->n
, set
->dim
->nparam
, set
->dim
->n_out
,
2836 for (i
= 0; i
< set
->n
; ++i
) {
2837 fprintf(out
, "%*s", indent
, "");
2838 fprintf(out
, "basic set %d:\n", i
);
2839 isl_basic_set_print_internal(set
->p
[i
], out
, indent
+4);
2843 void isl_map_print_internal(struct isl_map
*map
, FILE *out
, int indent
)
2848 fprintf(out
, "null map\n");
2852 fprintf(out
, "%*s", indent
, "");
2853 fprintf(out
, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2854 "flags: %x, n_name: %d\n",
2855 map
->ref
, map
->n
, map
->dim
->nparam
, map
->dim
->n_in
,
2856 map
->dim
->n_out
, map
->flags
, map
->dim
->n_id
);
2857 for (i
= 0; i
< map
->n
; ++i
) {
2858 fprintf(out
, "%*s", indent
, "");
2859 fprintf(out
, "basic map %d:\n", i
);
2860 isl_basic_map_print_internal(map
->p
[i
], out
, indent
+4);
2864 struct isl_basic_map
*isl_basic_map_intersect_domain(
2865 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
2867 struct isl_basic_map
*bmap_domain
;
2872 isl_assert(bset
->ctx
, isl_space_match(bmap
->dim
, isl_dim_param
,
2873 bset
->dim
, isl_dim_param
), goto error
);
2875 if (isl_space_dim(bset
->dim
, isl_dim_set
) != 0)
2876 isl_assert(bset
->ctx
,
2877 isl_basic_map_compatible_domain(bmap
, bset
), goto error
);
2879 bmap
= isl_basic_map_cow(bmap
);
2882 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
2883 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
2884 bmap_domain
= isl_basic_map_from_domain(bset
);
2885 bmap
= add_constraints(bmap
, bmap_domain
, 0, 0);
2887 bmap
= isl_basic_map_simplify(bmap
);
2888 return isl_basic_map_finalize(bmap
);
2890 isl_basic_map_free(bmap
);
2891 isl_basic_set_free(bset
);
2895 struct isl_basic_map
*isl_basic_map_intersect_range(
2896 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
2898 struct isl_basic_map
*bmap_range
;
2903 isl_assert(bset
->ctx
, isl_space_match(bmap
->dim
, isl_dim_param
,
2904 bset
->dim
, isl_dim_param
), goto error
);
2906 if (isl_space_dim(bset
->dim
, isl_dim_set
) != 0)
2907 isl_assert(bset
->ctx
,
2908 isl_basic_map_compatible_range(bmap
, bset
), goto error
);
2910 if (isl_basic_set_plain_is_universe(bset
)) {
2911 isl_basic_set_free(bset
);
2915 bmap
= isl_basic_map_cow(bmap
);
2918 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
2919 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
2920 bmap_range
= bset_to_bmap(bset
);
2921 bmap
= add_constraints(bmap
, bmap_range
, 0, 0);
2923 bmap
= isl_basic_map_simplify(bmap
);
2924 return isl_basic_map_finalize(bmap
);
2926 isl_basic_map_free(bmap
);
2927 isl_basic_set_free(bset
);
2931 isl_bool
isl_basic_map_contains(__isl_keep isl_basic_map
*bmap
,
2932 __isl_keep isl_vec
*vec
)
2939 return isl_bool_error
;
2941 total
= 1 + isl_basic_map_total_dim(bmap
);
2942 if (total
!= vec
->size
)
2943 return isl_bool_error
;
2947 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
2948 isl_seq_inner_product(vec
->el
, bmap
->eq
[i
], total
, &s
);
2949 if (!isl_int_is_zero(s
)) {
2951 return isl_bool_false
;
2955 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
2956 isl_seq_inner_product(vec
->el
, bmap
->ineq
[i
], total
, &s
);
2957 if (isl_int_is_neg(s
)) {
2959 return isl_bool_false
;
2965 return isl_bool_true
;
2968 isl_bool
isl_basic_set_contains(__isl_keep isl_basic_set
*bset
,
2969 __isl_keep isl_vec
*vec
)
2971 return isl_basic_map_contains(bset_to_bmap(bset
), vec
);
2974 struct isl_basic_map
*isl_basic_map_intersect(
2975 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
2977 struct isl_vec
*sample
= NULL
;
2979 if (!bmap1
|| !bmap2
)
2982 isl_assert(bmap1
->ctx
, isl_space_match(bmap1
->dim
, isl_dim_param
,
2983 bmap2
->dim
, isl_dim_param
), goto error
);
2984 if (isl_space_dim(bmap1
->dim
, isl_dim_all
) ==
2985 isl_space_dim(bmap1
->dim
, isl_dim_param
) &&
2986 isl_space_dim(bmap2
->dim
, isl_dim_all
) !=
2987 isl_space_dim(bmap2
->dim
, isl_dim_param
))
2988 return isl_basic_map_intersect(bmap2
, bmap1
);
2990 if (isl_space_dim(bmap2
->dim
, isl_dim_all
) !=
2991 isl_space_dim(bmap2
->dim
, isl_dim_param
))
2992 isl_assert(bmap1
->ctx
,
2993 isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
2995 if (isl_basic_map_plain_is_empty(bmap1
)) {
2996 isl_basic_map_free(bmap2
);
2999 if (isl_basic_map_plain_is_empty(bmap2
)) {
3000 isl_basic_map_free(bmap1
);
3004 if (bmap1
->sample
&&
3005 isl_basic_map_contains(bmap1
, bmap1
->sample
) > 0 &&
3006 isl_basic_map_contains(bmap2
, bmap1
->sample
) > 0)
3007 sample
= isl_vec_copy(bmap1
->sample
);
3008 else if (bmap2
->sample
&&
3009 isl_basic_map_contains(bmap1
, bmap2
->sample
) > 0 &&
3010 isl_basic_map_contains(bmap2
, bmap2
->sample
) > 0)
3011 sample
= isl_vec_copy(bmap2
->sample
);
3013 bmap1
= isl_basic_map_cow(bmap1
);
3016 bmap1
= isl_basic_map_extend_space(bmap1
, isl_space_copy(bmap1
->dim
),
3017 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
3018 bmap1
= add_constraints(bmap1
, bmap2
, 0, 0);
3021 isl_vec_free(sample
);
3023 isl_vec_free(bmap1
->sample
);
3024 bmap1
->sample
= sample
;
3027 bmap1
= isl_basic_map_simplify(bmap1
);
3028 return isl_basic_map_finalize(bmap1
);
3031 isl_vec_free(sample
);
3032 isl_basic_map_free(bmap1
);
3033 isl_basic_map_free(bmap2
);
3037 struct isl_basic_set
*isl_basic_set_intersect(
3038 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
3040 return bset_from_bmap(isl_basic_map_intersect(bset_to_bmap(bset1
),
3041 bset_to_bmap(bset2
)));
3044 __isl_give isl_basic_set
*isl_basic_set_intersect_params(
3045 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
3047 return isl_basic_set_intersect(bset1
, bset2
);
3050 /* Special case of isl_map_intersect, where both map1 and map2
3051 * are convex, without any divs and such that either map1 or map2
3052 * contains a single constraint. This constraint is then simply
3053 * added to the other map.
3055 static __isl_give isl_map
*map_intersect_add_constraint(
3056 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
3058 isl_assert(map1
->ctx
, map1
->n
== 1, goto error
);
3059 isl_assert(map2
->ctx
, map1
->n
== 1, goto error
);
3060 isl_assert(map1
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
3061 isl_assert(map2
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
3063 if (map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
!= 1)
3064 return isl_map_intersect(map2
, map1
);
3066 isl_assert(map2
->ctx
,
3067 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1, goto error
);
3069 map1
= isl_map_cow(map1
);
3072 if (isl_map_plain_is_empty(map1
)) {
3076 map1
->p
[0] = isl_basic_map_cow(map1
->p
[0]);
3077 if (map2
->p
[0]->n_eq
== 1)
3078 map1
->p
[0] = isl_basic_map_add_eq(map1
->p
[0], map2
->p
[0]->eq
[0]);
3080 map1
->p
[0] = isl_basic_map_add_ineq(map1
->p
[0],
3081 map2
->p
[0]->ineq
[0]);
3083 map1
->p
[0] = isl_basic_map_simplify(map1
->p
[0]);
3084 map1
->p
[0] = isl_basic_map_finalize(map1
->p
[0]);
3088 if (isl_basic_map_plain_is_empty(map1
->p
[0])) {
3089 isl_basic_map_free(map1
->p
[0]);
3102 /* map2 may be either a parameter domain or a map living in the same
3105 static __isl_give isl_map
*map_intersect_internal(__isl_take isl_map
*map1
,
3106 __isl_take isl_map
*map2
)
3115 if ((isl_map_plain_is_empty(map1
) ||
3116 isl_map_plain_is_universe(map2
)) &&
3117 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
3121 if ((isl_map_plain_is_empty(map2
) ||
3122 isl_map_plain_is_universe(map1
)) &&
3123 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
3128 if (map1
->n
== 1 && map2
->n
== 1 &&
3129 map1
->p
[0]->n_div
== 0 && map2
->p
[0]->n_div
== 0 &&
3130 isl_space_is_equal(map1
->dim
, map2
->dim
) &&
3131 (map1
->p
[0]->n_eq
+ map1
->p
[0]->n_ineq
== 1 ||
3132 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1))
3133 return map_intersect_add_constraint(map1
, map2
);
3135 if (isl_space_dim(map2
->dim
, isl_dim_all
) !=
3136 isl_space_dim(map2
->dim
, isl_dim_param
))
3137 isl_assert(map1
->ctx
,
3138 isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
3140 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
3141 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
3142 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
3144 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
3145 map1
->n
* map2
->n
, flags
);
3148 for (i
= 0; i
< map1
->n
; ++i
)
3149 for (j
= 0; j
< map2
->n
; ++j
) {
3150 struct isl_basic_map
*part
;
3151 part
= isl_basic_map_intersect(
3152 isl_basic_map_copy(map1
->p
[i
]),
3153 isl_basic_map_copy(map2
->p
[j
]));
3154 if (isl_basic_map_is_empty(part
) < 0)
3155 part
= isl_basic_map_free(part
);
3156 result
= isl_map_add_basic_map(result
, part
);
3169 static __isl_give isl_map
*map_intersect(__isl_take isl_map
*map1
,
3170 __isl_take isl_map
*map2
)
3174 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
3175 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
3176 "spaces don't match", goto error
);
3177 return map_intersect_internal(map1
, map2
);
3184 __isl_give isl_map
*isl_map_intersect(__isl_take isl_map
*map1
,
3185 __isl_take isl_map
*map2
)
3187 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect
);
3190 struct isl_set
*isl_set_intersect(struct isl_set
*set1
, struct isl_set
*set2
)
3192 return set_from_map(isl_map_intersect(set_to_map(set1
),
3196 /* map_intersect_internal accepts intersections
3197 * with parameter domains, so we can just call that function.
3199 static __isl_give isl_map
*map_intersect_params(__isl_take isl_map
*map
,
3200 __isl_take isl_set
*params
)
3202 return map_intersect_internal(map
, params
);
3205 __isl_give isl_map
*isl_map_intersect_params(__isl_take isl_map
*map1
,
3206 __isl_take isl_map
*map2
)
3208 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect_params
);
3211 __isl_give isl_set
*isl_set_intersect_params(__isl_take isl_set
*set
,
3212 __isl_take isl_set
*params
)
3214 return isl_map_intersect_params(set
, params
);
3217 struct isl_basic_map
*isl_basic_map_reverse(struct isl_basic_map
*bmap
)
3220 unsigned pos
, n1
, n2
;
3224 bmap
= isl_basic_map_cow(bmap
);
3227 space
= isl_space_reverse(isl_space_copy(bmap
->dim
));
3228 pos
= isl_basic_map_offset(bmap
, isl_dim_in
);
3229 n1
= isl_basic_map_dim(bmap
, isl_dim_in
);
3230 n2
= isl_basic_map_dim(bmap
, isl_dim_out
);
3231 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
3232 return isl_basic_map_reset_space(bmap
, space
);
3235 static __isl_give isl_basic_map
*basic_map_space_reset(
3236 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
)
3242 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
3245 space
= isl_basic_map_get_space(bmap
);
3246 space
= isl_space_reset(space
, type
);
3247 bmap
= isl_basic_map_reset_space(bmap
, space
);
3251 __isl_give isl_basic_map
*isl_basic_map_insert_dims(
3252 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
3253 unsigned pos
, unsigned n
)
3256 struct isl_basic_map
*res
;
3257 struct isl_dim_map
*dim_map
;
3258 unsigned total
, off
;
3259 enum isl_dim_type t
;
3262 return basic_map_space_reset(bmap
, type
);
3267 res_dim
= isl_space_insert_dims(isl_basic_map_get_space(bmap
), type
, pos
, n
);
3269 total
= isl_basic_map_total_dim(bmap
) + n
;
3270 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3272 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3274 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3276 unsigned size
= isl_basic_map_dim(bmap
, t
);
3277 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3279 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3280 pos
, size
- pos
, off
+ pos
+ n
);
3282 off
+= isl_space_dim(res_dim
, t
);
3284 isl_dim_map_div(dim_map
, bmap
, off
);
3286 res
= isl_basic_map_alloc_space(res_dim
,
3287 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3288 if (isl_basic_map_is_rational(bmap
))
3289 res
= isl_basic_map_set_rational(res
);
3290 if (isl_basic_map_plain_is_empty(bmap
)) {
3291 isl_basic_map_free(bmap
);
3293 return isl_basic_map_set_to_empty(res
);
3295 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3296 return isl_basic_map_finalize(res
);
3299 __isl_give isl_basic_set
*isl_basic_set_insert_dims(
3300 __isl_take isl_basic_set
*bset
,
3301 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3303 return isl_basic_map_insert_dims(bset
, type
, pos
, n
);
3306 __isl_give isl_basic_map
*isl_basic_map_add_dims(__isl_take isl_basic_map
*bmap
,
3307 enum isl_dim_type type
, unsigned n
)
3311 return isl_basic_map_insert_dims(bmap
, type
,
3312 isl_basic_map_dim(bmap
, type
), n
);
3315 __isl_give isl_basic_set
*isl_basic_set_add_dims(__isl_take isl_basic_set
*bset
,
3316 enum isl_dim_type type
, unsigned n
)
3320 isl_assert(bset
->ctx
, type
!= isl_dim_in
, goto error
);
3321 return isl_basic_map_add_dims(bset
, type
, n
);
3323 isl_basic_set_free(bset
);
3327 static __isl_give isl_map
*map_space_reset(__isl_take isl_map
*map
,
3328 enum isl_dim_type type
)
3332 if (!map
|| !isl_space_is_named_or_nested(map
->dim
, type
))
3335 space
= isl_map_get_space(map
);
3336 space
= isl_space_reset(space
, type
);
3337 map
= isl_map_reset_space(map
, space
);
3341 __isl_give isl_map
*isl_map_insert_dims(__isl_take isl_map
*map
,
3342 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3347 return map_space_reset(map
, type
);
3349 map
= isl_map_cow(map
);
3353 map
->dim
= isl_space_insert_dims(map
->dim
, type
, pos
, n
);
3357 for (i
= 0; i
< map
->n
; ++i
) {
3358 map
->p
[i
] = isl_basic_map_insert_dims(map
->p
[i
], type
, pos
, n
);
3369 __isl_give isl_set
*isl_set_insert_dims(__isl_take isl_set
*set
,
3370 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3372 return isl_map_insert_dims(set
, type
, pos
, n
);
3375 __isl_give isl_map
*isl_map_add_dims(__isl_take isl_map
*map
,
3376 enum isl_dim_type type
, unsigned n
)
3380 return isl_map_insert_dims(map
, type
, isl_map_dim(map
, type
), n
);
3383 __isl_give isl_set
*isl_set_add_dims(__isl_take isl_set
*set
,
3384 enum isl_dim_type type
, unsigned n
)
3388 isl_assert(set
->ctx
, type
!= isl_dim_in
, goto error
);
3389 return set_from_map(isl_map_add_dims(set_to_map(set
), type
, n
));
3395 __isl_give isl_basic_map
*isl_basic_map_move_dims(
3396 __isl_take isl_basic_map
*bmap
,
3397 enum isl_dim_type dst_type
, unsigned dst_pos
,
3398 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3400 struct isl_dim_map
*dim_map
;
3401 struct isl_basic_map
*res
;
3402 enum isl_dim_type t
;
3403 unsigned total
, off
;
3410 isl_assert(bmap
->ctx
, src_pos
+ n
<= isl_basic_map_dim(bmap
, src_type
),
3413 if (dst_type
== src_type
&& dst_pos
== src_pos
)
3416 isl_assert(bmap
->ctx
, dst_type
!= src_type
, goto error
);
3418 if (pos(bmap
->dim
, dst_type
) + dst_pos
==
3419 pos(bmap
->dim
, src_type
) + src_pos
+
3420 ((src_type
< dst_type
) ? n
: 0)) {
3421 bmap
= isl_basic_map_cow(bmap
);
3425 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
3426 src_type
, src_pos
, n
);
3430 bmap
= isl_basic_map_finalize(bmap
);
3435 total
= isl_basic_map_total_dim(bmap
);
3436 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3439 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3440 unsigned size
= isl_space_dim(bmap
->dim
, t
);
3441 if (t
== dst_type
) {
3442 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3445 isl_dim_map_dim_range(dim_map
, bmap
->dim
, src_type
,
3448 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3449 dst_pos
, size
- dst_pos
, off
);
3450 off
+= size
- dst_pos
;
3451 } else if (t
== src_type
) {
3452 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3455 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3456 src_pos
+ n
, size
- src_pos
- n
, off
);
3457 off
+= size
- src_pos
- n
;
3459 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3463 isl_dim_map_div(dim_map
, bmap
, off
);
3465 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
3466 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3467 bmap
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3471 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
3472 src_type
, src_pos
, n
);
3476 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
3477 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3478 bmap
= isl_basic_map_finalize(bmap
);
3482 isl_basic_map_free(bmap
);
3486 __isl_give isl_basic_set
*isl_basic_set_move_dims(__isl_take isl_basic_set
*bset
,
3487 enum isl_dim_type dst_type
, unsigned dst_pos
,
3488 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3490 isl_basic_map
*bmap
= bset_to_bmap(bset
);
3491 bmap
= isl_basic_map_move_dims(bmap
, dst_type
, dst_pos
,
3492 src_type
, src_pos
, n
);
3493 return bset_from_bmap(bmap
);
3496 __isl_give isl_set
*isl_set_move_dims(__isl_take isl_set
*set
,
3497 enum isl_dim_type dst_type
, unsigned dst_pos
,
3498 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3502 isl_assert(set
->ctx
, dst_type
!= isl_dim_in
, goto error
);
3503 return set_from_map(isl_map_move_dims(set_to_map(set
),
3504 dst_type
, dst_pos
, src_type
, src_pos
, n
));
3510 __isl_give isl_map
*isl_map_move_dims(__isl_take isl_map
*map
,
3511 enum isl_dim_type dst_type
, unsigned dst_pos
,
3512 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3521 isl_assert(map
->ctx
, src_pos
+ n
<= isl_map_dim(map
, src_type
),
3524 if (dst_type
== src_type
&& dst_pos
== src_pos
)
3527 isl_assert(map
->ctx
, dst_type
!= src_type
, goto error
);
3529 map
= isl_map_cow(map
);
3533 map
->dim
= isl_space_move_dims(map
->dim
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
3537 for (i
= 0; i
< map
->n
; ++i
) {
3538 map
->p
[i
] = isl_basic_map_move_dims(map
->p
[i
],
3540 src_type
, src_pos
, n
);
3551 /* Move the specified dimensions to the last columns right before
3552 * the divs. Don't change the dimension specification of bmap.
3553 * That's the responsibility of the caller.
3555 static __isl_give isl_basic_map
*move_last(__isl_take isl_basic_map
*bmap
,
3556 enum isl_dim_type type
, unsigned first
, unsigned n
)
3558 struct isl_dim_map
*dim_map
;
3559 struct isl_basic_map
*res
;
3560 enum isl_dim_type t
;
3561 unsigned total
, off
;
3565 if (pos(bmap
->dim
, type
) + first
+ n
==
3566 1 + isl_space_dim(bmap
->dim
, isl_dim_all
))
3569 total
= isl_basic_map_total_dim(bmap
);
3570 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3573 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3574 unsigned size
= isl_space_dim(bmap
->dim
, t
);
3576 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3579 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3580 first
, n
, total
- bmap
->n_div
- n
);
3581 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3582 first
+ n
, size
- (first
+ n
), off
);
3583 off
+= size
- (first
+ n
);
3585 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3589 isl_dim_map_div(dim_map
, bmap
, off
+ n
);
3591 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
3592 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3593 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3597 /* Insert "n" rows in the divs of "bmap".
3599 * The number of columns is not changed, which means that the last
3600 * dimensions of "bmap" are being reintepreted as the new divs.
3601 * The space of "bmap" is not adjusted, however, which means
3602 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
3603 * from the space of "bmap" is the responsibility of the caller.
3605 static __isl_give isl_basic_map
*insert_div_rows(__isl_take isl_basic_map
*bmap
,
3613 bmap
= isl_basic_map_cow(bmap
);
3617 row_size
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + bmap
->extra
;
3618 old
= bmap
->block2
.data
;
3619 bmap
->block2
= isl_blk_extend(bmap
->ctx
, bmap
->block2
,
3620 (bmap
->extra
+ n
) * (1 + row_size
));
3621 if (!bmap
->block2
.data
)
3622 return isl_basic_map_free(bmap
);
3623 new_div
= isl_alloc_array(bmap
->ctx
, isl_int
*, bmap
->extra
+ n
);
3625 return isl_basic_map_free(bmap
);
3626 for (i
= 0; i
< n
; ++i
) {
3627 new_div
[i
] = bmap
->block2
.data
+
3628 (bmap
->extra
+ i
) * (1 + row_size
);
3629 isl_seq_clr(new_div
[i
], 1 + row_size
);
3631 for (i
= 0; i
< bmap
->extra
; ++i
)
3632 new_div
[n
+ i
] = bmap
->block2
.data
+ (bmap
->div
[i
] - old
);
3634 bmap
->div
= new_div
;
3641 /* Drop constraints from "bmap" that only involve the variables
3642 * of "type" in the range [first, first + n] that are not related
3643 * to any of the variables outside that interval.
3644 * These constraints cannot influence the values for the variables
3645 * outside the interval, except in case they cause "bmap" to be empty.
3646 * Only drop the constraints if "bmap" is known to be non-empty.
3648 static __isl_give isl_basic_map
*drop_irrelevant_constraints(
3649 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
3650 unsigned first
, unsigned n
)
3654 unsigned dim
, n_div
;
3657 non_empty
= isl_basic_map_plain_is_non_empty(bmap
);
3659 return isl_basic_map_free(bmap
);
3663 dim
= isl_basic_map_dim(bmap
, isl_dim_all
);
3664 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
3665 groups
= isl_calloc_array(isl_basic_map_get_ctx(bmap
), int, dim
);
3667 return isl_basic_map_free(bmap
);
3668 first
+= isl_basic_map_offset(bmap
, type
) - 1;
3669 for (i
= 0; i
< first
; ++i
)
3671 for (i
= first
+ n
; i
< dim
- n_div
; ++i
)
3674 bmap
= isl_basic_map_drop_unrelated_constraints(bmap
, groups
);
3679 /* Turn the n dimensions of type type, starting at first
3680 * into existentially quantified variables.
3682 * If a subset of the projected out variables are unrelated
3683 * to any of the variables that remain, then the constraints
3684 * involving this subset are simply dropped first.
3686 __isl_give isl_basic_map
*isl_basic_map_project_out(
3687 __isl_take isl_basic_map
*bmap
,
3688 enum isl_dim_type type
, unsigned first
, unsigned n
)
3691 return basic_map_space_reset(bmap
, type
);
3692 if (type
== isl_dim_div
)
3693 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
3694 "cannot project out existentially quantified variables",
3695 return isl_basic_map_free(bmap
));
3697 bmap
= drop_irrelevant_constraints(bmap
, type
, first
, n
);
3701 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
3702 return isl_basic_map_remove_dims(bmap
, type
, first
, n
);
3704 isl_assert(bmap
->ctx
, first
+ n
<= isl_basic_map_dim(bmap
, type
),
3707 bmap
= move_last(bmap
, type
, first
, n
);
3708 bmap
= isl_basic_map_cow(bmap
);
3709 bmap
= insert_div_rows(bmap
, n
);
3713 bmap
->dim
= isl_space_drop_dims(bmap
->dim
, type
, first
, n
);
3716 bmap
= isl_basic_map_simplify(bmap
);
3717 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
3718 return isl_basic_map_finalize(bmap
);
3720 isl_basic_map_free(bmap
);
3724 /* Turn the n dimensions of type type, starting at first
3725 * into existentially quantified variables.
3727 struct isl_basic_set
*isl_basic_set_project_out(struct isl_basic_set
*bset
,
3728 enum isl_dim_type type
, unsigned first
, unsigned n
)
3730 return bset_from_bmap(isl_basic_map_project_out(bset_to_bmap(bset
),
3734 /* Turn the n dimensions of type type, starting at first
3735 * into existentially quantified variables.
3737 __isl_give isl_map
*isl_map_project_out(__isl_take isl_map
*map
,
3738 enum isl_dim_type type
, unsigned first
, unsigned n
)
3746 return map_space_reset(map
, type
);
3748 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
3750 map
= isl_map_cow(map
);
3754 map
->dim
= isl_space_drop_dims(map
->dim
, type
, first
, n
);
3758 for (i
= 0; i
< map
->n
; ++i
) {
3759 map
->p
[i
] = isl_basic_map_project_out(map
->p
[i
], type
, first
, n
);
3770 /* Turn the n dimensions of type type, starting at first
3771 * into existentially quantified variables.
3773 __isl_give isl_set
*isl_set_project_out(__isl_take isl_set
*set
,
3774 enum isl_dim_type type
, unsigned first
, unsigned n
)
3776 return set_from_map(isl_map_project_out(set_to_map(set
),
3780 /* Return a map that projects the elements in "set" onto their
3781 * "n" set dimensions starting at "first".
3782 * "type" should be equal to isl_dim_set.
3784 __isl_give isl_map
*isl_set_project_onto_map(__isl_take isl_set
*set
,
3785 enum isl_dim_type type
, unsigned first
, unsigned n
)
3793 if (type
!= isl_dim_set
)
3794 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
3795 "only set dimensions can be projected out", goto error
);
3796 dim
= isl_set_dim(set
, isl_dim_set
);
3797 if (first
+ n
> dim
|| first
+ n
< first
)
3798 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
3799 "index out of bounds", goto error
);
3801 map
= isl_map_from_domain(set
);
3802 map
= isl_map_add_dims(map
, isl_dim_out
, n
);
3803 for (i
= 0; i
< n
; ++i
)
3804 map
= isl_map_equate(map
, isl_dim_in
, first
+ i
,
3812 static struct isl_basic_map
*add_divs(struct isl_basic_map
*bmap
, unsigned n
)
3816 for (i
= 0; i
< n
; ++i
) {
3817 j
= isl_basic_map_alloc_div(bmap
);
3820 isl_seq_clr(bmap
->div
[j
], 1+1+isl_basic_map_total_dim(bmap
));
3824 isl_basic_map_free(bmap
);
3828 struct isl_basic_map
*isl_basic_map_apply_range(
3829 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3831 isl_space
*dim_result
= NULL
;
3832 struct isl_basic_map
*bmap
;
3833 unsigned n_in
, n_out
, n
, nparam
, total
, pos
;
3834 struct isl_dim_map
*dim_map1
, *dim_map2
;
3836 if (!bmap1
|| !bmap2
)
3838 if (!isl_space_match(bmap1
->dim
, isl_dim_param
,
3839 bmap2
->dim
, isl_dim_param
))
3840 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
3841 "parameters don't match", goto error
);
3842 if (!isl_space_tuple_is_equal(bmap1
->dim
, isl_dim_out
,
3843 bmap2
->dim
, isl_dim_in
))
3844 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
3845 "spaces don't match", goto error
);
3847 dim_result
= isl_space_join(isl_space_copy(bmap1
->dim
),
3848 isl_space_copy(bmap2
->dim
));
3850 n_in
= isl_basic_map_n_in(bmap1
);
3851 n_out
= isl_basic_map_n_out(bmap2
);
3852 n
= isl_basic_map_n_out(bmap1
);
3853 nparam
= isl_basic_map_n_param(bmap1
);
3855 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ n
;
3856 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
3857 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
3858 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
3859 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
3860 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
3861 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_in
);
3862 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_out
);
3863 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
3864 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
3865 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
3867 bmap
= isl_basic_map_alloc_space(dim_result
,
3868 bmap1
->n_div
+ bmap2
->n_div
+ n
,
3869 bmap1
->n_eq
+ bmap2
->n_eq
,
3870 bmap1
->n_ineq
+ bmap2
->n_ineq
);
3871 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
3872 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
3873 bmap
= add_divs(bmap
, n
);
3874 bmap
= isl_basic_map_simplify(bmap
);
3875 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
3876 return isl_basic_map_finalize(bmap
);
3878 isl_basic_map_free(bmap1
);
3879 isl_basic_map_free(bmap2
);
3883 struct isl_basic_set
*isl_basic_set_apply(
3884 struct isl_basic_set
*bset
, struct isl_basic_map
*bmap
)
3889 isl_assert(bset
->ctx
, isl_basic_map_compatible_domain(bmap
, bset
),
3892 return bset_from_bmap(isl_basic_map_apply_range(bset_to_bmap(bset
),
3895 isl_basic_set_free(bset
);
3896 isl_basic_map_free(bmap
);
3900 struct isl_basic_map
*isl_basic_map_apply_domain(
3901 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3903 if (!bmap1
|| !bmap2
)
3906 isl_assert(bmap1
->ctx
,
3907 isl_basic_map_n_in(bmap1
) == isl_basic_map_n_in(bmap2
), goto error
);
3908 isl_assert(bmap1
->ctx
,
3909 isl_basic_map_n_param(bmap1
) == isl_basic_map_n_param(bmap2
),
3912 bmap1
= isl_basic_map_reverse(bmap1
);
3913 bmap1
= isl_basic_map_apply_range(bmap1
, bmap2
);
3914 return isl_basic_map_reverse(bmap1
);
3916 isl_basic_map_free(bmap1
);
3917 isl_basic_map_free(bmap2
);
3921 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
3922 * A \cap B -> f(A) + f(B)
3924 struct isl_basic_map
*isl_basic_map_sum(
3925 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3927 unsigned n_in
, n_out
, nparam
, total
, pos
;
3928 struct isl_basic_map
*bmap
= NULL
;
3929 struct isl_dim_map
*dim_map1
, *dim_map2
;
3932 if (!bmap1
|| !bmap2
)
3935 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
),
3938 nparam
= isl_basic_map_n_param(bmap1
);
3939 n_in
= isl_basic_map_n_in(bmap1
);
3940 n_out
= isl_basic_map_n_out(bmap1
);
3942 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
;
3943 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
3944 dim_map2
= isl_dim_map_alloc(bmap2
->ctx
, total
);
3945 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
3946 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
);
3947 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
3948 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
3949 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_in
+ n_out
);
3950 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
3951 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
3952 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_out
);
3954 bmap
= isl_basic_map_alloc_space(isl_space_copy(bmap1
->dim
),
3955 bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
,
3956 bmap1
->n_eq
+ bmap2
->n_eq
+ n_out
,
3957 bmap1
->n_ineq
+ bmap2
->n_ineq
);
3958 for (i
= 0; i
< n_out
; ++i
) {
3959 int j
= isl_basic_map_alloc_equality(bmap
);
3962 isl_seq_clr(bmap
->eq
[j
], 1+total
);
3963 isl_int_set_si(bmap
->eq
[j
][1+nparam
+n_in
+i
], -1);
3964 isl_int_set_si(bmap
->eq
[j
][1+pos
+i
], 1);
3965 isl_int_set_si(bmap
->eq
[j
][1+pos
-n_out
+i
], 1);
3967 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
3968 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
3969 bmap
= add_divs(bmap
, 2 * n_out
);
3971 bmap
= isl_basic_map_simplify(bmap
);
3972 return isl_basic_map_finalize(bmap
);
3974 isl_basic_map_free(bmap
);
3975 isl_basic_map_free(bmap1
);
3976 isl_basic_map_free(bmap2
);
3980 /* Given two maps A -> f(A) and B -> g(B), construct a map
3981 * A \cap B -> f(A) + f(B)
3983 struct isl_map
*isl_map_sum(struct isl_map
*map1
, struct isl_map
*map2
)
3985 struct isl_map
*result
;
3991 isl_assert(map1
->ctx
, isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
3993 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
3994 map1
->n
* map2
->n
, 0);
3997 for (i
= 0; i
< map1
->n
; ++i
)
3998 for (j
= 0; j
< map2
->n
; ++j
) {
3999 struct isl_basic_map
*part
;
4000 part
= isl_basic_map_sum(
4001 isl_basic_map_copy(map1
->p
[i
]),
4002 isl_basic_map_copy(map2
->p
[j
]));
4003 if (isl_basic_map_is_empty(part
))
4004 isl_basic_map_free(part
);
4006 result
= isl_map_add_basic_map(result
, part
);
4019 __isl_give isl_set
*isl_set_sum(__isl_take isl_set
*set1
,
4020 __isl_take isl_set
*set2
)
4022 return set_from_map(isl_map_sum(set_to_map(set1
), set_to_map(set2
)));
4025 /* Given a basic map A -> f(A), construct A -> -f(A).
4027 struct isl_basic_map
*isl_basic_map_neg(struct isl_basic_map
*bmap
)
4032 bmap
= isl_basic_map_cow(bmap
);
4036 n
= isl_basic_map_dim(bmap
, isl_dim_out
);
4037 off
= isl_basic_map_offset(bmap
, isl_dim_out
);
4038 for (i
= 0; i
< bmap
->n_eq
; ++i
)
4039 for (j
= 0; j
< n
; ++j
)
4040 isl_int_neg(bmap
->eq
[i
][off
+j
], bmap
->eq
[i
][off
+j
]);
4041 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
4042 for (j
= 0; j
< n
; ++j
)
4043 isl_int_neg(bmap
->ineq
[i
][off
+j
], bmap
->ineq
[i
][off
+j
]);
4044 for (i
= 0; i
< bmap
->n_div
; ++i
)
4045 for (j
= 0; j
< n
; ++j
)
4046 isl_int_neg(bmap
->div
[i
][1+off
+j
], bmap
->div
[i
][1+off
+j
]);
4047 bmap
= isl_basic_map_gauss(bmap
, NULL
);
4048 return isl_basic_map_finalize(bmap
);
4051 __isl_give isl_basic_set
*isl_basic_set_neg(__isl_take isl_basic_set
*bset
)
4053 return isl_basic_map_neg(bset
);
4056 /* Given a map A -> f(A), construct A -> -f(A).
4058 struct isl_map
*isl_map_neg(struct isl_map
*map
)
4062 map
= isl_map_cow(map
);
4066 for (i
= 0; i
< map
->n
; ++i
) {
4067 map
->p
[i
] = isl_basic_map_neg(map
->p
[i
]);
4078 __isl_give isl_set
*isl_set_neg(__isl_take isl_set
*set
)
4080 return set_from_map(isl_map_neg(set_to_map(set
)));
4083 /* Given a basic map A -> f(A) and an integer d, construct a basic map
4084 * A -> floor(f(A)/d).
4086 struct isl_basic_map
*isl_basic_map_floordiv(struct isl_basic_map
*bmap
,
4089 unsigned n_in
, n_out
, nparam
, total
, pos
;
4090 struct isl_basic_map
*result
= NULL
;
4091 struct isl_dim_map
*dim_map
;
4097 nparam
= isl_basic_map_n_param(bmap
);
4098 n_in
= isl_basic_map_n_in(bmap
);
4099 n_out
= isl_basic_map_n_out(bmap
);
4101 total
= nparam
+ n_in
+ n_out
+ bmap
->n_div
+ n_out
;
4102 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
4103 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_param
, pos
= 0);
4104 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_in
, pos
+= nparam
);
4105 isl_dim_map_div(dim_map
, bmap
, pos
+= n_in
+ n_out
);
4106 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_out
, pos
+= bmap
->n_div
);
4108 result
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
4109 bmap
->n_div
+ n_out
,
4110 bmap
->n_eq
, bmap
->n_ineq
+ 2 * n_out
);
4111 result
= isl_basic_map_add_constraints_dim_map(result
, bmap
, dim_map
);
4112 result
= add_divs(result
, n_out
);
4113 for (i
= 0; i
< n_out
; ++i
) {
4115 j
= isl_basic_map_alloc_inequality(result
);
4118 isl_seq_clr(result
->ineq
[j
], 1+total
);
4119 isl_int_neg(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
4120 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], 1);
4121 j
= isl_basic_map_alloc_inequality(result
);
4124 isl_seq_clr(result
->ineq
[j
], 1+total
);
4125 isl_int_set(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
4126 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], -1);
4127 isl_int_sub_ui(result
->ineq
[j
][0], d
, 1);
4130 result
= isl_basic_map_simplify(result
);
4131 return isl_basic_map_finalize(result
);
4133 isl_basic_map_free(result
);
4137 /* Given a map A -> f(A) and an integer d, construct a map
4138 * A -> floor(f(A)/d).
4140 struct isl_map
*isl_map_floordiv(struct isl_map
*map
, isl_int d
)
4144 map
= isl_map_cow(map
);
4148 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
4149 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4150 for (i
= 0; i
< map
->n
; ++i
) {
4151 map
->p
[i
] = isl_basic_map_floordiv(map
->p
[i
], d
);
4162 /* Given a map A -> f(A) and an integer d, construct a map
4163 * A -> floor(f(A)/d).
4165 __isl_give isl_map
*isl_map_floordiv_val(__isl_take isl_map
*map
,
4166 __isl_take isl_val
*d
)
4170 if (!isl_val_is_int(d
))
4171 isl_die(isl_val_get_ctx(d
), isl_error_invalid
,
4172 "expecting integer denominator", goto error
);
4173 map
= isl_map_floordiv(map
, d
->n
);
4182 static struct isl_basic_map
*var_equal(struct isl_basic_map
*bmap
, unsigned pos
)
4188 i
= isl_basic_map_alloc_equality(bmap
);
4191 nparam
= isl_basic_map_n_param(bmap
);
4192 n_in
= isl_basic_map_n_in(bmap
);
4193 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4194 isl_int_set_si(bmap
->eq
[i
][1+nparam
+pos
], -1);
4195 isl_int_set_si(bmap
->eq
[i
][1+nparam
+n_in
+pos
], 1);
4196 return isl_basic_map_finalize(bmap
);
4198 isl_basic_map_free(bmap
);
4202 /* Add a constraint to "bmap" expressing i_pos < o_pos
4204 static struct isl_basic_map
*var_less(struct isl_basic_map
*bmap
, unsigned pos
)
4210 i
= isl_basic_map_alloc_inequality(bmap
);
4213 nparam
= isl_basic_map_n_param(bmap
);
4214 n_in
= isl_basic_map_n_in(bmap
);
4215 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4216 isl_int_set_si(bmap
->ineq
[i
][0], -1);
4217 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
4218 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
4219 return isl_basic_map_finalize(bmap
);
4221 isl_basic_map_free(bmap
);
4225 /* Add a constraint to "bmap" expressing i_pos <= o_pos
4227 static __isl_give isl_basic_map
*var_less_or_equal(
4228 __isl_take isl_basic_map
*bmap
, unsigned pos
)
4234 i
= isl_basic_map_alloc_inequality(bmap
);
4237 nparam
= isl_basic_map_n_param(bmap
);
4238 n_in
= isl_basic_map_n_in(bmap
);
4239 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4240 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
4241 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
4242 return isl_basic_map_finalize(bmap
);
4244 isl_basic_map_free(bmap
);
4248 /* Add a constraint to "bmap" expressing i_pos > o_pos
4250 static struct isl_basic_map
*var_more(struct isl_basic_map
*bmap
, unsigned pos
)
4256 i
= isl_basic_map_alloc_inequality(bmap
);
4259 nparam
= isl_basic_map_n_param(bmap
);
4260 n_in
= isl_basic_map_n_in(bmap
);
4261 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4262 isl_int_set_si(bmap
->ineq
[i
][0], -1);
4263 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
4264 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
4265 return isl_basic_map_finalize(bmap
);
4267 isl_basic_map_free(bmap
);
4271 /* Add a constraint to "bmap" expressing i_pos >= o_pos
4273 static __isl_give isl_basic_map
*var_more_or_equal(
4274 __isl_take isl_basic_map
*bmap
, unsigned pos
)
4280 i
= isl_basic_map_alloc_inequality(bmap
);
4283 nparam
= isl_basic_map_n_param(bmap
);
4284 n_in
= isl_basic_map_n_in(bmap
);
4285 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4286 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
4287 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
4288 return isl_basic_map_finalize(bmap
);
4290 isl_basic_map_free(bmap
);
4294 __isl_give isl_basic_map
*isl_basic_map_equal(
4295 __isl_take isl_space
*dim
, unsigned n_equal
)
4298 struct isl_basic_map
*bmap
;
4299 bmap
= isl_basic_map_alloc_space(dim
, 0, n_equal
, 0);
4302 for (i
= 0; i
< n_equal
&& bmap
; ++i
)
4303 bmap
= var_equal(bmap
, i
);
4304 return isl_basic_map_finalize(bmap
);
4307 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
4309 __isl_give isl_basic_map
*isl_basic_map_less_at(__isl_take isl_space
*dim
,
4313 struct isl_basic_map
*bmap
;
4314 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4317 for (i
= 0; i
< pos
&& bmap
; ++i
)
4318 bmap
= var_equal(bmap
, i
);
4320 bmap
= var_less(bmap
, pos
);
4321 return isl_basic_map_finalize(bmap
);
4324 /* Return a relation on "dim" expressing i_[0..pos] <<= o_[0..pos]
4326 __isl_give isl_basic_map
*isl_basic_map_less_or_equal_at(
4327 __isl_take isl_space
*dim
, unsigned pos
)
4330 isl_basic_map
*bmap
;
4332 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4333 for (i
= 0; i
< pos
; ++i
)
4334 bmap
= var_equal(bmap
, i
);
4335 bmap
= var_less_or_equal(bmap
, pos
);
4336 return isl_basic_map_finalize(bmap
);
4339 /* Return a relation on "dim" expressing i_pos > o_pos
4341 __isl_give isl_basic_map
*isl_basic_map_more_at(__isl_take isl_space
*dim
,
4345 struct isl_basic_map
*bmap
;
4346 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4349 for (i
= 0; i
< pos
&& bmap
; ++i
)
4350 bmap
= var_equal(bmap
, i
);
4352 bmap
= var_more(bmap
, pos
);
4353 return isl_basic_map_finalize(bmap
);
4356 /* Return a relation on "dim" expressing i_[0..pos] >>= o_[0..pos]
4358 __isl_give isl_basic_map
*isl_basic_map_more_or_equal_at(
4359 __isl_take isl_space
*dim
, unsigned pos
)
4362 isl_basic_map
*bmap
;
4364 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4365 for (i
= 0; i
< pos
; ++i
)
4366 bmap
= var_equal(bmap
, i
);
4367 bmap
= var_more_or_equal(bmap
, pos
);
4368 return isl_basic_map_finalize(bmap
);
4371 static __isl_give isl_map
*map_lex_lte_first(__isl_take isl_space
*dims
,
4372 unsigned n
, int equal
)
4374 struct isl_map
*map
;
4377 if (n
== 0 && equal
)
4378 return isl_map_universe(dims
);
4380 map
= isl_map_alloc_space(isl_space_copy(dims
), n
, ISL_MAP_DISJOINT
);
4382 for (i
= 0; i
+ 1 < n
; ++i
)
4383 map
= isl_map_add_basic_map(map
,
4384 isl_basic_map_less_at(isl_space_copy(dims
), i
));
4387 map
= isl_map_add_basic_map(map
,
4388 isl_basic_map_less_or_equal_at(dims
, n
- 1));
4390 map
= isl_map_add_basic_map(map
,
4391 isl_basic_map_less_at(dims
, n
- 1));
4393 isl_space_free(dims
);
4398 static __isl_give isl_map
*map_lex_lte(__isl_take isl_space
*dims
, int equal
)
4402 return map_lex_lte_first(dims
, dims
->n_out
, equal
);
4405 __isl_give isl_map
*isl_map_lex_lt_first(__isl_take isl_space
*dim
, unsigned n
)
4407 return map_lex_lte_first(dim
, n
, 0);
4410 __isl_give isl_map
*isl_map_lex_le_first(__isl_take isl_space
*dim
, unsigned n
)
4412 return map_lex_lte_first(dim
, n
, 1);
4415 __isl_give isl_map
*isl_map_lex_lt(__isl_take isl_space
*set_dim
)
4417 return map_lex_lte(isl_space_map_from_set(set_dim
), 0);
4420 __isl_give isl_map
*isl_map_lex_le(__isl_take isl_space
*set_dim
)
4422 return map_lex_lte(isl_space_map_from_set(set_dim
), 1);
4425 static __isl_give isl_map
*map_lex_gte_first(__isl_take isl_space
*dims
,
4426 unsigned n
, int equal
)
4428 struct isl_map
*map
;
4431 if (n
== 0 && equal
)
4432 return isl_map_universe(dims
);
4434 map
= isl_map_alloc_space(isl_space_copy(dims
), n
, ISL_MAP_DISJOINT
);
4436 for (i
= 0; i
+ 1 < n
; ++i
)
4437 map
= isl_map_add_basic_map(map
,
4438 isl_basic_map_more_at(isl_space_copy(dims
), i
));
4441 map
= isl_map_add_basic_map(map
,
4442 isl_basic_map_more_or_equal_at(dims
, n
- 1));
4444 map
= isl_map_add_basic_map(map
,
4445 isl_basic_map_more_at(dims
, n
- 1));
4447 isl_space_free(dims
);
4452 static __isl_give isl_map
*map_lex_gte(__isl_take isl_space
*dims
, int equal
)
4456 return map_lex_gte_first(dims
, dims
->n_out
, equal
);
4459 __isl_give isl_map
*isl_map_lex_gt_first(__isl_take isl_space
*dim
, unsigned n
)
4461 return map_lex_gte_first(dim
, n
, 0);
4464 __isl_give isl_map
*isl_map_lex_ge_first(__isl_take isl_space
*dim
, unsigned n
)
4466 return map_lex_gte_first(dim
, n
, 1);
4469 __isl_give isl_map
*isl_map_lex_gt(__isl_take isl_space
*set_dim
)
4471 return map_lex_gte(isl_space_map_from_set(set_dim
), 0);
4474 __isl_give isl_map
*isl_map_lex_ge(__isl_take isl_space
*set_dim
)
4476 return map_lex_gte(isl_space_map_from_set(set_dim
), 1);
4479 __isl_give isl_map
*isl_set_lex_le_set(__isl_take isl_set
*set1
,
4480 __isl_take isl_set
*set2
)
4483 map
= isl_map_lex_le(isl_set_get_space(set1
));
4484 map
= isl_map_intersect_domain(map
, set1
);
4485 map
= isl_map_intersect_range(map
, set2
);
4489 __isl_give isl_map
*isl_set_lex_lt_set(__isl_take isl_set
*set1
,
4490 __isl_take isl_set
*set2
)
4493 map
= isl_map_lex_lt(isl_set_get_space(set1
));
4494 map
= isl_map_intersect_domain(map
, set1
);
4495 map
= isl_map_intersect_range(map
, set2
);
4499 __isl_give isl_map
*isl_set_lex_ge_set(__isl_take isl_set
*set1
,
4500 __isl_take isl_set
*set2
)
4503 map
= isl_map_lex_ge(isl_set_get_space(set1
));
4504 map
= isl_map_intersect_domain(map
, set1
);
4505 map
= isl_map_intersect_range(map
, set2
);
4509 __isl_give isl_map
*isl_set_lex_gt_set(__isl_take isl_set
*set1
,
4510 __isl_take isl_set
*set2
)
4513 map
= isl_map_lex_gt(isl_set_get_space(set1
));
4514 map
= isl_map_intersect_domain(map
, set1
);
4515 map
= isl_map_intersect_range(map
, set2
);
4519 __isl_give isl_map
*isl_map_lex_le_map(__isl_take isl_map
*map1
,
4520 __isl_take isl_map
*map2
)
4523 map
= isl_map_lex_le(isl_space_range(isl_map_get_space(map1
)));
4524 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4525 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4529 __isl_give isl_map
*isl_map_lex_lt_map(__isl_take isl_map
*map1
,
4530 __isl_take isl_map
*map2
)
4533 map
= isl_map_lex_lt(isl_space_range(isl_map_get_space(map1
)));
4534 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4535 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4539 __isl_give isl_map
*isl_map_lex_ge_map(__isl_take isl_map
*map1
,
4540 __isl_take isl_map
*map2
)
4543 map
= isl_map_lex_ge(isl_space_range(isl_map_get_space(map1
)));
4544 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4545 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4549 __isl_give isl_map
*isl_map_lex_gt_map(__isl_take isl_map
*map1
,
4550 __isl_take isl_map
*map2
)
4553 map
= isl_map_lex_gt(isl_space_range(isl_map_get_space(map1
)));
4554 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4555 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4559 static __isl_give isl_basic_map
*basic_map_from_basic_set(
4560 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*dim
)
4562 struct isl_basic_map
*bmap
;
4564 bset
= isl_basic_set_cow(bset
);
4568 isl_assert(bset
->ctx
, isl_space_compatible_internal(bset
->dim
, dim
),
4570 isl_space_free(bset
->dim
);
4571 bmap
= bset_to_bmap(bset
);
4573 return isl_basic_map_finalize(bmap
);
4575 isl_basic_set_free(bset
);
4576 isl_space_free(dim
);
4580 __isl_give isl_basic_map
*isl_basic_map_from_basic_set(
4581 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*space
)
4583 return basic_map_from_basic_set(bset
, space
);
4586 /* For a div d = floor(f/m), add the constraint
4590 static int add_upper_div_constraint(__isl_keep isl_basic_map
*bmap
,
4591 unsigned pos
, isl_int
*div
)
4594 unsigned total
= isl_basic_map_total_dim(bmap
);
4596 i
= isl_basic_map_alloc_inequality(bmap
);
4599 isl_seq_cpy(bmap
->ineq
[i
], div
+ 1, 1 + total
);
4600 isl_int_neg(bmap
->ineq
[i
][1 + pos
], div
[0]);
4605 /* For a div d = floor(f/m), add the constraint
4607 * -(f-(m-1)) + m d >= 0
4609 static int add_lower_div_constraint(__isl_keep isl_basic_map
*bmap
,
4610 unsigned pos
, isl_int
*div
)
4613 unsigned total
= isl_basic_map_total_dim(bmap
);
4615 i
= isl_basic_map_alloc_inequality(bmap
);
4618 isl_seq_neg(bmap
->ineq
[i
], div
+ 1, 1 + total
);
4619 isl_int_set(bmap
->ineq
[i
][1 + pos
], div
[0]);
4620 isl_int_add(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], bmap
->ineq
[i
][1 + pos
]);
4621 isl_int_sub_ui(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], 1);
4626 /* For a div d = floor(f/m), add the constraints
4629 * -(f-(m-1)) + m d >= 0
4631 * Note that the second constraint is the negation of
4635 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map
*bmap
,
4636 unsigned pos
, isl_int
*div
)
4638 if (add_upper_div_constraint(bmap
, pos
, div
) < 0)
4640 if (add_lower_div_constraint(bmap
, pos
, div
) < 0)
4645 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set
*bset
,
4646 unsigned pos
, isl_int
*div
)
4648 return isl_basic_map_add_div_constraints_var(bset_to_bmap(bset
),
4652 int isl_basic_map_add_div_constraints(struct isl_basic_map
*bmap
, unsigned div
)
4654 unsigned total
= isl_basic_map_total_dim(bmap
);
4655 unsigned div_pos
= total
- bmap
->n_div
+ div
;
4657 return isl_basic_map_add_div_constraints_var(bmap
, div_pos
,
4661 /* For each known div d = floor(f/m), add the constraints
4664 * -(f-(m-1)) + m d >= 0
4666 * Remove duplicate constraints in case of some these div constraints
4667 * already appear in "bmap".
4669 __isl_give isl_basic_map
*isl_basic_map_add_known_div_constraints(
4670 __isl_take isl_basic_map
*bmap
)
4676 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4680 bmap
= add_known_div_constraints(bmap
);
4681 bmap
= isl_basic_map_remove_duplicate_constraints(bmap
, NULL
, 0);
4682 bmap
= isl_basic_map_finalize(bmap
);
4686 /* Add the div constraint of sign "sign" for div "div" of "bmap".
4688 * In particular, if this div is of the form d = floor(f/m),
4689 * then add the constraint
4693 * if sign < 0 or the constraint
4695 * -(f-(m-1)) + m d >= 0
4699 int isl_basic_map_add_div_constraint(__isl_keep isl_basic_map
*bmap
,
4700 unsigned div
, int sign
)
4708 total
= isl_basic_map_total_dim(bmap
);
4709 div_pos
= total
- bmap
->n_div
+ div
;
4712 return add_upper_div_constraint(bmap
, div_pos
, bmap
->div
[div
]);
4714 return add_lower_div_constraint(bmap
, div_pos
, bmap
->div
[div
]);
4717 int isl_basic_set_add_div_constraints(struct isl_basic_set
*bset
, unsigned div
)
4719 return isl_basic_map_add_div_constraints(bset
, div
);
4722 struct isl_basic_set
*isl_basic_map_underlying_set(
4723 struct isl_basic_map
*bmap
)
4727 if (bmap
->dim
->nparam
== 0 && bmap
->dim
->n_in
== 0 &&
4729 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_in
) &&
4730 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_out
))
4731 return bset_from_bmap(bmap
);
4732 bmap
= isl_basic_map_cow(bmap
);
4735 bmap
->dim
= isl_space_underlying(bmap
->dim
, bmap
->n_div
);
4738 bmap
->extra
-= bmap
->n_div
;
4740 bmap
= isl_basic_map_finalize(bmap
);
4741 return bset_from_bmap(bmap
);
4743 isl_basic_map_free(bmap
);
4747 __isl_give isl_basic_set
*isl_basic_set_underlying_set(
4748 __isl_take isl_basic_set
*bset
)
4750 return isl_basic_map_underlying_set(bset_to_bmap(bset
));
4753 /* Replace each element in "list" by the result of applying
4754 * isl_basic_map_underlying_set to the element.
4756 __isl_give isl_basic_set_list
*isl_basic_map_list_underlying_set(
4757 __isl_take isl_basic_map_list
*list
)
4764 n
= isl_basic_map_list_n_basic_map(list
);
4765 for (i
= 0; i
< n
; ++i
) {
4766 isl_basic_map
*bmap
;
4767 isl_basic_set
*bset
;
4769 bmap
= isl_basic_map_list_get_basic_map(list
, i
);
4770 bset
= isl_basic_set_underlying_set(bmap
);
4771 list
= isl_basic_set_list_set_basic_set(list
, i
, bset
);
4777 struct isl_basic_map
*isl_basic_map_overlying_set(
4778 struct isl_basic_set
*bset
, struct isl_basic_map
*like
)
4780 struct isl_basic_map
*bmap
;
4781 struct isl_ctx
*ctx
;
4788 isl_assert(ctx
, bset
->n_div
== 0, goto error
);
4789 isl_assert(ctx
, isl_basic_set_n_param(bset
) == 0, goto error
);
4790 isl_assert(ctx
, bset
->dim
->n_out
== isl_basic_map_total_dim(like
),
4792 if (like
->n_div
== 0) {
4793 isl_space
*space
= isl_basic_map_get_space(like
);
4794 isl_basic_map_free(like
);
4795 return isl_basic_map_reset_space(bset
, space
);
4797 bset
= isl_basic_set_cow(bset
);
4800 total
= bset
->dim
->n_out
+ bset
->extra
;
4801 bmap
= bset_to_bmap(bset
);
4802 isl_space_free(bmap
->dim
);
4803 bmap
->dim
= isl_space_copy(like
->dim
);
4806 bmap
->n_div
= like
->n_div
;
4807 bmap
->extra
+= like
->n_div
;
4811 ltotal
= total
- bmap
->extra
+ like
->extra
;
4814 bmap
->block2
= isl_blk_extend(ctx
, bmap
->block2
,
4815 bmap
->extra
* (1 + 1 + total
));
4816 if (isl_blk_is_error(bmap
->block2
))
4818 div
= isl_realloc_array(ctx
, bmap
->div
, isl_int
*, bmap
->extra
);
4822 for (i
= 0; i
< bmap
->extra
; ++i
)
4823 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + 1 + total
);
4824 for (i
= 0; i
< like
->n_div
; ++i
) {
4825 isl_seq_cpy(bmap
->div
[i
], like
->div
[i
], 1 + 1 + ltotal
);
4826 isl_seq_clr(bmap
->div
[i
]+1+1+ltotal
, total
- ltotal
);
4828 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
4830 isl_basic_map_free(like
);
4831 bmap
= isl_basic_map_simplify(bmap
);
4832 bmap
= isl_basic_map_finalize(bmap
);
4835 isl_basic_map_free(like
);
4836 isl_basic_set_free(bset
);
4840 struct isl_basic_set
*isl_basic_set_from_underlying_set(
4841 struct isl_basic_set
*bset
, struct isl_basic_set
*like
)
4843 return bset_from_bmap(isl_basic_map_overlying_set(bset
,
4844 bset_to_bmap(like
)));
4847 struct isl_set
*isl_set_from_underlying_set(
4848 struct isl_set
*set
, struct isl_basic_set
*like
)
4854 isl_assert(set
->ctx
, set
->dim
->n_out
== isl_basic_set_total_dim(like
),
4856 if (isl_space_is_equal(set
->dim
, like
->dim
) && like
->n_div
== 0) {
4857 isl_basic_set_free(like
);
4860 set
= isl_set_cow(set
);
4863 for (i
= 0; i
< set
->n
; ++i
) {
4864 set
->p
[i
] = isl_basic_set_from_underlying_set(set
->p
[i
],
4865 isl_basic_set_copy(like
));
4869 isl_space_free(set
->dim
);
4870 set
->dim
= isl_space_copy(like
->dim
);
4873 isl_basic_set_free(like
);
4876 isl_basic_set_free(like
);
4881 struct isl_set
*isl_map_underlying_set(struct isl_map
*map
)
4885 map
= isl_map_cow(map
);
4888 map
->dim
= isl_space_cow(map
->dim
);
4892 for (i
= 1; i
< map
->n
; ++i
)
4893 isl_assert(map
->ctx
, map
->p
[0]->n_div
== map
->p
[i
]->n_div
,
4895 for (i
= 0; i
< map
->n
; ++i
) {
4896 map
->p
[i
] = bset_to_bmap(
4897 isl_basic_map_underlying_set(map
->p
[i
]));
4902 map
->dim
= isl_space_underlying(map
->dim
, 0);
4904 isl_space_free(map
->dim
);
4905 map
->dim
= isl_space_copy(map
->p
[0]->dim
);
4909 return set_from_map(map
);
4915 struct isl_set
*isl_set_to_underlying_set(struct isl_set
*set
)
4917 return set_from_map(isl_map_underlying_set(set_to_map(set
)));
4920 /* Replace the space of "bmap" by "space".
4922 * If the space of "bmap" is identical to "space" (including the identifiers
4923 * of the input and output dimensions), then simply return the original input.
4925 __isl_give isl_basic_map
*isl_basic_map_reset_space(
4926 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*space
)
4932 equal
= isl_space_is_equal(bmap
->dim
, space
);
4933 if (equal
>= 0 && equal
)
4934 equal
= isl_space_match(bmap
->dim
, isl_dim_in
,
4936 if (equal
>= 0 && equal
)
4937 equal
= isl_space_match(bmap
->dim
, isl_dim_out
,
4938 space
, isl_dim_out
);
4942 isl_space_free(space
);
4945 bmap
= isl_basic_map_cow(bmap
);
4946 if (!bmap
|| !space
)
4949 isl_space_free(bmap
->dim
);
4952 bmap
= isl_basic_map_finalize(bmap
);
4956 isl_basic_map_free(bmap
);
4957 isl_space_free(space
);
4961 __isl_give isl_basic_set
*isl_basic_set_reset_space(
4962 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*dim
)
4964 return bset_from_bmap(isl_basic_map_reset_space(bset_to_bmap(bset
),
4968 __isl_give isl_map
*isl_map_reset_space(__isl_take isl_map
*map
,
4969 __isl_take isl_space
*dim
)
4973 map
= isl_map_cow(map
);
4977 for (i
= 0; i
< map
->n
; ++i
) {
4978 map
->p
[i
] = isl_basic_map_reset_space(map
->p
[i
],
4979 isl_space_copy(dim
));
4983 isl_space_free(map
->dim
);
4989 isl_space_free(dim
);
4993 __isl_give isl_set
*isl_set_reset_space(__isl_take isl_set
*set
,
4994 __isl_take isl_space
*dim
)
4996 return set_from_map(isl_map_reset_space(set_to_map(set
), dim
));
4999 /* Compute the parameter domain of the given basic set.
5001 __isl_give isl_basic_set
*isl_basic_set_params(__isl_take isl_basic_set
*bset
)
5006 if (isl_basic_set_is_params(bset
))
5009 n
= isl_basic_set_dim(bset
, isl_dim_set
);
5010 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
5011 space
= isl_basic_set_get_space(bset
);
5012 space
= isl_space_params(space
);
5013 bset
= isl_basic_set_reset_space(bset
, space
);
5017 /* Construct a zero-dimensional basic set with the given parameter domain.
5019 __isl_give isl_basic_set
*isl_basic_set_from_params(
5020 __isl_take isl_basic_set
*bset
)
5023 space
= isl_basic_set_get_space(bset
);
5024 space
= isl_space_set_from_params(space
);
5025 bset
= isl_basic_set_reset_space(bset
, space
);
5029 /* Compute the parameter domain of the given set.
5031 __isl_give isl_set
*isl_set_params(__isl_take isl_set
*set
)
5036 if (isl_set_is_params(set
))
5039 n
= isl_set_dim(set
, isl_dim_set
);
5040 set
= isl_set_project_out(set
, isl_dim_set
, 0, n
);
5041 space
= isl_set_get_space(set
);
5042 space
= isl_space_params(space
);
5043 set
= isl_set_reset_space(set
, space
);
5047 /* Construct a zero-dimensional set with the given parameter domain.
5049 __isl_give isl_set
*isl_set_from_params(__isl_take isl_set
*set
)
5052 space
= isl_set_get_space(set
);
5053 space
= isl_space_set_from_params(space
);
5054 set
= isl_set_reset_space(set
, space
);
5058 /* Compute the parameter domain of the given map.
5060 __isl_give isl_set
*isl_map_params(__isl_take isl_map
*map
)
5065 n
= isl_map_dim(map
, isl_dim_in
);
5066 map
= isl_map_project_out(map
, isl_dim_in
, 0, n
);
5067 n
= isl_map_dim(map
, isl_dim_out
);
5068 map
= isl_map_project_out(map
, isl_dim_out
, 0, n
);
5069 space
= isl_map_get_space(map
);
5070 space
= isl_space_params(space
);
5071 map
= isl_map_reset_space(map
, space
);
5075 struct isl_basic_set
*isl_basic_map_domain(struct isl_basic_map
*bmap
)
5082 space
= isl_space_domain(isl_basic_map_get_space(bmap
));
5084 n_out
= isl_basic_map_n_out(bmap
);
5085 bmap
= isl_basic_map_project_out(bmap
, isl_dim_out
, 0, n_out
);
5087 return isl_basic_map_reset_space(bmap
, space
);
5090 int isl_basic_map_may_be_set(__isl_keep isl_basic_map
*bmap
)
5094 return isl_space_may_be_set(bmap
->dim
);
5097 /* Is this basic map actually a set?
5098 * Users should never call this function. Outside of isl,
5099 * the type should indicate whether something is a set or a map.
5101 int isl_basic_map_is_set(__isl_keep isl_basic_map
*bmap
)
5105 return isl_space_is_set(bmap
->dim
);
5108 struct isl_basic_set
*isl_basic_map_range(struct isl_basic_map
*bmap
)
5112 if (isl_basic_map_is_set(bmap
))
5114 return isl_basic_map_domain(isl_basic_map_reverse(bmap
));
5117 __isl_give isl_basic_map
*isl_basic_map_domain_map(
5118 __isl_take isl_basic_map
*bmap
)
5122 isl_basic_map
*domain
;
5123 int nparam
, n_in
, n_out
;
5126 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5127 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5128 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5130 dim
= isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap
)));
5131 domain
= isl_basic_map_universe(dim
);
5133 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
5134 bmap
= isl_basic_map_apply_range(bmap
, domain
);
5135 bmap
= isl_basic_map_extend_constraints(bmap
, n_in
, 0);
5137 total
= isl_basic_map_total_dim(bmap
);
5139 for (i
= 0; i
< n_in
; ++i
) {
5140 k
= isl_basic_map_alloc_equality(bmap
);
5143 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
5144 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], -1);
5145 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ n_out
+ i
], 1);
5148 bmap
= isl_basic_map_gauss(bmap
, NULL
);
5149 return isl_basic_map_finalize(bmap
);
5151 isl_basic_map_free(bmap
);
5155 __isl_give isl_basic_map
*isl_basic_map_range_map(
5156 __isl_take isl_basic_map
*bmap
)
5160 isl_basic_map
*range
;
5161 int nparam
, n_in
, n_out
;
5164 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5165 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5166 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5168 dim
= isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap
)));
5169 range
= isl_basic_map_universe(dim
);
5171 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
5172 bmap
= isl_basic_map_apply_range(bmap
, range
);
5173 bmap
= isl_basic_map_extend_constraints(bmap
, n_out
, 0);
5175 total
= isl_basic_map_total_dim(bmap
);
5177 for (i
= 0; i
< n_out
; ++i
) {
5178 k
= isl_basic_map_alloc_equality(bmap
);
5181 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
5182 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ i
], -1);
5183 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n_in
+ n_out
+ i
], 1);
5186 bmap
= isl_basic_map_gauss(bmap
, NULL
);
5187 return isl_basic_map_finalize(bmap
);
5189 isl_basic_map_free(bmap
);
5193 int isl_map_may_be_set(__isl_keep isl_map
*map
)
5197 return isl_space_may_be_set(map
->dim
);
5200 /* Is this map actually a set?
5201 * Users should never call this function. Outside of isl,
5202 * the type should indicate whether something is a set or a map.
5204 int isl_map_is_set(__isl_keep isl_map
*map
)
5208 return isl_space_is_set(map
->dim
);
5211 struct isl_set
*isl_map_range(struct isl_map
*map
)
5214 struct isl_set
*set
;
5218 if (isl_map_is_set(map
))
5219 return set_from_map(map
);
5221 map
= isl_map_cow(map
);
5225 set
= set_from_map(map
);
5226 set
->dim
= isl_space_range(set
->dim
);
5229 for (i
= 0; i
< map
->n
; ++i
) {
5230 set
->p
[i
] = isl_basic_map_range(map
->p
[i
]);
5234 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
5235 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
5242 __isl_give isl_map
*isl_map_domain_map(__isl_take isl_map
*map
)
5246 map
= isl_map_cow(map
);
5250 map
->dim
= isl_space_domain_map(map
->dim
);
5253 for (i
= 0; i
< map
->n
; ++i
) {
5254 map
->p
[i
] = isl_basic_map_domain_map(map
->p
[i
]);
5258 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
5259 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5266 __isl_give isl_map
*isl_map_range_map(__isl_take isl_map
*map
)
5269 isl_space
*range_dim
;
5271 map
= isl_map_cow(map
);
5275 range_dim
= isl_space_range(isl_map_get_space(map
));
5276 range_dim
= isl_space_from_range(range_dim
);
5277 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
5278 map
->dim
= isl_space_join(map
->dim
, range_dim
);
5281 for (i
= 0; i
< map
->n
; ++i
) {
5282 map
->p
[i
] = isl_basic_map_range_map(map
->p
[i
]);
5286 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
5287 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5294 /* Given a wrapped map of the form A[B -> C],
5295 * return the map A[B -> C] -> B.
5297 __isl_give isl_map
*isl_set_wrapped_domain_map(__isl_take isl_set
*set
)
5304 if (!isl_set_has_tuple_id(set
))
5305 return isl_map_domain_map(isl_set_unwrap(set
));
5307 id
= isl_set_get_tuple_id(set
);
5308 map
= isl_map_domain_map(isl_set_unwrap(set
));
5309 map
= isl_map_set_tuple_id(map
, isl_dim_in
, id
);
5314 __isl_give isl_map
*isl_map_from_set(__isl_take isl_set
*set
,
5315 __isl_take isl_space
*dim
)
5318 struct isl_map
*map
= NULL
;
5320 set
= isl_set_cow(set
);
5323 isl_assert(set
->ctx
, isl_space_compatible_internal(set
->dim
, dim
),
5325 map
= set_to_map(set
);
5326 for (i
= 0; i
< set
->n
; ++i
) {
5327 map
->p
[i
] = basic_map_from_basic_set(
5328 set
->p
[i
], isl_space_copy(dim
));
5332 isl_space_free(map
->dim
);
5336 isl_space_free(dim
);
5341 __isl_give isl_basic_map
*isl_basic_map_from_domain(
5342 __isl_take isl_basic_set
*bset
)
5344 return isl_basic_map_reverse(isl_basic_map_from_range(bset
));
5347 __isl_give isl_basic_map
*isl_basic_map_from_range(
5348 __isl_take isl_basic_set
*bset
)
5351 space
= isl_basic_set_get_space(bset
);
5352 space
= isl_space_from_range(space
);
5353 bset
= isl_basic_set_reset_space(bset
, space
);
5354 return bset_to_bmap(bset
);
5357 /* Create a relation with the given set as range.
5358 * The domain of the created relation is a zero-dimensional
5359 * flat anonymous space.
5361 __isl_give isl_map
*isl_map_from_range(__isl_take isl_set
*set
)
5364 space
= isl_set_get_space(set
);
5365 space
= isl_space_from_range(space
);
5366 set
= isl_set_reset_space(set
, space
);
5367 return set_to_map(set
);
5370 /* Create a relation with the given set as domain.
5371 * The range of the created relation is a zero-dimensional
5372 * flat anonymous space.
5374 __isl_give isl_map
*isl_map_from_domain(__isl_take isl_set
*set
)
5376 return isl_map_reverse(isl_map_from_range(set
));
5379 __isl_give isl_basic_map
*isl_basic_map_from_domain_and_range(
5380 __isl_take isl_basic_set
*domain
, __isl_take isl_basic_set
*range
)
5382 return isl_basic_map_apply_range(isl_basic_map_reverse(domain
), range
);
5385 __isl_give isl_map
*isl_map_from_domain_and_range(__isl_take isl_set
*domain
,
5386 __isl_take isl_set
*range
)
5388 return isl_map_apply_range(isl_map_reverse(domain
), range
);
5391 /* Return a newly allocated isl_map with given space and flags and
5392 * room for "n" basic maps.
5393 * Make sure that all cached information is cleared.
5395 __isl_give isl_map
*isl_map_alloc_space(__isl_take isl_space
*space
, int n
,
5398 struct isl_map
*map
;
5403 isl_die(space
->ctx
, isl_error_internal
,
5404 "negative number of basic maps", goto error
);
5405 map
= isl_calloc(space
->ctx
, struct isl_map
,
5406 sizeof(struct isl_map
) +
5407 (n
- 1) * sizeof(struct isl_basic_map
*));
5411 map
->ctx
= space
->ctx
;
5412 isl_ctx_ref(map
->ctx
);
5420 isl_space_free(space
);
5424 struct isl_map
*isl_map_alloc(struct isl_ctx
*ctx
,
5425 unsigned nparam
, unsigned in
, unsigned out
, int n
,
5428 struct isl_map
*map
;
5431 dims
= isl_space_alloc(ctx
, nparam
, in
, out
);
5435 map
= isl_map_alloc_space(dims
, n
, flags
);
5439 __isl_give isl_basic_map
*isl_basic_map_empty(__isl_take isl_space
*dim
)
5441 struct isl_basic_map
*bmap
;
5442 bmap
= isl_basic_map_alloc_space(dim
, 0, 1, 0);
5443 bmap
= isl_basic_map_set_to_empty(bmap
);
5447 __isl_give isl_basic_set
*isl_basic_set_empty(__isl_take isl_space
*dim
)
5449 struct isl_basic_set
*bset
;
5450 bset
= isl_basic_set_alloc_space(dim
, 0, 1, 0);
5451 bset
= isl_basic_set_set_to_empty(bset
);
5455 __isl_give isl_basic_map
*isl_basic_map_universe(__isl_take isl_space
*dim
)
5457 struct isl_basic_map
*bmap
;
5458 bmap
= isl_basic_map_alloc_space(dim
, 0, 0, 0);
5459 bmap
= isl_basic_map_finalize(bmap
);
5463 __isl_give isl_basic_set
*isl_basic_set_universe(__isl_take isl_space
*dim
)
5465 struct isl_basic_set
*bset
;
5466 bset
= isl_basic_set_alloc_space(dim
, 0, 0, 0);
5467 bset
= isl_basic_set_finalize(bset
);
5471 __isl_give isl_basic_map
*isl_basic_map_nat_universe(__isl_take isl_space
*dim
)
5474 unsigned total
= isl_space_dim(dim
, isl_dim_all
);
5475 isl_basic_map
*bmap
;
5477 bmap
= isl_basic_map_alloc_space(dim
, 0, 0, total
);
5478 for (i
= 0; i
< total
; ++i
) {
5479 int k
= isl_basic_map_alloc_inequality(bmap
);
5482 isl_seq_clr(bmap
->ineq
[k
], 1 + total
);
5483 isl_int_set_si(bmap
->ineq
[k
][1 + i
], 1);
5487 isl_basic_map_free(bmap
);
5491 __isl_give isl_basic_set
*isl_basic_set_nat_universe(__isl_take isl_space
*dim
)
5493 return isl_basic_map_nat_universe(dim
);
5496 __isl_give isl_map
*isl_map_nat_universe(__isl_take isl_space
*dim
)
5498 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim
));
5501 __isl_give isl_set
*isl_set_nat_universe(__isl_take isl_space
*dim
)
5503 return isl_map_nat_universe(dim
);
5506 __isl_give isl_map
*isl_map_empty(__isl_take isl_space
*dim
)
5508 return isl_map_alloc_space(dim
, 0, ISL_MAP_DISJOINT
);
5511 __isl_give isl_set
*isl_set_empty(__isl_take isl_space
*dim
)
5513 return isl_set_alloc_space(dim
, 0, ISL_MAP_DISJOINT
);
5516 __isl_give isl_map
*isl_map_universe(__isl_take isl_space
*dim
)
5518 struct isl_map
*map
;
5521 map
= isl_map_alloc_space(isl_space_copy(dim
), 1, ISL_MAP_DISJOINT
);
5522 map
= isl_map_add_basic_map(map
, isl_basic_map_universe(dim
));
5526 __isl_give isl_set
*isl_set_universe(__isl_take isl_space
*dim
)
5528 struct isl_set
*set
;
5531 set
= isl_set_alloc_space(isl_space_copy(dim
), 1, ISL_MAP_DISJOINT
);
5532 set
= isl_set_add_basic_set(set
, isl_basic_set_universe(dim
));
5536 struct isl_map
*isl_map_dup(struct isl_map
*map
)
5539 struct isl_map
*dup
;
5543 dup
= isl_map_alloc_space(isl_space_copy(map
->dim
), map
->n
, map
->flags
);
5544 for (i
= 0; i
< map
->n
; ++i
)
5545 dup
= isl_map_add_basic_map(dup
, isl_basic_map_copy(map
->p
[i
]));
5549 __isl_give isl_map
*isl_map_add_basic_map(__isl_take isl_map
*map
,
5550 __isl_take isl_basic_map
*bmap
)
5554 if (isl_basic_map_plain_is_empty(bmap
)) {
5555 isl_basic_map_free(bmap
);
5558 isl_assert(map
->ctx
, isl_space_is_equal(map
->dim
, bmap
->dim
), goto error
);
5559 isl_assert(map
->ctx
, map
->n
< map
->size
, goto error
);
5560 map
->p
[map
->n
] = bmap
;
5562 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5568 isl_basic_map_free(bmap
);
5572 __isl_null isl_map
*isl_map_free(__isl_take isl_map
*map
)
5583 isl_ctx_deref(map
->ctx
);
5584 for (i
= 0; i
< map
->n
; ++i
)
5585 isl_basic_map_free(map
->p
[i
]);
5586 isl_space_free(map
->dim
);
5592 static struct isl_basic_map
*isl_basic_map_fix_pos_si(
5593 struct isl_basic_map
*bmap
, unsigned pos
, int value
)
5597 bmap
= isl_basic_map_cow(bmap
);
5598 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
5599 j
= isl_basic_map_alloc_equality(bmap
);
5602 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
5603 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
5604 isl_int_set_si(bmap
->eq
[j
][0], value
);
5605 bmap
= isl_basic_map_simplify(bmap
);
5606 return isl_basic_map_finalize(bmap
);
5608 isl_basic_map_free(bmap
);
5612 static __isl_give isl_basic_map
*isl_basic_map_fix_pos(
5613 __isl_take isl_basic_map
*bmap
, unsigned pos
, isl_int value
)
5617 bmap
= isl_basic_map_cow(bmap
);
5618 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
5619 j
= isl_basic_map_alloc_equality(bmap
);
5622 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
5623 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
5624 isl_int_set(bmap
->eq
[j
][0], value
);
5625 bmap
= isl_basic_map_simplify(bmap
);
5626 return isl_basic_map_finalize(bmap
);
5628 isl_basic_map_free(bmap
);
5632 struct isl_basic_map
*isl_basic_map_fix_si(struct isl_basic_map
*bmap
,
5633 enum isl_dim_type type
, unsigned pos
, int value
)
5637 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
5638 return isl_basic_map_fix_pos_si(bmap
,
5639 isl_basic_map_offset(bmap
, type
) + pos
, value
);
5641 isl_basic_map_free(bmap
);
5645 __isl_give isl_basic_map
*isl_basic_map_fix(__isl_take isl_basic_map
*bmap
,
5646 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5650 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
5651 return isl_basic_map_fix_pos(bmap
,
5652 isl_basic_map_offset(bmap
, type
) + pos
, value
);
5654 isl_basic_map_free(bmap
);
5658 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
5659 * to be equal to "v".
5661 __isl_give isl_basic_map
*isl_basic_map_fix_val(__isl_take isl_basic_map
*bmap
,
5662 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5666 if (!isl_val_is_int(v
))
5667 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
5668 "expecting integer value", goto error
);
5669 if (pos
>= isl_basic_map_dim(bmap
, type
))
5670 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
5671 "index out of bounds", goto error
);
5672 pos
+= isl_basic_map_offset(bmap
, type
);
5673 bmap
= isl_basic_map_fix_pos(bmap
, pos
, v
->n
);
5677 isl_basic_map_free(bmap
);
5682 /* Fix the value of the variable at position "pos" of type "type" of "bset"
5683 * to be equal to "v".
5685 __isl_give isl_basic_set
*isl_basic_set_fix_val(__isl_take isl_basic_set
*bset
,
5686 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5688 return isl_basic_map_fix_val(bset
, type
, pos
, v
);
5691 struct isl_basic_set
*isl_basic_set_fix_si(struct isl_basic_set
*bset
,
5692 enum isl_dim_type type
, unsigned pos
, int value
)
5694 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset
),
5698 __isl_give isl_basic_set
*isl_basic_set_fix(__isl_take isl_basic_set
*bset
,
5699 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5701 return bset_from_bmap(isl_basic_map_fix(bset_to_bmap(bset
),
5705 struct isl_basic_map
*isl_basic_map_fix_input_si(struct isl_basic_map
*bmap
,
5706 unsigned input
, int value
)
5708 return isl_basic_map_fix_si(bmap
, isl_dim_in
, input
, value
);
5711 struct isl_basic_set
*isl_basic_set_fix_dim_si(struct isl_basic_set
*bset
,
5712 unsigned dim
, int value
)
5714 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset
),
5715 isl_dim_set
, dim
, value
));
5718 static int remove_if_empty(__isl_keep isl_map
*map
, int i
)
5720 int empty
= isl_basic_map_plain_is_empty(map
->p
[i
]);
5727 isl_basic_map_free(map
->p
[i
]);
5728 if (i
!= map
->n
- 1) {
5729 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5730 map
->p
[i
] = map
->p
[map
->n
- 1];
5737 /* Perform "fn" on each basic map of "map", where we may not be holding
5738 * the only reference to "map".
5739 * In particular, "fn" should be a semantics preserving operation
5740 * that we want to apply to all copies of "map". We therefore need
5741 * to be careful not to modify "map" in a way that breaks "map"
5742 * in case anything goes wrong.
5744 __isl_give isl_map
*isl_map_inline_foreach_basic_map(__isl_take isl_map
*map
,
5745 __isl_give isl_basic_map
*(*fn
)(__isl_take isl_basic_map
*bmap
))
5747 struct isl_basic_map
*bmap
;
5753 for (i
= map
->n
- 1; i
>= 0; --i
) {
5754 bmap
= isl_basic_map_copy(map
->p
[i
]);
5758 isl_basic_map_free(map
->p
[i
]);
5760 if (remove_if_empty(map
, i
) < 0)
5770 struct isl_map
*isl_map_fix_si(struct isl_map
*map
,
5771 enum isl_dim_type type
, unsigned pos
, int value
)
5775 map
= isl_map_cow(map
);
5779 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
5780 for (i
= map
->n
- 1; i
>= 0; --i
) {
5781 map
->p
[i
] = isl_basic_map_fix_si(map
->p
[i
], type
, pos
, value
);
5782 if (remove_if_empty(map
, i
) < 0)
5785 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5792 __isl_give isl_set
*isl_set_fix_si(__isl_take isl_set
*set
,
5793 enum isl_dim_type type
, unsigned pos
, int value
)
5795 return set_from_map(isl_map_fix_si(set_to_map(set
), type
, pos
, value
));
5798 __isl_give isl_map
*isl_map_fix(__isl_take isl_map
*map
,
5799 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5803 map
= isl_map_cow(map
);
5807 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
5808 for (i
= 0; i
< map
->n
; ++i
) {
5809 map
->p
[i
] = isl_basic_map_fix(map
->p
[i
], type
, pos
, value
);
5813 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5820 __isl_give isl_set
*isl_set_fix(__isl_take isl_set
*set
,
5821 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5823 return set_from_map(isl_map_fix(set_to_map(set
), type
, pos
, value
));
5826 /* Fix the value of the variable at position "pos" of type "type" of "map"
5827 * to be equal to "v".
5829 __isl_give isl_map
*isl_map_fix_val(__isl_take isl_map
*map
,
5830 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5834 map
= isl_map_cow(map
);
5838 if (!isl_val_is_int(v
))
5839 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
5840 "expecting integer value", goto error
);
5841 if (pos
>= isl_map_dim(map
, type
))
5842 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
5843 "index out of bounds", goto error
);
5844 for (i
= map
->n
- 1; i
>= 0; --i
) {
5845 map
->p
[i
] = isl_basic_map_fix_val(map
->p
[i
], type
, pos
,
5847 if (remove_if_empty(map
, i
) < 0)
5850 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5859 /* Fix the value of the variable at position "pos" of type "type" of "set"
5860 * to be equal to "v".
5862 __isl_give isl_set
*isl_set_fix_val(__isl_take isl_set
*set
,
5863 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5865 return isl_map_fix_val(set
, type
, pos
, v
);
5868 struct isl_map
*isl_map_fix_input_si(struct isl_map
*map
,
5869 unsigned input
, int value
)
5871 return isl_map_fix_si(map
, isl_dim_in
, input
, value
);
5874 struct isl_set
*isl_set_fix_dim_si(struct isl_set
*set
, unsigned dim
, int value
)
5876 return set_from_map(isl_map_fix_si(set_to_map(set
),
5877 isl_dim_set
, dim
, value
));
5880 static __isl_give isl_basic_map
*basic_map_bound_si(
5881 __isl_take isl_basic_map
*bmap
,
5882 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
5888 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), goto error
);
5889 pos
+= isl_basic_map_offset(bmap
, type
);
5890 bmap
= isl_basic_map_cow(bmap
);
5891 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
5892 j
= isl_basic_map_alloc_inequality(bmap
);
5895 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
5897 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
5898 isl_int_set_si(bmap
->ineq
[j
][0], value
);
5900 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
5901 isl_int_set_si(bmap
->ineq
[j
][0], -value
);
5903 bmap
= isl_basic_map_simplify(bmap
);
5904 return isl_basic_map_finalize(bmap
);
5906 isl_basic_map_free(bmap
);
5910 __isl_give isl_basic_map
*isl_basic_map_lower_bound_si(
5911 __isl_take isl_basic_map
*bmap
,
5912 enum isl_dim_type type
, unsigned pos
, int value
)
5914 return basic_map_bound_si(bmap
, type
, pos
, value
, 0);
5917 /* Constrain the values of the given dimension to be no greater than "value".
5919 __isl_give isl_basic_map
*isl_basic_map_upper_bound_si(
5920 __isl_take isl_basic_map
*bmap
,
5921 enum isl_dim_type type
, unsigned pos
, int value
)
5923 return basic_map_bound_si(bmap
, type
, pos
, value
, 1);
5926 struct isl_basic_set
*isl_basic_set_lower_bound_dim(struct isl_basic_set
*bset
,
5927 unsigned dim
, isl_int value
)
5931 bset
= isl_basic_set_cow(bset
);
5932 bset
= isl_basic_set_extend_constraints(bset
, 0, 1);
5933 j
= isl_basic_set_alloc_inequality(bset
);
5936 isl_seq_clr(bset
->ineq
[j
], 1 + isl_basic_set_total_dim(bset
));
5937 isl_int_set_si(bset
->ineq
[j
][1 + isl_basic_set_n_param(bset
) + dim
], 1);
5938 isl_int_neg(bset
->ineq
[j
][0], value
);
5939 bset
= isl_basic_set_simplify(bset
);
5940 return isl_basic_set_finalize(bset
);
5942 isl_basic_set_free(bset
);
5946 static __isl_give isl_map
*map_bound_si(__isl_take isl_map
*map
,
5947 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
5951 map
= isl_map_cow(map
);
5955 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
5956 for (i
= 0; i
< map
->n
; ++i
) {
5957 map
->p
[i
] = basic_map_bound_si(map
->p
[i
],
5958 type
, pos
, value
, upper
);
5962 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5969 __isl_give isl_map
*isl_map_lower_bound_si(__isl_take isl_map
*map
,
5970 enum isl_dim_type type
, unsigned pos
, int value
)
5972 return map_bound_si(map
, type
, pos
, value
, 0);
5975 __isl_give isl_map
*isl_map_upper_bound_si(__isl_take isl_map
*map
,
5976 enum isl_dim_type type
, unsigned pos
, int value
)
5978 return map_bound_si(map
, type
, pos
, value
, 1);
5981 __isl_give isl_set
*isl_set_lower_bound_si(__isl_take isl_set
*set
,
5982 enum isl_dim_type type
, unsigned pos
, int value
)
5984 return set_from_map(isl_map_lower_bound_si(set_to_map(set
),
5988 __isl_give isl_set
*isl_set_upper_bound_si(__isl_take isl_set
*set
,
5989 enum isl_dim_type type
, unsigned pos
, int value
)
5991 return isl_map_upper_bound_si(set
, type
, pos
, value
);
5994 /* Bound the given variable of "bmap" from below (or above is "upper"
5995 * is set) to "value".
5997 static __isl_give isl_basic_map
*basic_map_bound(
5998 __isl_take isl_basic_map
*bmap
,
5999 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
6005 if (pos
>= isl_basic_map_dim(bmap
, type
))
6006 isl_die(bmap
->ctx
, isl_error_invalid
,
6007 "index out of bounds", goto error
);
6008 pos
+= isl_basic_map_offset(bmap
, type
);
6009 bmap
= isl_basic_map_cow(bmap
);
6010 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
6011 j
= isl_basic_map_alloc_inequality(bmap
);
6014 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
6016 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
6017 isl_int_set(bmap
->ineq
[j
][0], value
);
6019 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
6020 isl_int_neg(bmap
->ineq
[j
][0], value
);
6022 bmap
= isl_basic_map_simplify(bmap
);
6023 return isl_basic_map_finalize(bmap
);
6025 isl_basic_map_free(bmap
);
6029 /* Bound the given variable of "map" from below (or above is "upper"
6030 * is set) to "value".
6032 static __isl_give isl_map
*map_bound(__isl_take isl_map
*map
,
6033 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
6037 map
= isl_map_cow(map
);
6041 if (pos
>= isl_map_dim(map
, type
))
6042 isl_die(map
->ctx
, isl_error_invalid
,
6043 "index out of bounds", goto error
);
6044 for (i
= map
->n
- 1; i
>= 0; --i
) {
6045 map
->p
[i
] = basic_map_bound(map
->p
[i
], type
, pos
, value
, upper
);
6046 if (remove_if_empty(map
, i
) < 0)
6049 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6056 __isl_give isl_map
*isl_map_lower_bound(__isl_take isl_map
*map
,
6057 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6059 return map_bound(map
, type
, pos
, value
, 0);
6062 __isl_give isl_map
*isl_map_upper_bound(__isl_take isl_map
*map
,
6063 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6065 return map_bound(map
, type
, pos
, value
, 1);
6068 __isl_give isl_set
*isl_set_lower_bound(__isl_take isl_set
*set
,
6069 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6071 return isl_map_lower_bound(set
, type
, pos
, value
);
6074 __isl_give isl_set
*isl_set_upper_bound(__isl_take isl_set
*set
,
6075 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6077 return isl_map_upper_bound(set
, type
, pos
, value
);
6080 /* Force the values of the variable at position "pos" of type "type" of "set"
6081 * to be no smaller than "value".
6083 __isl_give isl_set
*isl_set_lower_bound_val(__isl_take isl_set
*set
,
6084 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
6088 if (!isl_val_is_int(value
))
6089 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
6090 "expecting integer value", goto error
);
6091 set
= isl_set_lower_bound(set
, type
, pos
, value
->n
);
6092 isl_val_free(value
);
6095 isl_val_free(value
);
6100 /* Force the values of the variable at position "pos" of type "type" of "set"
6101 * to be no greater than "value".
6103 __isl_give isl_set
*isl_set_upper_bound_val(__isl_take isl_set
*set
,
6104 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
6108 if (!isl_val_is_int(value
))
6109 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
6110 "expecting integer value", goto error
);
6111 set
= isl_set_upper_bound(set
, type
, pos
, value
->n
);
6112 isl_val_free(value
);
6115 isl_val_free(value
);
6120 struct isl_set
*isl_set_lower_bound_dim(struct isl_set
*set
, unsigned dim
,
6125 set
= isl_set_cow(set
);
6129 isl_assert(set
->ctx
, dim
< isl_set_n_dim(set
), goto error
);
6130 for (i
= 0; i
< set
->n
; ++i
) {
6131 set
->p
[i
] = isl_basic_set_lower_bound_dim(set
->p
[i
], dim
, value
);
6141 struct isl_map
*isl_map_reverse(struct isl_map
*map
)
6145 map
= isl_map_cow(map
);
6149 map
->dim
= isl_space_reverse(map
->dim
);
6152 for (i
= 0; i
< map
->n
; ++i
) {
6153 map
->p
[i
] = isl_basic_map_reverse(map
->p
[i
]);
6157 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6165 #define TYPE isl_pw_multi_aff
6167 #define SUFFIX _pw_multi_aff
6169 #define EMPTY isl_pw_multi_aff_empty
6171 #define ADD isl_pw_multi_aff_union_add
6172 #include "isl_map_lexopt_templ.c"
6174 /* Given a map "map", compute the lexicographically minimal
6175 * (or maximal) image element for each domain element in dom,
6176 * in the form of an isl_pw_multi_aff.
6177 * If "empty" is not NULL, then set *empty to those elements in dom that
6178 * do not have an image element.
6179 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6180 * should be computed over the domain of "map". "empty" is also NULL
6183 * We first compute the lexicographically minimal or maximal element
6184 * in the first basic map. This results in a partial solution "res"
6185 * and a subset "todo" of dom that still need to be handled.
6186 * We then consider each of the remaining maps in "map" and successively
6187 * update both "res" and "todo".
6188 * If "empty" is NULL, then the todo sets are not needed and therefore
6189 * also not computed.
6191 static __isl_give isl_pw_multi_aff
*isl_map_partial_lexopt_aligned_pw_multi_aff(
6192 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6193 __isl_give isl_set
**empty
, unsigned flags
)
6197 isl_pw_multi_aff
*res
;
6200 full
= ISL_FL_ISSET(flags
, ISL_OPT_FULL
);
6201 if (!map
|| (!full
&& !dom
))
6204 if (isl_map_plain_is_empty(map
)) {
6209 return isl_pw_multi_aff_from_map(map
);
6212 res
= basic_map_partial_lexopt_pw_multi_aff(
6213 isl_basic_map_copy(map
->p
[0]),
6214 isl_set_copy(dom
), empty
, flags
);
6218 for (i
= 1; i
< map
->n
; ++i
) {
6219 isl_pw_multi_aff
*res_i
;
6221 res_i
= basic_map_partial_lexopt_pw_multi_aff(
6222 isl_basic_map_copy(map
->p
[i
]),
6223 isl_set_copy(dom
), empty
, flags
);
6225 if (ISL_FL_ISSET(flags
, ISL_OPT_MAX
))
6226 res
= isl_pw_multi_aff_union_lexmax(res
, res_i
);
6228 res
= isl_pw_multi_aff_union_lexmin(res
, res_i
);
6231 todo
= isl_set_intersect(todo
, *empty
);
6250 #define TYPE isl_map
6254 #define EMPTY isl_map_empty
6256 #define ADD isl_map_union_disjoint
6257 #include "isl_map_lexopt_templ.c"
6259 /* Given a map "map", compute the lexicographically minimal
6260 * (or maximal) image element for each domain element in "dom",
6261 * in the form of an isl_map.
6262 * If "empty" is not NULL, then set *empty to those elements in "dom" that
6263 * do not have an image element.
6264 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6265 * should be computed over the domain of "map". "empty" is also NULL
6268 * If the input consists of more than one disjunct, then first
6269 * compute the desired result in the form of an isl_pw_multi_aff and
6270 * then convert that into an isl_map.
6272 * This function used to have an explicit implementation in terms
6273 * of isl_maps, but it would continually intersect the domains of
6274 * partial results with the complement of the domain of the next
6275 * partial solution, potentially leading to an explosion in the number
6276 * of disjuncts if there are several disjuncts in the input.
6277 * An even earlier implementation of this function would look for
6278 * better results in the domain of the partial result and for extra
6279 * results in the complement of this domain, which would lead to
6280 * even more splintering.
6282 static __isl_give isl_map
*isl_map_partial_lexopt_aligned(
6283 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6284 __isl_give isl_set
**empty
, unsigned flags
)
6287 struct isl_map
*res
;
6288 isl_pw_multi_aff
*pma
;
6290 full
= ISL_FL_ISSET(flags
, ISL_OPT_FULL
);
6291 if (!map
|| (!full
&& !dom
))
6294 if (isl_map_plain_is_empty(map
)) {
6303 res
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[0]),
6309 pma
= isl_map_partial_lexopt_aligned_pw_multi_aff(map
, dom
, empty
,
6311 return isl_map_from_pw_multi_aff(pma
);
6320 __isl_give isl_map
*isl_map_partial_lexmax(
6321 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6322 __isl_give isl_set
**empty
)
6324 return isl_map_partial_lexopt(map
, dom
, empty
, ISL_OPT_MAX
);
6327 __isl_give isl_map
*isl_map_partial_lexmin(
6328 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6329 __isl_give isl_set
**empty
)
6331 return isl_map_partial_lexopt(map
, dom
, empty
, 0);
6334 __isl_give isl_set
*isl_set_partial_lexmin(
6335 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
6336 __isl_give isl_set
**empty
)
6338 return set_from_map(isl_map_partial_lexmin(set_to_map(set
),
6342 __isl_give isl_set
*isl_set_partial_lexmax(
6343 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
6344 __isl_give isl_set
**empty
)
6346 return set_from_map(isl_map_partial_lexmax(set_to_map(set
),
6350 /* Compute the lexicographic minimum (or maximum if "flags" includes
6351 * ISL_OPT_MAX) of "bset" over its parametric domain.
6353 __isl_give isl_set
*isl_basic_set_lexopt(__isl_take isl_basic_set
*bset
,
6356 return isl_basic_map_lexopt(bset
, flags
);
6359 __isl_give isl_map
*isl_basic_map_lexmax(__isl_take isl_basic_map
*bmap
)
6361 return isl_basic_map_lexopt(bmap
, ISL_OPT_MAX
);
6364 __isl_give isl_set
*isl_basic_set_lexmin(__isl_take isl_basic_set
*bset
)
6366 return set_from_map(isl_basic_map_lexmin(bset_to_bmap(bset
)));
6369 __isl_give isl_set
*isl_basic_set_lexmax(__isl_take isl_basic_set
*bset
)
6371 return set_from_map(isl_basic_map_lexmax(bset_to_bmap(bset
)));
6374 /* Compute the lexicographic minimum of "bset" over its parametric domain
6375 * for the purpose of quantifier elimination.
6376 * That is, find an explicit representation for all the existentially
6377 * quantified variables in "bset" by computing their lexicographic
6380 static __isl_give isl_set
*isl_basic_set_lexmin_compute_divs(
6381 __isl_take isl_basic_set
*bset
)
6383 return isl_basic_set_lexopt(bset
, ISL_OPT_QE
);
6386 /* Extract the first and only affine expression from list
6387 * and then add it to *pwaff with the given dom.
6388 * This domain is known to be disjoint from other domains
6389 * because of the way isl_basic_map_foreach_lexmax works.
6391 static int update_dim_opt(__isl_take isl_basic_set
*dom
,
6392 __isl_take isl_aff_list
*list
, void *user
)
6394 isl_ctx
*ctx
= isl_basic_set_get_ctx(dom
);
6396 isl_pw_aff
**pwaff
= user
;
6397 isl_pw_aff
*pwaff_i
;
6401 if (isl_aff_list_n_aff(list
) != 1)
6402 isl_die(ctx
, isl_error_internal
,
6403 "expecting single element list", goto error
);
6405 aff
= isl_aff_list_get_aff(list
, 0);
6406 pwaff_i
= isl_pw_aff_alloc(isl_set_from_basic_set(dom
), aff
);
6408 *pwaff
= isl_pw_aff_add_disjoint(*pwaff
, pwaff_i
);
6410 isl_aff_list_free(list
);
6414 isl_basic_set_free(dom
);
6415 isl_aff_list_free(list
);
6419 /* Given a basic map with one output dimension, compute the minimum or
6420 * maximum of that dimension as an isl_pw_aff.
6422 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
6423 * call update_dim_opt on each leaf of the result.
6425 static __isl_give isl_pw_aff
*basic_map_dim_opt(__isl_keep isl_basic_map
*bmap
,
6428 isl_space
*dim
= isl_basic_map_get_space(bmap
);
6432 dim
= isl_space_from_domain(isl_space_domain(dim
));
6433 dim
= isl_space_add_dims(dim
, isl_dim_out
, 1);
6434 pwaff
= isl_pw_aff_empty(dim
);
6436 r
= isl_basic_map_foreach_lexopt(bmap
, max
, &update_dim_opt
, &pwaff
);
6438 return isl_pw_aff_free(pwaff
);
6443 /* Compute the minimum or maximum of the given output dimension
6444 * as a function of the parameters and the input dimensions,
6445 * but independently of the other output dimensions.
6447 * We first project out the other output dimension and then compute
6448 * the "lexicographic" maximum in each basic map, combining the results
6449 * using isl_pw_aff_union_max.
6451 static __isl_give isl_pw_aff
*map_dim_opt(__isl_take isl_map
*map
, int pos
,
6458 n_out
= isl_map_dim(map
, isl_dim_out
);
6459 map
= isl_map_project_out(map
, isl_dim_out
, pos
+ 1, n_out
- (pos
+ 1));
6460 map
= isl_map_project_out(map
, isl_dim_out
, 0, pos
);
6465 isl_space
*dim
= isl_map_get_space(map
);
6467 return isl_pw_aff_empty(dim
);
6470 pwaff
= basic_map_dim_opt(map
->p
[0], max
);
6471 for (i
= 1; i
< map
->n
; ++i
) {
6472 isl_pw_aff
*pwaff_i
;
6474 pwaff_i
= basic_map_dim_opt(map
->p
[i
], max
);
6475 pwaff
= isl_pw_aff_union_opt(pwaff
, pwaff_i
, max
);
6483 /* Compute the minimum of the given output dimension as a function of the
6484 * parameters and input dimensions, but independently of
6485 * the other output dimensions.
6487 __isl_give isl_pw_aff
*isl_map_dim_min(__isl_take isl_map
*map
, int pos
)
6489 return map_dim_opt(map
, pos
, 0);
6492 /* Compute the maximum of the given output dimension as a function of the
6493 * parameters and input dimensions, but independently of
6494 * the other output dimensions.
6496 __isl_give isl_pw_aff
*isl_map_dim_max(__isl_take isl_map
*map
, int pos
)
6498 return map_dim_opt(map
, pos
, 1);
6501 /* Compute the minimum or maximum of the given set dimension
6502 * as a function of the parameters,
6503 * but independently of the other set dimensions.
6505 static __isl_give isl_pw_aff
*set_dim_opt(__isl_take isl_set
*set
, int pos
,
6508 return map_dim_opt(set
, pos
, max
);
6511 /* Compute the maximum of the given set dimension as a function of the
6512 * parameters, but independently of the other set dimensions.
6514 __isl_give isl_pw_aff
*isl_set_dim_max(__isl_take isl_set
*set
, int pos
)
6516 return set_dim_opt(set
, pos
, 1);
6519 /* Compute the minimum of the given set dimension as a function of the
6520 * parameters, but independently of the other set dimensions.
6522 __isl_give isl_pw_aff
*isl_set_dim_min(__isl_take isl_set
*set
, int pos
)
6524 return set_dim_opt(set
, pos
, 0);
6527 /* Apply a preimage specified by "mat" on the parameters of "bset".
6528 * bset is assumed to have only parameters and divs.
6530 static struct isl_basic_set
*basic_set_parameter_preimage(
6531 struct isl_basic_set
*bset
, struct isl_mat
*mat
)
6538 bset
->dim
= isl_space_cow(bset
->dim
);
6542 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
6544 isl_assert(bset
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
6546 bset
->dim
->nparam
= 0;
6547 bset
->dim
->n_out
= nparam
;
6548 bset
= isl_basic_set_preimage(bset
, mat
);
6550 bset
->dim
->nparam
= bset
->dim
->n_out
;
6551 bset
->dim
->n_out
= 0;
6556 isl_basic_set_free(bset
);
6560 /* Apply a preimage specified by "mat" on the parameters of "set".
6561 * set is assumed to have only parameters and divs.
6563 static __isl_give isl_set
*set_parameter_preimage(__isl_take isl_set
*set
,
6564 __isl_take isl_mat
*mat
)
6572 nparam
= isl_set_dim(set
, isl_dim_param
);
6574 if (mat
->n_row
!= 1 + nparam
)
6575 isl_die(isl_set_get_ctx(set
), isl_error_internal
,
6576 "unexpected number of rows", goto error
);
6578 space
= isl_set_get_space(set
);
6579 space
= isl_space_move_dims(space
, isl_dim_set
, 0,
6580 isl_dim_param
, 0, nparam
);
6581 set
= isl_set_reset_space(set
, space
);
6582 set
= isl_set_preimage(set
, mat
);
6583 nparam
= isl_set_dim(set
, isl_dim_out
);
6584 space
= isl_set_get_space(set
);
6585 space
= isl_space_move_dims(space
, isl_dim_param
, 0,
6586 isl_dim_out
, 0, nparam
);
6587 set
= isl_set_reset_space(set
, space
);
6595 /* Intersect the basic set "bset" with the affine space specified by the
6596 * equalities in "eq".
6598 static struct isl_basic_set
*basic_set_append_equalities(
6599 struct isl_basic_set
*bset
, struct isl_mat
*eq
)
6607 bset
= isl_basic_set_extend_space(bset
, isl_space_copy(bset
->dim
), 0,
6612 len
= 1 + isl_space_dim(bset
->dim
, isl_dim_all
) + bset
->extra
;
6613 for (i
= 0; i
< eq
->n_row
; ++i
) {
6614 k
= isl_basic_set_alloc_equality(bset
);
6617 isl_seq_cpy(bset
->eq
[k
], eq
->row
[i
], eq
->n_col
);
6618 isl_seq_clr(bset
->eq
[k
] + eq
->n_col
, len
- eq
->n_col
);
6622 bset
= isl_basic_set_gauss(bset
, NULL
);
6623 bset
= isl_basic_set_finalize(bset
);
6628 isl_basic_set_free(bset
);
6632 /* Intersect the set "set" with the affine space specified by the
6633 * equalities in "eq".
6635 static struct isl_set
*set_append_equalities(struct isl_set
*set
,
6643 for (i
= 0; i
< set
->n
; ++i
) {
6644 set
->p
[i
] = basic_set_append_equalities(set
->p
[i
],
6657 /* Given a basic set "bset" that only involves parameters and existentially
6658 * quantified variables, return the index of the first equality
6659 * that only involves parameters. If there is no such equality then
6660 * return bset->n_eq.
6662 * This function assumes that isl_basic_set_gauss has been called on "bset".
6664 static int first_parameter_equality(__isl_keep isl_basic_set
*bset
)
6667 unsigned nparam
, n_div
;
6672 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
6673 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
6675 for (i
= 0, j
= n_div
- 1; i
< bset
->n_eq
&& j
>= 0; --j
) {
6676 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ j
]))
6683 /* Compute an explicit representation for the existentially quantified
6684 * variables in "bset" by computing the "minimal value" of the set
6685 * variables. Since there are no set variables, the computation of
6686 * the minimal value essentially computes an explicit representation
6687 * of the non-empty part(s) of "bset".
6689 * The input only involves parameters and existentially quantified variables.
6690 * All equalities among parameters have been removed.
6692 * Since the existentially quantified variables in the result are in general
6693 * going to be different from those in the input, we first replace
6694 * them by the minimal number of variables based on their equalities.
6695 * This should simplify the parametric integer programming.
6697 static __isl_give isl_set
*base_compute_divs(__isl_take isl_basic_set
*bset
)
6699 isl_morph
*morph1
, *morph2
;
6705 if (bset
->n_eq
== 0)
6706 return isl_basic_set_lexmin_compute_divs(bset
);
6708 morph1
= isl_basic_set_parameter_compression(bset
);
6709 bset
= isl_morph_basic_set(isl_morph_copy(morph1
), bset
);
6710 bset
= isl_basic_set_lift(bset
);
6711 morph2
= isl_basic_set_variable_compression(bset
, isl_dim_set
);
6712 bset
= isl_morph_basic_set(morph2
, bset
);
6713 n
= isl_basic_set_dim(bset
, isl_dim_set
);
6714 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
6716 set
= isl_basic_set_lexmin_compute_divs(bset
);
6718 set
= isl_morph_set(isl_morph_inverse(morph1
), set
);
6723 /* Project the given basic set onto its parameter domain, possibly introducing
6724 * new, explicit, existential variables in the constraints.
6725 * The input has parameters and (possibly implicit) existential variables.
6726 * The output has the same parameters, but only
6727 * explicit existentially quantified variables.
6729 * The actual projection is performed by pip, but pip doesn't seem
6730 * to like equalities very much, so we first remove the equalities
6731 * among the parameters by performing a variable compression on
6732 * the parameters. Afterward, an inverse transformation is performed
6733 * and the equalities among the parameters are inserted back in.
6735 * The variable compression on the parameters may uncover additional
6736 * equalities that were only implicit before. We therefore check
6737 * if there are any new parameter equalities in the result and
6738 * if so recurse. The removal of parameter equalities is required
6739 * for the parameter compression performed by base_compute_divs.
6741 static struct isl_set
*parameter_compute_divs(struct isl_basic_set
*bset
)
6745 struct isl_mat
*T
, *T2
;
6746 struct isl_set
*set
;
6749 bset
= isl_basic_set_cow(bset
);
6753 if (bset
->n_eq
== 0)
6754 return base_compute_divs(bset
);
6756 bset
= isl_basic_set_gauss(bset
, NULL
);
6759 if (isl_basic_set_plain_is_empty(bset
))
6760 return isl_set_from_basic_set(bset
);
6762 i
= first_parameter_equality(bset
);
6763 if (i
== bset
->n_eq
)
6764 return base_compute_divs(bset
);
6766 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
6767 eq
= isl_mat_sub_alloc6(bset
->ctx
, bset
->eq
, i
, bset
->n_eq
- i
,
6769 eq
= isl_mat_cow(eq
);
6770 T
= isl_mat_variable_compression(isl_mat_copy(eq
), &T2
);
6771 if (T
&& T
->n_col
== 0) {
6775 bset
= isl_basic_set_set_to_empty(bset
);
6776 return isl_set_from_basic_set(bset
);
6778 bset
= basic_set_parameter_preimage(bset
, T
);
6780 i
= first_parameter_equality(bset
);
6783 else if (i
== bset
->n_eq
)
6784 set
= base_compute_divs(bset
);
6786 set
= parameter_compute_divs(bset
);
6787 set
= set_parameter_preimage(set
, T2
);
6788 set
= set_append_equalities(set
, eq
);
6792 /* Insert the divs from "ls" before those of "bmap".
6794 * The number of columns is not changed, which means that the last
6795 * dimensions of "bmap" are being reintepreted as the divs from "ls".
6796 * The caller is responsible for removing the same number of dimensions
6797 * from the space of "bmap".
6799 static __isl_give isl_basic_map
*insert_divs_from_local_space(
6800 __isl_take isl_basic_map
*bmap
, __isl_keep isl_local_space
*ls
)
6806 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
6810 old_n_div
= bmap
->n_div
;
6811 bmap
= insert_div_rows(bmap
, n_div
);
6815 for (i
= 0; i
< n_div
; ++i
) {
6816 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], ls
->div
->n_col
);
6817 isl_seq_clr(bmap
->div
[i
] + ls
->div
->n_col
, old_n_div
);
6823 /* Replace the space of "bmap" by the space and divs of "ls".
6825 * If "ls" has any divs, then we simplify the result since we may
6826 * have discovered some additional equalities that could simplify
6827 * the div expressions.
6829 static __isl_give isl_basic_map
*basic_replace_space_by_local_space(
6830 __isl_take isl_basic_map
*bmap
, __isl_take isl_local_space
*ls
)
6834 bmap
= isl_basic_map_cow(bmap
);
6838 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
6839 bmap
= insert_divs_from_local_space(bmap
, ls
);
6843 isl_space_free(bmap
->dim
);
6844 bmap
->dim
= isl_local_space_get_space(ls
);
6848 isl_local_space_free(ls
);
6850 bmap
= isl_basic_map_simplify(bmap
);
6851 bmap
= isl_basic_map_finalize(bmap
);
6854 isl_basic_map_free(bmap
);
6855 isl_local_space_free(ls
);
6859 /* Replace the space of "map" by the space and divs of "ls".
6861 static __isl_give isl_map
*replace_space_by_local_space(__isl_take isl_map
*map
,
6862 __isl_take isl_local_space
*ls
)
6866 map
= isl_map_cow(map
);
6870 for (i
= 0; i
< map
->n
; ++i
) {
6871 map
->p
[i
] = basic_replace_space_by_local_space(map
->p
[i
],
6872 isl_local_space_copy(ls
));
6876 isl_space_free(map
->dim
);
6877 map
->dim
= isl_local_space_get_space(ls
);
6881 isl_local_space_free(ls
);
6884 isl_local_space_free(ls
);
6889 /* Compute an explicit representation for the existentially
6890 * quantified variables for which do not know any explicit representation yet.
6892 * We first sort the existentially quantified variables so that the
6893 * existentially quantified variables for which we already have an explicit
6894 * representation are placed before those for which we do not.
6895 * The input dimensions, the output dimensions and the existentially
6896 * quantified variables for which we already have an explicit
6897 * representation are then turned into parameters.
6898 * compute_divs returns a map with the same parameters and
6899 * no input or output dimensions and the dimension specification
6900 * is reset to that of the input, including the existentially quantified
6901 * variables for which we already had an explicit representation.
6903 static struct isl_map
*compute_divs(struct isl_basic_map
*bmap
)
6905 struct isl_basic_set
*bset
;
6906 struct isl_set
*set
;
6907 struct isl_map
*map
;
6909 isl_local_space
*ls
;
6916 bmap
= isl_basic_map_sort_divs(bmap
);
6917 bmap
= isl_basic_map_cow(bmap
);
6921 n_known
= isl_basic_map_first_unknown_div(bmap
);
6923 return isl_map_from_basic_map(isl_basic_map_free(bmap
));
6925 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
6926 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
6927 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
6928 dim
= isl_space_set_alloc(bmap
->ctx
,
6929 nparam
+ n_in
+ n_out
+ n_known
, 0);
6933 ls
= isl_basic_map_get_local_space(bmap
);
6934 ls
= isl_local_space_drop_dims(ls
, isl_dim_div
,
6935 n_known
, bmap
->n_div
- n_known
);
6937 for (i
= n_known
; i
< bmap
->n_div
; ++i
)
6938 swap_div(bmap
, i
- n_known
, i
);
6939 bmap
->n_div
-= n_known
;
6940 bmap
->extra
-= n_known
;
6942 bmap
= isl_basic_map_reset_space(bmap
, dim
);
6943 bset
= bset_from_bmap(bmap
);
6945 set
= parameter_compute_divs(bset
);
6946 map
= set_to_map(set
);
6947 map
= replace_space_by_local_space(map
, ls
);
6951 isl_basic_map_free(bmap
);
6955 /* Remove the explicit representation of local variable "div",
6958 __isl_give isl_basic_map
*isl_basic_map_mark_div_unknown(
6959 __isl_take isl_basic_map
*bmap
, int div
)
6963 unknown
= isl_basic_map_div_is_marked_unknown(bmap
, div
);
6965 return isl_basic_map_free(bmap
);
6969 bmap
= isl_basic_map_cow(bmap
);
6972 isl_int_set_si(bmap
->div
[div
][0], 0);
6976 /* Is local variable "div" of "bmap" marked as not having an explicit
6978 * Note that even if "div" is not marked in this way and therefore
6979 * has an explicit representation, this representation may still
6980 * depend (indirectly) on other local variables that do not
6981 * have an explicit representation.
6983 isl_bool
isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map
*bmap
,
6987 return isl_bool_error
;
6988 if (div
< 0 || div
>= isl_basic_map_dim(bmap
, isl_dim_div
))
6989 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
6990 "position out of bounds", return isl_bool_error
);
6991 return isl_int_is_zero(bmap
->div
[div
][0]);
6994 /* Return the position of the first local variable that does not
6995 * have an explicit representation.
6996 * Return the total number of local variables if they all have
6997 * an explicit representation.
6998 * Return -1 on error.
7000 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map
*bmap
)
7007 for (i
= 0; i
< bmap
->n_div
; ++i
) {
7008 if (!isl_basic_map_div_is_known(bmap
, i
))
7014 /* Return the position of the first local variable that does not
7015 * have an explicit representation.
7016 * Return the total number of local variables if they all have
7017 * an explicit representation.
7018 * Return -1 on error.
7020 int isl_basic_set_first_unknown_div(__isl_keep isl_basic_set
*bset
)
7022 return isl_basic_map_first_unknown_div(bset
);
7025 /* Does "bmap" have an explicit representation for all local variables?
7027 isl_bool
isl_basic_map_divs_known(__isl_keep isl_basic_map
*bmap
)
7031 n
= isl_basic_map_dim(bmap
, isl_dim_div
);
7032 first
= isl_basic_map_first_unknown_div(bmap
);
7034 return isl_bool_error
;
7038 /* Do all basic maps in "map" have an explicit representation
7039 * for all local variables?
7041 isl_bool
isl_map_divs_known(__isl_keep isl_map
*map
)
7046 return isl_bool_error
;
7048 for (i
= 0; i
< map
->n
; ++i
) {
7049 int known
= isl_basic_map_divs_known(map
->p
[i
]);
7054 return isl_bool_true
;
7057 /* If bmap contains any unknown divs, then compute explicit
7058 * expressions for them. However, this computation may be
7059 * quite expensive, so first try to remove divs that aren't
7062 struct isl_map
*isl_basic_map_compute_divs(struct isl_basic_map
*bmap
)
7065 struct isl_map
*map
;
7067 known
= isl_basic_map_divs_known(bmap
);
7071 return isl_map_from_basic_map(bmap
);
7073 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
7075 known
= isl_basic_map_divs_known(bmap
);
7079 return isl_map_from_basic_map(bmap
);
7081 map
= compute_divs(bmap
);
7084 isl_basic_map_free(bmap
);
7088 struct isl_map
*isl_map_compute_divs(struct isl_map
*map
)
7092 struct isl_map
*res
;
7099 known
= isl_map_divs_known(map
);
7107 res
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[0]));
7108 for (i
= 1 ; i
< map
->n
; ++i
) {
7110 r2
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[i
]));
7111 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
7112 res
= isl_map_union_disjoint(res
, r2
);
7114 res
= isl_map_union(res
, r2
);
7121 struct isl_set
*isl_basic_set_compute_divs(struct isl_basic_set
*bset
)
7123 return set_from_map(isl_basic_map_compute_divs(bset_to_bmap(bset
)));
7126 struct isl_set
*isl_set_compute_divs(struct isl_set
*set
)
7128 return set_from_map(isl_map_compute_divs(set_to_map(set
)));
7131 struct isl_set
*isl_map_domain(struct isl_map
*map
)
7134 struct isl_set
*set
;
7139 map
= isl_map_cow(map
);
7143 set
= set_from_map(map
);
7144 set
->dim
= isl_space_domain(set
->dim
);
7147 for (i
= 0; i
< map
->n
; ++i
) {
7148 set
->p
[i
] = isl_basic_map_domain(map
->p
[i
]);
7152 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
7153 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
7160 /* Return the union of "map1" and "map2", where we assume for now that
7161 * "map1" and "map2" are disjoint. Note that the basic maps inside
7162 * "map1" or "map2" may not be disjoint from each other.
7163 * Also note that this function is also called from isl_map_union,
7164 * which takes care of handling the situation where "map1" and "map2"
7165 * may not be disjoint.
7167 * If one of the inputs is empty, we can simply return the other input.
7168 * Similarly, if one of the inputs is universal, then it is equal to the union.
7170 static __isl_give isl_map
*map_union_disjoint(__isl_take isl_map
*map1
,
7171 __isl_take isl_map
*map2
)
7175 struct isl_map
*map
= NULL
;
7181 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
7182 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
7183 "spaces don't match", goto error
);
7194 is_universe
= isl_map_plain_is_universe(map1
);
7195 if (is_universe
< 0)
7202 is_universe
= isl_map_plain_is_universe(map2
);
7203 if (is_universe
< 0)
7210 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
7211 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
7212 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
7214 map
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
7215 map1
->n
+ map2
->n
, flags
);
7218 for (i
= 0; i
< map1
->n
; ++i
) {
7219 map
= isl_map_add_basic_map(map
,
7220 isl_basic_map_copy(map1
->p
[i
]));
7224 for (i
= 0; i
< map2
->n
; ++i
) {
7225 map
= isl_map_add_basic_map(map
,
7226 isl_basic_map_copy(map2
->p
[i
]));
7240 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7241 * guaranteed to be disjoint by the caller.
7243 * Note that this functions is called from within isl_map_make_disjoint,
7244 * so we have to be careful not to touch the constraints of the inputs
7247 __isl_give isl_map
*isl_map_union_disjoint(__isl_take isl_map
*map1
,
7248 __isl_take isl_map
*map2
)
7250 return isl_map_align_params_map_map_and(map1
, map2
, &map_union_disjoint
);
7253 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7254 * not be disjoint. The parameters are assumed to have been aligned.
7256 * We currently simply call map_union_disjoint, the internal operation
7257 * of which does not really depend on the inputs being disjoint.
7258 * If the result contains more than one basic map, then we clear
7259 * the disjoint flag since the result may contain basic maps from
7260 * both inputs and these are not guaranteed to be disjoint.
7262 * As a special case, if "map1" and "map2" are obviously equal,
7263 * then we simply return "map1".
7265 static __isl_give isl_map
*map_union_aligned(__isl_take isl_map
*map1
,
7266 __isl_take isl_map
*map2
)
7273 equal
= isl_map_plain_is_equal(map1
, map2
);
7281 map1
= map_union_disjoint(map1
, map2
);
7285 ISL_F_CLR(map1
, ISL_MAP_DISJOINT
);
7293 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7296 __isl_give isl_map
*isl_map_union(__isl_take isl_map
*map1
,
7297 __isl_take isl_map
*map2
)
7299 return isl_map_align_params_map_map_and(map1
, map2
, &map_union_aligned
);
7302 struct isl_set
*isl_set_union_disjoint(
7303 struct isl_set
*set1
, struct isl_set
*set2
)
7305 return set_from_map(isl_map_union_disjoint(set_to_map(set1
),
7309 struct isl_set
*isl_set_union(struct isl_set
*set1
, struct isl_set
*set2
)
7311 return set_from_map(isl_map_union(set_to_map(set1
), set_to_map(set2
)));
7314 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7317 * "map" and "set" are assumed to be compatible and non-NULL.
7319 static __isl_give isl_map
*map_intersect_set(__isl_take isl_map
*map
,
7320 __isl_take isl_set
*set
,
7321 __isl_give isl_basic_map
*fn(__isl_take isl_basic_map
*bmap
,
7322 __isl_take isl_basic_set
*bset
))
7325 struct isl_map
*result
;
7328 if (isl_set_plain_is_universe(set
)) {
7333 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
) &&
7334 ISL_F_ISSET(set
, ISL_MAP_DISJOINT
))
7335 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
7337 result
= isl_map_alloc_space(isl_space_copy(map
->dim
),
7338 map
->n
* set
->n
, flags
);
7339 for (i
= 0; result
&& i
< map
->n
; ++i
)
7340 for (j
= 0; j
< set
->n
; ++j
) {
7341 result
= isl_map_add_basic_map(result
,
7342 fn(isl_basic_map_copy(map
->p
[i
]),
7343 isl_basic_set_copy(set
->p
[j
])));
7353 static __isl_give isl_map
*map_intersect_range(__isl_take isl_map
*map
,
7354 __isl_take isl_set
*set
)
7359 if (!isl_map_compatible_range(map
, set
))
7360 isl_die(set
->ctx
, isl_error_invalid
,
7361 "incompatible spaces", goto error
);
7363 return map_intersect_set(map
, set
, &isl_basic_map_intersect_range
);
7370 __isl_give isl_map
*isl_map_intersect_range(__isl_take isl_map
*map
,
7371 __isl_take isl_set
*set
)
7373 return isl_map_align_params_map_map_and(map
, set
, &map_intersect_range
);
7376 static __isl_give isl_map
*map_intersect_domain(__isl_take isl_map
*map
,
7377 __isl_take isl_set
*set
)
7382 if (!isl_map_compatible_domain(map
, set
))
7383 isl_die(set
->ctx
, isl_error_invalid
,
7384 "incompatible spaces", goto error
);
7386 return map_intersect_set(map
, set
, &isl_basic_map_intersect_domain
);
7393 __isl_give isl_map
*isl_map_intersect_domain(__isl_take isl_map
*map
,
7394 __isl_take isl_set
*set
)
7396 return isl_map_align_params_map_map_and(map
, set
,
7397 &map_intersect_domain
);
7400 static __isl_give isl_map
*map_apply_domain(__isl_take isl_map
*map1
,
7401 __isl_take isl_map
*map2
)
7405 map1
= isl_map_reverse(map1
);
7406 map1
= isl_map_apply_range(map1
, map2
);
7407 return isl_map_reverse(map1
);
7414 __isl_give isl_map
*isl_map_apply_domain(__isl_take isl_map
*map1
,
7415 __isl_take isl_map
*map2
)
7417 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_domain
);
7420 static __isl_give isl_map
*map_apply_range(__isl_take isl_map
*map1
,
7421 __isl_take isl_map
*map2
)
7423 isl_space
*dim_result
;
7424 struct isl_map
*result
;
7430 dim_result
= isl_space_join(isl_space_copy(map1
->dim
),
7431 isl_space_copy(map2
->dim
));
7433 result
= isl_map_alloc_space(dim_result
, map1
->n
* map2
->n
, 0);
7436 for (i
= 0; i
< map1
->n
; ++i
)
7437 for (j
= 0; j
< map2
->n
; ++j
) {
7438 result
= isl_map_add_basic_map(result
,
7439 isl_basic_map_apply_range(
7440 isl_basic_map_copy(map1
->p
[i
]),
7441 isl_basic_map_copy(map2
->p
[j
])));
7447 if (result
&& result
->n
<= 1)
7448 ISL_F_SET(result
, ISL_MAP_DISJOINT
);
7456 __isl_give isl_map
*isl_map_apply_range(__isl_take isl_map
*map1
,
7457 __isl_take isl_map
*map2
)
7459 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_range
);
7463 * returns range - domain
7465 struct isl_basic_set
*isl_basic_map_deltas(struct isl_basic_map
*bmap
)
7467 isl_space
*target_space
;
7468 struct isl_basic_set
*bset
;
7475 isl_assert(bmap
->ctx
, isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
7476 bmap
->dim
, isl_dim_out
),
7478 target_space
= isl_space_domain(isl_basic_map_get_space(bmap
));
7479 dim
= isl_basic_map_n_in(bmap
);
7480 nparam
= isl_basic_map_n_param(bmap
);
7481 bmap
= isl_basic_map_from_range(isl_basic_map_wrap(bmap
));
7482 bmap
= isl_basic_map_add_dims(bmap
, isl_dim_in
, dim
);
7483 bmap
= isl_basic_map_extend_constraints(bmap
, dim
, 0);
7484 for (i
= 0; i
< dim
; ++i
) {
7485 int j
= isl_basic_map_alloc_equality(bmap
);
7487 bmap
= isl_basic_map_free(bmap
);
7490 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
7491 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
7492 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], 1);
7493 isl_int_set_si(bmap
->eq
[j
][1+nparam
+2*dim
+i
], -1);
7495 bset
= isl_basic_map_domain(bmap
);
7496 bset
= isl_basic_set_reset_space(bset
, target_space
);
7499 isl_basic_map_free(bmap
);
7504 * returns range - domain
7506 __isl_give isl_set
*isl_map_deltas(__isl_take isl_map
*map
)
7510 struct isl_set
*result
;
7515 isl_assert(map
->ctx
, isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
7516 map
->dim
, isl_dim_out
),
7518 dim
= isl_map_get_space(map
);
7519 dim
= isl_space_domain(dim
);
7520 result
= isl_set_alloc_space(dim
, map
->n
, 0);
7523 for (i
= 0; i
< map
->n
; ++i
)
7524 result
= isl_set_add_basic_set(result
,
7525 isl_basic_map_deltas(isl_basic_map_copy(map
->p
[i
])));
7534 * returns [domain -> range] -> range - domain
7536 __isl_give isl_basic_map
*isl_basic_map_deltas_map(
7537 __isl_take isl_basic_map
*bmap
)
7541 isl_basic_map
*domain
;
7545 if (!isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
7546 bmap
->dim
, isl_dim_out
))
7547 isl_die(bmap
->ctx
, isl_error_invalid
,
7548 "domain and range don't match", goto error
);
7550 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
7551 n
= isl_basic_map_dim(bmap
, isl_dim_in
);
7553 dim
= isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap
)));
7554 domain
= isl_basic_map_universe(dim
);
7556 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
7557 bmap
= isl_basic_map_apply_range(bmap
, domain
);
7558 bmap
= isl_basic_map_extend_constraints(bmap
, n
, 0);
7560 total
= isl_basic_map_total_dim(bmap
);
7562 for (i
= 0; i
< n
; ++i
) {
7563 k
= isl_basic_map_alloc_equality(bmap
);
7566 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
7567 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], 1);
7568 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ i
], -1);
7569 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ n
+ i
], 1);
7572 bmap
= isl_basic_map_gauss(bmap
, NULL
);
7573 return isl_basic_map_finalize(bmap
);
7575 isl_basic_map_free(bmap
);
7580 * returns [domain -> range] -> range - domain
7582 __isl_give isl_map
*isl_map_deltas_map(__isl_take isl_map
*map
)
7585 isl_space
*domain_dim
;
7590 if (!isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
7591 map
->dim
, isl_dim_out
))
7592 isl_die(map
->ctx
, isl_error_invalid
,
7593 "domain and range don't match", goto error
);
7595 map
= isl_map_cow(map
);
7599 domain_dim
= isl_space_from_range(isl_space_domain(isl_map_get_space(map
)));
7600 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
7601 map
->dim
= isl_space_join(map
->dim
, domain_dim
);
7604 for (i
= 0; i
< map
->n
; ++i
) {
7605 map
->p
[i
] = isl_basic_map_deltas_map(map
->p
[i
]);
7609 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
7616 static __isl_give isl_basic_map
*basic_map_identity(__isl_take isl_space
*dims
)
7618 struct isl_basic_map
*bmap
;
7626 nparam
= dims
->nparam
;
7628 bmap
= isl_basic_map_alloc_space(dims
, 0, dim
, 0);
7632 for (i
= 0; i
< dim
; ++i
) {
7633 int j
= isl_basic_map_alloc_equality(bmap
);
7636 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
7637 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
7638 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], -1);
7640 return isl_basic_map_finalize(bmap
);
7642 isl_basic_map_free(bmap
);
7646 __isl_give isl_basic_map
*isl_basic_map_identity(__isl_take isl_space
*dim
)
7650 if (dim
->n_in
!= dim
->n_out
)
7651 isl_die(dim
->ctx
, isl_error_invalid
,
7652 "number of input and output dimensions needs to be "
7653 "the same", goto error
);
7654 return basic_map_identity(dim
);
7656 isl_space_free(dim
);
7660 __isl_give isl_map
*isl_map_identity(__isl_take isl_space
*dim
)
7662 return isl_map_from_basic_map(isl_basic_map_identity(dim
));
7665 __isl_give isl_map
*isl_set_identity(__isl_take isl_set
*set
)
7667 isl_space
*dim
= isl_set_get_space(set
);
7669 id
= isl_map_identity(isl_space_map_from_set(dim
));
7670 return isl_map_intersect_range(id
, set
);
7673 /* Construct a basic set with all set dimensions having only non-negative
7676 __isl_give isl_basic_set
*isl_basic_set_positive_orthant(
7677 __isl_take isl_space
*space
)
7682 struct isl_basic_set
*bset
;
7686 nparam
= space
->nparam
;
7688 bset
= isl_basic_set_alloc_space(space
, 0, 0, dim
);
7691 for (i
= 0; i
< dim
; ++i
) {
7692 int k
= isl_basic_set_alloc_inequality(bset
);
7695 isl_seq_clr(bset
->ineq
[k
], 1 + isl_basic_set_total_dim(bset
));
7696 isl_int_set_si(bset
->ineq
[k
][1 + nparam
+ i
], 1);
7700 isl_basic_set_free(bset
);
7704 /* Construct the half-space x_pos >= 0.
7706 static __isl_give isl_basic_set
*nonneg_halfspace(__isl_take isl_space
*dim
,
7710 isl_basic_set
*nonneg
;
7712 nonneg
= isl_basic_set_alloc_space(dim
, 0, 0, 1);
7713 k
= isl_basic_set_alloc_inequality(nonneg
);
7716 isl_seq_clr(nonneg
->ineq
[k
], 1 + isl_basic_set_total_dim(nonneg
));
7717 isl_int_set_si(nonneg
->ineq
[k
][pos
], 1);
7719 return isl_basic_set_finalize(nonneg
);
7721 isl_basic_set_free(nonneg
);
7725 /* Construct the half-space x_pos <= -1.
7727 static __isl_give isl_basic_set
*neg_halfspace(__isl_take isl_space
*dim
, int pos
)
7732 neg
= isl_basic_set_alloc_space(dim
, 0, 0, 1);
7733 k
= isl_basic_set_alloc_inequality(neg
);
7736 isl_seq_clr(neg
->ineq
[k
], 1 + isl_basic_set_total_dim(neg
));
7737 isl_int_set_si(neg
->ineq
[k
][0], -1);
7738 isl_int_set_si(neg
->ineq
[k
][pos
], -1);
7740 return isl_basic_set_finalize(neg
);
7742 isl_basic_set_free(neg
);
7746 __isl_give isl_set
*isl_set_split_dims(__isl_take isl_set
*set
,
7747 enum isl_dim_type type
, unsigned first
, unsigned n
)
7751 isl_basic_set
*nonneg
;
7759 isl_assert(set
->ctx
, first
+ n
<= isl_set_dim(set
, type
), goto error
);
7761 offset
= pos(set
->dim
, type
);
7762 for (i
= 0; i
< n
; ++i
) {
7763 nonneg
= nonneg_halfspace(isl_set_get_space(set
),
7764 offset
+ first
+ i
);
7765 neg
= neg_halfspace(isl_set_get_space(set
), offset
+ first
+ i
);
7767 set
= isl_set_intersect(set
, isl_basic_set_union(nonneg
, neg
));
7776 static int foreach_orthant(__isl_take isl_set
*set
, int *signs
, int first
,
7777 int len
, int (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
7784 if (isl_set_plain_is_empty(set
)) {
7789 return fn(set
, signs
, user
);
7792 half
= isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set
),
7794 half
= isl_set_intersect(half
, isl_set_copy(set
));
7795 if (foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
) < 0)
7799 half
= isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set
),
7801 half
= isl_set_intersect(half
, set
);
7802 return foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
);
7808 /* Call "fn" on the intersections of "set" with each of the orthants
7809 * (except for obviously empty intersections). The orthant is identified
7810 * by the signs array, with each entry having value 1 or -1 according
7811 * to the sign of the corresponding variable.
7813 int isl_set_foreach_orthant(__isl_keep isl_set
*set
,
7814 int (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
7824 if (isl_set_plain_is_empty(set
))
7827 nparam
= isl_set_dim(set
, isl_dim_param
);
7828 nvar
= isl_set_dim(set
, isl_dim_set
);
7830 signs
= isl_alloc_array(set
->ctx
, int, nparam
+ nvar
);
7832 r
= foreach_orthant(isl_set_copy(set
), signs
, 0, nparam
+ nvar
,
7840 isl_bool
isl_set_is_equal(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
7842 return isl_map_is_equal(set_to_map(set1
), set_to_map(set2
));
7845 isl_bool
isl_basic_map_is_subset(__isl_keep isl_basic_map
*bmap1
,
7846 __isl_keep isl_basic_map
*bmap2
)
7849 struct isl_map
*map1
;
7850 struct isl_map
*map2
;
7852 if (!bmap1
|| !bmap2
)
7853 return isl_bool_error
;
7855 map1
= isl_map_from_basic_map(isl_basic_map_copy(bmap1
));
7856 map2
= isl_map_from_basic_map(isl_basic_map_copy(bmap2
));
7858 is_subset
= isl_map_is_subset(map1
, map2
);
7866 isl_bool
isl_basic_set_is_subset(__isl_keep isl_basic_set
*bset1
,
7867 __isl_keep isl_basic_set
*bset2
)
7869 return isl_basic_map_is_subset(bset1
, bset2
);
7872 isl_bool
isl_basic_map_is_equal(__isl_keep isl_basic_map
*bmap1
,
7873 __isl_keep isl_basic_map
*bmap2
)
7877 if (!bmap1
|| !bmap2
)
7878 return isl_bool_error
;
7879 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
7880 if (is_subset
!= isl_bool_true
)
7882 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
7886 isl_bool
isl_basic_set_is_equal(__isl_keep isl_basic_set
*bset1
,
7887 __isl_keep isl_basic_set
*bset2
)
7889 return isl_basic_map_is_equal(
7890 bset_to_bmap(bset1
), bset_to_bmap(bset2
));
7893 isl_bool
isl_map_is_empty(__isl_keep isl_map
*map
)
7899 return isl_bool_error
;
7900 for (i
= 0; i
< map
->n
; ++i
) {
7901 is_empty
= isl_basic_map_is_empty(map
->p
[i
]);
7903 return isl_bool_error
;
7905 return isl_bool_false
;
7907 return isl_bool_true
;
7910 isl_bool
isl_map_plain_is_empty(__isl_keep isl_map
*map
)
7912 return map
? map
->n
== 0 : isl_bool_error
;
7915 isl_bool
isl_set_plain_is_empty(__isl_keep isl_set
*set
)
7917 return set
? set
->n
== 0 : isl_bool_error
;
7920 isl_bool
isl_set_is_empty(__isl_keep isl_set
*set
)
7922 return isl_map_is_empty(set_to_map(set
));
7925 int isl_map_has_equal_space(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
7930 return isl_space_is_equal(map1
->dim
, map2
->dim
);
7933 int isl_set_has_equal_space(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
7938 return isl_space_is_equal(set1
->dim
, set2
->dim
);
7941 static isl_bool
map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
7946 return isl_bool_error
;
7947 is_subset
= isl_map_is_subset(map1
, map2
);
7948 if (is_subset
!= isl_bool_true
)
7950 is_subset
= isl_map_is_subset(map2
, map1
);
7954 /* Is "map1" equal to "map2"?
7956 * First check if they are obviously equal.
7957 * If not, then perform a more detailed analysis.
7959 isl_bool
isl_map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
7963 equal
= isl_map_plain_is_equal(map1
, map2
);
7964 if (equal
< 0 || equal
)
7966 return isl_map_align_params_map_map_and_test(map1
, map2
, &map_is_equal
);
7969 isl_bool
isl_basic_map_is_strict_subset(
7970 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
7974 if (!bmap1
|| !bmap2
)
7975 return isl_bool_error
;
7976 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
7977 if (is_subset
!= isl_bool_true
)
7979 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
7980 if (is_subset
== isl_bool_error
)
7985 isl_bool
isl_map_is_strict_subset(__isl_keep isl_map
*map1
,
7986 __isl_keep isl_map
*map2
)
7991 return isl_bool_error
;
7992 is_subset
= isl_map_is_subset(map1
, map2
);
7993 if (is_subset
!= isl_bool_true
)
7995 is_subset
= isl_map_is_subset(map2
, map1
);
7996 if (is_subset
== isl_bool_error
)
8001 isl_bool
isl_set_is_strict_subset(__isl_keep isl_set
*set1
,
8002 __isl_keep isl_set
*set2
)
8004 return isl_map_is_strict_subset(set_to_map(set1
), set_to_map(set2
));
8007 /* Is "bmap" obviously equal to the universe with the same space?
8009 * That is, does it not have any constraints?
8011 isl_bool
isl_basic_map_plain_is_universe(__isl_keep isl_basic_map
*bmap
)
8014 return isl_bool_error
;
8015 return bmap
->n_eq
== 0 && bmap
->n_ineq
== 0;
8018 /* Is "bset" obviously equal to the universe with the same space?
8020 isl_bool
isl_basic_set_plain_is_universe(__isl_keep isl_basic_set
*bset
)
8022 return isl_basic_map_plain_is_universe(bset
);
8025 /* If "c" does not involve any existentially quantified variables,
8026 * then set *univ to false and abort
8028 static isl_stat
involves_divs(__isl_take isl_constraint
*c
, void *user
)
8030 isl_bool
*univ
= user
;
8033 n
= isl_constraint_dim(c
, isl_dim_div
);
8034 *univ
= isl_constraint_involves_dims(c
, isl_dim_div
, 0, n
);
8035 isl_constraint_free(c
);
8036 if (*univ
< 0 || !*univ
)
8037 return isl_stat_error
;
8041 /* Is "bmap" equal to the universe with the same space?
8043 * First check if it is obviously equal to the universe.
8044 * If not and if there are any constraints not involving
8045 * existentially quantified variables, then it is certainly
8046 * not equal to the universe.
8047 * Otherwise, check if the universe is a subset of "bmap".
8049 isl_bool
isl_basic_map_is_universe(__isl_keep isl_basic_map
*bmap
)
8052 isl_basic_map
*test
;
8054 univ
= isl_basic_map_plain_is_universe(bmap
);
8055 if (univ
< 0 || univ
)
8057 if (isl_basic_map_dim(bmap
, isl_dim_div
) == 0)
8058 return isl_bool_false
;
8059 univ
= isl_bool_true
;
8060 if (isl_basic_map_foreach_constraint(bmap
, &involves_divs
, &univ
) < 0 &&
8062 return isl_bool_error
;
8063 if (univ
< 0 || !univ
)
8065 test
= isl_basic_map_universe(isl_basic_map_get_space(bmap
));
8066 univ
= isl_basic_map_is_subset(test
, bmap
);
8067 isl_basic_map_free(test
);
8071 /* Is "bset" equal to the universe with the same space?
8073 isl_bool
isl_basic_set_is_universe(__isl_keep isl_basic_set
*bset
)
8075 return isl_basic_map_is_universe(bset
);
8078 isl_bool
isl_map_plain_is_universe(__isl_keep isl_map
*map
)
8083 return isl_bool_error
;
8085 for (i
= 0; i
< map
->n
; ++i
) {
8086 isl_bool r
= isl_basic_map_plain_is_universe(map
->p
[i
]);
8091 return isl_bool_false
;
8094 isl_bool
isl_set_plain_is_universe(__isl_keep isl_set
*set
)
8096 return isl_map_plain_is_universe(set_to_map(set
));
8099 isl_bool
isl_basic_map_is_empty(__isl_keep isl_basic_map
*bmap
)
8101 struct isl_basic_set
*bset
= NULL
;
8102 struct isl_vec
*sample
= NULL
;
8103 isl_bool empty
, non_empty
;
8106 return isl_bool_error
;
8108 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
8109 return isl_bool_true
;
8111 if (isl_basic_map_plain_is_universe(bmap
))
8112 return isl_bool_false
;
8114 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
8115 struct isl_basic_map
*copy
= isl_basic_map_copy(bmap
);
8116 copy
= isl_basic_map_remove_redundancies(copy
);
8117 empty
= isl_basic_map_plain_is_empty(copy
);
8118 isl_basic_map_free(copy
);
8122 non_empty
= isl_basic_map_plain_is_non_empty(bmap
);
8124 return isl_bool_error
;
8126 return isl_bool_false
;
8127 isl_vec_free(bmap
->sample
);
8128 bmap
->sample
= NULL
;
8129 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
8131 return isl_bool_error
;
8132 sample
= isl_basic_set_sample_vec(bset
);
8134 return isl_bool_error
;
8135 empty
= sample
->size
== 0;
8136 isl_vec_free(bmap
->sample
);
8137 bmap
->sample
= sample
;
8139 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
8144 isl_bool
isl_basic_map_plain_is_empty(__isl_keep isl_basic_map
*bmap
)
8147 return isl_bool_error
;
8148 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
);
8151 isl_bool
isl_basic_set_plain_is_empty(__isl_keep isl_basic_set
*bset
)
8154 return isl_bool_error
;
8155 return ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
);
8158 /* Is "bmap" known to be non-empty?
8160 * That is, is the cached sample still valid?
8162 isl_bool
isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map
*bmap
)
8167 return isl_bool_error
;
8169 return isl_bool_false
;
8170 total
= 1 + isl_basic_map_total_dim(bmap
);
8171 if (bmap
->sample
->size
!= total
)
8172 return isl_bool_false
;
8173 return isl_basic_map_contains(bmap
, bmap
->sample
);
8176 isl_bool
isl_basic_set_is_empty(__isl_keep isl_basic_set
*bset
)
8178 return isl_basic_map_is_empty(bset_to_bmap(bset
));
8181 struct isl_map
*isl_basic_map_union(
8182 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
8184 struct isl_map
*map
;
8185 if (!bmap1
|| !bmap2
)
8188 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
8190 map
= isl_map_alloc_space(isl_space_copy(bmap1
->dim
), 2, 0);
8193 map
= isl_map_add_basic_map(map
, bmap1
);
8194 map
= isl_map_add_basic_map(map
, bmap2
);
8197 isl_basic_map_free(bmap1
);
8198 isl_basic_map_free(bmap2
);
8202 struct isl_set
*isl_basic_set_union(
8203 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
8205 return set_from_map(isl_basic_map_union(bset_to_bmap(bset1
),
8206 bset_to_bmap(bset2
)));
8209 /* Order divs such that any div only depends on previous divs */
8210 struct isl_basic_map
*isl_basic_map_order_divs(struct isl_basic_map
*bmap
)
8218 off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
8220 for (i
= 0; i
< bmap
->n_div
; ++i
) {
8222 if (isl_int_is_zero(bmap
->div
[i
][0]))
8224 pos
= isl_seq_first_non_zero(bmap
->div
[i
]+1+1+off
+i
,
8229 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_internal
,
8230 "integer division depends on itself",
8231 return isl_basic_map_free(bmap
));
8232 isl_basic_map_swap_div(bmap
, i
, i
+ pos
);
8238 struct isl_basic_set
*isl_basic_set_order_divs(struct isl_basic_set
*bset
)
8240 return bset_from_bmap(isl_basic_map_order_divs(bset_to_bmap(bset
)));
8243 __isl_give isl_map
*isl_map_order_divs(__isl_take isl_map
*map
)
8250 for (i
= 0; i
< map
->n
; ++i
) {
8251 map
->p
[i
] = isl_basic_map_order_divs(map
->p
[i
]);
8262 /* Sort the local variables of "bset".
8264 __isl_give isl_basic_set
*isl_basic_set_sort_divs(
8265 __isl_take isl_basic_set
*bset
)
8267 return bset_from_bmap(isl_basic_map_sort_divs(bset_to_bmap(bset
)));
8270 /* Apply the expansion computed by isl_merge_divs.
8271 * The expansion itself is given by "exp" while the resulting
8272 * list of divs is given by "div".
8274 * Move the integer divisions of "bmap" into the right position
8275 * according to "exp" and then introduce the additional integer
8276 * divisions, adding div constraints.
8277 * The moving should be done first to avoid moving coefficients
8278 * in the definitions of the extra integer divisions.
8280 __isl_give isl_basic_map
*isl_basic_map_expand_divs(
8281 __isl_take isl_basic_map
*bmap
, __isl_take isl_mat
*div
, int *exp
)
8286 bmap
= isl_basic_map_cow(bmap
);
8290 if (div
->n_row
< bmap
->n_div
)
8291 isl_die(isl_mat_get_ctx(div
), isl_error_invalid
,
8292 "not an expansion", goto error
);
8294 n_div
= bmap
->n_div
;
8295 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
8296 div
->n_row
- n_div
, 0,
8297 2 * (div
->n_row
- n_div
));
8299 for (i
= n_div
; i
< div
->n_row
; ++i
)
8300 if (isl_basic_map_alloc_div(bmap
) < 0)
8303 for (j
= n_div
- 1; j
>= 0; --j
) {
8306 isl_basic_map_swap_div(bmap
, j
, exp
[j
]);
8309 for (i
= 0; i
< div
->n_row
; ++i
) {
8310 if (j
< n_div
&& exp
[j
] == i
) {
8313 isl_seq_cpy(bmap
->div
[i
], div
->row
[i
], div
->n_col
);
8314 if (isl_basic_map_div_is_marked_unknown(bmap
, i
))
8316 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
8324 isl_basic_map_free(bmap
);
8329 /* Apply the expansion computed by isl_merge_divs.
8330 * The expansion itself is given by "exp" while the resulting
8331 * list of divs is given by "div".
8333 __isl_give isl_basic_set
*isl_basic_set_expand_divs(
8334 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*div
, int *exp
)
8336 return isl_basic_map_expand_divs(bset
, div
, exp
);
8339 /* Look for a div in dst that corresponds to the div "div" in src.
8340 * The divs before "div" in src and dst are assumed to be the same.
8342 * Returns -1 if no corresponding div was found and the position
8343 * of the corresponding div in dst otherwise.
8345 static int find_div(struct isl_basic_map
*dst
,
8346 struct isl_basic_map
*src
, unsigned div
)
8350 unsigned total
= isl_space_dim(src
->dim
, isl_dim_all
);
8352 isl_assert(dst
->ctx
, div
<= dst
->n_div
, return -1);
8353 for (i
= div
; i
< dst
->n_div
; ++i
)
8354 if (isl_seq_eq(dst
->div
[i
], src
->div
[div
], 1+1+total
+div
) &&
8355 isl_seq_first_non_zero(dst
->div
[i
]+1+1+total
+div
,
8356 dst
->n_div
- div
) == -1)
8361 /* Align the divs of "dst" to those of "src", adding divs from "src"
8362 * if needed. That is, make sure that the first src->n_div divs
8363 * of the result are equal to those of src.
8365 * The result is not finalized as by design it will have redundant
8366 * divs if any divs from "src" were copied.
8368 __isl_give isl_basic_map
*isl_basic_map_align_divs(
8369 __isl_take isl_basic_map
*dst
, __isl_keep isl_basic_map
*src
)
8372 int known
, extended
;
8376 return isl_basic_map_free(dst
);
8378 if (src
->n_div
== 0)
8381 known
= isl_basic_map_divs_known(src
);
8383 return isl_basic_map_free(dst
);
8385 isl_die(isl_basic_map_get_ctx(src
), isl_error_invalid
,
8386 "some src divs are unknown",
8387 return isl_basic_map_free(dst
));
8389 src
= isl_basic_map_order_divs(src
);
8392 total
= isl_space_dim(src
->dim
, isl_dim_all
);
8393 for (i
= 0; i
< src
->n_div
; ++i
) {
8394 int j
= find_div(dst
, src
, i
);
8397 int extra
= src
->n_div
- i
;
8398 dst
= isl_basic_map_cow(dst
);
8401 dst
= isl_basic_map_extend_space(dst
,
8402 isl_space_copy(dst
->dim
),
8403 extra
, 0, 2 * extra
);
8406 j
= isl_basic_map_alloc_div(dst
);
8408 return isl_basic_map_free(dst
);
8409 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
+i
);
8410 isl_seq_clr(dst
->div
[j
]+1+1+total
+i
, dst
->n_div
- i
);
8411 if (isl_basic_map_add_div_constraints(dst
, j
) < 0)
8412 return isl_basic_map_free(dst
);
8415 isl_basic_map_swap_div(dst
, i
, j
);
8420 struct isl_basic_set
*isl_basic_set_align_divs(
8421 struct isl_basic_set
*dst
, struct isl_basic_set
*src
)
8423 return bset_from_bmap(isl_basic_map_align_divs(bset_to_bmap(dst
),
8424 bset_to_bmap(src
)));
8427 struct isl_map
*isl_map_align_divs(struct isl_map
*map
)
8435 map
= isl_map_compute_divs(map
);
8436 map
= isl_map_cow(map
);
8440 for (i
= 1; i
< map
->n
; ++i
)
8441 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], map
->p
[i
]);
8442 for (i
= 1; i
< map
->n
; ++i
) {
8443 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], map
->p
[0]);
8445 return isl_map_free(map
);
8448 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
8452 struct isl_set
*isl_set_align_divs(struct isl_set
*set
)
8454 return set_from_map(isl_map_align_divs(set_to_map(set
)));
8457 /* Align the divs of the basic maps in "map" to those
8458 * of the basic maps in "list", as well as to the other basic maps in "map".
8459 * The elements in "list" are assumed to have known divs.
8461 __isl_give isl_map
*isl_map_align_divs_to_basic_map_list(
8462 __isl_take isl_map
*map
, __isl_keep isl_basic_map_list
*list
)
8466 map
= isl_map_compute_divs(map
);
8467 map
= isl_map_cow(map
);
8469 return isl_map_free(map
);
8473 n
= isl_basic_map_list_n_basic_map(list
);
8474 for (i
= 0; i
< n
; ++i
) {
8475 isl_basic_map
*bmap
;
8477 bmap
= isl_basic_map_list_get_basic_map(list
, i
);
8478 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], bmap
);
8479 isl_basic_map_free(bmap
);
8482 return isl_map_free(map
);
8484 return isl_map_align_divs(map
);
8487 /* Align the divs of each element of "list" to those of "bmap".
8488 * Both "bmap" and the elements of "list" are assumed to have known divs.
8490 __isl_give isl_basic_map_list
*isl_basic_map_list_align_divs_to_basic_map(
8491 __isl_take isl_basic_map_list
*list
, __isl_keep isl_basic_map
*bmap
)
8496 return isl_basic_map_list_free(list
);
8498 n
= isl_basic_map_list_n_basic_map(list
);
8499 for (i
= 0; i
< n
; ++i
) {
8500 isl_basic_map
*bmap_i
;
8502 bmap_i
= isl_basic_map_list_get_basic_map(list
, i
);
8503 bmap_i
= isl_basic_map_align_divs(bmap_i
, bmap
);
8504 list
= isl_basic_map_list_set_basic_map(list
, i
, bmap_i
);
8510 static __isl_give isl_set
*set_apply( __isl_take isl_set
*set
,
8511 __isl_take isl_map
*map
)
8515 isl_assert(set
->ctx
, isl_map_compatible_domain(map
, set
), goto error
);
8516 map
= isl_map_intersect_domain(map
, set
);
8517 set
= isl_map_range(map
);
8525 __isl_give isl_set
*isl_set_apply( __isl_take isl_set
*set
,
8526 __isl_take isl_map
*map
)
8528 return isl_map_align_params_map_map_and(set
, map
, &set_apply
);
8531 /* There is no need to cow as removing empty parts doesn't change
8532 * the meaning of the set.
8534 struct isl_map
*isl_map_remove_empty_parts(struct isl_map
*map
)
8541 for (i
= map
->n
- 1; i
>= 0; --i
)
8542 remove_if_empty(map
, i
);
8547 struct isl_set
*isl_set_remove_empty_parts(struct isl_set
*set
)
8549 return set_from_map(isl_map_remove_empty_parts(set_to_map(set
)));
8552 static __isl_give isl_basic_map
*map_copy_basic_map(__isl_keep isl_map
*map
)
8554 struct isl_basic_map
*bmap
;
8555 if (!map
|| map
->n
== 0)
8557 bmap
= map
->p
[map
->n
-1];
8558 isl_assert(map
->ctx
, ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
), return NULL
);
8559 return isl_basic_map_copy(bmap
);
8562 __isl_give isl_basic_map
*isl_map_copy_basic_map(__isl_keep isl_map
*map
)
8564 return map_copy_basic_map(map
);
8567 struct isl_basic_set
*isl_set_copy_basic_set(struct isl_set
*set
)
8569 return bset_from_bmap(map_copy_basic_map(set_to_map(set
)));
8572 static __isl_give isl_map
*map_drop_basic_map(__isl_take isl_map
*map
,
8573 __isl_keep isl_basic_map
*bmap
)
8579 for (i
= map
->n
-1; i
>= 0; --i
) {
8580 if (map
->p
[i
] != bmap
)
8582 map
= isl_map_cow(map
);
8585 isl_basic_map_free(map
->p
[i
]);
8586 if (i
!= map
->n
-1) {
8587 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
8588 map
->p
[i
] = map
->p
[map
->n
-1];
8599 __isl_give isl_map
*isl_map_drop_basic_map(__isl_take isl_map
*map
,
8600 __isl_keep isl_basic_map
*bmap
)
8602 return map_drop_basic_map(map
, bmap
);
8605 struct isl_set
*isl_set_drop_basic_set(struct isl_set
*set
,
8606 struct isl_basic_set
*bset
)
8608 return set_from_map(map_drop_basic_map(set_to_map(set
),
8609 bset_to_bmap(bset
)));
8612 /* Given two basic sets bset1 and bset2, compute the maximal difference
8613 * between the values of dimension pos in bset1 and those in bset2
8614 * for any common value of the parameters and dimensions preceding pos.
8616 static enum isl_lp_result
basic_set_maximal_difference_at(
8617 __isl_keep isl_basic_set
*bset1
, __isl_keep isl_basic_set
*bset2
,
8618 int pos
, isl_int
*opt
)
8620 isl_basic_map
*bmap1
;
8621 isl_basic_map
*bmap2
;
8622 struct isl_ctx
*ctx
;
8623 struct isl_vec
*obj
;
8627 enum isl_lp_result res
;
8629 if (!bset1
|| !bset2
)
8630 return isl_lp_error
;
8632 nparam
= isl_basic_set_n_param(bset1
);
8633 dim1
= isl_basic_set_n_dim(bset1
);
8635 bmap1
= isl_basic_map_from_range(isl_basic_set_copy(bset1
));
8636 bmap2
= isl_basic_map_from_range(isl_basic_set_copy(bset2
));
8637 bmap1
= isl_basic_map_move_dims(bmap1
, isl_dim_in
, 0,
8638 isl_dim_out
, 0, pos
);
8639 bmap2
= isl_basic_map_move_dims(bmap2
, isl_dim_in
, 0,
8640 isl_dim_out
, 0, pos
);
8641 bmap1
= isl_basic_map_range_product(bmap1
, bmap2
);
8643 return isl_lp_error
;
8645 total
= isl_basic_map_total_dim(bmap1
);
8647 obj
= isl_vec_alloc(ctx
, 1 + total
);
8650 isl_seq_clr(obj
->block
.data
, 1 + total
);
8651 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
], 1);
8652 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
+(dim1
-pos
)], -1);
8653 res
= isl_basic_map_solve_lp(bmap1
, 1, obj
->block
.data
, ctx
->one
,
8655 isl_basic_map_free(bmap1
);
8659 isl_basic_map_free(bmap1
);
8660 return isl_lp_error
;
8663 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
8664 * for any common value of the parameters and dimensions preceding pos
8665 * in both basic sets, the values of dimension pos in bset1 are
8666 * smaller or larger than those in bset2.
8669 * 1 if bset1 follows bset2
8670 * -1 if bset1 precedes bset2
8671 * 0 if bset1 and bset2 are incomparable
8672 * -2 if some error occurred.
8674 int isl_basic_set_compare_at(struct isl_basic_set
*bset1
,
8675 struct isl_basic_set
*bset2
, int pos
)
8678 enum isl_lp_result res
;
8683 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
8685 if (res
== isl_lp_empty
)
8687 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
8688 res
== isl_lp_unbounded
)
8690 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
8699 /* Given two basic sets bset1 and bset2, check whether
8700 * for any common value of the parameters and dimensions preceding pos
8701 * there is a value of dimension pos in bset1 that is larger
8702 * than a value of the same dimension in bset2.
8705 * 1 if there exists such a pair
8706 * 0 if there is no such pair, but there is a pair of equal values
8708 * -2 if some error occurred.
8710 int isl_basic_set_follows_at(__isl_keep isl_basic_set
*bset1
,
8711 __isl_keep isl_basic_set
*bset2
, int pos
)
8714 enum isl_lp_result res
;
8719 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
8721 if (res
== isl_lp_empty
)
8723 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
8724 res
== isl_lp_unbounded
)
8726 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
8728 else if (res
== isl_lp_ok
)
8737 /* Given two sets set1 and set2, check whether
8738 * for any common value of the parameters and dimensions preceding pos
8739 * there is a value of dimension pos in set1 that is larger
8740 * than a value of the same dimension in set2.
8743 * 1 if there exists such a pair
8744 * 0 if there is no such pair, but there is a pair of equal values
8746 * -2 if some error occurred.
8748 int isl_set_follows_at(__isl_keep isl_set
*set1
,
8749 __isl_keep isl_set
*set2
, int pos
)
8757 for (i
= 0; i
< set1
->n
; ++i
)
8758 for (j
= 0; j
< set2
->n
; ++j
) {
8760 f
= isl_basic_set_follows_at(set1
->p
[i
], set2
->p
[j
], pos
);
8761 if (f
== 1 || f
== -2)
8770 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map
*bmap
,
8771 unsigned pos
, isl_int
*val
)
8779 total
= isl_basic_map_total_dim(bmap
);
8780 for (i
= 0, d
= total
-1; i
< bmap
->n_eq
&& d
+1 > pos
; ++i
) {
8781 for (; d
+1 > pos
; --d
)
8782 if (!isl_int_is_zero(bmap
->eq
[i
][1+d
]))
8786 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1, d
) != -1)
8788 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1+d
+1, total
-d
-1) != -1)
8790 if (!isl_int_is_one(bmap
->eq
[i
][1+d
]))
8793 isl_int_neg(*val
, bmap
->eq
[i
][0]);
8799 static int isl_map_plain_has_fixed_var(__isl_keep isl_map
*map
,
8800 unsigned pos
, isl_int
*val
)
8812 return isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, val
);
8815 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, &v
);
8816 for (i
= 1; fixed
== 1 && i
< map
->n
; ++i
) {
8817 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[i
], pos
, &tmp
);
8818 if (fixed
== 1 && isl_int_ne(tmp
, v
))
8822 isl_int_set(*val
, v
);
8828 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set
*bset
,
8829 unsigned pos
, isl_int
*val
)
8831 return isl_basic_map_plain_has_fixed_var(bset_to_bmap(bset
),
8835 static int isl_set_plain_has_fixed_var(__isl_keep isl_set
*set
, unsigned pos
,
8838 return isl_map_plain_has_fixed_var(set_to_map(set
), pos
, val
);
8841 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map
*bmap
,
8842 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
8844 if (pos
>= isl_basic_map_dim(bmap
, type
))
8846 return isl_basic_map_plain_has_fixed_var(bmap
,
8847 isl_basic_map_offset(bmap
, type
) - 1 + pos
, val
);
8850 /* If "bmap" obviously lies on a hyperplane where the given dimension
8851 * has a fixed value, then return that value.
8852 * Otherwise return NaN.
8854 __isl_give isl_val
*isl_basic_map_plain_get_val_if_fixed(
8855 __isl_keep isl_basic_map
*bmap
,
8856 enum isl_dim_type type
, unsigned pos
)
8864 ctx
= isl_basic_map_get_ctx(bmap
);
8865 v
= isl_val_alloc(ctx
);
8868 fixed
= isl_basic_map_plain_is_fixed(bmap
, type
, pos
, &v
->n
);
8870 return isl_val_free(v
);
8872 isl_int_set_si(v
->d
, 1);
8876 return isl_val_nan(ctx
);
8879 int isl_map_plain_is_fixed(__isl_keep isl_map
*map
,
8880 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
8882 if (pos
>= isl_map_dim(map
, type
))
8884 return isl_map_plain_has_fixed_var(map
,
8885 map_offset(map
, type
) - 1 + pos
, val
);
8888 /* If "map" obviously lies on a hyperplane where the given dimension
8889 * has a fixed value, then return that value.
8890 * Otherwise return NaN.
8892 __isl_give isl_val
*isl_map_plain_get_val_if_fixed(__isl_keep isl_map
*map
,
8893 enum isl_dim_type type
, unsigned pos
)
8901 ctx
= isl_map_get_ctx(map
);
8902 v
= isl_val_alloc(ctx
);
8905 fixed
= isl_map_plain_is_fixed(map
, type
, pos
, &v
->n
);
8907 return isl_val_free(v
);
8909 isl_int_set_si(v
->d
, 1);
8913 return isl_val_nan(ctx
);
8916 /* If "set" obviously lies on a hyperplane where the given dimension
8917 * has a fixed value, then return that value.
8918 * Otherwise return NaN.
8920 __isl_give isl_val
*isl_set_plain_get_val_if_fixed(__isl_keep isl_set
*set
,
8921 enum isl_dim_type type
, unsigned pos
)
8923 return isl_map_plain_get_val_if_fixed(set
, type
, pos
);
8926 int isl_set_plain_is_fixed(__isl_keep isl_set
*set
,
8927 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
8929 return isl_map_plain_is_fixed(set
, type
, pos
, val
);
8932 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8933 * then return this fixed value in *val.
8935 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set
*bset
,
8936 unsigned dim
, isl_int
*val
)
8938 return isl_basic_set_plain_has_fixed_var(bset
,
8939 isl_basic_set_n_param(bset
) + dim
, val
);
8942 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8943 * then return this fixed value in *val.
8945 int isl_set_plain_dim_is_fixed(__isl_keep isl_set
*set
,
8946 unsigned dim
, isl_int
*val
)
8948 return isl_set_plain_has_fixed_var(set
, isl_set_n_param(set
) + dim
, val
);
8951 /* Check if input variable in has fixed value and if so and if val is not NULL,
8952 * then return this fixed value in *val.
8954 int isl_map_plain_input_is_fixed(__isl_keep isl_map
*map
,
8955 unsigned in
, isl_int
*val
)
8957 return isl_map_plain_has_fixed_var(map
, isl_map_n_param(map
) + in
, val
);
8960 /* Check if dimension dim has an (obvious) fixed lower bound and if so
8961 * and if val is not NULL, then return this lower bound in *val.
8963 int isl_basic_set_plain_dim_has_fixed_lower_bound(
8964 __isl_keep isl_basic_set
*bset
, unsigned dim
, isl_int
*val
)
8966 int i
, i_eq
= -1, i_ineq
= -1;
8973 total
= isl_basic_set_total_dim(bset
);
8974 nparam
= isl_basic_set_n_param(bset
);
8975 for (i
= 0; i
< bset
->n_eq
; ++i
) {
8976 if (isl_int_is_zero(bset
->eq
[i
][1+nparam
+dim
]))
8982 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
8983 if (!isl_int_is_pos(bset
->ineq
[i
][1+nparam
+dim
]))
8985 if (i_eq
!= -1 || i_ineq
!= -1)
8989 if (i_eq
== -1 && i_ineq
== -1)
8991 c
= i_eq
!= -1 ? bset
->eq
[i_eq
] : bset
->ineq
[i_ineq
];
8992 /* The coefficient should always be one due to normalization. */
8993 if (!isl_int_is_one(c
[1+nparam
+dim
]))
8995 if (isl_seq_first_non_zero(c
+1, nparam
+dim
) != -1)
8997 if (isl_seq_first_non_zero(c
+1+nparam
+dim
+1,
8998 total
- nparam
- dim
- 1) != -1)
9001 isl_int_neg(*val
, c
[0]);
9005 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set
*set
,
9006 unsigned dim
, isl_int
*val
)
9018 return isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[0],
9022 fixed
= isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[0],
9024 for (i
= 1; fixed
== 1 && i
< set
->n
; ++i
) {
9025 fixed
= isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[i
],
9027 if (fixed
== 1 && isl_int_ne(tmp
, v
))
9031 isl_int_set(*val
, v
);
9037 /* Return -1 if the constraint "c1" should be sorted before "c2"
9038 * and 1 if it should be sorted after "c2".
9039 * Return 0 if the two constraints are the same (up to the constant term).
9041 * In particular, if a constraint involves later variables than another
9042 * then it is sorted after this other constraint.
9043 * uset_gist depends on constraints without existentially quantified
9044 * variables sorting first.
9046 * For constraints that have the same latest variable, those
9047 * with the same coefficient for this latest variable (first in absolute value
9048 * and then in actual value) are grouped together.
9049 * This is useful for detecting pairs of constraints that can
9050 * be chained in their printed representation.
9052 * Finally, within a group, constraints are sorted according to
9053 * their coefficients (excluding the constant term).
9055 static int sort_constraint_cmp(const void *p1
, const void *p2
, void *arg
)
9057 isl_int
**c1
= (isl_int
**) p1
;
9058 isl_int
**c2
= (isl_int
**) p2
;
9060 unsigned size
= *(unsigned *) arg
;
9063 l1
= isl_seq_last_non_zero(*c1
+ 1, size
);
9064 l2
= isl_seq_last_non_zero(*c2
+ 1, size
);
9069 cmp
= isl_int_abs_cmp((*c1
)[1 + l1
], (*c2
)[1 + l1
]);
9072 cmp
= isl_int_cmp((*c1
)[1 + l1
], (*c2
)[1 + l1
]);
9076 return isl_seq_cmp(*c1
+ 1, *c2
+ 1, size
);
9079 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
9080 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
9081 * and 0 if the two constraints are the same (up to the constant term).
9083 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map
*bmap
,
9084 isl_int
*c1
, isl_int
*c2
)
9090 total
= isl_basic_map_total_dim(bmap
);
9091 return sort_constraint_cmp(&c1
, &c2
, &total
);
9094 __isl_give isl_basic_map
*isl_basic_map_sort_constraints(
9095 __isl_take isl_basic_map
*bmap
)
9101 if (bmap
->n_ineq
== 0)
9103 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED
))
9105 total
= isl_basic_map_total_dim(bmap
);
9106 if (isl_sort(bmap
->ineq
, bmap
->n_ineq
, sizeof(isl_int
*),
9107 &sort_constraint_cmp
, &total
) < 0)
9108 return isl_basic_map_free(bmap
);
9112 __isl_give isl_basic_set
*isl_basic_set_sort_constraints(
9113 __isl_take isl_basic_set
*bset
)
9115 isl_basic_map
*bmap
= bset_to_bmap(bset
);
9116 return bset_from_bmap(isl_basic_map_sort_constraints(bmap
));
9119 struct isl_basic_map
*isl_basic_map_normalize(struct isl_basic_map
*bmap
)
9123 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED
))
9125 bmap
= isl_basic_map_remove_redundancies(bmap
);
9126 bmap
= isl_basic_map_sort_constraints(bmap
);
9128 ISL_F_SET(bmap
, ISL_BASIC_MAP_NORMALIZED
);
9132 struct isl_basic_set
*isl_basic_set_normalize(struct isl_basic_set
*bset
)
9134 return bset_from_bmap(isl_basic_map_normalize(bset_to_bmap(bset
)));
9137 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map
*bmap1
,
9138 const __isl_keep isl_basic_map
*bmap2
)
9143 if (!bmap1
|| !bmap2
)
9148 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) !=
9149 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_RATIONAL
))
9150 return ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) ? -1 : 1;
9151 if (isl_basic_map_n_param(bmap1
) != isl_basic_map_n_param(bmap2
))
9152 return isl_basic_map_n_param(bmap1
) - isl_basic_map_n_param(bmap2
);
9153 if (isl_basic_map_n_in(bmap1
) != isl_basic_map_n_in(bmap2
))
9154 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
9155 if (isl_basic_map_n_out(bmap1
) != isl_basic_map_n_out(bmap2
))
9156 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
9157 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
) &&
9158 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
9160 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
))
9162 if (ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
9164 if (bmap1
->n_eq
!= bmap2
->n_eq
)
9165 return bmap1
->n_eq
- bmap2
->n_eq
;
9166 if (bmap1
->n_ineq
!= bmap2
->n_ineq
)
9167 return bmap1
->n_ineq
- bmap2
->n_ineq
;
9168 if (bmap1
->n_div
!= bmap2
->n_div
)
9169 return bmap1
->n_div
- bmap2
->n_div
;
9170 total
= isl_basic_map_total_dim(bmap1
);
9171 for (i
= 0; i
< bmap1
->n_eq
; ++i
) {
9172 cmp
= isl_seq_cmp(bmap1
->eq
[i
], bmap2
->eq
[i
], 1+total
);
9176 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
9177 cmp
= isl_seq_cmp(bmap1
->ineq
[i
], bmap2
->ineq
[i
], 1+total
);
9181 for (i
= 0; i
< bmap1
->n_div
; ++i
) {
9182 cmp
= isl_seq_cmp(bmap1
->div
[i
], bmap2
->div
[i
], 1+1+total
);
9189 int isl_basic_set_plain_cmp(const __isl_keep isl_basic_set
*bset1
,
9190 const __isl_keep isl_basic_set
*bset2
)
9192 return isl_basic_map_plain_cmp(bset1
, bset2
);
9195 int isl_set_plain_cmp(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
9201 if (set1
->n
!= set2
->n
)
9202 return set1
->n
- set2
->n
;
9204 for (i
= 0; i
< set1
->n
; ++i
) {
9205 cmp
= isl_basic_set_plain_cmp(set1
->p
[i
], set2
->p
[i
]);
9213 isl_bool
isl_basic_map_plain_is_equal(__isl_keep isl_basic_map
*bmap1
,
9214 __isl_keep isl_basic_map
*bmap2
)
9216 if (!bmap1
|| !bmap2
)
9217 return isl_bool_error
;
9218 return isl_basic_map_plain_cmp(bmap1
, bmap2
) == 0;
9221 isl_bool
isl_basic_set_plain_is_equal(__isl_keep isl_basic_set
*bset1
,
9222 __isl_keep isl_basic_set
*bset2
)
9224 return isl_basic_map_plain_is_equal(bset_to_bmap(bset1
),
9225 bset_to_bmap(bset2
));
9228 static int qsort_bmap_cmp(const void *p1
, const void *p2
)
9230 const struct isl_basic_map
*bmap1
= *(const struct isl_basic_map
**)p1
;
9231 const struct isl_basic_map
*bmap2
= *(const struct isl_basic_map
**)p2
;
9233 return isl_basic_map_plain_cmp(bmap1
, bmap2
);
9236 /* Sort the basic maps of "map" and remove duplicate basic maps.
9238 * While removing basic maps, we make sure that the basic maps remain
9239 * sorted because isl_map_normalize expects the basic maps of the result
9242 static __isl_give isl_map
*sort_and_remove_duplicates(__isl_take isl_map
*map
)
9246 map
= isl_map_remove_empty_parts(map
);
9249 qsort(map
->p
, map
->n
, sizeof(struct isl_basic_map
*), qsort_bmap_cmp
);
9250 for (i
= map
->n
- 1; i
>= 1; --i
) {
9251 if (!isl_basic_map_plain_is_equal(map
->p
[i
- 1], map
->p
[i
]))
9253 isl_basic_map_free(map
->p
[i
-1]);
9254 for (j
= i
; j
< map
->n
; ++j
)
9255 map
->p
[j
- 1] = map
->p
[j
];
9262 /* Remove obvious duplicates among the basic maps of "map".
9264 * Unlike isl_map_normalize, this function does not remove redundant
9265 * constraints and only removes duplicates that have exactly the same
9266 * constraints in the input. It does sort the constraints and
9267 * the basic maps to ease the detection of duplicates.
9269 * If "map" has already been normalized or if the basic maps are
9270 * disjoint, then there can be no duplicates.
9272 __isl_give isl_map
*isl_map_remove_obvious_duplicates(__isl_take isl_map
*map
)
9275 isl_basic_map
*bmap
;
9281 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
| ISL_MAP_DISJOINT
))
9283 for (i
= 0; i
< map
->n
; ++i
) {
9284 bmap
= isl_basic_map_copy(map
->p
[i
]);
9285 bmap
= isl_basic_map_sort_constraints(bmap
);
9287 return isl_map_free(map
);
9288 isl_basic_map_free(map
->p
[i
]);
9292 map
= sort_and_remove_duplicates(map
);
9296 /* We normalize in place, but if anything goes wrong we need
9297 * to return NULL, so we need to make sure we don't change the
9298 * meaning of any possible other copies of map.
9300 __isl_give isl_map
*isl_map_normalize(__isl_take isl_map
*map
)
9303 struct isl_basic_map
*bmap
;
9307 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
))
9309 for (i
= 0; i
< map
->n
; ++i
) {
9310 bmap
= isl_basic_map_normalize(isl_basic_map_copy(map
->p
[i
]));
9313 isl_basic_map_free(map
->p
[i
]);
9317 map
= sort_and_remove_duplicates(map
);
9319 ISL_F_SET(map
, ISL_MAP_NORMALIZED
);
9326 struct isl_set
*isl_set_normalize(struct isl_set
*set
)
9328 return set_from_map(isl_map_normalize(set_to_map(set
)));
9331 isl_bool
isl_map_plain_is_equal(__isl_keep isl_map
*map1
,
9332 __isl_keep isl_map
*map2
)
9338 return isl_bool_error
;
9341 return isl_bool_true
;
9342 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
9343 return isl_bool_false
;
9345 map1
= isl_map_copy(map1
);
9346 map2
= isl_map_copy(map2
);
9347 map1
= isl_map_normalize(map1
);
9348 map2
= isl_map_normalize(map2
);
9351 equal
= map1
->n
== map2
->n
;
9352 for (i
= 0; equal
&& i
< map1
->n
; ++i
) {
9353 equal
= isl_basic_map_plain_is_equal(map1
->p
[i
], map2
->p
[i
]);
9363 return isl_bool_error
;
9366 isl_bool
isl_set_plain_is_equal(__isl_keep isl_set
*set1
,
9367 __isl_keep isl_set
*set2
)
9369 return isl_map_plain_is_equal(set_to_map(set1
), set_to_map(set2
));
9372 /* Return an interval that ranges from min to max (inclusive)
9374 struct isl_basic_set
*isl_basic_set_interval(struct isl_ctx
*ctx
,
9375 isl_int min
, isl_int max
)
9378 struct isl_basic_set
*bset
= NULL
;
9380 bset
= isl_basic_set_alloc(ctx
, 0, 1, 0, 0, 2);
9384 k
= isl_basic_set_alloc_inequality(bset
);
9387 isl_int_set_si(bset
->ineq
[k
][1], 1);
9388 isl_int_neg(bset
->ineq
[k
][0], min
);
9390 k
= isl_basic_set_alloc_inequality(bset
);
9393 isl_int_set_si(bset
->ineq
[k
][1], -1);
9394 isl_int_set(bset
->ineq
[k
][0], max
);
9398 isl_basic_set_free(bset
);
9402 /* Return the basic maps in "map" as a list.
9404 __isl_give isl_basic_map_list
*isl_map_get_basic_map_list(
9405 __isl_keep isl_map
*map
)
9409 isl_basic_map_list
*list
;
9413 ctx
= isl_map_get_ctx(map
);
9414 list
= isl_basic_map_list_alloc(ctx
, map
->n
);
9416 for (i
= 0; i
< map
->n
; ++i
) {
9417 isl_basic_map
*bmap
;
9419 bmap
= isl_basic_map_copy(map
->p
[i
]);
9420 list
= isl_basic_map_list_add(list
, bmap
);
9426 /* Return the intersection of the elements in the non-empty list "list".
9427 * All elements are assumed to live in the same space.
9429 __isl_give isl_basic_map
*isl_basic_map_list_intersect(
9430 __isl_take isl_basic_map_list
*list
)
9433 isl_basic_map
*bmap
;
9437 n
= isl_basic_map_list_n_basic_map(list
);
9439 isl_die(isl_basic_map_list_get_ctx(list
), isl_error_invalid
,
9440 "expecting non-empty list", goto error
);
9442 bmap
= isl_basic_map_list_get_basic_map(list
, 0);
9443 for (i
= 1; i
< n
; ++i
) {
9444 isl_basic_map
*bmap_i
;
9446 bmap_i
= isl_basic_map_list_get_basic_map(list
, i
);
9447 bmap
= isl_basic_map_intersect(bmap
, bmap_i
);
9450 isl_basic_map_list_free(list
);
9453 isl_basic_map_list_free(list
);
9457 /* Return the intersection of the elements in the non-empty list "list".
9458 * All elements are assumed to live in the same space.
9460 __isl_give isl_basic_set
*isl_basic_set_list_intersect(
9461 __isl_take isl_basic_set_list
*list
)
9463 return isl_basic_map_list_intersect(list
);
9466 /* Return the union of the elements of "list".
9467 * The list is required to have at least one element.
9469 __isl_give isl_set
*isl_basic_set_list_union(
9470 __isl_take isl_basic_set_list
*list
)
9474 isl_basic_set
*bset
;
9479 n
= isl_basic_set_list_n_basic_set(list
);
9481 isl_die(isl_basic_set_list_get_ctx(list
), isl_error_invalid
,
9482 "expecting non-empty list", goto error
);
9484 bset
= isl_basic_set_list_get_basic_set(list
, 0);
9485 space
= isl_basic_set_get_space(bset
);
9486 isl_basic_set_free(bset
);
9488 set
= isl_set_alloc_space(space
, n
, 0);
9489 for (i
= 0; i
< n
; ++i
) {
9490 bset
= isl_basic_set_list_get_basic_set(list
, i
);
9491 set
= isl_set_add_basic_set(set
, bset
);
9494 isl_basic_set_list_free(list
);
9497 isl_basic_set_list_free(list
);
9501 /* Return the union of the elements in the non-empty list "list".
9502 * All elements are assumed to live in the same space.
9504 __isl_give isl_set
*isl_set_list_union(__isl_take isl_set_list
*list
)
9511 n
= isl_set_list_n_set(list
);
9513 isl_die(isl_set_list_get_ctx(list
), isl_error_invalid
,
9514 "expecting non-empty list", goto error
);
9516 set
= isl_set_list_get_set(list
, 0);
9517 for (i
= 1; i
< n
; ++i
) {
9520 set_i
= isl_set_list_get_set(list
, i
);
9521 set
= isl_set_union(set
, set_i
);
9524 isl_set_list_free(list
);
9527 isl_set_list_free(list
);
9531 /* Return the Cartesian product of the basic sets in list (in the given order).
9533 __isl_give isl_basic_set
*isl_basic_set_list_product(
9534 __isl_take
struct isl_basic_set_list
*list
)
9542 struct isl_basic_set
*product
= NULL
;
9546 isl_assert(list
->ctx
, list
->n
> 0, goto error
);
9547 isl_assert(list
->ctx
, list
->p
[0], goto error
);
9548 nparam
= isl_basic_set_n_param(list
->p
[0]);
9549 dim
= isl_basic_set_n_dim(list
->p
[0]);
9550 extra
= list
->p
[0]->n_div
;
9551 n_eq
= list
->p
[0]->n_eq
;
9552 n_ineq
= list
->p
[0]->n_ineq
;
9553 for (i
= 1; i
< list
->n
; ++i
) {
9554 isl_assert(list
->ctx
, list
->p
[i
], goto error
);
9555 isl_assert(list
->ctx
,
9556 nparam
== isl_basic_set_n_param(list
->p
[i
]), goto error
);
9557 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
9558 extra
+= list
->p
[i
]->n_div
;
9559 n_eq
+= list
->p
[i
]->n_eq
;
9560 n_ineq
+= list
->p
[i
]->n_ineq
;
9562 product
= isl_basic_set_alloc(list
->ctx
, nparam
, dim
, extra
,
9567 for (i
= 0; i
< list
->n
; ++i
) {
9568 isl_basic_set_add_constraints(product
,
9569 isl_basic_set_copy(list
->p
[i
]), dim
);
9570 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
9572 isl_basic_set_list_free(list
);
9575 isl_basic_set_free(product
);
9576 isl_basic_set_list_free(list
);
9580 struct isl_basic_map
*isl_basic_map_product(
9581 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
9583 isl_space
*dim_result
= NULL
;
9584 struct isl_basic_map
*bmap
;
9585 unsigned in1
, in2
, out1
, out2
, nparam
, total
, pos
;
9586 struct isl_dim_map
*dim_map1
, *dim_map2
;
9588 if (!bmap1
|| !bmap2
)
9591 isl_assert(bmap1
->ctx
, isl_space_match(bmap1
->dim
, isl_dim_param
,
9592 bmap2
->dim
, isl_dim_param
), goto error
);
9593 dim_result
= isl_space_product(isl_space_copy(bmap1
->dim
),
9594 isl_space_copy(bmap2
->dim
));
9596 in1
= isl_basic_map_n_in(bmap1
);
9597 in2
= isl_basic_map_n_in(bmap2
);
9598 out1
= isl_basic_map_n_out(bmap1
);
9599 out2
= isl_basic_map_n_out(bmap2
);
9600 nparam
= isl_basic_map_n_param(bmap1
);
9602 total
= nparam
+ in1
+ in2
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
9603 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9604 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9605 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9606 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9607 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9608 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
9609 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
9610 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
9611 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
9612 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9614 bmap
= isl_basic_map_alloc_space(dim_result
,
9615 bmap1
->n_div
+ bmap2
->n_div
,
9616 bmap1
->n_eq
+ bmap2
->n_eq
,
9617 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9618 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9619 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9620 bmap
= isl_basic_map_simplify(bmap
);
9621 return isl_basic_map_finalize(bmap
);
9623 isl_basic_map_free(bmap1
);
9624 isl_basic_map_free(bmap2
);
9628 __isl_give isl_basic_map
*isl_basic_map_flat_product(
9629 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9631 isl_basic_map
*prod
;
9633 prod
= isl_basic_map_product(bmap1
, bmap2
);
9634 prod
= isl_basic_map_flatten(prod
);
9638 __isl_give isl_basic_set
*isl_basic_set_flat_product(
9639 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
9641 return isl_basic_map_flat_range_product(bset1
, bset2
);
9644 __isl_give isl_basic_map
*isl_basic_map_domain_product(
9645 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9647 isl_space
*space_result
= NULL
;
9648 isl_basic_map
*bmap
;
9649 unsigned in1
, in2
, out
, nparam
, total
, pos
;
9650 struct isl_dim_map
*dim_map1
, *dim_map2
;
9652 if (!bmap1
|| !bmap2
)
9655 space_result
= isl_space_domain_product(isl_space_copy(bmap1
->dim
),
9656 isl_space_copy(bmap2
->dim
));
9658 in1
= isl_basic_map_dim(bmap1
, isl_dim_in
);
9659 in2
= isl_basic_map_dim(bmap2
, isl_dim_in
);
9660 out
= isl_basic_map_dim(bmap1
, isl_dim_out
);
9661 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
9663 total
= nparam
+ in1
+ in2
+ out
+ bmap1
->n_div
+ bmap2
->n_div
;
9664 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9665 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9666 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9667 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9668 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9669 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
9670 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
9671 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
);
9672 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out
);
9673 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9675 bmap
= isl_basic_map_alloc_space(space_result
,
9676 bmap1
->n_div
+ bmap2
->n_div
,
9677 bmap1
->n_eq
+ bmap2
->n_eq
,
9678 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9679 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9680 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9681 bmap
= isl_basic_map_simplify(bmap
);
9682 return isl_basic_map_finalize(bmap
);
9684 isl_basic_map_free(bmap1
);
9685 isl_basic_map_free(bmap2
);
9689 __isl_give isl_basic_map
*isl_basic_map_range_product(
9690 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9693 isl_space
*dim_result
= NULL
;
9694 isl_basic_map
*bmap
;
9695 unsigned in
, out1
, out2
, nparam
, total
, pos
;
9696 struct isl_dim_map
*dim_map1
, *dim_map2
;
9698 rational
= isl_basic_map_is_rational(bmap1
);
9699 if (rational
>= 0 && rational
)
9700 rational
= isl_basic_map_is_rational(bmap2
);
9701 if (!bmap1
|| !bmap2
|| rational
< 0)
9704 if (!isl_space_match(bmap1
->dim
, isl_dim_param
,
9705 bmap2
->dim
, isl_dim_param
))
9706 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
9707 "parameters don't match", goto error
);
9709 dim_result
= isl_space_range_product(isl_space_copy(bmap1
->dim
),
9710 isl_space_copy(bmap2
->dim
));
9712 in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
9713 out1
= isl_basic_map_n_out(bmap1
);
9714 out2
= isl_basic_map_n_out(bmap2
);
9715 nparam
= isl_basic_map_n_param(bmap1
);
9717 total
= nparam
+ in
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
9718 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9719 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9720 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9721 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9722 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9723 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
9724 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in
);
9725 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
9726 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
9727 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9729 bmap
= isl_basic_map_alloc_space(dim_result
,
9730 bmap1
->n_div
+ bmap2
->n_div
,
9731 bmap1
->n_eq
+ bmap2
->n_eq
,
9732 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9733 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9734 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9736 bmap
= isl_basic_map_set_rational(bmap
);
9737 bmap
= isl_basic_map_simplify(bmap
);
9738 return isl_basic_map_finalize(bmap
);
9740 isl_basic_map_free(bmap1
);
9741 isl_basic_map_free(bmap2
);
9745 __isl_give isl_basic_map
*isl_basic_map_flat_range_product(
9746 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9748 isl_basic_map
*prod
;
9750 prod
= isl_basic_map_range_product(bmap1
, bmap2
);
9751 prod
= isl_basic_map_flatten_range(prod
);
9755 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
9756 * and collect the results.
9757 * The result live in the space obtained by calling "space_product"
9758 * on the spaces of "map1" and "map2".
9759 * If "remove_duplicates" is set then the result may contain duplicates
9760 * (even if the inputs do not) and so we try and remove the obvious
9763 static __isl_give isl_map
*map_product(__isl_take isl_map
*map1
,
9764 __isl_take isl_map
*map2
,
9765 __isl_give isl_space
*(*space_product
)(__isl_take isl_space
*left
,
9766 __isl_take isl_space
*right
),
9767 __isl_give isl_basic_map
*(*basic_map_product
)(
9768 __isl_take isl_basic_map
*left
,
9769 __isl_take isl_basic_map
*right
),
9770 int remove_duplicates
)
9773 struct isl_map
*result
;
9779 isl_assert(map1
->ctx
, isl_space_match(map1
->dim
, isl_dim_param
,
9780 map2
->dim
, isl_dim_param
), goto error
);
9782 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
9783 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
9784 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
9786 result
= isl_map_alloc_space(space_product(isl_space_copy(map1
->dim
),
9787 isl_space_copy(map2
->dim
)),
9788 map1
->n
* map2
->n
, flags
);
9791 for (i
= 0; i
< map1
->n
; ++i
)
9792 for (j
= 0; j
< map2
->n
; ++j
) {
9793 struct isl_basic_map
*part
;
9794 part
= basic_map_product(isl_basic_map_copy(map1
->p
[i
]),
9795 isl_basic_map_copy(map2
->p
[j
]));
9796 if (isl_basic_map_is_empty(part
))
9797 isl_basic_map_free(part
);
9799 result
= isl_map_add_basic_map(result
, part
);
9803 if (remove_duplicates
)
9804 result
= isl_map_remove_obvious_duplicates(result
);
9814 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
9816 static __isl_give isl_map
*map_product_aligned(__isl_take isl_map
*map1
,
9817 __isl_take isl_map
*map2
)
9819 return map_product(map1
, map2
, &isl_space_product
,
9820 &isl_basic_map_product
, 0);
9823 __isl_give isl_map
*isl_map_product(__isl_take isl_map
*map1
,
9824 __isl_take isl_map
*map2
)
9826 return isl_map_align_params_map_map_and(map1
, map2
, &map_product_aligned
);
9829 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
9831 __isl_give isl_map
*isl_map_flat_product(__isl_take isl_map
*map1
,
9832 __isl_take isl_map
*map2
)
9836 prod
= isl_map_product(map1
, map2
);
9837 prod
= isl_map_flatten(prod
);
9841 /* Given two set A and B, construct its Cartesian product A x B.
9843 struct isl_set
*isl_set_product(struct isl_set
*set1
, struct isl_set
*set2
)
9845 return isl_map_range_product(set1
, set2
);
9848 __isl_give isl_set
*isl_set_flat_product(__isl_take isl_set
*set1
,
9849 __isl_take isl_set
*set2
)
9851 return isl_map_flat_range_product(set1
, set2
);
9854 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
9856 static __isl_give isl_map
*map_domain_product_aligned(__isl_take isl_map
*map1
,
9857 __isl_take isl_map
*map2
)
9859 return map_product(map1
, map2
, &isl_space_domain_product
,
9860 &isl_basic_map_domain_product
, 1);
9863 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
9865 static __isl_give isl_map
*map_range_product_aligned(__isl_take isl_map
*map1
,
9866 __isl_take isl_map
*map2
)
9868 return map_product(map1
, map2
, &isl_space_range_product
,
9869 &isl_basic_map_range_product
, 1);
9872 __isl_give isl_map
*isl_map_domain_product(__isl_take isl_map
*map1
,
9873 __isl_take isl_map
*map2
)
9875 return isl_map_align_params_map_map_and(map1
, map2
,
9876 &map_domain_product_aligned
);
9879 __isl_give isl_map
*isl_map_range_product(__isl_take isl_map
*map1
,
9880 __isl_take isl_map
*map2
)
9882 return isl_map_align_params_map_map_and(map1
, map2
,
9883 &map_range_product_aligned
);
9886 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
9888 __isl_give isl_map
*isl_map_factor_domain(__isl_take isl_map
*map
)
9891 int total1
, keep1
, total2
, keep2
;
9895 if (!isl_space_domain_is_wrapping(map
->dim
) ||
9896 !isl_space_range_is_wrapping(map
->dim
))
9897 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9898 "not a product", return isl_map_free(map
));
9900 space
= isl_map_get_space(map
);
9901 total1
= isl_space_dim(space
, isl_dim_in
);
9902 total2
= isl_space_dim(space
, isl_dim_out
);
9903 space
= isl_space_factor_domain(space
);
9904 keep1
= isl_space_dim(space
, isl_dim_in
);
9905 keep2
= isl_space_dim(space
, isl_dim_out
);
9906 map
= isl_map_project_out(map
, isl_dim_in
, keep1
, total1
- keep1
);
9907 map
= isl_map_project_out(map
, isl_dim_out
, keep2
, total2
- keep2
);
9908 map
= isl_map_reset_space(map
, space
);
9913 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
9915 __isl_give isl_map
*isl_map_factor_range(__isl_take isl_map
*map
)
9918 int total1
, keep1
, total2
, keep2
;
9922 if (!isl_space_domain_is_wrapping(map
->dim
) ||
9923 !isl_space_range_is_wrapping(map
->dim
))
9924 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9925 "not a product", return isl_map_free(map
));
9927 space
= isl_map_get_space(map
);
9928 total1
= isl_space_dim(space
, isl_dim_in
);
9929 total2
= isl_space_dim(space
, isl_dim_out
);
9930 space
= isl_space_factor_range(space
);
9931 keep1
= isl_space_dim(space
, isl_dim_in
);
9932 keep2
= isl_space_dim(space
, isl_dim_out
);
9933 map
= isl_map_project_out(map
, isl_dim_in
, 0, total1
- keep1
);
9934 map
= isl_map_project_out(map
, isl_dim_out
, 0, total2
- keep2
);
9935 map
= isl_map_reset_space(map
, space
);
9940 /* Given a map of the form [A -> B] -> C, return the map A -> C.
9942 __isl_give isl_map
*isl_map_domain_factor_domain(__isl_take isl_map
*map
)
9949 if (!isl_space_domain_is_wrapping(map
->dim
))
9950 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9951 "domain is not a product", return isl_map_free(map
));
9953 space
= isl_map_get_space(map
);
9954 total
= isl_space_dim(space
, isl_dim_in
);
9955 space
= isl_space_domain_factor_domain(space
);
9956 keep
= isl_space_dim(space
, isl_dim_in
);
9957 map
= isl_map_project_out(map
, isl_dim_in
, keep
, total
- keep
);
9958 map
= isl_map_reset_space(map
, space
);
9963 /* Given a map of the form [A -> B] -> C, return the map B -> C.
9965 __isl_give isl_map
*isl_map_domain_factor_range(__isl_take isl_map
*map
)
9972 if (!isl_space_domain_is_wrapping(map
->dim
))
9973 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9974 "domain is not a product", return isl_map_free(map
));
9976 space
= isl_map_get_space(map
);
9977 total
= isl_space_dim(space
, isl_dim_in
);
9978 space
= isl_space_domain_factor_range(space
);
9979 keep
= isl_space_dim(space
, isl_dim_in
);
9980 map
= isl_map_project_out(map
, isl_dim_in
, 0, total
- keep
);
9981 map
= isl_map_reset_space(map
, space
);
9986 /* Given a map A -> [B -> C], extract the map A -> B.
9988 __isl_give isl_map
*isl_map_range_factor_domain(__isl_take isl_map
*map
)
9995 if (!isl_space_range_is_wrapping(map
->dim
))
9996 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9997 "range is not a product", return isl_map_free(map
));
9999 space
= isl_map_get_space(map
);
10000 total
= isl_space_dim(space
, isl_dim_out
);
10001 space
= isl_space_range_factor_domain(space
);
10002 keep
= isl_space_dim(space
, isl_dim_out
);
10003 map
= isl_map_project_out(map
, isl_dim_out
, keep
, total
- keep
);
10004 map
= isl_map_reset_space(map
, space
);
10009 /* Given a map A -> [B -> C], extract the map A -> C.
10011 __isl_give isl_map
*isl_map_range_factor_range(__isl_take isl_map
*map
)
10018 if (!isl_space_range_is_wrapping(map
->dim
))
10019 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10020 "range is not a product", return isl_map_free(map
));
10022 space
= isl_map_get_space(map
);
10023 total
= isl_space_dim(space
, isl_dim_out
);
10024 space
= isl_space_range_factor_range(space
);
10025 keep
= isl_space_dim(space
, isl_dim_out
);
10026 map
= isl_map_project_out(map
, isl_dim_out
, 0, total
- keep
);
10027 map
= isl_map_reset_space(map
, space
);
10032 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
10034 __isl_give isl_map
*isl_map_flat_domain_product(__isl_take isl_map
*map1
,
10035 __isl_take isl_map
*map2
)
10039 prod
= isl_map_domain_product(map1
, map2
);
10040 prod
= isl_map_flatten_domain(prod
);
10044 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
10046 __isl_give isl_map
*isl_map_flat_range_product(__isl_take isl_map
*map1
,
10047 __isl_take isl_map
*map2
)
10051 prod
= isl_map_range_product(map1
, map2
);
10052 prod
= isl_map_flatten_range(prod
);
10056 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map
*bmap
)
10059 uint32_t hash
= isl_hash_init();
10064 bmap
= isl_basic_map_copy(bmap
);
10065 bmap
= isl_basic_map_normalize(bmap
);
10068 total
= isl_basic_map_total_dim(bmap
);
10069 isl_hash_byte(hash
, bmap
->n_eq
& 0xFF);
10070 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
10072 c_hash
= isl_seq_get_hash(bmap
->eq
[i
], 1 + total
);
10073 isl_hash_hash(hash
, c_hash
);
10075 isl_hash_byte(hash
, bmap
->n_ineq
& 0xFF);
10076 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
10078 c_hash
= isl_seq_get_hash(bmap
->ineq
[i
], 1 + total
);
10079 isl_hash_hash(hash
, c_hash
);
10081 isl_hash_byte(hash
, bmap
->n_div
& 0xFF);
10082 for (i
= 0; i
< bmap
->n_div
; ++i
) {
10084 if (isl_int_is_zero(bmap
->div
[i
][0]))
10086 isl_hash_byte(hash
, i
& 0xFF);
10087 c_hash
= isl_seq_get_hash(bmap
->div
[i
], 1 + 1 + total
);
10088 isl_hash_hash(hash
, c_hash
);
10090 isl_basic_map_free(bmap
);
10094 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set
*bset
)
10096 return isl_basic_map_get_hash(bset_to_bmap(bset
));
10099 uint32_t isl_map_get_hash(__isl_keep isl_map
*map
)
10106 map
= isl_map_copy(map
);
10107 map
= isl_map_normalize(map
);
10111 hash
= isl_hash_init();
10112 for (i
= 0; i
< map
->n
; ++i
) {
10113 uint32_t bmap_hash
;
10114 bmap_hash
= isl_basic_map_get_hash(map
->p
[i
]);
10115 isl_hash_hash(hash
, bmap_hash
);
10123 uint32_t isl_set_get_hash(__isl_keep isl_set
*set
)
10125 return isl_map_get_hash(set_to_map(set
));
10128 /* Check if the value for dimension dim is completely determined
10129 * by the values of the other parameters and variables.
10130 * That is, check if dimension dim is involved in an equality.
10132 int isl_basic_set_dim_is_unique(struct isl_basic_set
*bset
, unsigned dim
)
10139 nparam
= isl_basic_set_n_param(bset
);
10140 for (i
= 0; i
< bset
->n_eq
; ++i
)
10141 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ dim
]))
10146 /* Check if the value for dimension dim is completely determined
10147 * by the values of the other parameters and variables.
10148 * That is, check if dimension dim is involved in an equality
10149 * for each of the subsets.
10151 int isl_set_dim_is_unique(struct isl_set
*set
, unsigned dim
)
10157 for (i
= 0; i
< set
->n
; ++i
) {
10159 unique
= isl_basic_set_dim_is_unique(set
->p
[i
], dim
);
10166 /* Return the number of basic maps in the (current) representation of "map".
10168 int isl_map_n_basic_map(__isl_keep isl_map
*map
)
10170 return map
? map
->n
: 0;
10173 int isl_set_n_basic_set(__isl_keep isl_set
*set
)
10175 return set
? set
->n
: 0;
10178 isl_stat
isl_map_foreach_basic_map(__isl_keep isl_map
*map
,
10179 isl_stat (*fn
)(__isl_take isl_basic_map
*bmap
, void *user
), void *user
)
10184 return isl_stat_error
;
10186 for (i
= 0; i
< map
->n
; ++i
)
10187 if (fn(isl_basic_map_copy(map
->p
[i
]), user
) < 0)
10188 return isl_stat_error
;
10190 return isl_stat_ok
;
10193 isl_stat
isl_set_foreach_basic_set(__isl_keep isl_set
*set
,
10194 isl_stat (*fn
)(__isl_take isl_basic_set
*bset
, void *user
), void *user
)
10199 return isl_stat_error
;
10201 for (i
= 0; i
< set
->n
; ++i
)
10202 if (fn(isl_basic_set_copy(set
->p
[i
]), user
) < 0)
10203 return isl_stat_error
;
10205 return isl_stat_ok
;
10208 /* Return a list of basic sets, the union of which is equal to "set".
10210 __isl_give isl_basic_set_list
*isl_set_get_basic_set_list(
10211 __isl_keep isl_set
*set
)
10214 isl_basic_set_list
*list
;
10219 list
= isl_basic_set_list_alloc(isl_set_get_ctx(set
), set
->n
);
10220 for (i
= 0; i
< set
->n
; ++i
) {
10221 isl_basic_set
*bset
;
10223 bset
= isl_basic_set_copy(set
->p
[i
]);
10224 list
= isl_basic_set_list_add(list
, bset
);
10230 __isl_give isl_basic_set
*isl_basic_set_lift(__isl_take isl_basic_set
*bset
)
10237 bset
= isl_basic_set_cow(bset
);
10241 dim
= isl_basic_set_get_space(bset
);
10242 dim
= isl_space_lift(dim
, bset
->n_div
);
10245 isl_space_free(bset
->dim
);
10247 bset
->extra
-= bset
->n_div
;
10250 bset
= isl_basic_set_finalize(bset
);
10254 isl_basic_set_free(bset
);
10258 __isl_give isl_set
*isl_set_lift(__isl_take isl_set
*set
)
10264 set
= isl_set_align_divs(set
);
10269 set
= isl_set_cow(set
);
10273 n_div
= set
->p
[0]->n_div
;
10274 dim
= isl_set_get_space(set
);
10275 dim
= isl_space_lift(dim
, n_div
);
10278 isl_space_free(set
->dim
);
10281 for (i
= 0; i
< set
->n
; ++i
) {
10282 set
->p
[i
] = isl_basic_set_lift(set
->p
[i
]);
10293 __isl_give isl_map
*isl_set_lifting(__isl_take isl_set
*set
)
10296 struct isl_basic_map
*bmap
;
10303 set
= isl_set_align_divs(set
);
10308 dim
= isl_set_get_space(set
);
10309 if (set
->n
== 0 || set
->p
[0]->n_div
== 0) {
10311 return isl_map_identity(isl_space_map_from_set(dim
));
10314 n_div
= set
->p
[0]->n_div
;
10315 dim
= isl_space_map_from_set(dim
);
10316 n_param
= isl_space_dim(dim
, isl_dim_param
);
10317 n_set
= isl_space_dim(dim
, isl_dim_in
);
10318 dim
= isl_space_extend(dim
, n_param
, n_set
, n_set
+ n_div
);
10319 bmap
= isl_basic_map_alloc_space(dim
, 0, n_set
, 2 * n_div
);
10320 for (i
= 0; i
< n_set
; ++i
)
10321 bmap
= var_equal(bmap
, i
);
10323 total
= n_param
+ n_set
+ n_set
+ n_div
;
10324 for (i
= 0; i
< n_div
; ++i
) {
10325 k
= isl_basic_map_alloc_inequality(bmap
);
10328 isl_seq_cpy(bmap
->ineq
[k
], set
->p
[0]->div
[i
]+1, 1+n_param
);
10329 isl_seq_clr(bmap
->ineq
[k
]+1+n_param
, n_set
);
10330 isl_seq_cpy(bmap
->ineq
[k
]+1+n_param
+n_set
,
10331 set
->p
[0]->div
[i
]+1+1+n_param
, n_set
+ n_div
);
10332 isl_int_neg(bmap
->ineq
[k
][1+n_param
+n_set
+n_set
+i
],
10333 set
->p
[0]->div
[i
][0]);
10335 l
= isl_basic_map_alloc_inequality(bmap
);
10338 isl_seq_neg(bmap
->ineq
[l
], bmap
->ineq
[k
], 1 + total
);
10339 isl_int_add(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0],
10340 set
->p
[0]->div
[i
][0]);
10341 isl_int_sub_ui(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], 1);
10345 bmap
= isl_basic_map_simplify(bmap
);
10346 bmap
= isl_basic_map_finalize(bmap
);
10347 return isl_map_from_basic_map(bmap
);
10350 isl_basic_map_free(bmap
);
10354 int isl_basic_set_size(__isl_keep isl_basic_set
*bset
)
10362 dim
= isl_basic_set_total_dim(bset
);
10363 size
+= bset
->n_eq
* (1 + dim
);
10364 size
+= bset
->n_ineq
* (1 + dim
);
10365 size
+= bset
->n_div
* (2 + dim
);
10370 int isl_set_size(__isl_keep isl_set
*set
)
10378 for (i
= 0; i
< set
->n
; ++i
)
10379 size
+= isl_basic_set_size(set
->p
[i
]);
10384 /* Check if there is any lower bound (if lower == 0) and/or upper
10385 * bound (if upper == 0) on the specified dim.
10387 static isl_bool
basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
10388 enum isl_dim_type type
, unsigned pos
, int lower
, int upper
)
10393 return isl_bool_error
;
10395 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
),
10396 return isl_bool_error
);
10398 pos
+= isl_basic_map_offset(bmap
, type
);
10400 for (i
= 0; i
< bmap
->n_div
; ++i
) {
10401 if (isl_int_is_zero(bmap
->div
[i
][0]))
10403 if (!isl_int_is_zero(bmap
->div
[i
][1 + pos
]))
10404 return isl_bool_true
;
10407 for (i
= 0; i
< bmap
->n_eq
; ++i
)
10408 if (!isl_int_is_zero(bmap
->eq
[i
][pos
]))
10409 return isl_bool_true
;
10411 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
10412 int sgn
= isl_int_sgn(bmap
->ineq
[i
][pos
]);
10419 return lower
&& upper
;
10422 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
10423 enum isl_dim_type type
, unsigned pos
)
10425 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 0);
10428 isl_bool
isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map
*bmap
,
10429 enum isl_dim_type type
, unsigned pos
)
10431 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 1);
10434 isl_bool
isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map
*bmap
,
10435 enum isl_dim_type type
, unsigned pos
)
10437 return basic_map_dim_is_bounded(bmap
, type
, pos
, 1, 0);
10440 int isl_map_dim_is_bounded(__isl_keep isl_map
*map
,
10441 enum isl_dim_type type
, unsigned pos
)
10448 for (i
= 0; i
< map
->n
; ++i
) {
10450 bounded
= isl_basic_map_dim_is_bounded(map
->p
[i
], type
, pos
);
10451 if (bounded
< 0 || !bounded
)
10458 /* Return 1 if the specified dim is involved in both an upper bound
10459 * and a lower bound.
10461 int isl_set_dim_is_bounded(__isl_keep isl_set
*set
,
10462 enum isl_dim_type type
, unsigned pos
)
10464 return isl_map_dim_is_bounded(set_to_map(set
), type
, pos
);
10467 /* Does "map" have a bound (according to "fn") for any of its basic maps?
10469 static isl_bool
has_any_bound(__isl_keep isl_map
*map
,
10470 enum isl_dim_type type
, unsigned pos
,
10471 isl_bool (*fn
)(__isl_keep isl_basic_map
*bmap
,
10472 enum isl_dim_type type
, unsigned pos
))
10477 return isl_bool_error
;
10479 for (i
= 0; i
< map
->n
; ++i
) {
10481 bounded
= fn(map
->p
[i
], type
, pos
);
10482 if (bounded
< 0 || bounded
)
10486 return isl_bool_false
;
10489 /* Return 1 if the specified dim is involved in any lower bound.
10491 isl_bool
isl_set_dim_has_any_lower_bound(__isl_keep isl_set
*set
,
10492 enum isl_dim_type type
, unsigned pos
)
10494 return has_any_bound(set
, type
, pos
,
10495 &isl_basic_map_dim_has_lower_bound
);
10498 /* Return 1 if the specified dim is involved in any upper bound.
10500 isl_bool
isl_set_dim_has_any_upper_bound(__isl_keep isl_set
*set
,
10501 enum isl_dim_type type
, unsigned pos
)
10503 return has_any_bound(set
, type
, pos
,
10504 &isl_basic_map_dim_has_upper_bound
);
10507 /* Does "map" have a bound (according to "fn") for all of its basic maps?
10509 static isl_bool
has_bound(__isl_keep isl_map
*map
,
10510 enum isl_dim_type type
, unsigned pos
,
10511 isl_bool (*fn
)(__isl_keep isl_basic_map
*bmap
,
10512 enum isl_dim_type type
, unsigned pos
))
10517 return isl_bool_error
;
10519 for (i
= 0; i
< map
->n
; ++i
) {
10521 bounded
= fn(map
->p
[i
], type
, pos
);
10522 if (bounded
< 0 || !bounded
)
10526 return isl_bool_true
;
10529 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10531 isl_bool
isl_set_dim_has_lower_bound(__isl_keep isl_set
*set
,
10532 enum isl_dim_type type
, unsigned pos
)
10534 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_lower_bound
);
10537 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10539 isl_bool
isl_set_dim_has_upper_bound(__isl_keep isl_set
*set
,
10540 enum isl_dim_type type
, unsigned pos
)
10542 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_upper_bound
);
10545 /* For each of the "n" variables starting at "first", determine
10546 * the sign of the variable and put the results in the first "n"
10547 * elements of the array "signs".
10549 * 1 means that the variable is non-negative
10550 * -1 means that the variable is non-positive
10551 * 0 means the variable attains both positive and negative values.
10553 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set
*bset
,
10554 unsigned first
, unsigned n
, int *signs
)
10556 isl_vec
*bound
= NULL
;
10557 struct isl_tab
*tab
= NULL
;
10558 struct isl_tab_undo
*snap
;
10561 if (!bset
|| !signs
)
10564 bound
= isl_vec_alloc(bset
->ctx
, 1 + isl_basic_set_total_dim(bset
));
10565 tab
= isl_tab_from_basic_set(bset
, 0);
10566 if (!bound
|| !tab
)
10569 isl_seq_clr(bound
->el
, bound
->size
);
10570 isl_int_set_si(bound
->el
[0], -1);
10572 snap
= isl_tab_snap(tab
);
10573 for (i
= 0; i
< n
; ++i
) {
10576 isl_int_set_si(bound
->el
[1 + first
+ i
], -1);
10577 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
10579 empty
= tab
->empty
;
10580 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
10581 if (isl_tab_rollback(tab
, snap
) < 0)
10589 isl_int_set_si(bound
->el
[1 + first
+ i
], 1);
10590 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
10592 empty
= tab
->empty
;
10593 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
10594 if (isl_tab_rollback(tab
, snap
) < 0)
10597 signs
[i
] = empty
? -1 : 0;
10601 isl_vec_free(bound
);
10605 isl_vec_free(bound
);
10609 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set
*bset
,
10610 enum isl_dim_type type
, unsigned first
, unsigned n
, int *signs
)
10612 if (!bset
|| !signs
)
10614 isl_assert(bset
->ctx
, first
+ n
<= isl_basic_set_dim(bset
, type
),
10617 first
+= pos(bset
->dim
, type
) - 1;
10618 return isl_basic_set_vars_get_sign(bset
, first
, n
, signs
);
10621 /* Is it possible for the integer division "div" to depend (possibly
10622 * indirectly) on any output dimensions?
10624 * If the div is undefined, then we conservatively assume that it
10625 * may depend on them.
10626 * Otherwise, we check if it actually depends on them or on any integer
10627 * divisions that may depend on them.
10629 static int div_may_involve_output(__isl_keep isl_basic_map
*bmap
, int div
)
10632 unsigned n_out
, o_out
;
10633 unsigned n_div
, o_div
;
10635 if (isl_int_is_zero(bmap
->div
[div
][0]))
10638 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10639 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10641 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + o_out
, n_out
) != -1)
10644 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10645 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10647 for (i
= 0; i
< n_div
; ++i
) {
10648 if (isl_int_is_zero(bmap
->div
[div
][1 + o_div
+ i
]))
10650 if (div_may_involve_output(bmap
, i
))
10657 /* Return the first integer division of "bmap" in the range
10658 * [first, first + n[ that may depend on any output dimensions and
10659 * that has a non-zero coefficient in "c" (where the first coefficient
10660 * in "c" corresponds to integer division "first").
10662 static int first_div_may_involve_output(__isl_keep isl_basic_map
*bmap
,
10663 isl_int
*c
, int first
, int n
)
10670 for (k
= first
; k
< first
+ n
; ++k
) {
10671 if (isl_int_is_zero(c
[k
]))
10673 if (div_may_involve_output(bmap
, k
))
10680 /* Look for a pair of inequality constraints in "bmap" of the form
10682 * -l + i >= 0 or i >= l
10684 * n + l - i >= 0 or i <= l + n
10686 * with n < "m" and i the output dimension at position "pos".
10687 * (Note that n >= 0 as otherwise the two constraints would conflict.)
10688 * Furthermore, "l" is only allowed to involve parameters, input dimensions
10689 * and earlier output dimensions, as well as integer divisions that do
10690 * not involve any of the output dimensions.
10692 * Return the index of the first inequality constraint or bmap->n_ineq
10693 * if no such pair can be found.
10695 static int find_modulo_constraint_pair(__isl_keep isl_basic_map
*bmap
,
10696 int pos
, isl_int m
)
10701 unsigned n_div
, o_div
;
10702 unsigned n_out
, o_out
;
10708 ctx
= isl_basic_map_get_ctx(bmap
);
10709 total
= isl_basic_map_total_dim(bmap
);
10710 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10711 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10712 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10713 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10714 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
10715 if (!isl_int_abs_eq(bmap
->ineq
[i
][o_out
+ pos
], ctx
->one
))
10717 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + o_out
+ pos
+ 1,
10718 n_out
- (pos
+ 1)) != -1)
10720 if (first_div_may_involve_output(bmap
, bmap
->ineq
[i
] + o_div
,
10723 for (j
= i
+ 1; j
< bmap
->n_ineq
; ++j
) {
10724 if (!isl_int_abs_eq(bmap
->ineq
[j
][o_out
+ pos
],
10727 if (!isl_seq_is_neg(bmap
->ineq
[i
] + 1,
10728 bmap
->ineq
[j
] + 1, total
))
10732 if (j
>= bmap
->n_ineq
)
10734 isl_int_add(bmap
->ineq
[i
][0],
10735 bmap
->ineq
[i
][0], bmap
->ineq
[j
][0]);
10736 less
= isl_int_abs_lt(bmap
->ineq
[i
][0], m
);
10737 isl_int_sub(bmap
->ineq
[i
][0],
10738 bmap
->ineq
[i
][0], bmap
->ineq
[j
][0]);
10741 if (isl_int_is_one(bmap
->ineq
[i
][o_out
+ pos
]))
10747 return bmap
->n_ineq
;
10750 /* Return the index of the equality of "bmap" that defines
10751 * the output dimension "pos" in terms of earlier dimensions.
10752 * The equality may also involve integer divisions, as long
10753 * as those integer divisions are defined in terms of
10754 * parameters or input dimensions.
10755 * In this case, *div is set to the number of integer divisions and
10756 * *ineq is set to the number of inequality constraints (provided
10757 * div and ineq are not NULL).
10759 * The equality may also involve a single integer division involving
10760 * the output dimensions (typically only output dimension "pos") as
10761 * long as the coefficient of output dimension "pos" is 1 or -1 and
10762 * there is a pair of constraints i >= l and i <= l + n, with i referring
10763 * to output dimension "pos", l an expression involving only earlier
10764 * dimensions and n smaller than the coefficient of the integer division
10765 * in the equality. In this case, the output dimension can be defined
10766 * in terms of a modulo expression that does not involve the integer division.
10767 * *div is then set to this single integer division and
10768 * *ineq is set to the index of constraint i >= l.
10770 * Return bmap->n_eq if there is no such equality.
10771 * Return -1 on error.
10773 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map
*bmap
,
10774 int pos
, int *div
, int *ineq
)
10777 unsigned n_out
, o_out
;
10778 unsigned n_div
, o_div
;
10783 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10784 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10785 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10786 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10789 *ineq
= bmap
->n_ineq
;
10792 for (j
= 0; j
< bmap
->n_eq
; ++j
) {
10793 if (isl_int_is_zero(bmap
->eq
[j
][o_out
+ pos
]))
10795 if (isl_seq_first_non_zero(bmap
->eq
[j
] + o_out
+ pos
+ 1,
10796 n_out
- (pos
+ 1)) != -1)
10798 k
= first_div_may_involve_output(bmap
, bmap
->eq
[j
] + o_div
,
10802 if (!isl_int_is_one(bmap
->eq
[j
][o_out
+ pos
]) &&
10803 !isl_int_is_negone(bmap
->eq
[j
][o_out
+ pos
]))
10805 if (first_div_may_involve_output(bmap
, bmap
->eq
[j
] + o_div
,
10806 k
+ 1, n_div
- (k
+1)) < n_div
)
10808 l
= find_modulo_constraint_pair(bmap
, pos
,
10809 bmap
->eq
[j
][o_div
+ k
]);
10812 if (l
>= bmap
->n_ineq
)
10824 /* Check if the given basic map is obviously single-valued.
10825 * In particular, for each output dimension, check that there is
10826 * an equality that defines the output dimension in terms of
10827 * earlier dimensions.
10829 isl_bool
isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map
*bmap
)
10835 return isl_bool_error
;
10837 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10839 for (i
= 0; i
< n_out
; ++i
) {
10842 eq
= isl_basic_map_output_defining_equality(bmap
, i
,
10845 return isl_bool_error
;
10846 if (eq
>= bmap
->n_eq
)
10847 return isl_bool_false
;
10850 return isl_bool_true
;
10853 /* Check if the given basic map is single-valued.
10854 * We simply compute
10858 * and check if the result is a subset of the identity mapping.
10860 isl_bool
isl_basic_map_is_single_valued(__isl_keep isl_basic_map
*bmap
)
10863 isl_basic_map
*test
;
10867 sv
= isl_basic_map_plain_is_single_valued(bmap
);
10871 test
= isl_basic_map_reverse(isl_basic_map_copy(bmap
));
10872 test
= isl_basic_map_apply_range(test
, isl_basic_map_copy(bmap
));
10874 space
= isl_basic_map_get_space(bmap
);
10875 space
= isl_space_map_from_set(isl_space_range(space
));
10876 id
= isl_basic_map_identity(space
);
10878 sv
= isl_basic_map_is_subset(test
, id
);
10880 isl_basic_map_free(test
);
10881 isl_basic_map_free(id
);
10886 /* Check if the given map is obviously single-valued.
10888 isl_bool
isl_map_plain_is_single_valued(__isl_keep isl_map
*map
)
10891 return isl_bool_error
;
10893 return isl_bool_true
;
10895 return isl_bool_false
;
10897 return isl_basic_map_plain_is_single_valued(map
->p
[0]);
10900 /* Check if the given map is single-valued.
10901 * We simply compute
10905 * and check if the result is a subset of the identity mapping.
10907 isl_bool
isl_map_is_single_valued(__isl_keep isl_map
*map
)
10914 sv
= isl_map_plain_is_single_valued(map
);
10918 test
= isl_map_reverse(isl_map_copy(map
));
10919 test
= isl_map_apply_range(test
, isl_map_copy(map
));
10921 dim
= isl_space_map_from_set(isl_space_range(isl_map_get_space(map
)));
10922 id
= isl_map_identity(dim
);
10924 sv
= isl_map_is_subset(test
, id
);
10926 isl_map_free(test
);
10932 isl_bool
isl_map_is_injective(__isl_keep isl_map
*map
)
10936 map
= isl_map_copy(map
);
10937 map
= isl_map_reverse(map
);
10938 in
= isl_map_is_single_valued(map
);
10944 /* Check if the given map is obviously injective.
10946 isl_bool
isl_map_plain_is_injective(__isl_keep isl_map
*map
)
10950 map
= isl_map_copy(map
);
10951 map
= isl_map_reverse(map
);
10952 in
= isl_map_plain_is_single_valued(map
);
10958 isl_bool
isl_map_is_bijective(__isl_keep isl_map
*map
)
10962 sv
= isl_map_is_single_valued(map
);
10966 return isl_map_is_injective(map
);
10969 isl_bool
isl_set_is_singleton(__isl_keep isl_set
*set
)
10971 return isl_map_is_single_valued(set_to_map(set
));
10974 /* Does "map" only map elements to themselves?
10976 * If the domain and range spaces are different, then "map"
10977 * is considered not to be an identity relation, even if it is empty.
10978 * Otherwise, construct the maximal identity relation and
10979 * check whether "map" is a subset of this relation.
10981 isl_bool
isl_map_is_identity(__isl_keep isl_map
*map
)
10985 isl_bool equal
, is_identity
;
10987 space
= isl_map_get_space(map
);
10988 equal
= isl_space_tuple_is_equal(space
, isl_dim_in
, space
, isl_dim_out
);
10989 isl_space_free(space
);
10990 if (equal
< 0 || !equal
)
10993 id
= isl_map_identity(isl_map_get_space(map
));
10994 is_identity
= isl_map_is_subset(map
, id
);
10997 return is_identity
;
11000 int isl_map_is_translation(__isl_keep isl_map
*map
)
11005 delta
= isl_map_deltas(isl_map_copy(map
));
11006 ok
= isl_set_is_singleton(delta
);
11007 isl_set_free(delta
);
11012 static int unique(isl_int
*p
, unsigned pos
, unsigned len
)
11014 if (isl_seq_first_non_zero(p
, pos
) != -1)
11016 if (isl_seq_first_non_zero(p
+ pos
+ 1, len
- pos
- 1) != -1)
11021 int isl_basic_set_is_box(__isl_keep isl_basic_set
*bset
)
11030 if (isl_basic_set_dim(bset
, isl_dim_div
) != 0)
11033 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
11034 ovar
= isl_space_offset(bset
->dim
, isl_dim_set
);
11035 for (j
= 0; j
< nvar
; ++j
) {
11036 int lower
= 0, upper
= 0;
11037 for (i
= 0; i
< bset
->n_eq
; ++i
) {
11038 if (isl_int_is_zero(bset
->eq
[i
][1 + ovar
+ j
]))
11040 if (!unique(bset
->eq
[i
] + 1 + ovar
, j
, nvar
))
11044 if (i
< bset
->n_eq
)
11046 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
11047 if (isl_int_is_zero(bset
->ineq
[i
][1 + ovar
+ j
]))
11049 if (!unique(bset
->ineq
[i
] + 1 + ovar
, j
, nvar
))
11051 if (isl_int_is_pos(bset
->ineq
[i
][1 + ovar
+ j
]))
11056 if (!lower
|| !upper
)
11063 int isl_set_is_box(__isl_keep isl_set
*set
)
11070 return isl_basic_set_is_box(set
->p
[0]);
11073 isl_bool
isl_basic_set_is_wrapping(__isl_keep isl_basic_set
*bset
)
11076 return isl_bool_error
;
11078 return isl_space_is_wrapping(bset
->dim
);
11081 isl_bool
isl_set_is_wrapping(__isl_keep isl_set
*set
)
11084 return isl_bool_error
;
11086 return isl_space_is_wrapping(set
->dim
);
11089 /* Modify the space of "map" through a call to "change".
11090 * If "can_change" is set (not NULL), then first call it to check
11091 * if the modification is allowed, printing the error message "cannot_change"
11094 static __isl_give isl_map
*isl_map_change_space(__isl_take isl_map
*map
,
11095 isl_bool (*can_change
)(__isl_keep isl_map
*map
),
11096 const char *cannot_change
,
11097 __isl_give isl_space
*(*change
)(__isl_take isl_space
*space
))
11105 ok
= can_change
? can_change(map
) : isl_bool_true
;
11107 return isl_map_free(map
);
11109 isl_die(isl_map_get_ctx(map
), isl_error_invalid
, cannot_change
,
11110 return isl_map_free(map
));
11112 space
= change(isl_map_get_space(map
));
11113 map
= isl_map_reset_space(map
, space
);
11118 /* Is the domain of "map" a wrapped relation?
11120 isl_bool
isl_map_domain_is_wrapping(__isl_keep isl_map
*map
)
11123 return isl_bool_error
;
11125 return isl_space_domain_is_wrapping(map
->dim
);
11128 /* Is the range of "map" a wrapped relation?
11130 isl_bool
isl_map_range_is_wrapping(__isl_keep isl_map
*map
)
11133 return isl_bool_error
;
11135 return isl_space_range_is_wrapping(map
->dim
);
11138 __isl_give isl_basic_set
*isl_basic_map_wrap(__isl_take isl_basic_map
*bmap
)
11140 bmap
= isl_basic_map_cow(bmap
);
11144 bmap
->dim
= isl_space_wrap(bmap
->dim
);
11148 bmap
= isl_basic_map_finalize(bmap
);
11150 return bset_from_bmap(bmap
);
11152 isl_basic_map_free(bmap
);
11156 /* Given a map A -> B, return the set (A -> B).
11158 __isl_give isl_set
*isl_map_wrap(__isl_take isl_map
*map
)
11160 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_wrap
);
11163 __isl_give isl_basic_map
*isl_basic_set_unwrap(__isl_take isl_basic_set
*bset
)
11165 bset
= isl_basic_set_cow(bset
);
11169 bset
->dim
= isl_space_unwrap(bset
->dim
);
11173 bset
= isl_basic_set_finalize(bset
);
11175 return bset_to_bmap(bset
);
11177 isl_basic_set_free(bset
);
11181 /* Given a set (A -> B), return the map A -> B.
11182 * Error out if "set" is not of the form (A -> B).
11184 __isl_give isl_map
*isl_set_unwrap(__isl_take isl_set
*set
)
11186 return isl_map_change_space(set
, &isl_set_is_wrapping
,
11187 "not a wrapping set", &isl_space_unwrap
);
11190 __isl_give isl_basic_map
*isl_basic_map_reset(__isl_take isl_basic_map
*bmap
,
11191 enum isl_dim_type type
)
11196 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
11199 bmap
= isl_basic_map_cow(bmap
);
11203 bmap
->dim
= isl_space_reset(bmap
->dim
, type
);
11207 bmap
= isl_basic_map_finalize(bmap
);
11211 isl_basic_map_free(bmap
);
11215 __isl_give isl_map
*isl_map_reset(__isl_take isl_map
*map
,
11216 enum isl_dim_type type
)
11223 if (!isl_space_is_named_or_nested(map
->dim
, type
))
11226 map
= isl_map_cow(map
);
11230 for (i
= 0; i
< map
->n
; ++i
) {
11231 map
->p
[i
] = isl_basic_map_reset(map
->p
[i
], type
);
11235 map
->dim
= isl_space_reset(map
->dim
, type
);
11245 __isl_give isl_basic_map
*isl_basic_map_flatten(__isl_take isl_basic_map
*bmap
)
11250 if (!bmap
->dim
->nested
[0] && !bmap
->dim
->nested
[1])
11253 bmap
= isl_basic_map_cow(bmap
);
11257 bmap
->dim
= isl_space_flatten(bmap
->dim
);
11261 bmap
= isl_basic_map_finalize(bmap
);
11265 isl_basic_map_free(bmap
);
11269 __isl_give isl_basic_set
*isl_basic_set_flatten(__isl_take isl_basic_set
*bset
)
11271 return bset_from_bmap(isl_basic_map_flatten(bset_to_bmap(bset
)));
11274 __isl_give isl_basic_map
*isl_basic_map_flatten_domain(
11275 __isl_take isl_basic_map
*bmap
)
11280 if (!bmap
->dim
->nested
[0])
11283 bmap
= isl_basic_map_cow(bmap
);
11287 bmap
->dim
= isl_space_flatten_domain(bmap
->dim
);
11291 bmap
= isl_basic_map_finalize(bmap
);
11295 isl_basic_map_free(bmap
);
11299 __isl_give isl_basic_map
*isl_basic_map_flatten_range(
11300 __isl_take isl_basic_map
*bmap
)
11305 if (!bmap
->dim
->nested
[1])
11308 bmap
= isl_basic_map_cow(bmap
);
11312 bmap
->dim
= isl_space_flatten_range(bmap
->dim
);
11316 bmap
= isl_basic_map_finalize(bmap
);
11320 isl_basic_map_free(bmap
);
11324 /* Remove any internal structure from the spaces of domain and range of "map".
11326 __isl_give isl_map
*isl_map_flatten(__isl_take isl_map
*map
)
11331 if (!map
->dim
->nested
[0] && !map
->dim
->nested
[1])
11334 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten
);
11337 __isl_give isl_set
*isl_set_flatten(__isl_take isl_set
*set
)
11339 return set_from_map(isl_map_flatten(set_to_map(set
)));
11342 __isl_give isl_map
*isl_set_flatten_map(__isl_take isl_set
*set
)
11344 isl_space
*dim
, *flat_dim
;
11347 dim
= isl_set_get_space(set
);
11348 flat_dim
= isl_space_flatten(isl_space_copy(dim
));
11349 map
= isl_map_identity(isl_space_join(isl_space_reverse(dim
), flat_dim
));
11350 map
= isl_map_intersect_domain(map
, set
);
11355 /* Remove any internal structure from the space of the domain of "map".
11357 __isl_give isl_map
*isl_map_flatten_domain(__isl_take isl_map
*map
)
11362 if (!map
->dim
->nested
[0])
11365 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten_domain
);
11368 /* Remove any internal structure from the space of the range of "map".
11370 __isl_give isl_map
*isl_map_flatten_range(__isl_take isl_map
*map
)
11375 if (!map
->dim
->nested
[1])
11378 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten_range
);
11381 /* Reorder the dimensions of "bmap" according to the given dim_map
11382 * and set the dimension specification to "dim" and
11383 * perform Gaussian elimination on the result.
11385 __isl_give isl_basic_map
*isl_basic_map_realign(__isl_take isl_basic_map
*bmap
,
11386 __isl_take isl_space
*dim
, __isl_take
struct isl_dim_map
*dim_map
)
11388 isl_basic_map
*res
;
11391 bmap
= isl_basic_map_cow(bmap
);
11392 if (!bmap
|| !dim
|| !dim_map
)
11395 flags
= bmap
->flags
;
11396 ISL_FL_CLR(flags
, ISL_BASIC_MAP_FINAL
);
11397 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED
);
11398 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
11399 res
= isl_basic_map_alloc_space(dim
,
11400 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
11401 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
11403 res
->flags
= flags
;
11404 res
= isl_basic_map_gauss(res
, NULL
);
11405 res
= isl_basic_map_finalize(res
);
11409 isl_basic_map_free(bmap
);
11410 isl_space_free(dim
);
11414 /* Reorder the dimensions of "map" according to given reordering.
11416 __isl_give isl_map
*isl_map_realign(__isl_take isl_map
*map
,
11417 __isl_take isl_reordering
*r
)
11420 struct isl_dim_map
*dim_map
;
11422 map
= isl_map_cow(map
);
11423 dim_map
= isl_dim_map_from_reordering(r
);
11424 if (!map
|| !r
|| !dim_map
)
11427 for (i
= 0; i
< map
->n
; ++i
) {
11428 struct isl_dim_map
*dim_map_i
;
11430 dim_map_i
= isl_dim_map_extend(dim_map
, map
->p
[i
]);
11432 map
->p
[i
] = isl_basic_map_realign(map
->p
[i
],
11433 isl_space_copy(r
->dim
), dim_map_i
);
11439 map
= isl_map_reset_space(map
, isl_space_copy(r
->dim
));
11441 isl_reordering_free(r
);
11447 isl_reordering_free(r
);
11451 __isl_give isl_set
*isl_set_realign(__isl_take isl_set
*set
,
11452 __isl_take isl_reordering
*r
)
11454 return set_from_map(isl_map_realign(set_to_map(set
), r
));
11457 __isl_give isl_map
*isl_map_align_params(__isl_take isl_map
*map
,
11458 __isl_take isl_space
*model
)
11462 if (!map
|| !model
)
11465 ctx
= isl_space_get_ctx(model
);
11466 if (!isl_space_has_named_params(model
))
11467 isl_die(ctx
, isl_error_invalid
,
11468 "model has unnamed parameters", goto error
);
11469 if (!isl_space_has_named_params(map
->dim
))
11470 isl_die(ctx
, isl_error_invalid
,
11471 "relation has unnamed parameters", goto error
);
11472 if (!isl_space_match(map
->dim
, isl_dim_param
, model
, isl_dim_param
)) {
11473 isl_reordering
*exp
;
11475 model
= isl_space_drop_dims(model
, isl_dim_in
,
11476 0, isl_space_dim(model
, isl_dim_in
));
11477 model
= isl_space_drop_dims(model
, isl_dim_out
,
11478 0, isl_space_dim(model
, isl_dim_out
));
11479 exp
= isl_parameter_alignment_reordering(map
->dim
, model
);
11480 exp
= isl_reordering_extend_space(exp
, isl_map_get_space(map
));
11481 map
= isl_map_realign(map
, exp
);
11484 isl_space_free(model
);
11487 isl_space_free(model
);
11492 __isl_give isl_set
*isl_set_align_params(__isl_take isl_set
*set
,
11493 __isl_take isl_space
*model
)
11495 return isl_map_align_params(set
, model
);
11498 /* Align the parameters of "bmap" to those of "model", introducing
11499 * additional parameters if needed.
11501 __isl_give isl_basic_map
*isl_basic_map_align_params(
11502 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*model
)
11506 if (!bmap
|| !model
)
11509 ctx
= isl_space_get_ctx(model
);
11510 if (!isl_space_has_named_params(model
))
11511 isl_die(ctx
, isl_error_invalid
,
11512 "model has unnamed parameters", goto error
);
11513 if (!isl_space_has_named_params(bmap
->dim
))
11514 isl_die(ctx
, isl_error_invalid
,
11515 "relation has unnamed parameters", goto error
);
11516 if (!isl_space_match(bmap
->dim
, isl_dim_param
, model
, isl_dim_param
)) {
11517 isl_reordering
*exp
;
11518 struct isl_dim_map
*dim_map
;
11520 model
= isl_space_drop_dims(model
, isl_dim_in
,
11521 0, isl_space_dim(model
, isl_dim_in
));
11522 model
= isl_space_drop_dims(model
, isl_dim_out
,
11523 0, isl_space_dim(model
, isl_dim_out
));
11524 exp
= isl_parameter_alignment_reordering(bmap
->dim
, model
);
11525 exp
= isl_reordering_extend_space(exp
,
11526 isl_basic_map_get_space(bmap
));
11527 dim_map
= isl_dim_map_from_reordering(exp
);
11528 bmap
= isl_basic_map_realign(bmap
,
11529 exp
? isl_space_copy(exp
->dim
) : NULL
,
11530 isl_dim_map_extend(dim_map
, bmap
));
11531 isl_reordering_free(exp
);
11535 isl_space_free(model
);
11538 isl_space_free(model
);
11539 isl_basic_map_free(bmap
);
11543 /* Align the parameters of "bset" to those of "model", introducing
11544 * additional parameters if needed.
11546 __isl_give isl_basic_set
*isl_basic_set_align_params(
11547 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*model
)
11549 return isl_basic_map_align_params(bset
, model
);
11552 __isl_give isl_mat
*isl_basic_map_equalities_matrix(
11553 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
11554 enum isl_dim_type c2
, enum isl_dim_type c3
,
11555 enum isl_dim_type c4
, enum isl_dim_type c5
)
11557 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11558 struct isl_mat
*mat
;
11564 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_eq
,
11565 isl_basic_map_total_dim(bmap
) + 1);
11568 for (i
= 0; i
< bmap
->n_eq
; ++i
)
11569 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11570 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11571 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11572 isl_int_set(mat
->row
[i
][pos
],
11573 bmap
->eq
[i
][off
+ k
]);
11581 __isl_give isl_mat
*isl_basic_map_inequalities_matrix(
11582 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
11583 enum isl_dim_type c2
, enum isl_dim_type c3
,
11584 enum isl_dim_type c4
, enum isl_dim_type c5
)
11586 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11587 struct isl_mat
*mat
;
11593 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_ineq
,
11594 isl_basic_map_total_dim(bmap
) + 1);
11597 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
11598 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11599 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11600 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11601 isl_int_set(mat
->row
[i
][pos
],
11602 bmap
->ineq
[i
][off
+ k
]);
11610 __isl_give isl_basic_map
*isl_basic_map_from_constraint_matrices(
11611 __isl_take isl_space
*dim
,
11612 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
11613 enum isl_dim_type c2
, enum isl_dim_type c3
,
11614 enum isl_dim_type c4
, enum isl_dim_type c5
)
11616 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11617 isl_basic_map
*bmap
;
11623 if (!dim
|| !eq
|| !ineq
)
11626 if (eq
->n_col
!= ineq
->n_col
)
11627 isl_die(dim
->ctx
, isl_error_invalid
,
11628 "equalities and inequalities matrices should have "
11629 "same number of columns", goto error
);
11631 total
= 1 + isl_space_dim(dim
, isl_dim_all
);
11633 if (eq
->n_col
< total
)
11634 isl_die(dim
->ctx
, isl_error_invalid
,
11635 "number of columns too small", goto error
);
11637 extra
= eq
->n_col
- total
;
11639 bmap
= isl_basic_map_alloc_space(isl_space_copy(dim
), extra
,
11640 eq
->n_row
, ineq
->n_row
);
11643 for (i
= 0; i
< extra
; ++i
) {
11644 k
= isl_basic_map_alloc_div(bmap
);
11647 isl_int_set_si(bmap
->div
[k
][0], 0);
11649 for (i
= 0; i
< eq
->n_row
; ++i
) {
11650 l
= isl_basic_map_alloc_equality(bmap
);
11653 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11654 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11655 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11656 isl_int_set(bmap
->eq
[l
][off
+ k
],
11662 for (i
= 0; i
< ineq
->n_row
; ++i
) {
11663 l
= isl_basic_map_alloc_inequality(bmap
);
11666 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11667 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11668 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11669 isl_int_set(bmap
->ineq
[l
][off
+ k
],
11670 ineq
->row
[i
][pos
]);
11676 isl_space_free(dim
);
11678 isl_mat_free(ineq
);
11680 bmap
= isl_basic_map_simplify(bmap
);
11681 return isl_basic_map_finalize(bmap
);
11683 isl_space_free(dim
);
11685 isl_mat_free(ineq
);
11689 __isl_give isl_mat
*isl_basic_set_equalities_matrix(
11690 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
11691 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
11693 return isl_basic_map_equalities_matrix(bset_to_bmap(bset
),
11694 c1
, c2
, c3
, c4
, isl_dim_in
);
11697 __isl_give isl_mat
*isl_basic_set_inequalities_matrix(
11698 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
11699 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
11701 return isl_basic_map_inequalities_matrix(bset_to_bmap(bset
),
11702 c1
, c2
, c3
, c4
, isl_dim_in
);
11705 __isl_give isl_basic_set
*isl_basic_set_from_constraint_matrices(
11706 __isl_take isl_space
*dim
,
11707 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
11708 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
11710 isl_basic_map
*bmap
;
11711 bmap
= isl_basic_map_from_constraint_matrices(dim
, eq
, ineq
,
11712 c1
, c2
, c3
, c4
, isl_dim_in
);
11713 return bset_from_bmap(bmap
);
11716 isl_bool
isl_basic_map_can_zip(__isl_keep isl_basic_map
*bmap
)
11719 return isl_bool_error
;
11721 return isl_space_can_zip(bmap
->dim
);
11724 isl_bool
isl_map_can_zip(__isl_keep isl_map
*map
)
11727 return isl_bool_error
;
11729 return isl_space_can_zip(map
->dim
);
11732 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
11733 * (A -> C) -> (B -> D).
11735 __isl_give isl_basic_map
*isl_basic_map_zip(__isl_take isl_basic_map
*bmap
)
11744 if (!isl_basic_map_can_zip(bmap
))
11745 isl_die(bmap
->ctx
, isl_error_invalid
,
11746 "basic map cannot be zipped", goto error
);
11747 pos
= isl_basic_map_offset(bmap
, isl_dim_in
) +
11748 isl_space_dim(bmap
->dim
->nested
[0], isl_dim_in
);
11749 n1
= isl_space_dim(bmap
->dim
->nested
[0], isl_dim_out
);
11750 n2
= isl_space_dim(bmap
->dim
->nested
[1], isl_dim_in
);
11751 bmap
= isl_basic_map_cow(bmap
);
11752 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
11755 bmap
->dim
= isl_space_zip(bmap
->dim
);
11758 bmap
= isl_basic_map_mark_final(bmap
);
11761 isl_basic_map_free(bmap
);
11765 /* Given a map (A -> B) -> (C -> D), return the corresponding map
11766 * (A -> C) -> (B -> D).
11768 __isl_give isl_map
*isl_map_zip(__isl_take isl_map
*map
)
11775 if (!isl_map_can_zip(map
))
11776 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be zipped",
11779 map
= isl_map_cow(map
);
11783 for (i
= 0; i
< map
->n
; ++i
) {
11784 map
->p
[i
] = isl_basic_map_zip(map
->p
[i
]);
11789 map
->dim
= isl_space_zip(map
->dim
);
11799 /* Can we apply isl_basic_map_curry to "bmap"?
11800 * That is, does it have a nested relation in its domain?
11802 isl_bool
isl_basic_map_can_curry(__isl_keep isl_basic_map
*bmap
)
11805 return isl_bool_error
;
11807 return isl_space_can_curry(bmap
->dim
);
11810 /* Can we apply isl_map_curry to "map"?
11811 * That is, does it have a nested relation in its domain?
11813 isl_bool
isl_map_can_curry(__isl_keep isl_map
*map
)
11816 return isl_bool_error
;
11818 return isl_space_can_curry(map
->dim
);
11821 /* Given a basic map (A -> B) -> C, return the corresponding basic map
11824 __isl_give isl_basic_map
*isl_basic_map_curry(__isl_take isl_basic_map
*bmap
)
11830 if (!isl_basic_map_can_curry(bmap
))
11831 isl_die(bmap
->ctx
, isl_error_invalid
,
11832 "basic map cannot be curried", goto error
);
11833 bmap
= isl_basic_map_cow(bmap
);
11836 bmap
->dim
= isl_space_curry(bmap
->dim
);
11839 bmap
= isl_basic_map_mark_final(bmap
);
11842 isl_basic_map_free(bmap
);
11846 /* Given a map (A -> B) -> C, return the corresponding map
11849 __isl_give isl_map
*isl_map_curry(__isl_take isl_map
*map
)
11851 return isl_map_change_space(map
, &isl_map_can_curry
,
11852 "map cannot be curried", &isl_space_curry
);
11855 /* Can isl_map_range_curry be applied to "map"?
11856 * That is, does it have a nested relation in its range,
11857 * the domain of which is itself a nested relation?
11859 isl_bool
isl_map_can_range_curry(__isl_keep isl_map
*map
)
11862 return isl_bool_error
;
11864 return isl_space_can_range_curry(map
->dim
);
11867 /* Given a map A -> ((B -> C) -> D), return the corresponding map
11868 * A -> (B -> (C -> D)).
11870 __isl_give isl_map
*isl_map_range_curry(__isl_take isl_map
*map
)
11872 return isl_map_change_space(map
, &isl_map_can_range_curry
,
11873 "map range cannot be curried",
11874 &isl_space_range_curry
);
11877 /* Can we apply isl_basic_map_uncurry to "bmap"?
11878 * That is, does it have a nested relation in its domain?
11880 isl_bool
isl_basic_map_can_uncurry(__isl_keep isl_basic_map
*bmap
)
11883 return isl_bool_error
;
11885 return isl_space_can_uncurry(bmap
->dim
);
11888 /* Can we apply isl_map_uncurry to "map"?
11889 * That is, does it have a nested relation in its domain?
11891 isl_bool
isl_map_can_uncurry(__isl_keep isl_map
*map
)
11894 return isl_bool_error
;
11896 return isl_space_can_uncurry(map
->dim
);
11899 /* Given a basic map A -> (B -> C), return the corresponding basic map
11902 __isl_give isl_basic_map
*isl_basic_map_uncurry(__isl_take isl_basic_map
*bmap
)
11908 if (!isl_basic_map_can_uncurry(bmap
))
11909 isl_die(bmap
->ctx
, isl_error_invalid
,
11910 "basic map cannot be uncurried",
11911 return isl_basic_map_free(bmap
));
11912 bmap
= isl_basic_map_cow(bmap
);
11915 bmap
->dim
= isl_space_uncurry(bmap
->dim
);
11917 return isl_basic_map_free(bmap
);
11918 bmap
= isl_basic_map_mark_final(bmap
);
11922 /* Given a map A -> (B -> C), return the corresponding map
11925 __isl_give isl_map
*isl_map_uncurry(__isl_take isl_map
*map
)
11927 return isl_map_change_space(map
, &isl_map_can_uncurry
,
11928 "map cannot be uncurried", &isl_space_uncurry
);
11931 /* Construct a basic map mapping the domain of the affine expression
11932 * to a one-dimensional range prescribed by the affine expression.
11933 * If "rational" is set, then construct a rational basic map.
11935 * A NaN affine expression cannot be converted to a basic map.
11937 static __isl_give isl_basic_map
*isl_basic_map_from_aff2(
11938 __isl_take isl_aff
*aff
, int rational
)
11943 isl_local_space
*ls
;
11944 isl_basic_map
*bmap
= NULL
;
11948 is_nan
= isl_aff_is_nan(aff
);
11952 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
11953 "cannot convert NaN", goto error
);
11955 ls
= isl_aff_get_local_space(aff
);
11956 bmap
= isl_basic_map_from_local_space(ls
);
11957 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
11958 k
= isl_basic_map_alloc_equality(bmap
);
11962 pos
= isl_basic_map_offset(bmap
, isl_dim_out
);
11963 isl_seq_cpy(bmap
->eq
[k
], aff
->v
->el
+ 1, pos
);
11964 isl_int_neg(bmap
->eq
[k
][pos
], aff
->v
->el
[0]);
11965 isl_seq_cpy(bmap
->eq
[k
] + pos
+ 1, aff
->v
->el
+ 1 + pos
,
11966 aff
->v
->size
- (pos
+ 1));
11970 bmap
= isl_basic_map_set_rational(bmap
);
11971 bmap
= isl_basic_map_finalize(bmap
);
11975 isl_basic_map_free(bmap
);
11979 /* Construct a basic map mapping the domain of the affine expression
11980 * to a one-dimensional range prescribed by the affine expression.
11982 __isl_give isl_basic_map
*isl_basic_map_from_aff(__isl_take isl_aff
*aff
)
11984 return isl_basic_map_from_aff2(aff
, 0);
11987 /* Construct a map mapping the domain of the affine expression
11988 * to a one-dimensional range prescribed by the affine expression.
11990 __isl_give isl_map
*isl_map_from_aff(__isl_take isl_aff
*aff
)
11992 isl_basic_map
*bmap
;
11994 bmap
= isl_basic_map_from_aff(aff
);
11995 return isl_map_from_basic_map(bmap
);
11998 /* Construct a basic map mapping the domain the multi-affine expression
11999 * to its range, with each dimension in the range equated to the
12000 * corresponding affine expression.
12001 * If "rational" is set, then construct a rational basic map.
12003 __isl_give isl_basic_map
*isl_basic_map_from_multi_aff2(
12004 __isl_take isl_multi_aff
*maff
, int rational
)
12008 isl_basic_map
*bmap
;
12013 if (isl_space_dim(maff
->space
, isl_dim_out
) != maff
->n
)
12014 isl_die(isl_multi_aff_get_ctx(maff
), isl_error_internal
,
12015 "invalid space", goto error
);
12017 space
= isl_space_domain(isl_multi_aff_get_space(maff
));
12018 bmap
= isl_basic_map_universe(isl_space_from_domain(space
));
12020 bmap
= isl_basic_map_set_rational(bmap
);
12022 for (i
= 0; i
< maff
->n
; ++i
) {
12024 isl_basic_map
*bmap_i
;
12026 aff
= isl_aff_copy(maff
->p
[i
]);
12027 bmap_i
= isl_basic_map_from_aff2(aff
, rational
);
12029 bmap
= isl_basic_map_flat_range_product(bmap
, bmap_i
);
12032 bmap
= isl_basic_map_reset_space(bmap
, isl_multi_aff_get_space(maff
));
12034 isl_multi_aff_free(maff
);
12037 isl_multi_aff_free(maff
);
12041 /* Construct a basic map mapping the domain the multi-affine expression
12042 * to its range, with each dimension in the range equated to the
12043 * corresponding affine expression.
12045 __isl_give isl_basic_map
*isl_basic_map_from_multi_aff(
12046 __isl_take isl_multi_aff
*ma
)
12048 return isl_basic_map_from_multi_aff2(ma
, 0);
12051 /* Construct a map mapping the domain the multi-affine expression
12052 * to its range, with each dimension in the range equated to the
12053 * corresponding affine expression.
12055 __isl_give isl_map
*isl_map_from_multi_aff(__isl_take isl_multi_aff
*maff
)
12057 isl_basic_map
*bmap
;
12059 bmap
= isl_basic_map_from_multi_aff(maff
);
12060 return isl_map_from_basic_map(bmap
);
12063 /* Construct a basic map mapping a domain in the given space to
12064 * to an n-dimensional range, with n the number of elements in the list,
12065 * where each coordinate in the range is prescribed by the
12066 * corresponding affine expression.
12067 * The domains of all affine expressions in the list are assumed to match
12070 __isl_give isl_basic_map
*isl_basic_map_from_aff_list(
12071 __isl_take isl_space
*domain_dim
, __isl_take isl_aff_list
*list
)
12075 isl_basic_map
*bmap
;
12080 dim
= isl_space_from_domain(domain_dim
);
12081 bmap
= isl_basic_map_universe(dim
);
12083 for (i
= 0; i
< list
->n
; ++i
) {
12085 isl_basic_map
*bmap_i
;
12087 aff
= isl_aff_copy(list
->p
[i
]);
12088 bmap_i
= isl_basic_map_from_aff(aff
);
12090 bmap
= isl_basic_map_flat_range_product(bmap
, bmap_i
);
12093 isl_aff_list_free(list
);
12097 __isl_give isl_set
*isl_set_equate(__isl_take isl_set
*set
,
12098 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12100 return isl_map_equate(set
, type1
, pos1
, type2
, pos2
);
12103 /* Construct a basic map where the given dimensions are equal to each other.
12105 static __isl_give isl_basic_map
*equator(__isl_take isl_space
*space
,
12106 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12108 isl_basic_map
*bmap
= NULL
;
12114 if (pos1
>= isl_space_dim(space
, type1
))
12115 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
12116 "index out of bounds", goto error
);
12117 if (pos2
>= isl_space_dim(space
, type2
))
12118 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
12119 "index out of bounds", goto error
);
12121 if (type1
== type2
&& pos1
== pos2
)
12122 return isl_basic_map_universe(space
);
12124 bmap
= isl_basic_map_alloc_space(isl_space_copy(space
), 0, 1, 0);
12125 i
= isl_basic_map_alloc_equality(bmap
);
12128 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
12129 pos1
+= isl_basic_map_offset(bmap
, type1
);
12130 pos2
+= isl_basic_map_offset(bmap
, type2
);
12131 isl_int_set_si(bmap
->eq
[i
][pos1
], -1);
12132 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
12133 bmap
= isl_basic_map_finalize(bmap
);
12134 isl_space_free(space
);
12137 isl_space_free(space
);
12138 isl_basic_map_free(bmap
);
12142 /* Add a constraint imposing that the given two dimensions are equal.
12144 __isl_give isl_basic_map
*isl_basic_map_equate(__isl_take isl_basic_map
*bmap
,
12145 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12149 eq
= equator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
12151 bmap
= isl_basic_map_intersect(bmap
, eq
);
12156 /* Add a constraint imposing that the given two dimensions are equal.
12158 __isl_give isl_map
*isl_map_equate(__isl_take isl_map
*map
,
12159 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12161 isl_basic_map
*bmap
;
12163 bmap
= equator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
12165 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
12170 /* Add a constraint imposing that the given two dimensions have opposite values.
12172 __isl_give isl_map
*isl_map_oppose(__isl_take isl_map
*map
,
12173 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12175 isl_basic_map
*bmap
= NULL
;
12181 if (pos1
>= isl_map_dim(map
, type1
))
12182 isl_die(map
->ctx
, isl_error_invalid
,
12183 "index out of bounds", goto error
);
12184 if (pos2
>= isl_map_dim(map
, type2
))
12185 isl_die(map
->ctx
, isl_error_invalid
,
12186 "index out of bounds", goto error
);
12188 bmap
= isl_basic_map_alloc_space(isl_map_get_space(map
), 0, 1, 0);
12189 i
= isl_basic_map_alloc_equality(bmap
);
12192 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
12193 pos1
+= isl_basic_map_offset(bmap
, type1
);
12194 pos2
+= isl_basic_map_offset(bmap
, type2
);
12195 isl_int_set_si(bmap
->eq
[i
][pos1
], 1);
12196 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
12197 bmap
= isl_basic_map_finalize(bmap
);
12199 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
12203 isl_basic_map_free(bmap
);
12208 /* Construct a constraint imposing that the value of the first dimension is
12209 * greater than or equal to that of the second.
12211 static __isl_give isl_constraint
*constraint_order_ge(
12212 __isl_take isl_space
*space
, enum isl_dim_type type1
, int pos1
,
12213 enum isl_dim_type type2
, int pos2
)
12220 c
= isl_constraint_alloc_inequality(isl_local_space_from_space(space
));
12222 if (pos1
>= isl_constraint_dim(c
, type1
))
12223 isl_die(isl_constraint_get_ctx(c
), isl_error_invalid
,
12224 "index out of bounds", return isl_constraint_free(c
));
12225 if (pos2
>= isl_constraint_dim(c
, type2
))
12226 isl_die(isl_constraint_get_ctx(c
), isl_error_invalid
,
12227 "index out of bounds", return isl_constraint_free(c
));
12229 if (type1
== type2
&& pos1
== pos2
)
12232 c
= isl_constraint_set_coefficient_si(c
, type1
, pos1
, 1);
12233 c
= isl_constraint_set_coefficient_si(c
, type2
, pos2
, -1);
12238 /* Add a constraint imposing that the value of the first dimension is
12239 * greater than or equal to that of the second.
12241 __isl_give isl_basic_map
*isl_basic_map_order_ge(__isl_take isl_basic_map
*bmap
,
12242 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12247 if (type1
== type2
&& pos1
== pos2
)
12249 space
= isl_basic_map_get_space(bmap
);
12250 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
12251 bmap
= isl_basic_map_add_constraint(bmap
, c
);
12256 /* Add a constraint imposing that the value of the first dimension is
12257 * greater than or equal to that of the second.
12259 __isl_give isl_map
*isl_map_order_ge(__isl_take isl_map
*map
,
12260 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12265 if (type1
== type2
&& pos1
== pos2
)
12267 space
= isl_map_get_space(map
);
12268 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
12269 map
= isl_map_add_constraint(map
, c
);
12274 /* Add a constraint imposing that the value of the first dimension is
12275 * less than or equal to that of the second.
12277 __isl_give isl_map
*isl_map_order_le(__isl_take isl_map
*map
,
12278 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12280 return isl_map_order_ge(map
, type2
, pos2
, type1
, pos1
);
12283 /* Construct a basic map where the value of the first dimension is
12284 * greater than that of the second.
12286 static __isl_give isl_basic_map
*greator(__isl_take isl_space
*space
,
12287 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12289 isl_basic_map
*bmap
= NULL
;
12295 if (pos1
>= isl_space_dim(space
, type1
))
12296 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
12297 "index out of bounds", goto error
);
12298 if (pos2
>= isl_space_dim(space
, type2
))
12299 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
12300 "index out of bounds", goto error
);
12302 if (type1
== type2
&& pos1
== pos2
)
12303 return isl_basic_map_empty(space
);
12305 bmap
= isl_basic_map_alloc_space(space
, 0, 0, 1);
12306 i
= isl_basic_map_alloc_inequality(bmap
);
12308 return isl_basic_map_free(bmap
);
12309 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
12310 pos1
+= isl_basic_map_offset(bmap
, type1
);
12311 pos2
+= isl_basic_map_offset(bmap
, type2
);
12312 isl_int_set_si(bmap
->ineq
[i
][pos1
], 1);
12313 isl_int_set_si(bmap
->ineq
[i
][pos2
], -1);
12314 isl_int_set_si(bmap
->ineq
[i
][0], -1);
12315 bmap
= isl_basic_map_finalize(bmap
);
12319 isl_space_free(space
);
12320 isl_basic_map_free(bmap
);
12324 /* Add a constraint imposing that the value of the first dimension is
12325 * greater than that of the second.
12327 __isl_give isl_basic_map
*isl_basic_map_order_gt(__isl_take isl_basic_map
*bmap
,
12328 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12332 gt
= greator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
12334 bmap
= isl_basic_map_intersect(bmap
, gt
);
12339 /* Add a constraint imposing that the value of the first dimension is
12340 * greater than that of the second.
12342 __isl_give isl_map
*isl_map_order_gt(__isl_take isl_map
*map
,
12343 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12345 isl_basic_map
*bmap
;
12347 bmap
= greator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
12349 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
12354 /* Add a constraint imposing that the value of the first dimension is
12355 * smaller than that of the second.
12357 __isl_give isl_map
*isl_map_order_lt(__isl_take isl_map
*map
,
12358 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12360 return isl_map_order_gt(map
, type2
, pos2
, type1
, pos1
);
12363 __isl_give isl_aff
*isl_basic_map_get_div(__isl_keep isl_basic_map
*bmap
,
12367 isl_local_space
*ls
;
12372 if (!isl_basic_map_divs_known(bmap
))
12373 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12374 "some divs are unknown", return NULL
);
12376 ls
= isl_basic_map_get_local_space(bmap
);
12377 div
= isl_local_space_get_div(ls
, pos
);
12378 isl_local_space_free(ls
);
12383 __isl_give isl_aff
*isl_basic_set_get_div(__isl_keep isl_basic_set
*bset
,
12386 return isl_basic_map_get_div(bset
, pos
);
12389 /* Plug in "subs" for dimension "type", "pos" of "bset".
12391 * Let i be the dimension to replace and let "subs" be of the form
12395 * Any integer division with a non-zero coefficient for i,
12397 * floor((a i + g)/m)
12401 * floor((a f + d g)/(m d))
12403 * Constraints of the form
12411 * We currently require that "subs" is an integral expression.
12412 * Handling rational expressions may require us to add stride constraints
12413 * as we do in isl_basic_set_preimage_multi_aff.
12415 __isl_give isl_basic_set
*isl_basic_set_substitute(
12416 __isl_take isl_basic_set
*bset
,
12417 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
12423 if (bset
&& isl_basic_set_plain_is_empty(bset
))
12426 bset
= isl_basic_set_cow(bset
);
12427 if (!bset
|| !subs
)
12430 ctx
= isl_basic_set_get_ctx(bset
);
12431 if (!isl_space_is_equal(bset
->dim
, subs
->ls
->dim
))
12432 isl_die(ctx
, isl_error_invalid
,
12433 "spaces don't match", goto error
);
12434 if (isl_local_space_dim(subs
->ls
, isl_dim_div
) != 0)
12435 isl_die(ctx
, isl_error_unsupported
,
12436 "cannot handle divs yet", goto error
);
12437 if (!isl_int_is_one(subs
->v
->el
[0]))
12438 isl_die(ctx
, isl_error_invalid
,
12439 "can only substitute integer expressions", goto error
);
12441 pos
+= isl_basic_set_offset(bset
, type
);
12445 for (i
= 0; i
< bset
->n_eq
; ++i
) {
12446 if (isl_int_is_zero(bset
->eq
[i
][pos
]))
12448 isl_int_set(v
, bset
->eq
[i
][pos
]);
12449 isl_int_set_si(bset
->eq
[i
][pos
], 0);
12450 isl_seq_combine(bset
->eq
[i
], subs
->v
->el
[0], bset
->eq
[i
],
12451 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12454 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
12455 if (isl_int_is_zero(bset
->ineq
[i
][pos
]))
12457 isl_int_set(v
, bset
->ineq
[i
][pos
]);
12458 isl_int_set_si(bset
->ineq
[i
][pos
], 0);
12459 isl_seq_combine(bset
->ineq
[i
], subs
->v
->el
[0], bset
->ineq
[i
],
12460 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12463 for (i
= 0; i
< bset
->n_div
; ++i
) {
12464 if (isl_int_is_zero(bset
->div
[i
][1 + pos
]))
12466 isl_int_set(v
, bset
->div
[i
][1 + pos
]);
12467 isl_int_set_si(bset
->div
[i
][1 + pos
], 0);
12468 isl_seq_combine(bset
->div
[i
] + 1,
12469 subs
->v
->el
[0], bset
->div
[i
] + 1,
12470 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12471 isl_int_mul(bset
->div
[i
][0], bset
->div
[i
][0], subs
->v
->el
[0]);
12476 bset
= isl_basic_set_simplify(bset
);
12477 return isl_basic_set_finalize(bset
);
12479 isl_basic_set_free(bset
);
12483 /* Plug in "subs" for dimension "type", "pos" of "set".
12485 __isl_give isl_set
*isl_set_substitute(__isl_take isl_set
*set
,
12486 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
12490 if (set
&& isl_set_plain_is_empty(set
))
12493 set
= isl_set_cow(set
);
12497 for (i
= set
->n
- 1; i
>= 0; --i
) {
12498 set
->p
[i
] = isl_basic_set_substitute(set
->p
[i
], type
, pos
, subs
);
12499 if (remove_if_empty(set
, i
) < 0)
12509 /* Check if the range of "ma" is compatible with the domain or range
12510 * (depending on "type") of "bmap".
12511 * Return -1 if anything is wrong.
12513 static int check_basic_map_compatible_range_multi_aff(
12514 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type type
,
12515 __isl_keep isl_multi_aff
*ma
)
12518 isl_space
*ma_space
;
12520 ma_space
= isl_multi_aff_get_space(ma
);
12522 m
= isl_space_match(bmap
->dim
, isl_dim_param
, ma_space
, isl_dim_param
);
12526 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12527 "parameters don't match", goto error
);
12528 m
= isl_space_tuple_is_equal(bmap
->dim
, type
, ma_space
, isl_dim_out
);
12532 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12533 "spaces don't match", goto error
);
12535 isl_space_free(ma_space
);
12538 isl_space_free(ma_space
);
12542 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12543 * coefficients before the transformed range of dimensions,
12544 * the "n_after" coefficients after the transformed range of dimensions
12545 * and the coefficients of the other divs in "bmap".
12547 static int set_ma_divs(__isl_keep isl_basic_map
*bmap
,
12548 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
, int n_div
)
12553 isl_local_space
*ls
;
12558 ls
= isl_aff_get_domain_local_space(ma
->p
[0]);
12562 n_param
= isl_local_space_dim(ls
, isl_dim_param
);
12563 n_set
= isl_local_space_dim(ls
, isl_dim_set
);
12564 for (i
= 0; i
< n_div
; ++i
) {
12565 int o_bmap
= 0, o_ls
= 0;
12567 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], 1 + 1 + n_param
);
12568 o_bmap
+= 1 + 1 + n_param
;
12569 o_ls
+= 1 + 1 + n_param
;
12570 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_before
);
12571 o_bmap
+= n_before
;
12572 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
12573 ls
->div
->row
[i
] + o_ls
, n_set
);
12576 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_after
);
12578 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
12579 ls
->div
->row
[i
] + o_ls
, n_div
);
12582 isl_seq_clr(bmap
->div
[i
] + o_bmap
, bmap
->n_div
- n_div
);
12583 if (isl_basic_set_add_div_constraints(bmap
, i
) < 0)
12587 isl_local_space_free(ls
);
12590 isl_local_space_free(ls
);
12594 /* How many stride constraints does "ma" enforce?
12595 * That is, how many of the affine expressions have a denominator
12596 * different from one?
12598 static int multi_aff_strides(__isl_keep isl_multi_aff
*ma
)
12603 for (i
= 0; i
< ma
->n
; ++i
)
12604 if (!isl_int_is_one(ma
->p
[i
]->v
->el
[0]))
12610 /* For each affine expression in ma of the form
12612 * x_i = (f_i y + h_i)/m_i
12614 * with m_i different from one, add a constraint to "bmap"
12617 * f_i y + h_i = m_i alpha_i
12619 * with alpha_i an additional existentially quantified variable.
12621 * The input variables of "ma" correspond to a subset of the variables
12622 * of "bmap". There are "n_before" variables in "bmap" before this
12623 * subset and "n_after" variables after this subset.
12624 * The integer divisions of the affine expressions in "ma" are assumed
12625 * to have been aligned. There are "n_div_ma" of them and
12626 * they appear first in "bmap", straight after the "n_after" variables.
12628 static __isl_give isl_basic_map
*add_ma_strides(
12629 __isl_take isl_basic_map
*bmap
, __isl_keep isl_multi_aff
*ma
,
12630 int n_before
, int n_after
, int n_div_ma
)
12638 total
= isl_basic_map_total_dim(bmap
);
12639 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
12640 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
12641 for (i
= 0; i
< ma
->n
; ++i
) {
12642 int o_bmap
= 0, o_ma
= 1;
12644 if (isl_int_is_one(ma
->p
[i
]->v
->el
[0]))
12646 div
= isl_basic_map_alloc_div(bmap
);
12647 k
= isl_basic_map_alloc_equality(bmap
);
12648 if (div
< 0 || k
< 0)
12650 isl_int_set_si(bmap
->div
[div
][0], 0);
12651 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12652 ma
->p
[i
]->v
->el
+ o_ma
, 1 + n_param
);
12653 o_bmap
+= 1 + n_param
;
12654 o_ma
+= 1 + n_param
;
12655 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_before
);
12656 o_bmap
+= n_before
;
12657 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12658 ma
->p
[i
]->v
->el
+ o_ma
, n_in
);
12661 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_after
);
12663 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12664 ma
->p
[i
]->v
->el
+ o_ma
, n_div_ma
);
12665 o_bmap
+= n_div_ma
;
12667 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, 1 + total
- o_bmap
);
12668 isl_int_neg(bmap
->eq
[k
][1 + total
], ma
->p
[i
]->v
->el
[0]);
12674 isl_basic_map_free(bmap
);
12678 /* Replace the domain or range space (depending on "type) of "space" by "set".
12680 static __isl_give isl_space
*isl_space_set(__isl_take isl_space
*space
,
12681 enum isl_dim_type type
, __isl_take isl_space
*set
)
12683 if (type
== isl_dim_in
) {
12684 space
= isl_space_range(space
);
12685 space
= isl_space_map_from_domain_and_range(set
, space
);
12687 space
= isl_space_domain(space
);
12688 space
= isl_space_map_from_domain_and_range(space
, set
);
12694 /* Compute the preimage of the domain or range (depending on "type")
12695 * of "bmap" under the function represented by "ma".
12696 * In other words, plug in "ma" in the domain or range of "bmap".
12697 * The result is a basic map that lives in the same space as "bmap"
12698 * except that the domain or range has been replaced by
12699 * the domain space of "ma".
12701 * If bmap is represented by
12703 * A(p) + S u + B x + T v + C(divs) >= 0,
12705 * where u and x are input and output dimensions if type == isl_dim_out
12706 * while x and v are input and output dimensions if type == isl_dim_in,
12707 * and ma is represented by
12709 * x = D(p) + F(y) + G(divs')
12711 * then the result is
12713 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12715 * The divs in the input set are similarly adjusted.
12718 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12722 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
12723 * B_i G(divs') + c_i(divs))/n_i)
12725 * If bmap is not a rational map and if F(y) involves any denominators
12727 * x_i = (f_i y + h_i)/m_i
12729 * then additional constraints are added to ensure that we only
12730 * map back integer points. That is we enforce
12732 * f_i y + h_i = m_i alpha_i
12734 * with alpha_i an additional existentially quantified variable.
12736 * We first copy over the divs from "ma".
12737 * Then we add the modified constraints and divs from "bmap".
12738 * Finally, we add the stride constraints, if needed.
12740 __isl_give isl_basic_map
*isl_basic_map_preimage_multi_aff(
12741 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
12742 __isl_take isl_multi_aff
*ma
)
12746 isl_basic_map
*res
= NULL
;
12747 int n_before
, n_after
, n_div_bmap
, n_div_ma
;
12748 isl_int f
, c1
, c2
, g
;
12749 int rational
, strides
;
12756 ma
= isl_multi_aff_align_divs(ma
);
12759 if (check_basic_map_compatible_range_multi_aff(bmap
, type
, ma
) < 0)
12762 if (type
== isl_dim_in
) {
12764 n_after
= isl_basic_map_dim(bmap
, isl_dim_out
);
12766 n_before
= isl_basic_map_dim(bmap
, isl_dim_in
);
12769 n_div_bmap
= isl_basic_map_dim(bmap
, isl_dim_div
);
12770 n_div_ma
= ma
->n
? isl_aff_dim(ma
->p
[0], isl_dim_div
) : 0;
12772 space
= isl_multi_aff_get_domain_space(ma
);
12773 space
= isl_space_set(isl_basic_map_get_space(bmap
), type
, space
);
12774 rational
= isl_basic_map_is_rational(bmap
);
12775 strides
= rational
? 0 : multi_aff_strides(ma
);
12776 res
= isl_basic_map_alloc_space(space
, n_div_ma
+ n_div_bmap
+ strides
,
12777 bmap
->n_eq
+ strides
, bmap
->n_ineq
+ 2 * n_div_ma
);
12779 res
= isl_basic_map_set_rational(res
);
12781 for (i
= 0; i
< n_div_ma
+ n_div_bmap
; ++i
)
12782 if (isl_basic_map_alloc_div(res
) < 0)
12785 if (set_ma_divs(res
, ma
, n_before
, n_after
, n_div_ma
) < 0)
12788 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
12789 k
= isl_basic_map_alloc_equality(res
);
12792 isl_seq_preimage(res
->eq
[k
], bmap
->eq
[i
], ma
, n_before
,
12793 n_after
, n_div_ma
, n_div_bmap
, f
, c1
, c2
, g
, 0);
12796 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
12797 k
= isl_basic_map_alloc_inequality(res
);
12800 isl_seq_preimage(res
->ineq
[k
], bmap
->ineq
[i
], ma
, n_before
,
12801 n_after
, n_div_ma
, n_div_bmap
, f
, c1
, c2
, g
, 0);
12804 for (i
= 0; i
< bmap
->n_div
; ++i
) {
12805 if (isl_int_is_zero(bmap
->div
[i
][0])) {
12806 isl_int_set_si(res
->div
[n_div_ma
+ i
][0], 0);
12809 isl_seq_preimage(res
->div
[n_div_ma
+ i
], bmap
->div
[i
], ma
,
12810 n_before
, n_after
, n_div_ma
, n_div_bmap
,
12815 res
= add_ma_strides(res
, ma
, n_before
, n_after
, n_div_ma
);
12821 isl_basic_map_free(bmap
);
12822 isl_multi_aff_free(ma
);
12823 res
= isl_basic_set_simplify(res
);
12824 return isl_basic_map_finalize(res
);
12830 isl_basic_map_free(bmap
);
12831 isl_multi_aff_free(ma
);
12832 isl_basic_map_free(res
);
12836 /* Compute the preimage of "bset" under the function represented by "ma".
12837 * In other words, plug in "ma" in "bset". The result is a basic set
12838 * that lives in the domain space of "ma".
12840 __isl_give isl_basic_set
*isl_basic_set_preimage_multi_aff(
12841 __isl_take isl_basic_set
*bset
, __isl_take isl_multi_aff
*ma
)
12843 return isl_basic_map_preimage_multi_aff(bset
, isl_dim_set
, ma
);
12846 /* Compute the preimage of the domain of "bmap" under the function
12847 * represented by "ma".
12848 * In other words, plug in "ma" in the domain of "bmap".
12849 * The result is a basic map that lives in the same space as "bmap"
12850 * except that the domain has been replaced by the domain space of "ma".
12852 __isl_give isl_basic_map
*isl_basic_map_preimage_domain_multi_aff(
12853 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
12855 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_in
, ma
);
12858 /* Compute the preimage of the range of "bmap" under the function
12859 * represented by "ma".
12860 * In other words, plug in "ma" in the range of "bmap".
12861 * The result is a basic map that lives in the same space as "bmap"
12862 * except that the range has been replaced by the domain space of "ma".
12864 __isl_give isl_basic_map
*isl_basic_map_preimage_range_multi_aff(
12865 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
12867 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_out
, ma
);
12870 /* Check if the range of "ma" is compatible with the domain or range
12871 * (depending on "type") of "map".
12872 * Return -1 if anything is wrong.
12874 static int check_map_compatible_range_multi_aff(
12875 __isl_keep isl_map
*map
, enum isl_dim_type type
,
12876 __isl_keep isl_multi_aff
*ma
)
12879 isl_space
*ma_space
;
12881 ma_space
= isl_multi_aff_get_space(ma
);
12882 m
= isl_space_tuple_is_equal(map
->dim
, type
, ma_space
, isl_dim_out
);
12883 isl_space_free(ma_space
);
12885 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
12886 "spaces don't match", return -1);
12890 /* Compute the preimage of the domain or range (depending on "type")
12891 * of "map" under the function represented by "ma".
12892 * In other words, plug in "ma" in the domain or range of "map".
12893 * The result is a map that lives in the same space as "map"
12894 * except that the domain or range has been replaced by
12895 * the domain space of "ma".
12897 * The parameters are assumed to have been aligned.
12899 static __isl_give isl_map
*map_preimage_multi_aff(__isl_take isl_map
*map
,
12900 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
12905 map
= isl_map_cow(map
);
12906 ma
= isl_multi_aff_align_divs(ma
);
12909 if (check_map_compatible_range_multi_aff(map
, type
, ma
) < 0)
12912 for (i
= 0; i
< map
->n
; ++i
) {
12913 map
->p
[i
] = isl_basic_map_preimage_multi_aff(map
->p
[i
], type
,
12914 isl_multi_aff_copy(ma
));
12919 space
= isl_multi_aff_get_domain_space(ma
);
12920 space
= isl_space_set(isl_map_get_space(map
), type
, space
);
12922 isl_space_free(map
->dim
);
12927 isl_multi_aff_free(ma
);
12929 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
12930 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
12933 isl_multi_aff_free(ma
);
12938 /* Compute the preimage of the domain or range (depending on "type")
12939 * of "map" under the function represented by "ma".
12940 * In other words, plug in "ma" in the domain or range of "map".
12941 * The result is a map that lives in the same space as "map"
12942 * except that the domain or range has been replaced by
12943 * the domain space of "ma".
12945 __isl_give isl_map
*isl_map_preimage_multi_aff(__isl_take isl_map
*map
,
12946 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
12951 if (isl_space_match(map
->dim
, isl_dim_param
, ma
->space
, isl_dim_param
))
12952 return map_preimage_multi_aff(map
, type
, ma
);
12954 if (!isl_space_has_named_params(map
->dim
) ||
12955 !isl_space_has_named_params(ma
->space
))
12956 isl_die(map
->ctx
, isl_error_invalid
,
12957 "unaligned unnamed parameters", goto error
);
12958 map
= isl_map_align_params(map
, isl_multi_aff_get_space(ma
));
12959 ma
= isl_multi_aff_align_params(ma
, isl_map_get_space(map
));
12961 return map_preimage_multi_aff(map
, type
, ma
);
12963 isl_multi_aff_free(ma
);
12964 return isl_map_free(map
);
12967 /* Compute the preimage of "set" under the function represented by "ma".
12968 * In other words, plug in "ma" in "set". The result is a set
12969 * that lives in the domain space of "ma".
12971 __isl_give isl_set
*isl_set_preimage_multi_aff(__isl_take isl_set
*set
,
12972 __isl_take isl_multi_aff
*ma
)
12974 return isl_map_preimage_multi_aff(set
, isl_dim_set
, ma
);
12977 /* Compute the preimage of the domain of "map" under the function
12978 * represented by "ma".
12979 * In other words, plug in "ma" in the domain of "map".
12980 * The result is a map that lives in the same space as "map"
12981 * except that the domain has been replaced by the domain space of "ma".
12983 __isl_give isl_map
*isl_map_preimage_domain_multi_aff(__isl_take isl_map
*map
,
12984 __isl_take isl_multi_aff
*ma
)
12986 return isl_map_preimage_multi_aff(map
, isl_dim_in
, ma
);
12989 /* Compute the preimage of the range of "map" under the function
12990 * represented by "ma".
12991 * In other words, plug in "ma" in the range of "map".
12992 * The result is a map that lives in the same space as "map"
12993 * except that the range has been replaced by the domain space of "ma".
12995 __isl_give isl_map
*isl_map_preimage_range_multi_aff(__isl_take isl_map
*map
,
12996 __isl_take isl_multi_aff
*ma
)
12998 return isl_map_preimage_multi_aff(map
, isl_dim_out
, ma
);
13001 /* Compute the preimage of "map" under the function represented by "pma".
13002 * In other words, plug in "pma" in the domain or range of "map".
13003 * The result is a map that lives in the same space as "map",
13004 * except that the space of type "type" has been replaced by
13005 * the domain space of "pma".
13007 * The parameters of "map" and "pma" are assumed to have been aligned.
13009 static __isl_give isl_map
*isl_map_preimage_pw_multi_aff_aligned(
13010 __isl_take isl_map
*map
, enum isl_dim_type type
,
13011 __isl_take isl_pw_multi_aff
*pma
)
13020 isl_pw_multi_aff_free(pma
);
13021 res
= isl_map_empty(isl_map_get_space(map
));
13026 res
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
13027 isl_multi_aff_copy(pma
->p
[0].maff
));
13028 if (type
== isl_dim_in
)
13029 res
= isl_map_intersect_domain(res
,
13030 isl_map_copy(pma
->p
[0].set
));
13032 res
= isl_map_intersect_range(res
,
13033 isl_map_copy(pma
->p
[0].set
));
13035 for (i
= 1; i
< pma
->n
; ++i
) {
13038 res_i
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
13039 isl_multi_aff_copy(pma
->p
[i
].maff
));
13040 if (type
== isl_dim_in
)
13041 res_i
= isl_map_intersect_domain(res_i
,
13042 isl_map_copy(pma
->p
[i
].set
));
13044 res_i
= isl_map_intersect_range(res_i
,
13045 isl_map_copy(pma
->p
[i
].set
));
13046 res
= isl_map_union(res
, res_i
);
13049 isl_pw_multi_aff_free(pma
);
13053 isl_pw_multi_aff_free(pma
);
13058 /* Compute the preimage of "map" under the function represented by "pma".
13059 * In other words, plug in "pma" in the domain or range of "map".
13060 * The result is a map that lives in the same space as "map",
13061 * except that the space of type "type" has been replaced by
13062 * the domain space of "pma".
13064 __isl_give isl_map
*isl_map_preimage_pw_multi_aff(__isl_take isl_map
*map
,
13065 enum isl_dim_type type
, __isl_take isl_pw_multi_aff
*pma
)
13070 if (isl_space_match(map
->dim
, isl_dim_param
, pma
->dim
, isl_dim_param
))
13071 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
13073 if (!isl_space_has_named_params(map
->dim
) ||
13074 !isl_space_has_named_params(pma
->dim
))
13075 isl_die(map
->ctx
, isl_error_invalid
,
13076 "unaligned unnamed parameters", goto error
);
13077 map
= isl_map_align_params(map
, isl_pw_multi_aff_get_space(pma
));
13078 pma
= isl_pw_multi_aff_align_params(pma
, isl_map_get_space(map
));
13080 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
13082 isl_pw_multi_aff_free(pma
);
13083 return isl_map_free(map
);
13086 /* Compute the preimage of "set" under the function represented by "pma".
13087 * In other words, plug in "pma" in "set". The result is a set
13088 * that lives in the domain space of "pma".
13090 __isl_give isl_set
*isl_set_preimage_pw_multi_aff(__isl_take isl_set
*set
,
13091 __isl_take isl_pw_multi_aff
*pma
)
13093 return isl_map_preimage_pw_multi_aff(set
, isl_dim_set
, pma
);
13096 /* Compute the preimage of the domain of "map" under the function
13097 * represented by "pma".
13098 * In other words, plug in "pma" in the domain of "map".
13099 * The result is a map that lives in the same space as "map",
13100 * except that domain space has been replaced by the domain space of "pma".
13102 __isl_give isl_map
*isl_map_preimage_domain_pw_multi_aff(
13103 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
13105 return isl_map_preimage_pw_multi_aff(map
, isl_dim_in
, pma
);
13108 /* Compute the preimage of the range of "map" under the function
13109 * represented by "pma".
13110 * In other words, plug in "pma" in the range of "map".
13111 * The result is a map that lives in the same space as "map",
13112 * except that range space has been replaced by the domain space of "pma".
13114 __isl_give isl_map
*isl_map_preimage_range_pw_multi_aff(
13115 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
13117 return isl_map_preimage_pw_multi_aff(map
, isl_dim_out
, pma
);
13120 /* Compute the preimage of "map" under the function represented by "mpa".
13121 * In other words, plug in "mpa" in the domain or range of "map".
13122 * The result is a map that lives in the same space as "map",
13123 * except that the space of type "type" has been replaced by
13124 * the domain space of "mpa".
13126 * If the map does not involve any constraints that refer to the
13127 * dimensions of the substituted space, then the only possible
13128 * effect of "mpa" on the map is to map the space to a different space.
13129 * We create a separate isl_multi_aff to effectuate this change
13130 * in order to avoid spurious splitting of the map along the pieces
13133 __isl_give isl_map
*isl_map_preimage_multi_pw_aff(__isl_take isl_map
*map
,
13134 enum isl_dim_type type
, __isl_take isl_multi_pw_aff
*mpa
)
13137 isl_pw_multi_aff
*pma
;
13142 n
= isl_map_dim(map
, type
);
13143 if (!isl_map_involves_dims(map
, type
, 0, n
)) {
13147 space
= isl_multi_pw_aff_get_space(mpa
);
13148 isl_multi_pw_aff_free(mpa
);
13149 ma
= isl_multi_aff_zero(space
);
13150 return isl_map_preimage_multi_aff(map
, type
, ma
);
13153 pma
= isl_pw_multi_aff_from_multi_pw_aff(mpa
);
13154 return isl_map_preimage_pw_multi_aff(map
, type
, pma
);
13157 isl_multi_pw_aff_free(mpa
);
13161 /* Compute the preimage of "map" under the function represented by "mpa".
13162 * In other words, plug in "mpa" in the domain "map".
13163 * The result is a map that lives in the same space as "map",
13164 * except that domain space has been replaced by the domain space of "mpa".
13166 __isl_give isl_map
*isl_map_preimage_domain_multi_pw_aff(
13167 __isl_take isl_map
*map
, __isl_take isl_multi_pw_aff
*mpa
)
13169 return isl_map_preimage_multi_pw_aff(map
, isl_dim_in
, mpa
);
13172 /* Compute the preimage of "set" by the function represented by "mpa".
13173 * In other words, plug in "mpa" in "set".
13175 __isl_give isl_set
*isl_set_preimage_multi_pw_aff(__isl_take isl_set
*set
,
13176 __isl_take isl_multi_pw_aff
*mpa
)
13178 return isl_map_preimage_multi_pw_aff(set
, isl_dim_set
, mpa
);
13181 /* Is the point "inner" internal to inequality constraint "ineq"
13183 * The point is considered to be internal to the inequality constraint,
13184 * if it strictly lies on the positive side of the inequality constraint,
13185 * or if it lies on the constraint and the constraint is lexico-positive.
13187 static isl_bool
is_internal(__isl_keep isl_vec
*inner
,
13188 __isl_keep isl_basic_set
*bset
, int ineq
)
13194 if (!inner
|| !bset
)
13195 return isl_bool_error
;
13197 ctx
= isl_basic_set_get_ctx(bset
);
13198 isl_seq_inner_product(inner
->el
, bset
->ineq
[ineq
], inner
->size
,
13199 &ctx
->normalize_gcd
);
13200 if (!isl_int_is_zero(ctx
->normalize_gcd
))
13201 return isl_int_is_nonneg(ctx
->normalize_gcd
);
13203 total
= isl_basic_set_dim(bset
, isl_dim_all
);
13204 pos
= isl_seq_first_non_zero(bset
->ineq
[ineq
] + 1, total
);
13205 return isl_int_is_pos(bset
->ineq
[ineq
][1 + pos
]);
13208 /* Tighten the inequality constraints of "bset" that are outward with respect
13209 * to the point "vec".
13210 * That is, tighten the constraints that are not satisfied by "vec".
13212 * "vec" is a point internal to some superset S of "bset" that is used
13213 * to make the subsets of S disjoint, by tightening one half of the constraints
13214 * that separate two subsets. In particular, the constraints of S
13215 * are all satisfied by "vec" and should not be tightened.
13216 * Of the internal constraints, those that have "vec" on the outside
13217 * are tightened. The shared facet is included in the adjacent subset
13218 * with the opposite constraint.
13219 * For constraints that saturate "vec", this criterion cannot be used
13220 * to determine which of the two sides should be tightened.
13221 * Instead, the sign of the first non-zero coefficient is used
13222 * to make this choice. Note that this second criterion is never used
13223 * on the constraints of S since "vec" is interior to "S".
13225 __isl_give isl_basic_set
*isl_basic_set_tighten_outward(
13226 __isl_take isl_basic_set
*bset
, __isl_keep isl_vec
*vec
)
13230 bset
= isl_basic_set_cow(bset
);
13233 for (j
= 0; j
< bset
->n_ineq
; ++j
) {
13236 internal
= is_internal(vec
, bset
, j
);
13238 return isl_basic_set_free(bset
);
13241 isl_int_sub_ui(bset
->ineq
[j
][0], bset
->ineq
[j
][0], 1);