2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012-2014 Ecole Normale Superieure
5 * Copyright 2014 INRIA Rocquencourt
6 * Copyright 2016 INRIA Paris
7 * Copyright 2016 Sven Verdoolaege
8 * Copyright 2018-2019 Cerebras Systems
9 * Copyright 2022 Cerebras Systems
11 * Use of this software is governed by the MIT license
13 * Written by Sven Verdoolaege, K.U.Leuven, Departement
14 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
15 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
16 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
17 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
18 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
19 * B.P. 105 - 78153 Le Chesnay, France
20 * and Centre de Recherche Inria de Paris, 2 rue Simone Iff - Voie DQ12,
21 * CS 42112, 75589 Paris Cedex 12, France
22 * and Cerebras Systems, 175 S San Antonio Rd, Los Altos, CA, USA
26 #include <isl_ctx_private.h>
27 #include <isl_map_private.h>
29 #include <isl_id_private.h>
30 #include <isl/constraint.h>
31 #include "isl_space_private.h"
32 #include "isl_equalities.h"
33 #include <isl_lp_private.h>
37 #include <isl_reordering.h>
38 #include "isl_sample.h"
42 #include <isl_mat_private.h>
43 #include <isl_vec_private.h>
44 #include <isl_dim_map.h>
45 #include <isl_local_space_private.h>
46 #include <isl_aff_private.h>
47 #include <isl_options_private.h>
48 #include <isl_morph.h>
49 #include <isl_val_private.h>
50 #include <isl_printer_private.h>
52 #include <bset_to_bmap.c>
53 #include <bset_from_bmap.c>
54 #include <set_to_map.c>
55 #include <set_from_map.c>
57 /* Treat "bset" as a basic map.
58 * Internally, isl_basic_set is defined to isl_basic_map, so in practice,
59 * this function performs a redundant cast.
61 static __isl_keep
const isl_basic_map
*const_bset_to_bmap(
62 __isl_keep
const isl_basic_set
*bset
)
64 return (const isl_basic_map
*) bset
;
68 #define TYPE isl_basic_map
69 #include "has_single_reference_templ.c"
71 static unsigned pos(__isl_keep isl_space
*space
, enum isl_dim_type type
)
74 case isl_dim_param
: return 1;
75 case isl_dim_in
: return 1 + space
->nparam
;
76 case isl_dim_out
: return 1 + space
->nparam
+ space
->n_in
;
81 isl_size
isl_basic_map_dim(__isl_keep isl_basic_map
*bmap
,
82 enum isl_dim_type type
)
85 return isl_size_error
;
87 case isl_dim_cst
: return 1;
90 case isl_dim_out
: return isl_space_dim(bmap
->dim
, type
);
91 case isl_dim_div
: return bmap
->n_div
;
92 case isl_dim_all
: return isl_basic_map_total_dim(bmap
);
97 /* Return the space of "map".
99 __isl_keep isl_space
*isl_map_peek_space(__isl_keep
const isl_map
*map
)
101 return map
? map
->dim
: NULL
;
104 /* Return the space of "set".
106 __isl_keep isl_space
*isl_set_peek_space(__isl_keep isl_set
*set
)
108 return isl_map_peek_space(set_to_map(set
));
111 isl_size
isl_map_dim(__isl_keep isl_map
*map
, enum isl_dim_type type
)
113 return isl_space_dim(isl_map_peek_space(map
), type
);
116 /* Return the dimensionality of the domain (tuple) of the map.
118 isl_size
isl_map_domain_tuple_dim(__isl_keep isl_map
*map
)
120 return isl_map_dim(map
, isl_dim_in
);
123 /* Return the dimensionality of the range (tuple) of the map.
125 isl_size
isl_map_range_tuple_dim(__isl_keep isl_map
*map
)
127 return isl_map_dim(map
, isl_dim_out
);
130 isl_size
isl_set_dim(__isl_keep isl_set
*set
, enum isl_dim_type type
)
132 return isl_map_dim(set_to_map(set
), type
);
135 /* Return the dimensionality of the (tuple of the) set.
137 isl_size
isl_set_tuple_dim(__isl_keep isl_set
*set
)
139 return isl_set_dim(set
, isl_dim_set
);
142 /* Return the position of the variables of the given type
143 * within the sequence of variables of "bmap".
145 isl_size
isl_basic_map_var_offset(__isl_keep isl_basic_map
*bmap
,
146 enum isl_dim_type type
)
150 space
= isl_basic_map_peek_space(bmap
);
155 case isl_dim_out
: return isl_space_offset(space
, type
);
156 case isl_dim_div
: return isl_space_dim(space
, isl_dim_all
);
159 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
160 "invalid dimension type", return isl_size_error
);
164 /* Return the position of the variables of the given type
165 * within the sequence of variables of "bset".
167 isl_size
isl_basic_set_var_offset(__isl_keep isl_basic_set
*bset
,
168 enum isl_dim_type type
)
170 return isl_basic_map_var_offset(bset_to_bmap(bset
), type
);
173 /* Return the position of the coefficients of the variables of the given type
174 * within the sequence of coefficients of "bmap".
176 unsigned isl_basic_map_offset(__isl_keep isl_basic_map
*bmap
,
177 enum isl_dim_type type
)
180 case isl_dim_cst
: return 0;
184 case isl_dim_div
: return 1 + isl_basic_map_var_offset(bmap
, type
);
189 unsigned isl_basic_set_offset(__isl_keep isl_basic_set
*bset
,
190 enum isl_dim_type type
)
192 return isl_basic_map_offset(bset
, type
);
195 static unsigned map_offset(__isl_keep isl_map
*map
, enum isl_dim_type type
)
197 return pos(map
->dim
, type
);
200 isl_size
isl_basic_set_dim(__isl_keep isl_basic_set
*bset
,
201 enum isl_dim_type type
)
203 return isl_basic_map_dim(bset
, type
);
206 isl_size
isl_basic_set_n_dim(__isl_keep isl_basic_set
*bset
)
208 return isl_basic_set_dim(bset
, isl_dim_set
);
211 isl_size
isl_basic_set_n_param(__isl_keep isl_basic_set
*bset
)
213 return isl_basic_set_dim(bset
, isl_dim_param
);
216 isl_size
isl_basic_set_total_dim(__isl_keep
const isl_basic_set
*bset
)
218 return isl_basic_map_total_dim(const_bset_to_bmap(bset
));
221 isl_size
isl_set_n_dim(__isl_keep isl_set
*set
)
223 return isl_set_dim(set
, isl_dim_set
);
226 isl_size
isl_set_n_param(__isl_keep isl_set
*set
)
228 return isl_set_dim(set
, isl_dim_param
);
231 isl_size
isl_basic_map_total_dim(__isl_keep
const isl_basic_map
*bmap
)
236 return isl_size_error
;
237 dim
= isl_space_dim(bmap
->dim
, isl_dim_all
);
239 return isl_size_error
;
240 return dim
+ bmap
->n_div
;
243 /* Return the number of equality constraints in the description of "bmap".
244 * Return isl_size_error on error.
246 isl_size
isl_basic_map_n_equality(__isl_keep isl_basic_map
*bmap
)
249 return isl_size_error
;
253 /* Return the number of equality constraints in the description of "bset".
254 * Return isl_size_error on error.
256 isl_size
isl_basic_set_n_equality(__isl_keep isl_basic_set
*bset
)
258 return isl_basic_map_n_equality(bset_to_bmap(bset
));
261 /* Return the number of inequality constraints in the description of "bmap".
262 * Return isl_size_error on error.
264 isl_size
isl_basic_map_n_inequality(__isl_keep isl_basic_map
*bmap
)
267 return isl_size_error
;
271 /* Return the number of inequality constraints in the description of "bset".
272 * Return isl_size_error on error.
274 isl_size
isl_basic_set_n_inequality(__isl_keep isl_basic_set
*bset
)
276 return isl_basic_map_n_inequality(bset_to_bmap(bset
));
279 /* Do "bmap1" and "bmap2" have the same parameters?
281 static isl_bool
isl_basic_map_has_equal_params(__isl_keep isl_basic_map
*bmap1
,
282 __isl_keep isl_basic_map
*bmap2
)
284 isl_space
*space1
, *space2
;
286 space1
= isl_basic_map_peek_space(bmap1
);
287 space2
= isl_basic_map_peek_space(bmap2
);
288 return isl_space_has_equal_params(space1
, space2
);
291 /* Do "map1" and "map2" have the same parameters?
293 isl_bool
isl_map_has_equal_params(__isl_keep isl_map
*map1
,
294 __isl_keep isl_map
*map2
)
296 isl_space
*space1
, *space2
;
298 space1
= isl_map_peek_space(map1
);
299 space2
= isl_map_peek_space(map2
);
300 return isl_space_has_equal_params(space1
, space2
);
303 /* Do "map" and "set" have the same parameters?
305 static isl_bool
isl_map_set_has_equal_params(__isl_keep isl_map
*map
,
306 __isl_keep isl_set
*set
)
308 return isl_map_has_equal_params(map
, set_to_map(set
));
311 /* Is the tuple of type "type" of "bmap" the same as the single tuple of "bset"?
313 static isl_bool
isl_basic_map_set_tuple_is_equal(__isl_keep isl_basic_map
*bmap
,
314 enum isl_dim_type type
, __isl_keep isl_basic_set
*bset
)
316 isl_space
*bmap_space
, *bset_space
;
318 bmap_space
= isl_basic_map_peek_space(bmap
);
319 bset_space
= isl_basic_set_peek_space(bset
);
320 return isl_space_tuple_is_equal(bmap_space
, type
,
321 bset_space
, isl_dim_set
);
324 /* Is the tuple of type "type" of "map" the same as the single tuple of "set"?
326 static isl_bool
isl_map_set_tuple_is_equal(__isl_keep isl_map
*map
,
327 enum isl_dim_type type
, __isl_keep isl_set
*set
)
329 return isl_map_tuple_is_equal(map
, type
, set_to_map(set
), isl_dim_set
);
332 isl_bool
isl_map_compatible_domain(__isl_keep isl_map
*map
,
333 __isl_keep isl_set
*set
)
337 return isl_bool_error
;
338 m
= isl_map_has_equal_params(map
, set_to_map(set
));
341 return isl_map_set_tuple_is_equal(map
, isl_dim_in
, set
);
344 isl_bool
isl_basic_map_compatible_domain(__isl_keep isl_basic_map
*bmap
,
345 __isl_keep isl_basic_set
*bset
)
349 return isl_bool_error
;
350 m
= isl_basic_map_has_equal_params(bmap
, bset_to_bmap(bset
));
353 return isl_basic_map_set_tuple_is_equal(bmap
, isl_dim_in
, bset
);
356 isl_bool
isl_map_compatible_range(__isl_keep isl_map
*map
,
357 __isl_keep isl_set
*set
)
361 return isl_bool_error
;
362 m
= isl_map_has_equal_params(map
, set_to_map(set
));
365 return isl_map_set_tuple_is_equal(map
, isl_dim_out
, set
);
368 isl_bool
isl_basic_map_compatible_range(__isl_keep isl_basic_map
*bmap
,
369 __isl_keep isl_basic_set
*bset
)
373 return isl_bool_error
;
374 m
= isl_basic_map_has_equal_params(bmap
, bset_to_bmap(bset
));
377 return isl_basic_map_set_tuple_is_equal(bmap
, isl_dim_out
, bset
);
380 isl_ctx
*isl_basic_map_get_ctx(__isl_keep isl_basic_map
*bmap
)
382 return bmap
? bmap
->ctx
: NULL
;
385 isl_ctx
*isl_basic_set_get_ctx(__isl_keep isl_basic_set
*bset
)
387 return bset
? bset
->ctx
: NULL
;
390 isl_ctx
*isl_map_get_ctx(__isl_keep isl_map
*map
)
392 return map
? map
->ctx
: NULL
;
395 isl_ctx
*isl_set_get_ctx(__isl_keep isl_set
*set
)
397 return set
? set
->ctx
: NULL
;
400 /* Return the space of "bmap".
402 __isl_keep isl_space
*isl_basic_map_peek_space(
403 __isl_keep
const isl_basic_map
*bmap
)
405 return bmap
? bmap
->dim
: NULL
;
408 /* Return the space of "bset".
410 __isl_keep isl_space
*isl_basic_set_peek_space(__isl_keep isl_basic_set
*bset
)
412 return isl_basic_map_peek_space(bset_to_bmap(bset
));
415 __isl_give isl_space
*isl_basic_map_get_space(__isl_keep isl_basic_map
*bmap
)
417 return isl_space_copy(isl_basic_map_peek_space(bmap
));
420 __isl_give isl_space
*isl_basic_set_get_space(__isl_keep isl_basic_set
*bset
)
422 return isl_basic_map_get_space(bset_to_bmap(bset
));
425 /* Return the space of "bmap".
426 * This may be either a copy or the space itself
427 * if there is only one reference to "bmap".
428 * This allows the space to be modified inplace
429 * if both the basic map and its space have only a single reference.
430 * The caller is not allowed to modify "bmap" between this call and
431 * a subsequent call to isl_basic_map_restore_space.
432 * The only exception is that isl_basic_map_free can be called instead.
434 static __isl_give isl_space
*isl_basic_map_take_space(
435 __isl_keep isl_basic_map
*bmap
)
442 return isl_basic_map_get_space(bmap
);
448 /* Set the space of "bmap" to "space", where the space of "bmap" may be missing
449 * due to a preceding call to isl_basic_map_take_space.
450 * However, in this case, "bmap" only has a single reference and
451 * then the call to isl_basic_map_cow has no effect.
453 static __isl_give isl_basic_map
*isl_basic_map_restore_space(
454 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*space
)
459 if (bmap
->dim
== space
) {
460 isl_space_free(space
);
464 bmap
= isl_basic_map_cow(bmap
);
467 isl_space_free(bmap
->dim
);
472 isl_basic_map_free(bmap
);
473 isl_space_free(space
);
477 /* Extract the divs in "bmap" as a matrix.
479 __isl_give isl_mat
*isl_basic_map_get_divs(__isl_keep isl_basic_map
*bmap
)
487 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
491 ctx
= isl_basic_map_get_ctx(bmap
);
492 cols
= 1 + 1 + v_div
+ bmap
->n_div
;
493 div
= isl_mat_alloc(ctx
, bmap
->n_div
, cols
);
497 for (i
= 0; i
< bmap
->n_div
; ++i
)
498 isl_seq_cpy(div
->row
[i
], bmap
->div
[i
], cols
);
503 /* Extract the divs in "bset" as a matrix.
505 __isl_give isl_mat
*isl_basic_set_get_divs(__isl_keep isl_basic_set
*bset
)
507 return isl_basic_map_get_divs(bset
);
510 __isl_give isl_local_space
*isl_basic_map_get_local_space(
511 __isl_keep isl_basic_map
*bmap
)
518 div
= isl_basic_map_get_divs(bmap
);
519 return isl_local_space_alloc_div(isl_space_copy(bmap
->dim
), div
);
522 __isl_give isl_local_space
*isl_basic_set_get_local_space(
523 __isl_keep isl_basic_set
*bset
)
525 return isl_basic_map_get_local_space(bset
);
528 /* For each known div d = floor(f/m), add the constraints
531 * -(f-(m-1)) + m d >= 0
533 * Do not finalize the result.
535 static __isl_give isl_basic_map
*add_known_div_constraints(
536 __isl_take isl_basic_map
*bmap
)
541 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
543 return isl_basic_map_free(bmap
);
546 bmap
= isl_basic_map_cow(bmap
);
547 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 2 * n_div
);
550 for (i
= 0; i
< n_div
; ++i
) {
551 if (isl_int_is_zero(bmap
->div
[i
][0]))
553 bmap
= isl_basic_map_add_div_constraints(bmap
, i
);
559 __isl_give isl_basic_map
*isl_basic_map_from_local_space(
560 __isl_take isl_local_space
*ls
)
566 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
568 ls
= isl_local_space_free(ls
);
572 bmap
= isl_basic_map_alloc_space(isl_local_space_get_space(ls
),
573 n_div
, 0, 2 * n_div
);
575 for (i
= 0; i
< n_div
; ++i
)
576 if (isl_basic_map_alloc_div(bmap
) < 0)
579 for (i
= 0; i
< n_div
; ++i
)
580 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], ls
->div
->n_col
);
581 bmap
= add_known_div_constraints(bmap
);
583 isl_local_space_free(ls
);
586 isl_local_space_free(ls
);
587 isl_basic_map_free(bmap
);
591 __isl_give isl_basic_set
*isl_basic_set_from_local_space(
592 __isl_take isl_local_space
*ls
)
594 return isl_basic_map_from_local_space(ls
);
597 __isl_give isl_space
*isl_map_get_space(__isl_keep isl_map
*map
)
599 return isl_space_copy(isl_map_peek_space(map
));
602 __isl_give isl_space
*isl_set_get_space(__isl_keep isl_set
*set
)
606 return isl_space_copy(set
->dim
);
609 /* Return the space of "map".
610 * This may be either a copy or the space itself
611 * if there is only one reference to "map".
612 * This allows the space to be modified inplace
613 * if both the map and its space have only a single reference.
614 * The caller is not allowed to modify "map" between this call and
615 * a subsequent call to isl_map_restore_space.
616 * The only exception is that isl_map_free can be called instead.
618 static __isl_give isl_space
*isl_map_take_space(__isl_keep isl_map
*map
)
625 return isl_map_get_space(map
);
631 /* Set the space of "map" to "space", where the space of "map" may be missing
632 * due to a preceding call to isl_map_take_space.
633 * However, in this case, "map" only has a single reference and
634 * then the call to isl_map_cow has no effect.
636 static __isl_give isl_map
*isl_map_restore_space(__isl_take isl_map
*map
,
637 __isl_take isl_space
*space
)
642 if (map
->dim
== space
) {
643 isl_space_free(space
);
647 map
= isl_map_cow(map
);
650 isl_space_free(map
->dim
);
656 isl_space_free(space
);
660 __isl_give isl_basic_map
*isl_basic_map_set_tuple_name(
661 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
, const char *s
)
665 space
= isl_basic_map_take_space(bmap
);
666 space
= isl_space_set_tuple_name(space
, type
, s
);
667 bmap
= isl_basic_map_restore_space(bmap
, space
);
668 bmap
= isl_basic_map_finalize(bmap
);
672 __isl_give isl_basic_set
*isl_basic_set_set_tuple_name(
673 __isl_take isl_basic_set
*bset
, const char *s
)
675 return isl_basic_map_set_tuple_name(bset
, isl_dim_set
, s
);
678 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map
*bmap
,
679 enum isl_dim_type type
)
681 return bmap
? isl_space_get_tuple_name(bmap
->dim
, type
) : NULL
;
684 __isl_give isl_map
*isl_map_set_tuple_name(__isl_take isl_map
*map
,
685 enum isl_dim_type type
, const char *s
)
690 map
= isl_map_cow(map
);
694 for (i
= 0; i
< map
->n
; ++i
) {
695 map
->p
[i
] = isl_basic_map_set_tuple_name(map
->p
[i
], type
, s
);
700 space
= isl_map_take_space(map
);
701 space
= isl_space_set_tuple_name(space
, type
, s
);
702 map
= isl_map_restore_space(map
, space
);
710 /* Replace the identifier of the tuple of type "type" by "id".
712 __isl_give isl_basic_map
*isl_basic_map_set_tuple_id(
713 __isl_take isl_basic_map
*bmap
,
714 enum isl_dim_type type
, __isl_take isl_id
*id
)
718 space
= isl_basic_map_take_space(bmap
);
719 space
= isl_space_set_tuple_id(space
, type
, id
);
720 bmap
= isl_basic_map_restore_space(bmap
, space
);
721 bmap
= isl_basic_map_finalize(bmap
);
725 /* Replace the identifier of the tuple by "id".
727 __isl_give isl_basic_set
*isl_basic_set_set_tuple_id(
728 __isl_take isl_basic_set
*bset
, __isl_take isl_id
*id
)
730 return isl_basic_map_set_tuple_id(bset
, isl_dim_set
, id
);
733 /* Does the input or output tuple have a name?
735 isl_bool
isl_map_has_tuple_name(__isl_keep isl_map
*map
, enum isl_dim_type type
)
737 return map
? isl_space_has_tuple_name(map
->dim
, type
) : isl_bool_error
;
740 const char *isl_map_get_tuple_name(__isl_keep isl_map
*map
,
741 enum isl_dim_type type
)
743 return map
? isl_space_get_tuple_name(map
->dim
, type
) : NULL
;
746 __isl_give isl_set
*isl_set_set_tuple_name(__isl_take isl_set
*set
,
749 return set_from_map(isl_map_set_tuple_name(set_to_map(set
),
753 __isl_give isl_map
*isl_map_set_tuple_id(__isl_take isl_map
*map
,
754 enum isl_dim_type type
, __isl_take isl_id
*id
)
758 space
= isl_map_take_space(map
);
759 space
= isl_space_set_tuple_id(space
, type
, id
);
760 map
= isl_map_restore_space(map
, space
);
762 return isl_map_reset_space(map
, isl_map_get_space(map
));
765 /* Replace the identifier of the domain tuple of "map" by "id".
767 __isl_give isl_map
*isl_map_set_domain_tuple_id(__isl_take isl_map
*map
,
768 __isl_take isl_id
*id
)
770 return isl_map_set_tuple_id(map
, isl_dim_in
, id
);
773 /* Replace the identifier of the range tuple of "map" by "id".
775 __isl_give isl_map
*isl_map_set_range_tuple_id(__isl_take isl_map
*map
,
776 __isl_take isl_id
*id
)
778 return isl_map_set_tuple_id(map
, isl_dim_out
, id
);
781 __isl_give isl_set
*isl_set_set_tuple_id(__isl_take isl_set
*set
,
782 __isl_take isl_id
*id
)
784 return isl_map_set_tuple_id(set
, isl_dim_set
, id
);
787 __isl_give isl_map
*isl_map_reset_tuple_id(__isl_take isl_map
*map
,
788 enum isl_dim_type type
)
792 space
= isl_map_take_space(map
);
793 space
= isl_space_reset_tuple_id(space
, type
);
794 map
= isl_map_restore_space(map
, space
);
796 return isl_map_reset_space(map
, isl_map_get_space(map
));
799 __isl_give isl_set
*isl_set_reset_tuple_id(__isl_take isl_set
*set
)
801 return isl_map_reset_tuple_id(set
, isl_dim_set
);
804 isl_bool
isl_map_has_tuple_id(__isl_keep isl_map
*map
, enum isl_dim_type type
)
806 return map
? isl_space_has_tuple_id(map
->dim
, type
) : isl_bool_error
;
809 /* Does the domain tuple of "map" have an identifier?
811 isl_bool
isl_map_has_domain_tuple_id(__isl_keep isl_map
*map
)
813 return isl_map_has_tuple_id(map
, isl_dim_in
);
816 /* Does the range tuple of "map" have an identifier?
818 isl_bool
isl_map_has_range_tuple_id(__isl_keep isl_map
*map
)
820 return isl_map_has_tuple_id(map
, isl_dim_out
);
823 __isl_give isl_id
*isl_map_get_tuple_id(__isl_keep isl_map
*map
,
824 enum isl_dim_type type
)
826 return map
? isl_space_get_tuple_id(map
->dim
, type
) : NULL
;
829 /* Return the identifier of the domain tuple of "map", assuming it has one.
831 __isl_give isl_id
*isl_map_get_domain_tuple_id(__isl_keep isl_map
*map
)
833 return isl_map_get_tuple_id(map
, isl_dim_in
);
836 /* Return the identifier of the range tuple of "map", assuming it has one.
838 __isl_give isl_id
*isl_map_get_range_tuple_id(__isl_keep isl_map
*map
)
840 return isl_map_get_tuple_id(map
, isl_dim_out
);
843 isl_bool
isl_set_has_tuple_id(__isl_keep isl_set
*set
)
845 return isl_map_has_tuple_id(set
, isl_dim_set
);
848 __isl_give isl_id
*isl_set_get_tuple_id(__isl_keep isl_set
*set
)
850 return isl_map_get_tuple_id(set
, isl_dim_set
);
853 /* Does the set tuple have a name?
855 isl_bool
isl_set_has_tuple_name(__isl_keep isl_set
*set
)
858 return isl_bool_error
;
859 return isl_space_has_tuple_name(set
->dim
, isl_dim_set
);
863 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set
*bset
)
865 return bset
? isl_space_get_tuple_name(bset
->dim
, isl_dim_set
) : NULL
;
868 const char *isl_set_get_tuple_name(__isl_keep isl_set
*set
)
870 return set
? isl_space_get_tuple_name(set
->dim
, isl_dim_set
) : NULL
;
873 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map
*bmap
,
874 enum isl_dim_type type
, unsigned pos
)
876 return bmap
? isl_space_get_dim_name(bmap
->dim
, type
, pos
) : NULL
;
879 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set
*bset
,
880 enum isl_dim_type type
, unsigned pos
)
882 return bset
? isl_space_get_dim_name(bset
->dim
, type
, pos
) : NULL
;
885 /* Does the given dimension have a name?
887 isl_bool
isl_map_has_dim_name(__isl_keep isl_map
*map
,
888 enum isl_dim_type type
, unsigned pos
)
891 return isl_bool_error
;
892 return isl_space_has_dim_name(map
->dim
, type
, pos
);
895 const char *isl_map_get_dim_name(__isl_keep isl_map
*map
,
896 enum isl_dim_type type
, unsigned pos
)
898 return map
? isl_space_get_dim_name(map
->dim
, type
, pos
) : NULL
;
901 const char *isl_set_get_dim_name(__isl_keep isl_set
*set
,
902 enum isl_dim_type type
, unsigned pos
)
904 return set
? isl_space_get_dim_name(set
->dim
, type
, pos
) : NULL
;
907 /* Does the given dimension have a name?
909 isl_bool
isl_set_has_dim_name(__isl_keep isl_set
*set
,
910 enum isl_dim_type type
, unsigned pos
)
913 return isl_bool_error
;
914 return isl_space_has_dim_name(set
->dim
, type
, pos
);
917 __isl_give isl_basic_map
*isl_basic_map_set_dim_name(
918 __isl_take isl_basic_map
*bmap
,
919 enum isl_dim_type type
, unsigned pos
, const char *s
)
923 space
= isl_basic_map_take_space(bmap
);
924 space
= isl_space_set_dim_name(space
, type
, pos
, s
);
925 bmap
= isl_basic_map_restore_space(bmap
, space
);
926 return isl_basic_map_finalize(bmap
);
929 __isl_give isl_map
*isl_map_set_dim_name(__isl_take isl_map
*map
,
930 enum isl_dim_type type
, unsigned pos
, const char *s
)
935 map
= isl_map_cow(map
);
939 for (i
= 0; i
< map
->n
; ++i
) {
940 map
->p
[i
] = isl_basic_map_set_dim_name(map
->p
[i
], type
, pos
, s
);
945 space
= isl_map_take_space(map
);
946 space
= isl_space_set_dim_name(space
, type
, pos
, s
);
947 map
= isl_map_restore_space(map
, space
);
955 __isl_give isl_basic_set
*isl_basic_set_set_dim_name(
956 __isl_take isl_basic_set
*bset
,
957 enum isl_dim_type type
, unsigned pos
, const char *s
)
959 return bset_from_bmap(isl_basic_map_set_dim_name(bset_to_bmap(bset
),
963 __isl_give isl_set
*isl_set_set_dim_name(__isl_take isl_set
*set
,
964 enum isl_dim_type type
, unsigned pos
, const char *s
)
966 return set_from_map(isl_map_set_dim_name(set_to_map(set
),
970 isl_bool
isl_basic_map_has_dim_id(__isl_keep isl_basic_map
*bmap
,
971 enum isl_dim_type type
, unsigned pos
)
974 return isl_bool_error
;
975 return isl_space_has_dim_id(bmap
->dim
, type
, pos
);
978 __isl_give isl_id
*isl_basic_set_get_dim_id(__isl_keep isl_basic_set
*bset
,
979 enum isl_dim_type type
, unsigned pos
)
981 return bset
? isl_space_get_dim_id(bset
->dim
, type
, pos
) : NULL
;
984 isl_bool
isl_map_has_dim_id(__isl_keep isl_map
*map
,
985 enum isl_dim_type type
, unsigned pos
)
987 return map
? isl_space_has_dim_id(map
->dim
, type
, pos
) : isl_bool_error
;
990 __isl_give isl_id
*isl_map_get_dim_id(__isl_keep isl_map
*map
,
991 enum isl_dim_type type
, unsigned pos
)
993 return map
? isl_space_get_dim_id(map
->dim
, type
, pos
) : NULL
;
996 isl_bool
isl_set_has_dim_id(__isl_keep isl_set
*set
,
997 enum isl_dim_type type
, unsigned pos
)
999 return isl_map_has_dim_id(set
, type
, pos
);
1002 __isl_give isl_id
*isl_set_get_dim_id(__isl_keep isl_set
*set
,
1003 enum isl_dim_type type
, unsigned pos
)
1005 return isl_map_get_dim_id(set
, type
, pos
);
1008 __isl_give isl_map
*isl_map_set_dim_id(__isl_take isl_map
*map
,
1009 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
1013 space
= isl_map_take_space(map
);
1014 space
= isl_space_set_dim_id(space
, type
, pos
, id
);
1015 map
= isl_map_restore_space(map
, space
);
1017 return isl_map_reset_space(map
, isl_map_get_space(map
));
1020 __isl_give isl_set
*isl_set_set_dim_id(__isl_take isl_set
*set
,
1021 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
1023 return isl_map_set_dim_id(set
, type
, pos
, id
);
1026 int isl_map_find_dim_by_id(__isl_keep isl_map
*map
, enum isl_dim_type type
,
1027 __isl_keep isl_id
*id
)
1031 return isl_space_find_dim_by_id(map
->dim
, type
, id
);
1034 int isl_set_find_dim_by_id(__isl_keep isl_set
*set
, enum isl_dim_type type
,
1035 __isl_keep isl_id
*id
)
1037 return isl_map_find_dim_by_id(set
, type
, id
);
1040 /* Return the position of the dimension of the given type and name
1042 * Return -1 if no such dimension can be found.
1044 int isl_basic_map_find_dim_by_name(__isl_keep isl_basic_map
*bmap
,
1045 enum isl_dim_type type
, const char *name
)
1049 return isl_space_find_dim_by_name(bmap
->dim
, type
, name
);
1052 int isl_map_find_dim_by_name(__isl_keep isl_map
*map
, enum isl_dim_type type
,
1057 return isl_space_find_dim_by_name(map
->dim
, type
, name
);
1060 int isl_set_find_dim_by_name(__isl_keep isl_set
*set
, enum isl_dim_type type
,
1063 return isl_map_find_dim_by_name(set
, type
, name
);
1066 /* Check whether equality i of bset is a pure stride constraint
1067 * on a single dimension, i.e., of the form
1071 * with k a constant and e an existentially quantified variable.
1073 isl_bool
isl_basic_set_eq_is_stride(__isl_keep isl_basic_set
*bset
, int i
)
1081 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
1082 d
= isl_basic_set_dim(bset
, isl_dim_set
);
1083 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
1084 if (nparam
< 0 || d
< 0 || n_div
< 0)
1085 return isl_bool_error
;
1087 if (!isl_int_is_zero(bset
->eq
[i
][0]))
1088 return isl_bool_false
;
1090 if (isl_seq_first_non_zero(bset
->eq
[i
] + 1, nparam
) != -1)
1091 return isl_bool_false
;
1092 pos1
= isl_seq_first_non_zero(bset
->eq
[i
] + 1 + nparam
, d
);
1094 return isl_bool_false
;
1095 if (isl_seq_first_non_zero(bset
->eq
[i
] + 1 + nparam
+ pos1
+ 1,
1096 d
- pos1
- 1) != -1)
1097 return isl_bool_false
;
1099 pos2
= isl_seq_first_non_zero(bset
->eq
[i
] + 1 + nparam
+ d
, n_div
);
1101 return isl_bool_false
;
1102 if (isl_seq_first_non_zero(bset
->eq
[i
] + 1 + nparam
+ d
+ pos2
+ 1,
1103 n_div
- pos2
- 1) != -1)
1104 return isl_bool_false
;
1105 if (!isl_int_is_one(bset
->eq
[i
][1 + nparam
+ pos1
]) &&
1106 !isl_int_is_negone(bset
->eq
[i
][1 + nparam
+ pos1
]))
1107 return isl_bool_false
;
1109 return isl_bool_true
;
1112 /* Reset the user pointer on all identifiers of parameters and tuples
1113 * of the space of "map".
1115 __isl_give isl_map
*isl_map_reset_user(__isl_take isl_map
*map
)
1119 space
= isl_map_get_space(map
);
1120 space
= isl_space_reset_user(space
);
1121 map
= isl_map_reset_space(map
, space
);
1126 /* Reset the user pointer on all identifiers of parameters and tuples
1127 * of the space of "set".
1129 __isl_give isl_set
*isl_set_reset_user(__isl_take isl_set
*set
)
1131 return isl_map_reset_user(set
);
1134 isl_bool
isl_basic_map_is_rational(__isl_keep isl_basic_map
*bmap
)
1137 return isl_bool_error
;
1138 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
1141 /* Has "map" been marked as a rational map?
1142 * In particular, have all basic maps in "map" been marked this way?
1143 * An empty map is not considered to be rational.
1144 * Maps where only some of the basic maps are marked rational
1147 isl_bool
isl_map_is_rational(__isl_keep isl_map
*map
)
1153 return isl_bool_error
;
1155 return isl_bool_false
;
1156 rational
= isl_basic_map_is_rational(map
->p
[0]);
1159 for (i
= 1; i
< map
->n
; ++i
) {
1160 isl_bool rational_i
;
1162 rational_i
= isl_basic_map_is_rational(map
->p
[i
]);
1165 if (rational
!= rational_i
)
1166 isl_die(isl_map_get_ctx(map
), isl_error_unsupported
,
1167 "mixed rational and integer basic maps "
1168 "not supported", return isl_bool_error
);
1174 /* Has "set" been marked as a rational set?
1175 * In particular, have all basic set in "set" been marked this way?
1176 * An empty set is not considered to be rational.
1177 * Sets where only some of the basic sets are marked rational
1180 isl_bool
isl_set_is_rational(__isl_keep isl_set
*set
)
1182 return isl_map_is_rational(set
);
1185 int isl_basic_set_is_rational(__isl_keep isl_basic_set
*bset
)
1187 return isl_basic_map_is_rational(bset
);
1190 /* Does "bmap" contain any rational points?
1192 * If "bmap" has an equality for each dimension, equating the dimension
1193 * to an integer constant, then it has no rational points, even if it
1194 * is marked as rational.
1196 isl_bool
isl_basic_map_has_rational(__isl_keep isl_basic_map
*bmap
)
1198 isl_bool has_rational
= isl_bool_true
;
1202 return isl_bool_error
;
1203 if (isl_basic_map_plain_is_empty(bmap
))
1204 return isl_bool_false
;
1205 if (!isl_basic_map_is_rational(bmap
))
1206 return isl_bool_false
;
1207 bmap
= isl_basic_map_copy(bmap
);
1208 bmap
= isl_basic_map_implicit_equalities(bmap
);
1209 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
1211 return isl_bool_error
;
1212 if (bmap
->n_eq
== total
) {
1214 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
1215 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1, total
);
1218 if (!isl_int_is_one(bmap
->eq
[i
][1 + j
]) &&
1219 !isl_int_is_negone(bmap
->eq
[i
][1 + j
]))
1221 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + j
+ 1,
1226 if (i
== bmap
->n_eq
)
1227 has_rational
= isl_bool_false
;
1229 isl_basic_map_free(bmap
);
1231 return has_rational
;
1234 /* Does "map" contain any rational points?
1236 isl_bool
isl_map_has_rational(__isl_keep isl_map
*map
)
1239 isl_bool has_rational
;
1242 return isl_bool_error
;
1243 for (i
= 0; i
< map
->n
; ++i
) {
1244 has_rational
= isl_basic_map_has_rational(map
->p
[i
]);
1245 if (has_rational
< 0 || has_rational
)
1246 return has_rational
;
1248 return isl_bool_false
;
1251 /* Does "set" contain any rational points?
1253 isl_bool
isl_set_has_rational(__isl_keep isl_set
*set
)
1255 return isl_map_has_rational(set
);
1258 /* Is this basic set a parameter domain?
1260 isl_bool
isl_basic_set_is_params(__isl_keep isl_basic_set
*bset
)
1263 return isl_bool_error
;
1264 return isl_space_is_params(bset
->dim
);
1267 /* Is this set a parameter domain?
1269 isl_bool
isl_set_is_params(__isl_keep isl_set
*set
)
1272 return isl_bool_error
;
1273 return isl_space_is_params(set
->dim
);
1276 /* Is this map actually a parameter domain?
1277 * Users should never call this function. Outside of isl,
1278 * a map can never be a parameter domain.
1280 isl_bool
isl_map_is_params(__isl_keep isl_map
*map
)
1283 return isl_bool_error
;
1284 return isl_space_is_params(map
->dim
);
1287 static __isl_give isl_basic_map
*basic_map_init(isl_ctx
*ctx
,
1288 __isl_take isl_basic_map
*bmap
, unsigned extra
,
1289 unsigned n_eq
, unsigned n_ineq
)
1292 isl_space
*space
= isl_basic_map_peek_space(bmap
);
1293 isl_size n_var
= isl_space_dim(space
, isl_dim_all
);
1294 size_t row_size
= 1 + n_var
+ extra
;
1300 return isl_basic_map_free(bmap
);
1302 bmap
->block
= isl_blk_alloc(ctx
, (n_ineq
+ n_eq
) * row_size
);
1303 if (isl_blk_is_error(bmap
->block
))
1306 bmap
->ineq
= isl_alloc_array(ctx
, isl_int
*, n_ineq
+ n_eq
);
1307 if ((n_ineq
+ n_eq
) && !bmap
->ineq
)
1311 bmap
->block2
= isl_blk_empty();
1314 bmap
->block2
= isl_blk_alloc(ctx
, extra
* (1 + row_size
));
1315 if (isl_blk_is_error(bmap
->block2
))
1318 bmap
->div
= isl_alloc_array(ctx
, isl_int
*, extra
);
1323 for (i
= 0; i
< n_ineq
+ n_eq
; ++i
)
1324 bmap
->ineq
[i
] = bmap
->block
.data
+ i
* row_size
;
1326 for (i
= 0; i
< extra
; ++i
)
1327 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + row_size
);
1331 bmap
->c_size
= n_eq
+ n_ineq
;
1332 bmap
->eq
= bmap
->ineq
+ n_ineq
;
1333 bmap
->extra
= extra
;
1337 bmap
->sample
= NULL
;
1341 isl_basic_map_free(bmap
);
1345 __isl_give isl_basic_set
*isl_basic_set_alloc(isl_ctx
*ctx
,
1346 unsigned nparam
, unsigned dim
, unsigned extra
,
1347 unsigned n_eq
, unsigned n_ineq
)
1349 struct isl_basic_map
*bmap
;
1352 space
= isl_space_set_alloc(ctx
, nparam
, dim
);
1356 bmap
= isl_basic_map_alloc_space(space
, extra
, n_eq
, n_ineq
);
1357 return bset_from_bmap(bmap
);
1360 __isl_give isl_basic_set
*isl_basic_set_alloc_space(__isl_take isl_space
*space
,
1361 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
1363 struct isl_basic_map
*bmap
;
1366 isl_assert(space
->ctx
, space
->n_in
== 0, goto error
);
1367 bmap
= isl_basic_map_alloc_space(space
, extra
, n_eq
, n_ineq
);
1368 return bset_from_bmap(bmap
);
1370 isl_space_free(space
);
1374 __isl_give isl_basic_map
*isl_basic_map_alloc_space(__isl_take isl_space
*space
,
1375 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
1377 struct isl_basic_map
*bmap
;
1381 bmap
= isl_calloc_type(space
->ctx
, struct isl_basic_map
);
1386 return basic_map_init(space
->ctx
, bmap
, extra
, n_eq
, n_ineq
);
1388 isl_space_free(space
);
1392 __isl_give isl_basic_map
*isl_basic_map_alloc(isl_ctx
*ctx
,
1393 unsigned nparam
, unsigned in
, unsigned out
, unsigned extra
,
1394 unsigned n_eq
, unsigned n_ineq
)
1396 struct isl_basic_map
*bmap
;
1399 space
= isl_space_alloc(ctx
, nparam
, in
, out
);
1403 bmap
= isl_basic_map_alloc_space(space
, extra
, n_eq
, n_ineq
);
1407 static __isl_give isl_basic_map
*dup_constraints(__isl_take isl_basic_map
*dst
,
1408 __isl_keep isl_basic_map
*src
)
1411 isl_size total
= isl_basic_map_dim(src
, isl_dim_all
);
1413 if (!dst
|| total
< 0)
1414 return isl_basic_map_free(dst
);
1416 for (i
= 0; i
< src
->n_eq
; ++i
) {
1417 int j
= isl_basic_map_alloc_equality(dst
);
1419 return isl_basic_map_free(dst
);
1420 isl_seq_cpy(dst
->eq
[j
], src
->eq
[i
], 1+total
);
1423 for (i
= 0; i
< src
->n_ineq
; ++i
) {
1424 int j
= isl_basic_map_alloc_inequality(dst
);
1426 return isl_basic_map_free(dst
);
1427 isl_seq_cpy(dst
->ineq
[j
], src
->ineq
[i
], 1+total
);
1430 for (i
= 0; i
< src
->n_div
; ++i
) {
1431 int j
= isl_basic_map_alloc_div(dst
);
1433 return isl_basic_map_free(dst
);
1434 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
);
1436 ISL_F_SET(dst
, ISL_BASIC_SET_FINAL
);
1440 __isl_give isl_basic_map
*isl_basic_map_dup(__isl_keep isl_basic_map
*bmap
)
1442 struct isl_basic_map
*dup
;
1446 dup
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
1447 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
1448 dup
= dup_constraints(dup
, bmap
);
1451 dup
->flags
= bmap
->flags
;
1452 dup
->sample
= isl_vec_copy(bmap
->sample
);
1456 __isl_give isl_basic_set
*isl_basic_set_dup(__isl_keep isl_basic_set
*bset
)
1458 struct isl_basic_map
*dup
;
1460 dup
= isl_basic_map_dup(bset_to_bmap(bset
));
1461 return bset_from_bmap(dup
);
1464 __isl_give isl_basic_set
*isl_basic_set_copy(__isl_keep isl_basic_set
*bset
)
1466 return bset_from_bmap(isl_basic_map_copy(bset_to_bmap(bset
)));
1469 __isl_give isl_set
*isl_set_copy(__isl_keep isl_set
*set
)
1478 __isl_give isl_basic_map
*isl_basic_map_copy(__isl_keep isl_basic_map
*bmap
)
1483 if (ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
)) {
1487 bmap
= isl_basic_map_dup(bmap
);
1489 ISL_F_SET(bmap
, ISL_BASIC_SET_FINAL
);
1493 __isl_give isl_map
*isl_map_copy(__isl_keep isl_map
*map
)
1502 __isl_null isl_basic_map
*isl_basic_map_free(__isl_take isl_basic_map
*bmap
)
1507 if (--bmap
->ref
> 0)
1510 isl_ctx_deref(bmap
->ctx
);
1512 isl_blk_free(bmap
->ctx
, bmap
->block2
);
1514 isl_blk_free(bmap
->ctx
, bmap
->block
);
1515 isl_vec_free(bmap
->sample
);
1516 isl_space_free(bmap
->dim
);
1522 __isl_null isl_basic_set
*isl_basic_set_free(__isl_take isl_basic_set
*bset
)
1524 return isl_basic_map_free(bset_to_bmap(bset
));
1527 static int room_for_con(__isl_keep isl_basic_map
*bmap
, unsigned n
)
1529 return bmap
->n_eq
+ bmap
->n_ineq
+ n
<= bmap
->c_size
;
1532 /* Check that "bset" does not involve any parameters.
1534 isl_stat
isl_basic_set_check_no_params(__isl_keep isl_basic_set
*bset
)
1538 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
1540 return isl_stat_error
;
1542 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
1543 "basic set should not have any parameters",
1544 return isl_stat_error
);
1548 /* Check that "bset" does not involve any local variables.
1550 isl_stat
isl_basic_set_check_no_locals(__isl_keep isl_basic_set
*bset
)
1554 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
1556 return isl_stat_error
;
1558 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
1559 "basic set should not have any local variables",
1560 return isl_stat_error
);
1565 #define TYPE isl_map
1567 #include "isl_check_named_params_templ.c"
1570 #define TYPE isl_basic_map
1573 #include "isl_check_named_params_templ.c"
1575 /* Check that "bmap1" and "bmap2" have the same parameters,
1576 * reporting an error if they do not.
1578 static isl_stat
isl_basic_map_check_equal_params(
1579 __isl_keep isl_basic_map
*bmap1
, __isl_keep isl_basic_map
*bmap2
)
1583 match
= isl_basic_map_has_equal_params(bmap1
, bmap2
);
1585 return isl_stat_error
;
1587 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
1588 "parameters don't match", return isl_stat_error
);
1593 #define TYPE isl_map
1595 #include "isl_align_params_bin_templ.c"
1600 #define ARG1 isl_map
1602 #define ARG2 isl_set
1604 #include "isl_align_params_templ.c"
1606 isl_bool
isl_map_align_params_map_map_and_test(__isl_keep isl_map
*map1
,
1607 __isl_keep isl_map
*map2
,
1608 isl_bool (*fn
)(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
))
1613 return isl_bool_error
;
1614 if (isl_map_has_equal_params(map1
, map2
))
1615 return fn(map1
, map2
);
1616 if (isl_map_check_named_params(map1
) < 0)
1617 return isl_bool_error
;
1618 if (isl_map_check_named_params(map2
) < 0)
1619 return isl_bool_error
;
1620 map1
= isl_map_copy(map1
);
1621 map2
= isl_map_copy(map2
);
1622 map1
= isl_map_align_params(map1
, isl_map_get_space(map2
));
1623 map2
= isl_map_align_params(map2
, isl_map_get_space(map1
));
1630 int isl_basic_map_alloc_equality(__isl_keep isl_basic_map
*bmap
)
1633 struct isl_ctx
*ctx
;
1635 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
1639 isl_assert(ctx
, room_for_con(bmap
, 1), return -1);
1640 isl_assert(ctx
, (bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
<= bmap
->c_size
,
1642 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1643 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1644 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1645 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1646 if ((bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
== bmap
->c_size
) {
1648 int j
= isl_basic_map_alloc_inequality(bmap
);
1652 bmap
->ineq
[j
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1653 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
1660 isl_seq_clr(bmap
->eq
[bmap
->n_eq
] + 1 + total
,
1661 bmap
->extra
- bmap
->n_div
);
1662 return bmap
->n_eq
++;
1665 int isl_basic_set_alloc_equality(__isl_keep isl_basic_set
*bset
)
1667 return isl_basic_map_alloc_equality(bset_to_bmap(bset
));
1670 __isl_give isl_basic_map
*isl_basic_map_free_equality(
1671 __isl_take isl_basic_map
*bmap
, unsigned n
)
1676 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
1677 "invalid number of equalities",
1678 isl_basic_map_free(bmap
));
1683 __isl_give isl_basic_set
*isl_basic_set_free_equality(
1684 __isl_take isl_basic_set
*bset
, unsigned n
)
1686 return bset_from_bmap(isl_basic_map_free_equality(bset_to_bmap(bset
),
1690 /* Drop the equality constraint at position "pos",
1691 * preserving the order of the other equality constraints.
1693 int isl_basic_map_drop_equality(__isl_keep isl_basic_map
*bmap
, unsigned pos
)
1700 isl_assert(bmap
->ctx
, pos
< bmap
->n_eq
, return -1);
1704 for (r
= pos
; r
< bmap
->n_eq
; ++r
)
1705 bmap
->eq
[r
] = bmap
->eq
[r
+ 1];
1706 bmap
->eq
[bmap
->n_eq
] = t
;
1711 /* Turn inequality "pos" of "bmap" into an equality.
1713 * In particular, we move the inequality in front of the equalities
1714 * and move the last inequality in the position of the moved inequality.
1715 * Note that isl_tab_make_equalities_explicit depends on this particular
1716 * change in the ordering of the constraints.
1718 void isl_basic_map_inequality_to_equality(
1719 __isl_keep isl_basic_map
*bmap
, unsigned pos
)
1723 t
= bmap
->ineq
[pos
];
1724 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1725 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
1730 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1731 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
1732 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1733 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1736 static int room_for_ineq(__isl_keep isl_basic_map
*bmap
, unsigned n
)
1738 return bmap
->n_ineq
+ n
<= bmap
->eq
- bmap
->ineq
;
1741 int isl_basic_map_alloc_inequality(__isl_keep isl_basic_map
*bmap
)
1744 struct isl_ctx
*ctx
;
1746 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
1750 isl_assert(ctx
, room_for_ineq(bmap
, 1), return -1);
1751 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1752 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1753 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
1754 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1755 ISL_F_CLR(bmap
, ISL_BASIC_MAP_REDUCED_COEFFICIENTS
);
1756 isl_seq_clr(bmap
->ineq
[bmap
->n_ineq
] + 1 + total
,
1757 bmap
->extra
- bmap
->n_div
);
1758 return bmap
->n_ineq
++;
1761 int isl_basic_set_alloc_inequality(__isl_keep isl_basic_set
*bset
)
1763 return isl_basic_map_alloc_inequality(bset_to_bmap(bset
));
1766 __isl_give isl_basic_map
*isl_basic_map_free_inequality(
1767 __isl_take isl_basic_map
*bmap
, unsigned n
)
1771 if (n
> bmap
->n_ineq
)
1772 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
1773 "invalid number of inequalities",
1774 return isl_basic_map_free(bmap
));
1779 __isl_give isl_basic_set
*isl_basic_set_free_inequality(
1780 __isl_take isl_basic_set
*bset
, unsigned n
)
1782 return bset_from_bmap(isl_basic_map_free_inequality(bset_to_bmap(bset
),
1786 int isl_basic_map_drop_inequality(__isl_keep isl_basic_map
*bmap
, unsigned pos
)
1791 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
1793 if (pos
!= bmap
->n_ineq
- 1) {
1794 t
= bmap
->ineq
[pos
];
1795 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1796 bmap
->ineq
[bmap
->n_ineq
- 1] = t
;
1797 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
1803 int isl_basic_set_drop_inequality(__isl_keep isl_basic_set
*bset
, unsigned pos
)
1805 return isl_basic_map_drop_inequality(bset_to_bmap(bset
), pos
);
1808 __isl_give isl_basic_map
*isl_basic_map_add_eq(__isl_take isl_basic_map
*bmap
,
1815 empty
= isl_basic_map_plain_is_empty(bmap
);
1817 return isl_basic_map_free(bmap
);
1821 bmap
= isl_basic_map_cow(bmap
);
1822 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
1823 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
1825 return isl_basic_map_free(bmap
);
1826 k
= isl_basic_map_alloc_equality(bmap
);
1829 isl_seq_cpy(bmap
->eq
[k
], eq
, 1 + total
);
1832 isl_basic_map_free(bmap
);
1836 __isl_give isl_basic_set
*isl_basic_set_add_eq(__isl_take isl_basic_set
*bset
,
1839 return bset_from_bmap(isl_basic_map_add_eq(bset_to_bmap(bset
), eq
));
1842 __isl_give isl_basic_map
*isl_basic_map_add_ineq(__isl_take isl_basic_map
*bmap
,
1848 bmap
= isl_basic_map_cow(bmap
);
1849 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
1850 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
1852 return isl_basic_map_free(bmap
);
1853 k
= isl_basic_map_alloc_inequality(bmap
);
1856 isl_seq_cpy(bmap
->ineq
[k
], ineq
, 1 + total
);
1859 isl_basic_map_free(bmap
);
1863 __isl_give isl_basic_set
*isl_basic_set_add_ineq(__isl_take isl_basic_set
*bset
,
1866 return bset_from_bmap(isl_basic_map_add_ineq(bset_to_bmap(bset
), ineq
));
1869 int isl_basic_map_alloc_div(__isl_keep isl_basic_map
*bmap
)
1873 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
1876 isl_assert(bmap
->ctx
, bmap
->n_div
< bmap
->extra
, return -1);
1877 isl_seq_clr(bmap
->div
[bmap
->n_div
] + 1 + 1 + total
,
1878 bmap
->extra
- bmap
->n_div
);
1879 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1880 return bmap
->n_div
++;
1883 int isl_basic_set_alloc_div(__isl_keep isl_basic_set
*bset
)
1885 return isl_basic_map_alloc_div(bset_to_bmap(bset
));
1889 #define TYPE isl_basic_map
1890 #include "check_type_range_templ.c"
1892 /* Check that there are "n" dimensions of type "type" starting at "first"
1895 isl_stat
isl_basic_set_check_range(__isl_keep isl_basic_set
*bset
,
1896 enum isl_dim_type type
, unsigned first
, unsigned n
)
1898 return isl_basic_map_check_range(bset_to_bmap(bset
),
1902 /* Insert an extra integer division, prescribed by "div", to "bmap"
1903 * at (integer division) position "pos".
1905 * The integer division is first added at the end and then moved
1906 * into the right position.
1908 __isl_give isl_basic_map
*isl_basic_map_insert_div(
1909 __isl_take isl_basic_map
*bmap
, int pos
, __isl_keep isl_vec
*div
)
1914 bmap
= isl_basic_map_cow(bmap
);
1915 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
1916 if (total
< 0 || !div
)
1917 return isl_basic_map_free(bmap
);
1919 if (div
->size
!= 1 + 1 + total
)
1920 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
1921 "unexpected size", return isl_basic_map_free(bmap
));
1922 if (isl_basic_map_check_range(bmap
, isl_dim_div
, pos
, 0) < 0)
1923 return isl_basic_map_free(bmap
);
1925 bmap
= isl_basic_map_extend(bmap
, 1, 0, 2);
1926 k
= isl_basic_map_alloc_div(bmap
);
1928 return isl_basic_map_free(bmap
);
1929 isl_seq_cpy(bmap
->div
[k
], div
->el
, div
->size
);
1930 isl_int_set_si(bmap
->div
[k
][div
->size
], 0);
1932 for (i
= k
; i
> pos
; --i
)
1933 bmap
= isl_basic_map_swap_div(bmap
, i
, i
- 1);
1938 isl_stat
isl_basic_map_free_div(__isl_keep isl_basic_map
*bmap
, unsigned n
)
1941 return isl_stat_error
;
1942 isl_assert(bmap
->ctx
, n
<= bmap
->n_div
, return isl_stat_error
);
1947 static __isl_give isl_basic_map
*add_constraints(
1948 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
,
1949 unsigned i_pos
, unsigned o_pos
)
1951 isl_size total
, n_param
, n_in
, n_out
, n_div
;
1952 unsigned o_in
, o_out
;
1955 struct isl_dim_map
*dim_map
;
1957 space
= isl_basic_map_peek_space(bmap2
);
1958 if (!bmap1
|| !space
)
1961 total
= isl_basic_map_dim(bmap1
, isl_dim_all
);
1962 n_param
= isl_basic_map_dim(bmap2
, isl_dim_param
);
1963 n_in
= isl_basic_map_dim(bmap2
, isl_dim_in
);
1964 o_in
= isl_basic_map_offset(bmap1
, isl_dim_in
) - 1 + i_pos
;
1965 n_out
= isl_basic_map_dim(bmap2
, isl_dim_out
);
1966 o_out
= isl_basic_map_offset(bmap1
, isl_dim_out
) - 1 + o_pos
;
1967 n_div
= isl_basic_map_dim(bmap2
, isl_dim_div
);
1968 if (total
< 0 || n_param
< 0 || n_in
< 0 || n_out
< 0 || n_div
< 0)
1970 ctx
= isl_basic_map_get_ctx(bmap1
);
1971 dim_map
= isl_dim_map_alloc(ctx
, total
+ n_div
);
1972 isl_dim_map_dim_range(dim_map
, space
, isl_dim_param
, 0, n_param
, 0);
1973 isl_dim_map_dim_range(dim_map
, space
, isl_dim_in
, 0, n_in
, o_in
);
1974 isl_dim_map_dim_range(dim_map
, space
, isl_dim_out
, 0, n_out
, o_out
);
1975 isl_dim_map_div(dim_map
, bmap2
, total
);
1977 return isl_basic_map_add_constraints_dim_map(bmap1
, bmap2
, dim_map
);
1979 isl_basic_map_free(bmap1
);
1980 isl_basic_map_free(bmap2
);
1984 __isl_give isl_basic_map
*isl_basic_map_extend(__isl_take isl_basic_map
*base
,
1985 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
1988 struct isl_basic_map
*ext
;
1995 dims_ok
= base
->extra
>= base
->n_div
+ extra
;
1997 if (dims_ok
&& room_for_con(base
, n_eq
+ n_ineq
) &&
1998 room_for_ineq(base
, n_ineq
))
2001 extra
+= base
->extra
;
2003 n_ineq
+= base
->n_ineq
;
2005 space
= isl_basic_map_get_space(base
);
2006 ext
= isl_basic_map_alloc_space(space
, extra
, n_eq
, n_ineq
);
2011 ext
->sample
= isl_vec_copy(base
->sample
);
2012 flags
= base
->flags
;
2013 ext
= add_constraints(ext
, base
, 0, 0);
2016 ISL_F_CLR(ext
, ISL_BASIC_SET_FINAL
);
2022 isl_basic_map_free(base
);
2026 __isl_give isl_basic_set
*isl_basic_set_extend(__isl_take isl_basic_set
*base
,
2027 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
2029 return bset_from_bmap(isl_basic_map_extend(bset_to_bmap(base
),
2030 extra
, n_eq
, n_ineq
));
2033 __isl_give isl_basic_map
*isl_basic_map_extend_constraints(
2034 __isl_take isl_basic_map
*base
, unsigned n_eq
, unsigned n_ineq
)
2036 return isl_basic_map_extend(base
, 0, n_eq
, n_ineq
);
2039 __isl_give isl_basic_set
*isl_basic_set_extend_constraints(
2040 __isl_take isl_basic_set
*base
, unsigned n_eq
, unsigned n_ineq
)
2042 isl_basic_map
*bmap
= bset_to_bmap(base
);
2043 bmap
= isl_basic_map_extend_constraints(bmap
, n_eq
, n_ineq
);
2044 return bset_from_bmap(bmap
);
2047 __isl_give isl_basic_set
*isl_basic_set_cow(__isl_take isl_basic_set
*bset
)
2049 return bset_from_bmap(isl_basic_map_cow(bset_to_bmap(bset
)));
2052 __isl_give isl_basic_map
*isl_basic_map_cow(__isl_take isl_basic_map
*bmap
)
2057 if (bmap
->ref
> 1) {
2059 bmap
= isl_basic_map_dup(bmap
);
2062 ISL_F_CLR(bmap
, ISL_BASIC_SET_FINAL
);
2066 /* Clear all cached information in "map", either because it is about
2067 * to be modified or because it is being freed.
2068 * Always return the same pointer that is passed in.
2069 * This is needed for the use in isl_map_free.
2071 static __isl_give isl_map
*clear_caches(__isl_take isl_map
*map
)
2073 isl_basic_map_free(map
->cached_simple_hull
[0]);
2074 isl_basic_map_free(map
->cached_simple_hull
[1]);
2075 map
->cached_simple_hull
[0] = NULL
;
2076 map
->cached_simple_hull
[1] = NULL
;
2080 __isl_give isl_set
*isl_set_cow(__isl_take isl_set
*set
)
2082 return isl_map_cow(set
);
2085 /* Return an isl_map that is equal to "map" and that has only
2086 * a single reference.
2088 * If the original input already has only one reference, then
2089 * simply return it, but clear all cached information, since
2090 * it may be rendered invalid by the operations that will be
2091 * performed on the result.
2093 * Otherwise, create a duplicate (without any cached information).
2095 __isl_give isl_map
*isl_map_cow(__isl_take isl_map
*map
)
2101 return clear_caches(map
);
2103 return isl_map_dup(map
);
2106 static void swap_vars(struct isl_blk blk
, isl_int
*a
,
2107 unsigned a_len
, unsigned b_len
)
2109 isl_seq_cpy(blk
.data
, a
+a_len
, b_len
);
2110 isl_seq_cpy(blk
.data
+b_len
, a
, a_len
);
2111 isl_seq_cpy(a
, blk
.data
, b_len
+a_len
);
2114 static __isl_give isl_basic_map
*isl_basic_map_swap_vars(
2115 __isl_take isl_basic_map
*bmap
, unsigned pos
, unsigned n1
, unsigned n2
)
2120 if (isl_basic_map_check_range(bmap
, isl_dim_all
, pos
- 1, n1
+ n2
) < 0)
2123 if (n1
== 0 || n2
== 0)
2126 bmap
= isl_basic_map_cow(bmap
);
2130 blk
= isl_blk_alloc(bmap
->ctx
, n1
+ n2
);
2131 if (isl_blk_is_error(blk
))
2134 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2136 bmap
->eq
[i
] + pos
, n1
, n2
);
2138 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2140 bmap
->ineq
[i
] + pos
, n1
, n2
);
2142 for (i
= 0; i
< bmap
->n_div
; ++i
)
2144 bmap
->div
[i
]+1 + pos
, n1
, n2
);
2146 isl_blk_free(bmap
->ctx
, blk
);
2148 ISL_F_CLR(bmap
, ISL_BASIC_SET_SORTED
);
2149 bmap
= isl_basic_map_gauss(bmap
, NULL
);
2150 return isl_basic_map_finalize(bmap
);
2152 isl_basic_map_free(bmap
);
2156 /* The given basic map has turned out to be empty.
2157 * Explicitly mark it as such and change the representation
2158 * to a canonical representation of the empty basic map.
2159 * Since the basic map has conflicting constraints,
2160 * it must have at least one constraint, except perhaps
2161 * if it was already explicitly marked as being empty.
2162 * Do nothing in the latter case, i.e., if it has been marked empty and
2163 * has no constraints.
2165 __isl_give isl_basic_map
*isl_basic_map_set_to_empty(
2166 __isl_take isl_basic_map
*bmap
)
2173 n
= isl_basic_map_n_constraint(bmap
);
2174 empty
= isl_basic_map_plain_is_empty(bmap
);
2175 if (n
< 0 || empty
< 0)
2176 return isl_basic_map_free(bmap
);
2177 if (n
== 0 && empty
)
2179 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
2181 return isl_basic_map_free(bmap
);
2182 if (isl_basic_map_free_div(bmap
, bmap
->n_div
) < 0)
2183 return isl_basic_map_free(bmap
);
2184 bmap
= isl_basic_map_free_inequality(bmap
, bmap
->n_ineq
);
2187 if (bmap
->n_eq
> 0) {
2188 bmap
= isl_basic_map_free_equality(bmap
, bmap
->n_eq
- 1);
2192 i
= isl_basic_map_alloc_equality(bmap
);
2196 isl_int_set_si(bmap
->eq
[i
][0], 1);
2197 isl_seq_clr(bmap
->eq
[i
]+1, total
);
2198 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
2199 isl_vec_free(bmap
->sample
);
2200 bmap
->sample
= NULL
;
2201 return isl_basic_map_finalize(bmap
);
2203 isl_basic_map_free(bmap
);
2207 __isl_give isl_basic_set
*isl_basic_set_set_to_empty(
2208 __isl_take isl_basic_set
*bset
)
2210 return bset_from_bmap(isl_basic_map_set_to_empty(bset_to_bmap(bset
)));
2213 __isl_give isl_basic_map
*isl_basic_map_set_rational(
2214 __isl_take isl_basic_map
*bmap
)
2219 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
2222 bmap
= isl_basic_map_cow(bmap
);
2226 ISL_F_SET(bmap
, ISL_BASIC_MAP_RATIONAL
);
2228 return isl_basic_map_finalize(bmap
);
2231 __isl_give isl_basic_set
*isl_basic_set_set_rational(
2232 __isl_take isl_basic_set
*bset
)
2234 return isl_basic_map_set_rational(bset
);
2237 __isl_give isl_basic_set
*isl_basic_set_set_integral(
2238 __isl_take isl_basic_set
*bset
)
2243 if (!ISL_F_ISSET(bset
, ISL_BASIC_MAP_RATIONAL
))
2246 bset
= isl_basic_set_cow(bset
);
2250 ISL_F_CLR(bset
, ISL_BASIC_MAP_RATIONAL
);
2252 return isl_basic_set_finalize(bset
);
2255 __isl_give isl_map
*isl_map_set_rational(__isl_take isl_map
*map
)
2259 map
= isl_map_cow(map
);
2262 for (i
= 0; i
< map
->n
; ++i
) {
2263 map
->p
[i
] = isl_basic_map_set_rational(map
->p
[i
]);
2273 __isl_give isl_set
*isl_set_set_rational(__isl_take isl_set
*set
)
2275 return isl_map_set_rational(set
);
2278 /* Given a constraint "c" that expresses a bound
2279 * on the variable at position "pos" in terms of the first "len" variables
2280 * (other than the variable itself if pos < len), extract this bound
2281 * as a function of those first "len" variables.
2283 * That is, the constraint is of one of the following forms
2285 * -e(...) + m x >= 0
2290 * Return (e(...)) / m, with the denominator m in the first position.
2292 static __isl_give isl_vec
*extract_bound_from_constraint(isl_ctx
*ctx
,
2293 isl_int
*c
, int len
, int pos
)
2297 v
= isl_vec_alloc(ctx
, 1 + 1 + len
);
2300 if (isl_int_is_pos(c
[1 + pos
])) {
2301 isl_int_set(v
->el
[0], c
[1 + pos
]);
2302 isl_seq_neg(v
->el
+ 1, c
, 1 + len
);
2304 isl_int_neg(v
->el
[0], c
[1 + pos
]);
2305 isl_seq_cpy(v
->el
+ 1, c
, 1 + len
);
2308 isl_int_set_si(v
->el
[1 + 1 + pos
], 0);
2313 /* Do the inequality constraints "i" and "j" of "bmap"
2314 * form a pair of opposite constraints, in the (first) "len" coefficients?
2316 static int is_constraint_pair(__isl_keep isl_basic_map
*bmap
, int i
, int j
,
2319 return isl_seq_is_neg(bmap
->ineq
[i
] + 1, bmap
->ineq
[j
] + 1, len
);
2322 /* Given that inequality constraints "i" and "j" of "bmap"
2323 * form a pair of opposite constraints
2332 * do they allow for at most "bound" values in that direction?
2333 * That is, is the sum of their constant terms smaller than "bound"?
2335 * "tmp" is a temporary location that can be used to store the sum.
2337 static int constraint_pair_has_bound(__isl_keep isl_basic_map
*bmap
,
2338 int i
, int j
, isl_int bound
, isl_int
*tmp
)
2340 isl_int_add(*tmp
, bmap
->ineq
[i
][0], bmap
->ineq
[j
][0]);
2341 return isl_int_abs_lt(*tmp
, bound
);
2344 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
2347 static void swap_div(__isl_keep isl_basic_map
*bmap
, int a
, int b
)
2349 isl_int
*t
= bmap
->div
[a
];
2350 bmap
->div
[a
] = bmap
->div
[b
];
2354 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
2355 * div definitions accordingly.
2357 __isl_give isl_basic_map
*isl_basic_map_swap_div(__isl_take isl_basic_map
*bmap
,
2363 off
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
2365 return isl_basic_map_free(bmap
);
2367 swap_div(bmap
, a
, b
);
2369 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2370 isl_int_swap(bmap
->eq
[i
][1+off
+a
], bmap
->eq
[i
][1+off
+b
]);
2372 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2373 isl_int_swap(bmap
->ineq
[i
][1+off
+a
], bmap
->ineq
[i
][1+off
+b
]);
2375 for (i
= 0; i
< bmap
->n_div
; ++i
)
2376 isl_int_swap(bmap
->div
[i
][1+1+off
+a
], bmap
->div
[i
][1+1+off
+b
]);
2377 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
2382 static void constraint_drop_vars(isl_int
*c
, unsigned n
, unsigned rem
)
2384 isl_seq_cpy(c
, c
+ n
, rem
);
2385 isl_seq_clr(c
+ rem
, n
);
2388 /* Drop n dimensions starting at first.
2390 * In principle, this frees up some extra variables as the number
2391 * of columns remains constant, but we would have to extend
2392 * the div array too as the number of rows in this array is assumed
2393 * to be equal to extra.
2395 __isl_give isl_basic_set
*isl_basic_set_drop_dims(
2396 __isl_take isl_basic_set
*bset
, unsigned first
, unsigned n
)
2398 return isl_basic_map_drop(bset_to_bmap(bset
), isl_dim_set
, first
, n
);
2401 /* Move "n" divs starting at "first" to the end of the list of divs.
2403 static __isl_give isl_basic_map
*move_divs_last(__isl_take isl_basic_map
*bmap
,
2404 unsigned first
, unsigned n
)
2409 if (first
+ n
== bmap
->n_div
)
2412 div
= isl_alloc_array(bmap
->ctx
, isl_int
*, n
);
2415 for (i
= 0; i
< n
; ++i
)
2416 div
[i
] = bmap
->div
[first
+ i
];
2417 for (i
= 0; i
< bmap
->n_div
- first
- n
; ++i
)
2418 bmap
->div
[first
+ i
] = bmap
->div
[first
+ n
+ i
];
2419 for (i
= 0; i
< n
; ++i
)
2420 bmap
->div
[bmap
->n_div
- n
+ i
] = div
[i
];
2424 isl_basic_map_free(bmap
);
2429 #define TYPE isl_map
2431 #include "check_type_range_templ.c"
2433 /* Check that there are "n" dimensions of type "type" starting at "first"
2436 isl_stat
isl_set_check_range(__isl_keep isl_set
*set
,
2437 enum isl_dim_type type
, unsigned first
, unsigned n
)
2439 return isl_map_check_range(set_to_map(set
), type
, first
, n
);
2442 /* Drop "n" dimensions of type "type" starting at "first".
2443 * Perform the core computation, without cowing or
2444 * simplifying and finalizing the result.
2446 * In principle, this frees up some extra variables as the number
2447 * of columns remains constant, but we would have to extend
2448 * the div array too as the number of rows in this array is assumed
2449 * to be equal to extra.
2451 __isl_give isl_basic_map
*isl_basic_map_drop_core(
2452 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
2453 unsigned first
, unsigned n
)
2460 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2461 return isl_basic_map_free(bmap
);
2463 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
2465 return isl_basic_map_free(bmap
);
2467 offset
= isl_basic_map_offset(bmap
, type
) + first
;
2468 left
= total
- (offset
- 1) - n
;
2469 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2470 constraint_drop_vars(bmap
->eq
[i
]+offset
, n
, left
);
2472 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2473 constraint_drop_vars(bmap
->ineq
[i
]+offset
, n
, left
);
2475 for (i
= 0; i
< bmap
->n_div
; ++i
)
2476 constraint_drop_vars(bmap
->div
[i
]+1+offset
, n
, left
);
2478 if (type
== isl_dim_div
) {
2479 bmap
= move_divs_last(bmap
, first
, n
);
2482 if (isl_basic_map_free_div(bmap
, n
) < 0)
2483 return isl_basic_map_free(bmap
);
2485 bmap
->dim
= isl_space_drop_dims(bmap
->dim
, type
, first
, n
);
2487 return isl_basic_map_free(bmap
);
2489 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
2490 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
2491 ISL_F_CLR(bmap
, ISL_BASIC_MAP_REDUCED_COEFFICIENTS
);
2495 /* Drop "n" dimensions of type "type" starting at "first".
2497 * In principle, this frees up some extra variables as the number
2498 * of columns remains constant, but we would have to extend
2499 * the div array too as the number of rows in this array is assumed
2500 * to be equal to extra.
2502 __isl_give isl_basic_map
*isl_basic_map_drop(__isl_take isl_basic_map
*bmap
,
2503 enum isl_dim_type type
, unsigned first
, unsigned n
)
2507 if (n
== 0 && !isl_space_is_named_or_nested(bmap
->dim
, type
))
2510 bmap
= isl_basic_map_cow(bmap
);
2514 bmap
= isl_basic_map_drop_core(bmap
, type
, first
, n
);
2516 bmap
= isl_basic_map_simplify(bmap
);
2517 return isl_basic_map_finalize(bmap
);
2520 __isl_give isl_basic_set
*isl_basic_set_drop(__isl_take isl_basic_set
*bset
,
2521 enum isl_dim_type type
, unsigned first
, unsigned n
)
2523 return bset_from_bmap(isl_basic_map_drop(bset_to_bmap(bset
),
2527 /* No longer consider "map" to be normalized.
2529 static __isl_give isl_map
*isl_map_unmark_normalized(__isl_take isl_map
*map
)
2533 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
2537 __isl_give isl_map
*isl_map_drop(__isl_take isl_map
*map
,
2538 enum isl_dim_type type
, unsigned first
, unsigned n
)
2543 if (isl_map_check_range(map
, type
, first
, n
) < 0)
2544 return isl_map_free(map
);
2546 if (n
== 0 && !isl_space_is_named_or_nested(map
->dim
, type
))
2548 map
= isl_map_cow(map
);
2552 for (i
= 0; i
< map
->n
; ++i
) {
2553 map
->p
[i
] = isl_basic_map_drop(map
->p
[i
], type
, first
, n
);
2557 map
= isl_map_unmark_normalized(map
);
2559 space
= isl_map_take_space(map
);
2560 space
= isl_space_drop_dims(space
, type
, first
, n
);
2561 map
= isl_map_restore_space(map
, space
);
2569 __isl_give isl_set
*isl_set_drop(__isl_take isl_set
*set
,
2570 enum isl_dim_type type
, unsigned first
, unsigned n
)
2572 return set_from_map(isl_map_drop(set_to_map(set
), type
, first
, n
));
2575 /* Drop the integer division at position "div", which is assumed
2576 * not to appear in any of the constraints or
2577 * in any of the other integer divisions.
2579 * Since the integer division is redundant, there is no need to cow.
2581 __isl_give isl_basic_map
*isl_basic_map_drop_div(
2582 __isl_take isl_basic_map
*bmap
, unsigned div
)
2584 return isl_basic_map_drop_core(bmap
, isl_dim_div
, div
, 1);
2587 /* Eliminate the specified n dimensions starting at first from the
2588 * constraints, without removing the dimensions from the space.
2589 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2591 __isl_give isl_map
*isl_map_eliminate(__isl_take isl_map
*map
,
2592 enum isl_dim_type type
, unsigned first
, unsigned n
)
2599 if (isl_map_check_range(map
, type
, first
, n
) < 0)
2600 return isl_map_free(map
);
2602 map
= isl_map_cow(map
);
2606 for (i
= 0; i
< map
->n
; ++i
) {
2607 map
->p
[i
] = isl_basic_map_eliminate(map
->p
[i
], type
, first
, n
);
2617 /* Eliminate the specified n dimensions starting at first from the
2618 * constraints, without removing the dimensions from the space.
2619 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2621 __isl_give isl_set
*isl_set_eliminate(__isl_take isl_set
*set
,
2622 enum isl_dim_type type
, unsigned first
, unsigned n
)
2624 return set_from_map(isl_map_eliminate(set_to_map(set
), type
, first
, n
));
2627 /* Eliminate the specified n dimensions starting at first from the
2628 * constraints, without removing the dimensions from the space.
2629 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2631 __isl_give isl_set
*isl_set_eliminate_dims(__isl_take isl_set
*set
,
2632 unsigned first
, unsigned n
)
2634 return isl_set_eliminate(set
, isl_dim_set
, first
, n
);
2637 __isl_give isl_basic_map
*isl_basic_map_remove_divs(
2638 __isl_take isl_basic_map
*bmap
)
2642 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
2644 return isl_basic_map_free(bmap
);
2645 bmap
= isl_basic_map_eliminate_vars(bmap
, v_div
, bmap
->n_div
);
2649 return isl_basic_map_finalize(bmap
);
2652 __isl_give isl_basic_set
*isl_basic_set_remove_divs(
2653 __isl_take isl_basic_set
*bset
)
2655 return bset_from_bmap(isl_basic_map_remove_divs(bset_to_bmap(bset
)));
2658 __isl_give isl_map
*isl_map_remove_divs(__isl_take isl_map
*map
)
2667 map
= isl_map_cow(map
);
2671 for (i
= 0; i
< map
->n
; ++i
) {
2672 map
->p
[i
] = isl_basic_map_remove_divs(map
->p
[i
]);
2682 __isl_give isl_set
*isl_set_remove_divs(__isl_take isl_set
*set
)
2684 return isl_map_remove_divs(set
);
2687 __isl_give isl_basic_map
*isl_basic_map_remove_dims(
2688 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
2689 unsigned first
, unsigned n
)
2691 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2692 return isl_basic_map_free(bmap
);
2693 if (n
== 0 && !isl_space_is_named_or_nested(bmap
->dim
, type
))
2695 bmap
= isl_basic_map_eliminate_vars(bmap
,
2696 isl_basic_map_offset(bmap
, type
) - 1 + first
, n
);
2699 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
) && type
== isl_dim_div
)
2701 bmap
= isl_basic_map_drop(bmap
, type
, first
, n
);
2705 /* Does the local variable "div" of "bmap" have a known expression
2706 * that involves the "n" variables starting at "first"?
2708 isl_bool
isl_basic_map_div_expr_involves_vars(__isl_keep isl_basic_map
*bmap
,
2709 int div
, unsigned first
, unsigned n
)
2713 unknown
= isl_basic_map_div_is_marked_unknown(bmap
, div
);
2714 if (unknown
< 0 || unknown
)
2715 return isl_bool_not(unknown
);
2716 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + 1 + first
, n
) >= 0)
2717 return isl_bool_true
;
2719 return isl_bool_false
;
2722 /* Return true if the definition of the given div (recursively) involves
2723 * any of the given variables.
2725 static isl_bool
div_involves_vars(__isl_keep isl_basic_map
*bmap
, int div
,
2726 unsigned first
, unsigned n
)
2730 isl_size n_div
, v_div
;
2732 involves
= isl_basic_map_div_expr_involves_vars(bmap
, div
, first
, n
);
2733 if (involves
< 0 || involves
)
2736 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
2737 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
2738 if (n_div
< 0 || v_div
< 0)
2739 return isl_bool_error
;
2740 for (i
= n_div
- 1; i
>= 0; --i
) {
2743 if (isl_int_is_zero(bmap
->div
[div
][1 + 1 + v_div
+ i
]))
2745 involves
= div_involves_vars(bmap
, i
, first
, n
);
2746 if (involves
< 0 || involves
)
2750 return isl_bool_false
;
2753 /* Does the definition of any integer division involve
2754 * any of the given variables?
2756 isl_bool
isl_basic_map_any_div_involves_vars(__isl_keep isl_basic_map
*bmap
,
2757 unsigned first
, unsigned n
)
2762 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
2764 return isl_bool_error
;
2766 for (i
= 0; i
< n_div
; ++i
) {
2769 has
= isl_basic_map_div_expr_involves_vars(bmap
, i
, first
, n
);
2774 return isl_bool_false
;
2777 /* Try and add a lower and/or upper bound on "div" to "bmap"
2778 * based on inequality "i".
2779 * "total" is the total number of variables (excluding the divs).
2780 * "v" is a temporary object that can be used during the calculations.
2781 * If "lb" is set, then a lower bound should be constructed.
2782 * If "ub" is set, then an upper bound should be constructed.
2784 * The calling function has already checked that the inequality does not
2785 * reference "div", but we still need to check that the inequality is
2786 * of the right form. We'll consider the case where we want to construct
2787 * a lower bound. The construction of upper bounds is similar.
2789 * Let "div" be of the form
2791 * q = floor((a + f(x))/d)
2793 * We essentially check if constraint "i" is of the form
2797 * so that we can use it to derive a lower bound on "div".
2798 * However, we allow a slightly more general form
2802 * with the condition that the coefficients of g(x) - f(x) are all
2804 * Rewriting this constraint as
2808 * adding a + f(x) to both sides and dividing by d, we obtain
2810 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
2812 * Taking the floor on both sides, we obtain
2814 * q >= floor((a-b)/d) + (f(x)-g(x))/d
2818 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
2820 * In the case of an upper bound, we construct the constraint
2822 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
2825 static __isl_give isl_basic_map
*insert_bounds_on_div_from_ineq(
2826 __isl_take isl_basic_map
*bmap
, int div
, int i
,
2827 unsigned total
, isl_int v
, int lb
, int ub
)
2831 for (j
= 0; (lb
|| ub
) && j
< total
+ bmap
->n_div
; ++j
) {
2833 isl_int_sub(v
, bmap
->ineq
[i
][1 + j
],
2834 bmap
->div
[div
][1 + 1 + j
]);
2835 lb
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
2838 isl_int_add(v
, bmap
->ineq
[i
][1 + j
],
2839 bmap
->div
[div
][1 + 1 + j
]);
2840 ub
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
2846 bmap
= isl_basic_map_cow(bmap
);
2847 bmap
= isl_basic_map_extend_constraints(bmap
, 0, lb
+ ub
);
2849 int k
= isl_basic_map_alloc_inequality(bmap
);
2852 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
2853 isl_int_sub(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
2854 bmap
->div
[div
][1 + j
]);
2855 isl_int_cdiv_q(bmap
->ineq
[k
][j
],
2856 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
2858 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], 1);
2861 int k
= isl_basic_map_alloc_inequality(bmap
);
2864 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
2865 isl_int_add(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
2866 bmap
->div
[div
][1 + j
]);
2867 isl_int_fdiv_q(bmap
->ineq
[k
][j
],
2868 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
2870 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], -1);
2875 isl_basic_map_free(bmap
);
2879 /* This function is called right before "div" is eliminated from "bmap"
2880 * using Fourier-Motzkin.
2881 * Look through the constraints of "bmap" for constraints on the argument
2882 * of the integer division and use them to construct constraints on the
2883 * integer division itself. These constraints can then be combined
2884 * during the Fourier-Motzkin elimination.
2885 * Note that it is only useful to introduce lower bounds on "div"
2886 * if "bmap" already contains upper bounds on "div" as the newly
2887 * introduce lower bounds can then be combined with the pre-existing
2888 * upper bounds. Similarly for upper bounds.
2889 * We therefore first check if "bmap" contains any lower and/or upper bounds
2892 * It is interesting to note that the introduction of these constraints
2893 * can indeed lead to more accurate results, even when compared to
2894 * deriving constraints on the argument of "div" from constraints on "div".
2895 * Consider, for example, the set
2897 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2899 * The second constraint can be rewritten as
2901 * 2 * [(-i-2j+3)/4] + k >= 0
2903 * from which we can derive
2905 * -i - 2j + 3 >= -2k
2911 * Combined with the first constraint, we obtain
2913 * -3 <= 3 + 2k or k >= -3
2915 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2916 * the first constraint, we obtain
2918 * [(i + 2j)/4] >= [-3/4] = -1
2920 * Combining this constraint with the second constraint, we obtain
2924 static __isl_give isl_basic_map
*insert_bounds_on_div(
2925 __isl_take isl_basic_map
*bmap
, int div
)
2928 int check_lb
, check_ub
;
2935 if (isl_int_is_zero(bmap
->div
[div
][0]))
2938 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
2940 return isl_basic_map_free(bmap
);
2944 for (i
= 0; (!check_lb
|| !check_ub
) && i
< bmap
->n_ineq
; ++i
) {
2945 int s
= isl_int_sgn(bmap
->ineq
[i
][1 + v_div
+ div
]);
2952 if (!check_lb
&& !check_ub
)
2957 for (i
= 0; bmap
&& i
< bmap
->n_ineq
; ++i
) {
2958 if (!isl_int_is_zero(bmap
->ineq
[i
][1 + v_div
+ div
]))
2961 bmap
= insert_bounds_on_div_from_ineq(bmap
, div
, i
, v_div
, v
,
2962 check_lb
, check_ub
);
2970 /* Remove all divs (recursively) involving any of the given variables
2971 * in their definitions.
2973 static __isl_give isl_basic_map
*remove_divs_involving_vars(
2974 __isl_take isl_basic_map
*bmap
, unsigned first
, unsigned n
)
2978 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2981 involves
= div_involves_vars(bmap
, i
, first
, n
);
2983 return isl_basic_map_free(bmap
);
2986 bmap
= insert_bounds_on_div(bmap
, i
);
2987 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
2996 /* Remove all divs (recursively) involving any of the given dimensions
2997 * in their definitions.
2999 __isl_give isl_basic_map
*isl_basic_map_remove_divs_involving_dims(
3000 __isl_take isl_basic_map
*bmap
,
3001 enum isl_dim_type type
, unsigned first
, unsigned n
)
3005 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
3006 return isl_basic_map_free(bmap
);
3007 off
= isl_basic_map_var_offset(bmap
, type
);
3009 return isl_basic_map_free(bmap
);
3012 return remove_divs_involving_vars(bmap
, first
, n
);
3015 __isl_give isl_basic_set
*isl_basic_set_remove_divs_involving_dims(
3016 __isl_take isl_basic_set
*bset
,
3017 enum isl_dim_type type
, unsigned first
, unsigned n
)
3019 return isl_basic_map_remove_divs_involving_dims(bset
, type
, first
, n
);
3022 __isl_give isl_map
*isl_map_remove_divs_involving_dims(__isl_take isl_map
*map
,
3023 enum isl_dim_type type
, unsigned first
, unsigned n
)
3032 map
= isl_map_cow(map
);
3036 for (i
= 0; i
< map
->n
; ++i
) {
3037 map
->p
[i
] = isl_basic_map_remove_divs_involving_dims(map
->p
[i
],
3048 __isl_give isl_set
*isl_set_remove_divs_involving_dims(__isl_take isl_set
*set
,
3049 enum isl_dim_type type
, unsigned first
, unsigned n
)
3051 return set_from_map(isl_map_remove_divs_involving_dims(set_to_map(set
),
3055 /* Does the description of "bmap" depend on the specified dimensions?
3056 * We also check whether the dimensions appear in any of the div definitions.
3057 * In principle there is no need for this check. If the dimensions appear
3058 * in a div definition, they also appear in the defining constraints of that
3061 isl_bool
isl_basic_map_involves_dims(__isl_keep isl_basic_map
*bmap
,
3062 enum isl_dim_type type
, unsigned first
, unsigned n
)
3066 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
3067 return isl_bool_error
;
3069 first
+= isl_basic_map_offset(bmap
, type
);
3070 for (i
= 0; i
< bmap
->n_eq
; ++i
)
3071 if (isl_seq_first_non_zero(bmap
->eq
[i
] + first
, n
) >= 0)
3072 return isl_bool_true
;
3073 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
3074 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + first
, n
) >= 0)
3075 return isl_bool_true
;
3076 for (i
= 0; i
< bmap
->n_div
; ++i
) {
3077 if (isl_int_is_zero(bmap
->div
[i
][0]))
3079 if (isl_seq_first_non_zero(bmap
->div
[i
] + 1 + first
, n
) >= 0)
3080 return isl_bool_true
;
3083 return isl_bool_false
;
3086 isl_bool
isl_map_involves_dims(__isl_keep isl_map
*map
,
3087 enum isl_dim_type type
, unsigned first
, unsigned n
)
3091 if (isl_map_check_range(map
, type
, first
, n
) < 0)
3092 return isl_bool_error
;
3094 for (i
= 0; i
< map
->n
; ++i
) {
3095 isl_bool involves
= isl_basic_map_involves_dims(map
->p
[i
],
3097 if (involves
< 0 || involves
)
3101 return isl_bool_false
;
3104 isl_bool
isl_basic_set_involves_dims(__isl_keep isl_basic_set
*bset
,
3105 enum isl_dim_type type
, unsigned first
, unsigned n
)
3107 return isl_basic_map_involves_dims(bset
, type
, first
, n
);
3110 isl_bool
isl_set_involves_dims(__isl_keep isl_set
*set
,
3111 enum isl_dim_type type
, unsigned first
, unsigned n
)
3113 return isl_map_involves_dims(set
, type
, first
, n
);
3116 /* Does "bset" involve any local variables, i.e., integer divisions?
3118 static isl_bool
isl_basic_set_involves_locals(__isl_keep isl_basic_set
*bset
)
3122 n
= isl_basic_set_dim(bset
, isl_dim_div
);
3124 return isl_bool_error
;
3125 return isl_bool_ok(n
> 0);
3128 /* isl_set_every_basic_set callback that checks whether "bset"
3129 * is free of local variables.
3131 static isl_bool
basic_set_no_locals(__isl_keep isl_basic_set
*bset
, void *user
)
3133 return isl_bool_not(isl_basic_set_involves_locals(bset
));
3136 /* Does "set" involve any local variables, i.e., integer divisions?
3138 isl_bool
isl_set_involves_locals(__isl_keep isl_set
*set
)
3142 no_locals
= isl_set_every_basic_set(set
, &basic_set_no_locals
, NULL
);
3143 return isl_bool_not(no_locals
);
3146 /* Drop all constraints in bmap that involve any of the dimensions
3147 * first to first+n-1.
3148 * This function only performs the actual removal of constraints.
3150 * This function should not call finalize since it is used by
3151 * remove_redundant_divs, which in turn is called by isl_basic_map_finalize.
3153 __isl_give isl_basic_map
*isl_basic_map_drop_constraints_involving(
3154 __isl_take isl_basic_map
*bmap
, unsigned first
, unsigned n
)
3161 bmap
= isl_basic_map_cow(bmap
);
3166 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
) {
3167 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + first
, n
) == -1)
3169 if (isl_basic_map_drop_equality(bmap
, i
) < 0)
3170 return isl_basic_map_free(bmap
);
3173 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
3174 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + 1 + first
, n
) == -1)
3176 if (isl_basic_map_drop_inequality(bmap
, i
) < 0)
3177 return isl_basic_map_free(bmap
);
3183 /* Drop all constraints in bset that involve any of the dimensions
3184 * first to first+n-1.
3185 * This function only performs the actual removal of constraints.
3187 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_involving(
3188 __isl_take isl_basic_set
*bset
, unsigned first
, unsigned n
)
3190 return isl_basic_map_drop_constraints_involving(bset
, first
, n
);
3193 /* Drop all constraints in bmap that do not involve any of the dimensions
3194 * first to first + n - 1 of the given type.
3196 __isl_give isl_basic_map
*isl_basic_map_drop_constraints_not_involving_dims(
3197 __isl_take isl_basic_map
*bmap
,
3198 enum isl_dim_type type
, unsigned first
, unsigned n
)
3203 isl_space
*space
= isl_basic_map_get_space(bmap
);
3204 isl_basic_map_free(bmap
);
3205 return isl_basic_map_universe(space
);
3207 bmap
= isl_basic_map_cow(bmap
);
3211 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
3212 return isl_basic_map_free(bmap
);
3214 first
+= isl_basic_map_offset(bmap
, type
) - 1;
3216 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
) {
3217 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + first
, n
) != -1)
3219 if (isl_basic_map_drop_equality(bmap
, i
) < 0)
3220 return isl_basic_map_free(bmap
);
3223 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
3224 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + 1 + first
, n
) != -1)
3226 if (isl_basic_map_drop_inequality(bmap
, i
) < 0)
3227 return isl_basic_map_free(bmap
);
3230 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
3234 /* Drop all constraints in bset that do not involve any of the dimensions
3235 * first to first + n - 1 of the given type.
3237 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_not_involving_dims(
3238 __isl_take isl_basic_set
*bset
,
3239 enum isl_dim_type type
, unsigned first
, unsigned n
)
3241 return isl_basic_map_drop_constraints_not_involving_dims(bset
,
3245 /* Drop all constraints in bmap that involve any of the dimensions
3246 * first to first + n - 1 of the given type.
3248 __isl_give isl_basic_map
*isl_basic_map_drop_constraints_involving_dims(
3249 __isl_take isl_basic_map
*bmap
,
3250 enum isl_dim_type type
, unsigned first
, unsigned n
)
3257 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
3258 return isl_basic_map_free(bmap
);
3260 bmap
= isl_basic_map_remove_divs_involving_dims(bmap
, type
, first
, n
);
3261 first
+= isl_basic_map_offset(bmap
, type
) - 1;
3262 bmap
= isl_basic_map_drop_constraints_involving(bmap
, first
, n
);
3263 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
3267 /* Drop all constraints in bset that involve any of the dimensions
3268 * first to first + n - 1 of the given type.
3270 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_involving_dims(
3271 __isl_take isl_basic_set
*bset
,
3272 enum isl_dim_type type
, unsigned first
, unsigned n
)
3274 return isl_basic_map_drop_constraints_involving_dims(bset
,
3278 /* Drop constraints from "map" by applying "drop" to each basic map.
3280 static __isl_give isl_map
*drop_constraints(__isl_take isl_map
*map
,
3281 enum isl_dim_type type
, unsigned first
, unsigned n
,
3282 __isl_give isl_basic_map
*(*drop
)(__isl_take isl_basic_map
*bmap
,
3283 enum isl_dim_type type
, unsigned first
, unsigned n
))
3287 if (isl_map_check_range(map
, type
, first
, n
) < 0)
3288 return isl_map_free(map
);
3290 map
= isl_map_cow(map
);
3294 for (i
= 0; i
< map
->n
; ++i
) {
3295 map
->p
[i
] = drop(map
->p
[i
], type
, first
, n
);
3297 return isl_map_free(map
);
3301 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
3306 /* Drop all constraints in map that involve any of the dimensions
3307 * first to first + n - 1 of the given type.
3309 __isl_give isl_map
*isl_map_drop_constraints_involving_dims(
3310 __isl_take isl_map
*map
,
3311 enum isl_dim_type type
, unsigned first
, unsigned n
)
3315 return drop_constraints(map
, type
, first
, n
,
3316 &isl_basic_map_drop_constraints_involving_dims
);
3319 /* Drop all constraints in "map" that do not involve any of the dimensions
3320 * first to first + n - 1 of the given type.
3322 __isl_give isl_map
*isl_map_drop_constraints_not_involving_dims(
3323 __isl_take isl_map
*map
,
3324 enum isl_dim_type type
, unsigned first
, unsigned n
)
3327 isl_space
*space
= isl_map_get_space(map
);
3329 return isl_map_universe(space
);
3331 return drop_constraints(map
, type
, first
, n
,
3332 &isl_basic_map_drop_constraints_not_involving_dims
);
3335 /* Drop all constraints in set that involve any of the dimensions
3336 * first to first + n - 1 of the given type.
3338 __isl_give isl_set
*isl_set_drop_constraints_involving_dims(
3339 __isl_take isl_set
*set
,
3340 enum isl_dim_type type
, unsigned first
, unsigned n
)
3342 return isl_map_drop_constraints_involving_dims(set
, type
, first
, n
);
3345 /* Drop all constraints in "set" that do not involve any of the dimensions
3346 * first to first + n - 1 of the given type.
3348 __isl_give isl_set
*isl_set_drop_constraints_not_involving_dims(
3349 __isl_take isl_set
*set
,
3350 enum isl_dim_type type
, unsigned first
, unsigned n
)
3352 return isl_map_drop_constraints_not_involving_dims(set
, type
, first
, n
);
3355 /* Does local variable "div" of "bmap" have a complete explicit representation?
3356 * Having a complete explicit representation requires not only
3357 * an explicit representation, but also that all local variables
3358 * that appear in this explicit representation in turn have
3359 * a complete explicit representation.
3361 isl_bool
isl_basic_map_div_is_known(__isl_keep isl_basic_map
*bmap
, int div
)
3364 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
3367 marked
= isl_basic_map_div_is_marked_unknown(bmap
, div
);
3368 if (marked
< 0 || marked
)
3369 return isl_bool_not(marked
);
3371 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
3374 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
3376 known
= isl_basic_map_div_is_known(bmap
, i
);
3377 if (known
< 0 || !known
)
3381 return isl_bool_true
;
3384 /* Remove all divs that are unknown or defined in terms of unknown divs.
3386 __isl_give isl_basic_map
*isl_basic_map_remove_unknown_divs(
3387 __isl_take isl_basic_map
*bmap
)
3394 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
3395 if (isl_basic_map_div_is_known(bmap
, i
))
3397 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
3406 /* Remove all divs that are unknown or defined in terms of unknown divs.
3408 __isl_give isl_basic_set
*isl_basic_set_remove_unknown_divs(
3409 __isl_take isl_basic_set
*bset
)
3411 return isl_basic_map_remove_unknown_divs(bset
);
3414 __isl_give isl_map
*isl_map_remove_unknown_divs(__isl_take isl_map
*map
)
3423 map
= isl_map_cow(map
);
3427 for (i
= 0; i
< map
->n
; ++i
) {
3428 map
->p
[i
] = isl_basic_map_remove_unknown_divs(map
->p
[i
]);
3438 __isl_give isl_set
*isl_set_remove_unknown_divs(__isl_take isl_set
*set
)
3440 return set_from_map(isl_map_remove_unknown_divs(set_to_map(set
)));
3443 __isl_give isl_basic_set
*isl_basic_set_remove_dims(
3444 __isl_take isl_basic_set
*bset
,
3445 enum isl_dim_type type
, unsigned first
, unsigned n
)
3447 isl_basic_map
*bmap
= bset_to_bmap(bset
);
3448 bmap
= isl_basic_map_remove_dims(bmap
, type
, first
, n
);
3449 return bset_from_bmap(bmap
);
3452 __isl_give isl_map
*isl_map_remove_dims(__isl_take isl_map
*map
,
3453 enum isl_dim_type type
, unsigned first
, unsigned n
)
3460 map
= isl_map_cow(map
);
3461 if (isl_map_check_range(map
, type
, first
, n
) < 0)
3462 return isl_map_free(map
);
3464 for (i
= 0; i
< map
->n
; ++i
) {
3465 map
->p
[i
] = isl_basic_map_eliminate_vars(map
->p
[i
],
3466 isl_basic_map_offset(map
->p
[i
], type
) - 1 + first
, n
);
3470 map
= isl_map_drop(map
, type
, first
, n
);
3477 __isl_give isl_set
*isl_set_remove_dims(__isl_take isl_set
*bset
,
3478 enum isl_dim_type type
, unsigned first
, unsigned n
)
3480 return set_from_map(isl_map_remove_dims(set_to_map(bset
),
3484 /* Project out n inputs starting at first using Fourier-Motzkin */
3485 __isl_give isl_map
*isl_map_remove_inputs(__isl_take isl_map
*map
,
3486 unsigned first
, unsigned n
)
3488 return isl_map_remove_dims(map
, isl_dim_in
, first
, n
);
3491 void isl_basic_set_print_internal(__isl_keep isl_basic_set
*bset
,
3492 FILE *out
, int indent
)
3497 fprintf(out
, "null basic set\n");
3501 fprintf(out
, "%*s", indent
, "");
3502 fprintf(out
, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
3503 bset
->ref
, bset
->dim
->nparam
, bset
->dim
->n_out
,
3504 bset
->extra
, bset
->flags
);
3506 p
= isl_printer_to_file(isl_basic_set_get_ctx(bset
), out
);
3507 p
= isl_printer_set_dump(p
, 1);
3508 p
= isl_printer_set_indent(p
, indent
);
3509 p
= isl_printer_start_line(p
);
3510 p
= isl_printer_print_basic_set(p
, bset
);
3511 p
= isl_printer_end_line(p
);
3512 isl_printer_free(p
);
3515 void isl_basic_map_print_internal(__isl_keep isl_basic_map
*bmap
,
3516 FILE *out
, int indent
)
3521 fprintf(out
, "null basic map\n");
3525 fprintf(out
, "%*s", indent
, "");
3526 fprintf(out
, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
3527 "flags: %x, n_name: %d\n",
3529 bmap
->dim
->nparam
, bmap
->dim
->n_in
, bmap
->dim
->n_out
,
3530 bmap
->extra
, bmap
->flags
, bmap
->dim
->n_id
);
3532 p
= isl_printer_to_file(isl_basic_map_get_ctx(bmap
), out
);
3533 p
= isl_printer_set_dump(p
, 1);
3534 p
= isl_printer_set_indent(p
, indent
);
3535 p
= isl_printer_start_line(p
);
3536 p
= isl_printer_print_basic_map(p
, bmap
);
3537 p
= isl_printer_end_line(p
);
3538 isl_printer_free(p
);
3541 __isl_give isl_basic_map
*isl_inequality_negate(__isl_take isl_basic_map
*bmap
,
3546 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
3548 return isl_basic_map_free(bmap
);
3549 if (pos
>= bmap
->n_ineq
)
3550 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
3551 "invalid position", return isl_basic_map_free(bmap
));
3552 isl_seq_neg(bmap
->ineq
[pos
], bmap
->ineq
[pos
], 1 + total
);
3553 isl_int_sub_ui(bmap
->ineq
[pos
][0], bmap
->ineq
[pos
][0], 1);
3554 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
3555 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
3559 __isl_give isl_set
*isl_set_alloc_space(__isl_take isl_space
*space
, int n
,
3562 if (isl_space_check_is_set(space
) < 0)
3564 return isl_map_alloc_space(space
, n
, flags
);
3566 isl_space_free(space
);
3570 /* Make sure "map" has room for at least "n" more basic maps.
3572 __isl_give isl_map
*isl_map_grow(__isl_take isl_map
*map
, int n
)
3575 struct isl_map
*grown
= NULL
;
3579 isl_assert(map
->ctx
, n
>= 0, goto error
);
3580 if (map
->n
+ n
<= map
->size
)
3582 grown
= isl_map_alloc_space(isl_map_get_space(map
), map
->n
+ n
, map
->flags
);
3585 for (i
= 0; i
< map
->n
; ++i
) {
3586 grown
->p
[i
] = isl_basic_map_copy(map
->p
[i
]);
3594 isl_map_free(grown
);
3599 /* Make sure "set" has room for at least "n" more basic sets.
3601 __isl_give isl_set
*isl_set_grow(__isl_take isl_set
*set
, int n
)
3603 return set_from_map(isl_map_grow(set_to_map(set
), n
));
3606 __isl_give isl_set
*isl_set_from_basic_set(__isl_take isl_basic_set
*bset
)
3608 return isl_map_from_basic_map(bset
);
3611 /* This function performs the same operation as isl_set_from_basic_set,
3612 * but is considered as a function on an isl_basic_set when exported.
3614 __isl_give isl_set
*isl_basic_set_to_set(__isl_take isl_basic_set
*bset
)
3616 return isl_set_from_basic_set(bset
);
3619 __isl_give isl_map
*isl_map_from_basic_map(__isl_take isl_basic_map
*bmap
)
3621 struct isl_map
*map
;
3626 map
= isl_map_alloc_space(isl_space_copy(bmap
->dim
), 1, ISL_MAP_DISJOINT
);
3627 return isl_map_add_basic_map(map
, bmap
);
3630 __isl_give isl_set
*isl_set_add_basic_set(__isl_take isl_set
*set
,
3631 __isl_take isl_basic_set
*bset
)
3633 return set_from_map(isl_map_add_basic_map(set_to_map(set
),
3634 bset_to_bmap(bset
)));
3637 __isl_null isl_set
*isl_set_free(__isl_take isl_set
*set
)
3639 return isl_map_free(set
);
3642 void isl_set_print_internal(__isl_keep isl_set
*set
, FILE *out
, int indent
)
3647 fprintf(out
, "null set\n");
3651 fprintf(out
, "%*s", indent
, "");
3652 fprintf(out
, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
3653 set
->ref
, set
->n
, set
->dim
->nparam
, set
->dim
->n_out
,
3655 for (i
= 0; i
< set
->n
; ++i
) {
3656 fprintf(out
, "%*s", indent
, "");
3657 fprintf(out
, "basic set %d:\n", i
);
3658 isl_basic_set_print_internal(set
->p
[i
], out
, indent
+4);
3662 void isl_map_print_internal(__isl_keep isl_map
*map
, FILE *out
, int indent
)
3667 fprintf(out
, "null map\n");
3671 fprintf(out
, "%*s", indent
, "");
3672 fprintf(out
, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
3673 "flags: %x, n_name: %d\n",
3674 map
->ref
, map
->n
, map
->dim
->nparam
, map
->dim
->n_in
,
3675 map
->dim
->n_out
, map
->flags
, map
->dim
->n_id
);
3676 for (i
= 0; i
< map
->n
; ++i
) {
3677 fprintf(out
, "%*s", indent
, "");
3678 fprintf(out
, "basic map %d:\n", i
);
3679 isl_basic_map_print_internal(map
->p
[i
], out
, indent
+4);
3683 /* Check that the space of "bset" is the same as that of the domain of "bmap".
3685 static isl_stat
isl_basic_map_check_compatible_domain(
3686 __isl_keep isl_basic_map
*bmap
, __isl_keep isl_basic_set
*bset
)
3690 ok
= isl_basic_map_compatible_domain(bmap
, bset
);
3692 return isl_stat_error
;
3694 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
3695 "incompatible spaces", return isl_stat_error
);
3700 __isl_give isl_basic_map
*isl_basic_map_intersect_domain(
3701 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*bset
)
3703 struct isl_basic_map
*bmap_domain
;
3706 if (isl_basic_map_check_equal_params(bmap
, bset_to_bmap(bset
)) < 0)
3709 dim
= isl_basic_set_dim(bset
, isl_dim_set
);
3713 isl_basic_map_check_compatible_domain(bmap
, bset
) < 0)
3716 bmap
= isl_basic_map_cow(bmap
);
3719 bmap
= isl_basic_map_extend(bmap
,
3720 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
3721 bmap_domain
= isl_basic_map_from_domain(bset
);
3722 bmap
= add_constraints(bmap
, bmap_domain
, 0, 0);
3724 bmap
= isl_basic_map_simplify(bmap
);
3725 return isl_basic_map_finalize(bmap
);
3727 isl_basic_map_free(bmap
);
3728 isl_basic_set_free(bset
);
3732 /* Check that the space of "bset" is the same as that of the range of "bmap".
3734 static isl_stat
isl_basic_map_check_compatible_range(
3735 __isl_keep isl_basic_map
*bmap
, __isl_keep isl_basic_set
*bset
)
3739 ok
= isl_basic_map_compatible_range(bmap
, bset
);
3741 return isl_stat_error
;
3743 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
3744 "incompatible spaces", return isl_stat_error
);
3749 __isl_give isl_basic_map
*isl_basic_map_intersect_range(
3750 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*bset
)
3752 struct isl_basic_map
*bmap_range
;
3755 if (isl_basic_map_check_equal_params(bmap
, bset_to_bmap(bset
)) < 0)
3758 dim
= isl_basic_set_dim(bset
, isl_dim_set
);
3761 if (dim
!= 0 && isl_basic_map_check_compatible_range(bmap
, bset
) < 0)
3764 if (isl_basic_set_plain_is_universe(bset
)) {
3765 isl_basic_set_free(bset
);
3769 bmap
= isl_basic_map_cow(bmap
);
3772 bmap
= isl_basic_map_extend(bmap
,
3773 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
3774 bmap_range
= bset_to_bmap(bset
);
3775 bmap
= add_constraints(bmap
, bmap_range
, 0, 0);
3777 bmap
= isl_basic_map_simplify(bmap
);
3778 return isl_basic_map_finalize(bmap
);
3780 isl_basic_map_free(bmap
);
3781 isl_basic_set_free(bset
);
3785 isl_bool
isl_basic_map_contains(__isl_keep isl_basic_map
*bmap
,
3786 __isl_keep isl_vec
*vec
)
3792 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
3793 if (total
< 0 || !vec
)
3794 return isl_bool_error
;
3796 if (1 + total
!= vec
->size
)
3797 return isl_bool_false
;
3801 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
3802 isl_seq_inner_product(vec
->el
, bmap
->eq
[i
], 1 + total
, &s
);
3803 if (!isl_int_is_zero(s
)) {
3805 return isl_bool_false
;
3809 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
3810 isl_seq_inner_product(vec
->el
, bmap
->ineq
[i
], 1 + total
, &s
);
3811 if (isl_int_is_neg(s
)) {
3813 return isl_bool_false
;
3819 return isl_bool_true
;
3822 isl_bool
isl_basic_set_contains(__isl_keep isl_basic_set
*bset
,
3823 __isl_keep isl_vec
*vec
)
3825 return isl_basic_map_contains(bset_to_bmap(bset
), vec
);
3828 __isl_give isl_basic_map
*isl_basic_map_intersect(
3829 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
3831 struct isl_vec
*sample
= NULL
;
3832 isl_space
*space1
, *space2
;
3833 isl_size dim1
, dim2
, nparam1
, nparam2
;
3835 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
3837 space1
= isl_basic_map_peek_space(bmap1
);
3838 space2
= isl_basic_map_peek_space(bmap2
);
3839 dim1
= isl_space_dim(space1
, isl_dim_all
);
3840 dim2
= isl_space_dim(space2
, isl_dim_all
);
3841 nparam1
= isl_space_dim(space1
, isl_dim_param
);
3842 nparam2
= isl_space_dim(space2
, isl_dim_param
);
3843 if (dim1
< 0 || dim2
< 0 || nparam1
< 0 || nparam2
< 0)
3845 if (dim1
== nparam1
&& dim2
!= nparam2
)
3846 return isl_basic_map_intersect(bmap2
, bmap1
);
3848 if (dim2
!= nparam2
&&
3849 isl_basic_map_check_equal_space(bmap1
, bmap2
) < 0)
3852 if (isl_basic_map_plain_is_empty(bmap1
)) {
3853 isl_basic_map_free(bmap2
);
3856 if (isl_basic_map_plain_is_empty(bmap2
)) {
3857 isl_basic_map_free(bmap1
);
3861 if (bmap1
->sample
&&
3862 isl_basic_map_contains(bmap1
, bmap1
->sample
) > 0 &&
3863 isl_basic_map_contains(bmap2
, bmap1
->sample
) > 0)
3864 sample
= isl_vec_copy(bmap1
->sample
);
3865 else if (bmap2
->sample
&&
3866 isl_basic_map_contains(bmap1
, bmap2
->sample
) > 0 &&
3867 isl_basic_map_contains(bmap2
, bmap2
->sample
) > 0)
3868 sample
= isl_vec_copy(bmap2
->sample
);
3870 bmap1
= isl_basic_map_cow(bmap1
);
3873 bmap1
= isl_basic_map_extend(bmap1
,
3874 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
3875 bmap1
= add_constraints(bmap1
, bmap2
, 0, 0);
3878 isl_vec_free(sample
);
3880 isl_vec_free(bmap1
->sample
);
3881 bmap1
->sample
= sample
;
3884 bmap1
= isl_basic_map_simplify(bmap1
);
3885 return isl_basic_map_finalize(bmap1
);
3888 isl_vec_free(sample
);
3889 isl_basic_map_free(bmap1
);
3890 isl_basic_map_free(bmap2
);
3894 __isl_give isl_basic_set
*isl_basic_set_intersect(
3895 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
3897 return bset_from_bmap(isl_basic_map_intersect(bset_to_bmap(bset1
),
3898 bset_to_bmap(bset2
)));
3901 /* Intersect the parameter domain of "bmap" with "bset".
3903 * isl_basic_map_intersect handles this as a special case.
3905 __isl_give isl_basic_map
*isl_basic_map_intersect_params(
3906 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*bset
)
3908 return isl_basic_map_intersect(bmap
, bset
);
3911 __isl_give isl_basic_set
*isl_basic_set_intersect_params(
3912 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
3914 isl_basic_map
*bmap
;
3916 bmap
= bset_to_bmap(bset1
);
3917 bmap
= isl_basic_map_intersect_params(bmap
, bset2
);
3918 return bset_from_bmap(bmap
);
3921 /* Does "map" consist of a single disjunct, without any local variables?
3923 static isl_bool
is_convex_no_locals(__isl_keep isl_map
*map
)
3928 return isl_bool_error
;
3930 return isl_bool_false
;
3931 n_div
= isl_basic_map_dim(map
->p
[0], isl_dim_div
);
3933 return isl_bool_error
;
3935 return isl_bool_false
;
3936 return isl_bool_true
;
3939 /* Check that "map" consists of a single disjunct, without any local variables.
3941 static isl_stat
check_convex_no_locals(__isl_keep isl_map
*map
)
3945 ok
= is_convex_no_locals(map
);
3947 return isl_stat_error
;
3951 isl_die(isl_map_get_ctx(map
), isl_error_internal
,
3952 "unexpectedly not convex or involving local variables",
3953 return isl_stat_error
);
3956 /* Special case of isl_map_intersect, where both map1 and map2
3957 * are convex, without any divs and such that either map1 or map2
3958 * contains a single constraint. This constraint is then simply
3959 * added to the other map.
3961 static __isl_give isl_map
*map_intersect_add_constraint(
3962 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
3964 if (check_convex_no_locals(map1
) < 0 ||
3965 check_convex_no_locals(map2
) < 0)
3968 if (map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
!= 1)
3969 return isl_map_intersect(map2
, map1
);
3971 map1
= isl_map_cow(map1
);
3974 if (isl_map_plain_is_empty(map1
)) {
3978 if (map2
->p
[0]->n_eq
== 1)
3979 map1
->p
[0] = isl_basic_map_add_eq(map1
->p
[0], map2
->p
[0]->eq
[0]);
3981 map1
->p
[0] = isl_basic_map_add_ineq(map1
->p
[0],
3982 map2
->p
[0]->ineq
[0]);
3984 map1
->p
[0] = isl_basic_map_simplify(map1
->p
[0]);
3985 map1
->p
[0] = isl_basic_map_finalize(map1
->p
[0]);
3989 if (isl_basic_map_plain_is_empty(map1
->p
[0])) {
3990 isl_basic_map_free(map1
->p
[0]);
3996 map1
= isl_map_unmark_normalized(map1
);
4004 /* map2 may be either a parameter domain or a map living in the same
4007 static __isl_give isl_map
*map_intersect_internal(__isl_take isl_map
*map1
,
4008 __isl_take isl_map
*map2
)
4014 isl_size dim2
, nparam2
;
4019 if ((isl_map_plain_is_empty(map1
) ||
4020 isl_map_plain_is_universe(map2
)) &&
4021 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
4025 if ((isl_map_plain_is_empty(map2
) ||
4026 isl_map_plain_is_universe(map1
)) &&
4027 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
4032 if (is_convex_no_locals(map1
) == isl_bool_true
&&
4033 is_convex_no_locals(map2
) == isl_bool_true
&&
4034 isl_space_is_equal(map1
->dim
, map2
->dim
) &&
4035 (map1
->p
[0]->n_eq
+ map1
->p
[0]->n_ineq
== 1 ||
4036 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1))
4037 return map_intersect_add_constraint(map1
, map2
);
4039 equal
= isl_map_plain_is_equal(map1
, map2
);
4047 dim2
= isl_map_dim(map2
, isl_dim_all
);
4048 nparam2
= isl_map_dim(map2
, isl_dim_param
);
4049 if (dim2
< 0 || nparam2
< 0)
4051 if (dim2
!= nparam2
)
4052 isl_assert(map1
->ctx
,
4053 isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
4055 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
4056 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
4057 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
4059 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
4060 map1
->n
* map2
->n
, flags
);
4063 for (i
= 0; i
< map1
->n
; ++i
)
4064 for (j
= 0; j
< map2
->n
; ++j
) {
4065 struct isl_basic_map
*part
;
4066 part
= isl_basic_map_intersect(
4067 isl_basic_map_copy(map1
->p
[i
]),
4068 isl_basic_map_copy(map2
->p
[j
]));
4069 if (isl_basic_map_is_empty(part
) < 0)
4070 part
= isl_basic_map_free(part
);
4071 result
= isl_map_add_basic_map(result
, part
);
4084 static __isl_give isl_map
*map_intersect(__isl_take isl_map
*map1
,
4085 __isl_take isl_map
*map2
)
4087 if (isl_map_check_equal_space(map1
, map2
) < 0)
4089 return map_intersect_internal(map1
, map2
);
4096 __isl_give isl_map
*isl_map_intersect(__isl_take isl_map
*map1
,
4097 __isl_take isl_map
*map2
)
4099 isl_map_align_params_bin(&map1
, &map2
);
4100 return map_intersect(map1
, map2
);
4103 __isl_give isl_set
*isl_set_intersect(__isl_take isl_set
*set1
,
4104 __isl_take isl_set
*set2
)
4106 return set_from_map(isl_map_intersect(set_to_map(set1
),
4110 /* map_intersect_internal accepts intersections
4111 * with parameter domains, so we can just call that function.
4113 __isl_give isl_map
*isl_map_intersect_params(__isl_take isl_map
*map
,
4114 __isl_take isl_set
*params
)
4116 isl_map_align_params_set(&map
, ¶ms
);
4117 return map_intersect_internal(map
, params
);
4120 __isl_give isl_set
*isl_set_intersect_params(__isl_take isl_set
*set
,
4121 __isl_take isl_set
*params
)
4123 return isl_map_intersect_params(set
, params
);
4126 __isl_give isl_basic_map
*isl_basic_map_reverse(__isl_take isl_basic_map
*bmap
)
4134 bmap
= isl_basic_map_cow(bmap
);
4137 space
= isl_space_reverse(isl_space_copy(bmap
->dim
));
4138 pos
= isl_basic_map_offset(bmap
, isl_dim_in
);
4139 n1
= isl_basic_map_dim(bmap
, isl_dim_in
);
4140 n2
= isl_basic_map_dim(bmap
, isl_dim_out
);
4141 if (n1
< 0 || n2
< 0)
4142 bmap
= isl_basic_map_free(bmap
);
4143 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
4144 return isl_basic_map_reset_space(bmap
, space
);
4147 /* Given a basic map where the tuple of type "type" is a wrapped map,
4148 * swap domain and range of that wrapped map.
4150 static __isl_give isl_basic_map
*isl_basic_map_reverse_wrapped(
4151 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
)
4154 isl_size offset
, n1
, n2
;
4156 space
= isl_basic_map_peek_space(bmap
);
4157 offset
= isl_basic_map_var_offset(bmap
, type
);
4158 n1
= isl_space_wrapped_dim(space
, type
, isl_dim_in
);
4159 n2
= isl_space_wrapped_dim(space
, type
, isl_dim_out
);
4160 if (offset
< 0 || n1
< 0 || n2
< 0)
4161 return isl_basic_map_free(bmap
);
4163 bmap
= isl_basic_map_swap_vars(bmap
, 1 + offset
, n1
, n2
);
4165 space
= isl_basic_map_take_space(bmap
);
4166 space
= isl_space_reverse_wrapped(space
, type
);
4167 bmap
= isl_basic_map_restore_space(bmap
, space
);
4172 /* Given a basic map (A -> B) -> C, return the corresponding basic map
4175 static __isl_give isl_basic_map
*isl_basic_map_domain_reverse(
4176 __isl_take isl_basic_map
*bmap
)
4180 space
= isl_basic_map_peek_space(bmap
);
4181 if (isl_space_check_domain_is_wrapping(space
) < 0)
4182 return isl_basic_map_free(bmap
);
4183 bmap
= isl_basic_map_reverse_wrapped(bmap
, isl_dim_in
);
4188 /* Given a basic map A -> (B -> C), return the corresponding basic map
4191 static __isl_give isl_basic_map
*isl_basic_map_range_reverse(
4192 __isl_take isl_basic_map
*bmap
)
4196 space
= isl_basic_map_peek_space(bmap
);
4197 if (isl_space_check_range_is_wrapping(space
) < 0)
4198 return isl_basic_map_free(bmap
);
4199 bmap
= isl_basic_map_reverse_wrapped(bmap
, isl_dim_out
);
4204 /* Given a basic map that is actually a basic set (A -> B),
4205 * return the corresponding basic set (B -> A) as a basic map.
4207 static __isl_give isl_basic_map
*isl_basic_map_set_reverse(
4208 __isl_take isl_basic_map
*bmap
)
4212 space
= isl_basic_map_peek_space(bmap
);
4213 if (isl_space_check_is_wrapping(space
) < 0)
4214 return isl_basic_map_free(bmap
);
4215 bmap
= isl_basic_map_reverse_wrapped(bmap
, isl_dim_set
);
4220 static __isl_give isl_basic_map
*basic_map_space_reset(
4221 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
)
4227 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
4230 space
= isl_basic_map_get_space(bmap
);
4231 space
= isl_space_reset(space
, type
);
4232 bmap
= isl_basic_map_reset_space(bmap
, space
);
4236 __isl_give isl_basic_map
*isl_basic_map_insert_dims(
4237 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
4238 unsigned pos
, unsigned n
)
4240 isl_bool rational
, is_empty
;
4241 isl_space
*res_space
;
4242 struct isl_basic_map
*res
;
4243 struct isl_dim_map
*dim_map
;
4246 enum isl_dim_type t
;
4249 return basic_map_space_reset(bmap
, type
);
4251 is_empty
= isl_basic_map_plain_is_empty(bmap
);
4252 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4253 if (is_empty
< 0 || total
< 0)
4254 return isl_basic_map_free(bmap
);
4255 res_space
= isl_space_insert_dims(isl_basic_map_get_space(bmap
),
4258 return isl_basic_map_free(bmap
);
4260 isl_basic_map_free(bmap
);
4261 return isl_basic_map_empty(res_space
);
4264 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
+ n
);
4266 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
4270 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
4272 isl_size size
= isl_basic_map_dim(bmap
, t
);
4274 dim_map
= isl_dim_map_free(dim_map
);
4275 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
4277 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
4278 pos
, size
- pos
, off
+ pos
+ n
);
4280 dim
= isl_space_dim(res_space
, t
);
4282 dim_map
= isl_dim_map_free(dim_map
);
4285 isl_dim_map_div(dim_map
, bmap
, off
);
4287 res
= isl_basic_map_alloc_space(res_space
,
4288 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
4289 rational
= isl_basic_map_is_rational(bmap
);
4291 res
= isl_basic_map_free(res
);
4293 res
= isl_basic_map_set_rational(res
);
4294 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
4295 return isl_basic_map_finalize(res
);
4298 __isl_give isl_basic_set
*isl_basic_set_insert_dims(
4299 __isl_take isl_basic_set
*bset
,
4300 enum isl_dim_type type
, unsigned pos
, unsigned n
)
4302 return isl_basic_map_insert_dims(bset
, type
, pos
, n
);
4305 __isl_give isl_basic_map
*isl_basic_map_add_dims(__isl_take isl_basic_map
*bmap
,
4306 enum isl_dim_type type
, unsigned n
)
4310 dim
= isl_basic_map_dim(bmap
, type
);
4312 return isl_basic_map_free(bmap
);
4313 return isl_basic_map_insert_dims(bmap
, type
, dim
, n
);
4316 __isl_give isl_basic_set
*isl_basic_set_add_dims(__isl_take isl_basic_set
*bset
,
4317 enum isl_dim_type type
, unsigned n
)
4321 isl_assert(bset
->ctx
, type
!= isl_dim_in
, goto error
);
4322 return isl_basic_map_add_dims(bset
, type
, n
);
4324 isl_basic_set_free(bset
);
4328 static __isl_give isl_map
*map_space_reset(__isl_take isl_map
*map
,
4329 enum isl_dim_type type
)
4333 if (!map
|| !isl_space_is_named_or_nested(map
->dim
, type
))
4336 space
= isl_map_get_space(map
);
4337 space
= isl_space_reset(space
, type
);
4338 map
= isl_map_reset_space(map
, space
);
4342 __isl_give isl_map
*isl_map_insert_dims(__isl_take isl_map
*map
,
4343 enum isl_dim_type type
, unsigned pos
, unsigned n
)
4349 return map_space_reset(map
, type
);
4351 map
= isl_map_cow(map
);
4355 for (i
= 0; i
< map
->n
; ++i
) {
4356 map
->p
[i
] = isl_basic_map_insert_dims(map
->p
[i
], type
, pos
, n
);
4361 space
= isl_map_take_space(map
);
4362 space
= isl_space_insert_dims(space
, type
, pos
, n
);
4363 map
= isl_map_restore_space(map
, space
);
4371 __isl_give isl_set
*isl_set_insert_dims(__isl_take isl_set
*set
,
4372 enum isl_dim_type type
, unsigned pos
, unsigned n
)
4374 return isl_map_insert_dims(set
, type
, pos
, n
);
4377 __isl_give isl_map
*isl_map_add_dims(__isl_take isl_map
*map
,
4378 enum isl_dim_type type
, unsigned n
)
4382 dim
= isl_map_dim(map
, type
);
4384 return isl_map_free(map
);
4385 return isl_map_insert_dims(map
, type
, dim
, n
);
4388 __isl_give isl_set
*isl_set_add_dims(__isl_take isl_set
*set
,
4389 enum isl_dim_type type
, unsigned n
)
4393 isl_assert(set
->ctx
, type
!= isl_dim_in
, goto error
);
4394 return set_from_map(isl_map_add_dims(set_to_map(set
), type
, n
));
4400 __isl_give isl_basic_map
*isl_basic_map_move_dims(
4401 __isl_take isl_basic_map
*bmap
,
4402 enum isl_dim_type dst_type
, unsigned dst_pos
,
4403 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
4406 struct isl_dim_map
*dim_map
;
4407 struct isl_basic_map
*res
;
4408 enum isl_dim_type t
;
4415 bmap
= isl_basic_map_reset(bmap
, src_type
);
4416 bmap
= isl_basic_map_reset(bmap
, dst_type
);
4420 if (isl_basic_map_check_range(bmap
, src_type
, src_pos
, n
) < 0)
4421 return isl_basic_map_free(bmap
);
4423 if (dst_type
== src_type
&& dst_pos
== src_pos
)
4426 isl_assert(bmap
->ctx
, dst_type
!= src_type
, goto error
);
4428 if (pos(bmap
->dim
, dst_type
) + dst_pos
==
4429 pos(bmap
->dim
, src_type
) + src_pos
+
4430 ((src_type
< dst_type
) ? n
: 0)) {
4431 space
= isl_basic_map_take_space(bmap
);
4432 space
= isl_space_move_dims(space
, dst_type
, dst_pos
,
4433 src_type
, src_pos
, n
);
4434 bmap
= isl_basic_map_restore_space(bmap
, space
);
4435 bmap
= isl_basic_map_finalize(bmap
);
4440 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4442 return isl_basic_map_free(bmap
);
4443 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
4446 space
= isl_basic_map_peek_space(bmap
);
4447 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
4448 isl_size size
= isl_space_dim(space
, t
);
4450 dim_map
= isl_dim_map_free(dim_map
);
4451 if (t
== dst_type
) {
4452 isl_dim_map_dim_range(dim_map
, space
, t
,
4455 isl_dim_map_dim_range(dim_map
, space
, src_type
,
4458 isl_dim_map_dim_range(dim_map
, space
, t
,
4459 dst_pos
, size
- dst_pos
, off
);
4460 off
+= size
- dst_pos
;
4461 } else if (t
== src_type
) {
4462 isl_dim_map_dim_range(dim_map
, space
, t
,
4465 isl_dim_map_dim_range(dim_map
, space
, t
,
4466 src_pos
+ n
, size
- src_pos
- n
, off
);
4467 off
+= size
- src_pos
- n
;
4469 isl_dim_map_dim(dim_map
, space
, t
, off
);
4473 isl_dim_map_div(dim_map
, bmap
, off
);
4475 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
4476 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
4477 bmap
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
4478 space
= isl_basic_map_take_space(bmap
);
4479 space
= isl_space_move_dims(space
, dst_type
, dst_pos
,
4480 src_type
, src_pos
, n
);
4481 bmap
= isl_basic_map_restore_space(bmap
, space
);
4485 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
4486 bmap
= isl_basic_map_gauss(bmap
, NULL
);
4487 bmap
= isl_basic_map_finalize(bmap
);
4491 isl_basic_map_free(bmap
);
4495 __isl_give isl_basic_set
*isl_basic_set_move_dims(__isl_take isl_basic_set
*bset
,
4496 enum isl_dim_type dst_type
, unsigned dst_pos
,
4497 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
4499 isl_basic_map
*bmap
= bset_to_bmap(bset
);
4500 bmap
= isl_basic_map_move_dims(bmap
, dst_type
, dst_pos
,
4501 src_type
, src_pos
, n
);
4502 return bset_from_bmap(bmap
);
4505 __isl_give isl_set
*isl_set_move_dims(__isl_take isl_set
*set
,
4506 enum isl_dim_type dst_type
, unsigned dst_pos
,
4507 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
4511 isl_assert(set
->ctx
, dst_type
!= isl_dim_in
, goto error
);
4512 return set_from_map(isl_map_move_dims(set_to_map(set
),
4513 dst_type
, dst_pos
, src_type
, src_pos
, n
));
4519 __isl_give isl_map
*isl_map_move_dims(__isl_take isl_map
*map
,
4520 enum isl_dim_type dst_type
, unsigned dst_pos
,
4521 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
4527 map
= isl_map_reset(map
, src_type
);
4528 map
= isl_map_reset(map
, dst_type
);
4532 if (isl_map_check_range(map
, src_type
, src_pos
, n
))
4533 return isl_map_free(map
);
4535 if (dst_type
== src_type
&& dst_pos
== src_pos
)
4538 isl_assert(map
->ctx
, dst_type
!= src_type
, goto error
);
4540 map
= isl_map_cow(map
);
4544 for (i
= 0; i
< map
->n
; ++i
) {
4545 map
->p
[i
] = isl_basic_map_move_dims(map
->p
[i
],
4547 src_type
, src_pos
, n
);
4552 space
= isl_map_take_space(map
);
4553 space
= isl_space_move_dims(space
, dst_type
, dst_pos
,
4554 src_type
, src_pos
, n
);
4555 map
= isl_map_restore_space(map
, space
);
4563 /* Move the specified dimensions to the last columns right before
4564 * the divs. Don't change the dimension specification of bmap.
4565 * That's the responsibility of the caller.
4567 static __isl_give isl_basic_map
*move_last(__isl_take isl_basic_map
*bmap
,
4568 enum isl_dim_type type
, unsigned first
, unsigned n
)
4571 struct isl_dim_map
*dim_map
;
4572 struct isl_basic_map
*res
;
4573 enum isl_dim_type t
;
4579 if (isl_basic_map_offset(bmap
, type
) + first
+ n
==
4580 isl_basic_map_offset(bmap
, isl_dim_div
))
4583 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4585 return isl_basic_map_free(bmap
);
4586 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
4589 space
= isl_basic_map_peek_space(bmap
);
4590 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
4591 isl_size size
= isl_space_dim(space
, t
);
4593 dim_map
= isl_dim_map_free(dim_map
);
4595 isl_dim_map_dim_range(dim_map
, space
, t
,
4598 isl_dim_map_dim_range(dim_map
, space
, t
,
4599 first
, n
, total
- bmap
->n_div
- n
);
4600 isl_dim_map_dim_range(dim_map
, space
, t
,
4601 first
+ n
, size
- (first
+ n
), off
);
4602 off
+= size
- (first
+ n
);
4604 isl_dim_map_dim(dim_map
, space
, t
, off
);
4608 isl_dim_map_div(dim_map
, bmap
, off
+ n
);
4610 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
4611 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
4612 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
4616 /* Insert "n" rows in the divs of "bmap".
4618 * The number of columns is not changed, which means that the last
4619 * dimensions of "bmap" are being reintepreted as the new divs.
4620 * The space of "bmap" is not adjusted, however, which means
4621 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
4622 * from the space of "bmap" is the responsibility of the caller.
4624 static __isl_give isl_basic_map
*insert_div_rows(__isl_take isl_basic_map
*bmap
,
4632 bmap
= isl_basic_map_cow(bmap
);
4636 row_size
= isl_basic_map_offset(bmap
, isl_dim_div
) + bmap
->extra
;
4637 old
= bmap
->block2
.data
;
4638 bmap
->block2
= isl_blk_extend(bmap
->ctx
, bmap
->block2
,
4639 (bmap
->extra
+ n
) * (1 + row_size
));
4640 if (!bmap
->block2
.data
)
4641 return isl_basic_map_free(bmap
);
4642 new_div
= isl_alloc_array(bmap
->ctx
, isl_int
*, bmap
->extra
+ n
);
4644 return isl_basic_map_free(bmap
);
4645 for (i
= 0; i
< n
; ++i
) {
4646 new_div
[i
] = bmap
->block2
.data
+
4647 (bmap
->extra
+ i
) * (1 + row_size
);
4648 isl_seq_clr(new_div
[i
], 1 + row_size
);
4650 for (i
= 0; i
< bmap
->extra
; ++i
)
4651 new_div
[n
+ i
] = bmap
->block2
.data
+ (bmap
->div
[i
] - old
);
4653 bmap
->div
= new_div
;
4660 /* Drop constraints from "bmap" that only involve the variables
4661 * of "type" in the range [first, first + n] that are not related
4662 * to any of the variables outside that interval.
4663 * These constraints cannot influence the values for the variables
4664 * outside the interval, except in case they cause "bmap" to be empty.
4665 * Only drop the constraints if "bmap" is known to be non-empty.
4667 static __isl_give isl_basic_map
*drop_irrelevant_constraints(
4668 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
4669 unsigned first
, unsigned n
)
4673 isl_size dim
, n_div
;
4676 non_empty
= isl_basic_map_plain_is_non_empty(bmap
);
4678 return isl_basic_map_free(bmap
);
4682 dim
= isl_basic_map_dim(bmap
, isl_dim_all
);
4683 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4684 if (dim
< 0 || n_div
< 0)
4685 return isl_basic_map_free(bmap
);
4686 groups
= isl_calloc_array(isl_basic_map_get_ctx(bmap
), int, dim
);
4688 return isl_basic_map_free(bmap
);
4689 first
+= isl_basic_map_offset(bmap
, type
) - 1;
4690 for (i
= 0; i
< first
; ++i
)
4692 for (i
= first
+ n
; i
< dim
- n_div
; ++i
)
4695 bmap
= isl_basic_map_drop_unrelated_constraints(bmap
, groups
);
4700 /* Turn the n dimensions of type type, starting at first
4701 * into existentially quantified variables.
4703 * If a subset of the projected out variables are unrelated
4704 * to any of the variables that remain, then the constraints
4705 * involving this subset are simply dropped first.
4707 __isl_give isl_basic_map
*isl_basic_map_project_out(
4708 __isl_take isl_basic_map
*bmap
,
4709 enum isl_dim_type type
, unsigned first
, unsigned n
)
4715 return basic_map_space_reset(bmap
, type
);
4716 if (type
== isl_dim_div
)
4717 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
4718 "cannot project out existentially quantified variables",
4719 return isl_basic_map_free(bmap
));
4721 empty
= isl_basic_map_plain_is_empty(bmap
);
4723 return isl_basic_map_free(bmap
);
4725 bmap
= isl_basic_map_set_to_empty(bmap
);
4727 bmap
= drop_irrelevant_constraints(bmap
, type
, first
, n
);
4731 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
4732 return isl_basic_map_remove_dims(bmap
, type
, first
, n
);
4734 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
4735 return isl_basic_map_free(bmap
);
4737 bmap
= move_last(bmap
, type
, first
, n
);
4738 bmap
= isl_basic_map_cow(bmap
);
4739 bmap
= insert_div_rows(bmap
, n
);
4741 space
= isl_basic_map_take_space(bmap
);
4742 space
= isl_space_drop_dims(space
, type
, first
, n
);
4743 bmap
= isl_basic_map_restore_space(bmap
, space
);
4744 bmap
= isl_basic_map_simplify(bmap
);
4745 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
4746 return isl_basic_map_finalize(bmap
);
4749 /* Turn the n dimensions of type type, starting at first
4750 * into existentially quantified variables.
4752 __isl_give isl_basic_set
*isl_basic_set_project_out(
4753 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
,
4754 unsigned first
, unsigned n
)
4756 return bset_from_bmap(isl_basic_map_project_out(bset_to_bmap(bset
),
4760 /* Turn the n dimensions of type type, starting at first
4761 * into existentially quantified variables.
4763 __isl_give isl_map
*isl_map_project_out(__isl_take isl_map
*map
,
4764 enum isl_dim_type type
, unsigned first
, unsigned n
)
4770 return map_space_reset(map
, type
);
4772 if (isl_map_check_range(map
, type
, first
, n
) < 0)
4773 return isl_map_free(map
);
4775 map
= isl_map_cow(map
);
4779 for (i
= 0; i
< map
->n
; ++i
) {
4780 map
->p
[i
] = isl_basic_map_project_out(map
->p
[i
], type
, first
, n
);
4786 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
4787 map
= isl_map_unmark_normalized(map
);
4789 space
= isl_map_take_space(map
);
4790 space
= isl_space_drop_dims(space
, type
, first
, n
);
4791 map
= isl_map_restore_space(map
, space
);
4800 #define TYPE isl_map
4801 #include "isl_project_out_all_params_templ.c"
4802 #include "isl_project_out_param_templ.c"
4804 /* Turn all the dimensions of type "type", except the "n" starting at "first"
4805 * into existentially quantified variables.
4807 __isl_give isl_map
*isl_map_project_onto(__isl_take isl_map
*map
,
4808 enum isl_dim_type type
, unsigned first
, unsigned n
)
4812 dim
= isl_map_dim(map
, type
);
4813 if (isl_map_check_range(map
, type
, first
, n
) < 0 || dim
< 0)
4814 return isl_map_free(map
);
4815 map
= isl_map_project_out(map
, type
, first
+ n
, dim
- (first
+ n
));
4816 map
= isl_map_project_out(map
, type
, 0, first
);
4820 /* Turn the n dimensions of type type, starting at first
4821 * into existentially quantified variables.
4823 __isl_give isl_set
*isl_set_project_out(__isl_take isl_set
*set
,
4824 enum isl_dim_type type
, unsigned first
, unsigned n
)
4826 return set_from_map(isl_map_project_out(set_to_map(set
),
4830 /* If "set" involves a parameter with identifier "id",
4831 * then turn it into an existentially quantified variable.
4833 __isl_give isl_set
*isl_set_project_out_param_id(__isl_take isl_set
*set
,
4834 __isl_take isl_id
*id
)
4836 return set_from_map(isl_map_project_out_param_id(set_to_map(set
), id
));
4839 /* If "set" involves any of the parameters with identifiers in "list",
4840 * then turn them into existentially quantified variables.
4842 __isl_give isl_set
*isl_set_project_out_param_id_list(__isl_take isl_set
*set
,
4843 __isl_take isl_id_list
*list
)
4847 map
= set_to_map(set
);
4848 map
= isl_map_project_out_param_id_list(map
, list
);
4849 return set_from_map(map
);
4852 /* Project out all parameters from "set" by existentially quantifying
4855 __isl_give isl_set
*isl_set_project_out_all_params(__isl_take isl_set
*set
)
4857 return set_from_map(isl_map_project_out_all_params(set_to_map(set
)));
4860 /* Return a map that projects the elements in "set" onto their
4861 * "n" set dimensions starting at "first".
4862 * "type" should be equal to isl_dim_set.
4864 __isl_give isl_map
*isl_set_project_onto_map(__isl_take isl_set
*set
,
4865 enum isl_dim_type type
, unsigned first
, unsigned n
)
4870 if (type
!= isl_dim_set
)
4871 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
4872 "only set dimensions can be projected out", goto error
);
4873 if (isl_set_check_range(set
, type
, first
, n
) < 0)
4874 return isl_set_free(set
);
4876 map
= isl_map_from_domain(set
);
4877 map
= isl_map_add_dims(map
, isl_dim_out
, n
);
4878 for (i
= 0; i
< n
; ++i
)
4879 map
= isl_map_equate(map
, isl_dim_in
, first
+ i
,
4887 static __isl_give isl_basic_map
*add_divs(__isl_take isl_basic_map
*bmap
,
4893 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4895 return isl_basic_map_free(bmap
);
4896 for (i
= 0; i
< n
; ++i
) {
4897 j
= isl_basic_map_alloc_div(bmap
);
4900 isl_seq_clr(bmap
->div
[j
], 1 + 1 + total
);
4904 isl_basic_map_free(bmap
);
4908 /* Does "bmap2" apply to the range of "bmap1" (ignoring parameters)?
4910 isl_bool
isl_basic_map_applies_range(__isl_keep isl_basic_map
*bmap1
,
4911 __isl_keep isl_basic_map
*bmap2
)
4913 isl_space
*space1
, *space2
;
4915 space1
= isl_basic_map_peek_space(bmap1
);
4916 space2
= isl_basic_map_peek_space(bmap2
);
4917 return isl_space_tuple_is_equal(space1
, isl_dim_out
,
4918 space2
, isl_dim_in
);
4921 /* Check that "bmap2" applies to the range of "bmap1" (ignoring parameters).
4923 static isl_stat
isl_basic_map_check_applies_range(
4924 __isl_keep isl_basic_map
*bmap1
, __isl_keep isl_basic_map
*bmap2
)
4928 equal
= isl_basic_map_applies_range(bmap1
, bmap2
);
4930 return isl_stat_error
;
4932 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
4933 "spaces don't match", return isl_stat_error
);
4937 __isl_give isl_basic_map
*isl_basic_map_apply_range(
4938 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
4940 isl_space
*space_result
= NULL
;
4941 struct isl_basic_map
*bmap
;
4942 isl_size n_in
, n_out
, n
, nparam
;
4943 unsigned total
, pos
;
4944 struct isl_dim_map
*dim_map1
, *dim_map2
;
4946 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
4948 if (isl_basic_map_check_applies_range(bmap1
, bmap2
) < 0)
4951 n_in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
4952 n_out
= isl_basic_map_dim(bmap2
, isl_dim_out
);
4953 n
= isl_basic_map_dim(bmap1
, isl_dim_out
);
4954 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
4955 if (n_in
< 0 || n_out
< 0 || n
< 0 || nparam
< 0)
4958 space_result
= isl_space_join(isl_basic_map_get_space(bmap1
),
4959 isl_basic_map_get_space(bmap2
));
4961 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ n
;
4962 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4963 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4964 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
4965 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
4966 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
4967 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_in
);
4968 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_out
);
4969 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
4970 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
4971 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
4973 bmap
= isl_basic_map_alloc_space(space_result
,
4974 bmap1
->n_div
+ bmap2
->n_div
+ n
,
4975 bmap1
->n_eq
+ bmap2
->n_eq
,
4976 bmap1
->n_ineq
+ bmap2
->n_ineq
);
4977 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
4978 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
4979 bmap
= add_divs(bmap
, n
);
4980 bmap
= isl_basic_map_simplify(bmap
);
4981 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
4982 return isl_basic_map_finalize(bmap
);
4984 isl_basic_map_free(bmap1
);
4985 isl_basic_map_free(bmap2
);
4989 __isl_give isl_basic_set
*isl_basic_set_apply(__isl_take isl_basic_set
*bset
,
4990 __isl_take isl_basic_map
*bmap
)
4992 if (isl_basic_map_check_compatible_domain(bmap
, bset
) < 0)
4995 return bset_from_bmap(isl_basic_map_apply_range(bset_to_bmap(bset
),
4998 isl_basic_set_free(bset
);
4999 isl_basic_map_free(bmap
);
5003 __isl_give isl_basic_map
*isl_basic_map_apply_domain(
5004 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
5006 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
5008 if (!isl_space_tuple_is_equal(bmap1
->dim
, isl_dim_in
,
5009 bmap2
->dim
, isl_dim_in
))
5010 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
5011 "spaces don't match", goto error
);
5013 bmap1
= isl_basic_map_reverse(bmap1
);
5014 bmap1
= isl_basic_map_apply_range(bmap1
, bmap2
);
5015 return isl_basic_map_reverse(bmap1
);
5017 isl_basic_map_free(bmap1
);
5018 isl_basic_map_free(bmap2
);
5022 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
5023 * A \cap B -> f(A) + f(B)
5025 __isl_give isl_basic_map
*isl_basic_map_sum(__isl_take isl_basic_map
*bmap1
,
5026 __isl_take isl_basic_map
*bmap2
)
5028 isl_size n_in
, n_out
, nparam
;
5029 unsigned total
, pos
;
5030 struct isl_basic_map
*bmap
= NULL
;
5031 struct isl_dim_map
*dim_map1
, *dim_map2
;
5034 if (isl_basic_map_check_equal_space(bmap1
, bmap2
) < 0)
5037 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
5038 n_in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
5039 n_out
= isl_basic_map_dim(bmap1
, isl_dim_out
);
5040 if (nparam
< 0 || n_in
< 0 || n_out
< 0)
5043 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
;
5044 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
5045 dim_map2
= isl_dim_map_alloc(bmap2
->ctx
, total
);
5046 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
5047 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
);
5048 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
5049 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
5050 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_in
+ n_out
);
5051 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
5052 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
5053 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_out
);
5055 bmap
= isl_basic_map_alloc_space(isl_space_copy(bmap1
->dim
),
5056 bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
,
5057 bmap1
->n_eq
+ bmap2
->n_eq
+ n_out
,
5058 bmap1
->n_ineq
+ bmap2
->n_ineq
);
5059 for (i
= 0; i
< n_out
; ++i
) {
5060 int j
= isl_basic_map_alloc_equality(bmap
);
5063 isl_seq_clr(bmap
->eq
[j
], 1+total
);
5064 isl_int_set_si(bmap
->eq
[j
][1+nparam
+n_in
+i
], -1);
5065 isl_int_set_si(bmap
->eq
[j
][1+pos
+i
], 1);
5066 isl_int_set_si(bmap
->eq
[j
][1+pos
-n_out
+i
], 1);
5068 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
5069 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
5070 bmap
= add_divs(bmap
, 2 * n_out
);
5072 bmap
= isl_basic_map_simplify(bmap
);
5073 return isl_basic_map_finalize(bmap
);
5075 isl_basic_map_free(bmap
);
5076 isl_basic_map_free(bmap1
);
5077 isl_basic_map_free(bmap2
);
5081 /* Given two maps A -> f(A) and B -> g(B), construct a map
5082 * A \cap B -> f(A) + f(B)
5084 __isl_give isl_map
*isl_map_sum(__isl_take isl_map
*map1
,
5085 __isl_take isl_map
*map2
)
5087 struct isl_map
*result
;
5090 if (isl_map_check_equal_space(map1
, map2
) < 0)
5093 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
5094 map1
->n
* map2
->n
, 0);
5097 for (i
= 0; i
< map1
->n
; ++i
)
5098 for (j
= 0; j
< map2
->n
; ++j
) {
5099 struct isl_basic_map
*part
;
5100 part
= isl_basic_map_sum(
5101 isl_basic_map_copy(map1
->p
[i
]),
5102 isl_basic_map_copy(map2
->p
[j
]));
5103 if (isl_basic_map_is_empty(part
))
5104 isl_basic_map_free(part
);
5106 result
= isl_map_add_basic_map(result
, part
);
5119 __isl_give isl_set
*isl_set_sum(__isl_take isl_set
*set1
,
5120 __isl_take isl_set
*set2
)
5122 return set_from_map(isl_map_sum(set_to_map(set1
), set_to_map(set2
)));
5125 /* Given a basic map A -> f(A), construct A -> -f(A).
5127 __isl_give isl_basic_map
*isl_basic_map_neg(__isl_take isl_basic_map
*bmap
)
5133 bmap
= isl_basic_map_cow(bmap
);
5134 n
= isl_basic_map_dim(bmap
, isl_dim_out
);
5136 return isl_basic_map_free(bmap
);
5138 off
= isl_basic_map_offset(bmap
, isl_dim_out
);
5139 for (i
= 0; i
< bmap
->n_eq
; ++i
)
5140 for (j
= 0; j
< n
; ++j
)
5141 isl_int_neg(bmap
->eq
[i
][off
+j
], bmap
->eq
[i
][off
+j
]);
5142 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
5143 for (j
= 0; j
< n
; ++j
)
5144 isl_int_neg(bmap
->ineq
[i
][off
+j
], bmap
->ineq
[i
][off
+j
]);
5145 for (i
= 0; i
< bmap
->n_div
; ++i
)
5146 for (j
= 0; j
< n
; ++j
)
5147 isl_int_neg(bmap
->div
[i
][1+off
+j
], bmap
->div
[i
][1+off
+j
]);
5148 bmap
= isl_basic_map_gauss(bmap
, NULL
);
5149 return isl_basic_map_finalize(bmap
);
5152 __isl_give isl_basic_set
*isl_basic_set_neg(__isl_take isl_basic_set
*bset
)
5154 return isl_basic_map_neg(bset
);
5157 /* Given a map A -> f(A), construct A -> -f(A).
5159 __isl_give isl_map
*isl_map_neg(__isl_take isl_map
*map
)
5163 map
= isl_map_cow(map
);
5167 for (i
= 0; i
< map
->n
; ++i
) {
5168 map
->p
[i
] = isl_basic_map_neg(map
->p
[i
]);
5179 __isl_give isl_set
*isl_set_neg(__isl_take isl_set
*set
)
5181 return set_from_map(isl_map_neg(set_to_map(set
)));
5184 /* Given a basic map A -> f(A) and an integer d, construct a basic map
5185 * A -> floor(f(A)/d).
5187 __isl_give isl_basic_map
*isl_basic_map_floordiv(__isl_take isl_basic_map
*bmap
,
5190 isl_size n_in
, n_out
, nparam
;
5191 unsigned total
, pos
;
5192 struct isl_basic_map
*result
= NULL
;
5193 struct isl_dim_map
*dim_map
;
5196 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5197 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5198 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5199 if (nparam
< 0 || n_in
< 0 || n_out
< 0)
5200 return isl_basic_map_free(bmap
);
5202 total
= nparam
+ n_in
+ n_out
+ bmap
->n_div
+ n_out
;
5203 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
5204 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_param
, pos
= 0);
5205 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_in
, pos
+= nparam
);
5206 isl_dim_map_div(dim_map
, bmap
, pos
+= n_in
+ n_out
);
5207 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_out
, pos
+= bmap
->n_div
);
5209 result
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
5210 bmap
->n_div
+ n_out
,
5211 bmap
->n_eq
, bmap
->n_ineq
+ 2 * n_out
);
5212 result
= isl_basic_map_add_constraints_dim_map(result
, bmap
, dim_map
);
5213 result
= add_divs(result
, n_out
);
5214 for (i
= 0; i
< n_out
; ++i
) {
5216 j
= isl_basic_map_alloc_inequality(result
);
5219 isl_seq_clr(result
->ineq
[j
], 1+total
);
5220 isl_int_neg(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
5221 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], 1);
5222 j
= isl_basic_map_alloc_inequality(result
);
5225 isl_seq_clr(result
->ineq
[j
], 1+total
);
5226 isl_int_set(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
5227 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], -1);
5228 isl_int_sub_ui(result
->ineq
[j
][0], d
, 1);
5231 result
= isl_basic_map_simplify(result
);
5232 return isl_basic_map_finalize(result
);
5234 isl_basic_map_free(result
);
5238 /* Given a map A -> f(A) and an integer d, construct a map
5239 * A -> floor(f(A)/d).
5241 __isl_give isl_map
*isl_map_floordiv(__isl_take isl_map
*map
, isl_int d
)
5245 map
= isl_map_cow(map
);
5249 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
5250 for (i
= 0; i
< map
->n
; ++i
) {
5251 map
->p
[i
] = isl_basic_map_floordiv(map
->p
[i
], d
);
5255 map
= isl_map_unmark_normalized(map
);
5263 /* Given a map A -> f(A) and an integer d, construct a map
5264 * A -> floor(f(A)/d).
5266 __isl_give isl_map
*isl_map_floordiv_val(__isl_take isl_map
*map
,
5267 __isl_take isl_val
*d
)
5271 if (!isl_val_is_int(d
))
5272 isl_die(isl_val_get_ctx(d
), isl_error_invalid
,
5273 "expecting integer denominator", goto error
);
5274 map
= isl_map_floordiv(map
, d
->n
);
5283 static __isl_give isl_basic_map
*var_equal(__isl_take isl_basic_map
*bmap
,
5291 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5292 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5293 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5294 if (total
< 0 || nparam
< 0 || n_in
< 0)
5295 return isl_basic_map_free(bmap
);
5296 i
= isl_basic_map_alloc_equality(bmap
);
5299 isl_seq_clr(bmap
->eq
[i
], 1 + total
);
5300 isl_int_set_si(bmap
->eq
[i
][1+nparam
+pos
], -1);
5301 isl_int_set_si(bmap
->eq
[i
][1+nparam
+n_in
+pos
], 1);
5302 return isl_basic_map_finalize(bmap
);
5304 isl_basic_map_free(bmap
);
5308 /* Add a constraint to "bmap" expressing i_pos < o_pos
5310 static __isl_give isl_basic_map
*var_less(__isl_take isl_basic_map
*bmap
,
5318 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5319 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5320 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5321 if (total
< 0 || nparam
< 0 || n_in
< 0)
5322 return isl_basic_map_free(bmap
);
5323 i
= isl_basic_map_alloc_inequality(bmap
);
5326 isl_seq_clr(bmap
->ineq
[i
], 1 + total
);
5327 isl_int_set_si(bmap
->ineq
[i
][0], -1);
5328 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
5329 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
5330 return isl_basic_map_finalize(bmap
);
5332 isl_basic_map_free(bmap
);
5336 /* Add a constraint to "bmap" expressing i_pos <= o_pos
5338 static __isl_give isl_basic_map
*var_less_or_equal(
5339 __isl_take isl_basic_map
*bmap
, unsigned pos
)
5346 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5347 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5348 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5349 if (total
< 0 || nparam
< 0 || n_in
< 0)
5350 return isl_basic_map_free(bmap
);
5351 i
= isl_basic_map_alloc_inequality(bmap
);
5354 isl_seq_clr(bmap
->ineq
[i
], 1 + total
);
5355 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
5356 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
5357 return isl_basic_map_finalize(bmap
);
5359 isl_basic_map_free(bmap
);
5363 /* Add a constraint to "bmap" expressing i_pos > o_pos
5365 static __isl_give isl_basic_map
*var_more(__isl_take isl_basic_map
*bmap
,
5373 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5374 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5375 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5376 if (total
< 0 || nparam
< 0 || n_in
< 0)
5377 return isl_basic_map_free(bmap
);
5378 i
= isl_basic_map_alloc_inequality(bmap
);
5381 isl_seq_clr(bmap
->ineq
[i
], 1 + total
);
5382 isl_int_set_si(bmap
->ineq
[i
][0], -1);
5383 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
5384 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
5385 return isl_basic_map_finalize(bmap
);
5387 isl_basic_map_free(bmap
);
5391 /* Add a constraint to "bmap" expressing i_pos >= o_pos
5393 static __isl_give isl_basic_map
*var_more_or_equal(
5394 __isl_take isl_basic_map
*bmap
, unsigned pos
)
5401 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5402 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5403 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5404 if (total
< 0 || nparam
< 0 || n_in
< 0)
5405 return isl_basic_map_free(bmap
);
5406 i
= isl_basic_map_alloc_inequality(bmap
);
5409 isl_seq_clr(bmap
->ineq
[i
], 1 + total
);
5410 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
5411 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
5412 return isl_basic_map_finalize(bmap
);
5414 isl_basic_map_free(bmap
);
5418 __isl_give isl_basic_map
*isl_basic_map_equal(
5419 __isl_take isl_space
*space
, unsigned n_equal
)
5422 struct isl_basic_map
*bmap
;
5423 bmap
= isl_basic_map_alloc_space(space
, 0, n_equal
, 0);
5426 for (i
= 0; i
< n_equal
&& bmap
; ++i
)
5427 bmap
= var_equal(bmap
, i
);
5428 return isl_basic_map_finalize(bmap
);
5431 /* Return a relation on of dimension "space" expressing i_[0..pos] << o_[0..pos]
5433 __isl_give isl_basic_map
*isl_basic_map_less_at(__isl_take isl_space
*space
,
5437 struct isl_basic_map
*bmap
;
5438 bmap
= isl_basic_map_alloc_space(space
, 0, pos
, 1);
5441 for (i
= 0; i
< pos
&& bmap
; ++i
)
5442 bmap
= var_equal(bmap
, i
);
5444 bmap
= var_less(bmap
, pos
);
5445 return isl_basic_map_finalize(bmap
);
5448 /* Return a relation on "space" expressing i_[0..pos] <<= o_[0..pos]
5450 __isl_give isl_basic_map
*isl_basic_map_less_or_equal_at(
5451 __isl_take isl_space
*space
, unsigned pos
)
5454 isl_basic_map
*bmap
;
5456 bmap
= isl_basic_map_alloc_space(space
, 0, pos
, 1);
5457 for (i
= 0; i
< pos
; ++i
)
5458 bmap
= var_equal(bmap
, i
);
5459 bmap
= var_less_or_equal(bmap
, pos
);
5460 return isl_basic_map_finalize(bmap
);
5463 /* Return a relation on "space" expressing i_pos > o_pos
5465 __isl_give isl_basic_map
*isl_basic_map_more_at(__isl_take isl_space
*space
,
5469 struct isl_basic_map
*bmap
;
5470 bmap
= isl_basic_map_alloc_space(space
, 0, pos
, 1);
5473 for (i
= 0; i
< pos
&& bmap
; ++i
)
5474 bmap
= var_equal(bmap
, i
);
5476 bmap
= var_more(bmap
, pos
);
5477 return isl_basic_map_finalize(bmap
);
5480 /* Return a relation on "space" expressing i_[0..pos] >>= o_[0..pos]
5482 __isl_give isl_basic_map
*isl_basic_map_more_or_equal_at(
5483 __isl_take isl_space
*space
, unsigned pos
)
5486 isl_basic_map
*bmap
;
5488 bmap
= isl_basic_map_alloc_space(space
, 0, pos
, 1);
5489 for (i
= 0; i
< pos
; ++i
)
5490 bmap
= var_equal(bmap
, i
);
5491 bmap
= var_more_or_equal(bmap
, pos
);
5492 return isl_basic_map_finalize(bmap
);
5495 static __isl_give isl_map
*map_lex_lte_first(__isl_take isl_space
*space
,
5496 unsigned n
, int equal
)
5498 struct isl_map
*map
;
5501 if (n
== 0 && equal
)
5502 return isl_map_universe(space
);
5504 map
= isl_map_alloc_space(isl_space_copy(space
), n
, ISL_MAP_DISJOINT
);
5506 for (i
= 0; i
+ 1 < n
; ++i
)
5507 map
= isl_map_add_basic_map(map
,
5508 isl_basic_map_less_at(isl_space_copy(space
), i
));
5511 map
= isl_map_add_basic_map(map
,
5512 isl_basic_map_less_or_equal_at(space
, n
- 1));
5514 map
= isl_map_add_basic_map(map
,
5515 isl_basic_map_less_at(space
, n
- 1));
5517 isl_space_free(space
);
5522 static __isl_give isl_map
*map_lex_lte(__isl_take isl_space
*space
, int equal
)
5526 return map_lex_lte_first(space
, space
->n_out
, equal
);
5529 __isl_give isl_map
*isl_map_lex_lt_first(__isl_take isl_space
*space
,
5532 return map_lex_lte_first(space
, n
, 0);
5535 __isl_give isl_map
*isl_map_lex_le_first(__isl_take isl_space
*space
,
5538 return map_lex_lte_first(space
, n
, 1);
5541 __isl_give isl_map
*isl_map_lex_lt(__isl_take isl_space
*set_space
)
5543 return map_lex_lte(isl_space_map_from_set(set_space
), 0);
5546 __isl_give isl_map
*isl_map_lex_le(__isl_take isl_space
*set_space
)
5548 return map_lex_lte(isl_space_map_from_set(set_space
), 1);
5551 static __isl_give isl_map
*map_lex_gte_first(__isl_take isl_space
*space
,
5552 unsigned n
, int equal
)
5554 struct isl_map
*map
;
5557 if (n
== 0 && equal
)
5558 return isl_map_universe(space
);
5560 map
= isl_map_alloc_space(isl_space_copy(space
), n
, ISL_MAP_DISJOINT
);
5562 for (i
= 0; i
+ 1 < n
; ++i
)
5563 map
= isl_map_add_basic_map(map
,
5564 isl_basic_map_more_at(isl_space_copy(space
), i
));
5567 map
= isl_map_add_basic_map(map
,
5568 isl_basic_map_more_or_equal_at(space
, n
- 1));
5570 map
= isl_map_add_basic_map(map
,
5571 isl_basic_map_more_at(space
, n
- 1));
5573 isl_space_free(space
);
5578 static __isl_give isl_map
*map_lex_gte(__isl_take isl_space
*space
, int equal
)
5582 return map_lex_gte_first(space
, space
->n_out
, equal
);
5585 __isl_give isl_map
*isl_map_lex_gt_first(__isl_take isl_space
*space
,
5588 return map_lex_gte_first(space
, n
, 0);
5591 __isl_give isl_map
*isl_map_lex_ge_first(__isl_take isl_space
*space
,
5594 return map_lex_gte_first(space
, n
, 1);
5597 __isl_give isl_map
*isl_map_lex_gt(__isl_take isl_space
*set_space
)
5599 return map_lex_gte(isl_space_map_from_set(set_space
), 0);
5602 __isl_give isl_map
*isl_map_lex_ge(__isl_take isl_space
*set_space
)
5604 return map_lex_gte(isl_space_map_from_set(set_space
), 1);
5607 __isl_give isl_map
*isl_set_lex_le_set(__isl_take isl_set
*set1
,
5608 __isl_take isl_set
*set2
)
5611 map
= isl_map_lex_le(isl_set_get_space(set1
));
5612 map
= isl_map_intersect_domain(map
, set1
);
5613 map
= isl_map_intersect_range(map
, set2
);
5617 __isl_give isl_map
*isl_set_lex_lt_set(__isl_take isl_set
*set1
,
5618 __isl_take isl_set
*set2
)
5621 map
= isl_map_lex_lt(isl_set_get_space(set1
));
5622 map
= isl_map_intersect_domain(map
, set1
);
5623 map
= isl_map_intersect_range(map
, set2
);
5627 __isl_give isl_map
*isl_set_lex_ge_set(__isl_take isl_set
*set1
,
5628 __isl_take isl_set
*set2
)
5631 map
= isl_map_lex_ge(isl_set_get_space(set1
));
5632 map
= isl_map_intersect_domain(map
, set1
);
5633 map
= isl_map_intersect_range(map
, set2
);
5637 __isl_give isl_map
*isl_set_lex_gt_set(__isl_take isl_set
*set1
,
5638 __isl_take isl_set
*set2
)
5641 map
= isl_map_lex_gt(isl_set_get_space(set1
));
5642 map
= isl_map_intersect_domain(map
, set1
);
5643 map
= isl_map_intersect_range(map
, set2
);
5647 __isl_give isl_map
*isl_map_lex_le_map(__isl_take isl_map
*map1
,
5648 __isl_take isl_map
*map2
)
5651 map
= isl_map_lex_le(isl_space_range(isl_map_get_space(map1
)));
5652 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
5653 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
5657 __isl_give isl_map
*isl_map_lex_lt_map(__isl_take isl_map
*map1
,
5658 __isl_take isl_map
*map2
)
5661 map
= isl_map_lex_lt(isl_space_range(isl_map_get_space(map1
)));
5662 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
5663 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
5667 __isl_give isl_map
*isl_map_lex_ge_map(__isl_take isl_map
*map1
,
5668 __isl_take isl_map
*map2
)
5671 map
= isl_map_lex_ge(isl_space_range(isl_map_get_space(map1
)));
5672 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
5673 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
5677 __isl_give isl_map
*isl_map_lex_gt_map(__isl_take isl_map
*map1
,
5678 __isl_take isl_map
*map2
)
5681 map
= isl_map_lex_gt(isl_space_range(isl_map_get_space(map1
)));
5682 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
5683 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
5687 /* For the div d = floor(f/m) at position "div", add the constraint
5691 static __isl_give isl_basic_map
*add_upper_div_constraint(
5692 __isl_take isl_basic_map
*bmap
, unsigned div
)
5695 isl_size v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
5699 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
5700 if (v_div
< 0 || n_div
< 0)
5701 return isl_basic_map_free(bmap
);
5703 i
= isl_basic_map_alloc_inequality(bmap
);
5705 return isl_basic_map_free(bmap
);
5706 isl_seq_cpy(bmap
->ineq
[i
], bmap
->div
[div
] + 1, 1 + v_div
+ n_div
);
5707 isl_int_neg(bmap
->ineq
[i
][1 + pos
], bmap
->div
[div
][0]);
5712 /* For the div d = floor(f/m) at position "div", add the constraint
5714 * -(f-(m-1)) + m d >= 0
5716 static __isl_give isl_basic_map
*add_lower_div_constraint(
5717 __isl_take isl_basic_map
*bmap
, unsigned div
)
5720 isl_size v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
5724 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
5725 if (v_div
< 0 || n_div
< 0)
5726 return isl_basic_map_free(bmap
);
5728 i
= isl_basic_map_alloc_inequality(bmap
);
5730 return isl_basic_map_free(bmap
);
5731 isl_seq_neg(bmap
->ineq
[i
], bmap
->div
[div
] + 1, 1 + v_div
+ n_div
);
5732 isl_int_set(bmap
->ineq
[i
][1 + pos
], bmap
->div
[div
][0]);
5733 isl_int_add(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], bmap
->ineq
[i
][1 + pos
]);
5734 isl_int_sub_ui(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], 1);
5739 /* For the div d = floor(f/m) at position "pos", add the constraints
5742 * -(f-(m-1)) + m d >= 0
5744 * Note that the second constraint is the negation of
5748 __isl_give isl_basic_map
*isl_basic_map_add_div_constraints(
5749 __isl_take isl_basic_map
*bmap
, unsigned pos
)
5751 bmap
= add_upper_div_constraint(bmap
, pos
);
5752 bmap
= add_lower_div_constraint(bmap
, pos
);
5756 /* For each known div d = floor(f/m), add the constraints
5759 * -(f-(m-1)) + m d >= 0
5761 * Remove duplicate constraints in case of some these div constraints
5762 * already appear in "bmap".
5764 __isl_give isl_basic_map
*isl_basic_map_add_known_div_constraints(
5765 __isl_take isl_basic_map
*bmap
)
5769 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
5771 return isl_basic_map_free(bmap
);
5775 bmap
= add_known_div_constraints(bmap
);
5776 bmap
= isl_basic_map_remove_duplicate_constraints(bmap
, NULL
, 0);
5777 bmap
= isl_basic_map_finalize(bmap
);
5781 /* Add the div constraint of sign "sign" for div "div" of "bmap".
5783 * In particular, if this div is of the form d = floor(f/m),
5784 * then add the constraint
5788 * if sign < 0 or the constraint
5790 * -(f-(m-1)) + m d >= 0
5794 __isl_give isl_basic_map
*isl_basic_map_add_div_constraint(
5795 __isl_take isl_basic_map
*bmap
, unsigned div
, int sign
)
5798 return add_upper_div_constraint(bmap
, div
);
5800 return add_lower_div_constraint(bmap
, div
);
5803 __isl_give isl_basic_set
*isl_basic_map_underlying_set(
5804 __isl_take isl_basic_map
*bmap
)
5810 if (bmap
->dim
->nparam
== 0 && bmap
->dim
->n_in
== 0 &&
5812 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_in
) &&
5813 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_out
))
5814 return bset_from_bmap(bmap
);
5815 bmap
= isl_basic_map_cow(bmap
);
5818 space
= isl_basic_map_take_space(bmap
);
5819 space
= isl_space_underlying(space
, bmap
->n_div
);
5820 bmap
= isl_basic_map_restore_space(bmap
, space
);
5823 bmap
->extra
-= bmap
->n_div
;
5825 bmap
= isl_basic_map_finalize(bmap
);
5826 return bset_from_bmap(bmap
);
5828 isl_basic_map_free(bmap
);
5832 __isl_give isl_basic_set
*isl_basic_set_underlying_set(
5833 __isl_take isl_basic_set
*bset
)
5835 return isl_basic_map_underlying_set(bset_to_bmap(bset
));
5838 /* Replace each element in "list" by the result of applying
5839 * isl_basic_map_underlying_set to the element.
5841 __isl_give isl_basic_set_list
*isl_basic_map_list_underlying_set(
5842 __isl_take isl_basic_map_list
*list
)
5847 n
= isl_basic_map_list_n_basic_map(list
);
5851 for (i
= 0; i
< n
; ++i
) {
5852 isl_basic_map
*bmap
;
5853 isl_basic_set
*bset
;
5855 bmap
= isl_basic_map_list_get_basic_map(list
, i
);
5856 bset
= isl_basic_set_underlying_set(bmap
);
5857 list
= isl_basic_set_list_set_basic_set(list
, i
, bset
);
5862 isl_basic_map_list_free(list
);
5866 __isl_give isl_basic_map
*isl_basic_map_overlying_set(
5867 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_map
*like
)
5869 struct isl_basic_map
*bmap
;
5870 struct isl_ctx
*ctx
;
5871 isl_size dim
, bmap_total
;
5878 if (isl_basic_set_check_no_params(bset
) < 0 ||
5879 isl_basic_set_check_no_locals(bset
) < 0)
5881 dim
= isl_basic_set_dim(bset
, isl_dim_set
);
5882 bmap_total
= isl_basic_map_dim(like
, isl_dim_all
);
5883 if (dim
< 0 || bmap_total
< 0)
5885 isl_assert(ctx
, dim
== bmap_total
, goto error
);
5886 if (like
->n_div
== 0) {
5887 isl_space
*space
= isl_basic_map_get_space(like
);
5888 isl_basic_map_free(like
);
5889 return isl_basic_map_reset_space(bset
, space
);
5891 bset
= isl_basic_set_cow(bset
);
5894 total
= dim
+ bset
->extra
;
5895 bmap
= bset_to_bmap(bset
);
5896 isl_space_free(isl_basic_map_take_space(bmap
));
5897 bmap
= isl_basic_map_restore_space(bmap
, isl_basic_map_get_space(like
));
5900 bmap
->n_div
= like
->n_div
;
5901 bmap
->extra
+= like
->n_div
;
5905 ltotal
= total
- bmap
->extra
+ like
->extra
;
5908 bmap
->block2
= isl_blk_extend(ctx
, bmap
->block2
,
5909 bmap
->extra
* (1 + 1 + total
));
5910 if (isl_blk_is_error(bmap
->block2
))
5912 div
= isl_realloc_array(ctx
, bmap
->div
, isl_int
*, bmap
->extra
);
5916 for (i
= 0; i
< bmap
->extra
; ++i
)
5917 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + 1 + total
);
5918 for (i
= 0; i
< like
->n_div
; ++i
) {
5919 isl_seq_cpy(bmap
->div
[i
], like
->div
[i
], 1 + 1 + ltotal
);
5920 isl_seq_clr(bmap
->div
[i
]+1+1+ltotal
, total
- ltotal
);
5922 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
5924 isl_basic_map_free(like
);
5925 bmap
= isl_basic_map_simplify(bmap
);
5926 bmap
= isl_basic_map_finalize(bmap
);
5929 isl_basic_map_free(like
);
5930 isl_basic_set_free(bset
);
5934 __isl_give isl_basic_set
*isl_basic_set_from_underlying_set(
5935 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*like
)
5937 return bset_from_bmap(isl_basic_map_overlying_set(bset
,
5938 bset_to_bmap(like
)));
5941 __isl_give isl_set
*isl_map_underlying_set(__isl_take isl_map
*map
)
5945 map
= isl_map_cow(map
);
5948 map
->dim
= isl_space_cow(map
->dim
);
5952 for (i
= 1; i
< map
->n
; ++i
)
5953 isl_assert(map
->ctx
, map
->p
[0]->n_div
== map
->p
[i
]->n_div
,
5955 for (i
= 0; i
< map
->n
; ++i
) {
5956 map
->p
[i
] = bset_to_bmap(
5957 isl_basic_map_underlying_set(map
->p
[i
]));
5962 map
->dim
= isl_space_underlying(map
->dim
, 0);
5964 isl_space_free(map
->dim
);
5965 map
->dim
= isl_space_copy(map
->p
[0]->dim
);
5969 return set_from_map(map
);
5975 /* Replace the space of "bmap" by "space".
5977 * If the space of "bmap" is identical to "space" (including the identifiers
5978 * of the input and output dimensions), then simply return the original input.
5980 __isl_give isl_basic_map
*isl_basic_map_reset_space(
5981 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*space
)
5984 isl_space
*bmap_space
;
5986 bmap_space
= isl_basic_map_peek_space(bmap
);
5987 equal
= isl_space_is_equal(bmap_space
, space
);
5988 if (equal
>= 0 && equal
)
5989 equal
= isl_space_has_equal_ids(bmap_space
, space
);
5993 isl_space_free(space
);
5996 isl_space_free(isl_basic_map_take_space(bmap
));
5997 bmap
= isl_basic_map_restore_space(bmap
, space
);
5999 bmap
= isl_basic_map_finalize(bmap
);
6003 isl_basic_map_free(bmap
);
6004 isl_space_free(space
);
6008 __isl_give isl_basic_set
*isl_basic_set_reset_space(
6009 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*space
)
6011 return bset_from_bmap(isl_basic_map_reset_space(bset_to_bmap(bset
),
6015 /* Check that the total dimensions of "map" and "space" are the same.
6017 static isl_stat
check_map_space_equal_total_dim(__isl_keep isl_map
*map
,
6018 __isl_keep isl_space
*space
)
6020 isl_size dim1
, dim2
;
6022 dim1
= isl_map_dim(map
, isl_dim_all
);
6023 dim2
= isl_space_dim(space
, isl_dim_all
);
6024 if (dim1
< 0 || dim2
< 0)
6025 return isl_stat_error
;
6028 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
6029 "total dimensions do not match", return isl_stat_error
);
6032 __isl_give isl_map
*isl_map_reset_space(__isl_take isl_map
*map
,
6033 __isl_take isl_space
*space
)
6037 map
= isl_map_cow(map
);
6041 for (i
= 0; i
< map
->n
; ++i
) {
6042 map
->p
[i
] = isl_basic_map_reset_space(map
->p
[i
],
6043 isl_space_copy(space
));
6047 isl_space_free(isl_map_take_space(map
));
6048 map
= isl_map_restore_space(map
, space
);
6053 isl_space_free(space
);
6057 /* Replace the space of "map" by "space", without modifying
6058 * the dimension of "map".
6060 * If the space of "map" is identical to "space" (including the identifiers
6061 * of the input and output dimensions), then simply return the original input.
6063 __isl_give isl_map
*isl_map_reset_equal_dim_space(__isl_take isl_map
*map
,
6064 __isl_take isl_space
*space
)
6067 isl_space
*map_space
;
6069 map_space
= isl_map_peek_space(map
);
6070 equal
= isl_space_is_equal(map_space
, space
);
6071 if (equal
>= 0 && equal
)
6072 equal
= isl_space_has_equal_ids(map_space
, space
);
6076 isl_space_free(space
);
6079 if (check_map_space_equal_total_dim(map
, space
) < 0)
6081 return isl_map_reset_space(map
, space
);
6084 isl_space_free(space
);
6088 __isl_give isl_set
*isl_set_reset_space(__isl_take isl_set
*set
,
6089 __isl_take isl_space
*space
)
6091 return set_from_map(isl_map_reset_space(set_to_map(set
), space
));
6094 /* Compute the parameter domain of the given basic set.
6096 __isl_give isl_basic_set
*isl_basic_set_params(__isl_take isl_basic_set
*bset
)
6102 is_params
= isl_basic_set_is_params(bset
);
6104 return isl_basic_set_free(bset
);
6108 n
= isl_basic_set_dim(bset
, isl_dim_set
);
6110 return isl_basic_set_free(bset
);
6111 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
6112 space
= isl_basic_set_get_space(bset
);
6113 space
= isl_space_params(space
);
6114 bset
= isl_basic_set_reset_space(bset
, space
);
6118 /* Construct a zero-dimensional basic set with the given parameter domain.
6120 __isl_give isl_basic_set
*isl_basic_set_from_params(
6121 __isl_take isl_basic_set
*bset
)
6124 space
= isl_basic_set_get_space(bset
);
6125 space
= isl_space_set_from_params(space
);
6126 bset
= isl_basic_set_reset_space(bset
, space
);
6130 /* Compute the parameter domain of the given set.
6132 __isl_give isl_set
*isl_set_params(__isl_take isl_set
*set
)
6134 return isl_map_params(set_to_map(set
));
6137 /* Construct a zero-dimensional set with the given parameter domain.
6139 __isl_give isl_set
*isl_set_from_params(__isl_take isl_set
*set
)
6142 space
= isl_set_get_space(set
);
6143 space
= isl_space_set_from_params(space
);
6144 set
= isl_set_reset_space(set
, space
);
6148 /* Compute the parameter domain of the given map.
6150 __isl_give isl_set
*isl_map_params(__isl_take isl_map
*map
)
6153 isl_size n_in
, n_out
;
6155 n_in
= isl_map_dim(map
, isl_dim_in
);
6156 n_out
= isl_map_dim(map
, isl_dim_out
);
6157 if (n_in
< 0 || n_out
< 0)
6158 return isl_map_free(map
);
6159 map
= isl_map_project_out(map
, isl_dim_in
, 0, n_in
);
6160 map
= isl_map_project_out(map
, isl_dim_out
, 0, n_out
);
6161 space
= isl_map_get_space(map
);
6162 space
= isl_space_params(space
);
6163 map
= isl_map_reset_space(map
, space
);
6167 __isl_give isl_basic_set
*isl_basic_map_domain(__isl_take isl_basic_map
*bmap
)
6172 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
6174 return isl_basic_map_free(bmap
);
6175 space
= isl_space_domain(isl_basic_map_get_space(bmap
));
6177 bmap
= isl_basic_map_project_out(bmap
, isl_dim_out
, 0, n_out
);
6179 return isl_basic_map_reset_space(bmap
, space
);
6182 isl_bool
isl_basic_map_may_be_set(__isl_keep isl_basic_map
*bmap
)
6185 return isl_bool_error
;
6186 return isl_space_may_be_set(bmap
->dim
);
6189 /* Is this basic map actually a set?
6190 * Users should never call this function. Outside of isl,
6191 * the type should indicate whether something is a set or a map.
6193 isl_bool
isl_basic_map_is_set(__isl_keep isl_basic_map
*bmap
)
6196 return isl_bool_error
;
6197 return isl_space_is_set(bmap
->dim
);
6200 __isl_give isl_basic_set
*isl_basic_map_range(__isl_take isl_basic_map
*bmap
)
6204 is_set
= isl_basic_map_is_set(bmap
);
6209 return isl_basic_map_domain(isl_basic_map_reverse(bmap
));
6211 isl_basic_map_free(bmap
);
6215 __isl_give isl_basic_map
*isl_basic_map_domain_map(
6216 __isl_take isl_basic_map
*bmap
)
6220 isl_basic_map
*domain
;
6221 isl_size nparam
, n_in
, n_out
;
6223 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
6224 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
6225 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
6226 if (nparam
< 0 || n_in
< 0 || n_out
< 0)
6227 return isl_basic_map_free(bmap
);
6229 space
= isl_basic_map_get_space(bmap
);
6230 space
= isl_space_from_range(isl_space_domain(space
));
6231 domain
= isl_basic_map_universe(space
);
6233 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
6234 bmap
= isl_basic_map_apply_range(bmap
, domain
);
6235 bmap
= isl_basic_map_extend_constraints(bmap
, n_in
, 0);
6237 for (i
= 0; i
< n_in
; ++i
)
6238 bmap
= isl_basic_map_equate(bmap
, isl_dim_in
, i
,
6241 bmap
= isl_basic_map_gauss(bmap
, NULL
);
6242 return isl_basic_map_finalize(bmap
);
6245 __isl_give isl_basic_map
*isl_basic_map_range_map(
6246 __isl_take isl_basic_map
*bmap
)
6250 isl_basic_map
*range
;
6251 isl_size nparam
, n_in
, n_out
;
6253 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
6254 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
6255 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
6256 if (nparam
< 0 || n_in
< 0 || n_out
< 0)
6257 return isl_basic_map_free(bmap
);
6259 space
= isl_basic_map_get_space(bmap
);
6260 space
= isl_space_from_range(isl_space_range(space
));
6261 range
= isl_basic_map_universe(space
);
6263 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
6264 bmap
= isl_basic_map_apply_range(bmap
, range
);
6265 bmap
= isl_basic_map_extend_constraints(bmap
, n_out
, 0);
6267 for (i
= 0; i
< n_out
; ++i
)
6268 bmap
= isl_basic_map_equate(bmap
, isl_dim_in
, n_in
+ i
,
6271 bmap
= isl_basic_map_gauss(bmap
, NULL
);
6272 return isl_basic_map_finalize(bmap
);
6275 int isl_map_may_be_set(__isl_keep isl_map
*map
)
6279 return isl_space_may_be_set(map
->dim
);
6282 /* Is this map actually a set?
6283 * Users should never call this function. Outside of isl,
6284 * the type should indicate whether something is a set or a map.
6286 isl_bool
isl_map_is_set(__isl_keep isl_map
*map
)
6289 return isl_bool_error
;
6290 return isl_space_is_set(map
->dim
);
6293 __isl_give isl_set
*isl_map_range(__isl_take isl_map
*map
)
6298 n_in
= isl_map_dim(map
, isl_dim_in
);
6300 return set_from_map(isl_map_free(map
));
6301 space
= isl_space_range(isl_map_get_space(map
));
6303 map
= isl_map_project_out(map
, isl_dim_in
, 0, n_in
);
6305 return set_from_map(isl_map_reset_space(map
, space
));
6308 /* Transform "map" by applying "fn_space" to its space and "fn_bmap"
6309 * to each of its basic maps.
6311 static __isl_give isl_map
*isl_map_transform(__isl_take isl_map
*map
,
6312 __isl_give isl_space
*(*fn_space
)(__isl_take isl_space
*space
),
6313 __isl_give isl_basic_map
*(*fn_bmap
)(__isl_take isl_basic_map
*bmap
))
6318 map
= isl_map_cow(map
);
6322 for (i
= 0; i
< map
->n
; ++i
) {
6323 map
->p
[i
] = fn_bmap(map
->p
[i
]);
6325 return isl_map_free(map
);
6327 map
= isl_map_unmark_normalized(map
);
6329 space
= isl_map_take_space(map
);
6330 space
= fn_space(space
);
6331 map
= isl_map_restore_space(map
, space
);
6336 __isl_give isl_map
*isl_map_domain_map(__isl_take isl_map
*map
)
6338 return isl_map_transform(map
, &isl_space_domain_map
,
6339 &isl_basic_map_domain_map
);
6342 __isl_give isl_map
*isl_map_range_map(__isl_take isl_map
*map
)
6344 return isl_map_transform(map
, &isl_space_range_map
,
6345 &isl_basic_map_range_map
);
6348 /* Given a wrapped map of the form A[B -> C],
6349 * return the map A[B -> C] -> B.
6351 __isl_give isl_map
*isl_set_wrapped_domain_map(__isl_take isl_set
*set
)
6358 if (!isl_set_has_tuple_id(set
))
6359 return isl_map_domain_map(isl_set_unwrap(set
));
6361 id
= isl_set_get_tuple_id(set
);
6362 map
= isl_map_domain_map(isl_set_unwrap(set
));
6363 map
= isl_map_set_tuple_id(map
, isl_dim_in
, id
);
6368 __isl_give isl_basic_map
*isl_basic_map_from_domain(
6369 __isl_take isl_basic_set
*bset
)
6371 return isl_basic_map_reverse(isl_basic_map_from_range(bset
));
6374 __isl_give isl_basic_map
*isl_basic_map_from_range(
6375 __isl_take isl_basic_set
*bset
)
6378 space
= isl_basic_set_get_space(bset
);
6379 space
= isl_space_from_range(space
);
6380 bset
= isl_basic_set_reset_space(bset
, space
);
6381 return bset_to_bmap(bset
);
6384 /* Create a relation with the given set as range.
6385 * The domain of the created relation is a zero-dimensional
6386 * flat anonymous space.
6388 __isl_give isl_map
*isl_map_from_range(__isl_take isl_set
*set
)
6391 space
= isl_set_get_space(set
);
6392 space
= isl_space_from_range(space
);
6393 set
= isl_set_reset_space(set
, space
);
6394 return set_to_map(set
);
6397 /* Create a relation with the given set as domain.
6398 * The range of the created relation is a zero-dimensional
6399 * flat anonymous space.
6401 __isl_give isl_map
*isl_map_from_domain(__isl_take isl_set
*set
)
6403 return isl_map_reverse(isl_map_from_range(set
));
6406 __isl_give isl_basic_map
*isl_basic_map_from_domain_and_range(
6407 __isl_take isl_basic_set
*domain
, __isl_take isl_basic_set
*range
)
6409 return isl_basic_map_apply_range(isl_basic_map_reverse(domain
), range
);
6412 __isl_give isl_map
*isl_map_from_domain_and_range(__isl_take isl_set
*domain
,
6413 __isl_take isl_set
*range
)
6415 return isl_map_apply_range(isl_map_reverse(domain
), range
);
6418 /* Return a newly allocated isl_map with given space and flags and
6419 * room for "n" basic maps.
6420 * Make sure that all cached information is cleared.
6422 __isl_give isl_map
*isl_map_alloc_space(__isl_take isl_space
*space
, int n
,
6425 struct isl_map
*map
;
6430 isl_die(space
->ctx
, isl_error_internal
,
6431 "negative number of basic maps", goto error
);
6432 map
= isl_calloc(space
->ctx
, struct isl_map
,
6433 sizeof(struct isl_map
) +
6434 (n
- 1) * sizeof(struct isl_basic_map
*));
6438 map
->ctx
= space
->ctx
;
6439 isl_ctx_ref(map
->ctx
);
6447 isl_space_free(space
);
6451 __isl_give isl_basic_map
*isl_basic_map_empty(__isl_take isl_space
*space
)
6453 struct isl_basic_map
*bmap
;
6454 bmap
= isl_basic_map_alloc_space(space
, 0, 1, 0);
6455 bmap
= isl_basic_map_set_to_empty(bmap
);
6459 __isl_give isl_basic_set
*isl_basic_set_empty(__isl_take isl_space
*space
)
6461 struct isl_basic_set
*bset
;
6462 bset
= isl_basic_set_alloc_space(space
, 0, 1, 0);
6463 bset
= isl_basic_set_set_to_empty(bset
);
6467 __isl_give isl_basic_map
*isl_basic_map_universe(__isl_take isl_space
*space
)
6469 struct isl_basic_map
*bmap
;
6470 bmap
= isl_basic_map_alloc_space(space
, 0, 0, 0);
6471 bmap
= isl_basic_map_finalize(bmap
);
6475 __isl_give isl_basic_set
*isl_basic_set_universe(__isl_take isl_space
*space
)
6477 struct isl_basic_set
*bset
;
6478 bset
= isl_basic_set_alloc_space(space
, 0, 0, 0);
6479 bset
= isl_basic_set_finalize(bset
);
6483 __isl_give isl_basic_map
*isl_basic_map_nat_universe(
6484 __isl_take isl_space
*space
)
6487 isl_size total
= isl_space_dim(space
, isl_dim_all
);
6488 isl_basic_map
*bmap
;
6491 space
= isl_space_free(space
);
6492 bmap
= isl_basic_map_alloc_space(space
, 0, 0, total
);
6493 for (i
= 0; i
< total
; ++i
) {
6494 int k
= isl_basic_map_alloc_inequality(bmap
);
6497 isl_seq_clr(bmap
->ineq
[k
], 1 + total
);
6498 isl_int_set_si(bmap
->ineq
[k
][1 + i
], 1);
6502 isl_basic_map_free(bmap
);
6506 __isl_give isl_basic_set
*isl_basic_set_nat_universe(
6507 __isl_take isl_space
*space
)
6509 return isl_basic_map_nat_universe(space
);
6512 __isl_give isl_map
*isl_map_nat_universe(__isl_take isl_space
*space
)
6514 return isl_map_from_basic_map(isl_basic_map_nat_universe(space
));
6517 __isl_give isl_set
*isl_set_nat_universe(__isl_take isl_space
*space
)
6519 return isl_map_nat_universe(space
);
6522 __isl_give isl_map
*isl_map_empty(__isl_take isl_space
*space
)
6524 return isl_map_alloc_space(space
, 0, ISL_MAP_DISJOINT
);
6527 __isl_give isl_set
*isl_set_empty(__isl_take isl_space
*space
)
6529 return isl_set_alloc_space(space
, 0, ISL_MAP_DISJOINT
);
6532 __isl_give isl_map
*isl_map_universe(__isl_take isl_space
*space
)
6534 struct isl_map
*map
;
6537 map
= isl_map_alloc_space(isl_space_copy(space
), 1, ISL_MAP_DISJOINT
);
6538 map
= isl_map_add_basic_map(map
, isl_basic_map_universe(space
));
6542 /* This function performs the same operation as isl_map_universe,
6543 * but is considered as a function on an isl_space when exported.
6545 __isl_give isl_map
*isl_space_universe_map(__isl_take isl_space
*space
)
6547 return isl_map_universe(space
);
6550 __isl_give isl_set
*isl_set_universe(__isl_take isl_space
*space
)
6552 struct isl_set
*set
;
6555 set
= isl_set_alloc_space(isl_space_copy(space
), 1, ISL_MAP_DISJOINT
);
6556 set
= isl_set_add_basic_set(set
, isl_basic_set_universe(space
));
6560 /* This function performs the same operation as isl_set_universe,
6561 * but is considered as a function on an isl_space when exported.
6563 __isl_give isl_set
*isl_space_universe_set(__isl_take isl_space
*space
)
6565 return isl_set_universe(space
);
6568 __isl_give isl_map
*isl_map_dup(__isl_keep isl_map
*map
)
6571 struct isl_map
*dup
;
6575 dup
= isl_map_alloc_space(isl_space_copy(map
->dim
), map
->n
, map
->flags
);
6576 for (i
= 0; i
< map
->n
; ++i
)
6577 dup
= isl_map_add_basic_map(dup
, isl_basic_map_copy(map
->p
[i
]));
6581 __isl_give isl_map
*isl_map_add_basic_map(__isl_take isl_map
*map
,
6582 __isl_take isl_basic_map
*bmap
)
6586 if (isl_basic_map_plain_is_empty(bmap
)) {
6587 isl_basic_map_free(bmap
);
6590 if (isl_map_basic_map_check_equal_space(map
, bmap
) < 0)
6592 isl_assert(map
->ctx
, map
->n
< map
->size
, goto error
);
6593 map
->p
[map
->n
] = bmap
;
6595 map
= isl_map_unmark_normalized(map
);
6601 isl_basic_map_free(bmap
);
6605 __isl_null isl_map
*isl_map_free(__isl_take isl_map
*map
)
6616 isl_ctx_deref(map
->ctx
);
6617 for (i
= 0; i
< map
->n
; ++i
)
6618 isl_basic_map_free(map
->p
[i
]);
6619 isl_space_free(map
->dim
);
6625 static __isl_give isl_basic_map
*isl_basic_map_fix_pos_si(
6626 __isl_take isl_basic_map
*bmap
, unsigned pos
, int value
)
6631 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
6633 return isl_basic_map_free(bmap
);
6635 bmap
= isl_basic_map_cow(bmap
);
6636 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
6637 j
= isl_basic_map_alloc_equality(bmap
);
6640 isl_seq_clr(bmap
->eq
[j
] + 1, total
);
6641 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
6642 isl_int_set_si(bmap
->eq
[j
][0], value
);
6643 bmap
= isl_basic_map_simplify(bmap
);
6644 return isl_basic_map_finalize(bmap
);
6646 isl_basic_map_free(bmap
);
6650 static __isl_give isl_basic_map
*isl_basic_map_fix_pos(
6651 __isl_take isl_basic_map
*bmap
, unsigned pos
, isl_int value
)
6656 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
6658 return isl_basic_map_free(bmap
);
6660 bmap
= isl_basic_map_cow(bmap
);
6661 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
6662 j
= isl_basic_map_alloc_equality(bmap
);
6665 isl_seq_clr(bmap
->eq
[j
] + 1, total
);
6666 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
6667 isl_int_set(bmap
->eq
[j
][0], value
);
6668 bmap
= isl_basic_map_simplify(bmap
);
6669 return isl_basic_map_finalize(bmap
);
6671 isl_basic_map_free(bmap
);
6675 __isl_give isl_basic_map
*isl_basic_map_fix_si(__isl_take isl_basic_map
*bmap
,
6676 enum isl_dim_type type
, unsigned pos
, int value
)
6678 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6679 return isl_basic_map_free(bmap
);
6680 return isl_basic_map_fix_pos_si(bmap
,
6681 isl_basic_map_offset(bmap
, type
) + pos
, value
);
6684 __isl_give isl_basic_map
*isl_basic_map_fix(__isl_take isl_basic_map
*bmap
,
6685 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6687 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6688 return isl_basic_map_free(bmap
);
6689 return isl_basic_map_fix_pos(bmap
,
6690 isl_basic_map_offset(bmap
, type
) + pos
, value
);
6693 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
6694 * to be equal to "v".
6696 __isl_give isl_basic_map
*isl_basic_map_fix_val(__isl_take isl_basic_map
*bmap
,
6697 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6701 if (!isl_val_is_int(v
))
6702 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
6703 "expecting integer value", goto error
);
6704 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6706 pos
+= isl_basic_map_offset(bmap
, type
);
6707 bmap
= isl_basic_map_fix_pos(bmap
, pos
, v
->n
);
6711 isl_basic_map_free(bmap
);
6716 /* Fix the value of the variable at position "pos" of type "type" of "bset"
6717 * to be equal to "v".
6719 __isl_give isl_basic_set
*isl_basic_set_fix_val(__isl_take isl_basic_set
*bset
,
6720 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6722 return isl_basic_map_fix_val(bset
, type
, pos
, v
);
6725 __isl_give isl_basic_set
*isl_basic_set_fix_si(__isl_take isl_basic_set
*bset
,
6726 enum isl_dim_type type
, unsigned pos
, int value
)
6728 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset
),
6732 __isl_give isl_basic_set
*isl_basic_set_fix(__isl_take isl_basic_set
*bset
,
6733 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6735 return bset_from_bmap(isl_basic_map_fix(bset_to_bmap(bset
),
6739 /* Remove the basic map at position "i" from "map" if this basic map
6740 * is (obviously) empty.
6742 static __isl_give isl_map
*remove_if_empty(__isl_take isl_map
*map
, int i
)
6749 empty
= isl_basic_map_plain_is_empty(map
->p
[i
]);
6751 return isl_map_free(map
);
6755 isl_basic_map_free(map
->p
[i
]);
6758 map
->p
[i
] = map
->p
[map
->n
];
6759 map
= isl_map_unmark_normalized(map
);
6766 /* Perform "fn" on each basic map of "map", where we may not be holding
6767 * the only reference to "map".
6768 * In particular, "fn" should be a semantics preserving operation
6769 * that we want to apply to all copies of "map". We therefore need
6770 * to be careful not to modify "map" in a way that breaks "map"
6771 * in case anything goes wrong.
6773 __isl_give isl_map
*isl_map_inline_foreach_basic_map(__isl_take isl_map
*map
,
6774 __isl_give isl_basic_map
*(*fn
)(__isl_take isl_basic_map
*bmap
))
6776 struct isl_basic_map
*bmap
;
6782 for (i
= map
->n
- 1; i
>= 0; --i
) {
6783 bmap
= isl_basic_map_copy(map
->p
[i
]);
6787 isl_basic_map_free(map
->p
[i
]);
6789 map
= remove_if_empty(map
, i
);
6800 __isl_give isl_map
*isl_map_fix_si(__isl_take isl_map
*map
,
6801 enum isl_dim_type type
, unsigned pos
, int value
)
6805 map
= isl_map_cow(map
);
6806 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
6807 return isl_map_free(map
);
6808 for (i
= map
->n
- 1; i
>= 0; --i
) {
6809 map
->p
[i
] = isl_basic_map_fix_si(map
->p
[i
], type
, pos
, value
);
6810 map
= remove_if_empty(map
, i
);
6814 map
= isl_map_unmark_normalized(map
);
6818 __isl_give isl_set
*isl_set_fix_si(__isl_take isl_set
*set
,
6819 enum isl_dim_type type
, unsigned pos
, int value
)
6821 return set_from_map(isl_map_fix_si(set_to_map(set
), type
, pos
, value
));
6824 __isl_give isl_map
*isl_map_fix(__isl_take isl_map
*map
,
6825 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6829 map
= isl_map_cow(map
);
6830 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
6831 return isl_map_free(map
);
6832 for (i
= 0; i
< map
->n
; ++i
) {
6833 map
->p
[i
] = isl_basic_map_fix(map
->p
[i
], type
, pos
, value
);
6837 map
= isl_map_unmark_normalized(map
);
6844 __isl_give isl_set
*isl_set_fix(__isl_take isl_set
*set
,
6845 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6847 return set_from_map(isl_map_fix(set_to_map(set
), type
, pos
, value
));
6850 /* Fix the value of the variable at position "pos" of type "type" of "map"
6851 * to be equal to "v".
6853 __isl_give isl_map
*isl_map_fix_val(__isl_take isl_map
*map
,
6854 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6858 map
= isl_map_cow(map
);
6862 if (!isl_val_is_int(v
))
6863 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
6864 "expecting integer value", goto error
);
6865 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
6867 for (i
= map
->n
- 1; i
>= 0; --i
) {
6868 map
->p
[i
] = isl_basic_map_fix_val(map
->p
[i
], type
, pos
,
6870 map
= remove_if_empty(map
, i
);
6874 map
= isl_map_unmark_normalized(map
);
6883 /* Fix the value of the variable at position "pos" of type "type" of "set"
6884 * to be equal to "v".
6886 __isl_give isl_set
*isl_set_fix_val(__isl_take isl_set
*set
,
6887 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6889 return isl_map_fix_val(set
, type
, pos
, v
);
6892 __isl_give isl_map
*isl_map_fix_input_si(__isl_take isl_map
*map
,
6893 unsigned input
, int value
)
6895 return isl_map_fix_si(map
, isl_dim_in
, input
, value
);
6898 __isl_give isl_set
*isl_set_fix_dim_si(__isl_take isl_set
*set
, unsigned dim
,
6901 return set_from_map(isl_map_fix_si(set_to_map(set
),
6902 isl_dim_set
, dim
, value
));
6905 static __isl_give isl_basic_map
*basic_map_bound_si(
6906 __isl_take isl_basic_map
*bmap
,
6907 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
6912 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6913 return isl_basic_map_free(bmap
);
6914 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
6916 return isl_basic_map_free(bmap
);
6917 pos
+= isl_basic_map_offset(bmap
, type
);
6918 bmap
= isl_basic_map_cow(bmap
);
6919 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
6920 j
= isl_basic_map_alloc_inequality(bmap
);
6923 isl_seq_clr(bmap
->ineq
[j
], 1 + total
);
6925 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
6926 isl_int_set_si(bmap
->ineq
[j
][0], value
);
6928 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
6929 isl_int_set_si(bmap
->ineq
[j
][0], -value
);
6931 bmap
= isl_basic_map_simplify(bmap
);
6932 return isl_basic_map_finalize(bmap
);
6934 isl_basic_map_free(bmap
);
6938 __isl_give isl_basic_map
*isl_basic_map_lower_bound_si(
6939 __isl_take isl_basic_map
*bmap
,
6940 enum isl_dim_type type
, unsigned pos
, int value
)
6942 return basic_map_bound_si(bmap
, type
, pos
, value
, 0);
6945 /* Constrain the values of the given dimension to be no greater than "value".
6947 __isl_give isl_basic_map
*isl_basic_map_upper_bound_si(
6948 __isl_take isl_basic_map
*bmap
,
6949 enum isl_dim_type type
, unsigned pos
, int value
)
6951 return basic_map_bound_si(bmap
, type
, pos
, value
, 1);
6954 static __isl_give isl_map
*map_bound_si(__isl_take isl_map
*map
,
6955 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
6959 map
= isl_map_cow(map
);
6960 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
6961 return isl_map_free(map
);
6962 for (i
= 0; i
< map
->n
; ++i
) {
6963 map
->p
[i
] = basic_map_bound_si(map
->p
[i
],
6964 type
, pos
, value
, upper
);
6968 map
= isl_map_unmark_normalized(map
);
6975 __isl_give isl_map
*isl_map_lower_bound_si(__isl_take isl_map
*map
,
6976 enum isl_dim_type type
, unsigned pos
, int value
)
6978 return map_bound_si(map
, type
, pos
, value
, 0);
6981 __isl_give isl_map
*isl_map_upper_bound_si(__isl_take isl_map
*map
,
6982 enum isl_dim_type type
, unsigned pos
, int value
)
6984 return map_bound_si(map
, type
, pos
, value
, 1);
6987 __isl_give isl_set
*isl_set_lower_bound_si(__isl_take isl_set
*set
,
6988 enum isl_dim_type type
, unsigned pos
, int value
)
6990 return set_from_map(isl_map_lower_bound_si(set_to_map(set
),
6994 __isl_give isl_set
*isl_set_upper_bound_si(__isl_take isl_set
*set
,
6995 enum isl_dim_type type
, unsigned pos
, int value
)
6997 return isl_map_upper_bound_si(set
, type
, pos
, value
);
7000 /* Bound the given variable of "bmap" from below (or above is "upper"
7001 * is set) to "value".
7003 static __isl_give isl_basic_map
*basic_map_bound(
7004 __isl_take isl_basic_map
*bmap
,
7005 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
7010 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
7011 return isl_basic_map_free(bmap
);
7012 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
7014 return isl_basic_map_free(bmap
);
7015 pos
+= isl_basic_map_offset(bmap
, type
);
7016 bmap
= isl_basic_map_cow(bmap
);
7017 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
7018 j
= isl_basic_map_alloc_inequality(bmap
);
7021 isl_seq_clr(bmap
->ineq
[j
], 1 + total
);
7023 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
7024 isl_int_set(bmap
->ineq
[j
][0], value
);
7026 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
7027 isl_int_neg(bmap
->ineq
[j
][0], value
);
7029 bmap
= isl_basic_map_simplify(bmap
);
7030 return isl_basic_map_finalize(bmap
);
7032 isl_basic_map_free(bmap
);
7036 /* Bound the given variable of "map" from below (or above is "upper"
7037 * is set) to "value".
7039 static __isl_give isl_map
*map_bound(__isl_take isl_map
*map
,
7040 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
7044 map
= isl_map_cow(map
);
7045 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
7046 return isl_map_free(map
);
7047 for (i
= map
->n
- 1; i
>= 0; --i
) {
7048 map
->p
[i
] = basic_map_bound(map
->p
[i
], type
, pos
, value
, upper
);
7049 map
= remove_if_empty(map
, i
);
7053 map
= isl_map_unmark_normalized(map
);
7057 __isl_give isl_map
*isl_map_lower_bound(__isl_take isl_map
*map
,
7058 enum isl_dim_type type
, unsigned pos
, isl_int value
)
7060 return map_bound(map
, type
, pos
, value
, 0);
7063 __isl_give isl_map
*isl_map_upper_bound(__isl_take isl_map
*map
,
7064 enum isl_dim_type type
, unsigned pos
, isl_int value
)
7066 return map_bound(map
, type
, pos
, value
, 1);
7069 __isl_give isl_set
*isl_set_lower_bound(__isl_take isl_set
*set
,
7070 enum isl_dim_type type
, unsigned pos
, isl_int value
)
7072 return isl_map_lower_bound(set
, type
, pos
, value
);
7075 __isl_give isl_set
*isl_set_upper_bound(__isl_take isl_set
*set
,
7076 enum isl_dim_type type
, unsigned pos
, isl_int value
)
7078 return isl_map_upper_bound(set
, type
, pos
, value
);
7081 /* Force the values of the variable at position "pos" of type "type" of "map"
7082 * to be no smaller than "value".
7084 __isl_give isl_map
*isl_map_lower_bound_val(__isl_take isl_map
*map
,
7085 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
7089 if (!isl_val_is_int(value
))
7090 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
7091 "expecting integer value", goto error
);
7092 map
= isl_map_lower_bound(map
, type
, pos
, value
->n
);
7093 isl_val_free(value
);
7096 isl_val_free(value
);
7101 /* Force the values of the variable at position "pos" of type "type" of "set"
7102 * to be no smaller than "value".
7104 __isl_give isl_set
*isl_set_lower_bound_val(__isl_take isl_set
*set
,
7105 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
7109 map
= set_to_map(set
);
7110 return set_from_map(isl_map_lower_bound_val(map
, type
, pos
, value
));
7113 /* Force the values of the variable at position "pos" of type "type" of "map"
7114 * to be no greater than "value".
7116 __isl_give isl_map
*isl_map_upper_bound_val(__isl_take isl_map
*map
,
7117 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
7121 if (!isl_val_is_int(value
))
7122 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
7123 "expecting integer value", goto error
);
7124 map
= isl_map_upper_bound(map
, type
, pos
, value
->n
);
7125 isl_val_free(value
);
7128 isl_val_free(value
);
7133 /* Force the values of the variable at position "pos" of type "type" of "set"
7134 * to be no greater than "value".
7136 __isl_give isl_set
*isl_set_upper_bound_val(__isl_take isl_set
*set
,
7137 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
7141 map
= set_to_map(set
);
7142 return set_from_map(isl_map_upper_bound_val(map
, type
, pos
, value
));
7145 /* If "mv" has an explicit domain, then intersect the domain of "map"
7146 * with this explicit domain.
7148 * An isl_multi_val object never has an explicit domain,
7149 * so simply return "map".
7151 static __isl_give isl_map
*isl_map_intersect_multi_val_explicit_domain(
7152 __isl_take isl_map
*map
, __isl_keep isl_multi_val
*mv
)
7159 #include "isl_map_bound_templ.c"
7161 /* Apply "map_bound" to "set" with the corresponding value in "bound"
7162 * for each set dimension, by treating the set as a map.
7164 static __isl_give isl_set
*set_bound_multi_val(__isl_take isl_set
*set
,
7165 __isl_take isl_multi_val
*bound
,
7166 __isl_give isl_map
*map_bound(__isl_take isl_map
*map
,
7167 unsigned pos
, __isl_take isl_val
*value
))
7171 map
= set_to_map(set
);
7172 return set_from_map(map_bound_multi_val(map
, bound
, map_bound
));
7177 #include "isl_map_bound_templ.c"
7179 /* Apply "map_bound" to "set" with the corresponding value in "bound"
7180 * for each set dimension, by converting the set and the bound
7181 * to objects living in a map space.
7183 static __isl_give isl_set
*set_bound_multi_pw_aff(__isl_take isl_set
*set
,
7184 __isl_take isl_multi_pw_aff
*bound
,
7185 __isl_give isl_map
*set_bound(__isl_take isl_map
*map
,
7186 unsigned pos
, __isl_take TYPE
*value
))
7190 map
= isl_map_from_range(set
);
7191 bound
= isl_multi_pw_aff_from_range(bound
);
7192 map
= map_bound_multi_pw_aff(map
, bound
, set_bound
);
7193 return isl_map_range(map
);
7196 /* Wrapper around isl_map_lower_bound_val for use in map_bound_multi_val,
7197 * setting a bound on the given output dimension.
7199 static __isl_give isl_map
*map_lower_bound_val(__isl_take isl_map
*map
,
7200 unsigned pos
, __isl_take isl_val
*v
)
7202 return isl_map_lower_bound_val(map
, isl_dim_out
, pos
, v
);
7205 /* Force the values of the set dimensions of "set"
7206 * to be no smaller than the corresponding values in "lower".
7208 __isl_give isl_set
*isl_set_lower_bound_multi_val(__isl_take isl_set
*set
,
7209 __isl_take isl_multi_val
*lower
)
7211 return set_bound_multi_val(set
, lower
, &map_lower_bound_val
);
7214 /* Wrapper around isl_map_upper_bound_val for use in map_bound_multi_val,
7215 * setting a bound on the given output dimension.
7217 static __isl_give isl_map
*map_upper_bound_val(__isl_take isl_map
*map
,
7218 unsigned pos
, __isl_take isl_val
*v
)
7220 return isl_map_upper_bound_val(map
, isl_dim_out
, pos
, v
);
7223 /* Force the values of the set dimensions of "set"
7224 * to be no greater than the corresponding values in "upper".
7226 __isl_give isl_set
*isl_set_upper_bound_multi_val(__isl_take isl_set
*set
,
7227 __isl_take isl_multi_val
*upper
)
7229 return set_bound_multi_val(set
, upper
, &map_upper_bound_val
);
7232 /* Force the symbolic constant expression "bound"
7233 * to satisfy the relation "order" with respect to
7234 * the output variable at position "pos" of "map".
7236 * Create an affine expression representing the output variable
7237 * in terms of the range and
7238 * compare it using "order" to "bound" (defined on the domain).
7239 * The result is a relation between elements in domain and range that
7240 * can be intersected with "map".
7242 static __isl_give isl_map
*map_bound_pw_aff(__isl_take isl_map
*map
,
7243 unsigned pos
, __isl_take isl_pw_aff
*bound
,
7244 __isl_give isl_map
*(*order
)(__isl_take isl_pw_aff
*pa1
,
7245 __isl_take isl_pw_aff
*pa2
))
7248 isl_local_space
*ls
;
7251 space
= isl_space_range(isl_map_get_space(map
));
7252 ls
= isl_local_space_from_space(space
);
7253 var
= isl_pw_aff_var_on_domain(ls
, isl_dim_set
, pos
);
7254 map
= isl_map_intersect(map
, order(bound
, var
));
7258 /* Force the values of the output variable at position "pos" of "map"
7259 * to be no smaller than the symbolic constant expression "lower".
7261 static __isl_give isl_map
*map_lower_bound_pw_aff(__isl_take isl_map
*map
,
7262 unsigned pos
, __isl_take isl_pw_aff
*lower
)
7264 return map_bound_pw_aff(map
, pos
, lower
, &isl_pw_aff_le_map
);
7267 /* Force the values of the output variable at position "pos" of "map"
7268 * to be no greater than the symbolic constant expression "upper".
7270 static __isl_give isl_map
*map_upper_bound_pw_aff(__isl_take isl_map
*map
,
7271 unsigned pos
, __isl_take isl_pw_aff
*upper
)
7273 return map_bound_pw_aff(map
, pos
, upper
, &isl_pw_aff_ge_map
);
7276 /* Force the values of the set dimensions of "set"
7277 * to be no smaller than the corresponding constant symbolic expressions
7280 __isl_give isl_set
*isl_set_lower_bound_multi_pw_aff(__isl_take isl_set
*set
,
7281 __isl_take isl_multi_pw_aff
*lower
)
7283 return set_bound_multi_pw_aff(set
, lower
, &map_lower_bound_pw_aff
);
7286 /* Force the values of the set dimensions of "set"
7287 * to be no greater than the corresponding constant symbolic expressions
7290 __isl_give isl_set
*isl_set_upper_bound_multi_pw_aff(__isl_take isl_set
*set
,
7291 __isl_take isl_multi_pw_aff
*upper
)
7293 return set_bound_multi_pw_aff(set
, upper
, &map_upper_bound_pw_aff
);
7296 /* Force the values of the output dimensions of "map"
7297 * to be no smaller than the corresponding constant symbolic expressions
7300 __isl_give isl_map
*isl_map_lower_bound_multi_pw_aff(__isl_take isl_map
*map
,
7301 __isl_take isl_multi_pw_aff
*lower
)
7303 return map_bound_multi_pw_aff(map
, lower
, &map_lower_bound_pw_aff
);
7306 /* Force the values of the output dimensions of "map"
7307 * to be no greater than the corresponding constant symbolic expressions
7310 __isl_give isl_map
*isl_map_upper_bound_multi_pw_aff(__isl_take isl_map
*map
,
7311 __isl_take isl_multi_pw_aff
*upper
)
7313 return map_bound_multi_pw_aff(map
, upper
, &map_upper_bound_pw_aff
);
7316 /* Bound the given variable of "bset" from below (or above is "upper"
7317 * is set) to "value".
7319 static __isl_give isl_basic_set
*isl_basic_set_bound(
7320 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned pos
,
7321 isl_int value
, int upper
)
7323 return bset_from_bmap(basic_map_bound(bset_to_bmap(bset
),
7324 type
, pos
, value
, upper
));
7327 /* Bound the given variable of "bset" from below (or above is "upper"
7328 * is set) to "value".
7330 static __isl_give isl_basic_set
*isl_basic_set_bound_val(
7331 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned pos
,
7332 __isl_take isl_val
*value
, int upper
)
7336 if (!isl_val_is_int(value
))
7337 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
7338 "expecting integer value", goto error
);
7339 bset
= isl_basic_set_bound(bset
, type
, pos
, value
->n
, upper
);
7340 isl_val_free(value
);
7343 isl_val_free(value
);
7344 isl_basic_set_free(bset
);
7348 /* Bound the given variable of "bset" from below to "value".
7350 __isl_give isl_basic_set
*isl_basic_set_lower_bound_val(
7351 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned pos
,
7352 __isl_take isl_val
*value
)
7354 return isl_basic_set_bound_val(bset
, type
, pos
, value
, 0);
7357 /* Bound the given variable of "bset" from above to "value".
7359 __isl_give isl_basic_set
*isl_basic_set_upper_bound_val(
7360 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned pos
,
7361 __isl_take isl_val
*value
)
7363 return isl_basic_set_bound_val(bset
, type
, pos
, value
, 1);
7366 __isl_give isl_map
*isl_map_reverse(__isl_take isl_map
*map
)
7368 return isl_map_transform(map
, &isl_space_reverse
,
7369 &isl_basic_map_reverse
);
7372 /* Given a map (A -> B) -> C, return the corresponding map (B -> A) -> C.
7374 __isl_give isl_map
*isl_map_domain_reverse(__isl_take isl_map
*map
)
7376 return isl_map_transform(map
, &isl_space_domain_reverse
,
7377 &isl_basic_map_domain_reverse
);
7380 /* Given a map A -> (B -> C), return the corresponding map A -> (C -> B).
7382 __isl_give isl_map
*isl_map_range_reverse(__isl_take isl_map
*map
)
7384 return isl_map_transform(map
, &isl_space_range_reverse
,
7385 &isl_basic_map_range_reverse
);
7388 /* Given a set (A -> B), return the corresponding set (B -> A).
7390 __isl_give isl_set
*isl_set_wrapped_reverse(__isl_take isl_set
*set
)
7392 isl_map
*map
= set_to_map(set
);
7394 map
= isl_map_transform(map
, &isl_space_wrapped_reverse
,
7395 &isl_basic_map_set_reverse
);
7396 return set_from_map(map
);
7400 #define TYPE isl_pw_multi_aff
7402 #define SUFFIX _pw_multi_aff
7404 #define EMPTY isl_pw_multi_aff_empty
7406 #define ADD isl_pw_multi_aff_union_add
7407 #include "isl_map_lexopt_templ.c"
7409 /* Given a map "map", compute the lexicographically minimal
7410 * (or maximal) image element for each domain element in dom,
7411 * in the form of an isl_pw_multi_aff.
7412 * If "empty" is not NULL, then set *empty to those elements in dom that
7413 * do not have an image element.
7414 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
7415 * should be computed over the domain of "map". "empty" is also NULL
7418 * We first compute the lexicographically minimal or maximal element
7419 * in the first basic map. This results in a partial solution "res"
7420 * and a subset "todo" of dom that still need to be handled.
7421 * We then consider each of the remaining maps in "map" and successively
7422 * update both "res" and "todo".
7423 * If "empty" is NULL, then the todo sets are not needed and therefore
7424 * also not computed.
7426 static __isl_give isl_pw_multi_aff
*isl_map_partial_lexopt_aligned_pw_multi_aff(
7427 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
7428 __isl_give isl_set
**empty
, unsigned flags
)
7432 isl_pw_multi_aff
*res
;
7435 full
= ISL_FL_ISSET(flags
, ISL_OPT_FULL
);
7436 if (!map
|| (!full
&& !dom
))
7439 if (isl_map_plain_is_empty(map
)) {
7444 return isl_pw_multi_aff_from_map(map
);
7447 res
= basic_map_partial_lexopt_pw_multi_aff(
7448 isl_basic_map_copy(map
->p
[0]),
7449 isl_set_copy(dom
), empty
, flags
);
7453 for (i
= 1; i
< map
->n
; ++i
) {
7454 isl_pw_multi_aff
*res_i
;
7456 res_i
= basic_map_partial_lexopt_pw_multi_aff(
7457 isl_basic_map_copy(map
->p
[i
]),
7458 isl_set_copy(dom
), empty
, flags
);
7460 if (ISL_FL_ISSET(flags
, ISL_OPT_MAX
))
7461 res
= isl_pw_multi_aff_union_lexmax(res
, res_i
);
7463 res
= isl_pw_multi_aff_union_lexmin(res
, res_i
);
7466 todo
= isl_set_intersect(todo
, *empty
);
7485 #define TYPE isl_map
7489 #define EMPTY isl_map_empty
7491 #define ADD isl_map_union_disjoint
7492 #include "isl_map_lexopt_templ.c"
7494 /* Given a map "map", compute the lexicographically minimal
7495 * (or maximal) image element for each domain element in "dom",
7496 * in the form of an isl_map.
7497 * If "empty" is not NULL, then set *empty to those elements in "dom" that
7498 * do not have an image element.
7499 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
7500 * should be computed over the domain of "map". "empty" is also NULL
7503 * If the input consists of more than one disjunct, then first
7504 * compute the desired result in the form of an isl_pw_multi_aff and
7505 * then convert that into an isl_map.
7507 * This function used to have an explicit implementation in terms
7508 * of isl_maps, but it would continually intersect the domains of
7509 * partial results with the complement of the domain of the next
7510 * partial solution, potentially leading to an explosion in the number
7511 * of disjuncts if there are several disjuncts in the input.
7512 * An even earlier implementation of this function would look for
7513 * better results in the domain of the partial result and for extra
7514 * results in the complement of this domain, which would lead to
7515 * even more splintering.
7517 static __isl_give isl_map
*isl_map_partial_lexopt_aligned(
7518 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
7519 __isl_give isl_set
**empty
, unsigned flags
)
7522 struct isl_map
*res
;
7523 isl_pw_multi_aff
*pma
;
7525 full
= ISL_FL_ISSET(flags
, ISL_OPT_FULL
);
7526 if (!map
|| (!full
&& !dom
))
7529 if (isl_map_plain_is_empty(map
)) {
7538 res
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[0]),
7544 pma
= isl_map_partial_lexopt_aligned_pw_multi_aff(map
, dom
, empty
,
7546 return isl_map_from_pw_multi_aff_internal(pma
);
7555 __isl_give isl_map
*isl_map_partial_lexmax(
7556 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
7557 __isl_give isl_set
**empty
)
7559 return isl_map_partial_lexopt(map
, dom
, empty
, ISL_OPT_MAX
);
7562 __isl_give isl_map
*isl_map_partial_lexmin(
7563 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
7564 __isl_give isl_set
**empty
)
7566 return isl_map_partial_lexopt(map
, dom
, empty
, 0);
7569 __isl_give isl_set
*isl_set_partial_lexmin(
7570 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
7571 __isl_give isl_set
**empty
)
7573 return set_from_map(isl_map_partial_lexmin(set_to_map(set
),
7577 __isl_give isl_set
*isl_set_partial_lexmax(
7578 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
7579 __isl_give isl_set
**empty
)
7581 return set_from_map(isl_map_partial_lexmax(set_to_map(set
),
7585 /* Compute the lexicographic minimum (or maximum if "flags" includes
7586 * ISL_OPT_MAX) of "bset" over its parametric domain.
7588 __isl_give isl_set
*isl_basic_set_lexopt(__isl_take isl_basic_set
*bset
,
7591 return isl_basic_map_lexopt(bset
, flags
);
7594 __isl_give isl_map
*isl_basic_map_lexmax(__isl_take isl_basic_map
*bmap
)
7596 return isl_basic_map_lexopt(bmap
, ISL_OPT_MAX
);
7599 __isl_give isl_set
*isl_basic_set_lexmin(__isl_take isl_basic_set
*bset
)
7601 return set_from_map(isl_basic_map_lexmin(bset_to_bmap(bset
)));
7604 __isl_give isl_set
*isl_basic_set_lexmax(__isl_take isl_basic_set
*bset
)
7606 return set_from_map(isl_basic_map_lexmax(bset_to_bmap(bset
)));
7609 /* Compute the lexicographic minimum of "bset" over its parametric domain
7610 * for the purpose of quantifier elimination.
7611 * That is, find an explicit representation for all the existentially
7612 * quantified variables in "bset" by computing their lexicographic
7615 static __isl_give isl_set
*isl_basic_set_lexmin_compute_divs(
7616 __isl_take isl_basic_set
*bset
)
7618 return isl_basic_set_lexopt(bset
, ISL_OPT_QE
);
7621 /* Given a basic map with one output dimension, compute the minimum or
7622 * maximum of that dimension as an isl_pw_aff.
7624 * Compute the optimum as a lexicographic optimum over the single
7625 * output dimension and extract the single isl_pw_aff from the result.
7627 static __isl_give isl_pw_aff
*basic_map_dim_opt(__isl_keep isl_basic_map
*bmap
,
7630 isl_pw_multi_aff
*pma
;
7633 bmap
= isl_basic_map_copy(bmap
);
7634 pma
= isl_basic_map_lexopt_pw_multi_aff(bmap
, max
? ISL_OPT_MAX
: 0);
7635 pwaff
= isl_pw_multi_aff_get_pw_aff(pma
, 0);
7636 isl_pw_multi_aff_free(pma
);
7641 /* Compute the minimum or maximum of the given output dimension
7642 * as a function of the parameters and the input dimensions,
7643 * but independently of the other output dimensions.
7645 * We first project out the other output dimension and then compute
7646 * the "lexicographic" maximum in each basic map, combining the results
7647 * using isl_pw_aff_union_max.
7649 static __isl_give isl_pw_aff
*map_dim_opt(__isl_take isl_map
*map
, int pos
,
7656 n_out
= isl_map_dim(map
, isl_dim_out
);
7658 map
= isl_map_free(map
);
7659 map
= isl_map_project_out(map
, isl_dim_out
, pos
+ 1, n_out
- (pos
+ 1));
7660 map
= isl_map_project_out(map
, isl_dim_out
, 0, pos
);
7665 isl_space
*space
= isl_map_get_space(map
);
7667 return isl_pw_aff_empty(space
);
7670 pwaff
= basic_map_dim_opt(map
->p
[0], max
);
7671 for (i
= 1; i
< map
->n
; ++i
) {
7672 isl_pw_aff
*pwaff_i
;
7674 pwaff_i
= basic_map_dim_opt(map
->p
[i
], max
);
7675 pwaff
= isl_pw_aff_union_opt(pwaff
, pwaff_i
, max
);
7683 /* Compute the minimum of the given output dimension as a function of the
7684 * parameters and input dimensions, but independently of
7685 * the other output dimensions.
7687 __isl_give isl_pw_aff
*isl_map_dim_min(__isl_take isl_map
*map
, int pos
)
7689 return map_dim_opt(map
, pos
, 0);
7692 /* Compute the maximum of the given output dimension as a function of the
7693 * parameters and input dimensions, but independently of
7694 * the other output dimensions.
7696 __isl_give isl_pw_aff
*isl_map_dim_max(__isl_take isl_map
*map
, int pos
)
7698 return map_dim_opt(map
, pos
, 1);
7701 /* Compute the minimum or maximum of the given set dimension
7702 * as a function of the parameters,
7703 * but independently of the other set dimensions.
7705 static __isl_give isl_pw_aff
*set_dim_opt(__isl_take isl_set
*set
, int pos
,
7708 return map_dim_opt(set
, pos
, max
);
7711 /* Compute the maximum of the given set dimension as a function of the
7712 * parameters, but independently of the other set dimensions.
7714 __isl_give isl_pw_aff
*isl_set_dim_max(__isl_take isl_set
*set
, int pos
)
7716 return set_dim_opt(set
, pos
, 1);
7719 /* Compute the minimum of the given set dimension as a function of the
7720 * parameters, but independently of the other set dimensions.
7722 __isl_give isl_pw_aff
*isl_set_dim_min(__isl_take isl_set
*set
, int pos
)
7724 return set_dim_opt(set
, pos
, 0);
7727 /* Apply a preimage specified by "mat" on the parameters of "bset".
7728 * bset is assumed to have only parameters and divs.
7730 static __isl_give isl_basic_set
*basic_set_parameter_preimage(
7731 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*mat
)
7735 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
7736 if (nparam
< 0 || !mat
)
7739 bset
->dim
= isl_space_cow(bset
->dim
);
7743 isl_assert(bset
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
7745 bset
->dim
->nparam
= 0;
7746 bset
->dim
->n_out
= nparam
;
7747 bset
= isl_basic_set_preimage(bset
, mat
);
7749 bset
->dim
->nparam
= bset
->dim
->n_out
;
7750 bset
->dim
->n_out
= 0;
7755 isl_basic_set_free(bset
);
7759 /* Apply a preimage specified by "mat" on the parameters of "set".
7760 * set is assumed to have only parameters and divs.
7762 static __isl_give isl_set
*set_parameter_preimage(__isl_take isl_set
*set
,
7763 __isl_take isl_mat
*mat
)
7768 nparam
= isl_set_dim(set
, isl_dim_param
);
7769 if (nparam
< 0 || !mat
)
7772 if (mat
->n_row
!= 1 + nparam
)
7773 isl_die(isl_set_get_ctx(set
), isl_error_internal
,
7774 "unexpected number of rows", goto error
);
7776 space
= isl_set_get_space(set
);
7777 space
= isl_space_move_dims(space
, isl_dim_set
, 0,
7778 isl_dim_param
, 0, nparam
);
7779 set
= isl_set_reset_space(set
, space
);
7780 set
= isl_set_preimage(set
, mat
);
7781 nparam
= isl_set_dim(set
, isl_dim_out
);
7783 set
= isl_set_free(set
);
7784 space
= isl_set_get_space(set
);
7785 space
= isl_space_move_dims(space
, isl_dim_param
, 0,
7786 isl_dim_out
, 0, nparam
);
7787 set
= isl_set_reset_space(set
, space
);
7795 /* Intersect the basic set "bset" with the affine space specified by the
7796 * equalities in "eq".
7798 static __isl_give isl_basic_set
*basic_set_append_equalities(
7799 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*eq
)
7807 bset
= isl_basic_set_extend(bset
, 0, eq
->n_row
, 0);
7811 len
= isl_basic_set_offset(bset
, isl_dim_div
) + bset
->extra
;
7812 for (i
= 0; i
< eq
->n_row
; ++i
) {
7813 k
= isl_basic_set_alloc_equality(bset
);
7816 isl_seq_cpy(bset
->eq
[k
], eq
->row
[i
], eq
->n_col
);
7817 isl_seq_clr(bset
->eq
[k
] + eq
->n_col
, len
- eq
->n_col
);
7821 bset
= isl_basic_set_gauss(bset
, NULL
);
7822 bset
= isl_basic_set_finalize(bset
);
7827 isl_basic_set_free(bset
);
7831 /* Intersect the set "set" with the affine space specified by the
7832 * equalities in "eq".
7834 static __isl_give isl_set
*set_append_equalities(__isl_take isl_set
*set
,
7835 __isl_take isl_mat
*eq
)
7842 for (i
= 0; i
< set
->n
; ++i
) {
7843 set
->p
[i
] = basic_set_append_equalities(set
->p
[i
],
7856 /* Given a basic set "bset" that only involves parameters and existentially
7857 * quantified variables, return the index of the first equality
7858 * that only involves parameters. If there is no such equality then
7859 * return bset->n_eq.
7861 * This function assumes that isl_basic_set_gauss has been called on "bset".
7863 static int first_parameter_equality(__isl_keep isl_basic_set
*bset
)
7866 isl_size nparam
, n_div
;
7868 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
7869 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
7870 if (nparam
< 0 || n_div
< 0)
7873 for (i
= 0, j
= n_div
- 1; i
< bset
->n_eq
&& j
>= 0; --j
) {
7874 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ j
]))
7881 /* Compute an explicit representation for the existentially quantified
7882 * variables in "bset" by computing the "minimal value" of the set
7883 * variables. Since there are no set variables, the computation of
7884 * the minimal value essentially computes an explicit representation
7885 * of the non-empty part(s) of "bset".
7887 * The input only involves parameters and existentially quantified variables.
7888 * All equalities among parameters have been removed.
7890 * Since the existentially quantified variables in the result are in general
7891 * going to be different from those in the input, we first replace
7892 * them by the minimal number of variables based on their equalities.
7893 * This should simplify the parametric integer programming.
7895 static __isl_give isl_set
*base_compute_divs(__isl_take isl_basic_set
*bset
)
7897 isl_morph
*morph1
, *morph2
;
7903 if (bset
->n_eq
== 0)
7904 return isl_basic_set_lexmin_compute_divs(bset
);
7906 morph1
= isl_basic_set_parameter_compression(bset
);
7907 bset
= isl_morph_basic_set(isl_morph_copy(morph1
), bset
);
7908 bset
= isl_basic_set_lift(bset
);
7909 morph2
= isl_basic_set_variable_compression(bset
, isl_dim_set
);
7910 bset
= isl_morph_basic_set(morph2
, bset
);
7911 n
= isl_basic_set_dim(bset
, isl_dim_set
);
7913 bset
= isl_basic_set_free(bset
);
7914 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
7916 set
= isl_basic_set_lexmin_compute_divs(bset
);
7918 set
= isl_morph_set(isl_morph_inverse(morph1
), set
);
7923 /* Project the given basic set onto its parameter domain, possibly introducing
7924 * new, explicit, existential variables in the constraints.
7925 * The input has parameters and (possibly implicit) existential variables.
7926 * The output has the same parameters, but only
7927 * explicit existentially quantified variables.
7929 * The actual projection is performed by pip, but pip doesn't seem
7930 * to like equalities very much, so we first remove the equalities
7931 * among the parameters by performing a variable compression on
7932 * the parameters. Afterward, an inverse transformation is performed
7933 * and the equalities among the parameters are inserted back in.
7935 * The variable compression on the parameters may uncover additional
7936 * equalities that were only implicit before. We therefore check
7937 * if there are any new parameter equalities in the result and
7938 * if so recurse. The removal of parameter equalities is required
7939 * for the parameter compression performed by base_compute_divs.
7941 static __isl_give isl_set
*parameter_compute_divs(
7942 __isl_take isl_basic_set
*bset
)
7946 struct isl_mat
*T
, *T2
;
7947 struct isl_set
*set
;
7950 bset
= isl_basic_set_cow(bset
);
7954 if (bset
->n_eq
== 0)
7955 return base_compute_divs(bset
);
7957 bset
= isl_basic_set_gauss(bset
, NULL
);
7960 if (isl_basic_set_plain_is_empty(bset
))
7961 return isl_set_from_basic_set(bset
);
7963 i
= first_parameter_equality(bset
);
7964 if (i
== bset
->n_eq
)
7965 return base_compute_divs(bset
);
7967 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
7969 return isl_set_from_basic_set(isl_basic_set_free(bset
));
7970 eq
= isl_mat_sub_alloc6(bset
->ctx
, bset
->eq
, i
, bset
->n_eq
- i
,
7972 eq
= isl_mat_cow(eq
);
7973 T
= isl_mat_variable_compression(isl_mat_copy(eq
), &T2
);
7974 if (T
&& T
->n_col
== 0) {
7978 bset
= isl_basic_set_set_to_empty(bset
);
7979 return isl_set_from_basic_set(bset
);
7981 bset
= basic_set_parameter_preimage(bset
, T
);
7983 i
= first_parameter_equality(bset
);
7986 else if (i
== bset
->n_eq
)
7987 set
= base_compute_divs(bset
);
7989 set
= parameter_compute_divs(bset
);
7990 set
= set_parameter_preimage(set
, T2
);
7991 set
= set_append_equalities(set
, eq
);
7995 /* Insert the divs from "ls" before those of "bmap".
7997 * The number of columns is not changed, which means that the last
7998 * dimensions of "bmap" are being reintepreted as the divs from "ls".
7999 * The caller is responsible for removing the same number of dimensions
8000 * from the space of "bmap".
8002 static __isl_give isl_basic_map
*insert_divs_from_local_space(
8003 __isl_take isl_basic_map
*bmap
, __isl_keep isl_local_space
*ls
)
8009 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
8011 return isl_basic_map_free(bmap
);
8015 old_n_div
= bmap
->n_div
;
8016 bmap
= insert_div_rows(bmap
, n_div
);
8020 for (i
= 0; i
< n_div
; ++i
) {
8021 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], ls
->div
->n_col
);
8022 isl_seq_clr(bmap
->div
[i
] + ls
->div
->n_col
, old_n_div
);
8028 /* Replace the space of "bmap" by the space and divs of "ls".
8030 * If "ls" has any divs, then we simplify the result since we may
8031 * have discovered some additional equalities that could simplify
8032 * the div expressions.
8034 static __isl_give isl_basic_map
*basic_replace_space_by_local_space(
8035 __isl_take isl_basic_map
*bmap
, __isl_take isl_local_space
*ls
)
8039 bmap
= isl_basic_map_cow(bmap
);
8040 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
8041 if (!bmap
|| n_div
< 0)
8044 bmap
= insert_divs_from_local_space(bmap
, ls
);
8048 isl_space_free(bmap
->dim
);
8049 bmap
->dim
= isl_local_space_get_space(ls
);
8053 isl_local_space_free(ls
);
8055 bmap
= isl_basic_map_simplify(bmap
);
8056 bmap
= isl_basic_map_finalize(bmap
);
8059 isl_basic_map_free(bmap
);
8060 isl_local_space_free(ls
);
8064 /* Replace the space of "map" by the space and divs of "ls".
8066 static __isl_give isl_map
*replace_space_by_local_space(__isl_take isl_map
*map
,
8067 __isl_take isl_local_space
*ls
)
8071 map
= isl_map_cow(map
);
8075 for (i
= 0; i
< map
->n
; ++i
) {
8076 map
->p
[i
] = basic_replace_space_by_local_space(map
->p
[i
],
8077 isl_local_space_copy(ls
));
8081 isl_space_free(isl_map_take_space(map
));
8082 map
= isl_map_restore_space(map
, isl_local_space_get_space(ls
));
8084 isl_local_space_free(ls
);
8087 isl_local_space_free(ls
);
8092 /* Compute an explicit representation for the existentially
8093 * quantified variables for which do not know any explicit representation yet.
8095 * We first sort the existentially quantified variables so that the
8096 * existentially quantified variables for which we already have an explicit
8097 * representation are placed before those for which we do not.
8098 * The input dimensions, the output dimensions and the existentially
8099 * quantified variables for which we already have an explicit
8100 * representation are then turned into parameters.
8101 * compute_divs returns a map with the same parameters and
8102 * no input or output dimensions and the dimension specification
8103 * is reset to that of the input, including the existentially quantified
8104 * variables for which we already had an explicit representation.
8106 static __isl_give isl_map
*compute_divs(__isl_take isl_basic_map
*bmap
)
8108 struct isl_basic_set
*bset
;
8109 struct isl_set
*set
;
8110 struct isl_map
*map
;
8112 isl_local_space
*ls
;
8119 bmap
= isl_basic_map_sort_divs(bmap
);
8120 bmap
= isl_basic_map_cow(bmap
);
8124 n_known
= isl_basic_map_first_unknown_div(bmap
);
8125 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
8126 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
8127 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
8128 if (n_known
< 0 || nparam
< 0 || n_in
< 0 || n_out
< 0)
8129 return isl_map_from_basic_map(isl_basic_map_free(bmap
));
8131 space
= isl_space_set_alloc(bmap
->ctx
,
8132 nparam
+ n_in
+ n_out
+ n_known
, 0);
8136 ls
= isl_basic_map_get_local_space(bmap
);
8137 ls
= isl_local_space_drop_dims(ls
, isl_dim_div
,
8138 n_known
, bmap
->n_div
- n_known
);
8140 for (i
= n_known
; i
< bmap
->n_div
; ++i
)
8141 swap_div(bmap
, i
- n_known
, i
);
8142 bmap
->n_div
-= n_known
;
8143 bmap
->extra
-= n_known
;
8145 bmap
= isl_basic_map_reset_space(bmap
, space
);
8146 bset
= bset_from_bmap(bmap
);
8148 set
= parameter_compute_divs(bset
);
8149 map
= set_to_map(set
);
8150 map
= replace_space_by_local_space(map
, ls
);
8154 isl_basic_map_free(bmap
);
8158 /* Is the integer division at position "div" of "bmap" integral?
8159 * That is, does it have denominator 1?
8161 isl_bool
isl_basic_map_div_is_integral(__isl_keep isl_basic_map
*bmap
, int div
)
8163 if (isl_basic_map_check_range(bmap
, isl_dim_div
, div
, 1) < 0)
8164 return isl_bool_error
;
8165 return isl_int_is_one(bmap
->div
[div
][0]);
8168 /* Remove the explicit representation of local variable "div",
8171 __isl_give isl_basic_map
*isl_basic_map_mark_div_unknown(
8172 __isl_take isl_basic_map
*bmap
, int div
)
8176 unknown
= isl_basic_map_div_is_marked_unknown(bmap
, div
);
8178 return isl_basic_map_free(bmap
);
8182 bmap
= isl_basic_map_cow(bmap
);
8185 isl_int_set_si(bmap
->div
[div
][0], 0);
8189 /* Is local variable "div" of "bmap" marked as not having an explicit
8191 * Note that even if "div" is not marked in this way and therefore
8192 * has an explicit representation, this representation may still
8193 * depend (indirectly) on other local variables that do not
8194 * have an explicit representation.
8196 isl_bool
isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map
*bmap
,
8199 if (isl_basic_map_check_range(bmap
, isl_dim_div
, div
, 1) < 0)
8200 return isl_bool_error
;
8201 return isl_int_is_zero(bmap
->div
[div
][0]);
8204 /* Return the position of the first local variable that does not
8205 * have an explicit representation.
8206 * Return the total number of local variables if they all have
8207 * an explicit representation.
8208 * Return -1 on error.
8210 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map
*bmap
)
8217 for (i
= 0; i
< bmap
->n_div
; ++i
) {
8218 if (!isl_basic_map_div_is_known(bmap
, i
))
8224 /* Return the position of the first local variable that does not
8225 * have an explicit representation.
8226 * Return the total number of local variables if they all have
8227 * an explicit representation.
8228 * Return -1 on error.
8230 int isl_basic_set_first_unknown_div(__isl_keep isl_basic_set
*bset
)
8232 return isl_basic_map_first_unknown_div(bset
);
8235 /* Does "bmap" have an explicit representation for all local variables?
8237 isl_bool
isl_basic_map_divs_known(__isl_keep isl_basic_map
*bmap
)
8242 n
= isl_basic_map_dim(bmap
, isl_dim_div
);
8243 first
= isl_basic_map_first_unknown_div(bmap
);
8244 if (n
< 0 || first
< 0)
8245 return isl_bool_error
;
8249 /* Do all basic maps in "map" have an explicit representation
8250 * for all local variables?
8252 isl_bool
isl_map_divs_known(__isl_keep isl_map
*map
)
8257 return isl_bool_error
;
8259 for (i
= 0; i
< map
->n
; ++i
) {
8260 int known
= isl_basic_map_divs_known(map
->p
[i
]);
8265 return isl_bool_true
;
8268 /* If bmap contains any unknown divs, then compute explicit
8269 * expressions for them. However, this computation may be
8270 * quite expensive, so first try to remove divs that aren't
8273 __isl_give isl_map
*isl_basic_map_compute_divs(__isl_take isl_basic_map
*bmap
)
8276 struct isl_map
*map
;
8278 known
= isl_basic_map_divs_known(bmap
);
8282 return isl_map_from_basic_map(bmap
);
8284 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
8286 known
= isl_basic_map_divs_known(bmap
);
8290 return isl_map_from_basic_map(bmap
);
8292 map
= compute_divs(bmap
);
8295 isl_basic_map_free(bmap
);
8299 __isl_give isl_map
*isl_map_compute_divs(__isl_take isl_map
*map
)
8303 struct isl_map
*res
;
8310 known
= isl_map_divs_known(map
);
8318 res
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[0]));
8319 for (i
= 1 ; i
< map
->n
; ++i
) {
8321 r2
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[i
]));
8322 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
8323 res
= isl_map_union_disjoint(res
, r2
);
8325 res
= isl_map_union(res
, r2
);
8332 __isl_give isl_set
*isl_basic_set_compute_divs(__isl_take isl_basic_set
*bset
)
8334 return set_from_map(isl_basic_map_compute_divs(bset_to_bmap(bset
)));
8337 __isl_give isl_set
*isl_set_compute_divs(__isl_take isl_set
*set
)
8339 return set_from_map(isl_map_compute_divs(set_to_map(set
)));
8342 __isl_give isl_set
*isl_map_domain(__isl_take isl_map
*map
)
8347 n_out
= isl_map_dim(map
, isl_dim_out
);
8349 return set_from_map(isl_map_free(map
));
8350 space
= isl_space_domain(isl_map_get_space(map
));
8352 map
= isl_map_project_out(map
, isl_dim_out
, 0, n_out
);
8354 return set_from_map(isl_map_reset_space(map
, space
));
8357 /* Return the union of "map1" and "map2", where we assume for now that
8358 * "map1" and "map2" are disjoint. Note that the basic maps inside
8359 * "map1" or "map2" may not be disjoint from each other.
8360 * Also note that this function is also called from isl_map_union,
8361 * which takes care of handling the situation where "map1" and "map2"
8362 * may not be disjoint.
8364 * If one of the inputs is empty, we can simply return the other input.
8365 * Similarly, if one of the inputs is universal, then it is equal to the union.
8367 static __isl_give isl_map
*map_union_disjoint(__isl_take isl_map
*map1
,
8368 __isl_take isl_map
*map2
)
8372 struct isl_map
*map
= NULL
;
8375 if (isl_map_check_equal_space(map1
, map2
) < 0)
8387 is_universe
= isl_map_plain_is_universe(map1
);
8388 if (is_universe
< 0)
8395 is_universe
= isl_map_plain_is_universe(map2
);
8396 if (is_universe
< 0)
8403 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
8404 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
8405 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
8407 map
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
8408 map1
->n
+ map2
->n
, flags
);
8411 for (i
= 0; i
< map1
->n
; ++i
) {
8412 map
= isl_map_add_basic_map(map
,
8413 isl_basic_map_copy(map1
->p
[i
]));
8417 for (i
= 0; i
< map2
->n
; ++i
) {
8418 map
= isl_map_add_basic_map(map
,
8419 isl_basic_map_copy(map2
->p
[i
]));
8433 /* Return the union of "map1" and "map2", where "map1" and "map2" are
8434 * guaranteed to be disjoint by the caller.
8436 * Note that this functions is called from within isl_map_make_disjoint,
8437 * so we have to be careful not to touch the constraints of the inputs
8440 __isl_give isl_map
*isl_map_union_disjoint(__isl_take isl_map
*map1
,
8441 __isl_take isl_map
*map2
)
8443 isl_map_align_params_bin(&map1
, &map2
);
8444 return map_union_disjoint(map1
, map2
);
8447 /* Return the union of "map1" and "map2", where "map1" and "map2" may
8450 * We currently simply call map_union_disjoint, the internal operation
8451 * of which does not really depend on the inputs being disjoint.
8452 * If the result contains more than one basic map, then we clear
8453 * the disjoint flag since the result may contain basic maps from
8454 * both inputs and these are not guaranteed to be disjoint.
8456 * As a special case, if "map1" and "map2" are obviously equal,
8457 * then we simply return "map1".
8459 __isl_give isl_map
*isl_map_union(__isl_take isl_map
*map1
,
8460 __isl_take isl_map
*map2
)
8464 if (isl_map_align_params_bin(&map1
, &map2
) < 0)
8467 equal
= isl_map_plain_is_equal(map1
, map2
);
8475 map1
= map_union_disjoint(map1
, map2
);
8479 ISL_F_CLR(map1
, ISL_MAP_DISJOINT
);
8487 __isl_give isl_set
*isl_set_union_disjoint(
8488 __isl_take isl_set
*set1
, __isl_take isl_set
*set2
)
8490 return set_from_map(isl_map_union_disjoint(set_to_map(set1
),
8494 __isl_give isl_set
*isl_set_union(__isl_take isl_set
*set1
,
8495 __isl_take isl_set
*set2
)
8497 return set_from_map(isl_map_union(set_to_map(set1
), set_to_map(set2
)));
8500 /* Apply "fn" to pairs of elements from "map" and "set" and collect
8501 * the results in a map living in "space".
8503 * "map" and "set" are assumed to be compatible and non-NULL.
8505 static __isl_give isl_map
*map_intersect_set(__isl_take isl_map
*map
,
8506 __isl_take isl_space
*space
, __isl_take isl_set
*set
,
8507 __isl_give isl_basic_map
*fn(__isl_take isl_basic_map
*bmap
,
8508 __isl_take isl_basic_set
*bset
))
8511 struct isl_map
*result
;
8514 if (isl_set_plain_is_universe(set
)) {
8516 return isl_map_reset_equal_dim_space(map
, space
);
8519 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
) &&
8520 ISL_F_ISSET(set
, ISL_MAP_DISJOINT
))
8521 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
8523 result
= isl_map_alloc_space(space
, map
->n
* set
->n
, flags
);
8524 for (i
= 0; result
&& i
< map
->n
; ++i
)
8525 for (j
= 0; j
< set
->n
; ++j
) {
8526 result
= isl_map_add_basic_map(result
,
8527 fn(isl_basic_map_copy(map
->p
[i
]),
8528 isl_basic_set_copy(set
->p
[j
])));
8538 __isl_give isl_map
*isl_map_intersect_range(__isl_take isl_map
*map
,
8539 __isl_take isl_set
*set
)
8544 isl_map_align_params_set(&map
, &set
);
8545 ok
= isl_map_compatible_range(map
, set
);
8549 isl_die(set
->ctx
, isl_error_invalid
,
8550 "incompatible spaces", goto error
);
8552 space
= isl_map_get_space(map
);
8553 return map_intersect_set(map
, space
, set
,
8554 &isl_basic_map_intersect_range
);
8561 /* Intersect the domain of "map" with "set".
8563 * If the domain dimensions of "map" do not have any identifiers,
8564 * then copy them over from "set".
8566 __isl_give isl_map
*isl_map_intersect_domain(__isl_take isl_map
*map
,
8567 __isl_take isl_set
*set
)
8572 isl_map_align_params_set(&map
, &set
);
8573 ok
= isl_map_compatible_domain(map
, set
);
8577 isl_die(set
->ctx
, isl_error_invalid
,
8578 "incompatible spaces", goto error
);
8580 space
= isl_map_get_space(map
);
8581 space
= isl_space_copy_ids_if_unset(space
, isl_dim_in
,
8582 isl_set_peek_space(set
), isl_dim_set
);
8583 return map_intersect_set(map
, space
, set
,
8584 &isl_basic_map_intersect_domain
);
8594 #include "isl_copy_tuple_id_templ.c"
8596 /* Data structure that specifies how isl_map_intersect_factor
8599 * "preserve_type" is the tuple where the factor differs from
8600 * the input map and of which the identifiers needs
8601 * to be preserved explicitly.
8602 * "other_factor" is used to extract the space of the other factor
8603 * from the space of the product ("map").
8604 * "product" is used to combine the given factor and a universe map
8605 * in the space returned by "other_factor" to produce a map
8606 * that lives in the same space as the input map.
8608 struct isl_intersect_factor_control
{
8609 enum isl_dim_type preserve_type
;
8610 __isl_give isl_space
*(*other_factor
)(__isl_take isl_space
*space
);
8611 __isl_give isl_map
*(*product
)(__isl_take isl_map
*factor
,
8612 __isl_take isl_map
*other
);
8615 /* Given a map "map" in some product space and a map "factor"
8616 * living in some factor space, return the intersection.
8618 * After aligning the parameters,
8619 * the map "factor" is first extended to a map living in the same space
8620 * as "map" and then a regular intersection is computed.
8622 * Note that the extension is computed as a product, which is anonymous
8623 * by default. If "map" has an identifier on the corresponding tuple,
8624 * then this identifier needs to be set on the product
8625 * before the intersection is computed.
8627 static __isl_give isl_map
*isl_map_intersect_factor(
8628 __isl_take isl_map
*map
, __isl_take isl_map
*factor
,
8629 struct isl_intersect_factor_control
*control
)
8633 isl_map
*other
, *product
;
8635 equal
= isl_map_has_equal_params(map
, factor
);
8639 map
= isl_map_align_params(map
, isl_map_get_space(factor
));
8640 factor
= isl_map_align_params(factor
, isl_map_get_space(map
));
8643 space
= isl_map_get_space(map
);
8644 other
= isl_map_universe(control
->other_factor(space
));
8645 product
= control
->product(factor
, other
);
8647 space
= isl_map_peek_space(map
);
8648 product
= isl_map_copy_tuple_id(product
, control
->preserve_type
,
8649 space
, control
->preserve_type
);
8650 return map_intersect(map
, product
);
8653 isl_map_free(factor
);
8657 /* Return the domain product of "map2" and "map1".
8659 static __isl_give isl_map
*isl_map_reverse_domain_product(
8660 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
8662 return isl_map_domain_product(map2
, map1
);
8665 /* Return the range product of "map2" and "map1".
8667 static __isl_give isl_map
*isl_map_reverse_range_product(
8668 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
8670 return isl_map_range_product(map2
, map1
);
8673 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
8674 * in the space A -> C, return the intersection.
8676 __isl_give isl_map
*isl_map_intersect_domain_factor_domain(
8677 __isl_take isl_map
*map
, __isl_take isl_map
*factor
)
8679 struct isl_intersect_factor_control control
= {
8680 .preserve_type
= isl_dim_in
,
8681 .other_factor
= isl_space_domain_factor_range
,
8682 .product
= isl_map_domain_product
,
8685 return isl_map_intersect_factor(map
, factor
, &control
);
8688 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
8689 * in the space B -> C, return the intersection.
8691 __isl_give isl_map
*isl_map_intersect_domain_factor_range(
8692 __isl_take isl_map
*map
, __isl_take isl_map
*factor
)
8694 struct isl_intersect_factor_control control
= {
8695 .preserve_type
= isl_dim_in
,
8696 .other_factor
= isl_space_domain_factor_domain
,
8697 .product
= isl_map_reverse_domain_product
,
8700 return isl_map_intersect_factor(map
, factor
, &control
);
8703 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
8704 * in the space A -> B, return the intersection.
8706 __isl_give isl_map
*isl_map_intersect_range_factor_domain(
8707 __isl_take isl_map
*map
, __isl_take isl_map
*factor
)
8709 struct isl_intersect_factor_control control
= {
8710 .preserve_type
= isl_dim_out
,
8711 .other_factor
= isl_space_range_factor_range
,
8712 .product
= isl_map_range_product
,
8715 return isl_map_intersect_factor(map
, factor
, &control
);
8718 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
8719 * in the space A -> C, return the intersection.
8721 __isl_give isl_map
*isl_map_intersect_range_factor_range(
8722 __isl_take isl_map
*map
, __isl_take isl_map
*factor
)
8724 struct isl_intersect_factor_control control
= {
8725 .preserve_type
= isl_dim_out
,
8726 .other_factor
= isl_space_range_factor_domain
,
8727 .product
= isl_map_reverse_range_product
,
8730 return isl_map_intersect_factor(map
, factor
, &control
);
8733 /* Given a set "set" in a space [A -> B] and a set "domain"
8734 * in the space A, return the intersection.
8736 * The set "domain" is first extended to a set living in the space
8737 * [A -> B] and then a regular intersection is computed.
8739 __isl_give isl_set
*isl_set_intersect_factor_domain(__isl_take isl_set
*set
,
8740 __isl_take isl_set
*domain
)
8742 struct isl_intersect_factor_control control
= {
8743 .preserve_type
= isl_dim_set
,
8744 .other_factor
= isl_space_factor_range
,
8745 .product
= isl_map_range_product
,
8748 return set_from_map(isl_map_intersect_factor(set_to_map(set
),
8749 set_to_map(domain
), &control
));
8752 /* Given a set "set" in a space [A -> B] and a set "range"
8753 * in the space B, return the intersection.
8755 * The set "range" is first extended to a set living in the space
8756 * [A -> B] and then a regular intersection is computed.
8758 __isl_give isl_set
*isl_set_intersect_factor_range(__isl_take isl_set
*set
,
8759 __isl_take isl_set
*range
)
8761 struct isl_intersect_factor_control control
= {
8762 .preserve_type
= isl_dim_set
,
8763 .other_factor
= isl_space_factor_domain
,
8764 .product
= isl_map_reverse_range_product
,
8767 return set_from_map(isl_map_intersect_factor(set_to_map(set
),
8768 set_to_map(range
), &control
));
8774 #include "isl_copy_tuple_id_templ.c"
8776 /* Given a map "map" in a space [A -> B] -> C and a set "domain"
8777 * in the space A, return the intersection.
8779 * The set "domain" is extended to a set living in the space [A -> B] and
8780 * the domain of "map" is intersected with this set.
8782 * If "map" has an identifier on the domain tuple,
8783 * then this identifier needs to be set on this product
8784 * before the intersection is computed.
8786 __isl_give isl_map
*isl_map_intersect_domain_wrapped_domain(
8787 __isl_take isl_map
*map
, __isl_take isl_set
*domain
)
8792 isl_map_align_params_set(&map
, &domain
);
8793 space
= isl_map_get_space(map
);
8794 space
= isl_space_domain_wrapped_range(space
);
8795 factor
= isl_set_universe(space
);
8796 domain
= isl_set_product(domain
, factor
);
8797 space
= isl_map_peek_space(map
);
8798 domain
= isl_set_copy_tuple_id(domain
, isl_dim_set
, space
, isl_dim_in
);
8799 return isl_map_intersect_domain(map
, domain
);
8802 /* Given a map "map" in a space A -> [B -> C] and a set "domain"
8803 * in the space B, return the intersection.
8805 * The set "domain" is extended to a set living in the space [B -> C] and
8806 * the range of "map" is intersected with this set.
8808 * If "map" has an identifier on the range tuple,
8809 * then this identifier needs to be set on this product
8810 * before the intersection is computed.
8812 __isl_give isl_map
*isl_map_intersect_range_wrapped_domain(
8813 __isl_take isl_map
*map
, __isl_take isl_set
*domain
)
8818 isl_map_align_params_set(&map
, &domain
);
8819 space
= isl_map_get_space(map
);
8820 space
= isl_space_range_wrapped_range(space
);
8821 factor
= isl_set_universe(space
);
8822 domain
= isl_set_product(domain
, factor
);
8823 space
= isl_map_peek_space(map
);
8824 domain
= isl_set_copy_tuple_id(domain
, isl_dim_set
, space
, isl_dim_out
);
8825 return isl_map_intersect_range(map
, domain
);
8828 __isl_give isl_map
*isl_map_apply_domain(__isl_take isl_map
*map1
,
8829 __isl_take isl_map
*map2
)
8831 if (isl_map_align_params_bin(&map1
, &map2
) < 0)
8833 map1
= isl_map_reverse(map1
);
8834 map1
= isl_map_apply_range(map1
, map2
);
8835 return isl_map_reverse(map1
);
8842 __isl_give isl_map
*isl_map_apply_range(__isl_take isl_map
*map1
,
8843 __isl_take isl_map
*map2
)
8846 struct isl_map
*result
;
8849 if (isl_map_align_params_bin(&map1
, &map2
) < 0)
8852 space
= isl_space_join(isl_space_copy(map1
->dim
),
8853 isl_space_copy(map2
->dim
));
8855 result
= isl_map_alloc_space(space
, map1
->n
* map2
->n
, 0);
8858 for (i
= 0; i
< map1
->n
; ++i
)
8859 for (j
= 0; j
< map2
->n
; ++j
) {
8860 result
= isl_map_add_basic_map(result
,
8861 isl_basic_map_apply_range(
8862 isl_basic_map_copy(map1
->p
[i
]),
8863 isl_basic_map_copy(map2
->p
[j
])));
8869 if (result
&& result
->n
<= 1)
8870 ISL_F_SET(result
, ISL_MAP_DISJOINT
);
8878 /* Is "bmap" a transformation, i.e.,
8879 * does it relate elements from the same space.
8881 isl_bool
isl_basic_map_is_transformation(__isl_keep isl_basic_map
*bmap
)
8885 space
= isl_basic_map_peek_space(bmap
);
8886 return isl_space_tuple_is_equal(space
, isl_dim_in
, space
, isl_dim_out
);
8889 /* Check that "bmap" is a transformation, i.e.,
8890 * that it relates elements from the same space.
8892 static isl_stat
isl_basic_map_check_transformation(
8893 __isl_keep isl_basic_map
*bmap
)
8897 equal
= isl_basic_map_is_transformation(bmap
);
8899 return isl_stat_error
;
8901 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
8902 "domain and range don't match", return isl_stat_error
);
8907 * returns range - domain
8909 __isl_give isl_basic_set
*isl_basic_map_deltas(__isl_take isl_basic_map
*bmap
)
8911 isl_space
*target_space
;
8912 struct isl_basic_set
*bset
;
8918 if (isl_basic_map_check_transformation(bmap
) < 0)
8919 return isl_basic_map_free(bmap
);
8920 dim
= isl_basic_map_dim(bmap
, isl_dim_in
);
8921 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
8922 if (dim
< 0 || nparam
< 0)
8924 target_space
= isl_space_domain(isl_basic_map_get_space(bmap
));
8925 bmap
= isl_basic_map_from_range(isl_basic_map_wrap(bmap
));
8926 bmap
= isl_basic_map_add_dims(bmap
, isl_dim_in
, dim
);
8927 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
8929 bmap
= isl_basic_map_free(bmap
);
8930 bmap
= isl_basic_map_extend_constraints(bmap
, dim
, 0);
8931 for (i
= 0; i
< dim
; ++i
) {
8932 int j
= isl_basic_map_alloc_equality(bmap
);
8934 bmap
= isl_basic_map_free(bmap
);
8937 isl_seq_clr(bmap
->eq
[j
], 1 + total
);
8938 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
8939 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], 1);
8940 isl_int_set_si(bmap
->eq
[j
][1+nparam
+2*dim
+i
], -1);
8942 bset
= isl_basic_map_domain(bmap
);
8943 bset
= isl_basic_set_reset_space(bset
, target_space
);
8946 isl_basic_map_free(bmap
);
8950 /* Is the tuple of type "type1" of "map" the same as
8951 * the tuple of type "type2" of "space"?
8953 isl_bool
isl_map_space_tuple_is_equal(__isl_keep isl_map
*map
,
8954 enum isl_dim_type type1
, __isl_keep isl_space
*space
,
8955 enum isl_dim_type type2
)
8957 isl_space
*map_space
;
8959 map_space
= isl_map_peek_space(map
);
8960 return isl_space_tuple_is_equal(map_space
, type1
, space
, type2
);
8963 /* Is the tuple of type "type1" of "map1" the same as
8964 * the tuple of type "type2" of "map2"?
8966 isl_bool
isl_map_tuple_is_equal(__isl_keep isl_map
*map1
,
8967 enum isl_dim_type type1
, __isl_keep isl_map
*map2
,
8968 enum isl_dim_type type2
)
8970 isl_space
*space1
, *space2
;
8972 space1
= isl_map_peek_space(map1
);
8973 space2
= isl_map_peek_space(map2
);
8974 return isl_space_tuple_is_equal(space1
, type1
, space2
, type2
);
8977 /* Is the space of "obj" equal to "space", ignoring parameters?
8979 isl_bool
isl_map_has_space_tuples(__isl_keep isl_map
*map
,
8980 __isl_keep isl_space
*space
)
8982 isl_space
*map_space
;
8984 map_space
= isl_map_peek_space(map
);
8985 return isl_space_has_equal_tuples(map_space
, space
);
8988 /* Check that "map" is a transformation, i.e.,
8989 * that it relates elements from the same space.
8991 isl_stat
isl_map_check_transformation(__isl_keep isl_map
*map
)
8995 equal
= isl_map_tuple_is_equal(map
, isl_dim_in
, map
, isl_dim_out
);
8997 return isl_stat_error
;
8999 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
9000 "domain and range don't match", return isl_stat_error
);
9005 * returns range - domain
9007 __isl_give isl_set
*isl_map_deltas(__isl_take isl_map
*map
)
9011 struct isl_set
*result
;
9013 if (isl_map_check_transformation(map
) < 0)
9015 space
= isl_map_get_space(map
);
9016 space
= isl_space_domain(space
);
9017 result
= isl_set_alloc_space(space
, map
->n
, 0);
9020 for (i
= 0; i
< map
->n
; ++i
)
9021 result
= isl_set_add_basic_set(result
,
9022 isl_basic_map_deltas(isl_basic_map_copy(map
->p
[i
])));
9031 * returns [domain -> range] -> range - domain
9033 __isl_give isl_basic_map
*isl_basic_map_deltas_map(
9034 __isl_take isl_basic_map
*bmap
)
9038 isl_basic_map
*domain
;
9042 if (isl_basic_map_check_transformation(bmap
) < 0)
9043 return isl_basic_map_free(bmap
);
9045 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
9046 n
= isl_basic_map_dim(bmap
, isl_dim_in
);
9047 if (nparam
< 0 || n
< 0)
9048 return isl_basic_map_free(bmap
);
9050 space
= isl_basic_map_get_space(bmap
);
9051 space
= isl_space_from_range(isl_space_domain(space
));
9052 domain
= isl_basic_map_universe(space
);
9054 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
9055 bmap
= isl_basic_map_apply_range(bmap
, domain
);
9056 bmap
= isl_basic_map_extend_constraints(bmap
, n
, 0);
9058 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
9060 return isl_basic_map_free(bmap
);
9062 for (i
= 0; i
< n
; ++i
) {
9063 k
= isl_basic_map_alloc_equality(bmap
);
9066 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
9067 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], 1);
9068 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ i
], -1);
9069 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ n
+ i
], 1);
9072 bmap
= isl_basic_map_gauss(bmap
, NULL
);
9073 return isl_basic_map_finalize(bmap
);
9075 isl_basic_map_free(bmap
);
9080 * returns [domain -> range] -> range - domain
9082 __isl_give isl_map
*isl_map_deltas_map(__isl_take isl_map
*map
)
9084 if (isl_map_check_transformation(map
) < 0)
9085 return isl_map_free(map
);
9087 return isl_map_transform(map
, &isl_space_range_map
,
9088 &isl_basic_map_deltas_map
);
9091 /* Return pairs of elements { x -> y } such that y - x is in "deltas".
9093 __isl_give isl_map
*isl_set_translation(__isl_take isl_set
*deltas
)
9098 space
= isl_space_map_from_set(isl_set_get_space(deltas
));
9099 map
= isl_map_deltas_map(isl_map_universe(space
));
9100 map
= isl_map_intersect_range(map
, deltas
);
9102 return isl_set_unwrap(isl_map_domain(map
));
9105 __isl_give isl_basic_map
*isl_basic_map_identity(__isl_take isl_space
*space
)
9107 isl_size n_in
, n_out
;
9109 n_in
= isl_space_dim(space
, isl_dim_in
);
9110 n_out
= isl_space_dim(space
, isl_dim_out
);
9111 if (n_in
< 0 || n_out
< 0)
9114 isl_die(space
->ctx
, isl_error_invalid
,
9115 "number of input and output dimensions needs to be "
9116 "the same", goto error
);
9117 return isl_basic_map_equal(space
, n_in
);
9119 isl_space_free(space
);
9123 __isl_give isl_map
*isl_map_identity(__isl_take isl_space
*space
)
9125 return isl_map_from_basic_map(isl_basic_map_identity(space
));
9128 __isl_give isl_map
*isl_set_identity(__isl_take isl_set
*set
)
9130 isl_space
*space
= isl_set_get_space(set
);
9132 id
= isl_map_identity(isl_space_map_from_set(space
));
9133 return isl_map_intersect_range(id
, set
);
9136 /* Construct a basic set with all set dimensions having only non-negative
9139 __isl_give isl_basic_set
*isl_basic_set_positive_orthant(
9140 __isl_take isl_space
*space
)
9146 struct isl_basic_set
*bset
;
9148 nparam
= isl_space_dim(space
, isl_dim_param
);
9149 dim
= isl_space_dim(space
, isl_dim_set
);
9150 total
= isl_space_dim(space
, isl_dim_all
);
9151 if (nparam
< 0 || dim
< 0 || total
< 0)
9152 space
= isl_space_free(space
);
9153 bset
= isl_basic_set_alloc_space(space
, 0, 0, dim
);
9156 for (i
= 0; i
< dim
; ++i
) {
9157 int k
= isl_basic_set_alloc_inequality(bset
);
9160 isl_seq_clr(bset
->ineq
[k
], 1 + total
);
9161 isl_int_set_si(bset
->ineq
[k
][1 + nparam
+ i
], 1);
9165 isl_basic_set_free(bset
);
9169 /* Construct the half-space x_pos >= 0.
9171 static __isl_give isl_basic_set
*nonneg_halfspace(__isl_take isl_space
*space
,
9176 isl_basic_set
*nonneg
;
9178 total
= isl_space_dim(space
, isl_dim_all
);
9180 space
= isl_space_free(space
);
9181 nonneg
= isl_basic_set_alloc_space(space
, 0, 0, 1);
9182 k
= isl_basic_set_alloc_inequality(nonneg
);
9185 isl_seq_clr(nonneg
->ineq
[k
], 1 + total
);
9186 isl_int_set_si(nonneg
->ineq
[k
][pos
], 1);
9188 return isl_basic_set_finalize(nonneg
);
9190 isl_basic_set_free(nonneg
);
9194 /* Construct the half-space x_pos <= -1.
9196 static __isl_give isl_basic_set
*neg_halfspace(__isl_take isl_space
*space
,
9203 total
= isl_space_dim(space
, isl_dim_all
);
9205 space
= isl_space_free(space
);
9206 neg
= isl_basic_set_alloc_space(space
, 0, 0, 1);
9207 k
= isl_basic_set_alloc_inequality(neg
);
9210 isl_seq_clr(neg
->ineq
[k
], 1 + total
);
9211 isl_int_set_si(neg
->ineq
[k
][0], -1);
9212 isl_int_set_si(neg
->ineq
[k
][pos
], -1);
9214 return isl_basic_set_finalize(neg
);
9216 isl_basic_set_free(neg
);
9220 __isl_give isl_set
*isl_set_split_dims(__isl_take isl_set
*set
,
9221 enum isl_dim_type type
, unsigned first
, unsigned n
)
9225 isl_basic_set
*nonneg
;
9231 if (isl_set_check_range(set
, type
, first
, n
) < 0)
9232 return isl_set_free(set
);
9234 offset
= pos(set
->dim
, type
);
9235 for (i
= 0; i
< n
; ++i
) {
9236 nonneg
= nonneg_halfspace(isl_set_get_space(set
),
9237 offset
+ first
+ i
);
9238 neg
= neg_halfspace(isl_set_get_space(set
), offset
+ first
+ i
);
9240 set
= isl_set_intersect(set
, isl_basic_set_union(nonneg
, neg
));
9246 static isl_stat
foreach_orthant(__isl_take isl_set
*set
, int *signs
, int first
,
9248 isl_stat (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
9254 return isl_stat_error
;
9255 if (isl_set_plain_is_empty(set
)) {
9260 return fn(set
, signs
, user
);
9263 half
= isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set
),
9265 half
= isl_set_intersect(half
, isl_set_copy(set
));
9266 if (foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
) < 0)
9270 half
= isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set
),
9272 half
= isl_set_intersect(half
, set
);
9273 return foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
);
9276 return isl_stat_error
;
9279 /* Call "fn" on the intersections of "set" with each of the orthants
9280 * (except for obviously empty intersections). The orthant is identified
9281 * by the signs array, with each entry having value 1 or -1 according
9282 * to the sign of the corresponding variable.
9284 isl_stat
isl_set_foreach_orthant(__isl_keep isl_set
*set
,
9285 isl_stat (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
9294 return isl_stat_error
;
9295 if (isl_set_plain_is_empty(set
))
9298 nparam
= isl_set_dim(set
, isl_dim_param
);
9299 nvar
= isl_set_dim(set
, isl_dim_set
);
9300 if (nparam
< 0 || nvar
< 0)
9301 return isl_stat_error
;
9303 signs
= isl_alloc_array(set
->ctx
, int, nparam
+ nvar
);
9305 r
= foreach_orthant(isl_set_copy(set
), signs
, 0, nparam
+ nvar
,
9313 isl_bool
isl_set_is_equal(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
9315 return isl_map_is_equal(set_to_map(set1
), set_to_map(set2
));
9318 isl_bool
isl_basic_map_is_subset(__isl_keep isl_basic_map
*bmap1
,
9319 __isl_keep isl_basic_map
*bmap2
)
9322 struct isl_map
*map1
;
9323 struct isl_map
*map2
;
9325 if (!bmap1
|| !bmap2
)
9326 return isl_bool_error
;
9328 map1
= isl_map_from_basic_map(isl_basic_map_copy(bmap1
));
9329 map2
= isl_map_from_basic_map(isl_basic_map_copy(bmap2
));
9331 is_subset
= isl_map_is_subset(map1
, map2
);
9339 isl_bool
isl_basic_set_is_subset(__isl_keep isl_basic_set
*bset1
,
9340 __isl_keep isl_basic_set
*bset2
)
9342 return isl_basic_map_is_subset(bset1
, bset2
);
9345 isl_bool
isl_basic_map_is_equal(__isl_keep isl_basic_map
*bmap1
,
9346 __isl_keep isl_basic_map
*bmap2
)
9350 if (!bmap1
|| !bmap2
)
9351 return isl_bool_error
;
9352 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
9353 if (is_subset
!= isl_bool_true
)
9355 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
9359 isl_bool
isl_basic_set_is_equal(__isl_keep isl_basic_set
*bset1
,
9360 __isl_keep isl_basic_set
*bset2
)
9362 return isl_basic_map_is_equal(
9363 bset_to_bmap(bset1
), bset_to_bmap(bset2
));
9366 isl_bool
isl_map_is_empty(__isl_keep isl_map
*map
)
9372 return isl_bool_error
;
9373 for (i
= 0; i
< map
->n
; ++i
) {
9374 is_empty
= isl_basic_map_is_empty(map
->p
[i
]);
9376 return isl_bool_error
;
9378 return isl_bool_false
;
9380 return isl_bool_true
;
9383 isl_bool
isl_map_plain_is_empty(__isl_keep isl_map
*map
)
9385 return map
? map
->n
== 0 : isl_bool_error
;
9388 isl_bool
isl_set_plain_is_empty(__isl_keep isl_set
*set
)
9390 return set
? set
->n
== 0 : isl_bool_error
;
9393 isl_bool
isl_set_is_empty(__isl_keep isl_set
*set
)
9395 return isl_map_is_empty(set_to_map(set
));
9399 #define TYPE isl_basic_map
9402 #include "isl_type_has_equal_space_bin_templ.c"
9403 #include "isl_type_check_equal_space_templ.c"
9405 /* Check that "bset1" and "bset2" live in the same space,
9406 * reporting an error if they do not.
9408 isl_stat
isl_basic_set_check_equal_space(__isl_keep isl_basic_set
*bset1
,
9409 __isl_keep isl_basic_set
*bset2
)
9411 return isl_basic_map_check_equal_space(bset_to_bmap(bset1
),
9412 bset_to_bmap(bset1
));
9416 #define TYPE isl_map
9418 #include "isl_type_has_equal_space_bin_templ.c"
9419 #include "isl_type_check_equal_space_templ.c"
9420 #include "isl_type_has_space_templ.c"
9422 isl_bool
isl_set_has_equal_space(__isl_keep isl_set
*set1
,
9423 __isl_keep isl_set
*set2
)
9425 return isl_map_has_equal_space(set_to_map(set1
), set_to_map(set2
));
9429 #define TYPE1 isl_map
9431 #define TYPE2 isl_basic_map
9433 #define TYPE_PAIR isl_map_basic_map
9436 #include "isl_type_has_equal_space_templ.c"
9437 #include "isl_type_check_equal_space_templ.c"
9439 /* Check that "set" and "bset" live in the same space,
9440 * reporting an error if they do not.
9442 isl_stat
isl_set_basic_set_check_equal_space(__isl_keep isl_set
*set
,
9443 __isl_keep isl_basic_set
*bset
)
9445 return isl_map_basic_map_check_equal_space(set_to_map(set
),
9446 bset_to_bmap(bset
));
9449 static isl_bool
map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
9454 return isl_bool_error
;
9455 is_subset
= isl_map_is_subset(map1
, map2
);
9456 if (is_subset
!= isl_bool_true
)
9458 is_subset
= isl_map_is_subset(map2
, map1
);
9462 /* Is "map1" equal to "map2"?
9464 * First check if they are obviously equal.
9465 * If not, then perform a more detailed analysis.
9467 isl_bool
isl_map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
9471 equal
= isl_map_plain_is_equal(map1
, map2
);
9472 if (equal
< 0 || equal
)
9474 return isl_map_align_params_map_map_and_test(map1
, map2
, &map_is_equal
);
9477 isl_bool
isl_basic_map_is_strict_subset(__isl_keep isl_basic_map
*bmap1
,
9478 __isl_keep isl_basic_map
*bmap2
)
9482 if (!bmap1
|| !bmap2
)
9483 return isl_bool_error
;
9484 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
9485 if (is_subset
!= isl_bool_true
)
9487 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
9488 return isl_bool_not(is_subset
);
9491 isl_bool
isl_map_is_strict_subset(__isl_keep isl_map
*map1
,
9492 __isl_keep isl_map
*map2
)
9497 return isl_bool_error
;
9498 is_subset
= isl_map_is_subset(map1
, map2
);
9499 if (is_subset
!= isl_bool_true
)
9501 is_subset
= isl_map_is_subset(map2
, map1
);
9502 return isl_bool_not(is_subset
);
9505 isl_bool
isl_set_is_strict_subset(__isl_keep isl_set
*set1
,
9506 __isl_keep isl_set
*set2
)
9508 return isl_map_is_strict_subset(set_to_map(set1
), set_to_map(set2
));
9511 /* Is "bmap" obviously equal to the universe with the same space?
9513 * That is, does it not have any constraints?
9515 isl_bool
isl_basic_map_plain_is_universe(__isl_keep isl_basic_map
*bmap
)
9518 return isl_bool_error
;
9519 return bmap
->n_eq
== 0 && bmap
->n_ineq
== 0;
9522 /* Is "bset" obviously equal to the universe with the same space?
9524 isl_bool
isl_basic_set_plain_is_universe(__isl_keep isl_basic_set
*bset
)
9526 return isl_basic_map_plain_is_universe(bset
);
9529 /* If "c" does not involve any existentially quantified variables,
9530 * then set *univ to false and abort
9532 static isl_stat
involves_divs(__isl_take isl_constraint
*c
, void *user
)
9534 isl_bool
*univ
= user
;
9537 n
= isl_constraint_dim(c
, isl_dim_div
);
9539 c
= isl_constraint_free(c
);
9540 *univ
= isl_constraint_involves_dims(c
, isl_dim_div
, 0, n
);
9541 isl_constraint_free(c
);
9542 if (*univ
< 0 || !*univ
)
9543 return isl_stat_error
;
9547 /* Is "bmap" equal to the universe with the same space?
9549 * First check if it is obviously equal to the universe.
9550 * If not and if there are any constraints not involving
9551 * existentially quantified variables, then it is certainly
9552 * not equal to the universe.
9553 * Otherwise, check if the universe is a subset of "bmap".
9555 isl_bool
isl_basic_map_is_universe(__isl_keep isl_basic_map
*bmap
)
9559 isl_basic_map
*test
;
9561 univ
= isl_basic_map_plain_is_universe(bmap
);
9562 if (univ
< 0 || univ
)
9564 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
9566 return isl_bool_error
;
9568 return isl_bool_false
;
9569 univ
= isl_bool_true
;
9570 if (isl_basic_map_foreach_constraint(bmap
, &involves_divs
, &univ
) < 0 &&
9572 return isl_bool_error
;
9573 if (univ
< 0 || !univ
)
9575 test
= isl_basic_map_universe(isl_basic_map_get_space(bmap
));
9576 univ
= isl_basic_map_is_subset(test
, bmap
);
9577 isl_basic_map_free(test
);
9581 /* Is "bset" equal to the universe with the same space?
9583 isl_bool
isl_basic_set_is_universe(__isl_keep isl_basic_set
*bset
)
9585 return isl_basic_map_is_universe(bset
);
9588 isl_bool
isl_map_plain_is_universe(__isl_keep isl_map
*map
)
9593 return isl_bool_error
;
9595 for (i
= 0; i
< map
->n
; ++i
) {
9596 isl_bool r
= isl_basic_map_plain_is_universe(map
->p
[i
]);
9601 return isl_bool_false
;
9604 isl_bool
isl_set_plain_is_universe(__isl_keep isl_set
*set
)
9606 return isl_map_plain_is_universe(set_to_map(set
));
9609 isl_bool
isl_basic_map_is_empty(__isl_keep isl_basic_map
*bmap
)
9611 struct isl_basic_set
*bset
= NULL
;
9612 struct isl_vec
*sample
= NULL
;
9613 isl_bool empty
, non_empty
;
9616 return isl_bool_error
;
9618 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
9619 return isl_bool_true
;
9621 if (isl_basic_map_plain_is_universe(bmap
))
9622 return isl_bool_false
;
9624 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
9625 struct isl_basic_map
*copy
= isl_basic_map_copy(bmap
);
9626 copy
= isl_basic_map_remove_redundancies(copy
);
9627 empty
= isl_basic_map_plain_is_empty(copy
);
9628 isl_basic_map_free(copy
);
9632 non_empty
= isl_basic_map_plain_is_non_empty(bmap
);
9634 return isl_bool_error
;
9636 return isl_bool_false
;
9637 isl_vec_free(bmap
->sample
);
9638 bmap
->sample
= NULL
;
9639 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
9641 return isl_bool_error
;
9642 sample
= isl_basic_set_sample_vec(bset
);
9644 return isl_bool_error
;
9645 empty
= sample
->size
== 0;
9646 isl_vec_free(bmap
->sample
);
9647 bmap
->sample
= sample
;
9649 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
9654 isl_bool
isl_basic_map_plain_is_empty(__isl_keep isl_basic_map
*bmap
)
9657 return isl_bool_error
;
9658 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
);
9661 isl_bool
isl_basic_set_plain_is_empty(__isl_keep isl_basic_set
*bset
)
9664 return isl_bool_error
;
9665 return ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
);
9668 /* Is "bmap" known to be non-empty?
9670 * That is, is the cached sample still valid?
9672 isl_bool
isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map
*bmap
)
9677 return isl_bool_error
;
9679 return isl_bool_false
;
9680 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
9682 return isl_bool_error
;
9683 if (bmap
->sample
->size
!= 1 + total
)
9684 return isl_bool_false
;
9685 return isl_basic_map_contains(bmap
, bmap
->sample
);
9688 isl_bool
isl_basic_set_is_empty(__isl_keep isl_basic_set
*bset
)
9690 return isl_basic_map_is_empty(bset_to_bmap(bset
));
9693 __isl_give isl_map
*isl_basic_map_union(__isl_take isl_basic_map
*bmap1
,
9694 __isl_take isl_basic_map
*bmap2
)
9696 struct isl_map
*map
;
9698 if (isl_basic_map_check_equal_space(bmap1
, bmap2
) < 0)
9701 map
= isl_map_alloc_space(isl_space_copy(bmap1
->dim
), 2, 0);
9704 map
= isl_map_add_basic_map(map
, bmap1
);
9705 map
= isl_map_add_basic_map(map
, bmap2
);
9708 isl_basic_map_free(bmap1
);
9709 isl_basic_map_free(bmap2
);
9713 __isl_give isl_set
*isl_basic_set_union(__isl_take isl_basic_set
*bset1
,
9714 __isl_take isl_basic_set
*bset2
)
9716 return set_from_map(isl_basic_map_union(bset_to_bmap(bset1
),
9717 bset_to_bmap(bset2
)));
9720 /* Order divs such that any div only depends on previous divs */
9721 __isl_give isl_basic_map
*isl_basic_map_order_divs(
9722 __isl_take isl_basic_map
*bmap
)
9727 off
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
9729 return isl_basic_map_free(bmap
);
9731 for (i
= 0; i
< bmap
->n_div
; ++i
) {
9733 if (isl_int_is_zero(bmap
->div
[i
][0]))
9735 pos
= isl_seq_first_non_zero(bmap
->div
[i
]+1+1+off
+i
,
9740 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_internal
,
9741 "integer division depends on itself",
9742 return isl_basic_map_free(bmap
));
9743 bmap
= isl_basic_map_swap_div(bmap
, i
, i
+ pos
);
9751 __isl_give isl_map
*isl_map_order_divs(__isl_take isl_map
*map
)
9758 for (i
= 0; i
< map
->n
; ++i
) {
9759 map
->p
[i
] = isl_basic_map_order_divs(map
->p
[i
]);
9770 /* Sort the local variables of "bset".
9772 __isl_give isl_basic_set
*isl_basic_set_sort_divs(
9773 __isl_take isl_basic_set
*bset
)
9775 return bset_from_bmap(isl_basic_map_sort_divs(bset_to_bmap(bset
)));
9778 /* Apply the expansion computed by isl_merge_divs.
9779 * The expansion itself is given by "exp" while the resulting
9780 * list of divs is given by "div".
9782 * Move the integer divisions of "bmap" into the right position
9783 * according to "exp" and then introduce the additional integer
9784 * divisions, adding div constraints.
9785 * The moving should be done first to avoid moving coefficients
9786 * in the definitions of the extra integer divisions.
9788 __isl_give isl_basic_map
*isl_basic_map_expand_divs(
9789 __isl_take isl_basic_map
*bmap
, __isl_take isl_mat
*div
, int *exp
)
9794 bmap
= isl_basic_map_cow(bmap
);
9798 if (div
->n_row
< bmap
->n_div
)
9799 isl_die(isl_mat_get_ctx(div
), isl_error_invalid
,
9800 "not an expansion", goto error
);
9802 n_div
= bmap
->n_div
;
9803 bmap
= isl_basic_map_extend(bmap
, div
->n_row
- n_div
, 0,
9804 2 * (div
->n_row
- n_div
));
9806 for (i
= n_div
; i
< div
->n_row
; ++i
)
9807 if (isl_basic_map_alloc_div(bmap
) < 0)
9810 for (j
= n_div
- 1; j
>= 0; --j
) {
9813 bmap
= isl_basic_map_swap_div(bmap
, j
, exp
[j
]);
9818 for (i
= 0; i
< div
->n_row
; ++i
) {
9819 if (j
< n_div
&& exp
[j
] == i
) {
9822 isl_seq_cpy(bmap
->div
[i
], div
->row
[i
], div
->n_col
);
9823 if (isl_basic_map_div_is_marked_unknown(bmap
, i
))
9825 bmap
= isl_basic_map_add_div_constraints(bmap
, i
);
9834 isl_basic_map_free(bmap
);
9839 /* Apply the expansion computed by isl_merge_divs.
9840 * The expansion itself is given by "exp" while the resulting
9841 * list of divs is given by "div".
9843 __isl_give isl_basic_set
*isl_basic_set_expand_divs(
9844 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*div
, int *exp
)
9846 return isl_basic_map_expand_divs(bset
, div
, exp
);
9849 /* Look for a div in dst that corresponds to the div "div" in src.
9850 * The divs before "div" in src and dst are assumed to be the same.
9852 * Return the position of the corresponding div in dst
9853 * if there is one. Otherwise, return a position beyond the integer divisions.
9854 * Return -1 on error.
9856 static int find_div(__isl_keep isl_basic_map
*dst
,
9857 __isl_keep isl_basic_map
*src
, unsigned div
)
9863 v_div
= isl_basic_map_var_offset(src
, isl_dim_div
);
9864 n_div
= isl_basic_map_dim(dst
, isl_dim_div
);
9865 if (n_div
< 0 || v_div
< 0)
9867 isl_assert(dst
->ctx
, div
<= n_div
, return -1);
9868 for (i
= div
; i
< n_div
; ++i
)
9869 if (isl_seq_eq(dst
->div
[i
], src
->div
[div
], 1+1+v_div
+div
) &&
9870 isl_seq_first_non_zero(dst
->div
[i
] + 1 + 1 + v_div
+ div
,
9876 /* Align the divs of "dst" to those of "src", adding divs from "src"
9877 * if needed. That is, make sure that the first src->n_div divs
9878 * of the result are equal to those of src.
9879 * The integer division of "src" are assumed to be ordered.
9881 * The integer divisions are swapped into the right position
9882 * (possibly after adding them first). This may result
9883 * in the remaining integer divisions appearing in the wrong order,
9884 * i.e., with some integer division appearing before
9885 * some other integer division on which it depends.
9886 * The integer divisions therefore need to be ordered.
9887 * This will not affect the integer divisions aligned to those of "src",
9888 * since "src" is assumed to have ordered integer divisions.
9890 * The result is not finalized as by design it will have redundant
9891 * divs if any divs from "src" were copied.
9893 __isl_give isl_basic_map
*isl_basic_map_align_divs(
9894 __isl_take isl_basic_map
*dst
, __isl_keep isl_basic_map
*src
)
9903 return isl_basic_map_free(dst
);
9905 if (src
->n_div
== 0)
9908 known
= isl_basic_map_divs_known(src
);
9910 return isl_basic_map_free(dst
);
9912 isl_die(isl_basic_map_get_ctx(src
), isl_error_invalid
,
9913 "some src divs are unknown",
9914 return isl_basic_map_free(dst
));
9916 v_div
= isl_basic_map_var_offset(src
, isl_dim_div
);
9918 return isl_basic_map_free(dst
);
9921 dst_n_div
= isl_basic_map_dim(dst
, isl_dim_div
);
9923 dst
= isl_basic_map_free(dst
);
9924 for (i
= 0; i
< src
->n_div
; ++i
) {
9925 int j
= find_div(dst
, src
, i
);
9927 dst
= isl_basic_map_free(dst
);
9928 if (j
== dst_n_div
) {
9930 int extra
= src
->n_div
- i
;
9931 dst
= isl_basic_map_cow(dst
);
9933 return isl_basic_map_free(dst
);
9934 dst
= isl_basic_map_extend(dst
,
9935 extra
, 0, 2 * extra
);
9938 j
= isl_basic_map_alloc_div(dst
);
9940 return isl_basic_map_free(dst
);
9941 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+v_div
+i
);
9942 isl_seq_clr(dst
->div
[j
]+1+1+v_div
+i
, dst
->n_div
- i
);
9944 dst
= isl_basic_map_add_div_constraints(dst
, j
);
9946 return isl_basic_map_free(dst
);
9949 dst
= isl_basic_map_swap_div(dst
, i
, j
);
9951 return isl_basic_map_free(dst
);
9953 return isl_basic_map_order_divs(dst
);
9956 __isl_give isl_map
*isl_map_align_divs_internal(__isl_take isl_map
*map
)
9964 map
= isl_map_compute_divs(map
);
9965 map
= isl_map_order_divs(map
);
9966 map
= isl_map_cow(map
);
9970 for (i
= 1; i
< map
->n
; ++i
)
9971 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], map
->p
[i
]);
9972 for (i
= 1; i
< map
->n
; ++i
) {
9973 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], map
->p
[0]);
9975 return isl_map_free(map
);
9978 map
= isl_map_unmark_normalized(map
);
9982 __isl_give isl_map
*isl_map_align_divs(__isl_take isl_map
*map
)
9984 return isl_map_align_divs_internal(map
);
9987 __isl_give isl_set
*isl_set_align_divs(__isl_take isl_set
*set
)
9989 return set_from_map(isl_map_align_divs_internal(set_to_map(set
)));
9992 /* Align the divs of the basic maps in "map" to those
9993 * of the basic maps in "list", as well as to the other basic maps in "map".
9994 * The elements in "list" are assumed to have known divs.
9996 __isl_give isl_map
*isl_map_align_divs_to_basic_map_list(
9997 __isl_take isl_map
*map
, __isl_keep isl_basic_map_list
*list
)
10002 n
= isl_basic_map_list_n_basic_map(list
);
10003 map
= isl_map_compute_divs(map
);
10004 map
= isl_map_cow(map
);
10006 return isl_map_free(map
);
10010 for (i
= 0; i
< n
; ++i
) {
10011 isl_basic_map
*bmap
;
10013 bmap
= isl_basic_map_list_get_basic_map(list
, i
);
10014 bmap
= isl_basic_map_order_divs(bmap
);
10015 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], bmap
);
10016 isl_basic_map_free(bmap
);
10019 return isl_map_free(map
);
10021 return isl_map_align_divs_internal(map
);
10024 /* Align the divs of each element of "list" to those of "bmap".
10025 * Both "bmap" and the elements of "list" are assumed to have known divs.
10027 __isl_give isl_basic_map_list
*isl_basic_map_list_align_divs_to_basic_map(
10028 __isl_take isl_basic_map_list
*list
, __isl_keep isl_basic_map
*bmap
)
10033 n
= isl_basic_map_list_n_basic_map(list
);
10034 if (n
< 0 || !bmap
)
10035 return isl_basic_map_list_free(list
);
10037 for (i
= 0; i
< n
; ++i
) {
10038 isl_basic_map
*bmap_i
;
10040 bmap_i
= isl_basic_map_list_get_basic_map(list
, i
);
10041 bmap_i
= isl_basic_map_align_divs(bmap_i
, bmap
);
10042 list
= isl_basic_map_list_set_basic_map(list
, i
, bmap_i
);
10048 __isl_give isl_set
*isl_set_apply( __isl_take isl_set
*set
,
10049 __isl_take isl_map
*map
)
10053 isl_map_align_params_set(&map
, &set
);
10054 ok
= isl_map_compatible_domain(map
, set
);
10058 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
10059 "incompatible spaces", goto error
);
10060 map
= isl_map_intersect_domain(map
, set
);
10061 set
= isl_map_range(map
);
10069 /* There is no need to cow as removing empty parts doesn't change
10070 * the meaning of the set.
10072 __isl_give isl_map
*isl_map_remove_empty_parts(__isl_take isl_map
*map
)
10079 for (i
= map
->n
- 1; i
>= 0; --i
)
10080 map
= remove_if_empty(map
, i
);
10085 __isl_give isl_set
*isl_set_remove_empty_parts(__isl_take isl_set
*set
)
10087 return set_from_map(isl_map_remove_empty_parts(set_to_map(set
)));
10090 /* Create a binary relation that maps the shared initial "pos" dimensions
10091 * of "bset1" and "bset2" to the remaining dimensions of "bset1" and "bset2".
10093 static __isl_give isl_basic_map
*join_initial(__isl_keep isl_basic_set
*bset1
,
10094 __isl_keep isl_basic_set
*bset2
, int pos
)
10096 isl_basic_map
*bmap1
;
10097 isl_basic_map
*bmap2
;
10099 bmap1
= isl_basic_map_from_range(isl_basic_set_copy(bset1
));
10100 bmap2
= isl_basic_map_from_range(isl_basic_set_copy(bset2
));
10101 bmap1
= isl_basic_map_move_dims(bmap1
, isl_dim_in
, 0,
10102 isl_dim_out
, 0, pos
);
10103 bmap2
= isl_basic_map_move_dims(bmap2
, isl_dim_in
, 0,
10104 isl_dim_out
, 0, pos
);
10105 return isl_basic_map_range_product(bmap1
, bmap2
);
10108 /* Given two basic sets bset1 and bset2, compute the maximal difference
10109 * between the values of dimension pos in bset1 and those in bset2
10110 * for any common value of the parameters and dimensions preceding pos.
10112 static enum isl_lp_result
basic_set_maximal_difference_at(
10113 __isl_keep isl_basic_set
*bset1
, __isl_keep isl_basic_set
*bset2
,
10114 int pos
, isl_int
*opt
)
10116 isl_basic_map
*bmap1
;
10117 struct isl_ctx
*ctx
;
10118 struct isl_vec
*obj
;
10122 enum isl_lp_result res
;
10124 nparam
= isl_basic_set_dim(bset1
, isl_dim_param
);
10125 dim1
= isl_basic_set_dim(bset1
, isl_dim_set
);
10126 if (nparam
< 0 || dim1
< 0 || !bset2
)
10127 return isl_lp_error
;
10129 bmap1
= join_initial(bset1
, bset2
, pos
);
10130 total
= isl_basic_map_dim(bmap1
, isl_dim_all
);
10132 return isl_lp_error
;
10135 obj
= isl_vec_alloc(ctx
, 1 + total
);
10138 isl_seq_clr(obj
->block
.data
, 1 + total
);
10139 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
], 1);
10140 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
+(dim1
-pos
)], -1);
10141 res
= isl_basic_map_solve_lp(bmap1
, 1, obj
->block
.data
, ctx
->one
,
10143 isl_basic_map_free(bmap1
);
10147 isl_basic_map_free(bmap1
);
10148 return isl_lp_error
;
10151 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
10152 * for any common value of the parameters and dimensions preceding pos
10153 * in both basic sets, the values of dimension pos in bset1 are
10154 * smaller or larger than those in bset2.
10157 * 1 if bset1 follows bset2
10158 * -1 if bset1 precedes bset2
10159 * 0 if bset1 and bset2 are incomparable
10160 * -2 if some error occurred.
10162 int isl_basic_set_compare_at(__isl_keep isl_basic_set
*bset1
,
10163 __isl_keep isl_basic_set
*bset2
, int pos
)
10166 enum isl_lp_result res
;
10171 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
10173 if (res
== isl_lp_empty
)
10175 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
10176 res
== isl_lp_unbounded
)
10178 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
10183 isl_int_clear(opt
);
10187 /* Given two basic sets bset1 and bset2, check whether
10188 * for any common value of the parameters and dimensions preceding pos
10189 * there is a value of dimension pos in bset1 that is larger
10190 * than a value of the same dimension in bset2.
10193 * 1 if there exists such a pair
10194 * 0 if there is no such pair, but there is a pair of equal values
10196 * -2 if some error occurred.
10198 int isl_basic_set_follows_at(__isl_keep isl_basic_set
*bset1
,
10199 __isl_keep isl_basic_set
*bset2
, int pos
)
10202 isl_basic_map
*bmap
;
10205 dim1
= isl_basic_set_dim(bset1
, isl_dim_set
);
10208 bmap
= join_initial(bset1
, bset2
, pos
);
10209 bmap
= isl_basic_map_order_ge(bmap
, isl_dim_out
, 0,
10210 isl_dim_out
, dim1
- pos
);
10211 empty
= isl_basic_map_is_empty(bmap
);
10215 isl_basic_map_free(bmap
);
10218 bmap
= isl_basic_map_order_gt(bmap
, isl_dim_out
, 0,
10219 isl_dim_out
, dim1
- pos
);
10220 empty
= isl_basic_map_is_empty(bmap
);
10223 isl_basic_map_free(bmap
);
10228 isl_basic_map_free(bmap
);
10232 /* Given two sets set1 and set2, check whether
10233 * for any common value of the parameters and dimensions preceding pos
10234 * there is a value of dimension pos in set1 that is larger
10235 * than a value of the same dimension in set2.
10238 * 1 if there exists such a pair
10239 * 0 if there is no such pair, but there is a pair of equal values
10241 * -2 if some error occurred.
10243 int isl_set_follows_at(__isl_keep isl_set
*set1
,
10244 __isl_keep isl_set
*set2
, int pos
)
10249 if (!set1
|| !set2
)
10252 for (i
= 0; i
< set1
->n
; ++i
)
10253 for (j
= 0; j
< set2
->n
; ++j
) {
10255 f
= isl_basic_set_follows_at(set1
->p
[i
], set2
->p
[j
], pos
);
10256 if (f
== 1 || f
== -2)
10265 static isl_bool
isl_basic_map_plain_has_fixed_var(
10266 __isl_keep isl_basic_map
*bmap
, unsigned pos
, isl_int
*val
)
10272 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
10274 return isl_bool_error
;
10275 for (i
= 0, d
= total
-1; i
< bmap
->n_eq
&& d
+1 > pos
; ++i
) {
10276 for (; d
+1 > pos
; --d
)
10277 if (!isl_int_is_zero(bmap
->eq
[i
][1+d
]))
10281 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1, d
) != -1)
10282 return isl_bool_false
;
10283 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1+d
+1, total
-d
-1) != -1)
10284 return isl_bool_false
;
10285 if (!isl_int_is_one(bmap
->eq
[i
][1+d
]))
10286 return isl_bool_false
;
10288 isl_int_neg(*val
, bmap
->eq
[i
][0]);
10289 return isl_bool_true
;
10291 return isl_bool_false
;
10294 static isl_bool
isl_map_plain_has_fixed_var(__isl_keep isl_map
*map
,
10295 unsigned pos
, isl_int
*val
)
10303 return isl_bool_error
;
10305 return isl_bool_false
;
10307 return isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, val
);
10310 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, &v
);
10311 for (i
= 1; fixed
== isl_bool_true
&& i
< map
->n
; ++i
) {
10312 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[i
], pos
, &tmp
);
10313 if (fixed
== isl_bool_true
&& isl_int_ne(tmp
, v
))
10314 fixed
= isl_bool_false
;
10317 isl_int_set(*val
, v
);
10318 isl_int_clear(tmp
);
10323 static isl_bool
isl_basic_set_plain_has_fixed_var(
10324 __isl_keep isl_basic_set
*bset
, unsigned pos
, isl_int
*val
)
10326 return isl_basic_map_plain_has_fixed_var(bset_to_bmap(bset
),
10330 isl_bool
isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map
*bmap
,
10331 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
10333 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
10334 return isl_bool_error
;
10335 return isl_basic_map_plain_has_fixed_var(bmap
,
10336 isl_basic_map_offset(bmap
, type
) - 1 + pos
, val
);
10339 /* If "bmap" obviously lies on a hyperplane where the given dimension
10340 * has a fixed value, then return that value.
10341 * Otherwise return NaN.
10343 __isl_give isl_val
*isl_basic_map_plain_get_val_if_fixed(
10344 __isl_keep isl_basic_map
*bmap
,
10345 enum isl_dim_type type
, unsigned pos
)
10353 ctx
= isl_basic_map_get_ctx(bmap
);
10354 v
= isl_val_alloc(ctx
);
10357 fixed
= isl_basic_map_plain_is_fixed(bmap
, type
, pos
, &v
->n
);
10359 return isl_val_free(v
);
10361 isl_int_set_si(v
->d
, 1);
10365 return isl_val_nan(ctx
);
10368 isl_bool
isl_map_plain_is_fixed(__isl_keep isl_map
*map
,
10369 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
10371 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
10372 return isl_bool_error
;
10373 return isl_map_plain_has_fixed_var(map
,
10374 map_offset(map
, type
) - 1 + pos
, val
);
10377 /* If "map" obviously lies on a hyperplane where the given dimension
10378 * has a fixed value, then return that value.
10379 * Otherwise return NaN.
10381 __isl_give isl_val
*isl_map_plain_get_val_if_fixed(__isl_keep isl_map
*map
,
10382 enum isl_dim_type type
, unsigned pos
)
10390 ctx
= isl_map_get_ctx(map
);
10391 v
= isl_val_alloc(ctx
);
10394 fixed
= isl_map_plain_is_fixed(map
, type
, pos
, &v
->n
);
10396 return isl_val_free(v
);
10398 isl_int_set_si(v
->d
, 1);
10402 return isl_val_nan(ctx
);
10405 /* If "set" obviously lies on a hyperplane where the given dimension
10406 * has a fixed value, then return that value.
10407 * Otherwise return NaN.
10409 __isl_give isl_val
*isl_set_plain_get_val_if_fixed(__isl_keep isl_set
*set
,
10410 enum isl_dim_type type
, unsigned pos
)
10412 return isl_map_plain_get_val_if_fixed(set
, type
, pos
);
10415 /* Return a sequence of values in the same space as "set"
10416 * that are equal to the corresponding set dimensions of "set"
10417 * for those set dimensions that obviously lie on a hyperplane
10418 * where the dimension has a fixed value.
10419 * The other elements are set to NaN.
10421 __isl_give isl_multi_val
*isl_set_get_plain_multi_val_if_fixed(
10422 __isl_keep isl_set
*set
)
10429 space
= isl_space_drop_all_params(isl_set_get_space(set
));
10430 mv
= isl_multi_val_alloc(space
);
10431 n
= isl_multi_val_size(mv
);
10433 return isl_multi_val_free(mv
);
10435 for (i
= 0; i
< n
; ++i
) {
10438 v
= isl_set_plain_get_val_if_fixed(set
, isl_dim_set
, i
);
10439 mv
= isl_multi_val_set_val(mv
, i
, v
);
10445 /* Check if dimension dim has fixed value and if so and if val is not NULL,
10446 * then return this fixed value in *val.
10448 isl_bool
isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set
*bset
,
10449 unsigned dim
, isl_int
*val
)
10453 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
10455 return isl_bool_error
;
10456 return isl_basic_set_plain_has_fixed_var(bset
, nparam
+ dim
, val
);
10459 /* Return -1 if the constraint "c1" should be sorted before "c2"
10460 * and 1 if it should be sorted after "c2".
10461 * Return 0 if the two constraints are the same (up to the constant term).
10463 * In particular, if a constraint involves later variables than another
10464 * then it is sorted after this other constraint.
10465 * uset_gist depends on constraints without existentially quantified
10466 * variables sorting first.
10468 * For constraints that have the same latest variable, those
10469 * with the same coefficient for this latest variable (first in absolute value
10470 * and then in actual value) are grouped together.
10471 * This is useful for detecting pairs of constraints that can
10472 * be chained in their printed representation.
10474 * Finally, within a group, constraints are sorted according to
10475 * their coefficients (excluding the constant term).
10477 static int sort_constraint_cmp(const void *p1
, const void *p2
, void *arg
)
10479 isl_int
**c1
= (isl_int
**) p1
;
10480 isl_int
**c2
= (isl_int
**) p2
;
10482 unsigned size
= *(unsigned *) arg
;
10485 l1
= isl_seq_last_non_zero(*c1
+ 1, size
);
10486 l2
= isl_seq_last_non_zero(*c2
+ 1, size
);
10491 cmp
= isl_int_abs_cmp((*c1
)[1 + l1
], (*c2
)[1 + l1
]);
10494 cmp
= isl_int_cmp((*c1
)[1 + l1
], (*c2
)[1 + l1
]);
10498 return isl_seq_cmp(*c1
+ 1, *c2
+ 1, size
);
10501 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
10502 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
10503 * and 0 if the two constraints are the same (up to the constant term).
10505 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map
*bmap
,
10506 isl_int
*c1
, isl_int
*c2
)
10511 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
10515 return sort_constraint_cmp(&c1
, &c2
, &size
);
10518 __isl_give isl_basic_map
*isl_basic_map_sort_constraints(
10519 __isl_take isl_basic_map
*bmap
)
10526 if (bmap
->n_ineq
== 0)
10528 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_SORTED
))
10530 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
10532 return isl_basic_map_free(bmap
);
10534 if (isl_sort(bmap
->ineq
, bmap
->n_ineq
, sizeof(isl_int
*),
10535 &sort_constraint_cmp
, &size
) < 0)
10536 return isl_basic_map_free(bmap
);
10537 ISL_F_SET(bmap
, ISL_BASIC_MAP_SORTED
);
10541 __isl_give isl_basic_set
*isl_basic_set_sort_constraints(
10542 __isl_take isl_basic_set
*bset
)
10544 isl_basic_map
*bmap
= bset_to_bmap(bset
);
10545 return bset_from_bmap(isl_basic_map_sort_constraints(bmap
));
10548 __isl_give isl_basic_map
*isl_basic_map_normalize(
10549 __isl_take isl_basic_map
*bmap
)
10551 bmap
= isl_basic_map_remove_redundancies(bmap
);
10552 bmap
= isl_basic_map_sort_constraints(bmap
);
10555 int isl_basic_map_plain_cmp(__isl_keep isl_basic_map
*bmap1
,
10556 __isl_keep isl_basic_map
*bmap2
)
10560 isl_space
*space1
, *space2
;
10562 if (!bmap1
|| !bmap2
)
10565 if (bmap1
== bmap2
)
10567 space1
= isl_basic_map_peek_space(bmap1
);
10568 space2
= isl_basic_map_peek_space(bmap2
);
10569 cmp
= isl_space_cmp(space1
, space2
);
10572 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) !=
10573 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_RATIONAL
))
10574 return ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) ? -1 : 1;
10575 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
) &&
10576 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
10578 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
))
10580 if (ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
10582 if (bmap1
->n_eq
!= bmap2
->n_eq
)
10583 return bmap1
->n_eq
- bmap2
->n_eq
;
10584 if (bmap1
->n_ineq
!= bmap2
->n_ineq
)
10585 return bmap1
->n_ineq
- bmap2
->n_ineq
;
10586 if (bmap1
->n_div
!= bmap2
->n_div
)
10587 return bmap1
->n_div
- bmap2
->n_div
;
10588 total
= isl_basic_map_dim(bmap1
, isl_dim_all
);
10591 for (i
= 0; i
< bmap1
->n_eq
; ++i
) {
10592 cmp
= isl_seq_cmp(bmap1
->eq
[i
], bmap2
->eq
[i
], 1+total
);
10596 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
10597 cmp
= isl_seq_cmp(bmap1
->ineq
[i
], bmap2
->ineq
[i
], 1+total
);
10601 for (i
= 0; i
< bmap1
->n_div
; ++i
) {
10602 isl_bool unknown1
, unknown2
;
10604 unknown1
= isl_basic_map_div_is_marked_unknown(bmap1
, i
);
10605 unknown2
= isl_basic_map_div_is_marked_unknown(bmap2
, i
);
10606 if (unknown1
< 0 || unknown2
< 0)
10608 if (unknown1
&& unknown2
)
10614 cmp
= isl_seq_cmp(bmap1
->div
[i
], bmap2
->div
[i
], 1+1+total
);
10621 int isl_basic_set_plain_cmp(__isl_keep isl_basic_set
*bset1
,
10622 __isl_keep isl_basic_set
*bset2
)
10624 return isl_basic_map_plain_cmp(bset1
, bset2
);
10627 int isl_set_plain_cmp(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
10633 if (set1
->n
!= set2
->n
)
10634 return set1
->n
- set2
->n
;
10636 for (i
= 0; i
< set1
->n
; ++i
) {
10637 cmp
= isl_basic_set_plain_cmp(set1
->p
[i
], set2
->p
[i
]);
10645 isl_bool
isl_basic_map_plain_is_equal(__isl_keep isl_basic_map
*bmap1
,
10646 __isl_keep isl_basic_map
*bmap2
)
10648 if (!bmap1
|| !bmap2
)
10649 return isl_bool_error
;
10650 return isl_basic_map_plain_cmp(bmap1
, bmap2
) == 0;
10653 isl_bool
isl_basic_set_plain_is_equal(__isl_keep isl_basic_set
*bset1
,
10654 __isl_keep isl_basic_set
*bset2
)
10656 return isl_basic_map_plain_is_equal(bset_to_bmap(bset1
),
10657 bset_to_bmap(bset2
));
10660 static int qsort_bmap_cmp(const void *p1
, const void *p2
)
10662 isl_basic_map
*bmap1
= *(isl_basic_map
**) p1
;
10663 isl_basic_map
*bmap2
= *(isl_basic_map
**) p2
;
10665 return isl_basic_map_plain_cmp(bmap1
, bmap2
);
10668 /* Sort the basic maps of "map" and remove duplicate basic maps.
10670 * While removing basic maps, we make sure that the basic maps remain
10671 * sorted because isl_map_normalize expects the basic maps of the result
10674 static __isl_give isl_map
*sort_and_remove_duplicates(__isl_take isl_map
*map
)
10678 map
= isl_map_remove_empty_parts(map
);
10681 qsort(map
->p
, map
->n
, sizeof(struct isl_basic_map
*), qsort_bmap_cmp
);
10682 for (i
= map
->n
- 1; i
>= 1; --i
) {
10683 if (!isl_basic_map_plain_is_equal(map
->p
[i
- 1], map
->p
[i
]))
10685 isl_basic_map_free(map
->p
[i
-1]);
10686 for (j
= i
; j
< map
->n
; ++j
)
10687 map
->p
[j
- 1] = map
->p
[j
];
10694 /* Remove obvious duplicates among the basic maps of "map".
10696 * Unlike isl_map_normalize, this function does not remove redundant
10697 * constraints and only removes duplicates that have exactly the same
10698 * constraints in the input. It does sort the constraints and
10699 * the basic maps to ease the detection of duplicates.
10701 * If "map" has already been normalized or if the basic maps are
10702 * disjoint, then there can be no duplicates.
10704 __isl_give isl_map
*isl_map_remove_obvious_duplicates(__isl_take isl_map
*map
)
10707 isl_basic_map
*bmap
;
10713 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
| ISL_MAP_DISJOINT
))
10715 for (i
= 0; i
< map
->n
; ++i
) {
10716 bmap
= isl_basic_map_copy(map
->p
[i
]);
10717 bmap
= isl_basic_map_sort_constraints(bmap
);
10719 return isl_map_free(map
);
10720 isl_basic_map_free(map
->p
[i
]);
10724 map
= sort_and_remove_duplicates(map
);
10728 /* We normalize in place, but if anything goes wrong we need
10729 * to return NULL, so we need to make sure we don't change the
10730 * meaning of any possible other copies of map.
10732 __isl_give isl_map
*isl_map_normalize(__isl_take isl_map
*map
)
10735 struct isl_basic_map
*bmap
;
10739 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
))
10741 for (i
= 0; i
< map
->n
; ++i
) {
10742 bmap
= isl_basic_map_normalize(isl_basic_map_copy(map
->p
[i
]));
10745 isl_basic_map_free(map
->p
[i
]);
10749 map
= sort_and_remove_duplicates(map
);
10751 ISL_F_SET(map
, ISL_MAP_NORMALIZED
);
10758 __isl_give isl_set
*isl_set_normalize(__isl_take isl_set
*set
)
10760 return set_from_map(isl_map_normalize(set_to_map(set
)));
10763 isl_bool
isl_map_plain_is_equal(__isl_keep isl_map
*map1
,
10764 __isl_keep isl_map
*map2
)
10769 if (!map1
|| !map2
)
10770 return isl_bool_error
;
10773 return isl_bool_true
;
10774 equal
= isl_map_has_equal_space(map1
, map2
);
10775 if (equal
< 0 || !equal
)
10778 map1
= isl_map_copy(map1
);
10779 map2
= isl_map_copy(map2
);
10780 map1
= isl_map_normalize(map1
);
10781 map2
= isl_map_normalize(map2
);
10782 if (!map1
|| !map2
)
10784 equal
= map1
->n
== map2
->n
;
10785 for (i
= 0; equal
&& i
< map1
->n
; ++i
) {
10786 equal
= isl_basic_map_plain_is_equal(map1
->p
[i
], map2
->p
[i
]);
10790 isl_map_free(map1
);
10791 isl_map_free(map2
);
10794 isl_map_free(map1
);
10795 isl_map_free(map2
);
10796 return isl_bool_error
;
10799 isl_bool
isl_set_plain_is_equal(__isl_keep isl_set
*set1
,
10800 __isl_keep isl_set
*set2
)
10802 return isl_map_plain_is_equal(set_to_map(set1
), set_to_map(set2
));
10805 /* Return the basic maps in "map" as a list.
10807 __isl_give isl_basic_map_list
*isl_map_get_basic_map_list(
10808 __isl_keep isl_map
*map
)
10812 isl_basic_map_list
*list
;
10816 ctx
= isl_map_get_ctx(map
);
10817 list
= isl_basic_map_list_alloc(ctx
, map
->n
);
10819 for (i
= 0; i
< map
->n
; ++i
) {
10820 isl_basic_map
*bmap
;
10822 bmap
= isl_basic_map_copy(map
->p
[i
]);
10823 list
= isl_basic_map_list_add(list
, bmap
);
10829 /* Return the intersection of the elements in the non-empty list "list".
10830 * All elements are assumed to live in the same space.
10832 __isl_give isl_basic_map
*isl_basic_map_list_intersect(
10833 __isl_take isl_basic_map_list
*list
)
10837 isl_basic_map
*bmap
;
10839 n
= isl_basic_map_list_n_basic_map(list
);
10843 isl_die(isl_basic_map_list_get_ctx(list
), isl_error_invalid
,
10844 "expecting non-empty list", goto error
);
10846 bmap
= isl_basic_map_list_get_basic_map(list
, 0);
10847 for (i
= 1; i
< n
; ++i
) {
10848 isl_basic_map
*bmap_i
;
10850 bmap_i
= isl_basic_map_list_get_basic_map(list
, i
);
10851 bmap
= isl_basic_map_intersect(bmap
, bmap_i
);
10854 isl_basic_map_list_free(list
);
10857 isl_basic_map_list_free(list
);
10861 /* Return the intersection of the elements in the non-empty list "list".
10862 * All elements are assumed to live in the same space.
10864 __isl_give isl_basic_set
*isl_basic_set_list_intersect(
10865 __isl_take isl_basic_set_list
*list
)
10867 return isl_basic_map_list_intersect(list
);
10870 /* Return the union of the elements of "list".
10871 * The list is required to have at least one element.
10873 __isl_give isl_set
*isl_basic_set_list_union(
10874 __isl_take isl_basic_set_list
*list
)
10879 isl_basic_set
*bset
;
10882 n
= isl_basic_set_list_n_basic_set(list
);
10886 isl_die(isl_basic_set_list_get_ctx(list
), isl_error_invalid
,
10887 "expecting non-empty list", goto error
);
10889 bset
= isl_basic_set_list_get_basic_set(list
, 0);
10890 space
= isl_basic_set_get_space(bset
);
10891 isl_basic_set_free(bset
);
10893 set
= isl_set_alloc_space(space
, n
, 0);
10894 for (i
= 0; i
< n
; ++i
) {
10895 bset
= isl_basic_set_list_get_basic_set(list
, i
);
10896 set
= isl_set_add_basic_set(set
, bset
);
10899 isl_basic_set_list_free(list
);
10902 isl_basic_set_list_free(list
);
10906 /* Return the union of the elements in the non-empty list "list".
10907 * All elements are assumed to live in the same space.
10909 __isl_give isl_set
*isl_set_list_union(__isl_take isl_set_list
*list
)
10915 n
= isl_set_list_n_set(list
);
10919 isl_die(isl_set_list_get_ctx(list
), isl_error_invalid
,
10920 "expecting non-empty list", goto error
);
10922 set
= isl_set_list_get_set(list
, 0);
10923 for (i
= 1; i
< n
; ++i
) {
10926 set_i
= isl_set_list_get_set(list
, i
);
10927 set
= isl_set_union(set
, set_i
);
10930 isl_set_list_free(list
);
10933 isl_set_list_free(list
);
10937 __isl_give isl_basic_map
*isl_basic_map_product(
10938 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
10940 isl_space
*space_result
= NULL
;
10941 struct isl_basic_map
*bmap
;
10942 unsigned in1
, in2
, out1
, out2
, nparam
, total
, pos
;
10943 struct isl_dim_map
*dim_map1
, *dim_map2
;
10945 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
10947 space_result
= isl_space_product(isl_space_copy(bmap1
->dim
),
10948 isl_space_copy(bmap2
->dim
));
10950 in1
= isl_basic_map_dim(bmap1
, isl_dim_in
);
10951 in2
= isl_basic_map_dim(bmap2
, isl_dim_in
);
10952 out1
= isl_basic_map_dim(bmap1
, isl_dim_out
);
10953 out2
= isl_basic_map_dim(bmap2
, isl_dim_out
);
10954 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
10956 total
= nparam
+ in1
+ in2
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
10957 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
10958 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
10959 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
10960 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
10961 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
10962 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
10963 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
10964 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
10965 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
10966 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
10968 bmap
= isl_basic_map_alloc_space(space_result
,
10969 bmap1
->n_div
+ bmap2
->n_div
,
10970 bmap1
->n_eq
+ bmap2
->n_eq
,
10971 bmap1
->n_ineq
+ bmap2
->n_ineq
);
10972 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
10973 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
10974 bmap
= isl_basic_map_simplify(bmap
);
10975 return isl_basic_map_finalize(bmap
);
10977 isl_basic_map_free(bmap1
);
10978 isl_basic_map_free(bmap2
);
10982 __isl_give isl_basic_map
*isl_basic_map_flat_product(
10983 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
10985 isl_basic_map
*prod
;
10987 prod
= isl_basic_map_product(bmap1
, bmap2
);
10988 prod
= isl_basic_map_flatten(prod
);
10992 __isl_give isl_basic_set
*isl_basic_set_flat_product(
10993 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
10995 return isl_basic_map_flat_range_product(bset1
, bset2
);
10998 __isl_give isl_basic_map
*isl_basic_map_domain_product(
10999 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
11001 isl_space
*space1
, *space2
;
11002 isl_space
*space_result
= NULL
;
11003 isl_basic_map
*bmap
;
11004 isl_size in1
, in2
, out
, nparam
;
11005 unsigned total
, pos
;
11006 struct isl_dim_map
*dim_map1
, *dim_map2
;
11008 in1
= isl_basic_map_dim(bmap1
, isl_dim_in
);
11009 in2
= isl_basic_map_dim(bmap2
, isl_dim_in
);
11010 out
= isl_basic_map_dim(bmap1
, isl_dim_out
);
11011 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
11012 if (in1
< 0 || in2
< 0 || out
< 0 || nparam
< 0)
11015 space1
= isl_basic_map_get_space(bmap1
);
11016 space2
= isl_basic_map_get_space(bmap2
);
11017 space_result
= isl_space_domain_product(space1
, space2
);
11019 total
= nparam
+ in1
+ in2
+ out
+ bmap1
->n_div
+ bmap2
->n_div
;
11020 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
11021 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
11022 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
11023 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
11024 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
11025 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
11026 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
11027 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
);
11028 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out
);
11029 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
11031 bmap
= isl_basic_map_alloc_space(space_result
,
11032 bmap1
->n_div
+ bmap2
->n_div
,
11033 bmap1
->n_eq
+ bmap2
->n_eq
,
11034 bmap1
->n_ineq
+ bmap2
->n_ineq
);
11035 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
11036 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
11037 bmap
= isl_basic_map_simplify(bmap
);
11038 return isl_basic_map_finalize(bmap
);
11040 isl_basic_map_free(bmap1
);
11041 isl_basic_map_free(bmap2
);
11045 __isl_give isl_basic_map
*isl_basic_map_range_product(
11046 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
11049 isl_space
*space_result
= NULL
;
11050 isl_basic_map
*bmap
;
11051 isl_size in
, out1
, out2
, nparam
;
11052 unsigned total
, pos
;
11053 struct isl_dim_map
*dim_map1
, *dim_map2
;
11055 rational
= isl_basic_map_is_rational(bmap1
);
11056 if (rational
>= 0 && rational
)
11057 rational
= isl_basic_map_is_rational(bmap2
);
11058 in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
11059 out1
= isl_basic_map_dim(bmap1
, isl_dim_out
);
11060 out2
= isl_basic_map_dim(bmap2
, isl_dim_out
);
11061 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
11062 if (in
< 0 || out1
< 0 || out2
< 0 || nparam
< 0 || rational
< 0)
11065 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
11068 space_result
= isl_space_range_product(isl_space_copy(bmap1
->dim
),
11069 isl_space_copy(bmap2
->dim
));
11071 total
= nparam
+ in
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
11072 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
11073 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
11074 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
11075 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
11076 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
11077 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
11078 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in
);
11079 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
11080 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
11081 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
11083 bmap
= isl_basic_map_alloc_space(space_result
,
11084 bmap1
->n_div
+ bmap2
->n_div
,
11085 bmap1
->n_eq
+ bmap2
->n_eq
,
11086 bmap1
->n_ineq
+ bmap2
->n_ineq
);
11087 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
11088 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
11090 bmap
= isl_basic_map_set_rational(bmap
);
11091 bmap
= isl_basic_map_simplify(bmap
);
11092 return isl_basic_map_finalize(bmap
);
11094 isl_basic_map_free(bmap1
);
11095 isl_basic_map_free(bmap2
);
11099 __isl_give isl_basic_map
*isl_basic_map_flat_range_product(
11100 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
11102 isl_basic_map
*prod
;
11104 prod
= isl_basic_map_range_product(bmap1
, bmap2
);
11105 prod
= isl_basic_map_flatten_range(prod
);
11109 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
11110 * and collect the results.
11111 * The result live in the space obtained by calling "space_product"
11112 * on the spaces of "map1" and "map2".
11113 * If "remove_duplicates" is set then the result may contain duplicates
11114 * (even if the inputs do not) and so we try and remove the obvious
11117 static __isl_give isl_map
*map_product(__isl_take isl_map
*map1
,
11118 __isl_take isl_map
*map2
,
11119 __isl_give isl_space
*(*space_product
)(__isl_take isl_space
*left
,
11120 __isl_take isl_space
*right
),
11121 __isl_give isl_basic_map
*(*basic_map_product
)(
11122 __isl_take isl_basic_map
*left
,
11123 __isl_take isl_basic_map
*right
),
11124 int remove_duplicates
)
11126 unsigned flags
= 0;
11127 struct isl_map
*result
;
11131 m
= isl_map_has_equal_params(map1
, map2
);
11135 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
11136 "parameters don't match", goto error
);
11138 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
11139 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
11140 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
11142 result
= isl_map_alloc_space(space_product(isl_space_copy(map1
->dim
),
11143 isl_space_copy(map2
->dim
)),
11144 map1
->n
* map2
->n
, flags
);
11147 for (i
= 0; i
< map1
->n
; ++i
)
11148 for (j
= 0; j
< map2
->n
; ++j
) {
11149 struct isl_basic_map
*part
;
11150 part
= basic_map_product(isl_basic_map_copy(map1
->p
[i
]),
11151 isl_basic_map_copy(map2
->p
[j
]));
11152 if (isl_basic_map_is_empty(part
))
11153 isl_basic_map_free(part
);
11155 result
= isl_map_add_basic_map(result
, part
);
11159 if (remove_duplicates
)
11160 result
= isl_map_remove_obvious_duplicates(result
);
11161 isl_map_free(map1
);
11162 isl_map_free(map2
);
11165 isl_map_free(map1
);
11166 isl_map_free(map2
);
11170 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
11172 __isl_give isl_map
*isl_map_product(__isl_take isl_map
*map1
,
11173 __isl_take isl_map
*map2
)
11175 isl_map_align_params_bin(&map1
, &map2
);
11176 return map_product(map1
, map2
, &isl_space_product
,
11177 &isl_basic_map_product
, 0);
11180 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
11182 __isl_give isl_map
*isl_map_flat_product(__isl_take isl_map
*map1
,
11183 __isl_take isl_map
*map2
)
11187 prod
= isl_map_product(map1
, map2
);
11188 prod
= isl_map_flatten(prod
);
11192 /* Given two set A and B, construct its Cartesian product A x B.
11194 __isl_give isl_set
*isl_set_product(__isl_take isl_set
*set1
,
11195 __isl_take isl_set
*set2
)
11197 return isl_map_range_product(set1
, set2
);
11200 __isl_give isl_set
*isl_set_flat_product(__isl_take isl_set
*set1
,
11201 __isl_take isl_set
*set2
)
11203 return isl_map_flat_range_product(set1
, set2
);
11206 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
11208 __isl_give isl_map
*isl_map_domain_product(__isl_take isl_map
*map1
,
11209 __isl_take isl_map
*map2
)
11211 isl_map_align_params_bin(&map1
, &map2
);
11212 return map_product(map1
, map2
, &isl_space_domain_product
,
11213 &isl_basic_map_domain_product
, 1);
11216 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
11218 __isl_give isl_map
*isl_map_range_product(__isl_take isl_map
*map1
,
11219 __isl_take isl_map
*map2
)
11221 isl_map_align_params_bin(&map1
, &map2
);
11222 return map_product(map1
, map2
, &isl_space_range_product
,
11223 &isl_basic_map_range_product
, 1);
11226 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
11228 __isl_give isl_map
*isl_map_factor_domain(__isl_take isl_map
*map
)
11231 isl_size total1
, keep1
, total2
, keep2
;
11233 total1
= isl_map_dim(map
, isl_dim_in
);
11234 total2
= isl_map_dim(map
, isl_dim_out
);
11235 if (total1
< 0 || total2
< 0)
11236 return isl_map_free(map
);
11237 if (!isl_space_domain_is_wrapping(map
->dim
) ||
11238 !isl_space_range_is_wrapping(map
->dim
))
11239 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
11240 "not a product", return isl_map_free(map
));
11242 space
= isl_map_get_space(map
);
11243 space
= isl_space_factor_domain(space
);
11244 keep1
= isl_space_dim(space
, isl_dim_in
);
11245 keep2
= isl_space_dim(space
, isl_dim_out
);
11246 if (keep1
< 0 || keep2
< 0)
11247 map
= isl_map_free(map
);
11248 map
= isl_map_project_out(map
, isl_dim_in
, keep1
, total1
- keep1
);
11249 map
= isl_map_project_out(map
, isl_dim_out
, keep2
, total2
- keep2
);
11250 map
= isl_map_reset_space(map
, space
);
11255 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
11257 __isl_give isl_map
*isl_map_factor_range(__isl_take isl_map
*map
)
11260 isl_size total1
, keep1
, total2
, keep2
;
11262 total1
= isl_map_dim(map
, isl_dim_in
);
11263 total2
= isl_map_dim(map
, isl_dim_out
);
11264 if (total1
< 0 || total2
< 0)
11265 return isl_map_free(map
);
11266 if (!isl_space_domain_is_wrapping(map
->dim
) ||
11267 !isl_space_range_is_wrapping(map
->dim
))
11268 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
11269 "not a product", return isl_map_free(map
));
11271 space
= isl_map_get_space(map
);
11272 space
= isl_space_factor_range(space
);
11273 keep1
= isl_space_dim(space
, isl_dim_in
);
11274 keep2
= isl_space_dim(space
, isl_dim_out
);
11275 if (keep1
< 0 || keep2
< 0)
11276 map
= isl_map_free(map
);
11277 map
= isl_map_project_out(map
, isl_dim_in
, 0, total1
- keep1
);
11278 map
= isl_map_project_out(map
, isl_dim_out
, 0, total2
- keep2
);
11279 map
= isl_map_reset_space(map
, space
);
11284 /* Given a map of the form [A -> B] -> C, return the map A -> C.
11286 __isl_give isl_map
*isl_map_domain_factor_domain(__isl_take isl_map
*map
)
11289 isl_size total
, keep
;
11291 total
= isl_map_dim(map
, isl_dim_in
);
11293 return isl_map_free(map
);
11294 if (!isl_space_domain_is_wrapping(map
->dim
))
11295 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
11296 "domain is not a product", return isl_map_free(map
));
11298 space
= isl_map_get_space(map
);
11299 space
= isl_space_domain_factor_domain(space
);
11300 keep
= isl_space_dim(space
, isl_dim_in
);
11302 map
= isl_map_free(map
);
11303 map
= isl_map_project_out(map
, isl_dim_in
, keep
, total
- keep
);
11304 map
= isl_map_reset_space(map
, space
);
11309 /* Given a map of the form [A -> B] -> C, return the map B -> C.
11311 __isl_give isl_map
*isl_map_domain_factor_range(__isl_take isl_map
*map
)
11314 isl_size total
, keep
;
11316 total
= isl_map_dim(map
, isl_dim_in
);
11318 return isl_map_free(map
);
11319 if (!isl_space_domain_is_wrapping(map
->dim
))
11320 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
11321 "domain is not a product", return isl_map_free(map
));
11323 space
= isl_map_get_space(map
);
11324 space
= isl_space_domain_factor_range(space
);
11325 keep
= isl_space_dim(space
, isl_dim_in
);
11327 map
= isl_map_free(map
);
11328 map
= isl_map_project_out(map
, isl_dim_in
, 0, total
- keep
);
11329 map
= isl_map_reset_space(map
, space
);
11334 /* Given a map A -> [B -> C], extract the map A -> B.
11336 __isl_give isl_map
*isl_map_range_factor_domain(__isl_take isl_map
*map
)
11339 isl_size total
, keep
;
11341 total
= isl_map_dim(map
, isl_dim_out
);
11343 return isl_map_free(map
);
11344 if (!isl_space_range_is_wrapping(map
->dim
))
11345 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
11346 "range is not a product", return isl_map_free(map
));
11348 space
= isl_map_get_space(map
);
11349 space
= isl_space_range_factor_domain(space
);
11350 keep
= isl_space_dim(space
, isl_dim_out
);
11352 map
= isl_map_free(map
);
11353 map
= isl_map_project_out(map
, isl_dim_out
, keep
, total
- keep
);
11354 map
= isl_map_reset_space(map
, space
);
11359 /* Given a map A -> [B -> C], extract the map A -> C.
11361 __isl_give isl_map
*isl_map_range_factor_range(__isl_take isl_map
*map
)
11364 isl_size total
, keep
;
11366 total
= isl_map_dim(map
, isl_dim_out
);
11368 return isl_map_free(map
);
11369 if (!isl_space_range_is_wrapping(map
->dim
))
11370 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
11371 "range is not a product", return isl_map_free(map
));
11373 space
= isl_map_get_space(map
);
11374 space
= isl_space_range_factor_range(space
);
11375 keep
= isl_space_dim(space
, isl_dim_out
);
11377 map
= isl_map_free(map
);
11378 map
= isl_map_project_out(map
, isl_dim_out
, 0, total
- keep
);
11379 map
= isl_map_reset_space(map
, space
);
11384 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
11386 __isl_give isl_map
*isl_map_flat_domain_product(__isl_take isl_map
*map1
,
11387 __isl_take isl_map
*map2
)
11391 prod
= isl_map_domain_product(map1
, map2
);
11392 prod
= isl_map_flatten_domain(prod
);
11396 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
11398 __isl_give isl_map
*isl_map_flat_range_product(__isl_take isl_map
*map1
,
11399 __isl_take isl_map
*map2
)
11403 prod
= isl_map_range_product(map1
, map2
);
11404 prod
= isl_map_flatten_range(prod
);
11408 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map
*bmap
)
11411 uint32_t hash
= isl_hash_init();
11416 bmap
= isl_basic_map_copy(bmap
);
11417 bmap
= isl_basic_map_normalize(bmap
);
11418 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
11421 isl_hash_byte(hash
, bmap
->n_eq
& 0xFF);
11422 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
11424 c_hash
= isl_seq_get_hash(bmap
->eq
[i
], 1 + total
);
11425 isl_hash_hash(hash
, c_hash
);
11427 isl_hash_byte(hash
, bmap
->n_ineq
& 0xFF);
11428 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
11430 c_hash
= isl_seq_get_hash(bmap
->ineq
[i
], 1 + total
);
11431 isl_hash_hash(hash
, c_hash
);
11433 isl_hash_byte(hash
, bmap
->n_div
& 0xFF);
11434 for (i
= 0; i
< bmap
->n_div
; ++i
) {
11436 if (isl_int_is_zero(bmap
->div
[i
][0]))
11438 isl_hash_byte(hash
, i
& 0xFF);
11439 c_hash
= isl_seq_get_hash(bmap
->div
[i
], 1 + 1 + total
);
11440 isl_hash_hash(hash
, c_hash
);
11442 isl_basic_map_free(bmap
);
11446 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set
*bset
)
11448 return isl_basic_map_get_hash(bset_to_bmap(bset
));
11451 uint32_t isl_map_get_hash(__isl_keep isl_map
*map
)
11458 map
= isl_map_copy(map
);
11459 map
= isl_map_normalize(map
);
11463 hash
= isl_hash_init();
11464 for (i
= 0; i
< map
->n
; ++i
) {
11465 uint32_t bmap_hash
;
11466 bmap_hash
= isl_basic_map_get_hash(map
->p
[i
]);
11467 isl_hash_hash(hash
, bmap_hash
);
11475 uint32_t isl_set_get_hash(__isl_keep isl_set
*set
)
11477 return isl_map_get_hash(set_to_map(set
));
11480 /* Return the number of basic maps in the (current) representation of "map".
11482 isl_size
isl_map_n_basic_map(__isl_keep isl_map
*map
)
11484 return map
? map
->n
: isl_size_error
;
11487 isl_size
isl_set_n_basic_set(__isl_keep isl_set
*set
)
11489 return set
? set
->n
: isl_size_error
;
11492 isl_stat
isl_map_foreach_basic_map(__isl_keep isl_map
*map
,
11493 isl_stat (*fn
)(__isl_take isl_basic_map
*bmap
, void *user
), void *user
)
11498 return isl_stat_error
;
11500 for (i
= 0; i
< map
->n
; ++i
)
11501 if (fn(isl_basic_map_copy(map
->p
[i
]), user
) < 0)
11502 return isl_stat_error
;
11504 return isl_stat_ok
;
11507 isl_stat
isl_set_foreach_basic_set(__isl_keep isl_set
*set
,
11508 isl_stat (*fn
)(__isl_take isl_basic_set
*bset
, void *user
), void *user
)
11513 return isl_stat_error
;
11515 for (i
= 0; i
< set
->n
; ++i
)
11516 if (fn(isl_basic_set_copy(set
->p
[i
]), user
) < 0)
11517 return isl_stat_error
;
11519 return isl_stat_ok
;
11522 /* Does "test" succeed on every basic set in "set"?
11524 isl_bool
isl_set_every_basic_set(__isl_keep isl_set
*set
,
11525 isl_bool (*test
)(__isl_keep isl_basic_set
*bset
, void *user
),
11531 return isl_bool_error
;
11533 for (i
= 0; i
< set
->n
; ++i
) {
11536 r
= test(set
->p
[i
], user
);
11541 return isl_bool_true
;
11544 /* Return a list of basic sets, the union of which is equal to "set".
11546 __isl_give isl_basic_set_list
*isl_set_get_basic_set_list(
11547 __isl_keep isl_set
*set
)
11550 isl_basic_set_list
*list
;
11555 list
= isl_basic_set_list_alloc(isl_set_get_ctx(set
), set
->n
);
11556 for (i
= 0; i
< set
->n
; ++i
) {
11557 isl_basic_set
*bset
;
11559 bset
= isl_basic_set_copy(set
->p
[i
]);
11560 list
= isl_basic_set_list_add(list
, bset
);
11566 __isl_give isl_basic_set
*isl_basic_set_lift(__isl_take isl_basic_set
*bset
)
11573 bset
= isl_basic_set_cow(bset
);
11577 space
= isl_basic_set_get_space(bset
);
11578 space
= isl_space_lift(space
, bset
->n_div
);
11581 isl_space_free(bset
->dim
);
11583 bset
->extra
-= bset
->n_div
;
11586 bset
= isl_basic_set_finalize(bset
);
11590 isl_basic_set_free(bset
);
11594 __isl_give isl_set
*isl_set_lift(__isl_take isl_set
*set
)
11600 set
= set_from_map(isl_map_align_divs_internal(set_to_map(set
)));
11605 set
= isl_set_cow(set
);
11609 n_div
= set
->p
[0]->n_div
;
11610 space
= isl_set_get_space(set
);
11611 space
= isl_space_lift(space
, n_div
);
11614 isl_space_free(set
->dim
);
11617 for (i
= 0; i
< set
->n
; ++i
) {
11618 set
->p
[i
] = isl_basic_set_lift(set
->p
[i
]);
11629 int isl_basic_set_size(__isl_keep isl_basic_set
*bset
)
11634 dim
= isl_basic_set_dim(bset
, isl_dim_all
);
11637 size
+= bset
->n_eq
* (1 + dim
);
11638 size
+= bset
->n_ineq
* (1 + dim
);
11639 size
+= bset
->n_div
* (2 + dim
);
11644 int isl_set_size(__isl_keep isl_set
*set
)
11652 for (i
= 0; i
< set
->n
; ++i
)
11653 size
+= isl_basic_set_size(set
->p
[i
]);
11658 /* Check if there is any lower bound (if lower == 0) and/or upper
11659 * bound (if upper == 0) on the specified dim.
11661 static isl_bool
basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
11662 enum isl_dim_type type
, unsigned pos
, int lower
, int upper
)
11668 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
11669 return isl_bool_error
;
11671 off
= isl_basic_map_var_offset(bmap
, type
);
11673 return isl_bool_error
;
11674 involves
= isl_basic_map_any_div_involves_vars(bmap
, off
+ pos
, 1);
11675 if (involves
< 0 || involves
)
11678 for (i
= 0; i
< bmap
->n_eq
; ++i
)
11679 if (!isl_int_is_zero(bmap
->eq
[i
][1 + off
+ pos
]))
11680 return isl_bool_true
;
11682 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
11683 int sgn
= isl_int_sgn(bmap
->ineq
[i
][1 + off
+ pos
]);
11690 return lower
&& upper
;
11693 isl_bool
isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
11694 enum isl_dim_type type
, unsigned pos
)
11696 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 0);
11699 isl_bool
isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map
*bmap
,
11700 enum isl_dim_type type
, unsigned pos
)
11702 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 1);
11705 isl_bool
isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map
*bmap
,
11706 enum isl_dim_type type
, unsigned pos
)
11708 return basic_map_dim_is_bounded(bmap
, type
, pos
, 1, 0);
11711 isl_bool
isl_map_dim_is_bounded(__isl_keep isl_map
*map
,
11712 enum isl_dim_type type
, unsigned pos
)
11717 return isl_bool_error
;
11719 for (i
= 0; i
< map
->n
; ++i
) {
11721 bounded
= isl_basic_map_dim_is_bounded(map
->p
[i
], type
, pos
);
11722 if (bounded
< 0 || !bounded
)
11726 return isl_bool_true
;
11729 /* Return true if the specified dim is involved in both an upper bound
11730 * and a lower bound.
11732 isl_bool
isl_set_dim_is_bounded(__isl_keep isl_set
*set
,
11733 enum isl_dim_type type
, unsigned pos
)
11735 return isl_map_dim_is_bounded(set_to_map(set
), type
, pos
);
11738 /* Does "map" have a bound (according to "fn") for any of its basic maps?
11740 static isl_bool
has_any_bound(__isl_keep isl_map
*map
,
11741 enum isl_dim_type type
, unsigned pos
,
11742 isl_bool (*fn
)(__isl_keep isl_basic_map
*bmap
,
11743 enum isl_dim_type type
, unsigned pos
))
11748 return isl_bool_error
;
11750 for (i
= 0; i
< map
->n
; ++i
) {
11752 bounded
= fn(map
->p
[i
], type
, pos
);
11753 if (bounded
< 0 || bounded
)
11757 return isl_bool_false
;
11760 /* Return 1 if the specified dim is involved in any lower bound.
11762 isl_bool
isl_set_dim_has_any_lower_bound(__isl_keep isl_set
*set
,
11763 enum isl_dim_type type
, unsigned pos
)
11765 return has_any_bound(set
, type
, pos
,
11766 &isl_basic_map_dim_has_lower_bound
);
11769 /* Return 1 if the specified dim is involved in any upper bound.
11771 isl_bool
isl_set_dim_has_any_upper_bound(__isl_keep isl_set
*set
,
11772 enum isl_dim_type type
, unsigned pos
)
11774 return has_any_bound(set
, type
, pos
,
11775 &isl_basic_map_dim_has_upper_bound
);
11778 /* Does "map" have a bound (according to "fn") for all of its basic maps?
11780 static isl_bool
has_bound(__isl_keep isl_map
*map
,
11781 enum isl_dim_type type
, unsigned pos
,
11782 isl_bool (*fn
)(__isl_keep isl_basic_map
*bmap
,
11783 enum isl_dim_type type
, unsigned pos
))
11788 return isl_bool_error
;
11790 for (i
= 0; i
< map
->n
; ++i
) {
11792 bounded
= fn(map
->p
[i
], type
, pos
);
11793 if (bounded
< 0 || !bounded
)
11797 return isl_bool_true
;
11800 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
11802 isl_bool
isl_set_dim_has_lower_bound(__isl_keep isl_set
*set
,
11803 enum isl_dim_type type
, unsigned pos
)
11805 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_lower_bound
);
11808 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
11810 isl_bool
isl_set_dim_has_upper_bound(__isl_keep isl_set
*set
,
11811 enum isl_dim_type type
, unsigned pos
)
11813 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_upper_bound
);
11816 /* For each of the "n" variables starting at "first", determine
11817 * the sign of the variable and put the results in the first "n"
11818 * elements of the array "signs".
11820 * 1 means that the variable is non-negative
11821 * -1 means that the variable is non-positive
11822 * 0 means the variable attains both positive and negative values.
11824 isl_stat
isl_basic_set_vars_get_sign(__isl_keep isl_basic_set
*bset
,
11825 unsigned first
, unsigned n
, int *signs
)
11827 isl_vec
*bound
= NULL
;
11828 struct isl_tab
*tab
= NULL
;
11829 struct isl_tab_undo
*snap
;
11833 total
= isl_basic_set_dim(bset
, isl_dim_all
);
11834 if (total
< 0 || !signs
)
11835 return isl_stat_error
;
11837 bound
= isl_vec_alloc(bset
->ctx
, 1 + total
);
11838 tab
= isl_tab_from_basic_set(bset
, 0);
11839 if (!bound
|| !tab
)
11842 isl_seq_clr(bound
->el
, bound
->size
);
11843 isl_int_set_si(bound
->el
[0], -1);
11845 snap
= isl_tab_snap(tab
);
11846 for (i
= 0; i
< n
; ++i
) {
11849 isl_int_set_si(bound
->el
[1 + first
+ i
], -1);
11850 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
11852 empty
= tab
->empty
;
11853 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
11854 if (isl_tab_rollback(tab
, snap
) < 0)
11862 isl_int_set_si(bound
->el
[1 + first
+ i
], 1);
11863 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
11865 empty
= tab
->empty
;
11866 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
11867 if (isl_tab_rollback(tab
, snap
) < 0)
11870 signs
[i
] = empty
? -1 : 0;
11874 isl_vec_free(bound
);
11875 return isl_stat_ok
;
11878 isl_vec_free(bound
);
11879 return isl_stat_error
;
11882 isl_stat
isl_basic_set_dims_get_sign(__isl_keep isl_basic_set
*bset
,
11883 enum isl_dim_type type
, unsigned first
, unsigned n
, int *signs
)
11885 if (!bset
|| !signs
)
11886 return isl_stat_error
;
11887 if (isl_basic_set_check_range(bset
, type
, first
, n
) < 0)
11888 return isl_stat_error
;
11890 first
+= pos(bset
->dim
, type
) - 1;
11891 return isl_basic_set_vars_get_sign(bset
, first
, n
, signs
);
11894 /* Is it possible for the integer division "div" to depend (possibly
11895 * indirectly) on any output dimensions?
11897 * If the div is undefined, then we conservatively assume that it
11898 * may depend on them.
11899 * Otherwise, we check if it actually depends on them or on any integer
11900 * divisions that may depend on them.
11902 static isl_bool
div_may_involve_output(__isl_keep isl_basic_map
*bmap
, int div
)
11905 isl_size n_out
, n_div
;
11906 unsigned o_out
, o_div
;
11908 if (isl_int_is_zero(bmap
->div
[div
][0]))
11909 return isl_bool_true
;
11911 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
11913 return isl_bool_error
;
11914 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
11916 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + o_out
, n_out
) != -1)
11917 return isl_bool_true
;
11919 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
11921 return isl_bool_error
;
11922 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
11924 for (i
= 0; i
< n_div
; ++i
) {
11925 isl_bool may_involve
;
11927 if (isl_int_is_zero(bmap
->div
[div
][1 + o_div
+ i
]))
11929 may_involve
= div_may_involve_output(bmap
, i
);
11930 if (may_involve
< 0 || may_involve
)
11931 return may_involve
;
11934 return isl_bool_false
;
11937 /* Return the first integer division of "bmap" in the range
11938 * [first, first + n[ that may depend on any output dimensions and
11939 * that has a non-zero coefficient in "c" (where the first coefficient
11940 * in "c" corresponds to integer division "first").
11942 static int first_div_may_involve_output(__isl_keep isl_basic_map
*bmap
,
11943 isl_int
*c
, int first
, int n
)
11950 for (k
= first
; k
< first
+ n
; ++k
) {
11951 isl_bool may_involve
;
11953 if (isl_int_is_zero(c
[k
]))
11955 may_involve
= div_may_involve_output(bmap
, k
);
11956 if (may_involve
< 0)
11965 /* Look for a pair of inequality constraints in "bmap" of the form
11967 * -l + i >= 0 or i >= l
11969 * n + l - i >= 0 or i <= l + n
11971 * with n < "m" and i the output dimension at position "pos".
11972 * (Note that n >= 0 as otherwise the two constraints would conflict.)
11973 * Furthermore, "l" is only allowed to involve parameters, input dimensions
11974 * and earlier output dimensions, as well as integer divisions that do
11975 * not involve any of the output dimensions.
11977 * Return the index of the first inequality constraint or bmap->n_ineq
11978 * if no such pair can be found.
11980 static int find_modulo_constraint_pair(__isl_keep isl_basic_map
*bmap
,
11981 int pos
, isl_int m
)
11986 isl_size n_div
, n_out
;
11987 unsigned o_div
, o_out
;
11990 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
11991 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
11992 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
11993 if (total
< 0 || n_out
< 0 || n_div
< 0)
11996 ctx
= isl_basic_map_get_ctx(bmap
);
11997 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
11998 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
11999 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
12000 if (!isl_int_abs_eq(bmap
->ineq
[i
][o_out
+ pos
], ctx
->one
))
12002 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + o_out
+ pos
+ 1,
12003 n_out
- (pos
+ 1)) != -1)
12005 if (first_div_may_involve_output(bmap
, bmap
->ineq
[i
] + o_div
,
12008 for (j
= i
+ 1; j
< bmap
->n_ineq
; ++j
) {
12009 if (!isl_int_abs_eq(bmap
->ineq
[j
][o_out
+ pos
],
12012 if (!isl_seq_is_neg(bmap
->ineq
[i
] + 1,
12013 bmap
->ineq
[j
] + 1, total
))
12017 if (j
>= bmap
->n_ineq
)
12019 isl_int_add(bmap
->ineq
[i
][0],
12020 bmap
->ineq
[i
][0], bmap
->ineq
[j
][0]);
12021 less
= isl_int_abs_lt(bmap
->ineq
[i
][0], m
);
12022 isl_int_sub(bmap
->ineq
[i
][0],
12023 bmap
->ineq
[i
][0], bmap
->ineq
[j
][0]);
12026 if (isl_int_is_one(bmap
->ineq
[i
][o_out
+ pos
]))
12032 return bmap
->n_ineq
;
12035 /* Return the index of the equality of "bmap" that defines
12036 * the output dimension "pos" in terms of earlier dimensions.
12037 * The equality may also involve integer divisions, as long
12038 * as those integer divisions are defined in terms of
12039 * parameters or input dimensions.
12040 * In this case, *div is set to the number of integer divisions and
12041 * *ineq is set to the number of inequality constraints (provided
12042 * div and ineq are not NULL).
12044 * The equality may also involve a single integer division involving
12045 * the output dimensions (typically only output dimension "pos") as
12046 * long as the coefficient of output dimension "pos" is 1 or -1 and
12047 * there is a pair of constraints i >= l and i <= l + n, with i referring
12048 * to output dimension "pos", l an expression involving only earlier
12049 * dimensions and n smaller than the coefficient of the integer division
12050 * in the equality. In this case, the output dimension can be defined
12051 * in terms of a modulo expression that does not involve the integer division.
12052 * *div is then set to this single integer division and
12053 * *ineq is set to the index of constraint i >= l.
12055 * Return bmap->n_eq if there is no such equality.
12056 * Return -1 on error.
12058 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map
*bmap
,
12059 int pos
, int *div
, int *ineq
)
12062 isl_size n_div
, n_out
;
12063 unsigned o_div
, o_out
;
12065 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
12066 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
12067 if (n_out
< 0 || n_div
< 0)
12070 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
12071 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
12074 *ineq
= bmap
->n_ineq
;
12077 for (j
= 0; j
< bmap
->n_eq
; ++j
) {
12078 if (isl_int_is_zero(bmap
->eq
[j
][o_out
+ pos
]))
12080 if (isl_seq_first_non_zero(bmap
->eq
[j
] + o_out
+ pos
+ 1,
12081 n_out
- (pos
+ 1)) != -1)
12083 k
= first_div_may_involve_output(bmap
, bmap
->eq
[j
] + o_div
,
12087 if (!isl_int_is_one(bmap
->eq
[j
][o_out
+ pos
]) &&
12088 !isl_int_is_negone(bmap
->eq
[j
][o_out
+ pos
]))
12090 if (first_div_may_involve_output(bmap
, bmap
->eq
[j
] + o_div
,
12091 k
+ 1, n_div
- (k
+1)) < n_div
)
12093 l
= find_modulo_constraint_pair(bmap
, pos
,
12094 bmap
->eq
[j
][o_div
+ k
]);
12097 if (l
>= bmap
->n_ineq
)
12109 /* Check if the given basic map is obviously single-valued.
12110 * In particular, for each output dimension, check that there is
12111 * an equality that defines the output dimension in terms of
12112 * earlier dimensions.
12114 isl_bool
isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map
*bmap
)
12119 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
12121 return isl_bool_error
;
12123 for (i
= 0; i
< n_out
; ++i
) {
12126 eq
= isl_basic_map_output_defining_equality(bmap
, i
,
12129 return isl_bool_error
;
12130 if (eq
>= bmap
->n_eq
)
12131 return isl_bool_false
;
12134 return isl_bool_true
;
12137 /* Check if the given basic map is single-valued.
12138 * We simply compute
12142 * and check if the result is a subset of the identity mapping.
12144 isl_bool
isl_basic_map_is_single_valued(__isl_keep isl_basic_map
*bmap
)
12147 isl_basic_map
*test
;
12151 sv
= isl_basic_map_plain_is_single_valued(bmap
);
12155 test
= isl_basic_map_reverse(isl_basic_map_copy(bmap
));
12156 test
= isl_basic_map_apply_range(test
, isl_basic_map_copy(bmap
));
12158 space
= isl_basic_map_get_space(bmap
);
12159 space
= isl_space_map_from_set(isl_space_range(space
));
12160 id
= isl_basic_map_identity(space
);
12162 sv
= isl_basic_map_is_subset(test
, id
);
12164 isl_basic_map_free(test
);
12165 isl_basic_map_free(id
);
12170 /* Check if the given map is obviously single-valued.
12172 isl_bool
isl_map_plain_is_single_valued(__isl_keep isl_map
*map
)
12175 return isl_bool_error
;
12177 return isl_bool_true
;
12179 return isl_bool_false
;
12181 return isl_basic_map_plain_is_single_valued(map
->p
[0]);
12184 /* Check if the given map is single-valued.
12185 * We simply compute
12189 * and check if the result is a subset of the identity mapping.
12191 isl_bool
isl_map_is_single_valued(__isl_keep isl_map
*map
)
12198 sv
= isl_map_plain_is_single_valued(map
);
12202 test
= isl_map_reverse(isl_map_copy(map
));
12203 test
= isl_map_apply_range(test
, isl_map_copy(map
));
12205 space
= isl_space_map_from_set(isl_space_range(isl_map_get_space(map
)));
12206 id
= isl_map_identity(space
);
12208 sv
= isl_map_is_subset(test
, id
);
12210 isl_map_free(test
);
12216 isl_bool
isl_map_is_injective(__isl_keep isl_map
*map
)
12220 map
= isl_map_copy(map
);
12221 map
= isl_map_reverse(map
);
12222 in
= isl_map_is_single_valued(map
);
12228 /* Check if the given map is obviously injective.
12230 isl_bool
isl_map_plain_is_injective(__isl_keep isl_map
*map
)
12234 map
= isl_map_copy(map
);
12235 map
= isl_map_reverse(map
);
12236 in
= isl_map_plain_is_single_valued(map
);
12242 isl_bool
isl_map_is_bijective(__isl_keep isl_map
*map
)
12246 sv
= isl_map_is_single_valued(map
);
12250 return isl_map_is_injective(map
);
12253 isl_bool
isl_set_is_singleton(__isl_keep isl_set
*set
)
12255 return isl_map_is_single_valued(set_to_map(set
));
12258 /* Does "map" only map elements to themselves?
12260 * If the domain and range spaces are different, then "map"
12261 * is considered not to be an identity relation, even if it is empty.
12262 * Otherwise, construct the maximal identity relation and
12263 * check whether "map" is a subset of this relation.
12265 isl_bool
isl_map_is_identity(__isl_keep isl_map
*map
)
12268 isl_bool equal
, is_identity
;
12270 equal
= isl_map_tuple_is_equal(map
, isl_dim_in
, map
, isl_dim_out
);
12271 if (equal
< 0 || !equal
)
12274 id
= isl_map_identity(isl_map_get_space(map
));
12275 is_identity
= isl_map_is_subset(map
, id
);
12278 return is_identity
;
12281 int isl_map_is_translation(__isl_keep isl_map
*map
)
12286 delta
= isl_map_deltas(isl_map_copy(map
));
12287 ok
= isl_set_is_singleton(delta
);
12288 isl_set_free(delta
);
12293 static int unique(isl_int
*p
, unsigned pos
, unsigned len
)
12295 if (isl_seq_first_non_zero(p
, pos
) != -1)
12297 if (isl_seq_first_non_zero(p
+ pos
+ 1, len
- pos
- 1) != -1)
12302 isl_bool
isl_basic_set_is_box(__isl_keep isl_basic_set
*bset
)
12305 isl_size nvar
, ovar
, n_div
;
12307 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
12309 return isl_bool_error
;
12311 return isl_bool_false
;
12313 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
12314 ovar
= isl_space_offset(bset
->dim
, isl_dim_set
);
12315 if (nvar
< 0 || ovar
< 0)
12316 return isl_bool_error
;
12317 for (j
= 0; j
< nvar
; ++j
) {
12318 int lower
= 0, upper
= 0;
12319 for (i
= 0; i
< bset
->n_eq
; ++i
) {
12320 if (isl_int_is_zero(bset
->eq
[i
][1 + ovar
+ j
]))
12322 if (!unique(bset
->eq
[i
] + 1 + ovar
, j
, nvar
))
12323 return isl_bool_false
;
12326 if (i
< bset
->n_eq
)
12328 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
12329 if (isl_int_is_zero(bset
->ineq
[i
][1 + ovar
+ j
]))
12331 if (!unique(bset
->ineq
[i
] + 1 + ovar
, j
, nvar
))
12332 return isl_bool_false
;
12333 if (isl_int_is_pos(bset
->ineq
[i
][1 + ovar
+ j
]))
12338 if (!lower
|| !upper
)
12339 return isl_bool_false
;
12342 return isl_bool_true
;
12345 isl_bool
isl_set_is_box(__isl_keep isl_set
*set
)
12348 return isl_bool_error
;
12350 return isl_bool_false
;
12352 return isl_basic_set_is_box(set
->p
[0]);
12355 isl_bool
isl_basic_set_is_wrapping(__isl_keep isl_basic_set
*bset
)
12358 return isl_bool_error
;
12360 return isl_space_is_wrapping(bset
->dim
);
12363 isl_bool
isl_set_is_wrapping(__isl_keep isl_set
*set
)
12366 return isl_bool_error
;
12368 return isl_space_is_wrapping(set
->dim
);
12371 /* Modify the space of "map" through a call to "change".
12372 * If "can_change" is set (not NULL), then first call it to check
12373 * if the modification is allowed, printing the error message "cannot_change"
12376 static __isl_give isl_map
*isl_map_change_space(__isl_take isl_map
*map
,
12377 isl_bool (*can_change
)(__isl_keep isl_map
*map
),
12378 const char *cannot_change
,
12379 __isl_give isl_space
*(*change
)(__isl_take isl_space
*space
))
12387 ok
= can_change
? can_change(map
) : isl_bool_true
;
12389 return isl_map_free(map
);
12391 isl_die(isl_map_get_ctx(map
), isl_error_invalid
, cannot_change
,
12392 return isl_map_free(map
));
12394 space
= change(isl_map_get_space(map
));
12395 map
= isl_map_reset_space(map
, space
);
12400 /* Is the domain of "map" a wrapped relation?
12402 isl_bool
isl_map_domain_is_wrapping(__isl_keep isl_map
*map
)
12405 return isl_bool_error
;
12407 return isl_space_domain_is_wrapping(map
->dim
);
12410 /* Does "map" have a wrapped relation in both domain and range?
12412 isl_bool
isl_map_is_product(__isl_keep isl_map
*map
)
12414 return isl_space_is_product(isl_map_peek_space(map
));
12417 /* Is the range of "map" a wrapped relation?
12419 isl_bool
isl_map_range_is_wrapping(__isl_keep isl_map
*map
)
12422 return isl_bool_error
;
12424 return isl_space_range_is_wrapping(map
->dim
);
12427 __isl_give isl_basic_set
*isl_basic_map_wrap(__isl_take isl_basic_map
*bmap
)
12431 space
= isl_basic_map_take_space(bmap
);
12432 space
= isl_space_wrap(space
);
12433 bmap
= isl_basic_map_restore_space(bmap
, space
);
12435 bmap
= isl_basic_map_finalize(bmap
);
12437 return bset_from_bmap(bmap
);
12440 /* Given a map A -> B, return the set (A -> B).
12442 __isl_give isl_set
*isl_map_wrap(__isl_take isl_map
*map
)
12444 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_wrap
);
12447 __isl_give isl_basic_map
*isl_basic_set_unwrap(__isl_take isl_basic_set
*bset
)
12449 bset
= isl_basic_set_cow(bset
);
12453 bset
->dim
= isl_space_unwrap(bset
->dim
);
12457 bset
= isl_basic_set_finalize(bset
);
12459 return bset_to_bmap(bset
);
12461 isl_basic_set_free(bset
);
12465 /* Given a set (A -> B), return the map A -> B.
12466 * Error out if "set" is not of the form (A -> B).
12468 __isl_give isl_map
*isl_set_unwrap(__isl_take isl_set
*set
)
12470 return isl_map_change_space(set
, &isl_set_is_wrapping
,
12471 "not a wrapping set", &isl_space_unwrap
);
12474 __isl_give isl_basic_map
*isl_basic_map_reset(__isl_take isl_basic_map
*bmap
,
12475 enum isl_dim_type type
)
12479 space
= isl_basic_map_take_space(bmap
);
12480 space
= isl_space_reset(space
, type
);
12481 bmap
= isl_basic_map_restore_space(bmap
, space
);
12483 bmap
= isl_basic_map_mark_final(bmap
);
12488 __isl_give isl_map
*isl_map_reset(__isl_take isl_map
*map
,
12489 enum isl_dim_type type
)
12497 if (!isl_space_is_named_or_nested(map
->dim
, type
))
12500 map
= isl_map_cow(map
);
12504 for (i
= 0; i
< map
->n
; ++i
) {
12505 map
->p
[i
] = isl_basic_map_reset(map
->p
[i
], type
);
12510 space
= isl_map_take_space(map
);
12511 space
= isl_space_reset(space
, type
);
12512 map
= isl_map_restore_space(map
, space
);
12520 __isl_give isl_basic_map
*isl_basic_map_flatten(__isl_take isl_basic_map
*bmap
)
12524 space
= isl_basic_map_take_space(bmap
);
12525 space
= isl_space_flatten(space
);
12526 bmap
= isl_basic_map_restore_space(bmap
, space
);
12528 bmap
= isl_basic_map_mark_final(bmap
);
12533 __isl_give isl_basic_set
*isl_basic_set_flatten(__isl_take isl_basic_set
*bset
)
12535 return bset_from_bmap(isl_basic_map_flatten(bset_to_bmap(bset
)));
12538 __isl_give isl_basic_map
*isl_basic_map_flatten_domain(
12539 __isl_take isl_basic_map
*bmap
)
12543 space
= isl_basic_map_take_space(bmap
);
12544 space
= isl_space_flatten_domain(space
);
12545 bmap
= isl_basic_map_restore_space(bmap
, space
);
12547 bmap
= isl_basic_map_mark_final(bmap
);
12552 __isl_give isl_basic_map
*isl_basic_map_flatten_range(
12553 __isl_take isl_basic_map
*bmap
)
12557 space
= isl_basic_map_take_space(bmap
);
12558 space
= isl_space_flatten_range(space
);
12559 bmap
= isl_basic_map_restore_space(bmap
, space
);
12561 bmap
= isl_basic_map_mark_final(bmap
);
12566 /* Remove any internal structure from the spaces of domain and range of "map".
12568 __isl_give isl_map
*isl_map_flatten(__isl_take isl_map
*map
)
12573 if (!map
->dim
->nested
[0] && !map
->dim
->nested
[1])
12576 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten
);
12579 __isl_give isl_set
*isl_set_flatten(__isl_take isl_set
*set
)
12581 return set_from_map(isl_map_flatten(set_to_map(set
)));
12584 __isl_give isl_map
*isl_set_flatten_map(__isl_take isl_set
*set
)
12586 isl_space
*space
, *flat_space
;
12589 space
= isl_set_get_space(set
);
12590 flat_space
= isl_space_flatten(isl_space_copy(space
));
12591 map
= isl_map_identity(isl_space_join(isl_space_reverse(space
),
12593 map
= isl_map_intersect_domain(map
, set
);
12598 /* Remove any internal structure from the space of the domain of "map".
12600 __isl_give isl_map
*isl_map_flatten_domain(__isl_take isl_map
*map
)
12605 if (!map
->dim
->nested
[0])
12608 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten_domain
);
12611 /* Remove any internal structure from the space of the range of "map".
12613 __isl_give isl_map
*isl_map_flatten_range(__isl_take isl_map
*map
)
12618 if (!map
->dim
->nested
[1])
12621 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten_range
);
12624 /* Reorder the dimensions of "bmap" according to the given dim_map
12625 * and set the dimension specification to "space" and
12626 * perform Gaussian elimination on the result.
12628 __isl_give isl_basic_map
*isl_basic_map_realign(__isl_take isl_basic_map
*bmap
,
12629 __isl_take isl_space
*space
, __isl_take
struct isl_dim_map
*dim_map
)
12631 isl_basic_map
*res
;
12635 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
12636 if (n_div
< 0 || !space
|| !dim_map
)
12639 flags
= bmap
->flags
;
12640 ISL_FL_CLR(flags
, ISL_BASIC_MAP_FINAL
);
12641 ISL_FL_CLR(flags
, ISL_BASIC_MAP_SORTED
);
12642 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
12643 res
= isl_basic_map_alloc_space(space
, n_div
, bmap
->n_eq
, bmap
->n_ineq
);
12644 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
12646 res
->flags
= flags
;
12647 res
= isl_basic_map_gauss(res
, NULL
);
12648 res
= isl_basic_map_finalize(res
);
12651 isl_dim_map_free(dim_map
);
12652 isl_basic_map_free(bmap
);
12653 isl_space_free(space
);
12657 /* Reorder the dimensions of "map" according to given reordering.
12659 __isl_give isl_map
*isl_map_realign(__isl_take isl_map
*map
,
12660 __isl_take isl_reordering
*r
)
12663 struct isl_dim_map
*dim_map
;
12665 map
= isl_map_cow(map
);
12666 dim_map
= isl_dim_map_from_reordering(r
);
12667 if (!map
|| !r
|| !dim_map
)
12670 for (i
= 0; i
< map
->n
; ++i
) {
12671 struct isl_dim_map
*dim_map_i
;
12674 dim_map_i
= isl_dim_map_extend(dim_map
, map
->p
[i
]);
12676 space
= isl_reordering_get_space(r
);
12677 map
->p
[i
] = isl_basic_map_realign(map
->p
[i
], space
, dim_map_i
);
12683 map
= isl_map_reset_space(map
, isl_reordering_get_space(r
));
12684 map
= isl_map_unmark_normalized(map
);
12686 isl_reordering_free(r
);
12687 isl_dim_map_free(dim_map
);
12690 isl_dim_map_free(dim_map
);
12692 isl_reordering_free(r
);
12696 __isl_give isl_set
*isl_set_realign(__isl_take isl_set
*set
,
12697 __isl_take isl_reordering
*r
)
12699 return set_from_map(isl_map_realign(set_to_map(set
), r
));
12702 __isl_give isl_map
*isl_map_align_params(__isl_take isl_map
*map
,
12703 __isl_take isl_space
*model
)
12708 if (!map
|| !model
)
12711 ctx
= isl_space_get_ctx(model
);
12712 if (!isl_space_has_named_params(model
))
12713 isl_die(ctx
, isl_error_invalid
,
12714 "model has unnamed parameters", goto error
);
12715 if (isl_map_check_named_params(map
) < 0)
12717 aligned
= isl_map_space_has_equal_params(map
, model
);
12722 isl_reordering
*exp
;
12724 space
= isl_map_peek_space(map
);
12725 exp
= isl_parameter_alignment_reordering(space
, model
);
12726 map
= isl_map_realign(map
, exp
);
12729 isl_space_free(model
);
12732 isl_space_free(model
);
12737 __isl_give isl_set
*isl_set_align_params(__isl_take isl_set
*set
,
12738 __isl_take isl_space
*model
)
12740 return isl_map_align_params(set
, model
);
12743 /* Align the parameters of "bmap" to those of "model", introducing
12744 * additional parameters if needed.
12746 __isl_give isl_basic_map
*isl_basic_map_align_params(
12747 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*model
)
12750 isl_bool equal_params
;
12751 isl_space
*bmap_space
;
12753 if (!bmap
|| !model
)
12756 ctx
= isl_space_get_ctx(model
);
12757 if (!isl_space_has_named_params(model
))
12758 isl_die(ctx
, isl_error_invalid
,
12759 "model has unnamed parameters", goto error
);
12760 if (isl_basic_map_check_named_params(bmap
) < 0)
12762 bmap_space
= isl_basic_map_peek_space(bmap
);
12763 equal_params
= isl_space_has_equal_params(bmap_space
, model
);
12764 if (equal_params
< 0)
12766 if (!equal_params
) {
12767 isl_reordering
*exp
;
12768 struct isl_dim_map
*dim_map
;
12770 exp
= isl_parameter_alignment_reordering(bmap_space
, model
);
12771 dim_map
= isl_dim_map_from_reordering(exp
);
12772 bmap
= isl_basic_map_realign(bmap
,
12773 isl_reordering_get_space(exp
),
12774 isl_dim_map_extend(dim_map
, bmap
));
12775 isl_reordering_free(exp
);
12776 isl_dim_map_free(dim_map
);
12779 isl_space_free(model
);
12782 isl_space_free(model
);
12783 isl_basic_map_free(bmap
);
12787 /* Do "bset" and "space" have the same parameters?
12789 isl_bool
isl_basic_set_space_has_equal_params(__isl_keep isl_basic_set
*bset
,
12790 __isl_keep isl_space
*space
)
12792 isl_space
*bset_space
;
12794 bset_space
= isl_basic_set_peek_space(bset
);
12795 return isl_space_has_equal_params(bset_space
, space
);
12798 /* Do "map" and "space" have the same parameters?
12800 isl_bool
isl_map_space_has_equal_params(__isl_keep isl_map
*map
,
12801 __isl_keep isl_space
*space
)
12803 isl_space
*map_space
;
12805 map_space
= isl_map_peek_space(map
);
12806 return isl_space_has_equal_params(map_space
, space
);
12809 /* Do "set" and "space" have the same parameters?
12811 isl_bool
isl_set_space_has_equal_params(__isl_keep isl_set
*set
,
12812 __isl_keep isl_space
*space
)
12814 return isl_map_space_has_equal_params(set_to_map(set
), space
);
12817 /* Align the parameters of "bset" to those of "model", introducing
12818 * additional parameters if needed.
12820 __isl_give isl_basic_set
*isl_basic_set_align_params(
12821 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*model
)
12823 return isl_basic_map_align_params(bset
, model
);
12827 #define TYPE isl_map
12828 #define isl_map_drop_dims isl_map_drop
12829 #include "isl_drop_unused_params_templ.c"
12831 /* Drop all parameters not referenced by "set".
12833 __isl_give isl_set
*isl_set_drop_unused_params(
12834 __isl_take isl_set
*set
)
12836 return set_from_map(isl_map_drop_unused_params(set_to_map(set
)));
12840 #define TYPE isl_basic_map
12841 #define isl_basic_map_drop_dims isl_basic_map_drop
12842 #include "isl_drop_unused_params_templ.c"
12844 /* Drop all parameters not referenced by "bset".
12846 __isl_give isl_basic_set
*isl_basic_set_drop_unused_params(
12847 __isl_take isl_basic_set
*bset
)
12849 return bset_from_bmap(isl_basic_map_drop_unused_params(
12850 bset_to_bmap(bset
)));
12853 /* Given a tuple of identifiers "tuple" in a space that corresponds
12854 * to that of "set", if any of those identifiers appear as parameters
12855 * in "set", then equate those parameters with the corresponding
12856 * set dimensions and project out the parameters.
12857 * The result therefore has no such parameters.
12859 static __isl_give isl_set
*equate_params(__isl_take isl_set
*set
,
12860 __isl_keep isl_multi_id
*tuple
)
12864 isl_space
*set_space
, *tuple_space
;
12866 set_space
= isl_set_peek_space(set
);
12867 tuple_space
= isl_multi_id_peek_space(tuple
);
12868 if (isl_space_check_equal_tuples(tuple_space
, set_space
) < 0)
12869 return isl_set_free(set
);
12870 n
= isl_multi_id_size(tuple
);
12872 return isl_set_free(set
);
12873 for (i
= 0; i
< n
; ++i
) {
12877 id
= isl_multi_id_get_at(tuple
, i
);
12879 return isl_set_free(set
);
12880 pos
= isl_set_find_dim_by_id(set
, isl_dim_param
, id
);
12884 set
= isl_set_equate(set
, isl_dim_param
, pos
, isl_dim_set
, i
);
12885 set
= isl_set_project_out(set
, isl_dim_param
, pos
, 1);
12890 /* Bind the set dimensions of "set" to parameters with identifiers
12891 * specified by "tuple", living in the same space as "set".
12893 * If no parameters with these identifiers appear in "set" already,
12894 * then the set dimensions are simply reinterpreted as parameters.
12895 * Otherwise, the parameters are first equated to the corresponding
12898 __isl_give isl_set
*isl_set_bind(__isl_take isl_set
*set
,
12899 __isl_take isl_multi_id
*tuple
)
12903 set
= equate_params(set
, tuple
);
12904 space
= isl_set_get_space(set
);
12905 space
= isl_space_bind_set(space
, tuple
);
12906 isl_multi_id_free(tuple
);
12907 set
= isl_set_reset_space(set
, space
);
12912 /* Given a tuple of identifiers "tuple" in a space that corresponds
12913 * to the domain of "map", if any of those identifiers appear as parameters
12914 * in "map", then equate those parameters with the corresponding
12915 * input dimensions and project out the parameters.
12916 * The result therefore has no such parameters.
12918 static __isl_give isl_map
*map_equate_params(__isl_take isl_map
*map
,
12919 __isl_keep isl_multi_id
*tuple
)
12923 isl_space
*map_space
, *tuple_space
;
12925 map_space
= isl_map_peek_space(map
);
12926 tuple_space
= isl_multi_id_peek_space(tuple
);
12927 if (isl_space_check_domain_tuples(tuple_space
, map_space
) < 0)
12928 return isl_map_free(map
);
12929 n
= isl_multi_id_size(tuple
);
12931 return isl_map_free(map
);
12932 for (i
= 0; i
< n
; ++i
) {
12936 id
= isl_multi_id_get_at(tuple
, i
);
12938 return isl_map_free(map
);
12939 pos
= isl_map_find_dim_by_id(map
, isl_dim_param
, id
);
12943 map
= isl_map_equate(map
, isl_dim_param
, pos
, isl_dim_in
, i
);
12944 map
= isl_map_project_out(map
, isl_dim_param
, pos
, 1);
12949 /* Bind the input dimensions of "map" to parameters with identifiers
12950 * specified by "tuple", living in the domain space of "map".
12952 * If no parameters with these identifiers appear in "map" already,
12953 * then the input dimensions are simply reinterpreted as parameters.
12954 * Otherwise, the parameters are first equated to the corresponding
12955 * input dimensions.
12957 __isl_give isl_set
*isl_map_bind_domain(__isl_take isl_map
*map
,
12958 __isl_take isl_multi_id
*tuple
)
12963 map
= map_equate_params(map
, tuple
);
12964 space
= isl_map_get_space(map
);
12965 space
= isl_space_bind_map_domain(space
, tuple
);
12966 isl_multi_id_free(tuple
);
12967 set
= set_from_map(isl_map_reset_space(map
, space
));
12972 /* Bind the output dimensions of "map" to parameters with identifiers
12973 * specified by "tuple", living in the range space of "map".
12975 * Since binding is more easily implemented on the domain,
12976 * bind the input dimensions of the inverse of "map".
12978 __isl_give isl_set
*isl_map_bind_range(__isl_take isl_map
*map
,
12979 __isl_take isl_multi_id
*tuple
)
12981 return isl_map_bind_domain(isl_map_reverse(map
), tuple
);
12984 /* Insert a domain corresponding to "tuple"
12985 * into the nullary or unary relation "set".
12986 * The result has an extra initial tuple and is therefore
12987 * either a unary or binary relation.
12988 * Any parameters with identifiers in "tuple" are reinterpreted
12989 * as the corresponding domain dimensions.
12991 static __isl_give isl_map
*unbind_params_insert_domain(
12992 __isl_take isl_set
*set
, __isl_take isl_multi_id
*tuple
)
12997 space
= isl_set_peek_space(set
);
12998 r
= isl_reordering_unbind_params_insert_domain(space
, tuple
);
12999 isl_multi_id_free(tuple
);
13001 return isl_map_realign(set_to_map(set
), r
);
13004 /* Construct a set with "tuple" as domain from the parameter domain "set".
13005 * Any parameters with identifiers in "tuple" are reinterpreted
13006 * as the corresponding set dimensions.
13008 __isl_give isl_set
*isl_set_unbind_params(__isl_take isl_set
*set
,
13009 __isl_take isl_multi_id
*tuple
)
13011 isl_bool is_params
;
13013 is_params
= isl_set_is_params(set
);
13015 set
= isl_set_free(set
);
13016 else if (!is_params
)
13017 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
13018 "expecting parameter domain", set
= isl_set_free(set
));
13019 return set_from_map(unbind_params_insert_domain(set
, tuple
));
13022 /* Check that "set" is a proper set, i.e., that it is not a parameter domain.
13024 static isl_stat
isl_set_check_is_set(__isl_keep isl_set
*set
)
13026 isl_bool is_params
;
13028 is_params
= isl_set_is_params(set
);
13030 return isl_stat_error
;
13031 else if (is_params
)
13032 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
13033 "expecting proper set", return isl_stat_error
);
13035 return isl_stat_ok
;
13038 /* Construct a map with "domain" as domain and "set" as range.
13039 * Any parameters with identifiers in "domain" are reinterpreted
13040 * as the corresponding domain dimensions.
13042 __isl_give isl_map
*isl_set_unbind_params_insert_domain(
13043 __isl_take isl_set
*set
, __isl_take isl_multi_id
*domain
)
13045 if (isl_set_check_is_set(set
) < 0)
13046 set
= isl_set_free(set
);
13047 return unbind_params_insert_domain(set
, domain
);
13050 /* Construct a map with "domain" as domain and "set" as range.
13052 __isl_give isl_map
*isl_set_insert_domain(__isl_take isl_set
*set
,
13053 __isl_take isl_space
*domain
)
13059 if (isl_set_check_is_set(set
) < 0 || isl_space_check_is_set(domain
) < 0)
13060 domain
= isl_space_free(domain
);
13061 dim
= isl_space_dim(domain
, isl_dim_set
);
13063 domain
= isl_space_free(domain
);
13064 space
= isl_set_get_space(set
);
13065 domain
= isl_space_replace_params(domain
, space
);
13066 space
= isl_space_map_from_domain_and_range(domain
, space
);
13068 map
= isl_map_from_range(set
);
13069 map
= isl_map_add_dims(map
, isl_dim_in
, dim
);
13070 map
= isl_map_reset_space(map
, space
);
13075 __isl_give isl_mat
*isl_basic_map_equalities_matrix(
13076 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
13077 enum isl_dim_type c2
, enum isl_dim_type c3
,
13078 enum isl_dim_type c4
, enum isl_dim_type c5
)
13080 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
13081 struct isl_mat
*mat
;
13086 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
13089 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_eq
, total
+ 1);
13092 for (i
= 0; i
< bmap
->n_eq
; ++i
)
13093 for (j
= 0, pos
= 0; j
< 5; ++j
) {
13094 int off
= isl_basic_map_offset(bmap
, c
[j
]);
13095 isl_size dim
= isl_basic_map_dim(bmap
, c
[j
]);
13097 return isl_mat_free(mat
);
13098 for (k
= 0; k
< dim
; ++k
) {
13099 isl_int_set(mat
->row
[i
][pos
],
13100 bmap
->eq
[i
][off
+ k
]);
13108 __isl_give isl_mat
*isl_basic_map_inequalities_matrix(
13109 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
13110 enum isl_dim_type c2
, enum isl_dim_type c3
,
13111 enum isl_dim_type c4
, enum isl_dim_type c5
)
13113 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
13114 struct isl_mat
*mat
;
13119 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
13122 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_ineq
, total
+ 1);
13125 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
13126 for (j
= 0, pos
= 0; j
< 5; ++j
) {
13127 int off
= isl_basic_map_offset(bmap
, c
[j
]);
13128 isl_size dim
= isl_basic_map_dim(bmap
, c
[j
]);
13130 return isl_mat_free(mat
);
13131 for (k
= 0; k
< dim
; ++k
) {
13132 isl_int_set(mat
->row
[i
][pos
],
13133 bmap
->ineq
[i
][off
+ k
]);
13141 __isl_give isl_basic_map
*isl_basic_map_from_constraint_matrices(
13142 __isl_take isl_space
*space
,
13143 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
13144 enum isl_dim_type c2
, enum isl_dim_type c3
,
13145 enum isl_dim_type c4
, enum isl_dim_type c5
)
13147 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
13148 isl_basic_map
*bmap
= NULL
;
13155 dim
= isl_space_dim(space
, isl_dim_all
);
13156 if (dim
< 0 || !eq
|| !ineq
)
13159 if (eq
->n_col
!= ineq
->n_col
)
13160 isl_die(space
->ctx
, isl_error_invalid
,
13161 "equalities and inequalities matrices should have "
13162 "same number of columns", goto error
);
13166 if (eq
->n_col
< total
)
13167 isl_die(space
->ctx
, isl_error_invalid
,
13168 "number of columns too small", goto error
);
13170 extra
= eq
->n_col
- total
;
13172 bmap
= isl_basic_map_alloc_space(isl_space_copy(space
), extra
,
13173 eq
->n_row
, ineq
->n_row
);
13176 for (i
= 0; i
< extra
; ++i
) {
13177 k
= isl_basic_map_alloc_div(bmap
);
13180 isl_int_set_si(bmap
->div
[k
][0], 0);
13182 for (i
= 0; i
< eq
->n_row
; ++i
) {
13183 l
= isl_basic_map_alloc_equality(bmap
);
13186 for (j
= 0, pos
= 0; j
< 5; ++j
) {
13187 int off
= isl_basic_map_offset(bmap
, c
[j
]);
13188 isl_size dim
= isl_basic_map_dim(bmap
, c
[j
]);
13191 for (k
= 0; k
< dim
; ++k
) {
13192 isl_int_set(bmap
->eq
[l
][off
+ k
],
13198 for (i
= 0; i
< ineq
->n_row
; ++i
) {
13199 l
= isl_basic_map_alloc_inequality(bmap
);
13202 for (j
= 0, pos
= 0; j
< 5; ++j
) {
13203 int off
= isl_basic_map_offset(bmap
, c
[j
]);
13204 isl_size dim
= isl_basic_map_dim(bmap
, c
[j
]);
13207 for (k
= 0; k
< dim
; ++k
) {
13208 isl_int_set(bmap
->ineq
[l
][off
+ k
],
13209 ineq
->row
[i
][pos
]);
13215 isl_space_free(space
);
13217 isl_mat_free(ineq
);
13219 bmap
= isl_basic_map_simplify(bmap
);
13220 return isl_basic_map_finalize(bmap
);
13222 isl_space_free(space
);
13224 isl_mat_free(ineq
);
13225 isl_basic_map_free(bmap
);
13229 __isl_give isl_mat
*isl_basic_set_equalities_matrix(
13230 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
13231 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
13233 return isl_basic_map_equalities_matrix(bset_to_bmap(bset
),
13234 c1
, c2
, c3
, c4
, isl_dim_in
);
13237 __isl_give isl_mat
*isl_basic_set_inequalities_matrix(
13238 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
13239 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
13241 return isl_basic_map_inequalities_matrix(bset_to_bmap(bset
),
13242 c1
, c2
, c3
, c4
, isl_dim_in
);
13245 __isl_give isl_basic_set
*isl_basic_set_from_constraint_matrices(
13246 __isl_take isl_space
*space
,
13247 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
13248 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
13250 isl_basic_map
*bmap
;
13251 bmap
= isl_basic_map_from_constraint_matrices(space
, eq
, ineq
,
13252 c1
, c2
, c3
, c4
, isl_dim_in
);
13253 return bset_from_bmap(bmap
);
13256 isl_bool
isl_basic_map_can_zip(__isl_keep isl_basic_map
*bmap
)
13259 return isl_bool_error
;
13261 return isl_space_can_zip(bmap
->dim
);
13264 isl_bool
isl_map_can_zip(__isl_keep isl_map
*map
)
13267 return isl_bool_error
;
13269 return isl_space_can_zip(map
->dim
);
13272 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
13273 * (A -> C) -> (B -> D).
13275 __isl_give isl_basic_map
*isl_basic_map_zip(__isl_take isl_basic_map
*bmap
)
13285 if (!isl_basic_map_can_zip(bmap
))
13286 isl_die(bmap
->ctx
, isl_error_invalid
,
13287 "basic map cannot be zipped", goto error
);
13288 n_in
= isl_space_dim(bmap
->dim
->nested
[0], isl_dim_in
);
13289 n1
= isl_space_dim(bmap
->dim
->nested
[0], isl_dim_out
);
13290 n2
= isl_space_dim(bmap
->dim
->nested
[1], isl_dim_in
);
13291 if (n_in
< 0 || n1
< 0 || n2
< 0)
13292 return isl_basic_map_free(bmap
);
13293 pos
= isl_basic_map_offset(bmap
, isl_dim_in
) + n_in
;
13294 bmap
= isl_basic_map_cow(bmap
);
13295 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
13298 bmap
->dim
= isl_space_zip(bmap
->dim
);
13301 bmap
= isl_basic_map_mark_final(bmap
);
13304 isl_basic_map_free(bmap
);
13308 /* Given a map (A -> B) -> (C -> D), return the corresponding map
13309 * (A -> C) -> (B -> D).
13311 __isl_give isl_map
*isl_map_zip(__isl_take isl_map
*map
)
13316 if (!isl_map_can_zip(map
))
13317 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be zipped",
13320 return isl_map_transform(map
, &isl_space_zip
, &isl_basic_map_zip
);
13326 /* Can we apply isl_basic_map_curry to "bmap"?
13327 * That is, does it have a nested relation in its domain?
13329 isl_bool
isl_basic_map_can_curry(__isl_keep isl_basic_map
*bmap
)
13332 return isl_bool_error
;
13334 return isl_space_can_curry(bmap
->dim
);
13337 /* Can we apply isl_map_curry to "map"?
13338 * That is, does it have a nested relation in its domain?
13340 isl_bool
isl_map_can_curry(__isl_keep isl_map
*map
)
13343 return isl_bool_error
;
13345 return isl_space_can_curry(map
->dim
);
13348 /* Given a basic map (A -> B) -> C, return the corresponding basic map
13351 __isl_give isl_basic_map
*isl_basic_map_curry(__isl_take isl_basic_map
*bmap
)
13357 if (!isl_basic_map_can_curry(bmap
))
13358 isl_die(bmap
->ctx
, isl_error_invalid
,
13359 "basic map cannot be curried", goto error
);
13360 bmap
= isl_basic_map_cow(bmap
);
13363 bmap
->dim
= isl_space_curry(bmap
->dim
);
13366 bmap
= isl_basic_map_mark_final(bmap
);
13369 isl_basic_map_free(bmap
);
13373 /* Given a map (A -> B) -> C, return the corresponding map
13376 __isl_give isl_map
*isl_map_curry(__isl_take isl_map
*map
)
13378 return isl_map_change_space(map
, &isl_map_can_curry
,
13379 "map cannot be curried", &isl_space_curry
);
13382 /* Can isl_map_range_curry be applied to "map"?
13383 * That is, does it have a nested relation in its range,
13384 * the domain of which is itself a nested relation?
13386 isl_bool
isl_map_can_range_curry(__isl_keep isl_map
*map
)
13389 return isl_bool_error
;
13391 return isl_space_can_range_curry(map
->dim
);
13394 /* Given a map A -> ((B -> C) -> D), return the corresponding map
13395 * A -> (B -> (C -> D)).
13397 __isl_give isl_map
*isl_map_range_curry(__isl_take isl_map
*map
)
13399 return isl_map_change_space(map
, &isl_map_can_range_curry
,
13400 "map range cannot be curried",
13401 &isl_space_range_curry
);
13404 /* Can we apply isl_basic_map_uncurry to "bmap"?
13405 * That is, does it have a nested relation in its domain?
13407 isl_bool
isl_basic_map_can_uncurry(__isl_keep isl_basic_map
*bmap
)
13410 return isl_bool_error
;
13412 return isl_space_can_uncurry(bmap
->dim
);
13415 /* Can we apply isl_map_uncurry to "map"?
13416 * That is, does it have a nested relation in its domain?
13418 isl_bool
isl_map_can_uncurry(__isl_keep isl_map
*map
)
13421 return isl_bool_error
;
13423 return isl_space_can_uncurry(map
->dim
);
13426 /* Given a basic map A -> (B -> C), return the corresponding basic map
13429 __isl_give isl_basic_map
*isl_basic_map_uncurry(__isl_take isl_basic_map
*bmap
)
13435 if (!isl_basic_map_can_uncurry(bmap
))
13436 isl_die(bmap
->ctx
, isl_error_invalid
,
13437 "basic map cannot be uncurried",
13438 return isl_basic_map_free(bmap
));
13439 bmap
= isl_basic_map_cow(bmap
);
13442 bmap
->dim
= isl_space_uncurry(bmap
->dim
);
13444 return isl_basic_map_free(bmap
);
13445 bmap
= isl_basic_map_mark_final(bmap
);
13449 /* Given a map A -> (B -> C), return the corresponding map
13452 __isl_give isl_map
*isl_map_uncurry(__isl_take isl_map
*map
)
13454 return isl_map_change_space(map
, &isl_map_can_uncurry
,
13455 "map cannot be uncurried", &isl_space_uncurry
);
13458 __isl_give isl_set
*isl_set_equate(__isl_take isl_set
*set
,
13459 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13461 return isl_map_equate(set
, type1
, pos1
, type2
, pos2
);
13464 /* Construct a basic map where the given dimensions are equal to each other.
13466 static __isl_give isl_basic_map
*equator(__isl_take isl_space
*space
,
13467 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13469 isl_basic_map
*bmap
= NULL
;
13473 total
= isl_space_dim(space
, isl_dim_all
);
13475 isl_space_check_range(space
, type1
, pos1
, 1) < 0 ||
13476 isl_space_check_range(space
, type2
, pos2
, 1) < 0)
13479 if (type1
== type2
&& pos1
== pos2
)
13480 return isl_basic_map_universe(space
);
13482 bmap
= isl_basic_map_alloc_space(isl_space_copy(space
), 0, 1, 0);
13483 i
= isl_basic_map_alloc_equality(bmap
);
13486 isl_seq_clr(bmap
->eq
[i
], 1 + total
);
13487 pos1
+= isl_basic_map_offset(bmap
, type1
);
13488 pos2
+= isl_basic_map_offset(bmap
, type2
);
13489 isl_int_set_si(bmap
->eq
[i
][pos1
], -1);
13490 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
13491 bmap
= isl_basic_map_finalize(bmap
);
13492 isl_space_free(space
);
13495 isl_space_free(space
);
13496 isl_basic_map_free(bmap
);
13500 /* Add a constraint imposing that the given two dimensions are equal.
13502 __isl_give isl_basic_map
*isl_basic_map_equate(__isl_take isl_basic_map
*bmap
,
13503 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13507 eq
= equator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
13509 bmap
= isl_basic_map_intersect(bmap
, eq
);
13514 /* Add a constraint imposing that the given two dimensions are equal.
13516 __isl_give isl_map
*isl_map_equate(__isl_take isl_map
*map
,
13517 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13519 isl_basic_map
*bmap
;
13521 bmap
= equator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
13523 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
13528 /* Add a constraint imposing that the given two dimensions have opposite values.
13530 __isl_give isl_map
*isl_map_oppose(__isl_take isl_map
*map
,
13531 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13533 isl_basic_map
*bmap
= NULL
;
13537 if (isl_map_check_range(map
, type1
, pos1
, 1) < 0)
13538 return isl_map_free(map
);
13539 if (isl_map_check_range(map
, type2
, pos2
, 1) < 0)
13540 return isl_map_free(map
);
13542 total
= isl_map_dim(map
, isl_dim_all
);
13544 return isl_map_free(map
);
13545 bmap
= isl_basic_map_alloc_space(isl_map_get_space(map
), 0, 1, 0);
13546 i
= isl_basic_map_alloc_equality(bmap
);
13549 isl_seq_clr(bmap
->eq
[i
], 1 + total
);
13550 pos1
+= isl_basic_map_offset(bmap
, type1
);
13551 pos2
+= isl_basic_map_offset(bmap
, type2
);
13552 isl_int_set_si(bmap
->eq
[i
][pos1
], 1);
13553 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
13554 bmap
= isl_basic_map_finalize(bmap
);
13556 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
13560 isl_basic_map_free(bmap
);
13565 /* Construct a constraint imposing that the value of the first dimension is
13566 * greater than or equal to that of the second.
13568 static __isl_give isl_constraint
*constraint_order_ge(
13569 __isl_take isl_space
*space
, enum isl_dim_type type1
, int pos1
,
13570 enum isl_dim_type type2
, int pos2
)
13574 if (isl_space_check_range(space
, type1
, pos1
, 1) < 0 ||
13575 isl_space_check_range(space
, type2
, pos2
, 1) < 0)
13576 space
= isl_space_free(space
);
13580 c
= isl_constraint_alloc_inequality(isl_local_space_from_space(space
));
13582 if (type1
== type2
&& pos1
== pos2
)
13585 c
= isl_constraint_set_coefficient_si(c
, type1
, pos1
, 1);
13586 c
= isl_constraint_set_coefficient_si(c
, type2
, pos2
, -1);
13591 /* Add a constraint imposing that the value of the first dimension is
13592 * greater than or equal to that of the second.
13594 __isl_give isl_basic_map
*isl_basic_map_order_ge(__isl_take isl_basic_map
*bmap
,
13595 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13600 if (type1
== type2
&& pos1
== pos2
)
13602 space
= isl_basic_map_get_space(bmap
);
13603 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
13604 bmap
= isl_basic_map_add_constraint(bmap
, c
);
13609 /* Add a constraint imposing that the value of the first dimension is
13610 * greater than or equal to that of the second.
13612 __isl_give isl_map
*isl_map_order_ge(__isl_take isl_map
*map
,
13613 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13618 if (type1
== type2
&& pos1
== pos2
)
13620 space
= isl_map_get_space(map
);
13621 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
13622 map
= isl_map_add_constraint(map
, c
);
13627 /* Add a constraint imposing that the value of the first dimension is
13628 * less than or equal to that of the second.
13630 __isl_give isl_map
*isl_map_order_le(__isl_take isl_map
*map
,
13631 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13633 return isl_map_order_ge(map
, type2
, pos2
, type1
, pos1
);
13636 /* Construct a basic map where the value of the first dimension is
13637 * greater than that of the second.
13639 static __isl_give isl_basic_map
*greator(__isl_take isl_space
*space
,
13640 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13642 isl_basic_map
*bmap
= NULL
;
13646 if (isl_space_check_range(space
, type1
, pos1
, 1) < 0 ||
13647 isl_space_check_range(space
, type2
, pos2
, 1) < 0)
13650 if (type1
== type2
&& pos1
== pos2
)
13651 return isl_basic_map_empty(space
);
13653 bmap
= isl_basic_map_alloc_space(space
, 0, 0, 1);
13654 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
13655 i
= isl_basic_map_alloc_inequality(bmap
);
13656 if (total
< 0 || i
< 0)
13657 return isl_basic_map_free(bmap
);
13658 isl_seq_clr(bmap
->ineq
[i
], 1 + total
);
13659 pos1
+= isl_basic_map_offset(bmap
, type1
);
13660 pos2
+= isl_basic_map_offset(bmap
, type2
);
13661 isl_int_set_si(bmap
->ineq
[i
][pos1
], 1);
13662 isl_int_set_si(bmap
->ineq
[i
][pos2
], -1);
13663 isl_int_set_si(bmap
->ineq
[i
][0], -1);
13664 bmap
= isl_basic_map_finalize(bmap
);
13668 isl_space_free(space
);
13669 isl_basic_map_free(bmap
);
13673 /* Add a constraint imposing that the value of the first dimension is
13674 * greater than that of the second.
13676 __isl_give isl_basic_map
*isl_basic_map_order_gt(__isl_take isl_basic_map
*bmap
,
13677 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13681 gt
= greator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
13683 bmap
= isl_basic_map_intersect(bmap
, gt
);
13688 /* Add a constraint imposing that the value of the first dimension is
13689 * greater than that of the second.
13691 __isl_give isl_map
*isl_map_order_gt(__isl_take isl_map
*map
,
13692 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13694 isl_basic_map
*bmap
;
13696 bmap
= greator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
13698 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
13703 /* Add a constraint imposing that the value of the first dimension is
13704 * smaller than that of the second.
13706 __isl_give isl_map
*isl_map_order_lt(__isl_take isl_map
*map
,
13707 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13709 return isl_map_order_gt(map
, type2
, pos2
, type1
, pos1
);
13712 __isl_give isl_aff
*isl_basic_map_get_div(__isl_keep isl_basic_map
*bmap
,
13716 isl_local_space
*ls
;
13721 if (!isl_basic_map_divs_known(bmap
))
13722 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
13723 "some divs are unknown", return NULL
);
13725 ls
= isl_basic_map_get_local_space(bmap
);
13726 div
= isl_local_space_get_div(ls
, pos
);
13727 isl_local_space_free(ls
);
13732 __isl_give isl_aff
*isl_basic_set_get_div(__isl_keep isl_basic_set
*bset
,
13735 return isl_basic_map_get_div(bset
, pos
);
13738 /* Plug in "subs" for set dimension "pos" of "set".
13740 __isl_give isl_set
*isl_set_substitute(__isl_take isl_set
*set
,
13741 unsigned pos
, __isl_keep isl_aff
*subs
)
13745 if (set
&& isl_set_plain_is_empty(set
))
13748 ma
= isl_multi_aff_identity_on_domain_space(isl_set_get_space(set
));
13749 ma
= isl_multi_aff_set_aff(ma
, pos
, isl_aff_copy(subs
));
13750 return isl_set_preimage_multi_aff(set
, ma
);
13753 /* Check if the range of "ma" is compatible with the domain or range
13754 * (depending on "type") of "bmap".
13756 static isl_stat
check_basic_map_compatible_range_multi_aff(
13757 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type type
,
13758 __isl_keep isl_multi_aff
*ma
)
13761 isl_space
*ma_space
;
13763 ma_space
= isl_multi_aff_get_space(ma
);
13765 m
= isl_space_has_equal_params(bmap
->dim
, ma_space
);
13769 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
13770 "parameters don't match", goto error
);
13771 m
= isl_space_tuple_is_equal(bmap
->dim
, type
, ma_space
, isl_dim_out
);
13775 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
13776 "spaces don't match", goto error
);
13778 isl_space_free(ma_space
);
13779 return isl_stat_ok
;
13781 isl_space_free(ma_space
);
13782 return isl_stat_error
;
13785 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
13786 * coefficients before the transformed range of dimensions,
13787 * the "n_after" coefficients after the transformed range of dimensions
13788 * and the coefficients of the other divs in "bmap".
13790 static __isl_give isl_basic_map
*set_ma_divs(__isl_take isl_basic_map
*bmap
,
13791 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
, int n_div
)
13796 isl_local_space
*ls
;
13801 ls
= isl_aff_get_domain_local_space(ma
->u
.p
[0]);
13802 n_param
= isl_local_space_dim(ls
, isl_dim_param
);
13803 n_set
= isl_local_space_dim(ls
, isl_dim_set
);
13804 if (n_param
< 0 || n_set
< 0)
13805 return isl_basic_map_free(bmap
);
13807 for (i
= 0; i
< n_div
; ++i
) {
13808 int o_bmap
= 0, o_ls
= 0;
13810 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], 1 + 1 + n_param
);
13811 o_bmap
+= 1 + 1 + n_param
;
13812 o_ls
+= 1 + 1 + n_param
;
13813 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_before
);
13814 o_bmap
+= n_before
;
13815 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
13816 ls
->div
->row
[i
] + o_ls
, n_set
);
13819 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_after
);
13821 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
13822 ls
->div
->row
[i
] + o_ls
, n_div
);
13825 isl_seq_clr(bmap
->div
[i
] + o_bmap
, bmap
->n_div
- n_div
);
13826 bmap
= isl_basic_map_add_div_constraints(bmap
, i
);
13831 isl_local_space_free(ls
);
13834 isl_local_space_free(ls
);
13835 return isl_basic_map_free(bmap
);
13838 /* How many stride constraints does "ma" enforce?
13839 * That is, how many of the affine expressions have a denominator
13840 * different from one?
13842 static int multi_aff_strides(__isl_keep isl_multi_aff
*ma
)
13847 for (i
= 0; i
< ma
->n
; ++i
)
13848 if (!isl_int_is_one(ma
->u
.p
[i
]->v
->el
[0]))
13854 /* For each affine expression in ma of the form
13856 * x_i = (f_i y + h_i)/m_i
13858 * with m_i different from one, add a constraint to "bmap"
13861 * f_i y + h_i = m_i alpha_i
13863 * with alpha_i an additional existentially quantified variable.
13865 * The input variables of "ma" correspond to a subset of the variables
13866 * of "bmap". There are "n_before" variables in "bmap" before this
13867 * subset and "n_after" variables after this subset.
13868 * The integer divisions of the affine expressions in "ma" are assumed
13869 * to have been aligned. There are "n_div_ma" of them and
13870 * they appear first in "bmap", straight after the "n_after" variables.
13872 static __isl_give isl_basic_map
*add_ma_strides(
13873 __isl_take isl_basic_map
*bmap
, __isl_keep isl_multi_aff
*ma
,
13874 int n_before
, int n_after
, int n_div_ma
)
13882 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
13883 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
13884 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
13885 if (total
< 0 || n_param
< 0 || n_in
< 0)
13886 return isl_basic_map_free(bmap
);
13887 for (i
= 0; i
< ma
->n
; ++i
) {
13888 int o_bmap
= 0, o_ma
= 1;
13890 if (isl_int_is_one(ma
->u
.p
[i
]->v
->el
[0]))
13892 div
= isl_basic_map_alloc_div(bmap
);
13893 k
= isl_basic_map_alloc_equality(bmap
);
13894 if (div
< 0 || k
< 0)
13896 isl_int_set_si(bmap
->div
[div
][0], 0);
13897 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
13898 ma
->u
.p
[i
]->v
->el
+ o_ma
, 1 + n_param
);
13899 o_bmap
+= 1 + n_param
;
13900 o_ma
+= 1 + n_param
;
13901 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_before
);
13902 o_bmap
+= n_before
;
13903 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
13904 ma
->u
.p
[i
]->v
->el
+ o_ma
, n_in
);
13907 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_after
);
13909 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
13910 ma
->u
.p
[i
]->v
->el
+ o_ma
, n_div_ma
);
13911 o_bmap
+= n_div_ma
;
13913 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, 1 + total
- o_bmap
);
13914 isl_int_neg(bmap
->eq
[k
][1 + total
], ma
->u
.p
[i
]->v
->el
[0]);
13920 isl_basic_map_free(bmap
);
13924 /* Replace the domain or range space (depending on "type) of "space" by "set".
13926 static __isl_give isl_space
*isl_space_set(__isl_take isl_space
*space
,
13927 enum isl_dim_type type
, __isl_take isl_space
*set
)
13929 if (type
== isl_dim_in
) {
13930 space
= isl_space_range(space
);
13931 space
= isl_space_map_from_domain_and_range(set
, space
);
13933 space
= isl_space_domain(space
);
13934 space
= isl_space_map_from_domain_and_range(space
, set
);
13940 /* Compute the preimage of the domain or range (depending on "type")
13941 * of "bmap" under the function represented by "ma".
13942 * In other words, plug in "ma" in the domain or range of "bmap".
13943 * The result is a basic map that lives in the same space as "bmap"
13944 * except that the domain or range has been replaced by
13945 * the domain space of "ma".
13947 * If bmap is represented by
13949 * A(p) + S u + B x + T v + C(divs) >= 0,
13951 * where u and x are input and output dimensions if type == isl_dim_out
13952 * while x and v are input and output dimensions if type == isl_dim_in,
13953 * and ma is represented by
13955 * x = D(p) + F(y) + G(divs')
13957 * then the result is
13959 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
13961 * The divs in the input set are similarly adjusted.
13964 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
13968 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
13969 * B_i G(divs') + c_i(divs))/n_i)
13971 * If bmap is not a rational map and if F(y) involves any denominators
13973 * x_i = (f_i y + h_i)/m_i
13975 * then additional constraints are added to ensure that we only
13976 * map back integer points. That is we enforce
13978 * f_i y + h_i = m_i alpha_i
13980 * with alpha_i an additional existentially quantified variable.
13982 * We first copy over the divs from "ma".
13983 * Then we add the modified constraints and divs from "bmap".
13984 * Finally, we add the stride constraints, if needed.
13986 __isl_give isl_basic_map
*isl_basic_map_preimage_multi_aff(
13987 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
13988 __isl_take isl_multi_aff
*ma
)
13992 isl_basic_map
*res
= NULL
;
13993 isl_size n_before
, n_after
, n_div_bmap
, n_div_ma
;
13994 isl_int f
, c1
, c2
, g
;
14003 ma
= isl_multi_aff_align_divs(ma
);
14006 if (check_basic_map_compatible_range_multi_aff(bmap
, type
, ma
) < 0)
14009 if (type
== isl_dim_in
) {
14011 n_after
= isl_basic_map_dim(bmap
, isl_dim_out
);
14013 n_before
= isl_basic_map_dim(bmap
, isl_dim_in
);
14016 n_div_bmap
= isl_basic_map_dim(bmap
, isl_dim_div
);
14017 n_div_ma
= ma
->n
? isl_aff_dim(ma
->u
.p
[0], isl_dim_div
) : 0;
14018 if (n_before
< 0 || n_after
< 0 || n_div_bmap
< 0 || n_div_ma
< 0)
14021 space
= isl_multi_aff_get_domain_space(ma
);
14022 space
= isl_space_set(isl_basic_map_get_space(bmap
), type
, space
);
14023 rational
= isl_basic_map_is_rational(bmap
);
14024 strides
= rational
? 0 : multi_aff_strides(ma
);
14025 res
= isl_basic_map_alloc_space(space
, n_div_ma
+ n_div_bmap
+ strides
,
14026 bmap
->n_eq
+ strides
, bmap
->n_ineq
+ 2 * n_div_ma
);
14028 res
= isl_basic_map_set_rational(res
);
14030 for (i
= 0; i
< n_div_ma
+ n_div_bmap
; ++i
)
14031 if (isl_basic_map_alloc_div(res
) < 0)
14034 res
= set_ma_divs(res
, ma
, n_before
, n_after
, n_div_ma
);
14038 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
14039 k
= isl_basic_map_alloc_equality(res
);
14042 if (isl_seq_preimage(res
->eq
[k
], bmap
->eq
[i
], ma
, n_before
,
14043 n_after
, n_div_ma
, n_div_bmap
,
14044 f
, c1
, c2
, g
, 0) < 0)
14048 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
14049 k
= isl_basic_map_alloc_inequality(res
);
14052 if (isl_seq_preimage(res
->ineq
[k
], bmap
->ineq
[i
], ma
, n_before
,
14053 n_after
, n_div_ma
, n_div_bmap
,
14054 f
, c1
, c2
, g
, 0) < 0)
14058 for (i
= 0; i
< bmap
->n_div
; ++i
) {
14059 if (isl_int_is_zero(bmap
->div
[i
][0])) {
14060 isl_int_set_si(res
->div
[n_div_ma
+ i
][0], 0);
14063 if (isl_seq_preimage(res
->div
[n_div_ma
+ i
], bmap
->div
[i
], ma
,
14064 n_before
, n_after
, n_div_ma
, n_div_bmap
,
14065 f
, c1
, c2
, g
, 1) < 0)
14070 res
= add_ma_strides(res
, ma
, n_before
, n_after
, n_div_ma
);
14076 isl_basic_map_free(bmap
);
14077 isl_multi_aff_free(ma
);
14078 res
= isl_basic_map_simplify(res
);
14079 return isl_basic_map_finalize(res
);
14085 isl_basic_map_free(bmap
);
14086 isl_multi_aff_free(ma
);
14087 isl_basic_map_free(res
);
14091 /* Compute the preimage of "bset" under the function represented by "ma".
14092 * In other words, plug in "ma" in "bset". The result is a basic set
14093 * that lives in the domain space of "ma".
14095 __isl_give isl_basic_set
*isl_basic_set_preimage_multi_aff(
14096 __isl_take isl_basic_set
*bset
, __isl_take isl_multi_aff
*ma
)
14098 return isl_basic_map_preimage_multi_aff(bset
, isl_dim_set
, ma
);
14101 /* Compute the preimage of the domain of "bmap" under the function
14102 * represented by "ma".
14103 * In other words, plug in "ma" in the domain of "bmap".
14104 * The result is a basic map that lives in the same space as "bmap"
14105 * except that the domain has been replaced by the domain space of "ma".
14107 __isl_give isl_basic_map
*isl_basic_map_preimage_domain_multi_aff(
14108 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
14110 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_in
, ma
);
14113 /* Compute the preimage of the range of "bmap" under the function
14114 * represented by "ma".
14115 * In other words, plug in "ma" in the range of "bmap".
14116 * The result is a basic map that lives in the same space as "bmap"
14117 * except that the range has been replaced by the domain space of "ma".
14119 __isl_give isl_basic_map
*isl_basic_map_preimage_range_multi_aff(
14120 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
14122 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_out
, ma
);
14125 /* Check if the range of "ma" is compatible with the domain or range
14126 * (depending on "type") of "map".
14127 * Return isl_stat_error if anything is wrong.
14129 static isl_stat
check_map_compatible_range_multi_aff(
14130 __isl_keep isl_map
*map
, enum isl_dim_type type
,
14131 __isl_keep isl_multi_aff
*ma
)
14134 isl_space
*ma_space
;
14136 ma_space
= isl_multi_aff_get_space(ma
);
14137 m
= isl_map_space_tuple_is_equal(map
, type
, ma_space
, isl_dim_out
);
14138 isl_space_free(ma_space
);
14140 return isl_stat_error
;
14142 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
14143 "spaces don't match", return isl_stat_error
);
14144 return isl_stat_ok
;
14147 /* Compute the preimage of the domain or range (depending on "type")
14148 * of "map" under the function represented by "ma".
14149 * In other words, plug in "ma" in the domain or range of "map".
14150 * The result is a map that lives in the same space as "map"
14151 * except that the domain or range has been replaced by
14152 * the domain space of "ma".
14154 * The parameters are assumed to have been aligned.
14156 static __isl_give isl_map
*map_preimage_multi_aff(__isl_take isl_map
*map
,
14157 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
14162 map
= isl_map_cow(map
);
14163 ma
= isl_multi_aff_align_divs(ma
);
14166 if (check_map_compatible_range_multi_aff(map
, type
, ma
) < 0)
14169 for (i
= 0; i
< map
->n
; ++i
) {
14170 map
->p
[i
] = isl_basic_map_preimage_multi_aff(map
->p
[i
], type
,
14171 isl_multi_aff_copy(ma
));
14176 space
= isl_multi_aff_get_domain_space(ma
);
14177 space
= isl_space_set(isl_map_get_space(map
), type
, space
);
14179 isl_space_free(isl_map_take_space(map
));
14180 map
= isl_map_restore_space(map
, space
);
14184 isl_multi_aff_free(ma
);
14186 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
14187 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
14190 isl_multi_aff_free(ma
);
14195 /* Compute the preimage of the domain or range (depending on "type")
14196 * of "map" under the function represented by "ma".
14197 * In other words, plug in "ma" in the domain or range of "map".
14198 * The result is a map that lives in the same space as "map"
14199 * except that the domain or range has been replaced by
14200 * the domain space of "ma".
14202 __isl_give isl_map
*isl_map_preimage_multi_aff(__isl_take isl_map
*map
,
14203 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
14210 aligned
= isl_map_space_has_equal_params(map
, ma
->space
);
14214 return map_preimage_multi_aff(map
, type
, ma
);
14216 if (isl_map_check_named_params(map
) < 0)
14218 if (!isl_space_has_named_params(ma
->space
))
14219 isl_die(map
->ctx
, isl_error_invalid
,
14220 "unaligned unnamed parameters", goto error
);
14221 map
= isl_map_align_params(map
, isl_multi_aff_get_space(ma
));
14222 ma
= isl_multi_aff_align_params(ma
, isl_map_get_space(map
));
14224 return map_preimage_multi_aff(map
, type
, ma
);
14226 isl_multi_aff_free(ma
);
14227 return isl_map_free(map
);
14230 /* Compute the preimage of "set" under the function represented by "ma".
14231 * In other words, plug in "ma" in "set". The result is a set
14232 * that lives in the domain space of "ma".
14234 __isl_give isl_set
*isl_set_preimage_multi_aff(__isl_take isl_set
*set
,
14235 __isl_take isl_multi_aff
*ma
)
14237 return isl_map_preimage_multi_aff(set
, isl_dim_set
, ma
);
14240 /* Compute the preimage of the domain of "map" under the function
14241 * represented by "ma".
14242 * In other words, plug in "ma" in the domain of "map".
14243 * The result is a map that lives in the same space as "map"
14244 * except that the domain has been replaced by the domain space of "ma".
14246 __isl_give isl_map
*isl_map_preimage_domain_multi_aff(__isl_take isl_map
*map
,
14247 __isl_take isl_multi_aff
*ma
)
14249 return isl_map_preimage_multi_aff(map
, isl_dim_in
, ma
);
14252 /* Compute the preimage of the range of "map" under the function
14253 * represented by "ma".
14254 * In other words, plug in "ma" in the range of "map".
14255 * The result is a map that lives in the same space as "map"
14256 * except that the range has been replaced by the domain space of "ma".
14258 __isl_give isl_map
*isl_map_preimage_range_multi_aff(__isl_take isl_map
*map
,
14259 __isl_take isl_multi_aff
*ma
)
14261 return isl_map_preimage_multi_aff(map
, isl_dim_out
, ma
);
14264 /* Compute the preimage of "map" under the function represented by "pma".
14265 * In other words, plug in "pma" in the domain or range of "map".
14266 * The result is a map that lives in the same space as "map",
14267 * except that the space of type "type" has been replaced by
14268 * the domain space of "pma".
14270 * The parameters of "map" and "pma" are assumed to have been aligned.
14272 static __isl_give isl_map
*isl_map_preimage_pw_multi_aff_aligned(
14273 __isl_take isl_map
*map
, enum isl_dim_type type
,
14274 __isl_take isl_pw_multi_aff
*pma
)
14285 space
= isl_pw_multi_aff_get_domain_space(pma
);
14286 isl_pw_multi_aff_free(pma
);
14287 space
= isl_space_set(isl_map_get_space(map
), type
, space
);
14289 return isl_map_empty(space
);
14292 res
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
14293 isl_multi_aff_copy(pma
->p
[0].maff
));
14294 if (type
== isl_dim_in
)
14295 res
= isl_map_intersect_domain(res
,
14296 isl_map_copy(pma
->p
[0].set
));
14298 res
= isl_map_intersect_range(res
,
14299 isl_map_copy(pma
->p
[0].set
));
14301 for (i
= 1; i
< pma
->n
; ++i
) {
14304 res_i
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
14305 isl_multi_aff_copy(pma
->p
[i
].maff
));
14306 if (type
== isl_dim_in
)
14307 res_i
= isl_map_intersect_domain(res_i
,
14308 isl_map_copy(pma
->p
[i
].set
));
14310 res_i
= isl_map_intersect_range(res_i
,
14311 isl_map_copy(pma
->p
[i
].set
));
14312 res
= isl_map_union(res
, res_i
);
14315 isl_pw_multi_aff_free(pma
);
14319 isl_pw_multi_aff_free(pma
);
14324 /* Compute the preimage of "map" under the function represented by "pma".
14325 * In other words, plug in "pma" in the domain or range of "map".
14326 * The result is a map that lives in the same space as "map",
14327 * except that the space of type "type" has been replaced by
14328 * the domain space of "pma".
14330 __isl_give isl_map
*isl_map_preimage_pw_multi_aff(__isl_take isl_map
*map
,
14331 enum isl_dim_type type
, __isl_take isl_pw_multi_aff
*pma
)
14338 aligned
= isl_map_space_has_equal_params(map
, pma
->dim
);
14342 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
14344 if (isl_map_check_named_params(map
) < 0)
14346 if (isl_pw_multi_aff_check_named_params(pma
) < 0)
14348 map
= isl_map_align_params(map
, isl_pw_multi_aff_get_space(pma
));
14349 pma
= isl_pw_multi_aff_align_params(pma
, isl_map_get_space(map
));
14351 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
14353 isl_pw_multi_aff_free(pma
);
14354 return isl_map_free(map
);
14357 /* Compute the preimage of "set" under the function represented by "pma".
14358 * In other words, plug in "pma" in "set". The result is a set
14359 * that lives in the domain space of "pma".
14361 __isl_give isl_set
*isl_set_preimage_pw_multi_aff(__isl_take isl_set
*set
,
14362 __isl_take isl_pw_multi_aff
*pma
)
14364 return isl_map_preimage_pw_multi_aff(set
, isl_dim_set
, pma
);
14367 /* Compute the preimage of the domain of "map" under the function
14368 * represented by "pma".
14369 * In other words, plug in "pma" in the domain of "map".
14370 * The result is a map that lives in the same space as "map",
14371 * except that domain space has been replaced by the domain space of "pma".
14373 __isl_give isl_map
*isl_map_preimage_domain_pw_multi_aff(
14374 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
14376 return isl_map_preimage_pw_multi_aff(map
, isl_dim_in
, pma
);
14379 /* Compute the preimage of the range of "map" under the function
14380 * represented by "pma".
14381 * In other words, plug in "pma" in the range of "map".
14382 * The result is a map that lives in the same space as "map",
14383 * except that range space has been replaced by the domain space of "pma".
14385 __isl_give isl_map
*isl_map_preimage_range_pw_multi_aff(
14386 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
14388 return isl_map_preimage_pw_multi_aff(map
, isl_dim_out
, pma
);
14391 /* Compute the preimage of "map" under the function represented by "mpa".
14392 * In other words, plug in "mpa" in the domain or range of "map".
14393 * The result is a map that lives in the same space as "map",
14394 * except that the space of type "type" has been replaced by
14395 * the domain space of "mpa".
14397 * If the map does not involve any constraints that refer to the
14398 * dimensions of the substituted space, then the only possible
14399 * effect of "mpa" on the map is to map the space to a different space.
14400 * We create a separate isl_multi_aff to effectuate this change
14401 * in order to avoid spurious splitting of the map along the pieces
14403 * If "mpa" has a non-trivial explicit domain, however,
14404 * then the full substitution should be performed.
14406 __isl_give isl_map
*isl_map_preimage_multi_pw_aff(__isl_take isl_map
*map
,
14407 enum isl_dim_type type
, __isl_take isl_multi_pw_aff
*mpa
)
14411 isl_pw_multi_aff
*pma
;
14413 n
= isl_map_dim(map
, type
);
14417 full
= isl_map_involves_dims(map
, type
, 0, n
);
14418 if (full
>= 0 && !full
)
14419 full
= isl_multi_pw_aff_has_non_trivial_domain(mpa
);
14426 space
= isl_multi_pw_aff_get_space(mpa
);
14427 isl_multi_pw_aff_free(mpa
);
14428 ma
= isl_multi_aff_zero(space
);
14429 return isl_map_preimage_multi_aff(map
, type
, ma
);
14432 pma
= isl_pw_multi_aff_from_multi_pw_aff(mpa
);
14433 return isl_map_preimage_pw_multi_aff(map
, type
, pma
);
14436 isl_multi_pw_aff_free(mpa
);
14440 /* Compute the preimage of "map" under the function represented by "mpa".
14441 * In other words, plug in "mpa" in the domain "map".
14442 * The result is a map that lives in the same space as "map",
14443 * except that domain space has been replaced by the domain space of "mpa".
14445 __isl_give isl_map
*isl_map_preimage_domain_multi_pw_aff(
14446 __isl_take isl_map
*map
, __isl_take isl_multi_pw_aff
*mpa
)
14448 return isl_map_preimage_multi_pw_aff(map
, isl_dim_in
, mpa
);
14451 /* Compute the preimage of "set" by the function represented by "mpa".
14452 * In other words, plug in "mpa" in "set".
14454 __isl_give isl_set
*isl_set_preimage_multi_pw_aff(__isl_take isl_set
*set
,
14455 __isl_take isl_multi_pw_aff
*mpa
)
14457 return isl_map_preimage_multi_pw_aff(set
, isl_dim_set
, mpa
);
14460 /* Given that inequality "ineq" of "bmap" expresses an upper bound
14461 * on the output dimension "pos" in terms of the parameters,
14462 * the input dimensions and possibly some integer divisions,
14463 * but not any other output dimensions, extract this upper bound
14464 * as a function of all dimensions (with zero coefficients
14465 * for the output dimensions).
14467 * That is, the inequality is of the form
14469 * e(...) + c - m x >= 0
14471 * where e does not depend on any other output dimensions.
14472 * Return (e(...) + c) / m, with the denominator m in the first position.
14474 __isl_give isl_vec
*isl_basic_map_inequality_extract_output_upper_bound(
14475 __isl_keep isl_basic_map
*bmap
, int ineq
, int pos
)
14478 isl_size v_out
, total
;
14480 v_out
= isl_basic_map_var_offset(bmap
, isl_dim_out
);
14481 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
14482 if (v_out
< 0 || total
< 0)
14484 ctx
= isl_basic_map_get_ctx(bmap
);
14485 return extract_bound_from_constraint(ctx
, bmap
->ineq
[ineq
],
14486 total
, v_out
+ pos
);
14489 /* Is constraint "c" of "bmap" of the form
14491 * e(...) + c1 - m x >= 0
14495 * -e(...) + c2 + m x >= 0
14497 * where m > 1 and e does not involve any other output variables?
14499 * "v_out" is the offset to the output variables.
14500 * "d" is the position of x among the output variables.
14501 * "v_div" is the offset to the local variables.
14502 * "total" is the total number of variables.
14504 * Since the purpose of this function is to use the constraint
14505 * to express the output variable as an integer division,
14506 * do not allow the constraint to involve any local variables
14507 * that do not have an explicit representation.
14509 static isl_bool
is_potential_div_constraint(__isl_keep isl_basic_map
*bmap
,
14510 isl_int
*c
, int v_out
, int d
, int v_div
, int total
)
14514 if (isl_int_is_zero(c
[1 + v_out
+ d
]))
14515 return isl_bool_false
;
14516 if (isl_int_is_one(c
[1 + v_out
+ d
]))
14517 return isl_bool_false
;
14518 if (isl_int_is_negone(c
[1 + v_out
+ d
]))
14519 return isl_bool_false
;
14520 if (isl_seq_first_non_zero(c
+ 1 + v_out
, d
) != -1)
14521 return isl_bool_false
;
14522 if (isl_seq_first_non_zero(c
+ 1 + v_out
+ d
+ 1,
14523 v_div
- (v_out
+ d
+ 1)) != -1)
14524 return isl_bool_false
;
14525 for (i
= 0; v_div
+ i
< total
; ++i
) {
14526 isl_bool known
, involves
;
14528 if (isl_int_is_zero(c
[1 + v_div
+ i
]))
14530 known
= isl_basic_map_div_is_known(bmap
, i
);
14531 if (known
< 0 || !known
)
14533 involves
= div_involves_vars(bmap
, i
, v_out
, v_div
- v_out
);
14534 if (involves
< 0 || involves
)
14535 return isl_bool_not(involves
);
14537 return isl_bool_true
;
14540 /* Look for a pair of constraints
14542 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
14546 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
14548 * that express that the output dimension x at position "pos"
14549 * is some integer division of an expression in terms of the parameters,
14550 * input dimensions and integer divisions.
14551 * If such a pair can be found, then return the index
14552 * of the upper bound constraint, m x <= e(...) + c1.
14553 * Otherwise, return an index beyond the number of constraints.
14555 * In order for the constraints above to express an integer division,
14556 * m needs to be greater than 1 and such that
14558 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
14560 * In particular, this ensures that
14562 * x = floor((e(...) + c1) / m)
14564 isl_size
isl_basic_map_find_output_upper_div_constraint(
14565 __isl_keep isl_basic_map
*bmap
, int pos
)
14569 isl_size v_out
, v_div
;
14573 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
14574 v_out
= isl_basic_map_var_offset(bmap
, isl_dim_out
);
14575 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
14576 n_ineq
= isl_basic_map_n_inequality(bmap
);
14577 if (total
< 0 || v_out
< 0 || v_div
< 0 || n_ineq
< 0)
14578 return isl_size_error
;
14581 for (i
= 0; i
< n_ineq
; ++i
) {
14582 isl_bool potential
;
14584 potential
= is_potential_div_constraint(bmap
, bmap
->ineq
[i
],
14585 v_out
, pos
, v_div
, total
);
14590 for (j
= i
+ 1; j
< n_ineq
; ++j
) {
14591 if (!is_constraint_pair(bmap
, i
, j
, total
))
14593 if (constraint_pair_has_bound(bmap
, i
, j
,
14594 bmap
->ineq
[i
][1 + v_out
+ pos
], &sum
))
14600 isl_int_clear(sum
);
14604 if (isl_int_is_pos(bmap
->ineq
[j
][1 + v_out
+ pos
]))
14609 isl_int_clear(sum
);
14610 return isl_size_error
;
14613 /* Return a copy of the equality constraints of "bset" as a matrix.
14615 __isl_give isl_mat
*isl_basic_set_extract_equalities(
14616 __isl_keep isl_basic_set
*bset
)
14621 total
= isl_basic_set_dim(bset
, isl_dim_all
);
14625 ctx
= isl_basic_set_get_ctx(bset
);
14626 return isl_mat_sub_alloc6(ctx
, bset
->eq
, 0, bset
->n_eq
, 0, 1 + total
);
14629 /* Are the "n" "coefficients" starting at "first" of the integer division
14630 * expressions at position "pos1" in "bmap1" and "pos2" in "bmap2" equal
14632 * The "coefficient" at position 0 is the denominator.
14633 * The "coefficient" at position 1 is the constant term.
14635 isl_bool
isl_basic_map_equal_div_expr_part(__isl_keep isl_basic_map
*bmap1
,
14636 int pos1
, __isl_keep isl_basic_map
*bmap2
, int pos2
,
14637 unsigned first
, unsigned n
)
14639 if (isl_basic_map_check_range(bmap1
, isl_dim_div
, pos1
, 1) < 0)
14640 return isl_bool_error
;
14641 if (isl_basic_map_check_range(bmap2
, isl_dim_div
, pos2
, 1) < 0)
14642 return isl_bool_error
;
14643 return isl_seq_eq(bmap1
->div
[pos1
] + first
,
14644 bmap2
->div
[pos2
] + first
, n
);
14647 /* Are the integer division expressions at position "pos1" in "bmap1" and
14648 * "pos2" in "bmap2" equal to each other, except that the constant terms
14651 isl_bool
isl_basic_map_equal_div_expr_except_constant(
14652 __isl_keep isl_basic_map
*bmap1
, int pos1
,
14653 __isl_keep isl_basic_map
*bmap2
, int pos2
)
14656 isl_size total
, total2
;
14658 total
= isl_basic_map_dim(bmap1
, isl_dim_all
);
14659 total2
= isl_basic_map_dim(bmap2
, isl_dim_all
);
14660 if (total
< 0 || total2
< 0)
14661 return isl_bool_error
;
14662 if (total
!= total2
)
14663 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
14664 "incomparable div expressions", return isl_bool_error
);
14665 equal
= isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
14667 if (equal
< 0 || !equal
)
14669 equal
= isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
14671 if (equal
< 0 || equal
)
14672 return isl_bool_not(equal
);
14673 return isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
14677 /* Replace the numerator of the constant term of the integer division
14678 * expression at position "div" in "bmap" by "value".
14679 * The caller guarantees that this does not change the meaning
14682 __isl_give isl_basic_map
*isl_basic_map_set_div_expr_constant_num_si_inplace(
14683 __isl_take isl_basic_map
*bmap
, int div
, int value
)
14685 if (isl_basic_map_check_range(bmap
, isl_dim_div
, div
, 1) < 0)
14686 return isl_basic_map_free(bmap
);
14688 isl_int_set_si(bmap
->div
[div
][1], value
);
14693 /* Is the point "inner" internal to inequality constraint "ineq"
14695 * The point is considered to be internal to the inequality constraint,
14696 * if it strictly lies on the positive side of the inequality constraint,
14697 * or if it lies on the constraint and the constraint is lexico-positive.
14699 static isl_bool
is_internal(__isl_keep isl_vec
*inner
,
14700 __isl_keep isl_basic_set
*bset
, int ineq
)
14706 if (!inner
|| !bset
)
14707 return isl_bool_error
;
14709 ctx
= isl_basic_set_get_ctx(bset
);
14710 isl_seq_inner_product(inner
->el
, bset
->ineq
[ineq
], inner
->size
,
14711 &ctx
->normalize_gcd
);
14712 if (!isl_int_is_zero(ctx
->normalize_gcd
))
14713 return isl_int_is_nonneg(ctx
->normalize_gcd
);
14715 total
= isl_basic_set_dim(bset
, isl_dim_all
);
14717 return isl_bool_error
;
14718 pos
= isl_seq_first_non_zero(bset
->ineq
[ineq
] + 1, total
);
14719 return isl_int_is_pos(bset
->ineq
[ineq
][1 + pos
]);
14722 /* Tighten the inequality constraints of "bset" that are outward with respect
14723 * to the point "vec".
14724 * That is, tighten the constraints that are not satisfied by "vec".
14726 * "vec" is a point internal to some superset S of "bset" that is used
14727 * to make the subsets of S disjoint, by tightening one half of the constraints
14728 * that separate two subsets. In particular, the constraints of S
14729 * are all satisfied by "vec" and should not be tightened.
14730 * Of the internal constraints, those that have "vec" on the outside
14731 * are tightened. The shared facet is included in the adjacent subset
14732 * with the opposite constraint.
14733 * For constraints that saturate "vec", this criterion cannot be used
14734 * to determine which of the two sides should be tightened.
14735 * Instead, the sign of the first non-zero coefficient is used
14736 * to make this choice. Note that this second criterion is never used
14737 * on the constraints of S since "vec" is interior to "S".
14739 __isl_give isl_basic_set
*isl_basic_set_tighten_outward(
14740 __isl_take isl_basic_set
*bset
, __isl_keep isl_vec
*vec
)
14744 bset
= isl_basic_set_cow(bset
);
14747 for (j
= 0; j
< bset
->n_ineq
; ++j
) {
14750 internal
= is_internal(vec
, bset
, j
);
14752 return isl_basic_set_free(bset
);
14755 isl_int_sub_ui(bset
->ineq
[j
][0], bset
->ineq
[j
][0], 1);
14761 /* Replace the variables x of type "type" starting at "first" in "bmap"
14762 * by x' with x = M x' with M the matrix trans.
14763 * That is, replace the corresponding coefficients c by c M.
14765 * The transformation matrix should be a square matrix.
14767 __isl_give isl_basic_map
*isl_basic_map_transform_dims(
14768 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
, unsigned first
,
14769 __isl_take isl_mat
*trans
)
14773 bmap
= isl_basic_map_cow(bmap
);
14774 if (!bmap
|| !trans
)
14777 if (trans
->n_row
!= trans
->n_col
)
14778 isl_die(trans
->ctx
, isl_error_invalid
,
14779 "expecting square transformation matrix", goto error
);
14780 if (isl_basic_map_check_range(bmap
, type
, first
, trans
->n_row
) < 0)
14783 pos
= isl_basic_map_offset(bmap
, type
) + first
;
14785 if (isl_mat_sub_transform(bmap
->eq
, bmap
->n_eq
, pos
,
14786 isl_mat_copy(trans
)) < 0)
14788 if (isl_mat_sub_transform(bmap
->ineq
, bmap
->n_ineq
, pos
,
14789 isl_mat_copy(trans
)) < 0)
14791 if (isl_mat_sub_transform(bmap
->div
, bmap
->n_div
, 1 + pos
,
14792 isl_mat_copy(trans
)) < 0)
14795 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
14796 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
14797 ISL_F_CLR(bmap
, ISL_BASIC_MAP_REDUCED_COEFFICIENTS
);
14799 isl_mat_free(trans
);
14802 isl_mat_free(trans
);
14803 isl_basic_map_free(bmap
);
14807 /* Replace the variables x of type "type" starting at "first" in "bset"
14808 * by x' with x = M x' with M the matrix trans.
14809 * That is, replace the corresponding coefficients c by c M.
14811 * The transformation matrix should be a square matrix.
14813 __isl_give isl_basic_set
*isl_basic_set_transform_dims(
14814 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned first
,
14815 __isl_take isl_mat
*trans
)
14817 return isl_basic_map_transform_dims(bset
, type
, first
, trans
);