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
);
152 return isl_size_error
;
157 case isl_dim_out
: return isl_space_offset(space
, type
);
158 case isl_dim_div
: return isl_space_dim(space
, isl_dim_all
);
161 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
162 "invalid dimension type", return isl_size_error
);
166 /* Return the position of the variables of the given type
167 * within the sequence of variables of "bset".
169 isl_size
isl_basic_set_var_offset(__isl_keep isl_basic_set
*bset
,
170 enum isl_dim_type type
)
172 return isl_basic_map_var_offset(bset_to_bmap(bset
), type
);
175 /* Return the position of the coefficients of the variables of the given type
176 * within the sequence of coefficients of "bmap".
178 unsigned isl_basic_map_offset(__isl_keep isl_basic_map
*bmap
,
179 enum isl_dim_type type
)
182 case isl_dim_cst
: return 0;
186 case isl_dim_div
: return 1 + isl_basic_map_var_offset(bmap
, type
);
191 unsigned isl_basic_set_offset(__isl_keep isl_basic_set
*bset
,
192 enum isl_dim_type type
)
194 return isl_basic_map_offset(bset
, type
);
197 static unsigned map_offset(__isl_keep isl_map
*map
, enum isl_dim_type type
)
199 return pos(map
->dim
, type
);
202 isl_size
isl_basic_set_dim(__isl_keep isl_basic_set
*bset
,
203 enum isl_dim_type type
)
205 return isl_basic_map_dim(bset
, type
);
208 isl_size
isl_basic_set_n_dim(__isl_keep isl_basic_set
*bset
)
210 return isl_basic_set_dim(bset
, isl_dim_set
);
213 isl_size
isl_basic_set_n_param(__isl_keep isl_basic_set
*bset
)
215 return isl_basic_set_dim(bset
, isl_dim_param
);
218 isl_size
isl_basic_set_total_dim(__isl_keep
const isl_basic_set
*bset
)
220 return isl_basic_map_total_dim(const_bset_to_bmap(bset
));
223 isl_size
isl_set_n_dim(__isl_keep isl_set
*set
)
225 return isl_set_dim(set
, isl_dim_set
);
228 isl_size
isl_set_n_param(__isl_keep isl_set
*set
)
230 return isl_set_dim(set
, isl_dim_param
);
233 isl_size
isl_basic_map_total_dim(__isl_keep
const isl_basic_map
*bmap
)
238 return isl_size_error
;
239 dim
= isl_space_dim(bmap
->dim
, isl_dim_all
);
241 return isl_size_error
;
242 return dim
+ bmap
->n_div
;
245 /* Return the number of equality constraints in the description of "bmap".
246 * Return isl_size_error on error.
248 isl_size
isl_basic_map_n_equality(__isl_keep isl_basic_map
*bmap
)
251 return isl_size_error
;
255 /* Return the number of equality constraints in the description of "bset".
256 * Return isl_size_error on error.
258 isl_size
isl_basic_set_n_equality(__isl_keep isl_basic_set
*bset
)
260 return isl_basic_map_n_equality(bset_to_bmap(bset
));
263 /* Return the number of inequality constraints in the description of "bmap".
264 * Return isl_size_error on error.
266 isl_size
isl_basic_map_n_inequality(__isl_keep isl_basic_map
*bmap
)
269 return isl_size_error
;
273 /* Return the number of inequality constraints in the description of "bset".
274 * Return isl_size_error on error.
276 isl_size
isl_basic_set_n_inequality(__isl_keep isl_basic_set
*bset
)
278 return isl_basic_map_n_inequality(bset_to_bmap(bset
));
281 /* Do "bmap1" and "bmap2" have the same parameters?
283 static isl_bool
isl_basic_map_has_equal_params(__isl_keep isl_basic_map
*bmap1
,
284 __isl_keep isl_basic_map
*bmap2
)
286 isl_space
*space1
, *space2
;
288 space1
= isl_basic_map_peek_space(bmap1
);
289 space2
= isl_basic_map_peek_space(bmap2
);
290 return isl_space_has_equal_params(space1
, space2
);
293 /* Do "map1" and "map2" have the same parameters?
295 isl_bool
isl_map_has_equal_params(__isl_keep isl_map
*map1
,
296 __isl_keep isl_map
*map2
)
298 isl_space
*space1
, *space2
;
300 space1
= isl_map_peek_space(map1
);
301 space2
= isl_map_peek_space(map2
);
302 return isl_space_has_equal_params(space1
, space2
);
305 /* Do "map" and "set" have the same parameters?
307 static isl_bool
isl_map_set_has_equal_params(__isl_keep isl_map
*map
,
308 __isl_keep isl_set
*set
)
310 return isl_map_has_equal_params(map
, set_to_map(set
));
313 /* Is the tuple of type "type" of "bmap" the same as the single tuple of "bset"?
315 static isl_bool
isl_basic_map_set_tuple_is_equal(__isl_keep isl_basic_map
*bmap
,
316 enum isl_dim_type type
, __isl_keep isl_basic_set
*bset
)
318 isl_space
*bmap_space
, *bset_space
;
320 bmap_space
= isl_basic_map_peek_space(bmap
);
321 bset_space
= isl_basic_set_peek_space(bset
);
322 return isl_space_tuple_is_equal(bmap_space
, type
,
323 bset_space
, isl_dim_set
);
326 /* Is the tuple of type "type" of "map" the same as the single tuple of "set"?
328 static isl_bool
isl_map_set_tuple_is_equal(__isl_keep isl_map
*map
,
329 enum isl_dim_type type
, __isl_keep isl_set
*set
)
331 return isl_map_tuple_is_equal(map
, type
, set_to_map(set
), isl_dim_set
);
334 isl_bool
isl_map_compatible_domain(__isl_keep isl_map
*map
,
335 __isl_keep isl_set
*set
)
339 return isl_bool_error
;
340 m
= isl_map_has_equal_params(map
, set_to_map(set
));
343 return isl_map_set_tuple_is_equal(map
, isl_dim_in
, set
);
346 isl_bool
isl_basic_map_compatible_domain(__isl_keep isl_basic_map
*bmap
,
347 __isl_keep isl_basic_set
*bset
)
351 return isl_bool_error
;
352 m
= isl_basic_map_has_equal_params(bmap
, bset_to_bmap(bset
));
355 return isl_basic_map_set_tuple_is_equal(bmap
, isl_dim_in
, bset
);
358 isl_bool
isl_map_compatible_range(__isl_keep isl_map
*map
,
359 __isl_keep isl_set
*set
)
363 return isl_bool_error
;
364 m
= isl_map_has_equal_params(map
, set_to_map(set
));
367 return isl_map_set_tuple_is_equal(map
, isl_dim_out
, set
);
370 isl_bool
isl_basic_map_compatible_range(__isl_keep isl_basic_map
*bmap
,
371 __isl_keep isl_basic_set
*bset
)
375 return isl_bool_error
;
376 m
= isl_basic_map_has_equal_params(bmap
, bset_to_bmap(bset
));
379 return isl_basic_map_set_tuple_is_equal(bmap
, isl_dim_out
, bset
);
382 isl_ctx
*isl_basic_map_get_ctx(__isl_keep isl_basic_map
*bmap
)
384 return bmap
? bmap
->ctx
: NULL
;
387 isl_ctx
*isl_basic_set_get_ctx(__isl_keep isl_basic_set
*bset
)
389 return bset
? bset
->ctx
: NULL
;
392 isl_ctx
*isl_map_get_ctx(__isl_keep isl_map
*map
)
394 return map
? map
->ctx
: NULL
;
397 isl_ctx
*isl_set_get_ctx(__isl_keep isl_set
*set
)
399 return set
? set
->ctx
: NULL
;
402 /* Return the space of "bmap".
404 __isl_keep isl_space
*isl_basic_map_peek_space(
405 __isl_keep
const isl_basic_map
*bmap
)
407 return bmap
? bmap
->dim
: NULL
;
410 /* Return the space of "bset".
412 __isl_keep isl_space
*isl_basic_set_peek_space(__isl_keep isl_basic_set
*bset
)
414 return isl_basic_map_peek_space(bset_to_bmap(bset
));
417 __isl_give isl_space
*isl_basic_map_get_space(__isl_keep isl_basic_map
*bmap
)
419 return isl_space_copy(isl_basic_map_peek_space(bmap
));
422 __isl_give isl_space
*isl_basic_set_get_space(__isl_keep isl_basic_set
*bset
)
424 return isl_basic_map_get_space(bset_to_bmap(bset
));
427 /* Return the space of "bmap".
428 * This may be either a copy or the space itself
429 * if there is only one reference to "bmap".
430 * This allows the space to be modified inplace
431 * if both the basic map and its space have only a single reference.
432 * The caller is not allowed to modify "bmap" between this call and
433 * a subsequent call to isl_basic_map_restore_space.
434 * The only exception is that isl_basic_map_free can be called instead.
436 static __isl_give isl_space
*isl_basic_map_take_space(
437 __isl_keep isl_basic_map
*bmap
)
444 return isl_basic_map_get_space(bmap
);
450 /* Set the space of "bmap" to "space", where the space of "bmap" may be missing
451 * due to a preceding call to isl_basic_map_take_space.
452 * However, in this case, "bmap" only has a single reference and
453 * then the call to isl_basic_map_cow has no effect.
455 static __isl_give isl_basic_map
*isl_basic_map_restore_space(
456 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*space
)
461 if (bmap
->dim
== space
) {
462 isl_space_free(space
);
466 bmap
= isl_basic_map_cow(bmap
);
469 isl_space_free(bmap
->dim
);
474 isl_basic_map_free(bmap
);
475 isl_space_free(space
);
479 /* Extract the divs in "bmap" as a matrix.
481 __isl_give isl_mat
*isl_basic_map_get_divs(__isl_keep isl_basic_map
*bmap
)
489 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
493 ctx
= isl_basic_map_get_ctx(bmap
);
494 cols
= 1 + 1 + v_div
+ bmap
->n_div
;
495 div
= isl_mat_alloc(ctx
, bmap
->n_div
, cols
);
499 for (i
= 0; i
< bmap
->n_div
; ++i
)
500 isl_seq_cpy(div
->row
[i
], bmap
->div
[i
], cols
);
505 /* Extract the divs in "bset" as a matrix.
507 __isl_give isl_mat
*isl_basic_set_get_divs(__isl_keep isl_basic_set
*bset
)
509 return isl_basic_map_get_divs(bset
);
512 __isl_give isl_local_space
*isl_basic_map_get_local_space(
513 __isl_keep isl_basic_map
*bmap
)
520 div
= isl_basic_map_get_divs(bmap
);
521 return isl_local_space_alloc_div(isl_space_copy(bmap
->dim
), div
);
524 __isl_give isl_local_space
*isl_basic_set_get_local_space(
525 __isl_keep isl_basic_set
*bset
)
527 return isl_basic_map_get_local_space(bset
);
530 /* For each known div d = floor(f/m), add the constraints
533 * -(f-(m-1)) + m d >= 0
535 * Do not finalize the result.
537 static __isl_give isl_basic_map
*add_known_div_constraints(
538 __isl_take isl_basic_map
*bmap
)
543 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
545 return isl_basic_map_free(bmap
);
548 bmap
= isl_basic_map_cow(bmap
);
549 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 2 * n_div
);
552 for (i
= 0; i
< n_div
; ++i
) {
553 if (isl_int_is_zero(bmap
->div
[i
][0]))
555 bmap
= isl_basic_map_add_div_constraints(bmap
, i
);
561 __isl_give isl_basic_map
*isl_basic_map_from_local_space(
562 __isl_take isl_local_space
*ls
)
568 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
570 ls
= isl_local_space_free(ls
);
574 bmap
= isl_basic_map_alloc_space(isl_local_space_get_space(ls
),
575 n_div
, 0, 2 * n_div
);
577 for (i
= 0; i
< n_div
; ++i
)
578 if (isl_basic_map_alloc_div(bmap
) < 0)
581 for (i
= 0; i
< n_div
; ++i
)
582 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], ls
->div
->n_col
);
583 bmap
= add_known_div_constraints(bmap
);
585 isl_local_space_free(ls
);
588 isl_local_space_free(ls
);
589 isl_basic_map_free(bmap
);
593 __isl_give isl_basic_set
*isl_basic_set_from_local_space(
594 __isl_take isl_local_space
*ls
)
596 return isl_basic_map_from_local_space(ls
);
599 __isl_give isl_space
*isl_map_get_space(__isl_keep isl_map
*map
)
601 return isl_space_copy(isl_map_peek_space(map
));
604 __isl_give isl_space
*isl_set_get_space(__isl_keep isl_set
*set
)
608 return isl_space_copy(set
->dim
);
611 /* Return the space of "map".
612 * This may be either a copy or the space itself
613 * if there is only one reference to "map".
614 * This allows the space to be modified inplace
615 * if both the map and its space have only a single reference.
616 * The caller is not allowed to modify "map" between this call and
617 * a subsequent call to isl_map_restore_space.
618 * The only exception is that isl_map_free can be called instead.
620 static __isl_give isl_space
*isl_map_take_space(__isl_keep isl_map
*map
)
627 return isl_map_get_space(map
);
633 /* Set the space of "map" to "space", where the space of "map" may be missing
634 * due to a preceding call to isl_map_take_space.
635 * However, in this case, "map" only has a single reference and
636 * then the call to isl_map_cow has no effect.
638 static __isl_give isl_map
*isl_map_restore_space(__isl_take isl_map
*map
,
639 __isl_take isl_space
*space
)
644 if (map
->dim
== space
) {
645 isl_space_free(space
);
649 map
= isl_map_cow(map
);
652 isl_space_free(map
->dim
);
658 isl_space_free(space
);
662 __isl_give isl_basic_map
*isl_basic_map_set_tuple_name(
663 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
, const char *s
)
667 space
= isl_basic_map_take_space(bmap
);
668 space
= isl_space_set_tuple_name(space
, type
, s
);
669 bmap
= isl_basic_map_restore_space(bmap
, space
);
670 bmap
= isl_basic_map_finalize(bmap
);
674 __isl_give isl_basic_set
*isl_basic_set_set_tuple_name(
675 __isl_take isl_basic_set
*bset
, const char *s
)
677 return isl_basic_map_set_tuple_name(bset
, isl_dim_set
, s
);
680 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map
*bmap
,
681 enum isl_dim_type type
)
683 return bmap
? isl_space_get_tuple_name(bmap
->dim
, type
) : NULL
;
686 __isl_give isl_map
*isl_map_set_tuple_name(__isl_take isl_map
*map
,
687 enum isl_dim_type type
, const char *s
)
692 map
= isl_map_cow(map
);
696 for (i
= 0; i
< map
->n
; ++i
) {
697 map
->p
[i
] = isl_basic_map_set_tuple_name(map
->p
[i
], type
, s
);
702 space
= isl_map_take_space(map
);
703 space
= isl_space_set_tuple_name(space
, type
, s
);
704 map
= isl_map_restore_space(map
, space
);
712 /* Replace the identifier of the tuple of type "type" by "id".
714 __isl_give isl_basic_map
*isl_basic_map_set_tuple_id(
715 __isl_take isl_basic_map
*bmap
,
716 enum isl_dim_type type
, __isl_take isl_id
*id
)
720 space
= isl_basic_map_take_space(bmap
);
721 space
= isl_space_set_tuple_id(space
, type
, id
);
722 bmap
= isl_basic_map_restore_space(bmap
, space
);
723 bmap
= isl_basic_map_finalize(bmap
);
727 /* Replace the identifier of the tuple by "id".
729 __isl_give isl_basic_set
*isl_basic_set_set_tuple_id(
730 __isl_take isl_basic_set
*bset
, __isl_take isl_id
*id
)
732 return isl_basic_map_set_tuple_id(bset
, isl_dim_set
, id
);
735 /* Does the input or output tuple have a name?
737 isl_bool
isl_map_has_tuple_name(__isl_keep isl_map
*map
, enum isl_dim_type type
)
739 return map
? isl_space_has_tuple_name(map
->dim
, type
) : isl_bool_error
;
742 const char *isl_map_get_tuple_name(__isl_keep isl_map
*map
,
743 enum isl_dim_type type
)
745 return map
? isl_space_get_tuple_name(map
->dim
, type
) : NULL
;
748 __isl_give isl_set
*isl_set_set_tuple_name(__isl_take isl_set
*set
,
751 return set_from_map(isl_map_set_tuple_name(set_to_map(set
),
755 __isl_give isl_map
*isl_map_set_tuple_id(__isl_take isl_map
*map
,
756 enum isl_dim_type type
, __isl_take isl_id
*id
)
760 space
= isl_map_take_space(map
);
761 space
= isl_space_set_tuple_id(space
, type
, id
);
762 map
= isl_map_restore_space(map
, space
);
764 return isl_map_reset_space(map
, isl_map_get_space(map
));
767 /* Replace the identifier of the domain tuple of "map" by "id".
769 __isl_give isl_map
*isl_map_set_domain_tuple_id(__isl_take isl_map
*map
,
770 __isl_take isl_id
*id
)
772 return isl_map_set_tuple_id(map
, isl_dim_in
, id
);
775 /* Replace the identifier of the range tuple of "map" by "id".
777 __isl_give isl_map
*isl_map_set_range_tuple_id(__isl_take isl_map
*map
,
778 __isl_take isl_id
*id
)
780 return isl_map_set_tuple_id(map
, isl_dim_out
, id
);
783 __isl_give isl_set
*isl_set_set_tuple_id(__isl_take isl_set
*set
,
784 __isl_take isl_id
*id
)
786 return isl_map_set_tuple_id(set
, isl_dim_set
, id
);
789 __isl_give isl_map
*isl_map_reset_tuple_id(__isl_take isl_map
*map
,
790 enum isl_dim_type type
)
794 space
= isl_map_take_space(map
);
795 space
= isl_space_reset_tuple_id(space
, type
);
796 map
= isl_map_restore_space(map
, space
);
798 return isl_map_reset_space(map
, isl_map_get_space(map
));
801 __isl_give isl_set
*isl_set_reset_tuple_id(__isl_take isl_set
*set
)
803 return isl_map_reset_tuple_id(set
, isl_dim_set
);
806 isl_bool
isl_map_has_tuple_id(__isl_keep isl_map
*map
, enum isl_dim_type type
)
808 return map
? isl_space_has_tuple_id(map
->dim
, type
) : isl_bool_error
;
811 /* Does the domain tuple of "map" have an identifier?
813 isl_bool
isl_map_has_domain_tuple_id(__isl_keep isl_map
*map
)
815 return isl_map_has_tuple_id(map
, isl_dim_in
);
818 /* Does the range tuple of "map" have an identifier?
820 isl_bool
isl_map_has_range_tuple_id(__isl_keep isl_map
*map
)
822 return isl_map_has_tuple_id(map
, isl_dim_out
);
825 __isl_give isl_id
*isl_map_get_tuple_id(__isl_keep isl_map
*map
,
826 enum isl_dim_type type
)
828 return map
? isl_space_get_tuple_id(map
->dim
, type
) : NULL
;
831 /* Return the identifier of the domain tuple of "map", assuming it has one.
833 __isl_give isl_id
*isl_map_get_domain_tuple_id(__isl_keep isl_map
*map
)
835 return isl_map_get_tuple_id(map
, isl_dim_in
);
838 /* Return the identifier of the range tuple of "map", assuming it has one.
840 __isl_give isl_id
*isl_map_get_range_tuple_id(__isl_keep isl_map
*map
)
842 return isl_map_get_tuple_id(map
, isl_dim_out
);
845 isl_bool
isl_set_has_tuple_id(__isl_keep isl_set
*set
)
847 return isl_map_has_tuple_id(set
, isl_dim_set
);
850 __isl_give isl_id
*isl_set_get_tuple_id(__isl_keep isl_set
*set
)
852 return isl_map_get_tuple_id(set
, isl_dim_set
);
855 /* Does the set tuple have a name?
857 isl_bool
isl_set_has_tuple_name(__isl_keep isl_set
*set
)
860 return isl_bool_error
;
861 return isl_space_has_tuple_name(set
->dim
, isl_dim_set
);
865 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set
*bset
)
867 return bset
? isl_space_get_tuple_name(bset
->dim
, isl_dim_set
) : NULL
;
870 const char *isl_set_get_tuple_name(__isl_keep isl_set
*set
)
872 return set
? isl_space_get_tuple_name(set
->dim
, isl_dim_set
) : NULL
;
875 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map
*bmap
,
876 enum isl_dim_type type
, unsigned pos
)
878 return bmap
? isl_space_get_dim_name(bmap
->dim
, type
, pos
) : NULL
;
881 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set
*bset
,
882 enum isl_dim_type type
, unsigned pos
)
884 return bset
? isl_space_get_dim_name(bset
->dim
, type
, pos
) : NULL
;
887 /* Does the given dimension have a name?
889 isl_bool
isl_map_has_dim_name(__isl_keep isl_map
*map
,
890 enum isl_dim_type type
, unsigned pos
)
893 return isl_bool_error
;
894 return isl_space_has_dim_name(map
->dim
, type
, pos
);
897 const char *isl_map_get_dim_name(__isl_keep isl_map
*map
,
898 enum isl_dim_type type
, unsigned pos
)
900 return map
? isl_space_get_dim_name(map
->dim
, type
, pos
) : NULL
;
903 const char *isl_set_get_dim_name(__isl_keep isl_set
*set
,
904 enum isl_dim_type type
, unsigned pos
)
906 return set
? isl_space_get_dim_name(set
->dim
, type
, pos
) : NULL
;
909 /* Does the given dimension have a name?
911 isl_bool
isl_set_has_dim_name(__isl_keep isl_set
*set
,
912 enum isl_dim_type type
, unsigned pos
)
915 return isl_bool_error
;
916 return isl_space_has_dim_name(set
->dim
, type
, pos
);
919 __isl_give isl_basic_map
*isl_basic_map_set_dim_name(
920 __isl_take isl_basic_map
*bmap
,
921 enum isl_dim_type type
, unsigned pos
, const char *s
)
925 space
= isl_basic_map_take_space(bmap
);
926 space
= isl_space_set_dim_name(space
, type
, pos
, s
);
927 bmap
= isl_basic_map_restore_space(bmap
, space
);
928 return isl_basic_map_finalize(bmap
);
931 __isl_give isl_map
*isl_map_set_dim_name(__isl_take isl_map
*map
,
932 enum isl_dim_type type
, unsigned pos
, const char *s
)
937 map
= isl_map_cow(map
);
941 for (i
= 0; i
< map
->n
; ++i
) {
942 map
->p
[i
] = isl_basic_map_set_dim_name(map
->p
[i
], type
, pos
, s
);
947 space
= isl_map_take_space(map
);
948 space
= isl_space_set_dim_name(space
, type
, pos
, s
);
949 map
= isl_map_restore_space(map
, space
);
957 __isl_give isl_basic_set
*isl_basic_set_set_dim_name(
958 __isl_take isl_basic_set
*bset
,
959 enum isl_dim_type type
, unsigned pos
, const char *s
)
961 return bset_from_bmap(isl_basic_map_set_dim_name(bset_to_bmap(bset
),
965 __isl_give isl_set
*isl_set_set_dim_name(__isl_take isl_set
*set
,
966 enum isl_dim_type type
, unsigned pos
, const char *s
)
968 return set_from_map(isl_map_set_dim_name(set_to_map(set
),
972 isl_bool
isl_basic_map_has_dim_id(__isl_keep isl_basic_map
*bmap
,
973 enum isl_dim_type type
, unsigned pos
)
976 return isl_bool_error
;
977 return isl_space_has_dim_id(bmap
->dim
, type
, pos
);
980 __isl_give isl_id
*isl_basic_set_get_dim_id(__isl_keep isl_basic_set
*bset
,
981 enum isl_dim_type type
, unsigned pos
)
983 return bset
? isl_space_get_dim_id(bset
->dim
, type
, pos
) : NULL
;
986 isl_bool
isl_map_has_dim_id(__isl_keep isl_map
*map
,
987 enum isl_dim_type type
, unsigned pos
)
989 return map
? isl_space_has_dim_id(map
->dim
, type
, pos
) : isl_bool_error
;
992 __isl_give isl_id
*isl_map_get_dim_id(__isl_keep isl_map
*map
,
993 enum isl_dim_type type
, unsigned pos
)
995 return map
? isl_space_get_dim_id(map
->dim
, type
, pos
) : NULL
;
998 isl_bool
isl_set_has_dim_id(__isl_keep isl_set
*set
,
999 enum isl_dim_type type
, unsigned pos
)
1001 return isl_map_has_dim_id(set
, type
, pos
);
1004 __isl_give isl_id
*isl_set_get_dim_id(__isl_keep isl_set
*set
,
1005 enum isl_dim_type type
, unsigned pos
)
1007 return isl_map_get_dim_id(set
, type
, pos
);
1010 __isl_give isl_map
*isl_map_set_dim_id(__isl_take isl_map
*map
,
1011 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
1015 space
= isl_map_take_space(map
);
1016 space
= isl_space_set_dim_id(space
, type
, pos
, id
);
1017 map
= isl_map_restore_space(map
, space
);
1019 return isl_map_reset_space(map
, isl_map_get_space(map
));
1022 __isl_give isl_set
*isl_set_set_dim_id(__isl_take isl_set
*set
,
1023 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
1025 return isl_map_set_dim_id(set
, type
, pos
, id
);
1028 int isl_map_find_dim_by_id(__isl_keep isl_map
*map
, enum isl_dim_type type
,
1029 __isl_keep isl_id
*id
)
1033 return isl_space_find_dim_by_id(map
->dim
, type
, id
);
1036 int isl_set_find_dim_by_id(__isl_keep isl_set
*set
, enum isl_dim_type type
,
1037 __isl_keep isl_id
*id
)
1039 return isl_map_find_dim_by_id(set
, type
, id
);
1042 /* Return the position of the dimension of the given type and name
1044 * Return -1 if no such dimension can be found.
1046 int isl_basic_map_find_dim_by_name(__isl_keep isl_basic_map
*bmap
,
1047 enum isl_dim_type type
, const char *name
)
1051 return isl_space_find_dim_by_name(bmap
->dim
, type
, name
);
1054 int isl_map_find_dim_by_name(__isl_keep isl_map
*map
, enum isl_dim_type type
,
1059 return isl_space_find_dim_by_name(map
->dim
, type
, name
);
1062 int isl_set_find_dim_by_name(__isl_keep isl_set
*set
, enum isl_dim_type type
,
1065 return isl_map_find_dim_by_name(set
, type
, name
);
1068 /* Check whether equality i of bset is a pure stride constraint
1069 * on a single dimension, i.e., of the form
1073 * with k a constant and e an existentially quantified variable.
1075 isl_bool
isl_basic_set_eq_is_stride(__isl_keep isl_basic_set
*bset
, int i
)
1083 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
1084 d
= isl_basic_set_dim(bset
, isl_dim_set
);
1085 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
1086 if (nparam
< 0 || d
< 0 || n_div
< 0)
1087 return isl_bool_error
;
1089 if (!isl_int_is_zero(bset
->eq
[i
][0]))
1090 return isl_bool_false
;
1092 if (isl_seq_first_non_zero(bset
->eq
[i
] + 1, nparam
) != -1)
1093 return isl_bool_false
;
1094 pos1
= isl_seq_first_non_zero(bset
->eq
[i
] + 1 + nparam
, d
);
1096 return isl_bool_false
;
1097 if (isl_seq_first_non_zero(bset
->eq
[i
] + 1 + nparam
+ pos1
+ 1,
1098 d
- pos1
- 1) != -1)
1099 return isl_bool_false
;
1101 pos2
= isl_seq_first_non_zero(bset
->eq
[i
] + 1 + nparam
+ d
, n_div
);
1103 return isl_bool_false
;
1104 if (isl_seq_first_non_zero(bset
->eq
[i
] + 1 + nparam
+ d
+ pos2
+ 1,
1105 n_div
- pos2
- 1) != -1)
1106 return isl_bool_false
;
1107 if (!isl_int_is_one(bset
->eq
[i
][1 + nparam
+ pos1
]) &&
1108 !isl_int_is_negone(bset
->eq
[i
][1 + nparam
+ pos1
]))
1109 return isl_bool_false
;
1111 return isl_bool_true
;
1114 /* Reset the user pointer on all identifiers of parameters and tuples
1115 * of the space of "map".
1117 __isl_give isl_map
*isl_map_reset_user(__isl_take isl_map
*map
)
1121 space
= isl_map_get_space(map
);
1122 space
= isl_space_reset_user(space
);
1123 map
= isl_map_reset_space(map
, space
);
1128 /* Reset the user pointer on all identifiers of parameters and tuples
1129 * of the space of "set".
1131 __isl_give isl_set
*isl_set_reset_user(__isl_take isl_set
*set
)
1133 return isl_map_reset_user(set
);
1136 isl_bool
isl_basic_map_is_rational(__isl_keep isl_basic_map
*bmap
)
1139 return isl_bool_error
;
1140 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
1143 /* Has "map" been marked as a rational map?
1144 * In particular, have all basic maps in "map" been marked this way?
1145 * An empty map is not considered to be rational.
1146 * Maps where only some of the basic maps are marked rational
1149 isl_bool
isl_map_is_rational(__isl_keep isl_map
*map
)
1155 return isl_bool_error
;
1157 return isl_bool_false
;
1158 rational
= isl_basic_map_is_rational(map
->p
[0]);
1161 for (i
= 1; i
< map
->n
; ++i
) {
1162 isl_bool rational_i
;
1164 rational_i
= isl_basic_map_is_rational(map
->p
[i
]);
1167 if (rational
!= rational_i
)
1168 isl_die(isl_map_get_ctx(map
), isl_error_unsupported
,
1169 "mixed rational and integer basic maps "
1170 "not supported", return isl_bool_error
);
1176 /* Has "set" been marked as a rational set?
1177 * In particular, have all basic set in "set" been marked this way?
1178 * An empty set is not considered to be rational.
1179 * Sets where only some of the basic sets are marked rational
1182 isl_bool
isl_set_is_rational(__isl_keep isl_set
*set
)
1184 return isl_map_is_rational(set
);
1187 int isl_basic_set_is_rational(__isl_keep isl_basic_set
*bset
)
1189 return isl_basic_map_is_rational(bset
);
1192 /* Does "bmap" contain any rational points?
1194 * If "bmap" has an equality for each dimension, equating the dimension
1195 * to an integer constant, then it has no rational points, even if it
1196 * is marked as rational.
1198 isl_bool
isl_basic_map_has_rational(__isl_keep isl_basic_map
*bmap
)
1200 isl_bool has_rational
= isl_bool_true
;
1204 return isl_bool_error
;
1205 if (isl_basic_map_plain_is_empty(bmap
))
1206 return isl_bool_false
;
1207 if (!isl_basic_map_is_rational(bmap
))
1208 return isl_bool_false
;
1209 bmap
= isl_basic_map_copy(bmap
);
1210 bmap
= isl_basic_map_implicit_equalities(bmap
);
1211 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
1213 return isl_bool_error
;
1214 if (bmap
->n_eq
== total
) {
1216 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
1217 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1, total
);
1220 if (!isl_int_is_one(bmap
->eq
[i
][1 + j
]) &&
1221 !isl_int_is_negone(bmap
->eq
[i
][1 + j
]))
1223 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + j
+ 1,
1228 if (i
== bmap
->n_eq
)
1229 has_rational
= isl_bool_false
;
1231 isl_basic_map_free(bmap
);
1233 return has_rational
;
1236 /* Does "map" contain any rational points?
1238 isl_bool
isl_map_has_rational(__isl_keep isl_map
*map
)
1241 isl_bool has_rational
;
1244 return isl_bool_error
;
1245 for (i
= 0; i
< map
->n
; ++i
) {
1246 has_rational
= isl_basic_map_has_rational(map
->p
[i
]);
1247 if (has_rational
< 0 || has_rational
)
1248 return has_rational
;
1250 return isl_bool_false
;
1253 /* Does "set" contain any rational points?
1255 isl_bool
isl_set_has_rational(__isl_keep isl_set
*set
)
1257 return isl_map_has_rational(set
);
1260 /* Is this basic set a parameter domain?
1262 isl_bool
isl_basic_set_is_params(__isl_keep isl_basic_set
*bset
)
1265 return isl_bool_error
;
1266 return isl_space_is_params(bset
->dim
);
1269 /* Is this set a parameter domain?
1271 isl_bool
isl_set_is_params(__isl_keep isl_set
*set
)
1274 return isl_bool_error
;
1275 return isl_space_is_params(set
->dim
);
1278 /* Is this map actually a parameter domain?
1279 * Users should never call this function. Outside of isl,
1280 * a map can never be a parameter domain.
1282 isl_bool
isl_map_is_params(__isl_keep isl_map
*map
)
1285 return isl_bool_error
;
1286 return isl_space_is_params(map
->dim
);
1289 static __isl_give isl_basic_map
*basic_map_init(isl_ctx
*ctx
,
1290 __isl_take isl_basic_map
*bmap
, unsigned extra
,
1291 unsigned n_eq
, unsigned n_ineq
)
1294 isl_space
*space
= isl_basic_map_peek_space(bmap
);
1295 isl_size n_var
= isl_space_dim(space
, isl_dim_all
);
1296 size_t row_size
= 1 + n_var
+ extra
;
1302 return isl_basic_map_free(bmap
);
1304 bmap
->block
= isl_blk_alloc(ctx
, (n_ineq
+ n_eq
) * row_size
);
1305 if (isl_blk_is_error(bmap
->block
))
1308 bmap
->ineq
= isl_alloc_array(ctx
, isl_int
*, n_ineq
+ n_eq
);
1309 if ((n_ineq
+ n_eq
) && !bmap
->ineq
)
1313 bmap
->block2
= isl_blk_empty();
1316 bmap
->block2
= isl_blk_alloc(ctx
, extra
* (1 + row_size
));
1317 if (isl_blk_is_error(bmap
->block2
))
1320 bmap
->div
= isl_alloc_array(ctx
, isl_int
*, extra
);
1325 for (i
= 0; i
< n_ineq
+ n_eq
; ++i
)
1326 bmap
->ineq
[i
] = bmap
->block
.data
+ i
* row_size
;
1328 for (i
= 0; i
< extra
; ++i
)
1329 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + row_size
);
1333 bmap
->c_size
= n_eq
+ n_ineq
;
1334 bmap
->eq
= bmap
->ineq
+ n_ineq
;
1335 bmap
->extra
= extra
;
1339 bmap
->sample
= NULL
;
1343 isl_basic_map_free(bmap
);
1347 __isl_give isl_basic_set
*isl_basic_set_alloc(isl_ctx
*ctx
,
1348 unsigned nparam
, unsigned dim
, unsigned extra
,
1349 unsigned n_eq
, unsigned n_ineq
)
1351 struct isl_basic_map
*bmap
;
1354 space
= isl_space_set_alloc(ctx
, nparam
, dim
);
1358 bmap
= isl_basic_map_alloc_space(space
, extra
, n_eq
, n_ineq
);
1359 return bset_from_bmap(bmap
);
1362 __isl_give isl_basic_set
*isl_basic_set_alloc_space(__isl_take isl_space
*space
,
1363 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
1365 struct isl_basic_map
*bmap
;
1368 isl_assert(space
->ctx
, space
->n_in
== 0, goto error
);
1369 bmap
= isl_basic_map_alloc_space(space
, extra
, n_eq
, n_ineq
);
1370 return bset_from_bmap(bmap
);
1372 isl_space_free(space
);
1376 __isl_give isl_basic_map
*isl_basic_map_alloc_space(__isl_take isl_space
*space
,
1377 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
1379 struct isl_basic_map
*bmap
;
1383 bmap
= isl_calloc_type(space
->ctx
, struct isl_basic_map
);
1388 return basic_map_init(space
->ctx
, bmap
, extra
, n_eq
, n_ineq
);
1390 isl_space_free(space
);
1394 __isl_give isl_basic_map
*isl_basic_map_alloc(isl_ctx
*ctx
,
1395 unsigned nparam
, unsigned in
, unsigned out
, unsigned extra
,
1396 unsigned n_eq
, unsigned n_ineq
)
1398 struct isl_basic_map
*bmap
;
1401 space
= isl_space_alloc(ctx
, nparam
, in
, out
);
1405 bmap
= isl_basic_map_alloc_space(space
, extra
, n_eq
, n_ineq
);
1409 static __isl_give isl_basic_map
*dup_constraints(__isl_take isl_basic_map
*dst
,
1410 __isl_keep isl_basic_map
*src
)
1413 isl_size total
= isl_basic_map_dim(src
, isl_dim_all
);
1415 if (!dst
|| total
< 0)
1416 return isl_basic_map_free(dst
);
1418 for (i
= 0; i
< src
->n_eq
; ++i
) {
1419 int j
= isl_basic_map_alloc_equality(dst
);
1421 return isl_basic_map_free(dst
);
1422 isl_seq_cpy(dst
->eq
[j
], src
->eq
[i
], 1+total
);
1425 for (i
= 0; i
< src
->n_ineq
; ++i
) {
1426 int j
= isl_basic_map_alloc_inequality(dst
);
1428 return isl_basic_map_free(dst
);
1429 isl_seq_cpy(dst
->ineq
[j
], src
->ineq
[i
], 1+total
);
1432 for (i
= 0; i
< src
->n_div
; ++i
) {
1433 int j
= isl_basic_map_alloc_div(dst
);
1435 return isl_basic_map_free(dst
);
1436 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
);
1438 ISL_F_SET(dst
, ISL_BASIC_SET_FINAL
);
1442 __isl_give isl_basic_map
*isl_basic_map_dup(__isl_keep isl_basic_map
*bmap
)
1444 struct isl_basic_map
*dup
;
1448 dup
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
1449 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
1450 dup
= dup_constraints(dup
, bmap
);
1453 dup
->flags
= bmap
->flags
;
1454 dup
->sample
= isl_vec_copy(bmap
->sample
);
1458 __isl_give isl_basic_set
*isl_basic_set_dup(__isl_keep isl_basic_set
*bset
)
1460 struct isl_basic_map
*dup
;
1462 dup
= isl_basic_map_dup(bset_to_bmap(bset
));
1463 return bset_from_bmap(dup
);
1466 __isl_give isl_basic_set
*isl_basic_set_copy(__isl_keep isl_basic_set
*bset
)
1468 return bset_from_bmap(isl_basic_map_copy(bset_to_bmap(bset
)));
1471 __isl_give isl_set
*isl_set_copy(__isl_keep isl_set
*set
)
1480 __isl_give isl_basic_map
*isl_basic_map_copy(__isl_keep isl_basic_map
*bmap
)
1485 if (ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
)) {
1489 bmap
= isl_basic_map_dup(bmap
);
1491 ISL_F_SET(bmap
, ISL_BASIC_SET_FINAL
);
1495 __isl_give isl_map
*isl_map_copy(__isl_keep isl_map
*map
)
1504 __isl_null isl_basic_map
*isl_basic_map_free(__isl_take isl_basic_map
*bmap
)
1509 if (--bmap
->ref
> 0)
1512 isl_ctx_deref(bmap
->ctx
);
1514 isl_blk_free(bmap
->ctx
, bmap
->block2
);
1516 isl_blk_free(bmap
->ctx
, bmap
->block
);
1517 isl_vec_free(bmap
->sample
);
1518 isl_space_free(bmap
->dim
);
1524 __isl_null isl_basic_set
*isl_basic_set_free(__isl_take isl_basic_set
*bset
)
1526 return isl_basic_map_free(bset_to_bmap(bset
));
1529 static int room_for_con(__isl_keep isl_basic_map
*bmap
, unsigned n
)
1531 return bmap
->n_eq
+ bmap
->n_ineq
+ n
<= bmap
->c_size
;
1534 /* Check that "bset" does not involve any parameters.
1536 isl_stat
isl_basic_set_check_no_params(__isl_keep isl_basic_set
*bset
)
1540 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
1542 return isl_stat_error
;
1544 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
1545 "basic set should not have any parameters",
1546 return isl_stat_error
);
1550 /* Check that "bset" does not involve any local variables.
1552 isl_stat
isl_basic_set_check_no_locals(__isl_keep isl_basic_set
*bset
)
1556 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
1558 return isl_stat_error
;
1560 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
1561 "basic set should not have any local variables",
1562 return isl_stat_error
);
1567 #define TYPE isl_map
1569 #include "isl_check_named_params_templ.c"
1572 #define TYPE isl_basic_map
1575 #include "isl_check_named_params_templ.c"
1577 /* Check that "bmap1" and "bmap2" have the same parameters,
1578 * reporting an error if they do not.
1580 static isl_stat
isl_basic_map_check_equal_params(
1581 __isl_keep isl_basic_map
*bmap1
, __isl_keep isl_basic_map
*bmap2
)
1585 match
= isl_basic_map_has_equal_params(bmap1
, bmap2
);
1587 return isl_stat_error
;
1589 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
1590 "parameters don't match", return isl_stat_error
);
1595 #define TYPE isl_map
1597 #include "isl_align_params_bin_templ.c"
1602 #define ARG1 isl_map
1604 #define ARG2 isl_set
1606 #include "isl_align_params_templ.c"
1608 isl_bool
isl_map_align_params_map_map_and_test(__isl_keep isl_map
*map1
,
1609 __isl_keep isl_map
*map2
,
1610 isl_bool (*fn
)(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
))
1615 return isl_bool_error
;
1616 if (isl_map_has_equal_params(map1
, map2
))
1617 return fn(map1
, map2
);
1618 if (isl_map_check_named_params(map1
) < 0)
1619 return isl_bool_error
;
1620 if (isl_map_check_named_params(map2
) < 0)
1621 return isl_bool_error
;
1622 map1
= isl_map_copy(map1
);
1623 map2
= isl_map_copy(map2
);
1624 map1
= isl_map_align_params(map1
, isl_map_get_space(map2
));
1625 map2
= isl_map_align_params(map2
, isl_map_get_space(map1
));
1632 int isl_basic_map_alloc_equality(__isl_keep isl_basic_map
*bmap
)
1635 struct isl_ctx
*ctx
;
1637 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
1641 isl_assert(ctx
, room_for_con(bmap
, 1), return -1);
1642 isl_assert(ctx
, (bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
<= bmap
->c_size
,
1644 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1645 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1646 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1647 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1648 if ((bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
== bmap
->c_size
) {
1650 int j
= isl_basic_map_alloc_inequality(bmap
);
1654 bmap
->ineq
[j
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1655 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
1662 isl_seq_clr(bmap
->eq
[bmap
->n_eq
] + 1 + total
,
1663 bmap
->extra
- bmap
->n_div
);
1664 return bmap
->n_eq
++;
1667 int isl_basic_set_alloc_equality(__isl_keep isl_basic_set
*bset
)
1669 return isl_basic_map_alloc_equality(bset_to_bmap(bset
));
1672 __isl_give isl_basic_map
*isl_basic_map_free_equality(
1673 __isl_take isl_basic_map
*bmap
, unsigned n
)
1678 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
1679 "invalid number of equalities",
1680 isl_basic_map_free(bmap
));
1685 __isl_give isl_basic_set
*isl_basic_set_free_equality(
1686 __isl_take isl_basic_set
*bset
, unsigned n
)
1688 return bset_from_bmap(isl_basic_map_free_equality(bset_to_bmap(bset
),
1692 /* Drop the equality constraint at position "pos",
1693 * preserving the order of the other equality constraints.
1695 int isl_basic_map_drop_equality(__isl_keep isl_basic_map
*bmap
, unsigned pos
)
1702 isl_assert(bmap
->ctx
, pos
< bmap
->n_eq
, return -1);
1706 for (r
= pos
; r
< bmap
->n_eq
; ++r
)
1707 bmap
->eq
[r
] = bmap
->eq
[r
+ 1];
1708 bmap
->eq
[bmap
->n_eq
] = t
;
1713 /* Turn inequality "pos" of "bmap" into an equality.
1715 * In particular, we move the inequality in front of the equalities
1716 * and move the last inequality in the position of the moved inequality.
1717 * Note that isl_tab_make_equalities_explicit depends on this particular
1718 * change in the ordering of the constraints.
1720 void isl_basic_map_inequality_to_equality(
1721 __isl_keep isl_basic_map
*bmap
, unsigned pos
)
1725 t
= bmap
->ineq
[pos
];
1726 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1727 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
1732 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1733 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
1734 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1735 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1738 static int room_for_ineq(__isl_keep isl_basic_map
*bmap
, unsigned n
)
1740 return bmap
->n_ineq
+ n
<= bmap
->eq
- bmap
->ineq
;
1743 int isl_basic_map_alloc_inequality(__isl_keep isl_basic_map
*bmap
)
1746 struct isl_ctx
*ctx
;
1748 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
1752 isl_assert(ctx
, room_for_ineq(bmap
, 1), return -1);
1753 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1754 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1755 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
1756 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1757 isl_seq_clr(bmap
->ineq
[bmap
->n_ineq
] + 1 + total
,
1758 bmap
->extra
- bmap
->n_div
);
1759 return bmap
->n_ineq
++;
1762 int isl_basic_set_alloc_inequality(__isl_keep isl_basic_set
*bset
)
1764 return isl_basic_map_alloc_inequality(bset_to_bmap(bset
));
1767 __isl_give isl_basic_map
*isl_basic_map_free_inequality(
1768 __isl_take isl_basic_map
*bmap
, unsigned n
)
1772 if (n
> bmap
->n_ineq
)
1773 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
1774 "invalid number of inequalities",
1775 return isl_basic_map_free(bmap
));
1780 __isl_give isl_basic_set
*isl_basic_set_free_inequality(
1781 __isl_take isl_basic_set
*bset
, unsigned n
)
1783 return bset_from_bmap(isl_basic_map_free_inequality(bset_to_bmap(bset
),
1787 int isl_basic_map_drop_inequality(__isl_keep isl_basic_map
*bmap
, unsigned pos
)
1792 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
1794 if (pos
!= bmap
->n_ineq
- 1) {
1795 t
= bmap
->ineq
[pos
];
1796 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1797 bmap
->ineq
[bmap
->n_ineq
- 1] = t
;
1798 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
1804 int isl_basic_set_drop_inequality(__isl_keep isl_basic_set
*bset
, unsigned pos
)
1806 return isl_basic_map_drop_inequality(bset_to_bmap(bset
), pos
);
1809 __isl_give isl_basic_map
*isl_basic_map_add_eq(__isl_take isl_basic_map
*bmap
,
1816 empty
= isl_basic_map_plain_is_empty(bmap
);
1818 return isl_basic_map_free(bmap
);
1822 bmap
= isl_basic_map_cow(bmap
);
1823 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
1824 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
1826 return isl_basic_map_free(bmap
);
1827 k
= isl_basic_map_alloc_equality(bmap
);
1830 isl_seq_cpy(bmap
->eq
[k
], eq
, 1 + total
);
1833 isl_basic_map_free(bmap
);
1837 __isl_give isl_basic_set
*isl_basic_set_add_eq(__isl_take isl_basic_set
*bset
,
1840 return bset_from_bmap(isl_basic_map_add_eq(bset_to_bmap(bset
), eq
));
1843 __isl_give isl_basic_map
*isl_basic_map_add_ineq(__isl_take isl_basic_map
*bmap
,
1849 bmap
= isl_basic_map_cow(bmap
);
1850 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
1851 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
1853 return isl_basic_map_free(bmap
);
1854 k
= isl_basic_map_alloc_inequality(bmap
);
1857 isl_seq_cpy(bmap
->ineq
[k
], ineq
, 1 + total
);
1860 isl_basic_map_free(bmap
);
1864 __isl_give isl_basic_set
*isl_basic_set_add_ineq(__isl_take isl_basic_set
*bset
,
1867 return bset_from_bmap(isl_basic_map_add_ineq(bset_to_bmap(bset
), ineq
));
1870 int isl_basic_map_alloc_div(__isl_keep isl_basic_map
*bmap
)
1874 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
1877 isl_assert(bmap
->ctx
, bmap
->n_div
< bmap
->extra
, return -1);
1878 isl_seq_clr(bmap
->div
[bmap
->n_div
] + 1 + 1 + total
,
1879 bmap
->extra
- bmap
->n_div
);
1880 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1881 return bmap
->n_div
++;
1884 int isl_basic_set_alloc_div(__isl_keep isl_basic_set
*bset
)
1886 return isl_basic_map_alloc_div(bset_to_bmap(bset
));
1890 #define TYPE isl_basic_map
1891 #include "check_type_range_templ.c"
1893 /* Check that there are "n" dimensions of type "type" starting at "first"
1896 isl_stat
isl_basic_set_check_range(__isl_keep isl_basic_set
*bset
,
1897 enum isl_dim_type type
, unsigned first
, unsigned n
)
1899 return isl_basic_map_check_range(bset_to_bmap(bset
),
1903 /* Insert an extra integer division, prescribed by "div", to "bmap"
1904 * at (integer division) position "pos".
1906 * The integer division is first added at the end and then moved
1907 * into the right position.
1909 __isl_give isl_basic_map
*isl_basic_map_insert_div(
1910 __isl_take isl_basic_map
*bmap
, int pos
, __isl_keep isl_vec
*div
)
1915 bmap
= isl_basic_map_cow(bmap
);
1916 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
1917 if (total
< 0 || !div
)
1918 return isl_basic_map_free(bmap
);
1920 if (div
->size
!= 1 + 1 + total
)
1921 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
1922 "unexpected size", return isl_basic_map_free(bmap
));
1923 if (isl_basic_map_check_range(bmap
, isl_dim_div
, pos
, 0) < 0)
1924 return isl_basic_map_free(bmap
);
1926 bmap
= isl_basic_map_extend(bmap
, 1, 0, 2);
1927 k
= isl_basic_map_alloc_div(bmap
);
1929 return isl_basic_map_free(bmap
);
1930 isl_seq_cpy(bmap
->div
[k
], div
->el
, div
->size
);
1931 isl_int_set_si(bmap
->div
[k
][div
->size
], 0);
1933 for (i
= k
; i
> pos
; --i
)
1934 bmap
= isl_basic_map_swap_div(bmap
, i
, i
- 1);
1939 isl_stat
isl_basic_map_free_div(__isl_keep isl_basic_map
*bmap
, unsigned n
)
1942 return isl_stat_error
;
1943 isl_assert(bmap
->ctx
, n
<= bmap
->n_div
, return isl_stat_error
);
1948 static __isl_give isl_basic_map
*add_constraints(
1949 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
,
1950 unsigned i_pos
, unsigned o_pos
)
1952 isl_size total
, n_param
, n_in
, n_out
, n_div
;
1953 unsigned o_in
, o_out
;
1956 struct isl_dim_map
*dim_map
;
1958 space
= isl_basic_map_peek_space(bmap2
);
1959 if (!bmap1
|| !space
)
1962 total
= isl_basic_map_dim(bmap1
, isl_dim_all
);
1963 n_param
= isl_basic_map_dim(bmap2
, isl_dim_param
);
1964 n_in
= isl_basic_map_dim(bmap2
, isl_dim_in
);
1965 o_in
= isl_basic_map_offset(bmap1
, isl_dim_in
) - 1 + i_pos
;
1966 n_out
= isl_basic_map_dim(bmap2
, isl_dim_out
);
1967 o_out
= isl_basic_map_offset(bmap1
, isl_dim_out
) - 1 + o_pos
;
1968 n_div
= isl_basic_map_dim(bmap2
, isl_dim_div
);
1969 if (total
< 0 || n_param
< 0 || n_in
< 0 || n_out
< 0 || n_div
< 0)
1971 ctx
= isl_basic_map_get_ctx(bmap1
);
1972 dim_map
= isl_dim_map_alloc(ctx
, total
+ n_div
);
1973 isl_dim_map_dim_range(dim_map
, space
, isl_dim_param
, 0, n_param
, 0);
1974 isl_dim_map_dim_range(dim_map
, space
, isl_dim_in
, 0, n_in
, o_in
);
1975 isl_dim_map_dim_range(dim_map
, space
, isl_dim_out
, 0, n_out
, o_out
);
1976 isl_dim_map_div(dim_map
, bmap2
, total
);
1978 return isl_basic_map_add_constraints_dim_map(bmap1
, bmap2
, dim_map
);
1980 isl_basic_map_free(bmap1
);
1981 isl_basic_map_free(bmap2
);
1985 __isl_give isl_basic_map
*isl_basic_map_extend(__isl_take isl_basic_map
*base
,
1986 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
1989 struct isl_basic_map
*ext
;
1996 dims_ok
= base
->extra
>= base
->n_div
+ extra
;
1998 if (dims_ok
&& room_for_con(base
, n_eq
+ n_ineq
) &&
1999 room_for_ineq(base
, n_ineq
))
2002 extra
+= base
->extra
;
2004 n_ineq
+= base
->n_ineq
;
2006 space
= isl_basic_map_get_space(base
);
2007 ext
= isl_basic_map_alloc_space(space
, extra
, n_eq
, n_ineq
);
2012 ext
->sample
= isl_vec_copy(base
->sample
);
2013 flags
= base
->flags
;
2014 ext
= add_constraints(ext
, base
, 0, 0);
2017 ISL_F_CLR(ext
, ISL_BASIC_SET_FINAL
);
2023 isl_basic_map_free(base
);
2027 __isl_give isl_basic_set
*isl_basic_set_extend(__isl_take isl_basic_set
*base
,
2028 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
2030 return bset_from_bmap(isl_basic_map_extend(bset_to_bmap(base
),
2031 extra
, n_eq
, n_ineq
));
2034 __isl_give isl_basic_map
*isl_basic_map_extend_constraints(
2035 __isl_take isl_basic_map
*base
, unsigned n_eq
, unsigned n_ineq
)
2037 return isl_basic_map_extend(base
, 0, n_eq
, n_ineq
);
2040 __isl_give isl_basic_set
*isl_basic_set_extend_constraints(
2041 __isl_take isl_basic_set
*base
, unsigned n_eq
, unsigned n_ineq
)
2043 isl_basic_map
*bmap
= bset_to_bmap(base
);
2044 bmap
= isl_basic_map_extend_constraints(bmap
, n_eq
, n_ineq
);
2045 return bset_from_bmap(bmap
);
2048 __isl_give isl_basic_set
*isl_basic_set_cow(__isl_take isl_basic_set
*bset
)
2050 return bset_from_bmap(isl_basic_map_cow(bset_to_bmap(bset
)));
2053 __isl_give isl_basic_map
*isl_basic_map_cow(__isl_take isl_basic_map
*bmap
)
2058 if (bmap
->ref
> 1) {
2060 bmap
= isl_basic_map_dup(bmap
);
2063 ISL_F_CLR(bmap
, ISL_BASIC_SET_FINAL
);
2064 ISL_F_CLR(bmap
, ISL_BASIC_MAP_REDUCED_COEFFICIENTS
);
2069 /* Clear all cached information in "map", either because it is about
2070 * to be modified or because it is being freed.
2071 * Always return the same pointer that is passed in.
2072 * This is needed for the use in isl_map_free.
2074 static __isl_give isl_map
*clear_caches(__isl_take isl_map
*map
)
2076 isl_basic_map_free(map
->cached_simple_hull
[0]);
2077 isl_basic_map_free(map
->cached_simple_hull
[1]);
2078 map
->cached_simple_hull
[0] = NULL
;
2079 map
->cached_simple_hull
[1] = NULL
;
2083 __isl_give isl_set
*isl_set_cow(__isl_take isl_set
*set
)
2085 return isl_map_cow(set
);
2088 /* Return an isl_map that is equal to "map" and that has only
2089 * a single reference.
2091 * If the original input already has only one reference, then
2092 * simply return it, but clear all cached information, since
2093 * it may be rendered invalid by the operations that will be
2094 * performed on the result.
2096 * Otherwise, create a duplicate (without any cached information).
2098 __isl_give isl_map
*isl_map_cow(__isl_take isl_map
*map
)
2104 return clear_caches(map
);
2106 return isl_map_dup(map
);
2109 static void swap_vars(struct isl_blk blk
, isl_int
*a
,
2110 unsigned a_len
, unsigned b_len
)
2112 isl_seq_cpy(blk
.data
, a
+a_len
, b_len
);
2113 isl_seq_cpy(blk
.data
+b_len
, a
, a_len
);
2114 isl_seq_cpy(a
, blk
.data
, b_len
+a_len
);
2117 static __isl_give isl_basic_map
*isl_basic_map_swap_vars(
2118 __isl_take isl_basic_map
*bmap
, unsigned pos
, unsigned n1
, unsigned n2
)
2123 if (isl_basic_map_check_range(bmap
, isl_dim_all
, pos
- 1, n1
+ n2
) < 0)
2126 if (n1
== 0 || n2
== 0)
2129 bmap
= isl_basic_map_cow(bmap
);
2133 blk
= isl_blk_alloc(bmap
->ctx
, n1
+ n2
);
2134 if (isl_blk_is_error(blk
))
2137 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2139 bmap
->eq
[i
] + pos
, n1
, n2
);
2141 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2143 bmap
->ineq
[i
] + pos
, n1
, n2
);
2145 for (i
= 0; i
< bmap
->n_div
; ++i
)
2147 bmap
->div
[i
]+1 + pos
, n1
, n2
);
2149 isl_blk_free(bmap
->ctx
, blk
);
2151 ISL_F_CLR(bmap
, ISL_BASIC_SET_SORTED
);
2152 bmap
= isl_basic_map_gauss(bmap
, NULL
);
2153 return isl_basic_map_finalize(bmap
);
2155 isl_basic_map_free(bmap
);
2159 /* The given basic map has turned out to be empty.
2160 * Explicitly mark it as such and change the representation
2161 * to a canonical representation of the empty basic map.
2162 * Since the basic map has conflicting constraints,
2163 * it must have at least one constraint, except perhaps
2164 * if it was already explicitly marked as being empty.
2165 * Do nothing in the latter case, i.e., if it has been marked empty and
2166 * has no constraints.
2168 __isl_give isl_basic_map
*isl_basic_map_set_to_empty(
2169 __isl_take isl_basic_map
*bmap
)
2176 n
= isl_basic_map_n_constraint(bmap
);
2177 empty
= isl_basic_map_plain_is_empty(bmap
);
2178 if (n
< 0 || empty
< 0)
2179 return isl_basic_map_free(bmap
);
2180 if (n
== 0 && empty
)
2182 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
2184 return isl_basic_map_free(bmap
);
2185 if (isl_basic_map_free_div(bmap
, bmap
->n_div
) < 0)
2186 return isl_basic_map_free(bmap
);
2187 bmap
= isl_basic_map_free_inequality(bmap
, bmap
->n_ineq
);
2190 if (bmap
->n_eq
> 0) {
2191 bmap
= isl_basic_map_free_equality(bmap
, bmap
->n_eq
- 1);
2195 i
= isl_basic_map_alloc_equality(bmap
);
2199 isl_int_set_si(bmap
->eq
[i
][0], 1);
2200 isl_seq_clr(bmap
->eq
[i
]+1, total
);
2201 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
2202 isl_vec_free(bmap
->sample
);
2203 bmap
->sample
= NULL
;
2204 return isl_basic_map_finalize(bmap
);
2206 isl_basic_map_free(bmap
);
2210 __isl_give isl_basic_set
*isl_basic_set_set_to_empty(
2211 __isl_take isl_basic_set
*bset
)
2213 return bset_from_bmap(isl_basic_map_set_to_empty(bset_to_bmap(bset
)));
2216 __isl_give isl_basic_map
*isl_basic_map_set_rational(
2217 __isl_take isl_basic_map
*bmap
)
2222 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
2225 bmap
= isl_basic_map_cow(bmap
);
2229 ISL_F_SET(bmap
, ISL_BASIC_MAP_RATIONAL
);
2231 return isl_basic_map_finalize(bmap
);
2234 __isl_give isl_basic_set
*isl_basic_set_set_rational(
2235 __isl_take isl_basic_set
*bset
)
2237 return isl_basic_map_set_rational(bset
);
2240 __isl_give isl_basic_set
*isl_basic_set_set_integral(
2241 __isl_take isl_basic_set
*bset
)
2246 if (!ISL_F_ISSET(bset
, ISL_BASIC_MAP_RATIONAL
))
2249 bset
= isl_basic_set_cow(bset
);
2253 ISL_F_CLR(bset
, ISL_BASIC_MAP_RATIONAL
);
2255 return isl_basic_set_finalize(bset
);
2258 __isl_give isl_map
*isl_map_set_rational(__isl_take isl_map
*map
)
2262 map
= isl_map_cow(map
);
2265 for (i
= 0; i
< map
->n
; ++i
) {
2266 map
->p
[i
] = isl_basic_map_set_rational(map
->p
[i
]);
2276 __isl_give isl_set
*isl_set_set_rational(__isl_take isl_set
*set
)
2278 return isl_map_set_rational(set
);
2281 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
2284 static void swap_div(__isl_keep isl_basic_map
*bmap
, int a
, int b
)
2286 isl_int
*t
= bmap
->div
[a
];
2287 bmap
->div
[a
] = bmap
->div
[b
];
2291 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
2292 * div definitions accordingly.
2294 __isl_give isl_basic_map
*isl_basic_map_swap_div(__isl_take isl_basic_map
*bmap
,
2300 off
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
2302 return isl_basic_map_free(bmap
);
2304 swap_div(bmap
, a
, b
);
2306 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2307 isl_int_swap(bmap
->eq
[i
][1+off
+a
], bmap
->eq
[i
][1+off
+b
]);
2309 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2310 isl_int_swap(bmap
->ineq
[i
][1+off
+a
], bmap
->ineq
[i
][1+off
+b
]);
2312 for (i
= 0; i
< bmap
->n_div
; ++i
)
2313 isl_int_swap(bmap
->div
[i
][1+1+off
+a
], bmap
->div
[i
][1+1+off
+b
]);
2314 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
2319 static void constraint_drop_vars(isl_int
*c
, unsigned n
, unsigned rem
)
2321 isl_seq_cpy(c
, c
+ n
, rem
);
2322 isl_seq_clr(c
+ rem
, n
);
2325 /* Drop n dimensions starting at first.
2327 * In principle, this frees up some extra variables as the number
2328 * of columns remains constant, but we would have to extend
2329 * the div array too as the number of rows in this array is assumed
2330 * to be equal to extra.
2332 __isl_give isl_basic_set
*isl_basic_set_drop_dims(
2333 __isl_take isl_basic_set
*bset
, unsigned first
, unsigned n
)
2335 return isl_basic_map_drop(bset_to_bmap(bset
), isl_dim_set
, first
, n
);
2338 /* Move "n" divs starting at "first" to the end of the list of divs.
2340 static __isl_give isl_basic_map
*move_divs_last(__isl_take isl_basic_map
*bmap
,
2341 unsigned first
, unsigned n
)
2346 if (first
+ n
== bmap
->n_div
)
2349 div
= isl_alloc_array(bmap
->ctx
, isl_int
*, n
);
2352 for (i
= 0; i
< n
; ++i
)
2353 div
[i
] = bmap
->div
[first
+ i
];
2354 for (i
= 0; i
< bmap
->n_div
- first
- n
; ++i
)
2355 bmap
->div
[first
+ i
] = bmap
->div
[first
+ n
+ i
];
2356 for (i
= 0; i
< n
; ++i
)
2357 bmap
->div
[bmap
->n_div
- n
+ i
] = div
[i
];
2361 isl_basic_map_free(bmap
);
2366 #define TYPE isl_map
2368 #include "check_type_range_templ.c"
2370 /* Check that there are "n" dimensions of type "type" starting at "first"
2373 isl_stat
isl_set_check_range(__isl_keep isl_set
*set
,
2374 enum isl_dim_type type
, unsigned first
, unsigned n
)
2376 return isl_map_check_range(set_to_map(set
), type
, first
, n
);
2379 /* Drop "n" dimensions of type "type" starting at "first".
2380 * Perform the core computation, without cowing or
2381 * simplifying and finalizing the result.
2383 * In principle, this frees up some extra variables as the number
2384 * of columns remains constant, but we would have to extend
2385 * the div array too as the number of rows in this array is assumed
2386 * to be equal to extra.
2388 __isl_give isl_basic_map
*isl_basic_map_drop_core(
2389 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
2390 unsigned first
, unsigned n
)
2397 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2398 return isl_basic_map_free(bmap
);
2400 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
2402 return isl_basic_map_free(bmap
);
2404 offset
= isl_basic_map_offset(bmap
, type
) + first
;
2405 left
= total
- (offset
- 1) - n
;
2406 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2407 constraint_drop_vars(bmap
->eq
[i
]+offset
, n
, left
);
2409 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2410 constraint_drop_vars(bmap
->ineq
[i
]+offset
, n
, left
);
2412 for (i
= 0; i
< bmap
->n_div
; ++i
)
2413 constraint_drop_vars(bmap
->div
[i
]+1+offset
, n
, left
);
2415 if (type
== isl_dim_div
) {
2416 bmap
= move_divs_last(bmap
, first
, n
);
2419 if (isl_basic_map_free_div(bmap
, n
) < 0)
2420 return isl_basic_map_free(bmap
);
2422 bmap
->dim
= isl_space_drop_dims(bmap
->dim
, type
, first
, n
);
2424 return isl_basic_map_free(bmap
);
2426 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
2427 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
2431 /* Drop "n" dimensions of type "type" starting at "first".
2433 * In principle, this frees up some extra variables as the number
2434 * of columns remains constant, but we would have to extend
2435 * the div array too as the number of rows in this array is assumed
2436 * to be equal to extra.
2438 __isl_give isl_basic_map
*isl_basic_map_drop(__isl_take isl_basic_map
*bmap
,
2439 enum isl_dim_type type
, unsigned first
, unsigned n
)
2443 if (n
== 0 && !isl_space_is_named_or_nested(bmap
->dim
, type
))
2446 bmap
= isl_basic_map_cow(bmap
);
2450 bmap
= isl_basic_map_drop_core(bmap
, type
, first
, n
);
2452 bmap
= isl_basic_map_simplify(bmap
);
2453 return isl_basic_map_finalize(bmap
);
2456 __isl_give isl_basic_set
*isl_basic_set_drop(__isl_take isl_basic_set
*bset
,
2457 enum isl_dim_type type
, unsigned first
, unsigned n
)
2459 return bset_from_bmap(isl_basic_map_drop(bset_to_bmap(bset
),
2463 /* No longer consider "map" to be normalized.
2465 static __isl_give isl_map
*isl_map_unmark_normalized(__isl_take isl_map
*map
)
2469 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
2473 __isl_give isl_map
*isl_map_drop(__isl_take isl_map
*map
,
2474 enum isl_dim_type type
, unsigned first
, unsigned n
)
2479 if (isl_map_check_range(map
, type
, first
, n
) < 0)
2480 return isl_map_free(map
);
2482 if (n
== 0 && !isl_space_is_named_or_nested(map
->dim
, type
))
2484 map
= isl_map_cow(map
);
2488 for (i
= 0; i
< map
->n
; ++i
) {
2489 map
->p
[i
] = isl_basic_map_drop(map
->p
[i
], type
, first
, n
);
2493 map
= isl_map_unmark_normalized(map
);
2495 space
= isl_map_take_space(map
);
2496 space
= isl_space_drop_dims(space
, type
, first
, n
);
2497 map
= isl_map_restore_space(map
, space
);
2505 __isl_give isl_set
*isl_set_drop(__isl_take isl_set
*set
,
2506 enum isl_dim_type type
, unsigned first
, unsigned n
)
2508 return set_from_map(isl_map_drop(set_to_map(set
), type
, first
, n
));
2511 /* Drop the integer division at position "div", which is assumed
2512 * not to appear in any of the constraints or
2513 * in any of the other integer divisions.
2515 * Since the integer division is redundant, there is no need to cow.
2517 __isl_give isl_basic_map
*isl_basic_map_drop_div(
2518 __isl_take isl_basic_map
*bmap
, unsigned div
)
2520 return isl_basic_map_drop_core(bmap
, isl_dim_div
, div
, 1);
2523 /* Eliminate the specified n dimensions starting at first from the
2524 * constraints, without removing the dimensions from the space.
2525 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2527 __isl_give isl_map
*isl_map_eliminate(__isl_take isl_map
*map
,
2528 enum isl_dim_type type
, unsigned first
, unsigned n
)
2535 if (isl_map_check_range(map
, type
, first
, n
) < 0)
2536 return isl_map_free(map
);
2538 map
= isl_map_cow(map
);
2542 for (i
= 0; i
< map
->n
; ++i
) {
2543 map
->p
[i
] = isl_basic_map_eliminate(map
->p
[i
], type
, first
, n
);
2553 /* Eliminate the specified n dimensions starting at first from the
2554 * constraints, without removing the dimensions from the space.
2555 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2557 __isl_give isl_set
*isl_set_eliminate(__isl_take isl_set
*set
,
2558 enum isl_dim_type type
, unsigned first
, unsigned n
)
2560 return set_from_map(isl_map_eliminate(set_to_map(set
), type
, first
, n
));
2563 /* Eliminate the specified n dimensions starting at first from the
2564 * constraints, without removing the dimensions from the space.
2565 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2567 __isl_give isl_set
*isl_set_eliminate_dims(__isl_take isl_set
*set
,
2568 unsigned first
, unsigned n
)
2570 return isl_set_eliminate(set
, isl_dim_set
, first
, n
);
2573 __isl_give isl_basic_map
*isl_basic_map_remove_divs(
2574 __isl_take isl_basic_map
*bmap
)
2578 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
2580 return isl_basic_map_free(bmap
);
2581 bmap
= isl_basic_map_eliminate_vars(bmap
, v_div
, bmap
->n_div
);
2585 return isl_basic_map_finalize(bmap
);
2588 __isl_give isl_basic_set
*isl_basic_set_remove_divs(
2589 __isl_take isl_basic_set
*bset
)
2591 return bset_from_bmap(isl_basic_map_remove_divs(bset_to_bmap(bset
)));
2594 __isl_give isl_map
*isl_map_remove_divs(__isl_take isl_map
*map
)
2603 map
= isl_map_cow(map
);
2607 for (i
= 0; i
< map
->n
; ++i
) {
2608 map
->p
[i
] = isl_basic_map_remove_divs(map
->p
[i
]);
2618 __isl_give isl_set
*isl_set_remove_divs(__isl_take isl_set
*set
)
2620 return isl_map_remove_divs(set
);
2623 __isl_give isl_basic_map
*isl_basic_map_remove_dims(
2624 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
2625 unsigned first
, unsigned n
)
2627 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2628 return isl_basic_map_free(bmap
);
2629 if (n
== 0 && !isl_space_is_named_or_nested(bmap
->dim
, type
))
2631 bmap
= isl_basic_map_eliminate_vars(bmap
,
2632 isl_basic_map_offset(bmap
, type
) - 1 + first
, n
);
2635 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
) && type
== isl_dim_div
)
2637 bmap
= isl_basic_map_drop(bmap
, type
, first
, n
);
2641 /* Return true if the definition of the given div (recursively) involves
2642 * any of the given variables.
2644 static isl_bool
div_involves_vars(__isl_keep isl_basic_map
*bmap
, int div
,
2645 unsigned first
, unsigned n
)
2648 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
2650 if (isl_int_is_zero(bmap
->div
[div
][0]))
2651 return isl_bool_false
;
2652 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + first
, n
) >= 0)
2653 return isl_bool_true
;
2655 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2658 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
2660 involves
= div_involves_vars(bmap
, i
, first
, n
);
2661 if (involves
< 0 || involves
)
2665 return isl_bool_false
;
2668 /* Try and add a lower and/or upper bound on "div" to "bmap"
2669 * based on inequality "i".
2670 * "total" is the total number of variables (excluding the divs).
2671 * "v" is a temporary object that can be used during the calculations.
2672 * If "lb" is set, then a lower bound should be constructed.
2673 * If "ub" is set, then an upper bound should be constructed.
2675 * The calling function has already checked that the inequality does not
2676 * reference "div", but we still need to check that the inequality is
2677 * of the right form. We'll consider the case where we want to construct
2678 * a lower bound. The construction of upper bounds is similar.
2680 * Let "div" be of the form
2682 * q = floor((a + f(x))/d)
2684 * We essentially check if constraint "i" is of the form
2688 * so that we can use it to derive a lower bound on "div".
2689 * However, we allow a slightly more general form
2693 * with the condition that the coefficients of g(x) - f(x) are all
2695 * Rewriting this constraint as
2699 * adding a + f(x) to both sides and dividing by d, we obtain
2701 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
2703 * Taking the floor on both sides, we obtain
2705 * q >= floor((a-b)/d) + (f(x)-g(x))/d
2709 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
2711 * In the case of an upper bound, we construct the constraint
2713 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
2716 static __isl_give isl_basic_map
*insert_bounds_on_div_from_ineq(
2717 __isl_take isl_basic_map
*bmap
, int div
, int i
,
2718 unsigned total
, isl_int v
, int lb
, int ub
)
2722 for (j
= 0; (lb
|| ub
) && j
< total
+ bmap
->n_div
; ++j
) {
2724 isl_int_sub(v
, bmap
->ineq
[i
][1 + j
],
2725 bmap
->div
[div
][1 + 1 + j
]);
2726 lb
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
2729 isl_int_add(v
, bmap
->ineq
[i
][1 + j
],
2730 bmap
->div
[div
][1 + 1 + j
]);
2731 ub
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
2737 bmap
= isl_basic_map_cow(bmap
);
2738 bmap
= isl_basic_map_extend_constraints(bmap
, 0, lb
+ ub
);
2740 int k
= isl_basic_map_alloc_inequality(bmap
);
2743 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
2744 isl_int_sub(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
2745 bmap
->div
[div
][1 + j
]);
2746 isl_int_cdiv_q(bmap
->ineq
[k
][j
],
2747 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
2749 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], 1);
2752 int k
= isl_basic_map_alloc_inequality(bmap
);
2755 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
2756 isl_int_add(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
2757 bmap
->div
[div
][1 + j
]);
2758 isl_int_fdiv_q(bmap
->ineq
[k
][j
],
2759 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
2761 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], -1);
2766 isl_basic_map_free(bmap
);
2770 /* This function is called right before "div" is eliminated from "bmap"
2771 * using Fourier-Motzkin.
2772 * Look through the constraints of "bmap" for constraints on the argument
2773 * of the integer division and use them to construct constraints on the
2774 * integer division itself. These constraints can then be combined
2775 * during the Fourier-Motzkin elimination.
2776 * Note that it is only useful to introduce lower bounds on "div"
2777 * if "bmap" already contains upper bounds on "div" as the newly
2778 * introduce lower bounds can then be combined with the pre-existing
2779 * upper bounds. Similarly for upper bounds.
2780 * We therefore first check if "bmap" contains any lower and/or upper bounds
2783 * It is interesting to note that the introduction of these constraints
2784 * can indeed lead to more accurate results, even when compared to
2785 * deriving constraints on the argument of "div" from constraints on "div".
2786 * Consider, for example, the set
2788 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2790 * The second constraint can be rewritten as
2792 * 2 * [(-i-2j+3)/4] + k >= 0
2794 * from which we can derive
2796 * -i - 2j + 3 >= -2k
2802 * Combined with the first constraint, we obtain
2804 * -3 <= 3 + 2k or k >= -3
2806 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2807 * the first constraint, we obtain
2809 * [(i + 2j)/4] >= [-3/4] = -1
2811 * Combining this constraint with the second constraint, we obtain
2815 static __isl_give isl_basic_map
*insert_bounds_on_div(
2816 __isl_take isl_basic_map
*bmap
, int div
)
2819 int check_lb
, check_ub
;
2826 if (isl_int_is_zero(bmap
->div
[div
][0]))
2829 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
2831 return isl_basic_map_free(bmap
);
2835 for (i
= 0; (!check_lb
|| !check_ub
) && i
< bmap
->n_ineq
; ++i
) {
2836 int s
= isl_int_sgn(bmap
->ineq
[i
][1 + v_div
+ div
]);
2843 if (!check_lb
&& !check_ub
)
2848 for (i
= 0; bmap
&& i
< bmap
->n_ineq
; ++i
) {
2849 if (!isl_int_is_zero(bmap
->ineq
[i
][1 + v_div
+ div
]))
2852 bmap
= insert_bounds_on_div_from_ineq(bmap
, div
, i
, v_div
, v
,
2853 check_lb
, check_ub
);
2861 /* Remove all divs (recursively) involving any of the given dimensions
2862 * in their definitions.
2864 __isl_give isl_basic_map
*isl_basic_map_remove_divs_involving_dims(
2865 __isl_take isl_basic_map
*bmap
,
2866 enum isl_dim_type type
, unsigned first
, unsigned n
)
2870 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2871 return isl_basic_map_free(bmap
);
2872 first
+= isl_basic_map_offset(bmap
, type
);
2874 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2877 involves
= div_involves_vars(bmap
, i
, first
, n
);
2879 return isl_basic_map_free(bmap
);
2882 bmap
= insert_bounds_on_div(bmap
, i
);
2883 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
2892 __isl_give isl_basic_set
*isl_basic_set_remove_divs_involving_dims(
2893 __isl_take isl_basic_set
*bset
,
2894 enum isl_dim_type type
, unsigned first
, unsigned n
)
2896 return isl_basic_map_remove_divs_involving_dims(bset
, type
, first
, n
);
2899 __isl_give isl_map
*isl_map_remove_divs_involving_dims(__isl_take isl_map
*map
,
2900 enum isl_dim_type type
, unsigned first
, unsigned n
)
2909 map
= isl_map_cow(map
);
2913 for (i
= 0; i
< map
->n
; ++i
) {
2914 map
->p
[i
] = isl_basic_map_remove_divs_involving_dims(map
->p
[i
],
2925 __isl_give isl_set
*isl_set_remove_divs_involving_dims(__isl_take isl_set
*set
,
2926 enum isl_dim_type type
, unsigned first
, unsigned n
)
2928 return set_from_map(isl_map_remove_divs_involving_dims(set_to_map(set
),
2932 /* Does the description of "bmap" depend on the specified dimensions?
2933 * We also check whether the dimensions appear in any of the div definitions.
2934 * In principle there is no need for this check. If the dimensions appear
2935 * in a div definition, they also appear in the defining constraints of that
2938 isl_bool
isl_basic_map_involves_dims(__isl_keep isl_basic_map
*bmap
,
2939 enum isl_dim_type type
, unsigned first
, unsigned n
)
2943 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2944 return isl_bool_error
;
2946 first
+= isl_basic_map_offset(bmap
, type
);
2947 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2948 if (isl_seq_first_non_zero(bmap
->eq
[i
] + first
, n
) >= 0)
2949 return isl_bool_true
;
2950 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2951 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + first
, n
) >= 0)
2952 return isl_bool_true
;
2953 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2954 if (isl_int_is_zero(bmap
->div
[i
][0]))
2956 if (isl_seq_first_non_zero(bmap
->div
[i
] + 1 + first
, n
) >= 0)
2957 return isl_bool_true
;
2960 return isl_bool_false
;
2963 isl_bool
isl_map_involves_dims(__isl_keep isl_map
*map
,
2964 enum isl_dim_type type
, unsigned first
, unsigned n
)
2968 if (isl_map_check_range(map
, type
, first
, n
) < 0)
2969 return isl_bool_error
;
2971 for (i
= 0; i
< map
->n
; ++i
) {
2972 isl_bool involves
= isl_basic_map_involves_dims(map
->p
[i
],
2974 if (involves
< 0 || involves
)
2978 return isl_bool_false
;
2981 isl_bool
isl_basic_set_involves_dims(__isl_keep isl_basic_set
*bset
,
2982 enum isl_dim_type type
, unsigned first
, unsigned n
)
2984 return isl_basic_map_involves_dims(bset
, type
, first
, n
);
2987 isl_bool
isl_set_involves_dims(__isl_keep isl_set
*set
,
2988 enum isl_dim_type type
, unsigned first
, unsigned n
)
2990 return isl_map_involves_dims(set
, type
, first
, n
);
2993 /* Does "bset" involve any local variables, i.e., integer divisions?
2995 static isl_bool
isl_basic_set_involves_locals(__isl_keep isl_basic_set
*bset
)
2999 n
= isl_basic_set_dim(bset
, isl_dim_div
);
3001 return isl_bool_error
;
3002 return isl_bool_ok(n
> 0);
3005 /* isl_set_every_basic_set callback that checks whether "bset"
3006 * is free of local variables.
3008 static isl_bool
basic_set_no_locals(__isl_keep isl_basic_set
*bset
, void *user
)
3010 return isl_bool_not(isl_basic_set_involves_locals(bset
));
3013 /* Does "set" involve any local variables, i.e., integer divisions?
3015 isl_bool
isl_set_involves_locals(__isl_keep isl_set
*set
)
3019 no_locals
= isl_set_every_basic_set(set
, &basic_set_no_locals
, NULL
);
3020 return isl_bool_not(no_locals
);
3023 /* Drop all constraints in bmap that involve any of the dimensions
3024 * first to first+n-1.
3025 * This function only performs the actual removal of constraints.
3027 * This function should not call finalize since it is used by
3028 * remove_redundant_divs, which in turn is called by isl_basic_map_finalize.
3030 __isl_give isl_basic_map
*isl_basic_map_drop_constraints_involving(
3031 __isl_take isl_basic_map
*bmap
, unsigned first
, unsigned n
)
3038 bmap
= isl_basic_map_cow(bmap
);
3043 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
) {
3044 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + first
, n
) == -1)
3046 if (isl_basic_map_drop_equality(bmap
, i
) < 0)
3047 return isl_basic_map_free(bmap
);
3050 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
3051 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + 1 + first
, n
) == -1)
3053 if (isl_basic_map_drop_inequality(bmap
, i
) < 0)
3054 return isl_basic_map_free(bmap
);
3060 /* Drop all constraints in bset that involve any of the dimensions
3061 * first to first+n-1.
3062 * This function only performs the actual removal of constraints.
3064 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_involving(
3065 __isl_take isl_basic_set
*bset
, unsigned first
, unsigned n
)
3067 return isl_basic_map_drop_constraints_involving(bset
, first
, n
);
3070 /* Drop all constraints in bmap that do not involve any of the dimensions
3071 * first to first + n - 1 of the given type.
3073 __isl_give isl_basic_map
*isl_basic_map_drop_constraints_not_involving_dims(
3074 __isl_take isl_basic_map
*bmap
,
3075 enum isl_dim_type type
, unsigned first
, unsigned n
)
3080 isl_space
*space
= isl_basic_map_get_space(bmap
);
3081 isl_basic_map_free(bmap
);
3082 return isl_basic_map_universe(space
);
3084 bmap
= isl_basic_map_cow(bmap
);
3088 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
3089 return isl_basic_map_free(bmap
);
3091 first
+= isl_basic_map_offset(bmap
, type
) - 1;
3093 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
) {
3094 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + first
, n
) != -1)
3096 if (isl_basic_map_drop_equality(bmap
, i
) < 0)
3097 return isl_basic_map_free(bmap
);
3100 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
3101 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + 1 + first
, n
) != -1)
3103 if (isl_basic_map_drop_inequality(bmap
, i
) < 0)
3104 return isl_basic_map_free(bmap
);
3107 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
3111 /* Drop all constraints in bset that do not involve any of the dimensions
3112 * first to first + n - 1 of the given type.
3114 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_not_involving_dims(
3115 __isl_take isl_basic_set
*bset
,
3116 enum isl_dim_type type
, unsigned first
, unsigned n
)
3118 return isl_basic_map_drop_constraints_not_involving_dims(bset
,
3122 /* Drop all constraints in bmap that involve any of the dimensions
3123 * first to first + n - 1 of the given type.
3125 __isl_give isl_basic_map
*isl_basic_map_drop_constraints_involving_dims(
3126 __isl_take isl_basic_map
*bmap
,
3127 enum isl_dim_type type
, unsigned first
, unsigned n
)
3134 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
3135 return isl_basic_map_free(bmap
);
3137 bmap
= isl_basic_map_remove_divs_involving_dims(bmap
, type
, first
, n
);
3138 first
+= isl_basic_map_offset(bmap
, type
) - 1;
3139 bmap
= isl_basic_map_drop_constraints_involving(bmap
, first
, n
);
3140 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
3144 /* Drop all constraints in bset that involve any of the dimensions
3145 * first to first + n - 1 of the given type.
3147 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_involving_dims(
3148 __isl_take isl_basic_set
*bset
,
3149 enum isl_dim_type type
, unsigned first
, unsigned n
)
3151 return isl_basic_map_drop_constraints_involving_dims(bset
,
3155 /* Drop constraints from "map" by applying "drop" to each basic map.
3157 static __isl_give isl_map
*drop_constraints(__isl_take isl_map
*map
,
3158 enum isl_dim_type type
, unsigned first
, unsigned n
,
3159 __isl_give isl_basic_map
*(*drop
)(__isl_take isl_basic_map
*bmap
,
3160 enum isl_dim_type type
, unsigned first
, unsigned n
))
3164 if (isl_map_check_range(map
, type
, first
, n
) < 0)
3165 return isl_map_free(map
);
3167 map
= isl_map_cow(map
);
3171 for (i
= 0; i
< map
->n
; ++i
) {
3172 map
->p
[i
] = drop(map
->p
[i
], type
, first
, n
);
3174 return isl_map_free(map
);
3178 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
3183 /* Drop all constraints in map that involve any of the dimensions
3184 * first to first + n - 1 of the given type.
3186 __isl_give isl_map
*isl_map_drop_constraints_involving_dims(
3187 __isl_take isl_map
*map
,
3188 enum isl_dim_type type
, unsigned first
, unsigned n
)
3192 return drop_constraints(map
, type
, first
, n
,
3193 &isl_basic_map_drop_constraints_involving_dims
);
3196 /* Drop all constraints in "map" that do not involve any of the dimensions
3197 * first to first + n - 1 of the given type.
3199 __isl_give isl_map
*isl_map_drop_constraints_not_involving_dims(
3200 __isl_take isl_map
*map
,
3201 enum isl_dim_type type
, unsigned first
, unsigned n
)
3204 isl_space
*space
= isl_map_get_space(map
);
3206 return isl_map_universe(space
);
3208 return drop_constraints(map
, type
, first
, n
,
3209 &isl_basic_map_drop_constraints_not_involving_dims
);
3212 /* Drop all constraints in set that involve any of the dimensions
3213 * first to first + n - 1 of the given type.
3215 __isl_give isl_set
*isl_set_drop_constraints_involving_dims(
3216 __isl_take isl_set
*set
,
3217 enum isl_dim_type type
, unsigned first
, unsigned n
)
3219 return isl_map_drop_constraints_involving_dims(set
, type
, first
, n
);
3222 /* Drop all constraints in "set" that do not involve any of the dimensions
3223 * first to first + n - 1 of the given type.
3225 __isl_give isl_set
*isl_set_drop_constraints_not_involving_dims(
3226 __isl_take isl_set
*set
,
3227 enum isl_dim_type type
, unsigned first
, unsigned n
)
3229 return isl_map_drop_constraints_not_involving_dims(set
, type
, first
, n
);
3232 /* Does local variable "div" of "bmap" have a complete explicit representation?
3233 * Having a complete explicit representation requires not only
3234 * an explicit representation, but also that all local variables
3235 * that appear in this explicit representation in turn have
3236 * a complete explicit representation.
3238 isl_bool
isl_basic_map_div_is_known(__isl_keep isl_basic_map
*bmap
, int div
)
3241 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
3244 marked
= isl_basic_map_div_is_marked_unknown(bmap
, div
);
3245 if (marked
< 0 || marked
)
3246 return isl_bool_not(marked
);
3248 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
3251 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
3253 known
= isl_basic_map_div_is_known(bmap
, i
);
3254 if (known
< 0 || !known
)
3258 return isl_bool_true
;
3261 /* Remove all divs that are unknown or defined in terms of unknown divs.
3263 __isl_give isl_basic_map
*isl_basic_map_remove_unknown_divs(
3264 __isl_take isl_basic_map
*bmap
)
3271 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
3272 if (isl_basic_map_div_is_known(bmap
, i
))
3274 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
3283 /* Remove all divs that are unknown or defined in terms of unknown divs.
3285 __isl_give isl_basic_set
*isl_basic_set_remove_unknown_divs(
3286 __isl_take isl_basic_set
*bset
)
3288 return isl_basic_map_remove_unknown_divs(bset
);
3291 __isl_give isl_map
*isl_map_remove_unknown_divs(__isl_take isl_map
*map
)
3300 map
= isl_map_cow(map
);
3304 for (i
= 0; i
< map
->n
; ++i
) {
3305 map
->p
[i
] = isl_basic_map_remove_unknown_divs(map
->p
[i
]);
3315 __isl_give isl_set
*isl_set_remove_unknown_divs(__isl_take isl_set
*set
)
3317 return set_from_map(isl_map_remove_unknown_divs(set_to_map(set
)));
3320 __isl_give isl_basic_set
*isl_basic_set_remove_dims(
3321 __isl_take isl_basic_set
*bset
,
3322 enum isl_dim_type type
, unsigned first
, unsigned n
)
3324 isl_basic_map
*bmap
= bset_to_bmap(bset
);
3325 bmap
= isl_basic_map_remove_dims(bmap
, type
, first
, n
);
3326 return bset_from_bmap(bmap
);
3329 __isl_give isl_map
*isl_map_remove_dims(__isl_take isl_map
*map
,
3330 enum isl_dim_type type
, unsigned first
, unsigned n
)
3337 map
= isl_map_cow(map
);
3338 if (isl_map_check_range(map
, type
, first
, n
) < 0)
3339 return isl_map_free(map
);
3341 for (i
= 0; i
< map
->n
; ++i
) {
3342 map
->p
[i
] = isl_basic_map_eliminate_vars(map
->p
[i
],
3343 isl_basic_map_offset(map
->p
[i
], type
) - 1 + first
, n
);
3347 map
= isl_map_drop(map
, type
, first
, n
);
3354 __isl_give isl_set
*isl_set_remove_dims(__isl_take isl_set
*bset
,
3355 enum isl_dim_type type
, unsigned first
, unsigned n
)
3357 return set_from_map(isl_map_remove_dims(set_to_map(bset
),
3361 /* Project out n inputs starting at first using Fourier-Motzkin */
3362 __isl_give isl_map
*isl_map_remove_inputs(__isl_take isl_map
*map
,
3363 unsigned first
, unsigned n
)
3365 return isl_map_remove_dims(map
, isl_dim_in
, first
, n
);
3368 void isl_basic_set_print_internal(__isl_keep isl_basic_set
*bset
,
3369 FILE *out
, int indent
)
3374 fprintf(out
, "null basic set\n");
3378 fprintf(out
, "%*s", indent
, "");
3379 fprintf(out
, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
3380 bset
->ref
, bset
->dim
->nparam
, bset
->dim
->n_out
,
3381 bset
->extra
, bset
->flags
);
3383 p
= isl_printer_to_file(isl_basic_set_get_ctx(bset
), out
);
3384 p
= isl_printer_set_dump(p
, 1);
3385 p
= isl_printer_set_indent(p
, indent
);
3386 p
= isl_printer_start_line(p
);
3387 p
= isl_printer_print_basic_set(p
, bset
);
3388 p
= isl_printer_end_line(p
);
3389 isl_printer_free(p
);
3392 void isl_basic_map_print_internal(__isl_keep isl_basic_map
*bmap
,
3393 FILE *out
, int indent
)
3398 fprintf(out
, "null basic map\n");
3402 fprintf(out
, "%*s", indent
, "");
3403 fprintf(out
, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
3404 "flags: %x, n_name: %d\n",
3406 bmap
->dim
->nparam
, bmap
->dim
->n_in
, bmap
->dim
->n_out
,
3407 bmap
->extra
, bmap
->flags
, bmap
->dim
->n_id
);
3409 p
= isl_printer_to_file(isl_basic_map_get_ctx(bmap
), out
);
3410 p
= isl_printer_set_dump(p
, 1);
3411 p
= isl_printer_set_indent(p
, indent
);
3412 p
= isl_printer_start_line(p
);
3413 p
= isl_printer_print_basic_map(p
, bmap
);
3414 p
= isl_printer_end_line(p
);
3415 isl_printer_free(p
);
3418 __isl_give isl_basic_map
*isl_inequality_negate(__isl_take isl_basic_map
*bmap
,
3423 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
3425 return isl_basic_map_free(bmap
);
3426 if (pos
>= bmap
->n_ineq
)
3427 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
3428 "invalid position", return isl_basic_map_free(bmap
));
3429 isl_seq_neg(bmap
->ineq
[pos
], bmap
->ineq
[pos
], 1 + total
);
3430 isl_int_sub_ui(bmap
->ineq
[pos
][0], bmap
->ineq
[pos
][0], 1);
3431 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
3432 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
3436 __isl_give isl_set
*isl_set_alloc_space(__isl_take isl_space
*space
, int n
,
3439 if (isl_space_check_is_set(space
) < 0)
3441 return isl_map_alloc_space(space
, n
, flags
);
3443 isl_space_free(space
);
3447 /* Make sure "map" has room for at least "n" more basic maps.
3449 __isl_give isl_map
*isl_map_grow(__isl_take isl_map
*map
, int n
)
3452 struct isl_map
*grown
= NULL
;
3456 isl_assert(map
->ctx
, n
>= 0, goto error
);
3457 if (map
->n
+ n
<= map
->size
)
3459 grown
= isl_map_alloc_space(isl_map_get_space(map
), map
->n
+ n
, map
->flags
);
3462 for (i
= 0; i
< map
->n
; ++i
) {
3463 grown
->p
[i
] = isl_basic_map_copy(map
->p
[i
]);
3471 isl_map_free(grown
);
3476 /* Make sure "set" has room for at least "n" more basic sets.
3478 __isl_give isl_set
*isl_set_grow(__isl_take isl_set
*set
, int n
)
3480 return set_from_map(isl_map_grow(set_to_map(set
), n
));
3483 __isl_give isl_set
*isl_set_from_basic_set(__isl_take isl_basic_set
*bset
)
3485 return isl_map_from_basic_map(bset
);
3488 /* This function performs the same operation as isl_set_from_basic_set,
3489 * but is considered as a function on an isl_basic_set when exported.
3491 __isl_give isl_set
*isl_basic_set_to_set(__isl_take isl_basic_set
*bset
)
3493 return isl_set_from_basic_set(bset
);
3496 __isl_give isl_map
*isl_map_from_basic_map(__isl_take isl_basic_map
*bmap
)
3498 struct isl_map
*map
;
3503 map
= isl_map_alloc_space(isl_space_copy(bmap
->dim
), 1, ISL_MAP_DISJOINT
);
3504 return isl_map_add_basic_map(map
, bmap
);
3507 __isl_give isl_set
*isl_set_add_basic_set(__isl_take isl_set
*set
,
3508 __isl_take isl_basic_set
*bset
)
3510 return set_from_map(isl_map_add_basic_map(set_to_map(set
),
3511 bset_to_bmap(bset
)));
3514 __isl_null isl_set
*isl_set_free(__isl_take isl_set
*set
)
3516 return isl_map_free(set
);
3519 void isl_set_print_internal(__isl_keep isl_set
*set
, FILE *out
, int indent
)
3524 fprintf(out
, "null set\n");
3528 fprintf(out
, "%*s", indent
, "");
3529 fprintf(out
, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
3530 set
->ref
, set
->n
, set
->dim
->nparam
, set
->dim
->n_out
,
3532 for (i
= 0; i
< set
->n
; ++i
) {
3533 fprintf(out
, "%*s", indent
, "");
3534 fprintf(out
, "basic set %d:\n", i
);
3535 isl_basic_set_print_internal(set
->p
[i
], out
, indent
+4);
3539 void isl_map_print_internal(__isl_keep isl_map
*map
, FILE *out
, int indent
)
3544 fprintf(out
, "null map\n");
3548 fprintf(out
, "%*s", indent
, "");
3549 fprintf(out
, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
3550 "flags: %x, n_name: %d\n",
3551 map
->ref
, map
->n
, map
->dim
->nparam
, map
->dim
->n_in
,
3552 map
->dim
->n_out
, map
->flags
, map
->dim
->n_id
);
3553 for (i
= 0; i
< map
->n
; ++i
) {
3554 fprintf(out
, "%*s", indent
, "");
3555 fprintf(out
, "basic map %d:\n", i
);
3556 isl_basic_map_print_internal(map
->p
[i
], out
, indent
+4);
3560 /* Check that the space of "bset" is the same as that of the domain of "bmap".
3562 static isl_stat
isl_basic_map_check_compatible_domain(
3563 __isl_keep isl_basic_map
*bmap
, __isl_keep isl_basic_set
*bset
)
3567 ok
= isl_basic_map_compatible_domain(bmap
, bset
);
3569 return isl_stat_error
;
3571 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
3572 "incompatible spaces", return isl_stat_error
);
3577 __isl_give isl_basic_map
*isl_basic_map_intersect_domain(
3578 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*bset
)
3580 struct isl_basic_map
*bmap_domain
;
3583 if (isl_basic_map_check_equal_params(bmap
, bset_to_bmap(bset
)) < 0)
3586 dim
= isl_basic_set_dim(bset
, isl_dim_set
);
3590 isl_basic_map_check_compatible_domain(bmap
, bset
) < 0)
3593 bmap
= isl_basic_map_cow(bmap
);
3596 bmap
= isl_basic_map_extend(bmap
,
3597 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
3598 bmap_domain
= isl_basic_map_from_domain(bset
);
3599 bmap
= add_constraints(bmap
, bmap_domain
, 0, 0);
3601 bmap
= isl_basic_map_simplify(bmap
);
3602 return isl_basic_map_finalize(bmap
);
3604 isl_basic_map_free(bmap
);
3605 isl_basic_set_free(bset
);
3609 /* Check that the space of "bset" is the same as that of the range of "bmap".
3611 static isl_stat
isl_basic_map_check_compatible_range(
3612 __isl_keep isl_basic_map
*bmap
, __isl_keep isl_basic_set
*bset
)
3616 ok
= isl_basic_map_compatible_range(bmap
, bset
);
3618 return isl_stat_error
;
3620 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
3621 "incompatible spaces", return isl_stat_error
);
3626 __isl_give isl_basic_map
*isl_basic_map_intersect_range(
3627 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*bset
)
3629 struct isl_basic_map
*bmap_range
;
3632 if (isl_basic_map_check_equal_params(bmap
, bset_to_bmap(bset
)) < 0)
3635 dim
= isl_basic_set_dim(bset
, isl_dim_set
);
3638 if (dim
!= 0 && isl_basic_map_check_compatible_range(bmap
, bset
) < 0)
3641 if (isl_basic_set_plain_is_universe(bset
)) {
3642 isl_basic_set_free(bset
);
3646 bmap
= isl_basic_map_cow(bmap
);
3649 bmap
= isl_basic_map_extend(bmap
,
3650 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
3651 bmap_range
= bset_to_bmap(bset
);
3652 bmap
= add_constraints(bmap
, bmap_range
, 0, 0);
3654 bmap
= isl_basic_map_simplify(bmap
);
3655 return isl_basic_map_finalize(bmap
);
3657 isl_basic_map_free(bmap
);
3658 isl_basic_set_free(bset
);
3662 isl_bool
isl_basic_map_contains(__isl_keep isl_basic_map
*bmap
,
3663 __isl_keep isl_vec
*vec
)
3669 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
3670 if (total
< 0 || !vec
)
3671 return isl_bool_error
;
3673 if (1 + total
!= vec
->size
)
3674 return isl_bool_false
;
3678 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
3679 isl_seq_inner_product(vec
->el
, bmap
->eq
[i
], 1 + total
, &s
);
3680 if (!isl_int_is_zero(s
)) {
3682 return isl_bool_false
;
3686 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
3687 isl_seq_inner_product(vec
->el
, bmap
->ineq
[i
], 1 + total
, &s
);
3688 if (isl_int_is_neg(s
)) {
3690 return isl_bool_false
;
3696 return isl_bool_true
;
3699 isl_bool
isl_basic_set_contains(__isl_keep isl_basic_set
*bset
,
3700 __isl_keep isl_vec
*vec
)
3702 return isl_basic_map_contains(bset_to_bmap(bset
), vec
);
3705 __isl_give isl_basic_map
*isl_basic_map_intersect(
3706 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
3708 struct isl_vec
*sample
= NULL
;
3709 isl_space
*space1
, *space2
;
3710 isl_size dim1
, dim2
, nparam1
, nparam2
;
3712 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
3714 space1
= isl_basic_map_peek_space(bmap1
);
3715 space2
= isl_basic_map_peek_space(bmap2
);
3716 dim1
= isl_space_dim(space1
, isl_dim_all
);
3717 dim2
= isl_space_dim(space2
, isl_dim_all
);
3718 nparam1
= isl_space_dim(space1
, isl_dim_param
);
3719 nparam2
= isl_space_dim(space2
, isl_dim_param
);
3720 if (dim1
< 0 || dim2
< 0 || nparam1
< 0 || nparam2
< 0)
3722 if (dim1
== nparam1
&& dim2
!= nparam2
)
3723 return isl_basic_map_intersect(bmap2
, bmap1
);
3725 if (dim2
!= nparam2
&&
3726 isl_basic_map_check_equal_space(bmap1
, bmap2
) < 0)
3729 if (isl_basic_map_plain_is_empty(bmap1
)) {
3730 isl_basic_map_free(bmap2
);
3733 if (isl_basic_map_plain_is_empty(bmap2
)) {
3734 isl_basic_map_free(bmap1
);
3738 if (bmap1
->sample
&&
3739 isl_basic_map_contains(bmap1
, bmap1
->sample
) > 0 &&
3740 isl_basic_map_contains(bmap2
, bmap1
->sample
) > 0)
3741 sample
= isl_vec_copy(bmap1
->sample
);
3742 else if (bmap2
->sample
&&
3743 isl_basic_map_contains(bmap1
, bmap2
->sample
) > 0 &&
3744 isl_basic_map_contains(bmap2
, bmap2
->sample
) > 0)
3745 sample
= isl_vec_copy(bmap2
->sample
);
3747 bmap1
= isl_basic_map_cow(bmap1
);
3750 bmap1
= isl_basic_map_extend(bmap1
,
3751 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
3752 bmap1
= add_constraints(bmap1
, bmap2
, 0, 0);
3755 isl_vec_free(sample
);
3757 isl_vec_free(bmap1
->sample
);
3758 bmap1
->sample
= sample
;
3761 bmap1
= isl_basic_map_simplify(bmap1
);
3762 return isl_basic_map_finalize(bmap1
);
3765 isl_vec_free(sample
);
3766 isl_basic_map_free(bmap1
);
3767 isl_basic_map_free(bmap2
);
3771 __isl_give isl_basic_set
*isl_basic_set_intersect(
3772 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
3774 return bset_from_bmap(isl_basic_map_intersect(bset_to_bmap(bset1
),
3775 bset_to_bmap(bset2
)));
3778 __isl_give isl_basic_set
*isl_basic_set_intersect_params(
3779 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
3781 return isl_basic_set_intersect(bset1
, bset2
);
3784 /* Does "map" consist of a single disjunct, without any local variables?
3786 static isl_bool
is_convex_no_locals(__isl_keep isl_map
*map
)
3791 return isl_bool_error
;
3793 return isl_bool_false
;
3794 n_div
= isl_basic_map_dim(map
->p
[0], isl_dim_div
);
3796 return isl_bool_error
;
3798 return isl_bool_false
;
3799 return isl_bool_true
;
3802 /* Check that "map" consists of a single disjunct, without any local variables.
3804 static isl_stat
check_convex_no_locals(__isl_keep isl_map
*map
)
3808 ok
= is_convex_no_locals(map
);
3810 return isl_stat_error
;
3814 isl_die(isl_map_get_ctx(map
), isl_error_internal
,
3815 "unexpectedly not convex or involving local variables",
3816 return isl_stat_error
);
3819 /* Special case of isl_map_intersect, where both map1 and map2
3820 * are convex, without any divs and such that either map1 or map2
3821 * contains a single constraint. This constraint is then simply
3822 * added to the other map.
3824 static __isl_give isl_map
*map_intersect_add_constraint(
3825 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
3827 if (check_convex_no_locals(map1
) < 0 ||
3828 check_convex_no_locals(map2
) < 0)
3831 if (map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
!= 1)
3832 return isl_map_intersect(map2
, map1
);
3834 map1
= isl_map_cow(map1
);
3837 if (isl_map_plain_is_empty(map1
)) {
3841 if (map2
->p
[0]->n_eq
== 1)
3842 map1
->p
[0] = isl_basic_map_add_eq(map1
->p
[0], map2
->p
[0]->eq
[0]);
3844 map1
->p
[0] = isl_basic_map_add_ineq(map1
->p
[0],
3845 map2
->p
[0]->ineq
[0]);
3847 map1
->p
[0] = isl_basic_map_simplify(map1
->p
[0]);
3848 map1
->p
[0] = isl_basic_map_finalize(map1
->p
[0]);
3852 if (isl_basic_map_plain_is_empty(map1
->p
[0])) {
3853 isl_basic_map_free(map1
->p
[0]);
3859 map1
= isl_map_unmark_normalized(map1
);
3867 /* map2 may be either a parameter domain or a map living in the same
3870 static __isl_give isl_map
*map_intersect_internal(__isl_take isl_map
*map1
,
3871 __isl_take isl_map
*map2
)
3877 isl_size dim2
, nparam2
;
3882 if ((isl_map_plain_is_empty(map1
) ||
3883 isl_map_plain_is_universe(map2
)) &&
3884 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
3888 if ((isl_map_plain_is_empty(map2
) ||
3889 isl_map_plain_is_universe(map1
)) &&
3890 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
3895 if (is_convex_no_locals(map1
) == isl_bool_true
&&
3896 is_convex_no_locals(map2
) == isl_bool_true
&&
3897 isl_space_is_equal(map1
->dim
, map2
->dim
) &&
3898 (map1
->p
[0]->n_eq
+ map1
->p
[0]->n_ineq
== 1 ||
3899 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1))
3900 return map_intersect_add_constraint(map1
, map2
);
3902 equal
= isl_map_plain_is_equal(map1
, map2
);
3910 dim2
= isl_map_dim(map2
, isl_dim_all
);
3911 nparam2
= isl_map_dim(map2
, isl_dim_param
);
3912 if (dim2
< 0 || nparam2
< 0)
3914 if (dim2
!= nparam2
)
3915 isl_assert(map1
->ctx
,
3916 isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
3918 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
3919 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
3920 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
3922 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
3923 map1
->n
* map2
->n
, flags
);
3926 for (i
= 0; i
< map1
->n
; ++i
)
3927 for (j
= 0; j
< map2
->n
; ++j
) {
3928 struct isl_basic_map
*part
;
3929 part
= isl_basic_map_intersect(
3930 isl_basic_map_copy(map1
->p
[i
]),
3931 isl_basic_map_copy(map2
->p
[j
]));
3932 if (isl_basic_map_is_empty(part
) < 0)
3933 part
= isl_basic_map_free(part
);
3934 result
= isl_map_add_basic_map(result
, part
);
3947 static __isl_give isl_map
*map_intersect(__isl_take isl_map
*map1
,
3948 __isl_take isl_map
*map2
)
3950 if (isl_map_check_equal_space(map1
, map2
) < 0)
3952 return map_intersect_internal(map1
, map2
);
3959 __isl_give isl_map
*isl_map_intersect(__isl_take isl_map
*map1
,
3960 __isl_take isl_map
*map2
)
3962 isl_map_align_params_bin(&map1
, &map2
);
3963 return map_intersect(map1
, map2
);
3966 __isl_give isl_set
*isl_set_intersect(__isl_take isl_set
*set1
,
3967 __isl_take isl_set
*set2
)
3969 return set_from_map(isl_map_intersect(set_to_map(set1
),
3973 /* map_intersect_internal accepts intersections
3974 * with parameter domains, so we can just call that function.
3976 __isl_give isl_map
*isl_map_intersect_params(__isl_take isl_map
*map
,
3977 __isl_take isl_set
*params
)
3979 isl_map_align_params_set(&map
, ¶ms
);
3980 return map_intersect_internal(map
, params
);
3983 __isl_give isl_set
*isl_set_intersect_params(__isl_take isl_set
*set
,
3984 __isl_take isl_set
*params
)
3986 return isl_map_intersect_params(set
, params
);
3989 __isl_give isl_basic_map
*isl_basic_map_reverse(__isl_take isl_basic_map
*bmap
)
3997 bmap
= isl_basic_map_cow(bmap
);
4000 space
= isl_space_reverse(isl_space_copy(bmap
->dim
));
4001 pos
= isl_basic_map_offset(bmap
, isl_dim_in
);
4002 n1
= isl_basic_map_dim(bmap
, isl_dim_in
);
4003 n2
= isl_basic_map_dim(bmap
, isl_dim_out
);
4004 if (n1
< 0 || n2
< 0)
4005 bmap
= isl_basic_map_free(bmap
);
4006 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
4007 return isl_basic_map_reset_space(bmap
, space
);
4010 /* Given a basic map where the tuple of type "type" is a wrapped map,
4011 * swap domain and range of that wrapped map.
4013 static __isl_give isl_basic_map
*isl_basic_map_reverse_wrapped(
4014 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
)
4017 isl_size offset
, n1
, n2
;
4019 space
= isl_basic_map_peek_space(bmap
);
4020 offset
= isl_basic_map_var_offset(bmap
, type
);
4021 n1
= isl_space_wrapped_dim(space
, type
, isl_dim_in
);
4022 n2
= isl_space_wrapped_dim(space
, type
, isl_dim_out
);
4023 if (offset
< 0 || n1
< 0 || n2
< 0)
4024 return isl_basic_map_free(bmap
);
4026 bmap
= isl_basic_map_swap_vars(bmap
, 1 + offset
, n1
, n2
);
4028 space
= isl_basic_map_take_space(bmap
);
4029 space
= isl_space_reverse_wrapped(space
, type
);
4030 bmap
= isl_basic_map_restore_space(bmap
, space
);
4035 /* Given a basic map (A -> B) -> C, return the corresponding basic map
4038 static __isl_give isl_basic_map
*isl_basic_map_domain_reverse(
4039 __isl_take isl_basic_map
*bmap
)
4043 space
= isl_basic_map_peek_space(bmap
);
4044 if (isl_space_check_domain_is_wrapping(space
) < 0)
4045 return isl_basic_map_free(bmap
);
4046 bmap
= isl_basic_map_reverse_wrapped(bmap
, isl_dim_in
);
4051 /* Given a basic map A -> (B -> C), return the corresponding basic map
4054 static __isl_give isl_basic_map
*isl_basic_map_range_reverse(
4055 __isl_take isl_basic_map
*bmap
)
4059 space
= isl_basic_map_peek_space(bmap
);
4060 if (isl_space_check_range_is_wrapping(space
) < 0)
4061 return isl_basic_map_free(bmap
);
4062 bmap
= isl_basic_map_reverse_wrapped(bmap
, isl_dim_out
);
4067 /* Given a basic map that is actually a basic set (A -> B),
4068 * return the corresponding basic set (B -> A) as a basic map.
4070 static __isl_give isl_basic_map
*isl_basic_map_set_reverse(
4071 __isl_take isl_basic_map
*bmap
)
4075 space
= isl_basic_map_peek_space(bmap
);
4076 if (isl_space_check_is_wrapping(space
) < 0)
4077 return isl_basic_map_free(bmap
);
4078 bmap
= isl_basic_map_reverse_wrapped(bmap
, isl_dim_set
);
4083 static __isl_give isl_basic_map
*basic_map_space_reset(
4084 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
)
4090 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
4093 space
= isl_basic_map_get_space(bmap
);
4094 space
= isl_space_reset(space
, type
);
4095 bmap
= isl_basic_map_reset_space(bmap
, space
);
4099 __isl_give isl_basic_map
*isl_basic_map_insert_dims(
4100 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
4101 unsigned pos
, unsigned n
)
4103 isl_bool rational
, is_empty
;
4104 isl_space
*res_space
;
4105 struct isl_basic_map
*res
;
4106 struct isl_dim_map
*dim_map
;
4109 enum isl_dim_type t
;
4112 return basic_map_space_reset(bmap
, type
);
4114 is_empty
= isl_basic_map_plain_is_empty(bmap
);
4115 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4116 if (is_empty
< 0 || total
< 0)
4117 return isl_basic_map_free(bmap
);
4118 res_space
= isl_space_insert_dims(isl_basic_map_get_space(bmap
),
4121 return isl_basic_map_free(bmap
);
4123 isl_basic_map_free(bmap
);
4124 return isl_basic_map_empty(res_space
);
4127 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
+ n
);
4129 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
4133 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
4135 isl_size size
= isl_basic_map_dim(bmap
, t
);
4137 dim_map
= isl_dim_map_free(dim_map
);
4138 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
4140 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
4141 pos
, size
- pos
, off
+ pos
+ n
);
4143 dim
= isl_space_dim(res_space
, t
);
4145 dim_map
= isl_dim_map_free(dim_map
);
4148 isl_dim_map_div(dim_map
, bmap
, off
);
4150 res
= isl_basic_map_alloc_space(res_space
,
4151 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
4152 rational
= isl_basic_map_is_rational(bmap
);
4154 res
= isl_basic_map_free(res
);
4156 res
= isl_basic_map_set_rational(res
);
4157 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
4158 return isl_basic_map_finalize(res
);
4161 __isl_give isl_basic_set
*isl_basic_set_insert_dims(
4162 __isl_take isl_basic_set
*bset
,
4163 enum isl_dim_type type
, unsigned pos
, unsigned n
)
4165 return isl_basic_map_insert_dims(bset
, type
, pos
, n
);
4168 __isl_give isl_basic_map
*isl_basic_map_add_dims(__isl_take isl_basic_map
*bmap
,
4169 enum isl_dim_type type
, unsigned n
)
4173 dim
= isl_basic_map_dim(bmap
, type
);
4175 return isl_basic_map_free(bmap
);
4176 return isl_basic_map_insert_dims(bmap
, type
, dim
, n
);
4179 __isl_give isl_basic_set
*isl_basic_set_add_dims(__isl_take isl_basic_set
*bset
,
4180 enum isl_dim_type type
, unsigned n
)
4184 isl_assert(bset
->ctx
, type
!= isl_dim_in
, goto error
);
4185 return isl_basic_map_add_dims(bset
, type
, n
);
4187 isl_basic_set_free(bset
);
4191 static __isl_give isl_map
*map_space_reset(__isl_take isl_map
*map
,
4192 enum isl_dim_type type
)
4196 if (!map
|| !isl_space_is_named_or_nested(map
->dim
, type
))
4199 space
= isl_map_get_space(map
);
4200 space
= isl_space_reset(space
, type
);
4201 map
= isl_map_reset_space(map
, space
);
4205 __isl_give isl_map
*isl_map_insert_dims(__isl_take isl_map
*map
,
4206 enum isl_dim_type type
, unsigned pos
, unsigned n
)
4212 return map_space_reset(map
, type
);
4214 map
= isl_map_cow(map
);
4218 for (i
= 0; i
< map
->n
; ++i
) {
4219 map
->p
[i
] = isl_basic_map_insert_dims(map
->p
[i
], type
, pos
, n
);
4224 space
= isl_map_take_space(map
);
4225 space
= isl_space_insert_dims(space
, type
, pos
, n
);
4226 map
= isl_map_restore_space(map
, space
);
4234 __isl_give isl_set
*isl_set_insert_dims(__isl_take isl_set
*set
,
4235 enum isl_dim_type type
, unsigned pos
, unsigned n
)
4237 return isl_map_insert_dims(set
, type
, pos
, n
);
4240 __isl_give isl_map
*isl_map_add_dims(__isl_take isl_map
*map
,
4241 enum isl_dim_type type
, unsigned n
)
4245 dim
= isl_map_dim(map
, type
);
4247 return isl_map_free(map
);
4248 return isl_map_insert_dims(map
, type
, dim
, n
);
4251 __isl_give isl_set
*isl_set_add_dims(__isl_take isl_set
*set
,
4252 enum isl_dim_type type
, unsigned n
)
4256 isl_assert(set
->ctx
, type
!= isl_dim_in
, goto error
);
4257 return set_from_map(isl_map_add_dims(set_to_map(set
), type
, n
));
4263 __isl_give isl_basic_map
*isl_basic_map_move_dims(
4264 __isl_take isl_basic_map
*bmap
,
4265 enum isl_dim_type dst_type
, unsigned dst_pos
,
4266 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
4269 struct isl_dim_map
*dim_map
;
4270 struct isl_basic_map
*res
;
4271 enum isl_dim_type t
;
4278 bmap
= isl_basic_map_reset(bmap
, src_type
);
4279 bmap
= isl_basic_map_reset(bmap
, dst_type
);
4283 if (isl_basic_map_check_range(bmap
, src_type
, src_pos
, n
) < 0)
4284 return isl_basic_map_free(bmap
);
4286 if (dst_type
== src_type
&& dst_pos
== src_pos
)
4289 isl_assert(bmap
->ctx
, dst_type
!= src_type
, goto error
);
4291 if (pos(bmap
->dim
, dst_type
) + dst_pos
==
4292 pos(bmap
->dim
, src_type
) + src_pos
+
4293 ((src_type
< dst_type
) ? n
: 0)) {
4294 space
= isl_basic_map_take_space(bmap
);
4295 space
= isl_space_move_dims(space
, dst_type
, dst_pos
,
4296 src_type
, src_pos
, n
);
4297 bmap
= isl_basic_map_restore_space(bmap
, space
);
4298 bmap
= isl_basic_map_finalize(bmap
);
4303 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4305 return isl_basic_map_free(bmap
);
4306 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
4309 space
= isl_basic_map_peek_space(bmap
);
4310 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
4311 isl_size size
= isl_space_dim(space
, t
);
4313 dim_map
= isl_dim_map_free(dim_map
);
4314 if (t
== dst_type
) {
4315 isl_dim_map_dim_range(dim_map
, space
, t
,
4318 isl_dim_map_dim_range(dim_map
, space
, src_type
,
4321 isl_dim_map_dim_range(dim_map
, space
, t
,
4322 dst_pos
, size
- dst_pos
, off
);
4323 off
+= size
- dst_pos
;
4324 } else if (t
== src_type
) {
4325 isl_dim_map_dim_range(dim_map
, space
, t
,
4328 isl_dim_map_dim_range(dim_map
, space
, t
,
4329 src_pos
+ n
, size
- src_pos
- n
, off
);
4330 off
+= size
- src_pos
- n
;
4332 isl_dim_map_dim(dim_map
, space
, t
, off
);
4336 isl_dim_map_div(dim_map
, bmap
, off
);
4338 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
4339 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
4340 bmap
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
4341 space
= isl_basic_map_take_space(bmap
);
4342 space
= isl_space_move_dims(space
, dst_type
, dst_pos
,
4343 src_type
, src_pos
, n
);
4344 bmap
= isl_basic_map_restore_space(bmap
, space
);
4348 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
4349 bmap
= isl_basic_map_gauss(bmap
, NULL
);
4350 bmap
= isl_basic_map_finalize(bmap
);
4354 isl_basic_map_free(bmap
);
4358 __isl_give isl_basic_set
*isl_basic_set_move_dims(__isl_take isl_basic_set
*bset
,
4359 enum isl_dim_type dst_type
, unsigned dst_pos
,
4360 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
4362 isl_basic_map
*bmap
= bset_to_bmap(bset
);
4363 bmap
= isl_basic_map_move_dims(bmap
, dst_type
, dst_pos
,
4364 src_type
, src_pos
, n
);
4365 return bset_from_bmap(bmap
);
4368 __isl_give isl_set
*isl_set_move_dims(__isl_take isl_set
*set
,
4369 enum isl_dim_type dst_type
, unsigned dst_pos
,
4370 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
4374 isl_assert(set
->ctx
, dst_type
!= isl_dim_in
, goto error
);
4375 return set_from_map(isl_map_move_dims(set_to_map(set
),
4376 dst_type
, dst_pos
, src_type
, src_pos
, n
));
4382 __isl_give isl_map
*isl_map_move_dims(__isl_take isl_map
*map
,
4383 enum isl_dim_type dst_type
, unsigned dst_pos
,
4384 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
4390 map
= isl_map_reset(map
, src_type
);
4391 map
= isl_map_reset(map
, dst_type
);
4395 if (isl_map_check_range(map
, src_type
, src_pos
, n
))
4396 return isl_map_free(map
);
4398 if (dst_type
== src_type
&& dst_pos
== src_pos
)
4401 isl_assert(map
->ctx
, dst_type
!= src_type
, goto error
);
4403 map
= isl_map_cow(map
);
4407 for (i
= 0; i
< map
->n
; ++i
) {
4408 map
->p
[i
] = isl_basic_map_move_dims(map
->p
[i
],
4410 src_type
, src_pos
, n
);
4415 space
= isl_map_take_space(map
);
4416 space
= isl_space_move_dims(space
, dst_type
, dst_pos
,
4417 src_type
, src_pos
, n
);
4418 map
= isl_map_restore_space(map
, space
);
4426 /* Move the specified dimensions to the last columns right before
4427 * the divs. Don't change the dimension specification of bmap.
4428 * That's the responsibility of the caller.
4430 static __isl_give isl_basic_map
*move_last(__isl_take isl_basic_map
*bmap
,
4431 enum isl_dim_type type
, unsigned first
, unsigned n
)
4434 struct isl_dim_map
*dim_map
;
4435 struct isl_basic_map
*res
;
4436 enum isl_dim_type t
;
4442 if (isl_basic_map_offset(bmap
, type
) + first
+ n
==
4443 isl_basic_map_offset(bmap
, isl_dim_div
))
4446 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4448 return isl_basic_map_free(bmap
);
4449 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
4452 space
= isl_basic_map_peek_space(bmap
);
4453 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
4454 isl_size size
= isl_space_dim(space
, t
);
4456 dim_map
= isl_dim_map_free(dim_map
);
4458 isl_dim_map_dim_range(dim_map
, space
, t
,
4461 isl_dim_map_dim_range(dim_map
, space
, t
,
4462 first
, n
, total
- bmap
->n_div
- n
);
4463 isl_dim_map_dim_range(dim_map
, space
, t
,
4464 first
+ n
, size
- (first
+ n
), off
);
4465 off
+= size
- (first
+ n
);
4467 isl_dim_map_dim(dim_map
, space
, t
, off
);
4471 isl_dim_map_div(dim_map
, bmap
, off
+ n
);
4473 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
4474 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
4475 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
4479 /* Insert "n" rows in the divs of "bmap".
4481 * The number of columns is not changed, which means that the last
4482 * dimensions of "bmap" are being reintepreted as the new divs.
4483 * The space of "bmap" is not adjusted, however, which means
4484 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
4485 * from the space of "bmap" is the responsibility of the caller.
4487 static __isl_give isl_basic_map
*insert_div_rows(__isl_take isl_basic_map
*bmap
,
4495 bmap
= isl_basic_map_cow(bmap
);
4499 row_size
= isl_basic_map_offset(bmap
, isl_dim_div
) + bmap
->extra
;
4500 old
= bmap
->block2
.data
;
4501 bmap
->block2
= isl_blk_extend(bmap
->ctx
, bmap
->block2
,
4502 (bmap
->extra
+ n
) * (1 + row_size
));
4503 if (!bmap
->block2
.data
)
4504 return isl_basic_map_free(bmap
);
4505 new_div
= isl_alloc_array(bmap
->ctx
, isl_int
*, bmap
->extra
+ n
);
4507 return isl_basic_map_free(bmap
);
4508 for (i
= 0; i
< n
; ++i
) {
4509 new_div
[i
] = bmap
->block2
.data
+
4510 (bmap
->extra
+ i
) * (1 + row_size
);
4511 isl_seq_clr(new_div
[i
], 1 + row_size
);
4513 for (i
= 0; i
< bmap
->extra
; ++i
)
4514 new_div
[n
+ i
] = bmap
->block2
.data
+ (bmap
->div
[i
] - old
);
4516 bmap
->div
= new_div
;
4523 /* Drop constraints from "bmap" that only involve the variables
4524 * of "type" in the range [first, first + n] that are not related
4525 * to any of the variables outside that interval.
4526 * These constraints cannot influence the values for the variables
4527 * outside the interval, except in case they cause "bmap" to be empty.
4528 * Only drop the constraints if "bmap" is known to be non-empty.
4530 static __isl_give isl_basic_map
*drop_irrelevant_constraints(
4531 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
4532 unsigned first
, unsigned n
)
4536 isl_size dim
, n_div
;
4539 non_empty
= isl_basic_map_plain_is_non_empty(bmap
);
4541 return isl_basic_map_free(bmap
);
4545 dim
= isl_basic_map_dim(bmap
, isl_dim_all
);
4546 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4547 if (dim
< 0 || n_div
< 0)
4548 return isl_basic_map_free(bmap
);
4549 groups
= isl_calloc_array(isl_basic_map_get_ctx(bmap
), int, dim
);
4551 return isl_basic_map_free(bmap
);
4552 first
+= isl_basic_map_offset(bmap
, type
) - 1;
4553 for (i
= 0; i
< first
; ++i
)
4555 for (i
= first
+ n
; i
< dim
- n_div
; ++i
)
4558 bmap
= isl_basic_map_drop_unrelated_constraints(bmap
, groups
);
4563 /* Turn the n dimensions of type type, starting at first
4564 * into existentially quantified variables.
4566 * If a subset of the projected out variables are unrelated
4567 * to any of the variables that remain, then the constraints
4568 * involving this subset are simply dropped first.
4570 __isl_give isl_basic_map
*isl_basic_map_project_out(
4571 __isl_take isl_basic_map
*bmap
,
4572 enum isl_dim_type type
, unsigned first
, unsigned n
)
4578 return basic_map_space_reset(bmap
, type
);
4579 if (type
== isl_dim_div
)
4580 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
4581 "cannot project out existentially quantified variables",
4582 return isl_basic_map_free(bmap
));
4584 empty
= isl_basic_map_plain_is_empty(bmap
);
4586 return isl_basic_map_free(bmap
);
4588 bmap
= isl_basic_map_set_to_empty(bmap
);
4590 bmap
= drop_irrelevant_constraints(bmap
, type
, first
, n
);
4594 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
4595 return isl_basic_map_remove_dims(bmap
, type
, first
, n
);
4597 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
4598 return isl_basic_map_free(bmap
);
4600 bmap
= move_last(bmap
, type
, first
, n
);
4601 bmap
= isl_basic_map_cow(bmap
);
4602 bmap
= insert_div_rows(bmap
, n
);
4604 space
= isl_basic_map_take_space(bmap
);
4605 space
= isl_space_drop_dims(space
, type
, first
, n
);
4606 bmap
= isl_basic_map_restore_space(bmap
, space
);
4607 bmap
= isl_basic_map_simplify(bmap
);
4608 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
4609 return isl_basic_map_finalize(bmap
);
4612 /* Turn the n dimensions of type type, starting at first
4613 * into existentially quantified variables.
4615 __isl_give isl_basic_set
*isl_basic_set_project_out(
4616 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
,
4617 unsigned first
, unsigned n
)
4619 return bset_from_bmap(isl_basic_map_project_out(bset_to_bmap(bset
),
4623 /* Turn the n dimensions of type type, starting at first
4624 * into existentially quantified variables.
4626 __isl_give isl_map
*isl_map_project_out(__isl_take isl_map
*map
,
4627 enum isl_dim_type type
, unsigned first
, unsigned n
)
4633 return map_space_reset(map
, type
);
4635 if (isl_map_check_range(map
, type
, first
, n
) < 0)
4636 return isl_map_free(map
);
4638 map
= isl_map_cow(map
);
4642 for (i
= 0; i
< map
->n
; ++i
) {
4643 map
->p
[i
] = isl_basic_map_project_out(map
->p
[i
], type
, first
, n
);
4649 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
4650 map
= isl_map_unmark_normalized(map
);
4652 space
= isl_map_take_space(map
);
4653 space
= isl_space_drop_dims(space
, type
, first
, n
);
4654 map
= isl_map_restore_space(map
, space
);
4663 #define TYPE isl_map
4664 #include "isl_project_out_all_params_templ.c"
4665 #include "isl_project_out_param_templ.c"
4667 /* Turn all the dimensions of type "type", except the "n" starting at "first"
4668 * into existentially quantified variables.
4670 __isl_give isl_map
*isl_map_project_onto(__isl_take isl_map
*map
,
4671 enum isl_dim_type type
, unsigned first
, unsigned n
)
4675 dim
= isl_map_dim(map
, type
);
4676 if (isl_map_check_range(map
, type
, first
, n
) < 0 || dim
< 0)
4677 return isl_map_free(map
);
4678 map
= isl_map_project_out(map
, type
, first
+ n
, dim
- (first
+ n
));
4679 map
= isl_map_project_out(map
, type
, 0, first
);
4683 /* Turn the n dimensions of type type, starting at first
4684 * into existentially quantified variables.
4686 __isl_give isl_set
*isl_set_project_out(__isl_take isl_set
*set
,
4687 enum isl_dim_type type
, unsigned first
, unsigned n
)
4689 return set_from_map(isl_map_project_out(set_to_map(set
),
4693 /* If "set" involves a parameter with identifier "id",
4694 * then turn it into an existentially quantified variable.
4696 __isl_give isl_set
*isl_set_project_out_param_id(__isl_take isl_set
*set
,
4697 __isl_take isl_id
*id
)
4699 return set_from_map(isl_map_project_out_param_id(set_to_map(set
), id
));
4702 /* If "set" involves any of the parameters with identifiers in "list",
4703 * then turn them into existentially quantified variables.
4705 __isl_give isl_set
*isl_set_project_out_param_id_list(__isl_take isl_set
*set
,
4706 __isl_take isl_id_list
*list
)
4710 map
= set_to_map(set
);
4711 map
= isl_map_project_out_param_id_list(map
, list
);
4712 return set_from_map(map
);
4715 /* Project out all parameters from "set" by existentially quantifying
4718 __isl_give isl_set
*isl_set_project_out_all_params(__isl_take isl_set
*set
)
4720 return set_from_map(isl_map_project_out_all_params(set_to_map(set
)));
4723 /* Return a map that projects the elements in "set" onto their
4724 * "n" set dimensions starting at "first".
4725 * "type" should be equal to isl_dim_set.
4727 __isl_give isl_map
*isl_set_project_onto_map(__isl_take isl_set
*set
,
4728 enum isl_dim_type type
, unsigned first
, unsigned n
)
4733 if (type
!= isl_dim_set
)
4734 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
4735 "only set dimensions can be projected out", goto error
);
4736 if (isl_set_check_range(set
, type
, first
, n
) < 0)
4737 return isl_set_free(set
);
4739 map
= isl_map_from_domain(set
);
4740 map
= isl_map_add_dims(map
, isl_dim_out
, n
);
4741 for (i
= 0; i
< n
; ++i
)
4742 map
= isl_map_equate(map
, isl_dim_in
, first
+ i
,
4750 static __isl_give isl_basic_map
*add_divs(__isl_take isl_basic_map
*bmap
,
4756 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4758 return isl_basic_map_free(bmap
);
4759 for (i
= 0; i
< n
; ++i
) {
4760 j
= isl_basic_map_alloc_div(bmap
);
4763 isl_seq_clr(bmap
->div
[j
], 1 + 1 + total
);
4767 isl_basic_map_free(bmap
);
4771 /* Does "bmap2" apply to the range of "bmap1" (ignoring parameters)?
4773 isl_bool
isl_basic_map_applies_range(__isl_keep isl_basic_map
*bmap1
,
4774 __isl_keep isl_basic_map
*bmap2
)
4776 isl_space
*space1
, *space2
;
4778 space1
= isl_basic_map_peek_space(bmap1
);
4779 space2
= isl_basic_map_peek_space(bmap2
);
4780 return isl_space_tuple_is_equal(space1
, isl_dim_out
,
4781 space2
, isl_dim_in
);
4784 /* Check that "bmap2" applies to the range of "bmap1" (ignoring parameters).
4786 static isl_stat
isl_basic_map_check_applies_range(
4787 __isl_keep isl_basic_map
*bmap1
, __isl_keep isl_basic_map
*bmap2
)
4791 equal
= isl_basic_map_applies_range(bmap1
, bmap2
);
4793 return isl_stat_error
;
4795 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
4796 "spaces don't match", return isl_stat_error
);
4800 __isl_give isl_basic_map
*isl_basic_map_apply_range(
4801 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
4803 isl_space
*space_result
= NULL
;
4804 struct isl_basic_map
*bmap
;
4805 isl_size n_in
, n_out
, n
, nparam
;
4806 unsigned total
, pos
;
4807 struct isl_dim_map
*dim_map1
, *dim_map2
;
4809 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
4811 if (isl_basic_map_check_applies_range(bmap1
, bmap2
) < 0)
4814 n_in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
4815 n_out
= isl_basic_map_dim(bmap2
, isl_dim_out
);
4816 n
= isl_basic_map_dim(bmap1
, isl_dim_out
);
4817 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
4818 if (n_in
< 0 || n_out
< 0 || n
< 0 || nparam
< 0)
4821 space_result
= isl_space_join(isl_basic_map_get_space(bmap1
),
4822 isl_basic_map_get_space(bmap2
));
4824 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ n
;
4825 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4826 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4827 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
4828 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
4829 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
4830 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_in
);
4831 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_out
);
4832 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
4833 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
4834 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
4836 bmap
= isl_basic_map_alloc_space(space_result
,
4837 bmap1
->n_div
+ bmap2
->n_div
+ n
,
4838 bmap1
->n_eq
+ bmap2
->n_eq
,
4839 bmap1
->n_ineq
+ bmap2
->n_ineq
);
4840 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
4841 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
4842 bmap
= add_divs(bmap
, n
);
4843 bmap
= isl_basic_map_simplify(bmap
);
4844 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
4845 return isl_basic_map_finalize(bmap
);
4847 isl_basic_map_free(bmap1
);
4848 isl_basic_map_free(bmap2
);
4852 __isl_give isl_basic_set
*isl_basic_set_apply(__isl_take isl_basic_set
*bset
,
4853 __isl_take isl_basic_map
*bmap
)
4855 if (isl_basic_map_check_compatible_domain(bmap
, bset
) < 0)
4858 return bset_from_bmap(isl_basic_map_apply_range(bset_to_bmap(bset
),
4861 isl_basic_set_free(bset
);
4862 isl_basic_map_free(bmap
);
4866 __isl_give isl_basic_map
*isl_basic_map_apply_domain(
4867 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
4869 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
4871 if (!isl_space_tuple_is_equal(bmap1
->dim
, isl_dim_in
,
4872 bmap2
->dim
, isl_dim_in
))
4873 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
4874 "spaces don't match", goto error
);
4876 bmap1
= isl_basic_map_reverse(bmap1
);
4877 bmap1
= isl_basic_map_apply_range(bmap1
, bmap2
);
4878 return isl_basic_map_reverse(bmap1
);
4880 isl_basic_map_free(bmap1
);
4881 isl_basic_map_free(bmap2
);
4885 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
4886 * A \cap B -> f(A) + f(B)
4888 __isl_give isl_basic_map
*isl_basic_map_sum(__isl_take isl_basic_map
*bmap1
,
4889 __isl_take isl_basic_map
*bmap2
)
4891 isl_size n_in
, n_out
, nparam
;
4892 unsigned total
, pos
;
4893 struct isl_basic_map
*bmap
= NULL
;
4894 struct isl_dim_map
*dim_map1
, *dim_map2
;
4897 if (isl_basic_map_check_equal_space(bmap1
, bmap2
) < 0)
4900 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
4901 n_in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
4902 n_out
= isl_basic_map_dim(bmap1
, isl_dim_out
);
4903 if (nparam
< 0 || n_in
< 0 || n_out
< 0)
4906 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
;
4907 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4908 dim_map2
= isl_dim_map_alloc(bmap2
->ctx
, total
);
4909 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
4910 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
);
4911 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
4912 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
4913 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_in
+ n_out
);
4914 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
4915 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
4916 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_out
);
4918 bmap
= isl_basic_map_alloc_space(isl_space_copy(bmap1
->dim
),
4919 bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
,
4920 bmap1
->n_eq
+ bmap2
->n_eq
+ n_out
,
4921 bmap1
->n_ineq
+ bmap2
->n_ineq
);
4922 for (i
= 0; i
< n_out
; ++i
) {
4923 int j
= isl_basic_map_alloc_equality(bmap
);
4926 isl_seq_clr(bmap
->eq
[j
], 1+total
);
4927 isl_int_set_si(bmap
->eq
[j
][1+nparam
+n_in
+i
], -1);
4928 isl_int_set_si(bmap
->eq
[j
][1+pos
+i
], 1);
4929 isl_int_set_si(bmap
->eq
[j
][1+pos
-n_out
+i
], 1);
4931 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
4932 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
4933 bmap
= add_divs(bmap
, 2 * n_out
);
4935 bmap
= isl_basic_map_simplify(bmap
);
4936 return isl_basic_map_finalize(bmap
);
4938 isl_basic_map_free(bmap
);
4939 isl_basic_map_free(bmap1
);
4940 isl_basic_map_free(bmap2
);
4944 /* Given two maps A -> f(A) and B -> g(B), construct a map
4945 * A \cap B -> f(A) + f(B)
4947 __isl_give isl_map
*isl_map_sum(__isl_take isl_map
*map1
,
4948 __isl_take isl_map
*map2
)
4950 struct isl_map
*result
;
4953 if (isl_map_check_equal_space(map1
, map2
) < 0)
4956 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
4957 map1
->n
* map2
->n
, 0);
4960 for (i
= 0; i
< map1
->n
; ++i
)
4961 for (j
= 0; j
< map2
->n
; ++j
) {
4962 struct isl_basic_map
*part
;
4963 part
= isl_basic_map_sum(
4964 isl_basic_map_copy(map1
->p
[i
]),
4965 isl_basic_map_copy(map2
->p
[j
]));
4966 if (isl_basic_map_is_empty(part
))
4967 isl_basic_map_free(part
);
4969 result
= isl_map_add_basic_map(result
, part
);
4982 __isl_give isl_set
*isl_set_sum(__isl_take isl_set
*set1
,
4983 __isl_take isl_set
*set2
)
4985 return set_from_map(isl_map_sum(set_to_map(set1
), set_to_map(set2
)));
4988 /* Given a basic map A -> f(A), construct A -> -f(A).
4990 __isl_give isl_basic_map
*isl_basic_map_neg(__isl_take isl_basic_map
*bmap
)
4996 bmap
= isl_basic_map_cow(bmap
);
4997 n
= isl_basic_map_dim(bmap
, isl_dim_out
);
4999 return isl_basic_map_free(bmap
);
5001 off
= isl_basic_map_offset(bmap
, isl_dim_out
);
5002 for (i
= 0; i
< bmap
->n_eq
; ++i
)
5003 for (j
= 0; j
< n
; ++j
)
5004 isl_int_neg(bmap
->eq
[i
][off
+j
], bmap
->eq
[i
][off
+j
]);
5005 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
5006 for (j
= 0; j
< n
; ++j
)
5007 isl_int_neg(bmap
->ineq
[i
][off
+j
], bmap
->ineq
[i
][off
+j
]);
5008 for (i
= 0; i
< bmap
->n_div
; ++i
)
5009 for (j
= 0; j
< n
; ++j
)
5010 isl_int_neg(bmap
->div
[i
][1+off
+j
], bmap
->div
[i
][1+off
+j
]);
5011 bmap
= isl_basic_map_gauss(bmap
, NULL
);
5012 return isl_basic_map_finalize(bmap
);
5015 __isl_give isl_basic_set
*isl_basic_set_neg(__isl_take isl_basic_set
*bset
)
5017 return isl_basic_map_neg(bset
);
5020 /* Given a map A -> f(A), construct A -> -f(A).
5022 __isl_give isl_map
*isl_map_neg(__isl_take isl_map
*map
)
5026 map
= isl_map_cow(map
);
5030 for (i
= 0; i
< map
->n
; ++i
) {
5031 map
->p
[i
] = isl_basic_map_neg(map
->p
[i
]);
5042 __isl_give isl_set
*isl_set_neg(__isl_take isl_set
*set
)
5044 return set_from_map(isl_map_neg(set_to_map(set
)));
5047 /* Given a basic map A -> f(A) and an integer d, construct a basic map
5048 * A -> floor(f(A)/d).
5050 __isl_give isl_basic_map
*isl_basic_map_floordiv(__isl_take isl_basic_map
*bmap
,
5053 isl_size n_in
, n_out
, nparam
;
5054 unsigned total
, pos
;
5055 struct isl_basic_map
*result
= NULL
;
5056 struct isl_dim_map
*dim_map
;
5059 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5060 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5061 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5062 if (nparam
< 0 || n_in
< 0 || n_out
< 0)
5063 return isl_basic_map_free(bmap
);
5065 total
= nparam
+ n_in
+ n_out
+ bmap
->n_div
+ n_out
;
5066 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
5067 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_param
, pos
= 0);
5068 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_in
, pos
+= nparam
);
5069 isl_dim_map_div(dim_map
, bmap
, pos
+= n_in
+ n_out
);
5070 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_out
, pos
+= bmap
->n_div
);
5072 result
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
5073 bmap
->n_div
+ n_out
,
5074 bmap
->n_eq
, bmap
->n_ineq
+ 2 * n_out
);
5075 result
= isl_basic_map_add_constraints_dim_map(result
, bmap
, dim_map
);
5076 result
= add_divs(result
, n_out
);
5077 for (i
= 0; i
< n_out
; ++i
) {
5079 j
= isl_basic_map_alloc_inequality(result
);
5082 isl_seq_clr(result
->ineq
[j
], 1+total
);
5083 isl_int_neg(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
5084 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], 1);
5085 j
= isl_basic_map_alloc_inequality(result
);
5088 isl_seq_clr(result
->ineq
[j
], 1+total
);
5089 isl_int_set(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
5090 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], -1);
5091 isl_int_sub_ui(result
->ineq
[j
][0], d
, 1);
5094 result
= isl_basic_map_simplify(result
);
5095 return isl_basic_map_finalize(result
);
5097 isl_basic_map_free(result
);
5101 /* Given a map A -> f(A) and an integer d, construct a map
5102 * A -> floor(f(A)/d).
5104 __isl_give isl_map
*isl_map_floordiv(__isl_take isl_map
*map
, isl_int d
)
5108 map
= isl_map_cow(map
);
5112 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
5113 for (i
= 0; i
< map
->n
; ++i
) {
5114 map
->p
[i
] = isl_basic_map_floordiv(map
->p
[i
], d
);
5118 map
= isl_map_unmark_normalized(map
);
5126 /* Given a map A -> f(A) and an integer d, construct a map
5127 * A -> floor(f(A)/d).
5129 __isl_give isl_map
*isl_map_floordiv_val(__isl_take isl_map
*map
,
5130 __isl_take isl_val
*d
)
5134 if (!isl_val_is_int(d
))
5135 isl_die(isl_val_get_ctx(d
), isl_error_invalid
,
5136 "expecting integer denominator", goto error
);
5137 map
= isl_map_floordiv(map
, d
->n
);
5146 static __isl_give isl_basic_map
*var_equal(__isl_take isl_basic_map
*bmap
,
5154 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5155 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5156 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5157 if (total
< 0 || nparam
< 0 || n_in
< 0)
5158 return isl_basic_map_free(bmap
);
5159 i
= isl_basic_map_alloc_equality(bmap
);
5162 isl_seq_clr(bmap
->eq
[i
], 1 + total
);
5163 isl_int_set_si(bmap
->eq
[i
][1+nparam
+pos
], -1);
5164 isl_int_set_si(bmap
->eq
[i
][1+nparam
+n_in
+pos
], 1);
5165 return isl_basic_map_finalize(bmap
);
5167 isl_basic_map_free(bmap
);
5171 /* Add a constraint to "bmap" expressing i_pos < o_pos
5173 static __isl_give isl_basic_map
*var_less(__isl_take isl_basic_map
*bmap
,
5181 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5182 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5183 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5184 if (total
< 0 || nparam
< 0 || n_in
< 0)
5185 return isl_basic_map_free(bmap
);
5186 i
= isl_basic_map_alloc_inequality(bmap
);
5189 isl_seq_clr(bmap
->ineq
[i
], 1 + total
);
5190 isl_int_set_si(bmap
->ineq
[i
][0], -1);
5191 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
5192 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
5193 return isl_basic_map_finalize(bmap
);
5195 isl_basic_map_free(bmap
);
5199 /* Add a constraint to "bmap" expressing i_pos <= o_pos
5201 static __isl_give isl_basic_map
*var_less_or_equal(
5202 __isl_take isl_basic_map
*bmap
, unsigned pos
)
5209 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5210 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5211 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5212 if (total
< 0 || nparam
< 0 || n_in
< 0)
5213 return isl_basic_map_free(bmap
);
5214 i
= isl_basic_map_alloc_inequality(bmap
);
5217 isl_seq_clr(bmap
->ineq
[i
], 1 + total
);
5218 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
5219 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
5220 return isl_basic_map_finalize(bmap
);
5222 isl_basic_map_free(bmap
);
5226 /* Add a constraint to "bmap" expressing i_pos > o_pos
5228 static __isl_give isl_basic_map
*var_more(__isl_take isl_basic_map
*bmap
,
5236 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5237 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5238 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5239 if (total
< 0 || nparam
< 0 || n_in
< 0)
5240 return isl_basic_map_free(bmap
);
5241 i
= isl_basic_map_alloc_inequality(bmap
);
5244 isl_seq_clr(bmap
->ineq
[i
], 1 + total
);
5245 isl_int_set_si(bmap
->ineq
[i
][0], -1);
5246 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
5247 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
5248 return isl_basic_map_finalize(bmap
);
5250 isl_basic_map_free(bmap
);
5254 /* Add a constraint to "bmap" expressing i_pos >= o_pos
5256 static __isl_give isl_basic_map
*var_more_or_equal(
5257 __isl_take isl_basic_map
*bmap
, unsigned pos
)
5264 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5265 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5266 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5267 if (total
< 0 || nparam
< 0 || n_in
< 0)
5268 return isl_basic_map_free(bmap
);
5269 i
= isl_basic_map_alloc_inequality(bmap
);
5272 isl_seq_clr(bmap
->ineq
[i
], 1 + total
);
5273 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
5274 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
5275 return isl_basic_map_finalize(bmap
);
5277 isl_basic_map_free(bmap
);
5281 __isl_give isl_basic_map
*isl_basic_map_equal(
5282 __isl_take isl_space
*space
, unsigned n_equal
)
5285 struct isl_basic_map
*bmap
;
5286 bmap
= isl_basic_map_alloc_space(space
, 0, n_equal
, 0);
5289 for (i
= 0; i
< n_equal
&& bmap
; ++i
)
5290 bmap
= var_equal(bmap
, i
);
5291 return isl_basic_map_finalize(bmap
);
5294 /* Return a relation on of dimension "space" expressing i_[0..pos] << o_[0..pos]
5296 __isl_give isl_basic_map
*isl_basic_map_less_at(__isl_take isl_space
*space
,
5300 struct isl_basic_map
*bmap
;
5301 bmap
= isl_basic_map_alloc_space(space
, 0, pos
, 1);
5304 for (i
= 0; i
< pos
&& bmap
; ++i
)
5305 bmap
= var_equal(bmap
, i
);
5307 bmap
= var_less(bmap
, pos
);
5308 return isl_basic_map_finalize(bmap
);
5311 /* Return a relation on "space" expressing i_[0..pos] <<= o_[0..pos]
5313 __isl_give isl_basic_map
*isl_basic_map_less_or_equal_at(
5314 __isl_take isl_space
*space
, unsigned pos
)
5317 isl_basic_map
*bmap
;
5319 bmap
= isl_basic_map_alloc_space(space
, 0, pos
, 1);
5320 for (i
= 0; i
< pos
; ++i
)
5321 bmap
= var_equal(bmap
, i
);
5322 bmap
= var_less_or_equal(bmap
, pos
);
5323 return isl_basic_map_finalize(bmap
);
5326 /* Return a relation on "space" expressing i_pos > o_pos
5328 __isl_give isl_basic_map
*isl_basic_map_more_at(__isl_take isl_space
*space
,
5332 struct isl_basic_map
*bmap
;
5333 bmap
= isl_basic_map_alloc_space(space
, 0, pos
, 1);
5336 for (i
= 0; i
< pos
&& bmap
; ++i
)
5337 bmap
= var_equal(bmap
, i
);
5339 bmap
= var_more(bmap
, pos
);
5340 return isl_basic_map_finalize(bmap
);
5343 /* Return a relation on "space" expressing i_[0..pos] >>= o_[0..pos]
5345 __isl_give isl_basic_map
*isl_basic_map_more_or_equal_at(
5346 __isl_take isl_space
*space
, unsigned pos
)
5349 isl_basic_map
*bmap
;
5351 bmap
= isl_basic_map_alloc_space(space
, 0, pos
, 1);
5352 for (i
= 0; i
< pos
; ++i
)
5353 bmap
= var_equal(bmap
, i
);
5354 bmap
= var_more_or_equal(bmap
, pos
);
5355 return isl_basic_map_finalize(bmap
);
5358 static __isl_give isl_map
*map_lex_lte_first(__isl_take isl_space
*space
,
5359 unsigned n
, int equal
)
5361 struct isl_map
*map
;
5364 if (n
== 0 && equal
)
5365 return isl_map_universe(space
);
5367 map
= isl_map_alloc_space(isl_space_copy(space
), n
, ISL_MAP_DISJOINT
);
5369 for (i
= 0; i
+ 1 < n
; ++i
)
5370 map
= isl_map_add_basic_map(map
,
5371 isl_basic_map_less_at(isl_space_copy(space
), i
));
5374 map
= isl_map_add_basic_map(map
,
5375 isl_basic_map_less_or_equal_at(space
, n
- 1));
5377 map
= isl_map_add_basic_map(map
,
5378 isl_basic_map_less_at(space
, n
- 1));
5380 isl_space_free(space
);
5385 static __isl_give isl_map
*map_lex_lte(__isl_take isl_space
*space
, int equal
)
5389 return map_lex_lte_first(space
, space
->n_out
, equal
);
5392 __isl_give isl_map
*isl_map_lex_lt_first(__isl_take isl_space
*space
,
5395 return map_lex_lte_first(space
, n
, 0);
5398 __isl_give isl_map
*isl_map_lex_le_first(__isl_take isl_space
*space
,
5401 return map_lex_lte_first(space
, n
, 1);
5404 __isl_give isl_map
*isl_map_lex_lt(__isl_take isl_space
*set_space
)
5406 return map_lex_lte(isl_space_map_from_set(set_space
), 0);
5409 __isl_give isl_map
*isl_map_lex_le(__isl_take isl_space
*set_space
)
5411 return map_lex_lte(isl_space_map_from_set(set_space
), 1);
5414 static __isl_give isl_map
*map_lex_gte_first(__isl_take isl_space
*space
,
5415 unsigned n
, int equal
)
5417 struct isl_map
*map
;
5420 if (n
== 0 && equal
)
5421 return isl_map_universe(space
);
5423 map
= isl_map_alloc_space(isl_space_copy(space
), n
, ISL_MAP_DISJOINT
);
5425 for (i
= 0; i
+ 1 < n
; ++i
)
5426 map
= isl_map_add_basic_map(map
,
5427 isl_basic_map_more_at(isl_space_copy(space
), i
));
5430 map
= isl_map_add_basic_map(map
,
5431 isl_basic_map_more_or_equal_at(space
, n
- 1));
5433 map
= isl_map_add_basic_map(map
,
5434 isl_basic_map_more_at(space
, n
- 1));
5436 isl_space_free(space
);
5441 static __isl_give isl_map
*map_lex_gte(__isl_take isl_space
*space
, int equal
)
5445 return map_lex_gte_first(space
, space
->n_out
, equal
);
5448 __isl_give isl_map
*isl_map_lex_gt_first(__isl_take isl_space
*space
,
5451 return map_lex_gte_first(space
, n
, 0);
5454 __isl_give isl_map
*isl_map_lex_ge_first(__isl_take isl_space
*space
,
5457 return map_lex_gte_first(space
, n
, 1);
5460 __isl_give isl_map
*isl_map_lex_gt(__isl_take isl_space
*set_space
)
5462 return map_lex_gte(isl_space_map_from_set(set_space
), 0);
5465 __isl_give isl_map
*isl_map_lex_ge(__isl_take isl_space
*set_space
)
5467 return map_lex_gte(isl_space_map_from_set(set_space
), 1);
5470 __isl_give isl_map
*isl_set_lex_le_set(__isl_take isl_set
*set1
,
5471 __isl_take isl_set
*set2
)
5474 map
= isl_map_lex_le(isl_set_get_space(set1
));
5475 map
= isl_map_intersect_domain(map
, set1
);
5476 map
= isl_map_intersect_range(map
, set2
);
5480 __isl_give isl_map
*isl_set_lex_lt_set(__isl_take isl_set
*set1
,
5481 __isl_take isl_set
*set2
)
5484 map
= isl_map_lex_lt(isl_set_get_space(set1
));
5485 map
= isl_map_intersect_domain(map
, set1
);
5486 map
= isl_map_intersect_range(map
, set2
);
5490 __isl_give isl_map
*isl_set_lex_ge_set(__isl_take isl_set
*set1
,
5491 __isl_take isl_set
*set2
)
5494 map
= isl_map_lex_ge(isl_set_get_space(set1
));
5495 map
= isl_map_intersect_domain(map
, set1
);
5496 map
= isl_map_intersect_range(map
, set2
);
5500 __isl_give isl_map
*isl_set_lex_gt_set(__isl_take isl_set
*set1
,
5501 __isl_take isl_set
*set2
)
5504 map
= isl_map_lex_gt(isl_set_get_space(set1
));
5505 map
= isl_map_intersect_domain(map
, set1
);
5506 map
= isl_map_intersect_range(map
, set2
);
5510 __isl_give isl_map
*isl_map_lex_le_map(__isl_take isl_map
*map1
,
5511 __isl_take isl_map
*map2
)
5514 map
= isl_map_lex_le(isl_space_range(isl_map_get_space(map1
)));
5515 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
5516 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
5520 __isl_give isl_map
*isl_map_lex_lt_map(__isl_take isl_map
*map1
,
5521 __isl_take isl_map
*map2
)
5524 map
= isl_map_lex_lt(isl_space_range(isl_map_get_space(map1
)));
5525 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
5526 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
5530 __isl_give isl_map
*isl_map_lex_ge_map(__isl_take isl_map
*map1
,
5531 __isl_take isl_map
*map2
)
5534 map
= isl_map_lex_ge(isl_space_range(isl_map_get_space(map1
)));
5535 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
5536 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
5540 __isl_give isl_map
*isl_map_lex_gt_map(__isl_take isl_map
*map1
,
5541 __isl_take isl_map
*map2
)
5544 map
= isl_map_lex_gt(isl_space_range(isl_map_get_space(map1
)));
5545 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
5546 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
5550 /* For the div d = floor(f/m) at position "div", add the constraint
5554 static __isl_give isl_basic_map
*add_upper_div_constraint(
5555 __isl_take isl_basic_map
*bmap
, unsigned div
)
5558 isl_size v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
5562 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
5563 if (v_div
< 0 || n_div
< 0)
5564 return isl_basic_map_free(bmap
);
5566 i
= isl_basic_map_alloc_inequality(bmap
);
5568 return isl_basic_map_free(bmap
);
5569 isl_seq_cpy(bmap
->ineq
[i
], bmap
->div
[div
] + 1, 1 + v_div
+ n_div
);
5570 isl_int_neg(bmap
->ineq
[i
][1 + pos
], bmap
->div
[div
][0]);
5575 /* For the div d = floor(f/m) at position "div", add the constraint
5577 * -(f-(m-1)) + m d >= 0
5579 static __isl_give isl_basic_map
*add_lower_div_constraint(
5580 __isl_take isl_basic_map
*bmap
, unsigned div
)
5583 isl_size v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
5587 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
5588 if (v_div
< 0 || n_div
< 0)
5589 return isl_basic_map_free(bmap
);
5591 i
= isl_basic_map_alloc_inequality(bmap
);
5593 return isl_basic_map_free(bmap
);
5594 isl_seq_neg(bmap
->ineq
[i
], bmap
->div
[div
] + 1, 1 + v_div
+ n_div
);
5595 isl_int_set(bmap
->ineq
[i
][1 + pos
], bmap
->div
[div
][0]);
5596 isl_int_add(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], bmap
->ineq
[i
][1 + pos
]);
5597 isl_int_sub_ui(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], 1);
5602 /* For the div d = floor(f/m) at position "pos", add the constraints
5605 * -(f-(m-1)) + m d >= 0
5607 * Note that the second constraint is the negation of
5611 __isl_give isl_basic_map
*isl_basic_map_add_div_constraints(
5612 __isl_take isl_basic_map
*bmap
, unsigned pos
)
5614 bmap
= add_upper_div_constraint(bmap
, pos
);
5615 bmap
= add_lower_div_constraint(bmap
, pos
);
5619 /* For each known div d = floor(f/m), add the constraints
5622 * -(f-(m-1)) + m d >= 0
5624 * Remove duplicate constraints in case of some these div constraints
5625 * already appear in "bmap".
5627 __isl_give isl_basic_map
*isl_basic_map_add_known_div_constraints(
5628 __isl_take isl_basic_map
*bmap
)
5632 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
5634 return isl_basic_map_free(bmap
);
5638 bmap
= add_known_div_constraints(bmap
);
5639 bmap
= isl_basic_map_remove_duplicate_constraints(bmap
, NULL
, 0);
5640 bmap
= isl_basic_map_finalize(bmap
);
5644 /* Add the div constraint of sign "sign" for div "div" of "bmap".
5646 * In particular, if this div is of the form d = floor(f/m),
5647 * then add the constraint
5651 * if sign < 0 or the constraint
5653 * -(f-(m-1)) + m d >= 0
5657 __isl_give isl_basic_map
*isl_basic_map_add_div_constraint(
5658 __isl_take isl_basic_map
*bmap
, unsigned div
, int sign
)
5661 return add_upper_div_constraint(bmap
, div
);
5663 return add_lower_div_constraint(bmap
, div
);
5666 __isl_give isl_basic_set
*isl_basic_map_underlying_set(
5667 __isl_take isl_basic_map
*bmap
)
5673 if (bmap
->dim
->nparam
== 0 && bmap
->dim
->n_in
== 0 &&
5675 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_in
) &&
5676 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_out
))
5677 return bset_from_bmap(bmap
);
5678 bmap
= isl_basic_map_cow(bmap
);
5681 space
= isl_basic_map_take_space(bmap
);
5682 space
= isl_space_underlying(space
, bmap
->n_div
);
5683 bmap
= isl_basic_map_restore_space(bmap
, space
);
5686 bmap
->extra
-= bmap
->n_div
;
5688 bmap
= isl_basic_map_finalize(bmap
);
5689 return bset_from_bmap(bmap
);
5691 isl_basic_map_free(bmap
);
5695 __isl_give isl_basic_set
*isl_basic_set_underlying_set(
5696 __isl_take isl_basic_set
*bset
)
5698 return isl_basic_map_underlying_set(bset_to_bmap(bset
));
5701 /* Replace each element in "list" by the result of applying
5702 * isl_basic_map_underlying_set to the element.
5704 __isl_give isl_basic_set_list
*isl_basic_map_list_underlying_set(
5705 __isl_take isl_basic_map_list
*list
)
5710 n
= isl_basic_map_list_n_basic_map(list
);
5714 for (i
= 0; i
< n
; ++i
) {
5715 isl_basic_map
*bmap
;
5716 isl_basic_set
*bset
;
5718 bmap
= isl_basic_map_list_get_basic_map(list
, i
);
5719 bset
= isl_basic_set_underlying_set(bmap
);
5720 list
= isl_basic_set_list_set_basic_set(list
, i
, bset
);
5725 isl_basic_map_list_free(list
);
5729 __isl_give isl_basic_map
*isl_basic_map_overlying_set(
5730 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_map
*like
)
5732 struct isl_basic_map
*bmap
;
5733 struct isl_ctx
*ctx
;
5734 isl_size dim
, bmap_total
;
5741 if (isl_basic_set_check_no_params(bset
) < 0 ||
5742 isl_basic_set_check_no_locals(bset
) < 0)
5744 dim
= isl_basic_set_dim(bset
, isl_dim_set
);
5745 bmap_total
= isl_basic_map_dim(like
, isl_dim_all
);
5746 if (dim
< 0 || bmap_total
< 0)
5748 isl_assert(ctx
, dim
== bmap_total
, goto error
);
5749 if (like
->n_div
== 0) {
5750 isl_space
*space
= isl_basic_map_get_space(like
);
5751 isl_basic_map_free(like
);
5752 return isl_basic_map_reset_space(bset
, space
);
5754 bset
= isl_basic_set_cow(bset
);
5757 total
= dim
+ bset
->extra
;
5758 bmap
= bset_to_bmap(bset
);
5759 isl_space_free(isl_basic_map_take_space(bmap
));
5760 bmap
= isl_basic_map_restore_space(bmap
, isl_basic_map_get_space(like
));
5763 bmap
->n_div
= like
->n_div
;
5764 bmap
->extra
+= like
->n_div
;
5768 ltotal
= total
- bmap
->extra
+ like
->extra
;
5771 bmap
->block2
= isl_blk_extend(ctx
, bmap
->block2
,
5772 bmap
->extra
* (1 + 1 + total
));
5773 if (isl_blk_is_error(bmap
->block2
))
5775 div
= isl_realloc_array(ctx
, bmap
->div
, isl_int
*, bmap
->extra
);
5779 for (i
= 0; i
< bmap
->extra
; ++i
)
5780 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + 1 + total
);
5781 for (i
= 0; i
< like
->n_div
; ++i
) {
5782 isl_seq_cpy(bmap
->div
[i
], like
->div
[i
], 1 + 1 + ltotal
);
5783 isl_seq_clr(bmap
->div
[i
]+1+1+ltotal
, total
- ltotal
);
5785 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
5787 isl_basic_map_free(like
);
5788 bmap
= isl_basic_map_simplify(bmap
);
5789 bmap
= isl_basic_map_finalize(bmap
);
5792 isl_basic_map_free(like
);
5793 isl_basic_set_free(bset
);
5797 __isl_give isl_basic_set
*isl_basic_set_from_underlying_set(
5798 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*like
)
5800 return bset_from_bmap(isl_basic_map_overlying_set(bset
,
5801 bset_to_bmap(like
)));
5804 __isl_give isl_set
*isl_map_underlying_set(__isl_take isl_map
*map
)
5808 map
= isl_map_cow(map
);
5811 map
->dim
= isl_space_cow(map
->dim
);
5815 for (i
= 1; i
< map
->n
; ++i
)
5816 isl_assert(map
->ctx
, map
->p
[0]->n_div
== map
->p
[i
]->n_div
,
5818 for (i
= 0; i
< map
->n
; ++i
) {
5819 map
->p
[i
] = bset_to_bmap(
5820 isl_basic_map_underlying_set(map
->p
[i
]));
5825 map
->dim
= isl_space_underlying(map
->dim
, 0);
5827 isl_space_free(map
->dim
);
5828 map
->dim
= isl_space_copy(map
->p
[0]->dim
);
5832 return set_from_map(map
);
5838 /* Replace the space of "bmap" by "space".
5840 * If the space of "bmap" is identical to "space" (including the identifiers
5841 * of the input and output dimensions), then simply return the original input.
5843 __isl_give isl_basic_map
*isl_basic_map_reset_space(
5844 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*space
)
5847 isl_space
*bmap_space
;
5849 bmap_space
= isl_basic_map_peek_space(bmap
);
5850 equal
= isl_space_is_equal(bmap_space
, space
);
5851 if (equal
>= 0 && equal
)
5852 equal
= isl_space_has_equal_ids(bmap_space
, space
);
5856 isl_space_free(space
);
5859 isl_space_free(isl_basic_map_take_space(bmap
));
5860 bmap
= isl_basic_map_restore_space(bmap
, space
);
5862 bmap
= isl_basic_map_finalize(bmap
);
5866 isl_basic_map_free(bmap
);
5867 isl_space_free(space
);
5871 __isl_give isl_basic_set
*isl_basic_set_reset_space(
5872 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*space
)
5874 return bset_from_bmap(isl_basic_map_reset_space(bset_to_bmap(bset
),
5878 /* Check that the total dimensions of "map" and "space" are the same.
5880 static isl_stat
check_map_space_equal_total_dim(__isl_keep isl_map
*map
,
5881 __isl_keep isl_space
*space
)
5883 isl_size dim1
, dim2
;
5885 dim1
= isl_map_dim(map
, isl_dim_all
);
5886 dim2
= isl_space_dim(space
, isl_dim_all
);
5887 if (dim1
< 0 || dim2
< 0)
5888 return isl_stat_error
;
5891 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
5892 "total dimensions do not match", return isl_stat_error
);
5895 __isl_give isl_map
*isl_map_reset_space(__isl_take isl_map
*map
,
5896 __isl_take isl_space
*space
)
5900 map
= isl_map_cow(map
);
5904 for (i
= 0; i
< map
->n
; ++i
) {
5905 map
->p
[i
] = isl_basic_map_reset_space(map
->p
[i
],
5906 isl_space_copy(space
));
5910 isl_space_free(isl_map_take_space(map
));
5911 map
= isl_map_restore_space(map
, space
);
5916 isl_space_free(space
);
5920 /* Replace the space of "map" by "space", without modifying
5921 * the dimension of "map".
5923 * If the space of "map" is identical to "space" (including the identifiers
5924 * of the input and output dimensions), then simply return the original input.
5926 __isl_give isl_map
*isl_map_reset_equal_dim_space(__isl_take isl_map
*map
,
5927 __isl_take isl_space
*space
)
5930 isl_space
*map_space
;
5932 map_space
= isl_map_peek_space(map
);
5933 equal
= isl_space_is_equal(map_space
, space
);
5934 if (equal
>= 0 && equal
)
5935 equal
= isl_space_has_equal_ids(map_space
, space
);
5939 isl_space_free(space
);
5942 if (check_map_space_equal_total_dim(map
, space
) < 0)
5944 return isl_map_reset_space(map
, space
);
5947 isl_space_free(space
);
5951 __isl_give isl_set
*isl_set_reset_space(__isl_take isl_set
*set
,
5952 __isl_take isl_space
*space
)
5954 return set_from_map(isl_map_reset_space(set_to_map(set
), space
));
5957 /* Compute the parameter domain of the given basic set.
5959 __isl_give isl_basic_set
*isl_basic_set_params(__isl_take isl_basic_set
*bset
)
5965 is_params
= isl_basic_set_is_params(bset
);
5967 return isl_basic_set_free(bset
);
5971 n
= isl_basic_set_dim(bset
, isl_dim_set
);
5973 return isl_basic_set_free(bset
);
5974 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
5975 space
= isl_basic_set_get_space(bset
);
5976 space
= isl_space_params(space
);
5977 bset
= isl_basic_set_reset_space(bset
, space
);
5981 /* Construct a zero-dimensional basic set with the given parameter domain.
5983 __isl_give isl_basic_set
*isl_basic_set_from_params(
5984 __isl_take isl_basic_set
*bset
)
5987 space
= isl_basic_set_get_space(bset
);
5988 space
= isl_space_set_from_params(space
);
5989 bset
= isl_basic_set_reset_space(bset
, space
);
5993 /* Compute the parameter domain of the given set.
5995 __isl_give isl_set
*isl_set_params(__isl_take isl_set
*set
)
5997 return isl_map_params(set_to_map(set
));
6000 /* Construct a zero-dimensional set with the given parameter domain.
6002 __isl_give isl_set
*isl_set_from_params(__isl_take isl_set
*set
)
6005 space
= isl_set_get_space(set
);
6006 space
= isl_space_set_from_params(space
);
6007 set
= isl_set_reset_space(set
, space
);
6011 /* Compute the parameter domain of the given map.
6013 __isl_give isl_set
*isl_map_params(__isl_take isl_map
*map
)
6016 isl_size n_in
, n_out
;
6018 n_in
= isl_map_dim(map
, isl_dim_in
);
6019 n_out
= isl_map_dim(map
, isl_dim_out
);
6020 if (n_in
< 0 || n_out
< 0)
6021 return isl_map_free(map
);
6022 map
= isl_map_project_out(map
, isl_dim_in
, 0, n_in
);
6023 map
= isl_map_project_out(map
, isl_dim_out
, 0, n_out
);
6024 space
= isl_map_get_space(map
);
6025 space
= isl_space_params(space
);
6026 map
= isl_map_reset_space(map
, space
);
6030 __isl_give isl_basic_set
*isl_basic_map_domain(__isl_take isl_basic_map
*bmap
)
6035 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
6037 return isl_basic_map_free(bmap
);
6038 space
= isl_space_domain(isl_basic_map_get_space(bmap
));
6040 bmap
= isl_basic_map_project_out(bmap
, isl_dim_out
, 0, n_out
);
6042 return isl_basic_map_reset_space(bmap
, space
);
6045 isl_bool
isl_basic_map_may_be_set(__isl_keep isl_basic_map
*bmap
)
6048 return isl_bool_error
;
6049 return isl_space_may_be_set(bmap
->dim
);
6052 /* Is this basic map actually a set?
6053 * Users should never call this function. Outside of isl,
6054 * the type should indicate whether something is a set or a map.
6056 isl_bool
isl_basic_map_is_set(__isl_keep isl_basic_map
*bmap
)
6059 return isl_bool_error
;
6060 return isl_space_is_set(bmap
->dim
);
6063 __isl_give isl_basic_set
*isl_basic_map_range(__isl_take isl_basic_map
*bmap
)
6067 is_set
= isl_basic_map_is_set(bmap
);
6072 return isl_basic_map_domain(isl_basic_map_reverse(bmap
));
6074 isl_basic_map_free(bmap
);
6078 __isl_give isl_basic_map
*isl_basic_map_domain_map(
6079 __isl_take isl_basic_map
*bmap
)
6083 isl_basic_map
*domain
;
6084 isl_size nparam
, n_in
, n_out
;
6086 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
6087 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
6088 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
6089 if (nparam
< 0 || n_in
< 0 || n_out
< 0)
6090 return isl_basic_map_free(bmap
);
6092 space
= isl_basic_map_get_space(bmap
);
6093 space
= isl_space_from_range(isl_space_domain(space
));
6094 domain
= isl_basic_map_universe(space
);
6096 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
6097 bmap
= isl_basic_map_apply_range(bmap
, domain
);
6098 bmap
= isl_basic_map_extend_constraints(bmap
, n_in
, 0);
6100 for (i
= 0; i
< n_in
; ++i
)
6101 bmap
= isl_basic_map_equate(bmap
, isl_dim_in
, i
,
6104 bmap
= isl_basic_map_gauss(bmap
, NULL
);
6105 return isl_basic_map_finalize(bmap
);
6108 __isl_give isl_basic_map
*isl_basic_map_range_map(
6109 __isl_take isl_basic_map
*bmap
)
6113 isl_basic_map
*range
;
6114 isl_size nparam
, n_in
, n_out
;
6116 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
6117 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
6118 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
6119 if (nparam
< 0 || n_in
< 0 || n_out
< 0)
6120 return isl_basic_map_free(bmap
);
6122 space
= isl_basic_map_get_space(bmap
);
6123 space
= isl_space_from_range(isl_space_range(space
));
6124 range
= isl_basic_map_universe(space
);
6126 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
6127 bmap
= isl_basic_map_apply_range(bmap
, range
);
6128 bmap
= isl_basic_map_extend_constraints(bmap
, n_out
, 0);
6130 for (i
= 0; i
< n_out
; ++i
)
6131 bmap
= isl_basic_map_equate(bmap
, isl_dim_in
, n_in
+ i
,
6134 bmap
= isl_basic_map_gauss(bmap
, NULL
);
6135 return isl_basic_map_finalize(bmap
);
6138 int isl_map_may_be_set(__isl_keep isl_map
*map
)
6142 return isl_space_may_be_set(map
->dim
);
6145 /* Is this map actually a set?
6146 * Users should never call this function. Outside of isl,
6147 * the type should indicate whether something is a set or a map.
6149 isl_bool
isl_map_is_set(__isl_keep isl_map
*map
)
6152 return isl_bool_error
;
6153 return isl_space_is_set(map
->dim
);
6156 __isl_give isl_set
*isl_map_range(__isl_take isl_map
*map
)
6161 n_in
= isl_map_dim(map
, isl_dim_in
);
6163 return set_from_map(isl_map_free(map
));
6164 space
= isl_space_range(isl_map_get_space(map
));
6166 map
= isl_map_project_out(map
, isl_dim_in
, 0, n_in
);
6168 return set_from_map(isl_map_reset_space(map
, space
));
6171 /* Transform "map" by applying "fn_space" to its space and "fn_bmap"
6172 * to each of its basic maps.
6174 static __isl_give isl_map
*isl_map_transform(__isl_take isl_map
*map
,
6175 __isl_give isl_space
*(*fn_space
)(__isl_take isl_space
*space
),
6176 __isl_give isl_basic_map
*(*fn_bmap
)(__isl_take isl_basic_map
*bmap
))
6181 map
= isl_map_cow(map
);
6185 for (i
= 0; i
< map
->n
; ++i
) {
6186 map
->p
[i
] = fn_bmap(map
->p
[i
]);
6188 return isl_map_free(map
);
6190 map
= isl_map_unmark_normalized(map
);
6192 space
= isl_map_take_space(map
);
6193 space
= fn_space(space
);
6194 map
= isl_map_restore_space(map
, space
);
6199 __isl_give isl_map
*isl_map_domain_map(__isl_take isl_map
*map
)
6201 return isl_map_transform(map
, &isl_space_domain_map
,
6202 &isl_basic_map_domain_map
);
6205 __isl_give isl_map
*isl_map_range_map(__isl_take isl_map
*map
)
6207 return isl_map_transform(map
, &isl_space_range_map
,
6208 &isl_basic_map_range_map
);
6211 /* Given a wrapped map of the form A[B -> C],
6212 * return the map A[B -> C] -> B.
6214 __isl_give isl_map
*isl_set_wrapped_domain_map(__isl_take isl_set
*set
)
6221 if (!isl_set_has_tuple_id(set
))
6222 return isl_map_domain_map(isl_set_unwrap(set
));
6224 id
= isl_set_get_tuple_id(set
);
6225 map
= isl_map_domain_map(isl_set_unwrap(set
));
6226 map
= isl_map_set_tuple_id(map
, isl_dim_in
, id
);
6231 __isl_give isl_basic_map
*isl_basic_map_from_domain(
6232 __isl_take isl_basic_set
*bset
)
6234 return isl_basic_map_reverse(isl_basic_map_from_range(bset
));
6237 __isl_give isl_basic_map
*isl_basic_map_from_range(
6238 __isl_take isl_basic_set
*bset
)
6241 space
= isl_basic_set_get_space(bset
);
6242 space
= isl_space_from_range(space
);
6243 bset
= isl_basic_set_reset_space(bset
, space
);
6244 return bset_to_bmap(bset
);
6247 /* Create a relation with the given set as range.
6248 * The domain of the created relation is a zero-dimensional
6249 * flat anonymous space.
6251 __isl_give isl_map
*isl_map_from_range(__isl_take isl_set
*set
)
6254 space
= isl_set_get_space(set
);
6255 space
= isl_space_from_range(space
);
6256 set
= isl_set_reset_space(set
, space
);
6257 return set_to_map(set
);
6260 /* Create a relation with the given set as domain.
6261 * The range of the created relation is a zero-dimensional
6262 * flat anonymous space.
6264 __isl_give isl_map
*isl_map_from_domain(__isl_take isl_set
*set
)
6266 return isl_map_reverse(isl_map_from_range(set
));
6269 __isl_give isl_basic_map
*isl_basic_map_from_domain_and_range(
6270 __isl_take isl_basic_set
*domain
, __isl_take isl_basic_set
*range
)
6272 return isl_basic_map_apply_range(isl_basic_map_reverse(domain
), range
);
6275 __isl_give isl_map
*isl_map_from_domain_and_range(__isl_take isl_set
*domain
,
6276 __isl_take isl_set
*range
)
6278 return isl_map_apply_range(isl_map_reverse(domain
), range
);
6281 /* Return a newly allocated isl_map with given space and flags and
6282 * room for "n" basic maps.
6283 * Make sure that all cached information is cleared.
6285 __isl_give isl_map
*isl_map_alloc_space(__isl_take isl_space
*space
, int n
,
6288 struct isl_map
*map
;
6293 isl_die(space
->ctx
, isl_error_internal
,
6294 "negative number of basic maps", goto error
);
6295 map
= isl_calloc(space
->ctx
, struct isl_map
,
6296 sizeof(struct isl_map
) +
6297 (n
- 1) * sizeof(struct isl_basic_map
*));
6301 map
->ctx
= space
->ctx
;
6302 isl_ctx_ref(map
->ctx
);
6310 isl_space_free(space
);
6314 __isl_give isl_basic_map
*isl_basic_map_empty(__isl_take isl_space
*space
)
6316 struct isl_basic_map
*bmap
;
6317 bmap
= isl_basic_map_alloc_space(space
, 0, 1, 0);
6318 bmap
= isl_basic_map_set_to_empty(bmap
);
6322 __isl_give isl_basic_set
*isl_basic_set_empty(__isl_take isl_space
*space
)
6324 struct isl_basic_set
*bset
;
6325 bset
= isl_basic_set_alloc_space(space
, 0, 1, 0);
6326 bset
= isl_basic_set_set_to_empty(bset
);
6330 __isl_give isl_basic_map
*isl_basic_map_universe(__isl_take isl_space
*space
)
6332 struct isl_basic_map
*bmap
;
6333 bmap
= isl_basic_map_alloc_space(space
, 0, 0, 0);
6334 bmap
= isl_basic_map_finalize(bmap
);
6338 __isl_give isl_basic_set
*isl_basic_set_universe(__isl_take isl_space
*space
)
6340 struct isl_basic_set
*bset
;
6341 bset
= isl_basic_set_alloc_space(space
, 0, 0, 0);
6342 bset
= isl_basic_set_finalize(bset
);
6346 __isl_give isl_basic_map
*isl_basic_map_nat_universe(
6347 __isl_take isl_space
*space
)
6350 isl_size total
= isl_space_dim(space
, isl_dim_all
);
6351 isl_basic_map
*bmap
;
6354 space
= isl_space_free(space
);
6355 bmap
= isl_basic_map_alloc_space(space
, 0, 0, total
);
6356 for (i
= 0; i
< total
; ++i
) {
6357 int k
= isl_basic_map_alloc_inequality(bmap
);
6360 isl_seq_clr(bmap
->ineq
[k
], 1 + total
);
6361 isl_int_set_si(bmap
->ineq
[k
][1 + i
], 1);
6365 isl_basic_map_free(bmap
);
6369 __isl_give isl_basic_set
*isl_basic_set_nat_universe(
6370 __isl_take isl_space
*space
)
6372 return isl_basic_map_nat_universe(space
);
6375 __isl_give isl_map
*isl_map_nat_universe(__isl_take isl_space
*space
)
6377 return isl_map_from_basic_map(isl_basic_map_nat_universe(space
));
6380 __isl_give isl_set
*isl_set_nat_universe(__isl_take isl_space
*space
)
6382 return isl_map_nat_universe(space
);
6385 __isl_give isl_map
*isl_map_empty(__isl_take isl_space
*space
)
6387 return isl_map_alloc_space(space
, 0, ISL_MAP_DISJOINT
);
6390 __isl_give isl_set
*isl_set_empty(__isl_take isl_space
*space
)
6392 return isl_set_alloc_space(space
, 0, ISL_MAP_DISJOINT
);
6395 __isl_give isl_map
*isl_map_universe(__isl_take isl_space
*space
)
6397 struct isl_map
*map
;
6400 map
= isl_map_alloc_space(isl_space_copy(space
), 1, ISL_MAP_DISJOINT
);
6401 map
= isl_map_add_basic_map(map
, isl_basic_map_universe(space
));
6405 /* This function performs the same operation as isl_map_universe,
6406 * but is considered as a function on an isl_space when exported.
6408 __isl_give isl_map
*isl_space_universe_map(__isl_take isl_space
*space
)
6410 return isl_map_universe(space
);
6413 __isl_give isl_set
*isl_set_universe(__isl_take isl_space
*space
)
6415 struct isl_set
*set
;
6418 set
= isl_set_alloc_space(isl_space_copy(space
), 1, ISL_MAP_DISJOINT
);
6419 set
= isl_set_add_basic_set(set
, isl_basic_set_universe(space
));
6423 /* This function performs the same operation as isl_set_universe,
6424 * but is considered as a function on an isl_space when exported.
6426 __isl_give isl_set
*isl_space_universe_set(__isl_take isl_space
*space
)
6428 return isl_set_universe(space
);
6431 __isl_give isl_map
*isl_map_dup(__isl_keep isl_map
*map
)
6434 struct isl_map
*dup
;
6438 dup
= isl_map_alloc_space(isl_space_copy(map
->dim
), map
->n
, map
->flags
);
6439 for (i
= 0; i
< map
->n
; ++i
)
6440 dup
= isl_map_add_basic_map(dup
, isl_basic_map_copy(map
->p
[i
]));
6444 __isl_give isl_map
*isl_map_add_basic_map(__isl_take isl_map
*map
,
6445 __isl_take isl_basic_map
*bmap
)
6449 if (isl_basic_map_plain_is_empty(bmap
)) {
6450 isl_basic_map_free(bmap
);
6453 if (isl_map_basic_map_check_equal_space(map
, bmap
) < 0)
6455 isl_assert(map
->ctx
, map
->n
< map
->size
, goto error
);
6456 map
->p
[map
->n
] = bmap
;
6458 map
= isl_map_unmark_normalized(map
);
6464 isl_basic_map_free(bmap
);
6468 __isl_null isl_map
*isl_map_free(__isl_take isl_map
*map
)
6479 isl_ctx_deref(map
->ctx
);
6480 for (i
= 0; i
< map
->n
; ++i
)
6481 isl_basic_map_free(map
->p
[i
]);
6482 isl_space_free(map
->dim
);
6488 static __isl_give isl_basic_map
*isl_basic_map_fix_pos_si(
6489 __isl_take isl_basic_map
*bmap
, unsigned pos
, int value
)
6494 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
6496 return isl_basic_map_free(bmap
);
6498 bmap
= isl_basic_map_cow(bmap
);
6499 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
6500 j
= isl_basic_map_alloc_equality(bmap
);
6503 isl_seq_clr(bmap
->eq
[j
] + 1, total
);
6504 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
6505 isl_int_set_si(bmap
->eq
[j
][0], value
);
6506 bmap
= isl_basic_map_simplify(bmap
);
6507 return isl_basic_map_finalize(bmap
);
6509 isl_basic_map_free(bmap
);
6513 static __isl_give isl_basic_map
*isl_basic_map_fix_pos(
6514 __isl_take isl_basic_map
*bmap
, unsigned pos
, isl_int value
)
6519 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
6521 return isl_basic_map_free(bmap
);
6523 bmap
= isl_basic_map_cow(bmap
);
6524 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
6525 j
= isl_basic_map_alloc_equality(bmap
);
6528 isl_seq_clr(bmap
->eq
[j
] + 1, total
);
6529 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
6530 isl_int_set(bmap
->eq
[j
][0], value
);
6531 bmap
= isl_basic_map_simplify(bmap
);
6532 return isl_basic_map_finalize(bmap
);
6534 isl_basic_map_free(bmap
);
6538 __isl_give isl_basic_map
*isl_basic_map_fix_si(__isl_take isl_basic_map
*bmap
,
6539 enum isl_dim_type type
, unsigned pos
, int value
)
6541 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6542 return isl_basic_map_free(bmap
);
6543 return isl_basic_map_fix_pos_si(bmap
,
6544 isl_basic_map_offset(bmap
, type
) + pos
, value
);
6547 __isl_give isl_basic_map
*isl_basic_map_fix(__isl_take isl_basic_map
*bmap
,
6548 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6550 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6551 return isl_basic_map_free(bmap
);
6552 return isl_basic_map_fix_pos(bmap
,
6553 isl_basic_map_offset(bmap
, type
) + pos
, value
);
6556 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
6557 * to be equal to "v".
6559 __isl_give isl_basic_map
*isl_basic_map_fix_val(__isl_take isl_basic_map
*bmap
,
6560 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6564 if (!isl_val_is_int(v
))
6565 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
6566 "expecting integer value", goto error
);
6567 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6569 pos
+= isl_basic_map_offset(bmap
, type
);
6570 bmap
= isl_basic_map_fix_pos(bmap
, pos
, v
->n
);
6574 isl_basic_map_free(bmap
);
6579 /* Fix the value of the variable at position "pos" of type "type" of "bset"
6580 * to be equal to "v".
6582 __isl_give isl_basic_set
*isl_basic_set_fix_val(__isl_take isl_basic_set
*bset
,
6583 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6585 return isl_basic_map_fix_val(bset
, type
, pos
, v
);
6588 __isl_give isl_basic_set
*isl_basic_set_fix_si(__isl_take isl_basic_set
*bset
,
6589 enum isl_dim_type type
, unsigned pos
, int value
)
6591 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset
),
6595 __isl_give isl_basic_set
*isl_basic_set_fix(__isl_take isl_basic_set
*bset
,
6596 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6598 return bset_from_bmap(isl_basic_map_fix(bset_to_bmap(bset
),
6602 /* Remove the basic map at position "i" from "map" if this basic map
6603 * is (obviously) empty.
6605 static __isl_give isl_map
*remove_if_empty(__isl_take isl_map
*map
, int i
)
6612 empty
= isl_basic_map_plain_is_empty(map
->p
[i
]);
6614 return isl_map_free(map
);
6618 isl_basic_map_free(map
->p
[i
]);
6621 map
->p
[i
] = map
->p
[map
->n
];
6622 map
= isl_map_unmark_normalized(map
);
6629 /* Perform "fn" on each basic map of "map", where we may not be holding
6630 * the only reference to "map".
6631 * In particular, "fn" should be a semantics preserving operation
6632 * that we want to apply to all copies of "map". We therefore need
6633 * to be careful not to modify "map" in a way that breaks "map"
6634 * in case anything goes wrong.
6636 __isl_give isl_map
*isl_map_inline_foreach_basic_map(__isl_take isl_map
*map
,
6637 __isl_give isl_basic_map
*(*fn
)(__isl_take isl_basic_map
*bmap
))
6639 struct isl_basic_map
*bmap
;
6645 for (i
= map
->n
- 1; i
>= 0; --i
) {
6646 bmap
= isl_basic_map_copy(map
->p
[i
]);
6650 isl_basic_map_free(map
->p
[i
]);
6652 map
= remove_if_empty(map
, i
);
6663 __isl_give isl_map
*isl_map_fix_si(__isl_take isl_map
*map
,
6664 enum isl_dim_type type
, unsigned pos
, int value
)
6668 map
= isl_map_cow(map
);
6669 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
6670 return isl_map_free(map
);
6671 for (i
= map
->n
- 1; i
>= 0; --i
) {
6672 map
->p
[i
] = isl_basic_map_fix_si(map
->p
[i
], type
, pos
, value
);
6673 map
= remove_if_empty(map
, i
);
6677 map
= isl_map_unmark_normalized(map
);
6681 __isl_give isl_set
*isl_set_fix_si(__isl_take isl_set
*set
,
6682 enum isl_dim_type type
, unsigned pos
, int value
)
6684 return set_from_map(isl_map_fix_si(set_to_map(set
), type
, pos
, value
));
6687 __isl_give isl_map
*isl_map_fix(__isl_take isl_map
*map
,
6688 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6692 map
= isl_map_cow(map
);
6693 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
6694 return isl_map_free(map
);
6695 for (i
= 0; i
< map
->n
; ++i
) {
6696 map
->p
[i
] = isl_basic_map_fix(map
->p
[i
], type
, pos
, value
);
6700 map
= isl_map_unmark_normalized(map
);
6707 __isl_give isl_set
*isl_set_fix(__isl_take isl_set
*set
,
6708 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6710 return set_from_map(isl_map_fix(set_to_map(set
), type
, pos
, value
));
6713 /* Fix the value of the variable at position "pos" of type "type" of "map"
6714 * to be equal to "v".
6716 __isl_give isl_map
*isl_map_fix_val(__isl_take isl_map
*map
,
6717 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6721 map
= isl_map_cow(map
);
6725 if (!isl_val_is_int(v
))
6726 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
6727 "expecting integer value", goto error
);
6728 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
6730 for (i
= map
->n
- 1; i
>= 0; --i
) {
6731 map
->p
[i
] = isl_basic_map_fix_val(map
->p
[i
], type
, pos
,
6733 map
= remove_if_empty(map
, i
);
6737 map
= isl_map_unmark_normalized(map
);
6746 /* Fix the value of the variable at position "pos" of type "type" of "set"
6747 * to be equal to "v".
6749 __isl_give isl_set
*isl_set_fix_val(__isl_take isl_set
*set
,
6750 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6752 return isl_map_fix_val(set
, type
, pos
, v
);
6755 __isl_give isl_map
*isl_map_fix_input_si(__isl_take isl_map
*map
,
6756 unsigned input
, int value
)
6758 return isl_map_fix_si(map
, isl_dim_in
, input
, value
);
6761 __isl_give isl_set
*isl_set_fix_dim_si(__isl_take isl_set
*set
, unsigned dim
,
6764 return set_from_map(isl_map_fix_si(set_to_map(set
),
6765 isl_dim_set
, dim
, value
));
6768 static __isl_give isl_basic_map
*basic_map_bound_si(
6769 __isl_take isl_basic_map
*bmap
,
6770 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
6775 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6776 return isl_basic_map_free(bmap
);
6777 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
6779 return isl_basic_map_free(bmap
);
6780 pos
+= isl_basic_map_offset(bmap
, type
);
6781 bmap
= isl_basic_map_cow(bmap
);
6782 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
6783 j
= isl_basic_map_alloc_inequality(bmap
);
6786 isl_seq_clr(bmap
->ineq
[j
], 1 + total
);
6788 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
6789 isl_int_set_si(bmap
->ineq
[j
][0], value
);
6791 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
6792 isl_int_set_si(bmap
->ineq
[j
][0], -value
);
6794 bmap
= isl_basic_map_simplify(bmap
);
6795 return isl_basic_map_finalize(bmap
);
6797 isl_basic_map_free(bmap
);
6801 __isl_give isl_basic_map
*isl_basic_map_lower_bound_si(
6802 __isl_take isl_basic_map
*bmap
,
6803 enum isl_dim_type type
, unsigned pos
, int value
)
6805 return basic_map_bound_si(bmap
, type
, pos
, value
, 0);
6808 /* Constrain the values of the given dimension to be no greater than "value".
6810 __isl_give isl_basic_map
*isl_basic_map_upper_bound_si(
6811 __isl_take isl_basic_map
*bmap
,
6812 enum isl_dim_type type
, unsigned pos
, int value
)
6814 return basic_map_bound_si(bmap
, type
, pos
, value
, 1);
6817 static __isl_give isl_map
*map_bound_si(__isl_take isl_map
*map
,
6818 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
6822 map
= isl_map_cow(map
);
6823 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
6824 return isl_map_free(map
);
6825 for (i
= 0; i
< map
->n
; ++i
) {
6826 map
->p
[i
] = basic_map_bound_si(map
->p
[i
],
6827 type
, pos
, value
, upper
);
6831 map
= isl_map_unmark_normalized(map
);
6838 __isl_give isl_map
*isl_map_lower_bound_si(__isl_take isl_map
*map
,
6839 enum isl_dim_type type
, unsigned pos
, int value
)
6841 return map_bound_si(map
, type
, pos
, value
, 0);
6844 __isl_give isl_map
*isl_map_upper_bound_si(__isl_take isl_map
*map
,
6845 enum isl_dim_type type
, unsigned pos
, int value
)
6847 return map_bound_si(map
, type
, pos
, value
, 1);
6850 __isl_give isl_set
*isl_set_lower_bound_si(__isl_take isl_set
*set
,
6851 enum isl_dim_type type
, unsigned pos
, int value
)
6853 return set_from_map(isl_map_lower_bound_si(set_to_map(set
),
6857 __isl_give isl_set
*isl_set_upper_bound_si(__isl_take isl_set
*set
,
6858 enum isl_dim_type type
, unsigned pos
, int value
)
6860 return isl_map_upper_bound_si(set
, type
, pos
, value
);
6863 /* Bound the given variable of "bmap" from below (or above is "upper"
6864 * is set) to "value".
6866 static __isl_give isl_basic_map
*basic_map_bound(
6867 __isl_take isl_basic_map
*bmap
,
6868 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
6873 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6874 return isl_basic_map_free(bmap
);
6875 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
6877 return isl_basic_map_free(bmap
);
6878 pos
+= isl_basic_map_offset(bmap
, type
);
6879 bmap
= isl_basic_map_cow(bmap
);
6880 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
6881 j
= isl_basic_map_alloc_inequality(bmap
);
6884 isl_seq_clr(bmap
->ineq
[j
], 1 + total
);
6886 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
6887 isl_int_set(bmap
->ineq
[j
][0], value
);
6889 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
6890 isl_int_neg(bmap
->ineq
[j
][0], value
);
6892 bmap
= isl_basic_map_simplify(bmap
);
6893 return isl_basic_map_finalize(bmap
);
6895 isl_basic_map_free(bmap
);
6899 /* Bound the given variable of "map" from below (or above is "upper"
6900 * is set) to "value".
6902 static __isl_give isl_map
*map_bound(__isl_take isl_map
*map
,
6903 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
6907 map
= isl_map_cow(map
);
6908 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
6909 return isl_map_free(map
);
6910 for (i
= map
->n
- 1; i
>= 0; --i
) {
6911 map
->p
[i
] = basic_map_bound(map
->p
[i
], type
, pos
, value
, upper
);
6912 map
= remove_if_empty(map
, i
);
6916 map
= isl_map_unmark_normalized(map
);
6920 __isl_give isl_map
*isl_map_lower_bound(__isl_take isl_map
*map
,
6921 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6923 return map_bound(map
, type
, pos
, value
, 0);
6926 __isl_give isl_map
*isl_map_upper_bound(__isl_take isl_map
*map
,
6927 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6929 return map_bound(map
, type
, pos
, value
, 1);
6932 __isl_give isl_set
*isl_set_lower_bound(__isl_take isl_set
*set
,
6933 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6935 return isl_map_lower_bound(set
, type
, pos
, value
);
6938 __isl_give isl_set
*isl_set_upper_bound(__isl_take isl_set
*set
,
6939 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6941 return isl_map_upper_bound(set
, type
, pos
, value
);
6944 /* Force the values of the variable at position "pos" of type "type" of "map"
6945 * to be no smaller than "value".
6947 __isl_give isl_map
*isl_map_lower_bound_val(__isl_take isl_map
*map
,
6948 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
6952 if (!isl_val_is_int(value
))
6953 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
6954 "expecting integer value", goto error
);
6955 map
= isl_map_lower_bound(map
, type
, pos
, value
->n
);
6956 isl_val_free(value
);
6959 isl_val_free(value
);
6964 /* Force the values of the variable at position "pos" of type "type" of "set"
6965 * to be no smaller than "value".
6967 __isl_give isl_set
*isl_set_lower_bound_val(__isl_take isl_set
*set
,
6968 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
6972 map
= set_to_map(set
);
6973 return set_from_map(isl_map_lower_bound_val(map
, type
, pos
, value
));
6976 /* Force the values of the variable at position "pos" of type "type" of "map"
6977 * to be no greater than "value".
6979 __isl_give isl_map
*isl_map_upper_bound_val(__isl_take isl_map
*map
,
6980 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
6984 if (!isl_val_is_int(value
))
6985 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
6986 "expecting integer value", goto error
);
6987 map
= isl_map_upper_bound(map
, type
, pos
, value
->n
);
6988 isl_val_free(value
);
6991 isl_val_free(value
);
6996 /* Force the values of the variable at position "pos" of type "type" of "set"
6997 * to be no greater than "value".
6999 __isl_give isl_set
*isl_set_upper_bound_val(__isl_take isl_set
*set
,
7000 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
7004 map
= set_to_map(set
);
7005 return set_from_map(isl_map_upper_bound_val(map
, type
, pos
, value
));
7008 /* If "mv" has an explicit domain, then intersect the domain of "map"
7009 * with this explicit domain.
7011 * An isl_multi_val object never has an explicit domain,
7012 * so simply return "map".
7014 static __isl_give isl_map
*isl_map_intersect_multi_val_explicit_domain(
7015 __isl_take isl_map
*map
, __isl_keep isl_multi_val
*mv
)
7022 #include "isl_map_bound_templ.c"
7024 /* Apply "map_bound" to "set" with the corresponding value in "bound"
7025 * for each set dimension, by treating the set as a map.
7027 static __isl_give isl_set
*set_bound_multi_val(__isl_take isl_set
*set
,
7028 __isl_take isl_multi_val
*bound
,
7029 __isl_give isl_map
*map_bound(__isl_take isl_map
*map
,
7030 unsigned pos
, __isl_take isl_val
*value
))
7034 map
= set_to_map(set
);
7035 return set_from_map(map_bound_multi_val(map
, bound
, map_bound
));
7040 #include "isl_map_bound_templ.c"
7042 /* Apply "map_bound" to "set" with the corresponding value in "bound"
7043 * for each set dimension, by converting the set and the bound
7044 * to objects living in a map space.
7046 static __isl_give isl_set
*set_bound_multi_pw_aff(__isl_take isl_set
*set
,
7047 __isl_take isl_multi_pw_aff
*bound
,
7048 __isl_give isl_map
*set_bound(__isl_take isl_map
*map
,
7049 unsigned pos
, __isl_take TYPE
*value
))
7053 map
= isl_map_from_range(set
);
7054 bound
= isl_multi_pw_aff_from_range(bound
);
7055 map
= map_bound_multi_pw_aff(map
, bound
, set_bound
);
7056 return isl_map_range(map
);
7059 /* Wrapper around isl_map_lower_bound_val for use in map_bound_multi_val,
7060 * setting a bound on the given output dimension.
7062 static __isl_give isl_map
*map_lower_bound_val(__isl_take isl_map
*map
,
7063 unsigned pos
, __isl_take isl_val
*v
)
7065 return isl_map_lower_bound_val(map
, isl_dim_out
, pos
, v
);
7068 /* Force the values of the set dimensions of "set"
7069 * to be no smaller than the corresponding values in "lower".
7071 __isl_give isl_set
*isl_set_lower_bound_multi_val(__isl_take isl_set
*set
,
7072 __isl_take isl_multi_val
*lower
)
7074 return set_bound_multi_val(set
, lower
, &map_lower_bound_val
);
7077 /* Wrapper around isl_map_upper_bound_val for use in map_bound_multi_val,
7078 * setting a bound on the given output dimension.
7080 static __isl_give isl_map
*map_upper_bound_val(__isl_take isl_map
*map
,
7081 unsigned pos
, __isl_take isl_val
*v
)
7083 return isl_map_upper_bound_val(map
, isl_dim_out
, pos
, v
);
7086 /* Force the values of the set dimensions of "set"
7087 * to be no greater than the corresponding values in "upper".
7089 __isl_give isl_set
*isl_set_upper_bound_multi_val(__isl_take isl_set
*set
,
7090 __isl_take isl_multi_val
*upper
)
7092 return set_bound_multi_val(set
, upper
, &map_upper_bound_val
);
7095 /* Force the symbolic constant expression "bound"
7096 * to satisfy the relation "order" with respect to
7097 * the output variable at position "pos" of "map".
7099 * Create an affine expression representing the output variable
7100 * in terms of the range and
7101 * compare it using "order" to "bound" (defined on the domain).
7102 * The result is a relation between elements in domain and range that
7103 * can be intersected with "map".
7105 static __isl_give isl_map
*map_bound_pw_aff(__isl_take isl_map
*map
,
7106 unsigned pos
, __isl_take isl_pw_aff
*bound
,
7107 __isl_give isl_map
*(*order
)(__isl_take isl_pw_aff
*pa1
,
7108 __isl_take isl_pw_aff
*pa2
))
7111 isl_local_space
*ls
;
7114 space
= isl_space_range(isl_map_get_space(map
));
7115 ls
= isl_local_space_from_space(space
);
7116 var
= isl_pw_aff_var_on_domain(ls
, isl_dim_set
, pos
);
7117 map
= isl_map_intersect(map
, order(bound
, var
));
7121 /* Force the values of the output variable at position "pos" of "map"
7122 * to be no smaller than the symbolic constant expression "lower".
7124 static __isl_give isl_map
*map_lower_bound_pw_aff(__isl_take isl_map
*map
,
7125 unsigned pos
, __isl_take isl_pw_aff
*lower
)
7127 return map_bound_pw_aff(map
, pos
, lower
, &isl_pw_aff_le_map
);
7130 /* Force the values of the output variable at position "pos" of "map"
7131 * to be no greater than the symbolic constant expression "upper".
7133 static __isl_give isl_map
*map_upper_bound_pw_aff(__isl_take isl_map
*map
,
7134 unsigned pos
, __isl_take isl_pw_aff
*upper
)
7136 return map_bound_pw_aff(map
, pos
, upper
, &isl_pw_aff_ge_map
);
7139 /* Force the values of the set dimensions of "set"
7140 * to be no smaller than the corresponding constant symbolic expressions
7143 __isl_give isl_set
*isl_set_lower_bound_multi_pw_aff(__isl_take isl_set
*set
,
7144 __isl_take isl_multi_pw_aff
*lower
)
7146 return set_bound_multi_pw_aff(set
, lower
, &map_lower_bound_pw_aff
);
7149 /* Force the values of the set dimensions of "set"
7150 * to be no greater than the corresponding constant symbolic expressions
7153 __isl_give isl_set
*isl_set_upper_bound_multi_pw_aff(__isl_take isl_set
*set
,
7154 __isl_take isl_multi_pw_aff
*upper
)
7156 return set_bound_multi_pw_aff(set
, upper
, &map_upper_bound_pw_aff
);
7159 /* Force the values of the output dimensions of "map"
7160 * to be no smaller than the corresponding constant symbolic expressions
7163 __isl_give isl_map
*isl_map_lower_bound_multi_pw_aff(__isl_take isl_map
*map
,
7164 __isl_take isl_multi_pw_aff
*lower
)
7166 return map_bound_multi_pw_aff(map
, lower
, &map_lower_bound_pw_aff
);
7169 /* Force the values of the output dimensions of "map"
7170 * to be no greater than the corresponding constant symbolic expressions
7173 __isl_give isl_map
*isl_map_upper_bound_multi_pw_aff(__isl_take isl_map
*map
,
7174 __isl_take isl_multi_pw_aff
*upper
)
7176 return map_bound_multi_pw_aff(map
, upper
, &map_upper_bound_pw_aff
);
7179 /* Bound the given variable of "bset" from below (or above is "upper"
7180 * is set) to "value".
7182 static __isl_give isl_basic_set
*isl_basic_set_bound(
7183 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned pos
,
7184 isl_int value
, int upper
)
7186 return bset_from_bmap(basic_map_bound(bset_to_bmap(bset
),
7187 type
, pos
, value
, upper
));
7190 /* Bound the given variable of "bset" from below (or above is "upper"
7191 * is set) to "value".
7193 static __isl_give isl_basic_set
*isl_basic_set_bound_val(
7194 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned pos
,
7195 __isl_take isl_val
*value
, int upper
)
7199 if (!isl_val_is_int(value
))
7200 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
7201 "expecting integer value", goto error
);
7202 bset
= isl_basic_set_bound(bset
, type
, pos
, value
->n
, upper
);
7203 isl_val_free(value
);
7206 isl_val_free(value
);
7207 isl_basic_set_free(bset
);
7211 /* Bound the given variable of "bset" from below to "value".
7213 __isl_give isl_basic_set
*isl_basic_set_lower_bound_val(
7214 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned pos
,
7215 __isl_take isl_val
*value
)
7217 return isl_basic_set_bound_val(bset
, type
, pos
, value
, 0);
7220 /* Bound the given variable of "bset" from above to "value".
7222 __isl_give isl_basic_set
*isl_basic_set_upper_bound_val(
7223 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned pos
,
7224 __isl_take isl_val
*value
)
7226 return isl_basic_set_bound_val(bset
, type
, pos
, value
, 1);
7229 __isl_give isl_map
*isl_map_reverse(__isl_take isl_map
*map
)
7231 return isl_map_transform(map
, &isl_space_reverse
,
7232 &isl_basic_map_reverse
);
7235 /* Given a map (A -> B) -> C, return the corresponding map (B -> A) -> C.
7237 __isl_give isl_map
*isl_map_domain_reverse(__isl_take isl_map
*map
)
7239 return isl_map_transform(map
, &isl_space_domain_reverse
,
7240 &isl_basic_map_domain_reverse
);
7243 /* Given a map A -> (B -> C), return the corresponding map A -> (C -> B).
7245 __isl_give isl_map
*isl_map_range_reverse(__isl_take isl_map
*map
)
7247 return isl_map_transform(map
, &isl_space_range_reverse
,
7248 &isl_basic_map_range_reverse
);
7251 /* Given a set (A -> B), return the corresponding set (B -> A).
7253 __isl_give isl_set
*isl_set_wrapped_reverse(__isl_take isl_set
*set
)
7255 isl_map
*map
= set_to_map(set
);
7257 map
= isl_map_transform(map
, &isl_space_wrapped_reverse
,
7258 &isl_basic_map_set_reverse
);
7259 return set_from_map(map
);
7263 #define TYPE isl_pw_multi_aff
7265 #define SUFFIX _pw_multi_aff
7267 #define EMPTY isl_pw_multi_aff_empty
7269 #define ADD isl_pw_multi_aff_union_add
7270 #include "isl_map_lexopt_templ.c"
7272 /* Given a map "map", compute the lexicographically minimal
7273 * (or maximal) image element for each domain element in dom,
7274 * in the form of an isl_pw_multi_aff.
7275 * If "empty" is not NULL, then set *empty to those elements in dom that
7276 * do not have an image element.
7277 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
7278 * should be computed over the domain of "map". "empty" is also NULL
7281 * We first compute the lexicographically minimal or maximal element
7282 * in the first basic map. This results in a partial solution "res"
7283 * and a subset "todo" of dom that still need to be handled.
7284 * We then consider each of the remaining maps in "map" and successively
7285 * update both "res" and "todo".
7286 * If "empty" is NULL, then the todo sets are not needed and therefore
7287 * also not computed.
7289 static __isl_give isl_pw_multi_aff
*isl_map_partial_lexopt_aligned_pw_multi_aff(
7290 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
7291 __isl_give isl_set
**empty
, unsigned flags
)
7295 isl_pw_multi_aff
*res
;
7298 full
= ISL_FL_ISSET(flags
, ISL_OPT_FULL
);
7299 if (!map
|| (!full
&& !dom
))
7302 if (isl_map_plain_is_empty(map
)) {
7307 return isl_pw_multi_aff_from_map(map
);
7310 res
= basic_map_partial_lexopt_pw_multi_aff(
7311 isl_basic_map_copy(map
->p
[0]),
7312 isl_set_copy(dom
), empty
, flags
);
7316 for (i
= 1; i
< map
->n
; ++i
) {
7317 isl_pw_multi_aff
*res_i
;
7319 res_i
= basic_map_partial_lexopt_pw_multi_aff(
7320 isl_basic_map_copy(map
->p
[i
]),
7321 isl_set_copy(dom
), empty
, flags
);
7323 if (ISL_FL_ISSET(flags
, ISL_OPT_MAX
))
7324 res
= isl_pw_multi_aff_union_lexmax(res
, res_i
);
7326 res
= isl_pw_multi_aff_union_lexmin(res
, res_i
);
7329 todo
= isl_set_intersect(todo
, *empty
);
7348 #define TYPE isl_map
7352 #define EMPTY isl_map_empty
7354 #define ADD isl_map_union_disjoint
7355 #include "isl_map_lexopt_templ.c"
7357 /* Given a map "map", compute the lexicographically minimal
7358 * (or maximal) image element for each domain element in "dom",
7359 * in the form of an isl_map.
7360 * If "empty" is not NULL, then set *empty to those elements in "dom" that
7361 * do not have an image element.
7362 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
7363 * should be computed over the domain of "map". "empty" is also NULL
7366 * If the input consists of more than one disjunct, then first
7367 * compute the desired result in the form of an isl_pw_multi_aff and
7368 * then convert that into an isl_map.
7370 * This function used to have an explicit implementation in terms
7371 * of isl_maps, but it would continually intersect the domains of
7372 * partial results with the complement of the domain of the next
7373 * partial solution, potentially leading to an explosion in the number
7374 * of disjuncts if there are several disjuncts in the input.
7375 * An even earlier implementation of this function would look for
7376 * better results in the domain of the partial result and for extra
7377 * results in the complement of this domain, which would lead to
7378 * even more splintering.
7380 static __isl_give isl_map
*isl_map_partial_lexopt_aligned(
7381 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
7382 __isl_give isl_set
**empty
, unsigned flags
)
7385 struct isl_map
*res
;
7386 isl_pw_multi_aff
*pma
;
7388 full
= ISL_FL_ISSET(flags
, ISL_OPT_FULL
);
7389 if (!map
|| (!full
&& !dom
))
7392 if (isl_map_plain_is_empty(map
)) {
7401 res
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[0]),
7407 pma
= isl_map_partial_lexopt_aligned_pw_multi_aff(map
, dom
, empty
,
7409 return isl_map_from_pw_multi_aff_internal(pma
);
7418 __isl_give isl_map
*isl_map_partial_lexmax(
7419 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
7420 __isl_give isl_set
**empty
)
7422 return isl_map_partial_lexopt(map
, dom
, empty
, ISL_OPT_MAX
);
7425 __isl_give isl_map
*isl_map_partial_lexmin(
7426 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
7427 __isl_give isl_set
**empty
)
7429 return isl_map_partial_lexopt(map
, dom
, empty
, 0);
7432 __isl_give isl_set
*isl_set_partial_lexmin(
7433 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
7434 __isl_give isl_set
**empty
)
7436 return set_from_map(isl_map_partial_lexmin(set_to_map(set
),
7440 __isl_give isl_set
*isl_set_partial_lexmax(
7441 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
7442 __isl_give isl_set
**empty
)
7444 return set_from_map(isl_map_partial_lexmax(set_to_map(set
),
7448 /* Compute the lexicographic minimum (or maximum if "flags" includes
7449 * ISL_OPT_MAX) of "bset" over its parametric domain.
7451 __isl_give isl_set
*isl_basic_set_lexopt(__isl_take isl_basic_set
*bset
,
7454 return isl_basic_map_lexopt(bset
, flags
);
7457 __isl_give isl_map
*isl_basic_map_lexmax(__isl_take isl_basic_map
*bmap
)
7459 return isl_basic_map_lexopt(bmap
, ISL_OPT_MAX
);
7462 __isl_give isl_set
*isl_basic_set_lexmin(__isl_take isl_basic_set
*bset
)
7464 return set_from_map(isl_basic_map_lexmin(bset_to_bmap(bset
)));
7467 __isl_give isl_set
*isl_basic_set_lexmax(__isl_take isl_basic_set
*bset
)
7469 return set_from_map(isl_basic_map_lexmax(bset_to_bmap(bset
)));
7472 /* Compute the lexicographic minimum of "bset" over its parametric domain
7473 * for the purpose of quantifier elimination.
7474 * That is, find an explicit representation for all the existentially
7475 * quantified variables in "bset" by computing their lexicographic
7478 static __isl_give isl_set
*isl_basic_set_lexmin_compute_divs(
7479 __isl_take isl_basic_set
*bset
)
7481 return isl_basic_set_lexopt(bset
, ISL_OPT_QE
);
7484 /* Given a basic map with one output dimension, compute the minimum or
7485 * maximum of that dimension as an isl_pw_aff.
7487 * Compute the optimum as a lexicographic optimum over the single
7488 * output dimension and extract the single isl_pw_aff from the result.
7490 static __isl_give isl_pw_aff
*basic_map_dim_opt(__isl_keep isl_basic_map
*bmap
,
7493 isl_pw_multi_aff
*pma
;
7496 bmap
= isl_basic_map_copy(bmap
);
7497 pma
= isl_basic_map_lexopt_pw_multi_aff(bmap
, max
? ISL_OPT_MAX
: 0);
7498 pwaff
= isl_pw_multi_aff_get_pw_aff(pma
, 0);
7499 isl_pw_multi_aff_free(pma
);
7504 /* Compute the minimum or maximum of the given output dimension
7505 * as a function of the parameters and the input dimensions,
7506 * but independently of the other output dimensions.
7508 * We first project out the other output dimension and then compute
7509 * the "lexicographic" maximum in each basic map, combining the results
7510 * using isl_pw_aff_union_max.
7512 static __isl_give isl_pw_aff
*map_dim_opt(__isl_take isl_map
*map
, int pos
,
7519 n_out
= isl_map_dim(map
, isl_dim_out
);
7521 map
= isl_map_free(map
);
7522 map
= isl_map_project_out(map
, isl_dim_out
, pos
+ 1, n_out
- (pos
+ 1));
7523 map
= isl_map_project_out(map
, isl_dim_out
, 0, pos
);
7528 isl_space
*space
= isl_map_get_space(map
);
7530 return isl_pw_aff_empty(space
);
7533 pwaff
= basic_map_dim_opt(map
->p
[0], max
);
7534 for (i
= 1; i
< map
->n
; ++i
) {
7535 isl_pw_aff
*pwaff_i
;
7537 pwaff_i
= basic_map_dim_opt(map
->p
[i
], max
);
7538 pwaff
= isl_pw_aff_union_opt(pwaff
, pwaff_i
, max
);
7546 /* Compute the minimum of the given output dimension as a function of the
7547 * parameters and input dimensions, but independently of
7548 * the other output dimensions.
7550 __isl_give isl_pw_aff
*isl_map_dim_min(__isl_take isl_map
*map
, int pos
)
7552 return map_dim_opt(map
, pos
, 0);
7555 /* Compute the maximum of the given output dimension as a function of the
7556 * parameters and input dimensions, but independently of
7557 * the other output dimensions.
7559 __isl_give isl_pw_aff
*isl_map_dim_max(__isl_take isl_map
*map
, int pos
)
7561 return map_dim_opt(map
, pos
, 1);
7564 /* Compute the minimum or maximum of the given set dimension
7565 * as a function of the parameters,
7566 * but independently of the other set dimensions.
7568 static __isl_give isl_pw_aff
*set_dim_opt(__isl_take isl_set
*set
, int pos
,
7571 return map_dim_opt(set
, pos
, max
);
7574 /* Compute the maximum of the given set dimension as a function of the
7575 * parameters, but independently of the other set dimensions.
7577 __isl_give isl_pw_aff
*isl_set_dim_max(__isl_take isl_set
*set
, int pos
)
7579 return set_dim_opt(set
, pos
, 1);
7582 /* Compute the minimum of the given set dimension as a function of the
7583 * parameters, but independently of the other set dimensions.
7585 __isl_give isl_pw_aff
*isl_set_dim_min(__isl_take isl_set
*set
, int pos
)
7587 return set_dim_opt(set
, pos
, 0);
7590 /* Apply a preimage specified by "mat" on the parameters of "bset".
7591 * bset is assumed to have only parameters and divs.
7593 static __isl_give isl_basic_set
*basic_set_parameter_preimage(
7594 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*mat
)
7598 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
7599 if (nparam
< 0 || !mat
)
7602 bset
->dim
= isl_space_cow(bset
->dim
);
7606 isl_assert(bset
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
7608 bset
->dim
->nparam
= 0;
7609 bset
->dim
->n_out
= nparam
;
7610 bset
= isl_basic_set_preimage(bset
, mat
);
7612 bset
->dim
->nparam
= bset
->dim
->n_out
;
7613 bset
->dim
->n_out
= 0;
7618 isl_basic_set_free(bset
);
7622 /* Apply a preimage specified by "mat" on the parameters of "set".
7623 * set is assumed to have only parameters and divs.
7625 static __isl_give isl_set
*set_parameter_preimage(__isl_take isl_set
*set
,
7626 __isl_take isl_mat
*mat
)
7631 nparam
= isl_set_dim(set
, isl_dim_param
);
7632 if (nparam
< 0 || !mat
)
7635 if (mat
->n_row
!= 1 + nparam
)
7636 isl_die(isl_set_get_ctx(set
), isl_error_internal
,
7637 "unexpected number of rows", goto error
);
7639 space
= isl_set_get_space(set
);
7640 space
= isl_space_move_dims(space
, isl_dim_set
, 0,
7641 isl_dim_param
, 0, nparam
);
7642 set
= isl_set_reset_space(set
, space
);
7643 set
= isl_set_preimage(set
, mat
);
7644 nparam
= isl_set_dim(set
, isl_dim_out
);
7646 set
= isl_set_free(set
);
7647 space
= isl_set_get_space(set
);
7648 space
= isl_space_move_dims(space
, isl_dim_param
, 0,
7649 isl_dim_out
, 0, nparam
);
7650 set
= isl_set_reset_space(set
, space
);
7658 /* Intersect the basic set "bset" with the affine space specified by the
7659 * equalities in "eq".
7661 static __isl_give isl_basic_set
*basic_set_append_equalities(
7662 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*eq
)
7670 bset
= isl_basic_set_extend(bset
, 0, eq
->n_row
, 0);
7674 len
= isl_basic_set_offset(bset
, isl_dim_div
) + bset
->extra
;
7675 for (i
= 0; i
< eq
->n_row
; ++i
) {
7676 k
= isl_basic_set_alloc_equality(bset
);
7679 isl_seq_cpy(bset
->eq
[k
], eq
->row
[i
], eq
->n_col
);
7680 isl_seq_clr(bset
->eq
[k
] + eq
->n_col
, len
- eq
->n_col
);
7684 bset
= isl_basic_set_gauss(bset
, NULL
);
7685 bset
= isl_basic_set_finalize(bset
);
7690 isl_basic_set_free(bset
);
7694 /* Intersect the set "set" with the affine space specified by the
7695 * equalities in "eq".
7697 static __isl_give isl_set
*set_append_equalities(__isl_take isl_set
*set
,
7698 __isl_take isl_mat
*eq
)
7705 for (i
= 0; i
< set
->n
; ++i
) {
7706 set
->p
[i
] = basic_set_append_equalities(set
->p
[i
],
7719 /* Given a basic set "bset" that only involves parameters and existentially
7720 * quantified variables, return the index of the first equality
7721 * that only involves parameters. If there is no such equality then
7722 * return bset->n_eq.
7724 * This function assumes that isl_basic_set_gauss has been called on "bset".
7726 static int first_parameter_equality(__isl_keep isl_basic_set
*bset
)
7729 isl_size nparam
, n_div
;
7731 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
7732 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
7733 if (nparam
< 0 || n_div
< 0)
7736 for (i
= 0, j
= n_div
- 1; i
< bset
->n_eq
&& j
>= 0; --j
) {
7737 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ j
]))
7744 /* Compute an explicit representation for the existentially quantified
7745 * variables in "bset" by computing the "minimal value" of the set
7746 * variables. Since there are no set variables, the computation of
7747 * the minimal value essentially computes an explicit representation
7748 * of the non-empty part(s) of "bset".
7750 * The input only involves parameters and existentially quantified variables.
7751 * All equalities among parameters have been removed.
7753 * Since the existentially quantified variables in the result are in general
7754 * going to be different from those in the input, we first replace
7755 * them by the minimal number of variables based on their equalities.
7756 * This should simplify the parametric integer programming.
7758 static __isl_give isl_set
*base_compute_divs(__isl_take isl_basic_set
*bset
)
7760 isl_morph
*morph1
, *morph2
;
7766 if (bset
->n_eq
== 0)
7767 return isl_basic_set_lexmin_compute_divs(bset
);
7769 morph1
= isl_basic_set_parameter_compression(bset
);
7770 bset
= isl_morph_basic_set(isl_morph_copy(morph1
), bset
);
7771 bset
= isl_basic_set_lift(bset
);
7772 morph2
= isl_basic_set_variable_compression(bset
, isl_dim_set
);
7773 bset
= isl_morph_basic_set(morph2
, bset
);
7774 n
= isl_basic_set_dim(bset
, isl_dim_set
);
7776 bset
= isl_basic_set_free(bset
);
7777 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
7779 set
= isl_basic_set_lexmin_compute_divs(bset
);
7781 set
= isl_morph_set(isl_morph_inverse(morph1
), set
);
7786 /* Project the given basic set onto its parameter domain, possibly introducing
7787 * new, explicit, existential variables in the constraints.
7788 * The input has parameters and (possibly implicit) existential variables.
7789 * The output has the same parameters, but only
7790 * explicit existentially quantified variables.
7792 * The actual projection is performed by pip, but pip doesn't seem
7793 * to like equalities very much, so we first remove the equalities
7794 * among the parameters by performing a variable compression on
7795 * the parameters. Afterward, an inverse transformation is performed
7796 * and the equalities among the parameters are inserted back in.
7798 * The variable compression on the parameters may uncover additional
7799 * equalities that were only implicit before. We therefore check
7800 * if there are any new parameter equalities in the result and
7801 * if so recurse. The removal of parameter equalities is required
7802 * for the parameter compression performed by base_compute_divs.
7804 static __isl_give isl_set
*parameter_compute_divs(
7805 __isl_take isl_basic_set
*bset
)
7809 struct isl_mat
*T
, *T2
;
7810 struct isl_set
*set
;
7813 bset
= isl_basic_set_cow(bset
);
7817 if (bset
->n_eq
== 0)
7818 return base_compute_divs(bset
);
7820 bset
= isl_basic_set_gauss(bset
, NULL
);
7823 if (isl_basic_set_plain_is_empty(bset
))
7824 return isl_set_from_basic_set(bset
);
7826 i
= first_parameter_equality(bset
);
7827 if (i
== bset
->n_eq
)
7828 return base_compute_divs(bset
);
7830 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
7832 return isl_set_from_basic_set(isl_basic_set_free(bset
));
7833 eq
= isl_mat_sub_alloc6(bset
->ctx
, bset
->eq
, i
, bset
->n_eq
- i
,
7835 eq
= isl_mat_cow(eq
);
7836 T
= isl_mat_variable_compression(isl_mat_copy(eq
), &T2
);
7837 if (T
&& T
->n_col
== 0) {
7841 bset
= isl_basic_set_set_to_empty(bset
);
7842 return isl_set_from_basic_set(bset
);
7844 bset
= basic_set_parameter_preimage(bset
, T
);
7846 i
= first_parameter_equality(bset
);
7849 else if (i
== bset
->n_eq
)
7850 set
= base_compute_divs(bset
);
7852 set
= parameter_compute_divs(bset
);
7853 set
= set_parameter_preimage(set
, T2
);
7854 set
= set_append_equalities(set
, eq
);
7858 /* Insert the divs from "ls" before those of "bmap".
7860 * The number of columns is not changed, which means that the last
7861 * dimensions of "bmap" are being reintepreted as the divs from "ls".
7862 * The caller is responsible for removing the same number of dimensions
7863 * from the space of "bmap".
7865 static __isl_give isl_basic_map
*insert_divs_from_local_space(
7866 __isl_take isl_basic_map
*bmap
, __isl_keep isl_local_space
*ls
)
7872 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
7874 return isl_basic_map_free(bmap
);
7878 old_n_div
= bmap
->n_div
;
7879 bmap
= insert_div_rows(bmap
, n_div
);
7883 for (i
= 0; i
< n_div
; ++i
) {
7884 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], ls
->div
->n_col
);
7885 isl_seq_clr(bmap
->div
[i
] + ls
->div
->n_col
, old_n_div
);
7891 /* Replace the space of "bmap" by the space and divs of "ls".
7893 * If "ls" has any divs, then we simplify the result since we may
7894 * have discovered some additional equalities that could simplify
7895 * the div expressions.
7897 static __isl_give isl_basic_map
*basic_replace_space_by_local_space(
7898 __isl_take isl_basic_map
*bmap
, __isl_take isl_local_space
*ls
)
7902 bmap
= isl_basic_map_cow(bmap
);
7903 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
7904 if (!bmap
|| n_div
< 0)
7907 bmap
= insert_divs_from_local_space(bmap
, ls
);
7911 isl_space_free(bmap
->dim
);
7912 bmap
->dim
= isl_local_space_get_space(ls
);
7916 isl_local_space_free(ls
);
7918 bmap
= isl_basic_map_simplify(bmap
);
7919 bmap
= isl_basic_map_finalize(bmap
);
7922 isl_basic_map_free(bmap
);
7923 isl_local_space_free(ls
);
7927 /* Replace the space of "map" by the space and divs of "ls".
7929 static __isl_give isl_map
*replace_space_by_local_space(__isl_take isl_map
*map
,
7930 __isl_take isl_local_space
*ls
)
7934 map
= isl_map_cow(map
);
7938 for (i
= 0; i
< map
->n
; ++i
) {
7939 map
->p
[i
] = basic_replace_space_by_local_space(map
->p
[i
],
7940 isl_local_space_copy(ls
));
7944 isl_space_free(isl_map_take_space(map
));
7945 map
= isl_map_restore_space(map
, isl_local_space_get_space(ls
));
7947 isl_local_space_free(ls
);
7950 isl_local_space_free(ls
);
7955 /* Compute an explicit representation for the existentially
7956 * quantified variables for which do not know any explicit representation yet.
7958 * We first sort the existentially quantified variables so that the
7959 * existentially quantified variables for which we already have an explicit
7960 * representation are placed before those for which we do not.
7961 * The input dimensions, the output dimensions and the existentially
7962 * quantified variables for which we already have an explicit
7963 * representation are then turned into parameters.
7964 * compute_divs returns a map with the same parameters and
7965 * no input or output dimensions and the dimension specification
7966 * is reset to that of the input, including the existentially quantified
7967 * variables for which we already had an explicit representation.
7969 static __isl_give isl_map
*compute_divs(__isl_take isl_basic_map
*bmap
)
7971 struct isl_basic_set
*bset
;
7972 struct isl_set
*set
;
7973 struct isl_map
*map
;
7975 isl_local_space
*ls
;
7982 bmap
= isl_basic_map_sort_divs(bmap
);
7983 bmap
= isl_basic_map_cow(bmap
);
7987 n_known
= isl_basic_map_first_unknown_div(bmap
);
7988 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
7989 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
7990 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
7991 if (n_known
< 0 || nparam
< 0 || n_in
< 0 || n_out
< 0)
7992 return isl_map_from_basic_map(isl_basic_map_free(bmap
));
7994 space
= isl_space_set_alloc(bmap
->ctx
,
7995 nparam
+ n_in
+ n_out
+ n_known
, 0);
7999 ls
= isl_basic_map_get_local_space(bmap
);
8000 ls
= isl_local_space_drop_dims(ls
, isl_dim_div
,
8001 n_known
, bmap
->n_div
- n_known
);
8003 for (i
= n_known
; i
< bmap
->n_div
; ++i
)
8004 swap_div(bmap
, i
- n_known
, i
);
8005 bmap
->n_div
-= n_known
;
8006 bmap
->extra
-= n_known
;
8008 bmap
= isl_basic_map_reset_space(bmap
, space
);
8009 bset
= bset_from_bmap(bmap
);
8011 set
= parameter_compute_divs(bset
);
8012 map
= set_to_map(set
);
8013 map
= replace_space_by_local_space(map
, ls
);
8017 isl_basic_map_free(bmap
);
8021 /* Remove the explicit representation of local variable "div",
8024 __isl_give isl_basic_map
*isl_basic_map_mark_div_unknown(
8025 __isl_take isl_basic_map
*bmap
, int div
)
8029 unknown
= isl_basic_map_div_is_marked_unknown(bmap
, div
);
8031 return isl_basic_map_free(bmap
);
8035 bmap
= isl_basic_map_cow(bmap
);
8038 isl_int_set_si(bmap
->div
[div
][0], 0);
8042 /* Is local variable "div" of "bmap" marked as not having an explicit
8044 * Note that even if "div" is not marked in this way and therefore
8045 * has an explicit representation, this representation may still
8046 * depend (indirectly) on other local variables that do not
8047 * have an explicit representation.
8049 isl_bool
isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map
*bmap
,
8052 if (isl_basic_map_check_range(bmap
, isl_dim_div
, div
, 1) < 0)
8053 return isl_bool_error
;
8054 return isl_int_is_zero(bmap
->div
[div
][0]);
8057 /* Return the position of the first local variable that does not
8058 * have an explicit representation.
8059 * Return the total number of local variables if they all have
8060 * an explicit representation.
8061 * Return -1 on error.
8063 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map
*bmap
)
8070 for (i
= 0; i
< bmap
->n_div
; ++i
) {
8071 if (!isl_basic_map_div_is_known(bmap
, i
))
8077 /* Return the position of the first local variable that does not
8078 * have an explicit representation.
8079 * Return the total number of local variables if they all have
8080 * an explicit representation.
8081 * Return -1 on error.
8083 int isl_basic_set_first_unknown_div(__isl_keep isl_basic_set
*bset
)
8085 return isl_basic_map_first_unknown_div(bset
);
8088 /* Does "bmap" have an explicit representation for all local variables?
8090 isl_bool
isl_basic_map_divs_known(__isl_keep isl_basic_map
*bmap
)
8095 n
= isl_basic_map_dim(bmap
, isl_dim_div
);
8096 first
= isl_basic_map_first_unknown_div(bmap
);
8097 if (n
< 0 || first
< 0)
8098 return isl_bool_error
;
8102 /* Do all basic maps in "map" have an explicit representation
8103 * for all local variables?
8105 isl_bool
isl_map_divs_known(__isl_keep isl_map
*map
)
8110 return isl_bool_error
;
8112 for (i
= 0; i
< map
->n
; ++i
) {
8113 int known
= isl_basic_map_divs_known(map
->p
[i
]);
8118 return isl_bool_true
;
8121 /* If bmap contains any unknown divs, then compute explicit
8122 * expressions for them. However, this computation may be
8123 * quite expensive, so first try to remove divs that aren't
8126 __isl_give isl_map
*isl_basic_map_compute_divs(__isl_take isl_basic_map
*bmap
)
8129 struct isl_map
*map
;
8131 known
= isl_basic_map_divs_known(bmap
);
8135 return isl_map_from_basic_map(bmap
);
8137 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
8139 known
= isl_basic_map_divs_known(bmap
);
8143 return isl_map_from_basic_map(bmap
);
8145 map
= compute_divs(bmap
);
8148 isl_basic_map_free(bmap
);
8152 __isl_give isl_map
*isl_map_compute_divs(__isl_take isl_map
*map
)
8156 struct isl_map
*res
;
8163 known
= isl_map_divs_known(map
);
8171 res
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[0]));
8172 for (i
= 1 ; i
< map
->n
; ++i
) {
8174 r2
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[i
]));
8175 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
8176 res
= isl_map_union_disjoint(res
, r2
);
8178 res
= isl_map_union(res
, r2
);
8185 __isl_give isl_set
*isl_basic_set_compute_divs(__isl_take isl_basic_set
*bset
)
8187 return set_from_map(isl_basic_map_compute_divs(bset_to_bmap(bset
)));
8190 __isl_give isl_set
*isl_set_compute_divs(__isl_take isl_set
*set
)
8192 return set_from_map(isl_map_compute_divs(set_to_map(set
)));
8195 __isl_give isl_set
*isl_map_domain(__isl_take isl_map
*map
)
8200 n_out
= isl_map_dim(map
, isl_dim_out
);
8202 return set_from_map(isl_map_free(map
));
8203 space
= isl_space_domain(isl_map_get_space(map
));
8205 map
= isl_map_project_out(map
, isl_dim_out
, 0, n_out
);
8207 return set_from_map(isl_map_reset_space(map
, space
));
8210 /* Return the union of "map1" and "map2", where we assume for now that
8211 * "map1" and "map2" are disjoint. Note that the basic maps inside
8212 * "map1" or "map2" may not be disjoint from each other.
8213 * Also note that this function is also called from isl_map_union,
8214 * which takes care of handling the situation where "map1" and "map2"
8215 * may not be disjoint.
8217 * If one of the inputs is empty, we can simply return the other input.
8218 * Similarly, if one of the inputs is universal, then it is equal to the union.
8220 static __isl_give isl_map
*map_union_disjoint(__isl_take isl_map
*map1
,
8221 __isl_take isl_map
*map2
)
8225 struct isl_map
*map
= NULL
;
8228 if (isl_map_check_equal_space(map1
, map2
) < 0)
8240 is_universe
= isl_map_plain_is_universe(map1
);
8241 if (is_universe
< 0)
8248 is_universe
= isl_map_plain_is_universe(map2
);
8249 if (is_universe
< 0)
8256 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
8257 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
8258 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
8260 map
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
8261 map1
->n
+ map2
->n
, flags
);
8264 for (i
= 0; i
< map1
->n
; ++i
) {
8265 map
= isl_map_add_basic_map(map
,
8266 isl_basic_map_copy(map1
->p
[i
]));
8270 for (i
= 0; i
< map2
->n
; ++i
) {
8271 map
= isl_map_add_basic_map(map
,
8272 isl_basic_map_copy(map2
->p
[i
]));
8286 /* Return the union of "map1" and "map2", where "map1" and "map2" are
8287 * guaranteed to be disjoint by the caller.
8289 * Note that this functions is called from within isl_map_make_disjoint,
8290 * so we have to be careful not to touch the constraints of the inputs
8293 __isl_give isl_map
*isl_map_union_disjoint(__isl_take isl_map
*map1
,
8294 __isl_take isl_map
*map2
)
8296 isl_map_align_params_bin(&map1
, &map2
);
8297 return map_union_disjoint(map1
, map2
);
8300 /* Return the union of "map1" and "map2", where "map1" and "map2" may
8303 * We currently simply call map_union_disjoint, the internal operation
8304 * of which does not really depend on the inputs being disjoint.
8305 * If the result contains more than one basic map, then we clear
8306 * the disjoint flag since the result may contain basic maps from
8307 * both inputs and these are not guaranteed to be disjoint.
8309 * As a special case, if "map1" and "map2" are obviously equal,
8310 * then we simply return "map1".
8312 __isl_give isl_map
*isl_map_union(__isl_take isl_map
*map1
,
8313 __isl_take isl_map
*map2
)
8317 if (isl_map_align_params_bin(&map1
, &map2
) < 0)
8320 equal
= isl_map_plain_is_equal(map1
, map2
);
8328 map1
= map_union_disjoint(map1
, map2
);
8332 ISL_F_CLR(map1
, ISL_MAP_DISJOINT
);
8340 __isl_give isl_set
*isl_set_union_disjoint(
8341 __isl_take isl_set
*set1
, __isl_take isl_set
*set2
)
8343 return set_from_map(isl_map_union_disjoint(set_to_map(set1
),
8347 __isl_give isl_set
*isl_set_union(__isl_take isl_set
*set1
,
8348 __isl_take isl_set
*set2
)
8350 return set_from_map(isl_map_union(set_to_map(set1
), set_to_map(set2
)));
8353 /* Apply "fn" to pairs of elements from "map" and "set" and collect
8354 * the results in a map living in "space".
8356 * "map" and "set" are assumed to be compatible and non-NULL.
8358 static __isl_give isl_map
*map_intersect_set(__isl_take isl_map
*map
,
8359 __isl_take isl_space
*space
, __isl_take isl_set
*set
,
8360 __isl_give isl_basic_map
*fn(__isl_take isl_basic_map
*bmap
,
8361 __isl_take isl_basic_set
*bset
))
8364 struct isl_map
*result
;
8367 if (isl_set_plain_is_universe(set
)) {
8369 return isl_map_reset_equal_dim_space(map
, space
);
8372 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
) &&
8373 ISL_F_ISSET(set
, ISL_MAP_DISJOINT
))
8374 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
8376 result
= isl_map_alloc_space(space
, map
->n
* set
->n
, flags
);
8377 for (i
= 0; result
&& i
< map
->n
; ++i
)
8378 for (j
= 0; j
< set
->n
; ++j
) {
8379 result
= isl_map_add_basic_map(result
,
8380 fn(isl_basic_map_copy(map
->p
[i
]),
8381 isl_basic_set_copy(set
->p
[j
])));
8391 __isl_give isl_map
*isl_map_intersect_range(__isl_take isl_map
*map
,
8392 __isl_take isl_set
*set
)
8397 isl_map_align_params_set(&map
, &set
);
8398 ok
= isl_map_compatible_range(map
, set
);
8402 isl_die(set
->ctx
, isl_error_invalid
,
8403 "incompatible spaces", goto error
);
8405 space
= isl_map_get_space(map
);
8406 return map_intersect_set(map
, space
, set
,
8407 &isl_basic_map_intersect_range
);
8414 /* Intersect the domain of "map" with "set".
8416 * If the domain dimensions of "map" do not have any identifiers,
8417 * then copy them over from "set".
8419 __isl_give isl_map
*isl_map_intersect_domain(__isl_take isl_map
*map
,
8420 __isl_take isl_set
*set
)
8425 isl_map_align_params_set(&map
, &set
);
8426 ok
= isl_map_compatible_domain(map
, set
);
8430 isl_die(set
->ctx
, isl_error_invalid
,
8431 "incompatible spaces", goto error
);
8433 space
= isl_map_get_space(map
);
8434 space
= isl_space_copy_ids_if_unset(space
, isl_dim_in
,
8435 isl_set_peek_space(set
), isl_dim_set
);
8436 return map_intersect_set(map
, space
, set
,
8437 &isl_basic_map_intersect_domain
);
8445 #define TYPE isl_map
8447 #include "isl_copy_tuple_id_templ.c"
8449 /* Data structure that specifies how isl_map_intersect_factor
8452 * "preserve_type" is the tuple where the factor differs from
8453 * the input map and of which the identifiers needs
8454 * to be preserved explicitly.
8455 * "other_factor" is used to extract the space of the other factor
8456 * from the space of the product ("map").
8457 * "product" is used to combine the given factor and a universe map
8458 * in the space returned by "other_factor" to produce a map
8459 * that lives in the same space as the input map.
8461 struct isl_intersect_factor_control
{
8462 enum isl_dim_type preserve_type
;
8463 __isl_give isl_space
*(*other_factor
)(__isl_take isl_space
*space
);
8464 __isl_give isl_map
*(*product
)(__isl_take isl_map
*factor
,
8465 __isl_take isl_map
*other
);
8468 /* Given a map "map" in some product space and a map "factor"
8469 * living in some factor space, return the intersection.
8471 * After aligning the parameters,
8472 * the map "factor" is first extended to a map living in the same space
8473 * as "map" and then a regular intersection is computed.
8475 * Note that the extension is computed as a product, which is anonymous
8476 * by default. If "map" has an identifier on the corresponding tuple,
8477 * then this identifier needs to be set on the product
8478 * before the intersection is computed.
8480 static __isl_give isl_map
*isl_map_intersect_factor(
8481 __isl_take isl_map
*map
, __isl_take isl_map
*factor
,
8482 struct isl_intersect_factor_control
*control
)
8486 isl_map
*other
, *product
;
8488 equal
= isl_map_has_equal_params(map
, factor
);
8492 map
= isl_map_align_params(map
, isl_map_get_space(factor
));
8493 factor
= isl_map_align_params(factor
, isl_map_get_space(map
));
8496 space
= isl_map_get_space(map
);
8497 other
= isl_map_universe(control
->other_factor(space
));
8498 product
= control
->product(factor
, other
);
8500 space
= isl_map_peek_space(map
);
8501 product
= isl_map_copy_tuple_id(product
, control
->preserve_type
,
8502 space
, control
->preserve_type
);
8503 return map_intersect(map
, product
);
8506 isl_map_free(factor
);
8510 /* Return the domain product of "map2" and "map1".
8512 static __isl_give isl_map
*isl_map_reverse_domain_product(
8513 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
8515 return isl_map_domain_product(map2
, map1
);
8518 /* Return the range product of "map2" and "map1".
8520 static __isl_give isl_map
*isl_map_reverse_range_product(
8521 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
8523 return isl_map_range_product(map2
, map1
);
8526 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
8527 * in the space A -> C, return the intersection.
8529 __isl_give isl_map
*isl_map_intersect_domain_factor_domain(
8530 __isl_take isl_map
*map
, __isl_take isl_map
*factor
)
8532 struct isl_intersect_factor_control control
= {
8533 .preserve_type
= isl_dim_in
,
8534 .other_factor
= isl_space_domain_factor_range
,
8535 .product
= isl_map_domain_product
,
8538 return isl_map_intersect_factor(map
, factor
, &control
);
8541 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
8542 * in the space B -> C, return the intersection.
8544 __isl_give isl_map
*isl_map_intersect_domain_factor_range(
8545 __isl_take isl_map
*map
, __isl_take isl_map
*factor
)
8547 struct isl_intersect_factor_control control
= {
8548 .preserve_type
= isl_dim_in
,
8549 .other_factor
= isl_space_domain_factor_domain
,
8550 .product
= isl_map_reverse_domain_product
,
8553 return isl_map_intersect_factor(map
, factor
, &control
);
8556 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
8557 * in the space A -> B, return the intersection.
8559 __isl_give isl_map
*isl_map_intersect_range_factor_domain(
8560 __isl_take isl_map
*map
, __isl_take isl_map
*factor
)
8562 struct isl_intersect_factor_control control
= {
8563 .preserve_type
= isl_dim_out
,
8564 .other_factor
= isl_space_range_factor_range
,
8565 .product
= isl_map_range_product
,
8568 return isl_map_intersect_factor(map
, factor
, &control
);
8571 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
8572 * in the space A -> C, return the intersection.
8574 __isl_give isl_map
*isl_map_intersect_range_factor_range(
8575 __isl_take isl_map
*map
, __isl_take isl_map
*factor
)
8577 struct isl_intersect_factor_control control
= {
8578 .preserve_type
= isl_dim_out
,
8579 .other_factor
= isl_space_range_factor_domain
,
8580 .product
= isl_map_reverse_range_product
,
8583 return isl_map_intersect_factor(map
, factor
, &control
);
8586 /* Given a set "set" in a space [A -> B] and a set "domain"
8587 * in the space A, return the intersection.
8589 * The set "domain" is first extended to a set living in the space
8590 * [A -> B] and then a regular intersection is computed.
8592 __isl_give isl_set
*isl_set_intersect_factor_domain(__isl_take isl_set
*set
,
8593 __isl_take isl_set
*domain
)
8595 struct isl_intersect_factor_control control
= {
8596 .preserve_type
= isl_dim_set
,
8597 .other_factor
= isl_space_factor_range
,
8598 .product
= isl_map_range_product
,
8601 return set_from_map(isl_map_intersect_factor(set_to_map(set
),
8602 set_to_map(domain
), &control
));
8605 /* Given a set "set" in a space [A -> B] and a set "range"
8606 * in the space B, return the intersection.
8608 * The set "range" is first extended to a set living in the space
8609 * [A -> B] and then a regular intersection is computed.
8611 __isl_give isl_set
*isl_set_intersect_factor_range(__isl_take isl_set
*set
,
8612 __isl_take isl_set
*range
)
8614 struct isl_intersect_factor_control control
= {
8615 .preserve_type
= isl_dim_set
,
8616 .other_factor
= isl_space_factor_domain
,
8617 .product
= isl_map_reverse_range_product
,
8620 return set_from_map(isl_map_intersect_factor(set_to_map(set
),
8621 set_to_map(range
), &control
));
8624 /* Given a map "map" in a space [A -> B] -> C and a set "domain"
8625 * in the space A, return the intersection.
8627 * The set "domain" is extended to a set living in the space [A -> B] and
8628 * the domain of "map" is intersected with this set.
8630 __isl_give isl_map
*isl_map_intersect_domain_wrapped_domain(
8631 __isl_take isl_map
*map
, __isl_take isl_set
*domain
)
8636 isl_map_align_params_set(&map
, &domain
);
8637 space
= isl_map_get_space(map
);
8638 space
= isl_space_domain_wrapped_range(space
);
8639 factor
= isl_set_universe(space
);
8640 domain
= isl_set_product(domain
, factor
);
8641 return isl_map_intersect_domain(map
, domain
);
8644 /* Given a map "map" in a space A -> [B -> C] and a set "domain"
8645 * in the space B, return the intersection.
8647 * The set "domain" is extended to a set living in the space [B -> C] and
8648 * the range of "map" is intersected with this set.
8650 __isl_give isl_map
*isl_map_intersect_range_wrapped_domain(
8651 __isl_take isl_map
*map
, __isl_take isl_set
*domain
)
8656 isl_map_align_params_set(&map
, &domain
);
8657 space
= isl_map_get_space(map
);
8658 space
= isl_space_range_wrapped_range(space
);
8659 factor
= isl_set_universe(space
);
8660 domain
= isl_set_product(domain
, factor
);
8661 return isl_map_intersect_range(map
, domain
);
8664 __isl_give isl_map
*isl_map_apply_domain(__isl_take isl_map
*map1
,
8665 __isl_take isl_map
*map2
)
8667 if (isl_map_align_params_bin(&map1
, &map2
) < 0)
8669 map1
= isl_map_reverse(map1
);
8670 map1
= isl_map_apply_range(map1
, map2
);
8671 return isl_map_reverse(map1
);
8678 __isl_give isl_map
*isl_map_apply_range(__isl_take isl_map
*map1
,
8679 __isl_take isl_map
*map2
)
8682 struct isl_map
*result
;
8685 if (isl_map_align_params_bin(&map1
, &map2
) < 0)
8688 space
= isl_space_join(isl_space_copy(map1
->dim
),
8689 isl_space_copy(map2
->dim
));
8691 result
= isl_map_alloc_space(space
, map1
->n
* map2
->n
, 0);
8694 for (i
= 0; i
< map1
->n
; ++i
)
8695 for (j
= 0; j
< map2
->n
; ++j
) {
8696 result
= isl_map_add_basic_map(result
,
8697 isl_basic_map_apply_range(
8698 isl_basic_map_copy(map1
->p
[i
]),
8699 isl_basic_map_copy(map2
->p
[j
])));
8705 if (result
&& result
->n
<= 1)
8706 ISL_F_SET(result
, ISL_MAP_DISJOINT
);
8714 /* Is "bmap" a transformation, i.e.,
8715 * does it relate elements from the same space.
8717 isl_bool
isl_basic_map_is_transformation(__isl_keep isl_basic_map
*bmap
)
8721 space
= isl_basic_map_peek_space(bmap
);
8722 return isl_space_tuple_is_equal(space
, isl_dim_in
, space
, isl_dim_out
);
8725 /* Check that "bmap" is a transformation, i.e.,
8726 * that it relates elements from the same space.
8728 static isl_stat
isl_basic_map_check_transformation(
8729 __isl_keep isl_basic_map
*bmap
)
8733 equal
= isl_basic_map_is_transformation(bmap
);
8735 return isl_stat_error
;
8737 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
8738 "domain and range don't match", return isl_stat_error
);
8743 * returns range - domain
8745 __isl_give isl_basic_set
*isl_basic_map_deltas(__isl_take isl_basic_map
*bmap
)
8747 isl_space
*target_space
;
8748 struct isl_basic_set
*bset
;
8754 if (isl_basic_map_check_transformation(bmap
) < 0)
8755 return isl_basic_map_free(bmap
);
8756 dim
= isl_basic_map_dim(bmap
, isl_dim_in
);
8757 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
8758 if (dim
< 0 || nparam
< 0)
8760 target_space
= isl_space_domain(isl_basic_map_get_space(bmap
));
8761 bmap
= isl_basic_map_from_range(isl_basic_map_wrap(bmap
));
8762 bmap
= isl_basic_map_add_dims(bmap
, isl_dim_in
, dim
);
8763 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
8765 bmap
= isl_basic_map_free(bmap
);
8766 bmap
= isl_basic_map_extend_constraints(bmap
, dim
, 0);
8767 for (i
= 0; i
< dim
; ++i
) {
8768 int j
= isl_basic_map_alloc_equality(bmap
);
8770 bmap
= isl_basic_map_free(bmap
);
8773 isl_seq_clr(bmap
->eq
[j
], 1 + total
);
8774 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
8775 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], 1);
8776 isl_int_set_si(bmap
->eq
[j
][1+nparam
+2*dim
+i
], -1);
8778 bset
= isl_basic_map_domain(bmap
);
8779 bset
= isl_basic_set_reset_space(bset
, target_space
);
8782 isl_basic_map_free(bmap
);
8786 /* Is the tuple of type "type1" of "map" the same as
8787 * the tuple of type "type2" of "space"?
8789 isl_bool
isl_map_space_tuple_is_equal(__isl_keep isl_map
*map
,
8790 enum isl_dim_type type1
, __isl_keep isl_space
*space
,
8791 enum isl_dim_type type2
)
8793 isl_space
*map_space
;
8795 map_space
= isl_map_peek_space(map
);
8796 return isl_space_tuple_is_equal(map_space
, type1
, space
, type2
);
8799 /* Is the tuple of type "type1" of "map1" the same as
8800 * the tuple of type "type2" of "map2"?
8802 isl_bool
isl_map_tuple_is_equal(__isl_keep isl_map
*map1
,
8803 enum isl_dim_type type1
, __isl_keep isl_map
*map2
,
8804 enum isl_dim_type type2
)
8806 isl_space
*space1
, *space2
;
8808 space1
= isl_map_peek_space(map1
);
8809 space2
= isl_map_peek_space(map2
);
8810 return isl_space_tuple_is_equal(space1
, type1
, space2
, type2
);
8813 /* Is the space of "obj" equal to "space", ignoring parameters?
8815 isl_bool
isl_map_has_space_tuples(__isl_keep isl_map
*map
,
8816 __isl_keep isl_space
*space
)
8818 isl_space
*map_space
;
8820 map_space
= isl_map_peek_space(map
);
8821 return isl_space_has_equal_tuples(map_space
, space
);
8824 /* Check that "map" is a transformation, i.e.,
8825 * that it relates elements from the same space.
8827 isl_stat
isl_map_check_transformation(__isl_keep isl_map
*map
)
8831 equal
= isl_map_tuple_is_equal(map
, isl_dim_in
, map
, isl_dim_out
);
8833 return isl_stat_error
;
8835 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
8836 "domain and range don't match", return isl_stat_error
);
8841 * returns range - domain
8843 __isl_give isl_set
*isl_map_deltas(__isl_take isl_map
*map
)
8847 struct isl_set
*result
;
8849 if (isl_map_check_transformation(map
) < 0)
8851 space
= isl_map_get_space(map
);
8852 space
= isl_space_domain(space
);
8853 result
= isl_set_alloc_space(space
, map
->n
, 0);
8856 for (i
= 0; i
< map
->n
; ++i
)
8857 result
= isl_set_add_basic_set(result
,
8858 isl_basic_map_deltas(isl_basic_map_copy(map
->p
[i
])));
8867 * returns [domain -> range] -> range - domain
8869 __isl_give isl_basic_map
*isl_basic_map_deltas_map(
8870 __isl_take isl_basic_map
*bmap
)
8874 isl_basic_map
*domain
;
8878 if (isl_basic_map_check_transformation(bmap
) < 0)
8879 return isl_basic_map_free(bmap
);
8881 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
8882 n
= isl_basic_map_dim(bmap
, isl_dim_in
);
8883 if (nparam
< 0 || n
< 0)
8884 return isl_basic_map_free(bmap
);
8886 space
= isl_basic_map_get_space(bmap
);
8887 space
= isl_space_from_range(isl_space_domain(space
));
8888 domain
= isl_basic_map_universe(space
);
8890 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
8891 bmap
= isl_basic_map_apply_range(bmap
, domain
);
8892 bmap
= isl_basic_map_extend_constraints(bmap
, n
, 0);
8894 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
8896 return isl_basic_map_free(bmap
);
8898 for (i
= 0; i
< n
; ++i
) {
8899 k
= isl_basic_map_alloc_equality(bmap
);
8902 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
8903 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], 1);
8904 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ i
], -1);
8905 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ n
+ i
], 1);
8908 bmap
= isl_basic_map_gauss(bmap
, NULL
);
8909 return isl_basic_map_finalize(bmap
);
8911 isl_basic_map_free(bmap
);
8916 * returns [domain -> range] -> range - domain
8918 __isl_give isl_map
*isl_map_deltas_map(__isl_take isl_map
*map
)
8920 if (isl_map_check_transformation(map
) < 0)
8921 return isl_map_free(map
);
8923 return isl_map_transform(map
, &isl_space_range_map
,
8924 &isl_basic_map_deltas_map
);
8927 /* Return pairs of elements { x -> y } such that y - x is in "deltas".
8929 __isl_give isl_map
*isl_set_translation(__isl_take isl_set
*deltas
)
8934 space
= isl_space_map_from_set(isl_set_get_space(deltas
));
8935 map
= isl_map_deltas_map(isl_map_universe(space
));
8936 map
= isl_map_intersect_range(map
, deltas
);
8938 return isl_set_unwrap(isl_map_domain(map
));
8941 __isl_give isl_basic_map
*isl_basic_map_identity(__isl_take isl_space
*space
)
8943 isl_size n_in
, n_out
;
8945 n_in
= isl_space_dim(space
, isl_dim_in
);
8946 n_out
= isl_space_dim(space
, isl_dim_out
);
8947 if (n_in
< 0 || n_out
< 0)
8950 isl_die(space
->ctx
, isl_error_invalid
,
8951 "number of input and output dimensions needs to be "
8952 "the same", goto error
);
8953 return isl_basic_map_equal(space
, n_in
);
8955 isl_space_free(space
);
8959 __isl_give isl_map
*isl_map_identity(__isl_take isl_space
*space
)
8961 return isl_map_from_basic_map(isl_basic_map_identity(space
));
8964 __isl_give isl_map
*isl_set_identity(__isl_take isl_set
*set
)
8966 isl_space
*space
= isl_set_get_space(set
);
8968 id
= isl_map_identity(isl_space_map_from_set(space
));
8969 return isl_map_intersect_range(id
, set
);
8972 /* Construct a basic set with all set dimensions having only non-negative
8975 __isl_give isl_basic_set
*isl_basic_set_positive_orthant(
8976 __isl_take isl_space
*space
)
8982 struct isl_basic_set
*bset
;
8984 nparam
= isl_space_dim(space
, isl_dim_param
);
8985 dim
= isl_space_dim(space
, isl_dim_set
);
8986 total
= isl_space_dim(space
, isl_dim_all
);
8987 if (nparam
< 0 || dim
< 0 || total
< 0)
8988 space
= isl_space_free(space
);
8989 bset
= isl_basic_set_alloc_space(space
, 0, 0, dim
);
8992 for (i
= 0; i
< dim
; ++i
) {
8993 int k
= isl_basic_set_alloc_inequality(bset
);
8996 isl_seq_clr(bset
->ineq
[k
], 1 + total
);
8997 isl_int_set_si(bset
->ineq
[k
][1 + nparam
+ i
], 1);
9001 isl_basic_set_free(bset
);
9005 /* Construct the half-space x_pos >= 0.
9007 static __isl_give isl_basic_set
*nonneg_halfspace(__isl_take isl_space
*space
,
9012 isl_basic_set
*nonneg
;
9014 total
= isl_space_dim(space
, isl_dim_all
);
9016 space
= isl_space_free(space
);
9017 nonneg
= isl_basic_set_alloc_space(space
, 0, 0, 1);
9018 k
= isl_basic_set_alloc_inequality(nonneg
);
9021 isl_seq_clr(nonneg
->ineq
[k
], 1 + total
);
9022 isl_int_set_si(nonneg
->ineq
[k
][pos
], 1);
9024 return isl_basic_set_finalize(nonneg
);
9026 isl_basic_set_free(nonneg
);
9030 /* Construct the half-space x_pos <= -1.
9032 static __isl_give isl_basic_set
*neg_halfspace(__isl_take isl_space
*space
,
9039 total
= isl_space_dim(space
, isl_dim_all
);
9041 space
= isl_space_free(space
);
9042 neg
= isl_basic_set_alloc_space(space
, 0, 0, 1);
9043 k
= isl_basic_set_alloc_inequality(neg
);
9046 isl_seq_clr(neg
->ineq
[k
], 1 + total
);
9047 isl_int_set_si(neg
->ineq
[k
][0], -1);
9048 isl_int_set_si(neg
->ineq
[k
][pos
], -1);
9050 return isl_basic_set_finalize(neg
);
9052 isl_basic_set_free(neg
);
9056 __isl_give isl_set
*isl_set_split_dims(__isl_take isl_set
*set
,
9057 enum isl_dim_type type
, unsigned first
, unsigned n
)
9061 isl_basic_set
*nonneg
;
9067 if (isl_set_check_range(set
, type
, first
, n
) < 0)
9068 return isl_set_free(set
);
9070 offset
= pos(set
->dim
, type
);
9071 for (i
= 0; i
< n
; ++i
) {
9072 nonneg
= nonneg_halfspace(isl_set_get_space(set
),
9073 offset
+ first
+ i
);
9074 neg
= neg_halfspace(isl_set_get_space(set
), offset
+ first
+ i
);
9076 set
= isl_set_intersect(set
, isl_basic_set_union(nonneg
, neg
));
9082 static isl_stat
foreach_orthant(__isl_take isl_set
*set
, int *signs
, int first
,
9084 isl_stat (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
9090 return isl_stat_error
;
9091 if (isl_set_plain_is_empty(set
)) {
9096 return fn(set
, signs
, user
);
9099 half
= isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set
),
9101 half
= isl_set_intersect(half
, isl_set_copy(set
));
9102 if (foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
) < 0)
9106 half
= isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set
),
9108 half
= isl_set_intersect(half
, set
);
9109 return foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
);
9112 return isl_stat_error
;
9115 /* Call "fn" on the intersections of "set" with each of the orthants
9116 * (except for obviously empty intersections). The orthant is identified
9117 * by the signs array, with each entry having value 1 or -1 according
9118 * to the sign of the corresponding variable.
9120 isl_stat
isl_set_foreach_orthant(__isl_keep isl_set
*set
,
9121 isl_stat (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
9130 return isl_stat_error
;
9131 if (isl_set_plain_is_empty(set
))
9134 nparam
= isl_set_dim(set
, isl_dim_param
);
9135 nvar
= isl_set_dim(set
, isl_dim_set
);
9136 if (nparam
< 0 || nvar
< 0)
9137 return isl_stat_error
;
9139 signs
= isl_alloc_array(set
->ctx
, int, nparam
+ nvar
);
9141 r
= foreach_orthant(isl_set_copy(set
), signs
, 0, nparam
+ nvar
,
9149 isl_bool
isl_set_is_equal(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
9151 return isl_map_is_equal(set_to_map(set1
), set_to_map(set2
));
9154 isl_bool
isl_basic_map_is_subset(__isl_keep isl_basic_map
*bmap1
,
9155 __isl_keep isl_basic_map
*bmap2
)
9158 struct isl_map
*map1
;
9159 struct isl_map
*map2
;
9161 if (!bmap1
|| !bmap2
)
9162 return isl_bool_error
;
9164 map1
= isl_map_from_basic_map(isl_basic_map_copy(bmap1
));
9165 map2
= isl_map_from_basic_map(isl_basic_map_copy(bmap2
));
9167 is_subset
= isl_map_is_subset(map1
, map2
);
9175 isl_bool
isl_basic_set_is_subset(__isl_keep isl_basic_set
*bset1
,
9176 __isl_keep isl_basic_set
*bset2
)
9178 return isl_basic_map_is_subset(bset1
, bset2
);
9181 isl_bool
isl_basic_map_is_equal(__isl_keep isl_basic_map
*bmap1
,
9182 __isl_keep isl_basic_map
*bmap2
)
9186 if (!bmap1
|| !bmap2
)
9187 return isl_bool_error
;
9188 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
9189 if (is_subset
!= isl_bool_true
)
9191 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
9195 isl_bool
isl_basic_set_is_equal(__isl_keep isl_basic_set
*bset1
,
9196 __isl_keep isl_basic_set
*bset2
)
9198 return isl_basic_map_is_equal(
9199 bset_to_bmap(bset1
), bset_to_bmap(bset2
));
9202 isl_bool
isl_map_is_empty(__isl_keep isl_map
*map
)
9208 return isl_bool_error
;
9209 for (i
= 0; i
< map
->n
; ++i
) {
9210 is_empty
= isl_basic_map_is_empty(map
->p
[i
]);
9212 return isl_bool_error
;
9214 return isl_bool_false
;
9216 return isl_bool_true
;
9219 isl_bool
isl_map_plain_is_empty(__isl_keep isl_map
*map
)
9221 return map
? map
->n
== 0 : isl_bool_error
;
9224 isl_bool
isl_set_plain_is_empty(__isl_keep isl_set
*set
)
9226 return set
? set
->n
== 0 : isl_bool_error
;
9229 isl_bool
isl_set_is_empty(__isl_keep isl_set
*set
)
9231 return isl_map_is_empty(set_to_map(set
));
9235 #define TYPE isl_basic_map
9238 #include "isl_type_has_equal_space_bin_templ.c"
9239 #include "isl_type_check_equal_space_templ.c"
9241 /* Check that "bset1" and "bset2" live in the same space,
9242 * reporting an error if they do not.
9244 isl_stat
isl_basic_set_check_equal_space(__isl_keep isl_basic_set
*bset1
,
9245 __isl_keep isl_basic_set
*bset2
)
9247 return isl_basic_map_check_equal_space(bset_to_bmap(bset1
),
9248 bset_to_bmap(bset1
));
9252 #define TYPE isl_map
9254 #include "isl_type_has_equal_space_bin_templ.c"
9255 #include "isl_type_check_equal_space_templ.c"
9256 #include "isl_type_has_space_templ.c"
9258 isl_bool
isl_set_has_equal_space(__isl_keep isl_set
*set1
,
9259 __isl_keep isl_set
*set2
)
9261 return isl_map_has_equal_space(set_to_map(set1
), set_to_map(set2
));
9265 #define TYPE1 isl_map
9267 #define TYPE2 isl_basic_map
9269 #define TYPE_PAIR isl_map_basic_map
9272 #include "isl_type_has_equal_space_templ.c"
9273 #include "isl_type_check_equal_space_templ.c"
9275 /* Check that "set" and "bset" live in the same space,
9276 * reporting an error if they do not.
9278 isl_stat
isl_set_basic_set_check_equal_space(__isl_keep isl_set
*set
,
9279 __isl_keep isl_basic_set
*bset
)
9281 return isl_map_basic_map_check_equal_space(set_to_map(set
),
9282 bset_to_bmap(bset
));
9285 static isl_bool
map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
9290 return isl_bool_error
;
9291 is_subset
= isl_map_is_subset(map1
, map2
);
9292 if (is_subset
!= isl_bool_true
)
9294 is_subset
= isl_map_is_subset(map2
, map1
);
9298 /* Is "map1" equal to "map2"?
9300 * First check if they are obviously equal.
9301 * If not, then perform a more detailed analysis.
9303 isl_bool
isl_map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
9307 equal
= isl_map_plain_is_equal(map1
, map2
);
9308 if (equal
< 0 || equal
)
9310 return isl_map_align_params_map_map_and_test(map1
, map2
, &map_is_equal
);
9313 isl_bool
isl_basic_map_is_strict_subset(__isl_keep isl_basic_map
*bmap1
,
9314 __isl_keep isl_basic_map
*bmap2
)
9318 if (!bmap1
|| !bmap2
)
9319 return isl_bool_error
;
9320 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
9321 if (is_subset
!= isl_bool_true
)
9323 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
9324 return isl_bool_not(is_subset
);
9327 isl_bool
isl_map_is_strict_subset(__isl_keep isl_map
*map1
,
9328 __isl_keep isl_map
*map2
)
9333 return isl_bool_error
;
9334 is_subset
= isl_map_is_subset(map1
, map2
);
9335 if (is_subset
!= isl_bool_true
)
9337 is_subset
= isl_map_is_subset(map2
, map1
);
9338 return isl_bool_not(is_subset
);
9341 isl_bool
isl_set_is_strict_subset(__isl_keep isl_set
*set1
,
9342 __isl_keep isl_set
*set2
)
9344 return isl_map_is_strict_subset(set_to_map(set1
), set_to_map(set2
));
9347 /* Is "bmap" obviously equal to the universe with the same space?
9349 * That is, does it not have any constraints?
9351 isl_bool
isl_basic_map_plain_is_universe(__isl_keep isl_basic_map
*bmap
)
9354 return isl_bool_error
;
9355 return bmap
->n_eq
== 0 && bmap
->n_ineq
== 0;
9358 /* Is "bset" obviously equal to the universe with the same space?
9360 isl_bool
isl_basic_set_plain_is_universe(__isl_keep isl_basic_set
*bset
)
9362 return isl_basic_map_plain_is_universe(bset
);
9365 /* If "c" does not involve any existentially quantified variables,
9366 * then set *univ to false and abort
9368 static isl_stat
involves_divs(__isl_take isl_constraint
*c
, void *user
)
9370 isl_bool
*univ
= user
;
9373 n
= isl_constraint_dim(c
, isl_dim_div
);
9375 c
= isl_constraint_free(c
);
9376 *univ
= isl_constraint_involves_dims(c
, isl_dim_div
, 0, n
);
9377 isl_constraint_free(c
);
9378 if (*univ
< 0 || !*univ
)
9379 return isl_stat_error
;
9383 /* Is "bmap" equal to the universe with the same space?
9385 * First check if it is obviously equal to the universe.
9386 * If not and if there are any constraints not involving
9387 * existentially quantified variables, then it is certainly
9388 * not equal to the universe.
9389 * Otherwise, check if the universe is a subset of "bmap".
9391 isl_bool
isl_basic_map_is_universe(__isl_keep isl_basic_map
*bmap
)
9395 isl_basic_map
*test
;
9397 univ
= isl_basic_map_plain_is_universe(bmap
);
9398 if (univ
< 0 || univ
)
9400 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
9402 return isl_bool_error
;
9404 return isl_bool_false
;
9405 univ
= isl_bool_true
;
9406 if (isl_basic_map_foreach_constraint(bmap
, &involves_divs
, &univ
) < 0 &&
9408 return isl_bool_error
;
9409 if (univ
< 0 || !univ
)
9411 test
= isl_basic_map_universe(isl_basic_map_get_space(bmap
));
9412 univ
= isl_basic_map_is_subset(test
, bmap
);
9413 isl_basic_map_free(test
);
9417 /* Is "bset" equal to the universe with the same space?
9419 isl_bool
isl_basic_set_is_universe(__isl_keep isl_basic_set
*bset
)
9421 return isl_basic_map_is_universe(bset
);
9424 isl_bool
isl_map_plain_is_universe(__isl_keep isl_map
*map
)
9429 return isl_bool_error
;
9431 for (i
= 0; i
< map
->n
; ++i
) {
9432 isl_bool r
= isl_basic_map_plain_is_universe(map
->p
[i
]);
9437 return isl_bool_false
;
9440 isl_bool
isl_set_plain_is_universe(__isl_keep isl_set
*set
)
9442 return isl_map_plain_is_universe(set_to_map(set
));
9445 isl_bool
isl_basic_map_is_empty(__isl_keep isl_basic_map
*bmap
)
9447 struct isl_basic_set
*bset
= NULL
;
9448 struct isl_vec
*sample
= NULL
;
9449 isl_bool empty
, non_empty
;
9452 return isl_bool_error
;
9454 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
9455 return isl_bool_true
;
9457 if (isl_basic_map_plain_is_universe(bmap
))
9458 return isl_bool_false
;
9460 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
9461 struct isl_basic_map
*copy
= isl_basic_map_copy(bmap
);
9462 copy
= isl_basic_map_remove_redundancies(copy
);
9463 empty
= isl_basic_map_plain_is_empty(copy
);
9464 isl_basic_map_free(copy
);
9468 non_empty
= isl_basic_map_plain_is_non_empty(bmap
);
9470 return isl_bool_error
;
9472 return isl_bool_false
;
9473 isl_vec_free(bmap
->sample
);
9474 bmap
->sample
= NULL
;
9475 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
9477 return isl_bool_error
;
9478 sample
= isl_basic_set_sample_vec(bset
);
9480 return isl_bool_error
;
9481 empty
= sample
->size
== 0;
9482 isl_vec_free(bmap
->sample
);
9483 bmap
->sample
= sample
;
9485 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
9490 isl_bool
isl_basic_map_plain_is_empty(__isl_keep isl_basic_map
*bmap
)
9493 return isl_bool_error
;
9494 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
);
9497 isl_bool
isl_basic_set_plain_is_empty(__isl_keep isl_basic_set
*bset
)
9500 return isl_bool_error
;
9501 return ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
);
9504 /* Is "bmap" known to be non-empty?
9506 * That is, is the cached sample still valid?
9508 isl_bool
isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map
*bmap
)
9513 return isl_bool_error
;
9515 return isl_bool_false
;
9516 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
9518 return isl_bool_error
;
9519 if (bmap
->sample
->size
!= 1 + total
)
9520 return isl_bool_false
;
9521 return isl_basic_map_contains(bmap
, bmap
->sample
);
9524 isl_bool
isl_basic_set_is_empty(__isl_keep isl_basic_set
*bset
)
9526 return isl_basic_map_is_empty(bset_to_bmap(bset
));
9529 __isl_give isl_map
*isl_basic_map_union(__isl_take isl_basic_map
*bmap1
,
9530 __isl_take isl_basic_map
*bmap2
)
9532 struct isl_map
*map
;
9534 if (isl_basic_map_check_equal_space(bmap1
, bmap2
) < 0)
9537 map
= isl_map_alloc_space(isl_space_copy(bmap1
->dim
), 2, 0);
9540 map
= isl_map_add_basic_map(map
, bmap1
);
9541 map
= isl_map_add_basic_map(map
, bmap2
);
9544 isl_basic_map_free(bmap1
);
9545 isl_basic_map_free(bmap2
);
9549 __isl_give isl_set
*isl_basic_set_union(__isl_take isl_basic_set
*bset1
,
9550 __isl_take isl_basic_set
*bset2
)
9552 return set_from_map(isl_basic_map_union(bset_to_bmap(bset1
),
9553 bset_to_bmap(bset2
)));
9556 /* Order divs such that any div only depends on previous divs */
9557 __isl_give isl_basic_map
*isl_basic_map_order_divs(
9558 __isl_take isl_basic_map
*bmap
)
9563 off
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
9565 return isl_basic_map_free(bmap
);
9567 for (i
= 0; i
< bmap
->n_div
; ++i
) {
9569 if (isl_int_is_zero(bmap
->div
[i
][0]))
9571 pos
= isl_seq_first_non_zero(bmap
->div
[i
]+1+1+off
+i
,
9576 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_internal
,
9577 "integer division depends on itself",
9578 return isl_basic_map_free(bmap
));
9579 bmap
= isl_basic_map_swap_div(bmap
, i
, i
+ pos
);
9587 __isl_give isl_map
*isl_map_order_divs(__isl_take isl_map
*map
)
9594 for (i
= 0; i
< map
->n
; ++i
) {
9595 map
->p
[i
] = isl_basic_map_order_divs(map
->p
[i
]);
9606 /* Sort the local variables of "bset".
9608 __isl_give isl_basic_set
*isl_basic_set_sort_divs(
9609 __isl_take isl_basic_set
*bset
)
9611 return bset_from_bmap(isl_basic_map_sort_divs(bset_to_bmap(bset
)));
9614 /* Apply the expansion computed by isl_merge_divs.
9615 * The expansion itself is given by "exp" while the resulting
9616 * list of divs is given by "div".
9618 * Move the integer divisions of "bmap" into the right position
9619 * according to "exp" and then introduce the additional integer
9620 * divisions, adding div constraints.
9621 * The moving should be done first to avoid moving coefficients
9622 * in the definitions of the extra integer divisions.
9624 __isl_give isl_basic_map
*isl_basic_map_expand_divs(
9625 __isl_take isl_basic_map
*bmap
, __isl_take isl_mat
*div
, int *exp
)
9630 bmap
= isl_basic_map_cow(bmap
);
9634 if (div
->n_row
< bmap
->n_div
)
9635 isl_die(isl_mat_get_ctx(div
), isl_error_invalid
,
9636 "not an expansion", goto error
);
9638 n_div
= bmap
->n_div
;
9639 bmap
= isl_basic_map_extend(bmap
, div
->n_row
- n_div
, 0,
9640 2 * (div
->n_row
- n_div
));
9642 for (i
= n_div
; i
< div
->n_row
; ++i
)
9643 if (isl_basic_map_alloc_div(bmap
) < 0)
9646 for (j
= n_div
- 1; j
>= 0; --j
) {
9649 bmap
= isl_basic_map_swap_div(bmap
, j
, exp
[j
]);
9654 for (i
= 0; i
< div
->n_row
; ++i
) {
9655 if (j
< n_div
&& exp
[j
] == i
) {
9658 isl_seq_cpy(bmap
->div
[i
], div
->row
[i
], div
->n_col
);
9659 if (isl_basic_map_div_is_marked_unknown(bmap
, i
))
9661 bmap
= isl_basic_map_add_div_constraints(bmap
, i
);
9670 isl_basic_map_free(bmap
);
9675 /* Apply the expansion computed by isl_merge_divs.
9676 * The expansion itself is given by "exp" while the resulting
9677 * list of divs is given by "div".
9679 __isl_give isl_basic_set
*isl_basic_set_expand_divs(
9680 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*div
, int *exp
)
9682 return isl_basic_map_expand_divs(bset
, div
, exp
);
9685 /* Look for a div in dst that corresponds to the div "div" in src.
9686 * The divs before "div" in src and dst are assumed to be the same.
9688 * Return the position of the corresponding div in dst
9689 * if there is one. Otherwise, return a position beyond the integer divisions.
9690 * Return -1 on error.
9692 static int find_div(__isl_keep isl_basic_map
*dst
,
9693 __isl_keep isl_basic_map
*src
, unsigned div
)
9699 v_div
= isl_basic_map_var_offset(src
, isl_dim_div
);
9700 n_div
= isl_basic_map_dim(dst
, isl_dim_div
);
9701 if (n_div
< 0 || v_div
< 0)
9703 isl_assert(dst
->ctx
, div
<= n_div
, return -1);
9704 for (i
= div
; i
< n_div
; ++i
)
9705 if (isl_seq_eq(dst
->div
[i
], src
->div
[div
], 1+1+v_div
+div
) &&
9706 isl_seq_first_non_zero(dst
->div
[i
] + 1 + 1 + v_div
+ div
,
9712 /* Align the divs of "dst" to those of "src", adding divs from "src"
9713 * if needed. That is, make sure that the first src->n_div divs
9714 * of the result are equal to those of src.
9715 * The integer division of "src" are assumed to be ordered.
9717 * The integer divisions are swapped into the right position
9718 * (possibly after adding them first). This may result
9719 * in the remaining integer divisions appearing in the wrong order,
9720 * i.e., with some integer division appearing before
9721 * some other integer division on which it depends.
9722 * The integer divisions therefore need to be ordered.
9723 * This will not affect the integer divisions aligned to those of "src",
9724 * since "src" is assumed to have ordered integer divisions.
9726 * The result is not finalized as by design it will have redundant
9727 * divs if any divs from "src" were copied.
9729 __isl_give isl_basic_map
*isl_basic_map_align_divs(
9730 __isl_take isl_basic_map
*dst
, __isl_keep isl_basic_map
*src
)
9739 return isl_basic_map_free(dst
);
9741 if (src
->n_div
== 0)
9744 known
= isl_basic_map_divs_known(src
);
9746 return isl_basic_map_free(dst
);
9748 isl_die(isl_basic_map_get_ctx(src
), isl_error_invalid
,
9749 "some src divs are unknown",
9750 return isl_basic_map_free(dst
));
9752 v_div
= isl_basic_map_var_offset(src
, isl_dim_div
);
9754 return isl_basic_map_free(dst
);
9757 dst_n_div
= isl_basic_map_dim(dst
, isl_dim_div
);
9759 dst
= isl_basic_map_free(dst
);
9760 for (i
= 0; i
< src
->n_div
; ++i
) {
9761 int j
= find_div(dst
, src
, i
);
9763 dst
= isl_basic_map_free(dst
);
9764 if (j
== dst_n_div
) {
9766 int extra
= src
->n_div
- i
;
9767 dst
= isl_basic_map_cow(dst
);
9769 return isl_basic_map_free(dst
);
9770 dst
= isl_basic_map_extend(dst
,
9771 extra
, 0, 2 * extra
);
9774 j
= isl_basic_map_alloc_div(dst
);
9776 return isl_basic_map_free(dst
);
9777 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+v_div
+i
);
9778 isl_seq_clr(dst
->div
[j
]+1+1+v_div
+i
, dst
->n_div
- i
);
9780 dst
= isl_basic_map_add_div_constraints(dst
, j
);
9782 return isl_basic_map_free(dst
);
9785 dst
= isl_basic_map_swap_div(dst
, i
, j
);
9787 return isl_basic_map_free(dst
);
9789 return isl_basic_map_order_divs(dst
);
9792 __isl_give isl_map
*isl_map_align_divs_internal(__isl_take isl_map
*map
)
9800 map
= isl_map_compute_divs(map
);
9801 map
= isl_map_order_divs(map
);
9802 map
= isl_map_cow(map
);
9806 for (i
= 1; i
< map
->n
; ++i
)
9807 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], map
->p
[i
]);
9808 for (i
= 1; i
< map
->n
; ++i
) {
9809 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], map
->p
[0]);
9811 return isl_map_free(map
);
9814 map
= isl_map_unmark_normalized(map
);
9818 __isl_give isl_map
*isl_map_align_divs(__isl_take isl_map
*map
)
9820 return isl_map_align_divs_internal(map
);
9823 __isl_give isl_set
*isl_set_align_divs(__isl_take isl_set
*set
)
9825 return set_from_map(isl_map_align_divs_internal(set_to_map(set
)));
9828 /* Align the divs of the basic maps in "map" to those
9829 * of the basic maps in "list", as well as to the other basic maps in "map".
9830 * The elements in "list" are assumed to have known divs.
9832 __isl_give isl_map
*isl_map_align_divs_to_basic_map_list(
9833 __isl_take isl_map
*map
, __isl_keep isl_basic_map_list
*list
)
9838 n
= isl_basic_map_list_n_basic_map(list
);
9839 map
= isl_map_compute_divs(map
);
9840 map
= isl_map_cow(map
);
9842 return isl_map_free(map
);
9846 for (i
= 0; i
< n
; ++i
) {
9847 isl_basic_map
*bmap
;
9849 bmap
= isl_basic_map_list_get_basic_map(list
, i
);
9850 bmap
= isl_basic_map_order_divs(bmap
);
9851 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], bmap
);
9852 isl_basic_map_free(bmap
);
9855 return isl_map_free(map
);
9857 return isl_map_align_divs_internal(map
);
9860 /* Align the divs of each element of "list" to those of "bmap".
9861 * Both "bmap" and the elements of "list" are assumed to have known divs.
9863 __isl_give isl_basic_map_list
*isl_basic_map_list_align_divs_to_basic_map(
9864 __isl_take isl_basic_map_list
*list
, __isl_keep isl_basic_map
*bmap
)
9869 n
= isl_basic_map_list_n_basic_map(list
);
9871 return isl_basic_map_list_free(list
);
9873 for (i
= 0; i
< n
; ++i
) {
9874 isl_basic_map
*bmap_i
;
9876 bmap_i
= isl_basic_map_list_get_basic_map(list
, i
);
9877 bmap_i
= isl_basic_map_align_divs(bmap_i
, bmap
);
9878 list
= isl_basic_map_list_set_basic_map(list
, i
, bmap_i
);
9884 __isl_give isl_set
*isl_set_apply( __isl_take isl_set
*set
,
9885 __isl_take isl_map
*map
)
9889 isl_map_align_params_set(&map
, &set
);
9890 ok
= isl_map_compatible_domain(map
, set
);
9894 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
9895 "incompatible spaces", goto error
);
9896 map
= isl_map_intersect_domain(map
, set
);
9897 set
= isl_map_range(map
);
9905 /* There is no need to cow as removing empty parts doesn't change
9906 * the meaning of the set.
9908 __isl_give isl_map
*isl_map_remove_empty_parts(__isl_take isl_map
*map
)
9915 for (i
= map
->n
- 1; i
>= 0; --i
)
9916 map
= remove_if_empty(map
, i
);
9921 __isl_give isl_set
*isl_set_remove_empty_parts(__isl_take isl_set
*set
)
9923 return set_from_map(isl_map_remove_empty_parts(set_to_map(set
)));
9926 /* Create a binary relation that maps the shared initial "pos" dimensions
9927 * of "bset1" and "bset2" to the remaining dimensions of "bset1" and "bset2".
9929 static __isl_give isl_basic_map
*join_initial(__isl_keep isl_basic_set
*bset1
,
9930 __isl_keep isl_basic_set
*bset2
, int pos
)
9932 isl_basic_map
*bmap1
;
9933 isl_basic_map
*bmap2
;
9935 bmap1
= isl_basic_map_from_range(isl_basic_set_copy(bset1
));
9936 bmap2
= isl_basic_map_from_range(isl_basic_set_copy(bset2
));
9937 bmap1
= isl_basic_map_move_dims(bmap1
, isl_dim_in
, 0,
9938 isl_dim_out
, 0, pos
);
9939 bmap2
= isl_basic_map_move_dims(bmap2
, isl_dim_in
, 0,
9940 isl_dim_out
, 0, pos
);
9941 return isl_basic_map_range_product(bmap1
, bmap2
);
9944 /* Given two basic sets bset1 and bset2, compute the maximal difference
9945 * between the values of dimension pos in bset1 and those in bset2
9946 * for any common value of the parameters and dimensions preceding pos.
9948 static enum isl_lp_result
basic_set_maximal_difference_at(
9949 __isl_keep isl_basic_set
*bset1
, __isl_keep isl_basic_set
*bset2
,
9950 int pos
, isl_int
*opt
)
9952 isl_basic_map
*bmap1
;
9953 struct isl_ctx
*ctx
;
9954 struct isl_vec
*obj
;
9958 enum isl_lp_result res
;
9960 nparam
= isl_basic_set_dim(bset1
, isl_dim_param
);
9961 dim1
= isl_basic_set_dim(bset1
, isl_dim_set
);
9962 if (nparam
< 0 || dim1
< 0 || !bset2
)
9963 return isl_lp_error
;
9965 bmap1
= join_initial(bset1
, bset2
, pos
);
9966 total
= isl_basic_map_dim(bmap1
, isl_dim_all
);
9968 return isl_lp_error
;
9971 obj
= isl_vec_alloc(ctx
, 1 + total
);
9974 isl_seq_clr(obj
->block
.data
, 1 + total
);
9975 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
], 1);
9976 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
+(dim1
-pos
)], -1);
9977 res
= isl_basic_map_solve_lp(bmap1
, 1, obj
->block
.data
, ctx
->one
,
9979 isl_basic_map_free(bmap1
);
9983 isl_basic_map_free(bmap1
);
9984 return isl_lp_error
;
9987 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
9988 * for any common value of the parameters and dimensions preceding pos
9989 * in both basic sets, the values of dimension pos in bset1 are
9990 * smaller or larger than those in bset2.
9993 * 1 if bset1 follows bset2
9994 * -1 if bset1 precedes bset2
9995 * 0 if bset1 and bset2 are incomparable
9996 * -2 if some error occurred.
9998 int isl_basic_set_compare_at(__isl_keep isl_basic_set
*bset1
,
9999 __isl_keep isl_basic_set
*bset2
, int pos
)
10002 enum isl_lp_result res
;
10007 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
10009 if (res
== isl_lp_empty
)
10011 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
10012 res
== isl_lp_unbounded
)
10014 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
10019 isl_int_clear(opt
);
10023 /* Given two basic sets bset1 and bset2, check whether
10024 * for any common value of the parameters and dimensions preceding pos
10025 * there is a value of dimension pos in bset1 that is larger
10026 * than a value of the same dimension in bset2.
10029 * 1 if there exists such a pair
10030 * 0 if there is no such pair, but there is a pair of equal values
10032 * -2 if some error occurred.
10034 int isl_basic_set_follows_at(__isl_keep isl_basic_set
*bset1
,
10035 __isl_keep isl_basic_set
*bset2
, int pos
)
10038 isl_basic_map
*bmap
;
10041 dim1
= isl_basic_set_dim(bset1
, isl_dim_set
);
10044 bmap
= join_initial(bset1
, bset2
, pos
);
10045 bmap
= isl_basic_map_order_ge(bmap
, isl_dim_out
, 0,
10046 isl_dim_out
, dim1
- pos
);
10047 empty
= isl_basic_map_is_empty(bmap
);
10051 isl_basic_map_free(bmap
);
10054 bmap
= isl_basic_map_order_gt(bmap
, isl_dim_out
, 0,
10055 isl_dim_out
, dim1
- pos
);
10056 empty
= isl_basic_map_is_empty(bmap
);
10059 isl_basic_map_free(bmap
);
10064 isl_basic_map_free(bmap
);
10068 /* Given two sets set1 and set2, check whether
10069 * for any common value of the parameters and dimensions preceding pos
10070 * there is a value of dimension pos in set1 that is larger
10071 * than a value of the same dimension in set2.
10074 * 1 if there exists such a pair
10075 * 0 if there is no such pair, but there is a pair of equal values
10077 * -2 if some error occurred.
10079 int isl_set_follows_at(__isl_keep isl_set
*set1
,
10080 __isl_keep isl_set
*set2
, int pos
)
10085 if (!set1
|| !set2
)
10088 for (i
= 0; i
< set1
->n
; ++i
)
10089 for (j
= 0; j
< set2
->n
; ++j
) {
10091 f
= isl_basic_set_follows_at(set1
->p
[i
], set2
->p
[j
], pos
);
10092 if (f
== 1 || f
== -2)
10101 static isl_bool
isl_basic_map_plain_has_fixed_var(
10102 __isl_keep isl_basic_map
*bmap
, unsigned pos
, isl_int
*val
)
10108 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
10110 return isl_bool_error
;
10111 for (i
= 0, d
= total
-1; i
< bmap
->n_eq
&& d
+1 > pos
; ++i
) {
10112 for (; d
+1 > pos
; --d
)
10113 if (!isl_int_is_zero(bmap
->eq
[i
][1+d
]))
10117 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1, d
) != -1)
10118 return isl_bool_false
;
10119 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1+d
+1, total
-d
-1) != -1)
10120 return isl_bool_false
;
10121 if (!isl_int_is_one(bmap
->eq
[i
][1+d
]))
10122 return isl_bool_false
;
10124 isl_int_neg(*val
, bmap
->eq
[i
][0]);
10125 return isl_bool_true
;
10127 return isl_bool_false
;
10130 static isl_bool
isl_map_plain_has_fixed_var(__isl_keep isl_map
*map
,
10131 unsigned pos
, isl_int
*val
)
10139 return isl_bool_error
;
10141 return isl_bool_false
;
10143 return isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, val
);
10146 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, &v
);
10147 for (i
= 1; fixed
== isl_bool_true
&& i
< map
->n
; ++i
) {
10148 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[i
], pos
, &tmp
);
10149 if (fixed
== isl_bool_true
&& isl_int_ne(tmp
, v
))
10150 fixed
= isl_bool_false
;
10153 isl_int_set(*val
, v
);
10154 isl_int_clear(tmp
);
10159 static isl_bool
isl_basic_set_plain_has_fixed_var(
10160 __isl_keep isl_basic_set
*bset
, unsigned pos
, isl_int
*val
)
10162 return isl_basic_map_plain_has_fixed_var(bset_to_bmap(bset
),
10166 isl_bool
isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map
*bmap
,
10167 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
10169 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
10170 return isl_bool_error
;
10171 return isl_basic_map_plain_has_fixed_var(bmap
,
10172 isl_basic_map_offset(bmap
, type
) - 1 + pos
, val
);
10175 /* If "bmap" obviously lies on a hyperplane where the given dimension
10176 * has a fixed value, then return that value.
10177 * Otherwise return NaN.
10179 __isl_give isl_val
*isl_basic_map_plain_get_val_if_fixed(
10180 __isl_keep isl_basic_map
*bmap
,
10181 enum isl_dim_type type
, unsigned pos
)
10189 ctx
= isl_basic_map_get_ctx(bmap
);
10190 v
= isl_val_alloc(ctx
);
10193 fixed
= isl_basic_map_plain_is_fixed(bmap
, type
, pos
, &v
->n
);
10195 return isl_val_free(v
);
10197 isl_int_set_si(v
->d
, 1);
10201 return isl_val_nan(ctx
);
10204 isl_bool
isl_map_plain_is_fixed(__isl_keep isl_map
*map
,
10205 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
10207 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
10208 return isl_bool_error
;
10209 return isl_map_plain_has_fixed_var(map
,
10210 map_offset(map
, type
) - 1 + pos
, val
);
10213 /* If "map" obviously lies on a hyperplane where the given dimension
10214 * has a fixed value, then return that value.
10215 * Otherwise return NaN.
10217 __isl_give isl_val
*isl_map_plain_get_val_if_fixed(__isl_keep isl_map
*map
,
10218 enum isl_dim_type type
, unsigned pos
)
10226 ctx
= isl_map_get_ctx(map
);
10227 v
= isl_val_alloc(ctx
);
10230 fixed
= isl_map_plain_is_fixed(map
, type
, pos
, &v
->n
);
10232 return isl_val_free(v
);
10234 isl_int_set_si(v
->d
, 1);
10238 return isl_val_nan(ctx
);
10241 /* If "set" obviously lies on a hyperplane where the given dimension
10242 * has a fixed value, then return that value.
10243 * Otherwise return NaN.
10245 __isl_give isl_val
*isl_set_plain_get_val_if_fixed(__isl_keep isl_set
*set
,
10246 enum isl_dim_type type
, unsigned pos
)
10248 return isl_map_plain_get_val_if_fixed(set
, type
, pos
);
10251 /* Return a sequence of values in the same space as "set"
10252 * that are equal to the corresponding set dimensions of "set"
10253 * for those set dimensions that obviously lie on a hyperplane
10254 * where the dimension has a fixed value.
10255 * The other elements are set to NaN.
10257 __isl_give isl_multi_val
*isl_set_get_plain_multi_val_if_fixed(
10258 __isl_keep isl_set
*set
)
10265 space
= isl_space_drop_all_params(isl_set_get_space(set
));
10266 mv
= isl_multi_val_alloc(space
);
10267 n
= isl_multi_val_size(mv
);
10269 return isl_multi_val_free(mv
);
10271 for (i
= 0; i
< n
; ++i
) {
10274 v
= isl_set_plain_get_val_if_fixed(set
, isl_dim_set
, i
);
10275 mv
= isl_multi_val_set_val(mv
, i
, v
);
10281 /* Check if dimension dim has fixed value and if so and if val is not NULL,
10282 * then return this fixed value in *val.
10284 isl_bool
isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set
*bset
,
10285 unsigned dim
, isl_int
*val
)
10289 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
10291 return isl_bool_error
;
10292 return isl_basic_set_plain_has_fixed_var(bset
, nparam
+ dim
, val
);
10295 /* Return -1 if the constraint "c1" should be sorted before "c2"
10296 * and 1 if it should be sorted after "c2".
10297 * Return 0 if the two constraints are the same (up to the constant term).
10299 * In particular, if a constraint involves later variables than another
10300 * then it is sorted after this other constraint.
10301 * uset_gist depends on constraints without existentially quantified
10302 * variables sorting first.
10304 * For constraints that have the same latest variable, those
10305 * with the same coefficient for this latest variable (first in absolute value
10306 * and then in actual value) are grouped together.
10307 * This is useful for detecting pairs of constraints that can
10308 * be chained in their printed representation.
10310 * Finally, within a group, constraints are sorted according to
10311 * their coefficients (excluding the constant term).
10313 static int sort_constraint_cmp(const void *p1
, const void *p2
, void *arg
)
10315 isl_int
**c1
= (isl_int
**) p1
;
10316 isl_int
**c2
= (isl_int
**) p2
;
10318 unsigned size
= *(unsigned *) arg
;
10321 l1
= isl_seq_last_non_zero(*c1
+ 1, size
);
10322 l2
= isl_seq_last_non_zero(*c2
+ 1, size
);
10327 cmp
= isl_int_abs_cmp((*c1
)[1 + l1
], (*c2
)[1 + l1
]);
10330 cmp
= isl_int_cmp((*c1
)[1 + l1
], (*c2
)[1 + l1
]);
10334 return isl_seq_cmp(*c1
+ 1, *c2
+ 1, size
);
10337 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
10338 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
10339 * and 0 if the two constraints are the same (up to the constant term).
10341 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map
*bmap
,
10342 isl_int
*c1
, isl_int
*c2
)
10347 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
10351 return sort_constraint_cmp(&c1
, &c2
, &size
);
10354 __isl_give isl_basic_map
*isl_basic_map_sort_constraints(
10355 __isl_take isl_basic_map
*bmap
)
10362 if (bmap
->n_ineq
== 0)
10364 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_SORTED
))
10366 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
10368 return isl_basic_map_free(bmap
);
10370 if (isl_sort(bmap
->ineq
, bmap
->n_ineq
, sizeof(isl_int
*),
10371 &sort_constraint_cmp
, &size
) < 0)
10372 return isl_basic_map_free(bmap
);
10373 ISL_F_SET(bmap
, ISL_BASIC_MAP_SORTED
);
10377 __isl_give isl_basic_set
*isl_basic_set_sort_constraints(
10378 __isl_take isl_basic_set
*bset
)
10380 isl_basic_map
*bmap
= bset_to_bmap(bset
);
10381 return bset_from_bmap(isl_basic_map_sort_constraints(bmap
));
10384 __isl_give isl_basic_map
*isl_basic_map_normalize(
10385 __isl_take isl_basic_map
*bmap
)
10387 bmap
= isl_basic_map_remove_redundancies(bmap
);
10388 bmap
= isl_basic_map_sort_constraints(bmap
);
10391 int isl_basic_map_plain_cmp(__isl_keep isl_basic_map
*bmap1
,
10392 __isl_keep isl_basic_map
*bmap2
)
10396 isl_space
*space1
, *space2
;
10398 if (!bmap1
|| !bmap2
)
10401 if (bmap1
== bmap2
)
10403 space1
= isl_basic_map_peek_space(bmap1
);
10404 space2
= isl_basic_map_peek_space(bmap2
);
10405 cmp
= isl_space_cmp(space1
, space2
);
10408 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) !=
10409 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_RATIONAL
))
10410 return ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) ? -1 : 1;
10411 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
) &&
10412 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
10414 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
))
10416 if (ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
10418 if (bmap1
->n_eq
!= bmap2
->n_eq
)
10419 return bmap1
->n_eq
- bmap2
->n_eq
;
10420 if (bmap1
->n_ineq
!= bmap2
->n_ineq
)
10421 return bmap1
->n_ineq
- bmap2
->n_ineq
;
10422 if (bmap1
->n_div
!= bmap2
->n_div
)
10423 return bmap1
->n_div
- bmap2
->n_div
;
10424 total
= isl_basic_map_dim(bmap1
, isl_dim_all
);
10427 for (i
= 0; i
< bmap1
->n_eq
; ++i
) {
10428 cmp
= isl_seq_cmp(bmap1
->eq
[i
], bmap2
->eq
[i
], 1+total
);
10432 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
10433 cmp
= isl_seq_cmp(bmap1
->ineq
[i
], bmap2
->ineq
[i
], 1+total
);
10437 for (i
= 0; i
< bmap1
->n_div
; ++i
) {
10438 isl_bool unknown1
, unknown2
;
10440 unknown1
= isl_basic_map_div_is_marked_unknown(bmap1
, i
);
10441 unknown2
= isl_basic_map_div_is_marked_unknown(bmap2
, i
);
10442 if (unknown1
< 0 || unknown2
< 0)
10444 if (unknown1
&& unknown2
)
10450 cmp
= isl_seq_cmp(bmap1
->div
[i
], bmap2
->div
[i
], 1+1+total
);
10457 int isl_basic_set_plain_cmp(__isl_keep isl_basic_set
*bset1
,
10458 __isl_keep isl_basic_set
*bset2
)
10460 return isl_basic_map_plain_cmp(bset1
, bset2
);
10463 int isl_set_plain_cmp(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
10469 if (set1
->n
!= set2
->n
)
10470 return set1
->n
- set2
->n
;
10472 for (i
= 0; i
< set1
->n
; ++i
) {
10473 cmp
= isl_basic_set_plain_cmp(set1
->p
[i
], set2
->p
[i
]);
10481 isl_bool
isl_basic_map_plain_is_equal(__isl_keep isl_basic_map
*bmap1
,
10482 __isl_keep isl_basic_map
*bmap2
)
10484 if (!bmap1
|| !bmap2
)
10485 return isl_bool_error
;
10486 return isl_basic_map_plain_cmp(bmap1
, bmap2
) == 0;
10489 isl_bool
isl_basic_set_plain_is_equal(__isl_keep isl_basic_set
*bset1
,
10490 __isl_keep isl_basic_set
*bset2
)
10492 return isl_basic_map_plain_is_equal(bset_to_bmap(bset1
),
10493 bset_to_bmap(bset2
));
10496 static int qsort_bmap_cmp(const void *p1
, const void *p2
)
10498 isl_basic_map
*bmap1
= *(isl_basic_map
**) p1
;
10499 isl_basic_map
*bmap2
= *(isl_basic_map
**) p2
;
10501 return isl_basic_map_plain_cmp(bmap1
, bmap2
);
10504 /* Sort the basic maps of "map" and remove duplicate basic maps.
10506 * While removing basic maps, we make sure that the basic maps remain
10507 * sorted because isl_map_normalize expects the basic maps of the result
10510 static __isl_give isl_map
*sort_and_remove_duplicates(__isl_take isl_map
*map
)
10514 map
= isl_map_remove_empty_parts(map
);
10517 qsort(map
->p
, map
->n
, sizeof(struct isl_basic_map
*), qsort_bmap_cmp
);
10518 for (i
= map
->n
- 1; i
>= 1; --i
) {
10519 if (!isl_basic_map_plain_is_equal(map
->p
[i
- 1], map
->p
[i
]))
10521 isl_basic_map_free(map
->p
[i
-1]);
10522 for (j
= i
; j
< map
->n
; ++j
)
10523 map
->p
[j
- 1] = map
->p
[j
];
10530 /* Remove obvious duplicates among the basic maps of "map".
10532 * Unlike isl_map_normalize, this function does not remove redundant
10533 * constraints and only removes duplicates that have exactly the same
10534 * constraints in the input. It does sort the constraints and
10535 * the basic maps to ease the detection of duplicates.
10537 * If "map" has already been normalized or if the basic maps are
10538 * disjoint, then there can be no duplicates.
10540 __isl_give isl_map
*isl_map_remove_obvious_duplicates(__isl_take isl_map
*map
)
10543 isl_basic_map
*bmap
;
10549 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
| ISL_MAP_DISJOINT
))
10551 for (i
= 0; i
< map
->n
; ++i
) {
10552 bmap
= isl_basic_map_copy(map
->p
[i
]);
10553 bmap
= isl_basic_map_sort_constraints(bmap
);
10555 return isl_map_free(map
);
10556 isl_basic_map_free(map
->p
[i
]);
10560 map
= sort_and_remove_duplicates(map
);
10564 /* We normalize in place, but if anything goes wrong we need
10565 * to return NULL, so we need to make sure we don't change the
10566 * meaning of any possible other copies of map.
10568 __isl_give isl_map
*isl_map_normalize(__isl_take isl_map
*map
)
10571 struct isl_basic_map
*bmap
;
10575 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
))
10577 for (i
= 0; i
< map
->n
; ++i
) {
10578 bmap
= isl_basic_map_normalize(isl_basic_map_copy(map
->p
[i
]));
10581 isl_basic_map_free(map
->p
[i
]);
10585 map
= sort_and_remove_duplicates(map
);
10587 ISL_F_SET(map
, ISL_MAP_NORMALIZED
);
10594 __isl_give isl_set
*isl_set_normalize(__isl_take isl_set
*set
)
10596 return set_from_map(isl_map_normalize(set_to_map(set
)));
10599 isl_bool
isl_map_plain_is_equal(__isl_keep isl_map
*map1
,
10600 __isl_keep isl_map
*map2
)
10605 if (!map1
|| !map2
)
10606 return isl_bool_error
;
10609 return isl_bool_true
;
10610 equal
= isl_map_has_equal_space(map1
, map2
);
10611 if (equal
< 0 || !equal
)
10614 map1
= isl_map_copy(map1
);
10615 map2
= isl_map_copy(map2
);
10616 map1
= isl_map_normalize(map1
);
10617 map2
= isl_map_normalize(map2
);
10618 if (!map1
|| !map2
)
10620 equal
= map1
->n
== map2
->n
;
10621 for (i
= 0; equal
&& i
< map1
->n
; ++i
) {
10622 equal
= isl_basic_map_plain_is_equal(map1
->p
[i
], map2
->p
[i
]);
10626 isl_map_free(map1
);
10627 isl_map_free(map2
);
10630 isl_map_free(map1
);
10631 isl_map_free(map2
);
10632 return isl_bool_error
;
10635 isl_bool
isl_set_plain_is_equal(__isl_keep isl_set
*set1
,
10636 __isl_keep isl_set
*set2
)
10638 return isl_map_plain_is_equal(set_to_map(set1
), set_to_map(set2
));
10641 /* Return the basic maps in "map" as a list.
10643 __isl_give isl_basic_map_list
*isl_map_get_basic_map_list(
10644 __isl_keep isl_map
*map
)
10648 isl_basic_map_list
*list
;
10652 ctx
= isl_map_get_ctx(map
);
10653 list
= isl_basic_map_list_alloc(ctx
, map
->n
);
10655 for (i
= 0; i
< map
->n
; ++i
) {
10656 isl_basic_map
*bmap
;
10658 bmap
= isl_basic_map_copy(map
->p
[i
]);
10659 list
= isl_basic_map_list_add(list
, bmap
);
10665 /* Return the intersection of the elements in the non-empty list "list".
10666 * All elements are assumed to live in the same space.
10668 __isl_give isl_basic_map
*isl_basic_map_list_intersect(
10669 __isl_take isl_basic_map_list
*list
)
10673 isl_basic_map
*bmap
;
10675 n
= isl_basic_map_list_n_basic_map(list
);
10679 isl_die(isl_basic_map_list_get_ctx(list
), isl_error_invalid
,
10680 "expecting non-empty list", goto error
);
10682 bmap
= isl_basic_map_list_get_basic_map(list
, 0);
10683 for (i
= 1; i
< n
; ++i
) {
10684 isl_basic_map
*bmap_i
;
10686 bmap_i
= isl_basic_map_list_get_basic_map(list
, i
);
10687 bmap
= isl_basic_map_intersect(bmap
, bmap_i
);
10690 isl_basic_map_list_free(list
);
10693 isl_basic_map_list_free(list
);
10697 /* Return the intersection of the elements in the non-empty list "list".
10698 * All elements are assumed to live in the same space.
10700 __isl_give isl_basic_set
*isl_basic_set_list_intersect(
10701 __isl_take isl_basic_set_list
*list
)
10703 return isl_basic_map_list_intersect(list
);
10706 /* Return the union of the elements of "list".
10707 * The list is required to have at least one element.
10709 __isl_give isl_set
*isl_basic_set_list_union(
10710 __isl_take isl_basic_set_list
*list
)
10715 isl_basic_set
*bset
;
10718 n
= isl_basic_set_list_n_basic_set(list
);
10722 isl_die(isl_basic_set_list_get_ctx(list
), isl_error_invalid
,
10723 "expecting non-empty list", goto error
);
10725 bset
= isl_basic_set_list_get_basic_set(list
, 0);
10726 space
= isl_basic_set_get_space(bset
);
10727 isl_basic_set_free(bset
);
10729 set
= isl_set_alloc_space(space
, n
, 0);
10730 for (i
= 0; i
< n
; ++i
) {
10731 bset
= isl_basic_set_list_get_basic_set(list
, i
);
10732 set
= isl_set_add_basic_set(set
, bset
);
10735 isl_basic_set_list_free(list
);
10738 isl_basic_set_list_free(list
);
10742 /* Return the union of the elements in the non-empty list "list".
10743 * All elements are assumed to live in the same space.
10745 __isl_give isl_set
*isl_set_list_union(__isl_take isl_set_list
*list
)
10751 n
= isl_set_list_n_set(list
);
10755 isl_die(isl_set_list_get_ctx(list
), isl_error_invalid
,
10756 "expecting non-empty list", goto error
);
10758 set
= isl_set_list_get_set(list
, 0);
10759 for (i
= 1; i
< n
; ++i
) {
10762 set_i
= isl_set_list_get_set(list
, i
);
10763 set
= isl_set_union(set
, set_i
);
10766 isl_set_list_free(list
);
10769 isl_set_list_free(list
);
10773 __isl_give isl_basic_map
*isl_basic_map_product(
10774 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
10776 isl_space
*space_result
= NULL
;
10777 struct isl_basic_map
*bmap
;
10778 unsigned in1
, in2
, out1
, out2
, nparam
, total
, pos
;
10779 struct isl_dim_map
*dim_map1
, *dim_map2
;
10781 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
10783 space_result
= isl_space_product(isl_space_copy(bmap1
->dim
),
10784 isl_space_copy(bmap2
->dim
));
10786 in1
= isl_basic_map_dim(bmap1
, isl_dim_in
);
10787 in2
= isl_basic_map_dim(bmap2
, isl_dim_in
);
10788 out1
= isl_basic_map_dim(bmap1
, isl_dim_out
);
10789 out2
= isl_basic_map_dim(bmap2
, isl_dim_out
);
10790 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
10792 total
= nparam
+ in1
+ in2
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
10793 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
10794 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
10795 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
10796 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
10797 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
10798 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
10799 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
10800 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
10801 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
10802 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
10804 bmap
= isl_basic_map_alloc_space(space_result
,
10805 bmap1
->n_div
+ bmap2
->n_div
,
10806 bmap1
->n_eq
+ bmap2
->n_eq
,
10807 bmap1
->n_ineq
+ bmap2
->n_ineq
);
10808 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
10809 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
10810 bmap
= isl_basic_map_simplify(bmap
);
10811 return isl_basic_map_finalize(bmap
);
10813 isl_basic_map_free(bmap1
);
10814 isl_basic_map_free(bmap2
);
10818 __isl_give isl_basic_map
*isl_basic_map_flat_product(
10819 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
10821 isl_basic_map
*prod
;
10823 prod
= isl_basic_map_product(bmap1
, bmap2
);
10824 prod
= isl_basic_map_flatten(prod
);
10828 __isl_give isl_basic_set
*isl_basic_set_flat_product(
10829 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
10831 return isl_basic_map_flat_range_product(bset1
, bset2
);
10834 __isl_give isl_basic_map
*isl_basic_map_domain_product(
10835 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
10837 isl_space
*space1
, *space2
;
10838 isl_space
*space_result
= NULL
;
10839 isl_basic_map
*bmap
;
10840 isl_size in1
, in2
, out
, nparam
;
10841 unsigned total
, pos
;
10842 struct isl_dim_map
*dim_map1
, *dim_map2
;
10844 in1
= isl_basic_map_dim(bmap1
, isl_dim_in
);
10845 in2
= isl_basic_map_dim(bmap2
, isl_dim_in
);
10846 out
= isl_basic_map_dim(bmap1
, isl_dim_out
);
10847 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
10848 if (in1
< 0 || in2
< 0 || out
< 0 || nparam
< 0)
10851 space1
= isl_basic_map_get_space(bmap1
);
10852 space2
= isl_basic_map_get_space(bmap2
);
10853 space_result
= isl_space_domain_product(space1
, space2
);
10855 total
= nparam
+ in1
+ in2
+ out
+ bmap1
->n_div
+ bmap2
->n_div
;
10856 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
10857 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
10858 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
10859 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
10860 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
10861 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
10862 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
10863 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
);
10864 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out
);
10865 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
10867 bmap
= isl_basic_map_alloc_space(space_result
,
10868 bmap1
->n_div
+ bmap2
->n_div
,
10869 bmap1
->n_eq
+ bmap2
->n_eq
,
10870 bmap1
->n_ineq
+ bmap2
->n_ineq
);
10871 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
10872 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
10873 bmap
= isl_basic_map_simplify(bmap
);
10874 return isl_basic_map_finalize(bmap
);
10876 isl_basic_map_free(bmap1
);
10877 isl_basic_map_free(bmap2
);
10881 __isl_give isl_basic_map
*isl_basic_map_range_product(
10882 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
10885 isl_space
*space_result
= NULL
;
10886 isl_basic_map
*bmap
;
10887 isl_size in
, out1
, out2
, nparam
;
10888 unsigned total
, pos
;
10889 struct isl_dim_map
*dim_map1
, *dim_map2
;
10891 rational
= isl_basic_map_is_rational(bmap1
);
10892 if (rational
>= 0 && rational
)
10893 rational
= isl_basic_map_is_rational(bmap2
);
10894 in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
10895 out1
= isl_basic_map_dim(bmap1
, isl_dim_out
);
10896 out2
= isl_basic_map_dim(bmap2
, isl_dim_out
);
10897 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
10898 if (in
< 0 || out1
< 0 || out2
< 0 || nparam
< 0 || rational
< 0)
10901 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
10904 space_result
= isl_space_range_product(isl_space_copy(bmap1
->dim
),
10905 isl_space_copy(bmap2
->dim
));
10907 total
= nparam
+ in
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
10908 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
10909 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
10910 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
10911 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
10912 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
10913 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
10914 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in
);
10915 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
10916 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
10917 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
10919 bmap
= isl_basic_map_alloc_space(space_result
,
10920 bmap1
->n_div
+ bmap2
->n_div
,
10921 bmap1
->n_eq
+ bmap2
->n_eq
,
10922 bmap1
->n_ineq
+ bmap2
->n_ineq
);
10923 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
10924 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
10926 bmap
= isl_basic_map_set_rational(bmap
);
10927 bmap
= isl_basic_map_simplify(bmap
);
10928 return isl_basic_map_finalize(bmap
);
10930 isl_basic_map_free(bmap1
);
10931 isl_basic_map_free(bmap2
);
10935 __isl_give isl_basic_map
*isl_basic_map_flat_range_product(
10936 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
10938 isl_basic_map
*prod
;
10940 prod
= isl_basic_map_range_product(bmap1
, bmap2
);
10941 prod
= isl_basic_map_flatten_range(prod
);
10945 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
10946 * and collect the results.
10947 * The result live in the space obtained by calling "space_product"
10948 * on the spaces of "map1" and "map2".
10949 * If "remove_duplicates" is set then the result may contain duplicates
10950 * (even if the inputs do not) and so we try and remove the obvious
10953 static __isl_give isl_map
*map_product(__isl_take isl_map
*map1
,
10954 __isl_take isl_map
*map2
,
10955 __isl_give isl_space
*(*space_product
)(__isl_take isl_space
*left
,
10956 __isl_take isl_space
*right
),
10957 __isl_give isl_basic_map
*(*basic_map_product
)(
10958 __isl_take isl_basic_map
*left
,
10959 __isl_take isl_basic_map
*right
),
10960 int remove_duplicates
)
10962 unsigned flags
= 0;
10963 struct isl_map
*result
;
10967 m
= isl_map_has_equal_params(map1
, map2
);
10971 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
10972 "parameters don't match", goto error
);
10974 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
10975 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
10976 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
10978 result
= isl_map_alloc_space(space_product(isl_space_copy(map1
->dim
),
10979 isl_space_copy(map2
->dim
)),
10980 map1
->n
* map2
->n
, flags
);
10983 for (i
= 0; i
< map1
->n
; ++i
)
10984 for (j
= 0; j
< map2
->n
; ++j
) {
10985 struct isl_basic_map
*part
;
10986 part
= basic_map_product(isl_basic_map_copy(map1
->p
[i
]),
10987 isl_basic_map_copy(map2
->p
[j
]));
10988 if (isl_basic_map_is_empty(part
))
10989 isl_basic_map_free(part
);
10991 result
= isl_map_add_basic_map(result
, part
);
10995 if (remove_duplicates
)
10996 result
= isl_map_remove_obvious_duplicates(result
);
10997 isl_map_free(map1
);
10998 isl_map_free(map2
);
11001 isl_map_free(map1
);
11002 isl_map_free(map2
);
11006 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
11008 __isl_give isl_map
*isl_map_product(__isl_take isl_map
*map1
,
11009 __isl_take isl_map
*map2
)
11011 isl_map_align_params_bin(&map1
, &map2
);
11012 return map_product(map1
, map2
, &isl_space_product
,
11013 &isl_basic_map_product
, 0);
11016 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
11018 __isl_give isl_map
*isl_map_flat_product(__isl_take isl_map
*map1
,
11019 __isl_take isl_map
*map2
)
11023 prod
= isl_map_product(map1
, map2
);
11024 prod
= isl_map_flatten(prod
);
11028 /* Given two set A and B, construct its Cartesian product A x B.
11030 __isl_give isl_set
*isl_set_product(__isl_take isl_set
*set1
,
11031 __isl_take isl_set
*set2
)
11033 return isl_map_range_product(set1
, set2
);
11036 __isl_give isl_set
*isl_set_flat_product(__isl_take isl_set
*set1
,
11037 __isl_take isl_set
*set2
)
11039 return isl_map_flat_range_product(set1
, set2
);
11042 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
11044 __isl_give isl_map
*isl_map_domain_product(__isl_take isl_map
*map1
,
11045 __isl_take isl_map
*map2
)
11047 isl_map_align_params_bin(&map1
, &map2
);
11048 return map_product(map1
, map2
, &isl_space_domain_product
,
11049 &isl_basic_map_domain_product
, 1);
11052 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
11054 __isl_give isl_map
*isl_map_range_product(__isl_take isl_map
*map1
,
11055 __isl_take isl_map
*map2
)
11057 isl_map_align_params_bin(&map1
, &map2
);
11058 return map_product(map1
, map2
, &isl_space_range_product
,
11059 &isl_basic_map_range_product
, 1);
11062 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
11064 __isl_give isl_map
*isl_map_factor_domain(__isl_take isl_map
*map
)
11067 isl_size total1
, keep1
, total2
, keep2
;
11069 total1
= isl_map_dim(map
, isl_dim_in
);
11070 total2
= isl_map_dim(map
, isl_dim_out
);
11071 if (total1
< 0 || total2
< 0)
11072 return isl_map_free(map
);
11073 if (!isl_space_domain_is_wrapping(map
->dim
) ||
11074 !isl_space_range_is_wrapping(map
->dim
))
11075 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
11076 "not a product", return isl_map_free(map
));
11078 space
= isl_map_get_space(map
);
11079 space
= isl_space_factor_domain(space
);
11080 keep1
= isl_space_dim(space
, isl_dim_in
);
11081 keep2
= isl_space_dim(space
, isl_dim_out
);
11082 if (keep1
< 0 || keep2
< 0)
11083 map
= isl_map_free(map
);
11084 map
= isl_map_project_out(map
, isl_dim_in
, keep1
, total1
- keep1
);
11085 map
= isl_map_project_out(map
, isl_dim_out
, keep2
, total2
- keep2
);
11086 map
= isl_map_reset_space(map
, space
);
11091 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
11093 __isl_give isl_map
*isl_map_factor_range(__isl_take isl_map
*map
)
11096 isl_size total1
, keep1
, total2
, keep2
;
11098 total1
= isl_map_dim(map
, isl_dim_in
);
11099 total2
= isl_map_dim(map
, isl_dim_out
);
11100 if (total1
< 0 || total2
< 0)
11101 return isl_map_free(map
);
11102 if (!isl_space_domain_is_wrapping(map
->dim
) ||
11103 !isl_space_range_is_wrapping(map
->dim
))
11104 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
11105 "not a product", return isl_map_free(map
));
11107 space
= isl_map_get_space(map
);
11108 space
= isl_space_factor_range(space
);
11109 keep1
= isl_space_dim(space
, isl_dim_in
);
11110 keep2
= isl_space_dim(space
, isl_dim_out
);
11111 if (keep1
< 0 || keep2
< 0)
11112 map
= isl_map_free(map
);
11113 map
= isl_map_project_out(map
, isl_dim_in
, 0, total1
- keep1
);
11114 map
= isl_map_project_out(map
, isl_dim_out
, 0, total2
- keep2
);
11115 map
= isl_map_reset_space(map
, space
);
11120 /* Given a map of the form [A -> B] -> C, return the map A -> C.
11122 __isl_give isl_map
*isl_map_domain_factor_domain(__isl_take isl_map
*map
)
11125 isl_size total
, keep
;
11127 total
= isl_map_dim(map
, isl_dim_in
);
11129 return isl_map_free(map
);
11130 if (!isl_space_domain_is_wrapping(map
->dim
))
11131 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
11132 "domain is not a product", return isl_map_free(map
));
11134 space
= isl_map_get_space(map
);
11135 space
= isl_space_domain_factor_domain(space
);
11136 keep
= isl_space_dim(space
, isl_dim_in
);
11138 map
= isl_map_free(map
);
11139 map
= isl_map_project_out(map
, isl_dim_in
, keep
, total
- keep
);
11140 map
= isl_map_reset_space(map
, space
);
11145 /* Given a map of the form [A -> B] -> C, return the map B -> C.
11147 __isl_give isl_map
*isl_map_domain_factor_range(__isl_take isl_map
*map
)
11150 isl_size total
, keep
;
11152 total
= isl_map_dim(map
, isl_dim_in
);
11154 return isl_map_free(map
);
11155 if (!isl_space_domain_is_wrapping(map
->dim
))
11156 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
11157 "domain is not a product", return isl_map_free(map
));
11159 space
= isl_map_get_space(map
);
11160 space
= isl_space_domain_factor_range(space
);
11161 keep
= isl_space_dim(space
, isl_dim_in
);
11163 map
= isl_map_free(map
);
11164 map
= isl_map_project_out(map
, isl_dim_in
, 0, total
- keep
);
11165 map
= isl_map_reset_space(map
, space
);
11170 /* Given a map A -> [B -> C], extract the map A -> B.
11172 __isl_give isl_map
*isl_map_range_factor_domain(__isl_take isl_map
*map
)
11175 isl_size total
, keep
;
11177 total
= isl_map_dim(map
, isl_dim_out
);
11179 return isl_map_free(map
);
11180 if (!isl_space_range_is_wrapping(map
->dim
))
11181 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
11182 "range is not a product", return isl_map_free(map
));
11184 space
= isl_map_get_space(map
);
11185 space
= isl_space_range_factor_domain(space
);
11186 keep
= isl_space_dim(space
, isl_dim_out
);
11188 map
= isl_map_free(map
);
11189 map
= isl_map_project_out(map
, isl_dim_out
, keep
, total
- keep
);
11190 map
= isl_map_reset_space(map
, space
);
11195 /* Given a map A -> [B -> C], extract the map A -> C.
11197 __isl_give isl_map
*isl_map_range_factor_range(__isl_take isl_map
*map
)
11200 isl_size total
, keep
;
11202 total
= isl_map_dim(map
, isl_dim_out
);
11204 return isl_map_free(map
);
11205 if (!isl_space_range_is_wrapping(map
->dim
))
11206 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
11207 "range is not a product", return isl_map_free(map
));
11209 space
= isl_map_get_space(map
);
11210 space
= isl_space_range_factor_range(space
);
11211 keep
= isl_space_dim(space
, isl_dim_out
);
11213 map
= isl_map_free(map
);
11214 map
= isl_map_project_out(map
, isl_dim_out
, 0, total
- keep
);
11215 map
= isl_map_reset_space(map
, space
);
11220 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
11222 __isl_give isl_map
*isl_map_flat_domain_product(__isl_take isl_map
*map1
,
11223 __isl_take isl_map
*map2
)
11227 prod
= isl_map_domain_product(map1
, map2
);
11228 prod
= isl_map_flatten_domain(prod
);
11232 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
11234 __isl_give isl_map
*isl_map_flat_range_product(__isl_take isl_map
*map1
,
11235 __isl_take isl_map
*map2
)
11239 prod
= isl_map_range_product(map1
, map2
);
11240 prod
= isl_map_flatten_range(prod
);
11244 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map
*bmap
)
11247 uint32_t hash
= isl_hash_init();
11252 bmap
= isl_basic_map_copy(bmap
);
11253 bmap
= isl_basic_map_normalize(bmap
);
11254 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
11257 isl_hash_byte(hash
, bmap
->n_eq
& 0xFF);
11258 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
11260 c_hash
= isl_seq_get_hash(bmap
->eq
[i
], 1 + total
);
11261 isl_hash_hash(hash
, c_hash
);
11263 isl_hash_byte(hash
, bmap
->n_ineq
& 0xFF);
11264 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
11266 c_hash
= isl_seq_get_hash(bmap
->ineq
[i
], 1 + total
);
11267 isl_hash_hash(hash
, c_hash
);
11269 isl_hash_byte(hash
, bmap
->n_div
& 0xFF);
11270 for (i
= 0; i
< bmap
->n_div
; ++i
) {
11272 if (isl_int_is_zero(bmap
->div
[i
][0]))
11274 isl_hash_byte(hash
, i
& 0xFF);
11275 c_hash
= isl_seq_get_hash(bmap
->div
[i
], 1 + 1 + total
);
11276 isl_hash_hash(hash
, c_hash
);
11278 isl_basic_map_free(bmap
);
11282 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set
*bset
)
11284 return isl_basic_map_get_hash(bset_to_bmap(bset
));
11287 uint32_t isl_map_get_hash(__isl_keep isl_map
*map
)
11294 map
= isl_map_copy(map
);
11295 map
= isl_map_normalize(map
);
11299 hash
= isl_hash_init();
11300 for (i
= 0; i
< map
->n
; ++i
) {
11301 uint32_t bmap_hash
;
11302 bmap_hash
= isl_basic_map_get_hash(map
->p
[i
]);
11303 isl_hash_hash(hash
, bmap_hash
);
11311 uint32_t isl_set_get_hash(__isl_keep isl_set
*set
)
11313 return isl_map_get_hash(set_to_map(set
));
11316 /* Return the number of basic maps in the (current) representation of "map".
11318 isl_size
isl_map_n_basic_map(__isl_keep isl_map
*map
)
11320 return map
? map
->n
: isl_size_error
;
11323 isl_size
isl_set_n_basic_set(__isl_keep isl_set
*set
)
11325 return set
? set
->n
: isl_size_error
;
11328 isl_stat
isl_map_foreach_basic_map(__isl_keep isl_map
*map
,
11329 isl_stat (*fn
)(__isl_take isl_basic_map
*bmap
, void *user
), void *user
)
11334 return isl_stat_error
;
11336 for (i
= 0; i
< map
->n
; ++i
)
11337 if (fn(isl_basic_map_copy(map
->p
[i
]), user
) < 0)
11338 return isl_stat_error
;
11340 return isl_stat_ok
;
11343 isl_stat
isl_set_foreach_basic_set(__isl_keep isl_set
*set
,
11344 isl_stat (*fn
)(__isl_take isl_basic_set
*bset
, void *user
), void *user
)
11349 return isl_stat_error
;
11351 for (i
= 0; i
< set
->n
; ++i
)
11352 if (fn(isl_basic_set_copy(set
->p
[i
]), user
) < 0)
11353 return isl_stat_error
;
11355 return isl_stat_ok
;
11358 /* Does "test" succeed on every basic set in "set"?
11360 isl_bool
isl_set_every_basic_set(__isl_keep isl_set
*set
,
11361 isl_bool (*test
)(__isl_keep isl_basic_set
*bset
, void *user
),
11367 return isl_bool_error
;
11369 for (i
= 0; i
< set
->n
; ++i
) {
11372 r
= test(set
->p
[i
], user
);
11377 return isl_bool_true
;
11380 /* Return a list of basic sets, the union of which is equal to "set".
11382 __isl_give isl_basic_set_list
*isl_set_get_basic_set_list(
11383 __isl_keep isl_set
*set
)
11386 isl_basic_set_list
*list
;
11391 list
= isl_basic_set_list_alloc(isl_set_get_ctx(set
), set
->n
);
11392 for (i
= 0; i
< set
->n
; ++i
) {
11393 isl_basic_set
*bset
;
11395 bset
= isl_basic_set_copy(set
->p
[i
]);
11396 list
= isl_basic_set_list_add(list
, bset
);
11402 __isl_give isl_basic_set
*isl_basic_set_lift(__isl_take isl_basic_set
*bset
)
11409 bset
= isl_basic_set_cow(bset
);
11413 space
= isl_basic_set_get_space(bset
);
11414 space
= isl_space_lift(space
, bset
->n_div
);
11417 isl_space_free(bset
->dim
);
11419 bset
->extra
-= bset
->n_div
;
11422 bset
= isl_basic_set_finalize(bset
);
11426 isl_basic_set_free(bset
);
11430 __isl_give isl_set
*isl_set_lift(__isl_take isl_set
*set
)
11436 set
= set_from_map(isl_map_align_divs_internal(set_to_map(set
)));
11441 set
= isl_set_cow(set
);
11445 n_div
= set
->p
[0]->n_div
;
11446 space
= isl_set_get_space(set
);
11447 space
= isl_space_lift(space
, n_div
);
11450 isl_space_free(set
->dim
);
11453 for (i
= 0; i
< set
->n
; ++i
) {
11454 set
->p
[i
] = isl_basic_set_lift(set
->p
[i
]);
11465 int isl_basic_set_size(__isl_keep isl_basic_set
*bset
)
11470 dim
= isl_basic_set_dim(bset
, isl_dim_all
);
11473 size
+= bset
->n_eq
* (1 + dim
);
11474 size
+= bset
->n_ineq
* (1 + dim
);
11475 size
+= bset
->n_div
* (2 + dim
);
11480 int isl_set_size(__isl_keep isl_set
*set
)
11488 for (i
= 0; i
< set
->n
; ++i
)
11489 size
+= isl_basic_set_size(set
->p
[i
]);
11494 /* Check if there is any lower bound (if lower == 0) and/or upper
11495 * bound (if upper == 0) on the specified dim.
11497 static isl_bool
basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
11498 enum isl_dim_type type
, unsigned pos
, int lower
, int upper
)
11502 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
11503 return isl_bool_error
;
11505 pos
+= isl_basic_map_offset(bmap
, type
);
11507 for (i
= 0; i
< bmap
->n_div
; ++i
) {
11508 if (isl_int_is_zero(bmap
->div
[i
][0]))
11510 if (!isl_int_is_zero(bmap
->div
[i
][1 + pos
]))
11511 return isl_bool_true
;
11514 for (i
= 0; i
< bmap
->n_eq
; ++i
)
11515 if (!isl_int_is_zero(bmap
->eq
[i
][pos
]))
11516 return isl_bool_true
;
11518 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
11519 int sgn
= isl_int_sgn(bmap
->ineq
[i
][pos
]);
11526 return lower
&& upper
;
11529 isl_bool
isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
11530 enum isl_dim_type type
, unsigned pos
)
11532 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 0);
11535 isl_bool
isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map
*bmap
,
11536 enum isl_dim_type type
, unsigned pos
)
11538 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 1);
11541 isl_bool
isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map
*bmap
,
11542 enum isl_dim_type type
, unsigned pos
)
11544 return basic_map_dim_is_bounded(bmap
, type
, pos
, 1, 0);
11547 isl_bool
isl_map_dim_is_bounded(__isl_keep isl_map
*map
,
11548 enum isl_dim_type type
, unsigned pos
)
11553 return isl_bool_error
;
11555 for (i
= 0; i
< map
->n
; ++i
) {
11557 bounded
= isl_basic_map_dim_is_bounded(map
->p
[i
], type
, pos
);
11558 if (bounded
< 0 || !bounded
)
11562 return isl_bool_true
;
11565 /* Return true if the specified dim is involved in both an upper bound
11566 * and a lower bound.
11568 isl_bool
isl_set_dim_is_bounded(__isl_keep isl_set
*set
,
11569 enum isl_dim_type type
, unsigned pos
)
11571 return isl_map_dim_is_bounded(set_to_map(set
), type
, pos
);
11574 /* Does "map" have a bound (according to "fn") for any of its basic maps?
11576 static isl_bool
has_any_bound(__isl_keep isl_map
*map
,
11577 enum isl_dim_type type
, unsigned pos
,
11578 isl_bool (*fn
)(__isl_keep isl_basic_map
*bmap
,
11579 enum isl_dim_type type
, unsigned pos
))
11584 return isl_bool_error
;
11586 for (i
= 0; i
< map
->n
; ++i
) {
11588 bounded
= fn(map
->p
[i
], type
, pos
);
11589 if (bounded
< 0 || bounded
)
11593 return isl_bool_false
;
11596 /* Return 1 if the specified dim is involved in any lower bound.
11598 isl_bool
isl_set_dim_has_any_lower_bound(__isl_keep isl_set
*set
,
11599 enum isl_dim_type type
, unsigned pos
)
11601 return has_any_bound(set
, type
, pos
,
11602 &isl_basic_map_dim_has_lower_bound
);
11605 /* Return 1 if the specified dim is involved in any upper bound.
11607 isl_bool
isl_set_dim_has_any_upper_bound(__isl_keep isl_set
*set
,
11608 enum isl_dim_type type
, unsigned pos
)
11610 return has_any_bound(set
, type
, pos
,
11611 &isl_basic_map_dim_has_upper_bound
);
11614 /* Does "map" have a bound (according to "fn") for all of its basic maps?
11616 static isl_bool
has_bound(__isl_keep isl_map
*map
,
11617 enum isl_dim_type type
, unsigned pos
,
11618 isl_bool (*fn
)(__isl_keep isl_basic_map
*bmap
,
11619 enum isl_dim_type type
, unsigned pos
))
11624 return isl_bool_error
;
11626 for (i
= 0; i
< map
->n
; ++i
) {
11628 bounded
= fn(map
->p
[i
], type
, pos
);
11629 if (bounded
< 0 || !bounded
)
11633 return isl_bool_true
;
11636 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
11638 isl_bool
isl_set_dim_has_lower_bound(__isl_keep isl_set
*set
,
11639 enum isl_dim_type type
, unsigned pos
)
11641 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_lower_bound
);
11644 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
11646 isl_bool
isl_set_dim_has_upper_bound(__isl_keep isl_set
*set
,
11647 enum isl_dim_type type
, unsigned pos
)
11649 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_upper_bound
);
11652 /* For each of the "n" variables starting at "first", determine
11653 * the sign of the variable and put the results in the first "n"
11654 * elements of the array "signs".
11656 * 1 means that the variable is non-negative
11657 * -1 means that the variable is non-positive
11658 * 0 means the variable attains both positive and negative values.
11660 isl_stat
isl_basic_set_vars_get_sign(__isl_keep isl_basic_set
*bset
,
11661 unsigned first
, unsigned n
, int *signs
)
11663 isl_vec
*bound
= NULL
;
11664 struct isl_tab
*tab
= NULL
;
11665 struct isl_tab_undo
*snap
;
11669 total
= isl_basic_set_dim(bset
, isl_dim_all
);
11670 if (total
< 0 || !signs
)
11671 return isl_stat_error
;
11673 bound
= isl_vec_alloc(bset
->ctx
, 1 + total
);
11674 tab
= isl_tab_from_basic_set(bset
, 0);
11675 if (!bound
|| !tab
)
11678 isl_seq_clr(bound
->el
, bound
->size
);
11679 isl_int_set_si(bound
->el
[0], -1);
11681 snap
= isl_tab_snap(tab
);
11682 for (i
= 0; i
< n
; ++i
) {
11685 isl_int_set_si(bound
->el
[1 + first
+ i
], -1);
11686 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
11688 empty
= tab
->empty
;
11689 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
11690 if (isl_tab_rollback(tab
, snap
) < 0)
11698 isl_int_set_si(bound
->el
[1 + first
+ i
], 1);
11699 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
11701 empty
= tab
->empty
;
11702 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
11703 if (isl_tab_rollback(tab
, snap
) < 0)
11706 signs
[i
] = empty
? -1 : 0;
11710 isl_vec_free(bound
);
11711 return isl_stat_ok
;
11714 isl_vec_free(bound
);
11715 return isl_stat_error
;
11718 isl_stat
isl_basic_set_dims_get_sign(__isl_keep isl_basic_set
*bset
,
11719 enum isl_dim_type type
, unsigned first
, unsigned n
, int *signs
)
11721 if (!bset
|| !signs
)
11722 return isl_stat_error
;
11723 if (isl_basic_set_check_range(bset
, type
, first
, n
) < 0)
11724 return isl_stat_error
;
11726 first
+= pos(bset
->dim
, type
) - 1;
11727 return isl_basic_set_vars_get_sign(bset
, first
, n
, signs
);
11730 /* Is it possible for the integer division "div" to depend (possibly
11731 * indirectly) on any output dimensions?
11733 * If the div is undefined, then we conservatively assume that it
11734 * may depend on them.
11735 * Otherwise, we check if it actually depends on them or on any integer
11736 * divisions that may depend on them.
11738 static isl_bool
div_may_involve_output(__isl_keep isl_basic_map
*bmap
, int div
)
11741 isl_size n_out
, n_div
;
11742 unsigned o_out
, o_div
;
11744 if (isl_int_is_zero(bmap
->div
[div
][0]))
11745 return isl_bool_true
;
11747 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
11749 return isl_bool_error
;
11750 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
11752 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + o_out
, n_out
) != -1)
11753 return isl_bool_true
;
11755 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
11757 return isl_bool_error
;
11758 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
11760 for (i
= 0; i
< n_div
; ++i
) {
11761 isl_bool may_involve
;
11763 if (isl_int_is_zero(bmap
->div
[div
][1 + o_div
+ i
]))
11765 may_involve
= div_may_involve_output(bmap
, i
);
11766 if (may_involve
< 0 || may_involve
)
11767 return may_involve
;
11770 return isl_bool_false
;
11773 /* Return the first integer division of "bmap" in the range
11774 * [first, first + n[ that may depend on any output dimensions and
11775 * that has a non-zero coefficient in "c" (where the first coefficient
11776 * in "c" corresponds to integer division "first").
11778 static int first_div_may_involve_output(__isl_keep isl_basic_map
*bmap
,
11779 isl_int
*c
, int first
, int n
)
11786 for (k
= first
; k
< first
+ n
; ++k
) {
11787 isl_bool may_involve
;
11789 if (isl_int_is_zero(c
[k
]))
11791 may_involve
= div_may_involve_output(bmap
, k
);
11792 if (may_involve
< 0)
11801 /* Look for a pair of inequality constraints in "bmap" of the form
11803 * -l + i >= 0 or i >= l
11805 * n + l - i >= 0 or i <= l + n
11807 * with n < "m" and i the output dimension at position "pos".
11808 * (Note that n >= 0 as otherwise the two constraints would conflict.)
11809 * Furthermore, "l" is only allowed to involve parameters, input dimensions
11810 * and earlier output dimensions, as well as integer divisions that do
11811 * not involve any of the output dimensions.
11813 * Return the index of the first inequality constraint or bmap->n_ineq
11814 * if no such pair can be found.
11816 static int find_modulo_constraint_pair(__isl_keep isl_basic_map
*bmap
,
11817 int pos
, isl_int m
)
11822 isl_size n_div
, n_out
;
11823 unsigned o_div
, o_out
;
11826 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
11827 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
11828 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
11829 if (total
< 0 || n_out
< 0 || n_div
< 0)
11832 ctx
= isl_basic_map_get_ctx(bmap
);
11833 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
11834 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
11835 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
11836 if (!isl_int_abs_eq(bmap
->ineq
[i
][o_out
+ pos
], ctx
->one
))
11838 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + o_out
+ pos
+ 1,
11839 n_out
- (pos
+ 1)) != -1)
11841 if (first_div_may_involve_output(bmap
, bmap
->ineq
[i
] + o_div
,
11844 for (j
= i
+ 1; j
< bmap
->n_ineq
; ++j
) {
11845 if (!isl_int_abs_eq(bmap
->ineq
[j
][o_out
+ pos
],
11848 if (!isl_seq_is_neg(bmap
->ineq
[i
] + 1,
11849 bmap
->ineq
[j
] + 1, total
))
11853 if (j
>= bmap
->n_ineq
)
11855 isl_int_add(bmap
->ineq
[i
][0],
11856 bmap
->ineq
[i
][0], bmap
->ineq
[j
][0]);
11857 less
= isl_int_abs_lt(bmap
->ineq
[i
][0], m
);
11858 isl_int_sub(bmap
->ineq
[i
][0],
11859 bmap
->ineq
[i
][0], bmap
->ineq
[j
][0]);
11862 if (isl_int_is_one(bmap
->ineq
[i
][o_out
+ pos
]))
11868 return bmap
->n_ineq
;
11871 /* Return the index of the equality of "bmap" that defines
11872 * the output dimension "pos" in terms of earlier dimensions.
11873 * The equality may also involve integer divisions, as long
11874 * as those integer divisions are defined in terms of
11875 * parameters or input dimensions.
11876 * In this case, *div is set to the number of integer divisions and
11877 * *ineq is set to the number of inequality constraints (provided
11878 * div and ineq are not NULL).
11880 * The equality may also involve a single integer division involving
11881 * the output dimensions (typically only output dimension "pos") as
11882 * long as the coefficient of output dimension "pos" is 1 or -1 and
11883 * there is a pair of constraints i >= l and i <= l + n, with i referring
11884 * to output dimension "pos", l an expression involving only earlier
11885 * dimensions and n smaller than the coefficient of the integer division
11886 * in the equality. In this case, the output dimension can be defined
11887 * in terms of a modulo expression that does not involve the integer division.
11888 * *div is then set to this single integer division and
11889 * *ineq is set to the index of constraint i >= l.
11891 * Return bmap->n_eq if there is no such equality.
11892 * Return -1 on error.
11894 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map
*bmap
,
11895 int pos
, int *div
, int *ineq
)
11898 isl_size n_div
, n_out
;
11899 unsigned o_div
, o_out
;
11901 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
11902 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
11903 if (n_out
< 0 || n_div
< 0)
11906 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
11907 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
11910 *ineq
= bmap
->n_ineq
;
11913 for (j
= 0; j
< bmap
->n_eq
; ++j
) {
11914 if (isl_int_is_zero(bmap
->eq
[j
][o_out
+ pos
]))
11916 if (isl_seq_first_non_zero(bmap
->eq
[j
] + o_out
+ pos
+ 1,
11917 n_out
- (pos
+ 1)) != -1)
11919 k
= first_div_may_involve_output(bmap
, bmap
->eq
[j
] + o_div
,
11923 if (!isl_int_is_one(bmap
->eq
[j
][o_out
+ pos
]) &&
11924 !isl_int_is_negone(bmap
->eq
[j
][o_out
+ pos
]))
11926 if (first_div_may_involve_output(bmap
, bmap
->eq
[j
] + o_div
,
11927 k
+ 1, n_div
- (k
+1)) < n_div
)
11929 l
= find_modulo_constraint_pair(bmap
, pos
,
11930 bmap
->eq
[j
][o_div
+ k
]);
11933 if (l
>= bmap
->n_ineq
)
11945 /* Check if the given basic map is obviously single-valued.
11946 * In particular, for each output dimension, check that there is
11947 * an equality that defines the output dimension in terms of
11948 * earlier dimensions.
11950 isl_bool
isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map
*bmap
)
11955 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
11957 return isl_bool_error
;
11959 for (i
= 0; i
< n_out
; ++i
) {
11962 eq
= isl_basic_map_output_defining_equality(bmap
, i
,
11965 return isl_bool_error
;
11966 if (eq
>= bmap
->n_eq
)
11967 return isl_bool_false
;
11970 return isl_bool_true
;
11973 /* Check if the given basic map is single-valued.
11974 * We simply compute
11978 * and check if the result is a subset of the identity mapping.
11980 isl_bool
isl_basic_map_is_single_valued(__isl_keep isl_basic_map
*bmap
)
11983 isl_basic_map
*test
;
11987 sv
= isl_basic_map_plain_is_single_valued(bmap
);
11991 test
= isl_basic_map_reverse(isl_basic_map_copy(bmap
));
11992 test
= isl_basic_map_apply_range(test
, isl_basic_map_copy(bmap
));
11994 space
= isl_basic_map_get_space(bmap
);
11995 space
= isl_space_map_from_set(isl_space_range(space
));
11996 id
= isl_basic_map_identity(space
);
11998 sv
= isl_basic_map_is_subset(test
, id
);
12000 isl_basic_map_free(test
);
12001 isl_basic_map_free(id
);
12006 /* Check if the given map is obviously single-valued.
12008 isl_bool
isl_map_plain_is_single_valued(__isl_keep isl_map
*map
)
12011 return isl_bool_error
;
12013 return isl_bool_true
;
12015 return isl_bool_false
;
12017 return isl_basic_map_plain_is_single_valued(map
->p
[0]);
12020 /* Check if the given map is single-valued.
12021 * We simply compute
12025 * and check if the result is a subset of the identity mapping.
12027 isl_bool
isl_map_is_single_valued(__isl_keep isl_map
*map
)
12034 sv
= isl_map_plain_is_single_valued(map
);
12038 test
= isl_map_reverse(isl_map_copy(map
));
12039 test
= isl_map_apply_range(test
, isl_map_copy(map
));
12041 space
= isl_space_map_from_set(isl_space_range(isl_map_get_space(map
)));
12042 id
= isl_map_identity(space
);
12044 sv
= isl_map_is_subset(test
, id
);
12046 isl_map_free(test
);
12052 isl_bool
isl_map_is_injective(__isl_keep isl_map
*map
)
12056 map
= isl_map_copy(map
);
12057 map
= isl_map_reverse(map
);
12058 in
= isl_map_is_single_valued(map
);
12064 /* Check if the given map is obviously injective.
12066 isl_bool
isl_map_plain_is_injective(__isl_keep isl_map
*map
)
12070 map
= isl_map_copy(map
);
12071 map
= isl_map_reverse(map
);
12072 in
= isl_map_plain_is_single_valued(map
);
12078 isl_bool
isl_map_is_bijective(__isl_keep isl_map
*map
)
12082 sv
= isl_map_is_single_valued(map
);
12086 return isl_map_is_injective(map
);
12089 isl_bool
isl_set_is_singleton(__isl_keep isl_set
*set
)
12091 return isl_map_is_single_valued(set_to_map(set
));
12094 /* Does "map" only map elements to themselves?
12096 * If the domain and range spaces are different, then "map"
12097 * is considered not to be an identity relation, even if it is empty.
12098 * Otherwise, construct the maximal identity relation and
12099 * check whether "map" is a subset of this relation.
12101 isl_bool
isl_map_is_identity(__isl_keep isl_map
*map
)
12104 isl_bool equal
, is_identity
;
12106 equal
= isl_map_tuple_is_equal(map
, isl_dim_in
, map
, isl_dim_out
);
12107 if (equal
< 0 || !equal
)
12110 id
= isl_map_identity(isl_map_get_space(map
));
12111 is_identity
= isl_map_is_subset(map
, id
);
12114 return is_identity
;
12117 int isl_map_is_translation(__isl_keep isl_map
*map
)
12122 delta
= isl_map_deltas(isl_map_copy(map
));
12123 ok
= isl_set_is_singleton(delta
);
12124 isl_set_free(delta
);
12129 static int unique(isl_int
*p
, unsigned pos
, unsigned len
)
12131 if (isl_seq_first_non_zero(p
, pos
) != -1)
12133 if (isl_seq_first_non_zero(p
+ pos
+ 1, len
- pos
- 1) != -1)
12138 isl_bool
isl_basic_set_is_box(__isl_keep isl_basic_set
*bset
)
12141 isl_size nvar
, n_div
;
12144 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
12146 return isl_bool_error
;
12148 return isl_bool_false
;
12150 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
12152 return isl_bool_error
;
12153 ovar
= isl_space_offset(bset
->dim
, isl_dim_set
);
12154 for (j
= 0; j
< nvar
; ++j
) {
12155 int lower
= 0, upper
= 0;
12156 for (i
= 0; i
< bset
->n_eq
; ++i
) {
12157 if (isl_int_is_zero(bset
->eq
[i
][1 + ovar
+ j
]))
12159 if (!unique(bset
->eq
[i
] + 1 + ovar
, j
, nvar
))
12160 return isl_bool_false
;
12163 if (i
< bset
->n_eq
)
12165 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
12166 if (isl_int_is_zero(bset
->ineq
[i
][1 + ovar
+ j
]))
12168 if (!unique(bset
->ineq
[i
] + 1 + ovar
, j
, nvar
))
12169 return isl_bool_false
;
12170 if (isl_int_is_pos(bset
->ineq
[i
][1 + ovar
+ j
]))
12175 if (!lower
|| !upper
)
12176 return isl_bool_false
;
12179 return isl_bool_true
;
12182 isl_bool
isl_set_is_box(__isl_keep isl_set
*set
)
12185 return isl_bool_error
;
12187 return isl_bool_false
;
12189 return isl_basic_set_is_box(set
->p
[0]);
12192 isl_bool
isl_basic_set_is_wrapping(__isl_keep isl_basic_set
*bset
)
12195 return isl_bool_error
;
12197 return isl_space_is_wrapping(bset
->dim
);
12200 isl_bool
isl_set_is_wrapping(__isl_keep isl_set
*set
)
12203 return isl_bool_error
;
12205 return isl_space_is_wrapping(set
->dim
);
12208 /* Modify the space of "map" through a call to "change".
12209 * If "can_change" is set (not NULL), then first call it to check
12210 * if the modification is allowed, printing the error message "cannot_change"
12213 static __isl_give isl_map
*isl_map_change_space(__isl_take isl_map
*map
,
12214 isl_bool (*can_change
)(__isl_keep isl_map
*map
),
12215 const char *cannot_change
,
12216 __isl_give isl_space
*(*change
)(__isl_take isl_space
*space
))
12224 ok
= can_change
? can_change(map
) : isl_bool_true
;
12226 return isl_map_free(map
);
12228 isl_die(isl_map_get_ctx(map
), isl_error_invalid
, cannot_change
,
12229 return isl_map_free(map
));
12231 space
= change(isl_map_get_space(map
));
12232 map
= isl_map_reset_space(map
, space
);
12237 /* Is the domain of "map" a wrapped relation?
12239 isl_bool
isl_map_domain_is_wrapping(__isl_keep isl_map
*map
)
12242 return isl_bool_error
;
12244 return isl_space_domain_is_wrapping(map
->dim
);
12247 /* Does "map" have a wrapped relation in both domain and range?
12249 isl_bool
isl_map_is_product(__isl_keep isl_map
*map
)
12251 return isl_space_is_product(isl_map_peek_space(map
));
12254 /* Is the range of "map" a wrapped relation?
12256 isl_bool
isl_map_range_is_wrapping(__isl_keep isl_map
*map
)
12259 return isl_bool_error
;
12261 return isl_space_range_is_wrapping(map
->dim
);
12264 __isl_give isl_basic_set
*isl_basic_map_wrap(__isl_take isl_basic_map
*bmap
)
12268 space
= isl_basic_map_take_space(bmap
);
12269 space
= isl_space_wrap(space
);
12270 bmap
= isl_basic_map_restore_space(bmap
, space
);
12272 bmap
= isl_basic_map_finalize(bmap
);
12274 return bset_from_bmap(bmap
);
12277 /* Given a map A -> B, return the set (A -> B).
12279 __isl_give isl_set
*isl_map_wrap(__isl_take isl_map
*map
)
12281 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_wrap
);
12284 __isl_give isl_basic_map
*isl_basic_set_unwrap(__isl_take isl_basic_set
*bset
)
12286 bset
= isl_basic_set_cow(bset
);
12290 bset
->dim
= isl_space_unwrap(bset
->dim
);
12294 bset
= isl_basic_set_finalize(bset
);
12296 return bset_to_bmap(bset
);
12298 isl_basic_set_free(bset
);
12302 /* Given a set (A -> B), return the map A -> B.
12303 * Error out if "set" is not of the form (A -> B).
12305 __isl_give isl_map
*isl_set_unwrap(__isl_take isl_set
*set
)
12307 return isl_map_change_space(set
, &isl_set_is_wrapping
,
12308 "not a wrapping set", &isl_space_unwrap
);
12311 __isl_give isl_basic_map
*isl_basic_map_reset(__isl_take isl_basic_map
*bmap
,
12312 enum isl_dim_type type
)
12316 space
= isl_basic_map_take_space(bmap
);
12317 space
= isl_space_reset(space
, type
);
12318 bmap
= isl_basic_map_restore_space(bmap
, space
);
12320 bmap
= isl_basic_map_mark_final(bmap
);
12325 __isl_give isl_map
*isl_map_reset(__isl_take isl_map
*map
,
12326 enum isl_dim_type type
)
12334 if (!isl_space_is_named_or_nested(map
->dim
, type
))
12337 map
= isl_map_cow(map
);
12341 for (i
= 0; i
< map
->n
; ++i
) {
12342 map
->p
[i
] = isl_basic_map_reset(map
->p
[i
], type
);
12347 space
= isl_map_take_space(map
);
12348 space
= isl_space_reset(space
, type
);
12349 map
= isl_map_restore_space(map
, space
);
12357 __isl_give isl_basic_map
*isl_basic_map_flatten(__isl_take isl_basic_map
*bmap
)
12361 space
= isl_basic_map_take_space(bmap
);
12362 space
= isl_space_flatten(space
);
12363 bmap
= isl_basic_map_restore_space(bmap
, space
);
12365 bmap
= isl_basic_map_mark_final(bmap
);
12370 __isl_give isl_basic_set
*isl_basic_set_flatten(__isl_take isl_basic_set
*bset
)
12372 return bset_from_bmap(isl_basic_map_flatten(bset_to_bmap(bset
)));
12375 __isl_give isl_basic_map
*isl_basic_map_flatten_domain(
12376 __isl_take isl_basic_map
*bmap
)
12380 space
= isl_basic_map_take_space(bmap
);
12381 space
= isl_space_flatten_domain(space
);
12382 bmap
= isl_basic_map_restore_space(bmap
, space
);
12384 bmap
= isl_basic_map_mark_final(bmap
);
12389 __isl_give isl_basic_map
*isl_basic_map_flatten_range(
12390 __isl_take isl_basic_map
*bmap
)
12394 space
= isl_basic_map_take_space(bmap
);
12395 space
= isl_space_flatten_range(space
);
12396 bmap
= isl_basic_map_restore_space(bmap
, space
);
12398 bmap
= isl_basic_map_mark_final(bmap
);
12403 /* Remove any internal structure from the spaces of domain and range of "map".
12405 __isl_give isl_map
*isl_map_flatten(__isl_take isl_map
*map
)
12410 if (!map
->dim
->nested
[0] && !map
->dim
->nested
[1])
12413 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten
);
12416 __isl_give isl_set
*isl_set_flatten(__isl_take isl_set
*set
)
12418 return set_from_map(isl_map_flatten(set_to_map(set
)));
12421 __isl_give isl_map
*isl_set_flatten_map(__isl_take isl_set
*set
)
12423 isl_space
*space
, *flat_space
;
12426 space
= isl_set_get_space(set
);
12427 flat_space
= isl_space_flatten(isl_space_copy(space
));
12428 map
= isl_map_identity(isl_space_join(isl_space_reverse(space
),
12430 map
= isl_map_intersect_domain(map
, set
);
12435 /* Remove any internal structure from the space of the domain of "map".
12437 __isl_give isl_map
*isl_map_flatten_domain(__isl_take isl_map
*map
)
12442 if (!map
->dim
->nested
[0])
12445 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten_domain
);
12448 /* Remove any internal structure from the space of the range of "map".
12450 __isl_give isl_map
*isl_map_flatten_range(__isl_take isl_map
*map
)
12455 if (!map
->dim
->nested
[1])
12458 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten_range
);
12461 /* Reorder the dimensions of "bmap" according to the given dim_map
12462 * and set the dimension specification to "space" and
12463 * perform Gaussian elimination on the result.
12465 __isl_give isl_basic_map
*isl_basic_map_realign(__isl_take isl_basic_map
*bmap
,
12466 __isl_take isl_space
*space
, __isl_take
struct isl_dim_map
*dim_map
)
12468 isl_basic_map
*res
;
12472 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
12473 if (n_div
< 0 || !space
|| !dim_map
)
12476 flags
= bmap
->flags
;
12477 ISL_FL_CLR(flags
, ISL_BASIC_MAP_FINAL
);
12478 ISL_FL_CLR(flags
, ISL_BASIC_MAP_SORTED
);
12479 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
12480 res
= isl_basic_map_alloc_space(space
, n_div
, bmap
->n_eq
, bmap
->n_ineq
);
12481 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
12483 res
->flags
= flags
;
12484 res
= isl_basic_map_gauss(res
, NULL
);
12485 res
= isl_basic_map_finalize(res
);
12488 isl_dim_map_free(dim_map
);
12489 isl_basic_map_free(bmap
);
12490 isl_space_free(space
);
12494 /* Reorder the dimensions of "map" according to given reordering.
12496 __isl_give isl_map
*isl_map_realign(__isl_take isl_map
*map
,
12497 __isl_take isl_reordering
*r
)
12500 struct isl_dim_map
*dim_map
;
12502 map
= isl_map_cow(map
);
12503 dim_map
= isl_dim_map_from_reordering(r
);
12504 if (!map
|| !r
|| !dim_map
)
12507 for (i
= 0; i
< map
->n
; ++i
) {
12508 struct isl_dim_map
*dim_map_i
;
12511 dim_map_i
= isl_dim_map_extend(dim_map
, map
->p
[i
]);
12513 space
= isl_reordering_get_space(r
);
12514 map
->p
[i
] = isl_basic_map_realign(map
->p
[i
], space
, dim_map_i
);
12520 map
= isl_map_reset_space(map
, isl_reordering_get_space(r
));
12521 map
= isl_map_unmark_normalized(map
);
12523 isl_reordering_free(r
);
12524 isl_dim_map_free(dim_map
);
12527 isl_dim_map_free(dim_map
);
12529 isl_reordering_free(r
);
12533 __isl_give isl_set
*isl_set_realign(__isl_take isl_set
*set
,
12534 __isl_take isl_reordering
*r
)
12536 return set_from_map(isl_map_realign(set_to_map(set
), r
));
12539 __isl_give isl_map
*isl_map_align_params(__isl_take isl_map
*map
,
12540 __isl_take isl_space
*model
)
12545 if (!map
|| !model
)
12548 ctx
= isl_space_get_ctx(model
);
12549 if (!isl_space_has_named_params(model
))
12550 isl_die(ctx
, isl_error_invalid
,
12551 "model has unnamed parameters", goto error
);
12552 if (isl_map_check_named_params(map
) < 0)
12554 aligned
= isl_map_space_has_equal_params(map
, model
);
12559 isl_reordering
*exp
;
12561 space
= isl_map_peek_space(map
);
12562 exp
= isl_parameter_alignment_reordering(space
, model
);
12563 map
= isl_map_realign(map
, exp
);
12566 isl_space_free(model
);
12569 isl_space_free(model
);
12574 __isl_give isl_set
*isl_set_align_params(__isl_take isl_set
*set
,
12575 __isl_take isl_space
*model
)
12577 return isl_map_align_params(set
, model
);
12580 /* Align the parameters of "bmap" to those of "model", introducing
12581 * additional parameters if needed.
12583 __isl_give isl_basic_map
*isl_basic_map_align_params(
12584 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*model
)
12587 isl_bool equal_params
;
12588 isl_space
*bmap_space
;
12590 if (!bmap
|| !model
)
12593 ctx
= isl_space_get_ctx(model
);
12594 if (!isl_space_has_named_params(model
))
12595 isl_die(ctx
, isl_error_invalid
,
12596 "model has unnamed parameters", goto error
);
12597 if (isl_basic_map_check_named_params(bmap
) < 0)
12599 bmap_space
= isl_basic_map_peek_space(bmap
);
12600 equal_params
= isl_space_has_equal_params(bmap_space
, model
);
12601 if (equal_params
< 0)
12603 if (!equal_params
) {
12604 isl_reordering
*exp
;
12605 struct isl_dim_map
*dim_map
;
12607 exp
= isl_parameter_alignment_reordering(bmap_space
, model
);
12608 dim_map
= isl_dim_map_from_reordering(exp
);
12609 bmap
= isl_basic_map_realign(bmap
,
12610 isl_reordering_get_space(exp
),
12611 isl_dim_map_extend(dim_map
, bmap
));
12612 isl_reordering_free(exp
);
12613 isl_dim_map_free(dim_map
);
12616 isl_space_free(model
);
12619 isl_space_free(model
);
12620 isl_basic_map_free(bmap
);
12624 /* Do "bset" and "space" have the same parameters?
12626 isl_bool
isl_basic_set_space_has_equal_params(__isl_keep isl_basic_set
*bset
,
12627 __isl_keep isl_space
*space
)
12629 isl_space
*bset_space
;
12631 bset_space
= isl_basic_set_peek_space(bset
);
12632 return isl_space_has_equal_params(bset_space
, space
);
12635 /* Do "map" and "space" have the same parameters?
12637 isl_bool
isl_map_space_has_equal_params(__isl_keep isl_map
*map
,
12638 __isl_keep isl_space
*space
)
12640 isl_space
*map_space
;
12642 map_space
= isl_map_peek_space(map
);
12643 return isl_space_has_equal_params(map_space
, space
);
12646 /* Do "set" and "space" have the same parameters?
12648 isl_bool
isl_set_space_has_equal_params(__isl_keep isl_set
*set
,
12649 __isl_keep isl_space
*space
)
12651 return isl_map_space_has_equal_params(set_to_map(set
), space
);
12654 /* Align the parameters of "bset" to those of "model", introducing
12655 * additional parameters if needed.
12657 __isl_give isl_basic_set
*isl_basic_set_align_params(
12658 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*model
)
12660 return isl_basic_map_align_params(bset
, model
);
12664 #define TYPE isl_map
12665 #define isl_map_drop_dims isl_map_drop
12666 #include "isl_drop_unused_params_templ.c"
12668 /* Drop all parameters not referenced by "set".
12670 __isl_give isl_set
*isl_set_drop_unused_params(
12671 __isl_take isl_set
*set
)
12673 return set_from_map(isl_map_drop_unused_params(set_to_map(set
)));
12677 #define TYPE isl_basic_map
12678 #define isl_basic_map_drop_dims isl_basic_map_drop
12679 #include "isl_drop_unused_params_templ.c"
12681 /* Drop all parameters not referenced by "bset".
12683 __isl_give isl_basic_set
*isl_basic_set_drop_unused_params(
12684 __isl_take isl_basic_set
*bset
)
12686 return bset_from_bmap(isl_basic_map_drop_unused_params(
12687 bset_to_bmap(bset
)));
12690 /* Given a tuple of identifiers "tuple" in a space that corresponds
12691 * to that of "set", if any of those identifiers appear as parameters
12692 * in "set", then equate those parameters with the corresponding
12693 * set dimensions and project out the parameters.
12694 * The result therefore has no such parameters.
12696 static __isl_give isl_set
*equate_params(__isl_take isl_set
*set
,
12697 __isl_keep isl_multi_id
*tuple
)
12701 isl_space
*set_space
, *tuple_space
;
12703 set_space
= isl_set_peek_space(set
);
12704 tuple_space
= isl_multi_id_peek_space(tuple
);
12705 if (isl_space_check_equal_tuples(tuple_space
, set_space
) < 0)
12706 return isl_set_free(set
);
12707 n
= isl_multi_id_size(tuple
);
12709 return isl_set_free(set
);
12710 for (i
= 0; i
< n
; ++i
) {
12714 id
= isl_multi_id_get_at(tuple
, i
);
12716 return isl_set_free(set
);
12717 pos
= isl_set_find_dim_by_id(set
, isl_dim_param
, id
);
12721 set
= isl_set_equate(set
, isl_dim_param
, pos
, isl_dim_set
, i
);
12722 set
= isl_set_project_out(set
, isl_dim_param
, pos
, 1);
12727 /* Bind the set dimensions of "set" to parameters with identifiers
12728 * specified by "tuple", living in the same space as "set".
12730 * If no parameters with these identifiers appear in "set" already,
12731 * then the set dimensions are simply reinterpreted as parameters.
12732 * Otherwise, the parameters are first equated to the corresponding
12735 __isl_give isl_set
*isl_set_bind(__isl_take isl_set
*set
,
12736 __isl_take isl_multi_id
*tuple
)
12740 set
= equate_params(set
, tuple
);
12741 space
= isl_set_get_space(set
);
12742 space
= isl_space_bind_set(space
, tuple
);
12743 isl_multi_id_free(tuple
);
12744 set
= isl_set_reset_space(set
, space
);
12749 /* Given a tuple of identifiers "tuple" in a space that corresponds
12750 * to the domain of "map", if any of those identifiers appear as parameters
12751 * in "map", then equate those parameters with the corresponding
12752 * input dimensions and project out the parameters.
12753 * The result therefore has no such parameters.
12755 static __isl_give isl_map
*map_equate_params(__isl_take isl_map
*map
,
12756 __isl_keep isl_multi_id
*tuple
)
12760 isl_space
*map_space
, *tuple_space
;
12762 map_space
= isl_map_peek_space(map
);
12763 tuple_space
= isl_multi_id_peek_space(tuple
);
12764 if (isl_space_check_domain_tuples(tuple_space
, map_space
) < 0)
12765 return isl_map_free(map
);
12766 n
= isl_multi_id_size(tuple
);
12768 return isl_map_free(map
);
12769 for (i
= 0; i
< n
; ++i
) {
12773 id
= isl_multi_id_get_at(tuple
, i
);
12775 return isl_map_free(map
);
12776 pos
= isl_map_find_dim_by_id(map
, isl_dim_param
, id
);
12780 map
= isl_map_equate(map
, isl_dim_param
, pos
, isl_dim_in
, i
);
12781 map
= isl_map_project_out(map
, isl_dim_param
, pos
, 1);
12786 /* Bind the input dimensions of "map" to parameters with identifiers
12787 * specified by "tuple", living in the domain space of "map".
12789 * If no parameters with these identifiers appear in "map" already,
12790 * then the input dimensions are simply reinterpreted as parameters.
12791 * Otherwise, the parameters are first equated to the corresponding
12792 * input dimensions.
12794 __isl_give isl_set
*isl_map_bind_domain(__isl_take isl_map
*map
,
12795 __isl_take isl_multi_id
*tuple
)
12800 map
= map_equate_params(map
, tuple
);
12801 space
= isl_map_get_space(map
);
12802 space
= isl_space_bind_map_domain(space
, tuple
);
12803 isl_multi_id_free(tuple
);
12804 set
= set_from_map(isl_map_reset_space(map
, space
));
12809 /* Bind the output dimensions of "map" to parameters with identifiers
12810 * specified by "tuple", living in the range space of "map".
12812 * Since binding is more easily implemented on the domain,
12813 * bind the input dimensions of the inverse of "map".
12815 __isl_give isl_set
*isl_map_bind_range(__isl_take isl_map
*map
,
12816 __isl_take isl_multi_id
*tuple
)
12818 return isl_map_bind_domain(isl_map_reverse(map
), tuple
);
12821 /* Insert a domain corresponding to "tuple"
12822 * into the nullary or unary relation "set".
12823 * The result has an extra initial tuple and is therefore
12824 * either a unary or binary relation.
12825 * Any parameters with identifiers in "tuple" are reinterpreted
12826 * as the corresponding domain dimensions.
12828 static __isl_give isl_map
*unbind_params_insert_domain(
12829 __isl_take isl_set
*set
, __isl_take isl_multi_id
*tuple
)
12834 space
= isl_set_peek_space(set
);
12835 r
= isl_reordering_unbind_params_insert_domain(space
, tuple
);
12836 isl_multi_id_free(tuple
);
12838 return isl_map_realign(set_to_map(set
), r
);
12841 /* Construct a set with "tuple" as domain from the parameter domain "set".
12842 * Any parameters with identifiers in "tuple" are reinterpreted
12843 * as the corresponding set dimensions.
12845 __isl_give isl_set
*isl_set_unbind_params(__isl_take isl_set
*set
,
12846 __isl_take isl_multi_id
*tuple
)
12848 isl_bool is_params
;
12850 is_params
= isl_set_is_params(set
);
12852 set
= isl_set_free(set
);
12853 else if (!is_params
)
12854 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
12855 "expecting parameter domain", set
= isl_set_free(set
));
12856 return set_from_map(unbind_params_insert_domain(set
, tuple
));
12859 /* Check that "set" is a proper set, i.e., that it is not a parameter domain.
12861 static isl_stat
isl_set_check_is_set(__isl_keep isl_set
*set
)
12863 isl_bool is_params
;
12865 is_params
= isl_set_is_params(set
);
12867 return isl_stat_error
;
12868 else if (is_params
)
12869 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
12870 "expecting proper set", return isl_stat_error
);
12872 return isl_stat_ok
;
12875 /* Construct a map with "domain" as domain and "set" as range.
12876 * Any parameters with identifiers in "domain" are reinterpreted
12877 * as the corresponding domain dimensions.
12879 __isl_give isl_map
*isl_set_unbind_params_insert_domain(
12880 __isl_take isl_set
*set
, __isl_take isl_multi_id
*domain
)
12882 if (isl_set_check_is_set(set
) < 0)
12883 set
= isl_set_free(set
);
12884 return unbind_params_insert_domain(set
, domain
);
12887 /* Construct a map with "domain" as domain and "set" as range.
12889 __isl_give isl_map
*isl_set_insert_domain(__isl_take isl_set
*set
,
12890 __isl_take isl_space
*domain
)
12896 if (isl_set_check_is_set(set
) < 0 || isl_space_check_is_set(domain
) < 0)
12897 domain
= isl_space_free(domain
);
12898 dim
= isl_space_dim(domain
, isl_dim_set
);
12900 domain
= isl_space_free(domain
);
12901 space
= isl_set_get_space(set
);
12902 domain
= isl_space_replace_params(domain
, space
);
12903 space
= isl_space_map_from_domain_and_range(domain
, space
);
12905 map
= isl_map_from_range(set
);
12906 map
= isl_map_add_dims(map
, isl_dim_in
, dim
);
12907 map
= isl_map_reset_space(map
, space
);
12912 __isl_give isl_mat
*isl_basic_map_equalities_matrix(
12913 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
12914 enum isl_dim_type c2
, enum isl_dim_type c3
,
12915 enum isl_dim_type c4
, enum isl_dim_type c5
)
12917 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
12918 struct isl_mat
*mat
;
12923 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
12926 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_eq
, total
+ 1);
12929 for (i
= 0; i
< bmap
->n_eq
; ++i
)
12930 for (j
= 0, pos
= 0; j
< 5; ++j
) {
12931 int off
= isl_basic_map_offset(bmap
, c
[j
]);
12932 isl_size dim
= isl_basic_map_dim(bmap
, c
[j
]);
12934 return isl_mat_free(mat
);
12935 for (k
= 0; k
< dim
; ++k
) {
12936 isl_int_set(mat
->row
[i
][pos
],
12937 bmap
->eq
[i
][off
+ k
]);
12945 __isl_give isl_mat
*isl_basic_map_inequalities_matrix(
12946 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
12947 enum isl_dim_type c2
, enum isl_dim_type c3
,
12948 enum isl_dim_type c4
, enum isl_dim_type c5
)
12950 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
12951 struct isl_mat
*mat
;
12956 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
12959 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_ineq
, total
+ 1);
12962 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
12963 for (j
= 0, pos
= 0; j
< 5; ++j
) {
12964 int off
= isl_basic_map_offset(bmap
, c
[j
]);
12965 isl_size dim
= isl_basic_map_dim(bmap
, c
[j
]);
12967 return isl_mat_free(mat
);
12968 for (k
= 0; k
< dim
; ++k
) {
12969 isl_int_set(mat
->row
[i
][pos
],
12970 bmap
->ineq
[i
][off
+ k
]);
12978 __isl_give isl_basic_map
*isl_basic_map_from_constraint_matrices(
12979 __isl_take isl_space
*space
,
12980 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
12981 enum isl_dim_type c2
, enum isl_dim_type c3
,
12982 enum isl_dim_type c4
, enum isl_dim_type c5
)
12984 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
12985 isl_basic_map
*bmap
= NULL
;
12992 dim
= isl_space_dim(space
, isl_dim_all
);
12993 if (dim
< 0 || !eq
|| !ineq
)
12996 if (eq
->n_col
!= ineq
->n_col
)
12997 isl_die(space
->ctx
, isl_error_invalid
,
12998 "equalities and inequalities matrices should have "
12999 "same number of columns", goto error
);
13003 if (eq
->n_col
< total
)
13004 isl_die(space
->ctx
, isl_error_invalid
,
13005 "number of columns too small", goto error
);
13007 extra
= eq
->n_col
- total
;
13009 bmap
= isl_basic_map_alloc_space(isl_space_copy(space
), extra
,
13010 eq
->n_row
, ineq
->n_row
);
13013 for (i
= 0; i
< extra
; ++i
) {
13014 k
= isl_basic_map_alloc_div(bmap
);
13017 isl_int_set_si(bmap
->div
[k
][0], 0);
13019 for (i
= 0; i
< eq
->n_row
; ++i
) {
13020 l
= isl_basic_map_alloc_equality(bmap
);
13023 for (j
= 0, pos
= 0; j
< 5; ++j
) {
13024 int off
= isl_basic_map_offset(bmap
, c
[j
]);
13025 isl_size dim
= isl_basic_map_dim(bmap
, c
[j
]);
13028 for (k
= 0; k
< dim
; ++k
) {
13029 isl_int_set(bmap
->eq
[l
][off
+ k
],
13035 for (i
= 0; i
< ineq
->n_row
; ++i
) {
13036 l
= isl_basic_map_alloc_inequality(bmap
);
13039 for (j
= 0, pos
= 0; j
< 5; ++j
) {
13040 int off
= isl_basic_map_offset(bmap
, c
[j
]);
13041 isl_size dim
= isl_basic_map_dim(bmap
, c
[j
]);
13044 for (k
= 0; k
< dim
; ++k
) {
13045 isl_int_set(bmap
->ineq
[l
][off
+ k
],
13046 ineq
->row
[i
][pos
]);
13052 isl_space_free(space
);
13054 isl_mat_free(ineq
);
13056 bmap
= isl_basic_map_simplify(bmap
);
13057 return isl_basic_map_finalize(bmap
);
13059 isl_space_free(space
);
13061 isl_mat_free(ineq
);
13062 isl_basic_map_free(bmap
);
13066 __isl_give isl_mat
*isl_basic_set_equalities_matrix(
13067 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
13068 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
13070 return isl_basic_map_equalities_matrix(bset_to_bmap(bset
),
13071 c1
, c2
, c3
, c4
, isl_dim_in
);
13074 __isl_give isl_mat
*isl_basic_set_inequalities_matrix(
13075 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
13076 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
13078 return isl_basic_map_inequalities_matrix(bset_to_bmap(bset
),
13079 c1
, c2
, c3
, c4
, isl_dim_in
);
13082 __isl_give isl_basic_set
*isl_basic_set_from_constraint_matrices(
13083 __isl_take isl_space
*space
,
13084 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
13085 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
13087 isl_basic_map
*bmap
;
13088 bmap
= isl_basic_map_from_constraint_matrices(space
, eq
, ineq
,
13089 c1
, c2
, c3
, c4
, isl_dim_in
);
13090 return bset_from_bmap(bmap
);
13093 isl_bool
isl_basic_map_can_zip(__isl_keep isl_basic_map
*bmap
)
13096 return isl_bool_error
;
13098 return isl_space_can_zip(bmap
->dim
);
13101 isl_bool
isl_map_can_zip(__isl_keep isl_map
*map
)
13104 return isl_bool_error
;
13106 return isl_space_can_zip(map
->dim
);
13109 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
13110 * (A -> C) -> (B -> D).
13112 __isl_give isl_basic_map
*isl_basic_map_zip(__isl_take isl_basic_map
*bmap
)
13122 if (!isl_basic_map_can_zip(bmap
))
13123 isl_die(bmap
->ctx
, isl_error_invalid
,
13124 "basic map cannot be zipped", goto error
);
13125 n_in
= isl_space_dim(bmap
->dim
->nested
[0], isl_dim_in
);
13126 n1
= isl_space_dim(bmap
->dim
->nested
[0], isl_dim_out
);
13127 n2
= isl_space_dim(bmap
->dim
->nested
[1], isl_dim_in
);
13128 if (n_in
< 0 || n1
< 0 || n2
< 0)
13129 return isl_basic_map_free(bmap
);
13130 pos
= isl_basic_map_offset(bmap
, isl_dim_in
) + n_in
;
13131 bmap
= isl_basic_map_cow(bmap
);
13132 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
13135 bmap
->dim
= isl_space_zip(bmap
->dim
);
13138 bmap
= isl_basic_map_mark_final(bmap
);
13141 isl_basic_map_free(bmap
);
13145 /* Given a map (A -> B) -> (C -> D), return the corresponding map
13146 * (A -> C) -> (B -> D).
13148 __isl_give isl_map
*isl_map_zip(__isl_take isl_map
*map
)
13153 if (!isl_map_can_zip(map
))
13154 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be zipped",
13157 return isl_map_transform(map
, &isl_space_zip
, &isl_basic_map_zip
);
13163 /* Can we apply isl_basic_map_curry to "bmap"?
13164 * That is, does it have a nested relation in its domain?
13166 isl_bool
isl_basic_map_can_curry(__isl_keep isl_basic_map
*bmap
)
13169 return isl_bool_error
;
13171 return isl_space_can_curry(bmap
->dim
);
13174 /* Can we apply isl_map_curry to "map"?
13175 * That is, does it have a nested relation in its domain?
13177 isl_bool
isl_map_can_curry(__isl_keep isl_map
*map
)
13180 return isl_bool_error
;
13182 return isl_space_can_curry(map
->dim
);
13185 /* Given a basic map (A -> B) -> C, return the corresponding basic map
13188 __isl_give isl_basic_map
*isl_basic_map_curry(__isl_take isl_basic_map
*bmap
)
13194 if (!isl_basic_map_can_curry(bmap
))
13195 isl_die(bmap
->ctx
, isl_error_invalid
,
13196 "basic map cannot be curried", goto error
);
13197 bmap
= isl_basic_map_cow(bmap
);
13200 bmap
->dim
= isl_space_curry(bmap
->dim
);
13203 bmap
= isl_basic_map_mark_final(bmap
);
13206 isl_basic_map_free(bmap
);
13210 /* Given a map (A -> B) -> C, return the corresponding map
13213 __isl_give isl_map
*isl_map_curry(__isl_take isl_map
*map
)
13215 return isl_map_change_space(map
, &isl_map_can_curry
,
13216 "map cannot be curried", &isl_space_curry
);
13219 /* Can isl_map_range_curry be applied to "map"?
13220 * That is, does it have a nested relation in its range,
13221 * the domain of which is itself a nested relation?
13223 isl_bool
isl_map_can_range_curry(__isl_keep isl_map
*map
)
13226 return isl_bool_error
;
13228 return isl_space_can_range_curry(map
->dim
);
13231 /* Given a map A -> ((B -> C) -> D), return the corresponding map
13232 * A -> (B -> (C -> D)).
13234 __isl_give isl_map
*isl_map_range_curry(__isl_take isl_map
*map
)
13236 return isl_map_change_space(map
, &isl_map_can_range_curry
,
13237 "map range cannot be curried",
13238 &isl_space_range_curry
);
13241 /* Can we apply isl_basic_map_uncurry to "bmap"?
13242 * That is, does it have a nested relation in its domain?
13244 isl_bool
isl_basic_map_can_uncurry(__isl_keep isl_basic_map
*bmap
)
13247 return isl_bool_error
;
13249 return isl_space_can_uncurry(bmap
->dim
);
13252 /* Can we apply isl_map_uncurry to "map"?
13253 * That is, does it have a nested relation in its domain?
13255 isl_bool
isl_map_can_uncurry(__isl_keep isl_map
*map
)
13258 return isl_bool_error
;
13260 return isl_space_can_uncurry(map
->dim
);
13263 /* Given a basic map A -> (B -> C), return the corresponding basic map
13266 __isl_give isl_basic_map
*isl_basic_map_uncurry(__isl_take isl_basic_map
*bmap
)
13272 if (!isl_basic_map_can_uncurry(bmap
))
13273 isl_die(bmap
->ctx
, isl_error_invalid
,
13274 "basic map cannot be uncurried",
13275 return isl_basic_map_free(bmap
));
13276 bmap
= isl_basic_map_cow(bmap
);
13279 bmap
->dim
= isl_space_uncurry(bmap
->dim
);
13281 return isl_basic_map_free(bmap
);
13282 bmap
= isl_basic_map_mark_final(bmap
);
13286 /* Given a map A -> (B -> C), return the corresponding map
13289 __isl_give isl_map
*isl_map_uncurry(__isl_take isl_map
*map
)
13291 return isl_map_change_space(map
, &isl_map_can_uncurry
,
13292 "map cannot be uncurried", &isl_space_uncurry
);
13295 __isl_give isl_set
*isl_set_equate(__isl_take isl_set
*set
,
13296 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13298 return isl_map_equate(set
, type1
, pos1
, type2
, pos2
);
13301 /* Construct a basic map where the given dimensions are equal to each other.
13303 static __isl_give isl_basic_map
*equator(__isl_take isl_space
*space
,
13304 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13306 isl_basic_map
*bmap
= NULL
;
13310 total
= isl_space_dim(space
, isl_dim_all
);
13312 isl_space_check_range(space
, type1
, pos1
, 1) < 0 ||
13313 isl_space_check_range(space
, type2
, pos2
, 1) < 0)
13316 if (type1
== type2
&& pos1
== pos2
)
13317 return isl_basic_map_universe(space
);
13319 bmap
= isl_basic_map_alloc_space(isl_space_copy(space
), 0, 1, 0);
13320 i
= isl_basic_map_alloc_equality(bmap
);
13323 isl_seq_clr(bmap
->eq
[i
], 1 + total
);
13324 pos1
+= isl_basic_map_offset(bmap
, type1
);
13325 pos2
+= isl_basic_map_offset(bmap
, type2
);
13326 isl_int_set_si(bmap
->eq
[i
][pos1
], -1);
13327 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
13328 bmap
= isl_basic_map_finalize(bmap
);
13329 isl_space_free(space
);
13332 isl_space_free(space
);
13333 isl_basic_map_free(bmap
);
13337 /* Add a constraint imposing that the given two dimensions are equal.
13339 __isl_give isl_basic_map
*isl_basic_map_equate(__isl_take isl_basic_map
*bmap
,
13340 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13344 eq
= equator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
13346 bmap
= isl_basic_map_intersect(bmap
, eq
);
13351 /* Add a constraint imposing that the given two dimensions are equal.
13353 __isl_give isl_map
*isl_map_equate(__isl_take isl_map
*map
,
13354 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13356 isl_basic_map
*bmap
;
13358 bmap
= equator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
13360 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
13365 /* Add a constraint imposing that the given two dimensions have opposite values.
13367 __isl_give isl_map
*isl_map_oppose(__isl_take isl_map
*map
,
13368 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13370 isl_basic_map
*bmap
= NULL
;
13374 if (isl_map_check_range(map
, type1
, pos1
, 1) < 0)
13375 return isl_map_free(map
);
13376 if (isl_map_check_range(map
, type2
, pos2
, 1) < 0)
13377 return isl_map_free(map
);
13379 total
= isl_map_dim(map
, isl_dim_all
);
13381 return isl_map_free(map
);
13382 bmap
= isl_basic_map_alloc_space(isl_map_get_space(map
), 0, 1, 0);
13383 i
= isl_basic_map_alloc_equality(bmap
);
13386 isl_seq_clr(bmap
->eq
[i
], 1 + total
);
13387 pos1
+= isl_basic_map_offset(bmap
, type1
);
13388 pos2
+= isl_basic_map_offset(bmap
, type2
);
13389 isl_int_set_si(bmap
->eq
[i
][pos1
], 1);
13390 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
13391 bmap
= isl_basic_map_finalize(bmap
);
13393 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
13397 isl_basic_map_free(bmap
);
13402 /* Construct a constraint imposing that the value of the first dimension is
13403 * greater than or equal to that of the second.
13405 static __isl_give isl_constraint
*constraint_order_ge(
13406 __isl_take isl_space
*space
, enum isl_dim_type type1
, int pos1
,
13407 enum isl_dim_type type2
, int pos2
)
13411 if (isl_space_check_range(space
, type1
, pos1
, 1) < 0 ||
13412 isl_space_check_range(space
, type2
, pos2
, 1) < 0)
13413 space
= isl_space_free(space
);
13417 c
= isl_constraint_alloc_inequality(isl_local_space_from_space(space
));
13419 if (type1
== type2
&& pos1
== pos2
)
13422 c
= isl_constraint_set_coefficient_si(c
, type1
, pos1
, 1);
13423 c
= isl_constraint_set_coefficient_si(c
, type2
, pos2
, -1);
13428 /* Add a constraint imposing that the value of the first dimension is
13429 * greater than or equal to that of the second.
13431 __isl_give isl_basic_map
*isl_basic_map_order_ge(__isl_take isl_basic_map
*bmap
,
13432 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13437 if (type1
== type2
&& pos1
== pos2
)
13439 space
= isl_basic_map_get_space(bmap
);
13440 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
13441 bmap
= isl_basic_map_add_constraint(bmap
, c
);
13446 /* Add a constraint imposing that the value of the first dimension is
13447 * greater than or equal to that of the second.
13449 __isl_give isl_map
*isl_map_order_ge(__isl_take isl_map
*map
,
13450 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13455 if (type1
== type2
&& pos1
== pos2
)
13457 space
= isl_map_get_space(map
);
13458 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
13459 map
= isl_map_add_constraint(map
, c
);
13464 /* Add a constraint imposing that the value of the first dimension is
13465 * less than or equal to that of the second.
13467 __isl_give isl_map
*isl_map_order_le(__isl_take isl_map
*map
,
13468 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13470 return isl_map_order_ge(map
, type2
, pos2
, type1
, pos1
);
13473 /* Construct a basic map where the value of the first dimension is
13474 * greater than that of the second.
13476 static __isl_give isl_basic_map
*greator(__isl_take isl_space
*space
,
13477 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13479 isl_basic_map
*bmap
= NULL
;
13483 if (isl_space_check_range(space
, type1
, pos1
, 1) < 0 ||
13484 isl_space_check_range(space
, type2
, pos2
, 1) < 0)
13487 if (type1
== type2
&& pos1
== pos2
)
13488 return isl_basic_map_empty(space
);
13490 bmap
= isl_basic_map_alloc_space(space
, 0, 0, 1);
13491 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
13492 i
= isl_basic_map_alloc_inequality(bmap
);
13493 if (total
< 0 || i
< 0)
13494 return isl_basic_map_free(bmap
);
13495 isl_seq_clr(bmap
->ineq
[i
], 1 + total
);
13496 pos1
+= isl_basic_map_offset(bmap
, type1
);
13497 pos2
+= isl_basic_map_offset(bmap
, type2
);
13498 isl_int_set_si(bmap
->ineq
[i
][pos1
], 1);
13499 isl_int_set_si(bmap
->ineq
[i
][pos2
], -1);
13500 isl_int_set_si(bmap
->ineq
[i
][0], -1);
13501 bmap
= isl_basic_map_finalize(bmap
);
13505 isl_space_free(space
);
13506 isl_basic_map_free(bmap
);
13510 /* Add a constraint imposing that the value of the first dimension is
13511 * greater than that of the second.
13513 __isl_give isl_basic_map
*isl_basic_map_order_gt(__isl_take isl_basic_map
*bmap
,
13514 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13518 gt
= greator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
13520 bmap
= isl_basic_map_intersect(bmap
, gt
);
13525 /* Add a constraint imposing that the value of the first dimension is
13526 * greater than that of the second.
13528 __isl_give isl_map
*isl_map_order_gt(__isl_take isl_map
*map
,
13529 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13531 isl_basic_map
*bmap
;
13533 bmap
= greator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
13535 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
13540 /* Add a constraint imposing that the value of the first dimension is
13541 * smaller than that of the second.
13543 __isl_give isl_map
*isl_map_order_lt(__isl_take isl_map
*map
,
13544 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13546 return isl_map_order_gt(map
, type2
, pos2
, type1
, pos1
);
13549 __isl_give isl_aff
*isl_basic_map_get_div(__isl_keep isl_basic_map
*bmap
,
13553 isl_local_space
*ls
;
13558 if (!isl_basic_map_divs_known(bmap
))
13559 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
13560 "some divs are unknown", return NULL
);
13562 ls
= isl_basic_map_get_local_space(bmap
);
13563 div
= isl_local_space_get_div(ls
, pos
);
13564 isl_local_space_free(ls
);
13569 __isl_give isl_aff
*isl_basic_set_get_div(__isl_keep isl_basic_set
*bset
,
13572 return isl_basic_map_get_div(bset
, pos
);
13575 /* Plug in "subs" for set dimension "pos" of "set".
13577 __isl_give isl_set
*isl_set_substitute(__isl_take isl_set
*set
,
13578 unsigned pos
, __isl_keep isl_aff
*subs
)
13582 if (set
&& isl_set_plain_is_empty(set
))
13585 ma
= isl_multi_aff_identity_on_domain_space(isl_set_get_space(set
));
13586 ma
= isl_multi_aff_set_aff(ma
, pos
, isl_aff_copy(subs
));
13587 return isl_set_preimage_multi_aff(set
, ma
);
13590 /* Check if the range of "ma" is compatible with the domain or range
13591 * (depending on "type") of "bmap".
13593 static isl_stat
check_basic_map_compatible_range_multi_aff(
13594 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type type
,
13595 __isl_keep isl_multi_aff
*ma
)
13598 isl_space
*ma_space
;
13600 ma_space
= isl_multi_aff_get_space(ma
);
13602 m
= isl_space_has_equal_params(bmap
->dim
, ma_space
);
13606 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
13607 "parameters don't match", goto error
);
13608 m
= isl_space_tuple_is_equal(bmap
->dim
, type
, ma_space
, isl_dim_out
);
13612 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
13613 "spaces don't match", goto error
);
13615 isl_space_free(ma_space
);
13616 return isl_stat_ok
;
13618 isl_space_free(ma_space
);
13619 return isl_stat_error
;
13622 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
13623 * coefficients before the transformed range of dimensions,
13624 * the "n_after" coefficients after the transformed range of dimensions
13625 * and the coefficients of the other divs in "bmap".
13627 static __isl_give isl_basic_map
*set_ma_divs(__isl_take isl_basic_map
*bmap
,
13628 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
, int n_div
)
13633 isl_local_space
*ls
;
13638 ls
= isl_aff_get_domain_local_space(ma
->u
.p
[0]);
13639 n_param
= isl_local_space_dim(ls
, isl_dim_param
);
13640 n_set
= isl_local_space_dim(ls
, isl_dim_set
);
13641 if (n_param
< 0 || n_set
< 0)
13642 return isl_basic_map_free(bmap
);
13644 for (i
= 0; i
< n_div
; ++i
) {
13645 int o_bmap
= 0, o_ls
= 0;
13647 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], 1 + 1 + n_param
);
13648 o_bmap
+= 1 + 1 + n_param
;
13649 o_ls
+= 1 + 1 + n_param
;
13650 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_before
);
13651 o_bmap
+= n_before
;
13652 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
13653 ls
->div
->row
[i
] + o_ls
, n_set
);
13656 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_after
);
13658 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
13659 ls
->div
->row
[i
] + o_ls
, n_div
);
13662 isl_seq_clr(bmap
->div
[i
] + o_bmap
, bmap
->n_div
- n_div
);
13663 bmap
= isl_basic_map_add_div_constraints(bmap
, i
);
13668 isl_local_space_free(ls
);
13671 isl_local_space_free(ls
);
13672 return isl_basic_map_free(bmap
);
13675 /* How many stride constraints does "ma" enforce?
13676 * That is, how many of the affine expressions have a denominator
13677 * different from one?
13679 static int multi_aff_strides(__isl_keep isl_multi_aff
*ma
)
13684 for (i
= 0; i
< ma
->n
; ++i
)
13685 if (!isl_int_is_one(ma
->u
.p
[i
]->v
->el
[0]))
13691 /* For each affine expression in ma of the form
13693 * x_i = (f_i y + h_i)/m_i
13695 * with m_i different from one, add a constraint to "bmap"
13698 * f_i y + h_i = m_i alpha_i
13700 * with alpha_i an additional existentially quantified variable.
13702 * The input variables of "ma" correspond to a subset of the variables
13703 * of "bmap". There are "n_before" variables in "bmap" before this
13704 * subset and "n_after" variables after this subset.
13705 * The integer divisions of the affine expressions in "ma" are assumed
13706 * to have been aligned. There are "n_div_ma" of them and
13707 * they appear first in "bmap", straight after the "n_after" variables.
13709 static __isl_give isl_basic_map
*add_ma_strides(
13710 __isl_take isl_basic_map
*bmap
, __isl_keep isl_multi_aff
*ma
,
13711 int n_before
, int n_after
, int n_div_ma
)
13719 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
13720 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
13721 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
13722 if (total
< 0 || n_param
< 0 || n_in
< 0)
13723 return isl_basic_map_free(bmap
);
13724 for (i
= 0; i
< ma
->n
; ++i
) {
13725 int o_bmap
= 0, o_ma
= 1;
13727 if (isl_int_is_one(ma
->u
.p
[i
]->v
->el
[0]))
13729 div
= isl_basic_map_alloc_div(bmap
);
13730 k
= isl_basic_map_alloc_equality(bmap
);
13731 if (div
< 0 || k
< 0)
13733 isl_int_set_si(bmap
->div
[div
][0], 0);
13734 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
13735 ma
->u
.p
[i
]->v
->el
+ o_ma
, 1 + n_param
);
13736 o_bmap
+= 1 + n_param
;
13737 o_ma
+= 1 + n_param
;
13738 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_before
);
13739 o_bmap
+= n_before
;
13740 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
13741 ma
->u
.p
[i
]->v
->el
+ o_ma
, n_in
);
13744 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_after
);
13746 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
13747 ma
->u
.p
[i
]->v
->el
+ o_ma
, n_div_ma
);
13748 o_bmap
+= n_div_ma
;
13750 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, 1 + total
- o_bmap
);
13751 isl_int_neg(bmap
->eq
[k
][1 + total
], ma
->u
.p
[i
]->v
->el
[0]);
13757 isl_basic_map_free(bmap
);
13761 /* Replace the domain or range space (depending on "type) of "space" by "set".
13763 static __isl_give isl_space
*isl_space_set(__isl_take isl_space
*space
,
13764 enum isl_dim_type type
, __isl_take isl_space
*set
)
13766 if (type
== isl_dim_in
) {
13767 space
= isl_space_range(space
);
13768 space
= isl_space_map_from_domain_and_range(set
, space
);
13770 space
= isl_space_domain(space
);
13771 space
= isl_space_map_from_domain_and_range(space
, set
);
13777 /* Compute the preimage of the domain or range (depending on "type")
13778 * of "bmap" under the function represented by "ma".
13779 * In other words, plug in "ma" in the domain or range of "bmap".
13780 * The result is a basic map that lives in the same space as "bmap"
13781 * except that the domain or range has been replaced by
13782 * the domain space of "ma".
13784 * If bmap is represented by
13786 * A(p) + S u + B x + T v + C(divs) >= 0,
13788 * where u and x are input and output dimensions if type == isl_dim_out
13789 * while x and v are input and output dimensions if type == isl_dim_in,
13790 * and ma is represented by
13792 * x = D(p) + F(y) + G(divs')
13794 * then the result is
13796 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
13798 * The divs in the input set are similarly adjusted.
13801 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
13805 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
13806 * B_i G(divs') + c_i(divs))/n_i)
13808 * If bmap is not a rational map and if F(y) involves any denominators
13810 * x_i = (f_i y + h_i)/m_i
13812 * then additional constraints are added to ensure that we only
13813 * map back integer points. That is we enforce
13815 * f_i y + h_i = m_i alpha_i
13817 * with alpha_i an additional existentially quantified variable.
13819 * We first copy over the divs from "ma".
13820 * Then we add the modified constraints and divs from "bmap".
13821 * Finally, we add the stride constraints, if needed.
13823 __isl_give isl_basic_map
*isl_basic_map_preimage_multi_aff(
13824 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
13825 __isl_take isl_multi_aff
*ma
)
13829 isl_basic_map
*res
= NULL
;
13830 isl_size n_before
, n_after
, n_div_bmap
, n_div_ma
;
13831 isl_int f
, c1
, c2
, g
;
13840 ma
= isl_multi_aff_align_divs(ma
);
13843 if (check_basic_map_compatible_range_multi_aff(bmap
, type
, ma
) < 0)
13846 if (type
== isl_dim_in
) {
13848 n_after
= isl_basic_map_dim(bmap
, isl_dim_out
);
13850 n_before
= isl_basic_map_dim(bmap
, isl_dim_in
);
13853 n_div_bmap
= isl_basic_map_dim(bmap
, isl_dim_div
);
13854 n_div_ma
= ma
->n
? isl_aff_dim(ma
->u
.p
[0], isl_dim_div
) : 0;
13855 if (n_before
< 0 || n_after
< 0 || n_div_bmap
< 0 || n_div_ma
< 0)
13858 space
= isl_multi_aff_get_domain_space(ma
);
13859 space
= isl_space_set(isl_basic_map_get_space(bmap
), type
, space
);
13860 rational
= isl_basic_map_is_rational(bmap
);
13861 strides
= rational
? 0 : multi_aff_strides(ma
);
13862 res
= isl_basic_map_alloc_space(space
, n_div_ma
+ n_div_bmap
+ strides
,
13863 bmap
->n_eq
+ strides
, bmap
->n_ineq
+ 2 * n_div_ma
);
13865 res
= isl_basic_map_set_rational(res
);
13867 for (i
= 0; i
< n_div_ma
+ n_div_bmap
; ++i
)
13868 if (isl_basic_map_alloc_div(res
) < 0)
13871 res
= set_ma_divs(res
, ma
, n_before
, n_after
, n_div_ma
);
13875 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
13876 k
= isl_basic_map_alloc_equality(res
);
13879 if (isl_seq_preimage(res
->eq
[k
], bmap
->eq
[i
], ma
, n_before
,
13880 n_after
, n_div_ma
, n_div_bmap
,
13881 f
, c1
, c2
, g
, 0) < 0)
13885 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
13886 k
= isl_basic_map_alloc_inequality(res
);
13889 if (isl_seq_preimage(res
->ineq
[k
], bmap
->ineq
[i
], ma
, n_before
,
13890 n_after
, n_div_ma
, n_div_bmap
,
13891 f
, c1
, c2
, g
, 0) < 0)
13895 for (i
= 0; i
< bmap
->n_div
; ++i
) {
13896 if (isl_int_is_zero(bmap
->div
[i
][0])) {
13897 isl_int_set_si(res
->div
[n_div_ma
+ i
][0], 0);
13900 if (isl_seq_preimage(res
->div
[n_div_ma
+ i
], bmap
->div
[i
], ma
,
13901 n_before
, n_after
, n_div_ma
, n_div_bmap
,
13902 f
, c1
, c2
, g
, 1) < 0)
13907 res
= add_ma_strides(res
, ma
, n_before
, n_after
, n_div_ma
);
13913 isl_basic_map_free(bmap
);
13914 isl_multi_aff_free(ma
);
13915 res
= isl_basic_map_simplify(res
);
13916 return isl_basic_map_finalize(res
);
13922 isl_basic_map_free(bmap
);
13923 isl_multi_aff_free(ma
);
13924 isl_basic_map_free(res
);
13928 /* Compute the preimage of "bset" under the function represented by "ma".
13929 * In other words, plug in "ma" in "bset". The result is a basic set
13930 * that lives in the domain space of "ma".
13932 __isl_give isl_basic_set
*isl_basic_set_preimage_multi_aff(
13933 __isl_take isl_basic_set
*bset
, __isl_take isl_multi_aff
*ma
)
13935 return isl_basic_map_preimage_multi_aff(bset
, isl_dim_set
, ma
);
13938 /* Compute the preimage of the domain of "bmap" under the function
13939 * represented by "ma".
13940 * In other words, plug in "ma" in the domain of "bmap".
13941 * The result is a basic map that lives in the same space as "bmap"
13942 * except that the domain has been replaced by the domain space of "ma".
13944 __isl_give isl_basic_map
*isl_basic_map_preimage_domain_multi_aff(
13945 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
13947 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_in
, ma
);
13950 /* Compute the preimage of the range of "bmap" under the function
13951 * represented by "ma".
13952 * In other words, plug in "ma" in the range of "bmap".
13953 * The result is a basic map that lives in the same space as "bmap"
13954 * except that the range has been replaced by the domain space of "ma".
13956 __isl_give isl_basic_map
*isl_basic_map_preimage_range_multi_aff(
13957 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
13959 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_out
, ma
);
13962 /* Check if the range of "ma" is compatible with the domain or range
13963 * (depending on "type") of "map".
13964 * Return isl_stat_error if anything is wrong.
13966 static isl_stat
check_map_compatible_range_multi_aff(
13967 __isl_keep isl_map
*map
, enum isl_dim_type type
,
13968 __isl_keep isl_multi_aff
*ma
)
13971 isl_space
*ma_space
;
13973 ma_space
= isl_multi_aff_get_space(ma
);
13974 m
= isl_map_space_tuple_is_equal(map
, type
, ma_space
, isl_dim_out
);
13975 isl_space_free(ma_space
);
13977 return isl_stat_error
;
13979 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
13980 "spaces don't match", return isl_stat_error
);
13981 return isl_stat_ok
;
13984 /* Compute the preimage of the domain or range (depending on "type")
13985 * of "map" under the function represented by "ma".
13986 * In other words, plug in "ma" in the domain or range of "map".
13987 * The result is a map that lives in the same space as "map"
13988 * except that the domain or range has been replaced by
13989 * the domain space of "ma".
13991 * The parameters are assumed to have been aligned.
13993 static __isl_give isl_map
*map_preimage_multi_aff(__isl_take isl_map
*map
,
13994 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
13999 map
= isl_map_cow(map
);
14000 ma
= isl_multi_aff_align_divs(ma
);
14003 if (check_map_compatible_range_multi_aff(map
, type
, ma
) < 0)
14006 for (i
= 0; i
< map
->n
; ++i
) {
14007 map
->p
[i
] = isl_basic_map_preimage_multi_aff(map
->p
[i
], type
,
14008 isl_multi_aff_copy(ma
));
14013 space
= isl_multi_aff_get_domain_space(ma
);
14014 space
= isl_space_set(isl_map_get_space(map
), type
, space
);
14016 isl_space_free(isl_map_take_space(map
));
14017 map
= isl_map_restore_space(map
, space
);
14021 isl_multi_aff_free(ma
);
14023 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
14024 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
14027 isl_multi_aff_free(ma
);
14032 /* Compute the preimage of the domain or range (depending on "type")
14033 * of "map" under the function represented by "ma".
14034 * In other words, plug in "ma" in the domain or range of "map".
14035 * The result is a map that lives in the same space as "map"
14036 * except that the domain or range has been replaced by
14037 * the domain space of "ma".
14039 __isl_give isl_map
*isl_map_preimage_multi_aff(__isl_take isl_map
*map
,
14040 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
14047 aligned
= isl_map_space_has_equal_params(map
, ma
->space
);
14051 return map_preimage_multi_aff(map
, type
, ma
);
14053 if (isl_map_check_named_params(map
) < 0)
14055 if (!isl_space_has_named_params(ma
->space
))
14056 isl_die(map
->ctx
, isl_error_invalid
,
14057 "unaligned unnamed parameters", goto error
);
14058 map
= isl_map_align_params(map
, isl_multi_aff_get_space(ma
));
14059 ma
= isl_multi_aff_align_params(ma
, isl_map_get_space(map
));
14061 return map_preimage_multi_aff(map
, type
, ma
);
14063 isl_multi_aff_free(ma
);
14064 return isl_map_free(map
);
14067 /* Compute the preimage of "set" under the function represented by "ma".
14068 * In other words, plug in "ma" in "set". The result is a set
14069 * that lives in the domain space of "ma".
14071 __isl_give isl_set
*isl_set_preimage_multi_aff(__isl_take isl_set
*set
,
14072 __isl_take isl_multi_aff
*ma
)
14074 return isl_map_preimage_multi_aff(set
, isl_dim_set
, ma
);
14077 /* Compute the preimage of the domain of "map" under the function
14078 * represented by "ma".
14079 * In other words, plug in "ma" in the domain of "map".
14080 * The result is a map that lives in the same space as "map"
14081 * except that the domain has been replaced by the domain space of "ma".
14083 __isl_give isl_map
*isl_map_preimage_domain_multi_aff(__isl_take isl_map
*map
,
14084 __isl_take isl_multi_aff
*ma
)
14086 return isl_map_preimage_multi_aff(map
, isl_dim_in
, ma
);
14089 /* Compute the preimage of the range of "map" under the function
14090 * represented by "ma".
14091 * In other words, plug in "ma" in the range of "map".
14092 * The result is a map that lives in the same space as "map"
14093 * except that the range has been replaced by the domain space of "ma".
14095 __isl_give isl_map
*isl_map_preimage_range_multi_aff(__isl_take isl_map
*map
,
14096 __isl_take isl_multi_aff
*ma
)
14098 return isl_map_preimage_multi_aff(map
, isl_dim_out
, ma
);
14101 /* Compute the preimage of "map" under the function represented by "pma".
14102 * In other words, plug in "pma" in the domain or range of "map".
14103 * The result is a map that lives in the same space as "map",
14104 * except that the space of type "type" has been replaced by
14105 * the domain space of "pma".
14107 * The parameters of "map" and "pma" are assumed to have been aligned.
14109 static __isl_give isl_map
*isl_map_preimage_pw_multi_aff_aligned(
14110 __isl_take isl_map
*map
, enum isl_dim_type type
,
14111 __isl_take isl_pw_multi_aff
*pma
)
14120 isl_pw_multi_aff_free(pma
);
14121 res
= isl_map_empty(isl_map_get_space(map
));
14126 res
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
14127 isl_multi_aff_copy(pma
->p
[0].maff
));
14128 if (type
== isl_dim_in
)
14129 res
= isl_map_intersect_domain(res
,
14130 isl_map_copy(pma
->p
[0].set
));
14132 res
= isl_map_intersect_range(res
,
14133 isl_map_copy(pma
->p
[0].set
));
14135 for (i
= 1; i
< pma
->n
; ++i
) {
14138 res_i
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
14139 isl_multi_aff_copy(pma
->p
[i
].maff
));
14140 if (type
== isl_dim_in
)
14141 res_i
= isl_map_intersect_domain(res_i
,
14142 isl_map_copy(pma
->p
[i
].set
));
14144 res_i
= isl_map_intersect_range(res_i
,
14145 isl_map_copy(pma
->p
[i
].set
));
14146 res
= isl_map_union(res
, res_i
);
14149 isl_pw_multi_aff_free(pma
);
14153 isl_pw_multi_aff_free(pma
);
14158 /* Compute the preimage of "map" under the function represented by "pma".
14159 * In other words, plug in "pma" in the domain or range of "map".
14160 * The result is a map that lives in the same space as "map",
14161 * except that the space of type "type" has been replaced by
14162 * the domain space of "pma".
14164 __isl_give isl_map
*isl_map_preimage_pw_multi_aff(__isl_take isl_map
*map
,
14165 enum isl_dim_type type
, __isl_take isl_pw_multi_aff
*pma
)
14172 aligned
= isl_map_space_has_equal_params(map
, pma
->dim
);
14176 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
14178 if (isl_map_check_named_params(map
) < 0)
14180 if (isl_pw_multi_aff_check_named_params(pma
) < 0)
14182 map
= isl_map_align_params(map
, isl_pw_multi_aff_get_space(pma
));
14183 pma
= isl_pw_multi_aff_align_params(pma
, isl_map_get_space(map
));
14185 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
14187 isl_pw_multi_aff_free(pma
);
14188 return isl_map_free(map
);
14191 /* Compute the preimage of "set" under the function represented by "pma".
14192 * In other words, plug in "pma" in "set". The result is a set
14193 * that lives in the domain space of "pma".
14195 __isl_give isl_set
*isl_set_preimage_pw_multi_aff(__isl_take isl_set
*set
,
14196 __isl_take isl_pw_multi_aff
*pma
)
14198 return isl_map_preimage_pw_multi_aff(set
, isl_dim_set
, pma
);
14201 /* Compute the preimage of the domain of "map" under the function
14202 * represented by "pma".
14203 * In other words, plug in "pma" in the domain of "map".
14204 * The result is a map that lives in the same space as "map",
14205 * except that domain space has been replaced by the domain space of "pma".
14207 __isl_give isl_map
*isl_map_preimage_domain_pw_multi_aff(
14208 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
14210 return isl_map_preimage_pw_multi_aff(map
, isl_dim_in
, pma
);
14213 /* Compute the preimage of the range of "map" under the function
14214 * represented by "pma".
14215 * In other words, plug in "pma" in the range of "map".
14216 * The result is a map that lives in the same space as "map",
14217 * except that range space has been replaced by the domain space of "pma".
14219 __isl_give isl_map
*isl_map_preimage_range_pw_multi_aff(
14220 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
14222 return isl_map_preimage_pw_multi_aff(map
, isl_dim_out
, pma
);
14225 /* Compute the preimage of "map" under the function represented by "mpa".
14226 * In other words, plug in "mpa" in the domain or range of "map".
14227 * The result is a map that lives in the same space as "map",
14228 * except that the space of type "type" has been replaced by
14229 * the domain space of "mpa".
14231 * If the map does not involve any constraints that refer to the
14232 * dimensions of the substituted space, then the only possible
14233 * effect of "mpa" on the map is to map the space to a different space.
14234 * We create a separate isl_multi_aff to effectuate this change
14235 * in order to avoid spurious splitting of the map along the pieces
14237 * If "mpa" has a non-trivial explicit domain, however,
14238 * then the full substitution should be performed.
14240 __isl_give isl_map
*isl_map_preimage_multi_pw_aff(__isl_take isl_map
*map
,
14241 enum isl_dim_type type
, __isl_take isl_multi_pw_aff
*mpa
)
14245 isl_pw_multi_aff
*pma
;
14247 n
= isl_map_dim(map
, type
);
14251 full
= isl_map_involves_dims(map
, type
, 0, n
);
14252 if (full
>= 0 && !full
)
14253 full
= isl_multi_pw_aff_has_non_trivial_domain(mpa
);
14260 space
= isl_multi_pw_aff_get_space(mpa
);
14261 isl_multi_pw_aff_free(mpa
);
14262 ma
= isl_multi_aff_zero(space
);
14263 return isl_map_preimage_multi_aff(map
, type
, ma
);
14266 pma
= isl_pw_multi_aff_from_multi_pw_aff(mpa
);
14267 return isl_map_preimage_pw_multi_aff(map
, type
, pma
);
14270 isl_multi_pw_aff_free(mpa
);
14274 /* Compute the preimage of "map" under the function represented by "mpa".
14275 * In other words, plug in "mpa" in the domain "map".
14276 * The result is a map that lives in the same space as "map",
14277 * except that domain space has been replaced by the domain space of "mpa".
14279 __isl_give isl_map
*isl_map_preimage_domain_multi_pw_aff(
14280 __isl_take isl_map
*map
, __isl_take isl_multi_pw_aff
*mpa
)
14282 return isl_map_preimage_multi_pw_aff(map
, isl_dim_in
, mpa
);
14285 /* Compute the preimage of "set" by the function represented by "mpa".
14286 * In other words, plug in "mpa" in "set".
14288 __isl_give isl_set
*isl_set_preimage_multi_pw_aff(__isl_take isl_set
*set
,
14289 __isl_take isl_multi_pw_aff
*mpa
)
14291 return isl_map_preimage_multi_pw_aff(set
, isl_dim_set
, mpa
);
14294 /* Return a copy of the equality constraints of "bset" as a matrix.
14296 __isl_give isl_mat
*isl_basic_set_extract_equalities(
14297 __isl_keep isl_basic_set
*bset
)
14302 total
= isl_basic_set_dim(bset
, isl_dim_all
);
14306 ctx
= isl_basic_set_get_ctx(bset
);
14307 return isl_mat_sub_alloc6(ctx
, bset
->eq
, 0, bset
->n_eq
, 0, 1 + total
);
14310 /* Are the "n" "coefficients" starting at "first" of the integer division
14311 * expressions at position "pos1" in "bmap1" and "pos2" in "bmap2" equal
14313 * The "coefficient" at position 0 is the denominator.
14314 * The "coefficient" at position 1 is the constant term.
14316 isl_bool
isl_basic_map_equal_div_expr_part(__isl_keep isl_basic_map
*bmap1
,
14317 int pos1
, __isl_keep isl_basic_map
*bmap2
, int pos2
,
14318 unsigned first
, unsigned n
)
14320 if (isl_basic_map_check_range(bmap1
, isl_dim_div
, pos1
, 1) < 0)
14321 return isl_bool_error
;
14322 if (isl_basic_map_check_range(bmap2
, isl_dim_div
, pos2
, 1) < 0)
14323 return isl_bool_error
;
14324 return isl_seq_eq(bmap1
->div
[pos1
] + first
,
14325 bmap2
->div
[pos2
] + first
, n
);
14328 /* Are the integer division expressions at position "pos1" in "bmap1" and
14329 * "pos2" in "bmap2" equal to each other, except that the constant terms
14332 isl_bool
isl_basic_map_equal_div_expr_except_constant(
14333 __isl_keep isl_basic_map
*bmap1
, int pos1
,
14334 __isl_keep isl_basic_map
*bmap2
, int pos2
)
14337 isl_size total
, total2
;
14339 total
= isl_basic_map_dim(bmap1
, isl_dim_all
);
14340 total2
= isl_basic_map_dim(bmap2
, isl_dim_all
);
14341 if (total
< 0 || total2
< 0)
14342 return isl_bool_error
;
14343 if (total
!= total2
)
14344 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
14345 "incomparable div expressions", return isl_bool_error
);
14346 equal
= isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
14348 if (equal
< 0 || !equal
)
14350 equal
= isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
14352 if (equal
< 0 || equal
)
14353 return isl_bool_not(equal
);
14354 return isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
14358 /* Replace the numerator of the constant term of the integer division
14359 * expression at position "div" in "bmap" by "value".
14360 * The caller guarantees that this does not change the meaning
14363 __isl_give isl_basic_map
*isl_basic_map_set_div_expr_constant_num_si_inplace(
14364 __isl_take isl_basic_map
*bmap
, int div
, int value
)
14366 if (isl_basic_map_check_range(bmap
, isl_dim_div
, div
, 1) < 0)
14367 return isl_basic_map_free(bmap
);
14369 isl_int_set_si(bmap
->div
[div
][1], value
);
14374 /* Is the point "inner" internal to inequality constraint "ineq"
14376 * The point is considered to be internal to the inequality constraint,
14377 * if it strictly lies on the positive side of the inequality constraint,
14378 * or if it lies on the constraint and the constraint is lexico-positive.
14380 static isl_bool
is_internal(__isl_keep isl_vec
*inner
,
14381 __isl_keep isl_basic_set
*bset
, int ineq
)
14387 if (!inner
|| !bset
)
14388 return isl_bool_error
;
14390 ctx
= isl_basic_set_get_ctx(bset
);
14391 isl_seq_inner_product(inner
->el
, bset
->ineq
[ineq
], inner
->size
,
14392 &ctx
->normalize_gcd
);
14393 if (!isl_int_is_zero(ctx
->normalize_gcd
))
14394 return isl_int_is_nonneg(ctx
->normalize_gcd
);
14396 total
= isl_basic_set_dim(bset
, isl_dim_all
);
14398 return isl_bool_error
;
14399 pos
= isl_seq_first_non_zero(bset
->ineq
[ineq
] + 1, total
);
14400 return isl_int_is_pos(bset
->ineq
[ineq
][1 + pos
]);
14403 /* Tighten the inequality constraints of "bset" that are outward with respect
14404 * to the point "vec".
14405 * That is, tighten the constraints that are not satisfied by "vec".
14407 * "vec" is a point internal to some superset S of "bset" that is used
14408 * to make the subsets of S disjoint, by tightening one half of the constraints
14409 * that separate two subsets. In particular, the constraints of S
14410 * are all satisfied by "vec" and should not be tightened.
14411 * Of the internal constraints, those that have "vec" on the outside
14412 * are tightened. The shared facet is included in the adjacent subset
14413 * with the opposite constraint.
14414 * For constraints that saturate "vec", this criterion cannot be used
14415 * to determine which of the two sides should be tightened.
14416 * Instead, the sign of the first non-zero coefficient is used
14417 * to make this choice. Note that this second criterion is never used
14418 * on the constraints of S since "vec" is interior to "S".
14420 __isl_give isl_basic_set
*isl_basic_set_tighten_outward(
14421 __isl_take isl_basic_set
*bset
, __isl_keep isl_vec
*vec
)
14425 bset
= isl_basic_set_cow(bset
);
14428 for (j
= 0; j
< bset
->n_ineq
; ++j
) {
14431 internal
= is_internal(vec
, bset
, j
);
14433 return isl_basic_set_free(bset
);
14436 isl_int_sub_ui(bset
->ineq
[j
][0], bset
->ineq
[j
][0], 1);
14442 /* Replace the variables x of type "type" starting at "first" in "bmap"
14443 * by x' with x = M x' with M the matrix trans.
14444 * That is, replace the corresponding coefficients c by c M.
14446 * The transformation matrix should be a square matrix.
14448 __isl_give isl_basic_map
*isl_basic_map_transform_dims(
14449 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
, unsigned first
,
14450 __isl_take isl_mat
*trans
)
14454 bmap
= isl_basic_map_cow(bmap
);
14455 if (!bmap
|| !trans
)
14458 if (trans
->n_row
!= trans
->n_col
)
14459 isl_die(trans
->ctx
, isl_error_invalid
,
14460 "expecting square transformation matrix", goto error
);
14461 if (isl_basic_map_check_range(bmap
, type
, first
, trans
->n_row
) < 0)
14464 pos
= isl_basic_map_offset(bmap
, type
) + first
;
14466 if (isl_mat_sub_transform(bmap
->eq
, bmap
->n_eq
, pos
,
14467 isl_mat_copy(trans
)) < 0)
14469 if (isl_mat_sub_transform(bmap
->ineq
, bmap
->n_ineq
, pos
,
14470 isl_mat_copy(trans
)) < 0)
14472 if (isl_mat_sub_transform(bmap
->div
, bmap
->n_div
, 1 + pos
,
14473 isl_mat_copy(trans
)) < 0)
14476 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
14477 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
14479 isl_mat_free(trans
);
14482 isl_mat_free(trans
);
14483 isl_basic_map_free(bmap
);
14487 /* Replace the variables x of type "type" starting at "first" in "bset"
14488 * by x' with x = M x' with M the matrix trans.
14489 * That is, replace the corresponding coefficients c by c M.
14491 * The transformation matrix should be a square matrix.
14493 __isl_give isl_basic_set
*isl_basic_set_transform_dims(
14494 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned first
,
14495 __isl_take isl_mat
*trans
)
14497 return isl_basic_map_transform_dims(bset
, type
, first
, trans
);