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
)
2682 isl_size n_div
, v_div
;
2684 unknown
= isl_basic_map_div_is_marked_unknown(bmap
, div
);
2685 if (unknown
< 0 || unknown
)
2686 return isl_bool_not(unknown
);
2687 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + 1 + first
, n
) >= 0)
2688 return isl_bool_true
;
2690 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
2691 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
2692 if (n_div
< 0 || v_div
< 0)
2693 return isl_bool_error
;
2694 for (i
= n_div
- 1; i
>= 0; --i
) {
2697 if (isl_int_is_zero(bmap
->div
[div
][1 + 1 + v_div
+ i
]))
2699 involves
= div_involves_vars(bmap
, i
, first
, n
);
2700 if (involves
< 0 || involves
)
2704 return isl_bool_false
;
2707 /* Try and add a lower and/or upper bound on "div" to "bmap"
2708 * based on inequality "i".
2709 * "total" is the total number of variables (excluding the divs).
2710 * "v" is a temporary object that can be used during the calculations.
2711 * If "lb" is set, then a lower bound should be constructed.
2712 * If "ub" is set, then an upper bound should be constructed.
2714 * The calling function has already checked that the inequality does not
2715 * reference "div", but we still need to check that the inequality is
2716 * of the right form. We'll consider the case where we want to construct
2717 * a lower bound. The construction of upper bounds is similar.
2719 * Let "div" be of the form
2721 * q = floor((a + f(x))/d)
2723 * We essentially check if constraint "i" is of the form
2727 * so that we can use it to derive a lower bound on "div".
2728 * However, we allow a slightly more general form
2732 * with the condition that the coefficients of g(x) - f(x) are all
2734 * Rewriting this constraint as
2738 * adding a + f(x) to both sides and dividing by d, we obtain
2740 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
2742 * Taking the floor on both sides, we obtain
2744 * q >= floor((a-b)/d) + (f(x)-g(x))/d
2748 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
2750 * In the case of an upper bound, we construct the constraint
2752 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
2755 static __isl_give isl_basic_map
*insert_bounds_on_div_from_ineq(
2756 __isl_take isl_basic_map
*bmap
, int div
, int i
,
2757 unsigned total
, isl_int v
, int lb
, int ub
)
2761 for (j
= 0; (lb
|| ub
) && j
< total
+ bmap
->n_div
; ++j
) {
2763 isl_int_sub(v
, bmap
->ineq
[i
][1 + j
],
2764 bmap
->div
[div
][1 + 1 + j
]);
2765 lb
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
2768 isl_int_add(v
, bmap
->ineq
[i
][1 + j
],
2769 bmap
->div
[div
][1 + 1 + j
]);
2770 ub
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
2776 bmap
= isl_basic_map_cow(bmap
);
2777 bmap
= isl_basic_map_extend_constraints(bmap
, 0, lb
+ ub
);
2779 int k
= isl_basic_map_alloc_inequality(bmap
);
2782 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
2783 isl_int_sub(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
2784 bmap
->div
[div
][1 + j
]);
2785 isl_int_cdiv_q(bmap
->ineq
[k
][j
],
2786 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
2788 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], 1);
2791 int k
= isl_basic_map_alloc_inequality(bmap
);
2794 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
2795 isl_int_add(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
2796 bmap
->div
[div
][1 + j
]);
2797 isl_int_fdiv_q(bmap
->ineq
[k
][j
],
2798 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
2800 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], -1);
2805 isl_basic_map_free(bmap
);
2809 /* This function is called right before "div" is eliminated from "bmap"
2810 * using Fourier-Motzkin.
2811 * Look through the constraints of "bmap" for constraints on the argument
2812 * of the integer division and use them to construct constraints on the
2813 * integer division itself. These constraints can then be combined
2814 * during the Fourier-Motzkin elimination.
2815 * Note that it is only useful to introduce lower bounds on "div"
2816 * if "bmap" already contains upper bounds on "div" as the newly
2817 * introduce lower bounds can then be combined with the pre-existing
2818 * upper bounds. Similarly for upper bounds.
2819 * We therefore first check if "bmap" contains any lower and/or upper bounds
2822 * It is interesting to note that the introduction of these constraints
2823 * can indeed lead to more accurate results, even when compared to
2824 * deriving constraints on the argument of "div" from constraints on "div".
2825 * Consider, for example, the set
2827 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2829 * The second constraint can be rewritten as
2831 * 2 * [(-i-2j+3)/4] + k >= 0
2833 * from which we can derive
2835 * -i - 2j + 3 >= -2k
2841 * Combined with the first constraint, we obtain
2843 * -3 <= 3 + 2k or k >= -3
2845 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2846 * the first constraint, we obtain
2848 * [(i + 2j)/4] >= [-3/4] = -1
2850 * Combining this constraint with the second constraint, we obtain
2854 static __isl_give isl_basic_map
*insert_bounds_on_div(
2855 __isl_take isl_basic_map
*bmap
, int div
)
2858 int check_lb
, check_ub
;
2865 if (isl_int_is_zero(bmap
->div
[div
][0]))
2868 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
2870 return isl_basic_map_free(bmap
);
2874 for (i
= 0; (!check_lb
|| !check_ub
) && i
< bmap
->n_ineq
; ++i
) {
2875 int s
= isl_int_sgn(bmap
->ineq
[i
][1 + v_div
+ div
]);
2882 if (!check_lb
&& !check_ub
)
2887 for (i
= 0; bmap
&& i
< bmap
->n_ineq
; ++i
) {
2888 if (!isl_int_is_zero(bmap
->ineq
[i
][1 + v_div
+ div
]))
2891 bmap
= insert_bounds_on_div_from_ineq(bmap
, div
, i
, v_div
, v
,
2892 check_lb
, check_ub
);
2900 /* Remove all divs (recursively) involving any of the given variables
2901 * in their definitions.
2903 static __isl_give isl_basic_map
*remove_divs_involving_vars(
2904 __isl_take isl_basic_map
*bmap
, unsigned first
, unsigned n
)
2908 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2911 involves
= div_involves_vars(bmap
, i
, first
, n
);
2913 return isl_basic_map_free(bmap
);
2916 bmap
= insert_bounds_on_div(bmap
, i
);
2917 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
2926 /* Remove all divs (recursively) involving any of the given dimensions
2927 * in their definitions.
2929 __isl_give isl_basic_map
*isl_basic_map_remove_divs_involving_dims(
2930 __isl_take isl_basic_map
*bmap
,
2931 enum isl_dim_type type
, unsigned first
, unsigned n
)
2935 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2936 return isl_basic_map_free(bmap
);
2937 off
= isl_basic_map_var_offset(bmap
, type
);
2939 return isl_basic_map_free(bmap
);
2942 return remove_divs_involving_vars(bmap
, first
, n
);
2945 __isl_give isl_basic_set
*isl_basic_set_remove_divs_involving_dims(
2946 __isl_take isl_basic_set
*bset
,
2947 enum isl_dim_type type
, unsigned first
, unsigned n
)
2949 return isl_basic_map_remove_divs_involving_dims(bset
, type
, first
, n
);
2952 __isl_give isl_map
*isl_map_remove_divs_involving_dims(__isl_take isl_map
*map
,
2953 enum isl_dim_type type
, unsigned first
, unsigned n
)
2962 map
= isl_map_cow(map
);
2966 for (i
= 0; i
< map
->n
; ++i
) {
2967 map
->p
[i
] = isl_basic_map_remove_divs_involving_dims(map
->p
[i
],
2978 __isl_give isl_set
*isl_set_remove_divs_involving_dims(__isl_take isl_set
*set
,
2979 enum isl_dim_type type
, unsigned first
, unsigned n
)
2981 return set_from_map(isl_map_remove_divs_involving_dims(set_to_map(set
),
2985 /* Does the description of "bmap" depend on the specified dimensions?
2986 * We also check whether the dimensions appear in any of the div definitions.
2987 * In principle there is no need for this check. If the dimensions appear
2988 * in a div definition, they also appear in the defining constraints of that
2991 isl_bool
isl_basic_map_involves_dims(__isl_keep isl_basic_map
*bmap
,
2992 enum isl_dim_type type
, unsigned first
, unsigned n
)
2996 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2997 return isl_bool_error
;
2999 first
+= isl_basic_map_offset(bmap
, type
);
3000 for (i
= 0; i
< bmap
->n_eq
; ++i
)
3001 if (isl_seq_first_non_zero(bmap
->eq
[i
] + first
, n
) >= 0)
3002 return isl_bool_true
;
3003 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
3004 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + first
, n
) >= 0)
3005 return isl_bool_true
;
3006 for (i
= 0; i
< bmap
->n_div
; ++i
) {
3007 if (isl_int_is_zero(bmap
->div
[i
][0]))
3009 if (isl_seq_first_non_zero(bmap
->div
[i
] + 1 + first
, n
) >= 0)
3010 return isl_bool_true
;
3013 return isl_bool_false
;
3016 isl_bool
isl_map_involves_dims(__isl_keep isl_map
*map
,
3017 enum isl_dim_type type
, unsigned first
, unsigned n
)
3021 if (isl_map_check_range(map
, type
, first
, n
) < 0)
3022 return isl_bool_error
;
3024 for (i
= 0; i
< map
->n
; ++i
) {
3025 isl_bool involves
= isl_basic_map_involves_dims(map
->p
[i
],
3027 if (involves
< 0 || involves
)
3031 return isl_bool_false
;
3034 isl_bool
isl_basic_set_involves_dims(__isl_keep isl_basic_set
*bset
,
3035 enum isl_dim_type type
, unsigned first
, unsigned n
)
3037 return isl_basic_map_involves_dims(bset
, type
, first
, n
);
3040 isl_bool
isl_set_involves_dims(__isl_keep isl_set
*set
,
3041 enum isl_dim_type type
, unsigned first
, unsigned n
)
3043 return isl_map_involves_dims(set
, type
, first
, n
);
3046 /* Does "bset" involve any local variables, i.e., integer divisions?
3048 static isl_bool
isl_basic_set_involves_locals(__isl_keep isl_basic_set
*bset
)
3052 n
= isl_basic_set_dim(bset
, isl_dim_div
);
3054 return isl_bool_error
;
3055 return isl_bool_ok(n
> 0);
3058 /* isl_set_every_basic_set callback that checks whether "bset"
3059 * is free of local variables.
3061 static isl_bool
basic_set_no_locals(__isl_keep isl_basic_set
*bset
, void *user
)
3063 return isl_bool_not(isl_basic_set_involves_locals(bset
));
3066 /* Does "set" involve any local variables, i.e., integer divisions?
3068 isl_bool
isl_set_involves_locals(__isl_keep isl_set
*set
)
3072 no_locals
= isl_set_every_basic_set(set
, &basic_set_no_locals
, NULL
);
3073 return isl_bool_not(no_locals
);
3076 /* Drop all constraints in bmap that involve any of the dimensions
3077 * first to first+n-1.
3078 * This function only performs the actual removal of constraints.
3080 * This function should not call finalize since it is used by
3081 * remove_redundant_divs, which in turn is called by isl_basic_map_finalize.
3083 __isl_give isl_basic_map
*isl_basic_map_drop_constraints_involving(
3084 __isl_take isl_basic_map
*bmap
, unsigned first
, unsigned n
)
3091 bmap
= isl_basic_map_cow(bmap
);
3096 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
) {
3097 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + first
, n
) == -1)
3099 if (isl_basic_map_drop_equality(bmap
, i
) < 0)
3100 return isl_basic_map_free(bmap
);
3103 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
3104 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + 1 + first
, n
) == -1)
3106 if (isl_basic_map_drop_inequality(bmap
, i
) < 0)
3107 return isl_basic_map_free(bmap
);
3113 /* Drop all constraints in bset that involve any of the dimensions
3114 * first to first+n-1.
3115 * This function only performs the actual removal of constraints.
3117 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_involving(
3118 __isl_take isl_basic_set
*bset
, unsigned first
, unsigned n
)
3120 return isl_basic_map_drop_constraints_involving(bset
, first
, n
);
3123 /* Drop all constraints in bmap that do not involve any of the dimensions
3124 * first to first + n - 1 of the given type.
3126 __isl_give isl_basic_map
*isl_basic_map_drop_constraints_not_involving_dims(
3127 __isl_take isl_basic_map
*bmap
,
3128 enum isl_dim_type type
, unsigned first
, unsigned n
)
3133 isl_space
*space
= isl_basic_map_get_space(bmap
);
3134 isl_basic_map_free(bmap
);
3135 return isl_basic_map_universe(space
);
3137 bmap
= isl_basic_map_cow(bmap
);
3141 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
3142 return isl_basic_map_free(bmap
);
3144 first
+= isl_basic_map_offset(bmap
, type
) - 1;
3146 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
) {
3147 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + first
, n
) != -1)
3149 if (isl_basic_map_drop_equality(bmap
, i
) < 0)
3150 return isl_basic_map_free(bmap
);
3153 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
3154 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + 1 + first
, n
) != -1)
3156 if (isl_basic_map_drop_inequality(bmap
, i
) < 0)
3157 return isl_basic_map_free(bmap
);
3160 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
3164 /* Drop all constraints in bset that do not involve any of the dimensions
3165 * first to first + n - 1 of the given type.
3167 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_not_involving_dims(
3168 __isl_take isl_basic_set
*bset
,
3169 enum isl_dim_type type
, unsigned first
, unsigned n
)
3171 return isl_basic_map_drop_constraints_not_involving_dims(bset
,
3175 /* Drop all constraints in bmap that involve any of the dimensions
3176 * first to first + n - 1 of the given type.
3178 __isl_give isl_basic_map
*isl_basic_map_drop_constraints_involving_dims(
3179 __isl_take isl_basic_map
*bmap
,
3180 enum isl_dim_type type
, unsigned first
, unsigned n
)
3187 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
3188 return isl_basic_map_free(bmap
);
3190 bmap
= isl_basic_map_remove_divs_involving_dims(bmap
, type
, first
, n
);
3191 first
+= isl_basic_map_offset(bmap
, type
) - 1;
3192 bmap
= isl_basic_map_drop_constraints_involving(bmap
, first
, n
);
3193 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
3197 /* Drop all constraints in bset that involve any of the dimensions
3198 * first to first + n - 1 of the given type.
3200 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_involving_dims(
3201 __isl_take isl_basic_set
*bset
,
3202 enum isl_dim_type type
, unsigned first
, unsigned n
)
3204 return isl_basic_map_drop_constraints_involving_dims(bset
,
3208 /* Drop constraints from "map" by applying "drop" to each basic map.
3210 static __isl_give isl_map
*drop_constraints(__isl_take isl_map
*map
,
3211 enum isl_dim_type type
, unsigned first
, unsigned n
,
3212 __isl_give isl_basic_map
*(*drop
)(__isl_take isl_basic_map
*bmap
,
3213 enum isl_dim_type type
, unsigned first
, unsigned n
))
3217 if (isl_map_check_range(map
, type
, first
, n
) < 0)
3218 return isl_map_free(map
);
3220 map
= isl_map_cow(map
);
3224 for (i
= 0; i
< map
->n
; ++i
) {
3225 map
->p
[i
] = drop(map
->p
[i
], type
, first
, n
);
3227 return isl_map_free(map
);
3231 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
3236 /* Drop all constraints in map that involve any of the dimensions
3237 * first to first + n - 1 of the given type.
3239 __isl_give isl_map
*isl_map_drop_constraints_involving_dims(
3240 __isl_take isl_map
*map
,
3241 enum isl_dim_type type
, unsigned first
, unsigned n
)
3245 return drop_constraints(map
, type
, first
, n
,
3246 &isl_basic_map_drop_constraints_involving_dims
);
3249 /* Drop all constraints in "map" that do not involve any of the dimensions
3250 * first to first + n - 1 of the given type.
3252 __isl_give isl_map
*isl_map_drop_constraints_not_involving_dims(
3253 __isl_take isl_map
*map
,
3254 enum isl_dim_type type
, unsigned first
, unsigned n
)
3257 isl_space
*space
= isl_map_get_space(map
);
3259 return isl_map_universe(space
);
3261 return drop_constraints(map
, type
, first
, n
,
3262 &isl_basic_map_drop_constraints_not_involving_dims
);
3265 /* Drop all constraints in set that involve any of the dimensions
3266 * first to first + n - 1 of the given type.
3268 __isl_give isl_set
*isl_set_drop_constraints_involving_dims(
3269 __isl_take isl_set
*set
,
3270 enum isl_dim_type type
, unsigned first
, unsigned n
)
3272 return isl_map_drop_constraints_involving_dims(set
, type
, first
, n
);
3275 /* Drop all constraints in "set" that do not involve any of the dimensions
3276 * first to first + n - 1 of the given type.
3278 __isl_give isl_set
*isl_set_drop_constraints_not_involving_dims(
3279 __isl_take isl_set
*set
,
3280 enum isl_dim_type type
, unsigned first
, unsigned n
)
3282 return isl_map_drop_constraints_not_involving_dims(set
, type
, first
, n
);
3285 /* Does local variable "div" of "bmap" have a complete explicit representation?
3286 * Having a complete explicit representation requires not only
3287 * an explicit representation, but also that all local variables
3288 * that appear in this explicit representation in turn have
3289 * a complete explicit representation.
3291 isl_bool
isl_basic_map_div_is_known(__isl_keep isl_basic_map
*bmap
, int div
)
3294 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
3297 marked
= isl_basic_map_div_is_marked_unknown(bmap
, div
);
3298 if (marked
< 0 || marked
)
3299 return isl_bool_not(marked
);
3301 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
3304 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
3306 known
= isl_basic_map_div_is_known(bmap
, i
);
3307 if (known
< 0 || !known
)
3311 return isl_bool_true
;
3314 /* Remove all divs that are unknown or defined in terms of unknown divs.
3316 __isl_give isl_basic_map
*isl_basic_map_remove_unknown_divs(
3317 __isl_take isl_basic_map
*bmap
)
3324 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
3325 if (isl_basic_map_div_is_known(bmap
, i
))
3327 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
3336 /* Remove all divs that are unknown or defined in terms of unknown divs.
3338 __isl_give isl_basic_set
*isl_basic_set_remove_unknown_divs(
3339 __isl_take isl_basic_set
*bset
)
3341 return isl_basic_map_remove_unknown_divs(bset
);
3344 __isl_give isl_map
*isl_map_remove_unknown_divs(__isl_take isl_map
*map
)
3353 map
= isl_map_cow(map
);
3357 for (i
= 0; i
< map
->n
; ++i
) {
3358 map
->p
[i
] = isl_basic_map_remove_unknown_divs(map
->p
[i
]);
3368 __isl_give isl_set
*isl_set_remove_unknown_divs(__isl_take isl_set
*set
)
3370 return set_from_map(isl_map_remove_unknown_divs(set_to_map(set
)));
3373 __isl_give isl_basic_set
*isl_basic_set_remove_dims(
3374 __isl_take isl_basic_set
*bset
,
3375 enum isl_dim_type type
, unsigned first
, unsigned n
)
3377 isl_basic_map
*bmap
= bset_to_bmap(bset
);
3378 bmap
= isl_basic_map_remove_dims(bmap
, type
, first
, n
);
3379 return bset_from_bmap(bmap
);
3382 __isl_give isl_map
*isl_map_remove_dims(__isl_take isl_map
*map
,
3383 enum isl_dim_type type
, unsigned first
, unsigned n
)
3390 map
= isl_map_cow(map
);
3391 if (isl_map_check_range(map
, type
, first
, n
) < 0)
3392 return isl_map_free(map
);
3394 for (i
= 0; i
< map
->n
; ++i
) {
3395 map
->p
[i
] = isl_basic_map_eliminate_vars(map
->p
[i
],
3396 isl_basic_map_offset(map
->p
[i
], type
) - 1 + first
, n
);
3400 map
= isl_map_drop(map
, type
, first
, n
);
3407 __isl_give isl_set
*isl_set_remove_dims(__isl_take isl_set
*bset
,
3408 enum isl_dim_type type
, unsigned first
, unsigned n
)
3410 return set_from_map(isl_map_remove_dims(set_to_map(bset
),
3414 /* Project out n inputs starting at first using Fourier-Motzkin */
3415 __isl_give isl_map
*isl_map_remove_inputs(__isl_take isl_map
*map
,
3416 unsigned first
, unsigned n
)
3418 return isl_map_remove_dims(map
, isl_dim_in
, first
, n
);
3421 void isl_basic_set_print_internal(__isl_keep isl_basic_set
*bset
,
3422 FILE *out
, int indent
)
3427 fprintf(out
, "null basic set\n");
3431 fprintf(out
, "%*s", indent
, "");
3432 fprintf(out
, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
3433 bset
->ref
, bset
->dim
->nparam
, bset
->dim
->n_out
,
3434 bset
->extra
, bset
->flags
);
3436 p
= isl_printer_to_file(isl_basic_set_get_ctx(bset
), out
);
3437 p
= isl_printer_set_dump(p
, 1);
3438 p
= isl_printer_set_indent(p
, indent
);
3439 p
= isl_printer_start_line(p
);
3440 p
= isl_printer_print_basic_set(p
, bset
);
3441 p
= isl_printer_end_line(p
);
3442 isl_printer_free(p
);
3445 void isl_basic_map_print_internal(__isl_keep isl_basic_map
*bmap
,
3446 FILE *out
, int indent
)
3451 fprintf(out
, "null basic map\n");
3455 fprintf(out
, "%*s", indent
, "");
3456 fprintf(out
, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
3457 "flags: %x, n_name: %d\n",
3459 bmap
->dim
->nparam
, bmap
->dim
->n_in
, bmap
->dim
->n_out
,
3460 bmap
->extra
, bmap
->flags
, bmap
->dim
->n_id
);
3462 p
= isl_printer_to_file(isl_basic_map_get_ctx(bmap
), out
);
3463 p
= isl_printer_set_dump(p
, 1);
3464 p
= isl_printer_set_indent(p
, indent
);
3465 p
= isl_printer_start_line(p
);
3466 p
= isl_printer_print_basic_map(p
, bmap
);
3467 p
= isl_printer_end_line(p
);
3468 isl_printer_free(p
);
3471 __isl_give isl_basic_map
*isl_inequality_negate(__isl_take isl_basic_map
*bmap
,
3476 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
3478 return isl_basic_map_free(bmap
);
3479 if (pos
>= bmap
->n_ineq
)
3480 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
3481 "invalid position", return isl_basic_map_free(bmap
));
3482 isl_seq_neg(bmap
->ineq
[pos
], bmap
->ineq
[pos
], 1 + total
);
3483 isl_int_sub_ui(bmap
->ineq
[pos
][0], bmap
->ineq
[pos
][0], 1);
3484 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
3485 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
3489 __isl_give isl_set
*isl_set_alloc_space(__isl_take isl_space
*space
, int n
,
3492 if (isl_space_check_is_set(space
) < 0)
3494 return isl_map_alloc_space(space
, n
, flags
);
3496 isl_space_free(space
);
3500 /* Make sure "map" has room for at least "n" more basic maps.
3502 __isl_give isl_map
*isl_map_grow(__isl_take isl_map
*map
, int n
)
3505 struct isl_map
*grown
= NULL
;
3509 isl_assert(map
->ctx
, n
>= 0, goto error
);
3510 if (map
->n
+ n
<= map
->size
)
3512 grown
= isl_map_alloc_space(isl_map_get_space(map
), map
->n
+ n
, map
->flags
);
3515 for (i
= 0; i
< map
->n
; ++i
) {
3516 grown
->p
[i
] = isl_basic_map_copy(map
->p
[i
]);
3524 isl_map_free(grown
);
3529 /* Make sure "set" has room for at least "n" more basic sets.
3531 __isl_give isl_set
*isl_set_grow(__isl_take isl_set
*set
, int n
)
3533 return set_from_map(isl_map_grow(set_to_map(set
), n
));
3536 __isl_give isl_set
*isl_set_from_basic_set(__isl_take isl_basic_set
*bset
)
3538 return isl_map_from_basic_map(bset
);
3541 /* This function performs the same operation as isl_set_from_basic_set,
3542 * but is considered as a function on an isl_basic_set when exported.
3544 __isl_give isl_set
*isl_basic_set_to_set(__isl_take isl_basic_set
*bset
)
3546 return isl_set_from_basic_set(bset
);
3549 __isl_give isl_map
*isl_map_from_basic_map(__isl_take isl_basic_map
*bmap
)
3551 struct isl_map
*map
;
3556 map
= isl_map_alloc_space(isl_space_copy(bmap
->dim
), 1, ISL_MAP_DISJOINT
);
3557 return isl_map_add_basic_map(map
, bmap
);
3560 __isl_give isl_set
*isl_set_add_basic_set(__isl_take isl_set
*set
,
3561 __isl_take isl_basic_set
*bset
)
3563 return set_from_map(isl_map_add_basic_map(set_to_map(set
),
3564 bset_to_bmap(bset
)));
3567 __isl_null isl_set
*isl_set_free(__isl_take isl_set
*set
)
3569 return isl_map_free(set
);
3572 void isl_set_print_internal(__isl_keep isl_set
*set
, FILE *out
, int indent
)
3577 fprintf(out
, "null set\n");
3581 fprintf(out
, "%*s", indent
, "");
3582 fprintf(out
, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
3583 set
->ref
, set
->n
, set
->dim
->nparam
, set
->dim
->n_out
,
3585 for (i
= 0; i
< set
->n
; ++i
) {
3586 fprintf(out
, "%*s", indent
, "");
3587 fprintf(out
, "basic set %d:\n", i
);
3588 isl_basic_set_print_internal(set
->p
[i
], out
, indent
+4);
3592 void isl_map_print_internal(__isl_keep isl_map
*map
, FILE *out
, int indent
)
3597 fprintf(out
, "null map\n");
3601 fprintf(out
, "%*s", indent
, "");
3602 fprintf(out
, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
3603 "flags: %x, n_name: %d\n",
3604 map
->ref
, map
->n
, map
->dim
->nparam
, map
->dim
->n_in
,
3605 map
->dim
->n_out
, map
->flags
, map
->dim
->n_id
);
3606 for (i
= 0; i
< map
->n
; ++i
) {
3607 fprintf(out
, "%*s", indent
, "");
3608 fprintf(out
, "basic map %d:\n", i
);
3609 isl_basic_map_print_internal(map
->p
[i
], out
, indent
+4);
3613 /* Check that the space of "bset" is the same as that of the domain of "bmap".
3615 static isl_stat
isl_basic_map_check_compatible_domain(
3616 __isl_keep isl_basic_map
*bmap
, __isl_keep isl_basic_set
*bset
)
3620 ok
= isl_basic_map_compatible_domain(bmap
, bset
);
3622 return isl_stat_error
;
3624 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
3625 "incompatible spaces", return isl_stat_error
);
3630 __isl_give isl_basic_map
*isl_basic_map_intersect_domain(
3631 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*bset
)
3633 struct isl_basic_map
*bmap_domain
;
3636 if (isl_basic_map_check_equal_params(bmap
, bset_to_bmap(bset
)) < 0)
3639 dim
= isl_basic_set_dim(bset
, isl_dim_set
);
3643 isl_basic_map_check_compatible_domain(bmap
, bset
) < 0)
3646 bmap
= isl_basic_map_cow(bmap
);
3649 bmap
= isl_basic_map_extend(bmap
,
3650 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
3651 bmap_domain
= isl_basic_map_from_domain(bset
);
3652 bmap
= add_constraints(bmap
, bmap_domain
, 0, 0);
3654 bmap
= isl_basic_map_simplify(bmap
);
3655 return isl_basic_map_finalize(bmap
);
3657 isl_basic_map_free(bmap
);
3658 isl_basic_set_free(bset
);
3662 /* Check that the space of "bset" is the same as that of the range of "bmap".
3664 static isl_stat
isl_basic_map_check_compatible_range(
3665 __isl_keep isl_basic_map
*bmap
, __isl_keep isl_basic_set
*bset
)
3669 ok
= isl_basic_map_compatible_range(bmap
, bset
);
3671 return isl_stat_error
;
3673 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
3674 "incompatible spaces", return isl_stat_error
);
3679 __isl_give isl_basic_map
*isl_basic_map_intersect_range(
3680 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*bset
)
3682 struct isl_basic_map
*bmap_range
;
3685 if (isl_basic_map_check_equal_params(bmap
, bset_to_bmap(bset
)) < 0)
3688 dim
= isl_basic_set_dim(bset
, isl_dim_set
);
3691 if (dim
!= 0 && isl_basic_map_check_compatible_range(bmap
, bset
) < 0)
3694 if (isl_basic_set_plain_is_universe(bset
)) {
3695 isl_basic_set_free(bset
);
3699 bmap
= isl_basic_map_cow(bmap
);
3702 bmap
= isl_basic_map_extend(bmap
,
3703 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
3704 bmap_range
= bset_to_bmap(bset
);
3705 bmap
= add_constraints(bmap
, bmap_range
, 0, 0);
3707 bmap
= isl_basic_map_simplify(bmap
);
3708 return isl_basic_map_finalize(bmap
);
3710 isl_basic_map_free(bmap
);
3711 isl_basic_set_free(bset
);
3715 isl_bool
isl_basic_map_contains(__isl_keep isl_basic_map
*bmap
,
3716 __isl_keep isl_vec
*vec
)
3722 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
3723 if (total
< 0 || !vec
)
3724 return isl_bool_error
;
3726 if (1 + total
!= vec
->size
)
3727 return isl_bool_false
;
3731 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
3732 isl_seq_inner_product(vec
->el
, bmap
->eq
[i
], 1 + total
, &s
);
3733 if (!isl_int_is_zero(s
)) {
3735 return isl_bool_false
;
3739 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
3740 isl_seq_inner_product(vec
->el
, bmap
->ineq
[i
], 1 + total
, &s
);
3741 if (isl_int_is_neg(s
)) {
3743 return isl_bool_false
;
3749 return isl_bool_true
;
3752 isl_bool
isl_basic_set_contains(__isl_keep isl_basic_set
*bset
,
3753 __isl_keep isl_vec
*vec
)
3755 return isl_basic_map_contains(bset_to_bmap(bset
), vec
);
3758 __isl_give isl_basic_map
*isl_basic_map_intersect(
3759 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
3761 struct isl_vec
*sample
= NULL
;
3762 isl_space
*space1
, *space2
;
3763 isl_size dim1
, dim2
, nparam1
, nparam2
;
3765 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
3767 space1
= isl_basic_map_peek_space(bmap1
);
3768 space2
= isl_basic_map_peek_space(bmap2
);
3769 dim1
= isl_space_dim(space1
, isl_dim_all
);
3770 dim2
= isl_space_dim(space2
, isl_dim_all
);
3771 nparam1
= isl_space_dim(space1
, isl_dim_param
);
3772 nparam2
= isl_space_dim(space2
, isl_dim_param
);
3773 if (dim1
< 0 || dim2
< 0 || nparam1
< 0 || nparam2
< 0)
3775 if (dim1
== nparam1
&& dim2
!= nparam2
)
3776 return isl_basic_map_intersect(bmap2
, bmap1
);
3778 if (dim2
!= nparam2
&&
3779 isl_basic_map_check_equal_space(bmap1
, bmap2
) < 0)
3782 if (isl_basic_map_plain_is_empty(bmap1
)) {
3783 isl_basic_map_free(bmap2
);
3786 if (isl_basic_map_plain_is_empty(bmap2
)) {
3787 isl_basic_map_free(bmap1
);
3791 if (bmap1
->sample
&&
3792 isl_basic_map_contains(bmap1
, bmap1
->sample
) > 0 &&
3793 isl_basic_map_contains(bmap2
, bmap1
->sample
) > 0)
3794 sample
= isl_vec_copy(bmap1
->sample
);
3795 else if (bmap2
->sample
&&
3796 isl_basic_map_contains(bmap1
, bmap2
->sample
) > 0 &&
3797 isl_basic_map_contains(bmap2
, bmap2
->sample
) > 0)
3798 sample
= isl_vec_copy(bmap2
->sample
);
3800 bmap1
= isl_basic_map_cow(bmap1
);
3803 bmap1
= isl_basic_map_extend(bmap1
,
3804 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
3805 bmap1
= add_constraints(bmap1
, bmap2
, 0, 0);
3808 isl_vec_free(sample
);
3810 isl_vec_free(bmap1
->sample
);
3811 bmap1
->sample
= sample
;
3814 bmap1
= isl_basic_map_simplify(bmap1
);
3815 return isl_basic_map_finalize(bmap1
);
3818 isl_vec_free(sample
);
3819 isl_basic_map_free(bmap1
);
3820 isl_basic_map_free(bmap2
);
3824 __isl_give isl_basic_set
*isl_basic_set_intersect(
3825 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
3827 return bset_from_bmap(isl_basic_map_intersect(bset_to_bmap(bset1
),
3828 bset_to_bmap(bset2
)));
3831 /* Intersect the parameter domain of "bmap" with "bset".
3833 * isl_basic_map_intersect handles this as a special case.
3835 __isl_give isl_basic_map
*isl_basic_map_intersect_params(
3836 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*bset
)
3838 return isl_basic_map_intersect(bmap
, bset
);
3841 __isl_give isl_basic_set
*isl_basic_set_intersect_params(
3842 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
3844 isl_basic_map
*bmap
;
3846 bmap
= bset_to_bmap(bset1
);
3847 bmap
= isl_basic_map_intersect_params(bmap
, bset2
);
3848 return bset_from_bmap(bmap
);
3851 /* Does "map" consist of a single disjunct, without any local variables?
3853 static isl_bool
is_convex_no_locals(__isl_keep isl_map
*map
)
3858 return isl_bool_error
;
3860 return isl_bool_false
;
3861 n_div
= isl_basic_map_dim(map
->p
[0], isl_dim_div
);
3863 return isl_bool_error
;
3865 return isl_bool_false
;
3866 return isl_bool_true
;
3869 /* Check that "map" consists of a single disjunct, without any local variables.
3871 static isl_stat
check_convex_no_locals(__isl_keep isl_map
*map
)
3875 ok
= is_convex_no_locals(map
);
3877 return isl_stat_error
;
3881 isl_die(isl_map_get_ctx(map
), isl_error_internal
,
3882 "unexpectedly not convex or involving local variables",
3883 return isl_stat_error
);
3886 /* Special case of isl_map_intersect, where both map1 and map2
3887 * are convex, without any divs and such that either map1 or map2
3888 * contains a single constraint. This constraint is then simply
3889 * added to the other map.
3891 static __isl_give isl_map
*map_intersect_add_constraint(
3892 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
3894 if (check_convex_no_locals(map1
) < 0 ||
3895 check_convex_no_locals(map2
) < 0)
3898 if (map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
!= 1)
3899 return isl_map_intersect(map2
, map1
);
3901 map1
= isl_map_cow(map1
);
3904 if (isl_map_plain_is_empty(map1
)) {
3908 if (map2
->p
[0]->n_eq
== 1)
3909 map1
->p
[0] = isl_basic_map_add_eq(map1
->p
[0], map2
->p
[0]->eq
[0]);
3911 map1
->p
[0] = isl_basic_map_add_ineq(map1
->p
[0],
3912 map2
->p
[0]->ineq
[0]);
3914 map1
->p
[0] = isl_basic_map_simplify(map1
->p
[0]);
3915 map1
->p
[0] = isl_basic_map_finalize(map1
->p
[0]);
3919 if (isl_basic_map_plain_is_empty(map1
->p
[0])) {
3920 isl_basic_map_free(map1
->p
[0]);
3926 map1
= isl_map_unmark_normalized(map1
);
3934 /* map2 may be either a parameter domain or a map living in the same
3937 static __isl_give isl_map
*map_intersect_internal(__isl_take isl_map
*map1
,
3938 __isl_take isl_map
*map2
)
3944 isl_size dim2
, nparam2
;
3949 if ((isl_map_plain_is_empty(map1
) ||
3950 isl_map_plain_is_universe(map2
)) &&
3951 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
3955 if ((isl_map_plain_is_empty(map2
) ||
3956 isl_map_plain_is_universe(map1
)) &&
3957 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
3962 if (is_convex_no_locals(map1
) == isl_bool_true
&&
3963 is_convex_no_locals(map2
) == isl_bool_true
&&
3964 isl_space_is_equal(map1
->dim
, map2
->dim
) &&
3965 (map1
->p
[0]->n_eq
+ map1
->p
[0]->n_ineq
== 1 ||
3966 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1))
3967 return map_intersect_add_constraint(map1
, map2
);
3969 equal
= isl_map_plain_is_equal(map1
, map2
);
3977 dim2
= isl_map_dim(map2
, isl_dim_all
);
3978 nparam2
= isl_map_dim(map2
, isl_dim_param
);
3979 if (dim2
< 0 || nparam2
< 0)
3981 if (dim2
!= nparam2
)
3982 isl_assert(map1
->ctx
,
3983 isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
3985 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
3986 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
3987 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
3989 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
3990 map1
->n
* map2
->n
, flags
);
3993 for (i
= 0; i
< map1
->n
; ++i
)
3994 for (j
= 0; j
< map2
->n
; ++j
) {
3995 struct isl_basic_map
*part
;
3996 part
= isl_basic_map_intersect(
3997 isl_basic_map_copy(map1
->p
[i
]),
3998 isl_basic_map_copy(map2
->p
[j
]));
3999 if (isl_basic_map_is_empty(part
) < 0)
4000 part
= isl_basic_map_free(part
);
4001 result
= isl_map_add_basic_map(result
, part
);
4014 static __isl_give isl_map
*map_intersect(__isl_take isl_map
*map1
,
4015 __isl_take isl_map
*map2
)
4017 if (isl_map_check_equal_space(map1
, map2
) < 0)
4019 return map_intersect_internal(map1
, map2
);
4026 __isl_give isl_map
*isl_map_intersect(__isl_take isl_map
*map1
,
4027 __isl_take isl_map
*map2
)
4029 isl_map_align_params_bin(&map1
, &map2
);
4030 return map_intersect(map1
, map2
);
4033 __isl_give isl_set
*isl_set_intersect(__isl_take isl_set
*set1
,
4034 __isl_take isl_set
*set2
)
4036 return set_from_map(isl_map_intersect(set_to_map(set1
),
4040 /* map_intersect_internal accepts intersections
4041 * with parameter domains, so we can just call that function.
4043 __isl_give isl_map
*isl_map_intersect_params(__isl_take isl_map
*map
,
4044 __isl_take isl_set
*params
)
4046 isl_map_align_params_set(&map
, ¶ms
);
4047 return map_intersect_internal(map
, params
);
4050 __isl_give isl_set
*isl_set_intersect_params(__isl_take isl_set
*set
,
4051 __isl_take isl_set
*params
)
4053 return isl_map_intersect_params(set
, params
);
4056 __isl_give isl_basic_map
*isl_basic_map_reverse(__isl_take isl_basic_map
*bmap
)
4064 bmap
= isl_basic_map_cow(bmap
);
4067 space
= isl_space_reverse(isl_space_copy(bmap
->dim
));
4068 pos
= isl_basic_map_offset(bmap
, isl_dim_in
);
4069 n1
= isl_basic_map_dim(bmap
, isl_dim_in
);
4070 n2
= isl_basic_map_dim(bmap
, isl_dim_out
);
4071 if (n1
< 0 || n2
< 0)
4072 bmap
= isl_basic_map_free(bmap
);
4073 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
4074 return isl_basic_map_reset_space(bmap
, space
);
4077 /* Given a basic map where the tuple of type "type" is a wrapped map,
4078 * swap domain and range of that wrapped map.
4080 static __isl_give isl_basic_map
*isl_basic_map_reverse_wrapped(
4081 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
)
4084 isl_size offset
, n1
, n2
;
4086 space
= isl_basic_map_peek_space(bmap
);
4087 offset
= isl_basic_map_var_offset(bmap
, type
);
4088 n1
= isl_space_wrapped_dim(space
, type
, isl_dim_in
);
4089 n2
= isl_space_wrapped_dim(space
, type
, isl_dim_out
);
4090 if (offset
< 0 || n1
< 0 || n2
< 0)
4091 return isl_basic_map_free(bmap
);
4093 bmap
= isl_basic_map_swap_vars(bmap
, 1 + offset
, n1
, n2
);
4095 space
= isl_basic_map_take_space(bmap
);
4096 space
= isl_space_reverse_wrapped(space
, type
);
4097 bmap
= isl_basic_map_restore_space(bmap
, space
);
4102 /* Given a basic map (A -> B) -> C, return the corresponding basic map
4105 static __isl_give isl_basic_map
*isl_basic_map_domain_reverse(
4106 __isl_take isl_basic_map
*bmap
)
4110 space
= isl_basic_map_peek_space(bmap
);
4111 if (isl_space_check_domain_is_wrapping(space
) < 0)
4112 return isl_basic_map_free(bmap
);
4113 bmap
= isl_basic_map_reverse_wrapped(bmap
, isl_dim_in
);
4118 /* Given a basic map A -> (B -> C), return the corresponding basic map
4121 static __isl_give isl_basic_map
*isl_basic_map_range_reverse(
4122 __isl_take isl_basic_map
*bmap
)
4126 space
= isl_basic_map_peek_space(bmap
);
4127 if (isl_space_check_range_is_wrapping(space
) < 0)
4128 return isl_basic_map_free(bmap
);
4129 bmap
= isl_basic_map_reverse_wrapped(bmap
, isl_dim_out
);
4134 /* Given a basic map that is actually a basic set (A -> B),
4135 * return the corresponding basic set (B -> A) as a basic map.
4137 static __isl_give isl_basic_map
*isl_basic_map_set_reverse(
4138 __isl_take isl_basic_map
*bmap
)
4142 space
= isl_basic_map_peek_space(bmap
);
4143 if (isl_space_check_is_wrapping(space
) < 0)
4144 return isl_basic_map_free(bmap
);
4145 bmap
= isl_basic_map_reverse_wrapped(bmap
, isl_dim_set
);
4150 static __isl_give isl_basic_map
*basic_map_space_reset(
4151 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
)
4157 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
4160 space
= isl_basic_map_get_space(bmap
);
4161 space
= isl_space_reset(space
, type
);
4162 bmap
= isl_basic_map_reset_space(bmap
, space
);
4166 __isl_give isl_basic_map
*isl_basic_map_insert_dims(
4167 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
4168 unsigned pos
, unsigned n
)
4170 isl_bool rational
, is_empty
;
4171 isl_space
*res_space
;
4172 struct isl_basic_map
*res
;
4173 struct isl_dim_map
*dim_map
;
4176 enum isl_dim_type t
;
4179 return basic_map_space_reset(bmap
, type
);
4181 is_empty
= isl_basic_map_plain_is_empty(bmap
);
4182 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4183 if (is_empty
< 0 || total
< 0)
4184 return isl_basic_map_free(bmap
);
4185 res_space
= isl_space_insert_dims(isl_basic_map_get_space(bmap
),
4188 return isl_basic_map_free(bmap
);
4190 isl_basic_map_free(bmap
);
4191 return isl_basic_map_empty(res_space
);
4194 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
+ n
);
4196 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
4200 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
4202 isl_size size
= isl_basic_map_dim(bmap
, t
);
4204 dim_map
= isl_dim_map_free(dim_map
);
4205 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
4207 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
4208 pos
, size
- pos
, off
+ pos
+ n
);
4210 dim
= isl_space_dim(res_space
, t
);
4212 dim_map
= isl_dim_map_free(dim_map
);
4215 isl_dim_map_div(dim_map
, bmap
, off
);
4217 res
= isl_basic_map_alloc_space(res_space
,
4218 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
4219 rational
= isl_basic_map_is_rational(bmap
);
4221 res
= isl_basic_map_free(res
);
4223 res
= isl_basic_map_set_rational(res
);
4224 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
4225 return isl_basic_map_finalize(res
);
4228 __isl_give isl_basic_set
*isl_basic_set_insert_dims(
4229 __isl_take isl_basic_set
*bset
,
4230 enum isl_dim_type type
, unsigned pos
, unsigned n
)
4232 return isl_basic_map_insert_dims(bset
, type
, pos
, n
);
4235 __isl_give isl_basic_map
*isl_basic_map_add_dims(__isl_take isl_basic_map
*bmap
,
4236 enum isl_dim_type type
, unsigned n
)
4240 dim
= isl_basic_map_dim(bmap
, type
);
4242 return isl_basic_map_free(bmap
);
4243 return isl_basic_map_insert_dims(bmap
, type
, dim
, n
);
4246 __isl_give isl_basic_set
*isl_basic_set_add_dims(__isl_take isl_basic_set
*bset
,
4247 enum isl_dim_type type
, unsigned n
)
4251 isl_assert(bset
->ctx
, type
!= isl_dim_in
, goto error
);
4252 return isl_basic_map_add_dims(bset
, type
, n
);
4254 isl_basic_set_free(bset
);
4258 static __isl_give isl_map
*map_space_reset(__isl_take isl_map
*map
,
4259 enum isl_dim_type type
)
4263 if (!map
|| !isl_space_is_named_or_nested(map
->dim
, type
))
4266 space
= isl_map_get_space(map
);
4267 space
= isl_space_reset(space
, type
);
4268 map
= isl_map_reset_space(map
, space
);
4272 __isl_give isl_map
*isl_map_insert_dims(__isl_take isl_map
*map
,
4273 enum isl_dim_type type
, unsigned pos
, unsigned n
)
4279 return map_space_reset(map
, type
);
4281 map
= isl_map_cow(map
);
4285 for (i
= 0; i
< map
->n
; ++i
) {
4286 map
->p
[i
] = isl_basic_map_insert_dims(map
->p
[i
], type
, pos
, n
);
4291 space
= isl_map_take_space(map
);
4292 space
= isl_space_insert_dims(space
, type
, pos
, n
);
4293 map
= isl_map_restore_space(map
, space
);
4301 __isl_give isl_set
*isl_set_insert_dims(__isl_take isl_set
*set
,
4302 enum isl_dim_type type
, unsigned pos
, unsigned n
)
4304 return isl_map_insert_dims(set
, type
, pos
, n
);
4307 __isl_give isl_map
*isl_map_add_dims(__isl_take isl_map
*map
,
4308 enum isl_dim_type type
, unsigned n
)
4312 dim
= isl_map_dim(map
, type
);
4314 return isl_map_free(map
);
4315 return isl_map_insert_dims(map
, type
, dim
, n
);
4318 __isl_give isl_set
*isl_set_add_dims(__isl_take isl_set
*set
,
4319 enum isl_dim_type type
, unsigned n
)
4323 isl_assert(set
->ctx
, type
!= isl_dim_in
, goto error
);
4324 return set_from_map(isl_map_add_dims(set_to_map(set
), type
, n
));
4330 __isl_give isl_basic_map
*isl_basic_map_move_dims(
4331 __isl_take isl_basic_map
*bmap
,
4332 enum isl_dim_type dst_type
, unsigned dst_pos
,
4333 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
4336 struct isl_dim_map
*dim_map
;
4337 struct isl_basic_map
*res
;
4338 enum isl_dim_type t
;
4345 bmap
= isl_basic_map_reset(bmap
, src_type
);
4346 bmap
= isl_basic_map_reset(bmap
, dst_type
);
4350 if (isl_basic_map_check_range(bmap
, src_type
, src_pos
, n
) < 0)
4351 return isl_basic_map_free(bmap
);
4353 if (dst_type
== src_type
&& dst_pos
== src_pos
)
4356 isl_assert(bmap
->ctx
, dst_type
!= src_type
, goto error
);
4358 if (pos(bmap
->dim
, dst_type
) + dst_pos
==
4359 pos(bmap
->dim
, src_type
) + src_pos
+
4360 ((src_type
< dst_type
) ? n
: 0)) {
4361 space
= isl_basic_map_take_space(bmap
);
4362 space
= isl_space_move_dims(space
, dst_type
, dst_pos
,
4363 src_type
, src_pos
, n
);
4364 bmap
= isl_basic_map_restore_space(bmap
, space
);
4365 bmap
= isl_basic_map_finalize(bmap
);
4370 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4372 return isl_basic_map_free(bmap
);
4373 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
4376 space
= isl_basic_map_peek_space(bmap
);
4377 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
4378 isl_size size
= isl_space_dim(space
, t
);
4380 dim_map
= isl_dim_map_free(dim_map
);
4381 if (t
== dst_type
) {
4382 isl_dim_map_dim_range(dim_map
, space
, t
,
4385 isl_dim_map_dim_range(dim_map
, space
, src_type
,
4388 isl_dim_map_dim_range(dim_map
, space
, t
,
4389 dst_pos
, size
- dst_pos
, off
);
4390 off
+= size
- dst_pos
;
4391 } else if (t
== src_type
) {
4392 isl_dim_map_dim_range(dim_map
, space
, t
,
4395 isl_dim_map_dim_range(dim_map
, space
, t
,
4396 src_pos
+ n
, size
- src_pos
- n
, off
);
4397 off
+= size
- src_pos
- n
;
4399 isl_dim_map_dim(dim_map
, space
, t
, off
);
4403 isl_dim_map_div(dim_map
, bmap
, off
);
4405 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
4406 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
4407 bmap
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
4408 space
= isl_basic_map_take_space(bmap
);
4409 space
= isl_space_move_dims(space
, dst_type
, dst_pos
,
4410 src_type
, src_pos
, n
);
4411 bmap
= isl_basic_map_restore_space(bmap
, space
);
4415 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
4416 bmap
= isl_basic_map_gauss(bmap
, NULL
);
4417 bmap
= isl_basic_map_finalize(bmap
);
4421 isl_basic_map_free(bmap
);
4425 __isl_give isl_basic_set
*isl_basic_set_move_dims(__isl_take isl_basic_set
*bset
,
4426 enum isl_dim_type dst_type
, unsigned dst_pos
,
4427 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
4429 isl_basic_map
*bmap
= bset_to_bmap(bset
);
4430 bmap
= isl_basic_map_move_dims(bmap
, dst_type
, dst_pos
,
4431 src_type
, src_pos
, n
);
4432 return bset_from_bmap(bmap
);
4435 __isl_give isl_set
*isl_set_move_dims(__isl_take isl_set
*set
,
4436 enum isl_dim_type dst_type
, unsigned dst_pos
,
4437 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
4441 isl_assert(set
->ctx
, dst_type
!= isl_dim_in
, goto error
);
4442 return set_from_map(isl_map_move_dims(set_to_map(set
),
4443 dst_type
, dst_pos
, src_type
, src_pos
, n
));
4449 __isl_give isl_map
*isl_map_move_dims(__isl_take isl_map
*map
,
4450 enum isl_dim_type dst_type
, unsigned dst_pos
,
4451 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
4457 map
= isl_map_reset(map
, src_type
);
4458 map
= isl_map_reset(map
, dst_type
);
4462 if (isl_map_check_range(map
, src_type
, src_pos
, n
))
4463 return isl_map_free(map
);
4465 if (dst_type
== src_type
&& dst_pos
== src_pos
)
4468 isl_assert(map
->ctx
, dst_type
!= src_type
, goto error
);
4470 map
= isl_map_cow(map
);
4474 for (i
= 0; i
< map
->n
; ++i
) {
4475 map
->p
[i
] = isl_basic_map_move_dims(map
->p
[i
],
4477 src_type
, src_pos
, n
);
4482 space
= isl_map_take_space(map
);
4483 space
= isl_space_move_dims(space
, dst_type
, dst_pos
,
4484 src_type
, src_pos
, n
);
4485 map
= isl_map_restore_space(map
, space
);
4493 /* Move the specified dimensions to the last columns right before
4494 * the divs. Don't change the dimension specification of bmap.
4495 * That's the responsibility of the caller.
4497 static __isl_give isl_basic_map
*move_last(__isl_take isl_basic_map
*bmap
,
4498 enum isl_dim_type type
, unsigned first
, unsigned n
)
4501 struct isl_dim_map
*dim_map
;
4502 struct isl_basic_map
*res
;
4503 enum isl_dim_type t
;
4509 if (isl_basic_map_offset(bmap
, type
) + first
+ n
==
4510 isl_basic_map_offset(bmap
, isl_dim_div
))
4513 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4515 return isl_basic_map_free(bmap
);
4516 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
4519 space
= isl_basic_map_peek_space(bmap
);
4520 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
4521 isl_size size
= isl_space_dim(space
, t
);
4523 dim_map
= isl_dim_map_free(dim_map
);
4525 isl_dim_map_dim_range(dim_map
, space
, t
,
4528 isl_dim_map_dim_range(dim_map
, space
, t
,
4529 first
, n
, total
- bmap
->n_div
- n
);
4530 isl_dim_map_dim_range(dim_map
, space
, t
,
4531 first
+ n
, size
- (first
+ n
), off
);
4532 off
+= size
- (first
+ n
);
4534 isl_dim_map_dim(dim_map
, space
, t
, off
);
4538 isl_dim_map_div(dim_map
, bmap
, off
+ n
);
4540 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
4541 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
4542 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
4546 /* Insert "n" rows in the divs of "bmap".
4548 * The number of columns is not changed, which means that the last
4549 * dimensions of "bmap" are being reintepreted as the new divs.
4550 * The space of "bmap" is not adjusted, however, which means
4551 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
4552 * from the space of "bmap" is the responsibility of the caller.
4554 static __isl_give isl_basic_map
*insert_div_rows(__isl_take isl_basic_map
*bmap
,
4562 bmap
= isl_basic_map_cow(bmap
);
4566 row_size
= isl_basic_map_offset(bmap
, isl_dim_div
) + bmap
->extra
;
4567 old
= bmap
->block2
.data
;
4568 bmap
->block2
= isl_blk_extend(bmap
->ctx
, bmap
->block2
,
4569 (bmap
->extra
+ n
) * (1 + row_size
));
4570 if (!bmap
->block2
.data
)
4571 return isl_basic_map_free(bmap
);
4572 new_div
= isl_alloc_array(bmap
->ctx
, isl_int
*, bmap
->extra
+ n
);
4574 return isl_basic_map_free(bmap
);
4575 for (i
= 0; i
< n
; ++i
) {
4576 new_div
[i
] = bmap
->block2
.data
+
4577 (bmap
->extra
+ i
) * (1 + row_size
);
4578 isl_seq_clr(new_div
[i
], 1 + row_size
);
4580 for (i
= 0; i
< bmap
->extra
; ++i
)
4581 new_div
[n
+ i
] = bmap
->block2
.data
+ (bmap
->div
[i
] - old
);
4583 bmap
->div
= new_div
;
4590 /* Drop constraints from "bmap" that only involve the variables
4591 * of "type" in the range [first, first + n] that are not related
4592 * to any of the variables outside that interval.
4593 * These constraints cannot influence the values for the variables
4594 * outside the interval, except in case they cause "bmap" to be empty.
4595 * Only drop the constraints if "bmap" is known to be non-empty.
4597 static __isl_give isl_basic_map
*drop_irrelevant_constraints(
4598 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
4599 unsigned first
, unsigned n
)
4603 isl_size dim
, n_div
;
4606 non_empty
= isl_basic_map_plain_is_non_empty(bmap
);
4608 return isl_basic_map_free(bmap
);
4612 dim
= isl_basic_map_dim(bmap
, isl_dim_all
);
4613 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4614 if (dim
< 0 || n_div
< 0)
4615 return isl_basic_map_free(bmap
);
4616 groups
= isl_calloc_array(isl_basic_map_get_ctx(bmap
), int, dim
);
4618 return isl_basic_map_free(bmap
);
4619 first
+= isl_basic_map_offset(bmap
, type
) - 1;
4620 for (i
= 0; i
< first
; ++i
)
4622 for (i
= first
+ n
; i
< dim
- n_div
; ++i
)
4625 bmap
= isl_basic_map_drop_unrelated_constraints(bmap
, groups
);
4630 /* Turn the n dimensions of type type, starting at first
4631 * into existentially quantified variables.
4633 * If a subset of the projected out variables are unrelated
4634 * to any of the variables that remain, then the constraints
4635 * involving this subset are simply dropped first.
4637 __isl_give isl_basic_map
*isl_basic_map_project_out(
4638 __isl_take isl_basic_map
*bmap
,
4639 enum isl_dim_type type
, unsigned first
, unsigned n
)
4645 return basic_map_space_reset(bmap
, type
);
4646 if (type
== isl_dim_div
)
4647 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
4648 "cannot project out existentially quantified variables",
4649 return isl_basic_map_free(bmap
));
4651 empty
= isl_basic_map_plain_is_empty(bmap
);
4653 return isl_basic_map_free(bmap
);
4655 bmap
= isl_basic_map_set_to_empty(bmap
);
4657 bmap
= drop_irrelevant_constraints(bmap
, type
, first
, n
);
4661 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
4662 return isl_basic_map_remove_dims(bmap
, type
, first
, n
);
4664 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
4665 return isl_basic_map_free(bmap
);
4667 bmap
= move_last(bmap
, type
, first
, n
);
4668 bmap
= isl_basic_map_cow(bmap
);
4669 bmap
= insert_div_rows(bmap
, n
);
4671 space
= isl_basic_map_take_space(bmap
);
4672 space
= isl_space_drop_dims(space
, type
, first
, n
);
4673 bmap
= isl_basic_map_restore_space(bmap
, space
);
4674 bmap
= isl_basic_map_simplify(bmap
);
4675 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
4676 return isl_basic_map_finalize(bmap
);
4679 /* Turn the n dimensions of type type, starting at first
4680 * into existentially quantified variables.
4682 __isl_give isl_basic_set
*isl_basic_set_project_out(
4683 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
,
4684 unsigned first
, unsigned n
)
4686 return bset_from_bmap(isl_basic_map_project_out(bset_to_bmap(bset
),
4690 /* Turn the n dimensions of type type, starting at first
4691 * into existentially quantified variables.
4693 __isl_give isl_map
*isl_map_project_out(__isl_take isl_map
*map
,
4694 enum isl_dim_type type
, unsigned first
, unsigned n
)
4700 return map_space_reset(map
, type
);
4702 if (isl_map_check_range(map
, type
, first
, n
) < 0)
4703 return isl_map_free(map
);
4705 map
= isl_map_cow(map
);
4709 for (i
= 0; i
< map
->n
; ++i
) {
4710 map
->p
[i
] = isl_basic_map_project_out(map
->p
[i
], type
, first
, n
);
4716 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
4717 map
= isl_map_unmark_normalized(map
);
4719 space
= isl_map_take_space(map
);
4720 space
= isl_space_drop_dims(space
, type
, first
, n
);
4721 map
= isl_map_restore_space(map
, space
);
4730 #define TYPE isl_map
4731 #include "isl_project_out_all_params_templ.c"
4732 #include "isl_project_out_param_templ.c"
4734 /* Turn all the dimensions of type "type", except the "n" starting at "first"
4735 * into existentially quantified variables.
4737 __isl_give isl_map
*isl_map_project_onto(__isl_take isl_map
*map
,
4738 enum isl_dim_type type
, unsigned first
, unsigned n
)
4742 dim
= isl_map_dim(map
, type
);
4743 if (isl_map_check_range(map
, type
, first
, n
) < 0 || dim
< 0)
4744 return isl_map_free(map
);
4745 map
= isl_map_project_out(map
, type
, first
+ n
, dim
- (first
+ n
));
4746 map
= isl_map_project_out(map
, type
, 0, first
);
4750 /* Turn the n dimensions of type type, starting at first
4751 * into existentially quantified variables.
4753 __isl_give isl_set
*isl_set_project_out(__isl_take isl_set
*set
,
4754 enum isl_dim_type type
, unsigned first
, unsigned n
)
4756 return set_from_map(isl_map_project_out(set_to_map(set
),
4760 /* If "set" involves a parameter with identifier "id",
4761 * then turn it into an existentially quantified variable.
4763 __isl_give isl_set
*isl_set_project_out_param_id(__isl_take isl_set
*set
,
4764 __isl_take isl_id
*id
)
4766 return set_from_map(isl_map_project_out_param_id(set_to_map(set
), id
));
4769 /* If "set" involves any of the parameters with identifiers in "list",
4770 * then turn them into existentially quantified variables.
4772 __isl_give isl_set
*isl_set_project_out_param_id_list(__isl_take isl_set
*set
,
4773 __isl_take isl_id_list
*list
)
4777 map
= set_to_map(set
);
4778 map
= isl_map_project_out_param_id_list(map
, list
);
4779 return set_from_map(map
);
4782 /* Project out all parameters from "set" by existentially quantifying
4785 __isl_give isl_set
*isl_set_project_out_all_params(__isl_take isl_set
*set
)
4787 return set_from_map(isl_map_project_out_all_params(set_to_map(set
)));
4790 /* Return a map that projects the elements in "set" onto their
4791 * "n" set dimensions starting at "first".
4792 * "type" should be equal to isl_dim_set.
4794 __isl_give isl_map
*isl_set_project_onto_map(__isl_take isl_set
*set
,
4795 enum isl_dim_type type
, unsigned first
, unsigned n
)
4800 if (type
!= isl_dim_set
)
4801 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
4802 "only set dimensions can be projected out", goto error
);
4803 if (isl_set_check_range(set
, type
, first
, n
) < 0)
4804 return isl_set_free(set
);
4806 map
= isl_map_from_domain(set
);
4807 map
= isl_map_add_dims(map
, isl_dim_out
, n
);
4808 for (i
= 0; i
< n
; ++i
)
4809 map
= isl_map_equate(map
, isl_dim_in
, first
+ i
,
4817 static __isl_give isl_basic_map
*add_divs(__isl_take isl_basic_map
*bmap
,
4823 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4825 return isl_basic_map_free(bmap
);
4826 for (i
= 0; i
< n
; ++i
) {
4827 j
= isl_basic_map_alloc_div(bmap
);
4830 isl_seq_clr(bmap
->div
[j
], 1 + 1 + total
);
4834 isl_basic_map_free(bmap
);
4838 /* Does "bmap2" apply to the range of "bmap1" (ignoring parameters)?
4840 isl_bool
isl_basic_map_applies_range(__isl_keep isl_basic_map
*bmap1
,
4841 __isl_keep isl_basic_map
*bmap2
)
4843 isl_space
*space1
, *space2
;
4845 space1
= isl_basic_map_peek_space(bmap1
);
4846 space2
= isl_basic_map_peek_space(bmap2
);
4847 return isl_space_tuple_is_equal(space1
, isl_dim_out
,
4848 space2
, isl_dim_in
);
4851 /* Check that "bmap2" applies to the range of "bmap1" (ignoring parameters).
4853 static isl_stat
isl_basic_map_check_applies_range(
4854 __isl_keep isl_basic_map
*bmap1
, __isl_keep isl_basic_map
*bmap2
)
4858 equal
= isl_basic_map_applies_range(bmap1
, bmap2
);
4860 return isl_stat_error
;
4862 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
4863 "spaces don't match", return isl_stat_error
);
4867 __isl_give isl_basic_map
*isl_basic_map_apply_range(
4868 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
4870 isl_space
*space_result
= NULL
;
4871 struct isl_basic_map
*bmap
;
4872 isl_size n_in
, n_out
, n
, nparam
;
4873 unsigned total
, pos
;
4874 struct isl_dim_map
*dim_map1
, *dim_map2
;
4876 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
4878 if (isl_basic_map_check_applies_range(bmap1
, bmap2
) < 0)
4881 n_in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
4882 n_out
= isl_basic_map_dim(bmap2
, isl_dim_out
);
4883 n
= isl_basic_map_dim(bmap1
, isl_dim_out
);
4884 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
4885 if (n_in
< 0 || n_out
< 0 || n
< 0 || nparam
< 0)
4888 space_result
= isl_space_join(isl_basic_map_get_space(bmap1
),
4889 isl_basic_map_get_space(bmap2
));
4891 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ n
;
4892 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4893 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4894 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
4895 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
4896 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
4897 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_in
);
4898 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_out
);
4899 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
4900 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
4901 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
4903 bmap
= isl_basic_map_alloc_space(space_result
,
4904 bmap1
->n_div
+ bmap2
->n_div
+ n
,
4905 bmap1
->n_eq
+ bmap2
->n_eq
,
4906 bmap1
->n_ineq
+ bmap2
->n_ineq
);
4907 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
4908 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
4909 bmap
= add_divs(bmap
, n
);
4910 bmap
= isl_basic_map_simplify(bmap
);
4911 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
4912 return isl_basic_map_finalize(bmap
);
4914 isl_basic_map_free(bmap1
);
4915 isl_basic_map_free(bmap2
);
4919 __isl_give isl_basic_set
*isl_basic_set_apply(__isl_take isl_basic_set
*bset
,
4920 __isl_take isl_basic_map
*bmap
)
4922 if (isl_basic_map_check_compatible_domain(bmap
, bset
) < 0)
4925 return bset_from_bmap(isl_basic_map_apply_range(bset_to_bmap(bset
),
4928 isl_basic_set_free(bset
);
4929 isl_basic_map_free(bmap
);
4933 __isl_give isl_basic_map
*isl_basic_map_apply_domain(
4934 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
4936 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
4938 if (!isl_space_tuple_is_equal(bmap1
->dim
, isl_dim_in
,
4939 bmap2
->dim
, isl_dim_in
))
4940 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
4941 "spaces don't match", goto error
);
4943 bmap1
= isl_basic_map_reverse(bmap1
);
4944 bmap1
= isl_basic_map_apply_range(bmap1
, bmap2
);
4945 return isl_basic_map_reverse(bmap1
);
4947 isl_basic_map_free(bmap1
);
4948 isl_basic_map_free(bmap2
);
4952 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
4953 * A \cap B -> f(A) + f(B)
4955 __isl_give isl_basic_map
*isl_basic_map_sum(__isl_take isl_basic_map
*bmap1
,
4956 __isl_take isl_basic_map
*bmap2
)
4958 isl_size n_in
, n_out
, nparam
;
4959 unsigned total
, pos
;
4960 struct isl_basic_map
*bmap
= NULL
;
4961 struct isl_dim_map
*dim_map1
, *dim_map2
;
4964 if (isl_basic_map_check_equal_space(bmap1
, bmap2
) < 0)
4967 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
4968 n_in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
4969 n_out
= isl_basic_map_dim(bmap1
, isl_dim_out
);
4970 if (nparam
< 0 || n_in
< 0 || n_out
< 0)
4973 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
;
4974 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4975 dim_map2
= isl_dim_map_alloc(bmap2
->ctx
, total
);
4976 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
4977 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
);
4978 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
4979 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
4980 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_in
+ n_out
);
4981 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
4982 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
4983 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_out
);
4985 bmap
= isl_basic_map_alloc_space(isl_space_copy(bmap1
->dim
),
4986 bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
,
4987 bmap1
->n_eq
+ bmap2
->n_eq
+ n_out
,
4988 bmap1
->n_ineq
+ bmap2
->n_ineq
);
4989 for (i
= 0; i
< n_out
; ++i
) {
4990 int j
= isl_basic_map_alloc_equality(bmap
);
4993 isl_seq_clr(bmap
->eq
[j
], 1+total
);
4994 isl_int_set_si(bmap
->eq
[j
][1+nparam
+n_in
+i
], -1);
4995 isl_int_set_si(bmap
->eq
[j
][1+pos
+i
], 1);
4996 isl_int_set_si(bmap
->eq
[j
][1+pos
-n_out
+i
], 1);
4998 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
4999 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
5000 bmap
= add_divs(bmap
, 2 * n_out
);
5002 bmap
= isl_basic_map_simplify(bmap
);
5003 return isl_basic_map_finalize(bmap
);
5005 isl_basic_map_free(bmap
);
5006 isl_basic_map_free(bmap1
);
5007 isl_basic_map_free(bmap2
);
5011 /* Given two maps A -> f(A) and B -> g(B), construct a map
5012 * A \cap B -> f(A) + f(B)
5014 __isl_give isl_map
*isl_map_sum(__isl_take isl_map
*map1
,
5015 __isl_take isl_map
*map2
)
5017 struct isl_map
*result
;
5020 if (isl_map_check_equal_space(map1
, map2
) < 0)
5023 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
5024 map1
->n
* map2
->n
, 0);
5027 for (i
= 0; i
< map1
->n
; ++i
)
5028 for (j
= 0; j
< map2
->n
; ++j
) {
5029 struct isl_basic_map
*part
;
5030 part
= isl_basic_map_sum(
5031 isl_basic_map_copy(map1
->p
[i
]),
5032 isl_basic_map_copy(map2
->p
[j
]));
5033 if (isl_basic_map_is_empty(part
))
5034 isl_basic_map_free(part
);
5036 result
= isl_map_add_basic_map(result
, part
);
5049 __isl_give isl_set
*isl_set_sum(__isl_take isl_set
*set1
,
5050 __isl_take isl_set
*set2
)
5052 return set_from_map(isl_map_sum(set_to_map(set1
), set_to_map(set2
)));
5055 /* Given a basic map A -> f(A), construct A -> -f(A).
5057 __isl_give isl_basic_map
*isl_basic_map_neg(__isl_take isl_basic_map
*bmap
)
5063 bmap
= isl_basic_map_cow(bmap
);
5064 n
= isl_basic_map_dim(bmap
, isl_dim_out
);
5066 return isl_basic_map_free(bmap
);
5068 off
= isl_basic_map_offset(bmap
, isl_dim_out
);
5069 for (i
= 0; i
< bmap
->n_eq
; ++i
)
5070 for (j
= 0; j
< n
; ++j
)
5071 isl_int_neg(bmap
->eq
[i
][off
+j
], bmap
->eq
[i
][off
+j
]);
5072 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
5073 for (j
= 0; j
< n
; ++j
)
5074 isl_int_neg(bmap
->ineq
[i
][off
+j
], bmap
->ineq
[i
][off
+j
]);
5075 for (i
= 0; i
< bmap
->n_div
; ++i
)
5076 for (j
= 0; j
< n
; ++j
)
5077 isl_int_neg(bmap
->div
[i
][1+off
+j
], bmap
->div
[i
][1+off
+j
]);
5078 bmap
= isl_basic_map_gauss(bmap
, NULL
);
5079 return isl_basic_map_finalize(bmap
);
5082 __isl_give isl_basic_set
*isl_basic_set_neg(__isl_take isl_basic_set
*bset
)
5084 return isl_basic_map_neg(bset
);
5087 /* Given a map A -> f(A), construct A -> -f(A).
5089 __isl_give isl_map
*isl_map_neg(__isl_take isl_map
*map
)
5093 map
= isl_map_cow(map
);
5097 for (i
= 0; i
< map
->n
; ++i
) {
5098 map
->p
[i
] = isl_basic_map_neg(map
->p
[i
]);
5109 __isl_give isl_set
*isl_set_neg(__isl_take isl_set
*set
)
5111 return set_from_map(isl_map_neg(set_to_map(set
)));
5114 /* Given a basic map A -> f(A) and an integer d, construct a basic map
5115 * A -> floor(f(A)/d).
5117 __isl_give isl_basic_map
*isl_basic_map_floordiv(__isl_take isl_basic_map
*bmap
,
5120 isl_size n_in
, n_out
, nparam
;
5121 unsigned total
, pos
;
5122 struct isl_basic_map
*result
= NULL
;
5123 struct isl_dim_map
*dim_map
;
5126 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5127 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5128 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5129 if (nparam
< 0 || n_in
< 0 || n_out
< 0)
5130 return isl_basic_map_free(bmap
);
5132 total
= nparam
+ n_in
+ n_out
+ bmap
->n_div
+ n_out
;
5133 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
5134 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_param
, pos
= 0);
5135 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_in
, pos
+= nparam
);
5136 isl_dim_map_div(dim_map
, bmap
, pos
+= n_in
+ n_out
);
5137 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_out
, pos
+= bmap
->n_div
);
5139 result
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
5140 bmap
->n_div
+ n_out
,
5141 bmap
->n_eq
, bmap
->n_ineq
+ 2 * n_out
);
5142 result
= isl_basic_map_add_constraints_dim_map(result
, bmap
, dim_map
);
5143 result
= add_divs(result
, n_out
);
5144 for (i
= 0; i
< n_out
; ++i
) {
5146 j
= isl_basic_map_alloc_inequality(result
);
5149 isl_seq_clr(result
->ineq
[j
], 1+total
);
5150 isl_int_neg(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
5151 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], 1);
5152 j
= isl_basic_map_alloc_inequality(result
);
5155 isl_seq_clr(result
->ineq
[j
], 1+total
);
5156 isl_int_set(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
5157 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], -1);
5158 isl_int_sub_ui(result
->ineq
[j
][0], d
, 1);
5161 result
= isl_basic_map_simplify(result
);
5162 return isl_basic_map_finalize(result
);
5164 isl_basic_map_free(result
);
5168 /* Given a map A -> f(A) and an integer d, construct a map
5169 * A -> floor(f(A)/d).
5171 __isl_give isl_map
*isl_map_floordiv(__isl_take isl_map
*map
, isl_int d
)
5175 map
= isl_map_cow(map
);
5179 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
5180 for (i
= 0; i
< map
->n
; ++i
) {
5181 map
->p
[i
] = isl_basic_map_floordiv(map
->p
[i
], d
);
5185 map
= isl_map_unmark_normalized(map
);
5193 /* Given a map A -> f(A) and an integer d, construct a map
5194 * A -> floor(f(A)/d).
5196 __isl_give isl_map
*isl_map_floordiv_val(__isl_take isl_map
*map
,
5197 __isl_take isl_val
*d
)
5201 if (!isl_val_is_int(d
))
5202 isl_die(isl_val_get_ctx(d
), isl_error_invalid
,
5203 "expecting integer denominator", goto error
);
5204 map
= isl_map_floordiv(map
, d
->n
);
5213 static __isl_give isl_basic_map
*var_equal(__isl_take isl_basic_map
*bmap
,
5221 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5222 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5223 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5224 if (total
< 0 || nparam
< 0 || n_in
< 0)
5225 return isl_basic_map_free(bmap
);
5226 i
= isl_basic_map_alloc_equality(bmap
);
5229 isl_seq_clr(bmap
->eq
[i
], 1 + total
);
5230 isl_int_set_si(bmap
->eq
[i
][1+nparam
+pos
], -1);
5231 isl_int_set_si(bmap
->eq
[i
][1+nparam
+n_in
+pos
], 1);
5232 return isl_basic_map_finalize(bmap
);
5234 isl_basic_map_free(bmap
);
5238 /* Add a constraint to "bmap" expressing i_pos < o_pos
5240 static __isl_give isl_basic_map
*var_less(__isl_take isl_basic_map
*bmap
,
5248 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5249 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5250 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5251 if (total
< 0 || nparam
< 0 || n_in
< 0)
5252 return isl_basic_map_free(bmap
);
5253 i
= isl_basic_map_alloc_inequality(bmap
);
5256 isl_seq_clr(bmap
->ineq
[i
], 1 + total
);
5257 isl_int_set_si(bmap
->ineq
[i
][0], -1);
5258 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
5259 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
5260 return isl_basic_map_finalize(bmap
);
5262 isl_basic_map_free(bmap
);
5266 /* Add a constraint to "bmap" expressing i_pos <= o_pos
5268 static __isl_give isl_basic_map
*var_less_or_equal(
5269 __isl_take isl_basic_map
*bmap
, unsigned pos
)
5276 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5277 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5278 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5279 if (total
< 0 || nparam
< 0 || n_in
< 0)
5280 return isl_basic_map_free(bmap
);
5281 i
= isl_basic_map_alloc_inequality(bmap
);
5284 isl_seq_clr(bmap
->ineq
[i
], 1 + total
);
5285 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
5286 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
5287 return isl_basic_map_finalize(bmap
);
5289 isl_basic_map_free(bmap
);
5293 /* Add a constraint to "bmap" expressing i_pos > o_pos
5295 static __isl_give isl_basic_map
*var_more(__isl_take isl_basic_map
*bmap
,
5303 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5304 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5305 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5306 if (total
< 0 || nparam
< 0 || n_in
< 0)
5307 return isl_basic_map_free(bmap
);
5308 i
= isl_basic_map_alloc_inequality(bmap
);
5311 isl_seq_clr(bmap
->ineq
[i
], 1 + total
);
5312 isl_int_set_si(bmap
->ineq
[i
][0], -1);
5313 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
5314 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
5315 return isl_basic_map_finalize(bmap
);
5317 isl_basic_map_free(bmap
);
5321 /* Add a constraint to "bmap" expressing i_pos >= o_pos
5323 static __isl_give isl_basic_map
*var_more_or_equal(
5324 __isl_take isl_basic_map
*bmap
, unsigned pos
)
5331 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5332 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5333 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5334 if (total
< 0 || nparam
< 0 || n_in
< 0)
5335 return isl_basic_map_free(bmap
);
5336 i
= isl_basic_map_alloc_inequality(bmap
);
5339 isl_seq_clr(bmap
->ineq
[i
], 1 + total
);
5340 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
5341 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
5342 return isl_basic_map_finalize(bmap
);
5344 isl_basic_map_free(bmap
);
5348 __isl_give isl_basic_map
*isl_basic_map_equal(
5349 __isl_take isl_space
*space
, unsigned n_equal
)
5352 struct isl_basic_map
*bmap
;
5353 bmap
= isl_basic_map_alloc_space(space
, 0, n_equal
, 0);
5356 for (i
= 0; i
< n_equal
&& bmap
; ++i
)
5357 bmap
= var_equal(bmap
, i
);
5358 return isl_basic_map_finalize(bmap
);
5361 /* Return a relation on of dimension "space" expressing i_[0..pos] << o_[0..pos]
5363 __isl_give isl_basic_map
*isl_basic_map_less_at(__isl_take isl_space
*space
,
5367 struct isl_basic_map
*bmap
;
5368 bmap
= isl_basic_map_alloc_space(space
, 0, pos
, 1);
5371 for (i
= 0; i
< pos
&& bmap
; ++i
)
5372 bmap
= var_equal(bmap
, i
);
5374 bmap
= var_less(bmap
, pos
);
5375 return isl_basic_map_finalize(bmap
);
5378 /* Return a relation on "space" expressing i_[0..pos] <<= o_[0..pos]
5380 __isl_give isl_basic_map
*isl_basic_map_less_or_equal_at(
5381 __isl_take isl_space
*space
, unsigned pos
)
5384 isl_basic_map
*bmap
;
5386 bmap
= isl_basic_map_alloc_space(space
, 0, pos
, 1);
5387 for (i
= 0; i
< pos
; ++i
)
5388 bmap
= var_equal(bmap
, i
);
5389 bmap
= var_less_or_equal(bmap
, pos
);
5390 return isl_basic_map_finalize(bmap
);
5393 /* Return a relation on "space" expressing i_pos > o_pos
5395 __isl_give isl_basic_map
*isl_basic_map_more_at(__isl_take isl_space
*space
,
5399 struct isl_basic_map
*bmap
;
5400 bmap
= isl_basic_map_alloc_space(space
, 0, pos
, 1);
5403 for (i
= 0; i
< pos
&& bmap
; ++i
)
5404 bmap
= var_equal(bmap
, i
);
5406 bmap
= var_more(bmap
, pos
);
5407 return isl_basic_map_finalize(bmap
);
5410 /* Return a relation on "space" expressing i_[0..pos] >>= o_[0..pos]
5412 __isl_give isl_basic_map
*isl_basic_map_more_or_equal_at(
5413 __isl_take isl_space
*space
, unsigned pos
)
5416 isl_basic_map
*bmap
;
5418 bmap
= isl_basic_map_alloc_space(space
, 0, pos
, 1);
5419 for (i
= 0; i
< pos
; ++i
)
5420 bmap
= var_equal(bmap
, i
);
5421 bmap
= var_more_or_equal(bmap
, pos
);
5422 return isl_basic_map_finalize(bmap
);
5425 static __isl_give isl_map
*map_lex_lte_first(__isl_take isl_space
*space
,
5426 unsigned n
, int equal
)
5428 struct isl_map
*map
;
5431 if (n
== 0 && equal
)
5432 return isl_map_universe(space
);
5434 map
= isl_map_alloc_space(isl_space_copy(space
), n
, ISL_MAP_DISJOINT
);
5436 for (i
= 0; i
+ 1 < n
; ++i
)
5437 map
= isl_map_add_basic_map(map
,
5438 isl_basic_map_less_at(isl_space_copy(space
), i
));
5441 map
= isl_map_add_basic_map(map
,
5442 isl_basic_map_less_or_equal_at(space
, n
- 1));
5444 map
= isl_map_add_basic_map(map
,
5445 isl_basic_map_less_at(space
, n
- 1));
5447 isl_space_free(space
);
5452 static __isl_give isl_map
*map_lex_lte(__isl_take isl_space
*space
, int equal
)
5456 return map_lex_lte_first(space
, space
->n_out
, equal
);
5459 __isl_give isl_map
*isl_map_lex_lt_first(__isl_take isl_space
*space
,
5462 return map_lex_lte_first(space
, n
, 0);
5465 __isl_give isl_map
*isl_map_lex_le_first(__isl_take isl_space
*space
,
5468 return map_lex_lte_first(space
, n
, 1);
5471 __isl_give isl_map
*isl_map_lex_lt(__isl_take isl_space
*set_space
)
5473 return map_lex_lte(isl_space_map_from_set(set_space
), 0);
5476 __isl_give isl_map
*isl_map_lex_le(__isl_take isl_space
*set_space
)
5478 return map_lex_lte(isl_space_map_from_set(set_space
), 1);
5481 static __isl_give isl_map
*map_lex_gte_first(__isl_take isl_space
*space
,
5482 unsigned n
, int equal
)
5484 struct isl_map
*map
;
5487 if (n
== 0 && equal
)
5488 return isl_map_universe(space
);
5490 map
= isl_map_alloc_space(isl_space_copy(space
), n
, ISL_MAP_DISJOINT
);
5492 for (i
= 0; i
+ 1 < n
; ++i
)
5493 map
= isl_map_add_basic_map(map
,
5494 isl_basic_map_more_at(isl_space_copy(space
), i
));
5497 map
= isl_map_add_basic_map(map
,
5498 isl_basic_map_more_or_equal_at(space
, n
- 1));
5500 map
= isl_map_add_basic_map(map
,
5501 isl_basic_map_more_at(space
, n
- 1));
5503 isl_space_free(space
);
5508 static __isl_give isl_map
*map_lex_gte(__isl_take isl_space
*space
, int equal
)
5512 return map_lex_gte_first(space
, space
->n_out
, equal
);
5515 __isl_give isl_map
*isl_map_lex_gt_first(__isl_take isl_space
*space
,
5518 return map_lex_gte_first(space
, n
, 0);
5521 __isl_give isl_map
*isl_map_lex_ge_first(__isl_take isl_space
*space
,
5524 return map_lex_gte_first(space
, n
, 1);
5527 __isl_give isl_map
*isl_map_lex_gt(__isl_take isl_space
*set_space
)
5529 return map_lex_gte(isl_space_map_from_set(set_space
), 0);
5532 __isl_give isl_map
*isl_map_lex_ge(__isl_take isl_space
*set_space
)
5534 return map_lex_gte(isl_space_map_from_set(set_space
), 1);
5537 __isl_give isl_map
*isl_set_lex_le_set(__isl_take isl_set
*set1
,
5538 __isl_take isl_set
*set2
)
5541 map
= isl_map_lex_le(isl_set_get_space(set1
));
5542 map
= isl_map_intersect_domain(map
, set1
);
5543 map
= isl_map_intersect_range(map
, set2
);
5547 __isl_give isl_map
*isl_set_lex_lt_set(__isl_take isl_set
*set1
,
5548 __isl_take isl_set
*set2
)
5551 map
= isl_map_lex_lt(isl_set_get_space(set1
));
5552 map
= isl_map_intersect_domain(map
, set1
);
5553 map
= isl_map_intersect_range(map
, set2
);
5557 __isl_give isl_map
*isl_set_lex_ge_set(__isl_take isl_set
*set1
,
5558 __isl_take isl_set
*set2
)
5561 map
= isl_map_lex_ge(isl_set_get_space(set1
));
5562 map
= isl_map_intersect_domain(map
, set1
);
5563 map
= isl_map_intersect_range(map
, set2
);
5567 __isl_give isl_map
*isl_set_lex_gt_set(__isl_take isl_set
*set1
,
5568 __isl_take isl_set
*set2
)
5571 map
= isl_map_lex_gt(isl_set_get_space(set1
));
5572 map
= isl_map_intersect_domain(map
, set1
);
5573 map
= isl_map_intersect_range(map
, set2
);
5577 __isl_give isl_map
*isl_map_lex_le_map(__isl_take isl_map
*map1
,
5578 __isl_take isl_map
*map2
)
5581 map
= isl_map_lex_le(isl_space_range(isl_map_get_space(map1
)));
5582 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
5583 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
5587 __isl_give isl_map
*isl_map_lex_lt_map(__isl_take isl_map
*map1
,
5588 __isl_take isl_map
*map2
)
5591 map
= isl_map_lex_lt(isl_space_range(isl_map_get_space(map1
)));
5592 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
5593 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
5597 __isl_give isl_map
*isl_map_lex_ge_map(__isl_take isl_map
*map1
,
5598 __isl_take isl_map
*map2
)
5601 map
= isl_map_lex_ge(isl_space_range(isl_map_get_space(map1
)));
5602 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
5603 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
5607 __isl_give isl_map
*isl_map_lex_gt_map(__isl_take isl_map
*map1
,
5608 __isl_take isl_map
*map2
)
5611 map
= isl_map_lex_gt(isl_space_range(isl_map_get_space(map1
)));
5612 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
5613 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
5617 /* For the div d = floor(f/m) at position "div", add the constraint
5621 static __isl_give isl_basic_map
*add_upper_div_constraint(
5622 __isl_take isl_basic_map
*bmap
, unsigned div
)
5625 isl_size v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
5629 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
5630 if (v_div
< 0 || n_div
< 0)
5631 return isl_basic_map_free(bmap
);
5633 i
= isl_basic_map_alloc_inequality(bmap
);
5635 return isl_basic_map_free(bmap
);
5636 isl_seq_cpy(bmap
->ineq
[i
], bmap
->div
[div
] + 1, 1 + v_div
+ n_div
);
5637 isl_int_neg(bmap
->ineq
[i
][1 + pos
], bmap
->div
[div
][0]);
5642 /* For the div d = floor(f/m) at position "div", add the constraint
5644 * -(f-(m-1)) + m d >= 0
5646 static __isl_give isl_basic_map
*add_lower_div_constraint(
5647 __isl_take isl_basic_map
*bmap
, unsigned div
)
5650 isl_size v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
5654 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
5655 if (v_div
< 0 || n_div
< 0)
5656 return isl_basic_map_free(bmap
);
5658 i
= isl_basic_map_alloc_inequality(bmap
);
5660 return isl_basic_map_free(bmap
);
5661 isl_seq_neg(bmap
->ineq
[i
], bmap
->div
[div
] + 1, 1 + v_div
+ n_div
);
5662 isl_int_set(bmap
->ineq
[i
][1 + pos
], bmap
->div
[div
][0]);
5663 isl_int_add(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], bmap
->ineq
[i
][1 + pos
]);
5664 isl_int_sub_ui(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], 1);
5669 /* For the div d = floor(f/m) at position "pos", add the constraints
5672 * -(f-(m-1)) + m d >= 0
5674 * Note that the second constraint is the negation of
5678 __isl_give isl_basic_map
*isl_basic_map_add_div_constraints(
5679 __isl_take isl_basic_map
*bmap
, unsigned pos
)
5681 bmap
= add_upper_div_constraint(bmap
, pos
);
5682 bmap
= add_lower_div_constraint(bmap
, pos
);
5686 /* For each known div d = floor(f/m), add the constraints
5689 * -(f-(m-1)) + m d >= 0
5691 * Remove duplicate constraints in case of some these div constraints
5692 * already appear in "bmap".
5694 __isl_give isl_basic_map
*isl_basic_map_add_known_div_constraints(
5695 __isl_take isl_basic_map
*bmap
)
5699 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
5701 return isl_basic_map_free(bmap
);
5705 bmap
= add_known_div_constraints(bmap
);
5706 bmap
= isl_basic_map_remove_duplicate_constraints(bmap
, NULL
, 0);
5707 bmap
= isl_basic_map_finalize(bmap
);
5711 /* Add the div constraint of sign "sign" for div "div" of "bmap".
5713 * In particular, if this div is of the form d = floor(f/m),
5714 * then add the constraint
5718 * if sign < 0 or the constraint
5720 * -(f-(m-1)) + m d >= 0
5724 __isl_give isl_basic_map
*isl_basic_map_add_div_constraint(
5725 __isl_take isl_basic_map
*bmap
, unsigned div
, int sign
)
5728 return add_upper_div_constraint(bmap
, div
);
5730 return add_lower_div_constraint(bmap
, div
);
5733 __isl_give isl_basic_set
*isl_basic_map_underlying_set(
5734 __isl_take isl_basic_map
*bmap
)
5740 if (bmap
->dim
->nparam
== 0 && bmap
->dim
->n_in
== 0 &&
5742 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_in
) &&
5743 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_out
))
5744 return bset_from_bmap(bmap
);
5745 bmap
= isl_basic_map_cow(bmap
);
5748 space
= isl_basic_map_take_space(bmap
);
5749 space
= isl_space_underlying(space
, bmap
->n_div
);
5750 bmap
= isl_basic_map_restore_space(bmap
, space
);
5753 bmap
->extra
-= bmap
->n_div
;
5755 bmap
= isl_basic_map_finalize(bmap
);
5756 return bset_from_bmap(bmap
);
5758 isl_basic_map_free(bmap
);
5762 __isl_give isl_basic_set
*isl_basic_set_underlying_set(
5763 __isl_take isl_basic_set
*bset
)
5765 return isl_basic_map_underlying_set(bset_to_bmap(bset
));
5768 /* Replace each element in "list" by the result of applying
5769 * isl_basic_map_underlying_set to the element.
5771 __isl_give isl_basic_set_list
*isl_basic_map_list_underlying_set(
5772 __isl_take isl_basic_map_list
*list
)
5777 n
= isl_basic_map_list_n_basic_map(list
);
5781 for (i
= 0; i
< n
; ++i
) {
5782 isl_basic_map
*bmap
;
5783 isl_basic_set
*bset
;
5785 bmap
= isl_basic_map_list_get_basic_map(list
, i
);
5786 bset
= isl_basic_set_underlying_set(bmap
);
5787 list
= isl_basic_set_list_set_basic_set(list
, i
, bset
);
5792 isl_basic_map_list_free(list
);
5796 __isl_give isl_basic_map
*isl_basic_map_overlying_set(
5797 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_map
*like
)
5799 struct isl_basic_map
*bmap
;
5800 struct isl_ctx
*ctx
;
5801 isl_size dim
, bmap_total
;
5808 if (isl_basic_set_check_no_params(bset
) < 0 ||
5809 isl_basic_set_check_no_locals(bset
) < 0)
5811 dim
= isl_basic_set_dim(bset
, isl_dim_set
);
5812 bmap_total
= isl_basic_map_dim(like
, isl_dim_all
);
5813 if (dim
< 0 || bmap_total
< 0)
5815 isl_assert(ctx
, dim
== bmap_total
, goto error
);
5816 if (like
->n_div
== 0) {
5817 isl_space
*space
= isl_basic_map_get_space(like
);
5818 isl_basic_map_free(like
);
5819 return isl_basic_map_reset_space(bset
, space
);
5821 bset
= isl_basic_set_cow(bset
);
5824 total
= dim
+ bset
->extra
;
5825 bmap
= bset_to_bmap(bset
);
5826 isl_space_free(isl_basic_map_take_space(bmap
));
5827 bmap
= isl_basic_map_restore_space(bmap
, isl_basic_map_get_space(like
));
5830 bmap
->n_div
= like
->n_div
;
5831 bmap
->extra
+= like
->n_div
;
5835 ltotal
= total
- bmap
->extra
+ like
->extra
;
5838 bmap
->block2
= isl_blk_extend(ctx
, bmap
->block2
,
5839 bmap
->extra
* (1 + 1 + total
));
5840 if (isl_blk_is_error(bmap
->block2
))
5842 div
= isl_realloc_array(ctx
, bmap
->div
, isl_int
*, bmap
->extra
);
5846 for (i
= 0; i
< bmap
->extra
; ++i
)
5847 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + 1 + total
);
5848 for (i
= 0; i
< like
->n_div
; ++i
) {
5849 isl_seq_cpy(bmap
->div
[i
], like
->div
[i
], 1 + 1 + ltotal
);
5850 isl_seq_clr(bmap
->div
[i
]+1+1+ltotal
, total
- ltotal
);
5852 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
5854 isl_basic_map_free(like
);
5855 bmap
= isl_basic_map_simplify(bmap
);
5856 bmap
= isl_basic_map_finalize(bmap
);
5859 isl_basic_map_free(like
);
5860 isl_basic_set_free(bset
);
5864 __isl_give isl_basic_set
*isl_basic_set_from_underlying_set(
5865 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*like
)
5867 return bset_from_bmap(isl_basic_map_overlying_set(bset
,
5868 bset_to_bmap(like
)));
5871 __isl_give isl_set
*isl_map_underlying_set(__isl_take isl_map
*map
)
5875 map
= isl_map_cow(map
);
5878 map
->dim
= isl_space_cow(map
->dim
);
5882 for (i
= 1; i
< map
->n
; ++i
)
5883 isl_assert(map
->ctx
, map
->p
[0]->n_div
== map
->p
[i
]->n_div
,
5885 for (i
= 0; i
< map
->n
; ++i
) {
5886 map
->p
[i
] = bset_to_bmap(
5887 isl_basic_map_underlying_set(map
->p
[i
]));
5892 map
->dim
= isl_space_underlying(map
->dim
, 0);
5894 isl_space_free(map
->dim
);
5895 map
->dim
= isl_space_copy(map
->p
[0]->dim
);
5899 return set_from_map(map
);
5905 /* Replace the space of "bmap" by "space".
5907 * If the space of "bmap" is identical to "space" (including the identifiers
5908 * of the input and output dimensions), then simply return the original input.
5910 __isl_give isl_basic_map
*isl_basic_map_reset_space(
5911 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*space
)
5914 isl_space
*bmap_space
;
5916 bmap_space
= isl_basic_map_peek_space(bmap
);
5917 equal
= isl_space_is_equal(bmap_space
, space
);
5918 if (equal
>= 0 && equal
)
5919 equal
= isl_space_has_equal_ids(bmap_space
, space
);
5923 isl_space_free(space
);
5926 isl_space_free(isl_basic_map_take_space(bmap
));
5927 bmap
= isl_basic_map_restore_space(bmap
, space
);
5929 bmap
= isl_basic_map_finalize(bmap
);
5933 isl_basic_map_free(bmap
);
5934 isl_space_free(space
);
5938 __isl_give isl_basic_set
*isl_basic_set_reset_space(
5939 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*space
)
5941 return bset_from_bmap(isl_basic_map_reset_space(bset_to_bmap(bset
),
5945 /* Check that the total dimensions of "map" and "space" are the same.
5947 static isl_stat
check_map_space_equal_total_dim(__isl_keep isl_map
*map
,
5948 __isl_keep isl_space
*space
)
5950 isl_size dim1
, dim2
;
5952 dim1
= isl_map_dim(map
, isl_dim_all
);
5953 dim2
= isl_space_dim(space
, isl_dim_all
);
5954 if (dim1
< 0 || dim2
< 0)
5955 return isl_stat_error
;
5958 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
5959 "total dimensions do not match", return isl_stat_error
);
5962 __isl_give isl_map
*isl_map_reset_space(__isl_take isl_map
*map
,
5963 __isl_take isl_space
*space
)
5967 map
= isl_map_cow(map
);
5971 for (i
= 0; i
< map
->n
; ++i
) {
5972 map
->p
[i
] = isl_basic_map_reset_space(map
->p
[i
],
5973 isl_space_copy(space
));
5977 isl_space_free(isl_map_take_space(map
));
5978 map
= isl_map_restore_space(map
, space
);
5983 isl_space_free(space
);
5987 /* Replace the space of "map" by "space", without modifying
5988 * the dimension of "map".
5990 * If the space of "map" is identical to "space" (including the identifiers
5991 * of the input and output dimensions), then simply return the original input.
5993 __isl_give isl_map
*isl_map_reset_equal_dim_space(__isl_take isl_map
*map
,
5994 __isl_take isl_space
*space
)
5997 isl_space
*map_space
;
5999 map_space
= isl_map_peek_space(map
);
6000 equal
= isl_space_is_equal(map_space
, space
);
6001 if (equal
>= 0 && equal
)
6002 equal
= isl_space_has_equal_ids(map_space
, space
);
6006 isl_space_free(space
);
6009 if (check_map_space_equal_total_dim(map
, space
) < 0)
6011 return isl_map_reset_space(map
, space
);
6014 isl_space_free(space
);
6018 __isl_give isl_set
*isl_set_reset_space(__isl_take isl_set
*set
,
6019 __isl_take isl_space
*space
)
6021 return set_from_map(isl_map_reset_space(set_to_map(set
), space
));
6024 /* Compute the parameter domain of the given basic set.
6026 __isl_give isl_basic_set
*isl_basic_set_params(__isl_take isl_basic_set
*bset
)
6032 is_params
= isl_basic_set_is_params(bset
);
6034 return isl_basic_set_free(bset
);
6038 n
= isl_basic_set_dim(bset
, isl_dim_set
);
6040 return isl_basic_set_free(bset
);
6041 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
6042 space
= isl_basic_set_get_space(bset
);
6043 space
= isl_space_params(space
);
6044 bset
= isl_basic_set_reset_space(bset
, space
);
6048 /* Construct a zero-dimensional basic set with the given parameter domain.
6050 __isl_give isl_basic_set
*isl_basic_set_from_params(
6051 __isl_take isl_basic_set
*bset
)
6054 space
= isl_basic_set_get_space(bset
);
6055 space
= isl_space_set_from_params(space
);
6056 bset
= isl_basic_set_reset_space(bset
, space
);
6060 /* Compute the parameter domain of the given set.
6062 __isl_give isl_set
*isl_set_params(__isl_take isl_set
*set
)
6064 return isl_map_params(set_to_map(set
));
6067 /* Construct a zero-dimensional set with the given parameter domain.
6069 __isl_give isl_set
*isl_set_from_params(__isl_take isl_set
*set
)
6072 space
= isl_set_get_space(set
);
6073 space
= isl_space_set_from_params(space
);
6074 set
= isl_set_reset_space(set
, space
);
6078 /* Compute the parameter domain of the given map.
6080 __isl_give isl_set
*isl_map_params(__isl_take isl_map
*map
)
6083 isl_size n_in
, n_out
;
6085 n_in
= isl_map_dim(map
, isl_dim_in
);
6086 n_out
= isl_map_dim(map
, isl_dim_out
);
6087 if (n_in
< 0 || n_out
< 0)
6088 return isl_map_free(map
);
6089 map
= isl_map_project_out(map
, isl_dim_in
, 0, n_in
);
6090 map
= isl_map_project_out(map
, isl_dim_out
, 0, n_out
);
6091 space
= isl_map_get_space(map
);
6092 space
= isl_space_params(space
);
6093 map
= isl_map_reset_space(map
, space
);
6097 __isl_give isl_basic_set
*isl_basic_map_domain(__isl_take isl_basic_map
*bmap
)
6102 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
6104 return isl_basic_map_free(bmap
);
6105 space
= isl_space_domain(isl_basic_map_get_space(bmap
));
6107 bmap
= isl_basic_map_project_out(bmap
, isl_dim_out
, 0, n_out
);
6109 return isl_basic_map_reset_space(bmap
, space
);
6112 isl_bool
isl_basic_map_may_be_set(__isl_keep isl_basic_map
*bmap
)
6115 return isl_bool_error
;
6116 return isl_space_may_be_set(bmap
->dim
);
6119 /* Is this basic map actually a set?
6120 * Users should never call this function. Outside of isl,
6121 * the type should indicate whether something is a set or a map.
6123 isl_bool
isl_basic_map_is_set(__isl_keep isl_basic_map
*bmap
)
6126 return isl_bool_error
;
6127 return isl_space_is_set(bmap
->dim
);
6130 __isl_give isl_basic_set
*isl_basic_map_range(__isl_take isl_basic_map
*bmap
)
6134 is_set
= isl_basic_map_is_set(bmap
);
6139 return isl_basic_map_domain(isl_basic_map_reverse(bmap
));
6141 isl_basic_map_free(bmap
);
6145 __isl_give isl_basic_map
*isl_basic_map_domain_map(
6146 __isl_take isl_basic_map
*bmap
)
6150 isl_basic_map
*domain
;
6151 isl_size nparam
, n_in
, n_out
;
6153 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
6154 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
6155 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
6156 if (nparam
< 0 || n_in
< 0 || n_out
< 0)
6157 return isl_basic_map_free(bmap
);
6159 space
= isl_basic_map_get_space(bmap
);
6160 space
= isl_space_from_range(isl_space_domain(space
));
6161 domain
= isl_basic_map_universe(space
);
6163 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
6164 bmap
= isl_basic_map_apply_range(bmap
, domain
);
6165 bmap
= isl_basic_map_extend_constraints(bmap
, n_in
, 0);
6167 for (i
= 0; i
< n_in
; ++i
)
6168 bmap
= isl_basic_map_equate(bmap
, isl_dim_in
, i
,
6171 bmap
= isl_basic_map_gauss(bmap
, NULL
);
6172 return isl_basic_map_finalize(bmap
);
6175 __isl_give isl_basic_map
*isl_basic_map_range_map(
6176 __isl_take isl_basic_map
*bmap
)
6180 isl_basic_map
*range
;
6181 isl_size nparam
, n_in
, n_out
;
6183 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
6184 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
6185 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
6186 if (nparam
< 0 || n_in
< 0 || n_out
< 0)
6187 return isl_basic_map_free(bmap
);
6189 space
= isl_basic_map_get_space(bmap
);
6190 space
= isl_space_from_range(isl_space_range(space
));
6191 range
= isl_basic_map_universe(space
);
6193 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
6194 bmap
= isl_basic_map_apply_range(bmap
, range
);
6195 bmap
= isl_basic_map_extend_constraints(bmap
, n_out
, 0);
6197 for (i
= 0; i
< n_out
; ++i
)
6198 bmap
= isl_basic_map_equate(bmap
, isl_dim_in
, n_in
+ i
,
6201 bmap
= isl_basic_map_gauss(bmap
, NULL
);
6202 return isl_basic_map_finalize(bmap
);
6205 int isl_map_may_be_set(__isl_keep isl_map
*map
)
6209 return isl_space_may_be_set(map
->dim
);
6212 /* Is this map actually a set?
6213 * Users should never call this function. Outside of isl,
6214 * the type should indicate whether something is a set or a map.
6216 isl_bool
isl_map_is_set(__isl_keep isl_map
*map
)
6219 return isl_bool_error
;
6220 return isl_space_is_set(map
->dim
);
6223 __isl_give isl_set
*isl_map_range(__isl_take isl_map
*map
)
6228 n_in
= isl_map_dim(map
, isl_dim_in
);
6230 return set_from_map(isl_map_free(map
));
6231 space
= isl_space_range(isl_map_get_space(map
));
6233 map
= isl_map_project_out(map
, isl_dim_in
, 0, n_in
);
6235 return set_from_map(isl_map_reset_space(map
, space
));
6238 /* Transform "map" by applying "fn_space" to its space and "fn_bmap"
6239 * to each of its basic maps.
6241 static __isl_give isl_map
*isl_map_transform(__isl_take isl_map
*map
,
6242 __isl_give isl_space
*(*fn_space
)(__isl_take isl_space
*space
),
6243 __isl_give isl_basic_map
*(*fn_bmap
)(__isl_take isl_basic_map
*bmap
))
6248 map
= isl_map_cow(map
);
6252 for (i
= 0; i
< map
->n
; ++i
) {
6253 map
->p
[i
] = fn_bmap(map
->p
[i
]);
6255 return isl_map_free(map
);
6257 map
= isl_map_unmark_normalized(map
);
6259 space
= isl_map_take_space(map
);
6260 space
= fn_space(space
);
6261 map
= isl_map_restore_space(map
, space
);
6266 __isl_give isl_map
*isl_map_domain_map(__isl_take isl_map
*map
)
6268 return isl_map_transform(map
, &isl_space_domain_map
,
6269 &isl_basic_map_domain_map
);
6272 __isl_give isl_map
*isl_map_range_map(__isl_take isl_map
*map
)
6274 return isl_map_transform(map
, &isl_space_range_map
,
6275 &isl_basic_map_range_map
);
6278 /* Given a wrapped map of the form A[B -> C],
6279 * return the map A[B -> C] -> B.
6281 __isl_give isl_map
*isl_set_wrapped_domain_map(__isl_take isl_set
*set
)
6288 if (!isl_set_has_tuple_id(set
))
6289 return isl_map_domain_map(isl_set_unwrap(set
));
6291 id
= isl_set_get_tuple_id(set
);
6292 map
= isl_map_domain_map(isl_set_unwrap(set
));
6293 map
= isl_map_set_tuple_id(map
, isl_dim_in
, id
);
6298 __isl_give isl_basic_map
*isl_basic_map_from_domain(
6299 __isl_take isl_basic_set
*bset
)
6301 return isl_basic_map_reverse(isl_basic_map_from_range(bset
));
6304 __isl_give isl_basic_map
*isl_basic_map_from_range(
6305 __isl_take isl_basic_set
*bset
)
6308 space
= isl_basic_set_get_space(bset
);
6309 space
= isl_space_from_range(space
);
6310 bset
= isl_basic_set_reset_space(bset
, space
);
6311 return bset_to_bmap(bset
);
6314 /* Create a relation with the given set as range.
6315 * The domain of the created relation is a zero-dimensional
6316 * flat anonymous space.
6318 __isl_give isl_map
*isl_map_from_range(__isl_take isl_set
*set
)
6321 space
= isl_set_get_space(set
);
6322 space
= isl_space_from_range(space
);
6323 set
= isl_set_reset_space(set
, space
);
6324 return set_to_map(set
);
6327 /* Create a relation with the given set as domain.
6328 * The range of the created relation is a zero-dimensional
6329 * flat anonymous space.
6331 __isl_give isl_map
*isl_map_from_domain(__isl_take isl_set
*set
)
6333 return isl_map_reverse(isl_map_from_range(set
));
6336 __isl_give isl_basic_map
*isl_basic_map_from_domain_and_range(
6337 __isl_take isl_basic_set
*domain
, __isl_take isl_basic_set
*range
)
6339 return isl_basic_map_apply_range(isl_basic_map_reverse(domain
), range
);
6342 __isl_give isl_map
*isl_map_from_domain_and_range(__isl_take isl_set
*domain
,
6343 __isl_take isl_set
*range
)
6345 return isl_map_apply_range(isl_map_reverse(domain
), range
);
6348 /* Return a newly allocated isl_map with given space and flags and
6349 * room for "n" basic maps.
6350 * Make sure that all cached information is cleared.
6352 __isl_give isl_map
*isl_map_alloc_space(__isl_take isl_space
*space
, int n
,
6355 struct isl_map
*map
;
6360 isl_die(space
->ctx
, isl_error_internal
,
6361 "negative number of basic maps", goto error
);
6362 map
= isl_calloc(space
->ctx
, struct isl_map
,
6363 sizeof(struct isl_map
) +
6364 (n
- 1) * sizeof(struct isl_basic_map
*));
6368 map
->ctx
= space
->ctx
;
6369 isl_ctx_ref(map
->ctx
);
6377 isl_space_free(space
);
6381 __isl_give isl_basic_map
*isl_basic_map_empty(__isl_take isl_space
*space
)
6383 struct isl_basic_map
*bmap
;
6384 bmap
= isl_basic_map_alloc_space(space
, 0, 1, 0);
6385 bmap
= isl_basic_map_set_to_empty(bmap
);
6389 __isl_give isl_basic_set
*isl_basic_set_empty(__isl_take isl_space
*space
)
6391 struct isl_basic_set
*bset
;
6392 bset
= isl_basic_set_alloc_space(space
, 0, 1, 0);
6393 bset
= isl_basic_set_set_to_empty(bset
);
6397 __isl_give isl_basic_map
*isl_basic_map_universe(__isl_take isl_space
*space
)
6399 struct isl_basic_map
*bmap
;
6400 bmap
= isl_basic_map_alloc_space(space
, 0, 0, 0);
6401 bmap
= isl_basic_map_finalize(bmap
);
6405 __isl_give isl_basic_set
*isl_basic_set_universe(__isl_take isl_space
*space
)
6407 struct isl_basic_set
*bset
;
6408 bset
= isl_basic_set_alloc_space(space
, 0, 0, 0);
6409 bset
= isl_basic_set_finalize(bset
);
6413 __isl_give isl_basic_map
*isl_basic_map_nat_universe(
6414 __isl_take isl_space
*space
)
6417 isl_size total
= isl_space_dim(space
, isl_dim_all
);
6418 isl_basic_map
*bmap
;
6421 space
= isl_space_free(space
);
6422 bmap
= isl_basic_map_alloc_space(space
, 0, 0, total
);
6423 for (i
= 0; i
< total
; ++i
) {
6424 int k
= isl_basic_map_alloc_inequality(bmap
);
6427 isl_seq_clr(bmap
->ineq
[k
], 1 + total
);
6428 isl_int_set_si(bmap
->ineq
[k
][1 + i
], 1);
6432 isl_basic_map_free(bmap
);
6436 __isl_give isl_basic_set
*isl_basic_set_nat_universe(
6437 __isl_take isl_space
*space
)
6439 return isl_basic_map_nat_universe(space
);
6442 __isl_give isl_map
*isl_map_nat_universe(__isl_take isl_space
*space
)
6444 return isl_map_from_basic_map(isl_basic_map_nat_universe(space
));
6447 __isl_give isl_set
*isl_set_nat_universe(__isl_take isl_space
*space
)
6449 return isl_map_nat_universe(space
);
6452 __isl_give isl_map
*isl_map_empty(__isl_take isl_space
*space
)
6454 return isl_map_alloc_space(space
, 0, ISL_MAP_DISJOINT
);
6457 __isl_give isl_set
*isl_set_empty(__isl_take isl_space
*space
)
6459 return isl_set_alloc_space(space
, 0, ISL_MAP_DISJOINT
);
6462 __isl_give isl_map
*isl_map_universe(__isl_take isl_space
*space
)
6464 struct isl_map
*map
;
6467 map
= isl_map_alloc_space(isl_space_copy(space
), 1, ISL_MAP_DISJOINT
);
6468 map
= isl_map_add_basic_map(map
, isl_basic_map_universe(space
));
6472 /* This function performs the same operation as isl_map_universe,
6473 * but is considered as a function on an isl_space when exported.
6475 __isl_give isl_map
*isl_space_universe_map(__isl_take isl_space
*space
)
6477 return isl_map_universe(space
);
6480 __isl_give isl_set
*isl_set_universe(__isl_take isl_space
*space
)
6482 struct isl_set
*set
;
6485 set
= isl_set_alloc_space(isl_space_copy(space
), 1, ISL_MAP_DISJOINT
);
6486 set
= isl_set_add_basic_set(set
, isl_basic_set_universe(space
));
6490 /* This function performs the same operation as isl_set_universe,
6491 * but is considered as a function on an isl_space when exported.
6493 __isl_give isl_set
*isl_space_universe_set(__isl_take isl_space
*space
)
6495 return isl_set_universe(space
);
6498 __isl_give isl_map
*isl_map_dup(__isl_keep isl_map
*map
)
6501 struct isl_map
*dup
;
6505 dup
= isl_map_alloc_space(isl_space_copy(map
->dim
), map
->n
, map
->flags
);
6506 for (i
= 0; i
< map
->n
; ++i
)
6507 dup
= isl_map_add_basic_map(dup
, isl_basic_map_copy(map
->p
[i
]));
6511 __isl_give isl_map
*isl_map_add_basic_map(__isl_take isl_map
*map
,
6512 __isl_take isl_basic_map
*bmap
)
6516 if (isl_basic_map_plain_is_empty(bmap
)) {
6517 isl_basic_map_free(bmap
);
6520 if (isl_map_basic_map_check_equal_space(map
, bmap
) < 0)
6522 isl_assert(map
->ctx
, map
->n
< map
->size
, goto error
);
6523 map
->p
[map
->n
] = bmap
;
6525 map
= isl_map_unmark_normalized(map
);
6531 isl_basic_map_free(bmap
);
6535 __isl_null isl_map
*isl_map_free(__isl_take isl_map
*map
)
6546 isl_ctx_deref(map
->ctx
);
6547 for (i
= 0; i
< map
->n
; ++i
)
6548 isl_basic_map_free(map
->p
[i
]);
6549 isl_space_free(map
->dim
);
6555 static __isl_give isl_basic_map
*isl_basic_map_fix_pos_si(
6556 __isl_take isl_basic_map
*bmap
, unsigned pos
, int value
)
6561 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
6563 return isl_basic_map_free(bmap
);
6565 bmap
= isl_basic_map_cow(bmap
);
6566 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
6567 j
= isl_basic_map_alloc_equality(bmap
);
6570 isl_seq_clr(bmap
->eq
[j
] + 1, total
);
6571 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
6572 isl_int_set_si(bmap
->eq
[j
][0], value
);
6573 bmap
= isl_basic_map_simplify(bmap
);
6574 return isl_basic_map_finalize(bmap
);
6576 isl_basic_map_free(bmap
);
6580 static __isl_give isl_basic_map
*isl_basic_map_fix_pos(
6581 __isl_take isl_basic_map
*bmap
, unsigned pos
, isl_int value
)
6586 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
6588 return isl_basic_map_free(bmap
);
6590 bmap
= isl_basic_map_cow(bmap
);
6591 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
6592 j
= isl_basic_map_alloc_equality(bmap
);
6595 isl_seq_clr(bmap
->eq
[j
] + 1, total
);
6596 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
6597 isl_int_set(bmap
->eq
[j
][0], value
);
6598 bmap
= isl_basic_map_simplify(bmap
);
6599 return isl_basic_map_finalize(bmap
);
6601 isl_basic_map_free(bmap
);
6605 __isl_give isl_basic_map
*isl_basic_map_fix_si(__isl_take isl_basic_map
*bmap
,
6606 enum isl_dim_type type
, unsigned pos
, int value
)
6608 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6609 return isl_basic_map_free(bmap
);
6610 return isl_basic_map_fix_pos_si(bmap
,
6611 isl_basic_map_offset(bmap
, type
) + pos
, value
);
6614 __isl_give isl_basic_map
*isl_basic_map_fix(__isl_take isl_basic_map
*bmap
,
6615 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6617 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6618 return isl_basic_map_free(bmap
);
6619 return isl_basic_map_fix_pos(bmap
,
6620 isl_basic_map_offset(bmap
, type
) + pos
, value
);
6623 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
6624 * to be equal to "v".
6626 __isl_give isl_basic_map
*isl_basic_map_fix_val(__isl_take isl_basic_map
*bmap
,
6627 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6631 if (!isl_val_is_int(v
))
6632 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
6633 "expecting integer value", goto error
);
6634 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6636 pos
+= isl_basic_map_offset(bmap
, type
);
6637 bmap
= isl_basic_map_fix_pos(bmap
, pos
, v
->n
);
6641 isl_basic_map_free(bmap
);
6646 /* Fix the value of the variable at position "pos" of type "type" of "bset"
6647 * to be equal to "v".
6649 __isl_give isl_basic_set
*isl_basic_set_fix_val(__isl_take isl_basic_set
*bset
,
6650 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6652 return isl_basic_map_fix_val(bset
, type
, pos
, v
);
6655 __isl_give isl_basic_set
*isl_basic_set_fix_si(__isl_take isl_basic_set
*bset
,
6656 enum isl_dim_type type
, unsigned pos
, int value
)
6658 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset
),
6662 __isl_give isl_basic_set
*isl_basic_set_fix(__isl_take isl_basic_set
*bset
,
6663 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6665 return bset_from_bmap(isl_basic_map_fix(bset_to_bmap(bset
),
6669 /* Remove the basic map at position "i" from "map" if this basic map
6670 * is (obviously) empty.
6672 static __isl_give isl_map
*remove_if_empty(__isl_take isl_map
*map
, int i
)
6679 empty
= isl_basic_map_plain_is_empty(map
->p
[i
]);
6681 return isl_map_free(map
);
6685 isl_basic_map_free(map
->p
[i
]);
6688 map
->p
[i
] = map
->p
[map
->n
];
6689 map
= isl_map_unmark_normalized(map
);
6696 /* Perform "fn" on each basic map of "map", where we may not be holding
6697 * the only reference to "map".
6698 * In particular, "fn" should be a semantics preserving operation
6699 * that we want to apply to all copies of "map". We therefore need
6700 * to be careful not to modify "map" in a way that breaks "map"
6701 * in case anything goes wrong.
6703 __isl_give isl_map
*isl_map_inline_foreach_basic_map(__isl_take isl_map
*map
,
6704 __isl_give isl_basic_map
*(*fn
)(__isl_take isl_basic_map
*bmap
))
6706 struct isl_basic_map
*bmap
;
6712 for (i
= map
->n
- 1; i
>= 0; --i
) {
6713 bmap
= isl_basic_map_copy(map
->p
[i
]);
6717 isl_basic_map_free(map
->p
[i
]);
6719 map
= remove_if_empty(map
, i
);
6730 __isl_give isl_map
*isl_map_fix_si(__isl_take isl_map
*map
,
6731 enum isl_dim_type type
, unsigned pos
, int value
)
6735 map
= isl_map_cow(map
);
6736 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
6737 return isl_map_free(map
);
6738 for (i
= map
->n
- 1; i
>= 0; --i
) {
6739 map
->p
[i
] = isl_basic_map_fix_si(map
->p
[i
], type
, pos
, value
);
6740 map
= remove_if_empty(map
, i
);
6744 map
= isl_map_unmark_normalized(map
);
6748 __isl_give isl_set
*isl_set_fix_si(__isl_take isl_set
*set
,
6749 enum isl_dim_type type
, unsigned pos
, int value
)
6751 return set_from_map(isl_map_fix_si(set_to_map(set
), type
, pos
, value
));
6754 __isl_give isl_map
*isl_map_fix(__isl_take isl_map
*map
,
6755 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6759 map
= isl_map_cow(map
);
6760 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
6761 return isl_map_free(map
);
6762 for (i
= 0; i
< map
->n
; ++i
) {
6763 map
->p
[i
] = isl_basic_map_fix(map
->p
[i
], type
, pos
, value
);
6767 map
= isl_map_unmark_normalized(map
);
6774 __isl_give isl_set
*isl_set_fix(__isl_take isl_set
*set
,
6775 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6777 return set_from_map(isl_map_fix(set_to_map(set
), type
, pos
, value
));
6780 /* Fix the value of the variable at position "pos" of type "type" of "map"
6781 * to be equal to "v".
6783 __isl_give isl_map
*isl_map_fix_val(__isl_take isl_map
*map
,
6784 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6788 map
= isl_map_cow(map
);
6792 if (!isl_val_is_int(v
))
6793 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
6794 "expecting integer value", goto error
);
6795 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
6797 for (i
= map
->n
- 1; i
>= 0; --i
) {
6798 map
->p
[i
] = isl_basic_map_fix_val(map
->p
[i
], type
, pos
,
6800 map
= remove_if_empty(map
, i
);
6804 map
= isl_map_unmark_normalized(map
);
6813 /* Fix the value of the variable at position "pos" of type "type" of "set"
6814 * to be equal to "v".
6816 __isl_give isl_set
*isl_set_fix_val(__isl_take isl_set
*set
,
6817 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6819 return isl_map_fix_val(set
, type
, pos
, v
);
6822 __isl_give isl_map
*isl_map_fix_input_si(__isl_take isl_map
*map
,
6823 unsigned input
, int value
)
6825 return isl_map_fix_si(map
, isl_dim_in
, input
, value
);
6828 __isl_give isl_set
*isl_set_fix_dim_si(__isl_take isl_set
*set
, unsigned dim
,
6831 return set_from_map(isl_map_fix_si(set_to_map(set
),
6832 isl_dim_set
, dim
, value
));
6835 static __isl_give isl_basic_map
*basic_map_bound_si(
6836 __isl_take isl_basic_map
*bmap
,
6837 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
6842 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6843 return isl_basic_map_free(bmap
);
6844 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
6846 return isl_basic_map_free(bmap
);
6847 pos
+= isl_basic_map_offset(bmap
, type
);
6848 bmap
= isl_basic_map_cow(bmap
);
6849 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
6850 j
= isl_basic_map_alloc_inequality(bmap
);
6853 isl_seq_clr(bmap
->ineq
[j
], 1 + total
);
6855 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
6856 isl_int_set_si(bmap
->ineq
[j
][0], value
);
6858 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
6859 isl_int_set_si(bmap
->ineq
[j
][0], -value
);
6861 bmap
= isl_basic_map_simplify(bmap
);
6862 return isl_basic_map_finalize(bmap
);
6864 isl_basic_map_free(bmap
);
6868 __isl_give isl_basic_map
*isl_basic_map_lower_bound_si(
6869 __isl_take isl_basic_map
*bmap
,
6870 enum isl_dim_type type
, unsigned pos
, int value
)
6872 return basic_map_bound_si(bmap
, type
, pos
, value
, 0);
6875 /* Constrain the values of the given dimension to be no greater than "value".
6877 __isl_give isl_basic_map
*isl_basic_map_upper_bound_si(
6878 __isl_take isl_basic_map
*bmap
,
6879 enum isl_dim_type type
, unsigned pos
, int value
)
6881 return basic_map_bound_si(bmap
, type
, pos
, value
, 1);
6884 static __isl_give isl_map
*map_bound_si(__isl_take isl_map
*map
,
6885 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
6889 map
= isl_map_cow(map
);
6890 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
6891 return isl_map_free(map
);
6892 for (i
= 0; i
< map
->n
; ++i
) {
6893 map
->p
[i
] = basic_map_bound_si(map
->p
[i
],
6894 type
, pos
, value
, upper
);
6898 map
= isl_map_unmark_normalized(map
);
6905 __isl_give isl_map
*isl_map_lower_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
, 0);
6911 __isl_give isl_map
*isl_map_upper_bound_si(__isl_take isl_map
*map
,
6912 enum isl_dim_type type
, unsigned pos
, int value
)
6914 return map_bound_si(map
, type
, pos
, value
, 1);
6917 __isl_give isl_set
*isl_set_lower_bound_si(__isl_take isl_set
*set
,
6918 enum isl_dim_type type
, unsigned pos
, int value
)
6920 return set_from_map(isl_map_lower_bound_si(set_to_map(set
),
6924 __isl_give isl_set
*isl_set_upper_bound_si(__isl_take isl_set
*set
,
6925 enum isl_dim_type type
, unsigned pos
, int value
)
6927 return isl_map_upper_bound_si(set
, type
, pos
, value
);
6930 /* Bound the given variable of "bmap" from below (or above is "upper"
6931 * is set) to "value".
6933 static __isl_give isl_basic_map
*basic_map_bound(
6934 __isl_take isl_basic_map
*bmap
,
6935 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
6940 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6941 return isl_basic_map_free(bmap
);
6942 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
6944 return isl_basic_map_free(bmap
);
6945 pos
+= isl_basic_map_offset(bmap
, type
);
6946 bmap
= isl_basic_map_cow(bmap
);
6947 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
6948 j
= isl_basic_map_alloc_inequality(bmap
);
6951 isl_seq_clr(bmap
->ineq
[j
], 1 + total
);
6953 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
6954 isl_int_set(bmap
->ineq
[j
][0], value
);
6956 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
6957 isl_int_neg(bmap
->ineq
[j
][0], value
);
6959 bmap
= isl_basic_map_simplify(bmap
);
6960 return isl_basic_map_finalize(bmap
);
6962 isl_basic_map_free(bmap
);
6966 /* Bound the given variable of "map" from below (or above is "upper"
6967 * is set) to "value".
6969 static __isl_give isl_map
*map_bound(__isl_take isl_map
*map
,
6970 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
6974 map
= isl_map_cow(map
);
6975 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
6976 return isl_map_free(map
);
6977 for (i
= map
->n
- 1; i
>= 0; --i
) {
6978 map
->p
[i
] = basic_map_bound(map
->p
[i
], type
, pos
, value
, upper
);
6979 map
= remove_if_empty(map
, i
);
6983 map
= isl_map_unmark_normalized(map
);
6987 __isl_give isl_map
*isl_map_lower_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
, 0);
6993 __isl_give isl_map
*isl_map_upper_bound(__isl_take isl_map
*map
,
6994 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6996 return map_bound(map
, type
, pos
, value
, 1);
6999 __isl_give isl_set
*isl_set_lower_bound(__isl_take isl_set
*set
,
7000 enum isl_dim_type type
, unsigned pos
, isl_int value
)
7002 return isl_map_lower_bound(set
, type
, pos
, value
);
7005 __isl_give isl_set
*isl_set_upper_bound(__isl_take isl_set
*set
,
7006 enum isl_dim_type type
, unsigned pos
, isl_int value
)
7008 return isl_map_upper_bound(set
, type
, pos
, value
);
7011 /* Force the values of the variable at position "pos" of type "type" of "map"
7012 * to be no smaller than "value".
7014 __isl_give isl_map
*isl_map_lower_bound_val(__isl_take isl_map
*map
,
7015 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
7019 if (!isl_val_is_int(value
))
7020 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
7021 "expecting integer value", goto error
);
7022 map
= isl_map_lower_bound(map
, type
, pos
, value
->n
);
7023 isl_val_free(value
);
7026 isl_val_free(value
);
7031 /* Force the values of the variable at position "pos" of type "type" of "set"
7032 * to be no smaller than "value".
7034 __isl_give isl_set
*isl_set_lower_bound_val(__isl_take isl_set
*set
,
7035 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
7039 map
= set_to_map(set
);
7040 return set_from_map(isl_map_lower_bound_val(map
, type
, pos
, value
));
7043 /* Force the values of the variable at position "pos" of type "type" of "map"
7044 * to be no greater than "value".
7046 __isl_give isl_map
*isl_map_upper_bound_val(__isl_take isl_map
*map
,
7047 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
7051 if (!isl_val_is_int(value
))
7052 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
7053 "expecting integer value", goto error
);
7054 map
= isl_map_upper_bound(map
, type
, pos
, value
->n
);
7055 isl_val_free(value
);
7058 isl_val_free(value
);
7063 /* Force the values of the variable at position "pos" of type "type" of "set"
7064 * to be no greater than "value".
7066 __isl_give isl_set
*isl_set_upper_bound_val(__isl_take isl_set
*set
,
7067 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
7071 map
= set_to_map(set
);
7072 return set_from_map(isl_map_upper_bound_val(map
, type
, pos
, value
));
7075 /* If "mv" has an explicit domain, then intersect the domain of "map"
7076 * with this explicit domain.
7078 * An isl_multi_val object never has an explicit domain,
7079 * so simply return "map".
7081 static __isl_give isl_map
*isl_map_intersect_multi_val_explicit_domain(
7082 __isl_take isl_map
*map
, __isl_keep isl_multi_val
*mv
)
7089 #include "isl_map_bound_templ.c"
7091 /* Apply "map_bound" to "set" with the corresponding value in "bound"
7092 * for each set dimension, by treating the set as a map.
7094 static __isl_give isl_set
*set_bound_multi_val(__isl_take isl_set
*set
,
7095 __isl_take isl_multi_val
*bound
,
7096 __isl_give isl_map
*map_bound(__isl_take isl_map
*map
,
7097 unsigned pos
, __isl_take isl_val
*value
))
7101 map
= set_to_map(set
);
7102 return set_from_map(map_bound_multi_val(map
, bound
, map_bound
));
7107 #include "isl_map_bound_templ.c"
7109 /* Apply "map_bound" to "set" with the corresponding value in "bound"
7110 * for each set dimension, by converting the set and the bound
7111 * to objects living in a map space.
7113 static __isl_give isl_set
*set_bound_multi_pw_aff(__isl_take isl_set
*set
,
7114 __isl_take isl_multi_pw_aff
*bound
,
7115 __isl_give isl_map
*set_bound(__isl_take isl_map
*map
,
7116 unsigned pos
, __isl_take TYPE
*value
))
7120 map
= isl_map_from_range(set
);
7121 bound
= isl_multi_pw_aff_from_range(bound
);
7122 map
= map_bound_multi_pw_aff(map
, bound
, set_bound
);
7123 return isl_map_range(map
);
7126 /* Wrapper around isl_map_lower_bound_val for use in map_bound_multi_val,
7127 * setting a bound on the given output dimension.
7129 static __isl_give isl_map
*map_lower_bound_val(__isl_take isl_map
*map
,
7130 unsigned pos
, __isl_take isl_val
*v
)
7132 return isl_map_lower_bound_val(map
, isl_dim_out
, pos
, v
);
7135 /* Force the values of the set dimensions of "set"
7136 * to be no smaller than the corresponding values in "lower".
7138 __isl_give isl_set
*isl_set_lower_bound_multi_val(__isl_take isl_set
*set
,
7139 __isl_take isl_multi_val
*lower
)
7141 return set_bound_multi_val(set
, lower
, &map_lower_bound_val
);
7144 /* Wrapper around isl_map_upper_bound_val for use in map_bound_multi_val,
7145 * setting a bound on the given output dimension.
7147 static __isl_give isl_map
*map_upper_bound_val(__isl_take isl_map
*map
,
7148 unsigned pos
, __isl_take isl_val
*v
)
7150 return isl_map_upper_bound_val(map
, isl_dim_out
, pos
, v
);
7153 /* Force the values of the set dimensions of "set"
7154 * to be no greater than the corresponding values in "upper".
7156 __isl_give isl_set
*isl_set_upper_bound_multi_val(__isl_take isl_set
*set
,
7157 __isl_take isl_multi_val
*upper
)
7159 return set_bound_multi_val(set
, upper
, &map_upper_bound_val
);
7162 /* Force the symbolic constant expression "bound"
7163 * to satisfy the relation "order" with respect to
7164 * the output variable at position "pos" of "map".
7166 * Create an affine expression representing the output variable
7167 * in terms of the range and
7168 * compare it using "order" to "bound" (defined on the domain).
7169 * The result is a relation between elements in domain and range that
7170 * can be intersected with "map".
7172 static __isl_give isl_map
*map_bound_pw_aff(__isl_take isl_map
*map
,
7173 unsigned pos
, __isl_take isl_pw_aff
*bound
,
7174 __isl_give isl_map
*(*order
)(__isl_take isl_pw_aff
*pa1
,
7175 __isl_take isl_pw_aff
*pa2
))
7178 isl_local_space
*ls
;
7181 space
= isl_space_range(isl_map_get_space(map
));
7182 ls
= isl_local_space_from_space(space
);
7183 var
= isl_pw_aff_var_on_domain(ls
, isl_dim_set
, pos
);
7184 map
= isl_map_intersect(map
, order(bound
, var
));
7188 /* Force the values of the output variable at position "pos" of "map"
7189 * to be no smaller than the symbolic constant expression "lower".
7191 static __isl_give isl_map
*map_lower_bound_pw_aff(__isl_take isl_map
*map
,
7192 unsigned pos
, __isl_take isl_pw_aff
*lower
)
7194 return map_bound_pw_aff(map
, pos
, lower
, &isl_pw_aff_le_map
);
7197 /* Force the values of the output variable at position "pos" of "map"
7198 * to be no greater than the symbolic constant expression "upper".
7200 static __isl_give isl_map
*map_upper_bound_pw_aff(__isl_take isl_map
*map
,
7201 unsigned pos
, __isl_take isl_pw_aff
*upper
)
7203 return map_bound_pw_aff(map
, pos
, upper
, &isl_pw_aff_ge_map
);
7206 /* Force the values of the set dimensions of "set"
7207 * to be no smaller than the corresponding constant symbolic expressions
7210 __isl_give isl_set
*isl_set_lower_bound_multi_pw_aff(__isl_take isl_set
*set
,
7211 __isl_take isl_multi_pw_aff
*lower
)
7213 return set_bound_multi_pw_aff(set
, lower
, &map_lower_bound_pw_aff
);
7216 /* Force the values of the set dimensions of "set"
7217 * to be no greater than the corresponding constant symbolic expressions
7220 __isl_give isl_set
*isl_set_upper_bound_multi_pw_aff(__isl_take isl_set
*set
,
7221 __isl_take isl_multi_pw_aff
*upper
)
7223 return set_bound_multi_pw_aff(set
, upper
, &map_upper_bound_pw_aff
);
7226 /* Force the values of the output dimensions of "map"
7227 * to be no smaller than the corresponding constant symbolic expressions
7230 __isl_give isl_map
*isl_map_lower_bound_multi_pw_aff(__isl_take isl_map
*map
,
7231 __isl_take isl_multi_pw_aff
*lower
)
7233 return map_bound_multi_pw_aff(map
, lower
, &map_lower_bound_pw_aff
);
7236 /* Force the values of the output dimensions of "map"
7237 * to be no greater than the corresponding constant symbolic expressions
7240 __isl_give isl_map
*isl_map_upper_bound_multi_pw_aff(__isl_take isl_map
*map
,
7241 __isl_take isl_multi_pw_aff
*upper
)
7243 return map_bound_multi_pw_aff(map
, upper
, &map_upper_bound_pw_aff
);
7246 /* Bound the given variable of "bset" from below (or above is "upper"
7247 * is set) to "value".
7249 static __isl_give isl_basic_set
*isl_basic_set_bound(
7250 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned pos
,
7251 isl_int value
, int upper
)
7253 return bset_from_bmap(basic_map_bound(bset_to_bmap(bset
),
7254 type
, pos
, value
, upper
));
7257 /* Bound the given variable of "bset" from below (or above is "upper"
7258 * is set) to "value".
7260 static __isl_give isl_basic_set
*isl_basic_set_bound_val(
7261 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned pos
,
7262 __isl_take isl_val
*value
, int upper
)
7266 if (!isl_val_is_int(value
))
7267 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
7268 "expecting integer value", goto error
);
7269 bset
= isl_basic_set_bound(bset
, type
, pos
, value
->n
, upper
);
7270 isl_val_free(value
);
7273 isl_val_free(value
);
7274 isl_basic_set_free(bset
);
7278 /* Bound the given variable of "bset" from below to "value".
7280 __isl_give isl_basic_set
*isl_basic_set_lower_bound_val(
7281 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned pos
,
7282 __isl_take isl_val
*value
)
7284 return isl_basic_set_bound_val(bset
, type
, pos
, value
, 0);
7287 /* Bound the given variable of "bset" from above to "value".
7289 __isl_give isl_basic_set
*isl_basic_set_upper_bound_val(
7290 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned pos
,
7291 __isl_take isl_val
*value
)
7293 return isl_basic_set_bound_val(bset
, type
, pos
, value
, 1);
7296 __isl_give isl_map
*isl_map_reverse(__isl_take isl_map
*map
)
7298 return isl_map_transform(map
, &isl_space_reverse
,
7299 &isl_basic_map_reverse
);
7302 /* Given a map (A -> B) -> C, return the corresponding map (B -> A) -> C.
7304 __isl_give isl_map
*isl_map_domain_reverse(__isl_take isl_map
*map
)
7306 return isl_map_transform(map
, &isl_space_domain_reverse
,
7307 &isl_basic_map_domain_reverse
);
7310 /* Given a map A -> (B -> C), return the corresponding map A -> (C -> B).
7312 __isl_give isl_map
*isl_map_range_reverse(__isl_take isl_map
*map
)
7314 return isl_map_transform(map
, &isl_space_range_reverse
,
7315 &isl_basic_map_range_reverse
);
7318 /* Given a set (A -> B), return the corresponding set (B -> A).
7320 __isl_give isl_set
*isl_set_wrapped_reverse(__isl_take isl_set
*set
)
7322 isl_map
*map
= set_to_map(set
);
7324 map
= isl_map_transform(map
, &isl_space_wrapped_reverse
,
7325 &isl_basic_map_set_reverse
);
7326 return set_from_map(map
);
7330 #define TYPE isl_pw_multi_aff
7332 #define SUFFIX _pw_multi_aff
7334 #define EMPTY isl_pw_multi_aff_empty
7336 #define ADD isl_pw_multi_aff_union_add
7337 #include "isl_map_lexopt_templ.c"
7339 /* Given a map "map", compute the lexicographically minimal
7340 * (or maximal) image element for each domain element in dom,
7341 * in the form of an isl_pw_multi_aff.
7342 * If "empty" is not NULL, then set *empty to those elements in dom that
7343 * do not have an image element.
7344 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
7345 * should be computed over the domain of "map". "empty" is also NULL
7348 * We first compute the lexicographically minimal or maximal element
7349 * in the first basic map. This results in a partial solution "res"
7350 * and a subset "todo" of dom that still need to be handled.
7351 * We then consider each of the remaining maps in "map" and successively
7352 * update both "res" and "todo".
7353 * If "empty" is NULL, then the todo sets are not needed and therefore
7354 * also not computed.
7356 static __isl_give isl_pw_multi_aff
*isl_map_partial_lexopt_aligned_pw_multi_aff(
7357 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
7358 __isl_give isl_set
**empty
, unsigned flags
)
7362 isl_pw_multi_aff
*res
;
7365 full
= ISL_FL_ISSET(flags
, ISL_OPT_FULL
);
7366 if (!map
|| (!full
&& !dom
))
7369 if (isl_map_plain_is_empty(map
)) {
7374 return isl_pw_multi_aff_from_map(map
);
7377 res
= basic_map_partial_lexopt_pw_multi_aff(
7378 isl_basic_map_copy(map
->p
[0]),
7379 isl_set_copy(dom
), empty
, flags
);
7383 for (i
= 1; i
< map
->n
; ++i
) {
7384 isl_pw_multi_aff
*res_i
;
7386 res_i
= basic_map_partial_lexopt_pw_multi_aff(
7387 isl_basic_map_copy(map
->p
[i
]),
7388 isl_set_copy(dom
), empty
, flags
);
7390 if (ISL_FL_ISSET(flags
, ISL_OPT_MAX
))
7391 res
= isl_pw_multi_aff_union_lexmax(res
, res_i
);
7393 res
= isl_pw_multi_aff_union_lexmin(res
, res_i
);
7396 todo
= isl_set_intersect(todo
, *empty
);
7415 #define TYPE isl_map
7419 #define EMPTY isl_map_empty
7421 #define ADD isl_map_union_disjoint
7422 #include "isl_map_lexopt_templ.c"
7424 /* Given a map "map", compute the lexicographically minimal
7425 * (or maximal) image element for each domain element in "dom",
7426 * in the form of an isl_map.
7427 * If "empty" is not NULL, then set *empty to those elements in "dom" that
7428 * do not have an image element.
7429 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
7430 * should be computed over the domain of "map". "empty" is also NULL
7433 * If the input consists of more than one disjunct, then first
7434 * compute the desired result in the form of an isl_pw_multi_aff and
7435 * then convert that into an isl_map.
7437 * This function used to have an explicit implementation in terms
7438 * of isl_maps, but it would continually intersect the domains of
7439 * partial results with the complement of the domain of the next
7440 * partial solution, potentially leading to an explosion in the number
7441 * of disjuncts if there are several disjuncts in the input.
7442 * An even earlier implementation of this function would look for
7443 * better results in the domain of the partial result and for extra
7444 * results in the complement of this domain, which would lead to
7445 * even more splintering.
7447 static __isl_give isl_map
*isl_map_partial_lexopt_aligned(
7448 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
7449 __isl_give isl_set
**empty
, unsigned flags
)
7452 struct isl_map
*res
;
7453 isl_pw_multi_aff
*pma
;
7455 full
= ISL_FL_ISSET(flags
, ISL_OPT_FULL
);
7456 if (!map
|| (!full
&& !dom
))
7459 if (isl_map_plain_is_empty(map
)) {
7468 res
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[0]),
7474 pma
= isl_map_partial_lexopt_aligned_pw_multi_aff(map
, dom
, empty
,
7476 return isl_map_from_pw_multi_aff_internal(pma
);
7485 __isl_give isl_map
*isl_map_partial_lexmax(
7486 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
7487 __isl_give isl_set
**empty
)
7489 return isl_map_partial_lexopt(map
, dom
, empty
, ISL_OPT_MAX
);
7492 __isl_give isl_map
*isl_map_partial_lexmin(
7493 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
7494 __isl_give isl_set
**empty
)
7496 return isl_map_partial_lexopt(map
, dom
, empty
, 0);
7499 __isl_give isl_set
*isl_set_partial_lexmin(
7500 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
7501 __isl_give isl_set
**empty
)
7503 return set_from_map(isl_map_partial_lexmin(set_to_map(set
),
7507 __isl_give isl_set
*isl_set_partial_lexmax(
7508 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
7509 __isl_give isl_set
**empty
)
7511 return set_from_map(isl_map_partial_lexmax(set_to_map(set
),
7515 /* Compute the lexicographic minimum (or maximum if "flags" includes
7516 * ISL_OPT_MAX) of "bset" over its parametric domain.
7518 __isl_give isl_set
*isl_basic_set_lexopt(__isl_take isl_basic_set
*bset
,
7521 return isl_basic_map_lexopt(bset
, flags
);
7524 __isl_give isl_map
*isl_basic_map_lexmax(__isl_take isl_basic_map
*bmap
)
7526 return isl_basic_map_lexopt(bmap
, ISL_OPT_MAX
);
7529 __isl_give isl_set
*isl_basic_set_lexmin(__isl_take isl_basic_set
*bset
)
7531 return set_from_map(isl_basic_map_lexmin(bset_to_bmap(bset
)));
7534 __isl_give isl_set
*isl_basic_set_lexmax(__isl_take isl_basic_set
*bset
)
7536 return set_from_map(isl_basic_map_lexmax(bset_to_bmap(bset
)));
7539 /* Compute the lexicographic minimum of "bset" over its parametric domain
7540 * for the purpose of quantifier elimination.
7541 * That is, find an explicit representation for all the existentially
7542 * quantified variables in "bset" by computing their lexicographic
7545 static __isl_give isl_set
*isl_basic_set_lexmin_compute_divs(
7546 __isl_take isl_basic_set
*bset
)
7548 return isl_basic_set_lexopt(bset
, ISL_OPT_QE
);
7551 /* Given a basic map with one output dimension, compute the minimum or
7552 * maximum of that dimension as an isl_pw_aff.
7554 * Compute the optimum as a lexicographic optimum over the single
7555 * output dimension and extract the single isl_pw_aff from the result.
7557 static __isl_give isl_pw_aff
*basic_map_dim_opt(__isl_keep isl_basic_map
*bmap
,
7560 isl_pw_multi_aff
*pma
;
7563 bmap
= isl_basic_map_copy(bmap
);
7564 pma
= isl_basic_map_lexopt_pw_multi_aff(bmap
, max
? ISL_OPT_MAX
: 0);
7565 pwaff
= isl_pw_multi_aff_get_pw_aff(pma
, 0);
7566 isl_pw_multi_aff_free(pma
);
7571 /* Compute the minimum or maximum of the given output dimension
7572 * as a function of the parameters and the input dimensions,
7573 * but independently of the other output dimensions.
7575 * We first project out the other output dimension and then compute
7576 * the "lexicographic" maximum in each basic map, combining the results
7577 * using isl_pw_aff_union_max.
7579 static __isl_give isl_pw_aff
*map_dim_opt(__isl_take isl_map
*map
, int pos
,
7586 n_out
= isl_map_dim(map
, isl_dim_out
);
7588 map
= isl_map_free(map
);
7589 map
= isl_map_project_out(map
, isl_dim_out
, pos
+ 1, n_out
- (pos
+ 1));
7590 map
= isl_map_project_out(map
, isl_dim_out
, 0, pos
);
7595 isl_space
*space
= isl_map_get_space(map
);
7597 return isl_pw_aff_empty(space
);
7600 pwaff
= basic_map_dim_opt(map
->p
[0], max
);
7601 for (i
= 1; i
< map
->n
; ++i
) {
7602 isl_pw_aff
*pwaff_i
;
7604 pwaff_i
= basic_map_dim_opt(map
->p
[i
], max
);
7605 pwaff
= isl_pw_aff_union_opt(pwaff
, pwaff_i
, max
);
7613 /* Compute the minimum of the given output dimension as a function of the
7614 * parameters and input dimensions, but independently of
7615 * the other output dimensions.
7617 __isl_give isl_pw_aff
*isl_map_dim_min(__isl_take isl_map
*map
, int pos
)
7619 return map_dim_opt(map
, pos
, 0);
7622 /* Compute the maximum of the given output dimension as a function of the
7623 * parameters and input dimensions, but independently of
7624 * the other output dimensions.
7626 __isl_give isl_pw_aff
*isl_map_dim_max(__isl_take isl_map
*map
, int pos
)
7628 return map_dim_opt(map
, pos
, 1);
7631 /* Compute the minimum or maximum of the given set dimension
7632 * as a function of the parameters,
7633 * but independently of the other set dimensions.
7635 static __isl_give isl_pw_aff
*set_dim_opt(__isl_take isl_set
*set
, int pos
,
7638 return map_dim_opt(set
, pos
, max
);
7641 /* Compute the maximum of the given set dimension as a function of the
7642 * parameters, but independently of the other set dimensions.
7644 __isl_give isl_pw_aff
*isl_set_dim_max(__isl_take isl_set
*set
, int pos
)
7646 return set_dim_opt(set
, pos
, 1);
7649 /* Compute the minimum of the given set dimension as a function of the
7650 * parameters, but independently of the other set dimensions.
7652 __isl_give isl_pw_aff
*isl_set_dim_min(__isl_take isl_set
*set
, int pos
)
7654 return set_dim_opt(set
, pos
, 0);
7657 /* Apply a preimage specified by "mat" on the parameters of "bset".
7658 * bset is assumed to have only parameters and divs.
7660 static __isl_give isl_basic_set
*basic_set_parameter_preimage(
7661 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*mat
)
7665 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
7666 if (nparam
< 0 || !mat
)
7669 bset
->dim
= isl_space_cow(bset
->dim
);
7673 isl_assert(bset
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
7675 bset
->dim
->nparam
= 0;
7676 bset
->dim
->n_out
= nparam
;
7677 bset
= isl_basic_set_preimage(bset
, mat
);
7679 bset
->dim
->nparam
= bset
->dim
->n_out
;
7680 bset
->dim
->n_out
= 0;
7685 isl_basic_set_free(bset
);
7689 /* Apply a preimage specified by "mat" on the parameters of "set".
7690 * set is assumed to have only parameters and divs.
7692 static __isl_give isl_set
*set_parameter_preimage(__isl_take isl_set
*set
,
7693 __isl_take isl_mat
*mat
)
7698 nparam
= isl_set_dim(set
, isl_dim_param
);
7699 if (nparam
< 0 || !mat
)
7702 if (mat
->n_row
!= 1 + nparam
)
7703 isl_die(isl_set_get_ctx(set
), isl_error_internal
,
7704 "unexpected number of rows", goto error
);
7706 space
= isl_set_get_space(set
);
7707 space
= isl_space_move_dims(space
, isl_dim_set
, 0,
7708 isl_dim_param
, 0, nparam
);
7709 set
= isl_set_reset_space(set
, space
);
7710 set
= isl_set_preimage(set
, mat
);
7711 nparam
= isl_set_dim(set
, isl_dim_out
);
7713 set
= isl_set_free(set
);
7714 space
= isl_set_get_space(set
);
7715 space
= isl_space_move_dims(space
, isl_dim_param
, 0,
7716 isl_dim_out
, 0, nparam
);
7717 set
= isl_set_reset_space(set
, space
);
7725 /* Intersect the basic set "bset" with the affine space specified by the
7726 * equalities in "eq".
7728 static __isl_give isl_basic_set
*basic_set_append_equalities(
7729 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*eq
)
7737 bset
= isl_basic_set_extend(bset
, 0, eq
->n_row
, 0);
7741 len
= isl_basic_set_offset(bset
, isl_dim_div
) + bset
->extra
;
7742 for (i
= 0; i
< eq
->n_row
; ++i
) {
7743 k
= isl_basic_set_alloc_equality(bset
);
7746 isl_seq_cpy(bset
->eq
[k
], eq
->row
[i
], eq
->n_col
);
7747 isl_seq_clr(bset
->eq
[k
] + eq
->n_col
, len
- eq
->n_col
);
7751 bset
= isl_basic_set_gauss(bset
, NULL
);
7752 bset
= isl_basic_set_finalize(bset
);
7757 isl_basic_set_free(bset
);
7761 /* Intersect the set "set" with the affine space specified by the
7762 * equalities in "eq".
7764 static __isl_give isl_set
*set_append_equalities(__isl_take isl_set
*set
,
7765 __isl_take isl_mat
*eq
)
7772 for (i
= 0; i
< set
->n
; ++i
) {
7773 set
->p
[i
] = basic_set_append_equalities(set
->p
[i
],
7786 /* Given a basic set "bset" that only involves parameters and existentially
7787 * quantified variables, return the index of the first equality
7788 * that only involves parameters. If there is no such equality then
7789 * return bset->n_eq.
7791 * This function assumes that isl_basic_set_gauss has been called on "bset".
7793 static int first_parameter_equality(__isl_keep isl_basic_set
*bset
)
7796 isl_size nparam
, n_div
;
7798 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
7799 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
7800 if (nparam
< 0 || n_div
< 0)
7803 for (i
= 0, j
= n_div
- 1; i
< bset
->n_eq
&& j
>= 0; --j
) {
7804 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ j
]))
7811 /* Compute an explicit representation for the existentially quantified
7812 * variables in "bset" by computing the "minimal value" of the set
7813 * variables. Since there are no set variables, the computation of
7814 * the minimal value essentially computes an explicit representation
7815 * of the non-empty part(s) of "bset".
7817 * The input only involves parameters and existentially quantified variables.
7818 * All equalities among parameters have been removed.
7820 * Since the existentially quantified variables in the result are in general
7821 * going to be different from those in the input, we first replace
7822 * them by the minimal number of variables based on their equalities.
7823 * This should simplify the parametric integer programming.
7825 static __isl_give isl_set
*base_compute_divs(__isl_take isl_basic_set
*bset
)
7827 isl_morph
*morph1
, *morph2
;
7833 if (bset
->n_eq
== 0)
7834 return isl_basic_set_lexmin_compute_divs(bset
);
7836 morph1
= isl_basic_set_parameter_compression(bset
);
7837 bset
= isl_morph_basic_set(isl_morph_copy(morph1
), bset
);
7838 bset
= isl_basic_set_lift(bset
);
7839 morph2
= isl_basic_set_variable_compression(bset
, isl_dim_set
);
7840 bset
= isl_morph_basic_set(morph2
, bset
);
7841 n
= isl_basic_set_dim(bset
, isl_dim_set
);
7843 bset
= isl_basic_set_free(bset
);
7844 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
7846 set
= isl_basic_set_lexmin_compute_divs(bset
);
7848 set
= isl_morph_set(isl_morph_inverse(morph1
), set
);
7853 /* Project the given basic set onto its parameter domain, possibly introducing
7854 * new, explicit, existential variables in the constraints.
7855 * The input has parameters and (possibly implicit) existential variables.
7856 * The output has the same parameters, but only
7857 * explicit existentially quantified variables.
7859 * The actual projection is performed by pip, but pip doesn't seem
7860 * to like equalities very much, so we first remove the equalities
7861 * among the parameters by performing a variable compression on
7862 * the parameters. Afterward, an inverse transformation is performed
7863 * and the equalities among the parameters are inserted back in.
7865 * The variable compression on the parameters may uncover additional
7866 * equalities that were only implicit before. We therefore check
7867 * if there are any new parameter equalities in the result and
7868 * if so recurse. The removal of parameter equalities is required
7869 * for the parameter compression performed by base_compute_divs.
7871 static __isl_give isl_set
*parameter_compute_divs(
7872 __isl_take isl_basic_set
*bset
)
7876 struct isl_mat
*T
, *T2
;
7877 struct isl_set
*set
;
7880 bset
= isl_basic_set_cow(bset
);
7884 if (bset
->n_eq
== 0)
7885 return base_compute_divs(bset
);
7887 bset
= isl_basic_set_gauss(bset
, NULL
);
7890 if (isl_basic_set_plain_is_empty(bset
))
7891 return isl_set_from_basic_set(bset
);
7893 i
= first_parameter_equality(bset
);
7894 if (i
== bset
->n_eq
)
7895 return base_compute_divs(bset
);
7897 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
7899 return isl_set_from_basic_set(isl_basic_set_free(bset
));
7900 eq
= isl_mat_sub_alloc6(bset
->ctx
, bset
->eq
, i
, bset
->n_eq
- i
,
7902 eq
= isl_mat_cow(eq
);
7903 T
= isl_mat_variable_compression(isl_mat_copy(eq
), &T2
);
7904 if (T
&& T
->n_col
== 0) {
7908 bset
= isl_basic_set_set_to_empty(bset
);
7909 return isl_set_from_basic_set(bset
);
7911 bset
= basic_set_parameter_preimage(bset
, T
);
7913 i
= first_parameter_equality(bset
);
7916 else if (i
== bset
->n_eq
)
7917 set
= base_compute_divs(bset
);
7919 set
= parameter_compute_divs(bset
);
7920 set
= set_parameter_preimage(set
, T2
);
7921 set
= set_append_equalities(set
, eq
);
7925 /* Insert the divs from "ls" before those of "bmap".
7927 * The number of columns is not changed, which means that the last
7928 * dimensions of "bmap" are being reintepreted as the divs from "ls".
7929 * The caller is responsible for removing the same number of dimensions
7930 * from the space of "bmap".
7932 static __isl_give isl_basic_map
*insert_divs_from_local_space(
7933 __isl_take isl_basic_map
*bmap
, __isl_keep isl_local_space
*ls
)
7939 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
7941 return isl_basic_map_free(bmap
);
7945 old_n_div
= bmap
->n_div
;
7946 bmap
= insert_div_rows(bmap
, n_div
);
7950 for (i
= 0; i
< n_div
; ++i
) {
7951 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], ls
->div
->n_col
);
7952 isl_seq_clr(bmap
->div
[i
] + ls
->div
->n_col
, old_n_div
);
7958 /* Replace the space of "bmap" by the space and divs of "ls".
7960 * If "ls" has any divs, then we simplify the result since we may
7961 * have discovered some additional equalities that could simplify
7962 * the div expressions.
7964 static __isl_give isl_basic_map
*basic_replace_space_by_local_space(
7965 __isl_take isl_basic_map
*bmap
, __isl_take isl_local_space
*ls
)
7969 bmap
= isl_basic_map_cow(bmap
);
7970 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
7971 if (!bmap
|| n_div
< 0)
7974 bmap
= insert_divs_from_local_space(bmap
, ls
);
7978 isl_space_free(bmap
->dim
);
7979 bmap
->dim
= isl_local_space_get_space(ls
);
7983 isl_local_space_free(ls
);
7985 bmap
= isl_basic_map_simplify(bmap
);
7986 bmap
= isl_basic_map_finalize(bmap
);
7989 isl_basic_map_free(bmap
);
7990 isl_local_space_free(ls
);
7994 /* Replace the space of "map" by the space and divs of "ls".
7996 static __isl_give isl_map
*replace_space_by_local_space(__isl_take isl_map
*map
,
7997 __isl_take isl_local_space
*ls
)
8001 map
= isl_map_cow(map
);
8005 for (i
= 0; i
< map
->n
; ++i
) {
8006 map
->p
[i
] = basic_replace_space_by_local_space(map
->p
[i
],
8007 isl_local_space_copy(ls
));
8011 isl_space_free(isl_map_take_space(map
));
8012 map
= isl_map_restore_space(map
, isl_local_space_get_space(ls
));
8014 isl_local_space_free(ls
);
8017 isl_local_space_free(ls
);
8022 /* Compute an explicit representation for the existentially
8023 * quantified variables for which do not know any explicit representation yet.
8025 * We first sort the existentially quantified variables so that the
8026 * existentially quantified variables for which we already have an explicit
8027 * representation are placed before those for which we do not.
8028 * The input dimensions, the output dimensions and the existentially
8029 * quantified variables for which we already have an explicit
8030 * representation are then turned into parameters.
8031 * compute_divs returns a map with the same parameters and
8032 * no input or output dimensions and the dimension specification
8033 * is reset to that of the input, including the existentially quantified
8034 * variables for which we already had an explicit representation.
8036 static __isl_give isl_map
*compute_divs(__isl_take isl_basic_map
*bmap
)
8038 struct isl_basic_set
*bset
;
8039 struct isl_set
*set
;
8040 struct isl_map
*map
;
8042 isl_local_space
*ls
;
8049 bmap
= isl_basic_map_sort_divs(bmap
);
8050 bmap
= isl_basic_map_cow(bmap
);
8054 n_known
= isl_basic_map_first_unknown_div(bmap
);
8055 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
8056 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
8057 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
8058 if (n_known
< 0 || nparam
< 0 || n_in
< 0 || n_out
< 0)
8059 return isl_map_from_basic_map(isl_basic_map_free(bmap
));
8061 space
= isl_space_set_alloc(bmap
->ctx
,
8062 nparam
+ n_in
+ n_out
+ n_known
, 0);
8066 ls
= isl_basic_map_get_local_space(bmap
);
8067 ls
= isl_local_space_drop_dims(ls
, isl_dim_div
,
8068 n_known
, bmap
->n_div
- n_known
);
8070 for (i
= n_known
; i
< bmap
->n_div
; ++i
)
8071 swap_div(bmap
, i
- n_known
, i
);
8072 bmap
->n_div
-= n_known
;
8073 bmap
->extra
-= n_known
;
8075 bmap
= isl_basic_map_reset_space(bmap
, space
);
8076 bset
= bset_from_bmap(bmap
);
8078 set
= parameter_compute_divs(bset
);
8079 map
= set_to_map(set
);
8080 map
= replace_space_by_local_space(map
, ls
);
8084 isl_basic_map_free(bmap
);
8088 /* Is the integer division at position "div" of "bmap" integral?
8089 * That is, does it have denominator 1?
8091 isl_bool
isl_basic_map_div_is_integral(__isl_keep isl_basic_map
*bmap
, int div
)
8093 if (isl_basic_map_check_range(bmap
, isl_dim_div
, div
, 1) < 0)
8094 return isl_bool_error
;
8095 return isl_int_is_one(bmap
->div
[div
][0]);
8098 /* Remove the explicit representation of local variable "div",
8101 __isl_give isl_basic_map
*isl_basic_map_mark_div_unknown(
8102 __isl_take isl_basic_map
*bmap
, int div
)
8106 unknown
= isl_basic_map_div_is_marked_unknown(bmap
, div
);
8108 return isl_basic_map_free(bmap
);
8112 bmap
= isl_basic_map_cow(bmap
);
8115 isl_int_set_si(bmap
->div
[div
][0], 0);
8119 /* Is local variable "div" of "bmap" marked as not having an explicit
8121 * Note that even if "div" is not marked in this way and therefore
8122 * has an explicit representation, this representation may still
8123 * depend (indirectly) on other local variables that do not
8124 * have an explicit representation.
8126 isl_bool
isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map
*bmap
,
8129 if (isl_basic_map_check_range(bmap
, isl_dim_div
, div
, 1) < 0)
8130 return isl_bool_error
;
8131 return isl_int_is_zero(bmap
->div
[div
][0]);
8134 /* Return the position of the first local variable that does not
8135 * have an explicit representation.
8136 * Return the total number of local variables if they all have
8137 * an explicit representation.
8138 * Return -1 on error.
8140 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map
*bmap
)
8147 for (i
= 0; i
< bmap
->n_div
; ++i
) {
8148 if (!isl_basic_map_div_is_known(bmap
, i
))
8154 /* Return the position of the first local variable that does not
8155 * have an explicit representation.
8156 * Return the total number of local variables if they all have
8157 * an explicit representation.
8158 * Return -1 on error.
8160 int isl_basic_set_first_unknown_div(__isl_keep isl_basic_set
*bset
)
8162 return isl_basic_map_first_unknown_div(bset
);
8165 /* Does "bmap" have an explicit representation for all local variables?
8167 isl_bool
isl_basic_map_divs_known(__isl_keep isl_basic_map
*bmap
)
8172 n
= isl_basic_map_dim(bmap
, isl_dim_div
);
8173 first
= isl_basic_map_first_unknown_div(bmap
);
8174 if (n
< 0 || first
< 0)
8175 return isl_bool_error
;
8179 /* Do all basic maps in "map" have an explicit representation
8180 * for all local variables?
8182 isl_bool
isl_map_divs_known(__isl_keep isl_map
*map
)
8187 return isl_bool_error
;
8189 for (i
= 0; i
< map
->n
; ++i
) {
8190 int known
= isl_basic_map_divs_known(map
->p
[i
]);
8195 return isl_bool_true
;
8198 /* If bmap contains any unknown divs, then compute explicit
8199 * expressions for them. However, this computation may be
8200 * quite expensive, so first try to remove divs that aren't
8203 __isl_give isl_map
*isl_basic_map_compute_divs(__isl_take isl_basic_map
*bmap
)
8206 struct isl_map
*map
;
8208 known
= isl_basic_map_divs_known(bmap
);
8212 return isl_map_from_basic_map(bmap
);
8214 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
8216 known
= isl_basic_map_divs_known(bmap
);
8220 return isl_map_from_basic_map(bmap
);
8222 map
= compute_divs(bmap
);
8225 isl_basic_map_free(bmap
);
8229 __isl_give isl_map
*isl_map_compute_divs(__isl_take isl_map
*map
)
8233 struct isl_map
*res
;
8240 known
= isl_map_divs_known(map
);
8248 res
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[0]));
8249 for (i
= 1 ; i
< map
->n
; ++i
) {
8251 r2
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[i
]));
8252 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
8253 res
= isl_map_union_disjoint(res
, r2
);
8255 res
= isl_map_union(res
, r2
);
8262 __isl_give isl_set
*isl_basic_set_compute_divs(__isl_take isl_basic_set
*bset
)
8264 return set_from_map(isl_basic_map_compute_divs(bset_to_bmap(bset
)));
8267 __isl_give isl_set
*isl_set_compute_divs(__isl_take isl_set
*set
)
8269 return set_from_map(isl_map_compute_divs(set_to_map(set
)));
8272 __isl_give isl_set
*isl_map_domain(__isl_take isl_map
*map
)
8277 n_out
= isl_map_dim(map
, isl_dim_out
);
8279 return set_from_map(isl_map_free(map
));
8280 space
= isl_space_domain(isl_map_get_space(map
));
8282 map
= isl_map_project_out(map
, isl_dim_out
, 0, n_out
);
8284 return set_from_map(isl_map_reset_space(map
, space
));
8287 /* Return the union of "map1" and "map2", where we assume for now that
8288 * "map1" and "map2" are disjoint. Note that the basic maps inside
8289 * "map1" or "map2" may not be disjoint from each other.
8290 * Also note that this function is also called from isl_map_union,
8291 * which takes care of handling the situation where "map1" and "map2"
8292 * may not be disjoint.
8294 * If one of the inputs is empty, we can simply return the other input.
8295 * Similarly, if one of the inputs is universal, then it is equal to the union.
8297 static __isl_give isl_map
*map_union_disjoint(__isl_take isl_map
*map1
,
8298 __isl_take isl_map
*map2
)
8302 struct isl_map
*map
= NULL
;
8305 if (isl_map_check_equal_space(map1
, map2
) < 0)
8317 is_universe
= isl_map_plain_is_universe(map1
);
8318 if (is_universe
< 0)
8325 is_universe
= isl_map_plain_is_universe(map2
);
8326 if (is_universe
< 0)
8333 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
8334 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
8335 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
8337 map
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
8338 map1
->n
+ map2
->n
, flags
);
8341 for (i
= 0; i
< map1
->n
; ++i
) {
8342 map
= isl_map_add_basic_map(map
,
8343 isl_basic_map_copy(map1
->p
[i
]));
8347 for (i
= 0; i
< map2
->n
; ++i
) {
8348 map
= isl_map_add_basic_map(map
,
8349 isl_basic_map_copy(map2
->p
[i
]));
8363 /* Return the union of "map1" and "map2", where "map1" and "map2" are
8364 * guaranteed to be disjoint by the caller.
8366 * Note that this functions is called from within isl_map_make_disjoint,
8367 * so we have to be careful not to touch the constraints of the inputs
8370 __isl_give isl_map
*isl_map_union_disjoint(__isl_take isl_map
*map1
,
8371 __isl_take isl_map
*map2
)
8373 isl_map_align_params_bin(&map1
, &map2
);
8374 return map_union_disjoint(map1
, map2
);
8377 /* Return the union of "map1" and "map2", where "map1" and "map2" may
8380 * We currently simply call map_union_disjoint, the internal operation
8381 * of which does not really depend on the inputs being disjoint.
8382 * If the result contains more than one basic map, then we clear
8383 * the disjoint flag since the result may contain basic maps from
8384 * both inputs and these are not guaranteed to be disjoint.
8386 * As a special case, if "map1" and "map2" are obviously equal,
8387 * then we simply return "map1".
8389 __isl_give isl_map
*isl_map_union(__isl_take isl_map
*map1
,
8390 __isl_take isl_map
*map2
)
8394 if (isl_map_align_params_bin(&map1
, &map2
) < 0)
8397 equal
= isl_map_plain_is_equal(map1
, map2
);
8405 map1
= map_union_disjoint(map1
, map2
);
8409 ISL_F_CLR(map1
, ISL_MAP_DISJOINT
);
8417 __isl_give isl_set
*isl_set_union_disjoint(
8418 __isl_take isl_set
*set1
, __isl_take isl_set
*set2
)
8420 return set_from_map(isl_map_union_disjoint(set_to_map(set1
),
8424 __isl_give isl_set
*isl_set_union(__isl_take isl_set
*set1
,
8425 __isl_take isl_set
*set2
)
8427 return set_from_map(isl_map_union(set_to_map(set1
), set_to_map(set2
)));
8430 /* Apply "fn" to pairs of elements from "map" and "set" and collect
8431 * the results in a map living in "space".
8433 * "map" and "set" are assumed to be compatible and non-NULL.
8435 static __isl_give isl_map
*map_intersect_set(__isl_take isl_map
*map
,
8436 __isl_take isl_space
*space
, __isl_take isl_set
*set
,
8437 __isl_give isl_basic_map
*fn(__isl_take isl_basic_map
*bmap
,
8438 __isl_take isl_basic_set
*bset
))
8441 struct isl_map
*result
;
8444 if (isl_set_plain_is_universe(set
)) {
8446 return isl_map_reset_equal_dim_space(map
, space
);
8449 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
) &&
8450 ISL_F_ISSET(set
, ISL_MAP_DISJOINT
))
8451 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
8453 result
= isl_map_alloc_space(space
, map
->n
* set
->n
, flags
);
8454 for (i
= 0; result
&& i
< map
->n
; ++i
)
8455 for (j
= 0; j
< set
->n
; ++j
) {
8456 result
= isl_map_add_basic_map(result
,
8457 fn(isl_basic_map_copy(map
->p
[i
]),
8458 isl_basic_set_copy(set
->p
[j
])));
8468 __isl_give isl_map
*isl_map_intersect_range(__isl_take isl_map
*map
,
8469 __isl_take isl_set
*set
)
8474 isl_map_align_params_set(&map
, &set
);
8475 ok
= isl_map_compatible_range(map
, set
);
8479 isl_die(set
->ctx
, isl_error_invalid
,
8480 "incompatible spaces", goto error
);
8482 space
= isl_map_get_space(map
);
8483 return map_intersect_set(map
, space
, set
,
8484 &isl_basic_map_intersect_range
);
8491 /* Intersect the domain of "map" with "set".
8493 * If the domain dimensions of "map" do not have any identifiers,
8494 * then copy them over from "set".
8496 __isl_give isl_map
*isl_map_intersect_domain(__isl_take isl_map
*map
,
8497 __isl_take isl_set
*set
)
8502 isl_map_align_params_set(&map
, &set
);
8503 ok
= isl_map_compatible_domain(map
, set
);
8507 isl_die(set
->ctx
, isl_error_invalid
,
8508 "incompatible spaces", goto error
);
8510 space
= isl_map_get_space(map
);
8511 space
= isl_space_copy_ids_if_unset(space
, isl_dim_in
,
8512 isl_set_peek_space(set
), isl_dim_set
);
8513 return map_intersect_set(map
, space
, set
,
8514 &isl_basic_map_intersect_domain
);
8524 #include "isl_copy_tuple_id_templ.c"
8526 /* Data structure that specifies how isl_map_intersect_factor
8529 * "preserve_type" is the tuple where the factor differs from
8530 * the input map and of which the identifiers needs
8531 * to be preserved explicitly.
8532 * "other_factor" is used to extract the space of the other factor
8533 * from the space of the product ("map").
8534 * "product" is used to combine the given factor and a universe map
8535 * in the space returned by "other_factor" to produce a map
8536 * that lives in the same space as the input map.
8538 struct isl_intersect_factor_control
{
8539 enum isl_dim_type preserve_type
;
8540 __isl_give isl_space
*(*other_factor
)(__isl_take isl_space
*space
);
8541 __isl_give isl_map
*(*product
)(__isl_take isl_map
*factor
,
8542 __isl_take isl_map
*other
);
8545 /* Given a map "map" in some product space and a map "factor"
8546 * living in some factor space, return the intersection.
8548 * After aligning the parameters,
8549 * the map "factor" is first extended to a map living in the same space
8550 * as "map" and then a regular intersection is computed.
8552 * Note that the extension is computed as a product, which is anonymous
8553 * by default. If "map" has an identifier on the corresponding tuple,
8554 * then this identifier needs to be set on the product
8555 * before the intersection is computed.
8557 static __isl_give isl_map
*isl_map_intersect_factor(
8558 __isl_take isl_map
*map
, __isl_take isl_map
*factor
,
8559 struct isl_intersect_factor_control
*control
)
8563 isl_map
*other
, *product
;
8565 equal
= isl_map_has_equal_params(map
, factor
);
8569 map
= isl_map_align_params(map
, isl_map_get_space(factor
));
8570 factor
= isl_map_align_params(factor
, isl_map_get_space(map
));
8573 space
= isl_map_get_space(map
);
8574 other
= isl_map_universe(control
->other_factor(space
));
8575 product
= control
->product(factor
, other
);
8577 space
= isl_map_peek_space(map
);
8578 product
= isl_map_copy_tuple_id(product
, control
->preserve_type
,
8579 space
, control
->preserve_type
);
8580 return map_intersect(map
, product
);
8583 isl_map_free(factor
);
8587 /* Return the domain product of "map2" and "map1".
8589 static __isl_give isl_map
*isl_map_reverse_domain_product(
8590 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
8592 return isl_map_domain_product(map2
, map1
);
8595 /* Return the range product of "map2" and "map1".
8597 static __isl_give isl_map
*isl_map_reverse_range_product(
8598 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
8600 return isl_map_range_product(map2
, map1
);
8603 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
8604 * in the space A -> C, return the intersection.
8606 __isl_give isl_map
*isl_map_intersect_domain_factor_domain(
8607 __isl_take isl_map
*map
, __isl_take isl_map
*factor
)
8609 struct isl_intersect_factor_control control
= {
8610 .preserve_type
= isl_dim_in
,
8611 .other_factor
= isl_space_domain_factor_range
,
8612 .product
= isl_map_domain_product
,
8615 return isl_map_intersect_factor(map
, factor
, &control
);
8618 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
8619 * in the space B -> C, return the intersection.
8621 __isl_give isl_map
*isl_map_intersect_domain_factor_range(
8622 __isl_take isl_map
*map
, __isl_take isl_map
*factor
)
8624 struct isl_intersect_factor_control control
= {
8625 .preserve_type
= isl_dim_in
,
8626 .other_factor
= isl_space_domain_factor_domain
,
8627 .product
= isl_map_reverse_domain_product
,
8630 return isl_map_intersect_factor(map
, factor
, &control
);
8633 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
8634 * in the space A -> B, return the intersection.
8636 __isl_give isl_map
*isl_map_intersect_range_factor_domain(
8637 __isl_take isl_map
*map
, __isl_take isl_map
*factor
)
8639 struct isl_intersect_factor_control control
= {
8640 .preserve_type
= isl_dim_out
,
8641 .other_factor
= isl_space_range_factor_range
,
8642 .product
= isl_map_range_product
,
8645 return isl_map_intersect_factor(map
, factor
, &control
);
8648 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
8649 * in the space A -> C, return the intersection.
8651 __isl_give isl_map
*isl_map_intersect_range_factor_range(
8652 __isl_take isl_map
*map
, __isl_take isl_map
*factor
)
8654 struct isl_intersect_factor_control control
= {
8655 .preserve_type
= isl_dim_out
,
8656 .other_factor
= isl_space_range_factor_domain
,
8657 .product
= isl_map_reverse_range_product
,
8660 return isl_map_intersect_factor(map
, factor
, &control
);
8663 /* Given a set "set" in a space [A -> B] and a set "domain"
8664 * in the space A, return the intersection.
8666 * The set "domain" is first extended to a set living in the space
8667 * [A -> B] and then a regular intersection is computed.
8669 __isl_give isl_set
*isl_set_intersect_factor_domain(__isl_take isl_set
*set
,
8670 __isl_take isl_set
*domain
)
8672 struct isl_intersect_factor_control control
= {
8673 .preserve_type
= isl_dim_set
,
8674 .other_factor
= isl_space_factor_range
,
8675 .product
= isl_map_range_product
,
8678 return set_from_map(isl_map_intersect_factor(set_to_map(set
),
8679 set_to_map(domain
), &control
));
8682 /* Given a set "set" in a space [A -> B] and a set "range"
8683 * in the space B, return the intersection.
8685 * The set "range" is first extended to a set living in the space
8686 * [A -> B] and then a regular intersection is computed.
8688 __isl_give isl_set
*isl_set_intersect_factor_range(__isl_take isl_set
*set
,
8689 __isl_take isl_set
*range
)
8691 struct isl_intersect_factor_control control
= {
8692 .preserve_type
= isl_dim_set
,
8693 .other_factor
= isl_space_factor_domain
,
8694 .product
= isl_map_reverse_range_product
,
8697 return set_from_map(isl_map_intersect_factor(set_to_map(set
),
8698 set_to_map(range
), &control
));
8704 #include "isl_copy_tuple_id_templ.c"
8706 /* Given a map "map" in a space [A -> B] -> C and a set "domain"
8707 * in the space A, return the intersection.
8709 * The set "domain" is extended to a set living in the space [A -> B] and
8710 * the domain of "map" is intersected with this set.
8712 * If "map" has an identifier on the domain tuple,
8713 * then this identifier needs to be set on this product
8714 * before the intersection is computed.
8716 __isl_give isl_map
*isl_map_intersect_domain_wrapped_domain(
8717 __isl_take isl_map
*map
, __isl_take isl_set
*domain
)
8722 isl_map_align_params_set(&map
, &domain
);
8723 space
= isl_map_get_space(map
);
8724 space
= isl_space_domain_wrapped_range(space
);
8725 factor
= isl_set_universe(space
);
8726 domain
= isl_set_product(domain
, factor
);
8727 space
= isl_map_peek_space(map
);
8728 domain
= isl_set_copy_tuple_id(domain
, isl_dim_set
, space
, isl_dim_in
);
8729 return isl_map_intersect_domain(map
, domain
);
8732 /* Given a map "map" in a space A -> [B -> C] and a set "domain"
8733 * in the space B, return the intersection.
8735 * The set "domain" is extended to a set living in the space [B -> C] and
8736 * the range of "map" is intersected with this set.
8738 * If "map" has an identifier on the range tuple,
8739 * then this identifier needs to be set on this product
8740 * before the intersection is computed.
8742 __isl_give isl_map
*isl_map_intersect_range_wrapped_domain(
8743 __isl_take isl_map
*map
, __isl_take isl_set
*domain
)
8748 isl_map_align_params_set(&map
, &domain
);
8749 space
= isl_map_get_space(map
);
8750 space
= isl_space_range_wrapped_range(space
);
8751 factor
= isl_set_universe(space
);
8752 domain
= isl_set_product(domain
, factor
);
8753 space
= isl_map_peek_space(map
);
8754 domain
= isl_set_copy_tuple_id(domain
, isl_dim_set
, space
, isl_dim_out
);
8755 return isl_map_intersect_range(map
, domain
);
8758 __isl_give isl_map
*isl_map_apply_domain(__isl_take isl_map
*map1
,
8759 __isl_take isl_map
*map2
)
8761 if (isl_map_align_params_bin(&map1
, &map2
) < 0)
8763 map1
= isl_map_reverse(map1
);
8764 map1
= isl_map_apply_range(map1
, map2
);
8765 return isl_map_reverse(map1
);
8772 __isl_give isl_map
*isl_map_apply_range(__isl_take isl_map
*map1
,
8773 __isl_take isl_map
*map2
)
8776 struct isl_map
*result
;
8779 if (isl_map_align_params_bin(&map1
, &map2
) < 0)
8782 space
= isl_space_join(isl_space_copy(map1
->dim
),
8783 isl_space_copy(map2
->dim
));
8785 result
= isl_map_alloc_space(space
, map1
->n
* map2
->n
, 0);
8788 for (i
= 0; i
< map1
->n
; ++i
)
8789 for (j
= 0; j
< map2
->n
; ++j
) {
8790 result
= isl_map_add_basic_map(result
,
8791 isl_basic_map_apply_range(
8792 isl_basic_map_copy(map1
->p
[i
]),
8793 isl_basic_map_copy(map2
->p
[j
])));
8799 if (result
&& result
->n
<= 1)
8800 ISL_F_SET(result
, ISL_MAP_DISJOINT
);
8808 /* Is "bmap" a transformation, i.e.,
8809 * does it relate elements from the same space.
8811 isl_bool
isl_basic_map_is_transformation(__isl_keep isl_basic_map
*bmap
)
8815 space
= isl_basic_map_peek_space(bmap
);
8816 return isl_space_tuple_is_equal(space
, isl_dim_in
, space
, isl_dim_out
);
8819 /* Check that "bmap" is a transformation, i.e.,
8820 * that it relates elements from the same space.
8822 static isl_stat
isl_basic_map_check_transformation(
8823 __isl_keep isl_basic_map
*bmap
)
8827 equal
= isl_basic_map_is_transformation(bmap
);
8829 return isl_stat_error
;
8831 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
8832 "domain and range don't match", return isl_stat_error
);
8837 * returns range - domain
8839 __isl_give isl_basic_set
*isl_basic_map_deltas(__isl_take isl_basic_map
*bmap
)
8841 isl_space
*target_space
;
8842 struct isl_basic_set
*bset
;
8848 if (isl_basic_map_check_transformation(bmap
) < 0)
8849 return isl_basic_map_free(bmap
);
8850 dim
= isl_basic_map_dim(bmap
, isl_dim_in
);
8851 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
8852 if (dim
< 0 || nparam
< 0)
8854 target_space
= isl_space_domain(isl_basic_map_get_space(bmap
));
8855 bmap
= isl_basic_map_from_range(isl_basic_map_wrap(bmap
));
8856 bmap
= isl_basic_map_add_dims(bmap
, isl_dim_in
, dim
);
8857 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
8859 bmap
= isl_basic_map_free(bmap
);
8860 bmap
= isl_basic_map_extend_constraints(bmap
, dim
, 0);
8861 for (i
= 0; i
< dim
; ++i
) {
8862 int j
= isl_basic_map_alloc_equality(bmap
);
8864 bmap
= isl_basic_map_free(bmap
);
8867 isl_seq_clr(bmap
->eq
[j
], 1 + total
);
8868 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
8869 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], 1);
8870 isl_int_set_si(bmap
->eq
[j
][1+nparam
+2*dim
+i
], -1);
8872 bset
= isl_basic_map_domain(bmap
);
8873 bset
= isl_basic_set_reset_space(bset
, target_space
);
8876 isl_basic_map_free(bmap
);
8880 /* Is the tuple of type "type1" of "map" the same as
8881 * the tuple of type "type2" of "space"?
8883 isl_bool
isl_map_space_tuple_is_equal(__isl_keep isl_map
*map
,
8884 enum isl_dim_type type1
, __isl_keep isl_space
*space
,
8885 enum isl_dim_type type2
)
8887 isl_space
*map_space
;
8889 map_space
= isl_map_peek_space(map
);
8890 return isl_space_tuple_is_equal(map_space
, type1
, space
, type2
);
8893 /* Is the tuple of type "type1" of "map1" the same as
8894 * the tuple of type "type2" of "map2"?
8896 isl_bool
isl_map_tuple_is_equal(__isl_keep isl_map
*map1
,
8897 enum isl_dim_type type1
, __isl_keep isl_map
*map2
,
8898 enum isl_dim_type type2
)
8900 isl_space
*space1
, *space2
;
8902 space1
= isl_map_peek_space(map1
);
8903 space2
= isl_map_peek_space(map2
);
8904 return isl_space_tuple_is_equal(space1
, type1
, space2
, type2
);
8907 /* Is the space of "obj" equal to "space", ignoring parameters?
8909 isl_bool
isl_map_has_space_tuples(__isl_keep isl_map
*map
,
8910 __isl_keep isl_space
*space
)
8912 isl_space
*map_space
;
8914 map_space
= isl_map_peek_space(map
);
8915 return isl_space_has_equal_tuples(map_space
, space
);
8918 /* Check that "map" is a transformation, i.e.,
8919 * that it relates elements from the same space.
8921 isl_stat
isl_map_check_transformation(__isl_keep isl_map
*map
)
8925 equal
= isl_map_tuple_is_equal(map
, isl_dim_in
, map
, isl_dim_out
);
8927 return isl_stat_error
;
8929 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
8930 "domain and range don't match", return isl_stat_error
);
8935 * returns range - domain
8937 __isl_give isl_set
*isl_map_deltas(__isl_take isl_map
*map
)
8941 struct isl_set
*result
;
8943 if (isl_map_check_transformation(map
) < 0)
8945 space
= isl_map_get_space(map
);
8946 space
= isl_space_domain(space
);
8947 result
= isl_set_alloc_space(space
, map
->n
, 0);
8950 for (i
= 0; i
< map
->n
; ++i
)
8951 result
= isl_set_add_basic_set(result
,
8952 isl_basic_map_deltas(isl_basic_map_copy(map
->p
[i
])));
8961 * returns [domain -> range] -> range - domain
8963 __isl_give isl_basic_map
*isl_basic_map_deltas_map(
8964 __isl_take isl_basic_map
*bmap
)
8968 isl_basic_map
*domain
;
8972 if (isl_basic_map_check_transformation(bmap
) < 0)
8973 return isl_basic_map_free(bmap
);
8975 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
8976 n
= isl_basic_map_dim(bmap
, isl_dim_in
);
8977 if (nparam
< 0 || n
< 0)
8978 return isl_basic_map_free(bmap
);
8980 space
= isl_basic_map_get_space(bmap
);
8981 space
= isl_space_from_range(isl_space_domain(space
));
8982 domain
= isl_basic_map_universe(space
);
8984 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
8985 bmap
= isl_basic_map_apply_range(bmap
, domain
);
8986 bmap
= isl_basic_map_extend_constraints(bmap
, n
, 0);
8988 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
8990 return isl_basic_map_free(bmap
);
8992 for (i
= 0; i
< n
; ++i
) {
8993 k
= isl_basic_map_alloc_equality(bmap
);
8996 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
8997 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], 1);
8998 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ i
], -1);
8999 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ n
+ i
], 1);
9002 bmap
= isl_basic_map_gauss(bmap
, NULL
);
9003 return isl_basic_map_finalize(bmap
);
9005 isl_basic_map_free(bmap
);
9010 * returns [domain -> range] -> range - domain
9012 __isl_give isl_map
*isl_map_deltas_map(__isl_take isl_map
*map
)
9014 if (isl_map_check_transformation(map
) < 0)
9015 return isl_map_free(map
);
9017 return isl_map_transform(map
, &isl_space_range_map
,
9018 &isl_basic_map_deltas_map
);
9021 /* Return pairs of elements { x -> y } such that y - x is in "deltas".
9023 __isl_give isl_map
*isl_set_translation(__isl_take isl_set
*deltas
)
9028 space
= isl_space_map_from_set(isl_set_get_space(deltas
));
9029 map
= isl_map_deltas_map(isl_map_universe(space
));
9030 map
= isl_map_intersect_range(map
, deltas
);
9032 return isl_set_unwrap(isl_map_domain(map
));
9035 __isl_give isl_basic_map
*isl_basic_map_identity(__isl_take isl_space
*space
)
9037 isl_size n_in
, n_out
;
9039 n_in
= isl_space_dim(space
, isl_dim_in
);
9040 n_out
= isl_space_dim(space
, isl_dim_out
);
9041 if (n_in
< 0 || n_out
< 0)
9044 isl_die(space
->ctx
, isl_error_invalid
,
9045 "number of input and output dimensions needs to be "
9046 "the same", goto error
);
9047 return isl_basic_map_equal(space
, n_in
);
9049 isl_space_free(space
);
9053 __isl_give isl_map
*isl_map_identity(__isl_take isl_space
*space
)
9055 return isl_map_from_basic_map(isl_basic_map_identity(space
));
9058 __isl_give isl_map
*isl_set_identity(__isl_take isl_set
*set
)
9060 isl_space
*space
= isl_set_get_space(set
);
9062 id
= isl_map_identity(isl_space_map_from_set(space
));
9063 return isl_map_intersect_range(id
, set
);
9066 /* Construct a basic set with all set dimensions having only non-negative
9069 __isl_give isl_basic_set
*isl_basic_set_positive_orthant(
9070 __isl_take isl_space
*space
)
9076 struct isl_basic_set
*bset
;
9078 nparam
= isl_space_dim(space
, isl_dim_param
);
9079 dim
= isl_space_dim(space
, isl_dim_set
);
9080 total
= isl_space_dim(space
, isl_dim_all
);
9081 if (nparam
< 0 || dim
< 0 || total
< 0)
9082 space
= isl_space_free(space
);
9083 bset
= isl_basic_set_alloc_space(space
, 0, 0, dim
);
9086 for (i
= 0; i
< dim
; ++i
) {
9087 int k
= isl_basic_set_alloc_inequality(bset
);
9090 isl_seq_clr(bset
->ineq
[k
], 1 + total
);
9091 isl_int_set_si(bset
->ineq
[k
][1 + nparam
+ i
], 1);
9095 isl_basic_set_free(bset
);
9099 /* Construct the half-space x_pos >= 0.
9101 static __isl_give isl_basic_set
*nonneg_halfspace(__isl_take isl_space
*space
,
9106 isl_basic_set
*nonneg
;
9108 total
= isl_space_dim(space
, isl_dim_all
);
9110 space
= isl_space_free(space
);
9111 nonneg
= isl_basic_set_alloc_space(space
, 0, 0, 1);
9112 k
= isl_basic_set_alloc_inequality(nonneg
);
9115 isl_seq_clr(nonneg
->ineq
[k
], 1 + total
);
9116 isl_int_set_si(nonneg
->ineq
[k
][pos
], 1);
9118 return isl_basic_set_finalize(nonneg
);
9120 isl_basic_set_free(nonneg
);
9124 /* Construct the half-space x_pos <= -1.
9126 static __isl_give isl_basic_set
*neg_halfspace(__isl_take isl_space
*space
,
9133 total
= isl_space_dim(space
, isl_dim_all
);
9135 space
= isl_space_free(space
);
9136 neg
= isl_basic_set_alloc_space(space
, 0, 0, 1);
9137 k
= isl_basic_set_alloc_inequality(neg
);
9140 isl_seq_clr(neg
->ineq
[k
], 1 + total
);
9141 isl_int_set_si(neg
->ineq
[k
][0], -1);
9142 isl_int_set_si(neg
->ineq
[k
][pos
], -1);
9144 return isl_basic_set_finalize(neg
);
9146 isl_basic_set_free(neg
);
9150 __isl_give isl_set
*isl_set_split_dims(__isl_take isl_set
*set
,
9151 enum isl_dim_type type
, unsigned first
, unsigned n
)
9155 isl_basic_set
*nonneg
;
9161 if (isl_set_check_range(set
, type
, first
, n
) < 0)
9162 return isl_set_free(set
);
9164 offset
= pos(set
->dim
, type
);
9165 for (i
= 0; i
< n
; ++i
) {
9166 nonneg
= nonneg_halfspace(isl_set_get_space(set
),
9167 offset
+ first
+ i
);
9168 neg
= neg_halfspace(isl_set_get_space(set
), offset
+ first
+ i
);
9170 set
= isl_set_intersect(set
, isl_basic_set_union(nonneg
, neg
));
9176 static isl_stat
foreach_orthant(__isl_take isl_set
*set
, int *signs
, int first
,
9178 isl_stat (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
9184 return isl_stat_error
;
9185 if (isl_set_plain_is_empty(set
)) {
9190 return fn(set
, signs
, user
);
9193 half
= isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set
),
9195 half
= isl_set_intersect(half
, isl_set_copy(set
));
9196 if (foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
) < 0)
9200 half
= isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set
),
9202 half
= isl_set_intersect(half
, set
);
9203 return foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
);
9206 return isl_stat_error
;
9209 /* Call "fn" on the intersections of "set" with each of the orthants
9210 * (except for obviously empty intersections). The orthant is identified
9211 * by the signs array, with each entry having value 1 or -1 according
9212 * to the sign of the corresponding variable.
9214 isl_stat
isl_set_foreach_orthant(__isl_keep isl_set
*set
,
9215 isl_stat (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
9224 return isl_stat_error
;
9225 if (isl_set_plain_is_empty(set
))
9228 nparam
= isl_set_dim(set
, isl_dim_param
);
9229 nvar
= isl_set_dim(set
, isl_dim_set
);
9230 if (nparam
< 0 || nvar
< 0)
9231 return isl_stat_error
;
9233 signs
= isl_alloc_array(set
->ctx
, int, nparam
+ nvar
);
9235 r
= foreach_orthant(isl_set_copy(set
), signs
, 0, nparam
+ nvar
,
9243 isl_bool
isl_set_is_equal(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
9245 return isl_map_is_equal(set_to_map(set1
), set_to_map(set2
));
9248 isl_bool
isl_basic_map_is_subset(__isl_keep isl_basic_map
*bmap1
,
9249 __isl_keep isl_basic_map
*bmap2
)
9252 struct isl_map
*map1
;
9253 struct isl_map
*map2
;
9255 if (!bmap1
|| !bmap2
)
9256 return isl_bool_error
;
9258 map1
= isl_map_from_basic_map(isl_basic_map_copy(bmap1
));
9259 map2
= isl_map_from_basic_map(isl_basic_map_copy(bmap2
));
9261 is_subset
= isl_map_is_subset(map1
, map2
);
9269 isl_bool
isl_basic_set_is_subset(__isl_keep isl_basic_set
*bset1
,
9270 __isl_keep isl_basic_set
*bset2
)
9272 return isl_basic_map_is_subset(bset1
, bset2
);
9275 isl_bool
isl_basic_map_is_equal(__isl_keep isl_basic_map
*bmap1
,
9276 __isl_keep isl_basic_map
*bmap2
)
9280 if (!bmap1
|| !bmap2
)
9281 return isl_bool_error
;
9282 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
9283 if (is_subset
!= isl_bool_true
)
9285 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
9289 isl_bool
isl_basic_set_is_equal(__isl_keep isl_basic_set
*bset1
,
9290 __isl_keep isl_basic_set
*bset2
)
9292 return isl_basic_map_is_equal(
9293 bset_to_bmap(bset1
), bset_to_bmap(bset2
));
9296 isl_bool
isl_map_is_empty(__isl_keep isl_map
*map
)
9302 return isl_bool_error
;
9303 for (i
= 0; i
< map
->n
; ++i
) {
9304 is_empty
= isl_basic_map_is_empty(map
->p
[i
]);
9306 return isl_bool_error
;
9308 return isl_bool_false
;
9310 return isl_bool_true
;
9313 isl_bool
isl_map_plain_is_empty(__isl_keep isl_map
*map
)
9315 return map
? map
->n
== 0 : isl_bool_error
;
9318 isl_bool
isl_set_plain_is_empty(__isl_keep isl_set
*set
)
9320 return set
? set
->n
== 0 : isl_bool_error
;
9323 isl_bool
isl_set_is_empty(__isl_keep isl_set
*set
)
9325 return isl_map_is_empty(set_to_map(set
));
9329 #define TYPE isl_basic_map
9332 #include "isl_type_has_equal_space_bin_templ.c"
9333 #include "isl_type_check_equal_space_templ.c"
9335 /* Check that "bset1" and "bset2" live in the same space,
9336 * reporting an error if they do not.
9338 isl_stat
isl_basic_set_check_equal_space(__isl_keep isl_basic_set
*bset1
,
9339 __isl_keep isl_basic_set
*bset2
)
9341 return isl_basic_map_check_equal_space(bset_to_bmap(bset1
),
9342 bset_to_bmap(bset1
));
9346 #define TYPE isl_map
9348 #include "isl_type_has_equal_space_bin_templ.c"
9349 #include "isl_type_check_equal_space_templ.c"
9350 #include "isl_type_has_space_templ.c"
9352 isl_bool
isl_set_has_equal_space(__isl_keep isl_set
*set1
,
9353 __isl_keep isl_set
*set2
)
9355 return isl_map_has_equal_space(set_to_map(set1
), set_to_map(set2
));
9359 #define TYPE1 isl_map
9361 #define TYPE2 isl_basic_map
9363 #define TYPE_PAIR isl_map_basic_map
9366 #include "isl_type_has_equal_space_templ.c"
9367 #include "isl_type_check_equal_space_templ.c"
9369 /* Check that "set" and "bset" live in the same space,
9370 * reporting an error if they do not.
9372 isl_stat
isl_set_basic_set_check_equal_space(__isl_keep isl_set
*set
,
9373 __isl_keep isl_basic_set
*bset
)
9375 return isl_map_basic_map_check_equal_space(set_to_map(set
),
9376 bset_to_bmap(bset
));
9379 static isl_bool
map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
9384 return isl_bool_error
;
9385 is_subset
= isl_map_is_subset(map1
, map2
);
9386 if (is_subset
!= isl_bool_true
)
9388 is_subset
= isl_map_is_subset(map2
, map1
);
9392 /* Is "map1" equal to "map2"?
9394 * First check if they are obviously equal.
9395 * If not, then perform a more detailed analysis.
9397 isl_bool
isl_map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
9401 equal
= isl_map_plain_is_equal(map1
, map2
);
9402 if (equal
< 0 || equal
)
9404 return isl_map_align_params_map_map_and_test(map1
, map2
, &map_is_equal
);
9407 isl_bool
isl_basic_map_is_strict_subset(__isl_keep isl_basic_map
*bmap1
,
9408 __isl_keep isl_basic_map
*bmap2
)
9412 if (!bmap1
|| !bmap2
)
9413 return isl_bool_error
;
9414 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
9415 if (is_subset
!= isl_bool_true
)
9417 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
9418 return isl_bool_not(is_subset
);
9421 isl_bool
isl_map_is_strict_subset(__isl_keep isl_map
*map1
,
9422 __isl_keep isl_map
*map2
)
9427 return isl_bool_error
;
9428 is_subset
= isl_map_is_subset(map1
, map2
);
9429 if (is_subset
!= isl_bool_true
)
9431 is_subset
= isl_map_is_subset(map2
, map1
);
9432 return isl_bool_not(is_subset
);
9435 isl_bool
isl_set_is_strict_subset(__isl_keep isl_set
*set1
,
9436 __isl_keep isl_set
*set2
)
9438 return isl_map_is_strict_subset(set_to_map(set1
), set_to_map(set2
));
9441 /* Is "bmap" obviously equal to the universe with the same space?
9443 * That is, does it not have any constraints?
9445 isl_bool
isl_basic_map_plain_is_universe(__isl_keep isl_basic_map
*bmap
)
9448 return isl_bool_error
;
9449 return bmap
->n_eq
== 0 && bmap
->n_ineq
== 0;
9452 /* Is "bset" obviously equal to the universe with the same space?
9454 isl_bool
isl_basic_set_plain_is_universe(__isl_keep isl_basic_set
*bset
)
9456 return isl_basic_map_plain_is_universe(bset
);
9459 /* If "c" does not involve any existentially quantified variables,
9460 * then set *univ to false and abort
9462 static isl_stat
involves_divs(__isl_take isl_constraint
*c
, void *user
)
9464 isl_bool
*univ
= user
;
9467 n
= isl_constraint_dim(c
, isl_dim_div
);
9469 c
= isl_constraint_free(c
);
9470 *univ
= isl_constraint_involves_dims(c
, isl_dim_div
, 0, n
);
9471 isl_constraint_free(c
);
9472 if (*univ
< 0 || !*univ
)
9473 return isl_stat_error
;
9477 /* Is "bmap" equal to the universe with the same space?
9479 * First check if it is obviously equal to the universe.
9480 * If not and if there are any constraints not involving
9481 * existentially quantified variables, then it is certainly
9482 * not equal to the universe.
9483 * Otherwise, check if the universe is a subset of "bmap".
9485 isl_bool
isl_basic_map_is_universe(__isl_keep isl_basic_map
*bmap
)
9489 isl_basic_map
*test
;
9491 univ
= isl_basic_map_plain_is_universe(bmap
);
9492 if (univ
< 0 || univ
)
9494 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
9496 return isl_bool_error
;
9498 return isl_bool_false
;
9499 univ
= isl_bool_true
;
9500 if (isl_basic_map_foreach_constraint(bmap
, &involves_divs
, &univ
) < 0 &&
9502 return isl_bool_error
;
9503 if (univ
< 0 || !univ
)
9505 test
= isl_basic_map_universe(isl_basic_map_get_space(bmap
));
9506 univ
= isl_basic_map_is_subset(test
, bmap
);
9507 isl_basic_map_free(test
);
9511 /* Is "bset" equal to the universe with the same space?
9513 isl_bool
isl_basic_set_is_universe(__isl_keep isl_basic_set
*bset
)
9515 return isl_basic_map_is_universe(bset
);
9518 isl_bool
isl_map_plain_is_universe(__isl_keep isl_map
*map
)
9523 return isl_bool_error
;
9525 for (i
= 0; i
< map
->n
; ++i
) {
9526 isl_bool r
= isl_basic_map_plain_is_universe(map
->p
[i
]);
9531 return isl_bool_false
;
9534 isl_bool
isl_set_plain_is_universe(__isl_keep isl_set
*set
)
9536 return isl_map_plain_is_universe(set_to_map(set
));
9539 isl_bool
isl_basic_map_is_empty(__isl_keep isl_basic_map
*bmap
)
9541 struct isl_basic_set
*bset
= NULL
;
9542 struct isl_vec
*sample
= NULL
;
9543 isl_bool empty
, non_empty
;
9546 return isl_bool_error
;
9548 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
9549 return isl_bool_true
;
9551 if (isl_basic_map_plain_is_universe(bmap
))
9552 return isl_bool_false
;
9554 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
9555 struct isl_basic_map
*copy
= isl_basic_map_copy(bmap
);
9556 copy
= isl_basic_map_remove_redundancies(copy
);
9557 empty
= isl_basic_map_plain_is_empty(copy
);
9558 isl_basic_map_free(copy
);
9562 non_empty
= isl_basic_map_plain_is_non_empty(bmap
);
9564 return isl_bool_error
;
9566 return isl_bool_false
;
9567 isl_vec_free(bmap
->sample
);
9568 bmap
->sample
= NULL
;
9569 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
9571 return isl_bool_error
;
9572 sample
= isl_basic_set_sample_vec(bset
);
9574 return isl_bool_error
;
9575 empty
= sample
->size
== 0;
9576 isl_vec_free(bmap
->sample
);
9577 bmap
->sample
= sample
;
9579 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
9584 isl_bool
isl_basic_map_plain_is_empty(__isl_keep isl_basic_map
*bmap
)
9587 return isl_bool_error
;
9588 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
);
9591 isl_bool
isl_basic_set_plain_is_empty(__isl_keep isl_basic_set
*bset
)
9594 return isl_bool_error
;
9595 return ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
);
9598 /* Is "bmap" known to be non-empty?
9600 * That is, is the cached sample still valid?
9602 isl_bool
isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map
*bmap
)
9607 return isl_bool_error
;
9609 return isl_bool_false
;
9610 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
9612 return isl_bool_error
;
9613 if (bmap
->sample
->size
!= 1 + total
)
9614 return isl_bool_false
;
9615 return isl_basic_map_contains(bmap
, bmap
->sample
);
9618 isl_bool
isl_basic_set_is_empty(__isl_keep isl_basic_set
*bset
)
9620 return isl_basic_map_is_empty(bset_to_bmap(bset
));
9623 __isl_give isl_map
*isl_basic_map_union(__isl_take isl_basic_map
*bmap1
,
9624 __isl_take isl_basic_map
*bmap2
)
9626 struct isl_map
*map
;
9628 if (isl_basic_map_check_equal_space(bmap1
, bmap2
) < 0)
9631 map
= isl_map_alloc_space(isl_space_copy(bmap1
->dim
), 2, 0);
9634 map
= isl_map_add_basic_map(map
, bmap1
);
9635 map
= isl_map_add_basic_map(map
, bmap2
);
9638 isl_basic_map_free(bmap1
);
9639 isl_basic_map_free(bmap2
);
9643 __isl_give isl_set
*isl_basic_set_union(__isl_take isl_basic_set
*bset1
,
9644 __isl_take isl_basic_set
*bset2
)
9646 return set_from_map(isl_basic_map_union(bset_to_bmap(bset1
),
9647 bset_to_bmap(bset2
)));
9650 /* Order divs such that any div only depends on previous divs */
9651 __isl_give isl_basic_map
*isl_basic_map_order_divs(
9652 __isl_take isl_basic_map
*bmap
)
9657 off
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
9659 return isl_basic_map_free(bmap
);
9661 for (i
= 0; i
< bmap
->n_div
; ++i
) {
9663 if (isl_int_is_zero(bmap
->div
[i
][0]))
9665 pos
= isl_seq_first_non_zero(bmap
->div
[i
]+1+1+off
+i
,
9670 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_internal
,
9671 "integer division depends on itself",
9672 return isl_basic_map_free(bmap
));
9673 bmap
= isl_basic_map_swap_div(bmap
, i
, i
+ pos
);
9681 __isl_give isl_map
*isl_map_order_divs(__isl_take isl_map
*map
)
9688 for (i
= 0; i
< map
->n
; ++i
) {
9689 map
->p
[i
] = isl_basic_map_order_divs(map
->p
[i
]);
9700 /* Sort the local variables of "bset".
9702 __isl_give isl_basic_set
*isl_basic_set_sort_divs(
9703 __isl_take isl_basic_set
*bset
)
9705 return bset_from_bmap(isl_basic_map_sort_divs(bset_to_bmap(bset
)));
9708 /* Apply the expansion computed by isl_merge_divs.
9709 * The expansion itself is given by "exp" while the resulting
9710 * list of divs is given by "div".
9712 * Move the integer divisions of "bmap" into the right position
9713 * according to "exp" and then introduce the additional integer
9714 * divisions, adding div constraints.
9715 * The moving should be done first to avoid moving coefficients
9716 * in the definitions of the extra integer divisions.
9718 __isl_give isl_basic_map
*isl_basic_map_expand_divs(
9719 __isl_take isl_basic_map
*bmap
, __isl_take isl_mat
*div
, int *exp
)
9724 bmap
= isl_basic_map_cow(bmap
);
9728 if (div
->n_row
< bmap
->n_div
)
9729 isl_die(isl_mat_get_ctx(div
), isl_error_invalid
,
9730 "not an expansion", goto error
);
9732 n_div
= bmap
->n_div
;
9733 bmap
= isl_basic_map_extend(bmap
, div
->n_row
- n_div
, 0,
9734 2 * (div
->n_row
- n_div
));
9736 for (i
= n_div
; i
< div
->n_row
; ++i
)
9737 if (isl_basic_map_alloc_div(bmap
) < 0)
9740 for (j
= n_div
- 1; j
>= 0; --j
) {
9743 bmap
= isl_basic_map_swap_div(bmap
, j
, exp
[j
]);
9748 for (i
= 0; i
< div
->n_row
; ++i
) {
9749 if (j
< n_div
&& exp
[j
] == i
) {
9752 isl_seq_cpy(bmap
->div
[i
], div
->row
[i
], div
->n_col
);
9753 if (isl_basic_map_div_is_marked_unknown(bmap
, i
))
9755 bmap
= isl_basic_map_add_div_constraints(bmap
, i
);
9764 isl_basic_map_free(bmap
);
9769 /* Apply the expansion computed by isl_merge_divs.
9770 * The expansion itself is given by "exp" while the resulting
9771 * list of divs is given by "div".
9773 __isl_give isl_basic_set
*isl_basic_set_expand_divs(
9774 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*div
, int *exp
)
9776 return isl_basic_map_expand_divs(bset
, div
, exp
);
9779 /* Look for a div in dst that corresponds to the div "div" in src.
9780 * The divs before "div" in src and dst are assumed to be the same.
9782 * Return the position of the corresponding div in dst
9783 * if there is one. Otherwise, return a position beyond the integer divisions.
9784 * Return -1 on error.
9786 static int find_div(__isl_keep isl_basic_map
*dst
,
9787 __isl_keep isl_basic_map
*src
, unsigned div
)
9793 v_div
= isl_basic_map_var_offset(src
, isl_dim_div
);
9794 n_div
= isl_basic_map_dim(dst
, isl_dim_div
);
9795 if (n_div
< 0 || v_div
< 0)
9797 isl_assert(dst
->ctx
, div
<= n_div
, return -1);
9798 for (i
= div
; i
< n_div
; ++i
)
9799 if (isl_seq_eq(dst
->div
[i
], src
->div
[div
], 1+1+v_div
+div
) &&
9800 isl_seq_first_non_zero(dst
->div
[i
] + 1 + 1 + v_div
+ div
,
9806 /* Align the divs of "dst" to those of "src", adding divs from "src"
9807 * if needed. That is, make sure that the first src->n_div divs
9808 * of the result are equal to those of src.
9809 * The integer division of "src" are assumed to be ordered.
9811 * The integer divisions are swapped into the right position
9812 * (possibly after adding them first). This may result
9813 * in the remaining integer divisions appearing in the wrong order,
9814 * i.e., with some integer division appearing before
9815 * some other integer division on which it depends.
9816 * The integer divisions therefore need to be ordered.
9817 * This will not affect the integer divisions aligned to those of "src",
9818 * since "src" is assumed to have ordered integer divisions.
9820 * The result is not finalized as by design it will have redundant
9821 * divs if any divs from "src" were copied.
9823 __isl_give isl_basic_map
*isl_basic_map_align_divs(
9824 __isl_take isl_basic_map
*dst
, __isl_keep isl_basic_map
*src
)
9833 return isl_basic_map_free(dst
);
9835 if (src
->n_div
== 0)
9838 known
= isl_basic_map_divs_known(src
);
9840 return isl_basic_map_free(dst
);
9842 isl_die(isl_basic_map_get_ctx(src
), isl_error_invalid
,
9843 "some src divs are unknown",
9844 return isl_basic_map_free(dst
));
9846 v_div
= isl_basic_map_var_offset(src
, isl_dim_div
);
9848 return isl_basic_map_free(dst
);
9851 dst_n_div
= isl_basic_map_dim(dst
, isl_dim_div
);
9853 dst
= isl_basic_map_free(dst
);
9854 for (i
= 0; i
< src
->n_div
; ++i
) {
9855 int j
= find_div(dst
, src
, i
);
9857 dst
= isl_basic_map_free(dst
);
9858 if (j
== dst_n_div
) {
9860 int extra
= src
->n_div
- i
;
9861 dst
= isl_basic_map_cow(dst
);
9863 return isl_basic_map_free(dst
);
9864 dst
= isl_basic_map_extend(dst
,
9865 extra
, 0, 2 * extra
);
9868 j
= isl_basic_map_alloc_div(dst
);
9870 return isl_basic_map_free(dst
);
9871 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+v_div
+i
);
9872 isl_seq_clr(dst
->div
[j
]+1+1+v_div
+i
, dst
->n_div
- i
);
9874 dst
= isl_basic_map_add_div_constraints(dst
, j
);
9876 return isl_basic_map_free(dst
);
9879 dst
= isl_basic_map_swap_div(dst
, i
, j
);
9881 return isl_basic_map_free(dst
);
9883 return isl_basic_map_order_divs(dst
);
9886 __isl_give isl_map
*isl_map_align_divs_internal(__isl_take isl_map
*map
)
9894 map
= isl_map_compute_divs(map
);
9895 map
= isl_map_order_divs(map
);
9896 map
= isl_map_cow(map
);
9900 for (i
= 1; i
< map
->n
; ++i
)
9901 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], map
->p
[i
]);
9902 for (i
= 1; i
< map
->n
; ++i
) {
9903 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], map
->p
[0]);
9905 return isl_map_free(map
);
9908 map
= isl_map_unmark_normalized(map
);
9912 __isl_give isl_map
*isl_map_align_divs(__isl_take isl_map
*map
)
9914 return isl_map_align_divs_internal(map
);
9917 __isl_give isl_set
*isl_set_align_divs(__isl_take isl_set
*set
)
9919 return set_from_map(isl_map_align_divs_internal(set_to_map(set
)));
9922 /* Align the divs of the basic maps in "map" to those
9923 * of the basic maps in "list", as well as to the other basic maps in "map".
9924 * The elements in "list" are assumed to have known divs.
9926 __isl_give isl_map
*isl_map_align_divs_to_basic_map_list(
9927 __isl_take isl_map
*map
, __isl_keep isl_basic_map_list
*list
)
9932 n
= isl_basic_map_list_n_basic_map(list
);
9933 map
= isl_map_compute_divs(map
);
9934 map
= isl_map_cow(map
);
9936 return isl_map_free(map
);
9940 for (i
= 0; i
< n
; ++i
) {
9941 isl_basic_map
*bmap
;
9943 bmap
= isl_basic_map_list_get_basic_map(list
, i
);
9944 bmap
= isl_basic_map_order_divs(bmap
);
9945 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], bmap
);
9946 isl_basic_map_free(bmap
);
9949 return isl_map_free(map
);
9951 return isl_map_align_divs_internal(map
);
9954 /* Align the divs of each element of "list" to those of "bmap".
9955 * Both "bmap" and the elements of "list" are assumed to have known divs.
9957 __isl_give isl_basic_map_list
*isl_basic_map_list_align_divs_to_basic_map(
9958 __isl_take isl_basic_map_list
*list
, __isl_keep isl_basic_map
*bmap
)
9963 n
= isl_basic_map_list_n_basic_map(list
);
9965 return isl_basic_map_list_free(list
);
9967 for (i
= 0; i
< n
; ++i
) {
9968 isl_basic_map
*bmap_i
;
9970 bmap_i
= isl_basic_map_list_get_basic_map(list
, i
);
9971 bmap_i
= isl_basic_map_align_divs(bmap_i
, bmap
);
9972 list
= isl_basic_map_list_set_basic_map(list
, i
, bmap_i
);
9978 __isl_give isl_set
*isl_set_apply( __isl_take isl_set
*set
,
9979 __isl_take isl_map
*map
)
9983 isl_map_align_params_set(&map
, &set
);
9984 ok
= isl_map_compatible_domain(map
, set
);
9988 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
9989 "incompatible spaces", goto error
);
9990 map
= isl_map_intersect_domain(map
, set
);
9991 set
= isl_map_range(map
);
9999 /* There is no need to cow as removing empty parts doesn't change
10000 * the meaning of the set.
10002 __isl_give isl_map
*isl_map_remove_empty_parts(__isl_take isl_map
*map
)
10009 for (i
= map
->n
- 1; i
>= 0; --i
)
10010 map
= remove_if_empty(map
, i
);
10015 __isl_give isl_set
*isl_set_remove_empty_parts(__isl_take isl_set
*set
)
10017 return set_from_map(isl_map_remove_empty_parts(set_to_map(set
)));
10020 /* Create a binary relation that maps the shared initial "pos" dimensions
10021 * of "bset1" and "bset2" to the remaining dimensions of "bset1" and "bset2".
10023 static __isl_give isl_basic_map
*join_initial(__isl_keep isl_basic_set
*bset1
,
10024 __isl_keep isl_basic_set
*bset2
, int pos
)
10026 isl_basic_map
*bmap1
;
10027 isl_basic_map
*bmap2
;
10029 bmap1
= isl_basic_map_from_range(isl_basic_set_copy(bset1
));
10030 bmap2
= isl_basic_map_from_range(isl_basic_set_copy(bset2
));
10031 bmap1
= isl_basic_map_move_dims(bmap1
, isl_dim_in
, 0,
10032 isl_dim_out
, 0, pos
);
10033 bmap2
= isl_basic_map_move_dims(bmap2
, isl_dim_in
, 0,
10034 isl_dim_out
, 0, pos
);
10035 return isl_basic_map_range_product(bmap1
, bmap2
);
10038 /* Given two basic sets bset1 and bset2, compute the maximal difference
10039 * between the values of dimension pos in bset1 and those in bset2
10040 * for any common value of the parameters and dimensions preceding pos.
10042 static enum isl_lp_result
basic_set_maximal_difference_at(
10043 __isl_keep isl_basic_set
*bset1
, __isl_keep isl_basic_set
*bset2
,
10044 int pos
, isl_int
*opt
)
10046 isl_basic_map
*bmap1
;
10047 struct isl_ctx
*ctx
;
10048 struct isl_vec
*obj
;
10052 enum isl_lp_result res
;
10054 nparam
= isl_basic_set_dim(bset1
, isl_dim_param
);
10055 dim1
= isl_basic_set_dim(bset1
, isl_dim_set
);
10056 if (nparam
< 0 || dim1
< 0 || !bset2
)
10057 return isl_lp_error
;
10059 bmap1
= join_initial(bset1
, bset2
, pos
);
10060 total
= isl_basic_map_dim(bmap1
, isl_dim_all
);
10062 return isl_lp_error
;
10065 obj
= isl_vec_alloc(ctx
, 1 + total
);
10068 isl_seq_clr(obj
->block
.data
, 1 + total
);
10069 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
], 1);
10070 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
+(dim1
-pos
)], -1);
10071 res
= isl_basic_map_solve_lp(bmap1
, 1, obj
->block
.data
, ctx
->one
,
10073 isl_basic_map_free(bmap1
);
10077 isl_basic_map_free(bmap1
);
10078 return isl_lp_error
;
10081 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
10082 * for any common value of the parameters and dimensions preceding pos
10083 * in both basic sets, the values of dimension pos in bset1 are
10084 * smaller or larger than those in bset2.
10087 * 1 if bset1 follows bset2
10088 * -1 if bset1 precedes bset2
10089 * 0 if bset1 and bset2 are incomparable
10090 * -2 if some error occurred.
10092 int isl_basic_set_compare_at(__isl_keep isl_basic_set
*bset1
,
10093 __isl_keep isl_basic_set
*bset2
, int pos
)
10096 enum isl_lp_result res
;
10101 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
10103 if (res
== isl_lp_empty
)
10105 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
10106 res
== isl_lp_unbounded
)
10108 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
10113 isl_int_clear(opt
);
10117 /* Given two basic sets bset1 and bset2, check whether
10118 * for any common value of the parameters and dimensions preceding pos
10119 * there is a value of dimension pos in bset1 that is larger
10120 * than a value of the same dimension in bset2.
10123 * 1 if there exists such a pair
10124 * 0 if there is no such pair, but there is a pair of equal values
10126 * -2 if some error occurred.
10128 int isl_basic_set_follows_at(__isl_keep isl_basic_set
*bset1
,
10129 __isl_keep isl_basic_set
*bset2
, int pos
)
10132 isl_basic_map
*bmap
;
10135 dim1
= isl_basic_set_dim(bset1
, isl_dim_set
);
10138 bmap
= join_initial(bset1
, bset2
, pos
);
10139 bmap
= isl_basic_map_order_ge(bmap
, isl_dim_out
, 0,
10140 isl_dim_out
, dim1
- pos
);
10141 empty
= isl_basic_map_is_empty(bmap
);
10145 isl_basic_map_free(bmap
);
10148 bmap
= isl_basic_map_order_gt(bmap
, isl_dim_out
, 0,
10149 isl_dim_out
, dim1
- pos
);
10150 empty
= isl_basic_map_is_empty(bmap
);
10153 isl_basic_map_free(bmap
);
10158 isl_basic_map_free(bmap
);
10162 /* Given two sets set1 and set2, check whether
10163 * for any common value of the parameters and dimensions preceding pos
10164 * there is a value of dimension pos in set1 that is larger
10165 * than a value of the same dimension in set2.
10168 * 1 if there exists such a pair
10169 * 0 if there is no such pair, but there is a pair of equal values
10171 * -2 if some error occurred.
10173 int isl_set_follows_at(__isl_keep isl_set
*set1
,
10174 __isl_keep isl_set
*set2
, int pos
)
10179 if (!set1
|| !set2
)
10182 for (i
= 0; i
< set1
->n
; ++i
)
10183 for (j
= 0; j
< set2
->n
; ++j
) {
10185 f
= isl_basic_set_follows_at(set1
->p
[i
], set2
->p
[j
], pos
);
10186 if (f
== 1 || f
== -2)
10195 static isl_bool
isl_basic_map_plain_has_fixed_var(
10196 __isl_keep isl_basic_map
*bmap
, unsigned pos
, isl_int
*val
)
10202 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
10204 return isl_bool_error
;
10205 for (i
= 0, d
= total
-1; i
< bmap
->n_eq
&& d
+1 > pos
; ++i
) {
10206 for (; d
+1 > pos
; --d
)
10207 if (!isl_int_is_zero(bmap
->eq
[i
][1+d
]))
10211 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1, d
) != -1)
10212 return isl_bool_false
;
10213 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1+d
+1, total
-d
-1) != -1)
10214 return isl_bool_false
;
10215 if (!isl_int_is_one(bmap
->eq
[i
][1+d
]))
10216 return isl_bool_false
;
10218 isl_int_neg(*val
, bmap
->eq
[i
][0]);
10219 return isl_bool_true
;
10221 return isl_bool_false
;
10224 static isl_bool
isl_map_plain_has_fixed_var(__isl_keep isl_map
*map
,
10225 unsigned pos
, isl_int
*val
)
10233 return isl_bool_error
;
10235 return isl_bool_false
;
10237 return isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, val
);
10240 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, &v
);
10241 for (i
= 1; fixed
== isl_bool_true
&& i
< map
->n
; ++i
) {
10242 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[i
], pos
, &tmp
);
10243 if (fixed
== isl_bool_true
&& isl_int_ne(tmp
, v
))
10244 fixed
= isl_bool_false
;
10247 isl_int_set(*val
, v
);
10248 isl_int_clear(tmp
);
10253 static isl_bool
isl_basic_set_plain_has_fixed_var(
10254 __isl_keep isl_basic_set
*bset
, unsigned pos
, isl_int
*val
)
10256 return isl_basic_map_plain_has_fixed_var(bset_to_bmap(bset
),
10260 isl_bool
isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map
*bmap
,
10261 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
10263 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
10264 return isl_bool_error
;
10265 return isl_basic_map_plain_has_fixed_var(bmap
,
10266 isl_basic_map_offset(bmap
, type
) - 1 + pos
, val
);
10269 /* If "bmap" obviously lies on a hyperplane where the given dimension
10270 * has a fixed value, then return that value.
10271 * Otherwise return NaN.
10273 __isl_give isl_val
*isl_basic_map_plain_get_val_if_fixed(
10274 __isl_keep isl_basic_map
*bmap
,
10275 enum isl_dim_type type
, unsigned pos
)
10283 ctx
= isl_basic_map_get_ctx(bmap
);
10284 v
= isl_val_alloc(ctx
);
10287 fixed
= isl_basic_map_plain_is_fixed(bmap
, type
, pos
, &v
->n
);
10289 return isl_val_free(v
);
10291 isl_int_set_si(v
->d
, 1);
10295 return isl_val_nan(ctx
);
10298 isl_bool
isl_map_plain_is_fixed(__isl_keep isl_map
*map
,
10299 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
10301 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
10302 return isl_bool_error
;
10303 return isl_map_plain_has_fixed_var(map
,
10304 map_offset(map
, type
) - 1 + pos
, val
);
10307 /* If "map" obviously lies on a hyperplane where the given dimension
10308 * has a fixed value, then return that value.
10309 * Otherwise return NaN.
10311 __isl_give isl_val
*isl_map_plain_get_val_if_fixed(__isl_keep isl_map
*map
,
10312 enum isl_dim_type type
, unsigned pos
)
10320 ctx
= isl_map_get_ctx(map
);
10321 v
= isl_val_alloc(ctx
);
10324 fixed
= isl_map_plain_is_fixed(map
, type
, pos
, &v
->n
);
10326 return isl_val_free(v
);
10328 isl_int_set_si(v
->d
, 1);
10332 return isl_val_nan(ctx
);
10335 /* If "set" obviously lies on a hyperplane where the given dimension
10336 * has a fixed value, then return that value.
10337 * Otherwise return NaN.
10339 __isl_give isl_val
*isl_set_plain_get_val_if_fixed(__isl_keep isl_set
*set
,
10340 enum isl_dim_type type
, unsigned pos
)
10342 return isl_map_plain_get_val_if_fixed(set
, type
, pos
);
10345 /* Return a sequence of values in the same space as "set"
10346 * that are equal to the corresponding set dimensions of "set"
10347 * for those set dimensions that obviously lie on a hyperplane
10348 * where the dimension has a fixed value.
10349 * The other elements are set to NaN.
10351 __isl_give isl_multi_val
*isl_set_get_plain_multi_val_if_fixed(
10352 __isl_keep isl_set
*set
)
10359 space
= isl_space_drop_all_params(isl_set_get_space(set
));
10360 mv
= isl_multi_val_alloc(space
);
10361 n
= isl_multi_val_size(mv
);
10363 return isl_multi_val_free(mv
);
10365 for (i
= 0; i
< n
; ++i
) {
10368 v
= isl_set_plain_get_val_if_fixed(set
, isl_dim_set
, i
);
10369 mv
= isl_multi_val_set_val(mv
, i
, v
);
10375 /* Check if dimension dim has fixed value and if so and if val is not NULL,
10376 * then return this fixed value in *val.
10378 isl_bool
isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set
*bset
,
10379 unsigned dim
, isl_int
*val
)
10383 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
10385 return isl_bool_error
;
10386 return isl_basic_set_plain_has_fixed_var(bset
, nparam
+ dim
, val
);
10389 /* Return -1 if the constraint "c1" should be sorted before "c2"
10390 * and 1 if it should be sorted after "c2".
10391 * Return 0 if the two constraints are the same (up to the constant term).
10393 * In particular, if a constraint involves later variables than another
10394 * then it is sorted after this other constraint.
10395 * uset_gist depends on constraints without existentially quantified
10396 * variables sorting first.
10398 * For constraints that have the same latest variable, those
10399 * with the same coefficient for this latest variable (first in absolute value
10400 * and then in actual value) are grouped together.
10401 * This is useful for detecting pairs of constraints that can
10402 * be chained in their printed representation.
10404 * Finally, within a group, constraints are sorted according to
10405 * their coefficients (excluding the constant term).
10407 static int sort_constraint_cmp(const void *p1
, const void *p2
, void *arg
)
10409 isl_int
**c1
= (isl_int
**) p1
;
10410 isl_int
**c2
= (isl_int
**) p2
;
10412 unsigned size
= *(unsigned *) arg
;
10415 l1
= isl_seq_last_non_zero(*c1
+ 1, size
);
10416 l2
= isl_seq_last_non_zero(*c2
+ 1, size
);
10421 cmp
= isl_int_abs_cmp((*c1
)[1 + l1
], (*c2
)[1 + l1
]);
10424 cmp
= isl_int_cmp((*c1
)[1 + l1
], (*c2
)[1 + l1
]);
10428 return isl_seq_cmp(*c1
+ 1, *c2
+ 1, size
);
10431 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
10432 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
10433 * and 0 if the two constraints are the same (up to the constant term).
10435 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map
*bmap
,
10436 isl_int
*c1
, isl_int
*c2
)
10441 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
10445 return sort_constraint_cmp(&c1
, &c2
, &size
);
10448 __isl_give isl_basic_map
*isl_basic_map_sort_constraints(
10449 __isl_take isl_basic_map
*bmap
)
10456 if (bmap
->n_ineq
== 0)
10458 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_SORTED
))
10460 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
10462 return isl_basic_map_free(bmap
);
10464 if (isl_sort(bmap
->ineq
, bmap
->n_ineq
, sizeof(isl_int
*),
10465 &sort_constraint_cmp
, &size
) < 0)
10466 return isl_basic_map_free(bmap
);
10467 ISL_F_SET(bmap
, ISL_BASIC_MAP_SORTED
);
10471 __isl_give isl_basic_set
*isl_basic_set_sort_constraints(
10472 __isl_take isl_basic_set
*bset
)
10474 isl_basic_map
*bmap
= bset_to_bmap(bset
);
10475 return bset_from_bmap(isl_basic_map_sort_constraints(bmap
));
10478 __isl_give isl_basic_map
*isl_basic_map_normalize(
10479 __isl_take isl_basic_map
*bmap
)
10481 bmap
= isl_basic_map_remove_redundancies(bmap
);
10482 bmap
= isl_basic_map_sort_constraints(bmap
);
10485 int isl_basic_map_plain_cmp(__isl_keep isl_basic_map
*bmap1
,
10486 __isl_keep isl_basic_map
*bmap2
)
10490 isl_space
*space1
, *space2
;
10492 if (!bmap1
|| !bmap2
)
10495 if (bmap1
== bmap2
)
10497 space1
= isl_basic_map_peek_space(bmap1
);
10498 space2
= isl_basic_map_peek_space(bmap2
);
10499 cmp
= isl_space_cmp(space1
, space2
);
10502 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) !=
10503 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_RATIONAL
))
10504 return ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) ? -1 : 1;
10505 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
) &&
10506 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
10508 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
))
10510 if (ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
10512 if (bmap1
->n_eq
!= bmap2
->n_eq
)
10513 return bmap1
->n_eq
- bmap2
->n_eq
;
10514 if (bmap1
->n_ineq
!= bmap2
->n_ineq
)
10515 return bmap1
->n_ineq
- bmap2
->n_ineq
;
10516 if (bmap1
->n_div
!= bmap2
->n_div
)
10517 return bmap1
->n_div
- bmap2
->n_div
;
10518 total
= isl_basic_map_dim(bmap1
, isl_dim_all
);
10521 for (i
= 0; i
< bmap1
->n_eq
; ++i
) {
10522 cmp
= isl_seq_cmp(bmap1
->eq
[i
], bmap2
->eq
[i
], 1+total
);
10526 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
10527 cmp
= isl_seq_cmp(bmap1
->ineq
[i
], bmap2
->ineq
[i
], 1+total
);
10531 for (i
= 0; i
< bmap1
->n_div
; ++i
) {
10532 isl_bool unknown1
, unknown2
;
10534 unknown1
= isl_basic_map_div_is_marked_unknown(bmap1
, i
);
10535 unknown2
= isl_basic_map_div_is_marked_unknown(bmap2
, i
);
10536 if (unknown1
< 0 || unknown2
< 0)
10538 if (unknown1
&& unknown2
)
10544 cmp
= isl_seq_cmp(bmap1
->div
[i
], bmap2
->div
[i
], 1+1+total
);
10551 int isl_basic_set_plain_cmp(__isl_keep isl_basic_set
*bset1
,
10552 __isl_keep isl_basic_set
*bset2
)
10554 return isl_basic_map_plain_cmp(bset1
, bset2
);
10557 int isl_set_plain_cmp(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
10563 if (set1
->n
!= set2
->n
)
10564 return set1
->n
- set2
->n
;
10566 for (i
= 0; i
< set1
->n
; ++i
) {
10567 cmp
= isl_basic_set_plain_cmp(set1
->p
[i
], set2
->p
[i
]);
10575 isl_bool
isl_basic_map_plain_is_equal(__isl_keep isl_basic_map
*bmap1
,
10576 __isl_keep isl_basic_map
*bmap2
)
10578 if (!bmap1
|| !bmap2
)
10579 return isl_bool_error
;
10580 return isl_basic_map_plain_cmp(bmap1
, bmap2
) == 0;
10583 isl_bool
isl_basic_set_plain_is_equal(__isl_keep isl_basic_set
*bset1
,
10584 __isl_keep isl_basic_set
*bset2
)
10586 return isl_basic_map_plain_is_equal(bset_to_bmap(bset1
),
10587 bset_to_bmap(bset2
));
10590 static int qsort_bmap_cmp(const void *p1
, const void *p2
)
10592 isl_basic_map
*bmap1
= *(isl_basic_map
**) p1
;
10593 isl_basic_map
*bmap2
= *(isl_basic_map
**) p2
;
10595 return isl_basic_map_plain_cmp(bmap1
, bmap2
);
10598 /* Sort the basic maps of "map" and remove duplicate basic maps.
10600 * While removing basic maps, we make sure that the basic maps remain
10601 * sorted because isl_map_normalize expects the basic maps of the result
10604 static __isl_give isl_map
*sort_and_remove_duplicates(__isl_take isl_map
*map
)
10608 map
= isl_map_remove_empty_parts(map
);
10611 qsort(map
->p
, map
->n
, sizeof(struct isl_basic_map
*), qsort_bmap_cmp
);
10612 for (i
= map
->n
- 1; i
>= 1; --i
) {
10613 if (!isl_basic_map_plain_is_equal(map
->p
[i
- 1], map
->p
[i
]))
10615 isl_basic_map_free(map
->p
[i
-1]);
10616 for (j
= i
; j
< map
->n
; ++j
)
10617 map
->p
[j
- 1] = map
->p
[j
];
10624 /* Remove obvious duplicates among the basic maps of "map".
10626 * Unlike isl_map_normalize, this function does not remove redundant
10627 * constraints and only removes duplicates that have exactly the same
10628 * constraints in the input. It does sort the constraints and
10629 * the basic maps to ease the detection of duplicates.
10631 * If "map" has already been normalized or if the basic maps are
10632 * disjoint, then there can be no duplicates.
10634 __isl_give isl_map
*isl_map_remove_obvious_duplicates(__isl_take isl_map
*map
)
10637 isl_basic_map
*bmap
;
10643 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
| ISL_MAP_DISJOINT
))
10645 for (i
= 0; i
< map
->n
; ++i
) {
10646 bmap
= isl_basic_map_copy(map
->p
[i
]);
10647 bmap
= isl_basic_map_sort_constraints(bmap
);
10649 return isl_map_free(map
);
10650 isl_basic_map_free(map
->p
[i
]);
10654 map
= sort_and_remove_duplicates(map
);
10658 /* We normalize in place, but if anything goes wrong we need
10659 * to return NULL, so we need to make sure we don't change the
10660 * meaning of any possible other copies of map.
10662 __isl_give isl_map
*isl_map_normalize(__isl_take isl_map
*map
)
10665 struct isl_basic_map
*bmap
;
10669 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
))
10671 for (i
= 0; i
< map
->n
; ++i
) {
10672 bmap
= isl_basic_map_normalize(isl_basic_map_copy(map
->p
[i
]));
10675 isl_basic_map_free(map
->p
[i
]);
10679 map
= sort_and_remove_duplicates(map
);
10681 ISL_F_SET(map
, ISL_MAP_NORMALIZED
);
10688 __isl_give isl_set
*isl_set_normalize(__isl_take isl_set
*set
)
10690 return set_from_map(isl_map_normalize(set_to_map(set
)));
10693 isl_bool
isl_map_plain_is_equal(__isl_keep isl_map
*map1
,
10694 __isl_keep isl_map
*map2
)
10699 if (!map1
|| !map2
)
10700 return isl_bool_error
;
10703 return isl_bool_true
;
10704 equal
= isl_map_has_equal_space(map1
, map2
);
10705 if (equal
< 0 || !equal
)
10708 map1
= isl_map_copy(map1
);
10709 map2
= isl_map_copy(map2
);
10710 map1
= isl_map_normalize(map1
);
10711 map2
= isl_map_normalize(map2
);
10712 if (!map1
|| !map2
)
10714 equal
= map1
->n
== map2
->n
;
10715 for (i
= 0; equal
&& i
< map1
->n
; ++i
) {
10716 equal
= isl_basic_map_plain_is_equal(map1
->p
[i
], map2
->p
[i
]);
10720 isl_map_free(map1
);
10721 isl_map_free(map2
);
10724 isl_map_free(map1
);
10725 isl_map_free(map2
);
10726 return isl_bool_error
;
10729 isl_bool
isl_set_plain_is_equal(__isl_keep isl_set
*set1
,
10730 __isl_keep isl_set
*set2
)
10732 return isl_map_plain_is_equal(set_to_map(set1
), set_to_map(set2
));
10735 /* Return the basic maps in "map" as a list.
10737 __isl_give isl_basic_map_list
*isl_map_get_basic_map_list(
10738 __isl_keep isl_map
*map
)
10742 isl_basic_map_list
*list
;
10746 ctx
= isl_map_get_ctx(map
);
10747 list
= isl_basic_map_list_alloc(ctx
, map
->n
);
10749 for (i
= 0; i
< map
->n
; ++i
) {
10750 isl_basic_map
*bmap
;
10752 bmap
= isl_basic_map_copy(map
->p
[i
]);
10753 list
= isl_basic_map_list_add(list
, bmap
);
10759 /* Return the intersection of the elements in the non-empty list "list".
10760 * All elements are assumed to live in the same space.
10762 __isl_give isl_basic_map
*isl_basic_map_list_intersect(
10763 __isl_take isl_basic_map_list
*list
)
10767 isl_basic_map
*bmap
;
10769 n
= isl_basic_map_list_n_basic_map(list
);
10773 isl_die(isl_basic_map_list_get_ctx(list
), isl_error_invalid
,
10774 "expecting non-empty list", goto error
);
10776 bmap
= isl_basic_map_list_get_basic_map(list
, 0);
10777 for (i
= 1; i
< n
; ++i
) {
10778 isl_basic_map
*bmap_i
;
10780 bmap_i
= isl_basic_map_list_get_basic_map(list
, i
);
10781 bmap
= isl_basic_map_intersect(bmap
, bmap_i
);
10784 isl_basic_map_list_free(list
);
10787 isl_basic_map_list_free(list
);
10791 /* Return the intersection of the elements in the non-empty list "list".
10792 * All elements are assumed to live in the same space.
10794 __isl_give isl_basic_set
*isl_basic_set_list_intersect(
10795 __isl_take isl_basic_set_list
*list
)
10797 return isl_basic_map_list_intersect(list
);
10800 /* Return the union of the elements of "list".
10801 * The list is required to have at least one element.
10803 __isl_give isl_set
*isl_basic_set_list_union(
10804 __isl_take isl_basic_set_list
*list
)
10809 isl_basic_set
*bset
;
10812 n
= isl_basic_set_list_n_basic_set(list
);
10816 isl_die(isl_basic_set_list_get_ctx(list
), isl_error_invalid
,
10817 "expecting non-empty list", goto error
);
10819 bset
= isl_basic_set_list_get_basic_set(list
, 0);
10820 space
= isl_basic_set_get_space(bset
);
10821 isl_basic_set_free(bset
);
10823 set
= isl_set_alloc_space(space
, n
, 0);
10824 for (i
= 0; i
< n
; ++i
) {
10825 bset
= isl_basic_set_list_get_basic_set(list
, i
);
10826 set
= isl_set_add_basic_set(set
, bset
);
10829 isl_basic_set_list_free(list
);
10832 isl_basic_set_list_free(list
);
10836 /* Return the union of the elements in the non-empty list "list".
10837 * All elements are assumed to live in the same space.
10839 __isl_give isl_set
*isl_set_list_union(__isl_take isl_set_list
*list
)
10845 n
= isl_set_list_n_set(list
);
10849 isl_die(isl_set_list_get_ctx(list
), isl_error_invalid
,
10850 "expecting non-empty list", goto error
);
10852 set
= isl_set_list_get_set(list
, 0);
10853 for (i
= 1; i
< n
; ++i
) {
10856 set_i
= isl_set_list_get_set(list
, i
);
10857 set
= isl_set_union(set
, set_i
);
10860 isl_set_list_free(list
);
10863 isl_set_list_free(list
);
10867 __isl_give isl_basic_map
*isl_basic_map_product(
10868 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
10870 isl_space
*space_result
= NULL
;
10871 struct isl_basic_map
*bmap
;
10872 unsigned in1
, in2
, out1
, out2
, nparam
, total
, pos
;
10873 struct isl_dim_map
*dim_map1
, *dim_map2
;
10875 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
10877 space_result
= isl_space_product(isl_space_copy(bmap1
->dim
),
10878 isl_space_copy(bmap2
->dim
));
10880 in1
= isl_basic_map_dim(bmap1
, isl_dim_in
);
10881 in2
= isl_basic_map_dim(bmap2
, isl_dim_in
);
10882 out1
= isl_basic_map_dim(bmap1
, isl_dim_out
);
10883 out2
= isl_basic_map_dim(bmap2
, isl_dim_out
);
10884 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
10886 total
= nparam
+ in1
+ in2
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
10887 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
10888 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
10889 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
10890 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
10891 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
10892 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
10893 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
10894 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
10895 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
10896 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
10898 bmap
= isl_basic_map_alloc_space(space_result
,
10899 bmap1
->n_div
+ bmap2
->n_div
,
10900 bmap1
->n_eq
+ bmap2
->n_eq
,
10901 bmap1
->n_ineq
+ bmap2
->n_ineq
);
10902 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
10903 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
10904 bmap
= isl_basic_map_simplify(bmap
);
10905 return isl_basic_map_finalize(bmap
);
10907 isl_basic_map_free(bmap1
);
10908 isl_basic_map_free(bmap2
);
10912 __isl_give isl_basic_map
*isl_basic_map_flat_product(
10913 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
10915 isl_basic_map
*prod
;
10917 prod
= isl_basic_map_product(bmap1
, bmap2
);
10918 prod
= isl_basic_map_flatten(prod
);
10922 __isl_give isl_basic_set
*isl_basic_set_flat_product(
10923 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
10925 return isl_basic_map_flat_range_product(bset1
, bset2
);
10928 __isl_give isl_basic_map
*isl_basic_map_domain_product(
10929 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
10931 isl_space
*space1
, *space2
;
10932 isl_space
*space_result
= NULL
;
10933 isl_basic_map
*bmap
;
10934 isl_size in1
, in2
, out
, nparam
;
10935 unsigned total
, pos
;
10936 struct isl_dim_map
*dim_map1
, *dim_map2
;
10938 in1
= isl_basic_map_dim(bmap1
, isl_dim_in
);
10939 in2
= isl_basic_map_dim(bmap2
, isl_dim_in
);
10940 out
= isl_basic_map_dim(bmap1
, isl_dim_out
);
10941 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
10942 if (in1
< 0 || in2
< 0 || out
< 0 || nparam
< 0)
10945 space1
= isl_basic_map_get_space(bmap1
);
10946 space2
= isl_basic_map_get_space(bmap2
);
10947 space_result
= isl_space_domain_product(space1
, space2
);
10949 total
= nparam
+ in1
+ in2
+ out
+ bmap1
->n_div
+ bmap2
->n_div
;
10950 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
10951 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
10952 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
10953 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
10954 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
10955 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
10956 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
10957 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
);
10958 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out
);
10959 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
10961 bmap
= isl_basic_map_alloc_space(space_result
,
10962 bmap1
->n_div
+ bmap2
->n_div
,
10963 bmap1
->n_eq
+ bmap2
->n_eq
,
10964 bmap1
->n_ineq
+ bmap2
->n_ineq
);
10965 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
10966 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
10967 bmap
= isl_basic_map_simplify(bmap
);
10968 return isl_basic_map_finalize(bmap
);
10970 isl_basic_map_free(bmap1
);
10971 isl_basic_map_free(bmap2
);
10975 __isl_give isl_basic_map
*isl_basic_map_range_product(
10976 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
10979 isl_space
*space_result
= NULL
;
10980 isl_basic_map
*bmap
;
10981 isl_size in
, out1
, out2
, nparam
;
10982 unsigned total
, pos
;
10983 struct isl_dim_map
*dim_map1
, *dim_map2
;
10985 rational
= isl_basic_map_is_rational(bmap1
);
10986 if (rational
>= 0 && rational
)
10987 rational
= isl_basic_map_is_rational(bmap2
);
10988 in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
10989 out1
= isl_basic_map_dim(bmap1
, isl_dim_out
);
10990 out2
= isl_basic_map_dim(bmap2
, isl_dim_out
);
10991 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
10992 if (in
< 0 || out1
< 0 || out2
< 0 || nparam
< 0 || rational
< 0)
10995 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
10998 space_result
= isl_space_range_product(isl_space_copy(bmap1
->dim
),
10999 isl_space_copy(bmap2
->dim
));
11001 total
= nparam
+ in
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
11002 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
11003 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
11004 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
11005 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
11006 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
11007 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
11008 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in
);
11009 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
11010 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
11011 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
11013 bmap
= isl_basic_map_alloc_space(space_result
,
11014 bmap1
->n_div
+ bmap2
->n_div
,
11015 bmap1
->n_eq
+ bmap2
->n_eq
,
11016 bmap1
->n_ineq
+ bmap2
->n_ineq
);
11017 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
11018 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
11020 bmap
= isl_basic_map_set_rational(bmap
);
11021 bmap
= isl_basic_map_simplify(bmap
);
11022 return isl_basic_map_finalize(bmap
);
11024 isl_basic_map_free(bmap1
);
11025 isl_basic_map_free(bmap2
);
11029 __isl_give isl_basic_map
*isl_basic_map_flat_range_product(
11030 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
11032 isl_basic_map
*prod
;
11034 prod
= isl_basic_map_range_product(bmap1
, bmap2
);
11035 prod
= isl_basic_map_flatten_range(prod
);
11039 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
11040 * and collect the results.
11041 * The result live in the space obtained by calling "space_product"
11042 * on the spaces of "map1" and "map2".
11043 * If "remove_duplicates" is set then the result may contain duplicates
11044 * (even if the inputs do not) and so we try and remove the obvious
11047 static __isl_give isl_map
*map_product(__isl_take isl_map
*map1
,
11048 __isl_take isl_map
*map2
,
11049 __isl_give isl_space
*(*space_product
)(__isl_take isl_space
*left
,
11050 __isl_take isl_space
*right
),
11051 __isl_give isl_basic_map
*(*basic_map_product
)(
11052 __isl_take isl_basic_map
*left
,
11053 __isl_take isl_basic_map
*right
),
11054 int remove_duplicates
)
11056 unsigned flags
= 0;
11057 struct isl_map
*result
;
11061 m
= isl_map_has_equal_params(map1
, map2
);
11065 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
11066 "parameters don't match", goto error
);
11068 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
11069 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
11070 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
11072 result
= isl_map_alloc_space(space_product(isl_space_copy(map1
->dim
),
11073 isl_space_copy(map2
->dim
)),
11074 map1
->n
* map2
->n
, flags
);
11077 for (i
= 0; i
< map1
->n
; ++i
)
11078 for (j
= 0; j
< map2
->n
; ++j
) {
11079 struct isl_basic_map
*part
;
11080 part
= basic_map_product(isl_basic_map_copy(map1
->p
[i
]),
11081 isl_basic_map_copy(map2
->p
[j
]));
11082 if (isl_basic_map_is_empty(part
))
11083 isl_basic_map_free(part
);
11085 result
= isl_map_add_basic_map(result
, part
);
11089 if (remove_duplicates
)
11090 result
= isl_map_remove_obvious_duplicates(result
);
11091 isl_map_free(map1
);
11092 isl_map_free(map2
);
11095 isl_map_free(map1
);
11096 isl_map_free(map2
);
11100 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
11102 __isl_give isl_map
*isl_map_product(__isl_take isl_map
*map1
,
11103 __isl_take isl_map
*map2
)
11105 isl_map_align_params_bin(&map1
, &map2
);
11106 return map_product(map1
, map2
, &isl_space_product
,
11107 &isl_basic_map_product
, 0);
11110 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
11112 __isl_give isl_map
*isl_map_flat_product(__isl_take isl_map
*map1
,
11113 __isl_take isl_map
*map2
)
11117 prod
= isl_map_product(map1
, map2
);
11118 prod
= isl_map_flatten(prod
);
11122 /* Given two set A and B, construct its Cartesian product A x B.
11124 __isl_give isl_set
*isl_set_product(__isl_take isl_set
*set1
,
11125 __isl_take isl_set
*set2
)
11127 return isl_map_range_product(set1
, set2
);
11130 __isl_give isl_set
*isl_set_flat_product(__isl_take isl_set
*set1
,
11131 __isl_take isl_set
*set2
)
11133 return isl_map_flat_range_product(set1
, set2
);
11136 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
11138 __isl_give isl_map
*isl_map_domain_product(__isl_take isl_map
*map1
,
11139 __isl_take isl_map
*map2
)
11141 isl_map_align_params_bin(&map1
, &map2
);
11142 return map_product(map1
, map2
, &isl_space_domain_product
,
11143 &isl_basic_map_domain_product
, 1);
11146 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
11148 __isl_give isl_map
*isl_map_range_product(__isl_take isl_map
*map1
,
11149 __isl_take isl_map
*map2
)
11151 isl_map_align_params_bin(&map1
, &map2
);
11152 return map_product(map1
, map2
, &isl_space_range_product
,
11153 &isl_basic_map_range_product
, 1);
11156 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
11158 __isl_give isl_map
*isl_map_factor_domain(__isl_take isl_map
*map
)
11161 isl_size total1
, keep1
, total2
, keep2
;
11163 total1
= isl_map_dim(map
, isl_dim_in
);
11164 total2
= isl_map_dim(map
, isl_dim_out
);
11165 if (total1
< 0 || total2
< 0)
11166 return isl_map_free(map
);
11167 if (!isl_space_domain_is_wrapping(map
->dim
) ||
11168 !isl_space_range_is_wrapping(map
->dim
))
11169 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
11170 "not a product", return isl_map_free(map
));
11172 space
= isl_map_get_space(map
);
11173 space
= isl_space_factor_domain(space
);
11174 keep1
= isl_space_dim(space
, isl_dim_in
);
11175 keep2
= isl_space_dim(space
, isl_dim_out
);
11176 if (keep1
< 0 || keep2
< 0)
11177 map
= isl_map_free(map
);
11178 map
= isl_map_project_out(map
, isl_dim_in
, keep1
, total1
- keep1
);
11179 map
= isl_map_project_out(map
, isl_dim_out
, keep2
, total2
- keep2
);
11180 map
= isl_map_reset_space(map
, space
);
11185 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
11187 __isl_give isl_map
*isl_map_factor_range(__isl_take isl_map
*map
)
11190 isl_size total1
, keep1
, total2
, keep2
;
11192 total1
= isl_map_dim(map
, isl_dim_in
);
11193 total2
= isl_map_dim(map
, isl_dim_out
);
11194 if (total1
< 0 || total2
< 0)
11195 return isl_map_free(map
);
11196 if (!isl_space_domain_is_wrapping(map
->dim
) ||
11197 !isl_space_range_is_wrapping(map
->dim
))
11198 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
11199 "not a product", return isl_map_free(map
));
11201 space
= isl_map_get_space(map
);
11202 space
= isl_space_factor_range(space
);
11203 keep1
= isl_space_dim(space
, isl_dim_in
);
11204 keep2
= isl_space_dim(space
, isl_dim_out
);
11205 if (keep1
< 0 || keep2
< 0)
11206 map
= isl_map_free(map
);
11207 map
= isl_map_project_out(map
, isl_dim_in
, 0, total1
- keep1
);
11208 map
= isl_map_project_out(map
, isl_dim_out
, 0, total2
- keep2
);
11209 map
= isl_map_reset_space(map
, space
);
11214 /* Given a map of the form [A -> B] -> C, return the map A -> C.
11216 __isl_give isl_map
*isl_map_domain_factor_domain(__isl_take isl_map
*map
)
11219 isl_size total
, keep
;
11221 total
= isl_map_dim(map
, isl_dim_in
);
11223 return isl_map_free(map
);
11224 if (!isl_space_domain_is_wrapping(map
->dim
))
11225 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
11226 "domain is not a product", return isl_map_free(map
));
11228 space
= isl_map_get_space(map
);
11229 space
= isl_space_domain_factor_domain(space
);
11230 keep
= isl_space_dim(space
, isl_dim_in
);
11232 map
= isl_map_free(map
);
11233 map
= isl_map_project_out(map
, isl_dim_in
, keep
, total
- keep
);
11234 map
= isl_map_reset_space(map
, space
);
11239 /* Given a map of the form [A -> B] -> C, return the map B -> C.
11241 __isl_give isl_map
*isl_map_domain_factor_range(__isl_take isl_map
*map
)
11244 isl_size total
, keep
;
11246 total
= isl_map_dim(map
, isl_dim_in
);
11248 return isl_map_free(map
);
11249 if (!isl_space_domain_is_wrapping(map
->dim
))
11250 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
11251 "domain is not a product", return isl_map_free(map
));
11253 space
= isl_map_get_space(map
);
11254 space
= isl_space_domain_factor_range(space
);
11255 keep
= isl_space_dim(space
, isl_dim_in
);
11257 map
= isl_map_free(map
);
11258 map
= isl_map_project_out(map
, isl_dim_in
, 0, total
- keep
);
11259 map
= isl_map_reset_space(map
, space
);
11264 /* Given a map A -> [B -> C], extract the map A -> B.
11266 __isl_give isl_map
*isl_map_range_factor_domain(__isl_take isl_map
*map
)
11269 isl_size total
, keep
;
11271 total
= isl_map_dim(map
, isl_dim_out
);
11273 return isl_map_free(map
);
11274 if (!isl_space_range_is_wrapping(map
->dim
))
11275 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
11276 "range is not a product", return isl_map_free(map
));
11278 space
= isl_map_get_space(map
);
11279 space
= isl_space_range_factor_domain(space
);
11280 keep
= isl_space_dim(space
, isl_dim_out
);
11282 map
= isl_map_free(map
);
11283 map
= isl_map_project_out(map
, isl_dim_out
, keep
, total
- keep
);
11284 map
= isl_map_reset_space(map
, space
);
11289 /* Given a map A -> [B -> C], extract the map A -> C.
11291 __isl_give isl_map
*isl_map_range_factor_range(__isl_take isl_map
*map
)
11294 isl_size total
, keep
;
11296 total
= isl_map_dim(map
, isl_dim_out
);
11298 return isl_map_free(map
);
11299 if (!isl_space_range_is_wrapping(map
->dim
))
11300 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
11301 "range is not a product", return isl_map_free(map
));
11303 space
= isl_map_get_space(map
);
11304 space
= isl_space_range_factor_range(space
);
11305 keep
= isl_space_dim(space
, isl_dim_out
);
11307 map
= isl_map_free(map
);
11308 map
= isl_map_project_out(map
, isl_dim_out
, 0, total
- keep
);
11309 map
= isl_map_reset_space(map
, space
);
11314 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
11316 __isl_give isl_map
*isl_map_flat_domain_product(__isl_take isl_map
*map1
,
11317 __isl_take isl_map
*map2
)
11321 prod
= isl_map_domain_product(map1
, map2
);
11322 prod
= isl_map_flatten_domain(prod
);
11326 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
11328 __isl_give isl_map
*isl_map_flat_range_product(__isl_take isl_map
*map1
,
11329 __isl_take isl_map
*map2
)
11333 prod
= isl_map_range_product(map1
, map2
);
11334 prod
= isl_map_flatten_range(prod
);
11338 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map
*bmap
)
11341 uint32_t hash
= isl_hash_init();
11346 bmap
= isl_basic_map_copy(bmap
);
11347 bmap
= isl_basic_map_normalize(bmap
);
11348 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
11351 isl_hash_byte(hash
, bmap
->n_eq
& 0xFF);
11352 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
11354 c_hash
= isl_seq_get_hash(bmap
->eq
[i
], 1 + total
);
11355 isl_hash_hash(hash
, c_hash
);
11357 isl_hash_byte(hash
, bmap
->n_ineq
& 0xFF);
11358 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
11360 c_hash
= isl_seq_get_hash(bmap
->ineq
[i
], 1 + total
);
11361 isl_hash_hash(hash
, c_hash
);
11363 isl_hash_byte(hash
, bmap
->n_div
& 0xFF);
11364 for (i
= 0; i
< bmap
->n_div
; ++i
) {
11366 if (isl_int_is_zero(bmap
->div
[i
][0]))
11368 isl_hash_byte(hash
, i
& 0xFF);
11369 c_hash
= isl_seq_get_hash(bmap
->div
[i
], 1 + 1 + total
);
11370 isl_hash_hash(hash
, c_hash
);
11372 isl_basic_map_free(bmap
);
11376 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set
*bset
)
11378 return isl_basic_map_get_hash(bset_to_bmap(bset
));
11381 uint32_t isl_map_get_hash(__isl_keep isl_map
*map
)
11388 map
= isl_map_copy(map
);
11389 map
= isl_map_normalize(map
);
11393 hash
= isl_hash_init();
11394 for (i
= 0; i
< map
->n
; ++i
) {
11395 uint32_t bmap_hash
;
11396 bmap_hash
= isl_basic_map_get_hash(map
->p
[i
]);
11397 isl_hash_hash(hash
, bmap_hash
);
11405 uint32_t isl_set_get_hash(__isl_keep isl_set
*set
)
11407 return isl_map_get_hash(set_to_map(set
));
11410 /* Return the number of basic maps in the (current) representation of "map".
11412 isl_size
isl_map_n_basic_map(__isl_keep isl_map
*map
)
11414 return map
? map
->n
: isl_size_error
;
11417 isl_size
isl_set_n_basic_set(__isl_keep isl_set
*set
)
11419 return set
? set
->n
: isl_size_error
;
11422 isl_stat
isl_map_foreach_basic_map(__isl_keep isl_map
*map
,
11423 isl_stat (*fn
)(__isl_take isl_basic_map
*bmap
, void *user
), void *user
)
11428 return isl_stat_error
;
11430 for (i
= 0; i
< map
->n
; ++i
)
11431 if (fn(isl_basic_map_copy(map
->p
[i
]), user
) < 0)
11432 return isl_stat_error
;
11434 return isl_stat_ok
;
11437 isl_stat
isl_set_foreach_basic_set(__isl_keep isl_set
*set
,
11438 isl_stat (*fn
)(__isl_take isl_basic_set
*bset
, void *user
), void *user
)
11443 return isl_stat_error
;
11445 for (i
= 0; i
< set
->n
; ++i
)
11446 if (fn(isl_basic_set_copy(set
->p
[i
]), user
) < 0)
11447 return isl_stat_error
;
11449 return isl_stat_ok
;
11452 /* Does "test" succeed on every basic set in "set"?
11454 isl_bool
isl_set_every_basic_set(__isl_keep isl_set
*set
,
11455 isl_bool (*test
)(__isl_keep isl_basic_set
*bset
, void *user
),
11461 return isl_bool_error
;
11463 for (i
= 0; i
< set
->n
; ++i
) {
11466 r
= test(set
->p
[i
], user
);
11471 return isl_bool_true
;
11474 /* Return a list of basic sets, the union of which is equal to "set".
11476 __isl_give isl_basic_set_list
*isl_set_get_basic_set_list(
11477 __isl_keep isl_set
*set
)
11480 isl_basic_set_list
*list
;
11485 list
= isl_basic_set_list_alloc(isl_set_get_ctx(set
), set
->n
);
11486 for (i
= 0; i
< set
->n
; ++i
) {
11487 isl_basic_set
*bset
;
11489 bset
= isl_basic_set_copy(set
->p
[i
]);
11490 list
= isl_basic_set_list_add(list
, bset
);
11496 __isl_give isl_basic_set
*isl_basic_set_lift(__isl_take isl_basic_set
*bset
)
11503 bset
= isl_basic_set_cow(bset
);
11507 space
= isl_basic_set_get_space(bset
);
11508 space
= isl_space_lift(space
, bset
->n_div
);
11511 isl_space_free(bset
->dim
);
11513 bset
->extra
-= bset
->n_div
;
11516 bset
= isl_basic_set_finalize(bset
);
11520 isl_basic_set_free(bset
);
11524 __isl_give isl_set
*isl_set_lift(__isl_take isl_set
*set
)
11530 set
= set_from_map(isl_map_align_divs_internal(set_to_map(set
)));
11535 set
= isl_set_cow(set
);
11539 n_div
= set
->p
[0]->n_div
;
11540 space
= isl_set_get_space(set
);
11541 space
= isl_space_lift(space
, n_div
);
11544 isl_space_free(set
->dim
);
11547 for (i
= 0; i
< set
->n
; ++i
) {
11548 set
->p
[i
] = isl_basic_set_lift(set
->p
[i
]);
11559 int isl_basic_set_size(__isl_keep isl_basic_set
*bset
)
11564 dim
= isl_basic_set_dim(bset
, isl_dim_all
);
11567 size
+= bset
->n_eq
* (1 + dim
);
11568 size
+= bset
->n_ineq
* (1 + dim
);
11569 size
+= bset
->n_div
* (2 + dim
);
11574 int isl_set_size(__isl_keep isl_set
*set
)
11582 for (i
= 0; i
< set
->n
; ++i
)
11583 size
+= isl_basic_set_size(set
->p
[i
]);
11588 /* Check if there is any lower bound (if lower == 0) and/or upper
11589 * bound (if upper == 0) on the specified dim.
11591 static isl_bool
basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
11592 enum isl_dim_type type
, unsigned pos
, int lower
, int upper
)
11596 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
11597 return isl_bool_error
;
11599 pos
+= isl_basic_map_offset(bmap
, type
);
11601 for (i
= 0; i
< bmap
->n_div
; ++i
) {
11602 if (isl_int_is_zero(bmap
->div
[i
][0]))
11604 if (!isl_int_is_zero(bmap
->div
[i
][1 + pos
]))
11605 return isl_bool_true
;
11608 for (i
= 0; i
< bmap
->n_eq
; ++i
)
11609 if (!isl_int_is_zero(bmap
->eq
[i
][pos
]))
11610 return isl_bool_true
;
11612 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
11613 int sgn
= isl_int_sgn(bmap
->ineq
[i
][pos
]);
11620 return lower
&& upper
;
11623 isl_bool
isl_basic_map_dim_is_bounded(__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, 0);
11629 isl_bool
isl_basic_map_dim_has_lower_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
, 0, 1);
11635 isl_bool
isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map
*bmap
,
11636 enum isl_dim_type type
, unsigned pos
)
11638 return basic_map_dim_is_bounded(bmap
, type
, pos
, 1, 0);
11641 isl_bool
isl_map_dim_is_bounded(__isl_keep isl_map
*map
,
11642 enum isl_dim_type type
, unsigned pos
)
11647 return isl_bool_error
;
11649 for (i
= 0; i
< map
->n
; ++i
) {
11651 bounded
= isl_basic_map_dim_is_bounded(map
->p
[i
], type
, pos
);
11652 if (bounded
< 0 || !bounded
)
11656 return isl_bool_true
;
11659 /* Return true if the specified dim is involved in both an upper bound
11660 * and a lower bound.
11662 isl_bool
isl_set_dim_is_bounded(__isl_keep isl_set
*set
,
11663 enum isl_dim_type type
, unsigned pos
)
11665 return isl_map_dim_is_bounded(set_to_map(set
), type
, pos
);
11668 /* Does "map" have a bound (according to "fn") for any of its basic maps?
11670 static isl_bool
has_any_bound(__isl_keep isl_map
*map
,
11671 enum isl_dim_type type
, unsigned pos
,
11672 isl_bool (*fn
)(__isl_keep isl_basic_map
*bmap
,
11673 enum isl_dim_type type
, unsigned pos
))
11678 return isl_bool_error
;
11680 for (i
= 0; i
< map
->n
; ++i
) {
11682 bounded
= fn(map
->p
[i
], type
, pos
);
11683 if (bounded
< 0 || bounded
)
11687 return isl_bool_false
;
11690 /* Return 1 if the specified dim is involved in any lower bound.
11692 isl_bool
isl_set_dim_has_any_lower_bound(__isl_keep isl_set
*set
,
11693 enum isl_dim_type type
, unsigned pos
)
11695 return has_any_bound(set
, type
, pos
,
11696 &isl_basic_map_dim_has_lower_bound
);
11699 /* Return 1 if the specified dim is involved in any upper bound.
11701 isl_bool
isl_set_dim_has_any_upper_bound(__isl_keep isl_set
*set
,
11702 enum isl_dim_type type
, unsigned pos
)
11704 return has_any_bound(set
, type
, pos
,
11705 &isl_basic_map_dim_has_upper_bound
);
11708 /* Does "map" have a bound (according to "fn") for all of its basic maps?
11710 static isl_bool
has_bound(__isl_keep isl_map
*map
,
11711 enum isl_dim_type type
, unsigned pos
,
11712 isl_bool (*fn
)(__isl_keep isl_basic_map
*bmap
,
11713 enum isl_dim_type type
, unsigned pos
))
11718 return isl_bool_error
;
11720 for (i
= 0; i
< map
->n
; ++i
) {
11722 bounded
= fn(map
->p
[i
], type
, pos
);
11723 if (bounded
< 0 || !bounded
)
11727 return isl_bool_true
;
11730 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
11732 isl_bool
isl_set_dim_has_lower_bound(__isl_keep isl_set
*set
,
11733 enum isl_dim_type type
, unsigned pos
)
11735 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_lower_bound
);
11738 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
11740 isl_bool
isl_set_dim_has_upper_bound(__isl_keep isl_set
*set
,
11741 enum isl_dim_type type
, unsigned pos
)
11743 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_upper_bound
);
11746 /* For each of the "n" variables starting at "first", determine
11747 * the sign of the variable and put the results in the first "n"
11748 * elements of the array "signs".
11750 * 1 means that the variable is non-negative
11751 * -1 means that the variable is non-positive
11752 * 0 means the variable attains both positive and negative values.
11754 isl_stat
isl_basic_set_vars_get_sign(__isl_keep isl_basic_set
*bset
,
11755 unsigned first
, unsigned n
, int *signs
)
11757 isl_vec
*bound
= NULL
;
11758 struct isl_tab
*tab
= NULL
;
11759 struct isl_tab_undo
*snap
;
11763 total
= isl_basic_set_dim(bset
, isl_dim_all
);
11764 if (total
< 0 || !signs
)
11765 return isl_stat_error
;
11767 bound
= isl_vec_alloc(bset
->ctx
, 1 + total
);
11768 tab
= isl_tab_from_basic_set(bset
, 0);
11769 if (!bound
|| !tab
)
11772 isl_seq_clr(bound
->el
, bound
->size
);
11773 isl_int_set_si(bound
->el
[0], -1);
11775 snap
= isl_tab_snap(tab
);
11776 for (i
= 0; i
< n
; ++i
) {
11779 isl_int_set_si(bound
->el
[1 + first
+ i
], -1);
11780 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
11782 empty
= tab
->empty
;
11783 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
11784 if (isl_tab_rollback(tab
, snap
) < 0)
11792 isl_int_set_si(bound
->el
[1 + first
+ i
], 1);
11793 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
11795 empty
= tab
->empty
;
11796 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
11797 if (isl_tab_rollback(tab
, snap
) < 0)
11800 signs
[i
] = empty
? -1 : 0;
11804 isl_vec_free(bound
);
11805 return isl_stat_ok
;
11808 isl_vec_free(bound
);
11809 return isl_stat_error
;
11812 isl_stat
isl_basic_set_dims_get_sign(__isl_keep isl_basic_set
*bset
,
11813 enum isl_dim_type type
, unsigned first
, unsigned n
, int *signs
)
11815 if (!bset
|| !signs
)
11816 return isl_stat_error
;
11817 if (isl_basic_set_check_range(bset
, type
, first
, n
) < 0)
11818 return isl_stat_error
;
11820 first
+= pos(bset
->dim
, type
) - 1;
11821 return isl_basic_set_vars_get_sign(bset
, first
, n
, signs
);
11824 /* Is it possible for the integer division "div" to depend (possibly
11825 * indirectly) on any output dimensions?
11827 * If the div is undefined, then we conservatively assume that it
11828 * may depend on them.
11829 * Otherwise, we check if it actually depends on them or on any integer
11830 * divisions that may depend on them.
11832 static isl_bool
div_may_involve_output(__isl_keep isl_basic_map
*bmap
, int div
)
11835 isl_size n_out
, n_div
;
11836 unsigned o_out
, o_div
;
11838 if (isl_int_is_zero(bmap
->div
[div
][0]))
11839 return isl_bool_true
;
11841 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
11843 return isl_bool_error
;
11844 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
11846 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + o_out
, n_out
) != -1)
11847 return isl_bool_true
;
11849 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
11851 return isl_bool_error
;
11852 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
11854 for (i
= 0; i
< n_div
; ++i
) {
11855 isl_bool may_involve
;
11857 if (isl_int_is_zero(bmap
->div
[div
][1 + o_div
+ i
]))
11859 may_involve
= div_may_involve_output(bmap
, i
);
11860 if (may_involve
< 0 || may_involve
)
11861 return may_involve
;
11864 return isl_bool_false
;
11867 /* Return the first integer division of "bmap" in the range
11868 * [first, first + n[ that may depend on any output dimensions and
11869 * that has a non-zero coefficient in "c" (where the first coefficient
11870 * in "c" corresponds to integer division "first").
11872 static int first_div_may_involve_output(__isl_keep isl_basic_map
*bmap
,
11873 isl_int
*c
, int first
, int n
)
11880 for (k
= first
; k
< first
+ n
; ++k
) {
11881 isl_bool may_involve
;
11883 if (isl_int_is_zero(c
[k
]))
11885 may_involve
= div_may_involve_output(bmap
, k
);
11886 if (may_involve
< 0)
11895 /* Look for a pair of inequality constraints in "bmap" of the form
11897 * -l + i >= 0 or i >= l
11899 * n + l - i >= 0 or i <= l + n
11901 * with n < "m" and i the output dimension at position "pos".
11902 * (Note that n >= 0 as otherwise the two constraints would conflict.)
11903 * Furthermore, "l" is only allowed to involve parameters, input dimensions
11904 * and earlier output dimensions, as well as integer divisions that do
11905 * not involve any of the output dimensions.
11907 * Return the index of the first inequality constraint or bmap->n_ineq
11908 * if no such pair can be found.
11910 static int find_modulo_constraint_pair(__isl_keep isl_basic_map
*bmap
,
11911 int pos
, isl_int m
)
11916 isl_size n_div
, n_out
;
11917 unsigned o_div
, o_out
;
11920 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
11921 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
11922 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
11923 if (total
< 0 || n_out
< 0 || n_div
< 0)
11926 ctx
= isl_basic_map_get_ctx(bmap
);
11927 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
11928 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
11929 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
11930 if (!isl_int_abs_eq(bmap
->ineq
[i
][o_out
+ pos
], ctx
->one
))
11932 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + o_out
+ pos
+ 1,
11933 n_out
- (pos
+ 1)) != -1)
11935 if (first_div_may_involve_output(bmap
, bmap
->ineq
[i
] + o_div
,
11938 for (j
= i
+ 1; j
< bmap
->n_ineq
; ++j
) {
11939 if (!isl_int_abs_eq(bmap
->ineq
[j
][o_out
+ pos
],
11942 if (!isl_seq_is_neg(bmap
->ineq
[i
] + 1,
11943 bmap
->ineq
[j
] + 1, total
))
11947 if (j
>= bmap
->n_ineq
)
11949 isl_int_add(bmap
->ineq
[i
][0],
11950 bmap
->ineq
[i
][0], bmap
->ineq
[j
][0]);
11951 less
= isl_int_abs_lt(bmap
->ineq
[i
][0], m
);
11952 isl_int_sub(bmap
->ineq
[i
][0],
11953 bmap
->ineq
[i
][0], bmap
->ineq
[j
][0]);
11956 if (isl_int_is_one(bmap
->ineq
[i
][o_out
+ pos
]))
11962 return bmap
->n_ineq
;
11965 /* Return the index of the equality of "bmap" that defines
11966 * the output dimension "pos" in terms of earlier dimensions.
11967 * The equality may also involve integer divisions, as long
11968 * as those integer divisions are defined in terms of
11969 * parameters or input dimensions.
11970 * In this case, *div is set to the number of integer divisions and
11971 * *ineq is set to the number of inequality constraints (provided
11972 * div and ineq are not NULL).
11974 * The equality may also involve a single integer division involving
11975 * the output dimensions (typically only output dimension "pos") as
11976 * long as the coefficient of output dimension "pos" is 1 or -1 and
11977 * there is a pair of constraints i >= l and i <= l + n, with i referring
11978 * to output dimension "pos", l an expression involving only earlier
11979 * dimensions and n smaller than the coefficient of the integer division
11980 * in the equality. In this case, the output dimension can be defined
11981 * in terms of a modulo expression that does not involve the integer division.
11982 * *div is then set to this single integer division and
11983 * *ineq is set to the index of constraint i >= l.
11985 * Return bmap->n_eq if there is no such equality.
11986 * Return -1 on error.
11988 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map
*bmap
,
11989 int pos
, int *div
, int *ineq
)
11992 isl_size n_div
, n_out
;
11993 unsigned o_div
, o_out
;
11995 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
11996 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
11997 if (n_out
< 0 || n_div
< 0)
12000 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
12001 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
12004 *ineq
= bmap
->n_ineq
;
12007 for (j
= 0; j
< bmap
->n_eq
; ++j
) {
12008 if (isl_int_is_zero(bmap
->eq
[j
][o_out
+ pos
]))
12010 if (isl_seq_first_non_zero(bmap
->eq
[j
] + o_out
+ pos
+ 1,
12011 n_out
- (pos
+ 1)) != -1)
12013 k
= first_div_may_involve_output(bmap
, bmap
->eq
[j
] + o_div
,
12017 if (!isl_int_is_one(bmap
->eq
[j
][o_out
+ pos
]) &&
12018 !isl_int_is_negone(bmap
->eq
[j
][o_out
+ pos
]))
12020 if (first_div_may_involve_output(bmap
, bmap
->eq
[j
] + o_div
,
12021 k
+ 1, n_div
- (k
+1)) < n_div
)
12023 l
= find_modulo_constraint_pair(bmap
, pos
,
12024 bmap
->eq
[j
][o_div
+ k
]);
12027 if (l
>= bmap
->n_ineq
)
12039 /* Check if the given basic map is obviously single-valued.
12040 * In particular, for each output dimension, check that there is
12041 * an equality that defines the output dimension in terms of
12042 * earlier dimensions.
12044 isl_bool
isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map
*bmap
)
12049 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
12051 return isl_bool_error
;
12053 for (i
= 0; i
< n_out
; ++i
) {
12056 eq
= isl_basic_map_output_defining_equality(bmap
, i
,
12059 return isl_bool_error
;
12060 if (eq
>= bmap
->n_eq
)
12061 return isl_bool_false
;
12064 return isl_bool_true
;
12067 /* Check if the given basic map is single-valued.
12068 * We simply compute
12072 * and check if the result is a subset of the identity mapping.
12074 isl_bool
isl_basic_map_is_single_valued(__isl_keep isl_basic_map
*bmap
)
12077 isl_basic_map
*test
;
12081 sv
= isl_basic_map_plain_is_single_valued(bmap
);
12085 test
= isl_basic_map_reverse(isl_basic_map_copy(bmap
));
12086 test
= isl_basic_map_apply_range(test
, isl_basic_map_copy(bmap
));
12088 space
= isl_basic_map_get_space(bmap
);
12089 space
= isl_space_map_from_set(isl_space_range(space
));
12090 id
= isl_basic_map_identity(space
);
12092 sv
= isl_basic_map_is_subset(test
, id
);
12094 isl_basic_map_free(test
);
12095 isl_basic_map_free(id
);
12100 /* Check if the given map is obviously single-valued.
12102 isl_bool
isl_map_plain_is_single_valued(__isl_keep isl_map
*map
)
12105 return isl_bool_error
;
12107 return isl_bool_true
;
12109 return isl_bool_false
;
12111 return isl_basic_map_plain_is_single_valued(map
->p
[0]);
12114 /* Check if the given map is single-valued.
12115 * We simply compute
12119 * and check if the result is a subset of the identity mapping.
12121 isl_bool
isl_map_is_single_valued(__isl_keep isl_map
*map
)
12128 sv
= isl_map_plain_is_single_valued(map
);
12132 test
= isl_map_reverse(isl_map_copy(map
));
12133 test
= isl_map_apply_range(test
, isl_map_copy(map
));
12135 space
= isl_space_map_from_set(isl_space_range(isl_map_get_space(map
)));
12136 id
= isl_map_identity(space
);
12138 sv
= isl_map_is_subset(test
, id
);
12140 isl_map_free(test
);
12146 isl_bool
isl_map_is_injective(__isl_keep isl_map
*map
)
12150 map
= isl_map_copy(map
);
12151 map
= isl_map_reverse(map
);
12152 in
= isl_map_is_single_valued(map
);
12158 /* Check if the given map is obviously injective.
12160 isl_bool
isl_map_plain_is_injective(__isl_keep isl_map
*map
)
12164 map
= isl_map_copy(map
);
12165 map
= isl_map_reverse(map
);
12166 in
= isl_map_plain_is_single_valued(map
);
12172 isl_bool
isl_map_is_bijective(__isl_keep isl_map
*map
)
12176 sv
= isl_map_is_single_valued(map
);
12180 return isl_map_is_injective(map
);
12183 isl_bool
isl_set_is_singleton(__isl_keep isl_set
*set
)
12185 return isl_map_is_single_valued(set_to_map(set
));
12188 /* Does "map" only map elements to themselves?
12190 * If the domain and range spaces are different, then "map"
12191 * is considered not to be an identity relation, even if it is empty.
12192 * Otherwise, construct the maximal identity relation and
12193 * check whether "map" is a subset of this relation.
12195 isl_bool
isl_map_is_identity(__isl_keep isl_map
*map
)
12198 isl_bool equal
, is_identity
;
12200 equal
= isl_map_tuple_is_equal(map
, isl_dim_in
, map
, isl_dim_out
);
12201 if (equal
< 0 || !equal
)
12204 id
= isl_map_identity(isl_map_get_space(map
));
12205 is_identity
= isl_map_is_subset(map
, id
);
12208 return is_identity
;
12211 int isl_map_is_translation(__isl_keep isl_map
*map
)
12216 delta
= isl_map_deltas(isl_map_copy(map
));
12217 ok
= isl_set_is_singleton(delta
);
12218 isl_set_free(delta
);
12223 static int unique(isl_int
*p
, unsigned pos
, unsigned len
)
12225 if (isl_seq_first_non_zero(p
, pos
) != -1)
12227 if (isl_seq_first_non_zero(p
+ pos
+ 1, len
- pos
- 1) != -1)
12232 isl_bool
isl_basic_set_is_box(__isl_keep isl_basic_set
*bset
)
12235 isl_size nvar
, ovar
, n_div
;
12237 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
12239 return isl_bool_error
;
12241 return isl_bool_false
;
12243 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
12244 ovar
= isl_space_offset(bset
->dim
, isl_dim_set
);
12245 if (nvar
< 0 || ovar
< 0)
12246 return isl_bool_error
;
12247 for (j
= 0; j
< nvar
; ++j
) {
12248 int lower
= 0, upper
= 0;
12249 for (i
= 0; i
< bset
->n_eq
; ++i
) {
12250 if (isl_int_is_zero(bset
->eq
[i
][1 + ovar
+ j
]))
12252 if (!unique(bset
->eq
[i
] + 1 + ovar
, j
, nvar
))
12253 return isl_bool_false
;
12256 if (i
< bset
->n_eq
)
12258 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
12259 if (isl_int_is_zero(bset
->ineq
[i
][1 + ovar
+ j
]))
12261 if (!unique(bset
->ineq
[i
] + 1 + ovar
, j
, nvar
))
12262 return isl_bool_false
;
12263 if (isl_int_is_pos(bset
->ineq
[i
][1 + ovar
+ j
]))
12268 if (!lower
|| !upper
)
12269 return isl_bool_false
;
12272 return isl_bool_true
;
12275 isl_bool
isl_set_is_box(__isl_keep isl_set
*set
)
12278 return isl_bool_error
;
12280 return isl_bool_false
;
12282 return isl_basic_set_is_box(set
->p
[0]);
12285 isl_bool
isl_basic_set_is_wrapping(__isl_keep isl_basic_set
*bset
)
12288 return isl_bool_error
;
12290 return isl_space_is_wrapping(bset
->dim
);
12293 isl_bool
isl_set_is_wrapping(__isl_keep isl_set
*set
)
12296 return isl_bool_error
;
12298 return isl_space_is_wrapping(set
->dim
);
12301 /* Modify the space of "map" through a call to "change".
12302 * If "can_change" is set (not NULL), then first call it to check
12303 * if the modification is allowed, printing the error message "cannot_change"
12306 static __isl_give isl_map
*isl_map_change_space(__isl_take isl_map
*map
,
12307 isl_bool (*can_change
)(__isl_keep isl_map
*map
),
12308 const char *cannot_change
,
12309 __isl_give isl_space
*(*change
)(__isl_take isl_space
*space
))
12317 ok
= can_change
? can_change(map
) : isl_bool_true
;
12319 return isl_map_free(map
);
12321 isl_die(isl_map_get_ctx(map
), isl_error_invalid
, cannot_change
,
12322 return isl_map_free(map
));
12324 space
= change(isl_map_get_space(map
));
12325 map
= isl_map_reset_space(map
, space
);
12330 /* Is the domain of "map" a wrapped relation?
12332 isl_bool
isl_map_domain_is_wrapping(__isl_keep isl_map
*map
)
12335 return isl_bool_error
;
12337 return isl_space_domain_is_wrapping(map
->dim
);
12340 /* Does "map" have a wrapped relation in both domain and range?
12342 isl_bool
isl_map_is_product(__isl_keep isl_map
*map
)
12344 return isl_space_is_product(isl_map_peek_space(map
));
12347 /* Is the range of "map" a wrapped relation?
12349 isl_bool
isl_map_range_is_wrapping(__isl_keep isl_map
*map
)
12352 return isl_bool_error
;
12354 return isl_space_range_is_wrapping(map
->dim
);
12357 __isl_give isl_basic_set
*isl_basic_map_wrap(__isl_take isl_basic_map
*bmap
)
12361 space
= isl_basic_map_take_space(bmap
);
12362 space
= isl_space_wrap(space
);
12363 bmap
= isl_basic_map_restore_space(bmap
, space
);
12365 bmap
= isl_basic_map_finalize(bmap
);
12367 return bset_from_bmap(bmap
);
12370 /* Given a map A -> B, return the set (A -> B).
12372 __isl_give isl_set
*isl_map_wrap(__isl_take isl_map
*map
)
12374 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_wrap
);
12377 __isl_give isl_basic_map
*isl_basic_set_unwrap(__isl_take isl_basic_set
*bset
)
12379 bset
= isl_basic_set_cow(bset
);
12383 bset
->dim
= isl_space_unwrap(bset
->dim
);
12387 bset
= isl_basic_set_finalize(bset
);
12389 return bset_to_bmap(bset
);
12391 isl_basic_set_free(bset
);
12395 /* Given a set (A -> B), return the map A -> B.
12396 * Error out if "set" is not of the form (A -> B).
12398 __isl_give isl_map
*isl_set_unwrap(__isl_take isl_set
*set
)
12400 return isl_map_change_space(set
, &isl_set_is_wrapping
,
12401 "not a wrapping set", &isl_space_unwrap
);
12404 __isl_give isl_basic_map
*isl_basic_map_reset(__isl_take isl_basic_map
*bmap
,
12405 enum isl_dim_type type
)
12409 space
= isl_basic_map_take_space(bmap
);
12410 space
= isl_space_reset(space
, type
);
12411 bmap
= isl_basic_map_restore_space(bmap
, space
);
12413 bmap
= isl_basic_map_mark_final(bmap
);
12418 __isl_give isl_map
*isl_map_reset(__isl_take isl_map
*map
,
12419 enum isl_dim_type type
)
12427 if (!isl_space_is_named_or_nested(map
->dim
, type
))
12430 map
= isl_map_cow(map
);
12434 for (i
= 0; i
< map
->n
; ++i
) {
12435 map
->p
[i
] = isl_basic_map_reset(map
->p
[i
], type
);
12440 space
= isl_map_take_space(map
);
12441 space
= isl_space_reset(space
, type
);
12442 map
= isl_map_restore_space(map
, space
);
12450 __isl_give isl_basic_map
*isl_basic_map_flatten(__isl_take isl_basic_map
*bmap
)
12454 space
= isl_basic_map_take_space(bmap
);
12455 space
= isl_space_flatten(space
);
12456 bmap
= isl_basic_map_restore_space(bmap
, space
);
12458 bmap
= isl_basic_map_mark_final(bmap
);
12463 __isl_give isl_basic_set
*isl_basic_set_flatten(__isl_take isl_basic_set
*bset
)
12465 return bset_from_bmap(isl_basic_map_flatten(bset_to_bmap(bset
)));
12468 __isl_give isl_basic_map
*isl_basic_map_flatten_domain(
12469 __isl_take isl_basic_map
*bmap
)
12473 space
= isl_basic_map_take_space(bmap
);
12474 space
= isl_space_flatten_domain(space
);
12475 bmap
= isl_basic_map_restore_space(bmap
, space
);
12477 bmap
= isl_basic_map_mark_final(bmap
);
12482 __isl_give isl_basic_map
*isl_basic_map_flatten_range(
12483 __isl_take isl_basic_map
*bmap
)
12487 space
= isl_basic_map_take_space(bmap
);
12488 space
= isl_space_flatten_range(space
);
12489 bmap
= isl_basic_map_restore_space(bmap
, space
);
12491 bmap
= isl_basic_map_mark_final(bmap
);
12496 /* Remove any internal structure from the spaces of domain and range of "map".
12498 __isl_give isl_map
*isl_map_flatten(__isl_take isl_map
*map
)
12503 if (!map
->dim
->nested
[0] && !map
->dim
->nested
[1])
12506 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten
);
12509 __isl_give isl_set
*isl_set_flatten(__isl_take isl_set
*set
)
12511 return set_from_map(isl_map_flatten(set_to_map(set
)));
12514 __isl_give isl_map
*isl_set_flatten_map(__isl_take isl_set
*set
)
12516 isl_space
*space
, *flat_space
;
12519 space
= isl_set_get_space(set
);
12520 flat_space
= isl_space_flatten(isl_space_copy(space
));
12521 map
= isl_map_identity(isl_space_join(isl_space_reverse(space
),
12523 map
= isl_map_intersect_domain(map
, set
);
12528 /* Remove any internal structure from the space of the domain of "map".
12530 __isl_give isl_map
*isl_map_flatten_domain(__isl_take isl_map
*map
)
12535 if (!map
->dim
->nested
[0])
12538 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten_domain
);
12541 /* Remove any internal structure from the space of the range of "map".
12543 __isl_give isl_map
*isl_map_flatten_range(__isl_take isl_map
*map
)
12548 if (!map
->dim
->nested
[1])
12551 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten_range
);
12554 /* Reorder the dimensions of "bmap" according to the given dim_map
12555 * and set the dimension specification to "space" and
12556 * perform Gaussian elimination on the result.
12558 __isl_give isl_basic_map
*isl_basic_map_realign(__isl_take isl_basic_map
*bmap
,
12559 __isl_take isl_space
*space
, __isl_take
struct isl_dim_map
*dim_map
)
12561 isl_basic_map
*res
;
12565 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
12566 if (n_div
< 0 || !space
|| !dim_map
)
12569 flags
= bmap
->flags
;
12570 ISL_FL_CLR(flags
, ISL_BASIC_MAP_FINAL
);
12571 ISL_FL_CLR(flags
, ISL_BASIC_MAP_SORTED
);
12572 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
12573 res
= isl_basic_map_alloc_space(space
, n_div
, bmap
->n_eq
, bmap
->n_ineq
);
12574 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
12576 res
->flags
= flags
;
12577 res
= isl_basic_map_gauss(res
, NULL
);
12578 res
= isl_basic_map_finalize(res
);
12581 isl_dim_map_free(dim_map
);
12582 isl_basic_map_free(bmap
);
12583 isl_space_free(space
);
12587 /* Reorder the dimensions of "map" according to given reordering.
12589 __isl_give isl_map
*isl_map_realign(__isl_take isl_map
*map
,
12590 __isl_take isl_reordering
*r
)
12593 struct isl_dim_map
*dim_map
;
12595 map
= isl_map_cow(map
);
12596 dim_map
= isl_dim_map_from_reordering(r
);
12597 if (!map
|| !r
|| !dim_map
)
12600 for (i
= 0; i
< map
->n
; ++i
) {
12601 struct isl_dim_map
*dim_map_i
;
12604 dim_map_i
= isl_dim_map_extend(dim_map
, map
->p
[i
]);
12606 space
= isl_reordering_get_space(r
);
12607 map
->p
[i
] = isl_basic_map_realign(map
->p
[i
], space
, dim_map_i
);
12613 map
= isl_map_reset_space(map
, isl_reordering_get_space(r
));
12614 map
= isl_map_unmark_normalized(map
);
12616 isl_reordering_free(r
);
12617 isl_dim_map_free(dim_map
);
12620 isl_dim_map_free(dim_map
);
12622 isl_reordering_free(r
);
12626 __isl_give isl_set
*isl_set_realign(__isl_take isl_set
*set
,
12627 __isl_take isl_reordering
*r
)
12629 return set_from_map(isl_map_realign(set_to_map(set
), r
));
12632 __isl_give isl_map
*isl_map_align_params(__isl_take isl_map
*map
,
12633 __isl_take isl_space
*model
)
12638 if (!map
|| !model
)
12641 ctx
= isl_space_get_ctx(model
);
12642 if (!isl_space_has_named_params(model
))
12643 isl_die(ctx
, isl_error_invalid
,
12644 "model has unnamed parameters", goto error
);
12645 if (isl_map_check_named_params(map
) < 0)
12647 aligned
= isl_map_space_has_equal_params(map
, model
);
12652 isl_reordering
*exp
;
12654 space
= isl_map_peek_space(map
);
12655 exp
= isl_parameter_alignment_reordering(space
, model
);
12656 map
= isl_map_realign(map
, exp
);
12659 isl_space_free(model
);
12662 isl_space_free(model
);
12667 __isl_give isl_set
*isl_set_align_params(__isl_take isl_set
*set
,
12668 __isl_take isl_space
*model
)
12670 return isl_map_align_params(set
, model
);
12673 /* Align the parameters of "bmap" to those of "model", introducing
12674 * additional parameters if needed.
12676 __isl_give isl_basic_map
*isl_basic_map_align_params(
12677 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*model
)
12680 isl_bool equal_params
;
12681 isl_space
*bmap_space
;
12683 if (!bmap
|| !model
)
12686 ctx
= isl_space_get_ctx(model
);
12687 if (!isl_space_has_named_params(model
))
12688 isl_die(ctx
, isl_error_invalid
,
12689 "model has unnamed parameters", goto error
);
12690 if (isl_basic_map_check_named_params(bmap
) < 0)
12692 bmap_space
= isl_basic_map_peek_space(bmap
);
12693 equal_params
= isl_space_has_equal_params(bmap_space
, model
);
12694 if (equal_params
< 0)
12696 if (!equal_params
) {
12697 isl_reordering
*exp
;
12698 struct isl_dim_map
*dim_map
;
12700 exp
= isl_parameter_alignment_reordering(bmap_space
, model
);
12701 dim_map
= isl_dim_map_from_reordering(exp
);
12702 bmap
= isl_basic_map_realign(bmap
,
12703 isl_reordering_get_space(exp
),
12704 isl_dim_map_extend(dim_map
, bmap
));
12705 isl_reordering_free(exp
);
12706 isl_dim_map_free(dim_map
);
12709 isl_space_free(model
);
12712 isl_space_free(model
);
12713 isl_basic_map_free(bmap
);
12717 /* Do "bset" and "space" have the same parameters?
12719 isl_bool
isl_basic_set_space_has_equal_params(__isl_keep isl_basic_set
*bset
,
12720 __isl_keep isl_space
*space
)
12722 isl_space
*bset_space
;
12724 bset_space
= isl_basic_set_peek_space(bset
);
12725 return isl_space_has_equal_params(bset_space
, space
);
12728 /* Do "map" and "space" have the same parameters?
12730 isl_bool
isl_map_space_has_equal_params(__isl_keep isl_map
*map
,
12731 __isl_keep isl_space
*space
)
12733 isl_space
*map_space
;
12735 map_space
= isl_map_peek_space(map
);
12736 return isl_space_has_equal_params(map_space
, space
);
12739 /* Do "set" and "space" have the same parameters?
12741 isl_bool
isl_set_space_has_equal_params(__isl_keep isl_set
*set
,
12742 __isl_keep isl_space
*space
)
12744 return isl_map_space_has_equal_params(set_to_map(set
), space
);
12747 /* Align the parameters of "bset" to those of "model", introducing
12748 * additional parameters if needed.
12750 __isl_give isl_basic_set
*isl_basic_set_align_params(
12751 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*model
)
12753 return isl_basic_map_align_params(bset
, model
);
12757 #define TYPE isl_map
12758 #define isl_map_drop_dims isl_map_drop
12759 #include "isl_drop_unused_params_templ.c"
12761 /* Drop all parameters not referenced by "set".
12763 __isl_give isl_set
*isl_set_drop_unused_params(
12764 __isl_take isl_set
*set
)
12766 return set_from_map(isl_map_drop_unused_params(set_to_map(set
)));
12770 #define TYPE isl_basic_map
12771 #define isl_basic_map_drop_dims isl_basic_map_drop
12772 #include "isl_drop_unused_params_templ.c"
12774 /* Drop all parameters not referenced by "bset".
12776 __isl_give isl_basic_set
*isl_basic_set_drop_unused_params(
12777 __isl_take isl_basic_set
*bset
)
12779 return bset_from_bmap(isl_basic_map_drop_unused_params(
12780 bset_to_bmap(bset
)));
12783 /* Given a tuple of identifiers "tuple" in a space that corresponds
12784 * to that of "set", if any of those identifiers appear as parameters
12785 * in "set", then equate those parameters with the corresponding
12786 * set dimensions and project out the parameters.
12787 * The result therefore has no such parameters.
12789 static __isl_give isl_set
*equate_params(__isl_take isl_set
*set
,
12790 __isl_keep isl_multi_id
*tuple
)
12794 isl_space
*set_space
, *tuple_space
;
12796 set_space
= isl_set_peek_space(set
);
12797 tuple_space
= isl_multi_id_peek_space(tuple
);
12798 if (isl_space_check_equal_tuples(tuple_space
, set_space
) < 0)
12799 return isl_set_free(set
);
12800 n
= isl_multi_id_size(tuple
);
12802 return isl_set_free(set
);
12803 for (i
= 0; i
< n
; ++i
) {
12807 id
= isl_multi_id_get_at(tuple
, i
);
12809 return isl_set_free(set
);
12810 pos
= isl_set_find_dim_by_id(set
, isl_dim_param
, id
);
12814 set
= isl_set_equate(set
, isl_dim_param
, pos
, isl_dim_set
, i
);
12815 set
= isl_set_project_out(set
, isl_dim_param
, pos
, 1);
12820 /* Bind the set dimensions of "set" to parameters with identifiers
12821 * specified by "tuple", living in the same space as "set".
12823 * If no parameters with these identifiers appear in "set" already,
12824 * then the set dimensions are simply reinterpreted as parameters.
12825 * Otherwise, the parameters are first equated to the corresponding
12828 __isl_give isl_set
*isl_set_bind(__isl_take isl_set
*set
,
12829 __isl_take isl_multi_id
*tuple
)
12833 set
= equate_params(set
, tuple
);
12834 space
= isl_set_get_space(set
);
12835 space
= isl_space_bind_set(space
, tuple
);
12836 isl_multi_id_free(tuple
);
12837 set
= isl_set_reset_space(set
, space
);
12842 /* Given a tuple of identifiers "tuple" in a space that corresponds
12843 * to the domain of "map", if any of those identifiers appear as parameters
12844 * in "map", then equate those parameters with the corresponding
12845 * input dimensions and project out the parameters.
12846 * The result therefore has no such parameters.
12848 static __isl_give isl_map
*map_equate_params(__isl_take isl_map
*map
,
12849 __isl_keep isl_multi_id
*tuple
)
12853 isl_space
*map_space
, *tuple_space
;
12855 map_space
= isl_map_peek_space(map
);
12856 tuple_space
= isl_multi_id_peek_space(tuple
);
12857 if (isl_space_check_domain_tuples(tuple_space
, map_space
) < 0)
12858 return isl_map_free(map
);
12859 n
= isl_multi_id_size(tuple
);
12861 return isl_map_free(map
);
12862 for (i
= 0; i
< n
; ++i
) {
12866 id
= isl_multi_id_get_at(tuple
, i
);
12868 return isl_map_free(map
);
12869 pos
= isl_map_find_dim_by_id(map
, isl_dim_param
, id
);
12873 map
= isl_map_equate(map
, isl_dim_param
, pos
, isl_dim_in
, i
);
12874 map
= isl_map_project_out(map
, isl_dim_param
, pos
, 1);
12879 /* Bind the input dimensions of "map" to parameters with identifiers
12880 * specified by "tuple", living in the domain space of "map".
12882 * If no parameters with these identifiers appear in "map" already,
12883 * then the input dimensions are simply reinterpreted as parameters.
12884 * Otherwise, the parameters are first equated to the corresponding
12885 * input dimensions.
12887 __isl_give isl_set
*isl_map_bind_domain(__isl_take isl_map
*map
,
12888 __isl_take isl_multi_id
*tuple
)
12893 map
= map_equate_params(map
, tuple
);
12894 space
= isl_map_get_space(map
);
12895 space
= isl_space_bind_map_domain(space
, tuple
);
12896 isl_multi_id_free(tuple
);
12897 set
= set_from_map(isl_map_reset_space(map
, space
));
12902 /* Bind the output dimensions of "map" to parameters with identifiers
12903 * specified by "tuple", living in the range space of "map".
12905 * Since binding is more easily implemented on the domain,
12906 * bind the input dimensions of the inverse of "map".
12908 __isl_give isl_set
*isl_map_bind_range(__isl_take isl_map
*map
,
12909 __isl_take isl_multi_id
*tuple
)
12911 return isl_map_bind_domain(isl_map_reverse(map
), tuple
);
12914 /* Insert a domain corresponding to "tuple"
12915 * into the nullary or unary relation "set".
12916 * The result has an extra initial tuple and is therefore
12917 * either a unary or binary relation.
12918 * Any parameters with identifiers in "tuple" are reinterpreted
12919 * as the corresponding domain dimensions.
12921 static __isl_give isl_map
*unbind_params_insert_domain(
12922 __isl_take isl_set
*set
, __isl_take isl_multi_id
*tuple
)
12927 space
= isl_set_peek_space(set
);
12928 r
= isl_reordering_unbind_params_insert_domain(space
, tuple
);
12929 isl_multi_id_free(tuple
);
12931 return isl_map_realign(set_to_map(set
), r
);
12934 /* Construct a set with "tuple" as domain from the parameter domain "set".
12935 * Any parameters with identifiers in "tuple" are reinterpreted
12936 * as the corresponding set dimensions.
12938 __isl_give isl_set
*isl_set_unbind_params(__isl_take isl_set
*set
,
12939 __isl_take isl_multi_id
*tuple
)
12941 isl_bool is_params
;
12943 is_params
= isl_set_is_params(set
);
12945 set
= isl_set_free(set
);
12946 else if (!is_params
)
12947 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
12948 "expecting parameter domain", set
= isl_set_free(set
));
12949 return set_from_map(unbind_params_insert_domain(set
, tuple
));
12952 /* Check that "set" is a proper set, i.e., that it is not a parameter domain.
12954 static isl_stat
isl_set_check_is_set(__isl_keep isl_set
*set
)
12956 isl_bool is_params
;
12958 is_params
= isl_set_is_params(set
);
12960 return isl_stat_error
;
12961 else if (is_params
)
12962 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
12963 "expecting proper set", return isl_stat_error
);
12965 return isl_stat_ok
;
12968 /* Construct a map with "domain" as domain and "set" as range.
12969 * Any parameters with identifiers in "domain" are reinterpreted
12970 * as the corresponding domain dimensions.
12972 __isl_give isl_map
*isl_set_unbind_params_insert_domain(
12973 __isl_take isl_set
*set
, __isl_take isl_multi_id
*domain
)
12975 if (isl_set_check_is_set(set
) < 0)
12976 set
= isl_set_free(set
);
12977 return unbind_params_insert_domain(set
, domain
);
12980 /* Construct a map with "domain" as domain and "set" as range.
12982 __isl_give isl_map
*isl_set_insert_domain(__isl_take isl_set
*set
,
12983 __isl_take isl_space
*domain
)
12989 if (isl_set_check_is_set(set
) < 0 || isl_space_check_is_set(domain
) < 0)
12990 domain
= isl_space_free(domain
);
12991 dim
= isl_space_dim(domain
, isl_dim_set
);
12993 domain
= isl_space_free(domain
);
12994 space
= isl_set_get_space(set
);
12995 domain
= isl_space_replace_params(domain
, space
);
12996 space
= isl_space_map_from_domain_and_range(domain
, space
);
12998 map
= isl_map_from_range(set
);
12999 map
= isl_map_add_dims(map
, isl_dim_in
, dim
);
13000 map
= isl_map_reset_space(map
, space
);
13005 __isl_give isl_mat
*isl_basic_map_equalities_matrix(
13006 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
13007 enum isl_dim_type c2
, enum isl_dim_type c3
,
13008 enum isl_dim_type c4
, enum isl_dim_type c5
)
13010 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
13011 struct isl_mat
*mat
;
13016 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
13019 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_eq
, total
+ 1);
13022 for (i
= 0; i
< bmap
->n_eq
; ++i
)
13023 for (j
= 0, pos
= 0; j
< 5; ++j
) {
13024 int off
= isl_basic_map_offset(bmap
, c
[j
]);
13025 isl_size dim
= isl_basic_map_dim(bmap
, c
[j
]);
13027 return isl_mat_free(mat
);
13028 for (k
= 0; k
< dim
; ++k
) {
13029 isl_int_set(mat
->row
[i
][pos
],
13030 bmap
->eq
[i
][off
+ k
]);
13038 __isl_give isl_mat
*isl_basic_map_inequalities_matrix(
13039 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
13040 enum isl_dim_type c2
, enum isl_dim_type c3
,
13041 enum isl_dim_type c4
, enum isl_dim_type c5
)
13043 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
13044 struct isl_mat
*mat
;
13049 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
13052 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_ineq
, total
+ 1);
13055 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
13056 for (j
= 0, pos
= 0; j
< 5; ++j
) {
13057 int off
= isl_basic_map_offset(bmap
, c
[j
]);
13058 isl_size dim
= isl_basic_map_dim(bmap
, c
[j
]);
13060 return isl_mat_free(mat
);
13061 for (k
= 0; k
< dim
; ++k
) {
13062 isl_int_set(mat
->row
[i
][pos
],
13063 bmap
->ineq
[i
][off
+ k
]);
13071 __isl_give isl_basic_map
*isl_basic_map_from_constraint_matrices(
13072 __isl_take isl_space
*space
,
13073 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
13074 enum isl_dim_type c2
, enum isl_dim_type c3
,
13075 enum isl_dim_type c4
, enum isl_dim_type c5
)
13077 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
13078 isl_basic_map
*bmap
= NULL
;
13085 dim
= isl_space_dim(space
, isl_dim_all
);
13086 if (dim
< 0 || !eq
|| !ineq
)
13089 if (eq
->n_col
!= ineq
->n_col
)
13090 isl_die(space
->ctx
, isl_error_invalid
,
13091 "equalities and inequalities matrices should have "
13092 "same number of columns", goto error
);
13096 if (eq
->n_col
< total
)
13097 isl_die(space
->ctx
, isl_error_invalid
,
13098 "number of columns too small", goto error
);
13100 extra
= eq
->n_col
- total
;
13102 bmap
= isl_basic_map_alloc_space(isl_space_copy(space
), extra
,
13103 eq
->n_row
, ineq
->n_row
);
13106 for (i
= 0; i
< extra
; ++i
) {
13107 k
= isl_basic_map_alloc_div(bmap
);
13110 isl_int_set_si(bmap
->div
[k
][0], 0);
13112 for (i
= 0; i
< eq
->n_row
; ++i
) {
13113 l
= isl_basic_map_alloc_equality(bmap
);
13116 for (j
= 0, pos
= 0; j
< 5; ++j
) {
13117 int off
= isl_basic_map_offset(bmap
, c
[j
]);
13118 isl_size dim
= isl_basic_map_dim(bmap
, c
[j
]);
13121 for (k
= 0; k
< dim
; ++k
) {
13122 isl_int_set(bmap
->eq
[l
][off
+ k
],
13128 for (i
= 0; i
< ineq
->n_row
; ++i
) {
13129 l
= isl_basic_map_alloc_inequality(bmap
);
13132 for (j
= 0, pos
= 0; j
< 5; ++j
) {
13133 int off
= isl_basic_map_offset(bmap
, c
[j
]);
13134 isl_size dim
= isl_basic_map_dim(bmap
, c
[j
]);
13137 for (k
= 0; k
< dim
; ++k
) {
13138 isl_int_set(bmap
->ineq
[l
][off
+ k
],
13139 ineq
->row
[i
][pos
]);
13145 isl_space_free(space
);
13147 isl_mat_free(ineq
);
13149 bmap
= isl_basic_map_simplify(bmap
);
13150 return isl_basic_map_finalize(bmap
);
13152 isl_space_free(space
);
13154 isl_mat_free(ineq
);
13155 isl_basic_map_free(bmap
);
13159 __isl_give isl_mat
*isl_basic_set_equalities_matrix(
13160 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
13161 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
13163 return isl_basic_map_equalities_matrix(bset_to_bmap(bset
),
13164 c1
, c2
, c3
, c4
, isl_dim_in
);
13167 __isl_give isl_mat
*isl_basic_set_inequalities_matrix(
13168 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
13169 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
13171 return isl_basic_map_inequalities_matrix(bset_to_bmap(bset
),
13172 c1
, c2
, c3
, c4
, isl_dim_in
);
13175 __isl_give isl_basic_set
*isl_basic_set_from_constraint_matrices(
13176 __isl_take isl_space
*space
,
13177 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
13178 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
13180 isl_basic_map
*bmap
;
13181 bmap
= isl_basic_map_from_constraint_matrices(space
, eq
, ineq
,
13182 c1
, c2
, c3
, c4
, isl_dim_in
);
13183 return bset_from_bmap(bmap
);
13186 isl_bool
isl_basic_map_can_zip(__isl_keep isl_basic_map
*bmap
)
13189 return isl_bool_error
;
13191 return isl_space_can_zip(bmap
->dim
);
13194 isl_bool
isl_map_can_zip(__isl_keep isl_map
*map
)
13197 return isl_bool_error
;
13199 return isl_space_can_zip(map
->dim
);
13202 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
13203 * (A -> C) -> (B -> D).
13205 __isl_give isl_basic_map
*isl_basic_map_zip(__isl_take isl_basic_map
*bmap
)
13215 if (!isl_basic_map_can_zip(bmap
))
13216 isl_die(bmap
->ctx
, isl_error_invalid
,
13217 "basic map cannot be zipped", goto error
);
13218 n_in
= isl_space_dim(bmap
->dim
->nested
[0], isl_dim_in
);
13219 n1
= isl_space_dim(bmap
->dim
->nested
[0], isl_dim_out
);
13220 n2
= isl_space_dim(bmap
->dim
->nested
[1], isl_dim_in
);
13221 if (n_in
< 0 || n1
< 0 || n2
< 0)
13222 return isl_basic_map_free(bmap
);
13223 pos
= isl_basic_map_offset(bmap
, isl_dim_in
) + n_in
;
13224 bmap
= isl_basic_map_cow(bmap
);
13225 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
13228 bmap
->dim
= isl_space_zip(bmap
->dim
);
13231 bmap
= isl_basic_map_mark_final(bmap
);
13234 isl_basic_map_free(bmap
);
13238 /* Given a map (A -> B) -> (C -> D), return the corresponding map
13239 * (A -> C) -> (B -> D).
13241 __isl_give isl_map
*isl_map_zip(__isl_take isl_map
*map
)
13246 if (!isl_map_can_zip(map
))
13247 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be zipped",
13250 return isl_map_transform(map
, &isl_space_zip
, &isl_basic_map_zip
);
13256 /* Can we apply isl_basic_map_curry to "bmap"?
13257 * That is, does it have a nested relation in its domain?
13259 isl_bool
isl_basic_map_can_curry(__isl_keep isl_basic_map
*bmap
)
13262 return isl_bool_error
;
13264 return isl_space_can_curry(bmap
->dim
);
13267 /* Can we apply isl_map_curry to "map"?
13268 * That is, does it have a nested relation in its domain?
13270 isl_bool
isl_map_can_curry(__isl_keep isl_map
*map
)
13273 return isl_bool_error
;
13275 return isl_space_can_curry(map
->dim
);
13278 /* Given a basic map (A -> B) -> C, return the corresponding basic map
13281 __isl_give isl_basic_map
*isl_basic_map_curry(__isl_take isl_basic_map
*bmap
)
13287 if (!isl_basic_map_can_curry(bmap
))
13288 isl_die(bmap
->ctx
, isl_error_invalid
,
13289 "basic map cannot be curried", goto error
);
13290 bmap
= isl_basic_map_cow(bmap
);
13293 bmap
->dim
= isl_space_curry(bmap
->dim
);
13296 bmap
= isl_basic_map_mark_final(bmap
);
13299 isl_basic_map_free(bmap
);
13303 /* Given a map (A -> B) -> C, return the corresponding map
13306 __isl_give isl_map
*isl_map_curry(__isl_take isl_map
*map
)
13308 return isl_map_change_space(map
, &isl_map_can_curry
,
13309 "map cannot be curried", &isl_space_curry
);
13312 /* Can isl_map_range_curry be applied to "map"?
13313 * That is, does it have a nested relation in its range,
13314 * the domain of which is itself a nested relation?
13316 isl_bool
isl_map_can_range_curry(__isl_keep isl_map
*map
)
13319 return isl_bool_error
;
13321 return isl_space_can_range_curry(map
->dim
);
13324 /* Given a map A -> ((B -> C) -> D), return the corresponding map
13325 * A -> (B -> (C -> D)).
13327 __isl_give isl_map
*isl_map_range_curry(__isl_take isl_map
*map
)
13329 return isl_map_change_space(map
, &isl_map_can_range_curry
,
13330 "map range cannot be curried",
13331 &isl_space_range_curry
);
13334 /* Can we apply isl_basic_map_uncurry to "bmap"?
13335 * That is, does it have a nested relation in its domain?
13337 isl_bool
isl_basic_map_can_uncurry(__isl_keep isl_basic_map
*bmap
)
13340 return isl_bool_error
;
13342 return isl_space_can_uncurry(bmap
->dim
);
13345 /* Can we apply isl_map_uncurry to "map"?
13346 * That is, does it have a nested relation in its domain?
13348 isl_bool
isl_map_can_uncurry(__isl_keep isl_map
*map
)
13351 return isl_bool_error
;
13353 return isl_space_can_uncurry(map
->dim
);
13356 /* Given a basic map A -> (B -> C), return the corresponding basic map
13359 __isl_give isl_basic_map
*isl_basic_map_uncurry(__isl_take isl_basic_map
*bmap
)
13365 if (!isl_basic_map_can_uncurry(bmap
))
13366 isl_die(bmap
->ctx
, isl_error_invalid
,
13367 "basic map cannot be uncurried",
13368 return isl_basic_map_free(bmap
));
13369 bmap
= isl_basic_map_cow(bmap
);
13372 bmap
->dim
= isl_space_uncurry(bmap
->dim
);
13374 return isl_basic_map_free(bmap
);
13375 bmap
= isl_basic_map_mark_final(bmap
);
13379 /* Given a map A -> (B -> C), return the corresponding map
13382 __isl_give isl_map
*isl_map_uncurry(__isl_take isl_map
*map
)
13384 return isl_map_change_space(map
, &isl_map_can_uncurry
,
13385 "map cannot be uncurried", &isl_space_uncurry
);
13388 __isl_give isl_set
*isl_set_equate(__isl_take isl_set
*set
,
13389 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13391 return isl_map_equate(set
, type1
, pos1
, type2
, pos2
);
13394 /* Construct a basic map where the given dimensions are equal to each other.
13396 static __isl_give isl_basic_map
*equator(__isl_take isl_space
*space
,
13397 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13399 isl_basic_map
*bmap
= NULL
;
13403 total
= isl_space_dim(space
, isl_dim_all
);
13405 isl_space_check_range(space
, type1
, pos1
, 1) < 0 ||
13406 isl_space_check_range(space
, type2
, pos2
, 1) < 0)
13409 if (type1
== type2
&& pos1
== pos2
)
13410 return isl_basic_map_universe(space
);
13412 bmap
= isl_basic_map_alloc_space(isl_space_copy(space
), 0, 1, 0);
13413 i
= isl_basic_map_alloc_equality(bmap
);
13416 isl_seq_clr(bmap
->eq
[i
], 1 + total
);
13417 pos1
+= isl_basic_map_offset(bmap
, type1
);
13418 pos2
+= isl_basic_map_offset(bmap
, type2
);
13419 isl_int_set_si(bmap
->eq
[i
][pos1
], -1);
13420 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
13421 bmap
= isl_basic_map_finalize(bmap
);
13422 isl_space_free(space
);
13425 isl_space_free(space
);
13426 isl_basic_map_free(bmap
);
13430 /* Add a constraint imposing that the given two dimensions are equal.
13432 __isl_give isl_basic_map
*isl_basic_map_equate(__isl_take isl_basic_map
*bmap
,
13433 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13437 eq
= equator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
13439 bmap
= isl_basic_map_intersect(bmap
, eq
);
13444 /* Add a constraint imposing that the given two dimensions are equal.
13446 __isl_give isl_map
*isl_map_equate(__isl_take isl_map
*map
,
13447 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13449 isl_basic_map
*bmap
;
13451 bmap
= equator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
13453 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
13458 /* Add a constraint imposing that the given two dimensions have opposite values.
13460 __isl_give isl_map
*isl_map_oppose(__isl_take isl_map
*map
,
13461 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13463 isl_basic_map
*bmap
= NULL
;
13467 if (isl_map_check_range(map
, type1
, pos1
, 1) < 0)
13468 return isl_map_free(map
);
13469 if (isl_map_check_range(map
, type2
, pos2
, 1) < 0)
13470 return isl_map_free(map
);
13472 total
= isl_map_dim(map
, isl_dim_all
);
13474 return isl_map_free(map
);
13475 bmap
= isl_basic_map_alloc_space(isl_map_get_space(map
), 0, 1, 0);
13476 i
= isl_basic_map_alloc_equality(bmap
);
13479 isl_seq_clr(bmap
->eq
[i
], 1 + total
);
13480 pos1
+= isl_basic_map_offset(bmap
, type1
);
13481 pos2
+= isl_basic_map_offset(bmap
, type2
);
13482 isl_int_set_si(bmap
->eq
[i
][pos1
], 1);
13483 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
13484 bmap
= isl_basic_map_finalize(bmap
);
13486 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
13490 isl_basic_map_free(bmap
);
13495 /* Construct a constraint imposing that the value of the first dimension is
13496 * greater than or equal to that of the second.
13498 static __isl_give isl_constraint
*constraint_order_ge(
13499 __isl_take isl_space
*space
, enum isl_dim_type type1
, int pos1
,
13500 enum isl_dim_type type2
, int pos2
)
13504 if (isl_space_check_range(space
, type1
, pos1
, 1) < 0 ||
13505 isl_space_check_range(space
, type2
, pos2
, 1) < 0)
13506 space
= isl_space_free(space
);
13510 c
= isl_constraint_alloc_inequality(isl_local_space_from_space(space
));
13512 if (type1
== type2
&& pos1
== pos2
)
13515 c
= isl_constraint_set_coefficient_si(c
, type1
, pos1
, 1);
13516 c
= isl_constraint_set_coefficient_si(c
, type2
, pos2
, -1);
13521 /* Add a constraint imposing that the value of the first dimension is
13522 * greater than or equal to that of the second.
13524 __isl_give isl_basic_map
*isl_basic_map_order_ge(__isl_take isl_basic_map
*bmap
,
13525 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13530 if (type1
== type2
&& pos1
== pos2
)
13532 space
= isl_basic_map_get_space(bmap
);
13533 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
13534 bmap
= isl_basic_map_add_constraint(bmap
, c
);
13539 /* Add a constraint imposing that the value of the first dimension is
13540 * greater than or equal to that of the second.
13542 __isl_give isl_map
*isl_map_order_ge(__isl_take isl_map
*map
,
13543 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13548 if (type1
== type2
&& pos1
== pos2
)
13550 space
= isl_map_get_space(map
);
13551 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
13552 map
= isl_map_add_constraint(map
, c
);
13557 /* Add a constraint imposing that the value of the first dimension is
13558 * less than or equal to that of the second.
13560 __isl_give isl_map
*isl_map_order_le(__isl_take isl_map
*map
,
13561 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13563 return isl_map_order_ge(map
, type2
, pos2
, type1
, pos1
);
13566 /* Construct a basic map where the value of the first dimension is
13567 * greater than that of the second.
13569 static __isl_give isl_basic_map
*greator(__isl_take isl_space
*space
,
13570 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13572 isl_basic_map
*bmap
= NULL
;
13576 if (isl_space_check_range(space
, type1
, pos1
, 1) < 0 ||
13577 isl_space_check_range(space
, type2
, pos2
, 1) < 0)
13580 if (type1
== type2
&& pos1
== pos2
)
13581 return isl_basic_map_empty(space
);
13583 bmap
= isl_basic_map_alloc_space(space
, 0, 0, 1);
13584 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
13585 i
= isl_basic_map_alloc_inequality(bmap
);
13586 if (total
< 0 || i
< 0)
13587 return isl_basic_map_free(bmap
);
13588 isl_seq_clr(bmap
->ineq
[i
], 1 + total
);
13589 pos1
+= isl_basic_map_offset(bmap
, type1
);
13590 pos2
+= isl_basic_map_offset(bmap
, type2
);
13591 isl_int_set_si(bmap
->ineq
[i
][pos1
], 1);
13592 isl_int_set_si(bmap
->ineq
[i
][pos2
], -1);
13593 isl_int_set_si(bmap
->ineq
[i
][0], -1);
13594 bmap
= isl_basic_map_finalize(bmap
);
13598 isl_space_free(space
);
13599 isl_basic_map_free(bmap
);
13603 /* Add a constraint imposing that the value of the first dimension is
13604 * greater than that of the second.
13606 __isl_give isl_basic_map
*isl_basic_map_order_gt(__isl_take isl_basic_map
*bmap
,
13607 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13611 gt
= greator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
13613 bmap
= isl_basic_map_intersect(bmap
, gt
);
13618 /* Add a constraint imposing that the value of the first dimension is
13619 * greater than that of the second.
13621 __isl_give isl_map
*isl_map_order_gt(__isl_take isl_map
*map
,
13622 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13624 isl_basic_map
*bmap
;
13626 bmap
= greator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
13628 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
13633 /* Add a constraint imposing that the value of the first dimension is
13634 * smaller than that of the second.
13636 __isl_give isl_map
*isl_map_order_lt(__isl_take isl_map
*map
,
13637 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13639 return isl_map_order_gt(map
, type2
, pos2
, type1
, pos1
);
13642 __isl_give isl_aff
*isl_basic_map_get_div(__isl_keep isl_basic_map
*bmap
,
13646 isl_local_space
*ls
;
13651 if (!isl_basic_map_divs_known(bmap
))
13652 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
13653 "some divs are unknown", return NULL
);
13655 ls
= isl_basic_map_get_local_space(bmap
);
13656 div
= isl_local_space_get_div(ls
, pos
);
13657 isl_local_space_free(ls
);
13662 __isl_give isl_aff
*isl_basic_set_get_div(__isl_keep isl_basic_set
*bset
,
13665 return isl_basic_map_get_div(bset
, pos
);
13668 /* Plug in "subs" for set dimension "pos" of "set".
13670 __isl_give isl_set
*isl_set_substitute(__isl_take isl_set
*set
,
13671 unsigned pos
, __isl_keep isl_aff
*subs
)
13675 if (set
&& isl_set_plain_is_empty(set
))
13678 ma
= isl_multi_aff_identity_on_domain_space(isl_set_get_space(set
));
13679 ma
= isl_multi_aff_set_aff(ma
, pos
, isl_aff_copy(subs
));
13680 return isl_set_preimage_multi_aff(set
, ma
);
13683 /* Check if the range of "ma" is compatible with the domain or range
13684 * (depending on "type") of "bmap".
13686 static isl_stat
check_basic_map_compatible_range_multi_aff(
13687 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type type
,
13688 __isl_keep isl_multi_aff
*ma
)
13691 isl_space
*ma_space
;
13693 ma_space
= isl_multi_aff_get_space(ma
);
13695 m
= isl_space_has_equal_params(bmap
->dim
, ma_space
);
13699 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
13700 "parameters don't match", goto error
);
13701 m
= isl_space_tuple_is_equal(bmap
->dim
, type
, ma_space
, isl_dim_out
);
13705 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
13706 "spaces don't match", goto error
);
13708 isl_space_free(ma_space
);
13709 return isl_stat_ok
;
13711 isl_space_free(ma_space
);
13712 return isl_stat_error
;
13715 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
13716 * coefficients before the transformed range of dimensions,
13717 * the "n_after" coefficients after the transformed range of dimensions
13718 * and the coefficients of the other divs in "bmap".
13720 static __isl_give isl_basic_map
*set_ma_divs(__isl_take isl_basic_map
*bmap
,
13721 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
, int n_div
)
13726 isl_local_space
*ls
;
13731 ls
= isl_aff_get_domain_local_space(ma
->u
.p
[0]);
13732 n_param
= isl_local_space_dim(ls
, isl_dim_param
);
13733 n_set
= isl_local_space_dim(ls
, isl_dim_set
);
13734 if (n_param
< 0 || n_set
< 0)
13735 return isl_basic_map_free(bmap
);
13737 for (i
= 0; i
< n_div
; ++i
) {
13738 int o_bmap
= 0, o_ls
= 0;
13740 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], 1 + 1 + n_param
);
13741 o_bmap
+= 1 + 1 + n_param
;
13742 o_ls
+= 1 + 1 + n_param
;
13743 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_before
);
13744 o_bmap
+= n_before
;
13745 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
13746 ls
->div
->row
[i
] + o_ls
, n_set
);
13749 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_after
);
13751 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
13752 ls
->div
->row
[i
] + o_ls
, n_div
);
13755 isl_seq_clr(bmap
->div
[i
] + o_bmap
, bmap
->n_div
- n_div
);
13756 bmap
= isl_basic_map_add_div_constraints(bmap
, i
);
13761 isl_local_space_free(ls
);
13764 isl_local_space_free(ls
);
13765 return isl_basic_map_free(bmap
);
13768 /* How many stride constraints does "ma" enforce?
13769 * That is, how many of the affine expressions have a denominator
13770 * different from one?
13772 static int multi_aff_strides(__isl_keep isl_multi_aff
*ma
)
13777 for (i
= 0; i
< ma
->n
; ++i
)
13778 if (!isl_int_is_one(ma
->u
.p
[i
]->v
->el
[0]))
13784 /* For each affine expression in ma of the form
13786 * x_i = (f_i y + h_i)/m_i
13788 * with m_i different from one, add a constraint to "bmap"
13791 * f_i y + h_i = m_i alpha_i
13793 * with alpha_i an additional existentially quantified variable.
13795 * The input variables of "ma" correspond to a subset of the variables
13796 * of "bmap". There are "n_before" variables in "bmap" before this
13797 * subset and "n_after" variables after this subset.
13798 * The integer divisions of the affine expressions in "ma" are assumed
13799 * to have been aligned. There are "n_div_ma" of them and
13800 * they appear first in "bmap", straight after the "n_after" variables.
13802 static __isl_give isl_basic_map
*add_ma_strides(
13803 __isl_take isl_basic_map
*bmap
, __isl_keep isl_multi_aff
*ma
,
13804 int n_before
, int n_after
, int n_div_ma
)
13812 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
13813 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
13814 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
13815 if (total
< 0 || n_param
< 0 || n_in
< 0)
13816 return isl_basic_map_free(bmap
);
13817 for (i
= 0; i
< ma
->n
; ++i
) {
13818 int o_bmap
= 0, o_ma
= 1;
13820 if (isl_int_is_one(ma
->u
.p
[i
]->v
->el
[0]))
13822 div
= isl_basic_map_alloc_div(bmap
);
13823 k
= isl_basic_map_alloc_equality(bmap
);
13824 if (div
< 0 || k
< 0)
13826 isl_int_set_si(bmap
->div
[div
][0], 0);
13827 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
13828 ma
->u
.p
[i
]->v
->el
+ o_ma
, 1 + n_param
);
13829 o_bmap
+= 1 + n_param
;
13830 o_ma
+= 1 + n_param
;
13831 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_before
);
13832 o_bmap
+= n_before
;
13833 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
13834 ma
->u
.p
[i
]->v
->el
+ o_ma
, n_in
);
13837 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_after
);
13839 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
13840 ma
->u
.p
[i
]->v
->el
+ o_ma
, n_div_ma
);
13841 o_bmap
+= n_div_ma
;
13843 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, 1 + total
- o_bmap
);
13844 isl_int_neg(bmap
->eq
[k
][1 + total
], ma
->u
.p
[i
]->v
->el
[0]);
13850 isl_basic_map_free(bmap
);
13854 /* Replace the domain or range space (depending on "type) of "space" by "set".
13856 static __isl_give isl_space
*isl_space_set(__isl_take isl_space
*space
,
13857 enum isl_dim_type type
, __isl_take isl_space
*set
)
13859 if (type
== isl_dim_in
) {
13860 space
= isl_space_range(space
);
13861 space
= isl_space_map_from_domain_and_range(set
, space
);
13863 space
= isl_space_domain(space
);
13864 space
= isl_space_map_from_domain_and_range(space
, set
);
13870 /* Compute the preimage of the domain or range (depending on "type")
13871 * of "bmap" under the function represented by "ma".
13872 * In other words, plug in "ma" in the domain or range of "bmap".
13873 * The result is a basic map that lives in the same space as "bmap"
13874 * except that the domain or range has been replaced by
13875 * the domain space of "ma".
13877 * If bmap is represented by
13879 * A(p) + S u + B x + T v + C(divs) >= 0,
13881 * where u and x are input and output dimensions if type == isl_dim_out
13882 * while x and v are input and output dimensions if type == isl_dim_in,
13883 * and ma is represented by
13885 * x = D(p) + F(y) + G(divs')
13887 * then the result is
13889 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
13891 * The divs in the input set are similarly adjusted.
13894 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
13898 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
13899 * B_i G(divs') + c_i(divs))/n_i)
13901 * If bmap is not a rational map and if F(y) involves any denominators
13903 * x_i = (f_i y + h_i)/m_i
13905 * then additional constraints are added to ensure that we only
13906 * map back integer points. That is we enforce
13908 * f_i y + h_i = m_i alpha_i
13910 * with alpha_i an additional existentially quantified variable.
13912 * We first copy over the divs from "ma".
13913 * Then we add the modified constraints and divs from "bmap".
13914 * Finally, we add the stride constraints, if needed.
13916 __isl_give isl_basic_map
*isl_basic_map_preimage_multi_aff(
13917 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
13918 __isl_take isl_multi_aff
*ma
)
13922 isl_basic_map
*res
= NULL
;
13923 isl_size n_before
, n_after
, n_div_bmap
, n_div_ma
;
13924 isl_int f
, c1
, c2
, g
;
13933 ma
= isl_multi_aff_align_divs(ma
);
13936 if (check_basic_map_compatible_range_multi_aff(bmap
, type
, ma
) < 0)
13939 if (type
== isl_dim_in
) {
13941 n_after
= isl_basic_map_dim(bmap
, isl_dim_out
);
13943 n_before
= isl_basic_map_dim(bmap
, isl_dim_in
);
13946 n_div_bmap
= isl_basic_map_dim(bmap
, isl_dim_div
);
13947 n_div_ma
= ma
->n
? isl_aff_dim(ma
->u
.p
[0], isl_dim_div
) : 0;
13948 if (n_before
< 0 || n_after
< 0 || n_div_bmap
< 0 || n_div_ma
< 0)
13951 space
= isl_multi_aff_get_domain_space(ma
);
13952 space
= isl_space_set(isl_basic_map_get_space(bmap
), type
, space
);
13953 rational
= isl_basic_map_is_rational(bmap
);
13954 strides
= rational
? 0 : multi_aff_strides(ma
);
13955 res
= isl_basic_map_alloc_space(space
, n_div_ma
+ n_div_bmap
+ strides
,
13956 bmap
->n_eq
+ strides
, bmap
->n_ineq
+ 2 * n_div_ma
);
13958 res
= isl_basic_map_set_rational(res
);
13960 for (i
= 0; i
< n_div_ma
+ n_div_bmap
; ++i
)
13961 if (isl_basic_map_alloc_div(res
) < 0)
13964 res
= set_ma_divs(res
, ma
, n_before
, n_after
, n_div_ma
);
13968 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
13969 k
= isl_basic_map_alloc_equality(res
);
13972 if (isl_seq_preimage(res
->eq
[k
], bmap
->eq
[i
], ma
, n_before
,
13973 n_after
, n_div_ma
, n_div_bmap
,
13974 f
, c1
, c2
, g
, 0) < 0)
13978 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
13979 k
= isl_basic_map_alloc_inequality(res
);
13982 if (isl_seq_preimage(res
->ineq
[k
], bmap
->ineq
[i
], ma
, n_before
,
13983 n_after
, n_div_ma
, n_div_bmap
,
13984 f
, c1
, c2
, g
, 0) < 0)
13988 for (i
= 0; i
< bmap
->n_div
; ++i
) {
13989 if (isl_int_is_zero(bmap
->div
[i
][0])) {
13990 isl_int_set_si(res
->div
[n_div_ma
+ i
][0], 0);
13993 if (isl_seq_preimage(res
->div
[n_div_ma
+ i
], bmap
->div
[i
], ma
,
13994 n_before
, n_after
, n_div_ma
, n_div_bmap
,
13995 f
, c1
, c2
, g
, 1) < 0)
14000 res
= add_ma_strides(res
, ma
, n_before
, n_after
, n_div_ma
);
14006 isl_basic_map_free(bmap
);
14007 isl_multi_aff_free(ma
);
14008 res
= isl_basic_map_simplify(res
);
14009 return isl_basic_map_finalize(res
);
14015 isl_basic_map_free(bmap
);
14016 isl_multi_aff_free(ma
);
14017 isl_basic_map_free(res
);
14021 /* Compute the preimage of "bset" under the function represented by "ma".
14022 * In other words, plug in "ma" in "bset". The result is a basic set
14023 * that lives in the domain space of "ma".
14025 __isl_give isl_basic_set
*isl_basic_set_preimage_multi_aff(
14026 __isl_take isl_basic_set
*bset
, __isl_take isl_multi_aff
*ma
)
14028 return isl_basic_map_preimage_multi_aff(bset
, isl_dim_set
, ma
);
14031 /* Compute the preimage of the domain of "bmap" under the function
14032 * represented by "ma".
14033 * In other words, plug in "ma" in the domain of "bmap".
14034 * The result is a basic map that lives in the same space as "bmap"
14035 * except that the domain has been replaced by the domain space of "ma".
14037 __isl_give isl_basic_map
*isl_basic_map_preimage_domain_multi_aff(
14038 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
14040 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_in
, ma
);
14043 /* Compute the preimage of the range of "bmap" under the function
14044 * represented by "ma".
14045 * In other words, plug in "ma" in the range of "bmap".
14046 * The result is a basic map that lives in the same space as "bmap"
14047 * except that the range has been replaced by the domain space of "ma".
14049 __isl_give isl_basic_map
*isl_basic_map_preimage_range_multi_aff(
14050 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
14052 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_out
, ma
);
14055 /* Check if the range of "ma" is compatible with the domain or range
14056 * (depending on "type") of "map".
14057 * Return isl_stat_error if anything is wrong.
14059 static isl_stat
check_map_compatible_range_multi_aff(
14060 __isl_keep isl_map
*map
, enum isl_dim_type type
,
14061 __isl_keep isl_multi_aff
*ma
)
14064 isl_space
*ma_space
;
14066 ma_space
= isl_multi_aff_get_space(ma
);
14067 m
= isl_map_space_tuple_is_equal(map
, type
, ma_space
, isl_dim_out
);
14068 isl_space_free(ma_space
);
14070 return isl_stat_error
;
14072 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
14073 "spaces don't match", return isl_stat_error
);
14074 return isl_stat_ok
;
14077 /* Compute the preimage of the domain or range (depending on "type")
14078 * of "map" under the function represented by "ma".
14079 * In other words, plug in "ma" in the domain or range of "map".
14080 * The result is a map that lives in the same space as "map"
14081 * except that the domain or range has been replaced by
14082 * the domain space of "ma".
14084 * The parameters are assumed to have been aligned.
14086 static __isl_give isl_map
*map_preimage_multi_aff(__isl_take isl_map
*map
,
14087 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
14092 map
= isl_map_cow(map
);
14093 ma
= isl_multi_aff_align_divs(ma
);
14096 if (check_map_compatible_range_multi_aff(map
, type
, ma
) < 0)
14099 for (i
= 0; i
< map
->n
; ++i
) {
14100 map
->p
[i
] = isl_basic_map_preimage_multi_aff(map
->p
[i
], type
,
14101 isl_multi_aff_copy(ma
));
14106 space
= isl_multi_aff_get_domain_space(ma
);
14107 space
= isl_space_set(isl_map_get_space(map
), type
, space
);
14109 isl_space_free(isl_map_take_space(map
));
14110 map
= isl_map_restore_space(map
, space
);
14114 isl_multi_aff_free(ma
);
14116 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
14117 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
14120 isl_multi_aff_free(ma
);
14125 /* Compute the preimage of the domain or range (depending on "type")
14126 * of "map" under the function represented by "ma".
14127 * In other words, plug in "ma" in the domain or range of "map".
14128 * The result is a map that lives in the same space as "map"
14129 * except that the domain or range has been replaced by
14130 * the domain space of "ma".
14132 __isl_give isl_map
*isl_map_preimage_multi_aff(__isl_take isl_map
*map
,
14133 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
14140 aligned
= isl_map_space_has_equal_params(map
, ma
->space
);
14144 return map_preimage_multi_aff(map
, type
, ma
);
14146 if (isl_map_check_named_params(map
) < 0)
14148 if (!isl_space_has_named_params(ma
->space
))
14149 isl_die(map
->ctx
, isl_error_invalid
,
14150 "unaligned unnamed parameters", goto error
);
14151 map
= isl_map_align_params(map
, isl_multi_aff_get_space(ma
));
14152 ma
= isl_multi_aff_align_params(ma
, isl_map_get_space(map
));
14154 return map_preimage_multi_aff(map
, type
, ma
);
14156 isl_multi_aff_free(ma
);
14157 return isl_map_free(map
);
14160 /* Compute the preimage of "set" under the function represented by "ma".
14161 * In other words, plug in "ma" in "set". The result is a set
14162 * that lives in the domain space of "ma".
14164 __isl_give isl_set
*isl_set_preimage_multi_aff(__isl_take isl_set
*set
,
14165 __isl_take isl_multi_aff
*ma
)
14167 return isl_map_preimage_multi_aff(set
, isl_dim_set
, ma
);
14170 /* Compute the preimage of the domain of "map" under the function
14171 * represented by "ma".
14172 * In other words, plug in "ma" in the domain of "map".
14173 * The result is a map that lives in the same space as "map"
14174 * except that the domain has been replaced by the domain space of "ma".
14176 __isl_give isl_map
*isl_map_preimage_domain_multi_aff(__isl_take isl_map
*map
,
14177 __isl_take isl_multi_aff
*ma
)
14179 return isl_map_preimage_multi_aff(map
, isl_dim_in
, ma
);
14182 /* Compute the preimage of the range of "map" under the function
14183 * represented by "ma".
14184 * In other words, plug in "ma" in the range of "map".
14185 * The result is a map that lives in the same space as "map"
14186 * except that the range has been replaced by the domain space of "ma".
14188 __isl_give isl_map
*isl_map_preimage_range_multi_aff(__isl_take isl_map
*map
,
14189 __isl_take isl_multi_aff
*ma
)
14191 return isl_map_preimage_multi_aff(map
, isl_dim_out
, ma
);
14194 /* Compute the preimage of "map" under the function represented by "pma".
14195 * In other words, plug in "pma" in the domain or range of "map".
14196 * The result is a map that lives in the same space as "map",
14197 * except that the space of type "type" has been replaced by
14198 * the domain space of "pma".
14200 * The parameters of "map" and "pma" are assumed to have been aligned.
14202 static __isl_give isl_map
*isl_map_preimage_pw_multi_aff_aligned(
14203 __isl_take isl_map
*map
, enum isl_dim_type type
,
14204 __isl_take isl_pw_multi_aff
*pma
)
14215 space
= isl_pw_multi_aff_get_domain_space(pma
);
14216 isl_pw_multi_aff_free(pma
);
14217 space
= isl_space_set(isl_map_get_space(map
), type
, space
);
14219 return isl_map_empty(space
);
14222 res
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
14223 isl_multi_aff_copy(pma
->p
[0].maff
));
14224 if (type
== isl_dim_in
)
14225 res
= isl_map_intersect_domain(res
,
14226 isl_map_copy(pma
->p
[0].set
));
14228 res
= isl_map_intersect_range(res
,
14229 isl_map_copy(pma
->p
[0].set
));
14231 for (i
= 1; i
< pma
->n
; ++i
) {
14234 res_i
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
14235 isl_multi_aff_copy(pma
->p
[i
].maff
));
14236 if (type
== isl_dim_in
)
14237 res_i
= isl_map_intersect_domain(res_i
,
14238 isl_map_copy(pma
->p
[i
].set
));
14240 res_i
= isl_map_intersect_range(res_i
,
14241 isl_map_copy(pma
->p
[i
].set
));
14242 res
= isl_map_union(res
, res_i
);
14245 isl_pw_multi_aff_free(pma
);
14249 isl_pw_multi_aff_free(pma
);
14254 /* Compute the preimage of "map" under the function represented by "pma".
14255 * In other words, plug in "pma" in the domain or range of "map".
14256 * The result is a map that lives in the same space as "map",
14257 * except that the space of type "type" has been replaced by
14258 * the domain space of "pma".
14260 __isl_give isl_map
*isl_map_preimage_pw_multi_aff(__isl_take isl_map
*map
,
14261 enum isl_dim_type type
, __isl_take isl_pw_multi_aff
*pma
)
14268 aligned
= isl_map_space_has_equal_params(map
, pma
->dim
);
14272 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
14274 if (isl_map_check_named_params(map
) < 0)
14276 if (isl_pw_multi_aff_check_named_params(pma
) < 0)
14278 map
= isl_map_align_params(map
, isl_pw_multi_aff_get_space(pma
));
14279 pma
= isl_pw_multi_aff_align_params(pma
, isl_map_get_space(map
));
14281 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
14283 isl_pw_multi_aff_free(pma
);
14284 return isl_map_free(map
);
14287 /* Compute the preimage of "set" under the function represented by "pma".
14288 * In other words, plug in "pma" in "set". The result is a set
14289 * that lives in the domain space of "pma".
14291 __isl_give isl_set
*isl_set_preimage_pw_multi_aff(__isl_take isl_set
*set
,
14292 __isl_take isl_pw_multi_aff
*pma
)
14294 return isl_map_preimage_pw_multi_aff(set
, isl_dim_set
, pma
);
14297 /* Compute the preimage of the domain of "map" under the function
14298 * represented by "pma".
14299 * In other words, plug in "pma" in the domain of "map".
14300 * The result is a map that lives in the same space as "map",
14301 * except that domain space has been replaced by the domain space of "pma".
14303 __isl_give isl_map
*isl_map_preimage_domain_pw_multi_aff(
14304 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
14306 return isl_map_preimage_pw_multi_aff(map
, isl_dim_in
, pma
);
14309 /* Compute the preimage of the range of "map" under the function
14310 * represented by "pma".
14311 * In other words, plug in "pma" in the range of "map".
14312 * The result is a map that lives in the same space as "map",
14313 * except that range space has been replaced by the domain space of "pma".
14315 __isl_give isl_map
*isl_map_preimage_range_pw_multi_aff(
14316 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
14318 return isl_map_preimage_pw_multi_aff(map
, isl_dim_out
, pma
);
14321 /* Compute the preimage of "map" under the function represented by "mpa".
14322 * In other words, plug in "mpa" in the domain or range of "map".
14323 * The result is a map that lives in the same space as "map",
14324 * except that the space of type "type" has been replaced by
14325 * the domain space of "mpa".
14327 * If the map does not involve any constraints that refer to the
14328 * dimensions of the substituted space, then the only possible
14329 * effect of "mpa" on the map is to map the space to a different space.
14330 * We create a separate isl_multi_aff to effectuate this change
14331 * in order to avoid spurious splitting of the map along the pieces
14333 * If "mpa" has a non-trivial explicit domain, however,
14334 * then the full substitution should be performed.
14336 __isl_give isl_map
*isl_map_preimage_multi_pw_aff(__isl_take isl_map
*map
,
14337 enum isl_dim_type type
, __isl_take isl_multi_pw_aff
*mpa
)
14341 isl_pw_multi_aff
*pma
;
14343 n
= isl_map_dim(map
, type
);
14347 full
= isl_map_involves_dims(map
, type
, 0, n
);
14348 if (full
>= 0 && !full
)
14349 full
= isl_multi_pw_aff_has_non_trivial_domain(mpa
);
14356 space
= isl_multi_pw_aff_get_space(mpa
);
14357 isl_multi_pw_aff_free(mpa
);
14358 ma
= isl_multi_aff_zero(space
);
14359 return isl_map_preimage_multi_aff(map
, type
, ma
);
14362 pma
= isl_pw_multi_aff_from_multi_pw_aff(mpa
);
14363 return isl_map_preimage_pw_multi_aff(map
, type
, pma
);
14366 isl_multi_pw_aff_free(mpa
);
14370 /* Compute the preimage of "map" under the function represented by "mpa".
14371 * In other words, plug in "mpa" in the domain "map".
14372 * The result is a map that lives in the same space as "map",
14373 * except that domain space has been replaced by the domain space of "mpa".
14375 __isl_give isl_map
*isl_map_preimage_domain_multi_pw_aff(
14376 __isl_take isl_map
*map
, __isl_take isl_multi_pw_aff
*mpa
)
14378 return isl_map_preimage_multi_pw_aff(map
, isl_dim_in
, mpa
);
14381 /* Compute the preimage of "set" by the function represented by "mpa".
14382 * In other words, plug in "mpa" in "set".
14384 __isl_give isl_set
*isl_set_preimage_multi_pw_aff(__isl_take isl_set
*set
,
14385 __isl_take isl_multi_pw_aff
*mpa
)
14387 return isl_map_preimage_multi_pw_aff(set
, isl_dim_set
, mpa
);
14390 /* Given that inequality "ineq" of "bmap" expresses an upper bound
14391 * on the output dimension "pos" in terms of the parameters,
14392 * the input dimensions and possibly some integer divisions,
14393 * but not any other output dimensions, extract this upper bound
14394 * as a function of all dimensions (with zero coefficients
14395 * for the output dimensions).
14397 * That is, the inequality is of the form
14399 * e(...) + c - m x >= 0
14401 * where e does not depend on any other output dimensions.
14402 * Return (e(...) + c) / m, with the denominator m in the first position.
14404 __isl_give isl_vec
*isl_basic_map_inequality_extract_output_upper_bound(
14405 __isl_keep isl_basic_map
*bmap
, int ineq
, int pos
)
14408 isl_size v_out
, total
;
14410 v_out
= isl_basic_map_var_offset(bmap
, isl_dim_out
);
14411 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
14412 if (v_out
< 0 || total
< 0)
14414 ctx
= isl_basic_map_get_ctx(bmap
);
14415 return extract_bound_from_constraint(ctx
, bmap
->ineq
[ineq
],
14416 total
, v_out
+ pos
);
14419 /* Is constraint "c" of "bmap" of the form
14421 * e(...) + c1 - m x >= 0
14425 * -e(...) + c2 + m x >= 0
14427 * where m > 1 and e does not involve any other output variables?
14429 * "v_out" is the offset to the output variables.
14430 * "d" is the position of x among the output variables.
14431 * "v_div" is the offset to the local variables.
14432 * "total" is the total number of variables.
14434 * Since the purpose of this function is to use the constraint
14435 * to express the output variable as an integer division,
14436 * do not allow the constraint to involve any local variables
14437 * that do not have an explicit representation.
14439 static isl_bool
is_potential_div_constraint(__isl_keep isl_basic_map
*bmap
,
14440 isl_int
*c
, int v_out
, int d
, int v_div
, int total
)
14444 if (isl_int_is_zero(c
[1 + v_out
+ d
]))
14445 return isl_bool_false
;
14446 if (isl_int_is_one(c
[1 + v_out
+ d
]))
14447 return isl_bool_false
;
14448 if (isl_int_is_negone(c
[1 + v_out
+ d
]))
14449 return isl_bool_false
;
14450 if (isl_seq_first_non_zero(c
+ 1 + v_out
, d
) != -1)
14451 return isl_bool_false
;
14452 if (isl_seq_first_non_zero(c
+ 1 + v_out
+ d
+ 1,
14453 v_div
- (v_out
+ d
+ 1)) != -1)
14454 return isl_bool_false
;
14455 for (i
= 0; v_div
+ i
< total
; ++i
) {
14456 isl_bool known
, involves
;
14458 if (isl_int_is_zero(c
[1 + v_div
+ i
]))
14460 known
= isl_basic_map_div_is_known(bmap
, i
);
14461 if (known
< 0 || !known
)
14463 involves
= div_involves_vars(bmap
, i
, v_out
, v_div
- v_out
);
14464 if (involves
< 0 || involves
)
14465 return isl_bool_not(involves
);
14467 return isl_bool_true
;
14470 /* Look for a pair of constraints
14472 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
14476 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
14478 * that express that the output dimension x at position "pos"
14479 * is some integer division of an expression in terms of the parameters,
14480 * input dimensions and integer divisions.
14481 * If such a pair can be found, then return the index
14482 * of the upper bound constraint, m x <= e(...) + c1.
14483 * Otherwise, return an index beyond the number of constraints.
14485 * In order for the constraints above to express an integer division,
14486 * m needs to be greater than 1 and such that
14488 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
14490 * In particular, this ensures that
14492 * x = floor((e(...) + c1) / m)
14494 isl_size
isl_basic_map_find_output_upper_div_constraint(
14495 __isl_keep isl_basic_map
*bmap
, int pos
)
14499 isl_size v_out
, v_div
;
14503 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
14504 v_out
= isl_basic_map_var_offset(bmap
, isl_dim_out
);
14505 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
14506 n_ineq
= isl_basic_map_n_inequality(bmap
);
14507 if (total
< 0 || v_out
< 0 || v_div
< 0 || n_ineq
< 0)
14508 return isl_size_error
;
14511 for (i
= 0; i
< n_ineq
; ++i
) {
14512 isl_bool potential
;
14514 potential
= is_potential_div_constraint(bmap
, bmap
->ineq
[i
],
14515 v_out
, pos
, v_div
, total
);
14520 for (j
= i
+ 1; j
< n_ineq
; ++j
) {
14521 if (!isl_seq_is_neg(bmap
->ineq
[i
] + 1,
14522 bmap
->ineq
[j
] + 1, total
))
14524 isl_int_add(sum
, bmap
->ineq
[i
][0], bmap
->ineq
[j
][0]);
14525 if (isl_int_abs_lt(sum
, bmap
->ineq
[i
][1 + v_out
+ pos
]))
14531 isl_int_clear(sum
);
14535 if (isl_int_is_pos(bmap
->ineq
[j
][1 + v_out
+ pos
]))
14540 isl_int_clear(sum
);
14541 return isl_size_error
;
14544 /* Return a copy of the equality constraints of "bset" as a matrix.
14546 __isl_give isl_mat
*isl_basic_set_extract_equalities(
14547 __isl_keep isl_basic_set
*bset
)
14552 total
= isl_basic_set_dim(bset
, isl_dim_all
);
14556 ctx
= isl_basic_set_get_ctx(bset
);
14557 return isl_mat_sub_alloc6(ctx
, bset
->eq
, 0, bset
->n_eq
, 0, 1 + total
);
14560 /* Are the "n" "coefficients" starting at "first" of the integer division
14561 * expressions at position "pos1" in "bmap1" and "pos2" in "bmap2" equal
14563 * The "coefficient" at position 0 is the denominator.
14564 * The "coefficient" at position 1 is the constant term.
14566 isl_bool
isl_basic_map_equal_div_expr_part(__isl_keep isl_basic_map
*bmap1
,
14567 int pos1
, __isl_keep isl_basic_map
*bmap2
, int pos2
,
14568 unsigned first
, unsigned n
)
14570 if (isl_basic_map_check_range(bmap1
, isl_dim_div
, pos1
, 1) < 0)
14571 return isl_bool_error
;
14572 if (isl_basic_map_check_range(bmap2
, isl_dim_div
, pos2
, 1) < 0)
14573 return isl_bool_error
;
14574 return isl_seq_eq(bmap1
->div
[pos1
] + first
,
14575 bmap2
->div
[pos2
] + first
, n
);
14578 /* Are the integer division expressions at position "pos1" in "bmap1" and
14579 * "pos2" in "bmap2" equal to each other, except that the constant terms
14582 isl_bool
isl_basic_map_equal_div_expr_except_constant(
14583 __isl_keep isl_basic_map
*bmap1
, int pos1
,
14584 __isl_keep isl_basic_map
*bmap2
, int pos2
)
14587 isl_size total
, total2
;
14589 total
= isl_basic_map_dim(bmap1
, isl_dim_all
);
14590 total2
= isl_basic_map_dim(bmap2
, isl_dim_all
);
14591 if (total
< 0 || total2
< 0)
14592 return isl_bool_error
;
14593 if (total
!= total2
)
14594 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
14595 "incomparable div expressions", return isl_bool_error
);
14596 equal
= isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
14598 if (equal
< 0 || !equal
)
14600 equal
= isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
14602 if (equal
< 0 || equal
)
14603 return isl_bool_not(equal
);
14604 return isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
14608 /* Replace the numerator of the constant term of the integer division
14609 * expression at position "div" in "bmap" by "value".
14610 * The caller guarantees that this does not change the meaning
14613 __isl_give isl_basic_map
*isl_basic_map_set_div_expr_constant_num_si_inplace(
14614 __isl_take isl_basic_map
*bmap
, int div
, int value
)
14616 if (isl_basic_map_check_range(bmap
, isl_dim_div
, div
, 1) < 0)
14617 return isl_basic_map_free(bmap
);
14619 isl_int_set_si(bmap
->div
[div
][1], value
);
14624 /* Is the point "inner" internal to inequality constraint "ineq"
14626 * The point is considered to be internal to the inequality constraint,
14627 * if it strictly lies on the positive side of the inequality constraint,
14628 * or if it lies on the constraint and the constraint is lexico-positive.
14630 static isl_bool
is_internal(__isl_keep isl_vec
*inner
,
14631 __isl_keep isl_basic_set
*bset
, int ineq
)
14637 if (!inner
|| !bset
)
14638 return isl_bool_error
;
14640 ctx
= isl_basic_set_get_ctx(bset
);
14641 isl_seq_inner_product(inner
->el
, bset
->ineq
[ineq
], inner
->size
,
14642 &ctx
->normalize_gcd
);
14643 if (!isl_int_is_zero(ctx
->normalize_gcd
))
14644 return isl_int_is_nonneg(ctx
->normalize_gcd
);
14646 total
= isl_basic_set_dim(bset
, isl_dim_all
);
14648 return isl_bool_error
;
14649 pos
= isl_seq_first_non_zero(bset
->ineq
[ineq
] + 1, total
);
14650 return isl_int_is_pos(bset
->ineq
[ineq
][1 + pos
]);
14653 /* Tighten the inequality constraints of "bset" that are outward with respect
14654 * to the point "vec".
14655 * That is, tighten the constraints that are not satisfied by "vec".
14657 * "vec" is a point internal to some superset S of "bset" that is used
14658 * to make the subsets of S disjoint, by tightening one half of the constraints
14659 * that separate two subsets. In particular, the constraints of S
14660 * are all satisfied by "vec" and should not be tightened.
14661 * Of the internal constraints, those that have "vec" on the outside
14662 * are tightened. The shared facet is included in the adjacent subset
14663 * with the opposite constraint.
14664 * For constraints that saturate "vec", this criterion cannot be used
14665 * to determine which of the two sides should be tightened.
14666 * Instead, the sign of the first non-zero coefficient is used
14667 * to make this choice. Note that this second criterion is never used
14668 * on the constraints of S since "vec" is interior to "S".
14670 __isl_give isl_basic_set
*isl_basic_set_tighten_outward(
14671 __isl_take isl_basic_set
*bset
, __isl_keep isl_vec
*vec
)
14675 bset
= isl_basic_set_cow(bset
);
14678 for (j
= 0; j
< bset
->n_ineq
; ++j
) {
14681 internal
= is_internal(vec
, bset
, j
);
14683 return isl_basic_set_free(bset
);
14686 isl_int_sub_ui(bset
->ineq
[j
][0], bset
->ineq
[j
][0], 1);
14692 /* Replace the variables x of type "type" starting at "first" in "bmap"
14693 * by x' with x = M x' with M the matrix trans.
14694 * That is, replace the corresponding coefficients c by c M.
14696 * The transformation matrix should be a square matrix.
14698 __isl_give isl_basic_map
*isl_basic_map_transform_dims(
14699 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
, unsigned first
,
14700 __isl_take isl_mat
*trans
)
14704 bmap
= isl_basic_map_cow(bmap
);
14705 if (!bmap
|| !trans
)
14708 if (trans
->n_row
!= trans
->n_col
)
14709 isl_die(trans
->ctx
, isl_error_invalid
,
14710 "expecting square transformation matrix", goto error
);
14711 if (isl_basic_map_check_range(bmap
, type
, first
, trans
->n_row
) < 0)
14714 pos
= isl_basic_map_offset(bmap
, type
) + first
;
14716 if (isl_mat_sub_transform(bmap
->eq
, bmap
->n_eq
, pos
,
14717 isl_mat_copy(trans
)) < 0)
14719 if (isl_mat_sub_transform(bmap
->ineq
, bmap
->n_ineq
, pos
,
14720 isl_mat_copy(trans
)) < 0)
14722 if (isl_mat_sub_transform(bmap
->div
, bmap
->n_div
, 1 + pos
,
14723 isl_mat_copy(trans
)) < 0)
14726 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
14727 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
14728 ISL_F_CLR(bmap
, ISL_BASIC_MAP_REDUCED_COEFFICIENTS
);
14730 isl_mat_free(trans
);
14733 isl_mat_free(trans
);
14734 isl_basic_map_free(bmap
);
14738 /* Replace the variables x of type "type" starting at "first" in "bset"
14739 * by x' with x = M x' with M the matrix trans.
14740 * That is, replace the corresponding coefficients c by c M.
14742 * The transformation matrix should be a square matrix.
14744 __isl_give isl_basic_set
*isl_basic_set_transform_dims(
14745 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned first
,
14746 __isl_take isl_mat
*trans
)
14748 return isl_basic_map_transform_dims(bset
, type
, first
, trans
);