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_seq_clr(bmap
->ineq
[bmap
->n_ineq
] + 1 + total
,
1756 bmap
->extra
- bmap
->n_div
);
1757 return bmap
->n_ineq
++;
1760 int isl_basic_set_alloc_inequality(__isl_keep isl_basic_set
*bset
)
1762 return isl_basic_map_alloc_inequality(bset_to_bmap(bset
));
1765 __isl_give isl_basic_map
*isl_basic_map_free_inequality(
1766 __isl_take isl_basic_map
*bmap
, unsigned n
)
1770 if (n
> bmap
->n_ineq
)
1771 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
1772 "invalid number of inequalities",
1773 return isl_basic_map_free(bmap
));
1778 __isl_give isl_basic_set
*isl_basic_set_free_inequality(
1779 __isl_take isl_basic_set
*bset
, unsigned n
)
1781 return bset_from_bmap(isl_basic_map_free_inequality(bset_to_bmap(bset
),
1785 int isl_basic_map_drop_inequality(__isl_keep isl_basic_map
*bmap
, unsigned pos
)
1790 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
1792 if (pos
!= bmap
->n_ineq
- 1) {
1793 t
= bmap
->ineq
[pos
];
1794 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1795 bmap
->ineq
[bmap
->n_ineq
- 1] = t
;
1796 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
1802 int isl_basic_set_drop_inequality(__isl_keep isl_basic_set
*bset
, unsigned pos
)
1804 return isl_basic_map_drop_inequality(bset_to_bmap(bset
), pos
);
1807 __isl_give isl_basic_map
*isl_basic_map_add_eq(__isl_take isl_basic_map
*bmap
,
1814 empty
= isl_basic_map_plain_is_empty(bmap
);
1816 return isl_basic_map_free(bmap
);
1820 bmap
= isl_basic_map_cow(bmap
);
1821 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
1822 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
1824 return isl_basic_map_free(bmap
);
1825 k
= isl_basic_map_alloc_equality(bmap
);
1828 isl_seq_cpy(bmap
->eq
[k
], eq
, 1 + total
);
1831 isl_basic_map_free(bmap
);
1835 __isl_give isl_basic_set
*isl_basic_set_add_eq(__isl_take isl_basic_set
*bset
,
1838 return bset_from_bmap(isl_basic_map_add_eq(bset_to_bmap(bset
), eq
));
1841 __isl_give isl_basic_map
*isl_basic_map_add_ineq(__isl_take isl_basic_map
*bmap
,
1847 bmap
= isl_basic_map_cow(bmap
);
1848 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
1849 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
1851 return isl_basic_map_free(bmap
);
1852 k
= isl_basic_map_alloc_inequality(bmap
);
1855 isl_seq_cpy(bmap
->ineq
[k
], ineq
, 1 + total
);
1858 isl_basic_map_free(bmap
);
1862 __isl_give isl_basic_set
*isl_basic_set_add_ineq(__isl_take isl_basic_set
*bset
,
1865 return bset_from_bmap(isl_basic_map_add_ineq(bset_to_bmap(bset
), ineq
));
1868 int isl_basic_map_alloc_div(__isl_keep isl_basic_map
*bmap
)
1872 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
1875 isl_assert(bmap
->ctx
, bmap
->n_div
< bmap
->extra
, return -1);
1876 isl_seq_clr(bmap
->div
[bmap
->n_div
] + 1 + 1 + total
,
1877 bmap
->extra
- bmap
->n_div
);
1878 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1879 return bmap
->n_div
++;
1882 int isl_basic_set_alloc_div(__isl_keep isl_basic_set
*bset
)
1884 return isl_basic_map_alloc_div(bset_to_bmap(bset
));
1888 #define TYPE isl_basic_map
1889 #include "check_type_range_templ.c"
1891 /* Check that there are "n" dimensions of type "type" starting at "first"
1894 isl_stat
isl_basic_set_check_range(__isl_keep isl_basic_set
*bset
,
1895 enum isl_dim_type type
, unsigned first
, unsigned n
)
1897 return isl_basic_map_check_range(bset_to_bmap(bset
),
1901 /* Insert an extra integer division, prescribed by "div", to "bmap"
1902 * at (integer division) position "pos".
1904 * The integer division is first added at the end and then moved
1905 * into the right position.
1907 __isl_give isl_basic_map
*isl_basic_map_insert_div(
1908 __isl_take isl_basic_map
*bmap
, int pos
, __isl_keep isl_vec
*div
)
1913 bmap
= isl_basic_map_cow(bmap
);
1914 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
1915 if (total
< 0 || !div
)
1916 return isl_basic_map_free(bmap
);
1918 if (div
->size
!= 1 + 1 + total
)
1919 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
1920 "unexpected size", return isl_basic_map_free(bmap
));
1921 if (isl_basic_map_check_range(bmap
, isl_dim_div
, pos
, 0) < 0)
1922 return isl_basic_map_free(bmap
);
1924 bmap
= isl_basic_map_extend(bmap
, 1, 0, 2);
1925 k
= isl_basic_map_alloc_div(bmap
);
1927 return isl_basic_map_free(bmap
);
1928 isl_seq_cpy(bmap
->div
[k
], div
->el
, div
->size
);
1929 isl_int_set_si(bmap
->div
[k
][div
->size
], 0);
1931 for (i
= k
; i
> pos
; --i
)
1932 bmap
= isl_basic_map_swap_div(bmap
, i
, i
- 1);
1937 isl_stat
isl_basic_map_free_div(__isl_keep isl_basic_map
*bmap
, unsigned n
)
1940 return isl_stat_error
;
1941 isl_assert(bmap
->ctx
, n
<= bmap
->n_div
, return isl_stat_error
);
1946 static __isl_give isl_basic_map
*add_constraints(
1947 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
,
1948 unsigned i_pos
, unsigned o_pos
)
1950 isl_size total
, n_param
, n_in
, n_out
, n_div
;
1951 unsigned o_in
, o_out
;
1954 struct isl_dim_map
*dim_map
;
1956 space
= isl_basic_map_peek_space(bmap2
);
1957 if (!bmap1
|| !space
)
1960 total
= isl_basic_map_dim(bmap1
, isl_dim_all
);
1961 n_param
= isl_basic_map_dim(bmap2
, isl_dim_param
);
1962 n_in
= isl_basic_map_dim(bmap2
, isl_dim_in
);
1963 o_in
= isl_basic_map_offset(bmap1
, isl_dim_in
) - 1 + i_pos
;
1964 n_out
= isl_basic_map_dim(bmap2
, isl_dim_out
);
1965 o_out
= isl_basic_map_offset(bmap1
, isl_dim_out
) - 1 + o_pos
;
1966 n_div
= isl_basic_map_dim(bmap2
, isl_dim_div
);
1967 if (total
< 0 || n_param
< 0 || n_in
< 0 || n_out
< 0 || n_div
< 0)
1969 ctx
= isl_basic_map_get_ctx(bmap1
);
1970 dim_map
= isl_dim_map_alloc(ctx
, total
+ n_div
);
1971 isl_dim_map_dim_range(dim_map
, space
, isl_dim_param
, 0, n_param
, 0);
1972 isl_dim_map_dim_range(dim_map
, space
, isl_dim_in
, 0, n_in
, o_in
);
1973 isl_dim_map_dim_range(dim_map
, space
, isl_dim_out
, 0, n_out
, o_out
);
1974 isl_dim_map_div(dim_map
, bmap2
, total
);
1976 return isl_basic_map_add_constraints_dim_map(bmap1
, bmap2
, dim_map
);
1978 isl_basic_map_free(bmap1
);
1979 isl_basic_map_free(bmap2
);
1983 __isl_give isl_basic_map
*isl_basic_map_extend(__isl_take isl_basic_map
*base
,
1984 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
1987 struct isl_basic_map
*ext
;
1994 dims_ok
= base
->extra
>= base
->n_div
+ extra
;
1996 if (dims_ok
&& room_for_con(base
, n_eq
+ n_ineq
) &&
1997 room_for_ineq(base
, n_ineq
))
2000 extra
+= base
->extra
;
2002 n_ineq
+= base
->n_ineq
;
2004 space
= isl_basic_map_get_space(base
);
2005 ext
= isl_basic_map_alloc_space(space
, extra
, n_eq
, n_ineq
);
2010 ext
->sample
= isl_vec_copy(base
->sample
);
2011 flags
= base
->flags
;
2012 ext
= add_constraints(ext
, base
, 0, 0);
2015 ISL_F_CLR(ext
, ISL_BASIC_SET_FINAL
);
2021 isl_basic_map_free(base
);
2025 __isl_give isl_basic_set
*isl_basic_set_extend(__isl_take isl_basic_set
*base
,
2026 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
2028 return bset_from_bmap(isl_basic_map_extend(bset_to_bmap(base
),
2029 extra
, n_eq
, n_ineq
));
2032 __isl_give isl_basic_map
*isl_basic_map_extend_constraints(
2033 __isl_take isl_basic_map
*base
, unsigned n_eq
, unsigned n_ineq
)
2035 return isl_basic_map_extend(base
, 0, n_eq
, n_ineq
);
2038 __isl_give isl_basic_set
*isl_basic_set_extend_constraints(
2039 __isl_take isl_basic_set
*base
, unsigned n_eq
, unsigned n_ineq
)
2041 isl_basic_map
*bmap
= bset_to_bmap(base
);
2042 bmap
= isl_basic_map_extend_constraints(bmap
, n_eq
, n_ineq
);
2043 return bset_from_bmap(bmap
);
2046 __isl_give isl_basic_set
*isl_basic_set_cow(__isl_take isl_basic_set
*bset
)
2048 return bset_from_bmap(isl_basic_map_cow(bset_to_bmap(bset
)));
2051 __isl_give isl_basic_map
*isl_basic_map_cow(__isl_take isl_basic_map
*bmap
)
2056 if (bmap
->ref
> 1) {
2058 bmap
= isl_basic_map_dup(bmap
);
2061 ISL_F_CLR(bmap
, ISL_BASIC_SET_FINAL
);
2062 ISL_F_CLR(bmap
, ISL_BASIC_MAP_REDUCED_COEFFICIENTS
);
2067 /* Clear all cached information in "map", either because it is about
2068 * to be modified or because it is being freed.
2069 * Always return the same pointer that is passed in.
2070 * This is needed for the use in isl_map_free.
2072 static __isl_give isl_map
*clear_caches(__isl_take isl_map
*map
)
2074 isl_basic_map_free(map
->cached_simple_hull
[0]);
2075 isl_basic_map_free(map
->cached_simple_hull
[1]);
2076 map
->cached_simple_hull
[0] = NULL
;
2077 map
->cached_simple_hull
[1] = NULL
;
2081 __isl_give isl_set
*isl_set_cow(__isl_take isl_set
*set
)
2083 return isl_map_cow(set
);
2086 /* Return an isl_map that is equal to "map" and that has only
2087 * a single reference.
2089 * If the original input already has only one reference, then
2090 * simply return it, but clear all cached information, since
2091 * it may be rendered invalid by the operations that will be
2092 * performed on the result.
2094 * Otherwise, create a duplicate (without any cached information).
2096 __isl_give isl_map
*isl_map_cow(__isl_take isl_map
*map
)
2102 return clear_caches(map
);
2104 return isl_map_dup(map
);
2107 static void swap_vars(struct isl_blk blk
, isl_int
*a
,
2108 unsigned a_len
, unsigned b_len
)
2110 isl_seq_cpy(blk
.data
, a
+a_len
, b_len
);
2111 isl_seq_cpy(blk
.data
+b_len
, a
, a_len
);
2112 isl_seq_cpy(a
, blk
.data
, b_len
+a_len
);
2115 static __isl_give isl_basic_map
*isl_basic_map_swap_vars(
2116 __isl_take isl_basic_map
*bmap
, unsigned pos
, unsigned n1
, unsigned n2
)
2121 if (isl_basic_map_check_range(bmap
, isl_dim_all
, pos
- 1, n1
+ n2
) < 0)
2124 if (n1
== 0 || n2
== 0)
2127 bmap
= isl_basic_map_cow(bmap
);
2131 blk
= isl_blk_alloc(bmap
->ctx
, n1
+ n2
);
2132 if (isl_blk_is_error(blk
))
2135 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2137 bmap
->eq
[i
] + pos
, n1
, n2
);
2139 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2141 bmap
->ineq
[i
] + pos
, n1
, n2
);
2143 for (i
= 0; i
< bmap
->n_div
; ++i
)
2145 bmap
->div
[i
]+1 + pos
, n1
, n2
);
2147 isl_blk_free(bmap
->ctx
, blk
);
2149 ISL_F_CLR(bmap
, ISL_BASIC_SET_SORTED
);
2150 bmap
= isl_basic_map_gauss(bmap
, NULL
);
2151 return isl_basic_map_finalize(bmap
);
2153 isl_basic_map_free(bmap
);
2157 /* The given basic map has turned out to be empty.
2158 * Explicitly mark it as such and change the representation
2159 * to a canonical representation of the empty basic map.
2160 * Since the basic map has conflicting constraints,
2161 * it must have at least one constraint, except perhaps
2162 * if it was already explicitly marked as being empty.
2163 * Do nothing in the latter case, i.e., if it has been marked empty and
2164 * has no constraints.
2166 __isl_give isl_basic_map
*isl_basic_map_set_to_empty(
2167 __isl_take isl_basic_map
*bmap
)
2174 n
= isl_basic_map_n_constraint(bmap
);
2175 empty
= isl_basic_map_plain_is_empty(bmap
);
2176 if (n
< 0 || empty
< 0)
2177 return isl_basic_map_free(bmap
);
2178 if (n
== 0 && empty
)
2180 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
2182 return isl_basic_map_free(bmap
);
2183 if (isl_basic_map_free_div(bmap
, bmap
->n_div
) < 0)
2184 return isl_basic_map_free(bmap
);
2185 bmap
= isl_basic_map_free_inequality(bmap
, bmap
->n_ineq
);
2188 if (bmap
->n_eq
> 0) {
2189 bmap
= isl_basic_map_free_equality(bmap
, bmap
->n_eq
- 1);
2193 i
= isl_basic_map_alloc_equality(bmap
);
2197 isl_int_set_si(bmap
->eq
[i
][0], 1);
2198 isl_seq_clr(bmap
->eq
[i
]+1, total
);
2199 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
2200 isl_vec_free(bmap
->sample
);
2201 bmap
->sample
= NULL
;
2202 return isl_basic_map_finalize(bmap
);
2204 isl_basic_map_free(bmap
);
2208 __isl_give isl_basic_set
*isl_basic_set_set_to_empty(
2209 __isl_take isl_basic_set
*bset
)
2211 return bset_from_bmap(isl_basic_map_set_to_empty(bset_to_bmap(bset
)));
2214 __isl_give isl_basic_map
*isl_basic_map_set_rational(
2215 __isl_take isl_basic_map
*bmap
)
2220 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
2223 bmap
= isl_basic_map_cow(bmap
);
2227 ISL_F_SET(bmap
, ISL_BASIC_MAP_RATIONAL
);
2229 return isl_basic_map_finalize(bmap
);
2232 __isl_give isl_basic_set
*isl_basic_set_set_rational(
2233 __isl_take isl_basic_set
*bset
)
2235 return isl_basic_map_set_rational(bset
);
2238 __isl_give isl_basic_set
*isl_basic_set_set_integral(
2239 __isl_take isl_basic_set
*bset
)
2244 if (!ISL_F_ISSET(bset
, ISL_BASIC_MAP_RATIONAL
))
2247 bset
= isl_basic_set_cow(bset
);
2251 ISL_F_CLR(bset
, ISL_BASIC_MAP_RATIONAL
);
2253 return isl_basic_set_finalize(bset
);
2256 __isl_give isl_map
*isl_map_set_rational(__isl_take isl_map
*map
)
2260 map
= isl_map_cow(map
);
2263 for (i
= 0; i
< map
->n
; ++i
) {
2264 map
->p
[i
] = isl_basic_map_set_rational(map
->p
[i
]);
2274 __isl_give isl_set
*isl_set_set_rational(__isl_take isl_set
*set
)
2276 return isl_map_set_rational(set
);
2279 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
2282 static void swap_div(__isl_keep isl_basic_map
*bmap
, int a
, int b
)
2284 isl_int
*t
= bmap
->div
[a
];
2285 bmap
->div
[a
] = bmap
->div
[b
];
2289 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
2290 * div definitions accordingly.
2292 __isl_give isl_basic_map
*isl_basic_map_swap_div(__isl_take isl_basic_map
*bmap
,
2298 off
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
2300 return isl_basic_map_free(bmap
);
2302 swap_div(bmap
, a
, b
);
2304 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2305 isl_int_swap(bmap
->eq
[i
][1+off
+a
], bmap
->eq
[i
][1+off
+b
]);
2307 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2308 isl_int_swap(bmap
->ineq
[i
][1+off
+a
], bmap
->ineq
[i
][1+off
+b
]);
2310 for (i
= 0; i
< bmap
->n_div
; ++i
)
2311 isl_int_swap(bmap
->div
[i
][1+1+off
+a
], bmap
->div
[i
][1+1+off
+b
]);
2312 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
2317 static void constraint_drop_vars(isl_int
*c
, unsigned n
, unsigned rem
)
2319 isl_seq_cpy(c
, c
+ n
, rem
);
2320 isl_seq_clr(c
+ rem
, n
);
2323 /* Drop n dimensions starting at first.
2325 * In principle, this frees up some extra variables as the number
2326 * of columns remains constant, but we would have to extend
2327 * the div array too as the number of rows in this array is assumed
2328 * to be equal to extra.
2330 __isl_give isl_basic_set
*isl_basic_set_drop_dims(
2331 __isl_take isl_basic_set
*bset
, unsigned first
, unsigned n
)
2333 return isl_basic_map_drop(bset_to_bmap(bset
), isl_dim_set
, first
, n
);
2336 /* Move "n" divs starting at "first" to the end of the list of divs.
2338 static __isl_give isl_basic_map
*move_divs_last(__isl_take isl_basic_map
*bmap
,
2339 unsigned first
, unsigned n
)
2344 if (first
+ n
== bmap
->n_div
)
2347 div
= isl_alloc_array(bmap
->ctx
, isl_int
*, n
);
2350 for (i
= 0; i
< n
; ++i
)
2351 div
[i
] = bmap
->div
[first
+ i
];
2352 for (i
= 0; i
< bmap
->n_div
- first
- n
; ++i
)
2353 bmap
->div
[first
+ i
] = bmap
->div
[first
+ n
+ i
];
2354 for (i
= 0; i
< n
; ++i
)
2355 bmap
->div
[bmap
->n_div
- n
+ i
] = div
[i
];
2359 isl_basic_map_free(bmap
);
2364 #define TYPE isl_map
2366 #include "check_type_range_templ.c"
2368 /* Check that there are "n" dimensions of type "type" starting at "first"
2371 isl_stat
isl_set_check_range(__isl_keep isl_set
*set
,
2372 enum isl_dim_type type
, unsigned first
, unsigned n
)
2374 return isl_map_check_range(set_to_map(set
), type
, first
, n
);
2377 /* Drop "n" dimensions of type "type" starting at "first".
2378 * Perform the core computation, without cowing or
2379 * simplifying and finalizing the result.
2381 * In principle, this frees up some extra variables as the number
2382 * of columns remains constant, but we would have to extend
2383 * the div array too as the number of rows in this array is assumed
2384 * to be equal to extra.
2386 __isl_give isl_basic_map
*isl_basic_map_drop_core(
2387 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
2388 unsigned first
, unsigned n
)
2395 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2396 return isl_basic_map_free(bmap
);
2398 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
2400 return isl_basic_map_free(bmap
);
2402 offset
= isl_basic_map_offset(bmap
, type
) + first
;
2403 left
= total
- (offset
- 1) - n
;
2404 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2405 constraint_drop_vars(bmap
->eq
[i
]+offset
, n
, left
);
2407 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2408 constraint_drop_vars(bmap
->ineq
[i
]+offset
, n
, left
);
2410 for (i
= 0; i
< bmap
->n_div
; ++i
)
2411 constraint_drop_vars(bmap
->div
[i
]+1+offset
, n
, left
);
2413 if (type
== isl_dim_div
) {
2414 bmap
= move_divs_last(bmap
, first
, n
);
2417 if (isl_basic_map_free_div(bmap
, n
) < 0)
2418 return isl_basic_map_free(bmap
);
2420 bmap
->dim
= isl_space_drop_dims(bmap
->dim
, type
, first
, n
);
2422 return isl_basic_map_free(bmap
);
2424 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
2425 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
2429 /* Drop "n" dimensions of type "type" starting at "first".
2431 * In principle, this frees up some extra variables as the number
2432 * of columns remains constant, but we would have to extend
2433 * the div array too as the number of rows in this array is assumed
2434 * to be equal to extra.
2436 __isl_give isl_basic_map
*isl_basic_map_drop(__isl_take isl_basic_map
*bmap
,
2437 enum isl_dim_type type
, unsigned first
, unsigned n
)
2441 if (n
== 0 && !isl_space_is_named_or_nested(bmap
->dim
, type
))
2444 bmap
= isl_basic_map_cow(bmap
);
2448 bmap
= isl_basic_map_drop_core(bmap
, type
, first
, n
);
2450 bmap
= isl_basic_map_simplify(bmap
);
2451 return isl_basic_map_finalize(bmap
);
2454 __isl_give isl_basic_set
*isl_basic_set_drop(__isl_take isl_basic_set
*bset
,
2455 enum isl_dim_type type
, unsigned first
, unsigned n
)
2457 return bset_from_bmap(isl_basic_map_drop(bset_to_bmap(bset
),
2461 /* No longer consider "map" to be normalized.
2463 static __isl_give isl_map
*isl_map_unmark_normalized(__isl_take isl_map
*map
)
2467 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
2471 __isl_give isl_map
*isl_map_drop(__isl_take isl_map
*map
,
2472 enum isl_dim_type type
, unsigned first
, unsigned n
)
2477 if (isl_map_check_range(map
, type
, first
, n
) < 0)
2478 return isl_map_free(map
);
2480 if (n
== 0 && !isl_space_is_named_or_nested(map
->dim
, type
))
2482 map
= isl_map_cow(map
);
2486 for (i
= 0; i
< map
->n
; ++i
) {
2487 map
->p
[i
] = isl_basic_map_drop(map
->p
[i
], type
, first
, n
);
2491 map
= isl_map_unmark_normalized(map
);
2493 space
= isl_map_take_space(map
);
2494 space
= isl_space_drop_dims(space
, type
, first
, n
);
2495 map
= isl_map_restore_space(map
, space
);
2503 __isl_give isl_set
*isl_set_drop(__isl_take isl_set
*set
,
2504 enum isl_dim_type type
, unsigned first
, unsigned n
)
2506 return set_from_map(isl_map_drop(set_to_map(set
), type
, first
, n
));
2509 /* Drop the integer division at position "div", which is assumed
2510 * not to appear in any of the constraints or
2511 * in any of the other integer divisions.
2513 * Since the integer division is redundant, there is no need to cow.
2515 __isl_give isl_basic_map
*isl_basic_map_drop_div(
2516 __isl_take isl_basic_map
*bmap
, unsigned div
)
2518 return isl_basic_map_drop_core(bmap
, isl_dim_div
, div
, 1);
2521 /* Eliminate the specified n dimensions starting at first from the
2522 * constraints, without removing the dimensions from the space.
2523 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2525 __isl_give isl_map
*isl_map_eliminate(__isl_take isl_map
*map
,
2526 enum isl_dim_type type
, unsigned first
, unsigned n
)
2533 if (isl_map_check_range(map
, type
, first
, n
) < 0)
2534 return isl_map_free(map
);
2536 map
= isl_map_cow(map
);
2540 for (i
= 0; i
< map
->n
; ++i
) {
2541 map
->p
[i
] = isl_basic_map_eliminate(map
->p
[i
], type
, first
, n
);
2551 /* Eliminate the specified n dimensions starting at first from the
2552 * constraints, without removing the dimensions from the space.
2553 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2555 __isl_give isl_set
*isl_set_eliminate(__isl_take isl_set
*set
,
2556 enum isl_dim_type type
, unsigned first
, unsigned n
)
2558 return set_from_map(isl_map_eliminate(set_to_map(set
), type
, first
, n
));
2561 /* Eliminate the specified n dimensions starting at first from the
2562 * constraints, without removing the dimensions from the space.
2563 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2565 __isl_give isl_set
*isl_set_eliminate_dims(__isl_take isl_set
*set
,
2566 unsigned first
, unsigned n
)
2568 return isl_set_eliminate(set
, isl_dim_set
, first
, n
);
2571 __isl_give isl_basic_map
*isl_basic_map_remove_divs(
2572 __isl_take isl_basic_map
*bmap
)
2576 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
2578 return isl_basic_map_free(bmap
);
2579 bmap
= isl_basic_map_eliminate_vars(bmap
, v_div
, bmap
->n_div
);
2583 return isl_basic_map_finalize(bmap
);
2586 __isl_give isl_basic_set
*isl_basic_set_remove_divs(
2587 __isl_take isl_basic_set
*bset
)
2589 return bset_from_bmap(isl_basic_map_remove_divs(bset_to_bmap(bset
)));
2592 __isl_give isl_map
*isl_map_remove_divs(__isl_take isl_map
*map
)
2601 map
= isl_map_cow(map
);
2605 for (i
= 0; i
< map
->n
; ++i
) {
2606 map
->p
[i
] = isl_basic_map_remove_divs(map
->p
[i
]);
2616 __isl_give isl_set
*isl_set_remove_divs(__isl_take isl_set
*set
)
2618 return isl_map_remove_divs(set
);
2621 __isl_give isl_basic_map
*isl_basic_map_remove_dims(
2622 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
2623 unsigned first
, unsigned n
)
2625 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2626 return isl_basic_map_free(bmap
);
2627 if (n
== 0 && !isl_space_is_named_or_nested(bmap
->dim
, type
))
2629 bmap
= isl_basic_map_eliminate_vars(bmap
,
2630 isl_basic_map_offset(bmap
, type
) - 1 + first
, n
);
2633 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
) && type
== isl_dim_div
)
2635 bmap
= isl_basic_map_drop(bmap
, type
, first
, n
);
2639 /* Return true if the definition of the given div (recursively) involves
2640 * any of the given variables.
2642 static isl_bool
div_involves_vars(__isl_keep isl_basic_map
*bmap
, int div
,
2643 unsigned first
, unsigned n
)
2646 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
2648 if (isl_int_is_zero(bmap
->div
[div
][0]))
2649 return isl_bool_false
;
2650 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + 1 + first
, n
) >= 0)
2651 return isl_bool_true
;
2653 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2656 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
2658 involves
= div_involves_vars(bmap
, i
, first
, n
);
2659 if (involves
< 0 || involves
)
2663 return isl_bool_false
;
2666 /* Try and add a lower and/or upper bound on "div" to "bmap"
2667 * based on inequality "i".
2668 * "total" is the total number of variables (excluding the divs).
2669 * "v" is a temporary object that can be used during the calculations.
2670 * If "lb" is set, then a lower bound should be constructed.
2671 * If "ub" is set, then an upper bound should be constructed.
2673 * The calling function has already checked that the inequality does not
2674 * reference "div", but we still need to check that the inequality is
2675 * of the right form. We'll consider the case where we want to construct
2676 * a lower bound. The construction of upper bounds is similar.
2678 * Let "div" be of the form
2680 * q = floor((a + f(x))/d)
2682 * We essentially check if constraint "i" is of the form
2686 * so that we can use it to derive a lower bound on "div".
2687 * However, we allow a slightly more general form
2691 * with the condition that the coefficients of g(x) - f(x) are all
2693 * Rewriting this constraint as
2697 * adding a + f(x) to both sides and dividing by d, we obtain
2699 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
2701 * Taking the floor on both sides, we obtain
2703 * q >= floor((a-b)/d) + (f(x)-g(x))/d
2707 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
2709 * In the case of an upper bound, we construct the constraint
2711 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
2714 static __isl_give isl_basic_map
*insert_bounds_on_div_from_ineq(
2715 __isl_take isl_basic_map
*bmap
, int div
, int i
,
2716 unsigned total
, isl_int v
, int lb
, int ub
)
2720 for (j
= 0; (lb
|| ub
) && j
< total
+ bmap
->n_div
; ++j
) {
2722 isl_int_sub(v
, bmap
->ineq
[i
][1 + j
],
2723 bmap
->div
[div
][1 + 1 + j
]);
2724 lb
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
2727 isl_int_add(v
, bmap
->ineq
[i
][1 + j
],
2728 bmap
->div
[div
][1 + 1 + j
]);
2729 ub
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
2735 bmap
= isl_basic_map_cow(bmap
);
2736 bmap
= isl_basic_map_extend_constraints(bmap
, 0, lb
+ ub
);
2738 int k
= isl_basic_map_alloc_inequality(bmap
);
2741 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
2742 isl_int_sub(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
2743 bmap
->div
[div
][1 + j
]);
2744 isl_int_cdiv_q(bmap
->ineq
[k
][j
],
2745 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
2747 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], 1);
2750 int k
= isl_basic_map_alloc_inequality(bmap
);
2753 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
2754 isl_int_add(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
2755 bmap
->div
[div
][1 + j
]);
2756 isl_int_fdiv_q(bmap
->ineq
[k
][j
],
2757 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
2759 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], -1);
2764 isl_basic_map_free(bmap
);
2768 /* This function is called right before "div" is eliminated from "bmap"
2769 * using Fourier-Motzkin.
2770 * Look through the constraints of "bmap" for constraints on the argument
2771 * of the integer division and use them to construct constraints on the
2772 * integer division itself. These constraints can then be combined
2773 * during the Fourier-Motzkin elimination.
2774 * Note that it is only useful to introduce lower bounds on "div"
2775 * if "bmap" already contains upper bounds on "div" as the newly
2776 * introduce lower bounds can then be combined with the pre-existing
2777 * upper bounds. Similarly for upper bounds.
2778 * We therefore first check if "bmap" contains any lower and/or upper bounds
2781 * It is interesting to note that the introduction of these constraints
2782 * can indeed lead to more accurate results, even when compared to
2783 * deriving constraints on the argument of "div" from constraints on "div".
2784 * Consider, for example, the set
2786 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2788 * The second constraint can be rewritten as
2790 * 2 * [(-i-2j+3)/4] + k >= 0
2792 * from which we can derive
2794 * -i - 2j + 3 >= -2k
2800 * Combined with the first constraint, we obtain
2802 * -3 <= 3 + 2k or k >= -3
2804 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2805 * the first constraint, we obtain
2807 * [(i + 2j)/4] >= [-3/4] = -1
2809 * Combining this constraint with the second constraint, we obtain
2813 static __isl_give isl_basic_map
*insert_bounds_on_div(
2814 __isl_take isl_basic_map
*bmap
, int div
)
2817 int check_lb
, check_ub
;
2824 if (isl_int_is_zero(bmap
->div
[div
][0]))
2827 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
2829 return isl_basic_map_free(bmap
);
2833 for (i
= 0; (!check_lb
|| !check_ub
) && i
< bmap
->n_ineq
; ++i
) {
2834 int s
= isl_int_sgn(bmap
->ineq
[i
][1 + v_div
+ div
]);
2841 if (!check_lb
&& !check_ub
)
2846 for (i
= 0; bmap
&& i
< bmap
->n_ineq
; ++i
) {
2847 if (!isl_int_is_zero(bmap
->ineq
[i
][1 + v_div
+ div
]))
2850 bmap
= insert_bounds_on_div_from_ineq(bmap
, div
, i
, v_div
, v
,
2851 check_lb
, check_ub
);
2859 /* Remove all divs (recursively) involving any of the given dimensions
2860 * in their definitions.
2862 __isl_give isl_basic_map
*isl_basic_map_remove_divs_involving_dims(
2863 __isl_take isl_basic_map
*bmap
,
2864 enum isl_dim_type type
, unsigned first
, unsigned n
)
2869 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2870 return isl_basic_map_free(bmap
);
2871 off
= isl_basic_map_var_offset(bmap
, type
);
2873 return isl_basic_map_free(bmap
);
2876 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2879 involves
= div_involves_vars(bmap
, i
, first
, n
);
2881 return isl_basic_map_free(bmap
);
2884 bmap
= insert_bounds_on_div(bmap
, i
);
2885 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
2894 __isl_give isl_basic_set
*isl_basic_set_remove_divs_involving_dims(
2895 __isl_take isl_basic_set
*bset
,
2896 enum isl_dim_type type
, unsigned first
, unsigned n
)
2898 return isl_basic_map_remove_divs_involving_dims(bset
, type
, first
, n
);
2901 __isl_give isl_map
*isl_map_remove_divs_involving_dims(__isl_take isl_map
*map
,
2902 enum isl_dim_type type
, unsigned first
, unsigned n
)
2911 map
= isl_map_cow(map
);
2915 for (i
= 0; i
< map
->n
; ++i
) {
2916 map
->p
[i
] = isl_basic_map_remove_divs_involving_dims(map
->p
[i
],
2927 __isl_give isl_set
*isl_set_remove_divs_involving_dims(__isl_take isl_set
*set
,
2928 enum isl_dim_type type
, unsigned first
, unsigned n
)
2930 return set_from_map(isl_map_remove_divs_involving_dims(set_to_map(set
),
2934 /* Does the description of "bmap" depend on the specified dimensions?
2935 * We also check whether the dimensions appear in any of the div definitions.
2936 * In principle there is no need for this check. If the dimensions appear
2937 * in a div definition, they also appear in the defining constraints of that
2940 isl_bool
isl_basic_map_involves_dims(__isl_keep isl_basic_map
*bmap
,
2941 enum isl_dim_type type
, unsigned first
, unsigned n
)
2945 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2946 return isl_bool_error
;
2948 first
+= isl_basic_map_offset(bmap
, type
);
2949 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2950 if (isl_seq_first_non_zero(bmap
->eq
[i
] + first
, n
) >= 0)
2951 return isl_bool_true
;
2952 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2953 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + first
, n
) >= 0)
2954 return isl_bool_true
;
2955 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2956 if (isl_int_is_zero(bmap
->div
[i
][0]))
2958 if (isl_seq_first_non_zero(bmap
->div
[i
] + 1 + first
, n
) >= 0)
2959 return isl_bool_true
;
2962 return isl_bool_false
;
2965 isl_bool
isl_map_involves_dims(__isl_keep isl_map
*map
,
2966 enum isl_dim_type type
, unsigned first
, unsigned n
)
2970 if (isl_map_check_range(map
, type
, first
, n
) < 0)
2971 return isl_bool_error
;
2973 for (i
= 0; i
< map
->n
; ++i
) {
2974 isl_bool involves
= isl_basic_map_involves_dims(map
->p
[i
],
2976 if (involves
< 0 || involves
)
2980 return isl_bool_false
;
2983 isl_bool
isl_basic_set_involves_dims(__isl_keep isl_basic_set
*bset
,
2984 enum isl_dim_type type
, unsigned first
, unsigned n
)
2986 return isl_basic_map_involves_dims(bset
, type
, first
, n
);
2989 isl_bool
isl_set_involves_dims(__isl_keep isl_set
*set
,
2990 enum isl_dim_type type
, unsigned first
, unsigned n
)
2992 return isl_map_involves_dims(set
, type
, first
, n
);
2995 /* Does "bset" involve any local variables, i.e., integer divisions?
2997 static isl_bool
isl_basic_set_involves_locals(__isl_keep isl_basic_set
*bset
)
3001 n
= isl_basic_set_dim(bset
, isl_dim_div
);
3003 return isl_bool_error
;
3004 return isl_bool_ok(n
> 0);
3007 /* isl_set_every_basic_set callback that checks whether "bset"
3008 * is free of local variables.
3010 static isl_bool
basic_set_no_locals(__isl_keep isl_basic_set
*bset
, void *user
)
3012 return isl_bool_not(isl_basic_set_involves_locals(bset
));
3015 /* Does "set" involve any local variables, i.e., integer divisions?
3017 isl_bool
isl_set_involves_locals(__isl_keep isl_set
*set
)
3021 no_locals
= isl_set_every_basic_set(set
, &basic_set_no_locals
, NULL
);
3022 return isl_bool_not(no_locals
);
3025 /* Drop all constraints in bmap that involve any of the dimensions
3026 * first to first+n-1.
3027 * This function only performs the actual removal of constraints.
3029 * This function should not call finalize since it is used by
3030 * remove_redundant_divs, which in turn is called by isl_basic_map_finalize.
3032 __isl_give isl_basic_map
*isl_basic_map_drop_constraints_involving(
3033 __isl_take isl_basic_map
*bmap
, unsigned first
, unsigned n
)
3040 bmap
= isl_basic_map_cow(bmap
);
3045 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
) {
3046 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + first
, n
) == -1)
3048 if (isl_basic_map_drop_equality(bmap
, i
) < 0)
3049 return isl_basic_map_free(bmap
);
3052 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
3053 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + 1 + first
, n
) == -1)
3055 if (isl_basic_map_drop_inequality(bmap
, i
) < 0)
3056 return isl_basic_map_free(bmap
);
3062 /* Drop all constraints in bset that involve any of the dimensions
3063 * first to first+n-1.
3064 * This function only performs the actual removal of constraints.
3066 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_involving(
3067 __isl_take isl_basic_set
*bset
, unsigned first
, unsigned n
)
3069 return isl_basic_map_drop_constraints_involving(bset
, first
, n
);
3072 /* Drop all constraints in bmap that do not involve any of the dimensions
3073 * first to first + n - 1 of the given type.
3075 __isl_give isl_basic_map
*isl_basic_map_drop_constraints_not_involving_dims(
3076 __isl_take isl_basic_map
*bmap
,
3077 enum isl_dim_type type
, unsigned first
, unsigned n
)
3082 isl_space
*space
= isl_basic_map_get_space(bmap
);
3083 isl_basic_map_free(bmap
);
3084 return isl_basic_map_universe(space
);
3086 bmap
= isl_basic_map_cow(bmap
);
3090 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
3091 return isl_basic_map_free(bmap
);
3093 first
+= isl_basic_map_offset(bmap
, type
) - 1;
3095 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
) {
3096 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + first
, n
) != -1)
3098 if (isl_basic_map_drop_equality(bmap
, i
) < 0)
3099 return isl_basic_map_free(bmap
);
3102 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
3103 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + 1 + first
, n
) != -1)
3105 if (isl_basic_map_drop_inequality(bmap
, i
) < 0)
3106 return isl_basic_map_free(bmap
);
3109 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
3113 /* Drop all constraints in bset that do not involve any of the dimensions
3114 * first to first + n - 1 of the given type.
3116 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_not_involving_dims(
3117 __isl_take isl_basic_set
*bset
,
3118 enum isl_dim_type type
, unsigned first
, unsigned n
)
3120 return isl_basic_map_drop_constraints_not_involving_dims(bset
,
3124 /* Drop all constraints in bmap that involve any of the dimensions
3125 * first to first + n - 1 of the given type.
3127 __isl_give isl_basic_map
*isl_basic_map_drop_constraints_involving_dims(
3128 __isl_take isl_basic_map
*bmap
,
3129 enum isl_dim_type type
, unsigned first
, unsigned n
)
3136 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
3137 return isl_basic_map_free(bmap
);
3139 bmap
= isl_basic_map_remove_divs_involving_dims(bmap
, type
, first
, n
);
3140 first
+= isl_basic_map_offset(bmap
, type
) - 1;
3141 bmap
= isl_basic_map_drop_constraints_involving(bmap
, first
, n
);
3142 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
3146 /* Drop all constraints in bset that involve any of the dimensions
3147 * first to first + n - 1 of the given type.
3149 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_involving_dims(
3150 __isl_take isl_basic_set
*bset
,
3151 enum isl_dim_type type
, unsigned first
, unsigned n
)
3153 return isl_basic_map_drop_constraints_involving_dims(bset
,
3157 /* Drop constraints from "map" by applying "drop" to each basic map.
3159 static __isl_give isl_map
*drop_constraints(__isl_take isl_map
*map
,
3160 enum isl_dim_type type
, unsigned first
, unsigned n
,
3161 __isl_give isl_basic_map
*(*drop
)(__isl_take isl_basic_map
*bmap
,
3162 enum isl_dim_type type
, unsigned first
, unsigned n
))
3166 if (isl_map_check_range(map
, type
, first
, n
) < 0)
3167 return isl_map_free(map
);
3169 map
= isl_map_cow(map
);
3173 for (i
= 0; i
< map
->n
; ++i
) {
3174 map
->p
[i
] = drop(map
->p
[i
], type
, first
, n
);
3176 return isl_map_free(map
);
3180 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
3185 /* Drop all constraints in map that involve any of the dimensions
3186 * first to first + n - 1 of the given type.
3188 __isl_give isl_map
*isl_map_drop_constraints_involving_dims(
3189 __isl_take isl_map
*map
,
3190 enum isl_dim_type type
, unsigned first
, unsigned n
)
3194 return drop_constraints(map
, type
, first
, n
,
3195 &isl_basic_map_drop_constraints_involving_dims
);
3198 /* Drop all constraints in "map" that do not involve any of the dimensions
3199 * first to first + n - 1 of the given type.
3201 __isl_give isl_map
*isl_map_drop_constraints_not_involving_dims(
3202 __isl_take isl_map
*map
,
3203 enum isl_dim_type type
, unsigned first
, unsigned n
)
3206 isl_space
*space
= isl_map_get_space(map
);
3208 return isl_map_universe(space
);
3210 return drop_constraints(map
, type
, first
, n
,
3211 &isl_basic_map_drop_constraints_not_involving_dims
);
3214 /* Drop all constraints in set that involve any of the dimensions
3215 * first to first + n - 1 of the given type.
3217 __isl_give isl_set
*isl_set_drop_constraints_involving_dims(
3218 __isl_take isl_set
*set
,
3219 enum isl_dim_type type
, unsigned first
, unsigned n
)
3221 return isl_map_drop_constraints_involving_dims(set
, type
, first
, n
);
3224 /* Drop all constraints in "set" that do not involve any of the dimensions
3225 * first to first + n - 1 of the given type.
3227 __isl_give isl_set
*isl_set_drop_constraints_not_involving_dims(
3228 __isl_take isl_set
*set
,
3229 enum isl_dim_type type
, unsigned first
, unsigned n
)
3231 return isl_map_drop_constraints_not_involving_dims(set
, type
, first
, n
);
3234 /* Does local variable "div" of "bmap" have a complete explicit representation?
3235 * Having a complete explicit representation requires not only
3236 * an explicit representation, but also that all local variables
3237 * that appear in this explicit representation in turn have
3238 * a complete explicit representation.
3240 isl_bool
isl_basic_map_div_is_known(__isl_keep isl_basic_map
*bmap
, int div
)
3243 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
3246 marked
= isl_basic_map_div_is_marked_unknown(bmap
, div
);
3247 if (marked
< 0 || marked
)
3248 return isl_bool_not(marked
);
3250 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
3253 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
3255 known
= isl_basic_map_div_is_known(bmap
, i
);
3256 if (known
< 0 || !known
)
3260 return isl_bool_true
;
3263 /* Remove all divs that are unknown or defined in terms of unknown divs.
3265 __isl_give isl_basic_map
*isl_basic_map_remove_unknown_divs(
3266 __isl_take isl_basic_map
*bmap
)
3273 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
3274 if (isl_basic_map_div_is_known(bmap
, i
))
3276 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
3285 /* Remove all divs that are unknown or defined in terms of unknown divs.
3287 __isl_give isl_basic_set
*isl_basic_set_remove_unknown_divs(
3288 __isl_take isl_basic_set
*bset
)
3290 return isl_basic_map_remove_unknown_divs(bset
);
3293 __isl_give isl_map
*isl_map_remove_unknown_divs(__isl_take isl_map
*map
)
3302 map
= isl_map_cow(map
);
3306 for (i
= 0; i
< map
->n
; ++i
) {
3307 map
->p
[i
] = isl_basic_map_remove_unknown_divs(map
->p
[i
]);
3317 __isl_give isl_set
*isl_set_remove_unknown_divs(__isl_take isl_set
*set
)
3319 return set_from_map(isl_map_remove_unknown_divs(set_to_map(set
)));
3322 __isl_give isl_basic_set
*isl_basic_set_remove_dims(
3323 __isl_take isl_basic_set
*bset
,
3324 enum isl_dim_type type
, unsigned first
, unsigned n
)
3326 isl_basic_map
*bmap
= bset_to_bmap(bset
);
3327 bmap
= isl_basic_map_remove_dims(bmap
, type
, first
, n
);
3328 return bset_from_bmap(bmap
);
3331 __isl_give isl_map
*isl_map_remove_dims(__isl_take isl_map
*map
,
3332 enum isl_dim_type type
, unsigned first
, unsigned n
)
3339 map
= isl_map_cow(map
);
3340 if (isl_map_check_range(map
, type
, first
, n
) < 0)
3341 return isl_map_free(map
);
3343 for (i
= 0; i
< map
->n
; ++i
) {
3344 map
->p
[i
] = isl_basic_map_eliminate_vars(map
->p
[i
],
3345 isl_basic_map_offset(map
->p
[i
], type
) - 1 + first
, n
);
3349 map
= isl_map_drop(map
, type
, first
, n
);
3356 __isl_give isl_set
*isl_set_remove_dims(__isl_take isl_set
*bset
,
3357 enum isl_dim_type type
, unsigned first
, unsigned n
)
3359 return set_from_map(isl_map_remove_dims(set_to_map(bset
),
3363 /* Project out n inputs starting at first using Fourier-Motzkin */
3364 __isl_give isl_map
*isl_map_remove_inputs(__isl_take isl_map
*map
,
3365 unsigned first
, unsigned n
)
3367 return isl_map_remove_dims(map
, isl_dim_in
, first
, n
);
3370 void isl_basic_set_print_internal(__isl_keep isl_basic_set
*bset
,
3371 FILE *out
, int indent
)
3376 fprintf(out
, "null basic set\n");
3380 fprintf(out
, "%*s", indent
, "");
3381 fprintf(out
, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
3382 bset
->ref
, bset
->dim
->nparam
, bset
->dim
->n_out
,
3383 bset
->extra
, bset
->flags
);
3385 p
= isl_printer_to_file(isl_basic_set_get_ctx(bset
), out
);
3386 p
= isl_printer_set_dump(p
, 1);
3387 p
= isl_printer_set_indent(p
, indent
);
3388 p
= isl_printer_start_line(p
);
3389 p
= isl_printer_print_basic_set(p
, bset
);
3390 p
= isl_printer_end_line(p
);
3391 isl_printer_free(p
);
3394 void isl_basic_map_print_internal(__isl_keep isl_basic_map
*bmap
,
3395 FILE *out
, int indent
)
3400 fprintf(out
, "null basic map\n");
3404 fprintf(out
, "%*s", indent
, "");
3405 fprintf(out
, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
3406 "flags: %x, n_name: %d\n",
3408 bmap
->dim
->nparam
, bmap
->dim
->n_in
, bmap
->dim
->n_out
,
3409 bmap
->extra
, bmap
->flags
, bmap
->dim
->n_id
);
3411 p
= isl_printer_to_file(isl_basic_map_get_ctx(bmap
), out
);
3412 p
= isl_printer_set_dump(p
, 1);
3413 p
= isl_printer_set_indent(p
, indent
);
3414 p
= isl_printer_start_line(p
);
3415 p
= isl_printer_print_basic_map(p
, bmap
);
3416 p
= isl_printer_end_line(p
);
3417 isl_printer_free(p
);
3420 __isl_give isl_basic_map
*isl_inequality_negate(__isl_take isl_basic_map
*bmap
,
3425 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
3427 return isl_basic_map_free(bmap
);
3428 if (pos
>= bmap
->n_ineq
)
3429 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
3430 "invalid position", return isl_basic_map_free(bmap
));
3431 isl_seq_neg(bmap
->ineq
[pos
], bmap
->ineq
[pos
], 1 + total
);
3432 isl_int_sub_ui(bmap
->ineq
[pos
][0], bmap
->ineq
[pos
][0], 1);
3433 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
3434 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
3438 __isl_give isl_set
*isl_set_alloc_space(__isl_take isl_space
*space
, int n
,
3441 if (isl_space_check_is_set(space
) < 0)
3443 return isl_map_alloc_space(space
, n
, flags
);
3445 isl_space_free(space
);
3449 /* Make sure "map" has room for at least "n" more basic maps.
3451 __isl_give isl_map
*isl_map_grow(__isl_take isl_map
*map
, int n
)
3454 struct isl_map
*grown
= NULL
;
3458 isl_assert(map
->ctx
, n
>= 0, goto error
);
3459 if (map
->n
+ n
<= map
->size
)
3461 grown
= isl_map_alloc_space(isl_map_get_space(map
), map
->n
+ n
, map
->flags
);
3464 for (i
= 0; i
< map
->n
; ++i
) {
3465 grown
->p
[i
] = isl_basic_map_copy(map
->p
[i
]);
3473 isl_map_free(grown
);
3478 /* Make sure "set" has room for at least "n" more basic sets.
3480 __isl_give isl_set
*isl_set_grow(__isl_take isl_set
*set
, int n
)
3482 return set_from_map(isl_map_grow(set_to_map(set
), n
));
3485 __isl_give isl_set
*isl_set_from_basic_set(__isl_take isl_basic_set
*bset
)
3487 return isl_map_from_basic_map(bset
);
3490 /* This function performs the same operation as isl_set_from_basic_set,
3491 * but is considered as a function on an isl_basic_set when exported.
3493 __isl_give isl_set
*isl_basic_set_to_set(__isl_take isl_basic_set
*bset
)
3495 return isl_set_from_basic_set(bset
);
3498 __isl_give isl_map
*isl_map_from_basic_map(__isl_take isl_basic_map
*bmap
)
3500 struct isl_map
*map
;
3505 map
= isl_map_alloc_space(isl_space_copy(bmap
->dim
), 1, ISL_MAP_DISJOINT
);
3506 return isl_map_add_basic_map(map
, bmap
);
3509 __isl_give isl_set
*isl_set_add_basic_set(__isl_take isl_set
*set
,
3510 __isl_take isl_basic_set
*bset
)
3512 return set_from_map(isl_map_add_basic_map(set_to_map(set
),
3513 bset_to_bmap(bset
)));
3516 __isl_null isl_set
*isl_set_free(__isl_take isl_set
*set
)
3518 return isl_map_free(set
);
3521 void isl_set_print_internal(__isl_keep isl_set
*set
, FILE *out
, int indent
)
3526 fprintf(out
, "null set\n");
3530 fprintf(out
, "%*s", indent
, "");
3531 fprintf(out
, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
3532 set
->ref
, set
->n
, set
->dim
->nparam
, set
->dim
->n_out
,
3534 for (i
= 0; i
< set
->n
; ++i
) {
3535 fprintf(out
, "%*s", indent
, "");
3536 fprintf(out
, "basic set %d:\n", i
);
3537 isl_basic_set_print_internal(set
->p
[i
], out
, indent
+4);
3541 void isl_map_print_internal(__isl_keep isl_map
*map
, FILE *out
, int indent
)
3546 fprintf(out
, "null map\n");
3550 fprintf(out
, "%*s", indent
, "");
3551 fprintf(out
, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
3552 "flags: %x, n_name: %d\n",
3553 map
->ref
, map
->n
, map
->dim
->nparam
, map
->dim
->n_in
,
3554 map
->dim
->n_out
, map
->flags
, map
->dim
->n_id
);
3555 for (i
= 0; i
< map
->n
; ++i
) {
3556 fprintf(out
, "%*s", indent
, "");
3557 fprintf(out
, "basic map %d:\n", i
);
3558 isl_basic_map_print_internal(map
->p
[i
], out
, indent
+4);
3562 /* Check that the space of "bset" is the same as that of the domain of "bmap".
3564 static isl_stat
isl_basic_map_check_compatible_domain(
3565 __isl_keep isl_basic_map
*bmap
, __isl_keep isl_basic_set
*bset
)
3569 ok
= isl_basic_map_compatible_domain(bmap
, bset
);
3571 return isl_stat_error
;
3573 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
3574 "incompatible spaces", return isl_stat_error
);
3579 __isl_give isl_basic_map
*isl_basic_map_intersect_domain(
3580 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*bset
)
3582 struct isl_basic_map
*bmap_domain
;
3585 if (isl_basic_map_check_equal_params(bmap
, bset_to_bmap(bset
)) < 0)
3588 dim
= isl_basic_set_dim(bset
, isl_dim_set
);
3592 isl_basic_map_check_compatible_domain(bmap
, bset
) < 0)
3595 bmap
= isl_basic_map_cow(bmap
);
3598 bmap
= isl_basic_map_extend(bmap
,
3599 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
3600 bmap_domain
= isl_basic_map_from_domain(bset
);
3601 bmap
= add_constraints(bmap
, bmap_domain
, 0, 0);
3603 bmap
= isl_basic_map_simplify(bmap
);
3604 return isl_basic_map_finalize(bmap
);
3606 isl_basic_map_free(bmap
);
3607 isl_basic_set_free(bset
);
3611 /* Check that the space of "bset" is the same as that of the range of "bmap".
3613 static isl_stat
isl_basic_map_check_compatible_range(
3614 __isl_keep isl_basic_map
*bmap
, __isl_keep isl_basic_set
*bset
)
3618 ok
= isl_basic_map_compatible_range(bmap
, bset
);
3620 return isl_stat_error
;
3622 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
3623 "incompatible spaces", return isl_stat_error
);
3628 __isl_give isl_basic_map
*isl_basic_map_intersect_range(
3629 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*bset
)
3631 struct isl_basic_map
*bmap_range
;
3634 if (isl_basic_map_check_equal_params(bmap
, bset_to_bmap(bset
)) < 0)
3637 dim
= isl_basic_set_dim(bset
, isl_dim_set
);
3640 if (dim
!= 0 && isl_basic_map_check_compatible_range(bmap
, bset
) < 0)
3643 if (isl_basic_set_plain_is_universe(bset
)) {
3644 isl_basic_set_free(bset
);
3648 bmap
= isl_basic_map_cow(bmap
);
3651 bmap
= isl_basic_map_extend(bmap
,
3652 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
3653 bmap_range
= bset_to_bmap(bset
);
3654 bmap
= add_constraints(bmap
, bmap_range
, 0, 0);
3656 bmap
= isl_basic_map_simplify(bmap
);
3657 return isl_basic_map_finalize(bmap
);
3659 isl_basic_map_free(bmap
);
3660 isl_basic_set_free(bset
);
3664 isl_bool
isl_basic_map_contains(__isl_keep isl_basic_map
*bmap
,
3665 __isl_keep isl_vec
*vec
)
3671 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
3672 if (total
< 0 || !vec
)
3673 return isl_bool_error
;
3675 if (1 + total
!= vec
->size
)
3676 return isl_bool_false
;
3680 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
3681 isl_seq_inner_product(vec
->el
, bmap
->eq
[i
], 1 + total
, &s
);
3682 if (!isl_int_is_zero(s
)) {
3684 return isl_bool_false
;
3688 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
3689 isl_seq_inner_product(vec
->el
, bmap
->ineq
[i
], 1 + total
, &s
);
3690 if (isl_int_is_neg(s
)) {
3692 return isl_bool_false
;
3698 return isl_bool_true
;
3701 isl_bool
isl_basic_set_contains(__isl_keep isl_basic_set
*bset
,
3702 __isl_keep isl_vec
*vec
)
3704 return isl_basic_map_contains(bset_to_bmap(bset
), vec
);
3707 __isl_give isl_basic_map
*isl_basic_map_intersect(
3708 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
3710 struct isl_vec
*sample
= NULL
;
3711 isl_space
*space1
, *space2
;
3712 isl_size dim1
, dim2
, nparam1
, nparam2
;
3714 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
3716 space1
= isl_basic_map_peek_space(bmap1
);
3717 space2
= isl_basic_map_peek_space(bmap2
);
3718 dim1
= isl_space_dim(space1
, isl_dim_all
);
3719 dim2
= isl_space_dim(space2
, isl_dim_all
);
3720 nparam1
= isl_space_dim(space1
, isl_dim_param
);
3721 nparam2
= isl_space_dim(space2
, isl_dim_param
);
3722 if (dim1
< 0 || dim2
< 0 || nparam1
< 0 || nparam2
< 0)
3724 if (dim1
== nparam1
&& dim2
!= nparam2
)
3725 return isl_basic_map_intersect(bmap2
, bmap1
);
3727 if (dim2
!= nparam2
&&
3728 isl_basic_map_check_equal_space(bmap1
, bmap2
) < 0)
3731 if (isl_basic_map_plain_is_empty(bmap1
)) {
3732 isl_basic_map_free(bmap2
);
3735 if (isl_basic_map_plain_is_empty(bmap2
)) {
3736 isl_basic_map_free(bmap1
);
3740 if (bmap1
->sample
&&
3741 isl_basic_map_contains(bmap1
, bmap1
->sample
) > 0 &&
3742 isl_basic_map_contains(bmap2
, bmap1
->sample
) > 0)
3743 sample
= isl_vec_copy(bmap1
->sample
);
3744 else if (bmap2
->sample
&&
3745 isl_basic_map_contains(bmap1
, bmap2
->sample
) > 0 &&
3746 isl_basic_map_contains(bmap2
, bmap2
->sample
) > 0)
3747 sample
= isl_vec_copy(bmap2
->sample
);
3749 bmap1
= isl_basic_map_cow(bmap1
);
3752 bmap1
= isl_basic_map_extend(bmap1
,
3753 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
3754 bmap1
= add_constraints(bmap1
, bmap2
, 0, 0);
3757 isl_vec_free(sample
);
3759 isl_vec_free(bmap1
->sample
);
3760 bmap1
->sample
= sample
;
3763 bmap1
= isl_basic_map_simplify(bmap1
);
3764 return isl_basic_map_finalize(bmap1
);
3767 isl_vec_free(sample
);
3768 isl_basic_map_free(bmap1
);
3769 isl_basic_map_free(bmap2
);
3773 __isl_give isl_basic_set
*isl_basic_set_intersect(
3774 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
3776 return bset_from_bmap(isl_basic_map_intersect(bset_to_bmap(bset1
),
3777 bset_to_bmap(bset2
)));
3780 /* Intersect the parameter domain of "bmap" with "bset".
3782 * isl_basic_map_intersect handles this as a special case.
3784 __isl_give isl_basic_map
*isl_basic_map_intersect_params(
3785 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*bset
)
3787 return isl_basic_map_intersect(bmap
, bset
);
3790 __isl_give isl_basic_set
*isl_basic_set_intersect_params(
3791 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
3793 isl_basic_map
*bmap
;
3795 bmap
= bset_to_bmap(bset1
);
3796 bmap
= isl_basic_map_intersect_params(bmap
, bset2
);
3797 return bset_from_bmap(bmap
);
3800 /* Does "map" consist of a single disjunct, without any local variables?
3802 static isl_bool
is_convex_no_locals(__isl_keep isl_map
*map
)
3807 return isl_bool_error
;
3809 return isl_bool_false
;
3810 n_div
= isl_basic_map_dim(map
->p
[0], isl_dim_div
);
3812 return isl_bool_error
;
3814 return isl_bool_false
;
3815 return isl_bool_true
;
3818 /* Check that "map" consists of a single disjunct, without any local variables.
3820 static isl_stat
check_convex_no_locals(__isl_keep isl_map
*map
)
3824 ok
= is_convex_no_locals(map
);
3826 return isl_stat_error
;
3830 isl_die(isl_map_get_ctx(map
), isl_error_internal
,
3831 "unexpectedly not convex or involving local variables",
3832 return isl_stat_error
);
3835 /* Special case of isl_map_intersect, where both map1 and map2
3836 * are convex, without any divs and such that either map1 or map2
3837 * contains a single constraint. This constraint is then simply
3838 * added to the other map.
3840 static __isl_give isl_map
*map_intersect_add_constraint(
3841 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
3843 if (check_convex_no_locals(map1
) < 0 ||
3844 check_convex_no_locals(map2
) < 0)
3847 if (map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
!= 1)
3848 return isl_map_intersect(map2
, map1
);
3850 map1
= isl_map_cow(map1
);
3853 if (isl_map_plain_is_empty(map1
)) {
3857 if (map2
->p
[0]->n_eq
== 1)
3858 map1
->p
[0] = isl_basic_map_add_eq(map1
->p
[0], map2
->p
[0]->eq
[0]);
3860 map1
->p
[0] = isl_basic_map_add_ineq(map1
->p
[0],
3861 map2
->p
[0]->ineq
[0]);
3863 map1
->p
[0] = isl_basic_map_simplify(map1
->p
[0]);
3864 map1
->p
[0] = isl_basic_map_finalize(map1
->p
[0]);
3868 if (isl_basic_map_plain_is_empty(map1
->p
[0])) {
3869 isl_basic_map_free(map1
->p
[0]);
3875 map1
= isl_map_unmark_normalized(map1
);
3883 /* map2 may be either a parameter domain or a map living in the same
3886 static __isl_give isl_map
*map_intersect_internal(__isl_take isl_map
*map1
,
3887 __isl_take isl_map
*map2
)
3893 isl_size dim2
, nparam2
;
3898 if ((isl_map_plain_is_empty(map1
) ||
3899 isl_map_plain_is_universe(map2
)) &&
3900 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
3904 if ((isl_map_plain_is_empty(map2
) ||
3905 isl_map_plain_is_universe(map1
)) &&
3906 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
3911 if (is_convex_no_locals(map1
) == isl_bool_true
&&
3912 is_convex_no_locals(map2
) == isl_bool_true
&&
3913 isl_space_is_equal(map1
->dim
, map2
->dim
) &&
3914 (map1
->p
[0]->n_eq
+ map1
->p
[0]->n_ineq
== 1 ||
3915 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1))
3916 return map_intersect_add_constraint(map1
, map2
);
3918 equal
= isl_map_plain_is_equal(map1
, map2
);
3926 dim2
= isl_map_dim(map2
, isl_dim_all
);
3927 nparam2
= isl_map_dim(map2
, isl_dim_param
);
3928 if (dim2
< 0 || nparam2
< 0)
3930 if (dim2
!= nparam2
)
3931 isl_assert(map1
->ctx
,
3932 isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
3934 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
3935 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
3936 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
3938 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
3939 map1
->n
* map2
->n
, flags
);
3942 for (i
= 0; i
< map1
->n
; ++i
)
3943 for (j
= 0; j
< map2
->n
; ++j
) {
3944 struct isl_basic_map
*part
;
3945 part
= isl_basic_map_intersect(
3946 isl_basic_map_copy(map1
->p
[i
]),
3947 isl_basic_map_copy(map2
->p
[j
]));
3948 if (isl_basic_map_is_empty(part
) < 0)
3949 part
= isl_basic_map_free(part
);
3950 result
= isl_map_add_basic_map(result
, part
);
3963 static __isl_give isl_map
*map_intersect(__isl_take isl_map
*map1
,
3964 __isl_take isl_map
*map2
)
3966 if (isl_map_check_equal_space(map1
, map2
) < 0)
3968 return map_intersect_internal(map1
, map2
);
3975 __isl_give isl_map
*isl_map_intersect(__isl_take isl_map
*map1
,
3976 __isl_take isl_map
*map2
)
3978 isl_map_align_params_bin(&map1
, &map2
);
3979 return map_intersect(map1
, map2
);
3982 __isl_give isl_set
*isl_set_intersect(__isl_take isl_set
*set1
,
3983 __isl_take isl_set
*set2
)
3985 return set_from_map(isl_map_intersect(set_to_map(set1
),
3989 /* map_intersect_internal accepts intersections
3990 * with parameter domains, so we can just call that function.
3992 __isl_give isl_map
*isl_map_intersect_params(__isl_take isl_map
*map
,
3993 __isl_take isl_set
*params
)
3995 isl_map_align_params_set(&map
, ¶ms
);
3996 return map_intersect_internal(map
, params
);
3999 __isl_give isl_set
*isl_set_intersect_params(__isl_take isl_set
*set
,
4000 __isl_take isl_set
*params
)
4002 return isl_map_intersect_params(set
, params
);
4005 __isl_give isl_basic_map
*isl_basic_map_reverse(__isl_take isl_basic_map
*bmap
)
4013 bmap
= isl_basic_map_cow(bmap
);
4016 space
= isl_space_reverse(isl_space_copy(bmap
->dim
));
4017 pos
= isl_basic_map_offset(bmap
, isl_dim_in
);
4018 n1
= isl_basic_map_dim(bmap
, isl_dim_in
);
4019 n2
= isl_basic_map_dim(bmap
, isl_dim_out
);
4020 if (n1
< 0 || n2
< 0)
4021 bmap
= isl_basic_map_free(bmap
);
4022 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
4023 return isl_basic_map_reset_space(bmap
, space
);
4026 /* Given a basic map where the tuple of type "type" is a wrapped map,
4027 * swap domain and range of that wrapped map.
4029 static __isl_give isl_basic_map
*isl_basic_map_reverse_wrapped(
4030 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
)
4033 isl_size offset
, n1
, n2
;
4035 space
= isl_basic_map_peek_space(bmap
);
4036 offset
= isl_basic_map_var_offset(bmap
, type
);
4037 n1
= isl_space_wrapped_dim(space
, type
, isl_dim_in
);
4038 n2
= isl_space_wrapped_dim(space
, type
, isl_dim_out
);
4039 if (offset
< 0 || n1
< 0 || n2
< 0)
4040 return isl_basic_map_free(bmap
);
4042 bmap
= isl_basic_map_swap_vars(bmap
, 1 + offset
, n1
, n2
);
4044 space
= isl_basic_map_take_space(bmap
);
4045 space
= isl_space_reverse_wrapped(space
, type
);
4046 bmap
= isl_basic_map_restore_space(bmap
, space
);
4051 /* Given a basic map (A -> B) -> C, return the corresponding basic map
4054 static __isl_give isl_basic_map
*isl_basic_map_domain_reverse(
4055 __isl_take isl_basic_map
*bmap
)
4059 space
= isl_basic_map_peek_space(bmap
);
4060 if (isl_space_check_domain_is_wrapping(space
) < 0)
4061 return isl_basic_map_free(bmap
);
4062 bmap
= isl_basic_map_reverse_wrapped(bmap
, isl_dim_in
);
4067 /* Given a basic map A -> (B -> C), return the corresponding basic map
4070 static __isl_give isl_basic_map
*isl_basic_map_range_reverse(
4071 __isl_take isl_basic_map
*bmap
)
4075 space
= isl_basic_map_peek_space(bmap
);
4076 if (isl_space_check_range_is_wrapping(space
) < 0)
4077 return isl_basic_map_free(bmap
);
4078 bmap
= isl_basic_map_reverse_wrapped(bmap
, isl_dim_out
);
4083 /* Given a basic map that is actually a basic set (A -> B),
4084 * return the corresponding basic set (B -> A) as a basic map.
4086 static __isl_give isl_basic_map
*isl_basic_map_set_reverse(
4087 __isl_take isl_basic_map
*bmap
)
4091 space
= isl_basic_map_peek_space(bmap
);
4092 if (isl_space_check_is_wrapping(space
) < 0)
4093 return isl_basic_map_free(bmap
);
4094 bmap
= isl_basic_map_reverse_wrapped(bmap
, isl_dim_set
);
4099 static __isl_give isl_basic_map
*basic_map_space_reset(
4100 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
)
4106 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
4109 space
= isl_basic_map_get_space(bmap
);
4110 space
= isl_space_reset(space
, type
);
4111 bmap
= isl_basic_map_reset_space(bmap
, space
);
4115 __isl_give isl_basic_map
*isl_basic_map_insert_dims(
4116 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
4117 unsigned pos
, unsigned n
)
4119 isl_bool rational
, is_empty
;
4120 isl_space
*res_space
;
4121 struct isl_basic_map
*res
;
4122 struct isl_dim_map
*dim_map
;
4125 enum isl_dim_type t
;
4128 return basic_map_space_reset(bmap
, type
);
4130 is_empty
= isl_basic_map_plain_is_empty(bmap
);
4131 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4132 if (is_empty
< 0 || total
< 0)
4133 return isl_basic_map_free(bmap
);
4134 res_space
= isl_space_insert_dims(isl_basic_map_get_space(bmap
),
4137 return isl_basic_map_free(bmap
);
4139 isl_basic_map_free(bmap
);
4140 return isl_basic_map_empty(res_space
);
4143 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
+ n
);
4145 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
4149 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
4151 isl_size size
= isl_basic_map_dim(bmap
, t
);
4153 dim_map
= isl_dim_map_free(dim_map
);
4154 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
4156 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
4157 pos
, size
- pos
, off
+ pos
+ n
);
4159 dim
= isl_space_dim(res_space
, t
);
4161 dim_map
= isl_dim_map_free(dim_map
);
4164 isl_dim_map_div(dim_map
, bmap
, off
);
4166 res
= isl_basic_map_alloc_space(res_space
,
4167 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
4168 rational
= isl_basic_map_is_rational(bmap
);
4170 res
= isl_basic_map_free(res
);
4172 res
= isl_basic_map_set_rational(res
);
4173 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
4174 return isl_basic_map_finalize(res
);
4177 __isl_give isl_basic_set
*isl_basic_set_insert_dims(
4178 __isl_take isl_basic_set
*bset
,
4179 enum isl_dim_type type
, unsigned pos
, unsigned n
)
4181 return isl_basic_map_insert_dims(bset
, type
, pos
, n
);
4184 __isl_give isl_basic_map
*isl_basic_map_add_dims(__isl_take isl_basic_map
*bmap
,
4185 enum isl_dim_type type
, unsigned n
)
4189 dim
= isl_basic_map_dim(bmap
, type
);
4191 return isl_basic_map_free(bmap
);
4192 return isl_basic_map_insert_dims(bmap
, type
, dim
, n
);
4195 __isl_give isl_basic_set
*isl_basic_set_add_dims(__isl_take isl_basic_set
*bset
,
4196 enum isl_dim_type type
, unsigned n
)
4200 isl_assert(bset
->ctx
, type
!= isl_dim_in
, goto error
);
4201 return isl_basic_map_add_dims(bset
, type
, n
);
4203 isl_basic_set_free(bset
);
4207 static __isl_give isl_map
*map_space_reset(__isl_take isl_map
*map
,
4208 enum isl_dim_type type
)
4212 if (!map
|| !isl_space_is_named_or_nested(map
->dim
, type
))
4215 space
= isl_map_get_space(map
);
4216 space
= isl_space_reset(space
, type
);
4217 map
= isl_map_reset_space(map
, space
);
4221 __isl_give isl_map
*isl_map_insert_dims(__isl_take isl_map
*map
,
4222 enum isl_dim_type type
, unsigned pos
, unsigned n
)
4228 return map_space_reset(map
, type
);
4230 map
= isl_map_cow(map
);
4234 for (i
= 0; i
< map
->n
; ++i
) {
4235 map
->p
[i
] = isl_basic_map_insert_dims(map
->p
[i
], type
, pos
, n
);
4240 space
= isl_map_take_space(map
);
4241 space
= isl_space_insert_dims(space
, type
, pos
, n
);
4242 map
= isl_map_restore_space(map
, space
);
4250 __isl_give isl_set
*isl_set_insert_dims(__isl_take isl_set
*set
,
4251 enum isl_dim_type type
, unsigned pos
, unsigned n
)
4253 return isl_map_insert_dims(set
, type
, pos
, n
);
4256 __isl_give isl_map
*isl_map_add_dims(__isl_take isl_map
*map
,
4257 enum isl_dim_type type
, unsigned n
)
4261 dim
= isl_map_dim(map
, type
);
4263 return isl_map_free(map
);
4264 return isl_map_insert_dims(map
, type
, dim
, n
);
4267 __isl_give isl_set
*isl_set_add_dims(__isl_take isl_set
*set
,
4268 enum isl_dim_type type
, unsigned n
)
4272 isl_assert(set
->ctx
, type
!= isl_dim_in
, goto error
);
4273 return set_from_map(isl_map_add_dims(set_to_map(set
), type
, n
));
4279 __isl_give isl_basic_map
*isl_basic_map_move_dims(
4280 __isl_take isl_basic_map
*bmap
,
4281 enum isl_dim_type dst_type
, unsigned dst_pos
,
4282 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
4285 struct isl_dim_map
*dim_map
;
4286 struct isl_basic_map
*res
;
4287 enum isl_dim_type t
;
4294 bmap
= isl_basic_map_reset(bmap
, src_type
);
4295 bmap
= isl_basic_map_reset(bmap
, dst_type
);
4299 if (isl_basic_map_check_range(bmap
, src_type
, src_pos
, n
) < 0)
4300 return isl_basic_map_free(bmap
);
4302 if (dst_type
== src_type
&& dst_pos
== src_pos
)
4305 isl_assert(bmap
->ctx
, dst_type
!= src_type
, goto error
);
4307 if (pos(bmap
->dim
, dst_type
) + dst_pos
==
4308 pos(bmap
->dim
, src_type
) + src_pos
+
4309 ((src_type
< dst_type
) ? n
: 0)) {
4310 space
= isl_basic_map_take_space(bmap
);
4311 space
= isl_space_move_dims(space
, dst_type
, dst_pos
,
4312 src_type
, src_pos
, n
);
4313 bmap
= isl_basic_map_restore_space(bmap
, space
);
4314 bmap
= isl_basic_map_finalize(bmap
);
4319 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4321 return isl_basic_map_free(bmap
);
4322 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
4325 space
= isl_basic_map_peek_space(bmap
);
4326 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
4327 isl_size size
= isl_space_dim(space
, t
);
4329 dim_map
= isl_dim_map_free(dim_map
);
4330 if (t
== dst_type
) {
4331 isl_dim_map_dim_range(dim_map
, space
, t
,
4334 isl_dim_map_dim_range(dim_map
, space
, src_type
,
4337 isl_dim_map_dim_range(dim_map
, space
, t
,
4338 dst_pos
, size
- dst_pos
, off
);
4339 off
+= size
- dst_pos
;
4340 } else if (t
== src_type
) {
4341 isl_dim_map_dim_range(dim_map
, space
, t
,
4344 isl_dim_map_dim_range(dim_map
, space
, t
,
4345 src_pos
+ n
, size
- src_pos
- n
, off
);
4346 off
+= size
- src_pos
- n
;
4348 isl_dim_map_dim(dim_map
, space
, t
, off
);
4352 isl_dim_map_div(dim_map
, bmap
, off
);
4354 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
4355 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
4356 bmap
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
4357 space
= isl_basic_map_take_space(bmap
);
4358 space
= isl_space_move_dims(space
, dst_type
, dst_pos
,
4359 src_type
, src_pos
, n
);
4360 bmap
= isl_basic_map_restore_space(bmap
, space
);
4364 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
4365 bmap
= isl_basic_map_gauss(bmap
, NULL
);
4366 bmap
= isl_basic_map_finalize(bmap
);
4370 isl_basic_map_free(bmap
);
4374 __isl_give isl_basic_set
*isl_basic_set_move_dims(__isl_take isl_basic_set
*bset
,
4375 enum isl_dim_type dst_type
, unsigned dst_pos
,
4376 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
4378 isl_basic_map
*bmap
= bset_to_bmap(bset
);
4379 bmap
= isl_basic_map_move_dims(bmap
, dst_type
, dst_pos
,
4380 src_type
, src_pos
, n
);
4381 return bset_from_bmap(bmap
);
4384 __isl_give isl_set
*isl_set_move_dims(__isl_take isl_set
*set
,
4385 enum isl_dim_type dst_type
, unsigned dst_pos
,
4386 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
4390 isl_assert(set
->ctx
, dst_type
!= isl_dim_in
, goto error
);
4391 return set_from_map(isl_map_move_dims(set_to_map(set
),
4392 dst_type
, dst_pos
, src_type
, src_pos
, n
));
4398 __isl_give isl_map
*isl_map_move_dims(__isl_take isl_map
*map
,
4399 enum isl_dim_type dst_type
, unsigned dst_pos
,
4400 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
4406 map
= isl_map_reset(map
, src_type
);
4407 map
= isl_map_reset(map
, dst_type
);
4411 if (isl_map_check_range(map
, src_type
, src_pos
, n
))
4412 return isl_map_free(map
);
4414 if (dst_type
== src_type
&& dst_pos
== src_pos
)
4417 isl_assert(map
->ctx
, dst_type
!= src_type
, goto error
);
4419 map
= isl_map_cow(map
);
4423 for (i
= 0; i
< map
->n
; ++i
) {
4424 map
->p
[i
] = isl_basic_map_move_dims(map
->p
[i
],
4426 src_type
, src_pos
, n
);
4431 space
= isl_map_take_space(map
);
4432 space
= isl_space_move_dims(space
, dst_type
, dst_pos
,
4433 src_type
, src_pos
, n
);
4434 map
= isl_map_restore_space(map
, space
);
4442 /* Move the specified dimensions to the last columns right before
4443 * the divs. Don't change the dimension specification of bmap.
4444 * That's the responsibility of the caller.
4446 static __isl_give isl_basic_map
*move_last(__isl_take isl_basic_map
*bmap
,
4447 enum isl_dim_type type
, unsigned first
, unsigned n
)
4450 struct isl_dim_map
*dim_map
;
4451 struct isl_basic_map
*res
;
4452 enum isl_dim_type t
;
4458 if (isl_basic_map_offset(bmap
, type
) + first
+ n
==
4459 isl_basic_map_offset(bmap
, isl_dim_div
))
4462 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4464 return isl_basic_map_free(bmap
);
4465 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
4468 space
= isl_basic_map_peek_space(bmap
);
4469 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
4470 isl_size size
= isl_space_dim(space
, t
);
4472 dim_map
= isl_dim_map_free(dim_map
);
4474 isl_dim_map_dim_range(dim_map
, space
, t
,
4477 isl_dim_map_dim_range(dim_map
, space
, t
,
4478 first
, n
, total
- bmap
->n_div
- n
);
4479 isl_dim_map_dim_range(dim_map
, space
, t
,
4480 first
+ n
, size
- (first
+ n
), off
);
4481 off
+= size
- (first
+ n
);
4483 isl_dim_map_dim(dim_map
, space
, t
, off
);
4487 isl_dim_map_div(dim_map
, bmap
, off
+ n
);
4489 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
4490 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
4491 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
4495 /* Insert "n" rows in the divs of "bmap".
4497 * The number of columns is not changed, which means that the last
4498 * dimensions of "bmap" are being reintepreted as the new divs.
4499 * The space of "bmap" is not adjusted, however, which means
4500 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
4501 * from the space of "bmap" is the responsibility of the caller.
4503 static __isl_give isl_basic_map
*insert_div_rows(__isl_take isl_basic_map
*bmap
,
4511 bmap
= isl_basic_map_cow(bmap
);
4515 row_size
= isl_basic_map_offset(bmap
, isl_dim_div
) + bmap
->extra
;
4516 old
= bmap
->block2
.data
;
4517 bmap
->block2
= isl_blk_extend(bmap
->ctx
, bmap
->block2
,
4518 (bmap
->extra
+ n
) * (1 + row_size
));
4519 if (!bmap
->block2
.data
)
4520 return isl_basic_map_free(bmap
);
4521 new_div
= isl_alloc_array(bmap
->ctx
, isl_int
*, bmap
->extra
+ n
);
4523 return isl_basic_map_free(bmap
);
4524 for (i
= 0; i
< n
; ++i
) {
4525 new_div
[i
] = bmap
->block2
.data
+
4526 (bmap
->extra
+ i
) * (1 + row_size
);
4527 isl_seq_clr(new_div
[i
], 1 + row_size
);
4529 for (i
= 0; i
< bmap
->extra
; ++i
)
4530 new_div
[n
+ i
] = bmap
->block2
.data
+ (bmap
->div
[i
] - old
);
4532 bmap
->div
= new_div
;
4539 /* Drop constraints from "bmap" that only involve the variables
4540 * of "type" in the range [first, first + n] that are not related
4541 * to any of the variables outside that interval.
4542 * These constraints cannot influence the values for the variables
4543 * outside the interval, except in case they cause "bmap" to be empty.
4544 * Only drop the constraints if "bmap" is known to be non-empty.
4546 static __isl_give isl_basic_map
*drop_irrelevant_constraints(
4547 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
4548 unsigned first
, unsigned n
)
4552 isl_size dim
, n_div
;
4555 non_empty
= isl_basic_map_plain_is_non_empty(bmap
);
4557 return isl_basic_map_free(bmap
);
4561 dim
= isl_basic_map_dim(bmap
, isl_dim_all
);
4562 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4563 if (dim
< 0 || n_div
< 0)
4564 return isl_basic_map_free(bmap
);
4565 groups
= isl_calloc_array(isl_basic_map_get_ctx(bmap
), int, dim
);
4567 return isl_basic_map_free(bmap
);
4568 first
+= isl_basic_map_offset(bmap
, type
) - 1;
4569 for (i
= 0; i
< first
; ++i
)
4571 for (i
= first
+ n
; i
< dim
- n_div
; ++i
)
4574 bmap
= isl_basic_map_drop_unrelated_constraints(bmap
, groups
);
4579 /* Turn the n dimensions of type type, starting at first
4580 * into existentially quantified variables.
4582 * If a subset of the projected out variables are unrelated
4583 * to any of the variables that remain, then the constraints
4584 * involving this subset are simply dropped first.
4586 __isl_give isl_basic_map
*isl_basic_map_project_out(
4587 __isl_take isl_basic_map
*bmap
,
4588 enum isl_dim_type type
, unsigned first
, unsigned n
)
4594 return basic_map_space_reset(bmap
, type
);
4595 if (type
== isl_dim_div
)
4596 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
4597 "cannot project out existentially quantified variables",
4598 return isl_basic_map_free(bmap
));
4600 empty
= isl_basic_map_plain_is_empty(bmap
);
4602 return isl_basic_map_free(bmap
);
4604 bmap
= isl_basic_map_set_to_empty(bmap
);
4606 bmap
= drop_irrelevant_constraints(bmap
, type
, first
, n
);
4610 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
4611 return isl_basic_map_remove_dims(bmap
, type
, first
, n
);
4613 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
4614 return isl_basic_map_free(bmap
);
4616 bmap
= move_last(bmap
, type
, first
, n
);
4617 bmap
= isl_basic_map_cow(bmap
);
4618 bmap
= insert_div_rows(bmap
, n
);
4620 space
= isl_basic_map_take_space(bmap
);
4621 space
= isl_space_drop_dims(space
, type
, first
, n
);
4622 bmap
= isl_basic_map_restore_space(bmap
, space
);
4623 bmap
= isl_basic_map_simplify(bmap
);
4624 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
4625 return isl_basic_map_finalize(bmap
);
4628 /* Turn the n dimensions of type type, starting at first
4629 * into existentially quantified variables.
4631 __isl_give isl_basic_set
*isl_basic_set_project_out(
4632 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
,
4633 unsigned first
, unsigned n
)
4635 return bset_from_bmap(isl_basic_map_project_out(bset_to_bmap(bset
),
4639 /* Turn the n dimensions of type type, starting at first
4640 * into existentially quantified variables.
4642 __isl_give isl_map
*isl_map_project_out(__isl_take isl_map
*map
,
4643 enum isl_dim_type type
, unsigned first
, unsigned n
)
4649 return map_space_reset(map
, type
);
4651 if (isl_map_check_range(map
, type
, first
, n
) < 0)
4652 return isl_map_free(map
);
4654 map
= isl_map_cow(map
);
4658 for (i
= 0; i
< map
->n
; ++i
) {
4659 map
->p
[i
] = isl_basic_map_project_out(map
->p
[i
], type
, first
, n
);
4665 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
4666 map
= isl_map_unmark_normalized(map
);
4668 space
= isl_map_take_space(map
);
4669 space
= isl_space_drop_dims(space
, type
, first
, n
);
4670 map
= isl_map_restore_space(map
, space
);
4679 #define TYPE isl_map
4680 #include "isl_project_out_all_params_templ.c"
4681 #include "isl_project_out_param_templ.c"
4683 /* Turn all the dimensions of type "type", except the "n" starting at "first"
4684 * into existentially quantified variables.
4686 __isl_give isl_map
*isl_map_project_onto(__isl_take isl_map
*map
,
4687 enum isl_dim_type type
, unsigned first
, unsigned n
)
4691 dim
= isl_map_dim(map
, type
);
4692 if (isl_map_check_range(map
, type
, first
, n
) < 0 || dim
< 0)
4693 return isl_map_free(map
);
4694 map
= isl_map_project_out(map
, type
, first
+ n
, dim
- (first
+ n
));
4695 map
= isl_map_project_out(map
, type
, 0, first
);
4699 /* Turn the n dimensions of type type, starting at first
4700 * into existentially quantified variables.
4702 __isl_give isl_set
*isl_set_project_out(__isl_take isl_set
*set
,
4703 enum isl_dim_type type
, unsigned first
, unsigned n
)
4705 return set_from_map(isl_map_project_out(set_to_map(set
),
4709 /* If "set" involves a parameter with identifier "id",
4710 * then turn it into an existentially quantified variable.
4712 __isl_give isl_set
*isl_set_project_out_param_id(__isl_take isl_set
*set
,
4713 __isl_take isl_id
*id
)
4715 return set_from_map(isl_map_project_out_param_id(set_to_map(set
), id
));
4718 /* If "set" involves any of the parameters with identifiers in "list",
4719 * then turn them into existentially quantified variables.
4721 __isl_give isl_set
*isl_set_project_out_param_id_list(__isl_take isl_set
*set
,
4722 __isl_take isl_id_list
*list
)
4726 map
= set_to_map(set
);
4727 map
= isl_map_project_out_param_id_list(map
, list
);
4728 return set_from_map(map
);
4731 /* Project out all parameters from "set" by existentially quantifying
4734 __isl_give isl_set
*isl_set_project_out_all_params(__isl_take isl_set
*set
)
4736 return set_from_map(isl_map_project_out_all_params(set_to_map(set
)));
4739 /* Return a map that projects the elements in "set" onto their
4740 * "n" set dimensions starting at "first".
4741 * "type" should be equal to isl_dim_set.
4743 __isl_give isl_map
*isl_set_project_onto_map(__isl_take isl_set
*set
,
4744 enum isl_dim_type type
, unsigned first
, unsigned n
)
4749 if (type
!= isl_dim_set
)
4750 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
4751 "only set dimensions can be projected out", goto error
);
4752 if (isl_set_check_range(set
, type
, first
, n
) < 0)
4753 return isl_set_free(set
);
4755 map
= isl_map_from_domain(set
);
4756 map
= isl_map_add_dims(map
, isl_dim_out
, n
);
4757 for (i
= 0; i
< n
; ++i
)
4758 map
= isl_map_equate(map
, isl_dim_in
, first
+ i
,
4766 static __isl_give isl_basic_map
*add_divs(__isl_take isl_basic_map
*bmap
,
4772 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4774 return isl_basic_map_free(bmap
);
4775 for (i
= 0; i
< n
; ++i
) {
4776 j
= isl_basic_map_alloc_div(bmap
);
4779 isl_seq_clr(bmap
->div
[j
], 1 + 1 + total
);
4783 isl_basic_map_free(bmap
);
4787 /* Does "bmap2" apply to the range of "bmap1" (ignoring parameters)?
4789 isl_bool
isl_basic_map_applies_range(__isl_keep isl_basic_map
*bmap1
,
4790 __isl_keep isl_basic_map
*bmap2
)
4792 isl_space
*space1
, *space2
;
4794 space1
= isl_basic_map_peek_space(bmap1
);
4795 space2
= isl_basic_map_peek_space(bmap2
);
4796 return isl_space_tuple_is_equal(space1
, isl_dim_out
,
4797 space2
, isl_dim_in
);
4800 /* Check that "bmap2" applies to the range of "bmap1" (ignoring parameters).
4802 static isl_stat
isl_basic_map_check_applies_range(
4803 __isl_keep isl_basic_map
*bmap1
, __isl_keep isl_basic_map
*bmap2
)
4807 equal
= isl_basic_map_applies_range(bmap1
, bmap2
);
4809 return isl_stat_error
;
4811 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
4812 "spaces don't match", return isl_stat_error
);
4816 __isl_give isl_basic_map
*isl_basic_map_apply_range(
4817 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
4819 isl_space
*space_result
= NULL
;
4820 struct isl_basic_map
*bmap
;
4821 isl_size n_in
, n_out
, n
, nparam
;
4822 unsigned total
, pos
;
4823 struct isl_dim_map
*dim_map1
, *dim_map2
;
4825 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
4827 if (isl_basic_map_check_applies_range(bmap1
, bmap2
) < 0)
4830 n_in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
4831 n_out
= isl_basic_map_dim(bmap2
, isl_dim_out
);
4832 n
= isl_basic_map_dim(bmap1
, isl_dim_out
);
4833 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
4834 if (n_in
< 0 || n_out
< 0 || n
< 0 || nparam
< 0)
4837 space_result
= isl_space_join(isl_basic_map_get_space(bmap1
),
4838 isl_basic_map_get_space(bmap2
));
4840 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ n
;
4841 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4842 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4843 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
4844 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
4845 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
4846 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_in
);
4847 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_out
);
4848 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
4849 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
4850 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
4852 bmap
= isl_basic_map_alloc_space(space_result
,
4853 bmap1
->n_div
+ bmap2
->n_div
+ n
,
4854 bmap1
->n_eq
+ bmap2
->n_eq
,
4855 bmap1
->n_ineq
+ bmap2
->n_ineq
);
4856 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
4857 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
4858 bmap
= add_divs(bmap
, n
);
4859 bmap
= isl_basic_map_simplify(bmap
);
4860 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
4861 return isl_basic_map_finalize(bmap
);
4863 isl_basic_map_free(bmap1
);
4864 isl_basic_map_free(bmap2
);
4868 __isl_give isl_basic_set
*isl_basic_set_apply(__isl_take isl_basic_set
*bset
,
4869 __isl_take isl_basic_map
*bmap
)
4871 if (isl_basic_map_check_compatible_domain(bmap
, bset
) < 0)
4874 return bset_from_bmap(isl_basic_map_apply_range(bset_to_bmap(bset
),
4877 isl_basic_set_free(bset
);
4878 isl_basic_map_free(bmap
);
4882 __isl_give isl_basic_map
*isl_basic_map_apply_domain(
4883 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
4885 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
4887 if (!isl_space_tuple_is_equal(bmap1
->dim
, isl_dim_in
,
4888 bmap2
->dim
, isl_dim_in
))
4889 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
4890 "spaces don't match", goto error
);
4892 bmap1
= isl_basic_map_reverse(bmap1
);
4893 bmap1
= isl_basic_map_apply_range(bmap1
, bmap2
);
4894 return isl_basic_map_reverse(bmap1
);
4896 isl_basic_map_free(bmap1
);
4897 isl_basic_map_free(bmap2
);
4901 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
4902 * A \cap B -> f(A) + f(B)
4904 __isl_give isl_basic_map
*isl_basic_map_sum(__isl_take isl_basic_map
*bmap1
,
4905 __isl_take isl_basic_map
*bmap2
)
4907 isl_size n_in
, n_out
, nparam
;
4908 unsigned total
, pos
;
4909 struct isl_basic_map
*bmap
= NULL
;
4910 struct isl_dim_map
*dim_map1
, *dim_map2
;
4913 if (isl_basic_map_check_equal_space(bmap1
, bmap2
) < 0)
4916 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
4917 n_in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
4918 n_out
= isl_basic_map_dim(bmap1
, isl_dim_out
);
4919 if (nparam
< 0 || n_in
< 0 || n_out
< 0)
4922 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
;
4923 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4924 dim_map2
= isl_dim_map_alloc(bmap2
->ctx
, total
);
4925 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
4926 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
);
4927 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
4928 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
4929 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_in
+ n_out
);
4930 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
4931 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
4932 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_out
);
4934 bmap
= isl_basic_map_alloc_space(isl_space_copy(bmap1
->dim
),
4935 bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
,
4936 bmap1
->n_eq
+ bmap2
->n_eq
+ n_out
,
4937 bmap1
->n_ineq
+ bmap2
->n_ineq
);
4938 for (i
= 0; i
< n_out
; ++i
) {
4939 int j
= isl_basic_map_alloc_equality(bmap
);
4942 isl_seq_clr(bmap
->eq
[j
], 1+total
);
4943 isl_int_set_si(bmap
->eq
[j
][1+nparam
+n_in
+i
], -1);
4944 isl_int_set_si(bmap
->eq
[j
][1+pos
+i
], 1);
4945 isl_int_set_si(bmap
->eq
[j
][1+pos
-n_out
+i
], 1);
4947 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
4948 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
4949 bmap
= add_divs(bmap
, 2 * n_out
);
4951 bmap
= isl_basic_map_simplify(bmap
);
4952 return isl_basic_map_finalize(bmap
);
4954 isl_basic_map_free(bmap
);
4955 isl_basic_map_free(bmap1
);
4956 isl_basic_map_free(bmap2
);
4960 /* Given two maps A -> f(A) and B -> g(B), construct a map
4961 * A \cap B -> f(A) + f(B)
4963 __isl_give isl_map
*isl_map_sum(__isl_take isl_map
*map1
,
4964 __isl_take isl_map
*map2
)
4966 struct isl_map
*result
;
4969 if (isl_map_check_equal_space(map1
, map2
) < 0)
4972 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
4973 map1
->n
* map2
->n
, 0);
4976 for (i
= 0; i
< map1
->n
; ++i
)
4977 for (j
= 0; j
< map2
->n
; ++j
) {
4978 struct isl_basic_map
*part
;
4979 part
= isl_basic_map_sum(
4980 isl_basic_map_copy(map1
->p
[i
]),
4981 isl_basic_map_copy(map2
->p
[j
]));
4982 if (isl_basic_map_is_empty(part
))
4983 isl_basic_map_free(part
);
4985 result
= isl_map_add_basic_map(result
, part
);
4998 __isl_give isl_set
*isl_set_sum(__isl_take isl_set
*set1
,
4999 __isl_take isl_set
*set2
)
5001 return set_from_map(isl_map_sum(set_to_map(set1
), set_to_map(set2
)));
5004 /* Given a basic map A -> f(A), construct A -> -f(A).
5006 __isl_give isl_basic_map
*isl_basic_map_neg(__isl_take isl_basic_map
*bmap
)
5012 bmap
= isl_basic_map_cow(bmap
);
5013 n
= isl_basic_map_dim(bmap
, isl_dim_out
);
5015 return isl_basic_map_free(bmap
);
5017 off
= isl_basic_map_offset(bmap
, isl_dim_out
);
5018 for (i
= 0; i
< bmap
->n_eq
; ++i
)
5019 for (j
= 0; j
< n
; ++j
)
5020 isl_int_neg(bmap
->eq
[i
][off
+j
], bmap
->eq
[i
][off
+j
]);
5021 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
5022 for (j
= 0; j
< n
; ++j
)
5023 isl_int_neg(bmap
->ineq
[i
][off
+j
], bmap
->ineq
[i
][off
+j
]);
5024 for (i
= 0; i
< bmap
->n_div
; ++i
)
5025 for (j
= 0; j
< n
; ++j
)
5026 isl_int_neg(bmap
->div
[i
][1+off
+j
], bmap
->div
[i
][1+off
+j
]);
5027 bmap
= isl_basic_map_gauss(bmap
, NULL
);
5028 return isl_basic_map_finalize(bmap
);
5031 __isl_give isl_basic_set
*isl_basic_set_neg(__isl_take isl_basic_set
*bset
)
5033 return isl_basic_map_neg(bset
);
5036 /* Given a map A -> f(A), construct A -> -f(A).
5038 __isl_give isl_map
*isl_map_neg(__isl_take isl_map
*map
)
5042 map
= isl_map_cow(map
);
5046 for (i
= 0; i
< map
->n
; ++i
) {
5047 map
->p
[i
] = isl_basic_map_neg(map
->p
[i
]);
5058 __isl_give isl_set
*isl_set_neg(__isl_take isl_set
*set
)
5060 return set_from_map(isl_map_neg(set_to_map(set
)));
5063 /* Given a basic map A -> f(A) and an integer d, construct a basic map
5064 * A -> floor(f(A)/d).
5066 __isl_give isl_basic_map
*isl_basic_map_floordiv(__isl_take isl_basic_map
*bmap
,
5069 isl_size n_in
, n_out
, nparam
;
5070 unsigned total
, pos
;
5071 struct isl_basic_map
*result
= NULL
;
5072 struct isl_dim_map
*dim_map
;
5075 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5076 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5077 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5078 if (nparam
< 0 || n_in
< 0 || n_out
< 0)
5079 return isl_basic_map_free(bmap
);
5081 total
= nparam
+ n_in
+ n_out
+ bmap
->n_div
+ n_out
;
5082 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
5083 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_param
, pos
= 0);
5084 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_in
, pos
+= nparam
);
5085 isl_dim_map_div(dim_map
, bmap
, pos
+= n_in
+ n_out
);
5086 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_out
, pos
+= bmap
->n_div
);
5088 result
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
5089 bmap
->n_div
+ n_out
,
5090 bmap
->n_eq
, bmap
->n_ineq
+ 2 * n_out
);
5091 result
= isl_basic_map_add_constraints_dim_map(result
, bmap
, dim_map
);
5092 result
= add_divs(result
, n_out
);
5093 for (i
= 0; i
< n_out
; ++i
) {
5095 j
= isl_basic_map_alloc_inequality(result
);
5098 isl_seq_clr(result
->ineq
[j
], 1+total
);
5099 isl_int_neg(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
5100 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], 1);
5101 j
= isl_basic_map_alloc_inequality(result
);
5104 isl_seq_clr(result
->ineq
[j
], 1+total
);
5105 isl_int_set(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
5106 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], -1);
5107 isl_int_sub_ui(result
->ineq
[j
][0], d
, 1);
5110 result
= isl_basic_map_simplify(result
);
5111 return isl_basic_map_finalize(result
);
5113 isl_basic_map_free(result
);
5117 /* Given a map A -> f(A) and an integer d, construct a map
5118 * A -> floor(f(A)/d).
5120 __isl_give isl_map
*isl_map_floordiv(__isl_take isl_map
*map
, isl_int d
)
5124 map
= isl_map_cow(map
);
5128 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
5129 for (i
= 0; i
< map
->n
; ++i
) {
5130 map
->p
[i
] = isl_basic_map_floordiv(map
->p
[i
], d
);
5134 map
= isl_map_unmark_normalized(map
);
5142 /* Given a map A -> f(A) and an integer d, construct a map
5143 * A -> floor(f(A)/d).
5145 __isl_give isl_map
*isl_map_floordiv_val(__isl_take isl_map
*map
,
5146 __isl_take isl_val
*d
)
5150 if (!isl_val_is_int(d
))
5151 isl_die(isl_val_get_ctx(d
), isl_error_invalid
,
5152 "expecting integer denominator", goto error
);
5153 map
= isl_map_floordiv(map
, d
->n
);
5162 static __isl_give isl_basic_map
*var_equal(__isl_take isl_basic_map
*bmap
,
5170 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5171 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5172 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5173 if (total
< 0 || nparam
< 0 || n_in
< 0)
5174 return isl_basic_map_free(bmap
);
5175 i
= isl_basic_map_alloc_equality(bmap
);
5178 isl_seq_clr(bmap
->eq
[i
], 1 + total
);
5179 isl_int_set_si(bmap
->eq
[i
][1+nparam
+pos
], -1);
5180 isl_int_set_si(bmap
->eq
[i
][1+nparam
+n_in
+pos
], 1);
5181 return isl_basic_map_finalize(bmap
);
5183 isl_basic_map_free(bmap
);
5187 /* Add a constraint to "bmap" expressing i_pos < o_pos
5189 static __isl_give isl_basic_map
*var_less(__isl_take isl_basic_map
*bmap
,
5197 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5198 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5199 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5200 if (total
< 0 || nparam
< 0 || n_in
< 0)
5201 return isl_basic_map_free(bmap
);
5202 i
= isl_basic_map_alloc_inequality(bmap
);
5205 isl_seq_clr(bmap
->ineq
[i
], 1 + total
);
5206 isl_int_set_si(bmap
->ineq
[i
][0], -1);
5207 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
5208 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
5209 return isl_basic_map_finalize(bmap
);
5211 isl_basic_map_free(bmap
);
5215 /* Add a constraint to "bmap" expressing i_pos <= o_pos
5217 static __isl_give isl_basic_map
*var_less_or_equal(
5218 __isl_take isl_basic_map
*bmap
, unsigned pos
)
5225 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5226 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5227 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5228 if (total
< 0 || nparam
< 0 || n_in
< 0)
5229 return isl_basic_map_free(bmap
);
5230 i
= isl_basic_map_alloc_inequality(bmap
);
5233 isl_seq_clr(bmap
->ineq
[i
], 1 + total
);
5234 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
5235 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
5236 return isl_basic_map_finalize(bmap
);
5238 isl_basic_map_free(bmap
);
5242 /* Add a constraint to "bmap" expressing i_pos > o_pos
5244 static __isl_give isl_basic_map
*var_more(__isl_take isl_basic_map
*bmap
,
5252 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5253 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5254 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5255 if (total
< 0 || nparam
< 0 || n_in
< 0)
5256 return isl_basic_map_free(bmap
);
5257 i
= isl_basic_map_alloc_inequality(bmap
);
5260 isl_seq_clr(bmap
->ineq
[i
], 1 + total
);
5261 isl_int_set_si(bmap
->ineq
[i
][0], -1);
5262 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
5263 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
5264 return isl_basic_map_finalize(bmap
);
5266 isl_basic_map_free(bmap
);
5270 /* Add a constraint to "bmap" expressing i_pos >= o_pos
5272 static __isl_give isl_basic_map
*var_more_or_equal(
5273 __isl_take isl_basic_map
*bmap
, unsigned pos
)
5280 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5281 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5282 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5283 if (total
< 0 || nparam
< 0 || n_in
< 0)
5284 return isl_basic_map_free(bmap
);
5285 i
= isl_basic_map_alloc_inequality(bmap
);
5288 isl_seq_clr(bmap
->ineq
[i
], 1 + total
);
5289 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
5290 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
5291 return isl_basic_map_finalize(bmap
);
5293 isl_basic_map_free(bmap
);
5297 __isl_give isl_basic_map
*isl_basic_map_equal(
5298 __isl_take isl_space
*space
, unsigned n_equal
)
5301 struct isl_basic_map
*bmap
;
5302 bmap
= isl_basic_map_alloc_space(space
, 0, n_equal
, 0);
5305 for (i
= 0; i
< n_equal
&& bmap
; ++i
)
5306 bmap
= var_equal(bmap
, i
);
5307 return isl_basic_map_finalize(bmap
);
5310 /* Return a relation on of dimension "space" expressing i_[0..pos] << o_[0..pos]
5312 __isl_give isl_basic_map
*isl_basic_map_less_at(__isl_take isl_space
*space
,
5316 struct isl_basic_map
*bmap
;
5317 bmap
= isl_basic_map_alloc_space(space
, 0, pos
, 1);
5320 for (i
= 0; i
< pos
&& bmap
; ++i
)
5321 bmap
= var_equal(bmap
, i
);
5323 bmap
= var_less(bmap
, pos
);
5324 return isl_basic_map_finalize(bmap
);
5327 /* Return a relation on "space" expressing i_[0..pos] <<= o_[0..pos]
5329 __isl_give isl_basic_map
*isl_basic_map_less_or_equal_at(
5330 __isl_take isl_space
*space
, unsigned pos
)
5333 isl_basic_map
*bmap
;
5335 bmap
= isl_basic_map_alloc_space(space
, 0, pos
, 1);
5336 for (i
= 0; i
< pos
; ++i
)
5337 bmap
= var_equal(bmap
, i
);
5338 bmap
= var_less_or_equal(bmap
, pos
);
5339 return isl_basic_map_finalize(bmap
);
5342 /* Return a relation on "space" expressing i_pos > o_pos
5344 __isl_give isl_basic_map
*isl_basic_map_more_at(__isl_take isl_space
*space
,
5348 struct isl_basic_map
*bmap
;
5349 bmap
= isl_basic_map_alloc_space(space
, 0, pos
, 1);
5352 for (i
= 0; i
< pos
&& bmap
; ++i
)
5353 bmap
= var_equal(bmap
, i
);
5355 bmap
= var_more(bmap
, pos
);
5356 return isl_basic_map_finalize(bmap
);
5359 /* Return a relation on "space" expressing i_[0..pos] >>= o_[0..pos]
5361 __isl_give isl_basic_map
*isl_basic_map_more_or_equal_at(
5362 __isl_take isl_space
*space
, unsigned pos
)
5365 isl_basic_map
*bmap
;
5367 bmap
= isl_basic_map_alloc_space(space
, 0, pos
, 1);
5368 for (i
= 0; i
< pos
; ++i
)
5369 bmap
= var_equal(bmap
, i
);
5370 bmap
= var_more_or_equal(bmap
, pos
);
5371 return isl_basic_map_finalize(bmap
);
5374 static __isl_give isl_map
*map_lex_lte_first(__isl_take isl_space
*space
,
5375 unsigned n
, int equal
)
5377 struct isl_map
*map
;
5380 if (n
== 0 && equal
)
5381 return isl_map_universe(space
);
5383 map
= isl_map_alloc_space(isl_space_copy(space
), n
, ISL_MAP_DISJOINT
);
5385 for (i
= 0; i
+ 1 < n
; ++i
)
5386 map
= isl_map_add_basic_map(map
,
5387 isl_basic_map_less_at(isl_space_copy(space
), i
));
5390 map
= isl_map_add_basic_map(map
,
5391 isl_basic_map_less_or_equal_at(space
, n
- 1));
5393 map
= isl_map_add_basic_map(map
,
5394 isl_basic_map_less_at(space
, n
- 1));
5396 isl_space_free(space
);
5401 static __isl_give isl_map
*map_lex_lte(__isl_take isl_space
*space
, int equal
)
5405 return map_lex_lte_first(space
, space
->n_out
, equal
);
5408 __isl_give isl_map
*isl_map_lex_lt_first(__isl_take isl_space
*space
,
5411 return map_lex_lte_first(space
, n
, 0);
5414 __isl_give isl_map
*isl_map_lex_le_first(__isl_take isl_space
*space
,
5417 return map_lex_lte_first(space
, n
, 1);
5420 __isl_give isl_map
*isl_map_lex_lt(__isl_take isl_space
*set_space
)
5422 return map_lex_lte(isl_space_map_from_set(set_space
), 0);
5425 __isl_give isl_map
*isl_map_lex_le(__isl_take isl_space
*set_space
)
5427 return map_lex_lte(isl_space_map_from_set(set_space
), 1);
5430 static __isl_give isl_map
*map_lex_gte_first(__isl_take isl_space
*space
,
5431 unsigned n
, int equal
)
5433 struct isl_map
*map
;
5436 if (n
== 0 && equal
)
5437 return isl_map_universe(space
);
5439 map
= isl_map_alloc_space(isl_space_copy(space
), n
, ISL_MAP_DISJOINT
);
5441 for (i
= 0; i
+ 1 < n
; ++i
)
5442 map
= isl_map_add_basic_map(map
,
5443 isl_basic_map_more_at(isl_space_copy(space
), i
));
5446 map
= isl_map_add_basic_map(map
,
5447 isl_basic_map_more_or_equal_at(space
, n
- 1));
5449 map
= isl_map_add_basic_map(map
,
5450 isl_basic_map_more_at(space
, n
- 1));
5452 isl_space_free(space
);
5457 static __isl_give isl_map
*map_lex_gte(__isl_take isl_space
*space
, int equal
)
5461 return map_lex_gte_first(space
, space
->n_out
, equal
);
5464 __isl_give isl_map
*isl_map_lex_gt_first(__isl_take isl_space
*space
,
5467 return map_lex_gte_first(space
, n
, 0);
5470 __isl_give isl_map
*isl_map_lex_ge_first(__isl_take isl_space
*space
,
5473 return map_lex_gte_first(space
, n
, 1);
5476 __isl_give isl_map
*isl_map_lex_gt(__isl_take isl_space
*set_space
)
5478 return map_lex_gte(isl_space_map_from_set(set_space
), 0);
5481 __isl_give isl_map
*isl_map_lex_ge(__isl_take isl_space
*set_space
)
5483 return map_lex_gte(isl_space_map_from_set(set_space
), 1);
5486 __isl_give isl_map
*isl_set_lex_le_set(__isl_take isl_set
*set1
,
5487 __isl_take isl_set
*set2
)
5490 map
= isl_map_lex_le(isl_set_get_space(set1
));
5491 map
= isl_map_intersect_domain(map
, set1
);
5492 map
= isl_map_intersect_range(map
, set2
);
5496 __isl_give isl_map
*isl_set_lex_lt_set(__isl_take isl_set
*set1
,
5497 __isl_take isl_set
*set2
)
5500 map
= isl_map_lex_lt(isl_set_get_space(set1
));
5501 map
= isl_map_intersect_domain(map
, set1
);
5502 map
= isl_map_intersect_range(map
, set2
);
5506 __isl_give isl_map
*isl_set_lex_ge_set(__isl_take isl_set
*set1
,
5507 __isl_take isl_set
*set2
)
5510 map
= isl_map_lex_ge(isl_set_get_space(set1
));
5511 map
= isl_map_intersect_domain(map
, set1
);
5512 map
= isl_map_intersect_range(map
, set2
);
5516 __isl_give isl_map
*isl_set_lex_gt_set(__isl_take isl_set
*set1
,
5517 __isl_take isl_set
*set2
)
5520 map
= isl_map_lex_gt(isl_set_get_space(set1
));
5521 map
= isl_map_intersect_domain(map
, set1
);
5522 map
= isl_map_intersect_range(map
, set2
);
5526 __isl_give isl_map
*isl_map_lex_le_map(__isl_take isl_map
*map1
,
5527 __isl_take isl_map
*map2
)
5530 map
= isl_map_lex_le(isl_space_range(isl_map_get_space(map1
)));
5531 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
5532 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
5536 __isl_give isl_map
*isl_map_lex_lt_map(__isl_take isl_map
*map1
,
5537 __isl_take isl_map
*map2
)
5540 map
= isl_map_lex_lt(isl_space_range(isl_map_get_space(map1
)));
5541 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
5542 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
5546 __isl_give isl_map
*isl_map_lex_ge_map(__isl_take isl_map
*map1
,
5547 __isl_take isl_map
*map2
)
5550 map
= isl_map_lex_ge(isl_space_range(isl_map_get_space(map1
)));
5551 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
5552 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
5556 __isl_give isl_map
*isl_map_lex_gt_map(__isl_take isl_map
*map1
,
5557 __isl_take isl_map
*map2
)
5560 map
= isl_map_lex_gt(isl_space_range(isl_map_get_space(map1
)));
5561 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
5562 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
5566 /* For the div d = floor(f/m) at position "div", add the constraint
5570 static __isl_give isl_basic_map
*add_upper_div_constraint(
5571 __isl_take isl_basic_map
*bmap
, unsigned div
)
5574 isl_size v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
5578 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
5579 if (v_div
< 0 || n_div
< 0)
5580 return isl_basic_map_free(bmap
);
5582 i
= isl_basic_map_alloc_inequality(bmap
);
5584 return isl_basic_map_free(bmap
);
5585 isl_seq_cpy(bmap
->ineq
[i
], bmap
->div
[div
] + 1, 1 + v_div
+ n_div
);
5586 isl_int_neg(bmap
->ineq
[i
][1 + pos
], bmap
->div
[div
][0]);
5591 /* For the div d = floor(f/m) at position "div", add the constraint
5593 * -(f-(m-1)) + m d >= 0
5595 static __isl_give isl_basic_map
*add_lower_div_constraint(
5596 __isl_take isl_basic_map
*bmap
, unsigned div
)
5599 isl_size v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
5603 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
5604 if (v_div
< 0 || n_div
< 0)
5605 return isl_basic_map_free(bmap
);
5607 i
= isl_basic_map_alloc_inequality(bmap
);
5609 return isl_basic_map_free(bmap
);
5610 isl_seq_neg(bmap
->ineq
[i
], bmap
->div
[div
] + 1, 1 + v_div
+ n_div
);
5611 isl_int_set(bmap
->ineq
[i
][1 + pos
], bmap
->div
[div
][0]);
5612 isl_int_add(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], bmap
->ineq
[i
][1 + pos
]);
5613 isl_int_sub_ui(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], 1);
5618 /* For the div d = floor(f/m) at position "pos", add the constraints
5621 * -(f-(m-1)) + m d >= 0
5623 * Note that the second constraint is the negation of
5627 __isl_give isl_basic_map
*isl_basic_map_add_div_constraints(
5628 __isl_take isl_basic_map
*bmap
, unsigned pos
)
5630 bmap
= add_upper_div_constraint(bmap
, pos
);
5631 bmap
= add_lower_div_constraint(bmap
, pos
);
5635 /* For each known div d = floor(f/m), add the constraints
5638 * -(f-(m-1)) + m d >= 0
5640 * Remove duplicate constraints in case of some these div constraints
5641 * already appear in "bmap".
5643 __isl_give isl_basic_map
*isl_basic_map_add_known_div_constraints(
5644 __isl_take isl_basic_map
*bmap
)
5648 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
5650 return isl_basic_map_free(bmap
);
5654 bmap
= add_known_div_constraints(bmap
);
5655 bmap
= isl_basic_map_remove_duplicate_constraints(bmap
, NULL
, 0);
5656 bmap
= isl_basic_map_finalize(bmap
);
5660 /* Add the div constraint of sign "sign" for div "div" of "bmap".
5662 * In particular, if this div is of the form d = floor(f/m),
5663 * then add the constraint
5667 * if sign < 0 or the constraint
5669 * -(f-(m-1)) + m d >= 0
5673 __isl_give isl_basic_map
*isl_basic_map_add_div_constraint(
5674 __isl_take isl_basic_map
*bmap
, unsigned div
, int sign
)
5677 return add_upper_div_constraint(bmap
, div
);
5679 return add_lower_div_constraint(bmap
, div
);
5682 __isl_give isl_basic_set
*isl_basic_map_underlying_set(
5683 __isl_take isl_basic_map
*bmap
)
5689 if (bmap
->dim
->nparam
== 0 && bmap
->dim
->n_in
== 0 &&
5691 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_in
) &&
5692 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_out
))
5693 return bset_from_bmap(bmap
);
5694 bmap
= isl_basic_map_cow(bmap
);
5697 space
= isl_basic_map_take_space(bmap
);
5698 space
= isl_space_underlying(space
, bmap
->n_div
);
5699 bmap
= isl_basic_map_restore_space(bmap
, space
);
5702 bmap
->extra
-= bmap
->n_div
;
5704 bmap
= isl_basic_map_finalize(bmap
);
5705 return bset_from_bmap(bmap
);
5707 isl_basic_map_free(bmap
);
5711 __isl_give isl_basic_set
*isl_basic_set_underlying_set(
5712 __isl_take isl_basic_set
*bset
)
5714 return isl_basic_map_underlying_set(bset_to_bmap(bset
));
5717 /* Replace each element in "list" by the result of applying
5718 * isl_basic_map_underlying_set to the element.
5720 __isl_give isl_basic_set_list
*isl_basic_map_list_underlying_set(
5721 __isl_take isl_basic_map_list
*list
)
5726 n
= isl_basic_map_list_n_basic_map(list
);
5730 for (i
= 0; i
< n
; ++i
) {
5731 isl_basic_map
*bmap
;
5732 isl_basic_set
*bset
;
5734 bmap
= isl_basic_map_list_get_basic_map(list
, i
);
5735 bset
= isl_basic_set_underlying_set(bmap
);
5736 list
= isl_basic_set_list_set_basic_set(list
, i
, bset
);
5741 isl_basic_map_list_free(list
);
5745 __isl_give isl_basic_map
*isl_basic_map_overlying_set(
5746 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_map
*like
)
5748 struct isl_basic_map
*bmap
;
5749 struct isl_ctx
*ctx
;
5750 isl_size dim
, bmap_total
;
5757 if (isl_basic_set_check_no_params(bset
) < 0 ||
5758 isl_basic_set_check_no_locals(bset
) < 0)
5760 dim
= isl_basic_set_dim(bset
, isl_dim_set
);
5761 bmap_total
= isl_basic_map_dim(like
, isl_dim_all
);
5762 if (dim
< 0 || bmap_total
< 0)
5764 isl_assert(ctx
, dim
== bmap_total
, goto error
);
5765 if (like
->n_div
== 0) {
5766 isl_space
*space
= isl_basic_map_get_space(like
);
5767 isl_basic_map_free(like
);
5768 return isl_basic_map_reset_space(bset
, space
);
5770 bset
= isl_basic_set_cow(bset
);
5773 total
= dim
+ bset
->extra
;
5774 bmap
= bset_to_bmap(bset
);
5775 isl_space_free(isl_basic_map_take_space(bmap
));
5776 bmap
= isl_basic_map_restore_space(bmap
, isl_basic_map_get_space(like
));
5779 bmap
->n_div
= like
->n_div
;
5780 bmap
->extra
+= like
->n_div
;
5784 ltotal
= total
- bmap
->extra
+ like
->extra
;
5787 bmap
->block2
= isl_blk_extend(ctx
, bmap
->block2
,
5788 bmap
->extra
* (1 + 1 + total
));
5789 if (isl_blk_is_error(bmap
->block2
))
5791 div
= isl_realloc_array(ctx
, bmap
->div
, isl_int
*, bmap
->extra
);
5795 for (i
= 0; i
< bmap
->extra
; ++i
)
5796 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + 1 + total
);
5797 for (i
= 0; i
< like
->n_div
; ++i
) {
5798 isl_seq_cpy(bmap
->div
[i
], like
->div
[i
], 1 + 1 + ltotal
);
5799 isl_seq_clr(bmap
->div
[i
]+1+1+ltotal
, total
- ltotal
);
5801 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
5803 isl_basic_map_free(like
);
5804 bmap
= isl_basic_map_simplify(bmap
);
5805 bmap
= isl_basic_map_finalize(bmap
);
5808 isl_basic_map_free(like
);
5809 isl_basic_set_free(bset
);
5813 __isl_give isl_basic_set
*isl_basic_set_from_underlying_set(
5814 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*like
)
5816 return bset_from_bmap(isl_basic_map_overlying_set(bset
,
5817 bset_to_bmap(like
)));
5820 __isl_give isl_set
*isl_map_underlying_set(__isl_take isl_map
*map
)
5824 map
= isl_map_cow(map
);
5827 map
->dim
= isl_space_cow(map
->dim
);
5831 for (i
= 1; i
< map
->n
; ++i
)
5832 isl_assert(map
->ctx
, map
->p
[0]->n_div
== map
->p
[i
]->n_div
,
5834 for (i
= 0; i
< map
->n
; ++i
) {
5835 map
->p
[i
] = bset_to_bmap(
5836 isl_basic_map_underlying_set(map
->p
[i
]));
5841 map
->dim
= isl_space_underlying(map
->dim
, 0);
5843 isl_space_free(map
->dim
);
5844 map
->dim
= isl_space_copy(map
->p
[0]->dim
);
5848 return set_from_map(map
);
5854 /* Replace the space of "bmap" by "space".
5856 * If the space of "bmap" is identical to "space" (including the identifiers
5857 * of the input and output dimensions), then simply return the original input.
5859 __isl_give isl_basic_map
*isl_basic_map_reset_space(
5860 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*space
)
5863 isl_space
*bmap_space
;
5865 bmap_space
= isl_basic_map_peek_space(bmap
);
5866 equal
= isl_space_is_equal(bmap_space
, space
);
5867 if (equal
>= 0 && equal
)
5868 equal
= isl_space_has_equal_ids(bmap_space
, space
);
5872 isl_space_free(space
);
5875 isl_space_free(isl_basic_map_take_space(bmap
));
5876 bmap
= isl_basic_map_restore_space(bmap
, space
);
5878 bmap
= isl_basic_map_finalize(bmap
);
5882 isl_basic_map_free(bmap
);
5883 isl_space_free(space
);
5887 __isl_give isl_basic_set
*isl_basic_set_reset_space(
5888 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*space
)
5890 return bset_from_bmap(isl_basic_map_reset_space(bset_to_bmap(bset
),
5894 /* Check that the total dimensions of "map" and "space" are the same.
5896 static isl_stat
check_map_space_equal_total_dim(__isl_keep isl_map
*map
,
5897 __isl_keep isl_space
*space
)
5899 isl_size dim1
, dim2
;
5901 dim1
= isl_map_dim(map
, isl_dim_all
);
5902 dim2
= isl_space_dim(space
, isl_dim_all
);
5903 if (dim1
< 0 || dim2
< 0)
5904 return isl_stat_error
;
5907 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
5908 "total dimensions do not match", return isl_stat_error
);
5911 __isl_give isl_map
*isl_map_reset_space(__isl_take isl_map
*map
,
5912 __isl_take isl_space
*space
)
5916 map
= isl_map_cow(map
);
5920 for (i
= 0; i
< map
->n
; ++i
) {
5921 map
->p
[i
] = isl_basic_map_reset_space(map
->p
[i
],
5922 isl_space_copy(space
));
5926 isl_space_free(isl_map_take_space(map
));
5927 map
= isl_map_restore_space(map
, space
);
5932 isl_space_free(space
);
5936 /* Replace the space of "map" by "space", without modifying
5937 * the dimension of "map".
5939 * If the space of "map" is identical to "space" (including the identifiers
5940 * of the input and output dimensions), then simply return the original input.
5942 __isl_give isl_map
*isl_map_reset_equal_dim_space(__isl_take isl_map
*map
,
5943 __isl_take isl_space
*space
)
5946 isl_space
*map_space
;
5948 map_space
= isl_map_peek_space(map
);
5949 equal
= isl_space_is_equal(map_space
, space
);
5950 if (equal
>= 0 && equal
)
5951 equal
= isl_space_has_equal_ids(map_space
, space
);
5955 isl_space_free(space
);
5958 if (check_map_space_equal_total_dim(map
, space
) < 0)
5960 return isl_map_reset_space(map
, space
);
5963 isl_space_free(space
);
5967 __isl_give isl_set
*isl_set_reset_space(__isl_take isl_set
*set
,
5968 __isl_take isl_space
*space
)
5970 return set_from_map(isl_map_reset_space(set_to_map(set
), space
));
5973 /* Compute the parameter domain of the given basic set.
5975 __isl_give isl_basic_set
*isl_basic_set_params(__isl_take isl_basic_set
*bset
)
5981 is_params
= isl_basic_set_is_params(bset
);
5983 return isl_basic_set_free(bset
);
5987 n
= isl_basic_set_dim(bset
, isl_dim_set
);
5989 return isl_basic_set_free(bset
);
5990 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
5991 space
= isl_basic_set_get_space(bset
);
5992 space
= isl_space_params(space
);
5993 bset
= isl_basic_set_reset_space(bset
, space
);
5997 /* Construct a zero-dimensional basic set with the given parameter domain.
5999 __isl_give isl_basic_set
*isl_basic_set_from_params(
6000 __isl_take isl_basic_set
*bset
)
6003 space
= isl_basic_set_get_space(bset
);
6004 space
= isl_space_set_from_params(space
);
6005 bset
= isl_basic_set_reset_space(bset
, space
);
6009 /* Compute the parameter domain of the given set.
6011 __isl_give isl_set
*isl_set_params(__isl_take isl_set
*set
)
6013 return isl_map_params(set_to_map(set
));
6016 /* Construct a zero-dimensional set with the given parameter domain.
6018 __isl_give isl_set
*isl_set_from_params(__isl_take isl_set
*set
)
6021 space
= isl_set_get_space(set
);
6022 space
= isl_space_set_from_params(space
);
6023 set
= isl_set_reset_space(set
, space
);
6027 /* Compute the parameter domain of the given map.
6029 __isl_give isl_set
*isl_map_params(__isl_take isl_map
*map
)
6032 isl_size n_in
, n_out
;
6034 n_in
= isl_map_dim(map
, isl_dim_in
);
6035 n_out
= isl_map_dim(map
, isl_dim_out
);
6036 if (n_in
< 0 || n_out
< 0)
6037 return isl_map_free(map
);
6038 map
= isl_map_project_out(map
, isl_dim_in
, 0, n_in
);
6039 map
= isl_map_project_out(map
, isl_dim_out
, 0, n_out
);
6040 space
= isl_map_get_space(map
);
6041 space
= isl_space_params(space
);
6042 map
= isl_map_reset_space(map
, space
);
6046 __isl_give isl_basic_set
*isl_basic_map_domain(__isl_take isl_basic_map
*bmap
)
6051 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
6053 return isl_basic_map_free(bmap
);
6054 space
= isl_space_domain(isl_basic_map_get_space(bmap
));
6056 bmap
= isl_basic_map_project_out(bmap
, isl_dim_out
, 0, n_out
);
6058 return isl_basic_map_reset_space(bmap
, space
);
6061 isl_bool
isl_basic_map_may_be_set(__isl_keep isl_basic_map
*bmap
)
6064 return isl_bool_error
;
6065 return isl_space_may_be_set(bmap
->dim
);
6068 /* Is this basic map actually a set?
6069 * Users should never call this function. Outside of isl,
6070 * the type should indicate whether something is a set or a map.
6072 isl_bool
isl_basic_map_is_set(__isl_keep isl_basic_map
*bmap
)
6075 return isl_bool_error
;
6076 return isl_space_is_set(bmap
->dim
);
6079 __isl_give isl_basic_set
*isl_basic_map_range(__isl_take isl_basic_map
*bmap
)
6083 is_set
= isl_basic_map_is_set(bmap
);
6088 return isl_basic_map_domain(isl_basic_map_reverse(bmap
));
6090 isl_basic_map_free(bmap
);
6094 __isl_give isl_basic_map
*isl_basic_map_domain_map(
6095 __isl_take isl_basic_map
*bmap
)
6099 isl_basic_map
*domain
;
6100 isl_size nparam
, n_in
, n_out
;
6102 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
6103 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
6104 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
6105 if (nparam
< 0 || n_in
< 0 || n_out
< 0)
6106 return isl_basic_map_free(bmap
);
6108 space
= isl_basic_map_get_space(bmap
);
6109 space
= isl_space_from_range(isl_space_domain(space
));
6110 domain
= isl_basic_map_universe(space
);
6112 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
6113 bmap
= isl_basic_map_apply_range(bmap
, domain
);
6114 bmap
= isl_basic_map_extend_constraints(bmap
, n_in
, 0);
6116 for (i
= 0; i
< n_in
; ++i
)
6117 bmap
= isl_basic_map_equate(bmap
, isl_dim_in
, i
,
6120 bmap
= isl_basic_map_gauss(bmap
, NULL
);
6121 return isl_basic_map_finalize(bmap
);
6124 __isl_give isl_basic_map
*isl_basic_map_range_map(
6125 __isl_take isl_basic_map
*bmap
)
6129 isl_basic_map
*range
;
6130 isl_size nparam
, n_in
, n_out
;
6132 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
6133 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
6134 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
6135 if (nparam
< 0 || n_in
< 0 || n_out
< 0)
6136 return isl_basic_map_free(bmap
);
6138 space
= isl_basic_map_get_space(bmap
);
6139 space
= isl_space_from_range(isl_space_range(space
));
6140 range
= isl_basic_map_universe(space
);
6142 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
6143 bmap
= isl_basic_map_apply_range(bmap
, range
);
6144 bmap
= isl_basic_map_extend_constraints(bmap
, n_out
, 0);
6146 for (i
= 0; i
< n_out
; ++i
)
6147 bmap
= isl_basic_map_equate(bmap
, isl_dim_in
, n_in
+ i
,
6150 bmap
= isl_basic_map_gauss(bmap
, NULL
);
6151 return isl_basic_map_finalize(bmap
);
6154 int isl_map_may_be_set(__isl_keep isl_map
*map
)
6158 return isl_space_may_be_set(map
->dim
);
6161 /* Is this map actually a set?
6162 * Users should never call this function. Outside of isl,
6163 * the type should indicate whether something is a set or a map.
6165 isl_bool
isl_map_is_set(__isl_keep isl_map
*map
)
6168 return isl_bool_error
;
6169 return isl_space_is_set(map
->dim
);
6172 __isl_give isl_set
*isl_map_range(__isl_take isl_map
*map
)
6177 n_in
= isl_map_dim(map
, isl_dim_in
);
6179 return set_from_map(isl_map_free(map
));
6180 space
= isl_space_range(isl_map_get_space(map
));
6182 map
= isl_map_project_out(map
, isl_dim_in
, 0, n_in
);
6184 return set_from_map(isl_map_reset_space(map
, space
));
6187 /* Transform "map" by applying "fn_space" to its space and "fn_bmap"
6188 * to each of its basic maps.
6190 static __isl_give isl_map
*isl_map_transform(__isl_take isl_map
*map
,
6191 __isl_give isl_space
*(*fn_space
)(__isl_take isl_space
*space
),
6192 __isl_give isl_basic_map
*(*fn_bmap
)(__isl_take isl_basic_map
*bmap
))
6197 map
= isl_map_cow(map
);
6201 for (i
= 0; i
< map
->n
; ++i
) {
6202 map
->p
[i
] = fn_bmap(map
->p
[i
]);
6204 return isl_map_free(map
);
6206 map
= isl_map_unmark_normalized(map
);
6208 space
= isl_map_take_space(map
);
6209 space
= fn_space(space
);
6210 map
= isl_map_restore_space(map
, space
);
6215 __isl_give isl_map
*isl_map_domain_map(__isl_take isl_map
*map
)
6217 return isl_map_transform(map
, &isl_space_domain_map
,
6218 &isl_basic_map_domain_map
);
6221 __isl_give isl_map
*isl_map_range_map(__isl_take isl_map
*map
)
6223 return isl_map_transform(map
, &isl_space_range_map
,
6224 &isl_basic_map_range_map
);
6227 /* Given a wrapped map of the form A[B -> C],
6228 * return the map A[B -> C] -> B.
6230 __isl_give isl_map
*isl_set_wrapped_domain_map(__isl_take isl_set
*set
)
6237 if (!isl_set_has_tuple_id(set
))
6238 return isl_map_domain_map(isl_set_unwrap(set
));
6240 id
= isl_set_get_tuple_id(set
);
6241 map
= isl_map_domain_map(isl_set_unwrap(set
));
6242 map
= isl_map_set_tuple_id(map
, isl_dim_in
, id
);
6247 __isl_give isl_basic_map
*isl_basic_map_from_domain(
6248 __isl_take isl_basic_set
*bset
)
6250 return isl_basic_map_reverse(isl_basic_map_from_range(bset
));
6253 __isl_give isl_basic_map
*isl_basic_map_from_range(
6254 __isl_take isl_basic_set
*bset
)
6257 space
= isl_basic_set_get_space(bset
);
6258 space
= isl_space_from_range(space
);
6259 bset
= isl_basic_set_reset_space(bset
, space
);
6260 return bset_to_bmap(bset
);
6263 /* Create a relation with the given set as range.
6264 * The domain of the created relation is a zero-dimensional
6265 * flat anonymous space.
6267 __isl_give isl_map
*isl_map_from_range(__isl_take isl_set
*set
)
6270 space
= isl_set_get_space(set
);
6271 space
= isl_space_from_range(space
);
6272 set
= isl_set_reset_space(set
, space
);
6273 return set_to_map(set
);
6276 /* Create a relation with the given set as domain.
6277 * The range of the created relation is a zero-dimensional
6278 * flat anonymous space.
6280 __isl_give isl_map
*isl_map_from_domain(__isl_take isl_set
*set
)
6282 return isl_map_reverse(isl_map_from_range(set
));
6285 __isl_give isl_basic_map
*isl_basic_map_from_domain_and_range(
6286 __isl_take isl_basic_set
*domain
, __isl_take isl_basic_set
*range
)
6288 return isl_basic_map_apply_range(isl_basic_map_reverse(domain
), range
);
6291 __isl_give isl_map
*isl_map_from_domain_and_range(__isl_take isl_set
*domain
,
6292 __isl_take isl_set
*range
)
6294 return isl_map_apply_range(isl_map_reverse(domain
), range
);
6297 /* Return a newly allocated isl_map with given space and flags and
6298 * room for "n" basic maps.
6299 * Make sure that all cached information is cleared.
6301 __isl_give isl_map
*isl_map_alloc_space(__isl_take isl_space
*space
, int n
,
6304 struct isl_map
*map
;
6309 isl_die(space
->ctx
, isl_error_internal
,
6310 "negative number of basic maps", goto error
);
6311 map
= isl_calloc(space
->ctx
, struct isl_map
,
6312 sizeof(struct isl_map
) +
6313 (n
- 1) * sizeof(struct isl_basic_map
*));
6317 map
->ctx
= space
->ctx
;
6318 isl_ctx_ref(map
->ctx
);
6326 isl_space_free(space
);
6330 __isl_give isl_basic_map
*isl_basic_map_empty(__isl_take isl_space
*space
)
6332 struct isl_basic_map
*bmap
;
6333 bmap
= isl_basic_map_alloc_space(space
, 0, 1, 0);
6334 bmap
= isl_basic_map_set_to_empty(bmap
);
6338 __isl_give isl_basic_set
*isl_basic_set_empty(__isl_take isl_space
*space
)
6340 struct isl_basic_set
*bset
;
6341 bset
= isl_basic_set_alloc_space(space
, 0, 1, 0);
6342 bset
= isl_basic_set_set_to_empty(bset
);
6346 __isl_give isl_basic_map
*isl_basic_map_universe(__isl_take isl_space
*space
)
6348 struct isl_basic_map
*bmap
;
6349 bmap
= isl_basic_map_alloc_space(space
, 0, 0, 0);
6350 bmap
= isl_basic_map_finalize(bmap
);
6354 __isl_give isl_basic_set
*isl_basic_set_universe(__isl_take isl_space
*space
)
6356 struct isl_basic_set
*bset
;
6357 bset
= isl_basic_set_alloc_space(space
, 0, 0, 0);
6358 bset
= isl_basic_set_finalize(bset
);
6362 __isl_give isl_basic_map
*isl_basic_map_nat_universe(
6363 __isl_take isl_space
*space
)
6366 isl_size total
= isl_space_dim(space
, isl_dim_all
);
6367 isl_basic_map
*bmap
;
6370 space
= isl_space_free(space
);
6371 bmap
= isl_basic_map_alloc_space(space
, 0, 0, total
);
6372 for (i
= 0; i
< total
; ++i
) {
6373 int k
= isl_basic_map_alloc_inequality(bmap
);
6376 isl_seq_clr(bmap
->ineq
[k
], 1 + total
);
6377 isl_int_set_si(bmap
->ineq
[k
][1 + i
], 1);
6381 isl_basic_map_free(bmap
);
6385 __isl_give isl_basic_set
*isl_basic_set_nat_universe(
6386 __isl_take isl_space
*space
)
6388 return isl_basic_map_nat_universe(space
);
6391 __isl_give isl_map
*isl_map_nat_universe(__isl_take isl_space
*space
)
6393 return isl_map_from_basic_map(isl_basic_map_nat_universe(space
));
6396 __isl_give isl_set
*isl_set_nat_universe(__isl_take isl_space
*space
)
6398 return isl_map_nat_universe(space
);
6401 __isl_give isl_map
*isl_map_empty(__isl_take isl_space
*space
)
6403 return isl_map_alloc_space(space
, 0, ISL_MAP_DISJOINT
);
6406 __isl_give isl_set
*isl_set_empty(__isl_take isl_space
*space
)
6408 return isl_set_alloc_space(space
, 0, ISL_MAP_DISJOINT
);
6411 __isl_give isl_map
*isl_map_universe(__isl_take isl_space
*space
)
6413 struct isl_map
*map
;
6416 map
= isl_map_alloc_space(isl_space_copy(space
), 1, ISL_MAP_DISJOINT
);
6417 map
= isl_map_add_basic_map(map
, isl_basic_map_universe(space
));
6421 /* This function performs the same operation as isl_map_universe,
6422 * but is considered as a function on an isl_space when exported.
6424 __isl_give isl_map
*isl_space_universe_map(__isl_take isl_space
*space
)
6426 return isl_map_universe(space
);
6429 __isl_give isl_set
*isl_set_universe(__isl_take isl_space
*space
)
6431 struct isl_set
*set
;
6434 set
= isl_set_alloc_space(isl_space_copy(space
), 1, ISL_MAP_DISJOINT
);
6435 set
= isl_set_add_basic_set(set
, isl_basic_set_universe(space
));
6439 /* This function performs the same operation as isl_set_universe,
6440 * but is considered as a function on an isl_space when exported.
6442 __isl_give isl_set
*isl_space_universe_set(__isl_take isl_space
*space
)
6444 return isl_set_universe(space
);
6447 __isl_give isl_map
*isl_map_dup(__isl_keep isl_map
*map
)
6450 struct isl_map
*dup
;
6454 dup
= isl_map_alloc_space(isl_space_copy(map
->dim
), map
->n
, map
->flags
);
6455 for (i
= 0; i
< map
->n
; ++i
)
6456 dup
= isl_map_add_basic_map(dup
, isl_basic_map_copy(map
->p
[i
]));
6460 __isl_give isl_map
*isl_map_add_basic_map(__isl_take isl_map
*map
,
6461 __isl_take isl_basic_map
*bmap
)
6465 if (isl_basic_map_plain_is_empty(bmap
)) {
6466 isl_basic_map_free(bmap
);
6469 if (isl_map_basic_map_check_equal_space(map
, bmap
) < 0)
6471 isl_assert(map
->ctx
, map
->n
< map
->size
, goto error
);
6472 map
->p
[map
->n
] = bmap
;
6474 map
= isl_map_unmark_normalized(map
);
6480 isl_basic_map_free(bmap
);
6484 __isl_null isl_map
*isl_map_free(__isl_take isl_map
*map
)
6495 isl_ctx_deref(map
->ctx
);
6496 for (i
= 0; i
< map
->n
; ++i
)
6497 isl_basic_map_free(map
->p
[i
]);
6498 isl_space_free(map
->dim
);
6504 static __isl_give isl_basic_map
*isl_basic_map_fix_pos_si(
6505 __isl_take isl_basic_map
*bmap
, unsigned pos
, int value
)
6510 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
6512 return isl_basic_map_free(bmap
);
6514 bmap
= isl_basic_map_cow(bmap
);
6515 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
6516 j
= isl_basic_map_alloc_equality(bmap
);
6519 isl_seq_clr(bmap
->eq
[j
] + 1, total
);
6520 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
6521 isl_int_set_si(bmap
->eq
[j
][0], value
);
6522 bmap
= isl_basic_map_simplify(bmap
);
6523 return isl_basic_map_finalize(bmap
);
6525 isl_basic_map_free(bmap
);
6529 static __isl_give isl_basic_map
*isl_basic_map_fix_pos(
6530 __isl_take isl_basic_map
*bmap
, unsigned pos
, isl_int value
)
6535 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
6537 return isl_basic_map_free(bmap
);
6539 bmap
= isl_basic_map_cow(bmap
);
6540 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
6541 j
= isl_basic_map_alloc_equality(bmap
);
6544 isl_seq_clr(bmap
->eq
[j
] + 1, total
);
6545 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
6546 isl_int_set(bmap
->eq
[j
][0], value
);
6547 bmap
= isl_basic_map_simplify(bmap
);
6548 return isl_basic_map_finalize(bmap
);
6550 isl_basic_map_free(bmap
);
6554 __isl_give isl_basic_map
*isl_basic_map_fix_si(__isl_take isl_basic_map
*bmap
,
6555 enum isl_dim_type type
, unsigned pos
, int value
)
6557 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6558 return isl_basic_map_free(bmap
);
6559 return isl_basic_map_fix_pos_si(bmap
,
6560 isl_basic_map_offset(bmap
, type
) + pos
, value
);
6563 __isl_give isl_basic_map
*isl_basic_map_fix(__isl_take isl_basic_map
*bmap
,
6564 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6566 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6567 return isl_basic_map_free(bmap
);
6568 return isl_basic_map_fix_pos(bmap
,
6569 isl_basic_map_offset(bmap
, type
) + pos
, value
);
6572 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
6573 * to be equal to "v".
6575 __isl_give isl_basic_map
*isl_basic_map_fix_val(__isl_take isl_basic_map
*bmap
,
6576 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6580 if (!isl_val_is_int(v
))
6581 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
6582 "expecting integer value", goto error
);
6583 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6585 pos
+= isl_basic_map_offset(bmap
, type
);
6586 bmap
= isl_basic_map_fix_pos(bmap
, pos
, v
->n
);
6590 isl_basic_map_free(bmap
);
6595 /* Fix the value of the variable at position "pos" of type "type" of "bset"
6596 * to be equal to "v".
6598 __isl_give isl_basic_set
*isl_basic_set_fix_val(__isl_take isl_basic_set
*bset
,
6599 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6601 return isl_basic_map_fix_val(bset
, type
, pos
, v
);
6604 __isl_give isl_basic_set
*isl_basic_set_fix_si(__isl_take isl_basic_set
*bset
,
6605 enum isl_dim_type type
, unsigned pos
, int value
)
6607 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset
),
6611 __isl_give isl_basic_set
*isl_basic_set_fix(__isl_take isl_basic_set
*bset
,
6612 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6614 return bset_from_bmap(isl_basic_map_fix(bset_to_bmap(bset
),
6618 /* Remove the basic map at position "i" from "map" if this basic map
6619 * is (obviously) empty.
6621 static __isl_give isl_map
*remove_if_empty(__isl_take isl_map
*map
, int i
)
6628 empty
= isl_basic_map_plain_is_empty(map
->p
[i
]);
6630 return isl_map_free(map
);
6634 isl_basic_map_free(map
->p
[i
]);
6637 map
->p
[i
] = map
->p
[map
->n
];
6638 map
= isl_map_unmark_normalized(map
);
6645 /* Perform "fn" on each basic map of "map", where we may not be holding
6646 * the only reference to "map".
6647 * In particular, "fn" should be a semantics preserving operation
6648 * that we want to apply to all copies of "map". We therefore need
6649 * to be careful not to modify "map" in a way that breaks "map"
6650 * in case anything goes wrong.
6652 __isl_give isl_map
*isl_map_inline_foreach_basic_map(__isl_take isl_map
*map
,
6653 __isl_give isl_basic_map
*(*fn
)(__isl_take isl_basic_map
*bmap
))
6655 struct isl_basic_map
*bmap
;
6661 for (i
= map
->n
- 1; i
>= 0; --i
) {
6662 bmap
= isl_basic_map_copy(map
->p
[i
]);
6666 isl_basic_map_free(map
->p
[i
]);
6668 map
= remove_if_empty(map
, i
);
6679 __isl_give isl_map
*isl_map_fix_si(__isl_take isl_map
*map
,
6680 enum isl_dim_type type
, unsigned pos
, int value
)
6684 map
= isl_map_cow(map
);
6685 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
6686 return isl_map_free(map
);
6687 for (i
= map
->n
- 1; i
>= 0; --i
) {
6688 map
->p
[i
] = isl_basic_map_fix_si(map
->p
[i
], type
, pos
, value
);
6689 map
= remove_if_empty(map
, i
);
6693 map
= isl_map_unmark_normalized(map
);
6697 __isl_give isl_set
*isl_set_fix_si(__isl_take isl_set
*set
,
6698 enum isl_dim_type type
, unsigned pos
, int value
)
6700 return set_from_map(isl_map_fix_si(set_to_map(set
), type
, pos
, value
));
6703 __isl_give isl_map
*isl_map_fix(__isl_take isl_map
*map
,
6704 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6708 map
= isl_map_cow(map
);
6709 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
6710 return isl_map_free(map
);
6711 for (i
= 0; i
< map
->n
; ++i
) {
6712 map
->p
[i
] = isl_basic_map_fix(map
->p
[i
], type
, pos
, value
);
6716 map
= isl_map_unmark_normalized(map
);
6723 __isl_give isl_set
*isl_set_fix(__isl_take isl_set
*set
,
6724 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6726 return set_from_map(isl_map_fix(set_to_map(set
), type
, pos
, value
));
6729 /* Fix the value of the variable at position "pos" of type "type" of "map"
6730 * to be equal to "v".
6732 __isl_give isl_map
*isl_map_fix_val(__isl_take isl_map
*map
,
6733 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6737 map
= isl_map_cow(map
);
6741 if (!isl_val_is_int(v
))
6742 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
6743 "expecting integer value", goto error
);
6744 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
6746 for (i
= map
->n
- 1; i
>= 0; --i
) {
6747 map
->p
[i
] = isl_basic_map_fix_val(map
->p
[i
], type
, pos
,
6749 map
= remove_if_empty(map
, i
);
6753 map
= isl_map_unmark_normalized(map
);
6762 /* Fix the value of the variable at position "pos" of type "type" of "set"
6763 * to be equal to "v".
6765 __isl_give isl_set
*isl_set_fix_val(__isl_take isl_set
*set
,
6766 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6768 return isl_map_fix_val(set
, type
, pos
, v
);
6771 __isl_give isl_map
*isl_map_fix_input_si(__isl_take isl_map
*map
,
6772 unsigned input
, int value
)
6774 return isl_map_fix_si(map
, isl_dim_in
, input
, value
);
6777 __isl_give isl_set
*isl_set_fix_dim_si(__isl_take isl_set
*set
, unsigned dim
,
6780 return set_from_map(isl_map_fix_si(set_to_map(set
),
6781 isl_dim_set
, dim
, value
));
6784 static __isl_give isl_basic_map
*basic_map_bound_si(
6785 __isl_take isl_basic_map
*bmap
,
6786 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
6791 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6792 return isl_basic_map_free(bmap
);
6793 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
6795 return isl_basic_map_free(bmap
);
6796 pos
+= isl_basic_map_offset(bmap
, type
);
6797 bmap
= isl_basic_map_cow(bmap
);
6798 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
6799 j
= isl_basic_map_alloc_inequality(bmap
);
6802 isl_seq_clr(bmap
->ineq
[j
], 1 + total
);
6804 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
6805 isl_int_set_si(bmap
->ineq
[j
][0], value
);
6807 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
6808 isl_int_set_si(bmap
->ineq
[j
][0], -value
);
6810 bmap
= isl_basic_map_simplify(bmap
);
6811 return isl_basic_map_finalize(bmap
);
6813 isl_basic_map_free(bmap
);
6817 __isl_give isl_basic_map
*isl_basic_map_lower_bound_si(
6818 __isl_take isl_basic_map
*bmap
,
6819 enum isl_dim_type type
, unsigned pos
, int value
)
6821 return basic_map_bound_si(bmap
, type
, pos
, value
, 0);
6824 /* Constrain the values of the given dimension to be no greater than "value".
6826 __isl_give isl_basic_map
*isl_basic_map_upper_bound_si(
6827 __isl_take isl_basic_map
*bmap
,
6828 enum isl_dim_type type
, unsigned pos
, int value
)
6830 return basic_map_bound_si(bmap
, type
, pos
, value
, 1);
6833 static __isl_give isl_map
*map_bound_si(__isl_take isl_map
*map
,
6834 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
6838 map
= isl_map_cow(map
);
6839 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
6840 return isl_map_free(map
);
6841 for (i
= 0; i
< map
->n
; ++i
) {
6842 map
->p
[i
] = basic_map_bound_si(map
->p
[i
],
6843 type
, pos
, value
, upper
);
6847 map
= isl_map_unmark_normalized(map
);
6854 __isl_give isl_map
*isl_map_lower_bound_si(__isl_take isl_map
*map
,
6855 enum isl_dim_type type
, unsigned pos
, int value
)
6857 return map_bound_si(map
, type
, pos
, value
, 0);
6860 __isl_give isl_map
*isl_map_upper_bound_si(__isl_take isl_map
*map
,
6861 enum isl_dim_type type
, unsigned pos
, int value
)
6863 return map_bound_si(map
, type
, pos
, value
, 1);
6866 __isl_give isl_set
*isl_set_lower_bound_si(__isl_take isl_set
*set
,
6867 enum isl_dim_type type
, unsigned pos
, int value
)
6869 return set_from_map(isl_map_lower_bound_si(set_to_map(set
),
6873 __isl_give isl_set
*isl_set_upper_bound_si(__isl_take isl_set
*set
,
6874 enum isl_dim_type type
, unsigned pos
, int value
)
6876 return isl_map_upper_bound_si(set
, type
, pos
, value
);
6879 /* Bound the given variable of "bmap" from below (or above is "upper"
6880 * is set) to "value".
6882 static __isl_give isl_basic_map
*basic_map_bound(
6883 __isl_take isl_basic_map
*bmap
,
6884 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
6889 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6890 return isl_basic_map_free(bmap
);
6891 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
6893 return isl_basic_map_free(bmap
);
6894 pos
+= isl_basic_map_offset(bmap
, type
);
6895 bmap
= isl_basic_map_cow(bmap
);
6896 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
6897 j
= isl_basic_map_alloc_inequality(bmap
);
6900 isl_seq_clr(bmap
->ineq
[j
], 1 + total
);
6902 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
6903 isl_int_set(bmap
->ineq
[j
][0], value
);
6905 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
6906 isl_int_neg(bmap
->ineq
[j
][0], value
);
6908 bmap
= isl_basic_map_simplify(bmap
);
6909 return isl_basic_map_finalize(bmap
);
6911 isl_basic_map_free(bmap
);
6915 /* Bound the given variable of "map" from below (or above is "upper"
6916 * is set) to "value".
6918 static __isl_give isl_map
*map_bound(__isl_take isl_map
*map
,
6919 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
6923 map
= isl_map_cow(map
);
6924 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
6925 return isl_map_free(map
);
6926 for (i
= map
->n
- 1; i
>= 0; --i
) {
6927 map
->p
[i
] = basic_map_bound(map
->p
[i
], type
, pos
, value
, upper
);
6928 map
= remove_if_empty(map
, i
);
6932 map
= isl_map_unmark_normalized(map
);
6936 __isl_give isl_map
*isl_map_lower_bound(__isl_take isl_map
*map
,
6937 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6939 return map_bound(map
, type
, pos
, value
, 0);
6942 __isl_give isl_map
*isl_map_upper_bound(__isl_take isl_map
*map
,
6943 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6945 return map_bound(map
, type
, pos
, value
, 1);
6948 __isl_give isl_set
*isl_set_lower_bound(__isl_take isl_set
*set
,
6949 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6951 return isl_map_lower_bound(set
, type
, pos
, value
);
6954 __isl_give isl_set
*isl_set_upper_bound(__isl_take isl_set
*set
,
6955 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6957 return isl_map_upper_bound(set
, type
, pos
, value
);
6960 /* Force the values of the variable at position "pos" of type "type" of "map"
6961 * to be no smaller than "value".
6963 __isl_give isl_map
*isl_map_lower_bound_val(__isl_take isl_map
*map
,
6964 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
6968 if (!isl_val_is_int(value
))
6969 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
6970 "expecting integer value", goto error
);
6971 map
= isl_map_lower_bound(map
, type
, pos
, value
->n
);
6972 isl_val_free(value
);
6975 isl_val_free(value
);
6980 /* Force the values of the variable at position "pos" of type "type" of "set"
6981 * to be no smaller than "value".
6983 __isl_give isl_set
*isl_set_lower_bound_val(__isl_take isl_set
*set
,
6984 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
6988 map
= set_to_map(set
);
6989 return set_from_map(isl_map_lower_bound_val(map
, type
, pos
, value
));
6992 /* Force the values of the variable at position "pos" of type "type" of "map"
6993 * to be no greater than "value".
6995 __isl_give isl_map
*isl_map_upper_bound_val(__isl_take isl_map
*map
,
6996 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
7000 if (!isl_val_is_int(value
))
7001 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
7002 "expecting integer value", goto error
);
7003 map
= isl_map_upper_bound(map
, type
, pos
, value
->n
);
7004 isl_val_free(value
);
7007 isl_val_free(value
);
7012 /* Force the values of the variable at position "pos" of type "type" of "set"
7013 * to be no greater than "value".
7015 __isl_give isl_set
*isl_set_upper_bound_val(__isl_take isl_set
*set
,
7016 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
7020 map
= set_to_map(set
);
7021 return set_from_map(isl_map_upper_bound_val(map
, type
, pos
, value
));
7024 /* If "mv" has an explicit domain, then intersect the domain of "map"
7025 * with this explicit domain.
7027 * An isl_multi_val object never has an explicit domain,
7028 * so simply return "map".
7030 static __isl_give isl_map
*isl_map_intersect_multi_val_explicit_domain(
7031 __isl_take isl_map
*map
, __isl_keep isl_multi_val
*mv
)
7038 #include "isl_map_bound_templ.c"
7040 /* Apply "map_bound" to "set" with the corresponding value in "bound"
7041 * for each set dimension, by treating the set as a map.
7043 static __isl_give isl_set
*set_bound_multi_val(__isl_take isl_set
*set
,
7044 __isl_take isl_multi_val
*bound
,
7045 __isl_give isl_map
*map_bound(__isl_take isl_map
*map
,
7046 unsigned pos
, __isl_take isl_val
*value
))
7050 map
= set_to_map(set
);
7051 return set_from_map(map_bound_multi_val(map
, bound
, map_bound
));
7056 #include "isl_map_bound_templ.c"
7058 /* Apply "map_bound" to "set" with the corresponding value in "bound"
7059 * for each set dimension, by converting the set and the bound
7060 * to objects living in a map space.
7062 static __isl_give isl_set
*set_bound_multi_pw_aff(__isl_take isl_set
*set
,
7063 __isl_take isl_multi_pw_aff
*bound
,
7064 __isl_give isl_map
*set_bound(__isl_take isl_map
*map
,
7065 unsigned pos
, __isl_take TYPE
*value
))
7069 map
= isl_map_from_range(set
);
7070 bound
= isl_multi_pw_aff_from_range(bound
);
7071 map
= map_bound_multi_pw_aff(map
, bound
, set_bound
);
7072 return isl_map_range(map
);
7075 /* Wrapper around isl_map_lower_bound_val for use in map_bound_multi_val,
7076 * setting a bound on the given output dimension.
7078 static __isl_give isl_map
*map_lower_bound_val(__isl_take isl_map
*map
,
7079 unsigned pos
, __isl_take isl_val
*v
)
7081 return isl_map_lower_bound_val(map
, isl_dim_out
, pos
, v
);
7084 /* Force the values of the set dimensions of "set"
7085 * to be no smaller than the corresponding values in "lower".
7087 __isl_give isl_set
*isl_set_lower_bound_multi_val(__isl_take isl_set
*set
,
7088 __isl_take isl_multi_val
*lower
)
7090 return set_bound_multi_val(set
, lower
, &map_lower_bound_val
);
7093 /* Wrapper around isl_map_upper_bound_val for use in map_bound_multi_val,
7094 * setting a bound on the given output dimension.
7096 static __isl_give isl_map
*map_upper_bound_val(__isl_take isl_map
*map
,
7097 unsigned pos
, __isl_take isl_val
*v
)
7099 return isl_map_upper_bound_val(map
, isl_dim_out
, pos
, v
);
7102 /* Force the values of the set dimensions of "set"
7103 * to be no greater than the corresponding values in "upper".
7105 __isl_give isl_set
*isl_set_upper_bound_multi_val(__isl_take isl_set
*set
,
7106 __isl_take isl_multi_val
*upper
)
7108 return set_bound_multi_val(set
, upper
, &map_upper_bound_val
);
7111 /* Force the symbolic constant expression "bound"
7112 * to satisfy the relation "order" with respect to
7113 * the output variable at position "pos" of "map".
7115 * Create an affine expression representing the output variable
7116 * in terms of the range and
7117 * compare it using "order" to "bound" (defined on the domain).
7118 * The result is a relation between elements in domain and range that
7119 * can be intersected with "map".
7121 static __isl_give isl_map
*map_bound_pw_aff(__isl_take isl_map
*map
,
7122 unsigned pos
, __isl_take isl_pw_aff
*bound
,
7123 __isl_give isl_map
*(*order
)(__isl_take isl_pw_aff
*pa1
,
7124 __isl_take isl_pw_aff
*pa2
))
7127 isl_local_space
*ls
;
7130 space
= isl_space_range(isl_map_get_space(map
));
7131 ls
= isl_local_space_from_space(space
);
7132 var
= isl_pw_aff_var_on_domain(ls
, isl_dim_set
, pos
);
7133 map
= isl_map_intersect(map
, order(bound
, var
));
7137 /* Force the values of the output variable at position "pos" of "map"
7138 * to be no smaller than the symbolic constant expression "lower".
7140 static __isl_give isl_map
*map_lower_bound_pw_aff(__isl_take isl_map
*map
,
7141 unsigned pos
, __isl_take isl_pw_aff
*lower
)
7143 return map_bound_pw_aff(map
, pos
, lower
, &isl_pw_aff_le_map
);
7146 /* Force the values of the output variable at position "pos" of "map"
7147 * to be no greater than the symbolic constant expression "upper".
7149 static __isl_give isl_map
*map_upper_bound_pw_aff(__isl_take isl_map
*map
,
7150 unsigned pos
, __isl_take isl_pw_aff
*upper
)
7152 return map_bound_pw_aff(map
, pos
, upper
, &isl_pw_aff_ge_map
);
7155 /* Force the values of the set dimensions of "set"
7156 * to be no smaller than the corresponding constant symbolic expressions
7159 __isl_give isl_set
*isl_set_lower_bound_multi_pw_aff(__isl_take isl_set
*set
,
7160 __isl_take isl_multi_pw_aff
*lower
)
7162 return set_bound_multi_pw_aff(set
, lower
, &map_lower_bound_pw_aff
);
7165 /* Force the values of the set dimensions of "set"
7166 * to be no greater than the corresponding constant symbolic expressions
7169 __isl_give isl_set
*isl_set_upper_bound_multi_pw_aff(__isl_take isl_set
*set
,
7170 __isl_take isl_multi_pw_aff
*upper
)
7172 return set_bound_multi_pw_aff(set
, upper
, &map_upper_bound_pw_aff
);
7175 /* Force the values of the output dimensions of "map"
7176 * to be no smaller than the corresponding constant symbolic expressions
7179 __isl_give isl_map
*isl_map_lower_bound_multi_pw_aff(__isl_take isl_map
*map
,
7180 __isl_take isl_multi_pw_aff
*lower
)
7182 return map_bound_multi_pw_aff(map
, lower
, &map_lower_bound_pw_aff
);
7185 /* Force the values of the output dimensions of "map"
7186 * to be no greater than the corresponding constant symbolic expressions
7189 __isl_give isl_map
*isl_map_upper_bound_multi_pw_aff(__isl_take isl_map
*map
,
7190 __isl_take isl_multi_pw_aff
*upper
)
7192 return map_bound_multi_pw_aff(map
, upper
, &map_upper_bound_pw_aff
);
7195 /* Bound the given variable of "bset" from below (or above is "upper"
7196 * is set) to "value".
7198 static __isl_give isl_basic_set
*isl_basic_set_bound(
7199 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned pos
,
7200 isl_int value
, int upper
)
7202 return bset_from_bmap(basic_map_bound(bset_to_bmap(bset
),
7203 type
, pos
, value
, upper
));
7206 /* Bound the given variable of "bset" from below (or above is "upper"
7207 * is set) to "value".
7209 static __isl_give isl_basic_set
*isl_basic_set_bound_val(
7210 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned pos
,
7211 __isl_take isl_val
*value
, int upper
)
7215 if (!isl_val_is_int(value
))
7216 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
7217 "expecting integer value", goto error
);
7218 bset
= isl_basic_set_bound(bset
, type
, pos
, value
->n
, upper
);
7219 isl_val_free(value
);
7222 isl_val_free(value
);
7223 isl_basic_set_free(bset
);
7227 /* Bound the given variable of "bset" from below to "value".
7229 __isl_give isl_basic_set
*isl_basic_set_lower_bound_val(
7230 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned pos
,
7231 __isl_take isl_val
*value
)
7233 return isl_basic_set_bound_val(bset
, type
, pos
, value
, 0);
7236 /* Bound the given variable of "bset" from above to "value".
7238 __isl_give isl_basic_set
*isl_basic_set_upper_bound_val(
7239 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned pos
,
7240 __isl_take isl_val
*value
)
7242 return isl_basic_set_bound_val(bset
, type
, pos
, value
, 1);
7245 __isl_give isl_map
*isl_map_reverse(__isl_take isl_map
*map
)
7247 return isl_map_transform(map
, &isl_space_reverse
,
7248 &isl_basic_map_reverse
);
7251 /* Given a map (A -> B) -> C, return the corresponding map (B -> A) -> C.
7253 __isl_give isl_map
*isl_map_domain_reverse(__isl_take isl_map
*map
)
7255 return isl_map_transform(map
, &isl_space_domain_reverse
,
7256 &isl_basic_map_domain_reverse
);
7259 /* Given a map A -> (B -> C), return the corresponding map A -> (C -> B).
7261 __isl_give isl_map
*isl_map_range_reverse(__isl_take isl_map
*map
)
7263 return isl_map_transform(map
, &isl_space_range_reverse
,
7264 &isl_basic_map_range_reverse
);
7267 /* Given a set (A -> B), return the corresponding set (B -> A).
7269 __isl_give isl_set
*isl_set_wrapped_reverse(__isl_take isl_set
*set
)
7271 isl_map
*map
= set_to_map(set
);
7273 map
= isl_map_transform(map
, &isl_space_wrapped_reverse
,
7274 &isl_basic_map_set_reverse
);
7275 return set_from_map(map
);
7279 #define TYPE isl_pw_multi_aff
7281 #define SUFFIX _pw_multi_aff
7283 #define EMPTY isl_pw_multi_aff_empty
7285 #define ADD isl_pw_multi_aff_union_add
7286 #include "isl_map_lexopt_templ.c"
7288 /* Given a map "map", compute the lexicographically minimal
7289 * (or maximal) image element for each domain element in dom,
7290 * in the form of an isl_pw_multi_aff.
7291 * If "empty" is not NULL, then set *empty to those elements in dom that
7292 * do not have an image element.
7293 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
7294 * should be computed over the domain of "map". "empty" is also NULL
7297 * We first compute the lexicographically minimal or maximal element
7298 * in the first basic map. This results in a partial solution "res"
7299 * and a subset "todo" of dom that still need to be handled.
7300 * We then consider each of the remaining maps in "map" and successively
7301 * update both "res" and "todo".
7302 * If "empty" is NULL, then the todo sets are not needed and therefore
7303 * also not computed.
7305 static __isl_give isl_pw_multi_aff
*isl_map_partial_lexopt_aligned_pw_multi_aff(
7306 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
7307 __isl_give isl_set
**empty
, unsigned flags
)
7311 isl_pw_multi_aff
*res
;
7314 full
= ISL_FL_ISSET(flags
, ISL_OPT_FULL
);
7315 if (!map
|| (!full
&& !dom
))
7318 if (isl_map_plain_is_empty(map
)) {
7323 return isl_pw_multi_aff_from_map(map
);
7326 res
= basic_map_partial_lexopt_pw_multi_aff(
7327 isl_basic_map_copy(map
->p
[0]),
7328 isl_set_copy(dom
), empty
, flags
);
7332 for (i
= 1; i
< map
->n
; ++i
) {
7333 isl_pw_multi_aff
*res_i
;
7335 res_i
= basic_map_partial_lexopt_pw_multi_aff(
7336 isl_basic_map_copy(map
->p
[i
]),
7337 isl_set_copy(dom
), empty
, flags
);
7339 if (ISL_FL_ISSET(flags
, ISL_OPT_MAX
))
7340 res
= isl_pw_multi_aff_union_lexmax(res
, res_i
);
7342 res
= isl_pw_multi_aff_union_lexmin(res
, res_i
);
7345 todo
= isl_set_intersect(todo
, *empty
);
7364 #define TYPE isl_map
7368 #define EMPTY isl_map_empty
7370 #define ADD isl_map_union_disjoint
7371 #include "isl_map_lexopt_templ.c"
7373 /* Given a map "map", compute the lexicographically minimal
7374 * (or maximal) image element for each domain element in "dom",
7375 * in the form of an isl_map.
7376 * If "empty" is not NULL, then set *empty to those elements in "dom" that
7377 * do not have an image element.
7378 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
7379 * should be computed over the domain of "map". "empty" is also NULL
7382 * If the input consists of more than one disjunct, then first
7383 * compute the desired result in the form of an isl_pw_multi_aff and
7384 * then convert that into an isl_map.
7386 * This function used to have an explicit implementation in terms
7387 * of isl_maps, but it would continually intersect the domains of
7388 * partial results with the complement of the domain of the next
7389 * partial solution, potentially leading to an explosion in the number
7390 * of disjuncts if there are several disjuncts in the input.
7391 * An even earlier implementation of this function would look for
7392 * better results in the domain of the partial result and for extra
7393 * results in the complement of this domain, which would lead to
7394 * even more splintering.
7396 static __isl_give isl_map
*isl_map_partial_lexopt_aligned(
7397 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
7398 __isl_give isl_set
**empty
, unsigned flags
)
7401 struct isl_map
*res
;
7402 isl_pw_multi_aff
*pma
;
7404 full
= ISL_FL_ISSET(flags
, ISL_OPT_FULL
);
7405 if (!map
|| (!full
&& !dom
))
7408 if (isl_map_plain_is_empty(map
)) {
7417 res
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[0]),
7423 pma
= isl_map_partial_lexopt_aligned_pw_multi_aff(map
, dom
, empty
,
7425 return isl_map_from_pw_multi_aff_internal(pma
);
7434 __isl_give isl_map
*isl_map_partial_lexmax(
7435 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
7436 __isl_give isl_set
**empty
)
7438 return isl_map_partial_lexopt(map
, dom
, empty
, ISL_OPT_MAX
);
7441 __isl_give isl_map
*isl_map_partial_lexmin(
7442 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
7443 __isl_give isl_set
**empty
)
7445 return isl_map_partial_lexopt(map
, dom
, empty
, 0);
7448 __isl_give isl_set
*isl_set_partial_lexmin(
7449 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
7450 __isl_give isl_set
**empty
)
7452 return set_from_map(isl_map_partial_lexmin(set_to_map(set
),
7456 __isl_give isl_set
*isl_set_partial_lexmax(
7457 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
7458 __isl_give isl_set
**empty
)
7460 return set_from_map(isl_map_partial_lexmax(set_to_map(set
),
7464 /* Compute the lexicographic minimum (or maximum if "flags" includes
7465 * ISL_OPT_MAX) of "bset" over its parametric domain.
7467 __isl_give isl_set
*isl_basic_set_lexopt(__isl_take isl_basic_set
*bset
,
7470 return isl_basic_map_lexopt(bset
, flags
);
7473 __isl_give isl_map
*isl_basic_map_lexmax(__isl_take isl_basic_map
*bmap
)
7475 return isl_basic_map_lexopt(bmap
, ISL_OPT_MAX
);
7478 __isl_give isl_set
*isl_basic_set_lexmin(__isl_take isl_basic_set
*bset
)
7480 return set_from_map(isl_basic_map_lexmin(bset_to_bmap(bset
)));
7483 __isl_give isl_set
*isl_basic_set_lexmax(__isl_take isl_basic_set
*bset
)
7485 return set_from_map(isl_basic_map_lexmax(bset_to_bmap(bset
)));
7488 /* Compute the lexicographic minimum of "bset" over its parametric domain
7489 * for the purpose of quantifier elimination.
7490 * That is, find an explicit representation for all the existentially
7491 * quantified variables in "bset" by computing their lexicographic
7494 static __isl_give isl_set
*isl_basic_set_lexmin_compute_divs(
7495 __isl_take isl_basic_set
*bset
)
7497 return isl_basic_set_lexopt(bset
, ISL_OPT_QE
);
7500 /* Given a basic map with one output dimension, compute the minimum or
7501 * maximum of that dimension as an isl_pw_aff.
7503 * Compute the optimum as a lexicographic optimum over the single
7504 * output dimension and extract the single isl_pw_aff from the result.
7506 static __isl_give isl_pw_aff
*basic_map_dim_opt(__isl_keep isl_basic_map
*bmap
,
7509 isl_pw_multi_aff
*pma
;
7512 bmap
= isl_basic_map_copy(bmap
);
7513 pma
= isl_basic_map_lexopt_pw_multi_aff(bmap
, max
? ISL_OPT_MAX
: 0);
7514 pwaff
= isl_pw_multi_aff_get_pw_aff(pma
, 0);
7515 isl_pw_multi_aff_free(pma
);
7520 /* Compute the minimum or maximum of the given output dimension
7521 * as a function of the parameters and the input dimensions,
7522 * but independently of the other output dimensions.
7524 * We first project out the other output dimension and then compute
7525 * the "lexicographic" maximum in each basic map, combining the results
7526 * using isl_pw_aff_union_max.
7528 static __isl_give isl_pw_aff
*map_dim_opt(__isl_take isl_map
*map
, int pos
,
7535 n_out
= isl_map_dim(map
, isl_dim_out
);
7537 map
= isl_map_free(map
);
7538 map
= isl_map_project_out(map
, isl_dim_out
, pos
+ 1, n_out
- (pos
+ 1));
7539 map
= isl_map_project_out(map
, isl_dim_out
, 0, pos
);
7544 isl_space
*space
= isl_map_get_space(map
);
7546 return isl_pw_aff_empty(space
);
7549 pwaff
= basic_map_dim_opt(map
->p
[0], max
);
7550 for (i
= 1; i
< map
->n
; ++i
) {
7551 isl_pw_aff
*pwaff_i
;
7553 pwaff_i
= basic_map_dim_opt(map
->p
[i
], max
);
7554 pwaff
= isl_pw_aff_union_opt(pwaff
, pwaff_i
, max
);
7562 /* Compute the minimum of the given output dimension as a function of the
7563 * parameters and input dimensions, but independently of
7564 * the other output dimensions.
7566 __isl_give isl_pw_aff
*isl_map_dim_min(__isl_take isl_map
*map
, int pos
)
7568 return map_dim_opt(map
, pos
, 0);
7571 /* Compute the maximum of the given output dimension as a function of the
7572 * parameters and input dimensions, but independently of
7573 * the other output dimensions.
7575 __isl_give isl_pw_aff
*isl_map_dim_max(__isl_take isl_map
*map
, int pos
)
7577 return map_dim_opt(map
, pos
, 1);
7580 /* Compute the minimum or maximum of the given set dimension
7581 * as a function of the parameters,
7582 * but independently of the other set dimensions.
7584 static __isl_give isl_pw_aff
*set_dim_opt(__isl_take isl_set
*set
, int pos
,
7587 return map_dim_opt(set
, pos
, max
);
7590 /* Compute the maximum of the given set dimension as a function of the
7591 * parameters, but independently of the other set dimensions.
7593 __isl_give isl_pw_aff
*isl_set_dim_max(__isl_take isl_set
*set
, int pos
)
7595 return set_dim_opt(set
, pos
, 1);
7598 /* Compute the minimum of the given set dimension as a function of the
7599 * parameters, but independently of the other set dimensions.
7601 __isl_give isl_pw_aff
*isl_set_dim_min(__isl_take isl_set
*set
, int pos
)
7603 return set_dim_opt(set
, pos
, 0);
7606 /* Apply a preimage specified by "mat" on the parameters of "bset".
7607 * bset is assumed to have only parameters and divs.
7609 static __isl_give isl_basic_set
*basic_set_parameter_preimage(
7610 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*mat
)
7614 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
7615 if (nparam
< 0 || !mat
)
7618 bset
->dim
= isl_space_cow(bset
->dim
);
7622 isl_assert(bset
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
7624 bset
->dim
->nparam
= 0;
7625 bset
->dim
->n_out
= nparam
;
7626 bset
= isl_basic_set_preimage(bset
, mat
);
7628 bset
->dim
->nparam
= bset
->dim
->n_out
;
7629 bset
->dim
->n_out
= 0;
7634 isl_basic_set_free(bset
);
7638 /* Apply a preimage specified by "mat" on the parameters of "set".
7639 * set is assumed to have only parameters and divs.
7641 static __isl_give isl_set
*set_parameter_preimage(__isl_take isl_set
*set
,
7642 __isl_take isl_mat
*mat
)
7647 nparam
= isl_set_dim(set
, isl_dim_param
);
7648 if (nparam
< 0 || !mat
)
7651 if (mat
->n_row
!= 1 + nparam
)
7652 isl_die(isl_set_get_ctx(set
), isl_error_internal
,
7653 "unexpected number of rows", goto error
);
7655 space
= isl_set_get_space(set
);
7656 space
= isl_space_move_dims(space
, isl_dim_set
, 0,
7657 isl_dim_param
, 0, nparam
);
7658 set
= isl_set_reset_space(set
, space
);
7659 set
= isl_set_preimage(set
, mat
);
7660 nparam
= isl_set_dim(set
, isl_dim_out
);
7662 set
= isl_set_free(set
);
7663 space
= isl_set_get_space(set
);
7664 space
= isl_space_move_dims(space
, isl_dim_param
, 0,
7665 isl_dim_out
, 0, nparam
);
7666 set
= isl_set_reset_space(set
, space
);
7674 /* Intersect the basic set "bset" with the affine space specified by the
7675 * equalities in "eq".
7677 static __isl_give isl_basic_set
*basic_set_append_equalities(
7678 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*eq
)
7686 bset
= isl_basic_set_extend(bset
, 0, eq
->n_row
, 0);
7690 len
= isl_basic_set_offset(bset
, isl_dim_div
) + bset
->extra
;
7691 for (i
= 0; i
< eq
->n_row
; ++i
) {
7692 k
= isl_basic_set_alloc_equality(bset
);
7695 isl_seq_cpy(bset
->eq
[k
], eq
->row
[i
], eq
->n_col
);
7696 isl_seq_clr(bset
->eq
[k
] + eq
->n_col
, len
- eq
->n_col
);
7700 bset
= isl_basic_set_gauss(bset
, NULL
);
7701 bset
= isl_basic_set_finalize(bset
);
7706 isl_basic_set_free(bset
);
7710 /* Intersect the set "set" with the affine space specified by the
7711 * equalities in "eq".
7713 static __isl_give isl_set
*set_append_equalities(__isl_take isl_set
*set
,
7714 __isl_take isl_mat
*eq
)
7721 for (i
= 0; i
< set
->n
; ++i
) {
7722 set
->p
[i
] = basic_set_append_equalities(set
->p
[i
],
7735 /* Given a basic set "bset" that only involves parameters and existentially
7736 * quantified variables, return the index of the first equality
7737 * that only involves parameters. If there is no such equality then
7738 * return bset->n_eq.
7740 * This function assumes that isl_basic_set_gauss has been called on "bset".
7742 static int first_parameter_equality(__isl_keep isl_basic_set
*bset
)
7745 isl_size nparam
, n_div
;
7747 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
7748 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
7749 if (nparam
< 0 || n_div
< 0)
7752 for (i
= 0, j
= n_div
- 1; i
< bset
->n_eq
&& j
>= 0; --j
) {
7753 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ j
]))
7760 /* Compute an explicit representation for the existentially quantified
7761 * variables in "bset" by computing the "minimal value" of the set
7762 * variables. Since there are no set variables, the computation of
7763 * the minimal value essentially computes an explicit representation
7764 * of the non-empty part(s) of "bset".
7766 * The input only involves parameters and existentially quantified variables.
7767 * All equalities among parameters have been removed.
7769 * Since the existentially quantified variables in the result are in general
7770 * going to be different from those in the input, we first replace
7771 * them by the minimal number of variables based on their equalities.
7772 * This should simplify the parametric integer programming.
7774 static __isl_give isl_set
*base_compute_divs(__isl_take isl_basic_set
*bset
)
7776 isl_morph
*morph1
, *morph2
;
7782 if (bset
->n_eq
== 0)
7783 return isl_basic_set_lexmin_compute_divs(bset
);
7785 morph1
= isl_basic_set_parameter_compression(bset
);
7786 bset
= isl_morph_basic_set(isl_morph_copy(morph1
), bset
);
7787 bset
= isl_basic_set_lift(bset
);
7788 morph2
= isl_basic_set_variable_compression(bset
, isl_dim_set
);
7789 bset
= isl_morph_basic_set(morph2
, bset
);
7790 n
= isl_basic_set_dim(bset
, isl_dim_set
);
7792 bset
= isl_basic_set_free(bset
);
7793 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
7795 set
= isl_basic_set_lexmin_compute_divs(bset
);
7797 set
= isl_morph_set(isl_morph_inverse(morph1
), set
);
7802 /* Project the given basic set onto its parameter domain, possibly introducing
7803 * new, explicit, existential variables in the constraints.
7804 * The input has parameters and (possibly implicit) existential variables.
7805 * The output has the same parameters, but only
7806 * explicit existentially quantified variables.
7808 * The actual projection is performed by pip, but pip doesn't seem
7809 * to like equalities very much, so we first remove the equalities
7810 * among the parameters by performing a variable compression on
7811 * the parameters. Afterward, an inverse transformation is performed
7812 * and the equalities among the parameters are inserted back in.
7814 * The variable compression on the parameters may uncover additional
7815 * equalities that were only implicit before. We therefore check
7816 * if there are any new parameter equalities in the result and
7817 * if so recurse. The removal of parameter equalities is required
7818 * for the parameter compression performed by base_compute_divs.
7820 static __isl_give isl_set
*parameter_compute_divs(
7821 __isl_take isl_basic_set
*bset
)
7825 struct isl_mat
*T
, *T2
;
7826 struct isl_set
*set
;
7829 bset
= isl_basic_set_cow(bset
);
7833 if (bset
->n_eq
== 0)
7834 return base_compute_divs(bset
);
7836 bset
= isl_basic_set_gauss(bset
, NULL
);
7839 if (isl_basic_set_plain_is_empty(bset
))
7840 return isl_set_from_basic_set(bset
);
7842 i
= first_parameter_equality(bset
);
7843 if (i
== bset
->n_eq
)
7844 return base_compute_divs(bset
);
7846 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
7848 return isl_set_from_basic_set(isl_basic_set_free(bset
));
7849 eq
= isl_mat_sub_alloc6(bset
->ctx
, bset
->eq
, i
, bset
->n_eq
- i
,
7851 eq
= isl_mat_cow(eq
);
7852 T
= isl_mat_variable_compression(isl_mat_copy(eq
), &T2
);
7853 if (T
&& T
->n_col
== 0) {
7857 bset
= isl_basic_set_set_to_empty(bset
);
7858 return isl_set_from_basic_set(bset
);
7860 bset
= basic_set_parameter_preimage(bset
, T
);
7862 i
= first_parameter_equality(bset
);
7865 else if (i
== bset
->n_eq
)
7866 set
= base_compute_divs(bset
);
7868 set
= parameter_compute_divs(bset
);
7869 set
= set_parameter_preimage(set
, T2
);
7870 set
= set_append_equalities(set
, eq
);
7874 /* Insert the divs from "ls" before those of "bmap".
7876 * The number of columns is not changed, which means that the last
7877 * dimensions of "bmap" are being reintepreted as the divs from "ls".
7878 * The caller is responsible for removing the same number of dimensions
7879 * from the space of "bmap".
7881 static __isl_give isl_basic_map
*insert_divs_from_local_space(
7882 __isl_take isl_basic_map
*bmap
, __isl_keep isl_local_space
*ls
)
7888 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
7890 return isl_basic_map_free(bmap
);
7894 old_n_div
= bmap
->n_div
;
7895 bmap
= insert_div_rows(bmap
, n_div
);
7899 for (i
= 0; i
< n_div
; ++i
) {
7900 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], ls
->div
->n_col
);
7901 isl_seq_clr(bmap
->div
[i
] + ls
->div
->n_col
, old_n_div
);
7907 /* Replace the space of "bmap" by the space and divs of "ls".
7909 * If "ls" has any divs, then we simplify the result since we may
7910 * have discovered some additional equalities that could simplify
7911 * the div expressions.
7913 static __isl_give isl_basic_map
*basic_replace_space_by_local_space(
7914 __isl_take isl_basic_map
*bmap
, __isl_take isl_local_space
*ls
)
7918 bmap
= isl_basic_map_cow(bmap
);
7919 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
7920 if (!bmap
|| n_div
< 0)
7923 bmap
= insert_divs_from_local_space(bmap
, ls
);
7927 isl_space_free(bmap
->dim
);
7928 bmap
->dim
= isl_local_space_get_space(ls
);
7932 isl_local_space_free(ls
);
7934 bmap
= isl_basic_map_simplify(bmap
);
7935 bmap
= isl_basic_map_finalize(bmap
);
7938 isl_basic_map_free(bmap
);
7939 isl_local_space_free(ls
);
7943 /* Replace the space of "map" by the space and divs of "ls".
7945 static __isl_give isl_map
*replace_space_by_local_space(__isl_take isl_map
*map
,
7946 __isl_take isl_local_space
*ls
)
7950 map
= isl_map_cow(map
);
7954 for (i
= 0; i
< map
->n
; ++i
) {
7955 map
->p
[i
] = basic_replace_space_by_local_space(map
->p
[i
],
7956 isl_local_space_copy(ls
));
7960 isl_space_free(isl_map_take_space(map
));
7961 map
= isl_map_restore_space(map
, isl_local_space_get_space(ls
));
7963 isl_local_space_free(ls
);
7966 isl_local_space_free(ls
);
7971 /* Compute an explicit representation for the existentially
7972 * quantified variables for which do not know any explicit representation yet.
7974 * We first sort the existentially quantified variables so that the
7975 * existentially quantified variables for which we already have an explicit
7976 * representation are placed before those for which we do not.
7977 * The input dimensions, the output dimensions and the existentially
7978 * quantified variables for which we already have an explicit
7979 * representation are then turned into parameters.
7980 * compute_divs returns a map with the same parameters and
7981 * no input or output dimensions and the dimension specification
7982 * is reset to that of the input, including the existentially quantified
7983 * variables for which we already had an explicit representation.
7985 static __isl_give isl_map
*compute_divs(__isl_take isl_basic_map
*bmap
)
7987 struct isl_basic_set
*bset
;
7988 struct isl_set
*set
;
7989 struct isl_map
*map
;
7991 isl_local_space
*ls
;
7998 bmap
= isl_basic_map_sort_divs(bmap
);
7999 bmap
= isl_basic_map_cow(bmap
);
8003 n_known
= isl_basic_map_first_unknown_div(bmap
);
8004 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
8005 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
8006 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
8007 if (n_known
< 0 || nparam
< 0 || n_in
< 0 || n_out
< 0)
8008 return isl_map_from_basic_map(isl_basic_map_free(bmap
));
8010 space
= isl_space_set_alloc(bmap
->ctx
,
8011 nparam
+ n_in
+ n_out
+ n_known
, 0);
8015 ls
= isl_basic_map_get_local_space(bmap
);
8016 ls
= isl_local_space_drop_dims(ls
, isl_dim_div
,
8017 n_known
, bmap
->n_div
- n_known
);
8019 for (i
= n_known
; i
< bmap
->n_div
; ++i
)
8020 swap_div(bmap
, i
- n_known
, i
);
8021 bmap
->n_div
-= n_known
;
8022 bmap
->extra
-= n_known
;
8024 bmap
= isl_basic_map_reset_space(bmap
, space
);
8025 bset
= bset_from_bmap(bmap
);
8027 set
= parameter_compute_divs(bset
);
8028 map
= set_to_map(set
);
8029 map
= replace_space_by_local_space(map
, ls
);
8033 isl_basic_map_free(bmap
);
8037 /* Remove the explicit representation of local variable "div",
8040 __isl_give isl_basic_map
*isl_basic_map_mark_div_unknown(
8041 __isl_take isl_basic_map
*bmap
, int div
)
8045 unknown
= isl_basic_map_div_is_marked_unknown(bmap
, div
);
8047 return isl_basic_map_free(bmap
);
8051 bmap
= isl_basic_map_cow(bmap
);
8054 isl_int_set_si(bmap
->div
[div
][0], 0);
8058 /* Is local variable "div" of "bmap" marked as not having an explicit
8060 * Note that even if "div" is not marked in this way and therefore
8061 * has an explicit representation, this representation may still
8062 * depend (indirectly) on other local variables that do not
8063 * have an explicit representation.
8065 isl_bool
isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map
*bmap
,
8068 if (isl_basic_map_check_range(bmap
, isl_dim_div
, div
, 1) < 0)
8069 return isl_bool_error
;
8070 return isl_int_is_zero(bmap
->div
[div
][0]);
8073 /* Return the position of the first local variable that does not
8074 * have an explicit representation.
8075 * Return the total number of local variables if they all have
8076 * an explicit representation.
8077 * Return -1 on error.
8079 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map
*bmap
)
8086 for (i
= 0; i
< bmap
->n_div
; ++i
) {
8087 if (!isl_basic_map_div_is_known(bmap
, i
))
8093 /* Return the position of the first local variable that does not
8094 * have an explicit representation.
8095 * Return the total number of local variables if they all have
8096 * an explicit representation.
8097 * Return -1 on error.
8099 int isl_basic_set_first_unknown_div(__isl_keep isl_basic_set
*bset
)
8101 return isl_basic_map_first_unknown_div(bset
);
8104 /* Does "bmap" have an explicit representation for all local variables?
8106 isl_bool
isl_basic_map_divs_known(__isl_keep isl_basic_map
*bmap
)
8111 n
= isl_basic_map_dim(bmap
, isl_dim_div
);
8112 first
= isl_basic_map_first_unknown_div(bmap
);
8113 if (n
< 0 || first
< 0)
8114 return isl_bool_error
;
8118 /* Do all basic maps in "map" have an explicit representation
8119 * for all local variables?
8121 isl_bool
isl_map_divs_known(__isl_keep isl_map
*map
)
8126 return isl_bool_error
;
8128 for (i
= 0; i
< map
->n
; ++i
) {
8129 int known
= isl_basic_map_divs_known(map
->p
[i
]);
8134 return isl_bool_true
;
8137 /* If bmap contains any unknown divs, then compute explicit
8138 * expressions for them. However, this computation may be
8139 * quite expensive, so first try to remove divs that aren't
8142 __isl_give isl_map
*isl_basic_map_compute_divs(__isl_take isl_basic_map
*bmap
)
8145 struct isl_map
*map
;
8147 known
= isl_basic_map_divs_known(bmap
);
8151 return isl_map_from_basic_map(bmap
);
8153 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
8155 known
= isl_basic_map_divs_known(bmap
);
8159 return isl_map_from_basic_map(bmap
);
8161 map
= compute_divs(bmap
);
8164 isl_basic_map_free(bmap
);
8168 __isl_give isl_map
*isl_map_compute_divs(__isl_take isl_map
*map
)
8172 struct isl_map
*res
;
8179 known
= isl_map_divs_known(map
);
8187 res
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[0]));
8188 for (i
= 1 ; i
< map
->n
; ++i
) {
8190 r2
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[i
]));
8191 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
8192 res
= isl_map_union_disjoint(res
, r2
);
8194 res
= isl_map_union(res
, r2
);
8201 __isl_give isl_set
*isl_basic_set_compute_divs(__isl_take isl_basic_set
*bset
)
8203 return set_from_map(isl_basic_map_compute_divs(bset_to_bmap(bset
)));
8206 __isl_give isl_set
*isl_set_compute_divs(__isl_take isl_set
*set
)
8208 return set_from_map(isl_map_compute_divs(set_to_map(set
)));
8211 __isl_give isl_set
*isl_map_domain(__isl_take isl_map
*map
)
8216 n_out
= isl_map_dim(map
, isl_dim_out
);
8218 return set_from_map(isl_map_free(map
));
8219 space
= isl_space_domain(isl_map_get_space(map
));
8221 map
= isl_map_project_out(map
, isl_dim_out
, 0, n_out
);
8223 return set_from_map(isl_map_reset_space(map
, space
));
8226 /* Return the union of "map1" and "map2", where we assume for now that
8227 * "map1" and "map2" are disjoint. Note that the basic maps inside
8228 * "map1" or "map2" may not be disjoint from each other.
8229 * Also note that this function is also called from isl_map_union,
8230 * which takes care of handling the situation where "map1" and "map2"
8231 * may not be disjoint.
8233 * If one of the inputs is empty, we can simply return the other input.
8234 * Similarly, if one of the inputs is universal, then it is equal to the union.
8236 static __isl_give isl_map
*map_union_disjoint(__isl_take isl_map
*map1
,
8237 __isl_take isl_map
*map2
)
8241 struct isl_map
*map
= NULL
;
8244 if (isl_map_check_equal_space(map1
, map2
) < 0)
8256 is_universe
= isl_map_plain_is_universe(map1
);
8257 if (is_universe
< 0)
8264 is_universe
= isl_map_plain_is_universe(map2
);
8265 if (is_universe
< 0)
8272 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
8273 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
8274 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
8276 map
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
8277 map1
->n
+ map2
->n
, flags
);
8280 for (i
= 0; i
< map1
->n
; ++i
) {
8281 map
= isl_map_add_basic_map(map
,
8282 isl_basic_map_copy(map1
->p
[i
]));
8286 for (i
= 0; i
< map2
->n
; ++i
) {
8287 map
= isl_map_add_basic_map(map
,
8288 isl_basic_map_copy(map2
->p
[i
]));
8302 /* Return the union of "map1" and "map2", where "map1" and "map2" are
8303 * guaranteed to be disjoint by the caller.
8305 * Note that this functions is called from within isl_map_make_disjoint,
8306 * so we have to be careful not to touch the constraints of the inputs
8309 __isl_give isl_map
*isl_map_union_disjoint(__isl_take isl_map
*map1
,
8310 __isl_take isl_map
*map2
)
8312 isl_map_align_params_bin(&map1
, &map2
);
8313 return map_union_disjoint(map1
, map2
);
8316 /* Return the union of "map1" and "map2", where "map1" and "map2" may
8319 * We currently simply call map_union_disjoint, the internal operation
8320 * of which does not really depend on the inputs being disjoint.
8321 * If the result contains more than one basic map, then we clear
8322 * the disjoint flag since the result may contain basic maps from
8323 * both inputs and these are not guaranteed to be disjoint.
8325 * As a special case, if "map1" and "map2" are obviously equal,
8326 * then we simply return "map1".
8328 __isl_give isl_map
*isl_map_union(__isl_take isl_map
*map1
,
8329 __isl_take isl_map
*map2
)
8333 if (isl_map_align_params_bin(&map1
, &map2
) < 0)
8336 equal
= isl_map_plain_is_equal(map1
, map2
);
8344 map1
= map_union_disjoint(map1
, map2
);
8348 ISL_F_CLR(map1
, ISL_MAP_DISJOINT
);
8356 __isl_give isl_set
*isl_set_union_disjoint(
8357 __isl_take isl_set
*set1
, __isl_take isl_set
*set2
)
8359 return set_from_map(isl_map_union_disjoint(set_to_map(set1
),
8363 __isl_give isl_set
*isl_set_union(__isl_take isl_set
*set1
,
8364 __isl_take isl_set
*set2
)
8366 return set_from_map(isl_map_union(set_to_map(set1
), set_to_map(set2
)));
8369 /* Apply "fn" to pairs of elements from "map" and "set" and collect
8370 * the results in a map living in "space".
8372 * "map" and "set" are assumed to be compatible and non-NULL.
8374 static __isl_give isl_map
*map_intersect_set(__isl_take isl_map
*map
,
8375 __isl_take isl_space
*space
, __isl_take isl_set
*set
,
8376 __isl_give isl_basic_map
*fn(__isl_take isl_basic_map
*bmap
,
8377 __isl_take isl_basic_set
*bset
))
8380 struct isl_map
*result
;
8383 if (isl_set_plain_is_universe(set
)) {
8385 return isl_map_reset_equal_dim_space(map
, space
);
8388 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
) &&
8389 ISL_F_ISSET(set
, ISL_MAP_DISJOINT
))
8390 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
8392 result
= isl_map_alloc_space(space
, map
->n
* set
->n
, flags
);
8393 for (i
= 0; result
&& i
< map
->n
; ++i
)
8394 for (j
= 0; j
< set
->n
; ++j
) {
8395 result
= isl_map_add_basic_map(result
,
8396 fn(isl_basic_map_copy(map
->p
[i
]),
8397 isl_basic_set_copy(set
->p
[j
])));
8407 __isl_give isl_map
*isl_map_intersect_range(__isl_take isl_map
*map
,
8408 __isl_take isl_set
*set
)
8413 isl_map_align_params_set(&map
, &set
);
8414 ok
= isl_map_compatible_range(map
, set
);
8418 isl_die(set
->ctx
, isl_error_invalid
,
8419 "incompatible spaces", goto error
);
8421 space
= isl_map_get_space(map
);
8422 return map_intersect_set(map
, space
, set
,
8423 &isl_basic_map_intersect_range
);
8430 /* Intersect the domain of "map" with "set".
8432 * If the domain dimensions of "map" do not have any identifiers,
8433 * then copy them over from "set".
8435 __isl_give isl_map
*isl_map_intersect_domain(__isl_take isl_map
*map
,
8436 __isl_take isl_set
*set
)
8441 isl_map_align_params_set(&map
, &set
);
8442 ok
= isl_map_compatible_domain(map
, set
);
8446 isl_die(set
->ctx
, isl_error_invalid
,
8447 "incompatible spaces", goto error
);
8449 space
= isl_map_get_space(map
);
8450 space
= isl_space_copy_ids_if_unset(space
, isl_dim_in
,
8451 isl_set_peek_space(set
), isl_dim_set
);
8452 return map_intersect_set(map
, space
, set
,
8453 &isl_basic_map_intersect_domain
);
8463 #include "isl_copy_tuple_id_templ.c"
8465 /* Data structure that specifies how isl_map_intersect_factor
8468 * "preserve_type" is the tuple where the factor differs from
8469 * the input map and of which the identifiers needs
8470 * to be preserved explicitly.
8471 * "other_factor" is used to extract the space of the other factor
8472 * from the space of the product ("map").
8473 * "product" is used to combine the given factor and a universe map
8474 * in the space returned by "other_factor" to produce a map
8475 * that lives in the same space as the input map.
8477 struct isl_intersect_factor_control
{
8478 enum isl_dim_type preserve_type
;
8479 __isl_give isl_space
*(*other_factor
)(__isl_take isl_space
*space
);
8480 __isl_give isl_map
*(*product
)(__isl_take isl_map
*factor
,
8481 __isl_take isl_map
*other
);
8484 /* Given a map "map" in some product space and a map "factor"
8485 * living in some factor space, return the intersection.
8487 * After aligning the parameters,
8488 * the map "factor" is first extended to a map living in the same space
8489 * as "map" and then a regular intersection is computed.
8491 * Note that the extension is computed as a product, which is anonymous
8492 * by default. If "map" has an identifier on the corresponding tuple,
8493 * then this identifier needs to be set on the product
8494 * before the intersection is computed.
8496 static __isl_give isl_map
*isl_map_intersect_factor(
8497 __isl_take isl_map
*map
, __isl_take isl_map
*factor
,
8498 struct isl_intersect_factor_control
*control
)
8502 isl_map
*other
, *product
;
8504 equal
= isl_map_has_equal_params(map
, factor
);
8508 map
= isl_map_align_params(map
, isl_map_get_space(factor
));
8509 factor
= isl_map_align_params(factor
, isl_map_get_space(map
));
8512 space
= isl_map_get_space(map
);
8513 other
= isl_map_universe(control
->other_factor(space
));
8514 product
= control
->product(factor
, other
);
8516 space
= isl_map_peek_space(map
);
8517 product
= isl_map_copy_tuple_id(product
, control
->preserve_type
,
8518 space
, control
->preserve_type
);
8519 return map_intersect(map
, product
);
8522 isl_map_free(factor
);
8526 /* Return the domain product of "map2" and "map1".
8528 static __isl_give isl_map
*isl_map_reverse_domain_product(
8529 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
8531 return isl_map_domain_product(map2
, map1
);
8534 /* Return the range product of "map2" and "map1".
8536 static __isl_give isl_map
*isl_map_reverse_range_product(
8537 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
8539 return isl_map_range_product(map2
, map1
);
8542 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
8543 * in the space A -> C, return the intersection.
8545 __isl_give isl_map
*isl_map_intersect_domain_factor_domain(
8546 __isl_take isl_map
*map
, __isl_take isl_map
*factor
)
8548 struct isl_intersect_factor_control control
= {
8549 .preserve_type
= isl_dim_in
,
8550 .other_factor
= isl_space_domain_factor_range
,
8551 .product
= isl_map_domain_product
,
8554 return isl_map_intersect_factor(map
, factor
, &control
);
8557 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
8558 * in the space B -> C, return the intersection.
8560 __isl_give isl_map
*isl_map_intersect_domain_factor_range(
8561 __isl_take isl_map
*map
, __isl_take isl_map
*factor
)
8563 struct isl_intersect_factor_control control
= {
8564 .preserve_type
= isl_dim_in
,
8565 .other_factor
= isl_space_domain_factor_domain
,
8566 .product
= isl_map_reverse_domain_product
,
8569 return isl_map_intersect_factor(map
, factor
, &control
);
8572 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
8573 * in the space A -> B, return the intersection.
8575 __isl_give isl_map
*isl_map_intersect_range_factor_domain(
8576 __isl_take isl_map
*map
, __isl_take isl_map
*factor
)
8578 struct isl_intersect_factor_control control
= {
8579 .preserve_type
= isl_dim_out
,
8580 .other_factor
= isl_space_range_factor_range
,
8581 .product
= isl_map_range_product
,
8584 return isl_map_intersect_factor(map
, factor
, &control
);
8587 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
8588 * in the space A -> C, return the intersection.
8590 __isl_give isl_map
*isl_map_intersect_range_factor_range(
8591 __isl_take isl_map
*map
, __isl_take isl_map
*factor
)
8593 struct isl_intersect_factor_control control
= {
8594 .preserve_type
= isl_dim_out
,
8595 .other_factor
= isl_space_range_factor_domain
,
8596 .product
= isl_map_reverse_range_product
,
8599 return isl_map_intersect_factor(map
, factor
, &control
);
8602 /* Given a set "set" in a space [A -> B] and a set "domain"
8603 * in the space A, return the intersection.
8605 * The set "domain" is first extended to a set living in the space
8606 * [A -> B] and then a regular intersection is computed.
8608 __isl_give isl_set
*isl_set_intersect_factor_domain(__isl_take isl_set
*set
,
8609 __isl_take isl_set
*domain
)
8611 struct isl_intersect_factor_control control
= {
8612 .preserve_type
= isl_dim_set
,
8613 .other_factor
= isl_space_factor_range
,
8614 .product
= isl_map_range_product
,
8617 return set_from_map(isl_map_intersect_factor(set_to_map(set
),
8618 set_to_map(domain
), &control
));
8621 /* Given a set "set" in a space [A -> B] and a set "range"
8622 * in the space B, return the intersection.
8624 * The set "range" is first extended to a set living in the space
8625 * [A -> B] and then a regular intersection is computed.
8627 __isl_give isl_set
*isl_set_intersect_factor_range(__isl_take isl_set
*set
,
8628 __isl_take isl_set
*range
)
8630 struct isl_intersect_factor_control control
= {
8631 .preserve_type
= isl_dim_set
,
8632 .other_factor
= isl_space_factor_domain
,
8633 .product
= isl_map_reverse_range_product
,
8636 return set_from_map(isl_map_intersect_factor(set_to_map(set
),
8637 set_to_map(range
), &control
));
8643 #include "isl_copy_tuple_id_templ.c"
8645 /* Given a map "map" in a space [A -> B] -> C and a set "domain"
8646 * in the space A, return the intersection.
8648 * The set "domain" is extended to a set living in the space [A -> B] and
8649 * the domain of "map" is intersected with this set.
8651 * If "map" has an identifier on the domain tuple,
8652 * then this identifier needs to be set on this product
8653 * before the intersection is computed.
8655 __isl_give isl_map
*isl_map_intersect_domain_wrapped_domain(
8656 __isl_take isl_map
*map
, __isl_take isl_set
*domain
)
8661 isl_map_align_params_set(&map
, &domain
);
8662 space
= isl_map_get_space(map
);
8663 space
= isl_space_domain_wrapped_range(space
);
8664 factor
= isl_set_universe(space
);
8665 domain
= isl_set_product(domain
, factor
);
8666 space
= isl_map_peek_space(map
);
8667 domain
= isl_set_copy_tuple_id(domain
, isl_dim_set
, space
, isl_dim_in
);
8668 return isl_map_intersect_domain(map
, domain
);
8671 /* Given a map "map" in a space A -> [B -> C] and a set "domain"
8672 * in the space B, return the intersection.
8674 * The set "domain" is extended to a set living in the space [B -> C] and
8675 * the range of "map" is intersected with this set.
8677 * If "map" has an identifier on the range tuple,
8678 * then this identifier needs to be set on this product
8679 * before the intersection is computed.
8681 __isl_give isl_map
*isl_map_intersect_range_wrapped_domain(
8682 __isl_take isl_map
*map
, __isl_take isl_set
*domain
)
8687 isl_map_align_params_set(&map
, &domain
);
8688 space
= isl_map_get_space(map
);
8689 space
= isl_space_range_wrapped_range(space
);
8690 factor
= isl_set_universe(space
);
8691 domain
= isl_set_product(domain
, factor
);
8692 space
= isl_map_peek_space(map
);
8693 domain
= isl_set_copy_tuple_id(domain
, isl_dim_set
, space
, isl_dim_out
);
8694 return isl_map_intersect_range(map
, domain
);
8697 __isl_give isl_map
*isl_map_apply_domain(__isl_take isl_map
*map1
,
8698 __isl_take isl_map
*map2
)
8700 if (isl_map_align_params_bin(&map1
, &map2
) < 0)
8702 map1
= isl_map_reverse(map1
);
8703 map1
= isl_map_apply_range(map1
, map2
);
8704 return isl_map_reverse(map1
);
8711 __isl_give isl_map
*isl_map_apply_range(__isl_take isl_map
*map1
,
8712 __isl_take isl_map
*map2
)
8715 struct isl_map
*result
;
8718 if (isl_map_align_params_bin(&map1
, &map2
) < 0)
8721 space
= isl_space_join(isl_space_copy(map1
->dim
),
8722 isl_space_copy(map2
->dim
));
8724 result
= isl_map_alloc_space(space
, map1
->n
* map2
->n
, 0);
8727 for (i
= 0; i
< map1
->n
; ++i
)
8728 for (j
= 0; j
< map2
->n
; ++j
) {
8729 result
= isl_map_add_basic_map(result
,
8730 isl_basic_map_apply_range(
8731 isl_basic_map_copy(map1
->p
[i
]),
8732 isl_basic_map_copy(map2
->p
[j
])));
8738 if (result
&& result
->n
<= 1)
8739 ISL_F_SET(result
, ISL_MAP_DISJOINT
);
8747 /* Is "bmap" a transformation, i.e.,
8748 * does it relate elements from the same space.
8750 isl_bool
isl_basic_map_is_transformation(__isl_keep isl_basic_map
*bmap
)
8754 space
= isl_basic_map_peek_space(bmap
);
8755 return isl_space_tuple_is_equal(space
, isl_dim_in
, space
, isl_dim_out
);
8758 /* Check that "bmap" is a transformation, i.e.,
8759 * that it relates elements from the same space.
8761 static isl_stat
isl_basic_map_check_transformation(
8762 __isl_keep isl_basic_map
*bmap
)
8766 equal
= isl_basic_map_is_transformation(bmap
);
8768 return isl_stat_error
;
8770 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
8771 "domain and range don't match", return isl_stat_error
);
8776 * returns range - domain
8778 __isl_give isl_basic_set
*isl_basic_map_deltas(__isl_take isl_basic_map
*bmap
)
8780 isl_space
*target_space
;
8781 struct isl_basic_set
*bset
;
8787 if (isl_basic_map_check_transformation(bmap
) < 0)
8788 return isl_basic_map_free(bmap
);
8789 dim
= isl_basic_map_dim(bmap
, isl_dim_in
);
8790 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
8791 if (dim
< 0 || nparam
< 0)
8793 target_space
= isl_space_domain(isl_basic_map_get_space(bmap
));
8794 bmap
= isl_basic_map_from_range(isl_basic_map_wrap(bmap
));
8795 bmap
= isl_basic_map_add_dims(bmap
, isl_dim_in
, dim
);
8796 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
8798 bmap
= isl_basic_map_free(bmap
);
8799 bmap
= isl_basic_map_extend_constraints(bmap
, dim
, 0);
8800 for (i
= 0; i
< dim
; ++i
) {
8801 int j
= isl_basic_map_alloc_equality(bmap
);
8803 bmap
= isl_basic_map_free(bmap
);
8806 isl_seq_clr(bmap
->eq
[j
], 1 + total
);
8807 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
8808 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], 1);
8809 isl_int_set_si(bmap
->eq
[j
][1+nparam
+2*dim
+i
], -1);
8811 bset
= isl_basic_map_domain(bmap
);
8812 bset
= isl_basic_set_reset_space(bset
, target_space
);
8815 isl_basic_map_free(bmap
);
8819 /* Is the tuple of type "type1" of "map" the same as
8820 * the tuple of type "type2" of "space"?
8822 isl_bool
isl_map_space_tuple_is_equal(__isl_keep isl_map
*map
,
8823 enum isl_dim_type type1
, __isl_keep isl_space
*space
,
8824 enum isl_dim_type type2
)
8826 isl_space
*map_space
;
8828 map_space
= isl_map_peek_space(map
);
8829 return isl_space_tuple_is_equal(map_space
, type1
, space
, type2
);
8832 /* Is the tuple of type "type1" of "map1" the same as
8833 * the tuple of type "type2" of "map2"?
8835 isl_bool
isl_map_tuple_is_equal(__isl_keep isl_map
*map1
,
8836 enum isl_dim_type type1
, __isl_keep isl_map
*map2
,
8837 enum isl_dim_type type2
)
8839 isl_space
*space1
, *space2
;
8841 space1
= isl_map_peek_space(map1
);
8842 space2
= isl_map_peek_space(map2
);
8843 return isl_space_tuple_is_equal(space1
, type1
, space2
, type2
);
8846 /* Is the space of "obj" equal to "space", ignoring parameters?
8848 isl_bool
isl_map_has_space_tuples(__isl_keep isl_map
*map
,
8849 __isl_keep isl_space
*space
)
8851 isl_space
*map_space
;
8853 map_space
= isl_map_peek_space(map
);
8854 return isl_space_has_equal_tuples(map_space
, space
);
8857 /* Check that "map" is a transformation, i.e.,
8858 * that it relates elements from the same space.
8860 isl_stat
isl_map_check_transformation(__isl_keep isl_map
*map
)
8864 equal
= isl_map_tuple_is_equal(map
, isl_dim_in
, map
, isl_dim_out
);
8866 return isl_stat_error
;
8868 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
8869 "domain and range don't match", return isl_stat_error
);
8874 * returns range - domain
8876 __isl_give isl_set
*isl_map_deltas(__isl_take isl_map
*map
)
8880 struct isl_set
*result
;
8882 if (isl_map_check_transformation(map
) < 0)
8884 space
= isl_map_get_space(map
);
8885 space
= isl_space_domain(space
);
8886 result
= isl_set_alloc_space(space
, map
->n
, 0);
8889 for (i
= 0; i
< map
->n
; ++i
)
8890 result
= isl_set_add_basic_set(result
,
8891 isl_basic_map_deltas(isl_basic_map_copy(map
->p
[i
])));
8900 * returns [domain -> range] -> range - domain
8902 __isl_give isl_basic_map
*isl_basic_map_deltas_map(
8903 __isl_take isl_basic_map
*bmap
)
8907 isl_basic_map
*domain
;
8911 if (isl_basic_map_check_transformation(bmap
) < 0)
8912 return isl_basic_map_free(bmap
);
8914 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
8915 n
= isl_basic_map_dim(bmap
, isl_dim_in
);
8916 if (nparam
< 0 || n
< 0)
8917 return isl_basic_map_free(bmap
);
8919 space
= isl_basic_map_get_space(bmap
);
8920 space
= isl_space_from_range(isl_space_domain(space
));
8921 domain
= isl_basic_map_universe(space
);
8923 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
8924 bmap
= isl_basic_map_apply_range(bmap
, domain
);
8925 bmap
= isl_basic_map_extend_constraints(bmap
, n
, 0);
8927 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
8929 return isl_basic_map_free(bmap
);
8931 for (i
= 0; i
< n
; ++i
) {
8932 k
= isl_basic_map_alloc_equality(bmap
);
8935 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
8936 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], 1);
8937 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ i
], -1);
8938 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ n
+ i
], 1);
8941 bmap
= isl_basic_map_gauss(bmap
, NULL
);
8942 return isl_basic_map_finalize(bmap
);
8944 isl_basic_map_free(bmap
);
8949 * returns [domain -> range] -> range - domain
8951 __isl_give isl_map
*isl_map_deltas_map(__isl_take isl_map
*map
)
8953 if (isl_map_check_transformation(map
) < 0)
8954 return isl_map_free(map
);
8956 return isl_map_transform(map
, &isl_space_range_map
,
8957 &isl_basic_map_deltas_map
);
8960 /* Return pairs of elements { x -> y } such that y - x is in "deltas".
8962 __isl_give isl_map
*isl_set_translation(__isl_take isl_set
*deltas
)
8967 space
= isl_space_map_from_set(isl_set_get_space(deltas
));
8968 map
= isl_map_deltas_map(isl_map_universe(space
));
8969 map
= isl_map_intersect_range(map
, deltas
);
8971 return isl_set_unwrap(isl_map_domain(map
));
8974 __isl_give isl_basic_map
*isl_basic_map_identity(__isl_take isl_space
*space
)
8976 isl_size n_in
, n_out
;
8978 n_in
= isl_space_dim(space
, isl_dim_in
);
8979 n_out
= isl_space_dim(space
, isl_dim_out
);
8980 if (n_in
< 0 || n_out
< 0)
8983 isl_die(space
->ctx
, isl_error_invalid
,
8984 "number of input and output dimensions needs to be "
8985 "the same", goto error
);
8986 return isl_basic_map_equal(space
, n_in
);
8988 isl_space_free(space
);
8992 __isl_give isl_map
*isl_map_identity(__isl_take isl_space
*space
)
8994 return isl_map_from_basic_map(isl_basic_map_identity(space
));
8997 __isl_give isl_map
*isl_set_identity(__isl_take isl_set
*set
)
8999 isl_space
*space
= isl_set_get_space(set
);
9001 id
= isl_map_identity(isl_space_map_from_set(space
));
9002 return isl_map_intersect_range(id
, set
);
9005 /* Construct a basic set with all set dimensions having only non-negative
9008 __isl_give isl_basic_set
*isl_basic_set_positive_orthant(
9009 __isl_take isl_space
*space
)
9015 struct isl_basic_set
*bset
;
9017 nparam
= isl_space_dim(space
, isl_dim_param
);
9018 dim
= isl_space_dim(space
, isl_dim_set
);
9019 total
= isl_space_dim(space
, isl_dim_all
);
9020 if (nparam
< 0 || dim
< 0 || total
< 0)
9021 space
= isl_space_free(space
);
9022 bset
= isl_basic_set_alloc_space(space
, 0, 0, dim
);
9025 for (i
= 0; i
< dim
; ++i
) {
9026 int k
= isl_basic_set_alloc_inequality(bset
);
9029 isl_seq_clr(bset
->ineq
[k
], 1 + total
);
9030 isl_int_set_si(bset
->ineq
[k
][1 + nparam
+ i
], 1);
9034 isl_basic_set_free(bset
);
9038 /* Construct the half-space x_pos >= 0.
9040 static __isl_give isl_basic_set
*nonneg_halfspace(__isl_take isl_space
*space
,
9045 isl_basic_set
*nonneg
;
9047 total
= isl_space_dim(space
, isl_dim_all
);
9049 space
= isl_space_free(space
);
9050 nonneg
= isl_basic_set_alloc_space(space
, 0, 0, 1);
9051 k
= isl_basic_set_alloc_inequality(nonneg
);
9054 isl_seq_clr(nonneg
->ineq
[k
], 1 + total
);
9055 isl_int_set_si(nonneg
->ineq
[k
][pos
], 1);
9057 return isl_basic_set_finalize(nonneg
);
9059 isl_basic_set_free(nonneg
);
9063 /* Construct the half-space x_pos <= -1.
9065 static __isl_give isl_basic_set
*neg_halfspace(__isl_take isl_space
*space
,
9072 total
= isl_space_dim(space
, isl_dim_all
);
9074 space
= isl_space_free(space
);
9075 neg
= isl_basic_set_alloc_space(space
, 0, 0, 1);
9076 k
= isl_basic_set_alloc_inequality(neg
);
9079 isl_seq_clr(neg
->ineq
[k
], 1 + total
);
9080 isl_int_set_si(neg
->ineq
[k
][0], -1);
9081 isl_int_set_si(neg
->ineq
[k
][pos
], -1);
9083 return isl_basic_set_finalize(neg
);
9085 isl_basic_set_free(neg
);
9089 __isl_give isl_set
*isl_set_split_dims(__isl_take isl_set
*set
,
9090 enum isl_dim_type type
, unsigned first
, unsigned n
)
9094 isl_basic_set
*nonneg
;
9100 if (isl_set_check_range(set
, type
, first
, n
) < 0)
9101 return isl_set_free(set
);
9103 offset
= pos(set
->dim
, type
);
9104 for (i
= 0; i
< n
; ++i
) {
9105 nonneg
= nonneg_halfspace(isl_set_get_space(set
),
9106 offset
+ first
+ i
);
9107 neg
= neg_halfspace(isl_set_get_space(set
), offset
+ first
+ i
);
9109 set
= isl_set_intersect(set
, isl_basic_set_union(nonneg
, neg
));
9115 static isl_stat
foreach_orthant(__isl_take isl_set
*set
, int *signs
, int first
,
9117 isl_stat (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
9123 return isl_stat_error
;
9124 if (isl_set_plain_is_empty(set
)) {
9129 return fn(set
, signs
, user
);
9132 half
= isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set
),
9134 half
= isl_set_intersect(half
, isl_set_copy(set
));
9135 if (foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
) < 0)
9139 half
= isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set
),
9141 half
= isl_set_intersect(half
, set
);
9142 return foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
);
9145 return isl_stat_error
;
9148 /* Call "fn" on the intersections of "set" with each of the orthants
9149 * (except for obviously empty intersections). The orthant is identified
9150 * by the signs array, with each entry having value 1 or -1 according
9151 * to the sign of the corresponding variable.
9153 isl_stat
isl_set_foreach_orthant(__isl_keep isl_set
*set
,
9154 isl_stat (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
9163 return isl_stat_error
;
9164 if (isl_set_plain_is_empty(set
))
9167 nparam
= isl_set_dim(set
, isl_dim_param
);
9168 nvar
= isl_set_dim(set
, isl_dim_set
);
9169 if (nparam
< 0 || nvar
< 0)
9170 return isl_stat_error
;
9172 signs
= isl_alloc_array(set
->ctx
, int, nparam
+ nvar
);
9174 r
= foreach_orthant(isl_set_copy(set
), signs
, 0, nparam
+ nvar
,
9182 isl_bool
isl_set_is_equal(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
9184 return isl_map_is_equal(set_to_map(set1
), set_to_map(set2
));
9187 isl_bool
isl_basic_map_is_subset(__isl_keep isl_basic_map
*bmap1
,
9188 __isl_keep isl_basic_map
*bmap2
)
9191 struct isl_map
*map1
;
9192 struct isl_map
*map2
;
9194 if (!bmap1
|| !bmap2
)
9195 return isl_bool_error
;
9197 map1
= isl_map_from_basic_map(isl_basic_map_copy(bmap1
));
9198 map2
= isl_map_from_basic_map(isl_basic_map_copy(bmap2
));
9200 is_subset
= isl_map_is_subset(map1
, map2
);
9208 isl_bool
isl_basic_set_is_subset(__isl_keep isl_basic_set
*bset1
,
9209 __isl_keep isl_basic_set
*bset2
)
9211 return isl_basic_map_is_subset(bset1
, bset2
);
9214 isl_bool
isl_basic_map_is_equal(__isl_keep isl_basic_map
*bmap1
,
9215 __isl_keep isl_basic_map
*bmap2
)
9219 if (!bmap1
|| !bmap2
)
9220 return isl_bool_error
;
9221 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
9222 if (is_subset
!= isl_bool_true
)
9224 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
9228 isl_bool
isl_basic_set_is_equal(__isl_keep isl_basic_set
*bset1
,
9229 __isl_keep isl_basic_set
*bset2
)
9231 return isl_basic_map_is_equal(
9232 bset_to_bmap(bset1
), bset_to_bmap(bset2
));
9235 isl_bool
isl_map_is_empty(__isl_keep isl_map
*map
)
9241 return isl_bool_error
;
9242 for (i
= 0; i
< map
->n
; ++i
) {
9243 is_empty
= isl_basic_map_is_empty(map
->p
[i
]);
9245 return isl_bool_error
;
9247 return isl_bool_false
;
9249 return isl_bool_true
;
9252 isl_bool
isl_map_plain_is_empty(__isl_keep isl_map
*map
)
9254 return map
? map
->n
== 0 : isl_bool_error
;
9257 isl_bool
isl_set_plain_is_empty(__isl_keep isl_set
*set
)
9259 return set
? set
->n
== 0 : isl_bool_error
;
9262 isl_bool
isl_set_is_empty(__isl_keep isl_set
*set
)
9264 return isl_map_is_empty(set_to_map(set
));
9268 #define TYPE isl_basic_map
9271 #include "isl_type_has_equal_space_bin_templ.c"
9272 #include "isl_type_check_equal_space_templ.c"
9274 /* Check that "bset1" and "bset2" live in the same space,
9275 * reporting an error if they do not.
9277 isl_stat
isl_basic_set_check_equal_space(__isl_keep isl_basic_set
*bset1
,
9278 __isl_keep isl_basic_set
*bset2
)
9280 return isl_basic_map_check_equal_space(bset_to_bmap(bset1
),
9281 bset_to_bmap(bset1
));
9285 #define TYPE isl_map
9287 #include "isl_type_has_equal_space_bin_templ.c"
9288 #include "isl_type_check_equal_space_templ.c"
9289 #include "isl_type_has_space_templ.c"
9291 isl_bool
isl_set_has_equal_space(__isl_keep isl_set
*set1
,
9292 __isl_keep isl_set
*set2
)
9294 return isl_map_has_equal_space(set_to_map(set1
), set_to_map(set2
));
9298 #define TYPE1 isl_map
9300 #define TYPE2 isl_basic_map
9302 #define TYPE_PAIR isl_map_basic_map
9305 #include "isl_type_has_equal_space_templ.c"
9306 #include "isl_type_check_equal_space_templ.c"
9308 /* Check that "set" and "bset" live in the same space,
9309 * reporting an error if they do not.
9311 isl_stat
isl_set_basic_set_check_equal_space(__isl_keep isl_set
*set
,
9312 __isl_keep isl_basic_set
*bset
)
9314 return isl_map_basic_map_check_equal_space(set_to_map(set
),
9315 bset_to_bmap(bset
));
9318 static isl_bool
map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
9323 return isl_bool_error
;
9324 is_subset
= isl_map_is_subset(map1
, map2
);
9325 if (is_subset
!= isl_bool_true
)
9327 is_subset
= isl_map_is_subset(map2
, map1
);
9331 /* Is "map1" equal to "map2"?
9333 * First check if they are obviously equal.
9334 * If not, then perform a more detailed analysis.
9336 isl_bool
isl_map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
9340 equal
= isl_map_plain_is_equal(map1
, map2
);
9341 if (equal
< 0 || equal
)
9343 return isl_map_align_params_map_map_and_test(map1
, map2
, &map_is_equal
);
9346 isl_bool
isl_basic_map_is_strict_subset(__isl_keep isl_basic_map
*bmap1
,
9347 __isl_keep isl_basic_map
*bmap2
)
9351 if (!bmap1
|| !bmap2
)
9352 return isl_bool_error
;
9353 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
9354 if (is_subset
!= isl_bool_true
)
9356 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
9357 return isl_bool_not(is_subset
);
9360 isl_bool
isl_map_is_strict_subset(__isl_keep isl_map
*map1
,
9361 __isl_keep isl_map
*map2
)
9366 return isl_bool_error
;
9367 is_subset
= isl_map_is_subset(map1
, map2
);
9368 if (is_subset
!= isl_bool_true
)
9370 is_subset
= isl_map_is_subset(map2
, map1
);
9371 return isl_bool_not(is_subset
);
9374 isl_bool
isl_set_is_strict_subset(__isl_keep isl_set
*set1
,
9375 __isl_keep isl_set
*set2
)
9377 return isl_map_is_strict_subset(set_to_map(set1
), set_to_map(set2
));
9380 /* Is "bmap" obviously equal to the universe with the same space?
9382 * That is, does it not have any constraints?
9384 isl_bool
isl_basic_map_plain_is_universe(__isl_keep isl_basic_map
*bmap
)
9387 return isl_bool_error
;
9388 return bmap
->n_eq
== 0 && bmap
->n_ineq
== 0;
9391 /* Is "bset" obviously equal to the universe with the same space?
9393 isl_bool
isl_basic_set_plain_is_universe(__isl_keep isl_basic_set
*bset
)
9395 return isl_basic_map_plain_is_universe(bset
);
9398 /* If "c" does not involve any existentially quantified variables,
9399 * then set *univ to false and abort
9401 static isl_stat
involves_divs(__isl_take isl_constraint
*c
, void *user
)
9403 isl_bool
*univ
= user
;
9406 n
= isl_constraint_dim(c
, isl_dim_div
);
9408 c
= isl_constraint_free(c
);
9409 *univ
= isl_constraint_involves_dims(c
, isl_dim_div
, 0, n
);
9410 isl_constraint_free(c
);
9411 if (*univ
< 0 || !*univ
)
9412 return isl_stat_error
;
9416 /* Is "bmap" equal to the universe with the same space?
9418 * First check if it is obviously equal to the universe.
9419 * If not and if there are any constraints not involving
9420 * existentially quantified variables, then it is certainly
9421 * not equal to the universe.
9422 * Otherwise, check if the universe is a subset of "bmap".
9424 isl_bool
isl_basic_map_is_universe(__isl_keep isl_basic_map
*bmap
)
9428 isl_basic_map
*test
;
9430 univ
= isl_basic_map_plain_is_universe(bmap
);
9431 if (univ
< 0 || univ
)
9433 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
9435 return isl_bool_error
;
9437 return isl_bool_false
;
9438 univ
= isl_bool_true
;
9439 if (isl_basic_map_foreach_constraint(bmap
, &involves_divs
, &univ
) < 0 &&
9441 return isl_bool_error
;
9442 if (univ
< 0 || !univ
)
9444 test
= isl_basic_map_universe(isl_basic_map_get_space(bmap
));
9445 univ
= isl_basic_map_is_subset(test
, bmap
);
9446 isl_basic_map_free(test
);
9450 /* Is "bset" equal to the universe with the same space?
9452 isl_bool
isl_basic_set_is_universe(__isl_keep isl_basic_set
*bset
)
9454 return isl_basic_map_is_universe(bset
);
9457 isl_bool
isl_map_plain_is_universe(__isl_keep isl_map
*map
)
9462 return isl_bool_error
;
9464 for (i
= 0; i
< map
->n
; ++i
) {
9465 isl_bool r
= isl_basic_map_plain_is_universe(map
->p
[i
]);
9470 return isl_bool_false
;
9473 isl_bool
isl_set_plain_is_universe(__isl_keep isl_set
*set
)
9475 return isl_map_plain_is_universe(set_to_map(set
));
9478 isl_bool
isl_basic_map_is_empty(__isl_keep isl_basic_map
*bmap
)
9480 struct isl_basic_set
*bset
= NULL
;
9481 struct isl_vec
*sample
= NULL
;
9482 isl_bool empty
, non_empty
;
9485 return isl_bool_error
;
9487 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
9488 return isl_bool_true
;
9490 if (isl_basic_map_plain_is_universe(bmap
))
9491 return isl_bool_false
;
9493 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
9494 struct isl_basic_map
*copy
= isl_basic_map_copy(bmap
);
9495 copy
= isl_basic_map_remove_redundancies(copy
);
9496 empty
= isl_basic_map_plain_is_empty(copy
);
9497 isl_basic_map_free(copy
);
9501 non_empty
= isl_basic_map_plain_is_non_empty(bmap
);
9503 return isl_bool_error
;
9505 return isl_bool_false
;
9506 isl_vec_free(bmap
->sample
);
9507 bmap
->sample
= NULL
;
9508 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
9510 return isl_bool_error
;
9511 sample
= isl_basic_set_sample_vec(bset
);
9513 return isl_bool_error
;
9514 empty
= sample
->size
== 0;
9515 isl_vec_free(bmap
->sample
);
9516 bmap
->sample
= sample
;
9518 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
9523 isl_bool
isl_basic_map_plain_is_empty(__isl_keep isl_basic_map
*bmap
)
9526 return isl_bool_error
;
9527 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
);
9530 isl_bool
isl_basic_set_plain_is_empty(__isl_keep isl_basic_set
*bset
)
9533 return isl_bool_error
;
9534 return ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
);
9537 /* Is "bmap" known to be non-empty?
9539 * That is, is the cached sample still valid?
9541 isl_bool
isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map
*bmap
)
9546 return isl_bool_error
;
9548 return isl_bool_false
;
9549 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
9551 return isl_bool_error
;
9552 if (bmap
->sample
->size
!= 1 + total
)
9553 return isl_bool_false
;
9554 return isl_basic_map_contains(bmap
, bmap
->sample
);
9557 isl_bool
isl_basic_set_is_empty(__isl_keep isl_basic_set
*bset
)
9559 return isl_basic_map_is_empty(bset_to_bmap(bset
));
9562 __isl_give isl_map
*isl_basic_map_union(__isl_take isl_basic_map
*bmap1
,
9563 __isl_take isl_basic_map
*bmap2
)
9565 struct isl_map
*map
;
9567 if (isl_basic_map_check_equal_space(bmap1
, bmap2
) < 0)
9570 map
= isl_map_alloc_space(isl_space_copy(bmap1
->dim
), 2, 0);
9573 map
= isl_map_add_basic_map(map
, bmap1
);
9574 map
= isl_map_add_basic_map(map
, bmap2
);
9577 isl_basic_map_free(bmap1
);
9578 isl_basic_map_free(bmap2
);
9582 __isl_give isl_set
*isl_basic_set_union(__isl_take isl_basic_set
*bset1
,
9583 __isl_take isl_basic_set
*bset2
)
9585 return set_from_map(isl_basic_map_union(bset_to_bmap(bset1
),
9586 bset_to_bmap(bset2
)));
9589 /* Order divs such that any div only depends on previous divs */
9590 __isl_give isl_basic_map
*isl_basic_map_order_divs(
9591 __isl_take isl_basic_map
*bmap
)
9596 off
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
9598 return isl_basic_map_free(bmap
);
9600 for (i
= 0; i
< bmap
->n_div
; ++i
) {
9602 if (isl_int_is_zero(bmap
->div
[i
][0]))
9604 pos
= isl_seq_first_non_zero(bmap
->div
[i
]+1+1+off
+i
,
9609 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_internal
,
9610 "integer division depends on itself",
9611 return isl_basic_map_free(bmap
));
9612 bmap
= isl_basic_map_swap_div(bmap
, i
, i
+ pos
);
9620 __isl_give isl_map
*isl_map_order_divs(__isl_take isl_map
*map
)
9627 for (i
= 0; i
< map
->n
; ++i
) {
9628 map
->p
[i
] = isl_basic_map_order_divs(map
->p
[i
]);
9639 /* Sort the local variables of "bset".
9641 __isl_give isl_basic_set
*isl_basic_set_sort_divs(
9642 __isl_take isl_basic_set
*bset
)
9644 return bset_from_bmap(isl_basic_map_sort_divs(bset_to_bmap(bset
)));
9647 /* Apply the expansion computed by isl_merge_divs.
9648 * The expansion itself is given by "exp" while the resulting
9649 * list of divs is given by "div".
9651 * Move the integer divisions of "bmap" into the right position
9652 * according to "exp" and then introduce the additional integer
9653 * divisions, adding div constraints.
9654 * The moving should be done first to avoid moving coefficients
9655 * in the definitions of the extra integer divisions.
9657 __isl_give isl_basic_map
*isl_basic_map_expand_divs(
9658 __isl_take isl_basic_map
*bmap
, __isl_take isl_mat
*div
, int *exp
)
9663 bmap
= isl_basic_map_cow(bmap
);
9667 if (div
->n_row
< bmap
->n_div
)
9668 isl_die(isl_mat_get_ctx(div
), isl_error_invalid
,
9669 "not an expansion", goto error
);
9671 n_div
= bmap
->n_div
;
9672 bmap
= isl_basic_map_extend(bmap
, div
->n_row
- n_div
, 0,
9673 2 * (div
->n_row
- n_div
));
9675 for (i
= n_div
; i
< div
->n_row
; ++i
)
9676 if (isl_basic_map_alloc_div(bmap
) < 0)
9679 for (j
= n_div
- 1; j
>= 0; --j
) {
9682 bmap
= isl_basic_map_swap_div(bmap
, j
, exp
[j
]);
9687 for (i
= 0; i
< div
->n_row
; ++i
) {
9688 if (j
< n_div
&& exp
[j
] == i
) {
9691 isl_seq_cpy(bmap
->div
[i
], div
->row
[i
], div
->n_col
);
9692 if (isl_basic_map_div_is_marked_unknown(bmap
, i
))
9694 bmap
= isl_basic_map_add_div_constraints(bmap
, i
);
9703 isl_basic_map_free(bmap
);
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 __isl_give isl_basic_set
*isl_basic_set_expand_divs(
9713 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*div
, int *exp
)
9715 return isl_basic_map_expand_divs(bset
, div
, exp
);
9718 /* Look for a div in dst that corresponds to the div "div" in src.
9719 * The divs before "div" in src and dst are assumed to be the same.
9721 * Return the position of the corresponding div in dst
9722 * if there is one. Otherwise, return a position beyond the integer divisions.
9723 * Return -1 on error.
9725 static int find_div(__isl_keep isl_basic_map
*dst
,
9726 __isl_keep isl_basic_map
*src
, unsigned div
)
9732 v_div
= isl_basic_map_var_offset(src
, isl_dim_div
);
9733 n_div
= isl_basic_map_dim(dst
, isl_dim_div
);
9734 if (n_div
< 0 || v_div
< 0)
9736 isl_assert(dst
->ctx
, div
<= n_div
, return -1);
9737 for (i
= div
; i
< n_div
; ++i
)
9738 if (isl_seq_eq(dst
->div
[i
], src
->div
[div
], 1+1+v_div
+div
) &&
9739 isl_seq_first_non_zero(dst
->div
[i
] + 1 + 1 + v_div
+ div
,
9745 /* Align the divs of "dst" to those of "src", adding divs from "src"
9746 * if needed. That is, make sure that the first src->n_div divs
9747 * of the result are equal to those of src.
9748 * The integer division of "src" are assumed to be ordered.
9750 * The integer divisions are swapped into the right position
9751 * (possibly after adding them first). This may result
9752 * in the remaining integer divisions appearing in the wrong order,
9753 * i.e., with some integer division appearing before
9754 * some other integer division on which it depends.
9755 * The integer divisions therefore need to be ordered.
9756 * This will not affect the integer divisions aligned to those of "src",
9757 * since "src" is assumed to have ordered integer divisions.
9759 * The result is not finalized as by design it will have redundant
9760 * divs if any divs from "src" were copied.
9762 __isl_give isl_basic_map
*isl_basic_map_align_divs(
9763 __isl_take isl_basic_map
*dst
, __isl_keep isl_basic_map
*src
)
9772 return isl_basic_map_free(dst
);
9774 if (src
->n_div
== 0)
9777 known
= isl_basic_map_divs_known(src
);
9779 return isl_basic_map_free(dst
);
9781 isl_die(isl_basic_map_get_ctx(src
), isl_error_invalid
,
9782 "some src divs are unknown",
9783 return isl_basic_map_free(dst
));
9785 v_div
= isl_basic_map_var_offset(src
, isl_dim_div
);
9787 return isl_basic_map_free(dst
);
9790 dst_n_div
= isl_basic_map_dim(dst
, isl_dim_div
);
9792 dst
= isl_basic_map_free(dst
);
9793 for (i
= 0; i
< src
->n_div
; ++i
) {
9794 int j
= find_div(dst
, src
, i
);
9796 dst
= isl_basic_map_free(dst
);
9797 if (j
== dst_n_div
) {
9799 int extra
= src
->n_div
- i
;
9800 dst
= isl_basic_map_cow(dst
);
9802 return isl_basic_map_free(dst
);
9803 dst
= isl_basic_map_extend(dst
,
9804 extra
, 0, 2 * extra
);
9807 j
= isl_basic_map_alloc_div(dst
);
9809 return isl_basic_map_free(dst
);
9810 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+v_div
+i
);
9811 isl_seq_clr(dst
->div
[j
]+1+1+v_div
+i
, dst
->n_div
- i
);
9813 dst
= isl_basic_map_add_div_constraints(dst
, j
);
9815 return isl_basic_map_free(dst
);
9818 dst
= isl_basic_map_swap_div(dst
, i
, j
);
9820 return isl_basic_map_free(dst
);
9822 return isl_basic_map_order_divs(dst
);
9825 __isl_give isl_map
*isl_map_align_divs_internal(__isl_take isl_map
*map
)
9833 map
= isl_map_compute_divs(map
);
9834 map
= isl_map_order_divs(map
);
9835 map
= isl_map_cow(map
);
9839 for (i
= 1; i
< map
->n
; ++i
)
9840 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], map
->p
[i
]);
9841 for (i
= 1; i
< map
->n
; ++i
) {
9842 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], map
->p
[0]);
9844 return isl_map_free(map
);
9847 map
= isl_map_unmark_normalized(map
);
9851 __isl_give isl_map
*isl_map_align_divs(__isl_take isl_map
*map
)
9853 return isl_map_align_divs_internal(map
);
9856 __isl_give isl_set
*isl_set_align_divs(__isl_take isl_set
*set
)
9858 return set_from_map(isl_map_align_divs_internal(set_to_map(set
)));
9861 /* Align the divs of the basic maps in "map" to those
9862 * of the basic maps in "list", as well as to the other basic maps in "map".
9863 * The elements in "list" are assumed to have known divs.
9865 __isl_give isl_map
*isl_map_align_divs_to_basic_map_list(
9866 __isl_take isl_map
*map
, __isl_keep isl_basic_map_list
*list
)
9871 n
= isl_basic_map_list_n_basic_map(list
);
9872 map
= isl_map_compute_divs(map
);
9873 map
= isl_map_cow(map
);
9875 return isl_map_free(map
);
9879 for (i
= 0; i
< n
; ++i
) {
9880 isl_basic_map
*bmap
;
9882 bmap
= isl_basic_map_list_get_basic_map(list
, i
);
9883 bmap
= isl_basic_map_order_divs(bmap
);
9884 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], bmap
);
9885 isl_basic_map_free(bmap
);
9888 return isl_map_free(map
);
9890 return isl_map_align_divs_internal(map
);
9893 /* Align the divs of each element of "list" to those of "bmap".
9894 * Both "bmap" and the elements of "list" are assumed to have known divs.
9896 __isl_give isl_basic_map_list
*isl_basic_map_list_align_divs_to_basic_map(
9897 __isl_take isl_basic_map_list
*list
, __isl_keep isl_basic_map
*bmap
)
9902 n
= isl_basic_map_list_n_basic_map(list
);
9904 return isl_basic_map_list_free(list
);
9906 for (i
= 0; i
< n
; ++i
) {
9907 isl_basic_map
*bmap_i
;
9909 bmap_i
= isl_basic_map_list_get_basic_map(list
, i
);
9910 bmap_i
= isl_basic_map_align_divs(bmap_i
, bmap
);
9911 list
= isl_basic_map_list_set_basic_map(list
, i
, bmap_i
);
9917 __isl_give isl_set
*isl_set_apply( __isl_take isl_set
*set
,
9918 __isl_take isl_map
*map
)
9922 isl_map_align_params_set(&map
, &set
);
9923 ok
= isl_map_compatible_domain(map
, set
);
9927 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
9928 "incompatible spaces", goto error
);
9929 map
= isl_map_intersect_domain(map
, set
);
9930 set
= isl_map_range(map
);
9938 /* There is no need to cow as removing empty parts doesn't change
9939 * the meaning of the set.
9941 __isl_give isl_map
*isl_map_remove_empty_parts(__isl_take isl_map
*map
)
9948 for (i
= map
->n
- 1; i
>= 0; --i
)
9949 map
= remove_if_empty(map
, i
);
9954 __isl_give isl_set
*isl_set_remove_empty_parts(__isl_take isl_set
*set
)
9956 return set_from_map(isl_map_remove_empty_parts(set_to_map(set
)));
9959 /* Create a binary relation that maps the shared initial "pos" dimensions
9960 * of "bset1" and "bset2" to the remaining dimensions of "bset1" and "bset2".
9962 static __isl_give isl_basic_map
*join_initial(__isl_keep isl_basic_set
*bset1
,
9963 __isl_keep isl_basic_set
*bset2
, int pos
)
9965 isl_basic_map
*bmap1
;
9966 isl_basic_map
*bmap2
;
9968 bmap1
= isl_basic_map_from_range(isl_basic_set_copy(bset1
));
9969 bmap2
= isl_basic_map_from_range(isl_basic_set_copy(bset2
));
9970 bmap1
= isl_basic_map_move_dims(bmap1
, isl_dim_in
, 0,
9971 isl_dim_out
, 0, pos
);
9972 bmap2
= isl_basic_map_move_dims(bmap2
, isl_dim_in
, 0,
9973 isl_dim_out
, 0, pos
);
9974 return isl_basic_map_range_product(bmap1
, bmap2
);
9977 /* Given two basic sets bset1 and bset2, compute the maximal difference
9978 * between the values of dimension pos in bset1 and those in bset2
9979 * for any common value of the parameters and dimensions preceding pos.
9981 static enum isl_lp_result
basic_set_maximal_difference_at(
9982 __isl_keep isl_basic_set
*bset1
, __isl_keep isl_basic_set
*bset2
,
9983 int pos
, isl_int
*opt
)
9985 isl_basic_map
*bmap1
;
9986 struct isl_ctx
*ctx
;
9987 struct isl_vec
*obj
;
9991 enum isl_lp_result res
;
9993 nparam
= isl_basic_set_dim(bset1
, isl_dim_param
);
9994 dim1
= isl_basic_set_dim(bset1
, isl_dim_set
);
9995 if (nparam
< 0 || dim1
< 0 || !bset2
)
9996 return isl_lp_error
;
9998 bmap1
= join_initial(bset1
, bset2
, pos
);
9999 total
= isl_basic_map_dim(bmap1
, isl_dim_all
);
10001 return isl_lp_error
;
10004 obj
= isl_vec_alloc(ctx
, 1 + total
);
10007 isl_seq_clr(obj
->block
.data
, 1 + total
);
10008 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
], 1);
10009 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
+(dim1
-pos
)], -1);
10010 res
= isl_basic_map_solve_lp(bmap1
, 1, obj
->block
.data
, ctx
->one
,
10012 isl_basic_map_free(bmap1
);
10016 isl_basic_map_free(bmap1
);
10017 return isl_lp_error
;
10020 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
10021 * for any common value of the parameters and dimensions preceding pos
10022 * in both basic sets, the values of dimension pos in bset1 are
10023 * smaller or larger than those in bset2.
10026 * 1 if bset1 follows bset2
10027 * -1 if bset1 precedes bset2
10028 * 0 if bset1 and bset2 are incomparable
10029 * -2 if some error occurred.
10031 int isl_basic_set_compare_at(__isl_keep isl_basic_set
*bset1
,
10032 __isl_keep isl_basic_set
*bset2
, int pos
)
10035 enum isl_lp_result res
;
10040 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
10042 if (res
== isl_lp_empty
)
10044 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
10045 res
== isl_lp_unbounded
)
10047 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
10052 isl_int_clear(opt
);
10056 /* Given two basic sets bset1 and bset2, check whether
10057 * for any common value of the parameters and dimensions preceding pos
10058 * there is a value of dimension pos in bset1 that is larger
10059 * than a value of the same dimension in bset2.
10062 * 1 if there exists such a pair
10063 * 0 if there is no such pair, but there is a pair of equal values
10065 * -2 if some error occurred.
10067 int isl_basic_set_follows_at(__isl_keep isl_basic_set
*bset1
,
10068 __isl_keep isl_basic_set
*bset2
, int pos
)
10071 isl_basic_map
*bmap
;
10074 dim1
= isl_basic_set_dim(bset1
, isl_dim_set
);
10077 bmap
= join_initial(bset1
, bset2
, pos
);
10078 bmap
= isl_basic_map_order_ge(bmap
, isl_dim_out
, 0,
10079 isl_dim_out
, dim1
- pos
);
10080 empty
= isl_basic_map_is_empty(bmap
);
10084 isl_basic_map_free(bmap
);
10087 bmap
= isl_basic_map_order_gt(bmap
, isl_dim_out
, 0,
10088 isl_dim_out
, dim1
- pos
);
10089 empty
= isl_basic_map_is_empty(bmap
);
10092 isl_basic_map_free(bmap
);
10097 isl_basic_map_free(bmap
);
10101 /* Given two sets set1 and set2, check whether
10102 * for any common value of the parameters and dimensions preceding pos
10103 * there is a value of dimension pos in set1 that is larger
10104 * than a value of the same dimension in set2.
10107 * 1 if there exists such a pair
10108 * 0 if there is no such pair, but there is a pair of equal values
10110 * -2 if some error occurred.
10112 int isl_set_follows_at(__isl_keep isl_set
*set1
,
10113 __isl_keep isl_set
*set2
, int pos
)
10118 if (!set1
|| !set2
)
10121 for (i
= 0; i
< set1
->n
; ++i
)
10122 for (j
= 0; j
< set2
->n
; ++j
) {
10124 f
= isl_basic_set_follows_at(set1
->p
[i
], set2
->p
[j
], pos
);
10125 if (f
== 1 || f
== -2)
10134 static isl_bool
isl_basic_map_plain_has_fixed_var(
10135 __isl_keep isl_basic_map
*bmap
, unsigned pos
, isl_int
*val
)
10141 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
10143 return isl_bool_error
;
10144 for (i
= 0, d
= total
-1; i
< bmap
->n_eq
&& d
+1 > pos
; ++i
) {
10145 for (; d
+1 > pos
; --d
)
10146 if (!isl_int_is_zero(bmap
->eq
[i
][1+d
]))
10150 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1, d
) != -1)
10151 return isl_bool_false
;
10152 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1+d
+1, total
-d
-1) != -1)
10153 return isl_bool_false
;
10154 if (!isl_int_is_one(bmap
->eq
[i
][1+d
]))
10155 return isl_bool_false
;
10157 isl_int_neg(*val
, bmap
->eq
[i
][0]);
10158 return isl_bool_true
;
10160 return isl_bool_false
;
10163 static isl_bool
isl_map_plain_has_fixed_var(__isl_keep isl_map
*map
,
10164 unsigned pos
, isl_int
*val
)
10172 return isl_bool_error
;
10174 return isl_bool_false
;
10176 return isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, val
);
10179 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, &v
);
10180 for (i
= 1; fixed
== isl_bool_true
&& i
< map
->n
; ++i
) {
10181 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[i
], pos
, &tmp
);
10182 if (fixed
== isl_bool_true
&& isl_int_ne(tmp
, v
))
10183 fixed
= isl_bool_false
;
10186 isl_int_set(*val
, v
);
10187 isl_int_clear(tmp
);
10192 static isl_bool
isl_basic_set_plain_has_fixed_var(
10193 __isl_keep isl_basic_set
*bset
, unsigned pos
, isl_int
*val
)
10195 return isl_basic_map_plain_has_fixed_var(bset_to_bmap(bset
),
10199 isl_bool
isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map
*bmap
,
10200 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
10202 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
10203 return isl_bool_error
;
10204 return isl_basic_map_plain_has_fixed_var(bmap
,
10205 isl_basic_map_offset(bmap
, type
) - 1 + pos
, val
);
10208 /* If "bmap" obviously lies on a hyperplane where the given dimension
10209 * has a fixed value, then return that value.
10210 * Otherwise return NaN.
10212 __isl_give isl_val
*isl_basic_map_plain_get_val_if_fixed(
10213 __isl_keep isl_basic_map
*bmap
,
10214 enum isl_dim_type type
, unsigned pos
)
10222 ctx
= isl_basic_map_get_ctx(bmap
);
10223 v
= isl_val_alloc(ctx
);
10226 fixed
= isl_basic_map_plain_is_fixed(bmap
, type
, pos
, &v
->n
);
10228 return isl_val_free(v
);
10230 isl_int_set_si(v
->d
, 1);
10234 return isl_val_nan(ctx
);
10237 isl_bool
isl_map_plain_is_fixed(__isl_keep isl_map
*map
,
10238 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
10240 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
10241 return isl_bool_error
;
10242 return isl_map_plain_has_fixed_var(map
,
10243 map_offset(map
, type
) - 1 + pos
, val
);
10246 /* If "map" obviously lies on a hyperplane where the given dimension
10247 * has a fixed value, then return that value.
10248 * Otherwise return NaN.
10250 __isl_give isl_val
*isl_map_plain_get_val_if_fixed(__isl_keep isl_map
*map
,
10251 enum isl_dim_type type
, unsigned pos
)
10259 ctx
= isl_map_get_ctx(map
);
10260 v
= isl_val_alloc(ctx
);
10263 fixed
= isl_map_plain_is_fixed(map
, type
, pos
, &v
->n
);
10265 return isl_val_free(v
);
10267 isl_int_set_si(v
->d
, 1);
10271 return isl_val_nan(ctx
);
10274 /* If "set" obviously lies on a hyperplane where the given dimension
10275 * has a fixed value, then return that value.
10276 * Otherwise return NaN.
10278 __isl_give isl_val
*isl_set_plain_get_val_if_fixed(__isl_keep isl_set
*set
,
10279 enum isl_dim_type type
, unsigned pos
)
10281 return isl_map_plain_get_val_if_fixed(set
, type
, pos
);
10284 /* Return a sequence of values in the same space as "set"
10285 * that are equal to the corresponding set dimensions of "set"
10286 * for those set dimensions that obviously lie on a hyperplane
10287 * where the dimension has a fixed value.
10288 * The other elements are set to NaN.
10290 __isl_give isl_multi_val
*isl_set_get_plain_multi_val_if_fixed(
10291 __isl_keep isl_set
*set
)
10298 space
= isl_space_drop_all_params(isl_set_get_space(set
));
10299 mv
= isl_multi_val_alloc(space
);
10300 n
= isl_multi_val_size(mv
);
10302 return isl_multi_val_free(mv
);
10304 for (i
= 0; i
< n
; ++i
) {
10307 v
= isl_set_plain_get_val_if_fixed(set
, isl_dim_set
, i
);
10308 mv
= isl_multi_val_set_val(mv
, i
, v
);
10314 /* Check if dimension dim has fixed value and if so and if val is not NULL,
10315 * then return this fixed value in *val.
10317 isl_bool
isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set
*bset
,
10318 unsigned dim
, isl_int
*val
)
10322 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
10324 return isl_bool_error
;
10325 return isl_basic_set_plain_has_fixed_var(bset
, nparam
+ dim
, val
);
10328 /* Return -1 if the constraint "c1" should be sorted before "c2"
10329 * and 1 if it should be sorted after "c2".
10330 * Return 0 if the two constraints are the same (up to the constant term).
10332 * In particular, if a constraint involves later variables than another
10333 * then it is sorted after this other constraint.
10334 * uset_gist depends on constraints without existentially quantified
10335 * variables sorting first.
10337 * For constraints that have the same latest variable, those
10338 * with the same coefficient for this latest variable (first in absolute value
10339 * and then in actual value) are grouped together.
10340 * This is useful for detecting pairs of constraints that can
10341 * be chained in their printed representation.
10343 * Finally, within a group, constraints are sorted according to
10344 * their coefficients (excluding the constant term).
10346 static int sort_constraint_cmp(const void *p1
, const void *p2
, void *arg
)
10348 isl_int
**c1
= (isl_int
**) p1
;
10349 isl_int
**c2
= (isl_int
**) p2
;
10351 unsigned size
= *(unsigned *) arg
;
10354 l1
= isl_seq_last_non_zero(*c1
+ 1, size
);
10355 l2
= isl_seq_last_non_zero(*c2
+ 1, size
);
10360 cmp
= isl_int_abs_cmp((*c1
)[1 + l1
], (*c2
)[1 + l1
]);
10363 cmp
= isl_int_cmp((*c1
)[1 + l1
], (*c2
)[1 + l1
]);
10367 return isl_seq_cmp(*c1
+ 1, *c2
+ 1, size
);
10370 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
10371 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
10372 * and 0 if the two constraints are the same (up to the constant term).
10374 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map
*bmap
,
10375 isl_int
*c1
, isl_int
*c2
)
10380 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
10384 return sort_constraint_cmp(&c1
, &c2
, &size
);
10387 __isl_give isl_basic_map
*isl_basic_map_sort_constraints(
10388 __isl_take isl_basic_map
*bmap
)
10395 if (bmap
->n_ineq
== 0)
10397 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_SORTED
))
10399 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
10401 return isl_basic_map_free(bmap
);
10403 if (isl_sort(bmap
->ineq
, bmap
->n_ineq
, sizeof(isl_int
*),
10404 &sort_constraint_cmp
, &size
) < 0)
10405 return isl_basic_map_free(bmap
);
10406 ISL_F_SET(bmap
, ISL_BASIC_MAP_SORTED
);
10410 __isl_give isl_basic_set
*isl_basic_set_sort_constraints(
10411 __isl_take isl_basic_set
*bset
)
10413 isl_basic_map
*bmap
= bset_to_bmap(bset
);
10414 return bset_from_bmap(isl_basic_map_sort_constraints(bmap
));
10417 __isl_give isl_basic_map
*isl_basic_map_normalize(
10418 __isl_take isl_basic_map
*bmap
)
10420 bmap
= isl_basic_map_remove_redundancies(bmap
);
10421 bmap
= isl_basic_map_sort_constraints(bmap
);
10424 int isl_basic_map_plain_cmp(__isl_keep isl_basic_map
*bmap1
,
10425 __isl_keep isl_basic_map
*bmap2
)
10429 isl_space
*space1
, *space2
;
10431 if (!bmap1
|| !bmap2
)
10434 if (bmap1
== bmap2
)
10436 space1
= isl_basic_map_peek_space(bmap1
);
10437 space2
= isl_basic_map_peek_space(bmap2
);
10438 cmp
= isl_space_cmp(space1
, space2
);
10441 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) !=
10442 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_RATIONAL
))
10443 return ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) ? -1 : 1;
10444 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
) &&
10445 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
10447 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
))
10449 if (ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
10451 if (bmap1
->n_eq
!= bmap2
->n_eq
)
10452 return bmap1
->n_eq
- bmap2
->n_eq
;
10453 if (bmap1
->n_ineq
!= bmap2
->n_ineq
)
10454 return bmap1
->n_ineq
- bmap2
->n_ineq
;
10455 if (bmap1
->n_div
!= bmap2
->n_div
)
10456 return bmap1
->n_div
- bmap2
->n_div
;
10457 total
= isl_basic_map_dim(bmap1
, isl_dim_all
);
10460 for (i
= 0; i
< bmap1
->n_eq
; ++i
) {
10461 cmp
= isl_seq_cmp(bmap1
->eq
[i
], bmap2
->eq
[i
], 1+total
);
10465 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
10466 cmp
= isl_seq_cmp(bmap1
->ineq
[i
], bmap2
->ineq
[i
], 1+total
);
10470 for (i
= 0; i
< bmap1
->n_div
; ++i
) {
10471 isl_bool unknown1
, unknown2
;
10473 unknown1
= isl_basic_map_div_is_marked_unknown(bmap1
, i
);
10474 unknown2
= isl_basic_map_div_is_marked_unknown(bmap2
, i
);
10475 if (unknown1
< 0 || unknown2
< 0)
10477 if (unknown1
&& unknown2
)
10483 cmp
= isl_seq_cmp(bmap1
->div
[i
], bmap2
->div
[i
], 1+1+total
);
10490 int isl_basic_set_plain_cmp(__isl_keep isl_basic_set
*bset1
,
10491 __isl_keep isl_basic_set
*bset2
)
10493 return isl_basic_map_plain_cmp(bset1
, bset2
);
10496 int isl_set_plain_cmp(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
10502 if (set1
->n
!= set2
->n
)
10503 return set1
->n
- set2
->n
;
10505 for (i
= 0; i
< set1
->n
; ++i
) {
10506 cmp
= isl_basic_set_plain_cmp(set1
->p
[i
], set2
->p
[i
]);
10514 isl_bool
isl_basic_map_plain_is_equal(__isl_keep isl_basic_map
*bmap1
,
10515 __isl_keep isl_basic_map
*bmap2
)
10517 if (!bmap1
|| !bmap2
)
10518 return isl_bool_error
;
10519 return isl_basic_map_plain_cmp(bmap1
, bmap2
) == 0;
10522 isl_bool
isl_basic_set_plain_is_equal(__isl_keep isl_basic_set
*bset1
,
10523 __isl_keep isl_basic_set
*bset2
)
10525 return isl_basic_map_plain_is_equal(bset_to_bmap(bset1
),
10526 bset_to_bmap(bset2
));
10529 static int qsort_bmap_cmp(const void *p1
, const void *p2
)
10531 isl_basic_map
*bmap1
= *(isl_basic_map
**) p1
;
10532 isl_basic_map
*bmap2
= *(isl_basic_map
**) p2
;
10534 return isl_basic_map_plain_cmp(bmap1
, bmap2
);
10537 /* Sort the basic maps of "map" and remove duplicate basic maps.
10539 * While removing basic maps, we make sure that the basic maps remain
10540 * sorted because isl_map_normalize expects the basic maps of the result
10543 static __isl_give isl_map
*sort_and_remove_duplicates(__isl_take isl_map
*map
)
10547 map
= isl_map_remove_empty_parts(map
);
10550 qsort(map
->p
, map
->n
, sizeof(struct isl_basic_map
*), qsort_bmap_cmp
);
10551 for (i
= map
->n
- 1; i
>= 1; --i
) {
10552 if (!isl_basic_map_plain_is_equal(map
->p
[i
- 1], map
->p
[i
]))
10554 isl_basic_map_free(map
->p
[i
-1]);
10555 for (j
= i
; j
< map
->n
; ++j
)
10556 map
->p
[j
- 1] = map
->p
[j
];
10563 /* Remove obvious duplicates among the basic maps of "map".
10565 * Unlike isl_map_normalize, this function does not remove redundant
10566 * constraints and only removes duplicates that have exactly the same
10567 * constraints in the input. It does sort the constraints and
10568 * the basic maps to ease the detection of duplicates.
10570 * If "map" has already been normalized or if the basic maps are
10571 * disjoint, then there can be no duplicates.
10573 __isl_give isl_map
*isl_map_remove_obvious_duplicates(__isl_take isl_map
*map
)
10576 isl_basic_map
*bmap
;
10582 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
| ISL_MAP_DISJOINT
))
10584 for (i
= 0; i
< map
->n
; ++i
) {
10585 bmap
= isl_basic_map_copy(map
->p
[i
]);
10586 bmap
= isl_basic_map_sort_constraints(bmap
);
10588 return isl_map_free(map
);
10589 isl_basic_map_free(map
->p
[i
]);
10593 map
= sort_and_remove_duplicates(map
);
10597 /* We normalize in place, but if anything goes wrong we need
10598 * to return NULL, so we need to make sure we don't change the
10599 * meaning of any possible other copies of map.
10601 __isl_give isl_map
*isl_map_normalize(__isl_take isl_map
*map
)
10604 struct isl_basic_map
*bmap
;
10608 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
))
10610 for (i
= 0; i
< map
->n
; ++i
) {
10611 bmap
= isl_basic_map_normalize(isl_basic_map_copy(map
->p
[i
]));
10614 isl_basic_map_free(map
->p
[i
]);
10618 map
= sort_and_remove_duplicates(map
);
10620 ISL_F_SET(map
, ISL_MAP_NORMALIZED
);
10627 __isl_give isl_set
*isl_set_normalize(__isl_take isl_set
*set
)
10629 return set_from_map(isl_map_normalize(set_to_map(set
)));
10632 isl_bool
isl_map_plain_is_equal(__isl_keep isl_map
*map1
,
10633 __isl_keep isl_map
*map2
)
10638 if (!map1
|| !map2
)
10639 return isl_bool_error
;
10642 return isl_bool_true
;
10643 equal
= isl_map_has_equal_space(map1
, map2
);
10644 if (equal
< 0 || !equal
)
10647 map1
= isl_map_copy(map1
);
10648 map2
= isl_map_copy(map2
);
10649 map1
= isl_map_normalize(map1
);
10650 map2
= isl_map_normalize(map2
);
10651 if (!map1
|| !map2
)
10653 equal
= map1
->n
== map2
->n
;
10654 for (i
= 0; equal
&& i
< map1
->n
; ++i
) {
10655 equal
= isl_basic_map_plain_is_equal(map1
->p
[i
], map2
->p
[i
]);
10659 isl_map_free(map1
);
10660 isl_map_free(map2
);
10663 isl_map_free(map1
);
10664 isl_map_free(map2
);
10665 return isl_bool_error
;
10668 isl_bool
isl_set_plain_is_equal(__isl_keep isl_set
*set1
,
10669 __isl_keep isl_set
*set2
)
10671 return isl_map_plain_is_equal(set_to_map(set1
), set_to_map(set2
));
10674 /* Return the basic maps in "map" as a list.
10676 __isl_give isl_basic_map_list
*isl_map_get_basic_map_list(
10677 __isl_keep isl_map
*map
)
10681 isl_basic_map_list
*list
;
10685 ctx
= isl_map_get_ctx(map
);
10686 list
= isl_basic_map_list_alloc(ctx
, map
->n
);
10688 for (i
= 0; i
< map
->n
; ++i
) {
10689 isl_basic_map
*bmap
;
10691 bmap
= isl_basic_map_copy(map
->p
[i
]);
10692 list
= isl_basic_map_list_add(list
, bmap
);
10698 /* Return the intersection of the elements in the non-empty list "list".
10699 * All elements are assumed to live in the same space.
10701 __isl_give isl_basic_map
*isl_basic_map_list_intersect(
10702 __isl_take isl_basic_map_list
*list
)
10706 isl_basic_map
*bmap
;
10708 n
= isl_basic_map_list_n_basic_map(list
);
10712 isl_die(isl_basic_map_list_get_ctx(list
), isl_error_invalid
,
10713 "expecting non-empty list", goto error
);
10715 bmap
= isl_basic_map_list_get_basic_map(list
, 0);
10716 for (i
= 1; i
< n
; ++i
) {
10717 isl_basic_map
*bmap_i
;
10719 bmap_i
= isl_basic_map_list_get_basic_map(list
, i
);
10720 bmap
= isl_basic_map_intersect(bmap
, bmap_i
);
10723 isl_basic_map_list_free(list
);
10726 isl_basic_map_list_free(list
);
10730 /* Return the intersection of the elements in the non-empty list "list".
10731 * All elements are assumed to live in the same space.
10733 __isl_give isl_basic_set
*isl_basic_set_list_intersect(
10734 __isl_take isl_basic_set_list
*list
)
10736 return isl_basic_map_list_intersect(list
);
10739 /* Return the union of the elements of "list".
10740 * The list is required to have at least one element.
10742 __isl_give isl_set
*isl_basic_set_list_union(
10743 __isl_take isl_basic_set_list
*list
)
10748 isl_basic_set
*bset
;
10751 n
= isl_basic_set_list_n_basic_set(list
);
10755 isl_die(isl_basic_set_list_get_ctx(list
), isl_error_invalid
,
10756 "expecting non-empty list", goto error
);
10758 bset
= isl_basic_set_list_get_basic_set(list
, 0);
10759 space
= isl_basic_set_get_space(bset
);
10760 isl_basic_set_free(bset
);
10762 set
= isl_set_alloc_space(space
, n
, 0);
10763 for (i
= 0; i
< n
; ++i
) {
10764 bset
= isl_basic_set_list_get_basic_set(list
, i
);
10765 set
= isl_set_add_basic_set(set
, bset
);
10768 isl_basic_set_list_free(list
);
10771 isl_basic_set_list_free(list
);
10775 /* Return the union of the elements in the non-empty list "list".
10776 * All elements are assumed to live in the same space.
10778 __isl_give isl_set
*isl_set_list_union(__isl_take isl_set_list
*list
)
10784 n
= isl_set_list_n_set(list
);
10788 isl_die(isl_set_list_get_ctx(list
), isl_error_invalid
,
10789 "expecting non-empty list", goto error
);
10791 set
= isl_set_list_get_set(list
, 0);
10792 for (i
= 1; i
< n
; ++i
) {
10795 set_i
= isl_set_list_get_set(list
, i
);
10796 set
= isl_set_union(set
, set_i
);
10799 isl_set_list_free(list
);
10802 isl_set_list_free(list
);
10806 __isl_give isl_basic_map
*isl_basic_map_product(
10807 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
10809 isl_space
*space_result
= NULL
;
10810 struct isl_basic_map
*bmap
;
10811 unsigned in1
, in2
, out1
, out2
, nparam
, total
, pos
;
10812 struct isl_dim_map
*dim_map1
, *dim_map2
;
10814 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
10816 space_result
= isl_space_product(isl_space_copy(bmap1
->dim
),
10817 isl_space_copy(bmap2
->dim
));
10819 in1
= isl_basic_map_dim(bmap1
, isl_dim_in
);
10820 in2
= isl_basic_map_dim(bmap2
, isl_dim_in
);
10821 out1
= isl_basic_map_dim(bmap1
, isl_dim_out
);
10822 out2
= isl_basic_map_dim(bmap2
, isl_dim_out
);
10823 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
10825 total
= nparam
+ in1
+ in2
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
10826 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
10827 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
10828 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
10829 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
10830 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
10831 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
10832 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
10833 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
10834 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
10835 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
10837 bmap
= isl_basic_map_alloc_space(space_result
,
10838 bmap1
->n_div
+ bmap2
->n_div
,
10839 bmap1
->n_eq
+ bmap2
->n_eq
,
10840 bmap1
->n_ineq
+ bmap2
->n_ineq
);
10841 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
10842 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
10843 bmap
= isl_basic_map_simplify(bmap
);
10844 return isl_basic_map_finalize(bmap
);
10846 isl_basic_map_free(bmap1
);
10847 isl_basic_map_free(bmap2
);
10851 __isl_give isl_basic_map
*isl_basic_map_flat_product(
10852 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
10854 isl_basic_map
*prod
;
10856 prod
= isl_basic_map_product(bmap1
, bmap2
);
10857 prod
= isl_basic_map_flatten(prod
);
10861 __isl_give isl_basic_set
*isl_basic_set_flat_product(
10862 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
10864 return isl_basic_map_flat_range_product(bset1
, bset2
);
10867 __isl_give isl_basic_map
*isl_basic_map_domain_product(
10868 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
10870 isl_space
*space1
, *space2
;
10871 isl_space
*space_result
= NULL
;
10872 isl_basic_map
*bmap
;
10873 isl_size in1
, in2
, out
, nparam
;
10874 unsigned total
, pos
;
10875 struct isl_dim_map
*dim_map1
, *dim_map2
;
10877 in1
= isl_basic_map_dim(bmap1
, isl_dim_in
);
10878 in2
= isl_basic_map_dim(bmap2
, isl_dim_in
);
10879 out
= isl_basic_map_dim(bmap1
, isl_dim_out
);
10880 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
10881 if (in1
< 0 || in2
< 0 || out
< 0 || nparam
< 0)
10884 space1
= isl_basic_map_get_space(bmap1
);
10885 space2
= isl_basic_map_get_space(bmap2
);
10886 space_result
= isl_space_domain_product(space1
, space2
);
10888 total
= nparam
+ in1
+ in2
+ out
+ bmap1
->n_div
+ bmap2
->n_div
;
10889 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
10890 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
10891 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
10892 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
10893 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
10894 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
10895 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
10896 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
);
10897 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out
);
10898 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
10900 bmap
= isl_basic_map_alloc_space(space_result
,
10901 bmap1
->n_div
+ bmap2
->n_div
,
10902 bmap1
->n_eq
+ bmap2
->n_eq
,
10903 bmap1
->n_ineq
+ bmap2
->n_ineq
);
10904 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
10905 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
10906 bmap
= isl_basic_map_simplify(bmap
);
10907 return isl_basic_map_finalize(bmap
);
10909 isl_basic_map_free(bmap1
);
10910 isl_basic_map_free(bmap2
);
10914 __isl_give isl_basic_map
*isl_basic_map_range_product(
10915 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
10918 isl_space
*space_result
= NULL
;
10919 isl_basic_map
*bmap
;
10920 isl_size in
, out1
, out2
, nparam
;
10921 unsigned total
, pos
;
10922 struct isl_dim_map
*dim_map1
, *dim_map2
;
10924 rational
= isl_basic_map_is_rational(bmap1
);
10925 if (rational
>= 0 && rational
)
10926 rational
= isl_basic_map_is_rational(bmap2
);
10927 in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
10928 out1
= isl_basic_map_dim(bmap1
, isl_dim_out
);
10929 out2
= isl_basic_map_dim(bmap2
, isl_dim_out
);
10930 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
10931 if (in
< 0 || out1
< 0 || out2
< 0 || nparam
< 0 || rational
< 0)
10934 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
10937 space_result
= isl_space_range_product(isl_space_copy(bmap1
->dim
),
10938 isl_space_copy(bmap2
->dim
));
10940 total
= nparam
+ in
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
10941 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
10942 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
10943 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
10944 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
10945 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
10946 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
10947 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in
);
10948 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
10949 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
10950 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
10952 bmap
= isl_basic_map_alloc_space(space_result
,
10953 bmap1
->n_div
+ bmap2
->n_div
,
10954 bmap1
->n_eq
+ bmap2
->n_eq
,
10955 bmap1
->n_ineq
+ bmap2
->n_ineq
);
10956 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
10957 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
10959 bmap
= isl_basic_map_set_rational(bmap
);
10960 bmap
= isl_basic_map_simplify(bmap
);
10961 return isl_basic_map_finalize(bmap
);
10963 isl_basic_map_free(bmap1
);
10964 isl_basic_map_free(bmap2
);
10968 __isl_give isl_basic_map
*isl_basic_map_flat_range_product(
10969 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
10971 isl_basic_map
*prod
;
10973 prod
= isl_basic_map_range_product(bmap1
, bmap2
);
10974 prod
= isl_basic_map_flatten_range(prod
);
10978 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
10979 * and collect the results.
10980 * The result live in the space obtained by calling "space_product"
10981 * on the spaces of "map1" and "map2".
10982 * If "remove_duplicates" is set then the result may contain duplicates
10983 * (even if the inputs do not) and so we try and remove the obvious
10986 static __isl_give isl_map
*map_product(__isl_take isl_map
*map1
,
10987 __isl_take isl_map
*map2
,
10988 __isl_give isl_space
*(*space_product
)(__isl_take isl_space
*left
,
10989 __isl_take isl_space
*right
),
10990 __isl_give isl_basic_map
*(*basic_map_product
)(
10991 __isl_take isl_basic_map
*left
,
10992 __isl_take isl_basic_map
*right
),
10993 int remove_duplicates
)
10995 unsigned flags
= 0;
10996 struct isl_map
*result
;
11000 m
= isl_map_has_equal_params(map1
, map2
);
11004 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
11005 "parameters don't match", goto error
);
11007 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
11008 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
11009 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
11011 result
= isl_map_alloc_space(space_product(isl_space_copy(map1
->dim
),
11012 isl_space_copy(map2
->dim
)),
11013 map1
->n
* map2
->n
, flags
);
11016 for (i
= 0; i
< map1
->n
; ++i
)
11017 for (j
= 0; j
< map2
->n
; ++j
) {
11018 struct isl_basic_map
*part
;
11019 part
= basic_map_product(isl_basic_map_copy(map1
->p
[i
]),
11020 isl_basic_map_copy(map2
->p
[j
]));
11021 if (isl_basic_map_is_empty(part
))
11022 isl_basic_map_free(part
);
11024 result
= isl_map_add_basic_map(result
, part
);
11028 if (remove_duplicates
)
11029 result
= isl_map_remove_obvious_duplicates(result
);
11030 isl_map_free(map1
);
11031 isl_map_free(map2
);
11034 isl_map_free(map1
);
11035 isl_map_free(map2
);
11039 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
11041 __isl_give isl_map
*isl_map_product(__isl_take isl_map
*map1
,
11042 __isl_take isl_map
*map2
)
11044 isl_map_align_params_bin(&map1
, &map2
);
11045 return map_product(map1
, map2
, &isl_space_product
,
11046 &isl_basic_map_product
, 0);
11049 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
11051 __isl_give isl_map
*isl_map_flat_product(__isl_take isl_map
*map1
,
11052 __isl_take isl_map
*map2
)
11056 prod
= isl_map_product(map1
, map2
);
11057 prod
= isl_map_flatten(prod
);
11061 /* Given two set A and B, construct its Cartesian product A x B.
11063 __isl_give isl_set
*isl_set_product(__isl_take isl_set
*set1
,
11064 __isl_take isl_set
*set2
)
11066 return isl_map_range_product(set1
, set2
);
11069 __isl_give isl_set
*isl_set_flat_product(__isl_take isl_set
*set1
,
11070 __isl_take isl_set
*set2
)
11072 return isl_map_flat_range_product(set1
, set2
);
11075 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
11077 __isl_give isl_map
*isl_map_domain_product(__isl_take isl_map
*map1
,
11078 __isl_take isl_map
*map2
)
11080 isl_map_align_params_bin(&map1
, &map2
);
11081 return map_product(map1
, map2
, &isl_space_domain_product
,
11082 &isl_basic_map_domain_product
, 1);
11085 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
11087 __isl_give isl_map
*isl_map_range_product(__isl_take isl_map
*map1
,
11088 __isl_take isl_map
*map2
)
11090 isl_map_align_params_bin(&map1
, &map2
);
11091 return map_product(map1
, map2
, &isl_space_range_product
,
11092 &isl_basic_map_range_product
, 1);
11095 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
11097 __isl_give isl_map
*isl_map_factor_domain(__isl_take isl_map
*map
)
11100 isl_size total1
, keep1
, total2
, keep2
;
11102 total1
= isl_map_dim(map
, isl_dim_in
);
11103 total2
= isl_map_dim(map
, isl_dim_out
);
11104 if (total1
< 0 || total2
< 0)
11105 return isl_map_free(map
);
11106 if (!isl_space_domain_is_wrapping(map
->dim
) ||
11107 !isl_space_range_is_wrapping(map
->dim
))
11108 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
11109 "not a product", return isl_map_free(map
));
11111 space
= isl_map_get_space(map
);
11112 space
= isl_space_factor_domain(space
);
11113 keep1
= isl_space_dim(space
, isl_dim_in
);
11114 keep2
= isl_space_dim(space
, isl_dim_out
);
11115 if (keep1
< 0 || keep2
< 0)
11116 map
= isl_map_free(map
);
11117 map
= isl_map_project_out(map
, isl_dim_in
, keep1
, total1
- keep1
);
11118 map
= isl_map_project_out(map
, isl_dim_out
, keep2
, total2
- keep2
);
11119 map
= isl_map_reset_space(map
, space
);
11124 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
11126 __isl_give isl_map
*isl_map_factor_range(__isl_take isl_map
*map
)
11129 isl_size total1
, keep1
, total2
, keep2
;
11131 total1
= isl_map_dim(map
, isl_dim_in
);
11132 total2
= isl_map_dim(map
, isl_dim_out
);
11133 if (total1
< 0 || total2
< 0)
11134 return isl_map_free(map
);
11135 if (!isl_space_domain_is_wrapping(map
->dim
) ||
11136 !isl_space_range_is_wrapping(map
->dim
))
11137 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
11138 "not a product", return isl_map_free(map
));
11140 space
= isl_map_get_space(map
);
11141 space
= isl_space_factor_range(space
);
11142 keep1
= isl_space_dim(space
, isl_dim_in
);
11143 keep2
= isl_space_dim(space
, isl_dim_out
);
11144 if (keep1
< 0 || keep2
< 0)
11145 map
= isl_map_free(map
);
11146 map
= isl_map_project_out(map
, isl_dim_in
, 0, total1
- keep1
);
11147 map
= isl_map_project_out(map
, isl_dim_out
, 0, total2
- keep2
);
11148 map
= isl_map_reset_space(map
, space
);
11153 /* Given a map of the form [A -> B] -> C, return the map A -> C.
11155 __isl_give isl_map
*isl_map_domain_factor_domain(__isl_take isl_map
*map
)
11158 isl_size total
, keep
;
11160 total
= isl_map_dim(map
, isl_dim_in
);
11162 return isl_map_free(map
);
11163 if (!isl_space_domain_is_wrapping(map
->dim
))
11164 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
11165 "domain is not a product", return isl_map_free(map
));
11167 space
= isl_map_get_space(map
);
11168 space
= isl_space_domain_factor_domain(space
);
11169 keep
= isl_space_dim(space
, isl_dim_in
);
11171 map
= isl_map_free(map
);
11172 map
= isl_map_project_out(map
, isl_dim_in
, keep
, total
- keep
);
11173 map
= isl_map_reset_space(map
, space
);
11178 /* Given a map of the form [A -> B] -> C, return the map B -> C.
11180 __isl_give isl_map
*isl_map_domain_factor_range(__isl_take isl_map
*map
)
11183 isl_size total
, keep
;
11185 total
= isl_map_dim(map
, isl_dim_in
);
11187 return isl_map_free(map
);
11188 if (!isl_space_domain_is_wrapping(map
->dim
))
11189 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
11190 "domain is not a product", return isl_map_free(map
));
11192 space
= isl_map_get_space(map
);
11193 space
= isl_space_domain_factor_range(space
);
11194 keep
= isl_space_dim(space
, isl_dim_in
);
11196 map
= isl_map_free(map
);
11197 map
= isl_map_project_out(map
, isl_dim_in
, 0, total
- keep
);
11198 map
= isl_map_reset_space(map
, space
);
11203 /* Given a map A -> [B -> C], extract the map A -> B.
11205 __isl_give isl_map
*isl_map_range_factor_domain(__isl_take isl_map
*map
)
11208 isl_size total
, keep
;
11210 total
= isl_map_dim(map
, isl_dim_out
);
11212 return isl_map_free(map
);
11213 if (!isl_space_range_is_wrapping(map
->dim
))
11214 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
11215 "range is not a product", return isl_map_free(map
));
11217 space
= isl_map_get_space(map
);
11218 space
= isl_space_range_factor_domain(space
);
11219 keep
= isl_space_dim(space
, isl_dim_out
);
11221 map
= isl_map_free(map
);
11222 map
= isl_map_project_out(map
, isl_dim_out
, keep
, total
- keep
);
11223 map
= isl_map_reset_space(map
, space
);
11228 /* Given a map A -> [B -> C], extract the map A -> C.
11230 __isl_give isl_map
*isl_map_range_factor_range(__isl_take isl_map
*map
)
11233 isl_size total
, keep
;
11235 total
= isl_map_dim(map
, isl_dim_out
);
11237 return isl_map_free(map
);
11238 if (!isl_space_range_is_wrapping(map
->dim
))
11239 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
11240 "range is not a product", return isl_map_free(map
));
11242 space
= isl_map_get_space(map
);
11243 space
= isl_space_range_factor_range(space
);
11244 keep
= isl_space_dim(space
, isl_dim_out
);
11246 map
= isl_map_free(map
);
11247 map
= isl_map_project_out(map
, isl_dim_out
, 0, total
- keep
);
11248 map
= isl_map_reset_space(map
, space
);
11253 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
11255 __isl_give isl_map
*isl_map_flat_domain_product(__isl_take isl_map
*map1
,
11256 __isl_take isl_map
*map2
)
11260 prod
= isl_map_domain_product(map1
, map2
);
11261 prod
= isl_map_flatten_domain(prod
);
11265 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
11267 __isl_give isl_map
*isl_map_flat_range_product(__isl_take isl_map
*map1
,
11268 __isl_take isl_map
*map2
)
11272 prod
= isl_map_range_product(map1
, map2
);
11273 prod
= isl_map_flatten_range(prod
);
11277 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map
*bmap
)
11280 uint32_t hash
= isl_hash_init();
11285 bmap
= isl_basic_map_copy(bmap
);
11286 bmap
= isl_basic_map_normalize(bmap
);
11287 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
11290 isl_hash_byte(hash
, bmap
->n_eq
& 0xFF);
11291 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
11293 c_hash
= isl_seq_get_hash(bmap
->eq
[i
], 1 + total
);
11294 isl_hash_hash(hash
, c_hash
);
11296 isl_hash_byte(hash
, bmap
->n_ineq
& 0xFF);
11297 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
11299 c_hash
= isl_seq_get_hash(bmap
->ineq
[i
], 1 + total
);
11300 isl_hash_hash(hash
, c_hash
);
11302 isl_hash_byte(hash
, bmap
->n_div
& 0xFF);
11303 for (i
= 0; i
< bmap
->n_div
; ++i
) {
11305 if (isl_int_is_zero(bmap
->div
[i
][0]))
11307 isl_hash_byte(hash
, i
& 0xFF);
11308 c_hash
= isl_seq_get_hash(bmap
->div
[i
], 1 + 1 + total
);
11309 isl_hash_hash(hash
, c_hash
);
11311 isl_basic_map_free(bmap
);
11315 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set
*bset
)
11317 return isl_basic_map_get_hash(bset_to_bmap(bset
));
11320 uint32_t isl_map_get_hash(__isl_keep isl_map
*map
)
11327 map
= isl_map_copy(map
);
11328 map
= isl_map_normalize(map
);
11332 hash
= isl_hash_init();
11333 for (i
= 0; i
< map
->n
; ++i
) {
11334 uint32_t bmap_hash
;
11335 bmap_hash
= isl_basic_map_get_hash(map
->p
[i
]);
11336 isl_hash_hash(hash
, bmap_hash
);
11344 uint32_t isl_set_get_hash(__isl_keep isl_set
*set
)
11346 return isl_map_get_hash(set_to_map(set
));
11349 /* Return the number of basic maps in the (current) representation of "map".
11351 isl_size
isl_map_n_basic_map(__isl_keep isl_map
*map
)
11353 return map
? map
->n
: isl_size_error
;
11356 isl_size
isl_set_n_basic_set(__isl_keep isl_set
*set
)
11358 return set
? set
->n
: isl_size_error
;
11361 isl_stat
isl_map_foreach_basic_map(__isl_keep isl_map
*map
,
11362 isl_stat (*fn
)(__isl_take isl_basic_map
*bmap
, void *user
), void *user
)
11367 return isl_stat_error
;
11369 for (i
= 0; i
< map
->n
; ++i
)
11370 if (fn(isl_basic_map_copy(map
->p
[i
]), user
) < 0)
11371 return isl_stat_error
;
11373 return isl_stat_ok
;
11376 isl_stat
isl_set_foreach_basic_set(__isl_keep isl_set
*set
,
11377 isl_stat (*fn
)(__isl_take isl_basic_set
*bset
, void *user
), void *user
)
11382 return isl_stat_error
;
11384 for (i
= 0; i
< set
->n
; ++i
)
11385 if (fn(isl_basic_set_copy(set
->p
[i
]), user
) < 0)
11386 return isl_stat_error
;
11388 return isl_stat_ok
;
11391 /* Does "test" succeed on every basic set in "set"?
11393 isl_bool
isl_set_every_basic_set(__isl_keep isl_set
*set
,
11394 isl_bool (*test
)(__isl_keep isl_basic_set
*bset
, void *user
),
11400 return isl_bool_error
;
11402 for (i
= 0; i
< set
->n
; ++i
) {
11405 r
= test(set
->p
[i
], user
);
11410 return isl_bool_true
;
11413 /* Return a list of basic sets, the union of which is equal to "set".
11415 __isl_give isl_basic_set_list
*isl_set_get_basic_set_list(
11416 __isl_keep isl_set
*set
)
11419 isl_basic_set_list
*list
;
11424 list
= isl_basic_set_list_alloc(isl_set_get_ctx(set
), set
->n
);
11425 for (i
= 0; i
< set
->n
; ++i
) {
11426 isl_basic_set
*bset
;
11428 bset
= isl_basic_set_copy(set
->p
[i
]);
11429 list
= isl_basic_set_list_add(list
, bset
);
11435 __isl_give isl_basic_set
*isl_basic_set_lift(__isl_take isl_basic_set
*bset
)
11442 bset
= isl_basic_set_cow(bset
);
11446 space
= isl_basic_set_get_space(bset
);
11447 space
= isl_space_lift(space
, bset
->n_div
);
11450 isl_space_free(bset
->dim
);
11452 bset
->extra
-= bset
->n_div
;
11455 bset
= isl_basic_set_finalize(bset
);
11459 isl_basic_set_free(bset
);
11463 __isl_give isl_set
*isl_set_lift(__isl_take isl_set
*set
)
11469 set
= set_from_map(isl_map_align_divs_internal(set_to_map(set
)));
11474 set
= isl_set_cow(set
);
11478 n_div
= set
->p
[0]->n_div
;
11479 space
= isl_set_get_space(set
);
11480 space
= isl_space_lift(space
, n_div
);
11483 isl_space_free(set
->dim
);
11486 for (i
= 0; i
< set
->n
; ++i
) {
11487 set
->p
[i
] = isl_basic_set_lift(set
->p
[i
]);
11498 int isl_basic_set_size(__isl_keep isl_basic_set
*bset
)
11503 dim
= isl_basic_set_dim(bset
, isl_dim_all
);
11506 size
+= bset
->n_eq
* (1 + dim
);
11507 size
+= bset
->n_ineq
* (1 + dim
);
11508 size
+= bset
->n_div
* (2 + dim
);
11513 int isl_set_size(__isl_keep isl_set
*set
)
11521 for (i
= 0; i
< set
->n
; ++i
)
11522 size
+= isl_basic_set_size(set
->p
[i
]);
11527 /* Check if there is any lower bound (if lower == 0) and/or upper
11528 * bound (if upper == 0) on the specified dim.
11530 static isl_bool
basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
11531 enum isl_dim_type type
, unsigned pos
, int lower
, int upper
)
11535 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
11536 return isl_bool_error
;
11538 pos
+= isl_basic_map_offset(bmap
, type
);
11540 for (i
= 0; i
< bmap
->n_div
; ++i
) {
11541 if (isl_int_is_zero(bmap
->div
[i
][0]))
11543 if (!isl_int_is_zero(bmap
->div
[i
][1 + pos
]))
11544 return isl_bool_true
;
11547 for (i
= 0; i
< bmap
->n_eq
; ++i
)
11548 if (!isl_int_is_zero(bmap
->eq
[i
][pos
]))
11549 return isl_bool_true
;
11551 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
11552 int sgn
= isl_int_sgn(bmap
->ineq
[i
][pos
]);
11559 return lower
&& upper
;
11562 isl_bool
isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
11563 enum isl_dim_type type
, unsigned pos
)
11565 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 0);
11568 isl_bool
isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map
*bmap
,
11569 enum isl_dim_type type
, unsigned pos
)
11571 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 1);
11574 isl_bool
isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map
*bmap
,
11575 enum isl_dim_type type
, unsigned pos
)
11577 return basic_map_dim_is_bounded(bmap
, type
, pos
, 1, 0);
11580 isl_bool
isl_map_dim_is_bounded(__isl_keep isl_map
*map
,
11581 enum isl_dim_type type
, unsigned pos
)
11586 return isl_bool_error
;
11588 for (i
= 0; i
< map
->n
; ++i
) {
11590 bounded
= isl_basic_map_dim_is_bounded(map
->p
[i
], type
, pos
);
11591 if (bounded
< 0 || !bounded
)
11595 return isl_bool_true
;
11598 /* Return true if the specified dim is involved in both an upper bound
11599 * and a lower bound.
11601 isl_bool
isl_set_dim_is_bounded(__isl_keep isl_set
*set
,
11602 enum isl_dim_type type
, unsigned pos
)
11604 return isl_map_dim_is_bounded(set_to_map(set
), type
, pos
);
11607 /* Does "map" have a bound (according to "fn") for any of its basic maps?
11609 static isl_bool
has_any_bound(__isl_keep isl_map
*map
,
11610 enum isl_dim_type type
, unsigned pos
,
11611 isl_bool (*fn
)(__isl_keep isl_basic_map
*bmap
,
11612 enum isl_dim_type type
, unsigned pos
))
11617 return isl_bool_error
;
11619 for (i
= 0; i
< map
->n
; ++i
) {
11621 bounded
= fn(map
->p
[i
], type
, pos
);
11622 if (bounded
< 0 || bounded
)
11626 return isl_bool_false
;
11629 /* Return 1 if the specified dim is involved in any lower bound.
11631 isl_bool
isl_set_dim_has_any_lower_bound(__isl_keep isl_set
*set
,
11632 enum isl_dim_type type
, unsigned pos
)
11634 return has_any_bound(set
, type
, pos
,
11635 &isl_basic_map_dim_has_lower_bound
);
11638 /* Return 1 if the specified dim is involved in any upper bound.
11640 isl_bool
isl_set_dim_has_any_upper_bound(__isl_keep isl_set
*set
,
11641 enum isl_dim_type type
, unsigned pos
)
11643 return has_any_bound(set
, type
, pos
,
11644 &isl_basic_map_dim_has_upper_bound
);
11647 /* Does "map" have a bound (according to "fn") for all of its basic maps?
11649 static isl_bool
has_bound(__isl_keep isl_map
*map
,
11650 enum isl_dim_type type
, unsigned pos
,
11651 isl_bool (*fn
)(__isl_keep isl_basic_map
*bmap
,
11652 enum isl_dim_type type
, unsigned pos
))
11657 return isl_bool_error
;
11659 for (i
= 0; i
< map
->n
; ++i
) {
11661 bounded
= fn(map
->p
[i
], type
, pos
);
11662 if (bounded
< 0 || !bounded
)
11666 return isl_bool_true
;
11669 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
11671 isl_bool
isl_set_dim_has_lower_bound(__isl_keep isl_set
*set
,
11672 enum isl_dim_type type
, unsigned pos
)
11674 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_lower_bound
);
11677 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
11679 isl_bool
isl_set_dim_has_upper_bound(__isl_keep isl_set
*set
,
11680 enum isl_dim_type type
, unsigned pos
)
11682 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_upper_bound
);
11685 /* For each of the "n" variables starting at "first", determine
11686 * the sign of the variable and put the results in the first "n"
11687 * elements of the array "signs".
11689 * 1 means that the variable is non-negative
11690 * -1 means that the variable is non-positive
11691 * 0 means the variable attains both positive and negative values.
11693 isl_stat
isl_basic_set_vars_get_sign(__isl_keep isl_basic_set
*bset
,
11694 unsigned first
, unsigned n
, int *signs
)
11696 isl_vec
*bound
= NULL
;
11697 struct isl_tab
*tab
= NULL
;
11698 struct isl_tab_undo
*snap
;
11702 total
= isl_basic_set_dim(bset
, isl_dim_all
);
11703 if (total
< 0 || !signs
)
11704 return isl_stat_error
;
11706 bound
= isl_vec_alloc(bset
->ctx
, 1 + total
);
11707 tab
= isl_tab_from_basic_set(bset
, 0);
11708 if (!bound
|| !tab
)
11711 isl_seq_clr(bound
->el
, bound
->size
);
11712 isl_int_set_si(bound
->el
[0], -1);
11714 snap
= isl_tab_snap(tab
);
11715 for (i
= 0; i
< n
; ++i
) {
11718 isl_int_set_si(bound
->el
[1 + first
+ i
], -1);
11719 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
11721 empty
= tab
->empty
;
11722 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
11723 if (isl_tab_rollback(tab
, snap
) < 0)
11731 isl_int_set_si(bound
->el
[1 + first
+ i
], 1);
11732 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
11734 empty
= tab
->empty
;
11735 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
11736 if (isl_tab_rollback(tab
, snap
) < 0)
11739 signs
[i
] = empty
? -1 : 0;
11743 isl_vec_free(bound
);
11744 return isl_stat_ok
;
11747 isl_vec_free(bound
);
11748 return isl_stat_error
;
11751 isl_stat
isl_basic_set_dims_get_sign(__isl_keep isl_basic_set
*bset
,
11752 enum isl_dim_type type
, unsigned first
, unsigned n
, int *signs
)
11754 if (!bset
|| !signs
)
11755 return isl_stat_error
;
11756 if (isl_basic_set_check_range(bset
, type
, first
, n
) < 0)
11757 return isl_stat_error
;
11759 first
+= pos(bset
->dim
, type
) - 1;
11760 return isl_basic_set_vars_get_sign(bset
, first
, n
, signs
);
11763 /* Is it possible for the integer division "div" to depend (possibly
11764 * indirectly) on any output dimensions?
11766 * If the div is undefined, then we conservatively assume that it
11767 * may depend on them.
11768 * Otherwise, we check if it actually depends on them or on any integer
11769 * divisions that may depend on them.
11771 static isl_bool
div_may_involve_output(__isl_keep isl_basic_map
*bmap
, int div
)
11774 isl_size n_out
, n_div
;
11775 unsigned o_out
, o_div
;
11777 if (isl_int_is_zero(bmap
->div
[div
][0]))
11778 return isl_bool_true
;
11780 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
11782 return isl_bool_error
;
11783 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
11785 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + o_out
, n_out
) != -1)
11786 return isl_bool_true
;
11788 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
11790 return isl_bool_error
;
11791 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
11793 for (i
= 0; i
< n_div
; ++i
) {
11794 isl_bool may_involve
;
11796 if (isl_int_is_zero(bmap
->div
[div
][1 + o_div
+ i
]))
11798 may_involve
= div_may_involve_output(bmap
, i
);
11799 if (may_involve
< 0 || may_involve
)
11800 return may_involve
;
11803 return isl_bool_false
;
11806 /* Return the first integer division of "bmap" in the range
11807 * [first, first + n[ that may depend on any output dimensions and
11808 * that has a non-zero coefficient in "c" (where the first coefficient
11809 * in "c" corresponds to integer division "first").
11811 static int first_div_may_involve_output(__isl_keep isl_basic_map
*bmap
,
11812 isl_int
*c
, int first
, int n
)
11819 for (k
= first
; k
< first
+ n
; ++k
) {
11820 isl_bool may_involve
;
11822 if (isl_int_is_zero(c
[k
]))
11824 may_involve
= div_may_involve_output(bmap
, k
);
11825 if (may_involve
< 0)
11834 /* Look for a pair of inequality constraints in "bmap" of the form
11836 * -l + i >= 0 or i >= l
11838 * n + l - i >= 0 or i <= l + n
11840 * with n < "m" and i the output dimension at position "pos".
11841 * (Note that n >= 0 as otherwise the two constraints would conflict.)
11842 * Furthermore, "l" is only allowed to involve parameters, input dimensions
11843 * and earlier output dimensions, as well as integer divisions that do
11844 * not involve any of the output dimensions.
11846 * Return the index of the first inequality constraint or bmap->n_ineq
11847 * if no such pair can be found.
11849 static int find_modulo_constraint_pair(__isl_keep isl_basic_map
*bmap
,
11850 int pos
, isl_int m
)
11855 isl_size n_div
, n_out
;
11856 unsigned o_div
, o_out
;
11859 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
11860 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
11861 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
11862 if (total
< 0 || n_out
< 0 || n_div
< 0)
11865 ctx
= isl_basic_map_get_ctx(bmap
);
11866 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
11867 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
11868 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
11869 if (!isl_int_abs_eq(bmap
->ineq
[i
][o_out
+ pos
], ctx
->one
))
11871 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + o_out
+ pos
+ 1,
11872 n_out
- (pos
+ 1)) != -1)
11874 if (first_div_may_involve_output(bmap
, bmap
->ineq
[i
] + o_div
,
11877 for (j
= i
+ 1; j
< bmap
->n_ineq
; ++j
) {
11878 if (!isl_int_abs_eq(bmap
->ineq
[j
][o_out
+ pos
],
11881 if (!isl_seq_is_neg(bmap
->ineq
[i
] + 1,
11882 bmap
->ineq
[j
] + 1, total
))
11886 if (j
>= bmap
->n_ineq
)
11888 isl_int_add(bmap
->ineq
[i
][0],
11889 bmap
->ineq
[i
][0], bmap
->ineq
[j
][0]);
11890 less
= isl_int_abs_lt(bmap
->ineq
[i
][0], m
);
11891 isl_int_sub(bmap
->ineq
[i
][0],
11892 bmap
->ineq
[i
][0], bmap
->ineq
[j
][0]);
11895 if (isl_int_is_one(bmap
->ineq
[i
][o_out
+ pos
]))
11901 return bmap
->n_ineq
;
11904 /* Return the index of the equality of "bmap" that defines
11905 * the output dimension "pos" in terms of earlier dimensions.
11906 * The equality may also involve integer divisions, as long
11907 * as those integer divisions are defined in terms of
11908 * parameters or input dimensions.
11909 * In this case, *div is set to the number of integer divisions and
11910 * *ineq is set to the number of inequality constraints (provided
11911 * div and ineq are not NULL).
11913 * The equality may also involve a single integer division involving
11914 * the output dimensions (typically only output dimension "pos") as
11915 * long as the coefficient of output dimension "pos" is 1 or -1 and
11916 * there is a pair of constraints i >= l and i <= l + n, with i referring
11917 * to output dimension "pos", l an expression involving only earlier
11918 * dimensions and n smaller than the coefficient of the integer division
11919 * in the equality. In this case, the output dimension can be defined
11920 * in terms of a modulo expression that does not involve the integer division.
11921 * *div is then set to this single integer division and
11922 * *ineq is set to the index of constraint i >= l.
11924 * Return bmap->n_eq if there is no such equality.
11925 * Return -1 on error.
11927 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map
*bmap
,
11928 int pos
, int *div
, int *ineq
)
11931 isl_size n_div
, n_out
;
11932 unsigned o_div
, o_out
;
11934 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
11935 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
11936 if (n_out
< 0 || n_div
< 0)
11939 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
11940 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
11943 *ineq
= bmap
->n_ineq
;
11946 for (j
= 0; j
< bmap
->n_eq
; ++j
) {
11947 if (isl_int_is_zero(bmap
->eq
[j
][o_out
+ pos
]))
11949 if (isl_seq_first_non_zero(bmap
->eq
[j
] + o_out
+ pos
+ 1,
11950 n_out
- (pos
+ 1)) != -1)
11952 k
= first_div_may_involve_output(bmap
, bmap
->eq
[j
] + o_div
,
11956 if (!isl_int_is_one(bmap
->eq
[j
][o_out
+ pos
]) &&
11957 !isl_int_is_negone(bmap
->eq
[j
][o_out
+ pos
]))
11959 if (first_div_may_involve_output(bmap
, bmap
->eq
[j
] + o_div
,
11960 k
+ 1, n_div
- (k
+1)) < n_div
)
11962 l
= find_modulo_constraint_pair(bmap
, pos
,
11963 bmap
->eq
[j
][o_div
+ k
]);
11966 if (l
>= bmap
->n_ineq
)
11978 /* Check if the given basic map is obviously single-valued.
11979 * In particular, for each output dimension, check that there is
11980 * an equality that defines the output dimension in terms of
11981 * earlier dimensions.
11983 isl_bool
isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map
*bmap
)
11988 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
11990 return isl_bool_error
;
11992 for (i
= 0; i
< n_out
; ++i
) {
11995 eq
= isl_basic_map_output_defining_equality(bmap
, i
,
11998 return isl_bool_error
;
11999 if (eq
>= bmap
->n_eq
)
12000 return isl_bool_false
;
12003 return isl_bool_true
;
12006 /* Check if the given basic map is single-valued.
12007 * We simply compute
12011 * and check if the result is a subset of the identity mapping.
12013 isl_bool
isl_basic_map_is_single_valued(__isl_keep isl_basic_map
*bmap
)
12016 isl_basic_map
*test
;
12020 sv
= isl_basic_map_plain_is_single_valued(bmap
);
12024 test
= isl_basic_map_reverse(isl_basic_map_copy(bmap
));
12025 test
= isl_basic_map_apply_range(test
, isl_basic_map_copy(bmap
));
12027 space
= isl_basic_map_get_space(bmap
);
12028 space
= isl_space_map_from_set(isl_space_range(space
));
12029 id
= isl_basic_map_identity(space
);
12031 sv
= isl_basic_map_is_subset(test
, id
);
12033 isl_basic_map_free(test
);
12034 isl_basic_map_free(id
);
12039 /* Check if the given map is obviously single-valued.
12041 isl_bool
isl_map_plain_is_single_valued(__isl_keep isl_map
*map
)
12044 return isl_bool_error
;
12046 return isl_bool_true
;
12048 return isl_bool_false
;
12050 return isl_basic_map_plain_is_single_valued(map
->p
[0]);
12053 /* Check if the given map is single-valued.
12054 * We simply compute
12058 * and check if the result is a subset of the identity mapping.
12060 isl_bool
isl_map_is_single_valued(__isl_keep isl_map
*map
)
12067 sv
= isl_map_plain_is_single_valued(map
);
12071 test
= isl_map_reverse(isl_map_copy(map
));
12072 test
= isl_map_apply_range(test
, isl_map_copy(map
));
12074 space
= isl_space_map_from_set(isl_space_range(isl_map_get_space(map
)));
12075 id
= isl_map_identity(space
);
12077 sv
= isl_map_is_subset(test
, id
);
12079 isl_map_free(test
);
12085 isl_bool
isl_map_is_injective(__isl_keep isl_map
*map
)
12089 map
= isl_map_copy(map
);
12090 map
= isl_map_reverse(map
);
12091 in
= isl_map_is_single_valued(map
);
12097 /* Check if the given map is obviously injective.
12099 isl_bool
isl_map_plain_is_injective(__isl_keep isl_map
*map
)
12103 map
= isl_map_copy(map
);
12104 map
= isl_map_reverse(map
);
12105 in
= isl_map_plain_is_single_valued(map
);
12111 isl_bool
isl_map_is_bijective(__isl_keep isl_map
*map
)
12115 sv
= isl_map_is_single_valued(map
);
12119 return isl_map_is_injective(map
);
12122 isl_bool
isl_set_is_singleton(__isl_keep isl_set
*set
)
12124 return isl_map_is_single_valued(set_to_map(set
));
12127 /* Does "map" only map elements to themselves?
12129 * If the domain and range spaces are different, then "map"
12130 * is considered not to be an identity relation, even if it is empty.
12131 * Otherwise, construct the maximal identity relation and
12132 * check whether "map" is a subset of this relation.
12134 isl_bool
isl_map_is_identity(__isl_keep isl_map
*map
)
12137 isl_bool equal
, is_identity
;
12139 equal
= isl_map_tuple_is_equal(map
, isl_dim_in
, map
, isl_dim_out
);
12140 if (equal
< 0 || !equal
)
12143 id
= isl_map_identity(isl_map_get_space(map
));
12144 is_identity
= isl_map_is_subset(map
, id
);
12147 return is_identity
;
12150 int isl_map_is_translation(__isl_keep isl_map
*map
)
12155 delta
= isl_map_deltas(isl_map_copy(map
));
12156 ok
= isl_set_is_singleton(delta
);
12157 isl_set_free(delta
);
12162 static int unique(isl_int
*p
, unsigned pos
, unsigned len
)
12164 if (isl_seq_first_non_zero(p
, pos
) != -1)
12166 if (isl_seq_first_non_zero(p
+ pos
+ 1, len
- pos
- 1) != -1)
12171 isl_bool
isl_basic_set_is_box(__isl_keep isl_basic_set
*bset
)
12174 isl_size nvar
, ovar
, n_div
;
12176 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
12178 return isl_bool_error
;
12180 return isl_bool_false
;
12182 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
12183 ovar
= isl_space_offset(bset
->dim
, isl_dim_set
);
12184 if (nvar
< 0 || ovar
< 0)
12185 return isl_bool_error
;
12186 for (j
= 0; j
< nvar
; ++j
) {
12187 int lower
= 0, upper
= 0;
12188 for (i
= 0; i
< bset
->n_eq
; ++i
) {
12189 if (isl_int_is_zero(bset
->eq
[i
][1 + ovar
+ j
]))
12191 if (!unique(bset
->eq
[i
] + 1 + ovar
, j
, nvar
))
12192 return isl_bool_false
;
12195 if (i
< bset
->n_eq
)
12197 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
12198 if (isl_int_is_zero(bset
->ineq
[i
][1 + ovar
+ j
]))
12200 if (!unique(bset
->ineq
[i
] + 1 + ovar
, j
, nvar
))
12201 return isl_bool_false
;
12202 if (isl_int_is_pos(bset
->ineq
[i
][1 + ovar
+ j
]))
12207 if (!lower
|| !upper
)
12208 return isl_bool_false
;
12211 return isl_bool_true
;
12214 isl_bool
isl_set_is_box(__isl_keep isl_set
*set
)
12217 return isl_bool_error
;
12219 return isl_bool_false
;
12221 return isl_basic_set_is_box(set
->p
[0]);
12224 isl_bool
isl_basic_set_is_wrapping(__isl_keep isl_basic_set
*bset
)
12227 return isl_bool_error
;
12229 return isl_space_is_wrapping(bset
->dim
);
12232 isl_bool
isl_set_is_wrapping(__isl_keep isl_set
*set
)
12235 return isl_bool_error
;
12237 return isl_space_is_wrapping(set
->dim
);
12240 /* Modify the space of "map" through a call to "change".
12241 * If "can_change" is set (not NULL), then first call it to check
12242 * if the modification is allowed, printing the error message "cannot_change"
12245 static __isl_give isl_map
*isl_map_change_space(__isl_take isl_map
*map
,
12246 isl_bool (*can_change
)(__isl_keep isl_map
*map
),
12247 const char *cannot_change
,
12248 __isl_give isl_space
*(*change
)(__isl_take isl_space
*space
))
12256 ok
= can_change
? can_change(map
) : isl_bool_true
;
12258 return isl_map_free(map
);
12260 isl_die(isl_map_get_ctx(map
), isl_error_invalid
, cannot_change
,
12261 return isl_map_free(map
));
12263 space
= change(isl_map_get_space(map
));
12264 map
= isl_map_reset_space(map
, space
);
12269 /* Is the domain of "map" a wrapped relation?
12271 isl_bool
isl_map_domain_is_wrapping(__isl_keep isl_map
*map
)
12274 return isl_bool_error
;
12276 return isl_space_domain_is_wrapping(map
->dim
);
12279 /* Does "map" have a wrapped relation in both domain and range?
12281 isl_bool
isl_map_is_product(__isl_keep isl_map
*map
)
12283 return isl_space_is_product(isl_map_peek_space(map
));
12286 /* Is the range of "map" a wrapped relation?
12288 isl_bool
isl_map_range_is_wrapping(__isl_keep isl_map
*map
)
12291 return isl_bool_error
;
12293 return isl_space_range_is_wrapping(map
->dim
);
12296 __isl_give isl_basic_set
*isl_basic_map_wrap(__isl_take isl_basic_map
*bmap
)
12300 space
= isl_basic_map_take_space(bmap
);
12301 space
= isl_space_wrap(space
);
12302 bmap
= isl_basic_map_restore_space(bmap
, space
);
12304 bmap
= isl_basic_map_finalize(bmap
);
12306 return bset_from_bmap(bmap
);
12309 /* Given a map A -> B, return the set (A -> B).
12311 __isl_give isl_set
*isl_map_wrap(__isl_take isl_map
*map
)
12313 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_wrap
);
12316 __isl_give isl_basic_map
*isl_basic_set_unwrap(__isl_take isl_basic_set
*bset
)
12318 bset
= isl_basic_set_cow(bset
);
12322 bset
->dim
= isl_space_unwrap(bset
->dim
);
12326 bset
= isl_basic_set_finalize(bset
);
12328 return bset_to_bmap(bset
);
12330 isl_basic_set_free(bset
);
12334 /* Given a set (A -> B), return the map A -> B.
12335 * Error out if "set" is not of the form (A -> B).
12337 __isl_give isl_map
*isl_set_unwrap(__isl_take isl_set
*set
)
12339 return isl_map_change_space(set
, &isl_set_is_wrapping
,
12340 "not a wrapping set", &isl_space_unwrap
);
12343 __isl_give isl_basic_map
*isl_basic_map_reset(__isl_take isl_basic_map
*bmap
,
12344 enum isl_dim_type type
)
12348 space
= isl_basic_map_take_space(bmap
);
12349 space
= isl_space_reset(space
, type
);
12350 bmap
= isl_basic_map_restore_space(bmap
, space
);
12352 bmap
= isl_basic_map_mark_final(bmap
);
12357 __isl_give isl_map
*isl_map_reset(__isl_take isl_map
*map
,
12358 enum isl_dim_type type
)
12366 if (!isl_space_is_named_or_nested(map
->dim
, type
))
12369 map
= isl_map_cow(map
);
12373 for (i
= 0; i
< map
->n
; ++i
) {
12374 map
->p
[i
] = isl_basic_map_reset(map
->p
[i
], type
);
12379 space
= isl_map_take_space(map
);
12380 space
= isl_space_reset(space
, type
);
12381 map
= isl_map_restore_space(map
, space
);
12389 __isl_give isl_basic_map
*isl_basic_map_flatten(__isl_take isl_basic_map
*bmap
)
12393 space
= isl_basic_map_take_space(bmap
);
12394 space
= isl_space_flatten(space
);
12395 bmap
= isl_basic_map_restore_space(bmap
, space
);
12397 bmap
= isl_basic_map_mark_final(bmap
);
12402 __isl_give isl_basic_set
*isl_basic_set_flatten(__isl_take isl_basic_set
*bset
)
12404 return bset_from_bmap(isl_basic_map_flatten(bset_to_bmap(bset
)));
12407 __isl_give isl_basic_map
*isl_basic_map_flatten_domain(
12408 __isl_take isl_basic_map
*bmap
)
12412 space
= isl_basic_map_take_space(bmap
);
12413 space
= isl_space_flatten_domain(space
);
12414 bmap
= isl_basic_map_restore_space(bmap
, space
);
12416 bmap
= isl_basic_map_mark_final(bmap
);
12421 __isl_give isl_basic_map
*isl_basic_map_flatten_range(
12422 __isl_take isl_basic_map
*bmap
)
12426 space
= isl_basic_map_take_space(bmap
);
12427 space
= isl_space_flatten_range(space
);
12428 bmap
= isl_basic_map_restore_space(bmap
, space
);
12430 bmap
= isl_basic_map_mark_final(bmap
);
12435 /* Remove any internal structure from the spaces of domain and range of "map".
12437 __isl_give isl_map
*isl_map_flatten(__isl_take isl_map
*map
)
12442 if (!map
->dim
->nested
[0] && !map
->dim
->nested
[1])
12445 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten
);
12448 __isl_give isl_set
*isl_set_flatten(__isl_take isl_set
*set
)
12450 return set_from_map(isl_map_flatten(set_to_map(set
)));
12453 __isl_give isl_map
*isl_set_flatten_map(__isl_take isl_set
*set
)
12455 isl_space
*space
, *flat_space
;
12458 space
= isl_set_get_space(set
);
12459 flat_space
= isl_space_flatten(isl_space_copy(space
));
12460 map
= isl_map_identity(isl_space_join(isl_space_reverse(space
),
12462 map
= isl_map_intersect_domain(map
, set
);
12467 /* Remove any internal structure from the space of the domain of "map".
12469 __isl_give isl_map
*isl_map_flatten_domain(__isl_take isl_map
*map
)
12474 if (!map
->dim
->nested
[0])
12477 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten_domain
);
12480 /* Remove any internal structure from the space of the range of "map".
12482 __isl_give isl_map
*isl_map_flatten_range(__isl_take isl_map
*map
)
12487 if (!map
->dim
->nested
[1])
12490 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten_range
);
12493 /* Reorder the dimensions of "bmap" according to the given dim_map
12494 * and set the dimension specification to "space" and
12495 * perform Gaussian elimination on the result.
12497 __isl_give isl_basic_map
*isl_basic_map_realign(__isl_take isl_basic_map
*bmap
,
12498 __isl_take isl_space
*space
, __isl_take
struct isl_dim_map
*dim_map
)
12500 isl_basic_map
*res
;
12504 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
12505 if (n_div
< 0 || !space
|| !dim_map
)
12508 flags
= bmap
->flags
;
12509 ISL_FL_CLR(flags
, ISL_BASIC_MAP_FINAL
);
12510 ISL_FL_CLR(flags
, ISL_BASIC_MAP_SORTED
);
12511 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
12512 res
= isl_basic_map_alloc_space(space
, n_div
, bmap
->n_eq
, bmap
->n_ineq
);
12513 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
12515 res
->flags
= flags
;
12516 res
= isl_basic_map_gauss(res
, NULL
);
12517 res
= isl_basic_map_finalize(res
);
12520 isl_dim_map_free(dim_map
);
12521 isl_basic_map_free(bmap
);
12522 isl_space_free(space
);
12526 /* Reorder the dimensions of "map" according to given reordering.
12528 __isl_give isl_map
*isl_map_realign(__isl_take isl_map
*map
,
12529 __isl_take isl_reordering
*r
)
12532 struct isl_dim_map
*dim_map
;
12534 map
= isl_map_cow(map
);
12535 dim_map
= isl_dim_map_from_reordering(r
);
12536 if (!map
|| !r
|| !dim_map
)
12539 for (i
= 0; i
< map
->n
; ++i
) {
12540 struct isl_dim_map
*dim_map_i
;
12543 dim_map_i
= isl_dim_map_extend(dim_map
, map
->p
[i
]);
12545 space
= isl_reordering_get_space(r
);
12546 map
->p
[i
] = isl_basic_map_realign(map
->p
[i
], space
, dim_map_i
);
12552 map
= isl_map_reset_space(map
, isl_reordering_get_space(r
));
12553 map
= isl_map_unmark_normalized(map
);
12555 isl_reordering_free(r
);
12556 isl_dim_map_free(dim_map
);
12559 isl_dim_map_free(dim_map
);
12561 isl_reordering_free(r
);
12565 __isl_give isl_set
*isl_set_realign(__isl_take isl_set
*set
,
12566 __isl_take isl_reordering
*r
)
12568 return set_from_map(isl_map_realign(set_to_map(set
), r
));
12571 __isl_give isl_map
*isl_map_align_params(__isl_take isl_map
*map
,
12572 __isl_take isl_space
*model
)
12577 if (!map
|| !model
)
12580 ctx
= isl_space_get_ctx(model
);
12581 if (!isl_space_has_named_params(model
))
12582 isl_die(ctx
, isl_error_invalid
,
12583 "model has unnamed parameters", goto error
);
12584 if (isl_map_check_named_params(map
) < 0)
12586 aligned
= isl_map_space_has_equal_params(map
, model
);
12591 isl_reordering
*exp
;
12593 space
= isl_map_peek_space(map
);
12594 exp
= isl_parameter_alignment_reordering(space
, model
);
12595 map
= isl_map_realign(map
, exp
);
12598 isl_space_free(model
);
12601 isl_space_free(model
);
12606 __isl_give isl_set
*isl_set_align_params(__isl_take isl_set
*set
,
12607 __isl_take isl_space
*model
)
12609 return isl_map_align_params(set
, model
);
12612 /* Align the parameters of "bmap" to those of "model", introducing
12613 * additional parameters if needed.
12615 __isl_give isl_basic_map
*isl_basic_map_align_params(
12616 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*model
)
12619 isl_bool equal_params
;
12620 isl_space
*bmap_space
;
12622 if (!bmap
|| !model
)
12625 ctx
= isl_space_get_ctx(model
);
12626 if (!isl_space_has_named_params(model
))
12627 isl_die(ctx
, isl_error_invalid
,
12628 "model has unnamed parameters", goto error
);
12629 if (isl_basic_map_check_named_params(bmap
) < 0)
12631 bmap_space
= isl_basic_map_peek_space(bmap
);
12632 equal_params
= isl_space_has_equal_params(bmap_space
, model
);
12633 if (equal_params
< 0)
12635 if (!equal_params
) {
12636 isl_reordering
*exp
;
12637 struct isl_dim_map
*dim_map
;
12639 exp
= isl_parameter_alignment_reordering(bmap_space
, model
);
12640 dim_map
= isl_dim_map_from_reordering(exp
);
12641 bmap
= isl_basic_map_realign(bmap
,
12642 isl_reordering_get_space(exp
),
12643 isl_dim_map_extend(dim_map
, bmap
));
12644 isl_reordering_free(exp
);
12645 isl_dim_map_free(dim_map
);
12648 isl_space_free(model
);
12651 isl_space_free(model
);
12652 isl_basic_map_free(bmap
);
12656 /* Do "bset" and "space" have the same parameters?
12658 isl_bool
isl_basic_set_space_has_equal_params(__isl_keep isl_basic_set
*bset
,
12659 __isl_keep isl_space
*space
)
12661 isl_space
*bset_space
;
12663 bset_space
= isl_basic_set_peek_space(bset
);
12664 return isl_space_has_equal_params(bset_space
, space
);
12667 /* Do "map" and "space" have the same parameters?
12669 isl_bool
isl_map_space_has_equal_params(__isl_keep isl_map
*map
,
12670 __isl_keep isl_space
*space
)
12672 isl_space
*map_space
;
12674 map_space
= isl_map_peek_space(map
);
12675 return isl_space_has_equal_params(map_space
, space
);
12678 /* Do "set" and "space" have the same parameters?
12680 isl_bool
isl_set_space_has_equal_params(__isl_keep isl_set
*set
,
12681 __isl_keep isl_space
*space
)
12683 return isl_map_space_has_equal_params(set_to_map(set
), space
);
12686 /* Align the parameters of "bset" to those of "model", introducing
12687 * additional parameters if needed.
12689 __isl_give isl_basic_set
*isl_basic_set_align_params(
12690 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*model
)
12692 return isl_basic_map_align_params(bset
, model
);
12696 #define TYPE isl_map
12697 #define isl_map_drop_dims isl_map_drop
12698 #include "isl_drop_unused_params_templ.c"
12700 /* Drop all parameters not referenced by "set".
12702 __isl_give isl_set
*isl_set_drop_unused_params(
12703 __isl_take isl_set
*set
)
12705 return set_from_map(isl_map_drop_unused_params(set_to_map(set
)));
12709 #define TYPE isl_basic_map
12710 #define isl_basic_map_drop_dims isl_basic_map_drop
12711 #include "isl_drop_unused_params_templ.c"
12713 /* Drop all parameters not referenced by "bset".
12715 __isl_give isl_basic_set
*isl_basic_set_drop_unused_params(
12716 __isl_take isl_basic_set
*bset
)
12718 return bset_from_bmap(isl_basic_map_drop_unused_params(
12719 bset_to_bmap(bset
)));
12722 /* Given a tuple of identifiers "tuple" in a space that corresponds
12723 * to that of "set", if any of those identifiers appear as parameters
12724 * in "set", then equate those parameters with the corresponding
12725 * set dimensions and project out the parameters.
12726 * The result therefore has no such parameters.
12728 static __isl_give isl_set
*equate_params(__isl_take isl_set
*set
,
12729 __isl_keep isl_multi_id
*tuple
)
12733 isl_space
*set_space
, *tuple_space
;
12735 set_space
= isl_set_peek_space(set
);
12736 tuple_space
= isl_multi_id_peek_space(tuple
);
12737 if (isl_space_check_equal_tuples(tuple_space
, set_space
) < 0)
12738 return isl_set_free(set
);
12739 n
= isl_multi_id_size(tuple
);
12741 return isl_set_free(set
);
12742 for (i
= 0; i
< n
; ++i
) {
12746 id
= isl_multi_id_get_at(tuple
, i
);
12748 return isl_set_free(set
);
12749 pos
= isl_set_find_dim_by_id(set
, isl_dim_param
, id
);
12753 set
= isl_set_equate(set
, isl_dim_param
, pos
, isl_dim_set
, i
);
12754 set
= isl_set_project_out(set
, isl_dim_param
, pos
, 1);
12759 /* Bind the set dimensions of "set" to parameters with identifiers
12760 * specified by "tuple", living in the same space as "set".
12762 * If no parameters with these identifiers appear in "set" already,
12763 * then the set dimensions are simply reinterpreted as parameters.
12764 * Otherwise, the parameters are first equated to the corresponding
12767 __isl_give isl_set
*isl_set_bind(__isl_take isl_set
*set
,
12768 __isl_take isl_multi_id
*tuple
)
12772 set
= equate_params(set
, tuple
);
12773 space
= isl_set_get_space(set
);
12774 space
= isl_space_bind_set(space
, tuple
);
12775 isl_multi_id_free(tuple
);
12776 set
= isl_set_reset_space(set
, space
);
12781 /* Given a tuple of identifiers "tuple" in a space that corresponds
12782 * to the domain of "map", if any of those identifiers appear as parameters
12783 * in "map", then equate those parameters with the corresponding
12784 * input dimensions and project out the parameters.
12785 * The result therefore has no such parameters.
12787 static __isl_give isl_map
*map_equate_params(__isl_take isl_map
*map
,
12788 __isl_keep isl_multi_id
*tuple
)
12792 isl_space
*map_space
, *tuple_space
;
12794 map_space
= isl_map_peek_space(map
);
12795 tuple_space
= isl_multi_id_peek_space(tuple
);
12796 if (isl_space_check_domain_tuples(tuple_space
, map_space
) < 0)
12797 return isl_map_free(map
);
12798 n
= isl_multi_id_size(tuple
);
12800 return isl_map_free(map
);
12801 for (i
= 0; i
< n
; ++i
) {
12805 id
= isl_multi_id_get_at(tuple
, i
);
12807 return isl_map_free(map
);
12808 pos
= isl_map_find_dim_by_id(map
, isl_dim_param
, id
);
12812 map
= isl_map_equate(map
, isl_dim_param
, pos
, isl_dim_in
, i
);
12813 map
= isl_map_project_out(map
, isl_dim_param
, pos
, 1);
12818 /* Bind the input dimensions of "map" to parameters with identifiers
12819 * specified by "tuple", living in the domain space of "map".
12821 * If no parameters with these identifiers appear in "map" already,
12822 * then the input dimensions are simply reinterpreted as parameters.
12823 * Otherwise, the parameters are first equated to the corresponding
12824 * input dimensions.
12826 __isl_give isl_set
*isl_map_bind_domain(__isl_take isl_map
*map
,
12827 __isl_take isl_multi_id
*tuple
)
12832 map
= map_equate_params(map
, tuple
);
12833 space
= isl_map_get_space(map
);
12834 space
= isl_space_bind_map_domain(space
, tuple
);
12835 isl_multi_id_free(tuple
);
12836 set
= set_from_map(isl_map_reset_space(map
, space
));
12841 /* Bind the output dimensions of "map" to parameters with identifiers
12842 * specified by "tuple", living in the range space of "map".
12844 * Since binding is more easily implemented on the domain,
12845 * bind the input dimensions of the inverse of "map".
12847 __isl_give isl_set
*isl_map_bind_range(__isl_take isl_map
*map
,
12848 __isl_take isl_multi_id
*tuple
)
12850 return isl_map_bind_domain(isl_map_reverse(map
), tuple
);
12853 /* Insert a domain corresponding to "tuple"
12854 * into the nullary or unary relation "set".
12855 * The result has an extra initial tuple and is therefore
12856 * either a unary or binary relation.
12857 * Any parameters with identifiers in "tuple" are reinterpreted
12858 * as the corresponding domain dimensions.
12860 static __isl_give isl_map
*unbind_params_insert_domain(
12861 __isl_take isl_set
*set
, __isl_take isl_multi_id
*tuple
)
12866 space
= isl_set_peek_space(set
);
12867 r
= isl_reordering_unbind_params_insert_domain(space
, tuple
);
12868 isl_multi_id_free(tuple
);
12870 return isl_map_realign(set_to_map(set
), r
);
12873 /* Construct a set with "tuple" as domain from the parameter domain "set".
12874 * Any parameters with identifiers in "tuple" are reinterpreted
12875 * as the corresponding set dimensions.
12877 __isl_give isl_set
*isl_set_unbind_params(__isl_take isl_set
*set
,
12878 __isl_take isl_multi_id
*tuple
)
12880 isl_bool is_params
;
12882 is_params
= isl_set_is_params(set
);
12884 set
= isl_set_free(set
);
12885 else if (!is_params
)
12886 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
12887 "expecting parameter domain", set
= isl_set_free(set
));
12888 return set_from_map(unbind_params_insert_domain(set
, tuple
));
12891 /* Check that "set" is a proper set, i.e., that it is not a parameter domain.
12893 static isl_stat
isl_set_check_is_set(__isl_keep isl_set
*set
)
12895 isl_bool is_params
;
12897 is_params
= isl_set_is_params(set
);
12899 return isl_stat_error
;
12900 else if (is_params
)
12901 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
12902 "expecting proper set", return isl_stat_error
);
12904 return isl_stat_ok
;
12907 /* Construct a map with "domain" as domain and "set" as range.
12908 * Any parameters with identifiers in "domain" are reinterpreted
12909 * as the corresponding domain dimensions.
12911 __isl_give isl_map
*isl_set_unbind_params_insert_domain(
12912 __isl_take isl_set
*set
, __isl_take isl_multi_id
*domain
)
12914 if (isl_set_check_is_set(set
) < 0)
12915 set
= isl_set_free(set
);
12916 return unbind_params_insert_domain(set
, domain
);
12919 /* Construct a map with "domain" as domain and "set" as range.
12921 __isl_give isl_map
*isl_set_insert_domain(__isl_take isl_set
*set
,
12922 __isl_take isl_space
*domain
)
12928 if (isl_set_check_is_set(set
) < 0 || isl_space_check_is_set(domain
) < 0)
12929 domain
= isl_space_free(domain
);
12930 dim
= isl_space_dim(domain
, isl_dim_set
);
12932 domain
= isl_space_free(domain
);
12933 space
= isl_set_get_space(set
);
12934 domain
= isl_space_replace_params(domain
, space
);
12935 space
= isl_space_map_from_domain_and_range(domain
, space
);
12937 map
= isl_map_from_range(set
);
12938 map
= isl_map_add_dims(map
, isl_dim_in
, dim
);
12939 map
= isl_map_reset_space(map
, space
);
12944 __isl_give isl_mat
*isl_basic_map_equalities_matrix(
12945 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
12946 enum isl_dim_type c2
, enum isl_dim_type c3
,
12947 enum isl_dim_type c4
, enum isl_dim_type c5
)
12949 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
12950 struct isl_mat
*mat
;
12955 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
12958 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_eq
, total
+ 1);
12961 for (i
= 0; i
< bmap
->n_eq
; ++i
)
12962 for (j
= 0, pos
= 0; j
< 5; ++j
) {
12963 int off
= isl_basic_map_offset(bmap
, c
[j
]);
12964 isl_size dim
= isl_basic_map_dim(bmap
, c
[j
]);
12966 return isl_mat_free(mat
);
12967 for (k
= 0; k
< dim
; ++k
) {
12968 isl_int_set(mat
->row
[i
][pos
],
12969 bmap
->eq
[i
][off
+ k
]);
12977 __isl_give isl_mat
*isl_basic_map_inequalities_matrix(
12978 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
12979 enum isl_dim_type c2
, enum isl_dim_type c3
,
12980 enum isl_dim_type c4
, enum isl_dim_type c5
)
12982 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
12983 struct isl_mat
*mat
;
12988 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
12991 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_ineq
, total
+ 1);
12994 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
12995 for (j
= 0, pos
= 0; j
< 5; ++j
) {
12996 int off
= isl_basic_map_offset(bmap
, c
[j
]);
12997 isl_size dim
= isl_basic_map_dim(bmap
, c
[j
]);
12999 return isl_mat_free(mat
);
13000 for (k
= 0; k
< dim
; ++k
) {
13001 isl_int_set(mat
->row
[i
][pos
],
13002 bmap
->ineq
[i
][off
+ k
]);
13010 __isl_give isl_basic_map
*isl_basic_map_from_constraint_matrices(
13011 __isl_take isl_space
*space
,
13012 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
13013 enum isl_dim_type c2
, enum isl_dim_type c3
,
13014 enum isl_dim_type c4
, enum isl_dim_type c5
)
13016 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
13017 isl_basic_map
*bmap
= NULL
;
13024 dim
= isl_space_dim(space
, isl_dim_all
);
13025 if (dim
< 0 || !eq
|| !ineq
)
13028 if (eq
->n_col
!= ineq
->n_col
)
13029 isl_die(space
->ctx
, isl_error_invalid
,
13030 "equalities and inequalities matrices should have "
13031 "same number of columns", goto error
);
13035 if (eq
->n_col
< total
)
13036 isl_die(space
->ctx
, isl_error_invalid
,
13037 "number of columns too small", goto error
);
13039 extra
= eq
->n_col
- total
;
13041 bmap
= isl_basic_map_alloc_space(isl_space_copy(space
), extra
,
13042 eq
->n_row
, ineq
->n_row
);
13045 for (i
= 0; i
< extra
; ++i
) {
13046 k
= isl_basic_map_alloc_div(bmap
);
13049 isl_int_set_si(bmap
->div
[k
][0], 0);
13051 for (i
= 0; i
< eq
->n_row
; ++i
) {
13052 l
= isl_basic_map_alloc_equality(bmap
);
13055 for (j
= 0, pos
= 0; j
< 5; ++j
) {
13056 int off
= isl_basic_map_offset(bmap
, c
[j
]);
13057 isl_size dim
= isl_basic_map_dim(bmap
, c
[j
]);
13060 for (k
= 0; k
< dim
; ++k
) {
13061 isl_int_set(bmap
->eq
[l
][off
+ k
],
13067 for (i
= 0; i
< ineq
->n_row
; ++i
) {
13068 l
= isl_basic_map_alloc_inequality(bmap
);
13071 for (j
= 0, pos
= 0; j
< 5; ++j
) {
13072 int off
= isl_basic_map_offset(bmap
, c
[j
]);
13073 isl_size dim
= isl_basic_map_dim(bmap
, c
[j
]);
13076 for (k
= 0; k
< dim
; ++k
) {
13077 isl_int_set(bmap
->ineq
[l
][off
+ k
],
13078 ineq
->row
[i
][pos
]);
13084 isl_space_free(space
);
13086 isl_mat_free(ineq
);
13088 bmap
= isl_basic_map_simplify(bmap
);
13089 return isl_basic_map_finalize(bmap
);
13091 isl_space_free(space
);
13093 isl_mat_free(ineq
);
13094 isl_basic_map_free(bmap
);
13098 __isl_give isl_mat
*isl_basic_set_equalities_matrix(
13099 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
13100 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
13102 return isl_basic_map_equalities_matrix(bset_to_bmap(bset
),
13103 c1
, c2
, c3
, c4
, isl_dim_in
);
13106 __isl_give isl_mat
*isl_basic_set_inequalities_matrix(
13107 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
13108 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
13110 return isl_basic_map_inequalities_matrix(bset_to_bmap(bset
),
13111 c1
, c2
, c3
, c4
, isl_dim_in
);
13114 __isl_give isl_basic_set
*isl_basic_set_from_constraint_matrices(
13115 __isl_take isl_space
*space
,
13116 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
13117 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
13119 isl_basic_map
*bmap
;
13120 bmap
= isl_basic_map_from_constraint_matrices(space
, eq
, ineq
,
13121 c1
, c2
, c3
, c4
, isl_dim_in
);
13122 return bset_from_bmap(bmap
);
13125 isl_bool
isl_basic_map_can_zip(__isl_keep isl_basic_map
*bmap
)
13128 return isl_bool_error
;
13130 return isl_space_can_zip(bmap
->dim
);
13133 isl_bool
isl_map_can_zip(__isl_keep isl_map
*map
)
13136 return isl_bool_error
;
13138 return isl_space_can_zip(map
->dim
);
13141 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
13142 * (A -> C) -> (B -> D).
13144 __isl_give isl_basic_map
*isl_basic_map_zip(__isl_take isl_basic_map
*bmap
)
13154 if (!isl_basic_map_can_zip(bmap
))
13155 isl_die(bmap
->ctx
, isl_error_invalid
,
13156 "basic map cannot be zipped", goto error
);
13157 n_in
= isl_space_dim(bmap
->dim
->nested
[0], isl_dim_in
);
13158 n1
= isl_space_dim(bmap
->dim
->nested
[0], isl_dim_out
);
13159 n2
= isl_space_dim(bmap
->dim
->nested
[1], isl_dim_in
);
13160 if (n_in
< 0 || n1
< 0 || n2
< 0)
13161 return isl_basic_map_free(bmap
);
13162 pos
= isl_basic_map_offset(bmap
, isl_dim_in
) + n_in
;
13163 bmap
= isl_basic_map_cow(bmap
);
13164 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
13167 bmap
->dim
= isl_space_zip(bmap
->dim
);
13170 bmap
= isl_basic_map_mark_final(bmap
);
13173 isl_basic_map_free(bmap
);
13177 /* Given a map (A -> B) -> (C -> D), return the corresponding map
13178 * (A -> C) -> (B -> D).
13180 __isl_give isl_map
*isl_map_zip(__isl_take isl_map
*map
)
13185 if (!isl_map_can_zip(map
))
13186 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be zipped",
13189 return isl_map_transform(map
, &isl_space_zip
, &isl_basic_map_zip
);
13195 /* Can we apply isl_basic_map_curry to "bmap"?
13196 * That is, does it have a nested relation in its domain?
13198 isl_bool
isl_basic_map_can_curry(__isl_keep isl_basic_map
*bmap
)
13201 return isl_bool_error
;
13203 return isl_space_can_curry(bmap
->dim
);
13206 /* Can we apply isl_map_curry to "map"?
13207 * That is, does it have a nested relation in its domain?
13209 isl_bool
isl_map_can_curry(__isl_keep isl_map
*map
)
13212 return isl_bool_error
;
13214 return isl_space_can_curry(map
->dim
);
13217 /* Given a basic map (A -> B) -> C, return the corresponding basic map
13220 __isl_give isl_basic_map
*isl_basic_map_curry(__isl_take isl_basic_map
*bmap
)
13226 if (!isl_basic_map_can_curry(bmap
))
13227 isl_die(bmap
->ctx
, isl_error_invalid
,
13228 "basic map cannot be curried", goto error
);
13229 bmap
= isl_basic_map_cow(bmap
);
13232 bmap
->dim
= isl_space_curry(bmap
->dim
);
13235 bmap
= isl_basic_map_mark_final(bmap
);
13238 isl_basic_map_free(bmap
);
13242 /* Given a map (A -> B) -> C, return the corresponding map
13245 __isl_give isl_map
*isl_map_curry(__isl_take isl_map
*map
)
13247 return isl_map_change_space(map
, &isl_map_can_curry
,
13248 "map cannot be curried", &isl_space_curry
);
13251 /* Can isl_map_range_curry be applied to "map"?
13252 * That is, does it have a nested relation in its range,
13253 * the domain of which is itself a nested relation?
13255 isl_bool
isl_map_can_range_curry(__isl_keep isl_map
*map
)
13258 return isl_bool_error
;
13260 return isl_space_can_range_curry(map
->dim
);
13263 /* Given a map A -> ((B -> C) -> D), return the corresponding map
13264 * A -> (B -> (C -> D)).
13266 __isl_give isl_map
*isl_map_range_curry(__isl_take isl_map
*map
)
13268 return isl_map_change_space(map
, &isl_map_can_range_curry
,
13269 "map range cannot be curried",
13270 &isl_space_range_curry
);
13273 /* Can we apply isl_basic_map_uncurry to "bmap"?
13274 * That is, does it have a nested relation in its domain?
13276 isl_bool
isl_basic_map_can_uncurry(__isl_keep isl_basic_map
*bmap
)
13279 return isl_bool_error
;
13281 return isl_space_can_uncurry(bmap
->dim
);
13284 /* Can we apply isl_map_uncurry to "map"?
13285 * That is, does it have a nested relation in its domain?
13287 isl_bool
isl_map_can_uncurry(__isl_keep isl_map
*map
)
13290 return isl_bool_error
;
13292 return isl_space_can_uncurry(map
->dim
);
13295 /* Given a basic map A -> (B -> C), return the corresponding basic map
13298 __isl_give isl_basic_map
*isl_basic_map_uncurry(__isl_take isl_basic_map
*bmap
)
13304 if (!isl_basic_map_can_uncurry(bmap
))
13305 isl_die(bmap
->ctx
, isl_error_invalid
,
13306 "basic map cannot be uncurried",
13307 return isl_basic_map_free(bmap
));
13308 bmap
= isl_basic_map_cow(bmap
);
13311 bmap
->dim
= isl_space_uncurry(bmap
->dim
);
13313 return isl_basic_map_free(bmap
);
13314 bmap
= isl_basic_map_mark_final(bmap
);
13318 /* Given a map A -> (B -> C), return the corresponding map
13321 __isl_give isl_map
*isl_map_uncurry(__isl_take isl_map
*map
)
13323 return isl_map_change_space(map
, &isl_map_can_uncurry
,
13324 "map cannot be uncurried", &isl_space_uncurry
);
13327 __isl_give isl_set
*isl_set_equate(__isl_take isl_set
*set
,
13328 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13330 return isl_map_equate(set
, type1
, pos1
, type2
, pos2
);
13333 /* Construct a basic map where the given dimensions are equal to each other.
13335 static __isl_give isl_basic_map
*equator(__isl_take isl_space
*space
,
13336 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13338 isl_basic_map
*bmap
= NULL
;
13342 total
= isl_space_dim(space
, isl_dim_all
);
13344 isl_space_check_range(space
, type1
, pos1
, 1) < 0 ||
13345 isl_space_check_range(space
, type2
, pos2
, 1) < 0)
13348 if (type1
== type2
&& pos1
== pos2
)
13349 return isl_basic_map_universe(space
);
13351 bmap
= isl_basic_map_alloc_space(isl_space_copy(space
), 0, 1, 0);
13352 i
= isl_basic_map_alloc_equality(bmap
);
13355 isl_seq_clr(bmap
->eq
[i
], 1 + total
);
13356 pos1
+= isl_basic_map_offset(bmap
, type1
);
13357 pos2
+= isl_basic_map_offset(bmap
, type2
);
13358 isl_int_set_si(bmap
->eq
[i
][pos1
], -1);
13359 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
13360 bmap
= isl_basic_map_finalize(bmap
);
13361 isl_space_free(space
);
13364 isl_space_free(space
);
13365 isl_basic_map_free(bmap
);
13369 /* Add a constraint imposing that the given two dimensions are equal.
13371 __isl_give isl_basic_map
*isl_basic_map_equate(__isl_take isl_basic_map
*bmap
,
13372 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13376 eq
= equator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
13378 bmap
= isl_basic_map_intersect(bmap
, eq
);
13383 /* Add a constraint imposing that the given two dimensions are equal.
13385 __isl_give isl_map
*isl_map_equate(__isl_take isl_map
*map
,
13386 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13388 isl_basic_map
*bmap
;
13390 bmap
= equator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
13392 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
13397 /* Add a constraint imposing that the given two dimensions have opposite values.
13399 __isl_give isl_map
*isl_map_oppose(__isl_take isl_map
*map
,
13400 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13402 isl_basic_map
*bmap
= NULL
;
13406 if (isl_map_check_range(map
, type1
, pos1
, 1) < 0)
13407 return isl_map_free(map
);
13408 if (isl_map_check_range(map
, type2
, pos2
, 1) < 0)
13409 return isl_map_free(map
);
13411 total
= isl_map_dim(map
, isl_dim_all
);
13413 return isl_map_free(map
);
13414 bmap
= isl_basic_map_alloc_space(isl_map_get_space(map
), 0, 1, 0);
13415 i
= isl_basic_map_alloc_equality(bmap
);
13418 isl_seq_clr(bmap
->eq
[i
], 1 + total
);
13419 pos1
+= isl_basic_map_offset(bmap
, type1
);
13420 pos2
+= isl_basic_map_offset(bmap
, type2
);
13421 isl_int_set_si(bmap
->eq
[i
][pos1
], 1);
13422 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
13423 bmap
= isl_basic_map_finalize(bmap
);
13425 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
13429 isl_basic_map_free(bmap
);
13434 /* Construct a constraint imposing that the value of the first dimension is
13435 * greater than or equal to that of the second.
13437 static __isl_give isl_constraint
*constraint_order_ge(
13438 __isl_take isl_space
*space
, enum isl_dim_type type1
, int pos1
,
13439 enum isl_dim_type type2
, int pos2
)
13443 if (isl_space_check_range(space
, type1
, pos1
, 1) < 0 ||
13444 isl_space_check_range(space
, type2
, pos2
, 1) < 0)
13445 space
= isl_space_free(space
);
13449 c
= isl_constraint_alloc_inequality(isl_local_space_from_space(space
));
13451 if (type1
== type2
&& pos1
== pos2
)
13454 c
= isl_constraint_set_coefficient_si(c
, type1
, pos1
, 1);
13455 c
= isl_constraint_set_coefficient_si(c
, type2
, pos2
, -1);
13460 /* Add a constraint imposing that the value of the first dimension is
13461 * greater than or equal to that of the second.
13463 __isl_give isl_basic_map
*isl_basic_map_order_ge(__isl_take isl_basic_map
*bmap
,
13464 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13469 if (type1
== type2
&& pos1
== pos2
)
13471 space
= isl_basic_map_get_space(bmap
);
13472 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
13473 bmap
= isl_basic_map_add_constraint(bmap
, c
);
13478 /* Add a constraint imposing that the value of the first dimension is
13479 * greater than or equal to that of the second.
13481 __isl_give isl_map
*isl_map_order_ge(__isl_take isl_map
*map
,
13482 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13487 if (type1
== type2
&& pos1
== pos2
)
13489 space
= isl_map_get_space(map
);
13490 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
13491 map
= isl_map_add_constraint(map
, c
);
13496 /* Add a constraint imposing that the value of the first dimension is
13497 * less than or equal to that of the second.
13499 __isl_give isl_map
*isl_map_order_le(__isl_take isl_map
*map
,
13500 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13502 return isl_map_order_ge(map
, type2
, pos2
, type1
, pos1
);
13505 /* Construct a basic map where the value of the first dimension is
13506 * greater than that of the second.
13508 static __isl_give isl_basic_map
*greator(__isl_take isl_space
*space
,
13509 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13511 isl_basic_map
*bmap
= NULL
;
13515 if (isl_space_check_range(space
, type1
, pos1
, 1) < 0 ||
13516 isl_space_check_range(space
, type2
, pos2
, 1) < 0)
13519 if (type1
== type2
&& pos1
== pos2
)
13520 return isl_basic_map_empty(space
);
13522 bmap
= isl_basic_map_alloc_space(space
, 0, 0, 1);
13523 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
13524 i
= isl_basic_map_alloc_inequality(bmap
);
13525 if (total
< 0 || i
< 0)
13526 return isl_basic_map_free(bmap
);
13527 isl_seq_clr(bmap
->ineq
[i
], 1 + total
);
13528 pos1
+= isl_basic_map_offset(bmap
, type1
);
13529 pos2
+= isl_basic_map_offset(bmap
, type2
);
13530 isl_int_set_si(bmap
->ineq
[i
][pos1
], 1);
13531 isl_int_set_si(bmap
->ineq
[i
][pos2
], -1);
13532 isl_int_set_si(bmap
->ineq
[i
][0], -1);
13533 bmap
= isl_basic_map_finalize(bmap
);
13537 isl_space_free(space
);
13538 isl_basic_map_free(bmap
);
13542 /* Add a constraint imposing that the value of the first dimension is
13543 * greater than that of the second.
13545 __isl_give isl_basic_map
*isl_basic_map_order_gt(__isl_take isl_basic_map
*bmap
,
13546 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13550 gt
= greator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
13552 bmap
= isl_basic_map_intersect(bmap
, gt
);
13557 /* Add a constraint imposing that the value of the first dimension is
13558 * greater than that of the second.
13560 __isl_give isl_map
*isl_map_order_gt(__isl_take isl_map
*map
,
13561 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13563 isl_basic_map
*bmap
;
13565 bmap
= greator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
13567 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
13572 /* Add a constraint imposing that the value of the first dimension is
13573 * smaller than that of the second.
13575 __isl_give isl_map
*isl_map_order_lt(__isl_take isl_map
*map
,
13576 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13578 return isl_map_order_gt(map
, type2
, pos2
, type1
, pos1
);
13581 __isl_give isl_aff
*isl_basic_map_get_div(__isl_keep isl_basic_map
*bmap
,
13585 isl_local_space
*ls
;
13590 if (!isl_basic_map_divs_known(bmap
))
13591 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
13592 "some divs are unknown", return NULL
);
13594 ls
= isl_basic_map_get_local_space(bmap
);
13595 div
= isl_local_space_get_div(ls
, pos
);
13596 isl_local_space_free(ls
);
13601 __isl_give isl_aff
*isl_basic_set_get_div(__isl_keep isl_basic_set
*bset
,
13604 return isl_basic_map_get_div(bset
, pos
);
13607 /* Plug in "subs" for set dimension "pos" of "set".
13609 __isl_give isl_set
*isl_set_substitute(__isl_take isl_set
*set
,
13610 unsigned pos
, __isl_keep isl_aff
*subs
)
13614 if (set
&& isl_set_plain_is_empty(set
))
13617 ma
= isl_multi_aff_identity_on_domain_space(isl_set_get_space(set
));
13618 ma
= isl_multi_aff_set_aff(ma
, pos
, isl_aff_copy(subs
));
13619 return isl_set_preimage_multi_aff(set
, ma
);
13622 /* Check if the range of "ma" is compatible with the domain or range
13623 * (depending on "type") of "bmap".
13625 static isl_stat
check_basic_map_compatible_range_multi_aff(
13626 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type type
,
13627 __isl_keep isl_multi_aff
*ma
)
13630 isl_space
*ma_space
;
13632 ma_space
= isl_multi_aff_get_space(ma
);
13634 m
= isl_space_has_equal_params(bmap
->dim
, ma_space
);
13638 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
13639 "parameters don't match", goto error
);
13640 m
= isl_space_tuple_is_equal(bmap
->dim
, type
, ma_space
, isl_dim_out
);
13644 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
13645 "spaces don't match", goto error
);
13647 isl_space_free(ma_space
);
13648 return isl_stat_ok
;
13650 isl_space_free(ma_space
);
13651 return isl_stat_error
;
13654 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
13655 * coefficients before the transformed range of dimensions,
13656 * the "n_after" coefficients after the transformed range of dimensions
13657 * and the coefficients of the other divs in "bmap".
13659 static __isl_give isl_basic_map
*set_ma_divs(__isl_take isl_basic_map
*bmap
,
13660 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
, int n_div
)
13665 isl_local_space
*ls
;
13670 ls
= isl_aff_get_domain_local_space(ma
->u
.p
[0]);
13671 n_param
= isl_local_space_dim(ls
, isl_dim_param
);
13672 n_set
= isl_local_space_dim(ls
, isl_dim_set
);
13673 if (n_param
< 0 || n_set
< 0)
13674 return isl_basic_map_free(bmap
);
13676 for (i
= 0; i
< n_div
; ++i
) {
13677 int o_bmap
= 0, o_ls
= 0;
13679 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], 1 + 1 + n_param
);
13680 o_bmap
+= 1 + 1 + n_param
;
13681 o_ls
+= 1 + 1 + n_param
;
13682 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_before
);
13683 o_bmap
+= n_before
;
13684 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
13685 ls
->div
->row
[i
] + o_ls
, n_set
);
13688 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_after
);
13690 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
13691 ls
->div
->row
[i
] + o_ls
, n_div
);
13694 isl_seq_clr(bmap
->div
[i
] + o_bmap
, bmap
->n_div
- n_div
);
13695 bmap
= isl_basic_map_add_div_constraints(bmap
, i
);
13700 isl_local_space_free(ls
);
13703 isl_local_space_free(ls
);
13704 return isl_basic_map_free(bmap
);
13707 /* How many stride constraints does "ma" enforce?
13708 * That is, how many of the affine expressions have a denominator
13709 * different from one?
13711 static int multi_aff_strides(__isl_keep isl_multi_aff
*ma
)
13716 for (i
= 0; i
< ma
->n
; ++i
)
13717 if (!isl_int_is_one(ma
->u
.p
[i
]->v
->el
[0]))
13723 /* For each affine expression in ma of the form
13725 * x_i = (f_i y + h_i)/m_i
13727 * with m_i different from one, add a constraint to "bmap"
13730 * f_i y + h_i = m_i alpha_i
13732 * with alpha_i an additional existentially quantified variable.
13734 * The input variables of "ma" correspond to a subset of the variables
13735 * of "bmap". There are "n_before" variables in "bmap" before this
13736 * subset and "n_after" variables after this subset.
13737 * The integer divisions of the affine expressions in "ma" are assumed
13738 * to have been aligned. There are "n_div_ma" of them and
13739 * they appear first in "bmap", straight after the "n_after" variables.
13741 static __isl_give isl_basic_map
*add_ma_strides(
13742 __isl_take isl_basic_map
*bmap
, __isl_keep isl_multi_aff
*ma
,
13743 int n_before
, int n_after
, int n_div_ma
)
13751 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
13752 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
13753 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
13754 if (total
< 0 || n_param
< 0 || n_in
< 0)
13755 return isl_basic_map_free(bmap
);
13756 for (i
= 0; i
< ma
->n
; ++i
) {
13757 int o_bmap
= 0, o_ma
= 1;
13759 if (isl_int_is_one(ma
->u
.p
[i
]->v
->el
[0]))
13761 div
= isl_basic_map_alloc_div(bmap
);
13762 k
= isl_basic_map_alloc_equality(bmap
);
13763 if (div
< 0 || k
< 0)
13765 isl_int_set_si(bmap
->div
[div
][0], 0);
13766 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
13767 ma
->u
.p
[i
]->v
->el
+ o_ma
, 1 + n_param
);
13768 o_bmap
+= 1 + n_param
;
13769 o_ma
+= 1 + n_param
;
13770 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_before
);
13771 o_bmap
+= n_before
;
13772 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
13773 ma
->u
.p
[i
]->v
->el
+ o_ma
, n_in
);
13776 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_after
);
13778 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
13779 ma
->u
.p
[i
]->v
->el
+ o_ma
, n_div_ma
);
13780 o_bmap
+= n_div_ma
;
13782 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, 1 + total
- o_bmap
);
13783 isl_int_neg(bmap
->eq
[k
][1 + total
], ma
->u
.p
[i
]->v
->el
[0]);
13789 isl_basic_map_free(bmap
);
13793 /* Replace the domain or range space (depending on "type) of "space" by "set".
13795 static __isl_give isl_space
*isl_space_set(__isl_take isl_space
*space
,
13796 enum isl_dim_type type
, __isl_take isl_space
*set
)
13798 if (type
== isl_dim_in
) {
13799 space
= isl_space_range(space
);
13800 space
= isl_space_map_from_domain_and_range(set
, space
);
13802 space
= isl_space_domain(space
);
13803 space
= isl_space_map_from_domain_and_range(space
, set
);
13809 /* Compute the preimage of the domain or range (depending on "type")
13810 * of "bmap" under the function represented by "ma".
13811 * In other words, plug in "ma" in the domain or range of "bmap".
13812 * The result is a basic map that lives in the same space as "bmap"
13813 * except that the domain or range has been replaced by
13814 * the domain space of "ma".
13816 * If bmap is represented by
13818 * A(p) + S u + B x + T v + C(divs) >= 0,
13820 * where u and x are input and output dimensions if type == isl_dim_out
13821 * while x and v are input and output dimensions if type == isl_dim_in,
13822 * and ma is represented by
13824 * x = D(p) + F(y) + G(divs')
13826 * then the result is
13828 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
13830 * The divs in the input set are similarly adjusted.
13833 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
13837 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
13838 * B_i G(divs') + c_i(divs))/n_i)
13840 * If bmap is not a rational map and if F(y) involves any denominators
13842 * x_i = (f_i y + h_i)/m_i
13844 * then additional constraints are added to ensure that we only
13845 * map back integer points. That is we enforce
13847 * f_i y + h_i = m_i alpha_i
13849 * with alpha_i an additional existentially quantified variable.
13851 * We first copy over the divs from "ma".
13852 * Then we add the modified constraints and divs from "bmap".
13853 * Finally, we add the stride constraints, if needed.
13855 __isl_give isl_basic_map
*isl_basic_map_preimage_multi_aff(
13856 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
13857 __isl_take isl_multi_aff
*ma
)
13861 isl_basic_map
*res
= NULL
;
13862 isl_size n_before
, n_after
, n_div_bmap
, n_div_ma
;
13863 isl_int f
, c1
, c2
, g
;
13872 ma
= isl_multi_aff_align_divs(ma
);
13875 if (check_basic_map_compatible_range_multi_aff(bmap
, type
, ma
) < 0)
13878 if (type
== isl_dim_in
) {
13880 n_after
= isl_basic_map_dim(bmap
, isl_dim_out
);
13882 n_before
= isl_basic_map_dim(bmap
, isl_dim_in
);
13885 n_div_bmap
= isl_basic_map_dim(bmap
, isl_dim_div
);
13886 n_div_ma
= ma
->n
? isl_aff_dim(ma
->u
.p
[0], isl_dim_div
) : 0;
13887 if (n_before
< 0 || n_after
< 0 || n_div_bmap
< 0 || n_div_ma
< 0)
13890 space
= isl_multi_aff_get_domain_space(ma
);
13891 space
= isl_space_set(isl_basic_map_get_space(bmap
), type
, space
);
13892 rational
= isl_basic_map_is_rational(bmap
);
13893 strides
= rational
? 0 : multi_aff_strides(ma
);
13894 res
= isl_basic_map_alloc_space(space
, n_div_ma
+ n_div_bmap
+ strides
,
13895 bmap
->n_eq
+ strides
, bmap
->n_ineq
+ 2 * n_div_ma
);
13897 res
= isl_basic_map_set_rational(res
);
13899 for (i
= 0; i
< n_div_ma
+ n_div_bmap
; ++i
)
13900 if (isl_basic_map_alloc_div(res
) < 0)
13903 res
= set_ma_divs(res
, ma
, n_before
, n_after
, n_div_ma
);
13907 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
13908 k
= isl_basic_map_alloc_equality(res
);
13911 if (isl_seq_preimage(res
->eq
[k
], bmap
->eq
[i
], ma
, n_before
,
13912 n_after
, n_div_ma
, n_div_bmap
,
13913 f
, c1
, c2
, g
, 0) < 0)
13917 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
13918 k
= isl_basic_map_alloc_inequality(res
);
13921 if (isl_seq_preimage(res
->ineq
[k
], bmap
->ineq
[i
], ma
, n_before
,
13922 n_after
, n_div_ma
, n_div_bmap
,
13923 f
, c1
, c2
, g
, 0) < 0)
13927 for (i
= 0; i
< bmap
->n_div
; ++i
) {
13928 if (isl_int_is_zero(bmap
->div
[i
][0])) {
13929 isl_int_set_si(res
->div
[n_div_ma
+ i
][0], 0);
13932 if (isl_seq_preimage(res
->div
[n_div_ma
+ i
], bmap
->div
[i
], ma
,
13933 n_before
, n_after
, n_div_ma
, n_div_bmap
,
13934 f
, c1
, c2
, g
, 1) < 0)
13939 res
= add_ma_strides(res
, ma
, n_before
, n_after
, n_div_ma
);
13945 isl_basic_map_free(bmap
);
13946 isl_multi_aff_free(ma
);
13947 res
= isl_basic_map_simplify(res
);
13948 return isl_basic_map_finalize(res
);
13954 isl_basic_map_free(bmap
);
13955 isl_multi_aff_free(ma
);
13956 isl_basic_map_free(res
);
13960 /* Compute the preimage of "bset" under the function represented by "ma".
13961 * In other words, plug in "ma" in "bset". The result is a basic set
13962 * that lives in the domain space of "ma".
13964 __isl_give isl_basic_set
*isl_basic_set_preimage_multi_aff(
13965 __isl_take isl_basic_set
*bset
, __isl_take isl_multi_aff
*ma
)
13967 return isl_basic_map_preimage_multi_aff(bset
, isl_dim_set
, ma
);
13970 /* Compute the preimage of the domain of "bmap" under the function
13971 * represented by "ma".
13972 * In other words, plug in "ma" in the domain of "bmap".
13973 * The result is a basic map that lives in the same space as "bmap"
13974 * except that the domain has been replaced by the domain space of "ma".
13976 __isl_give isl_basic_map
*isl_basic_map_preimage_domain_multi_aff(
13977 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
13979 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_in
, ma
);
13982 /* Compute the preimage of the range of "bmap" under the function
13983 * represented by "ma".
13984 * In other words, plug in "ma" in the range of "bmap".
13985 * The result is a basic map that lives in the same space as "bmap"
13986 * except that the range has been replaced by the domain space of "ma".
13988 __isl_give isl_basic_map
*isl_basic_map_preimage_range_multi_aff(
13989 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
13991 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_out
, ma
);
13994 /* Check if the range of "ma" is compatible with the domain or range
13995 * (depending on "type") of "map".
13996 * Return isl_stat_error if anything is wrong.
13998 static isl_stat
check_map_compatible_range_multi_aff(
13999 __isl_keep isl_map
*map
, enum isl_dim_type type
,
14000 __isl_keep isl_multi_aff
*ma
)
14003 isl_space
*ma_space
;
14005 ma_space
= isl_multi_aff_get_space(ma
);
14006 m
= isl_map_space_tuple_is_equal(map
, type
, ma_space
, isl_dim_out
);
14007 isl_space_free(ma_space
);
14009 return isl_stat_error
;
14011 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
14012 "spaces don't match", return isl_stat_error
);
14013 return isl_stat_ok
;
14016 /* Compute the preimage of the domain or range (depending on "type")
14017 * of "map" under the function represented by "ma".
14018 * In other words, plug in "ma" in the domain or range of "map".
14019 * The result is a map that lives in the same space as "map"
14020 * except that the domain or range has been replaced by
14021 * the domain space of "ma".
14023 * The parameters are assumed to have been aligned.
14025 static __isl_give isl_map
*map_preimage_multi_aff(__isl_take isl_map
*map
,
14026 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
14031 map
= isl_map_cow(map
);
14032 ma
= isl_multi_aff_align_divs(ma
);
14035 if (check_map_compatible_range_multi_aff(map
, type
, ma
) < 0)
14038 for (i
= 0; i
< map
->n
; ++i
) {
14039 map
->p
[i
] = isl_basic_map_preimage_multi_aff(map
->p
[i
], type
,
14040 isl_multi_aff_copy(ma
));
14045 space
= isl_multi_aff_get_domain_space(ma
);
14046 space
= isl_space_set(isl_map_get_space(map
), type
, space
);
14048 isl_space_free(isl_map_take_space(map
));
14049 map
= isl_map_restore_space(map
, space
);
14053 isl_multi_aff_free(ma
);
14055 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
14056 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
14059 isl_multi_aff_free(ma
);
14064 /* Compute the preimage of the domain or range (depending on "type")
14065 * of "map" under the function represented by "ma".
14066 * In other words, plug in "ma" in the domain or range of "map".
14067 * The result is a map that lives in the same space as "map"
14068 * except that the domain or range has been replaced by
14069 * the domain space of "ma".
14071 __isl_give isl_map
*isl_map_preimage_multi_aff(__isl_take isl_map
*map
,
14072 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
14079 aligned
= isl_map_space_has_equal_params(map
, ma
->space
);
14083 return map_preimage_multi_aff(map
, type
, ma
);
14085 if (isl_map_check_named_params(map
) < 0)
14087 if (!isl_space_has_named_params(ma
->space
))
14088 isl_die(map
->ctx
, isl_error_invalid
,
14089 "unaligned unnamed parameters", goto error
);
14090 map
= isl_map_align_params(map
, isl_multi_aff_get_space(ma
));
14091 ma
= isl_multi_aff_align_params(ma
, isl_map_get_space(map
));
14093 return map_preimage_multi_aff(map
, type
, ma
);
14095 isl_multi_aff_free(ma
);
14096 return isl_map_free(map
);
14099 /* Compute the preimage of "set" under the function represented by "ma".
14100 * In other words, plug in "ma" in "set". The result is a set
14101 * that lives in the domain space of "ma".
14103 __isl_give isl_set
*isl_set_preimage_multi_aff(__isl_take isl_set
*set
,
14104 __isl_take isl_multi_aff
*ma
)
14106 return isl_map_preimage_multi_aff(set
, isl_dim_set
, ma
);
14109 /* Compute the preimage of the domain of "map" under the function
14110 * represented by "ma".
14111 * In other words, plug in "ma" in the domain of "map".
14112 * The result is a map that lives in the same space as "map"
14113 * except that the domain has been replaced by the domain space of "ma".
14115 __isl_give isl_map
*isl_map_preimage_domain_multi_aff(__isl_take isl_map
*map
,
14116 __isl_take isl_multi_aff
*ma
)
14118 return isl_map_preimage_multi_aff(map
, isl_dim_in
, ma
);
14121 /* Compute the preimage of the range of "map" under the function
14122 * represented by "ma".
14123 * In other words, plug in "ma" in the range of "map".
14124 * The result is a map that lives in the same space as "map"
14125 * except that the range has been replaced by the domain space of "ma".
14127 __isl_give isl_map
*isl_map_preimage_range_multi_aff(__isl_take isl_map
*map
,
14128 __isl_take isl_multi_aff
*ma
)
14130 return isl_map_preimage_multi_aff(map
, isl_dim_out
, ma
);
14133 /* Compute the preimage of "map" under the function represented by "pma".
14134 * In other words, plug in "pma" in the domain or range of "map".
14135 * The result is a map that lives in the same space as "map",
14136 * except that the space of type "type" has been replaced by
14137 * the domain space of "pma".
14139 * The parameters of "map" and "pma" are assumed to have been aligned.
14141 static __isl_give isl_map
*isl_map_preimage_pw_multi_aff_aligned(
14142 __isl_take isl_map
*map
, enum isl_dim_type type
,
14143 __isl_take isl_pw_multi_aff
*pma
)
14154 space
= isl_pw_multi_aff_get_domain_space(pma
);
14155 isl_pw_multi_aff_free(pma
);
14156 space
= isl_space_set(isl_map_get_space(map
), type
, space
);
14158 return isl_map_empty(space
);
14161 res
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
14162 isl_multi_aff_copy(pma
->p
[0].maff
));
14163 if (type
== isl_dim_in
)
14164 res
= isl_map_intersect_domain(res
,
14165 isl_map_copy(pma
->p
[0].set
));
14167 res
= isl_map_intersect_range(res
,
14168 isl_map_copy(pma
->p
[0].set
));
14170 for (i
= 1; i
< pma
->n
; ++i
) {
14173 res_i
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
14174 isl_multi_aff_copy(pma
->p
[i
].maff
));
14175 if (type
== isl_dim_in
)
14176 res_i
= isl_map_intersect_domain(res_i
,
14177 isl_map_copy(pma
->p
[i
].set
));
14179 res_i
= isl_map_intersect_range(res_i
,
14180 isl_map_copy(pma
->p
[i
].set
));
14181 res
= isl_map_union(res
, res_i
);
14184 isl_pw_multi_aff_free(pma
);
14188 isl_pw_multi_aff_free(pma
);
14193 /* Compute the preimage of "map" under the function represented by "pma".
14194 * In other words, plug in "pma" in the domain or range of "map".
14195 * The result is a map that lives in the same space as "map",
14196 * except that the space of type "type" has been replaced by
14197 * the domain space of "pma".
14199 __isl_give isl_map
*isl_map_preimage_pw_multi_aff(__isl_take isl_map
*map
,
14200 enum isl_dim_type type
, __isl_take isl_pw_multi_aff
*pma
)
14207 aligned
= isl_map_space_has_equal_params(map
, pma
->dim
);
14211 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
14213 if (isl_map_check_named_params(map
) < 0)
14215 if (isl_pw_multi_aff_check_named_params(pma
) < 0)
14217 map
= isl_map_align_params(map
, isl_pw_multi_aff_get_space(pma
));
14218 pma
= isl_pw_multi_aff_align_params(pma
, isl_map_get_space(map
));
14220 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
14222 isl_pw_multi_aff_free(pma
);
14223 return isl_map_free(map
);
14226 /* Compute the preimage of "set" under the function represented by "pma".
14227 * In other words, plug in "pma" in "set". The result is a set
14228 * that lives in the domain space of "pma".
14230 __isl_give isl_set
*isl_set_preimage_pw_multi_aff(__isl_take isl_set
*set
,
14231 __isl_take isl_pw_multi_aff
*pma
)
14233 return isl_map_preimage_pw_multi_aff(set
, isl_dim_set
, pma
);
14236 /* Compute the preimage of the domain of "map" under the function
14237 * represented by "pma".
14238 * In other words, plug in "pma" in the domain of "map".
14239 * The result is a map that lives in the same space as "map",
14240 * except that domain space has been replaced by the domain space of "pma".
14242 __isl_give isl_map
*isl_map_preimage_domain_pw_multi_aff(
14243 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
14245 return isl_map_preimage_pw_multi_aff(map
, isl_dim_in
, pma
);
14248 /* Compute the preimage of the range of "map" under the function
14249 * represented by "pma".
14250 * In other words, plug in "pma" in the range of "map".
14251 * The result is a map that lives in the same space as "map",
14252 * except that range space has been replaced by the domain space of "pma".
14254 __isl_give isl_map
*isl_map_preimage_range_pw_multi_aff(
14255 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
14257 return isl_map_preimage_pw_multi_aff(map
, isl_dim_out
, pma
);
14260 /* Compute the preimage of "map" under the function represented by "mpa".
14261 * In other words, plug in "mpa" in the domain or range of "map".
14262 * The result is a map that lives in the same space as "map",
14263 * except that the space of type "type" has been replaced by
14264 * the domain space of "mpa".
14266 * If the map does not involve any constraints that refer to the
14267 * dimensions of the substituted space, then the only possible
14268 * effect of "mpa" on the map is to map the space to a different space.
14269 * We create a separate isl_multi_aff to effectuate this change
14270 * in order to avoid spurious splitting of the map along the pieces
14272 * If "mpa" has a non-trivial explicit domain, however,
14273 * then the full substitution should be performed.
14275 __isl_give isl_map
*isl_map_preimage_multi_pw_aff(__isl_take isl_map
*map
,
14276 enum isl_dim_type type
, __isl_take isl_multi_pw_aff
*mpa
)
14280 isl_pw_multi_aff
*pma
;
14282 n
= isl_map_dim(map
, type
);
14286 full
= isl_map_involves_dims(map
, type
, 0, n
);
14287 if (full
>= 0 && !full
)
14288 full
= isl_multi_pw_aff_has_non_trivial_domain(mpa
);
14295 space
= isl_multi_pw_aff_get_space(mpa
);
14296 isl_multi_pw_aff_free(mpa
);
14297 ma
= isl_multi_aff_zero(space
);
14298 return isl_map_preimage_multi_aff(map
, type
, ma
);
14301 pma
= isl_pw_multi_aff_from_multi_pw_aff(mpa
);
14302 return isl_map_preimage_pw_multi_aff(map
, type
, pma
);
14305 isl_multi_pw_aff_free(mpa
);
14309 /* Compute the preimage of "map" under the function represented by "mpa".
14310 * In other words, plug in "mpa" in the domain "map".
14311 * The result is a map that lives in the same space as "map",
14312 * except that domain space has been replaced by the domain space of "mpa".
14314 __isl_give isl_map
*isl_map_preimage_domain_multi_pw_aff(
14315 __isl_take isl_map
*map
, __isl_take isl_multi_pw_aff
*mpa
)
14317 return isl_map_preimage_multi_pw_aff(map
, isl_dim_in
, mpa
);
14320 /* Compute the preimage of "set" by the function represented by "mpa".
14321 * In other words, plug in "mpa" in "set".
14323 __isl_give isl_set
*isl_set_preimage_multi_pw_aff(__isl_take isl_set
*set
,
14324 __isl_take isl_multi_pw_aff
*mpa
)
14326 return isl_map_preimage_multi_pw_aff(set
, isl_dim_set
, mpa
);
14329 /* Given that inequality "ineq" of "bmap" expresses an upper bound
14330 * on the output dimension "pos" in terms of the parameters,
14331 * the input dimensions and possibly some integer divisions,
14332 * but not any other output dimensions, extract this upper bound
14333 * as a function of all dimensions (with zero coefficients
14334 * for the output dimensions).
14336 * That is, the inequality is of the form
14338 * e(...) + c - m x >= 0
14340 * where e does not depend on any other output dimensions.
14341 * Return (e(...) + c) / m, with the denominator m in the first position.
14343 __isl_give isl_vec
*isl_basic_map_inequality_extract_output_upper_bound(
14344 __isl_keep isl_basic_map
*bmap
, int ineq
, int pos
)
14348 isl_size v_out
, total
;
14350 v_out
= isl_basic_map_var_offset(bmap
, isl_dim_out
);
14351 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
14352 if (v_out
< 0 || total
< 0)
14354 ctx
= isl_basic_map_get_ctx(bmap
);
14355 v
= isl_vec_alloc(ctx
, 1 + 1 + total
);
14358 isl_int_neg(v
->el
[0], bmap
->ineq
[ineq
][1 + v_out
+ pos
]);
14359 isl_seq_cpy(v
->el
+ 1, bmap
->ineq
[ineq
], 1 + total
);
14360 isl_int_set_si(v
->el
[1 + 1 + v_out
+ pos
], 0);
14365 /* Is constraint "c" of "bmap" of the form
14367 * e(...) + c1 - m x >= 0
14371 * -e(...) + c2 + m x >= 0
14373 * where m > 1 and e does not involve any other output variables?
14375 * "v_out" is the offset to the output variables.
14376 * "d" is the position of x among the output variables.
14377 * "v_div" is the offset to the local variables.
14378 * "total" is the total number of variables.
14380 * Since the purpose of this function is to use the constraint
14381 * to express the output variable as an integer division,
14382 * do not allow the constraint to involve any local variables
14383 * that do not have an explicit representation.
14385 static isl_bool
is_potential_div_constraint(__isl_keep isl_basic_map
*bmap
,
14386 isl_int
*c
, int v_out
, int d
, int v_div
, int total
)
14390 if (isl_int_is_zero(c
[1 + v_out
+ d
]))
14391 return isl_bool_false
;
14392 if (isl_int_is_one(c
[1 + v_out
+ d
]))
14393 return isl_bool_false
;
14394 if (isl_int_is_negone(c
[1 + v_out
+ d
]))
14395 return isl_bool_false
;
14396 if (isl_seq_first_non_zero(c
+ 1 + v_out
, d
) != -1)
14397 return isl_bool_false
;
14398 if (isl_seq_first_non_zero(c
+ 1 + v_out
+ d
+ 1,
14399 v_div
- (v_out
+ d
+ 1)) != -1)
14400 return isl_bool_false
;
14401 for (i
= 0; v_div
+ i
< total
; ++i
) {
14402 isl_bool known
, involves
;
14404 if (isl_int_is_zero(c
[1 + v_div
+ i
]))
14406 known
= isl_basic_map_div_is_known(bmap
, i
);
14407 if (known
< 0 || !known
)
14409 involves
= div_involves_vars(bmap
, i
, v_out
, v_div
- v_out
);
14410 if (involves
< 0 || involves
)
14411 return isl_bool_not(involves
);
14413 return isl_bool_true
;
14416 /* Look for a pair of constraints
14418 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
14422 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
14424 * that express that the output dimension x at position "pos"
14425 * is some integer division of an expression in terms of the parameters,
14426 * input dimensions and integer divisions.
14427 * If such a pair can be found, then return the index
14428 * of the upper bound constraint, m x <= e(...) + c1.
14429 * Otherwise, return an index beyond the number of constraints.
14431 * In order for the constraints above to express an integer division,
14432 * m needs to be greater than 1 and such that
14434 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
14436 * In particular, this ensures that
14438 * x = floor((e(...) + c1) / m)
14440 isl_size
isl_basic_map_find_output_upper_div_constraint(
14441 __isl_keep isl_basic_map
*bmap
, int pos
)
14445 isl_size v_out
, v_div
;
14449 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
14450 v_out
= isl_basic_map_var_offset(bmap
, isl_dim_out
);
14451 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
14452 n_ineq
= isl_basic_map_n_inequality(bmap
);
14453 if (total
< 0 || v_out
< 0 || v_div
< 0 || n_ineq
< 0)
14454 return isl_size_error
;
14457 for (i
= 0; i
< n_ineq
; ++i
) {
14458 isl_bool potential
;
14460 potential
= is_potential_div_constraint(bmap
, bmap
->ineq
[i
],
14461 v_out
, pos
, v_div
, total
);
14466 for (j
= i
+ 1; j
< n_ineq
; ++j
) {
14467 if (!isl_seq_is_neg(bmap
->ineq
[i
] + 1,
14468 bmap
->ineq
[j
] + 1, total
))
14470 isl_int_add(sum
, bmap
->ineq
[i
][0], bmap
->ineq
[j
][0]);
14471 if (isl_int_abs_lt(sum
, bmap
->ineq
[i
][1 + v_out
+ pos
]))
14477 isl_int_clear(sum
);
14481 if (isl_int_is_pos(bmap
->ineq
[j
][1 + v_out
+ pos
]))
14486 isl_int_clear(sum
);
14487 return isl_size_error
;
14490 /* Return a copy of the equality constraints of "bset" as a matrix.
14492 __isl_give isl_mat
*isl_basic_set_extract_equalities(
14493 __isl_keep isl_basic_set
*bset
)
14498 total
= isl_basic_set_dim(bset
, isl_dim_all
);
14502 ctx
= isl_basic_set_get_ctx(bset
);
14503 return isl_mat_sub_alloc6(ctx
, bset
->eq
, 0, bset
->n_eq
, 0, 1 + total
);
14506 /* Are the "n" "coefficients" starting at "first" of the integer division
14507 * expressions at position "pos1" in "bmap1" and "pos2" in "bmap2" equal
14509 * The "coefficient" at position 0 is the denominator.
14510 * The "coefficient" at position 1 is the constant term.
14512 isl_bool
isl_basic_map_equal_div_expr_part(__isl_keep isl_basic_map
*bmap1
,
14513 int pos1
, __isl_keep isl_basic_map
*bmap2
, int pos2
,
14514 unsigned first
, unsigned n
)
14516 if (isl_basic_map_check_range(bmap1
, isl_dim_div
, pos1
, 1) < 0)
14517 return isl_bool_error
;
14518 if (isl_basic_map_check_range(bmap2
, isl_dim_div
, pos2
, 1) < 0)
14519 return isl_bool_error
;
14520 return isl_seq_eq(bmap1
->div
[pos1
] + first
,
14521 bmap2
->div
[pos2
] + first
, n
);
14524 /* Are the integer division expressions at position "pos1" in "bmap1" and
14525 * "pos2" in "bmap2" equal to each other, except that the constant terms
14528 isl_bool
isl_basic_map_equal_div_expr_except_constant(
14529 __isl_keep isl_basic_map
*bmap1
, int pos1
,
14530 __isl_keep isl_basic_map
*bmap2
, int pos2
)
14533 isl_size total
, total2
;
14535 total
= isl_basic_map_dim(bmap1
, isl_dim_all
);
14536 total2
= isl_basic_map_dim(bmap2
, isl_dim_all
);
14537 if (total
< 0 || total2
< 0)
14538 return isl_bool_error
;
14539 if (total
!= total2
)
14540 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
14541 "incomparable div expressions", return isl_bool_error
);
14542 equal
= isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
14544 if (equal
< 0 || !equal
)
14546 equal
= isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
14548 if (equal
< 0 || equal
)
14549 return isl_bool_not(equal
);
14550 return isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
14554 /* Replace the numerator of the constant term of the integer division
14555 * expression at position "div" in "bmap" by "value".
14556 * The caller guarantees that this does not change the meaning
14559 __isl_give isl_basic_map
*isl_basic_map_set_div_expr_constant_num_si_inplace(
14560 __isl_take isl_basic_map
*bmap
, int div
, int value
)
14562 if (isl_basic_map_check_range(bmap
, isl_dim_div
, div
, 1) < 0)
14563 return isl_basic_map_free(bmap
);
14565 isl_int_set_si(bmap
->div
[div
][1], value
);
14570 /* Is the point "inner" internal to inequality constraint "ineq"
14572 * The point is considered to be internal to the inequality constraint,
14573 * if it strictly lies on the positive side of the inequality constraint,
14574 * or if it lies on the constraint and the constraint is lexico-positive.
14576 static isl_bool
is_internal(__isl_keep isl_vec
*inner
,
14577 __isl_keep isl_basic_set
*bset
, int ineq
)
14583 if (!inner
|| !bset
)
14584 return isl_bool_error
;
14586 ctx
= isl_basic_set_get_ctx(bset
);
14587 isl_seq_inner_product(inner
->el
, bset
->ineq
[ineq
], inner
->size
,
14588 &ctx
->normalize_gcd
);
14589 if (!isl_int_is_zero(ctx
->normalize_gcd
))
14590 return isl_int_is_nonneg(ctx
->normalize_gcd
);
14592 total
= isl_basic_set_dim(bset
, isl_dim_all
);
14594 return isl_bool_error
;
14595 pos
= isl_seq_first_non_zero(bset
->ineq
[ineq
] + 1, total
);
14596 return isl_int_is_pos(bset
->ineq
[ineq
][1 + pos
]);
14599 /* Tighten the inequality constraints of "bset" that are outward with respect
14600 * to the point "vec".
14601 * That is, tighten the constraints that are not satisfied by "vec".
14603 * "vec" is a point internal to some superset S of "bset" that is used
14604 * to make the subsets of S disjoint, by tightening one half of the constraints
14605 * that separate two subsets. In particular, the constraints of S
14606 * are all satisfied by "vec" and should not be tightened.
14607 * Of the internal constraints, those that have "vec" on the outside
14608 * are tightened. The shared facet is included in the adjacent subset
14609 * with the opposite constraint.
14610 * For constraints that saturate "vec", this criterion cannot be used
14611 * to determine which of the two sides should be tightened.
14612 * Instead, the sign of the first non-zero coefficient is used
14613 * to make this choice. Note that this second criterion is never used
14614 * on the constraints of S since "vec" is interior to "S".
14616 __isl_give isl_basic_set
*isl_basic_set_tighten_outward(
14617 __isl_take isl_basic_set
*bset
, __isl_keep isl_vec
*vec
)
14621 bset
= isl_basic_set_cow(bset
);
14624 for (j
= 0; j
< bset
->n_ineq
; ++j
) {
14627 internal
= is_internal(vec
, bset
, j
);
14629 return isl_basic_set_free(bset
);
14632 isl_int_sub_ui(bset
->ineq
[j
][0], bset
->ineq
[j
][0], 1);
14638 /* Replace the variables x of type "type" starting at "first" in "bmap"
14639 * by x' with x = M x' with M the matrix trans.
14640 * That is, replace the corresponding coefficients c by c M.
14642 * The transformation matrix should be a square matrix.
14644 __isl_give isl_basic_map
*isl_basic_map_transform_dims(
14645 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
, unsigned first
,
14646 __isl_take isl_mat
*trans
)
14650 bmap
= isl_basic_map_cow(bmap
);
14651 if (!bmap
|| !trans
)
14654 if (trans
->n_row
!= trans
->n_col
)
14655 isl_die(trans
->ctx
, isl_error_invalid
,
14656 "expecting square transformation matrix", goto error
);
14657 if (isl_basic_map_check_range(bmap
, type
, first
, trans
->n_row
) < 0)
14660 pos
= isl_basic_map_offset(bmap
, type
) + first
;
14662 if (isl_mat_sub_transform(bmap
->eq
, bmap
->n_eq
, pos
,
14663 isl_mat_copy(trans
)) < 0)
14665 if (isl_mat_sub_transform(bmap
->ineq
, bmap
->n_ineq
, pos
,
14666 isl_mat_copy(trans
)) < 0)
14668 if (isl_mat_sub_transform(bmap
->div
, bmap
->n_div
, 1 + pos
,
14669 isl_mat_copy(trans
)) < 0)
14672 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
14673 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
14675 isl_mat_free(trans
);
14678 isl_mat_free(trans
);
14679 isl_basic_map_free(bmap
);
14683 /* Replace the variables x of type "type" starting at "first" in "bset"
14684 * by x' with x = M x' with M the matrix trans.
14685 * That is, replace the corresponding coefficients c by c M.
14687 * The transformation matrix should be a square matrix.
14689 __isl_give isl_basic_set
*isl_basic_set_transform_dims(
14690 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned first
,
14691 __isl_take isl_mat
*trans
)
14693 return isl_basic_map_transform_dims(bset
, type
, first
, trans
);