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
10 * Use of this software is governed by the MIT license
12 * Written by Sven Verdoolaege, K.U.Leuven, Departement
13 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
14 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
15 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
16 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
17 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
18 * B.P. 105 - 78153 Le Chesnay, France
19 * and Centre de Recherche Inria de Paris, 2 rue Simone Iff - Voie DQ12,
20 * CS 42112, 75589 Paris Cedex 12, France
21 * and Cerebras Systems, 175 S San Antonio Rd, Los Altos, CA, USA
25 #include <isl_ctx_private.h>
26 #include <isl_map_private.h>
28 #include <isl_id_private.h>
29 #include <isl/constraint.h>
30 #include "isl_space_private.h"
31 #include "isl_equalities.h"
32 #include <isl_lp_private.h>
36 #include <isl_reordering.h>
37 #include "isl_sample.h"
41 #include <isl_mat_private.h>
42 #include <isl_vec_private.h>
43 #include <isl_dim_map.h>
44 #include <isl_local_space_private.h>
45 #include <isl_aff_private.h>
46 #include <isl_options_private.h>
47 #include <isl_morph.h>
48 #include <isl_val_private.h>
49 #include <isl_printer_private.h>
51 #include <bset_to_bmap.c>
52 #include <bset_from_bmap.c>
53 #include <set_to_map.c>
54 #include <set_from_map.c>
56 /* Treat "bset" as a basic map.
57 * Internally, isl_basic_set is defined to isl_basic_map, so in practice,
58 * this function performs a redundant cast.
60 static __isl_keep
const isl_basic_map
*const_bset_to_bmap(
61 __isl_keep
const isl_basic_set
*bset
)
63 return (const isl_basic_map
*) bset
;
67 #define TYPE isl_basic_map
68 #include "has_single_reference_templ.c"
70 static unsigned pos(__isl_keep isl_space
*space
, enum isl_dim_type type
)
73 case isl_dim_param
: return 1;
74 case isl_dim_in
: return 1 + space
->nparam
;
75 case isl_dim_out
: return 1 + space
->nparam
+ space
->n_in
;
80 isl_size
isl_basic_map_dim(__isl_keep isl_basic_map
*bmap
,
81 enum isl_dim_type type
)
84 return isl_size_error
;
86 case isl_dim_cst
: return 1;
89 case isl_dim_out
: return isl_space_dim(bmap
->dim
, type
);
90 case isl_dim_div
: return bmap
->n_div
;
91 case isl_dim_all
: return isl_basic_map_total_dim(bmap
);
96 /* Return the space of "map".
98 __isl_keep isl_space
*isl_map_peek_space(__isl_keep
const isl_map
*map
)
100 return map
? map
->dim
: NULL
;
103 /* Return the space of "set".
105 __isl_keep isl_space
*isl_set_peek_space(__isl_keep isl_set
*set
)
107 return isl_map_peek_space(set_to_map(set
));
110 isl_size
isl_map_dim(__isl_keep isl_map
*map
, enum isl_dim_type type
)
112 return isl_space_dim(isl_map_peek_space(map
), type
);
115 /* Return the dimensionality of the domain (tuple) of the map.
117 isl_size
isl_map_domain_tuple_dim(__isl_keep isl_map
*map
)
119 return isl_map_dim(map
, isl_dim_in
);
122 /* Return the dimensionality of the range (tuple) of the map.
124 isl_size
isl_map_range_tuple_dim(__isl_keep isl_map
*map
)
126 return isl_map_dim(map
, isl_dim_out
);
129 isl_size
isl_set_dim(__isl_keep isl_set
*set
, enum isl_dim_type type
)
131 return isl_map_dim(set_to_map(set
), type
);
134 /* Return the dimensionality of the (tuple of the) set.
136 isl_size
isl_set_tuple_dim(__isl_keep isl_set
*set
)
138 return isl_set_dim(set
, isl_dim_set
);
141 /* Return the position of the variables of the given type
142 * within the sequence of variables of "bmap".
144 isl_size
isl_basic_map_var_offset(__isl_keep isl_basic_map
*bmap
,
145 enum isl_dim_type type
)
149 space
= isl_basic_map_peek_space(bmap
);
151 return isl_size_error
;
156 case isl_dim_out
: return isl_space_offset(space
, type
);
157 case isl_dim_div
: return isl_space_dim(space
, isl_dim_all
);
160 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
161 "invalid dimension type", return isl_size_error
);
165 /* Return the position of the variables of the given type
166 * within the sequence of variables of "bset".
168 isl_size
isl_basic_set_var_offset(__isl_keep isl_basic_set
*bset
,
169 enum isl_dim_type type
)
171 return isl_basic_map_var_offset(bset_to_bmap(bset
), type
);
174 /* Return the position of the coefficients of the variables of the given type
175 * within the sequence of coefficients of "bmap".
177 unsigned isl_basic_map_offset(__isl_keep isl_basic_map
*bmap
,
178 enum isl_dim_type type
)
181 case isl_dim_cst
: return 0;
185 case isl_dim_div
: return 1 + isl_basic_map_var_offset(bmap
, type
);
190 unsigned isl_basic_set_offset(__isl_keep isl_basic_set
*bset
,
191 enum isl_dim_type type
)
193 return isl_basic_map_offset(bset
, type
);
196 static unsigned map_offset(__isl_keep isl_map
*map
, enum isl_dim_type type
)
198 return pos(map
->dim
, type
);
201 isl_size
isl_basic_set_dim(__isl_keep isl_basic_set
*bset
,
202 enum isl_dim_type type
)
204 return isl_basic_map_dim(bset
, type
);
207 isl_size
isl_basic_set_n_dim(__isl_keep isl_basic_set
*bset
)
209 return isl_basic_set_dim(bset
, isl_dim_set
);
212 isl_size
isl_basic_set_n_param(__isl_keep isl_basic_set
*bset
)
214 return isl_basic_set_dim(bset
, isl_dim_param
);
217 isl_size
isl_basic_set_total_dim(__isl_keep
const isl_basic_set
*bset
)
219 return isl_basic_map_total_dim(const_bset_to_bmap(bset
));
222 isl_size
isl_set_n_dim(__isl_keep isl_set
*set
)
224 return isl_set_dim(set
, isl_dim_set
);
227 isl_size
isl_set_n_param(__isl_keep isl_set
*set
)
229 return isl_set_dim(set
, isl_dim_param
);
232 isl_size
isl_basic_map_total_dim(__isl_keep
const isl_basic_map
*bmap
)
237 return isl_size_error
;
238 dim
= isl_space_dim(bmap
->dim
, isl_dim_all
);
240 return isl_size_error
;
241 return dim
+ bmap
->n_div
;
244 /* Return the number of equality constraints in the description of "bmap".
245 * Return isl_size_error on error.
247 isl_size
isl_basic_map_n_equality(__isl_keep isl_basic_map
*bmap
)
250 return isl_size_error
;
254 /* Return the number of equality constraints in the description of "bset".
255 * Return isl_size_error on error.
257 isl_size
isl_basic_set_n_equality(__isl_keep isl_basic_set
*bset
)
259 return isl_basic_map_n_equality(bset_to_bmap(bset
));
262 /* Return the number of inequality constraints in the description of "bmap".
263 * Return isl_size_error on error.
265 isl_size
isl_basic_map_n_inequality(__isl_keep isl_basic_map
*bmap
)
268 return isl_size_error
;
272 /* Return the number of inequality constraints in the description of "bset".
273 * Return isl_size_error on error.
275 isl_size
isl_basic_set_n_inequality(__isl_keep isl_basic_set
*bset
)
277 return isl_basic_map_n_inequality(bset_to_bmap(bset
));
280 /* Do "bmap1" and "bmap2" have the same parameters?
282 static isl_bool
isl_basic_map_has_equal_params(__isl_keep isl_basic_map
*bmap1
,
283 __isl_keep isl_basic_map
*bmap2
)
285 isl_space
*space1
, *space2
;
287 space1
= isl_basic_map_peek_space(bmap1
);
288 space2
= isl_basic_map_peek_space(bmap2
);
289 return isl_space_has_equal_params(space1
, space2
);
292 /* Do "map1" and "map2" have the same parameters?
294 isl_bool
isl_map_has_equal_params(__isl_keep isl_map
*map1
,
295 __isl_keep isl_map
*map2
)
297 isl_space
*space1
, *space2
;
299 space1
= isl_map_peek_space(map1
);
300 space2
= isl_map_peek_space(map2
);
301 return isl_space_has_equal_params(space1
, space2
);
304 /* Do "map" and "set" have the same parameters?
306 static isl_bool
isl_map_set_has_equal_params(__isl_keep isl_map
*map
,
307 __isl_keep isl_set
*set
)
309 return isl_map_has_equal_params(map
, set_to_map(set
));
312 /* Is the tuple of type "type" of "bmap" the same as the single tuple of "bset"?
314 static isl_bool
isl_basic_map_set_tuple_is_equal(__isl_keep isl_basic_map
*bmap
,
315 enum isl_dim_type type
, __isl_keep isl_basic_set
*bset
)
317 isl_space
*bmap_space
, *bset_space
;
319 bmap_space
= isl_basic_map_peek_space(bmap
);
320 bset_space
= isl_basic_set_peek_space(bset
);
321 return isl_space_tuple_is_equal(bmap_space
, type
,
322 bset_space
, isl_dim_set
);
325 /* Is the tuple of type "type" of "map" the same as the single tuple of "set"?
327 static isl_bool
isl_map_set_tuple_is_equal(__isl_keep isl_map
*map
,
328 enum isl_dim_type type
, __isl_keep isl_set
*set
)
330 return isl_map_tuple_is_equal(map
, type
, set_to_map(set
), isl_dim_set
);
333 isl_bool
isl_map_compatible_domain(__isl_keep isl_map
*map
,
334 __isl_keep isl_set
*set
)
338 return isl_bool_error
;
339 m
= isl_map_has_equal_params(map
, set_to_map(set
));
342 return isl_map_set_tuple_is_equal(map
, isl_dim_in
, set
);
345 isl_bool
isl_basic_map_compatible_domain(__isl_keep isl_basic_map
*bmap
,
346 __isl_keep isl_basic_set
*bset
)
350 return isl_bool_error
;
351 m
= isl_basic_map_has_equal_params(bmap
, bset_to_bmap(bset
));
354 return isl_basic_map_set_tuple_is_equal(bmap
, isl_dim_in
, bset
);
357 isl_bool
isl_map_compatible_range(__isl_keep isl_map
*map
,
358 __isl_keep isl_set
*set
)
362 return isl_bool_error
;
363 m
= isl_map_has_equal_params(map
, set_to_map(set
));
366 return isl_map_set_tuple_is_equal(map
, isl_dim_out
, set
);
369 isl_bool
isl_basic_map_compatible_range(__isl_keep isl_basic_map
*bmap
,
370 __isl_keep isl_basic_set
*bset
)
374 return isl_bool_error
;
375 m
= isl_basic_map_has_equal_params(bmap
, bset_to_bmap(bset
));
378 return isl_basic_map_set_tuple_is_equal(bmap
, isl_dim_out
, bset
);
381 isl_ctx
*isl_basic_map_get_ctx(__isl_keep isl_basic_map
*bmap
)
383 return bmap
? bmap
->ctx
: NULL
;
386 isl_ctx
*isl_basic_set_get_ctx(__isl_keep isl_basic_set
*bset
)
388 return bset
? bset
->ctx
: NULL
;
391 isl_ctx
*isl_map_get_ctx(__isl_keep isl_map
*map
)
393 return map
? map
->ctx
: NULL
;
396 isl_ctx
*isl_set_get_ctx(__isl_keep isl_set
*set
)
398 return set
? set
->ctx
: NULL
;
401 /* Return the space of "bmap".
403 __isl_keep isl_space
*isl_basic_map_peek_space(
404 __isl_keep
const isl_basic_map
*bmap
)
406 return bmap
? bmap
->dim
: NULL
;
409 /* Return the space of "bset".
411 __isl_keep isl_space
*isl_basic_set_peek_space(__isl_keep isl_basic_set
*bset
)
413 return isl_basic_map_peek_space(bset_to_bmap(bset
));
416 __isl_give isl_space
*isl_basic_map_get_space(__isl_keep isl_basic_map
*bmap
)
418 return isl_space_copy(isl_basic_map_peek_space(bmap
));
421 __isl_give isl_space
*isl_basic_set_get_space(__isl_keep isl_basic_set
*bset
)
423 return isl_basic_map_get_space(bset_to_bmap(bset
));
426 /* Return the space of "bmap".
427 * This may be either a copy or the space itself
428 * if there is only one reference to "bmap".
429 * This allows the space to be modified inplace
430 * if both the basic map and its space have only a single reference.
431 * The caller is not allowed to modify "bmap" between this call and
432 * a subsequent call to isl_basic_map_restore_space.
433 * The only exception is that isl_basic_map_free can be called instead.
435 static __isl_give isl_space
*isl_basic_map_take_space(
436 __isl_keep isl_basic_map
*bmap
)
443 return isl_basic_map_get_space(bmap
);
449 /* Set the space of "bmap" to "space", where the space of "bmap" may be missing
450 * due to a preceding call to isl_basic_map_take_space.
451 * However, in this case, "bmap" only has a single reference and
452 * then the call to isl_basic_map_cow has no effect.
454 static __isl_give isl_basic_map
*isl_basic_map_restore_space(
455 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*space
)
460 if (bmap
->dim
== space
) {
461 isl_space_free(space
);
465 bmap
= isl_basic_map_cow(bmap
);
468 isl_space_free(bmap
->dim
);
473 isl_basic_map_free(bmap
);
474 isl_space_free(space
);
478 /* Extract the divs in "bmap" as a matrix.
480 __isl_give isl_mat
*isl_basic_map_get_divs(__isl_keep isl_basic_map
*bmap
)
488 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
492 ctx
= isl_basic_map_get_ctx(bmap
);
493 cols
= 1 + 1 + v_div
+ bmap
->n_div
;
494 div
= isl_mat_alloc(ctx
, bmap
->n_div
, cols
);
498 for (i
= 0; i
< bmap
->n_div
; ++i
)
499 isl_seq_cpy(div
->row
[i
], bmap
->div
[i
], cols
);
504 /* Extract the divs in "bset" as a matrix.
506 __isl_give isl_mat
*isl_basic_set_get_divs(__isl_keep isl_basic_set
*bset
)
508 return isl_basic_map_get_divs(bset
);
511 __isl_give isl_local_space
*isl_basic_map_get_local_space(
512 __isl_keep isl_basic_map
*bmap
)
519 div
= isl_basic_map_get_divs(bmap
);
520 return isl_local_space_alloc_div(isl_space_copy(bmap
->dim
), div
);
523 __isl_give isl_local_space
*isl_basic_set_get_local_space(
524 __isl_keep isl_basic_set
*bset
)
526 return isl_basic_map_get_local_space(bset
);
529 /* For each known div d = floor(f/m), add the constraints
532 * -(f-(m-1)) + m d >= 0
534 * Do not finalize the result.
536 static __isl_give isl_basic_map
*add_known_div_constraints(
537 __isl_take isl_basic_map
*bmap
)
542 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
544 return isl_basic_map_free(bmap
);
547 bmap
= isl_basic_map_cow(bmap
);
548 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 2 * n_div
);
551 for (i
= 0; i
< n_div
; ++i
) {
552 if (isl_int_is_zero(bmap
->div
[i
][0]))
554 bmap
= isl_basic_map_add_div_constraints(bmap
, i
);
560 __isl_give isl_basic_map
*isl_basic_map_from_local_space(
561 __isl_take isl_local_space
*ls
)
567 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
569 ls
= isl_local_space_free(ls
);
573 bmap
= isl_basic_map_alloc_space(isl_local_space_get_space(ls
),
574 n_div
, 0, 2 * n_div
);
576 for (i
= 0; i
< n_div
; ++i
)
577 if (isl_basic_map_alloc_div(bmap
) < 0)
580 for (i
= 0; i
< n_div
; ++i
)
581 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], ls
->div
->n_col
);
582 bmap
= add_known_div_constraints(bmap
);
584 isl_local_space_free(ls
);
587 isl_local_space_free(ls
);
588 isl_basic_map_free(bmap
);
592 __isl_give isl_basic_set
*isl_basic_set_from_local_space(
593 __isl_take isl_local_space
*ls
)
595 return isl_basic_map_from_local_space(ls
);
598 __isl_give isl_space
*isl_map_get_space(__isl_keep isl_map
*map
)
600 return isl_space_copy(isl_map_peek_space(map
));
603 __isl_give isl_space
*isl_set_get_space(__isl_keep isl_set
*set
)
607 return isl_space_copy(set
->dim
);
610 /* Return the space of "map".
611 * This may be either a copy or the space itself
612 * if there is only one reference to "map".
613 * This allows the space to be modified inplace
614 * if both the map and its space have only a single reference.
615 * The caller is not allowed to modify "map" between this call and
616 * a subsequent call to isl_map_restore_space.
617 * The only exception is that isl_map_free can be called instead.
619 static __isl_give isl_space
*isl_map_take_space(__isl_keep isl_map
*map
)
626 return isl_map_get_space(map
);
632 /* Set the space of "map" to "space", where the space of "map" may be missing
633 * due to a preceding call to isl_map_take_space.
634 * However, in this case, "map" only has a single reference and
635 * then the call to isl_map_cow has no effect.
637 static __isl_give isl_map
*isl_map_restore_space(__isl_take isl_map
*map
,
638 __isl_take isl_space
*space
)
643 if (map
->dim
== space
) {
644 isl_space_free(space
);
648 map
= isl_map_cow(map
);
651 isl_space_free(map
->dim
);
657 isl_space_free(space
);
661 __isl_give isl_basic_map
*isl_basic_map_set_tuple_name(
662 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
, const char *s
)
666 space
= isl_basic_map_take_space(bmap
);
667 space
= isl_space_set_tuple_name(space
, type
, s
);
668 bmap
= isl_basic_map_restore_space(bmap
, space
);
669 bmap
= isl_basic_map_finalize(bmap
);
673 __isl_give isl_basic_set
*isl_basic_set_set_tuple_name(
674 __isl_take isl_basic_set
*bset
, const char *s
)
676 return isl_basic_map_set_tuple_name(bset
, isl_dim_set
, s
);
679 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map
*bmap
,
680 enum isl_dim_type type
)
682 return bmap
? isl_space_get_tuple_name(bmap
->dim
, type
) : NULL
;
685 __isl_give isl_map
*isl_map_set_tuple_name(__isl_take isl_map
*map
,
686 enum isl_dim_type type
, const char *s
)
691 map
= isl_map_cow(map
);
695 for (i
= 0; i
< map
->n
; ++i
) {
696 map
->p
[i
] = isl_basic_map_set_tuple_name(map
->p
[i
], type
, s
);
701 space
= isl_map_take_space(map
);
702 space
= isl_space_set_tuple_name(space
, type
, s
);
703 map
= isl_map_restore_space(map
, space
);
711 /* Replace the identifier of the tuple of type "type" by "id".
713 __isl_give isl_basic_map
*isl_basic_map_set_tuple_id(
714 __isl_take isl_basic_map
*bmap
,
715 enum isl_dim_type type
, __isl_take isl_id
*id
)
719 space
= isl_basic_map_take_space(bmap
);
720 space
= isl_space_set_tuple_id(space
, type
, id
);
721 bmap
= isl_basic_map_restore_space(bmap
, space
);
722 bmap
= isl_basic_map_finalize(bmap
);
726 /* Replace the identifier of the tuple by "id".
728 __isl_give isl_basic_set
*isl_basic_set_set_tuple_id(
729 __isl_take isl_basic_set
*bset
, __isl_take isl_id
*id
)
731 return isl_basic_map_set_tuple_id(bset
, isl_dim_set
, id
);
734 /* Does the input or output tuple have a name?
736 isl_bool
isl_map_has_tuple_name(__isl_keep isl_map
*map
, enum isl_dim_type type
)
738 return map
? isl_space_has_tuple_name(map
->dim
, type
) : isl_bool_error
;
741 const char *isl_map_get_tuple_name(__isl_keep isl_map
*map
,
742 enum isl_dim_type type
)
744 return map
? isl_space_get_tuple_name(map
->dim
, type
) : NULL
;
747 __isl_give isl_set
*isl_set_set_tuple_name(__isl_take isl_set
*set
,
750 return set_from_map(isl_map_set_tuple_name(set_to_map(set
),
754 __isl_give isl_map
*isl_map_set_tuple_id(__isl_take isl_map
*map
,
755 enum isl_dim_type type
, __isl_take isl_id
*id
)
759 space
= isl_map_take_space(map
);
760 space
= isl_space_set_tuple_id(space
, type
, id
);
761 map
= isl_map_restore_space(map
, space
);
763 return isl_map_reset_space(map
, isl_map_get_space(map
));
766 /* Replace the identifier of the domain tuple of "map" by "id".
768 __isl_give isl_map
*isl_map_set_domain_tuple_id(__isl_take isl_map
*map
,
769 __isl_take isl_id
*id
)
771 return isl_map_set_tuple_id(map
, isl_dim_in
, id
);
774 /* Replace the identifier of the range tuple of "map" by "id".
776 __isl_give isl_map
*isl_map_set_range_tuple_id(__isl_take isl_map
*map
,
777 __isl_take isl_id
*id
)
779 return isl_map_set_tuple_id(map
, isl_dim_out
, id
);
782 __isl_give isl_set
*isl_set_set_tuple_id(__isl_take isl_set
*set
,
783 __isl_take isl_id
*id
)
785 return isl_map_set_tuple_id(set
, isl_dim_set
, id
);
788 __isl_give isl_map
*isl_map_reset_tuple_id(__isl_take isl_map
*map
,
789 enum isl_dim_type type
)
793 space
= isl_map_take_space(map
);
794 space
= isl_space_reset_tuple_id(space
, type
);
795 map
= isl_map_restore_space(map
, space
);
797 return isl_map_reset_space(map
, isl_map_get_space(map
));
800 __isl_give isl_set
*isl_set_reset_tuple_id(__isl_take isl_set
*set
)
802 return isl_map_reset_tuple_id(set
, isl_dim_set
);
805 isl_bool
isl_map_has_tuple_id(__isl_keep isl_map
*map
, enum isl_dim_type type
)
807 return map
? isl_space_has_tuple_id(map
->dim
, type
) : isl_bool_error
;
810 /* Does the domain tuple of "map" have an identifier?
812 isl_bool
isl_map_has_domain_tuple_id(__isl_keep isl_map
*map
)
814 return isl_map_has_tuple_id(map
, isl_dim_in
);
817 /* Does the range tuple of "map" have an identifier?
819 isl_bool
isl_map_has_range_tuple_id(__isl_keep isl_map
*map
)
821 return isl_map_has_tuple_id(map
, isl_dim_out
);
824 __isl_give isl_id
*isl_map_get_tuple_id(__isl_keep isl_map
*map
,
825 enum isl_dim_type type
)
827 return map
? isl_space_get_tuple_id(map
->dim
, type
) : NULL
;
830 /* Return the identifier of the domain tuple of "map", assuming it has one.
832 __isl_give isl_id
*isl_map_get_domain_tuple_id(__isl_keep isl_map
*map
)
834 return isl_map_get_tuple_id(map
, isl_dim_in
);
837 /* Return the identifier of the range tuple of "map", assuming it has one.
839 __isl_give isl_id
*isl_map_get_range_tuple_id(__isl_keep isl_map
*map
)
841 return isl_map_get_tuple_id(map
, isl_dim_out
);
844 isl_bool
isl_set_has_tuple_id(__isl_keep isl_set
*set
)
846 return isl_map_has_tuple_id(set
, isl_dim_set
);
849 __isl_give isl_id
*isl_set_get_tuple_id(__isl_keep isl_set
*set
)
851 return isl_map_get_tuple_id(set
, isl_dim_set
);
854 /* Does the set tuple have a name?
856 isl_bool
isl_set_has_tuple_name(__isl_keep isl_set
*set
)
859 return isl_bool_error
;
860 return isl_space_has_tuple_name(set
->dim
, isl_dim_set
);
864 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set
*bset
)
866 return bset
? isl_space_get_tuple_name(bset
->dim
, isl_dim_set
) : NULL
;
869 const char *isl_set_get_tuple_name(__isl_keep isl_set
*set
)
871 return set
? isl_space_get_tuple_name(set
->dim
, isl_dim_set
) : NULL
;
874 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map
*bmap
,
875 enum isl_dim_type type
, unsigned pos
)
877 return bmap
? isl_space_get_dim_name(bmap
->dim
, type
, pos
) : NULL
;
880 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set
*bset
,
881 enum isl_dim_type type
, unsigned pos
)
883 return bset
? isl_space_get_dim_name(bset
->dim
, type
, pos
) : NULL
;
886 /* Does the given dimension have a name?
888 isl_bool
isl_map_has_dim_name(__isl_keep isl_map
*map
,
889 enum isl_dim_type type
, unsigned pos
)
892 return isl_bool_error
;
893 return isl_space_has_dim_name(map
->dim
, type
, pos
);
896 const char *isl_map_get_dim_name(__isl_keep isl_map
*map
,
897 enum isl_dim_type type
, unsigned pos
)
899 return map
? isl_space_get_dim_name(map
->dim
, type
, pos
) : NULL
;
902 const char *isl_set_get_dim_name(__isl_keep isl_set
*set
,
903 enum isl_dim_type type
, unsigned pos
)
905 return set
? isl_space_get_dim_name(set
->dim
, type
, pos
) : NULL
;
908 /* Does the given dimension have a name?
910 isl_bool
isl_set_has_dim_name(__isl_keep isl_set
*set
,
911 enum isl_dim_type type
, unsigned pos
)
914 return isl_bool_error
;
915 return isl_space_has_dim_name(set
->dim
, type
, pos
);
918 __isl_give isl_basic_map
*isl_basic_map_set_dim_name(
919 __isl_take isl_basic_map
*bmap
,
920 enum isl_dim_type type
, unsigned pos
, const char *s
)
924 space
= isl_basic_map_take_space(bmap
);
925 space
= isl_space_set_dim_name(space
, type
, pos
, s
);
926 bmap
= isl_basic_map_restore_space(bmap
, space
);
927 return isl_basic_map_finalize(bmap
);
930 __isl_give isl_map
*isl_map_set_dim_name(__isl_take isl_map
*map
,
931 enum isl_dim_type type
, unsigned pos
, const char *s
)
936 map
= isl_map_cow(map
);
940 for (i
= 0; i
< map
->n
; ++i
) {
941 map
->p
[i
] = isl_basic_map_set_dim_name(map
->p
[i
], type
, pos
, s
);
946 space
= isl_map_take_space(map
);
947 space
= isl_space_set_dim_name(space
, type
, pos
, s
);
948 map
= isl_map_restore_space(map
, space
);
956 __isl_give isl_basic_set
*isl_basic_set_set_dim_name(
957 __isl_take isl_basic_set
*bset
,
958 enum isl_dim_type type
, unsigned pos
, const char *s
)
960 return bset_from_bmap(isl_basic_map_set_dim_name(bset_to_bmap(bset
),
964 __isl_give isl_set
*isl_set_set_dim_name(__isl_take isl_set
*set
,
965 enum isl_dim_type type
, unsigned pos
, const char *s
)
967 return set_from_map(isl_map_set_dim_name(set_to_map(set
),
971 isl_bool
isl_basic_map_has_dim_id(__isl_keep isl_basic_map
*bmap
,
972 enum isl_dim_type type
, unsigned pos
)
975 return isl_bool_error
;
976 return isl_space_has_dim_id(bmap
->dim
, type
, pos
);
979 __isl_give isl_id
*isl_basic_set_get_dim_id(__isl_keep isl_basic_set
*bset
,
980 enum isl_dim_type type
, unsigned pos
)
982 return bset
? isl_space_get_dim_id(bset
->dim
, type
, pos
) : NULL
;
985 isl_bool
isl_map_has_dim_id(__isl_keep isl_map
*map
,
986 enum isl_dim_type type
, unsigned pos
)
988 return map
? isl_space_has_dim_id(map
->dim
, type
, pos
) : isl_bool_error
;
991 __isl_give isl_id
*isl_map_get_dim_id(__isl_keep isl_map
*map
,
992 enum isl_dim_type type
, unsigned pos
)
994 return map
? isl_space_get_dim_id(map
->dim
, type
, pos
) : NULL
;
997 isl_bool
isl_set_has_dim_id(__isl_keep isl_set
*set
,
998 enum isl_dim_type type
, unsigned pos
)
1000 return isl_map_has_dim_id(set
, type
, pos
);
1003 __isl_give isl_id
*isl_set_get_dim_id(__isl_keep isl_set
*set
,
1004 enum isl_dim_type type
, unsigned pos
)
1006 return isl_map_get_dim_id(set
, type
, pos
);
1009 __isl_give isl_map
*isl_map_set_dim_id(__isl_take isl_map
*map
,
1010 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
1014 space
= isl_map_take_space(map
);
1015 space
= isl_space_set_dim_id(space
, type
, pos
, id
);
1016 map
= isl_map_restore_space(map
, space
);
1018 return isl_map_reset_space(map
, isl_map_get_space(map
));
1021 __isl_give isl_set
*isl_set_set_dim_id(__isl_take isl_set
*set
,
1022 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
1024 return isl_map_set_dim_id(set
, type
, pos
, id
);
1027 int isl_map_find_dim_by_id(__isl_keep isl_map
*map
, enum isl_dim_type type
,
1028 __isl_keep isl_id
*id
)
1032 return isl_space_find_dim_by_id(map
->dim
, type
, id
);
1035 int isl_set_find_dim_by_id(__isl_keep isl_set
*set
, enum isl_dim_type type
,
1036 __isl_keep isl_id
*id
)
1038 return isl_map_find_dim_by_id(set
, type
, id
);
1041 /* Return the position of the dimension of the given type and name
1043 * Return -1 if no such dimension can be found.
1045 int isl_basic_map_find_dim_by_name(__isl_keep isl_basic_map
*bmap
,
1046 enum isl_dim_type type
, const char *name
)
1050 return isl_space_find_dim_by_name(bmap
->dim
, type
, name
);
1053 int isl_map_find_dim_by_name(__isl_keep isl_map
*map
, enum isl_dim_type type
,
1058 return isl_space_find_dim_by_name(map
->dim
, type
, name
);
1061 int isl_set_find_dim_by_name(__isl_keep isl_set
*set
, enum isl_dim_type type
,
1064 return isl_map_find_dim_by_name(set
, type
, name
);
1067 /* Check whether equality i of bset is a pure stride constraint
1068 * on a single dimension, i.e., of the form
1072 * with k a constant and e an existentially quantified variable.
1074 isl_bool
isl_basic_set_eq_is_stride(__isl_keep isl_basic_set
*bset
, int i
)
1082 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
1083 d
= isl_basic_set_dim(bset
, isl_dim_set
);
1084 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
1085 if (nparam
< 0 || d
< 0 || n_div
< 0)
1086 return isl_bool_error
;
1088 if (!isl_int_is_zero(bset
->eq
[i
][0]))
1089 return isl_bool_false
;
1091 if (isl_seq_first_non_zero(bset
->eq
[i
] + 1, nparam
) != -1)
1092 return isl_bool_false
;
1093 pos1
= isl_seq_first_non_zero(bset
->eq
[i
] + 1 + nparam
, d
);
1095 return isl_bool_false
;
1096 if (isl_seq_first_non_zero(bset
->eq
[i
] + 1 + nparam
+ pos1
+ 1,
1097 d
- pos1
- 1) != -1)
1098 return isl_bool_false
;
1100 pos2
= isl_seq_first_non_zero(bset
->eq
[i
] + 1 + nparam
+ d
, n_div
);
1102 return isl_bool_false
;
1103 if (isl_seq_first_non_zero(bset
->eq
[i
] + 1 + nparam
+ d
+ pos2
+ 1,
1104 n_div
- pos2
- 1) != -1)
1105 return isl_bool_false
;
1106 if (!isl_int_is_one(bset
->eq
[i
][1 + nparam
+ pos1
]) &&
1107 !isl_int_is_negone(bset
->eq
[i
][1 + nparam
+ pos1
]))
1108 return isl_bool_false
;
1110 return isl_bool_true
;
1113 /* Reset the user pointer on all identifiers of parameters and tuples
1114 * of the space of "map".
1116 __isl_give isl_map
*isl_map_reset_user(__isl_take isl_map
*map
)
1120 space
= isl_map_get_space(map
);
1121 space
= isl_space_reset_user(space
);
1122 map
= isl_map_reset_space(map
, space
);
1127 /* Reset the user pointer on all identifiers of parameters and tuples
1128 * of the space of "set".
1130 __isl_give isl_set
*isl_set_reset_user(__isl_take isl_set
*set
)
1132 return isl_map_reset_user(set
);
1135 isl_bool
isl_basic_map_is_rational(__isl_keep isl_basic_map
*bmap
)
1138 return isl_bool_error
;
1139 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
1142 /* Has "map" been marked as a rational map?
1143 * In particular, have all basic maps in "map" been marked this way?
1144 * An empty map is not considered to be rational.
1145 * Maps where only some of the basic maps are marked rational
1148 isl_bool
isl_map_is_rational(__isl_keep isl_map
*map
)
1154 return isl_bool_error
;
1156 return isl_bool_false
;
1157 rational
= isl_basic_map_is_rational(map
->p
[0]);
1160 for (i
= 1; i
< map
->n
; ++i
) {
1161 isl_bool rational_i
;
1163 rational_i
= isl_basic_map_is_rational(map
->p
[i
]);
1166 if (rational
!= rational_i
)
1167 isl_die(isl_map_get_ctx(map
), isl_error_unsupported
,
1168 "mixed rational and integer basic maps "
1169 "not supported", return isl_bool_error
);
1175 /* Has "set" been marked as a rational set?
1176 * In particular, have all basic set in "set" been marked this way?
1177 * An empty set is not considered to be rational.
1178 * Sets where only some of the basic sets are marked rational
1181 isl_bool
isl_set_is_rational(__isl_keep isl_set
*set
)
1183 return isl_map_is_rational(set
);
1186 int isl_basic_set_is_rational(__isl_keep isl_basic_set
*bset
)
1188 return isl_basic_map_is_rational(bset
);
1191 /* Does "bmap" contain any rational points?
1193 * If "bmap" has an equality for each dimension, equating the dimension
1194 * to an integer constant, then it has no rational points, even if it
1195 * is marked as rational.
1197 isl_bool
isl_basic_map_has_rational(__isl_keep isl_basic_map
*bmap
)
1199 isl_bool has_rational
= isl_bool_true
;
1203 return isl_bool_error
;
1204 if (isl_basic_map_plain_is_empty(bmap
))
1205 return isl_bool_false
;
1206 if (!isl_basic_map_is_rational(bmap
))
1207 return isl_bool_false
;
1208 bmap
= isl_basic_map_copy(bmap
);
1209 bmap
= isl_basic_map_implicit_equalities(bmap
);
1210 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
1212 return isl_bool_error
;
1213 if (bmap
->n_eq
== total
) {
1215 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
1216 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1, total
);
1219 if (!isl_int_is_one(bmap
->eq
[i
][1 + j
]) &&
1220 !isl_int_is_negone(bmap
->eq
[i
][1 + j
]))
1222 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + j
+ 1,
1227 if (i
== bmap
->n_eq
)
1228 has_rational
= isl_bool_false
;
1230 isl_basic_map_free(bmap
);
1232 return has_rational
;
1235 /* Does "map" contain any rational points?
1237 isl_bool
isl_map_has_rational(__isl_keep isl_map
*map
)
1240 isl_bool has_rational
;
1243 return isl_bool_error
;
1244 for (i
= 0; i
< map
->n
; ++i
) {
1245 has_rational
= isl_basic_map_has_rational(map
->p
[i
]);
1246 if (has_rational
< 0 || has_rational
)
1247 return has_rational
;
1249 return isl_bool_false
;
1252 /* Does "set" contain any rational points?
1254 isl_bool
isl_set_has_rational(__isl_keep isl_set
*set
)
1256 return isl_map_has_rational(set
);
1259 /* Is this basic set a parameter domain?
1261 isl_bool
isl_basic_set_is_params(__isl_keep isl_basic_set
*bset
)
1264 return isl_bool_error
;
1265 return isl_space_is_params(bset
->dim
);
1268 /* Is this set a parameter domain?
1270 isl_bool
isl_set_is_params(__isl_keep isl_set
*set
)
1273 return isl_bool_error
;
1274 return isl_space_is_params(set
->dim
);
1277 /* Is this map actually a parameter domain?
1278 * Users should never call this function. Outside of isl,
1279 * a map can never be a parameter domain.
1281 isl_bool
isl_map_is_params(__isl_keep isl_map
*map
)
1284 return isl_bool_error
;
1285 return isl_space_is_params(map
->dim
);
1288 static __isl_give isl_basic_map
*basic_map_init(isl_ctx
*ctx
,
1289 __isl_take isl_basic_map
*bmap
, unsigned extra
,
1290 unsigned n_eq
, unsigned n_ineq
)
1293 isl_space
*space
= isl_basic_map_peek_space(bmap
);
1294 isl_size n_var
= isl_space_dim(space
, isl_dim_all
);
1295 size_t row_size
= 1 + n_var
+ extra
;
1301 return isl_basic_map_free(bmap
);
1303 bmap
->block
= isl_blk_alloc(ctx
, (n_ineq
+ n_eq
) * row_size
);
1304 if (isl_blk_is_error(bmap
->block
))
1307 bmap
->ineq
= isl_alloc_array(ctx
, isl_int
*, n_ineq
+ n_eq
);
1308 if ((n_ineq
+ n_eq
) && !bmap
->ineq
)
1312 bmap
->block2
= isl_blk_empty();
1315 bmap
->block2
= isl_blk_alloc(ctx
, extra
* (1 + row_size
));
1316 if (isl_blk_is_error(bmap
->block2
))
1319 bmap
->div
= isl_alloc_array(ctx
, isl_int
*, extra
);
1324 for (i
= 0; i
< n_ineq
+ n_eq
; ++i
)
1325 bmap
->ineq
[i
] = bmap
->block
.data
+ i
* row_size
;
1327 for (i
= 0; i
< extra
; ++i
)
1328 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + row_size
);
1332 bmap
->c_size
= n_eq
+ n_ineq
;
1333 bmap
->eq
= bmap
->ineq
+ n_ineq
;
1334 bmap
->extra
= extra
;
1338 bmap
->sample
= NULL
;
1342 isl_basic_map_free(bmap
);
1346 __isl_give isl_basic_set
*isl_basic_set_alloc(isl_ctx
*ctx
,
1347 unsigned nparam
, unsigned dim
, unsigned extra
,
1348 unsigned n_eq
, unsigned n_ineq
)
1350 struct isl_basic_map
*bmap
;
1353 space
= isl_space_set_alloc(ctx
, nparam
, dim
);
1357 bmap
= isl_basic_map_alloc_space(space
, extra
, n_eq
, n_ineq
);
1358 return bset_from_bmap(bmap
);
1361 __isl_give isl_basic_set
*isl_basic_set_alloc_space(__isl_take isl_space
*space
,
1362 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
1364 struct isl_basic_map
*bmap
;
1367 isl_assert(space
->ctx
, space
->n_in
== 0, goto error
);
1368 bmap
= isl_basic_map_alloc_space(space
, extra
, n_eq
, n_ineq
);
1369 return bset_from_bmap(bmap
);
1371 isl_space_free(space
);
1375 __isl_give isl_basic_map
*isl_basic_map_alloc_space(__isl_take isl_space
*space
,
1376 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
1378 struct isl_basic_map
*bmap
;
1382 bmap
= isl_calloc_type(space
->ctx
, struct isl_basic_map
);
1387 return basic_map_init(space
->ctx
, bmap
, extra
, n_eq
, n_ineq
);
1389 isl_space_free(space
);
1393 __isl_give isl_basic_map
*isl_basic_map_alloc(isl_ctx
*ctx
,
1394 unsigned nparam
, unsigned in
, unsigned out
, unsigned extra
,
1395 unsigned n_eq
, unsigned n_ineq
)
1397 struct isl_basic_map
*bmap
;
1400 space
= isl_space_alloc(ctx
, nparam
, in
, out
);
1404 bmap
= isl_basic_map_alloc_space(space
, extra
, n_eq
, n_ineq
);
1408 static __isl_give isl_basic_map
*dup_constraints(__isl_take isl_basic_map
*dst
,
1409 __isl_keep isl_basic_map
*src
)
1412 isl_size total
= isl_basic_map_dim(src
, isl_dim_all
);
1414 if (!dst
|| total
< 0)
1415 return isl_basic_map_free(dst
);
1417 for (i
= 0; i
< src
->n_eq
; ++i
) {
1418 int j
= isl_basic_map_alloc_equality(dst
);
1420 return isl_basic_map_free(dst
);
1421 isl_seq_cpy(dst
->eq
[j
], src
->eq
[i
], 1+total
);
1424 for (i
= 0; i
< src
->n_ineq
; ++i
) {
1425 int j
= isl_basic_map_alloc_inequality(dst
);
1427 return isl_basic_map_free(dst
);
1428 isl_seq_cpy(dst
->ineq
[j
], src
->ineq
[i
], 1+total
);
1431 for (i
= 0; i
< src
->n_div
; ++i
) {
1432 int j
= isl_basic_map_alloc_div(dst
);
1434 return isl_basic_map_free(dst
);
1435 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
);
1437 ISL_F_SET(dst
, ISL_BASIC_SET_FINAL
);
1441 __isl_give isl_basic_map
*isl_basic_map_dup(__isl_keep isl_basic_map
*bmap
)
1443 struct isl_basic_map
*dup
;
1447 dup
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
1448 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
1449 dup
= dup_constraints(dup
, bmap
);
1452 dup
->flags
= bmap
->flags
;
1453 dup
->sample
= isl_vec_copy(bmap
->sample
);
1457 __isl_give isl_basic_set
*isl_basic_set_dup(__isl_keep isl_basic_set
*bset
)
1459 struct isl_basic_map
*dup
;
1461 dup
= isl_basic_map_dup(bset_to_bmap(bset
));
1462 return bset_from_bmap(dup
);
1465 __isl_give isl_basic_set
*isl_basic_set_copy(__isl_keep isl_basic_set
*bset
)
1470 if (ISL_F_ISSET(bset
, ISL_BASIC_SET_FINAL
)) {
1474 return isl_basic_set_dup(bset
);
1477 __isl_give isl_set
*isl_set_copy(__isl_keep isl_set
*set
)
1486 __isl_give isl_basic_map
*isl_basic_map_copy(__isl_keep isl_basic_map
*bmap
)
1491 if (ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
)) {
1495 bmap
= isl_basic_map_dup(bmap
);
1497 ISL_F_SET(bmap
, ISL_BASIC_SET_FINAL
);
1501 __isl_give isl_map
*isl_map_copy(__isl_keep isl_map
*map
)
1510 __isl_null isl_basic_map
*isl_basic_map_free(__isl_take isl_basic_map
*bmap
)
1515 if (--bmap
->ref
> 0)
1518 isl_ctx_deref(bmap
->ctx
);
1520 isl_blk_free(bmap
->ctx
, bmap
->block2
);
1522 isl_blk_free(bmap
->ctx
, bmap
->block
);
1523 isl_vec_free(bmap
->sample
);
1524 isl_space_free(bmap
->dim
);
1530 __isl_null isl_basic_set
*isl_basic_set_free(__isl_take isl_basic_set
*bset
)
1532 return isl_basic_map_free(bset_to_bmap(bset
));
1535 static int room_for_con(__isl_keep isl_basic_map
*bmap
, unsigned n
)
1537 return bmap
->n_eq
+ bmap
->n_ineq
+ n
<= bmap
->c_size
;
1540 /* Check that "bset" does not involve any parameters.
1542 isl_stat
isl_basic_set_check_no_params(__isl_keep isl_basic_set
*bset
)
1546 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
1548 return isl_stat_error
;
1550 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
1551 "basic set should not have any parameters",
1552 return isl_stat_error
);
1556 /* Check that "bset" does not involve any local variables.
1558 isl_stat
isl_basic_set_check_no_locals(__isl_keep isl_basic_set
*bset
)
1562 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
1564 return isl_stat_error
;
1566 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
1567 "basic set should not have any local variables",
1568 return isl_stat_error
);
1573 #define TYPE isl_map
1575 #include "isl_check_named_params_templ.c"
1578 #define TYPE isl_basic_map
1581 #include "isl_check_named_params_templ.c"
1583 /* Check that "bmap1" and "bmap2" have the same parameters,
1584 * reporting an error if they do not.
1586 static isl_stat
isl_basic_map_check_equal_params(
1587 __isl_keep isl_basic_map
*bmap1
, __isl_keep isl_basic_map
*bmap2
)
1591 match
= isl_basic_map_has_equal_params(bmap1
, bmap2
);
1593 return isl_stat_error
;
1595 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
1596 "parameters don't match", return isl_stat_error
);
1601 #define TYPE isl_map
1603 #include "isl_align_params_bin_templ.c"
1608 #define ARG1 isl_map
1610 #define ARG2 isl_set
1612 #include "isl_align_params_templ.c"
1614 isl_bool
isl_map_align_params_map_map_and_test(__isl_keep isl_map
*map1
,
1615 __isl_keep isl_map
*map2
,
1616 isl_bool (*fn
)(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
))
1621 return isl_bool_error
;
1622 if (isl_map_has_equal_params(map1
, map2
))
1623 return fn(map1
, map2
);
1624 if (isl_map_check_named_params(map1
) < 0)
1625 return isl_bool_error
;
1626 if (isl_map_check_named_params(map2
) < 0)
1627 return isl_bool_error
;
1628 map1
= isl_map_copy(map1
);
1629 map2
= isl_map_copy(map2
);
1630 map1
= isl_map_align_params(map1
, isl_map_get_space(map2
));
1631 map2
= isl_map_align_params(map2
, isl_map_get_space(map1
));
1638 int isl_basic_map_alloc_equality(__isl_keep isl_basic_map
*bmap
)
1641 struct isl_ctx
*ctx
;
1643 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
1647 isl_assert(ctx
, room_for_con(bmap
, 1), return -1);
1648 isl_assert(ctx
, (bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
<= bmap
->c_size
,
1650 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1651 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1652 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1653 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1654 if ((bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
== bmap
->c_size
) {
1656 int j
= isl_basic_map_alloc_inequality(bmap
);
1660 bmap
->ineq
[j
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1661 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
1668 isl_seq_clr(bmap
->eq
[bmap
->n_eq
] + 1 + total
,
1669 bmap
->extra
- bmap
->n_div
);
1670 return bmap
->n_eq
++;
1673 int isl_basic_set_alloc_equality(__isl_keep isl_basic_set
*bset
)
1675 return isl_basic_map_alloc_equality(bset_to_bmap(bset
));
1678 __isl_give isl_basic_map
*isl_basic_map_free_equality(
1679 __isl_take isl_basic_map
*bmap
, unsigned n
)
1684 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
1685 "invalid number of equalities",
1686 isl_basic_map_free(bmap
));
1691 __isl_give isl_basic_set
*isl_basic_set_free_equality(
1692 __isl_take isl_basic_set
*bset
, unsigned n
)
1694 return bset_from_bmap(isl_basic_map_free_equality(bset_to_bmap(bset
),
1698 /* Drop the equality constraint at position "pos",
1699 * preserving the order of the other equality constraints.
1701 int isl_basic_map_drop_equality(__isl_keep isl_basic_map
*bmap
, unsigned pos
)
1708 isl_assert(bmap
->ctx
, pos
< bmap
->n_eq
, return -1);
1712 for (r
= pos
; r
< bmap
->n_eq
; ++r
)
1713 bmap
->eq
[r
] = bmap
->eq
[r
+ 1];
1714 bmap
->eq
[bmap
->n_eq
] = t
;
1719 /* Turn inequality "pos" of "bmap" into an equality.
1721 * In particular, we move the inequality in front of the equalities
1722 * and move the last inequality in the position of the moved inequality.
1723 * Note that isl_tab_make_equalities_explicit depends on this particular
1724 * change in the ordering of the constraints.
1726 void isl_basic_map_inequality_to_equality(
1727 __isl_keep isl_basic_map
*bmap
, unsigned pos
)
1731 t
= bmap
->ineq
[pos
];
1732 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1733 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
1738 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1739 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
1740 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1741 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1744 static int room_for_ineq(__isl_keep isl_basic_map
*bmap
, unsigned n
)
1746 return bmap
->n_ineq
+ n
<= bmap
->eq
- bmap
->ineq
;
1749 int isl_basic_map_alloc_inequality(__isl_keep isl_basic_map
*bmap
)
1752 struct isl_ctx
*ctx
;
1754 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
1758 isl_assert(ctx
, room_for_ineq(bmap
, 1), return -1);
1759 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1760 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1761 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
1762 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1763 isl_seq_clr(bmap
->ineq
[bmap
->n_ineq
] + 1 + total
,
1764 bmap
->extra
- bmap
->n_div
);
1765 return bmap
->n_ineq
++;
1768 int isl_basic_set_alloc_inequality(__isl_keep isl_basic_set
*bset
)
1770 return isl_basic_map_alloc_inequality(bset_to_bmap(bset
));
1773 __isl_give isl_basic_map
*isl_basic_map_free_inequality(
1774 __isl_take isl_basic_map
*bmap
, unsigned n
)
1778 if (n
> bmap
->n_ineq
)
1779 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
1780 "invalid number of inequalities",
1781 return isl_basic_map_free(bmap
));
1786 __isl_give isl_basic_set
*isl_basic_set_free_inequality(
1787 __isl_take isl_basic_set
*bset
, unsigned n
)
1789 return bset_from_bmap(isl_basic_map_free_inequality(bset_to_bmap(bset
),
1793 int isl_basic_map_drop_inequality(__isl_keep isl_basic_map
*bmap
, unsigned pos
)
1798 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
1800 if (pos
!= bmap
->n_ineq
- 1) {
1801 t
= bmap
->ineq
[pos
];
1802 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1803 bmap
->ineq
[bmap
->n_ineq
- 1] = t
;
1804 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
1810 int isl_basic_set_drop_inequality(__isl_keep isl_basic_set
*bset
, unsigned pos
)
1812 return isl_basic_map_drop_inequality(bset_to_bmap(bset
), pos
);
1815 __isl_give isl_basic_map
*isl_basic_map_add_eq(__isl_take isl_basic_map
*bmap
,
1822 empty
= isl_basic_map_plain_is_empty(bmap
);
1824 return isl_basic_map_free(bmap
);
1828 bmap
= isl_basic_map_cow(bmap
);
1829 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
1830 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
1832 return isl_basic_map_free(bmap
);
1833 k
= isl_basic_map_alloc_equality(bmap
);
1836 isl_seq_cpy(bmap
->eq
[k
], eq
, 1 + total
);
1839 isl_basic_map_free(bmap
);
1843 __isl_give isl_basic_set
*isl_basic_set_add_eq(__isl_take isl_basic_set
*bset
,
1846 return bset_from_bmap(isl_basic_map_add_eq(bset_to_bmap(bset
), eq
));
1849 __isl_give isl_basic_map
*isl_basic_map_add_ineq(__isl_take isl_basic_map
*bmap
,
1855 bmap
= isl_basic_map_cow(bmap
);
1856 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
1857 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
1859 return isl_basic_map_free(bmap
);
1860 k
= isl_basic_map_alloc_inequality(bmap
);
1863 isl_seq_cpy(bmap
->ineq
[k
], ineq
, 1 + total
);
1866 isl_basic_map_free(bmap
);
1870 __isl_give isl_basic_set
*isl_basic_set_add_ineq(__isl_take isl_basic_set
*bset
,
1873 return bset_from_bmap(isl_basic_map_add_ineq(bset_to_bmap(bset
), ineq
));
1876 int isl_basic_map_alloc_div(__isl_keep isl_basic_map
*bmap
)
1880 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
1883 isl_assert(bmap
->ctx
, bmap
->n_div
< bmap
->extra
, return -1);
1884 isl_seq_clr(bmap
->div
[bmap
->n_div
] + 1 + 1 + total
,
1885 bmap
->extra
- bmap
->n_div
);
1886 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1887 return bmap
->n_div
++;
1890 int isl_basic_set_alloc_div(__isl_keep isl_basic_set
*bset
)
1892 return isl_basic_map_alloc_div(bset_to_bmap(bset
));
1896 #define TYPE isl_basic_map
1897 #include "check_type_range_templ.c"
1899 /* Check that there are "n" dimensions of type "type" starting at "first"
1902 isl_stat
isl_basic_set_check_range(__isl_keep isl_basic_set
*bset
,
1903 enum isl_dim_type type
, unsigned first
, unsigned n
)
1905 return isl_basic_map_check_range(bset_to_bmap(bset
),
1909 /* Insert an extra integer division, prescribed by "div", to "bmap"
1910 * at (integer division) position "pos".
1912 * The integer division is first added at the end and then moved
1913 * into the right position.
1915 __isl_give isl_basic_map
*isl_basic_map_insert_div(
1916 __isl_take isl_basic_map
*bmap
, int pos
, __isl_keep isl_vec
*div
)
1921 bmap
= isl_basic_map_cow(bmap
);
1922 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
1923 if (total
< 0 || !div
)
1924 return isl_basic_map_free(bmap
);
1926 if (div
->size
!= 1 + 1 + total
)
1927 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
1928 "unexpected size", return isl_basic_map_free(bmap
));
1929 if (isl_basic_map_check_range(bmap
, isl_dim_div
, pos
, 0) < 0)
1930 return isl_basic_map_free(bmap
);
1932 bmap
= isl_basic_map_extend(bmap
, 1, 0, 2);
1933 k
= isl_basic_map_alloc_div(bmap
);
1935 return isl_basic_map_free(bmap
);
1936 isl_seq_cpy(bmap
->div
[k
], div
->el
, div
->size
);
1937 isl_int_set_si(bmap
->div
[k
][div
->size
], 0);
1939 for (i
= k
; i
> pos
; --i
)
1940 bmap
= isl_basic_map_swap_div(bmap
, i
, i
- 1);
1945 isl_stat
isl_basic_map_free_div(__isl_keep isl_basic_map
*bmap
, unsigned n
)
1948 return isl_stat_error
;
1949 isl_assert(bmap
->ctx
, n
<= bmap
->n_div
, return isl_stat_error
);
1954 static __isl_give isl_basic_map
*add_constraints(
1955 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
,
1956 unsigned i_pos
, unsigned o_pos
)
1958 isl_size total
, n_param
, n_in
, n_out
, n_div
;
1959 unsigned o_in
, o_out
;
1962 struct isl_dim_map
*dim_map
;
1964 space
= isl_basic_map_peek_space(bmap2
);
1965 if (!bmap1
|| !space
)
1968 total
= isl_basic_map_dim(bmap1
, isl_dim_all
);
1969 n_param
= isl_basic_map_dim(bmap2
, isl_dim_param
);
1970 n_in
= isl_basic_map_dim(bmap2
, isl_dim_in
);
1971 o_in
= isl_basic_map_offset(bmap1
, isl_dim_in
) - 1 + i_pos
;
1972 n_out
= isl_basic_map_dim(bmap2
, isl_dim_out
);
1973 o_out
= isl_basic_map_offset(bmap1
, isl_dim_out
) - 1 + o_pos
;
1974 n_div
= isl_basic_map_dim(bmap2
, isl_dim_div
);
1975 if (total
< 0 || n_param
< 0 || n_in
< 0 || n_out
< 0 || n_div
< 0)
1977 ctx
= isl_basic_map_get_ctx(bmap1
);
1978 dim_map
= isl_dim_map_alloc(ctx
, total
+ n_div
);
1979 isl_dim_map_dim_range(dim_map
, space
, isl_dim_param
, 0, n_param
, 0);
1980 isl_dim_map_dim_range(dim_map
, space
, isl_dim_in
, 0, n_in
, o_in
);
1981 isl_dim_map_dim_range(dim_map
, space
, isl_dim_out
, 0, n_out
, o_out
);
1982 isl_dim_map_div(dim_map
, bmap2
, total
);
1984 return isl_basic_map_add_constraints_dim_map(bmap1
, bmap2
, dim_map
);
1986 isl_basic_map_free(bmap1
);
1987 isl_basic_map_free(bmap2
);
1991 __isl_give isl_basic_map
*isl_basic_map_extend(__isl_take isl_basic_map
*base
,
1992 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
1995 struct isl_basic_map
*ext
;
2002 dims_ok
= base
->extra
>= base
->n_div
+ extra
;
2004 if (dims_ok
&& room_for_con(base
, n_eq
+ n_ineq
) &&
2005 room_for_ineq(base
, n_ineq
))
2008 extra
+= base
->extra
;
2010 n_ineq
+= base
->n_ineq
;
2012 space
= isl_basic_map_get_space(base
);
2013 ext
= isl_basic_map_alloc_space(space
, extra
, n_eq
, n_ineq
);
2018 ext
->sample
= isl_vec_copy(base
->sample
);
2019 flags
= base
->flags
;
2020 ext
= add_constraints(ext
, base
, 0, 0);
2023 ISL_F_CLR(ext
, ISL_BASIC_SET_FINAL
);
2029 isl_basic_map_free(base
);
2033 __isl_give isl_basic_set
*isl_basic_set_extend(__isl_take isl_basic_set
*base
,
2034 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
2036 return bset_from_bmap(isl_basic_map_extend(bset_to_bmap(base
),
2037 extra
, n_eq
, n_ineq
));
2040 __isl_give isl_basic_map
*isl_basic_map_extend_constraints(
2041 __isl_take isl_basic_map
*base
, unsigned n_eq
, unsigned n_ineq
)
2043 return isl_basic_map_extend(base
, 0, n_eq
, n_ineq
);
2046 __isl_give isl_basic_set
*isl_basic_set_extend_constraints(
2047 __isl_take isl_basic_set
*base
, unsigned n_eq
, unsigned n_ineq
)
2049 isl_basic_map
*bmap
= bset_to_bmap(base
);
2050 bmap
= isl_basic_map_extend_constraints(bmap
, n_eq
, n_ineq
);
2051 return bset_from_bmap(bmap
);
2054 __isl_give isl_basic_set
*isl_basic_set_cow(__isl_take isl_basic_set
*bset
)
2056 return bset_from_bmap(isl_basic_map_cow(bset_to_bmap(bset
)));
2059 __isl_give isl_basic_map
*isl_basic_map_cow(__isl_take isl_basic_map
*bmap
)
2064 if (bmap
->ref
> 1) {
2066 bmap
= isl_basic_map_dup(bmap
);
2069 ISL_F_CLR(bmap
, ISL_BASIC_SET_FINAL
);
2070 ISL_F_CLR(bmap
, ISL_BASIC_MAP_REDUCED_COEFFICIENTS
);
2075 /* Clear all cached information in "map", either because it is about
2076 * to be modified or because it is being freed.
2077 * Always return the same pointer that is passed in.
2078 * This is needed for the use in isl_map_free.
2080 static __isl_give isl_map
*clear_caches(__isl_take isl_map
*map
)
2082 isl_basic_map_free(map
->cached_simple_hull
[0]);
2083 isl_basic_map_free(map
->cached_simple_hull
[1]);
2084 map
->cached_simple_hull
[0] = NULL
;
2085 map
->cached_simple_hull
[1] = NULL
;
2089 __isl_give isl_set
*isl_set_cow(__isl_take isl_set
*set
)
2091 return isl_map_cow(set
);
2094 /* Return an isl_map that is equal to "map" and that has only
2095 * a single reference.
2097 * If the original input already has only one reference, then
2098 * simply return it, but clear all cached information, since
2099 * it may be rendered invalid by the operations that will be
2100 * performed on the result.
2102 * Otherwise, create a duplicate (without any cached information).
2104 __isl_give isl_map
*isl_map_cow(__isl_take isl_map
*map
)
2110 return clear_caches(map
);
2112 return isl_map_dup(map
);
2115 static void swap_vars(struct isl_blk blk
, isl_int
*a
,
2116 unsigned a_len
, unsigned b_len
)
2118 isl_seq_cpy(blk
.data
, a
+a_len
, b_len
);
2119 isl_seq_cpy(blk
.data
+b_len
, a
, a_len
);
2120 isl_seq_cpy(a
, blk
.data
, b_len
+a_len
);
2123 static __isl_give isl_basic_map
*isl_basic_map_swap_vars(
2124 __isl_take isl_basic_map
*bmap
, unsigned pos
, unsigned n1
, unsigned n2
)
2129 if (isl_basic_map_check_range(bmap
, isl_dim_all
, pos
- 1, n1
+ n2
) < 0)
2132 if (n1
== 0 || n2
== 0)
2135 bmap
= isl_basic_map_cow(bmap
);
2139 blk
= isl_blk_alloc(bmap
->ctx
, n1
+ n2
);
2140 if (isl_blk_is_error(blk
))
2143 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2145 bmap
->eq
[i
] + pos
, n1
, n2
);
2147 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2149 bmap
->ineq
[i
] + pos
, n1
, n2
);
2151 for (i
= 0; i
< bmap
->n_div
; ++i
)
2153 bmap
->div
[i
]+1 + pos
, n1
, n2
);
2155 isl_blk_free(bmap
->ctx
, blk
);
2157 ISL_F_CLR(bmap
, ISL_BASIC_SET_SORTED
);
2158 bmap
= isl_basic_map_gauss(bmap
, NULL
);
2159 return isl_basic_map_finalize(bmap
);
2161 isl_basic_map_free(bmap
);
2165 /* The given basic map has turned out to be empty.
2166 * Explicitly mark it as such and change the representation
2167 * to a canonical representation of the empty basic map.
2168 * Since the basic map has conflicting constraints,
2169 * it must have at least one constraint, except perhaps
2170 * if it was already explicitly marked as being empty.
2171 * Do nothing in the latter case, i.e., if it has been marked empty and
2172 * has no constraints.
2174 __isl_give isl_basic_map
*isl_basic_map_set_to_empty(
2175 __isl_take isl_basic_map
*bmap
)
2182 n
= isl_basic_map_n_constraint(bmap
);
2183 empty
= isl_basic_map_plain_is_empty(bmap
);
2184 if (n
< 0 || empty
< 0)
2185 return isl_basic_map_free(bmap
);
2186 if (n
== 0 && empty
)
2188 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
2190 return isl_basic_map_free(bmap
);
2191 if (isl_basic_map_free_div(bmap
, bmap
->n_div
) < 0)
2192 return isl_basic_map_free(bmap
);
2193 bmap
= isl_basic_map_free_inequality(bmap
, bmap
->n_ineq
);
2196 if (bmap
->n_eq
> 0) {
2197 bmap
= isl_basic_map_free_equality(bmap
, bmap
->n_eq
- 1);
2201 i
= isl_basic_map_alloc_equality(bmap
);
2205 isl_int_set_si(bmap
->eq
[i
][0], 1);
2206 isl_seq_clr(bmap
->eq
[i
]+1, total
);
2207 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
2208 isl_vec_free(bmap
->sample
);
2209 bmap
->sample
= NULL
;
2210 return isl_basic_map_finalize(bmap
);
2212 isl_basic_map_free(bmap
);
2216 __isl_give isl_basic_set
*isl_basic_set_set_to_empty(
2217 __isl_take isl_basic_set
*bset
)
2219 return bset_from_bmap(isl_basic_map_set_to_empty(bset_to_bmap(bset
)));
2222 __isl_give isl_basic_map
*isl_basic_map_set_rational(
2223 __isl_take isl_basic_map
*bmap
)
2228 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
2231 bmap
= isl_basic_map_cow(bmap
);
2235 ISL_F_SET(bmap
, ISL_BASIC_MAP_RATIONAL
);
2237 return isl_basic_map_finalize(bmap
);
2240 __isl_give isl_basic_set
*isl_basic_set_set_rational(
2241 __isl_take isl_basic_set
*bset
)
2243 return isl_basic_map_set_rational(bset
);
2246 __isl_give isl_basic_set
*isl_basic_set_set_integral(
2247 __isl_take isl_basic_set
*bset
)
2252 if (!ISL_F_ISSET(bset
, ISL_BASIC_MAP_RATIONAL
))
2255 bset
= isl_basic_set_cow(bset
);
2259 ISL_F_CLR(bset
, ISL_BASIC_MAP_RATIONAL
);
2261 return isl_basic_set_finalize(bset
);
2264 __isl_give isl_map
*isl_map_set_rational(__isl_take isl_map
*map
)
2268 map
= isl_map_cow(map
);
2271 for (i
= 0; i
< map
->n
; ++i
) {
2272 map
->p
[i
] = isl_basic_map_set_rational(map
->p
[i
]);
2282 __isl_give isl_set
*isl_set_set_rational(__isl_take isl_set
*set
)
2284 return isl_map_set_rational(set
);
2287 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
2290 static void swap_div(__isl_keep isl_basic_map
*bmap
, int a
, int b
)
2292 isl_int
*t
= bmap
->div
[a
];
2293 bmap
->div
[a
] = bmap
->div
[b
];
2297 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
2298 * div definitions accordingly.
2300 __isl_give isl_basic_map
*isl_basic_map_swap_div(__isl_take isl_basic_map
*bmap
,
2306 off
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
2308 return isl_basic_map_free(bmap
);
2310 swap_div(bmap
, a
, b
);
2312 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2313 isl_int_swap(bmap
->eq
[i
][1+off
+a
], bmap
->eq
[i
][1+off
+b
]);
2315 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2316 isl_int_swap(bmap
->ineq
[i
][1+off
+a
], bmap
->ineq
[i
][1+off
+b
]);
2318 for (i
= 0; i
< bmap
->n_div
; ++i
)
2319 isl_int_swap(bmap
->div
[i
][1+1+off
+a
], bmap
->div
[i
][1+1+off
+b
]);
2320 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
2325 static void constraint_drop_vars(isl_int
*c
, unsigned n
, unsigned rem
)
2327 isl_seq_cpy(c
, c
+ n
, rem
);
2328 isl_seq_clr(c
+ rem
, n
);
2331 /* Drop n dimensions starting at first.
2333 * In principle, this frees up some extra variables as the number
2334 * of columns remains constant, but we would have to extend
2335 * the div array too as the number of rows in this array is assumed
2336 * to be equal to extra.
2338 __isl_give isl_basic_set
*isl_basic_set_drop_dims(
2339 __isl_take isl_basic_set
*bset
, unsigned first
, unsigned n
)
2341 return isl_basic_map_drop(bset_to_bmap(bset
), isl_dim_set
, first
, n
);
2344 /* Move "n" divs starting at "first" to the end of the list of divs.
2346 static __isl_give isl_basic_map
*move_divs_last(__isl_take isl_basic_map
*bmap
,
2347 unsigned first
, unsigned n
)
2352 if (first
+ n
== bmap
->n_div
)
2355 div
= isl_alloc_array(bmap
->ctx
, isl_int
*, n
);
2358 for (i
= 0; i
< n
; ++i
)
2359 div
[i
] = bmap
->div
[first
+ i
];
2360 for (i
= 0; i
< bmap
->n_div
- first
- n
; ++i
)
2361 bmap
->div
[first
+ i
] = bmap
->div
[first
+ n
+ i
];
2362 for (i
= 0; i
< n
; ++i
)
2363 bmap
->div
[bmap
->n_div
- n
+ i
] = div
[i
];
2367 isl_basic_map_free(bmap
);
2372 #define TYPE isl_map
2374 #include "check_type_range_templ.c"
2376 /* Check that there are "n" dimensions of type "type" starting at "first"
2379 isl_stat
isl_set_check_range(__isl_keep isl_set
*set
,
2380 enum isl_dim_type type
, unsigned first
, unsigned n
)
2382 return isl_map_check_range(set_to_map(set
), type
, first
, n
);
2385 /* Drop "n" dimensions of type "type" starting at "first".
2386 * Perform the core computation, without cowing or
2387 * simplifying and finalizing the result.
2389 * In principle, this frees up some extra variables as the number
2390 * of columns remains constant, but we would have to extend
2391 * the div array too as the number of rows in this array is assumed
2392 * to be equal to extra.
2394 __isl_give isl_basic_map
*isl_basic_map_drop_core(
2395 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
2396 unsigned first
, unsigned n
)
2403 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2404 return isl_basic_map_free(bmap
);
2406 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
2408 return isl_basic_map_free(bmap
);
2410 offset
= isl_basic_map_offset(bmap
, type
) + first
;
2411 left
= total
- (offset
- 1) - n
;
2412 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2413 constraint_drop_vars(bmap
->eq
[i
]+offset
, n
, left
);
2415 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2416 constraint_drop_vars(bmap
->ineq
[i
]+offset
, n
, left
);
2418 for (i
= 0; i
< bmap
->n_div
; ++i
)
2419 constraint_drop_vars(bmap
->div
[i
]+1+offset
, n
, left
);
2421 if (type
== isl_dim_div
) {
2422 bmap
= move_divs_last(bmap
, first
, n
);
2425 if (isl_basic_map_free_div(bmap
, n
) < 0)
2426 return isl_basic_map_free(bmap
);
2428 bmap
->dim
= isl_space_drop_dims(bmap
->dim
, type
, first
, n
);
2430 return isl_basic_map_free(bmap
);
2432 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
2433 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
2437 /* Drop "n" dimensions of type "type" starting at "first".
2439 * In principle, this frees up some extra variables as the number
2440 * of columns remains constant, but we would have to extend
2441 * the div array too as the number of rows in this array is assumed
2442 * to be equal to extra.
2444 __isl_give isl_basic_map
*isl_basic_map_drop(__isl_take isl_basic_map
*bmap
,
2445 enum isl_dim_type type
, unsigned first
, unsigned n
)
2449 if (n
== 0 && !isl_space_is_named_or_nested(bmap
->dim
, type
))
2452 bmap
= isl_basic_map_cow(bmap
);
2456 bmap
= isl_basic_map_drop_core(bmap
, type
, first
, n
);
2458 bmap
= isl_basic_map_simplify(bmap
);
2459 return isl_basic_map_finalize(bmap
);
2462 __isl_give isl_basic_set
*isl_basic_set_drop(__isl_take isl_basic_set
*bset
,
2463 enum isl_dim_type type
, unsigned first
, unsigned n
)
2465 return bset_from_bmap(isl_basic_map_drop(bset_to_bmap(bset
),
2469 /* No longer consider "map" to be normalized.
2471 static __isl_give isl_map
*isl_map_unmark_normalized(__isl_take isl_map
*map
)
2475 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
2479 __isl_give isl_map
*isl_map_drop(__isl_take isl_map
*map
,
2480 enum isl_dim_type type
, unsigned first
, unsigned n
)
2485 if (isl_map_check_range(map
, type
, first
, n
) < 0)
2486 return isl_map_free(map
);
2488 if (n
== 0 && !isl_space_is_named_or_nested(map
->dim
, type
))
2490 map
= isl_map_cow(map
);
2494 for (i
= 0; i
< map
->n
; ++i
) {
2495 map
->p
[i
] = isl_basic_map_drop(map
->p
[i
], type
, first
, n
);
2499 map
= isl_map_unmark_normalized(map
);
2501 space
= isl_map_take_space(map
);
2502 space
= isl_space_drop_dims(space
, type
, first
, n
);
2503 map
= isl_map_restore_space(map
, space
);
2511 __isl_give isl_set
*isl_set_drop(__isl_take isl_set
*set
,
2512 enum isl_dim_type type
, unsigned first
, unsigned n
)
2514 return set_from_map(isl_map_drop(set_to_map(set
), type
, first
, n
));
2517 /* Drop the integer division at position "div", which is assumed
2518 * not to appear in any of the constraints or
2519 * in any of the other integer divisions.
2521 * Since the integer division is redundant, there is no need to cow.
2523 __isl_give isl_basic_map
*isl_basic_map_drop_div(
2524 __isl_take isl_basic_map
*bmap
, unsigned div
)
2526 return isl_basic_map_drop_core(bmap
, isl_dim_div
, div
, 1);
2529 /* Eliminate the specified n dimensions starting at first from the
2530 * constraints, without removing the dimensions from the space.
2531 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2533 __isl_give isl_map
*isl_map_eliminate(__isl_take isl_map
*map
,
2534 enum isl_dim_type type
, unsigned first
, unsigned n
)
2541 if (isl_map_check_range(map
, type
, first
, n
) < 0)
2542 return isl_map_free(map
);
2544 map
= isl_map_cow(map
);
2548 for (i
= 0; i
< map
->n
; ++i
) {
2549 map
->p
[i
] = isl_basic_map_eliminate(map
->p
[i
], type
, first
, n
);
2559 /* Eliminate the specified n dimensions starting at first from the
2560 * constraints, without removing the dimensions from the space.
2561 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2563 __isl_give isl_set
*isl_set_eliminate(__isl_take isl_set
*set
,
2564 enum isl_dim_type type
, unsigned first
, unsigned n
)
2566 return set_from_map(isl_map_eliminate(set_to_map(set
), type
, first
, n
));
2569 /* Eliminate the specified n dimensions starting at first from the
2570 * constraints, without removing the dimensions from the space.
2571 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2573 __isl_give isl_set
*isl_set_eliminate_dims(__isl_take isl_set
*set
,
2574 unsigned first
, unsigned n
)
2576 return isl_set_eliminate(set
, isl_dim_set
, first
, n
);
2579 __isl_give isl_basic_map
*isl_basic_map_remove_divs(
2580 __isl_take isl_basic_map
*bmap
)
2584 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
2586 return isl_basic_map_free(bmap
);
2587 bmap
= isl_basic_map_eliminate_vars(bmap
, v_div
, bmap
->n_div
);
2591 return isl_basic_map_finalize(bmap
);
2594 __isl_give isl_basic_set
*isl_basic_set_remove_divs(
2595 __isl_take isl_basic_set
*bset
)
2597 return bset_from_bmap(isl_basic_map_remove_divs(bset_to_bmap(bset
)));
2600 __isl_give isl_map
*isl_map_remove_divs(__isl_take isl_map
*map
)
2609 map
= isl_map_cow(map
);
2613 for (i
= 0; i
< map
->n
; ++i
) {
2614 map
->p
[i
] = isl_basic_map_remove_divs(map
->p
[i
]);
2624 __isl_give isl_set
*isl_set_remove_divs(__isl_take isl_set
*set
)
2626 return isl_map_remove_divs(set
);
2629 __isl_give isl_basic_map
*isl_basic_map_remove_dims(
2630 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
2631 unsigned first
, unsigned n
)
2633 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2634 return isl_basic_map_free(bmap
);
2635 if (n
== 0 && !isl_space_is_named_or_nested(bmap
->dim
, type
))
2637 bmap
= isl_basic_map_eliminate_vars(bmap
,
2638 isl_basic_map_offset(bmap
, type
) - 1 + first
, n
);
2641 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
) && type
== isl_dim_div
)
2643 bmap
= isl_basic_map_drop(bmap
, type
, first
, n
);
2647 /* Return true if the definition of the given div (recursively) involves
2648 * any of the given variables.
2650 static isl_bool
div_involves_vars(__isl_keep isl_basic_map
*bmap
, int div
,
2651 unsigned first
, unsigned n
)
2654 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
2656 if (isl_int_is_zero(bmap
->div
[div
][0]))
2657 return isl_bool_false
;
2658 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + first
, n
) >= 0)
2659 return isl_bool_true
;
2661 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2664 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
2666 involves
= div_involves_vars(bmap
, i
, first
, n
);
2667 if (involves
< 0 || involves
)
2671 return isl_bool_false
;
2674 /* Try and add a lower and/or upper bound on "div" to "bmap"
2675 * based on inequality "i".
2676 * "total" is the total number of variables (excluding the divs).
2677 * "v" is a temporary object that can be used during the calculations.
2678 * If "lb" is set, then a lower bound should be constructed.
2679 * If "ub" is set, then an upper bound should be constructed.
2681 * The calling function has already checked that the inequality does not
2682 * reference "div", but we still need to check that the inequality is
2683 * of the right form. We'll consider the case where we want to construct
2684 * a lower bound. The construction of upper bounds is similar.
2686 * Let "div" be of the form
2688 * q = floor((a + f(x))/d)
2690 * We essentially check if constraint "i" is of the form
2694 * so that we can use it to derive a lower bound on "div".
2695 * However, we allow a slightly more general form
2699 * with the condition that the coefficients of g(x) - f(x) are all
2701 * Rewriting this constraint as
2705 * adding a + f(x) to both sides and dividing by d, we obtain
2707 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
2709 * Taking the floor on both sides, we obtain
2711 * q >= floor((a-b)/d) + (f(x)-g(x))/d
2715 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
2717 * In the case of an upper bound, we construct the constraint
2719 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
2722 static __isl_give isl_basic_map
*insert_bounds_on_div_from_ineq(
2723 __isl_take isl_basic_map
*bmap
, int div
, int i
,
2724 unsigned total
, isl_int v
, int lb
, int ub
)
2728 for (j
= 0; (lb
|| ub
) && j
< total
+ bmap
->n_div
; ++j
) {
2730 isl_int_sub(v
, bmap
->ineq
[i
][1 + j
],
2731 bmap
->div
[div
][1 + 1 + j
]);
2732 lb
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
2735 isl_int_add(v
, bmap
->ineq
[i
][1 + j
],
2736 bmap
->div
[div
][1 + 1 + j
]);
2737 ub
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
2743 bmap
= isl_basic_map_cow(bmap
);
2744 bmap
= isl_basic_map_extend_constraints(bmap
, 0, lb
+ ub
);
2746 int k
= isl_basic_map_alloc_inequality(bmap
);
2749 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
2750 isl_int_sub(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
2751 bmap
->div
[div
][1 + j
]);
2752 isl_int_cdiv_q(bmap
->ineq
[k
][j
],
2753 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
2755 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], 1);
2758 int k
= isl_basic_map_alloc_inequality(bmap
);
2761 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
2762 isl_int_add(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
2763 bmap
->div
[div
][1 + j
]);
2764 isl_int_fdiv_q(bmap
->ineq
[k
][j
],
2765 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
2767 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], -1);
2772 isl_basic_map_free(bmap
);
2776 /* This function is called right before "div" is eliminated from "bmap"
2777 * using Fourier-Motzkin.
2778 * Look through the constraints of "bmap" for constraints on the argument
2779 * of the integer division and use them to construct constraints on the
2780 * integer division itself. These constraints can then be combined
2781 * during the Fourier-Motzkin elimination.
2782 * Note that it is only useful to introduce lower bounds on "div"
2783 * if "bmap" already contains upper bounds on "div" as the newly
2784 * introduce lower bounds can then be combined with the pre-existing
2785 * upper bounds. Similarly for upper bounds.
2786 * We therefore first check if "bmap" contains any lower and/or upper bounds
2789 * It is interesting to note that the introduction of these constraints
2790 * can indeed lead to more accurate results, even when compared to
2791 * deriving constraints on the argument of "div" from constraints on "div".
2792 * Consider, for example, the set
2794 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2796 * The second constraint can be rewritten as
2798 * 2 * [(-i-2j+3)/4] + k >= 0
2800 * from which we can derive
2802 * -i - 2j + 3 >= -2k
2808 * Combined with the first constraint, we obtain
2810 * -3 <= 3 + 2k or k >= -3
2812 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2813 * the first constraint, we obtain
2815 * [(i + 2j)/4] >= [-3/4] = -1
2817 * Combining this constraint with the second constraint, we obtain
2821 static __isl_give isl_basic_map
*insert_bounds_on_div(
2822 __isl_take isl_basic_map
*bmap
, int div
)
2825 int check_lb
, check_ub
;
2832 if (isl_int_is_zero(bmap
->div
[div
][0]))
2835 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
2837 return isl_basic_map_free(bmap
);
2841 for (i
= 0; (!check_lb
|| !check_ub
) && i
< bmap
->n_ineq
; ++i
) {
2842 int s
= isl_int_sgn(bmap
->ineq
[i
][1 + v_div
+ div
]);
2849 if (!check_lb
&& !check_ub
)
2854 for (i
= 0; bmap
&& i
< bmap
->n_ineq
; ++i
) {
2855 if (!isl_int_is_zero(bmap
->ineq
[i
][1 + v_div
+ div
]))
2858 bmap
= insert_bounds_on_div_from_ineq(bmap
, div
, i
, v_div
, v
,
2859 check_lb
, check_ub
);
2867 /* Remove all divs (recursively) involving any of the given dimensions
2868 * in their definitions.
2870 __isl_give isl_basic_map
*isl_basic_map_remove_divs_involving_dims(
2871 __isl_take isl_basic_map
*bmap
,
2872 enum isl_dim_type type
, unsigned first
, unsigned n
)
2876 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2877 return isl_basic_map_free(bmap
);
2878 first
+= isl_basic_map_offset(bmap
, type
);
2880 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2883 involves
= div_involves_vars(bmap
, i
, first
, n
);
2885 return isl_basic_map_free(bmap
);
2888 bmap
= insert_bounds_on_div(bmap
, i
);
2889 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
2898 __isl_give isl_basic_set
*isl_basic_set_remove_divs_involving_dims(
2899 __isl_take isl_basic_set
*bset
,
2900 enum isl_dim_type type
, unsigned first
, unsigned n
)
2902 return isl_basic_map_remove_divs_involving_dims(bset
, type
, first
, n
);
2905 __isl_give isl_map
*isl_map_remove_divs_involving_dims(__isl_take isl_map
*map
,
2906 enum isl_dim_type type
, unsigned first
, unsigned n
)
2915 map
= isl_map_cow(map
);
2919 for (i
= 0; i
< map
->n
; ++i
) {
2920 map
->p
[i
] = isl_basic_map_remove_divs_involving_dims(map
->p
[i
],
2931 __isl_give isl_set
*isl_set_remove_divs_involving_dims(__isl_take isl_set
*set
,
2932 enum isl_dim_type type
, unsigned first
, unsigned n
)
2934 return set_from_map(isl_map_remove_divs_involving_dims(set_to_map(set
),
2938 /* Does the description of "bmap" depend on the specified dimensions?
2939 * We also check whether the dimensions appear in any of the div definitions.
2940 * In principle there is no need for this check. If the dimensions appear
2941 * in a div definition, they also appear in the defining constraints of that
2944 isl_bool
isl_basic_map_involves_dims(__isl_keep isl_basic_map
*bmap
,
2945 enum isl_dim_type type
, unsigned first
, unsigned n
)
2949 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2950 return isl_bool_error
;
2952 first
+= isl_basic_map_offset(bmap
, type
);
2953 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2954 if (isl_seq_first_non_zero(bmap
->eq
[i
] + first
, n
) >= 0)
2955 return isl_bool_true
;
2956 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2957 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + first
, n
) >= 0)
2958 return isl_bool_true
;
2959 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2960 if (isl_int_is_zero(bmap
->div
[i
][0]))
2962 if (isl_seq_first_non_zero(bmap
->div
[i
] + 1 + first
, n
) >= 0)
2963 return isl_bool_true
;
2966 return isl_bool_false
;
2969 isl_bool
isl_map_involves_dims(__isl_keep isl_map
*map
,
2970 enum isl_dim_type type
, unsigned first
, unsigned n
)
2974 if (isl_map_check_range(map
, type
, first
, n
) < 0)
2975 return isl_bool_error
;
2977 for (i
= 0; i
< map
->n
; ++i
) {
2978 isl_bool involves
= isl_basic_map_involves_dims(map
->p
[i
],
2980 if (involves
< 0 || involves
)
2984 return isl_bool_false
;
2987 isl_bool
isl_basic_set_involves_dims(__isl_keep isl_basic_set
*bset
,
2988 enum isl_dim_type type
, unsigned first
, unsigned n
)
2990 return isl_basic_map_involves_dims(bset
, type
, first
, n
);
2993 isl_bool
isl_set_involves_dims(__isl_keep isl_set
*set
,
2994 enum isl_dim_type type
, unsigned first
, unsigned n
)
2996 return isl_map_involves_dims(set
, type
, first
, n
);
2999 /* Does "bset" involve any local variables, i.e., integer divisions?
3001 static isl_bool
isl_basic_set_involves_locals(__isl_keep isl_basic_set
*bset
)
3005 n
= isl_basic_set_dim(bset
, isl_dim_div
);
3007 return isl_bool_error
;
3008 return isl_bool_ok(n
> 0);
3011 /* isl_set_every_basic_set callback that checks whether "bset"
3012 * is free of local variables.
3014 static isl_bool
basic_set_no_locals(__isl_keep isl_basic_set
*bset
, void *user
)
3016 return isl_bool_not(isl_basic_set_involves_locals(bset
));
3019 /* Does "set" involve any local variables, i.e., integer divisions?
3021 isl_bool
isl_set_involves_locals(__isl_keep isl_set
*set
)
3025 no_locals
= isl_set_every_basic_set(set
, &basic_set_no_locals
, NULL
);
3026 return isl_bool_not(no_locals
);
3029 /* Drop all constraints in bmap that involve any of the dimensions
3030 * first to first+n-1.
3031 * This function only performs the actual removal of constraints.
3033 * This function should not call finalize since it is used by
3034 * remove_redundant_divs, which in turn is called by isl_basic_map_finalize.
3036 __isl_give isl_basic_map
*isl_basic_map_drop_constraints_involving(
3037 __isl_take isl_basic_map
*bmap
, unsigned first
, unsigned n
)
3044 bmap
= isl_basic_map_cow(bmap
);
3049 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
) {
3050 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + first
, n
) == -1)
3052 if (isl_basic_map_drop_equality(bmap
, i
) < 0)
3053 return isl_basic_map_free(bmap
);
3056 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
3057 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + 1 + first
, n
) == -1)
3059 if (isl_basic_map_drop_inequality(bmap
, i
) < 0)
3060 return isl_basic_map_free(bmap
);
3066 /* Drop all constraints in bset that involve any of the dimensions
3067 * first to first+n-1.
3068 * This function only performs the actual removal of constraints.
3070 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_involving(
3071 __isl_take isl_basic_set
*bset
, unsigned first
, unsigned n
)
3073 return isl_basic_map_drop_constraints_involving(bset
, first
, n
);
3076 /* Drop all constraints in bmap that do not involve any of the dimensions
3077 * first to first + n - 1 of the given type.
3079 __isl_give isl_basic_map
*isl_basic_map_drop_constraints_not_involving_dims(
3080 __isl_take isl_basic_map
*bmap
,
3081 enum isl_dim_type type
, unsigned first
, unsigned n
)
3086 isl_space
*space
= isl_basic_map_get_space(bmap
);
3087 isl_basic_map_free(bmap
);
3088 return isl_basic_map_universe(space
);
3090 bmap
= isl_basic_map_cow(bmap
);
3094 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
3095 return isl_basic_map_free(bmap
);
3097 first
+= isl_basic_map_offset(bmap
, type
) - 1;
3099 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
) {
3100 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + first
, n
) != -1)
3102 if (isl_basic_map_drop_equality(bmap
, i
) < 0)
3103 return isl_basic_map_free(bmap
);
3106 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
3107 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + 1 + first
, n
) != -1)
3109 if (isl_basic_map_drop_inequality(bmap
, i
) < 0)
3110 return isl_basic_map_free(bmap
);
3113 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
3117 /* Drop all constraints in bset that do not involve any of the dimensions
3118 * first to first + n - 1 of the given type.
3120 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_not_involving_dims(
3121 __isl_take isl_basic_set
*bset
,
3122 enum isl_dim_type type
, unsigned first
, unsigned n
)
3124 return isl_basic_map_drop_constraints_not_involving_dims(bset
,
3128 /* Drop all constraints in bmap that involve any of the dimensions
3129 * first to first + n - 1 of the given type.
3131 __isl_give isl_basic_map
*isl_basic_map_drop_constraints_involving_dims(
3132 __isl_take isl_basic_map
*bmap
,
3133 enum isl_dim_type type
, unsigned first
, unsigned n
)
3140 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
3141 return isl_basic_map_free(bmap
);
3143 bmap
= isl_basic_map_remove_divs_involving_dims(bmap
, type
, first
, n
);
3144 first
+= isl_basic_map_offset(bmap
, type
) - 1;
3145 bmap
= isl_basic_map_drop_constraints_involving(bmap
, first
, n
);
3146 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
3150 /* Drop all constraints in bset that involve any of the dimensions
3151 * first to first + n - 1 of the given type.
3153 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_involving_dims(
3154 __isl_take isl_basic_set
*bset
,
3155 enum isl_dim_type type
, unsigned first
, unsigned n
)
3157 return isl_basic_map_drop_constraints_involving_dims(bset
,
3161 /* Drop constraints from "map" by applying "drop" to each basic map.
3163 static __isl_give isl_map
*drop_constraints(__isl_take isl_map
*map
,
3164 enum isl_dim_type type
, unsigned first
, unsigned n
,
3165 __isl_give isl_basic_map
*(*drop
)(__isl_take isl_basic_map
*bmap
,
3166 enum isl_dim_type type
, unsigned first
, unsigned n
))
3170 if (isl_map_check_range(map
, type
, first
, n
) < 0)
3171 return isl_map_free(map
);
3173 map
= isl_map_cow(map
);
3177 for (i
= 0; i
< map
->n
; ++i
) {
3178 map
->p
[i
] = drop(map
->p
[i
], type
, first
, n
);
3180 return isl_map_free(map
);
3184 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
3189 /* Drop all constraints in map that involve any of the dimensions
3190 * first to first + n - 1 of the given type.
3192 __isl_give isl_map
*isl_map_drop_constraints_involving_dims(
3193 __isl_take isl_map
*map
,
3194 enum isl_dim_type type
, unsigned first
, unsigned n
)
3198 return drop_constraints(map
, type
, first
, n
,
3199 &isl_basic_map_drop_constraints_involving_dims
);
3202 /* Drop all constraints in "map" that do not involve any of the dimensions
3203 * first to first + n - 1 of the given type.
3205 __isl_give isl_map
*isl_map_drop_constraints_not_involving_dims(
3206 __isl_take isl_map
*map
,
3207 enum isl_dim_type type
, unsigned first
, unsigned n
)
3210 isl_space
*space
= isl_map_get_space(map
);
3212 return isl_map_universe(space
);
3214 return drop_constraints(map
, type
, first
, n
,
3215 &isl_basic_map_drop_constraints_not_involving_dims
);
3218 /* Drop all constraints in set that involve any of the dimensions
3219 * first to first + n - 1 of the given type.
3221 __isl_give isl_set
*isl_set_drop_constraints_involving_dims(
3222 __isl_take isl_set
*set
,
3223 enum isl_dim_type type
, unsigned first
, unsigned n
)
3225 return isl_map_drop_constraints_involving_dims(set
, type
, first
, n
);
3228 /* Drop all constraints in "set" that do not involve any of the dimensions
3229 * first to first + n - 1 of the given type.
3231 __isl_give isl_set
*isl_set_drop_constraints_not_involving_dims(
3232 __isl_take isl_set
*set
,
3233 enum isl_dim_type type
, unsigned first
, unsigned n
)
3235 return isl_map_drop_constraints_not_involving_dims(set
, type
, first
, n
);
3238 /* Does local variable "div" of "bmap" have a complete explicit representation?
3239 * Having a complete explicit representation requires not only
3240 * an explicit representation, but also that all local variables
3241 * that appear in this explicit representation in turn have
3242 * a complete explicit representation.
3244 isl_bool
isl_basic_map_div_is_known(__isl_keep isl_basic_map
*bmap
, int div
)
3247 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
3250 marked
= isl_basic_map_div_is_marked_unknown(bmap
, div
);
3251 if (marked
< 0 || marked
)
3252 return isl_bool_not(marked
);
3254 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
3257 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
3259 known
= isl_basic_map_div_is_known(bmap
, i
);
3260 if (known
< 0 || !known
)
3264 return isl_bool_true
;
3267 /* Remove all divs that are unknown or defined in terms of unknown divs.
3269 __isl_give isl_basic_map
*isl_basic_map_remove_unknown_divs(
3270 __isl_take isl_basic_map
*bmap
)
3277 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
3278 if (isl_basic_map_div_is_known(bmap
, i
))
3280 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
3289 /* Remove all divs that are unknown or defined in terms of unknown divs.
3291 __isl_give isl_basic_set
*isl_basic_set_remove_unknown_divs(
3292 __isl_take isl_basic_set
*bset
)
3294 return isl_basic_map_remove_unknown_divs(bset
);
3297 __isl_give isl_map
*isl_map_remove_unknown_divs(__isl_take isl_map
*map
)
3306 map
= isl_map_cow(map
);
3310 for (i
= 0; i
< map
->n
; ++i
) {
3311 map
->p
[i
] = isl_basic_map_remove_unknown_divs(map
->p
[i
]);
3321 __isl_give isl_set
*isl_set_remove_unknown_divs(__isl_take isl_set
*set
)
3323 return set_from_map(isl_map_remove_unknown_divs(set_to_map(set
)));
3326 __isl_give isl_basic_set
*isl_basic_set_remove_dims(
3327 __isl_take isl_basic_set
*bset
,
3328 enum isl_dim_type type
, unsigned first
, unsigned n
)
3330 isl_basic_map
*bmap
= bset_to_bmap(bset
);
3331 bmap
= isl_basic_map_remove_dims(bmap
, type
, first
, n
);
3332 return bset_from_bmap(bmap
);
3335 __isl_give isl_map
*isl_map_remove_dims(__isl_take isl_map
*map
,
3336 enum isl_dim_type type
, unsigned first
, unsigned n
)
3343 map
= isl_map_cow(map
);
3344 if (isl_map_check_range(map
, type
, first
, n
) < 0)
3345 return isl_map_free(map
);
3347 for (i
= 0; i
< map
->n
; ++i
) {
3348 map
->p
[i
] = isl_basic_map_eliminate_vars(map
->p
[i
],
3349 isl_basic_map_offset(map
->p
[i
], type
) - 1 + first
, n
);
3353 map
= isl_map_drop(map
, type
, first
, n
);
3360 __isl_give isl_set
*isl_set_remove_dims(__isl_take isl_set
*bset
,
3361 enum isl_dim_type type
, unsigned first
, unsigned n
)
3363 return set_from_map(isl_map_remove_dims(set_to_map(bset
),
3367 /* Project out n inputs starting at first using Fourier-Motzkin */
3368 __isl_give isl_map
*isl_map_remove_inputs(__isl_take isl_map
*map
,
3369 unsigned first
, unsigned n
)
3371 return isl_map_remove_dims(map
, isl_dim_in
, first
, n
);
3374 void isl_basic_set_print_internal(__isl_keep isl_basic_set
*bset
,
3375 FILE *out
, int indent
)
3380 fprintf(out
, "null basic set\n");
3384 fprintf(out
, "%*s", indent
, "");
3385 fprintf(out
, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
3386 bset
->ref
, bset
->dim
->nparam
, bset
->dim
->n_out
,
3387 bset
->extra
, bset
->flags
);
3389 p
= isl_printer_to_file(isl_basic_set_get_ctx(bset
), out
);
3390 p
= isl_printer_set_dump(p
, 1);
3391 p
= isl_printer_set_indent(p
, indent
);
3392 p
= isl_printer_start_line(p
);
3393 p
= isl_printer_print_basic_set(p
, bset
);
3394 p
= isl_printer_end_line(p
);
3395 isl_printer_free(p
);
3398 void isl_basic_map_print_internal(__isl_keep isl_basic_map
*bmap
,
3399 FILE *out
, int indent
)
3404 fprintf(out
, "null basic map\n");
3408 fprintf(out
, "%*s", indent
, "");
3409 fprintf(out
, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
3410 "flags: %x, n_name: %d\n",
3412 bmap
->dim
->nparam
, bmap
->dim
->n_in
, bmap
->dim
->n_out
,
3413 bmap
->extra
, bmap
->flags
, bmap
->dim
->n_id
);
3415 p
= isl_printer_to_file(isl_basic_map_get_ctx(bmap
), out
);
3416 p
= isl_printer_set_dump(p
, 1);
3417 p
= isl_printer_set_indent(p
, indent
);
3418 p
= isl_printer_start_line(p
);
3419 p
= isl_printer_print_basic_map(p
, bmap
);
3420 p
= isl_printer_end_line(p
);
3421 isl_printer_free(p
);
3424 __isl_give isl_basic_map
*isl_inequality_negate(__isl_take isl_basic_map
*bmap
,
3429 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
3431 return isl_basic_map_free(bmap
);
3432 if (pos
>= bmap
->n_ineq
)
3433 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
3434 "invalid position", return isl_basic_map_free(bmap
));
3435 isl_seq_neg(bmap
->ineq
[pos
], bmap
->ineq
[pos
], 1 + total
);
3436 isl_int_sub_ui(bmap
->ineq
[pos
][0], bmap
->ineq
[pos
][0], 1);
3437 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
3438 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
3442 __isl_give isl_set
*isl_set_alloc_space(__isl_take isl_space
*space
, int n
,
3445 if (isl_space_check_is_set(space
) < 0)
3447 return isl_map_alloc_space(space
, n
, flags
);
3449 isl_space_free(space
);
3453 /* Make sure "map" has room for at least "n" more basic maps.
3455 __isl_give isl_map
*isl_map_grow(__isl_take isl_map
*map
, int n
)
3458 struct isl_map
*grown
= NULL
;
3462 isl_assert(map
->ctx
, n
>= 0, goto error
);
3463 if (map
->n
+ n
<= map
->size
)
3465 grown
= isl_map_alloc_space(isl_map_get_space(map
), map
->n
+ n
, map
->flags
);
3468 for (i
= 0; i
< map
->n
; ++i
) {
3469 grown
->p
[i
] = isl_basic_map_copy(map
->p
[i
]);
3477 isl_map_free(grown
);
3482 /* Make sure "set" has room for at least "n" more basic sets.
3484 __isl_give isl_set
*isl_set_grow(__isl_take isl_set
*set
, int n
)
3486 return set_from_map(isl_map_grow(set_to_map(set
), n
));
3489 __isl_give isl_set
*isl_set_from_basic_set(__isl_take isl_basic_set
*bset
)
3491 return isl_map_from_basic_map(bset
);
3494 /* This function performs the same operation as isl_set_from_basic_set,
3495 * but is considered as a function on an isl_basic_set when exported.
3497 __isl_give isl_set
*isl_basic_set_to_set(__isl_take isl_basic_set
*bset
)
3499 return isl_set_from_basic_set(bset
);
3502 __isl_give isl_map
*isl_map_from_basic_map(__isl_take isl_basic_map
*bmap
)
3504 struct isl_map
*map
;
3509 map
= isl_map_alloc_space(isl_space_copy(bmap
->dim
), 1, ISL_MAP_DISJOINT
);
3510 return isl_map_add_basic_map(map
, bmap
);
3513 __isl_give isl_set
*isl_set_add_basic_set(__isl_take isl_set
*set
,
3514 __isl_take isl_basic_set
*bset
)
3516 return set_from_map(isl_map_add_basic_map(set_to_map(set
),
3517 bset_to_bmap(bset
)));
3520 __isl_null isl_set
*isl_set_free(__isl_take isl_set
*set
)
3522 return isl_map_free(set
);
3525 void isl_set_print_internal(__isl_keep isl_set
*set
, FILE *out
, int indent
)
3530 fprintf(out
, "null set\n");
3534 fprintf(out
, "%*s", indent
, "");
3535 fprintf(out
, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
3536 set
->ref
, set
->n
, set
->dim
->nparam
, set
->dim
->n_out
,
3538 for (i
= 0; i
< set
->n
; ++i
) {
3539 fprintf(out
, "%*s", indent
, "");
3540 fprintf(out
, "basic set %d:\n", i
);
3541 isl_basic_set_print_internal(set
->p
[i
], out
, indent
+4);
3545 void isl_map_print_internal(__isl_keep isl_map
*map
, FILE *out
, int indent
)
3550 fprintf(out
, "null map\n");
3554 fprintf(out
, "%*s", indent
, "");
3555 fprintf(out
, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
3556 "flags: %x, n_name: %d\n",
3557 map
->ref
, map
->n
, map
->dim
->nparam
, map
->dim
->n_in
,
3558 map
->dim
->n_out
, map
->flags
, map
->dim
->n_id
);
3559 for (i
= 0; i
< map
->n
; ++i
) {
3560 fprintf(out
, "%*s", indent
, "");
3561 fprintf(out
, "basic map %d:\n", i
);
3562 isl_basic_map_print_internal(map
->p
[i
], out
, indent
+4);
3566 /* Check that the space of "bset" is the same as that of the domain of "bmap".
3568 static isl_stat
isl_basic_map_check_compatible_domain(
3569 __isl_keep isl_basic_map
*bmap
, __isl_keep isl_basic_set
*bset
)
3573 ok
= isl_basic_map_compatible_domain(bmap
, bset
);
3575 return isl_stat_error
;
3577 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
3578 "incompatible spaces", return isl_stat_error
);
3583 __isl_give isl_basic_map
*isl_basic_map_intersect_domain(
3584 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*bset
)
3586 struct isl_basic_map
*bmap_domain
;
3589 if (isl_basic_map_check_equal_params(bmap
, bset_to_bmap(bset
)) < 0)
3592 dim
= isl_basic_set_dim(bset
, isl_dim_set
);
3596 isl_basic_map_check_compatible_domain(bmap
, bset
) < 0)
3599 bmap
= isl_basic_map_cow(bmap
);
3602 bmap
= isl_basic_map_extend(bmap
,
3603 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
3604 bmap_domain
= isl_basic_map_from_domain(bset
);
3605 bmap
= add_constraints(bmap
, bmap_domain
, 0, 0);
3607 bmap
= isl_basic_map_simplify(bmap
);
3608 return isl_basic_map_finalize(bmap
);
3610 isl_basic_map_free(bmap
);
3611 isl_basic_set_free(bset
);
3615 /* Check that the space of "bset" is the same as that of the range of "bmap".
3617 static isl_stat
isl_basic_map_check_compatible_range(
3618 __isl_keep isl_basic_map
*bmap
, __isl_keep isl_basic_set
*bset
)
3622 ok
= isl_basic_map_compatible_range(bmap
, bset
);
3624 return isl_stat_error
;
3626 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
3627 "incompatible spaces", return isl_stat_error
);
3632 __isl_give isl_basic_map
*isl_basic_map_intersect_range(
3633 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*bset
)
3635 struct isl_basic_map
*bmap_range
;
3638 if (isl_basic_map_check_equal_params(bmap
, bset_to_bmap(bset
)) < 0)
3641 dim
= isl_basic_set_dim(bset
, isl_dim_set
);
3644 if (dim
!= 0 && isl_basic_map_check_compatible_range(bmap
, bset
) < 0)
3647 if (isl_basic_set_plain_is_universe(bset
)) {
3648 isl_basic_set_free(bset
);
3652 bmap
= isl_basic_map_cow(bmap
);
3655 bmap
= isl_basic_map_extend(bmap
,
3656 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
3657 bmap_range
= bset_to_bmap(bset
);
3658 bmap
= add_constraints(bmap
, bmap_range
, 0, 0);
3660 bmap
= isl_basic_map_simplify(bmap
);
3661 return isl_basic_map_finalize(bmap
);
3663 isl_basic_map_free(bmap
);
3664 isl_basic_set_free(bset
);
3668 isl_bool
isl_basic_map_contains(__isl_keep isl_basic_map
*bmap
,
3669 __isl_keep isl_vec
*vec
)
3675 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
3676 if (total
< 0 || !vec
)
3677 return isl_bool_error
;
3679 if (1 + total
!= vec
->size
)
3680 return isl_bool_false
;
3684 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
3685 isl_seq_inner_product(vec
->el
, bmap
->eq
[i
], 1 + total
, &s
);
3686 if (!isl_int_is_zero(s
)) {
3688 return isl_bool_false
;
3692 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
3693 isl_seq_inner_product(vec
->el
, bmap
->ineq
[i
], 1 + total
, &s
);
3694 if (isl_int_is_neg(s
)) {
3696 return isl_bool_false
;
3702 return isl_bool_true
;
3705 isl_bool
isl_basic_set_contains(__isl_keep isl_basic_set
*bset
,
3706 __isl_keep isl_vec
*vec
)
3708 return isl_basic_map_contains(bset_to_bmap(bset
), vec
);
3711 __isl_give isl_basic_map
*isl_basic_map_intersect(
3712 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
3714 struct isl_vec
*sample
= NULL
;
3715 isl_space
*space1
, *space2
;
3716 isl_size dim1
, dim2
, nparam1
, nparam2
;
3718 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
3720 space1
= isl_basic_map_peek_space(bmap1
);
3721 space2
= isl_basic_map_peek_space(bmap2
);
3722 dim1
= isl_space_dim(space1
, isl_dim_all
);
3723 dim2
= isl_space_dim(space2
, isl_dim_all
);
3724 nparam1
= isl_space_dim(space1
, isl_dim_param
);
3725 nparam2
= isl_space_dim(space2
, isl_dim_param
);
3726 if (dim1
< 0 || dim2
< 0 || nparam1
< 0 || nparam2
< 0)
3728 if (dim1
== nparam1
&& dim2
!= nparam2
)
3729 return isl_basic_map_intersect(bmap2
, bmap1
);
3731 if (dim2
!= nparam2
&&
3732 isl_basic_map_check_equal_space(bmap1
, bmap2
) < 0)
3735 if (isl_basic_map_plain_is_empty(bmap1
)) {
3736 isl_basic_map_free(bmap2
);
3739 if (isl_basic_map_plain_is_empty(bmap2
)) {
3740 isl_basic_map_free(bmap1
);
3744 if (bmap1
->sample
&&
3745 isl_basic_map_contains(bmap1
, bmap1
->sample
) > 0 &&
3746 isl_basic_map_contains(bmap2
, bmap1
->sample
) > 0)
3747 sample
= isl_vec_copy(bmap1
->sample
);
3748 else if (bmap2
->sample
&&
3749 isl_basic_map_contains(bmap1
, bmap2
->sample
) > 0 &&
3750 isl_basic_map_contains(bmap2
, bmap2
->sample
) > 0)
3751 sample
= isl_vec_copy(bmap2
->sample
);
3753 bmap1
= isl_basic_map_cow(bmap1
);
3756 bmap1
= isl_basic_map_extend(bmap1
,
3757 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
3758 bmap1
= add_constraints(bmap1
, bmap2
, 0, 0);
3761 isl_vec_free(sample
);
3763 isl_vec_free(bmap1
->sample
);
3764 bmap1
->sample
= sample
;
3767 bmap1
= isl_basic_map_simplify(bmap1
);
3768 return isl_basic_map_finalize(bmap1
);
3771 isl_vec_free(sample
);
3772 isl_basic_map_free(bmap1
);
3773 isl_basic_map_free(bmap2
);
3777 __isl_give isl_basic_set
*isl_basic_set_intersect(
3778 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
3780 return bset_from_bmap(isl_basic_map_intersect(bset_to_bmap(bset1
),
3781 bset_to_bmap(bset2
)));
3784 __isl_give isl_basic_set
*isl_basic_set_intersect_params(
3785 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
3787 return isl_basic_set_intersect(bset1
, bset2
);
3790 /* Does "map" consist of a single disjunct, without any local variables?
3792 static isl_bool
is_convex_no_locals(__isl_keep isl_map
*map
)
3797 return isl_bool_error
;
3799 return isl_bool_false
;
3800 n_div
= isl_basic_map_dim(map
->p
[0], isl_dim_div
);
3802 return isl_bool_error
;
3804 return isl_bool_false
;
3805 return isl_bool_true
;
3808 /* Check that "map" consists of a single disjunct, without any local variables.
3810 static isl_stat
check_convex_no_locals(__isl_keep isl_map
*map
)
3814 ok
= is_convex_no_locals(map
);
3816 return isl_stat_error
;
3820 isl_die(isl_map_get_ctx(map
), isl_error_internal
,
3821 "unexpectedly not convex or involving local variables",
3822 return isl_stat_error
);
3825 /* Special case of isl_map_intersect, where both map1 and map2
3826 * are convex, without any divs and such that either map1 or map2
3827 * contains a single constraint. This constraint is then simply
3828 * added to the other map.
3830 static __isl_give isl_map
*map_intersect_add_constraint(
3831 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
3833 if (check_convex_no_locals(map1
) < 0 ||
3834 check_convex_no_locals(map2
) < 0)
3837 if (map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
!= 1)
3838 return isl_map_intersect(map2
, map1
);
3840 map1
= isl_map_cow(map1
);
3843 if (isl_map_plain_is_empty(map1
)) {
3847 if (map2
->p
[0]->n_eq
== 1)
3848 map1
->p
[0] = isl_basic_map_add_eq(map1
->p
[0], map2
->p
[0]->eq
[0]);
3850 map1
->p
[0] = isl_basic_map_add_ineq(map1
->p
[0],
3851 map2
->p
[0]->ineq
[0]);
3853 map1
->p
[0] = isl_basic_map_simplify(map1
->p
[0]);
3854 map1
->p
[0] = isl_basic_map_finalize(map1
->p
[0]);
3858 if (isl_basic_map_plain_is_empty(map1
->p
[0])) {
3859 isl_basic_map_free(map1
->p
[0]);
3865 map1
= isl_map_unmark_normalized(map1
);
3873 /* map2 may be either a parameter domain or a map living in the same
3876 static __isl_give isl_map
*map_intersect_internal(__isl_take isl_map
*map1
,
3877 __isl_take isl_map
*map2
)
3883 isl_size dim2
, nparam2
;
3888 if ((isl_map_plain_is_empty(map1
) ||
3889 isl_map_plain_is_universe(map2
)) &&
3890 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
3894 if ((isl_map_plain_is_empty(map2
) ||
3895 isl_map_plain_is_universe(map1
)) &&
3896 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
3901 if (is_convex_no_locals(map1
) == isl_bool_true
&&
3902 is_convex_no_locals(map2
) == isl_bool_true
&&
3903 isl_space_is_equal(map1
->dim
, map2
->dim
) &&
3904 (map1
->p
[0]->n_eq
+ map1
->p
[0]->n_ineq
== 1 ||
3905 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1))
3906 return map_intersect_add_constraint(map1
, map2
);
3908 equal
= isl_map_plain_is_equal(map1
, map2
);
3916 dim2
= isl_map_dim(map2
, isl_dim_all
);
3917 nparam2
= isl_map_dim(map2
, isl_dim_param
);
3918 if (dim2
< 0 || nparam2
< 0)
3920 if (dim2
!= nparam2
)
3921 isl_assert(map1
->ctx
,
3922 isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
3924 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
3925 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
3926 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
3928 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
3929 map1
->n
* map2
->n
, flags
);
3932 for (i
= 0; i
< map1
->n
; ++i
)
3933 for (j
= 0; j
< map2
->n
; ++j
) {
3934 struct isl_basic_map
*part
;
3935 part
= isl_basic_map_intersect(
3936 isl_basic_map_copy(map1
->p
[i
]),
3937 isl_basic_map_copy(map2
->p
[j
]));
3938 if (isl_basic_map_is_empty(part
) < 0)
3939 part
= isl_basic_map_free(part
);
3940 result
= isl_map_add_basic_map(result
, part
);
3953 static __isl_give isl_map
*map_intersect(__isl_take isl_map
*map1
,
3954 __isl_take isl_map
*map2
)
3956 if (isl_map_check_equal_space(map1
, map2
) < 0)
3958 return map_intersect_internal(map1
, map2
);
3965 __isl_give isl_map
*isl_map_intersect(__isl_take isl_map
*map1
,
3966 __isl_take isl_map
*map2
)
3968 isl_map_align_params_bin(&map1
, &map2
);
3969 return map_intersect(map1
, map2
);
3972 __isl_give isl_set
*isl_set_intersect(__isl_take isl_set
*set1
,
3973 __isl_take isl_set
*set2
)
3975 return set_from_map(isl_map_intersect(set_to_map(set1
),
3979 /* map_intersect_internal accepts intersections
3980 * with parameter domains, so we can just call that function.
3982 __isl_give isl_map
*isl_map_intersect_params(__isl_take isl_map
*map
,
3983 __isl_take isl_set
*params
)
3985 isl_map_align_params_set(&map
, ¶ms
);
3986 return map_intersect_internal(map
, params
);
3989 __isl_give isl_set
*isl_set_intersect_params(__isl_take isl_set
*set
,
3990 __isl_take isl_set
*params
)
3992 return isl_map_intersect_params(set
, params
);
3995 __isl_give isl_basic_map
*isl_basic_map_reverse(__isl_take isl_basic_map
*bmap
)
4003 bmap
= isl_basic_map_cow(bmap
);
4006 space
= isl_space_reverse(isl_space_copy(bmap
->dim
));
4007 pos
= isl_basic_map_offset(bmap
, isl_dim_in
);
4008 n1
= isl_basic_map_dim(bmap
, isl_dim_in
);
4009 n2
= isl_basic_map_dim(bmap
, isl_dim_out
);
4010 if (n1
< 0 || n2
< 0)
4011 bmap
= isl_basic_map_free(bmap
);
4012 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
4013 return isl_basic_map_reset_space(bmap
, space
);
4016 /* Given a basic map A -> (B -> C), return the corresponding basic map
4019 static __isl_give isl_basic_map
*isl_basic_map_range_reverse(
4020 __isl_take isl_basic_map
*bmap
)
4023 isl_size offset
, n1
, n2
;
4025 space
= isl_basic_map_peek_space(bmap
);
4026 if (isl_space_check_range_is_wrapping(space
) < 0)
4027 return isl_basic_map_free(bmap
);
4028 offset
= isl_basic_map_var_offset(bmap
, isl_dim_out
);
4029 n1
= isl_space_wrapped_dim(space
, isl_dim_out
, isl_dim_in
);
4030 n2
= isl_space_wrapped_dim(space
, isl_dim_out
, isl_dim_out
);
4031 if (offset
< 0 || n1
< 0 || n2
< 0)
4032 return isl_basic_map_free(bmap
);
4034 bmap
= isl_basic_map_swap_vars(bmap
, 1 + offset
, n1
, n2
);
4036 space
= isl_basic_map_take_space(bmap
);
4037 space
= isl_space_range_reverse(space
);
4038 bmap
= isl_basic_map_restore_space(bmap
, space
);
4043 static __isl_give isl_basic_map
*basic_map_space_reset(
4044 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
)
4050 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
4053 space
= isl_basic_map_get_space(bmap
);
4054 space
= isl_space_reset(space
, type
);
4055 bmap
= isl_basic_map_reset_space(bmap
, space
);
4059 __isl_give isl_basic_map
*isl_basic_map_insert_dims(
4060 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
4061 unsigned pos
, unsigned n
)
4063 isl_bool rational
, is_empty
;
4064 isl_space
*res_space
;
4065 struct isl_basic_map
*res
;
4066 struct isl_dim_map
*dim_map
;
4069 enum isl_dim_type t
;
4072 return basic_map_space_reset(bmap
, type
);
4074 is_empty
= isl_basic_map_plain_is_empty(bmap
);
4075 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4076 if (is_empty
< 0 || total
< 0)
4077 return isl_basic_map_free(bmap
);
4078 res_space
= isl_space_insert_dims(isl_basic_map_get_space(bmap
),
4081 return isl_basic_map_free(bmap
);
4083 isl_basic_map_free(bmap
);
4084 return isl_basic_map_empty(res_space
);
4087 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
+ n
);
4089 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
4093 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
4095 isl_size size
= isl_basic_map_dim(bmap
, t
);
4097 dim_map
= isl_dim_map_free(dim_map
);
4098 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
4100 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
4101 pos
, size
- pos
, off
+ pos
+ n
);
4103 dim
= isl_space_dim(res_space
, t
);
4105 dim_map
= isl_dim_map_free(dim_map
);
4108 isl_dim_map_div(dim_map
, bmap
, off
);
4110 res
= isl_basic_map_alloc_space(res_space
,
4111 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
4112 rational
= isl_basic_map_is_rational(bmap
);
4114 res
= isl_basic_map_free(res
);
4116 res
= isl_basic_map_set_rational(res
);
4117 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
4118 return isl_basic_map_finalize(res
);
4121 __isl_give isl_basic_set
*isl_basic_set_insert_dims(
4122 __isl_take isl_basic_set
*bset
,
4123 enum isl_dim_type type
, unsigned pos
, unsigned n
)
4125 return isl_basic_map_insert_dims(bset
, type
, pos
, n
);
4128 __isl_give isl_basic_map
*isl_basic_map_add_dims(__isl_take isl_basic_map
*bmap
,
4129 enum isl_dim_type type
, unsigned n
)
4133 dim
= isl_basic_map_dim(bmap
, type
);
4135 return isl_basic_map_free(bmap
);
4136 return isl_basic_map_insert_dims(bmap
, type
, dim
, n
);
4139 __isl_give isl_basic_set
*isl_basic_set_add_dims(__isl_take isl_basic_set
*bset
,
4140 enum isl_dim_type type
, unsigned n
)
4144 isl_assert(bset
->ctx
, type
!= isl_dim_in
, goto error
);
4145 return isl_basic_map_add_dims(bset
, type
, n
);
4147 isl_basic_set_free(bset
);
4151 static __isl_give isl_map
*map_space_reset(__isl_take isl_map
*map
,
4152 enum isl_dim_type type
)
4156 if (!map
|| !isl_space_is_named_or_nested(map
->dim
, type
))
4159 space
= isl_map_get_space(map
);
4160 space
= isl_space_reset(space
, type
);
4161 map
= isl_map_reset_space(map
, space
);
4165 __isl_give isl_map
*isl_map_insert_dims(__isl_take isl_map
*map
,
4166 enum isl_dim_type type
, unsigned pos
, unsigned n
)
4172 return map_space_reset(map
, type
);
4174 map
= isl_map_cow(map
);
4178 for (i
= 0; i
< map
->n
; ++i
) {
4179 map
->p
[i
] = isl_basic_map_insert_dims(map
->p
[i
], type
, pos
, n
);
4184 space
= isl_map_take_space(map
);
4185 space
= isl_space_insert_dims(space
, type
, pos
, n
);
4186 map
= isl_map_restore_space(map
, space
);
4194 __isl_give isl_set
*isl_set_insert_dims(__isl_take isl_set
*set
,
4195 enum isl_dim_type type
, unsigned pos
, unsigned n
)
4197 return isl_map_insert_dims(set
, type
, pos
, n
);
4200 __isl_give isl_map
*isl_map_add_dims(__isl_take isl_map
*map
,
4201 enum isl_dim_type type
, unsigned n
)
4205 dim
= isl_map_dim(map
, type
);
4207 return isl_map_free(map
);
4208 return isl_map_insert_dims(map
, type
, dim
, n
);
4211 __isl_give isl_set
*isl_set_add_dims(__isl_take isl_set
*set
,
4212 enum isl_dim_type type
, unsigned n
)
4216 isl_assert(set
->ctx
, type
!= isl_dim_in
, goto error
);
4217 return set_from_map(isl_map_add_dims(set_to_map(set
), type
, n
));
4223 __isl_give isl_basic_map
*isl_basic_map_move_dims(
4224 __isl_take isl_basic_map
*bmap
,
4225 enum isl_dim_type dst_type
, unsigned dst_pos
,
4226 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
4229 struct isl_dim_map
*dim_map
;
4230 struct isl_basic_map
*res
;
4231 enum isl_dim_type t
;
4238 bmap
= isl_basic_map_reset(bmap
, src_type
);
4239 bmap
= isl_basic_map_reset(bmap
, dst_type
);
4243 if (isl_basic_map_check_range(bmap
, src_type
, src_pos
, n
) < 0)
4244 return isl_basic_map_free(bmap
);
4246 if (dst_type
== src_type
&& dst_pos
== src_pos
)
4249 isl_assert(bmap
->ctx
, dst_type
!= src_type
, goto error
);
4251 if (pos(bmap
->dim
, dst_type
) + dst_pos
==
4252 pos(bmap
->dim
, src_type
) + src_pos
+
4253 ((src_type
< dst_type
) ? n
: 0)) {
4254 space
= isl_basic_map_take_space(bmap
);
4255 space
= isl_space_move_dims(space
, dst_type
, dst_pos
,
4256 src_type
, src_pos
, n
);
4257 bmap
= isl_basic_map_restore_space(bmap
, space
);
4258 bmap
= isl_basic_map_finalize(bmap
);
4263 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4265 return isl_basic_map_free(bmap
);
4266 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
4269 space
= isl_basic_map_peek_space(bmap
);
4270 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
4271 isl_size size
= isl_space_dim(space
, t
);
4273 dim_map
= isl_dim_map_free(dim_map
);
4274 if (t
== dst_type
) {
4275 isl_dim_map_dim_range(dim_map
, space
, t
,
4278 isl_dim_map_dim_range(dim_map
, space
, src_type
,
4281 isl_dim_map_dim_range(dim_map
, space
, t
,
4282 dst_pos
, size
- dst_pos
, off
);
4283 off
+= size
- dst_pos
;
4284 } else if (t
== src_type
) {
4285 isl_dim_map_dim_range(dim_map
, space
, t
,
4288 isl_dim_map_dim_range(dim_map
, space
, t
,
4289 src_pos
+ n
, size
- src_pos
- n
, off
);
4290 off
+= size
- src_pos
- n
;
4292 isl_dim_map_dim(dim_map
, space
, t
, off
);
4296 isl_dim_map_div(dim_map
, bmap
, off
);
4298 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
4299 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
4300 bmap
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
4301 space
= isl_basic_map_take_space(bmap
);
4302 space
= isl_space_move_dims(space
, dst_type
, dst_pos
,
4303 src_type
, src_pos
, n
);
4304 bmap
= isl_basic_map_restore_space(bmap
, space
);
4308 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
4309 bmap
= isl_basic_map_gauss(bmap
, NULL
);
4310 bmap
= isl_basic_map_finalize(bmap
);
4314 isl_basic_map_free(bmap
);
4318 __isl_give isl_basic_set
*isl_basic_set_move_dims(__isl_take isl_basic_set
*bset
,
4319 enum isl_dim_type dst_type
, unsigned dst_pos
,
4320 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
4322 isl_basic_map
*bmap
= bset_to_bmap(bset
);
4323 bmap
= isl_basic_map_move_dims(bmap
, dst_type
, dst_pos
,
4324 src_type
, src_pos
, n
);
4325 return bset_from_bmap(bmap
);
4328 __isl_give isl_set
*isl_set_move_dims(__isl_take isl_set
*set
,
4329 enum isl_dim_type dst_type
, unsigned dst_pos
,
4330 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
4334 isl_assert(set
->ctx
, dst_type
!= isl_dim_in
, goto error
);
4335 return set_from_map(isl_map_move_dims(set_to_map(set
),
4336 dst_type
, dst_pos
, src_type
, src_pos
, n
));
4342 __isl_give isl_map
*isl_map_move_dims(__isl_take isl_map
*map
,
4343 enum isl_dim_type dst_type
, unsigned dst_pos
,
4344 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
4350 map
= isl_map_reset(map
, src_type
);
4351 map
= isl_map_reset(map
, dst_type
);
4355 if (isl_map_check_range(map
, src_type
, src_pos
, n
))
4356 return isl_map_free(map
);
4358 if (dst_type
== src_type
&& dst_pos
== src_pos
)
4361 isl_assert(map
->ctx
, dst_type
!= src_type
, goto error
);
4363 map
= isl_map_cow(map
);
4367 for (i
= 0; i
< map
->n
; ++i
) {
4368 map
->p
[i
] = isl_basic_map_move_dims(map
->p
[i
],
4370 src_type
, src_pos
, n
);
4375 space
= isl_map_take_space(map
);
4376 space
= isl_space_move_dims(space
, dst_type
, dst_pos
,
4377 src_type
, src_pos
, n
);
4378 map
= isl_map_restore_space(map
, space
);
4386 /* Move the specified dimensions to the last columns right before
4387 * the divs. Don't change the dimension specification of bmap.
4388 * That's the responsibility of the caller.
4390 static __isl_give isl_basic_map
*move_last(__isl_take isl_basic_map
*bmap
,
4391 enum isl_dim_type type
, unsigned first
, unsigned n
)
4394 struct isl_dim_map
*dim_map
;
4395 struct isl_basic_map
*res
;
4396 enum isl_dim_type t
;
4402 if (isl_basic_map_offset(bmap
, type
) + first
+ n
==
4403 isl_basic_map_offset(bmap
, isl_dim_div
))
4406 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4408 return isl_basic_map_free(bmap
);
4409 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
4412 space
= isl_basic_map_peek_space(bmap
);
4413 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
4414 isl_size size
= isl_space_dim(space
, t
);
4416 dim_map
= isl_dim_map_free(dim_map
);
4418 isl_dim_map_dim_range(dim_map
, space
, t
,
4421 isl_dim_map_dim_range(dim_map
, space
, t
,
4422 first
, n
, total
- bmap
->n_div
- n
);
4423 isl_dim_map_dim_range(dim_map
, space
, t
,
4424 first
+ n
, size
- (first
+ n
), off
);
4425 off
+= size
- (first
+ n
);
4427 isl_dim_map_dim(dim_map
, space
, t
, off
);
4431 isl_dim_map_div(dim_map
, bmap
, off
+ n
);
4433 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
4434 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
4435 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
4439 /* Insert "n" rows in the divs of "bmap".
4441 * The number of columns is not changed, which means that the last
4442 * dimensions of "bmap" are being reintepreted as the new divs.
4443 * The space of "bmap" is not adjusted, however, which means
4444 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
4445 * from the space of "bmap" is the responsibility of the caller.
4447 static __isl_give isl_basic_map
*insert_div_rows(__isl_take isl_basic_map
*bmap
,
4455 bmap
= isl_basic_map_cow(bmap
);
4459 row_size
= isl_basic_map_offset(bmap
, isl_dim_div
) + bmap
->extra
;
4460 old
= bmap
->block2
.data
;
4461 bmap
->block2
= isl_blk_extend(bmap
->ctx
, bmap
->block2
,
4462 (bmap
->extra
+ n
) * (1 + row_size
));
4463 if (!bmap
->block2
.data
)
4464 return isl_basic_map_free(bmap
);
4465 new_div
= isl_alloc_array(bmap
->ctx
, isl_int
*, bmap
->extra
+ n
);
4467 return isl_basic_map_free(bmap
);
4468 for (i
= 0; i
< n
; ++i
) {
4469 new_div
[i
] = bmap
->block2
.data
+
4470 (bmap
->extra
+ i
) * (1 + row_size
);
4471 isl_seq_clr(new_div
[i
], 1 + row_size
);
4473 for (i
= 0; i
< bmap
->extra
; ++i
)
4474 new_div
[n
+ i
] = bmap
->block2
.data
+ (bmap
->div
[i
] - old
);
4476 bmap
->div
= new_div
;
4483 /* Drop constraints from "bmap" that only involve the variables
4484 * of "type" in the range [first, first + n] that are not related
4485 * to any of the variables outside that interval.
4486 * These constraints cannot influence the values for the variables
4487 * outside the interval, except in case they cause "bmap" to be empty.
4488 * Only drop the constraints if "bmap" is known to be non-empty.
4490 static __isl_give isl_basic_map
*drop_irrelevant_constraints(
4491 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
4492 unsigned first
, unsigned n
)
4496 isl_size dim
, n_div
;
4499 non_empty
= isl_basic_map_plain_is_non_empty(bmap
);
4501 return isl_basic_map_free(bmap
);
4505 dim
= isl_basic_map_dim(bmap
, isl_dim_all
);
4506 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4507 if (dim
< 0 || n_div
< 0)
4508 return isl_basic_map_free(bmap
);
4509 groups
= isl_calloc_array(isl_basic_map_get_ctx(bmap
), int, dim
);
4511 return isl_basic_map_free(bmap
);
4512 first
+= isl_basic_map_offset(bmap
, type
) - 1;
4513 for (i
= 0; i
< first
; ++i
)
4515 for (i
= first
+ n
; i
< dim
- n_div
; ++i
)
4518 bmap
= isl_basic_map_drop_unrelated_constraints(bmap
, groups
);
4523 /* Turn the n dimensions of type type, starting at first
4524 * into existentially quantified variables.
4526 * If a subset of the projected out variables are unrelated
4527 * to any of the variables that remain, then the constraints
4528 * involving this subset are simply dropped first.
4530 __isl_give isl_basic_map
*isl_basic_map_project_out(
4531 __isl_take isl_basic_map
*bmap
,
4532 enum isl_dim_type type
, unsigned first
, unsigned n
)
4538 return basic_map_space_reset(bmap
, type
);
4539 if (type
== isl_dim_div
)
4540 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
4541 "cannot project out existentially quantified variables",
4542 return isl_basic_map_free(bmap
));
4544 empty
= isl_basic_map_plain_is_empty(bmap
);
4546 return isl_basic_map_free(bmap
);
4548 bmap
= isl_basic_map_set_to_empty(bmap
);
4550 bmap
= drop_irrelevant_constraints(bmap
, type
, first
, n
);
4554 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
4555 return isl_basic_map_remove_dims(bmap
, type
, first
, n
);
4557 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
4558 return isl_basic_map_free(bmap
);
4560 bmap
= move_last(bmap
, type
, first
, n
);
4561 bmap
= isl_basic_map_cow(bmap
);
4562 bmap
= insert_div_rows(bmap
, n
);
4564 space
= isl_basic_map_take_space(bmap
);
4565 space
= isl_space_drop_dims(space
, type
, first
, n
);
4566 bmap
= isl_basic_map_restore_space(bmap
, space
);
4567 bmap
= isl_basic_map_simplify(bmap
);
4568 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
4569 return isl_basic_map_finalize(bmap
);
4572 /* Turn the n dimensions of type type, starting at first
4573 * into existentially quantified variables.
4575 __isl_give isl_basic_set
*isl_basic_set_project_out(
4576 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
,
4577 unsigned first
, unsigned n
)
4579 return bset_from_bmap(isl_basic_map_project_out(bset_to_bmap(bset
),
4583 /* Turn the n dimensions of type type, starting at first
4584 * into existentially quantified variables.
4586 __isl_give isl_map
*isl_map_project_out(__isl_take isl_map
*map
,
4587 enum isl_dim_type type
, unsigned first
, unsigned n
)
4593 return map_space_reset(map
, type
);
4595 if (isl_map_check_range(map
, type
, first
, n
) < 0)
4596 return isl_map_free(map
);
4598 map
= isl_map_cow(map
);
4602 for (i
= 0; i
< map
->n
; ++i
) {
4603 map
->p
[i
] = isl_basic_map_project_out(map
->p
[i
], type
, first
, n
);
4609 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
4610 map
= isl_map_unmark_normalized(map
);
4612 space
= isl_map_take_space(map
);
4613 space
= isl_space_drop_dims(space
, type
, first
, n
);
4614 map
= isl_map_restore_space(map
, space
);
4623 #define TYPE isl_map
4624 #include "isl_project_out_all_params_templ.c"
4626 /* Turn all the dimensions of type "type", except the "n" starting at "first"
4627 * into existentially quantified variables.
4629 __isl_give isl_map
*isl_map_project_onto(__isl_take isl_map
*map
,
4630 enum isl_dim_type type
, unsigned first
, unsigned n
)
4634 dim
= isl_map_dim(map
, type
);
4635 if (isl_map_check_range(map
, type
, first
, n
) < 0 || dim
< 0)
4636 return isl_map_free(map
);
4637 map
= isl_map_project_out(map
, type
, first
+ n
, dim
- (first
+ n
));
4638 map
= isl_map_project_out(map
, type
, 0, first
);
4642 /* Turn the n dimensions of type type, starting at first
4643 * into existentially quantified variables.
4645 __isl_give isl_set
*isl_set_project_out(__isl_take isl_set
*set
,
4646 enum isl_dim_type type
, unsigned first
, unsigned n
)
4648 return set_from_map(isl_map_project_out(set_to_map(set
),
4652 /* If "set" involves a parameter with identifier "id",
4653 * then turn it into an existentially quantified variable.
4655 __isl_give isl_set
*isl_set_project_out_param_id(__isl_take isl_set
*set
,
4656 __isl_take isl_id
*id
)
4662 pos
= isl_set_find_dim_by_id(set
, isl_dim_param
, id
);
4666 return isl_set_project_out(set
, isl_dim_param
, pos
, 1);
4673 /* If "set" involves any of the parameters with identifiers in "list",
4674 * then turn them into existentially quantified variables.
4676 __isl_give isl_set
*isl_set_project_out_param_id_list(__isl_take isl_set
*set
,
4677 __isl_take isl_id_list
*list
)
4682 n
= isl_id_list_size(list
);
4685 for (i
= 0; i
< n
; ++i
) {
4688 id
= isl_id_list_get_at(list
, i
);
4689 set
= isl_set_project_out_param_id(set
, id
);
4692 isl_id_list_free(list
);
4695 isl_id_list_free(list
);
4700 /* Project out all parameters from "set" by existentially quantifying
4703 __isl_give isl_set
*isl_set_project_out_all_params(__isl_take isl_set
*set
)
4705 return set_from_map(isl_map_project_out_all_params(set_to_map(set
)));
4708 /* Return a map that projects the elements in "set" onto their
4709 * "n" set dimensions starting at "first".
4710 * "type" should be equal to isl_dim_set.
4712 __isl_give isl_map
*isl_set_project_onto_map(__isl_take isl_set
*set
,
4713 enum isl_dim_type type
, unsigned first
, unsigned n
)
4718 if (type
!= isl_dim_set
)
4719 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
4720 "only set dimensions can be projected out", goto error
);
4721 if (isl_set_check_range(set
, type
, first
, n
) < 0)
4722 return isl_set_free(set
);
4724 map
= isl_map_from_domain(set
);
4725 map
= isl_map_add_dims(map
, isl_dim_out
, n
);
4726 for (i
= 0; i
< n
; ++i
)
4727 map
= isl_map_equate(map
, isl_dim_in
, first
+ i
,
4735 static __isl_give isl_basic_map
*add_divs(__isl_take isl_basic_map
*bmap
,
4741 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4743 return isl_basic_map_free(bmap
);
4744 for (i
= 0; i
< n
; ++i
) {
4745 j
= isl_basic_map_alloc_div(bmap
);
4748 isl_seq_clr(bmap
->div
[j
], 1 + 1 + total
);
4752 isl_basic_map_free(bmap
);
4756 /* Does "bmap2" apply to the range of "bmap1" (ignoring parameters)?
4758 isl_bool
isl_basic_map_applies_range(__isl_keep isl_basic_map
*bmap1
,
4759 __isl_keep isl_basic_map
*bmap2
)
4761 isl_space
*space1
, *space2
;
4763 space1
= isl_basic_map_peek_space(bmap1
);
4764 space2
= isl_basic_map_peek_space(bmap2
);
4765 return isl_space_tuple_is_equal(space1
, isl_dim_out
,
4766 space2
, isl_dim_in
);
4769 /* Check that "bmap2" applies to the range of "bmap1" (ignoring parameters).
4771 static isl_stat
isl_basic_map_check_applies_range(
4772 __isl_keep isl_basic_map
*bmap1
, __isl_keep isl_basic_map
*bmap2
)
4776 equal
= isl_basic_map_applies_range(bmap1
, bmap2
);
4778 return isl_stat_error
;
4780 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
4781 "spaces don't match", return isl_stat_error
);
4785 __isl_give isl_basic_map
*isl_basic_map_apply_range(
4786 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
4788 isl_space
*space_result
= NULL
;
4789 struct isl_basic_map
*bmap
;
4790 isl_size n_in
, n_out
, n
, nparam
;
4791 unsigned total
, pos
;
4792 struct isl_dim_map
*dim_map1
, *dim_map2
;
4794 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
4796 if (isl_basic_map_check_applies_range(bmap1
, bmap2
) < 0)
4799 n_in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
4800 n_out
= isl_basic_map_dim(bmap2
, isl_dim_out
);
4801 n
= isl_basic_map_dim(bmap1
, isl_dim_out
);
4802 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
4803 if (n_in
< 0 || n_out
< 0 || n
< 0 || nparam
< 0)
4806 space_result
= isl_space_join(isl_basic_map_get_space(bmap1
),
4807 isl_basic_map_get_space(bmap2
));
4809 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ n
;
4810 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4811 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4812 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
4813 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
4814 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
4815 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_in
);
4816 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_out
);
4817 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
4818 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
4819 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
4821 bmap
= isl_basic_map_alloc_space(space_result
,
4822 bmap1
->n_div
+ bmap2
->n_div
+ n
,
4823 bmap1
->n_eq
+ bmap2
->n_eq
,
4824 bmap1
->n_ineq
+ bmap2
->n_ineq
);
4825 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
4826 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
4827 bmap
= add_divs(bmap
, n
);
4828 bmap
= isl_basic_map_simplify(bmap
);
4829 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
4830 return isl_basic_map_finalize(bmap
);
4832 isl_basic_map_free(bmap1
);
4833 isl_basic_map_free(bmap2
);
4837 __isl_give isl_basic_set
*isl_basic_set_apply(__isl_take isl_basic_set
*bset
,
4838 __isl_take isl_basic_map
*bmap
)
4840 if (isl_basic_map_check_compatible_domain(bmap
, bset
) < 0)
4843 return bset_from_bmap(isl_basic_map_apply_range(bset_to_bmap(bset
),
4846 isl_basic_set_free(bset
);
4847 isl_basic_map_free(bmap
);
4851 __isl_give isl_basic_map
*isl_basic_map_apply_domain(
4852 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
4854 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
4856 if (!isl_space_tuple_is_equal(bmap1
->dim
, isl_dim_in
,
4857 bmap2
->dim
, isl_dim_in
))
4858 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
4859 "spaces don't match", goto error
);
4861 bmap1
= isl_basic_map_reverse(bmap1
);
4862 bmap1
= isl_basic_map_apply_range(bmap1
, bmap2
);
4863 return isl_basic_map_reverse(bmap1
);
4865 isl_basic_map_free(bmap1
);
4866 isl_basic_map_free(bmap2
);
4870 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
4871 * A \cap B -> f(A) + f(B)
4873 __isl_give isl_basic_map
*isl_basic_map_sum(__isl_take isl_basic_map
*bmap1
,
4874 __isl_take isl_basic_map
*bmap2
)
4876 isl_size n_in
, n_out
, nparam
;
4877 unsigned total
, pos
;
4878 struct isl_basic_map
*bmap
= NULL
;
4879 struct isl_dim_map
*dim_map1
, *dim_map2
;
4882 if (isl_basic_map_check_equal_space(bmap1
, bmap2
) < 0)
4885 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
4886 n_in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
4887 n_out
= isl_basic_map_dim(bmap1
, isl_dim_out
);
4888 if (nparam
< 0 || n_in
< 0 || n_out
< 0)
4891 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
;
4892 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4893 dim_map2
= isl_dim_map_alloc(bmap2
->ctx
, total
);
4894 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
4895 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
);
4896 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
4897 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
4898 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_in
+ n_out
);
4899 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
4900 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
4901 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_out
);
4903 bmap
= isl_basic_map_alloc_space(isl_space_copy(bmap1
->dim
),
4904 bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
,
4905 bmap1
->n_eq
+ bmap2
->n_eq
+ n_out
,
4906 bmap1
->n_ineq
+ bmap2
->n_ineq
);
4907 for (i
= 0; i
< n_out
; ++i
) {
4908 int j
= isl_basic_map_alloc_equality(bmap
);
4911 isl_seq_clr(bmap
->eq
[j
], 1+total
);
4912 isl_int_set_si(bmap
->eq
[j
][1+nparam
+n_in
+i
], -1);
4913 isl_int_set_si(bmap
->eq
[j
][1+pos
+i
], 1);
4914 isl_int_set_si(bmap
->eq
[j
][1+pos
-n_out
+i
], 1);
4916 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
4917 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
4918 bmap
= add_divs(bmap
, 2 * n_out
);
4920 bmap
= isl_basic_map_simplify(bmap
);
4921 return isl_basic_map_finalize(bmap
);
4923 isl_basic_map_free(bmap
);
4924 isl_basic_map_free(bmap1
);
4925 isl_basic_map_free(bmap2
);
4929 /* Given two maps A -> f(A) and B -> g(B), construct a map
4930 * A \cap B -> f(A) + f(B)
4932 __isl_give isl_map
*isl_map_sum(__isl_take isl_map
*map1
,
4933 __isl_take isl_map
*map2
)
4935 struct isl_map
*result
;
4938 if (isl_map_check_equal_space(map1
, map2
) < 0)
4941 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
4942 map1
->n
* map2
->n
, 0);
4945 for (i
= 0; i
< map1
->n
; ++i
)
4946 for (j
= 0; j
< map2
->n
; ++j
) {
4947 struct isl_basic_map
*part
;
4948 part
= isl_basic_map_sum(
4949 isl_basic_map_copy(map1
->p
[i
]),
4950 isl_basic_map_copy(map2
->p
[j
]));
4951 if (isl_basic_map_is_empty(part
))
4952 isl_basic_map_free(part
);
4954 result
= isl_map_add_basic_map(result
, part
);
4967 __isl_give isl_set
*isl_set_sum(__isl_take isl_set
*set1
,
4968 __isl_take isl_set
*set2
)
4970 return set_from_map(isl_map_sum(set_to_map(set1
), set_to_map(set2
)));
4973 /* Given a basic map A -> f(A), construct A -> -f(A).
4975 __isl_give isl_basic_map
*isl_basic_map_neg(__isl_take isl_basic_map
*bmap
)
4981 bmap
= isl_basic_map_cow(bmap
);
4982 n
= isl_basic_map_dim(bmap
, isl_dim_out
);
4984 return isl_basic_map_free(bmap
);
4986 off
= isl_basic_map_offset(bmap
, isl_dim_out
);
4987 for (i
= 0; i
< bmap
->n_eq
; ++i
)
4988 for (j
= 0; j
< n
; ++j
)
4989 isl_int_neg(bmap
->eq
[i
][off
+j
], bmap
->eq
[i
][off
+j
]);
4990 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
4991 for (j
= 0; j
< n
; ++j
)
4992 isl_int_neg(bmap
->ineq
[i
][off
+j
], bmap
->ineq
[i
][off
+j
]);
4993 for (i
= 0; i
< bmap
->n_div
; ++i
)
4994 for (j
= 0; j
< n
; ++j
)
4995 isl_int_neg(bmap
->div
[i
][1+off
+j
], bmap
->div
[i
][1+off
+j
]);
4996 bmap
= isl_basic_map_gauss(bmap
, NULL
);
4997 return isl_basic_map_finalize(bmap
);
5000 __isl_give isl_basic_set
*isl_basic_set_neg(__isl_take isl_basic_set
*bset
)
5002 return isl_basic_map_neg(bset
);
5005 /* Given a map A -> f(A), construct A -> -f(A).
5007 __isl_give isl_map
*isl_map_neg(__isl_take isl_map
*map
)
5011 map
= isl_map_cow(map
);
5015 for (i
= 0; i
< map
->n
; ++i
) {
5016 map
->p
[i
] = isl_basic_map_neg(map
->p
[i
]);
5027 __isl_give isl_set
*isl_set_neg(__isl_take isl_set
*set
)
5029 return set_from_map(isl_map_neg(set_to_map(set
)));
5032 /* Given a basic map A -> f(A) and an integer d, construct a basic map
5033 * A -> floor(f(A)/d).
5035 __isl_give isl_basic_map
*isl_basic_map_floordiv(__isl_take isl_basic_map
*bmap
,
5038 isl_size n_in
, n_out
, nparam
;
5039 unsigned total
, pos
;
5040 struct isl_basic_map
*result
= NULL
;
5041 struct isl_dim_map
*dim_map
;
5044 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5045 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5046 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5047 if (nparam
< 0 || n_in
< 0 || n_out
< 0)
5048 return isl_basic_map_free(bmap
);
5050 total
= nparam
+ n_in
+ n_out
+ bmap
->n_div
+ n_out
;
5051 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
5052 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_param
, pos
= 0);
5053 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_in
, pos
+= nparam
);
5054 isl_dim_map_div(dim_map
, bmap
, pos
+= n_in
+ n_out
);
5055 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_out
, pos
+= bmap
->n_div
);
5057 result
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
5058 bmap
->n_div
+ n_out
,
5059 bmap
->n_eq
, bmap
->n_ineq
+ 2 * n_out
);
5060 result
= isl_basic_map_add_constraints_dim_map(result
, bmap
, dim_map
);
5061 result
= add_divs(result
, n_out
);
5062 for (i
= 0; i
< n_out
; ++i
) {
5064 j
= isl_basic_map_alloc_inequality(result
);
5067 isl_seq_clr(result
->ineq
[j
], 1+total
);
5068 isl_int_neg(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
5069 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], 1);
5070 j
= isl_basic_map_alloc_inequality(result
);
5073 isl_seq_clr(result
->ineq
[j
], 1+total
);
5074 isl_int_set(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
5075 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], -1);
5076 isl_int_sub_ui(result
->ineq
[j
][0], d
, 1);
5079 result
= isl_basic_map_simplify(result
);
5080 return isl_basic_map_finalize(result
);
5082 isl_basic_map_free(result
);
5086 /* Given a map A -> f(A) and an integer d, construct a map
5087 * A -> floor(f(A)/d).
5089 __isl_give isl_map
*isl_map_floordiv(__isl_take isl_map
*map
, isl_int d
)
5093 map
= isl_map_cow(map
);
5097 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
5098 for (i
= 0; i
< map
->n
; ++i
) {
5099 map
->p
[i
] = isl_basic_map_floordiv(map
->p
[i
], d
);
5103 map
= isl_map_unmark_normalized(map
);
5111 /* Given a map A -> f(A) and an integer d, construct a map
5112 * A -> floor(f(A)/d).
5114 __isl_give isl_map
*isl_map_floordiv_val(__isl_take isl_map
*map
,
5115 __isl_take isl_val
*d
)
5119 if (!isl_val_is_int(d
))
5120 isl_die(isl_val_get_ctx(d
), isl_error_invalid
,
5121 "expecting integer denominator", goto error
);
5122 map
= isl_map_floordiv(map
, d
->n
);
5131 static __isl_give isl_basic_map
*var_equal(__isl_take isl_basic_map
*bmap
,
5139 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5140 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5141 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5142 if (total
< 0 || nparam
< 0 || n_in
< 0)
5143 return isl_basic_map_free(bmap
);
5144 i
= isl_basic_map_alloc_equality(bmap
);
5147 isl_seq_clr(bmap
->eq
[i
], 1 + total
);
5148 isl_int_set_si(bmap
->eq
[i
][1+nparam
+pos
], -1);
5149 isl_int_set_si(bmap
->eq
[i
][1+nparam
+n_in
+pos
], 1);
5150 return isl_basic_map_finalize(bmap
);
5152 isl_basic_map_free(bmap
);
5156 /* Add a constraint to "bmap" expressing i_pos < o_pos
5158 static __isl_give isl_basic_map
*var_less(__isl_take isl_basic_map
*bmap
,
5166 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5167 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5168 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5169 if (total
< 0 || nparam
< 0 || n_in
< 0)
5170 return isl_basic_map_free(bmap
);
5171 i
= isl_basic_map_alloc_inequality(bmap
);
5174 isl_seq_clr(bmap
->ineq
[i
], 1 + total
);
5175 isl_int_set_si(bmap
->ineq
[i
][0], -1);
5176 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
5177 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
5178 return isl_basic_map_finalize(bmap
);
5180 isl_basic_map_free(bmap
);
5184 /* Add a constraint to "bmap" expressing i_pos <= o_pos
5186 static __isl_give isl_basic_map
*var_less_or_equal(
5187 __isl_take isl_basic_map
*bmap
, unsigned pos
)
5194 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5195 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5196 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5197 if (total
< 0 || nparam
< 0 || n_in
< 0)
5198 return isl_basic_map_free(bmap
);
5199 i
= isl_basic_map_alloc_inequality(bmap
);
5202 isl_seq_clr(bmap
->ineq
[i
], 1 + total
);
5203 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
5204 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
5205 return isl_basic_map_finalize(bmap
);
5207 isl_basic_map_free(bmap
);
5211 /* Add a constraint to "bmap" expressing i_pos > o_pos
5213 static __isl_give isl_basic_map
*var_more(__isl_take isl_basic_map
*bmap
,
5221 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5222 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5223 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5224 if (total
< 0 || nparam
< 0 || n_in
< 0)
5225 return isl_basic_map_free(bmap
);
5226 i
= isl_basic_map_alloc_inequality(bmap
);
5229 isl_seq_clr(bmap
->ineq
[i
], 1 + total
);
5230 isl_int_set_si(bmap
->ineq
[i
][0], -1);
5231 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
5232 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
5233 return isl_basic_map_finalize(bmap
);
5235 isl_basic_map_free(bmap
);
5239 /* Add a constraint to "bmap" expressing i_pos >= o_pos
5241 static __isl_give isl_basic_map
*var_more_or_equal(
5242 __isl_take isl_basic_map
*bmap
, unsigned pos
)
5249 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5250 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5251 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5252 if (total
< 0 || nparam
< 0 || n_in
< 0)
5253 return isl_basic_map_free(bmap
);
5254 i
= isl_basic_map_alloc_inequality(bmap
);
5257 isl_seq_clr(bmap
->ineq
[i
], 1 + total
);
5258 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
5259 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
5260 return isl_basic_map_finalize(bmap
);
5262 isl_basic_map_free(bmap
);
5266 __isl_give isl_basic_map
*isl_basic_map_equal(
5267 __isl_take isl_space
*space
, unsigned n_equal
)
5270 struct isl_basic_map
*bmap
;
5271 bmap
= isl_basic_map_alloc_space(space
, 0, n_equal
, 0);
5274 for (i
= 0; i
< n_equal
&& bmap
; ++i
)
5275 bmap
= var_equal(bmap
, i
);
5276 return isl_basic_map_finalize(bmap
);
5279 /* Return a relation on of dimension "space" expressing i_[0..pos] << o_[0..pos]
5281 __isl_give isl_basic_map
*isl_basic_map_less_at(__isl_take isl_space
*space
,
5285 struct isl_basic_map
*bmap
;
5286 bmap
= isl_basic_map_alloc_space(space
, 0, pos
, 1);
5289 for (i
= 0; i
< pos
&& bmap
; ++i
)
5290 bmap
= var_equal(bmap
, i
);
5292 bmap
= var_less(bmap
, pos
);
5293 return isl_basic_map_finalize(bmap
);
5296 /* Return a relation on "space" expressing i_[0..pos] <<= o_[0..pos]
5298 __isl_give isl_basic_map
*isl_basic_map_less_or_equal_at(
5299 __isl_take isl_space
*space
, unsigned pos
)
5302 isl_basic_map
*bmap
;
5304 bmap
= isl_basic_map_alloc_space(space
, 0, pos
, 1);
5305 for (i
= 0; i
< pos
; ++i
)
5306 bmap
= var_equal(bmap
, i
);
5307 bmap
= var_less_or_equal(bmap
, pos
);
5308 return isl_basic_map_finalize(bmap
);
5311 /* Return a relation on "space" expressing i_pos > o_pos
5313 __isl_give isl_basic_map
*isl_basic_map_more_at(__isl_take isl_space
*space
,
5317 struct isl_basic_map
*bmap
;
5318 bmap
= isl_basic_map_alloc_space(space
, 0, pos
, 1);
5321 for (i
= 0; i
< pos
&& bmap
; ++i
)
5322 bmap
= var_equal(bmap
, i
);
5324 bmap
= var_more(bmap
, pos
);
5325 return isl_basic_map_finalize(bmap
);
5328 /* Return a relation on "space" expressing i_[0..pos] >>= o_[0..pos]
5330 __isl_give isl_basic_map
*isl_basic_map_more_or_equal_at(
5331 __isl_take isl_space
*space
, unsigned pos
)
5334 isl_basic_map
*bmap
;
5336 bmap
= isl_basic_map_alloc_space(space
, 0, pos
, 1);
5337 for (i
= 0; i
< pos
; ++i
)
5338 bmap
= var_equal(bmap
, i
);
5339 bmap
= var_more_or_equal(bmap
, pos
);
5340 return isl_basic_map_finalize(bmap
);
5343 static __isl_give isl_map
*map_lex_lte_first(__isl_take isl_space
*space
,
5344 unsigned n
, int equal
)
5346 struct isl_map
*map
;
5349 if (n
== 0 && equal
)
5350 return isl_map_universe(space
);
5352 map
= isl_map_alloc_space(isl_space_copy(space
), n
, ISL_MAP_DISJOINT
);
5354 for (i
= 0; i
+ 1 < n
; ++i
)
5355 map
= isl_map_add_basic_map(map
,
5356 isl_basic_map_less_at(isl_space_copy(space
), i
));
5359 map
= isl_map_add_basic_map(map
,
5360 isl_basic_map_less_or_equal_at(space
, n
- 1));
5362 map
= isl_map_add_basic_map(map
,
5363 isl_basic_map_less_at(space
, n
- 1));
5365 isl_space_free(space
);
5370 static __isl_give isl_map
*map_lex_lte(__isl_take isl_space
*space
, int equal
)
5374 return map_lex_lte_first(space
, space
->n_out
, equal
);
5377 __isl_give isl_map
*isl_map_lex_lt_first(__isl_take isl_space
*space
,
5380 return map_lex_lte_first(space
, n
, 0);
5383 __isl_give isl_map
*isl_map_lex_le_first(__isl_take isl_space
*space
,
5386 return map_lex_lte_first(space
, n
, 1);
5389 __isl_give isl_map
*isl_map_lex_lt(__isl_take isl_space
*set_space
)
5391 return map_lex_lte(isl_space_map_from_set(set_space
), 0);
5394 __isl_give isl_map
*isl_map_lex_le(__isl_take isl_space
*set_space
)
5396 return map_lex_lte(isl_space_map_from_set(set_space
), 1);
5399 static __isl_give isl_map
*map_lex_gte_first(__isl_take isl_space
*space
,
5400 unsigned n
, int equal
)
5402 struct isl_map
*map
;
5405 if (n
== 0 && equal
)
5406 return isl_map_universe(space
);
5408 map
= isl_map_alloc_space(isl_space_copy(space
), n
, ISL_MAP_DISJOINT
);
5410 for (i
= 0; i
+ 1 < n
; ++i
)
5411 map
= isl_map_add_basic_map(map
,
5412 isl_basic_map_more_at(isl_space_copy(space
), i
));
5415 map
= isl_map_add_basic_map(map
,
5416 isl_basic_map_more_or_equal_at(space
, n
- 1));
5418 map
= isl_map_add_basic_map(map
,
5419 isl_basic_map_more_at(space
, n
- 1));
5421 isl_space_free(space
);
5426 static __isl_give isl_map
*map_lex_gte(__isl_take isl_space
*space
, int equal
)
5430 return map_lex_gte_first(space
, space
->n_out
, equal
);
5433 __isl_give isl_map
*isl_map_lex_gt_first(__isl_take isl_space
*space
,
5436 return map_lex_gte_first(space
, n
, 0);
5439 __isl_give isl_map
*isl_map_lex_ge_first(__isl_take isl_space
*space
,
5442 return map_lex_gte_first(space
, n
, 1);
5445 __isl_give isl_map
*isl_map_lex_gt(__isl_take isl_space
*set_space
)
5447 return map_lex_gte(isl_space_map_from_set(set_space
), 0);
5450 __isl_give isl_map
*isl_map_lex_ge(__isl_take isl_space
*set_space
)
5452 return map_lex_gte(isl_space_map_from_set(set_space
), 1);
5455 __isl_give isl_map
*isl_set_lex_le_set(__isl_take isl_set
*set1
,
5456 __isl_take isl_set
*set2
)
5459 map
= isl_map_lex_le(isl_set_get_space(set1
));
5460 map
= isl_map_intersect_domain(map
, set1
);
5461 map
= isl_map_intersect_range(map
, set2
);
5465 __isl_give isl_map
*isl_set_lex_lt_set(__isl_take isl_set
*set1
,
5466 __isl_take isl_set
*set2
)
5469 map
= isl_map_lex_lt(isl_set_get_space(set1
));
5470 map
= isl_map_intersect_domain(map
, set1
);
5471 map
= isl_map_intersect_range(map
, set2
);
5475 __isl_give isl_map
*isl_set_lex_ge_set(__isl_take isl_set
*set1
,
5476 __isl_take isl_set
*set2
)
5479 map
= isl_map_lex_ge(isl_set_get_space(set1
));
5480 map
= isl_map_intersect_domain(map
, set1
);
5481 map
= isl_map_intersect_range(map
, set2
);
5485 __isl_give isl_map
*isl_set_lex_gt_set(__isl_take isl_set
*set1
,
5486 __isl_take isl_set
*set2
)
5489 map
= isl_map_lex_gt(isl_set_get_space(set1
));
5490 map
= isl_map_intersect_domain(map
, set1
);
5491 map
= isl_map_intersect_range(map
, set2
);
5495 __isl_give isl_map
*isl_map_lex_le_map(__isl_take isl_map
*map1
,
5496 __isl_take isl_map
*map2
)
5499 map
= isl_map_lex_le(isl_space_range(isl_map_get_space(map1
)));
5500 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
5501 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
5505 __isl_give isl_map
*isl_map_lex_lt_map(__isl_take isl_map
*map1
,
5506 __isl_take isl_map
*map2
)
5509 map
= isl_map_lex_lt(isl_space_range(isl_map_get_space(map1
)));
5510 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
5511 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
5515 __isl_give isl_map
*isl_map_lex_ge_map(__isl_take isl_map
*map1
,
5516 __isl_take isl_map
*map2
)
5519 map
= isl_map_lex_ge(isl_space_range(isl_map_get_space(map1
)));
5520 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
5521 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
5525 __isl_give isl_map
*isl_map_lex_gt_map(__isl_take isl_map
*map1
,
5526 __isl_take isl_map
*map2
)
5529 map
= isl_map_lex_gt(isl_space_range(isl_map_get_space(map1
)));
5530 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
5531 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
5535 /* For the div d = floor(f/m) at position "div", add the constraint
5539 static __isl_give isl_basic_map
*add_upper_div_constraint(
5540 __isl_take isl_basic_map
*bmap
, unsigned div
)
5543 isl_size v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
5547 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
5548 if (v_div
< 0 || n_div
< 0)
5549 return isl_basic_map_free(bmap
);
5551 i
= isl_basic_map_alloc_inequality(bmap
);
5553 return isl_basic_map_free(bmap
);
5554 isl_seq_cpy(bmap
->ineq
[i
], bmap
->div
[div
] + 1, 1 + v_div
+ n_div
);
5555 isl_int_neg(bmap
->ineq
[i
][1 + pos
], bmap
->div
[div
][0]);
5560 /* For the div d = floor(f/m) at position "div", add the constraint
5562 * -(f-(m-1)) + m d >= 0
5564 static __isl_give isl_basic_map
*add_lower_div_constraint(
5565 __isl_take isl_basic_map
*bmap
, unsigned div
)
5568 isl_size v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
5572 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
5573 if (v_div
< 0 || n_div
< 0)
5574 return isl_basic_map_free(bmap
);
5576 i
= isl_basic_map_alloc_inequality(bmap
);
5578 return isl_basic_map_free(bmap
);
5579 isl_seq_neg(bmap
->ineq
[i
], bmap
->div
[div
] + 1, 1 + v_div
+ n_div
);
5580 isl_int_set(bmap
->ineq
[i
][1 + pos
], bmap
->div
[div
][0]);
5581 isl_int_add(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], bmap
->ineq
[i
][1 + pos
]);
5582 isl_int_sub_ui(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], 1);
5587 /* For the div d = floor(f/m) at position "pos", add the constraints
5590 * -(f-(m-1)) + m d >= 0
5592 * Note that the second constraint is the negation of
5596 __isl_give isl_basic_map
*isl_basic_map_add_div_constraints(
5597 __isl_take isl_basic_map
*bmap
, unsigned pos
)
5599 bmap
= add_upper_div_constraint(bmap
, pos
);
5600 bmap
= add_lower_div_constraint(bmap
, pos
);
5604 /* For each known div d = floor(f/m), add the constraints
5607 * -(f-(m-1)) + m d >= 0
5609 * Remove duplicate constraints in case of some these div constraints
5610 * already appear in "bmap".
5612 __isl_give isl_basic_map
*isl_basic_map_add_known_div_constraints(
5613 __isl_take isl_basic_map
*bmap
)
5617 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
5619 return isl_basic_map_free(bmap
);
5623 bmap
= add_known_div_constraints(bmap
);
5624 bmap
= isl_basic_map_remove_duplicate_constraints(bmap
, NULL
, 0);
5625 bmap
= isl_basic_map_finalize(bmap
);
5629 /* Add the div constraint of sign "sign" for div "div" of "bmap".
5631 * In particular, if this div is of the form d = floor(f/m),
5632 * then add the constraint
5636 * if sign < 0 or the constraint
5638 * -(f-(m-1)) + m d >= 0
5642 __isl_give isl_basic_map
*isl_basic_map_add_div_constraint(
5643 __isl_take isl_basic_map
*bmap
, unsigned div
, int sign
)
5646 return add_upper_div_constraint(bmap
, div
);
5648 return add_lower_div_constraint(bmap
, div
);
5651 __isl_give isl_basic_set
*isl_basic_map_underlying_set(
5652 __isl_take isl_basic_map
*bmap
)
5658 if (bmap
->dim
->nparam
== 0 && bmap
->dim
->n_in
== 0 &&
5660 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_in
) &&
5661 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_out
))
5662 return bset_from_bmap(bmap
);
5663 bmap
= isl_basic_map_cow(bmap
);
5666 space
= isl_basic_map_take_space(bmap
);
5667 space
= isl_space_underlying(space
, bmap
->n_div
);
5668 bmap
= isl_basic_map_restore_space(bmap
, space
);
5671 bmap
->extra
-= bmap
->n_div
;
5673 bmap
= isl_basic_map_finalize(bmap
);
5674 return bset_from_bmap(bmap
);
5676 isl_basic_map_free(bmap
);
5680 __isl_give isl_basic_set
*isl_basic_set_underlying_set(
5681 __isl_take isl_basic_set
*bset
)
5683 return isl_basic_map_underlying_set(bset_to_bmap(bset
));
5686 /* Replace each element in "list" by the result of applying
5687 * isl_basic_map_underlying_set to the element.
5689 __isl_give isl_basic_set_list
*isl_basic_map_list_underlying_set(
5690 __isl_take isl_basic_map_list
*list
)
5695 n
= isl_basic_map_list_n_basic_map(list
);
5699 for (i
= 0; i
< n
; ++i
) {
5700 isl_basic_map
*bmap
;
5701 isl_basic_set
*bset
;
5703 bmap
= isl_basic_map_list_get_basic_map(list
, i
);
5704 bset
= isl_basic_set_underlying_set(bmap
);
5705 list
= isl_basic_set_list_set_basic_set(list
, i
, bset
);
5710 isl_basic_map_list_free(list
);
5714 __isl_give isl_basic_map
*isl_basic_map_overlying_set(
5715 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_map
*like
)
5717 struct isl_basic_map
*bmap
;
5718 struct isl_ctx
*ctx
;
5719 isl_size dim
, bmap_total
;
5726 if (isl_basic_set_check_no_params(bset
) < 0 ||
5727 isl_basic_set_check_no_locals(bset
) < 0)
5729 dim
= isl_basic_set_dim(bset
, isl_dim_set
);
5730 bmap_total
= isl_basic_map_dim(like
, isl_dim_all
);
5731 if (dim
< 0 || bmap_total
< 0)
5733 isl_assert(ctx
, dim
== bmap_total
, goto error
);
5734 if (like
->n_div
== 0) {
5735 isl_space
*space
= isl_basic_map_get_space(like
);
5736 isl_basic_map_free(like
);
5737 return isl_basic_map_reset_space(bset
, space
);
5739 bset
= isl_basic_set_cow(bset
);
5742 total
= dim
+ bset
->extra
;
5743 bmap
= bset_to_bmap(bset
);
5744 isl_space_free(isl_basic_map_take_space(bmap
));
5745 bmap
= isl_basic_map_restore_space(bmap
, isl_basic_map_get_space(like
));
5748 bmap
->n_div
= like
->n_div
;
5749 bmap
->extra
+= like
->n_div
;
5753 ltotal
= total
- bmap
->extra
+ like
->extra
;
5756 bmap
->block2
= isl_blk_extend(ctx
, bmap
->block2
,
5757 bmap
->extra
* (1 + 1 + total
));
5758 if (isl_blk_is_error(bmap
->block2
))
5760 div
= isl_realloc_array(ctx
, bmap
->div
, isl_int
*, bmap
->extra
);
5764 for (i
= 0; i
< bmap
->extra
; ++i
)
5765 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + 1 + total
);
5766 for (i
= 0; i
< like
->n_div
; ++i
) {
5767 isl_seq_cpy(bmap
->div
[i
], like
->div
[i
], 1 + 1 + ltotal
);
5768 isl_seq_clr(bmap
->div
[i
]+1+1+ltotal
, total
- ltotal
);
5770 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
5772 isl_basic_map_free(like
);
5773 bmap
= isl_basic_map_simplify(bmap
);
5774 bmap
= isl_basic_map_finalize(bmap
);
5777 isl_basic_map_free(like
);
5778 isl_basic_set_free(bset
);
5782 __isl_give isl_basic_set
*isl_basic_set_from_underlying_set(
5783 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*like
)
5785 return bset_from_bmap(isl_basic_map_overlying_set(bset
,
5786 bset_to_bmap(like
)));
5789 __isl_give isl_set
*isl_map_underlying_set(__isl_take isl_map
*map
)
5793 map
= isl_map_cow(map
);
5796 map
->dim
= isl_space_cow(map
->dim
);
5800 for (i
= 1; i
< map
->n
; ++i
)
5801 isl_assert(map
->ctx
, map
->p
[0]->n_div
== map
->p
[i
]->n_div
,
5803 for (i
= 0; i
< map
->n
; ++i
) {
5804 map
->p
[i
] = bset_to_bmap(
5805 isl_basic_map_underlying_set(map
->p
[i
]));
5810 map
->dim
= isl_space_underlying(map
->dim
, 0);
5812 isl_space_free(map
->dim
);
5813 map
->dim
= isl_space_copy(map
->p
[0]->dim
);
5817 return set_from_map(map
);
5823 /* Replace the space of "bmap" by "space".
5825 * If the space of "bmap" is identical to "space" (including the identifiers
5826 * of the input and output dimensions), then simply return the original input.
5828 __isl_give isl_basic_map
*isl_basic_map_reset_space(
5829 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*space
)
5832 isl_space
*bmap_space
;
5834 bmap_space
= isl_basic_map_peek_space(bmap
);
5835 equal
= isl_space_is_equal(bmap_space
, space
);
5836 if (equal
>= 0 && equal
)
5837 equal
= isl_space_has_equal_ids(bmap_space
, space
);
5841 isl_space_free(space
);
5844 isl_space_free(isl_basic_map_take_space(bmap
));
5845 bmap
= isl_basic_map_restore_space(bmap
, space
);
5847 bmap
= isl_basic_map_finalize(bmap
);
5851 isl_basic_map_free(bmap
);
5852 isl_space_free(space
);
5856 __isl_give isl_basic_set
*isl_basic_set_reset_space(
5857 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*space
)
5859 return bset_from_bmap(isl_basic_map_reset_space(bset_to_bmap(bset
),
5863 /* Check that the total dimensions of "map" and "space" are the same.
5865 static isl_stat
check_map_space_equal_total_dim(__isl_keep isl_map
*map
,
5866 __isl_keep isl_space
*space
)
5868 isl_size dim1
, dim2
;
5870 dim1
= isl_map_dim(map
, isl_dim_all
);
5871 dim2
= isl_space_dim(space
, isl_dim_all
);
5872 if (dim1
< 0 || dim2
< 0)
5873 return isl_stat_error
;
5876 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
5877 "total dimensions do not match", return isl_stat_error
);
5880 __isl_give isl_map
*isl_map_reset_space(__isl_take isl_map
*map
,
5881 __isl_take isl_space
*space
)
5885 map
= isl_map_cow(map
);
5889 for (i
= 0; i
< map
->n
; ++i
) {
5890 map
->p
[i
] = isl_basic_map_reset_space(map
->p
[i
],
5891 isl_space_copy(space
));
5895 isl_space_free(isl_map_take_space(map
));
5896 map
= isl_map_restore_space(map
, space
);
5901 isl_space_free(space
);
5905 /* Replace the space of "map" by "space", without modifying
5906 * the dimension of "map".
5908 * If the space of "map" is identical to "space" (including the identifiers
5909 * of the input and output dimensions), then simply return the original input.
5911 __isl_give isl_map
*isl_map_reset_equal_dim_space(__isl_take isl_map
*map
,
5912 __isl_take isl_space
*space
)
5915 isl_space
*map_space
;
5917 map_space
= isl_map_peek_space(map
);
5918 equal
= isl_space_is_equal(map_space
, space
);
5919 if (equal
>= 0 && equal
)
5920 equal
= isl_space_has_equal_ids(map_space
, space
);
5924 isl_space_free(space
);
5927 if (check_map_space_equal_total_dim(map
, space
) < 0)
5929 return isl_map_reset_space(map
, space
);
5932 isl_space_free(space
);
5936 __isl_give isl_set
*isl_set_reset_space(__isl_take isl_set
*set
,
5937 __isl_take isl_space
*space
)
5939 return set_from_map(isl_map_reset_space(set_to_map(set
), space
));
5942 /* Compute the parameter domain of the given basic set.
5944 __isl_give isl_basic_set
*isl_basic_set_params(__isl_take isl_basic_set
*bset
)
5950 is_params
= isl_basic_set_is_params(bset
);
5952 return isl_basic_set_free(bset
);
5956 n
= isl_basic_set_dim(bset
, isl_dim_set
);
5958 return isl_basic_set_free(bset
);
5959 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
5960 space
= isl_basic_set_get_space(bset
);
5961 space
= isl_space_params(space
);
5962 bset
= isl_basic_set_reset_space(bset
, space
);
5966 /* Construct a zero-dimensional basic set with the given parameter domain.
5968 __isl_give isl_basic_set
*isl_basic_set_from_params(
5969 __isl_take isl_basic_set
*bset
)
5972 space
= isl_basic_set_get_space(bset
);
5973 space
= isl_space_set_from_params(space
);
5974 bset
= isl_basic_set_reset_space(bset
, space
);
5978 /* Compute the parameter domain of the given set.
5980 __isl_give isl_set
*isl_set_params(__isl_take isl_set
*set
)
5982 return isl_map_params(set_to_map(set
));
5985 /* Construct a zero-dimensional set with the given parameter domain.
5987 __isl_give isl_set
*isl_set_from_params(__isl_take isl_set
*set
)
5990 space
= isl_set_get_space(set
);
5991 space
= isl_space_set_from_params(space
);
5992 set
= isl_set_reset_space(set
, space
);
5996 /* Compute the parameter domain of the given map.
5998 __isl_give isl_set
*isl_map_params(__isl_take isl_map
*map
)
6001 isl_size n_in
, n_out
;
6003 n_in
= isl_map_dim(map
, isl_dim_in
);
6004 n_out
= isl_map_dim(map
, isl_dim_out
);
6005 if (n_in
< 0 || n_out
< 0)
6006 return isl_map_free(map
);
6007 map
= isl_map_project_out(map
, isl_dim_in
, 0, n_in
);
6008 map
= isl_map_project_out(map
, isl_dim_out
, 0, n_out
);
6009 space
= isl_map_get_space(map
);
6010 space
= isl_space_params(space
);
6011 map
= isl_map_reset_space(map
, space
);
6015 __isl_give isl_basic_set
*isl_basic_map_domain(__isl_take isl_basic_map
*bmap
)
6020 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
6022 return isl_basic_map_free(bmap
);
6023 space
= isl_space_domain(isl_basic_map_get_space(bmap
));
6025 bmap
= isl_basic_map_project_out(bmap
, isl_dim_out
, 0, n_out
);
6027 return isl_basic_map_reset_space(bmap
, space
);
6030 isl_bool
isl_basic_map_may_be_set(__isl_keep isl_basic_map
*bmap
)
6033 return isl_bool_error
;
6034 return isl_space_may_be_set(bmap
->dim
);
6037 /* Is this basic map actually a set?
6038 * Users should never call this function. Outside of isl,
6039 * the type should indicate whether something is a set or a map.
6041 isl_bool
isl_basic_map_is_set(__isl_keep isl_basic_map
*bmap
)
6044 return isl_bool_error
;
6045 return isl_space_is_set(bmap
->dim
);
6048 __isl_give isl_basic_set
*isl_basic_map_range(__isl_take isl_basic_map
*bmap
)
6052 is_set
= isl_basic_map_is_set(bmap
);
6057 return isl_basic_map_domain(isl_basic_map_reverse(bmap
));
6059 isl_basic_map_free(bmap
);
6063 __isl_give isl_basic_map
*isl_basic_map_domain_map(
6064 __isl_take isl_basic_map
*bmap
)
6068 isl_basic_map
*domain
;
6069 isl_size nparam
, n_in
, n_out
;
6071 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
6072 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
6073 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
6074 if (nparam
< 0 || n_in
< 0 || n_out
< 0)
6075 return isl_basic_map_free(bmap
);
6077 space
= isl_basic_map_get_space(bmap
);
6078 space
= isl_space_from_range(isl_space_domain(space
));
6079 domain
= isl_basic_map_universe(space
);
6081 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
6082 bmap
= isl_basic_map_apply_range(bmap
, domain
);
6083 bmap
= isl_basic_map_extend_constraints(bmap
, n_in
, 0);
6085 for (i
= 0; i
< n_in
; ++i
)
6086 bmap
= isl_basic_map_equate(bmap
, isl_dim_in
, i
,
6089 bmap
= isl_basic_map_gauss(bmap
, NULL
);
6090 return isl_basic_map_finalize(bmap
);
6093 __isl_give isl_basic_map
*isl_basic_map_range_map(
6094 __isl_take isl_basic_map
*bmap
)
6098 isl_basic_map
*range
;
6099 isl_size nparam
, n_in
, n_out
;
6101 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
6102 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
6103 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
6104 if (nparam
< 0 || n_in
< 0 || n_out
< 0)
6105 return isl_basic_map_free(bmap
);
6107 space
= isl_basic_map_get_space(bmap
);
6108 space
= isl_space_from_range(isl_space_range(space
));
6109 range
= isl_basic_map_universe(space
);
6111 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
6112 bmap
= isl_basic_map_apply_range(bmap
, range
);
6113 bmap
= isl_basic_map_extend_constraints(bmap
, n_out
, 0);
6115 for (i
= 0; i
< n_out
; ++i
)
6116 bmap
= isl_basic_map_equate(bmap
, isl_dim_in
, n_in
+ i
,
6119 bmap
= isl_basic_map_gauss(bmap
, NULL
);
6120 return isl_basic_map_finalize(bmap
);
6123 int isl_map_may_be_set(__isl_keep isl_map
*map
)
6127 return isl_space_may_be_set(map
->dim
);
6130 /* Is this map actually a set?
6131 * Users should never call this function. Outside of isl,
6132 * the type should indicate whether something is a set or a map.
6134 isl_bool
isl_map_is_set(__isl_keep isl_map
*map
)
6137 return isl_bool_error
;
6138 return isl_space_is_set(map
->dim
);
6141 __isl_give isl_set
*isl_map_range(__isl_take isl_map
*map
)
6146 n_in
= isl_map_dim(map
, isl_dim_in
);
6148 return set_from_map(isl_map_free(map
));
6149 space
= isl_space_range(isl_map_get_space(map
));
6151 map
= isl_map_project_out(map
, isl_dim_in
, 0, n_in
);
6153 return set_from_map(isl_map_reset_space(map
, space
));
6156 /* Transform "map" by applying "fn_space" to its space and "fn_bmap"
6157 * to each of its basic maps.
6159 static __isl_give isl_map
*isl_map_transform(__isl_take isl_map
*map
,
6160 __isl_give isl_space
*(*fn_space
)(__isl_take isl_space
*space
),
6161 __isl_give isl_basic_map
*(*fn_bmap
)(__isl_take isl_basic_map
*bmap
))
6166 map
= isl_map_cow(map
);
6170 for (i
= 0; i
< map
->n
; ++i
) {
6171 map
->p
[i
] = fn_bmap(map
->p
[i
]);
6173 return isl_map_free(map
);
6175 map
= isl_map_unmark_normalized(map
);
6177 space
= isl_map_take_space(map
);
6178 space
= fn_space(space
);
6179 map
= isl_map_restore_space(map
, space
);
6184 __isl_give isl_map
*isl_map_domain_map(__isl_take isl_map
*map
)
6186 return isl_map_transform(map
, &isl_space_domain_map
,
6187 &isl_basic_map_domain_map
);
6190 __isl_give isl_map
*isl_map_range_map(__isl_take isl_map
*map
)
6192 return isl_map_transform(map
, &isl_space_range_map
,
6193 &isl_basic_map_range_map
);
6196 /* Given a wrapped map of the form A[B -> C],
6197 * return the map A[B -> C] -> B.
6199 __isl_give isl_map
*isl_set_wrapped_domain_map(__isl_take isl_set
*set
)
6206 if (!isl_set_has_tuple_id(set
))
6207 return isl_map_domain_map(isl_set_unwrap(set
));
6209 id
= isl_set_get_tuple_id(set
);
6210 map
= isl_map_domain_map(isl_set_unwrap(set
));
6211 map
= isl_map_set_tuple_id(map
, isl_dim_in
, id
);
6216 __isl_give isl_basic_map
*isl_basic_map_from_domain(
6217 __isl_take isl_basic_set
*bset
)
6219 return isl_basic_map_reverse(isl_basic_map_from_range(bset
));
6222 __isl_give isl_basic_map
*isl_basic_map_from_range(
6223 __isl_take isl_basic_set
*bset
)
6226 space
= isl_basic_set_get_space(bset
);
6227 space
= isl_space_from_range(space
);
6228 bset
= isl_basic_set_reset_space(bset
, space
);
6229 return bset_to_bmap(bset
);
6232 /* Create a relation with the given set as range.
6233 * The domain of the created relation is a zero-dimensional
6234 * flat anonymous space.
6236 __isl_give isl_map
*isl_map_from_range(__isl_take isl_set
*set
)
6239 space
= isl_set_get_space(set
);
6240 space
= isl_space_from_range(space
);
6241 set
= isl_set_reset_space(set
, space
);
6242 return set_to_map(set
);
6245 /* Create a relation with the given set as domain.
6246 * The range of the created relation is a zero-dimensional
6247 * flat anonymous space.
6249 __isl_give isl_map
*isl_map_from_domain(__isl_take isl_set
*set
)
6251 return isl_map_reverse(isl_map_from_range(set
));
6254 __isl_give isl_basic_map
*isl_basic_map_from_domain_and_range(
6255 __isl_take isl_basic_set
*domain
, __isl_take isl_basic_set
*range
)
6257 return isl_basic_map_apply_range(isl_basic_map_reverse(domain
), range
);
6260 __isl_give isl_map
*isl_map_from_domain_and_range(__isl_take isl_set
*domain
,
6261 __isl_take isl_set
*range
)
6263 return isl_map_apply_range(isl_map_reverse(domain
), range
);
6266 /* Return a newly allocated isl_map with given space and flags and
6267 * room for "n" basic maps.
6268 * Make sure that all cached information is cleared.
6270 __isl_give isl_map
*isl_map_alloc_space(__isl_take isl_space
*space
, int n
,
6273 struct isl_map
*map
;
6278 isl_die(space
->ctx
, isl_error_internal
,
6279 "negative number of basic maps", goto error
);
6280 map
= isl_calloc(space
->ctx
, struct isl_map
,
6281 sizeof(struct isl_map
) +
6282 (n
- 1) * sizeof(struct isl_basic_map
*));
6286 map
->ctx
= space
->ctx
;
6287 isl_ctx_ref(map
->ctx
);
6295 isl_space_free(space
);
6299 __isl_give isl_basic_map
*isl_basic_map_empty(__isl_take isl_space
*space
)
6301 struct isl_basic_map
*bmap
;
6302 bmap
= isl_basic_map_alloc_space(space
, 0, 1, 0);
6303 bmap
= isl_basic_map_set_to_empty(bmap
);
6307 __isl_give isl_basic_set
*isl_basic_set_empty(__isl_take isl_space
*space
)
6309 struct isl_basic_set
*bset
;
6310 bset
= isl_basic_set_alloc_space(space
, 0, 1, 0);
6311 bset
= isl_basic_set_set_to_empty(bset
);
6315 __isl_give isl_basic_map
*isl_basic_map_universe(__isl_take isl_space
*space
)
6317 struct isl_basic_map
*bmap
;
6318 bmap
= isl_basic_map_alloc_space(space
, 0, 0, 0);
6319 bmap
= isl_basic_map_finalize(bmap
);
6323 __isl_give isl_basic_set
*isl_basic_set_universe(__isl_take isl_space
*space
)
6325 struct isl_basic_set
*bset
;
6326 bset
= isl_basic_set_alloc_space(space
, 0, 0, 0);
6327 bset
= isl_basic_set_finalize(bset
);
6331 __isl_give isl_basic_map
*isl_basic_map_nat_universe(
6332 __isl_take isl_space
*space
)
6335 isl_size total
= isl_space_dim(space
, isl_dim_all
);
6336 isl_basic_map
*bmap
;
6339 space
= isl_space_free(space
);
6340 bmap
= isl_basic_map_alloc_space(space
, 0, 0, total
);
6341 for (i
= 0; i
< total
; ++i
) {
6342 int k
= isl_basic_map_alloc_inequality(bmap
);
6345 isl_seq_clr(bmap
->ineq
[k
], 1 + total
);
6346 isl_int_set_si(bmap
->ineq
[k
][1 + i
], 1);
6350 isl_basic_map_free(bmap
);
6354 __isl_give isl_basic_set
*isl_basic_set_nat_universe(
6355 __isl_take isl_space
*space
)
6357 return isl_basic_map_nat_universe(space
);
6360 __isl_give isl_map
*isl_map_nat_universe(__isl_take isl_space
*space
)
6362 return isl_map_from_basic_map(isl_basic_map_nat_universe(space
));
6365 __isl_give isl_set
*isl_set_nat_universe(__isl_take isl_space
*space
)
6367 return isl_map_nat_universe(space
);
6370 __isl_give isl_map
*isl_map_empty(__isl_take isl_space
*space
)
6372 return isl_map_alloc_space(space
, 0, ISL_MAP_DISJOINT
);
6375 __isl_give isl_set
*isl_set_empty(__isl_take isl_space
*space
)
6377 return isl_set_alloc_space(space
, 0, ISL_MAP_DISJOINT
);
6380 __isl_give isl_map
*isl_map_universe(__isl_take isl_space
*space
)
6382 struct isl_map
*map
;
6385 map
= isl_map_alloc_space(isl_space_copy(space
), 1, ISL_MAP_DISJOINT
);
6386 map
= isl_map_add_basic_map(map
, isl_basic_map_universe(space
));
6390 /* This function performs the same operation as isl_map_universe,
6391 * but is considered as a function on an isl_space when exported.
6393 __isl_give isl_map
*isl_space_universe_map(__isl_take isl_space
*space
)
6395 return isl_map_universe(space
);
6398 __isl_give isl_set
*isl_set_universe(__isl_take isl_space
*space
)
6400 struct isl_set
*set
;
6403 set
= isl_set_alloc_space(isl_space_copy(space
), 1, ISL_MAP_DISJOINT
);
6404 set
= isl_set_add_basic_set(set
, isl_basic_set_universe(space
));
6408 /* This function performs the same operation as isl_set_universe,
6409 * but is considered as a function on an isl_space when exported.
6411 __isl_give isl_set
*isl_space_universe_set(__isl_take isl_space
*space
)
6413 return isl_set_universe(space
);
6416 __isl_give isl_map
*isl_map_dup(__isl_keep isl_map
*map
)
6419 struct isl_map
*dup
;
6423 dup
= isl_map_alloc_space(isl_space_copy(map
->dim
), map
->n
, map
->flags
);
6424 for (i
= 0; i
< map
->n
; ++i
)
6425 dup
= isl_map_add_basic_map(dup
, isl_basic_map_copy(map
->p
[i
]));
6429 __isl_give isl_map
*isl_map_add_basic_map(__isl_take isl_map
*map
,
6430 __isl_take isl_basic_map
*bmap
)
6434 if (isl_basic_map_plain_is_empty(bmap
)) {
6435 isl_basic_map_free(bmap
);
6438 if (isl_map_basic_map_check_equal_space(map
, bmap
) < 0)
6440 isl_assert(map
->ctx
, map
->n
< map
->size
, goto error
);
6441 map
->p
[map
->n
] = bmap
;
6443 map
= isl_map_unmark_normalized(map
);
6449 isl_basic_map_free(bmap
);
6453 __isl_null isl_map
*isl_map_free(__isl_take isl_map
*map
)
6464 isl_ctx_deref(map
->ctx
);
6465 for (i
= 0; i
< map
->n
; ++i
)
6466 isl_basic_map_free(map
->p
[i
]);
6467 isl_space_free(map
->dim
);
6473 static __isl_give isl_basic_map
*isl_basic_map_fix_pos_si(
6474 __isl_take isl_basic_map
*bmap
, unsigned pos
, int value
)
6479 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
6481 return isl_basic_map_free(bmap
);
6483 bmap
= isl_basic_map_cow(bmap
);
6484 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
6485 j
= isl_basic_map_alloc_equality(bmap
);
6488 isl_seq_clr(bmap
->eq
[j
] + 1, total
);
6489 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
6490 isl_int_set_si(bmap
->eq
[j
][0], value
);
6491 bmap
= isl_basic_map_simplify(bmap
);
6492 return isl_basic_map_finalize(bmap
);
6494 isl_basic_map_free(bmap
);
6498 static __isl_give isl_basic_map
*isl_basic_map_fix_pos(
6499 __isl_take isl_basic_map
*bmap
, unsigned pos
, isl_int value
)
6504 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
6506 return isl_basic_map_free(bmap
);
6508 bmap
= isl_basic_map_cow(bmap
);
6509 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
6510 j
= isl_basic_map_alloc_equality(bmap
);
6513 isl_seq_clr(bmap
->eq
[j
] + 1, total
);
6514 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
6515 isl_int_set(bmap
->eq
[j
][0], value
);
6516 bmap
= isl_basic_map_simplify(bmap
);
6517 return isl_basic_map_finalize(bmap
);
6519 isl_basic_map_free(bmap
);
6523 __isl_give isl_basic_map
*isl_basic_map_fix_si(__isl_take isl_basic_map
*bmap
,
6524 enum isl_dim_type type
, unsigned pos
, int value
)
6526 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6527 return isl_basic_map_free(bmap
);
6528 return isl_basic_map_fix_pos_si(bmap
,
6529 isl_basic_map_offset(bmap
, type
) + pos
, value
);
6532 __isl_give isl_basic_map
*isl_basic_map_fix(__isl_take isl_basic_map
*bmap
,
6533 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6535 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6536 return isl_basic_map_free(bmap
);
6537 return isl_basic_map_fix_pos(bmap
,
6538 isl_basic_map_offset(bmap
, type
) + pos
, value
);
6541 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
6542 * to be equal to "v".
6544 __isl_give isl_basic_map
*isl_basic_map_fix_val(__isl_take isl_basic_map
*bmap
,
6545 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6549 if (!isl_val_is_int(v
))
6550 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
6551 "expecting integer value", goto error
);
6552 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6554 pos
+= isl_basic_map_offset(bmap
, type
);
6555 bmap
= isl_basic_map_fix_pos(bmap
, pos
, v
->n
);
6559 isl_basic_map_free(bmap
);
6564 /* Fix the value of the variable at position "pos" of type "type" of "bset"
6565 * to be equal to "v".
6567 __isl_give isl_basic_set
*isl_basic_set_fix_val(__isl_take isl_basic_set
*bset
,
6568 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6570 return isl_basic_map_fix_val(bset
, type
, pos
, v
);
6573 __isl_give isl_basic_set
*isl_basic_set_fix_si(__isl_take isl_basic_set
*bset
,
6574 enum isl_dim_type type
, unsigned pos
, int value
)
6576 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset
),
6580 __isl_give isl_basic_set
*isl_basic_set_fix(__isl_take isl_basic_set
*bset
,
6581 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6583 return bset_from_bmap(isl_basic_map_fix(bset_to_bmap(bset
),
6587 /* Remove the basic map at position "i" from "map" if this basic map
6588 * is (obviously) empty.
6590 static __isl_give isl_map
*remove_if_empty(__isl_take isl_map
*map
, int i
)
6597 empty
= isl_basic_map_plain_is_empty(map
->p
[i
]);
6599 return isl_map_free(map
);
6603 isl_basic_map_free(map
->p
[i
]);
6606 map
->p
[i
] = map
->p
[map
->n
];
6607 map
= isl_map_unmark_normalized(map
);
6614 /* Perform "fn" on each basic map of "map", where we may not be holding
6615 * the only reference to "map".
6616 * In particular, "fn" should be a semantics preserving operation
6617 * that we want to apply to all copies of "map". We therefore need
6618 * to be careful not to modify "map" in a way that breaks "map"
6619 * in case anything goes wrong.
6621 __isl_give isl_map
*isl_map_inline_foreach_basic_map(__isl_take isl_map
*map
,
6622 __isl_give isl_basic_map
*(*fn
)(__isl_take isl_basic_map
*bmap
))
6624 struct isl_basic_map
*bmap
;
6630 for (i
= map
->n
- 1; i
>= 0; --i
) {
6631 bmap
= isl_basic_map_copy(map
->p
[i
]);
6635 isl_basic_map_free(map
->p
[i
]);
6637 map
= remove_if_empty(map
, i
);
6648 __isl_give isl_map
*isl_map_fix_si(__isl_take isl_map
*map
,
6649 enum isl_dim_type type
, unsigned pos
, int value
)
6653 map
= isl_map_cow(map
);
6654 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
6655 return isl_map_free(map
);
6656 for (i
= map
->n
- 1; i
>= 0; --i
) {
6657 map
->p
[i
] = isl_basic_map_fix_si(map
->p
[i
], type
, pos
, value
);
6658 map
= remove_if_empty(map
, i
);
6662 map
= isl_map_unmark_normalized(map
);
6666 __isl_give isl_set
*isl_set_fix_si(__isl_take isl_set
*set
,
6667 enum isl_dim_type type
, unsigned pos
, int value
)
6669 return set_from_map(isl_map_fix_si(set_to_map(set
), type
, pos
, value
));
6672 __isl_give isl_map
*isl_map_fix(__isl_take isl_map
*map
,
6673 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6677 map
= isl_map_cow(map
);
6678 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
6679 return isl_map_free(map
);
6680 for (i
= 0; i
< map
->n
; ++i
) {
6681 map
->p
[i
] = isl_basic_map_fix(map
->p
[i
], type
, pos
, value
);
6685 map
= isl_map_unmark_normalized(map
);
6692 __isl_give isl_set
*isl_set_fix(__isl_take isl_set
*set
,
6693 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6695 return set_from_map(isl_map_fix(set_to_map(set
), type
, pos
, value
));
6698 /* Fix the value of the variable at position "pos" of type "type" of "map"
6699 * to be equal to "v".
6701 __isl_give isl_map
*isl_map_fix_val(__isl_take isl_map
*map
,
6702 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6706 map
= isl_map_cow(map
);
6710 if (!isl_val_is_int(v
))
6711 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
6712 "expecting integer value", goto error
);
6713 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
6715 for (i
= map
->n
- 1; i
>= 0; --i
) {
6716 map
->p
[i
] = isl_basic_map_fix_val(map
->p
[i
], type
, pos
,
6718 map
= remove_if_empty(map
, i
);
6722 map
= isl_map_unmark_normalized(map
);
6731 /* Fix the value of the variable at position "pos" of type "type" of "set"
6732 * to be equal to "v".
6734 __isl_give isl_set
*isl_set_fix_val(__isl_take isl_set
*set
,
6735 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6737 return isl_map_fix_val(set
, type
, pos
, v
);
6740 __isl_give isl_map
*isl_map_fix_input_si(__isl_take isl_map
*map
,
6741 unsigned input
, int value
)
6743 return isl_map_fix_si(map
, isl_dim_in
, input
, value
);
6746 __isl_give isl_set
*isl_set_fix_dim_si(__isl_take isl_set
*set
, unsigned dim
,
6749 return set_from_map(isl_map_fix_si(set_to_map(set
),
6750 isl_dim_set
, dim
, value
));
6753 static __isl_give isl_basic_map
*basic_map_bound_si(
6754 __isl_take isl_basic_map
*bmap
,
6755 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
6760 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6761 return isl_basic_map_free(bmap
);
6762 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
6764 return isl_basic_map_free(bmap
);
6765 pos
+= isl_basic_map_offset(bmap
, type
);
6766 bmap
= isl_basic_map_cow(bmap
);
6767 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
6768 j
= isl_basic_map_alloc_inequality(bmap
);
6771 isl_seq_clr(bmap
->ineq
[j
], 1 + total
);
6773 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
6774 isl_int_set_si(bmap
->ineq
[j
][0], value
);
6776 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
6777 isl_int_set_si(bmap
->ineq
[j
][0], -value
);
6779 bmap
= isl_basic_map_simplify(bmap
);
6780 return isl_basic_map_finalize(bmap
);
6782 isl_basic_map_free(bmap
);
6786 __isl_give isl_basic_map
*isl_basic_map_lower_bound_si(
6787 __isl_take isl_basic_map
*bmap
,
6788 enum isl_dim_type type
, unsigned pos
, int value
)
6790 return basic_map_bound_si(bmap
, type
, pos
, value
, 0);
6793 /* Constrain the values of the given dimension to be no greater than "value".
6795 __isl_give isl_basic_map
*isl_basic_map_upper_bound_si(
6796 __isl_take isl_basic_map
*bmap
,
6797 enum isl_dim_type type
, unsigned pos
, int value
)
6799 return basic_map_bound_si(bmap
, type
, pos
, value
, 1);
6802 static __isl_give isl_map
*map_bound_si(__isl_take isl_map
*map
,
6803 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
6807 map
= isl_map_cow(map
);
6808 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
6809 return isl_map_free(map
);
6810 for (i
= 0; i
< map
->n
; ++i
) {
6811 map
->p
[i
] = basic_map_bound_si(map
->p
[i
],
6812 type
, pos
, value
, upper
);
6816 map
= isl_map_unmark_normalized(map
);
6823 __isl_give isl_map
*isl_map_lower_bound_si(__isl_take isl_map
*map
,
6824 enum isl_dim_type type
, unsigned pos
, int value
)
6826 return map_bound_si(map
, type
, pos
, value
, 0);
6829 __isl_give isl_map
*isl_map_upper_bound_si(__isl_take isl_map
*map
,
6830 enum isl_dim_type type
, unsigned pos
, int value
)
6832 return map_bound_si(map
, type
, pos
, value
, 1);
6835 __isl_give isl_set
*isl_set_lower_bound_si(__isl_take isl_set
*set
,
6836 enum isl_dim_type type
, unsigned pos
, int value
)
6838 return set_from_map(isl_map_lower_bound_si(set_to_map(set
),
6842 __isl_give isl_set
*isl_set_upper_bound_si(__isl_take isl_set
*set
,
6843 enum isl_dim_type type
, unsigned pos
, int value
)
6845 return isl_map_upper_bound_si(set
, type
, pos
, value
);
6848 /* Bound the given variable of "bmap" from below (or above is "upper"
6849 * is set) to "value".
6851 static __isl_give isl_basic_map
*basic_map_bound(
6852 __isl_take isl_basic_map
*bmap
,
6853 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
6858 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6859 return isl_basic_map_free(bmap
);
6860 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
6862 return isl_basic_map_free(bmap
);
6863 pos
+= isl_basic_map_offset(bmap
, type
);
6864 bmap
= isl_basic_map_cow(bmap
);
6865 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
6866 j
= isl_basic_map_alloc_inequality(bmap
);
6869 isl_seq_clr(bmap
->ineq
[j
], 1 + total
);
6871 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
6872 isl_int_set(bmap
->ineq
[j
][0], value
);
6874 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
6875 isl_int_neg(bmap
->ineq
[j
][0], value
);
6877 bmap
= isl_basic_map_simplify(bmap
);
6878 return isl_basic_map_finalize(bmap
);
6880 isl_basic_map_free(bmap
);
6884 /* Bound the given variable of "map" from below (or above is "upper"
6885 * is set) to "value".
6887 static __isl_give isl_map
*map_bound(__isl_take isl_map
*map
,
6888 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
6892 map
= isl_map_cow(map
);
6893 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
6894 return isl_map_free(map
);
6895 for (i
= map
->n
- 1; i
>= 0; --i
) {
6896 map
->p
[i
] = basic_map_bound(map
->p
[i
], type
, pos
, value
, upper
);
6897 map
= remove_if_empty(map
, i
);
6901 map
= isl_map_unmark_normalized(map
);
6905 __isl_give isl_map
*isl_map_lower_bound(__isl_take isl_map
*map
,
6906 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6908 return map_bound(map
, type
, pos
, value
, 0);
6911 __isl_give isl_map
*isl_map_upper_bound(__isl_take isl_map
*map
,
6912 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6914 return map_bound(map
, type
, pos
, value
, 1);
6917 __isl_give isl_set
*isl_set_lower_bound(__isl_take isl_set
*set
,
6918 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6920 return isl_map_lower_bound(set
, type
, pos
, value
);
6923 __isl_give isl_set
*isl_set_upper_bound(__isl_take isl_set
*set
,
6924 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6926 return isl_map_upper_bound(set
, type
, pos
, value
);
6929 /* Force the values of the variable at position "pos" of type "type" of "map"
6930 * to be no smaller than "value".
6932 __isl_give isl_map
*isl_map_lower_bound_val(__isl_take isl_map
*map
,
6933 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
6937 if (!isl_val_is_int(value
))
6938 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
6939 "expecting integer value", goto error
);
6940 map
= isl_map_lower_bound(map
, type
, pos
, value
->n
);
6941 isl_val_free(value
);
6944 isl_val_free(value
);
6949 /* Force the values of the variable at position "pos" of type "type" of "set"
6950 * to be no smaller than "value".
6952 __isl_give isl_set
*isl_set_lower_bound_val(__isl_take isl_set
*set
,
6953 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
6957 map
= set_to_map(set
);
6958 return set_from_map(isl_map_lower_bound_val(map
, type
, pos
, value
));
6961 /* Force the values of the variable at position "pos" of type "type" of "map"
6962 * to be no greater than "value".
6964 __isl_give isl_map
*isl_map_upper_bound_val(__isl_take isl_map
*map
,
6965 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
6969 if (!isl_val_is_int(value
))
6970 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
6971 "expecting integer value", goto error
);
6972 map
= isl_map_upper_bound(map
, type
, pos
, value
->n
);
6973 isl_val_free(value
);
6976 isl_val_free(value
);
6981 /* Force the values of the variable at position "pos" of type "type" of "set"
6982 * to be no greater than "value".
6984 __isl_give isl_set
*isl_set_upper_bound_val(__isl_take isl_set
*set
,
6985 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
6989 map
= set_to_map(set
);
6990 return set_from_map(isl_map_upper_bound_val(map
, type
, pos
, value
));
6993 /* If "mv" has an explicit domain, then intersect the domain of "map"
6994 * with this explicit domain.
6996 * An isl_multi_val object never has an explicit domain,
6997 * so simply return "map".
6999 static __isl_give isl_map
*isl_map_intersect_multi_val_explicit_domain(
7000 __isl_take isl_map
*map
, __isl_keep isl_multi_val
*mv
)
7007 #include "isl_map_bound_templ.c"
7009 /* Apply "map_bound" to "set" with the corresponding value in "bound"
7010 * for each set dimension, by treating the set as a map.
7012 static __isl_give isl_set
*set_bound_multi_val(__isl_take isl_set
*set
,
7013 __isl_take isl_multi_val
*bound
,
7014 __isl_give isl_map
*map_bound(__isl_take isl_map
*map
,
7015 unsigned pos
, __isl_take isl_val
*value
))
7019 map
= set_to_map(set
);
7020 return set_from_map(map_bound_multi_val(map
, bound
, map_bound
));
7025 #include "isl_map_bound_templ.c"
7027 /* Apply "map_bound" to "set" with the corresponding value in "bound"
7028 * for each set dimension, by converting the set and the bound
7029 * to objects living in a map space.
7031 static __isl_give isl_set
*set_bound_multi_pw_aff(__isl_take isl_set
*set
,
7032 __isl_take isl_multi_pw_aff
*bound
,
7033 __isl_give isl_map
*set_bound(__isl_take isl_map
*map
,
7034 unsigned pos
, __isl_take TYPE
*value
))
7038 map
= isl_map_from_range(set
);
7039 bound
= isl_multi_pw_aff_from_range(bound
);
7040 map
= map_bound_multi_pw_aff(map
, bound
, set_bound
);
7041 return isl_map_range(map
);
7044 /* Wrapper around isl_map_lower_bound_val for use in map_bound_multi_val,
7045 * setting a bound on the given output dimension.
7047 static __isl_give isl_map
*map_lower_bound_val(__isl_take isl_map
*map
,
7048 unsigned pos
, __isl_take isl_val
*v
)
7050 return isl_map_lower_bound_val(map
, isl_dim_out
, pos
, v
);
7053 /* Force the values of the set dimensions of "set"
7054 * to be no smaller than the corresponding values in "lower".
7056 __isl_give isl_set
*isl_set_lower_bound_multi_val(__isl_take isl_set
*set
,
7057 __isl_take isl_multi_val
*lower
)
7059 return set_bound_multi_val(set
, lower
, &map_lower_bound_val
);
7062 /* Wrapper around isl_map_upper_bound_val for use in map_bound_multi_val,
7063 * setting a bound on the given output dimension.
7065 static __isl_give isl_map
*map_upper_bound_val(__isl_take isl_map
*map
,
7066 unsigned pos
, __isl_take isl_val
*v
)
7068 return isl_map_upper_bound_val(map
, isl_dim_out
, pos
, v
);
7071 /* Force the values of the set dimensions of "set"
7072 * to be no greater than the corresponding values in "upper".
7074 __isl_give isl_set
*isl_set_upper_bound_multi_val(__isl_take isl_set
*set
,
7075 __isl_take isl_multi_val
*upper
)
7077 return set_bound_multi_val(set
, upper
, &map_upper_bound_val
);
7080 /* Force the symbolic constant expression "bound"
7081 * to satisfy the relation "order" with respect to
7082 * the output variable at position "pos" of "map".
7084 * Create an affine expression representing the output variable
7085 * in terms of the range and
7086 * compare it using "order" to "bound" (defined on the domain).
7087 * The result is a relation between elements in domain and range that
7088 * can be intersected with "map".
7090 static __isl_give isl_map
*map_bound_pw_aff(__isl_take isl_map
*map
,
7091 unsigned pos
, __isl_take isl_pw_aff
*bound
,
7092 __isl_give isl_map
*(*order
)(__isl_take isl_pw_aff
*pa1
,
7093 __isl_take isl_pw_aff
*pa2
))
7096 isl_local_space
*ls
;
7099 space
= isl_space_range(isl_map_get_space(map
));
7100 ls
= isl_local_space_from_space(space
);
7101 var
= isl_pw_aff_var_on_domain(ls
, isl_dim_set
, pos
);
7102 map
= isl_map_intersect(map
, order(bound
, var
));
7106 /* Force the values of the output variable at position "pos" of "map"
7107 * to be no smaller than the symbolic constant expression "lower".
7109 static __isl_give isl_map
*map_lower_bound_pw_aff(__isl_take isl_map
*map
,
7110 unsigned pos
, __isl_take isl_pw_aff
*lower
)
7112 return map_bound_pw_aff(map
, pos
, lower
, &isl_pw_aff_le_map
);
7115 /* Force the values of the output variable at position "pos" of "map"
7116 * to be no greater than the symbolic constant expression "upper".
7118 static __isl_give isl_map
*map_upper_bound_pw_aff(__isl_take isl_map
*map
,
7119 unsigned pos
, __isl_take isl_pw_aff
*upper
)
7121 return map_bound_pw_aff(map
, pos
, upper
, &isl_pw_aff_ge_map
);
7124 /* Force the values of the set dimensions of "set"
7125 * to be no smaller than the corresponding constant symbolic expressions
7128 __isl_give isl_set
*isl_set_lower_bound_multi_pw_aff(__isl_take isl_set
*set
,
7129 __isl_take isl_multi_pw_aff
*lower
)
7131 return set_bound_multi_pw_aff(set
, lower
, &map_lower_bound_pw_aff
);
7134 /* Force the values of the set dimensions of "set"
7135 * to be no greater than the corresponding constant symbolic expressions
7138 __isl_give isl_set
*isl_set_upper_bound_multi_pw_aff(__isl_take isl_set
*set
,
7139 __isl_take isl_multi_pw_aff
*upper
)
7141 return set_bound_multi_pw_aff(set
, upper
, &map_upper_bound_pw_aff
);
7144 /* Force the values of the output dimensions of "map"
7145 * to be no smaller than the corresponding constant symbolic expressions
7148 __isl_give isl_map
*isl_map_lower_bound_multi_pw_aff(__isl_take isl_map
*map
,
7149 __isl_take isl_multi_pw_aff
*lower
)
7151 return map_bound_multi_pw_aff(map
, lower
, &map_lower_bound_pw_aff
);
7154 /* Force the values of the output dimensions of "map"
7155 * to be no greater than the corresponding constant symbolic expressions
7158 __isl_give isl_map
*isl_map_upper_bound_multi_pw_aff(__isl_take isl_map
*map
,
7159 __isl_take isl_multi_pw_aff
*upper
)
7161 return map_bound_multi_pw_aff(map
, upper
, &map_upper_bound_pw_aff
);
7164 /* Bound the given variable of "bset" from below (or above is "upper"
7165 * is set) to "value".
7167 static __isl_give isl_basic_set
*isl_basic_set_bound(
7168 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned pos
,
7169 isl_int value
, int upper
)
7171 return bset_from_bmap(basic_map_bound(bset_to_bmap(bset
),
7172 type
, pos
, value
, upper
));
7175 /* Bound the given variable of "bset" from below (or above is "upper"
7176 * is set) to "value".
7178 static __isl_give isl_basic_set
*isl_basic_set_bound_val(
7179 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned pos
,
7180 __isl_take isl_val
*value
, int upper
)
7184 if (!isl_val_is_int(value
))
7185 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
7186 "expecting integer value", goto error
);
7187 bset
= isl_basic_set_bound(bset
, type
, pos
, value
->n
, upper
);
7188 isl_val_free(value
);
7191 isl_val_free(value
);
7192 isl_basic_set_free(bset
);
7196 /* Bound the given variable of "bset" from below to "value".
7198 __isl_give isl_basic_set
*isl_basic_set_lower_bound_val(
7199 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned pos
,
7200 __isl_take isl_val
*value
)
7202 return isl_basic_set_bound_val(bset
, type
, pos
, value
, 0);
7205 /* Bound the given variable of "bset" from above to "value".
7207 __isl_give isl_basic_set
*isl_basic_set_upper_bound_val(
7208 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned pos
,
7209 __isl_take isl_val
*value
)
7211 return isl_basic_set_bound_val(bset
, type
, pos
, value
, 1);
7214 __isl_give isl_map
*isl_map_reverse(__isl_take isl_map
*map
)
7216 return isl_map_transform(map
, &isl_space_reverse
,
7217 &isl_basic_map_reverse
);
7220 /* Given a map A -> (B -> C), return the corresponding map A -> (C -> B).
7222 __isl_give isl_map
*isl_map_range_reverse(__isl_take isl_map
*map
)
7224 return isl_map_transform(map
, &isl_space_range_reverse
,
7225 &isl_basic_map_range_reverse
);
7229 #define TYPE isl_pw_multi_aff
7231 #define SUFFIX _pw_multi_aff
7233 #define EMPTY isl_pw_multi_aff_empty
7235 #define ADD isl_pw_multi_aff_union_add
7236 #include "isl_map_lexopt_templ.c"
7238 /* Given a map "map", compute the lexicographically minimal
7239 * (or maximal) image element for each domain element in dom,
7240 * in the form of an isl_pw_multi_aff.
7241 * If "empty" is not NULL, then set *empty to those elements in dom that
7242 * do not have an image element.
7243 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
7244 * should be computed over the domain of "map". "empty" is also NULL
7247 * We first compute the lexicographically minimal or maximal element
7248 * in the first basic map. This results in a partial solution "res"
7249 * and a subset "todo" of dom that still need to be handled.
7250 * We then consider each of the remaining maps in "map" and successively
7251 * update both "res" and "todo".
7252 * If "empty" is NULL, then the todo sets are not needed and therefore
7253 * also not computed.
7255 static __isl_give isl_pw_multi_aff
*isl_map_partial_lexopt_aligned_pw_multi_aff(
7256 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
7257 __isl_give isl_set
**empty
, unsigned flags
)
7261 isl_pw_multi_aff
*res
;
7264 full
= ISL_FL_ISSET(flags
, ISL_OPT_FULL
);
7265 if (!map
|| (!full
&& !dom
))
7268 if (isl_map_plain_is_empty(map
)) {
7273 return isl_pw_multi_aff_from_map(map
);
7276 res
= basic_map_partial_lexopt_pw_multi_aff(
7277 isl_basic_map_copy(map
->p
[0]),
7278 isl_set_copy(dom
), empty
, flags
);
7282 for (i
= 1; i
< map
->n
; ++i
) {
7283 isl_pw_multi_aff
*res_i
;
7285 res_i
= basic_map_partial_lexopt_pw_multi_aff(
7286 isl_basic_map_copy(map
->p
[i
]),
7287 isl_set_copy(dom
), empty
, flags
);
7289 if (ISL_FL_ISSET(flags
, ISL_OPT_MAX
))
7290 res
= isl_pw_multi_aff_union_lexmax(res
, res_i
);
7292 res
= isl_pw_multi_aff_union_lexmin(res
, res_i
);
7295 todo
= isl_set_intersect(todo
, *empty
);
7314 #define TYPE isl_map
7318 #define EMPTY isl_map_empty
7320 #define ADD isl_map_union_disjoint
7321 #include "isl_map_lexopt_templ.c"
7323 /* Given a map "map", compute the lexicographically minimal
7324 * (or maximal) image element for each domain element in "dom",
7325 * in the form of an isl_map.
7326 * If "empty" is not NULL, then set *empty to those elements in "dom" that
7327 * do not have an image element.
7328 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
7329 * should be computed over the domain of "map". "empty" is also NULL
7332 * If the input consists of more than one disjunct, then first
7333 * compute the desired result in the form of an isl_pw_multi_aff and
7334 * then convert that into an isl_map.
7336 * This function used to have an explicit implementation in terms
7337 * of isl_maps, but it would continually intersect the domains of
7338 * partial results with the complement of the domain of the next
7339 * partial solution, potentially leading to an explosion in the number
7340 * of disjuncts if there are several disjuncts in the input.
7341 * An even earlier implementation of this function would look for
7342 * better results in the domain of the partial result and for extra
7343 * results in the complement of this domain, which would lead to
7344 * even more splintering.
7346 static __isl_give isl_map
*isl_map_partial_lexopt_aligned(
7347 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
7348 __isl_give isl_set
**empty
, unsigned flags
)
7351 struct isl_map
*res
;
7352 isl_pw_multi_aff
*pma
;
7354 full
= ISL_FL_ISSET(flags
, ISL_OPT_FULL
);
7355 if (!map
|| (!full
&& !dom
))
7358 if (isl_map_plain_is_empty(map
)) {
7367 res
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[0]),
7373 pma
= isl_map_partial_lexopt_aligned_pw_multi_aff(map
, dom
, empty
,
7375 return isl_map_from_pw_multi_aff_internal(pma
);
7384 __isl_give isl_map
*isl_map_partial_lexmax(
7385 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
7386 __isl_give isl_set
**empty
)
7388 return isl_map_partial_lexopt(map
, dom
, empty
, ISL_OPT_MAX
);
7391 __isl_give isl_map
*isl_map_partial_lexmin(
7392 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
7393 __isl_give isl_set
**empty
)
7395 return isl_map_partial_lexopt(map
, dom
, empty
, 0);
7398 __isl_give isl_set
*isl_set_partial_lexmin(
7399 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
7400 __isl_give isl_set
**empty
)
7402 return set_from_map(isl_map_partial_lexmin(set_to_map(set
),
7406 __isl_give isl_set
*isl_set_partial_lexmax(
7407 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
7408 __isl_give isl_set
**empty
)
7410 return set_from_map(isl_map_partial_lexmax(set_to_map(set
),
7414 /* Compute the lexicographic minimum (or maximum if "flags" includes
7415 * ISL_OPT_MAX) of "bset" over its parametric domain.
7417 __isl_give isl_set
*isl_basic_set_lexopt(__isl_take isl_basic_set
*bset
,
7420 return isl_basic_map_lexopt(bset
, flags
);
7423 __isl_give isl_map
*isl_basic_map_lexmax(__isl_take isl_basic_map
*bmap
)
7425 return isl_basic_map_lexopt(bmap
, ISL_OPT_MAX
);
7428 __isl_give isl_set
*isl_basic_set_lexmin(__isl_take isl_basic_set
*bset
)
7430 return set_from_map(isl_basic_map_lexmin(bset_to_bmap(bset
)));
7433 __isl_give isl_set
*isl_basic_set_lexmax(__isl_take isl_basic_set
*bset
)
7435 return set_from_map(isl_basic_map_lexmax(bset_to_bmap(bset
)));
7438 /* Compute the lexicographic minimum of "bset" over its parametric domain
7439 * for the purpose of quantifier elimination.
7440 * That is, find an explicit representation for all the existentially
7441 * quantified variables in "bset" by computing their lexicographic
7444 static __isl_give isl_set
*isl_basic_set_lexmin_compute_divs(
7445 __isl_take isl_basic_set
*bset
)
7447 return isl_basic_set_lexopt(bset
, ISL_OPT_QE
);
7450 /* Given a basic map with one output dimension, compute the minimum or
7451 * maximum of that dimension as an isl_pw_aff.
7453 * Compute the optimum as a lexicographic optimum over the single
7454 * output dimension and extract the single isl_pw_aff from the result.
7456 static __isl_give isl_pw_aff
*basic_map_dim_opt(__isl_keep isl_basic_map
*bmap
,
7459 isl_pw_multi_aff
*pma
;
7462 bmap
= isl_basic_map_copy(bmap
);
7463 pma
= isl_basic_map_lexopt_pw_multi_aff(bmap
, max
? ISL_OPT_MAX
: 0);
7464 pwaff
= isl_pw_multi_aff_get_pw_aff(pma
, 0);
7465 isl_pw_multi_aff_free(pma
);
7470 /* Compute the minimum or maximum of the given output dimension
7471 * as a function of the parameters and the input dimensions,
7472 * but independently of the other output dimensions.
7474 * We first project out the other output dimension and then compute
7475 * the "lexicographic" maximum in each basic map, combining the results
7476 * using isl_pw_aff_union_max.
7478 static __isl_give isl_pw_aff
*map_dim_opt(__isl_take isl_map
*map
, int pos
,
7485 n_out
= isl_map_dim(map
, isl_dim_out
);
7487 map
= isl_map_free(map
);
7488 map
= isl_map_project_out(map
, isl_dim_out
, pos
+ 1, n_out
- (pos
+ 1));
7489 map
= isl_map_project_out(map
, isl_dim_out
, 0, pos
);
7494 isl_space
*space
= isl_map_get_space(map
);
7496 return isl_pw_aff_empty(space
);
7499 pwaff
= basic_map_dim_opt(map
->p
[0], max
);
7500 for (i
= 1; i
< map
->n
; ++i
) {
7501 isl_pw_aff
*pwaff_i
;
7503 pwaff_i
= basic_map_dim_opt(map
->p
[i
], max
);
7504 pwaff
= isl_pw_aff_union_opt(pwaff
, pwaff_i
, max
);
7512 /* Compute the minimum of the given output dimension as a function of the
7513 * parameters and input dimensions, but independently of
7514 * the other output dimensions.
7516 __isl_give isl_pw_aff
*isl_map_dim_min(__isl_take isl_map
*map
, int pos
)
7518 return map_dim_opt(map
, pos
, 0);
7521 /* Compute the maximum of the given output dimension as a function of the
7522 * parameters and input dimensions, but independently of
7523 * the other output dimensions.
7525 __isl_give isl_pw_aff
*isl_map_dim_max(__isl_take isl_map
*map
, int pos
)
7527 return map_dim_opt(map
, pos
, 1);
7530 /* Compute the minimum or maximum of the given set dimension
7531 * as a function of the parameters,
7532 * but independently of the other set dimensions.
7534 static __isl_give isl_pw_aff
*set_dim_opt(__isl_take isl_set
*set
, int pos
,
7537 return map_dim_opt(set
, pos
, max
);
7540 /* Compute the maximum of the given set dimension as a function of the
7541 * parameters, but independently of the other set dimensions.
7543 __isl_give isl_pw_aff
*isl_set_dim_max(__isl_take isl_set
*set
, int pos
)
7545 return set_dim_opt(set
, pos
, 1);
7548 /* Compute the minimum of the given set dimension as a function of the
7549 * parameters, but independently of the other set dimensions.
7551 __isl_give isl_pw_aff
*isl_set_dim_min(__isl_take isl_set
*set
, int pos
)
7553 return set_dim_opt(set
, pos
, 0);
7556 /* Apply a preimage specified by "mat" on the parameters of "bset".
7557 * bset is assumed to have only parameters and divs.
7559 static __isl_give isl_basic_set
*basic_set_parameter_preimage(
7560 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*mat
)
7564 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
7565 if (nparam
< 0 || !mat
)
7568 bset
->dim
= isl_space_cow(bset
->dim
);
7572 isl_assert(bset
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
7574 bset
->dim
->nparam
= 0;
7575 bset
->dim
->n_out
= nparam
;
7576 bset
= isl_basic_set_preimage(bset
, mat
);
7578 bset
->dim
->nparam
= bset
->dim
->n_out
;
7579 bset
->dim
->n_out
= 0;
7584 isl_basic_set_free(bset
);
7588 /* Apply a preimage specified by "mat" on the parameters of "set".
7589 * set is assumed to have only parameters and divs.
7591 static __isl_give isl_set
*set_parameter_preimage(__isl_take isl_set
*set
,
7592 __isl_take isl_mat
*mat
)
7597 nparam
= isl_set_dim(set
, isl_dim_param
);
7598 if (nparam
< 0 || !mat
)
7601 if (mat
->n_row
!= 1 + nparam
)
7602 isl_die(isl_set_get_ctx(set
), isl_error_internal
,
7603 "unexpected number of rows", goto error
);
7605 space
= isl_set_get_space(set
);
7606 space
= isl_space_move_dims(space
, isl_dim_set
, 0,
7607 isl_dim_param
, 0, nparam
);
7608 set
= isl_set_reset_space(set
, space
);
7609 set
= isl_set_preimage(set
, mat
);
7610 nparam
= isl_set_dim(set
, isl_dim_out
);
7612 set
= isl_set_free(set
);
7613 space
= isl_set_get_space(set
);
7614 space
= isl_space_move_dims(space
, isl_dim_param
, 0,
7615 isl_dim_out
, 0, nparam
);
7616 set
= isl_set_reset_space(set
, space
);
7624 /* Intersect the basic set "bset" with the affine space specified by the
7625 * equalities in "eq".
7627 static __isl_give isl_basic_set
*basic_set_append_equalities(
7628 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*eq
)
7636 bset
= isl_basic_set_extend(bset
, 0, eq
->n_row
, 0);
7640 len
= isl_basic_set_offset(bset
, isl_dim_div
) + bset
->extra
;
7641 for (i
= 0; i
< eq
->n_row
; ++i
) {
7642 k
= isl_basic_set_alloc_equality(bset
);
7645 isl_seq_cpy(bset
->eq
[k
], eq
->row
[i
], eq
->n_col
);
7646 isl_seq_clr(bset
->eq
[k
] + eq
->n_col
, len
- eq
->n_col
);
7650 bset
= isl_basic_set_gauss(bset
, NULL
);
7651 bset
= isl_basic_set_finalize(bset
);
7656 isl_basic_set_free(bset
);
7660 /* Intersect the set "set" with the affine space specified by the
7661 * equalities in "eq".
7663 static __isl_give isl_set
*set_append_equalities(__isl_take isl_set
*set
,
7664 __isl_take isl_mat
*eq
)
7671 for (i
= 0; i
< set
->n
; ++i
) {
7672 set
->p
[i
] = basic_set_append_equalities(set
->p
[i
],
7685 /* Given a basic set "bset" that only involves parameters and existentially
7686 * quantified variables, return the index of the first equality
7687 * that only involves parameters. If there is no such equality then
7688 * return bset->n_eq.
7690 * This function assumes that isl_basic_set_gauss has been called on "bset".
7692 static int first_parameter_equality(__isl_keep isl_basic_set
*bset
)
7695 isl_size nparam
, n_div
;
7697 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
7698 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
7699 if (nparam
< 0 || n_div
< 0)
7702 for (i
= 0, j
= n_div
- 1; i
< bset
->n_eq
&& j
>= 0; --j
) {
7703 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ j
]))
7710 /* Compute an explicit representation for the existentially quantified
7711 * variables in "bset" by computing the "minimal value" of the set
7712 * variables. Since there are no set variables, the computation of
7713 * the minimal value essentially computes an explicit representation
7714 * of the non-empty part(s) of "bset".
7716 * The input only involves parameters and existentially quantified variables.
7717 * All equalities among parameters have been removed.
7719 * Since the existentially quantified variables in the result are in general
7720 * going to be different from those in the input, we first replace
7721 * them by the minimal number of variables based on their equalities.
7722 * This should simplify the parametric integer programming.
7724 static __isl_give isl_set
*base_compute_divs(__isl_take isl_basic_set
*bset
)
7726 isl_morph
*morph1
, *morph2
;
7732 if (bset
->n_eq
== 0)
7733 return isl_basic_set_lexmin_compute_divs(bset
);
7735 morph1
= isl_basic_set_parameter_compression(bset
);
7736 bset
= isl_morph_basic_set(isl_morph_copy(morph1
), bset
);
7737 bset
= isl_basic_set_lift(bset
);
7738 morph2
= isl_basic_set_variable_compression(bset
, isl_dim_set
);
7739 bset
= isl_morph_basic_set(morph2
, bset
);
7740 n
= isl_basic_set_dim(bset
, isl_dim_set
);
7742 bset
= isl_basic_set_free(bset
);
7743 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
7745 set
= isl_basic_set_lexmin_compute_divs(bset
);
7747 set
= isl_morph_set(isl_morph_inverse(morph1
), set
);
7752 /* Project the given basic set onto its parameter domain, possibly introducing
7753 * new, explicit, existential variables in the constraints.
7754 * The input has parameters and (possibly implicit) existential variables.
7755 * The output has the same parameters, but only
7756 * explicit existentially quantified variables.
7758 * The actual projection is performed by pip, but pip doesn't seem
7759 * to like equalities very much, so we first remove the equalities
7760 * among the parameters by performing a variable compression on
7761 * the parameters. Afterward, an inverse transformation is performed
7762 * and the equalities among the parameters are inserted back in.
7764 * The variable compression on the parameters may uncover additional
7765 * equalities that were only implicit before. We therefore check
7766 * if there are any new parameter equalities in the result and
7767 * if so recurse. The removal of parameter equalities is required
7768 * for the parameter compression performed by base_compute_divs.
7770 static __isl_give isl_set
*parameter_compute_divs(
7771 __isl_take isl_basic_set
*bset
)
7775 struct isl_mat
*T
, *T2
;
7776 struct isl_set
*set
;
7779 bset
= isl_basic_set_cow(bset
);
7783 if (bset
->n_eq
== 0)
7784 return base_compute_divs(bset
);
7786 bset
= isl_basic_set_gauss(bset
, NULL
);
7789 if (isl_basic_set_plain_is_empty(bset
))
7790 return isl_set_from_basic_set(bset
);
7792 i
= first_parameter_equality(bset
);
7793 if (i
== bset
->n_eq
)
7794 return base_compute_divs(bset
);
7796 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
7798 return isl_set_from_basic_set(isl_basic_set_free(bset
));
7799 eq
= isl_mat_sub_alloc6(bset
->ctx
, bset
->eq
, i
, bset
->n_eq
- i
,
7801 eq
= isl_mat_cow(eq
);
7802 T
= isl_mat_variable_compression(isl_mat_copy(eq
), &T2
);
7803 if (T
&& T
->n_col
== 0) {
7807 bset
= isl_basic_set_set_to_empty(bset
);
7808 return isl_set_from_basic_set(bset
);
7810 bset
= basic_set_parameter_preimage(bset
, T
);
7812 i
= first_parameter_equality(bset
);
7815 else if (i
== bset
->n_eq
)
7816 set
= base_compute_divs(bset
);
7818 set
= parameter_compute_divs(bset
);
7819 set
= set_parameter_preimage(set
, T2
);
7820 set
= set_append_equalities(set
, eq
);
7824 /* Insert the divs from "ls" before those of "bmap".
7826 * The number of columns is not changed, which means that the last
7827 * dimensions of "bmap" are being reintepreted as the divs from "ls".
7828 * The caller is responsible for removing the same number of dimensions
7829 * from the space of "bmap".
7831 static __isl_give isl_basic_map
*insert_divs_from_local_space(
7832 __isl_take isl_basic_map
*bmap
, __isl_keep isl_local_space
*ls
)
7838 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
7840 return isl_basic_map_free(bmap
);
7844 old_n_div
= bmap
->n_div
;
7845 bmap
= insert_div_rows(bmap
, n_div
);
7849 for (i
= 0; i
< n_div
; ++i
) {
7850 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], ls
->div
->n_col
);
7851 isl_seq_clr(bmap
->div
[i
] + ls
->div
->n_col
, old_n_div
);
7857 /* Replace the space of "bmap" by the space and divs of "ls".
7859 * If "ls" has any divs, then we simplify the result since we may
7860 * have discovered some additional equalities that could simplify
7861 * the div expressions.
7863 static __isl_give isl_basic_map
*basic_replace_space_by_local_space(
7864 __isl_take isl_basic_map
*bmap
, __isl_take isl_local_space
*ls
)
7868 bmap
= isl_basic_map_cow(bmap
);
7869 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
7870 if (!bmap
|| n_div
< 0)
7873 bmap
= insert_divs_from_local_space(bmap
, ls
);
7877 isl_space_free(bmap
->dim
);
7878 bmap
->dim
= isl_local_space_get_space(ls
);
7882 isl_local_space_free(ls
);
7884 bmap
= isl_basic_map_simplify(bmap
);
7885 bmap
= isl_basic_map_finalize(bmap
);
7888 isl_basic_map_free(bmap
);
7889 isl_local_space_free(ls
);
7893 /* Replace the space of "map" by the space and divs of "ls".
7895 static __isl_give isl_map
*replace_space_by_local_space(__isl_take isl_map
*map
,
7896 __isl_take isl_local_space
*ls
)
7900 map
= isl_map_cow(map
);
7904 for (i
= 0; i
< map
->n
; ++i
) {
7905 map
->p
[i
] = basic_replace_space_by_local_space(map
->p
[i
],
7906 isl_local_space_copy(ls
));
7910 isl_space_free(isl_map_take_space(map
));
7911 map
= isl_map_restore_space(map
, isl_local_space_get_space(ls
));
7913 isl_local_space_free(ls
);
7916 isl_local_space_free(ls
);
7921 /* Compute an explicit representation for the existentially
7922 * quantified variables for which do not know any explicit representation yet.
7924 * We first sort the existentially quantified variables so that the
7925 * existentially quantified variables for which we already have an explicit
7926 * representation are placed before those for which we do not.
7927 * The input dimensions, the output dimensions and the existentially
7928 * quantified variables for which we already have an explicit
7929 * representation are then turned into parameters.
7930 * compute_divs returns a map with the same parameters and
7931 * no input or output dimensions and the dimension specification
7932 * is reset to that of the input, including the existentially quantified
7933 * variables for which we already had an explicit representation.
7935 static __isl_give isl_map
*compute_divs(__isl_take isl_basic_map
*bmap
)
7937 struct isl_basic_set
*bset
;
7938 struct isl_set
*set
;
7939 struct isl_map
*map
;
7941 isl_local_space
*ls
;
7948 bmap
= isl_basic_map_sort_divs(bmap
);
7949 bmap
= isl_basic_map_cow(bmap
);
7953 n_known
= isl_basic_map_first_unknown_div(bmap
);
7954 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
7955 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
7956 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
7957 if (n_known
< 0 || nparam
< 0 || n_in
< 0 || n_out
< 0)
7958 return isl_map_from_basic_map(isl_basic_map_free(bmap
));
7960 space
= isl_space_set_alloc(bmap
->ctx
,
7961 nparam
+ n_in
+ n_out
+ n_known
, 0);
7965 ls
= isl_basic_map_get_local_space(bmap
);
7966 ls
= isl_local_space_drop_dims(ls
, isl_dim_div
,
7967 n_known
, bmap
->n_div
- n_known
);
7969 for (i
= n_known
; i
< bmap
->n_div
; ++i
)
7970 swap_div(bmap
, i
- n_known
, i
);
7971 bmap
->n_div
-= n_known
;
7972 bmap
->extra
-= n_known
;
7974 bmap
= isl_basic_map_reset_space(bmap
, space
);
7975 bset
= bset_from_bmap(bmap
);
7977 set
= parameter_compute_divs(bset
);
7978 map
= set_to_map(set
);
7979 map
= replace_space_by_local_space(map
, ls
);
7983 isl_basic_map_free(bmap
);
7987 /* Remove the explicit representation of local variable "div",
7990 __isl_give isl_basic_map
*isl_basic_map_mark_div_unknown(
7991 __isl_take isl_basic_map
*bmap
, int div
)
7995 unknown
= isl_basic_map_div_is_marked_unknown(bmap
, div
);
7997 return isl_basic_map_free(bmap
);
8001 bmap
= isl_basic_map_cow(bmap
);
8004 isl_int_set_si(bmap
->div
[div
][0], 0);
8008 /* Is local variable "div" of "bmap" marked as not having an explicit
8010 * Note that even if "div" is not marked in this way and therefore
8011 * has an explicit representation, this representation may still
8012 * depend (indirectly) on other local variables that do not
8013 * have an explicit representation.
8015 isl_bool
isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map
*bmap
,
8018 if (isl_basic_map_check_range(bmap
, isl_dim_div
, div
, 1) < 0)
8019 return isl_bool_error
;
8020 return isl_int_is_zero(bmap
->div
[div
][0]);
8023 /* Return the position of the first local variable that does not
8024 * have an explicit representation.
8025 * Return the total number of local variables if they all have
8026 * an explicit representation.
8027 * Return -1 on error.
8029 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map
*bmap
)
8036 for (i
= 0; i
< bmap
->n_div
; ++i
) {
8037 if (!isl_basic_map_div_is_known(bmap
, i
))
8043 /* Return the position of the first local variable that does not
8044 * have an explicit representation.
8045 * Return the total number of local variables if they all have
8046 * an explicit representation.
8047 * Return -1 on error.
8049 int isl_basic_set_first_unknown_div(__isl_keep isl_basic_set
*bset
)
8051 return isl_basic_map_first_unknown_div(bset
);
8054 /* Does "bmap" have an explicit representation for all local variables?
8056 isl_bool
isl_basic_map_divs_known(__isl_keep isl_basic_map
*bmap
)
8061 n
= isl_basic_map_dim(bmap
, isl_dim_div
);
8062 first
= isl_basic_map_first_unknown_div(bmap
);
8063 if (n
< 0 || first
< 0)
8064 return isl_bool_error
;
8068 /* Do all basic maps in "map" have an explicit representation
8069 * for all local variables?
8071 isl_bool
isl_map_divs_known(__isl_keep isl_map
*map
)
8076 return isl_bool_error
;
8078 for (i
= 0; i
< map
->n
; ++i
) {
8079 int known
= isl_basic_map_divs_known(map
->p
[i
]);
8084 return isl_bool_true
;
8087 /* If bmap contains any unknown divs, then compute explicit
8088 * expressions for them. However, this computation may be
8089 * quite expensive, so first try to remove divs that aren't
8092 __isl_give isl_map
*isl_basic_map_compute_divs(__isl_take isl_basic_map
*bmap
)
8095 struct isl_map
*map
;
8097 known
= isl_basic_map_divs_known(bmap
);
8101 return isl_map_from_basic_map(bmap
);
8103 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
8105 known
= isl_basic_map_divs_known(bmap
);
8109 return isl_map_from_basic_map(bmap
);
8111 map
= compute_divs(bmap
);
8114 isl_basic_map_free(bmap
);
8118 __isl_give isl_map
*isl_map_compute_divs(__isl_take isl_map
*map
)
8122 struct isl_map
*res
;
8129 known
= isl_map_divs_known(map
);
8137 res
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[0]));
8138 for (i
= 1 ; i
< map
->n
; ++i
) {
8140 r2
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[i
]));
8141 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
8142 res
= isl_map_union_disjoint(res
, r2
);
8144 res
= isl_map_union(res
, r2
);
8151 __isl_give isl_set
*isl_basic_set_compute_divs(__isl_take isl_basic_set
*bset
)
8153 return set_from_map(isl_basic_map_compute_divs(bset_to_bmap(bset
)));
8156 __isl_give isl_set
*isl_set_compute_divs(__isl_take isl_set
*set
)
8158 return set_from_map(isl_map_compute_divs(set_to_map(set
)));
8161 __isl_give isl_set
*isl_map_domain(__isl_take isl_map
*map
)
8166 n_out
= isl_map_dim(map
, isl_dim_out
);
8168 return set_from_map(isl_map_free(map
));
8169 space
= isl_space_domain(isl_map_get_space(map
));
8171 map
= isl_map_project_out(map
, isl_dim_out
, 0, n_out
);
8173 return set_from_map(isl_map_reset_space(map
, space
));
8176 /* Return the union of "map1" and "map2", where we assume for now that
8177 * "map1" and "map2" are disjoint. Note that the basic maps inside
8178 * "map1" or "map2" may not be disjoint from each other.
8179 * Also note that this function is also called from isl_map_union,
8180 * which takes care of handling the situation where "map1" and "map2"
8181 * may not be disjoint.
8183 * If one of the inputs is empty, we can simply return the other input.
8184 * Similarly, if one of the inputs is universal, then it is equal to the union.
8186 static __isl_give isl_map
*map_union_disjoint(__isl_take isl_map
*map1
,
8187 __isl_take isl_map
*map2
)
8191 struct isl_map
*map
= NULL
;
8194 if (isl_map_check_equal_space(map1
, map2
) < 0)
8206 is_universe
= isl_map_plain_is_universe(map1
);
8207 if (is_universe
< 0)
8214 is_universe
= isl_map_plain_is_universe(map2
);
8215 if (is_universe
< 0)
8222 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
8223 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
8224 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
8226 map
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
8227 map1
->n
+ map2
->n
, flags
);
8230 for (i
= 0; i
< map1
->n
; ++i
) {
8231 map
= isl_map_add_basic_map(map
,
8232 isl_basic_map_copy(map1
->p
[i
]));
8236 for (i
= 0; i
< map2
->n
; ++i
) {
8237 map
= isl_map_add_basic_map(map
,
8238 isl_basic_map_copy(map2
->p
[i
]));
8252 /* Return the union of "map1" and "map2", where "map1" and "map2" are
8253 * guaranteed to be disjoint by the caller.
8255 * Note that this functions is called from within isl_map_make_disjoint,
8256 * so we have to be careful not to touch the constraints of the inputs
8259 __isl_give isl_map
*isl_map_union_disjoint(__isl_take isl_map
*map1
,
8260 __isl_take isl_map
*map2
)
8262 isl_map_align_params_bin(&map1
, &map2
);
8263 return map_union_disjoint(map1
, map2
);
8266 /* Return the union of "map1" and "map2", where "map1" and "map2" may
8269 * We currently simply call map_union_disjoint, the internal operation
8270 * of which does not really depend on the inputs being disjoint.
8271 * If the result contains more than one basic map, then we clear
8272 * the disjoint flag since the result may contain basic maps from
8273 * both inputs and these are not guaranteed to be disjoint.
8275 * As a special case, if "map1" and "map2" are obviously equal,
8276 * then we simply return "map1".
8278 __isl_give isl_map
*isl_map_union(__isl_take isl_map
*map1
,
8279 __isl_take isl_map
*map2
)
8283 if (isl_map_align_params_bin(&map1
, &map2
) < 0)
8286 equal
= isl_map_plain_is_equal(map1
, map2
);
8294 map1
= map_union_disjoint(map1
, map2
);
8298 ISL_F_CLR(map1
, ISL_MAP_DISJOINT
);
8306 __isl_give isl_set
*isl_set_union_disjoint(
8307 __isl_take isl_set
*set1
, __isl_take isl_set
*set2
)
8309 return set_from_map(isl_map_union_disjoint(set_to_map(set1
),
8313 __isl_give isl_set
*isl_set_union(__isl_take isl_set
*set1
,
8314 __isl_take isl_set
*set2
)
8316 return set_from_map(isl_map_union(set_to_map(set1
), set_to_map(set2
)));
8319 /* Apply "fn" to pairs of elements from "map" and "set" and collect
8320 * the results in a map living in "space".
8322 * "map" and "set" are assumed to be compatible and non-NULL.
8324 static __isl_give isl_map
*map_intersect_set(__isl_take isl_map
*map
,
8325 __isl_take isl_space
*space
, __isl_take isl_set
*set
,
8326 __isl_give isl_basic_map
*fn(__isl_take isl_basic_map
*bmap
,
8327 __isl_take isl_basic_set
*bset
))
8330 struct isl_map
*result
;
8333 if (isl_set_plain_is_universe(set
)) {
8335 return isl_map_reset_equal_dim_space(map
, space
);
8338 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
) &&
8339 ISL_F_ISSET(set
, ISL_MAP_DISJOINT
))
8340 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
8342 result
= isl_map_alloc_space(space
, map
->n
* set
->n
, flags
);
8343 for (i
= 0; result
&& i
< map
->n
; ++i
)
8344 for (j
= 0; j
< set
->n
; ++j
) {
8345 result
= isl_map_add_basic_map(result
,
8346 fn(isl_basic_map_copy(map
->p
[i
]),
8347 isl_basic_set_copy(set
->p
[j
])));
8357 __isl_give isl_map
*isl_map_intersect_range(__isl_take isl_map
*map
,
8358 __isl_take isl_set
*set
)
8363 isl_map_align_params_set(&map
, &set
);
8364 ok
= isl_map_compatible_range(map
, set
);
8368 isl_die(set
->ctx
, isl_error_invalid
,
8369 "incompatible spaces", goto error
);
8371 space
= isl_map_get_space(map
);
8372 return map_intersect_set(map
, space
, set
,
8373 &isl_basic_map_intersect_range
);
8380 /* Intersect the domain of "map" with "set".
8382 * If the domain dimensions of "map" do not have any identifiers,
8383 * then copy them over from "set".
8385 __isl_give isl_map
*isl_map_intersect_domain(__isl_take isl_map
*map
,
8386 __isl_take isl_set
*set
)
8391 isl_map_align_params_set(&map
, &set
);
8392 ok
= isl_map_compatible_domain(map
, set
);
8396 isl_die(set
->ctx
, isl_error_invalid
,
8397 "incompatible spaces", goto error
);
8399 space
= isl_map_get_space(map
);
8400 space
= isl_space_copy_ids_if_unset(space
, isl_dim_in
,
8401 isl_set_peek_space(set
), isl_dim_set
);
8402 return map_intersect_set(map
, space
, set
,
8403 &isl_basic_map_intersect_domain
);
8411 #define TYPE isl_map
8413 #include "isl_copy_tuple_id_templ.c"
8415 /* Data structure that specifies how isl_map_intersect_factor
8418 * "preserve_type" is the tuple where the factor differs from
8419 * the input map and of which the identifiers needs
8420 * to be preserved explicitly.
8421 * "other_factor" is used to extract the space of the other factor
8422 * from the space of the product ("map").
8423 * "product" is used to combine the given factor and a universe map
8424 * in the space returned by "other_factor" to produce a map
8425 * that lives in the same space as the input map.
8427 struct isl_intersect_factor_control
{
8428 enum isl_dim_type preserve_type
;
8429 __isl_give isl_space
*(*other_factor
)(__isl_take isl_space
*space
);
8430 __isl_give isl_map
*(*product
)(__isl_take isl_map
*factor
,
8431 __isl_take isl_map
*other
);
8434 /* Given a map "map" in some product space and a map "factor"
8435 * living in some factor space, return the intersection.
8437 * After aligning the parameters,
8438 * the map "factor" is first extended to a map living in the same space
8439 * as "map" and then a regular intersection is computed.
8441 * Note that the extension is computed as a product, which is anonymous
8442 * by default. If "map" has an identifier on the corresponding tuple,
8443 * then this identifier needs to be set on the product
8444 * before the intersection is computed.
8446 static __isl_give isl_map
*isl_map_intersect_factor(
8447 __isl_take isl_map
*map
, __isl_take isl_map
*factor
,
8448 struct isl_intersect_factor_control
*control
)
8452 isl_map
*other
, *product
;
8454 equal
= isl_map_has_equal_params(map
, factor
);
8458 map
= isl_map_align_params(map
, isl_map_get_space(factor
));
8459 factor
= isl_map_align_params(factor
, isl_map_get_space(map
));
8462 space
= isl_map_get_space(map
);
8463 other
= isl_map_universe(control
->other_factor(space
));
8464 product
= control
->product(factor
, other
);
8466 space
= isl_map_peek_space(map
);
8467 product
= isl_map_copy_tuple_id(product
, control
->preserve_type
,
8468 space
, control
->preserve_type
);
8469 return map_intersect(map
, product
);
8472 isl_map_free(factor
);
8476 /* Return the domain product of "map2" and "map1".
8478 static __isl_give isl_map
*isl_map_reverse_domain_product(
8479 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
8481 return isl_map_domain_product(map2
, map1
);
8484 /* Return the range product of "map2" and "map1".
8486 static __isl_give isl_map
*isl_map_reverse_range_product(
8487 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
8489 return isl_map_range_product(map2
, map1
);
8492 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
8493 * in the space A -> C, return the intersection.
8495 __isl_give isl_map
*isl_map_intersect_domain_factor_domain(
8496 __isl_take isl_map
*map
, __isl_take isl_map
*factor
)
8498 struct isl_intersect_factor_control control
= {
8499 .preserve_type
= isl_dim_in
,
8500 .other_factor
= isl_space_domain_factor_range
,
8501 .product
= isl_map_domain_product
,
8504 return isl_map_intersect_factor(map
, factor
, &control
);
8507 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
8508 * in the space B -> C, return the intersection.
8510 __isl_give isl_map
*isl_map_intersect_domain_factor_range(
8511 __isl_take isl_map
*map
, __isl_take isl_map
*factor
)
8513 struct isl_intersect_factor_control control
= {
8514 .preserve_type
= isl_dim_in
,
8515 .other_factor
= isl_space_domain_factor_domain
,
8516 .product
= isl_map_reverse_domain_product
,
8519 return isl_map_intersect_factor(map
, factor
, &control
);
8522 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
8523 * in the space A -> B, return the intersection.
8525 __isl_give isl_map
*isl_map_intersect_range_factor_domain(
8526 __isl_take isl_map
*map
, __isl_take isl_map
*factor
)
8528 struct isl_intersect_factor_control control
= {
8529 .preserve_type
= isl_dim_out
,
8530 .other_factor
= isl_space_range_factor_range
,
8531 .product
= isl_map_range_product
,
8534 return isl_map_intersect_factor(map
, factor
, &control
);
8537 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
8538 * in the space A -> C, return the intersection.
8540 __isl_give isl_map
*isl_map_intersect_range_factor_range(
8541 __isl_take isl_map
*map
, __isl_take isl_map
*factor
)
8543 struct isl_intersect_factor_control control
= {
8544 .preserve_type
= isl_dim_out
,
8545 .other_factor
= isl_space_range_factor_domain
,
8546 .product
= isl_map_reverse_range_product
,
8549 return isl_map_intersect_factor(map
, factor
, &control
);
8552 /* Given a set "set" in a space [A -> B] and a set "domain"
8553 * in the space A, return the intersection.
8555 * The set "domain" is first extended to a set living in the space
8556 * [A -> B] and then a regular intersection is computed.
8558 __isl_give isl_set
*isl_set_intersect_factor_domain(__isl_take isl_set
*set
,
8559 __isl_take isl_set
*domain
)
8561 struct isl_intersect_factor_control control
= {
8562 .preserve_type
= isl_dim_set
,
8563 .other_factor
= isl_space_factor_range
,
8564 .product
= isl_map_range_product
,
8567 return set_from_map(isl_map_intersect_factor(set_to_map(set
),
8568 set_to_map(domain
), &control
));
8571 /* Given a set "set" in a space [A -> B] and a set "range"
8572 * in the space B, return the intersection.
8574 * The set "range" is first extended to a set living in the space
8575 * [A -> B] and then a regular intersection is computed.
8577 __isl_give isl_set
*isl_set_intersect_factor_range(__isl_take isl_set
*set
,
8578 __isl_take isl_set
*range
)
8580 struct isl_intersect_factor_control control
= {
8581 .preserve_type
= isl_dim_set
,
8582 .other_factor
= isl_space_factor_domain
,
8583 .product
= isl_map_reverse_range_product
,
8586 return set_from_map(isl_map_intersect_factor(set_to_map(set
),
8587 set_to_map(range
), &control
));
8590 __isl_give isl_map
*isl_map_apply_domain(__isl_take isl_map
*map1
,
8591 __isl_take isl_map
*map2
)
8593 if (isl_map_align_params_bin(&map1
, &map2
) < 0)
8595 map1
= isl_map_reverse(map1
);
8596 map1
= isl_map_apply_range(map1
, map2
);
8597 return isl_map_reverse(map1
);
8604 __isl_give isl_map
*isl_map_apply_range(__isl_take isl_map
*map1
,
8605 __isl_take isl_map
*map2
)
8608 struct isl_map
*result
;
8611 if (isl_map_align_params_bin(&map1
, &map2
) < 0)
8614 space
= isl_space_join(isl_space_copy(map1
->dim
),
8615 isl_space_copy(map2
->dim
));
8617 result
= isl_map_alloc_space(space
, map1
->n
* map2
->n
, 0);
8620 for (i
= 0; i
< map1
->n
; ++i
)
8621 for (j
= 0; j
< map2
->n
; ++j
) {
8622 result
= isl_map_add_basic_map(result
,
8623 isl_basic_map_apply_range(
8624 isl_basic_map_copy(map1
->p
[i
]),
8625 isl_basic_map_copy(map2
->p
[j
])));
8631 if (result
&& result
->n
<= 1)
8632 ISL_F_SET(result
, ISL_MAP_DISJOINT
);
8640 /* Is "bmap" a transformation, i.e.,
8641 * does it relate elements from the same space.
8643 isl_bool
isl_basic_map_is_transformation(__isl_keep isl_basic_map
*bmap
)
8647 space
= isl_basic_map_peek_space(bmap
);
8648 return isl_space_tuple_is_equal(space
, isl_dim_in
, space
, isl_dim_out
);
8651 /* Check that "bmap" is a transformation, i.e.,
8652 * that it relates elements from the same space.
8654 static isl_stat
isl_basic_map_check_transformation(
8655 __isl_keep isl_basic_map
*bmap
)
8659 equal
= isl_basic_map_is_transformation(bmap
);
8661 return isl_stat_error
;
8663 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
8664 "domain and range don't match", return isl_stat_error
);
8669 * returns range - domain
8671 __isl_give isl_basic_set
*isl_basic_map_deltas(__isl_take isl_basic_map
*bmap
)
8673 isl_space
*target_space
;
8674 struct isl_basic_set
*bset
;
8680 if (isl_basic_map_check_transformation(bmap
) < 0)
8681 return isl_basic_map_free(bmap
);
8682 dim
= isl_basic_map_dim(bmap
, isl_dim_in
);
8683 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
8684 if (dim
< 0 || nparam
< 0)
8686 target_space
= isl_space_domain(isl_basic_map_get_space(bmap
));
8687 bmap
= isl_basic_map_from_range(isl_basic_map_wrap(bmap
));
8688 bmap
= isl_basic_map_add_dims(bmap
, isl_dim_in
, dim
);
8689 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
8691 bmap
= isl_basic_map_free(bmap
);
8692 bmap
= isl_basic_map_extend_constraints(bmap
, dim
, 0);
8693 for (i
= 0; i
< dim
; ++i
) {
8694 int j
= isl_basic_map_alloc_equality(bmap
);
8696 bmap
= isl_basic_map_free(bmap
);
8699 isl_seq_clr(bmap
->eq
[j
], 1 + total
);
8700 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
8701 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], 1);
8702 isl_int_set_si(bmap
->eq
[j
][1+nparam
+2*dim
+i
], -1);
8704 bset
= isl_basic_map_domain(bmap
);
8705 bset
= isl_basic_set_reset_space(bset
, target_space
);
8708 isl_basic_map_free(bmap
);
8712 /* Is the tuple of type "type1" of "map" the same as
8713 * the tuple of type "type2" of "space"?
8715 isl_bool
isl_map_space_tuple_is_equal(__isl_keep isl_map
*map
,
8716 enum isl_dim_type type1
, __isl_keep isl_space
*space
,
8717 enum isl_dim_type type2
)
8719 isl_space
*map_space
;
8721 map_space
= isl_map_peek_space(map
);
8722 return isl_space_tuple_is_equal(map_space
, type1
, space
, type2
);
8725 /* Is the tuple of type "type1" of "map1" the same as
8726 * the tuple of type "type2" of "map2"?
8728 isl_bool
isl_map_tuple_is_equal(__isl_keep isl_map
*map1
,
8729 enum isl_dim_type type1
, __isl_keep isl_map
*map2
,
8730 enum isl_dim_type type2
)
8732 isl_space
*space1
, *space2
;
8734 space1
= isl_map_peek_space(map1
);
8735 space2
= isl_map_peek_space(map2
);
8736 return isl_space_tuple_is_equal(space1
, type1
, space2
, type2
);
8739 /* Is the space of "obj" equal to "space", ignoring parameters?
8741 isl_bool
isl_map_has_space_tuples(__isl_keep isl_map
*map
,
8742 __isl_keep isl_space
*space
)
8744 isl_space
*map_space
;
8746 map_space
= isl_map_peek_space(map
);
8747 return isl_space_has_equal_tuples(map_space
, space
);
8750 /* Check that "map" is a transformation, i.e.,
8751 * that it relates elements from the same space.
8753 isl_stat
isl_map_check_transformation(__isl_keep isl_map
*map
)
8757 equal
= isl_map_tuple_is_equal(map
, isl_dim_in
, map
, isl_dim_out
);
8759 return isl_stat_error
;
8761 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
8762 "domain and range don't match", return isl_stat_error
);
8767 * returns range - domain
8769 __isl_give isl_set
*isl_map_deltas(__isl_take isl_map
*map
)
8773 struct isl_set
*result
;
8775 if (isl_map_check_transformation(map
) < 0)
8777 space
= isl_map_get_space(map
);
8778 space
= isl_space_domain(space
);
8779 result
= isl_set_alloc_space(space
, map
->n
, 0);
8782 for (i
= 0; i
< map
->n
; ++i
)
8783 result
= isl_set_add_basic_set(result
,
8784 isl_basic_map_deltas(isl_basic_map_copy(map
->p
[i
])));
8793 * returns [domain -> range] -> range - domain
8795 __isl_give isl_basic_map
*isl_basic_map_deltas_map(
8796 __isl_take isl_basic_map
*bmap
)
8800 isl_basic_map
*domain
;
8804 if (isl_basic_map_check_transformation(bmap
) < 0)
8805 return isl_basic_map_free(bmap
);
8807 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
8808 n
= isl_basic_map_dim(bmap
, isl_dim_in
);
8809 if (nparam
< 0 || n
< 0)
8810 return isl_basic_map_free(bmap
);
8812 space
= isl_basic_map_get_space(bmap
);
8813 space
= isl_space_from_range(isl_space_domain(space
));
8814 domain
= isl_basic_map_universe(space
);
8816 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
8817 bmap
= isl_basic_map_apply_range(bmap
, domain
);
8818 bmap
= isl_basic_map_extend_constraints(bmap
, n
, 0);
8820 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
8822 return isl_basic_map_free(bmap
);
8824 for (i
= 0; i
< n
; ++i
) {
8825 k
= isl_basic_map_alloc_equality(bmap
);
8828 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
8829 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], 1);
8830 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ i
], -1);
8831 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ n
+ i
], 1);
8834 bmap
= isl_basic_map_gauss(bmap
, NULL
);
8835 return isl_basic_map_finalize(bmap
);
8837 isl_basic_map_free(bmap
);
8842 * returns [domain -> range] -> range - domain
8844 __isl_give isl_map
*isl_map_deltas_map(__isl_take isl_map
*map
)
8846 if (isl_map_check_transformation(map
) < 0)
8847 return isl_map_free(map
);
8849 return isl_map_transform(map
, &isl_space_range_map
,
8850 &isl_basic_map_deltas_map
);
8853 /* Return pairs of elements { x -> y } such that y - x is in "deltas".
8855 __isl_give isl_map
*isl_set_translation(__isl_take isl_set
*deltas
)
8860 space
= isl_space_map_from_set(isl_set_get_space(deltas
));
8861 map
= isl_map_deltas_map(isl_map_universe(space
));
8862 map
= isl_map_intersect_range(map
, deltas
);
8864 return isl_set_unwrap(isl_map_domain(map
));
8867 __isl_give isl_basic_map
*isl_basic_map_identity(__isl_take isl_space
*space
)
8869 isl_size n_in
, n_out
;
8871 n_in
= isl_space_dim(space
, isl_dim_in
);
8872 n_out
= isl_space_dim(space
, isl_dim_out
);
8873 if (n_in
< 0 || n_out
< 0)
8876 isl_die(space
->ctx
, isl_error_invalid
,
8877 "number of input and output dimensions needs to be "
8878 "the same", goto error
);
8879 return isl_basic_map_equal(space
, n_in
);
8881 isl_space_free(space
);
8885 __isl_give isl_map
*isl_map_identity(__isl_take isl_space
*space
)
8887 return isl_map_from_basic_map(isl_basic_map_identity(space
));
8890 __isl_give isl_map
*isl_set_identity(__isl_take isl_set
*set
)
8892 isl_space
*space
= isl_set_get_space(set
);
8894 id
= isl_map_identity(isl_space_map_from_set(space
));
8895 return isl_map_intersect_range(id
, set
);
8898 /* Construct a basic set with all set dimensions having only non-negative
8901 __isl_give isl_basic_set
*isl_basic_set_positive_orthant(
8902 __isl_take isl_space
*space
)
8908 struct isl_basic_set
*bset
;
8910 nparam
= isl_space_dim(space
, isl_dim_param
);
8911 dim
= isl_space_dim(space
, isl_dim_set
);
8912 total
= isl_space_dim(space
, isl_dim_all
);
8913 if (nparam
< 0 || dim
< 0 || total
< 0)
8914 space
= isl_space_free(space
);
8915 bset
= isl_basic_set_alloc_space(space
, 0, 0, dim
);
8918 for (i
= 0; i
< dim
; ++i
) {
8919 int k
= isl_basic_set_alloc_inequality(bset
);
8922 isl_seq_clr(bset
->ineq
[k
], 1 + total
);
8923 isl_int_set_si(bset
->ineq
[k
][1 + nparam
+ i
], 1);
8927 isl_basic_set_free(bset
);
8931 /* Construct the half-space x_pos >= 0.
8933 static __isl_give isl_basic_set
*nonneg_halfspace(__isl_take isl_space
*space
,
8938 isl_basic_set
*nonneg
;
8940 total
= isl_space_dim(space
, isl_dim_all
);
8942 space
= isl_space_free(space
);
8943 nonneg
= isl_basic_set_alloc_space(space
, 0, 0, 1);
8944 k
= isl_basic_set_alloc_inequality(nonneg
);
8947 isl_seq_clr(nonneg
->ineq
[k
], 1 + total
);
8948 isl_int_set_si(nonneg
->ineq
[k
][pos
], 1);
8950 return isl_basic_set_finalize(nonneg
);
8952 isl_basic_set_free(nonneg
);
8956 /* Construct the half-space x_pos <= -1.
8958 static __isl_give isl_basic_set
*neg_halfspace(__isl_take isl_space
*space
,
8965 total
= isl_space_dim(space
, isl_dim_all
);
8967 space
= isl_space_free(space
);
8968 neg
= isl_basic_set_alloc_space(space
, 0, 0, 1);
8969 k
= isl_basic_set_alloc_inequality(neg
);
8972 isl_seq_clr(neg
->ineq
[k
], 1 + total
);
8973 isl_int_set_si(neg
->ineq
[k
][0], -1);
8974 isl_int_set_si(neg
->ineq
[k
][pos
], -1);
8976 return isl_basic_set_finalize(neg
);
8978 isl_basic_set_free(neg
);
8982 __isl_give isl_set
*isl_set_split_dims(__isl_take isl_set
*set
,
8983 enum isl_dim_type type
, unsigned first
, unsigned n
)
8987 isl_basic_set
*nonneg
;
8993 if (isl_set_check_range(set
, type
, first
, n
) < 0)
8994 return isl_set_free(set
);
8996 offset
= pos(set
->dim
, type
);
8997 for (i
= 0; i
< n
; ++i
) {
8998 nonneg
= nonneg_halfspace(isl_set_get_space(set
),
8999 offset
+ first
+ i
);
9000 neg
= neg_halfspace(isl_set_get_space(set
), offset
+ first
+ i
);
9002 set
= isl_set_intersect(set
, isl_basic_set_union(nonneg
, neg
));
9008 static isl_stat
foreach_orthant(__isl_take isl_set
*set
, int *signs
, int first
,
9010 isl_stat (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
9016 return isl_stat_error
;
9017 if (isl_set_plain_is_empty(set
)) {
9022 return fn(set
, signs
, user
);
9025 half
= isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set
),
9027 half
= isl_set_intersect(half
, isl_set_copy(set
));
9028 if (foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
) < 0)
9032 half
= isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set
),
9034 half
= isl_set_intersect(half
, set
);
9035 return foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
);
9038 return isl_stat_error
;
9041 /* Call "fn" on the intersections of "set" with each of the orthants
9042 * (except for obviously empty intersections). The orthant is identified
9043 * by the signs array, with each entry having value 1 or -1 according
9044 * to the sign of the corresponding variable.
9046 isl_stat
isl_set_foreach_orthant(__isl_keep isl_set
*set
,
9047 isl_stat (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
9056 return isl_stat_error
;
9057 if (isl_set_plain_is_empty(set
))
9060 nparam
= isl_set_dim(set
, isl_dim_param
);
9061 nvar
= isl_set_dim(set
, isl_dim_set
);
9062 if (nparam
< 0 || nvar
< 0)
9063 return isl_stat_error
;
9065 signs
= isl_alloc_array(set
->ctx
, int, nparam
+ nvar
);
9067 r
= foreach_orthant(isl_set_copy(set
), signs
, 0, nparam
+ nvar
,
9075 isl_bool
isl_set_is_equal(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
9077 return isl_map_is_equal(set_to_map(set1
), set_to_map(set2
));
9080 isl_bool
isl_basic_map_is_subset(__isl_keep isl_basic_map
*bmap1
,
9081 __isl_keep isl_basic_map
*bmap2
)
9084 struct isl_map
*map1
;
9085 struct isl_map
*map2
;
9087 if (!bmap1
|| !bmap2
)
9088 return isl_bool_error
;
9090 map1
= isl_map_from_basic_map(isl_basic_map_copy(bmap1
));
9091 map2
= isl_map_from_basic_map(isl_basic_map_copy(bmap2
));
9093 is_subset
= isl_map_is_subset(map1
, map2
);
9101 isl_bool
isl_basic_set_is_subset(__isl_keep isl_basic_set
*bset1
,
9102 __isl_keep isl_basic_set
*bset2
)
9104 return isl_basic_map_is_subset(bset1
, bset2
);
9107 isl_bool
isl_basic_map_is_equal(__isl_keep isl_basic_map
*bmap1
,
9108 __isl_keep isl_basic_map
*bmap2
)
9112 if (!bmap1
|| !bmap2
)
9113 return isl_bool_error
;
9114 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
9115 if (is_subset
!= isl_bool_true
)
9117 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
9121 isl_bool
isl_basic_set_is_equal(__isl_keep isl_basic_set
*bset1
,
9122 __isl_keep isl_basic_set
*bset2
)
9124 return isl_basic_map_is_equal(
9125 bset_to_bmap(bset1
), bset_to_bmap(bset2
));
9128 isl_bool
isl_map_is_empty(__isl_keep isl_map
*map
)
9134 return isl_bool_error
;
9135 for (i
= 0; i
< map
->n
; ++i
) {
9136 is_empty
= isl_basic_map_is_empty(map
->p
[i
]);
9138 return isl_bool_error
;
9140 return isl_bool_false
;
9142 return isl_bool_true
;
9145 isl_bool
isl_map_plain_is_empty(__isl_keep isl_map
*map
)
9147 return map
? map
->n
== 0 : isl_bool_error
;
9150 isl_bool
isl_set_plain_is_empty(__isl_keep isl_set
*set
)
9152 return set
? set
->n
== 0 : isl_bool_error
;
9155 isl_bool
isl_set_is_empty(__isl_keep isl_set
*set
)
9157 return isl_map_is_empty(set_to_map(set
));
9161 #define TYPE isl_basic_map
9164 #include "isl_type_has_equal_space_bin_templ.c"
9165 #include "isl_type_check_equal_space_templ.c"
9167 /* Check that "bset1" and "bset2" live in the same space,
9168 * reporting an error if they do not.
9170 isl_stat
isl_basic_set_check_equal_space(__isl_keep isl_basic_set
*bset1
,
9171 __isl_keep isl_basic_set
*bset2
)
9173 return isl_basic_map_check_equal_space(bset_to_bmap(bset1
),
9174 bset_to_bmap(bset1
));
9178 #define TYPE isl_map
9180 #include "isl_type_has_equal_space_bin_templ.c"
9181 #include "isl_type_check_equal_space_templ.c"
9182 #include "isl_type_has_space_templ.c"
9184 isl_bool
isl_set_has_equal_space(__isl_keep isl_set
*set1
,
9185 __isl_keep isl_set
*set2
)
9187 return isl_map_has_equal_space(set_to_map(set1
), set_to_map(set2
));
9191 #define TYPE1 isl_map
9193 #define TYPE2 isl_basic_map
9195 #define TYPE_PAIR isl_map_basic_map
9198 #include "isl_type_has_equal_space_templ.c"
9199 #include "isl_type_check_equal_space_templ.c"
9201 /* Check that "set" and "bset" live in the same space,
9202 * reporting an error if they do not.
9204 isl_stat
isl_set_basic_set_check_equal_space(__isl_keep isl_set
*set
,
9205 __isl_keep isl_basic_set
*bset
)
9207 return isl_map_basic_map_check_equal_space(set_to_map(set
),
9208 bset_to_bmap(bset
));
9211 static isl_bool
map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
9216 return isl_bool_error
;
9217 is_subset
= isl_map_is_subset(map1
, map2
);
9218 if (is_subset
!= isl_bool_true
)
9220 is_subset
= isl_map_is_subset(map2
, map1
);
9224 /* Is "map1" equal to "map2"?
9226 * First check if they are obviously equal.
9227 * If not, then perform a more detailed analysis.
9229 isl_bool
isl_map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
9233 equal
= isl_map_plain_is_equal(map1
, map2
);
9234 if (equal
< 0 || equal
)
9236 return isl_map_align_params_map_map_and_test(map1
, map2
, &map_is_equal
);
9239 isl_bool
isl_basic_map_is_strict_subset(__isl_keep isl_basic_map
*bmap1
,
9240 __isl_keep isl_basic_map
*bmap2
)
9244 if (!bmap1
|| !bmap2
)
9245 return isl_bool_error
;
9246 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
9247 if (is_subset
!= isl_bool_true
)
9249 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
9250 return isl_bool_not(is_subset
);
9253 isl_bool
isl_map_is_strict_subset(__isl_keep isl_map
*map1
,
9254 __isl_keep isl_map
*map2
)
9259 return isl_bool_error
;
9260 is_subset
= isl_map_is_subset(map1
, map2
);
9261 if (is_subset
!= isl_bool_true
)
9263 is_subset
= isl_map_is_subset(map2
, map1
);
9264 return isl_bool_not(is_subset
);
9267 isl_bool
isl_set_is_strict_subset(__isl_keep isl_set
*set1
,
9268 __isl_keep isl_set
*set2
)
9270 return isl_map_is_strict_subset(set_to_map(set1
), set_to_map(set2
));
9273 /* Is "bmap" obviously equal to the universe with the same space?
9275 * That is, does it not have any constraints?
9277 isl_bool
isl_basic_map_plain_is_universe(__isl_keep isl_basic_map
*bmap
)
9280 return isl_bool_error
;
9281 return bmap
->n_eq
== 0 && bmap
->n_ineq
== 0;
9284 /* Is "bset" obviously equal to the universe with the same space?
9286 isl_bool
isl_basic_set_plain_is_universe(__isl_keep isl_basic_set
*bset
)
9288 return isl_basic_map_plain_is_universe(bset
);
9291 /* If "c" does not involve any existentially quantified variables,
9292 * then set *univ to false and abort
9294 static isl_stat
involves_divs(__isl_take isl_constraint
*c
, void *user
)
9296 isl_bool
*univ
= user
;
9299 n
= isl_constraint_dim(c
, isl_dim_div
);
9301 c
= isl_constraint_free(c
);
9302 *univ
= isl_constraint_involves_dims(c
, isl_dim_div
, 0, n
);
9303 isl_constraint_free(c
);
9304 if (*univ
< 0 || !*univ
)
9305 return isl_stat_error
;
9309 /* Is "bmap" equal to the universe with the same space?
9311 * First check if it is obviously equal to the universe.
9312 * If not and if there are any constraints not involving
9313 * existentially quantified variables, then it is certainly
9314 * not equal to the universe.
9315 * Otherwise, check if the universe is a subset of "bmap".
9317 isl_bool
isl_basic_map_is_universe(__isl_keep isl_basic_map
*bmap
)
9321 isl_basic_map
*test
;
9323 univ
= isl_basic_map_plain_is_universe(bmap
);
9324 if (univ
< 0 || univ
)
9326 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
9328 return isl_bool_error
;
9330 return isl_bool_false
;
9331 univ
= isl_bool_true
;
9332 if (isl_basic_map_foreach_constraint(bmap
, &involves_divs
, &univ
) < 0 &&
9334 return isl_bool_error
;
9335 if (univ
< 0 || !univ
)
9337 test
= isl_basic_map_universe(isl_basic_map_get_space(bmap
));
9338 univ
= isl_basic_map_is_subset(test
, bmap
);
9339 isl_basic_map_free(test
);
9343 /* Is "bset" equal to the universe with the same space?
9345 isl_bool
isl_basic_set_is_universe(__isl_keep isl_basic_set
*bset
)
9347 return isl_basic_map_is_universe(bset
);
9350 isl_bool
isl_map_plain_is_universe(__isl_keep isl_map
*map
)
9355 return isl_bool_error
;
9357 for (i
= 0; i
< map
->n
; ++i
) {
9358 isl_bool r
= isl_basic_map_plain_is_universe(map
->p
[i
]);
9363 return isl_bool_false
;
9366 isl_bool
isl_set_plain_is_universe(__isl_keep isl_set
*set
)
9368 return isl_map_plain_is_universe(set_to_map(set
));
9371 isl_bool
isl_basic_map_is_empty(__isl_keep isl_basic_map
*bmap
)
9373 struct isl_basic_set
*bset
= NULL
;
9374 struct isl_vec
*sample
= NULL
;
9375 isl_bool empty
, non_empty
;
9378 return isl_bool_error
;
9380 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
9381 return isl_bool_true
;
9383 if (isl_basic_map_plain_is_universe(bmap
))
9384 return isl_bool_false
;
9386 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
9387 struct isl_basic_map
*copy
= isl_basic_map_copy(bmap
);
9388 copy
= isl_basic_map_remove_redundancies(copy
);
9389 empty
= isl_basic_map_plain_is_empty(copy
);
9390 isl_basic_map_free(copy
);
9394 non_empty
= isl_basic_map_plain_is_non_empty(bmap
);
9396 return isl_bool_error
;
9398 return isl_bool_false
;
9399 isl_vec_free(bmap
->sample
);
9400 bmap
->sample
= NULL
;
9401 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
9403 return isl_bool_error
;
9404 sample
= isl_basic_set_sample_vec(bset
);
9406 return isl_bool_error
;
9407 empty
= sample
->size
== 0;
9408 isl_vec_free(bmap
->sample
);
9409 bmap
->sample
= sample
;
9411 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
9416 isl_bool
isl_basic_map_plain_is_empty(__isl_keep isl_basic_map
*bmap
)
9419 return isl_bool_error
;
9420 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
);
9423 isl_bool
isl_basic_set_plain_is_empty(__isl_keep isl_basic_set
*bset
)
9426 return isl_bool_error
;
9427 return ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
);
9430 /* Is "bmap" known to be non-empty?
9432 * That is, is the cached sample still valid?
9434 isl_bool
isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map
*bmap
)
9439 return isl_bool_error
;
9441 return isl_bool_false
;
9442 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
9444 return isl_bool_error
;
9445 if (bmap
->sample
->size
!= 1 + total
)
9446 return isl_bool_false
;
9447 return isl_basic_map_contains(bmap
, bmap
->sample
);
9450 isl_bool
isl_basic_set_is_empty(__isl_keep isl_basic_set
*bset
)
9452 return isl_basic_map_is_empty(bset_to_bmap(bset
));
9455 __isl_give isl_map
*isl_basic_map_union(__isl_take isl_basic_map
*bmap1
,
9456 __isl_take isl_basic_map
*bmap2
)
9458 struct isl_map
*map
;
9460 if (isl_basic_map_check_equal_space(bmap1
, bmap2
) < 0)
9463 map
= isl_map_alloc_space(isl_space_copy(bmap1
->dim
), 2, 0);
9466 map
= isl_map_add_basic_map(map
, bmap1
);
9467 map
= isl_map_add_basic_map(map
, bmap2
);
9470 isl_basic_map_free(bmap1
);
9471 isl_basic_map_free(bmap2
);
9475 __isl_give isl_set
*isl_basic_set_union(__isl_take isl_basic_set
*bset1
,
9476 __isl_take isl_basic_set
*bset2
)
9478 return set_from_map(isl_basic_map_union(bset_to_bmap(bset1
),
9479 bset_to_bmap(bset2
)));
9482 /* Order divs such that any div only depends on previous divs */
9483 __isl_give isl_basic_map
*isl_basic_map_order_divs(
9484 __isl_take isl_basic_map
*bmap
)
9489 off
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
9491 return isl_basic_map_free(bmap
);
9493 for (i
= 0; i
< bmap
->n_div
; ++i
) {
9495 if (isl_int_is_zero(bmap
->div
[i
][0]))
9497 pos
= isl_seq_first_non_zero(bmap
->div
[i
]+1+1+off
+i
,
9502 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_internal
,
9503 "integer division depends on itself",
9504 return isl_basic_map_free(bmap
));
9505 bmap
= isl_basic_map_swap_div(bmap
, i
, i
+ pos
);
9513 __isl_give isl_map
*isl_map_order_divs(__isl_take isl_map
*map
)
9520 for (i
= 0; i
< map
->n
; ++i
) {
9521 map
->p
[i
] = isl_basic_map_order_divs(map
->p
[i
]);
9532 /* Sort the local variables of "bset".
9534 __isl_give isl_basic_set
*isl_basic_set_sort_divs(
9535 __isl_take isl_basic_set
*bset
)
9537 return bset_from_bmap(isl_basic_map_sort_divs(bset_to_bmap(bset
)));
9540 /* Apply the expansion computed by isl_merge_divs.
9541 * The expansion itself is given by "exp" while the resulting
9542 * list of divs is given by "div".
9544 * Move the integer divisions of "bmap" into the right position
9545 * according to "exp" and then introduce the additional integer
9546 * divisions, adding div constraints.
9547 * The moving should be done first to avoid moving coefficients
9548 * in the definitions of the extra integer divisions.
9550 __isl_give isl_basic_map
*isl_basic_map_expand_divs(
9551 __isl_take isl_basic_map
*bmap
, __isl_take isl_mat
*div
, int *exp
)
9556 bmap
= isl_basic_map_cow(bmap
);
9560 if (div
->n_row
< bmap
->n_div
)
9561 isl_die(isl_mat_get_ctx(div
), isl_error_invalid
,
9562 "not an expansion", goto error
);
9564 n_div
= bmap
->n_div
;
9565 bmap
= isl_basic_map_extend(bmap
, div
->n_row
- n_div
, 0,
9566 2 * (div
->n_row
- n_div
));
9568 for (i
= n_div
; i
< div
->n_row
; ++i
)
9569 if (isl_basic_map_alloc_div(bmap
) < 0)
9572 for (j
= n_div
- 1; j
>= 0; --j
) {
9575 bmap
= isl_basic_map_swap_div(bmap
, j
, exp
[j
]);
9580 for (i
= 0; i
< div
->n_row
; ++i
) {
9581 if (j
< n_div
&& exp
[j
] == i
) {
9584 isl_seq_cpy(bmap
->div
[i
], div
->row
[i
], div
->n_col
);
9585 if (isl_basic_map_div_is_marked_unknown(bmap
, i
))
9587 bmap
= isl_basic_map_add_div_constraints(bmap
, i
);
9596 isl_basic_map_free(bmap
);
9601 /* Apply the expansion computed by isl_merge_divs.
9602 * The expansion itself is given by "exp" while the resulting
9603 * list of divs is given by "div".
9605 __isl_give isl_basic_set
*isl_basic_set_expand_divs(
9606 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*div
, int *exp
)
9608 return isl_basic_map_expand_divs(bset
, div
, exp
);
9611 /* Look for a div in dst that corresponds to the div "div" in src.
9612 * The divs before "div" in src and dst are assumed to be the same.
9614 * Return the position of the corresponding div in dst
9615 * if there is one. Otherwise, return a position beyond the integer divisions.
9616 * Return -1 on error.
9618 static int find_div(__isl_keep isl_basic_map
*dst
,
9619 __isl_keep isl_basic_map
*src
, unsigned div
)
9625 v_div
= isl_basic_map_var_offset(src
, isl_dim_div
);
9626 n_div
= isl_basic_map_dim(dst
, isl_dim_div
);
9627 if (n_div
< 0 || v_div
< 0)
9629 isl_assert(dst
->ctx
, div
<= n_div
, return -1);
9630 for (i
= div
; i
< n_div
; ++i
)
9631 if (isl_seq_eq(dst
->div
[i
], src
->div
[div
], 1+1+v_div
+div
) &&
9632 isl_seq_first_non_zero(dst
->div
[i
] + 1 + 1 + v_div
+ div
,
9638 /* Align the divs of "dst" to those of "src", adding divs from "src"
9639 * if needed. That is, make sure that the first src->n_div divs
9640 * of the result are equal to those of src.
9641 * The integer division of "src" are assumed to be ordered.
9643 * The integer divisions are swapped into the right position
9644 * (possibly after adding them first). This may result
9645 * in the remaining integer divisions appearing in the wrong order,
9646 * i.e., with some integer division appearing before
9647 * some other integer division on which it depends.
9648 * The integer divisions therefore need to be ordered.
9649 * This will not affect the integer divisions aligned to those of "src",
9650 * since "src" is assumed to have ordered integer divisions.
9652 * The result is not finalized as by design it will have redundant
9653 * divs if any divs from "src" were copied.
9655 __isl_give isl_basic_map
*isl_basic_map_align_divs(
9656 __isl_take isl_basic_map
*dst
, __isl_keep isl_basic_map
*src
)
9665 return isl_basic_map_free(dst
);
9667 if (src
->n_div
== 0)
9670 known
= isl_basic_map_divs_known(src
);
9672 return isl_basic_map_free(dst
);
9674 isl_die(isl_basic_map_get_ctx(src
), isl_error_invalid
,
9675 "some src divs are unknown",
9676 return isl_basic_map_free(dst
));
9678 v_div
= isl_basic_map_var_offset(src
, isl_dim_div
);
9680 return isl_basic_map_free(dst
);
9683 dst_n_div
= isl_basic_map_dim(dst
, isl_dim_div
);
9685 dst
= isl_basic_map_free(dst
);
9686 for (i
= 0; i
< src
->n_div
; ++i
) {
9687 int j
= find_div(dst
, src
, i
);
9689 dst
= isl_basic_map_free(dst
);
9690 if (j
== dst_n_div
) {
9692 int extra
= src
->n_div
- i
;
9693 dst
= isl_basic_map_cow(dst
);
9695 return isl_basic_map_free(dst
);
9696 dst
= isl_basic_map_extend(dst
,
9697 extra
, 0, 2 * extra
);
9700 j
= isl_basic_map_alloc_div(dst
);
9702 return isl_basic_map_free(dst
);
9703 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+v_div
+i
);
9704 isl_seq_clr(dst
->div
[j
]+1+1+v_div
+i
, dst
->n_div
- i
);
9706 dst
= isl_basic_map_add_div_constraints(dst
, j
);
9708 return isl_basic_map_free(dst
);
9711 dst
= isl_basic_map_swap_div(dst
, i
, j
);
9713 return isl_basic_map_free(dst
);
9715 return isl_basic_map_order_divs(dst
);
9718 __isl_give isl_map
*isl_map_align_divs_internal(__isl_take isl_map
*map
)
9726 map
= isl_map_compute_divs(map
);
9727 map
= isl_map_order_divs(map
);
9728 map
= isl_map_cow(map
);
9732 for (i
= 1; i
< map
->n
; ++i
)
9733 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], map
->p
[i
]);
9734 for (i
= 1; i
< map
->n
; ++i
) {
9735 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], map
->p
[0]);
9737 return isl_map_free(map
);
9740 map
= isl_map_unmark_normalized(map
);
9744 __isl_give isl_map
*isl_map_align_divs(__isl_take isl_map
*map
)
9746 return isl_map_align_divs_internal(map
);
9749 __isl_give isl_set
*isl_set_align_divs(__isl_take isl_set
*set
)
9751 return set_from_map(isl_map_align_divs_internal(set_to_map(set
)));
9754 /* Align the divs of the basic maps in "map" to those
9755 * of the basic maps in "list", as well as to the other basic maps in "map".
9756 * The elements in "list" are assumed to have known divs.
9758 __isl_give isl_map
*isl_map_align_divs_to_basic_map_list(
9759 __isl_take isl_map
*map
, __isl_keep isl_basic_map_list
*list
)
9764 n
= isl_basic_map_list_n_basic_map(list
);
9765 map
= isl_map_compute_divs(map
);
9766 map
= isl_map_cow(map
);
9768 return isl_map_free(map
);
9772 for (i
= 0; i
< n
; ++i
) {
9773 isl_basic_map
*bmap
;
9775 bmap
= isl_basic_map_list_get_basic_map(list
, i
);
9776 bmap
= isl_basic_map_order_divs(bmap
);
9777 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], bmap
);
9778 isl_basic_map_free(bmap
);
9781 return isl_map_free(map
);
9783 return isl_map_align_divs_internal(map
);
9786 /* Align the divs of each element of "list" to those of "bmap".
9787 * Both "bmap" and the elements of "list" are assumed to have known divs.
9789 __isl_give isl_basic_map_list
*isl_basic_map_list_align_divs_to_basic_map(
9790 __isl_take isl_basic_map_list
*list
, __isl_keep isl_basic_map
*bmap
)
9795 n
= isl_basic_map_list_n_basic_map(list
);
9797 return isl_basic_map_list_free(list
);
9799 for (i
= 0; i
< n
; ++i
) {
9800 isl_basic_map
*bmap_i
;
9802 bmap_i
= isl_basic_map_list_get_basic_map(list
, i
);
9803 bmap_i
= isl_basic_map_align_divs(bmap_i
, bmap
);
9804 list
= isl_basic_map_list_set_basic_map(list
, i
, bmap_i
);
9810 __isl_give isl_set
*isl_set_apply( __isl_take isl_set
*set
,
9811 __isl_take isl_map
*map
)
9815 isl_map_align_params_set(&map
, &set
);
9816 ok
= isl_map_compatible_domain(map
, set
);
9820 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
9821 "incompatible spaces", goto error
);
9822 map
= isl_map_intersect_domain(map
, set
);
9823 set
= isl_map_range(map
);
9831 /* There is no need to cow as removing empty parts doesn't change
9832 * the meaning of the set.
9834 __isl_give isl_map
*isl_map_remove_empty_parts(__isl_take isl_map
*map
)
9841 for (i
= map
->n
- 1; i
>= 0; --i
)
9842 map
= remove_if_empty(map
, i
);
9847 __isl_give isl_set
*isl_set_remove_empty_parts(__isl_take isl_set
*set
)
9849 return set_from_map(isl_map_remove_empty_parts(set_to_map(set
)));
9852 /* Create a binary relation that maps the shared initial "pos" dimensions
9853 * of "bset1" and "bset2" to the remaining dimensions of "bset1" and "bset2".
9855 static __isl_give isl_basic_map
*join_initial(__isl_keep isl_basic_set
*bset1
,
9856 __isl_keep isl_basic_set
*bset2
, int pos
)
9858 isl_basic_map
*bmap1
;
9859 isl_basic_map
*bmap2
;
9861 bmap1
= isl_basic_map_from_range(isl_basic_set_copy(bset1
));
9862 bmap2
= isl_basic_map_from_range(isl_basic_set_copy(bset2
));
9863 bmap1
= isl_basic_map_move_dims(bmap1
, isl_dim_in
, 0,
9864 isl_dim_out
, 0, pos
);
9865 bmap2
= isl_basic_map_move_dims(bmap2
, isl_dim_in
, 0,
9866 isl_dim_out
, 0, pos
);
9867 return isl_basic_map_range_product(bmap1
, bmap2
);
9870 /* Given two basic sets bset1 and bset2, compute the maximal difference
9871 * between the values of dimension pos in bset1 and those in bset2
9872 * for any common value of the parameters and dimensions preceding pos.
9874 static enum isl_lp_result
basic_set_maximal_difference_at(
9875 __isl_keep isl_basic_set
*bset1
, __isl_keep isl_basic_set
*bset2
,
9876 int pos
, isl_int
*opt
)
9878 isl_basic_map
*bmap1
;
9879 struct isl_ctx
*ctx
;
9880 struct isl_vec
*obj
;
9884 enum isl_lp_result res
;
9886 nparam
= isl_basic_set_dim(bset1
, isl_dim_param
);
9887 dim1
= isl_basic_set_dim(bset1
, isl_dim_set
);
9888 if (nparam
< 0 || dim1
< 0 || !bset2
)
9889 return isl_lp_error
;
9891 bmap1
= join_initial(bset1
, bset2
, pos
);
9892 total
= isl_basic_map_dim(bmap1
, isl_dim_all
);
9894 return isl_lp_error
;
9897 obj
= isl_vec_alloc(ctx
, 1 + total
);
9900 isl_seq_clr(obj
->block
.data
, 1 + total
);
9901 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
], 1);
9902 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
+(dim1
-pos
)], -1);
9903 res
= isl_basic_map_solve_lp(bmap1
, 1, obj
->block
.data
, ctx
->one
,
9905 isl_basic_map_free(bmap1
);
9909 isl_basic_map_free(bmap1
);
9910 return isl_lp_error
;
9913 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
9914 * for any common value of the parameters and dimensions preceding pos
9915 * in both basic sets, the values of dimension pos in bset1 are
9916 * smaller or larger than those in bset2.
9919 * 1 if bset1 follows bset2
9920 * -1 if bset1 precedes bset2
9921 * 0 if bset1 and bset2 are incomparable
9922 * -2 if some error occurred.
9924 int isl_basic_set_compare_at(__isl_keep isl_basic_set
*bset1
,
9925 __isl_keep isl_basic_set
*bset2
, int pos
)
9928 enum isl_lp_result res
;
9933 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
9935 if (res
== isl_lp_empty
)
9937 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
9938 res
== isl_lp_unbounded
)
9940 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
9949 /* Given two basic sets bset1 and bset2, check whether
9950 * for any common value of the parameters and dimensions preceding pos
9951 * there is a value of dimension pos in bset1 that is larger
9952 * than a value of the same dimension in bset2.
9955 * 1 if there exists such a pair
9956 * 0 if there is no such pair, but there is a pair of equal values
9958 * -2 if some error occurred.
9960 int isl_basic_set_follows_at(__isl_keep isl_basic_set
*bset1
,
9961 __isl_keep isl_basic_set
*bset2
, int pos
)
9964 isl_basic_map
*bmap
;
9967 dim1
= isl_basic_set_dim(bset1
, isl_dim_set
);
9970 bmap
= join_initial(bset1
, bset2
, pos
);
9971 bmap
= isl_basic_map_order_ge(bmap
, isl_dim_out
, 0,
9972 isl_dim_out
, dim1
- pos
);
9973 empty
= isl_basic_map_is_empty(bmap
);
9977 isl_basic_map_free(bmap
);
9980 bmap
= isl_basic_map_order_gt(bmap
, isl_dim_out
, 0,
9981 isl_dim_out
, dim1
- pos
);
9982 empty
= isl_basic_map_is_empty(bmap
);
9985 isl_basic_map_free(bmap
);
9990 isl_basic_map_free(bmap
);
9994 /* Given two sets set1 and set2, check whether
9995 * for any common value of the parameters and dimensions preceding pos
9996 * there is a value of dimension pos in set1 that is larger
9997 * than a value of the same dimension in set2.
10000 * 1 if there exists such a pair
10001 * 0 if there is no such pair, but there is a pair of equal values
10003 * -2 if some error occurred.
10005 int isl_set_follows_at(__isl_keep isl_set
*set1
,
10006 __isl_keep isl_set
*set2
, int pos
)
10011 if (!set1
|| !set2
)
10014 for (i
= 0; i
< set1
->n
; ++i
)
10015 for (j
= 0; j
< set2
->n
; ++j
) {
10017 f
= isl_basic_set_follows_at(set1
->p
[i
], set2
->p
[j
], pos
);
10018 if (f
== 1 || f
== -2)
10027 static isl_bool
isl_basic_map_plain_has_fixed_var(
10028 __isl_keep isl_basic_map
*bmap
, unsigned pos
, isl_int
*val
)
10034 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
10036 return isl_bool_error
;
10037 for (i
= 0, d
= total
-1; i
< bmap
->n_eq
&& d
+1 > pos
; ++i
) {
10038 for (; d
+1 > pos
; --d
)
10039 if (!isl_int_is_zero(bmap
->eq
[i
][1+d
]))
10043 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1, d
) != -1)
10044 return isl_bool_false
;
10045 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1+d
+1, total
-d
-1) != -1)
10046 return isl_bool_false
;
10047 if (!isl_int_is_one(bmap
->eq
[i
][1+d
]))
10048 return isl_bool_false
;
10050 isl_int_neg(*val
, bmap
->eq
[i
][0]);
10051 return isl_bool_true
;
10053 return isl_bool_false
;
10056 static isl_bool
isl_map_plain_has_fixed_var(__isl_keep isl_map
*map
,
10057 unsigned pos
, isl_int
*val
)
10065 return isl_bool_error
;
10067 return isl_bool_false
;
10069 return isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, val
);
10072 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, &v
);
10073 for (i
= 1; fixed
== isl_bool_true
&& i
< map
->n
; ++i
) {
10074 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[i
], pos
, &tmp
);
10075 if (fixed
== isl_bool_true
&& isl_int_ne(tmp
, v
))
10076 fixed
= isl_bool_false
;
10079 isl_int_set(*val
, v
);
10080 isl_int_clear(tmp
);
10085 static isl_bool
isl_basic_set_plain_has_fixed_var(
10086 __isl_keep isl_basic_set
*bset
, unsigned pos
, isl_int
*val
)
10088 return isl_basic_map_plain_has_fixed_var(bset_to_bmap(bset
),
10092 isl_bool
isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map
*bmap
,
10093 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
10095 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
10096 return isl_bool_error
;
10097 return isl_basic_map_plain_has_fixed_var(bmap
,
10098 isl_basic_map_offset(bmap
, type
) - 1 + pos
, val
);
10101 /* If "bmap" obviously lies on a hyperplane where the given dimension
10102 * has a fixed value, then return that value.
10103 * Otherwise return NaN.
10105 __isl_give isl_val
*isl_basic_map_plain_get_val_if_fixed(
10106 __isl_keep isl_basic_map
*bmap
,
10107 enum isl_dim_type type
, unsigned pos
)
10115 ctx
= isl_basic_map_get_ctx(bmap
);
10116 v
= isl_val_alloc(ctx
);
10119 fixed
= isl_basic_map_plain_is_fixed(bmap
, type
, pos
, &v
->n
);
10121 return isl_val_free(v
);
10123 isl_int_set_si(v
->d
, 1);
10127 return isl_val_nan(ctx
);
10130 isl_bool
isl_map_plain_is_fixed(__isl_keep isl_map
*map
,
10131 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
10133 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
10134 return isl_bool_error
;
10135 return isl_map_plain_has_fixed_var(map
,
10136 map_offset(map
, type
) - 1 + pos
, val
);
10139 /* If "map" obviously lies on a hyperplane where the given dimension
10140 * has a fixed value, then return that value.
10141 * Otherwise return NaN.
10143 __isl_give isl_val
*isl_map_plain_get_val_if_fixed(__isl_keep isl_map
*map
,
10144 enum isl_dim_type type
, unsigned pos
)
10152 ctx
= isl_map_get_ctx(map
);
10153 v
= isl_val_alloc(ctx
);
10156 fixed
= isl_map_plain_is_fixed(map
, type
, pos
, &v
->n
);
10158 return isl_val_free(v
);
10160 isl_int_set_si(v
->d
, 1);
10164 return isl_val_nan(ctx
);
10167 /* If "set" obviously lies on a hyperplane where the given dimension
10168 * has a fixed value, then return that value.
10169 * Otherwise return NaN.
10171 __isl_give isl_val
*isl_set_plain_get_val_if_fixed(__isl_keep isl_set
*set
,
10172 enum isl_dim_type type
, unsigned pos
)
10174 return isl_map_plain_get_val_if_fixed(set
, type
, pos
);
10177 /* Return a sequence of values in the same space as "set"
10178 * that are equal to the corresponding set dimensions of "set"
10179 * for those set dimensions that obviously lie on a hyperplane
10180 * where the dimension has a fixed value.
10181 * The other elements are set to NaN.
10183 __isl_give isl_multi_val
*isl_set_get_plain_multi_val_if_fixed(
10184 __isl_keep isl_set
*set
)
10191 space
= isl_space_drop_all_params(isl_set_get_space(set
));
10192 mv
= isl_multi_val_alloc(space
);
10193 n
= isl_multi_val_size(mv
);
10195 return isl_multi_val_free(mv
);
10197 for (i
= 0; i
< n
; ++i
) {
10200 v
= isl_set_plain_get_val_if_fixed(set
, isl_dim_set
, i
);
10201 mv
= isl_multi_val_set_val(mv
, i
, v
);
10207 /* Check if dimension dim has fixed value and if so and if val is not NULL,
10208 * then return this fixed value in *val.
10210 isl_bool
isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set
*bset
,
10211 unsigned dim
, isl_int
*val
)
10215 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
10217 return isl_bool_error
;
10218 return isl_basic_set_plain_has_fixed_var(bset
, nparam
+ dim
, val
);
10221 /* Return -1 if the constraint "c1" should be sorted before "c2"
10222 * and 1 if it should be sorted after "c2".
10223 * Return 0 if the two constraints are the same (up to the constant term).
10225 * In particular, if a constraint involves later variables than another
10226 * then it is sorted after this other constraint.
10227 * uset_gist depends on constraints without existentially quantified
10228 * variables sorting first.
10230 * For constraints that have the same latest variable, those
10231 * with the same coefficient for this latest variable (first in absolute value
10232 * and then in actual value) are grouped together.
10233 * This is useful for detecting pairs of constraints that can
10234 * be chained in their printed representation.
10236 * Finally, within a group, constraints are sorted according to
10237 * their coefficients (excluding the constant term).
10239 static int sort_constraint_cmp(const void *p1
, const void *p2
, void *arg
)
10241 isl_int
**c1
= (isl_int
**) p1
;
10242 isl_int
**c2
= (isl_int
**) p2
;
10244 unsigned size
= *(unsigned *) arg
;
10247 l1
= isl_seq_last_non_zero(*c1
+ 1, size
);
10248 l2
= isl_seq_last_non_zero(*c2
+ 1, size
);
10253 cmp
= isl_int_abs_cmp((*c1
)[1 + l1
], (*c2
)[1 + l1
]);
10256 cmp
= isl_int_cmp((*c1
)[1 + l1
], (*c2
)[1 + l1
]);
10260 return isl_seq_cmp(*c1
+ 1, *c2
+ 1, size
);
10263 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
10264 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
10265 * and 0 if the two constraints are the same (up to the constant term).
10267 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map
*bmap
,
10268 isl_int
*c1
, isl_int
*c2
)
10273 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
10277 return sort_constraint_cmp(&c1
, &c2
, &size
);
10280 __isl_give isl_basic_map
*isl_basic_map_sort_constraints(
10281 __isl_take isl_basic_map
*bmap
)
10288 if (bmap
->n_ineq
== 0)
10290 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_SORTED
))
10292 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
10294 return isl_basic_map_free(bmap
);
10296 if (isl_sort(bmap
->ineq
, bmap
->n_ineq
, sizeof(isl_int
*),
10297 &sort_constraint_cmp
, &size
) < 0)
10298 return isl_basic_map_free(bmap
);
10299 ISL_F_SET(bmap
, ISL_BASIC_MAP_SORTED
);
10303 __isl_give isl_basic_set
*isl_basic_set_sort_constraints(
10304 __isl_take isl_basic_set
*bset
)
10306 isl_basic_map
*bmap
= bset_to_bmap(bset
);
10307 return bset_from_bmap(isl_basic_map_sort_constraints(bmap
));
10310 __isl_give isl_basic_map
*isl_basic_map_normalize(
10311 __isl_take isl_basic_map
*bmap
)
10313 bmap
= isl_basic_map_remove_redundancies(bmap
);
10314 bmap
= isl_basic_map_sort_constraints(bmap
);
10317 int isl_basic_map_plain_cmp(__isl_keep isl_basic_map
*bmap1
,
10318 __isl_keep isl_basic_map
*bmap2
)
10322 isl_space
*space1
, *space2
;
10324 if (!bmap1
|| !bmap2
)
10327 if (bmap1
== bmap2
)
10329 space1
= isl_basic_map_peek_space(bmap1
);
10330 space2
= isl_basic_map_peek_space(bmap2
);
10331 cmp
= isl_space_cmp(space1
, space2
);
10334 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) !=
10335 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_RATIONAL
))
10336 return ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) ? -1 : 1;
10337 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
) &&
10338 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
10340 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
))
10342 if (ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
10344 if (bmap1
->n_eq
!= bmap2
->n_eq
)
10345 return bmap1
->n_eq
- bmap2
->n_eq
;
10346 if (bmap1
->n_ineq
!= bmap2
->n_ineq
)
10347 return bmap1
->n_ineq
- bmap2
->n_ineq
;
10348 if (bmap1
->n_div
!= bmap2
->n_div
)
10349 return bmap1
->n_div
- bmap2
->n_div
;
10350 total
= isl_basic_map_dim(bmap1
, isl_dim_all
);
10353 for (i
= 0; i
< bmap1
->n_eq
; ++i
) {
10354 cmp
= isl_seq_cmp(bmap1
->eq
[i
], bmap2
->eq
[i
], 1+total
);
10358 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
10359 cmp
= isl_seq_cmp(bmap1
->ineq
[i
], bmap2
->ineq
[i
], 1+total
);
10363 for (i
= 0; i
< bmap1
->n_div
; ++i
) {
10364 cmp
= isl_seq_cmp(bmap1
->div
[i
], bmap2
->div
[i
], 1+1+total
);
10371 int isl_basic_set_plain_cmp(__isl_keep isl_basic_set
*bset1
,
10372 __isl_keep isl_basic_set
*bset2
)
10374 return isl_basic_map_plain_cmp(bset1
, bset2
);
10377 int isl_set_plain_cmp(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
10383 if (set1
->n
!= set2
->n
)
10384 return set1
->n
- set2
->n
;
10386 for (i
= 0; i
< set1
->n
; ++i
) {
10387 cmp
= isl_basic_set_plain_cmp(set1
->p
[i
], set2
->p
[i
]);
10395 isl_bool
isl_basic_map_plain_is_equal(__isl_keep isl_basic_map
*bmap1
,
10396 __isl_keep isl_basic_map
*bmap2
)
10398 if (!bmap1
|| !bmap2
)
10399 return isl_bool_error
;
10400 return isl_basic_map_plain_cmp(bmap1
, bmap2
) == 0;
10403 isl_bool
isl_basic_set_plain_is_equal(__isl_keep isl_basic_set
*bset1
,
10404 __isl_keep isl_basic_set
*bset2
)
10406 return isl_basic_map_plain_is_equal(bset_to_bmap(bset1
),
10407 bset_to_bmap(bset2
));
10410 static int qsort_bmap_cmp(const void *p1
, const void *p2
)
10412 isl_basic_map
*bmap1
= *(isl_basic_map
**) p1
;
10413 isl_basic_map
*bmap2
= *(isl_basic_map
**) p2
;
10415 return isl_basic_map_plain_cmp(bmap1
, bmap2
);
10418 /* Sort the basic maps of "map" and remove duplicate basic maps.
10420 * While removing basic maps, we make sure that the basic maps remain
10421 * sorted because isl_map_normalize expects the basic maps of the result
10424 static __isl_give isl_map
*sort_and_remove_duplicates(__isl_take isl_map
*map
)
10428 map
= isl_map_remove_empty_parts(map
);
10431 qsort(map
->p
, map
->n
, sizeof(struct isl_basic_map
*), qsort_bmap_cmp
);
10432 for (i
= map
->n
- 1; i
>= 1; --i
) {
10433 if (!isl_basic_map_plain_is_equal(map
->p
[i
- 1], map
->p
[i
]))
10435 isl_basic_map_free(map
->p
[i
-1]);
10436 for (j
= i
; j
< map
->n
; ++j
)
10437 map
->p
[j
- 1] = map
->p
[j
];
10444 /* Remove obvious duplicates among the basic maps of "map".
10446 * Unlike isl_map_normalize, this function does not remove redundant
10447 * constraints and only removes duplicates that have exactly the same
10448 * constraints in the input. It does sort the constraints and
10449 * the basic maps to ease the detection of duplicates.
10451 * If "map" has already been normalized or if the basic maps are
10452 * disjoint, then there can be no duplicates.
10454 __isl_give isl_map
*isl_map_remove_obvious_duplicates(__isl_take isl_map
*map
)
10457 isl_basic_map
*bmap
;
10463 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
| ISL_MAP_DISJOINT
))
10465 for (i
= 0; i
< map
->n
; ++i
) {
10466 bmap
= isl_basic_map_copy(map
->p
[i
]);
10467 bmap
= isl_basic_map_sort_constraints(bmap
);
10469 return isl_map_free(map
);
10470 isl_basic_map_free(map
->p
[i
]);
10474 map
= sort_and_remove_duplicates(map
);
10478 /* We normalize in place, but if anything goes wrong we need
10479 * to return NULL, so we need to make sure we don't change the
10480 * meaning of any possible other copies of map.
10482 __isl_give isl_map
*isl_map_normalize(__isl_take isl_map
*map
)
10485 struct isl_basic_map
*bmap
;
10489 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
))
10491 for (i
= 0; i
< map
->n
; ++i
) {
10492 bmap
= isl_basic_map_normalize(isl_basic_map_copy(map
->p
[i
]));
10495 isl_basic_map_free(map
->p
[i
]);
10499 map
= sort_and_remove_duplicates(map
);
10501 ISL_F_SET(map
, ISL_MAP_NORMALIZED
);
10508 __isl_give isl_set
*isl_set_normalize(__isl_take isl_set
*set
)
10510 return set_from_map(isl_map_normalize(set_to_map(set
)));
10513 isl_bool
isl_map_plain_is_equal(__isl_keep isl_map
*map1
,
10514 __isl_keep isl_map
*map2
)
10519 if (!map1
|| !map2
)
10520 return isl_bool_error
;
10523 return isl_bool_true
;
10524 equal
= isl_map_has_equal_space(map1
, map2
);
10525 if (equal
< 0 || !equal
)
10528 map1
= isl_map_copy(map1
);
10529 map2
= isl_map_copy(map2
);
10530 map1
= isl_map_normalize(map1
);
10531 map2
= isl_map_normalize(map2
);
10532 if (!map1
|| !map2
)
10534 equal
= map1
->n
== map2
->n
;
10535 for (i
= 0; equal
&& i
< map1
->n
; ++i
) {
10536 equal
= isl_basic_map_plain_is_equal(map1
->p
[i
], map2
->p
[i
]);
10540 isl_map_free(map1
);
10541 isl_map_free(map2
);
10544 isl_map_free(map1
);
10545 isl_map_free(map2
);
10546 return isl_bool_error
;
10549 isl_bool
isl_set_plain_is_equal(__isl_keep isl_set
*set1
,
10550 __isl_keep isl_set
*set2
)
10552 return isl_map_plain_is_equal(set_to_map(set1
), set_to_map(set2
));
10555 /* Return the basic maps in "map" as a list.
10557 __isl_give isl_basic_map_list
*isl_map_get_basic_map_list(
10558 __isl_keep isl_map
*map
)
10562 isl_basic_map_list
*list
;
10566 ctx
= isl_map_get_ctx(map
);
10567 list
= isl_basic_map_list_alloc(ctx
, map
->n
);
10569 for (i
= 0; i
< map
->n
; ++i
) {
10570 isl_basic_map
*bmap
;
10572 bmap
= isl_basic_map_copy(map
->p
[i
]);
10573 list
= isl_basic_map_list_add(list
, bmap
);
10579 /* Return the intersection of the elements in the non-empty list "list".
10580 * All elements are assumed to live in the same space.
10582 __isl_give isl_basic_map
*isl_basic_map_list_intersect(
10583 __isl_take isl_basic_map_list
*list
)
10587 isl_basic_map
*bmap
;
10589 n
= isl_basic_map_list_n_basic_map(list
);
10593 isl_die(isl_basic_map_list_get_ctx(list
), isl_error_invalid
,
10594 "expecting non-empty list", goto error
);
10596 bmap
= isl_basic_map_list_get_basic_map(list
, 0);
10597 for (i
= 1; i
< n
; ++i
) {
10598 isl_basic_map
*bmap_i
;
10600 bmap_i
= isl_basic_map_list_get_basic_map(list
, i
);
10601 bmap
= isl_basic_map_intersect(bmap
, bmap_i
);
10604 isl_basic_map_list_free(list
);
10607 isl_basic_map_list_free(list
);
10611 /* Return the intersection of the elements in the non-empty list "list".
10612 * All elements are assumed to live in the same space.
10614 __isl_give isl_basic_set
*isl_basic_set_list_intersect(
10615 __isl_take isl_basic_set_list
*list
)
10617 return isl_basic_map_list_intersect(list
);
10620 /* Return the union of the elements of "list".
10621 * The list is required to have at least one element.
10623 __isl_give isl_set
*isl_basic_set_list_union(
10624 __isl_take isl_basic_set_list
*list
)
10629 isl_basic_set
*bset
;
10632 n
= isl_basic_set_list_n_basic_set(list
);
10636 isl_die(isl_basic_set_list_get_ctx(list
), isl_error_invalid
,
10637 "expecting non-empty list", goto error
);
10639 bset
= isl_basic_set_list_get_basic_set(list
, 0);
10640 space
= isl_basic_set_get_space(bset
);
10641 isl_basic_set_free(bset
);
10643 set
= isl_set_alloc_space(space
, n
, 0);
10644 for (i
= 0; i
< n
; ++i
) {
10645 bset
= isl_basic_set_list_get_basic_set(list
, i
);
10646 set
= isl_set_add_basic_set(set
, bset
);
10649 isl_basic_set_list_free(list
);
10652 isl_basic_set_list_free(list
);
10656 /* Return the union of the elements in the non-empty list "list".
10657 * All elements are assumed to live in the same space.
10659 __isl_give isl_set
*isl_set_list_union(__isl_take isl_set_list
*list
)
10665 n
= isl_set_list_n_set(list
);
10669 isl_die(isl_set_list_get_ctx(list
), isl_error_invalid
,
10670 "expecting non-empty list", goto error
);
10672 set
= isl_set_list_get_set(list
, 0);
10673 for (i
= 1; i
< n
; ++i
) {
10676 set_i
= isl_set_list_get_set(list
, i
);
10677 set
= isl_set_union(set
, set_i
);
10680 isl_set_list_free(list
);
10683 isl_set_list_free(list
);
10687 __isl_give isl_basic_map
*isl_basic_map_product(
10688 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
10690 isl_space
*space_result
= NULL
;
10691 struct isl_basic_map
*bmap
;
10692 unsigned in1
, in2
, out1
, out2
, nparam
, total
, pos
;
10693 struct isl_dim_map
*dim_map1
, *dim_map2
;
10695 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
10697 space_result
= isl_space_product(isl_space_copy(bmap1
->dim
),
10698 isl_space_copy(bmap2
->dim
));
10700 in1
= isl_basic_map_dim(bmap1
, isl_dim_in
);
10701 in2
= isl_basic_map_dim(bmap2
, isl_dim_in
);
10702 out1
= isl_basic_map_dim(bmap1
, isl_dim_out
);
10703 out2
= isl_basic_map_dim(bmap2
, isl_dim_out
);
10704 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
10706 total
= nparam
+ in1
+ in2
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
10707 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
10708 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
10709 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
10710 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
10711 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
10712 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
10713 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
10714 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
10715 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
10716 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
10718 bmap
= isl_basic_map_alloc_space(space_result
,
10719 bmap1
->n_div
+ bmap2
->n_div
,
10720 bmap1
->n_eq
+ bmap2
->n_eq
,
10721 bmap1
->n_ineq
+ bmap2
->n_ineq
);
10722 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
10723 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
10724 bmap
= isl_basic_map_simplify(bmap
);
10725 return isl_basic_map_finalize(bmap
);
10727 isl_basic_map_free(bmap1
);
10728 isl_basic_map_free(bmap2
);
10732 __isl_give isl_basic_map
*isl_basic_map_flat_product(
10733 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
10735 isl_basic_map
*prod
;
10737 prod
= isl_basic_map_product(bmap1
, bmap2
);
10738 prod
= isl_basic_map_flatten(prod
);
10742 __isl_give isl_basic_set
*isl_basic_set_flat_product(
10743 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
10745 return isl_basic_map_flat_range_product(bset1
, bset2
);
10748 __isl_give isl_basic_map
*isl_basic_map_domain_product(
10749 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
10751 isl_space
*space1
, *space2
;
10752 isl_space
*space_result
= NULL
;
10753 isl_basic_map
*bmap
;
10754 isl_size in1
, in2
, out
, nparam
;
10755 unsigned total
, pos
;
10756 struct isl_dim_map
*dim_map1
, *dim_map2
;
10758 in1
= isl_basic_map_dim(bmap1
, isl_dim_in
);
10759 in2
= isl_basic_map_dim(bmap2
, isl_dim_in
);
10760 out
= isl_basic_map_dim(bmap1
, isl_dim_out
);
10761 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
10762 if (in1
< 0 || in2
< 0 || out
< 0 || nparam
< 0)
10765 space1
= isl_basic_map_get_space(bmap1
);
10766 space2
= isl_basic_map_get_space(bmap2
);
10767 space_result
= isl_space_domain_product(space1
, space2
);
10769 total
= nparam
+ in1
+ in2
+ out
+ bmap1
->n_div
+ bmap2
->n_div
;
10770 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
10771 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
10772 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
10773 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
10774 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
10775 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
10776 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
10777 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
);
10778 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out
);
10779 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
10781 bmap
= isl_basic_map_alloc_space(space_result
,
10782 bmap1
->n_div
+ bmap2
->n_div
,
10783 bmap1
->n_eq
+ bmap2
->n_eq
,
10784 bmap1
->n_ineq
+ bmap2
->n_ineq
);
10785 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
10786 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
10787 bmap
= isl_basic_map_simplify(bmap
);
10788 return isl_basic_map_finalize(bmap
);
10790 isl_basic_map_free(bmap1
);
10791 isl_basic_map_free(bmap2
);
10795 __isl_give isl_basic_map
*isl_basic_map_range_product(
10796 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
10799 isl_space
*space_result
= NULL
;
10800 isl_basic_map
*bmap
;
10801 isl_size in
, out1
, out2
, nparam
;
10802 unsigned total
, pos
;
10803 struct isl_dim_map
*dim_map1
, *dim_map2
;
10805 rational
= isl_basic_map_is_rational(bmap1
);
10806 if (rational
>= 0 && rational
)
10807 rational
= isl_basic_map_is_rational(bmap2
);
10808 in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
10809 out1
= isl_basic_map_dim(bmap1
, isl_dim_out
);
10810 out2
= isl_basic_map_dim(bmap2
, isl_dim_out
);
10811 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
10812 if (in
< 0 || out1
< 0 || out2
< 0 || nparam
< 0 || rational
< 0)
10815 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
10818 space_result
= isl_space_range_product(isl_space_copy(bmap1
->dim
),
10819 isl_space_copy(bmap2
->dim
));
10821 total
= nparam
+ in
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
10822 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
10823 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
10824 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
10825 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
10826 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
10827 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
10828 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in
);
10829 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
10830 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
10831 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
10833 bmap
= isl_basic_map_alloc_space(space_result
,
10834 bmap1
->n_div
+ bmap2
->n_div
,
10835 bmap1
->n_eq
+ bmap2
->n_eq
,
10836 bmap1
->n_ineq
+ bmap2
->n_ineq
);
10837 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
10838 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
10840 bmap
= isl_basic_map_set_rational(bmap
);
10841 bmap
= isl_basic_map_simplify(bmap
);
10842 return isl_basic_map_finalize(bmap
);
10844 isl_basic_map_free(bmap1
);
10845 isl_basic_map_free(bmap2
);
10849 __isl_give isl_basic_map
*isl_basic_map_flat_range_product(
10850 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
10852 isl_basic_map
*prod
;
10854 prod
= isl_basic_map_range_product(bmap1
, bmap2
);
10855 prod
= isl_basic_map_flatten_range(prod
);
10859 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
10860 * and collect the results.
10861 * The result live in the space obtained by calling "space_product"
10862 * on the spaces of "map1" and "map2".
10863 * If "remove_duplicates" is set then the result may contain duplicates
10864 * (even if the inputs do not) and so we try and remove the obvious
10867 static __isl_give isl_map
*map_product(__isl_take isl_map
*map1
,
10868 __isl_take isl_map
*map2
,
10869 __isl_give isl_space
*(*space_product
)(__isl_take isl_space
*left
,
10870 __isl_take isl_space
*right
),
10871 __isl_give isl_basic_map
*(*basic_map_product
)(
10872 __isl_take isl_basic_map
*left
,
10873 __isl_take isl_basic_map
*right
),
10874 int remove_duplicates
)
10876 unsigned flags
= 0;
10877 struct isl_map
*result
;
10881 m
= isl_map_has_equal_params(map1
, map2
);
10885 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
10886 "parameters don't match", goto error
);
10888 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
10889 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
10890 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
10892 result
= isl_map_alloc_space(space_product(isl_space_copy(map1
->dim
),
10893 isl_space_copy(map2
->dim
)),
10894 map1
->n
* map2
->n
, flags
);
10897 for (i
= 0; i
< map1
->n
; ++i
)
10898 for (j
= 0; j
< map2
->n
; ++j
) {
10899 struct isl_basic_map
*part
;
10900 part
= basic_map_product(isl_basic_map_copy(map1
->p
[i
]),
10901 isl_basic_map_copy(map2
->p
[j
]));
10902 if (isl_basic_map_is_empty(part
))
10903 isl_basic_map_free(part
);
10905 result
= isl_map_add_basic_map(result
, part
);
10909 if (remove_duplicates
)
10910 result
= isl_map_remove_obvious_duplicates(result
);
10911 isl_map_free(map1
);
10912 isl_map_free(map2
);
10915 isl_map_free(map1
);
10916 isl_map_free(map2
);
10920 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
10922 __isl_give isl_map
*isl_map_product(__isl_take isl_map
*map1
,
10923 __isl_take isl_map
*map2
)
10925 isl_map_align_params_bin(&map1
, &map2
);
10926 return map_product(map1
, map2
, &isl_space_product
,
10927 &isl_basic_map_product
, 0);
10930 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
10932 __isl_give isl_map
*isl_map_flat_product(__isl_take isl_map
*map1
,
10933 __isl_take isl_map
*map2
)
10937 prod
= isl_map_product(map1
, map2
);
10938 prod
= isl_map_flatten(prod
);
10942 /* Given two set A and B, construct its Cartesian product A x B.
10944 __isl_give isl_set
*isl_set_product(__isl_take isl_set
*set1
,
10945 __isl_take isl_set
*set2
)
10947 return isl_map_range_product(set1
, set2
);
10950 __isl_give isl_set
*isl_set_flat_product(__isl_take isl_set
*set1
,
10951 __isl_take isl_set
*set2
)
10953 return isl_map_flat_range_product(set1
, set2
);
10956 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
10958 __isl_give isl_map
*isl_map_domain_product(__isl_take isl_map
*map1
,
10959 __isl_take isl_map
*map2
)
10961 isl_map_align_params_bin(&map1
, &map2
);
10962 return map_product(map1
, map2
, &isl_space_domain_product
,
10963 &isl_basic_map_domain_product
, 1);
10966 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
10968 __isl_give isl_map
*isl_map_range_product(__isl_take isl_map
*map1
,
10969 __isl_take isl_map
*map2
)
10971 isl_map_align_params_bin(&map1
, &map2
);
10972 return map_product(map1
, map2
, &isl_space_range_product
,
10973 &isl_basic_map_range_product
, 1);
10976 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
10978 __isl_give isl_map
*isl_map_factor_domain(__isl_take isl_map
*map
)
10981 isl_size total1
, keep1
, total2
, keep2
;
10983 total1
= isl_map_dim(map
, isl_dim_in
);
10984 total2
= isl_map_dim(map
, isl_dim_out
);
10985 if (total1
< 0 || total2
< 0)
10986 return isl_map_free(map
);
10987 if (!isl_space_domain_is_wrapping(map
->dim
) ||
10988 !isl_space_range_is_wrapping(map
->dim
))
10989 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10990 "not a product", return isl_map_free(map
));
10992 space
= isl_map_get_space(map
);
10993 space
= isl_space_factor_domain(space
);
10994 keep1
= isl_space_dim(space
, isl_dim_in
);
10995 keep2
= isl_space_dim(space
, isl_dim_out
);
10996 if (keep1
< 0 || keep2
< 0)
10997 map
= isl_map_free(map
);
10998 map
= isl_map_project_out(map
, isl_dim_in
, keep1
, total1
- keep1
);
10999 map
= isl_map_project_out(map
, isl_dim_out
, keep2
, total2
- keep2
);
11000 map
= isl_map_reset_space(map
, space
);
11005 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
11007 __isl_give isl_map
*isl_map_factor_range(__isl_take isl_map
*map
)
11010 isl_size total1
, keep1
, total2
, keep2
;
11012 total1
= isl_map_dim(map
, isl_dim_in
);
11013 total2
= isl_map_dim(map
, isl_dim_out
);
11014 if (total1
< 0 || total2
< 0)
11015 return isl_map_free(map
);
11016 if (!isl_space_domain_is_wrapping(map
->dim
) ||
11017 !isl_space_range_is_wrapping(map
->dim
))
11018 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
11019 "not a product", return isl_map_free(map
));
11021 space
= isl_map_get_space(map
);
11022 space
= isl_space_factor_range(space
);
11023 keep1
= isl_space_dim(space
, isl_dim_in
);
11024 keep2
= isl_space_dim(space
, isl_dim_out
);
11025 if (keep1
< 0 || keep2
< 0)
11026 map
= isl_map_free(map
);
11027 map
= isl_map_project_out(map
, isl_dim_in
, 0, total1
- keep1
);
11028 map
= isl_map_project_out(map
, isl_dim_out
, 0, total2
- keep2
);
11029 map
= isl_map_reset_space(map
, space
);
11034 /* Given a map of the form [A -> B] -> C, return the map A -> C.
11036 __isl_give isl_map
*isl_map_domain_factor_domain(__isl_take isl_map
*map
)
11039 isl_size total
, keep
;
11041 total
= isl_map_dim(map
, isl_dim_in
);
11043 return isl_map_free(map
);
11044 if (!isl_space_domain_is_wrapping(map
->dim
))
11045 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
11046 "domain is not a product", return isl_map_free(map
));
11048 space
= isl_map_get_space(map
);
11049 space
= isl_space_domain_factor_domain(space
);
11050 keep
= isl_space_dim(space
, isl_dim_in
);
11052 map
= isl_map_free(map
);
11053 map
= isl_map_project_out(map
, isl_dim_in
, keep
, total
- keep
);
11054 map
= isl_map_reset_space(map
, space
);
11059 /* Given a map of the form [A -> B] -> C, return the map B -> C.
11061 __isl_give isl_map
*isl_map_domain_factor_range(__isl_take isl_map
*map
)
11064 isl_size total
, keep
;
11066 total
= isl_map_dim(map
, isl_dim_in
);
11068 return isl_map_free(map
);
11069 if (!isl_space_domain_is_wrapping(map
->dim
))
11070 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
11071 "domain is not a product", return isl_map_free(map
));
11073 space
= isl_map_get_space(map
);
11074 space
= isl_space_domain_factor_range(space
);
11075 keep
= isl_space_dim(space
, isl_dim_in
);
11077 map
= isl_map_free(map
);
11078 map
= isl_map_project_out(map
, isl_dim_in
, 0, total
- keep
);
11079 map
= isl_map_reset_space(map
, space
);
11084 /* Given a map A -> [B -> C], extract the map A -> B.
11086 __isl_give isl_map
*isl_map_range_factor_domain(__isl_take isl_map
*map
)
11089 isl_size total
, keep
;
11091 total
= isl_map_dim(map
, isl_dim_out
);
11093 return isl_map_free(map
);
11094 if (!isl_space_range_is_wrapping(map
->dim
))
11095 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
11096 "range is not a product", return isl_map_free(map
));
11098 space
= isl_map_get_space(map
);
11099 space
= isl_space_range_factor_domain(space
);
11100 keep
= isl_space_dim(space
, isl_dim_out
);
11102 map
= isl_map_free(map
);
11103 map
= isl_map_project_out(map
, isl_dim_out
, keep
, total
- keep
);
11104 map
= isl_map_reset_space(map
, space
);
11109 /* Given a map A -> [B -> C], extract the map A -> C.
11111 __isl_give isl_map
*isl_map_range_factor_range(__isl_take isl_map
*map
)
11114 isl_size total
, keep
;
11116 total
= isl_map_dim(map
, isl_dim_out
);
11118 return isl_map_free(map
);
11119 if (!isl_space_range_is_wrapping(map
->dim
))
11120 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
11121 "range is not a product", return isl_map_free(map
));
11123 space
= isl_map_get_space(map
);
11124 space
= isl_space_range_factor_range(space
);
11125 keep
= isl_space_dim(space
, isl_dim_out
);
11127 map
= isl_map_free(map
);
11128 map
= isl_map_project_out(map
, isl_dim_out
, 0, total
- keep
);
11129 map
= isl_map_reset_space(map
, space
);
11134 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
11136 __isl_give isl_map
*isl_map_flat_domain_product(__isl_take isl_map
*map1
,
11137 __isl_take isl_map
*map2
)
11141 prod
= isl_map_domain_product(map1
, map2
);
11142 prod
= isl_map_flatten_domain(prod
);
11146 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
11148 __isl_give isl_map
*isl_map_flat_range_product(__isl_take isl_map
*map1
,
11149 __isl_take isl_map
*map2
)
11153 prod
= isl_map_range_product(map1
, map2
);
11154 prod
= isl_map_flatten_range(prod
);
11158 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map
*bmap
)
11161 uint32_t hash
= isl_hash_init();
11166 bmap
= isl_basic_map_copy(bmap
);
11167 bmap
= isl_basic_map_normalize(bmap
);
11168 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
11171 isl_hash_byte(hash
, bmap
->n_eq
& 0xFF);
11172 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
11174 c_hash
= isl_seq_get_hash(bmap
->eq
[i
], 1 + total
);
11175 isl_hash_hash(hash
, c_hash
);
11177 isl_hash_byte(hash
, bmap
->n_ineq
& 0xFF);
11178 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
11180 c_hash
= isl_seq_get_hash(bmap
->ineq
[i
], 1 + total
);
11181 isl_hash_hash(hash
, c_hash
);
11183 isl_hash_byte(hash
, bmap
->n_div
& 0xFF);
11184 for (i
= 0; i
< bmap
->n_div
; ++i
) {
11186 if (isl_int_is_zero(bmap
->div
[i
][0]))
11188 isl_hash_byte(hash
, i
& 0xFF);
11189 c_hash
= isl_seq_get_hash(bmap
->div
[i
], 1 + 1 + total
);
11190 isl_hash_hash(hash
, c_hash
);
11192 isl_basic_map_free(bmap
);
11196 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set
*bset
)
11198 return isl_basic_map_get_hash(bset_to_bmap(bset
));
11201 uint32_t isl_map_get_hash(__isl_keep isl_map
*map
)
11208 map
= isl_map_copy(map
);
11209 map
= isl_map_normalize(map
);
11213 hash
= isl_hash_init();
11214 for (i
= 0; i
< map
->n
; ++i
) {
11215 uint32_t bmap_hash
;
11216 bmap_hash
= isl_basic_map_get_hash(map
->p
[i
]);
11217 isl_hash_hash(hash
, bmap_hash
);
11225 uint32_t isl_set_get_hash(__isl_keep isl_set
*set
)
11227 return isl_map_get_hash(set_to_map(set
));
11230 /* Return the number of basic maps in the (current) representation of "map".
11232 isl_size
isl_map_n_basic_map(__isl_keep isl_map
*map
)
11234 return map
? map
->n
: isl_size_error
;
11237 isl_size
isl_set_n_basic_set(__isl_keep isl_set
*set
)
11239 return set
? set
->n
: isl_size_error
;
11242 isl_stat
isl_map_foreach_basic_map(__isl_keep isl_map
*map
,
11243 isl_stat (*fn
)(__isl_take isl_basic_map
*bmap
, void *user
), void *user
)
11248 return isl_stat_error
;
11250 for (i
= 0; i
< map
->n
; ++i
)
11251 if (fn(isl_basic_map_copy(map
->p
[i
]), user
) < 0)
11252 return isl_stat_error
;
11254 return isl_stat_ok
;
11257 isl_stat
isl_set_foreach_basic_set(__isl_keep isl_set
*set
,
11258 isl_stat (*fn
)(__isl_take isl_basic_set
*bset
, void *user
), void *user
)
11263 return isl_stat_error
;
11265 for (i
= 0; i
< set
->n
; ++i
)
11266 if (fn(isl_basic_set_copy(set
->p
[i
]), user
) < 0)
11267 return isl_stat_error
;
11269 return isl_stat_ok
;
11272 /* Does "test" succeed on every basic set in "set"?
11274 isl_bool
isl_set_every_basic_set(__isl_keep isl_set
*set
,
11275 isl_bool (*test
)(__isl_keep isl_basic_set
*bset
, void *user
),
11281 return isl_bool_error
;
11283 for (i
= 0; i
< set
->n
; ++i
) {
11286 r
= test(set
->p
[i
], user
);
11291 return isl_bool_true
;
11294 /* Return a list of basic sets, the union of which is equal to "set".
11296 __isl_give isl_basic_set_list
*isl_set_get_basic_set_list(
11297 __isl_keep isl_set
*set
)
11300 isl_basic_set_list
*list
;
11305 list
= isl_basic_set_list_alloc(isl_set_get_ctx(set
), set
->n
);
11306 for (i
= 0; i
< set
->n
; ++i
) {
11307 isl_basic_set
*bset
;
11309 bset
= isl_basic_set_copy(set
->p
[i
]);
11310 list
= isl_basic_set_list_add(list
, bset
);
11316 __isl_give isl_basic_set
*isl_basic_set_lift(__isl_take isl_basic_set
*bset
)
11323 bset
= isl_basic_set_cow(bset
);
11327 space
= isl_basic_set_get_space(bset
);
11328 space
= isl_space_lift(space
, bset
->n_div
);
11331 isl_space_free(bset
->dim
);
11333 bset
->extra
-= bset
->n_div
;
11336 bset
= isl_basic_set_finalize(bset
);
11340 isl_basic_set_free(bset
);
11344 __isl_give isl_set
*isl_set_lift(__isl_take isl_set
*set
)
11350 set
= set_from_map(isl_map_align_divs_internal(set_to_map(set
)));
11355 set
= isl_set_cow(set
);
11359 n_div
= set
->p
[0]->n_div
;
11360 space
= isl_set_get_space(set
);
11361 space
= isl_space_lift(space
, n_div
);
11364 isl_space_free(set
->dim
);
11367 for (i
= 0; i
< set
->n
; ++i
) {
11368 set
->p
[i
] = isl_basic_set_lift(set
->p
[i
]);
11379 int isl_basic_set_size(__isl_keep isl_basic_set
*bset
)
11384 dim
= isl_basic_set_dim(bset
, isl_dim_all
);
11387 size
+= bset
->n_eq
* (1 + dim
);
11388 size
+= bset
->n_ineq
* (1 + dim
);
11389 size
+= bset
->n_div
* (2 + dim
);
11394 int isl_set_size(__isl_keep isl_set
*set
)
11402 for (i
= 0; i
< set
->n
; ++i
)
11403 size
+= isl_basic_set_size(set
->p
[i
]);
11408 /* Check if there is any lower bound (if lower == 0) and/or upper
11409 * bound (if upper == 0) on the specified dim.
11411 static isl_bool
basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
11412 enum isl_dim_type type
, unsigned pos
, int lower
, int upper
)
11416 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
11417 return isl_bool_error
;
11419 pos
+= isl_basic_map_offset(bmap
, type
);
11421 for (i
= 0; i
< bmap
->n_div
; ++i
) {
11422 if (isl_int_is_zero(bmap
->div
[i
][0]))
11424 if (!isl_int_is_zero(bmap
->div
[i
][1 + pos
]))
11425 return isl_bool_true
;
11428 for (i
= 0; i
< bmap
->n_eq
; ++i
)
11429 if (!isl_int_is_zero(bmap
->eq
[i
][pos
]))
11430 return isl_bool_true
;
11432 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
11433 int sgn
= isl_int_sgn(bmap
->ineq
[i
][pos
]);
11440 return lower
&& upper
;
11443 isl_bool
isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
11444 enum isl_dim_type type
, unsigned pos
)
11446 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 0);
11449 isl_bool
isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map
*bmap
,
11450 enum isl_dim_type type
, unsigned pos
)
11452 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 1);
11455 isl_bool
isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map
*bmap
,
11456 enum isl_dim_type type
, unsigned pos
)
11458 return basic_map_dim_is_bounded(bmap
, type
, pos
, 1, 0);
11461 isl_bool
isl_map_dim_is_bounded(__isl_keep isl_map
*map
,
11462 enum isl_dim_type type
, unsigned pos
)
11467 return isl_bool_error
;
11469 for (i
= 0; i
< map
->n
; ++i
) {
11471 bounded
= isl_basic_map_dim_is_bounded(map
->p
[i
], type
, pos
);
11472 if (bounded
< 0 || !bounded
)
11476 return isl_bool_true
;
11479 /* Return true if the specified dim is involved in both an upper bound
11480 * and a lower bound.
11482 isl_bool
isl_set_dim_is_bounded(__isl_keep isl_set
*set
,
11483 enum isl_dim_type type
, unsigned pos
)
11485 return isl_map_dim_is_bounded(set_to_map(set
), type
, pos
);
11488 /* Does "map" have a bound (according to "fn") for any of its basic maps?
11490 static isl_bool
has_any_bound(__isl_keep isl_map
*map
,
11491 enum isl_dim_type type
, unsigned pos
,
11492 isl_bool (*fn
)(__isl_keep isl_basic_map
*bmap
,
11493 enum isl_dim_type type
, unsigned pos
))
11498 return isl_bool_error
;
11500 for (i
= 0; i
< map
->n
; ++i
) {
11502 bounded
= fn(map
->p
[i
], type
, pos
);
11503 if (bounded
< 0 || bounded
)
11507 return isl_bool_false
;
11510 /* Return 1 if the specified dim is involved in any lower bound.
11512 isl_bool
isl_set_dim_has_any_lower_bound(__isl_keep isl_set
*set
,
11513 enum isl_dim_type type
, unsigned pos
)
11515 return has_any_bound(set
, type
, pos
,
11516 &isl_basic_map_dim_has_lower_bound
);
11519 /* Return 1 if the specified dim is involved in any upper bound.
11521 isl_bool
isl_set_dim_has_any_upper_bound(__isl_keep isl_set
*set
,
11522 enum isl_dim_type type
, unsigned pos
)
11524 return has_any_bound(set
, type
, pos
,
11525 &isl_basic_map_dim_has_upper_bound
);
11528 /* Does "map" have a bound (according to "fn") for all of its basic maps?
11530 static isl_bool
has_bound(__isl_keep isl_map
*map
,
11531 enum isl_dim_type type
, unsigned pos
,
11532 isl_bool (*fn
)(__isl_keep isl_basic_map
*bmap
,
11533 enum isl_dim_type type
, unsigned pos
))
11538 return isl_bool_error
;
11540 for (i
= 0; i
< map
->n
; ++i
) {
11542 bounded
= fn(map
->p
[i
], type
, pos
);
11543 if (bounded
< 0 || !bounded
)
11547 return isl_bool_true
;
11550 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
11552 isl_bool
isl_set_dim_has_lower_bound(__isl_keep isl_set
*set
,
11553 enum isl_dim_type type
, unsigned pos
)
11555 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_lower_bound
);
11558 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
11560 isl_bool
isl_set_dim_has_upper_bound(__isl_keep isl_set
*set
,
11561 enum isl_dim_type type
, unsigned pos
)
11563 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_upper_bound
);
11566 /* For each of the "n" variables starting at "first", determine
11567 * the sign of the variable and put the results in the first "n"
11568 * elements of the array "signs".
11570 * 1 means that the variable is non-negative
11571 * -1 means that the variable is non-positive
11572 * 0 means the variable attains both positive and negative values.
11574 isl_stat
isl_basic_set_vars_get_sign(__isl_keep isl_basic_set
*bset
,
11575 unsigned first
, unsigned n
, int *signs
)
11577 isl_vec
*bound
= NULL
;
11578 struct isl_tab
*tab
= NULL
;
11579 struct isl_tab_undo
*snap
;
11583 total
= isl_basic_set_dim(bset
, isl_dim_all
);
11584 if (total
< 0 || !signs
)
11585 return isl_stat_error
;
11587 bound
= isl_vec_alloc(bset
->ctx
, 1 + total
);
11588 tab
= isl_tab_from_basic_set(bset
, 0);
11589 if (!bound
|| !tab
)
11592 isl_seq_clr(bound
->el
, bound
->size
);
11593 isl_int_set_si(bound
->el
[0], -1);
11595 snap
= isl_tab_snap(tab
);
11596 for (i
= 0; i
< n
; ++i
) {
11599 isl_int_set_si(bound
->el
[1 + first
+ i
], -1);
11600 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
11602 empty
= tab
->empty
;
11603 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
11604 if (isl_tab_rollback(tab
, snap
) < 0)
11612 isl_int_set_si(bound
->el
[1 + first
+ i
], 1);
11613 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
11615 empty
= tab
->empty
;
11616 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
11617 if (isl_tab_rollback(tab
, snap
) < 0)
11620 signs
[i
] = empty
? -1 : 0;
11624 isl_vec_free(bound
);
11625 return isl_stat_ok
;
11628 isl_vec_free(bound
);
11629 return isl_stat_error
;
11632 isl_stat
isl_basic_set_dims_get_sign(__isl_keep isl_basic_set
*bset
,
11633 enum isl_dim_type type
, unsigned first
, unsigned n
, int *signs
)
11635 if (!bset
|| !signs
)
11636 return isl_stat_error
;
11637 if (isl_basic_set_check_range(bset
, type
, first
, n
) < 0)
11638 return isl_stat_error
;
11640 first
+= pos(bset
->dim
, type
) - 1;
11641 return isl_basic_set_vars_get_sign(bset
, first
, n
, signs
);
11644 /* Is it possible for the integer division "div" to depend (possibly
11645 * indirectly) on any output dimensions?
11647 * If the div is undefined, then we conservatively assume that it
11648 * may depend on them.
11649 * Otherwise, we check if it actually depends on them or on any integer
11650 * divisions that may depend on them.
11652 static isl_bool
div_may_involve_output(__isl_keep isl_basic_map
*bmap
, int div
)
11655 isl_size n_out
, n_div
;
11656 unsigned o_out
, o_div
;
11658 if (isl_int_is_zero(bmap
->div
[div
][0]))
11659 return isl_bool_true
;
11661 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
11663 return isl_bool_error
;
11664 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
11666 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + o_out
, n_out
) != -1)
11667 return isl_bool_true
;
11669 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
11671 return isl_bool_error
;
11672 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
11674 for (i
= 0; i
< n_div
; ++i
) {
11675 isl_bool may_involve
;
11677 if (isl_int_is_zero(bmap
->div
[div
][1 + o_div
+ i
]))
11679 may_involve
= div_may_involve_output(bmap
, i
);
11680 if (may_involve
< 0 || may_involve
)
11681 return may_involve
;
11684 return isl_bool_false
;
11687 /* Return the first integer division of "bmap" in the range
11688 * [first, first + n[ that may depend on any output dimensions and
11689 * that has a non-zero coefficient in "c" (where the first coefficient
11690 * in "c" corresponds to integer division "first").
11692 static int first_div_may_involve_output(__isl_keep isl_basic_map
*bmap
,
11693 isl_int
*c
, int first
, int n
)
11700 for (k
= first
; k
< first
+ n
; ++k
) {
11701 isl_bool may_involve
;
11703 if (isl_int_is_zero(c
[k
]))
11705 may_involve
= div_may_involve_output(bmap
, k
);
11706 if (may_involve
< 0)
11715 /* Look for a pair of inequality constraints in "bmap" of the form
11717 * -l + i >= 0 or i >= l
11719 * n + l - i >= 0 or i <= l + n
11721 * with n < "m" and i the output dimension at position "pos".
11722 * (Note that n >= 0 as otherwise the two constraints would conflict.)
11723 * Furthermore, "l" is only allowed to involve parameters, input dimensions
11724 * and earlier output dimensions, as well as integer divisions that do
11725 * not involve any of the output dimensions.
11727 * Return the index of the first inequality constraint or bmap->n_ineq
11728 * if no such pair can be found.
11730 static int find_modulo_constraint_pair(__isl_keep isl_basic_map
*bmap
,
11731 int pos
, isl_int m
)
11736 isl_size n_div
, n_out
;
11737 unsigned o_div
, o_out
;
11740 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
11741 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
11742 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
11743 if (total
< 0 || n_out
< 0 || n_div
< 0)
11746 ctx
= isl_basic_map_get_ctx(bmap
);
11747 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
11748 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
11749 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
11750 if (!isl_int_abs_eq(bmap
->ineq
[i
][o_out
+ pos
], ctx
->one
))
11752 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + o_out
+ pos
+ 1,
11753 n_out
- (pos
+ 1)) != -1)
11755 if (first_div_may_involve_output(bmap
, bmap
->ineq
[i
] + o_div
,
11758 for (j
= i
+ 1; j
< bmap
->n_ineq
; ++j
) {
11759 if (!isl_int_abs_eq(bmap
->ineq
[j
][o_out
+ pos
],
11762 if (!isl_seq_is_neg(bmap
->ineq
[i
] + 1,
11763 bmap
->ineq
[j
] + 1, total
))
11767 if (j
>= bmap
->n_ineq
)
11769 isl_int_add(bmap
->ineq
[i
][0],
11770 bmap
->ineq
[i
][0], bmap
->ineq
[j
][0]);
11771 less
= isl_int_abs_lt(bmap
->ineq
[i
][0], m
);
11772 isl_int_sub(bmap
->ineq
[i
][0],
11773 bmap
->ineq
[i
][0], bmap
->ineq
[j
][0]);
11776 if (isl_int_is_one(bmap
->ineq
[i
][o_out
+ pos
]))
11782 return bmap
->n_ineq
;
11785 /* Return the index of the equality of "bmap" that defines
11786 * the output dimension "pos" in terms of earlier dimensions.
11787 * The equality may also involve integer divisions, as long
11788 * as those integer divisions are defined in terms of
11789 * parameters or input dimensions.
11790 * In this case, *div is set to the number of integer divisions and
11791 * *ineq is set to the number of inequality constraints (provided
11792 * div and ineq are not NULL).
11794 * The equality may also involve a single integer division involving
11795 * the output dimensions (typically only output dimension "pos") as
11796 * long as the coefficient of output dimension "pos" is 1 or -1 and
11797 * there is a pair of constraints i >= l and i <= l + n, with i referring
11798 * to output dimension "pos", l an expression involving only earlier
11799 * dimensions and n smaller than the coefficient of the integer division
11800 * in the equality. In this case, the output dimension can be defined
11801 * in terms of a modulo expression that does not involve the integer division.
11802 * *div is then set to this single integer division and
11803 * *ineq is set to the index of constraint i >= l.
11805 * Return bmap->n_eq if there is no such equality.
11806 * Return -1 on error.
11808 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map
*bmap
,
11809 int pos
, int *div
, int *ineq
)
11812 isl_size n_div
, n_out
;
11813 unsigned o_div
, o_out
;
11815 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
11816 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
11817 if (n_out
< 0 || n_div
< 0)
11820 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
11821 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
11824 *ineq
= bmap
->n_ineq
;
11827 for (j
= 0; j
< bmap
->n_eq
; ++j
) {
11828 if (isl_int_is_zero(bmap
->eq
[j
][o_out
+ pos
]))
11830 if (isl_seq_first_non_zero(bmap
->eq
[j
] + o_out
+ pos
+ 1,
11831 n_out
- (pos
+ 1)) != -1)
11833 k
= first_div_may_involve_output(bmap
, bmap
->eq
[j
] + o_div
,
11837 if (!isl_int_is_one(bmap
->eq
[j
][o_out
+ pos
]) &&
11838 !isl_int_is_negone(bmap
->eq
[j
][o_out
+ pos
]))
11840 if (first_div_may_involve_output(bmap
, bmap
->eq
[j
] + o_div
,
11841 k
+ 1, n_div
- (k
+1)) < n_div
)
11843 l
= find_modulo_constraint_pair(bmap
, pos
,
11844 bmap
->eq
[j
][o_div
+ k
]);
11847 if (l
>= bmap
->n_ineq
)
11859 /* Check if the given basic map is obviously single-valued.
11860 * In particular, for each output dimension, check that there is
11861 * an equality that defines the output dimension in terms of
11862 * earlier dimensions.
11864 isl_bool
isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map
*bmap
)
11869 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
11871 return isl_bool_error
;
11873 for (i
= 0; i
< n_out
; ++i
) {
11876 eq
= isl_basic_map_output_defining_equality(bmap
, i
,
11879 return isl_bool_error
;
11880 if (eq
>= bmap
->n_eq
)
11881 return isl_bool_false
;
11884 return isl_bool_true
;
11887 /* Check if the given basic map is single-valued.
11888 * We simply compute
11892 * and check if the result is a subset of the identity mapping.
11894 isl_bool
isl_basic_map_is_single_valued(__isl_keep isl_basic_map
*bmap
)
11897 isl_basic_map
*test
;
11901 sv
= isl_basic_map_plain_is_single_valued(bmap
);
11905 test
= isl_basic_map_reverse(isl_basic_map_copy(bmap
));
11906 test
= isl_basic_map_apply_range(test
, isl_basic_map_copy(bmap
));
11908 space
= isl_basic_map_get_space(bmap
);
11909 space
= isl_space_map_from_set(isl_space_range(space
));
11910 id
= isl_basic_map_identity(space
);
11912 sv
= isl_basic_map_is_subset(test
, id
);
11914 isl_basic_map_free(test
);
11915 isl_basic_map_free(id
);
11920 /* Check if the given map is obviously single-valued.
11922 isl_bool
isl_map_plain_is_single_valued(__isl_keep isl_map
*map
)
11925 return isl_bool_error
;
11927 return isl_bool_true
;
11929 return isl_bool_false
;
11931 return isl_basic_map_plain_is_single_valued(map
->p
[0]);
11934 /* Check if the given map is single-valued.
11935 * We simply compute
11939 * and check if the result is a subset of the identity mapping.
11941 isl_bool
isl_map_is_single_valued(__isl_keep isl_map
*map
)
11948 sv
= isl_map_plain_is_single_valued(map
);
11952 test
= isl_map_reverse(isl_map_copy(map
));
11953 test
= isl_map_apply_range(test
, isl_map_copy(map
));
11955 space
= isl_space_map_from_set(isl_space_range(isl_map_get_space(map
)));
11956 id
= isl_map_identity(space
);
11958 sv
= isl_map_is_subset(test
, id
);
11960 isl_map_free(test
);
11966 isl_bool
isl_map_is_injective(__isl_keep isl_map
*map
)
11970 map
= isl_map_copy(map
);
11971 map
= isl_map_reverse(map
);
11972 in
= isl_map_is_single_valued(map
);
11978 /* Check if the given map is obviously injective.
11980 isl_bool
isl_map_plain_is_injective(__isl_keep isl_map
*map
)
11984 map
= isl_map_copy(map
);
11985 map
= isl_map_reverse(map
);
11986 in
= isl_map_plain_is_single_valued(map
);
11992 isl_bool
isl_map_is_bijective(__isl_keep isl_map
*map
)
11996 sv
= isl_map_is_single_valued(map
);
12000 return isl_map_is_injective(map
);
12003 isl_bool
isl_set_is_singleton(__isl_keep isl_set
*set
)
12005 return isl_map_is_single_valued(set_to_map(set
));
12008 /* Does "map" only map elements to themselves?
12010 * If the domain and range spaces are different, then "map"
12011 * is considered not to be an identity relation, even if it is empty.
12012 * Otherwise, construct the maximal identity relation and
12013 * check whether "map" is a subset of this relation.
12015 isl_bool
isl_map_is_identity(__isl_keep isl_map
*map
)
12018 isl_bool equal
, is_identity
;
12020 equal
= isl_map_tuple_is_equal(map
, isl_dim_in
, map
, isl_dim_out
);
12021 if (equal
< 0 || !equal
)
12024 id
= isl_map_identity(isl_map_get_space(map
));
12025 is_identity
= isl_map_is_subset(map
, id
);
12028 return is_identity
;
12031 int isl_map_is_translation(__isl_keep isl_map
*map
)
12036 delta
= isl_map_deltas(isl_map_copy(map
));
12037 ok
= isl_set_is_singleton(delta
);
12038 isl_set_free(delta
);
12043 static int unique(isl_int
*p
, unsigned pos
, unsigned len
)
12045 if (isl_seq_first_non_zero(p
, pos
) != -1)
12047 if (isl_seq_first_non_zero(p
+ pos
+ 1, len
- pos
- 1) != -1)
12052 isl_bool
isl_basic_set_is_box(__isl_keep isl_basic_set
*bset
)
12055 isl_size nvar
, n_div
;
12058 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
12060 return isl_bool_error
;
12062 return isl_bool_false
;
12064 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
12066 return isl_bool_error
;
12067 ovar
= isl_space_offset(bset
->dim
, isl_dim_set
);
12068 for (j
= 0; j
< nvar
; ++j
) {
12069 int lower
= 0, upper
= 0;
12070 for (i
= 0; i
< bset
->n_eq
; ++i
) {
12071 if (isl_int_is_zero(bset
->eq
[i
][1 + ovar
+ j
]))
12073 if (!unique(bset
->eq
[i
] + 1 + ovar
, j
, nvar
))
12074 return isl_bool_false
;
12077 if (i
< bset
->n_eq
)
12079 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
12080 if (isl_int_is_zero(bset
->ineq
[i
][1 + ovar
+ j
]))
12082 if (!unique(bset
->ineq
[i
] + 1 + ovar
, j
, nvar
))
12083 return isl_bool_false
;
12084 if (isl_int_is_pos(bset
->ineq
[i
][1 + ovar
+ j
]))
12089 if (!lower
|| !upper
)
12090 return isl_bool_false
;
12093 return isl_bool_true
;
12096 isl_bool
isl_set_is_box(__isl_keep isl_set
*set
)
12099 return isl_bool_error
;
12101 return isl_bool_false
;
12103 return isl_basic_set_is_box(set
->p
[0]);
12106 isl_bool
isl_basic_set_is_wrapping(__isl_keep isl_basic_set
*bset
)
12109 return isl_bool_error
;
12111 return isl_space_is_wrapping(bset
->dim
);
12114 isl_bool
isl_set_is_wrapping(__isl_keep isl_set
*set
)
12117 return isl_bool_error
;
12119 return isl_space_is_wrapping(set
->dim
);
12122 /* Modify the space of "map" through a call to "change".
12123 * If "can_change" is set (not NULL), then first call it to check
12124 * if the modification is allowed, printing the error message "cannot_change"
12127 static __isl_give isl_map
*isl_map_change_space(__isl_take isl_map
*map
,
12128 isl_bool (*can_change
)(__isl_keep isl_map
*map
),
12129 const char *cannot_change
,
12130 __isl_give isl_space
*(*change
)(__isl_take isl_space
*space
))
12138 ok
= can_change
? can_change(map
) : isl_bool_true
;
12140 return isl_map_free(map
);
12142 isl_die(isl_map_get_ctx(map
), isl_error_invalid
, cannot_change
,
12143 return isl_map_free(map
));
12145 space
= change(isl_map_get_space(map
));
12146 map
= isl_map_reset_space(map
, space
);
12151 /* Is the domain of "map" a wrapped relation?
12153 isl_bool
isl_map_domain_is_wrapping(__isl_keep isl_map
*map
)
12156 return isl_bool_error
;
12158 return isl_space_domain_is_wrapping(map
->dim
);
12161 /* Does "map" have a wrapped relation in both domain and range?
12163 isl_bool
isl_map_is_product(__isl_keep isl_map
*map
)
12165 return isl_space_is_product(isl_map_peek_space(map
));
12168 /* Is the range of "map" a wrapped relation?
12170 isl_bool
isl_map_range_is_wrapping(__isl_keep isl_map
*map
)
12173 return isl_bool_error
;
12175 return isl_space_range_is_wrapping(map
->dim
);
12178 __isl_give isl_basic_set
*isl_basic_map_wrap(__isl_take isl_basic_map
*bmap
)
12182 space
= isl_basic_map_take_space(bmap
);
12183 space
= isl_space_wrap(space
);
12184 bmap
= isl_basic_map_restore_space(bmap
, space
);
12186 bmap
= isl_basic_map_finalize(bmap
);
12188 return bset_from_bmap(bmap
);
12191 /* Given a map A -> B, return the set (A -> B).
12193 __isl_give isl_set
*isl_map_wrap(__isl_take isl_map
*map
)
12195 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_wrap
);
12198 __isl_give isl_basic_map
*isl_basic_set_unwrap(__isl_take isl_basic_set
*bset
)
12200 bset
= isl_basic_set_cow(bset
);
12204 bset
->dim
= isl_space_unwrap(bset
->dim
);
12208 bset
= isl_basic_set_finalize(bset
);
12210 return bset_to_bmap(bset
);
12212 isl_basic_set_free(bset
);
12216 /* Given a set (A -> B), return the map A -> B.
12217 * Error out if "set" is not of the form (A -> B).
12219 __isl_give isl_map
*isl_set_unwrap(__isl_take isl_set
*set
)
12221 return isl_map_change_space(set
, &isl_set_is_wrapping
,
12222 "not a wrapping set", &isl_space_unwrap
);
12225 __isl_give isl_basic_map
*isl_basic_map_reset(__isl_take isl_basic_map
*bmap
,
12226 enum isl_dim_type type
)
12230 space
= isl_basic_map_take_space(bmap
);
12231 space
= isl_space_reset(space
, type
);
12232 bmap
= isl_basic_map_restore_space(bmap
, space
);
12234 bmap
= isl_basic_map_mark_final(bmap
);
12239 __isl_give isl_map
*isl_map_reset(__isl_take isl_map
*map
,
12240 enum isl_dim_type type
)
12248 if (!isl_space_is_named_or_nested(map
->dim
, type
))
12251 map
= isl_map_cow(map
);
12255 for (i
= 0; i
< map
->n
; ++i
) {
12256 map
->p
[i
] = isl_basic_map_reset(map
->p
[i
], type
);
12261 space
= isl_map_take_space(map
);
12262 space
= isl_space_reset(space
, type
);
12263 map
= isl_map_restore_space(map
, space
);
12271 __isl_give isl_basic_map
*isl_basic_map_flatten(__isl_take isl_basic_map
*bmap
)
12275 space
= isl_basic_map_take_space(bmap
);
12276 space
= isl_space_flatten(space
);
12277 bmap
= isl_basic_map_restore_space(bmap
, space
);
12279 bmap
= isl_basic_map_mark_final(bmap
);
12284 __isl_give isl_basic_set
*isl_basic_set_flatten(__isl_take isl_basic_set
*bset
)
12286 return bset_from_bmap(isl_basic_map_flatten(bset_to_bmap(bset
)));
12289 __isl_give isl_basic_map
*isl_basic_map_flatten_domain(
12290 __isl_take isl_basic_map
*bmap
)
12294 space
= isl_basic_map_take_space(bmap
);
12295 space
= isl_space_flatten_domain(space
);
12296 bmap
= isl_basic_map_restore_space(bmap
, space
);
12298 bmap
= isl_basic_map_mark_final(bmap
);
12303 __isl_give isl_basic_map
*isl_basic_map_flatten_range(
12304 __isl_take isl_basic_map
*bmap
)
12308 space
= isl_basic_map_take_space(bmap
);
12309 space
= isl_space_flatten_range(space
);
12310 bmap
= isl_basic_map_restore_space(bmap
, space
);
12312 bmap
= isl_basic_map_mark_final(bmap
);
12317 /* Remove any internal structure from the spaces of domain and range of "map".
12319 __isl_give isl_map
*isl_map_flatten(__isl_take isl_map
*map
)
12324 if (!map
->dim
->nested
[0] && !map
->dim
->nested
[1])
12327 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten
);
12330 __isl_give isl_set
*isl_set_flatten(__isl_take isl_set
*set
)
12332 return set_from_map(isl_map_flatten(set_to_map(set
)));
12335 __isl_give isl_map
*isl_set_flatten_map(__isl_take isl_set
*set
)
12337 isl_space
*space
, *flat_space
;
12340 space
= isl_set_get_space(set
);
12341 flat_space
= isl_space_flatten(isl_space_copy(space
));
12342 map
= isl_map_identity(isl_space_join(isl_space_reverse(space
),
12344 map
= isl_map_intersect_domain(map
, set
);
12349 /* Remove any internal structure from the space of the domain of "map".
12351 __isl_give isl_map
*isl_map_flatten_domain(__isl_take isl_map
*map
)
12356 if (!map
->dim
->nested
[0])
12359 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten_domain
);
12362 /* Remove any internal structure from the space of the range of "map".
12364 __isl_give isl_map
*isl_map_flatten_range(__isl_take isl_map
*map
)
12369 if (!map
->dim
->nested
[1])
12372 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten_range
);
12375 /* Reorder the dimensions of "bmap" according to the given dim_map
12376 * and set the dimension specification to "space" and
12377 * perform Gaussian elimination on the result.
12379 __isl_give isl_basic_map
*isl_basic_map_realign(__isl_take isl_basic_map
*bmap
,
12380 __isl_take isl_space
*space
, __isl_take
struct isl_dim_map
*dim_map
)
12382 isl_basic_map
*res
;
12386 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
12387 if (n_div
< 0 || !space
|| !dim_map
)
12390 flags
= bmap
->flags
;
12391 ISL_FL_CLR(flags
, ISL_BASIC_MAP_FINAL
);
12392 ISL_FL_CLR(flags
, ISL_BASIC_MAP_SORTED
);
12393 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
12394 res
= isl_basic_map_alloc_space(space
, n_div
, bmap
->n_eq
, bmap
->n_ineq
);
12395 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
12397 res
->flags
= flags
;
12398 res
= isl_basic_map_gauss(res
, NULL
);
12399 res
= isl_basic_map_finalize(res
);
12402 isl_dim_map_free(dim_map
);
12403 isl_basic_map_free(bmap
);
12404 isl_space_free(space
);
12408 /* Reorder the dimensions of "map" according to given reordering.
12410 __isl_give isl_map
*isl_map_realign(__isl_take isl_map
*map
,
12411 __isl_take isl_reordering
*r
)
12414 struct isl_dim_map
*dim_map
;
12416 map
= isl_map_cow(map
);
12417 dim_map
= isl_dim_map_from_reordering(r
);
12418 if (!map
|| !r
|| !dim_map
)
12421 for (i
= 0; i
< map
->n
; ++i
) {
12422 struct isl_dim_map
*dim_map_i
;
12425 dim_map_i
= isl_dim_map_extend(dim_map
, map
->p
[i
]);
12427 space
= isl_reordering_get_space(r
);
12428 map
->p
[i
] = isl_basic_map_realign(map
->p
[i
], space
, dim_map_i
);
12434 map
= isl_map_reset_space(map
, isl_reordering_get_space(r
));
12435 map
= isl_map_unmark_normalized(map
);
12437 isl_reordering_free(r
);
12438 isl_dim_map_free(dim_map
);
12441 isl_dim_map_free(dim_map
);
12443 isl_reordering_free(r
);
12447 __isl_give isl_set
*isl_set_realign(__isl_take isl_set
*set
,
12448 __isl_take isl_reordering
*r
)
12450 return set_from_map(isl_map_realign(set_to_map(set
), r
));
12453 __isl_give isl_map
*isl_map_align_params(__isl_take isl_map
*map
,
12454 __isl_take isl_space
*model
)
12459 if (!map
|| !model
)
12462 ctx
= isl_space_get_ctx(model
);
12463 if (!isl_space_has_named_params(model
))
12464 isl_die(ctx
, isl_error_invalid
,
12465 "model has unnamed parameters", goto error
);
12466 if (isl_map_check_named_params(map
) < 0)
12468 aligned
= isl_map_space_has_equal_params(map
, model
);
12472 isl_reordering
*exp
;
12474 exp
= isl_parameter_alignment_reordering(map
->dim
, model
);
12475 exp
= isl_reordering_extend_space(exp
, isl_map_get_space(map
));
12476 map
= isl_map_realign(map
, exp
);
12479 isl_space_free(model
);
12482 isl_space_free(model
);
12487 __isl_give isl_set
*isl_set_align_params(__isl_take isl_set
*set
,
12488 __isl_take isl_space
*model
)
12490 return isl_map_align_params(set
, model
);
12493 /* Align the parameters of "bmap" to those of "model", introducing
12494 * additional parameters if needed.
12496 __isl_give isl_basic_map
*isl_basic_map_align_params(
12497 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*model
)
12500 isl_bool equal_params
;
12502 if (!bmap
|| !model
)
12505 ctx
= isl_space_get_ctx(model
);
12506 if (!isl_space_has_named_params(model
))
12507 isl_die(ctx
, isl_error_invalid
,
12508 "model has unnamed parameters", goto error
);
12509 if (isl_basic_map_check_named_params(bmap
) < 0)
12511 equal_params
= isl_space_has_equal_params(bmap
->dim
, model
);
12512 if (equal_params
< 0)
12514 if (!equal_params
) {
12515 isl_reordering
*exp
;
12516 struct isl_dim_map
*dim_map
;
12518 exp
= isl_parameter_alignment_reordering(bmap
->dim
, model
);
12519 exp
= isl_reordering_extend_space(exp
,
12520 isl_basic_map_get_space(bmap
));
12521 dim_map
= isl_dim_map_from_reordering(exp
);
12522 bmap
= isl_basic_map_realign(bmap
,
12523 isl_reordering_get_space(exp
),
12524 isl_dim_map_extend(dim_map
, bmap
));
12525 isl_reordering_free(exp
);
12526 isl_dim_map_free(dim_map
);
12529 isl_space_free(model
);
12532 isl_space_free(model
);
12533 isl_basic_map_free(bmap
);
12537 /* Do "bset" and "space" have the same parameters?
12539 isl_bool
isl_basic_set_space_has_equal_params(__isl_keep isl_basic_set
*bset
,
12540 __isl_keep isl_space
*space
)
12542 isl_space
*bset_space
;
12544 bset_space
= isl_basic_set_peek_space(bset
);
12545 return isl_space_has_equal_params(bset_space
, space
);
12548 /* Do "map" and "space" have the same parameters?
12550 isl_bool
isl_map_space_has_equal_params(__isl_keep isl_map
*map
,
12551 __isl_keep isl_space
*space
)
12553 isl_space
*map_space
;
12555 map_space
= isl_map_peek_space(map
);
12556 return isl_space_has_equal_params(map_space
, space
);
12559 /* Do "set" and "space" have the same parameters?
12561 isl_bool
isl_set_space_has_equal_params(__isl_keep isl_set
*set
,
12562 __isl_keep isl_space
*space
)
12564 return isl_map_space_has_equal_params(set_to_map(set
), space
);
12567 /* Align the parameters of "bset" to those of "model", introducing
12568 * additional parameters if needed.
12570 __isl_give isl_basic_set
*isl_basic_set_align_params(
12571 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*model
)
12573 return isl_basic_map_align_params(bset
, model
);
12576 /* Drop all parameters not referenced by "map".
12578 __isl_give isl_map
*isl_map_drop_unused_params(__isl_take isl_map
*map
)
12583 n
= isl_map_dim(map
, isl_dim_param
);
12584 if (isl_map_check_named_params(map
) < 0 || n
< 0)
12585 return isl_map_free(map
);
12587 for (i
= n
- 1; i
>= 0; i
--) {
12590 involves
= isl_map_involves_dims(map
, isl_dim_param
, i
, 1);
12592 return isl_map_free(map
);
12594 map
= isl_map_project_out(map
, isl_dim_param
, i
, 1);
12600 /* Drop all parameters not referenced by "set".
12602 __isl_give isl_set
*isl_set_drop_unused_params(
12603 __isl_take isl_set
*set
)
12605 return set_from_map(isl_map_drop_unused_params(set_to_map(set
)));
12608 /* Drop all parameters not referenced by "bmap".
12610 __isl_give isl_basic_map
*isl_basic_map_drop_unused_params(
12611 __isl_take isl_basic_map
*bmap
)
12616 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
12617 if (nparam
< 0 || isl_basic_map_check_named_params(bmap
) < 0)
12618 return isl_basic_map_free(bmap
);
12620 for (i
= nparam
- 1; i
>= 0; i
--) {
12623 involves
= isl_basic_map_involves_dims(bmap
,
12624 isl_dim_param
, i
, 1);
12626 return isl_basic_map_free(bmap
);
12628 bmap
= isl_basic_map_drop(bmap
, isl_dim_param
, i
, 1);
12634 /* Drop all parameters not referenced by "bset".
12636 __isl_give isl_basic_set
*isl_basic_set_drop_unused_params(
12637 __isl_take isl_basic_set
*bset
)
12639 return bset_from_bmap(isl_basic_map_drop_unused_params(
12640 bset_to_bmap(bset
)));
12643 /* Given a tuple of identifiers "tuple" in a space that corresponds
12644 * to that of "set", if any of those identifiers appear as parameters
12645 * in "set", then equate those parameters with the corresponding
12646 * set dimensions and project out the parameters.
12647 * The result therefore has no such parameters.
12649 static __isl_give isl_set
*equate_params(__isl_take isl_set
*set
,
12650 __isl_keep isl_multi_id
*tuple
)
12654 isl_space
*set_space
, *tuple_space
;
12656 set_space
= isl_set_peek_space(set
);
12657 tuple_space
= isl_multi_id_peek_space(tuple
);
12658 if (isl_space_check_equal_tuples(tuple_space
, set_space
) < 0)
12659 return isl_set_free(set
);
12660 n
= isl_multi_id_size(tuple
);
12662 return isl_set_free(set
);
12663 for (i
= 0; i
< n
; ++i
) {
12667 id
= isl_multi_id_get_at(tuple
, i
);
12669 return isl_set_free(set
);
12670 pos
= isl_set_find_dim_by_id(set
, isl_dim_param
, id
);
12674 set
= isl_set_equate(set
, isl_dim_param
, pos
, isl_dim_set
, i
);
12675 set
= isl_set_project_out(set
, isl_dim_param
, pos
, 1);
12680 /* Bind the set dimensions of "set" to parameters with identifiers
12681 * specified by "tuple", living in the same space as "set".
12683 * If no parameters with these identifiers appear in "set" already,
12684 * then the set dimensions are simply reinterpreted as parameters.
12685 * Otherwise, the parameters are first equated to the corresponding
12688 __isl_give isl_set
*isl_set_bind(__isl_take isl_set
*set
,
12689 __isl_take isl_multi_id
*tuple
)
12693 set
= equate_params(set
, tuple
);
12694 space
= isl_set_get_space(set
);
12695 space
= isl_space_bind_set(space
, tuple
);
12696 isl_multi_id_free(tuple
);
12697 set
= isl_set_reset_space(set
, space
);
12702 /* Given a tuple of identifiers "tuple" in a space that corresponds
12703 * to the domain of "map", if any of those identifiers appear as parameters
12704 * in "map", then equate those parameters with the corresponding
12705 * input dimensions and project out the parameters.
12706 * The result therefore has no such parameters.
12708 static __isl_give isl_map
*map_equate_params(__isl_take isl_map
*map
,
12709 __isl_keep isl_multi_id
*tuple
)
12713 isl_space
*map_space
, *tuple_space
;
12715 map_space
= isl_map_peek_space(map
);
12716 tuple_space
= isl_multi_id_peek_space(tuple
);
12717 if (isl_space_check_domain_tuples(tuple_space
, map_space
) < 0)
12718 return isl_map_free(map
);
12719 n
= isl_multi_id_size(tuple
);
12721 return isl_map_free(map
);
12722 for (i
= 0; i
< n
; ++i
) {
12726 id
= isl_multi_id_get_at(tuple
, i
);
12728 return isl_map_free(map
);
12729 pos
= isl_map_find_dim_by_id(map
, isl_dim_param
, id
);
12733 map
= isl_map_equate(map
, isl_dim_param
, pos
, isl_dim_in
, i
);
12734 map
= isl_map_project_out(map
, isl_dim_param
, pos
, 1);
12739 /* Bind the input dimensions of "map" to parameters with identifiers
12740 * specified by "tuple", living in the domain space of "map".
12742 * If no parameters with these identifiers appear in "map" already,
12743 * then the input dimensions are simply reinterpreted as parameters.
12744 * Otherwise, the parameters are first equated to the corresponding
12745 * input dimensions.
12747 __isl_give isl_set
*isl_map_bind_domain(__isl_take isl_map
*map
,
12748 __isl_take isl_multi_id
*tuple
)
12753 map
= map_equate_params(map
, tuple
);
12754 space
= isl_map_get_space(map
);
12755 space
= isl_space_bind_map_domain(space
, tuple
);
12756 isl_multi_id_free(tuple
);
12757 set
= set_from_map(isl_map_reset_space(map
, space
));
12762 /* Bind the output dimensions of "map" to parameters with identifiers
12763 * specified by "tuple", living in the range space of "map".
12765 * Since binding is more easily implemented on the domain,
12766 * bind the input dimensions of the inverse of "map".
12768 __isl_give isl_set
*isl_map_bind_range(__isl_take isl_map
*map
,
12769 __isl_take isl_multi_id
*tuple
)
12771 return isl_map_bind_domain(isl_map_reverse(map
), tuple
);
12774 /* Insert a domain corresponding to "tuple"
12775 * into the nullary or unary relation "set".
12776 * The result has an extra initial tuple and is therefore
12777 * either a unary or binary relation.
12778 * Any parameters with identifiers in "tuple" are reinterpreted
12779 * as the corresponding domain dimensions.
12781 static __isl_give isl_map
*unbind_params_insert_domain(
12782 __isl_take isl_set
*set
, __isl_take isl_multi_id
*tuple
)
12787 space
= isl_set_peek_space(set
);
12788 r
= isl_reordering_unbind_params_insert_domain(space
, tuple
);
12789 isl_multi_id_free(tuple
);
12791 return isl_map_realign(set_to_map(set
), r
);
12794 /* Construct a set with "tuple" as domain from the parameter domain "set".
12795 * Any parameters with identifiers in "tuple" are reinterpreted
12796 * as the corresponding set dimensions.
12798 __isl_give isl_set
*isl_set_unbind_params(__isl_take isl_set
*set
,
12799 __isl_take isl_multi_id
*tuple
)
12801 isl_bool is_params
;
12803 is_params
= isl_set_is_params(set
);
12805 set
= isl_set_free(set
);
12806 else if (!is_params
)
12807 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
12808 "expecting parameter domain", set
= isl_set_free(set
));
12809 return set_from_map(unbind_params_insert_domain(set
, tuple
));
12812 /* Check that "set" is a proper set, i.e., that it is not a parameter domain.
12814 static isl_stat
isl_set_check_is_set(__isl_keep isl_set
*set
)
12816 isl_bool is_params
;
12818 is_params
= isl_set_is_params(set
);
12820 return isl_stat_error
;
12821 else if (is_params
)
12822 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
12823 "expecting proper set", return isl_stat_error
);
12825 return isl_stat_ok
;
12828 /* Construct a map with "domain" as domain and "set" as range.
12829 * Any parameters with identifiers in "domain" are reinterpreted
12830 * as the corresponding domain dimensions.
12832 __isl_give isl_map
*isl_set_unbind_params_insert_domain(
12833 __isl_take isl_set
*set
, __isl_take isl_multi_id
*domain
)
12835 if (isl_set_check_is_set(set
) < 0)
12836 set
= isl_set_free(set
);
12837 return unbind_params_insert_domain(set
, domain
);
12840 /* Construct a map with "domain" as domain and "set" as range.
12842 __isl_give isl_map
*isl_set_insert_domain(__isl_take isl_set
*set
,
12843 __isl_take isl_space
*domain
)
12849 if (isl_set_check_is_set(set
) < 0 || isl_space_check_is_set(domain
) < 0)
12850 domain
= isl_space_free(domain
);
12851 dim
= isl_space_dim(domain
, isl_dim_set
);
12853 domain
= isl_space_free(domain
);
12854 space
= isl_set_get_space(set
);
12855 domain
= isl_space_replace_params(domain
, space
);
12856 space
= isl_space_map_from_domain_and_range(domain
, space
);
12858 map
= isl_map_from_range(set
);
12859 map
= isl_map_add_dims(map
, isl_dim_in
, dim
);
12860 map
= isl_map_reset_space(map
, space
);
12865 __isl_give isl_mat
*isl_basic_map_equalities_matrix(
12866 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
12867 enum isl_dim_type c2
, enum isl_dim_type c3
,
12868 enum isl_dim_type c4
, enum isl_dim_type c5
)
12870 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
12871 struct isl_mat
*mat
;
12876 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
12879 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_eq
, total
+ 1);
12882 for (i
= 0; i
< bmap
->n_eq
; ++i
)
12883 for (j
= 0, pos
= 0; j
< 5; ++j
) {
12884 int off
= isl_basic_map_offset(bmap
, c
[j
]);
12885 isl_size dim
= isl_basic_map_dim(bmap
, c
[j
]);
12887 return isl_mat_free(mat
);
12888 for (k
= 0; k
< dim
; ++k
) {
12889 isl_int_set(mat
->row
[i
][pos
],
12890 bmap
->eq
[i
][off
+ k
]);
12898 __isl_give isl_mat
*isl_basic_map_inequalities_matrix(
12899 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
12900 enum isl_dim_type c2
, enum isl_dim_type c3
,
12901 enum isl_dim_type c4
, enum isl_dim_type c5
)
12903 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
12904 struct isl_mat
*mat
;
12909 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
12912 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_ineq
, total
+ 1);
12915 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
12916 for (j
= 0, pos
= 0; j
< 5; ++j
) {
12917 int off
= isl_basic_map_offset(bmap
, c
[j
]);
12918 isl_size dim
= isl_basic_map_dim(bmap
, c
[j
]);
12920 return isl_mat_free(mat
);
12921 for (k
= 0; k
< dim
; ++k
) {
12922 isl_int_set(mat
->row
[i
][pos
],
12923 bmap
->ineq
[i
][off
+ k
]);
12931 __isl_give isl_basic_map
*isl_basic_map_from_constraint_matrices(
12932 __isl_take isl_space
*space
,
12933 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
12934 enum isl_dim_type c2
, enum isl_dim_type c3
,
12935 enum isl_dim_type c4
, enum isl_dim_type c5
)
12937 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
12938 isl_basic_map
*bmap
= NULL
;
12945 dim
= isl_space_dim(space
, isl_dim_all
);
12946 if (dim
< 0 || !eq
|| !ineq
)
12949 if (eq
->n_col
!= ineq
->n_col
)
12950 isl_die(space
->ctx
, isl_error_invalid
,
12951 "equalities and inequalities matrices should have "
12952 "same number of columns", goto error
);
12956 if (eq
->n_col
< total
)
12957 isl_die(space
->ctx
, isl_error_invalid
,
12958 "number of columns too small", goto error
);
12960 extra
= eq
->n_col
- total
;
12962 bmap
= isl_basic_map_alloc_space(isl_space_copy(space
), extra
,
12963 eq
->n_row
, ineq
->n_row
);
12966 for (i
= 0; i
< extra
; ++i
) {
12967 k
= isl_basic_map_alloc_div(bmap
);
12970 isl_int_set_si(bmap
->div
[k
][0], 0);
12972 for (i
= 0; i
< eq
->n_row
; ++i
) {
12973 l
= isl_basic_map_alloc_equality(bmap
);
12976 for (j
= 0, pos
= 0; j
< 5; ++j
) {
12977 int off
= isl_basic_map_offset(bmap
, c
[j
]);
12978 isl_size dim
= isl_basic_map_dim(bmap
, c
[j
]);
12981 for (k
= 0; k
< dim
; ++k
) {
12982 isl_int_set(bmap
->eq
[l
][off
+ k
],
12988 for (i
= 0; i
< ineq
->n_row
; ++i
) {
12989 l
= isl_basic_map_alloc_inequality(bmap
);
12992 for (j
= 0, pos
= 0; j
< 5; ++j
) {
12993 int off
= isl_basic_map_offset(bmap
, c
[j
]);
12994 isl_size dim
= isl_basic_map_dim(bmap
, c
[j
]);
12997 for (k
= 0; k
< dim
; ++k
) {
12998 isl_int_set(bmap
->ineq
[l
][off
+ k
],
12999 ineq
->row
[i
][pos
]);
13005 isl_space_free(space
);
13007 isl_mat_free(ineq
);
13009 bmap
= isl_basic_map_simplify(bmap
);
13010 return isl_basic_map_finalize(bmap
);
13012 isl_space_free(space
);
13014 isl_mat_free(ineq
);
13015 isl_basic_map_free(bmap
);
13019 __isl_give isl_mat
*isl_basic_set_equalities_matrix(
13020 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
13021 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
13023 return isl_basic_map_equalities_matrix(bset_to_bmap(bset
),
13024 c1
, c2
, c3
, c4
, isl_dim_in
);
13027 __isl_give isl_mat
*isl_basic_set_inequalities_matrix(
13028 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
13029 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
13031 return isl_basic_map_inequalities_matrix(bset_to_bmap(bset
),
13032 c1
, c2
, c3
, c4
, isl_dim_in
);
13035 __isl_give isl_basic_set
*isl_basic_set_from_constraint_matrices(
13036 __isl_take isl_space
*space
,
13037 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
13038 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
13040 isl_basic_map
*bmap
;
13041 bmap
= isl_basic_map_from_constraint_matrices(space
, eq
, ineq
,
13042 c1
, c2
, c3
, c4
, isl_dim_in
);
13043 return bset_from_bmap(bmap
);
13046 isl_bool
isl_basic_map_can_zip(__isl_keep isl_basic_map
*bmap
)
13049 return isl_bool_error
;
13051 return isl_space_can_zip(bmap
->dim
);
13054 isl_bool
isl_map_can_zip(__isl_keep isl_map
*map
)
13057 return isl_bool_error
;
13059 return isl_space_can_zip(map
->dim
);
13062 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
13063 * (A -> C) -> (B -> D).
13065 __isl_give isl_basic_map
*isl_basic_map_zip(__isl_take isl_basic_map
*bmap
)
13075 if (!isl_basic_map_can_zip(bmap
))
13076 isl_die(bmap
->ctx
, isl_error_invalid
,
13077 "basic map cannot be zipped", goto error
);
13078 n_in
= isl_space_dim(bmap
->dim
->nested
[0], isl_dim_in
);
13079 n1
= isl_space_dim(bmap
->dim
->nested
[0], isl_dim_out
);
13080 n2
= isl_space_dim(bmap
->dim
->nested
[1], isl_dim_in
);
13081 if (n_in
< 0 || n1
< 0 || n2
< 0)
13082 return isl_basic_map_free(bmap
);
13083 pos
= isl_basic_map_offset(bmap
, isl_dim_in
) + n_in
;
13084 bmap
= isl_basic_map_cow(bmap
);
13085 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
13088 bmap
->dim
= isl_space_zip(bmap
->dim
);
13091 bmap
= isl_basic_map_mark_final(bmap
);
13094 isl_basic_map_free(bmap
);
13098 /* Given a map (A -> B) -> (C -> D), return the corresponding map
13099 * (A -> C) -> (B -> D).
13101 __isl_give isl_map
*isl_map_zip(__isl_take isl_map
*map
)
13106 if (!isl_map_can_zip(map
))
13107 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be zipped",
13110 return isl_map_transform(map
, &isl_space_zip
, &isl_basic_map_zip
);
13116 /* Can we apply isl_basic_map_curry to "bmap"?
13117 * That is, does it have a nested relation in its domain?
13119 isl_bool
isl_basic_map_can_curry(__isl_keep isl_basic_map
*bmap
)
13122 return isl_bool_error
;
13124 return isl_space_can_curry(bmap
->dim
);
13127 /* Can we apply isl_map_curry to "map"?
13128 * That is, does it have a nested relation in its domain?
13130 isl_bool
isl_map_can_curry(__isl_keep isl_map
*map
)
13133 return isl_bool_error
;
13135 return isl_space_can_curry(map
->dim
);
13138 /* Given a basic map (A -> B) -> C, return the corresponding basic map
13141 __isl_give isl_basic_map
*isl_basic_map_curry(__isl_take isl_basic_map
*bmap
)
13147 if (!isl_basic_map_can_curry(bmap
))
13148 isl_die(bmap
->ctx
, isl_error_invalid
,
13149 "basic map cannot be curried", goto error
);
13150 bmap
= isl_basic_map_cow(bmap
);
13153 bmap
->dim
= isl_space_curry(bmap
->dim
);
13156 bmap
= isl_basic_map_mark_final(bmap
);
13159 isl_basic_map_free(bmap
);
13163 /* Given a map (A -> B) -> C, return the corresponding map
13166 __isl_give isl_map
*isl_map_curry(__isl_take isl_map
*map
)
13168 return isl_map_change_space(map
, &isl_map_can_curry
,
13169 "map cannot be curried", &isl_space_curry
);
13172 /* Can isl_map_range_curry be applied to "map"?
13173 * That is, does it have a nested relation in its range,
13174 * the domain of which is itself a nested relation?
13176 isl_bool
isl_map_can_range_curry(__isl_keep isl_map
*map
)
13179 return isl_bool_error
;
13181 return isl_space_can_range_curry(map
->dim
);
13184 /* Given a map A -> ((B -> C) -> D), return the corresponding map
13185 * A -> (B -> (C -> D)).
13187 __isl_give isl_map
*isl_map_range_curry(__isl_take isl_map
*map
)
13189 return isl_map_change_space(map
, &isl_map_can_range_curry
,
13190 "map range cannot be curried",
13191 &isl_space_range_curry
);
13194 /* Can we apply isl_basic_map_uncurry to "bmap"?
13195 * That is, does it have a nested relation in its domain?
13197 isl_bool
isl_basic_map_can_uncurry(__isl_keep isl_basic_map
*bmap
)
13200 return isl_bool_error
;
13202 return isl_space_can_uncurry(bmap
->dim
);
13205 /* Can we apply isl_map_uncurry to "map"?
13206 * That is, does it have a nested relation in its domain?
13208 isl_bool
isl_map_can_uncurry(__isl_keep isl_map
*map
)
13211 return isl_bool_error
;
13213 return isl_space_can_uncurry(map
->dim
);
13216 /* Given a basic map A -> (B -> C), return the corresponding basic map
13219 __isl_give isl_basic_map
*isl_basic_map_uncurry(__isl_take isl_basic_map
*bmap
)
13225 if (!isl_basic_map_can_uncurry(bmap
))
13226 isl_die(bmap
->ctx
, isl_error_invalid
,
13227 "basic map cannot be uncurried",
13228 return isl_basic_map_free(bmap
));
13229 bmap
= isl_basic_map_cow(bmap
);
13232 bmap
->dim
= isl_space_uncurry(bmap
->dim
);
13234 return isl_basic_map_free(bmap
);
13235 bmap
= isl_basic_map_mark_final(bmap
);
13239 /* Given a map A -> (B -> C), return the corresponding map
13242 __isl_give isl_map
*isl_map_uncurry(__isl_take isl_map
*map
)
13244 return isl_map_change_space(map
, &isl_map_can_uncurry
,
13245 "map cannot be uncurried", &isl_space_uncurry
);
13248 __isl_give isl_set
*isl_set_equate(__isl_take isl_set
*set
,
13249 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13251 return isl_map_equate(set
, type1
, pos1
, type2
, pos2
);
13254 /* Construct a basic map where the given dimensions are equal to each other.
13256 static __isl_give isl_basic_map
*equator(__isl_take isl_space
*space
,
13257 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13259 isl_basic_map
*bmap
= NULL
;
13263 total
= isl_space_dim(space
, isl_dim_all
);
13265 isl_space_check_range(space
, type1
, pos1
, 1) < 0 ||
13266 isl_space_check_range(space
, type2
, pos2
, 1) < 0)
13269 if (type1
== type2
&& pos1
== pos2
)
13270 return isl_basic_map_universe(space
);
13272 bmap
= isl_basic_map_alloc_space(isl_space_copy(space
), 0, 1, 0);
13273 i
= isl_basic_map_alloc_equality(bmap
);
13276 isl_seq_clr(bmap
->eq
[i
], 1 + total
);
13277 pos1
+= isl_basic_map_offset(bmap
, type1
);
13278 pos2
+= isl_basic_map_offset(bmap
, type2
);
13279 isl_int_set_si(bmap
->eq
[i
][pos1
], -1);
13280 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
13281 bmap
= isl_basic_map_finalize(bmap
);
13282 isl_space_free(space
);
13285 isl_space_free(space
);
13286 isl_basic_map_free(bmap
);
13290 /* Add a constraint imposing that the given two dimensions are equal.
13292 __isl_give isl_basic_map
*isl_basic_map_equate(__isl_take isl_basic_map
*bmap
,
13293 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13297 eq
= equator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
13299 bmap
= isl_basic_map_intersect(bmap
, eq
);
13304 /* Add a constraint imposing that the given two dimensions are equal.
13306 __isl_give isl_map
*isl_map_equate(__isl_take isl_map
*map
,
13307 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13309 isl_basic_map
*bmap
;
13311 bmap
= equator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
13313 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
13318 /* Add a constraint imposing that the given two dimensions have opposite values.
13320 __isl_give isl_map
*isl_map_oppose(__isl_take isl_map
*map
,
13321 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13323 isl_basic_map
*bmap
= NULL
;
13327 if (isl_map_check_range(map
, type1
, pos1
, 1) < 0)
13328 return isl_map_free(map
);
13329 if (isl_map_check_range(map
, type2
, pos2
, 1) < 0)
13330 return isl_map_free(map
);
13332 total
= isl_map_dim(map
, isl_dim_all
);
13334 return isl_map_free(map
);
13335 bmap
= isl_basic_map_alloc_space(isl_map_get_space(map
), 0, 1, 0);
13336 i
= isl_basic_map_alloc_equality(bmap
);
13339 isl_seq_clr(bmap
->eq
[i
], 1 + total
);
13340 pos1
+= isl_basic_map_offset(bmap
, type1
);
13341 pos2
+= isl_basic_map_offset(bmap
, type2
);
13342 isl_int_set_si(bmap
->eq
[i
][pos1
], 1);
13343 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
13344 bmap
= isl_basic_map_finalize(bmap
);
13346 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
13350 isl_basic_map_free(bmap
);
13355 /* Construct a constraint imposing that the value of the first dimension is
13356 * greater than or equal to that of the second.
13358 static __isl_give isl_constraint
*constraint_order_ge(
13359 __isl_take isl_space
*space
, enum isl_dim_type type1
, int pos1
,
13360 enum isl_dim_type type2
, int pos2
)
13364 if (isl_space_check_range(space
, type1
, pos1
, 1) < 0 ||
13365 isl_space_check_range(space
, type2
, pos2
, 1) < 0)
13366 space
= isl_space_free(space
);
13370 c
= isl_constraint_alloc_inequality(isl_local_space_from_space(space
));
13372 if (type1
== type2
&& pos1
== pos2
)
13375 c
= isl_constraint_set_coefficient_si(c
, type1
, pos1
, 1);
13376 c
= isl_constraint_set_coefficient_si(c
, type2
, pos2
, -1);
13381 /* Add a constraint imposing that the value of the first dimension is
13382 * greater than or equal to that of the second.
13384 __isl_give isl_basic_map
*isl_basic_map_order_ge(__isl_take isl_basic_map
*bmap
,
13385 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13390 if (type1
== type2
&& pos1
== pos2
)
13392 space
= isl_basic_map_get_space(bmap
);
13393 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
13394 bmap
= isl_basic_map_add_constraint(bmap
, c
);
13399 /* Add a constraint imposing that the value of the first dimension is
13400 * greater than or equal to that of the second.
13402 __isl_give isl_map
*isl_map_order_ge(__isl_take isl_map
*map
,
13403 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13408 if (type1
== type2
&& pos1
== pos2
)
13410 space
= isl_map_get_space(map
);
13411 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
13412 map
= isl_map_add_constraint(map
, c
);
13417 /* Add a constraint imposing that the value of the first dimension is
13418 * less than or equal to that of the second.
13420 __isl_give isl_map
*isl_map_order_le(__isl_take isl_map
*map
,
13421 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13423 return isl_map_order_ge(map
, type2
, pos2
, type1
, pos1
);
13426 /* Construct a basic map where the value of the first dimension is
13427 * greater than that of the second.
13429 static __isl_give isl_basic_map
*greator(__isl_take isl_space
*space
,
13430 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13432 isl_basic_map
*bmap
= NULL
;
13436 if (isl_space_check_range(space
, type1
, pos1
, 1) < 0 ||
13437 isl_space_check_range(space
, type2
, pos2
, 1) < 0)
13440 if (type1
== type2
&& pos1
== pos2
)
13441 return isl_basic_map_empty(space
);
13443 bmap
= isl_basic_map_alloc_space(space
, 0, 0, 1);
13444 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
13445 i
= isl_basic_map_alloc_inequality(bmap
);
13446 if (total
< 0 || i
< 0)
13447 return isl_basic_map_free(bmap
);
13448 isl_seq_clr(bmap
->ineq
[i
], 1 + total
);
13449 pos1
+= isl_basic_map_offset(bmap
, type1
);
13450 pos2
+= isl_basic_map_offset(bmap
, type2
);
13451 isl_int_set_si(bmap
->ineq
[i
][pos1
], 1);
13452 isl_int_set_si(bmap
->ineq
[i
][pos2
], -1);
13453 isl_int_set_si(bmap
->ineq
[i
][0], -1);
13454 bmap
= isl_basic_map_finalize(bmap
);
13458 isl_space_free(space
);
13459 isl_basic_map_free(bmap
);
13463 /* Add a constraint imposing that the value of the first dimension is
13464 * greater than that of the second.
13466 __isl_give isl_basic_map
*isl_basic_map_order_gt(__isl_take isl_basic_map
*bmap
,
13467 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13471 gt
= greator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
13473 bmap
= isl_basic_map_intersect(bmap
, gt
);
13478 /* Add a constraint imposing that the value of the first dimension is
13479 * greater than that of the second.
13481 __isl_give isl_map
*isl_map_order_gt(__isl_take isl_map
*map
,
13482 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13484 isl_basic_map
*bmap
;
13486 bmap
= greator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
13488 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
13493 /* Add a constraint imposing that the value of the first dimension is
13494 * smaller than that of the second.
13496 __isl_give isl_map
*isl_map_order_lt(__isl_take isl_map
*map
,
13497 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13499 return isl_map_order_gt(map
, type2
, pos2
, type1
, pos1
);
13502 __isl_give isl_aff
*isl_basic_map_get_div(__isl_keep isl_basic_map
*bmap
,
13506 isl_local_space
*ls
;
13511 if (!isl_basic_map_divs_known(bmap
))
13512 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
13513 "some divs are unknown", return NULL
);
13515 ls
= isl_basic_map_get_local_space(bmap
);
13516 div
= isl_local_space_get_div(ls
, pos
);
13517 isl_local_space_free(ls
);
13522 __isl_give isl_aff
*isl_basic_set_get_div(__isl_keep isl_basic_set
*bset
,
13525 return isl_basic_map_get_div(bset
, pos
);
13528 /* Plug in "subs" for set dimension "pos" of "set".
13530 __isl_give isl_set
*isl_set_substitute(__isl_take isl_set
*set
,
13531 unsigned pos
, __isl_keep isl_aff
*subs
)
13535 if (set
&& isl_set_plain_is_empty(set
))
13538 ma
= isl_multi_aff_identity_on_domain_space(isl_set_get_space(set
));
13539 ma
= isl_multi_aff_set_aff(ma
, pos
, isl_aff_copy(subs
));
13540 return isl_set_preimage_multi_aff(set
, ma
);
13543 /* Check if the range of "ma" is compatible with the domain or range
13544 * (depending on "type") of "bmap".
13546 static isl_stat
check_basic_map_compatible_range_multi_aff(
13547 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type type
,
13548 __isl_keep isl_multi_aff
*ma
)
13551 isl_space
*ma_space
;
13553 ma_space
= isl_multi_aff_get_space(ma
);
13555 m
= isl_space_has_equal_params(bmap
->dim
, ma_space
);
13559 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
13560 "parameters don't match", goto error
);
13561 m
= isl_space_tuple_is_equal(bmap
->dim
, type
, ma_space
, isl_dim_out
);
13565 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
13566 "spaces don't match", goto error
);
13568 isl_space_free(ma_space
);
13569 return isl_stat_ok
;
13571 isl_space_free(ma_space
);
13572 return isl_stat_error
;
13575 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
13576 * coefficients before the transformed range of dimensions,
13577 * the "n_after" coefficients after the transformed range of dimensions
13578 * and the coefficients of the other divs in "bmap".
13580 static __isl_give isl_basic_map
*set_ma_divs(__isl_take isl_basic_map
*bmap
,
13581 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
, int n_div
)
13586 isl_local_space
*ls
;
13591 ls
= isl_aff_get_domain_local_space(ma
->u
.p
[0]);
13592 n_param
= isl_local_space_dim(ls
, isl_dim_param
);
13593 n_set
= isl_local_space_dim(ls
, isl_dim_set
);
13594 if (n_param
< 0 || n_set
< 0)
13595 return isl_basic_map_free(bmap
);
13597 for (i
= 0; i
< n_div
; ++i
) {
13598 int o_bmap
= 0, o_ls
= 0;
13600 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], 1 + 1 + n_param
);
13601 o_bmap
+= 1 + 1 + n_param
;
13602 o_ls
+= 1 + 1 + n_param
;
13603 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_before
);
13604 o_bmap
+= n_before
;
13605 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
13606 ls
->div
->row
[i
] + o_ls
, n_set
);
13609 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_after
);
13611 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
13612 ls
->div
->row
[i
] + o_ls
, n_div
);
13615 isl_seq_clr(bmap
->div
[i
] + o_bmap
, bmap
->n_div
- n_div
);
13616 bmap
= isl_basic_map_add_div_constraints(bmap
, i
);
13621 isl_local_space_free(ls
);
13624 isl_local_space_free(ls
);
13625 return isl_basic_map_free(bmap
);
13628 /* How many stride constraints does "ma" enforce?
13629 * That is, how many of the affine expressions have a denominator
13630 * different from one?
13632 static int multi_aff_strides(__isl_keep isl_multi_aff
*ma
)
13637 for (i
= 0; i
< ma
->n
; ++i
)
13638 if (!isl_int_is_one(ma
->u
.p
[i
]->v
->el
[0]))
13644 /* For each affine expression in ma of the form
13646 * x_i = (f_i y + h_i)/m_i
13648 * with m_i different from one, add a constraint to "bmap"
13651 * f_i y + h_i = m_i alpha_i
13653 * with alpha_i an additional existentially quantified variable.
13655 * The input variables of "ma" correspond to a subset of the variables
13656 * of "bmap". There are "n_before" variables in "bmap" before this
13657 * subset and "n_after" variables after this subset.
13658 * The integer divisions of the affine expressions in "ma" are assumed
13659 * to have been aligned. There are "n_div_ma" of them and
13660 * they appear first in "bmap", straight after the "n_after" variables.
13662 static __isl_give isl_basic_map
*add_ma_strides(
13663 __isl_take isl_basic_map
*bmap
, __isl_keep isl_multi_aff
*ma
,
13664 int n_before
, int n_after
, int n_div_ma
)
13672 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
13673 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
13674 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
13675 if (total
< 0 || n_param
< 0 || n_in
< 0)
13676 return isl_basic_map_free(bmap
);
13677 for (i
= 0; i
< ma
->n
; ++i
) {
13678 int o_bmap
= 0, o_ma
= 1;
13680 if (isl_int_is_one(ma
->u
.p
[i
]->v
->el
[0]))
13682 div
= isl_basic_map_alloc_div(bmap
);
13683 k
= isl_basic_map_alloc_equality(bmap
);
13684 if (div
< 0 || k
< 0)
13686 isl_int_set_si(bmap
->div
[div
][0], 0);
13687 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
13688 ma
->u
.p
[i
]->v
->el
+ o_ma
, 1 + n_param
);
13689 o_bmap
+= 1 + n_param
;
13690 o_ma
+= 1 + n_param
;
13691 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_before
);
13692 o_bmap
+= n_before
;
13693 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
13694 ma
->u
.p
[i
]->v
->el
+ o_ma
, n_in
);
13697 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_after
);
13699 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
13700 ma
->u
.p
[i
]->v
->el
+ o_ma
, n_div_ma
);
13701 o_bmap
+= n_div_ma
;
13703 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, 1 + total
- o_bmap
);
13704 isl_int_neg(bmap
->eq
[k
][1 + total
], ma
->u
.p
[i
]->v
->el
[0]);
13710 isl_basic_map_free(bmap
);
13714 /* Replace the domain or range space (depending on "type) of "space" by "set".
13716 static __isl_give isl_space
*isl_space_set(__isl_take isl_space
*space
,
13717 enum isl_dim_type type
, __isl_take isl_space
*set
)
13719 if (type
== isl_dim_in
) {
13720 space
= isl_space_range(space
);
13721 space
= isl_space_map_from_domain_and_range(set
, space
);
13723 space
= isl_space_domain(space
);
13724 space
= isl_space_map_from_domain_and_range(space
, set
);
13730 /* Compute the preimage of the domain or range (depending on "type")
13731 * of "bmap" under the function represented by "ma".
13732 * In other words, plug in "ma" in the domain or range of "bmap".
13733 * The result is a basic map that lives in the same space as "bmap"
13734 * except that the domain or range has been replaced by
13735 * the domain space of "ma".
13737 * If bmap is represented by
13739 * A(p) + S u + B x + T v + C(divs) >= 0,
13741 * where u and x are input and output dimensions if type == isl_dim_out
13742 * while x and v are input and output dimensions if type == isl_dim_in,
13743 * and ma is represented by
13745 * x = D(p) + F(y) + G(divs')
13747 * then the result is
13749 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
13751 * The divs in the input set are similarly adjusted.
13754 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
13758 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
13759 * B_i G(divs') + c_i(divs))/n_i)
13761 * If bmap is not a rational map and if F(y) involves any denominators
13763 * x_i = (f_i y + h_i)/m_i
13765 * then additional constraints are added to ensure that we only
13766 * map back integer points. That is we enforce
13768 * f_i y + h_i = m_i alpha_i
13770 * with alpha_i an additional existentially quantified variable.
13772 * We first copy over the divs from "ma".
13773 * Then we add the modified constraints and divs from "bmap".
13774 * Finally, we add the stride constraints, if needed.
13776 __isl_give isl_basic_map
*isl_basic_map_preimage_multi_aff(
13777 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
13778 __isl_take isl_multi_aff
*ma
)
13782 isl_basic_map
*res
= NULL
;
13783 isl_size n_before
, n_after
, n_div_bmap
, n_div_ma
;
13784 isl_int f
, c1
, c2
, g
;
13793 ma
= isl_multi_aff_align_divs(ma
);
13796 if (check_basic_map_compatible_range_multi_aff(bmap
, type
, ma
) < 0)
13799 if (type
== isl_dim_in
) {
13801 n_after
= isl_basic_map_dim(bmap
, isl_dim_out
);
13803 n_before
= isl_basic_map_dim(bmap
, isl_dim_in
);
13806 n_div_bmap
= isl_basic_map_dim(bmap
, isl_dim_div
);
13807 n_div_ma
= ma
->n
? isl_aff_dim(ma
->u
.p
[0], isl_dim_div
) : 0;
13808 if (n_before
< 0 || n_after
< 0 || n_div_bmap
< 0 || n_div_ma
< 0)
13811 space
= isl_multi_aff_get_domain_space(ma
);
13812 space
= isl_space_set(isl_basic_map_get_space(bmap
), type
, space
);
13813 rational
= isl_basic_map_is_rational(bmap
);
13814 strides
= rational
? 0 : multi_aff_strides(ma
);
13815 res
= isl_basic_map_alloc_space(space
, n_div_ma
+ n_div_bmap
+ strides
,
13816 bmap
->n_eq
+ strides
, bmap
->n_ineq
+ 2 * n_div_ma
);
13818 res
= isl_basic_map_set_rational(res
);
13820 for (i
= 0; i
< n_div_ma
+ n_div_bmap
; ++i
)
13821 if (isl_basic_map_alloc_div(res
) < 0)
13824 res
= set_ma_divs(res
, ma
, n_before
, n_after
, n_div_ma
);
13828 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
13829 k
= isl_basic_map_alloc_equality(res
);
13832 if (isl_seq_preimage(res
->eq
[k
], bmap
->eq
[i
], ma
, n_before
,
13833 n_after
, n_div_ma
, n_div_bmap
,
13834 f
, c1
, c2
, g
, 0) < 0)
13838 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
13839 k
= isl_basic_map_alloc_inequality(res
);
13842 if (isl_seq_preimage(res
->ineq
[k
], bmap
->ineq
[i
], ma
, n_before
,
13843 n_after
, n_div_ma
, n_div_bmap
,
13844 f
, c1
, c2
, g
, 0) < 0)
13848 for (i
= 0; i
< bmap
->n_div
; ++i
) {
13849 if (isl_int_is_zero(bmap
->div
[i
][0])) {
13850 isl_int_set_si(res
->div
[n_div_ma
+ i
][0], 0);
13853 if (isl_seq_preimage(res
->div
[n_div_ma
+ i
], bmap
->div
[i
], ma
,
13854 n_before
, n_after
, n_div_ma
, n_div_bmap
,
13855 f
, c1
, c2
, g
, 1) < 0)
13860 res
= add_ma_strides(res
, ma
, n_before
, n_after
, n_div_ma
);
13866 isl_basic_map_free(bmap
);
13867 isl_multi_aff_free(ma
);
13868 res
= isl_basic_map_simplify(res
);
13869 return isl_basic_map_finalize(res
);
13875 isl_basic_map_free(bmap
);
13876 isl_multi_aff_free(ma
);
13877 isl_basic_map_free(res
);
13881 /* Compute the preimage of "bset" under the function represented by "ma".
13882 * In other words, plug in "ma" in "bset". The result is a basic set
13883 * that lives in the domain space of "ma".
13885 __isl_give isl_basic_set
*isl_basic_set_preimage_multi_aff(
13886 __isl_take isl_basic_set
*bset
, __isl_take isl_multi_aff
*ma
)
13888 return isl_basic_map_preimage_multi_aff(bset
, isl_dim_set
, ma
);
13891 /* Compute the preimage of the domain of "bmap" under the function
13892 * represented by "ma".
13893 * In other words, plug in "ma" in the domain of "bmap".
13894 * The result is a basic map that lives in the same space as "bmap"
13895 * except that the domain has been replaced by the domain space of "ma".
13897 __isl_give isl_basic_map
*isl_basic_map_preimage_domain_multi_aff(
13898 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
13900 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_in
, ma
);
13903 /* Compute the preimage of the range of "bmap" under the function
13904 * represented by "ma".
13905 * In other words, plug in "ma" in the range of "bmap".
13906 * The result is a basic map that lives in the same space as "bmap"
13907 * except that the range has been replaced by the domain space of "ma".
13909 __isl_give isl_basic_map
*isl_basic_map_preimage_range_multi_aff(
13910 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
13912 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_out
, ma
);
13915 /* Check if the range of "ma" is compatible with the domain or range
13916 * (depending on "type") of "map".
13917 * Return isl_stat_error if anything is wrong.
13919 static isl_stat
check_map_compatible_range_multi_aff(
13920 __isl_keep isl_map
*map
, enum isl_dim_type type
,
13921 __isl_keep isl_multi_aff
*ma
)
13924 isl_space
*ma_space
;
13926 ma_space
= isl_multi_aff_get_space(ma
);
13927 m
= isl_map_space_tuple_is_equal(map
, type
, ma_space
, isl_dim_out
);
13928 isl_space_free(ma_space
);
13930 return isl_stat_error
;
13932 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
13933 "spaces don't match", return isl_stat_error
);
13934 return isl_stat_ok
;
13937 /* Compute the preimage of the domain or range (depending on "type")
13938 * of "map" under the function represented by "ma".
13939 * In other words, plug in "ma" in the domain or range of "map".
13940 * The result is a map that lives in the same space as "map"
13941 * except that the domain or range has been replaced by
13942 * the domain space of "ma".
13944 * The parameters are assumed to have been aligned.
13946 static __isl_give isl_map
*map_preimage_multi_aff(__isl_take isl_map
*map
,
13947 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
13952 map
= isl_map_cow(map
);
13953 ma
= isl_multi_aff_align_divs(ma
);
13956 if (check_map_compatible_range_multi_aff(map
, type
, ma
) < 0)
13959 for (i
= 0; i
< map
->n
; ++i
) {
13960 map
->p
[i
] = isl_basic_map_preimage_multi_aff(map
->p
[i
], type
,
13961 isl_multi_aff_copy(ma
));
13966 space
= isl_multi_aff_get_domain_space(ma
);
13967 space
= isl_space_set(isl_map_get_space(map
), type
, space
);
13969 isl_space_free(isl_map_take_space(map
));
13970 map
= isl_map_restore_space(map
, space
);
13974 isl_multi_aff_free(ma
);
13976 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
13977 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
13980 isl_multi_aff_free(ma
);
13985 /* Compute the preimage of the domain or range (depending on "type")
13986 * of "map" under the function represented by "ma".
13987 * In other words, plug in "ma" in the domain or range of "map".
13988 * The result is a map that lives in the same space as "map"
13989 * except that the domain or range has been replaced by
13990 * the domain space of "ma".
13992 __isl_give isl_map
*isl_map_preimage_multi_aff(__isl_take isl_map
*map
,
13993 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
14000 aligned
= isl_map_space_has_equal_params(map
, ma
->space
);
14004 return map_preimage_multi_aff(map
, type
, ma
);
14006 if (isl_map_check_named_params(map
) < 0)
14008 if (!isl_space_has_named_params(ma
->space
))
14009 isl_die(map
->ctx
, isl_error_invalid
,
14010 "unaligned unnamed parameters", goto error
);
14011 map
= isl_map_align_params(map
, isl_multi_aff_get_space(ma
));
14012 ma
= isl_multi_aff_align_params(ma
, isl_map_get_space(map
));
14014 return map_preimage_multi_aff(map
, type
, ma
);
14016 isl_multi_aff_free(ma
);
14017 return isl_map_free(map
);
14020 /* Compute the preimage of "set" under the function represented by "ma".
14021 * In other words, plug in "ma" in "set". The result is a set
14022 * that lives in the domain space of "ma".
14024 __isl_give isl_set
*isl_set_preimage_multi_aff(__isl_take isl_set
*set
,
14025 __isl_take isl_multi_aff
*ma
)
14027 return isl_map_preimage_multi_aff(set
, isl_dim_set
, ma
);
14030 /* Compute the preimage of the domain of "map" under the function
14031 * represented by "ma".
14032 * In other words, plug in "ma" in the domain of "map".
14033 * The result is a map that lives in the same space as "map"
14034 * except that the domain has been replaced by the domain space of "ma".
14036 __isl_give isl_map
*isl_map_preimage_domain_multi_aff(__isl_take isl_map
*map
,
14037 __isl_take isl_multi_aff
*ma
)
14039 return isl_map_preimage_multi_aff(map
, isl_dim_in
, ma
);
14042 /* Compute the preimage of the range of "map" under the function
14043 * represented by "ma".
14044 * In other words, plug in "ma" in the range of "map".
14045 * The result is a map that lives in the same space as "map"
14046 * except that the range has been replaced by the domain space of "ma".
14048 __isl_give isl_map
*isl_map_preimage_range_multi_aff(__isl_take isl_map
*map
,
14049 __isl_take isl_multi_aff
*ma
)
14051 return isl_map_preimage_multi_aff(map
, isl_dim_out
, ma
);
14054 /* Compute the preimage of "map" under the function represented by "pma".
14055 * In other words, plug in "pma" in the domain or range of "map".
14056 * The result is a map that lives in the same space as "map",
14057 * except that the space of type "type" has been replaced by
14058 * the domain space of "pma".
14060 * The parameters of "map" and "pma" are assumed to have been aligned.
14062 static __isl_give isl_map
*isl_map_preimage_pw_multi_aff_aligned(
14063 __isl_take isl_map
*map
, enum isl_dim_type type
,
14064 __isl_take isl_pw_multi_aff
*pma
)
14073 isl_pw_multi_aff_free(pma
);
14074 res
= isl_map_empty(isl_map_get_space(map
));
14079 res
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
14080 isl_multi_aff_copy(pma
->p
[0].maff
));
14081 if (type
== isl_dim_in
)
14082 res
= isl_map_intersect_domain(res
,
14083 isl_map_copy(pma
->p
[0].set
));
14085 res
= isl_map_intersect_range(res
,
14086 isl_map_copy(pma
->p
[0].set
));
14088 for (i
= 1; i
< pma
->n
; ++i
) {
14091 res_i
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
14092 isl_multi_aff_copy(pma
->p
[i
].maff
));
14093 if (type
== isl_dim_in
)
14094 res_i
= isl_map_intersect_domain(res_i
,
14095 isl_map_copy(pma
->p
[i
].set
));
14097 res_i
= isl_map_intersect_range(res_i
,
14098 isl_map_copy(pma
->p
[i
].set
));
14099 res
= isl_map_union(res
, res_i
);
14102 isl_pw_multi_aff_free(pma
);
14106 isl_pw_multi_aff_free(pma
);
14111 /* Compute the preimage of "map" under the function represented by "pma".
14112 * In other words, plug in "pma" in the domain or range of "map".
14113 * The result is a map that lives in the same space as "map",
14114 * except that the space of type "type" has been replaced by
14115 * the domain space of "pma".
14117 __isl_give isl_map
*isl_map_preimage_pw_multi_aff(__isl_take isl_map
*map
,
14118 enum isl_dim_type type
, __isl_take isl_pw_multi_aff
*pma
)
14125 aligned
= isl_map_space_has_equal_params(map
, pma
->dim
);
14129 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
14131 if (isl_map_check_named_params(map
) < 0)
14133 if (isl_pw_multi_aff_check_named_params(pma
) < 0)
14135 map
= isl_map_align_params(map
, isl_pw_multi_aff_get_space(pma
));
14136 pma
= isl_pw_multi_aff_align_params(pma
, isl_map_get_space(map
));
14138 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
14140 isl_pw_multi_aff_free(pma
);
14141 return isl_map_free(map
);
14144 /* Compute the preimage of "set" under the function represented by "pma".
14145 * In other words, plug in "pma" in "set". The result is a set
14146 * that lives in the domain space of "pma".
14148 __isl_give isl_set
*isl_set_preimage_pw_multi_aff(__isl_take isl_set
*set
,
14149 __isl_take isl_pw_multi_aff
*pma
)
14151 return isl_map_preimage_pw_multi_aff(set
, isl_dim_set
, pma
);
14154 /* Compute the preimage of the domain of "map" under the function
14155 * represented by "pma".
14156 * In other words, plug in "pma" in the domain of "map".
14157 * The result is a map that lives in the same space as "map",
14158 * except that domain space has been replaced by the domain space of "pma".
14160 __isl_give isl_map
*isl_map_preimage_domain_pw_multi_aff(
14161 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
14163 return isl_map_preimage_pw_multi_aff(map
, isl_dim_in
, pma
);
14166 /* Compute the preimage of the range of "map" under the function
14167 * represented by "pma".
14168 * In other words, plug in "pma" in the range of "map".
14169 * The result is a map that lives in the same space as "map",
14170 * except that range space has been replaced by the domain space of "pma".
14172 __isl_give isl_map
*isl_map_preimage_range_pw_multi_aff(
14173 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
14175 return isl_map_preimage_pw_multi_aff(map
, isl_dim_out
, pma
);
14178 /* Compute the preimage of "map" under the function represented by "mpa".
14179 * In other words, plug in "mpa" in the domain or range of "map".
14180 * The result is a map that lives in the same space as "map",
14181 * except that the space of type "type" has been replaced by
14182 * the domain space of "mpa".
14184 * If the map does not involve any constraints that refer to the
14185 * dimensions of the substituted space, then the only possible
14186 * effect of "mpa" on the map is to map the space to a different space.
14187 * We create a separate isl_multi_aff to effectuate this change
14188 * in order to avoid spurious splitting of the map along the pieces
14190 * If "mpa" has a non-trivial explicit domain, however,
14191 * then the full substitution should be performed.
14193 __isl_give isl_map
*isl_map_preimage_multi_pw_aff(__isl_take isl_map
*map
,
14194 enum isl_dim_type type
, __isl_take isl_multi_pw_aff
*mpa
)
14198 isl_pw_multi_aff
*pma
;
14200 n
= isl_map_dim(map
, type
);
14204 full
= isl_map_involves_dims(map
, type
, 0, n
);
14205 if (full
>= 0 && !full
)
14206 full
= isl_multi_pw_aff_has_non_trivial_domain(mpa
);
14213 space
= isl_multi_pw_aff_get_space(mpa
);
14214 isl_multi_pw_aff_free(mpa
);
14215 ma
= isl_multi_aff_zero(space
);
14216 return isl_map_preimage_multi_aff(map
, type
, ma
);
14219 pma
= isl_pw_multi_aff_from_multi_pw_aff(mpa
);
14220 return isl_map_preimage_pw_multi_aff(map
, type
, pma
);
14223 isl_multi_pw_aff_free(mpa
);
14227 /* Compute the preimage of "map" under the function represented by "mpa".
14228 * In other words, plug in "mpa" in the domain "map".
14229 * The result is a map that lives in the same space as "map",
14230 * except that domain space has been replaced by the domain space of "mpa".
14232 __isl_give isl_map
*isl_map_preimage_domain_multi_pw_aff(
14233 __isl_take isl_map
*map
, __isl_take isl_multi_pw_aff
*mpa
)
14235 return isl_map_preimage_multi_pw_aff(map
, isl_dim_in
, mpa
);
14238 /* Compute the preimage of "set" by the function represented by "mpa".
14239 * In other words, plug in "mpa" in "set".
14241 __isl_give isl_set
*isl_set_preimage_multi_pw_aff(__isl_take isl_set
*set
,
14242 __isl_take isl_multi_pw_aff
*mpa
)
14244 return isl_map_preimage_multi_pw_aff(set
, isl_dim_set
, mpa
);
14247 /* Return a copy of the equality constraints of "bset" as a matrix.
14249 __isl_give isl_mat
*isl_basic_set_extract_equalities(
14250 __isl_keep isl_basic_set
*bset
)
14255 total
= isl_basic_set_dim(bset
, isl_dim_all
);
14259 ctx
= isl_basic_set_get_ctx(bset
);
14260 return isl_mat_sub_alloc6(ctx
, bset
->eq
, 0, bset
->n_eq
, 0, 1 + total
);
14263 /* Are the "n" "coefficients" starting at "first" of the integer division
14264 * expressions at position "pos1" in "bmap1" and "pos2" in "bmap2" equal
14266 * The "coefficient" at position 0 is the denominator.
14267 * The "coefficient" at position 1 is the constant term.
14269 isl_bool
isl_basic_map_equal_div_expr_part(__isl_keep isl_basic_map
*bmap1
,
14270 int pos1
, __isl_keep isl_basic_map
*bmap2
, int pos2
,
14271 unsigned first
, unsigned n
)
14273 if (isl_basic_map_check_range(bmap1
, isl_dim_div
, pos1
, 1) < 0)
14274 return isl_bool_error
;
14275 if (isl_basic_map_check_range(bmap2
, isl_dim_div
, pos2
, 1) < 0)
14276 return isl_bool_error
;
14277 return isl_seq_eq(bmap1
->div
[pos1
] + first
,
14278 bmap2
->div
[pos2
] + first
, n
);
14281 /* Are the integer division expressions at position "pos1" in "bmap1" and
14282 * "pos2" in "bmap2" equal to each other, except that the constant terms
14285 isl_bool
isl_basic_map_equal_div_expr_except_constant(
14286 __isl_keep isl_basic_map
*bmap1
, int pos1
,
14287 __isl_keep isl_basic_map
*bmap2
, int pos2
)
14290 isl_size total
, total2
;
14292 total
= isl_basic_map_dim(bmap1
, isl_dim_all
);
14293 total2
= isl_basic_map_dim(bmap2
, isl_dim_all
);
14294 if (total
< 0 || total2
< 0)
14295 return isl_bool_error
;
14296 if (total
!= total2
)
14297 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
14298 "incomparable div expressions", return isl_bool_error
);
14299 equal
= isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
14301 if (equal
< 0 || !equal
)
14303 equal
= isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
14305 if (equal
< 0 || equal
)
14306 return isl_bool_not(equal
);
14307 return isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
14311 /* Replace the numerator of the constant term of the integer division
14312 * expression at position "div" in "bmap" by "value".
14313 * The caller guarantees that this does not change the meaning
14316 __isl_give isl_basic_map
*isl_basic_map_set_div_expr_constant_num_si_inplace(
14317 __isl_take isl_basic_map
*bmap
, int div
, int value
)
14319 if (isl_basic_map_check_range(bmap
, isl_dim_div
, div
, 1) < 0)
14320 return isl_basic_map_free(bmap
);
14322 isl_int_set_si(bmap
->div
[div
][1], value
);
14327 /* Is the point "inner" internal to inequality constraint "ineq"
14329 * The point is considered to be internal to the inequality constraint,
14330 * if it strictly lies on the positive side of the inequality constraint,
14331 * or if it lies on the constraint and the constraint is lexico-positive.
14333 static isl_bool
is_internal(__isl_keep isl_vec
*inner
,
14334 __isl_keep isl_basic_set
*bset
, int ineq
)
14340 if (!inner
|| !bset
)
14341 return isl_bool_error
;
14343 ctx
= isl_basic_set_get_ctx(bset
);
14344 isl_seq_inner_product(inner
->el
, bset
->ineq
[ineq
], inner
->size
,
14345 &ctx
->normalize_gcd
);
14346 if (!isl_int_is_zero(ctx
->normalize_gcd
))
14347 return isl_int_is_nonneg(ctx
->normalize_gcd
);
14349 total
= isl_basic_set_dim(bset
, isl_dim_all
);
14351 return isl_bool_error
;
14352 pos
= isl_seq_first_non_zero(bset
->ineq
[ineq
] + 1, total
);
14353 return isl_int_is_pos(bset
->ineq
[ineq
][1 + pos
]);
14356 /* Tighten the inequality constraints of "bset" that are outward with respect
14357 * to the point "vec".
14358 * That is, tighten the constraints that are not satisfied by "vec".
14360 * "vec" is a point internal to some superset S of "bset" that is used
14361 * to make the subsets of S disjoint, by tightening one half of the constraints
14362 * that separate two subsets. In particular, the constraints of S
14363 * are all satisfied by "vec" and should not be tightened.
14364 * Of the internal constraints, those that have "vec" on the outside
14365 * are tightened. The shared facet is included in the adjacent subset
14366 * with the opposite constraint.
14367 * For constraints that saturate "vec", this criterion cannot be used
14368 * to determine which of the two sides should be tightened.
14369 * Instead, the sign of the first non-zero coefficient is used
14370 * to make this choice. Note that this second criterion is never used
14371 * on the constraints of S since "vec" is interior to "S".
14373 __isl_give isl_basic_set
*isl_basic_set_tighten_outward(
14374 __isl_take isl_basic_set
*bset
, __isl_keep isl_vec
*vec
)
14378 bset
= isl_basic_set_cow(bset
);
14381 for (j
= 0; j
< bset
->n_ineq
; ++j
) {
14384 internal
= is_internal(vec
, bset
, j
);
14386 return isl_basic_set_free(bset
);
14389 isl_int_sub_ui(bset
->ineq
[j
][0], bset
->ineq
[j
][0], 1);
14395 /* Replace the variables x of type "type" starting at "first" in "bmap"
14396 * by x' with x = M x' with M the matrix trans.
14397 * That is, replace the corresponding coefficients c by c M.
14399 * The transformation matrix should be a square matrix.
14401 __isl_give isl_basic_map
*isl_basic_map_transform_dims(
14402 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
, unsigned first
,
14403 __isl_take isl_mat
*trans
)
14407 bmap
= isl_basic_map_cow(bmap
);
14408 if (!bmap
|| !trans
)
14411 if (trans
->n_row
!= trans
->n_col
)
14412 isl_die(trans
->ctx
, isl_error_invalid
,
14413 "expecting square transformation matrix", goto error
);
14414 if (isl_basic_map_check_range(bmap
, type
, first
, trans
->n_row
) < 0)
14417 pos
= isl_basic_map_offset(bmap
, type
) + first
;
14419 if (isl_mat_sub_transform(bmap
->eq
, bmap
->n_eq
, pos
,
14420 isl_mat_copy(trans
)) < 0)
14422 if (isl_mat_sub_transform(bmap
->ineq
, bmap
->n_ineq
, pos
,
14423 isl_mat_copy(trans
)) < 0)
14425 if (isl_mat_sub_transform(bmap
->div
, bmap
->n_div
, 1 + pos
,
14426 isl_mat_copy(trans
)) < 0)
14429 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
14430 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
14432 isl_mat_free(trans
);
14435 isl_mat_free(trans
);
14436 isl_basic_map_free(bmap
);
14440 /* Replace the variables x of type "type" starting at "first" in "bset"
14441 * by x' with x = M x' with M the matrix trans.
14442 * That is, replace the corresponding coefficients c by c M.
14444 * The transformation matrix should be a square matrix.
14446 __isl_give isl_basic_set
*isl_basic_set_transform_dims(
14447 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned first
,
14448 __isl_take isl_mat
*trans
)
14450 return isl_basic_map_transform_dims(bset
, type
, first
, trans
);