2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012-2014 Ecole Normale Superieure
5 * Copyright 2014 INRIA Rocquencourt
6 * Copyright 2016 INRIA Paris
7 * Copyright 2016 Sven Verdoolaege
9 * Use of this software is governed by the MIT license
11 * Written by Sven Verdoolaege, K.U.Leuven, Departement
12 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
13 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
14 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
15 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
16 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
17 * B.P. 105 - 78153 Le Chesnay, France
18 * and Centre de Recherche Inria de Paris, 2 rue Simone Iff - Voie DQ12,
19 * CS 42112, 75589 Paris Cedex 12, France
23 #include <isl_ctx_private.h>
24 #include <isl_map_private.h>
26 #include <isl/constraint.h>
27 #include "isl_space_private.h"
28 #include "isl_equalities.h"
29 #include <isl_lp_private.h>
33 #include <isl_reordering.h>
34 #include "isl_sample.h"
38 #include <isl_mat_private.h>
39 #include <isl_vec_private.h>
40 #include <isl_dim_map.h>
41 #include <isl_local_space_private.h>
42 #include <isl_aff_private.h>
43 #include <isl_options_private.h>
44 #include <isl_morph.h>
45 #include <isl_val_private.h>
46 #include <isl/deprecated/map_int.h>
47 #include <isl/deprecated/set_int.h>
49 #include <bset_to_bmap.c>
50 #include <bset_from_bmap.c>
51 #include <set_to_map.c>
52 #include <set_from_map.c>
54 static unsigned n(__isl_keep isl_space
*dim
, enum isl_dim_type type
)
57 case isl_dim_param
: return dim
->nparam
;
58 case isl_dim_in
: return dim
->n_in
;
59 case isl_dim_out
: return dim
->n_out
;
60 case isl_dim_all
: return dim
->nparam
+ dim
->n_in
+ dim
->n_out
;
65 static unsigned pos(__isl_keep isl_space
*dim
, enum isl_dim_type type
)
68 case isl_dim_param
: return 1;
69 case isl_dim_in
: return 1 + dim
->nparam
;
70 case isl_dim_out
: return 1 + dim
->nparam
+ dim
->n_in
;
75 unsigned isl_basic_map_dim(__isl_keep isl_basic_map
*bmap
,
76 enum isl_dim_type type
)
81 case isl_dim_cst
: return 1;
84 case isl_dim_out
: return isl_space_dim(bmap
->dim
, type
);
85 case isl_dim_div
: return bmap
->n_div
;
86 case isl_dim_all
: return isl_basic_map_total_dim(bmap
);
91 unsigned isl_map_dim(__isl_keep isl_map
*map
, enum isl_dim_type type
)
93 return map
? n(map
->dim
, type
) : 0;
96 unsigned isl_set_dim(__isl_keep isl_set
*set
, enum isl_dim_type type
)
98 return set
? n(set
->dim
, type
) : 0;
101 unsigned isl_basic_map_offset(struct isl_basic_map
*bmap
,
102 enum isl_dim_type type
)
111 case isl_dim_cst
: return 0;
112 case isl_dim_param
: return 1;
113 case isl_dim_in
: return 1 + space
->nparam
;
114 case isl_dim_out
: return 1 + space
->nparam
+ space
->n_in
;
115 case isl_dim_div
: return 1 + space
->nparam
+ space
->n_in
+
121 unsigned isl_basic_set_offset(struct isl_basic_set
*bset
,
122 enum isl_dim_type type
)
124 return isl_basic_map_offset(bset
, type
);
127 static unsigned map_offset(struct isl_map
*map
, enum isl_dim_type type
)
129 return pos(map
->dim
, type
);
132 unsigned isl_basic_set_dim(__isl_keep isl_basic_set
*bset
,
133 enum isl_dim_type type
)
135 return isl_basic_map_dim(bset
, type
);
138 unsigned isl_basic_set_n_dim(__isl_keep isl_basic_set
*bset
)
140 return isl_basic_set_dim(bset
, isl_dim_set
);
143 unsigned isl_basic_set_n_param(__isl_keep isl_basic_set
*bset
)
145 return isl_basic_set_dim(bset
, isl_dim_param
);
148 unsigned isl_basic_set_total_dim(const struct isl_basic_set
*bset
)
152 return isl_space_dim(bset
->dim
, isl_dim_all
) + bset
->n_div
;
155 unsigned isl_set_n_dim(__isl_keep isl_set
*set
)
157 return isl_set_dim(set
, isl_dim_set
);
160 unsigned isl_set_n_param(__isl_keep isl_set
*set
)
162 return isl_set_dim(set
, isl_dim_param
);
165 unsigned isl_basic_map_n_in(const struct isl_basic_map
*bmap
)
167 return bmap
? bmap
->dim
->n_in
: 0;
170 unsigned isl_basic_map_n_out(const struct isl_basic_map
*bmap
)
172 return bmap
? bmap
->dim
->n_out
: 0;
175 unsigned isl_basic_map_n_param(const struct isl_basic_map
*bmap
)
177 return bmap
? bmap
->dim
->nparam
: 0;
180 unsigned isl_basic_map_n_div(const struct isl_basic_map
*bmap
)
182 return bmap
? bmap
->n_div
: 0;
185 unsigned isl_basic_map_total_dim(const struct isl_basic_map
*bmap
)
187 return bmap
? isl_space_dim(bmap
->dim
, isl_dim_all
) + bmap
->n_div
: 0;
190 unsigned isl_map_n_in(const struct isl_map
*map
)
192 return map
? map
->dim
->n_in
: 0;
195 unsigned isl_map_n_out(const struct isl_map
*map
)
197 return map
? map
->dim
->n_out
: 0;
200 unsigned isl_map_n_param(const struct isl_map
*map
)
202 return map
? map
->dim
->nparam
: 0;
205 isl_bool
isl_map_compatible_domain(__isl_keep isl_map
*map
,
206 __isl_keep isl_set
*set
)
210 return isl_bool_error
;
211 m
= isl_space_match(map
->dim
, isl_dim_param
, set
->dim
, isl_dim_param
);
214 return isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
215 set
->dim
, isl_dim_set
);
218 isl_bool
isl_basic_map_compatible_domain(__isl_keep isl_basic_map
*bmap
,
219 __isl_keep isl_basic_set
*bset
)
223 return isl_bool_error
;
224 m
= isl_space_match(bmap
->dim
, isl_dim_param
, bset
->dim
, isl_dim_param
);
227 return isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
228 bset
->dim
, isl_dim_set
);
231 isl_bool
isl_map_compatible_range(__isl_keep isl_map
*map
,
232 __isl_keep isl_set
*set
)
236 return isl_bool_error
;
237 m
= isl_space_match(map
->dim
, isl_dim_param
, set
->dim
, isl_dim_param
);
240 return isl_space_tuple_is_equal(map
->dim
, isl_dim_out
,
241 set
->dim
, isl_dim_set
);
244 isl_bool
isl_basic_map_compatible_range(__isl_keep isl_basic_map
*bmap
,
245 __isl_keep isl_basic_set
*bset
)
249 return isl_bool_error
;
250 m
= isl_space_match(bmap
->dim
, isl_dim_param
, bset
->dim
, isl_dim_param
);
253 return isl_space_tuple_is_equal(bmap
->dim
, isl_dim_out
,
254 bset
->dim
, isl_dim_set
);
257 isl_ctx
*isl_basic_map_get_ctx(__isl_keep isl_basic_map
*bmap
)
259 return bmap
? bmap
->ctx
: NULL
;
262 isl_ctx
*isl_basic_set_get_ctx(__isl_keep isl_basic_set
*bset
)
264 return bset
? bset
->ctx
: NULL
;
267 isl_ctx
*isl_map_get_ctx(__isl_keep isl_map
*map
)
269 return map
? map
->ctx
: NULL
;
272 isl_ctx
*isl_set_get_ctx(__isl_keep isl_set
*set
)
274 return set
? set
->ctx
: NULL
;
277 __isl_give isl_space
*isl_basic_map_get_space(__isl_keep isl_basic_map
*bmap
)
281 return isl_space_copy(bmap
->dim
);
284 __isl_give isl_space
*isl_basic_set_get_space(__isl_keep isl_basic_set
*bset
)
288 return isl_space_copy(bset
->dim
);
291 /* Extract the divs in "bmap" as a matrix.
293 __isl_give isl_mat
*isl_basic_map_get_divs(__isl_keep isl_basic_map
*bmap
)
304 ctx
= isl_basic_map_get_ctx(bmap
);
305 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
306 cols
= 1 + 1 + total
+ bmap
->n_div
;
307 div
= isl_mat_alloc(ctx
, bmap
->n_div
, cols
);
311 for (i
= 0; i
< bmap
->n_div
; ++i
)
312 isl_seq_cpy(div
->row
[i
], bmap
->div
[i
], cols
);
317 /* Extract the divs in "bset" as a matrix.
319 __isl_give isl_mat
*isl_basic_set_get_divs(__isl_keep isl_basic_set
*bset
)
321 return isl_basic_map_get_divs(bset
);
324 __isl_give isl_local_space
*isl_basic_map_get_local_space(
325 __isl_keep isl_basic_map
*bmap
)
332 div
= isl_basic_map_get_divs(bmap
);
333 return isl_local_space_alloc_div(isl_space_copy(bmap
->dim
), div
);
336 __isl_give isl_local_space
*isl_basic_set_get_local_space(
337 __isl_keep isl_basic_set
*bset
)
339 return isl_basic_map_get_local_space(bset
);
342 /* For each known div d = floor(f/m), add the constraints
345 * -(f-(m-1)) + m d >= 0
347 * Do not finalize the result.
349 static __isl_give isl_basic_map
*add_known_div_constraints(
350 __isl_take isl_basic_map
*bmap
)
357 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
360 bmap
= isl_basic_map_cow(bmap
);
361 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 2 * n_div
);
364 for (i
= 0; i
< n_div
; ++i
) {
365 if (isl_int_is_zero(bmap
->div
[i
][0]))
367 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
368 return isl_basic_map_free(bmap
);
374 __isl_give isl_basic_map
*isl_basic_map_from_local_space(
375 __isl_take isl_local_space
*ls
)
384 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
385 bmap
= isl_basic_map_alloc_space(isl_local_space_get_space(ls
),
386 n_div
, 0, 2 * n_div
);
388 for (i
= 0; i
< n_div
; ++i
)
389 if (isl_basic_map_alloc_div(bmap
) < 0)
392 for (i
= 0; i
< n_div
; ++i
)
393 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], ls
->div
->n_col
);
394 bmap
= add_known_div_constraints(bmap
);
396 isl_local_space_free(ls
);
399 isl_local_space_free(ls
);
400 isl_basic_map_free(bmap
);
404 __isl_give isl_basic_set
*isl_basic_set_from_local_space(
405 __isl_take isl_local_space
*ls
)
407 return isl_basic_map_from_local_space(ls
);
410 __isl_give isl_space
*isl_map_get_space(__isl_keep isl_map
*map
)
414 return isl_space_copy(map
->dim
);
417 __isl_give isl_space
*isl_set_get_space(__isl_keep isl_set
*set
)
421 return isl_space_copy(set
->dim
);
424 __isl_give isl_basic_map
*isl_basic_map_set_tuple_name(
425 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
, const char *s
)
427 bmap
= isl_basic_map_cow(bmap
);
430 bmap
->dim
= isl_space_set_tuple_name(bmap
->dim
, type
, s
);
433 bmap
= isl_basic_map_finalize(bmap
);
436 isl_basic_map_free(bmap
);
440 __isl_give isl_basic_set
*isl_basic_set_set_tuple_name(
441 __isl_take isl_basic_set
*bset
, const char *s
)
443 return isl_basic_map_set_tuple_name(bset
, isl_dim_set
, s
);
446 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map
*bmap
,
447 enum isl_dim_type type
)
449 return bmap
? isl_space_get_tuple_name(bmap
->dim
, type
) : NULL
;
452 __isl_give isl_map
*isl_map_set_tuple_name(__isl_take isl_map
*map
,
453 enum isl_dim_type type
, const char *s
)
457 map
= isl_map_cow(map
);
461 map
->dim
= isl_space_set_tuple_name(map
->dim
, type
, s
);
465 for (i
= 0; i
< map
->n
; ++i
) {
466 map
->p
[i
] = isl_basic_map_set_tuple_name(map
->p
[i
], type
, s
);
477 /* Replace the identifier of the tuple of type "type" by "id".
479 __isl_give isl_basic_map
*isl_basic_map_set_tuple_id(
480 __isl_take isl_basic_map
*bmap
,
481 enum isl_dim_type type
, __isl_take isl_id
*id
)
483 bmap
= isl_basic_map_cow(bmap
);
486 bmap
->dim
= isl_space_set_tuple_id(bmap
->dim
, type
, id
);
488 return isl_basic_map_free(bmap
);
489 bmap
= isl_basic_map_finalize(bmap
);
496 /* Replace the identifier of the tuple by "id".
498 __isl_give isl_basic_set
*isl_basic_set_set_tuple_id(
499 __isl_take isl_basic_set
*bset
, __isl_take isl_id
*id
)
501 return isl_basic_map_set_tuple_id(bset
, isl_dim_set
, id
);
504 /* Does the input or output tuple have a name?
506 isl_bool
isl_map_has_tuple_name(__isl_keep isl_map
*map
, enum isl_dim_type type
)
508 return map
? isl_space_has_tuple_name(map
->dim
, type
) : isl_bool_error
;
511 const char *isl_map_get_tuple_name(__isl_keep isl_map
*map
,
512 enum isl_dim_type type
)
514 return map
? isl_space_get_tuple_name(map
->dim
, type
) : NULL
;
517 __isl_give isl_set
*isl_set_set_tuple_name(__isl_take isl_set
*set
,
520 return set_from_map(isl_map_set_tuple_name(set_to_map(set
),
524 __isl_give isl_map
*isl_map_set_tuple_id(__isl_take isl_map
*map
,
525 enum isl_dim_type type
, __isl_take isl_id
*id
)
527 map
= isl_map_cow(map
);
531 map
->dim
= isl_space_set_tuple_id(map
->dim
, type
, id
);
533 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
539 __isl_give isl_set
*isl_set_set_tuple_id(__isl_take isl_set
*set
,
540 __isl_take isl_id
*id
)
542 return isl_map_set_tuple_id(set
, isl_dim_set
, id
);
545 __isl_give isl_map
*isl_map_reset_tuple_id(__isl_take isl_map
*map
,
546 enum isl_dim_type type
)
548 map
= isl_map_cow(map
);
552 map
->dim
= isl_space_reset_tuple_id(map
->dim
, type
);
554 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
557 __isl_give isl_set
*isl_set_reset_tuple_id(__isl_take isl_set
*set
)
559 return isl_map_reset_tuple_id(set
, isl_dim_set
);
562 isl_bool
isl_map_has_tuple_id(__isl_keep isl_map
*map
, enum isl_dim_type type
)
564 return map
? isl_space_has_tuple_id(map
->dim
, type
) : isl_bool_error
;
567 __isl_give isl_id
*isl_map_get_tuple_id(__isl_keep isl_map
*map
,
568 enum isl_dim_type type
)
570 return map
? isl_space_get_tuple_id(map
->dim
, type
) : NULL
;
573 isl_bool
isl_set_has_tuple_id(__isl_keep isl_set
*set
)
575 return isl_map_has_tuple_id(set
, isl_dim_set
);
578 __isl_give isl_id
*isl_set_get_tuple_id(__isl_keep isl_set
*set
)
580 return isl_map_get_tuple_id(set
, isl_dim_set
);
583 /* Does the set tuple have a name?
585 isl_bool
isl_set_has_tuple_name(__isl_keep isl_set
*set
)
588 return isl_bool_error
;
589 return isl_space_has_tuple_name(set
->dim
, isl_dim_set
);
593 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set
*bset
)
595 return bset
? isl_space_get_tuple_name(bset
->dim
, isl_dim_set
) : NULL
;
598 const char *isl_set_get_tuple_name(__isl_keep isl_set
*set
)
600 return set
? isl_space_get_tuple_name(set
->dim
, isl_dim_set
) : NULL
;
603 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map
*bmap
,
604 enum isl_dim_type type
, unsigned pos
)
606 return bmap
? isl_space_get_dim_name(bmap
->dim
, type
, pos
) : NULL
;
609 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set
*bset
,
610 enum isl_dim_type type
, unsigned pos
)
612 return bset
? isl_space_get_dim_name(bset
->dim
, type
, pos
) : NULL
;
615 /* Does the given dimension have a name?
617 isl_bool
isl_map_has_dim_name(__isl_keep isl_map
*map
,
618 enum isl_dim_type type
, unsigned pos
)
621 return isl_bool_error
;
622 return isl_space_has_dim_name(map
->dim
, type
, pos
);
625 const char *isl_map_get_dim_name(__isl_keep isl_map
*map
,
626 enum isl_dim_type type
, unsigned pos
)
628 return map
? isl_space_get_dim_name(map
->dim
, type
, pos
) : NULL
;
631 const char *isl_set_get_dim_name(__isl_keep isl_set
*set
,
632 enum isl_dim_type type
, unsigned pos
)
634 return set
? isl_space_get_dim_name(set
->dim
, type
, pos
) : NULL
;
637 /* Does the given dimension have a name?
639 isl_bool
isl_set_has_dim_name(__isl_keep isl_set
*set
,
640 enum isl_dim_type type
, unsigned pos
)
643 return isl_bool_error
;
644 return isl_space_has_dim_name(set
->dim
, type
, pos
);
647 __isl_give isl_basic_map
*isl_basic_map_set_dim_name(
648 __isl_take isl_basic_map
*bmap
,
649 enum isl_dim_type type
, unsigned pos
, const char *s
)
651 bmap
= isl_basic_map_cow(bmap
);
654 bmap
->dim
= isl_space_set_dim_name(bmap
->dim
, type
, pos
, s
);
657 return isl_basic_map_finalize(bmap
);
659 isl_basic_map_free(bmap
);
663 __isl_give isl_map
*isl_map_set_dim_name(__isl_take isl_map
*map
,
664 enum isl_dim_type type
, unsigned pos
, const char *s
)
668 map
= isl_map_cow(map
);
672 map
->dim
= isl_space_set_dim_name(map
->dim
, type
, pos
, s
);
676 for (i
= 0; i
< map
->n
; ++i
) {
677 map
->p
[i
] = isl_basic_map_set_dim_name(map
->p
[i
], type
, pos
, s
);
688 __isl_give isl_basic_set
*isl_basic_set_set_dim_name(
689 __isl_take isl_basic_set
*bset
,
690 enum isl_dim_type type
, unsigned pos
, const char *s
)
692 return bset_from_bmap(isl_basic_map_set_dim_name(bset_to_bmap(bset
),
696 __isl_give isl_set
*isl_set_set_dim_name(__isl_take isl_set
*set
,
697 enum isl_dim_type type
, unsigned pos
, const char *s
)
699 return set_from_map(isl_map_set_dim_name(set_to_map(set
),
703 isl_bool
isl_basic_map_has_dim_id(__isl_keep isl_basic_map
*bmap
,
704 enum isl_dim_type type
, unsigned pos
)
707 return isl_bool_error
;
708 return isl_space_has_dim_id(bmap
->dim
, type
, pos
);
711 __isl_give isl_id
*isl_basic_set_get_dim_id(__isl_keep isl_basic_set
*bset
,
712 enum isl_dim_type type
, unsigned pos
)
714 return bset
? isl_space_get_dim_id(bset
->dim
, type
, pos
) : NULL
;
717 isl_bool
isl_map_has_dim_id(__isl_keep isl_map
*map
,
718 enum isl_dim_type type
, unsigned pos
)
720 return map
? isl_space_has_dim_id(map
->dim
, type
, pos
) : isl_bool_error
;
723 __isl_give isl_id
*isl_map_get_dim_id(__isl_keep isl_map
*map
,
724 enum isl_dim_type type
, unsigned pos
)
726 return map
? isl_space_get_dim_id(map
->dim
, type
, pos
) : NULL
;
729 isl_bool
isl_set_has_dim_id(__isl_keep isl_set
*set
,
730 enum isl_dim_type type
, unsigned pos
)
732 return isl_map_has_dim_id(set
, type
, pos
);
735 __isl_give isl_id
*isl_set_get_dim_id(__isl_keep isl_set
*set
,
736 enum isl_dim_type type
, unsigned pos
)
738 return isl_map_get_dim_id(set
, type
, pos
);
741 __isl_give isl_map
*isl_map_set_dim_id(__isl_take isl_map
*map
,
742 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
744 map
= isl_map_cow(map
);
748 map
->dim
= isl_space_set_dim_id(map
->dim
, type
, pos
, id
);
750 return isl_map_reset_space(map
, isl_space_copy(map
->dim
));
756 __isl_give isl_set
*isl_set_set_dim_id(__isl_take isl_set
*set
,
757 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
759 return isl_map_set_dim_id(set
, type
, pos
, id
);
762 int isl_map_find_dim_by_id(__isl_keep isl_map
*map
, enum isl_dim_type type
,
763 __isl_keep isl_id
*id
)
767 return isl_space_find_dim_by_id(map
->dim
, type
, id
);
770 int isl_set_find_dim_by_id(__isl_keep isl_set
*set
, enum isl_dim_type type
,
771 __isl_keep isl_id
*id
)
773 return isl_map_find_dim_by_id(set
, type
, id
);
776 /* Return the position of the dimension of the given type and name
778 * Return -1 if no such dimension can be found.
780 int isl_basic_map_find_dim_by_name(__isl_keep isl_basic_map
*bmap
,
781 enum isl_dim_type type
, const char *name
)
785 return isl_space_find_dim_by_name(bmap
->dim
, type
, name
);
788 int isl_map_find_dim_by_name(__isl_keep isl_map
*map
, enum isl_dim_type type
,
793 return isl_space_find_dim_by_name(map
->dim
, type
, name
);
796 int isl_set_find_dim_by_name(__isl_keep isl_set
*set
, enum isl_dim_type type
,
799 return isl_map_find_dim_by_name(set
, type
, name
);
802 /* Reset the user pointer on all identifiers of parameters and tuples
803 * of the space of "map".
805 __isl_give isl_map
*isl_map_reset_user(__isl_take isl_map
*map
)
809 space
= isl_map_get_space(map
);
810 space
= isl_space_reset_user(space
);
811 map
= isl_map_reset_space(map
, space
);
816 /* Reset the user pointer on all identifiers of parameters and tuples
817 * of the space of "set".
819 __isl_give isl_set
*isl_set_reset_user(__isl_take isl_set
*set
)
821 return isl_map_reset_user(set
);
824 isl_bool
isl_basic_map_is_rational(__isl_keep isl_basic_map
*bmap
)
827 return isl_bool_error
;
828 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
831 /* Has "map" been marked as a rational map?
832 * In particular, have all basic maps in "map" been marked this way?
833 * An empty map is not considered to be rational.
834 * Maps where only some of the basic maps are marked rational
837 isl_bool
isl_map_is_rational(__isl_keep isl_map
*map
)
843 return isl_bool_error
;
845 return isl_bool_false
;
846 rational
= isl_basic_map_is_rational(map
->p
[0]);
849 for (i
= 1; i
< map
->n
; ++i
) {
852 rational_i
= isl_basic_map_is_rational(map
->p
[i
]);
855 if (rational
!= rational_i
)
856 isl_die(isl_map_get_ctx(map
), isl_error_unsupported
,
857 "mixed rational and integer basic maps "
858 "not supported", return isl_bool_error
);
864 /* Has "set" been marked as a rational set?
865 * In particular, have all basic set in "set" been marked this way?
866 * An empty set is not considered to be rational.
867 * Sets where only some of the basic sets are marked rational
870 isl_bool
isl_set_is_rational(__isl_keep isl_set
*set
)
872 return isl_map_is_rational(set
);
875 int isl_basic_set_is_rational(__isl_keep isl_basic_set
*bset
)
877 return isl_basic_map_is_rational(bset
);
880 /* Does "bmap" contain any rational points?
882 * If "bmap" has an equality for each dimension, equating the dimension
883 * to an integer constant, then it has no rational points, even if it
884 * is marked as rational.
886 isl_bool
isl_basic_map_has_rational(__isl_keep isl_basic_map
*bmap
)
888 isl_bool has_rational
= isl_bool_true
;
892 return isl_bool_error
;
893 if (isl_basic_map_plain_is_empty(bmap
))
894 return isl_bool_false
;
895 if (!isl_basic_map_is_rational(bmap
))
896 return isl_bool_false
;
897 bmap
= isl_basic_map_copy(bmap
);
898 bmap
= isl_basic_map_implicit_equalities(bmap
);
900 return isl_bool_error
;
901 total
= isl_basic_map_total_dim(bmap
);
902 if (bmap
->n_eq
== total
) {
904 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
905 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1, total
);
908 if (!isl_int_is_one(bmap
->eq
[i
][1 + j
]) &&
909 !isl_int_is_negone(bmap
->eq
[i
][1 + j
]))
911 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + j
+ 1,
917 has_rational
= isl_bool_false
;
919 isl_basic_map_free(bmap
);
924 /* Does "map" contain any rational points?
926 isl_bool
isl_map_has_rational(__isl_keep isl_map
*map
)
929 isl_bool has_rational
;
932 return isl_bool_error
;
933 for (i
= 0; i
< map
->n
; ++i
) {
934 has_rational
= isl_basic_map_has_rational(map
->p
[i
]);
935 if (has_rational
< 0 || has_rational
)
938 return isl_bool_false
;
941 /* Does "set" contain any rational points?
943 isl_bool
isl_set_has_rational(__isl_keep isl_set
*set
)
945 return isl_map_has_rational(set
);
948 /* Is this basic set a parameter domain?
950 isl_bool
isl_basic_set_is_params(__isl_keep isl_basic_set
*bset
)
953 return isl_bool_error
;
954 return isl_space_is_params(bset
->dim
);
957 /* Is this set a parameter domain?
959 isl_bool
isl_set_is_params(__isl_keep isl_set
*set
)
962 return isl_bool_error
;
963 return isl_space_is_params(set
->dim
);
966 /* Is this map actually a parameter domain?
967 * Users should never call this function. Outside of isl,
968 * a map can never be a parameter domain.
970 isl_bool
isl_map_is_params(__isl_keep isl_map
*map
)
973 return isl_bool_error
;
974 return isl_space_is_params(map
->dim
);
977 static struct isl_basic_map
*basic_map_init(struct isl_ctx
*ctx
,
978 struct isl_basic_map
*bmap
, unsigned extra
,
979 unsigned n_eq
, unsigned n_ineq
)
982 size_t row_size
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + extra
;
987 bmap
->block
= isl_blk_alloc(ctx
, (n_ineq
+ n_eq
) * row_size
);
988 if (isl_blk_is_error(bmap
->block
))
991 bmap
->ineq
= isl_alloc_array(ctx
, isl_int
*, n_ineq
+ n_eq
);
992 if ((n_ineq
+ n_eq
) && !bmap
->ineq
)
996 bmap
->block2
= isl_blk_empty();
999 bmap
->block2
= isl_blk_alloc(ctx
, extra
* (1 + row_size
));
1000 if (isl_blk_is_error(bmap
->block2
))
1003 bmap
->div
= isl_alloc_array(ctx
, isl_int
*, extra
);
1008 for (i
= 0; i
< n_ineq
+ n_eq
; ++i
)
1009 bmap
->ineq
[i
] = bmap
->block
.data
+ i
* row_size
;
1011 for (i
= 0; i
< extra
; ++i
)
1012 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + row_size
);
1016 bmap
->c_size
= n_eq
+ n_ineq
;
1017 bmap
->eq
= bmap
->ineq
+ n_ineq
;
1018 bmap
->extra
= extra
;
1022 bmap
->sample
= NULL
;
1026 isl_basic_map_free(bmap
);
1030 struct isl_basic_set
*isl_basic_set_alloc(struct isl_ctx
*ctx
,
1031 unsigned nparam
, unsigned dim
, unsigned extra
,
1032 unsigned n_eq
, unsigned n_ineq
)
1034 struct isl_basic_map
*bmap
;
1037 space
= isl_space_set_alloc(ctx
, nparam
, dim
);
1041 bmap
= isl_basic_map_alloc_space(space
, extra
, n_eq
, n_ineq
);
1042 return bset_from_bmap(bmap
);
1045 struct isl_basic_set
*isl_basic_set_alloc_space(__isl_take isl_space
*dim
,
1046 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
1048 struct isl_basic_map
*bmap
;
1051 isl_assert(dim
->ctx
, dim
->n_in
== 0, goto error
);
1052 bmap
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
1053 return bset_from_bmap(bmap
);
1055 isl_space_free(dim
);
1059 struct isl_basic_map
*isl_basic_map_alloc_space(__isl_take isl_space
*dim
,
1060 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
1062 struct isl_basic_map
*bmap
;
1066 bmap
= isl_calloc_type(dim
->ctx
, struct isl_basic_map
);
1071 return basic_map_init(dim
->ctx
, bmap
, extra
, n_eq
, n_ineq
);
1073 isl_space_free(dim
);
1077 struct isl_basic_map
*isl_basic_map_alloc(struct isl_ctx
*ctx
,
1078 unsigned nparam
, unsigned in
, unsigned out
, unsigned extra
,
1079 unsigned n_eq
, unsigned n_ineq
)
1081 struct isl_basic_map
*bmap
;
1084 dim
= isl_space_alloc(ctx
, nparam
, in
, out
);
1088 bmap
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
1092 static void dup_constraints(
1093 struct isl_basic_map
*dst
, struct isl_basic_map
*src
)
1096 unsigned total
= isl_basic_map_total_dim(src
);
1098 for (i
= 0; i
< src
->n_eq
; ++i
) {
1099 int j
= isl_basic_map_alloc_equality(dst
);
1100 isl_seq_cpy(dst
->eq
[j
], src
->eq
[i
], 1+total
);
1103 for (i
= 0; i
< src
->n_ineq
; ++i
) {
1104 int j
= isl_basic_map_alloc_inequality(dst
);
1105 isl_seq_cpy(dst
->ineq
[j
], src
->ineq
[i
], 1+total
);
1108 for (i
= 0; i
< src
->n_div
; ++i
) {
1109 int j
= isl_basic_map_alloc_div(dst
);
1110 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
);
1112 ISL_F_SET(dst
, ISL_BASIC_SET_FINAL
);
1115 struct isl_basic_map
*isl_basic_map_dup(struct isl_basic_map
*bmap
)
1117 struct isl_basic_map
*dup
;
1121 dup
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
1122 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
1125 dup_constraints(dup
, bmap
);
1126 dup
->flags
= bmap
->flags
;
1127 dup
->sample
= isl_vec_copy(bmap
->sample
);
1131 struct isl_basic_set
*isl_basic_set_dup(struct isl_basic_set
*bset
)
1133 struct isl_basic_map
*dup
;
1135 dup
= isl_basic_map_dup(bset_to_bmap(bset
));
1136 return bset_from_bmap(dup
);
1139 struct isl_basic_set
*isl_basic_set_copy(struct isl_basic_set
*bset
)
1144 if (ISL_F_ISSET(bset
, ISL_BASIC_SET_FINAL
)) {
1148 return isl_basic_set_dup(bset
);
1151 struct isl_set
*isl_set_copy(struct isl_set
*set
)
1160 struct isl_basic_map
*isl_basic_map_copy(struct isl_basic_map
*bmap
)
1165 if (ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
)) {
1169 bmap
= isl_basic_map_dup(bmap
);
1171 ISL_F_SET(bmap
, ISL_BASIC_SET_FINAL
);
1175 struct isl_map
*isl_map_copy(struct isl_map
*map
)
1184 __isl_null isl_basic_map
*isl_basic_map_free(__isl_take isl_basic_map
*bmap
)
1189 if (--bmap
->ref
> 0)
1192 isl_ctx_deref(bmap
->ctx
);
1194 isl_blk_free(bmap
->ctx
, bmap
->block2
);
1196 isl_blk_free(bmap
->ctx
, bmap
->block
);
1197 isl_vec_free(bmap
->sample
);
1198 isl_space_free(bmap
->dim
);
1204 __isl_null isl_basic_set
*isl_basic_set_free(__isl_take isl_basic_set
*bset
)
1206 return isl_basic_map_free(bset_to_bmap(bset
));
1209 static int room_for_con(struct isl_basic_map
*bmap
, unsigned n
)
1211 return bmap
->n_eq
+ bmap
->n_ineq
+ n
<= bmap
->c_size
;
1214 __isl_give isl_map
*isl_map_align_params_map_map_and(
1215 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
,
1216 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*map1
,
1217 __isl_take isl_map
*map2
))
1221 if (isl_space_match(map1
->dim
, isl_dim_param
, map2
->dim
, isl_dim_param
))
1222 return fn(map1
, map2
);
1223 if (!isl_space_has_named_params(map1
->dim
) ||
1224 !isl_space_has_named_params(map2
->dim
))
1225 isl_die(map1
->ctx
, isl_error_invalid
,
1226 "unaligned unnamed parameters", goto error
);
1227 map1
= isl_map_align_params(map1
, isl_map_get_space(map2
));
1228 map2
= isl_map_align_params(map2
, isl_map_get_space(map1
));
1229 return fn(map1
, map2
);
1236 isl_bool
isl_map_align_params_map_map_and_test(__isl_keep isl_map
*map1
,
1237 __isl_keep isl_map
*map2
,
1238 isl_bool (*fn
)(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
))
1243 return isl_bool_error
;
1244 if (isl_space_match(map1
->dim
, isl_dim_param
, map2
->dim
, isl_dim_param
))
1245 return fn(map1
, map2
);
1246 if (!isl_space_has_named_params(map1
->dim
) ||
1247 !isl_space_has_named_params(map2
->dim
))
1248 isl_die(map1
->ctx
, isl_error_invalid
,
1249 "unaligned unnamed parameters", return isl_bool_error
);
1250 map1
= isl_map_copy(map1
);
1251 map2
= isl_map_copy(map2
);
1252 map1
= isl_map_align_params(map1
, isl_map_get_space(map2
));
1253 map2
= isl_map_align_params(map2
, isl_map_get_space(map1
));
1260 int isl_basic_map_alloc_equality(struct isl_basic_map
*bmap
)
1262 struct isl_ctx
*ctx
;
1266 isl_assert(ctx
, room_for_con(bmap
, 1), return -1);
1267 isl_assert(ctx
, (bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
<= bmap
->c_size
,
1269 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1270 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1271 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1272 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1273 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1274 if ((bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
== bmap
->c_size
) {
1276 int j
= isl_basic_map_alloc_inequality(bmap
);
1280 bmap
->ineq
[j
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1281 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
1288 isl_seq_clr(bmap
->eq
[bmap
->n_eq
] + 1 + isl_basic_map_total_dim(bmap
),
1289 bmap
->extra
- bmap
->n_div
);
1290 return bmap
->n_eq
++;
1293 int isl_basic_set_alloc_equality(struct isl_basic_set
*bset
)
1295 return isl_basic_map_alloc_equality(bset_to_bmap(bset
));
1298 int isl_basic_map_free_equality(struct isl_basic_map
*bmap
, unsigned n
)
1302 isl_assert(bmap
->ctx
, n
<= bmap
->n_eq
, return -1);
1307 int isl_basic_set_free_equality(struct isl_basic_set
*bset
, unsigned n
)
1309 return isl_basic_map_free_equality(bset_to_bmap(bset
), n
);
1312 int isl_basic_map_drop_equality(struct isl_basic_map
*bmap
, unsigned pos
)
1317 isl_assert(bmap
->ctx
, pos
< bmap
->n_eq
, return -1);
1319 if (pos
!= bmap
->n_eq
- 1) {
1321 bmap
->eq
[pos
] = bmap
->eq
[bmap
->n_eq
- 1];
1322 bmap
->eq
[bmap
->n_eq
- 1] = t
;
1328 int isl_basic_set_drop_equality(struct isl_basic_set
*bset
, unsigned pos
)
1330 return isl_basic_map_drop_equality(bset_to_bmap(bset
), pos
);
1333 /* Turn inequality "pos" of "bmap" into an equality.
1335 * In particular, we move the inequality in front of the equalities
1336 * and move the last inequality in the position of the moved inequality.
1337 * Note that isl_tab_make_equalities_explicit depends on this particular
1338 * change in the ordering of the constraints.
1340 void isl_basic_map_inequality_to_equality(
1341 struct isl_basic_map
*bmap
, unsigned pos
)
1345 t
= bmap
->ineq
[pos
];
1346 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1347 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
1352 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1353 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1354 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1355 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1358 static int room_for_ineq(struct isl_basic_map
*bmap
, unsigned n
)
1360 return bmap
->n_ineq
+ n
<= bmap
->eq
- bmap
->ineq
;
1363 int isl_basic_map_alloc_inequality(struct isl_basic_map
*bmap
)
1365 struct isl_ctx
*ctx
;
1369 isl_assert(ctx
, room_for_ineq(bmap
, 1), return -1);
1370 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1371 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1372 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1373 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1374 isl_seq_clr(bmap
->ineq
[bmap
->n_ineq
] +
1375 1 + isl_basic_map_total_dim(bmap
),
1376 bmap
->extra
- bmap
->n_div
);
1377 return bmap
->n_ineq
++;
1380 int isl_basic_set_alloc_inequality(struct isl_basic_set
*bset
)
1382 return isl_basic_map_alloc_inequality(bset_to_bmap(bset
));
1385 int isl_basic_map_free_inequality(struct isl_basic_map
*bmap
, unsigned n
)
1389 isl_assert(bmap
->ctx
, n
<= bmap
->n_ineq
, return -1);
1394 int isl_basic_set_free_inequality(struct isl_basic_set
*bset
, unsigned n
)
1396 return isl_basic_map_free_inequality(bset_to_bmap(bset
), n
);
1399 int isl_basic_map_drop_inequality(struct isl_basic_map
*bmap
, unsigned pos
)
1404 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
1406 if (pos
!= bmap
->n_ineq
- 1) {
1407 t
= bmap
->ineq
[pos
];
1408 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1409 bmap
->ineq
[bmap
->n_ineq
- 1] = t
;
1410 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1416 int isl_basic_set_drop_inequality(struct isl_basic_set
*bset
, unsigned pos
)
1418 return isl_basic_map_drop_inequality(bset_to_bmap(bset
), pos
);
1421 __isl_give isl_basic_map
*isl_basic_map_add_eq(__isl_take isl_basic_map
*bmap
,
1426 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
1429 k
= isl_basic_map_alloc_equality(bmap
);
1432 isl_seq_cpy(bmap
->eq
[k
], eq
, 1 + isl_basic_map_total_dim(bmap
));
1435 isl_basic_map_free(bmap
);
1439 __isl_give isl_basic_set
*isl_basic_set_add_eq(__isl_take isl_basic_set
*bset
,
1442 return bset_from_bmap(isl_basic_map_add_eq(bset_to_bmap(bset
), eq
));
1445 __isl_give isl_basic_map
*isl_basic_map_add_ineq(__isl_take isl_basic_map
*bmap
,
1450 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
1453 k
= isl_basic_map_alloc_inequality(bmap
);
1456 isl_seq_cpy(bmap
->ineq
[k
], ineq
, 1 + isl_basic_map_total_dim(bmap
));
1459 isl_basic_map_free(bmap
);
1463 __isl_give isl_basic_set
*isl_basic_set_add_ineq(__isl_take isl_basic_set
*bset
,
1466 return bset_from_bmap(isl_basic_map_add_ineq(bset_to_bmap(bset
), ineq
));
1469 int isl_basic_map_alloc_div(struct isl_basic_map
*bmap
)
1473 isl_assert(bmap
->ctx
, bmap
->n_div
< bmap
->extra
, return -1);
1474 isl_seq_clr(bmap
->div
[bmap
->n_div
] +
1475 1 + 1 + isl_basic_map_total_dim(bmap
),
1476 bmap
->extra
- bmap
->n_div
);
1477 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1478 return bmap
->n_div
++;
1481 int isl_basic_set_alloc_div(struct isl_basic_set
*bset
)
1483 return isl_basic_map_alloc_div(bset_to_bmap(bset
));
1486 /* Check that there are "n" dimensions of type "type" starting at "first"
1489 static isl_stat
isl_basic_map_check_range(__isl_keep isl_basic_map
*bmap
,
1490 enum isl_dim_type type
, unsigned first
, unsigned n
)
1495 return isl_stat_error
;
1496 dim
= isl_basic_map_dim(bmap
, type
);
1497 if (first
+ n
> dim
|| first
+ n
< first
)
1498 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
1499 "position or range out of bounds",
1500 return isl_stat_error
);
1504 /* Insert an extra integer division, prescribed by "div", to "bmap"
1505 * at (integer division) position "pos".
1507 * The integer division is first added at the end and then moved
1508 * into the right position.
1510 __isl_give isl_basic_map
*isl_basic_map_insert_div(
1511 __isl_take isl_basic_map
*bmap
, int pos
, __isl_keep isl_vec
*div
)
1515 bmap
= isl_basic_map_cow(bmap
);
1517 return isl_basic_map_free(bmap
);
1519 if (div
->size
!= 1 + 1 + isl_basic_map_dim(bmap
, isl_dim_all
))
1520 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
1521 "unexpected size", return isl_basic_map_free(bmap
));
1522 if (isl_basic_map_check_range(bmap
, isl_dim_div
, pos
, 0) < 0)
1523 return isl_basic_map_free(bmap
);
1525 bmap
= isl_basic_map_extend_space(bmap
,
1526 isl_basic_map_get_space(bmap
), 1, 0, 2);
1527 k
= isl_basic_map_alloc_div(bmap
);
1529 return isl_basic_map_free(bmap
);
1530 isl_seq_cpy(bmap
->div
[k
], div
->el
, div
->size
);
1531 isl_int_set_si(bmap
->div
[k
][div
->size
], 0);
1533 for (i
= k
; i
> pos
; --i
)
1534 isl_basic_map_swap_div(bmap
, i
, i
- 1);
1539 isl_stat
isl_basic_map_free_div(struct isl_basic_map
*bmap
, unsigned n
)
1542 return isl_stat_error
;
1543 isl_assert(bmap
->ctx
, n
<= bmap
->n_div
, return isl_stat_error
);
1548 /* Copy constraint from src to dst, putting the vars of src at offset
1549 * dim_off in dst and the divs of src at offset div_off in dst.
1550 * If both sets are actually map, then dim_off applies to the input
1553 static void copy_constraint(struct isl_basic_map
*dst_map
, isl_int
*dst
,
1554 struct isl_basic_map
*src_map
, isl_int
*src
,
1555 unsigned in_off
, unsigned out_off
, unsigned div_off
)
1557 unsigned src_nparam
= isl_basic_map_n_param(src_map
);
1558 unsigned dst_nparam
= isl_basic_map_n_param(dst_map
);
1559 unsigned src_in
= isl_basic_map_n_in(src_map
);
1560 unsigned dst_in
= isl_basic_map_n_in(dst_map
);
1561 unsigned src_out
= isl_basic_map_n_out(src_map
);
1562 unsigned dst_out
= isl_basic_map_n_out(dst_map
);
1563 isl_int_set(dst
[0], src
[0]);
1564 isl_seq_cpy(dst
+1, src
+1, isl_min(dst_nparam
, src_nparam
));
1565 if (dst_nparam
> src_nparam
)
1566 isl_seq_clr(dst
+1+src_nparam
,
1567 dst_nparam
- src_nparam
);
1568 isl_seq_clr(dst
+1+dst_nparam
, in_off
);
1569 isl_seq_cpy(dst
+1+dst_nparam
+in_off
,
1571 isl_min(dst_in
-in_off
, src_in
));
1572 if (dst_in
-in_off
> src_in
)
1573 isl_seq_clr(dst
+1+dst_nparam
+in_off
+src_in
,
1574 dst_in
- in_off
- src_in
);
1575 isl_seq_clr(dst
+1+dst_nparam
+dst_in
, out_off
);
1576 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+out_off
,
1577 src
+1+src_nparam
+src_in
,
1578 isl_min(dst_out
-out_off
, src_out
));
1579 if (dst_out
-out_off
> src_out
)
1580 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+out_off
+src_out
,
1581 dst_out
- out_off
- src_out
);
1582 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
, div_off
);
1583 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+dst_out
+div_off
,
1584 src
+1+src_nparam
+src_in
+src_out
,
1585 isl_min(dst_map
->extra
-div_off
, src_map
->n_div
));
1586 if (dst_map
->n_div
-div_off
> src_map
->n_div
)
1587 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
+
1588 div_off
+src_map
->n_div
,
1589 dst_map
->n_div
- div_off
- src_map
->n_div
);
1592 static void copy_div(struct isl_basic_map
*dst_map
, isl_int
*dst
,
1593 struct isl_basic_map
*src_map
, isl_int
*src
,
1594 unsigned in_off
, unsigned out_off
, unsigned div_off
)
1596 isl_int_set(dst
[0], src
[0]);
1597 copy_constraint(dst_map
, dst
+1, src_map
, src
+1, in_off
, out_off
, div_off
);
1600 static struct isl_basic_map
*add_constraints(struct isl_basic_map
*bmap1
,
1601 struct isl_basic_map
*bmap2
, unsigned i_pos
, unsigned o_pos
)
1606 if (!bmap1
|| !bmap2
)
1609 div_off
= bmap1
->n_div
;
1611 for (i
= 0; i
< bmap2
->n_eq
; ++i
) {
1612 int i1
= isl_basic_map_alloc_equality(bmap1
);
1615 copy_constraint(bmap1
, bmap1
->eq
[i1
], bmap2
, bmap2
->eq
[i
],
1616 i_pos
, o_pos
, div_off
);
1619 for (i
= 0; i
< bmap2
->n_ineq
; ++i
) {
1620 int i1
= isl_basic_map_alloc_inequality(bmap1
);
1623 copy_constraint(bmap1
, bmap1
->ineq
[i1
], bmap2
, bmap2
->ineq
[i
],
1624 i_pos
, o_pos
, div_off
);
1627 for (i
= 0; i
< bmap2
->n_div
; ++i
) {
1628 int i1
= isl_basic_map_alloc_div(bmap1
);
1631 copy_div(bmap1
, bmap1
->div
[i1
], bmap2
, bmap2
->div
[i
],
1632 i_pos
, o_pos
, div_off
);
1635 isl_basic_map_free(bmap2
);
1640 isl_basic_map_free(bmap1
);
1641 isl_basic_map_free(bmap2
);
1645 struct isl_basic_set
*isl_basic_set_add_constraints(struct isl_basic_set
*bset1
,
1646 struct isl_basic_set
*bset2
, unsigned pos
)
1648 return bset_from_bmap(add_constraints(bset_to_bmap(bset1
),
1649 bset_to_bmap(bset2
), 0, pos
));
1652 struct isl_basic_map
*isl_basic_map_extend_space(struct isl_basic_map
*base
,
1653 __isl_take isl_space
*dim
, unsigned extra
,
1654 unsigned n_eq
, unsigned n_ineq
)
1656 struct isl_basic_map
*ext
;
1666 dims_ok
= isl_space_is_equal(base
->dim
, dim
) &&
1667 base
->extra
>= base
->n_div
+ extra
;
1669 if (dims_ok
&& room_for_con(base
, n_eq
+ n_ineq
) &&
1670 room_for_ineq(base
, n_ineq
)) {
1671 isl_space_free(dim
);
1675 isl_assert(base
->ctx
, base
->dim
->nparam
<= dim
->nparam
, goto error
);
1676 isl_assert(base
->ctx
, base
->dim
->n_in
<= dim
->n_in
, goto error
);
1677 isl_assert(base
->ctx
, base
->dim
->n_out
<= dim
->n_out
, goto error
);
1678 extra
+= base
->extra
;
1680 n_ineq
+= base
->n_ineq
;
1682 ext
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
1688 ext
->sample
= isl_vec_copy(base
->sample
);
1689 flags
= base
->flags
;
1690 ext
= add_constraints(ext
, base
, 0, 0);
1693 ISL_F_CLR(ext
, ISL_BASIC_SET_FINAL
);
1699 isl_space_free(dim
);
1700 isl_basic_map_free(base
);
1704 struct isl_basic_set
*isl_basic_set_extend_space(struct isl_basic_set
*base
,
1705 __isl_take isl_space
*dim
, unsigned extra
,
1706 unsigned n_eq
, unsigned n_ineq
)
1708 return bset_from_bmap(isl_basic_map_extend_space(bset_to_bmap(base
),
1709 dim
, extra
, n_eq
, n_ineq
));
1712 struct isl_basic_map
*isl_basic_map_extend_constraints(
1713 struct isl_basic_map
*base
, unsigned n_eq
, unsigned n_ineq
)
1717 return isl_basic_map_extend_space(base
, isl_space_copy(base
->dim
),
1721 struct isl_basic_map
*isl_basic_map_extend(struct isl_basic_map
*base
,
1722 unsigned nparam
, unsigned n_in
, unsigned n_out
, unsigned extra
,
1723 unsigned n_eq
, unsigned n_ineq
)
1725 struct isl_basic_map
*bmap
;
1730 dim
= isl_space_alloc(base
->ctx
, nparam
, n_in
, n_out
);
1734 bmap
= isl_basic_map_extend_space(base
, dim
, extra
, n_eq
, n_ineq
);
1737 isl_basic_map_free(base
);
1741 struct isl_basic_set
*isl_basic_set_extend(struct isl_basic_set
*base
,
1742 unsigned nparam
, unsigned dim
, unsigned extra
,
1743 unsigned n_eq
, unsigned n_ineq
)
1745 return bset_from_bmap(isl_basic_map_extend(bset_to_bmap(base
),
1746 nparam
, 0, dim
, extra
, n_eq
, n_ineq
));
1749 struct isl_basic_set
*isl_basic_set_extend_constraints(
1750 struct isl_basic_set
*base
, unsigned n_eq
, unsigned n_ineq
)
1752 isl_basic_map
*bmap
= bset_to_bmap(base
);
1753 bmap
= isl_basic_map_extend_constraints(bmap
, n_eq
, n_ineq
);
1754 return bset_from_bmap(bmap
);
1757 struct isl_basic_set
*isl_basic_set_cow(struct isl_basic_set
*bset
)
1759 return bset_from_bmap(isl_basic_map_cow(bset_to_bmap(bset
)));
1762 struct isl_basic_map
*isl_basic_map_cow(struct isl_basic_map
*bmap
)
1767 if (bmap
->ref
> 1) {
1769 bmap
= isl_basic_map_dup(bmap
);
1772 ISL_F_CLR(bmap
, ISL_BASIC_SET_FINAL
);
1773 ISL_F_CLR(bmap
, ISL_BASIC_MAP_REDUCED_COEFFICIENTS
);
1778 /* Clear all cached information in "map", either because it is about
1779 * to be modified or because it is being freed.
1780 * Always return the same pointer that is passed in.
1781 * This is needed for the use in isl_map_free.
1783 static __isl_give isl_map
*clear_caches(__isl_take isl_map
*map
)
1785 isl_basic_map_free(map
->cached_simple_hull
[0]);
1786 isl_basic_map_free(map
->cached_simple_hull
[1]);
1787 map
->cached_simple_hull
[0] = NULL
;
1788 map
->cached_simple_hull
[1] = NULL
;
1792 struct isl_set
*isl_set_cow(struct isl_set
*set
)
1794 return isl_map_cow(set
);
1797 /* Return an isl_map that is equal to "map" and that has only
1798 * a single reference.
1800 * If the original input already has only one reference, then
1801 * simply return it, but clear all cached information, since
1802 * it may be rendered invalid by the operations that will be
1803 * performed on the result.
1805 * Otherwise, create a duplicate (without any cached information).
1807 struct isl_map
*isl_map_cow(struct isl_map
*map
)
1813 return clear_caches(map
);
1815 return isl_map_dup(map
);
1818 static void swap_vars(struct isl_blk blk
, isl_int
*a
,
1819 unsigned a_len
, unsigned b_len
)
1821 isl_seq_cpy(blk
.data
, a
+a_len
, b_len
);
1822 isl_seq_cpy(blk
.data
+b_len
, a
, a_len
);
1823 isl_seq_cpy(a
, blk
.data
, b_len
+a_len
);
1826 static __isl_give isl_basic_map
*isl_basic_map_swap_vars(
1827 __isl_take isl_basic_map
*bmap
, unsigned pos
, unsigned n1
, unsigned n2
)
1835 isl_assert(bmap
->ctx
,
1836 pos
+ n1
+ n2
<= 1 + isl_basic_map_total_dim(bmap
), goto error
);
1838 if (n1
== 0 || n2
== 0)
1841 bmap
= isl_basic_map_cow(bmap
);
1845 blk
= isl_blk_alloc(bmap
->ctx
, n1
+ n2
);
1846 if (isl_blk_is_error(blk
))
1849 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1851 bmap
->eq
[i
] + pos
, n1
, n2
);
1853 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1855 bmap
->ineq
[i
] + pos
, n1
, n2
);
1857 for (i
= 0; i
< bmap
->n_div
; ++i
)
1859 bmap
->div
[i
]+1 + pos
, n1
, n2
);
1861 isl_blk_free(bmap
->ctx
, blk
);
1863 ISL_F_CLR(bmap
, ISL_BASIC_SET_NORMALIZED
);
1864 bmap
= isl_basic_map_gauss(bmap
, NULL
);
1865 return isl_basic_map_finalize(bmap
);
1867 isl_basic_map_free(bmap
);
1871 struct isl_basic_map
*isl_basic_map_set_to_empty(struct isl_basic_map
*bmap
)
1877 total
= isl_basic_map_total_dim(bmap
);
1878 if (isl_basic_map_free_div(bmap
, bmap
->n_div
) < 0)
1879 return isl_basic_map_free(bmap
);
1880 isl_basic_map_free_inequality(bmap
, bmap
->n_ineq
);
1882 isl_basic_map_free_equality(bmap
, bmap
->n_eq
-1);
1884 i
= isl_basic_map_alloc_equality(bmap
);
1888 isl_int_set_si(bmap
->eq
[i
][0], 1);
1889 isl_seq_clr(bmap
->eq
[i
]+1, total
);
1890 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
1891 isl_vec_free(bmap
->sample
);
1892 bmap
->sample
= NULL
;
1893 return isl_basic_map_finalize(bmap
);
1895 isl_basic_map_free(bmap
);
1899 struct isl_basic_set
*isl_basic_set_set_to_empty(struct isl_basic_set
*bset
)
1901 return bset_from_bmap(isl_basic_map_set_to_empty(bset_to_bmap(bset
)));
1904 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
1907 static void swap_div(__isl_keep isl_basic_map
*bmap
, int a
, int b
)
1909 isl_int
*t
= bmap
->div
[a
];
1910 bmap
->div
[a
] = bmap
->div
[b
];
1914 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
1915 * div definitions accordingly.
1917 void isl_basic_map_swap_div(struct isl_basic_map
*bmap
, int a
, int b
)
1920 unsigned off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
1922 swap_div(bmap
, a
, b
);
1924 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1925 isl_int_swap(bmap
->eq
[i
][1+off
+a
], bmap
->eq
[i
][1+off
+b
]);
1927 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1928 isl_int_swap(bmap
->ineq
[i
][1+off
+a
], bmap
->ineq
[i
][1+off
+b
]);
1930 for (i
= 0; i
< bmap
->n_div
; ++i
)
1931 isl_int_swap(bmap
->div
[i
][1+1+off
+a
], bmap
->div
[i
][1+1+off
+b
]);
1932 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1935 /* Swap divs "a" and "b" in "bset" and adjust the constraints and
1936 * div definitions accordingly.
1938 void isl_basic_set_swap_div(__isl_keep isl_basic_set
*bset
, int a
, int b
)
1940 isl_basic_map_swap_div(bset
, a
, b
);
1943 /* Eliminate the specified n dimensions starting at first from the
1944 * constraints, without removing the dimensions from the space.
1945 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1947 __isl_give isl_map
*isl_map_eliminate(__isl_take isl_map
*map
,
1948 enum isl_dim_type type
, unsigned first
, unsigned n
)
1957 if (first
+ n
> isl_map_dim(map
, type
) || first
+ n
< first
)
1958 isl_die(map
->ctx
, isl_error_invalid
,
1959 "index out of bounds", goto error
);
1961 map
= isl_map_cow(map
);
1965 for (i
= 0; i
< map
->n
; ++i
) {
1966 map
->p
[i
] = isl_basic_map_eliminate(map
->p
[i
], type
, first
, n
);
1976 /* Eliminate the specified n dimensions starting at first from the
1977 * constraints, without removing the dimensions from the space.
1978 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1980 __isl_give isl_set
*isl_set_eliminate(__isl_take isl_set
*set
,
1981 enum isl_dim_type type
, unsigned first
, unsigned n
)
1983 return set_from_map(isl_map_eliminate(set_to_map(set
), type
, first
, n
));
1986 /* Eliminate the specified n dimensions starting at first from the
1987 * constraints, without removing the dimensions from the space.
1988 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1990 __isl_give isl_set
*isl_set_eliminate_dims(__isl_take isl_set
*set
,
1991 unsigned first
, unsigned n
)
1993 return isl_set_eliminate(set
, isl_dim_set
, first
, n
);
1996 __isl_give isl_basic_map
*isl_basic_map_remove_divs(
1997 __isl_take isl_basic_map
*bmap
)
2001 bmap
= isl_basic_map_eliminate_vars(bmap
,
2002 isl_space_dim(bmap
->dim
, isl_dim_all
), bmap
->n_div
);
2006 return isl_basic_map_finalize(bmap
);
2009 __isl_give isl_basic_set
*isl_basic_set_remove_divs(
2010 __isl_take isl_basic_set
*bset
)
2012 return bset_from_bmap(isl_basic_map_remove_divs(bset_to_bmap(bset
)));
2015 __isl_give isl_map
*isl_map_remove_divs(__isl_take isl_map
*map
)
2024 map
= isl_map_cow(map
);
2028 for (i
= 0; i
< map
->n
; ++i
) {
2029 map
->p
[i
] = isl_basic_map_remove_divs(map
->p
[i
]);
2039 __isl_give isl_set
*isl_set_remove_divs(__isl_take isl_set
*set
)
2041 return isl_map_remove_divs(set
);
2044 struct isl_basic_map
*isl_basic_map_remove_dims(struct isl_basic_map
*bmap
,
2045 enum isl_dim_type type
, unsigned first
, unsigned n
)
2047 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2048 return isl_basic_map_free(bmap
);
2049 if (n
== 0 && !isl_space_is_named_or_nested(bmap
->dim
, type
))
2051 bmap
= isl_basic_map_eliminate_vars(bmap
,
2052 isl_basic_map_offset(bmap
, type
) - 1 + first
, n
);
2055 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
) && type
== isl_dim_div
)
2057 bmap
= isl_basic_map_drop(bmap
, type
, first
, n
);
2061 /* Return true if the definition of the given div (recursively) involves
2062 * any of the given variables.
2064 static isl_bool
div_involves_vars(__isl_keep isl_basic_map
*bmap
, int div
,
2065 unsigned first
, unsigned n
)
2068 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
2070 if (isl_int_is_zero(bmap
->div
[div
][0]))
2071 return isl_bool_false
;
2072 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + first
, n
) >= 0)
2073 return isl_bool_true
;
2075 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2078 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
2080 involves
= div_involves_vars(bmap
, i
, first
, n
);
2081 if (involves
< 0 || involves
)
2085 return isl_bool_false
;
2088 /* Try and add a lower and/or upper bound on "div" to "bmap"
2089 * based on inequality "i".
2090 * "total" is the total number of variables (excluding the divs).
2091 * "v" is a temporary object that can be used during the calculations.
2092 * If "lb" is set, then a lower bound should be constructed.
2093 * If "ub" is set, then an upper bound should be constructed.
2095 * The calling function has already checked that the inequality does not
2096 * reference "div", but we still need to check that the inequality is
2097 * of the right form. We'll consider the case where we want to construct
2098 * a lower bound. The construction of upper bounds is similar.
2100 * Let "div" be of the form
2102 * q = floor((a + f(x))/d)
2104 * We essentially check if constraint "i" is of the form
2108 * so that we can use it to derive a lower bound on "div".
2109 * However, we allow a slightly more general form
2113 * with the condition that the coefficients of g(x) - f(x) are all
2115 * Rewriting this constraint as
2119 * adding a + f(x) to both sides and dividing by d, we obtain
2121 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
2123 * Taking the floor on both sides, we obtain
2125 * q >= floor((a-b)/d) + (f(x)-g(x))/d
2129 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
2131 * In the case of an upper bound, we construct the constraint
2133 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
2136 static __isl_give isl_basic_map
*insert_bounds_on_div_from_ineq(
2137 __isl_take isl_basic_map
*bmap
, int div
, int i
,
2138 unsigned total
, isl_int v
, int lb
, int ub
)
2142 for (j
= 0; (lb
|| ub
) && j
< total
+ bmap
->n_div
; ++j
) {
2144 isl_int_sub(v
, bmap
->ineq
[i
][1 + j
],
2145 bmap
->div
[div
][1 + 1 + j
]);
2146 lb
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
2149 isl_int_add(v
, bmap
->ineq
[i
][1 + j
],
2150 bmap
->div
[div
][1 + 1 + j
]);
2151 ub
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
2157 bmap
= isl_basic_map_cow(bmap
);
2158 bmap
= isl_basic_map_extend_constraints(bmap
, 0, lb
+ ub
);
2160 int k
= isl_basic_map_alloc_inequality(bmap
);
2163 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
2164 isl_int_sub(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
2165 bmap
->div
[div
][1 + j
]);
2166 isl_int_cdiv_q(bmap
->ineq
[k
][j
],
2167 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
2169 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], 1);
2172 int k
= isl_basic_map_alloc_inequality(bmap
);
2175 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
2176 isl_int_add(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
2177 bmap
->div
[div
][1 + j
]);
2178 isl_int_fdiv_q(bmap
->ineq
[k
][j
],
2179 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
2181 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], -1);
2184 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
2187 isl_basic_map_free(bmap
);
2191 /* This function is called right before "div" is eliminated from "bmap"
2192 * using Fourier-Motzkin.
2193 * Look through the constraints of "bmap" for constraints on the argument
2194 * of the integer division and use them to construct constraints on the
2195 * integer division itself. These constraints can then be combined
2196 * during the Fourier-Motzkin elimination.
2197 * Note that it is only useful to introduce lower bounds on "div"
2198 * if "bmap" already contains upper bounds on "div" as the newly
2199 * introduce lower bounds can then be combined with the pre-existing
2200 * upper bounds. Similarly for upper bounds.
2201 * We therefore first check if "bmap" contains any lower and/or upper bounds
2204 * It is interesting to note that the introduction of these constraints
2205 * can indeed lead to more accurate results, even when compared to
2206 * deriving constraints on the argument of "div" from constraints on "div".
2207 * Consider, for example, the set
2209 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2211 * The second constraint can be rewritten as
2213 * 2 * [(-i-2j+3)/4] + k >= 0
2215 * from which we can derive
2217 * -i - 2j + 3 >= -2k
2223 * Combined with the first constraint, we obtain
2225 * -3 <= 3 + 2k or k >= -3
2227 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2228 * the first constraint, we obtain
2230 * [(i + 2j)/4] >= [-3/4] = -1
2232 * Combining this constraint with the second constraint, we obtain
2236 static __isl_give isl_basic_map
*insert_bounds_on_div(
2237 __isl_take isl_basic_map
*bmap
, int div
)
2240 int check_lb
, check_ub
;
2247 if (isl_int_is_zero(bmap
->div
[div
][0]))
2250 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
2254 for (i
= 0; (!check_lb
|| !check_ub
) && i
< bmap
->n_ineq
; ++i
) {
2255 int s
= isl_int_sgn(bmap
->ineq
[i
][1 + total
+ div
]);
2262 if (!check_lb
&& !check_ub
)
2267 for (i
= 0; bmap
&& i
< bmap
->n_ineq
; ++i
) {
2268 if (!isl_int_is_zero(bmap
->ineq
[i
][1 + total
+ div
]))
2271 bmap
= insert_bounds_on_div_from_ineq(bmap
, div
, i
, total
, v
,
2272 check_lb
, check_ub
);
2280 /* Remove all divs (recursively) involving any of the given dimensions
2281 * in their definitions.
2283 __isl_give isl_basic_map
*isl_basic_map_remove_divs_involving_dims(
2284 __isl_take isl_basic_map
*bmap
,
2285 enum isl_dim_type type
, unsigned first
, unsigned n
)
2289 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2290 return isl_basic_map_free(bmap
);
2291 first
+= isl_basic_map_offset(bmap
, type
);
2293 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2296 involves
= div_involves_vars(bmap
, i
, first
, n
);
2298 return isl_basic_map_free(bmap
);
2301 bmap
= insert_bounds_on_div(bmap
, i
);
2302 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
2311 __isl_give isl_basic_set
*isl_basic_set_remove_divs_involving_dims(
2312 __isl_take isl_basic_set
*bset
,
2313 enum isl_dim_type type
, unsigned first
, unsigned n
)
2315 return isl_basic_map_remove_divs_involving_dims(bset
, type
, first
, n
);
2318 __isl_give isl_map
*isl_map_remove_divs_involving_dims(__isl_take isl_map
*map
,
2319 enum isl_dim_type type
, unsigned first
, unsigned n
)
2328 map
= isl_map_cow(map
);
2332 for (i
= 0; i
< map
->n
; ++i
) {
2333 map
->p
[i
] = isl_basic_map_remove_divs_involving_dims(map
->p
[i
],
2344 __isl_give isl_set
*isl_set_remove_divs_involving_dims(__isl_take isl_set
*set
,
2345 enum isl_dim_type type
, unsigned first
, unsigned n
)
2347 return set_from_map(isl_map_remove_divs_involving_dims(set_to_map(set
),
2351 /* Does the description of "bmap" depend on the specified dimensions?
2352 * We also check whether the dimensions appear in any of the div definitions.
2353 * In principle there is no need for this check. If the dimensions appear
2354 * in a div definition, they also appear in the defining constraints of that
2357 isl_bool
isl_basic_map_involves_dims(__isl_keep isl_basic_map
*bmap
,
2358 enum isl_dim_type type
, unsigned first
, unsigned n
)
2362 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2363 return isl_bool_error
;
2365 first
+= isl_basic_map_offset(bmap
, type
);
2366 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2367 if (isl_seq_first_non_zero(bmap
->eq
[i
] + first
, n
) >= 0)
2368 return isl_bool_true
;
2369 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2370 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + first
, n
) >= 0)
2371 return isl_bool_true
;
2372 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2373 if (isl_int_is_zero(bmap
->div
[i
][0]))
2375 if (isl_seq_first_non_zero(bmap
->div
[i
] + 1 + first
, n
) >= 0)
2376 return isl_bool_true
;
2379 return isl_bool_false
;
2382 isl_bool
isl_map_involves_dims(__isl_keep isl_map
*map
,
2383 enum isl_dim_type type
, unsigned first
, unsigned n
)
2388 return isl_bool_error
;
2390 if (first
+ n
> isl_map_dim(map
, type
))
2391 isl_die(map
->ctx
, isl_error_invalid
,
2392 "index out of bounds", return isl_bool_error
);
2394 for (i
= 0; i
< map
->n
; ++i
) {
2395 isl_bool involves
= isl_basic_map_involves_dims(map
->p
[i
],
2397 if (involves
< 0 || involves
)
2401 return isl_bool_false
;
2404 isl_bool
isl_basic_set_involves_dims(__isl_keep isl_basic_set
*bset
,
2405 enum isl_dim_type type
, unsigned first
, unsigned n
)
2407 return isl_basic_map_involves_dims(bset
, type
, first
, n
);
2410 isl_bool
isl_set_involves_dims(__isl_keep isl_set
*set
,
2411 enum isl_dim_type type
, unsigned first
, unsigned n
)
2413 return isl_map_involves_dims(set
, type
, first
, n
);
2416 /* Drop all constraints in bmap that involve any of the dimensions
2417 * first to first+n-1.
2419 static __isl_give isl_basic_map
*isl_basic_map_drop_constraints_involving(
2420 __isl_take isl_basic_map
*bmap
, unsigned first
, unsigned n
)
2427 bmap
= isl_basic_map_cow(bmap
);
2432 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
) {
2433 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + first
, n
) == -1)
2435 isl_basic_map_drop_equality(bmap
, i
);
2438 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
2439 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + 1 + first
, n
) == -1)
2441 isl_basic_map_drop_inequality(bmap
, i
);
2444 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
2448 /* Drop all constraints in bset that involve any of the dimensions
2449 * first to first+n-1.
2451 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_involving(
2452 __isl_take isl_basic_set
*bset
, unsigned first
, unsigned n
)
2454 return isl_basic_map_drop_constraints_involving(bset
, first
, n
);
2457 /* Drop all constraints in bmap that do not involve any of the dimensions
2458 * first to first + n - 1 of the given type.
2460 __isl_give isl_basic_map
*isl_basic_map_drop_constraints_not_involving_dims(
2461 __isl_take isl_basic_map
*bmap
,
2462 enum isl_dim_type type
, unsigned first
, unsigned n
)
2467 isl_space
*space
= isl_basic_map_get_space(bmap
);
2468 isl_basic_map_free(bmap
);
2469 return isl_basic_map_universe(space
);
2471 bmap
= isl_basic_map_cow(bmap
);
2475 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2476 return isl_basic_map_free(bmap
);
2478 first
+= isl_basic_map_offset(bmap
, type
) - 1;
2480 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
) {
2481 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + first
, n
) != -1)
2483 isl_basic_map_drop_equality(bmap
, i
);
2486 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
2487 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + 1 + first
, n
) != -1)
2489 isl_basic_map_drop_inequality(bmap
, i
);
2492 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
2496 /* Drop all constraints in bset that do not involve any of the dimensions
2497 * first to first + n - 1 of the given type.
2499 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_not_involving_dims(
2500 __isl_take isl_basic_set
*bset
,
2501 enum isl_dim_type type
, unsigned first
, unsigned n
)
2503 return isl_basic_map_drop_constraints_not_involving_dims(bset
,
2507 /* Drop all constraints in bmap that involve any of the dimensions
2508 * first to first + n - 1 of the given type.
2510 __isl_give isl_basic_map
*isl_basic_map_drop_constraints_involving_dims(
2511 __isl_take isl_basic_map
*bmap
,
2512 enum isl_dim_type type
, unsigned first
, unsigned n
)
2519 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2520 return isl_basic_map_free(bmap
);
2522 bmap
= isl_basic_map_remove_divs_involving_dims(bmap
, type
, first
, n
);
2523 first
+= isl_basic_map_offset(bmap
, type
) - 1;
2524 return isl_basic_map_drop_constraints_involving(bmap
, first
, n
);
2527 /* Drop all constraints in bset that involve any of the dimensions
2528 * first to first + n - 1 of the given type.
2530 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_involving_dims(
2531 __isl_take isl_basic_set
*bset
,
2532 enum isl_dim_type type
, unsigned first
, unsigned n
)
2534 return isl_basic_map_drop_constraints_involving_dims(bset
,
2538 /* Drop constraints from "map" by applying "drop" to each basic map.
2540 static __isl_give isl_map
*drop_constraints(__isl_take isl_map
*map
,
2541 enum isl_dim_type type
, unsigned first
, unsigned n
,
2542 __isl_give isl_basic_map
*(*drop
)(__isl_take isl_basic_map
*bmap
,
2543 enum isl_dim_type type
, unsigned first
, unsigned n
))
2551 dim
= isl_map_dim(map
, type
);
2552 if (first
+ n
> dim
|| first
+ n
< first
)
2553 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
2554 "index out of bounds", return isl_map_free(map
));
2556 map
= isl_map_cow(map
);
2560 for (i
= 0; i
< map
->n
; ++i
) {
2561 map
->p
[i
] = drop(map
->p
[i
], type
, first
, n
);
2563 return isl_map_free(map
);
2567 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
2572 /* Drop all constraints in map that involve any of the dimensions
2573 * first to first + n - 1 of the given type.
2575 __isl_give isl_map
*isl_map_drop_constraints_involving_dims(
2576 __isl_take isl_map
*map
,
2577 enum isl_dim_type type
, unsigned first
, unsigned n
)
2581 return drop_constraints(map
, type
, first
, n
,
2582 &isl_basic_map_drop_constraints_involving_dims
);
2585 /* Drop all constraints in "map" that do not involve any of the dimensions
2586 * first to first + n - 1 of the given type.
2588 __isl_give isl_map
*isl_map_drop_constraints_not_involving_dims(
2589 __isl_take isl_map
*map
,
2590 enum isl_dim_type type
, unsigned first
, unsigned n
)
2593 isl_space
*space
= isl_map_get_space(map
);
2595 return isl_map_universe(space
);
2597 return drop_constraints(map
, type
, first
, n
,
2598 &isl_basic_map_drop_constraints_not_involving_dims
);
2601 /* Drop all constraints in set that involve any of the dimensions
2602 * first to first + n - 1 of the given type.
2604 __isl_give isl_set
*isl_set_drop_constraints_involving_dims(
2605 __isl_take isl_set
*set
,
2606 enum isl_dim_type type
, unsigned first
, unsigned n
)
2608 return isl_map_drop_constraints_involving_dims(set
, type
, first
, n
);
2611 /* Drop all constraints in "set" that do not involve any of the dimensions
2612 * first to first + n - 1 of the given type.
2614 __isl_give isl_set
*isl_set_drop_constraints_not_involving_dims(
2615 __isl_take isl_set
*set
,
2616 enum isl_dim_type type
, unsigned first
, unsigned n
)
2618 return isl_map_drop_constraints_not_involving_dims(set
, type
, first
, n
);
2621 /* Does local variable "div" of "bmap" have a complete explicit representation?
2622 * Having a complete explicit representation requires not only
2623 * an explicit representation, but also that all local variables
2624 * that appear in this explicit representation in turn have
2625 * a complete explicit representation.
2627 isl_bool
isl_basic_map_div_is_known(__isl_keep isl_basic_map
*bmap
, int div
)
2630 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
2633 marked
= isl_basic_map_div_is_marked_unknown(bmap
, div
);
2634 if (marked
< 0 || marked
)
2635 return isl_bool_not(marked
);
2637 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2640 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
2642 known
= isl_basic_map_div_is_known(bmap
, i
);
2643 if (known
< 0 || !known
)
2647 return isl_bool_true
;
2650 /* Remove all divs that are unknown or defined in terms of unknown divs.
2652 __isl_give isl_basic_map
*isl_basic_map_remove_unknown_divs(
2653 __isl_take isl_basic_map
*bmap
)
2660 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2661 if (isl_basic_map_div_is_known(bmap
, i
))
2663 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
2672 /* Remove all divs that are unknown or defined in terms of unknown divs.
2674 __isl_give isl_basic_set
*isl_basic_set_remove_unknown_divs(
2675 __isl_take isl_basic_set
*bset
)
2677 return isl_basic_map_remove_unknown_divs(bset
);
2680 __isl_give isl_map
*isl_map_remove_unknown_divs(__isl_take isl_map
*map
)
2689 map
= isl_map_cow(map
);
2693 for (i
= 0; i
< map
->n
; ++i
) {
2694 map
->p
[i
] = isl_basic_map_remove_unknown_divs(map
->p
[i
]);
2704 __isl_give isl_set
*isl_set_remove_unknown_divs(__isl_take isl_set
*set
)
2706 return set_from_map(isl_map_remove_unknown_divs(set_to_map(set
)));
2709 __isl_give isl_basic_set
*isl_basic_set_remove_dims(
2710 __isl_take isl_basic_set
*bset
,
2711 enum isl_dim_type type
, unsigned first
, unsigned n
)
2713 isl_basic_map
*bmap
= bset_to_bmap(bset
);
2714 bmap
= isl_basic_map_remove_dims(bmap
, type
, first
, n
);
2715 return bset_from_bmap(bmap
);
2718 struct isl_map
*isl_map_remove_dims(struct isl_map
*map
,
2719 enum isl_dim_type type
, unsigned first
, unsigned n
)
2726 map
= isl_map_cow(map
);
2729 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
2731 for (i
= 0; i
< map
->n
; ++i
) {
2732 map
->p
[i
] = isl_basic_map_eliminate_vars(map
->p
[i
],
2733 isl_basic_map_offset(map
->p
[i
], type
) - 1 + first
, n
);
2737 map
= isl_map_drop(map
, type
, first
, n
);
2744 __isl_give isl_set
*isl_set_remove_dims(__isl_take isl_set
*bset
,
2745 enum isl_dim_type type
, unsigned first
, unsigned n
)
2747 return set_from_map(isl_map_remove_dims(set_to_map(bset
),
2751 /* Project out n inputs starting at first using Fourier-Motzkin */
2752 struct isl_map
*isl_map_remove_inputs(struct isl_map
*map
,
2753 unsigned first
, unsigned n
)
2755 return isl_map_remove_dims(map
, isl_dim_in
, first
, n
);
2758 static void dump_term(struct isl_basic_map
*bmap
,
2759 isl_int c
, int pos
, FILE *out
)
2762 unsigned in
= isl_basic_map_n_in(bmap
);
2763 unsigned dim
= in
+ isl_basic_map_n_out(bmap
);
2764 unsigned nparam
= isl_basic_map_n_param(bmap
);
2766 isl_int_print(out
, c
, 0);
2768 if (!isl_int_is_one(c
))
2769 isl_int_print(out
, c
, 0);
2770 if (pos
< 1 + nparam
) {
2771 name
= isl_space_get_dim_name(bmap
->dim
,
2772 isl_dim_param
, pos
- 1);
2774 fprintf(out
, "%s", name
);
2776 fprintf(out
, "p%d", pos
- 1);
2777 } else if (pos
< 1 + nparam
+ in
)
2778 fprintf(out
, "i%d", pos
- 1 - nparam
);
2779 else if (pos
< 1 + nparam
+ dim
)
2780 fprintf(out
, "o%d", pos
- 1 - nparam
- in
);
2782 fprintf(out
, "e%d", pos
- 1 - nparam
- dim
);
2786 static void dump_constraint_sign(struct isl_basic_map
*bmap
, isl_int
*c
,
2787 int sign
, FILE *out
)
2791 unsigned len
= 1 + isl_basic_map_total_dim(bmap
);
2795 for (i
= 0, first
= 1; i
< len
; ++i
) {
2796 if (isl_int_sgn(c
[i
]) * sign
<= 0)
2799 fprintf(out
, " + ");
2801 isl_int_abs(v
, c
[i
]);
2802 dump_term(bmap
, v
, i
, out
);
2809 static void dump_constraint(struct isl_basic_map
*bmap
, isl_int
*c
,
2810 const char *op
, FILE *out
, int indent
)
2814 fprintf(out
, "%*s", indent
, "");
2816 dump_constraint_sign(bmap
, c
, 1, out
);
2817 fprintf(out
, " %s ", op
);
2818 dump_constraint_sign(bmap
, c
, -1, out
);
2822 for (i
= bmap
->n_div
; i
< bmap
->extra
; ++i
) {
2823 if (isl_int_is_zero(c
[1+isl_space_dim(bmap
->dim
, isl_dim_all
)+i
]))
2825 fprintf(out
, "%*s", indent
, "");
2826 fprintf(out
, "ERROR: unused div coefficient not zero\n");
2831 static void dump_constraints(struct isl_basic_map
*bmap
,
2832 isl_int
**c
, unsigned n
,
2833 const char *op
, FILE *out
, int indent
)
2837 for (i
= 0; i
< n
; ++i
)
2838 dump_constraint(bmap
, c
[i
], op
, out
, indent
);
2841 static void dump_affine(struct isl_basic_map
*bmap
, isl_int
*exp
, FILE *out
)
2845 unsigned total
= isl_basic_map_total_dim(bmap
);
2847 for (j
= 0; j
< 1 + total
; ++j
) {
2848 if (isl_int_is_zero(exp
[j
]))
2850 if (!first
&& isl_int_is_pos(exp
[j
]))
2852 dump_term(bmap
, exp
[j
], j
, out
);
2857 static void dump(struct isl_basic_map
*bmap
, FILE *out
, int indent
)
2861 dump_constraints(bmap
, bmap
->eq
, bmap
->n_eq
, "=", out
, indent
);
2862 dump_constraints(bmap
, bmap
->ineq
, bmap
->n_ineq
, ">=", out
, indent
);
2864 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2865 fprintf(out
, "%*s", indent
, "");
2866 fprintf(out
, "e%d = [(", i
);
2867 dump_affine(bmap
, bmap
->div
[i
]+1, out
);
2869 isl_int_print(out
, bmap
->div
[i
][0], 0);
2870 fprintf(out
, "]\n");
2874 void isl_basic_set_print_internal(struct isl_basic_set
*bset
,
2875 FILE *out
, int indent
)
2878 fprintf(out
, "null basic set\n");
2882 fprintf(out
, "%*s", indent
, "");
2883 fprintf(out
, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
2884 bset
->ref
, bset
->dim
->nparam
, bset
->dim
->n_out
,
2885 bset
->extra
, bset
->flags
);
2886 dump(bset_to_bmap(bset
), out
, indent
);
2889 void isl_basic_map_print_internal(struct isl_basic_map
*bmap
,
2890 FILE *out
, int indent
)
2893 fprintf(out
, "null basic map\n");
2897 fprintf(out
, "%*s", indent
, "");
2898 fprintf(out
, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
2899 "flags: %x, n_name: %d\n",
2901 bmap
->dim
->nparam
, bmap
->dim
->n_in
, bmap
->dim
->n_out
,
2902 bmap
->extra
, bmap
->flags
, bmap
->dim
->n_id
);
2903 dump(bmap
, out
, indent
);
2906 int isl_inequality_negate(struct isl_basic_map
*bmap
, unsigned pos
)
2911 total
= isl_basic_map_total_dim(bmap
);
2912 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
2913 isl_seq_neg(bmap
->ineq
[pos
], bmap
->ineq
[pos
], 1 + total
);
2914 isl_int_sub_ui(bmap
->ineq
[pos
][0], bmap
->ineq
[pos
][0], 1);
2915 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
2919 __isl_give isl_set
*isl_set_alloc_space(__isl_take isl_space
*space
, int n
,
2924 if (isl_space_dim(space
, isl_dim_in
) != 0)
2925 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
2926 "set cannot have input dimensions", goto error
);
2927 return isl_map_alloc_space(space
, n
, flags
);
2929 isl_space_free(space
);
2933 /* Make sure "map" has room for at least "n" more basic maps.
2935 struct isl_map
*isl_map_grow(struct isl_map
*map
, int n
)
2938 struct isl_map
*grown
= NULL
;
2942 isl_assert(map
->ctx
, n
>= 0, goto error
);
2943 if (map
->n
+ n
<= map
->size
)
2945 grown
= isl_map_alloc_space(isl_map_get_space(map
), map
->n
+ n
, map
->flags
);
2948 for (i
= 0; i
< map
->n
; ++i
) {
2949 grown
->p
[i
] = isl_basic_map_copy(map
->p
[i
]);
2957 isl_map_free(grown
);
2962 /* Make sure "set" has room for at least "n" more basic sets.
2964 struct isl_set
*isl_set_grow(struct isl_set
*set
, int n
)
2966 return set_from_map(isl_map_grow(set_to_map(set
), n
));
2969 struct isl_set
*isl_set_from_basic_set(struct isl_basic_set
*bset
)
2971 return isl_map_from_basic_map(bset
);
2974 struct isl_map
*isl_map_from_basic_map(struct isl_basic_map
*bmap
)
2976 struct isl_map
*map
;
2981 map
= isl_map_alloc_space(isl_space_copy(bmap
->dim
), 1, ISL_MAP_DISJOINT
);
2982 return isl_map_add_basic_map(map
, bmap
);
2985 __isl_give isl_set
*isl_set_add_basic_set(__isl_take isl_set
*set
,
2986 __isl_take isl_basic_set
*bset
)
2988 return set_from_map(isl_map_add_basic_map(set_to_map(set
),
2989 bset_to_bmap(bset
)));
2992 __isl_null isl_set
*isl_set_free(__isl_take isl_set
*set
)
2994 return isl_map_free(set
);
2997 void isl_set_print_internal(struct isl_set
*set
, FILE *out
, int indent
)
3002 fprintf(out
, "null set\n");
3006 fprintf(out
, "%*s", indent
, "");
3007 fprintf(out
, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
3008 set
->ref
, set
->n
, set
->dim
->nparam
, set
->dim
->n_out
,
3010 for (i
= 0; i
< set
->n
; ++i
) {
3011 fprintf(out
, "%*s", indent
, "");
3012 fprintf(out
, "basic set %d:\n", i
);
3013 isl_basic_set_print_internal(set
->p
[i
], out
, indent
+4);
3017 void isl_map_print_internal(struct isl_map
*map
, FILE *out
, int indent
)
3022 fprintf(out
, "null map\n");
3026 fprintf(out
, "%*s", indent
, "");
3027 fprintf(out
, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
3028 "flags: %x, n_name: %d\n",
3029 map
->ref
, map
->n
, map
->dim
->nparam
, map
->dim
->n_in
,
3030 map
->dim
->n_out
, map
->flags
, map
->dim
->n_id
);
3031 for (i
= 0; i
< map
->n
; ++i
) {
3032 fprintf(out
, "%*s", indent
, "");
3033 fprintf(out
, "basic map %d:\n", i
);
3034 isl_basic_map_print_internal(map
->p
[i
], out
, indent
+4);
3038 struct isl_basic_map
*isl_basic_map_intersect_domain(
3039 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
3041 struct isl_basic_map
*bmap_domain
;
3046 isl_assert(bset
->ctx
, isl_space_match(bmap
->dim
, isl_dim_param
,
3047 bset
->dim
, isl_dim_param
), goto error
);
3049 if (isl_space_dim(bset
->dim
, isl_dim_set
) != 0)
3050 isl_assert(bset
->ctx
,
3051 isl_basic_map_compatible_domain(bmap
, bset
), goto error
);
3053 bmap
= isl_basic_map_cow(bmap
);
3056 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
3057 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
3058 bmap_domain
= isl_basic_map_from_domain(bset
);
3059 bmap
= add_constraints(bmap
, bmap_domain
, 0, 0);
3061 bmap
= isl_basic_map_simplify(bmap
);
3062 return isl_basic_map_finalize(bmap
);
3064 isl_basic_map_free(bmap
);
3065 isl_basic_set_free(bset
);
3069 /* Check that the space of "bset" is the same as that of the range of "bmap".
3071 static isl_stat
isl_basic_map_check_compatible_range(
3072 __isl_keep isl_basic_map
*bmap
, __isl_keep isl_basic_set
*bset
)
3076 ok
= isl_basic_map_compatible_range(bmap
, bset
);
3078 return isl_stat_error
;
3080 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
3081 "incompatible spaces", return isl_stat_error
);
3086 struct isl_basic_map
*isl_basic_map_intersect_range(
3087 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
3089 struct isl_basic_map
*bmap_range
;
3094 isl_assert(bset
->ctx
, isl_space_match(bmap
->dim
, isl_dim_param
,
3095 bset
->dim
, isl_dim_param
), goto error
);
3097 if (isl_space_dim(bset
->dim
, isl_dim_set
) != 0 &&
3098 isl_basic_map_check_compatible_range(bmap
, bset
) < 0)
3101 if (isl_basic_set_plain_is_universe(bset
)) {
3102 isl_basic_set_free(bset
);
3106 bmap
= isl_basic_map_cow(bmap
);
3109 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
3110 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
3111 bmap_range
= bset_to_bmap(bset
);
3112 bmap
= add_constraints(bmap
, bmap_range
, 0, 0);
3114 bmap
= isl_basic_map_simplify(bmap
);
3115 return isl_basic_map_finalize(bmap
);
3117 isl_basic_map_free(bmap
);
3118 isl_basic_set_free(bset
);
3122 isl_bool
isl_basic_map_contains(__isl_keep isl_basic_map
*bmap
,
3123 __isl_keep isl_vec
*vec
)
3130 return isl_bool_error
;
3132 total
= 1 + isl_basic_map_total_dim(bmap
);
3133 if (total
!= vec
->size
)
3134 return isl_bool_false
;
3138 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
3139 isl_seq_inner_product(vec
->el
, bmap
->eq
[i
], total
, &s
);
3140 if (!isl_int_is_zero(s
)) {
3142 return isl_bool_false
;
3146 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
3147 isl_seq_inner_product(vec
->el
, bmap
->ineq
[i
], total
, &s
);
3148 if (isl_int_is_neg(s
)) {
3150 return isl_bool_false
;
3156 return isl_bool_true
;
3159 isl_bool
isl_basic_set_contains(__isl_keep isl_basic_set
*bset
,
3160 __isl_keep isl_vec
*vec
)
3162 return isl_basic_map_contains(bset_to_bmap(bset
), vec
);
3165 struct isl_basic_map
*isl_basic_map_intersect(
3166 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3168 struct isl_vec
*sample
= NULL
;
3170 if (!bmap1
|| !bmap2
)
3173 isl_assert(bmap1
->ctx
, isl_space_match(bmap1
->dim
, isl_dim_param
,
3174 bmap2
->dim
, isl_dim_param
), goto error
);
3175 if (isl_space_dim(bmap1
->dim
, isl_dim_all
) ==
3176 isl_space_dim(bmap1
->dim
, isl_dim_param
) &&
3177 isl_space_dim(bmap2
->dim
, isl_dim_all
) !=
3178 isl_space_dim(bmap2
->dim
, isl_dim_param
))
3179 return isl_basic_map_intersect(bmap2
, bmap1
);
3181 if (isl_space_dim(bmap2
->dim
, isl_dim_all
) !=
3182 isl_space_dim(bmap2
->dim
, isl_dim_param
))
3183 isl_assert(bmap1
->ctx
,
3184 isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
3186 if (isl_basic_map_plain_is_empty(bmap1
)) {
3187 isl_basic_map_free(bmap2
);
3190 if (isl_basic_map_plain_is_empty(bmap2
)) {
3191 isl_basic_map_free(bmap1
);
3195 if (bmap1
->sample
&&
3196 isl_basic_map_contains(bmap1
, bmap1
->sample
) > 0 &&
3197 isl_basic_map_contains(bmap2
, bmap1
->sample
) > 0)
3198 sample
= isl_vec_copy(bmap1
->sample
);
3199 else if (bmap2
->sample
&&
3200 isl_basic_map_contains(bmap1
, bmap2
->sample
) > 0 &&
3201 isl_basic_map_contains(bmap2
, bmap2
->sample
) > 0)
3202 sample
= isl_vec_copy(bmap2
->sample
);
3204 bmap1
= isl_basic_map_cow(bmap1
);
3207 bmap1
= isl_basic_map_extend_space(bmap1
, isl_space_copy(bmap1
->dim
),
3208 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
3209 bmap1
= add_constraints(bmap1
, bmap2
, 0, 0);
3212 isl_vec_free(sample
);
3214 isl_vec_free(bmap1
->sample
);
3215 bmap1
->sample
= sample
;
3218 bmap1
= isl_basic_map_simplify(bmap1
);
3219 return isl_basic_map_finalize(bmap1
);
3222 isl_vec_free(sample
);
3223 isl_basic_map_free(bmap1
);
3224 isl_basic_map_free(bmap2
);
3228 struct isl_basic_set
*isl_basic_set_intersect(
3229 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
3231 return bset_from_bmap(isl_basic_map_intersect(bset_to_bmap(bset1
),
3232 bset_to_bmap(bset2
)));
3235 __isl_give isl_basic_set
*isl_basic_set_intersect_params(
3236 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
3238 return isl_basic_set_intersect(bset1
, bset2
);
3241 /* Special case of isl_map_intersect, where both map1 and map2
3242 * are convex, without any divs and such that either map1 or map2
3243 * contains a single constraint. This constraint is then simply
3244 * added to the other map.
3246 static __isl_give isl_map
*map_intersect_add_constraint(
3247 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
3249 isl_assert(map1
->ctx
, map1
->n
== 1, goto error
);
3250 isl_assert(map2
->ctx
, map1
->n
== 1, goto error
);
3251 isl_assert(map1
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
3252 isl_assert(map2
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
3254 if (map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
!= 1)
3255 return isl_map_intersect(map2
, map1
);
3257 map1
= isl_map_cow(map1
);
3260 if (isl_map_plain_is_empty(map1
)) {
3264 map1
->p
[0] = isl_basic_map_cow(map1
->p
[0]);
3265 if (map2
->p
[0]->n_eq
== 1)
3266 map1
->p
[0] = isl_basic_map_add_eq(map1
->p
[0], map2
->p
[0]->eq
[0]);
3268 map1
->p
[0] = isl_basic_map_add_ineq(map1
->p
[0],
3269 map2
->p
[0]->ineq
[0]);
3271 map1
->p
[0] = isl_basic_map_simplify(map1
->p
[0]);
3272 map1
->p
[0] = isl_basic_map_finalize(map1
->p
[0]);
3276 if (isl_basic_map_plain_is_empty(map1
->p
[0])) {
3277 isl_basic_map_free(map1
->p
[0]);
3290 /* map2 may be either a parameter domain or a map living in the same
3293 static __isl_give isl_map
*map_intersect_internal(__isl_take isl_map
*map1
,
3294 __isl_take isl_map
*map2
)
3303 if ((isl_map_plain_is_empty(map1
) ||
3304 isl_map_plain_is_universe(map2
)) &&
3305 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
3309 if ((isl_map_plain_is_empty(map2
) ||
3310 isl_map_plain_is_universe(map1
)) &&
3311 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
3316 if (map1
->n
== 1 && map2
->n
== 1 &&
3317 map1
->p
[0]->n_div
== 0 && map2
->p
[0]->n_div
== 0 &&
3318 isl_space_is_equal(map1
->dim
, map2
->dim
) &&
3319 (map1
->p
[0]->n_eq
+ map1
->p
[0]->n_ineq
== 1 ||
3320 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1))
3321 return map_intersect_add_constraint(map1
, map2
);
3323 if (isl_space_dim(map2
->dim
, isl_dim_all
) !=
3324 isl_space_dim(map2
->dim
, isl_dim_param
))
3325 isl_assert(map1
->ctx
,
3326 isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
3328 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
3329 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
3330 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
3332 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
3333 map1
->n
* map2
->n
, flags
);
3336 for (i
= 0; i
< map1
->n
; ++i
)
3337 for (j
= 0; j
< map2
->n
; ++j
) {
3338 struct isl_basic_map
*part
;
3339 part
= isl_basic_map_intersect(
3340 isl_basic_map_copy(map1
->p
[i
]),
3341 isl_basic_map_copy(map2
->p
[j
]));
3342 if (isl_basic_map_is_empty(part
) < 0)
3343 part
= isl_basic_map_free(part
);
3344 result
= isl_map_add_basic_map(result
, part
);
3357 static __isl_give isl_map
*map_intersect(__isl_take isl_map
*map1
,
3358 __isl_take isl_map
*map2
)
3362 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
3363 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
3364 "spaces don't match", goto error
);
3365 return map_intersect_internal(map1
, map2
);
3372 __isl_give isl_map
*isl_map_intersect(__isl_take isl_map
*map1
,
3373 __isl_take isl_map
*map2
)
3375 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect
);
3378 struct isl_set
*isl_set_intersect(struct isl_set
*set1
, struct isl_set
*set2
)
3380 return set_from_map(isl_map_intersect(set_to_map(set1
),
3384 /* map_intersect_internal accepts intersections
3385 * with parameter domains, so we can just call that function.
3387 static __isl_give isl_map
*map_intersect_params(__isl_take isl_map
*map
,
3388 __isl_take isl_set
*params
)
3390 return map_intersect_internal(map
, params
);
3393 __isl_give isl_map
*isl_map_intersect_params(__isl_take isl_map
*map1
,
3394 __isl_take isl_map
*map2
)
3396 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect_params
);
3399 __isl_give isl_set
*isl_set_intersect_params(__isl_take isl_set
*set
,
3400 __isl_take isl_set
*params
)
3402 return isl_map_intersect_params(set
, params
);
3405 struct isl_basic_map
*isl_basic_map_reverse(struct isl_basic_map
*bmap
)
3408 unsigned pos
, n1
, n2
;
3412 bmap
= isl_basic_map_cow(bmap
);
3415 space
= isl_space_reverse(isl_space_copy(bmap
->dim
));
3416 pos
= isl_basic_map_offset(bmap
, isl_dim_in
);
3417 n1
= isl_basic_map_dim(bmap
, isl_dim_in
);
3418 n2
= isl_basic_map_dim(bmap
, isl_dim_out
);
3419 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
3420 return isl_basic_map_reset_space(bmap
, space
);
3423 static __isl_give isl_basic_map
*basic_map_space_reset(
3424 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
)
3430 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
3433 space
= isl_basic_map_get_space(bmap
);
3434 space
= isl_space_reset(space
, type
);
3435 bmap
= isl_basic_map_reset_space(bmap
, space
);
3439 __isl_give isl_basic_map
*isl_basic_map_insert_dims(
3440 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
3441 unsigned pos
, unsigned n
)
3445 struct isl_basic_map
*res
;
3446 struct isl_dim_map
*dim_map
;
3447 unsigned total
, off
;
3448 enum isl_dim_type t
;
3451 return basic_map_space_reset(bmap
, type
);
3456 res_dim
= isl_space_insert_dims(isl_basic_map_get_space(bmap
), type
, pos
, n
);
3458 total
= isl_basic_map_total_dim(bmap
) + n
;
3459 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3461 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3463 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3465 unsigned size
= isl_basic_map_dim(bmap
, t
);
3466 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3468 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3469 pos
, size
- pos
, off
+ pos
+ n
);
3471 off
+= isl_space_dim(res_dim
, t
);
3473 isl_dim_map_div(dim_map
, bmap
, off
);
3475 res
= isl_basic_map_alloc_space(res_dim
,
3476 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3477 rational
= isl_basic_map_is_rational(bmap
);
3479 res
= isl_basic_map_free(res
);
3481 res
= isl_basic_map_set_rational(res
);
3482 if (isl_basic_map_plain_is_empty(bmap
)) {
3483 isl_basic_map_free(bmap
);
3485 return isl_basic_map_set_to_empty(res
);
3487 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3488 return isl_basic_map_finalize(res
);
3491 __isl_give isl_basic_set
*isl_basic_set_insert_dims(
3492 __isl_take isl_basic_set
*bset
,
3493 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3495 return isl_basic_map_insert_dims(bset
, type
, pos
, n
);
3498 __isl_give isl_basic_map
*isl_basic_map_add_dims(__isl_take isl_basic_map
*bmap
,
3499 enum isl_dim_type type
, unsigned n
)
3503 return isl_basic_map_insert_dims(bmap
, type
,
3504 isl_basic_map_dim(bmap
, type
), n
);
3507 __isl_give isl_basic_set
*isl_basic_set_add_dims(__isl_take isl_basic_set
*bset
,
3508 enum isl_dim_type type
, unsigned n
)
3512 isl_assert(bset
->ctx
, type
!= isl_dim_in
, goto error
);
3513 return isl_basic_map_add_dims(bset
, type
, n
);
3515 isl_basic_set_free(bset
);
3519 static __isl_give isl_map
*map_space_reset(__isl_take isl_map
*map
,
3520 enum isl_dim_type type
)
3524 if (!map
|| !isl_space_is_named_or_nested(map
->dim
, type
))
3527 space
= isl_map_get_space(map
);
3528 space
= isl_space_reset(space
, type
);
3529 map
= isl_map_reset_space(map
, space
);
3533 __isl_give isl_map
*isl_map_insert_dims(__isl_take isl_map
*map
,
3534 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3539 return map_space_reset(map
, type
);
3541 map
= isl_map_cow(map
);
3545 map
->dim
= isl_space_insert_dims(map
->dim
, type
, pos
, n
);
3549 for (i
= 0; i
< map
->n
; ++i
) {
3550 map
->p
[i
] = isl_basic_map_insert_dims(map
->p
[i
], type
, pos
, n
);
3561 __isl_give isl_set
*isl_set_insert_dims(__isl_take isl_set
*set
,
3562 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3564 return isl_map_insert_dims(set
, type
, pos
, n
);
3567 __isl_give isl_map
*isl_map_add_dims(__isl_take isl_map
*map
,
3568 enum isl_dim_type type
, unsigned n
)
3572 return isl_map_insert_dims(map
, type
, isl_map_dim(map
, type
), n
);
3575 __isl_give isl_set
*isl_set_add_dims(__isl_take isl_set
*set
,
3576 enum isl_dim_type type
, unsigned n
)
3580 isl_assert(set
->ctx
, type
!= isl_dim_in
, goto error
);
3581 return set_from_map(isl_map_add_dims(set_to_map(set
), type
, n
));
3587 __isl_give isl_basic_map
*isl_basic_map_move_dims(
3588 __isl_take isl_basic_map
*bmap
,
3589 enum isl_dim_type dst_type
, unsigned dst_pos
,
3590 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3592 struct isl_dim_map
*dim_map
;
3593 struct isl_basic_map
*res
;
3594 enum isl_dim_type t
;
3595 unsigned total
, off
;
3602 if (isl_basic_map_check_range(bmap
, src_type
, src_pos
, n
) < 0)
3603 return isl_basic_map_free(bmap
);
3605 if (dst_type
== src_type
&& dst_pos
== src_pos
)
3608 isl_assert(bmap
->ctx
, dst_type
!= src_type
, goto error
);
3610 if (pos(bmap
->dim
, dst_type
) + dst_pos
==
3611 pos(bmap
->dim
, src_type
) + src_pos
+
3612 ((src_type
< dst_type
) ? n
: 0)) {
3613 bmap
= isl_basic_map_cow(bmap
);
3617 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
3618 src_type
, src_pos
, n
);
3622 bmap
= isl_basic_map_finalize(bmap
);
3627 total
= isl_basic_map_total_dim(bmap
);
3628 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3631 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3632 unsigned size
= isl_space_dim(bmap
->dim
, t
);
3633 if (t
== dst_type
) {
3634 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3637 isl_dim_map_dim_range(dim_map
, bmap
->dim
, src_type
,
3640 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3641 dst_pos
, size
- dst_pos
, off
);
3642 off
+= size
- dst_pos
;
3643 } else if (t
== src_type
) {
3644 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3647 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3648 src_pos
+ n
, size
- src_pos
- n
, off
);
3649 off
+= size
- src_pos
- n
;
3651 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3655 isl_dim_map_div(dim_map
, bmap
, off
);
3657 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
3658 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3659 bmap
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3663 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
3664 src_type
, src_pos
, n
);
3668 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
3669 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3670 bmap
= isl_basic_map_finalize(bmap
);
3674 isl_basic_map_free(bmap
);
3678 __isl_give isl_basic_set
*isl_basic_set_move_dims(__isl_take isl_basic_set
*bset
,
3679 enum isl_dim_type dst_type
, unsigned dst_pos
,
3680 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3682 isl_basic_map
*bmap
= bset_to_bmap(bset
);
3683 bmap
= isl_basic_map_move_dims(bmap
, dst_type
, dst_pos
,
3684 src_type
, src_pos
, n
);
3685 return bset_from_bmap(bmap
);
3688 __isl_give isl_set
*isl_set_move_dims(__isl_take isl_set
*set
,
3689 enum isl_dim_type dst_type
, unsigned dst_pos
,
3690 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3694 isl_assert(set
->ctx
, dst_type
!= isl_dim_in
, goto error
);
3695 return set_from_map(isl_map_move_dims(set_to_map(set
),
3696 dst_type
, dst_pos
, src_type
, src_pos
, n
));
3702 __isl_give isl_map
*isl_map_move_dims(__isl_take isl_map
*map
,
3703 enum isl_dim_type dst_type
, unsigned dst_pos
,
3704 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3713 isl_assert(map
->ctx
, src_pos
+ n
<= isl_map_dim(map
, src_type
),
3716 if (dst_type
== src_type
&& dst_pos
== src_pos
)
3719 isl_assert(map
->ctx
, dst_type
!= src_type
, goto error
);
3721 map
= isl_map_cow(map
);
3725 map
->dim
= isl_space_move_dims(map
->dim
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
3729 for (i
= 0; i
< map
->n
; ++i
) {
3730 map
->p
[i
] = isl_basic_map_move_dims(map
->p
[i
],
3732 src_type
, src_pos
, n
);
3743 /* Move the specified dimensions to the last columns right before
3744 * the divs. Don't change the dimension specification of bmap.
3745 * That's the responsibility of the caller.
3747 static __isl_give isl_basic_map
*move_last(__isl_take isl_basic_map
*bmap
,
3748 enum isl_dim_type type
, unsigned first
, unsigned n
)
3750 struct isl_dim_map
*dim_map
;
3751 struct isl_basic_map
*res
;
3752 enum isl_dim_type t
;
3753 unsigned total
, off
;
3757 if (pos(bmap
->dim
, type
) + first
+ n
==
3758 1 + isl_space_dim(bmap
->dim
, isl_dim_all
))
3761 total
= isl_basic_map_total_dim(bmap
);
3762 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3765 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3766 unsigned size
= isl_space_dim(bmap
->dim
, t
);
3768 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3771 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3772 first
, n
, total
- bmap
->n_div
- n
);
3773 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3774 first
+ n
, size
- (first
+ n
), off
);
3775 off
+= size
- (first
+ n
);
3777 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3781 isl_dim_map_div(dim_map
, bmap
, off
+ n
);
3783 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
3784 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3785 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3789 /* Insert "n" rows in the divs of "bmap".
3791 * The number of columns is not changed, which means that the last
3792 * dimensions of "bmap" are being reintepreted as the new divs.
3793 * The space of "bmap" is not adjusted, however, which means
3794 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
3795 * from the space of "bmap" is the responsibility of the caller.
3797 static __isl_give isl_basic_map
*insert_div_rows(__isl_take isl_basic_map
*bmap
,
3805 bmap
= isl_basic_map_cow(bmap
);
3809 row_size
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + bmap
->extra
;
3810 old
= bmap
->block2
.data
;
3811 bmap
->block2
= isl_blk_extend(bmap
->ctx
, bmap
->block2
,
3812 (bmap
->extra
+ n
) * (1 + row_size
));
3813 if (!bmap
->block2
.data
)
3814 return isl_basic_map_free(bmap
);
3815 new_div
= isl_alloc_array(bmap
->ctx
, isl_int
*, bmap
->extra
+ n
);
3817 return isl_basic_map_free(bmap
);
3818 for (i
= 0; i
< n
; ++i
) {
3819 new_div
[i
] = bmap
->block2
.data
+
3820 (bmap
->extra
+ i
) * (1 + row_size
);
3821 isl_seq_clr(new_div
[i
], 1 + row_size
);
3823 for (i
= 0; i
< bmap
->extra
; ++i
)
3824 new_div
[n
+ i
] = bmap
->block2
.data
+ (bmap
->div
[i
] - old
);
3826 bmap
->div
= new_div
;
3833 /* Drop constraints from "bmap" that only involve the variables
3834 * of "type" in the range [first, first + n] that are not related
3835 * to any of the variables outside that interval.
3836 * These constraints cannot influence the values for the variables
3837 * outside the interval, except in case they cause "bmap" to be empty.
3838 * Only drop the constraints if "bmap" is known to be non-empty.
3840 static __isl_give isl_basic_map
*drop_irrelevant_constraints(
3841 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
3842 unsigned first
, unsigned n
)
3846 unsigned dim
, n_div
;
3849 non_empty
= isl_basic_map_plain_is_non_empty(bmap
);
3851 return isl_basic_map_free(bmap
);
3855 dim
= isl_basic_map_dim(bmap
, isl_dim_all
);
3856 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
3857 groups
= isl_calloc_array(isl_basic_map_get_ctx(bmap
), int, dim
);
3859 return isl_basic_map_free(bmap
);
3860 first
+= isl_basic_map_offset(bmap
, type
) - 1;
3861 for (i
= 0; i
< first
; ++i
)
3863 for (i
= first
+ n
; i
< dim
- n_div
; ++i
)
3866 bmap
= isl_basic_map_drop_unrelated_constraints(bmap
, groups
);
3871 /* Turn the n dimensions of type type, starting at first
3872 * into existentially quantified variables.
3874 * If a subset of the projected out variables are unrelated
3875 * to any of the variables that remain, then the constraints
3876 * involving this subset are simply dropped first.
3878 __isl_give isl_basic_map
*isl_basic_map_project_out(
3879 __isl_take isl_basic_map
*bmap
,
3880 enum isl_dim_type type
, unsigned first
, unsigned n
)
3883 return basic_map_space_reset(bmap
, type
);
3884 if (type
== isl_dim_div
)
3885 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
3886 "cannot project out existentially quantified variables",
3887 return isl_basic_map_free(bmap
));
3889 bmap
= drop_irrelevant_constraints(bmap
, type
, first
, n
);
3893 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
3894 return isl_basic_map_remove_dims(bmap
, type
, first
, n
);
3896 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
3897 return isl_basic_map_free(bmap
);
3899 bmap
= move_last(bmap
, type
, first
, n
);
3900 bmap
= isl_basic_map_cow(bmap
);
3901 bmap
= insert_div_rows(bmap
, n
);
3905 bmap
->dim
= isl_space_drop_dims(bmap
->dim
, type
, first
, n
);
3908 bmap
= isl_basic_map_simplify(bmap
);
3909 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
3910 return isl_basic_map_finalize(bmap
);
3912 isl_basic_map_free(bmap
);
3916 /* Turn the n dimensions of type type, starting at first
3917 * into existentially quantified variables.
3919 struct isl_basic_set
*isl_basic_set_project_out(struct isl_basic_set
*bset
,
3920 enum isl_dim_type type
, unsigned first
, unsigned n
)
3922 return bset_from_bmap(isl_basic_map_project_out(bset_to_bmap(bset
),
3926 /* Turn the n dimensions of type type, starting at first
3927 * into existentially quantified variables.
3929 __isl_give isl_map
*isl_map_project_out(__isl_take isl_map
*map
,
3930 enum isl_dim_type type
, unsigned first
, unsigned n
)
3938 return map_space_reset(map
, type
);
3940 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
3942 map
= isl_map_cow(map
);
3946 map
->dim
= isl_space_drop_dims(map
->dim
, type
, first
, n
);
3950 for (i
= 0; i
< map
->n
; ++i
) {
3951 map
->p
[i
] = isl_basic_map_project_out(map
->p
[i
], type
, first
, n
);
3962 /* Turn the n dimensions of type type, starting at first
3963 * into existentially quantified variables.
3965 __isl_give isl_set
*isl_set_project_out(__isl_take isl_set
*set
,
3966 enum isl_dim_type type
, unsigned first
, unsigned n
)
3968 return set_from_map(isl_map_project_out(set_to_map(set
),
3972 /* Return a map that projects the elements in "set" onto their
3973 * "n" set dimensions starting at "first".
3974 * "type" should be equal to isl_dim_set.
3976 __isl_give isl_map
*isl_set_project_onto_map(__isl_take isl_set
*set
,
3977 enum isl_dim_type type
, unsigned first
, unsigned n
)
3985 if (type
!= isl_dim_set
)
3986 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
3987 "only set dimensions can be projected out", goto error
);
3988 dim
= isl_set_dim(set
, isl_dim_set
);
3989 if (first
+ n
> dim
|| first
+ n
< first
)
3990 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
3991 "index out of bounds", goto error
);
3993 map
= isl_map_from_domain(set
);
3994 map
= isl_map_add_dims(map
, isl_dim_out
, n
);
3995 for (i
= 0; i
< n
; ++i
)
3996 map
= isl_map_equate(map
, isl_dim_in
, first
+ i
,
4004 static struct isl_basic_map
*add_divs(struct isl_basic_map
*bmap
, unsigned n
)
4008 for (i
= 0; i
< n
; ++i
) {
4009 j
= isl_basic_map_alloc_div(bmap
);
4012 isl_seq_clr(bmap
->div
[j
], 1+1+isl_basic_map_total_dim(bmap
));
4016 isl_basic_map_free(bmap
);
4020 struct isl_basic_map
*isl_basic_map_apply_range(
4021 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
4023 isl_space
*dim_result
= NULL
;
4024 struct isl_basic_map
*bmap
;
4025 unsigned n_in
, n_out
, n
, nparam
, total
, pos
;
4026 struct isl_dim_map
*dim_map1
, *dim_map2
;
4028 if (!bmap1
|| !bmap2
)
4030 if (!isl_space_match(bmap1
->dim
, isl_dim_param
,
4031 bmap2
->dim
, isl_dim_param
))
4032 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
4033 "parameters don't match", goto error
);
4034 if (!isl_space_tuple_is_equal(bmap1
->dim
, isl_dim_out
,
4035 bmap2
->dim
, isl_dim_in
))
4036 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
4037 "spaces don't match", goto error
);
4039 dim_result
= isl_space_join(isl_space_copy(bmap1
->dim
),
4040 isl_space_copy(bmap2
->dim
));
4042 n_in
= isl_basic_map_n_in(bmap1
);
4043 n_out
= isl_basic_map_n_out(bmap2
);
4044 n
= isl_basic_map_n_out(bmap1
);
4045 nparam
= isl_basic_map_n_param(bmap1
);
4047 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ n
;
4048 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4049 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4050 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
4051 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
4052 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
4053 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_in
);
4054 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_out
);
4055 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
4056 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
4057 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
4059 bmap
= isl_basic_map_alloc_space(dim_result
,
4060 bmap1
->n_div
+ bmap2
->n_div
+ n
,
4061 bmap1
->n_eq
+ bmap2
->n_eq
,
4062 bmap1
->n_ineq
+ bmap2
->n_ineq
);
4063 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
4064 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
4065 bmap
= add_divs(bmap
, n
);
4066 bmap
= isl_basic_map_simplify(bmap
);
4067 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
4068 return isl_basic_map_finalize(bmap
);
4070 isl_basic_map_free(bmap1
);
4071 isl_basic_map_free(bmap2
);
4075 struct isl_basic_set
*isl_basic_set_apply(
4076 struct isl_basic_set
*bset
, struct isl_basic_map
*bmap
)
4081 isl_assert(bset
->ctx
, isl_basic_map_compatible_domain(bmap
, bset
),
4084 return bset_from_bmap(isl_basic_map_apply_range(bset_to_bmap(bset
),
4087 isl_basic_set_free(bset
);
4088 isl_basic_map_free(bmap
);
4092 struct isl_basic_map
*isl_basic_map_apply_domain(
4093 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
4095 if (!bmap1
|| !bmap2
)
4098 if (!isl_space_match(bmap1
->dim
, isl_dim_param
,
4099 bmap2
->dim
, isl_dim_param
))
4100 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
4101 "parameters don't match", goto error
);
4102 if (!isl_space_tuple_is_equal(bmap1
->dim
, isl_dim_in
,
4103 bmap2
->dim
, isl_dim_in
))
4104 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
4105 "spaces don't match", goto error
);
4107 bmap1
= isl_basic_map_reverse(bmap1
);
4108 bmap1
= isl_basic_map_apply_range(bmap1
, bmap2
);
4109 return isl_basic_map_reverse(bmap1
);
4111 isl_basic_map_free(bmap1
);
4112 isl_basic_map_free(bmap2
);
4116 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
4117 * A \cap B -> f(A) + f(B)
4119 struct isl_basic_map
*isl_basic_map_sum(
4120 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
4122 unsigned n_in
, n_out
, nparam
, total
, pos
;
4123 struct isl_basic_map
*bmap
= NULL
;
4124 struct isl_dim_map
*dim_map1
, *dim_map2
;
4127 if (!bmap1
|| !bmap2
)
4130 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
),
4133 nparam
= isl_basic_map_n_param(bmap1
);
4134 n_in
= isl_basic_map_n_in(bmap1
);
4135 n_out
= isl_basic_map_n_out(bmap1
);
4137 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
;
4138 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4139 dim_map2
= isl_dim_map_alloc(bmap2
->ctx
, total
);
4140 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
4141 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
);
4142 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
4143 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
4144 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_in
+ n_out
);
4145 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
4146 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
4147 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_out
);
4149 bmap
= isl_basic_map_alloc_space(isl_space_copy(bmap1
->dim
),
4150 bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
,
4151 bmap1
->n_eq
+ bmap2
->n_eq
+ n_out
,
4152 bmap1
->n_ineq
+ bmap2
->n_ineq
);
4153 for (i
= 0; i
< n_out
; ++i
) {
4154 int j
= isl_basic_map_alloc_equality(bmap
);
4157 isl_seq_clr(bmap
->eq
[j
], 1+total
);
4158 isl_int_set_si(bmap
->eq
[j
][1+nparam
+n_in
+i
], -1);
4159 isl_int_set_si(bmap
->eq
[j
][1+pos
+i
], 1);
4160 isl_int_set_si(bmap
->eq
[j
][1+pos
-n_out
+i
], 1);
4162 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
4163 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
4164 bmap
= add_divs(bmap
, 2 * n_out
);
4166 bmap
= isl_basic_map_simplify(bmap
);
4167 return isl_basic_map_finalize(bmap
);
4169 isl_basic_map_free(bmap
);
4170 isl_basic_map_free(bmap1
);
4171 isl_basic_map_free(bmap2
);
4175 /* Given two maps A -> f(A) and B -> g(B), construct a map
4176 * A \cap B -> f(A) + f(B)
4178 struct isl_map
*isl_map_sum(struct isl_map
*map1
, struct isl_map
*map2
)
4180 struct isl_map
*result
;
4186 isl_assert(map1
->ctx
, isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
4188 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
4189 map1
->n
* map2
->n
, 0);
4192 for (i
= 0; i
< map1
->n
; ++i
)
4193 for (j
= 0; j
< map2
->n
; ++j
) {
4194 struct isl_basic_map
*part
;
4195 part
= isl_basic_map_sum(
4196 isl_basic_map_copy(map1
->p
[i
]),
4197 isl_basic_map_copy(map2
->p
[j
]));
4198 if (isl_basic_map_is_empty(part
))
4199 isl_basic_map_free(part
);
4201 result
= isl_map_add_basic_map(result
, part
);
4214 __isl_give isl_set
*isl_set_sum(__isl_take isl_set
*set1
,
4215 __isl_take isl_set
*set2
)
4217 return set_from_map(isl_map_sum(set_to_map(set1
), set_to_map(set2
)));
4220 /* Given a basic map A -> f(A), construct A -> -f(A).
4222 struct isl_basic_map
*isl_basic_map_neg(struct isl_basic_map
*bmap
)
4227 bmap
= isl_basic_map_cow(bmap
);
4231 n
= isl_basic_map_dim(bmap
, isl_dim_out
);
4232 off
= isl_basic_map_offset(bmap
, isl_dim_out
);
4233 for (i
= 0; i
< bmap
->n_eq
; ++i
)
4234 for (j
= 0; j
< n
; ++j
)
4235 isl_int_neg(bmap
->eq
[i
][off
+j
], bmap
->eq
[i
][off
+j
]);
4236 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
4237 for (j
= 0; j
< n
; ++j
)
4238 isl_int_neg(bmap
->ineq
[i
][off
+j
], bmap
->ineq
[i
][off
+j
]);
4239 for (i
= 0; i
< bmap
->n_div
; ++i
)
4240 for (j
= 0; j
< n
; ++j
)
4241 isl_int_neg(bmap
->div
[i
][1+off
+j
], bmap
->div
[i
][1+off
+j
]);
4242 bmap
= isl_basic_map_gauss(bmap
, NULL
);
4243 return isl_basic_map_finalize(bmap
);
4246 __isl_give isl_basic_set
*isl_basic_set_neg(__isl_take isl_basic_set
*bset
)
4248 return isl_basic_map_neg(bset
);
4251 /* Given a map A -> f(A), construct A -> -f(A).
4253 struct isl_map
*isl_map_neg(struct isl_map
*map
)
4257 map
= isl_map_cow(map
);
4261 for (i
= 0; i
< map
->n
; ++i
) {
4262 map
->p
[i
] = isl_basic_map_neg(map
->p
[i
]);
4273 __isl_give isl_set
*isl_set_neg(__isl_take isl_set
*set
)
4275 return set_from_map(isl_map_neg(set_to_map(set
)));
4278 /* Given a basic map A -> f(A) and an integer d, construct a basic map
4279 * A -> floor(f(A)/d).
4281 struct isl_basic_map
*isl_basic_map_floordiv(struct isl_basic_map
*bmap
,
4284 unsigned n_in
, n_out
, nparam
, total
, pos
;
4285 struct isl_basic_map
*result
= NULL
;
4286 struct isl_dim_map
*dim_map
;
4292 nparam
= isl_basic_map_n_param(bmap
);
4293 n_in
= isl_basic_map_n_in(bmap
);
4294 n_out
= isl_basic_map_n_out(bmap
);
4296 total
= nparam
+ n_in
+ n_out
+ bmap
->n_div
+ n_out
;
4297 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
4298 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_param
, pos
= 0);
4299 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_in
, pos
+= nparam
);
4300 isl_dim_map_div(dim_map
, bmap
, pos
+= n_in
+ n_out
);
4301 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_out
, pos
+= bmap
->n_div
);
4303 result
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
4304 bmap
->n_div
+ n_out
,
4305 bmap
->n_eq
, bmap
->n_ineq
+ 2 * n_out
);
4306 result
= isl_basic_map_add_constraints_dim_map(result
, bmap
, dim_map
);
4307 result
= add_divs(result
, n_out
);
4308 for (i
= 0; i
< n_out
; ++i
) {
4310 j
= isl_basic_map_alloc_inequality(result
);
4313 isl_seq_clr(result
->ineq
[j
], 1+total
);
4314 isl_int_neg(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
4315 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], 1);
4316 j
= isl_basic_map_alloc_inequality(result
);
4319 isl_seq_clr(result
->ineq
[j
], 1+total
);
4320 isl_int_set(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
4321 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], -1);
4322 isl_int_sub_ui(result
->ineq
[j
][0], d
, 1);
4325 result
= isl_basic_map_simplify(result
);
4326 return isl_basic_map_finalize(result
);
4328 isl_basic_map_free(result
);
4332 /* Given a map A -> f(A) and an integer d, construct a map
4333 * A -> floor(f(A)/d).
4335 struct isl_map
*isl_map_floordiv(struct isl_map
*map
, isl_int d
)
4339 map
= isl_map_cow(map
);
4343 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
4344 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4345 for (i
= 0; i
< map
->n
; ++i
) {
4346 map
->p
[i
] = isl_basic_map_floordiv(map
->p
[i
], d
);
4357 /* Given a map A -> f(A) and an integer d, construct a map
4358 * A -> floor(f(A)/d).
4360 __isl_give isl_map
*isl_map_floordiv_val(__isl_take isl_map
*map
,
4361 __isl_take isl_val
*d
)
4365 if (!isl_val_is_int(d
))
4366 isl_die(isl_val_get_ctx(d
), isl_error_invalid
,
4367 "expecting integer denominator", goto error
);
4368 map
= isl_map_floordiv(map
, d
->n
);
4377 static struct isl_basic_map
*var_equal(struct isl_basic_map
*bmap
, unsigned pos
)
4383 i
= isl_basic_map_alloc_equality(bmap
);
4386 nparam
= isl_basic_map_n_param(bmap
);
4387 n_in
= isl_basic_map_n_in(bmap
);
4388 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4389 isl_int_set_si(bmap
->eq
[i
][1+nparam
+pos
], -1);
4390 isl_int_set_si(bmap
->eq
[i
][1+nparam
+n_in
+pos
], 1);
4391 return isl_basic_map_finalize(bmap
);
4393 isl_basic_map_free(bmap
);
4397 /* Add a constraint to "bmap" expressing i_pos < o_pos
4399 static struct isl_basic_map
*var_less(struct isl_basic_map
*bmap
, unsigned pos
)
4405 i
= isl_basic_map_alloc_inequality(bmap
);
4408 nparam
= isl_basic_map_n_param(bmap
);
4409 n_in
= isl_basic_map_n_in(bmap
);
4410 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4411 isl_int_set_si(bmap
->ineq
[i
][0], -1);
4412 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
4413 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
4414 return isl_basic_map_finalize(bmap
);
4416 isl_basic_map_free(bmap
);
4420 /* Add a constraint to "bmap" expressing i_pos <= o_pos
4422 static __isl_give isl_basic_map
*var_less_or_equal(
4423 __isl_take isl_basic_map
*bmap
, unsigned pos
)
4429 i
= isl_basic_map_alloc_inequality(bmap
);
4432 nparam
= isl_basic_map_n_param(bmap
);
4433 n_in
= isl_basic_map_n_in(bmap
);
4434 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4435 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
4436 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
4437 return isl_basic_map_finalize(bmap
);
4439 isl_basic_map_free(bmap
);
4443 /* Add a constraint to "bmap" expressing i_pos > o_pos
4445 static struct isl_basic_map
*var_more(struct isl_basic_map
*bmap
, unsigned pos
)
4451 i
= isl_basic_map_alloc_inequality(bmap
);
4454 nparam
= isl_basic_map_n_param(bmap
);
4455 n_in
= isl_basic_map_n_in(bmap
);
4456 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4457 isl_int_set_si(bmap
->ineq
[i
][0], -1);
4458 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
4459 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
4460 return isl_basic_map_finalize(bmap
);
4462 isl_basic_map_free(bmap
);
4466 /* Add a constraint to "bmap" expressing i_pos >= o_pos
4468 static __isl_give isl_basic_map
*var_more_or_equal(
4469 __isl_take isl_basic_map
*bmap
, unsigned pos
)
4475 i
= isl_basic_map_alloc_inequality(bmap
);
4478 nparam
= isl_basic_map_n_param(bmap
);
4479 n_in
= isl_basic_map_n_in(bmap
);
4480 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4481 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
4482 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
4483 return isl_basic_map_finalize(bmap
);
4485 isl_basic_map_free(bmap
);
4489 __isl_give isl_basic_map
*isl_basic_map_equal(
4490 __isl_take isl_space
*dim
, unsigned n_equal
)
4493 struct isl_basic_map
*bmap
;
4494 bmap
= isl_basic_map_alloc_space(dim
, 0, n_equal
, 0);
4497 for (i
= 0; i
< n_equal
&& bmap
; ++i
)
4498 bmap
= var_equal(bmap
, i
);
4499 return isl_basic_map_finalize(bmap
);
4502 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
4504 __isl_give isl_basic_map
*isl_basic_map_less_at(__isl_take isl_space
*dim
,
4508 struct isl_basic_map
*bmap
;
4509 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4512 for (i
= 0; i
< pos
&& bmap
; ++i
)
4513 bmap
= var_equal(bmap
, i
);
4515 bmap
= var_less(bmap
, pos
);
4516 return isl_basic_map_finalize(bmap
);
4519 /* Return a relation on "dim" expressing i_[0..pos] <<= o_[0..pos]
4521 __isl_give isl_basic_map
*isl_basic_map_less_or_equal_at(
4522 __isl_take isl_space
*dim
, unsigned pos
)
4525 isl_basic_map
*bmap
;
4527 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4528 for (i
= 0; i
< pos
; ++i
)
4529 bmap
= var_equal(bmap
, i
);
4530 bmap
= var_less_or_equal(bmap
, pos
);
4531 return isl_basic_map_finalize(bmap
);
4534 /* Return a relation on "dim" expressing i_pos > o_pos
4536 __isl_give isl_basic_map
*isl_basic_map_more_at(__isl_take isl_space
*dim
,
4540 struct isl_basic_map
*bmap
;
4541 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4544 for (i
= 0; i
< pos
&& bmap
; ++i
)
4545 bmap
= var_equal(bmap
, i
);
4547 bmap
= var_more(bmap
, pos
);
4548 return isl_basic_map_finalize(bmap
);
4551 /* Return a relation on "dim" expressing i_[0..pos] >>= o_[0..pos]
4553 __isl_give isl_basic_map
*isl_basic_map_more_or_equal_at(
4554 __isl_take isl_space
*dim
, unsigned pos
)
4557 isl_basic_map
*bmap
;
4559 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4560 for (i
= 0; i
< pos
; ++i
)
4561 bmap
= var_equal(bmap
, i
);
4562 bmap
= var_more_or_equal(bmap
, pos
);
4563 return isl_basic_map_finalize(bmap
);
4566 static __isl_give isl_map
*map_lex_lte_first(__isl_take isl_space
*dims
,
4567 unsigned n
, int equal
)
4569 struct isl_map
*map
;
4572 if (n
== 0 && equal
)
4573 return isl_map_universe(dims
);
4575 map
= isl_map_alloc_space(isl_space_copy(dims
), n
, ISL_MAP_DISJOINT
);
4577 for (i
= 0; i
+ 1 < n
; ++i
)
4578 map
= isl_map_add_basic_map(map
,
4579 isl_basic_map_less_at(isl_space_copy(dims
), i
));
4582 map
= isl_map_add_basic_map(map
,
4583 isl_basic_map_less_or_equal_at(dims
, n
- 1));
4585 map
= isl_map_add_basic_map(map
,
4586 isl_basic_map_less_at(dims
, n
- 1));
4588 isl_space_free(dims
);
4593 static __isl_give isl_map
*map_lex_lte(__isl_take isl_space
*dims
, int equal
)
4597 return map_lex_lte_first(dims
, dims
->n_out
, equal
);
4600 __isl_give isl_map
*isl_map_lex_lt_first(__isl_take isl_space
*dim
, unsigned n
)
4602 return map_lex_lte_first(dim
, n
, 0);
4605 __isl_give isl_map
*isl_map_lex_le_first(__isl_take isl_space
*dim
, unsigned n
)
4607 return map_lex_lte_first(dim
, n
, 1);
4610 __isl_give isl_map
*isl_map_lex_lt(__isl_take isl_space
*set_dim
)
4612 return map_lex_lte(isl_space_map_from_set(set_dim
), 0);
4615 __isl_give isl_map
*isl_map_lex_le(__isl_take isl_space
*set_dim
)
4617 return map_lex_lte(isl_space_map_from_set(set_dim
), 1);
4620 static __isl_give isl_map
*map_lex_gte_first(__isl_take isl_space
*dims
,
4621 unsigned n
, int equal
)
4623 struct isl_map
*map
;
4626 if (n
== 0 && equal
)
4627 return isl_map_universe(dims
);
4629 map
= isl_map_alloc_space(isl_space_copy(dims
), n
, ISL_MAP_DISJOINT
);
4631 for (i
= 0; i
+ 1 < n
; ++i
)
4632 map
= isl_map_add_basic_map(map
,
4633 isl_basic_map_more_at(isl_space_copy(dims
), i
));
4636 map
= isl_map_add_basic_map(map
,
4637 isl_basic_map_more_or_equal_at(dims
, n
- 1));
4639 map
= isl_map_add_basic_map(map
,
4640 isl_basic_map_more_at(dims
, n
- 1));
4642 isl_space_free(dims
);
4647 static __isl_give isl_map
*map_lex_gte(__isl_take isl_space
*dims
, int equal
)
4651 return map_lex_gte_first(dims
, dims
->n_out
, equal
);
4654 __isl_give isl_map
*isl_map_lex_gt_first(__isl_take isl_space
*dim
, unsigned n
)
4656 return map_lex_gte_first(dim
, n
, 0);
4659 __isl_give isl_map
*isl_map_lex_ge_first(__isl_take isl_space
*dim
, unsigned n
)
4661 return map_lex_gte_first(dim
, n
, 1);
4664 __isl_give isl_map
*isl_map_lex_gt(__isl_take isl_space
*set_dim
)
4666 return map_lex_gte(isl_space_map_from_set(set_dim
), 0);
4669 __isl_give isl_map
*isl_map_lex_ge(__isl_take isl_space
*set_dim
)
4671 return map_lex_gte(isl_space_map_from_set(set_dim
), 1);
4674 __isl_give isl_map
*isl_set_lex_le_set(__isl_take isl_set
*set1
,
4675 __isl_take isl_set
*set2
)
4678 map
= isl_map_lex_le(isl_set_get_space(set1
));
4679 map
= isl_map_intersect_domain(map
, set1
);
4680 map
= isl_map_intersect_range(map
, set2
);
4684 __isl_give isl_map
*isl_set_lex_lt_set(__isl_take isl_set
*set1
,
4685 __isl_take isl_set
*set2
)
4688 map
= isl_map_lex_lt(isl_set_get_space(set1
));
4689 map
= isl_map_intersect_domain(map
, set1
);
4690 map
= isl_map_intersect_range(map
, set2
);
4694 __isl_give isl_map
*isl_set_lex_ge_set(__isl_take isl_set
*set1
,
4695 __isl_take isl_set
*set2
)
4698 map
= isl_map_lex_ge(isl_set_get_space(set1
));
4699 map
= isl_map_intersect_domain(map
, set1
);
4700 map
= isl_map_intersect_range(map
, set2
);
4704 __isl_give isl_map
*isl_set_lex_gt_set(__isl_take isl_set
*set1
,
4705 __isl_take isl_set
*set2
)
4708 map
= isl_map_lex_gt(isl_set_get_space(set1
));
4709 map
= isl_map_intersect_domain(map
, set1
);
4710 map
= isl_map_intersect_range(map
, set2
);
4714 __isl_give isl_map
*isl_map_lex_le_map(__isl_take isl_map
*map1
,
4715 __isl_take isl_map
*map2
)
4718 map
= isl_map_lex_le(isl_space_range(isl_map_get_space(map1
)));
4719 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4720 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4724 __isl_give isl_map
*isl_map_lex_lt_map(__isl_take isl_map
*map1
,
4725 __isl_take isl_map
*map2
)
4728 map
= isl_map_lex_lt(isl_space_range(isl_map_get_space(map1
)));
4729 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4730 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4734 __isl_give isl_map
*isl_map_lex_ge_map(__isl_take isl_map
*map1
,
4735 __isl_take isl_map
*map2
)
4738 map
= isl_map_lex_ge(isl_space_range(isl_map_get_space(map1
)));
4739 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4740 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4744 __isl_give isl_map
*isl_map_lex_gt_map(__isl_take isl_map
*map1
,
4745 __isl_take isl_map
*map2
)
4748 map
= isl_map_lex_gt(isl_space_range(isl_map_get_space(map1
)));
4749 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4750 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4754 static __isl_give isl_basic_map
*basic_map_from_basic_set(
4755 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*dim
)
4757 struct isl_basic_map
*bmap
;
4759 bset
= isl_basic_set_cow(bset
);
4763 isl_assert(bset
->ctx
, isl_space_compatible_internal(bset
->dim
, dim
),
4765 isl_space_free(bset
->dim
);
4766 bmap
= bset_to_bmap(bset
);
4768 return isl_basic_map_finalize(bmap
);
4770 isl_basic_set_free(bset
);
4771 isl_space_free(dim
);
4775 __isl_give isl_basic_map
*isl_basic_map_from_basic_set(
4776 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*space
)
4778 return basic_map_from_basic_set(bset
, space
);
4781 /* For a div d = floor(f/m), add the constraint
4785 static isl_stat
add_upper_div_constraint(__isl_keep isl_basic_map
*bmap
,
4786 unsigned pos
, isl_int
*div
)
4789 unsigned total
= isl_basic_map_total_dim(bmap
);
4791 i
= isl_basic_map_alloc_inequality(bmap
);
4793 return isl_stat_error
;
4794 isl_seq_cpy(bmap
->ineq
[i
], div
+ 1, 1 + total
);
4795 isl_int_neg(bmap
->ineq
[i
][1 + pos
], div
[0]);
4800 /* For a div d = floor(f/m), add the constraint
4802 * -(f-(m-1)) + m d >= 0
4804 static isl_stat
add_lower_div_constraint(__isl_keep isl_basic_map
*bmap
,
4805 unsigned pos
, isl_int
*div
)
4808 unsigned total
= isl_basic_map_total_dim(bmap
);
4810 i
= isl_basic_map_alloc_inequality(bmap
);
4812 return isl_stat_error
;
4813 isl_seq_neg(bmap
->ineq
[i
], div
+ 1, 1 + total
);
4814 isl_int_set(bmap
->ineq
[i
][1 + pos
], div
[0]);
4815 isl_int_add(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], bmap
->ineq
[i
][1 + pos
]);
4816 isl_int_sub_ui(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], 1);
4821 /* For a div d = floor(f/m), add the constraints
4824 * -(f-(m-1)) + m d >= 0
4826 * Note that the second constraint is the negation of
4830 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map
*bmap
,
4831 unsigned pos
, isl_int
*div
)
4833 if (add_upper_div_constraint(bmap
, pos
, div
) < 0)
4835 if (add_lower_div_constraint(bmap
, pos
, div
) < 0)
4840 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set
*bset
,
4841 unsigned pos
, isl_int
*div
)
4843 return isl_basic_map_add_div_constraints_var(bset_to_bmap(bset
),
4847 int isl_basic_map_add_div_constraints(struct isl_basic_map
*bmap
, unsigned div
)
4849 unsigned total
= isl_basic_map_total_dim(bmap
);
4850 unsigned div_pos
= total
- bmap
->n_div
+ div
;
4852 return isl_basic_map_add_div_constraints_var(bmap
, div_pos
,
4856 /* For each known div d = floor(f/m), add the constraints
4859 * -(f-(m-1)) + m d >= 0
4861 * Remove duplicate constraints in case of some these div constraints
4862 * already appear in "bmap".
4864 __isl_give isl_basic_map
*isl_basic_map_add_known_div_constraints(
4865 __isl_take isl_basic_map
*bmap
)
4871 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4875 bmap
= add_known_div_constraints(bmap
);
4876 bmap
= isl_basic_map_remove_duplicate_constraints(bmap
, NULL
, 0);
4877 bmap
= isl_basic_map_finalize(bmap
);
4881 /* Add the div constraint of sign "sign" for div "div" of "bmap".
4883 * In particular, if this div is of the form d = floor(f/m),
4884 * then add the constraint
4888 * if sign < 0 or the constraint
4890 * -(f-(m-1)) + m d >= 0
4894 int isl_basic_map_add_div_constraint(__isl_keep isl_basic_map
*bmap
,
4895 unsigned div
, int sign
)
4903 total
= isl_basic_map_total_dim(bmap
);
4904 div_pos
= total
- bmap
->n_div
+ div
;
4907 return add_upper_div_constraint(bmap
, div_pos
, bmap
->div
[div
]);
4909 return add_lower_div_constraint(bmap
, div_pos
, bmap
->div
[div
]);
4912 struct isl_basic_set
*isl_basic_map_underlying_set(
4913 struct isl_basic_map
*bmap
)
4917 if (bmap
->dim
->nparam
== 0 && bmap
->dim
->n_in
== 0 &&
4919 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_in
) &&
4920 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_out
))
4921 return bset_from_bmap(bmap
);
4922 bmap
= isl_basic_map_cow(bmap
);
4925 bmap
->dim
= isl_space_underlying(bmap
->dim
, bmap
->n_div
);
4928 bmap
->extra
-= bmap
->n_div
;
4930 bmap
= isl_basic_map_finalize(bmap
);
4931 return bset_from_bmap(bmap
);
4933 isl_basic_map_free(bmap
);
4937 __isl_give isl_basic_set
*isl_basic_set_underlying_set(
4938 __isl_take isl_basic_set
*bset
)
4940 return isl_basic_map_underlying_set(bset_to_bmap(bset
));
4943 /* Replace each element in "list" by the result of applying
4944 * isl_basic_map_underlying_set to the element.
4946 __isl_give isl_basic_set_list
*isl_basic_map_list_underlying_set(
4947 __isl_take isl_basic_map_list
*list
)
4954 n
= isl_basic_map_list_n_basic_map(list
);
4955 for (i
= 0; i
< n
; ++i
) {
4956 isl_basic_map
*bmap
;
4957 isl_basic_set
*bset
;
4959 bmap
= isl_basic_map_list_get_basic_map(list
, i
);
4960 bset
= isl_basic_set_underlying_set(bmap
);
4961 list
= isl_basic_set_list_set_basic_set(list
, i
, bset
);
4967 struct isl_basic_map
*isl_basic_map_overlying_set(
4968 struct isl_basic_set
*bset
, struct isl_basic_map
*like
)
4970 struct isl_basic_map
*bmap
;
4971 struct isl_ctx
*ctx
;
4978 isl_assert(ctx
, bset
->n_div
== 0, goto error
);
4979 isl_assert(ctx
, isl_basic_set_n_param(bset
) == 0, goto error
);
4980 isl_assert(ctx
, bset
->dim
->n_out
== isl_basic_map_total_dim(like
),
4982 if (like
->n_div
== 0) {
4983 isl_space
*space
= isl_basic_map_get_space(like
);
4984 isl_basic_map_free(like
);
4985 return isl_basic_map_reset_space(bset
, space
);
4987 bset
= isl_basic_set_cow(bset
);
4990 total
= bset
->dim
->n_out
+ bset
->extra
;
4991 bmap
= bset_to_bmap(bset
);
4992 isl_space_free(bmap
->dim
);
4993 bmap
->dim
= isl_space_copy(like
->dim
);
4996 bmap
->n_div
= like
->n_div
;
4997 bmap
->extra
+= like
->n_div
;
5001 ltotal
= total
- bmap
->extra
+ like
->extra
;
5004 bmap
->block2
= isl_blk_extend(ctx
, bmap
->block2
,
5005 bmap
->extra
* (1 + 1 + total
));
5006 if (isl_blk_is_error(bmap
->block2
))
5008 div
= isl_realloc_array(ctx
, bmap
->div
, isl_int
*, bmap
->extra
);
5012 for (i
= 0; i
< bmap
->extra
; ++i
)
5013 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + 1 + total
);
5014 for (i
= 0; i
< like
->n_div
; ++i
) {
5015 isl_seq_cpy(bmap
->div
[i
], like
->div
[i
], 1 + 1 + ltotal
);
5016 isl_seq_clr(bmap
->div
[i
]+1+1+ltotal
, total
- ltotal
);
5018 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
5020 isl_basic_map_free(like
);
5021 bmap
= isl_basic_map_simplify(bmap
);
5022 bmap
= isl_basic_map_finalize(bmap
);
5025 isl_basic_map_free(like
);
5026 isl_basic_set_free(bset
);
5030 struct isl_basic_set
*isl_basic_set_from_underlying_set(
5031 struct isl_basic_set
*bset
, struct isl_basic_set
*like
)
5033 return bset_from_bmap(isl_basic_map_overlying_set(bset
,
5034 bset_to_bmap(like
)));
5037 struct isl_set
*isl_map_underlying_set(struct isl_map
*map
)
5041 map
= isl_map_cow(map
);
5044 map
->dim
= isl_space_cow(map
->dim
);
5048 for (i
= 1; i
< map
->n
; ++i
)
5049 isl_assert(map
->ctx
, map
->p
[0]->n_div
== map
->p
[i
]->n_div
,
5051 for (i
= 0; i
< map
->n
; ++i
) {
5052 map
->p
[i
] = bset_to_bmap(
5053 isl_basic_map_underlying_set(map
->p
[i
]));
5058 map
->dim
= isl_space_underlying(map
->dim
, 0);
5060 isl_space_free(map
->dim
);
5061 map
->dim
= isl_space_copy(map
->p
[0]->dim
);
5065 return set_from_map(map
);
5071 /* Replace the space of "bmap" by "space".
5073 * If the space of "bmap" is identical to "space" (including the identifiers
5074 * of the input and output dimensions), then simply return the original input.
5076 __isl_give isl_basic_map
*isl_basic_map_reset_space(
5077 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*space
)
5083 equal
= isl_space_is_equal(bmap
->dim
, space
);
5084 if (equal
>= 0 && equal
)
5085 equal
= isl_space_match(bmap
->dim
, isl_dim_in
,
5087 if (equal
>= 0 && equal
)
5088 equal
= isl_space_match(bmap
->dim
, isl_dim_out
,
5089 space
, isl_dim_out
);
5093 isl_space_free(space
);
5096 bmap
= isl_basic_map_cow(bmap
);
5097 if (!bmap
|| !space
)
5100 isl_space_free(bmap
->dim
);
5103 bmap
= isl_basic_map_finalize(bmap
);
5107 isl_basic_map_free(bmap
);
5108 isl_space_free(space
);
5112 __isl_give isl_basic_set
*isl_basic_set_reset_space(
5113 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*dim
)
5115 return bset_from_bmap(isl_basic_map_reset_space(bset_to_bmap(bset
),
5119 __isl_give isl_map
*isl_map_reset_space(__isl_take isl_map
*map
,
5120 __isl_take isl_space
*dim
)
5124 map
= isl_map_cow(map
);
5128 for (i
= 0; i
< map
->n
; ++i
) {
5129 map
->p
[i
] = isl_basic_map_reset_space(map
->p
[i
],
5130 isl_space_copy(dim
));
5134 isl_space_free(map
->dim
);
5140 isl_space_free(dim
);
5144 __isl_give isl_set
*isl_set_reset_space(__isl_take isl_set
*set
,
5145 __isl_take isl_space
*dim
)
5147 return set_from_map(isl_map_reset_space(set_to_map(set
), dim
));
5150 /* Compute the parameter domain of the given basic set.
5152 __isl_give isl_basic_set
*isl_basic_set_params(__isl_take isl_basic_set
*bset
)
5158 is_params
= isl_basic_set_is_params(bset
);
5160 return isl_basic_set_free(bset
);
5164 n
= isl_basic_set_dim(bset
, isl_dim_set
);
5165 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
5166 space
= isl_basic_set_get_space(bset
);
5167 space
= isl_space_params(space
);
5168 bset
= isl_basic_set_reset_space(bset
, space
);
5172 /* Construct a zero-dimensional basic set with the given parameter domain.
5174 __isl_give isl_basic_set
*isl_basic_set_from_params(
5175 __isl_take isl_basic_set
*bset
)
5178 space
= isl_basic_set_get_space(bset
);
5179 space
= isl_space_set_from_params(space
);
5180 bset
= isl_basic_set_reset_space(bset
, space
);
5184 /* Compute the parameter domain of the given set.
5186 __isl_give isl_set
*isl_set_params(__isl_take isl_set
*set
)
5191 if (isl_set_is_params(set
))
5194 n
= isl_set_dim(set
, isl_dim_set
);
5195 set
= isl_set_project_out(set
, isl_dim_set
, 0, n
);
5196 space
= isl_set_get_space(set
);
5197 space
= isl_space_params(space
);
5198 set
= isl_set_reset_space(set
, space
);
5202 /* Construct a zero-dimensional set with the given parameter domain.
5204 __isl_give isl_set
*isl_set_from_params(__isl_take isl_set
*set
)
5207 space
= isl_set_get_space(set
);
5208 space
= isl_space_set_from_params(space
);
5209 set
= isl_set_reset_space(set
, space
);
5213 /* Compute the parameter domain of the given map.
5215 __isl_give isl_set
*isl_map_params(__isl_take isl_map
*map
)
5220 n
= isl_map_dim(map
, isl_dim_in
);
5221 map
= isl_map_project_out(map
, isl_dim_in
, 0, n
);
5222 n
= isl_map_dim(map
, isl_dim_out
);
5223 map
= isl_map_project_out(map
, isl_dim_out
, 0, n
);
5224 space
= isl_map_get_space(map
);
5225 space
= isl_space_params(space
);
5226 map
= isl_map_reset_space(map
, space
);
5230 struct isl_basic_set
*isl_basic_map_domain(struct isl_basic_map
*bmap
)
5237 space
= isl_space_domain(isl_basic_map_get_space(bmap
));
5239 n_out
= isl_basic_map_n_out(bmap
);
5240 bmap
= isl_basic_map_project_out(bmap
, isl_dim_out
, 0, n_out
);
5242 return isl_basic_map_reset_space(bmap
, space
);
5245 isl_bool
isl_basic_map_may_be_set(__isl_keep isl_basic_map
*bmap
)
5248 return isl_bool_error
;
5249 return isl_space_may_be_set(bmap
->dim
);
5252 /* Is this basic map actually a set?
5253 * Users should never call this function. Outside of isl,
5254 * the type should indicate whether something is a set or a map.
5256 isl_bool
isl_basic_map_is_set(__isl_keep isl_basic_map
*bmap
)
5259 return isl_bool_error
;
5260 return isl_space_is_set(bmap
->dim
);
5263 struct isl_basic_set
*isl_basic_map_range(struct isl_basic_map
*bmap
)
5267 is_set
= isl_basic_map_is_set(bmap
);
5272 return isl_basic_map_domain(isl_basic_map_reverse(bmap
));
5274 isl_basic_map_free(bmap
);
5278 __isl_give isl_basic_map
*isl_basic_map_domain_map(
5279 __isl_take isl_basic_map
*bmap
)
5283 isl_basic_map
*domain
;
5284 int nparam
, n_in
, n_out
;
5286 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5287 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5288 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5290 dim
= isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap
)));
5291 domain
= isl_basic_map_universe(dim
);
5293 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
5294 bmap
= isl_basic_map_apply_range(bmap
, domain
);
5295 bmap
= isl_basic_map_extend_constraints(bmap
, n_in
, 0);
5297 for (i
= 0; i
< n_in
; ++i
)
5298 bmap
= isl_basic_map_equate(bmap
, isl_dim_in
, i
,
5301 bmap
= isl_basic_map_gauss(bmap
, NULL
);
5302 return isl_basic_map_finalize(bmap
);
5305 __isl_give isl_basic_map
*isl_basic_map_range_map(
5306 __isl_take isl_basic_map
*bmap
)
5310 isl_basic_map
*range
;
5311 int nparam
, n_in
, n_out
;
5313 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5314 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5315 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5317 dim
= isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap
)));
5318 range
= isl_basic_map_universe(dim
);
5320 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
5321 bmap
= isl_basic_map_apply_range(bmap
, range
);
5322 bmap
= isl_basic_map_extend_constraints(bmap
, n_out
, 0);
5324 for (i
= 0; i
< n_out
; ++i
)
5325 bmap
= isl_basic_map_equate(bmap
, isl_dim_in
, n_in
+ i
,
5328 bmap
= isl_basic_map_gauss(bmap
, NULL
);
5329 return isl_basic_map_finalize(bmap
);
5332 int isl_map_may_be_set(__isl_keep isl_map
*map
)
5336 return isl_space_may_be_set(map
->dim
);
5339 /* Is this map actually a set?
5340 * Users should never call this function. Outside of isl,
5341 * the type should indicate whether something is a set or a map.
5343 isl_bool
isl_map_is_set(__isl_keep isl_map
*map
)
5346 return isl_bool_error
;
5347 return isl_space_is_set(map
->dim
);
5350 struct isl_set
*isl_map_range(struct isl_map
*map
)
5354 struct isl_set
*set
;
5356 is_set
= isl_map_is_set(map
);
5360 return set_from_map(map
);
5362 map
= isl_map_cow(map
);
5366 set
= set_from_map(map
);
5367 set
->dim
= isl_space_range(set
->dim
);
5370 for (i
= 0; i
< map
->n
; ++i
) {
5371 set
->p
[i
] = isl_basic_map_range(map
->p
[i
]);
5375 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
5376 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
5383 __isl_give isl_map
*isl_map_domain_map(__isl_take isl_map
*map
)
5387 map
= isl_map_cow(map
);
5391 map
->dim
= isl_space_domain_map(map
->dim
);
5394 for (i
= 0; i
< map
->n
; ++i
) {
5395 map
->p
[i
] = isl_basic_map_domain_map(map
->p
[i
]);
5399 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
5400 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5407 __isl_give isl_map
*isl_map_range_map(__isl_take isl_map
*map
)
5410 isl_space
*range_dim
;
5412 map
= isl_map_cow(map
);
5416 range_dim
= isl_space_range(isl_map_get_space(map
));
5417 range_dim
= isl_space_from_range(range_dim
);
5418 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
5419 map
->dim
= isl_space_join(map
->dim
, range_dim
);
5422 for (i
= 0; i
< map
->n
; ++i
) {
5423 map
->p
[i
] = isl_basic_map_range_map(map
->p
[i
]);
5427 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
5428 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5435 /* Given a wrapped map of the form A[B -> C],
5436 * return the map A[B -> C] -> B.
5438 __isl_give isl_map
*isl_set_wrapped_domain_map(__isl_take isl_set
*set
)
5445 if (!isl_set_has_tuple_id(set
))
5446 return isl_map_domain_map(isl_set_unwrap(set
));
5448 id
= isl_set_get_tuple_id(set
);
5449 map
= isl_map_domain_map(isl_set_unwrap(set
));
5450 map
= isl_map_set_tuple_id(map
, isl_dim_in
, id
);
5455 __isl_give isl_basic_map
*isl_basic_map_from_domain(
5456 __isl_take isl_basic_set
*bset
)
5458 return isl_basic_map_reverse(isl_basic_map_from_range(bset
));
5461 __isl_give isl_basic_map
*isl_basic_map_from_range(
5462 __isl_take isl_basic_set
*bset
)
5465 space
= isl_basic_set_get_space(bset
);
5466 space
= isl_space_from_range(space
);
5467 bset
= isl_basic_set_reset_space(bset
, space
);
5468 return bset_to_bmap(bset
);
5471 /* Create a relation with the given set as range.
5472 * The domain of the created relation is a zero-dimensional
5473 * flat anonymous space.
5475 __isl_give isl_map
*isl_map_from_range(__isl_take isl_set
*set
)
5478 space
= isl_set_get_space(set
);
5479 space
= isl_space_from_range(space
);
5480 set
= isl_set_reset_space(set
, space
);
5481 return set_to_map(set
);
5484 /* Create a relation with the given set as domain.
5485 * The range of the created relation is a zero-dimensional
5486 * flat anonymous space.
5488 __isl_give isl_map
*isl_map_from_domain(__isl_take isl_set
*set
)
5490 return isl_map_reverse(isl_map_from_range(set
));
5493 __isl_give isl_basic_map
*isl_basic_map_from_domain_and_range(
5494 __isl_take isl_basic_set
*domain
, __isl_take isl_basic_set
*range
)
5496 return isl_basic_map_apply_range(isl_basic_map_reverse(domain
), range
);
5499 __isl_give isl_map
*isl_map_from_domain_and_range(__isl_take isl_set
*domain
,
5500 __isl_take isl_set
*range
)
5502 return isl_map_apply_range(isl_map_reverse(domain
), range
);
5505 /* Return a newly allocated isl_map with given space and flags and
5506 * room for "n" basic maps.
5507 * Make sure that all cached information is cleared.
5509 __isl_give isl_map
*isl_map_alloc_space(__isl_take isl_space
*space
, int n
,
5512 struct isl_map
*map
;
5517 isl_die(space
->ctx
, isl_error_internal
,
5518 "negative number of basic maps", goto error
);
5519 map
= isl_calloc(space
->ctx
, struct isl_map
,
5520 sizeof(struct isl_map
) +
5521 (n
- 1) * sizeof(struct isl_basic_map
*));
5525 map
->ctx
= space
->ctx
;
5526 isl_ctx_ref(map
->ctx
);
5534 isl_space_free(space
);
5538 __isl_give isl_basic_map
*isl_basic_map_empty(__isl_take isl_space
*dim
)
5540 struct isl_basic_map
*bmap
;
5541 bmap
= isl_basic_map_alloc_space(dim
, 0, 1, 0);
5542 bmap
= isl_basic_map_set_to_empty(bmap
);
5546 __isl_give isl_basic_set
*isl_basic_set_empty(__isl_take isl_space
*dim
)
5548 struct isl_basic_set
*bset
;
5549 bset
= isl_basic_set_alloc_space(dim
, 0, 1, 0);
5550 bset
= isl_basic_set_set_to_empty(bset
);
5554 __isl_give isl_basic_map
*isl_basic_map_universe(__isl_take isl_space
*dim
)
5556 struct isl_basic_map
*bmap
;
5557 bmap
= isl_basic_map_alloc_space(dim
, 0, 0, 0);
5558 bmap
= isl_basic_map_finalize(bmap
);
5562 __isl_give isl_basic_set
*isl_basic_set_universe(__isl_take isl_space
*dim
)
5564 struct isl_basic_set
*bset
;
5565 bset
= isl_basic_set_alloc_space(dim
, 0, 0, 0);
5566 bset
= isl_basic_set_finalize(bset
);
5570 __isl_give isl_basic_map
*isl_basic_map_nat_universe(__isl_take isl_space
*dim
)
5573 unsigned total
= isl_space_dim(dim
, isl_dim_all
);
5574 isl_basic_map
*bmap
;
5576 bmap
= isl_basic_map_alloc_space(dim
, 0, 0, total
);
5577 for (i
= 0; i
< total
; ++i
) {
5578 int k
= isl_basic_map_alloc_inequality(bmap
);
5581 isl_seq_clr(bmap
->ineq
[k
], 1 + total
);
5582 isl_int_set_si(bmap
->ineq
[k
][1 + i
], 1);
5586 isl_basic_map_free(bmap
);
5590 __isl_give isl_basic_set
*isl_basic_set_nat_universe(__isl_take isl_space
*dim
)
5592 return isl_basic_map_nat_universe(dim
);
5595 __isl_give isl_map
*isl_map_nat_universe(__isl_take isl_space
*dim
)
5597 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim
));
5600 __isl_give isl_set
*isl_set_nat_universe(__isl_take isl_space
*dim
)
5602 return isl_map_nat_universe(dim
);
5605 __isl_give isl_map
*isl_map_empty(__isl_take isl_space
*dim
)
5607 return isl_map_alloc_space(dim
, 0, ISL_MAP_DISJOINT
);
5610 __isl_give isl_set
*isl_set_empty(__isl_take isl_space
*dim
)
5612 return isl_set_alloc_space(dim
, 0, ISL_MAP_DISJOINT
);
5615 __isl_give isl_map
*isl_map_universe(__isl_take isl_space
*dim
)
5617 struct isl_map
*map
;
5620 map
= isl_map_alloc_space(isl_space_copy(dim
), 1, ISL_MAP_DISJOINT
);
5621 map
= isl_map_add_basic_map(map
, isl_basic_map_universe(dim
));
5625 __isl_give isl_set
*isl_set_universe(__isl_take isl_space
*dim
)
5627 struct isl_set
*set
;
5630 set
= isl_set_alloc_space(isl_space_copy(dim
), 1, ISL_MAP_DISJOINT
);
5631 set
= isl_set_add_basic_set(set
, isl_basic_set_universe(dim
));
5635 struct isl_map
*isl_map_dup(struct isl_map
*map
)
5638 struct isl_map
*dup
;
5642 dup
= isl_map_alloc_space(isl_space_copy(map
->dim
), map
->n
, map
->flags
);
5643 for (i
= 0; i
< map
->n
; ++i
)
5644 dup
= isl_map_add_basic_map(dup
, isl_basic_map_copy(map
->p
[i
]));
5648 __isl_give isl_map
*isl_map_add_basic_map(__isl_take isl_map
*map
,
5649 __isl_take isl_basic_map
*bmap
)
5653 if (isl_basic_map_plain_is_empty(bmap
)) {
5654 isl_basic_map_free(bmap
);
5657 isl_assert(map
->ctx
, isl_space_is_equal(map
->dim
, bmap
->dim
), goto error
);
5658 isl_assert(map
->ctx
, map
->n
< map
->size
, goto error
);
5659 map
->p
[map
->n
] = bmap
;
5661 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5667 isl_basic_map_free(bmap
);
5671 __isl_null isl_map
*isl_map_free(__isl_take isl_map
*map
)
5682 isl_ctx_deref(map
->ctx
);
5683 for (i
= 0; i
< map
->n
; ++i
)
5684 isl_basic_map_free(map
->p
[i
]);
5685 isl_space_free(map
->dim
);
5691 static struct isl_basic_map
*isl_basic_map_fix_pos_si(
5692 struct isl_basic_map
*bmap
, unsigned pos
, int value
)
5696 bmap
= isl_basic_map_cow(bmap
);
5697 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
5698 j
= isl_basic_map_alloc_equality(bmap
);
5701 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
5702 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
5703 isl_int_set_si(bmap
->eq
[j
][0], value
);
5704 bmap
= isl_basic_map_simplify(bmap
);
5705 return isl_basic_map_finalize(bmap
);
5707 isl_basic_map_free(bmap
);
5711 static __isl_give isl_basic_map
*isl_basic_map_fix_pos(
5712 __isl_take isl_basic_map
*bmap
, unsigned pos
, isl_int value
)
5716 bmap
= isl_basic_map_cow(bmap
);
5717 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
5718 j
= isl_basic_map_alloc_equality(bmap
);
5721 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
5722 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
5723 isl_int_set(bmap
->eq
[j
][0], value
);
5724 bmap
= isl_basic_map_simplify(bmap
);
5725 return isl_basic_map_finalize(bmap
);
5727 isl_basic_map_free(bmap
);
5731 struct isl_basic_map
*isl_basic_map_fix_si(struct isl_basic_map
*bmap
,
5732 enum isl_dim_type type
, unsigned pos
, int value
)
5734 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
5735 return isl_basic_map_free(bmap
);
5736 return isl_basic_map_fix_pos_si(bmap
,
5737 isl_basic_map_offset(bmap
, type
) + pos
, value
);
5740 __isl_give isl_basic_map
*isl_basic_map_fix(__isl_take isl_basic_map
*bmap
,
5741 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5743 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
5744 return isl_basic_map_free(bmap
);
5745 return isl_basic_map_fix_pos(bmap
,
5746 isl_basic_map_offset(bmap
, type
) + pos
, value
);
5749 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
5750 * to be equal to "v".
5752 __isl_give isl_basic_map
*isl_basic_map_fix_val(__isl_take isl_basic_map
*bmap
,
5753 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5757 if (!isl_val_is_int(v
))
5758 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
5759 "expecting integer value", goto error
);
5760 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
5762 pos
+= isl_basic_map_offset(bmap
, type
);
5763 bmap
= isl_basic_map_fix_pos(bmap
, pos
, v
->n
);
5767 isl_basic_map_free(bmap
);
5772 /* Fix the value of the variable at position "pos" of type "type" of "bset"
5773 * to be equal to "v".
5775 __isl_give isl_basic_set
*isl_basic_set_fix_val(__isl_take isl_basic_set
*bset
,
5776 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5778 return isl_basic_map_fix_val(bset
, type
, pos
, v
);
5781 struct isl_basic_set
*isl_basic_set_fix_si(struct isl_basic_set
*bset
,
5782 enum isl_dim_type type
, unsigned pos
, int value
)
5784 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset
),
5788 __isl_give isl_basic_set
*isl_basic_set_fix(__isl_take isl_basic_set
*bset
,
5789 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5791 return bset_from_bmap(isl_basic_map_fix(bset_to_bmap(bset
),
5795 struct isl_basic_map
*isl_basic_map_fix_input_si(struct isl_basic_map
*bmap
,
5796 unsigned input
, int value
)
5798 return isl_basic_map_fix_si(bmap
, isl_dim_in
, input
, value
);
5801 struct isl_basic_set
*isl_basic_set_fix_dim_si(struct isl_basic_set
*bset
,
5802 unsigned dim
, int value
)
5804 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset
),
5805 isl_dim_set
, dim
, value
));
5808 static int remove_if_empty(__isl_keep isl_map
*map
, int i
)
5810 int empty
= isl_basic_map_plain_is_empty(map
->p
[i
]);
5817 isl_basic_map_free(map
->p
[i
]);
5818 if (i
!= map
->n
- 1) {
5819 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5820 map
->p
[i
] = map
->p
[map
->n
- 1];
5827 /* Perform "fn" on each basic map of "map", where we may not be holding
5828 * the only reference to "map".
5829 * In particular, "fn" should be a semantics preserving operation
5830 * that we want to apply to all copies of "map". We therefore need
5831 * to be careful not to modify "map" in a way that breaks "map"
5832 * in case anything goes wrong.
5834 __isl_give isl_map
*isl_map_inline_foreach_basic_map(__isl_take isl_map
*map
,
5835 __isl_give isl_basic_map
*(*fn
)(__isl_take isl_basic_map
*bmap
))
5837 struct isl_basic_map
*bmap
;
5843 for (i
= map
->n
- 1; i
>= 0; --i
) {
5844 bmap
= isl_basic_map_copy(map
->p
[i
]);
5848 isl_basic_map_free(map
->p
[i
]);
5850 if (remove_if_empty(map
, i
) < 0)
5860 struct isl_map
*isl_map_fix_si(struct isl_map
*map
,
5861 enum isl_dim_type type
, unsigned pos
, int value
)
5865 map
= isl_map_cow(map
);
5869 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
5870 for (i
= map
->n
- 1; i
>= 0; --i
) {
5871 map
->p
[i
] = isl_basic_map_fix_si(map
->p
[i
], type
, pos
, value
);
5872 if (remove_if_empty(map
, i
) < 0)
5875 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5882 __isl_give isl_set
*isl_set_fix_si(__isl_take isl_set
*set
,
5883 enum isl_dim_type type
, unsigned pos
, int value
)
5885 return set_from_map(isl_map_fix_si(set_to_map(set
), type
, pos
, value
));
5888 __isl_give isl_map
*isl_map_fix(__isl_take isl_map
*map
,
5889 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5893 map
= isl_map_cow(map
);
5897 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
5898 for (i
= 0; i
< map
->n
; ++i
) {
5899 map
->p
[i
] = isl_basic_map_fix(map
->p
[i
], type
, pos
, value
);
5903 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5910 __isl_give isl_set
*isl_set_fix(__isl_take isl_set
*set
,
5911 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5913 return set_from_map(isl_map_fix(set_to_map(set
), type
, pos
, value
));
5916 /* Fix the value of the variable at position "pos" of type "type" of "map"
5917 * to be equal to "v".
5919 __isl_give isl_map
*isl_map_fix_val(__isl_take isl_map
*map
,
5920 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5924 map
= isl_map_cow(map
);
5928 if (!isl_val_is_int(v
))
5929 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
5930 "expecting integer value", goto error
);
5931 if (pos
>= isl_map_dim(map
, type
))
5932 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
5933 "index out of bounds", goto error
);
5934 for (i
= map
->n
- 1; i
>= 0; --i
) {
5935 map
->p
[i
] = isl_basic_map_fix_val(map
->p
[i
], type
, pos
,
5937 if (remove_if_empty(map
, i
) < 0)
5940 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5949 /* Fix the value of the variable at position "pos" of type "type" of "set"
5950 * to be equal to "v".
5952 __isl_give isl_set
*isl_set_fix_val(__isl_take isl_set
*set
,
5953 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5955 return isl_map_fix_val(set
, type
, pos
, v
);
5958 struct isl_map
*isl_map_fix_input_si(struct isl_map
*map
,
5959 unsigned input
, int value
)
5961 return isl_map_fix_si(map
, isl_dim_in
, input
, value
);
5964 struct isl_set
*isl_set_fix_dim_si(struct isl_set
*set
, unsigned dim
, int value
)
5966 return set_from_map(isl_map_fix_si(set_to_map(set
),
5967 isl_dim_set
, dim
, value
));
5970 static __isl_give isl_basic_map
*basic_map_bound_si(
5971 __isl_take isl_basic_map
*bmap
,
5972 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
5976 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
5977 return isl_basic_map_free(bmap
);
5978 pos
+= isl_basic_map_offset(bmap
, type
);
5979 bmap
= isl_basic_map_cow(bmap
);
5980 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
5981 j
= isl_basic_map_alloc_inequality(bmap
);
5984 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
5986 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
5987 isl_int_set_si(bmap
->ineq
[j
][0], value
);
5989 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
5990 isl_int_set_si(bmap
->ineq
[j
][0], -value
);
5992 bmap
= isl_basic_map_simplify(bmap
);
5993 return isl_basic_map_finalize(bmap
);
5995 isl_basic_map_free(bmap
);
5999 __isl_give isl_basic_map
*isl_basic_map_lower_bound_si(
6000 __isl_take isl_basic_map
*bmap
,
6001 enum isl_dim_type type
, unsigned pos
, int value
)
6003 return basic_map_bound_si(bmap
, type
, pos
, value
, 0);
6006 /* Constrain the values of the given dimension to be no greater than "value".
6008 __isl_give isl_basic_map
*isl_basic_map_upper_bound_si(
6009 __isl_take isl_basic_map
*bmap
,
6010 enum isl_dim_type type
, unsigned pos
, int value
)
6012 return basic_map_bound_si(bmap
, type
, pos
, value
, 1);
6015 static __isl_give isl_map
*map_bound_si(__isl_take isl_map
*map
,
6016 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
6020 map
= isl_map_cow(map
);
6024 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
6025 for (i
= 0; i
< map
->n
; ++i
) {
6026 map
->p
[i
] = basic_map_bound_si(map
->p
[i
],
6027 type
, pos
, value
, upper
);
6031 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6038 __isl_give isl_map
*isl_map_lower_bound_si(__isl_take isl_map
*map
,
6039 enum isl_dim_type type
, unsigned pos
, int value
)
6041 return map_bound_si(map
, type
, pos
, value
, 0);
6044 __isl_give isl_map
*isl_map_upper_bound_si(__isl_take isl_map
*map
,
6045 enum isl_dim_type type
, unsigned pos
, int value
)
6047 return map_bound_si(map
, type
, pos
, value
, 1);
6050 __isl_give isl_set
*isl_set_lower_bound_si(__isl_take isl_set
*set
,
6051 enum isl_dim_type type
, unsigned pos
, int value
)
6053 return set_from_map(isl_map_lower_bound_si(set_to_map(set
),
6057 __isl_give isl_set
*isl_set_upper_bound_si(__isl_take isl_set
*set
,
6058 enum isl_dim_type type
, unsigned pos
, int value
)
6060 return isl_map_upper_bound_si(set
, type
, pos
, value
);
6063 /* Bound the given variable of "bmap" from below (or above is "upper"
6064 * is set) to "value".
6066 static __isl_give isl_basic_map
*basic_map_bound(
6067 __isl_take isl_basic_map
*bmap
,
6068 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
6072 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6073 return isl_basic_map_free(bmap
);
6074 pos
+= isl_basic_map_offset(bmap
, type
);
6075 bmap
= isl_basic_map_cow(bmap
);
6076 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
6077 j
= isl_basic_map_alloc_inequality(bmap
);
6080 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
6082 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
6083 isl_int_set(bmap
->ineq
[j
][0], value
);
6085 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
6086 isl_int_neg(bmap
->ineq
[j
][0], value
);
6088 bmap
= isl_basic_map_simplify(bmap
);
6089 return isl_basic_map_finalize(bmap
);
6091 isl_basic_map_free(bmap
);
6095 /* Bound the given variable of "map" from below (or above is "upper"
6096 * is set) to "value".
6098 static __isl_give isl_map
*map_bound(__isl_take isl_map
*map
,
6099 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
6103 map
= isl_map_cow(map
);
6107 if (pos
>= isl_map_dim(map
, type
))
6108 isl_die(map
->ctx
, isl_error_invalid
,
6109 "index out of bounds", goto error
);
6110 for (i
= map
->n
- 1; i
>= 0; --i
) {
6111 map
->p
[i
] = basic_map_bound(map
->p
[i
], type
, pos
, value
, upper
);
6112 if (remove_if_empty(map
, i
) < 0)
6115 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6122 __isl_give isl_map
*isl_map_lower_bound(__isl_take isl_map
*map
,
6123 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6125 return map_bound(map
, type
, pos
, value
, 0);
6128 __isl_give isl_map
*isl_map_upper_bound(__isl_take isl_map
*map
,
6129 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6131 return map_bound(map
, type
, pos
, value
, 1);
6134 __isl_give isl_set
*isl_set_lower_bound(__isl_take isl_set
*set
,
6135 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6137 return isl_map_lower_bound(set
, type
, pos
, value
);
6140 __isl_give isl_set
*isl_set_upper_bound(__isl_take isl_set
*set
,
6141 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6143 return isl_map_upper_bound(set
, type
, pos
, value
);
6146 /* Force the values of the variable at position "pos" of type "type" of "set"
6147 * to be no smaller than "value".
6149 __isl_give isl_set
*isl_set_lower_bound_val(__isl_take isl_set
*set
,
6150 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
6154 if (!isl_val_is_int(value
))
6155 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
6156 "expecting integer value", goto error
);
6157 set
= isl_set_lower_bound(set
, type
, pos
, value
->n
);
6158 isl_val_free(value
);
6161 isl_val_free(value
);
6166 /* Force the values of the variable at position "pos" of type "type" of "set"
6167 * to be no greater than "value".
6169 __isl_give isl_set
*isl_set_upper_bound_val(__isl_take isl_set
*set
,
6170 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
6174 if (!isl_val_is_int(value
))
6175 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
6176 "expecting integer value", goto error
);
6177 set
= isl_set_upper_bound(set
, type
, pos
, value
->n
);
6178 isl_val_free(value
);
6181 isl_val_free(value
);
6186 struct isl_map
*isl_map_reverse(struct isl_map
*map
)
6190 map
= isl_map_cow(map
);
6194 map
->dim
= isl_space_reverse(map
->dim
);
6197 for (i
= 0; i
< map
->n
; ++i
) {
6198 map
->p
[i
] = isl_basic_map_reverse(map
->p
[i
]);
6202 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6210 #define TYPE isl_pw_multi_aff
6212 #define SUFFIX _pw_multi_aff
6214 #define EMPTY isl_pw_multi_aff_empty
6216 #define ADD isl_pw_multi_aff_union_add
6217 #include "isl_map_lexopt_templ.c"
6219 /* Given a map "map", compute the lexicographically minimal
6220 * (or maximal) image element for each domain element in dom,
6221 * in the form of an isl_pw_multi_aff.
6222 * If "empty" is not NULL, then set *empty to those elements in dom that
6223 * do not have an image element.
6224 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6225 * should be computed over the domain of "map". "empty" is also NULL
6228 * We first compute the lexicographically minimal or maximal element
6229 * in the first basic map. This results in a partial solution "res"
6230 * and a subset "todo" of dom that still need to be handled.
6231 * We then consider each of the remaining maps in "map" and successively
6232 * update both "res" and "todo".
6233 * If "empty" is NULL, then the todo sets are not needed and therefore
6234 * also not computed.
6236 static __isl_give isl_pw_multi_aff
*isl_map_partial_lexopt_aligned_pw_multi_aff(
6237 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6238 __isl_give isl_set
**empty
, unsigned flags
)
6242 isl_pw_multi_aff
*res
;
6245 full
= ISL_FL_ISSET(flags
, ISL_OPT_FULL
);
6246 if (!map
|| (!full
&& !dom
))
6249 if (isl_map_plain_is_empty(map
)) {
6254 return isl_pw_multi_aff_from_map(map
);
6257 res
= basic_map_partial_lexopt_pw_multi_aff(
6258 isl_basic_map_copy(map
->p
[0]),
6259 isl_set_copy(dom
), empty
, flags
);
6263 for (i
= 1; i
< map
->n
; ++i
) {
6264 isl_pw_multi_aff
*res_i
;
6266 res_i
= basic_map_partial_lexopt_pw_multi_aff(
6267 isl_basic_map_copy(map
->p
[i
]),
6268 isl_set_copy(dom
), empty
, flags
);
6270 if (ISL_FL_ISSET(flags
, ISL_OPT_MAX
))
6271 res
= isl_pw_multi_aff_union_lexmax(res
, res_i
);
6273 res
= isl_pw_multi_aff_union_lexmin(res
, res_i
);
6276 todo
= isl_set_intersect(todo
, *empty
);
6295 #define TYPE isl_map
6299 #define EMPTY isl_map_empty
6301 #define ADD isl_map_union_disjoint
6302 #include "isl_map_lexopt_templ.c"
6304 /* Given a map "map", compute the lexicographically minimal
6305 * (or maximal) image element for each domain element in "dom",
6306 * in the form of an isl_map.
6307 * If "empty" is not NULL, then set *empty to those elements in "dom" that
6308 * do not have an image element.
6309 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6310 * should be computed over the domain of "map". "empty" is also NULL
6313 * If the input consists of more than one disjunct, then first
6314 * compute the desired result in the form of an isl_pw_multi_aff and
6315 * then convert that into an isl_map.
6317 * This function used to have an explicit implementation in terms
6318 * of isl_maps, but it would continually intersect the domains of
6319 * partial results with the complement of the domain of the next
6320 * partial solution, potentially leading to an explosion in the number
6321 * of disjuncts if there are several disjuncts in the input.
6322 * An even earlier implementation of this function would look for
6323 * better results in the domain of the partial result and for extra
6324 * results in the complement of this domain, which would lead to
6325 * even more splintering.
6327 static __isl_give isl_map
*isl_map_partial_lexopt_aligned(
6328 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6329 __isl_give isl_set
**empty
, unsigned flags
)
6332 struct isl_map
*res
;
6333 isl_pw_multi_aff
*pma
;
6335 full
= ISL_FL_ISSET(flags
, ISL_OPT_FULL
);
6336 if (!map
|| (!full
&& !dom
))
6339 if (isl_map_plain_is_empty(map
)) {
6348 res
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[0]),
6354 pma
= isl_map_partial_lexopt_aligned_pw_multi_aff(map
, dom
, empty
,
6356 return isl_map_from_pw_multi_aff(pma
);
6365 __isl_give isl_map
*isl_map_partial_lexmax(
6366 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6367 __isl_give isl_set
**empty
)
6369 return isl_map_partial_lexopt(map
, dom
, empty
, ISL_OPT_MAX
);
6372 __isl_give isl_map
*isl_map_partial_lexmin(
6373 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6374 __isl_give isl_set
**empty
)
6376 return isl_map_partial_lexopt(map
, dom
, empty
, 0);
6379 __isl_give isl_set
*isl_set_partial_lexmin(
6380 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
6381 __isl_give isl_set
**empty
)
6383 return set_from_map(isl_map_partial_lexmin(set_to_map(set
),
6387 __isl_give isl_set
*isl_set_partial_lexmax(
6388 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
6389 __isl_give isl_set
**empty
)
6391 return set_from_map(isl_map_partial_lexmax(set_to_map(set
),
6395 /* Compute the lexicographic minimum (or maximum if "flags" includes
6396 * ISL_OPT_MAX) of "bset" over its parametric domain.
6398 __isl_give isl_set
*isl_basic_set_lexopt(__isl_take isl_basic_set
*bset
,
6401 return isl_basic_map_lexopt(bset
, flags
);
6404 __isl_give isl_map
*isl_basic_map_lexmax(__isl_take isl_basic_map
*bmap
)
6406 return isl_basic_map_lexopt(bmap
, ISL_OPT_MAX
);
6409 __isl_give isl_set
*isl_basic_set_lexmin(__isl_take isl_basic_set
*bset
)
6411 return set_from_map(isl_basic_map_lexmin(bset_to_bmap(bset
)));
6414 __isl_give isl_set
*isl_basic_set_lexmax(__isl_take isl_basic_set
*bset
)
6416 return set_from_map(isl_basic_map_lexmax(bset_to_bmap(bset
)));
6419 /* Compute the lexicographic minimum of "bset" over its parametric domain
6420 * for the purpose of quantifier elimination.
6421 * That is, find an explicit representation for all the existentially
6422 * quantified variables in "bset" by computing their lexicographic
6425 static __isl_give isl_set
*isl_basic_set_lexmin_compute_divs(
6426 __isl_take isl_basic_set
*bset
)
6428 return isl_basic_set_lexopt(bset
, ISL_OPT_QE
);
6431 /* Extract the first and only affine expression from list
6432 * and then add it to *pwaff with the given dom.
6433 * This domain is known to be disjoint from other domains
6434 * because of the way isl_basic_map_foreach_lexmax works.
6436 static isl_stat
update_dim_opt(__isl_take isl_basic_set
*dom
,
6437 __isl_take isl_aff_list
*list
, void *user
)
6439 isl_ctx
*ctx
= isl_basic_set_get_ctx(dom
);
6441 isl_pw_aff
**pwaff
= user
;
6442 isl_pw_aff
*pwaff_i
;
6446 if (isl_aff_list_n_aff(list
) != 1)
6447 isl_die(ctx
, isl_error_internal
,
6448 "expecting single element list", goto error
);
6450 aff
= isl_aff_list_get_aff(list
, 0);
6451 pwaff_i
= isl_pw_aff_alloc(isl_set_from_basic_set(dom
), aff
);
6453 *pwaff
= isl_pw_aff_add_disjoint(*pwaff
, pwaff_i
);
6455 isl_aff_list_free(list
);
6459 isl_basic_set_free(dom
);
6460 isl_aff_list_free(list
);
6461 return isl_stat_error
;
6464 /* Given a basic map with one output dimension, compute the minimum or
6465 * maximum of that dimension as an isl_pw_aff.
6467 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
6468 * call update_dim_opt on each leaf of the result.
6470 static __isl_give isl_pw_aff
*basic_map_dim_opt(__isl_keep isl_basic_map
*bmap
,
6473 isl_space
*dim
= isl_basic_map_get_space(bmap
);
6477 dim
= isl_space_from_domain(isl_space_domain(dim
));
6478 dim
= isl_space_add_dims(dim
, isl_dim_out
, 1);
6479 pwaff
= isl_pw_aff_empty(dim
);
6481 r
= isl_basic_map_foreach_lexopt(bmap
, max
, &update_dim_opt
, &pwaff
);
6483 return isl_pw_aff_free(pwaff
);
6488 /* Compute the minimum or maximum of the given output dimension
6489 * as a function of the parameters and the input dimensions,
6490 * but independently of the other output dimensions.
6492 * We first project out the other output dimension and then compute
6493 * the "lexicographic" maximum in each basic map, combining the results
6494 * using isl_pw_aff_union_max.
6496 static __isl_give isl_pw_aff
*map_dim_opt(__isl_take isl_map
*map
, int pos
,
6503 n_out
= isl_map_dim(map
, isl_dim_out
);
6504 map
= isl_map_project_out(map
, isl_dim_out
, pos
+ 1, n_out
- (pos
+ 1));
6505 map
= isl_map_project_out(map
, isl_dim_out
, 0, pos
);
6510 isl_space
*dim
= isl_map_get_space(map
);
6512 return isl_pw_aff_empty(dim
);
6515 pwaff
= basic_map_dim_opt(map
->p
[0], max
);
6516 for (i
= 1; i
< map
->n
; ++i
) {
6517 isl_pw_aff
*pwaff_i
;
6519 pwaff_i
= basic_map_dim_opt(map
->p
[i
], max
);
6520 pwaff
= isl_pw_aff_union_opt(pwaff
, pwaff_i
, max
);
6528 /* Compute the minimum of the given output dimension as a function of the
6529 * parameters and input dimensions, but independently of
6530 * the other output dimensions.
6532 __isl_give isl_pw_aff
*isl_map_dim_min(__isl_take isl_map
*map
, int pos
)
6534 return map_dim_opt(map
, pos
, 0);
6537 /* Compute the maximum of the given output dimension as a function of the
6538 * parameters and input dimensions, but independently of
6539 * the other output dimensions.
6541 __isl_give isl_pw_aff
*isl_map_dim_max(__isl_take isl_map
*map
, int pos
)
6543 return map_dim_opt(map
, pos
, 1);
6546 /* Compute the minimum or maximum of the given set dimension
6547 * as a function of the parameters,
6548 * but independently of the other set dimensions.
6550 static __isl_give isl_pw_aff
*set_dim_opt(__isl_take isl_set
*set
, int pos
,
6553 return map_dim_opt(set
, pos
, max
);
6556 /* Compute the maximum of the given set dimension as a function of the
6557 * parameters, but independently of the other set dimensions.
6559 __isl_give isl_pw_aff
*isl_set_dim_max(__isl_take isl_set
*set
, int pos
)
6561 return set_dim_opt(set
, pos
, 1);
6564 /* Compute the minimum of the given set dimension as a function of the
6565 * parameters, but independently of the other set dimensions.
6567 __isl_give isl_pw_aff
*isl_set_dim_min(__isl_take isl_set
*set
, int pos
)
6569 return set_dim_opt(set
, pos
, 0);
6572 /* Apply a preimage specified by "mat" on the parameters of "bset".
6573 * bset is assumed to have only parameters and divs.
6575 static struct isl_basic_set
*basic_set_parameter_preimage(
6576 struct isl_basic_set
*bset
, struct isl_mat
*mat
)
6583 bset
->dim
= isl_space_cow(bset
->dim
);
6587 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
6589 isl_assert(bset
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
6591 bset
->dim
->nparam
= 0;
6592 bset
->dim
->n_out
= nparam
;
6593 bset
= isl_basic_set_preimage(bset
, mat
);
6595 bset
->dim
->nparam
= bset
->dim
->n_out
;
6596 bset
->dim
->n_out
= 0;
6601 isl_basic_set_free(bset
);
6605 /* Apply a preimage specified by "mat" on the parameters of "set".
6606 * set is assumed to have only parameters and divs.
6608 static __isl_give isl_set
*set_parameter_preimage(__isl_take isl_set
*set
,
6609 __isl_take isl_mat
*mat
)
6617 nparam
= isl_set_dim(set
, isl_dim_param
);
6619 if (mat
->n_row
!= 1 + nparam
)
6620 isl_die(isl_set_get_ctx(set
), isl_error_internal
,
6621 "unexpected number of rows", goto error
);
6623 space
= isl_set_get_space(set
);
6624 space
= isl_space_move_dims(space
, isl_dim_set
, 0,
6625 isl_dim_param
, 0, nparam
);
6626 set
= isl_set_reset_space(set
, space
);
6627 set
= isl_set_preimage(set
, mat
);
6628 nparam
= isl_set_dim(set
, isl_dim_out
);
6629 space
= isl_set_get_space(set
);
6630 space
= isl_space_move_dims(space
, isl_dim_param
, 0,
6631 isl_dim_out
, 0, nparam
);
6632 set
= isl_set_reset_space(set
, space
);
6640 /* Intersect the basic set "bset" with the affine space specified by the
6641 * equalities in "eq".
6643 static struct isl_basic_set
*basic_set_append_equalities(
6644 struct isl_basic_set
*bset
, struct isl_mat
*eq
)
6652 bset
= isl_basic_set_extend_space(bset
, isl_space_copy(bset
->dim
), 0,
6657 len
= 1 + isl_space_dim(bset
->dim
, isl_dim_all
) + bset
->extra
;
6658 for (i
= 0; i
< eq
->n_row
; ++i
) {
6659 k
= isl_basic_set_alloc_equality(bset
);
6662 isl_seq_cpy(bset
->eq
[k
], eq
->row
[i
], eq
->n_col
);
6663 isl_seq_clr(bset
->eq
[k
] + eq
->n_col
, len
- eq
->n_col
);
6667 bset
= isl_basic_set_gauss(bset
, NULL
);
6668 bset
= isl_basic_set_finalize(bset
);
6673 isl_basic_set_free(bset
);
6677 /* Intersect the set "set" with the affine space specified by the
6678 * equalities in "eq".
6680 static struct isl_set
*set_append_equalities(struct isl_set
*set
,
6688 for (i
= 0; i
< set
->n
; ++i
) {
6689 set
->p
[i
] = basic_set_append_equalities(set
->p
[i
],
6702 /* Given a basic set "bset" that only involves parameters and existentially
6703 * quantified variables, return the index of the first equality
6704 * that only involves parameters. If there is no such equality then
6705 * return bset->n_eq.
6707 * This function assumes that isl_basic_set_gauss has been called on "bset".
6709 static int first_parameter_equality(__isl_keep isl_basic_set
*bset
)
6712 unsigned nparam
, n_div
;
6717 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
6718 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
6720 for (i
= 0, j
= n_div
- 1; i
< bset
->n_eq
&& j
>= 0; --j
) {
6721 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ j
]))
6728 /* Compute an explicit representation for the existentially quantified
6729 * variables in "bset" by computing the "minimal value" of the set
6730 * variables. Since there are no set variables, the computation of
6731 * the minimal value essentially computes an explicit representation
6732 * of the non-empty part(s) of "bset".
6734 * The input only involves parameters and existentially quantified variables.
6735 * All equalities among parameters have been removed.
6737 * Since the existentially quantified variables in the result are in general
6738 * going to be different from those in the input, we first replace
6739 * them by the minimal number of variables based on their equalities.
6740 * This should simplify the parametric integer programming.
6742 static __isl_give isl_set
*base_compute_divs(__isl_take isl_basic_set
*bset
)
6744 isl_morph
*morph1
, *morph2
;
6750 if (bset
->n_eq
== 0)
6751 return isl_basic_set_lexmin_compute_divs(bset
);
6753 morph1
= isl_basic_set_parameter_compression(bset
);
6754 bset
= isl_morph_basic_set(isl_morph_copy(morph1
), bset
);
6755 bset
= isl_basic_set_lift(bset
);
6756 morph2
= isl_basic_set_variable_compression(bset
, isl_dim_set
);
6757 bset
= isl_morph_basic_set(morph2
, bset
);
6758 n
= isl_basic_set_dim(bset
, isl_dim_set
);
6759 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
6761 set
= isl_basic_set_lexmin_compute_divs(bset
);
6763 set
= isl_morph_set(isl_morph_inverse(morph1
), set
);
6768 /* Project the given basic set onto its parameter domain, possibly introducing
6769 * new, explicit, existential variables in the constraints.
6770 * The input has parameters and (possibly implicit) existential variables.
6771 * The output has the same parameters, but only
6772 * explicit existentially quantified variables.
6774 * The actual projection is performed by pip, but pip doesn't seem
6775 * to like equalities very much, so we first remove the equalities
6776 * among the parameters by performing a variable compression on
6777 * the parameters. Afterward, an inverse transformation is performed
6778 * and the equalities among the parameters are inserted back in.
6780 * The variable compression on the parameters may uncover additional
6781 * equalities that were only implicit before. We therefore check
6782 * if there are any new parameter equalities in the result and
6783 * if so recurse. The removal of parameter equalities is required
6784 * for the parameter compression performed by base_compute_divs.
6786 static struct isl_set
*parameter_compute_divs(struct isl_basic_set
*bset
)
6790 struct isl_mat
*T
, *T2
;
6791 struct isl_set
*set
;
6794 bset
= isl_basic_set_cow(bset
);
6798 if (bset
->n_eq
== 0)
6799 return base_compute_divs(bset
);
6801 bset
= isl_basic_set_gauss(bset
, NULL
);
6804 if (isl_basic_set_plain_is_empty(bset
))
6805 return isl_set_from_basic_set(bset
);
6807 i
= first_parameter_equality(bset
);
6808 if (i
== bset
->n_eq
)
6809 return base_compute_divs(bset
);
6811 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
6812 eq
= isl_mat_sub_alloc6(bset
->ctx
, bset
->eq
, i
, bset
->n_eq
- i
,
6814 eq
= isl_mat_cow(eq
);
6815 T
= isl_mat_variable_compression(isl_mat_copy(eq
), &T2
);
6816 if (T
&& T
->n_col
== 0) {
6820 bset
= isl_basic_set_set_to_empty(bset
);
6821 return isl_set_from_basic_set(bset
);
6823 bset
= basic_set_parameter_preimage(bset
, T
);
6825 i
= first_parameter_equality(bset
);
6828 else if (i
== bset
->n_eq
)
6829 set
= base_compute_divs(bset
);
6831 set
= parameter_compute_divs(bset
);
6832 set
= set_parameter_preimage(set
, T2
);
6833 set
= set_append_equalities(set
, eq
);
6837 /* Insert the divs from "ls" before those of "bmap".
6839 * The number of columns is not changed, which means that the last
6840 * dimensions of "bmap" are being reintepreted as the divs from "ls".
6841 * The caller is responsible for removing the same number of dimensions
6842 * from the space of "bmap".
6844 static __isl_give isl_basic_map
*insert_divs_from_local_space(
6845 __isl_take isl_basic_map
*bmap
, __isl_keep isl_local_space
*ls
)
6851 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
6855 old_n_div
= bmap
->n_div
;
6856 bmap
= insert_div_rows(bmap
, n_div
);
6860 for (i
= 0; i
< n_div
; ++i
) {
6861 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], ls
->div
->n_col
);
6862 isl_seq_clr(bmap
->div
[i
] + ls
->div
->n_col
, old_n_div
);
6868 /* Replace the space of "bmap" by the space and divs of "ls".
6870 * If "ls" has any divs, then we simplify the result since we may
6871 * have discovered some additional equalities that could simplify
6872 * the div expressions.
6874 static __isl_give isl_basic_map
*basic_replace_space_by_local_space(
6875 __isl_take isl_basic_map
*bmap
, __isl_take isl_local_space
*ls
)
6879 bmap
= isl_basic_map_cow(bmap
);
6883 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
6884 bmap
= insert_divs_from_local_space(bmap
, ls
);
6888 isl_space_free(bmap
->dim
);
6889 bmap
->dim
= isl_local_space_get_space(ls
);
6893 isl_local_space_free(ls
);
6895 bmap
= isl_basic_map_simplify(bmap
);
6896 bmap
= isl_basic_map_finalize(bmap
);
6899 isl_basic_map_free(bmap
);
6900 isl_local_space_free(ls
);
6904 /* Replace the space of "map" by the space and divs of "ls".
6906 static __isl_give isl_map
*replace_space_by_local_space(__isl_take isl_map
*map
,
6907 __isl_take isl_local_space
*ls
)
6911 map
= isl_map_cow(map
);
6915 for (i
= 0; i
< map
->n
; ++i
) {
6916 map
->p
[i
] = basic_replace_space_by_local_space(map
->p
[i
],
6917 isl_local_space_copy(ls
));
6921 isl_space_free(map
->dim
);
6922 map
->dim
= isl_local_space_get_space(ls
);
6926 isl_local_space_free(ls
);
6929 isl_local_space_free(ls
);
6934 /* Compute an explicit representation for the existentially
6935 * quantified variables for which do not know any explicit representation yet.
6937 * We first sort the existentially quantified variables so that the
6938 * existentially quantified variables for which we already have an explicit
6939 * representation are placed before those for which we do not.
6940 * The input dimensions, the output dimensions and the existentially
6941 * quantified variables for which we already have an explicit
6942 * representation are then turned into parameters.
6943 * compute_divs returns a map with the same parameters and
6944 * no input or output dimensions and the dimension specification
6945 * is reset to that of the input, including the existentially quantified
6946 * variables for which we already had an explicit representation.
6948 static struct isl_map
*compute_divs(struct isl_basic_map
*bmap
)
6950 struct isl_basic_set
*bset
;
6951 struct isl_set
*set
;
6952 struct isl_map
*map
;
6954 isl_local_space
*ls
;
6961 bmap
= isl_basic_map_sort_divs(bmap
);
6962 bmap
= isl_basic_map_cow(bmap
);
6966 n_known
= isl_basic_map_first_unknown_div(bmap
);
6968 return isl_map_from_basic_map(isl_basic_map_free(bmap
));
6970 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
6971 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
6972 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
6973 dim
= isl_space_set_alloc(bmap
->ctx
,
6974 nparam
+ n_in
+ n_out
+ n_known
, 0);
6978 ls
= isl_basic_map_get_local_space(bmap
);
6979 ls
= isl_local_space_drop_dims(ls
, isl_dim_div
,
6980 n_known
, bmap
->n_div
- n_known
);
6982 for (i
= n_known
; i
< bmap
->n_div
; ++i
)
6983 swap_div(bmap
, i
- n_known
, i
);
6984 bmap
->n_div
-= n_known
;
6985 bmap
->extra
-= n_known
;
6987 bmap
= isl_basic_map_reset_space(bmap
, dim
);
6988 bset
= bset_from_bmap(bmap
);
6990 set
= parameter_compute_divs(bset
);
6991 map
= set_to_map(set
);
6992 map
= replace_space_by_local_space(map
, ls
);
6996 isl_basic_map_free(bmap
);
7000 /* Remove the explicit representation of local variable "div",
7003 __isl_give isl_basic_map
*isl_basic_map_mark_div_unknown(
7004 __isl_take isl_basic_map
*bmap
, int div
)
7008 unknown
= isl_basic_map_div_is_marked_unknown(bmap
, div
);
7010 return isl_basic_map_free(bmap
);
7014 bmap
= isl_basic_map_cow(bmap
);
7017 isl_int_set_si(bmap
->div
[div
][0], 0);
7021 /* Is local variable "div" of "bmap" marked as not having an explicit
7023 * Note that even if "div" is not marked in this way and therefore
7024 * has an explicit representation, this representation may still
7025 * depend (indirectly) on other local variables that do not
7026 * have an explicit representation.
7028 isl_bool
isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map
*bmap
,
7031 if (isl_basic_map_check_range(bmap
, isl_dim_div
, div
, 1) < 0)
7032 return isl_bool_error
;
7033 return isl_int_is_zero(bmap
->div
[div
][0]);
7036 /* Return the position of the first local variable that does not
7037 * have an explicit representation.
7038 * Return the total number of local variables if they all have
7039 * an explicit representation.
7040 * Return -1 on error.
7042 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map
*bmap
)
7049 for (i
= 0; i
< bmap
->n_div
; ++i
) {
7050 if (!isl_basic_map_div_is_known(bmap
, i
))
7056 /* Return the position of the first local variable that does not
7057 * have an explicit representation.
7058 * Return the total number of local variables if they all have
7059 * an explicit representation.
7060 * Return -1 on error.
7062 int isl_basic_set_first_unknown_div(__isl_keep isl_basic_set
*bset
)
7064 return isl_basic_map_first_unknown_div(bset
);
7067 /* Does "bmap" have an explicit representation for all local variables?
7069 isl_bool
isl_basic_map_divs_known(__isl_keep isl_basic_map
*bmap
)
7073 n
= isl_basic_map_dim(bmap
, isl_dim_div
);
7074 first
= isl_basic_map_first_unknown_div(bmap
);
7076 return isl_bool_error
;
7080 /* Do all basic maps in "map" have an explicit representation
7081 * for all local variables?
7083 isl_bool
isl_map_divs_known(__isl_keep isl_map
*map
)
7088 return isl_bool_error
;
7090 for (i
= 0; i
< map
->n
; ++i
) {
7091 int known
= isl_basic_map_divs_known(map
->p
[i
]);
7096 return isl_bool_true
;
7099 /* If bmap contains any unknown divs, then compute explicit
7100 * expressions for them. However, this computation may be
7101 * quite expensive, so first try to remove divs that aren't
7104 struct isl_map
*isl_basic_map_compute_divs(struct isl_basic_map
*bmap
)
7107 struct isl_map
*map
;
7109 known
= isl_basic_map_divs_known(bmap
);
7113 return isl_map_from_basic_map(bmap
);
7115 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
7117 known
= isl_basic_map_divs_known(bmap
);
7121 return isl_map_from_basic_map(bmap
);
7123 map
= compute_divs(bmap
);
7126 isl_basic_map_free(bmap
);
7130 struct isl_map
*isl_map_compute_divs(struct isl_map
*map
)
7134 struct isl_map
*res
;
7141 known
= isl_map_divs_known(map
);
7149 res
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[0]));
7150 for (i
= 1 ; i
< map
->n
; ++i
) {
7152 r2
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[i
]));
7153 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
7154 res
= isl_map_union_disjoint(res
, r2
);
7156 res
= isl_map_union(res
, r2
);
7163 struct isl_set
*isl_basic_set_compute_divs(struct isl_basic_set
*bset
)
7165 return set_from_map(isl_basic_map_compute_divs(bset_to_bmap(bset
)));
7168 struct isl_set
*isl_set_compute_divs(struct isl_set
*set
)
7170 return set_from_map(isl_map_compute_divs(set_to_map(set
)));
7173 struct isl_set
*isl_map_domain(struct isl_map
*map
)
7176 struct isl_set
*set
;
7181 map
= isl_map_cow(map
);
7185 set
= set_from_map(map
);
7186 set
->dim
= isl_space_domain(set
->dim
);
7189 for (i
= 0; i
< map
->n
; ++i
) {
7190 set
->p
[i
] = isl_basic_map_domain(map
->p
[i
]);
7194 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
7195 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
7202 /* Return the union of "map1" and "map2", where we assume for now that
7203 * "map1" and "map2" are disjoint. Note that the basic maps inside
7204 * "map1" or "map2" may not be disjoint from each other.
7205 * Also note that this function is also called from isl_map_union,
7206 * which takes care of handling the situation where "map1" and "map2"
7207 * may not be disjoint.
7209 * If one of the inputs is empty, we can simply return the other input.
7210 * Similarly, if one of the inputs is universal, then it is equal to the union.
7212 static __isl_give isl_map
*map_union_disjoint(__isl_take isl_map
*map1
,
7213 __isl_take isl_map
*map2
)
7217 struct isl_map
*map
= NULL
;
7223 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
7224 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
7225 "spaces don't match", goto error
);
7236 is_universe
= isl_map_plain_is_universe(map1
);
7237 if (is_universe
< 0)
7244 is_universe
= isl_map_plain_is_universe(map2
);
7245 if (is_universe
< 0)
7252 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
7253 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
7254 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
7256 map
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
7257 map1
->n
+ map2
->n
, flags
);
7260 for (i
= 0; i
< map1
->n
; ++i
) {
7261 map
= isl_map_add_basic_map(map
,
7262 isl_basic_map_copy(map1
->p
[i
]));
7266 for (i
= 0; i
< map2
->n
; ++i
) {
7267 map
= isl_map_add_basic_map(map
,
7268 isl_basic_map_copy(map2
->p
[i
]));
7282 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7283 * guaranteed to be disjoint by the caller.
7285 * Note that this functions is called from within isl_map_make_disjoint,
7286 * so we have to be careful not to touch the constraints of the inputs
7289 __isl_give isl_map
*isl_map_union_disjoint(__isl_take isl_map
*map1
,
7290 __isl_take isl_map
*map2
)
7292 return isl_map_align_params_map_map_and(map1
, map2
, &map_union_disjoint
);
7295 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7296 * not be disjoint. The parameters are assumed to have been aligned.
7298 * We currently simply call map_union_disjoint, the internal operation
7299 * of which does not really depend on the inputs being disjoint.
7300 * If the result contains more than one basic map, then we clear
7301 * the disjoint flag since the result may contain basic maps from
7302 * both inputs and these are not guaranteed to be disjoint.
7304 * As a special case, if "map1" and "map2" are obviously equal,
7305 * then we simply return "map1".
7307 static __isl_give isl_map
*map_union_aligned(__isl_take isl_map
*map1
,
7308 __isl_take isl_map
*map2
)
7315 equal
= isl_map_plain_is_equal(map1
, map2
);
7323 map1
= map_union_disjoint(map1
, map2
);
7327 ISL_F_CLR(map1
, ISL_MAP_DISJOINT
);
7335 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7338 __isl_give isl_map
*isl_map_union(__isl_take isl_map
*map1
,
7339 __isl_take isl_map
*map2
)
7341 return isl_map_align_params_map_map_and(map1
, map2
, &map_union_aligned
);
7344 struct isl_set
*isl_set_union_disjoint(
7345 struct isl_set
*set1
, struct isl_set
*set2
)
7347 return set_from_map(isl_map_union_disjoint(set_to_map(set1
),
7351 struct isl_set
*isl_set_union(struct isl_set
*set1
, struct isl_set
*set2
)
7353 return set_from_map(isl_map_union(set_to_map(set1
), set_to_map(set2
)));
7356 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7359 * "map" and "set" are assumed to be compatible and non-NULL.
7361 static __isl_give isl_map
*map_intersect_set(__isl_take isl_map
*map
,
7362 __isl_take isl_set
*set
,
7363 __isl_give isl_basic_map
*fn(__isl_take isl_basic_map
*bmap
,
7364 __isl_take isl_basic_set
*bset
))
7367 struct isl_map
*result
;
7370 if (isl_set_plain_is_universe(set
)) {
7375 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
) &&
7376 ISL_F_ISSET(set
, ISL_MAP_DISJOINT
))
7377 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
7379 result
= isl_map_alloc_space(isl_space_copy(map
->dim
),
7380 map
->n
* set
->n
, flags
);
7381 for (i
= 0; result
&& i
< map
->n
; ++i
)
7382 for (j
= 0; j
< set
->n
; ++j
) {
7383 result
= isl_map_add_basic_map(result
,
7384 fn(isl_basic_map_copy(map
->p
[i
]),
7385 isl_basic_set_copy(set
->p
[j
])));
7395 static __isl_give isl_map
*map_intersect_range(__isl_take isl_map
*map
,
7396 __isl_take isl_set
*set
)
7400 ok
= isl_map_compatible_range(map
, set
);
7404 isl_die(set
->ctx
, isl_error_invalid
,
7405 "incompatible spaces", goto error
);
7407 return map_intersect_set(map
, set
, &isl_basic_map_intersect_range
);
7414 __isl_give isl_map
*isl_map_intersect_range(__isl_take isl_map
*map
,
7415 __isl_take isl_set
*set
)
7417 return isl_map_align_params_map_map_and(map
, set
, &map_intersect_range
);
7420 static __isl_give isl_map
*map_intersect_domain(__isl_take isl_map
*map
,
7421 __isl_take isl_set
*set
)
7425 ok
= isl_map_compatible_domain(map
, set
);
7429 isl_die(set
->ctx
, isl_error_invalid
,
7430 "incompatible spaces", goto error
);
7432 return map_intersect_set(map
, set
, &isl_basic_map_intersect_domain
);
7439 __isl_give isl_map
*isl_map_intersect_domain(__isl_take isl_map
*map
,
7440 __isl_take isl_set
*set
)
7442 return isl_map_align_params_map_map_and(map
, set
,
7443 &map_intersect_domain
);
7446 static __isl_give isl_map
*map_apply_domain(__isl_take isl_map
*map1
,
7447 __isl_take isl_map
*map2
)
7451 map1
= isl_map_reverse(map1
);
7452 map1
= isl_map_apply_range(map1
, map2
);
7453 return isl_map_reverse(map1
);
7460 __isl_give isl_map
*isl_map_apply_domain(__isl_take isl_map
*map1
,
7461 __isl_take isl_map
*map2
)
7463 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_domain
);
7466 static __isl_give isl_map
*map_apply_range(__isl_take isl_map
*map1
,
7467 __isl_take isl_map
*map2
)
7469 isl_space
*dim_result
;
7470 struct isl_map
*result
;
7476 dim_result
= isl_space_join(isl_space_copy(map1
->dim
),
7477 isl_space_copy(map2
->dim
));
7479 result
= isl_map_alloc_space(dim_result
, map1
->n
* map2
->n
, 0);
7482 for (i
= 0; i
< map1
->n
; ++i
)
7483 for (j
= 0; j
< map2
->n
; ++j
) {
7484 result
= isl_map_add_basic_map(result
,
7485 isl_basic_map_apply_range(
7486 isl_basic_map_copy(map1
->p
[i
]),
7487 isl_basic_map_copy(map2
->p
[j
])));
7493 if (result
&& result
->n
<= 1)
7494 ISL_F_SET(result
, ISL_MAP_DISJOINT
);
7502 __isl_give isl_map
*isl_map_apply_range(__isl_take isl_map
*map1
,
7503 __isl_take isl_map
*map2
)
7505 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_range
);
7509 * returns range - domain
7511 struct isl_basic_set
*isl_basic_map_deltas(struct isl_basic_map
*bmap
)
7513 isl_space
*target_space
;
7514 struct isl_basic_set
*bset
;
7521 isl_assert(bmap
->ctx
, isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
7522 bmap
->dim
, isl_dim_out
),
7524 target_space
= isl_space_domain(isl_basic_map_get_space(bmap
));
7525 dim
= isl_basic_map_n_in(bmap
);
7526 nparam
= isl_basic_map_n_param(bmap
);
7527 bmap
= isl_basic_map_from_range(isl_basic_map_wrap(bmap
));
7528 bmap
= isl_basic_map_add_dims(bmap
, isl_dim_in
, dim
);
7529 bmap
= isl_basic_map_extend_constraints(bmap
, dim
, 0);
7530 for (i
= 0; i
< dim
; ++i
) {
7531 int j
= isl_basic_map_alloc_equality(bmap
);
7533 bmap
= isl_basic_map_free(bmap
);
7536 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
7537 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
7538 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], 1);
7539 isl_int_set_si(bmap
->eq
[j
][1+nparam
+2*dim
+i
], -1);
7541 bset
= isl_basic_map_domain(bmap
);
7542 bset
= isl_basic_set_reset_space(bset
, target_space
);
7545 isl_basic_map_free(bmap
);
7550 * returns range - domain
7552 __isl_give isl_set
*isl_map_deltas(__isl_take isl_map
*map
)
7556 struct isl_set
*result
;
7561 isl_assert(map
->ctx
, isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
7562 map
->dim
, isl_dim_out
),
7564 dim
= isl_map_get_space(map
);
7565 dim
= isl_space_domain(dim
);
7566 result
= isl_set_alloc_space(dim
, map
->n
, 0);
7569 for (i
= 0; i
< map
->n
; ++i
)
7570 result
= isl_set_add_basic_set(result
,
7571 isl_basic_map_deltas(isl_basic_map_copy(map
->p
[i
])));
7580 * returns [domain -> range] -> range - domain
7582 __isl_give isl_basic_map
*isl_basic_map_deltas_map(
7583 __isl_take isl_basic_map
*bmap
)
7587 isl_basic_map
*domain
;
7591 if (!isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
7592 bmap
->dim
, isl_dim_out
))
7593 isl_die(bmap
->ctx
, isl_error_invalid
,
7594 "domain and range don't match", goto error
);
7596 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
7597 n
= isl_basic_map_dim(bmap
, isl_dim_in
);
7599 dim
= isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap
)));
7600 domain
= isl_basic_map_universe(dim
);
7602 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
7603 bmap
= isl_basic_map_apply_range(bmap
, domain
);
7604 bmap
= isl_basic_map_extend_constraints(bmap
, n
, 0);
7606 total
= isl_basic_map_total_dim(bmap
);
7608 for (i
= 0; i
< n
; ++i
) {
7609 k
= isl_basic_map_alloc_equality(bmap
);
7612 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
7613 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], 1);
7614 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ i
], -1);
7615 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ n
+ i
], 1);
7618 bmap
= isl_basic_map_gauss(bmap
, NULL
);
7619 return isl_basic_map_finalize(bmap
);
7621 isl_basic_map_free(bmap
);
7626 * returns [domain -> range] -> range - domain
7628 __isl_give isl_map
*isl_map_deltas_map(__isl_take isl_map
*map
)
7631 isl_space
*domain_dim
;
7636 if (!isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
7637 map
->dim
, isl_dim_out
))
7638 isl_die(map
->ctx
, isl_error_invalid
,
7639 "domain and range don't match", goto error
);
7641 map
= isl_map_cow(map
);
7645 domain_dim
= isl_space_from_range(isl_space_domain(isl_map_get_space(map
)));
7646 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
7647 map
->dim
= isl_space_join(map
->dim
, domain_dim
);
7650 for (i
= 0; i
< map
->n
; ++i
) {
7651 map
->p
[i
] = isl_basic_map_deltas_map(map
->p
[i
]);
7655 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
7662 static __isl_give isl_basic_map
*basic_map_identity(__isl_take isl_space
*dims
)
7664 struct isl_basic_map
*bmap
;
7672 nparam
= dims
->nparam
;
7674 bmap
= isl_basic_map_alloc_space(dims
, 0, dim
, 0);
7678 for (i
= 0; i
< dim
; ++i
) {
7679 int j
= isl_basic_map_alloc_equality(bmap
);
7682 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
7683 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
7684 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], -1);
7686 return isl_basic_map_finalize(bmap
);
7688 isl_basic_map_free(bmap
);
7692 __isl_give isl_basic_map
*isl_basic_map_identity(__isl_take isl_space
*dim
)
7696 if (dim
->n_in
!= dim
->n_out
)
7697 isl_die(dim
->ctx
, isl_error_invalid
,
7698 "number of input and output dimensions needs to be "
7699 "the same", goto error
);
7700 return basic_map_identity(dim
);
7702 isl_space_free(dim
);
7706 __isl_give isl_map
*isl_map_identity(__isl_take isl_space
*dim
)
7708 return isl_map_from_basic_map(isl_basic_map_identity(dim
));
7711 __isl_give isl_map
*isl_set_identity(__isl_take isl_set
*set
)
7713 isl_space
*dim
= isl_set_get_space(set
);
7715 id
= isl_map_identity(isl_space_map_from_set(dim
));
7716 return isl_map_intersect_range(id
, set
);
7719 /* Construct a basic set with all set dimensions having only non-negative
7722 __isl_give isl_basic_set
*isl_basic_set_positive_orthant(
7723 __isl_take isl_space
*space
)
7728 struct isl_basic_set
*bset
;
7732 nparam
= space
->nparam
;
7734 bset
= isl_basic_set_alloc_space(space
, 0, 0, dim
);
7737 for (i
= 0; i
< dim
; ++i
) {
7738 int k
= isl_basic_set_alloc_inequality(bset
);
7741 isl_seq_clr(bset
->ineq
[k
], 1 + isl_basic_set_total_dim(bset
));
7742 isl_int_set_si(bset
->ineq
[k
][1 + nparam
+ i
], 1);
7746 isl_basic_set_free(bset
);
7750 /* Construct the half-space x_pos >= 0.
7752 static __isl_give isl_basic_set
*nonneg_halfspace(__isl_take isl_space
*dim
,
7756 isl_basic_set
*nonneg
;
7758 nonneg
= isl_basic_set_alloc_space(dim
, 0, 0, 1);
7759 k
= isl_basic_set_alloc_inequality(nonneg
);
7762 isl_seq_clr(nonneg
->ineq
[k
], 1 + isl_basic_set_total_dim(nonneg
));
7763 isl_int_set_si(nonneg
->ineq
[k
][pos
], 1);
7765 return isl_basic_set_finalize(nonneg
);
7767 isl_basic_set_free(nonneg
);
7771 /* Construct the half-space x_pos <= -1.
7773 static __isl_give isl_basic_set
*neg_halfspace(__isl_take isl_space
*dim
, int pos
)
7778 neg
= isl_basic_set_alloc_space(dim
, 0, 0, 1);
7779 k
= isl_basic_set_alloc_inequality(neg
);
7782 isl_seq_clr(neg
->ineq
[k
], 1 + isl_basic_set_total_dim(neg
));
7783 isl_int_set_si(neg
->ineq
[k
][0], -1);
7784 isl_int_set_si(neg
->ineq
[k
][pos
], -1);
7786 return isl_basic_set_finalize(neg
);
7788 isl_basic_set_free(neg
);
7792 __isl_give isl_set
*isl_set_split_dims(__isl_take isl_set
*set
,
7793 enum isl_dim_type type
, unsigned first
, unsigned n
)
7797 isl_basic_set
*nonneg
;
7805 isl_assert(set
->ctx
, first
+ n
<= isl_set_dim(set
, type
), goto error
);
7807 offset
= pos(set
->dim
, type
);
7808 for (i
= 0; i
< n
; ++i
) {
7809 nonneg
= nonneg_halfspace(isl_set_get_space(set
),
7810 offset
+ first
+ i
);
7811 neg
= neg_halfspace(isl_set_get_space(set
), offset
+ first
+ i
);
7813 set
= isl_set_intersect(set
, isl_basic_set_union(nonneg
, neg
));
7822 static isl_stat
foreach_orthant(__isl_take isl_set
*set
, int *signs
, int first
,
7824 isl_stat (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
7830 return isl_stat_error
;
7831 if (isl_set_plain_is_empty(set
)) {
7836 return fn(set
, signs
, user
);
7839 half
= isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set
),
7841 half
= isl_set_intersect(half
, isl_set_copy(set
));
7842 if (foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
) < 0)
7846 half
= isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set
),
7848 half
= isl_set_intersect(half
, set
);
7849 return foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
);
7852 return isl_stat_error
;
7855 /* Call "fn" on the intersections of "set" with each of the orthants
7856 * (except for obviously empty intersections). The orthant is identified
7857 * by the signs array, with each entry having value 1 or -1 according
7858 * to the sign of the corresponding variable.
7860 isl_stat
isl_set_foreach_orthant(__isl_keep isl_set
*set
,
7861 isl_stat (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
7870 return isl_stat_error
;
7871 if (isl_set_plain_is_empty(set
))
7874 nparam
= isl_set_dim(set
, isl_dim_param
);
7875 nvar
= isl_set_dim(set
, isl_dim_set
);
7877 signs
= isl_alloc_array(set
->ctx
, int, nparam
+ nvar
);
7879 r
= foreach_orthant(isl_set_copy(set
), signs
, 0, nparam
+ nvar
,
7887 isl_bool
isl_set_is_equal(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
7889 return isl_map_is_equal(set_to_map(set1
), set_to_map(set2
));
7892 isl_bool
isl_basic_map_is_subset(__isl_keep isl_basic_map
*bmap1
,
7893 __isl_keep isl_basic_map
*bmap2
)
7896 struct isl_map
*map1
;
7897 struct isl_map
*map2
;
7899 if (!bmap1
|| !bmap2
)
7900 return isl_bool_error
;
7902 map1
= isl_map_from_basic_map(isl_basic_map_copy(bmap1
));
7903 map2
= isl_map_from_basic_map(isl_basic_map_copy(bmap2
));
7905 is_subset
= isl_map_is_subset(map1
, map2
);
7913 isl_bool
isl_basic_set_is_subset(__isl_keep isl_basic_set
*bset1
,
7914 __isl_keep isl_basic_set
*bset2
)
7916 return isl_basic_map_is_subset(bset1
, bset2
);
7919 isl_bool
isl_basic_map_is_equal(__isl_keep isl_basic_map
*bmap1
,
7920 __isl_keep isl_basic_map
*bmap2
)
7924 if (!bmap1
|| !bmap2
)
7925 return isl_bool_error
;
7926 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
7927 if (is_subset
!= isl_bool_true
)
7929 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
7933 isl_bool
isl_basic_set_is_equal(__isl_keep isl_basic_set
*bset1
,
7934 __isl_keep isl_basic_set
*bset2
)
7936 return isl_basic_map_is_equal(
7937 bset_to_bmap(bset1
), bset_to_bmap(bset2
));
7940 isl_bool
isl_map_is_empty(__isl_keep isl_map
*map
)
7946 return isl_bool_error
;
7947 for (i
= 0; i
< map
->n
; ++i
) {
7948 is_empty
= isl_basic_map_is_empty(map
->p
[i
]);
7950 return isl_bool_error
;
7952 return isl_bool_false
;
7954 return isl_bool_true
;
7957 isl_bool
isl_map_plain_is_empty(__isl_keep isl_map
*map
)
7959 return map
? map
->n
== 0 : isl_bool_error
;
7962 isl_bool
isl_set_plain_is_empty(__isl_keep isl_set
*set
)
7964 return set
? set
->n
== 0 : isl_bool_error
;
7967 isl_bool
isl_set_is_empty(__isl_keep isl_set
*set
)
7969 return isl_map_is_empty(set_to_map(set
));
7972 isl_bool
isl_map_has_equal_space(__isl_keep isl_map
*map1
,
7973 __isl_keep isl_map
*map2
)
7976 return isl_bool_error
;
7978 return isl_space_is_equal(map1
->dim
, map2
->dim
);
7981 isl_bool
isl_set_has_equal_space(__isl_keep isl_set
*set1
,
7982 __isl_keep isl_set
*set2
)
7985 return isl_bool_error
;
7987 return isl_space_is_equal(set1
->dim
, set2
->dim
);
7990 static isl_bool
map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
7995 return isl_bool_error
;
7996 is_subset
= isl_map_is_subset(map1
, map2
);
7997 if (is_subset
!= isl_bool_true
)
7999 is_subset
= isl_map_is_subset(map2
, map1
);
8003 /* Is "map1" equal to "map2"?
8005 * First check if they are obviously equal.
8006 * If not, then perform a more detailed analysis.
8008 isl_bool
isl_map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
8012 equal
= isl_map_plain_is_equal(map1
, map2
);
8013 if (equal
< 0 || equal
)
8015 return isl_map_align_params_map_map_and_test(map1
, map2
, &map_is_equal
);
8018 isl_bool
isl_basic_map_is_strict_subset(
8019 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
8023 if (!bmap1
|| !bmap2
)
8024 return isl_bool_error
;
8025 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
8026 if (is_subset
!= isl_bool_true
)
8028 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
8029 if (is_subset
== isl_bool_error
)
8034 isl_bool
isl_map_is_strict_subset(__isl_keep isl_map
*map1
,
8035 __isl_keep isl_map
*map2
)
8040 return isl_bool_error
;
8041 is_subset
= isl_map_is_subset(map1
, map2
);
8042 if (is_subset
!= isl_bool_true
)
8044 is_subset
= isl_map_is_subset(map2
, map1
);
8045 if (is_subset
== isl_bool_error
)
8050 isl_bool
isl_set_is_strict_subset(__isl_keep isl_set
*set1
,
8051 __isl_keep isl_set
*set2
)
8053 return isl_map_is_strict_subset(set_to_map(set1
), set_to_map(set2
));
8056 /* Is "bmap" obviously equal to the universe with the same space?
8058 * That is, does it not have any constraints?
8060 isl_bool
isl_basic_map_plain_is_universe(__isl_keep isl_basic_map
*bmap
)
8063 return isl_bool_error
;
8064 return bmap
->n_eq
== 0 && bmap
->n_ineq
== 0;
8067 /* Is "bset" obviously equal to the universe with the same space?
8069 isl_bool
isl_basic_set_plain_is_universe(__isl_keep isl_basic_set
*bset
)
8071 return isl_basic_map_plain_is_universe(bset
);
8074 /* If "c" does not involve any existentially quantified variables,
8075 * then set *univ to false and abort
8077 static isl_stat
involves_divs(__isl_take isl_constraint
*c
, void *user
)
8079 isl_bool
*univ
= user
;
8082 n
= isl_constraint_dim(c
, isl_dim_div
);
8083 *univ
= isl_constraint_involves_dims(c
, isl_dim_div
, 0, n
);
8084 isl_constraint_free(c
);
8085 if (*univ
< 0 || !*univ
)
8086 return isl_stat_error
;
8090 /* Is "bmap" equal to the universe with the same space?
8092 * First check if it is obviously equal to the universe.
8093 * If not and if there are any constraints not involving
8094 * existentially quantified variables, then it is certainly
8095 * not equal to the universe.
8096 * Otherwise, check if the universe is a subset of "bmap".
8098 isl_bool
isl_basic_map_is_universe(__isl_keep isl_basic_map
*bmap
)
8101 isl_basic_map
*test
;
8103 univ
= isl_basic_map_plain_is_universe(bmap
);
8104 if (univ
< 0 || univ
)
8106 if (isl_basic_map_dim(bmap
, isl_dim_div
) == 0)
8107 return isl_bool_false
;
8108 univ
= isl_bool_true
;
8109 if (isl_basic_map_foreach_constraint(bmap
, &involves_divs
, &univ
) < 0 &&
8111 return isl_bool_error
;
8112 if (univ
< 0 || !univ
)
8114 test
= isl_basic_map_universe(isl_basic_map_get_space(bmap
));
8115 univ
= isl_basic_map_is_subset(test
, bmap
);
8116 isl_basic_map_free(test
);
8120 /* Is "bset" equal to the universe with the same space?
8122 isl_bool
isl_basic_set_is_universe(__isl_keep isl_basic_set
*bset
)
8124 return isl_basic_map_is_universe(bset
);
8127 isl_bool
isl_map_plain_is_universe(__isl_keep isl_map
*map
)
8132 return isl_bool_error
;
8134 for (i
= 0; i
< map
->n
; ++i
) {
8135 isl_bool r
= isl_basic_map_plain_is_universe(map
->p
[i
]);
8140 return isl_bool_false
;
8143 isl_bool
isl_set_plain_is_universe(__isl_keep isl_set
*set
)
8145 return isl_map_plain_is_universe(set_to_map(set
));
8148 isl_bool
isl_basic_map_is_empty(__isl_keep isl_basic_map
*bmap
)
8150 struct isl_basic_set
*bset
= NULL
;
8151 struct isl_vec
*sample
= NULL
;
8152 isl_bool empty
, non_empty
;
8155 return isl_bool_error
;
8157 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
8158 return isl_bool_true
;
8160 if (isl_basic_map_plain_is_universe(bmap
))
8161 return isl_bool_false
;
8163 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
8164 struct isl_basic_map
*copy
= isl_basic_map_copy(bmap
);
8165 copy
= isl_basic_map_remove_redundancies(copy
);
8166 empty
= isl_basic_map_plain_is_empty(copy
);
8167 isl_basic_map_free(copy
);
8171 non_empty
= isl_basic_map_plain_is_non_empty(bmap
);
8173 return isl_bool_error
;
8175 return isl_bool_false
;
8176 isl_vec_free(bmap
->sample
);
8177 bmap
->sample
= NULL
;
8178 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
8180 return isl_bool_error
;
8181 sample
= isl_basic_set_sample_vec(bset
);
8183 return isl_bool_error
;
8184 empty
= sample
->size
== 0;
8185 isl_vec_free(bmap
->sample
);
8186 bmap
->sample
= sample
;
8188 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
8193 isl_bool
isl_basic_map_plain_is_empty(__isl_keep isl_basic_map
*bmap
)
8196 return isl_bool_error
;
8197 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
);
8200 isl_bool
isl_basic_set_plain_is_empty(__isl_keep isl_basic_set
*bset
)
8203 return isl_bool_error
;
8204 return ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
);
8207 /* Is "bmap" known to be non-empty?
8209 * That is, is the cached sample still valid?
8211 isl_bool
isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map
*bmap
)
8216 return isl_bool_error
;
8218 return isl_bool_false
;
8219 total
= 1 + isl_basic_map_total_dim(bmap
);
8220 if (bmap
->sample
->size
!= total
)
8221 return isl_bool_false
;
8222 return isl_basic_map_contains(bmap
, bmap
->sample
);
8225 isl_bool
isl_basic_set_is_empty(__isl_keep isl_basic_set
*bset
)
8227 return isl_basic_map_is_empty(bset_to_bmap(bset
));
8230 struct isl_map
*isl_basic_map_union(
8231 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
8233 struct isl_map
*map
;
8234 if (!bmap1
|| !bmap2
)
8237 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
8239 map
= isl_map_alloc_space(isl_space_copy(bmap1
->dim
), 2, 0);
8242 map
= isl_map_add_basic_map(map
, bmap1
);
8243 map
= isl_map_add_basic_map(map
, bmap2
);
8246 isl_basic_map_free(bmap1
);
8247 isl_basic_map_free(bmap2
);
8251 struct isl_set
*isl_basic_set_union(
8252 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
8254 return set_from_map(isl_basic_map_union(bset_to_bmap(bset1
),
8255 bset_to_bmap(bset2
)));
8258 /* Order divs such that any div only depends on previous divs */
8259 struct isl_basic_map
*isl_basic_map_order_divs(struct isl_basic_map
*bmap
)
8267 off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
8269 for (i
= 0; i
< bmap
->n_div
; ++i
) {
8271 if (isl_int_is_zero(bmap
->div
[i
][0]))
8273 pos
= isl_seq_first_non_zero(bmap
->div
[i
]+1+1+off
+i
,
8278 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_internal
,
8279 "integer division depends on itself",
8280 return isl_basic_map_free(bmap
));
8281 isl_basic_map_swap_div(bmap
, i
, i
+ pos
);
8287 struct isl_basic_set
*isl_basic_set_order_divs(struct isl_basic_set
*bset
)
8289 return bset_from_bmap(isl_basic_map_order_divs(bset_to_bmap(bset
)));
8292 __isl_give isl_map
*isl_map_order_divs(__isl_take isl_map
*map
)
8299 for (i
= 0; i
< map
->n
; ++i
) {
8300 map
->p
[i
] = isl_basic_map_order_divs(map
->p
[i
]);
8311 /* Sort the local variables of "bset".
8313 __isl_give isl_basic_set
*isl_basic_set_sort_divs(
8314 __isl_take isl_basic_set
*bset
)
8316 return bset_from_bmap(isl_basic_map_sort_divs(bset_to_bmap(bset
)));
8319 /* Apply the expansion computed by isl_merge_divs.
8320 * The expansion itself is given by "exp" while the resulting
8321 * list of divs is given by "div".
8323 * Move the integer divisions of "bmap" into the right position
8324 * according to "exp" and then introduce the additional integer
8325 * divisions, adding div constraints.
8326 * The moving should be done first to avoid moving coefficients
8327 * in the definitions of the extra integer divisions.
8329 __isl_give isl_basic_map
*isl_basic_map_expand_divs(
8330 __isl_take isl_basic_map
*bmap
, __isl_take isl_mat
*div
, int *exp
)
8335 bmap
= isl_basic_map_cow(bmap
);
8339 if (div
->n_row
< bmap
->n_div
)
8340 isl_die(isl_mat_get_ctx(div
), isl_error_invalid
,
8341 "not an expansion", goto error
);
8343 n_div
= bmap
->n_div
;
8344 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
8345 div
->n_row
- n_div
, 0,
8346 2 * (div
->n_row
- n_div
));
8348 for (i
= n_div
; i
< div
->n_row
; ++i
)
8349 if (isl_basic_map_alloc_div(bmap
) < 0)
8352 for (j
= n_div
- 1; j
>= 0; --j
) {
8355 isl_basic_map_swap_div(bmap
, j
, exp
[j
]);
8358 for (i
= 0; i
< div
->n_row
; ++i
) {
8359 if (j
< n_div
&& exp
[j
] == i
) {
8362 isl_seq_cpy(bmap
->div
[i
], div
->row
[i
], div
->n_col
);
8363 if (isl_basic_map_div_is_marked_unknown(bmap
, i
))
8365 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
8373 isl_basic_map_free(bmap
);
8378 /* Apply the expansion computed by isl_merge_divs.
8379 * The expansion itself is given by "exp" while the resulting
8380 * list of divs is given by "div".
8382 __isl_give isl_basic_set
*isl_basic_set_expand_divs(
8383 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*div
, int *exp
)
8385 return isl_basic_map_expand_divs(bset
, div
, exp
);
8388 /* Look for a div in dst that corresponds to the div "div" in src.
8389 * The divs before "div" in src and dst are assumed to be the same.
8391 * Returns -1 if no corresponding div was found and the position
8392 * of the corresponding div in dst otherwise.
8394 static int find_div(struct isl_basic_map
*dst
,
8395 struct isl_basic_map
*src
, unsigned div
)
8399 unsigned total
= isl_space_dim(src
->dim
, isl_dim_all
);
8401 isl_assert(dst
->ctx
, div
<= dst
->n_div
, return -1);
8402 for (i
= div
; i
< dst
->n_div
; ++i
)
8403 if (isl_seq_eq(dst
->div
[i
], src
->div
[div
], 1+1+total
+div
) &&
8404 isl_seq_first_non_zero(dst
->div
[i
]+1+1+total
+div
,
8405 dst
->n_div
- div
) == -1)
8410 /* Align the divs of "dst" to those of "src", adding divs from "src"
8411 * if needed. That is, make sure that the first src->n_div divs
8412 * of the result are equal to those of src.
8414 * The result is not finalized as by design it will have redundant
8415 * divs if any divs from "src" were copied.
8417 __isl_give isl_basic_map
*isl_basic_map_align_divs(
8418 __isl_take isl_basic_map
*dst
, __isl_keep isl_basic_map
*src
)
8421 int known
, extended
;
8425 return isl_basic_map_free(dst
);
8427 if (src
->n_div
== 0)
8430 known
= isl_basic_map_divs_known(src
);
8432 return isl_basic_map_free(dst
);
8434 isl_die(isl_basic_map_get_ctx(src
), isl_error_invalid
,
8435 "some src divs are unknown",
8436 return isl_basic_map_free(dst
));
8438 src
= isl_basic_map_order_divs(src
);
8441 total
= isl_space_dim(src
->dim
, isl_dim_all
);
8442 for (i
= 0; i
< src
->n_div
; ++i
) {
8443 int j
= find_div(dst
, src
, i
);
8446 int extra
= src
->n_div
- i
;
8447 dst
= isl_basic_map_cow(dst
);
8450 dst
= isl_basic_map_extend_space(dst
,
8451 isl_space_copy(dst
->dim
),
8452 extra
, 0, 2 * extra
);
8455 j
= isl_basic_map_alloc_div(dst
);
8457 return isl_basic_map_free(dst
);
8458 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
+i
);
8459 isl_seq_clr(dst
->div
[j
]+1+1+total
+i
, dst
->n_div
- i
);
8460 if (isl_basic_map_add_div_constraints(dst
, j
) < 0)
8461 return isl_basic_map_free(dst
);
8464 isl_basic_map_swap_div(dst
, i
, j
);
8469 struct isl_map
*isl_map_align_divs(struct isl_map
*map
)
8477 map
= isl_map_compute_divs(map
);
8478 map
= isl_map_cow(map
);
8482 for (i
= 1; i
< map
->n
; ++i
)
8483 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], map
->p
[i
]);
8484 for (i
= 1; i
< map
->n
; ++i
) {
8485 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], map
->p
[0]);
8487 return isl_map_free(map
);
8490 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
8494 struct isl_set
*isl_set_align_divs(struct isl_set
*set
)
8496 return set_from_map(isl_map_align_divs(set_to_map(set
)));
8499 /* Align the divs of the basic maps in "map" to those
8500 * of the basic maps in "list", as well as to the other basic maps in "map".
8501 * The elements in "list" are assumed to have known divs.
8503 __isl_give isl_map
*isl_map_align_divs_to_basic_map_list(
8504 __isl_take isl_map
*map
, __isl_keep isl_basic_map_list
*list
)
8508 map
= isl_map_compute_divs(map
);
8509 map
= isl_map_cow(map
);
8511 return isl_map_free(map
);
8515 n
= isl_basic_map_list_n_basic_map(list
);
8516 for (i
= 0; i
< n
; ++i
) {
8517 isl_basic_map
*bmap
;
8519 bmap
= isl_basic_map_list_get_basic_map(list
, i
);
8520 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], bmap
);
8521 isl_basic_map_free(bmap
);
8524 return isl_map_free(map
);
8526 return isl_map_align_divs(map
);
8529 /* Align the divs of each element of "list" to those of "bmap".
8530 * Both "bmap" and the elements of "list" are assumed to have known divs.
8532 __isl_give isl_basic_map_list
*isl_basic_map_list_align_divs_to_basic_map(
8533 __isl_take isl_basic_map_list
*list
, __isl_keep isl_basic_map
*bmap
)
8538 return isl_basic_map_list_free(list
);
8540 n
= isl_basic_map_list_n_basic_map(list
);
8541 for (i
= 0; i
< n
; ++i
) {
8542 isl_basic_map
*bmap_i
;
8544 bmap_i
= isl_basic_map_list_get_basic_map(list
, i
);
8545 bmap_i
= isl_basic_map_align_divs(bmap_i
, bmap
);
8546 list
= isl_basic_map_list_set_basic_map(list
, i
, bmap_i
);
8552 static __isl_give isl_set
*set_apply( __isl_take isl_set
*set
,
8553 __isl_take isl_map
*map
)
8557 ok
= isl_map_compatible_domain(map
, set
);
8561 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
8562 "incompatible spaces", goto error
);
8563 map
= isl_map_intersect_domain(map
, set
);
8564 set
= isl_map_range(map
);
8572 __isl_give isl_set
*isl_set_apply( __isl_take isl_set
*set
,
8573 __isl_take isl_map
*map
)
8575 return isl_map_align_params_map_map_and(set
, map
, &set_apply
);
8578 /* There is no need to cow as removing empty parts doesn't change
8579 * the meaning of the set.
8581 struct isl_map
*isl_map_remove_empty_parts(struct isl_map
*map
)
8588 for (i
= map
->n
- 1; i
>= 0; --i
)
8589 remove_if_empty(map
, i
);
8594 struct isl_set
*isl_set_remove_empty_parts(struct isl_set
*set
)
8596 return set_from_map(isl_map_remove_empty_parts(set_to_map(set
)));
8599 static __isl_give isl_basic_map
*map_copy_basic_map(__isl_keep isl_map
*map
)
8601 struct isl_basic_map
*bmap
;
8602 if (!map
|| map
->n
== 0)
8604 bmap
= map
->p
[map
->n
-1];
8605 isl_assert(map
->ctx
, ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
), return NULL
);
8606 return isl_basic_map_copy(bmap
);
8609 __isl_give isl_basic_map
*isl_map_copy_basic_map(__isl_keep isl_map
*map
)
8611 return map_copy_basic_map(map
);
8614 struct isl_basic_set
*isl_set_copy_basic_set(struct isl_set
*set
)
8616 return bset_from_bmap(map_copy_basic_map(set_to_map(set
)));
8619 static __isl_give isl_map
*map_drop_basic_map(__isl_take isl_map
*map
,
8620 __isl_keep isl_basic_map
*bmap
)
8626 for (i
= map
->n
-1; i
>= 0; --i
) {
8627 if (map
->p
[i
] != bmap
)
8629 map
= isl_map_cow(map
);
8632 isl_basic_map_free(map
->p
[i
]);
8633 if (i
!= map
->n
-1) {
8634 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
8635 map
->p
[i
] = map
->p
[map
->n
-1];
8646 __isl_give isl_map
*isl_map_drop_basic_map(__isl_take isl_map
*map
,
8647 __isl_keep isl_basic_map
*bmap
)
8649 return map_drop_basic_map(map
, bmap
);
8652 struct isl_set
*isl_set_drop_basic_set(struct isl_set
*set
,
8653 struct isl_basic_set
*bset
)
8655 return set_from_map(map_drop_basic_map(set_to_map(set
),
8656 bset_to_bmap(bset
)));
8659 /* Given two basic sets bset1 and bset2, compute the maximal difference
8660 * between the values of dimension pos in bset1 and those in bset2
8661 * for any common value of the parameters and dimensions preceding pos.
8663 static enum isl_lp_result
basic_set_maximal_difference_at(
8664 __isl_keep isl_basic_set
*bset1
, __isl_keep isl_basic_set
*bset2
,
8665 int pos
, isl_int
*opt
)
8667 isl_basic_map
*bmap1
;
8668 isl_basic_map
*bmap2
;
8669 struct isl_ctx
*ctx
;
8670 struct isl_vec
*obj
;
8674 enum isl_lp_result res
;
8676 if (!bset1
|| !bset2
)
8677 return isl_lp_error
;
8679 nparam
= isl_basic_set_n_param(bset1
);
8680 dim1
= isl_basic_set_n_dim(bset1
);
8682 bmap1
= isl_basic_map_from_range(isl_basic_set_copy(bset1
));
8683 bmap2
= isl_basic_map_from_range(isl_basic_set_copy(bset2
));
8684 bmap1
= isl_basic_map_move_dims(bmap1
, isl_dim_in
, 0,
8685 isl_dim_out
, 0, pos
);
8686 bmap2
= isl_basic_map_move_dims(bmap2
, isl_dim_in
, 0,
8687 isl_dim_out
, 0, pos
);
8688 bmap1
= isl_basic_map_range_product(bmap1
, bmap2
);
8690 return isl_lp_error
;
8692 total
= isl_basic_map_total_dim(bmap1
);
8694 obj
= isl_vec_alloc(ctx
, 1 + total
);
8697 isl_seq_clr(obj
->block
.data
, 1 + total
);
8698 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
], 1);
8699 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
+(dim1
-pos
)], -1);
8700 res
= isl_basic_map_solve_lp(bmap1
, 1, obj
->block
.data
, ctx
->one
,
8702 isl_basic_map_free(bmap1
);
8706 isl_basic_map_free(bmap1
);
8707 return isl_lp_error
;
8710 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
8711 * for any common value of the parameters and dimensions preceding pos
8712 * in both basic sets, the values of dimension pos in bset1 are
8713 * smaller or larger than those in bset2.
8716 * 1 if bset1 follows bset2
8717 * -1 if bset1 precedes bset2
8718 * 0 if bset1 and bset2 are incomparable
8719 * -2 if some error occurred.
8721 int isl_basic_set_compare_at(struct isl_basic_set
*bset1
,
8722 struct isl_basic_set
*bset2
, int pos
)
8725 enum isl_lp_result res
;
8730 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
8732 if (res
== isl_lp_empty
)
8734 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
8735 res
== isl_lp_unbounded
)
8737 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
8746 /* Given two basic sets bset1 and bset2, check whether
8747 * for any common value of the parameters and dimensions preceding pos
8748 * there is a value of dimension pos in bset1 that is larger
8749 * than a value of the same dimension in bset2.
8752 * 1 if there exists such a pair
8753 * 0 if there is no such pair, but there is a pair of equal values
8755 * -2 if some error occurred.
8757 int isl_basic_set_follows_at(__isl_keep isl_basic_set
*bset1
,
8758 __isl_keep isl_basic_set
*bset2
, int pos
)
8761 enum isl_lp_result res
;
8766 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
8768 if (res
== isl_lp_empty
)
8770 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
8771 res
== isl_lp_unbounded
)
8773 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
8775 else if (res
== isl_lp_ok
)
8784 /* Given two sets set1 and set2, check whether
8785 * for any common value of the parameters and dimensions preceding pos
8786 * there is a value of dimension pos in set1 that is larger
8787 * than a value of the same dimension in set2.
8790 * 1 if there exists such a pair
8791 * 0 if there is no such pair, but there is a pair of equal values
8793 * -2 if some error occurred.
8795 int isl_set_follows_at(__isl_keep isl_set
*set1
,
8796 __isl_keep isl_set
*set2
, int pos
)
8804 for (i
= 0; i
< set1
->n
; ++i
)
8805 for (j
= 0; j
< set2
->n
; ++j
) {
8807 f
= isl_basic_set_follows_at(set1
->p
[i
], set2
->p
[j
], pos
);
8808 if (f
== 1 || f
== -2)
8817 static isl_bool
isl_basic_map_plain_has_fixed_var(
8818 __isl_keep isl_basic_map
*bmap
, unsigned pos
, isl_int
*val
)
8825 return isl_bool_error
;
8826 total
= isl_basic_map_total_dim(bmap
);
8827 for (i
= 0, d
= total
-1; i
< bmap
->n_eq
&& d
+1 > pos
; ++i
) {
8828 for (; d
+1 > pos
; --d
)
8829 if (!isl_int_is_zero(bmap
->eq
[i
][1+d
]))
8833 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1, d
) != -1)
8834 return isl_bool_false
;
8835 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1+d
+1, total
-d
-1) != -1)
8836 return isl_bool_false
;
8837 if (!isl_int_is_one(bmap
->eq
[i
][1+d
]))
8838 return isl_bool_false
;
8840 isl_int_neg(*val
, bmap
->eq
[i
][0]);
8841 return isl_bool_true
;
8843 return isl_bool_false
;
8846 static isl_bool
isl_map_plain_has_fixed_var(__isl_keep isl_map
*map
,
8847 unsigned pos
, isl_int
*val
)
8855 return isl_bool_error
;
8857 return isl_bool_false
;
8859 return isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, val
);
8862 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, &v
);
8863 for (i
= 1; fixed
== isl_bool_true
&& i
< map
->n
; ++i
) {
8864 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[i
], pos
, &tmp
);
8865 if (fixed
== isl_bool_true
&& isl_int_ne(tmp
, v
))
8866 fixed
= isl_bool_false
;
8869 isl_int_set(*val
, v
);
8875 static isl_bool
isl_basic_set_plain_has_fixed_var(
8876 __isl_keep isl_basic_set
*bset
, unsigned pos
, isl_int
*val
)
8878 return isl_basic_map_plain_has_fixed_var(bset_to_bmap(bset
),
8882 isl_bool
isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map
*bmap
,
8883 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
8885 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
8886 return isl_bool_error
;
8887 return isl_basic_map_plain_has_fixed_var(bmap
,
8888 isl_basic_map_offset(bmap
, type
) - 1 + pos
, val
);
8891 /* If "bmap" obviously lies on a hyperplane where the given dimension
8892 * has a fixed value, then return that value.
8893 * Otherwise return NaN.
8895 __isl_give isl_val
*isl_basic_map_plain_get_val_if_fixed(
8896 __isl_keep isl_basic_map
*bmap
,
8897 enum isl_dim_type type
, unsigned pos
)
8905 ctx
= isl_basic_map_get_ctx(bmap
);
8906 v
= isl_val_alloc(ctx
);
8909 fixed
= isl_basic_map_plain_is_fixed(bmap
, type
, pos
, &v
->n
);
8911 return isl_val_free(v
);
8913 isl_int_set_si(v
->d
, 1);
8917 return isl_val_nan(ctx
);
8920 isl_bool
isl_map_plain_is_fixed(__isl_keep isl_map
*map
,
8921 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
8923 if (pos
>= isl_map_dim(map
, type
))
8924 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
8925 "position out of bounds", return isl_bool_error
);
8926 return isl_map_plain_has_fixed_var(map
,
8927 map_offset(map
, type
) - 1 + pos
, val
);
8930 /* If "map" obviously lies on a hyperplane where the given dimension
8931 * has a fixed value, then return that value.
8932 * Otherwise return NaN.
8934 __isl_give isl_val
*isl_map_plain_get_val_if_fixed(__isl_keep isl_map
*map
,
8935 enum isl_dim_type type
, unsigned pos
)
8943 ctx
= isl_map_get_ctx(map
);
8944 v
= isl_val_alloc(ctx
);
8947 fixed
= isl_map_plain_is_fixed(map
, type
, pos
, &v
->n
);
8949 return isl_val_free(v
);
8951 isl_int_set_si(v
->d
, 1);
8955 return isl_val_nan(ctx
);
8958 /* If "set" obviously lies on a hyperplane where the given dimension
8959 * has a fixed value, then return that value.
8960 * Otherwise return NaN.
8962 __isl_give isl_val
*isl_set_plain_get_val_if_fixed(__isl_keep isl_set
*set
,
8963 enum isl_dim_type type
, unsigned pos
)
8965 return isl_map_plain_get_val_if_fixed(set
, type
, pos
);
8968 isl_bool
isl_set_plain_is_fixed(__isl_keep isl_set
*set
,
8969 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
8971 return isl_map_plain_is_fixed(set
, type
, pos
, val
);
8974 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8975 * then return this fixed value in *val.
8977 isl_bool
isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set
*bset
,
8978 unsigned dim
, isl_int
*val
)
8980 return isl_basic_set_plain_has_fixed_var(bset
,
8981 isl_basic_set_n_param(bset
) + dim
, val
);
8984 /* Return -1 if the constraint "c1" should be sorted before "c2"
8985 * and 1 if it should be sorted after "c2".
8986 * Return 0 if the two constraints are the same (up to the constant term).
8988 * In particular, if a constraint involves later variables than another
8989 * then it is sorted after this other constraint.
8990 * uset_gist depends on constraints without existentially quantified
8991 * variables sorting first.
8993 * For constraints that have the same latest variable, those
8994 * with the same coefficient for this latest variable (first in absolute value
8995 * and then in actual value) are grouped together.
8996 * This is useful for detecting pairs of constraints that can
8997 * be chained in their printed representation.
8999 * Finally, within a group, constraints are sorted according to
9000 * their coefficients (excluding the constant term).
9002 static int sort_constraint_cmp(const void *p1
, const void *p2
, void *arg
)
9004 isl_int
**c1
= (isl_int
**) p1
;
9005 isl_int
**c2
= (isl_int
**) p2
;
9007 unsigned size
= *(unsigned *) arg
;
9010 l1
= isl_seq_last_non_zero(*c1
+ 1, size
);
9011 l2
= isl_seq_last_non_zero(*c2
+ 1, size
);
9016 cmp
= isl_int_abs_cmp((*c1
)[1 + l1
], (*c2
)[1 + l1
]);
9019 cmp
= isl_int_cmp((*c1
)[1 + l1
], (*c2
)[1 + l1
]);
9023 return isl_seq_cmp(*c1
+ 1, *c2
+ 1, size
);
9026 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
9027 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
9028 * and 0 if the two constraints are the same (up to the constant term).
9030 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map
*bmap
,
9031 isl_int
*c1
, isl_int
*c2
)
9037 total
= isl_basic_map_total_dim(bmap
);
9038 return sort_constraint_cmp(&c1
, &c2
, &total
);
9041 __isl_give isl_basic_map
*isl_basic_map_sort_constraints(
9042 __isl_take isl_basic_map
*bmap
)
9048 if (bmap
->n_ineq
== 0)
9050 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED
))
9052 total
= isl_basic_map_total_dim(bmap
);
9053 if (isl_sort(bmap
->ineq
, bmap
->n_ineq
, sizeof(isl_int
*),
9054 &sort_constraint_cmp
, &total
) < 0)
9055 return isl_basic_map_free(bmap
);
9059 __isl_give isl_basic_set
*isl_basic_set_sort_constraints(
9060 __isl_take isl_basic_set
*bset
)
9062 isl_basic_map
*bmap
= bset_to_bmap(bset
);
9063 return bset_from_bmap(isl_basic_map_sort_constraints(bmap
));
9066 struct isl_basic_map
*isl_basic_map_normalize(struct isl_basic_map
*bmap
)
9070 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED
))
9072 bmap
= isl_basic_map_remove_redundancies(bmap
);
9073 bmap
= isl_basic_map_sort_constraints(bmap
);
9075 ISL_F_SET(bmap
, ISL_BASIC_MAP_NORMALIZED
);
9078 int isl_basic_map_plain_cmp(__isl_keep isl_basic_map
*bmap1
,
9079 __isl_keep isl_basic_map
*bmap2
)
9084 if (!bmap1
|| !bmap2
)
9089 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) !=
9090 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_RATIONAL
))
9091 return ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) ? -1 : 1;
9092 if (isl_basic_map_n_param(bmap1
) != isl_basic_map_n_param(bmap2
))
9093 return isl_basic_map_n_param(bmap1
) - isl_basic_map_n_param(bmap2
);
9094 if (isl_basic_map_n_in(bmap1
) != isl_basic_map_n_in(bmap2
))
9095 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
9096 if (isl_basic_map_n_out(bmap1
) != isl_basic_map_n_out(bmap2
))
9097 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
9098 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
) &&
9099 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
9101 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
))
9103 if (ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
9105 if (bmap1
->n_eq
!= bmap2
->n_eq
)
9106 return bmap1
->n_eq
- bmap2
->n_eq
;
9107 if (bmap1
->n_ineq
!= bmap2
->n_ineq
)
9108 return bmap1
->n_ineq
- bmap2
->n_ineq
;
9109 if (bmap1
->n_div
!= bmap2
->n_div
)
9110 return bmap1
->n_div
- bmap2
->n_div
;
9111 total
= isl_basic_map_total_dim(bmap1
);
9112 for (i
= 0; i
< bmap1
->n_eq
; ++i
) {
9113 cmp
= isl_seq_cmp(bmap1
->eq
[i
], bmap2
->eq
[i
], 1+total
);
9117 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
9118 cmp
= isl_seq_cmp(bmap1
->ineq
[i
], bmap2
->ineq
[i
], 1+total
);
9122 for (i
= 0; i
< bmap1
->n_div
; ++i
) {
9123 cmp
= isl_seq_cmp(bmap1
->div
[i
], bmap2
->div
[i
], 1+1+total
);
9130 int isl_basic_set_plain_cmp(__isl_keep isl_basic_set
*bset1
,
9131 __isl_keep isl_basic_set
*bset2
)
9133 return isl_basic_map_plain_cmp(bset1
, bset2
);
9136 int isl_set_plain_cmp(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
9142 if (set1
->n
!= set2
->n
)
9143 return set1
->n
- set2
->n
;
9145 for (i
= 0; i
< set1
->n
; ++i
) {
9146 cmp
= isl_basic_set_plain_cmp(set1
->p
[i
], set2
->p
[i
]);
9154 isl_bool
isl_basic_map_plain_is_equal(__isl_keep isl_basic_map
*bmap1
,
9155 __isl_keep isl_basic_map
*bmap2
)
9157 if (!bmap1
|| !bmap2
)
9158 return isl_bool_error
;
9159 return isl_basic_map_plain_cmp(bmap1
, bmap2
) == 0;
9162 isl_bool
isl_basic_set_plain_is_equal(__isl_keep isl_basic_set
*bset1
,
9163 __isl_keep isl_basic_set
*bset2
)
9165 return isl_basic_map_plain_is_equal(bset_to_bmap(bset1
),
9166 bset_to_bmap(bset2
));
9169 static int qsort_bmap_cmp(const void *p1
, const void *p2
)
9171 isl_basic_map
*bmap1
= *(isl_basic_map
**) p1
;
9172 isl_basic_map
*bmap2
= *(isl_basic_map
**) p2
;
9174 return isl_basic_map_plain_cmp(bmap1
, bmap2
);
9177 /* Sort the basic maps of "map" and remove duplicate basic maps.
9179 * While removing basic maps, we make sure that the basic maps remain
9180 * sorted because isl_map_normalize expects the basic maps of the result
9183 static __isl_give isl_map
*sort_and_remove_duplicates(__isl_take isl_map
*map
)
9187 map
= isl_map_remove_empty_parts(map
);
9190 qsort(map
->p
, map
->n
, sizeof(struct isl_basic_map
*), qsort_bmap_cmp
);
9191 for (i
= map
->n
- 1; i
>= 1; --i
) {
9192 if (!isl_basic_map_plain_is_equal(map
->p
[i
- 1], map
->p
[i
]))
9194 isl_basic_map_free(map
->p
[i
-1]);
9195 for (j
= i
; j
< map
->n
; ++j
)
9196 map
->p
[j
- 1] = map
->p
[j
];
9203 /* Remove obvious duplicates among the basic maps of "map".
9205 * Unlike isl_map_normalize, this function does not remove redundant
9206 * constraints and only removes duplicates that have exactly the same
9207 * constraints in the input. It does sort the constraints and
9208 * the basic maps to ease the detection of duplicates.
9210 * If "map" has already been normalized or if the basic maps are
9211 * disjoint, then there can be no duplicates.
9213 __isl_give isl_map
*isl_map_remove_obvious_duplicates(__isl_take isl_map
*map
)
9216 isl_basic_map
*bmap
;
9222 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
| ISL_MAP_DISJOINT
))
9224 for (i
= 0; i
< map
->n
; ++i
) {
9225 bmap
= isl_basic_map_copy(map
->p
[i
]);
9226 bmap
= isl_basic_map_sort_constraints(bmap
);
9228 return isl_map_free(map
);
9229 isl_basic_map_free(map
->p
[i
]);
9233 map
= sort_and_remove_duplicates(map
);
9237 /* We normalize in place, but if anything goes wrong we need
9238 * to return NULL, so we need to make sure we don't change the
9239 * meaning of any possible other copies of map.
9241 __isl_give isl_map
*isl_map_normalize(__isl_take isl_map
*map
)
9244 struct isl_basic_map
*bmap
;
9248 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
))
9250 for (i
= 0; i
< map
->n
; ++i
) {
9251 bmap
= isl_basic_map_normalize(isl_basic_map_copy(map
->p
[i
]));
9254 isl_basic_map_free(map
->p
[i
]);
9258 map
= sort_and_remove_duplicates(map
);
9260 ISL_F_SET(map
, ISL_MAP_NORMALIZED
);
9267 struct isl_set
*isl_set_normalize(struct isl_set
*set
)
9269 return set_from_map(isl_map_normalize(set_to_map(set
)));
9272 isl_bool
isl_map_plain_is_equal(__isl_keep isl_map
*map1
,
9273 __isl_keep isl_map
*map2
)
9279 return isl_bool_error
;
9282 return isl_bool_true
;
9283 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
9284 return isl_bool_false
;
9286 map1
= isl_map_copy(map1
);
9287 map2
= isl_map_copy(map2
);
9288 map1
= isl_map_normalize(map1
);
9289 map2
= isl_map_normalize(map2
);
9292 equal
= map1
->n
== map2
->n
;
9293 for (i
= 0; equal
&& i
< map1
->n
; ++i
) {
9294 equal
= isl_basic_map_plain_is_equal(map1
->p
[i
], map2
->p
[i
]);
9304 return isl_bool_error
;
9307 isl_bool
isl_set_plain_is_equal(__isl_keep isl_set
*set1
,
9308 __isl_keep isl_set
*set2
)
9310 return isl_map_plain_is_equal(set_to_map(set1
), set_to_map(set2
));
9313 /* Return the basic maps in "map" as a list.
9315 __isl_give isl_basic_map_list
*isl_map_get_basic_map_list(
9316 __isl_keep isl_map
*map
)
9320 isl_basic_map_list
*list
;
9324 ctx
= isl_map_get_ctx(map
);
9325 list
= isl_basic_map_list_alloc(ctx
, map
->n
);
9327 for (i
= 0; i
< map
->n
; ++i
) {
9328 isl_basic_map
*bmap
;
9330 bmap
= isl_basic_map_copy(map
->p
[i
]);
9331 list
= isl_basic_map_list_add(list
, bmap
);
9337 /* Return the intersection of the elements in the non-empty list "list".
9338 * All elements are assumed to live in the same space.
9340 __isl_give isl_basic_map
*isl_basic_map_list_intersect(
9341 __isl_take isl_basic_map_list
*list
)
9344 isl_basic_map
*bmap
;
9348 n
= isl_basic_map_list_n_basic_map(list
);
9350 isl_die(isl_basic_map_list_get_ctx(list
), isl_error_invalid
,
9351 "expecting non-empty list", goto error
);
9353 bmap
= isl_basic_map_list_get_basic_map(list
, 0);
9354 for (i
= 1; i
< n
; ++i
) {
9355 isl_basic_map
*bmap_i
;
9357 bmap_i
= isl_basic_map_list_get_basic_map(list
, i
);
9358 bmap
= isl_basic_map_intersect(bmap
, bmap_i
);
9361 isl_basic_map_list_free(list
);
9364 isl_basic_map_list_free(list
);
9368 /* Return the intersection of the elements in the non-empty list "list".
9369 * All elements are assumed to live in the same space.
9371 __isl_give isl_basic_set
*isl_basic_set_list_intersect(
9372 __isl_take isl_basic_set_list
*list
)
9374 return isl_basic_map_list_intersect(list
);
9377 /* Return the union of the elements of "list".
9378 * The list is required to have at least one element.
9380 __isl_give isl_set
*isl_basic_set_list_union(
9381 __isl_take isl_basic_set_list
*list
)
9385 isl_basic_set
*bset
;
9390 n
= isl_basic_set_list_n_basic_set(list
);
9392 isl_die(isl_basic_set_list_get_ctx(list
), isl_error_invalid
,
9393 "expecting non-empty list", goto error
);
9395 bset
= isl_basic_set_list_get_basic_set(list
, 0);
9396 space
= isl_basic_set_get_space(bset
);
9397 isl_basic_set_free(bset
);
9399 set
= isl_set_alloc_space(space
, n
, 0);
9400 for (i
= 0; i
< n
; ++i
) {
9401 bset
= isl_basic_set_list_get_basic_set(list
, i
);
9402 set
= isl_set_add_basic_set(set
, bset
);
9405 isl_basic_set_list_free(list
);
9408 isl_basic_set_list_free(list
);
9412 /* Return the union of the elements in the non-empty list "list".
9413 * All elements are assumed to live in the same space.
9415 __isl_give isl_set
*isl_set_list_union(__isl_take isl_set_list
*list
)
9422 n
= isl_set_list_n_set(list
);
9424 isl_die(isl_set_list_get_ctx(list
), isl_error_invalid
,
9425 "expecting non-empty list", goto error
);
9427 set
= isl_set_list_get_set(list
, 0);
9428 for (i
= 1; i
< n
; ++i
) {
9431 set_i
= isl_set_list_get_set(list
, i
);
9432 set
= isl_set_union(set
, set_i
);
9435 isl_set_list_free(list
);
9438 isl_set_list_free(list
);
9442 /* Return the Cartesian product of the basic sets in list (in the given order).
9444 __isl_give isl_basic_set
*isl_basic_set_list_product(
9445 __isl_take
struct isl_basic_set_list
*list
)
9453 struct isl_basic_set
*product
= NULL
;
9457 isl_assert(list
->ctx
, list
->n
> 0, goto error
);
9458 isl_assert(list
->ctx
, list
->p
[0], goto error
);
9459 nparam
= isl_basic_set_n_param(list
->p
[0]);
9460 dim
= isl_basic_set_n_dim(list
->p
[0]);
9461 extra
= list
->p
[0]->n_div
;
9462 n_eq
= list
->p
[0]->n_eq
;
9463 n_ineq
= list
->p
[0]->n_ineq
;
9464 for (i
= 1; i
< list
->n
; ++i
) {
9465 isl_assert(list
->ctx
, list
->p
[i
], goto error
);
9466 isl_assert(list
->ctx
,
9467 nparam
== isl_basic_set_n_param(list
->p
[i
]), goto error
);
9468 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
9469 extra
+= list
->p
[i
]->n_div
;
9470 n_eq
+= list
->p
[i
]->n_eq
;
9471 n_ineq
+= list
->p
[i
]->n_ineq
;
9473 product
= isl_basic_set_alloc(list
->ctx
, nparam
, dim
, extra
,
9478 for (i
= 0; i
< list
->n
; ++i
) {
9479 isl_basic_set_add_constraints(product
,
9480 isl_basic_set_copy(list
->p
[i
]), dim
);
9481 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
9483 isl_basic_set_list_free(list
);
9486 isl_basic_set_free(product
);
9487 isl_basic_set_list_free(list
);
9491 struct isl_basic_map
*isl_basic_map_product(
9492 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
9494 isl_space
*dim_result
= NULL
;
9495 struct isl_basic_map
*bmap
;
9496 unsigned in1
, in2
, out1
, out2
, nparam
, total
, pos
;
9497 struct isl_dim_map
*dim_map1
, *dim_map2
;
9499 if (!bmap1
|| !bmap2
)
9502 isl_assert(bmap1
->ctx
, isl_space_match(bmap1
->dim
, isl_dim_param
,
9503 bmap2
->dim
, isl_dim_param
), goto error
);
9504 dim_result
= isl_space_product(isl_space_copy(bmap1
->dim
),
9505 isl_space_copy(bmap2
->dim
));
9507 in1
= isl_basic_map_n_in(bmap1
);
9508 in2
= isl_basic_map_n_in(bmap2
);
9509 out1
= isl_basic_map_n_out(bmap1
);
9510 out2
= isl_basic_map_n_out(bmap2
);
9511 nparam
= isl_basic_map_n_param(bmap1
);
9513 total
= nparam
+ in1
+ in2
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
9514 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9515 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9516 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9517 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9518 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9519 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
9520 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
9521 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
9522 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
9523 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9525 bmap
= isl_basic_map_alloc_space(dim_result
,
9526 bmap1
->n_div
+ bmap2
->n_div
,
9527 bmap1
->n_eq
+ bmap2
->n_eq
,
9528 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9529 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9530 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9531 bmap
= isl_basic_map_simplify(bmap
);
9532 return isl_basic_map_finalize(bmap
);
9534 isl_basic_map_free(bmap1
);
9535 isl_basic_map_free(bmap2
);
9539 __isl_give isl_basic_map
*isl_basic_map_flat_product(
9540 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9542 isl_basic_map
*prod
;
9544 prod
= isl_basic_map_product(bmap1
, bmap2
);
9545 prod
= isl_basic_map_flatten(prod
);
9549 __isl_give isl_basic_set
*isl_basic_set_flat_product(
9550 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
9552 return isl_basic_map_flat_range_product(bset1
, bset2
);
9555 __isl_give isl_basic_map
*isl_basic_map_domain_product(
9556 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9558 isl_space
*space_result
= NULL
;
9559 isl_basic_map
*bmap
;
9560 unsigned in1
, in2
, out
, nparam
, total
, pos
;
9561 struct isl_dim_map
*dim_map1
, *dim_map2
;
9563 if (!bmap1
|| !bmap2
)
9566 space_result
= isl_space_domain_product(isl_space_copy(bmap1
->dim
),
9567 isl_space_copy(bmap2
->dim
));
9569 in1
= isl_basic_map_dim(bmap1
, isl_dim_in
);
9570 in2
= isl_basic_map_dim(bmap2
, isl_dim_in
);
9571 out
= isl_basic_map_dim(bmap1
, isl_dim_out
);
9572 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
9574 total
= nparam
+ in1
+ in2
+ out
+ bmap1
->n_div
+ bmap2
->n_div
;
9575 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9576 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9577 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9578 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9579 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9580 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
9581 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
9582 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
);
9583 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out
);
9584 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9586 bmap
= isl_basic_map_alloc_space(space_result
,
9587 bmap1
->n_div
+ bmap2
->n_div
,
9588 bmap1
->n_eq
+ bmap2
->n_eq
,
9589 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9590 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9591 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9592 bmap
= isl_basic_map_simplify(bmap
);
9593 return isl_basic_map_finalize(bmap
);
9595 isl_basic_map_free(bmap1
);
9596 isl_basic_map_free(bmap2
);
9600 __isl_give isl_basic_map
*isl_basic_map_range_product(
9601 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9604 isl_space
*dim_result
= NULL
;
9605 isl_basic_map
*bmap
;
9606 unsigned in
, out1
, out2
, nparam
, total
, pos
;
9607 struct isl_dim_map
*dim_map1
, *dim_map2
;
9609 rational
= isl_basic_map_is_rational(bmap1
);
9610 if (rational
>= 0 && rational
)
9611 rational
= isl_basic_map_is_rational(bmap2
);
9612 if (!bmap1
|| !bmap2
|| rational
< 0)
9615 if (!isl_space_match(bmap1
->dim
, isl_dim_param
,
9616 bmap2
->dim
, isl_dim_param
))
9617 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
9618 "parameters don't match", goto error
);
9620 dim_result
= isl_space_range_product(isl_space_copy(bmap1
->dim
),
9621 isl_space_copy(bmap2
->dim
));
9623 in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
9624 out1
= isl_basic_map_n_out(bmap1
);
9625 out2
= isl_basic_map_n_out(bmap2
);
9626 nparam
= isl_basic_map_n_param(bmap1
);
9628 total
= nparam
+ in
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
9629 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9630 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9631 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9632 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9633 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9634 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
9635 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in
);
9636 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
9637 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
9638 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9640 bmap
= isl_basic_map_alloc_space(dim_result
,
9641 bmap1
->n_div
+ bmap2
->n_div
,
9642 bmap1
->n_eq
+ bmap2
->n_eq
,
9643 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9644 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9645 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9647 bmap
= isl_basic_map_set_rational(bmap
);
9648 bmap
= isl_basic_map_simplify(bmap
);
9649 return isl_basic_map_finalize(bmap
);
9651 isl_basic_map_free(bmap1
);
9652 isl_basic_map_free(bmap2
);
9656 __isl_give isl_basic_map
*isl_basic_map_flat_range_product(
9657 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9659 isl_basic_map
*prod
;
9661 prod
= isl_basic_map_range_product(bmap1
, bmap2
);
9662 prod
= isl_basic_map_flatten_range(prod
);
9666 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
9667 * and collect the results.
9668 * The result live in the space obtained by calling "space_product"
9669 * on the spaces of "map1" and "map2".
9670 * If "remove_duplicates" is set then the result may contain duplicates
9671 * (even if the inputs do not) and so we try and remove the obvious
9674 static __isl_give isl_map
*map_product(__isl_take isl_map
*map1
,
9675 __isl_take isl_map
*map2
,
9676 __isl_give isl_space
*(*space_product
)(__isl_take isl_space
*left
,
9677 __isl_take isl_space
*right
),
9678 __isl_give isl_basic_map
*(*basic_map_product
)(
9679 __isl_take isl_basic_map
*left
,
9680 __isl_take isl_basic_map
*right
),
9681 int remove_duplicates
)
9684 struct isl_map
*result
;
9690 isl_assert(map1
->ctx
, isl_space_match(map1
->dim
, isl_dim_param
,
9691 map2
->dim
, isl_dim_param
), goto error
);
9693 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
9694 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
9695 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
9697 result
= isl_map_alloc_space(space_product(isl_space_copy(map1
->dim
),
9698 isl_space_copy(map2
->dim
)),
9699 map1
->n
* map2
->n
, flags
);
9702 for (i
= 0; i
< map1
->n
; ++i
)
9703 for (j
= 0; j
< map2
->n
; ++j
) {
9704 struct isl_basic_map
*part
;
9705 part
= basic_map_product(isl_basic_map_copy(map1
->p
[i
]),
9706 isl_basic_map_copy(map2
->p
[j
]));
9707 if (isl_basic_map_is_empty(part
))
9708 isl_basic_map_free(part
);
9710 result
= isl_map_add_basic_map(result
, part
);
9714 if (remove_duplicates
)
9715 result
= isl_map_remove_obvious_duplicates(result
);
9725 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
9727 static __isl_give isl_map
*map_product_aligned(__isl_take isl_map
*map1
,
9728 __isl_take isl_map
*map2
)
9730 return map_product(map1
, map2
, &isl_space_product
,
9731 &isl_basic_map_product
, 0);
9734 __isl_give isl_map
*isl_map_product(__isl_take isl_map
*map1
,
9735 __isl_take isl_map
*map2
)
9737 return isl_map_align_params_map_map_and(map1
, map2
, &map_product_aligned
);
9740 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
9742 __isl_give isl_map
*isl_map_flat_product(__isl_take isl_map
*map1
,
9743 __isl_take isl_map
*map2
)
9747 prod
= isl_map_product(map1
, map2
);
9748 prod
= isl_map_flatten(prod
);
9752 /* Given two set A and B, construct its Cartesian product A x B.
9754 struct isl_set
*isl_set_product(struct isl_set
*set1
, struct isl_set
*set2
)
9756 return isl_map_range_product(set1
, set2
);
9759 __isl_give isl_set
*isl_set_flat_product(__isl_take isl_set
*set1
,
9760 __isl_take isl_set
*set2
)
9762 return isl_map_flat_range_product(set1
, set2
);
9765 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
9767 static __isl_give isl_map
*map_domain_product_aligned(__isl_take isl_map
*map1
,
9768 __isl_take isl_map
*map2
)
9770 return map_product(map1
, map2
, &isl_space_domain_product
,
9771 &isl_basic_map_domain_product
, 1);
9774 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
9776 static __isl_give isl_map
*map_range_product_aligned(__isl_take isl_map
*map1
,
9777 __isl_take isl_map
*map2
)
9779 return map_product(map1
, map2
, &isl_space_range_product
,
9780 &isl_basic_map_range_product
, 1);
9783 __isl_give isl_map
*isl_map_domain_product(__isl_take isl_map
*map1
,
9784 __isl_take isl_map
*map2
)
9786 return isl_map_align_params_map_map_and(map1
, map2
,
9787 &map_domain_product_aligned
);
9790 __isl_give isl_map
*isl_map_range_product(__isl_take isl_map
*map1
,
9791 __isl_take isl_map
*map2
)
9793 return isl_map_align_params_map_map_and(map1
, map2
,
9794 &map_range_product_aligned
);
9797 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
9799 __isl_give isl_map
*isl_map_factor_domain(__isl_take isl_map
*map
)
9802 int total1
, keep1
, total2
, keep2
;
9806 if (!isl_space_domain_is_wrapping(map
->dim
) ||
9807 !isl_space_range_is_wrapping(map
->dim
))
9808 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9809 "not a product", return isl_map_free(map
));
9811 space
= isl_map_get_space(map
);
9812 total1
= isl_space_dim(space
, isl_dim_in
);
9813 total2
= isl_space_dim(space
, isl_dim_out
);
9814 space
= isl_space_factor_domain(space
);
9815 keep1
= isl_space_dim(space
, isl_dim_in
);
9816 keep2
= isl_space_dim(space
, isl_dim_out
);
9817 map
= isl_map_project_out(map
, isl_dim_in
, keep1
, total1
- keep1
);
9818 map
= isl_map_project_out(map
, isl_dim_out
, keep2
, total2
- keep2
);
9819 map
= isl_map_reset_space(map
, space
);
9824 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
9826 __isl_give isl_map
*isl_map_factor_range(__isl_take isl_map
*map
)
9829 int total1
, keep1
, total2
, keep2
;
9833 if (!isl_space_domain_is_wrapping(map
->dim
) ||
9834 !isl_space_range_is_wrapping(map
->dim
))
9835 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9836 "not a product", return isl_map_free(map
));
9838 space
= isl_map_get_space(map
);
9839 total1
= isl_space_dim(space
, isl_dim_in
);
9840 total2
= isl_space_dim(space
, isl_dim_out
);
9841 space
= isl_space_factor_range(space
);
9842 keep1
= isl_space_dim(space
, isl_dim_in
);
9843 keep2
= isl_space_dim(space
, isl_dim_out
);
9844 map
= isl_map_project_out(map
, isl_dim_in
, 0, total1
- keep1
);
9845 map
= isl_map_project_out(map
, isl_dim_out
, 0, total2
- keep2
);
9846 map
= isl_map_reset_space(map
, space
);
9851 /* Given a map of the form [A -> B] -> C, return the map A -> C.
9853 __isl_give isl_map
*isl_map_domain_factor_domain(__isl_take isl_map
*map
)
9860 if (!isl_space_domain_is_wrapping(map
->dim
))
9861 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9862 "domain is not a product", return isl_map_free(map
));
9864 space
= isl_map_get_space(map
);
9865 total
= isl_space_dim(space
, isl_dim_in
);
9866 space
= isl_space_domain_factor_domain(space
);
9867 keep
= isl_space_dim(space
, isl_dim_in
);
9868 map
= isl_map_project_out(map
, isl_dim_in
, keep
, total
- keep
);
9869 map
= isl_map_reset_space(map
, space
);
9874 /* Given a map of the form [A -> B] -> C, return the map B -> C.
9876 __isl_give isl_map
*isl_map_domain_factor_range(__isl_take isl_map
*map
)
9883 if (!isl_space_domain_is_wrapping(map
->dim
))
9884 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9885 "domain is not a product", return isl_map_free(map
));
9887 space
= isl_map_get_space(map
);
9888 total
= isl_space_dim(space
, isl_dim_in
);
9889 space
= isl_space_domain_factor_range(space
);
9890 keep
= isl_space_dim(space
, isl_dim_in
);
9891 map
= isl_map_project_out(map
, isl_dim_in
, 0, total
- keep
);
9892 map
= isl_map_reset_space(map
, space
);
9897 /* Given a map A -> [B -> C], extract the map A -> B.
9899 __isl_give isl_map
*isl_map_range_factor_domain(__isl_take isl_map
*map
)
9906 if (!isl_space_range_is_wrapping(map
->dim
))
9907 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9908 "range is not a product", return isl_map_free(map
));
9910 space
= isl_map_get_space(map
);
9911 total
= isl_space_dim(space
, isl_dim_out
);
9912 space
= isl_space_range_factor_domain(space
);
9913 keep
= isl_space_dim(space
, isl_dim_out
);
9914 map
= isl_map_project_out(map
, isl_dim_out
, keep
, total
- keep
);
9915 map
= isl_map_reset_space(map
, space
);
9920 /* Given a map A -> [B -> C], extract the map A -> C.
9922 __isl_give isl_map
*isl_map_range_factor_range(__isl_take isl_map
*map
)
9929 if (!isl_space_range_is_wrapping(map
->dim
))
9930 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9931 "range is not a product", return isl_map_free(map
));
9933 space
= isl_map_get_space(map
);
9934 total
= isl_space_dim(space
, isl_dim_out
);
9935 space
= isl_space_range_factor_range(space
);
9936 keep
= isl_space_dim(space
, isl_dim_out
);
9937 map
= isl_map_project_out(map
, isl_dim_out
, 0, total
- keep
);
9938 map
= isl_map_reset_space(map
, space
);
9943 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
9945 __isl_give isl_map
*isl_map_flat_domain_product(__isl_take isl_map
*map1
,
9946 __isl_take isl_map
*map2
)
9950 prod
= isl_map_domain_product(map1
, map2
);
9951 prod
= isl_map_flatten_domain(prod
);
9955 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
9957 __isl_give isl_map
*isl_map_flat_range_product(__isl_take isl_map
*map1
,
9958 __isl_take isl_map
*map2
)
9962 prod
= isl_map_range_product(map1
, map2
);
9963 prod
= isl_map_flatten_range(prod
);
9967 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map
*bmap
)
9970 uint32_t hash
= isl_hash_init();
9975 bmap
= isl_basic_map_copy(bmap
);
9976 bmap
= isl_basic_map_normalize(bmap
);
9979 total
= isl_basic_map_total_dim(bmap
);
9980 isl_hash_byte(hash
, bmap
->n_eq
& 0xFF);
9981 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
9983 c_hash
= isl_seq_get_hash(bmap
->eq
[i
], 1 + total
);
9984 isl_hash_hash(hash
, c_hash
);
9986 isl_hash_byte(hash
, bmap
->n_ineq
& 0xFF);
9987 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
9989 c_hash
= isl_seq_get_hash(bmap
->ineq
[i
], 1 + total
);
9990 isl_hash_hash(hash
, c_hash
);
9992 isl_hash_byte(hash
, bmap
->n_div
& 0xFF);
9993 for (i
= 0; i
< bmap
->n_div
; ++i
) {
9995 if (isl_int_is_zero(bmap
->div
[i
][0]))
9997 isl_hash_byte(hash
, i
& 0xFF);
9998 c_hash
= isl_seq_get_hash(bmap
->div
[i
], 1 + 1 + total
);
9999 isl_hash_hash(hash
, c_hash
);
10001 isl_basic_map_free(bmap
);
10005 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set
*bset
)
10007 return isl_basic_map_get_hash(bset_to_bmap(bset
));
10010 uint32_t isl_map_get_hash(__isl_keep isl_map
*map
)
10017 map
= isl_map_copy(map
);
10018 map
= isl_map_normalize(map
);
10022 hash
= isl_hash_init();
10023 for (i
= 0; i
< map
->n
; ++i
) {
10024 uint32_t bmap_hash
;
10025 bmap_hash
= isl_basic_map_get_hash(map
->p
[i
]);
10026 isl_hash_hash(hash
, bmap_hash
);
10034 uint32_t isl_set_get_hash(__isl_keep isl_set
*set
)
10036 return isl_map_get_hash(set_to_map(set
));
10039 /* Check if the value for dimension dim is completely determined
10040 * by the values of the other parameters and variables.
10041 * That is, check if dimension dim is involved in an equality.
10043 int isl_basic_set_dim_is_unique(struct isl_basic_set
*bset
, unsigned dim
)
10050 nparam
= isl_basic_set_n_param(bset
);
10051 for (i
= 0; i
< bset
->n_eq
; ++i
)
10052 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ dim
]))
10057 /* Check if the value for dimension dim is completely determined
10058 * by the values of the other parameters and variables.
10059 * That is, check if dimension dim is involved in an equality
10060 * for each of the subsets.
10062 int isl_set_dim_is_unique(struct isl_set
*set
, unsigned dim
)
10068 for (i
= 0; i
< set
->n
; ++i
) {
10070 unique
= isl_basic_set_dim_is_unique(set
->p
[i
], dim
);
10077 /* Return the number of basic maps in the (current) representation of "map".
10079 int isl_map_n_basic_map(__isl_keep isl_map
*map
)
10081 return map
? map
->n
: 0;
10084 int isl_set_n_basic_set(__isl_keep isl_set
*set
)
10086 return set
? set
->n
: 0;
10089 isl_stat
isl_map_foreach_basic_map(__isl_keep isl_map
*map
,
10090 isl_stat (*fn
)(__isl_take isl_basic_map
*bmap
, void *user
), void *user
)
10095 return isl_stat_error
;
10097 for (i
= 0; i
< map
->n
; ++i
)
10098 if (fn(isl_basic_map_copy(map
->p
[i
]), user
) < 0)
10099 return isl_stat_error
;
10101 return isl_stat_ok
;
10104 isl_stat
isl_set_foreach_basic_set(__isl_keep isl_set
*set
,
10105 isl_stat (*fn
)(__isl_take isl_basic_set
*bset
, void *user
), void *user
)
10110 return isl_stat_error
;
10112 for (i
= 0; i
< set
->n
; ++i
)
10113 if (fn(isl_basic_set_copy(set
->p
[i
]), user
) < 0)
10114 return isl_stat_error
;
10116 return isl_stat_ok
;
10119 /* Return a list of basic sets, the union of which is equal to "set".
10121 __isl_give isl_basic_set_list
*isl_set_get_basic_set_list(
10122 __isl_keep isl_set
*set
)
10125 isl_basic_set_list
*list
;
10130 list
= isl_basic_set_list_alloc(isl_set_get_ctx(set
), set
->n
);
10131 for (i
= 0; i
< set
->n
; ++i
) {
10132 isl_basic_set
*bset
;
10134 bset
= isl_basic_set_copy(set
->p
[i
]);
10135 list
= isl_basic_set_list_add(list
, bset
);
10141 __isl_give isl_basic_set
*isl_basic_set_lift(__isl_take isl_basic_set
*bset
)
10148 bset
= isl_basic_set_cow(bset
);
10152 dim
= isl_basic_set_get_space(bset
);
10153 dim
= isl_space_lift(dim
, bset
->n_div
);
10156 isl_space_free(bset
->dim
);
10158 bset
->extra
-= bset
->n_div
;
10161 bset
= isl_basic_set_finalize(bset
);
10165 isl_basic_set_free(bset
);
10169 __isl_give isl_set
*isl_set_lift(__isl_take isl_set
*set
)
10175 set
= isl_set_align_divs(set
);
10180 set
= isl_set_cow(set
);
10184 n_div
= set
->p
[0]->n_div
;
10185 dim
= isl_set_get_space(set
);
10186 dim
= isl_space_lift(dim
, n_div
);
10189 isl_space_free(set
->dim
);
10192 for (i
= 0; i
< set
->n
; ++i
) {
10193 set
->p
[i
] = isl_basic_set_lift(set
->p
[i
]);
10204 int isl_basic_set_size(__isl_keep isl_basic_set
*bset
)
10212 dim
= isl_basic_set_total_dim(bset
);
10213 size
+= bset
->n_eq
* (1 + dim
);
10214 size
+= bset
->n_ineq
* (1 + dim
);
10215 size
+= bset
->n_div
* (2 + dim
);
10220 int isl_set_size(__isl_keep isl_set
*set
)
10228 for (i
= 0; i
< set
->n
; ++i
)
10229 size
+= isl_basic_set_size(set
->p
[i
]);
10234 /* Check if there is any lower bound (if lower == 0) and/or upper
10235 * bound (if upper == 0) on the specified dim.
10237 static isl_bool
basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
10238 enum isl_dim_type type
, unsigned pos
, int lower
, int upper
)
10242 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
10243 return isl_bool_error
;
10245 pos
+= isl_basic_map_offset(bmap
, type
);
10247 for (i
= 0; i
< bmap
->n_div
; ++i
) {
10248 if (isl_int_is_zero(bmap
->div
[i
][0]))
10250 if (!isl_int_is_zero(bmap
->div
[i
][1 + pos
]))
10251 return isl_bool_true
;
10254 for (i
= 0; i
< bmap
->n_eq
; ++i
)
10255 if (!isl_int_is_zero(bmap
->eq
[i
][pos
]))
10256 return isl_bool_true
;
10258 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
10259 int sgn
= isl_int_sgn(bmap
->ineq
[i
][pos
]);
10266 return lower
&& upper
;
10269 isl_bool
isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
10270 enum isl_dim_type type
, unsigned pos
)
10272 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 0);
10275 isl_bool
isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map
*bmap
,
10276 enum isl_dim_type type
, unsigned pos
)
10278 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 1);
10281 isl_bool
isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map
*bmap
,
10282 enum isl_dim_type type
, unsigned pos
)
10284 return basic_map_dim_is_bounded(bmap
, type
, pos
, 1, 0);
10287 isl_bool
isl_map_dim_is_bounded(__isl_keep isl_map
*map
,
10288 enum isl_dim_type type
, unsigned pos
)
10293 return isl_bool_error
;
10295 for (i
= 0; i
< map
->n
; ++i
) {
10297 bounded
= isl_basic_map_dim_is_bounded(map
->p
[i
], type
, pos
);
10298 if (bounded
< 0 || !bounded
)
10302 return isl_bool_true
;
10305 /* Return true if the specified dim is involved in both an upper bound
10306 * and a lower bound.
10308 isl_bool
isl_set_dim_is_bounded(__isl_keep isl_set
*set
,
10309 enum isl_dim_type type
, unsigned pos
)
10311 return isl_map_dim_is_bounded(set_to_map(set
), type
, pos
);
10314 /* Does "map" have a bound (according to "fn") for any of its basic maps?
10316 static isl_bool
has_any_bound(__isl_keep isl_map
*map
,
10317 enum isl_dim_type type
, unsigned pos
,
10318 isl_bool (*fn
)(__isl_keep isl_basic_map
*bmap
,
10319 enum isl_dim_type type
, unsigned pos
))
10324 return isl_bool_error
;
10326 for (i
= 0; i
< map
->n
; ++i
) {
10328 bounded
= fn(map
->p
[i
], type
, pos
);
10329 if (bounded
< 0 || bounded
)
10333 return isl_bool_false
;
10336 /* Return 1 if the specified dim is involved in any lower bound.
10338 isl_bool
isl_set_dim_has_any_lower_bound(__isl_keep isl_set
*set
,
10339 enum isl_dim_type type
, unsigned pos
)
10341 return has_any_bound(set
, type
, pos
,
10342 &isl_basic_map_dim_has_lower_bound
);
10345 /* Return 1 if the specified dim is involved in any upper bound.
10347 isl_bool
isl_set_dim_has_any_upper_bound(__isl_keep isl_set
*set
,
10348 enum isl_dim_type type
, unsigned pos
)
10350 return has_any_bound(set
, type
, pos
,
10351 &isl_basic_map_dim_has_upper_bound
);
10354 /* Does "map" have a bound (according to "fn") for all of its basic maps?
10356 static isl_bool
has_bound(__isl_keep isl_map
*map
,
10357 enum isl_dim_type type
, unsigned pos
,
10358 isl_bool (*fn
)(__isl_keep isl_basic_map
*bmap
,
10359 enum isl_dim_type type
, unsigned pos
))
10364 return isl_bool_error
;
10366 for (i
= 0; i
< map
->n
; ++i
) {
10368 bounded
= fn(map
->p
[i
], type
, pos
);
10369 if (bounded
< 0 || !bounded
)
10373 return isl_bool_true
;
10376 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10378 isl_bool
isl_set_dim_has_lower_bound(__isl_keep isl_set
*set
,
10379 enum isl_dim_type type
, unsigned pos
)
10381 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_lower_bound
);
10384 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10386 isl_bool
isl_set_dim_has_upper_bound(__isl_keep isl_set
*set
,
10387 enum isl_dim_type type
, unsigned pos
)
10389 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_upper_bound
);
10392 /* For each of the "n" variables starting at "first", determine
10393 * the sign of the variable and put the results in the first "n"
10394 * elements of the array "signs".
10396 * 1 means that the variable is non-negative
10397 * -1 means that the variable is non-positive
10398 * 0 means the variable attains both positive and negative values.
10400 isl_stat
isl_basic_set_vars_get_sign(__isl_keep isl_basic_set
*bset
,
10401 unsigned first
, unsigned n
, int *signs
)
10403 isl_vec
*bound
= NULL
;
10404 struct isl_tab
*tab
= NULL
;
10405 struct isl_tab_undo
*snap
;
10408 if (!bset
|| !signs
)
10409 return isl_stat_error
;
10411 bound
= isl_vec_alloc(bset
->ctx
, 1 + isl_basic_set_total_dim(bset
));
10412 tab
= isl_tab_from_basic_set(bset
, 0);
10413 if (!bound
|| !tab
)
10416 isl_seq_clr(bound
->el
, bound
->size
);
10417 isl_int_set_si(bound
->el
[0], -1);
10419 snap
= isl_tab_snap(tab
);
10420 for (i
= 0; i
< n
; ++i
) {
10423 isl_int_set_si(bound
->el
[1 + first
+ i
], -1);
10424 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
10426 empty
= tab
->empty
;
10427 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
10428 if (isl_tab_rollback(tab
, snap
) < 0)
10436 isl_int_set_si(bound
->el
[1 + first
+ i
], 1);
10437 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
10439 empty
= tab
->empty
;
10440 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
10441 if (isl_tab_rollback(tab
, snap
) < 0)
10444 signs
[i
] = empty
? -1 : 0;
10448 isl_vec_free(bound
);
10449 return isl_stat_ok
;
10452 isl_vec_free(bound
);
10453 return isl_stat_error
;
10456 isl_stat
isl_basic_set_dims_get_sign(__isl_keep isl_basic_set
*bset
,
10457 enum isl_dim_type type
, unsigned first
, unsigned n
, int *signs
)
10459 if (!bset
|| !signs
)
10460 return isl_stat_error
;
10461 isl_assert(bset
->ctx
, first
+ n
<= isl_basic_set_dim(bset
, type
),
10462 return isl_stat_error
);
10464 first
+= pos(bset
->dim
, type
) - 1;
10465 return isl_basic_set_vars_get_sign(bset
, first
, n
, signs
);
10468 /* Is it possible for the integer division "div" to depend (possibly
10469 * indirectly) on any output dimensions?
10471 * If the div is undefined, then we conservatively assume that it
10472 * may depend on them.
10473 * Otherwise, we check if it actually depends on them or on any integer
10474 * divisions that may depend on them.
10476 static isl_bool
div_may_involve_output(__isl_keep isl_basic_map
*bmap
, int div
)
10479 unsigned n_out
, o_out
;
10480 unsigned n_div
, o_div
;
10482 if (isl_int_is_zero(bmap
->div
[div
][0]))
10483 return isl_bool_true
;
10485 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10486 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10488 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + o_out
, n_out
) != -1)
10489 return isl_bool_true
;
10491 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10492 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10494 for (i
= 0; i
< n_div
; ++i
) {
10495 isl_bool may_involve
;
10497 if (isl_int_is_zero(bmap
->div
[div
][1 + o_div
+ i
]))
10499 may_involve
= div_may_involve_output(bmap
, i
);
10500 if (may_involve
< 0 || may_involve
)
10501 return may_involve
;
10504 return isl_bool_false
;
10507 /* Return the first integer division of "bmap" in the range
10508 * [first, first + n[ that may depend on any output dimensions and
10509 * that has a non-zero coefficient in "c" (where the first coefficient
10510 * in "c" corresponds to integer division "first").
10512 static int first_div_may_involve_output(__isl_keep isl_basic_map
*bmap
,
10513 isl_int
*c
, int first
, int n
)
10520 for (k
= first
; k
< first
+ n
; ++k
) {
10521 isl_bool may_involve
;
10523 if (isl_int_is_zero(c
[k
]))
10525 may_involve
= div_may_involve_output(bmap
, k
);
10526 if (may_involve
< 0)
10535 /* Look for a pair of inequality constraints in "bmap" of the form
10537 * -l + i >= 0 or i >= l
10539 * n + l - i >= 0 or i <= l + n
10541 * with n < "m" and i the output dimension at position "pos".
10542 * (Note that n >= 0 as otherwise the two constraints would conflict.)
10543 * Furthermore, "l" is only allowed to involve parameters, input dimensions
10544 * and earlier output dimensions, as well as integer divisions that do
10545 * not involve any of the output dimensions.
10547 * Return the index of the first inequality constraint or bmap->n_ineq
10548 * if no such pair can be found.
10550 static int find_modulo_constraint_pair(__isl_keep isl_basic_map
*bmap
,
10551 int pos
, isl_int m
)
10556 unsigned n_div
, o_div
;
10557 unsigned n_out
, o_out
;
10563 ctx
= isl_basic_map_get_ctx(bmap
);
10564 total
= isl_basic_map_total_dim(bmap
);
10565 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10566 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10567 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10568 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10569 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
10570 if (!isl_int_abs_eq(bmap
->ineq
[i
][o_out
+ pos
], ctx
->one
))
10572 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + o_out
+ pos
+ 1,
10573 n_out
- (pos
+ 1)) != -1)
10575 if (first_div_may_involve_output(bmap
, bmap
->ineq
[i
] + o_div
,
10578 for (j
= i
+ 1; j
< bmap
->n_ineq
; ++j
) {
10579 if (!isl_int_abs_eq(bmap
->ineq
[j
][o_out
+ pos
],
10582 if (!isl_seq_is_neg(bmap
->ineq
[i
] + 1,
10583 bmap
->ineq
[j
] + 1, total
))
10587 if (j
>= bmap
->n_ineq
)
10589 isl_int_add(bmap
->ineq
[i
][0],
10590 bmap
->ineq
[i
][0], bmap
->ineq
[j
][0]);
10591 less
= isl_int_abs_lt(bmap
->ineq
[i
][0], m
);
10592 isl_int_sub(bmap
->ineq
[i
][0],
10593 bmap
->ineq
[i
][0], bmap
->ineq
[j
][0]);
10596 if (isl_int_is_one(bmap
->ineq
[i
][o_out
+ pos
]))
10602 return bmap
->n_ineq
;
10605 /* Return the index of the equality of "bmap" that defines
10606 * the output dimension "pos" in terms of earlier dimensions.
10607 * The equality may also involve integer divisions, as long
10608 * as those integer divisions are defined in terms of
10609 * parameters or input dimensions.
10610 * In this case, *div is set to the number of integer divisions and
10611 * *ineq is set to the number of inequality constraints (provided
10612 * div and ineq are not NULL).
10614 * The equality may also involve a single integer division involving
10615 * the output dimensions (typically only output dimension "pos") as
10616 * long as the coefficient of output dimension "pos" is 1 or -1 and
10617 * there is a pair of constraints i >= l and i <= l + n, with i referring
10618 * to output dimension "pos", l an expression involving only earlier
10619 * dimensions and n smaller than the coefficient of the integer division
10620 * in the equality. In this case, the output dimension can be defined
10621 * in terms of a modulo expression that does not involve the integer division.
10622 * *div is then set to this single integer division and
10623 * *ineq is set to the index of constraint i >= l.
10625 * Return bmap->n_eq if there is no such equality.
10626 * Return -1 on error.
10628 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map
*bmap
,
10629 int pos
, int *div
, int *ineq
)
10632 unsigned n_out
, o_out
;
10633 unsigned n_div
, o_div
;
10638 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10639 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10640 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10641 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10644 *ineq
= bmap
->n_ineq
;
10647 for (j
= 0; j
< bmap
->n_eq
; ++j
) {
10648 if (isl_int_is_zero(bmap
->eq
[j
][o_out
+ pos
]))
10650 if (isl_seq_first_non_zero(bmap
->eq
[j
] + o_out
+ pos
+ 1,
10651 n_out
- (pos
+ 1)) != -1)
10653 k
= first_div_may_involve_output(bmap
, bmap
->eq
[j
] + o_div
,
10657 if (!isl_int_is_one(bmap
->eq
[j
][o_out
+ pos
]) &&
10658 !isl_int_is_negone(bmap
->eq
[j
][o_out
+ pos
]))
10660 if (first_div_may_involve_output(bmap
, bmap
->eq
[j
] + o_div
,
10661 k
+ 1, n_div
- (k
+1)) < n_div
)
10663 l
= find_modulo_constraint_pair(bmap
, pos
,
10664 bmap
->eq
[j
][o_div
+ k
]);
10667 if (l
>= bmap
->n_ineq
)
10679 /* Check if the given basic map is obviously single-valued.
10680 * In particular, for each output dimension, check that there is
10681 * an equality that defines the output dimension in terms of
10682 * earlier dimensions.
10684 isl_bool
isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map
*bmap
)
10690 return isl_bool_error
;
10692 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10694 for (i
= 0; i
< n_out
; ++i
) {
10697 eq
= isl_basic_map_output_defining_equality(bmap
, i
,
10700 return isl_bool_error
;
10701 if (eq
>= bmap
->n_eq
)
10702 return isl_bool_false
;
10705 return isl_bool_true
;
10708 /* Check if the given basic map is single-valued.
10709 * We simply compute
10713 * and check if the result is a subset of the identity mapping.
10715 isl_bool
isl_basic_map_is_single_valued(__isl_keep isl_basic_map
*bmap
)
10718 isl_basic_map
*test
;
10722 sv
= isl_basic_map_plain_is_single_valued(bmap
);
10726 test
= isl_basic_map_reverse(isl_basic_map_copy(bmap
));
10727 test
= isl_basic_map_apply_range(test
, isl_basic_map_copy(bmap
));
10729 space
= isl_basic_map_get_space(bmap
);
10730 space
= isl_space_map_from_set(isl_space_range(space
));
10731 id
= isl_basic_map_identity(space
);
10733 sv
= isl_basic_map_is_subset(test
, id
);
10735 isl_basic_map_free(test
);
10736 isl_basic_map_free(id
);
10741 /* Check if the given map is obviously single-valued.
10743 isl_bool
isl_map_plain_is_single_valued(__isl_keep isl_map
*map
)
10746 return isl_bool_error
;
10748 return isl_bool_true
;
10750 return isl_bool_false
;
10752 return isl_basic_map_plain_is_single_valued(map
->p
[0]);
10755 /* Check if the given map is single-valued.
10756 * We simply compute
10760 * and check if the result is a subset of the identity mapping.
10762 isl_bool
isl_map_is_single_valued(__isl_keep isl_map
*map
)
10769 sv
= isl_map_plain_is_single_valued(map
);
10773 test
= isl_map_reverse(isl_map_copy(map
));
10774 test
= isl_map_apply_range(test
, isl_map_copy(map
));
10776 dim
= isl_space_map_from_set(isl_space_range(isl_map_get_space(map
)));
10777 id
= isl_map_identity(dim
);
10779 sv
= isl_map_is_subset(test
, id
);
10781 isl_map_free(test
);
10787 isl_bool
isl_map_is_injective(__isl_keep isl_map
*map
)
10791 map
= isl_map_copy(map
);
10792 map
= isl_map_reverse(map
);
10793 in
= isl_map_is_single_valued(map
);
10799 /* Check if the given map is obviously injective.
10801 isl_bool
isl_map_plain_is_injective(__isl_keep isl_map
*map
)
10805 map
= isl_map_copy(map
);
10806 map
= isl_map_reverse(map
);
10807 in
= isl_map_plain_is_single_valued(map
);
10813 isl_bool
isl_map_is_bijective(__isl_keep isl_map
*map
)
10817 sv
= isl_map_is_single_valued(map
);
10821 return isl_map_is_injective(map
);
10824 isl_bool
isl_set_is_singleton(__isl_keep isl_set
*set
)
10826 return isl_map_is_single_valued(set_to_map(set
));
10829 /* Does "map" only map elements to themselves?
10831 * If the domain and range spaces are different, then "map"
10832 * is considered not to be an identity relation, even if it is empty.
10833 * Otherwise, construct the maximal identity relation and
10834 * check whether "map" is a subset of this relation.
10836 isl_bool
isl_map_is_identity(__isl_keep isl_map
*map
)
10840 isl_bool equal
, is_identity
;
10842 space
= isl_map_get_space(map
);
10843 equal
= isl_space_tuple_is_equal(space
, isl_dim_in
, space
, isl_dim_out
);
10844 isl_space_free(space
);
10845 if (equal
< 0 || !equal
)
10848 id
= isl_map_identity(isl_map_get_space(map
));
10849 is_identity
= isl_map_is_subset(map
, id
);
10852 return is_identity
;
10855 int isl_map_is_translation(__isl_keep isl_map
*map
)
10860 delta
= isl_map_deltas(isl_map_copy(map
));
10861 ok
= isl_set_is_singleton(delta
);
10862 isl_set_free(delta
);
10867 static int unique(isl_int
*p
, unsigned pos
, unsigned len
)
10869 if (isl_seq_first_non_zero(p
, pos
) != -1)
10871 if (isl_seq_first_non_zero(p
+ pos
+ 1, len
- pos
- 1) != -1)
10876 isl_bool
isl_basic_set_is_box(__isl_keep isl_basic_set
*bset
)
10883 return isl_bool_error
;
10885 if (isl_basic_set_dim(bset
, isl_dim_div
) != 0)
10886 return isl_bool_false
;
10888 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
10889 ovar
= isl_space_offset(bset
->dim
, isl_dim_set
);
10890 for (j
= 0; j
< nvar
; ++j
) {
10891 int lower
= 0, upper
= 0;
10892 for (i
= 0; i
< bset
->n_eq
; ++i
) {
10893 if (isl_int_is_zero(bset
->eq
[i
][1 + ovar
+ j
]))
10895 if (!unique(bset
->eq
[i
] + 1 + ovar
, j
, nvar
))
10896 return isl_bool_false
;
10899 if (i
< bset
->n_eq
)
10901 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
10902 if (isl_int_is_zero(bset
->ineq
[i
][1 + ovar
+ j
]))
10904 if (!unique(bset
->ineq
[i
] + 1 + ovar
, j
, nvar
))
10905 return isl_bool_false
;
10906 if (isl_int_is_pos(bset
->ineq
[i
][1 + ovar
+ j
]))
10911 if (!lower
|| !upper
)
10912 return isl_bool_false
;
10915 return isl_bool_true
;
10918 isl_bool
isl_set_is_box(__isl_keep isl_set
*set
)
10921 return isl_bool_error
;
10923 return isl_bool_false
;
10925 return isl_basic_set_is_box(set
->p
[0]);
10928 isl_bool
isl_basic_set_is_wrapping(__isl_keep isl_basic_set
*bset
)
10931 return isl_bool_error
;
10933 return isl_space_is_wrapping(bset
->dim
);
10936 isl_bool
isl_set_is_wrapping(__isl_keep isl_set
*set
)
10939 return isl_bool_error
;
10941 return isl_space_is_wrapping(set
->dim
);
10944 /* Modify the space of "map" through a call to "change".
10945 * If "can_change" is set (not NULL), then first call it to check
10946 * if the modification is allowed, printing the error message "cannot_change"
10949 static __isl_give isl_map
*isl_map_change_space(__isl_take isl_map
*map
,
10950 isl_bool (*can_change
)(__isl_keep isl_map
*map
),
10951 const char *cannot_change
,
10952 __isl_give isl_space
*(*change
)(__isl_take isl_space
*space
))
10960 ok
= can_change
? can_change(map
) : isl_bool_true
;
10962 return isl_map_free(map
);
10964 isl_die(isl_map_get_ctx(map
), isl_error_invalid
, cannot_change
,
10965 return isl_map_free(map
));
10967 space
= change(isl_map_get_space(map
));
10968 map
= isl_map_reset_space(map
, space
);
10973 /* Is the domain of "map" a wrapped relation?
10975 isl_bool
isl_map_domain_is_wrapping(__isl_keep isl_map
*map
)
10978 return isl_bool_error
;
10980 return isl_space_domain_is_wrapping(map
->dim
);
10983 /* Is the range of "map" a wrapped relation?
10985 isl_bool
isl_map_range_is_wrapping(__isl_keep isl_map
*map
)
10988 return isl_bool_error
;
10990 return isl_space_range_is_wrapping(map
->dim
);
10993 __isl_give isl_basic_set
*isl_basic_map_wrap(__isl_take isl_basic_map
*bmap
)
10995 bmap
= isl_basic_map_cow(bmap
);
10999 bmap
->dim
= isl_space_wrap(bmap
->dim
);
11003 bmap
= isl_basic_map_finalize(bmap
);
11005 return bset_from_bmap(bmap
);
11007 isl_basic_map_free(bmap
);
11011 /* Given a map A -> B, return the set (A -> B).
11013 __isl_give isl_set
*isl_map_wrap(__isl_take isl_map
*map
)
11015 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_wrap
);
11018 __isl_give isl_basic_map
*isl_basic_set_unwrap(__isl_take isl_basic_set
*bset
)
11020 bset
= isl_basic_set_cow(bset
);
11024 bset
->dim
= isl_space_unwrap(bset
->dim
);
11028 bset
= isl_basic_set_finalize(bset
);
11030 return bset_to_bmap(bset
);
11032 isl_basic_set_free(bset
);
11036 /* Given a set (A -> B), return the map A -> B.
11037 * Error out if "set" is not of the form (A -> B).
11039 __isl_give isl_map
*isl_set_unwrap(__isl_take isl_set
*set
)
11041 return isl_map_change_space(set
, &isl_set_is_wrapping
,
11042 "not a wrapping set", &isl_space_unwrap
);
11045 __isl_give isl_basic_map
*isl_basic_map_reset(__isl_take isl_basic_map
*bmap
,
11046 enum isl_dim_type type
)
11051 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
11054 bmap
= isl_basic_map_cow(bmap
);
11058 bmap
->dim
= isl_space_reset(bmap
->dim
, type
);
11062 bmap
= isl_basic_map_finalize(bmap
);
11066 isl_basic_map_free(bmap
);
11070 __isl_give isl_map
*isl_map_reset(__isl_take isl_map
*map
,
11071 enum isl_dim_type type
)
11078 if (!isl_space_is_named_or_nested(map
->dim
, type
))
11081 map
= isl_map_cow(map
);
11085 for (i
= 0; i
< map
->n
; ++i
) {
11086 map
->p
[i
] = isl_basic_map_reset(map
->p
[i
], type
);
11090 map
->dim
= isl_space_reset(map
->dim
, type
);
11100 __isl_give isl_basic_map
*isl_basic_map_flatten(__isl_take isl_basic_map
*bmap
)
11105 if (!bmap
->dim
->nested
[0] && !bmap
->dim
->nested
[1])
11108 bmap
= isl_basic_map_cow(bmap
);
11112 bmap
->dim
= isl_space_flatten(bmap
->dim
);
11116 bmap
= isl_basic_map_finalize(bmap
);
11120 isl_basic_map_free(bmap
);
11124 __isl_give isl_basic_set
*isl_basic_set_flatten(__isl_take isl_basic_set
*bset
)
11126 return bset_from_bmap(isl_basic_map_flatten(bset_to_bmap(bset
)));
11129 __isl_give isl_basic_map
*isl_basic_map_flatten_domain(
11130 __isl_take isl_basic_map
*bmap
)
11135 if (!bmap
->dim
->nested
[0])
11138 bmap
= isl_basic_map_cow(bmap
);
11142 bmap
->dim
= isl_space_flatten_domain(bmap
->dim
);
11146 bmap
= isl_basic_map_finalize(bmap
);
11150 isl_basic_map_free(bmap
);
11154 __isl_give isl_basic_map
*isl_basic_map_flatten_range(
11155 __isl_take isl_basic_map
*bmap
)
11160 if (!bmap
->dim
->nested
[1])
11163 bmap
= isl_basic_map_cow(bmap
);
11167 bmap
->dim
= isl_space_flatten_range(bmap
->dim
);
11171 bmap
= isl_basic_map_finalize(bmap
);
11175 isl_basic_map_free(bmap
);
11179 /* Remove any internal structure from the spaces of domain and range of "map".
11181 __isl_give isl_map
*isl_map_flatten(__isl_take isl_map
*map
)
11186 if (!map
->dim
->nested
[0] && !map
->dim
->nested
[1])
11189 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten
);
11192 __isl_give isl_set
*isl_set_flatten(__isl_take isl_set
*set
)
11194 return set_from_map(isl_map_flatten(set_to_map(set
)));
11197 __isl_give isl_map
*isl_set_flatten_map(__isl_take isl_set
*set
)
11199 isl_space
*dim
, *flat_dim
;
11202 dim
= isl_set_get_space(set
);
11203 flat_dim
= isl_space_flatten(isl_space_copy(dim
));
11204 map
= isl_map_identity(isl_space_join(isl_space_reverse(dim
), flat_dim
));
11205 map
= isl_map_intersect_domain(map
, set
);
11210 /* Remove any internal structure from the space of the domain of "map".
11212 __isl_give isl_map
*isl_map_flatten_domain(__isl_take isl_map
*map
)
11217 if (!map
->dim
->nested
[0])
11220 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten_domain
);
11223 /* Remove any internal structure from the space of the range of "map".
11225 __isl_give isl_map
*isl_map_flatten_range(__isl_take isl_map
*map
)
11230 if (!map
->dim
->nested
[1])
11233 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten_range
);
11236 /* Reorder the dimensions of "bmap" according to the given dim_map
11237 * and set the dimension specification to "dim" and
11238 * perform Gaussian elimination on the result.
11240 __isl_give isl_basic_map
*isl_basic_map_realign(__isl_take isl_basic_map
*bmap
,
11241 __isl_take isl_space
*dim
, __isl_take
struct isl_dim_map
*dim_map
)
11243 isl_basic_map
*res
;
11246 bmap
= isl_basic_map_cow(bmap
);
11247 if (!bmap
|| !dim
|| !dim_map
)
11250 flags
= bmap
->flags
;
11251 ISL_FL_CLR(flags
, ISL_BASIC_MAP_FINAL
);
11252 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED
);
11253 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
11254 res
= isl_basic_map_alloc_space(dim
,
11255 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
11256 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
11258 res
->flags
= flags
;
11259 res
= isl_basic_map_gauss(res
, NULL
);
11260 res
= isl_basic_map_finalize(res
);
11264 isl_basic_map_free(bmap
);
11265 isl_space_free(dim
);
11269 /* Reorder the dimensions of "map" according to given reordering.
11271 __isl_give isl_map
*isl_map_realign(__isl_take isl_map
*map
,
11272 __isl_take isl_reordering
*r
)
11275 struct isl_dim_map
*dim_map
;
11277 map
= isl_map_cow(map
);
11278 dim_map
= isl_dim_map_from_reordering(r
);
11279 if (!map
|| !r
|| !dim_map
)
11282 for (i
= 0; i
< map
->n
; ++i
) {
11283 struct isl_dim_map
*dim_map_i
;
11285 dim_map_i
= isl_dim_map_extend(dim_map
, map
->p
[i
]);
11287 map
->p
[i
] = isl_basic_map_realign(map
->p
[i
],
11288 isl_space_copy(r
->dim
), dim_map_i
);
11294 map
= isl_map_reset_space(map
, isl_space_copy(r
->dim
));
11296 isl_reordering_free(r
);
11302 isl_reordering_free(r
);
11306 __isl_give isl_set
*isl_set_realign(__isl_take isl_set
*set
,
11307 __isl_take isl_reordering
*r
)
11309 return set_from_map(isl_map_realign(set_to_map(set
), r
));
11312 __isl_give isl_map
*isl_map_align_params(__isl_take isl_map
*map
,
11313 __isl_take isl_space
*model
)
11317 if (!map
|| !model
)
11320 ctx
= isl_space_get_ctx(model
);
11321 if (!isl_space_has_named_params(model
))
11322 isl_die(ctx
, isl_error_invalid
,
11323 "model has unnamed parameters", goto error
);
11324 if (!isl_space_has_named_params(map
->dim
))
11325 isl_die(ctx
, isl_error_invalid
,
11326 "relation has unnamed parameters", goto error
);
11327 if (!isl_space_match(map
->dim
, isl_dim_param
, model
, isl_dim_param
)) {
11328 isl_reordering
*exp
;
11330 model
= isl_space_drop_dims(model
, isl_dim_in
,
11331 0, isl_space_dim(model
, isl_dim_in
));
11332 model
= isl_space_drop_dims(model
, isl_dim_out
,
11333 0, isl_space_dim(model
, isl_dim_out
));
11334 exp
= isl_parameter_alignment_reordering(map
->dim
, model
);
11335 exp
= isl_reordering_extend_space(exp
, isl_map_get_space(map
));
11336 map
= isl_map_realign(map
, exp
);
11339 isl_space_free(model
);
11342 isl_space_free(model
);
11347 __isl_give isl_set
*isl_set_align_params(__isl_take isl_set
*set
,
11348 __isl_take isl_space
*model
)
11350 return isl_map_align_params(set
, model
);
11353 /* Align the parameters of "bmap" to those of "model", introducing
11354 * additional parameters if needed.
11356 __isl_give isl_basic_map
*isl_basic_map_align_params(
11357 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*model
)
11361 if (!bmap
|| !model
)
11364 ctx
= isl_space_get_ctx(model
);
11365 if (!isl_space_has_named_params(model
))
11366 isl_die(ctx
, isl_error_invalid
,
11367 "model has unnamed parameters", goto error
);
11368 if (!isl_space_has_named_params(bmap
->dim
))
11369 isl_die(ctx
, isl_error_invalid
,
11370 "relation has unnamed parameters", goto error
);
11371 if (!isl_space_match(bmap
->dim
, isl_dim_param
, model
, isl_dim_param
)) {
11372 isl_reordering
*exp
;
11373 struct isl_dim_map
*dim_map
;
11375 model
= isl_space_drop_dims(model
, isl_dim_in
,
11376 0, isl_space_dim(model
, isl_dim_in
));
11377 model
= isl_space_drop_dims(model
, isl_dim_out
,
11378 0, isl_space_dim(model
, isl_dim_out
));
11379 exp
= isl_parameter_alignment_reordering(bmap
->dim
, model
);
11380 exp
= isl_reordering_extend_space(exp
,
11381 isl_basic_map_get_space(bmap
));
11382 dim_map
= isl_dim_map_from_reordering(exp
);
11383 bmap
= isl_basic_map_realign(bmap
,
11384 exp
? isl_space_copy(exp
->dim
) : NULL
,
11385 isl_dim_map_extend(dim_map
, bmap
));
11386 isl_reordering_free(exp
);
11390 isl_space_free(model
);
11393 isl_space_free(model
);
11394 isl_basic_map_free(bmap
);
11398 /* Align the parameters of "bset" to those of "model", introducing
11399 * additional parameters if needed.
11401 __isl_give isl_basic_set
*isl_basic_set_align_params(
11402 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*model
)
11404 return isl_basic_map_align_params(bset
, model
);
11407 __isl_give isl_mat
*isl_basic_map_equalities_matrix(
11408 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
11409 enum isl_dim_type c2
, enum isl_dim_type c3
,
11410 enum isl_dim_type c4
, enum isl_dim_type c5
)
11412 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11413 struct isl_mat
*mat
;
11419 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_eq
,
11420 isl_basic_map_total_dim(bmap
) + 1);
11423 for (i
= 0; i
< bmap
->n_eq
; ++i
)
11424 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11425 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11426 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11427 isl_int_set(mat
->row
[i
][pos
],
11428 bmap
->eq
[i
][off
+ k
]);
11436 __isl_give isl_mat
*isl_basic_map_inequalities_matrix(
11437 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
11438 enum isl_dim_type c2
, enum isl_dim_type c3
,
11439 enum isl_dim_type c4
, enum isl_dim_type c5
)
11441 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11442 struct isl_mat
*mat
;
11448 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_ineq
,
11449 isl_basic_map_total_dim(bmap
) + 1);
11452 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
11453 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11454 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11455 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11456 isl_int_set(mat
->row
[i
][pos
],
11457 bmap
->ineq
[i
][off
+ k
]);
11465 __isl_give isl_basic_map
*isl_basic_map_from_constraint_matrices(
11466 __isl_take isl_space
*dim
,
11467 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
11468 enum isl_dim_type c2
, enum isl_dim_type c3
,
11469 enum isl_dim_type c4
, enum isl_dim_type c5
)
11471 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11472 isl_basic_map
*bmap
;
11478 if (!dim
|| !eq
|| !ineq
)
11481 if (eq
->n_col
!= ineq
->n_col
)
11482 isl_die(dim
->ctx
, isl_error_invalid
,
11483 "equalities and inequalities matrices should have "
11484 "same number of columns", goto error
);
11486 total
= 1 + isl_space_dim(dim
, isl_dim_all
);
11488 if (eq
->n_col
< total
)
11489 isl_die(dim
->ctx
, isl_error_invalid
,
11490 "number of columns too small", goto error
);
11492 extra
= eq
->n_col
- total
;
11494 bmap
= isl_basic_map_alloc_space(isl_space_copy(dim
), extra
,
11495 eq
->n_row
, ineq
->n_row
);
11498 for (i
= 0; i
< extra
; ++i
) {
11499 k
= isl_basic_map_alloc_div(bmap
);
11502 isl_int_set_si(bmap
->div
[k
][0], 0);
11504 for (i
= 0; i
< eq
->n_row
; ++i
) {
11505 l
= isl_basic_map_alloc_equality(bmap
);
11508 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11509 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11510 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11511 isl_int_set(bmap
->eq
[l
][off
+ k
],
11517 for (i
= 0; i
< ineq
->n_row
; ++i
) {
11518 l
= isl_basic_map_alloc_inequality(bmap
);
11521 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11522 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11523 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11524 isl_int_set(bmap
->ineq
[l
][off
+ k
],
11525 ineq
->row
[i
][pos
]);
11531 isl_space_free(dim
);
11533 isl_mat_free(ineq
);
11535 bmap
= isl_basic_map_simplify(bmap
);
11536 return isl_basic_map_finalize(bmap
);
11538 isl_space_free(dim
);
11540 isl_mat_free(ineq
);
11544 __isl_give isl_mat
*isl_basic_set_equalities_matrix(
11545 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
11546 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
11548 return isl_basic_map_equalities_matrix(bset_to_bmap(bset
),
11549 c1
, c2
, c3
, c4
, isl_dim_in
);
11552 __isl_give isl_mat
*isl_basic_set_inequalities_matrix(
11553 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
11554 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
11556 return isl_basic_map_inequalities_matrix(bset_to_bmap(bset
),
11557 c1
, c2
, c3
, c4
, isl_dim_in
);
11560 __isl_give isl_basic_set
*isl_basic_set_from_constraint_matrices(
11561 __isl_take isl_space
*dim
,
11562 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
11563 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
11565 isl_basic_map
*bmap
;
11566 bmap
= isl_basic_map_from_constraint_matrices(dim
, eq
, ineq
,
11567 c1
, c2
, c3
, c4
, isl_dim_in
);
11568 return bset_from_bmap(bmap
);
11571 isl_bool
isl_basic_map_can_zip(__isl_keep isl_basic_map
*bmap
)
11574 return isl_bool_error
;
11576 return isl_space_can_zip(bmap
->dim
);
11579 isl_bool
isl_map_can_zip(__isl_keep isl_map
*map
)
11582 return isl_bool_error
;
11584 return isl_space_can_zip(map
->dim
);
11587 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
11588 * (A -> C) -> (B -> D).
11590 __isl_give isl_basic_map
*isl_basic_map_zip(__isl_take isl_basic_map
*bmap
)
11599 if (!isl_basic_map_can_zip(bmap
))
11600 isl_die(bmap
->ctx
, isl_error_invalid
,
11601 "basic map cannot be zipped", goto error
);
11602 pos
= isl_basic_map_offset(bmap
, isl_dim_in
) +
11603 isl_space_dim(bmap
->dim
->nested
[0], isl_dim_in
);
11604 n1
= isl_space_dim(bmap
->dim
->nested
[0], isl_dim_out
);
11605 n2
= isl_space_dim(bmap
->dim
->nested
[1], isl_dim_in
);
11606 bmap
= isl_basic_map_cow(bmap
);
11607 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
11610 bmap
->dim
= isl_space_zip(bmap
->dim
);
11613 bmap
= isl_basic_map_mark_final(bmap
);
11616 isl_basic_map_free(bmap
);
11620 /* Given a map (A -> B) -> (C -> D), return the corresponding map
11621 * (A -> C) -> (B -> D).
11623 __isl_give isl_map
*isl_map_zip(__isl_take isl_map
*map
)
11630 if (!isl_map_can_zip(map
))
11631 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be zipped",
11634 map
= isl_map_cow(map
);
11638 for (i
= 0; i
< map
->n
; ++i
) {
11639 map
->p
[i
] = isl_basic_map_zip(map
->p
[i
]);
11644 map
->dim
= isl_space_zip(map
->dim
);
11654 /* Can we apply isl_basic_map_curry to "bmap"?
11655 * That is, does it have a nested relation in its domain?
11657 isl_bool
isl_basic_map_can_curry(__isl_keep isl_basic_map
*bmap
)
11660 return isl_bool_error
;
11662 return isl_space_can_curry(bmap
->dim
);
11665 /* Can we apply isl_map_curry to "map"?
11666 * That is, does it have a nested relation in its domain?
11668 isl_bool
isl_map_can_curry(__isl_keep isl_map
*map
)
11671 return isl_bool_error
;
11673 return isl_space_can_curry(map
->dim
);
11676 /* Given a basic map (A -> B) -> C, return the corresponding basic map
11679 __isl_give isl_basic_map
*isl_basic_map_curry(__isl_take isl_basic_map
*bmap
)
11685 if (!isl_basic_map_can_curry(bmap
))
11686 isl_die(bmap
->ctx
, isl_error_invalid
,
11687 "basic map cannot be curried", goto error
);
11688 bmap
= isl_basic_map_cow(bmap
);
11691 bmap
->dim
= isl_space_curry(bmap
->dim
);
11694 bmap
= isl_basic_map_mark_final(bmap
);
11697 isl_basic_map_free(bmap
);
11701 /* Given a map (A -> B) -> C, return the corresponding map
11704 __isl_give isl_map
*isl_map_curry(__isl_take isl_map
*map
)
11706 return isl_map_change_space(map
, &isl_map_can_curry
,
11707 "map cannot be curried", &isl_space_curry
);
11710 /* Can isl_map_range_curry be applied to "map"?
11711 * That is, does it have a nested relation in its range,
11712 * the domain of which is itself a nested relation?
11714 isl_bool
isl_map_can_range_curry(__isl_keep isl_map
*map
)
11717 return isl_bool_error
;
11719 return isl_space_can_range_curry(map
->dim
);
11722 /* Given a map A -> ((B -> C) -> D), return the corresponding map
11723 * A -> (B -> (C -> D)).
11725 __isl_give isl_map
*isl_map_range_curry(__isl_take isl_map
*map
)
11727 return isl_map_change_space(map
, &isl_map_can_range_curry
,
11728 "map range cannot be curried",
11729 &isl_space_range_curry
);
11732 /* Can we apply isl_basic_map_uncurry to "bmap"?
11733 * That is, does it have a nested relation in its domain?
11735 isl_bool
isl_basic_map_can_uncurry(__isl_keep isl_basic_map
*bmap
)
11738 return isl_bool_error
;
11740 return isl_space_can_uncurry(bmap
->dim
);
11743 /* Can we apply isl_map_uncurry to "map"?
11744 * That is, does it have a nested relation in its domain?
11746 isl_bool
isl_map_can_uncurry(__isl_keep isl_map
*map
)
11749 return isl_bool_error
;
11751 return isl_space_can_uncurry(map
->dim
);
11754 /* Given a basic map A -> (B -> C), return the corresponding basic map
11757 __isl_give isl_basic_map
*isl_basic_map_uncurry(__isl_take isl_basic_map
*bmap
)
11763 if (!isl_basic_map_can_uncurry(bmap
))
11764 isl_die(bmap
->ctx
, isl_error_invalid
,
11765 "basic map cannot be uncurried",
11766 return isl_basic_map_free(bmap
));
11767 bmap
= isl_basic_map_cow(bmap
);
11770 bmap
->dim
= isl_space_uncurry(bmap
->dim
);
11772 return isl_basic_map_free(bmap
);
11773 bmap
= isl_basic_map_mark_final(bmap
);
11777 /* Given a map A -> (B -> C), return the corresponding map
11780 __isl_give isl_map
*isl_map_uncurry(__isl_take isl_map
*map
)
11782 return isl_map_change_space(map
, &isl_map_can_uncurry
,
11783 "map cannot be uncurried", &isl_space_uncurry
);
11786 /* Construct a basic map mapping the domain of the affine expression
11787 * to a one-dimensional range prescribed by the affine expression.
11788 * If "rational" is set, then construct a rational basic map.
11790 * A NaN affine expression cannot be converted to a basic map.
11792 static __isl_give isl_basic_map
*isl_basic_map_from_aff2(
11793 __isl_take isl_aff
*aff
, int rational
)
11798 isl_local_space
*ls
;
11799 isl_basic_map
*bmap
= NULL
;
11803 is_nan
= isl_aff_is_nan(aff
);
11807 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
11808 "cannot convert NaN", goto error
);
11810 ls
= isl_aff_get_local_space(aff
);
11811 bmap
= isl_basic_map_from_local_space(ls
);
11812 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
11813 k
= isl_basic_map_alloc_equality(bmap
);
11817 pos
= isl_basic_map_offset(bmap
, isl_dim_out
);
11818 isl_seq_cpy(bmap
->eq
[k
], aff
->v
->el
+ 1, pos
);
11819 isl_int_neg(bmap
->eq
[k
][pos
], aff
->v
->el
[0]);
11820 isl_seq_cpy(bmap
->eq
[k
] + pos
+ 1, aff
->v
->el
+ 1 + pos
,
11821 aff
->v
->size
- (pos
+ 1));
11825 bmap
= isl_basic_map_set_rational(bmap
);
11826 bmap
= isl_basic_map_finalize(bmap
);
11830 isl_basic_map_free(bmap
);
11834 /* Construct a basic map mapping the domain of the affine expression
11835 * to a one-dimensional range prescribed by the affine expression.
11837 __isl_give isl_basic_map
*isl_basic_map_from_aff(__isl_take isl_aff
*aff
)
11839 return isl_basic_map_from_aff2(aff
, 0);
11842 /* Construct a map mapping the domain of the affine expression
11843 * to a one-dimensional range prescribed by the affine expression.
11845 __isl_give isl_map
*isl_map_from_aff(__isl_take isl_aff
*aff
)
11847 isl_basic_map
*bmap
;
11849 bmap
= isl_basic_map_from_aff(aff
);
11850 return isl_map_from_basic_map(bmap
);
11853 /* Construct a basic map mapping the domain the multi-affine expression
11854 * to its range, with each dimension in the range equated to the
11855 * corresponding affine expression.
11856 * If "rational" is set, then construct a rational basic map.
11858 __isl_give isl_basic_map
*isl_basic_map_from_multi_aff2(
11859 __isl_take isl_multi_aff
*maff
, int rational
)
11863 isl_basic_map
*bmap
;
11868 if (isl_space_dim(maff
->space
, isl_dim_out
) != maff
->n
)
11869 isl_die(isl_multi_aff_get_ctx(maff
), isl_error_internal
,
11870 "invalid space", goto error
);
11872 space
= isl_space_domain(isl_multi_aff_get_space(maff
));
11873 bmap
= isl_basic_map_universe(isl_space_from_domain(space
));
11875 bmap
= isl_basic_map_set_rational(bmap
);
11877 for (i
= 0; i
< maff
->n
; ++i
) {
11879 isl_basic_map
*bmap_i
;
11881 aff
= isl_aff_copy(maff
->p
[i
]);
11882 bmap_i
= isl_basic_map_from_aff2(aff
, rational
);
11884 bmap
= isl_basic_map_flat_range_product(bmap
, bmap_i
);
11887 bmap
= isl_basic_map_reset_space(bmap
, isl_multi_aff_get_space(maff
));
11889 isl_multi_aff_free(maff
);
11892 isl_multi_aff_free(maff
);
11896 /* Construct a basic map mapping the domain the multi-affine expression
11897 * to its range, with each dimension in the range equated to the
11898 * corresponding affine expression.
11900 __isl_give isl_basic_map
*isl_basic_map_from_multi_aff(
11901 __isl_take isl_multi_aff
*ma
)
11903 return isl_basic_map_from_multi_aff2(ma
, 0);
11906 /* Construct a map mapping the domain the multi-affine expression
11907 * to its range, with each dimension in the range equated to the
11908 * corresponding affine expression.
11910 __isl_give isl_map
*isl_map_from_multi_aff(__isl_take isl_multi_aff
*maff
)
11912 isl_basic_map
*bmap
;
11914 bmap
= isl_basic_map_from_multi_aff(maff
);
11915 return isl_map_from_basic_map(bmap
);
11918 /* Construct a basic map mapping a domain in the given space to
11919 * to an n-dimensional range, with n the number of elements in the list,
11920 * where each coordinate in the range is prescribed by the
11921 * corresponding affine expression.
11922 * The domains of all affine expressions in the list are assumed to match
11925 __isl_give isl_basic_map
*isl_basic_map_from_aff_list(
11926 __isl_take isl_space
*domain_dim
, __isl_take isl_aff_list
*list
)
11930 isl_basic_map
*bmap
;
11935 dim
= isl_space_from_domain(domain_dim
);
11936 bmap
= isl_basic_map_universe(dim
);
11938 for (i
= 0; i
< list
->n
; ++i
) {
11940 isl_basic_map
*bmap_i
;
11942 aff
= isl_aff_copy(list
->p
[i
]);
11943 bmap_i
= isl_basic_map_from_aff(aff
);
11945 bmap
= isl_basic_map_flat_range_product(bmap
, bmap_i
);
11948 isl_aff_list_free(list
);
11952 __isl_give isl_set
*isl_set_equate(__isl_take isl_set
*set
,
11953 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11955 return isl_map_equate(set
, type1
, pos1
, type2
, pos2
);
11958 /* Construct a basic map where the given dimensions are equal to each other.
11960 static __isl_give isl_basic_map
*equator(__isl_take isl_space
*space
,
11961 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
11963 isl_basic_map
*bmap
= NULL
;
11969 if (pos1
>= isl_space_dim(space
, type1
))
11970 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
11971 "index out of bounds", goto error
);
11972 if (pos2
>= isl_space_dim(space
, type2
))
11973 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
11974 "index out of bounds", goto error
);
11976 if (type1
== type2
&& pos1
== pos2
)
11977 return isl_basic_map_universe(space
);
11979 bmap
= isl_basic_map_alloc_space(isl_space_copy(space
), 0, 1, 0);
11980 i
= isl_basic_map_alloc_equality(bmap
);
11983 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
11984 pos1
+= isl_basic_map_offset(bmap
, type1
);
11985 pos2
+= isl_basic_map_offset(bmap
, type2
);
11986 isl_int_set_si(bmap
->eq
[i
][pos1
], -1);
11987 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
11988 bmap
= isl_basic_map_finalize(bmap
);
11989 isl_space_free(space
);
11992 isl_space_free(space
);
11993 isl_basic_map_free(bmap
);
11997 /* Add a constraint imposing that the given two dimensions are equal.
11999 __isl_give isl_basic_map
*isl_basic_map_equate(__isl_take isl_basic_map
*bmap
,
12000 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12004 eq
= equator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
12006 bmap
= isl_basic_map_intersect(bmap
, eq
);
12011 /* Add a constraint imposing that the given two dimensions are equal.
12013 __isl_give isl_map
*isl_map_equate(__isl_take isl_map
*map
,
12014 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12016 isl_basic_map
*bmap
;
12018 bmap
= equator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
12020 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
12025 /* Add a constraint imposing that the given two dimensions have opposite values.
12027 __isl_give isl_map
*isl_map_oppose(__isl_take isl_map
*map
,
12028 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12030 isl_basic_map
*bmap
= NULL
;
12036 if (pos1
>= isl_map_dim(map
, type1
))
12037 isl_die(map
->ctx
, isl_error_invalid
,
12038 "index out of bounds", goto error
);
12039 if (pos2
>= isl_map_dim(map
, type2
))
12040 isl_die(map
->ctx
, isl_error_invalid
,
12041 "index out of bounds", goto error
);
12043 bmap
= isl_basic_map_alloc_space(isl_map_get_space(map
), 0, 1, 0);
12044 i
= isl_basic_map_alloc_equality(bmap
);
12047 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
12048 pos1
+= isl_basic_map_offset(bmap
, type1
);
12049 pos2
+= isl_basic_map_offset(bmap
, type2
);
12050 isl_int_set_si(bmap
->eq
[i
][pos1
], 1);
12051 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
12052 bmap
= isl_basic_map_finalize(bmap
);
12054 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
12058 isl_basic_map_free(bmap
);
12063 /* Construct a constraint imposing that the value of the first dimension is
12064 * greater than or equal to that of the second.
12066 static __isl_give isl_constraint
*constraint_order_ge(
12067 __isl_take isl_space
*space
, enum isl_dim_type type1
, int pos1
,
12068 enum isl_dim_type type2
, int pos2
)
12075 c
= isl_constraint_alloc_inequality(isl_local_space_from_space(space
));
12077 if (pos1
>= isl_constraint_dim(c
, type1
))
12078 isl_die(isl_constraint_get_ctx(c
), isl_error_invalid
,
12079 "index out of bounds", return isl_constraint_free(c
));
12080 if (pos2
>= isl_constraint_dim(c
, type2
))
12081 isl_die(isl_constraint_get_ctx(c
), isl_error_invalid
,
12082 "index out of bounds", return isl_constraint_free(c
));
12084 if (type1
== type2
&& pos1
== pos2
)
12087 c
= isl_constraint_set_coefficient_si(c
, type1
, pos1
, 1);
12088 c
= isl_constraint_set_coefficient_si(c
, type2
, pos2
, -1);
12093 /* Add a constraint imposing that the value of the first dimension is
12094 * greater than or equal to that of the second.
12096 __isl_give isl_basic_map
*isl_basic_map_order_ge(__isl_take isl_basic_map
*bmap
,
12097 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12102 if (type1
== type2
&& pos1
== pos2
)
12104 space
= isl_basic_map_get_space(bmap
);
12105 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
12106 bmap
= isl_basic_map_add_constraint(bmap
, c
);
12111 /* Add a constraint imposing that the value of the first dimension is
12112 * greater than or equal to that of the second.
12114 __isl_give isl_map
*isl_map_order_ge(__isl_take isl_map
*map
,
12115 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12120 if (type1
== type2
&& pos1
== pos2
)
12122 space
= isl_map_get_space(map
);
12123 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
12124 map
= isl_map_add_constraint(map
, c
);
12129 /* Add a constraint imposing that the value of the first dimension is
12130 * less than or equal to that of the second.
12132 __isl_give isl_map
*isl_map_order_le(__isl_take isl_map
*map
,
12133 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12135 return isl_map_order_ge(map
, type2
, pos2
, type1
, pos1
);
12138 /* Construct a basic map where the value of the first dimension is
12139 * greater than that of the second.
12141 static __isl_give isl_basic_map
*greator(__isl_take isl_space
*space
,
12142 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12144 isl_basic_map
*bmap
= NULL
;
12150 if (pos1
>= isl_space_dim(space
, type1
))
12151 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
12152 "index out of bounds", goto error
);
12153 if (pos2
>= isl_space_dim(space
, type2
))
12154 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
12155 "index out of bounds", goto error
);
12157 if (type1
== type2
&& pos1
== pos2
)
12158 return isl_basic_map_empty(space
);
12160 bmap
= isl_basic_map_alloc_space(space
, 0, 0, 1);
12161 i
= isl_basic_map_alloc_inequality(bmap
);
12163 return isl_basic_map_free(bmap
);
12164 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
12165 pos1
+= isl_basic_map_offset(bmap
, type1
);
12166 pos2
+= isl_basic_map_offset(bmap
, type2
);
12167 isl_int_set_si(bmap
->ineq
[i
][pos1
], 1);
12168 isl_int_set_si(bmap
->ineq
[i
][pos2
], -1);
12169 isl_int_set_si(bmap
->ineq
[i
][0], -1);
12170 bmap
= isl_basic_map_finalize(bmap
);
12174 isl_space_free(space
);
12175 isl_basic_map_free(bmap
);
12179 /* Add a constraint imposing that the value of the first dimension is
12180 * greater than that of the second.
12182 __isl_give isl_basic_map
*isl_basic_map_order_gt(__isl_take isl_basic_map
*bmap
,
12183 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12187 gt
= greator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
12189 bmap
= isl_basic_map_intersect(bmap
, gt
);
12194 /* Add a constraint imposing that the value of the first dimension is
12195 * greater than that of the second.
12197 __isl_give isl_map
*isl_map_order_gt(__isl_take isl_map
*map
,
12198 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12200 isl_basic_map
*bmap
;
12202 bmap
= greator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
12204 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
12209 /* Add a constraint imposing that the value of the first dimension is
12210 * smaller than that of the second.
12212 __isl_give isl_map
*isl_map_order_lt(__isl_take isl_map
*map
,
12213 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12215 return isl_map_order_gt(map
, type2
, pos2
, type1
, pos1
);
12218 __isl_give isl_aff
*isl_basic_map_get_div(__isl_keep isl_basic_map
*bmap
,
12222 isl_local_space
*ls
;
12227 if (!isl_basic_map_divs_known(bmap
))
12228 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12229 "some divs are unknown", return NULL
);
12231 ls
= isl_basic_map_get_local_space(bmap
);
12232 div
= isl_local_space_get_div(ls
, pos
);
12233 isl_local_space_free(ls
);
12238 __isl_give isl_aff
*isl_basic_set_get_div(__isl_keep isl_basic_set
*bset
,
12241 return isl_basic_map_get_div(bset
, pos
);
12244 /* Plug in "subs" for dimension "type", "pos" of "bset".
12246 * Let i be the dimension to replace and let "subs" be of the form
12250 * Any integer division with a non-zero coefficient for i,
12252 * floor((a i + g)/m)
12256 * floor((a f + d g)/(m d))
12258 * Constraints of the form
12266 * We currently require that "subs" is an integral expression.
12267 * Handling rational expressions may require us to add stride constraints
12268 * as we do in isl_basic_set_preimage_multi_aff.
12270 __isl_give isl_basic_set
*isl_basic_set_substitute(
12271 __isl_take isl_basic_set
*bset
,
12272 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
12278 if (bset
&& isl_basic_set_plain_is_empty(bset
))
12281 bset
= isl_basic_set_cow(bset
);
12282 if (!bset
|| !subs
)
12285 ctx
= isl_basic_set_get_ctx(bset
);
12286 if (!isl_space_is_equal(bset
->dim
, subs
->ls
->dim
))
12287 isl_die(ctx
, isl_error_invalid
,
12288 "spaces don't match", goto error
);
12289 if (isl_local_space_dim(subs
->ls
, isl_dim_div
) != 0)
12290 isl_die(ctx
, isl_error_unsupported
,
12291 "cannot handle divs yet", goto error
);
12292 if (!isl_int_is_one(subs
->v
->el
[0]))
12293 isl_die(ctx
, isl_error_invalid
,
12294 "can only substitute integer expressions", goto error
);
12296 pos
+= isl_basic_set_offset(bset
, type
);
12300 for (i
= 0; i
< bset
->n_eq
; ++i
) {
12301 if (isl_int_is_zero(bset
->eq
[i
][pos
]))
12303 isl_int_set(v
, bset
->eq
[i
][pos
]);
12304 isl_int_set_si(bset
->eq
[i
][pos
], 0);
12305 isl_seq_combine(bset
->eq
[i
], subs
->v
->el
[0], bset
->eq
[i
],
12306 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12309 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
12310 if (isl_int_is_zero(bset
->ineq
[i
][pos
]))
12312 isl_int_set(v
, bset
->ineq
[i
][pos
]);
12313 isl_int_set_si(bset
->ineq
[i
][pos
], 0);
12314 isl_seq_combine(bset
->ineq
[i
], subs
->v
->el
[0], bset
->ineq
[i
],
12315 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12318 for (i
= 0; i
< bset
->n_div
; ++i
) {
12319 if (isl_int_is_zero(bset
->div
[i
][1 + pos
]))
12321 isl_int_set(v
, bset
->div
[i
][1 + pos
]);
12322 isl_int_set_si(bset
->div
[i
][1 + pos
], 0);
12323 isl_seq_combine(bset
->div
[i
] + 1,
12324 subs
->v
->el
[0], bset
->div
[i
] + 1,
12325 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12326 isl_int_mul(bset
->div
[i
][0], bset
->div
[i
][0], subs
->v
->el
[0]);
12331 bset
= isl_basic_set_simplify(bset
);
12332 return isl_basic_set_finalize(bset
);
12334 isl_basic_set_free(bset
);
12338 /* Plug in "subs" for dimension "type", "pos" of "set".
12340 __isl_give isl_set
*isl_set_substitute(__isl_take isl_set
*set
,
12341 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
12345 if (set
&& isl_set_plain_is_empty(set
))
12348 set
= isl_set_cow(set
);
12352 for (i
= set
->n
- 1; i
>= 0; --i
) {
12353 set
->p
[i
] = isl_basic_set_substitute(set
->p
[i
], type
, pos
, subs
);
12354 if (remove_if_empty(set
, i
) < 0)
12364 /* Check if the range of "ma" is compatible with the domain or range
12365 * (depending on "type") of "bmap".
12367 static isl_stat
check_basic_map_compatible_range_multi_aff(
12368 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type type
,
12369 __isl_keep isl_multi_aff
*ma
)
12372 isl_space
*ma_space
;
12374 ma_space
= isl_multi_aff_get_space(ma
);
12376 m
= isl_space_match(bmap
->dim
, isl_dim_param
, ma_space
, isl_dim_param
);
12380 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12381 "parameters don't match", goto error
);
12382 m
= isl_space_tuple_is_equal(bmap
->dim
, type
, ma_space
, isl_dim_out
);
12386 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12387 "spaces don't match", goto error
);
12389 isl_space_free(ma_space
);
12390 return isl_stat_ok
;
12392 isl_space_free(ma_space
);
12393 return isl_stat_error
;
12396 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12397 * coefficients before the transformed range of dimensions,
12398 * the "n_after" coefficients after the transformed range of dimensions
12399 * and the coefficients of the other divs in "bmap".
12401 static int set_ma_divs(__isl_keep isl_basic_map
*bmap
,
12402 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
, int n_div
)
12407 isl_local_space
*ls
;
12412 ls
= isl_aff_get_domain_local_space(ma
->p
[0]);
12416 n_param
= isl_local_space_dim(ls
, isl_dim_param
);
12417 n_set
= isl_local_space_dim(ls
, isl_dim_set
);
12418 for (i
= 0; i
< n_div
; ++i
) {
12419 int o_bmap
= 0, o_ls
= 0;
12421 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], 1 + 1 + n_param
);
12422 o_bmap
+= 1 + 1 + n_param
;
12423 o_ls
+= 1 + 1 + n_param
;
12424 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_before
);
12425 o_bmap
+= n_before
;
12426 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
12427 ls
->div
->row
[i
] + o_ls
, n_set
);
12430 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_after
);
12432 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
12433 ls
->div
->row
[i
] + o_ls
, n_div
);
12436 isl_seq_clr(bmap
->div
[i
] + o_bmap
, bmap
->n_div
- n_div
);
12437 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
12441 isl_local_space_free(ls
);
12444 isl_local_space_free(ls
);
12448 /* How many stride constraints does "ma" enforce?
12449 * That is, how many of the affine expressions have a denominator
12450 * different from one?
12452 static int multi_aff_strides(__isl_keep isl_multi_aff
*ma
)
12457 for (i
= 0; i
< ma
->n
; ++i
)
12458 if (!isl_int_is_one(ma
->p
[i
]->v
->el
[0]))
12464 /* For each affine expression in ma of the form
12466 * x_i = (f_i y + h_i)/m_i
12468 * with m_i different from one, add a constraint to "bmap"
12471 * f_i y + h_i = m_i alpha_i
12473 * with alpha_i an additional existentially quantified variable.
12475 * The input variables of "ma" correspond to a subset of the variables
12476 * of "bmap". There are "n_before" variables in "bmap" before this
12477 * subset and "n_after" variables after this subset.
12478 * The integer divisions of the affine expressions in "ma" are assumed
12479 * to have been aligned. There are "n_div_ma" of them and
12480 * they appear first in "bmap", straight after the "n_after" variables.
12482 static __isl_give isl_basic_map
*add_ma_strides(
12483 __isl_take isl_basic_map
*bmap
, __isl_keep isl_multi_aff
*ma
,
12484 int n_before
, int n_after
, int n_div_ma
)
12492 total
= isl_basic_map_total_dim(bmap
);
12493 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
12494 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
12495 for (i
= 0; i
< ma
->n
; ++i
) {
12496 int o_bmap
= 0, o_ma
= 1;
12498 if (isl_int_is_one(ma
->p
[i
]->v
->el
[0]))
12500 div
= isl_basic_map_alloc_div(bmap
);
12501 k
= isl_basic_map_alloc_equality(bmap
);
12502 if (div
< 0 || k
< 0)
12504 isl_int_set_si(bmap
->div
[div
][0], 0);
12505 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12506 ma
->p
[i
]->v
->el
+ o_ma
, 1 + n_param
);
12507 o_bmap
+= 1 + n_param
;
12508 o_ma
+= 1 + n_param
;
12509 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_before
);
12510 o_bmap
+= n_before
;
12511 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12512 ma
->p
[i
]->v
->el
+ o_ma
, n_in
);
12515 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_after
);
12517 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12518 ma
->p
[i
]->v
->el
+ o_ma
, n_div_ma
);
12519 o_bmap
+= n_div_ma
;
12521 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, 1 + total
- o_bmap
);
12522 isl_int_neg(bmap
->eq
[k
][1 + total
], ma
->p
[i
]->v
->el
[0]);
12528 isl_basic_map_free(bmap
);
12532 /* Replace the domain or range space (depending on "type) of "space" by "set".
12534 static __isl_give isl_space
*isl_space_set(__isl_take isl_space
*space
,
12535 enum isl_dim_type type
, __isl_take isl_space
*set
)
12537 if (type
== isl_dim_in
) {
12538 space
= isl_space_range(space
);
12539 space
= isl_space_map_from_domain_and_range(set
, space
);
12541 space
= isl_space_domain(space
);
12542 space
= isl_space_map_from_domain_and_range(space
, set
);
12548 /* Compute the preimage of the domain or range (depending on "type")
12549 * of "bmap" under the function represented by "ma".
12550 * In other words, plug in "ma" in the domain or range of "bmap".
12551 * The result is a basic map that lives in the same space as "bmap"
12552 * except that the domain or range has been replaced by
12553 * the domain space of "ma".
12555 * If bmap is represented by
12557 * A(p) + S u + B x + T v + C(divs) >= 0,
12559 * where u and x are input and output dimensions if type == isl_dim_out
12560 * while x and v are input and output dimensions if type == isl_dim_in,
12561 * and ma is represented by
12563 * x = D(p) + F(y) + G(divs')
12565 * then the result is
12567 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12569 * The divs in the input set are similarly adjusted.
12572 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12576 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
12577 * B_i G(divs') + c_i(divs))/n_i)
12579 * If bmap is not a rational map and if F(y) involves any denominators
12581 * x_i = (f_i y + h_i)/m_i
12583 * then additional constraints are added to ensure that we only
12584 * map back integer points. That is we enforce
12586 * f_i y + h_i = m_i alpha_i
12588 * with alpha_i an additional existentially quantified variable.
12590 * We first copy over the divs from "ma".
12591 * Then we add the modified constraints and divs from "bmap".
12592 * Finally, we add the stride constraints, if needed.
12594 __isl_give isl_basic_map
*isl_basic_map_preimage_multi_aff(
12595 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
12596 __isl_take isl_multi_aff
*ma
)
12600 isl_basic_map
*res
= NULL
;
12601 int n_before
, n_after
, n_div_bmap
, n_div_ma
;
12602 isl_int f
, c1
, c2
, g
;
12611 ma
= isl_multi_aff_align_divs(ma
);
12614 if (check_basic_map_compatible_range_multi_aff(bmap
, type
, ma
) < 0)
12617 if (type
== isl_dim_in
) {
12619 n_after
= isl_basic_map_dim(bmap
, isl_dim_out
);
12621 n_before
= isl_basic_map_dim(bmap
, isl_dim_in
);
12624 n_div_bmap
= isl_basic_map_dim(bmap
, isl_dim_div
);
12625 n_div_ma
= ma
->n
? isl_aff_dim(ma
->p
[0], isl_dim_div
) : 0;
12627 space
= isl_multi_aff_get_domain_space(ma
);
12628 space
= isl_space_set(isl_basic_map_get_space(bmap
), type
, space
);
12629 rational
= isl_basic_map_is_rational(bmap
);
12630 strides
= rational
? 0 : multi_aff_strides(ma
);
12631 res
= isl_basic_map_alloc_space(space
, n_div_ma
+ n_div_bmap
+ strides
,
12632 bmap
->n_eq
+ strides
, bmap
->n_ineq
+ 2 * n_div_ma
);
12634 res
= isl_basic_map_set_rational(res
);
12636 for (i
= 0; i
< n_div_ma
+ n_div_bmap
; ++i
)
12637 if (isl_basic_map_alloc_div(res
) < 0)
12640 if (set_ma_divs(res
, ma
, n_before
, n_after
, n_div_ma
) < 0)
12643 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
12644 k
= isl_basic_map_alloc_equality(res
);
12647 isl_seq_preimage(res
->eq
[k
], bmap
->eq
[i
], ma
, n_before
,
12648 n_after
, n_div_ma
, n_div_bmap
, f
, c1
, c2
, g
, 0);
12651 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
12652 k
= isl_basic_map_alloc_inequality(res
);
12655 isl_seq_preimage(res
->ineq
[k
], bmap
->ineq
[i
], ma
, n_before
,
12656 n_after
, n_div_ma
, n_div_bmap
, f
, c1
, c2
, g
, 0);
12659 for (i
= 0; i
< bmap
->n_div
; ++i
) {
12660 if (isl_int_is_zero(bmap
->div
[i
][0])) {
12661 isl_int_set_si(res
->div
[n_div_ma
+ i
][0], 0);
12664 isl_seq_preimage(res
->div
[n_div_ma
+ i
], bmap
->div
[i
], ma
,
12665 n_before
, n_after
, n_div_ma
, n_div_bmap
,
12670 res
= add_ma_strides(res
, ma
, n_before
, n_after
, n_div_ma
);
12676 isl_basic_map_free(bmap
);
12677 isl_multi_aff_free(ma
);
12678 res
= isl_basic_map_simplify(res
);
12679 return isl_basic_map_finalize(res
);
12685 isl_basic_map_free(bmap
);
12686 isl_multi_aff_free(ma
);
12687 isl_basic_map_free(res
);
12691 /* Compute the preimage of "bset" under the function represented by "ma".
12692 * In other words, plug in "ma" in "bset". The result is a basic set
12693 * that lives in the domain space of "ma".
12695 __isl_give isl_basic_set
*isl_basic_set_preimage_multi_aff(
12696 __isl_take isl_basic_set
*bset
, __isl_take isl_multi_aff
*ma
)
12698 return isl_basic_map_preimage_multi_aff(bset
, isl_dim_set
, ma
);
12701 /* Compute the preimage of the domain of "bmap" under the function
12702 * represented by "ma".
12703 * In other words, plug in "ma" in the domain of "bmap".
12704 * The result is a basic map that lives in the same space as "bmap"
12705 * except that the domain has been replaced by the domain space of "ma".
12707 __isl_give isl_basic_map
*isl_basic_map_preimage_domain_multi_aff(
12708 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
12710 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_in
, ma
);
12713 /* Compute the preimage of the range of "bmap" under the function
12714 * represented by "ma".
12715 * In other words, plug in "ma" in the range of "bmap".
12716 * The result is a basic map that lives in the same space as "bmap"
12717 * except that the range has been replaced by the domain space of "ma".
12719 __isl_give isl_basic_map
*isl_basic_map_preimage_range_multi_aff(
12720 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
12722 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_out
, ma
);
12725 /* Check if the range of "ma" is compatible with the domain or range
12726 * (depending on "type") of "map".
12727 * Return isl_stat_error if anything is wrong.
12729 static isl_stat
check_map_compatible_range_multi_aff(
12730 __isl_keep isl_map
*map
, enum isl_dim_type type
,
12731 __isl_keep isl_multi_aff
*ma
)
12734 isl_space
*ma_space
;
12736 ma_space
= isl_multi_aff_get_space(ma
);
12737 m
= isl_space_tuple_is_equal(map
->dim
, type
, ma_space
, isl_dim_out
);
12738 isl_space_free(ma_space
);
12740 return isl_stat_error
;
12742 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
12743 "spaces don't match", return isl_stat_error
);
12744 return isl_stat_ok
;
12747 /* Compute the preimage of the domain or range (depending on "type")
12748 * of "map" under the function represented by "ma".
12749 * In other words, plug in "ma" in the domain or range of "map".
12750 * The result is a map that lives in the same space as "map"
12751 * except that the domain or range has been replaced by
12752 * the domain space of "ma".
12754 * The parameters are assumed to have been aligned.
12756 static __isl_give isl_map
*map_preimage_multi_aff(__isl_take isl_map
*map
,
12757 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
12762 map
= isl_map_cow(map
);
12763 ma
= isl_multi_aff_align_divs(ma
);
12766 if (check_map_compatible_range_multi_aff(map
, type
, ma
) < 0)
12769 for (i
= 0; i
< map
->n
; ++i
) {
12770 map
->p
[i
] = isl_basic_map_preimage_multi_aff(map
->p
[i
], type
,
12771 isl_multi_aff_copy(ma
));
12776 space
= isl_multi_aff_get_domain_space(ma
);
12777 space
= isl_space_set(isl_map_get_space(map
), type
, space
);
12779 isl_space_free(map
->dim
);
12784 isl_multi_aff_free(ma
);
12786 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
12787 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
12790 isl_multi_aff_free(ma
);
12795 /* Compute the preimage of the domain or range (depending on "type")
12796 * of "map" under the function represented by "ma".
12797 * In other words, plug in "ma" in the domain or range of "map".
12798 * The result is a map that lives in the same space as "map"
12799 * except that the domain or range has been replaced by
12800 * the domain space of "ma".
12802 __isl_give isl_map
*isl_map_preimage_multi_aff(__isl_take isl_map
*map
,
12803 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
12808 if (isl_space_match(map
->dim
, isl_dim_param
, ma
->space
, isl_dim_param
))
12809 return map_preimage_multi_aff(map
, type
, ma
);
12811 if (!isl_space_has_named_params(map
->dim
) ||
12812 !isl_space_has_named_params(ma
->space
))
12813 isl_die(map
->ctx
, isl_error_invalid
,
12814 "unaligned unnamed parameters", goto error
);
12815 map
= isl_map_align_params(map
, isl_multi_aff_get_space(ma
));
12816 ma
= isl_multi_aff_align_params(ma
, isl_map_get_space(map
));
12818 return map_preimage_multi_aff(map
, type
, ma
);
12820 isl_multi_aff_free(ma
);
12821 return isl_map_free(map
);
12824 /* Compute the preimage of "set" under the function represented by "ma".
12825 * In other words, plug in "ma" in "set". The result is a set
12826 * that lives in the domain space of "ma".
12828 __isl_give isl_set
*isl_set_preimage_multi_aff(__isl_take isl_set
*set
,
12829 __isl_take isl_multi_aff
*ma
)
12831 return isl_map_preimage_multi_aff(set
, isl_dim_set
, ma
);
12834 /* Compute the preimage of the domain of "map" under the function
12835 * represented by "ma".
12836 * In other words, plug in "ma" in the domain of "map".
12837 * The result is a map that lives in the same space as "map"
12838 * except that the domain has been replaced by the domain space of "ma".
12840 __isl_give isl_map
*isl_map_preimage_domain_multi_aff(__isl_take isl_map
*map
,
12841 __isl_take isl_multi_aff
*ma
)
12843 return isl_map_preimage_multi_aff(map
, isl_dim_in
, ma
);
12846 /* Compute the preimage of the range of "map" under the function
12847 * represented by "ma".
12848 * In other words, plug in "ma" in the range of "map".
12849 * The result is a map that lives in the same space as "map"
12850 * except that the range has been replaced by the domain space of "ma".
12852 __isl_give isl_map
*isl_map_preimage_range_multi_aff(__isl_take isl_map
*map
,
12853 __isl_take isl_multi_aff
*ma
)
12855 return isl_map_preimage_multi_aff(map
, isl_dim_out
, ma
);
12858 /* Compute the preimage of "map" under the function represented by "pma".
12859 * In other words, plug in "pma" in the domain or range of "map".
12860 * The result is a map that lives in the same space as "map",
12861 * except that the space of type "type" has been replaced by
12862 * the domain space of "pma".
12864 * The parameters of "map" and "pma" are assumed to have been aligned.
12866 static __isl_give isl_map
*isl_map_preimage_pw_multi_aff_aligned(
12867 __isl_take isl_map
*map
, enum isl_dim_type type
,
12868 __isl_take isl_pw_multi_aff
*pma
)
12877 isl_pw_multi_aff_free(pma
);
12878 res
= isl_map_empty(isl_map_get_space(map
));
12883 res
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
12884 isl_multi_aff_copy(pma
->p
[0].maff
));
12885 if (type
== isl_dim_in
)
12886 res
= isl_map_intersect_domain(res
,
12887 isl_map_copy(pma
->p
[0].set
));
12889 res
= isl_map_intersect_range(res
,
12890 isl_map_copy(pma
->p
[0].set
));
12892 for (i
= 1; i
< pma
->n
; ++i
) {
12895 res_i
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
12896 isl_multi_aff_copy(pma
->p
[i
].maff
));
12897 if (type
== isl_dim_in
)
12898 res_i
= isl_map_intersect_domain(res_i
,
12899 isl_map_copy(pma
->p
[i
].set
));
12901 res_i
= isl_map_intersect_range(res_i
,
12902 isl_map_copy(pma
->p
[i
].set
));
12903 res
= isl_map_union(res
, res_i
);
12906 isl_pw_multi_aff_free(pma
);
12910 isl_pw_multi_aff_free(pma
);
12915 /* Compute the preimage of "map" under the function represented by "pma".
12916 * In other words, plug in "pma" in the domain or range of "map".
12917 * The result is a map that lives in the same space as "map",
12918 * except that the space of type "type" has been replaced by
12919 * the domain space of "pma".
12921 __isl_give isl_map
*isl_map_preimage_pw_multi_aff(__isl_take isl_map
*map
,
12922 enum isl_dim_type type
, __isl_take isl_pw_multi_aff
*pma
)
12927 if (isl_space_match(map
->dim
, isl_dim_param
, pma
->dim
, isl_dim_param
))
12928 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
12930 if (!isl_space_has_named_params(map
->dim
) ||
12931 !isl_space_has_named_params(pma
->dim
))
12932 isl_die(map
->ctx
, isl_error_invalid
,
12933 "unaligned unnamed parameters", goto error
);
12934 map
= isl_map_align_params(map
, isl_pw_multi_aff_get_space(pma
));
12935 pma
= isl_pw_multi_aff_align_params(pma
, isl_map_get_space(map
));
12937 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
12939 isl_pw_multi_aff_free(pma
);
12940 return isl_map_free(map
);
12943 /* Compute the preimage of "set" under the function represented by "pma".
12944 * In other words, plug in "pma" in "set". The result is a set
12945 * that lives in the domain space of "pma".
12947 __isl_give isl_set
*isl_set_preimage_pw_multi_aff(__isl_take isl_set
*set
,
12948 __isl_take isl_pw_multi_aff
*pma
)
12950 return isl_map_preimage_pw_multi_aff(set
, isl_dim_set
, pma
);
12953 /* Compute the preimage of the domain of "map" under the function
12954 * represented by "pma".
12955 * In other words, plug in "pma" in the domain of "map".
12956 * The result is a map that lives in the same space as "map",
12957 * except that domain space has been replaced by the domain space of "pma".
12959 __isl_give isl_map
*isl_map_preimage_domain_pw_multi_aff(
12960 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
12962 return isl_map_preimage_pw_multi_aff(map
, isl_dim_in
, pma
);
12965 /* Compute the preimage of the range of "map" under the function
12966 * represented by "pma".
12967 * In other words, plug in "pma" in the range of "map".
12968 * The result is a map that lives in the same space as "map",
12969 * except that range space has been replaced by the domain space of "pma".
12971 __isl_give isl_map
*isl_map_preimage_range_pw_multi_aff(
12972 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
12974 return isl_map_preimage_pw_multi_aff(map
, isl_dim_out
, pma
);
12977 /* Compute the preimage of "map" under the function represented by "mpa".
12978 * In other words, plug in "mpa" in the domain or range of "map".
12979 * The result is a map that lives in the same space as "map",
12980 * except that the space of type "type" has been replaced by
12981 * the domain space of "mpa".
12983 * If the map does not involve any constraints that refer to the
12984 * dimensions of the substituted space, then the only possible
12985 * effect of "mpa" on the map is to map the space to a different space.
12986 * We create a separate isl_multi_aff to effectuate this change
12987 * in order to avoid spurious splitting of the map along the pieces
12990 __isl_give isl_map
*isl_map_preimage_multi_pw_aff(__isl_take isl_map
*map
,
12991 enum isl_dim_type type
, __isl_take isl_multi_pw_aff
*mpa
)
12994 isl_pw_multi_aff
*pma
;
12999 n
= isl_map_dim(map
, type
);
13000 if (!isl_map_involves_dims(map
, type
, 0, n
)) {
13004 space
= isl_multi_pw_aff_get_space(mpa
);
13005 isl_multi_pw_aff_free(mpa
);
13006 ma
= isl_multi_aff_zero(space
);
13007 return isl_map_preimage_multi_aff(map
, type
, ma
);
13010 pma
= isl_pw_multi_aff_from_multi_pw_aff(mpa
);
13011 return isl_map_preimage_pw_multi_aff(map
, type
, pma
);
13014 isl_multi_pw_aff_free(mpa
);
13018 /* Compute the preimage of "map" under the function represented by "mpa".
13019 * In other words, plug in "mpa" in the domain "map".
13020 * The result is a map that lives in the same space as "map",
13021 * except that domain space has been replaced by the domain space of "mpa".
13023 __isl_give isl_map
*isl_map_preimage_domain_multi_pw_aff(
13024 __isl_take isl_map
*map
, __isl_take isl_multi_pw_aff
*mpa
)
13026 return isl_map_preimage_multi_pw_aff(map
, isl_dim_in
, mpa
);
13029 /* Compute the preimage of "set" by the function represented by "mpa".
13030 * In other words, plug in "mpa" in "set".
13032 __isl_give isl_set
*isl_set_preimage_multi_pw_aff(__isl_take isl_set
*set
,
13033 __isl_take isl_multi_pw_aff
*mpa
)
13035 return isl_map_preimage_multi_pw_aff(set
, isl_dim_set
, mpa
);
13038 /* Are the "n" "coefficients" starting at "first" of the integer division
13039 * expressions at position "pos1" in "bmap1" and "pos2" in "bmap2" equal
13041 * The "coefficient" at position 0 is the denominator.
13042 * The "coefficient" at position 1 is the constant term.
13044 isl_bool
isl_basic_map_equal_div_expr_part(__isl_keep isl_basic_map
*bmap1
,
13045 int pos1
, __isl_keep isl_basic_map
*bmap2
, int pos2
,
13046 unsigned first
, unsigned n
)
13048 if (isl_basic_map_check_range(bmap1
, isl_dim_div
, pos1
, 1) < 0)
13049 return isl_bool_error
;
13050 if (isl_basic_map_check_range(bmap2
, isl_dim_div
, pos2
, 1) < 0)
13051 return isl_bool_error
;
13052 return isl_seq_eq(bmap1
->div
[pos1
] + first
,
13053 bmap2
->div
[pos2
] + first
, n
);
13056 /* Are the integer division expressions at position "pos1" in "bmap1" and
13057 * "pos2" in "bmap2" equal to each other, except that the constant terms
13060 isl_bool
isl_basic_map_equal_div_expr_except_constant(
13061 __isl_keep isl_basic_map
*bmap1
, int pos1
,
13062 __isl_keep isl_basic_map
*bmap2
, int pos2
)
13067 if (!bmap1
|| !bmap2
)
13068 return isl_bool_error
;
13069 total
= isl_basic_map_total_dim(bmap1
);
13070 if (total
!= isl_basic_map_total_dim(bmap2
))
13071 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
13072 "incomparable div expressions", return isl_bool_error
);
13073 equal
= isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
13075 if (equal
< 0 || !equal
)
13077 equal
= isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
13079 if (equal
< 0 || equal
)
13080 return isl_bool_not(equal
);
13081 return isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
13085 /* Replace the numerator of the constant term of the integer division
13086 * expression at position "div" in "bmap" by "value".
13087 * The caller guarantees that this does not change the meaning
13090 __isl_give isl_basic_map
*isl_basic_map_set_div_expr_constant_num_si_inplace(
13091 __isl_take isl_basic_map
*bmap
, int div
, int value
)
13093 if (isl_basic_map_check_range(bmap
, isl_dim_div
, div
, 1) < 0)
13094 return isl_basic_map_free(bmap
);
13096 isl_int_set_si(bmap
->div
[div
][1], value
);
13101 /* Is the point "inner" internal to inequality constraint "ineq"
13103 * The point is considered to be internal to the inequality constraint,
13104 * if it strictly lies on the positive side of the inequality constraint,
13105 * or if it lies on the constraint and the constraint is lexico-positive.
13107 static isl_bool
is_internal(__isl_keep isl_vec
*inner
,
13108 __isl_keep isl_basic_set
*bset
, int ineq
)
13114 if (!inner
|| !bset
)
13115 return isl_bool_error
;
13117 ctx
= isl_basic_set_get_ctx(bset
);
13118 isl_seq_inner_product(inner
->el
, bset
->ineq
[ineq
], inner
->size
,
13119 &ctx
->normalize_gcd
);
13120 if (!isl_int_is_zero(ctx
->normalize_gcd
))
13121 return isl_int_is_nonneg(ctx
->normalize_gcd
);
13123 total
= isl_basic_set_dim(bset
, isl_dim_all
);
13124 pos
= isl_seq_first_non_zero(bset
->ineq
[ineq
] + 1, total
);
13125 return isl_int_is_pos(bset
->ineq
[ineq
][1 + pos
]);
13128 /* Tighten the inequality constraints of "bset" that are outward with respect
13129 * to the point "vec".
13130 * That is, tighten the constraints that are not satisfied by "vec".
13132 * "vec" is a point internal to some superset S of "bset" that is used
13133 * to make the subsets of S disjoint, by tightening one half of the constraints
13134 * that separate two subsets. In particular, the constraints of S
13135 * are all satisfied by "vec" and should not be tightened.
13136 * Of the internal constraints, those that have "vec" on the outside
13137 * are tightened. The shared facet is included in the adjacent subset
13138 * with the opposite constraint.
13139 * For constraints that saturate "vec", this criterion cannot be used
13140 * to determine which of the two sides should be tightened.
13141 * Instead, the sign of the first non-zero coefficient is used
13142 * to make this choice. Note that this second criterion is never used
13143 * on the constraints of S since "vec" is interior to "S".
13145 __isl_give isl_basic_set
*isl_basic_set_tighten_outward(
13146 __isl_take isl_basic_set
*bset
, __isl_keep isl_vec
*vec
)
13150 bset
= isl_basic_set_cow(bset
);
13153 for (j
= 0; j
< bset
->n_ineq
; ++j
) {
13156 internal
= is_internal(vec
, bset
, j
);
13158 return isl_basic_set_free(bset
);
13161 isl_int_sub_ui(bset
->ineq
[j
][0], bset
->ineq
[j
][0], 1);