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 int isl_basic_map_is_rational(__isl_keep isl_basic_map
*bmap
)
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 int isl_basic_map_has_rational(__isl_keep isl_basic_map
*bmap
)
888 int has_rational
= 1;
893 if (isl_basic_map_plain_is_empty(bmap
))
895 if (!isl_basic_map_is_rational(bmap
))
897 bmap
= isl_basic_map_copy(bmap
);
898 bmap
= isl_basic_map_implicit_equalities(bmap
);
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,
919 isl_basic_map_free(bmap
);
924 /* Does "map" contain any rational points?
926 int isl_map_has_rational(__isl_keep isl_map
*map
)
933 for (i
= 0; i
< map
->n
; ++i
) {
934 has_rational
= isl_basic_map_has_rational(map
->p
[i
]);
935 if (has_rational
< 0)
943 /* Does "set" contain any rational points?
945 int isl_set_has_rational(__isl_keep isl_set
*set
)
947 return isl_map_has_rational(set
);
950 /* Is this basic set a parameter domain?
952 isl_bool
isl_basic_set_is_params(__isl_keep isl_basic_set
*bset
)
955 return isl_bool_error
;
956 return isl_space_is_params(bset
->dim
);
959 /* Is this set a parameter domain?
961 isl_bool
isl_set_is_params(__isl_keep isl_set
*set
)
964 return isl_bool_error
;
965 return isl_space_is_params(set
->dim
);
968 /* Is this map actually a parameter domain?
969 * Users should never call this function. Outside of isl,
970 * a map can never be a parameter domain.
972 isl_bool
isl_map_is_params(__isl_keep isl_map
*map
)
975 return isl_bool_error
;
976 return isl_space_is_params(map
->dim
);
979 static struct isl_basic_map
*basic_map_init(struct isl_ctx
*ctx
,
980 struct isl_basic_map
*bmap
, unsigned extra
,
981 unsigned n_eq
, unsigned n_ineq
)
984 size_t row_size
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + extra
;
989 bmap
->block
= isl_blk_alloc(ctx
, (n_ineq
+ n_eq
) * row_size
);
990 if (isl_blk_is_error(bmap
->block
))
993 bmap
->ineq
= isl_alloc_array(ctx
, isl_int
*, n_ineq
+ n_eq
);
994 if ((n_ineq
+ n_eq
) && !bmap
->ineq
)
998 bmap
->block2
= isl_blk_empty();
1001 bmap
->block2
= isl_blk_alloc(ctx
, extra
* (1 + row_size
));
1002 if (isl_blk_is_error(bmap
->block2
))
1005 bmap
->div
= isl_alloc_array(ctx
, isl_int
*, extra
);
1010 for (i
= 0; i
< n_ineq
+ n_eq
; ++i
)
1011 bmap
->ineq
[i
] = bmap
->block
.data
+ i
* row_size
;
1013 for (i
= 0; i
< extra
; ++i
)
1014 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + row_size
);
1018 bmap
->c_size
= n_eq
+ n_ineq
;
1019 bmap
->eq
= bmap
->ineq
+ n_ineq
;
1020 bmap
->extra
= extra
;
1024 bmap
->sample
= NULL
;
1028 isl_basic_map_free(bmap
);
1032 struct isl_basic_set
*isl_basic_set_alloc(struct isl_ctx
*ctx
,
1033 unsigned nparam
, unsigned dim
, unsigned extra
,
1034 unsigned n_eq
, unsigned n_ineq
)
1036 struct isl_basic_map
*bmap
;
1039 space
= isl_space_set_alloc(ctx
, nparam
, dim
);
1043 bmap
= isl_basic_map_alloc_space(space
, extra
, n_eq
, n_ineq
);
1044 return bset_from_bmap(bmap
);
1047 struct isl_basic_set
*isl_basic_set_alloc_space(__isl_take isl_space
*dim
,
1048 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
1050 struct isl_basic_map
*bmap
;
1053 isl_assert(dim
->ctx
, dim
->n_in
== 0, goto error
);
1054 bmap
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
1055 return bset_from_bmap(bmap
);
1057 isl_space_free(dim
);
1061 struct isl_basic_map
*isl_basic_map_alloc_space(__isl_take isl_space
*dim
,
1062 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
1064 struct isl_basic_map
*bmap
;
1068 bmap
= isl_calloc_type(dim
->ctx
, struct isl_basic_map
);
1073 return basic_map_init(dim
->ctx
, bmap
, extra
, n_eq
, n_ineq
);
1075 isl_space_free(dim
);
1079 struct isl_basic_map
*isl_basic_map_alloc(struct isl_ctx
*ctx
,
1080 unsigned nparam
, unsigned in
, unsigned out
, unsigned extra
,
1081 unsigned n_eq
, unsigned n_ineq
)
1083 struct isl_basic_map
*bmap
;
1086 dim
= isl_space_alloc(ctx
, nparam
, in
, out
);
1090 bmap
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
1094 static void dup_constraints(
1095 struct isl_basic_map
*dst
, struct isl_basic_map
*src
)
1098 unsigned total
= isl_basic_map_total_dim(src
);
1100 for (i
= 0; i
< src
->n_eq
; ++i
) {
1101 int j
= isl_basic_map_alloc_equality(dst
);
1102 isl_seq_cpy(dst
->eq
[j
], src
->eq
[i
], 1+total
);
1105 for (i
= 0; i
< src
->n_ineq
; ++i
) {
1106 int j
= isl_basic_map_alloc_inequality(dst
);
1107 isl_seq_cpy(dst
->ineq
[j
], src
->ineq
[i
], 1+total
);
1110 for (i
= 0; i
< src
->n_div
; ++i
) {
1111 int j
= isl_basic_map_alloc_div(dst
);
1112 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
);
1114 ISL_F_SET(dst
, ISL_BASIC_SET_FINAL
);
1117 struct isl_basic_map
*isl_basic_map_dup(struct isl_basic_map
*bmap
)
1119 struct isl_basic_map
*dup
;
1123 dup
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
1124 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
1127 dup_constraints(dup
, bmap
);
1128 dup
->flags
= bmap
->flags
;
1129 dup
->sample
= isl_vec_copy(bmap
->sample
);
1133 struct isl_basic_set
*isl_basic_set_dup(struct isl_basic_set
*bset
)
1135 struct isl_basic_map
*dup
;
1137 dup
= isl_basic_map_dup(bset_to_bmap(bset
));
1138 return bset_from_bmap(dup
);
1141 struct isl_basic_set
*isl_basic_set_copy(struct isl_basic_set
*bset
)
1146 if (ISL_F_ISSET(bset
, ISL_BASIC_SET_FINAL
)) {
1150 return isl_basic_set_dup(bset
);
1153 struct isl_set
*isl_set_copy(struct isl_set
*set
)
1162 struct isl_basic_map
*isl_basic_map_copy(struct isl_basic_map
*bmap
)
1167 if (ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
)) {
1171 bmap
= isl_basic_map_dup(bmap
);
1173 ISL_F_SET(bmap
, ISL_BASIC_SET_FINAL
);
1177 struct isl_map
*isl_map_copy(struct isl_map
*map
)
1186 __isl_null isl_basic_map
*isl_basic_map_free(__isl_take isl_basic_map
*bmap
)
1191 if (--bmap
->ref
> 0)
1194 isl_ctx_deref(bmap
->ctx
);
1196 isl_blk_free(bmap
->ctx
, bmap
->block2
);
1198 isl_blk_free(bmap
->ctx
, bmap
->block
);
1199 isl_vec_free(bmap
->sample
);
1200 isl_space_free(bmap
->dim
);
1206 __isl_null isl_basic_set
*isl_basic_set_free(__isl_take isl_basic_set
*bset
)
1208 return isl_basic_map_free(bset_to_bmap(bset
));
1211 static int room_for_con(struct isl_basic_map
*bmap
, unsigned n
)
1213 return bmap
->n_eq
+ bmap
->n_ineq
+ n
<= bmap
->c_size
;
1216 __isl_give isl_map
*isl_map_align_params_map_map_and(
1217 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
,
1218 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*map1
,
1219 __isl_take isl_map
*map2
))
1223 if (isl_space_match(map1
->dim
, isl_dim_param
, map2
->dim
, isl_dim_param
))
1224 return fn(map1
, map2
);
1225 if (!isl_space_has_named_params(map1
->dim
) ||
1226 !isl_space_has_named_params(map2
->dim
))
1227 isl_die(map1
->ctx
, isl_error_invalid
,
1228 "unaligned unnamed parameters", goto error
);
1229 map1
= isl_map_align_params(map1
, isl_map_get_space(map2
));
1230 map2
= isl_map_align_params(map2
, isl_map_get_space(map1
));
1231 return fn(map1
, map2
);
1238 isl_bool
isl_map_align_params_map_map_and_test(__isl_keep isl_map
*map1
,
1239 __isl_keep isl_map
*map2
,
1240 isl_bool (*fn
)(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
))
1245 return isl_bool_error
;
1246 if (isl_space_match(map1
->dim
, isl_dim_param
, map2
->dim
, isl_dim_param
))
1247 return fn(map1
, map2
);
1248 if (!isl_space_has_named_params(map1
->dim
) ||
1249 !isl_space_has_named_params(map2
->dim
))
1250 isl_die(map1
->ctx
, isl_error_invalid
,
1251 "unaligned unnamed parameters", return isl_bool_error
);
1252 map1
= isl_map_copy(map1
);
1253 map2
= isl_map_copy(map2
);
1254 map1
= isl_map_align_params(map1
, isl_map_get_space(map2
));
1255 map2
= isl_map_align_params(map2
, isl_map_get_space(map1
));
1262 int isl_basic_map_alloc_equality(struct isl_basic_map
*bmap
)
1264 struct isl_ctx
*ctx
;
1268 isl_assert(ctx
, room_for_con(bmap
, 1), return -1);
1269 isl_assert(ctx
, (bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
<= bmap
->c_size
,
1271 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1272 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1273 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1274 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1275 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1276 if ((bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
== bmap
->c_size
) {
1278 int j
= isl_basic_map_alloc_inequality(bmap
);
1282 bmap
->ineq
[j
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1283 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
1290 isl_seq_clr(bmap
->eq
[bmap
->n_eq
] + 1 + isl_basic_map_total_dim(bmap
),
1291 bmap
->extra
- bmap
->n_div
);
1292 return bmap
->n_eq
++;
1295 int isl_basic_set_alloc_equality(struct isl_basic_set
*bset
)
1297 return isl_basic_map_alloc_equality(bset_to_bmap(bset
));
1300 int isl_basic_map_free_equality(struct isl_basic_map
*bmap
, unsigned n
)
1304 isl_assert(bmap
->ctx
, n
<= bmap
->n_eq
, return -1);
1309 int isl_basic_set_free_equality(struct isl_basic_set
*bset
, unsigned n
)
1311 return isl_basic_map_free_equality(bset_to_bmap(bset
), n
);
1314 int isl_basic_map_drop_equality(struct isl_basic_map
*bmap
, unsigned pos
)
1319 isl_assert(bmap
->ctx
, pos
< bmap
->n_eq
, return -1);
1321 if (pos
!= bmap
->n_eq
- 1) {
1323 bmap
->eq
[pos
] = bmap
->eq
[bmap
->n_eq
- 1];
1324 bmap
->eq
[bmap
->n_eq
- 1] = t
;
1330 int isl_basic_set_drop_equality(struct isl_basic_set
*bset
, unsigned pos
)
1332 return isl_basic_map_drop_equality(bset_to_bmap(bset
), pos
);
1335 /* Turn inequality "pos" of "bmap" into an equality.
1337 * In particular, we move the inequality in front of the equalities
1338 * and move the last inequality in the position of the moved inequality.
1339 * Note that isl_tab_make_equalities_explicit depends on this particular
1340 * change in the ordering of the constraints.
1342 void isl_basic_map_inequality_to_equality(
1343 struct isl_basic_map
*bmap
, unsigned pos
)
1347 t
= bmap
->ineq
[pos
];
1348 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1349 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
1354 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1355 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1356 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1357 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1360 static int room_for_ineq(struct isl_basic_map
*bmap
, unsigned n
)
1362 return bmap
->n_ineq
+ n
<= bmap
->eq
- bmap
->ineq
;
1365 int isl_basic_map_alloc_inequality(struct isl_basic_map
*bmap
)
1367 struct isl_ctx
*ctx
;
1371 isl_assert(ctx
, room_for_ineq(bmap
, 1), return -1);
1372 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1373 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1374 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1375 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1376 isl_seq_clr(bmap
->ineq
[bmap
->n_ineq
] +
1377 1 + isl_basic_map_total_dim(bmap
),
1378 bmap
->extra
- bmap
->n_div
);
1379 return bmap
->n_ineq
++;
1382 int isl_basic_set_alloc_inequality(struct isl_basic_set
*bset
)
1384 return isl_basic_map_alloc_inequality(bset_to_bmap(bset
));
1387 int isl_basic_map_free_inequality(struct isl_basic_map
*bmap
, unsigned n
)
1391 isl_assert(bmap
->ctx
, n
<= bmap
->n_ineq
, return -1);
1396 int isl_basic_set_free_inequality(struct isl_basic_set
*bset
, unsigned n
)
1398 return isl_basic_map_free_inequality(bset_to_bmap(bset
), n
);
1401 int isl_basic_map_drop_inequality(struct isl_basic_map
*bmap
, unsigned pos
)
1406 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
1408 if (pos
!= bmap
->n_ineq
- 1) {
1409 t
= bmap
->ineq
[pos
];
1410 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1411 bmap
->ineq
[bmap
->n_ineq
- 1] = t
;
1412 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1418 int isl_basic_set_drop_inequality(struct isl_basic_set
*bset
, unsigned pos
)
1420 return isl_basic_map_drop_inequality(bset_to_bmap(bset
), pos
);
1423 __isl_give isl_basic_map
*isl_basic_map_add_eq(__isl_take isl_basic_map
*bmap
,
1428 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
1431 k
= isl_basic_map_alloc_equality(bmap
);
1434 isl_seq_cpy(bmap
->eq
[k
], eq
, 1 + isl_basic_map_total_dim(bmap
));
1437 isl_basic_map_free(bmap
);
1441 __isl_give isl_basic_set
*isl_basic_set_add_eq(__isl_take isl_basic_set
*bset
,
1444 return bset_from_bmap(isl_basic_map_add_eq(bset_to_bmap(bset
), eq
));
1447 __isl_give isl_basic_map
*isl_basic_map_add_ineq(__isl_take isl_basic_map
*bmap
,
1452 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
1455 k
= isl_basic_map_alloc_inequality(bmap
);
1458 isl_seq_cpy(bmap
->ineq
[k
], ineq
, 1 + isl_basic_map_total_dim(bmap
));
1461 isl_basic_map_free(bmap
);
1465 __isl_give isl_basic_set
*isl_basic_set_add_ineq(__isl_take isl_basic_set
*bset
,
1468 return bset_from_bmap(isl_basic_map_add_ineq(bset_to_bmap(bset
), ineq
));
1471 int isl_basic_map_alloc_div(struct isl_basic_map
*bmap
)
1475 isl_assert(bmap
->ctx
, bmap
->n_div
< bmap
->extra
, return -1);
1476 isl_seq_clr(bmap
->div
[bmap
->n_div
] +
1477 1 + 1 + isl_basic_map_total_dim(bmap
),
1478 bmap
->extra
- bmap
->n_div
);
1479 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1480 return bmap
->n_div
++;
1483 int isl_basic_set_alloc_div(struct isl_basic_set
*bset
)
1485 return isl_basic_map_alloc_div(bset_to_bmap(bset
));
1488 /* Check that there are "n" dimensions of type "type" starting at "first"
1491 static isl_stat
isl_basic_map_check_range(__isl_keep isl_basic_map
*bmap
,
1492 enum isl_dim_type type
, unsigned first
, unsigned n
)
1497 return isl_stat_error
;
1498 dim
= isl_basic_map_dim(bmap
, type
);
1499 if (first
+ n
> dim
|| first
+ n
< first
)
1500 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
1501 "position or range out of bounds",
1502 return isl_stat_error
);
1506 /* Insert an extra integer division, prescribed by "div", to "bmap"
1507 * at (integer division) position "pos".
1509 * The integer division is first added at the end and then moved
1510 * into the right position.
1512 __isl_give isl_basic_map
*isl_basic_map_insert_div(
1513 __isl_take isl_basic_map
*bmap
, int pos
, __isl_keep isl_vec
*div
)
1517 bmap
= isl_basic_map_cow(bmap
);
1519 return isl_basic_map_free(bmap
);
1521 if (div
->size
!= 1 + 1 + isl_basic_map_dim(bmap
, isl_dim_all
))
1522 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
1523 "unexpected size", return isl_basic_map_free(bmap
));
1524 if (isl_basic_map_check_range(bmap
, isl_dim_div
, pos
, 0) < 0)
1525 return isl_basic_map_free(bmap
);
1527 bmap
= isl_basic_map_extend_space(bmap
,
1528 isl_basic_map_get_space(bmap
), 1, 0, 2);
1529 k
= isl_basic_map_alloc_div(bmap
);
1531 return isl_basic_map_free(bmap
);
1532 isl_seq_cpy(bmap
->div
[k
], div
->el
, div
->size
);
1533 isl_int_set_si(bmap
->div
[k
][div
->size
], 0);
1535 for (i
= k
; i
> pos
; --i
)
1536 isl_basic_map_swap_div(bmap
, i
, i
- 1);
1541 int isl_basic_map_free_div(struct isl_basic_map
*bmap
, unsigned n
)
1545 isl_assert(bmap
->ctx
, n
<= bmap
->n_div
, return -1);
1550 int isl_basic_set_free_div(struct isl_basic_set
*bset
, unsigned n
)
1552 return isl_basic_map_free_div(bset_to_bmap(bset
), n
);
1555 /* Copy constraint from src to dst, putting the vars of src at offset
1556 * dim_off in dst and the divs of src at offset div_off in dst.
1557 * If both sets are actually map, then dim_off applies to the input
1560 static void copy_constraint(struct isl_basic_map
*dst_map
, isl_int
*dst
,
1561 struct isl_basic_map
*src_map
, isl_int
*src
,
1562 unsigned in_off
, unsigned out_off
, unsigned div_off
)
1564 unsigned src_nparam
= isl_basic_map_n_param(src_map
);
1565 unsigned dst_nparam
= isl_basic_map_n_param(dst_map
);
1566 unsigned src_in
= isl_basic_map_n_in(src_map
);
1567 unsigned dst_in
= isl_basic_map_n_in(dst_map
);
1568 unsigned src_out
= isl_basic_map_n_out(src_map
);
1569 unsigned dst_out
= isl_basic_map_n_out(dst_map
);
1570 isl_int_set(dst
[0], src
[0]);
1571 isl_seq_cpy(dst
+1, src
+1, isl_min(dst_nparam
, src_nparam
));
1572 if (dst_nparam
> src_nparam
)
1573 isl_seq_clr(dst
+1+src_nparam
,
1574 dst_nparam
- src_nparam
);
1575 isl_seq_clr(dst
+1+dst_nparam
, in_off
);
1576 isl_seq_cpy(dst
+1+dst_nparam
+in_off
,
1578 isl_min(dst_in
-in_off
, src_in
));
1579 if (dst_in
-in_off
> src_in
)
1580 isl_seq_clr(dst
+1+dst_nparam
+in_off
+src_in
,
1581 dst_in
- in_off
- src_in
);
1582 isl_seq_clr(dst
+1+dst_nparam
+dst_in
, out_off
);
1583 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+out_off
,
1584 src
+1+src_nparam
+src_in
,
1585 isl_min(dst_out
-out_off
, src_out
));
1586 if (dst_out
-out_off
> src_out
)
1587 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+out_off
+src_out
,
1588 dst_out
- out_off
- src_out
);
1589 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
, div_off
);
1590 isl_seq_cpy(dst
+1+dst_nparam
+dst_in
+dst_out
+div_off
,
1591 src
+1+src_nparam
+src_in
+src_out
,
1592 isl_min(dst_map
->extra
-div_off
, src_map
->n_div
));
1593 if (dst_map
->n_div
-div_off
> src_map
->n_div
)
1594 isl_seq_clr(dst
+1+dst_nparam
+dst_in
+dst_out
+
1595 div_off
+src_map
->n_div
,
1596 dst_map
->n_div
- div_off
- src_map
->n_div
);
1599 static void copy_div(struct isl_basic_map
*dst_map
, isl_int
*dst
,
1600 struct isl_basic_map
*src_map
, isl_int
*src
,
1601 unsigned in_off
, unsigned out_off
, unsigned div_off
)
1603 isl_int_set(dst
[0], src
[0]);
1604 copy_constraint(dst_map
, dst
+1, src_map
, src
+1, in_off
, out_off
, div_off
);
1607 static struct isl_basic_map
*add_constraints(struct isl_basic_map
*bmap1
,
1608 struct isl_basic_map
*bmap2
, unsigned i_pos
, unsigned o_pos
)
1613 if (!bmap1
|| !bmap2
)
1616 div_off
= bmap1
->n_div
;
1618 for (i
= 0; i
< bmap2
->n_eq
; ++i
) {
1619 int i1
= isl_basic_map_alloc_equality(bmap1
);
1622 copy_constraint(bmap1
, bmap1
->eq
[i1
], bmap2
, bmap2
->eq
[i
],
1623 i_pos
, o_pos
, div_off
);
1626 for (i
= 0; i
< bmap2
->n_ineq
; ++i
) {
1627 int i1
= isl_basic_map_alloc_inequality(bmap1
);
1630 copy_constraint(bmap1
, bmap1
->ineq
[i1
], bmap2
, bmap2
->ineq
[i
],
1631 i_pos
, o_pos
, div_off
);
1634 for (i
= 0; i
< bmap2
->n_div
; ++i
) {
1635 int i1
= isl_basic_map_alloc_div(bmap1
);
1638 copy_div(bmap1
, bmap1
->div
[i1
], bmap2
, bmap2
->div
[i
],
1639 i_pos
, o_pos
, div_off
);
1642 isl_basic_map_free(bmap2
);
1647 isl_basic_map_free(bmap1
);
1648 isl_basic_map_free(bmap2
);
1652 struct isl_basic_set
*isl_basic_set_add_constraints(struct isl_basic_set
*bset1
,
1653 struct isl_basic_set
*bset2
, unsigned pos
)
1655 return bset_from_bmap(add_constraints(bset_to_bmap(bset1
),
1656 bset_to_bmap(bset2
), 0, pos
));
1659 struct isl_basic_map
*isl_basic_map_extend_space(struct isl_basic_map
*base
,
1660 __isl_take isl_space
*dim
, unsigned extra
,
1661 unsigned n_eq
, unsigned n_ineq
)
1663 struct isl_basic_map
*ext
;
1673 dims_ok
= isl_space_is_equal(base
->dim
, dim
) &&
1674 base
->extra
>= base
->n_div
+ extra
;
1676 if (dims_ok
&& room_for_con(base
, n_eq
+ n_ineq
) &&
1677 room_for_ineq(base
, n_ineq
)) {
1678 isl_space_free(dim
);
1682 isl_assert(base
->ctx
, base
->dim
->nparam
<= dim
->nparam
, goto error
);
1683 isl_assert(base
->ctx
, base
->dim
->n_in
<= dim
->n_in
, goto error
);
1684 isl_assert(base
->ctx
, base
->dim
->n_out
<= dim
->n_out
, goto error
);
1685 extra
+= base
->extra
;
1687 n_ineq
+= base
->n_ineq
;
1689 ext
= isl_basic_map_alloc_space(dim
, extra
, n_eq
, n_ineq
);
1695 ext
->sample
= isl_vec_copy(base
->sample
);
1696 flags
= base
->flags
;
1697 ext
= add_constraints(ext
, base
, 0, 0);
1700 ISL_F_CLR(ext
, ISL_BASIC_SET_FINAL
);
1706 isl_space_free(dim
);
1707 isl_basic_map_free(base
);
1711 struct isl_basic_set
*isl_basic_set_extend_space(struct isl_basic_set
*base
,
1712 __isl_take isl_space
*dim
, unsigned extra
,
1713 unsigned n_eq
, unsigned n_ineq
)
1715 return bset_from_bmap(isl_basic_map_extend_space(bset_to_bmap(base
),
1716 dim
, extra
, n_eq
, n_ineq
));
1719 struct isl_basic_map
*isl_basic_map_extend_constraints(
1720 struct isl_basic_map
*base
, unsigned n_eq
, unsigned n_ineq
)
1724 return isl_basic_map_extend_space(base
, isl_space_copy(base
->dim
),
1728 struct isl_basic_map
*isl_basic_map_extend(struct isl_basic_map
*base
,
1729 unsigned nparam
, unsigned n_in
, unsigned n_out
, unsigned extra
,
1730 unsigned n_eq
, unsigned n_ineq
)
1732 struct isl_basic_map
*bmap
;
1737 dim
= isl_space_alloc(base
->ctx
, nparam
, n_in
, n_out
);
1741 bmap
= isl_basic_map_extend_space(base
, dim
, extra
, n_eq
, n_ineq
);
1744 isl_basic_map_free(base
);
1748 struct isl_basic_set
*isl_basic_set_extend(struct isl_basic_set
*base
,
1749 unsigned nparam
, unsigned dim
, unsigned extra
,
1750 unsigned n_eq
, unsigned n_ineq
)
1752 return bset_from_bmap(isl_basic_map_extend(bset_to_bmap(base
),
1753 nparam
, 0, dim
, extra
, n_eq
, n_ineq
));
1756 struct isl_basic_set
*isl_basic_set_extend_constraints(
1757 struct isl_basic_set
*base
, unsigned n_eq
, unsigned n_ineq
)
1759 isl_basic_map
*bmap
= bset_to_bmap(base
);
1760 bmap
= isl_basic_map_extend_constraints(bmap
, n_eq
, n_ineq
);
1761 return bset_from_bmap(bmap
);
1764 struct isl_basic_set
*isl_basic_set_cow(struct isl_basic_set
*bset
)
1766 return bset_from_bmap(isl_basic_map_cow(bset_to_bmap(bset
)));
1769 struct isl_basic_map
*isl_basic_map_cow(struct isl_basic_map
*bmap
)
1774 if (bmap
->ref
> 1) {
1776 bmap
= isl_basic_map_dup(bmap
);
1779 ISL_F_CLR(bmap
, ISL_BASIC_SET_FINAL
);
1780 ISL_F_CLR(bmap
, ISL_BASIC_MAP_REDUCED_COEFFICIENTS
);
1785 /* Clear all cached information in "map", either because it is about
1786 * to be modified or because it is being freed.
1787 * Always return the same pointer that is passed in.
1788 * This is needed for the use in isl_map_free.
1790 static __isl_give isl_map
*clear_caches(__isl_take isl_map
*map
)
1792 isl_basic_map_free(map
->cached_simple_hull
[0]);
1793 isl_basic_map_free(map
->cached_simple_hull
[1]);
1794 map
->cached_simple_hull
[0] = NULL
;
1795 map
->cached_simple_hull
[1] = NULL
;
1799 struct isl_set
*isl_set_cow(struct isl_set
*set
)
1801 return isl_map_cow(set
);
1804 /* Return an isl_map that is equal to "map" and that has only
1805 * a single reference.
1807 * If the original input already has only one reference, then
1808 * simply return it, but clear all cached information, since
1809 * it may be rendered invalid by the operations that will be
1810 * performed on the result.
1812 * Otherwise, create a duplicate (without any cached information).
1814 struct isl_map
*isl_map_cow(struct isl_map
*map
)
1820 return clear_caches(map
);
1822 return isl_map_dup(map
);
1825 static void swap_vars(struct isl_blk blk
, isl_int
*a
,
1826 unsigned a_len
, unsigned b_len
)
1828 isl_seq_cpy(blk
.data
, a
+a_len
, b_len
);
1829 isl_seq_cpy(blk
.data
+b_len
, a
, a_len
);
1830 isl_seq_cpy(a
, blk
.data
, b_len
+a_len
);
1833 static __isl_give isl_basic_map
*isl_basic_map_swap_vars(
1834 __isl_take isl_basic_map
*bmap
, unsigned pos
, unsigned n1
, unsigned n2
)
1842 isl_assert(bmap
->ctx
,
1843 pos
+ n1
+ n2
<= 1 + isl_basic_map_total_dim(bmap
), goto error
);
1845 if (n1
== 0 || n2
== 0)
1848 bmap
= isl_basic_map_cow(bmap
);
1852 blk
= isl_blk_alloc(bmap
->ctx
, n1
+ n2
);
1853 if (isl_blk_is_error(blk
))
1856 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1858 bmap
->eq
[i
] + pos
, n1
, n2
);
1860 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1862 bmap
->ineq
[i
] + pos
, n1
, n2
);
1864 for (i
= 0; i
< bmap
->n_div
; ++i
)
1866 bmap
->div
[i
]+1 + pos
, n1
, n2
);
1868 isl_blk_free(bmap
->ctx
, blk
);
1870 ISL_F_CLR(bmap
, ISL_BASIC_SET_NORMALIZED
);
1871 bmap
= isl_basic_map_gauss(bmap
, NULL
);
1872 return isl_basic_map_finalize(bmap
);
1874 isl_basic_map_free(bmap
);
1878 struct isl_basic_map
*isl_basic_map_set_to_empty(struct isl_basic_map
*bmap
)
1884 total
= isl_basic_map_total_dim(bmap
);
1885 if (isl_basic_map_free_div(bmap
, bmap
->n_div
) < 0)
1886 return isl_basic_map_free(bmap
);
1887 isl_basic_map_free_inequality(bmap
, bmap
->n_ineq
);
1889 isl_basic_map_free_equality(bmap
, bmap
->n_eq
-1);
1891 i
= isl_basic_map_alloc_equality(bmap
);
1895 isl_int_set_si(bmap
->eq
[i
][0], 1);
1896 isl_seq_clr(bmap
->eq
[i
]+1, total
);
1897 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
1898 isl_vec_free(bmap
->sample
);
1899 bmap
->sample
= NULL
;
1900 return isl_basic_map_finalize(bmap
);
1902 isl_basic_map_free(bmap
);
1906 struct isl_basic_set
*isl_basic_set_set_to_empty(struct isl_basic_set
*bset
)
1908 return bset_from_bmap(isl_basic_map_set_to_empty(bset_to_bmap(bset
)));
1911 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
1914 static void swap_div(__isl_keep isl_basic_map
*bmap
, int a
, int b
)
1916 isl_int
*t
= bmap
->div
[a
];
1917 bmap
->div
[a
] = bmap
->div
[b
];
1921 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
1922 * div definitions accordingly.
1924 void isl_basic_map_swap_div(struct isl_basic_map
*bmap
, int a
, int b
)
1927 unsigned off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
1929 swap_div(bmap
, a
, b
);
1931 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1932 isl_int_swap(bmap
->eq
[i
][1+off
+a
], bmap
->eq
[i
][1+off
+b
]);
1934 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
1935 isl_int_swap(bmap
->ineq
[i
][1+off
+a
], bmap
->ineq
[i
][1+off
+b
]);
1937 for (i
= 0; i
< bmap
->n_div
; ++i
)
1938 isl_int_swap(bmap
->div
[i
][1+1+off
+a
], bmap
->div
[i
][1+1+off
+b
]);
1939 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1942 /* Swap divs "a" and "b" in "bset" and adjust the constraints and
1943 * div definitions accordingly.
1945 void isl_basic_set_swap_div(__isl_keep isl_basic_set
*bset
, int a
, int b
)
1947 isl_basic_map_swap_div(bset
, a
, b
);
1950 /* Eliminate the specified n dimensions starting at first from the
1951 * constraints, without removing the dimensions from the space.
1952 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1954 __isl_give isl_map
*isl_map_eliminate(__isl_take isl_map
*map
,
1955 enum isl_dim_type type
, unsigned first
, unsigned n
)
1964 if (first
+ n
> isl_map_dim(map
, type
) || first
+ n
< first
)
1965 isl_die(map
->ctx
, isl_error_invalid
,
1966 "index out of bounds", goto error
);
1968 map
= isl_map_cow(map
);
1972 for (i
= 0; i
< map
->n
; ++i
) {
1973 map
->p
[i
] = isl_basic_map_eliminate(map
->p
[i
], type
, first
, n
);
1983 /* Eliminate the specified n dimensions starting at first from the
1984 * constraints, without removing the dimensions from the space.
1985 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1987 __isl_give isl_set
*isl_set_eliminate(__isl_take isl_set
*set
,
1988 enum isl_dim_type type
, unsigned first
, unsigned n
)
1990 return set_from_map(isl_map_eliminate(set_to_map(set
), type
, first
, n
));
1993 /* Eliminate the specified n dimensions starting at first from the
1994 * constraints, without removing the dimensions from the space.
1995 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1997 __isl_give isl_set
*isl_set_eliminate_dims(__isl_take isl_set
*set
,
1998 unsigned first
, unsigned n
)
2000 return isl_set_eliminate(set
, isl_dim_set
, first
, n
);
2003 __isl_give isl_basic_map
*isl_basic_map_remove_divs(
2004 __isl_take isl_basic_map
*bmap
)
2008 bmap
= isl_basic_map_eliminate_vars(bmap
,
2009 isl_space_dim(bmap
->dim
, isl_dim_all
), bmap
->n_div
);
2013 return isl_basic_map_finalize(bmap
);
2016 __isl_give isl_basic_set
*isl_basic_set_remove_divs(
2017 __isl_take isl_basic_set
*bset
)
2019 return bset_from_bmap(isl_basic_map_remove_divs(bset_to_bmap(bset
)));
2022 __isl_give isl_map
*isl_map_remove_divs(__isl_take isl_map
*map
)
2031 map
= isl_map_cow(map
);
2035 for (i
= 0; i
< map
->n
; ++i
) {
2036 map
->p
[i
] = isl_basic_map_remove_divs(map
->p
[i
]);
2046 __isl_give isl_set
*isl_set_remove_divs(__isl_take isl_set
*set
)
2048 return isl_map_remove_divs(set
);
2051 struct isl_basic_map
*isl_basic_map_remove_dims(struct isl_basic_map
*bmap
,
2052 enum isl_dim_type type
, unsigned first
, unsigned n
)
2054 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2055 return isl_basic_map_free(bmap
);
2056 if (n
== 0 && !isl_space_is_named_or_nested(bmap
->dim
, type
))
2058 bmap
= isl_basic_map_eliminate_vars(bmap
,
2059 isl_basic_map_offset(bmap
, type
) - 1 + first
, n
);
2062 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
) && type
== isl_dim_div
)
2064 bmap
= isl_basic_map_drop(bmap
, type
, first
, n
);
2068 /* Return true if the definition of the given div (recursively) involves
2069 * any of the given variables.
2071 static isl_bool
div_involves_vars(__isl_keep isl_basic_map
*bmap
, int div
,
2072 unsigned first
, unsigned n
)
2075 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
2077 if (isl_int_is_zero(bmap
->div
[div
][0]))
2078 return isl_bool_false
;
2079 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + first
, n
) >= 0)
2080 return isl_bool_true
;
2082 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2085 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
2087 involves
= div_involves_vars(bmap
, i
, first
, n
);
2088 if (involves
< 0 || involves
)
2092 return isl_bool_false
;
2095 /* Try and add a lower and/or upper bound on "div" to "bmap"
2096 * based on inequality "i".
2097 * "total" is the total number of variables (excluding the divs).
2098 * "v" is a temporary object that can be used during the calculations.
2099 * If "lb" is set, then a lower bound should be constructed.
2100 * If "ub" is set, then an upper bound should be constructed.
2102 * The calling function has already checked that the inequality does not
2103 * reference "div", but we still need to check that the inequality is
2104 * of the right form. We'll consider the case where we want to construct
2105 * a lower bound. The construction of upper bounds is similar.
2107 * Let "div" be of the form
2109 * q = floor((a + f(x))/d)
2111 * We essentially check if constraint "i" is of the form
2115 * so that we can use it to derive a lower bound on "div".
2116 * However, we allow a slightly more general form
2120 * with the condition that the coefficients of g(x) - f(x) are all
2122 * Rewriting this constraint as
2126 * adding a + f(x) to both sides and dividing by d, we obtain
2128 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
2130 * Taking the floor on both sides, we obtain
2132 * q >= floor((a-b)/d) + (f(x)-g(x))/d
2136 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
2138 * In the case of an upper bound, we construct the constraint
2140 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
2143 static __isl_give isl_basic_map
*insert_bounds_on_div_from_ineq(
2144 __isl_take isl_basic_map
*bmap
, int div
, int i
,
2145 unsigned total
, isl_int v
, int lb
, int ub
)
2149 for (j
= 0; (lb
|| ub
) && j
< total
+ bmap
->n_div
; ++j
) {
2151 isl_int_sub(v
, bmap
->ineq
[i
][1 + j
],
2152 bmap
->div
[div
][1 + 1 + j
]);
2153 lb
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
2156 isl_int_add(v
, bmap
->ineq
[i
][1 + j
],
2157 bmap
->div
[div
][1 + 1 + j
]);
2158 ub
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
2164 bmap
= isl_basic_map_cow(bmap
);
2165 bmap
= isl_basic_map_extend_constraints(bmap
, 0, lb
+ ub
);
2167 int k
= isl_basic_map_alloc_inequality(bmap
);
2170 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
2171 isl_int_sub(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
2172 bmap
->div
[div
][1 + j
]);
2173 isl_int_cdiv_q(bmap
->ineq
[k
][j
],
2174 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
2176 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], 1);
2179 int k
= isl_basic_map_alloc_inequality(bmap
);
2182 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
2183 isl_int_add(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
2184 bmap
->div
[div
][1 + j
]);
2185 isl_int_fdiv_q(bmap
->ineq
[k
][j
],
2186 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
2188 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], -1);
2191 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
2194 isl_basic_map_free(bmap
);
2198 /* This function is called right before "div" is eliminated from "bmap"
2199 * using Fourier-Motzkin.
2200 * Look through the constraints of "bmap" for constraints on the argument
2201 * of the integer division and use them to construct constraints on the
2202 * integer division itself. These constraints can then be combined
2203 * during the Fourier-Motzkin elimination.
2204 * Note that it is only useful to introduce lower bounds on "div"
2205 * if "bmap" already contains upper bounds on "div" as the newly
2206 * introduce lower bounds can then be combined with the pre-existing
2207 * upper bounds. Similarly for upper bounds.
2208 * We therefore first check if "bmap" contains any lower and/or upper bounds
2211 * It is interesting to note that the introduction of these constraints
2212 * can indeed lead to more accurate results, even when compared to
2213 * deriving constraints on the argument of "div" from constraints on "div".
2214 * Consider, for example, the set
2216 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2218 * The second constraint can be rewritten as
2220 * 2 * [(-i-2j+3)/4] + k >= 0
2222 * from which we can derive
2224 * -i - 2j + 3 >= -2k
2230 * Combined with the first constraint, we obtain
2232 * -3 <= 3 + 2k or k >= -3
2234 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2235 * the first constraint, we obtain
2237 * [(i + 2j)/4] >= [-3/4] = -1
2239 * Combining this constraint with the second constraint, we obtain
2243 static __isl_give isl_basic_map
*insert_bounds_on_div(
2244 __isl_take isl_basic_map
*bmap
, int div
)
2247 int check_lb
, check_ub
;
2254 if (isl_int_is_zero(bmap
->div
[div
][0]))
2257 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
2261 for (i
= 0; (!check_lb
|| !check_ub
) && i
< bmap
->n_ineq
; ++i
) {
2262 int s
= isl_int_sgn(bmap
->ineq
[i
][1 + total
+ div
]);
2269 if (!check_lb
&& !check_ub
)
2274 for (i
= 0; bmap
&& i
< bmap
->n_ineq
; ++i
) {
2275 if (!isl_int_is_zero(bmap
->ineq
[i
][1 + total
+ div
]))
2278 bmap
= insert_bounds_on_div_from_ineq(bmap
, div
, i
, total
, v
,
2279 check_lb
, check_ub
);
2287 /* Remove all divs (recursively) involving any of the given dimensions
2288 * in their definitions.
2290 __isl_give isl_basic_map
*isl_basic_map_remove_divs_involving_dims(
2291 __isl_take isl_basic_map
*bmap
,
2292 enum isl_dim_type type
, unsigned first
, unsigned n
)
2296 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2297 return isl_basic_map_free(bmap
);
2298 first
+= isl_basic_map_offset(bmap
, type
);
2300 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2303 involves
= div_involves_vars(bmap
, i
, first
, n
);
2305 return isl_basic_map_free(bmap
);
2308 bmap
= insert_bounds_on_div(bmap
, i
);
2309 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
2318 __isl_give isl_basic_set
*isl_basic_set_remove_divs_involving_dims(
2319 __isl_take isl_basic_set
*bset
,
2320 enum isl_dim_type type
, unsigned first
, unsigned n
)
2322 return isl_basic_map_remove_divs_involving_dims(bset
, type
, first
, n
);
2325 __isl_give isl_map
*isl_map_remove_divs_involving_dims(__isl_take isl_map
*map
,
2326 enum isl_dim_type type
, unsigned first
, unsigned n
)
2335 map
= isl_map_cow(map
);
2339 for (i
= 0; i
< map
->n
; ++i
) {
2340 map
->p
[i
] = isl_basic_map_remove_divs_involving_dims(map
->p
[i
],
2351 __isl_give isl_set
*isl_set_remove_divs_involving_dims(__isl_take isl_set
*set
,
2352 enum isl_dim_type type
, unsigned first
, unsigned n
)
2354 return set_from_map(isl_map_remove_divs_involving_dims(set_to_map(set
),
2358 /* Does the description of "bmap" depend on the specified dimensions?
2359 * We also check whether the dimensions appear in any of the div definitions.
2360 * In principle there is no need for this check. If the dimensions appear
2361 * in a div definition, they also appear in the defining constraints of that
2364 isl_bool
isl_basic_map_involves_dims(__isl_keep isl_basic_map
*bmap
,
2365 enum isl_dim_type type
, unsigned first
, unsigned n
)
2369 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2370 return isl_bool_error
;
2372 first
+= isl_basic_map_offset(bmap
, type
);
2373 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2374 if (isl_seq_first_non_zero(bmap
->eq
[i
] + first
, n
) >= 0)
2375 return isl_bool_true
;
2376 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2377 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + first
, n
) >= 0)
2378 return isl_bool_true
;
2379 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2380 if (isl_int_is_zero(bmap
->div
[i
][0]))
2382 if (isl_seq_first_non_zero(bmap
->div
[i
] + 1 + first
, n
) >= 0)
2383 return isl_bool_true
;
2386 return isl_bool_false
;
2389 isl_bool
isl_map_involves_dims(__isl_keep isl_map
*map
,
2390 enum isl_dim_type type
, unsigned first
, unsigned n
)
2395 return isl_bool_error
;
2397 if (first
+ n
> isl_map_dim(map
, type
))
2398 isl_die(map
->ctx
, isl_error_invalid
,
2399 "index out of bounds", return isl_bool_error
);
2401 for (i
= 0; i
< map
->n
; ++i
) {
2402 isl_bool involves
= isl_basic_map_involves_dims(map
->p
[i
],
2404 if (involves
< 0 || involves
)
2408 return isl_bool_false
;
2411 isl_bool
isl_basic_set_involves_dims(__isl_keep isl_basic_set
*bset
,
2412 enum isl_dim_type type
, unsigned first
, unsigned n
)
2414 return isl_basic_map_involves_dims(bset
, type
, first
, n
);
2417 isl_bool
isl_set_involves_dims(__isl_keep isl_set
*set
,
2418 enum isl_dim_type type
, unsigned first
, unsigned n
)
2420 return isl_map_involves_dims(set
, type
, first
, n
);
2423 /* Drop all constraints in bmap that involve any of the dimensions
2424 * first to first+n-1.
2426 static __isl_give isl_basic_map
*isl_basic_map_drop_constraints_involving(
2427 __isl_take isl_basic_map
*bmap
, unsigned first
, unsigned n
)
2434 bmap
= isl_basic_map_cow(bmap
);
2439 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
) {
2440 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + first
, n
) == -1)
2442 isl_basic_map_drop_equality(bmap
, i
);
2445 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
2446 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + 1 + first
, n
) == -1)
2448 isl_basic_map_drop_inequality(bmap
, i
);
2451 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
2455 /* Drop all constraints in bset that involve any of the dimensions
2456 * first to first+n-1.
2458 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_involving(
2459 __isl_take isl_basic_set
*bset
, unsigned first
, unsigned n
)
2461 return isl_basic_map_drop_constraints_involving(bset
, first
, n
);
2464 /* Drop all constraints in bmap that do not involve any of the dimensions
2465 * first to first + n - 1 of the given type.
2467 __isl_give isl_basic_map
*isl_basic_map_drop_constraints_not_involving_dims(
2468 __isl_take isl_basic_map
*bmap
,
2469 enum isl_dim_type type
, unsigned first
, unsigned n
)
2474 isl_space
*space
= isl_basic_map_get_space(bmap
);
2475 isl_basic_map_free(bmap
);
2476 return isl_basic_map_universe(space
);
2478 bmap
= isl_basic_map_cow(bmap
);
2482 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2483 return isl_basic_map_free(bmap
);
2485 first
+= isl_basic_map_offset(bmap
, type
) - 1;
2487 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
) {
2488 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + first
, n
) != -1)
2490 isl_basic_map_drop_equality(bmap
, i
);
2493 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
2494 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + 1 + first
, n
) != -1)
2496 isl_basic_map_drop_inequality(bmap
, i
);
2499 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
2503 /* Drop all constraints in bset that do not involve any of the dimensions
2504 * first to first + n - 1 of the given type.
2506 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_not_involving_dims(
2507 __isl_take isl_basic_set
*bset
,
2508 enum isl_dim_type type
, unsigned first
, unsigned n
)
2510 return isl_basic_map_drop_constraints_not_involving_dims(bset
,
2514 /* Drop all constraints in bmap that involve any of the dimensions
2515 * first to first + n - 1 of the given type.
2517 __isl_give isl_basic_map
*isl_basic_map_drop_constraints_involving_dims(
2518 __isl_take isl_basic_map
*bmap
,
2519 enum isl_dim_type type
, unsigned first
, unsigned n
)
2526 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2527 return isl_basic_map_free(bmap
);
2529 bmap
= isl_basic_map_remove_divs_involving_dims(bmap
, type
, first
, n
);
2530 first
+= isl_basic_map_offset(bmap
, type
) - 1;
2531 return isl_basic_map_drop_constraints_involving(bmap
, first
, n
);
2534 /* Drop all constraints in bset that involve any of the dimensions
2535 * first to first + n - 1 of the given type.
2537 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_involving_dims(
2538 __isl_take isl_basic_set
*bset
,
2539 enum isl_dim_type type
, unsigned first
, unsigned n
)
2541 return isl_basic_map_drop_constraints_involving_dims(bset
,
2545 /* Drop constraints from "map" by applying "drop" to each basic map.
2547 static __isl_give isl_map
*drop_constraints(__isl_take isl_map
*map
,
2548 enum isl_dim_type type
, unsigned first
, unsigned n
,
2549 __isl_give isl_basic_map
*(*drop
)(__isl_take isl_basic_map
*bmap
,
2550 enum isl_dim_type type
, unsigned first
, unsigned n
))
2558 dim
= isl_map_dim(map
, type
);
2559 if (first
+ n
> dim
|| first
+ n
< first
)
2560 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
2561 "index out of bounds", return isl_map_free(map
));
2563 map
= isl_map_cow(map
);
2567 for (i
= 0; i
< map
->n
; ++i
) {
2568 map
->p
[i
] = drop(map
->p
[i
], type
, first
, n
);
2570 return isl_map_free(map
);
2574 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
2579 /* Drop all constraints in map that involve any of the dimensions
2580 * first to first + n - 1 of the given type.
2582 __isl_give isl_map
*isl_map_drop_constraints_involving_dims(
2583 __isl_take isl_map
*map
,
2584 enum isl_dim_type type
, unsigned first
, unsigned n
)
2588 return drop_constraints(map
, type
, first
, n
,
2589 &isl_basic_map_drop_constraints_involving_dims
);
2592 /* Drop all constraints in "map" that do not involve any of the dimensions
2593 * first to first + n - 1 of the given type.
2595 __isl_give isl_map
*isl_map_drop_constraints_not_involving_dims(
2596 __isl_take isl_map
*map
,
2597 enum isl_dim_type type
, unsigned first
, unsigned n
)
2600 isl_space
*space
= isl_map_get_space(map
);
2602 return isl_map_universe(space
);
2604 return drop_constraints(map
, type
, first
, n
,
2605 &isl_basic_map_drop_constraints_not_involving_dims
);
2608 /* Drop all constraints in set that involve any of the dimensions
2609 * first to first + n - 1 of the given type.
2611 __isl_give isl_set
*isl_set_drop_constraints_involving_dims(
2612 __isl_take isl_set
*set
,
2613 enum isl_dim_type type
, unsigned first
, unsigned n
)
2615 return isl_map_drop_constraints_involving_dims(set
, type
, first
, n
);
2618 /* Drop all constraints in "set" that do not involve any of the dimensions
2619 * first to first + n - 1 of the given type.
2621 __isl_give isl_set
*isl_set_drop_constraints_not_involving_dims(
2622 __isl_take isl_set
*set
,
2623 enum isl_dim_type type
, unsigned first
, unsigned n
)
2625 return isl_map_drop_constraints_not_involving_dims(set
, type
, first
, n
);
2628 /* Does local variable "div" of "bmap" have a complete explicit representation?
2629 * Having a complete explicit representation requires not only
2630 * an explicit representation, but also that all local variables
2631 * that appear in this explicit representation in turn have
2632 * a complete explicit representation.
2634 isl_bool
isl_basic_map_div_is_known(__isl_keep isl_basic_map
*bmap
, int div
)
2637 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
2640 marked
= isl_basic_map_div_is_marked_unknown(bmap
, div
);
2641 if (marked
< 0 || marked
)
2642 return isl_bool_not(marked
);
2644 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2647 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
2649 known
= isl_basic_map_div_is_known(bmap
, i
);
2650 if (known
< 0 || !known
)
2654 return isl_bool_true
;
2657 /* Does local variable "div" of "bset" have a complete explicit representation?
2659 isl_bool
isl_basic_set_div_is_known(__isl_keep isl_basic_set
*bset
, int div
)
2661 return isl_basic_map_div_is_known(bset
, div
);
2664 /* Remove all divs that are unknown or defined in terms of unknown divs.
2666 __isl_give isl_basic_map
*isl_basic_map_remove_unknown_divs(
2667 __isl_take isl_basic_map
*bmap
)
2674 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2675 if (isl_basic_map_div_is_known(bmap
, i
))
2677 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
2686 /* Remove all divs that are unknown or defined in terms of unknown divs.
2688 __isl_give isl_basic_set
*isl_basic_set_remove_unknown_divs(
2689 __isl_take isl_basic_set
*bset
)
2691 return isl_basic_map_remove_unknown_divs(bset
);
2694 __isl_give isl_map
*isl_map_remove_unknown_divs(__isl_take isl_map
*map
)
2703 map
= isl_map_cow(map
);
2707 for (i
= 0; i
< map
->n
; ++i
) {
2708 map
->p
[i
] = isl_basic_map_remove_unknown_divs(map
->p
[i
]);
2718 __isl_give isl_set
*isl_set_remove_unknown_divs(__isl_take isl_set
*set
)
2720 return set_from_map(isl_map_remove_unknown_divs(set_to_map(set
)));
2723 __isl_give isl_basic_set
*isl_basic_set_remove_dims(
2724 __isl_take isl_basic_set
*bset
,
2725 enum isl_dim_type type
, unsigned first
, unsigned n
)
2727 isl_basic_map
*bmap
= bset_to_bmap(bset
);
2728 bmap
= isl_basic_map_remove_dims(bmap
, type
, first
, n
);
2729 return bset_from_bmap(bmap
);
2732 struct isl_map
*isl_map_remove_dims(struct isl_map
*map
,
2733 enum isl_dim_type type
, unsigned first
, unsigned n
)
2740 map
= isl_map_cow(map
);
2743 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
2745 for (i
= 0; i
< map
->n
; ++i
) {
2746 map
->p
[i
] = isl_basic_map_eliminate_vars(map
->p
[i
],
2747 isl_basic_map_offset(map
->p
[i
], type
) - 1 + first
, n
);
2751 map
= isl_map_drop(map
, type
, first
, n
);
2758 __isl_give isl_set
*isl_set_remove_dims(__isl_take isl_set
*bset
,
2759 enum isl_dim_type type
, unsigned first
, unsigned n
)
2761 return set_from_map(isl_map_remove_dims(set_to_map(bset
),
2765 /* Project out n inputs starting at first using Fourier-Motzkin */
2766 struct isl_map
*isl_map_remove_inputs(struct isl_map
*map
,
2767 unsigned first
, unsigned n
)
2769 return isl_map_remove_dims(map
, isl_dim_in
, first
, n
);
2772 static void dump_term(struct isl_basic_map
*bmap
,
2773 isl_int c
, int pos
, FILE *out
)
2776 unsigned in
= isl_basic_map_n_in(bmap
);
2777 unsigned dim
= in
+ isl_basic_map_n_out(bmap
);
2778 unsigned nparam
= isl_basic_map_n_param(bmap
);
2780 isl_int_print(out
, c
, 0);
2782 if (!isl_int_is_one(c
))
2783 isl_int_print(out
, c
, 0);
2784 if (pos
< 1 + nparam
) {
2785 name
= isl_space_get_dim_name(bmap
->dim
,
2786 isl_dim_param
, pos
- 1);
2788 fprintf(out
, "%s", name
);
2790 fprintf(out
, "p%d", pos
- 1);
2791 } else if (pos
< 1 + nparam
+ in
)
2792 fprintf(out
, "i%d", pos
- 1 - nparam
);
2793 else if (pos
< 1 + nparam
+ dim
)
2794 fprintf(out
, "o%d", pos
- 1 - nparam
- in
);
2796 fprintf(out
, "e%d", pos
- 1 - nparam
- dim
);
2800 static void dump_constraint_sign(struct isl_basic_map
*bmap
, isl_int
*c
,
2801 int sign
, FILE *out
)
2805 unsigned len
= 1 + isl_basic_map_total_dim(bmap
);
2809 for (i
= 0, first
= 1; i
< len
; ++i
) {
2810 if (isl_int_sgn(c
[i
]) * sign
<= 0)
2813 fprintf(out
, " + ");
2815 isl_int_abs(v
, c
[i
]);
2816 dump_term(bmap
, v
, i
, out
);
2823 static void dump_constraint(struct isl_basic_map
*bmap
, isl_int
*c
,
2824 const char *op
, FILE *out
, int indent
)
2828 fprintf(out
, "%*s", indent
, "");
2830 dump_constraint_sign(bmap
, c
, 1, out
);
2831 fprintf(out
, " %s ", op
);
2832 dump_constraint_sign(bmap
, c
, -1, out
);
2836 for (i
= bmap
->n_div
; i
< bmap
->extra
; ++i
) {
2837 if (isl_int_is_zero(c
[1+isl_space_dim(bmap
->dim
, isl_dim_all
)+i
]))
2839 fprintf(out
, "%*s", indent
, "");
2840 fprintf(out
, "ERROR: unused div coefficient not zero\n");
2845 static void dump_constraints(struct isl_basic_map
*bmap
,
2846 isl_int
**c
, unsigned n
,
2847 const char *op
, FILE *out
, int indent
)
2851 for (i
= 0; i
< n
; ++i
)
2852 dump_constraint(bmap
, c
[i
], op
, out
, indent
);
2855 static void dump_affine(struct isl_basic_map
*bmap
, isl_int
*exp
, FILE *out
)
2859 unsigned total
= isl_basic_map_total_dim(bmap
);
2861 for (j
= 0; j
< 1 + total
; ++j
) {
2862 if (isl_int_is_zero(exp
[j
]))
2864 if (!first
&& isl_int_is_pos(exp
[j
]))
2866 dump_term(bmap
, exp
[j
], j
, out
);
2871 static void dump(struct isl_basic_map
*bmap
, FILE *out
, int indent
)
2875 dump_constraints(bmap
, bmap
->eq
, bmap
->n_eq
, "=", out
, indent
);
2876 dump_constraints(bmap
, bmap
->ineq
, bmap
->n_ineq
, ">=", out
, indent
);
2878 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2879 fprintf(out
, "%*s", indent
, "");
2880 fprintf(out
, "e%d = [(", i
);
2881 dump_affine(bmap
, bmap
->div
[i
]+1, out
);
2883 isl_int_print(out
, bmap
->div
[i
][0], 0);
2884 fprintf(out
, "]\n");
2888 void isl_basic_set_print_internal(struct isl_basic_set
*bset
,
2889 FILE *out
, int indent
)
2892 fprintf(out
, "null basic set\n");
2896 fprintf(out
, "%*s", indent
, "");
2897 fprintf(out
, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
2898 bset
->ref
, bset
->dim
->nparam
, bset
->dim
->n_out
,
2899 bset
->extra
, bset
->flags
);
2900 dump(bset_to_bmap(bset
), out
, indent
);
2903 void isl_basic_map_print_internal(struct isl_basic_map
*bmap
,
2904 FILE *out
, int indent
)
2907 fprintf(out
, "null basic map\n");
2911 fprintf(out
, "%*s", indent
, "");
2912 fprintf(out
, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
2913 "flags: %x, n_name: %d\n",
2915 bmap
->dim
->nparam
, bmap
->dim
->n_in
, bmap
->dim
->n_out
,
2916 bmap
->extra
, bmap
->flags
, bmap
->dim
->n_id
);
2917 dump(bmap
, out
, indent
);
2920 int isl_inequality_negate(struct isl_basic_map
*bmap
, unsigned pos
)
2925 total
= isl_basic_map_total_dim(bmap
);
2926 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
2927 isl_seq_neg(bmap
->ineq
[pos
], bmap
->ineq
[pos
], 1 + total
);
2928 isl_int_sub_ui(bmap
->ineq
[pos
][0], bmap
->ineq
[pos
][0], 1);
2929 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
2933 __isl_give isl_set
*isl_set_alloc_space(__isl_take isl_space
*space
, int n
,
2938 if (isl_space_dim(space
, isl_dim_in
) != 0)
2939 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
2940 "set cannot have input dimensions", goto error
);
2941 return isl_map_alloc_space(space
, n
, flags
);
2943 isl_space_free(space
);
2947 struct isl_set
*isl_set_alloc(struct isl_ctx
*ctx
,
2948 unsigned nparam
, unsigned dim
, int n
, unsigned flags
)
2950 struct isl_set
*set
;
2953 dims
= isl_space_alloc(ctx
, nparam
, 0, dim
);
2957 set
= isl_set_alloc_space(dims
, n
, flags
);
2961 /* Make sure "map" has room for at least "n" more basic maps.
2963 struct isl_map
*isl_map_grow(struct isl_map
*map
, int n
)
2966 struct isl_map
*grown
= NULL
;
2970 isl_assert(map
->ctx
, n
>= 0, goto error
);
2971 if (map
->n
+ n
<= map
->size
)
2973 grown
= isl_map_alloc_space(isl_map_get_space(map
), map
->n
+ n
, map
->flags
);
2976 for (i
= 0; i
< map
->n
; ++i
) {
2977 grown
->p
[i
] = isl_basic_map_copy(map
->p
[i
]);
2985 isl_map_free(grown
);
2990 /* Make sure "set" has room for at least "n" more basic sets.
2992 struct isl_set
*isl_set_grow(struct isl_set
*set
, int n
)
2994 return set_from_map(isl_map_grow(set_to_map(set
), n
));
2997 struct isl_set
*isl_set_dup(struct isl_set
*set
)
3000 struct isl_set
*dup
;
3005 dup
= isl_set_alloc_space(isl_space_copy(set
->dim
), set
->n
, set
->flags
);
3008 for (i
= 0; i
< set
->n
; ++i
)
3009 dup
= isl_set_add_basic_set(dup
, isl_basic_set_copy(set
->p
[i
]));
3013 struct isl_set
*isl_set_from_basic_set(struct isl_basic_set
*bset
)
3015 return isl_map_from_basic_map(bset
);
3018 struct isl_map
*isl_map_from_basic_map(struct isl_basic_map
*bmap
)
3020 struct isl_map
*map
;
3025 map
= isl_map_alloc_space(isl_space_copy(bmap
->dim
), 1, ISL_MAP_DISJOINT
);
3026 return isl_map_add_basic_map(map
, bmap
);
3029 __isl_give isl_set
*isl_set_add_basic_set(__isl_take isl_set
*set
,
3030 __isl_take isl_basic_set
*bset
)
3032 return set_from_map(isl_map_add_basic_map(set_to_map(set
),
3033 bset_to_bmap(bset
)));
3036 __isl_null isl_set
*isl_set_free(__isl_take isl_set
*set
)
3038 return isl_map_free(set
);
3041 void isl_set_print_internal(struct isl_set
*set
, FILE *out
, int indent
)
3046 fprintf(out
, "null set\n");
3050 fprintf(out
, "%*s", indent
, "");
3051 fprintf(out
, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
3052 set
->ref
, set
->n
, set
->dim
->nparam
, set
->dim
->n_out
,
3054 for (i
= 0; i
< set
->n
; ++i
) {
3055 fprintf(out
, "%*s", indent
, "");
3056 fprintf(out
, "basic set %d:\n", i
);
3057 isl_basic_set_print_internal(set
->p
[i
], out
, indent
+4);
3061 void isl_map_print_internal(struct isl_map
*map
, FILE *out
, int indent
)
3066 fprintf(out
, "null map\n");
3070 fprintf(out
, "%*s", indent
, "");
3071 fprintf(out
, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
3072 "flags: %x, n_name: %d\n",
3073 map
->ref
, map
->n
, map
->dim
->nparam
, map
->dim
->n_in
,
3074 map
->dim
->n_out
, map
->flags
, map
->dim
->n_id
);
3075 for (i
= 0; i
< map
->n
; ++i
) {
3076 fprintf(out
, "%*s", indent
, "");
3077 fprintf(out
, "basic map %d:\n", i
);
3078 isl_basic_map_print_internal(map
->p
[i
], out
, indent
+4);
3082 struct isl_basic_map
*isl_basic_map_intersect_domain(
3083 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
3085 struct isl_basic_map
*bmap_domain
;
3090 isl_assert(bset
->ctx
, isl_space_match(bmap
->dim
, isl_dim_param
,
3091 bset
->dim
, isl_dim_param
), goto error
);
3093 if (isl_space_dim(bset
->dim
, isl_dim_set
) != 0)
3094 isl_assert(bset
->ctx
,
3095 isl_basic_map_compatible_domain(bmap
, bset
), goto error
);
3097 bmap
= isl_basic_map_cow(bmap
);
3100 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
3101 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
3102 bmap_domain
= isl_basic_map_from_domain(bset
);
3103 bmap
= add_constraints(bmap
, bmap_domain
, 0, 0);
3105 bmap
= isl_basic_map_simplify(bmap
);
3106 return isl_basic_map_finalize(bmap
);
3108 isl_basic_map_free(bmap
);
3109 isl_basic_set_free(bset
);
3113 /* Check that the space of "bset" is the same as that of the range of "bmap".
3115 static isl_stat
isl_basic_map_check_compatible_range(
3116 __isl_keep isl_basic_map
*bmap
, __isl_keep isl_basic_set
*bset
)
3120 ok
= isl_basic_map_compatible_range(bmap
, bset
);
3122 return isl_stat_error
;
3124 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
3125 "incompatible spaces", return isl_stat_error
);
3130 struct isl_basic_map
*isl_basic_map_intersect_range(
3131 struct isl_basic_map
*bmap
, struct isl_basic_set
*bset
)
3133 struct isl_basic_map
*bmap_range
;
3138 isl_assert(bset
->ctx
, isl_space_match(bmap
->dim
, isl_dim_param
,
3139 bset
->dim
, isl_dim_param
), goto error
);
3141 if (isl_space_dim(bset
->dim
, isl_dim_set
) != 0 &&
3142 isl_basic_map_check_compatible_range(bmap
, bset
) < 0)
3145 if (isl_basic_set_plain_is_universe(bset
)) {
3146 isl_basic_set_free(bset
);
3150 bmap
= isl_basic_map_cow(bmap
);
3153 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
3154 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
3155 bmap_range
= bset_to_bmap(bset
);
3156 bmap
= add_constraints(bmap
, bmap_range
, 0, 0);
3158 bmap
= isl_basic_map_simplify(bmap
);
3159 return isl_basic_map_finalize(bmap
);
3161 isl_basic_map_free(bmap
);
3162 isl_basic_set_free(bset
);
3166 isl_bool
isl_basic_map_contains(__isl_keep isl_basic_map
*bmap
,
3167 __isl_keep isl_vec
*vec
)
3174 return isl_bool_error
;
3176 total
= 1 + isl_basic_map_total_dim(bmap
);
3177 if (total
!= vec
->size
)
3178 return isl_bool_false
;
3182 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
3183 isl_seq_inner_product(vec
->el
, bmap
->eq
[i
], total
, &s
);
3184 if (!isl_int_is_zero(s
)) {
3186 return isl_bool_false
;
3190 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
3191 isl_seq_inner_product(vec
->el
, bmap
->ineq
[i
], total
, &s
);
3192 if (isl_int_is_neg(s
)) {
3194 return isl_bool_false
;
3200 return isl_bool_true
;
3203 isl_bool
isl_basic_set_contains(__isl_keep isl_basic_set
*bset
,
3204 __isl_keep isl_vec
*vec
)
3206 return isl_basic_map_contains(bset_to_bmap(bset
), vec
);
3209 struct isl_basic_map
*isl_basic_map_intersect(
3210 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
3212 struct isl_vec
*sample
= NULL
;
3214 if (!bmap1
|| !bmap2
)
3217 isl_assert(bmap1
->ctx
, isl_space_match(bmap1
->dim
, isl_dim_param
,
3218 bmap2
->dim
, isl_dim_param
), goto error
);
3219 if (isl_space_dim(bmap1
->dim
, isl_dim_all
) ==
3220 isl_space_dim(bmap1
->dim
, isl_dim_param
) &&
3221 isl_space_dim(bmap2
->dim
, isl_dim_all
) !=
3222 isl_space_dim(bmap2
->dim
, isl_dim_param
))
3223 return isl_basic_map_intersect(bmap2
, bmap1
);
3225 if (isl_space_dim(bmap2
->dim
, isl_dim_all
) !=
3226 isl_space_dim(bmap2
->dim
, isl_dim_param
))
3227 isl_assert(bmap1
->ctx
,
3228 isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
3230 if (isl_basic_map_plain_is_empty(bmap1
)) {
3231 isl_basic_map_free(bmap2
);
3234 if (isl_basic_map_plain_is_empty(bmap2
)) {
3235 isl_basic_map_free(bmap1
);
3239 if (bmap1
->sample
&&
3240 isl_basic_map_contains(bmap1
, bmap1
->sample
) > 0 &&
3241 isl_basic_map_contains(bmap2
, bmap1
->sample
) > 0)
3242 sample
= isl_vec_copy(bmap1
->sample
);
3243 else if (bmap2
->sample
&&
3244 isl_basic_map_contains(bmap1
, bmap2
->sample
) > 0 &&
3245 isl_basic_map_contains(bmap2
, bmap2
->sample
) > 0)
3246 sample
= isl_vec_copy(bmap2
->sample
);
3248 bmap1
= isl_basic_map_cow(bmap1
);
3251 bmap1
= isl_basic_map_extend_space(bmap1
, isl_space_copy(bmap1
->dim
),
3252 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
3253 bmap1
= add_constraints(bmap1
, bmap2
, 0, 0);
3256 isl_vec_free(sample
);
3258 isl_vec_free(bmap1
->sample
);
3259 bmap1
->sample
= sample
;
3262 bmap1
= isl_basic_map_simplify(bmap1
);
3263 return isl_basic_map_finalize(bmap1
);
3266 isl_vec_free(sample
);
3267 isl_basic_map_free(bmap1
);
3268 isl_basic_map_free(bmap2
);
3272 struct isl_basic_set
*isl_basic_set_intersect(
3273 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
3275 return bset_from_bmap(isl_basic_map_intersect(bset_to_bmap(bset1
),
3276 bset_to_bmap(bset2
)));
3279 __isl_give isl_basic_set
*isl_basic_set_intersect_params(
3280 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
3282 return isl_basic_set_intersect(bset1
, bset2
);
3285 /* Special case of isl_map_intersect, where both map1 and map2
3286 * are convex, without any divs and such that either map1 or map2
3287 * contains a single constraint. This constraint is then simply
3288 * added to the other map.
3290 static __isl_give isl_map
*map_intersect_add_constraint(
3291 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
3293 isl_assert(map1
->ctx
, map1
->n
== 1, goto error
);
3294 isl_assert(map2
->ctx
, map1
->n
== 1, goto error
);
3295 isl_assert(map1
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
3296 isl_assert(map2
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
3298 if (map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
!= 1)
3299 return isl_map_intersect(map2
, map1
);
3301 isl_assert(map2
->ctx
,
3302 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1, goto error
);
3304 map1
= isl_map_cow(map1
);
3307 if (isl_map_plain_is_empty(map1
)) {
3311 map1
->p
[0] = isl_basic_map_cow(map1
->p
[0]);
3312 if (map2
->p
[0]->n_eq
== 1)
3313 map1
->p
[0] = isl_basic_map_add_eq(map1
->p
[0], map2
->p
[0]->eq
[0]);
3315 map1
->p
[0] = isl_basic_map_add_ineq(map1
->p
[0],
3316 map2
->p
[0]->ineq
[0]);
3318 map1
->p
[0] = isl_basic_map_simplify(map1
->p
[0]);
3319 map1
->p
[0] = isl_basic_map_finalize(map1
->p
[0]);
3323 if (isl_basic_map_plain_is_empty(map1
->p
[0])) {
3324 isl_basic_map_free(map1
->p
[0]);
3337 /* map2 may be either a parameter domain or a map living in the same
3340 static __isl_give isl_map
*map_intersect_internal(__isl_take isl_map
*map1
,
3341 __isl_take isl_map
*map2
)
3350 if ((isl_map_plain_is_empty(map1
) ||
3351 isl_map_plain_is_universe(map2
)) &&
3352 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
3356 if ((isl_map_plain_is_empty(map2
) ||
3357 isl_map_plain_is_universe(map1
)) &&
3358 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
3363 if (map1
->n
== 1 && map2
->n
== 1 &&
3364 map1
->p
[0]->n_div
== 0 && map2
->p
[0]->n_div
== 0 &&
3365 isl_space_is_equal(map1
->dim
, map2
->dim
) &&
3366 (map1
->p
[0]->n_eq
+ map1
->p
[0]->n_ineq
== 1 ||
3367 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1))
3368 return map_intersect_add_constraint(map1
, map2
);
3370 if (isl_space_dim(map2
->dim
, isl_dim_all
) !=
3371 isl_space_dim(map2
->dim
, isl_dim_param
))
3372 isl_assert(map1
->ctx
,
3373 isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
3375 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
3376 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
3377 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
3379 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
3380 map1
->n
* map2
->n
, flags
);
3383 for (i
= 0; i
< map1
->n
; ++i
)
3384 for (j
= 0; j
< map2
->n
; ++j
) {
3385 struct isl_basic_map
*part
;
3386 part
= isl_basic_map_intersect(
3387 isl_basic_map_copy(map1
->p
[i
]),
3388 isl_basic_map_copy(map2
->p
[j
]));
3389 if (isl_basic_map_is_empty(part
) < 0)
3390 part
= isl_basic_map_free(part
);
3391 result
= isl_map_add_basic_map(result
, part
);
3404 static __isl_give isl_map
*map_intersect(__isl_take isl_map
*map1
,
3405 __isl_take isl_map
*map2
)
3409 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
3410 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
3411 "spaces don't match", goto error
);
3412 return map_intersect_internal(map1
, map2
);
3419 __isl_give isl_map
*isl_map_intersect(__isl_take isl_map
*map1
,
3420 __isl_take isl_map
*map2
)
3422 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect
);
3425 struct isl_set
*isl_set_intersect(struct isl_set
*set1
, struct isl_set
*set2
)
3427 return set_from_map(isl_map_intersect(set_to_map(set1
),
3431 /* map_intersect_internal accepts intersections
3432 * with parameter domains, so we can just call that function.
3434 static __isl_give isl_map
*map_intersect_params(__isl_take isl_map
*map
,
3435 __isl_take isl_set
*params
)
3437 return map_intersect_internal(map
, params
);
3440 __isl_give isl_map
*isl_map_intersect_params(__isl_take isl_map
*map1
,
3441 __isl_take isl_map
*map2
)
3443 return isl_map_align_params_map_map_and(map1
, map2
, &map_intersect_params
);
3446 __isl_give isl_set
*isl_set_intersect_params(__isl_take isl_set
*set
,
3447 __isl_take isl_set
*params
)
3449 return isl_map_intersect_params(set
, params
);
3452 struct isl_basic_map
*isl_basic_map_reverse(struct isl_basic_map
*bmap
)
3455 unsigned pos
, n1
, n2
;
3459 bmap
= isl_basic_map_cow(bmap
);
3462 space
= isl_space_reverse(isl_space_copy(bmap
->dim
));
3463 pos
= isl_basic_map_offset(bmap
, isl_dim_in
);
3464 n1
= isl_basic_map_dim(bmap
, isl_dim_in
);
3465 n2
= isl_basic_map_dim(bmap
, isl_dim_out
);
3466 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
3467 return isl_basic_map_reset_space(bmap
, space
);
3470 static __isl_give isl_basic_map
*basic_map_space_reset(
3471 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
)
3477 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
3480 space
= isl_basic_map_get_space(bmap
);
3481 space
= isl_space_reset(space
, type
);
3482 bmap
= isl_basic_map_reset_space(bmap
, space
);
3486 __isl_give isl_basic_map
*isl_basic_map_insert_dims(
3487 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
3488 unsigned pos
, unsigned n
)
3491 struct isl_basic_map
*res
;
3492 struct isl_dim_map
*dim_map
;
3493 unsigned total
, off
;
3494 enum isl_dim_type t
;
3497 return basic_map_space_reset(bmap
, type
);
3502 res_dim
= isl_space_insert_dims(isl_basic_map_get_space(bmap
), type
, pos
, n
);
3504 total
= isl_basic_map_total_dim(bmap
) + n
;
3505 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3507 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3509 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3511 unsigned size
= isl_basic_map_dim(bmap
, t
);
3512 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3514 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3515 pos
, size
- pos
, off
+ pos
+ n
);
3517 off
+= isl_space_dim(res_dim
, t
);
3519 isl_dim_map_div(dim_map
, bmap
, off
);
3521 res
= isl_basic_map_alloc_space(res_dim
,
3522 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3523 if (isl_basic_map_is_rational(bmap
))
3524 res
= isl_basic_map_set_rational(res
);
3525 if (isl_basic_map_plain_is_empty(bmap
)) {
3526 isl_basic_map_free(bmap
);
3528 return isl_basic_map_set_to_empty(res
);
3530 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3531 return isl_basic_map_finalize(res
);
3534 __isl_give isl_basic_set
*isl_basic_set_insert_dims(
3535 __isl_take isl_basic_set
*bset
,
3536 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3538 return isl_basic_map_insert_dims(bset
, type
, pos
, n
);
3541 __isl_give isl_basic_map
*isl_basic_map_add_dims(__isl_take isl_basic_map
*bmap
,
3542 enum isl_dim_type type
, unsigned n
)
3546 return isl_basic_map_insert_dims(bmap
, type
,
3547 isl_basic_map_dim(bmap
, type
), n
);
3550 __isl_give isl_basic_set
*isl_basic_set_add_dims(__isl_take isl_basic_set
*bset
,
3551 enum isl_dim_type type
, unsigned n
)
3555 isl_assert(bset
->ctx
, type
!= isl_dim_in
, goto error
);
3556 return isl_basic_map_add_dims(bset
, type
, n
);
3558 isl_basic_set_free(bset
);
3562 static __isl_give isl_map
*map_space_reset(__isl_take isl_map
*map
,
3563 enum isl_dim_type type
)
3567 if (!map
|| !isl_space_is_named_or_nested(map
->dim
, type
))
3570 space
= isl_map_get_space(map
);
3571 space
= isl_space_reset(space
, type
);
3572 map
= isl_map_reset_space(map
, space
);
3576 __isl_give isl_map
*isl_map_insert_dims(__isl_take isl_map
*map
,
3577 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3582 return map_space_reset(map
, type
);
3584 map
= isl_map_cow(map
);
3588 map
->dim
= isl_space_insert_dims(map
->dim
, type
, pos
, n
);
3592 for (i
= 0; i
< map
->n
; ++i
) {
3593 map
->p
[i
] = isl_basic_map_insert_dims(map
->p
[i
], type
, pos
, n
);
3604 __isl_give isl_set
*isl_set_insert_dims(__isl_take isl_set
*set
,
3605 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3607 return isl_map_insert_dims(set
, type
, pos
, n
);
3610 __isl_give isl_map
*isl_map_add_dims(__isl_take isl_map
*map
,
3611 enum isl_dim_type type
, unsigned n
)
3615 return isl_map_insert_dims(map
, type
, isl_map_dim(map
, type
), n
);
3618 __isl_give isl_set
*isl_set_add_dims(__isl_take isl_set
*set
,
3619 enum isl_dim_type type
, unsigned n
)
3623 isl_assert(set
->ctx
, type
!= isl_dim_in
, goto error
);
3624 return set_from_map(isl_map_add_dims(set_to_map(set
), type
, n
));
3630 __isl_give isl_basic_map
*isl_basic_map_move_dims(
3631 __isl_take isl_basic_map
*bmap
,
3632 enum isl_dim_type dst_type
, unsigned dst_pos
,
3633 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3635 struct isl_dim_map
*dim_map
;
3636 struct isl_basic_map
*res
;
3637 enum isl_dim_type t
;
3638 unsigned total
, off
;
3645 if (isl_basic_map_check_range(bmap
, src_type
, src_pos
, n
) < 0)
3646 return isl_basic_map_free(bmap
);
3648 if (dst_type
== src_type
&& dst_pos
== src_pos
)
3651 isl_assert(bmap
->ctx
, dst_type
!= src_type
, goto error
);
3653 if (pos(bmap
->dim
, dst_type
) + dst_pos
==
3654 pos(bmap
->dim
, src_type
) + src_pos
+
3655 ((src_type
< dst_type
) ? n
: 0)) {
3656 bmap
= isl_basic_map_cow(bmap
);
3660 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
3661 src_type
, src_pos
, n
);
3665 bmap
= isl_basic_map_finalize(bmap
);
3670 total
= isl_basic_map_total_dim(bmap
);
3671 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3674 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3675 unsigned size
= isl_space_dim(bmap
->dim
, t
);
3676 if (t
== dst_type
) {
3677 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3680 isl_dim_map_dim_range(dim_map
, bmap
->dim
, src_type
,
3683 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3684 dst_pos
, size
- dst_pos
, off
);
3685 off
+= size
- dst_pos
;
3686 } else if (t
== src_type
) {
3687 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3690 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3691 src_pos
+ n
, size
- src_pos
- n
, off
);
3692 off
+= size
- src_pos
- n
;
3694 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3698 isl_dim_map_div(dim_map
, bmap
, off
);
3700 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
3701 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3702 bmap
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3706 bmap
->dim
= isl_space_move_dims(bmap
->dim
, dst_type
, dst_pos
,
3707 src_type
, src_pos
, n
);
3711 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
3712 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3713 bmap
= isl_basic_map_finalize(bmap
);
3717 isl_basic_map_free(bmap
);
3721 __isl_give isl_basic_set
*isl_basic_set_move_dims(__isl_take isl_basic_set
*bset
,
3722 enum isl_dim_type dst_type
, unsigned dst_pos
,
3723 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3725 isl_basic_map
*bmap
= bset_to_bmap(bset
);
3726 bmap
= isl_basic_map_move_dims(bmap
, dst_type
, dst_pos
,
3727 src_type
, src_pos
, n
);
3728 return bset_from_bmap(bmap
);
3731 __isl_give isl_set
*isl_set_move_dims(__isl_take isl_set
*set
,
3732 enum isl_dim_type dst_type
, unsigned dst_pos
,
3733 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3737 isl_assert(set
->ctx
, dst_type
!= isl_dim_in
, goto error
);
3738 return set_from_map(isl_map_move_dims(set_to_map(set
),
3739 dst_type
, dst_pos
, src_type
, src_pos
, n
));
3745 __isl_give isl_map
*isl_map_move_dims(__isl_take isl_map
*map
,
3746 enum isl_dim_type dst_type
, unsigned dst_pos
,
3747 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3756 isl_assert(map
->ctx
, src_pos
+ n
<= isl_map_dim(map
, src_type
),
3759 if (dst_type
== src_type
&& dst_pos
== src_pos
)
3762 isl_assert(map
->ctx
, dst_type
!= src_type
, goto error
);
3764 map
= isl_map_cow(map
);
3768 map
->dim
= isl_space_move_dims(map
->dim
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
3772 for (i
= 0; i
< map
->n
; ++i
) {
3773 map
->p
[i
] = isl_basic_map_move_dims(map
->p
[i
],
3775 src_type
, src_pos
, n
);
3786 /* Move the specified dimensions to the last columns right before
3787 * the divs. Don't change the dimension specification of bmap.
3788 * That's the responsibility of the caller.
3790 static __isl_give isl_basic_map
*move_last(__isl_take isl_basic_map
*bmap
,
3791 enum isl_dim_type type
, unsigned first
, unsigned n
)
3793 struct isl_dim_map
*dim_map
;
3794 struct isl_basic_map
*res
;
3795 enum isl_dim_type t
;
3796 unsigned total
, off
;
3800 if (pos(bmap
->dim
, type
) + first
+ n
==
3801 1 + isl_space_dim(bmap
->dim
, isl_dim_all
))
3804 total
= isl_basic_map_total_dim(bmap
);
3805 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
3808 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3809 unsigned size
= isl_space_dim(bmap
->dim
, t
);
3811 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3814 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3815 first
, n
, total
- bmap
->n_div
- n
);
3816 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3817 first
+ n
, size
- (first
+ n
), off
);
3818 off
+= size
- (first
+ n
);
3820 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3824 isl_dim_map_div(dim_map
, bmap
, off
+ n
);
3826 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
3827 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3828 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3832 /* Insert "n" rows in the divs of "bmap".
3834 * The number of columns is not changed, which means that the last
3835 * dimensions of "bmap" are being reintepreted as the new divs.
3836 * The space of "bmap" is not adjusted, however, which means
3837 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
3838 * from the space of "bmap" is the responsibility of the caller.
3840 static __isl_give isl_basic_map
*insert_div_rows(__isl_take isl_basic_map
*bmap
,
3848 bmap
= isl_basic_map_cow(bmap
);
3852 row_size
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + bmap
->extra
;
3853 old
= bmap
->block2
.data
;
3854 bmap
->block2
= isl_blk_extend(bmap
->ctx
, bmap
->block2
,
3855 (bmap
->extra
+ n
) * (1 + row_size
));
3856 if (!bmap
->block2
.data
)
3857 return isl_basic_map_free(bmap
);
3858 new_div
= isl_alloc_array(bmap
->ctx
, isl_int
*, bmap
->extra
+ n
);
3860 return isl_basic_map_free(bmap
);
3861 for (i
= 0; i
< n
; ++i
) {
3862 new_div
[i
] = bmap
->block2
.data
+
3863 (bmap
->extra
+ i
) * (1 + row_size
);
3864 isl_seq_clr(new_div
[i
], 1 + row_size
);
3866 for (i
= 0; i
< bmap
->extra
; ++i
)
3867 new_div
[n
+ i
] = bmap
->block2
.data
+ (bmap
->div
[i
] - old
);
3869 bmap
->div
= new_div
;
3876 /* Drop constraints from "bmap" that only involve the variables
3877 * of "type" in the range [first, first + n] that are not related
3878 * to any of the variables outside that interval.
3879 * These constraints cannot influence the values for the variables
3880 * outside the interval, except in case they cause "bmap" to be empty.
3881 * Only drop the constraints if "bmap" is known to be non-empty.
3883 static __isl_give isl_basic_map
*drop_irrelevant_constraints(
3884 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
3885 unsigned first
, unsigned n
)
3889 unsigned dim
, n_div
;
3892 non_empty
= isl_basic_map_plain_is_non_empty(bmap
);
3894 return isl_basic_map_free(bmap
);
3898 dim
= isl_basic_map_dim(bmap
, isl_dim_all
);
3899 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
3900 groups
= isl_calloc_array(isl_basic_map_get_ctx(bmap
), int, dim
);
3902 return isl_basic_map_free(bmap
);
3903 first
+= isl_basic_map_offset(bmap
, type
) - 1;
3904 for (i
= 0; i
< first
; ++i
)
3906 for (i
= first
+ n
; i
< dim
- n_div
; ++i
)
3909 bmap
= isl_basic_map_drop_unrelated_constraints(bmap
, groups
);
3914 /* Turn the n dimensions of type type, starting at first
3915 * into existentially quantified variables.
3917 * If a subset of the projected out variables are unrelated
3918 * to any of the variables that remain, then the constraints
3919 * involving this subset are simply dropped first.
3921 __isl_give isl_basic_map
*isl_basic_map_project_out(
3922 __isl_take isl_basic_map
*bmap
,
3923 enum isl_dim_type type
, unsigned first
, unsigned n
)
3926 return basic_map_space_reset(bmap
, type
);
3927 if (type
== isl_dim_div
)
3928 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
3929 "cannot project out existentially quantified variables",
3930 return isl_basic_map_free(bmap
));
3932 bmap
= drop_irrelevant_constraints(bmap
, type
, first
, n
);
3936 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
3937 return isl_basic_map_remove_dims(bmap
, type
, first
, n
);
3939 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
3940 return isl_basic_map_free(bmap
);
3942 bmap
= move_last(bmap
, type
, first
, n
);
3943 bmap
= isl_basic_map_cow(bmap
);
3944 bmap
= insert_div_rows(bmap
, n
);
3948 bmap
->dim
= isl_space_drop_dims(bmap
->dim
, type
, first
, n
);
3951 bmap
= isl_basic_map_simplify(bmap
);
3952 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
3953 return isl_basic_map_finalize(bmap
);
3955 isl_basic_map_free(bmap
);
3959 /* Turn the n dimensions of type type, starting at first
3960 * into existentially quantified variables.
3962 struct isl_basic_set
*isl_basic_set_project_out(struct isl_basic_set
*bset
,
3963 enum isl_dim_type type
, unsigned first
, unsigned n
)
3965 return bset_from_bmap(isl_basic_map_project_out(bset_to_bmap(bset
),
3969 /* Turn the n dimensions of type type, starting at first
3970 * into existentially quantified variables.
3972 __isl_give isl_map
*isl_map_project_out(__isl_take isl_map
*map
,
3973 enum isl_dim_type type
, unsigned first
, unsigned n
)
3981 return map_space_reset(map
, type
);
3983 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
3985 map
= isl_map_cow(map
);
3989 map
->dim
= isl_space_drop_dims(map
->dim
, type
, first
, n
);
3993 for (i
= 0; i
< map
->n
; ++i
) {
3994 map
->p
[i
] = isl_basic_map_project_out(map
->p
[i
], type
, first
, n
);
4005 /* Turn the n dimensions of type type, starting at first
4006 * into existentially quantified variables.
4008 __isl_give isl_set
*isl_set_project_out(__isl_take isl_set
*set
,
4009 enum isl_dim_type type
, unsigned first
, unsigned n
)
4011 return set_from_map(isl_map_project_out(set_to_map(set
),
4015 /* Return a map that projects the elements in "set" onto their
4016 * "n" set dimensions starting at "first".
4017 * "type" should be equal to isl_dim_set.
4019 __isl_give isl_map
*isl_set_project_onto_map(__isl_take isl_set
*set
,
4020 enum isl_dim_type type
, unsigned first
, unsigned n
)
4028 if (type
!= isl_dim_set
)
4029 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
4030 "only set dimensions can be projected out", goto error
);
4031 dim
= isl_set_dim(set
, isl_dim_set
);
4032 if (first
+ n
> dim
|| first
+ n
< first
)
4033 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
4034 "index out of bounds", goto error
);
4036 map
= isl_map_from_domain(set
);
4037 map
= isl_map_add_dims(map
, isl_dim_out
, n
);
4038 for (i
= 0; i
< n
; ++i
)
4039 map
= isl_map_equate(map
, isl_dim_in
, first
+ i
,
4047 static struct isl_basic_map
*add_divs(struct isl_basic_map
*bmap
, unsigned n
)
4051 for (i
= 0; i
< n
; ++i
) {
4052 j
= isl_basic_map_alloc_div(bmap
);
4055 isl_seq_clr(bmap
->div
[j
], 1+1+isl_basic_map_total_dim(bmap
));
4059 isl_basic_map_free(bmap
);
4063 struct isl_basic_map
*isl_basic_map_apply_range(
4064 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
4066 isl_space
*dim_result
= NULL
;
4067 struct isl_basic_map
*bmap
;
4068 unsigned n_in
, n_out
, n
, nparam
, total
, pos
;
4069 struct isl_dim_map
*dim_map1
, *dim_map2
;
4071 if (!bmap1
|| !bmap2
)
4073 if (!isl_space_match(bmap1
->dim
, isl_dim_param
,
4074 bmap2
->dim
, isl_dim_param
))
4075 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
4076 "parameters don't match", goto error
);
4077 if (!isl_space_tuple_is_equal(bmap1
->dim
, isl_dim_out
,
4078 bmap2
->dim
, isl_dim_in
))
4079 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
4080 "spaces don't match", goto error
);
4082 dim_result
= isl_space_join(isl_space_copy(bmap1
->dim
),
4083 isl_space_copy(bmap2
->dim
));
4085 n_in
= isl_basic_map_n_in(bmap1
);
4086 n_out
= isl_basic_map_n_out(bmap2
);
4087 n
= isl_basic_map_n_out(bmap1
);
4088 nparam
= isl_basic_map_n_param(bmap1
);
4090 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ n
;
4091 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4092 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4093 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
4094 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
4095 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
4096 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_in
);
4097 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_out
);
4098 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
4099 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
4100 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
4102 bmap
= isl_basic_map_alloc_space(dim_result
,
4103 bmap1
->n_div
+ bmap2
->n_div
+ n
,
4104 bmap1
->n_eq
+ bmap2
->n_eq
,
4105 bmap1
->n_ineq
+ bmap2
->n_ineq
);
4106 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
4107 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
4108 bmap
= add_divs(bmap
, n
);
4109 bmap
= isl_basic_map_simplify(bmap
);
4110 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
4111 return isl_basic_map_finalize(bmap
);
4113 isl_basic_map_free(bmap1
);
4114 isl_basic_map_free(bmap2
);
4118 struct isl_basic_set
*isl_basic_set_apply(
4119 struct isl_basic_set
*bset
, struct isl_basic_map
*bmap
)
4124 isl_assert(bset
->ctx
, isl_basic_map_compatible_domain(bmap
, bset
),
4127 return bset_from_bmap(isl_basic_map_apply_range(bset_to_bmap(bset
),
4130 isl_basic_set_free(bset
);
4131 isl_basic_map_free(bmap
);
4135 struct isl_basic_map
*isl_basic_map_apply_domain(
4136 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
4138 if (!bmap1
|| !bmap2
)
4141 isl_assert(bmap1
->ctx
,
4142 isl_basic_map_n_in(bmap1
) == isl_basic_map_n_in(bmap2
), goto error
);
4143 isl_assert(bmap1
->ctx
,
4144 isl_basic_map_n_param(bmap1
) == isl_basic_map_n_param(bmap2
),
4147 bmap1
= isl_basic_map_reverse(bmap1
);
4148 bmap1
= isl_basic_map_apply_range(bmap1
, bmap2
);
4149 return isl_basic_map_reverse(bmap1
);
4151 isl_basic_map_free(bmap1
);
4152 isl_basic_map_free(bmap2
);
4156 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
4157 * A \cap B -> f(A) + f(B)
4159 struct isl_basic_map
*isl_basic_map_sum(
4160 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
4162 unsigned n_in
, n_out
, nparam
, total
, pos
;
4163 struct isl_basic_map
*bmap
= NULL
;
4164 struct isl_dim_map
*dim_map1
, *dim_map2
;
4167 if (!bmap1
|| !bmap2
)
4170 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
),
4173 nparam
= isl_basic_map_n_param(bmap1
);
4174 n_in
= isl_basic_map_n_in(bmap1
);
4175 n_out
= isl_basic_map_n_out(bmap1
);
4177 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
;
4178 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4179 dim_map2
= isl_dim_map_alloc(bmap2
->ctx
, total
);
4180 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
4181 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
);
4182 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
4183 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
4184 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_in
+ n_out
);
4185 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
4186 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
4187 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_out
);
4189 bmap
= isl_basic_map_alloc_space(isl_space_copy(bmap1
->dim
),
4190 bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
,
4191 bmap1
->n_eq
+ bmap2
->n_eq
+ n_out
,
4192 bmap1
->n_ineq
+ bmap2
->n_ineq
);
4193 for (i
= 0; i
< n_out
; ++i
) {
4194 int j
= isl_basic_map_alloc_equality(bmap
);
4197 isl_seq_clr(bmap
->eq
[j
], 1+total
);
4198 isl_int_set_si(bmap
->eq
[j
][1+nparam
+n_in
+i
], -1);
4199 isl_int_set_si(bmap
->eq
[j
][1+pos
+i
], 1);
4200 isl_int_set_si(bmap
->eq
[j
][1+pos
-n_out
+i
], 1);
4202 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
4203 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
4204 bmap
= add_divs(bmap
, 2 * n_out
);
4206 bmap
= isl_basic_map_simplify(bmap
);
4207 return isl_basic_map_finalize(bmap
);
4209 isl_basic_map_free(bmap
);
4210 isl_basic_map_free(bmap1
);
4211 isl_basic_map_free(bmap2
);
4215 /* Given two maps A -> f(A) and B -> g(B), construct a map
4216 * A \cap B -> f(A) + f(B)
4218 struct isl_map
*isl_map_sum(struct isl_map
*map1
, struct isl_map
*map2
)
4220 struct isl_map
*result
;
4226 isl_assert(map1
->ctx
, isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
4228 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
4229 map1
->n
* map2
->n
, 0);
4232 for (i
= 0; i
< map1
->n
; ++i
)
4233 for (j
= 0; j
< map2
->n
; ++j
) {
4234 struct isl_basic_map
*part
;
4235 part
= isl_basic_map_sum(
4236 isl_basic_map_copy(map1
->p
[i
]),
4237 isl_basic_map_copy(map2
->p
[j
]));
4238 if (isl_basic_map_is_empty(part
))
4239 isl_basic_map_free(part
);
4241 result
= isl_map_add_basic_map(result
, part
);
4254 __isl_give isl_set
*isl_set_sum(__isl_take isl_set
*set1
,
4255 __isl_take isl_set
*set2
)
4257 return set_from_map(isl_map_sum(set_to_map(set1
), set_to_map(set2
)));
4260 /* Given a basic map A -> f(A), construct A -> -f(A).
4262 struct isl_basic_map
*isl_basic_map_neg(struct isl_basic_map
*bmap
)
4267 bmap
= isl_basic_map_cow(bmap
);
4271 n
= isl_basic_map_dim(bmap
, isl_dim_out
);
4272 off
= isl_basic_map_offset(bmap
, isl_dim_out
);
4273 for (i
= 0; i
< bmap
->n_eq
; ++i
)
4274 for (j
= 0; j
< n
; ++j
)
4275 isl_int_neg(bmap
->eq
[i
][off
+j
], bmap
->eq
[i
][off
+j
]);
4276 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
4277 for (j
= 0; j
< n
; ++j
)
4278 isl_int_neg(bmap
->ineq
[i
][off
+j
], bmap
->ineq
[i
][off
+j
]);
4279 for (i
= 0; i
< bmap
->n_div
; ++i
)
4280 for (j
= 0; j
< n
; ++j
)
4281 isl_int_neg(bmap
->div
[i
][1+off
+j
], bmap
->div
[i
][1+off
+j
]);
4282 bmap
= isl_basic_map_gauss(bmap
, NULL
);
4283 return isl_basic_map_finalize(bmap
);
4286 __isl_give isl_basic_set
*isl_basic_set_neg(__isl_take isl_basic_set
*bset
)
4288 return isl_basic_map_neg(bset
);
4291 /* Given a map A -> f(A), construct A -> -f(A).
4293 struct isl_map
*isl_map_neg(struct isl_map
*map
)
4297 map
= isl_map_cow(map
);
4301 for (i
= 0; i
< map
->n
; ++i
) {
4302 map
->p
[i
] = isl_basic_map_neg(map
->p
[i
]);
4313 __isl_give isl_set
*isl_set_neg(__isl_take isl_set
*set
)
4315 return set_from_map(isl_map_neg(set_to_map(set
)));
4318 /* Given a basic map A -> f(A) and an integer d, construct a basic map
4319 * A -> floor(f(A)/d).
4321 struct isl_basic_map
*isl_basic_map_floordiv(struct isl_basic_map
*bmap
,
4324 unsigned n_in
, n_out
, nparam
, total
, pos
;
4325 struct isl_basic_map
*result
= NULL
;
4326 struct isl_dim_map
*dim_map
;
4332 nparam
= isl_basic_map_n_param(bmap
);
4333 n_in
= isl_basic_map_n_in(bmap
);
4334 n_out
= isl_basic_map_n_out(bmap
);
4336 total
= nparam
+ n_in
+ n_out
+ bmap
->n_div
+ n_out
;
4337 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
4338 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_param
, pos
= 0);
4339 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_in
, pos
+= nparam
);
4340 isl_dim_map_div(dim_map
, bmap
, pos
+= n_in
+ n_out
);
4341 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_out
, pos
+= bmap
->n_div
);
4343 result
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
4344 bmap
->n_div
+ n_out
,
4345 bmap
->n_eq
, bmap
->n_ineq
+ 2 * n_out
);
4346 result
= isl_basic_map_add_constraints_dim_map(result
, bmap
, dim_map
);
4347 result
= add_divs(result
, n_out
);
4348 for (i
= 0; i
< n_out
; ++i
) {
4350 j
= isl_basic_map_alloc_inequality(result
);
4353 isl_seq_clr(result
->ineq
[j
], 1+total
);
4354 isl_int_neg(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
4355 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], 1);
4356 j
= isl_basic_map_alloc_inequality(result
);
4359 isl_seq_clr(result
->ineq
[j
], 1+total
);
4360 isl_int_set(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
4361 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], -1);
4362 isl_int_sub_ui(result
->ineq
[j
][0], d
, 1);
4365 result
= isl_basic_map_simplify(result
);
4366 return isl_basic_map_finalize(result
);
4368 isl_basic_map_free(result
);
4372 /* Given a map A -> f(A) and an integer d, construct a map
4373 * A -> floor(f(A)/d).
4375 struct isl_map
*isl_map_floordiv(struct isl_map
*map
, isl_int d
)
4379 map
= isl_map_cow(map
);
4383 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
4384 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
4385 for (i
= 0; i
< map
->n
; ++i
) {
4386 map
->p
[i
] = isl_basic_map_floordiv(map
->p
[i
], d
);
4397 /* Given a map A -> f(A) and an integer d, construct a map
4398 * A -> floor(f(A)/d).
4400 __isl_give isl_map
*isl_map_floordiv_val(__isl_take isl_map
*map
,
4401 __isl_take isl_val
*d
)
4405 if (!isl_val_is_int(d
))
4406 isl_die(isl_val_get_ctx(d
), isl_error_invalid
,
4407 "expecting integer denominator", goto error
);
4408 map
= isl_map_floordiv(map
, d
->n
);
4417 static struct isl_basic_map
*var_equal(struct isl_basic_map
*bmap
, unsigned pos
)
4423 i
= isl_basic_map_alloc_equality(bmap
);
4426 nparam
= isl_basic_map_n_param(bmap
);
4427 n_in
= isl_basic_map_n_in(bmap
);
4428 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4429 isl_int_set_si(bmap
->eq
[i
][1+nparam
+pos
], -1);
4430 isl_int_set_si(bmap
->eq
[i
][1+nparam
+n_in
+pos
], 1);
4431 return isl_basic_map_finalize(bmap
);
4433 isl_basic_map_free(bmap
);
4437 /* Add a constraint to "bmap" expressing i_pos < o_pos
4439 static struct isl_basic_map
*var_less(struct isl_basic_map
*bmap
, unsigned pos
)
4445 i
= isl_basic_map_alloc_inequality(bmap
);
4448 nparam
= isl_basic_map_n_param(bmap
);
4449 n_in
= isl_basic_map_n_in(bmap
);
4450 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4451 isl_int_set_si(bmap
->ineq
[i
][0], -1);
4452 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
4453 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
4454 return isl_basic_map_finalize(bmap
);
4456 isl_basic_map_free(bmap
);
4460 /* Add a constraint to "bmap" expressing i_pos <= o_pos
4462 static __isl_give isl_basic_map
*var_less_or_equal(
4463 __isl_take isl_basic_map
*bmap
, unsigned pos
)
4469 i
= isl_basic_map_alloc_inequality(bmap
);
4472 nparam
= isl_basic_map_n_param(bmap
);
4473 n_in
= isl_basic_map_n_in(bmap
);
4474 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4475 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
4476 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
4477 return isl_basic_map_finalize(bmap
);
4479 isl_basic_map_free(bmap
);
4483 /* Add a constraint to "bmap" expressing i_pos > o_pos
4485 static struct isl_basic_map
*var_more(struct isl_basic_map
*bmap
, unsigned pos
)
4491 i
= isl_basic_map_alloc_inequality(bmap
);
4494 nparam
= isl_basic_map_n_param(bmap
);
4495 n_in
= isl_basic_map_n_in(bmap
);
4496 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4497 isl_int_set_si(bmap
->ineq
[i
][0], -1);
4498 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
4499 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
4500 return isl_basic_map_finalize(bmap
);
4502 isl_basic_map_free(bmap
);
4506 /* Add a constraint to "bmap" expressing i_pos >= o_pos
4508 static __isl_give isl_basic_map
*var_more_or_equal(
4509 __isl_take isl_basic_map
*bmap
, unsigned pos
)
4515 i
= isl_basic_map_alloc_inequality(bmap
);
4518 nparam
= isl_basic_map_n_param(bmap
);
4519 n_in
= isl_basic_map_n_in(bmap
);
4520 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
4521 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
4522 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
4523 return isl_basic_map_finalize(bmap
);
4525 isl_basic_map_free(bmap
);
4529 __isl_give isl_basic_map
*isl_basic_map_equal(
4530 __isl_take isl_space
*dim
, unsigned n_equal
)
4533 struct isl_basic_map
*bmap
;
4534 bmap
= isl_basic_map_alloc_space(dim
, 0, n_equal
, 0);
4537 for (i
= 0; i
< n_equal
&& bmap
; ++i
)
4538 bmap
= var_equal(bmap
, i
);
4539 return isl_basic_map_finalize(bmap
);
4542 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
4544 __isl_give isl_basic_map
*isl_basic_map_less_at(__isl_take isl_space
*dim
,
4548 struct isl_basic_map
*bmap
;
4549 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4552 for (i
= 0; i
< pos
&& bmap
; ++i
)
4553 bmap
= var_equal(bmap
, i
);
4555 bmap
= var_less(bmap
, pos
);
4556 return isl_basic_map_finalize(bmap
);
4559 /* Return a relation on "dim" expressing i_[0..pos] <<= o_[0..pos]
4561 __isl_give isl_basic_map
*isl_basic_map_less_or_equal_at(
4562 __isl_take isl_space
*dim
, unsigned pos
)
4565 isl_basic_map
*bmap
;
4567 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4568 for (i
= 0; i
< pos
; ++i
)
4569 bmap
= var_equal(bmap
, i
);
4570 bmap
= var_less_or_equal(bmap
, pos
);
4571 return isl_basic_map_finalize(bmap
);
4574 /* Return a relation on "dim" expressing i_pos > o_pos
4576 __isl_give isl_basic_map
*isl_basic_map_more_at(__isl_take isl_space
*dim
,
4580 struct isl_basic_map
*bmap
;
4581 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4584 for (i
= 0; i
< pos
&& bmap
; ++i
)
4585 bmap
= var_equal(bmap
, i
);
4587 bmap
= var_more(bmap
, pos
);
4588 return isl_basic_map_finalize(bmap
);
4591 /* Return a relation on "dim" expressing i_[0..pos] >>= o_[0..pos]
4593 __isl_give isl_basic_map
*isl_basic_map_more_or_equal_at(
4594 __isl_take isl_space
*dim
, unsigned pos
)
4597 isl_basic_map
*bmap
;
4599 bmap
= isl_basic_map_alloc_space(dim
, 0, pos
, 1);
4600 for (i
= 0; i
< pos
; ++i
)
4601 bmap
= var_equal(bmap
, i
);
4602 bmap
= var_more_or_equal(bmap
, pos
);
4603 return isl_basic_map_finalize(bmap
);
4606 static __isl_give isl_map
*map_lex_lte_first(__isl_take isl_space
*dims
,
4607 unsigned n
, int equal
)
4609 struct isl_map
*map
;
4612 if (n
== 0 && equal
)
4613 return isl_map_universe(dims
);
4615 map
= isl_map_alloc_space(isl_space_copy(dims
), n
, ISL_MAP_DISJOINT
);
4617 for (i
= 0; i
+ 1 < n
; ++i
)
4618 map
= isl_map_add_basic_map(map
,
4619 isl_basic_map_less_at(isl_space_copy(dims
), i
));
4622 map
= isl_map_add_basic_map(map
,
4623 isl_basic_map_less_or_equal_at(dims
, n
- 1));
4625 map
= isl_map_add_basic_map(map
,
4626 isl_basic_map_less_at(dims
, n
- 1));
4628 isl_space_free(dims
);
4633 static __isl_give isl_map
*map_lex_lte(__isl_take isl_space
*dims
, int equal
)
4637 return map_lex_lte_first(dims
, dims
->n_out
, equal
);
4640 __isl_give isl_map
*isl_map_lex_lt_first(__isl_take isl_space
*dim
, unsigned n
)
4642 return map_lex_lte_first(dim
, n
, 0);
4645 __isl_give isl_map
*isl_map_lex_le_first(__isl_take isl_space
*dim
, unsigned n
)
4647 return map_lex_lte_first(dim
, n
, 1);
4650 __isl_give isl_map
*isl_map_lex_lt(__isl_take isl_space
*set_dim
)
4652 return map_lex_lte(isl_space_map_from_set(set_dim
), 0);
4655 __isl_give isl_map
*isl_map_lex_le(__isl_take isl_space
*set_dim
)
4657 return map_lex_lte(isl_space_map_from_set(set_dim
), 1);
4660 static __isl_give isl_map
*map_lex_gte_first(__isl_take isl_space
*dims
,
4661 unsigned n
, int equal
)
4663 struct isl_map
*map
;
4666 if (n
== 0 && equal
)
4667 return isl_map_universe(dims
);
4669 map
= isl_map_alloc_space(isl_space_copy(dims
), n
, ISL_MAP_DISJOINT
);
4671 for (i
= 0; i
+ 1 < n
; ++i
)
4672 map
= isl_map_add_basic_map(map
,
4673 isl_basic_map_more_at(isl_space_copy(dims
), i
));
4676 map
= isl_map_add_basic_map(map
,
4677 isl_basic_map_more_or_equal_at(dims
, n
- 1));
4679 map
= isl_map_add_basic_map(map
,
4680 isl_basic_map_more_at(dims
, n
- 1));
4682 isl_space_free(dims
);
4687 static __isl_give isl_map
*map_lex_gte(__isl_take isl_space
*dims
, int equal
)
4691 return map_lex_gte_first(dims
, dims
->n_out
, equal
);
4694 __isl_give isl_map
*isl_map_lex_gt_first(__isl_take isl_space
*dim
, unsigned n
)
4696 return map_lex_gte_first(dim
, n
, 0);
4699 __isl_give isl_map
*isl_map_lex_ge_first(__isl_take isl_space
*dim
, unsigned n
)
4701 return map_lex_gte_first(dim
, n
, 1);
4704 __isl_give isl_map
*isl_map_lex_gt(__isl_take isl_space
*set_dim
)
4706 return map_lex_gte(isl_space_map_from_set(set_dim
), 0);
4709 __isl_give isl_map
*isl_map_lex_ge(__isl_take isl_space
*set_dim
)
4711 return map_lex_gte(isl_space_map_from_set(set_dim
), 1);
4714 __isl_give isl_map
*isl_set_lex_le_set(__isl_take isl_set
*set1
,
4715 __isl_take isl_set
*set2
)
4718 map
= isl_map_lex_le(isl_set_get_space(set1
));
4719 map
= isl_map_intersect_domain(map
, set1
);
4720 map
= isl_map_intersect_range(map
, set2
);
4724 __isl_give isl_map
*isl_set_lex_lt_set(__isl_take isl_set
*set1
,
4725 __isl_take isl_set
*set2
)
4728 map
= isl_map_lex_lt(isl_set_get_space(set1
));
4729 map
= isl_map_intersect_domain(map
, set1
);
4730 map
= isl_map_intersect_range(map
, set2
);
4734 __isl_give isl_map
*isl_set_lex_ge_set(__isl_take isl_set
*set1
,
4735 __isl_take isl_set
*set2
)
4738 map
= isl_map_lex_ge(isl_set_get_space(set1
));
4739 map
= isl_map_intersect_domain(map
, set1
);
4740 map
= isl_map_intersect_range(map
, set2
);
4744 __isl_give isl_map
*isl_set_lex_gt_set(__isl_take isl_set
*set1
,
4745 __isl_take isl_set
*set2
)
4748 map
= isl_map_lex_gt(isl_set_get_space(set1
));
4749 map
= isl_map_intersect_domain(map
, set1
);
4750 map
= isl_map_intersect_range(map
, set2
);
4754 __isl_give isl_map
*isl_map_lex_le_map(__isl_take isl_map
*map1
,
4755 __isl_take isl_map
*map2
)
4758 map
= isl_map_lex_le(isl_space_range(isl_map_get_space(map1
)));
4759 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4760 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4764 __isl_give isl_map
*isl_map_lex_lt_map(__isl_take isl_map
*map1
,
4765 __isl_take isl_map
*map2
)
4768 map
= isl_map_lex_lt(isl_space_range(isl_map_get_space(map1
)));
4769 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4770 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4774 __isl_give isl_map
*isl_map_lex_ge_map(__isl_take isl_map
*map1
,
4775 __isl_take isl_map
*map2
)
4778 map
= isl_map_lex_ge(isl_space_range(isl_map_get_space(map1
)));
4779 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4780 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4784 __isl_give isl_map
*isl_map_lex_gt_map(__isl_take isl_map
*map1
,
4785 __isl_take isl_map
*map2
)
4788 map
= isl_map_lex_gt(isl_space_range(isl_map_get_space(map1
)));
4789 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
4790 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
4794 static __isl_give isl_basic_map
*basic_map_from_basic_set(
4795 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*dim
)
4797 struct isl_basic_map
*bmap
;
4799 bset
= isl_basic_set_cow(bset
);
4803 isl_assert(bset
->ctx
, isl_space_compatible_internal(bset
->dim
, dim
),
4805 isl_space_free(bset
->dim
);
4806 bmap
= bset_to_bmap(bset
);
4808 return isl_basic_map_finalize(bmap
);
4810 isl_basic_set_free(bset
);
4811 isl_space_free(dim
);
4815 __isl_give isl_basic_map
*isl_basic_map_from_basic_set(
4816 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*space
)
4818 return basic_map_from_basic_set(bset
, space
);
4821 /* For a div d = floor(f/m), add the constraint
4825 static int add_upper_div_constraint(__isl_keep isl_basic_map
*bmap
,
4826 unsigned pos
, isl_int
*div
)
4829 unsigned total
= isl_basic_map_total_dim(bmap
);
4831 i
= isl_basic_map_alloc_inequality(bmap
);
4834 isl_seq_cpy(bmap
->ineq
[i
], div
+ 1, 1 + total
);
4835 isl_int_neg(bmap
->ineq
[i
][1 + pos
], div
[0]);
4840 /* For a div d = floor(f/m), add the constraint
4842 * -(f-(m-1)) + m d >= 0
4844 static int add_lower_div_constraint(__isl_keep isl_basic_map
*bmap
,
4845 unsigned pos
, isl_int
*div
)
4848 unsigned total
= isl_basic_map_total_dim(bmap
);
4850 i
= isl_basic_map_alloc_inequality(bmap
);
4853 isl_seq_neg(bmap
->ineq
[i
], div
+ 1, 1 + total
);
4854 isl_int_set(bmap
->ineq
[i
][1 + pos
], div
[0]);
4855 isl_int_add(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], bmap
->ineq
[i
][1 + pos
]);
4856 isl_int_sub_ui(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], 1);
4861 /* For a div d = floor(f/m), add the constraints
4864 * -(f-(m-1)) + m d >= 0
4866 * Note that the second constraint is the negation of
4870 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map
*bmap
,
4871 unsigned pos
, isl_int
*div
)
4873 if (add_upper_div_constraint(bmap
, pos
, div
) < 0)
4875 if (add_lower_div_constraint(bmap
, pos
, div
) < 0)
4880 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set
*bset
,
4881 unsigned pos
, isl_int
*div
)
4883 return isl_basic_map_add_div_constraints_var(bset_to_bmap(bset
),
4887 int isl_basic_map_add_div_constraints(struct isl_basic_map
*bmap
, unsigned div
)
4889 unsigned total
= isl_basic_map_total_dim(bmap
);
4890 unsigned div_pos
= total
- bmap
->n_div
+ div
;
4892 return isl_basic_map_add_div_constraints_var(bmap
, div_pos
,
4896 /* For each known div d = floor(f/m), add the constraints
4899 * -(f-(m-1)) + m d >= 0
4901 * Remove duplicate constraints in case of some these div constraints
4902 * already appear in "bmap".
4904 __isl_give isl_basic_map
*isl_basic_map_add_known_div_constraints(
4905 __isl_take isl_basic_map
*bmap
)
4911 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4915 bmap
= add_known_div_constraints(bmap
);
4916 bmap
= isl_basic_map_remove_duplicate_constraints(bmap
, NULL
, 0);
4917 bmap
= isl_basic_map_finalize(bmap
);
4921 /* Add the div constraint of sign "sign" for div "div" of "bmap".
4923 * In particular, if this div is of the form d = floor(f/m),
4924 * then add the constraint
4928 * if sign < 0 or the constraint
4930 * -(f-(m-1)) + m d >= 0
4934 int isl_basic_map_add_div_constraint(__isl_keep isl_basic_map
*bmap
,
4935 unsigned div
, int sign
)
4943 total
= isl_basic_map_total_dim(bmap
);
4944 div_pos
= total
- bmap
->n_div
+ div
;
4947 return add_upper_div_constraint(bmap
, div_pos
, bmap
->div
[div
]);
4949 return add_lower_div_constraint(bmap
, div_pos
, bmap
->div
[div
]);
4952 int isl_basic_set_add_div_constraints(struct isl_basic_set
*bset
, unsigned div
)
4954 return isl_basic_map_add_div_constraints(bset
, div
);
4957 struct isl_basic_set
*isl_basic_map_underlying_set(
4958 struct isl_basic_map
*bmap
)
4962 if (bmap
->dim
->nparam
== 0 && bmap
->dim
->n_in
== 0 &&
4964 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_in
) &&
4965 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_out
))
4966 return bset_from_bmap(bmap
);
4967 bmap
= isl_basic_map_cow(bmap
);
4970 bmap
->dim
= isl_space_underlying(bmap
->dim
, bmap
->n_div
);
4973 bmap
->extra
-= bmap
->n_div
;
4975 bmap
= isl_basic_map_finalize(bmap
);
4976 return bset_from_bmap(bmap
);
4978 isl_basic_map_free(bmap
);
4982 __isl_give isl_basic_set
*isl_basic_set_underlying_set(
4983 __isl_take isl_basic_set
*bset
)
4985 return isl_basic_map_underlying_set(bset_to_bmap(bset
));
4988 /* Replace each element in "list" by the result of applying
4989 * isl_basic_map_underlying_set to the element.
4991 __isl_give isl_basic_set_list
*isl_basic_map_list_underlying_set(
4992 __isl_take isl_basic_map_list
*list
)
4999 n
= isl_basic_map_list_n_basic_map(list
);
5000 for (i
= 0; i
< n
; ++i
) {
5001 isl_basic_map
*bmap
;
5002 isl_basic_set
*bset
;
5004 bmap
= isl_basic_map_list_get_basic_map(list
, i
);
5005 bset
= isl_basic_set_underlying_set(bmap
);
5006 list
= isl_basic_set_list_set_basic_set(list
, i
, bset
);
5012 struct isl_basic_map
*isl_basic_map_overlying_set(
5013 struct isl_basic_set
*bset
, struct isl_basic_map
*like
)
5015 struct isl_basic_map
*bmap
;
5016 struct isl_ctx
*ctx
;
5023 isl_assert(ctx
, bset
->n_div
== 0, goto error
);
5024 isl_assert(ctx
, isl_basic_set_n_param(bset
) == 0, goto error
);
5025 isl_assert(ctx
, bset
->dim
->n_out
== isl_basic_map_total_dim(like
),
5027 if (like
->n_div
== 0) {
5028 isl_space
*space
= isl_basic_map_get_space(like
);
5029 isl_basic_map_free(like
);
5030 return isl_basic_map_reset_space(bset
, space
);
5032 bset
= isl_basic_set_cow(bset
);
5035 total
= bset
->dim
->n_out
+ bset
->extra
;
5036 bmap
= bset_to_bmap(bset
);
5037 isl_space_free(bmap
->dim
);
5038 bmap
->dim
= isl_space_copy(like
->dim
);
5041 bmap
->n_div
= like
->n_div
;
5042 bmap
->extra
+= like
->n_div
;
5046 ltotal
= total
- bmap
->extra
+ like
->extra
;
5049 bmap
->block2
= isl_blk_extend(ctx
, bmap
->block2
,
5050 bmap
->extra
* (1 + 1 + total
));
5051 if (isl_blk_is_error(bmap
->block2
))
5053 div
= isl_realloc_array(ctx
, bmap
->div
, isl_int
*, bmap
->extra
);
5057 for (i
= 0; i
< bmap
->extra
; ++i
)
5058 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + 1 + total
);
5059 for (i
= 0; i
< like
->n_div
; ++i
) {
5060 isl_seq_cpy(bmap
->div
[i
], like
->div
[i
], 1 + 1 + ltotal
);
5061 isl_seq_clr(bmap
->div
[i
]+1+1+ltotal
, total
- ltotal
);
5063 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
5065 isl_basic_map_free(like
);
5066 bmap
= isl_basic_map_simplify(bmap
);
5067 bmap
= isl_basic_map_finalize(bmap
);
5070 isl_basic_map_free(like
);
5071 isl_basic_set_free(bset
);
5075 struct isl_basic_set
*isl_basic_set_from_underlying_set(
5076 struct isl_basic_set
*bset
, struct isl_basic_set
*like
)
5078 return bset_from_bmap(isl_basic_map_overlying_set(bset
,
5079 bset_to_bmap(like
)));
5082 struct isl_set
*isl_set_from_underlying_set(
5083 struct isl_set
*set
, struct isl_basic_set
*like
)
5089 isl_assert(set
->ctx
, set
->dim
->n_out
== isl_basic_set_total_dim(like
),
5091 if (isl_space_is_equal(set
->dim
, like
->dim
) && like
->n_div
== 0) {
5092 isl_basic_set_free(like
);
5095 set
= isl_set_cow(set
);
5098 for (i
= 0; i
< set
->n
; ++i
) {
5099 set
->p
[i
] = isl_basic_set_from_underlying_set(set
->p
[i
],
5100 isl_basic_set_copy(like
));
5104 isl_space_free(set
->dim
);
5105 set
->dim
= isl_space_copy(like
->dim
);
5108 isl_basic_set_free(like
);
5111 isl_basic_set_free(like
);
5116 struct isl_set
*isl_map_underlying_set(struct isl_map
*map
)
5120 map
= isl_map_cow(map
);
5123 map
->dim
= isl_space_cow(map
->dim
);
5127 for (i
= 1; i
< map
->n
; ++i
)
5128 isl_assert(map
->ctx
, map
->p
[0]->n_div
== map
->p
[i
]->n_div
,
5130 for (i
= 0; i
< map
->n
; ++i
) {
5131 map
->p
[i
] = bset_to_bmap(
5132 isl_basic_map_underlying_set(map
->p
[i
]));
5137 map
->dim
= isl_space_underlying(map
->dim
, 0);
5139 isl_space_free(map
->dim
);
5140 map
->dim
= isl_space_copy(map
->p
[0]->dim
);
5144 return set_from_map(map
);
5150 struct isl_set
*isl_set_to_underlying_set(struct isl_set
*set
)
5152 return set_from_map(isl_map_underlying_set(set_to_map(set
)));
5155 /* Replace the space of "bmap" by "space".
5157 * If the space of "bmap" is identical to "space" (including the identifiers
5158 * of the input and output dimensions), then simply return the original input.
5160 __isl_give isl_basic_map
*isl_basic_map_reset_space(
5161 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*space
)
5167 equal
= isl_space_is_equal(bmap
->dim
, space
);
5168 if (equal
>= 0 && equal
)
5169 equal
= isl_space_match(bmap
->dim
, isl_dim_in
,
5171 if (equal
>= 0 && equal
)
5172 equal
= isl_space_match(bmap
->dim
, isl_dim_out
,
5173 space
, isl_dim_out
);
5177 isl_space_free(space
);
5180 bmap
= isl_basic_map_cow(bmap
);
5181 if (!bmap
|| !space
)
5184 isl_space_free(bmap
->dim
);
5187 bmap
= isl_basic_map_finalize(bmap
);
5191 isl_basic_map_free(bmap
);
5192 isl_space_free(space
);
5196 __isl_give isl_basic_set
*isl_basic_set_reset_space(
5197 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*dim
)
5199 return bset_from_bmap(isl_basic_map_reset_space(bset_to_bmap(bset
),
5203 __isl_give isl_map
*isl_map_reset_space(__isl_take isl_map
*map
,
5204 __isl_take isl_space
*dim
)
5208 map
= isl_map_cow(map
);
5212 for (i
= 0; i
< map
->n
; ++i
) {
5213 map
->p
[i
] = isl_basic_map_reset_space(map
->p
[i
],
5214 isl_space_copy(dim
));
5218 isl_space_free(map
->dim
);
5224 isl_space_free(dim
);
5228 __isl_give isl_set
*isl_set_reset_space(__isl_take isl_set
*set
,
5229 __isl_take isl_space
*dim
)
5231 return set_from_map(isl_map_reset_space(set_to_map(set
), dim
));
5234 /* Compute the parameter domain of the given basic set.
5236 __isl_give isl_basic_set
*isl_basic_set_params(__isl_take isl_basic_set
*bset
)
5242 is_params
= isl_basic_set_is_params(bset
);
5244 return isl_basic_set_free(bset
);
5248 n
= isl_basic_set_dim(bset
, isl_dim_set
);
5249 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
5250 space
= isl_basic_set_get_space(bset
);
5251 space
= isl_space_params(space
);
5252 bset
= isl_basic_set_reset_space(bset
, space
);
5256 /* Construct a zero-dimensional basic set with the given parameter domain.
5258 __isl_give isl_basic_set
*isl_basic_set_from_params(
5259 __isl_take isl_basic_set
*bset
)
5262 space
= isl_basic_set_get_space(bset
);
5263 space
= isl_space_set_from_params(space
);
5264 bset
= isl_basic_set_reset_space(bset
, space
);
5268 /* Compute the parameter domain of the given set.
5270 __isl_give isl_set
*isl_set_params(__isl_take isl_set
*set
)
5275 if (isl_set_is_params(set
))
5278 n
= isl_set_dim(set
, isl_dim_set
);
5279 set
= isl_set_project_out(set
, isl_dim_set
, 0, n
);
5280 space
= isl_set_get_space(set
);
5281 space
= isl_space_params(space
);
5282 set
= isl_set_reset_space(set
, space
);
5286 /* Construct a zero-dimensional set with the given parameter domain.
5288 __isl_give isl_set
*isl_set_from_params(__isl_take isl_set
*set
)
5291 space
= isl_set_get_space(set
);
5292 space
= isl_space_set_from_params(space
);
5293 set
= isl_set_reset_space(set
, space
);
5297 /* Compute the parameter domain of the given map.
5299 __isl_give isl_set
*isl_map_params(__isl_take isl_map
*map
)
5304 n
= isl_map_dim(map
, isl_dim_in
);
5305 map
= isl_map_project_out(map
, isl_dim_in
, 0, n
);
5306 n
= isl_map_dim(map
, isl_dim_out
);
5307 map
= isl_map_project_out(map
, isl_dim_out
, 0, n
);
5308 space
= isl_map_get_space(map
);
5309 space
= isl_space_params(space
);
5310 map
= isl_map_reset_space(map
, space
);
5314 struct isl_basic_set
*isl_basic_map_domain(struct isl_basic_map
*bmap
)
5321 space
= isl_space_domain(isl_basic_map_get_space(bmap
));
5323 n_out
= isl_basic_map_n_out(bmap
);
5324 bmap
= isl_basic_map_project_out(bmap
, isl_dim_out
, 0, n_out
);
5326 return isl_basic_map_reset_space(bmap
, space
);
5329 isl_bool
isl_basic_map_may_be_set(__isl_keep isl_basic_map
*bmap
)
5332 return isl_bool_error
;
5333 return isl_space_may_be_set(bmap
->dim
);
5336 /* Is this basic map actually a set?
5337 * Users should never call this function. Outside of isl,
5338 * the type should indicate whether something is a set or a map.
5340 isl_bool
isl_basic_map_is_set(__isl_keep isl_basic_map
*bmap
)
5343 return isl_bool_error
;
5344 return isl_space_is_set(bmap
->dim
);
5347 struct isl_basic_set
*isl_basic_map_range(struct isl_basic_map
*bmap
)
5351 is_set
= isl_basic_map_is_set(bmap
);
5356 return isl_basic_map_domain(isl_basic_map_reverse(bmap
));
5358 isl_basic_map_free(bmap
);
5362 __isl_give isl_basic_map
*isl_basic_map_domain_map(
5363 __isl_take isl_basic_map
*bmap
)
5367 isl_basic_map
*domain
;
5368 int nparam
, n_in
, n_out
;
5370 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5371 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5372 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5374 dim
= isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap
)));
5375 domain
= isl_basic_map_universe(dim
);
5377 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
5378 bmap
= isl_basic_map_apply_range(bmap
, domain
);
5379 bmap
= isl_basic_map_extend_constraints(bmap
, n_in
, 0);
5381 for (i
= 0; i
< n_in
; ++i
)
5382 bmap
= isl_basic_map_equate(bmap
, isl_dim_in
, i
,
5385 bmap
= isl_basic_map_gauss(bmap
, NULL
);
5386 return isl_basic_map_finalize(bmap
);
5389 __isl_give isl_basic_map
*isl_basic_map_range_map(
5390 __isl_take isl_basic_map
*bmap
)
5394 isl_basic_map
*range
;
5395 int nparam
, n_in
, n_out
;
5397 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5398 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5399 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5401 dim
= isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap
)));
5402 range
= isl_basic_map_universe(dim
);
5404 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
5405 bmap
= isl_basic_map_apply_range(bmap
, range
);
5406 bmap
= isl_basic_map_extend_constraints(bmap
, n_out
, 0);
5408 for (i
= 0; i
< n_out
; ++i
)
5409 bmap
= isl_basic_map_equate(bmap
, isl_dim_in
, n_in
+ i
,
5412 bmap
= isl_basic_map_gauss(bmap
, NULL
);
5413 return isl_basic_map_finalize(bmap
);
5416 int isl_map_may_be_set(__isl_keep isl_map
*map
)
5420 return isl_space_may_be_set(map
->dim
);
5423 /* Is this map actually a set?
5424 * Users should never call this function. Outside of isl,
5425 * the type should indicate whether something is a set or a map.
5427 isl_bool
isl_map_is_set(__isl_keep isl_map
*map
)
5430 return isl_bool_error
;
5431 return isl_space_is_set(map
->dim
);
5434 struct isl_set
*isl_map_range(struct isl_map
*map
)
5438 struct isl_set
*set
;
5440 is_set
= isl_map_is_set(map
);
5444 return set_from_map(map
);
5446 map
= isl_map_cow(map
);
5450 set
= set_from_map(map
);
5451 set
->dim
= isl_space_range(set
->dim
);
5454 for (i
= 0; i
< map
->n
; ++i
) {
5455 set
->p
[i
] = isl_basic_map_range(map
->p
[i
]);
5459 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
5460 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
5467 __isl_give isl_map
*isl_map_domain_map(__isl_take isl_map
*map
)
5471 map
= isl_map_cow(map
);
5475 map
->dim
= isl_space_domain_map(map
->dim
);
5478 for (i
= 0; i
< map
->n
; ++i
) {
5479 map
->p
[i
] = isl_basic_map_domain_map(map
->p
[i
]);
5483 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
5484 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5491 __isl_give isl_map
*isl_map_range_map(__isl_take isl_map
*map
)
5494 isl_space
*range_dim
;
5496 map
= isl_map_cow(map
);
5500 range_dim
= isl_space_range(isl_map_get_space(map
));
5501 range_dim
= isl_space_from_range(range_dim
);
5502 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
5503 map
->dim
= isl_space_join(map
->dim
, range_dim
);
5506 for (i
= 0; i
< map
->n
; ++i
) {
5507 map
->p
[i
] = isl_basic_map_range_map(map
->p
[i
]);
5511 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
5512 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5519 /* Given a wrapped map of the form A[B -> C],
5520 * return the map A[B -> C] -> B.
5522 __isl_give isl_map
*isl_set_wrapped_domain_map(__isl_take isl_set
*set
)
5529 if (!isl_set_has_tuple_id(set
))
5530 return isl_map_domain_map(isl_set_unwrap(set
));
5532 id
= isl_set_get_tuple_id(set
);
5533 map
= isl_map_domain_map(isl_set_unwrap(set
));
5534 map
= isl_map_set_tuple_id(map
, isl_dim_in
, id
);
5539 __isl_give isl_map
*isl_map_from_set(__isl_take isl_set
*set
,
5540 __isl_take isl_space
*dim
)
5543 struct isl_map
*map
= NULL
;
5545 set
= isl_set_cow(set
);
5548 isl_assert(set
->ctx
, isl_space_compatible_internal(set
->dim
, dim
),
5550 map
= set_to_map(set
);
5551 for (i
= 0; i
< set
->n
; ++i
) {
5552 map
->p
[i
] = basic_map_from_basic_set(
5553 set
->p
[i
], isl_space_copy(dim
));
5557 isl_space_free(map
->dim
);
5561 isl_space_free(dim
);
5566 __isl_give isl_basic_map
*isl_basic_map_from_domain(
5567 __isl_take isl_basic_set
*bset
)
5569 return isl_basic_map_reverse(isl_basic_map_from_range(bset
));
5572 __isl_give isl_basic_map
*isl_basic_map_from_range(
5573 __isl_take isl_basic_set
*bset
)
5576 space
= isl_basic_set_get_space(bset
);
5577 space
= isl_space_from_range(space
);
5578 bset
= isl_basic_set_reset_space(bset
, space
);
5579 return bset_to_bmap(bset
);
5582 /* Create a relation with the given set as range.
5583 * The domain of the created relation is a zero-dimensional
5584 * flat anonymous space.
5586 __isl_give isl_map
*isl_map_from_range(__isl_take isl_set
*set
)
5589 space
= isl_set_get_space(set
);
5590 space
= isl_space_from_range(space
);
5591 set
= isl_set_reset_space(set
, space
);
5592 return set_to_map(set
);
5595 /* Create a relation with the given set as domain.
5596 * The range of the created relation is a zero-dimensional
5597 * flat anonymous space.
5599 __isl_give isl_map
*isl_map_from_domain(__isl_take isl_set
*set
)
5601 return isl_map_reverse(isl_map_from_range(set
));
5604 __isl_give isl_basic_map
*isl_basic_map_from_domain_and_range(
5605 __isl_take isl_basic_set
*domain
, __isl_take isl_basic_set
*range
)
5607 return isl_basic_map_apply_range(isl_basic_map_reverse(domain
), range
);
5610 __isl_give isl_map
*isl_map_from_domain_and_range(__isl_take isl_set
*domain
,
5611 __isl_take isl_set
*range
)
5613 return isl_map_apply_range(isl_map_reverse(domain
), range
);
5616 /* Return a newly allocated isl_map with given space and flags and
5617 * room for "n" basic maps.
5618 * Make sure that all cached information is cleared.
5620 __isl_give isl_map
*isl_map_alloc_space(__isl_take isl_space
*space
, int n
,
5623 struct isl_map
*map
;
5628 isl_die(space
->ctx
, isl_error_internal
,
5629 "negative number of basic maps", goto error
);
5630 map
= isl_calloc(space
->ctx
, struct isl_map
,
5631 sizeof(struct isl_map
) +
5632 (n
- 1) * sizeof(struct isl_basic_map
*));
5636 map
->ctx
= space
->ctx
;
5637 isl_ctx_ref(map
->ctx
);
5645 isl_space_free(space
);
5649 struct isl_map
*isl_map_alloc(struct isl_ctx
*ctx
,
5650 unsigned nparam
, unsigned in
, unsigned out
, int n
,
5653 struct isl_map
*map
;
5656 dims
= isl_space_alloc(ctx
, nparam
, in
, out
);
5660 map
= isl_map_alloc_space(dims
, n
, flags
);
5664 __isl_give isl_basic_map
*isl_basic_map_empty(__isl_take isl_space
*dim
)
5666 struct isl_basic_map
*bmap
;
5667 bmap
= isl_basic_map_alloc_space(dim
, 0, 1, 0);
5668 bmap
= isl_basic_map_set_to_empty(bmap
);
5672 __isl_give isl_basic_set
*isl_basic_set_empty(__isl_take isl_space
*dim
)
5674 struct isl_basic_set
*bset
;
5675 bset
= isl_basic_set_alloc_space(dim
, 0, 1, 0);
5676 bset
= isl_basic_set_set_to_empty(bset
);
5680 __isl_give isl_basic_map
*isl_basic_map_universe(__isl_take isl_space
*dim
)
5682 struct isl_basic_map
*bmap
;
5683 bmap
= isl_basic_map_alloc_space(dim
, 0, 0, 0);
5684 bmap
= isl_basic_map_finalize(bmap
);
5688 __isl_give isl_basic_set
*isl_basic_set_universe(__isl_take isl_space
*dim
)
5690 struct isl_basic_set
*bset
;
5691 bset
= isl_basic_set_alloc_space(dim
, 0, 0, 0);
5692 bset
= isl_basic_set_finalize(bset
);
5696 __isl_give isl_basic_map
*isl_basic_map_nat_universe(__isl_take isl_space
*dim
)
5699 unsigned total
= isl_space_dim(dim
, isl_dim_all
);
5700 isl_basic_map
*bmap
;
5702 bmap
= isl_basic_map_alloc_space(dim
, 0, 0, total
);
5703 for (i
= 0; i
< total
; ++i
) {
5704 int k
= isl_basic_map_alloc_inequality(bmap
);
5707 isl_seq_clr(bmap
->ineq
[k
], 1 + total
);
5708 isl_int_set_si(bmap
->ineq
[k
][1 + i
], 1);
5712 isl_basic_map_free(bmap
);
5716 __isl_give isl_basic_set
*isl_basic_set_nat_universe(__isl_take isl_space
*dim
)
5718 return isl_basic_map_nat_universe(dim
);
5721 __isl_give isl_map
*isl_map_nat_universe(__isl_take isl_space
*dim
)
5723 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim
));
5726 __isl_give isl_set
*isl_set_nat_universe(__isl_take isl_space
*dim
)
5728 return isl_map_nat_universe(dim
);
5731 __isl_give isl_map
*isl_map_empty(__isl_take isl_space
*dim
)
5733 return isl_map_alloc_space(dim
, 0, ISL_MAP_DISJOINT
);
5736 __isl_give isl_set
*isl_set_empty(__isl_take isl_space
*dim
)
5738 return isl_set_alloc_space(dim
, 0, ISL_MAP_DISJOINT
);
5741 __isl_give isl_map
*isl_map_universe(__isl_take isl_space
*dim
)
5743 struct isl_map
*map
;
5746 map
= isl_map_alloc_space(isl_space_copy(dim
), 1, ISL_MAP_DISJOINT
);
5747 map
= isl_map_add_basic_map(map
, isl_basic_map_universe(dim
));
5751 __isl_give isl_set
*isl_set_universe(__isl_take isl_space
*dim
)
5753 struct isl_set
*set
;
5756 set
= isl_set_alloc_space(isl_space_copy(dim
), 1, ISL_MAP_DISJOINT
);
5757 set
= isl_set_add_basic_set(set
, isl_basic_set_universe(dim
));
5761 struct isl_map
*isl_map_dup(struct isl_map
*map
)
5764 struct isl_map
*dup
;
5768 dup
= isl_map_alloc_space(isl_space_copy(map
->dim
), map
->n
, map
->flags
);
5769 for (i
= 0; i
< map
->n
; ++i
)
5770 dup
= isl_map_add_basic_map(dup
, isl_basic_map_copy(map
->p
[i
]));
5774 __isl_give isl_map
*isl_map_add_basic_map(__isl_take isl_map
*map
,
5775 __isl_take isl_basic_map
*bmap
)
5779 if (isl_basic_map_plain_is_empty(bmap
)) {
5780 isl_basic_map_free(bmap
);
5783 isl_assert(map
->ctx
, isl_space_is_equal(map
->dim
, bmap
->dim
), goto error
);
5784 isl_assert(map
->ctx
, map
->n
< map
->size
, goto error
);
5785 map
->p
[map
->n
] = bmap
;
5787 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5793 isl_basic_map_free(bmap
);
5797 __isl_null isl_map
*isl_map_free(__isl_take isl_map
*map
)
5808 isl_ctx_deref(map
->ctx
);
5809 for (i
= 0; i
< map
->n
; ++i
)
5810 isl_basic_map_free(map
->p
[i
]);
5811 isl_space_free(map
->dim
);
5817 static struct isl_basic_map
*isl_basic_map_fix_pos_si(
5818 struct isl_basic_map
*bmap
, unsigned pos
, int value
)
5822 bmap
= isl_basic_map_cow(bmap
);
5823 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
5824 j
= isl_basic_map_alloc_equality(bmap
);
5827 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
5828 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
5829 isl_int_set_si(bmap
->eq
[j
][0], value
);
5830 bmap
= isl_basic_map_simplify(bmap
);
5831 return isl_basic_map_finalize(bmap
);
5833 isl_basic_map_free(bmap
);
5837 static __isl_give isl_basic_map
*isl_basic_map_fix_pos(
5838 __isl_take isl_basic_map
*bmap
, unsigned pos
, isl_int value
)
5842 bmap
= isl_basic_map_cow(bmap
);
5843 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
5844 j
= isl_basic_map_alloc_equality(bmap
);
5847 isl_seq_clr(bmap
->eq
[j
] + 1, isl_basic_map_total_dim(bmap
));
5848 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
5849 isl_int_set(bmap
->eq
[j
][0], value
);
5850 bmap
= isl_basic_map_simplify(bmap
);
5851 return isl_basic_map_finalize(bmap
);
5853 isl_basic_map_free(bmap
);
5857 struct isl_basic_map
*isl_basic_map_fix_si(struct isl_basic_map
*bmap
,
5858 enum isl_dim_type type
, unsigned pos
, int value
)
5860 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
5861 return isl_basic_map_free(bmap
);
5862 return isl_basic_map_fix_pos_si(bmap
,
5863 isl_basic_map_offset(bmap
, type
) + pos
, value
);
5866 __isl_give isl_basic_map
*isl_basic_map_fix(__isl_take isl_basic_map
*bmap
,
5867 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5869 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
5870 return isl_basic_map_free(bmap
);
5871 return isl_basic_map_fix_pos(bmap
,
5872 isl_basic_map_offset(bmap
, type
) + pos
, value
);
5875 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
5876 * to be equal to "v".
5878 __isl_give isl_basic_map
*isl_basic_map_fix_val(__isl_take isl_basic_map
*bmap
,
5879 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5883 if (!isl_val_is_int(v
))
5884 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
5885 "expecting integer value", goto error
);
5886 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
5888 pos
+= isl_basic_map_offset(bmap
, type
);
5889 bmap
= isl_basic_map_fix_pos(bmap
, pos
, v
->n
);
5893 isl_basic_map_free(bmap
);
5898 /* Fix the value of the variable at position "pos" of type "type" of "bset"
5899 * to be equal to "v".
5901 __isl_give isl_basic_set
*isl_basic_set_fix_val(__isl_take isl_basic_set
*bset
,
5902 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
5904 return isl_basic_map_fix_val(bset
, type
, pos
, v
);
5907 struct isl_basic_set
*isl_basic_set_fix_si(struct isl_basic_set
*bset
,
5908 enum isl_dim_type type
, unsigned pos
, int value
)
5910 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset
),
5914 __isl_give isl_basic_set
*isl_basic_set_fix(__isl_take isl_basic_set
*bset
,
5915 enum isl_dim_type type
, unsigned pos
, isl_int value
)
5917 return bset_from_bmap(isl_basic_map_fix(bset_to_bmap(bset
),
5921 struct isl_basic_map
*isl_basic_map_fix_input_si(struct isl_basic_map
*bmap
,
5922 unsigned input
, int value
)
5924 return isl_basic_map_fix_si(bmap
, isl_dim_in
, input
, value
);
5927 struct isl_basic_set
*isl_basic_set_fix_dim_si(struct isl_basic_set
*bset
,
5928 unsigned dim
, int value
)
5930 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset
),
5931 isl_dim_set
, dim
, value
));
5934 static int remove_if_empty(__isl_keep isl_map
*map
, int i
)
5936 int empty
= isl_basic_map_plain_is_empty(map
->p
[i
]);
5943 isl_basic_map_free(map
->p
[i
]);
5944 if (i
!= map
->n
- 1) {
5945 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5946 map
->p
[i
] = map
->p
[map
->n
- 1];
5953 /* Perform "fn" on each basic map of "map", where we may not be holding
5954 * the only reference to "map".
5955 * In particular, "fn" should be a semantics preserving operation
5956 * that we want to apply to all copies of "map". We therefore need
5957 * to be careful not to modify "map" in a way that breaks "map"
5958 * in case anything goes wrong.
5960 __isl_give isl_map
*isl_map_inline_foreach_basic_map(__isl_take isl_map
*map
,
5961 __isl_give isl_basic_map
*(*fn
)(__isl_take isl_basic_map
*bmap
))
5963 struct isl_basic_map
*bmap
;
5969 for (i
= map
->n
- 1; i
>= 0; --i
) {
5970 bmap
= isl_basic_map_copy(map
->p
[i
]);
5974 isl_basic_map_free(map
->p
[i
]);
5976 if (remove_if_empty(map
, i
) < 0)
5986 struct isl_map
*isl_map_fix_si(struct isl_map
*map
,
5987 enum isl_dim_type type
, unsigned pos
, int value
)
5991 map
= isl_map_cow(map
);
5995 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
5996 for (i
= map
->n
- 1; i
>= 0; --i
) {
5997 map
->p
[i
] = isl_basic_map_fix_si(map
->p
[i
], type
, pos
, value
);
5998 if (remove_if_empty(map
, i
) < 0)
6001 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6008 __isl_give isl_set
*isl_set_fix_si(__isl_take isl_set
*set
,
6009 enum isl_dim_type type
, unsigned pos
, int value
)
6011 return set_from_map(isl_map_fix_si(set_to_map(set
), type
, pos
, value
));
6014 __isl_give isl_map
*isl_map_fix(__isl_take isl_map
*map
,
6015 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6019 map
= isl_map_cow(map
);
6023 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
6024 for (i
= 0; i
< map
->n
; ++i
) {
6025 map
->p
[i
] = isl_basic_map_fix(map
->p
[i
], type
, pos
, value
);
6029 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6036 __isl_give isl_set
*isl_set_fix(__isl_take isl_set
*set
,
6037 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6039 return set_from_map(isl_map_fix(set_to_map(set
), type
, pos
, value
));
6042 /* Fix the value of the variable at position "pos" of type "type" of "map"
6043 * to be equal to "v".
6045 __isl_give isl_map
*isl_map_fix_val(__isl_take isl_map
*map
,
6046 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6050 map
= isl_map_cow(map
);
6054 if (!isl_val_is_int(v
))
6055 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
6056 "expecting integer value", goto error
);
6057 if (pos
>= isl_map_dim(map
, type
))
6058 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
6059 "index out of bounds", goto error
);
6060 for (i
= map
->n
- 1; i
>= 0; --i
) {
6061 map
->p
[i
] = isl_basic_map_fix_val(map
->p
[i
], type
, pos
,
6063 if (remove_if_empty(map
, i
) < 0)
6066 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6075 /* Fix the value of the variable at position "pos" of type "type" of "set"
6076 * to be equal to "v".
6078 __isl_give isl_set
*isl_set_fix_val(__isl_take isl_set
*set
,
6079 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6081 return isl_map_fix_val(set
, type
, pos
, v
);
6084 struct isl_map
*isl_map_fix_input_si(struct isl_map
*map
,
6085 unsigned input
, int value
)
6087 return isl_map_fix_si(map
, isl_dim_in
, input
, value
);
6090 struct isl_set
*isl_set_fix_dim_si(struct isl_set
*set
, unsigned dim
, int value
)
6092 return set_from_map(isl_map_fix_si(set_to_map(set
),
6093 isl_dim_set
, dim
, value
));
6096 static __isl_give isl_basic_map
*basic_map_bound_si(
6097 __isl_take isl_basic_map
*bmap
,
6098 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
6102 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6103 return isl_basic_map_free(bmap
);
6104 pos
+= isl_basic_map_offset(bmap
, type
);
6105 bmap
= isl_basic_map_cow(bmap
);
6106 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
6107 j
= isl_basic_map_alloc_inequality(bmap
);
6110 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
6112 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
6113 isl_int_set_si(bmap
->ineq
[j
][0], value
);
6115 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
6116 isl_int_set_si(bmap
->ineq
[j
][0], -value
);
6118 bmap
= isl_basic_map_simplify(bmap
);
6119 return isl_basic_map_finalize(bmap
);
6121 isl_basic_map_free(bmap
);
6125 __isl_give isl_basic_map
*isl_basic_map_lower_bound_si(
6126 __isl_take isl_basic_map
*bmap
,
6127 enum isl_dim_type type
, unsigned pos
, int value
)
6129 return basic_map_bound_si(bmap
, type
, pos
, value
, 0);
6132 /* Constrain the values of the given dimension to be no greater than "value".
6134 __isl_give isl_basic_map
*isl_basic_map_upper_bound_si(
6135 __isl_take isl_basic_map
*bmap
,
6136 enum isl_dim_type type
, unsigned pos
, int value
)
6138 return basic_map_bound_si(bmap
, type
, pos
, value
, 1);
6141 struct isl_basic_set
*isl_basic_set_lower_bound_dim(struct isl_basic_set
*bset
,
6142 unsigned dim
, isl_int value
)
6146 bset
= isl_basic_set_cow(bset
);
6147 bset
= isl_basic_set_extend_constraints(bset
, 0, 1);
6148 j
= isl_basic_set_alloc_inequality(bset
);
6151 isl_seq_clr(bset
->ineq
[j
], 1 + isl_basic_set_total_dim(bset
));
6152 isl_int_set_si(bset
->ineq
[j
][1 + isl_basic_set_n_param(bset
) + dim
], 1);
6153 isl_int_neg(bset
->ineq
[j
][0], value
);
6154 bset
= isl_basic_set_simplify(bset
);
6155 return isl_basic_set_finalize(bset
);
6157 isl_basic_set_free(bset
);
6161 static __isl_give isl_map
*map_bound_si(__isl_take isl_map
*map
,
6162 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
6166 map
= isl_map_cow(map
);
6170 isl_assert(map
->ctx
, pos
< isl_map_dim(map
, type
), goto error
);
6171 for (i
= 0; i
< map
->n
; ++i
) {
6172 map
->p
[i
] = basic_map_bound_si(map
->p
[i
],
6173 type
, pos
, value
, upper
);
6177 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6184 __isl_give isl_map
*isl_map_lower_bound_si(__isl_take isl_map
*map
,
6185 enum isl_dim_type type
, unsigned pos
, int value
)
6187 return map_bound_si(map
, type
, pos
, value
, 0);
6190 __isl_give isl_map
*isl_map_upper_bound_si(__isl_take isl_map
*map
,
6191 enum isl_dim_type type
, unsigned pos
, int value
)
6193 return map_bound_si(map
, type
, pos
, value
, 1);
6196 __isl_give isl_set
*isl_set_lower_bound_si(__isl_take isl_set
*set
,
6197 enum isl_dim_type type
, unsigned pos
, int value
)
6199 return set_from_map(isl_map_lower_bound_si(set_to_map(set
),
6203 __isl_give isl_set
*isl_set_upper_bound_si(__isl_take isl_set
*set
,
6204 enum isl_dim_type type
, unsigned pos
, int value
)
6206 return isl_map_upper_bound_si(set
, type
, pos
, value
);
6209 /* Bound the given variable of "bmap" from below (or above is "upper"
6210 * is set) to "value".
6212 static __isl_give isl_basic_map
*basic_map_bound(
6213 __isl_take isl_basic_map
*bmap
,
6214 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
6218 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6219 return isl_basic_map_free(bmap
);
6220 pos
+= isl_basic_map_offset(bmap
, type
);
6221 bmap
= isl_basic_map_cow(bmap
);
6222 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
6223 j
= isl_basic_map_alloc_inequality(bmap
);
6226 isl_seq_clr(bmap
->ineq
[j
], 1 + isl_basic_map_total_dim(bmap
));
6228 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
6229 isl_int_set(bmap
->ineq
[j
][0], value
);
6231 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
6232 isl_int_neg(bmap
->ineq
[j
][0], value
);
6234 bmap
= isl_basic_map_simplify(bmap
);
6235 return isl_basic_map_finalize(bmap
);
6237 isl_basic_map_free(bmap
);
6241 /* Bound the given variable of "map" from below (or above is "upper"
6242 * is set) to "value".
6244 static __isl_give isl_map
*map_bound(__isl_take isl_map
*map
,
6245 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
6249 map
= isl_map_cow(map
);
6253 if (pos
>= isl_map_dim(map
, type
))
6254 isl_die(map
->ctx
, isl_error_invalid
,
6255 "index out of bounds", goto error
);
6256 for (i
= map
->n
- 1; i
>= 0; --i
) {
6257 map
->p
[i
] = basic_map_bound(map
->p
[i
], type
, pos
, value
, upper
);
6258 if (remove_if_empty(map
, i
) < 0)
6261 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6268 __isl_give isl_map
*isl_map_lower_bound(__isl_take isl_map
*map
,
6269 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6271 return map_bound(map
, type
, pos
, value
, 0);
6274 __isl_give isl_map
*isl_map_upper_bound(__isl_take isl_map
*map
,
6275 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6277 return map_bound(map
, type
, pos
, value
, 1);
6280 __isl_give isl_set
*isl_set_lower_bound(__isl_take isl_set
*set
,
6281 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6283 return isl_map_lower_bound(set
, type
, pos
, value
);
6286 __isl_give isl_set
*isl_set_upper_bound(__isl_take isl_set
*set
,
6287 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6289 return isl_map_upper_bound(set
, type
, pos
, value
);
6292 /* Force the values of the variable at position "pos" of type "type" of "set"
6293 * to be no smaller than "value".
6295 __isl_give isl_set
*isl_set_lower_bound_val(__isl_take isl_set
*set
,
6296 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
6300 if (!isl_val_is_int(value
))
6301 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
6302 "expecting integer value", goto error
);
6303 set
= isl_set_lower_bound(set
, type
, pos
, value
->n
);
6304 isl_val_free(value
);
6307 isl_val_free(value
);
6312 /* Force the values of the variable at position "pos" of type "type" of "set"
6313 * to be no greater than "value".
6315 __isl_give isl_set
*isl_set_upper_bound_val(__isl_take isl_set
*set
,
6316 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
6320 if (!isl_val_is_int(value
))
6321 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
6322 "expecting integer value", goto error
);
6323 set
= isl_set_upper_bound(set
, type
, pos
, value
->n
);
6324 isl_val_free(value
);
6327 isl_val_free(value
);
6332 struct isl_set
*isl_set_lower_bound_dim(struct isl_set
*set
, unsigned dim
,
6337 set
= isl_set_cow(set
);
6341 isl_assert(set
->ctx
, dim
< isl_set_n_dim(set
), goto error
);
6342 for (i
= 0; i
< set
->n
; ++i
) {
6343 set
->p
[i
] = isl_basic_set_lower_bound_dim(set
->p
[i
], dim
, value
);
6353 struct isl_map
*isl_map_reverse(struct isl_map
*map
)
6357 map
= isl_map_cow(map
);
6361 map
->dim
= isl_space_reverse(map
->dim
);
6364 for (i
= 0; i
< map
->n
; ++i
) {
6365 map
->p
[i
] = isl_basic_map_reverse(map
->p
[i
]);
6369 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
6377 #define TYPE isl_pw_multi_aff
6379 #define SUFFIX _pw_multi_aff
6381 #define EMPTY isl_pw_multi_aff_empty
6383 #define ADD isl_pw_multi_aff_union_add
6384 #include "isl_map_lexopt_templ.c"
6386 /* Given a map "map", compute the lexicographically minimal
6387 * (or maximal) image element for each domain element in dom,
6388 * in the form of an isl_pw_multi_aff.
6389 * If "empty" is not NULL, then set *empty to those elements in dom that
6390 * do not have an image element.
6391 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6392 * should be computed over the domain of "map". "empty" is also NULL
6395 * We first compute the lexicographically minimal or maximal element
6396 * in the first basic map. This results in a partial solution "res"
6397 * and a subset "todo" of dom that still need to be handled.
6398 * We then consider each of the remaining maps in "map" and successively
6399 * update both "res" and "todo".
6400 * If "empty" is NULL, then the todo sets are not needed and therefore
6401 * also not computed.
6403 static __isl_give isl_pw_multi_aff
*isl_map_partial_lexopt_aligned_pw_multi_aff(
6404 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6405 __isl_give isl_set
**empty
, unsigned flags
)
6409 isl_pw_multi_aff
*res
;
6412 full
= ISL_FL_ISSET(flags
, ISL_OPT_FULL
);
6413 if (!map
|| (!full
&& !dom
))
6416 if (isl_map_plain_is_empty(map
)) {
6421 return isl_pw_multi_aff_from_map(map
);
6424 res
= basic_map_partial_lexopt_pw_multi_aff(
6425 isl_basic_map_copy(map
->p
[0]),
6426 isl_set_copy(dom
), empty
, flags
);
6430 for (i
= 1; i
< map
->n
; ++i
) {
6431 isl_pw_multi_aff
*res_i
;
6433 res_i
= basic_map_partial_lexopt_pw_multi_aff(
6434 isl_basic_map_copy(map
->p
[i
]),
6435 isl_set_copy(dom
), empty
, flags
);
6437 if (ISL_FL_ISSET(flags
, ISL_OPT_MAX
))
6438 res
= isl_pw_multi_aff_union_lexmax(res
, res_i
);
6440 res
= isl_pw_multi_aff_union_lexmin(res
, res_i
);
6443 todo
= isl_set_intersect(todo
, *empty
);
6462 #define TYPE isl_map
6466 #define EMPTY isl_map_empty
6468 #define ADD isl_map_union_disjoint
6469 #include "isl_map_lexopt_templ.c"
6471 /* Given a map "map", compute the lexicographically minimal
6472 * (or maximal) image element for each domain element in "dom",
6473 * in the form of an isl_map.
6474 * If "empty" is not NULL, then set *empty to those elements in "dom" that
6475 * do not have an image element.
6476 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6477 * should be computed over the domain of "map". "empty" is also NULL
6480 * If the input consists of more than one disjunct, then first
6481 * compute the desired result in the form of an isl_pw_multi_aff and
6482 * then convert that into an isl_map.
6484 * This function used to have an explicit implementation in terms
6485 * of isl_maps, but it would continually intersect the domains of
6486 * partial results with the complement of the domain of the next
6487 * partial solution, potentially leading to an explosion in the number
6488 * of disjuncts if there are several disjuncts in the input.
6489 * An even earlier implementation of this function would look for
6490 * better results in the domain of the partial result and for extra
6491 * results in the complement of this domain, which would lead to
6492 * even more splintering.
6494 static __isl_give isl_map
*isl_map_partial_lexopt_aligned(
6495 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6496 __isl_give isl_set
**empty
, unsigned flags
)
6499 struct isl_map
*res
;
6500 isl_pw_multi_aff
*pma
;
6502 full
= ISL_FL_ISSET(flags
, ISL_OPT_FULL
);
6503 if (!map
|| (!full
&& !dom
))
6506 if (isl_map_plain_is_empty(map
)) {
6515 res
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[0]),
6521 pma
= isl_map_partial_lexopt_aligned_pw_multi_aff(map
, dom
, empty
,
6523 return isl_map_from_pw_multi_aff(pma
);
6532 __isl_give isl_map
*isl_map_partial_lexmax(
6533 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6534 __isl_give isl_set
**empty
)
6536 return isl_map_partial_lexopt(map
, dom
, empty
, ISL_OPT_MAX
);
6539 __isl_give isl_map
*isl_map_partial_lexmin(
6540 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
6541 __isl_give isl_set
**empty
)
6543 return isl_map_partial_lexopt(map
, dom
, empty
, 0);
6546 __isl_give isl_set
*isl_set_partial_lexmin(
6547 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
6548 __isl_give isl_set
**empty
)
6550 return set_from_map(isl_map_partial_lexmin(set_to_map(set
),
6554 __isl_give isl_set
*isl_set_partial_lexmax(
6555 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
6556 __isl_give isl_set
**empty
)
6558 return set_from_map(isl_map_partial_lexmax(set_to_map(set
),
6562 /* Compute the lexicographic minimum (or maximum if "flags" includes
6563 * ISL_OPT_MAX) of "bset" over its parametric domain.
6565 __isl_give isl_set
*isl_basic_set_lexopt(__isl_take isl_basic_set
*bset
,
6568 return isl_basic_map_lexopt(bset
, flags
);
6571 __isl_give isl_map
*isl_basic_map_lexmax(__isl_take isl_basic_map
*bmap
)
6573 return isl_basic_map_lexopt(bmap
, ISL_OPT_MAX
);
6576 __isl_give isl_set
*isl_basic_set_lexmin(__isl_take isl_basic_set
*bset
)
6578 return set_from_map(isl_basic_map_lexmin(bset_to_bmap(bset
)));
6581 __isl_give isl_set
*isl_basic_set_lexmax(__isl_take isl_basic_set
*bset
)
6583 return set_from_map(isl_basic_map_lexmax(bset_to_bmap(bset
)));
6586 /* Compute the lexicographic minimum of "bset" over its parametric domain
6587 * for the purpose of quantifier elimination.
6588 * That is, find an explicit representation for all the existentially
6589 * quantified variables in "bset" by computing their lexicographic
6592 static __isl_give isl_set
*isl_basic_set_lexmin_compute_divs(
6593 __isl_take isl_basic_set
*bset
)
6595 return isl_basic_set_lexopt(bset
, ISL_OPT_QE
);
6598 /* Extract the first and only affine expression from list
6599 * and then add it to *pwaff with the given dom.
6600 * This domain is known to be disjoint from other domains
6601 * because of the way isl_basic_map_foreach_lexmax works.
6603 static int update_dim_opt(__isl_take isl_basic_set
*dom
,
6604 __isl_take isl_aff_list
*list
, void *user
)
6606 isl_ctx
*ctx
= isl_basic_set_get_ctx(dom
);
6608 isl_pw_aff
**pwaff
= user
;
6609 isl_pw_aff
*pwaff_i
;
6613 if (isl_aff_list_n_aff(list
) != 1)
6614 isl_die(ctx
, isl_error_internal
,
6615 "expecting single element list", goto error
);
6617 aff
= isl_aff_list_get_aff(list
, 0);
6618 pwaff_i
= isl_pw_aff_alloc(isl_set_from_basic_set(dom
), aff
);
6620 *pwaff
= isl_pw_aff_add_disjoint(*pwaff
, pwaff_i
);
6622 isl_aff_list_free(list
);
6626 isl_basic_set_free(dom
);
6627 isl_aff_list_free(list
);
6631 /* Given a basic map with one output dimension, compute the minimum or
6632 * maximum of that dimension as an isl_pw_aff.
6634 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
6635 * call update_dim_opt on each leaf of the result.
6637 static __isl_give isl_pw_aff
*basic_map_dim_opt(__isl_keep isl_basic_map
*bmap
,
6640 isl_space
*dim
= isl_basic_map_get_space(bmap
);
6644 dim
= isl_space_from_domain(isl_space_domain(dim
));
6645 dim
= isl_space_add_dims(dim
, isl_dim_out
, 1);
6646 pwaff
= isl_pw_aff_empty(dim
);
6648 r
= isl_basic_map_foreach_lexopt(bmap
, max
, &update_dim_opt
, &pwaff
);
6650 return isl_pw_aff_free(pwaff
);
6655 /* Compute the minimum or maximum of the given output dimension
6656 * as a function of the parameters and the input dimensions,
6657 * but independently of the other output dimensions.
6659 * We first project out the other output dimension and then compute
6660 * the "lexicographic" maximum in each basic map, combining the results
6661 * using isl_pw_aff_union_max.
6663 static __isl_give isl_pw_aff
*map_dim_opt(__isl_take isl_map
*map
, int pos
,
6670 n_out
= isl_map_dim(map
, isl_dim_out
);
6671 map
= isl_map_project_out(map
, isl_dim_out
, pos
+ 1, n_out
- (pos
+ 1));
6672 map
= isl_map_project_out(map
, isl_dim_out
, 0, pos
);
6677 isl_space
*dim
= isl_map_get_space(map
);
6679 return isl_pw_aff_empty(dim
);
6682 pwaff
= basic_map_dim_opt(map
->p
[0], max
);
6683 for (i
= 1; i
< map
->n
; ++i
) {
6684 isl_pw_aff
*pwaff_i
;
6686 pwaff_i
= basic_map_dim_opt(map
->p
[i
], max
);
6687 pwaff
= isl_pw_aff_union_opt(pwaff
, pwaff_i
, max
);
6695 /* Compute the minimum of the given output dimension as a function of the
6696 * parameters and input dimensions, but independently of
6697 * the other output dimensions.
6699 __isl_give isl_pw_aff
*isl_map_dim_min(__isl_take isl_map
*map
, int pos
)
6701 return map_dim_opt(map
, pos
, 0);
6704 /* Compute the maximum of the given output dimension as a function of the
6705 * parameters and input dimensions, but independently of
6706 * the other output dimensions.
6708 __isl_give isl_pw_aff
*isl_map_dim_max(__isl_take isl_map
*map
, int pos
)
6710 return map_dim_opt(map
, pos
, 1);
6713 /* Compute the minimum or maximum of the given set dimension
6714 * as a function of the parameters,
6715 * but independently of the other set dimensions.
6717 static __isl_give isl_pw_aff
*set_dim_opt(__isl_take isl_set
*set
, int pos
,
6720 return map_dim_opt(set
, pos
, max
);
6723 /* Compute the maximum of the given set dimension as a function of the
6724 * parameters, but independently of the other set dimensions.
6726 __isl_give isl_pw_aff
*isl_set_dim_max(__isl_take isl_set
*set
, int pos
)
6728 return set_dim_opt(set
, pos
, 1);
6731 /* Compute the minimum of the given set dimension as a function of the
6732 * parameters, but independently of the other set dimensions.
6734 __isl_give isl_pw_aff
*isl_set_dim_min(__isl_take isl_set
*set
, int pos
)
6736 return set_dim_opt(set
, pos
, 0);
6739 /* Apply a preimage specified by "mat" on the parameters of "bset".
6740 * bset is assumed to have only parameters and divs.
6742 static struct isl_basic_set
*basic_set_parameter_preimage(
6743 struct isl_basic_set
*bset
, struct isl_mat
*mat
)
6750 bset
->dim
= isl_space_cow(bset
->dim
);
6754 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
6756 isl_assert(bset
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
6758 bset
->dim
->nparam
= 0;
6759 bset
->dim
->n_out
= nparam
;
6760 bset
= isl_basic_set_preimage(bset
, mat
);
6762 bset
->dim
->nparam
= bset
->dim
->n_out
;
6763 bset
->dim
->n_out
= 0;
6768 isl_basic_set_free(bset
);
6772 /* Apply a preimage specified by "mat" on the parameters of "set".
6773 * set is assumed to have only parameters and divs.
6775 static __isl_give isl_set
*set_parameter_preimage(__isl_take isl_set
*set
,
6776 __isl_take isl_mat
*mat
)
6784 nparam
= isl_set_dim(set
, isl_dim_param
);
6786 if (mat
->n_row
!= 1 + nparam
)
6787 isl_die(isl_set_get_ctx(set
), isl_error_internal
,
6788 "unexpected number of rows", goto error
);
6790 space
= isl_set_get_space(set
);
6791 space
= isl_space_move_dims(space
, isl_dim_set
, 0,
6792 isl_dim_param
, 0, nparam
);
6793 set
= isl_set_reset_space(set
, space
);
6794 set
= isl_set_preimage(set
, mat
);
6795 nparam
= isl_set_dim(set
, isl_dim_out
);
6796 space
= isl_set_get_space(set
);
6797 space
= isl_space_move_dims(space
, isl_dim_param
, 0,
6798 isl_dim_out
, 0, nparam
);
6799 set
= isl_set_reset_space(set
, space
);
6807 /* Intersect the basic set "bset" with the affine space specified by the
6808 * equalities in "eq".
6810 static struct isl_basic_set
*basic_set_append_equalities(
6811 struct isl_basic_set
*bset
, struct isl_mat
*eq
)
6819 bset
= isl_basic_set_extend_space(bset
, isl_space_copy(bset
->dim
), 0,
6824 len
= 1 + isl_space_dim(bset
->dim
, isl_dim_all
) + bset
->extra
;
6825 for (i
= 0; i
< eq
->n_row
; ++i
) {
6826 k
= isl_basic_set_alloc_equality(bset
);
6829 isl_seq_cpy(bset
->eq
[k
], eq
->row
[i
], eq
->n_col
);
6830 isl_seq_clr(bset
->eq
[k
] + eq
->n_col
, len
- eq
->n_col
);
6834 bset
= isl_basic_set_gauss(bset
, NULL
);
6835 bset
= isl_basic_set_finalize(bset
);
6840 isl_basic_set_free(bset
);
6844 /* Intersect the set "set" with the affine space specified by the
6845 * equalities in "eq".
6847 static struct isl_set
*set_append_equalities(struct isl_set
*set
,
6855 for (i
= 0; i
< set
->n
; ++i
) {
6856 set
->p
[i
] = basic_set_append_equalities(set
->p
[i
],
6869 /* Given a basic set "bset" that only involves parameters and existentially
6870 * quantified variables, return the index of the first equality
6871 * that only involves parameters. If there is no such equality then
6872 * return bset->n_eq.
6874 * This function assumes that isl_basic_set_gauss has been called on "bset".
6876 static int first_parameter_equality(__isl_keep isl_basic_set
*bset
)
6879 unsigned nparam
, n_div
;
6884 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
6885 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
6887 for (i
= 0, j
= n_div
- 1; i
< bset
->n_eq
&& j
>= 0; --j
) {
6888 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ j
]))
6895 /* Compute an explicit representation for the existentially quantified
6896 * variables in "bset" by computing the "minimal value" of the set
6897 * variables. Since there are no set variables, the computation of
6898 * the minimal value essentially computes an explicit representation
6899 * of the non-empty part(s) of "bset".
6901 * The input only involves parameters and existentially quantified variables.
6902 * All equalities among parameters have been removed.
6904 * Since the existentially quantified variables in the result are in general
6905 * going to be different from those in the input, we first replace
6906 * them by the minimal number of variables based on their equalities.
6907 * This should simplify the parametric integer programming.
6909 static __isl_give isl_set
*base_compute_divs(__isl_take isl_basic_set
*bset
)
6911 isl_morph
*morph1
, *morph2
;
6917 if (bset
->n_eq
== 0)
6918 return isl_basic_set_lexmin_compute_divs(bset
);
6920 morph1
= isl_basic_set_parameter_compression(bset
);
6921 bset
= isl_morph_basic_set(isl_morph_copy(morph1
), bset
);
6922 bset
= isl_basic_set_lift(bset
);
6923 morph2
= isl_basic_set_variable_compression(bset
, isl_dim_set
);
6924 bset
= isl_morph_basic_set(morph2
, bset
);
6925 n
= isl_basic_set_dim(bset
, isl_dim_set
);
6926 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
6928 set
= isl_basic_set_lexmin_compute_divs(bset
);
6930 set
= isl_morph_set(isl_morph_inverse(morph1
), set
);
6935 /* Project the given basic set onto its parameter domain, possibly introducing
6936 * new, explicit, existential variables in the constraints.
6937 * The input has parameters and (possibly implicit) existential variables.
6938 * The output has the same parameters, but only
6939 * explicit existentially quantified variables.
6941 * The actual projection is performed by pip, but pip doesn't seem
6942 * to like equalities very much, so we first remove the equalities
6943 * among the parameters by performing a variable compression on
6944 * the parameters. Afterward, an inverse transformation is performed
6945 * and the equalities among the parameters are inserted back in.
6947 * The variable compression on the parameters may uncover additional
6948 * equalities that were only implicit before. We therefore check
6949 * if there are any new parameter equalities in the result and
6950 * if so recurse. The removal of parameter equalities is required
6951 * for the parameter compression performed by base_compute_divs.
6953 static struct isl_set
*parameter_compute_divs(struct isl_basic_set
*bset
)
6957 struct isl_mat
*T
, *T2
;
6958 struct isl_set
*set
;
6961 bset
= isl_basic_set_cow(bset
);
6965 if (bset
->n_eq
== 0)
6966 return base_compute_divs(bset
);
6968 bset
= isl_basic_set_gauss(bset
, NULL
);
6971 if (isl_basic_set_plain_is_empty(bset
))
6972 return isl_set_from_basic_set(bset
);
6974 i
= first_parameter_equality(bset
);
6975 if (i
== bset
->n_eq
)
6976 return base_compute_divs(bset
);
6978 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
6979 eq
= isl_mat_sub_alloc6(bset
->ctx
, bset
->eq
, i
, bset
->n_eq
- i
,
6981 eq
= isl_mat_cow(eq
);
6982 T
= isl_mat_variable_compression(isl_mat_copy(eq
), &T2
);
6983 if (T
&& T
->n_col
== 0) {
6987 bset
= isl_basic_set_set_to_empty(bset
);
6988 return isl_set_from_basic_set(bset
);
6990 bset
= basic_set_parameter_preimage(bset
, T
);
6992 i
= first_parameter_equality(bset
);
6995 else if (i
== bset
->n_eq
)
6996 set
= base_compute_divs(bset
);
6998 set
= parameter_compute_divs(bset
);
6999 set
= set_parameter_preimage(set
, T2
);
7000 set
= set_append_equalities(set
, eq
);
7004 /* Insert the divs from "ls" before those of "bmap".
7006 * The number of columns is not changed, which means that the last
7007 * dimensions of "bmap" are being reintepreted as the divs from "ls".
7008 * The caller is responsible for removing the same number of dimensions
7009 * from the space of "bmap".
7011 static __isl_give isl_basic_map
*insert_divs_from_local_space(
7012 __isl_take isl_basic_map
*bmap
, __isl_keep isl_local_space
*ls
)
7018 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
7022 old_n_div
= bmap
->n_div
;
7023 bmap
= insert_div_rows(bmap
, n_div
);
7027 for (i
= 0; i
< n_div
; ++i
) {
7028 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], ls
->div
->n_col
);
7029 isl_seq_clr(bmap
->div
[i
] + ls
->div
->n_col
, old_n_div
);
7035 /* Replace the space of "bmap" by the space and divs of "ls".
7037 * If "ls" has any divs, then we simplify the result since we may
7038 * have discovered some additional equalities that could simplify
7039 * the div expressions.
7041 static __isl_give isl_basic_map
*basic_replace_space_by_local_space(
7042 __isl_take isl_basic_map
*bmap
, __isl_take isl_local_space
*ls
)
7046 bmap
= isl_basic_map_cow(bmap
);
7050 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
7051 bmap
= insert_divs_from_local_space(bmap
, ls
);
7055 isl_space_free(bmap
->dim
);
7056 bmap
->dim
= isl_local_space_get_space(ls
);
7060 isl_local_space_free(ls
);
7062 bmap
= isl_basic_map_simplify(bmap
);
7063 bmap
= isl_basic_map_finalize(bmap
);
7066 isl_basic_map_free(bmap
);
7067 isl_local_space_free(ls
);
7071 /* Replace the space of "map" by the space and divs of "ls".
7073 static __isl_give isl_map
*replace_space_by_local_space(__isl_take isl_map
*map
,
7074 __isl_take isl_local_space
*ls
)
7078 map
= isl_map_cow(map
);
7082 for (i
= 0; i
< map
->n
; ++i
) {
7083 map
->p
[i
] = basic_replace_space_by_local_space(map
->p
[i
],
7084 isl_local_space_copy(ls
));
7088 isl_space_free(map
->dim
);
7089 map
->dim
= isl_local_space_get_space(ls
);
7093 isl_local_space_free(ls
);
7096 isl_local_space_free(ls
);
7101 /* Compute an explicit representation for the existentially
7102 * quantified variables for which do not know any explicit representation yet.
7104 * We first sort the existentially quantified variables so that the
7105 * existentially quantified variables for which we already have an explicit
7106 * representation are placed before those for which we do not.
7107 * The input dimensions, the output dimensions and the existentially
7108 * quantified variables for which we already have an explicit
7109 * representation are then turned into parameters.
7110 * compute_divs returns a map with the same parameters and
7111 * no input or output dimensions and the dimension specification
7112 * is reset to that of the input, including the existentially quantified
7113 * variables for which we already had an explicit representation.
7115 static struct isl_map
*compute_divs(struct isl_basic_map
*bmap
)
7117 struct isl_basic_set
*bset
;
7118 struct isl_set
*set
;
7119 struct isl_map
*map
;
7121 isl_local_space
*ls
;
7128 bmap
= isl_basic_map_sort_divs(bmap
);
7129 bmap
= isl_basic_map_cow(bmap
);
7133 n_known
= isl_basic_map_first_unknown_div(bmap
);
7135 return isl_map_from_basic_map(isl_basic_map_free(bmap
));
7137 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
7138 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
7139 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
7140 dim
= isl_space_set_alloc(bmap
->ctx
,
7141 nparam
+ n_in
+ n_out
+ n_known
, 0);
7145 ls
= isl_basic_map_get_local_space(bmap
);
7146 ls
= isl_local_space_drop_dims(ls
, isl_dim_div
,
7147 n_known
, bmap
->n_div
- n_known
);
7149 for (i
= n_known
; i
< bmap
->n_div
; ++i
)
7150 swap_div(bmap
, i
- n_known
, i
);
7151 bmap
->n_div
-= n_known
;
7152 bmap
->extra
-= n_known
;
7154 bmap
= isl_basic_map_reset_space(bmap
, dim
);
7155 bset
= bset_from_bmap(bmap
);
7157 set
= parameter_compute_divs(bset
);
7158 map
= set_to_map(set
);
7159 map
= replace_space_by_local_space(map
, ls
);
7163 isl_basic_map_free(bmap
);
7167 /* Remove the explicit representation of local variable "div",
7170 __isl_give isl_basic_map
*isl_basic_map_mark_div_unknown(
7171 __isl_take isl_basic_map
*bmap
, int div
)
7175 unknown
= isl_basic_map_div_is_marked_unknown(bmap
, div
);
7177 return isl_basic_map_free(bmap
);
7181 bmap
= isl_basic_map_cow(bmap
);
7184 isl_int_set_si(bmap
->div
[div
][0], 0);
7188 /* Is local variable "div" of "bmap" marked as not having an explicit
7190 * Note that even if "div" is not marked in this way and therefore
7191 * has an explicit representation, this representation may still
7192 * depend (indirectly) on other local variables that do not
7193 * have an explicit representation.
7195 isl_bool
isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map
*bmap
,
7198 if (isl_basic_map_check_range(bmap
, isl_dim_div
, div
, 1) < 0)
7199 return isl_bool_error
;
7200 return isl_int_is_zero(bmap
->div
[div
][0]);
7203 /* Return the position of the first local variable that does not
7204 * have an explicit representation.
7205 * Return the total number of local variables if they all have
7206 * an explicit representation.
7207 * Return -1 on error.
7209 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map
*bmap
)
7216 for (i
= 0; i
< bmap
->n_div
; ++i
) {
7217 if (!isl_basic_map_div_is_known(bmap
, i
))
7223 /* Return the position of the first local variable that does not
7224 * have an explicit representation.
7225 * Return the total number of local variables if they all have
7226 * an explicit representation.
7227 * Return -1 on error.
7229 int isl_basic_set_first_unknown_div(__isl_keep isl_basic_set
*bset
)
7231 return isl_basic_map_first_unknown_div(bset
);
7234 /* Does "bmap" have an explicit representation for all local variables?
7236 isl_bool
isl_basic_map_divs_known(__isl_keep isl_basic_map
*bmap
)
7240 n
= isl_basic_map_dim(bmap
, isl_dim_div
);
7241 first
= isl_basic_map_first_unknown_div(bmap
);
7243 return isl_bool_error
;
7247 /* Do all basic maps in "map" have an explicit representation
7248 * for all local variables?
7250 isl_bool
isl_map_divs_known(__isl_keep isl_map
*map
)
7255 return isl_bool_error
;
7257 for (i
= 0; i
< map
->n
; ++i
) {
7258 int known
= isl_basic_map_divs_known(map
->p
[i
]);
7263 return isl_bool_true
;
7266 /* If bmap contains any unknown divs, then compute explicit
7267 * expressions for them. However, this computation may be
7268 * quite expensive, so first try to remove divs that aren't
7271 struct isl_map
*isl_basic_map_compute_divs(struct isl_basic_map
*bmap
)
7274 struct isl_map
*map
;
7276 known
= isl_basic_map_divs_known(bmap
);
7280 return isl_map_from_basic_map(bmap
);
7282 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
7284 known
= isl_basic_map_divs_known(bmap
);
7288 return isl_map_from_basic_map(bmap
);
7290 map
= compute_divs(bmap
);
7293 isl_basic_map_free(bmap
);
7297 struct isl_map
*isl_map_compute_divs(struct isl_map
*map
)
7301 struct isl_map
*res
;
7308 known
= isl_map_divs_known(map
);
7316 res
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[0]));
7317 for (i
= 1 ; i
< map
->n
; ++i
) {
7319 r2
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[i
]));
7320 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
7321 res
= isl_map_union_disjoint(res
, r2
);
7323 res
= isl_map_union(res
, r2
);
7330 struct isl_set
*isl_basic_set_compute_divs(struct isl_basic_set
*bset
)
7332 return set_from_map(isl_basic_map_compute_divs(bset_to_bmap(bset
)));
7335 struct isl_set
*isl_set_compute_divs(struct isl_set
*set
)
7337 return set_from_map(isl_map_compute_divs(set_to_map(set
)));
7340 struct isl_set
*isl_map_domain(struct isl_map
*map
)
7343 struct isl_set
*set
;
7348 map
= isl_map_cow(map
);
7352 set
= set_from_map(map
);
7353 set
->dim
= isl_space_domain(set
->dim
);
7356 for (i
= 0; i
< map
->n
; ++i
) {
7357 set
->p
[i
] = isl_basic_map_domain(map
->p
[i
]);
7361 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
7362 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
7369 /* Return the union of "map1" and "map2", where we assume for now that
7370 * "map1" and "map2" are disjoint. Note that the basic maps inside
7371 * "map1" or "map2" may not be disjoint from each other.
7372 * Also note that this function is also called from isl_map_union,
7373 * which takes care of handling the situation where "map1" and "map2"
7374 * may not be disjoint.
7376 * If one of the inputs is empty, we can simply return the other input.
7377 * Similarly, if one of the inputs is universal, then it is equal to the union.
7379 static __isl_give isl_map
*map_union_disjoint(__isl_take isl_map
*map1
,
7380 __isl_take isl_map
*map2
)
7384 struct isl_map
*map
= NULL
;
7390 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
7391 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
7392 "spaces don't match", goto error
);
7403 is_universe
= isl_map_plain_is_universe(map1
);
7404 if (is_universe
< 0)
7411 is_universe
= isl_map_plain_is_universe(map2
);
7412 if (is_universe
< 0)
7419 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
7420 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
7421 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
7423 map
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
7424 map1
->n
+ map2
->n
, flags
);
7427 for (i
= 0; i
< map1
->n
; ++i
) {
7428 map
= isl_map_add_basic_map(map
,
7429 isl_basic_map_copy(map1
->p
[i
]));
7433 for (i
= 0; i
< map2
->n
; ++i
) {
7434 map
= isl_map_add_basic_map(map
,
7435 isl_basic_map_copy(map2
->p
[i
]));
7449 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7450 * guaranteed to be disjoint by the caller.
7452 * Note that this functions is called from within isl_map_make_disjoint,
7453 * so we have to be careful not to touch the constraints of the inputs
7456 __isl_give isl_map
*isl_map_union_disjoint(__isl_take isl_map
*map1
,
7457 __isl_take isl_map
*map2
)
7459 return isl_map_align_params_map_map_and(map1
, map2
, &map_union_disjoint
);
7462 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7463 * not be disjoint. The parameters are assumed to have been aligned.
7465 * We currently simply call map_union_disjoint, the internal operation
7466 * of which does not really depend on the inputs being disjoint.
7467 * If the result contains more than one basic map, then we clear
7468 * the disjoint flag since the result may contain basic maps from
7469 * both inputs and these are not guaranteed to be disjoint.
7471 * As a special case, if "map1" and "map2" are obviously equal,
7472 * then we simply return "map1".
7474 static __isl_give isl_map
*map_union_aligned(__isl_take isl_map
*map1
,
7475 __isl_take isl_map
*map2
)
7482 equal
= isl_map_plain_is_equal(map1
, map2
);
7490 map1
= map_union_disjoint(map1
, map2
);
7494 ISL_F_CLR(map1
, ISL_MAP_DISJOINT
);
7502 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7505 __isl_give isl_map
*isl_map_union(__isl_take isl_map
*map1
,
7506 __isl_take isl_map
*map2
)
7508 return isl_map_align_params_map_map_and(map1
, map2
, &map_union_aligned
);
7511 struct isl_set
*isl_set_union_disjoint(
7512 struct isl_set
*set1
, struct isl_set
*set2
)
7514 return set_from_map(isl_map_union_disjoint(set_to_map(set1
),
7518 struct isl_set
*isl_set_union(struct isl_set
*set1
, struct isl_set
*set2
)
7520 return set_from_map(isl_map_union(set_to_map(set1
), set_to_map(set2
)));
7523 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7526 * "map" and "set" are assumed to be compatible and non-NULL.
7528 static __isl_give isl_map
*map_intersect_set(__isl_take isl_map
*map
,
7529 __isl_take isl_set
*set
,
7530 __isl_give isl_basic_map
*fn(__isl_take isl_basic_map
*bmap
,
7531 __isl_take isl_basic_set
*bset
))
7534 struct isl_map
*result
;
7537 if (isl_set_plain_is_universe(set
)) {
7542 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
) &&
7543 ISL_F_ISSET(set
, ISL_MAP_DISJOINT
))
7544 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
7546 result
= isl_map_alloc_space(isl_space_copy(map
->dim
),
7547 map
->n
* set
->n
, flags
);
7548 for (i
= 0; result
&& i
< map
->n
; ++i
)
7549 for (j
= 0; j
< set
->n
; ++j
) {
7550 result
= isl_map_add_basic_map(result
,
7551 fn(isl_basic_map_copy(map
->p
[i
]),
7552 isl_basic_set_copy(set
->p
[j
])));
7562 static __isl_give isl_map
*map_intersect_range(__isl_take isl_map
*map
,
7563 __isl_take isl_set
*set
)
7567 ok
= isl_map_compatible_range(map
, set
);
7571 isl_die(set
->ctx
, isl_error_invalid
,
7572 "incompatible spaces", goto error
);
7574 return map_intersect_set(map
, set
, &isl_basic_map_intersect_range
);
7581 __isl_give isl_map
*isl_map_intersect_range(__isl_take isl_map
*map
,
7582 __isl_take isl_set
*set
)
7584 return isl_map_align_params_map_map_and(map
, set
, &map_intersect_range
);
7587 static __isl_give isl_map
*map_intersect_domain(__isl_take isl_map
*map
,
7588 __isl_take isl_set
*set
)
7592 ok
= isl_map_compatible_domain(map
, set
);
7596 isl_die(set
->ctx
, isl_error_invalid
,
7597 "incompatible spaces", goto error
);
7599 return map_intersect_set(map
, set
, &isl_basic_map_intersect_domain
);
7606 __isl_give isl_map
*isl_map_intersect_domain(__isl_take isl_map
*map
,
7607 __isl_take isl_set
*set
)
7609 return isl_map_align_params_map_map_and(map
, set
,
7610 &map_intersect_domain
);
7613 static __isl_give isl_map
*map_apply_domain(__isl_take isl_map
*map1
,
7614 __isl_take isl_map
*map2
)
7618 map1
= isl_map_reverse(map1
);
7619 map1
= isl_map_apply_range(map1
, map2
);
7620 return isl_map_reverse(map1
);
7627 __isl_give isl_map
*isl_map_apply_domain(__isl_take isl_map
*map1
,
7628 __isl_take isl_map
*map2
)
7630 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_domain
);
7633 static __isl_give isl_map
*map_apply_range(__isl_take isl_map
*map1
,
7634 __isl_take isl_map
*map2
)
7636 isl_space
*dim_result
;
7637 struct isl_map
*result
;
7643 dim_result
= isl_space_join(isl_space_copy(map1
->dim
),
7644 isl_space_copy(map2
->dim
));
7646 result
= isl_map_alloc_space(dim_result
, map1
->n
* map2
->n
, 0);
7649 for (i
= 0; i
< map1
->n
; ++i
)
7650 for (j
= 0; j
< map2
->n
; ++j
) {
7651 result
= isl_map_add_basic_map(result
,
7652 isl_basic_map_apply_range(
7653 isl_basic_map_copy(map1
->p
[i
]),
7654 isl_basic_map_copy(map2
->p
[j
])));
7660 if (result
&& result
->n
<= 1)
7661 ISL_F_SET(result
, ISL_MAP_DISJOINT
);
7669 __isl_give isl_map
*isl_map_apply_range(__isl_take isl_map
*map1
,
7670 __isl_take isl_map
*map2
)
7672 return isl_map_align_params_map_map_and(map1
, map2
, &map_apply_range
);
7676 * returns range - domain
7678 struct isl_basic_set
*isl_basic_map_deltas(struct isl_basic_map
*bmap
)
7680 isl_space
*target_space
;
7681 struct isl_basic_set
*bset
;
7688 isl_assert(bmap
->ctx
, isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
7689 bmap
->dim
, isl_dim_out
),
7691 target_space
= isl_space_domain(isl_basic_map_get_space(bmap
));
7692 dim
= isl_basic_map_n_in(bmap
);
7693 nparam
= isl_basic_map_n_param(bmap
);
7694 bmap
= isl_basic_map_from_range(isl_basic_map_wrap(bmap
));
7695 bmap
= isl_basic_map_add_dims(bmap
, isl_dim_in
, dim
);
7696 bmap
= isl_basic_map_extend_constraints(bmap
, dim
, 0);
7697 for (i
= 0; i
< dim
; ++i
) {
7698 int j
= isl_basic_map_alloc_equality(bmap
);
7700 bmap
= isl_basic_map_free(bmap
);
7703 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
7704 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
7705 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], 1);
7706 isl_int_set_si(bmap
->eq
[j
][1+nparam
+2*dim
+i
], -1);
7708 bset
= isl_basic_map_domain(bmap
);
7709 bset
= isl_basic_set_reset_space(bset
, target_space
);
7712 isl_basic_map_free(bmap
);
7717 * returns range - domain
7719 __isl_give isl_set
*isl_map_deltas(__isl_take isl_map
*map
)
7723 struct isl_set
*result
;
7728 isl_assert(map
->ctx
, isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
7729 map
->dim
, isl_dim_out
),
7731 dim
= isl_map_get_space(map
);
7732 dim
= isl_space_domain(dim
);
7733 result
= isl_set_alloc_space(dim
, map
->n
, 0);
7736 for (i
= 0; i
< map
->n
; ++i
)
7737 result
= isl_set_add_basic_set(result
,
7738 isl_basic_map_deltas(isl_basic_map_copy(map
->p
[i
])));
7747 * returns [domain -> range] -> range - domain
7749 __isl_give isl_basic_map
*isl_basic_map_deltas_map(
7750 __isl_take isl_basic_map
*bmap
)
7754 isl_basic_map
*domain
;
7758 if (!isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
7759 bmap
->dim
, isl_dim_out
))
7760 isl_die(bmap
->ctx
, isl_error_invalid
,
7761 "domain and range don't match", goto error
);
7763 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
7764 n
= isl_basic_map_dim(bmap
, isl_dim_in
);
7766 dim
= isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap
)));
7767 domain
= isl_basic_map_universe(dim
);
7769 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
7770 bmap
= isl_basic_map_apply_range(bmap
, domain
);
7771 bmap
= isl_basic_map_extend_constraints(bmap
, n
, 0);
7773 total
= isl_basic_map_total_dim(bmap
);
7775 for (i
= 0; i
< n
; ++i
) {
7776 k
= isl_basic_map_alloc_equality(bmap
);
7779 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
7780 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], 1);
7781 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ i
], -1);
7782 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ n
+ i
], 1);
7785 bmap
= isl_basic_map_gauss(bmap
, NULL
);
7786 return isl_basic_map_finalize(bmap
);
7788 isl_basic_map_free(bmap
);
7793 * returns [domain -> range] -> range - domain
7795 __isl_give isl_map
*isl_map_deltas_map(__isl_take isl_map
*map
)
7798 isl_space
*domain_dim
;
7803 if (!isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
7804 map
->dim
, isl_dim_out
))
7805 isl_die(map
->ctx
, isl_error_invalid
,
7806 "domain and range don't match", goto error
);
7808 map
= isl_map_cow(map
);
7812 domain_dim
= isl_space_from_range(isl_space_domain(isl_map_get_space(map
)));
7813 map
->dim
= isl_space_from_domain(isl_space_wrap(map
->dim
));
7814 map
->dim
= isl_space_join(map
->dim
, domain_dim
);
7817 for (i
= 0; i
< map
->n
; ++i
) {
7818 map
->p
[i
] = isl_basic_map_deltas_map(map
->p
[i
]);
7822 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
7829 static __isl_give isl_basic_map
*basic_map_identity(__isl_take isl_space
*dims
)
7831 struct isl_basic_map
*bmap
;
7839 nparam
= dims
->nparam
;
7841 bmap
= isl_basic_map_alloc_space(dims
, 0, dim
, 0);
7845 for (i
= 0; i
< dim
; ++i
) {
7846 int j
= isl_basic_map_alloc_equality(bmap
);
7849 isl_seq_clr(bmap
->eq
[j
], 1 + isl_basic_map_total_dim(bmap
));
7850 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
7851 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], -1);
7853 return isl_basic_map_finalize(bmap
);
7855 isl_basic_map_free(bmap
);
7859 __isl_give isl_basic_map
*isl_basic_map_identity(__isl_take isl_space
*dim
)
7863 if (dim
->n_in
!= dim
->n_out
)
7864 isl_die(dim
->ctx
, isl_error_invalid
,
7865 "number of input and output dimensions needs to be "
7866 "the same", goto error
);
7867 return basic_map_identity(dim
);
7869 isl_space_free(dim
);
7873 __isl_give isl_map
*isl_map_identity(__isl_take isl_space
*dim
)
7875 return isl_map_from_basic_map(isl_basic_map_identity(dim
));
7878 __isl_give isl_map
*isl_set_identity(__isl_take isl_set
*set
)
7880 isl_space
*dim
= isl_set_get_space(set
);
7882 id
= isl_map_identity(isl_space_map_from_set(dim
));
7883 return isl_map_intersect_range(id
, set
);
7886 /* Construct a basic set with all set dimensions having only non-negative
7889 __isl_give isl_basic_set
*isl_basic_set_positive_orthant(
7890 __isl_take isl_space
*space
)
7895 struct isl_basic_set
*bset
;
7899 nparam
= space
->nparam
;
7901 bset
= isl_basic_set_alloc_space(space
, 0, 0, dim
);
7904 for (i
= 0; i
< dim
; ++i
) {
7905 int k
= isl_basic_set_alloc_inequality(bset
);
7908 isl_seq_clr(bset
->ineq
[k
], 1 + isl_basic_set_total_dim(bset
));
7909 isl_int_set_si(bset
->ineq
[k
][1 + nparam
+ i
], 1);
7913 isl_basic_set_free(bset
);
7917 /* Construct the half-space x_pos >= 0.
7919 static __isl_give isl_basic_set
*nonneg_halfspace(__isl_take isl_space
*dim
,
7923 isl_basic_set
*nonneg
;
7925 nonneg
= isl_basic_set_alloc_space(dim
, 0, 0, 1);
7926 k
= isl_basic_set_alloc_inequality(nonneg
);
7929 isl_seq_clr(nonneg
->ineq
[k
], 1 + isl_basic_set_total_dim(nonneg
));
7930 isl_int_set_si(nonneg
->ineq
[k
][pos
], 1);
7932 return isl_basic_set_finalize(nonneg
);
7934 isl_basic_set_free(nonneg
);
7938 /* Construct the half-space x_pos <= -1.
7940 static __isl_give isl_basic_set
*neg_halfspace(__isl_take isl_space
*dim
, int pos
)
7945 neg
= isl_basic_set_alloc_space(dim
, 0, 0, 1);
7946 k
= isl_basic_set_alloc_inequality(neg
);
7949 isl_seq_clr(neg
->ineq
[k
], 1 + isl_basic_set_total_dim(neg
));
7950 isl_int_set_si(neg
->ineq
[k
][0], -1);
7951 isl_int_set_si(neg
->ineq
[k
][pos
], -1);
7953 return isl_basic_set_finalize(neg
);
7955 isl_basic_set_free(neg
);
7959 __isl_give isl_set
*isl_set_split_dims(__isl_take isl_set
*set
,
7960 enum isl_dim_type type
, unsigned first
, unsigned n
)
7964 isl_basic_set
*nonneg
;
7972 isl_assert(set
->ctx
, first
+ n
<= isl_set_dim(set
, type
), goto error
);
7974 offset
= pos(set
->dim
, type
);
7975 for (i
= 0; i
< n
; ++i
) {
7976 nonneg
= nonneg_halfspace(isl_set_get_space(set
),
7977 offset
+ first
+ i
);
7978 neg
= neg_halfspace(isl_set_get_space(set
), offset
+ first
+ i
);
7980 set
= isl_set_intersect(set
, isl_basic_set_union(nonneg
, neg
));
7989 static int foreach_orthant(__isl_take isl_set
*set
, int *signs
, int first
,
7990 int len
, int (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
7997 if (isl_set_plain_is_empty(set
)) {
8002 return fn(set
, signs
, user
);
8005 half
= isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set
),
8007 half
= isl_set_intersect(half
, isl_set_copy(set
));
8008 if (foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
) < 0)
8012 half
= isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set
),
8014 half
= isl_set_intersect(half
, set
);
8015 return foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
);
8021 /* Call "fn" on the intersections of "set" with each of the orthants
8022 * (except for obviously empty intersections). The orthant is identified
8023 * by the signs array, with each entry having value 1 or -1 according
8024 * to the sign of the corresponding variable.
8026 int isl_set_foreach_orthant(__isl_keep isl_set
*set
,
8027 int (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
8037 if (isl_set_plain_is_empty(set
))
8040 nparam
= isl_set_dim(set
, isl_dim_param
);
8041 nvar
= isl_set_dim(set
, isl_dim_set
);
8043 signs
= isl_alloc_array(set
->ctx
, int, nparam
+ nvar
);
8045 r
= foreach_orthant(isl_set_copy(set
), signs
, 0, nparam
+ nvar
,
8053 isl_bool
isl_set_is_equal(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
8055 return isl_map_is_equal(set_to_map(set1
), set_to_map(set2
));
8058 isl_bool
isl_basic_map_is_subset(__isl_keep isl_basic_map
*bmap1
,
8059 __isl_keep isl_basic_map
*bmap2
)
8062 struct isl_map
*map1
;
8063 struct isl_map
*map2
;
8065 if (!bmap1
|| !bmap2
)
8066 return isl_bool_error
;
8068 map1
= isl_map_from_basic_map(isl_basic_map_copy(bmap1
));
8069 map2
= isl_map_from_basic_map(isl_basic_map_copy(bmap2
));
8071 is_subset
= isl_map_is_subset(map1
, map2
);
8079 isl_bool
isl_basic_set_is_subset(__isl_keep isl_basic_set
*bset1
,
8080 __isl_keep isl_basic_set
*bset2
)
8082 return isl_basic_map_is_subset(bset1
, bset2
);
8085 isl_bool
isl_basic_map_is_equal(__isl_keep isl_basic_map
*bmap1
,
8086 __isl_keep isl_basic_map
*bmap2
)
8090 if (!bmap1
|| !bmap2
)
8091 return isl_bool_error
;
8092 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
8093 if (is_subset
!= isl_bool_true
)
8095 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
8099 isl_bool
isl_basic_set_is_equal(__isl_keep isl_basic_set
*bset1
,
8100 __isl_keep isl_basic_set
*bset2
)
8102 return isl_basic_map_is_equal(
8103 bset_to_bmap(bset1
), bset_to_bmap(bset2
));
8106 isl_bool
isl_map_is_empty(__isl_keep isl_map
*map
)
8112 return isl_bool_error
;
8113 for (i
= 0; i
< map
->n
; ++i
) {
8114 is_empty
= isl_basic_map_is_empty(map
->p
[i
]);
8116 return isl_bool_error
;
8118 return isl_bool_false
;
8120 return isl_bool_true
;
8123 isl_bool
isl_map_plain_is_empty(__isl_keep isl_map
*map
)
8125 return map
? map
->n
== 0 : isl_bool_error
;
8128 isl_bool
isl_set_plain_is_empty(__isl_keep isl_set
*set
)
8130 return set
? set
->n
== 0 : isl_bool_error
;
8133 isl_bool
isl_set_is_empty(__isl_keep isl_set
*set
)
8135 return isl_map_is_empty(set_to_map(set
));
8138 isl_bool
isl_map_has_equal_space(__isl_keep isl_map
*map1
,
8139 __isl_keep isl_map
*map2
)
8142 return isl_bool_error
;
8144 return isl_space_is_equal(map1
->dim
, map2
->dim
);
8147 isl_bool
isl_set_has_equal_space(__isl_keep isl_set
*set1
,
8148 __isl_keep isl_set
*set2
)
8151 return isl_bool_error
;
8153 return isl_space_is_equal(set1
->dim
, set2
->dim
);
8156 static isl_bool
map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
8161 return isl_bool_error
;
8162 is_subset
= isl_map_is_subset(map1
, map2
);
8163 if (is_subset
!= isl_bool_true
)
8165 is_subset
= isl_map_is_subset(map2
, map1
);
8169 /* Is "map1" equal to "map2"?
8171 * First check if they are obviously equal.
8172 * If not, then perform a more detailed analysis.
8174 isl_bool
isl_map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
8178 equal
= isl_map_plain_is_equal(map1
, map2
);
8179 if (equal
< 0 || equal
)
8181 return isl_map_align_params_map_map_and_test(map1
, map2
, &map_is_equal
);
8184 isl_bool
isl_basic_map_is_strict_subset(
8185 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
8189 if (!bmap1
|| !bmap2
)
8190 return isl_bool_error
;
8191 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
8192 if (is_subset
!= isl_bool_true
)
8194 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
8195 if (is_subset
== isl_bool_error
)
8200 isl_bool
isl_map_is_strict_subset(__isl_keep isl_map
*map1
,
8201 __isl_keep isl_map
*map2
)
8206 return isl_bool_error
;
8207 is_subset
= isl_map_is_subset(map1
, map2
);
8208 if (is_subset
!= isl_bool_true
)
8210 is_subset
= isl_map_is_subset(map2
, map1
);
8211 if (is_subset
== isl_bool_error
)
8216 isl_bool
isl_set_is_strict_subset(__isl_keep isl_set
*set1
,
8217 __isl_keep isl_set
*set2
)
8219 return isl_map_is_strict_subset(set_to_map(set1
), set_to_map(set2
));
8222 /* Is "bmap" obviously equal to the universe with the same space?
8224 * That is, does it not have any constraints?
8226 isl_bool
isl_basic_map_plain_is_universe(__isl_keep isl_basic_map
*bmap
)
8229 return isl_bool_error
;
8230 return bmap
->n_eq
== 0 && bmap
->n_ineq
== 0;
8233 /* Is "bset" obviously equal to the universe with the same space?
8235 isl_bool
isl_basic_set_plain_is_universe(__isl_keep isl_basic_set
*bset
)
8237 return isl_basic_map_plain_is_universe(bset
);
8240 /* If "c" does not involve any existentially quantified variables,
8241 * then set *univ to false and abort
8243 static isl_stat
involves_divs(__isl_take isl_constraint
*c
, void *user
)
8245 isl_bool
*univ
= user
;
8248 n
= isl_constraint_dim(c
, isl_dim_div
);
8249 *univ
= isl_constraint_involves_dims(c
, isl_dim_div
, 0, n
);
8250 isl_constraint_free(c
);
8251 if (*univ
< 0 || !*univ
)
8252 return isl_stat_error
;
8256 /* Is "bmap" equal to the universe with the same space?
8258 * First check if it is obviously equal to the universe.
8259 * If not and if there are any constraints not involving
8260 * existentially quantified variables, then it is certainly
8261 * not equal to the universe.
8262 * Otherwise, check if the universe is a subset of "bmap".
8264 isl_bool
isl_basic_map_is_universe(__isl_keep isl_basic_map
*bmap
)
8267 isl_basic_map
*test
;
8269 univ
= isl_basic_map_plain_is_universe(bmap
);
8270 if (univ
< 0 || univ
)
8272 if (isl_basic_map_dim(bmap
, isl_dim_div
) == 0)
8273 return isl_bool_false
;
8274 univ
= isl_bool_true
;
8275 if (isl_basic_map_foreach_constraint(bmap
, &involves_divs
, &univ
) < 0 &&
8277 return isl_bool_error
;
8278 if (univ
< 0 || !univ
)
8280 test
= isl_basic_map_universe(isl_basic_map_get_space(bmap
));
8281 univ
= isl_basic_map_is_subset(test
, bmap
);
8282 isl_basic_map_free(test
);
8286 /* Is "bset" equal to the universe with the same space?
8288 isl_bool
isl_basic_set_is_universe(__isl_keep isl_basic_set
*bset
)
8290 return isl_basic_map_is_universe(bset
);
8293 isl_bool
isl_map_plain_is_universe(__isl_keep isl_map
*map
)
8298 return isl_bool_error
;
8300 for (i
= 0; i
< map
->n
; ++i
) {
8301 isl_bool r
= isl_basic_map_plain_is_universe(map
->p
[i
]);
8306 return isl_bool_false
;
8309 isl_bool
isl_set_plain_is_universe(__isl_keep isl_set
*set
)
8311 return isl_map_plain_is_universe(set_to_map(set
));
8314 isl_bool
isl_basic_map_is_empty(__isl_keep isl_basic_map
*bmap
)
8316 struct isl_basic_set
*bset
= NULL
;
8317 struct isl_vec
*sample
= NULL
;
8318 isl_bool empty
, non_empty
;
8321 return isl_bool_error
;
8323 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
8324 return isl_bool_true
;
8326 if (isl_basic_map_plain_is_universe(bmap
))
8327 return isl_bool_false
;
8329 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
8330 struct isl_basic_map
*copy
= isl_basic_map_copy(bmap
);
8331 copy
= isl_basic_map_remove_redundancies(copy
);
8332 empty
= isl_basic_map_plain_is_empty(copy
);
8333 isl_basic_map_free(copy
);
8337 non_empty
= isl_basic_map_plain_is_non_empty(bmap
);
8339 return isl_bool_error
;
8341 return isl_bool_false
;
8342 isl_vec_free(bmap
->sample
);
8343 bmap
->sample
= NULL
;
8344 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
8346 return isl_bool_error
;
8347 sample
= isl_basic_set_sample_vec(bset
);
8349 return isl_bool_error
;
8350 empty
= sample
->size
== 0;
8351 isl_vec_free(bmap
->sample
);
8352 bmap
->sample
= sample
;
8354 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
8359 isl_bool
isl_basic_map_plain_is_empty(__isl_keep isl_basic_map
*bmap
)
8362 return isl_bool_error
;
8363 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
);
8366 isl_bool
isl_basic_set_plain_is_empty(__isl_keep isl_basic_set
*bset
)
8369 return isl_bool_error
;
8370 return ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
);
8373 /* Is "bmap" known to be non-empty?
8375 * That is, is the cached sample still valid?
8377 isl_bool
isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map
*bmap
)
8382 return isl_bool_error
;
8384 return isl_bool_false
;
8385 total
= 1 + isl_basic_map_total_dim(bmap
);
8386 if (bmap
->sample
->size
!= total
)
8387 return isl_bool_false
;
8388 return isl_basic_map_contains(bmap
, bmap
->sample
);
8391 isl_bool
isl_basic_set_is_empty(__isl_keep isl_basic_set
*bset
)
8393 return isl_basic_map_is_empty(bset_to_bmap(bset
));
8396 struct isl_map
*isl_basic_map_union(
8397 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
8399 struct isl_map
*map
;
8400 if (!bmap1
|| !bmap2
)
8403 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
), goto error
);
8405 map
= isl_map_alloc_space(isl_space_copy(bmap1
->dim
), 2, 0);
8408 map
= isl_map_add_basic_map(map
, bmap1
);
8409 map
= isl_map_add_basic_map(map
, bmap2
);
8412 isl_basic_map_free(bmap1
);
8413 isl_basic_map_free(bmap2
);
8417 struct isl_set
*isl_basic_set_union(
8418 struct isl_basic_set
*bset1
, struct isl_basic_set
*bset2
)
8420 return set_from_map(isl_basic_map_union(bset_to_bmap(bset1
),
8421 bset_to_bmap(bset2
)));
8424 /* Order divs such that any div only depends on previous divs */
8425 struct isl_basic_map
*isl_basic_map_order_divs(struct isl_basic_map
*bmap
)
8433 off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
8435 for (i
= 0; i
< bmap
->n_div
; ++i
) {
8437 if (isl_int_is_zero(bmap
->div
[i
][0]))
8439 pos
= isl_seq_first_non_zero(bmap
->div
[i
]+1+1+off
+i
,
8444 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_internal
,
8445 "integer division depends on itself",
8446 return isl_basic_map_free(bmap
));
8447 isl_basic_map_swap_div(bmap
, i
, i
+ pos
);
8453 struct isl_basic_set
*isl_basic_set_order_divs(struct isl_basic_set
*bset
)
8455 return bset_from_bmap(isl_basic_map_order_divs(bset_to_bmap(bset
)));
8458 __isl_give isl_map
*isl_map_order_divs(__isl_take isl_map
*map
)
8465 for (i
= 0; i
< map
->n
; ++i
) {
8466 map
->p
[i
] = isl_basic_map_order_divs(map
->p
[i
]);
8477 /* Sort the local variables of "bset".
8479 __isl_give isl_basic_set
*isl_basic_set_sort_divs(
8480 __isl_take isl_basic_set
*bset
)
8482 return bset_from_bmap(isl_basic_map_sort_divs(bset_to_bmap(bset
)));
8485 /* Apply the expansion computed by isl_merge_divs.
8486 * The expansion itself is given by "exp" while the resulting
8487 * list of divs is given by "div".
8489 * Move the integer divisions of "bmap" into the right position
8490 * according to "exp" and then introduce the additional integer
8491 * divisions, adding div constraints.
8492 * The moving should be done first to avoid moving coefficients
8493 * in the definitions of the extra integer divisions.
8495 __isl_give isl_basic_map
*isl_basic_map_expand_divs(
8496 __isl_take isl_basic_map
*bmap
, __isl_take isl_mat
*div
, int *exp
)
8501 bmap
= isl_basic_map_cow(bmap
);
8505 if (div
->n_row
< bmap
->n_div
)
8506 isl_die(isl_mat_get_ctx(div
), isl_error_invalid
,
8507 "not an expansion", goto error
);
8509 n_div
= bmap
->n_div
;
8510 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
8511 div
->n_row
- n_div
, 0,
8512 2 * (div
->n_row
- n_div
));
8514 for (i
= n_div
; i
< div
->n_row
; ++i
)
8515 if (isl_basic_map_alloc_div(bmap
) < 0)
8518 for (j
= n_div
- 1; j
>= 0; --j
) {
8521 isl_basic_map_swap_div(bmap
, j
, exp
[j
]);
8524 for (i
= 0; i
< div
->n_row
; ++i
) {
8525 if (j
< n_div
&& exp
[j
] == i
) {
8528 isl_seq_cpy(bmap
->div
[i
], div
->row
[i
], div
->n_col
);
8529 if (isl_basic_map_div_is_marked_unknown(bmap
, i
))
8531 if (isl_basic_map_add_div_constraints(bmap
, i
) < 0)
8539 isl_basic_map_free(bmap
);
8544 /* Apply the expansion computed by isl_merge_divs.
8545 * The expansion itself is given by "exp" while the resulting
8546 * list of divs is given by "div".
8548 __isl_give isl_basic_set
*isl_basic_set_expand_divs(
8549 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*div
, int *exp
)
8551 return isl_basic_map_expand_divs(bset
, div
, exp
);
8554 /* Look for a div in dst that corresponds to the div "div" in src.
8555 * The divs before "div" in src and dst are assumed to be the same.
8557 * Returns -1 if no corresponding div was found and the position
8558 * of the corresponding div in dst otherwise.
8560 static int find_div(struct isl_basic_map
*dst
,
8561 struct isl_basic_map
*src
, unsigned div
)
8565 unsigned total
= isl_space_dim(src
->dim
, isl_dim_all
);
8567 isl_assert(dst
->ctx
, div
<= dst
->n_div
, return -1);
8568 for (i
= div
; i
< dst
->n_div
; ++i
)
8569 if (isl_seq_eq(dst
->div
[i
], src
->div
[div
], 1+1+total
+div
) &&
8570 isl_seq_first_non_zero(dst
->div
[i
]+1+1+total
+div
,
8571 dst
->n_div
- div
) == -1)
8576 /* Align the divs of "dst" to those of "src", adding divs from "src"
8577 * if needed. That is, make sure that the first src->n_div divs
8578 * of the result are equal to those of src.
8580 * The result is not finalized as by design it will have redundant
8581 * divs if any divs from "src" were copied.
8583 __isl_give isl_basic_map
*isl_basic_map_align_divs(
8584 __isl_take isl_basic_map
*dst
, __isl_keep isl_basic_map
*src
)
8587 int known
, extended
;
8591 return isl_basic_map_free(dst
);
8593 if (src
->n_div
== 0)
8596 known
= isl_basic_map_divs_known(src
);
8598 return isl_basic_map_free(dst
);
8600 isl_die(isl_basic_map_get_ctx(src
), isl_error_invalid
,
8601 "some src divs are unknown",
8602 return isl_basic_map_free(dst
));
8604 src
= isl_basic_map_order_divs(src
);
8607 total
= isl_space_dim(src
->dim
, isl_dim_all
);
8608 for (i
= 0; i
< src
->n_div
; ++i
) {
8609 int j
= find_div(dst
, src
, i
);
8612 int extra
= src
->n_div
- i
;
8613 dst
= isl_basic_map_cow(dst
);
8616 dst
= isl_basic_map_extend_space(dst
,
8617 isl_space_copy(dst
->dim
),
8618 extra
, 0, 2 * extra
);
8621 j
= isl_basic_map_alloc_div(dst
);
8623 return isl_basic_map_free(dst
);
8624 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
+i
);
8625 isl_seq_clr(dst
->div
[j
]+1+1+total
+i
, dst
->n_div
- i
);
8626 if (isl_basic_map_add_div_constraints(dst
, j
) < 0)
8627 return isl_basic_map_free(dst
);
8630 isl_basic_map_swap_div(dst
, i
, j
);
8635 struct isl_basic_set
*isl_basic_set_align_divs(
8636 struct isl_basic_set
*dst
, struct isl_basic_set
*src
)
8638 return bset_from_bmap(isl_basic_map_align_divs(bset_to_bmap(dst
),
8639 bset_to_bmap(src
)));
8642 struct isl_map
*isl_map_align_divs(struct isl_map
*map
)
8650 map
= isl_map_compute_divs(map
);
8651 map
= isl_map_cow(map
);
8655 for (i
= 1; i
< map
->n
; ++i
)
8656 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], map
->p
[i
]);
8657 for (i
= 1; i
< map
->n
; ++i
) {
8658 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], map
->p
[0]);
8660 return isl_map_free(map
);
8663 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
8667 struct isl_set
*isl_set_align_divs(struct isl_set
*set
)
8669 return set_from_map(isl_map_align_divs(set_to_map(set
)));
8672 /* Align the divs of the basic maps in "map" to those
8673 * of the basic maps in "list", as well as to the other basic maps in "map".
8674 * The elements in "list" are assumed to have known divs.
8676 __isl_give isl_map
*isl_map_align_divs_to_basic_map_list(
8677 __isl_take isl_map
*map
, __isl_keep isl_basic_map_list
*list
)
8681 map
= isl_map_compute_divs(map
);
8682 map
= isl_map_cow(map
);
8684 return isl_map_free(map
);
8688 n
= isl_basic_map_list_n_basic_map(list
);
8689 for (i
= 0; i
< n
; ++i
) {
8690 isl_basic_map
*bmap
;
8692 bmap
= isl_basic_map_list_get_basic_map(list
, i
);
8693 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], bmap
);
8694 isl_basic_map_free(bmap
);
8697 return isl_map_free(map
);
8699 return isl_map_align_divs(map
);
8702 /* Align the divs of each element of "list" to those of "bmap".
8703 * Both "bmap" and the elements of "list" are assumed to have known divs.
8705 __isl_give isl_basic_map_list
*isl_basic_map_list_align_divs_to_basic_map(
8706 __isl_take isl_basic_map_list
*list
, __isl_keep isl_basic_map
*bmap
)
8711 return isl_basic_map_list_free(list
);
8713 n
= isl_basic_map_list_n_basic_map(list
);
8714 for (i
= 0; i
< n
; ++i
) {
8715 isl_basic_map
*bmap_i
;
8717 bmap_i
= isl_basic_map_list_get_basic_map(list
, i
);
8718 bmap_i
= isl_basic_map_align_divs(bmap_i
, bmap
);
8719 list
= isl_basic_map_list_set_basic_map(list
, i
, bmap_i
);
8725 static __isl_give isl_set
*set_apply( __isl_take isl_set
*set
,
8726 __isl_take isl_map
*map
)
8730 ok
= isl_map_compatible_domain(map
, set
);
8734 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
8735 "incompatible spaces", goto error
);
8736 map
= isl_map_intersect_domain(map
, set
);
8737 set
= isl_map_range(map
);
8745 __isl_give isl_set
*isl_set_apply( __isl_take isl_set
*set
,
8746 __isl_take isl_map
*map
)
8748 return isl_map_align_params_map_map_and(set
, map
, &set_apply
);
8751 /* There is no need to cow as removing empty parts doesn't change
8752 * the meaning of the set.
8754 struct isl_map
*isl_map_remove_empty_parts(struct isl_map
*map
)
8761 for (i
= map
->n
- 1; i
>= 0; --i
)
8762 remove_if_empty(map
, i
);
8767 struct isl_set
*isl_set_remove_empty_parts(struct isl_set
*set
)
8769 return set_from_map(isl_map_remove_empty_parts(set_to_map(set
)));
8772 static __isl_give isl_basic_map
*map_copy_basic_map(__isl_keep isl_map
*map
)
8774 struct isl_basic_map
*bmap
;
8775 if (!map
|| map
->n
== 0)
8777 bmap
= map
->p
[map
->n
-1];
8778 isl_assert(map
->ctx
, ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
), return NULL
);
8779 return isl_basic_map_copy(bmap
);
8782 __isl_give isl_basic_map
*isl_map_copy_basic_map(__isl_keep isl_map
*map
)
8784 return map_copy_basic_map(map
);
8787 struct isl_basic_set
*isl_set_copy_basic_set(struct isl_set
*set
)
8789 return bset_from_bmap(map_copy_basic_map(set_to_map(set
)));
8792 static __isl_give isl_map
*map_drop_basic_map(__isl_take isl_map
*map
,
8793 __isl_keep isl_basic_map
*bmap
)
8799 for (i
= map
->n
-1; i
>= 0; --i
) {
8800 if (map
->p
[i
] != bmap
)
8802 map
= isl_map_cow(map
);
8805 isl_basic_map_free(map
->p
[i
]);
8806 if (i
!= map
->n
-1) {
8807 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
8808 map
->p
[i
] = map
->p
[map
->n
-1];
8819 __isl_give isl_map
*isl_map_drop_basic_map(__isl_take isl_map
*map
,
8820 __isl_keep isl_basic_map
*bmap
)
8822 return map_drop_basic_map(map
, bmap
);
8825 struct isl_set
*isl_set_drop_basic_set(struct isl_set
*set
,
8826 struct isl_basic_set
*bset
)
8828 return set_from_map(map_drop_basic_map(set_to_map(set
),
8829 bset_to_bmap(bset
)));
8832 /* Given two basic sets bset1 and bset2, compute the maximal difference
8833 * between the values of dimension pos in bset1 and those in bset2
8834 * for any common value of the parameters and dimensions preceding pos.
8836 static enum isl_lp_result
basic_set_maximal_difference_at(
8837 __isl_keep isl_basic_set
*bset1
, __isl_keep isl_basic_set
*bset2
,
8838 int pos
, isl_int
*opt
)
8840 isl_basic_map
*bmap1
;
8841 isl_basic_map
*bmap2
;
8842 struct isl_ctx
*ctx
;
8843 struct isl_vec
*obj
;
8847 enum isl_lp_result res
;
8849 if (!bset1
|| !bset2
)
8850 return isl_lp_error
;
8852 nparam
= isl_basic_set_n_param(bset1
);
8853 dim1
= isl_basic_set_n_dim(bset1
);
8855 bmap1
= isl_basic_map_from_range(isl_basic_set_copy(bset1
));
8856 bmap2
= isl_basic_map_from_range(isl_basic_set_copy(bset2
));
8857 bmap1
= isl_basic_map_move_dims(bmap1
, isl_dim_in
, 0,
8858 isl_dim_out
, 0, pos
);
8859 bmap2
= isl_basic_map_move_dims(bmap2
, isl_dim_in
, 0,
8860 isl_dim_out
, 0, pos
);
8861 bmap1
= isl_basic_map_range_product(bmap1
, bmap2
);
8863 return isl_lp_error
;
8865 total
= isl_basic_map_total_dim(bmap1
);
8867 obj
= isl_vec_alloc(ctx
, 1 + total
);
8870 isl_seq_clr(obj
->block
.data
, 1 + total
);
8871 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
], 1);
8872 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
+(dim1
-pos
)], -1);
8873 res
= isl_basic_map_solve_lp(bmap1
, 1, obj
->block
.data
, ctx
->one
,
8875 isl_basic_map_free(bmap1
);
8879 isl_basic_map_free(bmap1
);
8880 return isl_lp_error
;
8883 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
8884 * for any common value of the parameters and dimensions preceding pos
8885 * in both basic sets, the values of dimension pos in bset1 are
8886 * smaller or larger than those in bset2.
8889 * 1 if bset1 follows bset2
8890 * -1 if bset1 precedes bset2
8891 * 0 if bset1 and bset2 are incomparable
8892 * -2 if some error occurred.
8894 int isl_basic_set_compare_at(struct isl_basic_set
*bset1
,
8895 struct isl_basic_set
*bset2
, int pos
)
8898 enum isl_lp_result res
;
8903 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
8905 if (res
== isl_lp_empty
)
8907 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
8908 res
== isl_lp_unbounded
)
8910 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
8919 /* Given two basic sets bset1 and bset2, check whether
8920 * for any common value of the parameters and dimensions preceding pos
8921 * there is a value of dimension pos in bset1 that is larger
8922 * than a value of the same dimension in bset2.
8925 * 1 if there exists such a pair
8926 * 0 if there is no such pair, but there is a pair of equal values
8928 * -2 if some error occurred.
8930 int isl_basic_set_follows_at(__isl_keep isl_basic_set
*bset1
,
8931 __isl_keep isl_basic_set
*bset2
, int pos
)
8934 enum isl_lp_result res
;
8939 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
8941 if (res
== isl_lp_empty
)
8943 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
8944 res
== isl_lp_unbounded
)
8946 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
8948 else if (res
== isl_lp_ok
)
8957 /* Given two sets set1 and set2, check whether
8958 * for any common value of the parameters and dimensions preceding pos
8959 * there is a value of dimension pos in set1 that is larger
8960 * than a value of the same dimension in set2.
8963 * 1 if there exists such a pair
8964 * 0 if there is no such pair, but there is a pair of equal values
8966 * -2 if some error occurred.
8968 int isl_set_follows_at(__isl_keep isl_set
*set1
,
8969 __isl_keep isl_set
*set2
, int pos
)
8977 for (i
= 0; i
< set1
->n
; ++i
)
8978 for (j
= 0; j
< set2
->n
; ++j
) {
8980 f
= isl_basic_set_follows_at(set1
->p
[i
], set2
->p
[j
], pos
);
8981 if (f
== 1 || f
== -2)
8990 static isl_bool
isl_basic_map_plain_has_fixed_var(
8991 __isl_keep isl_basic_map
*bmap
, unsigned pos
, isl_int
*val
)
8998 return isl_bool_error
;
8999 total
= isl_basic_map_total_dim(bmap
);
9000 for (i
= 0, d
= total
-1; i
< bmap
->n_eq
&& d
+1 > pos
; ++i
) {
9001 for (; d
+1 > pos
; --d
)
9002 if (!isl_int_is_zero(bmap
->eq
[i
][1+d
]))
9006 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1, d
) != -1)
9007 return isl_bool_false
;
9008 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1+d
+1, total
-d
-1) != -1)
9009 return isl_bool_false
;
9010 if (!isl_int_is_one(bmap
->eq
[i
][1+d
]))
9011 return isl_bool_false
;
9013 isl_int_neg(*val
, bmap
->eq
[i
][0]);
9014 return isl_bool_true
;
9016 return isl_bool_false
;
9019 static isl_bool
isl_map_plain_has_fixed_var(__isl_keep isl_map
*map
,
9020 unsigned pos
, isl_int
*val
)
9028 return isl_bool_error
;
9030 return isl_bool_false
;
9032 return isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, val
);
9035 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, &v
);
9036 for (i
= 1; fixed
== isl_bool_true
&& i
< map
->n
; ++i
) {
9037 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[i
], pos
, &tmp
);
9038 if (fixed
== isl_bool_true
&& isl_int_ne(tmp
, v
))
9039 fixed
= isl_bool_false
;
9042 isl_int_set(*val
, v
);
9048 static isl_bool
isl_basic_set_plain_has_fixed_var(
9049 __isl_keep isl_basic_set
*bset
, unsigned pos
, isl_int
*val
)
9051 return isl_basic_map_plain_has_fixed_var(bset_to_bmap(bset
),
9055 static int isl_set_plain_has_fixed_var(__isl_keep isl_set
*set
, unsigned pos
,
9058 return isl_map_plain_has_fixed_var(set_to_map(set
), pos
, val
);
9061 isl_bool
isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map
*bmap
,
9062 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
9064 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
9065 return isl_bool_error
;
9066 return isl_basic_map_plain_has_fixed_var(bmap
,
9067 isl_basic_map_offset(bmap
, type
) - 1 + pos
, val
);
9070 /* If "bmap" obviously lies on a hyperplane where the given dimension
9071 * has a fixed value, then return that value.
9072 * Otherwise return NaN.
9074 __isl_give isl_val
*isl_basic_map_plain_get_val_if_fixed(
9075 __isl_keep isl_basic_map
*bmap
,
9076 enum isl_dim_type type
, unsigned pos
)
9084 ctx
= isl_basic_map_get_ctx(bmap
);
9085 v
= isl_val_alloc(ctx
);
9088 fixed
= isl_basic_map_plain_is_fixed(bmap
, type
, pos
, &v
->n
);
9090 return isl_val_free(v
);
9092 isl_int_set_si(v
->d
, 1);
9096 return isl_val_nan(ctx
);
9099 isl_bool
isl_map_plain_is_fixed(__isl_keep isl_map
*map
,
9100 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
9102 if (pos
>= isl_map_dim(map
, type
))
9103 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9104 "position out of bounds", return isl_bool_error
);
9105 return isl_map_plain_has_fixed_var(map
,
9106 map_offset(map
, type
) - 1 + pos
, val
);
9109 /* If "map" obviously lies on a hyperplane where the given dimension
9110 * has a fixed value, then return that value.
9111 * Otherwise return NaN.
9113 __isl_give isl_val
*isl_map_plain_get_val_if_fixed(__isl_keep isl_map
*map
,
9114 enum isl_dim_type type
, unsigned pos
)
9122 ctx
= isl_map_get_ctx(map
);
9123 v
= isl_val_alloc(ctx
);
9126 fixed
= isl_map_plain_is_fixed(map
, type
, pos
, &v
->n
);
9128 return isl_val_free(v
);
9130 isl_int_set_si(v
->d
, 1);
9134 return isl_val_nan(ctx
);
9137 /* If "set" obviously lies on a hyperplane where the given dimension
9138 * has a fixed value, then return that value.
9139 * Otherwise return NaN.
9141 __isl_give isl_val
*isl_set_plain_get_val_if_fixed(__isl_keep isl_set
*set
,
9142 enum isl_dim_type type
, unsigned pos
)
9144 return isl_map_plain_get_val_if_fixed(set
, type
, pos
);
9147 isl_bool
isl_set_plain_is_fixed(__isl_keep isl_set
*set
,
9148 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
9150 return isl_map_plain_is_fixed(set
, type
, pos
, val
);
9153 /* Check if dimension dim has fixed value and if so and if val is not NULL,
9154 * then return this fixed value in *val.
9156 isl_bool
isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set
*bset
,
9157 unsigned dim
, isl_int
*val
)
9159 return isl_basic_set_plain_has_fixed_var(bset
,
9160 isl_basic_set_n_param(bset
) + dim
, val
);
9163 /* Check if dimension dim has fixed value and if so and if val is not NULL,
9164 * then return this fixed value in *val.
9166 int isl_set_plain_dim_is_fixed(__isl_keep isl_set
*set
,
9167 unsigned dim
, isl_int
*val
)
9169 return isl_set_plain_has_fixed_var(set
, isl_set_n_param(set
) + dim
, val
);
9172 /* Check if input variable in has fixed value and if so and if val is not NULL,
9173 * then return this fixed value in *val.
9175 int isl_map_plain_input_is_fixed(__isl_keep isl_map
*map
,
9176 unsigned in
, isl_int
*val
)
9178 return isl_map_plain_has_fixed_var(map
, isl_map_n_param(map
) + in
, val
);
9181 /* Check if dimension dim has an (obvious) fixed lower bound and if so
9182 * and if val is not NULL, then return this lower bound in *val.
9184 int isl_basic_set_plain_dim_has_fixed_lower_bound(
9185 __isl_keep isl_basic_set
*bset
, unsigned dim
, isl_int
*val
)
9187 int i
, i_eq
= -1, i_ineq
= -1;
9194 total
= isl_basic_set_total_dim(bset
);
9195 nparam
= isl_basic_set_n_param(bset
);
9196 for (i
= 0; i
< bset
->n_eq
; ++i
) {
9197 if (isl_int_is_zero(bset
->eq
[i
][1+nparam
+dim
]))
9203 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
9204 if (!isl_int_is_pos(bset
->ineq
[i
][1+nparam
+dim
]))
9206 if (i_eq
!= -1 || i_ineq
!= -1)
9210 if (i_eq
== -1 && i_ineq
== -1)
9212 c
= i_eq
!= -1 ? bset
->eq
[i_eq
] : bset
->ineq
[i_ineq
];
9213 /* The coefficient should always be one due to normalization. */
9214 if (!isl_int_is_one(c
[1+nparam
+dim
]))
9216 if (isl_seq_first_non_zero(c
+1, nparam
+dim
) != -1)
9218 if (isl_seq_first_non_zero(c
+1+nparam
+dim
+1,
9219 total
- nparam
- dim
- 1) != -1)
9222 isl_int_neg(*val
, c
[0]);
9226 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set
*set
,
9227 unsigned dim
, isl_int
*val
)
9239 return isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[0],
9243 fixed
= isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[0],
9245 for (i
= 1; fixed
== 1 && i
< set
->n
; ++i
) {
9246 fixed
= isl_basic_set_plain_dim_has_fixed_lower_bound(set
->p
[i
],
9248 if (fixed
== 1 && isl_int_ne(tmp
, v
))
9252 isl_int_set(*val
, v
);
9258 /* Return -1 if the constraint "c1" should be sorted before "c2"
9259 * and 1 if it should be sorted after "c2".
9260 * Return 0 if the two constraints are the same (up to the constant term).
9262 * In particular, if a constraint involves later variables than another
9263 * then it is sorted after this other constraint.
9264 * uset_gist depends on constraints without existentially quantified
9265 * variables sorting first.
9267 * For constraints that have the same latest variable, those
9268 * with the same coefficient for this latest variable (first in absolute value
9269 * and then in actual value) are grouped together.
9270 * This is useful for detecting pairs of constraints that can
9271 * be chained in their printed representation.
9273 * Finally, within a group, constraints are sorted according to
9274 * their coefficients (excluding the constant term).
9276 static int sort_constraint_cmp(const void *p1
, const void *p2
, void *arg
)
9278 isl_int
**c1
= (isl_int
**) p1
;
9279 isl_int
**c2
= (isl_int
**) p2
;
9281 unsigned size
= *(unsigned *) arg
;
9284 l1
= isl_seq_last_non_zero(*c1
+ 1, size
);
9285 l2
= isl_seq_last_non_zero(*c2
+ 1, size
);
9290 cmp
= isl_int_abs_cmp((*c1
)[1 + l1
], (*c2
)[1 + l1
]);
9293 cmp
= isl_int_cmp((*c1
)[1 + l1
], (*c2
)[1 + l1
]);
9297 return isl_seq_cmp(*c1
+ 1, *c2
+ 1, size
);
9300 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
9301 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
9302 * and 0 if the two constraints are the same (up to the constant term).
9304 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map
*bmap
,
9305 isl_int
*c1
, isl_int
*c2
)
9311 total
= isl_basic_map_total_dim(bmap
);
9312 return sort_constraint_cmp(&c1
, &c2
, &total
);
9315 __isl_give isl_basic_map
*isl_basic_map_sort_constraints(
9316 __isl_take isl_basic_map
*bmap
)
9322 if (bmap
->n_ineq
== 0)
9324 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED
))
9326 total
= isl_basic_map_total_dim(bmap
);
9327 if (isl_sort(bmap
->ineq
, bmap
->n_ineq
, sizeof(isl_int
*),
9328 &sort_constraint_cmp
, &total
) < 0)
9329 return isl_basic_map_free(bmap
);
9333 __isl_give isl_basic_set
*isl_basic_set_sort_constraints(
9334 __isl_take isl_basic_set
*bset
)
9336 isl_basic_map
*bmap
= bset_to_bmap(bset
);
9337 return bset_from_bmap(isl_basic_map_sort_constraints(bmap
));
9340 struct isl_basic_map
*isl_basic_map_normalize(struct isl_basic_map
*bmap
)
9344 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED
))
9346 bmap
= isl_basic_map_remove_redundancies(bmap
);
9347 bmap
= isl_basic_map_sort_constraints(bmap
);
9349 ISL_F_SET(bmap
, ISL_BASIC_MAP_NORMALIZED
);
9353 struct isl_basic_set
*isl_basic_set_normalize(struct isl_basic_set
*bset
)
9355 return bset_from_bmap(isl_basic_map_normalize(bset_to_bmap(bset
)));
9358 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map
*bmap1
,
9359 const __isl_keep isl_basic_map
*bmap2
)
9364 if (!bmap1
|| !bmap2
)
9369 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) !=
9370 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_RATIONAL
))
9371 return ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) ? -1 : 1;
9372 if (isl_basic_map_n_param(bmap1
) != isl_basic_map_n_param(bmap2
))
9373 return isl_basic_map_n_param(bmap1
) - isl_basic_map_n_param(bmap2
);
9374 if (isl_basic_map_n_in(bmap1
) != isl_basic_map_n_in(bmap2
))
9375 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
9376 if (isl_basic_map_n_out(bmap1
) != isl_basic_map_n_out(bmap2
))
9377 return isl_basic_map_n_out(bmap1
) - isl_basic_map_n_out(bmap2
);
9378 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
) &&
9379 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
9381 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
))
9383 if (ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
9385 if (bmap1
->n_eq
!= bmap2
->n_eq
)
9386 return bmap1
->n_eq
- bmap2
->n_eq
;
9387 if (bmap1
->n_ineq
!= bmap2
->n_ineq
)
9388 return bmap1
->n_ineq
- bmap2
->n_ineq
;
9389 if (bmap1
->n_div
!= bmap2
->n_div
)
9390 return bmap1
->n_div
- bmap2
->n_div
;
9391 total
= isl_basic_map_total_dim(bmap1
);
9392 for (i
= 0; i
< bmap1
->n_eq
; ++i
) {
9393 cmp
= isl_seq_cmp(bmap1
->eq
[i
], bmap2
->eq
[i
], 1+total
);
9397 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
9398 cmp
= isl_seq_cmp(bmap1
->ineq
[i
], bmap2
->ineq
[i
], 1+total
);
9402 for (i
= 0; i
< bmap1
->n_div
; ++i
) {
9403 cmp
= isl_seq_cmp(bmap1
->div
[i
], bmap2
->div
[i
], 1+1+total
);
9410 int isl_basic_set_plain_cmp(const __isl_keep isl_basic_set
*bset1
,
9411 const __isl_keep isl_basic_set
*bset2
)
9413 return isl_basic_map_plain_cmp(bset1
, bset2
);
9416 int isl_set_plain_cmp(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
9422 if (set1
->n
!= set2
->n
)
9423 return set1
->n
- set2
->n
;
9425 for (i
= 0; i
< set1
->n
; ++i
) {
9426 cmp
= isl_basic_set_plain_cmp(set1
->p
[i
], set2
->p
[i
]);
9434 isl_bool
isl_basic_map_plain_is_equal(__isl_keep isl_basic_map
*bmap1
,
9435 __isl_keep isl_basic_map
*bmap2
)
9437 if (!bmap1
|| !bmap2
)
9438 return isl_bool_error
;
9439 return isl_basic_map_plain_cmp(bmap1
, bmap2
) == 0;
9442 isl_bool
isl_basic_set_plain_is_equal(__isl_keep isl_basic_set
*bset1
,
9443 __isl_keep isl_basic_set
*bset2
)
9445 return isl_basic_map_plain_is_equal(bset_to_bmap(bset1
),
9446 bset_to_bmap(bset2
));
9449 static int qsort_bmap_cmp(const void *p1
, const void *p2
)
9451 const struct isl_basic_map
*bmap1
= *(const struct isl_basic_map
**)p1
;
9452 const struct isl_basic_map
*bmap2
= *(const struct isl_basic_map
**)p2
;
9454 return isl_basic_map_plain_cmp(bmap1
, bmap2
);
9457 /* Sort the basic maps of "map" and remove duplicate basic maps.
9459 * While removing basic maps, we make sure that the basic maps remain
9460 * sorted because isl_map_normalize expects the basic maps of the result
9463 static __isl_give isl_map
*sort_and_remove_duplicates(__isl_take isl_map
*map
)
9467 map
= isl_map_remove_empty_parts(map
);
9470 qsort(map
->p
, map
->n
, sizeof(struct isl_basic_map
*), qsort_bmap_cmp
);
9471 for (i
= map
->n
- 1; i
>= 1; --i
) {
9472 if (!isl_basic_map_plain_is_equal(map
->p
[i
- 1], map
->p
[i
]))
9474 isl_basic_map_free(map
->p
[i
-1]);
9475 for (j
= i
; j
< map
->n
; ++j
)
9476 map
->p
[j
- 1] = map
->p
[j
];
9483 /* Remove obvious duplicates among the basic maps of "map".
9485 * Unlike isl_map_normalize, this function does not remove redundant
9486 * constraints and only removes duplicates that have exactly the same
9487 * constraints in the input. It does sort the constraints and
9488 * the basic maps to ease the detection of duplicates.
9490 * If "map" has already been normalized or if the basic maps are
9491 * disjoint, then there can be no duplicates.
9493 __isl_give isl_map
*isl_map_remove_obvious_duplicates(__isl_take isl_map
*map
)
9496 isl_basic_map
*bmap
;
9502 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
| ISL_MAP_DISJOINT
))
9504 for (i
= 0; i
< map
->n
; ++i
) {
9505 bmap
= isl_basic_map_copy(map
->p
[i
]);
9506 bmap
= isl_basic_map_sort_constraints(bmap
);
9508 return isl_map_free(map
);
9509 isl_basic_map_free(map
->p
[i
]);
9513 map
= sort_and_remove_duplicates(map
);
9517 /* We normalize in place, but if anything goes wrong we need
9518 * to return NULL, so we need to make sure we don't change the
9519 * meaning of any possible other copies of map.
9521 __isl_give isl_map
*isl_map_normalize(__isl_take isl_map
*map
)
9524 struct isl_basic_map
*bmap
;
9528 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
))
9530 for (i
= 0; i
< map
->n
; ++i
) {
9531 bmap
= isl_basic_map_normalize(isl_basic_map_copy(map
->p
[i
]));
9534 isl_basic_map_free(map
->p
[i
]);
9538 map
= sort_and_remove_duplicates(map
);
9540 ISL_F_SET(map
, ISL_MAP_NORMALIZED
);
9547 struct isl_set
*isl_set_normalize(struct isl_set
*set
)
9549 return set_from_map(isl_map_normalize(set_to_map(set
)));
9552 isl_bool
isl_map_plain_is_equal(__isl_keep isl_map
*map1
,
9553 __isl_keep isl_map
*map2
)
9559 return isl_bool_error
;
9562 return isl_bool_true
;
9563 if (!isl_space_is_equal(map1
->dim
, map2
->dim
))
9564 return isl_bool_false
;
9566 map1
= isl_map_copy(map1
);
9567 map2
= isl_map_copy(map2
);
9568 map1
= isl_map_normalize(map1
);
9569 map2
= isl_map_normalize(map2
);
9572 equal
= map1
->n
== map2
->n
;
9573 for (i
= 0; equal
&& i
< map1
->n
; ++i
) {
9574 equal
= isl_basic_map_plain_is_equal(map1
->p
[i
], map2
->p
[i
]);
9584 return isl_bool_error
;
9587 isl_bool
isl_set_plain_is_equal(__isl_keep isl_set
*set1
,
9588 __isl_keep isl_set
*set2
)
9590 return isl_map_plain_is_equal(set_to_map(set1
), set_to_map(set2
));
9593 /* Return an interval that ranges from min to max (inclusive)
9595 struct isl_basic_set
*isl_basic_set_interval(struct isl_ctx
*ctx
,
9596 isl_int min
, isl_int max
)
9599 struct isl_basic_set
*bset
= NULL
;
9601 bset
= isl_basic_set_alloc(ctx
, 0, 1, 0, 0, 2);
9605 k
= isl_basic_set_alloc_inequality(bset
);
9608 isl_int_set_si(bset
->ineq
[k
][1], 1);
9609 isl_int_neg(bset
->ineq
[k
][0], min
);
9611 k
= isl_basic_set_alloc_inequality(bset
);
9614 isl_int_set_si(bset
->ineq
[k
][1], -1);
9615 isl_int_set(bset
->ineq
[k
][0], max
);
9619 isl_basic_set_free(bset
);
9623 /* Return the basic maps in "map" as a list.
9625 __isl_give isl_basic_map_list
*isl_map_get_basic_map_list(
9626 __isl_keep isl_map
*map
)
9630 isl_basic_map_list
*list
;
9634 ctx
= isl_map_get_ctx(map
);
9635 list
= isl_basic_map_list_alloc(ctx
, map
->n
);
9637 for (i
= 0; i
< map
->n
; ++i
) {
9638 isl_basic_map
*bmap
;
9640 bmap
= isl_basic_map_copy(map
->p
[i
]);
9641 list
= isl_basic_map_list_add(list
, bmap
);
9647 /* Return the intersection of the elements in the non-empty list "list".
9648 * All elements are assumed to live in the same space.
9650 __isl_give isl_basic_map
*isl_basic_map_list_intersect(
9651 __isl_take isl_basic_map_list
*list
)
9654 isl_basic_map
*bmap
;
9658 n
= isl_basic_map_list_n_basic_map(list
);
9660 isl_die(isl_basic_map_list_get_ctx(list
), isl_error_invalid
,
9661 "expecting non-empty list", goto error
);
9663 bmap
= isl_basic_map_list_get_basic_map(list
, 0);
9664 for (i
= 1; i
< n
; ++i
) {
9665 isl_basic_map
*bmap_i
;
9667 bmap_i
= isl_basic_map_list_get_basic_map(list
, i
);
9668 bmap
= isl_basic_map_intersect(bmap
, bmap_i
);
9671 isl_basic_map_list_free(list
);
9674 isl_basic_map_list_free(list
);
9678 /* Return the intersection of the elements in the non-empty list "list".
9679 * All elements are assumed to live in the same space.
9681 __isl_give isl_basic_set
*isl_basic_set_list_intersect(
9682 __isl_take isl_basic_set_list
*list
)
9684 return isl_basic_map_list_intersect(list
);
9687 /* Return the union of the elements of "list".
9688 * The list is required to have at least one element.
9690 __isl_give isl_set
*isl_basic_set_list_union(
9691 __isl_take isl_basic_set_list
*list
)
9695 isl_basic_set
*bset
;
9700 n
= isl_basic_set_list_n_basic_set(list
);
9702 isl_die(isl_basic_set_list_get_ctx(list
), isl_error_invalid
,
9703 "expecting non-empty list", goto error
);
9705 bset
= isl_basic_set_list_get_basic_set(list
, 0);
9706 space
= isl_basic_set_get_space(bset
);
9707 isl_basic_set_free(bset
);
9709 set
= isl_set_alloc_space(space
, n
, 0);
9710 for (i
= 0; i
< n
; ++i
) {
9711 bset
= isl_basic_set_list_get_basic_set(list
, i
);
9712 set
= isl_set_add_basic_set(set
, bset
);
9715 isl_basic_set_list_free(list
);
9718 isl_basic_set_list_free(list
);
9722 /* Return the union of the elements in the non-empty list "list".
9723 * All elements are assumed to live in the same space.
9725 __isl_give isl_set
*isl_set_list_union(__isl_take isl_set_list
*list
)
9732 n
= isl_set_list_n_set(list
);
9734 isl_die(isl_set_list_get_ctx(list
), isl_error_invalid
,
9735 "expecting non-empty list", goto error
);
9737 set
= isl_set_list_get_set(list
, 0);
9738 for (i
= 1; i
< n
; ++i
) {
9741 set_i
= isl_set_list_get_set(list
, i
);
9742 set
= isl_set_union(set
, set_i
);
9745 isl_set_list_free(list
);
9748 isl_set_list_free(list
);
9752 /* Return the Cartesian product of the basic sets in list (in the given order).
9754 __isl_give isl_basic_set
*isl_basic_set_list_product(
9755 __isl_take
struct isl_basic_set_list
*list
)
9763 struct isl_basic_set
*product
= NULL
;
9767 isl_assert(list
->ctx
, list
->n
> 0, goto error
);
9768 isl_assert(list
->ctx
, list
->p
[0], goto error
);
9769 nparam
= isl_basic_set_n_param(list
->p
[0]);
9770 dim
= isl_basic_set_n_dim(list
->p
[0]);
9771 extra
= list
->p
[0]->n_div
;
9772 n_eq
= list
->p
[0]->n_eq
;
9773 n_ineq
= list
->p
[0]->n_ineq
;
9774 for (i
= 1; i
< list
->n
; ++i
) {
9775 isl_assert(list
->ctx
, list
->p
[i
], goto error
);
9776 isl_assert(list
->ctx
,
9777 nparam
== isl_basic_set_n_param(list
->p
[i
]), goto error
);
9778 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
9779 extra
+= list
->p
[i
]->n_div
;
9780 n_eq
+= list
->p
[i
]->n_eq
;
9781 n_ineq
+= list
->p
[i
]->n_ineq
;
9783 product
= isl_basic_set_alloc(list
->ctx
, nparam
, dim
, extra
,
9788 for (i
= 0; i
< list
->n
; ++i
) {
9789 isl_basic_set_add_constraints(product
,
9790 isl_basic_set_copy(list
->p
[i
]), dim
);
9791 dim
+= isl_basic_set_n_dim(list
->p
[i
]);
9793 isl_basic_set_list_free(list
);
9796 isl_basic_set_free(product
);
9797 isl_basic_set_list_free(list
);
9801 struct isl_basic_map
*isl_basic_map_product(
9802 struct isl_basic_map
*bmap1
, struct isl_basic_map
*bmap2
)
9804 isl_space
*dim_result
= NULL
;
9805 struct isl_basic_map
*bmap
;
9806 unsigned in1
, in2
, out1
, out2
, nparam
, total
, pos
;
9807 struct isl_dim_map
*dim_map1
, *dim_map2
;
9809 if (!bmap1
|| !bmap2
)
9812 isl_assert(bmap1
->ctx
, isl_space_match(bmap1
->dim
, isl_dim_param
,
9813 bmap2
->dim
, isl_dim_param
), goto error
);
9814 dim_result
= isl_space_product(isl_space_copy(bmap1
->dim
),
9815 isl_space_copy(bmap2
->dim
));
9817 in1
= isl_basic_map_n_in(bmap1
);
9818 in2
= isl_basic_map_n_in(bmap2
);
9819 out1
= isl_basic_map_n_out(bmap1
);
9820 out2
= isl_basic_map_n_out(bmap2
);
9821 nparam
= isl_basic_map_n_param(bmap1
);
9823 total
= nparam
+ in1
+ in2
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
9824 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9825 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9826 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9827 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9828 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9829 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
9830 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
9831 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
9832 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
9833 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9835 bmap
= isl_basic_map_alloc_space(dim_result
,
9836 bmap1
->n_div
+ bmap2
->n_div
,
9837 bmap1
->n_eq
+ bmap2
->n_eq
,
9838 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9839 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9840 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9841 bmap
= isl_basic_map_simplify(bmap
);
9842 return isl_basic_map_finalize(bmap
);
9844 isl_basic_map_free(bmap1
);
9845 isl_basic_map_free(bmap2
);
9849 __isl_give isl_basic_map
*isl_basic_map_flat_product(
9850 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9852 isl_basic_map
*prod
;
9854 prod
= isl_basic_map_product(bmap1
, bmap2
);
9855 prod
= isl_basic_map_flatten(prod
);
9859 __isl_give isl_basic_set
*isl_basic_set_flat_product(
9860 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
9862 return isl_basic_map_flat_range_product(bset1
, bset2
);
9865 __isl_give isl_basic_map
*isl_basic_map_domain_product(
9866 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9868 isl_space
*space_result
= NULL
;
9869 isl_basic_map
*bmap
;
9870 unsigned in1
, in2
, out
, nparam
, total
, pos
;
9871 struct isl_dim_map
*dim_map1
, *dim_map2
;
9873 if (!bmap1
|| !bmap2
)
9876 space_result
= isl_space_domain_product(isl_space_copy(bmap1
->dim
),
9877 isl_space_copy(bmap2
->dim
));
9879 in1
= isl_basic_map_dim(bmap1
, isl_dim_in
);
9880 in2
= isl_basic_map_dim(bmap2
, isl_dim_in
);
9881 out
= isl_basic_map_dim(bmap1
, isl_dim_out
);
9882 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
9884 total
= nparam
+ in1
+ in2
+ out
+ bmap1
->n_div
+ bmap2
->n_div
;
9885 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9886 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9887 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9888 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9889 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9890 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
9891 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
9892 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
);
9893 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out
);
9894 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9896 bmap
= isl_basic_map_alloc_space(space_result
,
9897 bmap1
->n_div
+ bmap2
->n_div
,
9898 bmap1
->n_eq
+ bmap2
->n_eq
,
9899 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9900 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9901 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9902 bmap
= isl_basic_map_simplify(bmap
);
9903 return isl_basic_map_finalize(bmap
);
9905 isl_basic_map_free(bmap1
);
9906 isl_basic_map_free(bmap2
);
9910 __isl_give isl_basic_map
*isl_basic_map_range_product(
9911 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9914 isl_space
*dim_result
= NULL
;
9915 isl_basic_map
*bmap
;
9916 unsigned in
, out1
, out2
, nparam
, total
, pos
;
9917 struct isl_dim_map
*dim_map1
, *dim_map2
;
9919 rational
= isl_basic_map_is_rational(bmap1
);
9920 if (rational
>= 0 && rational
)
9921 rational
= isl_basic_map_is_rational(bmap2
);
9922 if (!bmap1
|| !bmap2
|| rational
< 0)
9925 if (!isl_space_match(bmap1
->dim
, isl_dim_param
,
9926 bmap2
->dim
, isl_dim_param
))
9927 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
9928 "parameters don't match", goto error
);
9930 dim_result
= isl_space_range_product(isl_space_copy(bmap1
->dim
),
9931 isl_space_copy(bmap2
->dim
));
9933 in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
9934 out1
= isl_basic_map_n_out(bmap1
);
9935 out2
= isl_basic_map_n_out(bmap2
);
9936 nparam
= isl_basic_map_n_param(bmap1
);
9938 total
= nparam
+ in
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
9939 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9940 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
9941 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
9942 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
9943 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
9944 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
9945 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in
);
9946 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
9947 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
9948 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
9950 bmap
= isl_basic_map_alloc_space(dim_result
,
9951 bmap1
->n_div
+ bmap2
->n_div
,
9952 bmap1
->n_eq
+ bmap2
->n_eq
,
9953 bmap1
->n_ineq
+ bmap2
->n_ineq
);
9954 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
9955 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
9957 bmap
= isl_basic_map_set_rational(bmap
);
9958 bmap
= isl_basic_map_simplify(bmap
);
9959 return isl_basic_map_finalize(bmap
);
9961 isl_basic_map_free(bmap1
);
9962 isl_basic_map_free(bmap2
);
9966 __isl_give isl_basic_map
*isl_basic_map_flat_range_product(
9967 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
9969 isl_basic_map
*prod
;
9971 prod
= isl_basic_map_range_product(bmap1
, bmap2
);
9972 prod
= isl_basic_map_flatten_range(prod
);
9976 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
9977 * and collect the results.
9978 * The result live in the space obtained by calling "space_product"
9979 * on the spaces of "map1" and "map2".
9980 * If "remove_duplicates" is set then the result may contain duplicates
9981 * (even if the inputs do not) and so we try and remove the obvious
9984 static __isl_give isl_map
*map_product(__isl_take isl_map
*map1
,
9985 __isl_take isl_map
*map2
,
9986 __isl_give isl_space
*(*space_product
)(__isl_take isl_space
*left
,
9987 __isl_take isl_space
*right
),
9988 __isl_give isl_basic_map
*(*basic_map_product
)(
9989 __isl_take isl_basic_map
*left
,
9990 __isl_take isl_basic_map
*right
),
9991 int remove_duplicates
)
9994 struct isl_map
*result
;
10000 isl_assert(map1
->ctx
, isl_space_match(map1
->dim
, isl_dim_param
,
10001 map2
->dim
, isl_dim_param
), goto error
);
10003 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
10004 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
10005 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
10007 result
= isl_map_alloc_space(space_product(isl_space_copy(map1
->dim
),
10008 isl_space_copy(map2
->dim
)),
10009 map1
->n
* map2
->n
, flags
);
10012 for (i
= 0; i
< map1
->n
; ++i
)
10013 for (j
= 0; j
< map2
->n
; ++j
) {
10014 struct isl_basic_map
*part
;
10015 part
= basic_map_product(isl_basic_map_copy(map1
->p
[i
]),
10016 isl_basic_map_copy(map2
->p
[j
]));
10017 if (isl_basic_map_is_empty(part
))
10018 isl_basic_map_free(part
);
10020 result
= isl_map_add_basic_map(result
, part
);
10024 if (remove_duplicates
)
10025 result
= isl_map_remove_obvious_duplicates(result
);
10026 isl_map_free(map1
);
10027 isl_map_free(map2
);
10030 isl_map_free(map1
);
10031 isl_map_free(map2
);
10035 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
10037 static __isl_give isl_map
*map_product_aligned(__isl_take isl_map
*map1
,
10038 __isl_take isl_map
*map2
)
10040 return map_product(map1
, map2
, &isl_space_product
,
10041 &isl_basic_map_product
, 0);
10044 __isl_give isl_map
*isl_map_product(__isl_take isl_map
*map1
,
10045 __isl_take isl_map
*map2
)
10047 return isl_map_align_params_map_map_and(map1
, map2
, &map_product_aligned
);
10050 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
10052 __isl_give isl_map
*isl_map_flat_product(__isl_take isl_map
*map1
,
10053 __isl_take isl_map
*map2
)
10057 prod
= isl_map_product(map1
, map2
);
10058 prod
= isl_map_flatten(prod
);
10062 /* Given two set A and B, construct its Cartesian product A x B.
10064 struct isl_set
*isl_set_product(struct isl_set
*set1
, struct isl_set
*set2
)
10066 return isl_map_range_product(set1
, set2
);
10069 __isl_give isl_set
*isl_set_flat_product(__isl_take isl_set
*set1
,
10070 __isl_take isl_set
*set2
)
10072 return isl_map_flat_range_product(set1
, set2
);
10075 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
10077 static __isl_give isl_map
*map_domain_product_aligned(__isl_take isl_map
*map1
,
10078 __isl_take isl_map
*map2
)
10080 return map_product(map1
, map2
, &isl_space_domain_product
,
10081 &isl_basic_map_domain_product
, 1);
10084 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
10086 static __isl_give isl_map
*map_range_product_aligned(__isl_take isl_map
*map1
,
10087 __isl_take isl_map
*map2
)
10089 return map_product(map1
, map2
, &isl_space_range_product
,
10090 &isl_basic_map_range_product
, 1);
10093 __isl_give isl_map
*isl_map_domain_product(__isl_take isl_map
*map1
,
10094 __isl_take isl_map
*map2
)
10096 return isl_map_align_params_map_map_and(map1
, map2
,
10097 &map_domain_product_aligned
);
10100 __isl_give isl_map
*isl_map_range_product(__isl_take isl_map
*map1
,
10101 __isl_take isl_map
*map2
)
10103 return isl_map_align_params_map_map_and(map1
, map2
,
10104 &map_range_product_aligned
);
10107 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
10109 __isl_give isl_map
*isl_map_factor_domain(__isl_take isl_map
*map
)
10112 int total1
, keep1
, total2
, keep2
;
10116 if (!isl_space_domain_is_wrapping(map
->dim
) ||
10117 !isl_space_range_is_wrapping(map
->dim
))
10118 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10119 "not a product", return isl_map_free(map
));
10121 space
= isl_map_get_space(map
);
10122 total1
= isl_space_dim(space
, isl_dim_in
);
10123 total2
= isl_space_dim(space
, isl_dim_out
);
10124 space
= isl_space_factor_domain(space
);
10125 keep1
= isl_space_dim(space
, isl_dim_in
);
10126 keep2
= isl_space_dim(space
, isl_dim_out
);
10127 map
= isl_map_project_out(map
, isl_dim_in
, keep1
, total1
- keep1
);
10128 map
= isl_map_project_out(map
, isl_dim_out
, keep2
, total2
- keep2
);
10129 map
= isl_map_reset_space(map
, space
);
10134 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
10136 __isl_give isl_map
*isl_map_factor_range(__isl_take isl_map
*map
)
10139 int total1
, keep1
, total2
, keep2
;
10143 if (!isl_space_domain_is_wrapping(map
->dim
) ||
10144 !isl_space_range_is_wrapping(map
->dim
))
10145 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10146 "not a product", return isl_map_free(map
));
10148 space
= isl_map_get_space(map
);
10149 total1
= isl_space_dim(space
, isl_dim_in
);
10150 total2
= isl_space_dim(space
, isl_dim_out
);
10151 space
= isl_space_factor_range(space
);
10152 keep1
= isl_space_dim(space
, isl_dim_in
);
10153 keep2
= isl_space_dim(space
, isl_dim_out
);
10154 map
= isl_map_project_out(map
, isl_dim_in
, 0, total1
- keep1
);
10155 map
= isl_map_project_out(map
, isl_dim_out
, 0, total2
- keep2
);
10156 map
= isl_map_reset_space(map
, space
);
10161 /* Given a map of the form [A -> B] -> C, return the map A -> C.
10163 __isl_give isl_map
*isl_map_domain_factor_domain(__isl_take isl_map
*map
)
10170 if (!isl_space_domain_is_wrapping(map
->dim
))
10171 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10172 "domain is not a product", return isl_map_free(map
));
10174 space
= isl_map_get_space(map
);
10175 total
= isl_space_dim(space
, isl_dim_in
);
10176 space
= isl_space_domain_factor_domain(space
);
10177 keep
= isl_space_dim(space
, isl_dim_in
);
10178 map
= isl_map_project_out(map
, isl_dim_in
, keep
, total
- keep
);
10179 map
= isl_map_reset_space(map
, space
);
10184 /* Given a map of the form [A -> B] -> C, return the map B -> C.
10186 __isl_give isl_map
*isl_map_domain_factor_range(__isl_take isl_map
*map
)
10193 if (!isl_space_domain_is_wrapping(map
->dim
))
10194 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10195 "domain is not a product", return isl_map_free(map
));
10197 space
= isl_map_get_space(map
);
10198 total
= isl_space_dim(space
, isl_dim_in
);
10199 space
= isl_space_domain_factor_range(space
);
10200 keep
= isl_space_dim(space
, isl_dim_in
);
10201 map
= isl_map_project_out(map
, isl_dim_in
, 0, total
- keep
);
10202 map
= isl_map_reset_space(map
, space
);
10207 /* Given a map A -> [B -> C], extract the map A -> B.
10209 __isl_give isl_map
*isl_map_range_factor_domain(__isl_take isl_map
*map
)
10216 if (!isl_space_range_is_wrapping(map
->dim
))
10217 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10218 "range is not a product", return isl_map_free(map
));
10220 space
= isl_map_get_space(map
);
10221 total
= isl_space_dim(space
, isl_dim_out
);
10222 space
= isl_space_range_factor_domain(space
);
10223 keep
= isl_space_dim(space
, isl_dim_out
);
10224 map
= isl_map_project_out(map
, isl_dim_out
, keep
, total
- keep
);
10225 map
= isl_map_reset_space(map
, space
);
10230 /* Given a map A -> [B -> C], extract the map A -> C.
10232 __isl_give isl_map
*isl_map_range_factor_range(__isl_take isl_map
*map
)
10239 if (!isl_space_range_is_wrapping(map
->dim
))
10240 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10241 "range is not a product", return isl_map_free(map
));
10243 space
= isl_map_get_space(map
);
10244 total
= isl_space_dim(space
, isl_dim_out
);
10245 space
= isl_space_range_factor_range(space
);
10246 keep
= isl_space_dim(space
, isl_dim_out
);
10247 map
= isl_map_project_out(map
, isl_dim_out
, 0, total
- keep
);
10248 map
= isl_map_reset_space(map
, space
);
10253 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
10255 __isl_give isl_map
*isl_map_flat_domain_product(__isl_take isl_map
*map1
,
10256 __isl_take isl_map
*map2
)
10260 prod
= isl_map_domain_product(map1
, map2
);
10261 prod
= isl_map_flatten_domain(prod
);
10265 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
10267 __isl_give isl_map
*isl_map_flat_range_product(__isl_take isl_map
*map1
,
10268 __isl_take isl_map
*map2
)
10272 prod
= isl_map_range_product(map1
, map2
);
10273 prod
= isl_map_flatten_range(prod
);
10277 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map
*bmap
)
10280 uint32_t hash
= isl_hash_init();
10285 bmap
= isl_basic_map_copy(bmap
);
10286 bmap
= isl_basic_map_normalize(bmap
);
10289 total
= isl_basic_map_total_dim(bmap
);
10290 isl_hash_byte(hash
, bmap
->n_eq
& 0xFF);
10291 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
10293 c_hash
= isl_seq_get_hash(bmap
->eq
[i
], 1 + total
);
10294 isl_hash_hash(hash
, c_hash
);
10296 isl_hash_byte(hash
, bmap
->n_ineq
& 0xFF);
10297 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
10299 c_hash
= isl_seq_get_hash(bmap
->ineq
[i
], 1 + total
);
10300 isl_hash_hash(hash
, c_hash
);
10302 isl_hash_byte(hash
, bmap
->n_div
& 0xFF);
10303 for (i
= 0; i
< bmap
->n_div
; ++i
) {
10305 if (isl_int_is_zero(bmap
->div
[i
][0]))
10307 isl_hash_byte(hash
, i
& 0xFF);
10308 c_hash
= isl_seq_get_hash(bmap
->div
[i
], 1 + 1 + total
);
10309 isl_hash_hash(hash
, c_hash
);
10311 isl_basic_map_free(bmap
);
10315 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set
*bset
)
10317 return isl_basic_map_get_hash(bset_to_bmap(bset
));
10320 uint32_t isl_map_get_hash(__isl_keep isl_map
*map
)
10327 map
= isl_map_copy(map
);
10328 map
= isl_map_normalize(map
);
10332 hash
= isl_hash_init();
10333 for (i
= 0; i
< map
->n
; ++i
) {
10334 uint32_t bmap_hash
;
10335 bmap_hash
= isl_basic_map_get_hash(map
->p
[i
]);
10336 isl_hash_hash(hash
, bmap_hash
);
10344 uint32_t isl_set_get_hash(__isl_keep isl_set
*set
)
10346 return isl_map_get_hash(set_to_map(set
));
10349 /* Check if the value for dimension dim is completely determined
10350 * by the values of the other parameters and variables.
10351 * That is, check if dimension dim is involved in an equality.
10353 int isl_basic_set_dim_is_unique(struct isl_basic_set
*bset
, unsigned dim
)
10360 nparam
= isl_basic_set_n_param(bset
);
10361 for (i
= 0; i
< bset
->n_eq
; ++i
)
10362 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ dim
]))
10367 /* Check if the value for dimension dim is completely determined
10368 * by the values of the other parameters and variables.
10369 * That is, check if dimension dim is involved in an equality
10370 * for each of the subsets.
10372 int isl_set_dim_is_unique(struct isl_set
*set
, unsigned dim
)
10378 for (i
= 0; i
< set
->n
; ++i
) {
10380 unique
= isl_basic_set_dim_is_unique(set
->p
[i
], dim
);
10387 /* Return the number of basic maps in the (current) representation of "map".
10389 int isl_map_n_basic_map(__isl_keep isl_map
*map
)
10391 return map
? map
->n
: 0;
10394 int isl_set_n_basic_set(__isl_keep isl_set
*set
)
10396 return set
? set
->n
: 0;
10399 isl_stat
isl_map_foreach_basic_map(__isl_keep isl_map
*map
,
10400 isl_stat (*fn
)(__isl_take isl_basic_map
*bmap
, void *user
), void *user
)
10405 return isl_stat_error
;
10407 for (i
= 0; i
< map
->n
; ++i
)
10408 if (fn(isl_basic_map_copy(map
->p
[i
]), user
) < 0)
10409 return isl_stat_error
;
10411 return isl_stat_ok
;
10414 isl_stat
isl_set_foreach_basic_set(__isl_keep isl_set
*set
,
10415 isl_stat (*fn
)(__isl_take isl_basic_set
*bset
, void *user
), void *user
)
10420 return isl_stat_error
;
10422 for (i
= 0; i
< set
->n
; ++i
)
10423 if (fn(isl_basic_set_copy(set
->p
[i
]), user
) < 0)
10424 return isl_stat_error
;
10426 return isl_stat_ok
;
10429 /* Return a list of basic sets, the union of which is equal to "set".
10431 __isl_give isl_basic_set_list
*isl_set_get_basic_set_list(
10432 __isl_keep isl_set
*set
)
10435 isl_basic_set_list
*list
;
10440 list
= isl_basic_set_list_alloc(isl_set_get_ctx(set
), set
->n
);
10441 for (i
= 0; i
< set
->n
; ++i
) {
10442 isl_basic_set
*bset
;
10444 bset
= isl_basic_set_copy(set
->p
[i
]);
10445 list
= isl_basic_set_list_add(list
, bset
);
10451 __isl_give isl_basic_set
*isl_basic_set_lift(__isl_take isl_basic_set
*bset
)
10458 bset
= isl_basic_set_cow(bset
);
10462 dim
= isl_basic_set_get_space(bset
);
10463 dim
= isl_space_lift(dim
, bset
->n_div
);
10466 isl_space_free(bset
->dim
);
10468 bset
->extra
-= bset
->n_div
;
10471 bset
= isl_basic_set_finalize(bset
);
10475 isl_basic_set_free(bset
);
10479 __isl_give isl_set
*isl_set_lift(__isl_take isl_set
*set
)
10485 set
= isl_set_align_divs(set
);
10490 set
= isl_set_cow(set
);
10494 n_div
= set
->p
[0]->n_div
;
10495 dim
= isl_set_get_space(set
);
10496 dim
= isl_space_lift(dim
, n_div
);
10499 isl_space_free(set
->dim
);
10502 for (i
= 0; i
< set
->n
; ++i
) {
10503 set
->p
[i
] = isl_basic_set_lift(set
->p
[i
]);
10514 __isl_give isl_map
*isl_set_lifting(__isl_take isl_set
*set
)
10517 struct isl_basic_map
*bmap
;
10524 set
= isl_set_align_divs(set
);
10529 dim
= isl_set_get_space(set
);
10530 if (set
->n
== 0 || set
->p
[0]->n_div
== 0) {
10532 return isl_map_identity(isl_space_map_from_set(dim
));
10535 n_div
= set
->p
[0]->n_div
;
10536 dim
= isl_space_map_from_set(dim
);
10537 n_param
= isl_space_dim(dim
, isl_dim_param
);
10538 n_set
= isl_space_dim(dim
, isl_dim_in
);
10539 dim
= isl_space_extend(dim
, n_param
, n_set
, n_set
+ n_div
);
10540 bmap
= isl_basic_map_alloc_space(dim
, 0, n_set
, 2 * n_div
);
10541 for (i
= 0; i
< n_set
; ++i
)
10542 bmap
= var_equal(bmap
, i
);
10544 total
= n_param
+ n_set
+ n_set
+ n_div
;
10545 for (i
= 0; i
< n_div
; ++i
) {
10546 k
= isl_basic_map_alloc_inequality(bmap
);
10549 isl_seq_cpy(bmap
->ineq
[k
], set
->p
[0]->div
[i
]+1, 1+n_param
);
10550 isl_seq_clr(bmap
->ineq
[k
]+1+n_param
, n_set
);
10551 isl_seq_cpy(bmap
->ineq
[k
]+1+n_param
+n_set
,
10552 set
->p
[0]->div
[i
]+1+1+n_param
, n_set
+ n_div
);
10553 isl_int_neg(bmap
->ineq
[k
][1+n_param
+n_set
+n_set
+i
],
10554 set
->p
[0]->div
[i
][0]);
10556 l
= isl_basic_map_alloc_inequality(bmap
);
10559 isl_seq_neg(bmap
->ineq
[l
], bmap
->ineq
[k
], 1 + total
);
10560 isl_int_add(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0],
10561 set
->p
[0]->div
[i
][0]);
10562 isl_int_sub_ui(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], 1);
10566 bmap
= isl_basic_map_simplify(bmap
);
10567 bmap
= isl_basic_map_finalize(bmap
);
10568 return isl_map_from_basic_map(bmap
);
10571 isl_basic_map_free(bmap
);
10575 int isl_basic_set_size(__isl_keep isl_basic_set
*bset
)
10583 dim
= isl_basic_set_total_dim(bset
);
10584 size
+= bset
->n_eq
* (1 + dim
);
10585 size
+= bset
->n_ineq
* (1 + dim
);
10586 size
+= bset
->n_div
* (2 + dim
);
10591 int isl_set_size(__isl_keep isl_set
*set
)
10599 for (i
= 0; i
< set
->n
; ++i
)
10600 size
+= isl_basic_set_size(set
->p
[i
]);
10605 /* Check if there is any lower bound (if lower == 0) and/or upper
10606 * bound (if upper == 0) on the specified dim.
10608 static isl_bool
basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
10609 enum isl_dim_type type
, unsigned pos
, int lower
, int upper
)
10613 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
10614 return isl_bool_error
;
10616 pos
+= isl_basic_map_offset(bmap
, type
);
10618 for (i
= 0; i
< bmap
->n_div
; ++i
) {
10619 if (isl_int_is_zero(bmap
->div
[i
][0]))
10621 if (!isl_int_is_zero(bmap
->div
[i
][1 + pos
]))
10622 return isl_bool_true
;
10625 for (i
= 0; i
< bmap
->n_eq
; ++i
)
10626 if (!isl_int_is_zero(bmap
->eq
[i
][pos
]))
10627 return isl_bool_true
;
10629 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
10630 int sgn
= isl_int_sgn(bmap
->ineq
[i
][pos
]);
10637 return lower
&& upper
;
10640 isl_bool
isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
10641 enum isl_dim_type type
, unsigned pos
)
10643 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 0);
10646 isl_bool
isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map
*bmap
,
10647 enum isl_dim_type type
, unsigned pos
)
10649 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 1);
10652 isl_bool
isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map
*bmap
,
10653 enum isl_dim_type type
, unsigned pos
)
10655 return basic_map_dim_is_bounded(bmap
, type
, pos
, 1, 0);
10658 isl_bool
isl_map_dim_is_bounded(__isl_keep isl_map
*map
,
10659 enum isl_dim_type type
, unsigned pos
)
10664 return isl_bool_error
;
10666 for (i
= 0; i
< map
->n
; ++i
) {
10668 bounded
= isl_basic_map_dim_is_bounded(map
->p
[i
], type
, pos
);
10669 if (bounded
< 0 || !bounded
)
10673 return isl_bool_true
;
10676 /* Return true if the specified dim is involved in both an upper bound
10677 * and a lower bound.
10679 isl_bool
isl_set_dim_is_bounded(__isl_keep isl_set
*set
,
10680 enum isl_dim_type type
, unsigned pos
)
10682 return isl_map_dim_is_bounded(set_to_map(set
), type
, pos
);
10685 /* Does "map" have a bound (according to "fn") for any of its basic maps?
10687 static isl_bool
has_any_bound(__isl_keep isl_map
*map
,
10688 enum isl_dim_type type
, unsigned pos
,
10689 isl_bool (*fn
)(__isl_keep isl_basic_map
*bmap
,
10690 enum isl_dim_type type
, unsigned pos
))
10695 return isl_bool_error
;
10697 for (i
= 0; i
< map
->n
; ++i
) {
10699 bounded
= fn(map
->p
[i
], type
, pos
);
10700 if (bounded
< 0 || bounded
)
10704 return isl_bool_false
;
10707 /* Return 1 if the specified dim is involved in any lower bound.
10709 isl_bool
isl_set_dim_has_any_lower_bound(__isl_keep isl_set
*set
,
10710 enum isl_dim_type type
, unsigned pos
)
10712 return has_any_bound(set
, type
, pos
,
10713 &isl_basic_map_dim_has_lower_bound
);
10716 /* Return 1 if the specified dim is involved in any upper bound.
10718 isl_bool
isl_set_dim_has_any_upper_bound(__isl_keep isl_set
*set
,
10719 enum isl_dim_type type
, unsigned pos
)
10721 return has_any_bound(set
, type
, pos
,
10722 &isl_basic_map_dim_has_upper_bound
);
10725 /* Does "map" have a bound (according to "fn") for all of its basic maps?
10727 static isl_bool
has_bound(__isl_keep isl_map
*map
,
10728 enum isl_dim_type type
, unsigned pos
,
10729 isl_bool (*fn
)(__isl_keep isl_basic_map
*bmap
,
10730 enum isl_dim_type type
, unsigned pos
))
10735 return isl_bool_error
;
10737 for (i
= 0; i
< map
->n
; ++i
) {
10739 bounded
= fn(map
->p
[i
], type
, pos
);
10740 if (bounded
< 0 || !bounded
)
10744 return isl_bool_true
;
10747 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10749 isl_bool
isl_set_dim_has_lower_bound(__isl_keep isl_set
*set
,
10750 enum isl_dim_type type
, unsigned pos
)
10752 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_lower_bound
);
10755 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10757 isl_bool
isl_set_dim_has_upper_bound(__isl_keep isl_set
*set
,
10758 enum isl_dim_type type
, unsigned pos
)
10760 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_upper_bound
);
10763 /* For each of the "n" variables starting at "first", determine
10764 * the sign of the variable and put the results in the first "n"
10765 * elements of the array "signs".
10767 * 1 means that the variable is non-negative
10768 * -1 means that the variable is non-positive
10769 * 0 means the variable attains both positive and negative values.
10771 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set
*bset
,
10772 unsigned first
, unsigned n
, int *signs
)
10774 isl_vec
*bound
= NULL
;
10775 struct isl_tab
*tab
= NULL
;
10776 struct isl_tab_undo
*snap
;
10779 if (!bset
|| !signs
)
10782 bound
= isl_vec_alloc(bset
->ctx
, 1 + isl_basic_set_total_dim(bset
));
10783 tab
= isl_tab_from_basic_set(bset
, 0);
10784 if (!bound
|| !tab
)
10787 isl_seq_clr(bound
->el
, bound
->size
);
10788 isl_int_set_si(bound
->el
[0], -1);
10790 snap
= isl_tab_snap(tab
);
10791 for (i
= 0; i
< n
; ++i
) {
10794 isl_int_set_si(bound
->el
[1 + first
+ i
], -1);
10795 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
10797 empty
= tab
->empty
;
10798 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
10799 if (isl_tab_rollback(tab
, snap
) < 0)
10807 isl_int_set_si(bound
->el
[1 + first
+ i
], 1);
10808 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
10810 empty
= tab
->empty
;
10811 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
10812 if (isl_tab_rollback(tab
, snap
) < 0)
10815 signs
[i
] = empty
? -1 : 0;
10819 isl_vec_free(bound
);
10823 isl_vec_free(bound
);
10827 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set
*bset
,
10828 enum isl_dim_type type
, unsigned first
, unsigned n
, int *signs
)
10830 if (!bset
|| !signs
)
10832 isl_assert(bset
->ctx
, first
+ n
<= isl_basic_set_dim(bset
, type
),
10835 first
+= pos(bset
->dim
, type
) - 1;
10836 return isl_basic_set_vars_get_sign(bset
, first
, n
, signs
);
10839 /* Is it possible for the integer division "div" to depend (possibly
10840 * indirectly) on any output dimensions?
10842 * If the div is undefined, then we conservatively assume that it
10843 * may depend on them.
10844 * Otherwise, we check if it actually depends on them or on any integer
10845 * divisions that may depend on them.
10847 static isl_bool
div_may_involve_output(__isl_keep isl_basic_map
*bmap
, int div
)
10850 unsigned n_out
, o_out
;
10851 unsigned n_div
, o_div
;
10853 if (isl_int_is_zero(bmap
->div
[div
][0]))
10854 return isl_bool_true
;
10856 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10857 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10859 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + o_out
, n_out
) != -1)
10860 return isl_bool_true
;
10862 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10863 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10865 for (i
= 0; i
< n_div
; ++i
) {
10866 isl_bool may_involve
;
10868 if (isl_int_is_zero(bmap
->div
[div
][1 + o_div
+ i
]))
10870 may_involve
= div_may_involve_output(bmap
, i
);
10871 if (may_involve
< 0 || may_involve
)
10872 return may_involve
;
10875 return isl_bool_false
;
10878 /* Return the first integer division of "bmap" in the range
10879 * [first, first + n[ that may depend on any output dimensions and
10880 * that has a non-zero coefficient in "c" (where the first coefficient
10881 * in "c" corresponds to integer division "first").
10883 static int first_div_may_involve_output(__isl_keep isl_basic_map
*bmap
,
10884 isl_int
*c
, int first
, int n
)
10891 for (k
= first
; k
< first
+ n
; ++k
) {
10892 isl_bool may_involve
;
10894 if (isl_int_is_zero(c
[k
]))
10896 may_involve
= div_may_involve_output(bmap
, k
);
10897 if (may_involve
< 0)
10906 /* Look for a pair of inequality constraints in "bmap" of the form
10908 * -l + i >= 0 or i >= l
10910 * n + l - i >= 0 or i <= l + n
10912 * with n < "m" and i the output dimension at position "pos".
10913 * (Note that n >= 0 as otherwise the two constraints would conflict.)
10914 * Furthermore, "l" is only allowed to involve parameters, input dimensions
10915 * and earlier output dimensions, as well as integer divisions that do
10916 * not involve any of the output dimensions.
10918 * Return the index of the first inequality constraint or bmap->n_ineq
10919 * if no such pair can be found.
10921 static int find_modulo_constraint_pair(__isl_keep isl_basic_map
*bmap
,
10922 int pos
, isl_int m
)
10927 unsigned n_div
, o_div
;
10928 unsigned n_out
, o_out
;
10934 ctx
= isl_basic_map_get_ctx(bmap
);
10935 total
= isl_basic_map_total_dim(bmap
);
10936 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
10937 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
10938 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
10939 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
10940 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
10941 if (!isl_int_abs_eq(bmap
->ineq
[i
][o_out
+ pos
], ctx
->one
))
10943 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + o_out
+ pos
+ 1,
10944 n_out
- (pos
+ 1)) != -1)
10946 if (first_div_may_involve_output(bmap
, bmap
->ineq
[i
] + o_div
,
10949 for (j
= i
+ 1; j
< bmap
->n_ineq
; ++j
) {
10950 if (!isl_int_abs_eq(bmap
->ineq
[j
][o_out
+ pos
],
10953 if (!isl_seq_is_neg(bmap
->ineq
[i
] + 1,
10954 bmap
->ineq
[j
] + 1, total
))
10958 if (j
>= bmap
->n_ineq
)
10960 isl_int_add(bmap
->ineq
[i
][0],
10961 bmap
->ineq
[i
][0], bmap
->ineq
[j
][0]);
10962 less
= isl_int_abs_lt(bmap
->ineq
[i
][0], m
);
10963 isl_int_sub(bmap
->ineq
[i
][0],
10964 bmap
->ineq
[i
][0], bmap
->ineq
[j
][0]);
10967 if (isl_int_is_one(bmap
->ineq
[i
][o_out
+ pos
]))
10973 return bmap
->n_ineq
;
10976 /* Return the index of the equality of "bmap" that defines
10977 * the output dimension "pos" in terms of earlier dimensions.
10978 * The equality may also involve integer divisions, as long
10979 * as those integer divisions are defined in terms of
10980 * parameters or input dimensions.
10981 * In this case, *div is set to the number of integer divisions and
10982 * *ineq is set to the number of inequality constraints (provided
10983 * div and ineq are not NULL).
10985 * The equality may also involve a single integer division involving
10986 * the output dimensions (typically only output dimension "pos") as
10987 * long as the coefficient of output dimension "pos" is 1 or -1 and
10988 * there is a pair of constraints i >= l and i <= l + n, with i referring
10989 * to output dimension "pos", l an expression involving only earlier
10990 * dimensions and n smaller than the coefficient of the integer division
10991 * in the equality. In this case, the output dimension can be defined
10992 * in terms of a modulo expression that does not involve the integer division.
10993 * *div is then set to this single integer division and
10994 * *ineq is set to the index of constraint i >= l.
10996 * Return bmap->n_eq if there is no such equality.
10997 * Return -1 on error.
10999 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map
*bmap
,
11000 int pos
, int *div
, int *ineq
)
11003 unsigned n_out
, o_out
;
11004 unsigned n_div
, o_div
;
11009 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
11010 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
11011 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
11012 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
11015 *ineq
= bmap
->n_ineq
;
11018 for (j
= 0; j
< bmap
->n_eq
; ++j
) {
11019 if (isl_int_is_zero(bmap
->eq
[j
][o_out
+ pos
]))
11021 if (isl_seq_first_non_zero(bmap
->eq
[j
] + o_out
+ pos
+ 1,
11022 n_out
- (pos
+ 1)) != -1)
11024 k
= first_div_may_involve_output(bmap
, bmap
->eq
[j
] + o_div
,
11028 if (!isl_int_is_one(bmap
->eq
[j
][o_out
+ pos
]) &&
11029 !isl_int_is_negone(bmap
->eq
[j
][o_out
+ pos
]))
11031 if (first_div_may_involve_output(bmap
, bmap
->eq
[j
] + o_div
,
11032 k
+ 1, n_div
- (k
+1)) < n_div
)
11034 l
= find_modulo_constraint_pair(bmap
, pos
,
11035 bmap
->eq
[j
][o_div
+ k
]);
11038 if (l
>= bmap
->n_ineq
)
11050 /* Check if the given basic map is obviously single-valued.
11051 * In particular, for each output dimension, check that there is
11052 * an equality that defines the output dimension in terms of
11053 * earlier dimensions.
11055 isl_bool
isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map
*bmap
)
11061 return isl_bool_error
;
11063 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
11065 for (i
= 0; i
< n_out
; ++i
) {
11068 eq
= isl_basic_map_output_defining_equality(bmap
, i
,
11071 return isl_bool_error
;
11072 if (eq
>= bmap
->n_eq
)
11073 return isl_bool_false
;
11076 return isl_bool_true
;
11079 /* Check if the given basic map is single-valued.
11080 * We simply compute
11084 * and check if the result is a subset of the identity mapping.
11086 isl_bool
isl_basic_map_is_single_valued(__isl_keep isl_basic_map
*bmap
)
11089 isl_basic_map
*test
;
11093 sv
= isl_basic_map_plain_is_single_valued(bmap
);
11097 test
= isl_basic_map_reverse(isl_basic_map_copy(bmap
));
11098 test
= isl_basic_map_apply_range(test
, isl_basic_map_copy(bmap
));
11100 space
= isl_basic_map_get_space(bmap
);
11101 space
= isl_space_map_from_set(isl_space_range(space
));
11102 id
= isl_basic_map_identity(space
);
11104 sv
= isl_basic_map_is_subset(test
, id
);
11106 isl_basic_map_free(test
);
11107 isl_basic_map_free(id
);
11112 /* Check if the given map is obviously single-valued.
11114 isl_bool
isl_map_plain_is_single_valued(__isl_keep isl_map
*map
)
11117 return isl_bool_error
;
11119 return isl_bool_true
;
11121 return isl_bool_false
;
11123 return isl_basic_map_plain_is_single_valued(map
->p
[0]);
11126 /* Check if the given map is single-valued.
11127 * We simply compute
11131 * and check if the result is a subset of the identity mapping.
11133 isl_bool
isl_map_is_single_valued(__isl_keep isl_map
*map
)
11140 sv
= isl_map_plain_is_single_valued(map
);
11144 test
= isl_map_reverse(isl_map_copy(map
));
11145 test
= isl_map_apply_range(test
, isl_map_copy(map
));
11147 dim
= isl_space_map_from_set(isl_space_range(isl_map_get_space(map
)));
11148 id
= isl_map_identity(dim
);
11150 sv
= isl_map_is_subset(test
, id
);
11152 isl_map_free(test
);
11158 isl_bool
isl_map_is_injective(__isl_keep isl_map
*map
)
11162 map
= isl_map_copy(map
);
11163 map
= isl_map_reverse(map
);
11164 in
= isl_map_is_single_valued(map
);
11170 /* Check if the given map is obviously injective.
11172 isl_bool
isl_map_plain_is_injective(__isl_keep isl_map
*map
)
11176 map
= isl_map_copy(map
);
11177 map
= isl_map_reverse(map
);
11178 in
= isl_map_plain_is_single_valued(map
);
11184 isl_bool
isl_map_is_bijective(__isl_keep isl_map
*map
)
11188 sv
= isl_map_is_single_valued(map
);
11192 return isl_map_is_injective(map
);
11195 isl_bool
isl_set_is_singleton(__isl_keep isl_set
*set
)
11197 return isl_map_is_single_valued(set_to_map(set
));
11200 /* Does "map" only map elements to themselves?
11202 * If the domain and range spaces are different, then "map"
11203 * is considered not to be an identity relation, even if it is empty.
11204 * Otherwise, construct the maximal identity relation and
11205 * check whether "map" is a subset of this relation.
11207 isl_bool
isl_map_is_identity(__isl_keep isl_map
*map
)
11211 isl_bool equal
, is_identity
;
11213 space
= isl_map_get_space(map
);
11214 equal
= isl_space_tuple_is_equal(space
, isl_dim_in
, space
, isl_dim_out
);
11215 isl_space_free(space
);
11216 if (equal
< 0 || !equal
)
11219 id
= isl_map_identity(isl_map_get_space(map
));
11220 is_identity
= isl_map_is_subset(map
, id
);
11223 return is_identity
;
11226 int isl_map_is_translation(__isl_keep isl_map
*map
)
11231 delta
= isl_map_deltas(isl_map_copy(map
));
11232 ok
= isl_set_is_singleton(delta
);
11233 isl_set_free(delta
);
11238 static int unique(isl_int
*p
, unsigned pos
, unsigned len
)
11240 if (isl_seq_first_non_zero(p
, pos
) != -1)
11242 if (isl_seq_first_non_zero(p
+ pos
+ 1, len
- pos
- 1) != -1)
11247 isl_bool
isl_basic_set_is_box(__isl_keep isl_basic_set
*bset
)
11254 return isl_bool_error
;
11256 if (isl_basic_set_dim(bset
, isl_dim_div
) != 0)
11257 return isl_bool_false
;
11259 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
11260 ovar
= isl_space_offset(bset
->dim
, isl_dim_set
);
11261 for (j
= 0; j
< nvar
; ++j
) {
11262 int lower
= 0, upper
= 0;
11263 for (i
= 0; i
< bset
->n_eq
; ++i
) {
11264 if (isl_int_is_zero(bset
->eq
[i
][1 + ovar
+ j
]))
11266 if (!unique(bset
->eq
[i
] + 1 + ovar
, j
, nvar
))
11267 return isl_bool_false
;
11270 if (i
< bset
->n_eq
)
11272 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
11273 if (isl_int_is_zero(bset
->ineq
[i
][1 + ovar
+ j
]))
11275 if (!unique(bset
->ineq
[i
] + 1 + ovar
, j
, nvar
))
11276 return isl_bool_false
;
11277 if (isl_int_is_pos(bset
->ineq
[i
][1 + ovar
+ j
]))
11282 if (!lower
|| !upper
)
11283 return isl_bool_false
;
11286 return isl_bool_true
;
11289 isl_bool
isl_set_is_box(__isl_keep isl_set
*set
)
11292 return isl_bool_error
;
11294 return isl_bool_false
;
11296 return isl_basic_set_is_box(set
->p
[0]);
11299 isl_bool
isl_basic_set_is_wrapping(__isl_keep isl_basic_set
*bset
)
11302 return isl_bool_error
;
11304 return isl_space_is_wrapping(bset
->dim
);
11307 isl_bool
isl_set_is_wrapping(__isl_keep isl_set
*set
)
11310 return isl_bool_error
;
11312 return isl_space_is_wrapping(set
->dim
);
11315 /* Modify the space of "map" through a call to "change".
11316 * If "can_change" is set (not NULL), then first call it to check
11317 * if the modification is allowed, printing the error message "cannot_change"
11320 static __isl_give isl_map
*isl_map_change_space(__isl_take isl_map
*map
,
11321 isl_bool (*can_change
)(__isl_keep isl_map
*map
),
11322 const char *cannot_change
,
11323 __isl_give isl_space
*(*change
)(__isl_take isl_space
*space
))
11331 ok
= can_change
? can_change(map
) : isl_bool_true
;
11333 return isl_map_free(map
);
11335 isl_die(isl_map_get_ctx(map
), isl_error_invalid
, cannot_change
,
11336 return isl_map_free(map
));
11338 space
= change(isl_map_get_space(map
));
11339 map
= isl_map_reset_space(map
, space
);
11344 /* Is the domain of "map" a wrapped relation?
11346 isl_bool
isl_map_domain_is_wrapping(__isl_keep isl_map
*map
)
11349 return isl_bool_error
;
11351 return isl_space_domain_is_wrapping(map
->dim
);
11354 /* Is the range of "map" a wrapped relation?
11356 isl_bool
isl_map_range_is_wrapping(__isl_keep isl_map
*map
)
11359 return isl_bool_error
;
11361 return isl_space_range_is_wrapping(map
->dim
);
11364 __isl_give isl_basic_set
*isl_basic_map_wrap(__isl_take isl_basic_map
*bmap
)
11366 bmap
= isl_basic_map_cow(bmap
);
11370 bmap
->dim
= isl_space_wrap(bmap
->dim
);
11374 bmap
= isl_basic_map_finalize(bmap
);
11376 return bset_from_bmap(bmap
);
11378 isl_basic_map_free(bmap
);
11382 /* Given a map A -> B, return the set (A -> B).
11384 __isl_give isl_set
*isl_map_wrap(__isl_take isl_map
*map
)
11386 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_wrap
);
11389 __isl_give isl_basic_map
*isl_basic_set_unwrap(__isl_take isl_basic_set
*bset
)
11391 bset
= isl_basic_set_cow(bset
);
11395 bset
->dim
= isl_space_unwrap(bset
->dim
);
11399 bset
= isl_basic_set_finalize(bset
);
11401 return bset_to_bmap(bset
);
11403 isl_basic_set_free(bset
);
11407 /* Given a set (A -> B), return the map A -> B.
11408 * Error out if "set" is not of the form (A -> B).
11410 __isl_give isl_map
*isl_set_unwrap(__isl_take isl_set
*set
)
11412 return isl_map_change_space(set
, &isl_set_is_wrapping
,
11413 "not a wrapping set", &isl_space_unwrap
);
11416 __isl_give isl_basic_map
*isl_basic_map_reset(__isl_take isl_basic_map
*bmap
,
11417 enum isl_dim_type type
)
11422 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
11425 bmap
= isl_basic_map_cow(bmap
);
11429 bmap
->dim
= isl_space_reset(bmap
->dim
, type
);
11433 bmap
= isl_basic_map_finalize(bmap
);
11437 isl_basic_map_free(bmap
);
11441 __isl_give isl_map
*isl_map_reset(__isl_take isl_map
*map
,
11442 enum isl_dim_type type
)
11449 if (!isl_space_is_named_or_nested(map
->dim
, type
))
11452 map
= isl_map_cow(map
);
11456 for (i
= 0; i
< map
->n
; ++i
) {
11457 map
->p
[i
] = isl_basic_map_reset(map
->p
[i
], type
);
11461 map
->dim
= isl_space_reset(map
->dim
, type
);
11471 __isl_give isl_basic_map
*isl_basic_map_flatten(__isl_take isl_basic_map
*bmap
)
11476 if (!bmap
->dim
->nested
[0] && !bmap
->dim
->nested
[1])
11479 bmap
= isl_basic_map_cow(bmap
);
11483 bmap
->dim
= isl_space_flatten(bmap
->dim
);
11487 bmap
= isl_basic_map_finalize(bmap
);
11491 isl_basic_map_free(bmap
);
11495 __isl_give isl_basic_set
*isl_basic_set_flatten(__isl_take isl_basic_set
*bset
)
11497 return bset_from_bmap(isl_basic_map_flatten(bset_to_bmap(bset
)));
11500 __isl_give isl_basic_map
*isl_basic_map_flatten_domain(
11501 __isl_take isl_basic_map
*bmap
)
11506 if (!bmap
->dim
->nested
[0])
11509 bmap
= isl_basic_map_cow(bmap
);
11513 bmap
->dim
= isl_space_flatten_domain(bmap
->dim
);
11517 bmap
= isl_basic_map_finalize(bmap
);
11521 isl_basic_map_free(bmap
);
11525 __isl_give isl_basic_map
*isl_basic_map_flatten_range(
11526 __isl_take isl_basic_map
*bmap
)
11531 if (!bmap
->dim
->nested
[1])
11534 bmap
= isl_basic_map_cow(bmap
);
11538 bmap
->dim
= isl_space_flatten_range(bmap
->dim
);
11542 bmap
= isl_basic_map_finalize(bmap
);
11546 isl_basic_map_free(bmap
);
11550 /* Remove any internal structure from the spaces of domain and range of "map".
11552 __isl_give isl_map
*isl_map_flatten(__isl_take isl_map
*map
)
11557 if (!map
->dim
->nested
[0] && !map
->dim
->nested
[1])
11560 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten
);
11563 __isl_give isl_set
*isl_set_flatten(__isl_take isl_set
*set
)
11565 return set_from_map(isl_map_flatten(set_to_map(set
)));
11568 __isl_give isl_map
*isl_set_flatten_map(__isl_take isl_set
*set
)
11570 isl_space
*dim
, *flat_dim
;
11573 dim
= isl_set_get_space(set
);
11574 flat_dim
= isl_space_flatten(isl_space_copy(dim
));
11575 map
= isl_map_identity(isl_space_join(isl_space_reverse(dim
), flat_dim
));
11576 map
= isl_map_intersect_domain(map
, set
);
11581 /* Remove any internal structure from the space of the domain of "map".
11583 __isl_give isl_map
*isl_map_flatten_domain(__isl_take isl_map
*map
)
11588 if (!map
->dim
->nested
[0])
11591 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten_domain
);
11594 /* Remove any internal structure from the space of the range of "map".
11596 __isl_give isl_map
*isl_map_flatten_range(__isl_take isl_map
*map
)
11601 if (!map
->dim
->nested
[1])
11604 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten_range
);
11607 /* Reorder the dimensions of "bmap" according to the given dim_map
11608 * and set the dimension specification to "dim" and
11609 * perform Gaussian elimination on the result.
11611 __isl_give isl_basic_map
*isl_basic_map_realign(__isl_take isl_basic_map
*bmap
,
11612 __isl_take isl_space
*dim
, __isl_take
struct isl_dim_map
*dim_map
)
11614 isl_basic_map
*res
;
11617 bmap
= isl_basic_map_cow(bmap
);
11618 if (!bmap
|| !dim
|| !dim_map
)
11621 flags
= bmap
->flags
;
11622 ISL_FL_CLR(flags
, ISL_BASIC_MAP_FINAL
);
11623 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED
);
11624 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
11625 res
= isl_basic_map_alloc_space(dim
,
11626 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
11627 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
11629 res
->flags
= flags
;
11630 res
= isl_basic_map_gauss(res
, NULL
);
11631 res
= isl_basic_map_finalize(res
);
11635 isl_basic_map_free(bmap
);
11636 isl_space_free(dim
);
11640 /* Reorder the dimensions of "map" according to given reordering.
11642 __isl_give isl_map
*isl_map_realign(__isl_take isl_map
*map
,
11643 __isl_take isl_reordering
*r
)
11646 struct isl_dim_map
*dim_map
;
11648 map
= isl_map_cow(map
);
11649 dim_map
= isl_dim_map_from_reordering(r
);
11650 if (!map
|| !r
|| !dim_map
)
11653 for (i
= 0; i
< map
->n
; ++i
) {
11654 struct isl_dim_map
*dim_map_i
;
11656 dim_map_i
= isl_dim_map_extend(dim_map
, map
->p
[i
]);
11658 map
->p
[i
] = isl_basic_map_realign(map
->p
[i
],
11659 isl_space_copy(r
->dim
), dim_map_i
);
11665 map
= isl_map_reset_space(map
, isl_space_copy(r
->dim
));
11667 isl_reordering_free(r
);
11673 isl_reordering_free(r
);
11677 __isl_give isl_set
*isl_set_realign(__isl_take isl_set
*set
,
11678 __isl_take isl_reordering
*r
)
11680 return set_from_map(isl_map_realign(set_to_map(set
), r
));
11683 __isl_give isl_map
*isl_map_align_params(__isl_take isl_map
*map
,
11684 __isl_take isl_space
*model
)
11688 if (!map
|| !model
)
11691 ctx
= isl_space_get_ctx(model
);
11692 if (!isl_space_has_named_params(model
))
11693 isl_die(ctx
, isl_error_invalid
,
11694 "model has unnamed parameters", goto error
);
11695 if (!isl_space_has_named_params(map
->dim
))
11696 isl_die(ctx
, isl_error_invalid
,
11697 "relation has unnamed parameters", goto error
);
11698 if (!isl_space_match(map
->dim
, isl_dim_param
, model
, isl_dim_param
)) {
11699 isl_reordering
*exp
;
11701 model
= isl_space_drop_dims(model
, isl_dim_in
,
11702 0, isl_space_dim(model
, isl_dim_in
));
11703 model
= isl_space_drop_dims(model
, isl_dim_out
,
11704 0, isl_space_dim(model
, isl_dim_out
));
11705 exp
= isl_parameter_alignment_reordering(map
->dim
, model
);
11706 exp
= isl_reordering_extend_space(exp
, isl_map_get_space(map
));
11707 map
= isl_map_realign(map
, exp
);
11710 isl_space_free(model
);
11713 isl_space_free(model
);
11718 __isl_give isl_set
*isl_set_align_params(__isl_take isl_set
*set
,
11719 __isl_take isl_space
*model
)
11721 return isl_map_align_params(set
, model
);
11724 /* Align the parameters of "bmap" to those of "model", introducing
11725 * additional parameters if needed.
11727 __isl_give isl_basic_map
*isl_basic_map_align_params(
11728 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*model
)
11732 if (!bmap
|| !model
)
11735 ctx
= isl_space_get_ctx(model
);
11736 if (!isl_space_has_named_params(model
))
11737 isl_die(ctx
, isl_error_invalid
,
11738 "model has unnamed parameters", goto error
);
11739 if (!isl_space_has_named_params(bmap
->dim
))
11740 isl_die(ctx
, isl_error_invalid
,
11741 "relation has unnamed parameters", goto error
);
11742 if (!isl_space_match(bmap
->dim
, isl_dim_param
, model
, isl_dim_param
)) {
11743 isl_reordering
*exp
;
11744 struct isl_dim_map
*dim_map
;
11746 model
= isl_space_drop_dims(model
, isl_dim_in
,
11747 0, isl_space_dim(model
, isl_dim_in
));
11748 model
= isl_space_drop_dims(model
, isl_dim_out
,
11749 0, isl_space_dim(model
, isl_dim_out
));
11750 exp
= isl_parameter_alignment_reordering(bmap
->dim
, model
);
11751 exp
= isl_reordering_extend_space(exp
,
11752 isl_basic_map_get_space(bmap
));
11753 dim_map
= isl_dim_map_from_reordering(exp
);
11754 bmap
= isl_basic_map_realign(bmap
,
11755 exp
? isl_space_copy(exp
->dim
) : NULL
,
11756 isl_dim_map_extend(dim_map
, bmap
));
11757 isl_reordering_free(exp
);
11761 isl_space_free(model
);
11764 isl_space_free(model
);
11765 isl_basic_map_free(bmap
);
11769 /* Align the parameters of "bset" to those of "model", introducing
11770 * additional parameters if needed.
11772 __isl_give isl_basic_set
*isl_basic_set_align_params(
11773 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*model
)
11775 return isl_basic_map_align_params(bset
, model
);
11778 __isl_give isl_mat
*isl_basic_map_equalities_matrix(
11779 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
11780 enum isl_dim_type c2
, enum isl_dim_type c3
,
11781 enum isl_dim_type c4
, enum isl_dim_type c5
)
11783 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11784 struct isl_mat
*mat
;
11790 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_eq
,
11791 isl_basic_map_total_dim(bmap
) + 1);
11794 for (i
= 0; i
< bmap
->n_eq
; ++i
)
11795 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11796 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11797 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11798 isl_int_set(mat
->row
[i
][pos
],
11799 bmap
->eq
[i
][off
+ k
]);
11807 __isl_give isl_mat
*isl_basic_map_inequalities_matrix(
11808 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
11809 enum isl_dim_type c2
, enum isl_dim_type c3
,
11810 enum isl_dim_type c4
, enum isl_dim_type c5
)
11812 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11813 struct isl_mat
*mat
;
11819 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_ineq
,
11820 isl_basic_map_total_dim(bmap
) + 1);
11823 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
11824 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11825 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11826 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11827 isl_int_set(mat
->row
[i
][pos
],
11828 bmap
->ineq
[i
][off
+ k
]);
11836 __isl_give isl_basic_map
*isl_basic_map_from_constraint_matrices(
11837 __isl_take isl_space
*dim
,
11838 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
11839 enum isl_dim_type c2
, enum isl_dim_type c3
,
11840 enum isl_dim_type c4
, enum isl_dim_type c5
)
11842 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
11843 isl_basic_map
*bmap
;
11849 if (!dim
|| !eq
|| !ineq
)
11852 if (eq
->n_col
!= ineq
->n_col
)
11853 isl_die(dim
->ctx
, isl_error_invalid
,
11854 "equalities and inequalities matrices should have "
11855 "same number of columns", goto error
);
11857 total
= 1 + isl_space_dim(dim
, isl_dim_all
);
11859 if (eq
->n_col
< total
)
11860 isl_die(dim
->ctx
, isl_error_invalid
,
11861 "number of columns too small", goto error
);
11863 extra
= eq
->n_col
- total
;
11865 bmap
= isl_basic_map_alloc_space(isl_space_copy(dim
), extra
,
11866 eq
->n_row
, ineq
->n_row
);
11869 for (i
= 0; i
< extra
; ++i
) {
11870 k
= isl_basic_map_alloc_div(bmap
);
11873 isl_int_set_si(bmap
->div
[k
][0], 0);
11875 for (i
= 0; i
< eq
->n_row
; ++i
) {
11876 l
= isl_basic_map_alloc_equality(bmap
);
11879 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11880 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11881 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11882 isl_int_set(bmap
->eq
[l
][off
+ k
],
11888 for (i
= 0; i
< ineq
->n_row
; ++i
) {
11889 l
= isl_basic_map_alloc_inequality(bmap
);
11892 for (j
= 0, pos
= 0; j
< 5; ++j
) {
11893 int off
= isl_basic_map_offset(bmap
, c
[j
]);
11894 for (k
= 0; k
< isl_basic_map_dim(bmap
, c
[j
]); ++k
) {
11895 isl_int_set(bmap
->ineq
[l
][off
+ k
],
11896 ineq
->row
[i
][pos
]);
11902 isl_space_free(dim
);
11904 isl_mat_free(ineq
);
11906 bmap
= isl_basic_map_simplify(bmap
);
11907 return isl_basic_map_finalize(bmap
);
11909 isl_space_free(dim
);
11911 isl_mat_free(ineq
);
11915 __isl_give isl_mat
*isl_basic_set_equalities_matrix(
11916 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
11917 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
11919 return isl_basic_map_equalities_matrix(bset_to_bmap(bset
),
11920 c1
, c2
, c3
, c4
, isl_dim_in
);
11923 __isl_give isl_mat
*isl_basic_set_inequalities_matrix(
11924 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
11925 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
11927 return isl_basic_map_inequalities_matrix(bset_to_bmap(bset
),
11928 c1
, c2
, c3
, c4
, isl_dim_in
);
11931 __isl_give isl_basic_set
*isl_basic_set_from_constraint_matrices(
11932 __isl_take isl_space
*dim
,
11933 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
11934 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
11936 isl_basic_map
*bmap
;
11937 bmap
= isl_basic_map_from_constraint_matrices(dim
, eq
, ineq
,
11938 c1
, c2
, c3
, c4
, isl_dim_in
);
11939 return bset_from_bmap(bmap
);
11942 isl_bool
isl_basic_map_can_zip(__isl_keep isl_basic_map
*bmap
)
11945 return isl_bool_error
;
11947 return isl_space_can_zip(bmap
->dim
);
11950 isl_bool
isl_map_can_zip(__isl_keep isl_map
*map
)
11953 return isl_bool_error
;
11955 return isl_space_can_zip(map
->dim
);
11958 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
11959 * (A -> C) -> (B -> D).
11961 __isl_give isl_basic_map
*isl_basic_map_zip(__isl_take isl_basic_map
*bmap
)
11970 if (!isl_basic_map_can_zip(bmap
))
11971 isl_die(bmap
->ctx
, isl_error_invalid
,
11972 "basic map cannot be zipped", goto error
);
11973 pos
= isl_basic_map_offset(bmap
, isl_dim_in
) +
11974 isl_space_dim(bmap
->dim
->nested
[0], isl_dim_in
);
11975 n1
= isl_space_dim(bmap
->dim
->nested
[0], isl_dim_out
);
11976 n2
= isl_space_dim(bmap
->dim
->nested
[1], isl_dim_in
);
11977 bmap
= isl_basic_map_cow(bmap
);
11978 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
11981 bmap
->dim
= isl_space_zip(bmap
->dim
);
11984 bmap
= isl_basic_map_mark_final(bmap
);
11987 isl_basic_map_free(bmap
);
11991 /* Given a map (A -> B) -> (C -> D), return the corresponding map
11992 * (A -> C) -> (B -> D).
11994 __isl_give isl_map
*isl_map_zip(__isl_take isl_map
*map
)
12001 if (!isl_map_can_zip(map
))
12002 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be zipped",
12005 map
= isl_map_cow(map
);
12009 for (i
= 0; i
< map
->n
; ++i
) {
12010 map
->p
[i
] = isl_basic_map_zip(map
->p
[i
]);
12015 map
->dim
= isl_space_zip(map
->dim
);
12025 /* Can we apply isl_basic_map_curry to "bmap"?
12026 * That is, does it have a nested relation in its domain?
12028 isl_bool
isl_basic_map_can_curry(__isl_keep isl_basic_map
*bmap
)
12031 return isl_bool_error
;
12033 return isl_space_can_curry(bmap
->dim
);
12036 /* Can we apply isl_map_curry to "map"?
12037 * That is, does it have a nested relation in its domain?
12039 isl_bool
isl_map_can_curry(__isl_keep isl_map
*map
)
12042 return isl_bool_error
;
12044 return isl_space_can_curry(map
->dim
);
12047 /* Given a basic map (A -> B) -> C, return the corresponding basic map
12050 __isl_give isl_basic_map
*isl_basic_map_curry(__isl_take isl_basic_map
*bmap
)
12056 if (!isl_basic_map_can_curry(bmap
))
12057 isl_die(bmap
->ctx
, isl_error_invalid
,
12058 "basic map cannot be curried", goto error
);
12059 bmap
= isl_basic_map_cow(bmap
);
12062 bmap
->dim
= isl_space_curry(bmap
->dim
);
12065 bmap
= isl_basic_map_mark_final(bmap
);
12068 isl_basic_map_free(bmap
);
12072 /* Given a map (A -> B) -> C, return the corresponding map
12075 __isl_give isl_map
*isl_map_curry(__isl_take isl_map
*map
)
12077 return isl_map_change_space(map
, &isl_map_can_curry
,
12078 "map cannot be curried", &isl_space_curry
);
12081 /* Can isl_map_range_curry be applied to "map"?
12082 * That is, does it have a nested relation in its range,
12083 * the domain of which is itself a nested relation?
12085 isl_bool
isl_map_can_range_curry(__isl_keep isl_map
*map
)
12088 return isl_bool_error
;
12090 return isl_space_can_range_curry(map
->dim
);
12093 /* Given a map A -> ((B -> C) -> D), return the corresponding map
12094 * A -> (B -> (C -> D)).
12096 __isl_give isl_map
*isl_map_range_curry(__isl_take isl_map
*map
)
12098 return isl_map_change_space(map
, &isl_map_can_range_curry
,
12099 "map range cannot be curried",
12100 &isl_space_range_curry
);
12103 /* Can we apply isl_basic_map_uncurry to "bmap"?
12104 * That is, does it have a nested relation in its domain?
12106 isl_bool
isl_basic_map_can_uncurry(__isl_keep isl_basic_map
*bmap
)
12109 return isl_bool_error
;
12111 return isl_space_can_uncurry(bmap
->dim
);
12114 /* Can we apply isl_map_uncurry to "map"?
12115 * That is, does it have a nested relation in its domain?
12117 isl_bool
isl_map_can_uncurry(__isl_keep isl_map
*map
)
12120 return isl_bool_error
;
12122 return isl_space_can_uncurry(map
->dim
);
12125 /* Given a basic map A -> (B -> C), return the corresponding basic map
12128 __isl_give isl_basic_map
*isl_basic_map_uncurry(__isl_take isl_basic_map
*bmap
)
12134 if (!isl_basic_map_can_uncurry(bmap
))
12135 isl_die(bmap
->ctx
, isl_error_invalid
,
12136 "basic map cannot be uncurried",
12137 return isl_basic_map_free(bmap
));
12138 bmap
= isl_basic_map_cow(bmap
);
12141 bmap
->dim
= isl_space_uncurry(bmap
->dim
);
12143 return isl_basic_map_free(bmap
);
12144 bmap
= isl_basic_map_mark_final(bmap
);
12148 /* Given a map A -> (B -> C), return the corresponding map
12151 __isl_give isl_map
*isl_map_uncurry(__isl_take isl_map
*map
)
12153 return isl_map_change_space(map
, &isl_map_can_uncurry
,
12154 "map cannot be uncurried", &isl_space_uncurry
);
12157 /* Construct a basic map mapping the domain of the affine expression
12158 * to a one-dimensional range prescribed by the affine expression.
12159 * If "rational" is set, then construct a rational basic map.
12161 * A NaN affine expression cannot be converted to a basic map.
12163 static __isl_give isl_basic_map
*isl_basic_map_from_aff2(
12164 __isl_take isl_aff
*aff
, int rational
)
12169 isl_local_space
*ls
;
12170 isl_basic_map
*bmap
= NULL
;
12174 is_nan
= isl_aff_is_nan(aff
);
12178 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
12179 "cannot convert NaN", goto error
);
12181 ls
= isl_aff_get_local_space(aff
);
12182 bmap
= isl_basic_map_from_local_space(ls
);
12183 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
12184 k
= isl_basic_map_alloc_equality(bmap
);
12188 pos
= isl_basic_map_offset(bmap
, isl_dim_out
);
12189 isl_seq_cpy(bmap
->eq
[k
], aff
->v
->el
+ 1, pos
);
12190 isl_int_neg(bmap
->eq
[k
][pos
], aff
->v
->el
[0]);
12191 isl_seq_cpy(bmap
->eq
[k
] + pos
+ 1, aff
->v
->el
+ 1 + pos
,
12192 aff
->v
->size
- (pos
+ 1));
12196 bmap
= isl_basic_map_set_rational(bmap
);
12197 bmap
= isl_basic_map_finalize(bmap
);
12201 isl_basic_map_free(bmap
);
12205 /* Construct a basic map mapping the domain of the affine expression
12206 * to a one-dimensional range prescribed by the affine expression.
12208 __isl_give isl_basic_map
*isl_basic_map_from_aff(__isl_take isl_aff
*aff
)
12210 return isl_basic_map_from_aff2(aff
, 0);
12213 /* Construct a map mapping the domain of the affine expression
12214 * to a one-dimensional range prescribed by the affine expression.
12216 __isl_give isl_map
*isl_map_from_aff(__isl_take isl_aff
*aff
)
12218 isl_basic_map
*bmap
;
12220 bmap
= isl_basic_map_from_aff(aff
);
12221 return isl_map_from_basic_map(bmap
);
12224 /* Construct a basic map mapping the domain the multi-affine expression
12225 * to its range, with each dimension in the range equated to the
12226 * corresponding affine expression.
12227 * If "rational" is set, then construct a rational basic map.
12229 __isl_give isl_basic_map
*isl_basic_map_from_multi_aff2(
12230 __isl_take isl_multi_aff
*maff
, int rational
)
12234 isl_basic_map
*bmap
;
12239 if (isl_space_dim(maff
->space
, isl_dim_out
) != maff
->n
)
12240 isl_die(isl_multi_aff_get_ctx(maff
), isl_error_internal
,
12241 "invalid space", goto error
);
12243 space
= isl_space_domain(isl_multi_aff_get_space(maff
));
12244 bmap
= isl_basic_map_universe(isl_space_from_domain(space
));
12246 bmap
= isl_basic_map_set_rational(bmap
);
12248 for (i
= 0; i
< maff
->n
; ++i
) {
12250 isl_basic_map
*bmap_i
;
12252 aff
= isl_aff_copy(maff
->p
[i
]);
12253 bmap_i
= isl_basic_map_from_aff2(aff
, rational
);
12255 bmap
= isl_basic_map_flat_range_product(bmap
, bmap_i
);
12258 bmap
= isl_basic_map_reset_space(bmap
, isl_multi_aff_get_space(maff
));
12260 isl_multi_aff_free(maff
);
12263 isl_multi_aff_free(maff
);
12267 /* Construct a basic map mapping the domain the multi-affine expression
12268 * to its range, with each dimension in the range equated to the
12269 * corresponding affine expression.
12271 __isl_give isl_basic_map
*isl_basic_map_from_multi_aff(
12272 __isl_take isl_multi_aff
*ma
)
12274 return isl_basic_map_from_multi_aff2(ma
, 0);
12277 /* Construct a map mapping the domain the multi-affine expression
12278 * to its range, with each dimension in the range equated to the
12279 * corresponding affine expression.
12281 __isl_give isl_map
*isl_map_from_multi_aff(__isl_take isl_multi_aff
*maff
)
12283 isl_basic_map
*bmap
;
12285 bmap
= isl_basic_map_from_multi_aff(maff
);
12286 return isl_map_from_basic_map(bmap
);
12289 /* Construct a basic map mapping a domain in the given space to
12290 * to an n-dimensional range, with n the number of elements in the list,
12291 * where each coordinate in the range is prescribed by the
12292 * corresponding affine expression.
12293 * The domains of all affine expressions in the list are assumed to match
12296 __isl_give isl_basic_map
*isl_basic_map_from_aff_list(
12297 __isl_take isl_space
*domain_dim
, __isl_take isl_aff_list
*list
)
12301 isl_basic_map
*bmap
;
12306 dim
= isl_space_from_domain(domain_dim
);
12307 bmap
= isl_basic_map_universe(dim
);
12309 for (i
= 0; i
< list
->n
; ++i
) {
12311 isl_basic_map
*bmap_i
;
12313 aff
= isl_aff_copy(list
->p
[i
]);
12314 bmap_i
= isl_basic_map_from_aff(aff
);
12316 bmap
= isl_basic_map_flat_range_product(bmap
, bmap_i
);
12319 isl_aff_list_free(list
);
12323 __isl_give isl_set
*isl_set_equate(__isl_take isl_set
*set
,
12324 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12326 return isl_map_equate(set
, type1
, pos1
, type2
, pos2
);
12329 /* Construct a basic map where the given dimensions are equal to each other.
12331 static __isl_give isl_basic_map
*equator(__isl_take isl_space
*space
,
12332 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12334 isl_basic_map
*bmap
= NULL
;
12340 if (pos1
>= isl_space_dim(space
, type1
))
12341 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
12342 "index out of bounds", goto error
);
12343 if (pos2
>= isl_space_dim(space
, type2
))
12344 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
12345 "index out of bounds", goto error
);
12347 if (type1
== type2
&& pos1
== pos2
)
12348 return isl_basic_map_universe(space
);
12350 bmap
= isl_basic_map_alloc_space(isl_space_copy(space
), 0, 1, 0);
12351 i
= isl_basic_map_alloc_equality(bmap
);
12354 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
12355 pos1
+= isl_basic_map_offset(bmap
, type1
);
12356 pos2
+= isl_basic_map_offset(bmap
, type2
);
12357 isl_int_set_si(bmap
->eq
[i
][pos1
], -1);
12358 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
12359 bmap
= isl_basic_map_finalize(bmap
);
12360 isl_space_free(space
);
12363 isl_space_free(space
);
12364 isl_basic_map_free(bmap
);
12368 /* Add a constraint imposing that the given two dimensions are equal.
12370 __isl_give isl_basic_map
*isl_basic_map_equate(__isl_take isl_basic_map
*bmap
,
12371 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12375 eq
= equator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
12377 bmap
= isl_basic_map_intersect(bmap
, eq
);
12382 /* Add a constraint imposing that the given two dimensions are equal.
12384 __isl_give isl_map
*isl_map_equate(__isl_take isl_map
*map
,
12385 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12387 isl_basic_map
*bmap
;
12389 bmap
= equator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
12391 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
12396 /* Add a constraint imposing that the given two dimensions have opposite values.
12398 __isl_give isl_map
*isl_map_oppose(__isl_take isl_map
*map
,
12399 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12401 isl_basic_map
*bmap
= NULL
;
12407 if (pos1
>= isl_map_dim(map
, type1
))
12408 isl_die(map
->ctx
, isl_error_invalid
,
12409 "index out of bounds", goto error
);
12410 if (pos2
>= isl_map_dim(map
, type2
))
12411 isl_die(map
->ctx
, isl_error_invalid
,
12412 "index out of bounds", goto error
);
12414 bmap
= isl_basic_map_alloc_space(isl_map_get_space(map
), 0, 1, 0);
12415 i
= isl_basic_map_alloc_equality(bmap
);
12418 isl_seq_clr(bmap
->eq
[i
], 1 + isl_basic_map_total_dim(bmap
));
12419 pos1
+= isl_basic_map_offset(bmap
, type1
);
12420 pos2
+= isl_basic_map_offset(bmap
, type2
);
12421 isl_int_set_si(bmap
->eq
[i
][pos1
], 1);
12422 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
12423 bmap
= isl_basic_map_finalize(bmap
);
12425 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
12429 isl_basic_map_free(bmap
);
12434 /* Construct a constraint imposing that the value of the first dimension is
12435 * greater than or equal to that of the second.
12437 static __isl_give isl_constraint
*constraint_order_ge(
12438 __isl_take isl_space
*space
, enum isl_dim_type type1
, int pos1
,
12439 enum isl_dim_type type2
, int pos2
)
12446 c
= isl_constraint_alloc_inequality(isl_local_space_from_space(space
));
12448 if (pos1
>= isl_constraint_dim(c
, type1
))
12449 isl_die(isl_constraint_get_ctx(c
), isl_error_invalid
,
12450 "index out of bounds", return isl_constraint_free(c
));
12451 if (pos2
>= isl_constraint_dim(c
, type2
))
12452 isl_die(isl_constraint_get_ctx(c
), isl_error_invalid
,
12453 "index out of bounds", return isl_constraint_free(c
));
12455 if (type1
== type2
&& pos1
== pos2
)
12458 c
= isl_constraint_set_coefficient_si(c
, type1
, pos1
, 1);
12459 c
= isl_constraint_set_coefficient_si(c
, type2
, pos2
, -1);
12464 /* Add a constraint imposing that the value of the first dimension is
12465 * greater than or equal to that of the second.
12467 __isl_give isl_basic_map
*isl_basic_map_order_ge(__isl_take isl_basic_map
*bmap
,
12468 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12473 if (type1
== type2
&& pos1
== pos2
)
12475 space
= isl_basic_map_get_space(bmap
);
12476 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
12477 bmap
= isl_basic_map_add_constraint(bmap
, c
);
12482 /* Add a constraint imposing that the value of the first dimension is
12483 * greater than or equal to that of the second.
12485 __isl_give isl_map
*isl_map_order_ge(__isl_take isl_map
*map
,
12486 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12491 if (type1
== type2
&& pos1
== pos2
)
12493 space
= isl_map_get_space(map
);
12494 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
12495 map
= isl_map_add_constraint(map
, c
);
12500 /* Add a constraint imposing that the value of the first dimension is
12501 * less than or equal to that of the second.
12503 __isl_give isl_map
*isl_map_order_le(__isl_take isl_map
*map
,
12504 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12506 return isl_map_order_ge(map
, type2
, pos2
, type1
, pos1
);
12509 /* Construct a basic map where the value of the first dimension is
12510 * greater than that of the second.
12512 static __isl_give isl_basic_map
*greator(__isl_take isl_space
*space
,
12513 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12515 isl_basic_map
*bmap
= NULL
;
12521 if (pos1
>= isl_space_dim(space
, type1
))
12522 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
12523 "index out of bounds", goto error
);
12524 if (pos2
>= isl_space_dim(space
, type2
))
12525 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
12526 "index out of bounds", goto error
);
12528 if (type1
== type2
&& pos1
== pos2
)
12529 return isl_basic_map_empty(space
);
12531 bmap
= isl_basic_map_alloc_space(space
, 0, 0, 1);
12532 i
= isl_basic_map_alloc_inequality(bmap
);
12534 return isl_basic_map_free(bmap
);
12535 isl_seq_clr(bmap
->ineq
[i
], 1 + isl_basic_map_total_dim(bmap
));
12536 pos1
+= isl_basic_map_offset(bmap
, type1
);
12537 pos2
+= isl_basic_map_offset(bmap
, type2
);
12538 isl_int_set_si(bmap
->ineq
[i
][pos1
], 1);
12539 isl_int_set_si(bmap
->ineq
[i
][pos2
], -1);
12540 isl_int_set_si(bmap
->ineq
[i
][0], -1);
12541 bmap
= isl_basic_map_finalize(bmap
);
12545 isl_space_free(space
);
12546 isl_basic_map_free(bmap
);
12550 /* Add a constraint imposing that the value of the first dimension is
12551 * greater than that of the second.
12553 __isl_give isl_basic_map
*isl_basic_map_order_gt(__isl_take isl_basic_map
*bmap
,
12554 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12558 gt
= greator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
12560 bmap
= isl_basic_map_intersect(bmap
, gt
);
12565 /* Add a constraint imposing that the value of the first dimension is
12566 * greater than that of the second.
12568 __isl_give isl_map
*isl_map_order_gt(__isl_take isl_map
*map
,
12569 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12571 isl_basic_map
*bmap
;
12573 bmap
= greator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
12575 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
12580 /* Add a constraint imposing that the value of the first dimension is
12581 * smaller than that of the second.
12583 __isl_give isl_map
*isl_map_order_lt(__isl_take isl_map
*map
,
12584 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12586 return isl_map_order_gt(map
, type2
, pos2
, type1
, pos1
);
12589 __isl_give isl_aff
*isl_basic_map_get_div(__isl_keep isl_basic_map
*bmap
,
12593 isl_local_space
*ls
;
12598 if (!isl_basic_map_divs_known(bmap
))
12599 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12600 "some divs are unknown", return NULL
);
12602 ls
= isl_basic_map_get_local_space(bmap
);
12603 div
= isl_local_space_get_div(ls
, pos
);
12604 isl_local_space_free(ls
);
12609 __isl_give isl_aff
*isl_basic_set_get_div(__isl_keep isl_basic_set
*bset
,
12612 return isl_basic_map_get_div(bset
, pos
);
12615 /* Plug in "subs" for dimension "type", "pos" of "bset".
12617 * Let i be the dimension to replace and let "subs" be of the form
12621 * Any integer division with a non-zero coefficient for i,
12623 * floor((a i + g)/m)
12627 * floor((a f + d g)/(m d))
12629 * Constraints of the form
12637 * We currently require that "subs" is an integral expression.
12638 * Handling rational expressions may require us to add stride constraints
12639 * as we do in isl_basic_set_preimage_multi_aff.
12641 __isl_give isl_basic_set
*isl_basic_set_substitute(
12642 __isl_take isl_basic_set
*bset
,
12643 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
12649 if (bset
&& isl_basic_set_plain_is_empty(bset
))
12652 bset
= isl_basic_set_cow(bset
);
12653 if (!bset
|| !subs
)
12656 ctx
= isl_basic_set_get_ctx(bset
);
12657 if (!isl_space_is_equal(bset
->dim
, subs
->ls
->dim
))
12658 isl_die(ctx
, isl_error_invalid
,
12659 "spaces don't match", goto error
);
12660 if (isl_local_space_dim(subs
->ls
, isl_dim_div
) != 0)
12661 isl_die(ctx
, isl_error_unsupported
,
12662 "cannot handle divs yet", goto error
);
12663 if (!isl_int_is_one(subs
->v
->el
[0]))
12664 isl_die(ctx
, isl_error_invalid
,
12665 "can only substitute integer expressions", goto error
);
12667 pos
+= isl_basic_set_offset(bset
, type
);
12671 for (i
= 0; i
< bset
->n_eq
; ++i
) {
12672 if (isl_int_is_zero(bset
->eq
[i
][pos
]))
12674 isl_int_set(v
, bset
->eq
[i
][pos
]);
12675 isl_int_set_si(bset
->eq
[i
][pos
], 0);
12676 isl_seq_combine(bset
->eq
[i
], subs
->v
->el
[0], bset
->eq
[i
],
12677 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12680 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
12681 if (isl_int_is_zero(bset
->ineq
[i
][pos
]))
12683 isl_int_set(v
, bset
->ineq
[i
][pos
]);
12684 isl_int_set_si(bset
->ineq
[i
][pos
], 0);
12685 isl_seq_combine(bset
->ineq
[i
], subs
->v
->el
[0], bset
->ineq
[i
],
12686 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12689 for (i
= 0; i
< bset
->n_div
; ++i
) {
12690 if (isl_int_is_zero(bset
->div
[i
][1 + pos
]))
12692 isl_int_set(v
, bset
->div
[i
][1 + pos
]);
12693 isl_int_set_si(bset
->div
[i
][1 + pos
], 0);
12694 isl_seq_combine(bset
->div
[i
] + 1,
12695 subs
->v
->el
[0], bset
->div
[i
] + 1,
12696 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
12697 isl_int_mul(bset
->div
[i
][0], bset
->div
[i
][0], subs
->v
->el
[0]);
12702 bset
= isl_basic_set_simplify(bset
);
12703 return isl_basic_set_finalize(bset
);
12705 isl_basic_set_free(bset
);
12709 /* Plug in "subs" for dimension "type", "pos" of "set".
12711 __isl_give isl_set
*isl_set_substitute(__isl_take isl_set
*set
,
12712 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
12716 if (set
&& isl_set_plain_is_empty(set
))
12719 set
= isl_set_cow(set
);
12723 for (i
= set
->n
- 1; i
>= 0; --i
) {
12724 set
->p
[i
] = isl_basic_set_substitute(set
->p
[i
], type
, pos
, subs
);
12725 if (remove_if_empty(set
, i
) < 0)
12735 /* Check if the range of "ma" is compatible with the domain or range
12736 * (depending on "type") of "bmap".
12737 * Return -1 if anything is wrong.
12739 static int check_basic_map_compatible_range_multi_aff(
12740 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type type
,
12741 __isl_keep isl_multi_aff
*ma
)
12744 isl_space
*ma_space
;
12746 ma_space
= isl_multi_aff_get_space(ma
);
12748 m
= isl_space_match(bmap
->dim
, isl_dim_param
, ma_space
, isl_dim_param
);
12752 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12753 "parameters don't match", goto error
);
12754 m
= isl_space_tuple_is_equal(bmap
->dim
, type
, ma_space
, isl_dim_out
);
12758 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
12759 "spaces don't match", goto error
);
12761 isl_space_free(ma_space
);
12764 isl_space_free(ma_space
);
12768 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12769 * coefficients before the transformed range of dimensions,
12770 * the "n_after" coefficients after the transformed range of dimensions
12771 * and the coefficients of the other divs in "bmap".
12773 static int set_ma_divs(__isl_keep isl_basic_map
*bmap
,
12774 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
, int n_div
)
12779 isl_local_space
*ls
;
12784 ls
= isl_aff_get_domain_local_space(ma
->p
[0]);
12788 n_param
= isl_local_space_dim(ls
, isl_dim_param
);
12789 n_set
= isl_local_space_dim(ls
, isl_dim_set
);
12790 for (i
= 0; i
< n_div
; ++i
) {
12791 int o_bmap
= 0, o_ls
= 0;
12793 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], 1 + 1 + n_param
);
12794 o_bmap
+= 1 + 1 + n_param
;
12795 o_ls
+= 1 + 1 + n_param
;
12796 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_before
);
12797 o_bmap
+= n_before
;
12798 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
12799 ls
->div
->row
[i
] + o_ls
, n_set
);
12802 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_after
);
12804 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
12805 ls
->div
->row
[i
] + o_ls
, n_div
);
12808 isl_seq_clr(bmap
->div
[i
] + o_bmap
, bmap
->n_div
- n_div
);
12809 if (isl_basic_set_add_div_constraints(bmap
, i
) < 0)
12813 isl_local_space_free(ls
);
12816 isl_local_space_free(ls
);
12820 /* How many stride constraints does "ma" enforce?
12821 * That is, how many of the affine expressions have a denominator
12822 * different from one?
12824 static int multi_aff_strides(__isl_keep isl_multi_aff
*ma
)
12829 for (i
= 0; i
< ma
->n
; ++i
)
12830 if (!isl_int_is_one(ma
->p
[i
]->v
->el
[0]))
12836 /* For each affine expression in ma of the form
12838 * x_i = (f_i y + h_i)/m_i
12840 * with m_i different from one, add a constraint to "bmap"
12843 * f_i y + h_i = m_i alpha_i
12845 * with alpha_i an additional existentially quantified variable.
12847 * The input variables of "ma" correspond to a subset of the variables
12848 * of "bmap". There are "n_before" variables in "bmap" before this
12849 * subset and "n_after" variables after this subset.
12850 * The integer divisions of the affine expressions in "ma" are assumed
12851 * to have been aligned. There are "n_div_ma" of them and
12852 * they appear first in "bmap", straight after the "n_after" variables.
12854 static __isl_give isl_basic_map
*add_ma_strides(
12855 __isl_take isl_basic_map
*bmap
, __isl_keep isl_multi_aff
*ma
,
12856 int n_before
, int n_after
, int n_div_ma
)
12864 total
= isl_basic_map_total_dim(bmap
);
12865 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
12866 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
12867 for (i
= 0; i
< ma
->n
; ++i
) {
12868 int o_bmap
= 0, o_ma
= 1;
12870 if (isl_int_is_one(ma
->p
[i
]->v
->el
[0]))
12872 div
= isl_basic_map_alloc_div(bmap
);
12873 k
= isl_basic_map_alloc_equality(bmap
);
12874 if (div
< 0 || k
< 0)
12876 isl_int_set_si(bmap
->div
[div
][0], 0);
12877 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12878 ma
->p
[i
]->v
->el
+ o_ma
, 1 + n_param
);
12879 o_bmap
+= 1 + n_param
;
12880 o_ma
+= 1 + n_param
;
12881 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_before
);
12882 o_bmap
+= n_before
;
12883 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12884 ma
->p
[i
]->v
->el
+ o_ma
, n_in
);
12887 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_after
);
12889 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
12890 ma
->p
[i
]->v
->el
+ o_ma
, n_div_ma
);
12891 o_bmap
+= n_div_ma
;
12893 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, 1 + total
- o_bmap
);
12894 isl_int_neg(bmap
->eq
[k
][1 + total
], ma
->p
[i
]->v
->el
[0]);
12900 isl_basic_map_free(bmap
);
12904 /* Replace the domain or range space (depending on "type) of "space" by "set".
12906 static __isl_give isl_space
*isl_space_set(__isl_take isl_space
*space
,
12907 enum isl_dim_type type
, __isl_take isl_space
*set
)
12909 if (type
== isl_dim_in
) {
12910 space
= isl_space_range(space
);
12911 space
= isl_space_map_from_domain_and_range(set
, space
);
12913 space
= isl_space_domain(space
);
12914 space
= isl_space_map_from_domain_and_range(space
, set
);
12920 /* Compute the preimage of the domain or range (depending on "type")
12921 * of "bmap" under the function represented by "ma".
12922 * In other words, plug in "ma" in the domain or range of "bmap".
12923 * The result is a basic map that lives in the same space as "bmap"
12924 * except that the domain or range has been replaced by
12925 * the domain space of "ma".
12927 * If bmap is represented by
12929 * A(p) + S u + B x + T v + C(divs) >= 0,
12931 * where u and x are input and output dimensions if type == isl_dim_out
12932 * while x and v are input and output dimensions if type == isl_dim_in,
12933 * and ma is represented by
12935 * x = D(p) + F(y) + G(divs')
12937 * then the result is
12939 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12941 * The divs in the input set are similarly adjusted.
12944 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12948 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
12949 * B_i G(divs') + c_i(divs))/n_i)
12951 * If bmap is not a rational map and if F(y) involves any denominators
12953 * x_i = (f_i y + h_i)/m_i
12955 * then additional constraints are added to ensure that we only
12956 * map back integer points. That is we enforce
12958 * f_i y + h_i = m_i alpha_i
12960 * with alpha_i an additional existentially quantified variable.
12962 * We first copy over the divs from "ma".
12963 * Then we add the modified constraints and divs from "bmap".
12964 * Finally, we add the stride constraints, if needed.
12966 __isl_give isl_basic_map
*isl_basic_map_preimage_multi_aff(
12967 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
12968 __isl_take isl_multi_aff
*ma
)
12972 isl_basic_map
*res
= NULL
;
12973 int n_before
, n_after
, n_div_bmap
, n_div_ma
;
12974 isl_int f
, c1
, c2
, g
;
12975 int rational
, strides
;
12982 ma
= isl_multi_aff_align_divs(ma
);
12985 if (check_basic_map_compatible_range_multi_aff(bmap
, type
, ma
) < 0)
12988 if (type
== isl_dim_in
) {
12990 n_after
= isl_basic_map_dim(bmap
, isl_dim_out
);
12992 n_before
= isl_basic_map_dim(bmap
, isl_dim_in
);
12995 n_div_bmap
= isl_basic_map_dim(bmap
, isl_dim_div
);
12996 n_div_ma
= ma
->n
? isl_aff_dim(ma
->p
[0], isl_dim_div
) : 0;
12998 space
= isl_multi_aff_get_domain_space(ma
);
12999 space
= isl_space_set(isl_basic_map_get_space(bmap
), type
, space
);
13000 rational
= isl_basic_map_is_rational(bmap
);
13001 strides
= rational
? 0 : multi_aff_strides(ma
);
13002 res
= isl_basic_map_alloc_space(space
, n_div_ma
+ n_div_bmap
+ strides
,
13003 bmap
->n_eq
+ strides
, bmap
->n_ineq
+ 2 * n_div_ma
);
13005 res
= isl_basic_map_set_rational(res
);
13007 for (i
= 0; i
< n_div_ma
+ n_div_bmap
; ++i
)
13008 if (isl_basic_map_alloc_div(res
) < 0)
13011 if (set_ma_divs(res
, ma
, n_before
, n_after
, n_div_ma
) < 0)
13014 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
13015 k
= isl_basic_map_alloc_equality(res
);
13018 isl_seq_preimage(res
->eq
[k
], bmap
->eq
[i
], ma
, n_before
,
13019 n_after
, n_div_ma
, n_div_bmap
, f
, c1
, c2
, g
, 0);
13022 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
13023 k
= isl_basic_map_alloc_inequality(res
);
13026 isl_seq_preimage(res
->ineq
[k
], bmap
->ineq
[i
], ma
, n_before
,
13027 n_after
, n_div_ma
, n_div_bmap
, f
, c1
, c2
, g
, 0);
13030 for (i
= 0; i
< bmap
->n_div
; ++i
) {
13031 if (isl_int_is_zero(bmap
->div
[i
][0])) {
13032 isl_int_set_si(res
->div
[n_div_ma
+ i
][0], 0);
13035 isl_seq_preimage(res
->div
[n_div_ma
+ i
], bmap
->div
[i
], ma
,
13036 n_before
, n_after
, n_div_ma
, n_div_bmap
,
13041 res
= add_ma_strides(res
, ma
, n_before
, n_after
, n_div_ma
);
13047 isl_basic_map_free(bmap
);
13048 isl_multi_aff_free(ma
);
13049 res
= isl_basic_set_simplify(res
);
13050 return isl_basic_map_finalize(res
);
13056 isl_basic_map_free(bmap
);
13057 isl_multi_aff_free(ma
);
13058 isl_basic_map_free(res
);
13062 /* Compute the preimage of "bset" under the function represented by "ma".
13063 * In other words, plug in "ma" in "bset". The result is a basic set
13064 * that lives in the domain space of "ma".
13066 __isl_give isl_basic_set
*isl_basic_set_preimage_multi_aff(
13067 __isl_take isl_basic_set
*bset
, __isl_take isl_multi_aff
*ma
)
13069 return isl_basic_map_preimage_multi_aff(bset
, isl_dim_set
, ma
);
13072 /* Compute the preimage of the domain of "bmap" under the function
13073 * represented by "ma".
13074 * In other words, plug in "ma" in the domain of "bmap".
13075 * The result is a basic map that lives in the same space as "bmap"
13076 * except that the domain has been replaced by the domain space of "ma".
13078 __isl_give isl_basic_map
*isl_basic_map_preimage_domain_multi_aff(
13079 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
13081 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_in
, ma
);
13084 /* Compute the preimage of the range of "bmap" under the function
13085 * represented by "ma".
13086 * In other words, plug in "ma" in the range of "bmap".
13087 * The result is a basic map that lives in the same space as "bmap"
13088 * except that the range has been replaced by the domain space of "ma".
13090 __isl_give isl_basic_map
*isl_basic_map_preimage_range_multi_aff(
13091 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
13093 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_out
, ma
);
13096 /* Check if the range of "ma" is compatible with the domain or range
13097 * (depending on "type") of "map".
13098 * Return -1 if anything is wrong.
13100 static int check_map_compatible_range_multi_aff(
13101 __isl_keep isl_map
*map
, enum isl_dim_type type
,
13102 __isl_keep isl_multi_aff
*ma
)
13105 isl_space
*ma_space
;
13107 ma_space
= isl_multi_aff_get_space(ma
);
13108 m
= isl_space_tuple_is_equal(map
->dim
, type
, ma_space
, isl_dim_out
);
13109 isl_space_free(ma_space
);
13111 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
13112 "spaces don't match", return -1);
13116 /* Compute the preimage of the domain or range (depending on "type")
13117 * of "map" under the function represented by "ma".
13118 * In other words, plug in "ma" in the domain or range of "map".
13119 * The result is a map that lives in the same space as "map"
13120 * except that the domain or range has been replaced by
13121 * the domain space of "ma".
13123 * The parameters are assumed to have been aligned.
13125 static __isl_give isl_map
*map_preimage_multi_aff(__isl_take isl_map
*map
,
13126 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
13131 map
= isl_map_cow(map
);
13132 ma
= isl_multi_aff_align_divs(ma
);
13135 if (check_map_compatible_range_multi_aff(map
, type
, ma
) < 0)
13138 for (i
= 0; i
< map
->n
; ++i
) {
13139 map
->p
[i
] = isl_basic_map_preimage_multi_aff(map
->p
[i
], type
,
13140 isl_multi_aff_copy(ma
));
13145 space
= isl_multi_aff_get_domain_space(ma
);
13146 space
= isl_space_set(isl_map_get_space(map
), type
, space
);
13148 isl_space_free(map
->dim
);
13153 isl_multi_aff_free(ma
);
13155 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
13156 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
13159 isl_multi_aff_free(ma
);
13164 /* Compute the preimage of the domain or range (depending on "type")
13165 * of "map" under the function represented by "ma".
13166 * In other words, plug in "ma" in the domain or range of "map".
13167 * The result is a map that lives in the same space as "map"
13168 * except that the domain or range has been replaced by
13169 * the domain space of "ma".
13171 __isl_give isl_map
*isl_map_preimage_multi_aff(__isl_take isl_map
*map
,
13172 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
13177 if (isl_space_match(map
->dim
, isl_dim_param
, ma
->space
, isl_dim_param
))
13178 return map_preimage_multi_aff(map
, type
, ma
);
13180 if (!isl_space_has_named_params(map
->dim
) ||
13181 !isl_space_has_named_params(ma
->space
))
13182 isl_die(map
->ctx
, isl_error_invalid
,
13183 "unaligned unnamed parameters", goto error
);
13184 map
= isl_map_align_params(map
, isl_multi_aff_get_space(ma
));
13185 ma
= isl_multi_aff_align_params(ma
, isl_map_get_space(map
));
13187 return map_preimage_multi_aff(map
, type
, ma
);
13189 isl_multi_aff_free(ma
);
13190 return isl_map_free(map
);
13193 /* Compute the preimage of "set" under the function represented by "ma".
13194 * In other words, plug in "ma" in "set". The result is a set
13195 * that lives in the domain space of "ma".
13197 __isl_give isl_set
*isl_set_preimage_multi_aff(__isl_take isl_set
*set
,
13198 __isl_take isl_multi_aff
*ma
)
13200 return isl_map_preimage_multi_aff(set
, isl_dim_set
, ma
);
13203 /* Compute the preimage of the domain of "map" under the function
13204 * represented by "ma".
13205 * In other words, plug in "ma" in the domain of "map".
13206 * The result is a map that lives in the same space as "map"
13207 * except that the domain has been replaced by the domain space of "ma".
13209 __isl_give isl_map
*isl_map_preimage_domain_multi_aff(__isl_take isl_map
*map
,
13210 __isl_take isl_multi_aff
*ma
)
13212 return isl_map_preimage_multi_aff(map
, isl_dim_in
, ma
);
13215 /* Compute the preimage of the range of "map" under the function
13216 * represented by "ma".
13217 * In other words, plug in "ma" in the range of "map".
13218 * The result is a map that lives in the same space as "map"
13219 * except that the range has been replaced by the domain space of "ma".
13221 __isl_give isl_map
*isl_map_preimage_range_multi_aff(__isl_take isl_map
*map
,
13222 __isl_take isl_multi_aff
*ma
)
13224 return isl_map_preimage_multi_aff(map
, isl_dim_out
, ma
);
13227 /* Compute the preimage of "map" under the function represented by "pma".
13228 * In other words, plug in "pma" in the domain or range of "map".
13229 * The result is a map that lives in the same space as "map",
13230 * except that the space of type "type" has been replaced by
13231 * the domain space of "pma".
13233 * The parameters of "map" and "pma" are assumed to have been aligned.
13235 static __isl_give isl_map
*isl_map_preimage_pw_multi_aff_aligned(
13236 __isl_take isl_map
*map
, enum isl_dim_type type
,
13237 __isl_take isl_pw_multi_aff
*pma
)
13246 isl_pw_multi_aff_free(pma
);
13247 res
= isl_map_empty(isl_map_get_space(map
));
13252 res
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
13253 isl_multi_aff_copy(pma
->p
[0].maff
));
13254 if (type
== isl_dim_in
)
13255 res
= isl_map_intersect_domain(res
,
13256 isl_map_copy(pma
->p
[0].set
));
13258 res
= isl_map_intersect_range(res
,
13259 isl_map_copy(pma
->p
[0].set
));
13261 for (i
= 1; i
< pma
->n
; ++i
) {
13264 res_i
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
13265 isl_multi_aff_copy(pma
->p
[i
].maff
));
13266 if (type
== isl_dim_in
)
13267 res_i
= isl_map_intersect_domain(res_i
,
13268 isl_map_copy(pma
->p
[i
].set
));
13270 res_i
= isl_map_intersect_range(res_i
,
13271 isl_map_copy(pma
->p
[i
].set
));
13272 res
= isl_map_union(res
, res_i
);
13275 isl_pw_multi_aff_free(pma
);
13279 isl_pw_multi_aff_free(pma
);
13284 /* Compute the preimage of "map" under the function represented by "pma".
13285 * In other words, plug in "pma" in the domain or range of "map".
13286 * The result is a map that lives in the same space as "map",
13287 * except that the space of type "type" has been replaced by
13288 * the domain space of "pma".
13290 __isl_give isl_map
*isl_map_preimage_pw_multi_aff(__isl_take isl_map
*map
,
13291 enum isl_dim_type type
, __isl_take isl_pw_multi_aff
*pma
)
13296 if (isl_space_match(map
->dim
, isl_dim_param
, pma
->dim
, isl_dim_param
))
13297 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
13299 if (!isl_space_has_named_params(map
->dim
) ||
13300 !isl_space_has_named_params(pma
->dim
))
13301 isl_die(map
->ctx
, isl_error_invalid
,
13302 "unaligned unnamed parameters", goto error
);
13303 map
= isl_map_align_params(map
, isl_pw_multi_aff_get_space(pma
));
13304 pma
= isl_pw_multi_aff_align_params(pma
, isl_map_get_space(map
));
13306 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
13308 isl_pw_multi_aff_free(pma
);
13309 return isl_map_free(map
);
13312 /* Compute the preimage of "set" under the function represented by "pma".
13313 * In other words, plug in "pma" in "set". The result is a set
13314 * that lives in the domain space of "pma".
13316 __isl_give isl_set
*isl_set_preimage_pw_multi_aff(__isl_take isl_set
*set
,
13317 __isl_take isl_pw_multi_aff
*pma
)
13319 return isl_map_preimage_pw_multi_aff(set
, isl_dim_set
, pma
);
13322 /* Compute the preimage of the domain of "map" under the function
13323 * represented by "pma".
13324 * In other words, plug in "pma" in the domain of "map".
13325 * The result is a map that lives in the same space as "map",
13326 * except that domain space has been replaced by the domain space of "pma".
13328 __isl_give isl_map
*isl_map_preimage_domain_pw_multi_aff(
13329 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
13331 return isl_map_preimage_pw_multi_aff(map
, isl_dim_in
, pma
);
13334 /* Compute the preimage of the range of "map" under the function
13335 * represented by "pma".
13336 * In other words, plug in "pma" in the range of "map".
13337 * The result is a map that lives in the same space as "map",
13338 * except that range space has been replaced by the domain space of "pma".
13340 __isl_give isl_map
*isl_map_preimage_range_pw_multi_aff(
13341 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
13343 return isl_map_preimage_pw_multi_aff(map
, isl_dim_out
, pma
);
13346 /* Compute the preimage of "map" under the function represented by "mpa".
13347 * In other words, plug in "mpa" in the domain or range of "map".
13348 * The result is a map that lives in the same space as "map",
13349 * except that the space of type "type" has been replaced by
13350 * the domain space of "mpa".
13352 * If the map does not involve any constraints that refer to the
13353 * dimensions of the substituted space, then the only possible
13354 * effect of "mpa" on the map is to map the space to a different space.
13355 * We create a separate isl_multi_aff to effectuate this change
13356 * in order to avoid spurious splitting of the map along the pieces
13359 __isl_give isl_map
*isl_map_preimage_multi_pw_aff(__isl_take isl_map
*map
,
13360 enum isl_dim_type type
, __isl_take isl_multi_pw_aff
*mpa
)
13363 isl_pw_multi_aff
*pma
;
13368 n
= isl_map_dim(map
, type
);
13369 if (!isl_map_involves_dims(map
, type
, 0, n
)) {
13373 space
= isl_multi_pw_aff_get_space(mpa
);
13374 isl_multi_pw_aff_free(mpa
);
13375 ma
= isl_multi_aff_zero(space
);
13376 return isl_map_preimage_multi_aff(map
, type
, ma
);
13379 pma
= isl_pw_multi_aff_from_multi_pw_aff(mpa
);
13380 return isl_map_preimage_pw_multi_aff(map
, type
, pma
);
13383 isl_multi_pw_aff_free(mpa
);
13387 /* Compute the preimage of "map" under the function represented by "mpa".
13388 * In other words, plug in "mpa" in the domain "map".
13389 * The result is a map that lives in the same space as "map",
13390 * except that domain space has been replaced by the domain space of "mpa".
13392 __isl_give isl_map
*isl_map_preimage_domain_multi_pw_aff(
13393 __isl_take isl_map
*map
, __isl_take isl_multi_pw_aff
*mpa
)
13395 return isl_map_preimage_multi_pw_aff(map
, isl_dim_in
, mpa
);
13398 /* Compute the preimage of "set" by the function represented by "mpa".
13399 * In other words, plug in "mpa" in "set".
13401 __isl_give isl_set
*isl_set_preimage_multi_pw_aff(__isl_take isl_set
*set
,
13402 __isl_take isl_multi_pw_aff
*mpa
)
13404 return isl_map_preimage_multi_pw_aff(set
, isl_dim_set
, mpa
);
13407 /* Are the "n" "coefficients" starting at "first" of the integer division
13408 * expressions at position "pos1" in "bmap1" and "pos2" in "bmap2" equal
13410 * The "coefficient" at position 0 is the denominator.
13411 * The "coefficient" at position 1 is the constant term.
13413 isl_bool
isl_basic_map_equal_div_expr_part(__isl_keep isl_basic_map
*bmap1
,
13414 int pos1
, __isl_keep isl_basic_map
*bmap2
, int pos2
,
13415 unsigned first
, unsigned n
)
13417 if (isl_basic_map_check_range(bmap1
, isl_dim_div
, pos1
, 1) < 0)
13418 return isl_bool_error
;
13419 if (isl_basic_map_check_range(bmap2
, isl_dim_div
, pos2
, 1) < 0)
13420 return isl_bool_error
;
13421 return isl_seq_eq(bmap1
->div
[pos1
] + first
,
13422 bmap2
->div
[pos2
] + first
, n
);
13425 /* Are the integer division expressions at position "pos1" in "bmap1" and
13426 * "pos2" in "bmap2" equal to each other, except that the constant terms
13429 isl_bool
isl_basic_map_equal_div_expr_except_constant(
13430 __isl_keep isl_basic_map
*bmap1
, int pos1
,
13431 __isl_keep isl_basic_map
*bmap2
, int pos2
)
13436 if (!bmap1
|| !bmap2
)
13437 return isl_bool_error
;
13438 total
= isl_basic_map_total_dim(bmap1
);
13439 if (total
!= isl_basic_map_total_dim(bmap2
))
13440 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
13441 "incomparable div expressions", return isl_bool_error
);
13442 equal
= isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
13444 if (equal
< 0 || !equal
)
13446 equal
= isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
13448 if (equal
< 0 || equal
)
13449 return isl_bool_not(equal
);
13450 return isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
13454 /* Replace the numerator of the constant term of the integer division
13455 * expression at position "div" in "bmap" by "value".
13456 * The caller guarantees that this does not change the meaning
13459 __isl_give isl_basic_map
*isl_basic_map_set_div_expr_constant_num_si_inplace(
13460 __isl_take isl_basic_map
*bmap
, int div
, int value
)
13462 if (isl_basic_map_check_range(bmap
, isl_dim_div
, div
, 1) < 0)
13463 return isl_basic_map_free(bmap
);
13465 isl_int_set_si(bmap
->div
[div
][1], value
);
13470 /* Is the point "inner" internal to inequality constraint "ineq"
13472 * The point is considered to be internal to the inequality constraint,
13473 * if it strictly lies on the positive side of the inequality constraint,
13474 * or if it lies on the constraint and the constraint is lexico-positive.
13476 static isl_bool
is_internal(__isl_keep isl_vec
*inner
,
13477 __isl_keep isl_basic_set
*bset
, int ineq
)
13483 if (!inner
|| !bset
)
13484 return isl_bool_error
;
13486 ctx
= isl_basic_set_get_ctx(bset
);
13487 isl_seq_inner_product(inner
->el
, bset
->ineq
[ineq
], inner
->size
,
13488 &ctx
->normalize_gcd
);
13489 if (!isl_int_is_zero(ctx
->normalize_gcd
))
13490 return isl_int_is_nonneg(ctx
->normalize_gcd
);
13492 total
= isl_basic_set_dim(bset
, isl_dim_all
);
13493 pos
= isl_seq_first_non_zero(bset
->ineq
[ineq
] + 1, total
);
13494 return isl_int_is_pos(bset
->ineq
[ineq
][1 + pos
]);
13497 /* Tighten the inequality constraints of "bset" that are outward with respect
13498 * to the point "vec".
13499 * That is, tighten the constraints that are not satisfied by "vec".
13501 * "vec" is a point internal to some superset S of "bset" that is used
13502 * to make the subsets of S disjoint, by tightening one half of the constraints
13503 * that separate two subsets. In particular, the constraints of S
13504 * are all satisfied by "vec" and should not be tightened.
13505 * Of the internal constraints, those that have "vec" on the outside
13506 * are tightened. The shared facet is included in the adjacent subset
13507 * with the opposite constraint.
13508 * For constraints that saturate "vec", this criterion cannot be used
13509 * to determine which of the two sides should be tightened.
13510 * Instead, the sign of the first non-zero coefficient is used
13511 * to make this choice. Note that this second criterion is never used
13512 * on the constraints of S since "vec" is interior to "S".
13514 __isl_give isl_basic_set
*isl_basic_set_tighten_outward(
13515 __isl_take isl_basic_set
*bset
, __isl_keep isl_vec
*vec
)
13519 bset
= isl_basic_set_cow(bset
);
13522 for (j
= 0; j
< bset
->n_ineq
; ++j
) {
13525 internal
= is_internal(vec
, bset
, j
);
13527 return isl_basic_set_free(bset
);
13530 isl_int_sub_ui(bset
->ineq
[j
][0], bset
->ineq
[j
][0], 1);