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
8 * Copyright 2018-2019 Cerebras Systems
9 * Copyright 2022 Cerebras Systems
11 * Use of this software is governed by the MIT license
13 * Written by Sven Verdoolaege, K.U.Leuven, Departement
14 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
15 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
16 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
17 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
18 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
19 * B.P. 105 - 78153 Le Chesnay, France
20 * and Centre de Recherche Inria de Paris, 2 rue Simone Iff - Voie DQ12,
21 * CS 42112, 75589 Paris Cedex 12, France
22 * and Cerebras Systems, 175 S San Antonio Rd, Los Altos, CA, USA
26 #include <isl_ctx_private.h>
27 #include <isl_map_private.h>
29 #include <isl_id_private.h>
30 #include <isl/constraint.h>
31 #include "isl_space_private.h"
32 #include "isl_equalities.h"
33 #include <isl_lp_private.h>
37 #include <isl_reordering.h>
38 #include "isl_sample.h"
42 #include <isl_mat_private.h>
43 #include <isl_vec_private.h>
44 #include <isl_dim_map.h>
45 #include <isl_local_space_private.h>
46 #include <isl_aff_private.h>
47 #include <isl_options_private.h>
48 #include <isl_morph.h>
49 #include <isl_val_private.h>
50 #include <isl_printer_private.h>
52 #include <bset_to_bmap.c>
53 #include <bset_from_bmap.c>
54 #include <set_to_map.c>
55 #include <set_from_map.c>
57 /* Treat "bset" as a basic map.
58 * Internally, isl_basic_set is defined to isl_basic_map, so in practice,
59 * this function performs a redundant cast.
61 static __isl_keep
const isl_basic_map
*const_bset_to_bmap(
62 __isl_keep
const isl_basic_set
*bset
)
64 return (const isl_basic_map
*) bset
;
68 #define TYPE isl_basic_map
69 #include "has_single_reference_templ.c"
71 static unsigned pos(__isl_keep isl_space
*space
, enum isl_dim_type type
)
74 case isl_dim_param
: return 1;
75 case isl_dim_in
: return 1 + space
->nparam
;
76 case isl_dim_out
: return 1 + space
->nparam
+ space
->n_in
;
81 isl_size
isl_basic_map_dim(__isl_keep isl_basic_map
*bmap
,
82 enum isl_dim_type type
)
85 return isl_size_error
;
87 case isl_dim_cst
: return 1;
90 case isl_dim_out
: return isl_space_dim(bmap
->dim
, type
);
91 case isl_dim_div
: return bmap
->n_div
;
92 case isl_dim_all
: return isl_basic_map_total_dim(bmap
);
97 /* Return the space of "map".
99 __isl_keep isl_space
*isl_map_peek_space(__isl_keep
const isl_map
*map
)
101 return map
? map
->dim
: NULL
;
104 /* Return the space of "set".
106 __isl_keep isl_space
*isl_set_peek_space(__isl_keep isl_set
*set
)
108 return isl_map_peek_space(set_to_map(set
));
111 isl_size
isl_map_dim(__isl_keep isl_map
*map
, enum isl_dim_type type
)
113 return isl_space_dim(isl_map_peek_space(map
), type
);
116 /* Return the dimensionality of the domain (tuple) of the map.
118 isl_size
isl_map_domain_tuple_dim(__isl_keep isl_map
*map
)
120 return isl_map_dim(map
, isl_dim_in
);
123 /* Return the dimensionality of the range (tuple) of the map.
125 isl_size
isl_map_range_tuple_dim(__isl_keep isl_map
*map
)
127 return isl_map_dim(map
, isl_dim_out
);
130 isl_size
isl_set_dim(__isl_keep isl_set
*set
, enum isl_dim_type type
)
132 return isl_map_dim(set_to_map(set
), type
);
135 /* Return the dimensionality of the (tuple of the) set.
137 isl_size
isl_set_tuple_dim(__isl_keep isl_set
*set
)
139 return isl_set_dim(set
, isl_dim_set
);
142 /* Return the position of the variables of the given type
143 * within the sequence of variables of "bmap".
145 isl_size
isl_basic_map_var_offset(__isl_keep isl_basic_map
*bmap
,
146 enum isl_dim_type type
)
150 space
= isl_basic_map_peek_space(bmap
);
155 case isl_dim_out
: return isl_space_offset(space
, type
);
156 case isl_dim_div
: return isl_space_dim(space
, isl_dim_all
);
159 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
160 "invalid dimension type", return isl_size_error
);
164 /* Return the position of the variables of the given type
165 * within the sequence of variables of "bset".
167 isl_size
isl_basic_set_var_offset(__isl_keep isl_basic_set
*bset
,
168 enum isl_dim_type type
)
170 return isl_basic_map_var_offset(bset_to_bmap(bset
), type
);
173 /* Return the position of the coefficients of the variables of the given type
174 * within the sequence of coefficients of "bmap".
176 unsigned isl_basic_map_offset(__isl_keep isl_basic_map
*bmap
,
177 enum isl_dim_type type
)
180 case isl_dim_cst
: return 0;
184 case isl_dim_div
: return 1 + isl_basic_map_var_offset(bmap
, type
);
189 unsigned isl_basic_set_offset(__isl_keep isl_basic_set
*bset
,
190 enum isl_dim_type type
)
192 return isl_basic_map_offset(bset
, type
);
195 static unsigned map_offset(__isl_keep isl_map
*map
, enum isl_dim_type type
)
197 return pos(map
->dim
, type
);
200 isl_size
isl_basic_set_dim(__isl_keep isl_basic_set
*bset
,
201 enum isl_dim_type type
)
203 return isl_basic_map_dim(bset
, type
);
206 isl_size
isl_basic_set_n_dim(__isl_keep isl_basic_set
*bset
)
208 return isl_basic_set_dim(bset
, isl_dim_set
);
211 isl_size
isl_basic_set_n_param(__isl_keep isl_basic_set
*bset
)
213 return isl_basic_set_dim(bset
, isl_dim_param
);
216 isl_size
isl_basic_set_total_dim(__isl_keep
const isl_basic_set
*bset
)
218 return isl_basic_map_total_dim(const_bset_to_bmap(bset
));
221 isl_size
isl_set_n_dim(__isl_keep isl_set
*set
)
223 return isl_set_dim(set
, isl_dim_set
);
226 isl_size
isl_set_n_param(__isl_keep isl_set
*set
)
228 return isl_set_dim(set
, isl_dim_param
);
231 isl_size
isl_basic_map_total_dim(__isl_keep
const isl_basic_map
*bmap
)
236 return isl_size_error
;
237 dim
= isl_space_dim(bmap
->dim
, isl_dim_all
);
239 return isl_size_error
;
240 return dim
+ bmap
->n_div
;
243 /* Return the number of equality constraints in the description of "bmap".
244 * Return isl_size_error on error.
246 isl_size
isl_basic_map_n_equality(__isl_keep isl_basic_map
*bmap
)
249 return isl_size_error
;
253 /* Return the number of equality constraints in the description of "bset".
254 * Return isl_size_error on error.
256 isl_size
isl_basic_set_n_equality(__isl_keep isl_basic_set
*bset
)
258 return isl_basic_map_n_equality(bset_to_bmap(bset
));
261 /* Return the number of inequality constraints in the description of "bmap".
262 * Return isl_size_error on error.
264 isl_size
isl_basic_map_n_inequality(__isl_keep isl_basic_map
*bmap
)
267 return isl_size_error
;
271 /* Return the number of inequality constraints in the description of "bset".
272 * Return isl_size_error on error.
274 isl_size
isl_basic_set_n_inequality(__isl_keep isl_basic_set
*bset
)
276 return isl_basic_map_n_inequality(bset_to_bmap(bset
));
279 /* Do "bmap1" and "bmap2" have the same parameters?
281 static isl_bool
isl_basic_map_has_equal_params(__isl_keep isl_basic_map
*bmap1
,
282 __isl_keep isl_basic_map
*bmap2
)
284 isl_space
*space1
, *space2
;
286 space1
= isl_basic_map_peek_space(bmap1
);
287 space2
= isl_basic_map_peek_space(bmap2
);
288 return isl_space_has_equal_params(space1
, space2
);
291 /* Do "map1" and "map2" have the same parameters?
293 isl_bool
isl_map_has_equal_params(__isl_keep isl_map
*map1
,
294 __isl_keep isl_map
*map2
)
296 isl_space
*space1
, *space2
;
298 space1
= isl_map_peek_space(map1
);
299 space2
= isl_map_peek_space(map2
);
300 return isl_space_has_equal_params(space1
, space2
);
303 /* Do "map" and "set" have the same parameters?
305 static isl_bool
isl_map_set_has_equal_params(__isl_keep isl_map
*map
,
306 __isl_keep isl_set
*set
)
308 return isl_map_has_equal_params(map
, set_to_map(set
));
311 /* Is the tuple of type "type" of "bmap" the same as the single tuple of "bset"?
313 static isl_bool
isl_basic_map_set_tuple_is_equal(__isl_keep isl_basic_map
*bmap
,
314 enum isl_dim_type type
, __isl_keep isl_basic_set
*bset
)
316 isl_space
*bmap_space
, *bset_space
;
318 bmap_space
= isl_basic_map_peek_space(bmap
);
319 bset_space
= isl_basic_set_peek_space(bset
);
320 return isl_space_tuple_is_equal(bmap_space
, type
,
321 bset_space
, isl_dim_set
);
324 /* Is the tuple of type "type" of "map" the same as the single tuple of "set"?
326 static isl_bool
isl_map_set_tuple_is_equal(__isl_keep isl_map
*map
,
327 enum isl_dim_type type
, __isl_keep isl_set
*set
)
329 return isl_map_tuple_is_equal(map
, type
, set_to_map(set
), isl_dim_set
);
332 isl_bool
isl_map_compatible_domain(__isl_keep isl_map
*map
,
333 __isl_keep isl_set
*set
)
337 return isl_bool_error
;
338 m
= isl_map_has_equal_params(map
, set_to_map(set
));
341 return isl_map_set_tuple_is_equal(map
, isl_dim_in
, set
);
344 isl_bool
isl_basic_map_compatible_domain(__isl_keep isl_basic_map
*bmap
,
345 __isl_keep isl_basic_set
*bset
)
349 return isl_bool_error
;
350 m
= isl_basic_map_has_equal_params(bmap
, bset_to_bmap(bset
));
353 return isl_basic_map_set_tuple_is_equal(bmap
, isl_dim_in
, bset
);
356 isl_bool
isl_map_compatible_range(__isl_keep isl_map
*map
,
357 __isl_keep isl_set
*set
)
361 return isl_bool_error
;
362 m
= isl_map_has_equal_params(map
, set_to_map(set
));
365 return isl_map_set_tuple_is_equal(map
, isl_dim_out
, set
);
368 isl_bool
isl_basic_map_compatible_range(__isl_keep isl_basic_map
*bmap
,
369 __isl_keep isl_basic_set
*bset
)
373 return isl_bool_error
;
374 m
= isl_basic_map_has_equal_params(bmap
, bset_to_bmap(bset
));
377 return isl_basic_map_set_tuple_is_equal(bmap
, isl_dim_out
, bset
);
380 isl_ctx
*isl_basic_map_get_ctx(__isl_keep isl_basic_map
*bmap
)
382 return bmap
? bmap
->ctx
: NULL
;
385 isl_ctx
*isl_basic_set_get_ctx(__isl_keep isl_basic_set
*bset
)
387 return bset
? bset
->ctx
: NULL
;
390 isl_ctx
*isl_map_get_ctx(__isl_keep isl_map
*map
)
392 return map
? map
->ctx
: NULL
;
395 isl_ctx
*isl_set_get_ctx(__isl_keep isl_set
*set
)
397 return set
? set
->ctx
: NULL
;
400 /* Return the space of "bmap".
402 __isl_keep isl_space
*isl_basic_map_peek_space(
403 __isl_keep
const isl_basic_map
*bmap
)
405 return bmap
? bmap
->dim
: NULL
;
408 /* Return the space of "bset".
410 __isl_keep isl_space
*isl_basic_set_peek_space(__isl_keep isl_basic_set
*bset
)
412 return isl_basic_map_peek_space(bset_to_bmap(bset
));
415 __isl_give isl_space
*isl_basic_map_get_space(__isl_keep isl_basic_map
*bmap
)
417 return isl_space_copy(isl_basic_map_peek_space(bmap
));
420 __isl_give isl_space
*isl_basic_set_get_space(__isl_keep isl_basic_set
*bset
)
422 return isl_basic_map_get_space(bset_to_bmap(bset
));
425 /* Return the space of "bmap".
426 * This may be either a copy or the space itself
427 * if there is only one reference to "bmap".
428 * This allows the space to be modified inplace
429 * if both the basic map and its space have only a single reference.
430 * The caller is not allowed to modify "bmap" between this call and
431 * a subsequent call to isl_basic_map_restore_space.
432 * The only exception is that isl_basic_map_free can be called instead.
434 static __isl_give isl_space
*isl_basic_map_take_space(
435 __isl_keep isl_basic_map
*bmap
)
442 return isl_basic_map_get_space(bmap
);
448 /* Set the space of "bmap" to "space", where the space of "bmap" may be missing
449 * due to a preceding call to isl_basic_map_take_space.
450 * However, in this case, "bmap" only has a single reference and
451 * then the call to isl_basic_map_cow has no effect.
453 static __isl_give isl_basic_map
*isl_basic_map_restore_space(
454 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*space
)
459 if (bmap
->dim
== space
) {
460 isl_space_free(space
);
464 bmap
= isl_basic_map_cow(bmap
);
467 isl_space_free(bmap
->dim
);
472 isl_basic_map_free(bmap
);
473 isl_space_free(space
);
477 /* Extract the divs in "bmap" as a matrix.
479 __isl_give isl_mat
*isl_basic_map_get_divs(__isl_keep isl_basic_map
*bmap
)
487 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
491 ctx
= isl_basic_map_get_ctx(bmap
);
492 cols
= 1 + 1 + v_div
+ bmap
->n_div
;
493 div
= isl_mat_alloc(ctx
, bmap
->n_div
, cols
);
497 for (i
= 0; i
< bmap
->n_div
; ++i
)
498 isl_seq_cpy(div
->row
[i
], bmap
->div
[i
], cols
);
503 /* Extract the divs in "bset" as a matrix.
505 __isl_give isl_mat
*isl_basic_set_get_divs(__isl_keep isl_basic_set
*bset
)
507 return isl_basic_map_get_divs(bset
);
510 __isl_give isl_local_space
*isl_basic_map_get_local_space(
511 __isl_keep isl_basic_map
*bmap
)
518 div
= isl_basic_map_get_divs(bmap
);
519 return isl_local_space_alloc_div(isl_space_copy(bmap
->dim
), div
);
522 __isl_give isl_local_space
*isl_basic_set_get_local_space(
523 __isl_keep isl_basic_set
*bset
)
525 return isl_basic_map_get_local_space(bset
);
528 /* For each known div d = floor(f/m), add the constraints
531 * -(f-(m-1)) + m d >= 0
533 * Do not finalize the result.
535 static __isl_give isl_basic_map
*add_known_div_constraints(
536 __isl_take isl_basic_map
*bmap
)
541 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
543 return isl_basic_map_free(bmap
);
546 bmap
= isl_basic_map_cow(bmap
);
547 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 2 * n_div
);
550 for (i
= 0; i
< n_div
; ++i
) {
551 if (isl_int_is_zero(bmap
->div
[i
][0]))
553 bmap
= isl_basic_map_add_div_constraints(bmap
, i
);
559 __isl_give isl_basic_map
*isl_basic_map_from_local_space(
560 __isl_take isl_local_space
*ls
)
566 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
568 ls
= isl_local_space_free(ls
);
572 bmap
= isl_basic_map_alloc_space(isl_local_space_get_space(ls
),
573 n_div
, 0, 2 * n_div
);
575 for (i
= 0; i
< n_div
; ++i
)
576 if (isl_basic_map_alloc_div(bmap
) < 0)
579 for (i
= 0; i
< n_div
; ++i
)
580 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], ls
->div
->n_col
);
581 bmap
= add_known_div_constraints(bmap
);
583 isl_local_space_free(ls
);
586 isl_local_space_free(ls
);
587 isl_basic_map_free(bmap
);
591 __isl_give isl_basic_set
*isl_basic_set_from_local_space(
592 __isl_take isl_local_space
*ls
)
594 return isl_basic_map_from_local_space(ls
);
597 __isl_give isl_space
*isl_map_get_space(__isl_keep isl_map
*map
)
599 return isl_space_copy(isl_map_peek_space(map
));
602 __isl_give isl_space
*isl_set_get_space(__isl_keep isl_set
*set
)
606 return isl_space_copy(set
->dim
);
609 /* Return the space of "map".
610 * This may be either a copy or the space itself
611 * if there is only one reference to "map".
612 * This allows the space to be modified inplace
613 * if both the map and its space have only a single reference.
614 * The caller is not allowed to modify "map" between this call and
615 * a subsequent call to isl_map_restore_space.
616 * The only exception is that isl_map_free can be called instead.
618 static __isl_give isl_space
*isl_map_take_space(__isl_keep isl_map
*map
)
625 return isl_map_get_space(map
);
631 /* Set the space of "map" to "space", where the space of "map" may be missing
632 * due to a preceding call to isl_map_take_space.
633 * However, in this case, "map" only has a single reference and
634 * then the call to isl_map_cow has no effect.
636 static __isl_give isl_map
*isl_map_restore_space(__isl_take isl_map
*map
,
637 __isl_take isl_space
*space
)
642 if (map
->dim
== space
) {
643 isl_space_free(space
);
647 map
= isl_map_cow(map
);
650 isl_space_free(map
->dim
);
656 isl_space_free(space
);
660 __isl_give isl_basic_map
*isl_basic_map_set_tuple_name(
661 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
, const char *s
)
665 space
= isl_basic_map_take_space(bmap
);
666 space
= isl_space_set_tuple_name(space
, type
, s
);
667 bmap
= isl_basic_map_restore_space(bmap
, space
);
668 bmap
= isl_basic_map_finalize(bmap
);
672 __isl_give isl_basic_set
*isl_basic_set_set_tuple_name(
673 __isl_take isl_basic_set
*bset
, const char *s
)
675 return isl_basic_map_set_tuple_name(bset
, isl_dim_set
, s
);
678 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map
*bmap
,
679 enum isl_dim_type type
)
681 return bmap
? isl_space_get_tuple_name(bmap
->dim
, type
) : NULL
;
684 __isl_give isl_map
*isl_map_set_tuple_name(__isl_take isl_map
*map
,
685 enum isl_dim_type type
, const char *s
)
690 map
= isl_map_cow(map
);
694 for (i
= 0; i
< map
->n
; ++i
) {
695 map
->p
[i
] = isl_basic_map_set_tuple_name(map
->p
[i
], type
, s
);
700 space
= isl_map_take_space(map
);
701 space
= isl_space_set_tuple_name(space
, type
, s
);
702 map
= isl_map_restore_space(map
, space
);
710 /* Replace the identifier of the tuple of type "type" by "id".
712 __isl_give isl_basic_map
*isl_basic_map_set_tuple_id(
713 __isl_take isl_basic_map
*bmap
,
714 enum isl_dim_type type
, __isl_take isl_id
*id
)
718 space
= isl_basic_map_take_space(bmap
);
719 space
= isl_space_set_tuple_id(space
, type
, id
);
720 bmap
= isl_basic_map_restore_space(bmap
, space
);
721 bmap
= isl_basic_map_finalize(bmap
);
725 /* Replace the identifier of the tuple by "id".
727 __isl_give isl_basic_set
*isl_basic_set_set_tuple_id(
728 __isl_take isl_basic_set
*bset
, __isl_take isl_id
*id
)
730 return isl_basic_map_set_tuple_id(bset
, isl_dim_set
, id
);
733 /* Does the input or output tuple have a name?
735 isl_bool
isl_map_has_tuple_name(__isl_keep isl_map
*map
, enum isl_dim_type type
)
737 return map
? isl_space_has_tuple_name(map
->dim
, type
) : isl_bool_error
;
740 const char *isl_map_get_tuple_name(__isl_keep isl_map
*map
,
741 enum isl_dim_type type
)
743 return map
? isl_space_get_tuple_name(map
->dim
, type
) : NULL
;
746 __isl_give isl_set
*isl_set_set_tuple_name(__isl_take isl_set
*set
,
749 return set_from_map(isl_map_set_tuple_name(set_to_map(set
),
753 __isl_give isl_map
*isl_map_set_tuple_id(__isl_take isl_map
*map
,
754 enum isl_dim_type type
, __isl_take isl_id
*id
)
758 space
= isl_map_take_space(map
);
759 space
= isl_space_set_tuple_id(space
, type
, id
);
760 map
= isl_map_restore_space(map
, space
);
762 return isl_map_reset_space(map
, isl_map_get_space(map
));
765 /* Replace the identifier of the domain tuple of "map" by "id".
767 __isl_give isl_map
*isl_map_set_domain_tuple_id(__isl_take isl_map
*map
,
768 __isl_take isl_id
*id
)
770 return isl_map_set_tuple_id(map
, isl_dim_in
, id
);
773 /* Replace the identifier of the range tuple of "map" by "id".
775 __isl_give isl_map
*isl_map_set_range_tuple_id(__isl_take isl_map
*map
,
776 __isl_take isl_id
*id
)
778 return isl_map_set_tuple_id(map
, isl_dim_out
, id
);
781 __isl_give isl_set
*isl_set_set_tuple_id(__isl_take isl_set
*set
,
782 __isl_take isl_id
*id
)
784 return isl_map_set_tuple_id(set
, isl_dim_set
, id
);
787 __isl_give isl_map
*isl_map_reset_tuple_id(__isl_take isl_map
*map
,
788 enum isl_dim_type type
)
792 space
= isl_map_take_space(map
);
793 space
= isl_space_reset_tuple_id(space
, type
);
794 map
= isl_map_restore_space(map
, space
);
796 return isl_map_reset_space(map
, isl_map_get_space(map
));
799 __isl_give isl_set
*isl_set_reset_tuple_id(__isl_take isl_set
*set
)
801 return isl_map_reset_tuple_id(set
, isl_dim_set
);
804 isl_bool
isl_map_has_tuple_id(__isl_keep isl_map
*map
, enum isl_dim_type type
)
806 return map
? isl_space_has_tuple_id(map
->dim
, type
) : isl_bool_error
;
809 /* Does the domain tuple of "map" have an identifier?
811 isl_bool
isl_map_has_domain_tuple_id(__isl_keep isl_map
*map
)
813 return isl_map_has_tuple_id(map
, isl_dim_in
);
816 /* Does the range tuple of "map" have an identifier?
818 isl_bool
isl_map_has_range_tuple_id(__isl_keep isl_map
*map
)
820 return isl_map_has_tuple_id(map
, isl_dim_out
);
823 __isl_give isl_id
*isl_map_get_tuple_id(__isl_keep isl_map
*map
,
824 enum isl_dim_type type
)
826 return map
? isl_space_get_tuple_id(map
->dim
, type
) : NULL
;
829 /* Return the identifier of the domain tuple of "map", assuming it has one.
831 __isl_give isl_id
*isl_map_get_domain_tuple_id(__isl_keep isl_map
*map
)
833 return isl_map_get_tuple_id(map
, isl_dim_in
);
836 /* Return the identifier of the range tuple of "map", assuming it has one.
838 __isl_give isl_id
*isl_map_get_range_tuple_id(__isl_keep isl_map
*map
)
840 return isl_map_get_tuple_id(map
, isl_dim_out
);
843 isl_bool
isl_set_has_tuple_id(__isl_keep isl_set
*set
)
845 return isl_map_has_tuple_id(set
, isl_dim_set
);
848 __isl_give isl_id
*isl_set_get_tuple_id(__isl_keep isl_set
*set
)
850 return isl_map_get_tuple_id(set
, isl_dim_set
);
853 /* Does the set tuple have a name?
855 isl_bool
isl_set_has_tuple_name(__isl_keep isl_set
*set
)
858 return isl_bool_error
;
859 return isl_space_has_tuple_name(set
->dim
, isl_dim_set
);
863 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set
*bset
)
865 return bset
? isl_space_get_tuple_name(bset
->dim
, isl_dim_set
) : NULL
;
868 const char *isl_set_get_tuple_name(__isl_keep isl_set
*set
)
870 return set
? isl_space_get_tuple_name(set
->dim
, isl_dim_set
) : NULL
;
873 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map
*bmap
,
874 enum isl_dim_type type
, unsigned pos
)
876 return bmap
? isl_space_get_dim_name(bmap
->dim
, type
, pos
) : NULL
;
879 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set
*bset
,
880 enum isl_dim_type type
, unsigned pos
)
882 return bset
? isl_space_get_dim_name(bset
->dim
, type
, pos
) : NULL
;
885 /* Does the given dimension have a name?
887 isl_bool
isl_map_has_dim_name(__isl_keep isl_map
*map
,
888 enum isl_dim_type type
, unsigned pos
)
891 return isl_bool_error
;
892 return isl_space_has_dim_name(map
->dim
, type
, pos
);
895 const char *isl_map_get_dim_name(__isl_keep isl_map
*map
,
896 enum isl_dim_type type
, unsigned pos
)
898 return map
? isl_space_get_dim_name(map
->dim
, type
, pos
) : NULL
;
901 const char *isl_set_get_dim_name(__isl_keep isl_set
*set
,
902 enum isl_dim_type type
, unsigned pos
)
904 return set
? isl_space_get_dim_name(set
->dim
, type
, pos
) : NULL
;
907 /* Does the given dimension have a name?
909 isl_bool
isl_set_has_dim_name(__isl_keep isl_set
*set
,
910 enum isl_dim_type type
, unsigned pos
)
913 return isl_bool_error
;
914 return isl_space_has_dim_name(set
->dim
, type
, pos
);
917 __isl_give isl_basic_map
*isl_basic_map_set_dim_name(
918 __isl_take isl_basic_map
*bmap
,
919 enum isl_dim_type type
, unsigned pos
, const char *s
)
923 space
= isl_basic_map_take_space(bmap
);
924 space
= isl_space_set_dim_name(space
, type
, pos
, s
);
925 bmap
= isl_basic_map_restore_space(bmap
, space
);
926 return isl_basic_map_finalize(bmap
);
929 __isl_give isl_map
*isl_map_set_dim_name(__isl_take isl_map
*map
,
930 enum isl_dim_type type
, unsigned pos
, const char *s
)
935 map
= isl_map_cow(map
);
939 for (i
= 0; i
< map
->n
; ++i
) {
940 map
->p
[i
] = isl_basic_map_set_dim_name(map
->p
[i
], type
, pos
, s
);
945 space
= isl_map_take_space(map
);
946 space
= isl_space_set_dim_name(space
, type
, pos
, s
);
947 map
= isl_map_restore_space(map
, space
);
955 __isl_give isl_basic_set
*isl_basic_set_set_dim_name(
956 __isl_take isl_basic_set
*bset
,
957 enum isl_dim_type type
, unsigned pos
, const char *s
)
959 return bset_from_bmap(isl_basic_map_set_dim_name(bset_to_bmap(bset
),
963 __isl_give isl_set
*isl_set_set_dim_name(__isl_take isl_set
*set
,
964 enum isl_dim_type type
, unsigned pos
, const char *s
)
966 return set_from_map(isl_map_set_dim_name(set_to_map(set
),
970 isl_bool
isl_basic_map_has_dim_id(__isl_keep isl_basic_map
*bmap
,
971 enum isl_dim_type type
, unsigned pos
)
974 return isl_bool_error
;
975 return isl_space_has_dim_id(bmap
->dim
, type
, pos
);
978 __isl_give isl_id
*isl_basic_set_get_dim_id(__isl_keep isl_basic_set
*bset
,
979 enum isl_dim_type type
, unsigned pos
)
981 return bset
? isl_space_get_dim_id(bset
->dim
, type
, pos
) : NULL
;
984 isl_bool
isl_map_has_dim_id(__isl_keep isl_map
*map
,
985 enum isl_dim_type type
, unsigned pos
)
987 return map
? isl_space_has_dim_id(map
->dim
, type
, pos
) : isl_bool_error
;
990 __isl_give isl_id
*isl_map_get_dim_id(__isl_keep isl_map
*map
,
991 enum isl_dim_type type
, unsigned pos
)
993 return map
? isl_space_get_dim_id(map
->dim
, type
, pos
) : NULL
;
996 isl_bool
isl_set_has_dim_id(__isl_keep isl_set
*set
,
997 enum isl_dim_type type
, unsigned pos
)
999 return isl_map_has_dim_id(set
, type
, pos
);
1002 __isl_give isl_id
*isl_set_get_dim_id(__isl_keep isl_set
*set
,
1003 enum isl_dim_type type
, unsigned pos
)
1005 return isl_map_get_dim_id(set
, type
, pos
);
1008 __isl_give isl_map
*isl_map_set_dim_id(__isl_take isl_map
*map
,
1009 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
1013 space
= isl_map_take_space(map
);
1014 space
= isl_space_set_dim_id(space
, type
, pos
, id
);
1015 map
= isl_map_restore_space(map
, space
);
1017 return isl_map_reset_space(map
, isl_map_get_space(map
));
1020 __isl_give isl_set
*isl_set_set_dim_id(__isl_take isl_set
*set
,
1021 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
1023 return isl_map_set_dim_id(set
, type
, pos
, id
);
1026 int isl_map_find_dim_by_id(__isl_keep isl_map
*map
, enum isl_dim_type type
,
1027 __isl_keep isl_id
*id
)
1031 return isl_space_find_dim_by_id(map
->dim
, type
, id
);
1034 int isl_set_find_dim_by_id(__isl_keep isl_set
*set
, enum isl_dim_type type
,
1035 __isl_keep isl_id
*id
)
1037 return isl_map_find_dim_by_id(set
, type
, id
);
1040 /* Return the position of the dimension of the given type and name
1042 * Return -1 if no such dimension can be found.
1044 int isl_basic_map_find_dim_by_name(__isl_keep isl_basic_map
*bmap
,
1045 enum isl_dim_type type
, const char *name
)
1049 return isl_space_find_dim_by_name(bmap
->dim
, type
, name
);
1052 int isl_map_find_dim_by_name(__isl_keep isl_map
*map
, enum isl_dim_type type
,
1057 return isl_space_find_dim_by_name(map
->dim
, type
, name
);
1060 int isl_set_find_dim_by_name(__isl_keep isl_set
*set
, enum isl_dim_type type
,
1063 return isl_map_find_dim_by_name(set
, type
, name
);
1066 /* Check whether equality i of bset is a pure stride constraint
1067 * on a single dimension, i.e., of the form
1071 * with k a constant and e an existentially quantified variable.
1073 isl_bool
isl_basic_set_eq_is_stride(__isl_keep isl_basic_set
*bset
, int i
)
1081 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
1082 d
= isl_basic_set_dim(bset
, isl_dim_set
);
1083 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
1084 if (nparam
< 0 || d
< 0 || n_div
< 0)
1085 return isl_bool_error
;
1087 if (!isl_int_is_zero(bset
->eq
[i
][0]))
1088 return isl_bool_false
;
1090 if (isl_seq_first_non_zero(bset
->eq
[i
] + 1, nparam
) != -1)
1091 return isl_bool_false
;
1092 pos1
= isl_seq_first_non_zero(bset
->eq
[i
] + 1 + nparam
, d
);
1094 return isl_bool_false
;
1095 if (isl_seq_first_non_zero(bset
->eq
[i
] + 1 + nparam
+ pos1
+ 1,
1096 d
- pos1
- 1) != -1)
1097 return isl_bool_false
;
1099 pos2
= isl_seq_first_non_zero(bset
->eq
[i
] + 1 + nparam
+ d
, n_div
);
1101 return isl_bool_false
;
1102 if (isl_seq_first_non_zero(bset
->eq
[i
] + 1 + nparam
+ d
+ pos2
+ 1,
1103 n_div
- pos2
- 1) != -1)
1104 return isl_bool_false
;
1105 if (!isl_int_is_one(bset
->eq
[i
][1 + nparam
+ pos1
]) &&
1106 !isl_int_is_negone(bset
->eq
[i
][1 + nparam
+ pos1
]))
1107 return isl_bool_false
;
1109 return isl_bool_true
;
1112 /* Reset the user pointer on all identifiers of parameters and tuples
1113 * of the space of "map".
1115 __isl_give isl_map
*isl_map_reset_user(__isl_take isl_map
*map
)
1119 space
= isl_map_get_space(map
);
1120 space
= isl_space_reset_user(space
);
1121 map
= isl_map_reset_space(map
, space
);
1126 /* Reset the user pointer on all identifiers of parameters and tuples
1127 * of the space of "set".
1129 __isl_give isl_set
*isl_set_reset_user(__isl_take isl_set
*set
)
1131 return isl_map_reset_user(set
);
1134 isl_bool
isl_basic_map_is_rational(__isl_keep isl_basic_map
*bmap
)
1137 return isl_bool_error
;
1138 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
1141 /* Has "map" been marked as a rational map?
1142 * In particular, have all basic maps in "map" been marked this way?
1143 * An empty map is not considered to be rational.
1144 * Maps where only some of the basic maps are marked rational
1147 isl_bool
isl_map_is_rational(__isl_keep isl_map
*map
)
1153 return isl_bool_error
;
1155 return isl_bool_false
;
1156 rational
= isl_basic_map_is_rational(map
->p
[0]);
1159 for (i
= 1; i
< map
->n
; ++i
) {
1160 isl_bool rational_i
;
1162 rational_i
= isl_basic_map_is_rational(map
->p
[i
]);
1165 if (rational
!= rational_i
)
1166 isl_die(isl_map_get_ctx(map
), isl_error_unsupported
,
1167 "mixed rational and integer basic maps "
1168 "not supported", return isl_bool_error
);
1174 /* Has "set" been marked as a rational set?
1175 * In particular, have all basic set in "set" been marked this way?
1176 * An empty set is not considered to be rational.
1177 * Sets where only some of the basic sets are marked rational
1180 isl_bool
isl_set_is_rational(__isl_keep isl_set
*set
)
1182 return isl_map_is_rational(set
);
1185 int isl_basic_set_is_rational(__isl_keep isl_basic_set
*bset
)
1187 return isl_basic_map_is_rational(bset
);
1190 /* Does "bmap" contain any rational points?
1192 * If "bmap" has an equality for each dimension, equating the dimension
1193 * to an integer constant, then it has no rational points, even if it
1194 * is marked as rational.
1196 isl_bool
isl_basic_map_has_rational(__isl_keep isl_basic_map
*bmap
)
1198 isl_bool has_rational
= isl_bool_true
;
1202 return isl_bool_error
;
1203 if (isl_basic_map_plain_is_empty(bmap
))
1204 return isl_bool_false
;
1205 if (!isl_basic_map_is_rational(bmap
))
1206 return isl_bool_false
;
1207 bmap
= isl_basic_map_copy(bmap
);
1208 bmap
= isl_basic_map_implicit_equalities(bmap
);
1209 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
1211 return isl_bool_error
;
1212 if (bmap
->n_eq
== total
) {
1214 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
1215 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1, total
);
1218 if (!isl_int_is_one(bmap
->eq
[i
][1 + j
]) &&
1219 !isl_int_is_negone(bmap
->eq
[i
][1 + j
]))
1221 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + j
+ 1,
1226 if (i
== bmap
->n_eq
)
1227 has_rational
= isl_bool_false
;
1229 isl_basic_map_free(bmap
);
1231 return has_rational
;
1234 /* Does "map" contain any rational points?
1236 isl_bool
isl_map_has_rational(__isl_keep isl_map
*map
)
1239 isl_bool has_rational
;
1242 return isl_bool_error
;
1243 for (i
= 0; i
< map
->n
; ++i
) {
1244 has_rational
= isl_basic_map_has_rational(map
->p
[i
]);
1245 if (has_rational
< 0 || has_rational
)
1246 return has_rational
;
1248 return isl_bool_false
;
1251 /* Does "set" contain any rational points?
1253 isl_bool
isl_set_has_rational(__isl_keep isl_set
*set
)
1255 return isl_map_has_rational(set
);
1258 /* Is this basic set a parameter domain?
1260 isl_bool
isl_basic_set_is_params(__isl_keep isl_basic_set
*bset
)
1263 return isl_bool_error
;
1264 return isl_space_is_params(bset
->dim
);
1267 /* Is this set a parameter domain?
1269 isl_bool
isl_set_is_params(__isl_keep isl_set
*set
)
1272 return isl_bool_error
;
1273 return isl_space_is_params(set
->dim
);
1276 /* Is this map actually a parameter domain?
1277 * Users should never call this function. Outside of isl,
1278 * a map can never be a parameter domain.
1280 isl_bool
isl_map_is_params(__isl_keep isl_map
*map
)
1283 return isl_bool_error
;
1284 return isl_space_is_params(map
->dim
);
1287 static __isl_give isl_basic_map
*basic_map_init(isl_ctx
*ctx
,
1288 __isl_take isl_basic_map
*bmap
, unsigned extra
,
1289 unsigned n_eq
, unsigned n_ineq
)
1292 isl_space
*space
= isl_basic_map_peek_space(bmap
);
1293 isl_size n_var
= isl_space_dim(space
, isl_dim_all
);
1294 size_t row_size
= 1 + n_var
+ extra
;
1300 return isl_basic_map_free(bmap
);
1302 bmap
->block
= isl_blk_alloc(ctx
, (n_ineq
+ n_eq
) * row_size
);
1303 if (isl_blk_is_error(bmap
->block
))
1306 bmap
->ineq
= isl_alloc_array(ctx
, isl_int
*, n_ineq
+ n_eq
);
1307 if ((n_ineq
+ n_eq
) && !bmap
->ineq
)
1311 bmap
->block2
= isl_blk_empty();
1314 bmap
->block2
= isl_blk_alloc(ctx
, extra
* (1 + row_size
));
1315 if (isl_blk_is_error(bmap
->block2
))
1318 bmap
->div
= isl_alloc_array(ctx
, isl_int
*, extra
);
1323 for (i
= 0; i
< n_ineq
+ n_eq
; ++i
)
1324 bmap
->ineq
[i
] = bmap
->block
.data
+ i
* row_size
;
1326 for (i
= 0; i
< extra
; ++i
)
1327 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + row_size
);
1331 bmap
->c_size
= n_eq
+ n_ineq
;
1332 bmap
->eq
= bmap
->ineq
+ n_ineq
;
1333 bmap
->extra
= extra
;
1337 bmap
->sample
= NULL
;
1341 isl_basic_map_free(bmap
);
1345 __isl_give isl_basic_set
*isl_basic_set_alloc(isl_ctx
*ctx
,
1346 unsigned nparam
, unsigned dim
, unsigned extra
,
1347 unsigned n_eq
, unsigned n_ineq
)
1349 struct isl_basic_map
*bmap
;
1352 space
= isl_space_set_alloc(ctx
, nparam
, dim
);
1356 bmap
= isl_basic_map_alloc_space(space
, extra
, n_eq
, n_ineq
);
1357 return bset_from_bmap(bmap
);
1360 __isl_give isl_basic_set
*isl_basic_set_alloc_space(__isl_take isl_space
*space
,
1361 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
1363 struct isl_basic_map
*bmap
;
1366 isl_assert(space
->ctx
, space
->n_in
== 0, goto error
);
1367 bmap
= isl_basic_map_alloc_space(space
, extra
, n_eq
, n_ineq
);
1368 return bset_from_bmap(bmap
);
1370 isl_space_free(space
);
1374 __isl_give isl_basic_map
*isl_basic_map_alloc_space(__isl_take isl_space
*space
,
1375 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
1377 struct isl_basic_map
*bmap
;
1381 bmap
= isl_calloc_type(space
->ctx
, struct isl_basic_map
);
1386 return basic_map_init(space
->ctx
, bmap
, extra
, n_eq
, n_ineq
);
1388 isl_space_free(space
);
1392 __isl_give isl_basic_map
*isl_basic_map_alloc(isl_ctx
*ctx
,
1393 unsigned nparam
, unsigned in
, unsigned out
, unsigned extra
,
1394 unsigned n_eq
, unsigned n_ineq
)
1396 struct isl_basic_map
*bmap
;
1399 space
= isl_space_alloc(ctx
, nparam
, in
, out
);
1403 bmap
= isl_basic_map_alloc_space(space
, extra
, n_eq
, n_ineq
);
1407 static __isl_give isl_basic_map
*dup_constraints(__isl_take isl_basic_map
*dst
,
1408 __isl_keep isl_basic_map
*src
)
1411 isl_size total
= isl_basic_map_dim(src
, isl_dim_all
);
1413 if (!dst
|| total
< 0)
1414 return isl_basic_map_free(dst
);
1416 for (i
= 0; i
< src
->n_eq
; ++i
) {
1417 int j
= isl_basic_map_alloc_equality(dst
);
1419 return isl_basic_map_free(dst
);
1420 isl_seq_cpy(dst
->eq
[j
], src
->eq
[i
], 1+total
);
1423 for (i
= 0; i
< src
->n_ineq
; ++i
) {
1424 int j
= isl_basic_map_alloc_inequality(dst
);
1426 return isl_basic_map_free(dst
);
1427 isl_seq_cpy(dst
->ineq
[j
], src
->ineq
[i
], 1+total
);
1430 for (i
= 0; i
< src
->n_div
; ++i
) {
1431 int j
= isl_basic_map_alloc_div(dst
);
1433 return isl_basic_map_free(dst
);
1434 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
);
1436 ISL_F_SET(dst
, ISL_BASIC_SET_FINAL
);
1440 __isl_give isl_basic_map
*isl_basic_map_dup(__isl_keep isl_basic_map
*bmap
)
1442 struct isl_basic_map
*dup
;
1446 dup
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
1447 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
1448 dup
= dup_constraints(dup
, bmap
);
1451 dup
->flags
= bmap
->flags
;
1452 dup
->sample
= isl_vec_copy(bmap
->sample
);
1456 __isl_give isl_basic_set
*isl_basic_set_dup(__isl_keep isl_basic_set
*bset
)
1458 struct isl_basic_map
*dup
;
1460 dup
= isl_basic_map_dup(bset_to_bmap(bset
));
1461 return bset_from_bmap(dup
);
1464 __isl_give isl_basic_set
*isl_basic_set_copy(__isl_keep isl_basic_set
*bset
)
1466 return bset_from_bmap(isl_basic_map_copy(bset_to_bmap(bset
)));
1469 __isl_give isl_set
*isl_set_copy(__isl_keep isl_set
*set
)
1478 __isl_give isl_basic_map
*isl_basic_map_copy(__isl_keep isl_basic_map
*bmap
)
1483 if (ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
)) {
1487 bmap
= isl_basic_map_dup(bmap
);
1489 ISL_F_SET(bmap
, ISL_BASIC_SET_FINAL
);
1493 __isl_give isl_map
*isl_map_copy(__isl_keep isl_map
*map
)
1502 __isl_null isl_basic_map
*isl_basic_map_free(__isl_take isl_basic_map
*bmap
)
1507 if (--bmap
->ref
> 0)
1510 isl_ctx_deref(bmap
->ctx
);
1512 isl_blk_free(bmap
->ctx
, bmap
->block2
);
1514 isl_blk_free(bmap
->ctx
, bmap
->block
);
1515 isl_vec_free(bmap
->sample
);
1516 isl_space_free(bmap
->dim
);
1522 __isl_null isl_basic_set
*isl_basic_set_free(__isl_take isl_basic_set
*bset
)
1524 return isl_basic_map_free(bset_to_bmap(bset
));
1527 static int room_for_con(__isl_keep isl_basic_map
*bmap
, unsigned n
)
1529 return bmap
->n_eq
+ bmap
->n_ineq
+ n
<= bmap
->c_size
;
1532 /* Check that "bset" does not involve any parameters.
1534 isl_stat
isl_basic_set_check_no_params(__isl_keep isl_basic_set
*bset
)
1538 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
1540 return isl_stat_error
;
1542 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
1543 "basic set should not have any parameters",
1544 return isl_stat_error
);
1548 /* Check that "bset" does not involve any local variables.
1550 isl_stat
isl_basic_set_check_no_locals(__isl_keep isl_basic_set
*bset
)
1554 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
1556 return isl_stat_error
;
1558 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
1559 "basic set should not have any local variables",
1560 return isl_stat_error
);
1565 #define TYPE isl_map
1567 #include "isl_check_named_params_templ.c"
1570 #define TYPE isl_basic_map
1573 #include "isl_check_named_params_templ.c"
1575 /* Check that "bmap1" and "bmap2" have the same parameters,
1576 * reporting an error if they do not.
1578 static isl_stat
isl_basic_map_check_equal_params(
1579 __isl_keep isl_basic_map
*bmap1
, __isl_keep isl_basic_map
*bmap2
)
1583 match
= isl_basic_map_has_equal_params(bmap1
, bmap2
);
1585 return isl_stat_error
;
1587 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
1588 "parameters don't match", return isl_stat_error
);
1593 #define TYPE isl_map
1595 #include "isl_align_params_bin_templ.c"
1600 #define ARG1 isl_map
1602 #define ARG2 isl_set
1604 #include "isl_align_params_templ.c"
1606 isl_bool
isl_map_align_params_map_map_and_test(__isl_keep isl_map
*map1
,
1607 __isl_keep isl_map
*map2
,
1608 isl_bool (*fn
)(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
))
1613 return isl_bool_error
;
1614 if (isl_map_has_equal_params(map1
, map2
))
1615 return fn(map1
, map2
);
1616 if (isl_map_check_named_params(map1
) < 0)
1617 return isl_bool_error
;
1618 if (isl_map_check_named_params(map2
) < 0)
1619 return isl_bool_error
;
1620 map1
= isl_map_copy(map1
);
1621 map2
= isl_map_copy(map2
);
1622 map1
= isl_map_align_params(map1
, isl_map_get_space(map2
));
1623 map2
= isl_map_align_params(map2
, isl_map_get_space(map1
));
1630 int isl_basic_map_alloc_equality(__isl_keep isl_basic_map
*bmap
)
1633 struct isl_ctx
*ctx
;
1635 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
1639 isl_assert(ctx
, room_for_con(bmap
, 1), return -1);
1640 isl_assert(ctx
, (bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
<= bmap
->c_size
,
1642 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1643 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1644 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1645 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1646 if ((bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
== bmap
->c_size
) {
1648 int j
= isl_basic_map_alloc_inequality(bmap
);
1652 bmap
->ineq
[j
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1653 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
1660 isl_seq_clr(bmap
->eq
[bmap
->n_eq
] + 1 + total
,
1661 bmap
->extra
- bmap
->n_div
);
1662 return bmap
->n_eq
++;
1665 int isl_basic_set_alloc_equality(__isl_keep isl_basic_set
*bset
)
1667 return isl_basic_map_alloc_equality(bset_to_bmap(bset
));
1670 __isl_give isl_basic_map
*isl_basic_map_free_equality(
1671 __isl_take isl_basic_map
*bmap
, unsigned n
)
1676 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
1677 "invalid number of equalities",
1678 isl_basic_map_free(bmap
));
1683 __isl_give isl_basic_set
*isl_basic_set_free_equality(
1684 __isl_take isl_basic_set
*bset
, unsigned n
)
1686 return bset_from_bmap(isl_basic_map_free_equality(bset_to_bmap(bset
),
1690 /* Drop the equality constraint at position "pos",
1691 * preserving the order of the other equality constraints.
1693 int isl_basic_map_drop_equality(__isl_keep isl_basic_map
*bmap
, unsigned pos
)
1700 isl_assert(bmap
->ctx
, pos
< bmap
->n_eq
, return -1);
1704 for (r
= pos
; r
< bmap
->n_eq
; ++r
)
1705 bmap
->eq
[r
] = bmap
->eq
[r
+ 1];
1706 bmap
->eq
[bmap
->n_eq
] = t
;
1711 /* Turn inequality "pos" of "bmap" into an equality.
1713 * In particular, we move the inequality in front of the equalities
1714 * and move the last inequality in the position of the moved inequality.
1715 * Note that isl_tab_make_equalities_explicit depends on this particular
1716 * change in the ordering of the constraints.
1718 void isl_basic_map_inequality_to_equality(
1719 __isl_keep isl_basic_map
*bmap
, unsigned pos
)
1723 t
= bmap
->ineq
[pos
];
1724 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1725 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
1730 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1731 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
1732 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1733 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1736 static int room_for_ineq(__isl_keep isl_basic_map
*bmap
, unsigned n
)
1738 return bmap
->n_ineq
+ n
<= bmap
->eq
- bmap
->ineq
;
1741 int isl_basic_map_alloc_inequality(__isl_keep isl_basic_map
*bmap
)
1744 struct isl_ctx
*ctx
;
1746 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
1750 isl_assert(ctx
, room_for_ineq(bmap
, 1), return -1);
1751 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1752 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1753 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
1754 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1755 ISL_F_CLR(bmap
, ISL_BASIC_MAP_REDUCED_COEFFICIENTS
);
1756 isl_seq_clr(bmap
->ineq
[bmap
->n_ineq
] + 1 + total
,
1757 bmap
->extra
- bmap
->n_div
);
1758 return bmap
->n_ineq
++;
1761 int isl_basic_set_alloc_inequality(__isl_keep isl_basic_set
*bset
)
1763 return isl_basic_map_alloc_inequality(bset_to_bmap(bset
));
1766 __isl_give isl_basic_map
*isl_basic_map_free_inequality(
1767 __isl_take isl_basic_map
*bmap
, unsigned n
)
1771 if (n
> bmap
->n_ineq
)
1772 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
1773 "invalid number of inequalities",
1774 return isl_basic_map_free(bmap
));
1779 __isl_give isl_basic_set
*isl_basic_set_free_inequality(
1780 __isl_take isl_basic_set
*bset
, unsigned n
)
1782 return bset_from_bmap(isl_basic_map_free_inequality(bset_to_bmap(bset
),
1786 int isl_basic_map_drop_inequality(__isl_keep isl_basic_map
*bmap
, unsigned pos
)
1791 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
1793 if (pos
!= bmap
->n_ineq
- 1) {
1794 t
= bmap
->ineq
[pos
];
1795 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1796 bmap
->ineq
[bmap
->n_ineq
- 1] = t
;
1797 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
1803 int isl_basic_set_drop_inequality(__isl_keep isl_basic_set
*bset
, unsigned pos
)
1805 return isl_basic_map_drop_inequality(bset_to_bmap(bset
), pos
);
1808 __isl_give isl_basic_map
*isl_basic_map_add_eq(__isl_take isl_basic_map
*bmap
,
1815 empty
= isl_basic_map_plain_is_empty(bmap
);
1817 return isl_basic_map_free(bmap
);
1821 bmap
= isl_basic_map_cow(bmap
);
1822 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
1823 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
1825 return isl_basic_map_free(bmap
);
1826 k
= isl_basic_map_alloc_equality(bmap
);
1829 isl_seq_cpy(bmap
->eq
[k
], eq
, 1 + total
);
1832 isl_basic_map_free(bmap
);
1836 __isl_give isl_basic_set
*isl_basic_set_add_eq(__isl_take isl_basic_set
*bset
,
1839 return bset_from_bmap(isl_basic_map_add_eq(bset_to_bmap(bset
), eq
));
1842 __isl_give isl_basic_map
*isl_basic_map_add_ineq(__isl_take isl_basic_map
*bmap
,
1848 bmap
= isl_basic_map_cow(bmap
);
1849 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
1850 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
1852 return isl_basic_map_free(bmap
);
1853 k
= isl_basic_map_alloc_inequality(bmap
);
1856 isl_seq_cpy(bmap
->ineq
[k
], ineq
, 1 + total
);
1859 isl_basic_map_free(bmap
);
1863 __isl_give isl_basic_set
*isl_basic_set_add_ineq(__isl_take isl_basic_set
*bset
,
1866 return bset_from_bmap(isl_basic_map_add_ineq(bset_to_bmap(bset
), ineq
));
1869 int isl_basic_map_alloc_div(__isl_keep isl_basic_map
*bmap
)
1873 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
1876 isl_assert(bmap
->ctx
, bmap
->n_div
< bmap
->extra
, return -1);
1877 isl_seq_clr(bmap
->div
[bmap
->n_div
] + 1 + 1 + total
,
1878 bmap
->extra
- bmap
->n_div
);
1879 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1880 return bmap
->n_div
++;
1883 int isl_basic_set_alloc_div(__isl_keep isl_basic_set
*bset
)
1885 return isl_basic_map_alloc_div(bset_to_bmap(bset
));
1889 #define TYPE isl_basic_map
1890 #include "check_type_range_templ.c"
1892 /* Check that there are "n" dimensions of type "type" starting at "first"
1895 isl_stat
isl_basic_set_check_range(__isl_keep isl_basic_set
*bset
,
1896 enum isl_dim_type type
, unsigned first
, unsigned n
)
1898 return isl_basic_map_check_range(bset_to_bmap(bset
),
1902 /* Insert an extra integer division, prescribed by "div", to "bmap"
1903 * at (integer division) position "pos".
1905 * The integer division is first added at the end and then moved
1906 * into the right position.
1908 __isl_give isl_basic_map
*isl_basic_map_insert_div(
1909 __isl_take isl_basic_map
*bmap
, int pos
, __isl_keep isl_vec
*div
)
1914 bmap
= isl_basic_map_cow(bmap
);
1915 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
1916 if (total
< 0 || !div
)
1917 return isl_basic_map_free(bmap
);
1919 if (div
->size
!= 1 + 1 + total
)
1920 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
1921 "unexpected size", return isl_basic_map_free(bmap
));
1922 if (isl_basic_map_check_range(bmap
, isl_dim_div
, pos
, 0) < 0)
1923 return isl_basic_map_free(bmap
);
1925 bmap
= isl_basic_map_extend(bmap
, 1, 0, 2);
1926 k
= isl_basic_map_alloc_div(bmap
);
1928 return isl_basic_map_free(bmap
);
1929 isl_seq_cpy(bmap
->div
[k
], div
->el
, div
->size
);
1930 isl_int_set_si(bmap
->div
[k
][div
->size
], 0);
1932 for (i
= k
; i
> pos
; --i
)
1933 bmap
= isl_basic_map_swap_div(bmap
, i
, i
- 1);
1938 isl_stat
isl_basic_map_free_div(__isl_keep isl_basic_map
*bmap
, unsigned n
)
1941 return isl_stat_error
;
1942 isl_assert(bmap
->ctx
, n
<= bmap
->n_div
, return isl_stat_error
);
1947 static __isl_give isl_basic_map
*add_constraints(
1948 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
,
1949 unsigned i_pos
, unsigned o_pos
)
1951 isl_size total
, n_param
, n_in
, n_out
, n_div
;
1952 unsigned o_in
, o_out
;
1955 struct isl_dim_map
*dim_map
;
1957 space
= isl_basic_map_peek_space(bmap2
);
1958 if (!bmap1
|| !space
)
1961 total
= isl_basic_map_dim(bmap1
, isl_dim_all
);
1962 n_param
= isl_basic_map_dim(bmap2
, isl_dim_param
);
1963 n_in
= isl_basic_map_dim(bmap2
, isl_dim_in
);
1964 o_in
= isl_basic_map_offset(bmap1
, isl_dim_in
) - 1 + i_pos
;
1965 n_out
= isl_basic_map_dim(bmap2
, isl_dim_out
);
1966 o_out
= isl_basic_map_offset(bmap1
, isl_dim_out
) - 1 + o_pos
;
1967 n_div
= isl_basic_map_dim(bmap2
, isl_dim_div
);
1968 if (total
< 0 || n_param
< 0 || n_in
< 0 || n_out
< 0 || n_div
< 0)
1970 ctx
= isl_basic_map_get_ctx(bmap1
);
1971 dim_map
= isl_dim_map_alloc(ctx
, total
+ n_div
);
1972 isl_dim_map_dim_range(dim_map
, space
, isl_dim_param
, 0, n_param
, 0);
1973 isl_dim_map_dim_range(dim_map
, space
, isl_dim_in
, 0, n_in
, o_in
);
1974 isl_dim_map_dim_range(dim_map
, space
, isl_dim_out
, 0, n_out
, o_out
);
1975 isl_dim_map_div(dim_map
, bmap2
, total
);
1977 return isl_basic_map_add_constraints_dim_map(bmap1
, bmap2
, dim_map
);
1979 isl_basic_map_free(bmap1
);
1980 isl_basic_map_free(bmap2
);
1984 __isl_give isl_basic_map
*isl_basic_map_extend(__isl_take isl_basic_map
*base
,
1985 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
1988 struct isl_basic_map
*ext
;
1995 dims_ok
= base
->extra
>= base
->n_div
+ extra
;
1997 if (dims_ok
&& room_for_con(base
, n_eq
+ n_ineq
) &&
1998 room_for_ineq(base
, n_ineq
))
2001 extra
+= base
->extra
;
2003 n_ineq
+= base
->n_ineq
;
2005 space
= isl_basic_map_get_space(base
);
2006 ext
= isl_basic_map_alloc_space(space
, extra
, n_eq
, n_ineq
);
2011 ext
->sample
= isl_vec_copy(base
->sample
);
2012 flags
= base
->flags
;
2013 ext
= add_constraints(ext
, base
, 0, 0);
2016 ISL_F_CLR(ext
, ISL_BASIC_SET_FINAL
);
2022 isl_basic_map_free(base
);
2026 __isl_give isl_basic_set
*isl_basic_set_extend(__isl_take isl_basic_set
*base
,
2027 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
2029 return bset_from_bmap(isl_basic_map_extend(bset_to_bmap(base
),
2030 extra
, n_eq
, n_ineq
));
2033 __isl_give isl_basic_map
*isl_basic_map_extend_constraints(
2034 __isl_take isl_basic_map
*base
, unsigned n_eq
, unsigned n_ineq
)
2036 return isl_basic_map_extend(base
, 0, n_eq
, n_ineq
);
2039 __isl_give isl_basic_set
*isl_basic_set_extend_constraints(
2040 __isl_take isl_basic_set
*base
, unsigned n_eq
, unsigned n_ineq
)
2042 isl_basic_map
*bmap
= bset_to_bmap(base
);
2043 bmap
= isl_basic_map_extend_constraints(bmap
, n_eq
, n_ineq
);
2044 return bset_from_bmap(bmap
);
2047 __isl_give isl_basic_set
*isl_basic_set_cow(__isl_take isl_basic_set
*bset
)
2049 return bset_from_bmap(isl_basic_map_cow(bset_to_bmap(bset
)));
2052 __isl_give isl_basic_map
*isl_basic_map_cow(__isl_take isl_basic_map
*bmap
)
2057 if (bmap
->ref
> 1) {
2059 bmap
= isl_basic_map_dup(bmap
);
2062 ISL_F_CLR(bmap
, ISL_BASIC_SET_FINAL
);
2066 /* Clear all cached information in "map", either because it is about
2067 * to be modified or because it is being freed.
2068 * Always return the same pointer that is passed in.
2069 * This is needed for the use in isl_map_free.
2071 static __isl_give isl_map
*clear_caches(__isl_take isl_map
*map
)
2073 isl_basic_map_free(map
->cached_simple_hull
[0]);
2074 isl_basic_map_free(map
->cached_simple_hull
[1]);
2075 map
->cached_simple_hull
[0] = NULL
;
2076 map
->cached_simple_hull
[1] = NULL
;
2080 __isl_give isl_set
*isl_set_cow(__isl_take isl_set
*set
)
2082 return isl_map_cow(set
);
2085 /* Return an isl_map that is equal to "map" and that has only
2086 * a single reference.
2088 * If the original input already has only one reference, then
2089 * simply return it, but clear all cached information, since
2090 * it may be rendered invalid by the operations that will be
2091 * performed on the result.
2093 * Otherwise, create a duplicate (without any cached information).
2095 __isl_give isl_map
*isl_map_cow(__isl_take isl_map
*map
)
2101 return clear_caches(map
);
2103 return isl_map_dup(map
);
2106 static void swap_vars(struct isl_blk blk
, isl_int
*a
,
2107 unsigned a_len
, unsigned b_len
)
2109 isl_seq_cpy(blk
.data
, a
+a_len
, b_len
);
2110 isl_seq_cpy(blk
.data
+b_len
, a
, a_len
);
2111 isl_seq_cpy(a
, blk
.data
, b_len
+a_len
);
2114 static __isl_give isl_basic_map
*isl_basic_map_swap_vars(
2115 __isl_take isl_basic_map
*bmap
, unsigned pos
, unsigned n1
, unsigned n2
)
2120 if (isl_basic_map_check_range(bmap
, isl_dim_all
, pos
- 1, n1
+ n2
) < 0)
2123 if (n1
== 0 || n2
== 0)
2126 bmap
= isl_basic_map_cow(bmap
);
2130 blk
= isl_blk_alloc(bmap
->ctx
, n1
+ n2
);
2131 if (isl_blk_is_error(blk
))
2134 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2136 bmap
->eq
[i
] + pos
, n1
, n2
);
2138 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2140 bmap
->ineq
[i
] + pos
, n1
, n2
);
2142 for (i
= 0; i
< bmap
->n_div
; ++i
)
2144 bmap
->div
[i
]+1 + pos
, n1
, n2
);
2146 isl_blk_free(bmap
->ctx
, blk
);
2148 ISL_F_CLR(bmap
, ISL_BASIC_SET_SORTED
);
2149 bmap
= isl_basic_map_gauss(bmap
, NULL
);
2150 return isl_basic_map_finalize(bmap
);
2152 isl_basic_map_free(bmap
);
2156 /* The given basic map has turned out to be empty.
2157 * Explicitly mark it as such and change the representation
2158 * to a canonical representation of the empty basic map.
2159 * Since the basic map has conflicting constraints,
2160 * it must have at least one constraint, except perhaps
2161 * if it was already explicitly marked as being empty.
2162 * Do nothing in the latter case, i.e., if it has been marked empty and
2163 * has no constraints.
2165 __isl_give isl_basic_map
*isl_basic_map_set_to_empty(
2166 __isl_take isl_basic_map
*bmap
)
2173 n
= isl_basic_map_n_constraint(bmap
);
2174 empty
= isl_basic_map_plain_is_empty(bmap
);
2175 if (n
< 0 || empty
< 0)
2176 return isl_basic_map_free(bmap
);
2177 if (n
== 0 && empty
)
2179 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
2181 return isl_basic_map_free(bmap
);
2182 if (isl_basic_map_free_div(bmap
, bmap
->n_div
) < 0)
2183 return isl_basic_map_free(bmap
);
2184 bmap
= isl_basic_map_free_inequality(bmap
, bmap
->n_ineq
);
2187 if (bmap
->n_eq
> 0) {
2188 bmap
= isl_basic_map_free_equality(bmap
, bmap
->n_eq
- 1);
2192 i
= isl_basic_map_alloc_equality(bmap
);
2196 isl_int_set_si(bmap
->eq
[i
][0], 1);
2197 isl_seq_clr(bmap
->eq
[i
]+1, total
);
2198 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
2199 isl_vec_free(bmap
->sample
);
2200 bmap
->sample
= NULL
;
2201 return isl_basic_map_finalize(bmap
);
2203 isl_basic_map_free(bmap
);
2207 __isl_give isl_basic_set
*isl_basic_set_set_to_empty(
2208 __isl_take isl_basic_set
*bset
)
2210 return bset_from_bmap(isl_basic_map_set_to_empty(bset_to_bmap(bset
)));
2213 __isl_give isl_basic_map
*isl_basic_map_set_rational(
2214 __isl_take isl_basic_map
*bmap
)
2219 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
2222 bmap
= isl_basic_map_cow(bmap
);
2226 ISL_F_SET(bmap
, ISL_BASIC_MAP_RATIONAL
);
2228 return isl_basic_map_finalize(bmap
);
2231 __isl_give isl_basic_set
*isl_basic_set_set_rational(
2232 __isl_take isl_basic_set
*bset
)
2234 return isl_basic_map_set_rational(bset
);
2237 __isl_give isl_basic_set
*isl_basic_set_set_integral(
2238 __isl_take isl_basic_set
*bset
)
2243 if (!ISL_F_ISSET(bset
, ISL_BASIC_MAP_RATIONAL
))
2246 bset
= isl_basic_set_cow(bset
);
2250 ISL_F_CLR(bset
, ISL_BASIC_MAP_RATIONAL
);
2252 return isl_basic_set_finalize(bset
);
2255 __isl_give isl_map
*isl_map_set_rational(__isl_take isl_map
*map
)
2259 map
= isl_map_cow(map
);
2262 for (i
= 0; i
< map
->n
; ++i
) {
2263 map
->p
[i
] = isl_basic_map_set_rational(map
->p
[i
]);
2273 __isl_give isl_set
*isl_set_set_rational(__isl_take isl_set
*set
)
2275 return isl_map_set_rational(set
);
2278 /* Given a constraint "c" that expresses a bound
2279 * on the variable at position "pos" in terms of the first "len" variables
2280 * (other than the variable itself if pos < len), extract this bound
2281 * as a function of those first "len" variables.
2283 * That is, the constraint is of one of the following forms
2285 * -e(...) + m x >= 0
2290 * Return (e(...)) / m, with the denominator m in the first position.
2292 static __isl_give isl_vec
*extract_bound_from_constraint(isl_ctx
*ctx
,
2293 isl_int
*c
, int len
, int pos
)
2297 v
= isl_vec_alloc(ctx
, 1 + 1 + len
);
2300 if (isl_int_is_pos(c
[1 + pos
])) {
2301 isl_int_set(v
->el
[0], c
[1 + pos
]);
2302 isl_seq_neg(v
->el
+ 1, c
, 1 + len
);
2304 isl_int_neg(v
->el
[0], c
[1 + pos
]);
2305 isl_seq_cpy(v
->el
+ 1, c
, 1 + len
);
2308 isl_int_set_si(v
->el
[1 + 1 + pos
], 0);
2313 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
2316 static void swap_div(__isl_keep isl_basic_map
*bmap
, int a
, int b
)
2318 isl_int
*t
= bmap
->div
[a
];
2319 bmap
->div
[a
] = bmap
->div
[b
];
2323 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
2324 * div definitions accordingly.
2326 __isl_give isl_basic_map
*isl_basic_map_swap_div(__isl_take isl_basic_map
*bmap
,
2332 off
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
2334 return isl_basic_map_free(bmap
);
2336 swap_div(bmap
, a
, b
);
2338 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2339 isl_int_swap(bmap
->eq
[i
][1+off
+a
], bmap
->eq
[i
][1+off
+b
]);
2341 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2342 isl_int_swap(bmap
->ineq
[i
][1+off
+a
], bmap
->ineq
[i
][1+off
+b
]);
2344 for (i
= 0; i
< bmap
->n_div
; ++i
)
2345 isl_int_swap(bmap
->div
[i
][1+1+off
+a
], bmap
->div
[i
][1+1+off
+b
]);
2346 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
2351 static void constraint_drop_vars(isl_int
*c
, unsigned n
, unsigned rem
)
2353 isl_seq_cpy(c
, c
+ n
, rem
);
2354 isl_seq_clr(c
+ rem
, n
);
2357 /* Drop n dimensions starting at first.
2359 * In principle, this frees up some extra variables as the number
2360 * of columns remains constant, but we would have to extend
2361 * the div array too as the number of rows in this array is assumed
2362 * to be equal to extra.
2364 __isl_give isl_basic_set
*isl_basic_set_drop_dims(
2365 __isl_take isl_basic_set
*bset
, unsigned first
, unsigned n
)
2367 return isl_basic_map_drop(bset_to_bmap(bset
), isl_dim_set
, first
, n
);
2370 /* Move "n" divs starting at "first" to the end of the list of divs.
2372 static __isl_give isl_basic_map
*move_divs_last(__isl_take isl_basic_map
*bmap
,
2373 unsigned first
, unsigned n
)
2378 if (first
+ n
== bmap
->n_div
)
2381 div
= isl_alloc_array(bmap
->ctx
, isl_int
*, n
);
2384 for (i
= 0; i
< n
; ++i
)
2385 div
[i
] = bmap
->div
[first
+ i
];
2386 for (i
= 0; i
< bmap
->n_div
- first
- n
; ++i
)
2387 bmap
->div
[first
+ i
] = bmap
->div
[first
+ n
+ i
];
2388 for (i
= 0; i
< n
; ++i
)
2389 bmap
->div
[bmap
->n_div
- n
+ i
] = div
[i
];
2393 isl_basic_map_free(bmap
);
2398 #define TYPE isl_map
2400 #include "check_type_range_templ.c"
2402 /* Check that there are "n" dimensions of type "type" starting at "first"
2405 isl_stat
isl_set_check_range(__isl_keep isl_set
*set
,
2406 enum isl_dim_type type
, unsigned first
, unsigned n
)
2408 return isl_map_check_range(set_to_map(set
), type
, first
, n
);
2411 /* Drop "n" dimensions of type "type" starting at "first".
2412 * Perform the core computation, without cowing or
2413 * simplifying and finalizing the result.
2415 * In principle, this frees up some extra variables as the number
2416 * of columns remains constant, but we would have to extend
2417 * the div array too as the number of rows in this array is assumed
2418 * to be equal to extra.
2420 __isl_give isl_basic_map
*isl_basic_map_drop_core(
2421 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
2422 unsigned first
, unsigned n
)
2429 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2430 return isl_basic_map_free(bmap
);
2432 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
2434 return isl_basic_map_free(bmap
);
2436 offset
= isl_basic_map_offset(bmap
, type
) + first
;
2437 left
= total
- (offset
- 1) - n
;
2438 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2439 constraint_drop_vars(bmap
->eq
[i
]+offset
, n
, left
);
2441 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2442 constraint_drop_vars(bmap
->ineq
[i
]+offset
, n
, left
);
2444 for (i
= 0; i
< bmap
->n_div
; ++i
)
2445 constraint_drop_vars(bmap
->div
[i
]+1+offset
, n
, left
);
2447 if (type
== isl_dim_div
) {
2448 bmap
= move_divs_last(bmap
, first
, n
);
2451 if (isl_basic_map_free_div(bmap
, n
) < 0)
2452 return isl_basic_map_free(bmap
);
2454 bmap
->dim
= isl_space_drop_dims(bmap
->dim
, type
, first
, n
);
2456 return isl_basic_map_free(bmap
);
2458 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
2459 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
2460 ISL_F_CLR(bmap
, ISL_BASIC_MAP_REDUCED_COEFFICIENTS
);
2464 /* Drop "n" dimensions of type "type" starting at "first".
2466 * In principle, this frees up some extra variables as the number
2467 * of columns remains constant, but we would have to extend
2468 * the div array too as the number of rows in this array is assumed
2469 * to be equal to extra.
2471 __isl_give isl_basic_map
*isl_basic_map_drop(__isl_take isl_basic_map
*bmap
,
2472 enum isl_dim_type type
, unsigned first
, unsigned n
)
2476 if (n
== 0 && !isl_space_is_named_or_nested(bmap
->dim
, type
))
2479 bmap
= isl_basic_map_cow(bmap
);
2483 bmap
= isl_basic_map_drop_core(bmap
, type
, first
, n
);
2485 bmap
= isl_basic_map_simplify(bmap
);
2486 return isl_basic_map_finalize(bmap
);
2489 __isl_give isl_basic_set
*isl_basic_set_drop(__isl_take isl_basic_set
*bset
,
2490 enum isl_dim_type type
, unsigned first
, unsigned n
)
2492 return bset_from_bmap(isl_basic_map_drop(bset_to_bmap(bset
),
2496 /* No longer consider "map" to be normalized.
2498 static __isl_give isl_map
*isl_map_unmark_normalized(__isl_take isl_map
*map
)
2502 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
2506 __isl_give isl_map
*isl_map_drop(__isl_take isl_map
*map
,
2507 enum isl_dim_type type
, unsigned first
, unsigned n
)
2512 if (isl_map_check_range(map
, type
, first
, n
) < 0)
2513 return isl_map_free(map
);
2515 if (n
== 0 && !isl_space_is_named_or_nested(map
->dim
, type
))
2517 map
= isl_map_cow(map
);
2521 for (i
= 0; i
< map
->n
; ++i
) {
2522 map
->p
[i
] = isl_basic_map_drop(map
->p
[i
], type
, first
, n
);
2526 map
= isl_map_unmark_normalized(map
);
2528 space
= isl_map_take_space(map
);
2529 space
= isl_space_drop_dims(space
, type
, first
, n
);
2530 map
= isl_map_restore_space(map
, space
);
2538 __isl_give isl_set
*isl_set_drop(__isl_take isl_set
*set
,
2539 enum isl_dim_type type
, unsigned first
, unsigned n
)
2541 return set_from_map(isl_map_drop(set_to_map(set
), type
, first
, n
));
2544 /* Drop the integer division at position "div", which is assumed
2545 * not to appear in any of the constraints or
2546 * in any of the other integer divisions.
2548 * Since the integer division is redundant, there is no need to cow.
2550 __isl_give isl_basic_map
*isl_basic_map_drop_div(
2551 __isl_take isl_basic_map
*bmap
, unsigned div
)
2553 return isl_basic_map_drop_core(bmap
, isl_dim_div
, div
, 1);
2556 /* Eliminate the specified n dimensions starting at first from the
2557 * constraints, without removing the dimensions from the space.
2558 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2560 __isl_give isl_map
*isl_map_eliminate(__isl_take isl_map
*map
,
2561 enum isl_dim_type type
, unsigned first
, unsigned n
)
2568 if (isl_map_check_range(map
, type
, first
, n
) < 0)
2569 return isl_map_free(map
);
2571 map
= isl_map_cow(map
);
2575 for (i
= 0; i
< map
->n
; ++i
) {
2576 map
->p
[i
] = isl_basic_map_eliminate(map
->p
[i
], type
, first
, n
);
2586 /* Eliminate the specified n dimensions starting at first from the
2587 * constraints, without removing the dimensions from the space.
2588 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2590 __isl_give isl_set
*isl_set_eliminate(__isl_take isl_set
*set
,
2591 enum isl_dim_type type
, unsigned first
, unsigned n
)
2593 return set_from_map(isl_map_eliminate(set_to_map(set
), type
, first
, n
));
2596 /* Eliminate the specified n dimensions starting at first from the
2597 * constraints, without removing the dimensions from the space.
2598 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2600 __isl_give isl_set
*isl_set_eliminate_dims(__isl_take isl_set
*set
,
2601 unsigned first
, unsigned n
)
2603 return isl_set_eliminate(set
, isl_dim_set
, first
, n
);
2606 __isl_give isl_basic_map
*isl_basic_map_remove_divs(
2607 __isl_take isl_basic_map
*bmap
)
2611 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
2613 return isl_basic_map_free(bmap
);
2614 bmap
= isl_basic_map_eliminate_vars(bmap
, v_div
, bmap
->n_div
);
2618 return isl_basic_map_finalize(bmap
);
2621 __isl_give isl_basic_set
*isl_basic_set_remove_divs(
2622 __isl_take isl_basic_set
*bset
)
2624 return bset_from_bmap(isl_basic_map_remove_divs(bset_to_bmap(bset
)));
2627 __isl_give isl_map
*isl_map_remove_divs(__isl_take isl_map
*map
)
2636 map
= isl_map_cow(map
);
2640 for (i
= 0; i
< map
->n
; ++i
) {
2641 map
->p
[i
] = isl_basic_map_remove_divs(map
->p
[i
]);
2651 __isl_give isl_set
*isl_set_remove_divs(__isl_take isl_set
*set
)
2653 return isl_map_remove_divs(set
);
2656 __isl_give isl_basic_map
*isl_basic_map_remove_dims(
2657 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
2658 unsigned first
, unsigned n
)
2660 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2661 return isl_basic_map_free(bmap
);
2662 if (n
== 0 && !isl_space_is_named_or_nested(bmap
->dim
, type
))
2664 bmap
= isl_basic_map_eliminate_vars(bmap
,
2665 isl_basic_map_offset(bmap
, type
) - 1 + first
, n
);
2668 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
) && type
== isl_dim_div
)
2670 bmap
= isl_basic_map_drop(bmap
, type
, first
, n
);
2674 /* Return true if the definition of the given div (recursively) involves
2675 * any of the given variables.
2677 static isl_bool
div_involves_vars(__isl_keep isl_basic_map
*bmap
, int div
,
2678 unsigned first
, unsigned n
)
2681 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
2683 if (isl_int_is_zero(bmap
->div
[div
][0]))
2684 return isl_bool_false
;
2685 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + 1 + first
, n
) >= 0)
2686 return isl_bool_true
;
2688 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2691 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
2693 involves
= div_involves_vars(bmap
, i
, first
, n
);
2694 if (involves
< 0 || involves
)
2698 return isl_bool_false
;
2701 /* Try and add a lower and/or upper bound on "div" to "bmap"
2702 * based on inequality "i".
2703 * "total" is the total number of variables (excluding the divs).
2704 * "v" is a temporary object that can be used during the calculations.
2705 * If "lb" is set, then a lower bound should be constructed.
2706 * If "ub" is set, then an upper bound should be constructed.
2708 * The calling function has already checked that the inequality does not
2709 * reference "div", but we still need to check that the inequality is
2710 * of the right form. We'll consider the case where we want to construct
2711 * a lower bound. The construction of upper bounds is similar.
2713 * Let "div" be of the form
2715 * q = floor((a + f(x))/d)
2717 * We essentially check if constraint "i" is of the form
2721 * so that we can use it to derive a lower bound on "div".
2722 * However, we allow a slightly more general form
2726 * with the condition that the coefficients of g(x) - f(x) are all
2728 * Rewriting this constraint as
2732 * adding a + f(x) to both sides and dividing by d, we obtain
2734 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
2736 * Taking the floor on both sides, we obtain
2738 * q >= floor((a-b)/d) + (f(x)-g(x))/d
2742 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
2744 * In the case of an upper bound, we construct the constraint
2746 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
2749 static __isl_give isl_basic_map
*insert_bounds_on_div_from_ineq(
2750 __isl_take isl_basic_map
*bmap
, int div
, int i
,
2751 unsigned total
, isl_int v
, int lb
, int ub
)
2755 for (j
= 0; (lb
|| ub
) && j
< total
+ bmap
->n_div
; ++j
) {
2757 isl_int_sub(v
, bmap
->ineq
[i
][1 + j
],
2758 bmap
->div
[div
][1 + 1 + j
]);
2759 lb
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
2762 isl_int_add(v
, bmap
->ineq
[i
][1 + j
],
2763 bmap
->div
[div
][1 + 1 + j
]);
2764 ub
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
2770 bmap
= isl_basic_map_cow(bmap
);
2771 bmap
= isl_basic_map_extend_constraints(bmap
, 0, lb
+ ub
);
2773 int k
= isl_basic_map_alloc_inequality(bmap
);
2776 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
2777 isl_int_sub(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
2778 bmap
->div
[div
][1 + j
]);
2779 isl_int_cdiv_q(bmap
->ineq
[k
][j
],
2780 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
2782 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], 1);
2785 int k
= isl_basic_map_alloc_inequality(bmap
);
2788 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
2789 isl_int_add(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
2790 bmap
->div
[div
][1 + j
]);
2791 isl_int_fdiv_q(bmap
->ineq
[k
][j
],
2792 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
2794 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], -1);
2799 isl_basic_map_free(bmap
);
2803 /* This function is called right before "div" is eliminated from "bmap"
2804 * using Fourier-Motzkin.
2805 * Look through the constraints of "bmap" for constraints on the argument
2806 * of the integer division and use them to construct constraints on the
2807 * integer division itself. These constraints can then be combined
2808 * during the Fourier-Motzkin elimination.
2809 * Note that it is only useful to introduce lower bounds on "div"
2810 * if "bmap" already contains upper bounds on "div" as the newly
2811 * introduce lower bounds can then be combined with the pre-existing
2812 * upper bounds. Similarly for upper bounds.
2813 * We therefore first check if "bmap" contains any lower and/or upper bounds
2816 * It is interesting to note that the introduction of these constraints
2817 * can indeed lead to more accurate results, even when compared to
2818 * deriving constraints on the argument of "div" from constraints on "div".
2819 * Consider, for example, the set
2821 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2823 * The second constraint can be rewritten as
2825 * 2 * [(-i-2j+3)/4] + k >= 0
2827 * from which we can derive
2829 * -i - 2j + 3 >= -2k
2835 * Combined with the first constraint, we obtain
2837 * -3 <= 3 + 2k or k >= -3
2839 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2840 * the first constraint, we obtain
2842 * [(i + 2j)/4] >= [-3/4] = -1
2844 * Combining this constraint with the second constraint, we obtain
2848 static __isl_give isl_basic_map
*insert_bounds_on_div(
2849 __isl_take isl_basic_map
*bmap
, int div
)
2852 int check_lb
, check_ub
;
2859 if (isl_int_is_zero(bmap
->div
[div
][0]))
2862 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
2864 return isl_basic_map_free(bmap
);
2868 for (i
= 0; (!check_lb
|| !check_ub
) && i
< bmap
->n_ineq
; ++i
) {
2869 int s
= isl_int_sgn(bmap
->ineq
[i
][1 + v_div
+ div
]);
2876 if (!check_lb
&& !check_ub
)
2881 for (i
= 0; bmap
&& i
< bmap
->n_ineq
; ++i
) {
2882 if (!isl_int_is_zero(bmap
->ineq
[i
][1 + v_div
+ div
]))
2885 bmap
= insert_bounds_on_div_from_ineq(bmap
, div
, i
, v_div
, v
,
2886 check_lb
, check_ub
);
2894 /* Remove all divs (recursively) involving any of the given variables
2895 * in their definitions.
2897 static __isl_give isl_basic_map
*remove_divs_involving_vars(
2898 __isl_take isl_basic_map
*bmap
, unsigned first
, unsigned n
)
2902 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2905 involves
= div_involves_vars(bmap
, i
, first
, n
);
2907 return isl_basic_map_free(bmap
);
2910 bmap
= insert_bounds_on_div(bmap
, i
);
2911 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
2920 /* Remove all divs (recursively) involving any of the given dimensions
2921 * in their definitions.
2923 __isl_give isl_basic_map
*isl_basic_map_remove_divs_involving_dims(
2924 __isl_take isl_basic_map
*bmap
,
2925 enum isl_dim_type type
, unsigned first
, unsigned n
)
2929 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2930 return isl_basic_map_free(bmap
);
2931 off
= isl_basic_map_var_offset(bmap
, type
);
2933 return isl_basic_map_free(bmap
);
2936 return remove_divs_involving_vars(bmap
, first
, n
);
2939 __isl_give isl_basic_set
*isl_basic_set_remove_divs_involving_dims(
2940 __isl_take isl_basic_set
*bset
,
2941 enum isl_dim_type type
, unsigned first
, unsigned n
)
2943 return isl_basic_map_remove_divs_involving_dims(bset
, type
, first
, n
);
2946 __isl_give isl_map
*isl_map_remove_divs_involving_dims(__isl_take isl_map
*map
,
2947 enum isl_dim_type type
, unsigned first
, unsigned n
)
2956 map
= isl_map_cow(map
);
2960 for (i
= 0; i
< map
->n
; ++i
) {
2961 map
->p
[i
] = isl_basic_map_remove_divs_involving_dims(map
->p
[i
],
2972 __isl_give isl_set
*isl_set_remove_divs_involving_dims(__isl_take isl_set
*set
,
2973 enum isl_dim_type type
, unsigned first
, unsigned n
)
2975 return set_from_map(isl_map_remove_divs_involving_dims(set_to_map(set
),
2979 /* Does the description of "bmap" depend on the specified dimensions?
2980 * We also check whether the dimensions appear in any of the div definitions.
2981 * In principle there is no need for this check. If the dimensions appear
2982 * in a div definition, they also appear in the defining constraints of that
2985 isl_bool
isl_basic_map_involves_dims(__isl_keep isl_basic_map
*bmap
,
2986 enum isl_dim_type type
, unsigned first
, unsigned n
)
2990 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2991 return isl_bool_error
;
2993 first
+= isl_basic_map_offset(bmap
, type
);
2994 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2995 if (isl_seq_first_non_zero(bmap
->eq
[i
] + first
, n
) >= 0)
2996 return isl_bool_true
;
2997 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2998 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + first
, n
) >= 0)
2999 return isl_bool_true
;
3000 for (i
= 0; i
< bmap
->n_div
; ++i
) {
3001 if (isl_int_is_zero(bmap
->div
[i
][0]))
3003 if (isl_seq_first_non_zero(bmap
->div
[i
] + 1 + first
, n
) >= 0)
3004 return isl_bool_true
;
3007 return isl_bool_false
;
3010 isl_bool
isl_map_involves_dims(__isl_keep isl_map
*map
,
3011 enum isl_dim_type type
, unsigned first
, unsigned n
)
3015 if (isl_map_check_range(map
, type
, first
, n
) < 0)
3016 return isl_bool_error
;
3018 for (i
= 0; i
< map
->n
; ++i
) {
3019 isl_bool involves
= isl_basic_map_involves_dims(map
->p
[i
],
3021 if (involves
< 0 || involves
)
3025 return isl_bool_false
;
3028 isl_bool
isl_basic_set_involves_dims(__isl_keep isl_basic_set
*bset
,
3029 enum isl_dim_type type
, unsigned first
, unsigned n
)
3031 return isl_basic_map_involves_dims(bset
, type
, first
, n
);
3034 isl_bool
isl_set_involves_dims(__isl_keep isl_set
*set
,
3035 enum isl_dim_type type
, unsigned first
, unsigned n
)
3037 return isl_map_involves_dims(set
, type
, first
, n
);
3040 /* Does "bset" involve any local variables, i.e., integer divisions?
3042 static isl_bool
isl_basic_set_involves_locals(__isl_keep isl_basic_set
*bset
)
3046 n
= isl_basic_set_dim(bset
, isl_dim_div
);
3048 return isl_bool_error
;
3049 return isl_bool_ok(n
> 0);
3052 /* isl_set_every_basic_set callback that checks whether "bset"
3053 * is free of local variables.
3055 static isl_bool
basic_set_no_locals(__isl_keep isl_basic_set
*bset
, void *user
)
3057 return isl_bool_not(isl_basic_set_involves_locals(bset
));
3060 /* Does "set" involve any local variables, i.e., integer divisions?
3062 isl_bool
isl_set_involves_locals(__isl_keep isl_set
*set
)
3066 no_locals
= isl_set_every_basic_set(set
, &basic_set_no_locals
, NULL
);
3067 return isl_bool_not(no_locals
);
3070 /* Drop all constraints in bmap that involve any of the dimensions
3071 * first to first+n-1.
3072 * This function only performs the actual removal of constraints.
3074 * This function should not call finalize since it is used by
3075 * remove_redundant_divs, which in turn is called by isl_basic_map_finalize.
3077 __isl_give isl_basic_map
*isl_basic_map_drop_constraints_involving(
3078 __isl_take isl_basic_map
*bmap
, unsigned first
, unsigned n
)
3085 bmap
= isl_basic_map_cow(bmap
);
3090 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
) {
3091 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + first
, n
) == -1)
3093 if (isl_basic_map_drop_equality(bmap
, i
) < 0)
3094 return isl_basic_map_free(bmap
);
3097 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
3098 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + 1 + first
, n
) == -1)
3100 if (isl_basic_map_drop_inequality(bmap
, i
) < 0)
3101 return isl_basic_map_free(bmap
);
3107 /* Drop all constraints in bset that involve any of the dimensions
3108 * first to first+n-1.
3109 * This function only performs the actual removal of constraints.
3111 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_involving(
3112 __isl_take isl_basic_set
*bset
, unsigned first
, unsigned n
)
3114 return isl_basic_map_drop_constraints_involving(bset
, first
, n
);
3117 /* Drop all constraints in bmap that do not involve any of the dimensions
3118 * first to first + n - 1 of the given type.
3120 __isl_give isl_basic_map
*isl_basic_map_drop_constraints_not_involving_dims(
3121 __isl_take isl_basic_map
*bmap
,
3122 enum isl_dim_type type
, unsigned first
, unsigned n
)
3127 isl_space
*space
= isl_basic_map_get_space(bmap
);
3128 isl_basic_map_free(bmap
);
3129 return isl_basic_map_universe(space
);
3131 bmap
= isl_basic_map_cow(bmap
);
3135 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
3136 return isl_basic_map_free(bmap
);
3138 first
+= isl_basic_map_offset(bmap
, type
) - 1;
3140 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
) {
3141 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + first
, n
) != -1)
3143 if (isl_basic_map_drop_equality(bmap
, i
) < 0)
3144 return isl_basic_map_free(bmap
);
3147 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
3148 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + 1 + first
, n
) != -1)
3150 if (isl_basic_map_drop_inequality(bmap
, i
) < 0)
3151 return isl_basic_map_free(bmap
);
3154 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
3158 /* Drop all constraints in bset that do not involve any of the dimensions
3159 * first to first + n - 1 of the given type.
3161 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_not_involving_dims(
3162 __isl_take isl_basic_set
*bset
,
3163 enum isl_dim_type type
, unsigned first
, unsigned n
)
3165 return isl_basic_map_drop_constraints_not_involving_dims(bset
,
3169 /* Drop all constraints in bmap that involve any of the dimensions
3170 * first to first + n - 1 of the given type.
3172 __isl_give isl_basic_map
*isl_basic_map_drop_constraints_involving_dims(
3173 __isl_take isl_basic_map
*bmap
,
3174 enum isl_dim_type type
, unsigned first
, unsigned n
)
3181 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
3182 return isl_basic_map_free(bmap
);
3184 bmap
= isl_basic_map_remove_divs_involving_dims(bmap
, type
, first
, n
);
3185 first
+= isl_basic_map_offset(bmap
, type
) - 1;
3186 bmap
= isl_basic_map_drop_constraints_involving(bmap
, first
, n
);
3187 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
3191 /* Drop all constraints in bset that involve any of the dimensions
3192 * first to first + n - 1 of the given type.
3194 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_involving_dims(
3195 __isl_take isl_basic_set
*bset
,
3196 enum isl_dim_type type
, unsigned first
, unsigned n
)
3198 return isl_basic_map_drop_constraints_involving_dims(bset
,
3202 /* Drop constraints from "map" by applying "drop" to each basic map.
3204 static __isl_give isl_map
*drop_constraints(__isl_take isl_map
*map
,
3205 enum isl_dim_type type
, unsigned first
, unsigned n
,
3206 __isl_give isl_basic_map
*(*drop
)(__isl_take isl_basic_map
*bmap
,
3207 enum isl_dim_type type
, unsigned first
, unsigned n
))
3211 if (isl_map_check_range(map
, type
, first
, n
) < 0)
3212 return isl_map_free(map
);
3214 map
= isl_map_cow(map
);
3218 for (i
= 0; i
< map
->n
; ++i
) {
3219 map
->p
[i
] = drop(map
->p
[i
], type
, first
, n
);
3221 return isl_map_free(map
);
3225 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
3230 /* Drop all constraints in map that involve any of the dimensions
3231 * first to first + n - 1 of the given type.
3233 __isl_give isl_map
*isl_map_drop_constraints_involving_dims(
3234 __isl_take isl_map
*map
,
3235 enum isl_dim_type type
, unsigned first
, unsigned n
)
3239 return drop_constraints(map
, type
, first
, n
,
3240 &isl_basic_map_drop_constraints_involving_dims
);
3243 /* Drop all constraints in "map" that do not involve any of the dimensions
3244 * first to first + n - 1 of the given type.
3246 __isl_give isl_map
*isl_map_drop_constraints_not_involving_dims(
3247 __isl_take isl_map
*map
,
3248 enum isl_dim_type type
, unsigned first
, unsigned n
)
3251 isl_space
*space
= isl_map_get_space(map
);
3253 return isl_map_universe(space
);
3255 return drop_constraints(map
, type
, first
, n
,
3256 &isl_basic_map_drop_constraints_not_involving_dims
);
3259 /* Drop all constraints in set that involve any of the dimensions
3260 * first to first + n - 1 of the given type.
3262 __isl_give isl_set
*isl_set_drop_constraints_involving_dims(
3263 __isl_take isl_set
*set
,
3264 enum isl_dim_type type
, unsigned first
, unsigned n
)
3266 return isl_map_drop_constraints_involving_dims(set
, type
, first
, n
);
3269 /* Drop all constraints in "set" that do not involve any of the dimensions
3270 * first to first + n - 1 of the given type.
3272 __isl_give isl_set
*isl_set_drop_constraints_not_involving_dims(
3273 __isl_take isl_set
*set
,
3274 enum isl_dim_type type
, unsigned first
, unsigned n
)
3276 return isl_map_drop_constraints_not_involving_dims(set
, type
, first
, n
);
3279 /* Does local variable "div" of "bmap" have a complete explicit representation?
3280 * Having a complete explicit representation requires not only
3281 * an explicit representation, but also that all local variables
3282 * that appear in this explicit representation in turn have
3283 * a complete explicit representation.
3285 isl_bool
isl_basic_map_div_is_known(__isl_keep isl_basic_map
*bmap
, int div
)
3288 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
3291 marked
= isl_basic_map_div_is_marked_unknown(bmap
, div
);
3292 if (marked
< 0 || marked
)
3293 return isl_bool_not(marked
);
3295 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
3298 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
3300 known
= isl_basic_map_div_is_known(bmap
, i
);
3301 if (known
< 0 || !known
)
3305 return isl_bool_true
;
3308 /* Remove all divs that are unknown or defined in terms of unknown divs.
3310 __isl_give isl_basic_map
*isl_basic_map_remove_unknown_divs(
3311 __isl_take isl_basic_map
*bmap
)
3318 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
3319 if (isl_basic_map_div_is_known(bmap
, i
))
3321 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
3330 /* Remove all divs that are unknown or defined in terms of unknown divs.
3332 __isl_give isl_basic_set
*isl_basic_set_remove_unknown_divs(
3333 __isl_take isl_basic_set
*bset
)
3335 return isl_basic_map_remove_unknown_divs(bset
);
3338 __isl_give isl_map
*isl_map_remove_unknown_divs(__isl_take isl_map
*map
)
3347 map
= isl_map_cow(map
);
3351 for (i
= 0; i
< map
->n
; ++i
) {
3352 map
->p
[i
] = isl_basic_map_remove_unknown_divs(map
->p
[i
]);
3362 __isl_give isl_set
*isl_set_remove_unknown_divs(__isl_take isl_set
*set
)
3364 return set_from_map(isl_map_remove_unknown_divs(set_to_map(set
)));
3367 __isl_give isl_basic_set
*isl_basic_set_remove_dims(
3368 __isl_take isl_basic_set
*bset
,
3369 enum isl_dim_type type
, unsigned first
, unsigned n
)
3371 isl_basic_map
*bmap
= bset_to_bmap(bset
);
3372 bmap
= isl_basic_map_remove_dims(bmap
, type
, first
, n
);
3373 return bset_from_bmap(bmap
);
3376 __isl_give isl_map
*isl_map_remove_dims(__isl_take isl_map
*map
,
3377 enum isl_dim_type type
, unsigned first
, unsigned n
)
3384 map
= isl_map_cow(map
);
3385 if (isl_map_check_range(map
, type
, first
, n
) < 0)
3386 return isl_map_free(map
);
3388 for (i
= 0; i
< map
->n
; ++i
) {
3389 map
->p
[i
] = isl_basic_map_eliminate_vars(map
->p
[i
],
3390 isl_basic_map_offset(map
->p
[i
], type
) - 1 + first
, n
);
3394 map
= isl_map_drop(map
, type
, first
, n
);
3401 __isl_give isl_set
*isl_set_remove_dims(__isl_take isl_set
*bset
,
3402 enum isl_dim_type type
, unsigned first
, unsigned n
)
3404 return set_from_map(isl_map_remove_dims(set_to_map(bset
),
3408 /* Project out n inputs starting at first using Fourier-Motzkin */
3409 __isl_give isl_map
*isl_map_remove_inputs(__isl_take isl_map
*map
,
3410 unsigned first
, unsigned n
)
3412 return isl_map_remove_dims(map
, isl_dim_in
, first
, n
);
3415 void isl_basic_set_print_internal(__isl_keep isl_basic_set
*bset
,
3416 FILE *out
, int indent
)
3421 fprintf(out
, "null basic set\n");
3425 fprintf(out
, "%*s", indent
, "");
3426 fprintf(out
, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
3427 bset
->ref
, bset
->dim
->nparam
, bset
->dim
->n_out
,
3428 bset
->extra
, bset
->flags
);
3430 p
= isl_printer_to_file(isl_basic_set_get_ctx(bset
), out
);
3431 p
= isl_printer_set_dump(p
, 1);
3432 p
= isl_printer_set_indent(p
, indent
);
3433 p
= isl_printer_start_line(p
);
3434 p
= isl_printer_print_basic_set(p
, bset
);
3435 p
= isl_printer_end_line(p
);
3436 isl_printer_free(p
);
3439 void isl_basic_map_print_internal(__isl_keep isl_basic_map
*bmap
,
3440 FILE *out
, int indent
)
3445 fprintf(out
, "null basic map\n");
3449 fprintf(out
, "%*s", indent
, "");
3450 fprintf(out
, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
3451 "flags: %x, n_name: %d\n",
3453 bmap
->dim
->nparam
, bmap
->dim
->n_in
, bmap
->dim
->n_out
,
3454 bmap
->extra
, bmap
->flags
, bmap
->dim
->n_id
);
3456 p
= isl_printer_to_file(isl_basic_map_get_ctx(bmap
), out
);
3457 p
= isl_printer_set_dump(p
, 1);
3458 p
= isl_printer_set_indent(p
, indent
);
3459 p
= isl_printer_start_line(p
);
3460 p
= isl_printer_print_basic_map(p
, bmap
);
3461 p
= isl_printer_end_line(p
);
3462 isl_printer_free(p
);
3465 __isl_give isl_basic_map
*isl_inequality_negate(__isl_take isl_basic_map
*bmap
,
3470 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
3472 return isl_basic_map_free(bmap
);
3473 if (pos
>= bmap
->n_ineq
)
3474 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
3475 "invalid position", return isl_basic_map_free(bmap
));
3476 isl_seq_neg(bmap
->ineq
[pos
], bmap
->ineq
[pos
], 1 + total
);
3477 isl_int_sub_ui(bmap
->ineq
[pos
][0], bmap
->ineq
[pos
][0], 1);
3478 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
3479 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
3483 __isl_give isl_set
*isl_set_alloc_space(__isl_take isl_space
*space
, int n
,
3486 if (isl_space_check_is_set(space
) < 0)
3488 return isl_map_alloc_space(space
, n
, flags
);
3490 isl_space_free(space
);
3494 /* Make sure "map" has room for at least "n" more basic maps.
3496 __isl_give isl_map
*isl_map_grow(__isl_take isl_map
*map
, int n
)
3499 struct isl_map
*grown
= NULL
;
3503 isl_assert(map
->ctx
, n
>= 0, goto error
);
3504 if (map
->n
+ n
<= map
->size
)
3506 grown
= isl_map_alloc_space(isl_map_get_space(map
), map
->n
+ n
, map
->flags
);
3509 for (i
= 0; i
< map
->n
; ++i
) {
3510 grown
->p
[i
] = isl_basic_map_copy(map
->p
[i
]);
3518 isl_map_free(grown
);
3523 /* Make sure "set" has room for at least "n" more basic sets.
3525 __isl_give isl_set
*isl_set_grow(__isl_take isl_set
*set
, int n
)
3527 return set_from_map(isl_map_grow(set_to_map(set
), n
));
3530 __isl_give isl_set
*isl_set_from_basic_set(__isl_take isl_basic_set
*bset
)
3532 return isl_map_from_basic_map(bset
);
3535 /* This function performs the same operation as isl_set_from_basic_set,
3536 * but is considered as a function on an isl_basic_set when exported.
3538 __isl_give isl_set
*isl_basic_set_to_set(__isl_take isl_basic_set
*bset
)
3540 return isl_set_from_basic_set(bset
);
3543 __isl_give isl_map
*isl_map_from_basic_map(__isl_take isl_basic_map
*bmap
)
3545 struct isl_map
*map
;
3550 map
= isl_map_alloc_space(isl_space_copy(bmap
->dim
), 1, ISL_MAP_DISJOINT
);
3551 return isl_map_add_basic_map(map
, bmap
);
3554 __isl_give isl_set
*isl_set_add_basic_set(__isl_take isl_set
*set
,
3555 __isl_take isl_basic_set
*bset
)
3557 return set_from_map(isl_map_add_basic_map(set_to_map(set
),
3558 bset_to_bmap(bset
)));
3561 __isl_null isl_set
*isl_set_free(__isl_take isl_set
*set
)
3563 return isl_map_free(set
);
3566 void isl_set_print_internal(__isl_keep isl_set
*set
, FILE *out
, int indent
)
3571 fprintf(out
, "null set\n");
3575 fprintf(out
, "%*s", indent
, "");
3576 fprintf(out
, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
3577 set
->ref
, set
->n
, set
->dim
->nparam
, set
->dim
->n_out
,
3579 for (i
= 0; i
< set
->n
; ++i
) {
3580 fprintf(out
, "%*s", indent
, "");
3581 fprintf(out
, "basic set %d:\n", i
);
3582 isl_basic_set_print_internal(set
->p
[i
], out
, indent
+4);
3586 void isl_map_print_internal(__isl_keep isl_map
*map
, FILE *out
, int indent
)
3591 fprintf(out
, "null map\n");
3595 fprintf(out
, "%*s", indent
, "");
3596 fprintf(out
, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
3597 "flags: %x, n_name: %d\n",
3598 map
->ref
, map
->n
, map
->dim
->nparam
, map
->dim
->n_in
,
3599 map
->dim
->n_out
, map
->flags
, map
->dim
->n_id
);
3600 for (i
= 0; i
< map
->n
; ++i
) {
3601 fprintf(out
, "%*s", indent
, "");
3602 fprintf(out
, "basic map %d:\n", i
);
3603 isl_basic_map_print_internal(map
->p
[i
], out
, indent
+4);
3607 /* Check that the space of "bset" is the same as that of the domain of "bmap".
3609 static isl_stat
isl_basic_map_check_compatible_domain(
3610 __isl_keep isl_basic_map
*bmap
, __isl_keep isl_basic_set
*bset
)
3614 ok
= isl_basic_map_compatible_domain(bmap
, bset
);
3616 return isl_stat_error
;
3618 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
3619 "incompatible spaces", return isl_stat_error
);
3624 __isl_give isl_basic_map
*isl_basic_map_intersect_domain(
3625 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*bset
)
3627 struct isl_basic_map
*bmap_domain
;
3630 if (isl_basic_map_check_equal_params(bmap
, bset_to_bmap(bset
)) < 0)
3633 dim
= isl_basic_set_dim(bset
, isl_dim_set
);
3637 isl_basic_map_check_compatible_domain(bmap
, bset
) < 0)
3640 bmap
= isl_basic_map_cow(bmap
);
3643 bmap
= isl_basic_map_extend(bmap
,
3644 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
3645 bmap_domain
= isl_basic_map_from_domain(bset
);
3646 bmap
= add_constraints(bmap
, bmap_domain
, 0, 0);
3648 bmap
= isl_basic_map_simplify(bmap
);
3649 return isl_basic_map_finalize(bmap
);
3651 isl_basic_map_free(bmap
);
3652 isl_basic_set_free(bset
);
3656 /* Check that the space of "bset" is the same as that of the range of "bmap".
3658 static isl_stat
isl_basic_map_check_compatible_range(
3659 __isl_keep isl_basic_map
*bmap
, __isl_keep isl_basic_set
*bset
)
3663 ok
= isl_basic_map_compatible_range(bmap
, bset
);
3665 return isl_stat_error
;
3667 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
3668 "incompatible spaces", return isl_stat_error
);
3673 __isl_give isl_basic_map
*isl_basic_map_intersect_range(
3674 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*bset
)
3676 struct isl_basic_map
*bmap_range
;
3679 if (isl_basic_map_check_equal_params(bmap
, bset_to_bmap(bset
)) < 0)
3682 dim
= isl_basic_set_dim(bset
, isl_dim_set
);
3685 if (dim
!= 0 && isl_basic_map_check_compatible_range(bmap
, bset
) < 0)
3688 if (isl_basic_set_plain_is_universe(bset
)) {
3689 isl_basic_set_free(bset
);
3693 bmap
= isl_basic_map_cow(bmap
);
3696 bmap
= isl_basic_map_extend(bmap
,
3697 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
3698 bmap_range
= bset_to_bmap(bset
);
3699 bmap
= add_constraints(bmap
, bmap_range
, 0, 0);
3701 bmap
= isl_basic_map_simplify(bmap
);
3702 return isl_basic_map_finalize(bmap
);
3704 isl_basic_map_free(bmap
);
3705 isl_basic_set_free(bset
);
3709 isl_bool
isl_basic_map_contains(__isl_keep isl_basic_map
*bmap
,
3710 __isl_keep isl_vec
*vec
)
3716 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
3717 if (total
< 0 || !vec
)
3718 return isl_bool_error
;
3720 if (1 + total
!= vec
->size
)
3721 return isl_bool_false
;
3725 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
3726 isl_seq_inner_product(vec
->el
, bmap
->eq
[i
], 1 + total
, &s
);
3727 if (!isl_int_is_zero(s
)) {
3729 return isl_bool_false
;
3733 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
3734 isl_seq_inner_product(vec
->el
, bmap
->ineq
[i
], 1 + total
, &s
);
3735 if (isl_int_is_neg(s
)) {
3737 return isl_bool_false
;
3743 return isl_bool_true
;
3746 isl_bool
isl_basic_set_contains(__isl_keep isl_basic_set
*bset
,
3747 __isl_keep isl_vec
*vec
)
3749 return isl_basic_map_contains(bset_to_bmap(bset
), vec
);
3752 __isl_give isl_basic_map
*isl_basic_map_intersect(
3753 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
3755 struct isl_vec
*sample
= NULL
;
3756 isl_space
*space1
, *space2
;
3757 isl_size dim1
, dim2
, nparam1
, nparam2
;
3759 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
3761 space1
= isl_basic_map_peek_space(bmap1
);
3762 space2
= isl_basic_map_peek_space(bmap2
);
3763 dim1
= isl_space_dim(space1
, isl_dim_all
);
3764 dim2
= isl_space_dim(space2
, isl_dim_all
);
3765 nparam1
= isl_space_dim(space1
, isl_dim_param
);
3766 nparam2
= isl_space_dim(space2
, isl_dim_param
);
3767 if (dim1
< 0 || dim2
< 0 || nparam1
< 0 || nparam2
< 0)
3769 if (dim1
== nparam1
&& dim2
!= nparam2
)
3770 return isl_basic_map_intersect(bmap2
, bmap1
);
3772 if (dim2
!= nparam2
&&
3773 isl_basic_map_check_equal_space(bmap1
, bmap2
) < 0)
3776 if (isl_basic_map_plain_is_empty(bmap1
)) {
3777 isl_basic_map_free(bmap2
);
3780 if (isl_basic_map_plain_is_empty(bmap2
)) {
3781 isl_basic_map_free(bmap1
);
3785 if (bmap1
->sample
&&
3786 isl_basic_map_contains(bmap1
, bmap1
->sample
) > 0 &&
3787 isl_basic_map_contains(bmap2
, bmap1
->sample
) > 0)
3788 sample
= isl_vec_copy(bmap1
->sample
);
3789 else if (bmap2
->sample
&&
3790 isl_basic_map_contains(bmap1
, bmap2
->sample
) > 0 &&
3791 isl_basic_map_contains(bmap2
, bmap2
->sample
) > 0)
3792 sample
= isl_vec_copy(bmap2
->sample
);
3794 bmap1
= isl_basic_map_cow(bmap1
);
3797 bmap1
= isl_basic_map_extend(bmap1
,
3798 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
3799 bmap1
= add_constraints(bmap1
, bmap2
, 0, 0);
3802 isl_vec_free(sample
);
3804 isl_vec_free(bmap1
->sample
);
3805 bmap1
->sample
= sample
;
3808 bmap1
= isl_basic_map_simplify(bmap1
);
3809 return isl_basic_map_finalize(bmap1
);
3812 isl_vec_free(sample
);
3813 isl_basic_map_free(bmap1
);
3814 isl_basic_map_free(bmap2
);
3818 __isl_give isl_basic_set
*isl_basic_set_intersect(
3819 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
3821 return bset_from_bmap(isl_basic_map_intersect(bset_to_bmap(bset1
),
3822 bset_to_bmap(bset2
)));
3825 /* Intersect the parameter domain of "bmap" with "bset".
3827 * isl_basic_map_intersect handles this as a special case.
3829 __isl_give isl_basic_map
*isl_basic_map_intersect_params(
3830 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*bset
)
3832 return isl_basic_map_intersect(bmap
, bset
);
3835 __isl_give isl_basic_set
*isl_basic_set_intersect_params(
3836 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
3838 isl_basic_map
*bmap
;
3840 bmap
= bset_to_bmap(bset1
);
3841 bmap
= isl_basic_map_intersect_params(bmap
, bset2
);
3842 return bset_from_bmap(bmap
);
3845 /* Does "map" consist of a single disjunct, without any local variables?
3847 static isl_bool
is_convex_no_locals(__isl_keep isl_map
*map
)
3852 return isl_bool_error
;
3854 return isl_bool_false
;
3855 n_div
= isl_basic_map_dim(map
->p
[0], isl_dim_div
);
3857 return isl_bool_error
;
3859 return isl_bool_false
;
3860 return isl_bool_true
;
3863 /* Check that "map" consists of a single disjunct, without any local variables.
3865 static isl_stat
check_convex_no_locals(__isl_keep isl_map
*map
)
3869 ok
= is_convex_no_locals(map
);
3871 return isl_stat_error
;
3875 isl_die(isl_map_get_ctx(map
), isl_error_internal
,
3876 "unexpectedly not convex or involving local variables",
3877 return isl_stat_error
);
3880 /* Special case of isl_map_intersect, where both map1 and map2
3881 * are convex, without any divs and such that either map1 or map2
3882 * contains a single constraint. This constraint is then simply
3883 * added to the other map.
3885 static __isl_give isl_map
*map_intersect_add_constraint(
3886 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
3888 if (check_convex_no_locals(map1
) < 0 ||
3889 check_convex_no_locals(map2
) < 0)
3892 if (map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
!= 1)
3893 return isl_map_intersect(map2
, map1
);
3895 map1
= isl_map_cow(map1
);
3898 if (isl_map_plain_is_empty(map1
)) {
3902 if (map2
->p
[0]->n_eq
== 1)
3903 map1
->p
[0] = isl_basic_map_add_eq(map1
->p
[0], map2
->p
[0]->eq
[0]);
3905 map1
->p
[0] = isl_basic_map_add_ineq(map1
->p
[0],
3906 map2
->p
[0]->ineq
[0]);
3908 map1
->p
[0] = isl_basic_map_simplify(map1
->p
[0]);
3909 map1
->p
[0] = isl_basic_map_finalize(map1
->p
[0]);
3913 if (isl_basic_map_plain_is_empty(map1
->p
[0])) {
3914 isl_basic_map_free(map1
->p
[0]);
3920 map1
= isl_map_unmark_normalized(map1
);
3928 /* map2 may be either a parameter domain or a map living in the same
3931 static __isl_give isl_map
*map_intersect_internal(__isl_take isl_map
*map1
,
3932 __isl_take isl_map
*map2
)
3938 isl_size dim2
, nparam2
;
3943 if ((isl_map_plain_is_empty(map1
) ||
3944 isl_map_plain_is_universe(map2
)) &&
3945 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
3949 if ((isl_map_plain_is_empty(map2
) ||
3950 isl_map_plain_is_universe(map1
)) &&
3951 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
3956 if (is_convex_no_locals(map1
) == isl_bool_true
&&
3957 is_convex_no_locals(map2
) == isl_bool_true
&&
3958 isl_space_is_equal(map1
->dim
, map2
->dim
) &&
3959 (map1
->p
[0]->n_eq
+ map1
->p
[0]->n_ineq
== 1 ||
3960 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1))
3961 return map_intersect_add_constraint(map1
, map2
);
3963 equal
= isl_map_plain_is_equal(map1
, map2
);
3971 dim2
= isl_map_dim(map2
, isl_dim_all
);
3972 nparam2
= isl_map_dim(map2
, isl_dim_param
);
3973 if (dim2
< 0 || nparam2
< 0)
3975 if (dim2
!= nparam2
)
3976 isl_assert(map1
->ctx
,
3977 isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
3979 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
3980 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
3981 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
3983 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
3984 map1
->n
* map2
->n
, flags
);
3987 for (i
= 0; i
< map1
->n
; ++i
)
3988 for (j
= 0; j
< map2
->n
; ++j
) {
3989 struct isl_basic_map
*part
;
3990 part
= isl_basic_map_intersect(
3991 isl_basic_map_copy(map1
->p
[i
]),
3992 isl_basic_map_copy(map2
->p
[j
]));
3993 if (isl_basic_map_is_empty(part
) < 0)
3994 part
= isl_basic_map_free(part
);
3995 result
= isl_map_add_basic_map(result
, part
);
4008 static __isl_give isl_map
*map_intersect(__isl_take isl_map
*map1
,
4009 __isl_take isl_map
*map2
)
4011 if (isl_map_check_equal_space(map1
, map2
) < 0)
4013 return map_intersect_internal(map1
, map2
);
4020 __isl_give isl_map
*isl_map_intersect(__isl_take isl_map
*map1
,
4021 __isl_take isl_map
*map2
)
4023 isl_map_align_params_bin(&map1
, &map2
);
4024 return map_intersect(map1
, map2
);
4027 __isl_give isl_set
*isl_set_intersect(__isl_take isl_set
*set1
,
4028 __isl_take isl_set
*set2
)
4030 return set_from_map(isl_map_intersect(set_to_map(set1
),
4034 /* map_intersect_internal accepts intersections
4035 * with parameter domains, so we can just call that function.
4037 __isl_give isl_map
*isl_map_intersect_params(__isl_take isl_map
*map
,
4038 __isl_take isl_set
*params
)
4040 isl_map_align_params_set(&map
, ¶ms
);
4041 return map_intersect_internal(map
, params
);
4044 __isl_give isl_set
*isl_set_intersect_params(__isl_take isl_set
*set
,
4045 __isl_take isl_set
*params
)
4047 return isl_map_intersect_params(set
, params
);
4050 __isl_give isl_basic_map
*isl_basic_map_reverse(__isl_take isl_basic_map
*bmap
)
4058 bmap
= isl_basic_map_cow(bmap
);
4061 space
= isl_space_reverse(isl_space_copy(bmap
->dim
));
4062 pos
= isl_basic_map_offset(bmap
, isl_dim_in
);
4063 n1
= isl_basic_map_dim(bmap
, isl_dim_in
);
4064 n2
= isl_basic_map_dim(bmap
, isl_dim_out
);
4065 if (n1
< 0 || n2
< 0)
4066 bmap
= isl_basic_map_free(bmap
);
4067 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
4068 return isl_basic_map_reset_space(bmap
, space
);
4071 /* Given a basic map where the tuple of type "type" is a wrapped map,
4072 * swap domain and range of that wrapped map.
4074 static __isl_give isl_basic_map
*isl_basic_map_reverse_wrapped(
4075 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
)
4078 isl_size offset
, n1
, n2
;
4080 space
= isl_basic_map_peek_space(bmap
);
4081 offset
= isl_basic_map_var_offset(bmap
, type
);
4082 n1
= isl_space_wrapped_dim(space
, type
, isl_dim_in
);
4083 n2
= isl_space_wrapped_dim(space
, type
, isl_dim_out
);
4084 if (offset
< 0 || n1
< 0 || n2
< 0)
4085 return isl_basic_map_free(bmap
);
4087 bmap
= isl_basic_map_swap_vars(bmap
, 1 + offset
, n1
, n2
);
4089 space
= isl_basic_map_take_space(bmap
);
4090 space
= isl_space_reverse_wrapped(space
, type
);
4091 bmap
= isl_basic_map_restore_space(bmap
, space
);
4096 /* Given a basic map (A -> B) -> C, return the corresponding basic map
4099 static __isl_give isl_basic_map
*isl_basic_map_domain_reverse(
4100 __isl_take isl_basic_map
*bmap
)
4104 space
= isl_basic_map_peek_space(bmap
);
4105 if (isl_space_check_domain_is_wrapping(space
) < 0)
4106 return isl_basic_map_free(bmap
);
4107 bmap
= isl_basic_map_reverse_wrapped(bmap
, isl_dim_in
);
4112 /* Given a basic map A -> (B -> C), return the corresponding basic map
4115 static __isl_give isl_basic_map
*isl_basic_map_range_reverse(
4116 __isl_take isl_basic_map
*bmap
)
4120 space
= isl_basic_map_peek_space(bmap
);
4121 if (isl_space_check_range_is_wrapping(space
) < 0)
4122 return isl_basic_map_free(bmap
);
4123 bmap
= isl_basic_map_reverse_wrapped(bmap
, isl_dim_out
);
4128 /* Given a basic map that is actually a basic set (A -> B),
4129 * return the corresponding basic set (B -> A) as a basic map.
4131 static __isl_give isl_basic_map
*isl_basic_map_set_reverse(
4132 __isl_take isl_basic_map
*bmap
)
4136 space
= isl_basic_map_peek_space(bmap
);
4137 if (isl_space_check_is_wrapping(space
) < 0)
4138 return isl_basic_map_free(bmap
);
4139 bmap
= isl_basic_map_reverse_wrapped(bmap
, isl_dim_set
);
4144 static __isl_give isl_basic_map
*basic_map_space_reset(
4145 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
)
4151 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
4154 space
= isl_basic_map_get_space(bmap
);
4155 space
= isl_space_reset(space
, type
);
4156 bmap
= isl_basic_map_reset_space(bmap
, space
);
4160 __isl_give isl_basic_map
*isl_basic_map_insert_dims(
4161 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
4162 unsigned pos
, unsigned n
)
4164 isl_bool rational
, is_empty
;
4165 isl_space
*res_space
;
4166 struct isl_basic_map
*res
;
4167 struct isl_dim_map
*dim_map
;
4170 enum isl_dim_type t
;
4173 return basic_map_space_reset(bmap
, type
);
4175 is_empty
= isl_basic_map_plain_is_empty(bmap
);
4176 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4177 if (is_empty
< 0 || total
< 0)
4178 return isl_basic_map_free(bmap
);
4179 res_space
= isl_space_insert_dims(isl_basic_map_get_space(bmap
),
4182 return isl_basic_map_free(bmap
);
4184 isl_basic_map_free(bmap
);
4185 return isl_basic_map_empty(res_space
);
4188 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
+ n
);
4190 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
4194 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
4196 isl_size size
= isl_basic_map_dim(bmap
, t
);
4198 dim_map
= isl_dim_map_free(dim_map
);
4199 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
4201 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
4202 pos
, size
- pos
, off
+ pos
+ n
);
4204 dim
= isl_space_dim(res_space
, t
);
4206 dim_map
= isl_dim_map_free(dim_map
);
4209 isl_dim_map_div(dim_map
, bmap
, off
);
4211 res
= isl_basic_map_alloc_space(res_space
,
4212 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
4213 rational
= isl_basic_map_is_rational(bmap
);
4215 res
= isl_basic_map_free(res
);
4217 res
= isl_basic_map_set_rational(res
);
4218 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
4219 return isl_basic_map_finalize(res
);
4222 __isl_give isl_basic_set
*isl_basic_set_insert_dims(
4223 __isl_take isl_basic_set
*bset
,
4224 enum isl_dim_type type
, unsigned pos
, unsigned n
)
4226 return isl_basic_map_insert_dims(bset
, type
, pos
, n
);
4229 __isl_give isl_basic_map
*isl_basic_map_add_dims(__isl_take isl_basic_map
*bmap
,
4230 enum isl_dim_type type
, unsigned n
)
4234 dim
= isl_basic_map_dim(bmap
, type
);
4236 return isl_basic_map_free(bmap
);
4237 return isl_basic_map_insert_dims(bmap
, type
, dim
, n
);
4240 __isl_give isl_basic_set
*isl_basic_set_add_dims(__isl_take isl_basic_set
*bset
,
4241 enum isl_dim_type type
, unsigned n
)
4245 isl_assert(bset
->ctx
, type
!= isl_dim_in
, goto error
);
4246 return isl_basic_map_add_dims(bset
, type
, n
);
4248 isl_basic_set_free(bset
);
4252 static __isl_give isl_map
*map_space_reset(__isl_take isl_map
*map
,
4253 enum isl_dim_type type
)
4257 if (!map
|| !isl_space_is_named_or_nested(map
->dim
, type
))
4260 space
= isl_map_get_space(map
);
4261 space
= isl_space_reset(space
, type
);
4262 map
= isl_map_reset_space(map
, space
);
4266 __isl_give isl_map
*isl_map_insert_dims(__isl_take isl_map
*map
,
4267 enum isl_dim_type type
, unsigned pos
, unsigned n
)
4273 return map_space_reset(map
, type
);
4275 map
= isl_map_cow(map
);
4279 for (i
= 0; i
< map
->n
; ++i
) {
4280 map
->p
[i
] = isl_basic_map_insert_dims(map
->p
[i
], type
, pos
, n
);
4285 space
= isl_map_take_space(map
);
4286 space
= isl_space_insert_dims(space
, type
, pos
, n
);
4287 map
= isl_map_restore_space(map
, space
);
4295 __isl_give isl_set
*isl_set_insert_dims(__isl_take isl_set
*set
,
4296 enum isl_dim_type type
, unsigned pos
, unsigned n
)
4298 return isl_map_insert_dims(set
, type
, pos
, n
);
4301 __isl_give isl_map
*isl_map_add_dims(__isl_take isl_map
*map
,
4302 enum isl_dim_type type
, unsigned n
)
4306 dim
= isl_map_dim(map
, type
);
4308 return isl_map_free(map
);
4309 return isl_map_insert_dims(map
, type
, dim
, n
);
4312 __isl_give isl_set
*isl_set_add_dims(__isl_take isl_set
*set
,
4313 enum isl_dim_type type
, unsigned n
)
4317 isl_assert(set
->ctx
, type
!= isl_dim_in
, goto error
);
4318 return set_from_map(isl_map_add_dims(set_to_map(set
), type
, n
));
4324 __isl_give isl_basic_map
*isl_basic_map_move_dims(
4325 __isl_take isl_basic_map
*bmap
,
4326 enum isl_dim_type dst_type
, unsigned dst_pos
,
4327 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
4330 struct isl_dim_map
*dim_map
;
4331 struct isl_basic_map
*res
;
4332 enum isl_dim_type t
;
4339 bmap
= isl_basic_map_reset(bmap
, src_type
);
4340 bmap
= isl_basic_map_reset(bmap
, dst_type
);
4344 if (isl_basic_map_check_range(bmap
, src_type
, src_pos
, n
) < 0)
4345 return isl_basic_map_free(bmap
);
4347 if (dst_type
== src_type
&& dst_pos
== src_pos
)
4350 isl_assert(bmap
->ctx
, dst_type
!= src_type
, goto error
);
4352 if (pos(bmap
->dim
, dst_type
) + dst_pos
==
4353 pos(bmap
->dim
, src_type
) + src_pos
+
4354 ((src_type
< dst_type
) ? n
: 0)) {
4355 space
= isl_basic_map_take_space(bmap
);
4356 space
= isl_space_move_dims(space
, dst_type
, dst_pos
,
4357 src_type
, src_pos
, n
);
4358 bmap
= isl_basic_map_restore_space(bmap
, space
);
4359 bmap
= isl_basic_map_finalize(bmap
);
4364 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4366 return isl_basic_map_free(bmap
);
4367 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
4370 space
= isl_basic_map_peek_space(bmap
);
4371 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
4372 isl_size size
= isl_space_dim(space
, t
);
4374 dim_map
= isl_dim_map_free(dim_map
);
4375 if (t
== dst_type
) {
4376 isl_dim_map_dim_range(dim_map
, space
, t
,
4379 isl_dim_map_dim_range(dim_map
, space
, src_type
,
4382 isl_dim_map_dim_range(dim_map
, space
, t
,
4383 dst_pos
, size
- dst_pos
, off
);
4384 off
+= size
- dst_pos
;
4385 } else if (t
== src_type
) {
4386 isl_dim_map_dim_range(dim_map
, space
, t
,
4389 isl_dim_map_dim_range(dim_map
, space
, t
,
4390 src_pos
+ n
, size
- src_pos
- n
, off
);
4391 off
+= size
- src_pos
- n
;
4393 isl_dim_map_dim(dim_map
, space
, t
, off
);
4397 isl_dim_map_div(dim_map
, bmap
, off
);
4399 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
4400 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
4401 bmap
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
4402 space
= isl_basic_map_take_space(bmap
);
4403 space
= isl_space_move_dims(space
, dst_type
, dst_pos
,
4404 src_type
, src_pos
, n
);
4405 bmap
= isl_basic_map_restore_space(bmap
, space
);
4409 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
4410 bmap
= isl_basic_map_gauss(bmap
, NULL
);
4411 bmap
= isl_basic_map_finalize(bmap
);
4415 isl_basic_map_free(bmap
);
4419 __isl_give isl_basic_set
*isl_basic_set_move_dims(__isl_take isl_basic_set
*bset
,
4420 enum isl_dim_type dst_type
, unsigned dst_pos
,
4421 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
4423 isl_basic_map
*bmap
= bset_to_bmap(bset
);
4424 bmap
= isl_basic_map_move_dims(bmap
, dst_type
, dst_pos
,
4425 src_type
, src_pos
, n
);
4426 return bset_from_bmap(bmap
);
4429 __isl_give isl_set
*isl_set_move_dims(__isl_take isl_set
*set
,
4430 enum isl_dim_type dst_type
, unsigned dst_pos
,
4431 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
4435 isl_assert(set
->ctx
, dst_type
!= isl_dim_in
, goto error
);
4436 return set_from_map(isl_map_move_dims(set_to_map(set
),
4437 dst_type
, dst_pos
, src_type
, src_pos
, n
));
4443 __isl_give isl_map
*isl_map_move_dims(__isl_take isl_map
*map
,
4444 enum isl_dim_type dst_type
, unsigned dst_pos
,
4445 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
4451 map
= isl_map_reset(map
, src_type
);
4452 map
= isl_map_reset(map
, dst_type
);
4456 if (isl_map_check_range(map
, src_type
, src_pos
, n
))
4457 return isl_map_free(map
);
4459 if (dst_type
== src_type
&& dst_pos
== src_pos
)
4462 isl_assert(map
->ctx
, dst_type
!= src_type
, goto error
);
4464 map
= isl_map_cow(map
);
4468 for (i
= 0; i
< map
->n
; ++i
) {
4469 map
->p
[i
] = isl_basic_map_move_dims(map
->p
[i
],
4471 src_type
, src_pos
, n
);
4476 space
= isl_map_take_space(map
);
4477 space
= isl_space_move_dims(space
, dst_type
, dst_pos
,
4478 src_type
, src_pos
, n
);
4479 map
= isl_map_restore_space(map
, space
);
4487 /* Move the specified dimensions to the last columns right before
4488 * the divs. Don't change the dimension specification of bmap.
4489 * That's the responsibility of the caller.
4491 static __isl_give isl_basic_map
*move_last(__isl_take isl_basic_map
*bmap
,
4492 enum isl_dim_type type
, unsigned first
, unsigned n
)
4495 struct isl_dim_map
*dim_map
;
4496 struct isl_basic_map
*res
;
4497 enum isl_dim_type t
;
4503 if (isl_basic_map_offset(bmap
, type
) + first
+ n
==
4504 isl_basic_map_offset(bmap
, isl_dim_div
))
4507 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4509 return isl_basic_map_free(bmap
);
4510 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
4513 space
= isl_basic_map_peek_space(bmap
);
4514 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
4515 isl_size size
= isl_space_dim(space
, t
);
4517 dim_map
= isl_dim_map_free(dim_map
);
4519 isl_dim_map_dim_range(dim_map
, space
, t
,
4522 isl_dim_map_dim_range(dim_map
, space
, t
,
4523 first
, n
, total
- bmap
->n_div
- n
);
4524 isl_dim_map_dim_range(dim_map
, space
, t
,
4525 first
+ n
, size
- (first
+ n
), off
);
4526 off
+= size
- (first
+ n
);
4528 isl_dim_map_dim(dim_map
, space
, t
, off
);
4532 isl_dim_map_div(dim_map
, bmap
, off
+ n
);
4534 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
4535 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
4536 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
4540 /* Insert "n" rows in the divs of "bmap".
4542 * The number of columns is not changed, which means that the last
4543 * dimensions of "bmap" are being reintepreted as the new divs.
4544 * The space of "bmap" is not adjusted, however, which means
4545 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
4546 * from the space of "bmap" is the responsibility of the caller.
4548 static __isl_give isl_basic_map
*insert_div_rows(__isl_take isl_basic_map
*bmap
,
4556 bmap
= isl_basic_map_cow(bmap
);
4560 row_size
= isl_basic_map_offset(bmap
, isl_dim_div
) + bmap
->extra
;
4561 old
= bmap
->block2
.data
;
4562 bmap
->block2
= isl_blk_extend(bmap
->ctx
, bmap
->block2
,
4563 (bmap
->extra
+ n
) * (1 + row_size
));
4564 if (!bmap
->block2
.data
)
4565 return isl_basic_map_free(bmap
);
4566 new_div
= isl_alloc_array(bmap
->ctx
, isl_int
*, bmap
->extra
+ n
);
4568 return isl_basic_map_free(bmap
);
4569 for (i
= 0; i
< n
; ++i
) {
4570 new_div
[i
] = bmap
->block2
.data
+
4571 (bmap
->extra
+ i
) * (1 + row_size
);
4572 isl_seq_clr(new_div
[i
], 1 + row_size
);
4574 for (i
= 0; i
< bmap
->extra
; ++i
)
4575 new_div
[n
+ i
] = bmap
->block2
.data
+ (bmap
->div
[i
] - old
);
4577 bmap
->div
= new_div
;
4584 /* Drop constraints from "bmap" that only involve the variables
4585 * of "type" in the range [first, first + n] that are not related
4586 * to any of the variables outside that interval.
4587 * These constraints cannot influence the values for the variables
4588 * outside the interval, except in case they cause "bmap" to be empty.
4589 * Only drop the constraints if "bmap" is known to be non-empty.
4591 static __isl_give isl_basic_map
*drop_irrelevant_constraints(
4592 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
4593 unsigned first
, unsigned n
)
4597 isl_size dim
, n_div
;
4600 non_empty
= isl_basic_map_plain_is_non_empty(bmap
);
4602 return isl_basic_map_free(bmap
);
4606 dim
= isl_basic_map_dim(bmap
, isl_dim_all
);
4607 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4608 if (dim
< 0 || n_div
< 0)
4609 return isl_basic_map_free(bmap
);
4610 groups
= isl_calloc_array(isl_basic_map_get_ctx(bmap
), int, dim
);
4612 return isl_basic_map_free(bmap
);
4613 first
+= isl_basic_map_offset(bmap
, type
) - 1;
4614 for (i
= 0; i
< first
; ++i
)
4616 for (i
= first
+ n
; i
< dim
- n_div
; ++i
)
4619 bmap
= isl_basic_map_drop_unrelated_constraints(bmap
, groups
);
4624 /* Turn the n dimensions of type type, starting at first
4625 * into existentially quantified variables.
4627 * If a subset of the projected out variables are unrelated
4628 * to any of the variables that remain, then the constraints
4629 * involving this subset are simply dropped first.
4631 __isl_give isl_basic_map
*isl_basic_map_project_out(
4632 __isl_take isl_basic_map
*bmap
,
4633 enum isl_dim_type type
, unsigned first
, unsigned n
)
4639 return basic_map_space_reset(bmap
, type
);
4640 if (type
== isl_dim_div
)
4641 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
4642 "cannot project out existentially quantified variables",
4643 return isl_basic_map_free(bmap
));
4645 empty
= isl_basic_map_plain_is_empty(bmap
);
4647 return isl_basic_map_free(bmap
);
4649 bmap
= isl_basic_map_set_to_empty(bmap
);
4651 bmap
= drop_irrelevant_constraints(bmap
, type
, first
, n
);
4655 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
4656 return isl_basic_map_remove_dims(bmap
, type
, first
, n
);
4658 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
4659 return isl_basic_map_free(bmap
);
4661 bmap
= move_last(bmap
, type
, first
, n
);
4662 bmap
= isl_basic_map_cow(bmap
);
4663 bmap
= insert_div_rows(bmap
, n
);
4665 space
= isl_basic_map_take_space(bmap
);
4666 space
= isl_space_drop_dims(space
, type
, first
, n
);
4667 bmap
= isl_basic_map_restore_space(bmap
, space
);
4668 bmap
= isl_basic_map_simplify(bmap
);
4669 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
4670 return isl_basic_map_finalize(bmap
);
4673 /* Turn the n dimensions of type type, starting at first
4674 * into existentially quantified variables.
4676 __isl_give isl_basic_set
*isl_basic_set_project_out(
4677 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
,
4678 unsigned first
, unsigned n
)
4680 return bset_from_bmap(isl_basic_map_project_out(bset_to_bmap(bset
),
4684 /* Turn the n dimensions of type type, starting at first
4685 * into existentially quantified variables.
4687 __isl_give isl_map
*isl_map_project_out(__isl_take isl_map
*map
,
4688 enum isl_dim_type type
, unsigned first
, unsigned n
)
4694 return map_space_reset(map
, type
);
4696 if (isl_map_check_range(map
, type
, first
, n
) < 0)
4697 return isl_map_free(map
);
4699 map
= isl_map_cow(map
);
4703 for (i
= 0; i
< map
->n
; ++i
) {
4704 map
->p
[i
] = isl_basic_map_project_out(map
->p
[i
], type
, first
, n
);
4710 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
4711 map
= isl_map_unmark_normalized(map
);
4713 space
= isl_map_take_space(map
);
4714 space
= isl_space_drop_dims(space
, type
, first
, n
);
4715 map
= isl_map_restore_space(map
, space
);
4724 #define TYPE isl_map
4725 #include "isl_project_out_all_params_templ.c"
4726 #include "isl_project_out_param_templ.c"
4728 /* Turn all the dimensions of type "type", except the "n" starting at "first"
4729 * into existentially quantified variables.
4731 __isl_give isl_map
*isl_map_project_onto(__isl_take isl_map
*map
,
4732 enum isl_dim_type type
, unsigned first
, unsigned n
)
4736 dim
= isl_map_dim(map
, type
);
4737 if (isl_map_check_range(map
, type
, first
, n
) < 0 || dim
< 0)
4738 return isl_map_free(map
);
4739 map
= isl_map_project_out(map
, type
, first
+ n
, dim
- (first
+ n
));
4740 map
= isl_map_project_out(map
, type
, 0, first
);
4744 /* Turn the n dimensions of type type, starting at first
4745 * into existentially quantified variables.
4747 __isl_give isl_set
*isl_set_project_out(__isl_take isl_set
*set
,
4748 enum isl_dim_type type
, unsigned first
, unsigned n
)
4750 return set_from_map(isl_map_project_out(set_to_map(set
),
4754 /* If "set" involves a parameter with identifier "id",
4755 * then turn it into an existentially quantified variable.
4757 __isl_give isl_set
*isl_set_project_out_param_id(__isl_take isl_set
*set
,
4758 __isl_take isl_id
*id
)
4760 return set_from_map(isl_map_project_out_param_id(set_to_map(set
), id
));
4763 /* If "set" involves any of the parameters with identifiers in "list",
4764 * then turn them into existentially quantified variables.
4766 __isl_give isl_set
*isl_set_project_out_param_id_list(__isl_take isl_set
*set
,
4767 __isl_take isl_id_list
*list
)
4771 map
= set_to_map(set
);
4772 map
= isl_map_project_out_param_id_list(map
, list
);
4773 return set_from_map(map
);
4776 /* Project out all parameters from "set" by existentially quantifying
4779 __isl_give isl_set
*isl_set_project_out_all_params(__isl_take isl_set
*set
)
4781 return set_from_map(isl_map_project_out_all_params(set_to_map(set
)));
4784 /* Return a map that projects the elements in "set" onto their
4785 * "n" set dimensions starting at "first".
4786 * "type" should be equal to isl_dim_set.
4788 __isl_give isl_map
*isl_set_project_onto_map(__isl_take isl_set
*set
,
4789 enum isl_dim_type type
, unsigned first
, unsigned n
)
4794 if (type
!= isl_dim_set
)
4795 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
4796 "only set dimensions can be projected out", goto error
);
4797 if (isl_set_check_range(set
, type
, first
, n
) < 0)
4798 return isl_set_free(set
);
4800 map
= isl_map_from_domain(set
);
4801 map
= isl_map_add_dims(map
, isl_dim_out
, n
);
4802 for (i
= 0; i
< n
; ++i
)
4803 map
= isl_map_equate(map
, isl_dim_in
, first
+ i
,
4811 static __isl_give isl_basic_map
*add_divs(__isl_take isl_basic_map
*bmap
,
4817 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4819 return isl_basic_map_free(bmap
);
4820 for (i
= 0; i
< n
; ++i
) {
4821 j
= isl_basic_map_alloc_div(bmap
);
4824 isl_seq_clr(bmap
->div
[j
], 1 + 1 + total
);
4828 isl_basic_map_free(bmap
);
4832 /* Does "bmap2" apply to the range of "bmap1" (ignoring parameters)?
4834 isl_bool
isl_basic_map_applies_range(__isl_keep isl_basic_map
*bmap1
,
4835 __isl_keep isl_basic_map
*bmap2
)
4837 isl_space
*space1
, *space2
;
4839 space1
= isl_basic_map_peek_space(bmap1
);
4840 space2
= isl_basic_map_peek_space(bmap2
);
4841 return isl_space_tuple_is_equal(space1
, isl_dim_out
,
4842 space2
, isl_dim_in
);
4845 /* Check that "bmap2" applies to the range of "bmap1" (ignoring parameters).
4847 static isl_stat
isl_basic_map_check_applies_range(
4848 __isl_keep isl_basic_map
*bmap1
, __isl_keep isl_basic_map
*bmap2
)
4852 equal
= isl_basic_map_applies_range(bmap1
, bmap2
);
4854 return isl_stat_error
;
4856 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
4857 "spaces don't match", return isl_stat_error
);
4861 __isl_give isl_basic_map
*isl_basic_map_apply_range(
4862 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
4864 isl_space
*space_result
= NULL
;
4865 struct isl_basic_map
*bmap
;
4866 isl_size n_in
, n_out
, n
, nparam
;
4867 unsigned total
, pos
;
4868 struct isl_dim_map
*dim_map1
, *dim_map2
;
4870 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
4872 if (isl_basic_map_check_applies_range(bmap1
, bmap2
) < 0)
4875 n_in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
4876 n_out
= isl_basic_map_dim(bmap2
, isl_dim_out
);
4877 n
= isl_basic_map_dim(bmap1
, isl_dim_out
);
4878 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
4879 if (n_in
< 0 || n_out
< 0 || n
< 0 || nparam
< 0)
4882 space_result
= isl_space_join(isl_basic_map_get_space(bmap1
),
4883 isl_basic_map_get_space(bmap2
));
4885 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ n
;
4886 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4887 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4888 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
4889 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
4890 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
4891 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_in
);
4892 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_out
);
4893 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
4894 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
4895 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
4897 bmap
= isl_basic_map_alloc_space(space_result
,
4898 bmap1
->n_div
+ bmap2
->n_div
+ n
,
4899 bmap1
->n_eq
+ bmap2
->n_eq
,
4900 bmap1
->n_ineq
+ bmap2
->n_ineq
);
4901 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
4902 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
4903 bmap
= add_divs(bmap
, n
);
4904 bmap
= isl_basic_map_simplify(bmap
);
4905 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
4906 return isl_basic_map_finalize(bmap
);
4908 isl_basic_map_free(bmap1
);
4909 isl_basic_map_free(bmap2
);
4913 __isl_give isl_basic_set
*isl_basic_set_apply(__isl_take isl_basic_set
*bset
,
4914 __isl_take isl_basic_map
*bmap
)
4916 if (isl_basic_map_check_compatible_domain(bmap
, bset
) < 0)
4919 return bset_from_bmap(isl_basic_map_apply_range(bset_to_bmap(bset
),
4922 isl_basic_set_free(bset
);
4923 isl_basic_map_free(bmap
);
4927 __isl_give isl_basic_map
*isl_basic_map_apply_domain(
4928 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
4930 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
4932 if (!isl_space_tuple_is_equal(bmap1
->dim
, isl_dim_in
,
4933 bmap2
->dim
, isl_dim_in
))
4934 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
4935 "spaces don't match", goto error
);
4937 bmap1
= isl_basic_map_reverse(bmap1
);
4938 bmap1
= isl_basic_map_apply_range(bmap1
, bmap2
);
4939 return isl_basic_map_reverse(bmap1
);
4941 isl_basic_map_free(bmap1
);
4942 isl_basic_map_free(bmap2
);
4946 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
4947 * A \cap B -> f(A) + f(B)
4949 __isl_give isl_basic_map
*isl_basic_map_sum(__isl_take isl_basic_map
*bmap1
,
4950 __isl_take isl_basic_map
*bmap2
)
4952 isl_size n_in
, n_out
, nparam
;
4953 unsigned total
, pos
;
4954 struct isl_basic_map
*bmap
= NULL
;
4955 struct isl_dim_map
*dim_map1
, *dim_map2
;
4958 if (isl_basic_map_check_equal_space(bmap1
, bmap2
) < 0)
4961 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
4962 n_in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
4963 n_out
= isl_basic_map_dim(bmap1
, isl_dim_out
);
4964 if (nparam
< 0 || n_in
< 0 || n_out
< 0)
4967 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
;
4968 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4969 dim_map2
= isl_dim_map_alloc(bmap2
->ctx
, total
);
4970 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
4971 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
);
4972 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
4973 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
4974 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_in
+ n_out
);
4975 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
4976 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
4977 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_out
);
4979 bmap
= isl_basic_map_alloc_space(isl_space_copy(bmap1
->dim
),
4980 bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
,
4981 bmap1
->n_eq
+ bmap2
->n_eq
+ n_out
,
4982 bmap1
->n_ineq
+ bmap2
->n_ineq
);
4983 for (i
= 0; i
< n_out
; ++i
) {
4984 int j
= isl_basic_map_alloc_equality(bmap
);
4987 isl_seq_clr(bmap
->eq
[j
], 1+total
);
4988 isl_int_set_si(bmap
->eq
[j
][1+nparam
+n_in
+i
], -1);
4989 isl_int_set_si(bmap
->eq
[j
][1+pos
+i
], 1);
4990 isl_int_set_si(bmap
->eq
[j
][1+pos
-n_out
+i
], 1);
4992 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
4993 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
4994 bmap
= add_divs(bmap
, 2 * n_out
);
4996 bmap
= isl_basic_map_simplify(bmap
);
4997 return isl_basic_map_finalize(bmap
);
4999 isl_basic_map_free(bmap
);
5000 isl_basic_map_free(bmap1
);
5001 isl_basic_map_free(bmap2
);
5005 /* Given two maps A -> f(A) and B -> g(B), construct a map
5006 * A \cap B -> f(A) + f(B)
5008 __isl_give isl_map
*isl_map_sum(__isl_take isl_map
*map1
,
5009 __isl_take isl_map
*map2
)
5011 struct isl_map
*result
;
5014 if (isl_map_check_equal_space(map1
, map2
) < 0)
5017 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
5018 map1
->n
* map2
->n
, 0);
5021 for (i
= 0; i
< map1
->n
; ++i
)
5022 for (j
= 0; j
< map2
->n
; ++j
) {
5023 struct isl_basic_map
*part
;
5024 part
= isl_basic_map_sum(
5025 isl_basic_map_copy(map1
->p
[i
]),
5026 isl_basic_map_copy(map2
->p
[j
]));
5027 if (isl_basic_map_is_empty(part
))
5028 isl_basic_map_free(part
);
5030 result
= isl_map_add_basic_map(result
, part
);
5043 __isl_give isl_set
*isl_set_sum(__isl_take isl_set
*set1
,
5044 __isl_take isl_set
*set2
)
5046 return set_from_map(isl_map_sum(set_to_map(set1
), set_to_map(set2
)));
5049 /* Given a basic map A -> f(A), construct A -> -f(A).
5051 __isl_give isl_basic_map
*isl_basic_map_neg(__isl_take isl_basic_map
*bmap
)
5057 bmap
= isl_basic_map_cow(bmap
);
5058 n
= isl_basic_map_dim(bmap
, isl_dim_out
);
5060 return isl_basic_map_free(bmap
);
5062 off
= isl_basic_map_offset(bmap
, isl_dim_out
);
5063 for (i
= 0; i
< bmap
->n_eq
; ++i
)
5064 for (j
= 0; j
< n
; ++j
)
5065 isl_int_neg(bmap
->eq
[i
][off
+j
], bmap
->eq
[i
][off
+j
]);
5066 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
5067 for (j
= 0; j
< n
; ++j
)
5068 isl_int_neg(bmap
->ineq
[i
][off
+j
], bmap
->ineq
[i
][off
+j
]);
5069 for (i
= 0; i
< bmap
->n_div
; ++i
)
5070 for (j
= 0; j
< n
; ++j
)
5071 isl_int_neg(bmap
->div
[i
][1+off
+j
], bmap
->div
[i
][1+off
+j
]);
5072 bmap
= isl_basic_map_gauss(bmap
, NULL
);
5073 return isl_basic_map_finalize(bmap
);
5076 __isl_give isl_basic_set
*isl_basic_set_neg(__isl_take isl_basic_set
*bset
)
5078 return isl_basic_map_neg(bset
);
5081 /* Given a map A -> f(A), construct A -> -f(A).
5083 __isl_give isl_map
*isl_map_neg(__isl_take isl_map
*map
)
5087 map
= isl_map_cow(map
);
5091 for (i
= 0; i
< map
->n
; ++i
) {
5092 map
->p
[i
] = isl_basic_map_neg(map
->p
[i
]);
5103 __isl_give isl_set
*isl_set_neg(__isl_take isl_set
*set
)
5105 return set_from_map(isl_map_neg(set_to_map(set
)));
5108 /* Given a basic map A -> f(A) and an integer d, construct a basic map
5109 * A -> floor(f(A)/d).
5111 __isl_give isl_basic_map
*isl_basic_map_floordiv(__isl_take isl_basic_map
*bmap
,
5114 isl_size n_in
, n_out
, nparam
;
5115 unsigned total
, pos
;
5116 struct isl_basic_map
*result
= NULL
;
5117 struct isl_dim_map
*dim_map
;
5120 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5121 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5122 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5123 if (nparam
< 0 || n_in
< 0 || n_out
< 0)
5124 return isl_basic_map_free(bmap
);
5126 total
= nparam
+ n_in
+ n_out
+ bmap
->n_div
+ n_out
;
5127 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
5128 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_param
, pos
= 0);
5129 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_in
, pos
+= nparam
);
5130 isl_dim_map_div(dim_map
, bmap
, pos
+= n_in
+ n_out
);
5131 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_out
, pos
+= bmap
->n_div
);
5133 result
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
5134 bmap
->n_div
+ n_out
,
5135 bmap
->n_eq
, bmap
->n_ineq
+ 2 * n_out
);
5136 result
= isl_basic_map_add_constraints_dim_map(result
, bmap
, dim_map
);
5137 result
= add_divs(result
, n_out
);
5138 for (i
= 0; i
< n_out
; ++i
) {
5140 j
= isl_basic_map_alloc_inequality(result
);
5143 isl_seq_clr(result
->ineq
[j
], 1+total
);
5144 isl_int_neg(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
5145 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], 1);
5146 j
= isl_basic_map_alloc_inequality(result
);
5149 isl_seq_clr(result
->ineq
[j
], 1+total
);
5150 isl_int_set(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
5151 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], -1);
5152 isl_int_sub_ui(result
->ineq
[j
][0], d
, 1);
5155 result
= isl_basic_map_simplify(result
);
5156 return isl_basic_map_finalize(result
);
5158 isl_basic_map_free(result
);
5162 /* Given a map A -> f(A) and an integer d, construct a map
5163 * A -> floor(f(A)/d).
5165 __isl_give isl_map
*isl_map_floordiv(__isl_take isl_map
*map
, isl_int d
)
5169 map
= isl_map_cow(map
);
5173 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
5174 for (i
= 0; i
< map
->n
; ++i
) {
5175 map
->p
[i
] = isl_basic_map_floordiv(map
->p
[i
], d
);
5179 map
= isl_map_unmark_normalized(map
);
5187 /* Given a map A -> f(A) and an integer d, construct a map
5188 * A -> floor(f(A)/d).
5190 __isl_give isl_map
*isl_map_floordiv_val(__isl_take isl_map
*map
,
5191 __isl_take isl_val
*d
)
5195 if (!isl_val_is_int(d
))
5196 isl_die(isl_val_get_ctx(d
), isl_error_invalid
,
5197 "expecting integer denominator", goto error
);
5198 map
= isl_map_floordiv(map
, d
->n
);
5207 static __isl_give isl_basic_map
*var_equal(__isl_take isl_basic_map
*bmap
,
5215 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5216 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5217 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5218 if (total
< 0 || nparam
< 0 || n_in
< 0)
5219 return isl_basic_map_free(bmap
);
5220 i
= isl_basic_map_alloc_equality(bmap
);
5223 isl_seq_clr(bmap
->eq
[i
], 1 + total
);
5224 isl_int_set_si(bmap
->eq
[i
][1+nparam
+pos
], -1);
5225 isl_int_set_si(bmap
->eq
[i
][1+nparam
+n_in
+pos
], 1);
5226 return isl_basic_map_finalize(bmap
);
5228 isl_basic_map_free(bmap
);
5232 /* Add a constraint to "bmap" expressing i_pos < o_pos
5234 static __isl_give isl_basic_map
*var_less(__isl_take isl_basic_map
*bmap
,
5242 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5243 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5244 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5245 if (total
< 0 || nparam
< 0 || n_in
< 0)
5246 return isl_basic_map_free(bmap
);
5247 i
= isl_basic_map_alloc_inequality(bmap
);
5250 isl_seq_clr(bmap
->ineq
[i
], 1 + total
);
5251 isl_int_set_si(bmap
->ineq
[i
][0], -1);
5252 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
5253 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
5254 return isl_basic_map_finalize(bmap
);
5256 isl_basic_map_free(bmap
);
5260 /* Add a constraint to "bmap" expressing i_pos <= o_pos
5262 static __isl_give isl_basic_map
*var_less_or_equal(
5263 __isl_take isl_basic_map
*bmap
, unsigned pos
)
5270 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5271 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5272 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5273 if (total
< 0 || nparam
< 0 || n_in
< 0)
5274 return isl_basic_map_free(bmap
);
5275 i
= isl_basic_map_alloc_inequality(bmap
);
5278 isl_seq_clr(bmap
->ineq
[i
], 1 + total
);
5279 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
5280 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
5281 return isl_basic_map_finalize(bmap
);
5283 isl_basic_map_free(bmap
);
5287 /* Add a constraint to "bmap" expressing i_pos > o_pos
5289 static __isl_give isl_basic_map
*var_more(__isl_take isl_basic_map
*bmap
,
5297 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5298 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5299 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5300 if (total
< 0 || nparam
< 0 || n_in
< 0)
5301 return isl_basic_map_free(bmap
);
5302 i
= isl_basic_map_alloc_inequality(bmap
);
5305 isl_seq_clr(bmap
->ineq
[i
], 1 + total
);
5306 isl_int_set_si(bmap
->ineq
[i
][0], -1);
5307 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
5308 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
5309 return isl_basic_map_finalize(bmap
);
5311 isl_basic_map_free(bmap
);
5315 /* Add a constraint to "bmap" expressing i_pos >= o_pos
5317 static __isl_give isl_basic_map
*var_more_or_equal(
5318 __isl_take isl_basic_map
*bmap
, unsigned pos
)
5325 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5326 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5327 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5328 if (total
< 0 || nparam
< 0 || n_in
< 0)
5329 return isl_basic_map_free(bmap
);
5330 i
= isl_basic_map_alloc_inequality(bmap
);
5333 isl_seq_clr(bmap
->ineq
[i
], 1 + total
);
5334 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
5335 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
5336 return isl_basic_map_finalize(bmap
);
5338 isl_basic_map_free(bmap
);
5342 __isl_give isl_basic_map
*isl_basic_map_equal(
5343 __isl_take isl_space
*space
, unsigned n_equal
)
5346 struct isl_basic_map
*bmap
;
5347 bmap
= isl_basic_map_alloc_space(space
, 0, n_equal
, 0);
5350 for (i
= 0; i
< n_equal
&& bmap
; ++i
)
5351 bmap
= var_equal(bmap
, i
);
5352 return isl_basic_map_finalize(bmap
);
5355 /* Return a relation on of dimension "space" expressing i_[0..pos] << o_[0..pos]
5357 __isl_give isl_basic_map
*isl_basic_map_less_at(__isl_take isl_space
*space
,
5361 struct isl_basic_map
*bmap
;
5362 bmap
= isl_basic_map_alloc_space(space
, 0, pos
, 1);
5365 for (i
= 0; i
< pos
&& bmap
; ++i
)
5366 bmap
= var_equal(bmap
, i
);
5368 bmap
= var_less(bmap
, pos
);
5369 return isl_basic_map_finalize(bmap
);
5372 /* Return a relation on "space" expressing i_[0..pos] <<= o_[0..pos]
5374 __isl_give isl_basic_map
*isl_basic_map_less_or_equal_at(
5375 __isl_take isl_space
*space
, unsigned pos
)
5378 isl_basic_map
*bmap
;
5380 bmap
= isl_basic_map_alloc_space(space
, 0, pos
, 1);
5381 for (i
= 0; i
< pos
; ++i
)
5382 bmap
= var_equal(bmap
, i
);
5383 bmap
= var_less_or_equal(bmap
, pos
);
5384 return isl_basic_map_finalize(bmap
);
5387 /* Return a relation on "space" expressing i_pos > o_pos
5389 __isl_give isl_basic_map
*isl_basic_map_more_at(__isl_take isl_space
*space
,
5393 struct isl_basic_map
*bmap
;
5394 bmap
= isl_basic_map_alloc_space(space
, 0, pos
, 1);
5397 for (i
= 0; i
< pos
&& bmap
; ++i
)
5398 bmap
= var_equal(bmap
, i
);
5400 bmap
= var_more(bmap
, pos
);
5401 return isl_basic_map_finalize(bmap
);
5404 /* Return a relation on "space" expressing i_[0..pos] >>= o_[0..pos]
5406 __isl_give isl_basic_map
*isl_basic_map_more_or_equal_at(
5407 __isl_take isl_space
*space
, unsigned pos
)
5410 isl_basic_map
*bmap
;
5412 bmap
= isl_basic_map_alloc_space(space
, 0, pos
, 1);
5413 for (i
= 0; i
< pos
; ++i
)
5414 bmap
= var_equal(bmap
, i
);
5415 bmap
= var_more_or_equal(bmap
, pos
);
5416 return isl_basic_map_finalize(bmap
);
5419 static __isl_give isl_map
*map_lex_lte_first(__isl_take isl_space
*space
,
5420 unsigned n
, int equal
)
5422 struct isl_map
*map
;
5425 if (n
== 0 && equal
)
5426 return isl_map_universe(space
);
5428 map
= isl_map_alloc_space(isl_space_copy(space
), n
, ISL_MAP_DISJOINT
);
5430 for (i
= 0; i
+ 1 < n
; ++i
)
5431 map
= isl_map_add_basic_map(map
,
5432 isl_basic_map_less_at(isl_space_copy(space
), i
));
5435 map
= isl_map_add_basic_map(map
,
5436 isl_basic_map_less_or_equal_at(space
, n
- 1));
5438 map
= isl_map_add_basic_map(map
,
5439 isl_basic_map_less_at(space
, n
- 1));
5441 isl_space_free(space
);
5446 static __isl_give isl_map
*map_lex_lte(__isl_take isl_space
*space
, int equal
)
5450 return map_lex_lte_first(space
, space
->n_out
, equal
);
5453 __isl_give isl_map
*isl_map_lex_lt_first(__isl_take isl_space
*space
,
5456 return map_lex_lte_first(space
, n
, 0);
5459 __isl_give isl_map
*isl_map_lex_le_first(__isl_take isl_space
*space
,
5462 return map_lex_lte_first(space
, n
, 1);
5465 __isl_give isl_map
*isl_map_lex_lt(__isl_take isl_space
*set_space
)
5467 return map_lex_lte(isl_space_map_from_set(set_space
), 0);
5470 __isl_give isl_map
*isl_map_lex_le(__isl_take isl_space
*set_space
)
5472 return map_lex_lte(isl_space_map_from_set(set_space
), 1);
5475 static __isl_give isl_map
*map_lex_gte_first(__isl_take isl_space
*space
,
5476 unsigned n
, int equal
)
5478 struct isl_map
*map
;
5481 if (n
== 0 && equal
)
5482 return isl_map_universe(space
);
5484 map
= isl_map_alloc_space(isl_space_copy(space
), n
, ISL_MAP_DISJOINT
);
5486 for (i
= 0; i
+ 1 < n
; ++i
)
5487 map
= isl_map_add_basic_map(map
,
5488 isl_basic_map_more_at(isl_space_copy(space
), i
));
5491 map
= isl_map_add_basic_map(map
,
5492 isl_basic_map_more_or_equal_at(space
, n
- 1));
5494 map
= isl_map_add_basic_map(map
,
5495 isl_basic_map_more_at(space
, n
- 1));
5497 isl_space_free(space
);
5502 static __isl_give isl_map
*map_lex_gte(__isl_take isl_space
*space
, int equal
)
5506 return map_lex_gte_first(space
, space
->n_out
, equal
);
5509 __isl_give isl_map
*isl_map_lex_gt_first(__isl_take isl_space
*space
,
5512 return map_lex_gte_first(space
, n
, 0);
5515 __isl_give isl_map
*isl_map_lex_ge_first(__isl_take isl_space
*space
,
5518 return map_lex_gte_first(space
, n
, 1);
5521 __isl_give isl_map
*isl_map_lex_gt(__isl_take isl_space
*set_space
)
5523 return map_lex_gte(isl_space_map_from_set(set_space
), 0);
5526 __isl_give isl_map
*isl_map_lex_ge(__isl_take isl_space
*set_space
)
5528 return map_lex_gte(isl_space_map_from_set(set_space
), 1);
5531 __isl_give isl_map
*isl_set_lex_le_set(__isl_take isl_set
*set1
,
5532 __isl_take isl_set
*set2
)
5535 map
= isl_map_lex_le(isl_set_get_space(set1
));
5536 map
= isl_map_intersect_domain(map
, set1
);
5537 map
= isl_map_intersect_range(map
, set2
);
5541 __isl_give isl_map
*isl_set_lex_lt_set(__isl_take isl_set
*set1
,
5542 __isl_take isl_set
*set2
)
5545 map
= isl_map_lex_lt(isl_set_get_space(set1
));
5546 map
= isl_map_intersect_domain(map
, set1
);
5547 map
= isl_map_intersect_range(map
, set2
);
5551 __isl_give isl_map
*isl_set_lex_ge_set(__isl_take isl_set
*set1
,
5552 __isl_take isl_set
*set2
)
5555 map
= isl_map_lex_ge(isl_set_get_space(set1
));
5556 map
= isl_map_intersect_domain(map
, set1
);
5557 map
= isl_map_intersect_range(map
, set2
);
5561 __isl_give isl_map
*isl_set_lex_gt_set(__isl_take isl_set
*set1
,
5562 __isl_take isl_set
*set2
)
5565 map
= isl_map_lex_gt(isl_set_get_space(set1
));
5566 map
= isl_map_intersect_domain(map
, set1
);
5567 map
= isl_map_intersect_range(map
, set2
);
5571 __isl_give isl_map
*isl_map_lex_le_map(__isl_take isl_map
*map1
,
5572 __isl_take isl_map
*map2
)
5575 map
= isl_map_lex_le(isl_space_range(isl_map_get_space(map1
)));
5576 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
5577 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
5581 __isl_give isl_map
*isl_map_lex_lt_map(__isl_take isl_map
*map1
,
5582 __isl_take isl_map
*map2
)
5585 map
= isl_map_lex_lt(isl_space_range(isl_map_get_space(map1
)));
5586 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
5587 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
5591 __isl_give isl_map
*isl_map_lex_ge_map(__isl_take isl_map
*map1
,
5592 __isl_take isl_map
*map2
)
5595 map
= isl_map_lex_ge(isl_space_range(isl_map_get_space(map1
)));
5596 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
5597 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
5601 __isl_give isl_map
*isl_map_lex_gt_map(__isl_take isl_map
*map1
,
5602 __isl_take isl_map
*map2
)
5605 map
= isl_map_lex_gt(isl_space_range(isl_map_get_space(map1
)));
5606 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
5607 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
5611 /* For the div d = floor(f/m) at position "div", add the constraint
5615 static __isl_give isl_basic_map
*add_upper_div_constraint(
5616 __isl_take isl_basic_map
*bmap
, unsigned div
)
5619 isl_size v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
5623 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
5624 if (v_div
< 0 || n_div
< 0)
5625 return isl_basic_map_free(bmap
);
5627 i
= isl_basic_map_alloc_inequality(bmap
);
5629 return isl_basic_map_free(bmap
);
5630 isl_seq_cpy(bmap
->ineq
[i
], bmap
->div
[div
] + 1, 1 + v_div
+ n_div
);
5631 isl_int_neg(bmap
->ineq
[i
][1 + pos
], bmap
->div
[div
][0]);
5636 /* For the div d = floor(f/m) at position "div", add the constraint
5638 * -(f-(m-1)) + m d >= 0
5640 static __isl_give isl_basic_map
*add_lower_div_constraint(
5641 __isl_take isl_basic_map
*bmap
, unsigned div
)
5644 isl_size v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
5648 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
5649 if (v_div
< 0 || n_div
< 0)
5650 return isl_basic_map_free(bmap
);
5652 i
= isl_basic_map_alloc_inequality(bmap
);
5654 return isl_basic_map_free(bmap
);
5655 isl_seq_neg(bmap
->ineq
[i
], bmap
->div
[div
] + 1, 1 + v_div
+ n_div
);
5656 isl_int_set(bmap
->ineq
[i
][1 + pos
], bmap
->div
[div
][0]);
5657 isl_int_add(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], bmap
->ineq
[i
][1 + pos
]);
5658 isl_int_sub_ui(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], 1);
5663 /* For the div d = floor(f/m) at position "pos", add the constraints
5666 * -(f-(m-1)) + m d >= 0
5668 * Note that the second constraint is the negation of
5672 __isl_give isl_basic_map
*isl_basic_map_add_div_constraints(
5673 __isl_take isl_basic_map
*bmap
, unsigned pos
)
5675 bmap
= add_upper_div_constraint(bmap
, pos
);
5676 bmap
= add_lower_div_constraint(bmap
, pos
);
5680 /* For each known div d = floor(f/m), add the constraints
5683 * -(f-(m-1)) + m d >= 0
5685 * Remove duplicate constraints in case of some these div constraints
5686 * already appear in "bmap".
5688 __isl_give isl_basic_map
*isl_basic_map_add_known_div_constraints(
5689 __isl_take isl_basic_map
*bmap
)
5693 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
5695 return isl_basic_map_free(bmap
);
5699 bmap
= add_known_div_constraints(bmap
);
5700 bmap
= isl_basic_map_remove_duplicate_constraints(bmap
, NULL
, 0);
5701 bmap
= isl_basic_map_finalize(bmap
);
5705 /* Add the div constraint of sign "sign" for div "div" of "bmap".
5707 * In particular, if this div is of the form d = floor(f/m),
5708 * then add the constraint
5712 * if sign < 0 or the constraint
5714 * -(f-(m-1)) + m d >= 0
5718 __isl_give isl_basic_map
*isl_basic_map_add_div_constraint(
5719 __isl_take isl_basic_map
*bmap
, unsigned div
, int sign
)
5722 return add_upper_div_constraint(bmap
, div
);
5724 return add_lower_div_constraint(bmap
, div
);
5727 __isl_give isl_basic_set
*isl_basic_map_underlying_set(
5728 __isl_take isl_basic_map
*bmap
)
5734 if (bmap
->dim
->nparam
== 0 && bmap
->dim
->n_in
== 0 &&
5736 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_in
) &&
5737 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_out
))
5738 return bset_from_bmap(bmap
);
5739 bmap
= isl_basic_map_cow(bmap
);
5742 space
= isl_basic_map_take_space(bmap
);
5743 space
= isl_space_underlying(space
, bmap
->n_div
);
5744 bmap
= isl_basic_map_restore_space(bmap
, space
);
5747 bmap
->extra
-= bmap
->n_div
;
5749 bmap
= isl_basic_map_finalize(bmap
);
5750 return bset_from_bmap(bmap
);
5752 isl_basic_map_free(bmap
);
5756 __isl_give isl_basic_set
*isl_basic_set_underlying_set(
5757 __isl_take isl_basic_set
*bset
)
5759 return isl_basic_map_underlying_set(bset_to_bmap(bset
));
5762 /* Replace each element in "list" by the result of applying
5763 * isl_basic_map_underlying_set to the element.
5765 __isl_give isl_basic_set_list
*isl_basic_map_list_underlying_set(
5766 __isl_take isl_basic_map_list
*list
)
5771 n
= isl_basic_map_list_n_basic_map(list
);
5775 for (i
= 0; i
< n
; ++i
) {
5776 isl_basic_map
*bmap
;
5777 isl_basic_set
*bset
;
5779 bmap
= isl_basic_map_list_get_basic_map(list
, i
);
5780 bset
= isl_basic_set_underlying_set(bmap
);
5781 list
= isl_basic_set_list_set_basic_set(list
, i
, bset
);
5786 isl_basic_map_list_free(list
);
5790 __isl_give isl_basic_map
*isl_basic_map_overlying_set(
5791 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_map
*like
)
5793 struct isl_basic_map
*bmap
;
5794 struct isl_ctx
*ctx
;
5795 isl_size dim
, bmap_total
;
5802 if (isl_basic_set_check_no_params(bset
) < 0 ||
5803 isl_basic_set_check_no_locals(bset
) < 0)
5805 dim
= isl_basic_set_dim(bset
, isl_dim_set
);
5806 bmap_total
= isl_basic_map_dim(like
, isl_dim_all
);
5807 if (dim
< 0 || bmap_total
< 0)
5809 isl_assert(ctx
, dim
== bmap_total
, goto error
);
5810 if (like
->n_div
== 0) {
5811 isl_space
*space
= isl_basic_map_get_space(like
);
5812 isl_basic_map_free(like
);
5813 return isl_basic_map_reset_space(bset
, space
);
5815 bset
= isl_basic_set_cow(bset
);
5818 total
= dim
+ bset
->extra
;
5819 bmap
= bset_to_bmap(bset
);
5820 isl_space_free(isl_basic_map_take_space(bmap
));
5821 bmap
= isl_basic_map_restore_space(bmap
, isl_basic_map_get_space(like
));
5824 bmap
->n_div
= like
->n_div
;
5825 bmap
->extra
+= like
->n_div
;
5829 ltotal
= total
- bmap
->extra
+ like
->extra
;
5832 bmap
->block2
= isl_blk_extend(ctx
, bmap
->block2
,
5833 bmap
->extra
* (1 + 1 + total
));
5834 if (isl_blk_is_error(bmap
->block2
))
5836 div
= isl_realloc_array(ctx
, bmap
->div
, isl_int
*, bmap
->extra
);
5840 for (i
= 0; i
< bmap
->extra
; ++i
)
5841 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + 1 + total
);
5842 for (i
= 0; i
< like
->n_div
; ++i
) {
5843 isl_seq_cpy(bmap
->div
[i
], like
->div
[i
], 1 + 1 + ltotal
);
5844 isl_seq_clr(bmap
->div
[i
]+1+1+ltotal
, total
- ltotal
);
5846 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
5848 isl_basic_map_free(like
);
5849 bmap
= isl_basic_map_simplify(bmap
);
5850 bmap
= isl_basic_map_finalize(bmap
);
5853 isl_basic_map_free(like
);
5854 isl_basic_set_free(bset
);
5858 __isl_give isl_basic_set
*isl_basic_set_from_underlying_set(
5859 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*like
)
5861 return bset_from_bmap(isl_basic_map_overlying_set(bset
,
5862 bset_to_bmap(like
)));
5865 __isl_give isl_set
*isl_map_underlying_set(__isl_take isl_map
*map
)
5869 map
= isl_map_cow(map
);
5872 map
->dim
= isl_space_cow(map
->dim
);
5876 for (i
= 1; i
< map
->n
; ++i
)
5877 isl_assert(map
->ctx
, map
->p
[0]->n_div
== map
->p
[i
]->n_div
,
5879 for (i
= 0; i
< map
->n
; ++i
) {
5880 map
->p
[i
] = bset_to_bmap(
5881 isl_basic_map_underlying_set(map
->p
[i
]));
5886 map
->dim
= isl_space_underlying(map
->dim
, 0);
5888 isl_space_free(map
->dim
);
5889 map
->dim
= isl_space_copy(map
->p
[0]->dim
);
5893 return set_from_map(map
);
5899 /* Replace the space of "bmap" by "space".
5901 * If the space of "bmap" is identical to "space" (including the identifiers
5902 * of the input and output dimensions), then simply return the original input.
5904 __isl_give isl_basic_map
*isl_basic_map_reset_space(
5905 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*space
)
5908 isl_space
*bmap_space
;
5910 bmap_space
= isl_basic_map_peek_space(bmap
);
5911 equal
= isl_space_is_equal(bmap_space
, space
);
5912 if (equal
>= 0 && equal
)
5913 equal
= isl_space_has_equal_ids(bmap_space
, space
);
5917 isl_space_free(space
);
5920 isl_space_free(isl_basic_map_take_space(bmap
));
5921 bmap
= isl_basic_map_restore_space(bmap
, space
);
5923 bmap
= isl_basic_map_finalize(bmap
);
5927 isl_basic_map_free(bmap
);
5928 isl_space_free(space
);
5932 __isl_give isl_basic_set
*isl_basic_set_reset_space(
5933 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*space
)
5935 return bset_from_bmap(isl_basic_map_reset_space(bset_to_bmap(bset
),
5939 /* Check that the total dimensions of "map" and "space" are the same.
5941 static isl_stat
check_map_space_equal_total_dim(__isl_keep isl_map
*map
,
5942 __isl_keep isl_space
*space
)
5944 isl_size dim1
, dim2
;
5946 dim1
= isl_map_dim(map
, isl_dim_all
);
5947 dim2
= isl_space_dim(space
, isl_dim_all
);
5948 if (dim1
< 0 || dim2
< 0)
5949 return isl_stat_error
;
5952 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
5953 "total dimensions do not match", return isl_stat_error
);
5956 __isl_give isl_map
*isl_map_reset_space(__isl_take isl_map
*map
,
5957 __isl_take isl_space
*space
)
5961 map
= isl_map_cow(map
);
5965 for (i
= 0; i
< map
->n
; ++i
) {
5966 map
->p
[i
] = isl_basic_map_reset_space(map
->p
[i
],
5967 isl_space_copy(space
));
5971 isl_space_free(isl_map_take_space(map
));
5972 map
= isl_map_restore_space(map
, space
);
5977 isl_space_free(space
);
5981 /* Replace the space of "map" by "space", without modifying
5982 * the dimension of "map".
5984 * If the space of "map" is identical to "space" (including the identifiers
5985 * of the input and output dimensions), then simply return the original input.
5987 __isl_give isl_map
*isl_map_reset_equal_dim_space(__isl_take isl_map
*map
,
5988 __isl_take isl_space
*space
)
5991 isl_space
*map_space
;
5993 map_space
= isl_map_peek_space(map
);
5994 equal
= isl_space_is_equal(map_space
, space
);
5995 if (equal
>= 0 && equal
)
5996 equal
= isl_space_has_equal_ids(map_space
, space
);
6000 isl_space_free(space
);
6003 if (check_map_space_equal_total_dim(map
, space
) < 0)
6005 return isl_map_reset_space(map
, space
);
6008 isl_space_free(space
);
6012 __isl_give isl_set
*isl_set_reset_space(__isl_take isl_set
*set
,
6013 __isl_take isl_space
*space
)
6015 return set_from_map(isl_map_reset_space(set_to_map(set
), space
));
6018 /* Compute the parameter domain of the given basic set.
6020 __isl_give isl_basic_set
*isl_basic_set_params(__isl_take isl_basic_set
*bset
)
6026 is_params
= isl_basic_set_is_params(bset
);
6028 return isl_basic_set_free(bset
);
6032 n
= isl_basic_set_dim(bset
, isl_dim_set
);
6034 return isl_basic_set_free(bset
);
6035 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
6036 space
= isl_basic_set_get_space(bset
);
6037 space
= isl_space_params(space
);
6038 bset
= isl_basic_set_reset_space(bset
, space
);
6042 /* Construct a zero-dimensional basic set with the given parameter domain.
6044 __isl_give isl_basic_set
*isl_basic_set_from_params(
6045 __isl_take isl_basic_set
*bset
)
6048 space
= isl_basic_set_get_space(bset
);
6049 space
= isl_space_set_from_params(space
);
6050 bset
= isl_basic_set_reset_space(bset
, space
);
6054 /* Compute the parameter domain of the given set.
6056 __isl_give isl_set
*isl_set_params(__isl_take isl_set
*set
)
6058 return isl_map_params(set_to_map(set
));
6061 /* Construct a zero-dimensional set with the given parameter domain.
6063 __isl_give isl_set
*isl_set_from_params(__isl_take isl_set
*set
)
6066 space
= isl_set_get_space(set
);
6067 space
= isl_space_set_from_params(space
);
6068 set
= isl_set_reset_space(set
, space
);
6072 /* Compute the parameter domain of the given map.
6074 __isl_give isl_set
*isl_map_params(__isl_take isl_map
*map
)
6077 isl_size n_in
, n_out
;
6079 n_in
= isl_map_dim(map
, isl_dim_in
);
6080 n_out
= isl_map_dim(map
, isl_dim_out
);
6081 if (n_in
< 0 || n_out
< 0)
6082 return isl_map_free(map
);
6083 map
= isl_map_project_out(map
, isl_dim_in
, 0, n_in
);
6084 map
= isl_map_project_out(map
, isl_dim_out
, 0, n_out
);
6085 space
= isl_map_get_space(map
);
6086 space
= isl_space_params(space
);
6087 map
= isl_map_reset_space(map
, space
);
6091 __isl_give isl_basic_set
*isl_basic_map_domain(__isl_take isl_basic_map
*bmap
)
6096 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
6098 return isl_basic_map_free(bmap
);
6099 space
= isl_space_domain(isl_basic_map_get_space(bmap
));
6101 bmap
= isl_basic_map_project_out(bmap
, isl_dim_out
, 0, n_out
);
6103 return isl_basic_map_reset_space(bmap
, space
);
6106 isl_bool
isl_basic_map_may_be_set(__isl_keep isl_basic_map
*bmap
)
6109 return isl_bool_error
;
6110 return isl_space_may_be_set(bmap
->dim
);
6113 /* Is this basic map actually a set?
6114 * Users should never call this function. Outside of isl,
6115 * the type should indicate whether something is a set or a map.
6117 isl_bool
isl_basic_map_is_set(__isl_keep isl_basic_map
*bmap
)
6120 return isl_bool_error
;
6121 return isl_space_is_set(bmap
->dim
);
6124 __isl_give isl_basic_set
*isl_basic_map_range(__isl_take isl_basic_map
*bmap
)
6128 is_set
= isl_basic_map_is_set(bmap
);
6133 return isl_basic_map_domain(isl_basic_map_reverse(bmap
));
6135 isl_basic_map_free(bmap
);
6139 __isl_give isl_basic_map
*isl_basic_map_domain_map(
6140 __isl_take isl_basic_map
*bmap
)
6144 isl_basic_map
*domain
;
6145 isl_size nparam
, n_in
, n_out
;
6147 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
6148 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
6149 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
6150 if (nparam
< 0 || n_in
< 0 || n_out
< 0)
6151 return isl_basic_map_free(bmap
);
6153 space
= isl_basic_map_get_space(bmap
);
6154 space
= isl_space_from_range(isl_space_domain(space
));
6155 domain
= isl_basic_map_universe(space
);
6157 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
6158 bmap
= isl_basic_map_apply_range(bmap
, domain
);
6159 bmap
= isl_basic_map_extend_constraints(bmap
, n_in
, 0);
6161 for (i
= 0; i
< n_in
; ++i
)
6162 bmap
= isl_basic_map_equate(bmap
, isl_dim_in
, i
,
6165 bmap
= isl_basic_map_gauss(bmap
, NULL
);
6166 return isl_basic_map_finalize(bmap
);
6169 __isl_give isl_basic_map
*isl_basic_map_range_map(
6170 __isl_take isl_basic_map
*bmap
)
6174 isl_basic_map
*range
;
6175 isl_size nparam
, n_in
, n_out
;
6177 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
6178 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
6179 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
6180 if (nparam
< 0 || n_in
< 0 || n_out
< 0)
6181 return isl_basic_map_free(bmap
);
6183 space
= isl_basic_map_get_space(bmap
);
6184 space
= isl_space_from_range(isl_space_range(space
));
6185 range
= isl_basic_map_universe(space
);
6187 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
6188 bmap
= isl_basic_map_apply_range(bmap
, range
);
6189 bmap
= isl_basic_map_extend_constraints(bmap
, n_out
, 0);
6191 for (i
= 0; i
< n_out
; ++i
)
6192 bmap
= isl_basic_map_equate(bmap
, isl_dim_in
, n_in
+ i
,
6195 bmap
= isl_basic_map_gauss(bmap
, NULL
);
6196 return isl_basic_map_finalize(bmap
);
6199 int isl_map_may_be_set(__isl_keep isl_map
*map
)
6203 return isl_space_may_be_set(map
->dim
);
6206 /* Is this map actually a set?
6207 * Users should never call this function. Outside of isl,
6208 * the type should indicate whether something is a set or a map.
6210 isl_bool
isl_map_is_set(__isl_keep isl_map
*map
)
6213 return isl_bool_error
;
6214 return isl_space_is_set(map
->dim
);
6217 __isl_give isl_set
*isl_map_range(__isl_take isl_map
*map
)
6222 n_in
= isl_map_dim(map
, isl_dim_in
);
6224 return set_from_map(isl_map_free(map
));
6225 space
= isl_space_range(isl_map_get_space(map
));
6227 map
= isl_map_project_out(map
, isl_dim_in
, 0, n_in
);
6229 return set_from_map(isl_map_reset_space(map
, space
));
6232 /* Transform "map" by applying "fn_space" to its space and "fn_bmap"
6233 * to each of its basic maps.
6235 static __isl_give isl_map
*isl_map_transform(__isl_take isl_map
*map
,
6236 __isl_give isl_space
*(*fn_space
)(__isl_take isl_space
*space
),
6237 __isl_give isl_basic_map
*(*fn_bmap
)(__isl_take isl_basic_map
*bmap
))
6242 map
= isl_map_cow(map
);
6246 for (i
= 0; i
< map
->n
; ++i
) {
6247 map
->p
[i
] = fn_bmap(map
->p
[i
]);
6249 return isl_map_free(map
);
6251 map
= isl_map_unmark_normalized(map
);
6253 space
= isl_map_take_space(map
);
6254 space
= fn_space(space
);
6255 map
= isl_map_restore_space(map
, space
);
6260 __isl_give isl_map
*isl_map_domain_map(__isl_take isl_map
*map
)
6262 return isl_map_transform(map
, &isl_space_domain_map
,
6263 &isl_basic_map_domain_map
);
6266 __isl_give isl_map
*isl_map_range_map(__isl_take isl_map
*map
)
6268 return isl_map_transform(map
, &isl_space_range_map
,
6269 &isl_basic_map_range_map
);
6272 /* Given a wrapped map of the form A[B -> C],
6273 * return the map A[B -> C] -> B.
6275 __isl_give isl_map
*isl_set_wrapped_domain_map(__isl_take isl_set
*set
)
6282 if (!isl_set_has_tuple_id(set
))
6283 return isl_map_domain_map(isl_set_unwrap(set
));
6285 id
= isl_set_get_tuple_id(set
);
6286 map
= isl_map_domain_map(isl_set_unwrap(set
));
6287 map
= isl_map_set_tuple_id(map
, isl_dim_in
, id
);
6292 __isl_give isl_basic_map
*isl_basic_map_from_domain(
6293 __isl_take isl_basic_set
*bset
)
6295 return isl_basic_map_reverse(isl_basic_map_from_range(bset
));
6298 __isl_give isl_basic_map
*isl_basic_map_from_range(
6299 __isl_take isl_basic_set
*bset
)
6302 space
= isl_basic_set_get_space(bset
);
6303 space
= isl_space_from_range(space
);
6304 bset
= isl_basic_set_reset_space(bset
, space
);
6305 return bset_to_bmap(bset
);
6308 /* Create a relation with the given set as range.
6309 * The domain of the created relation is a zero-dimensional
6310 * flat anonymous space.
6312 __isl_give isl_map
*isl_map_from_range(__isl_take isl_set
*set
)
6315 space
= isl_set_get_space(set
);
6316 space
= isl_space_from_range(space
);
6317 set
= isl_set_reset_space(set
, space
);
6318 return set_to_map(set
);
6321 /* Create a relation with the given set as domain.
6322 * The range of the created relation is a zero-dimensional
6323 * flat anonymous space.
6325 __isl_give isl_map
*isl_map_from_domain(__isl_take isl_set
*set
)
6327 return isl_map_reverse(isl_map_from_range(set
));
6330 __isl_give isl_basic_map
*isl_basic_map_from_domain_and_range(
6331 __isl_take isl_basic_set
*domain
, __isl_take isl_basic_set
*range
)
6333 return isl_basic_map_apply_range(isl_basic_map_reverse(domain
), range
);
6336 __isl_give isl_map
*isl_map_from_domain_and_range(__isl_take isl_set
*domain
,
6337 __isl_take isl_set
*range
)
6339 return isl_map_apply_range(isl_map_reverse(domain
), range
);
6342 /* Return a newly allocated isl_map with given space and flags and
6343 * room for "n" basic maps.
6344 * Make sure that all cached information is cleared.
6346 __isl_give isl_map
*isl_map_alloc_space(__isl_take isl_space
*space
, int n
,
6349 struct isl_map
*map
;
6354 isl_die(space
->ctx
, isl_error_internal
,
6355 "negative number of basic maps", goto error
);
6356 map
= isl_calloc(space
->ctx
, struct isl_map
,
6357 sizeof(struct isl_map
) +
6358 (n
- 1) * sizeof(struct isl_basic_map
*));
6362 map
->ctx
= space
->ctx
;
6363 isl_ctx_ref(map
->ctx
);
6371 isl_space_free(space
);
6375 __isl_give isl_basic_map
*isl_basic_map_empty(__isl_take isl_space
*space
)
6377 struct isl_basic_map
*bmap
;
6378 bmap
= isl_basic_map_alloc_space(space
, 0, 1, 0);
6379 bmap
= isl_basic_map_set_to_empty(bmap
);
6383 __isl_give isl_basic_set
*isl_basic_set_empty(__isl_take isl_space
*space
)
6385 struct isl_basic_set
*bset
;
6386 bset
= isl_basic_set_alloc_space(space
, 0, 1, 0);
6387 bset
= isl_basic_set_set_to_empty(bset
);
6391 __isl_give isl_basic_map
*isl_basic_map_universe(__isl_take isl_space
*space
)
6393 struct isl_basic_map
*bmap
;
6394 bmap
= isl_basic_map_alloc_space(space
, 0, 0, 0);
6395 bmap
= isl_basic_map_finalize(bmap
);
6399 __isl_give isl_basic_set
*isl_basic_set_universe(__isl_take isl_space
*space
)
6401 struct isl_basic_set
*bset
;
6402 bset
= isl_basic_set_alloc_space(space
, 0, 0, 0);
6403 bset
= isl_basic_set_finalize(bset
);
6407 __isl_give isl_basic_map
*isl_basic_map_nat_universe(
6408 __isl_take isl_space
*space
)
6411 isl_size total
= isl_space_dim(space
, isl_dim_all
);
6412 isl_basic_map
*bmap
;
6415 space
= isl_space_free(space
);
6416 bmap
= isl_basic_map_alloc_space(space
, 0, 0, total
);
6417 for (i
= 0; i
< total
; ++i
) {
6418 int k
= isl_basic_map_alloc_inequality(bmap
);
6421 isl_seq_clr(bmap
->ineq
[k
], 1 + total
);
6422 isl_int_set_si(bmap
->ineq
[k
][1 + i
], 1);
6426 isl_basic_map_free(bmap
);
6430 __isl_give isl_basic_set
*isl_basic_set_nat_universe(
6431 __isl_take isl_space
*space
)
6433 return isl_basic_map_nat_universe(space
);
6436 __isl_give isl_map
*isl_map_nat_universe(__isl_take isl_space
*space
)
6438 return isl_map_from_basic_map(isl_basic_map_nat_universe(space
));
6441 __isl_give isl_set
*isl_set_nat_universe(__isl_take isl_space
*space
)
6443 return isl_map_nat_universe(space
);
6446 __isl_give isl_map
*isl_map_empty(__isl_take isl_space
*space
)
6448 return isl_map_alloc_space(space
, 0, ISL_MAP_DISJOINT
);
6451 __isl_give isl_set
*isl_set_empty(__isl_take isl_space
*space
)
6453 return isl_set_alloc_space(space
, 0, ISL_MAP_DISJOINT
);
6456 __isl_give isl_map
*isl_map_universe(__isl_take isl_space
*space
)
6458 struct isl_map
*map
;
6461 map
= isl_map_alloc_space(isl_space_copy(space
), 1, ISL_MAP_DISJOINT
);
6462 map
= isl_map_add_basic_map(map
, isl_basic_map_universe(space
));
6466 /* This function performs the same operation as isl_map_universe,
6467 * but is considered as a function on an isl_space when exported.
6469 __isl_give isl_map
*isl_space_universe_map(__isl_take isl_space
*space
)
6471 return isl_map_universe(space
);
6474 __isl_give isl_set
*isl_set_universe(__isl_take isl_space
*space
)
6476 struct isl_set
*set
;
6479 set
= isl_set_alloc_space(isl_space_copy(space
), 1, ISL_MAP_DISJOINT
);
6480 set
= isl_set_add_basic_set(set
, isl_basic_set_universe(space
));
6484 /* This function performs the same operation as isl_set_universe,
6485 * but is considered as a function on an isl_space when exported.
6487 __isl_give isl_set
*isl_space_universe_set(__isl_take isl_space
*space
)
6489 return isl_set_universe(space
);
6492 __isl_give isl_map
*isl_map_dup(__isl_keep isl_map
*map
)
6495 struct isl_map
*dup
;
6499 dup
= isl_map_alloc_space(isl_space_copy(map
->dim
), map
->n
, map
->flags
);
6500 for (i
= 0; i
< map
->n
; ++i
)
6501 dup
= isl_map_add_basic_map(dup
, isl_basic_map_copy(map
->p
[i
]));
6505 __isl_give isl_map
*isl_map_add_basic_map(__isl_take isl_map
*map
,
6506 __isl_take isl_basic_map
*bmap
)
6510 if (isl_basic_map_plain_is_empty(bmap
)) {
6511 isl_basic_map_free(bmap
);
6514 if (isl_map_basic_map_check_equal_space(map
, bmap
) < 0)
6516 isl_assert(map
->ctx
, map
->n
< map
->size
, goto error
);
6517 map
->p
[map
->n
] = bmap
;
6519 map
= isl_map_unmark_normalized(map
);
6525 isl_basic_map_free(bmap
);
6529 __isl_null isl_map
*isl_map_free(__isl_take isl_map
*map
)
6540 isl_ctx_deref(map
->ctx
);
6541 for (i
= 0; i
< map
->n
; ++i
)
6542 isl_basic_map_free(map
->p
[i
]);
6543 isl_space_free(map
->dim
);
6549 static __isl_give isl_basic_map
*isl_basic_map_fix_pos_si(
6550 __isl_take isl_basic_map
*bmap
, unsigned pos
, int value
)
6555 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
6557 return isl_basic_map_free(bmap
);
6559 bmap
= isl_basic_map_cow(bmap
);
6560 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
6561 j
= isl_basic_map_alloc_equality(bmap
);
6564 isl_seq_clr(bmap
->eq
[j
] + 1, total
);
6565 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
6566 isl_int_set_si(bmap
->eq
[j
][0], value
);
6567 bmap
= isl_basic_map_simplify(bmap
);
6568 return isl_basic_map_finalize(bmap
);
6570 isl_basic_map_free(bmap
);
6574 static __isl_give isl_basic_map
*isl_basic_map_fix_pos(
6575 __isl_take isl_basic_map
*bmap
, unsigned pos
, isl_int value
)
6580 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
6582 return isl_basic_map_free(bmap
);
6584 bmap
= isl_basic_map_cow(bmap
);
6585 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
6586 j
= isl_basic_map_alloc_equality(bmap
);
6589 isl_seq_clr(bmap
->eq
[j
] + 1, total
);
6590 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
6591 isl_int_set(bmap
->eq
[j
][0], value
);
6592 bmap
= isl_basic_map_simplify(bmap
);
6593 return isl_basic_map_finalize(bmap
);
6595 isl_basic_map_free(bmap
);
6599 __isl_give isl_basic_map
*isl_basic_map_fix_si(__isl_take isl_basic_map
*bmap
,
6600 enum isl_dim_type type
, unsigned pos
, int value
)
6602 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6603 return isl_basic_map_free(bmap
);
6604 return isl_basic_map_fix_pos_si(bmap
,
6605 isl_basic_map_offset(bmap
, type
) + pos
, value
);
6608 __isl_give isl_basic_map
*isl_basic_map_fix(__isl_take isl_basic_map
*bmap
,
6609 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6611 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6612 return isl_basic_map_free(bmap
);
6613 return isl_basic_map_fix_pos(bmap
,
6614 isl_basic_map_offset(bmap
, type
) + pos
, value
);
6617 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
6618 * to be equal to "v".
6620 __isl_give isl_basic_map
*isl_basic_map_fix_val(__isl_take isl_basic_map
*bmap
,
6621 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6625 if (!isl_val_is_int(v
))
6626 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
6627 "expecting integer value", goto error
);
6628 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6630 pos
+= isl_basic_map_offset(bmap
, type
);
6631 bmap
= isl_basic_map_fix_pos(bmap
, pos
, v
->n
);
6635 isl_basic_map_free(bmap
);
6640 /* Fix the value of the variable at position "pos" of type "type" of "bset"
6641 * to be equal to "v".
6643 __isl_give isl_basic_set
*isl_basic_set_fix_val(__isl_take isl_basic_set
*bset
,
6644 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6646 return isl_basic_map_fix_val(bset
, type
, pos
, v
);
6649 __isl_give isl_basic_set
*isl_basic_set_fix_si(__isl_take isl_basic_set
*bset
,
6650 enum isl_dim_type type
, unsigned pos
, int value
)
6652 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset
),
6656 __isl_give isl_basic_set
*isl_basic_set_fix(__isl_take isl_basic_set
*bset
,
6657 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6659 return bset_from_bmap(isl_basic_map_fix(bset_to_bmap(bset
),
6663 /* Remove the basic map at position "i" from "map" if this basic map
6664 * is (obviously) empty.
6666 static __isl_give isl_map
*remove_if_empty(__isl_take isl_map
*map
, int i
)
6673 empty
= isl_basic_map_plain_is_empty(map
->p
[i
]);
6675 return isl_map_free(map
);
6679 isl_basic_map_free(map
->p
[i
]);
6682 map
->p
[i
] = map
->p
[map
->n
];
6683 map
= isl_map_unmark_normalized(map
);
6690 /* Perform "fn" on each basic map of "map", where we may not be holding
6691 * the only reference to "map".
6692 * In particular, "fn" should be a semantics preserving operation
6693 * that we want to apply to all copies of "map". We therefore need
6694 * to be careful not to modify "map" in a way that breaks "map"
6695 * in case anything goes wrong.
6697 __isl_give isl_map
*isl_map_inline_foreach_basic_map(__isl_take isl_map
*map
,
6698 __isl_give isl_basic_map
*(*fn
)(__isl_take isl_basic_map
*bmap
))
6700 struct isl_basic_map
*bmap
;
6706 for (i
= map
->n
- 1; i
>= 0; --i
) {
6707 bmap
= isl_basic_map_copy(map
->p
[i
]);
6711 isl_basic_map_free(map
->p
[i
]);
6713 map
= remove_if_empty(map
, i
);
6724 __isl_give isl_map
*isl_map_fix_si(__isl_take isl_map
*map
,
6725 enum isl_dim_type type
, unsigned pos
, int value
)
6729 map
= isl_map_cow(map
);
6730 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
6731 return isl_map_free(map
);
6732 for (i
= map
->n
- 1; i
>= 0; --i
) {
6733 map
->p
[i
] = isl_basic_map_fix_si(map
->p
[i
], type
, pos
, value
);
6734 map
= remove_if_empty(map
, i
);
6738 map
= isl_map_unmark_normalized(map
);
6742 __isl_give isl_set
*isl_set_fix_si(__isl_take isl_set
*set
,
6743 enum isl_dim_type type
, unsigned pos
, int value
)
6745 return set_from_map(isl_map_fix_si(set_to_map(set
), type
, pos
, value
));
6748 __isl_give isl_map
*isl_map_fix(__isl_take isl_map
*map
,
6749 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6753 map
= isl_map_cow(map
);
6754 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
6755 return isl_map_free(map
);
6756 for (i
= 0; i
< map
->n
; ++i
) {
6757 map
->p
[i
] = isl_basic_map_fix(map
->p
[i
], type
, pos
, value
);
6761 map
= isl_map_unmark_normalized(map
);
6768 __isl_give isl_set
*isl_set_fix(__isl_take isl_set
*set
,
6769 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6771 return set_from_map(isl_map_fix(set_to_map(set
), type
, pos
, value
));
6774 /* Fix the value of the variable at position "pos" of type "type" of "map"
6775 * to be equal to "v".
6777 __isl_give isl_map
*isl_map_fix_val(__isl_take isl_map
*map
,
6778 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6782 map
= isl_map_cow(map
);
6786 if (!isl_val_is_int(v
))
6787 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
6788 "expecting integer value", goto error
);
6789 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
6791 for (i
= map
->n
- 1; i
>= 0; --i
) {
6792 map
->p
[i
] = isl_basic_map_fix_val(map
->p
[i
], type
, pos
,
6794 map
= remove_if_empty(map
, i
);
6798 map
= isl_map_unmark_normalized(map
);
6807 /* Fix the value of the variable at position "pos" of type "type" of "set"
6808 * to be equal to "v".
6810 __isl_give isl_set
*isl_set_fix_val(__isl_take isl_set
*set
,
6811 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6813 return isl_map_fix_val(set
, type
, pos
, v
);
6816 __isl_give isl_map
*isl_map_fix_input_si(__isl_take isl_map
*map
,
6817 unsigned input
, int value
)
6819 return isl_map_fix_si(map
, isl_dim_in
, input
, value
);
6822 __isl_give isl_set
*isl_set_fix_dim_si(__isl_take isl_set
*set
, unsigned dim
,
6825 return set_from_map(isl_map_fix_si(set_to_map(set
),
6826 isl_dim_set
, dim
, value
));
6829 static __isl_give isl_basic_map
*basic_map_bound_si(
6830 __isl_take isl_basic_map
*bmap
,
6831 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
6836 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6837 return isl_basic_map_free(bmap
);
6838 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
6840 return isl_basic_map_free(bmap
);
6841 pos
+= isl_basic_map_offset(bmap
, type
);
6842 bmap
= isl_basic_map_cow(bmap
);
6843 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
6844 j
= isl_basic_map_alloc_inequality(bmap
);
6847 isl_seq_clr(bmap
->ineq
[j
], 1 + total
);
6849 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
6850 isl_int_set_si(bmap
->ineq
[j
][0], value
);
6852 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
6853 isl_int_set_si(bmap
->ineq
[j
][0], -value
);
6855 bmap
= isl_basic_map_simplify(bmap
);
6856 return isl_basic_map_finalize(bmap
);
6858 isl_basic_map_free(bmap
);
6862 __isl_give isl_basic_map
*isl_basic_map_lower_bound_si(
6863 __isl_take isl_basic_map
*bmap
,
6864 enum isl_dim_type type
, unsigned pos
, int value
)
6866 return basic_map_bound_si(bmap
, type
, pos
, value
, 0);
6869 /* Constrain the values of the given dimension to be no greater than "value".
6871 __isl_give isl_basic_map
*isl_basic_map_upper_bound_si(
6872 __isl_take isl_basic_map
*bmap
,
6873 enum isl_dim_type type
, unsigned pos
, int value
)
6875 return basic_map_bound_si(bmap
, type
, pos
, value
, 1);
6878 static __isl_give isl_map
*map_bound_si(__isl_take isl_map
*map
,
6879 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
6883 map
= isl_map_cow(map
);
6884 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
6885 return isl_map_free(map
);
6886 for (i
= 0; i
< map
->n
; ++i
) {
6887 map
->p
[i
] = basic_map_bound_si(map
->p
[i
],
6888 type
, pos
, value
, upper
);
6892 map
= isl_map_unmark_normalized(map
);
6899 __isl_give isl_map
*isl_map_lower_bound_si(__isl_take isl_map
*map
,
6900 enum isl_dim_type type
, unsigned pos
, int value
)
6902 return map_bound_si(map
, type
, pos
, value
, 0);
6905 __isl_give isl_map
*isl_map_upper_bound_si(__isl_take isl_map
*map
,
6906 enum isl_dim_type type
, unsigned pos
, int value
)
6908 return map_bound_si(map
, type
, pos
, value
, 1);
6911 __isl_give isl_set
*isl_set_lower_bound_si(__isl_take isl_set
*set
,
6912 enum isl_dim_type type
, unsigned pos
, int value
)
6914 return set_from_map(isl_map_lower_bound_si(set_to_map(set
),
6918 __isl_give isl_set
*isl_set_upper_bound_si(__isl_take isl_set
*set
,
6919 enum isl_dim_type type
, unsigned pos
, int value
)
6921 return isl_map_upper_bound_si(set
, type
, pos
, value
);
6924 /* Bound the given variable of "bmap" from below (or above is "upper"
6925 * is set) to "value".
6927 static __isl_give isl_basic_map
*basic_map_bound(
6928 __isl_take isl_basic_map
*bmap
,
6929 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
6934 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6935 return isl_basic_map_free(bmap
);
6936 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
6938 return isl_basic_map_free(bmap
);
6939 pos
+= isl_basic_map_offset(bmap
, type
);
6940 bmap
= isl_basic_map_cow(bmap
);
6941 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
6942 j
= isl_basic_map_alloc_inequality(bmap
);
6945 isl_seq_clr(bmap
->ineq
[j
], 1 + total
);
6947 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
6948 isl_int_set(bmap
->ineq
[j
][0], value
);
6950 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
6951 isl_int_neg(bmap
->ineq
[j
][0], value
);
6953 bmap
= isl_basic_map_simplify(bmap
);
6954 return isl_basic_map_finalize(bmap
);
6956 isl_basic_map_free(bmap
);
6960 /* Bound the given variable of "map" from below (or above is "upper"
6961 * is set) to "value".
6963 static __isl_give isl_map
*map_bound(__isl_take isl_map
*map
,
6964 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
6968 map
= isl_map_cow(map
);
6969 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
6970 return isl_map_free(map
);
6971 for (i
= map
->n
- 1; i
>= 0; --i
) {
6972 map
->p
[i
] = basic_map_bound(map
->p
[i
], type
, pos
, value
, upper
);
6973 map
= remove_if_empty(map
, i
);
6977 map
= isl_map_unmark_normalized(map
);
6981 __isl_give isl_map
*isl_map_lower_bound(__isl_take isl_map
*map
,
6982 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6984 return map_bound(map
, type
, pos
, value
, 0);
6987 __isl_give isl_map
*isl_map_upper_bound(__isl_take isl_map
*map
,
6988 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6990 return map_bound(map
, type
, pos
, value
, 1);
6993 __isl_give isl_set
*isl_set_lower_bound(__isl_take isl_set
*set
,
6994 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6996 return isl_map_lower_bound(set
, type
, pos
, value
);
6999 __isl_give isl_set
*isl_set_upper_bound(__isl_take isl_set
*set
,
7000 enum isl_dim_type type
, unsigned pos
, isl_int value
)
7002 return isl_map_upper_bound(set
, type
, pos
, value
);
7005 /* Force the values of the variable at position "pos" of type "type" of "map"
7006 * to be no smaller than "value".
7008 __isl_give isl_map
*isl_map_lower_bound_val(__isl_take isl_map
*map
,
7009 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
7013 if (!isl_val_is_int(value
))
7014 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
7015 "expecting integer value", goto error
);
7016 map
= isl_map_lower_bound(map
, type
, pos
, value
->n
);
7017 isl_val_free(value
);
7020 isl_val_free(value
);
7025 /* Force the values of the variable at position "pos" of type "type" of "set"
7026 * to be no smaller than "value".
7028 __isl_give isl_set
*isl_set_lower_bound_val(__isl_take isl_set
*set
,
7029 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
7033 map
= set_to_map(set
);
7034 return set_from_map(isl_map_lower_bound_val(map
, type
, pos
, value
));
7037 /* Force the values of the variable at position "pos" of type "type" of "map"
7038 * to be no greater than "value".
7040 __isl_give isl_map
*isl_map_upper_bound_val(__isl_take isl_map
*map
,
7041 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
7045 if (!isl_val_is_int(value
))
7046 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
7047 "expecting integer value", goto error
);
7048 map
= isl_map_upper_bound(map
, type
, pos
, value
->n
);
7049 isl_val_free(value
);
7052 isl_val_free(value
);
7057 /* Force the values of the variable at position "pos" of type "type" of "set"
7058 * to be no greater than "value".
7060 __isl_give isl_set
*isl_set_upper_bound_val(__isl_take isl_set
*set
,
7061 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
7065 map
= set_to_map(set
);
7066 return set_from_map(isl_map_upper_bound_val(map
, type
, pos
, value
));
7069 /* If "mv" has an explicit domain, then intersect the domain of "map"
7070 * with this explicit domain.
7072 * An isl_multi_val object never has an explicit domain,
7073 * so simply return "map".
7075 static __isl_give isl_map
*isl_map_intersect_multi_val_explicit_domain(
7076 __isl_take isl_map
*map
, __isl_keep isl_multi_val
*mv
)
7083 #include "isl_map_bound_templ.c"
7085 /* Apply "map_bound" to "set" with the corresponding value in "bound"
7086 * for each set dimension, by treating the set as a map.
7088 static __isl_give isl_set
*set_bound_multi_val(__isl_take isl_set
*set
,
7089 __isl_take isl_multi_val
*bound
,
7090 __isl_give isl_map
*map_bound(__isl_take isl_map
*map
,
7091 unsigned pos
, __isl_take isl_val
*value
))
7095 map
= set_to_map(set
);
7096 return set_from_map(map_bound_multi_val(map
, bound
, map_bound
));
7101 #include "isl_map_bound_templ.c"
7103 /* Apply "map_bound" to "set" with the corresponding value in "bound"
7104 * for each set dimension, by converting the set and the bound
7105 * to objects living in a map space.
7107 static __isl_give isl_set
*set_bound_multi_pw_aff(__isl_take isl_set
*set
,
7108 __isl_take isl_multi_pw_aff
*bound
,
7109 __isl_give isl_map
*set_bound(__isl_take isl_map
*map
,
7110 unsigned pos
, __isl_take TYPE
*value
))
7114 map
= isl_map_from_range(set
);
7115 bound
= isl_multi_pw_aff_from_range(bound
);
7116 map
= map_bound_multi_pw_aff(map
, bound
, set_bound
);
7117 return isl_map_range(map
);
7120 /* Wrapper around isl_map_lower_bound_val for use in map_bound_multi_val,
7121 * setting a bound on the given output dimension.
7123 static __isl_give isl_map
*map_lower_bound_val(__isl_take isl_map
*map
,
7124 unsigned pos
, __isl_take isl_val
*v
)
7126 return isl_map_lower_bound_val(map
, isl_dim_out
, pos
, v
);
7129 /* Force the values of the set dimensions of "set"
7130 * to be no smaller than the corresponding values in "lower".
7132 __isl_give isl_set
*isl_set_lower_bound_multi_val(__isl_take isl_set
*set
,
7133 __isl_take isl_multi_val
*lower
)
7135 return set_bound_multi_val(set
, lower
, &map_lower_bound_val
);
7138 /* Wrapper around isl_map_upper_bound_val for use in map_bound_multi_val,
7139 * setting a bound on the given output dimension.
7141 static __isl_give isl_map
*map_upper_bound_val(__isl_take isl_map
*map
,
7142 unsigned pos
, __isl_take isl_val
*v
)
7144 return isl_map_upper_bound_val(map
, isl_dim_out
, pos
, v
);
7147 /* Force the values of the set dimensions of "set"
7148 * to be no greater than the corresponding values in "upper".
7150 __isl_give isl_set
*isl_set_upper_bound_multi_val(__isl_take isl_set
*set
,
7151 __isl_take isl_multi_val
*upper
)
7153 return set_bound_multi_val(set
, upper
, &map_upper_bound_val
);
7156 /* Force the symbolic constant expression "bound"
7157 * to satisfy the relation "order" with respect to
7158 * the output variable at position "pos" of "map".
7160 * Create an affine expression representing the output variable
7161 * in terms of the range and
7162 * compare it using "order" to "bound" (defined on the domain).
7163 * The result is a relation between elements in domain and range that
7164 * can be intersected with "map".
7166 static __isl_give isl_map
*map_bound_pw_aff(__isl_take isl_map
*map
,
7167 unsigned pos
, __isl_take isl_pw_aff
*bound
,
7168 __isl_give isl_map
*(*order
)(__isl_take isl_pw_aff
*pa1
,
7169 __isl_take isl_pw_aff
*pa2
))
7172 isl_local_space
*ls
;
7175 space
= isl_space_range(isl_map_get_space(map
));
7176 ls
= isl_local_space_from_space(space
);
7177 var
= isl_pw_aff_var_on_domain(ls
, isl_dim_set
, pos
);
7178 map
= isl_map_intersect(map
, order(bound
, var
));
7182 /* Force the values of the output variable at position "pos" of "map"
7183 * to be no smaller than the symbolic constant expression "lower".
7185 static __isl_give isl_map
*map_lower_bound_pw_aff(__isl_take isl_map
*map
,
7186 unsigned pos
, __isl_take isl_pw_aff
*lower
)
7188 return map_bound_pw_aff(map
, pos
, lower
, &isl_pw_aff_le_map
);
7191 /* Force the values of the output variable at position "pos" of "map"
7192 * to be no greater than the symbolic constant expression "upper".
7194 static __isl_give isl_map
*map_upper_bound_pw_aff(__isl_take isl_map
*map
,
7195 unsigned pos
, __isl_take isl_pw_aff
*upper
)
7197 return map_bound_pw_aff(map
, pos
, upper
, &isl_pw_aff_ge_map
);
7200 /* Force the values of the set dimensions of "set"
7201 * to be no smaller than the corresponding constant symbolic expressions
7204 __isl_give isl_set
*isl_set_lower_bound_multi_pw_aff(__isl_take isl_set
*set
,
7205 __isl_take isl_multi_pw_aff
*lower
)
7207 return set_bound_multi_pw_aff(set
, lower
, &map_lower_bound_pw_aff
);
7210 /* Force the values of the set dimensions of "set"
7211 * to be no greater than the corresponding constant symbolic expressions
7214 __isl_give isl_set
*isl_set_upper_bound_multi_pw_aff(__isl_take isl_set
*set
,
7215 __isl_take isl_multi_pw_aff
*upper
)
7217 return set_bound_multi_pw_aff(set
, upper
, &map_upper_bound_pw_aff
);
7220 /* Force the values of the output dimensions of "map"
7221 * to be no smaller than the corresponding constant symbolic expressions
7224 __isl_give isl_map
*isl_map_lower_bound_multi_pw_aff(__isl_take isl_map
*map
,
7225 __isl_take isl_multi_pw_aff
*lower
)
7227 return map_bound_multi_pw_aff(map
, lower
, &map_lower_bound_pw_aff
);
7230 /* Force the values of the output dimensions of "map"
7231 * to be no greater than the corresponding constant symbolic expressions
7234 __isl_give isl_map
*isl_map_upper_bound_multi_pw_aff(__isl_take isl_map
*map
,
7235 __isl_take isl_multi_pw_aff
*upper
)
7237 return map_bound_multi_pw_aff(map
, upper
, &map_upper_bound_pw_aff
);
7240 /* Bound the given variable of "bset" from below (or above is "upper"
7241 * is set) to "value".
7243 static __isl_give isl_basic_set
*isl_basic_set_bound(
7244 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned pos
,
7245 isl_int value
, int upper
)
7247 return bset_from_bmap(basic_map_bound(bset_to_bmap(bset
),
7248 type
, pos
, value
, upper
));
7251 /* Bound the given variable of "bset" from below (or above is "upper"
7252 * is set) to "value".
7254 static __isl_give isl_basic_set
*isl_basic_set_bound_val(
7255 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned pos
,
7256 __isl_take isl_val
*value
, int upper
)
7260 if (!isl_val_is_int(value
))
7261 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
7262 "expecting integer value", goto error
);
7263 bset
= isl_basic_set_bound(bset
, type
, pos
, value
->n
, upper
);
7264 isl_val_free(value
);
7267 isl_val_free(value
);
7268 isl_basic_set_free(bset
);
7272 /* Bound the given variable of "bset" from below to "value".
7274 __isl_give isl_basic_set
*isl_basic_set_lower_bound_val(
7275 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned pos
,
7276 __isl_take isl_val
*value
)
7278 return isl_basic_set_bound_val(bset
, type
, pos
, value
, 0);
7281 /* Bound the given variable of "bset" from above to "value".
7283 __isl_give isl_basic_set
*isl_basic_set_upper_bound_val(
7284 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned pos
,
7285 __isl_take isl_val
*value
)
7287 return isl_basic_set_bound_val(bset
, type
, pos
, value
, 1);
7290 __isl_give isl_map
*isl_map_reverse(__isl_take isl_map
*map
)
7292 return isl_map_transform(map
, &isl_space_reverse
,
7293 &isl_basic_map_reverse
);
7296 /* Given a map (A -> B) -> C, return the corresponding map (B -> A) -> C.
7298 __isl_give isl_map
*isl_map_domain_reverse(__isl_take isl_map
*map
)
7300 return isl_map_transform(map
, &isl_space_domain_reverse
,
7301 &isl_basic_map_domain_reverse
);
7304 /* Given a map A -> (B -> C), return the corresponding map A -> (C -> B).
7306 __isl_give isl_map
*isl_map_range_reverse(__isl_take isl_map
*map
)
7308 return isl_map_transform(map
, &isl_space_range_reverse
,
7309 &isl_basic_map_range_reverse
);
7312 /* Given a set (A -> B), return the corresponding set (B -> A).
7314 __isl_give isl_set
*isl_set_wrapped_reverse(__isl_take isl_set
*set
)
7316 isl_map
*map
= set_to_map(set
);
7318 map
= isl_map_transform(map
, &isl_space_wrapped_reverse
,
7319 &isl_basic_map_set_reverse
);
7320 return set_from_map(map
);
7324 #define TYPE isl_pw_multi_aff
7326 #define SUFFIX _pw_multi_aff
7328 #define EMPTY isl_pw_multi_aff_empty
7330 #define ADD isl_pw_multi_aff_union_add
7331 #include "isl_map_lexopt_templ.c"
7333 /* Given a map "map", compute the lexicographically minimal
7334 * (or maximal) image element for each domain element in dom,
7335 * in the form of an isl_pw_multi_aff.
7336 * If "empty" is not NULL, then set *empty to those elements in dom that
7337 * do not have an image element.
7338 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
7339 * should be computed over the domain of "map". "empty" is also NULL
7342 * We first compute the lexicographically minimal or maximal element
7343 * in the first basic map. This results in a partial solution "res"
7344 * and a subset "todo" of dom that still need to be handled.
7345 * We then consider each of the remaining maps in "map" and successively
7346 * update both "res" and "todo".
7347 * If "empty" is NULL, then the todo sets are not needed and therefore
7348 * also not computed.
7350 static __isl_give isl_pw_multi_aff
*isl_map_partial_lexopt_aligned_pw_multi_aff(
7351 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
7352 __isl_give isl_set
**empty
, unsigned flags
)
7356 isl_pw_multi_aff
*res
;
7359 full
= ISL_FL_ISSET(flags
, ISL_OPT_FULL
);
7360 if (!map
|| (!full
&& !dom
))
7363 if (isl_map_plain_is_empty(map
)) {
7368 return isl_pw_multi_aff_from_map(map
);
7371 res
= basic_map_partial_lexopt_pw_multi_aff(
7372 isl_basic_map_copy(map
->p
[0]),
7373 isl_set_copy(dom
), empty
, flags
);
7377 for (i
= 1; i
< map
->n
; ++i
) {
7378 isl_pw_multi_aff
*res_i
;
7380 res_i
= basic_map_partial_lexopt_pw_multi_aff(
7381 isl_basic_map_copy(map
->p
[i
]),
7382 isl_set_copy(dom
), empty
, flags
);
7384 if (ISL_FL_ISSET(flags
, ISL_OPT_MAX
))
7385 res
= isl_pw_multi_aff_union_lexmax(res
, res_i
);
7387 res
= isl_pw_multi_aff_union_lexmin(res
, res_i
);
7390 todo
= isl_set_intersect(todo
, *empty
);
7409 #define TYPE isl_map
7413 #define EMPTY isl_map_empty
7415 #define ADD isl_map_union_disjoint
7416 #include "isl_map_lexopt_templ.c"
7418 /* Given a map "map", compute the lexicographically minimal
7419 * (or maximal) image element for each domain element in "dom",
7420 * in the form of an isl_map.
7421 * If "empty" is not NULL, then set *empty to those elements in "dom" that
7422 * do not have an image element.
7423 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
7424 * should be computed over the domain of "map". "empty" is also NULL
7427 * If the input consists of more than one disjunct, then first
7428 * compute the desired result in the form of an isl_pw_multi_aff and
7429 * then convert that into an isl_map.
7431 * This function used to have an explicit implementation in terms
7432 * of isl_maps, but it would continually intersect the domains of
7433 * partial results with the complement of the domain of the next
7434 * partial solution, potentially leading to an explosion in the number
7435 * of disjuncts if there are several disjuncts in the input.
7436 * An even earlier implementation of this function would look for
7437 * better results in the domain of the partial result and for extra
7438 * results in the complement of this domain, which would lead to
7439 * even more splintering.
7441 static __isl_give isl_map
*isl_map_partial_lexopt_aligned(
7442 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
7443 __isl_give isl_set
**empty
, unsigned flags
)
7446 struct isl_map
*res
;
7447 isl_pw_multi_aff
*pma
;
7449 full
= ISL_FL_ISSET(flags
, ISL_OPT_FULL
);
7450 if (!map
|| (!full
&& !dom
))
7453 if (isl_map_plain_is_empty(map
)) {
7462 res
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[0]),
7468 pma
= isl_map_partial_lexopt_aligned_pw_multi_aff(map
, dom
, empty
,
7470 return isl_map_from_pw_multi_aff_internal(pma
);
7479 __isl_give isl_map
*isl_map_partial_lexmax(
7480 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
7481 __isl_give isl_set
**empty
)
7483 return isl_map_partial_lexopt(map
, dom
, empty
, ISL_OPT_MAX
);
7486 __isl_give isl_map
*isl_map_partial_lexmin(
7487 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
7488 __isl_give isl_set
**empty
)
7490 return isl_map_partial_lexopt(map
, dom
, empty
, 0);
7493 __isl_give isl_set
*isl_set_partial_lexmin(
7494 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
7495 __isl_give isl_set
**empty
)
7497 return set_from_map(isl_map_partial_lexmin(set_to_map(set
),
7501 __isl_give isl_set
*isl_set_partial_lexmax(
7502 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
7503 __isl_give isl_set
**empty
)
7505 return set_from_map(isl_map_partial_lexmax(set_to_map(set
),
7509 /* Compute the lexicographic minimum (or maximum if "flags" includes
7510 * ISL_OPT_MAX) of "bset" over its parametric domain.
7512 __isl_give isl_set
*isl_basic_set_lexopt(__isl_take isl_basic_set
*bset
,
7515 return isl_basic_map_lexopt(bset
, flags
);
7518 __isl_give isl_map
*isl_basic_map_lexmax(__isl_take isl_basic_map
*bmap
)
7520 return isl_basic_map_lexopt(bmap
, ISL_OPT_MAX
);
7523 __isl_give isl_set
*isl_basic_set_lexmin(__isl_take isl_basic_set
*bset
)
7525 return set_from_map(isl_basic_map_lexmin(bset_to_bmap(bset
)));
7528 __isl_give isl_set
*isl_basic_set_lexmax(__isl_take isl_basic_set
*bset
)
7530 return set_from_map(isl_basic_map_lexmax(bset_to_bmap(bset
)));
7533 /* Compute the lexicographic minimum of "bset" over its parametric domain
7534 * for the purpose of quantifier elimination.
7535 * That is, find an explicit representation for all the existentially
7536 * quantified variables in "bset" by computing their lexicographic
7539 static __isl_give isl_set
*isl_basic_set_lexmin_compute_divs(
7540 __isl_take isl_basic_set
*bset
)
7542 return isl_basic_set_lexopt(bset
, ISL_OPT_QE
);
7545 /* Given a basic map with one output dimension, compute the minimum or
7546 * maximum of that dimension as an isl_pw_aff.
7548 * Compute the optimum as a lexicographic optimum over the single
7549 * output dimension and extract the single isl_pw_aff from the result.
7551 static __isl_give isl_pw_aff
*basic_map_dim_opt(__isl_keep isl_basic_map
*bmap
,
7554 isl_pw_multi_aff
*pma
;
7557 bmap
= isl_basic_map_copy(bmap
);
7558 pma
= isl_basic_map_lexopt_pw_multi_aff(bmap
, max
? ISL_OPT_MAX
: 0);
7559 pwaff
= isl_pw_multi_aff_get_pw_aff(pma
, 0);
7560 isl_pw_multi_aff_free(pma
);
7565 /* Compute the minimum or maximum of the given output dimension
7566 * as a function of the parameters and the input dimensions,
7567 * but independently of the other output dimensions.
7569 * We first project out the other output dimension and then compute
7570 * the "lexicographic" maximum in each basic map, combining the results
7571 * using isl_pw_aff_union_max.
7573 static __isl_give isl_pw_aff
*map_dim_opt(__isl_take isl_map
*map
, int pos
,
7580 n_out
= isl_map_dim(map
, isl_dim_out
);
7582 map
= isl_map_free(map
);
7583 map
= isl_map_project_out(map
, isl_dim_out
, pos
+ 1, n_out
- (pos
+ 1));
7584 map
= isl_map_project_out(map
, isl_dim_out
, 0, pos
);
7589 isl_space
*space
= isl_map_get_space(map
);
7591 return isl_pw_aff_empty(space
);
7594 pwaff
= basic_map_dim_opt(map
->p
[0], max
);
7595 for (i
= 1; i
< map
->n
; ++i
) {
7596 isl_pw_aff
*pwaff_i
;
7598 pwaff_i
= basic_map_dim_opt(map
->p
[i
], max
);
7599 pwaff
= isl_pw_aff_union_opt(pwaff
, pwaff_i
, max
);
7607 /* Compute the minimum of the given output dimension as a function of the
7608 * parameters and input dimensions, but independently of
7609 * the other output dimensions.
7611 __isl_give isl_pw_aff
*isl_map_dim_min(__isl_take isl_map
*map
, int pos
)
7613 return map_dim_opt(map
, pos
, 0);
7616 /* Compute the maximum of the given output dimension as a function of the
7617 * parameters and input dimensions, but independently of
7618 * the other output dimensions.
7620 __isl_give isl_pw_aff
*isl_map_dim_max(__isl_take isl_map
*map
, int pos
)
7622 return map_dim_opt(map
, pos
, 1);
7625 /* Compute the minimum or maximum of the given set dimension
7626 * as a function of the parameters,
7627 * but independently of the other set dimensions.
7629 static __isl_give isl_pw_aff
*set_dim_opt(__isl_take isl_set
*set
, int pos
,
7632 return map_dim_opt(set
, pos
, max
);
7635 /* Compute the maximum of the given set dimension as a function of the
7636 * parameters, but independently of the other set dimensions.
7638 __isl_give isl_pw_aff
*isl_set_dim_max(__isl_take isl_set
*set
, int pos
)
7640 return set_dim_opt(set
, pos
, 1);
7643 /* Compute the minimum of the given set dimension as a function of the
7644 * parameters, but independently of the other set dimensions.
7646 __isl_give isl_pw_aff
*isl_set_dim_min(__isl_take isl_set
*set
, int pos
)
7648 return set_dim_opt(set
, pos
, 0);
7651 /* Apply a preimage specified by "mat" on the parameters of "bset".
7652 * bset is assumed to have only parameters and divs.
7654 static __isl_give isl_basic_set
*basic_set_parameter_preimage(
7655 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*mat
)
7659 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
7660 if (nparam
< 0 || !mat
)
7663 bset
->dim
= isl_space_cow(bset
->dim
);
7667 isl_assert(bset
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
7669 bset
->dim
->nparam
= 0;
7670 bset
->dim
->n_out
= nparam
;
7671 bset
= isl_basic_set_preimage(bset
, mat
);
7673 bset
->dim
->nparam
= bset
->dim
->n_out
;
7674 bset
->dim
->n_out
= 0;
7679 isl_basic_set_free(bset
);
7683 /* Apply a preimage specified by "mat" on the parameters of "set".
7684 * set is assumed to have only parameters and divs.
7686 static __isl_give isl_set
*set_parameter_preimage(__isl_take isl_set
*set
,
7687 __isl_take isl_mat
*mat
)
7692 nparam
= isl_set_dim(set
, isl_dim_param
);
7693 if (nparam
< 0 || !mat
)
7696 if (mat
->n_row
!= 1 + nparam
)
7697 isl_die(isl_set_get_ctx(set
), isl_error_internal
,
7698 "unexpected number of rows", goto error
);
7700 space
= isl_set_get_space(set
);
7701 space
= isl_space_move_dims(space
, isl_dim_set
, 0,
7702 isl_dim_param
, 0, nparam
);
7703 set
= isl_set_reset_space(set
, space
);
7704 set
= isl_set_preimage(set
, mat
);
7705 nparam
= isl_set_dim(set
, isl_dim_out
);
7707 set
= isl_set_free(set
);
7708 space
= isl_set_get_space(set
);
7709 space
= isl_space_move_dims(space
, isl_dim_param
, 0,
7710 isl_dim_out
, 0, nparam
);
7711 set
= isl_set_reset_space(set
, space
);
7719 /* Intersect the basic set "bset" with the affine space specified by the
7720 * equalities in "eq".
7722 static __isl_give isl_basic_set
*basic_set_append_equalities(
7723 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*eq
)
7731 bset
= isl_basic_set_extend(bset
, 0, eq
->n_row
, 0);
7735 len
= isl_basic_set_offset(bset
, isl_dim_div
) + bset
->extra
;
7736 for (i
= 0; i
< eq
->n_row
; ++i
) {
7737 k
= isl_basic_set_alloc_equality(bset
);
7740 isl_seq_cpy(bset
->eq
[k
], eq
->row
[i
], eq
->n_col
);
7741 isl_seq_clr(bset
->eq
[k
] + eq
->n_col
, len
- eq
->n_col
);
7745 bset
= isl_basic_set_gauss(bset
, NULL
);
7746 bset
= isl_basic_set_finalize(bset
);
7751 isl_basic_set_free(bset
);
7755 /* Intersect the set "set" with the affine space specified by the
7756 * equalities in "eq".
7758 static __isl_give isl_set
*set_append_equalities(__isl_take isl_set
*set
,
7759 __isl_take isl_mat
*eq
)
7766 for (i
= 0; i
< set
->n
; ++i
) {
7767 set
->p
[i
] = basic_set_append_equalities(set
->p
[i
],
7780 /* Given a basic set "bset" that only involves parameters and existentially
7781 * quantified variables, return the index of the first equality
7782 * that only involves parameters. If there is no such equality then
7783 * return bset->n_eq.
7785 * This function assumes that isl_basic_set_gauss has been called on "bset".
7787 static int first_parameter_equality(__isl_keep isl_basic_set
*bset
)
7790 isl_size nparam
, n_div
;
7792 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
7793 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
7794 if (nparam
< 0 || n_div
< 0)
7797 for (i
= 0, j
= n_div
- 1; i
< bset
->n_eq
&& j
>= 0; --j
) {
7798 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ j
]))
7805 /* Compute an explicit representation for the existentially quantified
7806 * variables in "bset" by computing the "minimal value" of the set
7807 * variables. Since there are no set variables, the computation of
7808 * the minimal value essentially computes an explicit representation
7809 * of the non-empty part(s) of "bset".
7811 * The input only involves parameters and existentially quantified variables.
7812 * All equalities among parameters have been removed.
7814 * Since the existentially quantified variables in the result are in general
7815 * going to be different from those in the input, we first replace
7816 * them by the minimal number of variables based on their equalities.
7817 * This should simplify the parametric integer programming.
7819 static __isl_give isl_set
*base_compute_divs(__isl_take isl_basic_set
*bset
)
7821 isl_morph
*morph1
, *morph2
;
7827 if (bset
->n_eq
== 0)
7828 return isl_basic_set_lexmin_compute_divs(bset
);
7830 morph1
= isl_basic_set_parameter_compression(bset
);
7831 bset
= isl_morph_basic_set(isl_morph_copy(morph1
), bset
);
7832 bset
= isl_basic_set_lift(bset
);
7833 morph2
= isl_basic_set_variable_compression(bset
, isl_dim_set
);
7834 bset
= isl_morph_basic_set(morph2
, bset
);
7835 n
= isl_basic_set_dim(bset
, isl_dim_set
);
7837 bset
= isl_basic_set_free(bset
);
7838 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
7840 set
= isl_basic_set_lexmin_compute_divs(bset
);
7842 set
= isl_morph_set(isl_morph_inverse(morph1
), set
);
7847 /* Project the given basic set onto its parameter domain, possibly introducing
7848 * new, explicit, existential variables in the constraints.
7849 * The input has parameters and (possibly implicit) existential variables.
7850 * The output has the same parameters, but only
7851 * explicit existentially quantified variables.
7853 * The actual projection is performed by pip, but pip doesn't seem
7854 * to like equalities very much, so we first remove the equalities
7855 * among the parameters by performing a variable compression on
7856 * the parameters. Afterward, an inverse transformation is performed
7857 * and the equalities among the parameters are inserted back in.
7859 * The variable compression on the parameters may uncover additional
7860 * equalities that were only implicit before. We therefore check
7861 * if there are any new parameter equalities in the result and
7862 * if so recurse. The removal of parameter equalities is required
7863 * for the parameter compression performed by base_compute_divs.
7865 static __isl_give isl_set
*parameter_compute_divs(
7866 __isl_take isl_basic_set
*bset
)
7870 struct isl_mat
*T
, *T2
;
7871 struct isl_set
*set
;
7874 bset
= isl_basic_set_cow(bset
);
7878 if (bset
->n_eq
== 0)
7879 return base_compute_divs(bset
);
7881 bset
= isl_basic_set_gauss(bset
, NULL
);
7884 if (isl_basic_set_plain_is_empty(bset
))
7885 return isl_set_from_basic_set(bset
);
7887 i
= first_parameter_equality(bset
);
7888 if (i
== bset
->n_eq
)
7889 return base_compute_divs(bset
);
7891 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
7893 return isl_set_from_basic_set(isl_basic_set_free(bset
));
7894 eq
= isl_mat_sub_alloc6(bset
->ctx
, bset
->eq
, i
, bset
->n_eq
- i
,
7896 eq
= isl_mat_cow(eq
);
7897 T
= isl_mat_variable_compression(isl_mat_copy(eq
), &T2
);
7898 if (T
&& T
->n_col
== 0) {
7902 bset
= isl_basic_set_set_to_empty(bset
);
7903 return isl_set_from_basic_set(bset
);
7905 bset
= basic_set_parameter_preimage(bset
, T
);
7907 i
= first_parameter_equality(bset
);
7910 else if (i
== bset
->n_eq
)
7911 set
= base_compute_divs(bset
);
7913 set
= parameter_compute_divs(bset
);
7914 set
= set_parameter_preimage(set
, T2
);
7915 set
= set_append_equalities(set
, eq
);
7919 /* Insert the divs from "ls" before those of "bmap".
7921 * The number of columns is not changed, which means that the last
7922 * dimensions of "bmap" are being reintepreted as the divs from "ls".
7923 * The caller is responsible for removing the same number of dimensions
7924 * from the space of "bmap".
7926 static __isl_give isl_basic_map
*insert_divs_from_local_space(
7927 __isl_take isl_basic_map
*bmap
, __isl_keep isl_local_space
*ls
)
7933 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
7935 return isl_basic_map_free(bmap
);
7939 old_n_div
= bmap
->n_div
;
7940 bmap
= insert_div_rows(bmap
, n_div
);
7944 for (i
= 0; i
< n_div
; ++i
) {
7945 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], ls
->div
->n_col
);
7946 isl_seq_clr(bmap
->div
[i
] + ls
->div
->n_col
, old_n_div
);
7952 /* Replace the space of "bmap" by the space and divs of "ls".
7954 * If "ls" has any divs, then we simplify the result since we may
7955 * have discovered some additional equalities that could simplify
7956 * the div expressions.
7958 static __isl_give isl_basic_map
*basic_replace_space_by_local_space(
7959 __isl_take isl_basic_map
*bmap
, __isl_take isl_local_space
*ls
)
7963 bmap
= isl_basic_map_cow(bmap
);
7964 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
7965 if (!bmap
|| n_div
< 0)
7968 bmap
= insert_divs_from_local_space(bmap
, ls
);
7972 isl_space_free(bmap
->dim
);
7973 bmap
->dim
= isl_local_space_get_space(ls
);
7977 isl_local_space_free(ls
);
7979 bmap
= isl_basic_map_simplify(bmap
);
7980 bmap
= isl_basic_map_finalize(bmap
);
7983 isl_basic_map_free(bmap
);
7984 isl_local_space_free(ls
);
7988 /* Replace the space of "map" by the space and divs of "ls".
7990 static __isl_give isl_map
*replace_space_by_local_space(__isl_take isl_map
*map
,
7991 __isl_take isl_local_space
*ls
)
7995 map
= isl_map_cow(map
);
7999 for (i
= 0; i
< map
->n
; ++i
) {
8000 map
->p
[i
] = basic_replace_space_by_local_space(map
->p
[i
],
8001 isl_local_space_copy(ls
));
8005 isl_space_free(isl_map_take_space(map
));
8006 map
= isl_map_restore_space(map
, isl_local_space_get_space(ls
));
8008 isl_local_space_free(ls
);
8011 isl_local_space_free(ls
);
8016 /* Compute an explicit representation for the existentially
8017 * quantified variables for which do not know any explicit representation yet.
8019 * We first sort the existentially quantified variables so that the
8020 * existentially quantified variables for which we already have an explicit
8021 * representation are placed before those for which we do not.
8022 * The input dimensions, the output dimensions and the existentially
8023 * quantified variables for which we already have an explicit
8024 * representation are then turned into parameters.
8025 * compute_divs returns a map with the same parameters and
8026 * no input or output dimensions and the dimension specification
8027 * is reset to that of the input, including the existentially quantified
8028 * variables for which we already had an explicit representation.
8030 static __isl_give isl_map
*compute_divs(__isl_take isl_basic_map
*bmap
)
8032 struct isl_basic_set
*bset
;
8033 struct isl_set
*set
;
8034 struct isl_map
*map
;
8036 isl_local_space
*ls
;
8043 bmap
= isl_basic_map_sort_divs(bmap
);
8044 bmap
= isl_basic_map_cow(bmap
);
8048 n_known
= isl_basic_map_first_unknown_div(bmap
);
8049 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
8050 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
8051 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
8052 if (n_known
< 0 || nparam
< 0 || n_in
< 0 || n_out
< 0)
8053 return isl_map_from_basic_map(isl_basic_map_free(bmap
));
8055 space
= isl_space_set_alloc(bmap
->ctx
,
8056 nparam
+ n_in
+ n_out
+ n_known
, 0);
8060 ls
= isl_basic_map_get_local_space(bmap
);
8061 ls
= isl_local_space_drop_dims(ls
, isl_dim_div
,
8062 n_known
, bmap
->n_div
- n_known
);
8064 for (i
= n_known
; i
< bmap
->n_div
; ++i
)
8065 swap_div(bmap
, i
- n_known
, i
);
8066 bmap
->n_div
-= n_known
;
8067 bmap
->extra
-= n_known
;
8069 bmap
= isl_basic_map_reset_space(bmap
, space
);
8070 bset
= bset_from_bmap(bmap
);
8072 set
= parameter_compute_divs(bset
);
8073 map
= set_to_map(set
);
8074 map
= replace_space_by_local_space(map
, ls
);
8078 isl_basic_map_free(bmap
);
8082 /* Is the integer division at position "div" of "bmap" integral?
8083 * That is, does it have denominator 1?
8085 isl_bool
isl_basic_map_div_is_integral(__isl_keep isl_basic_map
*bmap
, int div
)
8087 if (isl_basic_map_check_range(bmap
, isl_dim_div
, div
, 1) < 0)
8088 return isl_bool_error
;
8089 return isl_int_is_one(bmap
->div
[div
][0]);
8092 /* Remove the explicit representation of local variable "div",
8095 __isl_give isl_basic_map
*isl_basic_map_mark_div_unknown(
8096 __isl_take isl_basic_map
*bmap
, int div
)
8100 unknown
= isl_basic_map_div_is_marked_unknown(bmap
, div
);
8102 return isl_basic_map_free(bmap
);
8106 bmap
= isl_basic_map_cow(bmap
);
8109 isl_int_set_si(bmap
->div
[div
][0], 0);
8113 /* Is local variable "div" of "bmap" marked as not having an explicit
8115 * Note that even if "div" is not marked in this way and therefore
8116 * has an explicit representation, this representation may still
8117 * depend (indirectly) on other local variables that do not
8118 * have an explicit representation.
8120 isl_bool
isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map
*bmap
,
8123 if (isl_basic_map_check_range(bmap
, isl_dim_div
, div
, 1) < 0)
8124 return isl_bool_error
;
8125 return isl_int_is_zero(bmap
->div
[div
][0]);
8128 /* Return the position of the first local variable that does not
8129 * have an explicit representation.
8130 * Return the total number of local variables if they all have
8131 * an explicit representation.
8132 * Return -1 on error.
8134 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map
*bmap
)
8141 for (i
= 0; i
< bmap
->n_div
; ++i
) {
8142 if (!isl_basic_map_div_is_known(bmap
, i
))
8148 /* Return the position of the first local variable that does not
8149 * have an explicit representation.
8150 * Return the total number of local variables if they all have
8151 * an explicit representation.
8152 * Return -1 on error.
8154 int isl_basic_set_first_unknown_div(__isl_keep isl_basic_set
*bset
)
8156 return isl_basic_map_first_unknown_div(bset
);
8159 /* Does "bmap" have an explicit representation for all local variables?
8161 isl_bool
isl_basic_map_divs_known(__isl_keep isl_basic_map
*bmap
)
8166 n
= isl_basic_map_dim(bmap
, isl_dim_div
);
8167 first
= isl_basic_map_first_unknown_div(bmap
);
8168 if (n
< 0 || first
< 0)
8169 return isl_bool_error
;
8173 /* Do all basic maps in "map" have an explicit representation
8174 * for all local variables?
8176 isl_bool
isl_map_divs_known(__isl_keep isl_map
*map
)
8181 return isl_bool_error
;
8183 for (i
= 0; i
< map
->n
; ++i
) {
8184 int known
= isl_basic_map_divs_known(map
->p
[i
]);
8189 return isl_bool_true
;
8192 /* If bmap contains any unknown divs, then compute explicit
8193 * expressions for them. However, this computation may be
8194 * quite expensive, so first try to remove divs that aren't
8197 __isl_give isl_map
*isl_basic_map_compute_divs(__isl_take isl_basic_map
*bmap
)
8200 struct isl_map
*map
;
8202 known
= isl_basic_map_divs_known(bmap
);
8206 return isl_map_from_basic_map(bmap
);
8208 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
8210 known
= isl_basic_map_divs_known(bmap
);
8214 return isl_map_from_basic_map(bmap
);
8216 map
= compute_divs(bmap
);
8219 isl_basic_map_free(bmap
);
8223 __isl_give isl_map
*isl_map_compute_divs(__isl_take isl_map
*map
)
8227 struct isl_map
*res
;
8234 known
= isl_map_divs_known(map
);
8242 res
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[0]));
8243 for (i
= 1 ; i
< map
->n
; ++i
) {
8245 r2
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[i
]));
8246 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
8247 res
= isl_map_union_disjoint(res
, r2
);
8249 res
= isl_map_union(res
, r2
);
8256 __isl_give isl_set
*isl_basic_set_compute_divs(__isl_take isl_basic_set
*bset
)
8258 return set_from_map(isl_basic_map_compute_divs(bset_to_bmap(bset
)));
8261 __isl_give isl_set
*isl_set_compute_divs(__isl_take isl_set
*set
)
8263 return set_from_map(isl_map_compute_divs(set_to_map(set
)));
8266 __isl_give isl_set
*isl_map_domain(__isl_take isl_map
*map
)
8271 n_out
= isl_map_dim(map
, isl_dim_out
);
8273 return set_from_map(isl_map_free(map
));
8274 space
= isl_space_domain(isl_map_get_space(map
));
8276 map
= isl_map_project_out(map
, isl_dim_out
, 0, n_out
);
8278 return set_from_map(isl_map_reset_space(map
, space
));
8281 /* Return the union of "map1" and "map2", where we assume for now that
8282 * "map1" and "map2" are disjoint. Note that the basic maps inside
8283 * "map1" or "map2" may not be disjoint from each other.
8284 * Also note that this function is also called from isl_map_union,
8285 * which takes care of handling the situation where "map1" and "map2"
8286 * may not be disjoint.
8288 * If one of the inputs is empty, we can simply return the other input.
8289 * Similarly, if one of the inputs is universal, then it is equal to the union.
8291 static __isl_give isl_map
*map_union_disjoint(__isl_take isl_map
*map1
,
8292 __isl_take isl_map
*map2
)
8296 struct isl_map
*map
= NULL
;
8299 if (isl_map_check_equal_space(map1
, map2
) < 0)
8311 is_universe
= isl_map_plain_is_universe(map1
);
8312 if (is_universe
< 0)
8319 is_universe
= isl_map_plain_is_universe(map2
);
8320 if (is_universe
< 0)
8327 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
8328 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
8329 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
8331 map
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
8332 map1
->n
+ map2
->n
, flags
);
8335 for (i
= 0; i
< map1
->n
; ++i
) {
8336 map
= isl_map_add_basic_map(map
,
8337 isl_basic_map_copy(map1
->p
[i
]));
8341 for (i
= 0; i
< map2
->n
; ++i
) {
8342 map
= isl_map_add_basic_map(map
,
8343 isl_basic_map_copy(map2
->p
[i
]));
8357 /* Return the union of "map1" and "map2", where "map1" and "map2" are
8358 * guaranteed to be disjoint by the caller.
8360 * Note that this functions is called from within isl_map_make_disjoint,
8361 * so we have to be careful not to touch the constraints of the inputs
8364 __isl_give isl_map
*isl_map_union_disjoint(__isl_take isl_map
*map1
,
8365 __isl_take isl_map
*map2
)
8367 isl_map_align_params_bin(&map1
, &map2
);
8368 return map_union_disjoint(map1
, map2
);
8371 /* Return the union of "map1" and "map2", where "map1" and "map2" may
8374 * We currently simply call map_union_disjoint, the internal operation
8375 * of which does not really depend on the inputs being disjoint.
8376 * If the result contains more than one basic map, then we clear
8377 * the disjoint flag since the result may contain basic maps from
8378 * both inputs and these are not guaranteed to be disjoint.
8380 * As a special case, if "map1" and "map2" are obviously equal,
8381 * then we simply return "map1".
8383 __isl_give isl_map
*isl_map_union(__isl_take isl_map
*map1
,
8384 __isl_take isl_map
*map2
)
8388 if (isl_map_align_params_bin(&map1
, &map2
) < 0)
8391 equal
= isl_map_plain_is_equal(map1
, map2
);
8399 map1
= map_union_disjoint(map1
, map2
);
8403 ISL_F_CLR(map1
, ISL_MAP_DISJOINT
);
8411 __isl_give isl_set
*isl_set_union_disjoint(
8412 __isl_take isl_set
*set1
, __isl_take isl_set
*set2
)
8414 return set_from_map(isl_map_union_disjoint(set_to_map(set1
),
8418 __isl_give isl_set
*isl_set_union(__isl_take isl_set
*set1
,
8419 __isl_take isl_set
*set2
)
8421 return set_from_map(isl_map_union(set_to_map(set1
), set_to_map(set2
)));
8424 /* Apply "fn" to pairs of elements from "map" and "set" and collect
8425 * the results in a map living in "space".
8427 * "map" and "set" are assumed to be compatible and non-NULL.
8429 static __isl_give isl_map
*map_intersect_set(__isl_take isl_map
*map
,
8430 __isl_take isl_space
*space
, __isl_take isl_set
*set
,
8431 __isl_give isl_basic_map
*fn(__isl_take isl_basic_map
*bmap
,
8432 __isl_take isl_basic_set
*bset
))
8435 struct isl_map
*result
;
8438 if (isl_set_plain_is_universe(set
)) {
8440 return isl_map_reset_equal_dim_space(map
, space
);
8443 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
) &&
8444 ISL_F_ISSET(set
, ISL_MAP_DISJOINT
))
8445 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
8447 result
= isl_map_alloc_space(space
, map
->n
* set
->n
, flags
);
8448 for (i
= 0; result
&& i
< map
->n
; ++i
)
8449 for (j
= 0; j
< set
->n
; ++j
) {
8450 result
= isl_map_add_basic_map(result
,
8451 fn(isl_basic_map_copy(map
->p
[i
]),
8452 isl_basic_set_copy(set
->p
[j
])));
8462 __isl_give isl_map
*isl_map_intersect_range(__isl_take isl_map
*map
,
8463 __isl_take isl_set
*set
)
8468 isl_map_align_params_set(&map
, &set
);
8469 ok
= isl_map_compatible_range(map
, set
);
8473 isl_die(set
->ctx
, isl_error_invalid
,
8474 "incompatible spaces", goto error
);
8476 space
= isl_map_get_space(map
);
8477 return map_intersect_set(map
, space
, set
,
8478 &isl_basic_map_intersect_range
);
8485 /* Intersect the domain of "map" with "set".
8487 * If the domain dimensions of "map" do not have any identifiers,
8488 * then copy them over from "set".
8490 __isl_give isl_map
*isl_map_intersect_domain(__isl_take isl_map
*map
,
8491 __isl_take isl_set
*set
)
8496 isl_map_align_params_set(&map
, &set
);
8497 ok
= isl_map_compatible_domain(map
, set
);
8501 isl_die(set
->ctx
, isl_error_invalid
,
8502 "incompatible spaces", goto error
);
8504 space
= isl_map_get_space(map
);
8505 space
= isl_space_copy_ids_if_unset(space
, isl_dim_in
,
8506 isl_set_peek_space(set
), isl_dim_set
);
8507 return map_intersect_set(map
, space
, set
,
8508 &isl_basic_map_intersect_domain
);
8518 #include "isl_copy_tuple_id_templ.c"
8520 /* Data structure that specifies how isl_map_intersect_factor
8523 * "preserve_type" is the tuple where the factor differs from
8524 * the input map and of which the identifiers needs
8525 * to be preserved explicitly.
8526 * "other_factor" is used to extract the space of the other factor
8527 * from the space of the product ("map").
8528 * "product" is used to combine the given factor and a universe map
8529 * in the space returned by "other_factor" to produce a map
8530 * that lives in the same space as the input map.
8532 struct isl_intersect_factor_control
{
8533 enum isl_dim_type preserve_type
;
8534 __isl_give isl_space
*(*other_factor
)(__isl_take isl_space
*space
);
8535 __isl_give isl_map
*(*product
)(__isl_take isl_map
*factor
,
8536 __isl_take isl_map
*other
);
8539 /* Given a map "map" in some product space and a map "factor"
8540 * living in some factor space, return the intersection.
8542 * After aligning the parameters,
8543 * the map "factor" is first extended to a map living in the same space
8544 * as "map" and then a regular intersection is computed.
8546 * Note that the extension is computed as a product, which is anonymous
8547 * by default. If "map" has an identifier on the corresponding tuple,
8548 * then this identifier needs to be set on the product
8549 * before the intersection is computed.
8551 static __isl_give isl_map
*isl_map_intersect_factor(
8552 __isl_take isl_map
*map
, __isl_take isl_map
*factor
,
8553 struct isl_intersect_factor_control
*control
)
8557 isl_map
*other
, *product
;
8559 equal
= isl_map_has_equal_params(map
, factor
);
8563 map
= isl_map_align_params(map
, isl_map_get_space(factor
));
8564 factor
= isl_map_align_params(factor
, isl_map_get_space(map
));
8567 space
= isl_map_get_space(map
);
8568 other
= isl_map_universe(control
->other_factor(space
));
8569 product
= control
->product(factor
, other
);
8571 space
= isl_map_peek_space(map
);
8572 product
= isl_map_copy_tuple_id(product
, control
->preserve_type
,
8573 space
, control
->preserve_type
);
8574 return map_intersect(map
, product
);
8577 isl_map_free(factor
);
8581 /* Return the domain product of "map2" and "map1".
8583 static __isl_give isl_map
*isl_map_reverse_domain_product(
8584 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
8586 return isl_map_domain_product(map2
, map1
);
8589 /* Return the range product of "map2" and "map1".
8591 static __isl_give isl_map
*isl_map_reverse_range_product(
8592 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
8594 return isl_map_range_product(map2
, map1
);
8597 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
8598 * in the space A -> C, return the intersection.
8600 __isl_give isl_map
*isl_map_intersect_domain_factor_domain(
8601 __isl_take isl_map
*map
, __isl_take isl_map
*factor
)
8603 struct isl_intersect_factor_control control
= {
8604 .preserve_type
= isl_dim_in
,
8605 .other_factor
= isl_space_domain_factor_range
,
8606 .product
= isl_map_domain_product
,
8609 return isl_map_intersect_factor(map
, factor
, &control
);
8612 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
8613 * in the space B -> C, return the intersection.
8615 __isl_give isl_map
*isl_map_intersect_domain_factor_range(
8616 __isl_take isl_map
*map
, __isl_take isl_map
*factor
)
8618 struct isl_intersect_factor_control control
= {
8619 .preserve_type
= isl_dim_in
,
8620 .other_factor
= isl_space_domain_factor_domain
,
8621 .product
= isl_map_reverse_domain_product
,
8624 return isl_map_intersect_factor(map
, factor
, &control
);
8627 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
8628 * in the space A -> B, return the intersection.
8630 __isl_give isl_map
*isl_map_intersect_range_factor_domain(
8631 __isl_take isl_map
*map
, __isl_take isl_map
*factor
)
8633 struct isl_intersect_factor_control control
= {
8634 .preserve_type
= isl_dim_out
,
8635 .other_factor
= isl_space_range_factor_range
,
8636 .product
= isl_map_range_product
,
8639 return isl_map_intersect_factor(map
, factor
, &control
);
8642 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
8643 * in the space A -> C, return the intersection.
8645 __isl_give isl_map
*isl_map_intersect_range_factor_range(
8646 __isl_take isl_map
*map
, __isl_take isl_map
*factor
)
8648 struct isl_intersect_factor_control control
= {
8649 .preserve_type
= isl_dim_out
,
8650 .other_factor
= isl_space_range_factor_domain
,
8651 .product
= isl_map_reverse_range_product
,
8654 return isl_map_intersect_factor(map
, factor
, &control
);
8657 /* Given a set "set" in a space [A -> B] and a set "domain"
8658 * in the space A, return the intersection.
8660 * The set "domain" is first extended to a set living in the space
8661 * [A -> B] and then a regular intersection is computed.
8663 __isl_give isl_set
*isl_set_intersect_factor_domain(__isl_take isl_set
*set
,
8664 __isl_take isl_set
*domain
)
8666 struct isl_intersect_factor_control control
= {
8667 .preserve_type
= isl_dim_set
,
8668 .other_factor
= isl_space_factor_range
,
8669 .product
= isl_map_range_product
,
8672 return set_from_map(isl_map_intersect_factor(set_to_map(set
),
8673 set_to_map(domain
), &control
));
8676 /* Given a set "set" in a space [A -> B] and a set "range"
8677 * in the space B, return the intersection.
8679 * The set "range" is first extended to a set living in the space
8680 * [A -> B] and then a regular intersection is computed.
8682 __isl_give isl_set
*isl_set_intersect_factor_range(__isl_take isl_set
*set
,
8683 __isl_take isl_set
*range
)
8685 struct isl_intersect_factor_control control
= {
8686 .preserve_type
= isl_dim_set
,
8687 .other_factor
= isl_space_factor_domain
,
8688 .product
= isl_map_reverse_range_product
,
8691 return set_from_map(isl_map_intersect_factor(set_to_map(set
),
8692 set_to_map(range
), &control
));
8698 #include "isl_copy_tuple_id_templ.c"
8700 /* Given a map "map" in a space [A -> B] -> C and a set "domain"
8701 * in the space A, return the intersection.
8703 * The set "domain" is extended to a set living in the space [A -> B] and
8704 * the domain of "map" is intersected with this set.
8706 * If "map" has an identifier on the domain tuple,
8707 * then this identifier needs to be set on this product
8708 * before the intersection is computed.
8710 __isl_give isl_map
*isl_map_intersect_domain_wrapped_domain(
8711 __isl_take isl_map
*map
, __isl_take isl_set
*domain
)
8716 isl_map_align_params_set(&map
, &domain
);
8717 space
= isl_map_get_space(map
);
8718 space
= isl_space_domain_wrapped_range(space
);
8719 factor
= isl_set_universe(space
);
8720 domain
= isl_set_product(domain
, factor
);
8721 space
= isl_map_peek_space(map
);
8722 domain
= isl_set_copy_tuple_id(domain
, isl_dim_set
, space
, isl_dim_in
);
8723 return isl_map_intersect_domain(map
, domain
);
8726 /* Given a map "map" in a space A -> [B -> C] and a set "domain"
8727 * in the space B, return the intersection.
8729 * The set "domain" is extended to a set living in the space [B -> C] and
8730 * the range of "map" is intersected with this set.
8732 * If "map" has an identifier on the range tuple,
8733 * then this identifier needs to be set on this product
8734 * before the intersection is computed.
8736 __isl_give isl_map
*isl_map_intersect_range_wrapped_domain(
8737 __isl_take isl_map
*map
, __isl_take isl_set
*domain
)
8742 isl_map_align_params_set(&map
, &domain
);
8743 space
= isl_map_get_space(map
);
8744 space
= isl_space_range_wrapped_range(space
);
8745 factor
= isl_set_universe(space
);
8746 domain
= isl_set_product(domain
, factor
);
8747 space
= isl_map_peek_space(map
);
8748 domain
= isl_set_copy_tuple_id(domain
, isl_dim_set
, space
, isl_dim_out
);
8749 return isl_map_intersect_range(map
, domain
);
8752 __isl_give isl_map
*isl_map_apply_domain(__isl_take isl_map
*map1
,
8753 __isl_take isl_map
*map2
)
8755 if (isl_map_align_params_bin(&map1
, &map2
) < 0)
8757 map1
= isl_map_reverse(map1
);
8758 map1
= isl_map_apply_range(map1
, map2
);
8759 return isl_map_reverse(map1
);
8766 __isl_give isl_map
*isl_map_apply_range(__isl_take isl_map
*map1
,
8767 __isl_take isl_map
*map2
)
8770 struct isl_map
*result
;
8773 if (isl_map_align_params_bin(&map1
, &map2
) < 0)
8776 space
= isl_space_join(isl_space_copy(map1
->dim
),
8777 isl_space_copy(map2
->dim
));
8779 result
= isl_map_alloc_space(space
, map1
->n
* map2
->n
, 0);
8782 for (i
= 0; i
< map1
->n
; ++i
)
8783 for (j
= 0; j
< map2
->n
; ++j
) {
8784 result
= isl_map_add_basic_map(result
,
8785 isl_basic_map_apply_range(
8786 isl_basic_map_copy(map1
->p
[i
]),
8787 isl_basic_map_copy(map2
->p
[j
])));
8793 if (result
&& result
->n
<= 1)
8794 ISL_F_SET(result
, ISL_MAP_DISJOINT
);
8802 /* Is "bmap" a transformation, i.e.,
8803 * does it relate elements from the same space.
8805 isl_bool
isl_basic_map_is_transformation(__isl_keep isl_basic_map
*bmap
)
8809 space
= isl_basic_map_peek_space(bmap
);
8810 return isl_space_tuple_is_equal(space
, isl_dim_in
, space
, isl_dim_out
);
8813 /* Check that "bmap" is a transformation, i.e.,
8814 * that it relates elements from the same space.
8816 static isl_stat
isl_basic_map_check_transformation(
8817 __isl_keep isl_basic_map
*bmap
)
8821 equal
= isl_basic_map_is_transformation(bmap
);
8823 return isl_stat_error
;
8825 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
8826 "domain and range don't match", return isl_stat_error
);
8831 * returns range - domain
8833 __isl_give isl_basic_set
*isl_basic_map_deltas(__isl_take isl_basic_map
*bmap
)
8835 isl_space
*target_space
;
8836 struct isl_basic_set
*bset
;
8842 if (isl_basic_map_check_transformation(bmap
) < 0)
8843 return isl_basic_map_free(bmap
);
8844 dim
= isl_basic_map_dim(bmap
, isl_dim_in
);
8845 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
8846 if (dim
< 0 || nparam
< 0)
8848 target_space
= isl_space_domain(isl_basic_map_get_space(bmap
));
8849 bmap
= isl_basic_map_from_range(isl_basic_map_wrap(bmap
));
8850 bmap
= isl_basic_map_add_dims(bmap
, isl_dim_in
, dim
);
8851 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
8853 bmap
= isl_basic_map_free(bmap
);
8854 bmap
= isl_basic_map_extend_constraints(bmap
, dim
, 0);
8855 for (i
= 0; i
< dim
; ++i
) {
8856 int j
= isl_basic_map_alloc_equality(bmap
);
8858 bmap
= isl_basic_map_free(bmap
);
8861 isl_seq_clr(bmap
->eq
[j
], 1 + total
);
8862 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
8863 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], 1);
8864 isl_int_set_si(bmap
->eq
[j
][1+nparam
+2*dim
+i
], -1);
8866 bset
= isl_basic_map_domain(bmap
);
8867 bset
= isl_basic_set_reset_space(bset
, target_space
);
8870 isl_basic_map_free(bmap
);
8874 /* Is the tuple of type "type1" of "map" the same as
8875 * the tuple of type "type2" of "space"?
8877 isl_bool
isl_map_space_tuple_is_equal(__isl_keep isl_map
*map
,
8878 enum isl_dim_type type1
, __isl_keep isl_space
*space
,
8879 enum isl_dim_type type2
)
8881 isl_space
*map_space
;
8883 map_space
= isl_map_peek_space(map
);
8884 return isl_space_tuple_is_equal(map_space
, type1
, space
, type2
);
8887 /* Is the tuple of type "type1" of "map1" the same as
8888 * the tuple of type "type2" of "map2"?
8890 isl_bool
isl_map_tuple_is_equal(__isl_keep isl_map
*map1
,
8891 enum isl_dim_type type1
, __isl_keep isl_map
*map2
,
8892 enum isl_dim_type type2
)
8894 isl_space
*space1
, *space2
;
8896 space1
= isl_map_peek_space(map1
);
8897 space2
= isl_map_peek_space(map2
);
8898 return isl_space_tuple_is_equal(space1
, type1
, space2
, type2
);
8901 /* Is the space of "obj" equal to "space", ignoring parameters?
8903 isl_bool
isl_map_has_space_tuples(__isl_keep isl_map
*map
,
8904 __isl_keep isl_space
*space
)
8906 isl_space
*map_space
;
8908 map_space
= isl_map_peek_space(map
);
8909 return isl_space_has_equal_tuples(map_space
, space
);
8912 /* Check that "map" is a transformation, i.e.,
8913 * that it relates elements from the same space.
8915 isl_stat
isl_map_check_transformation(__isl_keep isl_map
*map
)
8919 equal
= isl_map_tuple_is_equal(map
, isl_dim_in
, map
, isl_dim_out
);
8921 return isl_stat_error
;
8923 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
8924 "domain and range don't match", return isl_stat_error
);
8929 * returns range - domain
8931 __isl_give isl_set
*isl_map_deltas(__isl_take isl_map
*map
)
8935 struct isl_set
*result
;
8937 if (isl_map_check_transformation(map
) < 0)
8939 space
= isl_map_get_space(map
);
8940 space
= isl_space_domain(space
);
8941 result
= isl_set_alloc_space(space
, map
->n
, 0);
8944 for (i
= 0; i
< map
->n
; ++i
)
8945 result
= isl_set_add_basic_set(result
,
8946 isl_basic_map_deltas(isl_basic_map_copy(map
->p
[i
])));
8955 * returns [domain -> range] -> range - domain
8957 __isl_give isl_basic_map
*isl_basic_map_deltas_map(
8958 __isl_take isl_basic_map
*bmap
)
8962 isl_basic_map
*domain
;
8966 if (isl_basic_map_check_transformation(bmap
) < 0)
8967 return isl_basic_map_free(bmap
);
8969 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
8970 n
= isl_basic_map_dim(bmap
, isl_dim_in
);
8971 if (nparam
< 0 || n
< 0)
8972 return isl_basic_map_free(bmap
);
8974 space
= isl_basic_map_get_space(bmap
);
8975 space
= isl_space_from_range(isl_space_domain(space
));
8976 domain
= isl_basic_map_universe(space
);
8978 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
8979 bmap
= isl_basic_map_apply_range(bmap
, domain
);
8980 bmap
= isl_basic_map_extend_constraints(bmap
, n
, 0);
8982 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
8984 return isl_basic_map_free(bmap
);
8986 for (i
= 0; i
< n
; ++i
) {
8987 k
= isl_basic_map_alloc_equality(bmap
);
8990 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
8991 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], 1);
8992 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ i
], -1);
8993 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ n
+ i
], 1);
8996 bmap
= isl_basic_map_gauss(bmap
, NULL
);
8997 return isl_basic_map_finalize(bmap
);
8999 isl_basic_map_free(bmap
);
9004 * returns [domain -> range] -> range - domain
9006 __isl_give isl_map
*isl_map_deltas_map(__isl_take isl_map
*map
)
9008 if (isl_map_check_transformation(map
) < 0)
9009 return isl_map_free(map
);
9011 return isl_map_transform(map
, &isl_space_range_map
,
9012 &isl_basic_map_deltas_map
);
9015 /* Return pairs of elements { x -> y } such that y - x is in "deltas".
9017 __isl_give isl_map
*isl_set_translation(__isl_take isl_set
*deltas
)
9022 space
= isl_space_map_from_set(isl_set_get_space(deltas
));
9023 map
= isl_map_deltas_map(isl_map_universe(space
));
9024 map
= isl_map_intersect_range(map
, deltas
);
9026 return isl_set_unwrap(isl_map_domain(map
));
9029 __isl_give isl_basic_map
*isl_basic_map_identity(__isl_take isl_space
*space
)
9031 isl_size n_in
, n_out
;
9033 n_in
= isl_space_dim(space
, isl_dim_in
);
9034 n_out
= isl_space_dim(space
, isl_dim_out
);
9035 if (n_in
< 0 || n_out
< 0)
9038 isl_die(space
->ctx
, isl_error_invalid
,
9039 "number of input and output dimensions needs to be "
9040 "the same", goto error
);
9041 return isl_basic_map_equal(space
, n_in
);
9043 isl_space_free(space
);
9047 __isl_give isl_map
*isl_map_identity(__isl_take isl_space
*space
)
9049 return isl_map_from_basic_map(isl_basic_map_identity(space
));
9052 __isl_give isl_map
*isl_set_identity(__isl_take isl_set
*set
)
9054 isl_space
*space
= isl_set_get_space(set
);
9056 id
= isl_map_identity(isl_space_map_from_set(space
));
9057 return isl_map_intersect_range(id
, set
);
9060 /* Construct a basic set with all set dimensions having only non-negative
9063 __isl_give isl_basic_set
*isl_basic_set_positive_orthant(
9064 __isl_take isl_space
*space
)
9070 struct isl_basic_set
*bset
;
9072 nparam
= isl_space_dim(space
, isl_dim_param
);
9073 dim
= isl_space_dim(space
, isl_dim_set
);
9074 total
= isl_space_dim(space
, isl_dim_all
);
9075 if (nparam
< 0 || dim
< 0 || total
< 0)
9076 space
= isl_space_free(space
);
9077 bset
= isl_basic_set_alloc_space(space
, 0, 0, dim
);
9080 for (i
= 0; i
< dim
; ++i
) {
9081 int k
= isl_basic_set_alloc_inequality(bset
);
9084 isl_seq_clr(bset
->ineq
[k
], 1 + total
);
9085 isl_int_set_si(bset
->ineq
[k
][1 + nparam
+ i
], 1);
9089 isl_basic_set_free(bset
);
9093 /* Construct the half-space x_pos >= 0.
9095 static __isl_give isl_basic_set
*nonneg_halfspace(__isl_take isl_space
*space
,
9100 isl_basic_set
*nonneg
;
9102 total
= isl_space_dim(space
, isl_dim_all
);
9104 space
= isl_space_free(space
);
9105 nonneg
= isl_basic_set_alloc_space(space
, 0, 0, 1);
9106 k
= isl_basic_set_alloc_inequality(nonneg
);
9109 isl_seq_clr(nonneg
->ineq
[k
], 1 + total
);
9110 isl_int_set_si(nonneg
->ineq
[k
][pos
], 1);
9112 return isl_basic_set_finalize(nonneg
);
9114 isl_basic_set_free(nonneg
);
9118 /* Construct the half-space x_pos <= -1.
9120 static __isl_give isl_basic_set
*neg_halfspace(__isl_take isl_space
*space
,
9127 total
= isl_space_dim(space
, isl_dim_all
);
9129 space
= isl_space_free(space
);
9130 neg
= isl_basic_set_alloc_space(space
, 0, 0, 1);
9131 k
= isl_basic_set_alloc_inequality(neg
);
9134 isl_seq_clr(neg
->ineq
[k
], 1 + total
);
9135 isl_int_set_si(neg
->ineq
[k
][0], -1);
9136 isl_int_set_si(neg
->ineq
[k
][pos
], -1);
9138 return isl_basic_set_finalize(neg
);
9140 isl_basic_set_free(neg
);
9144 __isl_give isl_set
*isl_set_split_dims(__isl_take isl_set
*set
,
9145 enum isl_dim_type type
, unsigned first
, unsigned n
)
9149 isl_basic_set
*nonneg
;
9155 if (isl_set_check_range(set
, type
, first
, n
) < 0)
9156 return isl_set_free(set
);
9158 offset
= pos(set
->dim
, type
);
9159 for (i
= 0; i
< n
; ++i
) {
9160 nonneg
= nonneg_halfspace(isl_set_get_space(set
),
9161 offset
+ first
+ i
);
9162 neg
= neg_halfspace(isl_set_get_space(set
), offset
+ first
+ i
);
9164 set
= isl_set_intersect(set
, isl_basic_set_union(nonneg
, neg
));
9170 static isl_stat
foreach_orthant(__isl_take isl_set
*set
, int *signs
, int first
,
9172 isl_stat (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
9178 return isl_stat_error
;
9179 if (isl_set_plain_is_empty(set
)) {
9184 return fn(set
, signs
, user
);
9187 half
= isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set
),
9189 half
= isl_set_intersect(half
, isl_set_copy(set
));
9190 if (foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
) < 0)
9194 half
= isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set
),
9196 half
= isl_set_intersect(half
, set
);
9197 return foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
);
9200 return isl_stat_error
;
9203 /* Call "fn" on the intersections of "set" with each of the orthants
9204 * (except for obviously empty intersections). The orthant is identified
9205 * by the signs array, with each entry having value 1 or -1 according
9206 * to the sign of the corresponding variable.
9208 isl_stat
isl_set_foreach_orthant(__isl_keep isl_set
*set
,
9209 isl_stat (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
9218 return isl_stat_error
;
9219 if (isl_set_plain_is_empty(set
))
9222 nparam
= isl_set_dim(set
, isl_dim_param
);
9223 nvar
= isl_set_dim(set
, isl_dim_set
);
9224 if (nparam
< 0 || nvar
< 0)
9225 return isl_stat_error
;
9227 signs
= isl_alloc_array(set
->ctx
, int, nparam
+ nvar
);
9229 r
= foreach_orthant(isl_set_copy(set
), signs
, 0, nparam
+ nvar
,
9237 isl_bool
isl_set_is_equal(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
9239 return isl_map_is_equal(set_to_map(set1
), set_to_map(set2
));
9242 isl_bool
isl_basic_map_is_subset(__isl_keep isl_basic_map
*bmap1
,
9243 __isl_keep isl_basic_map
*bmap2
)
9246 struct isl_map
*map1
;
9247 struct isl_map
*map2
;
9249 if (!bmap1
|| !bmap2
)
9250 return isl_bool_error
;
9252 map1
= isl_map_from_basic_map(isl_basic_map_copy(bmap1
));
9253 map2
= isl_map_from_basic_map(isl_basic_map_copy(bmap2
));
9255 is_subset
= isl_map_is_subset(map1
, map2
);
9263 isl_bool
isl_basic_set_is_subset(__isl_keep isl_basic_set
*bset1
,
9264 __isl_keep isl_basic_set
*bset2
)
9266 return isl_basic_map_is_subset(bset1
, bset2
);
9269 isl_bool
isl_basic_map_is_equal(__isl_keep isl_basic_map
*bmap1
,
9270 __isl_keep isl_basic_map
*bmap2
)
9274 if (!bmap1
|| !bmap2
)
9275 return isl_bool_error
;
9276 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
9277 if (is_subset
!= isl_bool_true
)
9279 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
9283 isl_bool
isl_basic_set_is_equal(__isl_keep isl_basic_set
*bset1
,
9284 __isl_keep isl_basic_set
*bset2
)
9286 return isl_basic_map_is_equal(
9287 bset_to_bmap(bset1
), bset_to_bmap(bset2
));
9290 isl_bool
isl_map_is_empty(__isl_keep isl_map
*map
)
9296 return isl_bool_error
;
9297 for (i
= 0; i
< map
->n
; ++i
) {
9298 is_empty
= isl_basic_map_is_empty(map
->p
[i
]);
9300 return isl_bool_error
;
9302 return isl_bool_false
;
9304 return isl_bool_true
;
9307 isl_bool
isl_map_plain_is_empty(__isl_keep isl_map
*map
)
9309 return map
? map
->n
== 0 : isl_bool_error
;
9312 isl_bool
isl_set_plain_is_empty(__isl_keep isl_set
*set
)
9314 return set
? set
->n
== 0 : isl_bool_error
;
9317 isl_bool
isl_set_is_empty(__isl_keep isl_set
*set
)
9319 return isl_map_is_empty(set_to_map(set
));
9323 #define TYPE isl_basic_map
9326 #include "isl_type_has_equal_space_bin_templ.c"
9327 #include "isl_type_check_equal_space_templ.c"
9329 /* Check that "bset1" and "bset2" live in the same space,
9330 * reporting an error if they do not.
9332 isl_stat
isl_basic_set_check_equal_space(__isl_keep isl_basic_set
*bset1
,
9333 __isl_keep isl_basic_set
*bset2
)
9335 return isl_basic_map_check_equal_space(bset_to_bmap(bset1
),
9336 bset_to_bmap(bset1
));
9340 #define TYPE isl_map
9342 #include "isl_type_has_equal_space_bin_templ.c"
9343 #include "isl_type_check_equal_space_templ.c"
9344 #include "isl_type_has_space_templ.c"
9346 isl_bool
isl_set_has_equal_space(__isl_keep isl_set
*set1
,
9347 __isl_keep isl_set
*set2
)
9349 return isl_map_has_equal_space(set_to_map(set1
), set_to_map(set2
));
9353 #define TYPE1 isl_map
9355 #define TYPE2 isl_basic_map
9357 #define TYPE_PAIR isl_map_basic_map
9360 #include "isl_type_has_equal_space_templ.c"
9361 #include "isl_type_check_equal_space_templ.c"
9363 /* Check that "set" and "bset" live in the same space,
9364 * reporting an error if they do not.
9366 isl_stat
isl_set_basic_set_check_equal_space(__isl_keep isl_set
*set
,
9367 __isl_keep isl_basic_set
*bset
)
9369 return isl_map_basic_map_check_equal_space(set_to_map(set
),
9370 bset_to_bmap(bset
));
9373 static isl_bool
map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
9378 return isl_bool_error
;
9379 is_subset
= isl_map_is_subset(map1
, map2
);
9380 if (is_subset
!= isl_bool_true
)
9382 is_subset
= isl_map_is_subset(map2
, map1
);
9386 /* Is "map1" equal to "map2"?
9388 * First check if they are obviously equal.
9389 * If not, then perform a more detailed analysis.
9391 isl_bool
isl_map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
9395 equal
= isl_map_plain_is_equal(map1
, map2
);
9396 if (equal
< 0 || equal
)
9398 return isl_map_align_params_map_map_and_test(map1
, map2
, &map_is_equal
);
9401 isl_bool
isl_basic_map_is_strict_subset(__isl_keep isl_basic_map
*bmap1
,
9402 __isl_keep isl_basic_map
*bmap2
)
9406 if (!bmap1
|| !bmap2
)
9407 return isl_bool_error
;
9408 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
9409 if (is_subset
!= isl_bool_true
)
9411 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
9412 return isl_bool_not(is_subset
);
9415 isl_bool
isl_map_is_strict_subset(__isl_keep isl_map
*map1
,
9416 __isl_keep isl_map
*map2
)
9421 return isl_bool_error
;
9422 is_subset
= isl_map_is_subset(map1
, map2
);
9423 if (is_subset
!= isl_bool_true
)
9425 is_subset
= isl_map_is_subset(map2
, map1
);
9426 return isl_bool_not(is_subset
);
9429 isl_bool
isl_set_is_strict_subset(__isl_keep isl_set
*set1
,
9430 __isl_keep isl_set
*set2
)
9432 return isl_map_is_strict_subset(set_to_map(set1
), set_to_map(set2
));
9435 /* Is "bmap" obviously equal to the universe with the same space?
9437 * That is, does it not have any constraints?
9439 isl_bool
isl_basic_map_plain_is_universe(__isl_keep isl_basic_map
*bmap
)
9442 return isl_bool_error
;
9443 return bmap
->n_eq
== 0 && bmap
->n_ineq
== 0;
9446 /* Is "bset" obviously equal to the universe with the same space?
9448 isl_bool
isl_basic_set_plain_is_universe(__isl_keep isl_basic_set
*bset
)
9450 return isl_basic_map_plain_is_universe(bset
);
9453 /* If "c" does not involve any existentially quantified variables,
9454 * then set *univ to false and abort
9456 static isl_stat
involves_divs(__isl_take isl_constraint
*c
, void *user
)
9458 isl_bool
*univ
= user
;
9461 n
= isl_constraint_dim(c
, isl_dim_div
);
9463 c
= isl_constraint_free(c
);
9464 *univ
= isl_constraint_involves_dims(c
, isl_dim_div
, 0, n
);
9465 isl_constraint_free(c
);
9466 if (*univ
< 0 || !*univ
)
9467 return isl_stat_error
;
9471 /* Is "bmap" equal to the universe with the same space?
9473 * First check if it is obviously equal to the universe.
9474 * If not and if there are any constraints not involving
9475 * existentially quantified variables, then it is certainly
9476 * not equal to the universe.
9477 * Otherwise, check if the universe is a subset of "bmap".
9479 isl_bool
isl_basic_map_is_universe(__isl_keep isl_basic_map
*bmap
)
9483 isl_basic_map
*test
;
9485 univ
= isl_basic_map_plain_is_universe(bmap
);
9486 if (univ
< 0 || univ
)
9488 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
9490 return isl_bool_error
;
9492 return isl_bool_false
;
9493 univ
= isl_bool_true
;
9494 if (isl_basic_map_foreach_constraint(bmap
, &involves_divs
, &univ
) < 0 &&
9496 return isl_bool_error
;
9497 if (univ
< 0 || !univ
)
9499 test
= isl_basic_map_universe(isl_basic_map_get_space(bmap
));
9500 univ
= isl_basic_map_is_subset(test
, bmap
);
9501 isl_basic_map_free(test
);
9505 /* Is "bset" equal to the universe with the same space?
9507 isl_bool
isl_basic_set_is_universe(__isl_keep isl_basic_set
*bset
)
9509 return isl_basic_map_is_universe(bset
);
9512 isl_bool
isl_map_plain_is_universe(__isl_keep isl_map
*map
)
9517 return isl_bool_error
;
9519 for (i
= 0; i
< map
->n
; ++i
) {
9520 isl_bool r
= isl_basic_map_plain_is_universe(map
->p
[i
]);
9525 return isl_bool_false
;
9528 isl_bool
isl_set_plain_is_universe(__isl_keep isl_set
*set
)
9530 return isl_map_plain_is_universe(set_to_map(set
));
9533 isl_bool
isl_basic_map_is_empty(__isl_keep isl_basic_map
*bmap
)
9535 struct isl_basic_set
*bset
= NULL
;
9536 struct isl_vec
*sample
= NULL
;
9537 isl_bool empty
, non_empty
;
9540 return isl_bool_error
;
9542 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
9543 return isl_bool_true
;
9545 if (isl_basic_map_plain_is_universe(bmap
))
9546 return isl_bool_false
;
9548 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
9549 struct isl_basic_map
*copy
= isl_basic_map_copy(bmap
);
9550 copy
= isl_basic_map_remove_redundancies(copy
);
9551 empty
= isl_basic_map_plain_is_empty(copy
);
9552 isl_basic_map_free(copy
);
9556 non_empty
= isl_basic_map_plain_is_non_empty(bmap
);
9558 return isl_bool_error
;
9560 return isl_bool_false
;
9561 isl_vec_free(bmap
->sample
);
9562 bmap
->sample
= NULL
;
9563 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
9565 return isl_bool_error
;
9566 sample
= isl_basic_set_sample_vec(bset
);
9568 return isl_bool_error
;
9569 empty
= sample
->size
== 0;
9570 isl_vec_free(bmap
->sample
);
9571 bmap
->sample
= sample
;
9573 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
9578 isl_bool
isl_basic_map_plain_is_empty(__isl_keep isl_basic_map
*bmap
)
9581 return isl_bool_error
;
9582 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
);
9585 isl_bool
isl_basic_set_plain_is_empty(__isl_keep isl_basic_set
*bset
)
9588 return isl_bool_error
;
9589 return ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
);
9592 /* Is "bmap" known to be non-empty?
9594 * That is, is the cached sample still valid?
9596 isl_bool
isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map
*bmap
)
9601 return isl_bool_error
;
9603 return isl_bool_false
;
9604 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
9606 return isl_bool_error
;
9607 if (bmap
->sample
->size
!= 1 + total
)
9608 return isl_bool_false
;
9609 return isl_basic_map_contains(bmap
, bmap
->sample
);
9612 isl_bool
isl_basic_set_is_empty(__isl_keep isl_basic_set
*bset
)
9614 return isl_basic_map_is_empty(bset_to_bmap(bset
));
9617 __isl_give isl_map
*isl_basic_map_union(__isl_take isl_basic_map
*bmap1
,
9618 __isl_take isl_basic_map
*bmap2
)
9620 struct isl_map
*map
;
9622 if (isl_basic_map_check_equal_space(bmap1
, bmap2
) < 0)
9625 map
= isl_map_alloc_space(isl_space_copy(bmap1
->dim
), 2, 0);
9628 map
= isl_map_add_basic_map(map
, bmap1
);
9629 map
= isl_map_add_basic_map(map
, bmap2
);
9632 isl_basic_map_free(bmap1
);
9633 isl_basic_map_free(bmap2
);
9637 __isl_give isl_set
*isl_basic_set_union(__isl_take isl_basic_set
*bset1
,
9638 __isl_take isl_basic_set
*bset2
)
9640 return set_from_map(isl_basic_map_union(bset_to_bmap(bset1
),
9641 bset_to_bmap(bset2
)));
9644 /* Order divs such that any div only depends on previous divs */
9645 __isl_give isl_basic_map
*isl_basic_map_order_divs(
9646 __isl_take isl_basic_map
*bmap
)
9651 off
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
9653 return isl_basic_map_free(bmap
);
9655 for (i
= 0; i
< bmap
->n_div
; ++i
) {
9657 if (isl_int_is_zero(bmap
->div
[i
][0]))
9659 pos
= isl_seq_first_non_zero(bmap
->div
[i
]+1+1+off
+i
,
9664 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_internal
,
9665 "integer division depends on itself",
9666 return isl_basic_map_free(bmap
));
9667 bmap
= isl_basic_map_swap_div(bmap
, i
, i
+ pos
);
9675 __isl_give isl_map
*isl_map_order_divs(__isl_take isl_map
*map
)
9682 for (i
= 0; i
< map
->n
; ++i
) {
9683 map
->p
[i
] = isl_basic_map_order_divs(map
->p
[i
]);
9694 /* Sort the local variables of "bset".
9696 __isl_give isl_basic_set
*isl_basic_set_sort_divs(
9697 __isl_take isl_basic_set
*bset
)
9699 return bset_from_bmap(isl_basic_map_sort_divs(bset_to_bmap(bset
)));
9702 /* Apply the expansion computed by isl_merge_divs.
9703 * The expansion itself is given by "exp" while the resulting
9704 * list of divs is given by "div".
9706 * Move the integer divisions of "bmap" into the right position
9707 * according to "exp" and then introduce the additional integer
9708 * divisions, adding div constraints.
9709 * The moving should be done first to avoid moving coefficients
9710 * in the definitions of the extra integer divisions.
9712 __isl_give isl_basic_map
*isl_basic_map_expand_divs(
9713 __isl_take isl_basic_map
*bmap
, __isl_take isl_mat
*div
, int *exp
)
9718 bmap
= isl_basic_map_cow(bmap
);
9722 if (div
->n_row
< bmap
->n_div
)
9723 isl_die(isl_mat_get_ctx(div
), isl_error_invalid
,
9724 "not an expansion", goto error
);
9726 n_div
= bmap
->n_div
;
9727 bmap
= isl_basic_map_extend(bmap
, div
->n_row
- n_div
, 0,
9728 2 * (div
->n_row
- n_div
));
9730 for (i
= n_div
; i
< div
->n_row
; ++i
)
9731 if (isl_basic_map_alloc_div(bmap
) < 0)
9734 for (j
= n_div
- 1; j
>= 0; --j
) {
9737 bmap
= isl_basic_map_swap_div(bmap
, j
, exp
[j
]);
9742 for (i
= 0; i
< div
->n_row
; ++i
) {
9743 if (j
< n_div
&& exp
[j
] == i
) {
9746 isl_seq_cpy(bmap
->div
[i
], div
->row
[i
], div
->n_col
);
9747 if (isl_basic_map_div_is_marked_unknown(bmap
, i
))
9749 bmap
= isl_basic_map_add_div_constraints(bmap
, i
);
9758 isl_basic_map_free(bmap
);
9763 /* Apply the expansion computed by isl_merge_divs.
9764 * The expansion itself is given by "exp" while the resulting
9765 * list of divs is given by "div".
9767 __isl_give isl_basic_set
*isl_basic_set_expand_divs(
9768 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*div
, int *exp
)
9770 return isl_basic_map_expand_divs(bset
, div
, exp
);
9773 /* Look for a div in dst that corresponds to the div "div" in src.
9774 * The divs before "div" in src and dst are assumed to be the same.
9776 * Return the position of the corresponding div in dst
9777 * if there is one. Otherwise, return a position beyond the integer divisions.
9778 * Return -1 on error.
9780 static int find_div(__isl_keep isl_basic_map
*dst
,
9781 __isl_keep isl_basic_map
*src
, unsigned div
)
9787 v_div
= isl_basic_map_var_offset(src
, isl_dim_div
);
9788 n_div
= isl_basic_map_dim(dst
, isl_dim_div
);
9789 if (n_div
< 0 || v_div
< 0)
9791 isl_assert(dst
->ctx
, div
<= n_div
, return -1);
9792 for (i
= div
; i
< n_div
; ++i
)
9793 if (isl_seq_eq(dst
->div
[i
], src
->div
[div
], 1+1+v_div
+div
) &&
9794 isl_seq_first_non_zero(dst
->div
[i
] + 1 + 1 + v_div
+ div
,
9800 /* Align the divs of "dst" to those of "src", adding divs from "src"
9801 * if needed. That is, make sure that the first src->n_div divs
9802 * of the result are equal to those of src.
9803 * The integer division of "src" are assumed to be ordered.
9805 * The integer divisions are swapped into the right position
9806 * (possibly after adding them first). This may result
9807 * in the remaining integer divisions appearing in the wrong order,
9808 * i.e., with some integer division appearing before
9809 * some other integer division on which it depends.
9810 * The integer divisions therefore need to be ordered.
9811 * This will not affect the integer divisions aligned to those of "src",
9812 * since "src" is assumed to have ordered integer divisions.
9814 * The result is not finalized as by design it will have redundant
9815 * divs if any divs from "src" were copied.
9817 __isl_give isl_basic_map
*isl_basic_map_align_divs(
9818 __isl_take isl_basic_map
*dst
, __isl_keep isl_basic_map
*src
)
9827 return isl_basic_map_free(dst
);
9829 if (src
->n_div
== 0)
9832 known
= isl_basic_map_divs_known(src
);
9834 return isl_basic_map_free(dst
);
9836 isl_die(isl_basic_map_get_ctx(src
), isl_error_invalid
,
9837 "some src divs are unknown",
9838 return isl_basic_map_free(dst
));
9840 v_div
= isl_basic_map_var_offset(src
, isl_dim_div
);
9842 return isl_basic_map_free(dst
);
9845 dst_n_div
= isl_basic_map_dim(dst
, isl_dim_div
);
9847 dst
= isl_basic_map_free(dst
);
9848 for (i
= 0; i
< src
->n_div
; ++i
) {
9849 int j
= find_div(dst
, src
, i
);
9851 dst
= isl_basic_map_free(dst
);
9852 if (j
== dst_n_div
) {
9854 int extra
= src
->n_div
- i
;
9855 dst
= isl_basic_map_cow(dst
);
9857 return isl_basic_map_free(dst
);
9858 dst
= isl_basic_map_extend(dst
,
9859 extra
, 0, 2 * extra
);
9862 j
= isl_basic_map_alloc_div(dst
);
9864 return isl_basic_map_free(dst
);
9865 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+v_div
+i
);
9866 isl_seq_clr(dst
->div
[j
]+1+1+v_div
+i
, dst
->n_div
- i
);
9868 dst
= isl_basic_map_add_div_constraints(dst
, j
);
9870 return isl_basic_map_free(dst
);
9873 dst
= isl_basic_map_swap_div(dst
, i
, j
);
9875 return isl_basic_map_free(dst
);
9877 return isl_basic_map_order_divs(dst
);
9880 __isl_give isl_map
*isl_map_align_divs_internal(__isl_take isl_map
*map
)
9888 map
= isl_map_compute_divs(map
);
9889 map
= isl_map_order_divs(map
);
9890 map
= isl_map_cow(map
);
9894 for (i
= 1; i
< map
->n
; ++i
)
9895 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], map
->p
[i
]);
9896 for (i
= 1; i
< map
->n
; ++i
) {
9897 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], map
->p
[0]);
9899 return isl_map_free(map
);
9902 map
= isl_map_unmark_normalized(map
);
9906 __isl_give isl_map
*isl_map_align_divs(__isl_take isl_map
*map
)
9908 return isl_map_align_divs_internal(map
);
9911 __isl_give isl_set
*isl_set_align_divs(__isl_take isl_set
*set
)
9913 return set_from_map(isl_map_align_divs_internal(set_to_map(set
)));
9916 /* Align the divs of the basic maps in "map" to those
9917 * of the basic maps in "list", as well as to the other basic maps in "map".
9918 * The elements in "list" are assumed to have known divs.
9920 __isl_give isl_map
*isl_map_align_divs_to_basic_map_list(
9921 __isl_take isl_map
*map
, __isl_keep isl_basic_map_list
*list
)
9926 n
= isl_basic_map_list_n_basic_map(list
);
9927 map
= isl_map_compute_divs(map
);
9928 map
= isl_map_cow(map
);
9930 return isl_map_free(map
);
9934 for (i
= 0; i
< n
; ++i
) {
9935 isl_basic_map
*bmap
;
9937 bmap
= isl_basic_map_list_get_basic_map(list
, i
);
9938 bmap
= isl_basic_map_order_divs(bmap
);
9939 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], bmap
);
9940 isl_basic_map_free(bmap
);
9943 return isl_map_free(map
);
9945 return isl_map_align_divs_internal(map
);
9948 /* Align the divs of each element of "list" to those of "bmap".
9949 * Both "bmap" and the elements of "list" are assumed to have known divs.
9951 __isl_give isl_basic_map_list
*isl_basic_map_list_align_divs_to_basic_map(
9952 __isl_take isl_basic_map_list
*list
, __isl_keep isl_basic_map
*bmap
)
9957 n
= isl_basic_map_list_n_basic_map(list
);
9959 return isl_basic_map_list_free(list
);
9961 for (i
= 0; i
< n
; ++i
) {
9962 isl_basic_map
*bmap_i
;
9964 bmap_i
= isl_basic_map_list_get_basic_map(list
, i
);
9965 bmap_i
= isl_basic_map_align_divs(bmap_i
, bmap
);
9966 list
= isl_basic_map_list_set_basic_map(list
, i
, bmap_i
);
9972 __isl_give isl_set
*isl_set_apply( __isl_take isl_set
*set
,
9973 __isl_take isl_map
*map
)
9977 isl_map_align_params_set(&map
, &set
);
9978 ok
= isl_map_compatible_domain(map
, set
);
9982 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
9983 "incompatible spaces", goto error
);
9984 map
= isl_map_intersect_domain(map
, set
);
9985 set
= isl_map_range(map
);
9993 /* There is no need to cow as removing empty parts doesn't change
9994 * the meaning of the set.
9996 __isl_give isl_map
*isl_map_remove_empty_parts(__isl_take isl_map
*map
)
10003 for (i
= map
->n
- 1; i
>= 0; --i
)
10004 map
= remove_if_empty(map
, i
);
10009 __isl_give isl_set
*isl_set_remove_empty_parts(__isl_take isl_set
*set
)
10011 return set_from_map(isl_map_remove_empty_parts(set_to_map(set
)));
10014 /* Create a binary relation that maps the shared initial "pos" dimensions
10015 * of "bset1" and "bset2" to the remaining dimensions of "bset1" and "bset2".
10017 static __isl_give isl_basic_map
*join_initial(__isl_keep isl_basic_set
*bset1
,
10018 __isl_keep isl_basic_set
*bset2
, int pos
)
10020 isl_basic_map
*bmap1
;
10021 isl_basic_map
*bmap2
;
10023 bmap1
= isl_basic_map_from_range(isl_basic_set_copy(bset1
));
10024 bmap2
= isl_basic_map_from_range(isl_basic_set_copy(bset2
));
10025 bmap1
= isl_basic_map_move_dims(bmap1
, isl_dim_in
, 0,
10026 isl_dim_out
, 0, pos
);
10027 bmap2
= isl_basic_map_move_dims(bmap2
, isl_dim_in
, 0,
10028 isl_dim_out
, 0, pos
);
10029 return isl_basic_map_range_product(bmap1
, bmap2
);
10032 /* Given two basic sets bset1 and bset2, compute the maximal difference
10033 * between the values of dimension pos in bset1 and those in bset2
10034 * for any common value of the parameters and dimensions preceding pos.
10036 static enum isl_lp_result
basic_set_maximal_difference_at(
10037 __isl_keep isl_basic_set
*bset1
, __isl_keep isl_basic_set
*bset2
,
10038 int pos
, isl_int
*opt
)
10040 isl_basic_map
*bmap1
;
10041 struct isl_ctx
*ctx
;
10042 struct isl_vec
*obj
;
10046 enum isl_lp_result res
;
10048 nparam
= isl_basic_set_dim(bset1
, isl_dim_param
);
10049 dim1
= isl_basic_set_dim(bset1
, isl_dim_set
);
10050 if (nparam
< 0 || dim1
< 0 || !bset2
)
10051 return isl_lp_error
;
10053 bmap1
= join_initial(bset1
, bset2
, pos
);
10054 total
= isl_basic_map_dim(bmap1
, isl_dim_all
);
10056 return isl_lp_error
;
10059 obj
= isl_vec_alloc(ctx
, 1 + total
);
10062 isl_seq_clr(obj
->block
.data
, 1 + total
);
10063 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
], 1);
10064 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
+(dim1
-pos
)], -1);
10065 res
= isl_basic_map_solve_lp(bmap1
, 1, obj
->block
.data
, ctx
->one
,
10067 isl_basic_map_free(bmap1
);
10071 isl_basic_map_free(bmap1
);
10072 return isl_lp_error
;
10075 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
10076 * for any common value of the parameters and dimensions preceding pos
10077 * in both basic sets, the values of dimension pos in bset1 are
10078 * smaller or larger than those in bset2.
10081 * 1 if bset1 follows bset2
10082 * -1 if bset1 precedes bset2
10083 * 0 if bset1 and bset2 are incomparable
10084 * -2 if some error occurred.
10086 int isl_basic_set_compare_at(__isl_keep isl_basic_set
*bset1
,
10087 __isl_keep isl_basic_set
*bset2
, int pos
)
10090 enum isl_lp_result res
;
10095 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
10097 if (res
== isl_lp_empty
)
10099 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
10100 res
== isl_lp_unbounded
)
10102 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
10107 isl_int_clear(opt
);
10111 /* Given two basic sets bset1 and bset2, check whether
10112 * for any common value of the parameters and dimensions preceding pos
10113 * there is a value of dimension pos in bset1 that is larger
10114 * than a value of the same dimension in bset2.
10117 * 1 if there exists such a pair
10118 * 0 if there is no such pair, but there is a pair of equal values
10120 * -2 if some error occurred.
10122 int isl_basic_set_follows_at(__isl_keep isl_basic_set
*bset1
,
10123 __isl_keep isl_basic_set
*bset2
, int pos
)
10126 isl_basic_map
*bmap
;
10129 dim1
= isl_basic_set_dim(bset1
, isl_dim_set
);
10132 bmap
= join_initial(bset1
, bset2
, pos
);
10133 bmap
= isl_basic_map_order_ge(bmap
, isl_dim_out
, 0,
10134 isl_dim_out
, dim1
- pos
);
10135 empty
= isl_basic_map_is_empty(bmap
);
10139 isl_basic_map_free(bmap
);
10142 bmap
= isl_basic_map_order_gt(bmap
, isl_dim_out
, 0,
10143 isl_dim_out
, dim1
- pos
);
10144 empty
= isl_basic_map_is_empty(bmap
);
10147 isl_basic_map_free(bmap
);
10152 isl_basic_map_free(bmap
);
10156 /* Given two sets set1 and set2, check whether
10157 * for any common value of the parameters and dimensions preceding pos
10158 * there is a value of dimension pos in set1 that is larger
10159 * than a value of the same dimension in set2.
10162 * 1 if there exists such a pair
10163 * 0 if there is no such pair, but there is a pair of equal values
10165 * -2 if some error occurred.
10167 int isl_set_follows_at(__isl_keep isl_set
*set1
,
10168 __isl_keep isl_set
*set2
, int pos
)
10173 if (!set1
|| !set2
)
10176 for (i
= 0; i
< set1
->n
; ++i
)
10177 for (j
= 0; j
< set2
->n
; ++j
) {
10179 f
= isl_basic_set_follows_at(set1
->p
[i
], set2
->p
[j
], pos
);
10180 if (f
== 1 || f
== -2)
10189 static isl_bool
isl_basic_map_plain_has_fixed_var(
10190 __isl_keep isl_basic_map
*bmap
, unsigned pos
, isl_int
*val
)
10196 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
10198 return isl_bool_error
;
10199 for (i
= 0, d
= total
-1; i
< bmap
->n_eq
&& d
+1 > pos
; ++i
) {
10200 for (; d
+1 > pos
; --d
)
10201 if (!isl_int_is_zero(bmap
->eq
[i
][1+d
]))
10205 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1, d
) != -1)
10206 return isl_bool_false
;
10207 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1+d
+1, total
-d
-1) != -1)
10208 return isl_bool_false
;
10209 if (!isl_int_is_one(bmap
->eq
[i
][1+d
]))
10210 return isl_bool_false
;
10212 isl_int_neg(*val
, bmap
->eq
[i
][0]);
10213 return isl_bool_true
;
10215 return isl_bool_false
;
10218 static isl_bool
isl_map_plain_has_fixed_var(__isl_keep isl_map
*map
,
10219 unsigned pos
, isl_int
*val
)
10227 return isl_bool_error
;
10229 return isl_bool_false
;
10231 return isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, val
);
10234 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, &v
);
10235 for (i
= 1; fixed
== isl_bool_true
&& i
< map
->n
; ++i
) {
10236 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[i
], pos
, &tmp
);
10237 if (fixed
== isl_bool_true
&& isl_int_ne(tmp
, v
))
10238 fixed
= isl_bool_false
;
10241 isl_int_set(*val
, v
);
10242 isl_int_clear(tmp
);
10247 static isl_bool
isl_basic_set_plain_has_fixed_var(
10248 __isl_keep isl_basic_set
*bset
, unsigned pos
, isl_int
*val
)
10250 return isl_basic_map_plain_has_fixed_var(bset_to_bmap(bset
),
10254 isl_bool
isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map
*bmap
,
10255 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
10257 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
10258 return isl_bool_error
;
10259 return isl_basic_map_plain_has_fixed_var(bmap
,
10260 isl_basic_map_offset(bmap
, type
) - 1 + pos
, val
);
10263 /* If "bmap" obviously lies on a hyperplane where the given dimension
10264 * has a fixed value, then return that value.
10265 * Otherwise return NaN.
10267 __isl_give isl_val
*isl_basic_map_plain_get_val_if_fixed(
10268 __isl_keep isl_basic_map
*bmap
,
10269 enum isl_dim_type type
, unsigned pos
)
10277 ctx
= isl_basic_map_get_ctx(bmap
);
10278 v
= isl_val_alloc(ctx
);
10281 fixed
= isl_basic_map_plain_is_fixed(bmap
, type
, pos
, &v
->n
);
10283 return isl_val_free(v
);
10285 isl_int_set_si(v
->d
, 1);
10289 return isl_val_nan(ctx
);
10292 isl_bool
isl_map_plain_is_fixed(__isl_keep isl_map
*map
,
10293 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
10295 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
10296 return isl_bool_error
;
10297 return isl_map_plain_has_fixed_var(map
,
10298 map_offset(map
, type
) - 1 + pos
, val
);
10301 /* If "map" obviously lies on a hyperplane where the given dimension
10302 * has a fixed value, then return that value.
10303 * Otherwise return NaN.
10305 __isl_give isl_val
*isl_map_plain_get_val_if_fixed(__isl_keep isl_map
*map
,
10306 enum isl_dim_type type
, unsigned pos
)
10314 ctx
= isl_map_get_ctx(map
);
10315 v
= isl_val_alloc(ctx
);
10318 fixed
= isl_map_plain_is_fixed(map
, type
, pos
, &v
->n
);
10320 return isl_val_free(v
);
10322 isl_int_set_si(v
->d
, 1);
10326 return isl_val_nan(ctx
);
10329 /* If "set" obviously lies on a hyperplane where the given dimension
10330 * has a fixed value, then return that value.
10331 * Otherwise return NaN.
10333 __isl_give isl_val
*isl_set_plain_get_val_if_fixed(__isl_keep isl_set
*set
,
10334 enum isl_dim_type type
, unsigned pos
)
10336 return isl_map_plain_get_val_if_fixed(set
, type
, pos
);
10339 /* Return a sequence of values in the same space as "set"
10340 * that are equal to the corresponding set dimensions of "set"
10341 * for those set dimensions that obviously lie on a hyperplane
10342 * where the dimension has a fixed value.
10343 * The other elements are set to NaN.
10345 __isl_give isl_multi_val
*isl_set_get_plain_multi_val_if_fixed(
10346 __isl_keep isl_set
*set
)
10353 space
= isl_space_drop_all_params(isl_set_get_space(set
));
10354 mv
= isl_multi_val_alloc(space
);
10355 n
= isl_multi_val_size(mv
);
10357 return isl_multi_val_free(mv
);
10359 for (i
= 0; i
< n
; ++i
) {
10362 v
= isl_set_plain_get_val_if_fixed(set
, isl_dim_set
, i
);
10363 mv
= isl_multi_val_set_val(mv
, i
, v
);
10369 /* Check if dimension dim has fixed value and if so and if val is not NULL,
10370 * then return this fixed value in *val.
10372 isl_bool
isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set
*bset
,
10373 unsigned dim
, isl_int
*val
)
10377 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
10379 return isl_bool_error
;
10380 return isl_basic_set_plain_has_fixed_var(bset
, nparam
+ dim
, val
);
10383 /* Return -1 if the constraint "c1" should be sorted before "c2"
10384 * and 1 if it should be sorted after "c2".
10385 * Return 0 if the two constraints are the same (up to the constant term).
10387 * In particular, if a constraint involves later variables than another
10388 * then it is sorted after this other constraint.
10389 * uset_gist depends on constraints without existentially quantified
10390 * variables sorting first.
10392 * For constraints that have the same latest variable, those
10393 * with the same coefficient for this latest variable (first in absolute value
10394 * and then in actual value) are grouped together.
10395 * This is useful for detecting pairs of constraints that can
10396 * be chained in their printed representation.
10398 * Finally, within a group, constraints are sorted according to
10399 * their coefficients (excluding the constant term).
10401 static int sort_constraint_cmp(const void *p1
, const void *p2
, void *arg
)
10403 isl_int
**c1
= (isl_int
**) p1
;
10404 isl_int
**c2
= (isl_int
**) p2
;
10406 unsigned size
= *(unsigned *) arg
;
10409 l1
= isl_seq_last_non_zero(*c1
+ 1, size
);
10410 l2
= isl_seq_last_non_zero(*c2
+ 1, size
);
10415 cmp
= isl_int_abs_cmp((*c1
)[1 + l1
], (*c2
)[1 + l1
]);
10418 cmp
= isl_int_cmp((*c1
)[1 + l1
], (*c2
)[1 + l1
]);
10422 return isl_seq_cmp(*c1
+ 1, *c2
+ 1, size
);
10425 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
10426 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
10427 * and 0 if the two constraints are the same (up to the constant term).
10429 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map
*bmap
,
10430 isl_int
*c1
, isl_int
*c2
)
10435 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
10439 return sort_constraint_cmp(&c1
, &c2
, &size
);
10442 __isl_give isl_basic_map
*isl_basic_map_sort_constraints(
10443 __isl_take isl_basic_map
*bmap
)
10450 if (bmap
->n_ineq
== 0)
10452 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_SORTED
))
10454 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
10456 return isl_basic_map_free(bmap
);
10458 if (isl_sort(bmap
->ineq
, bmap
->n_ineq
, sizeof(isl_int
*),
10459 &sort_constraint_cmp
, &size
) < 0)
10460 return isl_basic_map_free(bmap
);
10461 ISL_F_SET(bmap
, ISL_BASIC_MAP_SORTED
);
10465 __isl_give isl_basic_set
*isl_basic_set_sort_constraints(
10466 __isl_take isl_basic_set
*bset
)
10468 isl_basic_map
*bmap
= bset_to_bmap(bset
);
10469 return bset_from_bmap(isl_basic_map_sort_constraints(bmap
));
10472 __isl_give isl_basic_map
*isl_basic_map_normalize(
10473 __isl_take isl_basic_map
*bmap
)
10475 bmap
= isl_basic_map_remove_redundancies(bmap
);
10476 bmap
= isl_basic_map_sort_constraints(bmap
);
10479 int isl_basic_map_plain_cmp(__isl_keep isl_basic_map
*bmap1
,
10480 __isl_keep isl_basic_map
*bmap2
)
10484 isl_space
*space1
, *space2
;
10486 if (!bmap1
|| !bmap2
)
10489 if (bmap1
== bmap2
)
10491 space1
= isl_basic_map_peek_space(bmap1
);
10492 space2
= isl_basic_map_peek_space(bmap2
);
10493 cmp
= isl_space_cmp(space1
, space2
);
10496 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) !=
10497 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_RATIONAL
))
10498 return ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) ? -1 : 1;
10499 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
) &&
10500 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
10502 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
))
10504 if (ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
10506 if (bmap1
->n_eq
!= bmap2
->n_eq
)
10507 return bmap1
->n_eq
- bmap2
->n_eq
;
10508 if (bmap1
->n_ineq
!= bmap2
->n_ineq
)
10509 return bmap1
->n_ineq
- bmap2
->n_ineq
;
10510 if (bmap1
->n_div
!= bmap2
->n_div
)
10511 return bmap1
->n_div
- bmap2
->n_div
;
10512 total
= isl_basic_map_dim(bmap1
, isl_dim_all
);
10515 for (i
= 0; i
< bmap1
->n_eq
; ++i
) {
10516 cmp
= isl_seq_cmp(bmap1
->eq
[i
], bmap2
->eq
[i
], 1+total
);
10520 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
10521 cmp
= isl_seq_cmp(bmap1
->ineq
[i
], bmap2
->ineq
[i
], 1+total
);
10525 for (i
= 0; i
< bmap1
->n_div
; ++i
) {
10526 isl_bool unknown1
, unknown2
;
10528 unknown1
= isl_basic_map_div_is_marked_unknown(bmap1
, i
);
10529 unknown2
= isl_basic_map_div_is_marked_unknown(bmap2
, i
);
10530 if (unknown1
< 0 || unknown2
< 0)
10532 if (unknown1
&& unknown2
)
10538 cmp
= isl_seq_cmp(bmap1
->div
[i
], bmap2
->div
[i
], 1+1+total
);
10545 int isl_basic_set_plain_cmp(__isl_keep isl_basic_set
*bset1
,
10546 __isl_keep isl_basic_set
*bset2
)
10548 return isl_basic_map_plain_cmp(bset1
, bset2
);
10551 int isl_set_plain_cmp(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
10557 if (set1
->n
!= set2
->n
)
10558 return set1
->n
- set2
->n
;
10560 for (i
= 0; i
< set1
->n
; ++i
) {
10561 cmp
= isl_basic_set_plain_cmp(set1
->p
[i
], set2
->p
[i
]);
10569 isl_bool
isl_basic_map_plain_is_equal(__isl_keep isl_basic_map
*bmap1
,
10570 __isl_keep isl_basic_map
*bmap2
)
10572 if (!bmap1
|| !bmap2
)
10573 return isl_bool_error
;
10574 return isl_basic_map_plain_cmp(bmap1
, bmap2
) == 0;
10577 isl_bool
isl_basic_set_plain_is_equal(__isl_keep isl_basic_set
*bset1
,
10578 __isl_keep isl_basic_set
*bset2
)
10580 return isl_basic_map_plain_is_equal(bset_to_bmap(bset1
),
10581 bset_to_bmap(bset2
));
10584 static int qsort_bmap_cmp(const void *p1
, const void *p2
)
10586 isl_basic_map
*bmap1
= *(isl_basic_map
**) p1
;
10587 isl_basic_map
*bmap2
= *(isl_basic_map
**) p2
;
10589 return isl_basic_map_plain_cmp(bmap1
, bmap2
);
10592 /* Sort the basic maps of "map" and remove duplicate basic maps.
10594 * While removing basic maps, we make sure that the basic maps remain
10595 * sorted because isl_map_normalize expects the basic maps of the result
10598 static __isl_give isl_map
*sort_and_remove_duplicates(__isl_take isl_map
*map
)
10602 map
= isl_map_remove_empty_parts(map
);
10605 qsort(map
->p
, map
->n
, sizeof(struct isl_basic_map
*), qsort_bmap_cmp
);
10606 for (i
= map
->n
- 1; i
>= 1; --i
) {
10607 if (!isl_basic_map_plain_is_equal(map
->p
[i
- 1], map
->p
[i
]))
10609 isl_basic_map_free(map
->p
[i
-1]);
10610 for (j
= i
; j
< map
->n
; ++j
)
10611 map
->p
[j
- 1] = map
->p
[j
];
10618 /* Remove obvious duplicates among the basic maps of "map".
10620 * Unlike isl_map_normalize, this function does not remove redundant
10621 * constraints and only removes duplicates that have exactly the same
10622 * constraints in the input. It does sort the constraints and
10623 * the basic maps to ease the detection of duplicates.
10625 * If "map" has already been normalized or if the basic maps are
10626 * disjoint, then there can be no duplicates.
10628 __isl_give isl_map
*isl_map_remove_obvious_duplicates(__isl_take isl_map
*map
)
10631 isl_basic_map
*bmap
;
10637 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
| ISL_MAP_DISJOINT
))
10639 for (i
= 0; i
< map
->n
; ++i
) {
10640 bmap
= isl_basic_map_copy(map
->p
[i
]);
10641 bmap
= isl_basic_map_sort_constraints(bmap
);
10643 return isl_map_free(map
);
10644 isl_basic_map_free(map
->p
[i
]);
10648 map
= sort_and_remove_duplicates(map
);
10652 /* We normalize in place, but if anything goes wrong we need
10653 * to return NULL, so we need to make sure we don't change the
10654 * meaning of any possible other copies of map.
10656 __isl_give isl_map
*isl_map_normalize(__isl_take isl_map
*map
)
10659 struct isl_basic_map
*bmap
;
10663 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
))
10665 for (i
= 0; i
< map
->n
; ++i
) {
10666 bmap
= isl_basic_map_normalize(isl_basic_map_copy(map
->p
[i
]));
10669 isl_basic_map_free(map
->p
[i
]);
10673 map
= sort_and_remove_duplicates(map
);
10675 ISL_F_SET(map
, ISL_MAP_NORMALIZED
);
10682 __isl_give isl_set
*isl_set_normalize(__isl_take isl_set
*set
)
10684 return set_from_map(isl_map_normalize(set_to_map(set
)));
10687 isl_bool
isl_map_plain_is_equal(__isl_keep isl_map
*map1
,
10688 __isl_keep isl_map
*map2
)
10693 if (!map1
|| !map2
)
10694 return isl_bool_error
;
10697 return isl_bool_true
;
10698 equal
= isl_map_has_equal_space(map1
, map2
);
10699 if (equal
< 0 || !equal
)
10702 map1
= isl_map_copy(map1
);
10703 map2
= isl_map_copy(map2
);
10704 map1
= isl_map_normalize(map1
);
10705 map2
= isl_map_normalize(map2
);
10706 if (!map1
|| !map2
)
10708 equal
= map1
->n
== map2
->n
;
10709 for (i
= 0; equal
&& i
< map1
->n
; ++i
) {
10710 equal
= isl_basic_map_plain_is_equal(map1
->p
[i
], map2
->p
[i
]);
10714 isl_map_free(map1
);
10715 isl_map_free(map2
);
10718 isl_map_free(map1
);
10719 isl_map_free(map2
);
10720 return isl_bool_error
;
10723 isl_bool
isl_set_plain_is_equal(__isl_keep isl_set
*set1
,
10724 __isl_keep isl_set
*set2
)
10726 return isl_map_plain_is_equal(set_to_map(set1
), set_to_map(set2
));
10729 /* Return the basic maps in "map" as a list.
10731 __isl_give isl_basic_map_list
*isl_map_get_basic_map_list(
10732 __isl_keep isl_map
*map
)
10736 isl_basic_map_list
*list
;
10740 ctx
= isl_map_get_ctx(map
);
10741 list
= isl_basic_map_list_alloc(ctx
, map
->n
);
10743 for (i
= 0; i
< map
->n
; ++i
) {
10744 isl_basic_map
*bmap
;
10746 bmap
= isl_basic_map_copy(map
->p
[i
]);
10747 list
= isl_basic_map_list_add(list
, bmap
);
10753 /* Return the intersection of the elements in the non-empty list "list".
10754 * All elements are assumed to live in the same space.
10756 __isl_give isl_basic_map
*isl_basic_map_list_intersect(
10757 __isl_take isl_basic_map_list
*list
)
10761 isl_basic_map
*bmap
;
10763 n
= isl_basic_map_list_n_basic_map(list
);
10767 isl_die(isl_basic_map_list_get_ctx(list
), isl_error_invalid
,
10768 "expecting non-empty list", goto error
);
10770 bmap
= isl_basic_map_list_get_basic_map(list
, 0);
10771 for (i
= 1; i
< n
; ++i
) {
10772 isl_basic_map
*bmap_i
;
10774 bmap_i
= isl_basic_map_list_get_basic_map(list
, i
);
10775 bmap
= isl_basic_map_intersect(bmap
, bmap_i
);
10778 isl_basic_map_list_free(list
);
10781 isl_basic_map_list_free(list
);
10785 /* Return the intersection of the elements in the non-empty list "list".
10786 * All elements are assumed to live in the same space.
10788 __isl_give isl_basic_set
*isl_basic_set_list_intersect(
10789 __isl_take isl_basic_set_list
*list
)
10791 return isl_basic_map_list_intersect(list
);
10794 /* Return the union of the elements of "list".
10795 * The list is required to have at least one element.
10797 __isl_give isl_set
*isl_basic_set_list_union(
10798 __isl_take isl_basic_set_list
*list
)
10803 isl_basic_set
*bset
;
10806 n
= isl_basic_set_list_n_basic_set(list
);
10810 isl_die(isl_basic_set_list_get_ctx(list
), isl_error_invalid
,
10811 "expecting non-empty list", goto error
);
10813 bset
= isl_basic_set_list_get_basic_set(list
, 0);
10814 space
= isl_basic_set_get_space(bset
);
10815 isl_basic_set_free(bset
);
10817 set
= isl_set_alloc_space(space
, n
, 0);
10818 for (i
= 0; i
< n
; ++i
) {
10819 bset
= isl_basic_set_list_get_basic_set(list
, i
);
10820 set
= isl_set_add_basic_set(set
, bset
);
10823 isl_basic_set_list_free(list
);
10826 isl_basic_set_list_free(list
);
10830 /* Return the union of the elements in the non-empty list "list".
10831 * All elements are assumed to live in the same space.
10833 __isl_give isl_set
*isl_set_list_union(__isl_take isl_set_list
*list
)
10839 n
= isl_set_list_n_set(list
);
10843 isl_die(isl_set_list_get_ctx(list
), isl_error_invalid
,
10844 "expecting non-empty list", goto error
);
10846 set
= isl_set_list_get_set(list
, 0);
10847 for (i
= 1; i
< n
; ++i
) {
10850 set_i
= isl_set_list_get_set(list
, i
);
10851 set
= isl_set_union(set
, set_i
);
10854 isl_set_list_free(list
);
10857 isl_set_list_free(list
);
10861 __isl_give isl_basic_map
*isl_basic_map_product(
10862 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
10864 isl_space
*space_result
= NULL
;
10865 struct isl_basic_map
*bmap
;
10866 unsigned in1
, in2
, out1
, out2
, nparam
, total
, pos
;
10867 struct isl_dim_map
*dim_map1
, *dim_map2
;
10869 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
10871 space_result
= isl_space_product(isl_space_copy(bmap1
->dim
),
10872 isl_space_copy(bmap2
->dim
));
10874 in1
= isl_basic_map_dim(bmap1
, isl_dim_in
);
10875 in2
= isl_basic_map_dim(bmap2
, isl_dim_in
);
10876 out1
= isl_basic_map_dim(bmap1
, isl_dim_out
);
10877 out2
= isl_basic_map_dim(bmap2
, isl_dim_out
);
10878 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
10880 total
= nparam
+ in1
+ in2
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
10881 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
10882 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
10883 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
10884 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
10885 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
10886 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
10887 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
10888 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
10889 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
10890 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
10892 bmap
= isl_basic_map_alloc_space(space_result
,
10893 bmap1
->n_div
+ bmap2
->n_div
,
10894 bmap1
->n_eq
+ bmap2
->n_eq
,
10895 bmap1
->n_ineq
+ bmap2
->n_ineq
);
10896 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
10897 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
10898 bmap
= isl_basic_map_simplify(bmap
);
10899 return isl_basic_map_finalize(bmap
);
10901 isl_basic_map_free(bmap1
);
10902 isl_basic_map_free(bmap2
);
10906 __isl_give isl_basic_map
*isl_basic_map_flat_product(
10907 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
10909 isl_basic_map
*prod
;
10911 prod
= isl_basic_map_product(bmap1
, bmap2
);
10912 prod
= isl_basic_map_flatten(prod
);
10916 __isl_give isl_basic_set
*isl_basic_set_flat_product(
10917 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
10919 return isl_basic_map_flat_range_product(bset1
, bset2
);
10922 __isl_give isl_basic_map
*isl_basic_map_domain_product(
10923 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
10925 isl_space
*space1
, *space2
;
10926 isl_space
*space_result
= NULL
;
10927 isl_basic_map
*bmap
;
10928 isl_size in1
, in2
, out
, nparam
;
10929 unsigned total
, pos
;
10930 struct isl_dim_map
*dim_map1
, *dim_map2
;
10932 in1
= isl_basic_map_dim(bmap1
, isl_dim_in
);
10933 in2
= isl_basic_map_dim(bmap2
, isl_dim_in
);
10934 out
= isl_basic_map_dim(bmap1
, isl_dim_out
);
10935 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
10936 if (in1
< 0 || in2
< 0 || out
< 0 || nparam
< 0)
10939 space1
= isl_basic_map_get_space(bmap1
);
10940 space2
= isl_basic_map_get_space(bmap2
);
10941 space_result
= isl_space_domain_product(space1
, space2
);
10943 total
= nparam
+ in1
+ in2
+ out
+ bmap1
->n_div
+ bmap2
->n_div
;
10944 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
10945 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
10946 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
10947 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
10948 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
10949 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
10950 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
10951 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
);
10952 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out
);
10953 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
10955 bmap
= isl_basic_map_alloc_space(space_result
,
10956 bmap1
->n_div
+ bmap2
->n_div
,
10957 bmap1
->n_eq
+ bmap2
->n_eq
,
10958 bmap1
->n_ineq
+ bmap2
->n_ineq
);
10959 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
10960 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
10961 bmap
= isl_basic_map_simplify(bmap
);
10962 return isl_basic_map_finalize(bmap
);
10964 isl_basic_map_free(bmap1
);
10965 isl_basic_map_free(bmap2
);
10969 __isl_give isl_basic_map
*isl_basic_map_range_product(
10970 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
10973 isl_space
*space_result
= NULL
;
10974 isl_basic_map
*bmap
;
10975 isl_size in
, out1
, out2
, nparam
;
10976 unsigned total
, pos
;
10977 struct isl_dim_map
*dim_map1
, *dim_map2
;
10979 rational
= isl_basic_map_is_rational(bmap1
);
10980 if (rational
>= 0 && rational
)
10981 rational
= isl_basic_map_is_rational(bmap2
);
10982 in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
10983 out1
= isl_basic_map_dim(bmap1
, isl_dim_out
);
10984 out2
= isl_basic_map_dim(bmap2
, isl_dim_out
);
10985 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
10986 if (in
< 0 || out1
< 0 || out2
< 0 || nparam
< 0 || rational
< 0)
10989 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
10992 space_result
= isl_space_range_product(isl_space_copy(bmap1
->dim
),
10993 isl_space_copy(bmap2
->dim
));
10995 total
= nparam
+ in
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
10996 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
10997 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
10998 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
10999 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
11000 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
11001 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
11002 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in
);
11003 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
11004 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
11005 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
11007 bmap
= isl_basic_map_alloc_space(space_result
,
11008 bmap1
->n_div
+ bmap2
->n_div
,
11009 bmap1
->n_eq
+ bmap2
->n_eq
,
11010 bmap1
->n_ineq
+ bmap2
->n_ineq
);
11011 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
11012 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
11014 bmap
= isl_basic_map_set_rational(bmap
);
11015 bmap
= isl_basic_map_simplify(bmap
);
11016 return isl_basic_map_finalize(bmap
);
11018 isl_basic_map_free(bmap1
);
11019 isl_basic_map_free(bmap2
);
11023 __isl_give isl_basic_map
*isl_basic_map_flat_range_product(
11024 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
11026 isl_basic_map
*prod
;
11028 prod
= isl_basic_map_range_product(bmap1
, bmap2
);
11029 prod
= isl_basic_map_flatten_range(prod
);
11033 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
11034 * and collect the results.
11035 * The result live in the space obtained by calling "space_product"
11036 * on the spaces of "map1" and "map2".
11037 * If "remove_duplicates" is set then the result may contain duplicates
11038 * (even if the inputs do not) and so we try and remove the obvious
11041 static __isl_give isl_map
*map_product(__isl_take isl_map
*map1
,
11042 __isl_take isl_map
*map2
,
11043 __isl_give isl_space
*(*space_product
)(__isl_take isl_space
*left
,
11044 __isl_take isl_space
*right
),
11045 __isl_give isl_basic_map
*(*basic_map_product
)(
11046 __isl_take isl_basic_map
*left
,
11047 __isl_take isl_basic_map
*right
),
11048 int remove_duplicates
)
11050 unsigned flags
= 0;
11051 struct isl_map
*result
;
11055 m
= isl_map_has_equal_params(map1
, map2
);
11059 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
11060 "parameters don't match", goto error
);
11062 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
11063 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
11064 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
11066 result
= isl_map_alloc_space(space_product(isl_space_copy(map1
->dim
),
11067 isl_space_copy(map2
->dim
)),
11068 map1
->n
* map2
->n
, flags
);
11071 for (i
= 0; i
< map1
->n
; ++i
)
11072 for (j
= 0; j
< map2
->n
; ++j
) {
11073 struct isl_basic_map
*part
;
11074 part
= basic_map_product(isl_basic_map_copy(map1
->p
[i
]),
11075 isl_basic_map_copy(map2
->p
[j
]));
11076 if (isl_basic_map_is_empty(part
))
11077 isl_basic_map_free(part
);
11079 result
= isl_map_add_basic_map(result
, part
);
11083 if (remove_duplicates
)
11084 result
= isl_map_remove_obvious_duplicates(result
);
11085 isl_map_free(map1
);
11086 isl_map_free(map2
);
11089 isl_map_free(map1
);
11090 isl_map_free(map2
);
11094 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
11096 __isl_give isl_map
*isl_map_product(__isl_take isl_map
*map1
,
11097 __isl_take isl_map
*map2
)
11099 isl_map_align_params_bin(&map1
, &map2
);
11100 return map_product(map1
, map2
, &isl_space_product
,
11101 &isl_basic_map_product
, 0);
11104 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
11106 __isl_give isl_map
*isl_map_flat_product(__isl_take isl_map
*map1
,
11107 __isl_take isl_map
*map2
)
11111 prod
= isl_map_product(map1
, map2
);
11112 prod
= isl_map_flatten(prod
);
11116 /* Given two set A and B, construct its Cartesian product A x B.
11118 __isl_give isl_set
*isl_set_product(__isl_take isl_set
*set1
,
11119 __isl_take isl_set
*set2
)
11121 return isl_map_range_product(set1
, set2
);
11124 __isl_give isl_set
*isl_set_flat_product(__isl_take isl_set
*set1
,
11125 __isl_take isl_set
*set2
)
11127 return isl_map_flat_range_product(set1
, set2
);
11130 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
11132 __isl_give isl_map
*isl_map_domain_product(__isl_take isl_map
*map1
,
11133 __isl_take isl_map
*map2
)
11135 isl_map_align_params_bin(&map1
, &map2
);
11136 return map_product(map1
, map2
, &isl_space_domain_product
,
11137 &isl_basic_map_domain_product
, 1);
11140 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
11142 __isl_give isl_map
*isl_map_range_product(__isl_take isl_map
*map1
,
11143 __isl_take isl_map
*map2
)
11145 isl_map_align_params_bin(&map1
, &map2
);
11146 return map_product(map1
, map2
, &isl_space_range_product
,
11147 &isl_basic_map_range_product
, 1);
11150 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
11152 __isl_give isl_map
*isl_map_factor_domain(__isl_take isl_map
*map
)
11155 isl_size total1
, keep1
, total2
, keep2
;
11157 total1
= isl_map_dim(map
, isl_dim_in
);
11158 total2
= isl_map_dim(map
, isl_dim_out
);
11159 if (total1
< 0 || total2
< 0)
11160 return isl_map_free(map
);
11161 if (!isl_space_domain_is_wrapping(map
->dim
) ||
11162 !isl_space_range_is_wrapping(map
->dim
))
11163 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
11164 "not a product", return isl_map_free(map
));
11166 space
= isl_map_get_space(map
);
11167 space
= isl_space_factor_domain(space
);
11168 keep1
= isl_space_dim(space
, isl_dim_in
);
11169 keep2
= isl_space_dim(space
, isl_dim_out
);
11170 if (keep1
< 0 || keep2
< 0)
11171 map
= isl_map_free(map
);
11172 map
= isl_map_project_out(map
, isl_dim_in
, keep1
, total1
- keep1
);
11173 map
= isl_map_project_out(map
, isl_dim_out
, keep2
, total2
- keep2
);
11174 map
= isl_map_reset_space(map
, space
);
11179 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
11181 __isl_give isl_map
*isl_map_factor_range(__isl_take isl_map
*map
)
11184 isl_size total1
, keep1
, total2
, keep2
;
11186 total1
= isl_map_dim(map
, isl_dim_in
);
11187 total2
= isl_map_dim(map
, isl_dim_out
);
11188 if (total1
< 0 || total2
< 0)
11189 return isl_map_free(map
);
11190 if (!isl_space_domain_is_wrapping(map
->dim
) ||
11191 !isl_space_range_is_wrapping(map
->dim
))
11192 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
11193 "not a product", return isl_map_free(map
));
11195 space
= isl_map_get_space(map
);
11196 space
= isl_space_factor_range(space
);
11197 keep1
= isl_space_dim(space
, isl_dim_in
);
11198 keep2
= isl_space_dim(space
, isl_dim_out
);
11199 if (keep1
< 0 || keep2
< 0)
11200 map
= isl_map_free(map
);
11201 map
= isl_map_project_out(map
, isl_dim_in
, 0, total1
- keep1
);
11202 map
= isl_map_project_out(map
, isl_dim_out
, 0, total2
- keep2
);
11203 map
= isl_map_reset_space(map
, space
);
11208 /* Given a map of the form [A -> B] -> C, return the map A -> C.
11210 __isl_give isl_map
*isl_map_domain_factor_domain(__isl_take isl_map
*map
)
11213 isl_size total
, keep
;
11215 total
= isl_map_dim(map
, isl_dim_in
);
11217 return isl_map_free(map
);
11218 if (!isl_space_domain_is_wrapping(map
->dim
))
11219 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
11220 "domain is not a product", return isl_map_free(map
));
11222 space
= isl_map_get_space(map
);
11223 space
= isl_space_domain_factor_domain(space
);
11224 keep
= isl_space_dim(space
, isl_dim_in
);
11226 map
= isl_map_free(map
);
11227 map
= isl_map_project_out(map
, isl_dim_in
, keep
, total
- keep
);
11228 map
= isl_map_reset_space(map
, space
);
11233 /* Given a map of the form [A -> B] -> C, return the map B -> C.
11235 __isl_give isl_map
*isl_map_domain_factor_range(__isl_take isl_map
*map
)
11238 isl_size total
, keep
;
11240 total
= isl_map_dim(map
, isl_dim_in
);
11242 return isl_map_free(map
);
11243 if (!isl_space_domain_is_wrapping(map
->dim
))
11244 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
11245 "domain is not a product", return isl_map_free(map
));
11247 space
= isl_map_get_space(map
);
11248 space
= isl_space_domain_factor_range(space
);
11249 keep
= isl_space_dim(space
, isl_dim_in
);
11251 map
= isl_map_free(map
);
11252 map
= isl_map_project_out(map
, isl_dim_in
, 0, total
- keep
);
11253 map
= isl_map_reset_space(map
, space
);
11258 /* Given a map A -> [B -> C], extract the map A -> B.
11260 __isl_give isl_map
*isl_map_range_factor_domain(__isl_take isl_map
*map
)
11263 isl_size total
, keep
;
11265 total
= isl_map_dim(map
, isl_dim_out
);
11267 return isl_map_free(map
);
11268 if (!isl_space_range_is_wrapping(map
->dim
))
11269 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
11270 "range is not a product", return isl_map_free(map
));
11272 space
= isl_map_get_space(map
);
11273 space
= isl_space_range_factor_domain(space
);
11274 keep
= isl_space_dim(space
, isl_dim_out
);
11276 map
= isl_map_free(map
);
11277 map
= isl_map_project_out(map
, isl_dim_out
, keep
, total
- keep
);
11278 map
= isl_map_reset_space(map
, space
);
11283 /* Given a map A -> [B -> C], extract the map A -> C.
11285 __isl_give isl_map
*isl_map_range_factor_range(__isl_take isl_map
*map
)
11288 isl_size total
, keep
;
11290 total
= isl_map_dim(map
, isl_dim_out
);
11292 return isl_map_free(map
);
11293 if (!isl_space_range_is_wrapping(map
->dim
))
11294 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
11295 "range is not a product", return isl_map_free(map
));
11297 space
= isl_map_get_space(map
);
11298 space
= isl_space_range_factor_range(space
);
11299 keep
= isl_space_dim(space
, isl_dim_out
);
11301 map
= isl_map_free(map
);
11302 map
= isl_map_project_out(map
, isl_dim_out
, 0, total
- keep
);
11303 map
= isl_map_reset_space(map
, space
);
11308 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
11310 __isl_give isl_map
*isl_map_flat_domain_product(__isl_take isl_map
*map1
,
11311 __isl_take isl_map
*map2
)
11315 prod
= isl_map_domain_product(map1
, map2
);
11316 prod
= isl_map_flatten_domain(prod
);
11320 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
11322 __isl_give isl_map
*isl_map_flat_range_product(__isl_take isl_map
*map1
,
11323 __isl_take isl_map
*map2
)
11327 prod
= isl_map_range_product(map1
, map2
);
11328 prod
= isl_map_flatten_range(prod
);
11332 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map
*bmap
)
11335 uint32_t hash
= isl_hash_init();
11340 bmap
= isl_basic_map_copy(bmap
);
11341 bmap
= isl_basic_map_normalize(bmap
);
11342 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
11345 isl_hash_byte(hash
, bmap
->n_eq
& 0xFF);
11346 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
11348 c_hash
= isl_seq_get_hash(bmap
->eq
[i
], 1 + total
);
11349 isl_hash_hash(hash
, c_hash
);
11351 isl_hash_byte(hash
, bmap
->n_ineq
& 0xFF);
11352 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
11354 c_hash
= isl_seq_get_hash(bmap
->ineq
[i
], 1 + total
);
11355 isl_hash_hash(hash
, c_hash
);
11357 isl_hash_byte(hash
, bmap
->n_div
& 0xFF);
11358 for (i
= 0; i
< bmap
->n_div
; ++i
) {
11360 if (isl_int_is_zero(bmap
->div
[i
][0]))
11362 isl_hash_byte(hash
, i
& 0xFF);
11363 c_hash
= isl_seq_get_hash(bmap
->div
[i
], 1 + 1 + total
);
11364 isl_hash_hash(hash
, c_hash
);
11366 isl_basic_map_free(bmap
);
11370 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set
*bset
)
11372 return isl_basic_map_get_hash(bset_to_bmap(bset
));
11375 uint32_t isl_map_get_hash(__isl_keep isl_map
*map
)
11382 map
= isl_map_copy(map
);
11383 map
= isl_map_normalize(map
);
11387 hash
= isl_hash_init();
11388 for (i
= 0; i
< map
->n
; ++i
) {
11389 uint32_t bmap_hash
;
11390 bmap_hash
= isl_basic_map_get_hash(map
->p
[i
]);
11391 isl_hash_hash(hash
, bmap_hash
);
11399 uint32_t isl_set_get_hash(__isl_keep isl_set
*set
)
11401 return isl_map_get_hash(set_to_map(set
));
11404 /* Return the number of basic maps in the (current) representation of "map".
11406 isl_size
isl_map_n_basic_map(__isl_keep isl_map
*map
)
11408 return map
? map
->n
: isl_size_error
;
11411 isl_size
isl_set_n_basic_set(__isl_keep isl_set
*set
)
11413 return set
? set
->n
: isl_size_error
;
11416 isl_stat
isl_map_foreach_basic_map(__isl_keep isl_map
*map
,
11417 isl_stat (*fn
)(__isl_take isl_basic_map
*bmap
, void *user
), void *user
)
11422 return isl_stat_error
;
11424 for (i
= 0; i
< map
->n
; ++i
)
11425 if (fn(isl_basic_map_copy(map
->p
[i
]), user
) < 0)
11426 return isl_stat_error
;
11428 return isl_stat_ok
;
11431 isl_stat
isl_set_foreach_basic_set(__isl_keep isl_set
*set
,
11432 isl_stat (*fn
)(__isl_take isl_basic_set
*bset
, void *user
), void *user
)
11437 return isl_stat_error
;
11439 for (i
= 0; i
< set
->n
; ++i
)
11440 if (fn(isl_basic_set_copy(set
->p
[i
]), user
) < 0)
11441 return isl_stat_error
;
11443 return isl_stat_ok
;
11446 /* Does "test" succeed on every basic set in "set"?
11448 isl_bool
isl_set_every_basic_set(__isl_keep isl_set
*set
,
11449 isl_bool (*test
)(__isl_keep isl_basic_set
*bset
, void *user
),
11455 return isl_bool_error
;
11457 for (i
= 0; i
< set
->n
; ++i
) {
11460 r
= test(set
->p
[i
], user
);
11465 return isl_bool_true
;
11468 /* Return a list of basic sets, the union of which is equal to "set".
11470 __isl_give isl_basic_set_list
*isl_set_get_basic_set_list(
11471 __isl_keep isl_set
*set
)
11474 isl_basic_set_list
*list
;
11479 list
= isl_basic_set_list_alloc(isl_set_get_ctx(set
), set
->n
);
11480 for (i
= 0; i
< set
->n
; ++i
) {
11481 isl_basic_set
*bset
;
11483 bset
= isl_basic_set_copy(set
->p
[i
]);
11484 list
= isl_basic_set_list_add(list
, bset
);
11490 __isl_give isl_basic_set
*isl_basic_set_lift(__isl_take isl_basic_set
*bset
)
11497 bset
= isl_basic_set_cow(bset
);
11501 space
= isl_basic_set_get_space(bset
);
11502 space
= isl_space_lift(space
, bset
->n_div
);
11505 isl_space_free(bset
->dim
);
11507 bset
->extra
-= bset
->n_div
;
11510 bset
= isl_basic_set_finalize(bset
);
11514 isl_basic_set_free(bset
);
11518 __isl_give isl_set
*isl_set_lift(__isl_take isl_set
*set
)
11524 set
= set_from_map(isl_map_align_divs_internal(set_to_map(set
)));
11529 set
= isl_set_cow(set
);
11533 n_div
= set
->p
[0]->n_div
;
11534 space
= isl_set_get_space(set
);
11535 space
= isl_space_lift(space
, n_div
);
11538 isl_space_free(set
->dim
);
11541 for (i
= 0; i
< set
->n
; ++i
) {
11542 set
->p
[i
] = isl_basic_set_lift(set
->p
[i
]);
11553 int isl_basic_set_size(__isl_keep isl_basic_set
*bset
)
11558 dim
= isl_basic_set_dim(bset
, isl_dim_all
);
11561 size
+= bset
->n_eq
* (1 + dim
);
11562 size
+= bset
->n_ineq
* (1 + dim
);
11563 size
+= bset
->n_div
* (2 + dim
);
11568 int isl_set_size(__isl_keep isl_set
*set
)
11576 for (i
= 0; i
< set
->n
; ++i
)
11577 size
+= isl_basic_set_size(set
->p
[i
]);
11582 /* Check if there is any lower bound (if lower == 0) and/or upper
11583 * bound (if upper == 0) on the specified dim.
11585 static isl_bool
basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
11586 enum isl_dim_type type
, unsigned pos
, int lower
, int upper
)
11590 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
11591 return isl_bool_error
;
11593 pos
+= isl_basic_map_offset(bmap
, type
);
11595 for (i
= 0; i
< bmap
->n_div
; ++i
) {
11596 if (isl_int_is_zero(bmap
->div
[i
][0]))
11598 if (!isl_int_is_zero(bmap
->div
[i
][1 + pos
]))
11599 return isl_bool_true
;
11602 for (i
= 0; i
< bmap
->n_eq
; ++i
)
11603 if (!isl_int_is_zero(bmap
->eq
[i
][pos
]))
11604 return isl_bool_true
;
11606 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
11607 int sgn
= isl_int_sgn(bmap
->ineq
[i
][pos
]);
11614 return lower
&& upper
;
11617 isl_bool
isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
11618 enum isl_dim_type type
, unsigned pos
)
11620 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 0);
11623 isl_bool
isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map
*bmap
,
11624 enum isl_dim_type type
, unsigned pos
)
11626 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 1);
11629 isl_bool
isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map
*bmap
,
11630 enum isl_dim_type type
, unsigned pos
)
11632 return basic_map_dim_is_bounded(bmap
, type
, pos
, 1, 0);
11635 isl_bool
isl_map_dim_is_bounded(__isl_keep isl_map
*map
,
11636 enum isl_dim_type type
, unsigned pos
)
11641 return isl_bool_error
;
11643 for (i
= 0; i
< map
->n
; ++i
) {
11645 bounded
= isl_basic_map_dim_is_bounded(map
->p
[i
], type
, pos
);
11646 if (bounded
< 0 || !bounded
)
11650 return isl_bool_true
;
11653 /* Return true if the specified dim is involved in both an upper bound
11654 * and a lower bound.
11656 isl_bool
isl_set_dim_is_bounded(__isl_keep isl_set
*set
,
11657 enum isl_dim_type type
, unsigned pos
)
11659 return isl_map_dim_is_bounded(set_to_map(set
), type
, pos
);
11662 /* Does "map" have a bound (according to "fn") for any of its basic maps?
11664 static isl_bool
has_any_bound(__isl_keep isl_map
*map
,
11665 enum isl_dim_type type
, unsigned pos
,
11666 isl_bool (*fn
)(__isl_keep isl_basic_map
*bmap
,
11667 enum isl_dim_type type
, unsigned pos
))
11672 return isl_bool_error
;
11674 for (i
= 0; i
< map
->n
; ++i
) {
11676 bounded
= fn(map
->p
[i
], type
, pos
);
11677 if (bounded
< 0 || bounded
)
11681 return isl_bool_false
;
11684 /* Return 1 if the specified dim is involved in any lower bound.
11686 isl_bool
isl_set_dim_has_any_lower_bound(__isl_keep isl_set
*set
,
11687 enum isl_dim_type type
, unsigned pos
)
11689 return has_any_bound(set
, type
, pos
,
11690 &isl_basic_map_dim_has_lower_bound
);
11693 /* Return 1 if the specified dim is involved in any upper bound.
11695 isl_bool
isl_set_dim_has_any_upper_bound(__isl_keep isl_set
*set
,
11696 enum isl_dim_type type
, unsigned pos
)
11698 return has_any_bound(set
, type
, pos
,
11699 &isl_basic_map_dim_has_upper_bound
);
11702 /* Does "map" have a bound (according to "fn") for all of its basic maps?
11704 static isl_bool
has_bound(__isl_keep isl_map
*map
,
11705 enum isl_dim_type type
, unsigned pos
,
11706 isl_bool (*fn
)(__isl_keep isl_basic_map
*bmap
,
11707 enum isl_dim_type type
, unsigned pos
))
11712 return isl_bool_error
;
11714 for (i
= 0; i
< map
->n
; ++i
) {
11716 bounded
= fn(map
->p
[i
], type
, pos
);
11717 if (bounded
< 0 || !bounded
)
11721 return isl_bool_true
;
11724 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
11726 isl_bool
isl_set_dim_has_lower_bound(__isl_keep isl_set
*set
,
11727 enum isl_dim_type type
, unsigned pos
)
11729 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_lower_bound
);
11732 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
11734 isl_bool
isl_set_dim_has_upper_bound(__isl_keep isl_set
*set
,
11735 enum isl_dim_type type
, unsigned pos
)
11737 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_upper_bound
);
11740 /* For each of the "n" variables starting at "first", determine
11741 * the sign of the variable and put the results in the first "n"
11742 * elements of the array "signs".
11744 * 1 means that the variable is non-negative
11745 * -1 means that the variable is non-positive
11746 * 0 means the variable attains both positive and negative values.
11748 isl_stat
isl_basic_set_vars_get_sign(__isl_keep isl_basic_set
*bset
,
11749 unsigned first
, unsigned n
, int *signs
)
11751 isl_vec
*bound
= NULL
;
11752 struct isl_tab
*tab
= NULL
;
11753 struct isl_tab_undo
*snap
;
11757 total
= isl_basic_set_dim(bset
, isl_dim_all
);
11758 if (total
< 0 || !signs
)
11759 return isl_stat_error
;
11761 bound
= isl_vec_alloc(bset
->ctx
, 1 + total
);
11762 tab
= isl_tab_from_basic_set(bset
, 0);
11763 if (!bound
|| !tab
)
11766 isl_seq_clr(bound
->el
, bound
->size
);
11767 isl_int_set_si(bound
->el
[0], -1);
11769 snap
= isl_tab_snap(tab
);
11770 for (i
= 0; i
< n
; ++i
) {
11773 isl_int_set_si(bound
->el
[1 + first
+ i
], -1);
11774 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
11776 empty
= tab
->empty
;
11777 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
11778 if (isl_tab_rollback(tab
, snap
) < 0)
11786 isl_int_set_si(bound
->el
[1 + first
+ i
], 1);
11787 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
11789 empty
= tab
->empty
;
11790 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
11791 if (isl_tab_rollback(tab
, snap
) < 0)
11794 signs
[i
] = empty
? -1 : 0;
11798 isl_vec_free(bound
);
11799 return isl_stat_ok
;
11802 isl_vec_free(bound
);
11803 return isl_stat_error
;
11806 isl_stat
isl_basic_set_dims_get_sign(__isl_keep isl_basic_set
*bset
,
11807 enum isl_dim_type type
, unsigned first
, unsigned n
, int *signs
)
11809 if (!bset
|| !signs
)
11810 return isl_stat_error
;
11811 if (isl_basic_set_check_range(bset
, type
, first
, n
) < 0)
11812 return isl_stat_error
;
11814 first
+= pos(bset
->dim
, type
) - 1;
11815 return isl_basic_set_vars_get_sign(bset
, first
, n
, signs
);
11818 /* Is it possible for the integer division "div" to depend (possibly
11819 * indirectly) on any output dimensions?
11821 * If the div is undefined, then we conservatively assume that it
11822 * may depend on them.
11823 * Otherwise, we check if it actually depends on them or on any integer
11824 * divisions that may depend on them.
11826 static isl_bool
div_may_involve_output(__isl_keep isl_basic_map
*bmap
, int div
)
11829 isl_size n_out
, n_div
;
11830 unsigned o_out
, o_div
;
11832 if (isl_int_is_zero(bmap
->div
[div
][0]))
11833 return isl_bool_true
;
11835 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
11837 return isl_bool_error
;
11838 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
11840 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + o_out
, n_out
) != -1)
11841 return isl_bool_true
;
11843 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
11845 return isl_bool_error
;
11846 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
11848 for (i
= 0; i
< n_div
; ++i
) {
11849 isl_bool may_involve
;
11851 if (isl_int_is_zero(bmap
->div
[div
][1 + o_div
+ i
]))
11853 may_involve
= div_may_involve_output(bmap
, i
);
11854 if (may_involve
< 0 || may_involve
)
11855 return may_involve
;
11858 return isl_bool_false
;
11861 /* Return the first integer division of "bmap" in the range
11862 * [first, first + n[ that may depend on any output dimensions and
11863 * that has a non-zero coefficient in "c" (where the first coefficient
11864 * in "c" corresponds to integer division "first").
11866 static int first_div_may_involve_output(__isl_keep isl_basic_map
*bmap
,
11867 isl_int
*c
, int first
, int n
)
11874 for (k
= first
; k
< first
+ n
; ++k
) {
11875 isl_bool may_involve
;
11877 if (isl_int_is_zero(c
[k
]))
11879 may_involve
= div_may_involve_output(bmap
, k
);
11880 if (may_involve
< 0)
11889 /* Look for a pair of inequality constraints in "bmap" of the form
11891 * -l + i >= 0 or i >= l
11893 * n + l - i >= 0 or i <= l + n
11895 * with n < "m" and i the output dimension at position "pos".
11896 * (Note that n >= 0 as otherwise the two constraints would conflict.)
11897 * Furthermore, "l" is only allowed to involve parameters, input dimensions
11898 * and earlier output dimensions, as well as integer divisions that do
11899 * not involve any of the output dimensions.
11901 * Return the index of the first inequality constraint or bmap->n_ineq
11902 * if no such pair can be found.
11904 static int find_modulo_constraint_pair(__isl_keep isl_basic_map
*bmap
,
11905 int pos
, isl_int m
)
11910 isl_size n_div
, n_out
;
11911 unsigned o_div
, o_out
;
11914 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
11915 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
11916 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
11917 if (total
< 0 || n_out
< 0 || n_div
< 0)
11920 ctx
= isl_basic_map_get_ctx(bmap
);
11921 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
11922 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
11923 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
11924 if (!isl_int_abs_eq(bmap
->ineq
[i
][o_out
+ pos
], ctx
->one
))
11926 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + o_out
+ pos
+ 1,
11927 n_out
- (pos
+ 1)) != -1)
11929 if (first_div_may_involve_output(bmap
, bmap
->ineq
[i
] + o_div
,
11932 for (j
= i
+ 1; j
< bmap
->n_ineq
; ++j
) {
11933 if (!isl_int_abs_eq(bmap
->ineq
[j
][o_out
+ pos
],
11936 if (!isl_seq_is_neg(bmap
->ineq
[i
] + 1,
11937 bmap
->ineq
[j
] + 1, total
))
11941 if (j
>= bmap
->n_ineq
)
11943 isl_int_add(bmap
->ineq
[i
][0],
11944 bmap
->ineq
[i
][0], bmap
->ineq
[j
][0]);
11945 less
= isl_int_abs_lt(bmap
->ineq
[i
][0], m
);
11946 isl_int_sub(bmap
->ineq
[i
][0],
11947 bmap
->ineq
[i
][0], bmap
->ineq
[j
][0]);
11950 if (isl_int_is_one(bmap
->ineq
[i
][o_out
+ pos
]))
11956 return bmap
->n_ineq
;
11959 /* Return the index of the equality of "bmap" that defines
11960 * the output dimension "pos" in terms of earlier dimensions.
11961 * The equality may also involve integer divisions, as long
11962 * as those integer divisions are defined in terms of
11963 * parameters or input dimensions.
11964 * In this case, *div is set to the number of integer divisions and
11965 * *ineq is set to the number of inequality constraints (provided
11966 * div and ineq are not NULL).
11968 * The equality may also involve a single integer division involving
11969 * the output dimensions (typically only output dimension "pos") as
11970 * long as the coefficient of output dimension "pos" is 1 or -1 and
11971 * there is a pair of constraints i >= l and i <= l + n, with i referring
11972 * to output dimension "pos", l an expression involving only earlier
11973 * dimensions and n smaller than the coefficient of the integer division
11974 * in the equality. In this case, the output dimension can be defined
11975 * in terms of a modulo expression that does not involve the integer division.
11976 * *div is then set to this single integer division and
11977 * *ineq is set to the index of constraint i >= l.
11979 * Return bmap->n_eq if there is no such equality.
11980 * Return -1 on error.
11982 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map
*bmap
,
11983 int pos
, int *div
, int *ineq
)
11986 isl_size n_div
, n_out
;
11987 unsigned o_div
, o_out
;
11989 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
11990 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
11991 if (n_out
< 0 || n_div
< 0)
11994 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
11995 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
11998 *ineq
= bmap
->n_ineq
;
12001 for (j
= 0; j
< bmap
->n_eq
; ++j
) {
12002 if (isl_int_is_zero(bmap
->eq
[j
][o_out
+ pos
]))
12004 if (isl_seq_first_non_zero(bmap
->eq
[j
] + o_out
+ pos
+ 1,
12005 n_out
- (pos
+ 1)) != -1)
12007 k
= first_div_may_involve_output(bmap
, bmap
->eq
[j
] + o_div
,
12011 if (!isl_int_is_one(bmap
->eq
[j
][o_out
+ pos
]) &&
12012 !isl_int_is_negone(bmap
->eq
[j
][o_out
+ pos
]))
12014 if (first_div_may_involve_output(bmap
, bmap
->eq
[j
] + o_div
,
12015 k
+ 1, n_div
- (k
+1)) < n_div
)
12017 l
= find_modulo_constraint_pair(bmap
, pos
,
12018 bmap
->eq
[j
][o_div
+ k
]);
12021 if (l
>= bmap
->n_ineq
)
12033 /* Check if the given basic map is obviously single-valued.
12034 * In particular, for each output dimension, check that there is
12035 * an equality that defines the output dimension in terms of
12036 * earlier dimensions.
12038 isl_bool
isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map
*bmap
)
12043 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
12045 return isl_bool_error
;
12047 for (i
= 0; i
< n_out
; ++i
) {
12050 eq
= isl_basic_map_output_defining_equality(bmap
, i
,
12053 return isl_bool_error
;
12054 if (eq
>= bmap
->n_eq
)
12055 return isl_bool_false
;
12058 return isl_bool_true
;
12061 /* Check if the given basic map is single-valued.
12062 * We simply compute
12066 * and check if the result is a subset of the identity mapping.
12068 isl_bool
isl_basic_map_is_single_valued(__isl_keep isl_basic_map
*bmap
)
12071 isl_basic_map
*test
;
12075 sv
= isl_basic_map_plain_is_single_valued(bmap
);
12079 test
= isl_basic_map_reverse(isl_basic_map_copy(bmap
));
12080 test
= isl_basic_map_apply_range(test
, isl_basic_map_copy(bmap
));
12082 space
= isl_basic_map_get_space(bmap
);
12083 space
= isl_space_map_from_set(isl_space_range(space
));
12084 id
= isl_basic_map_identity(space
);
12086 sv
= isl_basic_map_is_subset(test
, id
);
12088 isl_basic_map_free(test
);
12089 isl_basic_map_free(id
);
12094 /* Check if the given map is obviously single-valued.
12096 isl_bool
isl_map_plain_is_single_valued(__isl_keep isl_map
*map
)
12099 return isl_bool_error
;
12101 return isl_bool_true
;
12103 return isl_bool_false
;
12105 return isl_basic_map_plain_is_single_valued(map
->p
[0]);
12108 /* Check if the given map is single-valued.
12109 * We simply compute
12113 * and check if the result is a subset of the identity mapping.
12115 isl_bool
isl_map_is_single_valued(__isl_keep isl_map
*map
)
12122 sv
= isl_map_plain_is_single_valued(map
);
12126 test
= isl_map_reverse(isl_map_copy(map
));
12127 test
= isl_map_apply_range(test
, isl_map_copy(map
));
12129 space
= isl_space_map_from_set(isl_space_range(isl_map_get_space(map
)));
12130 id
= isl_map_identity(space
);
12132 sv
= isl_map_is_subset(test
, id
);
12134 isl_map_free(test
);
12140 isl_bool
isl_map_is_injective(__isl_keep isl_map
*map
)
12144 map
= isl_map_copy(map
);
12145 map
= isl_map_reverse(map
);
12146 in
= isl_map_is_single_valued(map
);
12152 /* Check if the given map is obviously injective.
12154 isl_bool
isl_map_plain_is_injective(__isl_keep isl_map
*map
)
12158 map
= isl_map_copy(map
);
12159 map
= isl_map_reverse(map
);
12160 in
= isl_map_plain_is_single_valued(map
);
12166 isl_bool
isl_map_is_bijective(__isl_keep isl_map
*map
)
12170 sv
= isl_map_is_single_valued(map
);
12174 return isl_map_is_injective(map
);
12177 isl_bool
isl_set_is_singleton(__isl_keep isl_set
*set
)
12179 return isl_map_is_single_valued(set_to_map(set
));
12182 /* Does "map" only map elements to themselves?
12184 * If the domain and range spaces are different, then "map"
12185 * is considered not to be an identity relation, even if it is empty.
12186 * Otherwise, construct the maximal identity relation and
12187 * check whether "map" is a subset of this relation.
12189 isl_bool
isl_map_is_identity(__isl_keep isl_map
*map
)
12192 isl_bool equal
, is_identity
;
12194 equal
= isl_map_tuple_is_equal(map
, isl_dim_in
, map
, isl_dim_out
);
12195 if (equal
< 0 || !equal
)
12198 id
= isl_map_identity(isl_map_get_space(map
));
12199 is_identity
= isl_map_is_subset(map
, id
);
12202 return is_identity
;
12205 int isl_map_is_translation(__isl_keep isl_map
*map
)
12210 delta
= isl_map_deltas(isl_map_copy(map
));
12211 ok
= isl_set_is_singleton(delta
);
12212 isl_set_free(delta
);
12217 static int unique(isl_int
*p
, unsigned pos
, unsigned len
)
12219 if (isl_seq_first_non_zero(p
, pos
) != -1)
12221 if (isl_seq_first_non_zero(p
+ pos
+ 1, len
- pos
- 1) != -1)
12226 isl_bool
isl_basic_set_is_box(__isl_keep isl_basic_set
*bset
)
12229 isl_size nvar
, ovar
, n_div
;
12231 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
12233 return isl_bool_error
;
12235 return isl_bool_false
;
12237 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
12238 ovar
= isl_space_offset(bset
->dim
, isl_dim_set
);
12239 if (nvar
< 0 || ovar
< 0)
12240 return isl_bool_error
;
12241 for (j
= 0; j
< nvar
; ++j
) {
12242 int lower
= 0, upper
= 0;
12243 for (i
= 0; i
< bset
->n_eq
; ++i
) {
12244 if (isl_int_is_zero(bset
->eq
[i
][1 + ovar
+ j
]))
12246 if (!unique(bset
->eq
[i
] + 1 + ovar
, j
, nvar
))
12247 return isl_bool_false
;
12250 if (i
< bset
->n_eq
)
12252 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
12253 if (isl_int_is_zero(bset
->ineq
[i
][1 + ovar
+ j
]))
12255 if (!unique(bset
->ineq
[i
] + 1 + ovar
, j
, nvar
))
12256 return isl_bool_false
;
12257 if (isl_int_is_pos(bset
->ineq
[i
][1 + ovar
+ j
]))
12262 if (!lower
|| !upper
)
12263 return isl_bool_false
;
12266 return isl_bool_true
;
12269 isl_bool
isl_set_is_box(__isl_keep isl_set
*set
)
12272 return isl_bool_error
;
12274 return isl_bool_false
;
12276 return isl_basic_set_is_box(set
->p
[0]);
12279 isl_bool
isl_basic_set_is_wrapping(__isl_keep isl_basic_set
*bset
)
12282 return isl_bool_error
;
12284 return isl_space_is_wrapping(bset
->dim
);
12287 isl_bool
isl_set_is_wrapping(__isl_keep isl_set
*set
)
12290 return isl_bool_error
;
12292 return isl_space_is_wrapping(set
->dim
);
12295 /* Modify the space of "map" through a call to "change".
12296 * If "can_change" is set (not NULL), then first call it to check
12297 * if the modification is allowed, printing the error message "cannot_change"
12300 static __isl_give isl_map
*isl_map_change_space(__isl_take isl_map
*map
,
12301 isl_bool (*can_change
)(__isl_keep isl_map
*map
),
12302 const char *cannot_change
,
12303 __isl_give isl_space
*(*change
)(__isl_take isl_space
*space
))
12311 ok
= can_change
? can_change(map
) : isl_bool_true
;
12313 return isl_map_free(map
);
12315 isl_die(isl_map_get_ctx(map
), isl_error_invalid
, cannot_change
,
12316 return isl_map_free(map
));
12318 space
= change(isl_map_get_space(map
));
12319 map
= isl_map_reset_space(map
, space
);
12324 /* Is the domain of "map" a wrapped relation?
12326 isl_bool
isl_map_domain_is_wrapping(__isl_keep isl_map
*map
)
12329 return isl_bool_error
;
12331 return isl_space_domain_is_wrapping(map
->dim
);
12334 /* Does "map" have a wrapped relation in both domain and range?
12336 isl_bool
isl_map_is_product(__isl_keep isl_map
*map
)
12338 return isl_space_is_product(isl_map_peek_space(map
));
12341 /* Is the range of "map" a wrapped relation?
12343 isl_bool
isl_map_range_is_wrapping(__isl_keep isl_map
*map
)
12346 return isl_bool_error
;
12348 return isl_space_range_is_wrapping(map
->dim
);
12351 __isl_give isl_basic_set
*isl_basic_map_wrap(__isl_take isl_basic_map
*bmap
)
12355 space
= isl_basic_map_take_space(bmap
);
12356 space
= isl_space_wrap(space
);
12357 bmap
= isl_basic_map_restore_space(bmap
, space
);
12359 bmap
= isl_basic_map_finalize(bmap
);
12361 return bset_from_bmap(bmap
);
12364 /* Given a map A -> B, return the set (A -> B).
12366 __isl_give isl_set
*isl_map_wrap(__isl_take isl_map
*map
)
12368 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_wrap
);
12371 __isl_give isl_basic_map
*isl_basic_set_unwrap(__isl_take isl_basic_set
*bset
)
12373 bset
= isl_basic_set_cow(bset
);
12377 bset
->dim
= isl_space_unwrap(bset
->dim
);
12381 bset
= isl_basic_set_finalize(bset
);
12383 return bset_to_bmap(bset
);
12385 isl_basic_set_free(bset
);
12389 /* Given a set (A -> B), return the map A -> B.
12390 * Error out if "set" is not of the form (A -> B).
12392 __isl_give isl_map
*isl_set_unwrap(__isl_take isl_set
*set
)
12394 return isl_map_change_space(set
, &isl_set_is_wrapping
,
12395 "not a wrapping set", &isl_space_unwrap
);
12398 __isl_give isl_basic_map
*isl_basic_map_reset(__isl_take isl_basic_map
*bmap
,
12399 enum isl_dim_type type
)
12403 space
= isl_basic_map_take_space(bmap
);
12404 space
= isl_space_reset(space
, type
);
12405 bmap
= isl_basic_map_restore_space(bmap
, space
);
12407 bmap
= isl_basic_map_mark_final(bmap
);
12412 __isl_give isl_map
*isl_map_reset(__isl_take isl_map
*map
,
12413 enum isl_dim_type type
)
12421 if (!isl_space_is_named_or_nested(map
->dim
, type
))
12424 map
= isl_map_cow(map
);
12428 for (i
= 0; i
< map
->n
; ++i
) {
12429 map
->p
[i
] = isl_basic_map_reset(map
->p
[i
], type
);
12434 space
= isl_map_take_space(map
);
12435 space
= isl_space_reset(space
, type
);
12436 map
= isl_map_restore_space(map
, space
);
12444 __isl_give isl_basic_map
*isl_basic_map_flatten(__isl_take isl_basic_map
*bmap
)
12448 space
= isl_basic_map_take_space(bmap
);
12449 space
= isl_space_flatten(space
);
12450 bmap
= isl_basic_map_restore_space(bmap
, space
);
12452 bmap
= isl_basic_map_mark_final(bmap
);
12457 __isl_give isl_basic_set
*isl_basic_set_flatten(__isl_take isl_basic_set
*bset
)
12459 return bset_from_bmap(isl_basic_map_flatten(bset_to_bmap(bset
)));
12462 __isl_give isl_basic_map
*isl_basic_map_flatten_domain(
12463 __isl_take isl_basic_map
*bmap
)
12467 space
= isl_basic_map_take_space(bmap
);
12468 space
= isl_space_flatten_domain(space
);
12469 bmap
= isl_basic_map_restore_space(bmap
, space
);
12471 bmap
= isl_basic_map_mark_final(bmap
);
12476 __isl_give isl_basic_map
*isl_basic_map_flatten_range(
12477 __isl_take isl_basic_map
*bmap
)
12481 space
= isl_basic_map_take_space(bmap
);
12482 space
= isl_space_flatten_range(space
);
12483 bmap
= isl_basic_map_restore_space(bmap
, space
);
12485 bmap
= isl_basic_map_mark_final(bmap
);
12490 /* Remove any internal structure from the spaces of domain and range of "map".
12492 __isl_give isl_map
*isl_map_flatten(__isl_take isl_map
*map
)
12497 if (!map
->dim
->nested
[0] && !map
->dim
->nested
[1])
12500 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten
);
12503 __isl_give isl_set
*isl_set_flatten(__isl_take isl_set
*set
)
12505 return set_from_map(isl_map_flatten(set_to_map(set
)));
12508 __isl_give isl_map
*isl_set_flatten_map(__isl_take isl_set
*set
)
12510 isl_space
*space
, *flat_space
;
12513 space
= isl_set_get_space(set
);
12514 flat_space
= isl_space_flatten(isl_space_copy(space
));
12515 map
= isl_map_identity(isl_space_join(isl_space_reverse(space
),
12517 map
= isl_map_intersect_domain(map
, set
);
12522 /* Remove any internal structure from the space of the domain of "map".
12524 __isl_give isl_map
*isl_map_flatten_domain(__isl_take isl_map
*map
)
12529 if (!map
->dim
->nested
[0])
12532 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten_domain
);
12535 /* Remove any internal structure from the space of the range of "map".
12537 __isl_give isl_map
*isl_map_flatten_range(__isl_take isl_map
*map
)
12542 if (!map
->dim
->nested
[1])
12545 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten_range
);
12548 /* Reorder the dimensions of "bmap" according to the given dim_map
12549 * and set the dimension specification to "space" and
12550 * perform Gaussian elimination on the result.
12552 __isl_give isl_basic_map
*isl_basic_map_realign(__isl_take isl_basic_map
*bmap
,
12553 __isl_take isl_space
*space
, __isl_take
struct isl_dim_map
*dim_map
)
12555 isl_basic_map
*res
;
12559 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
12560 if (n_div
< 0 || !space
|| !dim_map
)
12563 flags
= bmap
->flags
;
12564 ISL_FL_CLR(flags
, ISL_BASIC_MAP_FINAL
);
12565 ISL_FL_CLR(flags
, ISL_BASIC_MAP_SORTED
);
12566 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
12567 res
= isl_basic_map_alloc_space(space
, n_div
, bmap
->n_eq
, bmap
->n_ineq
);
12568 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
12570 res
->flags
= flags
;
12571 res
= isl_basic_map_gauss(res
, NULL
);
12572 res
= isl_basic_map_finalize(res
);
12575 isl_dim_map_free(dim_map
);
12576 isl_basic_map_free(bmap
);
12577 isl_space_free(space
);
12581 /* Reorder the dimensions of "map" according to given reordering.
12583 __isl_give isl_map
*isl_map_realign(__isl_take isl_map
*map
,
12584 __isl_take isl_reordering
*r
)
12587 struct isl_dim_map
*dim_map
;
12589 map
= isl_map_cow(map
);
12590 dim_map
= isl_dim_map_from_reordering(r
);
12591 if (!map
|| !r
|| !dim_map
)
12594 for (i
= 0; i
< map
->n
; ++i
) {
12595 struct isl_dim_map
*dim_map_i
;
12598 dim_map_i
= isl_dim_map_extend(dim_map
, map
->p
[i
]);
12600 space
= isl_reordering_get_space(r
);
12601 map
->p
[i
] = isl_basic_map_realign(map
->p
[i
], space
, dim_map_i
);
12607 map
= isl_map_reset_space(map
, isl_reordering_get_space(r
));
12608 map
= isl_map_unmark_normalized(map
);
12610 isl_reordering_free(r
);
12611 isl_dim_map_free(dim_map
);
12614 isl_dim_map_free(dim_map
);
12616 isl_reordering_free(r
);
12620 __isl_give isl_set
*isl_set_realign(__isl_take isl_set
*set
,
12621 __isl_take isl_reordering
*r
)
12623 return set_from_map(isl_map_realign(set_to_map(set
), r
));
12626 __isl_give isl_map
*isl_map_align_params(__isl_take isl_map
*map
,
12627 __isl_take isl_space
*model
)
12632 if (!map
|| !model
)
12635 ctx
= isl_space_get_ctx(model
);
12636 if (!isl_space_has_named_params(model
))
12637 isl_die(ctx
, isl_error_invalid
,
12638 "model has unnamed parameters", goto error
);
12639 if (isl_map_check_named_params(map
) < 0)
12641 aligned
= isl_map_space_has_equal_params(map
, model
);
12646 isl_reordering
*exp
;
12648 space
= isl_map_peek_space(map
);
12649 exp
= isl_parameter_alignment_reordering(space
, model
);
12650 map
= isl_map_realign(map
, exp
);
12653 isl_space_free(model
);
12656 isl_space_free(model
);
12661 __isl_give isl_set
*isl_set_align_params(__isl_take isl_set
*set
,
12662 __isl_take isl_space
*model
)
12664 return isl_map_align_params(set
, model
);
12667 /* Align the parameters of "bmap" to those of "model", introducing
12668 * additional parameters if needed.
12670 __isl_give isl_basic_map
*isl_basic_map_align_params(
12671 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*model
)
12674 isl_bool equal_params
;
12675 isl_space
*bmap_space
;
12677 if (!bmap
|| !model
)
12680 ctx
= isl_space_get_ctx(model
);
12681 if (!isl_space_has_named_params(model
))
12682 isl_die(ctx
, isl_error_invalid
,
12683 "model has unnamed parameters", goto error
);
12684 if (isl_basic_map_check_named_params(bmap
) < 0)
12686 bmap_space
= isl_basic_map_peek_space(bmap
);
12687 equal_params
= isl_space_has_equal_params(bmap_space
, model
);
12688 if (equal_params
< 0)
12690 if (!equal_params
) {
12691 isl_reordering
*exp
;
12692 struct isl_dim_map
*dim_map
;
12694 exp
= isl_parameter_alignment_reordering(bmap_space
, model
);
12695 dim_map
= isl_dim_map_from_reordering(exp
);
12696 bmap
= isl_basic_map_realign(bmap
,
12697 isl_reordering_get_space(exp
),
12698 isl_dim_map_extend(dim_map
, bmap
));
12699 isl_reordering_free(exp
);
12700 isl_dim_map_free(dim_map
);
12703 isl_space_free(model
);
12706 isl_space_free(model
);
12707 isl_basic_map_free(bmap
);
12711 /* Do "bset" and "space" have the same parameters?
12713 isl_bool
isl_basic_set_space_has_equal_params(__isl_keep isl_basic_set
*bset
,
12714 __isl_keep isl_space
*space
)
12716 isl_space
*bset_space
;
12718 bset_space
= isl_basic_set_peek_space(bset
);
12719 return isl_space_has_equal_params(bset_space
, space
);
12722 /* Do "map" and "space" have the same parameters?
12724 isl_bool
isl_map_space_has_equal_params(__isl_keep isl_map
*map
,
12725 __isl_keep isl_space
*space
)
12727 isl_space
*map_space
;
12729 map_space
= isl_map_peek_space(map
);
12730 return isl_space_has_equal_params(map_space
, space
);
12733 /* Do "set" and "space" have the same parameters?
12735 isl_bool
isl_set_space_has_equal_params(__isl_keep isl_set
*set
,
12736 __isl_keep isl_space
*space
)
12738 return isl_map_space_has_equal_params(set_to_map(set
), space
);
12741 /* Align the parameters of "bset" to those of "model", introducing
12742 * additional parameters if needed.
12744 __isl_give isl_basic_set
*isl_basic_set_align_params(
12745 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*model
)
12747 return isl_basic_map_align_params(bset
, model
);
12751 #define TYPE isl_map
12752 #define isl_map_drop_dims isl_map_drop
12753 #include "isl_drop_unused_params_templ.c"
12755 /* Drop all parameters not referenced by "set".
12757 __isl_give isl_set
*isl_set_drop_unused_params(
12758 __isl_take isl_set
*set
)
12760 return set_from_map(isl_map_drop_unused_params(set_to_map(set
)));
12764 #define TYPE isl_basic_map
12765 #define isl_basic_map_drop_dims isl_basic_map_drop
12766 #include "isl_drop_unused_params_templ.c"
12768 /* Drop all parameters not referenced by "bset".
12770 __isl_give isl_basic_set
*isl_basic_set_drop_unused_params(
12771 __isl_take isl_basic_set
*bset
)
12773 return bset_from_bmap(isl_basic_map_drop_unused_params(
12774 bset_to_bmap(bset
)));
12777 /* Given a tuple of identifiers "tuple" in a space that corresponds
12778 * to that of "set", if any of those identifiers appear as parameters
12779 * in "set", then equate those parameters with the corresponding
12780 * set dimensions and project out the parameters.
12781 * The result therefore has no such parameters.
12783 static __isl_give isl_set
*equate_params(__isl_take isl_set
*set
,
12784 __isl_keep isl_multi_id
*tuple
)
12788 isl_space
*set_space
, *tuple_space
;
12790 set_space
= isl_set_peek_space(set
);
12791 tuple_space
= isl_multi_id_peek_space(tuple
);
12792 if (isl_space_check_equal_tuples(tuple_space
, set_space
) < 0)
12793 return isl_set_free(set
);
12794 n
= isl_multi_id_size(tuple
);
12796 return isl_set_free(set
);
12797 for (i
= 0; i
< n
; ++i
) {
12801 id
= isl_multi_id_get_at(tuple
, i
);
12803 return isl_set_free(set
);
12804 pos
= isl_set_find_dim_by_id(set
, isl_dim_param
, id
);
12808 set
= isl_set_equate(set
, isl_dim_param
, pos
, isl_dim_set
, i
);
12809 set
= isl_set_project_out(set
, isl_dim_param
, pos
, 1);
12814 /* Bind the set dimensions of "set" to parameters with identifiers
12815 * specified by "tuple", living in the same space as "set".
12817 * If no parameters with these identifiers appear in "set" already,
12818 * then the set dimensions are simply reinterpreted as parameters.
12819 * Otherwise, the parameters are first equated to the corresponding
12822 __isl_give isl_set
*isl_set_bind(__isl_take isl_set
*set
,
12823 __isl_take isl_multi_id
*tuple
)
12827 set
= equate_params(set
, tuple
);
12828 space
= isl_set_get_space(set
);
12829 space
= isl_space_bind_set(space
, tuple
);
12830 isl_multi_id_free(tuple
);
12831 set
= isl_set_reset_space(set
, space
);
12836 /* Given a tuple of identifiers "tuple" in a space that corresponds
12837 * to the domain of "map", if any of those identifiers appear as parameters
12838 * in "map", then equate those parameters with the corresponding
12839 * input dimensions and project out the parameters.
12840 * The result therefore has no such parameters.
12842 static __isl_give isl_map
*map_equate_params(__isl_take isl_map
*map
,
12843 __isl_keep isl_multi_id
*tuple
)
12847 isl_space
*map_space
, *tuple_space
;
12849 map_space
= isl_map_peek_space(map
);
12850 tuple_space
= isl_multi_id_peek_space(tuple
);
12851 if (isl_space_check_domain_tuples(tuple_space
, map_space
) < 0)
12852 return isl_map_free(map
);
12853 n
= isl_multi_id_size(tuple
);
12855 return isl_map_free(map
);
12856 for (i
= 0; i
< n
; ++i
) {
12860 id
= isl_multi_id_get_at(tuple
, i
);
12862 return isl_map_free(map
);
12863 pos
= isl_map_find_dim_by_id(map
, isl_dim_param
, id
);
12867 map
= isl_map_equate(map
, isl_dim_param
, pos
, isl_dim_in
, i
);
12868 map
= isl_map_project_out(map
, isl_dim_param
, pos
, 1);
12873 /* Bind the input dimensions of "map" to parameters with identifiers
12874 * specified by "tuple", living in the domain space of "map".
12876 * If no parameters with these identifiers appear in "map" already,
12877 * then the input dimensions are simply reinterpreted as parameters.
12878 * Otherwise, the parameters are first equated to the corresponding
12879 * input dimensions.
12881 __isl_give isl_set
*isl_map_bind_domain(__isl_take isl_map
*map
,
12882 __isl_take isl_multi_id
*tuple
)
12887 map
= map_equate_params(map
, tuple
);
12888 space
= isl_map_get_space(map
);
12889 space
= isl_space_bind_map_domain(space
, tuple
);
12890 isl_multi_id_free(tuple
);
12891 set
= set_from_map(isl_map_reset_space(map
, space
));
12896 /* Bind the output dimensions of "map" to parameters with identifiers
12897 * specified by "tuple", living in the range space of "map".
12899 * Since binding is more easily implemented on the domain,
12900 * bind the input dimensions of the inverse of "map".
12902 __isl_give isl_set
*isl_map_bind_range(__isl_take isl_map
*map
,
12903 __isl_take isl_multi_id
*tuple
)
12905 return isl_map_bind_domain(isl_map_reverse(map
), tuple
);
12908 /* Insert a domain corresponding to "tuple"
12909 * into the nullary or unary relation "set".
12910 * The result has an extra initial tuple and is therefore
12911 * either a unary or binary relation.
12912 * Any parameters with identifiers in "tuple" are reinterpreted
12913 * as the corresponding domain dimensions.
12915 static __isl_give isl_map
*unbind_params_insert_domain(
12916 __isl_take isl_set
*set
, __isl_take isl_multi_id
*tuple
)
12921 space
= isl_set_peek_space(set
);
12922 r
= isl_reordering_unbind_params_insert_domain(space
, tuple
);
12923 isl_multi_id_free(tuple
);
12925 return isl_map_realign(set_to_map(set
), r
);
12928 /* Construct a set with "tuple" as domain from the parameter domain "set".
12929 * Any parameters with identifiers in "tuple" are reinterpreted
12930 * as the corresponding set dimensions.
12932 __isl_give isl_set
*isl_set_unbind_params(__isl_take isl_set
*set
,
12933 __isl_take isl_multi_id
*tuple
)
12935 isl_bool is_params
;
12937 is_params
= isl_set_is_params(set
);
12939 set
= isl_set_free(set
);
12940 else if (!is_params
)
12941 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
12942 "expecting parameter domain", set
= isl_set_free(set
));
12943 return set_from_map(unbind_params_insert_domain(set
, tuple
));
12946 /* Check that "set" is a proper set, i.e., that it is not a parameter domain.
12948 static isl_stat
isl_set_check_is_set(__isl_keep isl_set
*set
)
12950 isl_bool is_params
;
12952 is_params
= isl_set_is_params(set
);
12954 return isl_stat_error
;
12955 else if (is_params
)
12956 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
12957 "expecting proper set", return isl_stat_error
);
12959 return isl_stat_ok
;
12962 /* Construct a map with "domain" as domain and "set" as range.
12963 * Any parameters with identifiers in "domain" are reinterpreted
12964 * as the corresponding domain dimensions.
12966 __isl_give isl_map
*isl_set_unbind_params_insert_domain(
12967 __isl_take isl_set
*set
, __isl_take isl_multi_id
*domain
)
12969 if (isl_set_check_is_set(set
) < 0)
12970 set
= isl_set_free(set
);
12971 return unbind_params_insert_domain(set
, domain
);
12974 /* Construct a map with "domain" as domain and "set" as range.
12976 __isl_give isl_map
*isl_set_insert_domain(__isl_take isl_set
*set
,
12977 __isl_take isl_space
*domain
)
12983 if (isl_set_check_is_set(set
) < 0 || isl_space_check_is_set(domain
) < 0)
12984 domain
= isl_space_free(domain
);
12985 dim
= isl_space_dim(domain
, isl_dim_set
);
12987 domain
= isl_space_free(domain
);
12988 space
= isl_set_get_space(set
);
12989 domain
= isl_space_replace_params(domain
, space
);
12990 space
= isl_space_map_from_domain_and_range(domain
, space
);
12992 map
= isl_map_from_range(set
);
12993 map
= isl_map_add_dims(map
, isl_dim_in
, dim
);
12994 map
= isl_map_reset_space(map
, space
);
12999 __isl_give isl_mat
*isl_basic_map_equalities_matrix(
13000 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
13001 enum isl_dim_type c2
, enum isl_dim_type c3
,
13002 enum isl_dim_type c4
, enum isl_dim_type c5
)
13004 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
13005 struct isl_mat
*mat
;
13010 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
13013 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_eq
, total
+ 1);
13016 for (i
= 0; i
< bmap
->n_eq
; ++i
)
13017 for (j
= 0, pos
= 0; j
< 5; ++j
) {
13018 int off
= isl_basic_map_offset(bmap
, c
[j
]);
13019 isl_size dim
= isl_basic_map_dim(bmap
, c
[j
]);
13021 return isl_mat_free(mat
);
13022 for (k
= 0; k
< dim
; ++k
) {
13023 isl_int_set(mat
->row
[i
][pos
],
13024 bmap
->eq
[i
][off
+ k
]);
13032 __isl_give isl_mat
*isl_basic_map_inequalities_matrix(
13033 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
13034 enum isl_dim_type c2
, enum isl_dim_type c3
,
13035 enum isl_dim_type c4
, enum isl_dim_type c5
)
13037 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
13038 struct isl_mat
*mat
;
13043 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
13046 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_ineq
, total
+ 1);
13049 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
13050 for (j
= 0, pos
= 0; j
< 5; ++j
) {
13051 int off
= isl_basic_map_offset(bmap
, c
[j
]);
13052 isl_size dim
= isl_basic_map_dim(bmap
, c
[j
]);
13054 return isl_mat_free(mat
);
13055 for (k
= 0; k
< dim
; ++k
) {
13056 isl_int_set(mat
->row
[i
][pos
],
13057 bmap
->ineq
[i
][off
+ k
]);
13065 __isl_give isl_basic_map
*isl_basic_map_from_constraint_matrices(
13066 __isl_take isl_space
*space
,
13067 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
13068 enum isl_dim_type c2
, enum isl_dim_type c3
,
13069 enum isl_dim_type c4
, enum isl_dim_type c5
)
13071 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
13072 isl_basic_map
*bmap
= NULL
;
13079 dim
= isl_space_dim(space
, isl_dim_all
);
13080 if (dim
< 0 || !eq
|| !ineq
)
13083 if (eq
->n_col
!= ineq
->n_col
)
13084 isl_die(space
->ctx
, isl_error_invalid
,
13085 "equalities and inequalities matrices should have "
13086 "same number of columns", goto error
);
13090 if (eq
->n_col
< total
)
13091 isl_die(space
->ctx
, isl_error_invalid
,
13092 "number of columns too small", goto error
);
13094 extra
= eq
->n_col
- total
;
13096 bmap
= isl_basic_map_alloc_space(isl_space_copy(space
), extra
,
13097 eq
->n_row
, ineq
->n_row
);
13100 for (i
= 0; i
< extra
; ++i
) {
13101 k
= isl_basic_map_alloc_div(bmap
);
13104 isl_int_set_si(bmap
->div
[k
][0], 0);
13106 for (i
= 0; i
< eq
->n_row
; ++i
) {
13107 l
= isl_basic_map_alloc_equality(bmap
);
13110 for (j
= 0, pos
= 0; j
< 5; ++j
) {
13111 int off
= isl_basic_map_offset(bmap
, c
[j
]);
13112 isl_size dim
= isl_basic_map_dim(bmap
, c
[j
]);
13115 for (k
= 0; k
< dim
; ++k
) {
13116 isl_int_set(bmap
->eq
[l
][off
+ k
],
13122 for (i
= 0; i
< ineq
->n_row
; ++i
) {
13123 l
= isl_basic_map_alloc_inequality(bmap
);
13126 for (j
= 0, pos
= 0; j
< 5; ++j
) {
13127 int off
= isl_basic_map_offset(bmap
, c
[j
]);
13128 isl_size dim
= isl_basic_map_dim(bmap
, c
[j
]);
13131 for (k
= 0; k
< dim
; ++k
) {
13132 isl_int_set(bmap
->ineq
[l
][off
+ k
],
13133 ineq
->row
[i
][pos
]);
13139 isl_space_free(space
);
13141 isl_mat_free(ineq
);
13143 bmap
= isl_basic_map_simplify(bmap
);
13144 return isl_basic_map_finalize(bmap
);
13146 isl_space_free(space
);
13148 isl_mat_free(ineq
);
13149 isl_basic_map_free(bmap
);
13153 __isl_give isl_mat
*isl_basic_set_equalities_matrix(
13154 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
13155 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
13157 return isl_basic_map_equalities_matrix(bset_to_bmap(bset
),
13158 c1
, c2
, c3
, c4
, isl_dim_in
);
13161 __isl_give isl_mat
*isl_basic_set_inequalities_matrix(
13162 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
13163 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
13165 return isl_basic_map_inequalities_matrix(bset_to_bmap(bset
),
13166 c1
, c2
, c3
, c4
, isl_dim_in
);
13169 __isl_give isl_basic_set
*isl_basic_set_from_constraint_matrices(
13170 __isl_take isl_space
*space
,
13171 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
13172 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
13174 isl_basic_map
*bmap
;
13175 bmap
= isl_basic_map_from_constraint_matrices(space
, eq
, ineq
,
13176 c1
, c2
, c3
, c4
, isl_dim_in
);
13177 return bset_from_bmap(bmap
);
13180 isl_bool
isl_basic_map_can_zip(__isl_keep isl_basic_map
*bmap
)
13183 return isl_bool_error
;
13185 return isl_space_can_zip(bmap
->dim
);
13188 isl_bool
isl_map_can_zip(__isl_keep isl_map
*map
)
13191 return isl_bool_error
;
13193 return isl_space_can_zip(map
->dim
);
13196 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
13197 * (A -> C) -> (B -> D).
13199 __isl_give isl_basic_map
*isl_basic_map_zip(__isl_take isl_basic_map
*bmap
)
13209 if (!isl_basic_map_can_zip(bmap
))
13210 isl_die(bmap
->ctx
, isl_error_invalid
,
13211 "basic map cannot be zipped", goto error
);
13212 n_in
= isl_space_dim(bmap
->dim
->nested
[0], isl_dim_in
);
13213 n1
= isl_space_dim(bmap
->dim
->nested
[0], isl_dim_out
);
13214 n2
= isl_space_dim(bmap
->dim
->nested
[1], isl_dim_in
);
13215 if (n_in
< 0 || n1
< 0 || n2
< 0)
13216 return isl_basic_map_free(bmap
);
13217 pos
= isl_basic_map_offset(bmap
, isl_dim_in
) + n_in
;
13218 bmap
= isl_basic_map_cow(bmap
);
13219 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
13222 bmap
->dim
= isl_space_zip(bmap
->dim
);
13225 bmap
= isl_basic_map_mark_final(bmap
);
13228 isl_basic_map_free(bmap
);
13232 /* Given a map (A -> B) -> (C -> D), return the corresponding map
13233 * (A -> C) -> (B -> D).
13235 __isl_give isl_map
*isl_map_zip(__isl_take isl_map
*map
)
13240 if (!isl_map_can_zip(map
))
13241 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be zipped",
13244 return isl_map_transform(map
, &isl_space_zip
, &isl_basic_map_zip
);
13250 /* Can we apply isl_basic_map_curry to "bmap"?
13251 * That is, does it have a nested relation in its domain?
13253 isl_bool
isl_basic_map_can_curry(__isl_keep isl_basic_map
*bmap
)
13256 return isl_bool_error
;
13258 return isl_space_can_curry(bmap
->dim
);
13261 /* Can we apply isl_map_curry to "map"?
13262 * That is, does it have a nested relation in its domain?
13264 isl_bool
isl_map_can_curry(__isl_keep isl_map
*map
)
13267 return isl_bool_error
;
13269 return isl_space_can_curry(map
->dim
);
13272 /* Given a basic map (A -> B) -> C, return the corresponding basic map
13275 __isl_give isl_basic_map
*isl_basic_map_curry(__isl_take isl_basic_map
*bmap
)
13281 if (!isl_basic_map_can_curry(bmap
))
13282 isl_die(bmap
->ctx
, isl_error_invalid
,
13283 "basic map cannot be curried", goto error
);
13284 bmap
= isl_basic_map_cow(bmap
);
13287 bmap
->dim
= isl_space_curry(bmap
->dim
);
13290 bmap
= isl_basic_map_mark_final(bmap
);
13293 isl_basic_map_free(bmap
);
13297 /* Given a map (A -> B) -> C, return the corresponding map
13300 __isl_give isl_map
*isl_map_curry(__isl_take isl_map
*map
)
13302 return isl_map_change_space(map
, &isl_map_can_curry
,
13303 "map cannot be curried", &isl_space_curry
);
13306 /* Can isl_map_range_curry be applied to "map"?
13307 * That is, does it have a nested relation in its range,
13308 * the domain of which is itself a nested relation?
13310 isl_bool
isl_map_can_range_curry(__isl_keep isl_map
*map
)
13313 return isl_bool_error
;
13315 return isl_space_can_range_curry(map
->dim
);
13318 /* Given a map A -> ((B -> C) -> D), return the corresponding map
13319 * A -> (B -> (C -> D)).
13321 __isl_give isl_map
*isl_map_range_curry(__isl_take isl_map
*map
)
13323 return isl_map_change_space(map
, &isl_map_can_range_curry
,
13324 "map range cannot be curried",
13325 &isl_space_range_curry
);
13328 /* Can we apply isl_basic_map_uncurry to "bmap"?
13329 * That is, does it have a nested relation in its domain?
13331 isl_bool
isl_basic_map_can_uncurry(__isl_keep isl_basic_map
*bmap
)
13334 return isl_bool_error
;
13336 return isl_space_can_uncurry(bmap
->dim
);
13339 /* Can we apply isl_map_uncurry to "map"?
13340 * That is, does it have a nested relation in its domain?
13342 isl_bool
isl_map_can_uncurry(__isl_keep isl_map
*map
)
13345 return isl_bool_error
;
13347 return isl_space_can_uncurry(map
->dim
);
13350 /* Given a basic map A -> (B -> C), return the corresponding basic map
13353 __isl_give isl_basic_map
*isl_basic_map_uncurry(__isl_take isl_basic_map
*bmap
)
13359 if (!isl_basic_map_can_uncurry(bmap
))
13360 isl_die(bmap
->ctx
, isl_error_invalid
,
13361 "basic map cannot be uncurried",
13362 return isl_basic_map_free(bmap
));
13363 bmap
= isl_basic_map_cow(bmap
);
13366 bmap
->dim
= isl_space_uncurry(bmap
->dim
);
13368 return isl_basic_map_free(bmap
);
13369 bmap
= isl_basic_map_mark_final(bmap
);
13373 /* Given a map A -> (B -> C), return the corresponding map
13376 __isl_give isl_map
*isl_map_uncurry(__isl_take isl_map
*map
)
13378 return isl_map_change_space(map
, &isl_map_can_uncurry
,
13379 "map cannot be uncurried", &isl_space_uncurry
);
13382 __isl_give isl_set
*isl_set_equate(__isl_take isl_set
*set
,
13383 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13385 return isl_map_equate(set
, type1
, pos1
, type2
, pos2
);
13388 /* Construct a basic map where the given dimensions are equal to each other.
13390 static __isl_give isl_basic_map
*equator(__isl_take isl_space
*space
,
13391 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13393 isl_basic_map
*bmap
= NULL
;
13397 total
= isl_space_dim(space
, isl_dim_all
);
13399 isl_space_check_range(space
, type1
, pos1
, 1) < 0 ||
13400 isl_space_check_range(space
, type2
, pos2
, 1) < 0)
13403 if (type1
== type2
&& pos1
== pos2
)
13404 return isl_basic_map_universe(space
);
13406 bmap
= isl_basic_map_alloc_space(isl_space_copy(space
), 0, 1, 0);
13407 i
= isl_basic_map_alloc_equality(bmap
);
13410 isl_seq_clr(bmap
->eq
[i
], 1 + total
);
13411 pos1
+= isl_basic_map_offset(bmap
, type1
);
13412 pos2
+= isl_basic_map_offset(bmap
, type2
);
13413 isl_int_set_si(bmap
->eq
[i
][pos1
], -1);
13414 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
13415 bmap
= isl_basic_map_finalize(bmap
);
13416 isl_space_free(space
);
13419 isl_space_free(space
);
13420 isl_basic_map_free(bmap
);
13424 /* Add a constraint imposing that the given two dimensions are equal.
13426 __isl_give isl_basic_map
*isl_basic_map_equate(__isl_take isl_basic_map
*bmap
,
13427 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13431 eq
= equator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
13433 bmap
= isl_basic_map_intersect(bmap
, eq
);
13438 /* Add a constraint imposing that the given two dimensions are equal.
13440 __isl_give isl_map
*isl_map_equate(__isl_take isl_map
*map
,
13441 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13443 isl_basic_map
*bmap
;
13445 bmap
= equator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
13447 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
13452 /* Add a constraint imposing that the given two dimensions have opposite values.
13454 __isl_give isl_map
*isl_map_oppose(__isl_take isl_map
*map
,
13455 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13457 isl_basic_map
*bmap
= NULL
;
13461 if (isl_map_check_range(map
, type1
, pos1
, 1) < 0)
13462 return isl_map_free(map
);
13463 if (isl_map_check_range(map
, type2
, pos2
, 1) < 0)
13464 return isl_map_free(map
);
13466 total
= isl_map_dim(map
, isl_dim_all
);
13468 return isl_map_free(map
);
13469 bmap
= isl_basic_map_alloc_space(isl_map_get_space(map
), 0, 1, 0);
13470 i
= isl_basic_map_alloc_equality(bmap
);
13473 isl_seq_clr(bmap
->eq
[i
], 1 + total
);
13474 pos1
+= isl_basic_map_offset(bmap
, type1
);
13475 pos2
+= isl_basic_map_offset(bmap
, type2
);
13476 isl_int_set_si(bmap
->eq
[i
][pos1
], 1);
13477 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
13478 bmap
= isl_basic_map_finalize(bmap
);
13480 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
13484 isl_basic_map_free(bmap
);
13489 /* Construct a constraint imposing that the value of the first dimension is
13490 * greater than or equal to that of the second.
13492 static __isl_give isl_constraint
*constraint_order_ge(
13493 __isl_take isl_space
*space
, enum isl_dim_type type1
, int pos1
,
13494 enum isl_dim_type type2
, int pos2
)
13498 if (isl_space_check_range(space
, type1
, pos1
, 1) < 0 ||
13499 isl_space_check_range(space
, type2
, pos2
, 1) < 0)
13500 space
= isl_space_free(space
);
13504 c
= isl_constraint_alloc_inequality(isl_local_space_from_space(space
));
13506 if (type1
== type2
&& pos1
== pos2
)
13509 c
= isl_constraint_set_coefficient_si(c
, type1
, pos1
, 1);
13510 c
= isl_constraint_set_coefficient_si(c
, type2
, pos2
, -1);
13515 /* Add a constraint imposing that the value of the first dimension is
13516 * greater than or equal to that of the second.
13518 __isl_give isl_basic_map
*isl_basic_map_order_ge(__isl_take isl_basic_map
*bmap
,
13519 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13524 if (type1
== type2
&& pos1
== pos2
)
13526 space
= isl_basic_map_get_space(bmap
);
13527 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
13528 bmap
= isl_basic_map_add_constraint(bmap
, c
);
13533 /* Add a constraint imposing that the value of the first dimension is
13534 * greater than or equal to that of the second.
13536 __isl_give isl_map
*isl_map_order_ge(__isl_take isl_map
*map
,
13537 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13542 if (type1
== type2
&& pos1
== pos2
)
13544 space
= isl_map_get_space(map
);
13545 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
13546 map
= isl_map_add_constraint(map
, c
);
13551 /* Add a constraint imposing that the value of the first dimension is
13552 * less than or equal to that of the second.
13554 __isl_give isl_map
*isl_map_order_le(__isl_take isl_map
*map
,
13555 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13557 return isl_map_order_ge(map
, type2
, pos2
, type1
, pos1
);
13560 /* Construct a basic map where the value of the first dimension is
13561 * greater than that of the second.
13563 static __isl_give isl_basic_map
*greator(__isl_take isl_space
*space
,
13564 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13566 isl_basic_map
*bmap
= NULL
;
13570 if (isl_space_check_range(space
, type1
, pos1
, 1) < 0 ||
13571 isl_space_check_range(space
, type2
, pos2
, 1) < 0)
13574 if (type1
== type2
&& pos1
== pos2
)
13575 return isl_basic_map_empty(space
);
13577 bmap
= isl_basic_map_alloc_space(space
, 0, 0, 1);
13578 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
13579 i
= isl_basic_map_alloc_inequality(bmap
);
13580 if (total
< 0 || i
< 0)
13581 return isl_basic_map_free(bmap
);
13582 isl_seq_clr(bmap
->ineq
[i
], 1 + total
);
13583 pos1
+= isl_basic_map_offset(bmap
, type1
);
13584 pos2
+= isl_basic_map_offset(bmap
, type2
);
13585 isl_int_set_si(bmap
->ineq
[i
][pos1
], 1);
13586 isl_int_set_si(bmap
->ineq
[i
][pos2
], -1);
13587 isl_int_set_si(bmap
->ineq
[i
][0], -1);
13588 bmap
= isl_basic_map_finalize(bmap
);
13592 isl_space_free(space
);
13593 isl_basic_map_free(bmap
);
13597 /* Add a constraint imposing that the value of the first dimension is
13598 * greater than that of the second.
13600 __isl_give isl_basic_map
*isl_basic_map_order_gt(__isl_take isl_basic_map
*bmap
,
13601 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13605 gt
= greator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
13607 bmap
= isl_basic_map_intersect(bmap
, gt
);
13612 /* Add a constraint imposing that the value of the first dimension is
13613 * greater than that of the second.
13615 __isl_give isl_map
*isl_map_order_gt(__isl_take isl_map
*map
,
13616 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13618 isl_basic_map
*bmap
;
13620 bmap
= greator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
13622 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
13627 /* Add a constraint imposing that the value of the first dimension is
13628 * smaller than that of the second.
13630 __isl_give isl_map
*isl_map_order_lt(__isl_take isl_map
*map
,
13631 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13633 return isl_map_order_gt(map
, type2
, pos2
, type1
, pos1
);
13636 __isl_give isl_aff
*isl_basic_map_get_div(__isl_keep isl_basic_map
*bmap
,
13640 isl_local_space
*ls
;
13645 if (!isl_basic_map_divs_known(bmap
))
13646 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
13647 "some divs are unknown", return NULL
);
13649 ls
= isl_basic_map_get_local_space(bmap
);
13650 div
= isl_local_space_get_div(ls
, pos
);
13651 isl_local_space_free(ls
);
13656 __isl_give isl_aff
*isl_basic_set_get_div(__isl_keep isl_basic_set
*bset
,
13659 return isl_basic_map_get_div(bset
, pos
);
13662 /* Plug in "subs" for set dimension "pos" of "set".
13664 __isl_give isl_set
*isl_set_substitute(__isl_take isl_set
*set
,
13665 unsigned pos
, __isl_keep isl_aff
*subs
)
13669 if (set
&& isl_set_plain_is_empty(set
))
13672 ma
= isl_multi_aff_identity_on_domain_space(isl_set_get_space(set
));
13673 ma
= isl_multi_aff_set_aff(ma
, pos
, isl_aff_copy(subs
));
13674 return isl_set_preimage_multi_aff(set
, ma
);
13677 /* Check if the range of "ma" is compatible with the domain or range
13678 * (depending on "type") of "bmap".
13680 static isl_stat
check_basic_map_compatible_range_multi_aff(
13681 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type type
,
13682 __isl_keep isl_multi_aff
*ma
)
13685 isl_space
*ma_space
;
13687 ma_space
= isl_multi_aff_get_space(ma
);
13689 m
= isl_space_has_equal_params(bmap
->dim
, ma_space
);
13693 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
13694 "parameters don't match", goto error
);
13695 m
= isl_space_tuple_is_equal(bmap
->dim
, type
, ma_space
, isl_dim_out
);
13699 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
13700 "spaces don't match", goto error
);
13702 isl_space_free(ma_space
);
13703 return isl_stat_ok
;
13705 isl_space_free(ma_space
);
13706 return isl_stat_error
;
13709 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
13710 * coefficients before the transformed range of dimensions,
13711 * the "n_after" coefficients after the transformed range of dimensions
13712 * and the coefficients of the other divs in "bmap".
13714 static __isl_give isl_basic_map
*set_ma_divs(__isl_take isl_basic_map
*bmap
,
13715 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
, int n_div
)
13720 isl_local_space
*ls
;
13725 ls
= isl_aff_get_domain_local_space(ma
->u
.p
[0]);
13726 n_param
= isl_local_space_dim(ls
, isl_dim_param
);
13727 n_set
= isl_local_space_dim(ls
, isl_dim_set
);
13728 if (n_param
< 0 || n_set
< 0)
13729 return isl_basic_map_free(bmap
);
13731 for (i
= 0; i
< n_div
; ++i
) {
13732 int o_bmap
= 0, o_ls
= 0;
13734 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], 1 + 1 + n_param
);
13735 o_bmap
+= 1 + 1 + n_param
;
13736 o_ls
+= 1 + 1 + n_param
;
13737 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_before
);
13738 o_bmap
+= n_before
;
13739 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
13740 ls
->div
->row
[i
] + o_ls
, n_set
);
13743 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_after
);
13745 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
13746 ls
->div
->row
[i
] + o_ls
, n_div
);
13749 isl_seq_clr(bmap
->div
[i
] + o_bmap
, bmap
->n_div
- n_div
);
13750 bmap
= isl_basic_map_add_div_constraints(bmap
, i
);
13755 isl_local_space_free(ls
);
13758 isl_local_space_free(ls
);
13759 return isl_basic_map_free(bmap
);
13762 /* How many stride constraints does "ma" enforce?
13763 * That is, how many of the affine expressions have a denominator
13764 * different from one?
13766 static int multi_aff_strides(__isl_keep isl_multi_aff
*ma
)
13771 for (i
= 0; i
< ma
->n
; ++i
)
13772 if (!isl_int_is_one(ma
->u
.p
[i
]->v
->el
[0]))
13778 /* For each affine expression in ma of the form
13780 * x_i = (f_i y + h_i)/m_i
13782 * with m_i different from one, add a constraint to "bmap"
13785 * f_i y + h_i = m_i alpha_i
13787 * with alpha_i an additional existentially quantified variable.
13789 * The input variables of "ma" correspond to a subset of the variables
13790 * of "bmap". There are "n_before" variables in "bmap" before this
13791 * subset and "n_after" variables after this subset.
13792 * The integer divisions of the affine expressions in "ma" are assumed
13793 * to have been aligned. There are "n_div_ma" of them and
13794 * they appear first in "bmap", straight after the "n_after" variables.
13796 static __isl_give isl_basic_map
*add_ma_strides(
13797 __isl_take isl_basic_map
*bmap
, __isl_keep isl_multi_aff
*ma
,
13798 int n_before
, int n_after
, int n_div_ma
)
13806 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
13807 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
13808 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
13809 if (total
< 0 || n_param
< 0 || n_in
< 0)
13810 return isl_basic_map_free(bmap
);
13811 for (i
= 0; i
< ma
->n
; ++i
) {
13812 int o_bmap
= 0, o_ma
= 1;
13814 if (isl_int_is_one(ma
->u
.p
[i
]->v
->el
[0]))
13816 div
= isl_basic_map_alloc_div(bmap
);
13817 k
= isl_basic_map_alloc_equality(bmap
);
13818 if (div
< 0 || k
< 0)
13820 isl_int_set_si(bmap
->div
[div
][0], 0);
13821 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
13822 ma
->u
.p
[i
]->v
->el
+ o_ma
, 1 + n_param
);
13823 o_bmap
+= 1 + n_param
;
13824 o_ma
+= 1 + n_param
;
13825 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_before
);
13826 o_bmap
+= n_before
;
13827 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
13828 ma
->u
.p
[i
]->v
->el
+ o_ma
, n_in
);
13831 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_after
);
13833 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
13834 ma
->u
.p
[i
]->v
->el
+ o_ma
, n_div_ma
);
13835 o_bmap
+= n_div_ma
;
13837 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, 1 + total
- o_bmap
);
13838 isl_int_neg(bmap
->eq
[k
][1 + total
], ma
->u
.p
[i
]->v
->el
[0]);
13844 isl_basic_map_free(bmap
);
13848 /* Replace the domain or range space (depending on "type) of "space" by "set".
13850 static __isl_give isl_space
*isl_space_set(__isl_take isl_space
*space
,
13851 enum isl_dim_type type
, __isl_take isl_space
*set
)
13853 if (type
== isl_dim_in
) {
13854 space
= isl_space_range(space
);
13855 space
= isl_space_map_from_domain_and_range(set
, space
);
13857 space
= isl_space_domain(space
);
13858 space
= isl_space_map_from_domain_and_range(space
, set
);
13864 /* Compute the preimage of the domain or range (depending on "type")
13865 * of "bmap" under the function represented by "ma".
13866 * In other words, plug in "ma" in the domain or range of "bmap".
13867 * The result is a basic map that lives in the same space as "bmap"
13868 * except that the domain or range has been replaced by
13869 * the domain space of "ma".
13871 * If bmap is represented by
13873 * A(p) + S u + B x + T v + C(divs) >= 0,
13875 * where u and x are input and output dimensions if type == isl_dim_out
13876 * while x and v are input and output dimensions if type == isl_dim_in,
13877 * and ma is represented by
13879 * x = D(p) + F(y) + G(divs')
13881 * then the result is
13883 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
13885 * The divs in the input set are similarly adjusted.
13888 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
13892 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
13893 * B_i G(divs') + c_i(divs))/n_i)
13895 * If bmap is not a rational map and if F(y) involves any denominators
13897 * x_i = (f_i y + h_i)/m_i
13899 * then additional constraints are added to ensure that we only
13900 * map back integer points. That is we enforce
13902 * f_i y + h_i = m_i alpha_i
13904 * with alpha_i an additional existentially quantified variable.
13906 * We first copy over the divs from "ma".
13907 * Then we add the modified constraints and divs from "bmap".
13908 * Finally, we add the stride constraints, if needed.
13910 __isl_give isl_basic_map
*isl_basic_map_preimage_multi_aff(
13911 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
13912 __isl_take isl_multi_aff
*ma
)
13916 isl_basic_map
*res
= NULL
;
13917 isl_size n_before
, n_after
, n_div_bmap
, n_div_ma
;
13918 isl_int f
, c1
, c2
, g
;
13927 ma
= isl_multi_aff_align_divs(ma
);
13930 if (check_basic_map_compatible_range_multi_aff(bmap
, type
, ma
) < 0)
13933 if (type
== isl_dim_in
) {
13935 n_after
= isl_basic_map_dim(bmap
, isl_dim_out
);
13937 n_before
= isl_basic_map_dim(bmap
, isl_dim_in
);
13940 n_div_bmap
= isl_basic_map_dim(bmap
, isl_dim_div
);
13941 n_div_ma
= ma
->n
? isl_aff_dim(ma
->u
.p
[0], isl_dim_div
) : 0;
13942 if (n_before
< 0 || n_after
< 0 || n_div_bmap
< 0 || n_div_ma
< 0)
13945 space
= isl_multi_aff_get_domain_space(ma
);
13946 space
= isl_space_set(isl_basic_map_get_space(bmap
), type
, space
);
13947 rational
= isl_basic_map_is_rational(bmap
);
13948 strides
= rational
? 0 : multi_aff_strides(ma
);
13949 res
= isl_basic_map_alloc_space(space
, n_div_ma
+ n_div_bmap
+ strides
,
13950 bmap
->n_eq
+ strides
, bmap
->n_ineq
+ 2 * n_div_ma
);
13952 res
= isl_basic_map_set_rational(res
);
13954 for (i
= 0; i
< n_div_ma
+ n_div_bmap
; ++i
)
13955 if (isl_basic_map_alloc_div(res
) < 0)
13958 res
= set_ma_divs(res
, ma
, n_before
, n_after
, n_div_ma
);
13962 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
13963 k
= isl_basic_map_alloc_equality(res
);
13966 if (isl_seq_preimage(res
->eq
[k
], bmap
->eq
[i
], ma
, n_before
,
13967 n_after
, n_div_ma
, n_div_bmap
,
13968 f
, c1
, c2
, g
, 0) < 0)
13972 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
13973 k
= isl_basic_map_alloc_inequality(res
);
13976 if (isl_seq_preimage(res
->ineq
[k
], bmap
->ineq
[i
], ma
, n_before
,
13977 n_after
, n_div_ma
, n_div_bmap
,
13978 f
, c1
, c2
, g
, 0) < 0)
13982 for (i
= 0; i
< bmap
->n_div
; ++i
) {
13983 if (isl_int_is_zero(bmap
->div
[i
][0])) {
13984 isl_int_set_si(res
->div
[n_div_ma
+ i
][0], 0);
13987 if (isl_seq_preimage(res
->div
[n_div_ma
+ i
], bmap
->div
[i
], ma
,
13988 n_before
, n_after
, n_div_ma
, n_div_bmap
,
13989 f
, c1
, c2
, g
, 1) < 0)
13994 res
= add_ma_strides(res
, ma
, n_before
, n_after
, n_div_ma
);
14000 isl_basic_map_free(bmap
);
14001 isl_multi_aff_free(ma
);
14002 res
= isl_basic_map_simplify(res
);
14003 return isl_basic_map_finalize(res
);
14009 isl_basic_map_free(bmap
);
14010 isl_multi_aff_free(ma
);
14011 isl_basic_map_free(res
);
14015 /* Compute the preimage of "bset" under the function represented by "ma".
14016 * In other words, plug in "ma" in "bset". The result is a basic set
14017 * that lives in the domain space of "ma".
14019 __isl_give isl_basic_set
*isl_basic_set_preimage_multi_aff(
14020 __isl_take isl_basic_set
*bset
, __isl_take isl_multi_aff
*ma
)
14022 return isl_basic_map_preimage_multi_aff(bset
, isl_dim_set
, ma
);
14025 /* Compute the preimage of the domain of "bmap" under the function
14026 * represented by "ma".
14027 * In other words, plug in "ma" in the domain of "bmap".
14028 * The result is a basic map that lives in the same space as "bmap"
14029 * except that the domain has been replaced by the domain space of "ma".
14031 __isl_give isl_basic_map
*isl_basic_map_preimage_domain_multi_aff(
14032 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
14034 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_in
, ma
);
14037 /* Compute the preimage of the range of "bmap" under the function
14038 * represented by "ma".
14039 * In other words, plug in "ma" in the range of "bmap".
14040 * The result is a basic map that lives in the same space as "bmap"
14041 * except that the range has been replaced by the domain space of "ma".
14043 __isl_give isl_basic_map
*isl_basic_map_preimage_range_multi_aff(
14044 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
14046 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_out
, ma
);
14049 /* Check if the range of "ma" is compatible with the domain or range
14050 * (depending on "type") of "map".
14051 * Return isl_stat_error if anything is wrong.
14053 static isl_stat
check_map_compatible_range_multi_aff(
14054 __isl_keep isl_map
*map
, enum isl_dim_type type
,
14055 __isl_keep isl_multi_aff
*ma
)
14058 isl_space
*ma_space
;
14060 ma_space
= isl_multi_aff_get_space(ma
);
14061 m
= isl_map_space_tuple_is_equal(map
, type
, ma_space
, isl_dim_out
);
14062 isl_space_free(ma_space
);
14064 return isl_stat_error
;
14066 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
14067 "spaces don't match", return isl_stat_error
);
14068 return isl_stat_ok
;
14071 /* Compute the preimage of the domain or range (depending on "type")
14072 * of "map" under the function represented by "ma".
14073 * In other words, plug in "ma" in the domain or range of "map".
14074 * The result is a map that lives in the same space as "map"
14075 * except that the domain or range has been replaced by
14076 * the domain space of "ma".
14078 * The parameters are assumed to have been aligned.
14080 static __isl_give isl_map
*map_preimage_multi_aff(__isl_take isl_map
*map
,
14081 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
14086 map
= isl_map_cow(map
);
14087 ma
= isl_multi_aff_align_divs(ma
);
14090 if (check_map_compatible_range_multi_aff(map
, type
, ma
) < 0)
14093 for (i
= 0; i
< map
->n
; ++i
) {
14094 map
->p
[i
] = isl_basic_map_preimage_multi_aff(map
->p
[i
], type
,
14095 isl_multi_aff_copy(ma
));
14100 space
= isl_multi_aff_get_domain_space(ma
);
14101 space
= isl_space_set(isl_map_get_space(map
), type
, space
);
14103 isl_space_free(isl_map_take_space(map
));
14104 map
= isl_map_restore_space(map
, space
);
14108 isl_multi_aff_free(ma
);
14110 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
14111 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
14114 isl_multi_aff_free(ma
);
14119 /* Compute the preimage of the domain or range (depending on "type")
14120 * of "map" under the function represented by "ma".
14121 * In other words, plug in "ma" in the domain or range of "map".
14122 * The result is a map that lives in the same space as "map"
14123 * except that the domain or range has been replaced by
14124 * the domain space of "ma".
14126 __isl_give isl_map
*isl_map_preimage_multi_aff(__isl_take isl_map
*map
,
14127 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
14134 aligned
= isl_map_space_has_equal_params(map
, ma
->space
);
14138 return map_preimage_multi_aff(map
, type
, ma
);
14140 if (isl_map_check_named_params(map
) < 0)
14142 if (!isl_space_has_named_params(ma
->space
))
14143 isl_die(map
->ctx
, isl_error_invalid
,
14144 "unaligned unnamed parameters", goto error
);
14145 map
= isl_map_align_params(map
, isl_multi_aff_get_space(ma
));
14146 ma
= isl_multi_aff_align_params(ma
, isl_map_get_space(map
));
14148 return map_preimage_multi_aff(map
, type
, ma
);
14150 isl_multi_aff_free(ma
);
14151 return isl_map_free(map
);
14154 /* Compute the preimage of "set" under the function represented by "ma".
14155 * In other words, plug in "ma" in "set". The result is a set
14156 * that lives in the domain space of "ma".
14158 __isl_give isl_set
*isl_set_preimage_multi_aff(__isl_take isl_set
*set
,
14159 __isl_take isl_multi_aff
*ma
)
14161 return isl_map_preimage_multi_aff(set
, isl_dim_set
, ma
);
14164 /* Compute the preimage of the domain of "map" under the function
14165 * represented by "ma".
14166 * In other words, plug in "ma" in the domain of "map".
14167 * The result is a map that lives in the same space as "map"
14168 * except that the domain has been replaced by the domain space of "ma".
14170 __isl_give isl_map
*isl_map_preimage_domain_multi_aff(__isl_take isl_map
*map
,
14171 __isl_take isl_multi_aff
*ma
)
14173 return isl_map_preimage_multi_aff(map
, isl_dim_in
, ma
);
14176 /* Compute the preimage of the range of "map" under the function
14177 * represented by "ma".
14178 * In other words, plug in "ma" in the range of "map".
14179 * The result is a map that lives in the same space as "map"
14180 * except that the range has been replaced by the domain space of "ma".
14182 __isl_give isl_map
*isl_map_preimage_range_multi_aff(__isl_take isl_map
*map
,
14183 __isl_take isl_multi_aff
*ma
)
14185 return isl_map_preimage_multi_aff(map
, isl_dim_out
, ma
);
14188 /* Compute the preimage of "map" under the function represented by "pma".
14189 * In other words, plug in "pma" in the domain or range of "map".
14190 * The result is a map that lives in the same space as "map",
14191 * except that the space of type "type" has been replaced by
14192 * the domain space of "pma".
14194 * The parameters of "map" and "pma" are assumed to have been aligned.
14196 static __isl_give isl_map
*isl_map_preimage_pw_multi_aff_aligned(
14197 __isl_take isl_map
*map
, enum isl_dim_type type
,
14198 __isl_take isl_pw_multi_aff
*pma
)
14209 space
= isl_pw_multi_aff_get_domain_space(pma
);
14210 isl_pw_multi_aff_free(pma
);
14211 space
= isl_space_set(isl_map_get_space(map
), type
, space
);
14213 return isl_map_empty(space
);
14216 res
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
14217 isl_multi_aff_copy(pma
->p
[0].maff
));
14218 if (type
== isl_dim_in
)
14219 res
= isl_map_intersect_domain(res
,
14220 isl_map_copy(pma
->p
[0].set
));
14222 res
= isl_map_intersect_range(res
,
14223 isl_map_copy(pma
->p
[0].set
));
14225 for (i
= 1; i
< pma
->n
; ++i
) {
14228 res_i
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
14229 isl_multi_aff_copy(pma
->p
[i
].maff
));
14230 if (type
== isl_dim_in
)
14231 res_i
= isl_map_intersect_domain(res_i
,
14232 isl_map_copy(pma
->p
[i
].set
));
14234 res_i
= isl_map_intersect_range(res_i
,
14235 isl_map_copy(pma
->p
[i
].set
));
14236 res
= isl_map_union(res
, res_i
);
14239 isl_pw_multi_aff_free(pma
);
14243 isl_pw_multi_aff_free(pma
);
14248 /* Compute the preimage of "map" under the function represented by "pma".
14249 * In other words, plug in "pma" in the domain or range of "map".
14250 * The result is a map that lives in the same space as "map",
14251 * except that the space of type "type" has been replaced by
14252 * the domain space of "pma".
14254 __isl_give isl_map
*isl_map_preimage_pw_multi_aff(__isl_take isl_map
*map
,
14255 enum isl_dim_type type
, __isl_take isl_pw_multi_aff
*pma
)
14262 aligned
= isl_map_space_has_equal_params(map
, pma
->dim
);
14266 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
14268 if (isl_map_check_named_params(map
) < 0)
14270 if (isl_pw_multi_aff_check_named_params(pma
) < 0)
14272 map
= isl_map_align_params(map
, isl_pw_multi_aff_get_space(pma
));
14273 pma
= isl_pw_multi_aff_align_params(pma
, isl_map_get_space(map
));
14275 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
14277 isl_pw_multi_aff_free(pma
);
14278 return isl_map_free(map
);
14281 /* Compute the preimage of "set" under the function represented by "pma".
14282 * In other words, plug in "pma" in "set". The result is a set
14283 * that lives in the domain space of "pma".
14285 __isl_give isl_set
*isl_set_preimage_pw_multi_aff(__isl_take isl_set
*set
,
14286 __isl_take isl_pw_multi_aff
*pma
)
14288 return isl_map_preimage_pw_multi_aff(set
, isl_dim_set
, pma
);
14291 /* Compute the preimage of the domain of "map" under the function
14292 * represented by "pma".
14293 * In other words, plug in "pma" in the domain of "map".
14294 * The result is a map that lives in the same space as "map",
14295 * except that domain space has been replaced by the domain space of "pma".
14297 __isl_give isl_map
*isl_map_preimage_domain_pw_multi_aff(
14298 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
14300 return isl_map_preimage_pw_multi_aff(map
, isl_dim_in
, pma
);
14303 /* Compute the preimage of the range of "map" under the function
14304 * represented by "pma".
14305 * In other words, plug in "pma" in the range of "map".
14306 * The result is a map that lives in the same space as "map",
14307 * except that range space has been replaced by the domain space of "pma".
14309 __isl_give isl_map
*isl_map_preimage_range_pw_multi_aff(
14310 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
14312 return isl_map_preimage_pw_multi_aff(map
, isl_dim_out
, pma
);
14315 /* Compute the preimage of "map" under the function represented by "mpa".
14316 * In other words, plug in "mpa" in the domain or range of "map".
14317 * The result is a map that lives in the same space as "map",
14318 * except that the space of type "type" has been replaced by
14319 * the domain space of "mpa".
14321 * If the map does not involve any constraints that refer to the
14322 * dimensions of the substituted space, then the only possible
14323 * effect of "mpa" on the map is to map the space to a different space.
14324 * We create a separate isl_multi_aff to effectuate this change
14325 * in order to avoid spurious splitting of the map along the pieces
14327 * If "mpa" has a non-trivial explicit domain, however,
14328 * then the full substitution should be performed.
14330 __isl_give isl_map
*isl_map_preimage_multi_pw_aff(__isl_take isl_map
*map
,
14331 enum isl_dim_type type
, __isl_take isl_multi_pw_aff
*mpa
)
14335 isl_pw_multi_aff
*pma
;
14337 n
= isl_map_dim(map
, type
);
14341 full
= isl_map_involves_dims(map
, type
, 0, n
);
14342 if (full
>= 0 && !full
)
14343 full
= isl_multi_pw_aff_has_non_trivial_domain(mpa
);
14350 space
= isl_multi_pw_aff_get_space(mpa
);
14351 isl_multi_pw_aff_free(mpa
);
14352 ma
= isl_multi_aff_zero(space
);
14353 return isl_map_preimage_multi_aff(map
, type
, ma
);
14356 pma
= isl_pw_multi_aff_from_multi_pw_aff(mpa
);
14357 return isl_map_preimage_pw_multi_aff(map
, type
, pma
);
14360 isl_multi_pw_aff_free(mpa
);
14364 /* Compute the preimage of "map" under the function represented by "mpa".
14365 * In other words, plug in "mpa" in the domain "map".
14366 * The result is a map that lives in the same space as "map",
14367 * except that domain space has been replaced by the domain space of "mpa".
14369 __isl_give isl_map
*isl_map_preimage_domain_multi_pw_aff(
14370 __isl_take isl_map
*map
, __isl_take isl_multi_pw_aff
*mpa
)
14372 return isl_map_preimage_multi_pw_aff(map
, isl_dim_in
, mpa
);
14375 /* Compute the preimage of "set" by the function represented by "mpa".
14376 * In other words, plug in "mpa" in "set".
14378 __isl_give isl_set
*isl_set_preimage_multi_pw_aff(__isl_take isl_set
*set
,
14379 __isl_take isl_multi_pw_aff
*mpa
)
14381 return isl_map_preimage_multi_pw_aff(set
, isl_dim_set
, mpa
);
14384 /* Given that inequality "ineq" of "bmap" expresses an upper bound
14385 * on the output dimension "pos" in terms of the parameters,
14386 * the input dimensions and possibly some integer divisions,
14387 * but not any other output dimensions, extract this upper bound
14388 * as a function of all dimensions (with zero coefficients
14389 * for the output dimensions).
14391 * That is, the inequality is of the form
14393 * e(...) + c - m x >= 0
14395 * where e does not depend on any other output dimensions.
14396 * Return (e(...) + c) / m, with the denominator m in the first position.
14398 __isl_give isl_vec
*isl_basic_map_inequality_extract_output_upper_bound(
14399 __isl_keep isl_basic_map
*bmap
, int ineq
, int pos
)
14402 isl_size v_out
, total
;
14404 v_out
= isl_basic_map_var_offset(bmap
, isl_dim_out
);
14405 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
14406 if (v_out
< 0 || total
< 0)
14408 ctx
= isl_basic_map_get_ctx(bmap
);
14409 return extract_bound_from_constraint(ctx
, bmap
->ineq
[ineq
],
14410 total
, v_out
+ pos
);
14413 /* Is constraint "c" of "bmap" of the form
14415 * e(...) + c1 - m x >= 0
14419 * -e(...) + c2 + m x >= 0
14421 * where m > 1 and e does not involve any other output variables?
14423 * "v_out" is the offset to the output variables.
14424 * "d" is the position of x among the output variables.
14425 * "v_div" is the offset to the local variables.
14426 * "total" is the total number of variables.
14428 * Since the purpose of this function is to use the constraint
14429 * to express the output variable as an integer division,
14430 * do not allow the constraint to involve any local variables
14431 * that do not have an explicit representation.
14433 static isl_bool
is_potential_div_constraint(__isl_keep isl_basic_map
*bmap
,
14434 isl_int
*c
, int v_out
, int d
, int v_div
, int total
)
14438 if (isl_int_is_zero(c
[1 + v_out
+ d
]))
14439 return isl_bool_false
;
14440 if (isl_int_is_one(c
[1 + v_out
+ d
]))
14441 return isl_bool_false
;
14442 if (isl_int_is_negone(c
[1 + v_out
+ d
]))
14443 return isl_bool_false
;
14444 if (isl_seq_first_non_zero(c
+ 1 + v_out
, d
) != -1)
14445 return isl_bool_false
;
14446 if (isl_seq_first_non_zero(c
+ 1 + v_out
+ d
+ 1,
14447 v_div
- (v_out
+ d
+ 1)) != -1)
14448 return isl_bool_false
;
14449 for (i
= 0; v_div
+ i
< total
; ++i
) {
14450 isl_bool known
, involves
;
14452 if (isl_int_is_zero(c
[1 + v_div
+ i
]))
14454 known
= isl_basic_map_div_is_known(bmap
, i
);
14455 if (known
< 0 || !known
)
14457 involves
= div_involves_vars(bmap
, i
, v_out
, v_div
- v_out
);
14458 if (involves
< 0 || involves
)
14459 return isl_bool_not(involves
);
14461 return isl_bool_true
;
14464 /* Look for a pair of constraints
14466 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
14470 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
14472 * that express that the output dimension x at position "pos"
14473 * is some integer division of an expression in terms of the parameters,
14474 * input dimensions and integer divisions.
14475 * If such a pair can be found, then return the index
14476 * of the upper bound constraint, m x <= e(...) + c1.
14477 * Otherwise, return an index beyond the number of constraints.
14479 * In order for the constraints above to express an integer division,
14480 * m needs to be greater than 1 and such that
14482 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
14484 * In particular, this ensures that
14486 * x = floor((e(...) + c1) / m)
14488 isl_size
isl_basic_map_find_output_upper_div_constraint(
14489 __isl_keep isl_basic_map
*bmap
, int pos
)
14493 isl_size v_out
, v_div
;
14497 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
14498 v_out
= isl_basic_map_var_offset(bmap
, isl_dim_out
);
14499 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
14500 n_ineq
= isl_basic_map_n_inequality(bmap
);
14501 if (total
< 0 || v_out
< 0 || v_div
< 0 || n_ineq
< 0)
14502 return isl_size_error
;
14505 for (i
= 0; i
< n_ineq
; ++i
) {
14506 isl_bool potential
;
14508 potential
= is_potential_div_constraint(bmap
, bmap
->ineq
[i
],
14509 v_out
, pos
, v_div
, total
);
14514 for (j
= i
+ 1; j
< n_ineq
; ++j
) {
14515 if (!isl_seq_is_neg(bmap
->ineq
[i
] + 1,
14516 bmap
->ineq
[j
] + 1, total
))
14518 isl_int_add(sum
, bmap
->ineq
[i
][0], bmap
->ineq
[j
][0]);
14519 if (isl_int_abs_lt(sum
, bmap
->ineq
[i
][1 + v_out
+ pos
]))
14525 isl_int_clear(sum
);
14529 if (isl_int_is_pos(bmap
->ineq
[j
][1 + v_out
+ pos
]))
14534 isl_int_clear(sum
);
14535 return isl_size_error
;
14538 /* Return a copy of the equality constraints of "bset" as a matrix.
14540 __isl_give isl_mat
*isl_basic_set_extract_equalities(
14541 __isl_keep isl_basic_set
*bset
)
14546 total
= isl_basic_set_dim(bset
, isl_dim_all
);
14550 ctx
= isl_basic_set_get_ctx(bset
);
14551 return isl_mat_sub_alloc6(ctx
, bset
->eq
, 0, bset
->n_eq
, 0, 1 + total
);
14554 /* Are the "n" "coefficients" starting at "first" of the integer division
14555 * expressions at position "pos1" in "bmap1" and "pos2" in "bmap2" equal
14557 * The "coefficient" at position 0 is the denominator.
14558 * The "coefficient" at position 1 is the constant term.
14560 isl_bool
isl_basic_map_equal_div_expr_part(__isl_keep isl_basic_map
*bmap1
,
14561 int pos1
, __isl_keep isl_basic_map
*bmap2
, int pos2
,
14562 unsigned first
, unsigned n
)
14564 if (isl_basic_map_check_range(bmap1
, isl_dim_div
, pos1
, 1) < 0)
14565 return isl_bool_error
;
14566 if (isl_basic_map_check_range(bmap2
, isl_dim_div
, pos2
, 1) < 0)
14567 return isl_bool_error
;
14568 return isl_seq_eq(bmap1
->div
[pos1
] + first
,
14569 bmap2
->div
[pos2
] + first
, n
);
14572 /* Are the integer division expressions at position "pos1" in "bmap1" and
14573 * "pos2" in "bmap2" equal to each other, except that the constant terms
14576 isl_bool
isl_basic_map_equal_div_expr_except_constant(
14577 __isl_keep isl_basic_map
*bmap1
, int pos1
,
14578 __isl_keep isl_basic_map
*bmap2
, int pos2
)
14581 isl_size total
, total2
;
14583 total
= isl_basic_map_dim(bmap1
, isl_dim_all
);
14584 total2
= isl_basic_map_dim(bmap2
, isl_dim_all
);
14585 if (total
< 0 || total2
< 0)
14586 return isl_bool_error
;
14587 if (total
!= total2
)
14588 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
14589 "incomparable div expressions", return isl_bool_error
);
14590 equal
= isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
14592 if (equal
< 0 || !equal
)
14594 equal
= isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
14596 if (equal
< 0 || equal
)
14597 return isl_bool_not(equal
);
14598 return isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
14602 /* Replace the numerator of the constant term of the integer division
14603 * expression at position "div" in "bmap" by "value".
14604 * The caller guarantees that this does not change the meaning
14607 __isl_give isl_basic_map
*isl_basic_map_set_div_expr_constant_num_si_inplace(
14608 __isl_take isl_basic_map
*bmap
, int div
, int value
)
14610 if (isl_basic_map_check_range(bmap
, isl_dim_div
, div
, 1) < 0)
14611 return isl_basic_map_free(bmap
);
14613 isl_int_set_si(bmap
->div
[div
][1], value
);
14618 /* Is the point "inner" internal to inequality constraint "ineq"
14620 * The point is considered to be internal to the inequality constraint,
14621 * if it strictly lies on the positive side of the inequality constraint,
14622 * or if it lies on the constraint and the constraint is lexico-positive.
14624 static isl_bool
is_internal(__isl_keep isl_vec
*inner
,
14625 __isl_keep isl_basic_set
*bset
, int ineq
)
14631 if (!inner
|| !bset
)
14632 return isl_bool_error
;
14634 ctx
= isl_basic_set_get_ctx(bset
);
14635 isl_seq_inner_product(inner
->el
, bset
->ineq
[ineq
], inner
->size
,
14636 &ctx
->normalize_gcd
);
14637 if (!isl_int_is_zero(ctx
->normalize_gcd
))
14638 return isl_int_is_nonneg(ctx
->normalize_gcd
);
14640 total
= isl_basic_set_dim(bset
, isl_dim_all
);
14642 return isl_bool_error
;
14643 pos
= isl_seq_first_non_zero(bset
->ineq
[ineq
] + 1, total
);
14644 return isl_int_is_pos(bset
->ineq
[ineq
][1 + pos
]);
14647 /* Tighten the inequality constraints of "bset" that are outward with respect
14648 * to the point "vec".
14649 * That is, tighten the constraints that are not satisfied by "vec".
14651 * "vec" is a point internal to some superset S of "bset" that is used
14652 * to make the subsets of S disjoint, by tightening one half of the constraints
14653 * that separate two subsets. In particular, the constraints of S
14654 * are all satisfied by "vec" and should not be tightened.
14655 * Of the internal constraints, those that have "vec" on the outside
14656 * are tightened. The shared facet is included in the adjacent subset
14657 * with the opposite constraint.
14658 * For constraints that saturate "vec", this criterion cannot be used
14659 * to determine which of the two sides should be tightened.
14660 * Instead, the sign of the first non-zero coefficient is used
14661 * to make this choice. Note that this second criterion is never used
14662 * on the constraints of S since "vec" is interior to "S".
14664 __isl_give isl_basic_set
*isl_basic_set_tighten_outward(
14665 __isl_take isl_basic_set
*bset
, __isl_keep isl_vec
*vec
)
14669 bset
= isl_basic_set_cow(bset
);
14672 for (j
= 0; j
< bset
->n_ineq
; ++j
) {
14675 internal
= is_internal(vec
, bset
, j
);
14677 return isl_basic_set_free(bset
);
14680 isl_int_sub_ui(bset
->ineq
[j
][0], bset
->ineq
[j
][0], 1);
14686 /* Replace the variables x of type "type" starting at "first" in "bmap"
14687 * by x' with x = M x' with M the matrix trans.
14688 * That is, replace the corresponding coefficients c by c M.
14690 * The transformation matrix should be a square matrix.
14692 __isl_give isl_basic_map
*isl_basic_map_transform_dims(
14693 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
, unsigned first
,
14694 __isl_take isl_mat
*trans
)
14698 bmap
= isl_basic_map_cow(bmap
);
14699 if (!bmap
|| !trans
)
14702 if (trans
->n_row
!= trans
->n_col
)
14703 isl_die(trans
->ctx
, isl_error_invalid
,
14704 "expecting square transformation matrix", goto error
);
14705 if (isl_basic_map_check_range(bmap
, type
, first
, trans
->n_row
) < 0)
14708 pos
= isl_basic_map_offset(bmap
, type
) + first
;
14710 if (isl_mat_sub_transform(bmap
->eq
, bmap
->n_eq
, pos
,
14711 isl_mat_copy(trans
)) < 0)
14713 if (isl_mat_sub_transform(bmap
->ineq
, bmap
->n_ineq
, pos
,
14714 isl_mat_copy(trans
)) < 0)
14716 if (isl_mat_sub_transform(bmap
->div
, bmap
->n_div
, 1 + pos
,
14717 isl_mat_copy(trans
)) < 0)
14720 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
14721 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
14722 ISL_F_CLR(bmap
, ISL_BASIC_MAP_REDUCED_COEFFICIENTS
);
14724 isl_mat_free(trans
);
14727 isl_mat_free(trans
);
14728 isl_basic_map_free(bmap
);
14732 /* Replace the variables x of type "type" starting at "first" in "bset"
14733 * by x' with x = M x' with M the matrix trans.
14734 * That is, replace the corresponding coefficients c by c M.
14736 * The transformation matrix should be a square matrix.
14738 __isl_give isl_basic_set
*isl_basic_set_transform_dims(
14739 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned first
,
14740 __isl_take isl_mat
*trans
)
14742 return isl_basic_map_transform_dims(bset
, type
, first
, trans
);