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 isl_size
isl_set_dim(__isl_keep isl_set
*set
, enum isl_dim_type type
)
117 return isl_map_dim(set_to_map(set
), type
);
120 /* Return the position of the variables of the given type
121 * within the sequence of variables of "bmap".
123 isl_size
isl_basic_map_var_offset(__isl_keep isl_basic_map
*bmap
,
124 enum isl_dim_type type
)
128 space
= isl_basic_map_peek_space(bmap
);
130 return isl_size_error
;
135 case isl_dim_out
: return isl_space_offset(space
, type
);
136 case isl_dim_div
: return isl_space_dim(space
, isl_dim_all
);
139 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
140 "invalid dimension type", return isl_size_error
);
144 /* Return the position of the variables of the given type
145 * within the sequence of variables of "bset".
147 isl_size
isl_basic_set_var_offset(__isl_keep isl_basic_set
*bset
,
148 enum isl_dim_type type
)
150 return isl_basic_map_var_offset(bset_to_bmap(bset
), type
);
153 /* Return the position of the coefficients of the variables of the given type
154 * within the sequence of coefficients of "bmap".
156 unsigned isl_basic_map_offset(__isl_keep isl_basic_map
*bmap
,
157 enum isl_dim_type type
)
160 case isl_dim_cst
: return 0;
164 case isl_dim_div
: return 1 + isl_basic_map_var_offset(bmap
, type
);
169 unsigned isl_basic_set_offset(__isl_keep isl_basic_set
*bset
,
170 enum isl_dim_type type
)
172 return isl_basic_map_offset(bset
, type
);
175 static unsigned map_offset(__isl_keep isl_map
*map
, enum isl_dim_type type
)
177 return pos(map
->dim
, type
);
180 isl_size
isl_basic_set_dim(__isl_keep isl_basic_set
*bset
,
181 enum isl_dim_type type
)
183 return isl_basic_map_dim(bset
, type
);
186 isl_size
isl_basic_set_n_dim(__isl_keep isl_basic_set
*bset
)
188 return isl_basic_set_dim(bset
, isl_dim_set
);
191 isl_size
isl_basic_set_n_param(__isl_keep isl_basic_set
*bset
)
193 return isl_basic_set_dim(bset
, isl_dim_param
);
196 isl_size
isl_basic_set_total_dim(__isl_keep
const isl_basic_set
*bset
)
198 return isl_basic_map_total_dim(const_bset_to_bmap(bset
));
201 isl_size
isl_set_n_dim(__isl_keep isl_set
*set
)
203 return isl_set_dim(set
, isl_dim_set
);
206 isl_size
isl_set_n_param(__isl_keep isl_set
*set
)
208 return isl_set_dim(set
, isl_dim_param
);
211 isl_size
isl_basic_map_total_dim(__isl_keep
const isl_basic_map
*bmap
)
216 return isl_size_error
;
217 dim
= isl_space_dim(bmap
->dim
, isl_dim_all
);
219 return isl_size_error
;
220 return dim
+ bmap
->n_div
;
223 /* Return the number of equality constraints in the description of "bmap".
224 * Return isl_size_error on error.
226 isl_size
isl_basic_map_n_equality(__isl_keep isl_basic_map
*bmap
)
229 return isl_size_error
;
233 /* Return the number of equality constraints in the description of "bset".
234 * Return isl_size_error on error.
236 isl_size
isl_basic_set_n_equality(__isl_keep isl_basic_set
*bset
)
238 return isl_basic_map_n_equality(bset_to_bmap(bset
));
241 /* Return the number of inequality constraints in the description of "bmap".
242 * Return isl_size_error on error.
244 isl_size
isl_basic_map_n_inequality(__isl_keep isl_basic_map
*bmap
)
247 return isl_size_error
;
251 /* Return the number of inequality constraints in the description of "bset".
252 * Return isl_size_error on error.
254 isl_size
isl_basic_set_n_inequality(__isl_keep isl_basic_set
*bset
)
256 return isl_basic_map_n_inequality(bset_to_bmap(bset
));
259 /* Do "bmap1" and "bmap2" have the same parameters?
261 static isl_bool
isl_basic_map_has_equal_params(__isl_keep isl_basic_map
*bmap1
,
262 __isl_keep isl_basic_map
*bmap2
)
264 isl_space
*space1
, *space2
;
266 space1
= isl_basic_map_peek_space(bmap1
);
267 space2
= isl_basic_map_peek_space(bmap2
);
268 return isl_space_has_equal_params(space1
, space2
);
271 /* Do "map1" and "map2" have the same parameters?
273 isl_bool
isl_map_has_equal_params(__isl_keep isl_map
*map1
,
274 __isl_keep isl_map
*map2
)
276 isl_space
*space1
, *space2
;
278 space1
= isl_map_peek_space(map1
);
279 space2
= isl_map_peek_space(map2
);
280 return isl_space_has_equal_params(space1
, space2
);
283 /* Do "map" and "set" have the same parameters?
285 static isl_bool
isl_map_set_has_equal_params(__isl_keep isl_map
*map
,
286 __isl_keep isl_set
*set
)
288 return isl_map_has_equal_params(map
, set_to_map(set
));
291 isl_bool
isl_map_compatible_domain(__isl_keep isl_map
*map
,
292 __isl_keep isl_set
*set
)
296 return isl_bool_error
;
297 m
= isl_map_has_equal_params(map
, set_to_map(set
));
300 return isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
301 set
->dim
, isl_dim_set
);
304 isl_bool
isl_basic_map_compatible_domain(__isl_keep isl_basic_map
*bmap
,
305 __isl_keep isl_basic_set
*bset
)
309 return isl_bool_error
;
310 m
= isl_basic_map_has_equal_params(bmap
, bset_to_bmap(bset
));
313 return isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
314 bset
->dim
, isl_dim_set
);
317 isl_bool
isl_map_compatible_range(__isl_keep isl_map
*map
,
318 __isl_keep isl_set
*set
)
322 return isl_bool_error
;
323 m
= isl_map_has_equal_params(map
, set_to_map(set
));
326 return isl_space_tuple_is_equal(map
->dim
, isl_dim_out
,
327 set
->dim
, isl_dim_set
);
330 isl_bool
isl_basic_map_compatible_range(__isl_keep isl_basic_map
*bmap
,
331 __isl_keep isl_basic_set
*bset
)
335 return isl_bool_error
;
336 m
= isl_basic_map_has_equal_params(bmap
, bset_to_bmap(bset
));
339 return isl_space_tuple_is_equal(bmap
->dim
, isl_dim_out
,
340 bset
->dim
, isl_dim_set
);
343 isl_ctx
*isl_basic_map_get_ctx(__isl_keep isl_basic_map
*bmap
)
345 return bmap
? bmap
->ctx
: NULL
;
348 isl_ctx
*isl_basic_set_get_ctx(__isl_keep isl_basic_set
*bset
)
350 return bset
? bset
->ctx
: NULL
;
353 isl_ctx
*isl_map_get_ctx(__isl_keep isl_map
*map
)
355 return map
? map
->ctx
: NULL
;
358 isl_ctx
*isl_set_get_ctx(__isl_keep isl_set
*set
)
360 return set
? set
->ctx
: NULL
;
363 /* Return the space of "bmap".
365 __isl_keep isl_space
*isl_basic_map_peek_space(
366 __isl_keep
const isl_basic_map
*bmap
)
368 return bmap
? bmap
->dim
: NULL
;
371 /* Return the space of "bset".
373 __isl_keep isl_space
*isl_basic_set_peek_space(__isl_keep isl_basic_set
*bset
)
375 return isl_basic_map_peek_space(bset_to_bmap(bset
));
378 __isl_give isl_space
*isl_basic_map_get_space(__isl_keep isl_basic_map
*bmap
)
380 return isl_space_copy(isl_basic_map_peek_space(bmap
));
383 __isl_give isl_space
*isl_basic_set_get_space(__isl_keep isl_basic_set
*bset
)
385 return isl_basic_map_get_space(bset_to_bmap(bset
));
388 /* Return the space of "bmap".
389 * This may be either a copy or the space itself
390 * if there is only one reference to "bmap".
391 * This allows the space to be modified inplace
392 * if both the basic map and its space have only a single reference.
393 * The caller is not allowed to modify "bmap" between this call and
394 * a subsequent call to isl_basic_map_restore_space.
395 * The only exception is that isl_basic_map_free can be called instead.
397 static __isl_give isl_space
*isl_basic_map_take_space(
398 __isl_keep isl_basic_map
*bmap
)
405 return isl_basic_map_get_space(bmap
);
411 /* Set the space of "bmap" to "space", where the space of "bmap" may be missing
412 * due to a preceding call to isl_basic_map_take_space.
413 * However, in this case, "bmap" only has a single reference and
414 * then the call to isl_basic_map_cow has no effect.
416 static __isl_give isl_basic_map
*isl_basic_map_restore_space(
417 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*space
)
422 if (bmap
->dim
== space
) {
423 isl_space_free(space
);
427 bmap
= isl_basic_map_cow(bmap
);
430 isl_space_free(bmap
->dim
);
435 isl_basic_map_free(bmap
);
436 isl_space_free(space
);
440 /* Extract the divs in "bmap" as a matrix.
442 __isl_give isl_mat
*isl_basic_map_get_divs(__isl_keep isl_basic_map
*bmap
)
450 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
454 ctx
= isl_basic_map_get_ctx(bmap
);
455 cols
= 1 + 1 + v_div
+ bmap
->n_div
;
456 div
= isl_mat_alloc(ctx
, bmap
->n_div
, cols
);
460 for (i
= 0; i
< bmap
->n_div
; ++i
)
461 isl_seq_cpy(div
->row
[i
], bmap
->div
[i
], cols
);
466 /* Extract the divs in "bset" as a matrix.
468 __isl_give isl_mat
*isl_basic_set_get_divs(__isl_keep isl_basic_set
*bset
)
470 return isl_basic_map_get_divs(bset
);
473 __isl_give isl_local_space
*isl_basic_map_get_local_space(
474 __isl_keep isl_basic_map
*bmap
)
481 div
= isl_basic_map_get_divs(bmap
);
482 return isl_local_space_alloc_div(isl_space_copy(bmap
->dim
), div
);
485 __isl_give isl_local_space
*isl_basic_set_get_local_space(
486 __isl_keep isl_basic_set
*bset
)
488 return isl_basic_map_get_local_space(bset
);
491 /* For each known div d = floor(f/m), add the constraints
494 * -(f-(m-1)) + m d >= 0
496 * Do not finalize the result.
498 static __isl_give isl_basic_map
*add_known_div_constraints(
499 __isl_take isl_basic_map
*bmap
)
504 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
506 return isl_basic_map_free(bmap
);
509 bmap
= isl_basic_map_cow(bmap
);
510 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 2 * n_div
);
513 for (i
= 0; i
< n_div
; ++i
) {
514 if (isl_int_is_zero(bmap
->div
[i
][0]))
516 bmap
= isl_basic_map_add_div_constraints(bmap
, i
);
522 __isl_give isl_basic_map
*isl_basic_map_from_local_space(
523 __isl_take isl_local_space
*ls
)
529 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
531 ls
= isl_local_space_free(ls
);
535 bmap
= isl_basic_map_alloc_space(isl_local_space_get_space(ls
),
536 n_div
, 0, 2 * n_div
);
538 for (i
= 0; i
< n_div
; ++i
)
539 if (isl_basic_map_alloc_div(bmap
) < 0)
542 for (i
= 0; i
< n_div
; ++i
)
543 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], ls
->div
->n_col
);
544 bmap
= add_known_div_constraints(bmap
);
546 isl_local_space_free(ls
);
549 isl_local_space_free(ls
);
550 isl_basic_map_free(bmap
);
554 __isl_give isl_basic_set
*isl_basic_set_from_local_space(
555 __isl_take isl_local_space
*ls
)
557 return isl_basic_map_from_local_space(ls
);
560 __isl_give isl_space
*isl_map_get_space(__isl_keep isl_map
*map
)
562 return isl_space_copy(isl_map_peek_space(map
));
565 __isl_give isl_space
*isl_set_get_space(__isl_keep isl_set
*set
)
569 return isl_space_copy(set
->dim
);
572 /* Return the space of "map".
573 * This may be either a copy or the space itself
574 * if there is only one reference to "map".
575 * This allows the space to be modified inplace
576 * if both the map and its space have only a single reference.
577 * The caller is not allowed to modify "map" between this call and
578 * a subsequent call to isl_map_restore_space.
579 * The only exception is that isl_map_free can be called instead.
581 static __isl_give isl_space
*isl_map_take_space(__isl_keep isl_map
*map
)
588 return isl_map_get_space(map
);
594 /* Set the space of "map" to "space", where the space of "map" may be missing
595 * due to a preceding call to isl_map_take_space.
596 * However, in this case, "map" only has a single reference and
597 * then the call to isl_map_cow has no effect.
599 static __isl_give isl_map
*isl_map_restore_space(__isl_take isl_map
*map
,
600 __isl_take isl_space
*space
)
605 if (map
->dim
== space
) {
606 isl_space_free(space
);
610 map
= isl_map_cow(map
);
613 isl_space_free(map
->dim
);
619 isl_space_free(space
);
623 __isl_give isl_basic_map
*isl_basic_map_set_tuple_name(
624 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
, const char *s
)
628 space
= isl_basic_map_take_space(bmap
);
629 space
= isl_space_set_tuple_name(space
, type
, s
);
630 bmap
= isl_basic_map_restore_space(bmap
, space
);
631 bmap
= isl_basic_map_finalize(bmap
);
635 __isl_give isl_basic_set
*isl_basic_set_set_tuple_name(
636 __isl_take isl_basic_set
*bset
, const char *s
)
638 return isl_basic_map_set_tuple_name(bset
, isl_dim_set
, s
);
641 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map
*bmap
,
642 enum isl_dim_type type
)
644 return bmap
? isl_space_get_tuple_name(bmap
->dim
, type
) : NULL
;
647 __isl_give isl_map
*isl_map_set_tuple_name(__isl_take isl_map
*map
,
648 enum isl_dim_type type
, const char *s
)
653 map
= isl_map_cow(map
);
657 for (i
= 0; i
< map
->n
; ++i
) {
658 map
->p
[i
] = isl_basic_map_set_tuple_name(map
->p
[i
], type
, s
);
663 space
= isl_map_take_space(map
);
664 space
= isl_space_set_tuple_name(space
, type
, s
);
665 map
= isl_map_restore_space(map
, space
);
673 /* Replace the identifier of the tuple of type "type" by "id".
675 __isl_give isl_basic_map
*isl_basic_map_set_tuple_id(
676 __isl_take isl_basic_map
*bmap
,
677 enum isl_dim_type type
, __isl_take isl_id
*id
)
681 space
= isl_basic_map_take_space(bmap
);
682 space
= isl_space_set_tuple_id(space
, type
, id
);
683 bmap
= isl_basic_map_restore_space(bmap
, space
);
684 bmap
= isl_basic_map_finalize(bmap
);
688 /* Replace the identifier of the tuple by "id".
690 __isl_give isl_basic_set
*isl_basic_set_set_tuple_id(
691 __isl_take isl_basic_set
*bset
, __isl_take isl_id
*id
)
693 return isl_basic_map_set_tuple_id(bset
, isl_dim_set
, id
);
696 /* Does the input or output tuple have a name?
698 isl_bool
isl_map_has_tuple_name(__isl_keep isl_map
*map
, enum isl_dim_type type
)
700 return map
? isl_space_has_tuple_name(map
->dim
, type
) : isl_bool_error
;
703 const char *isl_map_get_tuple_name(__isl_keep isl_map
*map
,
704 enum isl_dim_type type
)
706 return map
? isl_space_get_tuple_name(map
->dim
, type
) : NULL
;
709 __isl_give isl_set
*isl_set_set_tuple_name(__isl_take isl_set
*set
,
712 return set_from_map(isl_map_set_tuple_name(set_to_map(set
),
716 __isl_give isl_map
*isl_map_set_tuple_id(__isl_take isl_map
*map
,
717 enum isl_dim_type type
, __isl_take isl_id
*id
)
721 space
= isl_map_take_space(map
);
722 space
= isl_space_set_tuple_id(space
, type
, id
);
723 map
= isl_map_restore_space(map
, space
);
725 return isl_map_reset_space(map
, isl_map_get_space(map
));
728 __isl_give isl_set
*isl_set_set_tuple_id(__isl_take isl_set
*set
,
729 __isl_take isl_id
*id
)
731 return isl_map_set_tuple_id(set
, isl_dim_set
, id
);
734 __isl_give isl_map
*isl_map_reset_tuple_id(__isl_take isl_map
*map
,
735 enum isl_dim_type type
)
739 space
= isl_map_take_space(map
);
740 space
= isl_space_reset_tuple_id(space
, type
);
741 map
= isl_map_restore_space(map
, space
);
743 return isl_map_reset_space(map
, isl_map_get_space(map
));
746 __isl_give isl_set
*isl_set_reset_tuple_id(__isl_take isl_set
*set
)
748 return isl_map_reset_tuple_id(set
, isl_dim_set
);
751 isl_bool
isl_map_has_tuple_id(__isl_keep isl_map
*map
, enum isl_dim_type type
)
753 return map
? isl_space_has_tuple_id(map
->dim
, type
) : isl_bool_error
;
756 __isl_give isl_id
*isl_map_get_tuple_id(__isl_keep isl_map
*map
,
757 enum isl_dim_type type
)
759 return map
? isl_space_get_tuple_id(map
->dim
, type
) : NULL
;
762 isl_bool
isl_set_has_tuple_id(__isl_keep isl_set
*set
)
764 return isl_map_has_tuple_id(set
, isl_dim_set
);
767 __isl_give isl_id
*isl_set_get_tuple_id(__isl_keep isl_set
*set
)
769 return isl_map_get_tuple_id(set
, isl_dim_set
);
772 /* Does the set tuple have a name?
774 isl_bool
isl_set_has_tuple_name(__isl_keep isl_set
*set
)
777 return isl_bool_error
;
778 return isl_space_has_tuple_name(set
->dim
, isl_dim_set
);
782 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set
*bset
)
784 return bset
? isl_space_get_tuple_name(bset
->dim
, isl_dim_set
) : NULL
;
787 const char *isl_set_get_tuple_name(__isl_keep isl_set
*set
)
789 return set
? isl_space_get_tuple_name(set
->dim
, isl_dim_set
) : NULL
;
792 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map
*bmap
,
793 enum isl_dim_type type
, unsigned pos
)
795 return bmap
? isl_space_get_dim_name(bmap
->dim
, type
, pos
) : NULL
;
798 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set
*bset
,
799 enum isl_dim_type type
, unsigned pos
)
801 return bset
? isl_space_get_dim_name(bset
->dim
, type
, pos
) : NULL
;
804 /* Does the given dimension have a name?
806 isl_bool
isl_map_has_dim_name(__isl_keep isl_map
*map
,
807 enum isl_dim_type type
, unsigned pos
)
810 return isl_bool_error
;
811 return isl_space_has_dim_name(map
->dim
, type
, pos
);
814 const char *isl_map_get_dim_name(__isl_keep isl_map
*map
,
815 enum isl_dim_type type
, unsigned pos
)
817 return map
? isl_space_get_dim_name(map
->dim
, type
, pos
) : NULL
;
820 const char *isl_set_get_dim_name(__isl_keep isl_set
*set
,
821 enum isl_dim_type type
, unsigned pos
)
823 return set
? isl_space_get_dim_name(set
->dim
, type
, pos
) : NULL
;
826 /* Does the given dimension have a name?
828 isl_bool
isl_set_has_dim_name(__isl_keep isl_set
*set
,
829 enum isl_dim_type type
, unsigned pos
)
832 return isl_bool_error
;
833 return isl_space_has_dim_name(set
->dim
, type
, pos
);
836 __isl_give isl_basic_map
*isl_basic_map_set_dim_name(
837 __isl_take isl_basic_map
*bmap
,
838 enum isl_dim_type type
, unsigned pos
, const char *s
)
842 space
= isl_basic_map_take_space(bmap
);
843 space
= isl_space_set_dim_name(space
, type
, pos
, s
);
844 bmap
= isl_basic_map_restore_space(bmap
, space
);
845 return isl_basic_map_finalize(bmap
);
848 __isl_give isl_map
*isl_map_set_dim_name(__isl_take isl_map
*map
,
849 enum isl_dim_type type
, unsigned pos
, const char *s
)
854 map
= isl_map_cow(map
);
858 for (i
= 0; i
< map
->n
; ++i
) {
859 map
->p
[i
] = isl_basic_map_set_dim_name(map
->p
[i
], type
, pos
, s
);
864 space
= isl_map_take_space(map
);
865 space
= isl_space_set_dim_name(space
, type
, pos
, s
);
866 map
= isl_map_restore_space(map
, space
);
874 __isl_give isl_basic_set
*isl_basic_set_set_dim_name(
875 __isl_take isl_basic_set
*bset
,
876 enum isl_dim_type type
, unsigned pos
, const char *s
)
878 return bset_from_bmap(isl_basic_map_set_dim_name(bset_to_bmap(bset
),
882 __isl_give isl_set
*isl_set_set_dim_name(__isl_take isl_set
*set
,
883 enum isl_dim_type type
, unsigned pos
, const char *s
)
885 return set_from_map(isl_map_set_dim_name(set_to_map(set
),
889 isl_bool
isl_basic_map_has_dim_id(__isl_keep isl_basic_map
*bmap
,
890 enum isl_dim_type type
, unsigned pos
)
893 return isl_bool_error
;
894 return isl_space_has_dim_id(bmap
->dim
, type
, pos
);
897 __isl_give isl_id
*isl_basic_set_get_dim_id(__isl_keep isl_basic_set
*bset
,
898 enum isl_dim_type type
, unsigned pos
)
900 return bset
? isl_space_get_dim_id(bset
->dim
, type
, pos
) : NULL
;
903 isl_bool
isl_map_has_dim_id(__isl_keep isl_map
*map
,
904 enum isl_dim_type type
, unsigned pos
)
906 return map
? isl_space_has_dim_id(map
->dim
, type
, pos
) : isl_bool_error
;
909 __isl_give isl_id
*isl_map_get_dim_id(__isl_keep isl_map
*map
,
910 enum isl_dim_type type
, unsigned pos
)
912 return map
? isl_space_get_dim_id(map
->dim
, type
, pos
) : NULL
;
915 isl_bool
isl_set_has_dim_id(__isl_keep isl_set
*set
,
916 enum isl_dim_type type
, unsigned pos
)
918 return isl_map_has_dim_id(set
, type
, pos
);
921 __isl_give isl_id
*isl_set_get_dim_id(__isl_keep isl_set
*set
,
922 enum isl_dim_type type
, unsigned pos
)
924 return isl_map_get_dim_id(set
, type
, pos
);
927 __isl_give isl_map
*isl_map_set_dim_id(__isl_take isl_map
*map
,
928 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
932 space
= isl_map_take_space(map
);
933 space
= isl_space_set_dim_id(space
, type
, pos
, id
);
934 map
= isl_map_restore_space(map
, space
);
936 return isl_map_reset_space(map
, isl_map_get_space(map
));
939 __isl_give isl_set
*isl_set_set_dim_id(__isl_take isl_set
*set
,
940 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
942 return isl_map_set_dim_id(set
, type
, pos
, id
);
945 int isl_map_find_dim_by_id(__isl_keep isl_map
*map
, enum isl_dim_type type
,
946 __isl_keep isl_id
*id
)
950 return isl_space_find_dim_by_id(map
->dim
, type
, id
);
953 int isl_set_find_dim_by_id(__isl_keep isl_set
*set
, enum isl_dim_type type
,
954 __isl_keep isl_id
*id
)
956 return isl_map_find_dim_by_id(set
, type
, id
);
959 /* Return the position of the dimension of the given type and name
961 * Return -1 if no such dimension can be found.
963 int isl_basic_map_find_dim_by_name(__isl_keep isl_basic_map
*bmap
,
964 enum isl_dim_type type
, const char *name
)
968 return isl_space_find_dim_by_name(bmap
->dim
, type
, name
);
971 int isl_map_find_dim_by_name(__isl_keep isl_map
*map
, enum isl_dim_type type
,
976 return isl_space_find_dim_by_name(map
->dim
, type
, name
);
979 int isl_set_find_dim_by_name(__isl_keep isl_set
*set
, enum isl_dim_type type
,
982 return isl_map_find_dim_by_name(set
, type
, name
);
985 /* Check whether equality i of bset is a pure stride constraint
986 * on a single dimension, i.e., of the form
990 * with k a constant and e an existentially quantified variable.
992 isl_bool
isl_basic_set_eq_is_stride(__isl_keep isl_basic_set
*bset
, int i
)
1000 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
1001 d
= isl_basic_set_dim(bset
, isl_dim_set
);
1002 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
1003 if (nparam
< 0 || d
< 0 || n_div
< 0)
1004 return isl_bool_error
;
1006 if (!isl_int_is_zero(bset
->eq
[i
][0]))
1007 return isl_bool_false
;
1009 if (isl_seq_first_non_zero(bset
->eq
[i
] + 1, nparam
) != -1)
1010 return isl_bool_false
;
1011 pos1
= isl_seq_first_non_zero(bset
->eq
[i
] + 1 + nparam
, d
);
1013 return isl_bool_false
;
1014 if (isl_seq_first_non_zero(bset
->eq
[i
] + 1 + nparam
+ pos1
+ 1,
1015 d
- pos1
- 1) != -1)
1016 return isl_bool_false
;
1018 pos2
= isl_seq_first_non_zero(bset
->eq
[i
] + 1 + nparam
+ d
, n_div
);
1020 return isl_bool_false
;
1021 if (isl_seq_first_non_zero(bset
->eq
[i
] + 1 + nparam
+ d
+ pos2
+ 1,
1022 n_div
- pos2
- 1) != -1)
1023 return isl_bool_false
;
1024 if (!isl_int_is_one(bset
->eq
[i
][1 + nparam
+ pos1
]) &&
1025 !isl_int_is_negone(bset
->eq
[i
][1 + nparam
+ pos1
]))
1026 return isl_bool_false
;
1028 return isl_bool_true
;
1031 /* Reset the user pointer on all identifiers of parameters and tuples
1032 * of the space of "map".
1034 __isl_give isl_map
*isl_map_reset_user(__isl_take isl_map
*map
)
1038 space
= isl_map_get_space(map
);
1039 space
= isl_space_reset_user(space
);
1040 map
= isl_map_reset_space(map
, space
);
1045 /* Reset the user pointer on all identifiers of parameters and tuples
1046 * of the space of "set".
1048 __isl_give isl_set
*isl_set_reset_user(__isl_take isl_set
*set
)
1050 return isl_map_reset_user(set
);
1053 isl_bool
isl_basic_map_is_rational(__isl_keep isl_basic_map
*bmap
)
1056 return isl_bool_error
;
1057 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
1060 /* Has "map" been marked as a rational map?
1061 * In particular, have all basic maps in "map" been marked this way?
1062 * An empty map is not considered to be rational.
1063 * Maps where only some of the basic maps are marked rational
1066 isl_bool
isl_map_is_rational(__isl_keep isl_map
*map
)
1072 return isl_bool_error
;
1074 return isl_bool_false
;
1075 rational
= isl_basic_map_is_rational(map
->p
[0]);
1078 for (i
= 1; i
< map
->n
; ++i
) {
1079 isl_bool rational_i
;
1081 rational_i
= isl_basic_map_is_rational(map
->p
[i
]);
1084 if (rational
!= rational_i
)
1085 isl_die(isl_map_get_ctx(map
), isl_error_unsupported
,
1086 "mixed rational and integer basic maps "
1087 "not supported", return isl_bool_error
);
1093 /* Has "set" been marked as a rational set?
1094 * In particular, have all basic set in "set" been marked this way?
1095 * An empty set is not considered to be rational.
1096 * Sets where only some of the basic sets are marked rational
1099 isl_bool
isl_set_is_rational(__isl_keep isl_set
*set
)
1101 return isl_map_is_rational(set
);
1104 int isl_basic_set_is_rational(__isl_keep isl_basic_set
*bset
)
1106 return isl_basic_map_is_rational(bset
);
1109 /* Does "bmap" contain any rational points?
1111 * If "bmap" has an equality for each dimension, equating the dimension
1112 * to an integer constant, then it has no rational points, even if it
1113 * is marked as rational.
1115 isl_bool
isl_basic_map_has_rational(__isl_keep isl_basic_map
*bmap
)
1117 isl_bool has_rational
= isl_bool_true
;
1121 return isl_bool_error
;
1122 if (isl_basic_map_plain_is_empty(bmap
))
1123 return isl_bool_false
;
1124 if (!isl_basic_map_is_rational(bmap
))
1125 return isl_bool_false
;
1126 bmap
= isl_basic_map_copy(bmap
);
1127 bmap
= isl_basic_map_implicit_equalities(bmap
);
1128 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
1130 return isl_bool_error
;
1131 if (bmap
->n_eq
== total
) {
1133 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
1134 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1, total
);
1137 if (!isl_int_is_one(bmap
->eq
[i
][1 + j
]) &&
1138 !isl_int_is_negone(bmap
->eq
[i
][1 + j
]))
1140 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + j
+ 1,
1145 if (i
== bmap
->n_eq
)
1146 has_rational
= isl_bool_false
;
1148 isl_basic_map_free(bmap
);
1150 return has_rational
;
1153 /* Does "map" contain any rational points?
1155 isl_bool
isl_map_has_rational(__isl_keep isl_map
*map
)
1158 isl_bool has_rational
;
1161 return isl_bool_error
;
1162 for (i
= 0; i
< map
->n
; ++i
) {
1163 has_rational
= isl_basic_map_has_rational(map
->p
[i
]);
1164 if (has_rational
< 0 || has_rational
)
1165 return has_rational
;
1167 return isl_bool_false
;
1170 /* Does "set" contain any rational points?
1172 isl_bool
isl_set_has_rational(__isl_keep isl_set
*set
)
1174 return isl_map_has_rational(set
);
1177 /* Is this basic set a parameter domain?
1179 isl_bool
isl_basic_set_is_params(__isl_keep isl_basic_set
*bset
)
1182 return isl_bool_error
;
1183 return isl_space_is_params(bset
->dim
);
1186 /* Is this set a parameter domain?
1188 isl_bool
isl_set_is_params(__isl_keep isl_set
*set
)
1191 return isl_bool_error
;
1192 return isl_space_is_params(set
->dim
);
1195 /* Is this map actually a parameter domain?
1196 * Users should never call this function. Outside of isl,
1197 * a map can never be a parameter domain.
1199 isl_bool
isl_map_is_params(__isl_keep isl_map
*map
)
1202 return isl_bool_error
;
1203 return isl_space_is_params(map
->dim
);
1206 static __isl_give isl_basic_map
*basic_map_init(isl_ctx
*ctx
,
1207 __isl_take isl_basic_map
*bmap
, unsigned extra
,
1208 unsigned n_eq
, unsigned n_ineq
)
1211 isl_space
*space
= isl_basic_map_peek_space(bmap
);
1212 isl_size n_var
= isl_space_dim(space
, isl_dim_all
);
1213 size_t row_size
= 1 + n_var
+ extra
;
1219 return isl_basic_map_free(bmap
);
1221 bmap
->block
= isl_blk_alloc(ctx
, (n_ineq
+ n_eq
) * row_size
);
1222 if (isl_blk_is_error(bmap
->block
))
1225 bmap
->ineq
= isl_alloc_array(ctx
, isl_int
*, n_ineq
+ n_eq
);
1226 if ((n_ineq
+ n_eq
) && !bmap
->ineq
)
1230 bmap
->block2
= isl_blk_empty();
1233 bmap
->block2
= isl_blk_alloc(ctx
, extra
* (1 + row_size
));
1234 if (isl_blk_is_error(bmap
->block2
))
1237 bmap
->div
= isl_alloc_array(ctx
, isl_int
*, extra
);
1242 for (i
= 0; i
< n_ineq
+ n_eq
; ++i
)
1243 bmap
->ineq
[i
] = bmap
->block
.data
+ i
* row_size
;
1245 for (i
= 0; i
< extra
; ++i
)
1246 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + row_size
);
1250 bmap
->c_size
= n_eq
+ n_ineq
;
1251 bmap
->eq
= bmap
->ineq
+ n_ineq
;
1252 bmap
->extra
= extra
;
1256 bmap
->sample
= NULL
;
1260 isl_basic_map_free(bmap
);
1264 __isl_give isl_basic_set
*isl_basic_set_alloc(isl_ctx
*ctx
,
1265 unsigned nparam
, unsigned dim
, unsigned extra
,
1266 unsigned n_eq
, unsigned n_ineq
)
1268 struct isl_basic_map
*bmap
;
1271 space
= isl_space_set_alloc(ctx
, nparam
, dim
);
1275 bmap
= isl_basic_map_alloc_space(space
, extra
, n_eq
, n_ineq
);
1276 return bset_from_bmap(bmap
);
1279 __isl_give isl_basic_set
*isl_basic_set_alloc_space(__isl_take isl_space
*space
,
1280 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
1282 struct isl_basic_map
*bmap
;
1285 isl_assert(space
->ctx
, space
->n_in
== 0, goto error
);
1286 bmap
= isl_basic_map_alloc_space(space
, extra
, n_eq
, n_ineq
);
1287 return bset_from_bmap(bmap
);
1289 isl_space_free(space
);
1293 __isl_give isl_basic_map
*isl_basic_map_alloc_space(__isl_take isl_space
*space
,
1294 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
1296 struct isl_basic_map
*bmap
;
1300 bmap
= isl_calloc_type(space
->ctx
, struct isl_basic_map
);
1305 return basic_map_init(space
->ctx
, bmap
, extra
, n_eq
, n_ineq
);
1307 isl_space_free(space
);
1311 __isl_give isl_basic_map
*isl_basic_map_alloc(isl_ctx
*ctx
,
1312 unsigned nparam
, unsigned in
, unsigned out
, unsigned extra
,
1313 unsigned n_eq
, unsigned n_ineq
)
1315 struct isl_basic_map
*bmap
;
1318 space
= isl_space_alloc(ctx
, nparam
, in
, out
);
1322 bmap
= isl_basic_map_alloc_space(space
, extra
, n_eq
, n_ineq
);
1326 static __isl_give isl_basic_map
*dup_constraints(__isl_take isl_basic_map
*dst
,
1327 __isl_keep isl_basic_map
*src
)
1330 isl_size total
= isl_basic_map_dim(src
, isl_dim_all
);
1332 if (!dst
|| total
< 0)
1333 return isl_basic_map_free(dst
);
1335 for (i
= 0; i
< src
->n_eq
; ++i
) {
1336 int j
= isl_basic_map_alloc_equality(dst
);
1338 return isl_basic_map_free(dst
);
1339 isl_seq_cpy(dst
->eq
[j
], src
->eq
[i
], 1+total
);
1342 for (i
= 0; i
< src
->n_ineq
; ++i
) {
1343 int j
= isl_basic_map_alloc_inequality(dst
);
1345 return isl_basic_map_free(dst
);
1346 isl_seq_cpy(dst
->ineq
[j
], src
->ineq
[i
], 1+total
);
1349 for (i
= 0; i
< src
->n_div
; ++i
) {
1350 int j
= isl_basic_map_alloc_div(dst
);
1352 return isl_basic_map_free(dst
);
1353 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
);
1355 ISL_F_SET(dst
, ISL_BASIC_SET_FINAL
);
1359 __isl_give isl_basic_map
*isl_basic_map_dup(__isl_keep isl_basic_map
*bmap
)
1361 struct isl_basic_map
*dup
;
1365 dup
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
1366 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
1367 dup
= dup_constraints(dup
, bmap
);
1370 dup
->flags
= bmap
->flags
;
1371 dup
->sample
= isl_vec_copy(bmap
->sample
);
1375 __isl_give isl_basic_set
*isl_basic_set_dup(__isl_keep isl_basic_set
*bset
)
1377 struct isl_basic_map
*dup
;
1379 dup
= isl_basic_map_dup(bset_to_bmap(bset
));
1380 return bset_from_bmap(dup
);
1383 __isl_give isl_basic_set
*isl_basic_set_copy(__isl_keep isl_basic_set
*bset
)
1388 if (ISL_F_ISSET(bset
, ISL_BASIC_SET_FINAL
)) {
1392 return isl_basic_set_dup(bset
);
1395 __isl_give isl_set
*isl_set_copy(__isl_keep isl_set
*set
)
1404 __isl_give isl_basic_map
*isl_basic_map_copy(__isl_keep isl_basic_map
*bmap
)
1409 if (ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
)) {
1413 bmap
= isl_basic_map_dup(bmap
);
1415 ISL_F_SET(bmap
, ISL_BASIC_SET_FINAL
);
1419 __isl_give isl_map
*isl_map_copy(__isl_keep isl_map
*map
)
1428 __isl_null isl_basic_map
*isl_basic_map_free(__isl_take isl_basic_map
*bmap
)
1433 if (--bmap
->ref
> 0)
1436 isl_ctx_deref(bmap
->ctx
);
1438 isl_blk_free(bmap
->ctx
, bmap
->block2
);
1440 isl_blk_free(bmap
->ctx
, bmap
->block
);
1441 isl_vec_free(bmap
->sample
);
1442 isl_space_free(bmap
->dim
);
1448 __isl_null isl_basic_set
*isl_basic_set_free(__isl_take isl_basic_set
*bset
)
1450 return isl_basic_map_free(bset_to_bmap(bset
));
1453 static int room_for_con(__isl_keep isl_basic_map
*bmap
, unsigned n
)
1455 return bmap
->n_eq
+ bmap
->n_ineq
+ n
<= bmap
->c_size
;
1458 /* Check that "bset" does not involve any parameters.
1460 isl_stat
isl_basic_set_check_no_params(__isl_keep isl_basic_set
*bset
)
1464 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
1466 return isl_stat_error
;
1468 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
1469 "basic set should not have any parameters",
1470 return isl_stat_error
);
1474 /* Check that "bset" does not involve any local variables.
1476 isl_stat
isl_basic_set_check_no_locals(__isl_keep isl_basic_set
*bset
)
1480 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
1482 return isl_stat_error
;
1484 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
1485 "basic set should not have any local variables",
1486 return isl_stat_error
);
1491 #define TYPE isl_map
1493 #include "isl_check_named_params_templ.c"
1496 #define TYPE isl_basic_map
1499 #include "isl_check_named_params_templ.c"
1501 /* Check that "bmap1" and "bmap2" have the same parameters,
1502 * reporting an error if they do not.
1504 static isl_stat
isl_basic_map_check_equal_params(
1505 __isl_keep isl_basic_map
*bmap1
, __isl_keep isl_basic_map
*bmap2
)
1509 match
= isl_basic_map_has_equal_params(bmap1
, bmap2
);
1511 return isl_stat_error
;
1513 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
1514 "parameters don't match", return isl_stat_error
);
1519 #define TYPE isl_map
1521 #include "isl_align_params_bin_templ.c"
1526 #define ARG1 isl_map
1528 #define ARG2 isl_set
1530 #include "isl_align_params_templ.c"
1532 isl_bool
isl_map_align_params_map_map_and_test(__isl_keep isl_map
*map1
,
1533 __isl_keep isl_map
*map2
,
1534 isl_bool (*fn
)(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
))
1539 return isl_bool_error
;
1540 if (isl_map_has_equal_params(map1
, map2
))
1541 return fn(map1
, map2
);
1542 if (isl_map_check_named_params(map1
) < 0)
1543 return isl_bool_error
;
1544 if (isl_map_check_named_params(map2
) < 0)
1545 return isl_bool_error
;
1546 map1
= isl_map_copy(map1
);
1547 map2
= isl_map_copy(map2
);
1548 map1
= isl_map_align_params(map1
, isl_map_get_space(map2
));
1549 map2
= isl_map_align_params(map2
, isl_map_get_space(map1
));
1556 int isl_basic_map_alloc_equality(__isl_keep isl_basic_map
*bmap
)
1559 struct isl_ctx
*ctx
;
1561 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
1565 isl_assert(ctx
, room_for_con(bmap
, 1), return -1);
1566 isl_assert(ctx
, (bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
<= bmap
->c_size
,
1568 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1569 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1570 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1571 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1572 if ((bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
== bmap
->c_size
) {
1574 int j
= isl_basic_map_alloc_inequality(bmap
);
1578 bmap
->ineq
[j
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1579 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
1586 isl_seq_clr(bmap
->eq
[bmap
->n_eq
] + 1 + total
,
1587 bmap
->extra
- bmap
->n_div
);
1588 return bmap
->n_eq
++;
1591 int isl_basic_set_alloc_equality(__isl_keep isl_basic_set
*bset
)
1593 return isl_basic_map_alloc_equality(bset_to_bmap(bset
));
1596 __isl_give isl_basic_map
*isl_basic_map_free_equality(
1597 __isl_take isl_basic_map
*bmap
, unsigned n
)
1602 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
1603 "invalid number of equalities",
1604 isl_basic_map_free(bmap
));
1609 __isl_give isl_basic_set
*isl_basic_set_free_equality(
1610 __isl_take isl_basic_set
*bset
, unsigned n
)
1612 return bset_from_bmap(isl_basic_map_free_equality(bset_to_bmap(bset
),
1616 /* Drop the equality constraint at position "pos",
1617 * preserving the order of the other equality constraints.
1619 int isl_basic_map_drop_equality(__isl_keep isl_basic_map
*bmap
, unsigned pos
)
1626 isl_assert(bmap
->ctx
, pos
< bmap
->n_eq
, return -1);
1630 for (r
= pos
; r
< bmap
->n_eq
; ++r
)
1631 bmap
->eq
[r
] = bmap
->eq
[r
+ 1];
1632 bmap
->eq
[bmap
->n_eq
] = t
;
1637 /* Turn inequality "pos" of "bmap" into an equality.
1639 * In particular, we move the inequality in front of the equalities
1640 * and move the last inequality in the position of the moved inequality.
1641 * Note that isl_tab_make_equalities_explicit depends on this particular
1642 * change in the ordering of the constraints.
1644 void isl_basic_map_inequality_to_equality(
1645 __isl_keep isl_basic_map
*bmap
, unsigned pos
)
1649 t
= bmap
->ineq
[pos
];
1650 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1651 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
1656 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1657 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
1658 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1659 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1662 static int room_for_ineq(__isl_keep isl_basic_map
*bmap
, unsigned n
)
1664 return bmap
->n_ineq
+ n
<= bmap
->eq
- bmap
->ineq
;
1667 int isl_basic_map_alloc_inequality(__isl_keep isl_basic_map
*bmap
)
1670 struct isl_ctx
*ctx
;
1672 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
1676 isl_assert(ctx
, room_for_ineq(bmap
, 1), return -1);
1677 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1678 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1679 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
1680 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1681 isl_seq_clr(bmap
->ineq
[bmap
->n_ineq
] + 1 + total
,
1682 bmap
->extra
- bmap
->n_div
);
1683 return bmap
->n_ineq
++;
1686 int isl_basic_set_alloc_inequality(__isl_keep isl_basic_set
*bset
)
1688 return isl_basic_map_alloc_inequality(bset_to_bmap(bset
));
1691 __isl_give isl_basic_map
*isl_basic_map_free_inequality(
1692 __isl_take isl_basic_map
*bmap
, unsigned n
)
1696 if (n
> bmap
->n_ineq
)
1697 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
1698 "invalid number of inequalities",
1699 return isl_basic_map_free(bmap
));
1704 __isl_give isl_basic_set
*isl_basic_set_free_inequality(
1705 __isl_take isl_basic_set
*bset
, unsigned n
)
1707 return bset_from_bmap(isl_basic_map_free_inequality(bset_to_bmap(bset
),
1711 int isl_basic_map_drop_inequality(__isl_keep isl_basic_map
*bmap
, unsigned pos
)
1716 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
1718 if (pos
!= bmap
->n_ineq
- 1) {
1719 t
= bmap
->ineq
[pos
];
1720 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1721 bmap
->ineq
[bmap
->n_ineq
- 1] = t
;
1722 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
1728 int isl_basic_set_drop_inequality(__isl_keep isl_basic_set
*bset
, unsigned pos
)
1730 return isl_basic_map_drop_inequality(bset_to_bmap(bset
), pos
);
1733 __isl_give isl_basic_map
*isl_basic_map_add_eq(__isl_take isl_basic_map
*bmap
,
1740 empty
= isl_basic_map_plain_is_empty(bmap
);
1742 return isl_basic_map_free(bmap
);
1746 bmap
= isl_basic_map_cow(bmap
);
1747 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
1748 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
1750 return isl_basic_map_free(bmap
);
1751 k
= isl_basic_map_alloc_equality(bmap
);
1754 isl_seq_cpy(bmap
->eq
[k
], eq
, 1 + total
);
1757 isl_basic_map_free(bmap
);
1761 __isl_give isl_basic_set
*isl_basic_set_add_eq(__isl_take isl_basic_set
*bset
,
1764 return bset_from_bmap(isl_basic_map_add_eq(bset_to_bmap(bset
), eq
));
1767 __isl_give isl_basic_map
*isl_basic_map_add_ineq(__isl_take isl_basic_map
*bmap
,
1773 bmap
= isl_basic_map_cow(bmap
);
1774 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
1775 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
1777 return isl_basic_map_free(bmap
);
1778 k
= isl_basic_map_alloc_inequality(bmap
);
1781 isl_seq_cpy(bmap
->ineq
[k
], ineq
, 1 + total
);
1784 isl_basic_map_free(bmap
);
1788 __isl_give isl_basic_set
*isl_basic_set_add_ineq(__isl_take isl_basic_set
*bset
,
1791 return bset_from_bmap(isl_basic_map_add_ineq(bset_to_bmap(bset
), ineq
));
1794 int isl_basic_map_alloc_div(__isl_keep isl_basic_map
*bmap
)
1798 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
1801 isl_assert(bmap
->ctx
, bmap
->n_div
< bmap
->extra
, return -1);
1802 isl_seq_clr(bmap
->div
[bmap
->n_div
] + 1 + 1 + total
,
1803 bmap
->extra
- bmap
->n_div
);
1804 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1805 return bmap
->n_div
++;
1808 int isl_basic_set_alloc_div(__isl_keep isl_basic_set
*bset
)
1810 return isl_basic_map_alloc_div(bset_to_bmap(bset
));
1814 #define TYPE isl_basic_map
1815 #include "check_type_range_templ.c"
1817 /* Check that there are "n" dimensions of type "type" starting at "first"
1820 isl_stat
isl_basic_set_check_range(__isl_keep isl_basic_set
*bset
,
1821 enum isl_dim_type type
, unsigned first
, unsigned n
)
1823 return isl_basic_map_check_range(bset_to_bmap(bset
),
1827 /* Insert an extra integer division, prescribed by "div", to "bmap"
1828 * at (integer division) position "pos".
1830 * The integer division is first added at the end and then moved
1831 * into the right position.
1833 __isl_give isl_basic_map
*isl_basic_map_insert_div(
1834 __isl_take isl_basic_map
*bmap
, int pos
, __isl_keep isl_vec
*div
)
1839 bmap
= isl_basic_map_cow(bmap
);
1840 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
1841 if (total
< 0 || !div
)
1842 return isl_basic_map_free(bmap
);
1844 if (div
->size
!= 1 + 1 + total
)
1845 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
1846 "unexpected size", return isl_basic_map_free(bmap
));
1847 if (isl_basic_map_check_range(bmap
, isl_dim_div
, pos
, 0) < 0)
1848 return isl_basic_map_free(bmap
);
1850 bmap
= isl_basic_map_extend(bmap
, 1, 0, 2);
1851 k
= isl_basic_map_alloc_div(bmap
);
1853 return isl_basic_map_free(bmap
);
1854 isl_seq_cpy(bmap
->div
[k
], div
->el
, div
->size
);
1855 isl_int_set_si(bmap
->div
[k
][div
->size
], 0);
1857 for (i
= k
; i
> pos
; --i
)
1858 bmap
= isl_basic_map_swap_div(bmap
, i
, i
- 1);
1863 isl_stat
isl_basic_map_free_div(__isl_keep isl_basic_map
*bmap
, unsigned n
)
1866 return isl_stat_error
;
1867 isl_assert(bmap
->ctx
, n
<= bmap
->n_div
, return isl_stat_error
);
1872 static __isl_give isl_basic_map
*add_constraints(
1873 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
,
1874 unsigned i_pos
, unsigned o_pos
)
1876 isl_size total
, n_param
, n_in
, n_out
, n_div
;
1877 unsigned o_in
, o_out
;
1880 struct isl_dim_map
*dim_map
;
1882 space
= isl_basic_map_peek_space(bmap2
);
1883 if (!bmap1
|| !space
)
1886 total
= isl_basic_map_dim(bmap1
, isl_dim_all
);
1887 n_param
= isl_basic_map_dim(bmap2
, isl_dim_param
);
1888 n_in
= isl_basic_map_dim(bmap2
, isl_dim_in
);
1889 o_in
= isl_basic_map_offset(bmap1
, isl_dim_in
) - 1 + i_pos
;
1890 n_out
= isl_basic_map_dim(bmap2
, isl_dim_out
);
1891 o_out
= isl_basic_map_offset(bmap1
, isl_dim_out
) - 1 + o_pos
;
1892 n_div
= isl_basic_map_dim(bmap2
, isl_dim_div
);
1893 if (total
< 0 || n_param
< 0 || n_in
< 0 || n_out
< 0 || n_div
< 0)
1895 ctx
= isl_basic_map_get_ctx(bmap1
);
1896 dim_map
= isl_dim_map_alloc(ctx
, total
+ n_div
);
1897 isl_dim_map_dim_range(dim_map
, space
, isl_dim_param
, 0, n_param
, 0);
1898 isl_dim_map_dim_range(dim_map
, space
, isl_dim_in
, 0, n_in
, o_in
);
1899 isl_dim_map_dim_range(dim_map
, space
, isl_dim_out
, 0, n_out
, o_out
);
1900 isl_dim_map_div(dim_map
, bmap2
, total
);
1902 return isl_basic_map_add_constraints_dim_map(bmap1
, bmap2
, dim_map
);
1904 isl_basic_map_free(bmap1
);
1905 isl_basic_map_free(bmap2
);
1909 __isl_give isl_basic_map
*isl_basic_map_extend(__isl_take isl_basic_map
*base
,
1910 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
1913 struct isl_basic_map
*ext
;
1920 dims_ok
= base
->extra
>= base
->n_div
+ extra
;
1922 if (dims_ok
&& room_for_con(base
, n_eq
+ n_ineq
) &&
1923 room_for_ineq(base
, n_ineq
))
1926 extra
+= base
->extra
;
1928 n_ineq
+= base
->n_ineq
;
1930 space
= isl_basic_map_get_space(base
);
1931 ext
= isl_basic_map_alloc_space(space
, extra
, n_eq
, n_ineq
);
1936 ext
->sample
= isl_vec_copy(base
->sample
);
1937 flags
= base
->flags
;
1938 ext
= add_constraints(ext
, base
, 0, 0);
1941 ISL_F_CLR(ext
, ISL_BASIC_SET_FINAL
);
1947 isl_basic_map_free(base
);
1951 __isl_give isl_basic_set
*isl_basic_set_extend(__isl_take isl_basic_set
*base
,
1952 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
1954 return bset_from_bmap(isl_basic_map_extend(bset_to_bmap(base
),
1955 extra
, n_eq
, n_ineq
));
1958 __isl_give isl_basic_map
*isl_basic_map_extend_constraints(
1959 __isl_take isl_basic_map
*base
, unsigned n_eq
, unsigned n_ineq
)
1961 return isl_basic_map_extend(base
, 0, n_eq
, n_ineq
);
1964 __isl_give isl_basic_set
*isl_basic_set_extend_constraints(
1965 __isl_take isl_basic_set
*base
, unsigned n_eq
, unsigned n_ineq
)
1967 isl_basic_map
*bmap
= bset_to_bmap(base
);
1968 bmap
= isl_basic_map_extend_constraints(bmap
, n_eq
, n_ineq
);
1969 return bset_from_bmap(bmap
);
1972 __isl_give isl_basic_set
*isl_basic_set_cow(__isl_take isl_basic_set
*bset
)
1974 return bset_from_bmap(isl_basic_map_cow(bset_to_bmap(bset
)));
1977 __isl_give isl_basic_map
*isl_basic_map_cow(__isl_take isl_basic_map
*bmap
)
1982 if (bmap
->ref
> 1) {
1984 bmap
= isl_basic_map_dup(bmap
);
1987 ISL_F_CLR(bmap
, ISL_BASIC_SET_FINAL
);
1988 ISL_F_CLR(bmap
, ISL_BASIC_MAP_REDUCED_COEFFICIENTS
);
1993 /* Clear all cached information in "map", either because it is about
1994 * to be modified or because it is being freed.
1995 * Always return the same pointer that is passed in.
1996 * This is needed for the use in isl_map_free.
1998 static __isl_give isl_map
*clear_caches(__isl_take isl_map
*map
)
2000 isl_basic_map_free(map
->cached_simple_hull
[0]);
2001 isl_basic_map_free(map
->cached_simple_hull
[1]);
2002 map
->cached_simple_hull
[0] = NULL
;
2003 map
->cached_simple_hull
[1] = NULL
;
2007 __isl_give isl_set
*isl_set_cow(__isl_take isl_set
*set
)
2009 return isl_map_cow(set
);
2012 /* Return an isl_map that is equal to "map" and that has only
2013 * a single reference.
2015 * If the original input already has only one reference, then
2016 * simply return it, but clear all cached information, since
2017 * it may be rendered invalid by the operations that will be
2018 * performed on the result.
2020 * Otherwise, create a duplicate (without any cached information).
2022 __isl_give isl_map
*isl_map_cow(__isl_take isl_map
*map
)
2028 return clear_caches(map
);
2030 return isl_map_dup(map
);
2033 static void swap_vars(struct isl_blk blk
, isl_int
*a
,
2034 unsigned a_len
, unsigned b_len
)
2036 isl_seq_cpy(blk
.data
, a
+a_len
, b_len
);
2037 isl_seq_cpy(blk
.data
+b_len
, a
, a_len
);
2038 isl_seq_cpy(a
, blk
.data
, b_len
+a_len
);
2041 static __isl_give isl_basic_map
*isl_basic_map_swap_vars(
2042 __isl_take isl_basic_map
*bmap
, unsigned pos
, unsigned n1
, unsigned n2
)
2047 if (isl_basic_map_check_range(bmap
, isl_dim_all
, pos
- 1, n1
+ n2
) < 0)
2050 if (n1
== 0 || n2
== 0)
2053 bmap
= isl_basic_map_cow(bmap
);
2057 blk
= isl_blk_alloc(bmap
->ctx
, n1
+ n2
);
2058 if (isl_blk_is_error(blk
))
2061 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2063 bmap
->eq
[i
] + pos
, n1
, n2
);
2065 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2067 bmap
->ineq
[i
] + pos
, n1
, n2
);
2069 for (i
= 0; i
< bmap
->n_div
; ++i
)
2071 bmap
->div
[i
]+1 + pos
, n1
, n2
);
2073 isl_blk_free(bmap
->ctx
, blk
);
2075 ISL_F_CLR(bmap
, ISL_BASIC_SET_SORTED
);
2076 bmap
= isl_basic_map_gauss(bmap
, NULL
);
2077 return isl_basic_map_finalize(bmap
);
2079 isl_basic_map_free(bmap
);
2083 /* The given basic map has turned out to be empty.
2084 * Explicitly mark it as such and change the representation
2085 * to a canonical representation of the empty basic map.
2086 * Since the basic map has conflicting constraints,
2087 * it must have at least one constraint, except perhaps
2088 * if it was already explicitly marked as being empty.
2089 * Do nothing in the latter case.
2091 __isl_give isl_basic_map
*isl_basic_map_set_to_empty(
2092 __isl_take isl_basic_map
*bmap
)
2098 empty
= isl_basic_map_plain_is_empty(bmap
);
2100 return isl_basic_map_free(bmap
);
2103 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
2105 return isl_basic_map_free(bmap
);
2106 if (isl_basic_map_free_div(bmap
, bmap
->n_div
) < 0)
2107 return isl_basic_map_free(bmap
);
2108 bmap
= isl_basic_map_free_inequality(bmap
, bmap
->n_ineq
);
2111 if (bmap
->n_eq
> 0) {
2112 bmap
= isl_basic_map_free_equality(bmap
, bmap
->n_eq
- 1);
2116 i
= isl_basic_map_alloc_equality(bmap
);
2120 isl_int_set_si(bmap
->eq
[i
][0], 1);
2121 isl_seq_clr(bmap
->eq
[i
]+1, total
);
2122 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
2123 isl_vec_free(bmap
->sample
);
2124 bmap
->sample
= NULL
;
2125 return isl_basic_map_finalize(bmap
);
2127 isl_basic_map_free(bmap
);
2131 __isl_give isl_basic_set
*isl_basic_set_set_to_empty(
2132 __isl_take isl_basic_set
*bset
)
2134 return bset_from_bmap(isl_basic_map_set_to_empty(bset_to_bmap(bset
)));
2137 __isl_give isl_basic_map
*isl_basic_map_set_rational(
2138 __isl_take isl_basic_map
*bmap
)
2143 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
2146 bmap
= isl_basic_map_cow(bmap
);
2150 ISL_F_SET(bmap
, ISL_BASIC_MAP_RATIONAL
);
2152 return isl_basic_map_finalize(bmap
);
2155 __isl_give isl_basic_set
*isl_basic_set_set_rational(
2156 __isl_take isl_basic_set
*bset
)
2158 return isl_basic_map_set_rational(bset
);
2161 __isl_give isl_basic_set
*isl_basic_set_set_integral(
2162 __isl_take isl_basic_set
*bset
)
2167 if (!ISL_F_ISSET(bset
, ISL_BASIC_MAP_RATIONAL
))
2170 bset
= isl_basic_set_cow(bset
);
2174 ISL_F_CLR(bset
, ISL_BASIC_MAP_RATIONAL
);
2176 return isl_basic_set_finalize(bset
);
2179 __isl_give isl_map
*isl_map_set_rational(__isl_take isl_map
*map
)
2183 map
= isl_map_cow(map
);
2186 for (i
= 0; i
< map
->n
; ++i
) {
2187 map
->p
[i
] = isl_basic_map_set_rational(map
->p
[i
]);
2197 __isl_give isl_set
*isl_set_set_rational(__isl_take isl_set
*set
)
2199 return isl_map_set_rational(set
);
2202 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
2205 static void swap_div(__isl_keep isl_basic_map
*bmap
, int a
, int b
)
2207 isl_int
*t
= bmap
->div
[a
];
2208 bmap
->div
[a
] = bmap
->div
[b
];
2212 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
2213 * div definitions accordingly.
2215 __isl_give isl_basic_map
*isl_basic_map_swap_div(__isl_take isl_basic_map
*bmap
,
2221 off
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
2223 return isl_basic_map_free(bmap
);
2225 swap_div(bmap
, a
, b
);
2227 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2228 isl_int_swap(bmap
->eq
[i
][1+off
+a
], bmap
->eq
[i
][1+off
+b
]);
2230 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2231 isl_int_swap(bmap
->ineq
[i
][1+off
+a
], bmap
->ineq
[i
][1+off
+b
]);
2233 for (i
= 0; i
< bmap
->n_div
; ++i
)
2234 isl_int_swap(bmap
->div
[i
][1+1+off
+a
], bmap
->div
[i
][1+1+off
+b
]);
2235 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
2240 static void constraint_drop_vars(isl_int
*c
, unsigned n
, unsigned rem
)
2242 isl_seq_cpy(c
, c
+ n
, rem
);
2243 isl_seq_clr(c
+ rem
, n
);
2246 /* Drop n dimensions starting at first.
2248 * In principle, this frees up some extra variables as the number
2249 * of columns remains constant, but we would have to extend
2250 * the div array too as the number of rows in this array is assumed
2251 * to be equal to extra.
2253 __isl_give isl_basic_set
*isl_basic_set_drop_dims(
2254 __isl_take isl_basic_set
*bset
, unsigned first
, unsigned n
)
2256 return isl_basic_map_drop(bset_to_bmap(bset
), isl_dim_set
, first
, n
);
2259 /* Move "n" divs starting at "first" to the end of the list of divs.
2261 static struct isl_basic_map
*move_divs_last(struct isl_basic_map
*bmap
,
2262 unsigned first
, unsigned n
)
2267 if (first
+ n
== bmap
->n_div
)
2270 div
= isl_alloc_array(bmap
->ctx
, isl_int
*, n
);
2273 for (i
= 0; i
< n
; ++i
)
2274 div
[i
] = bmap
->div
[first
+ i
];
2275 for (i
= 0; i
< bmap
->n_div
- first
- n
; ++i
)
2276 bmap
->div
[first
+ i
] = bmap
->div
[first
+ n
+ i
];
2277 for (i
= 0; i
< n
; ++i
)
2278 bmap
->div
[bmap
->n_div
- n
+ i
] = div
[i
];
2282 isl_basic_map_free(bmap
);
2287 #define TYPE isl_map
2289 #include "check_type_range_templ.c"
2291 /* Check that there are "n" dimensions of type "type" starting at "first"
2294 static isl_stat
isl_set_check_range(__isl_keep isl_set
*set
,
2295 enum isl_dim_type type
, unsigned first
, unsigned n
)
2297 return isl_map_check_range(set_to_map(set
), type
, first
, n
);
2300 /* Drop "n" dimensions of type "type" starting at "first".
2301 * Perform the core computation, without cowing or
2302 * simplifying and finalizing the result.
2304 * In principle, this frees up some extra variables as the number
2305 * of columns remains constant, but we would have to extend
2306 * the div array too as the number of rows in this array is assumed
2307 * to be equal to extra.
2309 __isl_give isl_basic_map
*isl_basic_map_drop_core(
2310 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
2311 unsigned first
, unsigned n
)
2318 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2319 return isl_basic_map_free(bmap
);
2321 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
2323 return isl_basic_map_free(bmap
);
2325 offset
= isl_basic_map_offset(bmap
, type
) + first
;
2326 left
= total
- (offset
- 1) - n
;
2327 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2328 constraint_drop_vars(bmap
->eq
[i
]+offset
, n
, left
);
2330 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2331 constraint_drop_vars(bmap
->ineq
[i
]+offset
, n
, left
);
2333 for (i
= 0; i
< bmap
->n_div
; ++i
)
2334 constraint_drop_vars(bmap
->div
[i
]+1+offset
, n
, left
);
2336 if (type
== isl_dim_div
) {
2337 bmap
= move_divs_last(bmap
, first
, n
);
2340 if (isl_basic_map_free_div(bmap
, n
) < 0)
2341 return isl_basic_map_free(bmap
);
2343 bmap
->dim
= isl_space_drop_dims(bmap
->dim
, type
, first
, n
);
2345 return isl_basic_map_free(bmap
);
2347 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
2348 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
2352 /* Drop "n" dimensions of type "type" starting at "first".
2354 * In principle, this frees up some extra variables as the number
2355 * of columns remains constant, but we would have to extend
2356 * the div array too as the number of rows in this array is assumed
2357 * to be equal to extra.
2359 __isl_give isl_basic_map
*isl_basic_map_drop(__isl_take isl_basic_map
*bmap
,
2360 enum isl_dim_type type
, unsigned first
, unsigned n
)
2364 if (n
== 0 && !isl_space_is_named_or_nested(bmap
->dim
, type
))
2367 bmap
= isl_basic_map_cow(bmap
);
2371 bmap
= isl_basic_map_drop_core(bmap
, type
, first
, n
);
2373 bmap
= isl_basic_map_simplify(bmap
);
2374 return isl_basic_map_finalize(bmap
);
2377 __isl_give isl_basic_set
*isl_basic_set_drop(__isl_take isl_basic_set
*bset
,
2378 enum isl_dim_type type
, unsigned first
, unsigned n
)
2380 return bset_from_bmap(isl_basic_map_drop(bset_to_bmap(bset
),
2384 /* No longer consider "map" to be normalized.
2386 static __isl_give isl_map
*isl_map_unmark_normalized(__isl_take isl_map
*map
)
2390 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
2394 __isl_give isl_map
*isl_map_drop(__isl_take isl_map
*map
,
2395 enum isl_dim_type type
, unsigned first
, unsigned n
)
2400 if (isl_map_check_range(map
, type
, first
, n
) < 0)
2401 return isl_map_free(map
);
2403 if (n
== 0 && !isl_space_is_named_or_nested(map
->dim
, type
))
2405 map
= isl_map_cow(map
);
2409 for (i
= 0; i
< map
->n
; ++i
) {
2410 map
->p
[i
] = isl_basic_map_drop(map
->p
[i
], type
, first
, n
);
2414 map
= isl_map_unmark_normalized(map
);
2416 space
= isl_map_take_space(map
);
2417 space
= isl_space_drop_dims(space
, type
, first
, n
);
2418 map
= isl_map_restore_space(map
, space
);
2426 __isl_give isl_set
*isl_set_drop(__isl_take isl_set
*set
,
2427 enum isl_dim_type type
, unsigned first
, unsigned n
)
2429 return set_from_map(isl_map_drop(set_to_map(set
), type
, first
, n
));
2432 /* Drop the integer division at position "div", which is assumed
2433 * not to appear in any of the constraints or
2434 * in any of the other integer divisions.
2436 * Since the integer division is redundant, there is no need to cow.
2438 __isl_give isl_basic_map
*isl_basic_map_drop_div(
2439 __isl_take isl_basic_map
*bmap
, unsigned div
)
2441 return isl_basic_map_drop_core(bmap
, isl_dim_div
, div
, 1);
2444 /* Eliminate the specified n dimensions starting at first from the
2445 * constraints, without removing the dimensions from the space.
2446 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2448 __isl_give isl_map
*isl_map_eliminate(__isl_take isl_map
*map
,
2449 enum isl_dim_type type
, unsigned first
, unsigned n
)
2456 if (isl_map_check_range(map
, type
, first
, n
) < 0)
2457 return isl_map_free(map
);
2459 map
= isl_map_cow(map
);
2463 for (i
= 0; i
< map
->n
; ++i
) {
2464 map
->p
[i
] = isl_basic_map_eliminate(map
->p
[i
], type
, first
, n
);
2474 /* Eliminate the specified n dimensions starting at first from the
2475 * constraints, without removing the dimensions from the space.
2476 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2478 __isl_give isl_set
*isl_set_eliminate(__isl_take isl_set
*set
,
2479 enum isl_dim_type type
, unsigned first
, unsigned n
)
2481 return set_from_map(isl_map_eliminate(set_to_map(set
), type
, first
, n
));
2484 /* Eliminate the specified n dimensions starting at first from the
2485 * constraints, without removing the dimensions from the space.
2486 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2488 __isl_give isl_set
*isl_set_eliminate_dims(__isl_take isl_set
*set
,
2489 unsigned first
, unsigned n
)
2491 return isl_set_eliminate(set
, isl_dim_set
, first
, n
);
2494 __isl_give isl_basic_map
*isl_basic_map_remove_divs(
2495 __isl_take isl_basic_map
*bmap
)
2499 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
2501 return isl_basic_map_free(bmap
);
2502 bmap
= isl_basic_map_eliminate_vars(bmap
, v_div
, bmap
->n_div
);
2506 return isl_basic_map_finalize(bmap
);
2509 __isl_give isl_basic_set
*isl_basic_set_remove_divs(
2510 __isl_take isl_basic_set
*bset
)
2512 return bset_from_bmap(isl_basic_map_remove_divs(bset_to_bmap(bset
)));
2515 __isl_give isl_map
*isl_map_remove_divs(__isl_take isl_map
*map
)
2524 map
= isl_map_cow(map
);
2528 for (i
= 0; i
< map
->n
; ++i
) {
2529 map
->p
[i
] = isl_basic_map_remove_divs(map
->p
[i
]);
2539 __isl_give isl_set
*isl_set_remove_divs(__isl_take isl_set
*set
)
2541 return isl_map_remove_divs(set
);
2544 __isl_give isl_basic_map
*isl_basic_map_remove_dims(
2545 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
2546 unsigned first
, unsigned n
)
2548 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2549 return isl_basic_map_free(bmap
);
2550 if (n
== 0 && !isl_space_is_named_or_nested(bmap
->dim
, type
))
2552 bmap
= isl_basic_map_eliminate_vars(bmap
,
2553 isl_basic_map_offset(bmap
, type
) - 1 + first
, n
);
2556 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
) && type
== isl_dim_div
)
2558 bmap
= isl_basic_map_drop(bmap
, type
, first
, n
);
2562 /* Return true if the definition of the given div (recursively) involves
2563 * any of the given variables.
2565 static isl_bool
div_involves_vars(__isl_keep isl_basic_map
*bmap
, int div
,
2566 unsigned first
, unsigned n
)
2569 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
2571 if (isl_int_is_zero(bmap
->div
[div
][0]))
2572 return isl_bool_false
;
2573 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + first
, n
) >= 0)
2574 return isl_bool_true
;
2576 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2579 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
2581 involves
= div_involves_vars(bmap
, i
, first
, n
);
2582 if (involves
< 0 || involves
)
2586 return isl_bool_false
;
2589 /* Try and add a lower and/or upper bound on "div" to "bmap"
2590 * based on inequality "i".
2591 * "total" is the total number of variables (excluding the divs).
2592 * "v" is a temporary object that can be used during the calculations.
2593 * If "lb" is set, then a lower bound should be constructed.
2594 * If "ub" is set, then an upper bound should be constructed.
2596 * The calling function has already checked that the inequality does not
2597 * reference "div", but we still need to check that the inequality is
2598 * of the right form. We'll consider the case where we want to construct
2599 * a lower bound. The construction of upper bounds is similar.
2601 * Let "div" be of the form
2603 * q = floor((a + f(x))/d)
2605 * We essentially check if constraint "i" is of the form
2609 * so that we can use it to derive a lower bound on "div".
2610 * However, we allow a slightly more general form
2614 * with the condition that the coefficients of g(x) - f(x) are all
2616 * Rewriting this constraint as
2620 * adding a + f(x) to both sides and dividing by d, we obtain
2622 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
2624 * Taking the floor on both sides, we obtain
2626 * q >= floor((a-b)/d) + (f(x)-g(x))/d
2630 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
2632 * In the case of an upper bound, we construct the constraint
2634 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
2637 static __isl_give isl_basic_map
*insert_bounds_on_div_from_ineq(
2638 __isl_take isl_basic_map
*bmap
, int div
, int i
,
2639 unsigned total
, isl_int v
, int lb
, int ub
)
2643 for (j
= 0; (lb
|| ub
) && j
< total
+ bmap
->n_div
; ++j
) {
2645 isl_int_sub(v
, bmap
->ineq
[i
][1 + j
],
2646 bmap
->div
[div
][1 + 1 + j
]);
2647 lb
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
2650 isl_int_add(v
, bmap
->ineq
[i
][1 + j
],
2651 bmap
->div
[div
][1 + 1 + j
]);
2652 ub
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
2658 bmap
= isl_basic_map_cow(bmap
);
2659 bmap
= isl_basic_map_extend_constraints(bmap
, 0, lb
+ ub
);
2661 int k
= isl_basic_map_alloc_inequality(bmap
);
2664 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
2665 isl_int_sub(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
2666 bmap
->div
[div
][1 + j
]);
2667 isl_int_cdiv_q(bmap
->ineq
[k
][j
],
2668 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
2670 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], 1);
2673 int k
= isl_basic_map_alloc_inequality(bmap
);
2676 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
2677 isl_int_add(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
2678 bmap
->div
[div
][1 + j
]);
2679 isl_int_fdiv_q(bmap
->ineq
[k
][j
],
2680 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
2682 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], -1);
2687 isl_basic_map_free(bmap
);
2691 /* This function is called right before "div" is eliminated from "bmap"
2692 * using Fourier-Motzkin.
2693 * Look through the constraints of "bmap" for constraints on the argument
2694 * of the integer division and use them to construct constraints on the
2695 * integer division itself. These constraints can then be combined
2696 * during the Fourier-Motzkin elimination.
2697 * Note that it is only useful to introduce lower bounds on "div"
2698 * if "bmap" already contains upper bounds on "div" as the newly
2699 * introduce lower bounds can then be combined with the pre-existing
2700 * upper bounds. Similarly for upper bounds.
2701 * We therefore first check if "bmap" contains any lower and/or upper bounds
2704 * It is interesting to note that the introduction of these constraints
2705 * can indeed lead to more accurate results, even when compared to
2706 * deriving constraints on the argument of "div" from constraints on "div".
2707 * Consider, for example, the set
2709 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2711 * The second constraint can be rewritten as
2713 * 2 * [(-i-2j+3)/4] + k >= 0
2715 * from which we can derive
2717 * -i - 2j + 3 >= -2k
2723 * Combined with the first constraint, we obtain
2725 * -3 <= 3 + 2k or k >= -3
2727 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2728 * the first constraint, we obtain
2730 * [(i + 2j)/4] >= [-3/4] = -1
2732 * Combining this constraint with the second constraint, we obtain
2736 static __isl_give isl_basic_map
*insert_bounds_on_div(
2737 __isl_take isl_basic_map
*bmap
, int div
)
2740 int check_lb
, check_ub
;
2747 if (isl_int_is_zero(bmap
->div
[div
][0]))
2750 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
2752 return isl_basic_map_free(bmap
);
2756 for (i
= 0; (!check_lb
|| !check_ub
) && i
< bmap
->n_ineq
; ++i
) {
2757 int s
= isl_int_sgn(bmap
->ineq
[i
][1 + v_div
+ div
]);
2764 if (!check_lb
&& !check_ub
)
2769 for (i
= 0; bmap
&& i
< bmap
->n_ineq
; ++i
) {
2770 if (!isl_int_is_zero(bmap
->ineq
[i
][1 + v_div
+ div
]))
2773 bmap
= insert_bounds_on_div_from_ineq(bmap
, div
, i
, v_div
, v
,
2774 check_lb
, check_ub
);
2782 /* Remove all divs (recursively) involving any of the given dimensions
2783 * in their definitions.
2785 __isl_give isl_basic_map
*isl_basic_map_remove_divs_involving_dims(
2786 __isl_take isl_basic_map
*bmap
,
2787 enum isl_dim_type type
, unsigned first
, unsigned n
)
2791 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2792 return isl_basic_map_free(bmap
);
2793 first
+= isl_basic_map_offset(bmap
, type
);
2795 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2798 involves
= div_involves_vars(bmap
, i
, first
, n
);
2800 return isl_basic_map_free(bmap
);
2803 bmap
= insert_bounds_on_div(bmap
, i
);
2804 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
2813 __isl_give isl_basic_set
*isl_basic_set_remove_divs_involving_dims(
2814 __isl_take isl_basic_set
*bset
,
2815 enum isl_dim_type type
, unsigned first
, unsigned n
)
2817 return isl_basic_map_remove_divs_involving_dims(bset
, type
, first
, n
);
2820 __isl_give isl_map
*isl_map_remove_divs_involving_dims(__isl_take isl_map
*map
,
2821 enum isl_dim_type type
, unsigned first
, unsigned n
)
2830 map
= isl_map_cow(map
);
2834 for (i
= 0; i
< map
->n
; ++i
) {
2835 map
->p
[i
] = isl_basic_map_remove_divs_involving_dims(map
->p
[i
],
2846 __isl_give isl_set
*isl_set_remove_divs_involving_dims(__isl_take isl_set
*set
,
2847 enum isl_dim_type type
, unsigned first
, unsigned n
)
2849 return set_from_map(isl_map_remove_divs_involving_dims(set_to_map(set
),
2853 /* Does the description of "bmap" depend on the specified dimensions?
2854 * We also check whether the dimensions appear in any of the div definitions.
2855 * In principle there is no need for this check. If the dimensions appear
2856 * in a div definition, they also appear in the defining constraints of that
2859 isl_bool
isl_basic_map_involves_dims(__isl_keep isl_basic_map
*bmap
,
2860 enum isl_dim_type type
, unsigned first
, unsigned n
)
2864 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2865 return isl_bool_error
;
2867 first
+= isl_basic_map_offset(bmap
, type
);
2868 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2869 if (isl_seq_first_non_zero(bmap
->eq
[i
] + first
, n
) >= 0)
2870 return isl_bool_true
;
2871 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2872 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + first
, n
) >= 0)
2873 return isl_bool_true
;
2874 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2875 if (isl_int_is_zero(bmap
->div
[i
][0]))
2877 if (isl_seq_first_non_zero(bmap
->div
[i
] + 1 + first
, n
) >= 0)
2878 return isl_bool_true
;
2881 return isl_bool_false
;
2884 isl_bool
isl_map_involves_dims(__isl_keep isl_map
*map
,
2885 enum isl_dim_type type
, unsigned first
, unsigned n
)
2889 if (isl_map_check_range(map
, type
, first
, n
) < 0)
2890 return isl_bool_error
;
2892 for (i
= 0; i
< map
->n
; ++i
) {
2893 isl_bool involves
= isl_basic_map_involves_dims(map
->p
[i
],
2895 if (involves
< 0 || involves
)
2899 return isl_bool_false
;
2902 isl_bool
isl_basic_set_involves_dims(__isl_keep isl_basic_set
*bset
,
2903 enum isl_dim_type type
, unsigned first
, unsigned n
)
2905 return isl_basic_map_involves_dims(bset
, type
, first
, n
);
2908 isl_bool
isl_set_involves_dims(__isl_keep isl_set
*set
,
2909 enum isl_dim_type type
, unsigned first
, unsigned n
)
2911 return isl_map_involves_dims(set
, type
, first
, n
);
2914 /* Drop all constraints in bmap that involve any of the dimensions
2915 * first to first+n-1.
2916 * This function only performs the actual removal of constraints.
2918 * This function should not call finalize since it is used by
2919 * remove_redundant_divs, which in turn is called by isl_basic_map_finalize.
2921 __isl_give isl_basic_map
*isl_basic_map_drop_constraints_involving(
2922 __isl_take isl_basic_map
*bmap
, unsigned first
, unsigned n
)
2929 bmap
= isl_basic_map_cow(bmap
);
2934 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
) {
2935 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + first
, n
) == -1)
2937 if (isl_basic_map_drop_equality(bmap
, i
) < 0)
2938 return isl_basic_map_free(bmap
);
2941 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
2942 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + 1 + first
, n
) == -1)
2944 if (isl_basic_map_drop_inequality(bmap
, i
) < 0)
2945 return isl_basic_map_free(bmap
);
2951 /* Drop all constraints in bset that involve any of the dimensions
2952 * first to first+n-1.
2953 * This function only performs the actual removal of constraints.
2955 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_involving(
2956 __isl_take isl_basic_set
*bset
, unsigned first
, unsigned n
)
2958 return isl_basic_map_drop_constraints_involving(bset
, first
, n
);
2961 /* Drop all constraints in bmap that do not involve any of the dimensions
2962 * first to first + n - 1 of the given type.
2964 __isl_give isl_basic_map
*isl_basic_map_drop_constraints_not_involving_dims(
2965 __isl_take isl_basic_map
*bmap
,
2966 enum isl_dim_type type
, unsigned first
, unsigned n
)
2971 isl_space
*space
= isl_basic_map_get_space(bmap
);
2972 isl_basic_map_free(bmap
);
2973 return isl_basic_map_universe(space
);
2975 bmap
= isl_basic_map_cow(bmap
);
2979 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2980 return isl_basic_map_free(bmap
);
2982 first
+= isl_basic_map_offset(bmap
, type
) - 1;
2984 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
) {
2985 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + first
, n
) != -1)
2987 if (isl_basic_map_drop_equality(bmap
, i
) < 0)
2988 return isl_basic_map_free(bmap
);
2991 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
2992 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + 1 + first
, n
) != -1)
2994 if (isl_basic_map_drop_inequality(bmap
, i
) < 0)
2995 return isl_basic_map_free(bmap
);
2998 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
3002 /* Drop all constraints in bset that do not involve any of the dimensions
3003 * first to first + n - 1 of the given type.
3005 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_not_involving_dims(
3006 __isl_take isl_basic_set
*bset
,
3007 enum isl_dim_type type
, unsigned first
, unsigned n
)
3009 return isl_basic_map_drop_constraints_not_involving_dims(bset
,
3013 /* Drop all constraints in bmap that involve any of the dimensions
3014 * first to first + n - 1 of the given type.
3016 __isl_give isl_basic_map
*isl_basic_map_drop_constraints_involving_dims(
3017 __isl_take isl_basic_map
*bmap
,
3018 enum isl_dim_type type
, unsigned first
, unsigned n
)
3025 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
3026 return isl_basic_map_free(bmap
);
3028 bmap
= isl_basic_map_remove_divs_involving_dims(bmap
, type
, first
, n
);
3029 first
+= isl_basic_map_offset(bmap
, type
) - 1;
3030 bmap
= isl_basic_map_drop_constraints_involving(bmap
, first
, n
);
3031 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
3035 /* Drop all constraints in bset that involve any of the dimensions
3036 * first to first + n - 1 of the given type.
3038 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_involving_dims(
3039 __isl_take isl_basic_set
*bset
,
3040 enum isl_dim_type type
, unsigned first
, unsigned n
)
3042 return isl_basic_map_drop_constraints_involving_dims(bset
,
3046 /* Drop constraints from "map" by applying "drop" to each basic map.
3048 static __isl_give isl_map
*drop_constraints(__isl_take isl_map
*map
,
3049 enum isl_dim_type type
, unsigned first
, unsigned n
,
3050 __isl_give isl_basic_map
*(*drop
)(__isl_take isl_basic_map
*bmap
,
3051 enum isl_dim_type type
, unsigned first
, unsigned n
))
3055 if (isl_map_check_range(map
, type
, first
, n
) < 0)
3056 return isl_map_free(map
);
3058 map
= isl_map_cow(map
);
3062 for (i
= 0; i
< map
->n
; ++i
) {
3063 map
->p
[i
] = drop(map
->p
[i
], type
, first
, n
);
3065 return isl_map_free(map
);
3069 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
3074 /* Drop all constraints in map that involve any of the dimensions
3075 * first to first + n - 1 of the given type.
3077 __isl_give isl_map
*isl_map_drop_constraints_involving_dims(
3078 __isl_take isl_map
*map
,
3079 enum isl_dim_type type
, unsigned first
, unsigned n
)
3083 return drop_constraints(map
, type
, first
, n
,
3084 &isl_basic_map_drop_constraints_involving_dims
);
3087 /* Drop all constraints in "map" that do not involve any of the dimensions
3088 * first to first + n - 1 of the given type.
3090 __isl_give isl_map
*isl_map_drop_constraints_not_involving_dims(
3091 __isl_take isl_map
*map
,
3092 enum isl_dim_type type
, unsigned first
, unsigned n
)
3095 isl_space
*space
= isl_map_get_space(map
);
3097 return isl_map_universe(space
);
3099 return drop_constraints(map
, type
, first
, n
,
3100 &isl_basic_map_drop_constraints_not_involving_dims
);
3103 /* Drop all constraints in set that involve any of the dimensions
3104 * first to first + n - 1 of the given type.
3106 __isl_give isl_set
*isl_set_drop_constraints_involving_dims(
3107 __isl_take isl_set
*set
,
3108 enum isl_dim_type type
, unsigned first
, unsigned n
)
3110 return isl_map_drop_constraints_involving_dims(set
, type
, first
, n
);
3113 /* Drop all constraints in "set" that do not involve any of the dimensions
3114 * first to first + n - 1 of the given type.
3116 __isl_give isl_set
*isl_set_drop_constraints_not_involving_dims(
3117 __isl_take isl_set
*set
,
3118 enum isl_dim_type type
, unsigned first
, unsigned n
)
3120 return isl_map_drop_constraints_not_involving_dims(set
, type
, first
, n
);
3123 /* Does local variable "div" of "bmap" have a complete explicit representation?
3124 * Having a complete explicit representation requires not only
3125 * an explicit representation, but also that all local variables
3126 * that appear in this explicit representation in turn have
3127 * a complete explicit representation.
3129 isl_bool
isl_basic_map_div_is_known(__isl_keep isl_basic_map
*bmap
, int div
)
3132 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
3135 marked
= isl_basic_map_div_is_marked_unknown(bmap
, div
);
3136 if (marked
< 0 || marked
)
3137 return isl_bool_not(marked
);
3139 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
3142 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
3144 known
= isl_basic_map_div_is_known(bmap
, i
);
3145 if (known
< 0 || !known
)
3149 return isl_bool_true
;
3152 /* Remove all divs that are unknown or defined in terms of unknown divs.
3154 __isl_give isl_basic_map
*isl_basic_map_remove_unknown_divs(
3155 __isl_take isl_basic_map
*bmap
)
3162 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
3163 if (isl_basic_map_div_is_known(bmap
, i
))
3165 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
3174 /* Remove all divs that are unknown or defined in terms of unknown divs.
3176 __isl_give isl_basic_set
*isl_basic_set_remove_unknown_divs(
3177 __isl_take isl_basic_set
*bset
)
3179 return isl_basic_map_remove_unknown_divs(bset
);
3182 __isl_give isl_map
*isl_map_remove_unknown_divs(__isl_take isl_map
*map
)
3191 map
= isl_map_cow(map
);
3195 for (i
= 0; i
< map
->n
; ++i
) {
3196 map
->p
[i
] = isl_basic_map_remove_unknown_divs(map
->p
[i
]);
3206 __isl_give isl_set
*isl_set_remove_unknown_divs(__isl_take isl_set
*set
)
3208 return set_from_map(isl_map_remove_unknown_divs(set_to_map(set
)));
3211 __isl_give isl_basic_set
*isl_basic_set_remove_dims(
3212 __isl_take isl_basic_set
*bset
,
3213 enum isl_dim_type type
, unsigned first
, unsigned n
)
3215 isl_basic_map
*bmap
= bset_to_bmap(bset
);
3216 bmap
= isl_basic_map_remove_dims(bmap
, type
, first
, n
);
3217 return bset_from_bmap(bmap
);
3220 __isl_give isl_map
*isl_map_remove_dims(__isl_take isl_map
*map
,
3221 enum isl_dim_type type
, unsigned first
, unsigned n
)
3228 map
= isl_map_cow(map
);
3229 if (isl_map_check_range(map
, type
, first
, n
) < 0)
3230 return isl_map_free(map
);
3232 for (i
= 0; i
< map
->n
; ++i
) {
3233 map
->p
[i
] = isl_basic_map_eliminate_vars(map
->p
[i
],
3234 isl_basic_map_offset(map
->p
[i
], type
) - 1 + first
, n
);
3238 map
= isl_map_drop(map
, type
, first
, n
);
3245 __isl_give isl_set
*isl_set_remove_dims(__isl_take isl_set
*bset
,
3246 enum isl_dim_type type
, unsigned first
, unsigned n
)
3248 return set_from_map(isl_map_remove_dims(set_to_map(bset
),
3252 /* Project out n inputs starting at first using Fourier-Motzkin */
3253 __isl_give isl_map
*isl_map_remove_inputs(__isl_take isl_map
*map
,
3254 unsigned first
, unsigned n
)
3256 return isl_map_remove_dims(map
, isl_dim_in
, first
, n
);
3259 void isl_basic_set_print_internal(__isl_keep isl_basic_set
*bset
,
3260 FILE *out
, int indent
)
3265 fprintf(out
, "null basic set\n");
3269 fprintf(out
, "%*s", indent
, "");
3270 fprintf(out
, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
3271 bset
->ref
, bset
->dim
->nparam
, bset
->dim
->n_out
,
3272 bset
->extra
, bset
->flags
);
3274 p
= isl_printer_to_file(isl_basic_set_get_ctx(bset
), out
);
3275 p
= isl_printer_set_dump(p
, 1);
3276 p
= isl_printer_set_indent(p
, indent
);
3277 p
= isl_printer_start_line(p
);
3278 p
= isl_printer_print_basic_set(p
, bset
);
3279 p
= isl_printer_end_line(p
);
3280 isl_printer_free(p
);
3283 void isl_basic_map_print_internal(__isl_keep isl_basic_map
*bmap
,
3284 FILE *out
, int indent
)
3289 fprintf(out
, "null basic map\n");
3293 fprintf(out
, "%*s", indent
, "");
3294 fprintf(out
, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
3295 "flags: %x, n_name: %d\n",
3297 bmap
->dim
->nparam
, bmap
->dim
->n_in
, bmap
->dim
->n_out
,
3298 bmap
->extra
, bmap
->flags
, bmap
->dim
->n_id
);
3300 p
= isl_printer_to_file(isl_basic_map_get_ctx(bmap
), out
);
3301 p
= isl_printer_set_dump(p
, 1);
3302 p
= isl_printer_set_indent(p
, indent
);
3303 p
= isl_printer_start_line(p
);
3304 p
= isl_printer_print_basic_map(p
, bmap
);
3305 p
= isl_printer_end_line(p
);
3306 isl_printer_free(p
);
3309 __isl_give isl_basic_map
*isl_inequality_negate(__isl_take isl_basic_map
*bmap
,
3314 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
3316 return isl_basic_map_free(bmap
);
3317 if (pos
>= bmap
->n_ineq
)
3318 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
3319 "invalid position", return isl_basic_map_free(bmap
));
3320 isl_seq_neg(bmap
->ineq
[pos
], bmap
->ineq
[pos
], 1 + total
);
3321 isl_int_sub_ui(bmap
->ineq
[pos
][0], bmap
->ineq
[pos
][0], 1);
3322 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
3323 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
3327 __isl_give isl_set
*isl_set_alloc_space(__isl_take isl_space
*space
, int n
,
3330 if (isl_space_check_is_set(space
) < 0)
3332 return isl_map_alloc_space(space
, n
, flags
);
3334 isl_space_free(space
);
3338 /* Make sure "map" has room for at least "n" more basic maps.
3340 __isl_give isl_map
*isl_map_grow(__isl_take isl_map
*map
, int n
)
3343 struct isl_map
*grown
= NULL
;
3347 isl_assert(map
->ctx
, n
>= 0, goto error
);
3348 if (map
->n
+ n
<= map
->size
)
3350 grown
= isl_map_alloc_space(isl_map_get_space(map
), map
->n
+ n
, map
->flags
);
3353 for (i
= 0; i
< map
->n
; ++i
) {
3354 grown
->p
[i
] = isl_basic_map_copy(map
->p
[i
]);
3362 isl_map_free(grown
);
3367 /* Make sure "set" has room for at least "n" more basic sets.
3369 __isl_give isl_set
*isl_set_grow(__isl_take isl_set
*set
, int n
)
3371 return set_from_map(isl_map_grow(set_to_map(set
), n
));
3374 __isl_give isl_set
*isl_set_from_basic_set(__isl_take isl_basic_set
*bset
)
3376 return isl_map_from_basic_map(bset
);
3379 __isl_give isl_map
*isl_map_from_basic_map(__isl_take isl_basic_map
*bmap
)
3381 struct isl_map
*map
;
3386 map
= isl_map_alloc_space(isl_space_copy(bmap
->dim
), 1, ISL_MAP_DISJOINT
);
3387 return isl_map_add_basic_map(map
, bmap
);
3390 __isl_give isl_set
*isl_set_add_basic_set(__isl_take isl_set
*set
,
3391 __isl_take isl_basic_set
*bset
)
3393 return set_from_map(isl_map_add_basic_map(set_to_map(set
),
3394 bset_to_bmap(bset
)));
3397 __isl_null isl_set
*isl_set_free(__isl_take isl_set
*set
)
3399 return isl_map_free(set
);
3402 void isl_set_print_internal(__isl_keep isl_set
*set
, FILE *out
, int indent
)
3407 fprintf(out
, "null set\n");
3411 fprintf(out
, "%*s", indent
, "");
3412 fprintf(out
, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
3413 set
->ref
, set
->n
, set
->dim
->nparam
, set
->dim
->n_out
,
3415 for (i
= 0; i
< set
->n
; ++i
) {
3416 fprintf(out
, "%*s", indent
, "");
3417 fprintf(out
, "basic set %d:\n", i
);
3418 isl_basic_set_print_internal(set
->p
[i
], out
, indent
+4);
3422 void isl_map_print_internal(__isl_keep isl_map
*map
, FILE *out
, int indent
)
3427 fprintf(out
, "null map\n");
3431 fprintf(out
, "%*s", indent
, "");
3432 fprintf(out
, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
3433 "flags: %x, n_name: %d\n",
3434 map
->ref
, map
->n
, map
->dim
->nparam
, map
->dim
->n_in
,
3435 map
->dim
->n_out
, map
->flags
, map
->dim
->n_id
);
3436 for (i
= 0; i
< map
->n
; ++i
) {
3437 fprintf(out
, "%*s", indent
, "");
3438 fprintf(out
, "basic map %d:\n", i
);
3439 isl_basic_map_print_internal(map
->p
[i
], out
, indent
+4);
3443 /* Check that the space of "bset" is the same as that of the domain of "bmap".
3445 static isl_stat
isl_basic_map_check_compatible_domain(
3446 __isl_keep isl_basic_map
*bmap
, __isl_keep isl_basic_set
*bset
)
3450 ok
= isl_basic_map_compatible_domain(bmap
, bset
);
3452 return isl_stat_error
;
3454 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
3455 "incompatible spaces", return isl_stat_error
);
3460 __isl_give isl_basic_map
*isl_basic_map_intersect_domain(
3461 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*bset
)
3463 struct isl_basic_map
*bmap_domain
;
3466 if (isl_basic_map_check_equal_params(bmap
, bset_to_bmap(bset
)) < 0)
3469 dim
= isl_basic_set_dim(bset
, isl_dim_set
);
3473 isl_basic_map_check_compatible_domain(bmap
, bset
) < 0)
3476 bmap
= isl_basic_map_cow(bmap
);
3479 bmap
= isl_basic_map_extend(bmap
,
3480 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
3481 bmap_domain
= isl_basic_map_from_domain(bset
);
3482 bmap
= add_constraints(bmap
, bmap_domain
, 0, 0);
3484 bmap
= isl_basic_map_simplify(bmap
);
3485 return isl_basic_map_finalize(bmap
);
3487 isl_basic_map_free(bmap
);
3488 isl_basic_set_free(bset
);
3492 /* Check that the space of "bset" is the same as that of the range of "bmap".
3494 static isl_stat
isl_basic_map_check_compatible_range(
3495 __isl_keep isl_basic_map
*bmap
, __isl_keep isl_basic_set
*bset
)
3499 ok
= isl_basic_map_compatible_range(bmap
, bset
);
3501 return isl_stat_error
;
3503 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
3504 "incompatible spaces", return isl_stat_error
);
3509 __isl_give isl_basic_map
*isl_basic_map_intersect_range(
3510 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*bset
)
3512 struct isl_basic_map
*bmap_range
;
3515 if (isl_basic_map_check_equal_params(bmap
, bset_to_bmap(bset
)) < 0)
3518 dim
= isl_basic_set_dim(bset
, isl_dim_set
);
3521 if (dim
!= 0 && isl_basic_map_check_compatible_range(bmap
, bset
) < 0)
3524 if (isl_basic_set_plain_is_universe(bset
)) {
3525 isl_basic_set_free(bset
);
3529 bmap
= isl_basic_map_cow(bmap
);
3532 bmap
= isl_basic_map_extend(bmap
,
3533 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
3534 bmap_range
= bset_to_bmap(bset
);
3535 bmap
= add_constraints(bmap
, bmap_range
, 0, 0);
3537 bmap
= isl_basic_map_simplify(bmap
);
3538 return isl_basic_map_finalize(bmap
);
3540 isl_basic_map_free(bmap
);
3541 isl_basic_set_free(bset
);
3545 isl_bool
isl_basic_map_contains(__isl_keep isl_basic_map
*bmap
,
3546 __isl_keep isl_vec
*vec
)
3552 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
3553 if (total
< 0 || !vec
)
3554 return isl_bool_error
;
3556 if (1 + total
!= vec
->size
)
3557 return isl_bool_false
;
3561 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
3562 isl_seq_inner_product(vec
->el
, bmap
->eq
[i
], 1 + total
, &s
);
3563 if (!isl_int_is_zero(s
)) {
3565 return isl_bool_false
;
3569 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
3570 isl_seq_inner_product(vec
->el
, bmap
->ineq
[i
], 1 + total
, &s
);
3571 if (isl_int_is_neg(s
)) {
3573 return isl_bool_false
;
3579 return isl_bool_true
;
3582 isl_bool
isl_basic_set_contains(__isl_keep isl_basic_set
*bset
,
3583 __isl_keep isl_vec
*vec
)
3585 return isl_basic_map_contains(bset_to_bmap(bset
), vec
);
3588 __isl_give isl_basic_map
*isl_basic_map_intersect(
3589 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
3591 struct isl_vec
*sample
= NULL
;
3592 isl_space
*space1
, *space2
;
3593 isl_size dim1
, dim2
, nparam1
, nparam2
;
3595 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
3597 space1
= isl_basic_map_peek_space(bmap1
);
3598 space2
= isl_basic_map_peek_space(bmap2
);
3599 dim1
= isl_space_dim(space1
, isl_dim_all
);
3600 dim2
= isl_space_dim(space2
, isl_dim_all
);
3601 nparam1
= isl_space_dim(space1
, isl_dim_param
);
3602 nparam2
= isl_space_dim(space2
, isl_dim_param
);
3603 if (dim1
< 0 || dim2
< 0 || nparam1
< 0 || nparam2
< 0)
3605 if (dim1
== nparam1
&& dim2
!= nparam2
)
3606 return isl_basic_map_intersect(bmap2
, bmap1
);
3608 if (dim2
!= nparam2
&&
3609 isl_basic_map_check_equal_space(bmap1
, bmap2
) < 0)
3612 if (isl_basic_map_plain_is_empty(bmap1
)) {
3613 isl_basic_map_free(bmap2
);
3616 if (isl_basic_map_plain_is_empty(bmap2
)) {
3617 isl_basic_map_free(bmap1
);
3621 if (bmap1
->sample
&&
3622 isl_basic_map_contains(bmap1
, bmap1
->sample
) > 0 &&
3623 isl_basic_map_contains(bmap2
, bmap1
->sample
) > 0)
3624 sample
= isl_vec_copy(bmap1
->sample
);
3625 else if (bmap2
->sample
&&
3626 isl_basic_map_contains(bmap1
, bmap2
->sample
) > 0 &&
3627 isl_basic_map_contains(bmap2
, bmap2
->sample
) > 0)
3628 sample
= isl_vec_copy(bmap2
->sample
);
3630 bmap1
= isl_basic_map_cow(bmap1
);
3633 bmap1
= isl_basic_map_extend(bmap1
,
3634 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
3635 bmap1
= add_constraints(bmap1
, bmap2
, 0, 0);
3638 isl_vec_free(sample
);
3640 isl_vec_free(bmap1
->sample
);
3641 bmap1
->sample
= sample
;
3644 bmap1
= isl_basic_map_simplify(bmap1
);
3645 return isl_basic_map_finalize(bmap1
);
3648 isl_vec_free(sample
);
3649 isl_basic_map_free(bmap1
);
3650 isl_basic_map_free(bmap2
);
3654 __isl_give isl_basic_set
*isl_basic_set_intersect(
3655 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
3657 return bset_from_bmap(isl_basic_map_intersect(bset_to_bmap(bset1
),
3658 bset_to_bmap(bset2
)));
3661 __isl_give isl_basic_set
*isl_basic_set_intersect_params(
3662 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
3664 return isl_basic_set_intersect(bset1
, bset2
);
3667 /* Special case of isl_map_intersect, where both map1 and map2
3668 * are convex, without any divs and such that either map1 or map2
3669 * contains a single constraint. This constraint is then simply
3670 * added to the other map.
3672 static __isl_give isl_map
*map_intersect_add_constraint(
3673 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
3675 isl_assert(map1
->ctx
, map1
->n
== 1, goto error
);
3676 isl_assert(map2
->ctx
, map1
->n
== 1, goto error
);
3677 isl_assert(map1
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
3678 isl_assert(map2
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
3680 if (map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
!= 1)
3681 return isl_map_intersect(map2
, map1
);
3683 map1
= isl_map_cow(map1
);
3686 if (isl_map_plain_is_empty(map1
)) {
3690 if (map2
->p
[0]->n_eq
== 1)
3691 map1
->p
[0] = isl_basic_map_add_eq(map1
->p
[0], map2
->p
[0]->eq
[0]);
3693 map1
->p
[0] = isl_basic_map_add_ineq(map1
->p
[0],
3694 map2
->p
[0]->ineq
[0]);
3696 map1
->p
[0] = isl_basic_map_simplify(map1
->p
[0]);
3697 map1
->p
[0] = isl_basic_map_finalize(map1
->p
[0]);
3701 if (isl_basic_map_plain_is_empty(map1
->p
[0])) {
3702 isl_basic_map_free(map1
->p
[0]);
3708 map1
= isl_map_unmark_normalized(map1
);
3716 /* map2 may be either a parameter domain or a map living in the same
3719 static __isl_give isl_map
*map_intersect_internal(__isl_take isl_map
*map1
,
3720 __isl_take isl_map
*map2
)
3726 isl_size dim2
, nparam2
;
3731 if ((isl_map_plain_is_empty(map1
) ||
3732 isl_map_plain_is_universe(map2
)) &&
3733 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
3737 if ((isl_map_plain_is_empty(map2
) ||
3738 isl_map_plain_is_universe(map1
)) &&
3739 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
3744 if (map1
->n
== 1 && map2
->n
== 1 &&
3745 map1
->p
[0]->n_div
== 0 && map2
->p
[0]->n_div
== 0 &&
3746 isl_space_is_equal(map1
->dim
, map2
->dim
) &&
3747 (map1
->p
[0]->n_eq
+ map1
->p
[0]->n_ineq
== 1 ||
3748 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1))
3749 return map_intersect_add_constraint(map1
, map2
);
3751 equal
= isl_map_plain_is_equal(map1
, map2
);
3759 dim2
= isl_map_dim(map2
, isl_dim_all
);
3760 nparam2
= isl_map_dim(map2
, isl_dim_param
);
3761 if (dim2
< 0 || nparam2
< 0)
3763 if (dim2
!= nparam2
)
3764 isl_assert(map1
->ctx
,
3765 isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
3767 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
3768 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
3769 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
3771 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
3772 map1
->n
* map2
->n
, flags
);
3775 for (i
= 0; i
< map1
->n
; ++i
)
3776 for (j
= 0; j
< map2
->n
; ++j
) {
3777 struct isl_basic_map
*part
;
3778 part
= isl_basic_map_intersect(
3779 isl_basic_map_copy(map1
->p
[i
]),
3780 isl_basic_map_copy(map2
->p
[j
]));
3781 if (isl_basic_map_is_empty(part
) < 0)
3782 part
= isl_basic_map_free(part
);
3783 result
= isl_map_add_basic_map(result
, part
);
3796 static __isl_give isl_map
*map_intersect(__isl_take isl_map
*map1
,
3797 __isl_take isl_map
*map2
)
3799 if (isl_map_check_equal_space(map1
, map2
) < 0)
3801 return map_intersect_internal(map1
, map2
);
3808 __isl_give isl_map
*isl_map_intersect(__isl_take isl_map
*map1
,
3809 __isl_take isl_map
*map2
)
3811 isl_map_align_params_bin(&map1
, &map2
);
3812 return map_intersect(map1
, map2
);
3815 __isl_give isl_set
*isl_set_intersect(__isl_take isl_set
*set1
,
3816 __isl_take isl_set
*set2
)
3818 return set_from_map(isl_map_intersect(set_to_map(set1
),
3822 /* map_intersect_internal accepts intersections
3823 * with parameter domains, so we can just call that function.
3825 __isl_give isl_map
*isl_map_intersect_params(__isl_take isl_map
*map
,
3826 __isl_take isl_set
*params
)
3828 isl_map_align_params_set(&map
, ¶ms
);
3829 return map_intersect_internal(map
, params
);
3832 __isl_give isl_set
*isl_set_intersect_params(__isl_take isl_set
*set
,
3833 __isl_take isl_set
*params
)
3835 return isl_map_intersect_params(set
, params
);
3838 __isl_give isl_basic_map
*isl_basic_map_reverse(__isl_take isl_basic_map
*bmap
)
3846 bmap
= isl_basic_map_cow(bmap
);
3849 space
= isl_space_reverse(isl_space_copy(bmap
->dim
));
3850 pos
= isl_basic_map_offset(bmap
, isl_dim_in
);
3851 n1
= isl_basic_map_dim(bmap
, isl_dim_in
);
3852 n2
= isl_basic_map_dim(bmap
, isl_dim_out
);
3853 if (n1
< 0 || n2
< 0)
3854 bmap
= isl_basic_map_free(bmap
);
3855 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
3856 return isl_basic_map_reset_space(bmap
, space
);
3859 /* Given a basic map A -> (B -> C), return the corresponding basic map
3862 static __isl_give isl_basic_map
*isl_basic_map_range_reverse(
3863 __isl_take isl_basic_map
*bmap
)
3866 isl_size offset
, n1
, n2
;
3868 space
= isl_basic_map_peek_space(bmap
);
3869 if (isl_space_check_range_is_wrapping(space
) < 0)
3870 return isl_basic_map_free(bmap
);
3871 offset
= isl_basic_map_var_offset(bmap
, isl_dim_out
);
3872 n1
= isl_space_wrapped_dim(space
, isl_dim_out
, isl_dim_in
);
3873 n2
= isl_space_wrapped_dim(space
, isl_dim_out
, isl_dim_out
);
3874 if (offset
< 0 || n1
< 0 || n2
< 0)
3875 return isl_basic_map_free(bmap
);
3877 bmap
= isl_basic_map_swap_vars(bmap
, 1 + offset
, n1
, n2
);
3879 space
= isl_basic_map_take_space(bmap
);
3880 space
= isl_space_range_reverse(space
);
3881 bmap
= isl_basic_map_restore_space(bmap
, space
);
3886 static __isl_give isl_basic_map
*basic_map_space_reset(
3887 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
)
3893 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
3896 space
= isl_basic_map_get_space(bmap
);
3897 space
= isl_space_reset(space
, type
);
3898 bmap
= isl_basic_map_reset_space(bmap
, space
);
3902 __isl_give isl_basic_map
*isl_basic_map_insert_dims(
3903 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
3904 unsigned pos
, unsigned n
)
3906 isl_bool rational
, is_empty
;
3907 isl_space
*res_space
;
3908 struct isl_basic_map
*res
;
3909 struct isl_dim_map
*dim_map
;
3912 enum isl_dim_type t
;
3915 return basic_map_space_reset(bmap
, type
);
3917 is_empty
= isl_basic_map_plain_is_empty(bmap
);
3918 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
3919 if (is_empty
< 0 || total
< 0)
3920 return isl_basic_map_free(bmap
);
3921 res_space
= isl_space_insert_dims(isl_basic_map_get_space(bmap
),
3924 return isl_basic_map_free(bmap
);
3926 isl_basic_map_free(bmap
);
3927 return isl_basic_map_empty(res_space
);
3930 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
+ n
);
3932 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3936 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3938 isl_size size
= isl_basic_map_dim(bmap
, t
);
3940 dim_map
= isl_dim_map_free(dim_map
);
3941 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3943 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3944 pos
, size
- pos
, off
+ pos
+ n
);
3946 dim
= isl_space_dim(res_space
, t
);
3948 dim_map
= isl_dim_map_free(dim_map
);
3951 isl_dim_map_div(dim_map
, bmap
, off
);
3953 res
= isl_basic_map_alloc_space(res_space
,
3954 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3955 rational
= isl_basic_map_is_rational(bmap
);
3957 res
= isl_basic_map_free(res
);
3959 res
= isl_basic_map_set_rational(res
);
3960 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3961 return isl_basic_map_finalize(res
);
3964 __isl_give isl_basic_set
*isl_basic_set_insert_dims(
3965 __isl_take isl_basic_set
*bset
,
3966 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3968 return isl_basic_map_insert_dims(bset
, type
, pos
, n
);
3971 __isl_give isl_basic_map
*isl_basic_map_add_dims(__isl_take isl_basic_map
*bmap
,
3972 enum isl_dim_type type
, unsigned n
)
3976 dim
= isl_basic_map_dim(bmap
, type
);
3978 return isl_basic_map_free(bmap
);
3979 return isl_basic_map_insert_dims(bmap
, type
, dim
, n
);
3982 __isl_give isl_basic_set
*isl_basic_set_add_dims(__isl_take isl_basic_set
*bset
,
3983 enum isl_dim_type type
, unsigned n
)
3987 isl_assert(bset
->ctx
, type
!= isl_dim_in
, goto error
);
3988 return isl_basic_map_add_dims(bset
, type
, n
);
3990 isl_basic_set_free(bset
);
3994 static __isl_give isl_map
*map_space_reset(__isl_take isl_map
*map
,
3995 enum isl_dim_type type
)
3999 if (!map
|| !isl_space_is_named_or_nested(map
->dim
, type
))
4002 space
= isl_map_get_space(map
);
4003 space
= isl_space_reset(space
, type
);
4004 map
= isl_map_reset_space(map
, space
);
4008 __isl_give isl_map
*isl_map_insert_dims(__isl_take isl_map
*map
,
4009 enum isl_dim_type type
, unsigned pos
, unsigned n
)
4015 return map_space_reset(map
, type
);
4017 map
= isl_map_cow(map
);
4021 for (i
= 0; i
< map
->n
; ++i
) {
4022 map
->p
[i
] = isl_basic_map_insert_dims(map
->p
[i
], type
, pos
, n
);
4027 space
= isl_map_take_space(map
);
4028 space
= isl_space_insert_dims(space
, type
, pos
, n
);
4029 map
= isl_map_restore_space(map
, space
);
4037 __isl_give isl_set
*isl_set_insert_dims(__isl_take isl_set
*set
,
4038 enum isl_dim_type type
, unsigned pos
, unsigned n
)
4040 return isl_map_insert_dims(set
, type
, pos
, n
);
4043 __isl_give isl_map
*isl_map_add_dims(__isl_take isl_map
*map
,
4044 enum isl_dim_type type
, unsigned n
)
4048 dim
= isl_map_dim(map
, type
);
4050 return isl_map_free(map
);
4051 return isl_map_insert_dims(map
, type
, dim
, n
);
4054 __isl_give isl_set
*isl_set_add_dims(__isl_take isl_set
*set
,
4055 enum isl_dim_type type
, unsigned n
)
4059 isl_assert(set
->ctx
, type
!= isl_dim_in
, goto error
);
4060 return set_from_map(isl_map_add_dims(set_to_map(set
), type
, n
));
4066 __isl_give isl_basic_map
*isl_basic_map_move_dims(
4067 __isl_take isl_basic_map
*bmap
,
4068 enum isl_dim_type dst_type
, unsigned dst_pos
,
4069 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
4072 struct isl_dim_map
*dim_map
;
4073 struct isl_basic_map
*res
;
4074 enum isl_dim_type t
;
4081 bmap
= isl_basic_map_reset(bmap
, src_type
);
4082 bmap
= isl_basic_map_reset(bmap
, dst_type
);
4086 if (isl_basic_map_check_range(bmap
, src_type
, src_pos
, n
) < 0)
4087 return isl_basic_map_free(bmap
);
4089 if (dst_type
== src_type
&& dst_pos
== src_pos
)
4092 isl_assert(bmap
->ctx
, dst_type
!= src_type
, goto error
);
4094 if (pos(bmap
->dim
, dst_type
) + dst_pos
==
4095 pos(bmap
->dim
, src_type
) + src_pos
+
4096 ((src_type
< dst_type
) ? n
: 0)) {
4097 space
= isl_basic_map_take_space(bmap
);
4098 space
= isl_space_move_dims(space
, dst_type
, dst_pos
,
4099 src_type
, src_pos
, n
);
4100 bmap
= isl_basic_map_restore_space(bmap
, space
);
4101 bmap
= isl_basic_map_finalize(bmap
);
4106 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4108 return isl_basic_map_free(bmap
);
4109 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
4112 space
= isl_basic_map_peek_space(bmap
);
4113 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
4114 isl_size size
= isl_space_dim(space
, t
);
4116 dim_map
= isl_dim_map_free(dim_map
);
4117 if (t
== dst_type
) {
4118 isl_dim_map_dim_range(dim_map
, space
, t
,
4121 isl_dim_map_dim_range(dim_map
, space
, src_type
,
4124 isl_dim_map_dim_range(dim_map
, space
, t
,
4125 dst_pos
, size
- dst_pos
, off
);
4126 off
+= size
- dst_pos
;
4127 } else if (t
== src_type
) {
4128 isl_dim_map_dim_range(dim_map
, space
, t
,
4131 isl_dim_map_dim_range(dim_map
, space
, t
,
4132 src_pos
+ n
, size
- src_pos
- n
, off
);
4133 off
+= size
- src_pos
- n
;
4135 isl_dim_map_dim(dim_map
, space
, t
, off
);
4139 isl_dim_map_div(dim_map
, bmap
, off
);
4141 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
4142 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
4143 bmap
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
4144 space
= isl_basic_map_take_space(bmap
);
4145 space
= isl_space_move_dims(space
, dst_type
, dst_pos
,
4146 src_type
, src_pos
, n
);
4147 bmap
= isl_basic_map_restore_space(bmap
, space
);
4151 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
4152 bmap
= isl_basic_map_gauss(bmap
, NULL
);
4153 bmap
= isl_basic_map_finalize(bmap
);
4157 isl_basic_map_free(bmap
);
4161 __isl_give isl_basic_set
*isl_basic_set_move_dims(__isl_take isl_basic_set
*bset
,
4162 enum isl_dim_type dst_type
, unsigned dst_pos
,
4163 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
4165 isl_basic_map
*bmap
= bset_to_bmap(bset
);
4166 bmap
= isl_basic_map_move_dims(bmap
, dst_type
, dst_pos
,
4167 src_type
, src_pos
, n
);
4168 return bset_from_bmap(bmap
);
4171 __isl_give isl_set
*isl_set_move_dims(__isl_take isl_set
*set
,
4172 enum isl_dim_type dst_type
, unsigned dst_pos
,
4173 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
4177 isl_assert(set
->ctx
, dst_type
!= isl_dim_in
, goto error
);
4178 return set_from_map(isl_map_move_dims(set_to_map(set
),
4179 dst_type
, dst_pos
, src_type
, src_pos
, n
));
4185 __isl_give isl_map
*isl_map_move_dims(__isl_take isl_map
*map
,
4186 enum isl_dim_type dst_type
, unsigned dst_pos
,
4187 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
4193 map
= isl_map_reset(map
, src_type
);
4194 map
= isl_map_reset(map
, dst_type
);
4198 if (isl_map_check_range(map
, src_type
, src_pos
, n
))
4199 return isl_map_free(map
);
4201 if (dst_type
== src_type
&& dst_pos
== src_pos
)
4204 isl_assert(map
->ctx
, dst_type
!= src_type
, goto error
);
4206 map
= isl_map_cow(map
);
4210 for (i
= 0; i
< map
->n
; ++i
) {
4211 map
->p
[i
] = isl_basic_map_move_dims(map
->p
[i
],
4213 src_type
, src_pos
, n
);
4218 space
= isl_map_take_space(map
);
4219 space
= isl_space_move_dims(space
, dst_type
, dst_pos
,
4220 src_type
, src_pos
, n
);
4221 map
= isl_map_restore_space(map
, space
);
4229 /* Move the specified dimensions to the last columns right before
4230 * the divs. Don't change the dimension specification of bmap.
4231 * That's the responsibility of the caller.
4233 static __isl_give isl_basic_map
*move_last(__isl_take isl_basic_map
*bmap
,
4234 enum isl_dim_type type
, unsigned first
, unsigned n
)
4237 struct isl_dim_map
*dim_map
;
4238 struct isl_basic_map
*res
;
4239 enum isl_dim_type t
;
4245 if (isl_basic_map_offset(bmap
, type
) + first
+ n
==
4246 isl_basic_map_offset(bmap
, isl_dim_div
))
4249 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4251 return isl_basic_map_free(bmap
);
4252 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
4255 space
= isl_basic_map_peek_space(bmap
);
4256 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
4257 isl_size size
= isl_space_dim(space
, t
);
4259 dim_map
= isl_dim_map_free(dim_map
);
4261 isl_dim_map_dim_range(dim_map
, space
, t
,
4264 isl_dim_map_dim_range(dim_map
, space
, t
,
4265 first
, n
, total
- bmap
->n_div
- n
);
4266 isl_dim_map_dim_range(dim_map
, space
, t
,
4267 first
+ n
, size
- (first
+ n
), off
);
4268 off
+= size
- (first
+ n
);
4270 isl_dim_map_dim(dim_map
, space
, t
, off
);
4274 isl_dim_map_div(dim_map
, bmap
, off
+ n
);
4276 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
4277 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
4278 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
4282 /* Insert "n" rows in the divs of "bmap".
4284 * The number of columns is not changed, which means that the last
4285 * dimensions of "bmap" are being reintepreted as the new divs.
4286 * The space of "bmap" is not adjusted, however, which means
4287 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
4288 * from the space of "bmap" is the responsibility of the caller.
4290 static __isl_give isl_basic_map
*insert_div_rows(__isl_take isl_basic_map
*bmap
,
4298 bmap
= isl_basic_map_cow(bmap
);
4302 row_size
= isl_basic_map_offset(bmap
, isl_dim_div
) + bmap
->extra
;
4303 old
= bmap
->block2
.data
;
4304 bmap
->block2
= isl_blk_extend(bmap
->ctx
, bmap
->block2
,
4305 (bmap
->extra
+ n
) * (1 + row_size
));
4306 if (!bmap
->block2
.data
)
4307 return isl_basic_map_free(bmap
);
4308 new_div
= isl_alloc_array(bmap
->ctx
, isl_int
*, bmap
->extra
+ n
);
4310 return isl_basic_map_free(bmap
);
4311 for (i
= 0; i
< n
; ++i
) {
4312 new_div
[i
] = bmap
->block2
.data
+
4313 (bmap
->extra
+ i
) * (1 + row_size
);
4314 isl_seq_clr(new_div
[i
], 1 + row_size
);
4316 for (i
= 0; i
< bmap
->extra
; ++i
)
4317 new_div
[n
+ i
] = bmap
->block2
.data
+ (bmap
->div
[i
] - old
);
4319 bmap
->div
= new_div
;
4326 /* Drop constraints from "bmap" that only involve the variables
4327 * of "type" in the range [first, first + n] that are not related
4328 * to any of the variables outside that interval.
4329 * These constraints cannot influence the values for the variables
4330 * outside the interval, except in case they cause "bmap" to be empty.
4331 * Only drop the constraints if "bmap" is known to be non-empty.
4333 static __isl_give isl_basic_map
*drop_irrelevant_constraints(
4334 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
4335 unsigned first
, unsigned n
)
4339 isl_size dim
, n_div
;
4342 non_empty
= isl_basic_map_plain_is_non_empty(bmap
);
4344 return isl_basic_map_free(bmap
);
4348 dim
= isl_basic_map_dim(bmap
, isl_dim_all
);
4349 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4350 if (dim
< 0 || n_div
< 0)
4351 return isl_basic_map_free(bmap
);
4352 groups
= isl_calloc_array(isl_basic_map_get_ctx(bmap
), int, dim
);
4354 return isl_basic_map_free(bmap
);
4355 first
+= isl_basic_map_offset(bmap
, type
) - 1;
4356 for (i
= 0; i
< first
; ++i
)
4358 for (i
= first
+ n
; i
< dim
- n_div
; ++i
)
4361 bmap
= isl_basic_map_drop_unrelated_constraints(bmap
, groups
);
4366 /* Turn the n dimensions of type type, starting at first
4367 * into existentially quantified variables.
4369 * If a subset of the projected out variables are unrelated
4370 * to any of the variables that remain, then the constraints
4371 * involving this subset are simply dropped first.
4373 __isl_give isl_basic_map
*isl_basic_map_project_out(
4374 __isl_take isl_basic_map
*bmap
,
4375 enum isl_dim_type type
, unsigned first
, unsigned n
)
4381 return basic_map_space_reset(bmap
, type
);
4382 if (type
== isl_dim_div
)
4383 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
4384 "cannot project out existentially quantified variables",
4385 return isl_basic_map_free(bmap
));
4387 empty
= isl_basic_map_plain_is_empty(bmap
);
4389 return isl_basic_map_free(bmap
);
4391 bmap
= isl_basic_map_set_to_empty(bmap
);
4393 bmap
= drop_irrelevant_constraints(bmap
, type
, first
, n
);
4397 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
4398 return isl_basic_map_remove_dims(bmap
, type
, first
, n
);
4400 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
4401 return isl_basic_map_free(bmap
);
4403 bmap
= move_last(bmap
, type
, first
, n
);
4404 bmap
= isl_basic_map_cow(bmap
);
4405 bmap
= insert_div_rows(bmap
, n
);
4407 space
= isl_basic_map_take_space(bmap
);
4408 space
= isl_space_drop_dims(space
, type
, first
, n
);
4409 bmap
= isl_basic_map_restore_space(bmap
, space
);
4410 bmap
= isl_basic_map_simplify(bmap
);
4411 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
4412 return isl_basic_map_finalize(bmap
);
4415 /* Turn the n dimensions of type type, starting at first
4416 * into existentially quantified variables.
4418 __isl_give isl_basic_set
*isl_basic_set_project_out(
4419 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
,
4420 unsigned first
, unsigned n
)
4422 return bset_from_bmap(isl_basic_map_project_out(bset_to_bmap(bset
),
4426 /* Turn the n dimensions of type type, starting at first
4427 * into existentially quantified variables.
4429 __isl_give isl_map
*isl_map_project_out(__isl_take isl_map
*map
,
4430 enum isl_dim_type type
, unsigned first
, unsigned n
)
4436 return map_space_reset(map
, type
);
4438 if (isl_map_check_range(map
, type
, first
, n
) < 0)
4439 return isl_map_free(map
);
4441 map
= isl_map_cow(map
);
4445 for (i
= 0; i
< map
->n
; ++i
) {
4446 map
->p
[i
] = isl_basic_map_project_out(map
->p
[i
], type
, first
, n
);
4452 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
4453 map
= isl_map_unmark_normalized(map
);
4455 space
= isl_map_take_space(map
);
4456 space
= isl_space_drop_dims(space
, type
, first
, n
);
4457 map
= isl_map_restore_space(map
, space
);
4466 #define TYPE isl_map
4467 #include "isl_project_out_all_params_templ.c"
4469 /* Turn all the dimensions of type "type", except the "n" starting at "first"
4470 * into existentially quantified variables.
4472 __isl_give isl_map
*isl_map_project_onto(__isl_take isl_map
*map
,
4473 enum isl_dim_type type
, unsigned first
, unsigned n
)
4477 dim
= isl_map_dim(map
, type
);
4478 if (isl_map_check_range(map
, type
, first
, n
) < 0 || dim
< 0)
4479 return isl_map_free(map
);
4480 map
= isl_map_project_out(map
, type
, first
+ n
, dim
- (first
+ n
));
4481 map
= isl_map_project_out(map
, type
, 0, first
);
4485 /* Turn the n dimensions of type type, starting at first
4486 * into existentially quantified variables.
4488 __isl_give isl_set
*isl_set_project_out(__isl_take isl_set
*set
,
4489 enum isl_dim_type type
, unsigned first
, unsigned n
)
4491 return set_from_map(isl_map_project_out(set_to_map(set
),
4495 /* If "set" involves a parameter with identifier "id",
4496 * then turn it into an existentially quantified variable.
4498 __isl_give isl_set
*isl_set_project_out_param_id(__isl_take isl_set
*set
,
4499 __isl_take isl_id
*id
)
4505 pos
= isl_set_find_dim_by_id(set
, isl_dim_param
, id
);
4509 return isl_set_project_out(set
, isl_dim_param
, pos
, 1);
4516 /* If "set" involves any of the parameters with identifiers in "list",
4517 * then turn them into existentially quantified variables.
4519 __isl_give isl_set
*isl_set_project_out_param_id_list(__isl_take isl_set
*set
,
4520 __isl_take isl_id_list
*list
)
4525 n
= isl_id_list_size(list
);
4528 for (i
= 0; i
< n
; ++i
) {
4531 id
= isl_id_list_get_at(list
, i
);
4532 set
= isl_set_project_out_param_id(set
, id
);
4535 isl_id_list_free(list
);
4538 isl_id_list_free(list
);
4543 /* Project out all parameters from "set" by existentially quantifying
4546 __isl_give isl_set
*isl_set_project_out_all_params(__isl_take isl_set
*set
)
4548 return set_from_map(isl_map_project_out_all_params(set_to_map(set
)));
4551 /* Return a map that projects the elements in "set" onto their
4552 * "n" set dimensions starting at "first".
4553 * "type" should be equal to isl_dim_set.
4555 __isl_give isl_map
*isl_set_project_onto_map(__isl_take isl_set
*set
,
4556 enum isl_dim_type type
, unsigned first
, unsigned n
)
4561 if (type
!= isl_dim_set
)
4562 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
4563 "only set dimensions can be projected out", goto error
);
4564 if (isl_set_check_range(set
, type
, first
, n
) < 0)
4565 return isl_set_free(set
);
4567 map
= isl_map_from_domain(set
);
4568 map
= isl_map_add_dims(map
, isl_dim_out
, n
);
4569 for (i
= 0; i
< n
; ++i
)
4570 map
= isl_map_equate(map
, isl_dim_in
, first
+ i
,
4578 static __isl_give isl_basic_map
*add_divs(__isl_take isl_basic_map
*bmap
,
4584 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4586 return isl_basic_map_free(bmap
);
4587 for (i
= 0; i
< n
; ++i
) {
4588 j
= isl_basic_map_alloc_div(bmap
);
4591 isl_seq_clr(bmap
->div
[j
], 1 + 1 + total
);
4595 isl_basic_map_free(bmap
);
4599 __isl_give isl_basic_map
*isl_basic_map_apply_range(
4600 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
4602 isl_space
*space_result
= NULL
;
4603 struct isl_basic_map
*bmap
;
4604 isl_size n_in
, n_out
, n
, nparam
;
4605 unsigned total
, pos
;
4606 struct isl_dim_map
*dim_map1
, *dim_map2
;
4608 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
4610 if (!isl_space_tuple_is_equal(bmap1
->dim
, isl_dim_out
,
4611 bmap2
->dim
, isl_dim_in
))
4612 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
4613 "spaces don't match", goto error
);
4615 n_in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
4616 n_out
= isl_basic_map_dim(bmap2
, isl_dim_out
);
4617 n
= isl_basic_map_dim(bmap1
, isl_dim_out
);
4618 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
4619 if (n_in
< 0 || n_out
< 0 || n
< 0 || nparam
< 0)
4622 space_result
= isl_space_join(isl_basic_map_get_space(bmap1
),
4623 isl_basic_map_get_space(bmap2
));
4625 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ n
;
4626 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4627 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4628 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
4629 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
4630 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
4631 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_in
);
4632 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_out
);
4633 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
4634 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
4635 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
4637 bmap
= isl_basic_map_alloc_space(space_result
,
4638 bmap1
->n_div
+ bmap2
->n_div
+ n
,
4639 bmap1
->n_eq
+ bmap2
->n_eq
,
4640 bmap1
->n_ineq
+ bmap2
->n_ineq
);
4641 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
4642 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
4643 bmap
= add_divs(bmap
, n
);
4644 bmap
= isl_basic_map_simplify(bmap
);
4645 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
4646 return isl_basic_map_finalize(bmap
);
4648 isl_basic_map_free(bmap1
);
4649 isl_basic_map_free(bmap2
);
4653 __isl_give isl_basic_set
*isl_basic_set_apply(__isl_take isl_basic_set
*bset
,
4654 __isl_take isl_basic_map
*bmap
)
4656 if (isl_basic_map_check_compatible_domain(bmap
, bset
) < 0)
4659 return bset_from_bmap(isl_basic_map_apply_range(bset_to_bmap(bset
),
4662 isl_basic_set_free(bset
);
4663 isl_basic_map_free(bmap
);
4667 __isl_give isl_basic_map
*isl_basic_map_apply_domain(
4668 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
4670 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
4672 if (!isl_space_tuple_is_equal(bmap1
->dim
, isl_dim_in
,
4673 bmap2
->dim
, isl_dim_in
))
4674 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
4675 "spaces don't match", goto error
);
4677 bmap1
= isl_basic_map_reverse(bmap1
);
4678 bmap1
= isl_basic_map_apply_range(bmap1
, bmap2
);
4679 return isl_basic_map_reverse(bmap1
);
4681 isl_basic_map_free(bmap1
);
4682 isl_basic_map_free(bmap2
);
4686 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
4687 * A \cap B -> f(A) + f(B)
4689 __isl_give isl_basic_map
*isl_basic_map_sum(__isl_take isl_basic_map
*bmap1
,
4690 __isl_take isl_basic_map
*bmap2
)
4692 isl_size n_in
, n_out
, nparam
;
4693 unsigned total
, pos
;
4694 struct isl_basic_map
*bmap
= NULL
;
4695 struct isl_dim_map
*dim_map1
, *dim_map2
;
4698 if (isl_basic_map_check_equal_space(bmap1
, bmap2
) < 0)
4701 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
4702 n_in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
4703 n_out
= isl_basic_map_dim(bmap1
, isl_dim_out
);
4704 if (nparam
< 0 || n_in
< 0 || n_out
< 0)
4707 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
;
4708 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4709 dim_map2
= isl_dim_map_alloc(bmap2
->ctx
, total
);
4710 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
4711 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
);
4712 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
4713 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
4714 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_in
+ n_out
);
4715 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
4716 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
4717 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_out
);
4719 bmap
= isl_basic_map_alloc_space(isl_space_copy(bmap1
->dim
),
4720 bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
,
4721 bmap1
->n_eq
+ bmap2
->n_eq
+ n_out
,
4722 bmap1
->n_ineq
+ bmap2
->n_ineq
);
4723 for (i
= 0; i
< n_out
; ++i
) {
4724 int j
= isl_basic_map_alloc_equality(bmap
);
4727 isl_seq_clr(bmap
->eq
[j
], 1+total
);
4728 isl_int_set_si(bmap
->eq
[j
][1+nparam
+n_in
+i
], -1);
4729 isl_int_set_si(bmap
->eq
[j
][1+pos
+i
], 1);
4730 isl_int_set_si(bmap
->eq
[j
][1+pos
-n_out
+i
], 1);
4732 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
4733 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
4734 bmap
= add_divs(bmap
, 2 * n_out
);
4736 bmap
= isl_basic_map_simplify(bmap
);
4737 return isl_basic_map_finalize(bmap
);
4739 isl_basic_map_free(bmap
);
4740 isl_basic_map_free(bmap1
);
4741 isl_basic_map_free(bmap2
);
4745 /* Given two maps A -> f(A) and B -> g(B), construct a map
4746 * A \cap B -> f(A) + f(B)
4748 __isl_give isl_map
*isl_map_sum(__isl_take isl_map
*map1
,
4749 __isl_take isl_map
*map2
)
4751 struct isl_map
*result
;
4754 if (isl_map_check_equal_space(map1
, map2
) < 0)
4757 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
4758 map1
->n
* map2
->n
, 0);
4761 for (i
= 0; i
< map1
->n
; ++i
)
4762 for (j
= 0; j
< map2
->n
; ++j
) {
4763 struct isl_basic_map
*part
;
4764 part
= isl_basic_map_sum(
4765 isl_basic_map_copy(map1
->p
[i
]),
4766 isl_basic_map_copy(map2
->p
[j
]));
4767 if (isl_basic_map_is_empty(part
))
4768 isl_basic_map_free(part
);
4770 result
= isl_map_add_basic_map(result
, part
);
4783 __isl_give isl_set
*isl_set_sum(__isl_take isl_set
*set1
,
4784 __isl_take isl_set
*set2
)
4786 return set_from_map(isl_map_sum(set_to_map(set1
), set_to_map(set2
)));
4789 /* Given a basic map A -> f(A), construct A -> -f(A).
4791 __isl_give isl_basic_map
*isl_basic_map_neg(__isl_take isl_basic_map
*bmap
)
4797 bmap
= isl_basic_map_cow(bmap
);
4798 n
= isl_basic_map_dim(bmap
, isl_dim_out
);
4800 return isl_basic_map_free(bmap
);
4802 off
= isl_basic_map_offset(bmap
, isl_dim_out
);
4803 for (i
= 0; i
< bmap
->n_eq
; ++i
)
4804 for (j
= 0; j
< n
; ++j
)
4805 isl_int_neg(bmap
->eq
[i
][off
+j
], bmap
->eq
[i
][off
+j
]);
4806 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
4807 for (j
= 0; j
< n
; ++j
)
4808 isl_int_neg(bmap
->ineq
[i
][off
+j
], bmap
->ineq
[i
][off
+j
]);
4809 for (i
= 0; i
< bmap
->n_div
; ++i
)
4810 for (j
= 0; j
< n
; ++j
)
4811 isl_int_neg(bmap
->div
[i
][1+off
+j
], bmap
->div
[i
][1+off
+j
]);
4812 bmap
= isl_basic_map_gauss(bmap
, NULL
);
4813 return isl_basic_map_finalize(bmap
);
4816 __isl_give isl_basic_set
*isl_basic_set_neg(__isl_take isl_basic_set
*bset
)
4818 return isl_basic_map_neg(bset
);
4821 /* Given a map A -> f(A), construct A -> -f(A).
4823 __isl_give isl_map
*isl_map_neg(__isl_take isl_map
*map
)
4827 map
= isl_map_cow(map
);
4831 for (i
= 0; i
< map
->n
; ++i
) {
4832 map
->p
[i
] = isl_basic_map_neg(map
->p
[i
]);
4843 __isl_give isl_set
*isl_set_neg(__isl_take isl_set
*set
)
4845 return set_from_map(isl_map_neg(set_to_map(set
)));
4848 /* Given a basic map A -> f(A) and an integer d, construct a basic map
4849 * A -> floor(f(A)/d).
4851 __isl_give isl_basic_map
*isl_basic_map_floordiv(__isl_take isl_basic_map
*bmap
,
4854 isl_size n_in
, n_out
, nparam
;
4855 unsigned total
, pos
;
4856 struct isl_basic_map
*result
= NULL
;
4857 struct isl_dim_map
*dim_map
;
4860 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4861 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4862 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4863 if (nparam
< 0 || n_in
< 0 || n_out
< 0)
4864 return isl_basic_map_free(bmap
);
4866 total
= nparam
+ n_in
+ n_out
+ bmap
->n_div
+ n_out
;
4867 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
4868 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_param
, pos
= 0);
4869 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_in
, pos
+= nparam
);
4870 isl_dim_map_div(dim_map
, bmap
, pos
+= n_in
+ n_out
);
4871 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_out
, pos
+= bmap
->n_div
);
4873 result
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
4874 bmap
->n_div
+ n_out
,
4875 bmap
->n_eq
, bmap
->n_ineq
+ 2 * n_out
);
4876 result
= isl_basic_map_add_constraints_dim_map(result
, bmap
, dim_map
);
4877 result
= add_divs(result
, n_out
);
4878 for (i
= 0; i
< n_out
; ++i
) {
4880 j
= isl_basic_map_alloc_inequality(result
);
4883 isl_seq_clr(result
->ineq
[j
], 1+total
);
4884 isl_int_neg(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
4885 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], 1);
4886 j
= isl_basic_map_alloc_inequality(result
);
4889 isl_seq_clr(result
->ineq
[j
], 1+total
);
4890 isl_int_set(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
4891 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], -1);
4892 isl_int_sub_ui(result
->ineq
[j
][0], d
, 1);
4895 result
= isl_basic_map_simplify(result
);
4896 return isl_basic_map_finalize(result
);
4898 isl_basic_map_free(result
);
4902 /* Given a map A -> f(A) and an integer d, construct a map
4903 * A -> floor(f(A)/d).
4905 __isl_give isl_map
*isl_map_floordiv(__isl_take isl_map
*map
, isl_int d
)
4909 map
= isl_map_cow(map
);
4913 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
4914 for (i
= 0; i
< map
->n
; ++i
) {
4915 map
->p
[i
] = isl_basic_map_floordiv(map
->p
[i
], d
);
4919 map
= isl_map_unmark_normalized(map
);
4927 /* Given a map A -> f(A) and an integer d, construct a map
4928 * A -> floor(f(A)/d).
4930 __isl_give isl_map
*isl_map_floordiv_val(__isl_take isl_map
*map
,
4931 __isl_take isl_val
*d
)
4935 if (!isl_val_is_int(d
))
4936 isl_die(isl_val_get_ctx(d
), isl_error_invalid
,
4937 "expecting integer denominator", goto error
);
4938 map
= isl_map_floordiv(map
, d
->n
);
4947 static __isl_give isl_basic_map
*var_equal(__isl_take isl_basic_map
*bmap
,
4955 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4956 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4957 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4958 if (total
< 0 || nparam
< 0 || n_in
< 0)
4959 return isl_basic_map_free(bmap
);
4960 i
= isl_basic_map_alloc_equality(bmap
);
4963 isl_seq_clr(bmap
->eq
[i
], 1 + total
);
4964 isl_int_set_si(bmap
->eq
[i
][1+nparam
+pos
], -1);
4965 isl_int_set_si(bmap
->eq
[i
][1+nparam
+n_in
+pos
], 1);
4966 return isl_basic_map_finalize(bmap
);
4968 isl_basic_map_free(bmap
);
4972 /* Add a constraint to "bmap" expressing i_pos < o_pos
4974 static __isl_give isl_basic_map
*var_less(__isl_take isl_basic_map
*bmap
,
4982 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4983 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4984 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4985 if (total
< 0 || nparam
< 0 || n_in
< 0)
4986 return isl_basic_map_free(bmap
);
4987 i
= isl_basic_map_alloc_inequality(bmap
);
4990 isl_seq_clr(bmap
->ineq
[i
], 1 + total
);
4991 isl_int_set_si(bmap
->ineq
[i
][0], -1);
4992 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
4993 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
4994 return isl_basic_map_finalize(bmap
);
4996 isl_basic_map_free(bmap
);
5000 /* Add a constraint to "bmap" expressing i_pos <= o_pos
5002 static __isl_give isl_basic_map
*var_less_or_equal(
5003 __isl_take isl_basic_map
*bmap
, unsigned pos
)
5010 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5011 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5012 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5013 if (total
< 0 || nparam
< 0 || n_in
< 0)
5014 return isl_basic_map_free(bmap
);
5015 i
= isl_basic_map_alloc_inequality(bmap
);
5018 isl_seq_clr(bmap
->ineq
[i
], 1 + total
);
5019 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
5020 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
5021 return isl_basic_map_finalize(bmap
);
5023 isl_basic_map_free(bmap
);
5027 /* Add a constraint to "bmap" expressing i_pos > o_pos
5029 static __isl_give isl_basic_map
*var_more(__isl_take isl_basic_map
*bmap
,
5037 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5038 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5039 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5040 if (total
< 0 || nparam
< 0 || n_in
< 0)
5041 return isl_basic_map_free(bmap
);
5042 i
= isl_basic_map_alloc_inequality(bmap
);
5045 isl_seq_clr(bmap
->ineq
[i
], 1 + total
);
5046 isl_int_set_si(bmap
->ineq
[i
][0], -1);
5047 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
5048 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
5049 return isl_basic_map_finalize(bmap
);
5051 isl_basic_map_free(bmap
);
5055 /* Add a constraint to "bmap" expressing i_pos >= o_pos
5057 static __isl_give isl_basic_map
*var_more_or_equal(
5058 __isl_take isl_basic_map
*bmap
, unsigned pos
)
5065 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5066 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5067 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5068 if (total
< 0 || nparam
< 0 || n_in
< 0)
5069 return isl_basic_map_free(bmap
);
5070 i
= isl_basic_map_alloc_inequality(bmap
);
5073 isl_seq_clr(bmap
->ineq
[i
], 1 + total
);
5074 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
5075 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
5076 return isl_basic_map_finalize(bmap
);
5078 isl_basic_map_free(bmap
);
5082 __isl_give isl_basic_map
*isl_basic_map_equal(
5083 __isl_take isl_space
*space
, unsigned n_equal
)
5086 struct isl_basic_map
*bmap
;
5087 bmap
= isl_basic_map_alloc_space(space
, 0, n_equal
, 0);
5090 for (i
= 0; i
< n_equal
&& bmap
; ++i
)
5091 bmap
= var_equal(bmap
, i
);
5092 return isl_basic_map_finalize(bmap
);
5095 /* Return a relation on of dimension "space" expressing i_[0..pos] << o_[0..pos]
5097 __isl_give isl_basic_map
*isl_basic_map_less_at(__isl_take isl_space
*space
,
5101 struct isl_basic_map
*bmap
;
5102 bmap
= isl_basic_map_alloc_space(space
, 0, pos
, 1);
5105 for (i
= 0; i
< pos
&& bmap
; ++i
)
5106 bmap
= var_equal(bmap
, i
);
5108 bmap
= var_less(bmap
, pos
);
5109 return isl_basic_map_finalize(bmap
);
5112 /* Return a relation on "space" expressing i_[0..pos] <<= o_[0..pos]
5114 __isl_give isl_basic_map
*isl_basic_map_less_or_equal_at(
5115 __isl_take isl_space
*space
, unsigned pos
)
5118 isl_basic_map
*bmap
;
5120 bmap
= isl_basic_map_alloc_space(space
, 0, pos
, 1);
5121 for (i
= 0; i
< pos
; ++i
)
5122 bmap
= var_equal(bmap
, i
);
5123 bmap
= var_less_or_equal(bmap
, pos
);
5124 return isl_basic_map_finalize(bmap
);
5127 /* Return a relation on "space" expressing i_pos > o_pos
5129 __isl_give isl_basic_map
*isl_basic_map_more_at(__isl_take isl_space
*space
,
5133 struct isl_basic_map
*bmap
;
5134 bmap
= isl_basic_map_alloc_space(space
, 0, pos
, 1);
5137 for (i
= 0; i
< pos
&& bmap
; ++i
)
5138 bmap
= var_equal(bmap
, i
);
5140 bmap
= var_more(bmap
, pos
);
5141 return isl_basic_map_finalize(bmap
);
5144 /* Return a relation on "space" expressing i_[0..pos] >>= o_[0..pos]
5146 __isl_give isl_basic_map
*isl_basic_map_more_or_equal_at(
5147 __isl_take isl_space
*space
, unsigned pos
)
5150 isl_basic_map
*bmap
;
5152 bmap
= isl_basic_map_alloc_space(space
, 0, pos
, 1);
5153 for (i
= 0; i
< pos
; ++i
)
5154 bmap
= var_equal(bmap
, i
);
5155 bmap
= var_more_or_equal(bmap
, pos
);
5156 return isl_basic_map_finalize(bmap
);
5159 static __isl_give isl_map
*map_lex_lte_first(__isl_take isl_space
*space
,
5160 unsigned n
, int equal
)
5162 struct isl_map
*map
;
5165 if (n
== 0 && equal
)
5166 return isl_map_universe(space
);
5168 map
= isl_map_alloc_space(isl_space_copy(space
), n
, ISL_MAP_DISJOINT
);
5170 for (i
= 0; i
+ 1 < n
; ++i
)
5171 map
= isl_map_add_basic_map(map
,
5172 isl_basic_map_less_at(isl_space_copy(space
), i
));
5175 map
= isl_map_add_basic_map(map
,
5176 isl_basic_map_less_or_equal_at(space
, n
- 1));
5178 map
= isl_map_add_basic_map(map
,
5179 isl_basic_map_less_at(space
, n
- 1));
5181 isl_space_free(space
);
5186 static __isl_give isl_map
*map_lex_lte(__isl_take isl_space
*space
, int equal
)
5190 return map_lex_lte_first(space
, space
->n_out
, equal
);
5193 __isl_give isl_map
*isl_map_lex_lt_first(__isl_take isl_space
*space
,
5196 return map_lex_lte_first(space
, n
, 0);
5199 __isl_give isl_map
*isl_map_lex_le_first(__isl_take isl_space
*space
,
5202 return map_lex_lte_first(space
, n
, 1);
5205 __isl_give isl_map
*isl_map_lex_lt(__isl_take isl_space
*set_space
)
5207 return map_lex_lte(isl_space_map_from_set(set_space
), 0);
5210 __isl_give isl_map
*isl_map_lex_le(__isl_take isl_space
*set_space
)
5212 return map_lex_lte(isl_space_map_from_set(set_space
), 1);
5215 static __isl_give isl_map
*map_lex_gte_first(__isl_take isl_space
*space
,
5216 unsigned n
, int equal
)
5218 struct isl_map
*map
;
5221 if (n
== 0 && equal
)
5222 return isl_map_universe(space
);
5224 map
= isl_map_alloc_space(isl_space_copy(space
), n
, ISL_MAP_DISJOINT
);
5226 for (i
= 0; i
+ 1 < n
; ++i
)
5227 map
= isl_map_add_basic_map(map
,
5228 isl_basic_map_more_at(isl_space_copy(space
), i
));
5231 map
= isl_map_add_basic_map(map
,
5232 isl_basic_map_more_or_equal_at(space
, n
- 1));
5234 map
= isl_map_add_basic_map(map
,
5235 isl_basic_map_more_at(space
, n
- 1));
5237 isl_space_free(space
);
5242 static __isl_give isl_map
*map_lex_gte(__isl_take isl_space
*space
, int equal
)
5246 return map_lex_gte_first(space
, space
->n_out
, equal
);
5249 __isl_give isl_map
*isl_map_lex_gt_first(__isl_take isl_space
*space
,
5252 return map_lex_gte_first(space
, n
, 0);
5255 __isl_give isl_map
*isl_map_lex_ge_first(__isl_take isl_space
*space
,
5258 return map_lex_gte_first(space
, n
, 1);
5261 __isl_give isl_map
*isl_map_lex_gt(__isl_take isl_space
*set_space
)
5263 return map_lex_gte(isl_space_map_from_set(set_space
), 0);
5266 __isl_give isl_map
*isl_map_lex_ge(__isl_take isl_space
*set_space
)
5268 return map_lex_gte(isl_space_map_from_set(set_space
), 1);
5271 __isl_give isl_map
*isl_set_lex_le_set(__isl_take isl_set
*set1
,
5272 __isl_take isl_set
*set2
)
5275 map
= isl_map_lex_le(isl_set_get_space(set1
));
5276 map
= isl_map_intersect_domain(map
, set1
);
5277 map
= isl_map_intersect_range(map
, set2
);
5281 __isl_give isl_map
*isl_set_lex_lt_set(__isl_take isl_set
*set1
,
5282 __isl_take isl_set
*set2
)
5285 map
= isl_map_lex_lt(isl_set_get_space(set1
));
5286 map
= isl_map_intersect_domain(map
, set1
);
5287 map
= isl_map_intersect_range(map
, set2
);
5291 __isl_give isl_map
*isl_set_lex_ge_set(__isl_take isl_set
*set1
,
5292 __isl_take isl_set
*set2
)
5295 map
= isl_map_lex_ge(isl_set_get_space(set1
));
5296 map
= isl_map_intersect_domain(map
, set1
);
5297 map
= isl_map_intersect_range(map
, set2
);
5301 __isl_give isl_map
*isl_set_lex_gt_set(__isl_take isl_set
*set1
,
5302 __isl_take isl_set
*set2
)
5305 map
= isl_map_lex_gt(isl_set_get_space(set1
));
5306 map
= isl_map_intersect_domain(map
, set1
);
5307 map
= isl_map_intersect_range(map
, set2
);
5311 __isl_give isl_map
*isl_map_lex_le_map(__isl_take isl_map
*map1
,
5312 __isl_take isl_map
*map2
)
5315 map
= isl_map_lex_le(isl_space_range(isl_map_get_space(map1
)));
5316 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
5317 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
5321 __isl_give isl_map
*isl_map_lex_lt_map(__isl_take isl_map
*map1
,
5322 __isl_take isl_map
*map2
)
5325 map
= isl_map_lex_lt(isl_space_range(isl_map_get_space(map1
)));
5326 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
5327 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
5331 __isl_give isl_map
*isl_map_lex_ge_map(__isl_take isl_map
*map1
,
5332 __isl_take isl_map
*map2
)
5335 map
= isl_map_lex_ge(isl_space_range(isl_map_get_space(map1
)));
5336 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
5337 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
5341 __isl_give isl_map
*isl_map_lex_gt_map(__isl_take isl_map
*map1
,
5342 __isl_take isl_map
*map2
)
5345 map
= isl_map_lex_gt(isl_space_range(isl_map_get_space(map1
)));
5346 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
5347 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
5351 /* For the div d = floor(f/m) at position "div", add the constraint
5355 static __isl_give isl_basic_map
*add_upper_div_constraint(
5356 __isl_take isl_basic_map
*bmap
, unsigned div
)
5359 isl_size v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
5363 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
5364 if (v_div
< 0 || n_div
< 0)
5365 return isl_basic_map_free(bmap
);
5367 i
= isl_basic_map_alloc_inequality(bmap
);
5369 return isl_basic_map_free(bmap
);
5370 isl_seq_cpy(bmap
->ineq
[i
], bmap
->div
[div
] + 1, 1 + v_div
+ n_div
);
5371 isl_int_neg(bmap
->ineq
[i
][1 + pos
], bmap
->div
[div
][0]);
5376 /* For the div d = floor(f/m) at position "div", add the constraint
5378 * -(f-(m-1)) + m d >= 0
5380 static __isl_give isl_basic_map
*add_lower_div_constraint(
5381 __isl_take isl_basic_map
*bmap
, unsigned div
)
5384 isl_size v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
5388 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
5389 if (v_div
< 0 || n_div
< 0)
5390 return isl_basic_map_free(bmap
);
5392 i
= isl_basic_map_alloc_inequality(bmap
);
5394 return isl_basic_map_free(bmap
);
5395 isl_seq_neg(bmap
->ineq
[i
], bmap
->div
[div
] + 1, 1 + v_div
+ n_div
);
5396 isl_int_set(bmap
->ineq
[i
][1 + pos
], bmap
->div
[div
][0]);
5397 isl_int_add(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], bmap
->ineq
[i
][1 + pos
]);
5398 isl_int_sub_ui(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], 1);
5403 /* For the div d = floor(f/m) at position "pos", add the constraints
5406 * -(f-(m-1)) + m d >= 0
5408 * Note that the second constraint is the negation of
5412 __isl_give isl_basic_map
*isl_basic_map_add_div_constraints(
5413 __isl_take isl_basic_map
*bmap
, unsigned pos
)
5415 bmap
= add_upper_div_constraint(bmap
, pos
);
5416 bmap
= add_lower_div_constraint(bmap
, pos
);
5420 /* For each known div d = floor(f/m), add the constraints
5423 * -(f-(m-1)) + m d >= 0
5425 * Remove duplicate constraints in case of some these div constraints
5426 * already appear in "bmap".
5428 __isl_give isl_basic_map
*isl_basic_map_add_known_div_constraints(
5429 __isl_take isl_basic_map
*bmap
)
5433 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
5435 return isl_basic_map_free(bmap
);
5439 bmap
= add_known_div_constraints(bmap
);
5440 bmap
= isl_basic_map_remove_duplicate_constraints(bmap
, NULL
, 0);
5441 bmap
= isl_basic_map_finalize(bmap
);
5445 /* Add the div constraint of sign "sign" for div "div" of "bmap".
5447 * In particular, if this div is of the form d = floor(f/m),
5448 * then add the constraint
5452 * if sign < 0 or the constraint
5454 * -(f-(m-1)) + m d >= 0
5458 __isl_give isl_basic_map
*isl_basic_map_add_div_constraint(
5459 __isl_take isl_basic_map
*bmap
, unsigned div
, int sign
)
5462 return add_upper_div_constraint(bmap
, div
);
5464 return add_lower_div_constraint(bmap
, div
);
5467 __isl_give isl_basic_set
*isl_basic_map_underlying_set(
5468 __isl_take isl_basic_map
*bmap
)
5474 if (bmap
->dim
->nparam
== 0 && bmap
->dim
->n_in
== 0 &&
5476 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_in
) &&
5477 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_out
))
5478 return bset_from_bmap(bmap
);
5479 bmap
= isl_basic_map_cow(bmap
);
5482 space
= isl_basic_map_take_space(bmap
);
5483 space
= isl_space_underlying(space
, bmap
->n_div
);
5484 bmap
= isl_basic_map_restore_space(bmap
, space
);
5487 bmap
->extra
-= bmap
->n_div
;
5489 bmap
= isl_basic_map_finalize(bmap
);
5490 return bset_from_bmap(bmap
);
5492 isl_basic_map_free(bmap
);
5496 __isl_give isl_basic_set
*isl_basic_set_underlying_set(
5497 __isl_take isl_basic_set
*bset
)
5499 return isl_basic_map_underlying_set(bset_to_bmap(bset
));
5502 /* Replace each element in "list" by the result of applying
5503 * isl_basic_map_underlying_set to the element.
5505 __isl_give isl_basic_set_list
*isl_basic_map_list_underlying_set(
5506 __isl_take isl_basic_map_list
*list
)
5511 n
= isl_basic_map_list_n_basic_map(list
);
5515 for (i
= 0; i
< n
; ++i
) {
5516 isl_basic_map
*bmap
;
5517 isl_basic_set
*bset
;
5519 bmap
= isl_basic_map_list_get_basic_map(list
, i
);
5520 bset
= isl_basic_set_underlying_set(bmap
);
5521 list
= isl_basic_set_list_set_basic_set(list
, i
, bset
);
5526 isl_basic_map_list_free(list
);
5530 __isl_give isl_basic_map
*isl_basic_map_overlying_set(
5531 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_map
*like
)
5533 struct isl_basic_map
*bmap
;
5534 struct isl_ctx
*ctx
;
5535 isl_size dim
, bmap_total
;
5542 if (isl_basic_set_check_no_params(bset
) < 0 ||
5543 isl_basic_set_check_no_locals(bset
) < 0)
5545 dim
= isl_basic_set_dim(bset
, isl_dim_set
);
5546 bmap_total
= isl_basic_map_dim(like
, isl_dim_all
);
5547 if (dim
< 0 || bmap_total
< 0)
5549 isl_assert(ctx
, dim
== bmap_total
, goto error
);
5550 if (like
->n_div
== 0) {
5551 isl_space
*space
= isl_basic_map_get_space(like
);
5552 isl_basic_map_free(like
);
5553 return isl_basic_map_reset_space(bset
, space
);
5555 bset
= isl_basic_set_cow(bset
);
5558 total
= dim
+ bset
->extra
;
5559 bmap
= bset_to_bmap(bset
);
5560 isl_space_free(isl_basic_map_take_space(bmap
));
5561 bmap
= isl_basic_map_restore_space(bmap
, isl_basic_map_get_space(like
));
5564 bmap
->n_div
= like
->n_div
;
5565 bmap
->extra
+= like
->n_div
;
5569 ltotal
= total
- bmap
->extra
+ like
->extra
;
5572 bmap
->block2
= isl_blk_extend(ctx
, bmap
->block2
,
5573 bmap
->extra
* (1 + 1 + total
));
5574 if (isl_blk_is_error(bmap
->block2
))
5576 div
= isl_realloc_array(ctx
, bmap
->div
, isl_int
*, bmap
->extra
);
5580 for (i
= 0; i
< bmap
->extra
; ++i
)
5581 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + 1 + total
);
5582 for (i
= 0; i
< like
->n_div
; ++i
) {
5583 isl_seq_cpy(bmap
->div
[i
], like
->div
[i
], 1 + 1 + ltotal
);
5584 isl_seq_clr(bmap
->div
[i
]+1+1+ltotal
, total
- ltotal
);
5586 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
5588 isl_basic_map_free(like
);
5589 bmap
= isl_basic_map_simplify(bmap
);
5590 bmap
= isl_basic_map_finalize(bmap
);
5593 isl_basic_map_free(like
);
5594 isl_basic_set_free(bset
);
5598 __isl_give isl_basic_set
*isl_basic_set_from_underlying_set(
5599 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*like
)
5601 return bset_from_bmap(isl_basic_map_overlying_set(bset
,
5602 bset_to_bmap(like
)));
5605 __isl_give isl_set
*isl_map_underlying_set(__isl_take isl_map
*map
)
5609 map
= isl_map_cow(map
);
5612 map
->dim
= isl_space_cow(map
->dim
);
5616 for (i
= 1; i
< map
->n
; ++i
)
5617 isl_assert(map
->ctx
, map
->p
[0]->n_div
== map
->p
[i
]->n_div
,
5619 for (i
= 0; i
< map
->n
; ++i
) {
5620 map
->p
[i
] = bset_to_bmap(
5621 isl_basic_map_underlying_set(map
->p
[i
]));
5626 map
->dim
= isl_space_underlying(map
->dim
, 0);
5628 isl_space_free(map
->dim
);
5629 map
->dim
= isl_space_copy(map
->p
[0]->dim
);
5633 return set_from_map(map
);
5639 /* Replace the space of "bmap" by "space".
5641 * If the space of "bmap" is identical to "space" (including the identifiers
5642 * of the input and output dimensions), then simply return the original input.
5644 __isl_give isl_basic_map
*isl_basic_map_reset_space(
5645 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*space
)
5648 isl_space
*bmap_space
;
5650 bmap_space
= isl_basic_map_peek_space(bmap
);
5651 equal
= isl_space_is_equal(bmap_space
, space
);
5652 if (equal
>= 0 && equal
)
5653 equal
= isl_space_has_equal_ids(bmap_space
, space
);
5657 isl_space_free(space
);
5660 isl_space_free(isl_basic_map_take_space(bmap
));
5661 bmap
= isl_basic_map_restore_space(bmap
, space
);
5663 bmap
= isl_basic_map_finalize(bmap
);
5667 isl_basic_map_free(bmap
);
5668 isl_space_free(space
);
5672 __isl_give isl_basic_set
*isl_basic_set_reset_space(
5673 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*space
)
5675 return bset_from_bmap(isl_basic_map_reset_space(bset_to_bmap(bset
),
5679 /* Check that the total dimensions of "map" and "space" are the same.
5681 static isl_stat
check_map_space_equal_total_dim(__isl_keep isl_map
*map
,
5682 __isl_keep isl_space
*space
)
5684 isl_size dim1
, dim2
;
5686 dim1
= isl_map_dim(map
, isl_dim_all
);
5687 dim2
= isl_space_dim(space
, isl_dim_all
);
5688 if (dim1
< 0 || dim2
< 0)
5689 return isl_stat_error
;
5692 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
5693 "total dimensions do not match", return isl_stat_error
);
5696 __isl_give isl_map
*isl_map_reset_space(__isl_take isl_map
*map
,
5697 __isl_take isl_space
*space
)
5701 map
= isl_map_cow(map
);
5705 for (i
= 0; i
< map
->n
; ++i
) {
5706 map
->p
[i
] = isl_basic_map_reset_space(map
->p
[i
],
5707 isl_space_copy(space
));
5711 isl_space_free(isl_map_take_space(map
));
5712 map
= isl_map_restore_space(map
, space
);
5717 isl_space_free(space
);
5721 /* Replace the space of "map" by "space", without modifying
5722 * the dimension of "map".
5724 * If the space of "map" is identical to "space" (including the identifiers
5725 * of the input and output dimensions), then simply return the original input.
5727 __isl_give isl_map
*isl_map_reset_equal_dim_space(__isl_take isl_map
*map
,
5728 __isl_take isl_space
*space
)
5731 isl_space
*map_space
;
5733 map_space
= isl_map_peek_space(map
);
5734 equal
= isl_space_is_equal(map_space
, space
);
5735 if (equal
>= 0 && equal
)
5736 equal
= isl_space_has_equal_ids(map_space
, space
);
5740 isl_space_free(space
);
5743 if (check_map_space_equal_total_dim(map
, space
) < 0)
5745 return isl_map_reset_space(map
, space
);
5748 isl_space_free(space
);
5752 __isl_give isl_set
*isl_set_reset_space(__isl_take isl_set
*set
,
5753 __isl_take isl_space
*space
)
5755 return set_from_map(isl_map_reset_space(set_to_map(set
), space
));
5758 /* Compute the parameter domain of the given basic set.
5760 __isl_give isl_basic_set
*isl_basic_set_params(__isl_take isl_basic_set
*bset
)
5766 is_params
= isl_basic_set_is_params(bset
);
5768 return isl_basic_set_free(bset
);
5772 n
= isl_basic_set_dim(bset
, isl_dim_set
);
5774 return isl_basic_set_free(bset
);
5775 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
5776 space
= isl_basic_set_get_space(bset
);
5777 space
= isl_space_params(space
);
5778 bset
= isl_basic_set_reset_space(bset
, space
);
5782 /* Construct a zero-dimensional basic set with the given parameter domain.
5784 __isl_give isl_basic_set
*isl_basic_set_from_params(
5785 __isl_take isl_basic_set
*bset
)
5788 space
= isl_basic_set_get_space(bset
);
5789 space
= isl_space_set_from_params(space
);
5790 bset
= isl_basic_set_reset_space(bset
, space
);
5794 /* Compute the parameter domain of the given set.
5796 __isl_give isl_set
*isl_set_params(__isl_take isl_set
*set
)
5798 return isl_map_params(set_to_map(set
));
5801 /* Construct a zero-dimensional set with the given parameter domain.
5803 __isl_give isl_set
*isl_set_from_params(__isl_take isl_set
*set
)
5806 space
= isl_set_get_space(set
);
5807 space
= isl_space_set_from_params(space
);
5808 set
= isl_set_reset_space(set
, space
);
5812 /* Compute the parameter domain of the given map.
5814 __isl_give isl_set
*isl_map_params(__isl_take isl_map
*map
)
5817 isl_size n_in
, n_out
;
5819 n_in
= isl_map_dim(map
, isl_dim_in
);
5820 n_out
= isl_map_dim(map
, isl_dim_out
);
5821 if (n_in
< 0 || n_out
< 0)
5822 return isl_map_free(map
);
5823 map
= isl_map_project_out(map
, isl_dim_in
, 0, n_in
);
5824 map
= isl_map_project_out(map
, isl_dim_out
, 0, n_out
);
5825 space
= isl_map_get_space(map
);
5826 space
= isl_space_params(space
);
5827 map
= isl_map_reset_space(map
, space
);
5831 __isl_give isl_basic_set
*isl_basic_map_domain(__isl_take isl_basic_map
*bmap
)
5836 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5838 return isl_basic_map_free(bmap
);
5839 space
= isl_space_domain(isl_basic_map_get_space(bmap
));
5841 bmap
= isl_basic_map_project_out(bmap
, isl_dim_out
, 0, n_out
);
5843 return isl_basic_map_reset_space(bmap
, space
);
5846 isl_bool
isl_basic_map_may_be_set(__isl_keep isl_basic_map
*bmap
)
5849 return isl_bool_error
;
5850 return isl_space_may_be_set(bmap
->dim
);
5853 /* Is this basic map actually a set?
5854 * Users should never call this function. Outside of isl,
5855 * the type should indicate whether something is a set or a map.
5857 isl_bool
isl_basic_map_is_set(__isl_keep isl_basic_map
*bmap
)
5860 return isl_bool_error
;
5861 return isl_space_is_set(bmap
->dim
);
5864 __isl_give isl_basic_set
*isl_basic_map_range(__isl_take isl_basic_map
*bmap
)
5868 is_set
= isl_basic_map_is_set(bmap
);
5873 return isl_basic_map_domain(isl_basic_map_reverse(bmap
));
5875 isl_basic_map_free(bmap
);
5879 __isl_give isl_basic_map
*isl_basic_map_domain_map(
5880 __isl_take isl_basic_map
*bmap
)
5884 isl_basic_map
*domain
;
5885 isl_size nparam
, n_in
, n_out
;
5887 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5888 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5889 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5890 if (nparam
< 0 || n_in
< 0 || n_out
< 0)
5891 return isl_basic_map_free(bmap
);
5893 space
= isl_basic_map_get_space(bmap
);
5894 space
= isl_space_from_range(isl_space_domain(space
));
5895 domain
= isl_basic_map_universe(space
);
5897 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
5898 bmap
= isl_basic_map_apply_range(bmap
, domain
);
5899 bmap
= isl_basic_map_extend_constraints(bmap
, n_in
, 0);
5901 for (i
= 0; i
< n_in
; ++i
)
5902 bmap
= isl_basic_map_equate(bmap
, isl_dim_in
, i
,
5905 bmap
= isl_basic_map_gauss(bmap
, NULL
);
5906 return isl_basic_map_finalize(bmap
);
5909 __isl_give isl_basic_map
*isl_basic_map_range_map(
5910 __isl_take isl_basic_map
*bmap
)
5914 isl_basic_map
*range
;
5915 isl_size nparam
, n_in
, n_out
;
5917 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5918 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5919 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5920 if (nparam
< 0 || n_in
< 0 || n_out
< 0)
5921 return isl_basic_map_free(bmap
);
5923 space
= isl_basic_map_get_space(bmap
);
5924 space
= isl_space_from_range(isl_space_range(space
));
5925 range
= isl_basic_map_universe(space
);
5927 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
5928 bmap
= isl_basic_map_apply_range(bmap
, range
);
5929 bmap
= isl_basic_map_extend_constraints(bmap
, n_out
, 0);
5931 for (i
= 0; i
< n_out
; ++i
)
5932 bmap
= isl_basic_map_equate(bmap
, isl_dim_in
, n_in
+ i
,
5935 bmap
= isl_basic_map_gauss(bmap
, NULL
);
5936 return isl_basic_map_finalize(bmap
);
5939 int isl_map_may_be_set(__isl_keep isl_map
*map
)
5943 return isl_space_may_be_set(map
->dim
);
5946 /* Is this map actually a set?
5947 * Users should never call this function. Outside of isl,
5948 * the type should indicate whether something is a set or a map.
5950 isl_bool
isl_map_is_set(__isl_keep isl_map
*map
)
5953 return isl_bool_error
;
5954 return isl_space_is_set(map
->dim
);
5957 __isl_give isl_set
*isl_map_range(__isl_take isl_map
*map
)
5961 struct isl_set
*set
;
5963 is_set
= isl_map_is_set(map
);
5967 return set_from_map(map
);
5969 map
= isl_map_cow(map
);
5973 set
= set_from_map(map
);
5974 set
->dim
= isl_space_range(set
->dim
);
5977 for (i
= 0; i
< map
->n
; ++i
) {
5978 set
->p
[i
] = isl_basic_map_range(map
->p
[i
]);
5982 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
5983 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
5990 /* Transform "map" by applying "fn_space" to its space and "fn_bmap"
5991 * to each of its basic maps.
5993 static __isl_give isl_map
*isl_map_transform(__isl_take isl_map
*map
,
5994 __isl_give isl_space
*(*fn_space
)(__isl_take isl_space
*space
),
5995 __isl_give isl_basic_map
*(*fn_bmap
)(__isl_take isl_basic_map
*bmap
))
6000 map
= isl_map_cow(map
);
6004 for (i
= 0; i
< map
->n
; ++i
) {
6005 map
->p
[i
] = fn_bmap(map
->p
[i
]);
6007 return isl_map_free(map
);
6009 map
= isl_map_unmark_normalized(map
);
6011 space
= isl_map_take_space(map
);
6012 space
= fn_space(space
);
6013 map
= isl_map_restore_space(map
, space
);
6018 __isl_give isl_map
*isl_map_domain_map(__isl_take isl_map
*map
)
6020 return isl_map_transform(map
, &isl_space_domain_map
,
6021 &isl_basic_map_domain_map
);
6024 __isl_give isl_map
*isl_map_range_map(__isl_take isl_map
*map
)
6026 return isl_map_transform(map
, &isl_space_range_map
,
6027 &isl_basic_map_range_map
);
6030 /* Given a wrapped map of the form A[B -> C],
6031 * return the map A[B -> C] -> B.
6033 __isl_give isl_map
*isl_set_wrapped_domain_map(__isl_take isl_set
*set
)
6040 if (!isl_set_has_tuple_id(set
))
6041 return isl_map_domain_map(isl_set_unwrap(set
));
6043 id
= isl_set_get_tuple_id(set
);
6044 map
= isl_map_domain_map(isl_set_unwrap(set
));
6045 map
= isl_map_set_tuple_id(map
, isl_dim_in
, id
);
6050 __isl_give isl_basic_map
*isl_basic_map_from_domain(
6051 __isl_take isl_basic_set
*bset
)
6053 return isl_basic_map_reverse(isl_basic_map_from_range(bset
));
6056 __isl_give isl_basic_map
*isl_basic_map_from_range(
6057 __isl_take isl_basic_set
*bset
)
6060 space
= isl_basic_set_get_space(bset
);
6061 space
= isl_space_from_range(space
);
6062 bset
= isl_basic_set_reset_space(bset
, space
);
6063 return bset_to_bmap(bset
);
6066 /* Create a relation with the given set as range.
6067 * The domain of the created relation is a zero-dimensional
6068 * flat anonymous space.
6070 __isl_give isl_map
*isl_map_from_range(__isl_take isl_set
*set
)
6073 space
= isl_set_get_space(set
);
6074 space
= isl_space_from_range(space
);
6075 set
= isl_set_reset_space(set
, space
);
6076 return set_to_map(set
);
6079 /* Create a relation with the given set as domain.
6080 * The range of the created relation is a zero-dimensional
6081 * flat anonymous space.
6083 __isl_give isl_map
*isl_map_from_domain(__isl_take isl_set
*set
)
6085 return isl_map_reverse(isl_map_from_range(set
));
6088 __isl_give isl_basic_map
*isl_basic_map_from_domain_and_range(
6089 __isl_take isl_basic_set
*domain
, __isl_take isl_basic_set
*range
)
6091 return isl_basic_map_apply_range(isl_basic_map_reverse(domain
), range
);
6094 __isl_give isl_map
*isl_map_from_domain_and_range(__isl_take isl_set
*domain
,
6095 __isl_take isl_set
*range
)
6097 return isl_map_apply_range(isl_map_reverse(domain
), range
);
6100 /* Return a newly allocated isl_map with given space and flags and
6101 * room for "n" basic maps.
6102 * Make sure that all cached information is cleared.
6104 __isl_give isl_map
*isl_map_alloc_space(__isl_take isl_space
*space
, int n
,
6107 struct isl_map
*map
;
6112 isl_die(space
->ctx
, isl_error_internal
,
6113 "negative number of basic maps", goto error
);
6114 map
= isl_calloc(space
->ctx
, struct isl_map
,
6115 sizeof(struct isl_map
) +
6116 (n
- 1) * sizeof(struct isl_basic_map
*));
6120 map
->ctx
= space
->ctx
;
6121 isl_ctx_ref(map
->ctx
);
6129 isl_space_free(space
);
6133 __isl_give isl_basic_map
*isl_basic_map_empty(__isl_take isl_space
*space
)
6135 struct isl_basic_map
*bmap
;
6136 bmap
= isl_basic_map_alloc_space(space
, 0, 1, 0);
6137 bmap
= isl_basic_map_set_to_empty(bmap
);
6141 __isl_give isl_basic_set
*isl_basic_set_empty(__isl_take isl_space
*space
)
6143 struct isl_basic_set
*bset
;
6144 bset
= isl_basic_set_alloc_space(space
, 0, 1, 0);
6145 bset
= isl_basic_set_set_to_empty(bset
);
6149 __isl_give isl_basic_map
*isl_basic_map_universe(__isl_take isl_space
*space
)
6151 struct isl_basic_map
*bmap
;
6152 bmap
= isl_basic_map_alloc_space(space
, 0, 0, 0);
6153 bmap
= isl_basic_map_finalize(bmap
);
6157 __isl_give isl_basic_set
*isl_basic_set_universe(__isl_take isl_space
*space
)
6159 struct isl_basic_set
*bset
;
6160 bset
= isl_basic_set_alloc_space(space
, 0, 0, 0);
6161 bset
= isl_basic_set_finalize(bset
);
6165 __isl_give isl_basic_map
*isl_basic_map_nat_universe(
6166 __isl_take isl_space
*space
)
6169 isl_size total
= isl_space_dim(space
, isl_dim_all
);
6170 isl_basic_map
*bmap
;
6173 space
= isl_space_free(space
);
6174 bmap
= isl_basic_map_alloc_space(space
, 0, 0, total
);
6175 for (i
= 0; i
< total
; ++i
) {
6176 int k
= isl_basic_map_alloc_inequality(bmap
);
6179 isl_seq_clr(bmap
->ineq
[k
], 1 + total
);
6180 isl_int_set_si(bmap
->ineq
[k
][1 + i
], 1);
6184 isl_basic_map_free(bmap
);
6188 __isl_give isl_basic_set
*isl_basic_set_nat_universe(
6189 __isl_take isl_space
*space
)
6191 return isl_basic_map_nat_universe(space
);
6194 __isl_give isl_map
*isl_map_nat_universe(__isl_take isl_space
*space
)
6196 return isl_map_from_basic_map(isl_basic_map_nat_universe(space
));
6199 __isl_give isl_set
*isl_set_nat_universe(__isl_take isl_space
*space
)
6201 return isl_map_nat_universe(space
);
6204 __isl_give isl_map
*isl_map_empty(__isl_take isl_space
*space
)
6206 return isl_map_alloc_space(space
, 0, ISL_MAP_DISJOINT
);
6209 __isl_give isl_set
*isl_set_empty(__isl_take isl_space
*space
)
6211 return isl_set_alloc_space(space
, 0, ISL_MAP_DISJOINT
);
6214 __isl_give isl_map
*isl_map_universe(__isl_take isl_space
*space
)
6216 struct isl_map
*map
;
6219 map
= isl_map_alloc_space(isl_space_copy(space
), 1, ISL_MAP_DISJOINT
);
6220 map
= isl_map_add_basic_map(map
, isl_basic_map_universe(space
));
6224 __isl_give isl_set
*isl_set_universe(__isl_take isl_space
*space
)
6226 struct isl_set
*set
;
6229 set
= isl_set_alloc_space(isl_space_copy(space
), 1, ISL_MAP_DISJOINT
);
6230 set
= isl_set_add_basic_set(set
, isl_basic_set_universe(space
));
6234 __isl_give isl_map
*isl_map_dup(__isl_keep isl_map
*map
)
6237 struct isl_map
*dup
;
6241 dup
= isl_map_alloc_space(isl_space_copy(map
->dim
), map
->n
, map
->flags
);
6242 for (i
= 0; i
< map
->n
; ++i
)
6243 dup
= isl_map_add_basic_map(dup
, isl_basic_map_copy(map
->p
[i
]));
6247 __isl_give isl_map
*isl_map_add_basic_map(__isl_take isl_map
*map
,
6248 __isl_take isl_basic_map
*bmap
)
6252 if (isl_basic_map_plain_is_empty(bmap
)) {
6253 isl_basic_map_free(bmap
);
6256 if (isl_map_basic_map_check_equal_space(map
, bmap
) < 0)
6258 isl_assert(map
->ctx
, map
->n
< map
->size
, goto error
);
6259 map
->p
[map
->n
] = bmap
;
6261 map
= isl_map_unmark_normalized(map
);
6267 isl_basic_map_free(bmap
);
6271 __isl_null isl_map
*isl_map_free(__isl_take isl_map
*map
)
6282 isl_ctx_deref(map
->ctx
);
6283 for (i
= 0; i
< map
->n
; ++i
)
6284 isl_basic_map_free(map
->p
[i
]);
6285 isl_space_free(map
->dim
);
6291 static __isl_give isl_basic_map
*isl_basic_map_fix_pos_si(
6292 __isl_take isl_basic_map
*bmap
, unsigned pos
, int value
)
6297 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
6299 return isl_basic_map_free(bmap
);
6301 bmap
= isl_basic_map_cow(bmap
);
6302 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
6303 j
= isl_basic_map_alloc_equality(bmap
);
6306 isl_seq_clr(bmap
->eq
[j
] + 1, total
);
6307 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
6308 isl_int_set_si(bmap
->eq
[j
][0], value
);
6309 bmap
= isl_basic_map_simplify(bmap
);
6310 return isl_basic_map_finalize(bmap
);
6312 isl_basic_map_free(bmap
);
6316 static __isl_give isl_basic_map
*isl_basic_map_fix_pos(
6317 __isl_take isl_basic_map
*bmap
, unsigned pos
, isl_int value
)
6322 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
6324 return isl_basic_map_free(bmap
);
6326 bmap
= isl_basic_map_cow(bmap
);
6327 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
6328 j
= isl_basic_map_alloc_equality(bmap
);
6331 isl_seq_clr(bmap
->eq
[j
] + 1, total
);
6332 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
6333 isl_int_set(bmap
->eq
[j
][0], value
);
6334 bmap
= isl_basic_map_simplify(bmap
);
6335 return isl_basic_map_finalize(bmap
);
6337 isl_basic_map_free(bmap
);
6341 __isl_give isl_basic_map
*isl_basic_map_fix_si(__isl_take isl_basic_map
*bmap
,
6342 enum isl_dim_type type
, unsigned pos
, int value
)
6344 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6345 return isl_basic_map_free(bmap
);
6346 return isl_basic_map_fix_pos_si(bmap
,
6347 isl_basic_map_offset(bmap
, type
) + pos
, value
);
6350 __isl_give isl_basic_map
*isl_basic_map_fix(__isl_take isl_basic_map
*bmap
,
6351 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6353 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6354 return isl_basic_map_free(bmap
);
6355 return isl_basic_map_fix_pos(bmap
,
6356 isl_basic_map_offset(bmap
, type
) + pos
, value
);
6359 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
6360 * to be equal to "v".
6362 __isl_give isl_basic_map
*isl_basic_map_fix_val(__isl_take isl_basic_map
*bmap
,
6363 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6367 if (!isl_val_is_int(v
))
6368 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
6369 "expecting integer value", goto error
);
6370 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6372 pos
+= isl_basic_map_offset(bmap
, type
);
6373 bmap
= isl_basic_map_fix_pos(bmap
, pos
, v
->n
);
6377 isl_basic_map_free(bmap
);
6382 /* Fix the value of the variable at position "pos" of type "type" of "bset"
6383 * to be equal to "v".
6385 __isl_give isl_basic_set
*isl_basic_set_fix_val(__isl_take isl_basic_set
*bset
,
6386 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6388 return isl_basic_map_fix_val(bset
, type
, pos
, v
);
6391 __isl_give isl_basic_set
*isl_basic_set_fix_si(__isl_take isl_basic_set
*bset
,
6392 enum isl_dim_type type
, unsigned pos
, int value
)
6394 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset
),
6398 __isl_give isl_basic_set
*isl_basic_set_fix(__isl_take isl_basic_set
*bset
,
6399 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6401 return bset_from_bmap(isl_basic_map_fix(bset_to_bmap(bset
),
6405 /* Remove the basic map at position "i" from "map" if this basic map
6406 * is (obviously) empty.
6408 static __isl_give isl_map
*remove_if_empty(__isl_take isl_map
*map
, int i
)
6415 empty
= isl_basic_map_plain_is_empty(map
->p
[i
]);
6417 return isl_map_free(map
);
6421 isl_basic_map_free(map
->p
[i
]);
6424 map
->p
[i
] = map
->p
[map
->n
];
6425 map
= isl_map_unmark_normalized(map
);
6432 /* Perform "fn" on each basic map of "map", where we may not be holding
6433 * the only reference to "map".
6434 * In particular, "fn" should be a semantics preserving operation
6435 * that we want to apply to all copies of "map". We therefore need
6436 * to be careful not to modify "map" in a way that breaks "map"
6437 * in case anything goes wrong.
6439 __isl_give isl_map
*isl_map_inline_foreach_basic_map(__isl_take isl_map
*map
,
6440 __isl_give isl_basic_map
*(*fn
)(__isl_take isl_basic_map
*bmap
))
6442 struct isl_basic_map
*bmap
;
6448 for (i
= map
->n
- 1; i
>= 0; --i
) {
6449 bmap
= isl_basic_map_copy(map
->p
[i
]);
6453 isl_basic_map_free(map
->p
[i
]);
6455 map
= remove_if_empty(map
, i
);
6466 __isl_give isl_map
*isl_map_fix_si(__isl_take isl_map
*map
,
6467 enum isl_dim_type type
, unsigned pos
, int value
)
6471 map
= isl_map_cow(map
);
6472 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
6473 return isl_map_free(map
);
6474 for (i
= map
->n
- 1; i
>= 0; --i
) {
6475 map
->p
[i
] = isl_basic_map_fix_si(map
->p
[i
], type
, pos
, value
);
6476 map
= remove_if_empty(map
, i
);
6480 map
= isl_map_unmark_normalized(map
);
6484 __isl_give isl_set
*isl_set_fix_si(__isl_take isl_set
*set
,
6485 enum isl_dim_type type
, unsigned pos
, int value
)
6487 return set_from_map(isl_map_fix_si(set_to_map(set
), type
, pos
, value
));
6490 __isl_give isl_map
*isl_map_fix(__isl_take isl_map
*map
,
6491 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6495 map
= isl_map_cow(map
);
6496 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
6497 return isl_map_free(map
);
6498 for (i
= 0; i
< map
->n
; ++i
) {
6499 map
->p
[i
] = isl_basic_map_fix(map
->p
[i
], type
, pos
, value
);
6503 map
= isl_map_unmark_normalized(map
);
6510 __isl_give isl_set
*isl_set_fix(__isl_take isl_set
*set
,
6511 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6513 return set_from_map(isl_map_fix(set_to_map(set
), type
, pos
, value
));
6516 /* Fix the value of the variable at position "pos" of type "type" of "map"
6517 * to be equal to "v".
6519 __isl_give isl_map
*isl_map_fix_val(__isl_take isl_map
*map
,
6520 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6524 map
= isl_map_cow(map
);
6528 if (!isl_val_is_int(v
))
6529 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
6530 "expecting integer value", goto error
);
6531 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
6533 for (i
= map
->n
- 1; i
>= 0; --i
) {
6534 map
->p
[i
] = isl_basic_map_fix_val(map
->p
[i
], type
, pos
,
6536 map
= remove_if_empty(map
, i
);
6540 map
= isl_map_unmark_normalized(map
);
6549 /* Fix the value of the variable at position "pos" of type "type" of "set"
6550 * to be equal to "v".
6552 __isl_give isl_set
*isl_set_fix_val(__isl_take isl_set
*set
,
6553 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6555 return isl_map_fix_val(set
, type
, pos
, v
);
6558 __isl_give isl_map
*isl_map_fix_input_si(__isl_take isl_map
*map
,
6559 unsigned input
, int value
)
6561 return isl_map_fix_si(map
, isl_dim_in
, input
, value
);
6564 __isl_give isl_set
*isl_set_fix_dim_si(__isl_take isl_set
*set
, unsigned dim
,
6567 return set_from_map(isl_map_fix_si(set_to_map(set
),
6568 isl_dim_set
, dim
, value
));
6571 static __isl_give isl_basic_map
*basic_map_bound_si(
6572 __isl_take isl_basic_map
*bmap
,
6573 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
6578 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6579 return isl_basic_map_free(bmap
);
6580 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
6582 return isl_basic_map_free(bmap
);
6583 pos
+= isl_basic_map_offset(bmap
, type
);
6584 bmap
= isl_basic_map_cow(bmap
);
6585 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
6586 j
= isl_basic_map_alloc_inequality(bmap
);
6589 isl_seq_clr(bmap
->ineq
[j
], 1 + total
);
6591 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
6592 isl_int_set_si(bmap
->ineq
[j
][0], value
);
6594 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
6595 isl_int_set_si(bmap
->ineq
[j
][0], -value
);
6597 bmap
= isl_basic_map_simplify(bmap
);
6598 return isl_basic_map_finalize(bmap
);
6600 isl_basic_map_free(bmap
);
6604 __isl_give isl_basic_map
*isl_basic_map_lower_bound_si(
6605 __isl_take isl_basic_map
*bmap
,
6606 enum isl_dim_type type
, unsigned pos
, int value
)
6608 return basic_map_bound_si(bmap
, type
, pos
, value
, 0);
6611 /* Constrain the values of the given dimension to be no greater than "value".
6613 __isl_give isl_basic_map
*isl_basic_map_upper_bound_si(
6614 __isl_take isl_basic_map
*bmap
,
6615 enum isl_dim_type type
, unsigned pos
, int value
)
6617 return basic_map_bound_si(bmap
, type
, pos
, value
, 1);
6620 static __isl_give isl_map
*map_bound_si(__isl_take isl_map
*map
,
6621 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
6625 map
= isl_map_cow(map
);
6626 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
6627 return isl_map_free(map
);
6628 for (i
= 0; i
< map
->n
; ++i
) {
6629 map
->p
[i
] = basic_map_bound_si(map
->p
[i
],
6630 type
, pos
, value
, upper
);
6634 map
= isl_map_unmark_normalized(map
);
6641 __isl_give isl_map
*isl_map_lower_bound_si(__isl_take isl_map
*map
,
6642 enum isl_dim_type type
, unsigned pos
, int value
)
6644 return map_bound_si(map
, type
, pos
, value
, 0);
6647 __isl_give isl_map
*isl_map_upper_bound_si(__isl_take isl_map
*map
,
6648 enum isl_dim_type type
, unsigned pos
, int value
)
6650 return map_bound_si(map
, type
, pos
, value
, 1);
6653 __isl_give isl_set
*isl_set_lower_bound_si(__isl_take isl_set
*set
,
6654 enum isl_dim_type type
, unsigned pos
, int value
)
6656 return set_from_map(isl_map_lower_bound_si(set_to_map(set
),
6660 __isl_give isl_set
*isl_set_upper_bound_si(__isl_take isl_set
*set
,
6661 enum isl_dim_type type
, unsigned pos
, int value
)
6663 return isl_map_upper_bound_si(set
, type
, pos
, value
);
6666 /* Bound the given variable of "bmap" from below (or above is "upper"
6667 * is set) to "value".
6669 static __isl_give isl_basic_map
*basic_map_bound(
6670 __isl_take isl_basic_map
*bmap
,
6671 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
6676 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6677 return isl_basic_map_free(bmap
);
6678 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
6680 return isl_basic_map_free(bmap
);
6681 pos
+= isl_basic_map_offset(bmap
, type
);
6682 bmap
= isl_basic_map_cow(bmap
);
6683 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
6684 j
= isl_basic_map_alloc_inequality(bmap
);
6687 isl_seq_clr(bmap
->ineq
[j
], 1 + total
);
6689 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
6690 isl_int_set(bmap
->ineq
[j
][0], value
);
6692 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
6693 isl_int_neg(bmap
->ineq
[j
][0], value
);
6695 bmap
= isl_basic_map_simplify(bmap
);
6696 return isl_basic_map_finalize(bmap
);
6698 isl_basic_map_free(bmap
);
6702 /* Bound the given variable of "map" from below (or above is "upper"
6703 * is set) to "value".
6705 static __isl_give isl_map
*map_bound(__isl_take isl_map
*map
,
6706 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
6710 map
= isl_map_cow(map
);
6711 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
6712 return isl_map_free(map
);
6713 for (i
= map
->n
- 1; i
>= 0; --i
) {
6714 map
->p
[i
] = basic_map_bound(map
->p
[i
], type
, pos
, value
, upper
);
6715 map
= remove_if_empty(map
, i
);
6719 map
= isl_map_unmark_normalized(map
);
6723 __isl_give isl_map
*isl_map_lower_bound(__isl_take isl_map
*map
,
6724 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6726 return map_bound(map
, type
, pos
, value
, 0);
6729 __isl_give isl_map
*isl_map_upper_bound(__isl_take isl_map
*map
,
6730 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6732 return map_bound(map
, type
, pos
, value
, 1);
6735 __isl_give isl_set
*isl_set_lower_bound(__isl_take isl_set
*set
,
6736 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6738 return isl_map_lower_bound(set
, type
, pos
, value
);
6741 __isl_give isl_set
*isl_set_upper_bound(__isl_take isl_set
*set
,
6742 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6744 return isl_map_upper_bound(set
, type
, pos
, value
);
6747 /* Force the values of the variable at position "pos" of type "type" of "map"
6748 * to be no smaller than "value".
6750 __isl_give isl_map
*isl_map_lower_bound_val(__isl_take isl_map
*map
,
6751 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
6755 if (!isl_val_is_int(value
))
6756 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
6757 "expecting integer value", goto error
);
6758 map
= isl_map_lower_bound(map
, type
, pos
, value
->n
);
6759 isl_val_free(value
);
6762 isl_val_free(value
);
6767 /* Force the values of the variable at position "pos" of type "type" of "set"
6768 * to be no smaller than "value".
6770 __isl_give isl_set
*isl_set_lower_bound_val(__isl_take isl_set
*set
,
6771 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
6775 map
= set_to_map(set
);
6776 return set_from_map(isl_map_lower_bound_val(map
, type
, pos
, value
));
6779 /* Force the values of the variable at position "pos" of type "type" of "map"
6780 * to be no greater than "value".
6782 __isl_give isl_map
*isl_map_upper_bound_val(__isl_take isl_map
*map
,
6783 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
6787 if (!isl_val_is_int(value
))
6788 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
6789 "expecting integer value", goto error
);
6790 map
= isl_map_upper_bound(map
, type
, pos
, value
->n
);
6791 isl_val_free(value
);
6794 isl_val_free(value
);
6799 /* Force the values of the variable at position "pos" of type "type" of "set"
6800 * to be no greater than "value".
6802 __isl_give isl_set
*isl_set_upper_bound_val(__isl_take isl_set
*set
,
6803 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
6807 map
= set_to_map(set
);
6808 return set_from_map(isl_map_upper_bound_val(map
, type
, pos
, value
));
6813 #include "isl_map_bound_templ.c"
6815 /* Apply "map_bound" to "set" with the corresponding value in "bound"
6816 * for each set dimension, by treating the set as a map.
6818 static __isl_give isl_set
*set_bound_multi_val(__isl_take isl_set
*set
,
6819 __isl_take isl_multi_val
*bound
,
6820 __isl_give isl_map
*map_bound(__isl_take isl_map
*map
,
6821 unsigned pos
, __isl_take isl_val
*value
))
6825 map
= set_to_map(set
);
6826 return set_from_map(map_bound_multi_val(map
, bound
, map_bound
));
6831 #include "isl_map_bound_templ.c"
6833 /* Apply "map_bound" to "set" with the corresponding value in "bound"
6834 * for each set dimension, by converting the set and the bound
6835 * to objects living in a map space.
6837 static __isl_give isl_set
*set_bound_multi_pw_aff(__isl_take isl_set
*set
,
6838 __isl_take isl_multi_pw_aff
*bound
,
6839 __isl_give isl_map
*set_bound(__isl_take isl_map
*map
,
6840 unsigned pos
, __isl_take TYPE
*value
))
6844 map
= isl_map_from_range(set
);
6845 bound
= isl_multi_pw_aff_from_range(bound
);
6846 map
= map_bound_multi_pw_aff(map
, bound
, set_bound
);
6847 return isl_map_range(map
);
6850 /* Wrapper around isl_map_lower_bound_val for use in map_bound_multi_val,
6851 * setting a bound on the given output dimension.
6853 static __isl_give isl_map
*map_lower_bound_val(__isl_take isl_map
*map
,
6854 unsigned pos
, __isl_take isl_val
*v
)
6856 return isl_map_lower_bound_val(map
, isl_dim_out
, pos
, v
);
6859 /* Force the values of the set dimensions of "set"
6860 * to be no smaller than the corresponding values in "lower".
6862 __isl_give isl_set
*isl_set_lower_bound_multi_val(__isl_take isl_set
*set
,
6863 __isl_take isl_multi_val
*lower
)
6865 return set_bound_multi_val(set
, lower
, &map_lower_bound_val
);
6868 /* Force the values of the output dimensions of "map"
6869 * to be no smaller than the corresponding values in "lower".
6871 __isl_give isl_map
*isl_map_lower_bound_multi_val(__isl_take isl_map
*map
,
6872 __isl_take isl_multi_val
*lower
)
6874 return map_bound_multi_val(map
, lower
, &map_lower_bound_val
);
6877 /* Wrapper around isl_map_upper_bound_val for use in map_bound_multi_val,
6878 * setting a bound on the given output dimension.
6880 static __isl_give isl_map
*map_upper_bound_val(__isl_take isl_map
*map
,
6881 unsigned pos
, __isl_take isl_val
*v
)
6883 return isl_map_upper_bound_val(map
, isl_dim_out
, pos
, v
);
6886 /* Force the values of the set dimensions of "set"
6887 * to be no greater than the corresponding values in "upper".
6889 __isl_give isl_set
*isl_set_upper_bound_multi_val(__isl_take isl_set
*set
,
6890 __isl_take isl_multi_val
*upper
)
6892 return set_bound_multi_val(set
, upper
, &map_upper_bound_val
);
6895 /* Force the values of the set dimensions of "set"
6896 * to be no greater than the corresponding values in "upper".
6898 __isl_give isl_map
*isl_map_upper_bound_multi_val(__isl_take isl_map
*map
,
6899 __isl_take isl_multi_val
*upper
)
6901 return map_bound_multi_val(map
, upper
, &map_upper_bound_val
);
6904 /* Force the symbolic constant expression "bound"
6905 * to satisfy the relation "order" with respect to
6906 * the output variable at position "pos" of "map".
6908 * Create an affine expression representing the output variable
6909 * in terms of the range and
6910 * compare it using "order" to "bound" (defined on the domain).
6911 * The result is a relation between elements in domain and range that
6912 * can be intersected with "map".
6914 static __isl_give isl_map
*map_bound_pw_aff(__isl_take isl_map
*map
,
6915 unsigned pos
, __isl_take isl_pw_aff
*bound
,
6916 __isl_give isl_map
*(*order
)(__isl_take isl_pw_aff
*pa1
,
6917 __isl_take isl_pw_aff
*pa2
))
6920 isl_local_space
*ls
;
6923 space
= isl_space_range(isl_map_get_space(map
));
6924 ls
= isl_local_space_from_space(space
);
6925 var
= isl_pw_aff_var_on_domain(ls
, isl_dim_set
, pos
);
6926 map
= isl_map_intersect(map
, order(bound
, var
));
6930 /* Force the values of the output variable at position "pos" of "map"
6931 * to be no smaller than the symbolic constant expression "lower".
6933 static __isl_give isl_map
*map_lower_bound_pw_aff(__isl_take isl_map
*map
,
6934 unsigned pos
, __isl_take isl_pw_aff
*lower
)
6936 return map_bound_pw_aff(map
, pos
, lower
, &isl_pw_aff_le_map
);
6939 /* Force the values of the output variable at position "pos" of "map"
6940 * to be no greater than the symbolic constant expression "upper".
6942 static __isl_give isl_map
*map_upper_bound_pw_aff(__isl_take isl_map
*map
,
6943 unsigned pos
, __isl_take isl_pw_aff
*upper
)
6945 return map_bound_pw_aff(map
, pos
, upper
, &isl_pw_aff_ge_map
);
6948 /* Force the values of the set dimensions of "set"
6949 * to be no smaller than the corresponding constant symbolic expressions
6952 __isl_give isl_set
*isl_set_lower_bound_multi_pw_aff(__isl_take isl_set
*set
,
6953 __isl_take isl_multi_pw_aff
*lower
)
6955 return set_bound_multi_pw_aff(set
, lower
, &map_lower_bound_pw_aff
);
6958 /* Force the values of the set dimensions of "set"
6959 * to be no greater than the corresponding constant symbolic expressions
6962 __isl_give isl_set
*isl_set_upper_bound_multi_pw_aff(__isl_take isl_set
*set
,
6963 __isl_take isl_multi_pw_aff
*upper
)
6965 return set_bound_multi_pw_aff(set
, upper
, &map_upper_bound_pw_aff
);
6968 /* Force the values of the output dimensions of "map"
6969 * to be no smaller than the corresponding constant symbolic expressions
6972 __isl_give isl_map
*isl_map_lower_bound_multi_pw_aff(__isl_take isl_map
*map
,
6973 __isl_take isl_multi_pw_aff
*lower
)
6975 return map_bound_multi_pw_aff(map
, lower
, &map_lower_bound_pw_aff
);
6978 /* Force the values of the output dimensions of "map"
6979 * to be no greater than the corresponding constant symbolic expressions
6982 __isl_give isl_map
*isl_map_upper_bound_multi_pw_aff(__isl_take isl_map
*map
,
6983 __isl_take isl_multi_pw_aff
*upper
)
6985 return map_bound_multi_pw_aff(map
, upper
, &map_upper_bound_pw_aff
);
6988 /* Bound the given variable of "bset" from below (or above is "upper"
6989 * is set) to "value".
6991 static __isl_give isl_basic_set
*isl_basic_set_bound(
6992 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned pos
,
6993 isl_int value
, int upper
)
6995 return bset_from_bmap(basic_map_bound(bset_to_bmap(bset
),
6996 type
, pos
, value
, upper
));
6999 /* Bound the given variable of "bset" from below (or above is "upper"
7000 * is set) to "value".
7002 static __isl_give isl_basic_set
*isl_basic_set_bound_val(
7003 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned pos
,
7004 __isl_take isl_val
*value
, int upper
)
7008 if (!isl_val_is_int(value
))
7009 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
7010 "expecting integer value", goto error
);
7011 bset
= isl_basic_set_bound(bset
, type
, pos
, value
->n
, upper
);
7012 isl_val_free(value
);
7015 isl_val_free(value
);
7016 isl_basic_set_free(bset
);
7020 /* Bound the given variable of "bset" from below to "value".
7022 __isl_give isl_basic_set
*isl_basic_set_lower_bound_val(
7023 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned pos
,
7024 __isl_take isl_val
*value
)
7026 return isl_basic_set_bound_val(bset
, type
, pos
, value
, 0);
7029 /* Bound the given variable of "bset" from above to "value".
7031 __isl_give isl_basic_set
*isl_basic_set_upper_bound_val(
7032 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned pos
,
7033 __isl_take isl_val
*value
)
7035 return isl_basic_set_bound_val(bset
, type
, pos
, value
, 1);
7038 __isl_give isl_map
*isl_map_reverse(__isl_take isl_map
*map
)
7040 return isl_map_transform(map
, &isl_space_reverse
,
7041 &isl_basic_map_reverse
);
7044 /* Given a map A -> (B -> C), return the corresponding map A -> (C -> B).
7046 __isl_give isl_map
*isl_map_range_reverse(__isl_take isl_map
*map
)
7048 return isl_map_transform(map
, &isl_space_range_reverse
,
7049 &isl_basic_map_range_reverse
);
7053 #define TYPE isl_pw_multi_aff
7055 #define SUFFIX _pw_multi_aff
7057 #define EMPTY isl_pw_multi_aff_empty
7059 #define ADD isl_pw_multi_aff_union_add
7060 #include "isl_map_lexopt_templ.c"
7062 /* Given a map "map", compute the lexicographically minimal
7063 * (or maximal) image element for each domain element in dom,
7064 * in the form of an isl_pw_multi_aff.
7065 * If "empty" is not NULL, then set *empty to those elements in dom that
7066 * do not have an image element.
7067 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
7068 * should be computed over the domain of "map". "empty" is also NULL
7071 * We first compute the lexicographically minimal or maximal element
7072 * in the first basic map. This results in a partial solution "res"
7073 * and a subset "todo" of dom that still need to be handled.
7074 * We then consider each of the remaining maps in "map" and successively
7075 * update both "res" and "todo".
7076 * If "empty" is NULL, then the todo sets are not needed and therefore
7077 * also not computed.
7079 static __isl_give isl_pw_multi_aff
*isl_map_partial_lexopt_aligned_pw_multi_aff(
7080 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
7081 __isl_give isl_set
**empty
, unsigned flags
)
7085 isl_pw_multi_aff
*res
;
7088 full
= ISL_FL_ISSET(flags
, ISL_OPT_FULL
);
7089 if (!map
|| (!full
&& !dom
))
7092 if (isl_map_plain_is_empty(map
)) {
7097 return isl_pw_multi_aff_from_map(map
);
7100 res
= basic_map_partial_lexopt_pw_multi_aff(
7101 isl_basic_map_copy(map
->p
[0]),
7102 isl_set_copy(dom
), empty
, flags
);
7106 for (i
= 1; i
< map
->n
; ++i
) {
7107 isl_pw_multi_aff
*res_i
;
7109 res_i
= basic_map_partial_lexopt_pw_multi_aff(
7110 isl_basic_map_copy(map
->p
[i
]),
7111 isl_set_copy(dom
), empty
, flags
);
7113 if (ISL_FL_ISSET(flags
, ISL_OPT_MAX
))
7114 res
= isl_pw_multi_aff_union_lexmax(res
, res_i
);
7116 res
= isl_pw_multi_aff_union_lexmin(res
, res_i
);
7119 todo
= isl_set_intersect(todo
, *empty
);
7138 #define TYPE isl_map
7142 #define EMPTY isl_map_empty
7144 #define ADD isl_map_union_disjoint
7145 #include "isl_map_lexopt_templ.c"
7147 /* Given a map "map", compute the lexicographically minimal
7148 * (or maximal) image element for each domain element in "dom",
7149 * in the form of an isl_map.
7150 * If "empty" is not NULL, then set *empty to those elements in "dom" that
7151 * do not have an image element.
7152 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
7153 * should be computed over the domain of "map". "empty" is also NULL
7156 * If the input consists of more than one disjunct, then first
7157 * compute the desired result in the form of an isl_pw_multi_aff and
7158 * then convert that into an isl_map.
7160 * This function used to have an explicit implementation in terms
7161 * of isl_maps, but it would continually intersect the domains of
7162 * partial results with the complement of the domain of the next
7163 * partial solution, potentially leading to an explosion in the number
7164 * of disjuncts if there are several disjuncts in the input.
7165 * An even earlier implementation of this function would look for
7166 * better results in the domain of the partial result and for extra
7167 * results in the complement of this domain, which would lead to
7168 * even more splintering.
7170 static __isl_give isl_map
*isl_map_partial_lexopt_aligned(
7171 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
7172 __isl_give isl_set
**empty
, unsigned flags
)
7175 struct isl_map
*res
;
7176 isl_pw_multi_aff
*pma
;
7178 full
= ISL_FL_ISSET(flags
, ISL_OPT_FULL
);
7179 if (!map
|| (!full
&& !dom
))
7182 if (isl_map_plain_is_empty(map
)) {
7191 res
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[0]),
7197 pma
= isl_map_partial_lexopt_aligned_pw_multi_aff(map
, dom
, empty
,
7199 return isl_map_from_pw_multi_aff_internal(pma
);
7208 __isl_give isl_map
*isl_map_partial_lexmax(
7209 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
7210 __isl_give isl_set
**empty
)
7212 return isl_map_partial_lexopt(map
, dom
, empty
, ISL_OPT_MAX
);
7215 __isl_give isl_map
*isl_map_partial_lexmin(
7216 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
7217 __isl_give isl_set
**empty
)
7219 return isl_map_partial_lexopt(map
, dom
, empty
, 0);
7222 __isl_give isl_set
*isl_set_partial_lexmin(
7223 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
7224 __isl_give isl_set
**empty
)
7226 return set_from_map(isl_map_partial_lexmin(set_to_map(set
),
7230 __isl_give isl_set
*isl_set_partial_lexmax(
7231 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
7232 __isl_give isl_set
**empty
)
7234 return set_from_map(isl_map_partial_lexmax(set_to_map(set
),
7238 /* Compute the lexicographic minimum (or maximum if "flags" includes
7239 * ISL_OPT_MAX) of "bset" over its parametric domain.
7241 __isl_give isl_set
*isl_basic_set_lexopt(__isl_take isl_basic_set
*bset
,
7244 return isl_basic_map_lexopt(bset
, flags
);
7247 __isl_give isl_map
*isl_basic_map_lexmax(__isl_take isl_basic_map
*bmap
)
7249 return isl_basic_map_lexopt(bmap
, ISL_OPT_MAX
);
7252 __isl_give isl_set
*isl_basic_set_lexmin(__isl_take isl_basic_set
*bset
)
7254 return set_from_map(isl_basic_map_lexmin(bset_to_bmap(bset
)));
7257 __isl_give isl_set
*isl_basic_set_lexmax(__isl_take isl_basic_set
*bset
)
7259 return set_from_map(isl_basic_map_lexmax(bset_to_bmap(bset
)));
7262 /* Compute the lexicographic minimum of "bset" over its parametric domain
7263 * for the purpose of quantifier elimination.
7264 * That is, find an explicit representation for all the existentially
7265 * quantified variables in "bset" by computing their lexicographic
7268 static __isl_give isl_set
*isl_basic_set_lexmin_compute_divs(
7269 __isl_take isl_basic_set
*bset
)
7271 return isl_basic_set_lexopt(bset
, ISL_OPT_QE
);
7274 /* Given a basic map with one output dimension, compute the minimum or
7275 * maximum of that dimension as an isl_pw_aff.
7277 * Compute the optimum as a lexicographic optimum over the single
7278 * output dimension and extract the single isl_pw_aff from the result.
7280 static __isl_give isl_pw_aff
*basic_map_dim_opt(__isl_keep isl_basic_map
*bmap
,
7283 isl_pw_multi_aff
*pma
;
7286 bmap
= isl_basic_map_copy(bmap
);
7287 pma
= isl_basic_map_lexopt_pw_multi_aff(bmap
, max
? ISL_OPT_MAX
: 0);
7288 pwaff
= isl_pw_multi_aff_get_pw_aff(pma
, 0);
7289 isl_pw_multi_aff_free(pma
);
7294 /* Compute the minimum or maximum of the given output dimension
7295 * as a function of the parameters and the input dimensions,
7296 * but independently of the other output dimensions.
7298 * We first project out the other output dimension and then compute
7299 * the "lexicographic" maximum in each basic map, combining the results
7300 * using isl_pw_aff_union_max.
7302 static __isl_give isl_pw_aff
*map_dim_opt(__isl_take isl_map
*map
, int pos
,
7309 n_out
= isl_map_dim(map
, isl_dim_out
);
7311 map
= isl_map_free(map
);
7312 map
= isl_map_project_out(map
, isl_dim_out
, pos
+ 1, n_out
- (pos
+ 1));
7313 map
= isl_map_project_out(map
, isl_dim_out
, 0, pos
);
7318 isl_space
*space
= isl_map_get_space(map
);
7320 return isl_pw_aff_empty(space
);
7323 pwaff
= basic_map_dim_opt(map
->p
[0], max
);
7324 for (i
= 1; i
< map
->n
; ++i
) {
7325 isl_pw_aff
*pwaff_i
;
7327 pwaff_i
= basic_map_dim_opt(map
->p
[i
], max
);
7328 pwaff
= isl_pw_aff_union_opt(pwaff
, pwaff_i
, max
);
7336 /* Compute the minimum of the given output dimension as a function of the
7337 * parameters and input dimensions, but independently of
7338 * the other output dimensions.
7340 __isl_give isl_pw_aff
*isl_map_dim_min(__isl_take isl_map
*map
, int pos
)
7342 return map_dim_opt(map
, pos
, 0);
7345 /* Compute the maximum of the given output dimension as a function of the
7346 * parameters and input dimensions, but independently of
7347 * the other output dimensions.
7349 __isl_give isl_pw_aff
*isl_map_dim_max(__isl_take isl_map
*map
, int pos
)
7351 return map_dim_opt(map
, pos
, 1);
7354 /* Compute the minimum or maximum of the given set dimension
7355 * as a function of the parameters,
7356 * but independently of the other set dimensions.
7358 static __isl_give isl_pw_aff
*set_dim_opt(__isl_take isl_set
*set
, int pos
,
7361 return map_dim_opt(set
, pos
, max
);
7364 /* Compute the maximum of the given set dimension as a function of the
7365 * parameters, but independently of the other set dimensions.
7367 __isl_give isl_pw_aff
*isl_set_dim_max(__isl_take isl_set
*set
, int pos
)
7369 return set_dim_opt(set
, pos
, 1);
7372 /* Compute the minimum of the given set dimension as a function of the
7373 * parameters, but independently of the other set dimensions.
7375 __isl_give isl_pw_aff
*isl_set_dim_min(__isl_take isl_set
*set
, int pos
)
7377 return set_dim_opt(set
, pos
, 0);
7380 /* Apply a preimage specified by "mat" on the parameters of "bset".
7381 * bset is assumed to have only parameters and divs.
7383 static __isl_give isl_basic_set
*basic_set_parameter_preimage(
7384 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*mat
)
7388 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
7389 if (nparam
< 0 || !mat
)
7392 bset
->dim
= isl_space_cow(bset
->dim
);
7396 isl_assert(bset
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
7398 bset
->dim
->nparam
= 0;
7399 bset
->dim
->n_out
= nparam
;
7400 bset
= isl_basic_set_preimage(bset
, mat
);
7402 bset
->dim
->nparam
= bset
->dim
->n_out
;
7403 bset
->dim
->n_out
= 0;
7408 isl_basic_set_free(bset
);
7412 /* Apply a preimage specified by "mat" on the parameters of "set".
7413 * set is assumed to have only parameters and divs.
7415 static __isl_give isl_set
*set_parameter_preimage(__isl_take isl_set
*set
,
7416 __isl_take isl_mat
*mat
)
7421 nparam
= isl_set_dim(set
, isl_dim_param
);
7422 if (nparam
< 0 || !mat
)
7425 if (mat
->n_row
!= 1 + nparam
)
7426 isl_die(isl_set_get_ctx(set
), isl_error_internal
,
7427 "unexpected number of rows", goto error
);
7429 space
= isl_set_get_space(set
);
7430 space
= isl_space_move_dims(space
, isl_dim_set
, 0,
7431 isl_dim_param
, 0, nparam
);
7432 set
= isl_set_reset_space(set
, space
);
7433 set
= isl_set_preimage(set
, mat
);
7434 nparam
= isl_set_dim(set
, isl_dim_out
);
7436 set
= isl_set_free(set
);
7437 space
= isl_set_get_space(set
);
7438 space
= isl_space_move_dims(space
, isl_dim_param
, 0,
7439 isl_dim_out
, 0, nparam
);
7440 set
= isl_set_reset_space(set
, space
);
7448 /* Intersect the basic set "bset" with the affine space specified by the
7449 * equalities in "eq".
7451 static __isl_give isl_basic_set
*basic_set_append_equalities(
7452 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*eq
)
7460 bset
= isl_basic_set_extend(bset
, 0, eq
->n_row
, 0);
7464 len
= isl_basic_set_offset(bset
, isl_dim_div
) + bset
->extra
;
7465 for (i
= 0; i
< eq
->n_row
; ++i
) {
7466 k
= isl_basic_set_alloc_equality(bset
);
7469 isl_seq_cpy(bset
->eq
[k
], eq
->row
[i
], eq
->n_col
);
7470 isl_seq_clr(bset
->eq
[k
] + eq
->n_col
, len
- eq
->n_col
);
7474 bset
= isl_basic_set_gauss(bset
, NULL
);
7475 bset
= isl_basic_set_finalize(bset
);
7480 isl_basic_set_free(bset
);
7484 /* Intersect the set "set" with the affine space specified by the
7485 * equalities in "eq".
7487 static struct isl_set
*set_append_equalities(struct isl_set
*set
,
7495 for (i
= 0; i
< set
->n
; ++i
) {
7496 set
->p
[i
] = basic_set_append_equalities(set
->p
[i
],
7509 /* Given a basic set "bset" that only involves parameters and existentially
7510 * quantified variables, return the index of the first equality
7511 * that only involves parameters. If there is no such equality then
7512 * return bset->n_eq.
7514 * This function assumes that isl_basic_set_gauss has been called on "bset".
7516 static int first_parameter_equality(__isl_keep isl_basic_set
*bset
)
7519 isl_size nparam
, n_div
;
7521 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
7522 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
7523 if (nparam
< 0 || n_div
< 0)
7526 for (i
= 0, j
= n_div
- 1; i
< bset
->n_eq
&& j
>= 0; --j
) {
7527 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ j
]))
7534 /* Compute an explicit representation for the existentially quantified
7535 * variables in "bset" by computing the "minimal value" of the set
7536 * variables. Since there are no set variables, the computation of
7537 * the minimal value essentially computes an explicit representation
7538 * of the non-empty part(s) of "bset".
7540 * The input only involves parameters and existentially quantified variables.
7541 * All equalities among parameters have been removed.
7543 * Since the existentially quantified variables in the result are in general
7544 * going to be different from those in the input, we first replace
7545 * them by the minimal number of variables based on their equalities.
7546 * This should simplify the parametric integer programming.
7548 static __isl_give isl_set
*base_compute_divs(__isl_take isl_basic_set
*bset
)
7550 isl_morph
*morph1
, *morph2
;
7556 if (bset
->n_eq
== 0)
7557 return isl_basic_set_lexmin_compute_divs(bset
);
7559 morph1
= isl_basic_set_parameter_compression(bset
);
7560 bset
= isl_morph_basic_set(isl_morph_copy(morph1
), bset
);
7561 bset
= isl_basic_set_lift(bset
);
7562 morph2
= isl_basic_set_variable_compression(bset
, isl_dim_set
);
7563 bset
= isl_morph_basic_set(morph2
, bset
);
7564 n
= isl_basic_set_dim(bset
, isl_dim_set
);
7566 bset
= isl_basic_set_free(bset
);
7567 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
7569 set
= isl_basic_set_lexmin_compute_divs(bset
);
7571 set
= isl_morph_set(isl_morph_inverse(morph1
), set
);
7576 /* Project the given basic set onto its parameter domain, possibly introducing
7577 * new, explicit, existential variables in the constraints.
7578 * The input has parameters and (possibly implicit) existential variables.
7579 * The output has the same parameters, but only
7580 * explicit existentially quantified variables.
7582 * The actual projection is performed by pip, but pip doesn't seem
7583 * to like equalities very much, so we first remove the equalities
7584 * among the parameters by performing a variable compression on
7585 * the parameters. Afterward, an inverse transformation is performed
7586 * and the equalities among the parameters are inserted back in.
7588 * The variable compression on the parameters may uncover additional
7589 * equalities that were only implicit before. We therefore check
7590 * if there are any new parameter equalities in the result and
7591 * if so recurse. The removal of parameter equalities is required
7592 * for the parameter compression performed by base_compute_divs.
7594 static struct isl_set
*parameter_compute_divs(struct isl_basic_set
*bset
)
7598 struct isl_mat
*T
, *T2
;
7599 struct isl_set
*set
;
7602 bset
= isl_basic_set_cow(bset
);
7606 if (bset
->n_eq
== 0)
7607 return base_compute_divs(bset
);
7609 bset
= isl_basic_set_gauss(bset
, NULL
);
7612 if (isl_basic_set_plain_is_empty(bset
))
7613 return isl_set_from_basic_set(bset
);
7615 i
= first_parameter_equality(bset
);
7616 if (i
== bset
->n_eq
)
7617 return base_compute_divs(bset
);
7619 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
7621 return isl_set_from_basic_set(isl_basic_set_free(bset
));
7622 eq
= isl_mat_sub_alloc6(bset
->ctx
, bset
->eq
, i
, bset
->n_eq
- i
,
7624 eq
= isl_mat_cow(eq
);
7625 T
= isl_mat_variable_compression(isl_mat_copy(eq
), &T2
);
7626 if (T
&& T
->n_col
== 0) {
7630 bset
= isl_basic_set_set_to_empty(bset
);
7631 return isl_set_from_basic_set(bset
);
7633 bset
= basic_set_parameter_preimage(bset
, T
);
7635 i
= first_parameter_equality(bset
);
7638 else if (i
== bset
->n_eq
)
7639 set
= base_compute_divs(bset
);
7641 set
= parameter_compute_divs(bset
);
7642 set
= set_parameter_preimage(set
, T2
);
7643 set
= set_append_equalities(set
, eq
);
7647 /* Insert the divs from "ls" before those of "bmap".
7649 * The number of columns is not changed, which means that the last
7650 * dimensions of "bmap" are being reintepreted as the divs from "ls".
7651 * The caller is responsible for removing the same number of dimensions
7652 * from the space of "bmap".
7654 static __isl_give isl_basic_map
*insert_divs_from_local_space(
7655 __isl_take isl_basic_map
*bmap
, __isl_keep isl_local_space
*ls
)
7661 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
7663 return isl_basic_map_free(bmap
);
7667 old_n_div
= bmap
->n_div
;
7668 bmap
= insert_div_rows(bmap
, n_div
);
7672 for (i
= 0; i
< n_div
; ++i
) {
7673 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], ls
->div
->n_col
);
7674 isl_seq_clr(bmap
->div
[i
] + ls
->div
->n_col
, old_n_div
);
7680 /* Replace the space of "bmap" by the space and divs of "ls".
7682 * If "ls" has any divs, then we simplify the result since we may
7683 * have discovered some additional equalities that could simplify
7684 * the div expressions.
7686 static __isl_give isl_basic_map
*basic_replace_space_by_local_space(
7687 __isl_take isl_basic_map
*bmap
, __isl_take isl_local_space
*ls
)
7691 bmap
= isl_basic_map_cow(bmap
);
7692 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
7693 if (!bmap
|| n_div
< 0)
7696 bmap
= insert_divs_from_local_space(bmap
, ls
);
7700 isl_space_free(bmap
->dim
);
7701 bmap
->dim
= isl_local_space_get_space(ls
);
7705 isl_local_space_free(ls
);
7707 bmap
= isl_basic_map_simplify(bmap
);
7708 bmap
= isl_basic_map_finalize(bmap
);
7711 isl_basic_map_free(bmap
);
7712 isl_local_space_free(ls
);
7716 /* Replace the space of "map" by the space and divs of "ls".
7718 static __isl_give isl_map
*replace_space_by_local_space(__isl_take isl_map
*map
,
7719 __isl_take isl_local_space
*ls
)
7723 map
= isl_map_cow(map
);
7727 for (i
= 0; i
< map
->n
; ++i
) {
7728 map
->p
[i
] = basic_replace_space_by_local_space(map
->p
[i
],
7729 isl_local_space_copy(ls
));
7733 isl_space_free(isl_map_take_space(map
));
7734 map
= isl_map_restore_space(map
, isl_local_space_get_space(ls
));
7736 isl_local_space_free(ls
);
7739 isl_local_space_free(ls
);
7744 /* Compute an explicit representation for the existentially
7745 * quantified variables for which do not know any explicit representation yet.
7747 * We first sort the existentially quantified variables so that the
7748 * existentially quantified variables for which we already have an explicit
7749 * representation are placed before those for which we do not.
7750 * The input dimensions, the output dimensions and the existentially
7751 * quantified variables for which we already have an explicit
7752 * representation are then turned into parameters.
7753 * compute_divs returns a map with the same parameters and
7754 * no input or output dimensions and the dimension specification
7755 * is reset to that of the input, including the existentially quantified
7756 * variables for which we already had an explicit representation.
7758 static __isl_give isl_map
*compute_divs(__isl_take isl_basic_map
*bmap
)
7760 struct isl_basic_set
*bset
;
7761 struct isl_set
*set
;
7762 struct isl_map
*map
;
7764 isl_local_space
*ls
;
7771 bmap
= isl_basic_map_sort_divs(bmap
);
7772 bmap
= isl_basic_map_cow(bmap
);
7776 n_known
= isl_basic_map_first_unknown_div(bmap
);
7777 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
7778 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
7779 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
7780 if (n_known
< 0 || nparam
< 0 || n_in
< 0 || n_out
< 0)
7781 return isl_map_from_basic_map(isl_basic_map_free(bmap
));
7783 space
= isl_space_set_alloc(bmap
->ctx
,
7784 nparam
+ n_in
+ n_out
+ n_known
, 0);
7788 ls
= isl_basic_map_get_local_space(bmap
);
7789 ls
= isl_local_space_drop_dims(ls
, isl_dim_div
,
7790 n_known
, bmap
->n_div
- n_known
);
7792 for (i
= n_known
; i
< bmap
->n_div
; ++i
)
7793 swap_div(bmap
, i
- n_known
, i
);
7794 bmap
->n_div
-= n_known
;
7795 bmap
->extra
-= n_known
;
7797 bmap
= isl_basic_map_reset_space(bmap
, space
);
7798 bset
= bset_from_bmap(bmap
);
7800 set
= parameter_compute_divs(bset
);
7801 map
= set_to_map(set
);
7802 map
= replace_space_by_local_space(map
, ls
);
7806 isl_basic_map_free(bmap
);
7810 /* Remove the explicit representation of local variable "div",
7813 __isl_give isl_basic_map
*isl_basic_map_mark_div_unknown(
7814 __isl_take isl_basic_map
*bmap
, int div
)
7818 unknown
= isl_basic_map_div_is_marked_unknown(bmap
, div
);
7820 return isl_basic_map_free(bmap
);
7824 bmap
= isl_basic_map_cow(bmap
);
7827 isl_int_set_si(bmap
->div
[div
][0], 0);
7831 /* Is local variable "div" of "bmap" marked as not having an explicit
7833 * Note that even if "div" is not marked in this way and therefore
7834 * has an explicit representation, this representation may still
7835 * depend (indirectly) on other local variables that do not
7836 * have an explicit representation.
7838 isl_bool
isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map
*bmap
,
7841 if (isl_basic_map_check_range(bmap
, isl_dim_div
, div
, 1) < 0)
7842 return isl_bool_error
;
7843 return isl_int_is_zero(bmap
->div
[div
][0]);
7846 /* Return the position of the first local variable that does not
7847 * have an explicit representation.
7848 * Return the total number of local variables if they all have
7849 * an explicit representation.
7850 * Return -1 on error.
7852 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map
*bmap
)
7859 for (i
= 0; i
< bmap
->n_div
; ++i
) {
7860 if (!isl_basic_map_div_is_known(bmap
, i
))
7866 /* Return the position of the first local variable that does not
7867 * have an explicit representation.
7868 * Return the total number of local variables if they all have
7869 * an explicit representation.
7870 * Return -1 on error.
7872 int isl_basic_set_first_unknown_div(__isl_keep isl_basic_set
*bset
)
7874 return isl_basic_map_first_unknown_div(bset
);
7877 /* Does "bmap" have an explicit representation for all local variables?
7879 isl_bool
isl_basic_map_divs_known(__isl_keep isl_basic_map
*bmap
)
7884 n
= isl_basic_map_dim(bmap
, isl_dim_div
);
7885 first
= isl_basic_map_first_unknown_div(bmap
);
7886 if (n
< 0 || first
< 0)
7887 return isl_bool_error
;
7891 /* Do all basic maps in "map" have an explicit representation
7892 * for all local variables?
7894 isl_bool
isl_map_divs_known(__isl_keep isl_map
*map
)
7899 return isl_bool_error
;
7901 for (i
= 0; i
< map
->n
; ++i
) {
7902 int known
= isl_basic_map_divs_known(map
->p
[i
]);
7907 return isl_bool_true
;
7910 /* If bmap contains any unknown divs, then compute explicit
7911 * expressions for them. However, this computation may be
7912 * quite expensive, so first try to remove divs that aren't
7915 __isl_give isl_map
*isl_basic_map_compute_divs(__isl_take isl_basic_map
*bmap
)
7918 struct isl_map
*map
;
7920 known
= isl_basic_map_divs_known(bmap
);
7924 return isl_map_from_basic_map(bmap
);
7926 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
7928 known
= isl_basic_map_divs_known(bmap
);
7932 return isl_map_from_basic_map(bmap
);
7934 map
= compute_divs(bmap
);
7937 isl_basic_map_free(bmap
);
7941 __isl_give isl_map
*isl_map_compute_divs(__isl_take isl_map
*map
)
7945 struct isl_map
*res
;
7952 known
= isl_map_divs_known(map
);
7960 res
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[0]));
7961 for (i
= 1 ; i
< map
->n
; ++i
) {
7963 r2
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[i
]));
7964 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
7965 res
= isl_map_union_disjoint(res
, r2
);
7967 res
= isl_map_union(res
, r2
);
7974 __isl_give isl_set
*isl_basic_set_compute_divs(__isl_take isl_basic_set
*bset
)
7976 return set_from_map(isl_basic_map_compute_divs(bset_to_bmap(bset
)));
7979 __isl_give isl_set
*isl_set_compute_divs(__isl_take isl_set
*set
)
7981 return set_from_map(isl_map_compute_divs(set_to_map(set
)));
7984 __isl_give isl_set
*isl_map_domain(__isl_take isl_map
*map
)
7987 struct isl_set
*set
;
7992 map
= isl_map_cow(map
);
7996 set
= set_from_map(map
);
7997 set
->dim
= isl_space_domain(set
->dim
);
8000 for (i
= 0; i
< map
->n
; ++i
) {
8001 set
->p
[i
] = isl_basic_map_domain(map
->p
[i
]);
8005 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
8006 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
8013 /* Return the union of "map1" and "map2", where we assume for now that
8014 * "map1" and "map2" are disjoint. Note that the basic maps inside
8015 * "map1" or "map2" may not be disjoint from each other.
8016 * Also note that this function is also called from isl_map_union,
8017 * which takes care of handling the situation where "map1" and "map2"
8018 * may not be disjoint.
8020 * If one of the inputs is empty, we can simply return the other input.
8021 * Similarly, if one of the inputs is universal, then it is equal to the union.
8023 static __isl_give isl_map
*map_union_disjoint(__isl_take isl_map
*map1
,
8024 __isl_take isl_map
*map2
)
8028 struct isl_map
*map
= NULL
;
8031 if (isl_map_check_equal_space(map1
, map2
) < 0)
8043 is_universe
= isl_map_plain_is_universe(map1
);
8044 if (is_universe
< 0)
8051 is_universe
= isl_map_plain_is_universe(map2
);
8052 if (is_universe
< 0)
8059 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
8060 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
8061 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
8063 map
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
8064 map1
->n
+ map2
->n
, flags
);
8067 for (i
= 0; i
< map1
->n
; ++i
) {
8068 map
= isl_map_add_basic_map(map
,
8069 isl_basic_map_copy(map1
->p
[i
]));
8073 for (i
= 0; i
< map2
->n
; ++i
) {
8074 map
= isl_map_add_basic_map(map
,
8075 isl_basic_map_copy(map2
->p
[i
]));
8089 /* Return the union of "map1" and "map2", where "map1" and "map2" are
8090 * guaranteed to be disjoint by the caller.
8092 * Note that this functions is called from within isl_map_make_disjoint,
8093 * so we have to be careful not to touch the constraints of the inputs
8096 __isl_give isl_map
*isl_map_union_disjoint(__isl_take isl_map
*map1
,
8097 __isl_take isl_map
*map2
)
8099 isl_map_align_params_bin(&map1
, &map2
);
8100 return map_union_disjoint(map1
, map2
);
8103 /* Return the union of "map1" and "map2", where "map1" and "map2" may
8106 * We currently simply call map_union_disjoint, the internal operation
8107 * of which does not really depend on the inputs being disjoint.
8108 * If the result contains more than one basic map, then we clear
8109 * the disjoint flag since the result may contain basic maps from
8110 * both inputs and these are not guaranteed to be disjoint.
8112 * As a special case, if "map1" and "map2" are obviously equal,
8113 * then we simply return "map1".
8115 __isl_give isl_map
*isl_map_union(__isl_take isl_map
*map1
,
8116 __isl_take isl_map
*map2
)
8120 if (isl_map_align_params_bin(&map1
, &map2
) < 0)
8123 equal
= isl_map_plain_is_equal(map1
, map2
);
8131 map1
= map_union_disjoint(map1
, map2
);
8135 ISL_F_CLR(map1
, ISL_MAP_DISJOINT
);
8143 __isl_give isl_set
*isl_set_union_disjoint(
8144 __isl_take isl_set
*set1
, __isl_take isl_set
*set2
)
8146 return set_from_map(isl_map_union_disjoint(set_to_map(set1
),
8150 __isl_give isl_set
*isl_set_union(__isl_take isl_set
*set1
,
8151 __isl_take isl_set
*set2
)
8153 return set_from_map(isl_map_union(set_to_map(set1
), set_to_map(set2
)));
8156 /* Apply "fn" to pairs of elements from "map" and "set" and collect
8157 * the results in a map living in "space".
8159 * "map" and "set" are assumed to be compatible and non-NULL.
8161 static __isl_give isl_map
*map_intersect_set(__isl_take isl_map
*map
,
8162 __isl_take isl_space
*space
, __isl_take isl_set
*set
,
8163 __isl_give isl_basic_map
*fn(__isl_take isl_basic_map
*bmap
,
8164 __isl_take isl_basic_set
*bset
))
8167 struct isl_map
*result
;
8170 if (isl_set_plain_is_universe(set
)) {
8172 return isl_map_reset_equal_dim_space(map
, space
);
8175 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
) &&
8176 ISL_F_ISSET(set
, ISL_MAP_DISJOINT
))
8177 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
8179 result
= isl_map_alloc_space(space
, map
->n
* set
->n
, flags
);
8180 for (i
= 0; result
&& i
< map
->n
; ++i
)
8181 for (j
= 0; j
< set
->n
; ++j
) {
8182 result
= isl_map_add_basic_map(result
,
8183 fn(isl_basic_map_copy(map
->p
[i
]),
8184 isl_basic_set_copy(set
->p
[j
])));
8194 __isl_give isl_map
*isl_map_intersect_range(__isl_take isl_map
*map
,
8195 __isl_take isl_set
*set
)
8200 isl_map_align_params_set(&map
, &set
);
8201 ok
= isl_map_compatible_range(map
, set
);
8205 isl_die(set
->ctx
, isl_error_invalid
,
8206 "incompatible spaces", goto error
);
8208 space
= isl_map_get_space(map
);
8209 return map_intersect_set(map
, space
, set
,
8210 &isl_basic_map_intersect_range
);
8217 /* Intersect the domain of "map" with "set".
8219 * If the domain dimensions of "map" do not have any identifiers,
8220 * then copy them over from "set".
8222 __isl_give isl_map
*isl_map_intersect_domain(__isl_take isl_map
*map
,
8223 __isl_take isl_set
*set
)
8228 isl_map_align_params_set(&map
, &set
);
8229 ok
= isl_map_compatible_domain(map
, set
);
8233 isl_die(set
->ctx
, isl_error_invalid
,
8234 "incompatible spaces", goto error
);
8236 space
= isl_map_get_space(map
);
8237 space
= isl_space_copy_ids_if_unset(space
, isl_dim_in
,
8238 isl_set_peek_space(set
), isl_dim_set
);
8239 return map_intersect_set(map
, space
, set
,
8240 &isl_basic_map_intersect_domain
);
8247 /* Data structure that specifies how isl_map_intersect_factor
8250 * "preserve_type" is the tuple where the factor differs from
8251 * the input map and of which the identifiers needs
8252 * to be preserved explicitly.
8253 * "other_factor" is used to extract the space of the other factor
8254 * from the space of the product ("map").
8255 * "product" is used to combine the given factor and a universe map
8256 * in the space returned by "other_factor" to produce a map
8257 * that lives in the same space as the input map.
8259 struct isl_intersect_factor_control
{
8260 enum isl_dim_type preserve_type
;
8261 __isl_give isl_space
*(*other_factor
)(__isl_take isl_space
*space
);
8262 __isl_give isl_map
*(*product
)(__isl_take isl_map
*factor
,
8263 __isl_take isl_map
*other
);
8266 /* Given a map "map" in some product space and a map "factor"
8267 * living in some factor space, return the intersection.
8269 * After aligning the parameters,
8270 * the map "factor" is first extended to a map living in the same space
8271 * as "map" and then a regular intersection is computed.
8273 * Note that the extension is computed as a product, which is anonymous
8274 * by default. If "map" has an identifier on the corresponding tuple,
8275 * then this identifier needs to be set on the product
8276 * before the intersection is computed.
8278 static __isl_give isl_map
*isl_map_intersect_factor(
8279 __isl_take isl_map
*map
, __isl_take isl_map
*factor
,
8280 struct isl_intersect_factor_control
*control
)
8282 isl_bool equal
, has_id
;
8285 isl_map
*other
, *product
;
8287 equal
= isl_map_has_equal_params(map
, factor
);
8291 map
= isl_map_align_params(map
, isl_map_get_space(factor
));
8292 factor
= isl_map_align_params(factor
, isl_map_get_space(map
));
8295 space
= isl_map_get_space(map
);
8296 has_id
= isl_space_has_tuple_id(space
, control
->preserve_type
);
8298 space
= isl_space_free(space
);
8300 id
= isl_space_get_tuple_id(space
, control
->preserve_type
);
8302 other
= isl_map_universe(control
->other_factor(space
));
8303 product
= control
->product(factor
, other
);
8305 if (has_id
>= 0 && has_id
)
8306 product
= isl_map_set_tuple_id(product
,
8307 control
->preserve_type
, id
);
8309 return map_intersect(map
, product
);
8312 isl_map_free(factor
);
8316 /* Return the domain product of "map2" and "map1".
8318 static __isl_give isl_map
*isl_map_reverse_domain_product(
8319 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
8321 return isl_map_domain_product(map2
, map1
);
8324 /* Return the range product of "map2" and "map1".
8326 static __isl_give isl_map
*isl_map_reverse_range_product(
8327 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
8329 return isl_map_range_product(map2
, map1
);
8332 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
8333 * in the space B -> C, return the intersection.
8335 __isl_give isl_map
*isl_map_intersect_domain_factor_range(
8336 __isl_take isl_map
*map
, __isl_take isl_map
*factor
)
8338 struct isl_intersect_factor_control control
= {
8339 .preserve_type
= isl_dim_in
,
8340 .other_factor
= isl_space_domain_factor_domain
,
8341 .product
= isl_map_reverse_domain_product
,
8344 return isl_map_intersect_factor(map
, factor
, &control
);
8347 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
8348 * in the space A -> B, return the intersection.
8350 __isl_give isl_map
*isl_map_intersect_range_factor_domain(
8351 __isl_take isl_map
*map
, __isl_take isl_map
*factor
)
8353 struct isl_intersect_factor_control control
= {
8354 .preserve_type
= isl_dim_out
,
8355 .other_factor
= isl_space_range_factor_range
,
8356 .product
= isl_map_range_product
,
8359 return isl_map_intersect_factor(map
, factor
, &control
);
8362 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
8363 * in the space A -> C, return the intersection.
8365 __isl_give isl_map
*isl_map_intersect_range_factor_range(
8366 __isl_take isl_map
*map
, __isl_take isl_map
*factor
)
8368 struct isl_intersect_factor_control control
= {
8369 .preserve_type
= isl_dim_out
,
8370 .other_factor
= isl_space_range_factor_domain
,
8371 .product
= isl_map_reverse_range_product
,
8374 return isl_map_intersect_factor(map
, factor
, &control
);
8377 /* Given a set "set" in a space [A -> B] and a set "domain"
8378 * in the space A, return the intersection.
8380 * The set "domain" is first extended to a set living in the space
8381 * [A -> B] and then a regular intersection is computed.
8383 __isl_give isl_set
*isl_set_intersect_factor_domain(__isl_take isl_set
*set
,
8384 __isl_take isl_set
*domain
)
8386 struct isl_intersect_factor_control control
= {
8387 .preserve_type
= isl_dim_set
,
8388 .other_factor
= isl_space_factor_range
,
8389 .product
= isl_map_range_product
,
8392 return set_from_map(isl_map_intersect_factor(set_to_map(set
),
8393 set_to_map(domain
), &control
));
8396 /* Given a set "set" in a space [A -> B] and a set "range"
8397 * in the space B, return the intersection.
8399 * The set "range" is first extended to a set living in the space
8400 * [A -> B] and then a regular intersection is computed.
8402 __isl_give isl_set
*isl_set_intersect_factor_range(__isl_take isl_set
*set
,
8403 __isl_take isl_set
*range
)
8405 struct isl_intersect_factor_control control
= {
8406 .preserve_type
= isl_dim_set
,
8407 .other_factor
= isl_space_factor_domain
,
8408 .product
= isl_map_reverse_range_product
,
8411 return set_from_map(isl_map_intersect_factor(set_to_map(set
),
8412 set_to_map(range
), &control
));
8415 __isl_give isl_map
*isl_map_apply_domain(__isl_take isl_map
*map1
,
8416 __isl_take isl_map
*map2
)
8418 if (isl_map_align_params_bin(&map1
, &map2
) < 0)
8420 map1
= isl_map_reverse(map1
);
8421 map1
= isl_map_apply_range(map1
, map2
);
8422 return isl_map_reverse(map1
);
8429 __isl_give isl_map
*isl_map_apply_range(__isl_take isl_map
*map1
,
8430 __isl_take isl_map
*map2
)
8433 struct isl_map
*result
;
8436 if (isl_map_align_params_bin(&map1
, &map2
) < 0)
8439 space
= isl_space_join(isl_space_copy(map1
->dim
),
8440 isl_space_copy(map2
->dim
));
8442 result
= isl_map_alloc_space(space
, map1
->n
* map2
->n
, 0);
8445 for (i
= 0; i
< map1
->n
; ++i
)
8446 for (j
= 0; j
< map2
->n
; ++j
) {
8447 result
= isl_map_add_basic_map(result
,
8448 isl_basic_map_apply_range(
8449 isl_basic_map_copy(map1
->p
[i
]),
8450 isl_basic_map_copy(map2
->p
[j
])));
8456 if (result
&& result
->n
<= 1)
8457 ISL_F_SET(result
, ISL_MAP_DISJOINT
);
8466 * returns range - domain
8468 __isl_give isl_basic_set
*isl_basic_map_deltas(__isl_take isl_basic_map
*bmap
)
8470 isl_space
*target_space
;
8471 struct isl_basic_set
*bset
;
8479 isl_assert(bmap
->ctx
, isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
8480 bmap
->dim
, isl_dim_out
),
8482 dim
= isl_basic_map_dim(bmap
, isl_dim_in
);
8483 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
8484 if (dim
< 0 || nparam
< 0)
8486 target_space
= isl_space_domain(isl_basic_map_get_space(bmap
));
8487 bmap
= isl_basic_map_from_range(isl_basic_map_wrap(bmap
));
8488 bmap
= isl_basic_map_add_dims(bmap
, isl_dim_in
, dim
);
8489 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
8491 bmap
= isl_basic_map_free(bmap
);
8492 bmap
= isl_basic_map_extend_constraints(bmap
, dim
, 0);
8493 for (i
= 0; i
< dim
; ++i
) {
8494 int j
= isl_basic_map_alloc_equality(bmap
);
8496 bmap
= isl_basic_map_free(bmap
);
8499 isl_seq_clr(bmap
->eq
[j
], 1 + total
);
8500 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
8501 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], 1);
8502 isl_int_set_si(bmap
->eq
[j
][1+nparam
+2*dim
+i
], -1);
8504 bset
= isl_basic_map_domain(bmap
);
8505 bset
= isl_basic_set_reset_space(bset
, target_space
);
8508 isl_basic_map_free(bmap
);
8512 /* Check that domain and range of "map" are the same.
8514 isl_stat
isl_map_check_equal_tuples(__isl_keep isl_map
*map
)
8519 space
= isl_map_peek_space(map
);
8520 equal
= isl_space_tuple_is_equal(space
, isl_dim_in
, space
, isl_dim_out
);
8522 return isl_stat_error
;
8524 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
8525 "domain and range don't match", return isl_stat_error
);
8530 * returns range - domain
8532 __isl_give isl_set
*isl_map_deltas(__isl_take isl_map
*map
)
8536 struct isl_set
*result
;
8541 isl_assert(map
->ctx
, isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
8542 map
->dim
, isl_dim_out
),
8544 space
= isl_map_get_space(map
);
8545 space
= isl_space_domain(space
);
8546 result
= isl_set_alloc_space(space
, map
->n
, 0);
8549 for (i
= 0; i
< map
->n
; ++i
)
8550 result
= isl_set_add_basic_set(result
,
8551 isl_basic_map_deltas(isl_basic_map_copy(map
->p
[i
])));
8560 * returns [domain -> range] -> range - domain
8562 __isl_give isl_basic_map
*isl_basic_map_deltas_map(
8563 __isl_take isl_basic_map
*bmap
)
8567 isl_basic_map
*domain
;
8571 if (!isl_space_tuple_is_equal(bmap
->dim
, isl_dim_in
,
8572 bmap
->dim
, isl_dim_out
))
8573 isl_die(bmap
->ctx
, isl_error_invalid
,
8574 "domain and range don't match", goto error
);
8576 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
8577 n
= isl_basic_map_dim(bmap
, isl_dim_in
);
8578 if (nparam
< 0 || n
< 0)
8579 return isl_basic_map_free(bmap
);
8581 space
= isl_basic_map_get_space(bmap
);
8582 space
= isl_space_from_range(isl_space_domain(space
));
8583 domain
= isl_basic_map_universe(space
);
8585 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
8586 bmap
= isl_basic_map_apply_range(bmap
, domain
);
8587 bmap
= isl_basic_map_extend_constraints(bmap
, n
, 0);
8589 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
8591 return isl_basic_map_free(bmap
);
8593 for (i
= 0; i
< n
; ++i
) {
8594 k
= isl_basic_map_alloc_equality(bmap
);
8597 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
8598 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], 1);
8599 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ i
], -1);
8600 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ n
+ i
], 1);
8603 bmap
= isl_basic_map_gauss(bmap
, NULL
);
8604 return isl_basic_map_finalize(bmap
);
8606 isl_basic_map_free(bmap
);
8611 * returns [domain -> range] -> range - domain
8613 __isl_give isl_map
*isl_map_deltas_map(__isl_take isl_map
*map
)
8615 if (isl_map_check_equal_tuples(map
) < 0)
8616 return isl_map_free(map
);
8618 return isl_map_transform(map
, &isl_space_range_map
,
8619 &isl_basic_map_deltas_map
);
8622 __isl_give isl_basic_map
*isl_basic_map_identity(__isl_take isl_space
*space
)
8624 isl_size n_in
, n_out
;
8626 n_in
= isl_space_dim(space
, isl_dim_in
);
8627 n_out
= isl_space_dim(space
, isl_dim_out
);
8628 if (n_in
< 0 || n_out
< 0)
8631 isl_die(space
->ctx
, isl_error_invalid
,
8632 "number of input and output dimensions needs to be "
8633 "the same", goto error
);
8634 return isl_basic_map_equal(space
, n_in
);
8636 isl_space_free(space
);
8640 __isl_give isl_map
*isl_map_identity(__isl_take isl_space
*space
)
8642 return isl_map_from_basic_map(isl_basic_map_identity(space
));
8645 __isl_give isl_map
*isl_set_identity(__isl_take isl_set
*set
)
8647 isl_space
*space
= isl_set_get_space(set
);
8649 id
= isl_map_identity(isl_space_map_from_set(space
));
8650 return isl_map_intersect_range(id
, set
);
8653 /* Construct a basic set with all set dimensions having only non-negative
8656 __isl_give isl_basic_set
*isl_basic_set_positive_orthant(
8657 __isl_take isl_space
*space
)
8663 struct isl_basic_set
*bset
;
8665 nparam
= isl_space_dim(space
, isl_dim_param
);
8666 dim
= isl_space_dim(space
, isl_dim_set
);
8667 total
= isl_space_dim(space
, isl_dim_all
);
8668 if (nparam
< 0 || dim
< 0 || total
< 0)
8669 space
= isl_space_free(space
);
8670 bset
= isl_basic_set_alloc_space(space
, 0, 0, dim
);
8673 for (i
= 0; i
< dim
; ++i
) {
8674 int k
= isl_basic_set_alloc_inequality(bset
);
8677 isl_seq_clr(bset
->ineq
[k
], 1 + total
);
8678 isl_int_set_si(bset
->ineq
[k
][1 + nparam
+ i
], 1);
8682 isl_basic_set_free(bset
);
8686 /* Construct the half-space x_pos >= 0.
8688 static __isl_give isl_basic_set
*nonneg_halfspace(__isl_take isl_space
*space
,
8693 isl_basic_set
*nonneg
;
8695 total
= isl_space_dim(space
, isl_dim_all
);
8697 space
= isl_space_free(space
);
8698 nonneg
= isl_basic_set_alloc_space(space
, 0, 0, 1);
8699 k
= isl_basic_set_alloc_inequality(nonneg
);
8702 isl_seq_clr(nonneg
->ineq
[k
], 1 + total
);
8703 isl_int_set_si(nonneg
->ineq
[k
][pos
], 1);
8705 return isl_basic_set_finalize(nonneg
);
8707 isl_basic_set_free(nonneg
);
8711 /* Construct the half-space x_pos <= -1.
8713 static __isl_give isl_basic_set
*neg_halfspace(__isl_take isl_space
*space
,
8720 total
= isl_space_dim(space
, isl_dim_all
);
8722 space
= isl_space_free(space
);
8723 neg
= isl_basic_set_alloc_space(space
, 0, 0, 1);
8724 k
= isl_basic_set_alloc_inequality(neg
);
8727 isl_seq_clr(neg
->ineq
[k
], 1 + total
);
8728 isl_int_set_si(neg
->ineq
[k
][0], -1);
8729 isl_int_set_si(neg
->ineq
[k
][pos
], -1);
8731 return isl_basic_set_finalize(neg
);
8733 isl_basic_set_free(neg
);
8737 __isl_give isl_set
*isl_set_split_dims(__isl_take isl_set
*set
,
8738 enum isl_dim_type type
, unsigned first
, unsigned n
)
8742 isl_basic_set
*nonneg
;
8748 if (isl_set_check_range(set
, type
, first
, n
) < 0)
8749 return isl_set_free(set
);
8751 offset
= pos(set
->dim
, type
);
8752 for (i
= 0; i
< n
; ++i
) {
8753 nonneg
= nonneg_halfspace(isl_set_get_space(set
),
8754 offset
+ first
+ i
);
8755 neg
= neg_halfspace(isl_set_get_space(set
), offset
+ first
+ i
);
8757 set
= isl_set_intersect(set
, isl_basic_set_union(nonneg
, neg
));
8763 static isl_stat
foreach_orthant(__isl_take isl_set
*set
, int *signs
, int first
,
8765 isl_stat (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
8771 return isl_stat_error
;
8772 if (isl_set_plain_is_empty(set
)) {
8777 return fn(set
, signs
, user
);
8780 half
= isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set
),
8782 half
= isl_set_intersect(half
, isl_set_copy(set
));
8783 if (foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
) < 0)
8787 half
= isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set
),
8789 half
= isl_set_intersect(half
, set
);
8790 return foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
);
8793 return isl_stat_error
;
8796 /* Call "fn" on the intersections of "set" with each of the orthants
8797 * (except for obviously empty intersections). The orthant is identified
8798 * by the signs array, with each entry having value 1 or -1 according
8799 * to the sign of the corresponding variable.
8801 isl_stat
isl_set_foreach_orthant(__isl_keep isl_set
*set
,
8802 isl_stat (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
8811 return isl_stat_error
;
8812 if (isl_set_plain_is_empty(set
))
8815 nparam
= isl_set_dim(set
, isl_dim_param
);
8816 nvar
= isl_set_dim(set
, isl_dim_set
);
8817 if (nparam
< 0 || nvar
< 0)
8818 return isl_stat_error
;
8820 signs
= isl_alloc_array(set
->ctx
, int, nparam
+ nvar
);
8822 r
= foreach_orthant(isl_set_copy(set
), signs
, 0, nparam
+ nvar
,
8830 isl_bool
isl_set_is_equal(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
8832 return isl_map_is_equal(set_to_map(set1
), set_to_map(set2
));
8835 isl_bool
isl_basic_map_is_subset(__isl_keep isl_basic_map
*bmap1
,
8836 __isl_keep isl_basic_map
*bmap2
)
8839 struct isl_map
*map1
;
8840 struct isl_map
*map2
;
8842 if (!bmap1
|| !bmap2
)
8843 return isl_bool_error
;
8845 map1
= isl_map_from_basic_map(isl_basic_map_copy(bmap1
));
8846 map2
= isl_map_from_basic_map(isl_basic_map_copy(bmap2
));
8848 is_subset
= isl_map_is_subset(map1
, map2
);
8856 isl_bool
isl_basic_set_is_subset(__isl_keep isl_basic_set
*bset1
,
8857 __isl_keep isl_basic_set
*bset2
)
8859 return isl_basic_map_is_subset(bset1
, bset2
);
8862 isl_bool
isl_basic_map_is_equal(__isl_keep isl_basic_map
*bmap1
,
8863 __isl_keep isl_basic_map
*bmap2
)
8867 if (!bmap1
|| !bmap2
)
8868 return isl_bool_error
;
8869 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
8870 if (is_subset
!= isl_bool_true
)
8872 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
8876 isl_bool
isl_basic_set_is_equal(__isl_keep isl_basic_set
*bset1
,
8877 __isl_keep isl_basic_set
*bset2
)
8879 return isl_basic_map_is_equal(
8880 bset_to_bmap(bset1
), bset_to_bmap(bset2
));
8883 isl_bool
isl_map_is_empty(__isl_keep isl_map
*map
)
8889 return isl_bool_error
;
8890 for (i
= 0; i
< map
->n
; ++i
) {
8891 is_empty
= isl_basic_map_is_empty(map
->p
[i
]);
8893 return isl_bool_error
;
8895 return isl_bool_false
;
8897 return isl_bool_true
;
8900 isl_bool
isl_map_plain_is_empty(__isl_keep isl_map
*map
)
8902 return map
? map
->n
== 0 : isl_bool_error
;
8905 isl_bool
isl_set_plain_is_empty(__isl_keep isl_set
*set
)
8907 return set
? set
->n
== 0 : isl_bool_error
;
8910 isl_bool
isl_set_is_empty(__isl_keep isl_set
*set
)
8912 return isl_map_is_empty(set_to_map(set
));
8916 #define TYPE isl_basic_map
8919 #include "isl_type_has_equal_space_bin_templ.c"
8920 #include "isl_type_check_equal_space_templ.c"
8922 /* Check that "bset1" and "bset2" live in the same space,
8923 * reporting an error if they do not.
8925 isl_stat
isl_basic_set_check_equal_space(__isl_keep isl_basic_set
*bset1
,
8926 __isl_keep isl_basic_set
*bset2
)
8928 return isl_basic_map_check_equal_space(bset_to_bmap(bset1
),
8929 bset_to_bmap(bset1
));
8933 #define TYPE isl_map
8935 #include "isl_type_has_equal_space_bin_templ.c"
8936 #include "isl_type_check_equal_space_templ.c"
8938 isl_bool
isl_set_has_equal_space(__isl_keep isl_set
*set1
,
8939 __isl_keep isl_set
*set2
)
8941 return isl_map_has_equal_space(set_to_map(set1
), set_to_map(set2
));
8945 #define TYPE1 isl_map
8947 #define TYPE2 isl_basic_map
8949 #define TYPE_PAIR isl_map_basic_map
8952 #include "isl_type_has_equal_space_templ.c"
8953 #include "isl_type_check_equal_space_templ.c"
8955 /* Check that "set" and "bset" live in the same space,
8956 * reporting an error if they do not.
8958 isl_stat
isl_set_basic_set_check_equal_space(__isl_keep isl_set
*set
,
8959 __isl_keep isl_basic_set
*bset
)
8961 return isl_map_basic_map_check_equal_space(set_to_map(set
),
8962 bset_to_bmap(bset
));
8965 static isl_bool
map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
8970 return isl_bool_error
;
8971 is_subset
= isl_map_is_subset(map1
, map2
);
8972 if (is_subset
!= isl_bool_true
)
8974 is_subset
= isl_map_is_subset(map2
, map1
);
8978 /* Is "map1" equal to "map2"?
8980 * First check if they are obviously equal.
8981 * If not, then perform a more detailed analysis.
8983 isl_bool
isl_map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
8987 equal
= isl_map_plain_is_equal(map1
, map2
);
8988 if (equal
< 0 || equal
)
8990 return isl_map_align_params_map_map_and_test(map1
, map2
, &map_is_equal
);
8993 isl_bool
isl_basic_map_is_strict_subset(__isl_keep isl_basic_map
*bmap1
,
8994 __isl_keep isl_basic_map
*bmap2
)
8998 if (!bmap1
|| !bmap2
)
8999 return isl_bool_error
;
9000 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
9001 if (is_subset
!= isl_bool_true
)
9003 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
9004 return isl_bool_not(is_subset
);
9007 isl_bool
isl_map_is_strict_subset(__isl_keep isl_map
*map1
,
9008 __isl_keep isl_map
*map2
)
9013 return isl_bool_error
;
9014 is_subset
= isl_map_is_subset(map1
, map2
);
9015 if (is_subset
!= isl_bool_true
)
9017 is_subset
= isl_map_is_subset(map2
, map1
);
9018 return isl_bool_not(is_subset
);
9021 isl_bool
isl_set_is_strict_subset(__isl_keep isl_set
*set1
,
9022 __isl_keep isl_set
*set2
)
9024 return isl_map_is_strict_subset(set_to_map(set1
), set_to_map(set2
));
9027 /* Is "bmap" obviously equal to the universe with the same space?
9029 * That is, does it not have any constraints?
9031 isl_bool
isl_basic_map_plain_is_universe(__isl_keep isl_basic_map
*bmap
)
9034 return isl_bool_error
;
9035 return bmap
->n_eq
== 0 && bmap
->n_ineq
== 0;
9038 /* Is "bset" obviously equal to the universe with the same space?
9040 isl_bool
isl_basic_set_plain_is_universe(__isl_keep isl_basic_set
*bset
)
9042 return isl_basic_map_plain_is_universe(bset
);
9045 /* If "c" does not involve any existentially quantified variables,
9046 * then set *univ to false and abort
9048 static isl_stat
involves_divs(__isl_take isl_constraint
*c
, void *user
)
9050 isl_bool
*univ
= user
;
9053 n
= isl_constraint_dim(c
, isl_dim_div
);
9055 c
= isl_constraint_free(c
);
9056 *univ
= isl_constraint_involves_dims(c
, isl_dim_div
, 0, n
);
9057 isl_constraint_free(c
);
9058 if (*univ
< 0 || !*univ
)
9059 return isl_stat_error
;
9063 /* Is "bmap" equal to the universe with the same space?
9065 * First check if it is obviously equal to the universe.
9066 * If not and if there are any constraints not involving
9067 * existentially quantified variables, then it is certainly
9068 * not equal to the universe.
9069 * Otherwise, check if the universe is a subset of "bmap".
9071 isl_bool
isl_basic_map_is_universe(__isl_keep isl_basic_map
*bmap
)
9075 isl_basic_map
*test
;
9077 univ
= isl_basic_map_plain_is_universe(bmap
);
9078 if (univ
< 0 || univ
)
9080 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
9082 return isl_bool_error
;
9084 return isl_bool_false
;
9085 univ
= isl_bool_true
;
9086 if (isl_basic_map_foreach_constraint(bmap
, &involves_divs
, &univ
) < 0 &&
9088 return isl_bool_error
;
9089 if (univ
< 0 || !univ
)
9091 test
= isl_basic_map_universe(isl_basic_map_get_space(bmap
));
9092 univ
= isl_basic_map_is_subset(test
, bmap
);
9093 isl_basic_map_free(test
);
9097 /* Is "bset" equal to the universe with the same space?
9099 isl_bool
isl_basic_set_is_universe(__isl_keep isl_basic_set
*bset
)
9101 return isl_basic_map_is_universe(bset
);
9104 isl_bool
isl_map_plain_is_universe(__isl_keep isl_map
*map
)
9109 return isl_bool_error
;
9111 for (i
= 0; i
< map
->n
; ++i
) {
9112 isl_bool r
= isl_basic_map_plain_is_universe(map
->p
[i
]);
9117 return isl_bool_false
;
9120 isl_bool
isl_set_plain_is_universe(__isl_keep isl_set
*set
)
9122 return isl_map_plain_is_universe(set_to_map(set
));
9125 isl_bool
isl_basic_map_is_empty(__isl_keep isl_basic_map
*bmap
)
9127 struct isl_basic_set
*bset
= NULL
;
9128 struct isl_vec
*sample
= NULL
;
9129 isl_bool empty
, non_empty
;
9132 return isl_bool_error
;
9134 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
9135 return isl_bool_true
;
9137 if (isl_basic_map_plain_is_universe(bmap
))
9138 return isl_bool_false
;
9140 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
9141 struct isl_basic_map
*copy
= isl_basic_map_copy(bmap
);
9142 copy
= isl_basic_map_remove_redundancies(copy
);
9143 empty
= isl_basic_map_plain_is_empty(copy
);
9144 isl_basic_map_free(copy
);
9148 non_empty
= isl_basic_map_plain_is_non_empty(bmap
);
9150 return isl_bool_error
;
9152 return isl_bool_false
;
9153 isl_vec_free(bmap
->sample
);
9154 bmap
->sample
= NULL
;
9155 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
9157 return isl_bool_error
;
9158 sample
= isl_basic_set_sample_vec(bset
);
9160 return isl_bool_error
;
9161 empty
= sample
->size
== 0;
9162 isl_vec_free(bmap
->sample
);
9163 bmap
->sample
= sample
;
9165 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
9170 isl_bool
isl_basic_map_plain_is_empty(__isl_keep isl_basic_map
*bmap
)
9173 return isl_bool_error
;
9174 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
);
9177 isl_bool
isl_basic_set_plain_is_empty(__isl_keep isl_basic_set
*bset
)
9180 return isl_bool_error
;
9181 return ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
);
9184 /* Is "bmap" known to be non-empty?
9186 * That is, is the cached sample still valid?
9188 isl_bool
isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map
*bmap
)
9193 return isl_bool_error
;
9195 return isl_bool_false
;
9196 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
9198 return isl_bool_error
;
9199 if (bmap
->sample
->size
!= 1 + total
)
9200 return isl_bool_false
;
9201 return isl_basic_map_contains(bmap
, bmap
->sample
);
9204 isl_bool
isl_basic_set_is_empty(__isl_keep isl_basic_set
*bset
)
9206 return isl_basic_map_is_empty(bset_to_bmap(bset
));
9209 __isl_give isl_map
*isl_basic_map_union(__isl_take isl_basic_map
*bmap1
,
9210 __isl_take isl_basic_map
*bmap2
)
9212 struct isl_map
*map
;
9214 if (isl_basic_map_check_equal_space(bmap1
, bmap2
) < 0)
9217 map
= isl_map_alloc_space(isl_space_copy(bmap1
->dim
), 2, 0);
9220 map
= isl_map_add_basic_map(map
, bmap1
);
9221 map
= isl_map_add_basic_map(map
, bmap2
);
9224 isl_basic_map_free(bmap1
);
9225 isl_basic_map_free(bmap2
);
9229 __isl_give isl_set
*isl_basic_set_union(__isl_take isl_basic_set
*bset1
,
9230 __isl_take isl_basic_set
*bset2
)
9232 return set_from_map(isl_basic_map_union(bset_to_bmap(bset1
),
9233 bset_to_bmap(bset2
)));
9236 /* Order divs such that any div only depends on previous divs */
9237 __isl_give isl_basic_map
*isl_basic_map_order_divs(
9238 __isl_take isl_basic_map
*bmap
)
9243 off
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
9245 return isl_basic_map_free(bmap
);
9247 for (i
= 0; i
< bmap
->n_div
; ++i
) {
9249 if (isl_int_is_zero(bmap
->div
[i
][0]))
9251 pos
= isl_seq_first_non_zero(bmap
->div
[i
]+1+1+off
+i
,
9256 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_internal
,
9257 "integer division depends on itself",
9258 return isl_basic_map_free(bmap
));
9259 bmap
= isl_basic_map_swap_div(bmap
, i
, i
+ pos
);
9267 __isl_give isl_map
*isl_map_order_divs(__isl_take isl_map
*map
)
9274 for (i
= 0; i
< map
->n
; ++i
) {
9275 map
->p
[i
] = isl_basic_map_order_divs(map
->p
[i
]);
9286 /* Sort the local variables of "bset".
9288 __isl_give isl_basic_set
*isl_basic_set_sort_divs(
9289 __isl_take isl_basic_set
*bset
)
9291 return bset_from_bmap(isl_basic_map_sort_divs(bset_to_bmap(bset
)));
9294 /* Apply the expansion computed by isl_merge_divs.
9295 * The expansion itself is given by "exp" while the resulting
9296 * list of divs is given by "div".
9298 * Move the integer divisions of "bmap" into the right position
9299 * according to "exp" and then introduce the additional integer
9300 * divisions, adding div constraints.
9301 * The moving should be done first to avoid moving coefficients
9302 * in the definitions of the extra integer divisions.
9304 __isl_give isl_basic_map
*isl_basic_map_expand_divs(
9305 __isl_take isl_basic_map
*bmap
, __isl_take isl_mat
*div
, int *exp
)
9310 bmap
= isl_basic_map_cow(bmap
);
9314 if (div
->n_row
< bmap
->n_div
)
9315 isl_die(isl_mat_get_ctx(div
), isl_error_invalid
,
9316 "not an expansion", goto error
);
9318 n_div
= bmap
->n_div
;
9319 bmap
= isl_basic_map_extend(bmap
, div
->n_row
- n_div
, 0,
9320 2 * (div
->n_row
- n_div
));
9322 for (i
= n_div
; i
< div
->n_row
; ++i
)
9323 if (isl_basic_map_alloc_div(bmap
) < 0)
9326 for (j
= n_div
- 1; j
>= 0; --j
) {
9329 bmap
= isl_basic_map_swap_div(bmap
, j
, exp
[j
]);
9334 for (i
= 0; i
< div
->n_row
; ++i
) {
9335 if (j
< n_div
&& exp
[j
] == i
) {
9338 isl_seq_cpy(bmap
->div
[i
], div
->row
[i
], div
->n_col
);
9339 if (isl_basic_map_div_is_marked_unknown(bmap
, i
))
9341 bmap
= isl_basic_map_add_div_constraints(bmap
, i
);
9350 isl_basic_map_free(bmap
);
9355 /* Apply the expansion computed by isl_merge_divs.
9356 * The expansion itself is given by "exp" while the resulting
9357 * list of divs is given by "div".
9359 __isl_give isl_basic_set
*isl_basic_set_expand_divs(
9360 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*div
, int *exp
)
9362 return isl_basic_map_expand_divs(bset
, div
, exp
);
9365 /* Look for a div in dst that corresponds to the div "div" in src.
9366 * The divs before "div" in src and dst are assumed to be the same.
9368 * Return the position of the corresponding div in dst
9369 * if there is one. Otherwise, return a position beyond the integer divisions.
9370 * Return -1 on error.
9372 static int find_div(__isl_keep isl_basic_map
*dst
,
9373 __isl_keep isl_basic_map
*src
, unsigned div
)
9379 v_div
= isl_basic_map_var_offset(src
, isl_dim_div
);
9380 n_div
= isl_basic_map_dim(dst
, isl_dim_div
);
9381 if (n_div
< 0 || v_div
< 0)
9383 isl_assert(dst
->ctx
, div
<= n_div
, return -1);
9384 for (i
= div
; i
< n_div
; ++i
)
9385 if (isl_seq_eq(dst
->div
[i
], src
->div
[div
], 1+1+v_div
+div
) &&
9386 isl_seq_first_non_zero(dst
->div
[i
] + 1 + 1 + v_div
+ div
,
9392 /* Align the divs of "dst" to those of "src", adding divs from "src"
9393 * if needed. That is, make sure that the first src->n_div divs
9394 * of the result are equal to those of src.
9396 * The result is not finalized as by design it will have redundant
9397 * divs if any divs from "src" were copied.
9399 __isl_give isl_basic_map
*isl_basic_map_align_divs(
9400 __isl_take isl_basic_map
*dst
, __isl_keep isl_basic_map
*src
)
9409 return isl_basic_map_free(dst
);
9411 if (src
->n_div
== 0)
9414 known
= isl_basic_map_divs_known(src
);
9416 return isl_basic_map_free(dst
);
9418 isl_die(isl_basic_map_get_ctx(src
), isl_error_invalid
,
9419 "some src divs are unknown",
9420 return isl_basic_map_free(dst
));
9422 v_div
= isl_basic_map_var_offset(src
, isl_dim_div
);
9424 return isl_basic_map_free(dst
);
9426 src
= isl_basic_map_order_divs(isl_basic_map_copy(src
));
9428 return isl_basic_map_free(dst
);
9431 dst_n_div
= isl_basic_map_dim(dst
, isl_dim_div
);
9433 dst
= isl_basic_map_free(dst
);
9434 for (i
= 0; i
< src
->n_div
; ++i
) {
9435 int j
= find_div(dst
, src
, i
);
9437 dst
= isl_basic_map_free(dst
);
9438 if (j
== dst_n_div
) {
9440 int extra
= src
->n_div
- i
;
9441 dst
= isl_basic_map_cow(dst
);
9444 dst
= isl_basic_map_extend(dst
,
9445 extra
, 0, 2 * extra
);
9448 j
= isl_basic_map_alloc_div(dst
);
9451 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+v_div
+i
);
9452 isl_seq_clr(dst
->div
[j
]+1+1+v_div
+i
, dst
->n_div
- i
);
9454 dst
= isl_basic_map_add_div_constraints(dst
, j
);
9459 dst
= isl_basic_map_swap_div(dst
, i
, j
);
9463 isl_basic_map_free(src
);
9466 isl_basic_map_free(src
);
9467 isl_basic_map_free(dst
);
9471 __isl_give isl_map
*isl_map_align_divs_internal(__isl_take isl_map
*map
)
9479 map
= isl_map_compute_divs(map
);
9480 map
= isl_map_cow(map
);
9484 for (i
= 1; i
< map
->n
; ++i
)
9485 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], map
->p
[i
]);
9486 for (i
= 1; i
< map
->n
; ++i
) {
9487 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], map
->p
[0]);
9489 return isl_map_free(map
);
9492 map
= isl_map_unmark_normalized(map
);
9496 __isl_give isl_map
*isl_map_align_divs(__isl_take isl_map
*map
)
9498 return isl_map_align_divs_internal(map
);
9501 __isl_give isl_set
*isl_set_align_divs(__isl_take isl_set
*set
)
9503 return set_from_map(isl_map_align_divs_internal(set_to_map(set
)));
9506 /* Align the divs of the basic maps in "map" to those
9507 * of the basic maps in "list", as well as to the other basic maps in "map".
9508 * The elements in "list" are assumed to have known divs.
9510 __isl_give isl_map
*isl_map_align_divs_to_basic_map_list(
9511 __isl_take isl_map
*map
, __isl_keep isl_basic_map_list
*list
)
9516 n
= isl_basic_map_list_n_basic_map(list
);
9517 map
= isl_map_compute_divs(map
);
9518 map
= isl_map_cow(map
);
9520 return isl_map_free(map
);
9524 for (i
= 0; i
< n
; ++i
) {
9525 isl_basic_map
*bmap
;
9527 bmap
= isl_basic_map_list_get_basic_map(list
, i
);
9528 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], bmap
);
9529 isl_basic_map_free(bmap
);
9532 return isl_map_free(map
);
9534 return isl_map_align_divs_internal(map
);
9537 /* Align the divs of each element of "list" to those of "bmap".
9538 * Both "bmap" and the elements of "list" are assumed to have known divs.
9540 __isl_give isl_basic_map_list
*isl_basic_map_list_align_divs_to_basic_map(
9541 __isl_take isl_basic_map_list
*list
, __isl_keep isl_basic_map
*bmap
)
9546 n
= isl_basic_map_list_n_basic_map(list
);
9548 return isl_basic_map_list_free(list
);
9550 for (i
= 0; i
< n
; ++i
) {
9551 isl_basic_map
*bmap_i
;
9553 bmap_i
= isl_basic_map_list_get_basic_map(list
, i
);
9554 bmap_i
= isl_basic_map_align_divs(bmap_i
, bmap
);
9555 list
= isl_basic_map_list_set_basic_map(list
, i
, bmap_i
);
9561 __isl_give isl_set
*isl_set_apply( __isl_take isl_set
*set
,
9562 __isl_take isl_map
*map
)
9566 isl_map_align_params_set(&map
, &set
);
9567 ok
= isl_map_compatible_domain(map
, set
);
9571 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
9572 "incompatible spaces", goto error
);
9573 map
= isl_map_intersect_domain(map
, set
);
9574 set
= isl_map_range(map
);
9582 /* There is no need to cow as removing empty parts doesn't change
9583 * the meaning of the set.
9585 __isl_give isl_map
*isl_map_remove_empty_parts(__isl_take isl_map
*map
)
9592 for (i
= map
->n
- 1; i
>= 0; --i
)
9593 map
= remove_if_empty(map
, i
);
9598 __isl_give isl_set
*isl_set_remove_empty_parts(__isl_take isl_set
*set
)
9600 return set_from_map(isl_map_remove_empty_parts(set_to_map(set
)));
9603 /* Create a binary relation that maps the shared initial "pos" dimensions
9604 * of "bset1" and "bset2" to the remaining dimensions of "bset1" and "bset2".
9606 static __isl_give isl_basic_map
*join_initial(__isl_keep isl_basic_set
*bset1
,
9607 __isl_keep isl_basic_set
*bset2
, int pos
)
9609 isl_basic_map
*bmap1
;
9610 isl_basic_map
*bmap2
;
9612 bmap1
= isl_basic_map_from_range(isl_basic_set_copy(bset1
));
9613 bmap2
= isl_basic_map_from_range(isl_basic_set_copy(bset2
));
9614 bmap1
= isl_basic_map_move_dims(bmap1
, isl_dim_in
, 0,
9615 isl_dim_out
, 0, pos
);
9616 bmap2
= isl_basic_map_move_dims(bmap2
, isl_dim_in
, 0,
9617 isl_dim_out
, 0, pos
);
9618 return isl_basic_map_range_product(bmap1
, bmap2
);
9621 /* Given two basic sets bset1 and bset2, compute the maximal difference
9622 * between the values of dimension pos in bset1 and those in bset2
9623 * for any common value of the parameters and dimensions preceding pos.
9625 static enum isl_lp_result
basic_set_maximal_difference_at(
9626 __isl_keep isl_basic_set
*bset1
, __isl_keep isl_basic_set
*bset2
,
9627 int pos
, isl_int
*opt
)
9629 isl_basic_map
*bmap1
;
9630 struct isl_ctx
*ctx
;
9631 struct isl_vec
*obj
;
9635 enum isl_lp_result res
;
9637 nparam
= isl_basic_set_dim(bset1
, isl_dim_param
);
9638 dim1
= isl_basic_set_dim(bset1
, isl_dim_set
);
9639 if (nparam
< 0 || dim1
< 0 || !bset2
)
9640 return isl_lp_error
;
9642 bmap1
= join_initial(bset1
, bset2
, pos
);
9643 total
= isl_basic_map_dim(bmap1
, isl_dim_all
);
9645 return isl_lp_error
;
9648 obj
= isl_vec_alloc(ctx
, 1 + total
);
9651 isl_seq_clr(obj
->block
.data
, 1 + total
);
9652 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
], 1);
9653 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
+(dim1
-pos
)], -1);
9654 res
= isl_basic_map_solve_lp(bmap1
, 1, obj
->block
.data
, ctx
->one
,
9656 isl_basic_map_free(bmap1
);
9660 isl_basic_map_free(bmap1
);
9661 return isl_lp_error
;
9664 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
9665 * for any common value of the parameters and dimensions preceding pos
9666 * in both basic sets, the values of dimension pos in bset1 are
9667 * smaller or larger than those in bset2.
9670 * 1 if bset1 follows bset2
9671 * -1 if bset1 precedes bset2
9672 * 0 if bset1 and bset2 are incomparable
9673 * -2 if some error occurred.
9675 int isl_basic_set_compare_at(__isl_keep isl_basic_set
*bset1
,
9676 __isl_keep isl_basic_set
*bset2
, int pos
)
9679 enum isl_lp_result res
;
9684 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
9686 if (res
== isl_lp_empty
)
9688 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
9689 res
== isl_lp_unbounded
)
9691 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
9700 /* Given two basic sets bset1 and bset2, check whether
9701 * for any common value of the parameters and dimensions preceding pos
9702 * there is a value of dimension pos in bset1 that is larger
9703 * than a value of the same dimension in bset2.
9706 * 1 if there exists such a pair
9707 * 0 if there is no such pair, but there is a pair of equal values
9709 * -2 if some error occurred.
9711 int isl_basic_set_follows_at(__isl_keep isl_basic_set
*bset1
,
9712 __isl_keep isl_basic_set
*bset2
, int pos
)
9715 isl_basic_map
*bmap
;
9718 dim1
= isl_basic_set_dim(bset1
, isl_dim_set
);
9721 bmap
= join_initial(bset1
, bset2
, pos
);
9722 bmap
= isl_basic_map_order_ge(bmap
, isl_dim_out
, 0,
9723 isl_dim_out
, dim1
- pos
);
9724 empty
= isl_basic_map_is_empty(bmap
);
9728 isl_basic_map_free(bmap
);
9731 bmap
= isl_basic_map_order_gt(bmap
, isl_dim_out
, 0,
9732 isl_dim_out
, dim1
- pos
);
9733 empty
= isl_basic_map_is_empty(bmap
);
9736 isl_basic_map_free(bmap
);
9741 isl_basic_map_free(bmap
);
9745 /* Given two sets set1 and set2, check whether
9746 * for any common value of the parameters and dimensions preceding pos
9747 * there is a value of dimension pos in set1 that is larger
9748 * than a value of the same dimension in set2.
9751 * 1 if there exists such a pair
9752 * 0 if there is no such pair, but there is a pair of equal values
9754 * -2 if some error occurred.
9756 int isl_set_follows_at(__isl_keep isl_set
*set1
,
9757 __isl_keep isl_set
*set2
, int pos
)
9765 for (i
= 0; i
< set1
->n
; ++i
)
9766 for (j
= 0; j
< set2
->n
; ++j
) {
9768 f
= isl_basic_set_follows_at(set1
->p
[i
], set2
->p
[j
], pos
);
9769 if (f
== 1 || f
== -2)
9778 static isl_bool
isl_basic_map_plain_has_fixed_var(
9779 __isl_keep isl_basic_map
*bmap
, unsigned pos
, isl_int
*val
)
9785 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
9787 return isl_bool_error
;
9788 for (i
= 0, d
= total
-1; i
< bmap
->n_eq
&& d
+1 > pos
; ++i
) {
9789 for (; d
+1 > pos
; --d
)
9790 if (!isl_int_is_zero(bmap
->eq
[i
][1+d
]))
9794 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1, d
) != -1)
9795 return isl_bool_false
;
9796 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1+d
+1, total
-d
-1) != -1)
9797 return isl_bool_false
;
9798 if (!isl_int_is_one(bmap
->eq
[i
][1+d
]))
9799 return isl_bool_false
;
9801 isl_int_neg(*val
, bmap
->eq
[i
][0]);
9802 return isl_bool_true
;
9804 return isl_bool_false
;
9807 static isl_bool
isl_map_plain_has_fixed_var(__isl_keep isl_map
*map
,
9808 unsigned pos
, isl_int
*val
)
9816 return isl_bool_error
;
9818 return isl_bool_false
;
9820 return isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, val
);
9823 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, &v
);
9824 for (i
= 1; fixed
== isl_bool_true
&& i
< map
->n
; ++i
) {
9825 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[i
], pos
, &tmp
);
9826 if (fixed
== isl_bool_true
&& isl_int_ne(tmp
, v
))
9827 fixed
= isl_bool_false
;
9830 isl_int_set(*val
, v
);
9836 static isl_bool
isl_basic_set_plain_has_fixed_var(
9837 __isl_keep isl_basic_set
*bset
, unsigned pos
, isl_int
*val
)
9839 return isl_basic_map_plain_has_fixed_var(bset_to_bmap(bset
),
9843 isl_bool
isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map
*bmap
,
9844 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
9846 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
9847 return isl_bool_error
;
9848 return isl_basic_map_plain_has_fixed_var(bmap
,
9849 isl_basic_map_offset(bmap
, type
) - 1 + pos
, val
);
9852 /* If "bmap" obviously lies on a hyperplane where the given dimension
9853 * has a fixed value, then return that value.
9854 * Otherwise return NaN.
9856 __isl_give isl_val
*isl_basic_map_plain_get_val_if_fixed(
9857 __isl_keep isl_basic_map
*bmap
,
9858 enum isl_dim_type type
, unsigned pos
)
9866 ctx
= isl_basic_map_get_ctx(bmap
);
9867 v
= isl_val_alloc(ctx
);
9870 fixed
= isl_basic_map_plain_is_fixed(bmap
, type
, pos
, &v
->n
);
9872 return isl_val_free(v
);
9874 isl_int_set_si(v
->d
, 1);
9878 return isl_val_nan(ctx
);
9881 isl_bool
isl_map_plain_is_fixed(__isl_keep isl_map
*map
,
9882 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
9884 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
9885 return isl_bool_error
;
9886 return isl_map_plain_has_fixed_var(map
,
9887 map_offset(map
, type
) - 1 + pos
, val
);
9890 /* If "map" obviously lies on a hyperplane where the given dimension
9891 * has a fixed value, then return that value.
9892 * Otherwise return NaN.
9894 __isl_give isl_val
*isl_map_plain_get_val_if_fixed(__isl_keep isl_map
*map
,
9895 enum isl_dim_type type
, unsigned pos
)
9903 ctx
= isl_map_get_ctx(map
);
9904 v
= isl_val_alloc(ctx
);
9907 fixed
= isl_map_plain_is_fixed(map
, type
, pos
, &v
->n
);
9909 return isl_val_free(v
);
9911 isl_int_set_si(v
->d
, 1);
9915 return isl_val_nan(ctx
);
9918 /* If "set" obviously lies on a hyperplane where the given dimension
9919 * has a fixed value, then return that value.
9920 * Otherwise return NaN.
9922 __isl_give isl_val
*isl_set_plain_get_val_if_fixed(__isl_keep isl_set
*set
,
9923 enum isl_dim_type type
, unsigned pos
)
9925 return isl_map_plain_get_val_if_fixed(set
, type
, pos
);
9928 /* Return a sequence of values in the same space as "set"
9929 * that are equal to the corresponding set dimensions of "set"
9930 * for those set dimensions that obviously lie on a hyperplane
9931 * where the dimension has a fixed value.
9932 * The other elements are set to NaN.
9934 __isl_give isl_multi_val
*isl_set_get_plain_multi_val_if_fixed(
9935 __isl_keep isl_set
*set
)
9942 space
= isl_space_drop_all_params(isl_set_get_space(set
));
9943 mv
= isl_multi_val_alloc(space
);
9944 n
= isl_multi_val_size(mv
);
9946 return isl_multi_val_free(mv
);
9948 for (i
= 0; i
< n
; ++i
) {
9951 v
= isl_set_plain_get_val_if_fixed(set
, isl_dim_set
, i
);
9952 mv
= isl_multi_val_set_val(mv
, i
, v
);
9958 /* Check if dimension dim has fixed value and if so and if val is not NULL,
9959 * then return this fixed value in *val.
9961 isl_bool
isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set
*bset
,
9962 unsigned dim
, isl_int
*val
)
9966 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
9968 return isl_bool_error
;
9969 return isl_basic_set_plain_has_fixed_var(bset
, nparam
+ dim
, val
);
9972 /* Return -1 if the constraint "c1" should be sorted before "c2"
9973 * and 1 if it should be sorted after "c2".
9974 * Return 0 if the two constraints are the same (up to the constant term).
9976 * In particular, if a constraint involves later variables than another
9977 * then it is sorted after this other constraint.
9978 * uset_gist depends on constraints without existentially quantified
9979 * variables sorting first.
9981 * For constraints that have the same latest variable, those
9982 * with the same coefficient for this latest variable (first in absolute value
9983 * and then in actual value) are grouped together.
9984 * This is useful for detecting pairs of constraints that can
9985 * be chained in their printed representation.
9987 * Finally, within a group, constraints are sorted according to
9988 * their coefficients (excluding the constant term).
9990 static int sort_constraint_cmp(const void *p1
, const void *p2
, void *arg
)
9992 isl_int
**c1
= (isl_int
**) p1
;
9993 isl_int
**c2
= (isl_int
**) p2
;
9995 unsigned size
= *(unsigned *) arg
;
9998 l1
= isl_seq_last_non_zero(*c1
+ 1, size
);
9999 l2
= isl_seq_last_non_zero(*c2
+ 1, size
);
10004 cmp
= isl_int_abs_cmp((*c1
)[1 + l1
], (*c2
)[1 + l1
]);
10007 cmp
= isl_int_cmp((*c1
)[1 + l1
], (*c2
)[1 + l1
]);
10011 return isl_seq_cmp(*c1
+ 1, *c2
+ 1, size
);
10014 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
10015 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
10016 * and 0 if the two constraints are the same (up to the constant term).
10018 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map
*bmap
,
10019 isl_int
*c1
, isl_int
*c2
)
10024 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
10028 return sort_constraint_cmp(&c1
, &c2
, &size
);
10031 __isl_give isl_basic_map
*isl_basic_map_sort_constraints(
10032 __isl_take isl_basic_map
*bmap
)
10039 if (bmap
->n_ineq
== 0)
10041 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_SORTED
))
10043 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
10045 return isl_basic_map_free(bmap
);
10047 if (isl_sort(bmap
->ineq
, bmap
->n_ineq
, sizeof(isl_int
*),
10048 &sort_constraint_cmp
, &size
) < 0)
10049 return isl_basic_map_free(bmap
);
10050 ISL_F_SET(bmap
, ISL_BASIC_MAP_SORTED
);
10054 __isl_give isl_basic_set
*isl_basic_set_sort_constraints(
10055 __isl_take isl_basic_set
*bset
)
10057 isl_basic_map
*bmap
= bset_to_bmap(bset
);
10058 return bset_from_bmap(isl_basic_map_sort_constraints(bmap
));
10061 __isl_give isl_basic_map
*isl_basic_map_normalize(
10062 __isl_take isl_basic_map
*bmap
)
10064 bmap
= isl_basic_map_remove_redundancies(bmap
);
10065 bmap
= isl_basic_map_sort_constraints(bmap
);
10068 int isl_basic_map_plain_cmp(__isl_keep isl_basic_map
*bmap1
,
10069 __isl_keep isl_basic_map
*bmap2
)
10073 isl_space
*space1
, *space2
;
10075 if (!bmap1
|| !bmap2
)
10078 if (bmap1
== bmap2
)
10080 space1
= isl_basic_map_peek_space(bmap1
);
10081 space2
= isl_basic_map_peek_space(bmap2
);
10082 cmp
= isl_space_cmp(space1
, space2
);
10085 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) !=
10086 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_RATIONAL
))
10087 return ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) ? -1 : 1;
10088 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
) &&
10089 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
10091 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
))
10093 if (ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
10095 if (bmap1
->n_eq
!= bmap2
->n_eq
)
10096 return bmap1
->n_eq
- bmap2
->n_eq
;
10097 if (bmap1
->n_ineq
!= bmap2
->n_ineq
)
10098 return bmap1
->n_ineq
- bmap2
->n_ineq
;
10099 if (bmap1
->n_div
!= bmap2
->n_div
)
10100 return bmap1
->n_div
- bmap2
->n_div
;
10101 total
= isl_basic_map_dim(bmap1
, isl_dim_all
);
10104 for (i
= 0; i
< bmap1
->n_eq
; ++i
) {
10105 cmp
= isl_seq_cmp(bmap1
->eq
[i
], bmap2
->eq
[i
], 1+total
);
10109 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
10110 cmp
= isl_seq_cmp(bmap1
->ineq
[i
], bmap2
->ineq
[i
], 1+total
);
10114 for (i
= 0; i
< bmap1
->n_div
; ++i
) {
10115 cmp
= isl_seq_cmp(bmap1
->div
[i
], bmap2
->div
[i
], 1+1+total
);
10122 int isl_basic_set_plain_cmp(__isl_keep isl_basic_set
*bset1
,
10123 __isl_keep isl_basic_set
*bset2
)
10125 return isl_basic_map_plain_cmp(bset1
, bset2
);
10128 int isl_set_plain_cmp(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
10134 if (set1
->n
!= set2
->n
)
10135 return set1
->n
- set2
->n
;
10137 for (i
= 0; i
< set1
->n
; ++i
) {
10138 cmp
= isl_basic_set_plain_cmp(set1
->p
[i
], set2
->p
[i
]);
10146 isl_bool
isl_basic_map_plain_is_equal(__isl_keep isl_basic_map
*bmap1
,
10147 __isl_keep isl_basic_map
*bmap2
)
10149 if (!bmap1
|| !bmap2
)
10150 return isl_bool_error
;
10151 return isl_basic_map_plain_cmp(bmap1
, bmap2
) == 0;
10154 isl_bool
isl_basic_set_plain_is_equal(__isl_keep isl_basic_set
*bset1
,
10155 __isl_keep isl_basic_set
*bset2
)
10157 return isl_basic_map_plain_is_equal(bset_to_bmap(bset1
),
10158 bset_to_bmap(bset2
));
10161 static int qsort_bmap_cmp(const void *p1
, const void *p2
)
10163 isl_basic_map
*bmap1
= *(isl_basic_map
**) p1
;
10164 isl_basic_map
*bmap2
= *(isl_basic_map
**) p2
;
10166 return isl_basic_map_plain_cmp(bmap1
, bmap2
);
10169 /* Sort the basic maps of "map" and remove duplicate basic maps.
10171 * While removing basic maps, we make sure that the basic maps remain
10172 * sorted because isl_map_normalize expects the basic maps of the result
10175 static __isl_give isl_map
*sort_and_remove_duplicates(__isl_take isl_map
*map
)
10179 map
= isl_map_remove_empty_parts(map
);
10182 qsort(map
->p
, map
->n
, sizeof(struct isl_basic_map
*), qsort_bmap_cmp
);
10183 for (i
= map
->n
- 1; i
>= 1; --i
) {
10184 if (!isl_basic_map_plain_is_equal(map
->p
[i
- 1], map
->p
[i
]))
10186 isl_basic_map_free(map
->p
[i
-1]);
10187 for (j
= i
; j
< map
->n
; ++j
)
10188 map
->p
[j
- 1] = map
->p
[j
];
10195 /* Remove obvious duplicates among the basic maps of "map".
10197 * Unlike isl_map_normalize, this function does not remove redundant
10198 * constraints and only removes duplicates that have exactly the same
10199 * constraints in the input. It does sort the constraints and
10200 * the basic maps to ease the detection of duplicates.
10202 * If "map" has already been normalized or if the basic maps are
10203 * disjoint, then there can be no duplicates.
10205 __isl_give isl_map
*isl_map_remove_obvious_duplicates(__isl_take isl_map
*map
)
10208 isl_basic_map
*bmap
;
10214 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
| ISL_MAP_DISJOINT
))
10216 for (i
= 0; i
< map
->n
; ++i
) {
10217 bmap
= isl_basic_map_copy(map
->p
[i
]);
10218 bmap
= isl_basic_map_sort_constraints(bmap
);
10220 return isl_map_free(map
);
10221 isl_basic_map_free(map
->p
[i
]);
10225 map
= sort_and_remove_duplicates(map
);
10229 /* We normalize in place, but if anything goes wrong we need
10230 * to return NULL, so we need to make sure we don't change the
10231 * meaning of any possible other copies of map.
10233 __isl_give isl_map
*isl_map_normalize(__isl_take isl_map
*map
)
10236 struct isl_basic_map
*bmap
;
10240 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
))
10242 for (i
= 0; i
< map
->n
; ++i
) {
10243 bmap
= isl_basic_map_normalize(isl_basic_map_copy(map
->p
[i
]));
10246 isl_basic_map_free(map
->p
[i
]);
10250 map
= sort_and_remove_duplicates(map
);
10252 ISL_F_SET(map
, ISL_MAP_NORMALIZED
);
10259 __isl_give isl_set
*isl_set_normalize(__isl_take isl_set
*set
)
10261 return set_from_map(isl_map_normalize(set_to_map(set
)));
10264 isl_bool
isl_map_plain_is_equal(__isl_keep isl_map
*map1
,
10265 __isl_keep isl_map
*map2
)
10270 if (!map1
|| !map2
)
10271 return isl_bool_error
;
10274 return isl_bool_true
;
10275 equal
= isl_map_has_equal_space(map1
, map2
);
10276 if (equal
< 0 || !equal
)
10279 map1
= isl_map_copy(map1
);
10280 map2
= isl_map_copy(map2
);
10281 map1
= isl_map_normalize(map1
);
10282 map2
= isl_map_normalize(map2
);
10283 if (!map1
|| !map2
)
10285 equal
= map1
->n
== map2
->n
;
10286 for (i
= 0; equal
&& i
< map1
->n
; ++i
) {
10287 equal
= isl_basic_map_plain_is_equal(map1
->p
[i
], map2
->p
[i
]);
10291 isl_map_free(map1
);
10292 isl_map_free(map2
);
10295 isl_map_free(map1
);
10296 isl_map_free(map2
);
10297 return isl_bool_error
;
10300 isl_bool
isl_set_plain_is_equal(__isl_keep isl_set
*set1
,
10301 __isl_keep isl_set
*set2
)
10303 return isl_map_plain_is_equal(set_to_map(set1
), set_to_map(set2
));
10306 /* Return the basic maps in "map" as a list.
10308 __isl_give isl_basic_map_list
*isl_map_get_basic_map_list(
10309 __isl_keep isl_map
*map
)
10313 isl_basic_map_list
*list
;
10317 ctx
= isl_map_get_ctx(map
);
10318 list
= isl_basic_map_list_alloc(ctx
, map
->n
);
10320 for (i
= 0; i
< map
->n
; ++i
) {
10321 isl_basic_map
*bmap
;
10323 bmap
= isl_basic_map_copy(map
->p
[i
]);
10324 list
= isl_basic_map_list_add(list
, bmap
);
10330 /* Return the intersection of the elements in the non-empty list "list".
10331 * All elements are assumed to live in the same space.
10333 __isl_give isl_basic_map
*isl_basic_map_list_intersect(
10334 __isl_take isl_basic_map_list
*list
)
10338 isl_basic_map
*bmap
;
10340 n
= isl_basic_map_list_n_basic_map(list
);
10344 isl_die(isl_basic_map_list_get_ctx(list
), isl_error_invalid
,
10345 "expecting non-empty list", goto error
);
10347 bmap
= isl_basic_map_list_get_basic_map(list
, 0);
10348 for (i
= 1; i
< n
; ++i
) {
10349 isl_basic_map
*bmap_i
;
10351 bmap_i
= isl_basic_map_list_get_basic_map(list
, i
);
10352 bmap
= isl_basic_map_intersect(bmap
, bmap_i
);
10355 isl_basic_map_list_free(list
);
10358 isl_basic_map_list_free(list
);
10362 /* Return the intersection of the elements in the non-empty list "list".
10363 * All elements are assumed to live in the same space.
10365 __isl_give isl_basic_set
*isl_basic_set_list_intersect(
10366 __isl_take isl_basic_set_list
*list
)
10368 return isl_basic_map_list_intersect(list
);
10371 /* Return the union of the elements of "list".
10372 * The list is required to have at least one element.
10374 __isl_give isl_set
*isl_basic_set_list_union(
10375 __isl_take isl_basic_set_list
*list
)
10380 isl_basic_set
*bset
;
10383 n
= isl_basic_set_list_n_basic_set(list
);
10387 isl_die(isl_basic_set_list_get_ctx(list
), isl_error_invalid
,
10388 "expecting non-empty list", goto error
);
10390 bset
= isl_basic_set_list_get_basic_set(list
, 0);
10391 space
= isl_basic_set_get_space(bset
);
10392 isl_basic_set_free(bset
);
10394 set
= isl_set_alloc_space(space
, n
, 0);
10395 for (i
= 0; i
< n
; ++i
) {
10396 bset
= isl_basic_set_list_get_basic_set(list
, i
);
10397 set
= isl_set_add_basic_set(set
, bset
);
10400 isl_basic_set_list_free(list
);
10403 isl_basic_set_list_free(list
);
10407 /* Return the union of the elements in the non-empty list "list".
10408 * All elements are assumed to live in the same space.
10410 __isl_give isl_set
*isl_set_list_union(__isl_take isl_set_list
*list
)
10416 n
= isl_set_list_n_set(list
);
10420 isl_die(isl_set_list_get_ctx(list
), isl_error_invalid
,
10421 "expecting non-empty list", goto error
);
10423 set
= isl_set_list_get_set(list
, 0);
10424 for (i
= 1; i
< n
; ++i
) {
10427 set_i
= isl_set_list_get_set(list
, i
);
10428 set
= isl_set_union(set
, set_i
);
10431 isl_set_list_free(list
);
10434 isl_set_list_free(list
);
10438 __isl_give isl_basic_map
*isl_basic_map_product(
10439 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
10441 isl_space
*space_result
= NULL
;
10442 struct isl_basic_map
*bmap
;
10443 unsigned in1
, in2
, out1
, out2
, nparam
, total
, pos
;
10444 struct isl_dim_map
*dim_map1
, *dim_map2
;
10446 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
10448 space_result
= isl_space_product(isl_space_copy(bmap1
->dim
),
10449 isl_space_copy(bmap2
->dim
));
10451 in1
= isl_basic_map_dim(bmap1
, isl_dim_in
);
10452 in2
= isl_basic_map_dim(bmap2
, isl_dim_in
);
10453 out1
= isl_basic_map_dim(bmap1
, isl_dim_out
);
10454 out2
= isl_basic_map_dim(bmap2
, isl_dim_out
);
10455 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
10457 total
= nparam
+ in1
+ in2
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
10458 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
10459 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
10460 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
10461 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
10462 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
10463 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
10464 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
10465 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
10466 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
10467 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
10469 bmap
= isl_basic_map_alloc_space(space_result
,
10470 bmap1
->n_div
+ bmap2
->n_div
,
10471 bmap1
->n_eq
+ bmap2
->n_eq
,
10472 bmap1
->n_ineq
+ bmap2
->n_ineq
);
10473 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
10474 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
10475 bmap
= isl_basic_map_simplify(bmap
);
10476 return isl_basic_map_finalize(bmap
);
10478 isl_basic_map_free(bmap1
);
10479 isl_basic_map_free(bmap2
);
10483 __isl_give isl_basic_map
*isl_basic_map_flat_product(
10484 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
10486 isl_basic_map
*prod
;
10488 prod
= isl_basic_map_product(bmap1
, bmap2
);
10489 prod
= isl_basic_map_flatten(prod
);
10493 __isl_give isl_basic_set
*isl_basic_set_flat_product(
10494 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
10496 return isl_basic_map_flat_range_product(bset1
, bset2
);
10499 __isl_give isl_basic_map
*isl_basic_map_domain_product(
10500 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
10502 isl_space
*space1
, *space2
;
10503 isl_space
*space_result
= NULL
;
10504 isl_basic_map
*bmap
;
10505 isl_size in1
, in2
, out
, nparam
;
10506 unsigned total
, pos
;
10507 struct isl_dim_map
*dim_map1
, *dim_map2
;
10509 in1
= isl_basic_map_dim(bmap1
, isl_dim_in
);
10510 in2
= isl_basic_map_dim(bmap2
, isl_dim_in
);
10511 out
= isl_basic_map_dim(bmap1
, isl_dim_out
);
10512 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
10513 if (in1
< 0 || in2
< 0 || out
< 0 || nparam
< 0)
10516 space1
= isl_basic_map_get_space(bmap1
);
10517 space2
= isl_basic_map_get_space(bmap2
);
10518 space_result
= isl_space_domain_product(space1
, space2
);
10520 total
= nparam
+ in1
+ in2
+ out
+ bmap1
->n_div
+ bmap2
->n_div
;
10521 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
10522 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
10523 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
10524 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
10525 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
10526 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
10527 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
10528 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
);
10529 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out
);
10530 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
10532 bmap
= isl_basic_map_alloc_space(space_result
,
10533 bmap1
->n_div
+ bmap2
->n_div
,
10534 bmap1
->n_eq
+ bmap2
->n_eq
,
10535 bmap1
->n_ineq
+ bmap2
->n_ineq
);
10536 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
10537 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
10538 bmap
= isl_basic_map_simplify(bmap
);
10539 return isl_basic_map_finalize(bmap
);
10541 isl_basic_map_free(bmap1
);
10542 isl_basic_map_free(bmap2
);
10546 __isl_give isl_basic_map
*isl_basic_map_range_product(
10547 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
10550 isl_space
*space_result
= NULL
;
10551 isl_basic_map
*bmap
;
10552 isl_size in
, out1
, out2
, nparam
;
10553 unsigned total
, pos
;
10554 struct isl_dim_map
*dim_map1
, *dim_map2
;
10556 rational
= isl_basic_map_is_rational(bmap1
);
10557 if (rational
>= 0 && rational
)
10558 rational
= isl_basic_map_is_rational(bmap2
);
10559 in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
10560 out1
= isl_basic_map_dim(bmap1
, isl_dim_out
);
10561 out2
= isl_basic_map_dim(bmap2
, isl_dim_out
);
10562 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
10563 if (in
< 0 || out1
< 0 || out2
< 0 || nparam
< 0 || rational
< 0)
10566 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
10569 space_result
= isl_space_range_product(isl_space_copy(bmap1
->dim
),
10570 isl_space_copy(bmap2
->dim
));
10572 total
= nparam
+ in
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
10573 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
10574 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
10575 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
10576 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
10577 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
10578 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
10579 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in
);
10580 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
10581 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
10582 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
10584 bmap
= isl_basic_map_alloc_space(space_result
,
10585 bmap1
->n_div
+ bmap2
->n_div
,
10586 bmap1
->n_eq
+ bmap2
->n_eq
,
10587 bmap1
->n_ineq
+ bmap2
->n_ineq
);
10588 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
10589 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
10591 bmap
= isl_basic_map_set_rational(bmap
);
10592 bmap
= isl_basic_map_simplify(bmap
);
10593 return isl_basic_map_finalize(bmap
);
10595 isl_basic_map_free(bmap1
);
10596 isl_basic_map_free(bmap2
);
10600 __isl_give isl_basic_map
*isl_basic_map_flat_range_product(
10601 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
10603 isl_basic_map
*prod
;
10605 prod
= isl_basic_map_range_product(bmap1
, bmap2
);
10606 prod
= isl_basic_map_flatten_range(prod
);
10610 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
10611 * and collect the results.
10612 * The result live in the space obtained by calling "space_product"
10613 * on the spaces of "map1" and "map2".
10614 * If "remove_duplicates" is set then the result may contain duplicates
10615 * (even if the inputs do not) and so we try and remove the obvious
10618 static __isl_give isl_map
*map_product(__isl_take isl_map
*map1
,
10619 __isl_take isl_map
*map2
,
10620 __isl_give isl_space
*(*space_product
)(__isl_take isl_space
*left
,
10621 __isl_take isl_space
*right
),
10622 __isl_give isl_basic_map
*(*basic_map_product
)(
10623 __isl_take isl_basic_map
*left
,
10624 __isl_take isl_basic_map
*right
),
10625 int remove_duplicates
)
10627 unsigned flags
= 0;
10628 struct isl_map
*result
;
10632 m
= isl_map_has_equal_params(map1
, map2
);
10636 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
10637 "parameters don't match", goto error
);
10639 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
10640 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
10641 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
10643 result
= isl_map_alloc_space(space_product(isl_space_copy(map1
->dim
),
10644 isl_space_copy(map2
->dim
)),
10645 map1
->n
* map2
->n
, flags
);
10648 for (i
= 0; i
< map1
->n
; ++i
)
10649 for (j
= 0; j
< map2
->n
; ++j
) {
10650 struct isl_basic_map
*part
;
10651 part
= basic_map_product(isl_basic_map_copy(map1
->p
[i
]),
10652 isl_basic_map_copy(map2
->p
[j
]));
10653 if (isl_basic_map_is_empty(part
))
10654 isl_basic_map_free(part
);
10656 result
= isl_map_add_basic_map(result
, part
);
10660 if (remove_duplicates
)
10661 result
= isl_map_remove_obvious_duplicates(result
);
10662 isl_map_free(map1
);
10663 isl_map_free(map2
);
10666 isl_map_free(map1
);
10667 isl_map_free(map2
);
10671 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
10673 __isl_give isl_map
*isl_map_product(__isl_take isl_map
*map1
,
10674 __isl_take isl_map
*map2
)
10676 isl_map_align_params_bin(&map1
, &map2
);
10677 return map_product(map1
, map2
, &isl_space_product
,
10678 &isl_basic_map_product
, 0);
10681 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
10683 __isl_give isl_map
*isl_map_flat_product(__isl_take isl_map
*map1
,
10684 __isl_take isl_map
*map2
)
10688 prod
= isl_map_product(map1
, map2
);
10689 prod
= isl_map_flatten(prod
);
10693 /* Given two set A and B, construct its Cartesian product A x B.
10695 __isl_give isl_set
*isl_set_product(__isl_take isl_set
*set1
,
10696 __isl_take isl_set
*set2
)
10698 return isl_map_range_product(set1
, set2
);
10701 __isl_give isl_set
*isl_set_flat_product(__isl_take isl_set
*set1
,
10702 __isl_take isl_set
*set2
)
10704 return isl_map_flat_range_product(set1
, set2
);
10707 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
10709 __isl_give isl_map
*isl_map_domain_product(__isl_take isl_map
*map1
,
10710 __isl_take isl_map
*map2
)
10712 isl_map_align_params_bin(&map1
, &map2
);
10713 return map_product(map1
, map2
, &isl_space_domain_product
,
10714 &isl_basic_map_domain_product
, 1);
10717 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
10719 __isl_give isl_map
*isl_map_range_product(__isl_take isl_map
*map1
,
10720 __isl_take isl_map
*map2
)
10722 isl_map_align_params_bin(&map1
, &map2
);
10723 return map_product(map1
, map2
, &isl_space_range_product
,
10724 &isl_basic_map_range_product
, 1);
10727 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
10729 __isl_give isl_map
*isl_map_factor_domain(__isl_take isl_map
*map
)
10732 isl_size total1
, keep1
, total2
, keep2
;
10734 total1
= isl_map_dim(map
, isl_dim_in
);
10735 total2
= isl_map_dim(map
, isl_dim_out
);
10736 if (total1
< 0 || total2
< 0)
10737 return isl_map_free(map
);
10738 if (!isl_space_domain_is_wrapping(map
->dim
) ||
10739 !isl_space_range_is_wrapping(map
->dim
))
10740 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10741 "not a product", return isl_map_free(map
));
10743 space
= isl_map_get_space(map
);
10744 space
= isl_space_factor_domain(space
);
10745 keep1
= isl_space_dim(space
, isl_dim_in
);
10746 keep2
= isl_space_dim(space
, isl_dim_out
);
10747 if (keep1
< 0 || keep2
< 0)
10748 map
= isl_map_free(map
);
10749 map
= isl_map_project_out(map
, isl_dim_in
, keep1
, total1
- keep1
);
10750 map
= isl_map_project_out(map
, isl_dim_out
, keep2
, total2
- keep2
);
10751 map
= isl_map_reset_space(map
, space
);
10756 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
10758 __isl_give isl_map
*isl_map_factor_range(__isl_take isl_map
*map
)
10761 isl_size total1
, keep1
, total2
, keep2
;
10763 total1
= isl_map_dim(map
, isl_dim_in
);
10764 total2
= isl_map_dim(map
, isl_dim_out
);
10765 if (total1
< 0 || total2
< 0)
10766 return isl_map_free(map
);
10767 if (!isl_space_domain_is_wrapping(map
->dim
) ||
10768 !isl_space_range_is_wrapping(map
->dim
))
10769 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10770 "not a product", return isl_map_free(map
));
10772 space
= isl_map_get_space(map
);
10773 space
= isl_space_factor_range(space
);
10774 keep1
= isl_space_dim(space
, isl_dim_in
);
10775 keep2
= isl_space_dim(space
, isl_dim_out
);
10776 if (keep1
< 0 || keep2
< 0)
10777 map
= isl_map_free(map
);
10778 map
= isl_map_project_out(map
, isl_dim_in
, 0, total1
- keep1
);
10779 map
= isl_map_project_out(map
, isl_dim_out
, 0, total2
- keep2
);
10780 map
= isl_map_reset_space(map
, space
);
10785 /* Given a map of the form [A -> B] -> C, return the map A -> C.
10787 __isl_give isl_map
*isl_map_domain_factor_domain(__isl_take isl_map
*map
)
10790 isl_size total
, keep
;
10792 total
= isl_map_dim(map
, isl_dim_in
);
10794 return isl_map_free(map
);
10795 if (!isl_space_domain_is_wrapping(map
->dim
))
10796 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10797 "domain is not a product", return isl_map_free(map
));
10799 space
= isl_map_get_space(map
);
10800 space
= isl_space_domain_factor_domain(space
);
10801 keep
= isl_space_dim(space
, isl_dim_in
);
10803 map
= isl_map_free(map
);
10804 map
= isl_map_project_out(map
, isl_dim_in
, keep
, total
- keep
);
10805 map
= isl_map_reset_space(map
, space
);
10810 /* Given a map of the form [A -> B] -> C, return the map B -> C.
10812 __isl_give isl_map
*isl_map_domain_factor_range(__isl_take isl_map
*map
)
10815 isl_size total
, keep
;
10817 total
= isl_map_dim(map
, isl_dim_in
);
10819 return isl_map_free(map
);
10820 if (!isl_space_domain_is_wrapping(map
->dim
))
10821 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10822 "domain is not a product", return isl_map_free(map
));
10824 space
= isl_map_get_space(map
);
10825 space
= isl_space_domain_factor_range(space
);
10826 keep
= isl_space_dim(space
, isl_dim_in
);
10828 map
= isl_map_free(map
);
10829 map
= isl_map_project_out(map
, isl_dim_in
, 0, total
- keep
);
10830 map
= isl_map_reset_space(map
, space
);
10835 /* Given a map A -> [B -> C], extract the map A -> B.
10837 __isl_give isl_map
*isl_map_range_factor_domain(__isl_take isl_map
*map
)
10840 isl_size total
, keep
;
10842 total
= isl_map_dim(map
, isl_dim_out
);
10844 return isl_map_free(map
);
10845 if (!isl_space_range_is_wrapping(map
->dim
))
10846 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10847 "range is not a product", return isl_map_free(map
));
10849 space
= isl_map_get_space(map
);
10850 space
= isl_space_range_factor_domain(space
);
10851 keep
= isl_space_dim(space
, isl_dim_out
);
10853 map
= isl_map_free(map
);
10854 map
= isl_map_project_out(map
, isl_dim_out
, keep
, total
- keep
);
10855 map
= isl_map_reset_space(map
, space
);
10860 /* Given a map A -> [B -> C], extract the map A -> C.
10862 __isl_give isl_map
*isl_map_range_factor_range(__isl_take isl_map
*map
)
10865 isl_size total
, keep
;
10867 total
= isl_map_dim(map
, isl_dim_out
);
10869 return isl_map_free(map
);
10870 if (!isl_space_range_is_wrapping(map
->dim
))
10871 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10872 "range is not a product", return isl_map_free(map
));
10874 space
= isl_map_get_space(map
);
10875 space
= isl_space_range_factor_range(space
);
10876 keep
= isl_space_dim(space
, isl_dim_out
);
10878 map
= isl_map_free(map
);
10879 map
= isl_map_project_out(map
, isl_dim_out
, 0, total
- keep
);
10880 map
= isl_map_reset_space(map
, space
);
10885 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
10887 __isl_give isl_map
*isl_map_flat_domain_product(__isl_take isl_map
*map1
,
10888 __isl_take isl_map
*map2
)
10892 prod
= isl_map_domain_product(map1
, map2
);
10893 prod
= isl_map_flatten_domain(prod
);
10897 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
10899 __isl_give isl_map
*isl_map_flat_range_product(__isl_take isl_map
*map1
,
10900 __isl_take isl_map
*map2
)
10904 prod
= isl_map_range_product(map1
, map2
);
10905 prod
= isl_map_flatten_range(prod
);
10909 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map
*bmap
)
10912 uint32_t hash
= isl_hash_init();
10917 bmap
= isl_basic_map_copy(bmap
);
10918 bmap
= isl_basic_map_normalize(bmap
);
10919 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
10922 isl_hash_byte(hash
, bmap
->n_eq
& 0xFF);
10923 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
10925 c_hash
= isl_seq_get_hash(bmap
->eq
[i
], 1 + total
);
10926 isl_hash_hash(hash
, c_hash
);
10928 isl_hash_byte(hash
, bmap
->n_ineq
& 0xFF);
10929 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
10931 c_hash
= isl_seq_get_hash(bmap
->ineq
[i
], 1 + total
);
10932 isl_hash_hash(hash
, c_hash
);
10934 isl_hash_byte(hash
, bmap
->n_div
& 0xFF);
10935 for (i
= 0; i
< bmap
->n_div
; ++i
) {
10937 if (isl_int_is_zero(bmap
->div
[i
][0]))
10939 isl_hash_byte(hash
, i
& 0xFF);
10940 c_hash
= isl_seq_get_hash(bmap
->div
[i
], 1 + 1 + total
);
10941 isl_hash_hash(hash
, c_hash
);
10943 isl_basic_map_free(bmap
);
10947 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set
*bset
)
10949 return isl_basic_map_get_hash(bset_to_bmap(bset
));
10952 uint32_t isl_map_get_hash(__isl_keep isl_map
*map
)
10959 map
= isl_map_copy(map
);
10960 map
= isl_map_normalize(map
);
10964 hash
= isl_hash_init();
10965 for (i
= 0; i
< map
->n
; ++i
) {
10966 uint32_t bmap_hash
;
10967 bmap_hash
= isl_basic_map_get_hash(map
->p
[i
]);
10968 isl_hash_hash(hash
, bmap_hash
);
10976 uint32_t isl_set_get_hash(__isl_keep isl_set
*set
)
10978 return isl_map_get_hash(set_to_map(set
));
10981 /* Return the number of basic maps in the (current) representation of "map".
10983 isl_size
isl_map_n_basic_map(__isl_keep isl_map
*map
)
10985 return map
? map
->n
: isl_size_error
;
10988 isl_size
isl_set_n_basic_set(__isl_keep isl_set
*set
)
10990 return set
? set
->n
: isl_size_error
;
10993 isl_stat
isl_map_foreach_basic_map(__isl_keep isl_map
*map
,
10994 isl_stat (*fn
)(__isl_take isl_basic_map
*bmap
, void *user
), void *user
)
10999 return isl_stat_error
;
11001 for (i
= 0; i
< map
->n
; ++i
)
11002 if (fn(isl_basic_map_copy(map
->p
[i
]), user
) < 0)
11003 return isl_stat_error
;
11005 return isl_stat_ok
;
11008 isl_stat
isl_set_foreach_basic_set(__isl_keep isl_set
*set
,
11009 isl_stat (*fn
)(__isl_take isl_basic_set
*bset
, void *user
), void *user
)
11014 return isl_stat_error
;
11016 for (i
= 0; i
< set
->n
; ++i
)
11017 if (fn(isl_basic_set_copy(set
->p
[i
]), user
) < 0)
11018 return isl_stat_error
;
11020 return isl_stat_ok
;
11023 /* Return a list of basic sets, the union of which is equal to "set".
11025 __isl_give isl_basic_set_list
*isl_set_get_basic_set_list(
11026 __isl_keep isl_set
*set
)
11029 isl_basic_set_list
*list
;
11034 list
= isl_basic_set_list_alloc(isl_set_get_ctx(set
), set
->n
);
11035 for (i
= 0; i
< set
->n
; ++i
) {
11036 isl_basic_set
*bset
;
11038 bset
= isl_basic_set_copy(set
->p
[i
]);
11039 list
= isl_basic_set_list_add(list
, bset
);
11045 __isl_give isl_basic_set
*isl_basic_set_lift(__isl_take isl_basic_set
*bset
)
11052 bset
= isl_basic_set_cow(bset
);
11056 space
= isl_basic_set_get_space(bset
);
11057 space
= isl_space_lift(space
, bset
->n_div
);
11060 isl_space_free(bset
->dim
);
11062 bset
->extra
-= bset
->n_div
;
11065 bset
= isl_basic_set_finalize(bset
);
11069 isl_basic_set_free(bset
);
11073 __isl_give isl_set
*isl_set_lift(__isl_take isl_set
*set
)
11079 set
= set_from_map(isl_map_align_divs_internal(set_to_map(set
)));
11084 set
= isl_set_cow(set
);
11088 n_div
= set
->p
[0]->n_div
;
11089 space
= isl_set_get_space(set
);
11090 space
= isl_space_lift(space
, n_div
);
11093 isl_space_free(set
->dim
);
11096 for (i
= 0; i
< set
->n
; ++i
) {
11097 set
->p
[i
] = isl_basic_set_lift(set
->p
[i
]);
11108 int isl_basic_set_size(__isl_keep isl_basic_set
*bset
)
11113 dim
= isl_basic_set_dim(bset
, isl_dim_all
);
11116 size
+= bset
->n_eq
* (1 + dim
);
11117 size
+= bset
->n_ineq
* (1 + dim
);
11118 size
+= bset
->n_div
* (2 + dim
);
11123 int isl_set_size(__isl_keep isl_set
*set
)
11131 for (i
= 0; i
< set
->n
; ++i
)
11132 size
+= isl_basic_set_size(set
->p
[i
]);
11137 /* Check if there is any lower bound (if lower == 0) and/or upper
11138 * bound (if upper == 0) on the specified dim.
11140 static isl_bool
basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
11141 enum isl_dim_type type
, unsigned pos
, int lower
, int upper
)
11145 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
11146 return isl_bool_error
;
11148 pos
+= isl_basic_map_offset(bmap
, type
);
11150 for (i
= 0; i
< bmap
->n_div
; ++i
) {
11151 if (isl_int_is_zero(bmap
->div
[i
][0]))
11153 if (!isl_int_is_zero(bmap
->div
[i
][1 + pos
]))
11154 return isl_bool_true
;
11157 for (i
= 0; i
< bmap
->n_eq
; ++i
)
11158 if (!isl_int_is_zero(bmap
->eq
[i
][pos
]))
11159 return isl_bool_true
;
11161 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
11162 int sgn
= isl_int_sgn(bmap
->ineq
[i
][pos
]);
11169 return lower
&& upper
;
11172 isl_bool
isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
11173 enum isl_dim_type type
, unsigned pos
)
11175 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 0);
11178 isl_bool
isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map
*bmap
,
11179 enum isl_dim_type type
, unsigned pos
)
11181 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 1);
11184 isl_bool
isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map
*bmap
,
11185 enum isl_dim_type type
, unsigned pos
)
11187 return basic_map_dim_is_bounded(bmap
, type
, pos
, 1, 0);
11190 isl_bool
isl_map_dim_is_bounded(__isl_keep isl_map
*map
,
11191 enum isl_dim_type type
, unsigned pos
)
11196 return isl_bool_error
;
11198 for (i
= 0; i
< map
->n
; ++i
) {
11200 bounded
= isl_basic_map_dim_is_bounded(map
->p
[i
], type
, pos
);
11201 if (bounded
< 0 || !bounded
)
11205 return isl_bool_true
;
11208 /* Return true if the specified dim is involved in both an upper bound
11209 * and a lower bound.
11211 isl_bool
isl_set_dim_is_bounded(__isl_keep isl_set
*set
,
11212 enum isl_dim_type type
, unsigned pos
)
11214 return isl_map_dim_is_bounded(set_to_map(set
), type
, pos
);
11217 /* Does "map" have a bound (according to "fn") for any of its basic maps?
11219 static isl_bool
has_any_bound(__isl_keep isl_map
*map
,
11220 enum isl_dim_type type
, unsigned pos
,
11221 isl_bool (*fn
)(__isl_keep isl_basic_map
*bmap
,
11222 enum isl_dim_type type
, unsigned pos
))
11227 return isl_bool_error
;
11229 for (i
= 0; i
< map
->n
; ++i
) {
11231 bounded
= fn(map
->p
[i
], type
, pos
);
11232 if (bounded
< 0 || bounded
)
11236 return isl_bool_false
;
11239 /* Return 1 if the specified dim is involved in any lower bound.
11241 isl_bool
isl_set_dim_has_any_lower_bound(__isl_keep isl_set
*set
,
11242 enum isl_dim_type type
, unsigned pos
)
11244 return has_any_bound(set
, type
, pos
,
11245 &isl_basic_map_dim_has_lower_bound
);
11248 /* Return 1 if the specified dim is involved in any upper bound.
11250 isl_bool
isl_set_dim_has_any_upper_bound(__isl_keep isl_set
*set
,
11251 enum isl_dim_type type
, unsigned pos
)
11253 return has_any_bound(set
, type
, pos
,
11254 &isl_basic_map_dim_has_upper_bound
);
11257 /* Does "map" have a bound (according to "fn") for all of its basic maps?
11259 static isl_bool
has_bound(__isl_keep isl_map
*map
,
11260 enum isl_dim_type type
, unsigned pos
,
11261 isl_bool (*fn
)(__isl_keep isl_basic_map
*bmap
,
11262 enum isl_dim_type type
, unsigned pos
))
11267 return isl_bool_error
;
11269 for (i
= 0; i
< map
->n
; ++i
) {
11271 bounded
= fn(map
->p
[i
], type
, pos
);
11272 if (bounded
< 0 || !bounded
)
11276 return isl_bool_true
;
11279 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
11281 isl_bool
isl_set_dim_has_lower_bound(__isl_keep isl_set
*set
,
11282 enum isl_dim_type type
, unsigned pos
)
11284 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_lower_bound
);
11287 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
11289 isl_bool
isl_set_dim_has_upper_bound(__isl_keep isl_set
*set
,
11290 enum isl_dim_type type
, unsigned pos
)
11292 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_upper_bound
);
11295 /* For each of the "n" variables starting at "first", determine
11296 * the sign of the variable and put the results in the first "n"
11297 * elements of the array "signs".
11299 * 1 means that the variable is non-negative
11300 * -1 means that the variable is non-positive
11301 * 0 means the variable attains both positive and negative values.
11303 isl_stat
isl_basic_set_vars_get_sign(__isl_keep isl_basic_set
*bset
,
11304 unsigned first
, unsigned n
, int *signs
)
11306 isl_vec
*bound
= NULL
;
11307 struct isl_tab
*tab
= NULL
;
11308 struct isl_tab_undo
*snap
;
11312 total
= isl_basic_set_dim(bset
, isl_dim_all
);
11313 if (total
< 0 || !signs
)
11314 return isl_stat_error
;
11316 bound
= isl_vec_alloc(bset
->ctx
, 1 + total
);
11317 tab
= isl_tab_from_basic_set(bset
, 0);
11318 if (!bound
|| !tab
)
11321 isl_seq_clr(bound
->el
, bound
->size
);
11322 isl_int_set_si(bound
->el
[0], -1);
11324 snap
= isl_tab_snap(tab
);
11325 for (i
= 0; i
< n
; ++i
) {
11328 isl_int_set_si(bound
->el
[1 + first
+ i
], -1);
11329 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
11331 empty
= tab
->empty
;
11332 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
11333 if (isl_tab_rollback(tab
, snap
) < 0)
11341 isl_int_set_si(bound
->el
[1 + first
+ i
], 1);
11342 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
11344 empty
= tab
->empty
;
11345 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
11346 if (isl_tab_rollback(tab
, snap
) < 0)
11349 signs
[i
] = empty
? -1 : 0;
11353 isl_vec_free(bound
);
11354 return isl_stat_ok
;
11357 isl_vec_free(bound
);
11358 return isl_stat_error
;
11361 isl_stat
isl_basic_set_dims_get_sign(__isl_keep isl_basic_set
*bset
,
11362 enum isl_dim_type type
, unsigned first
, unsigned n
, int *signs
)
11364 if (!bset
|| !signs
)
11365 return isl_stat_error
;
11366 if (isl_basic_set_check_range(bset
, type
, first
, n
) < 0)
11367 return isl_stat_error
;
11369 first
+= pos(bset
->dim
, type
) - 1;
11370 return isl_basic_set_vars_get_sign(bset
, first
, n
, signs
);
11373 /* Is it possible for the integer division "div" to depend (possibly
11374 * indirectly) on any output dimensions?
11376 * If the div is undefined, then we conservatively assume that it
11377 * may depend on them.
11378 * Otherwise, we check if it actually depends on them or on any integer
11379 * divisions that may depend on them.
11381 static isl_bool
div_may_involve_output(__isl_keep isl_basic_map
*bmap
, int div
)
11384 isl_size n_out
, n_div
;
11385 unsigned o_out
, o_div
;
11387 if (isl_int_is_zero(bmap
->div
[div
][0]))
11388 return isl_bool_true
;
11390 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
11392 return isl_bool_error
;
11393 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
11395 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + o_out
, n_out
) != -1)
11396 return isl_bool_true
;
11398 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
11400 return isl_bool_error
;
11401 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
11403 for (i
= 0; i
< n_div
; ++i
) {
11404 isl_bool may_involve
;
11406 if (isl_int_is_zero(bmap
->div
[div
][1 + o_div
+ i
]))
11408 may_involve
= div_may_involve_output(bmap
, i
);
11409 if (may_involve
< 0 || may_involve
)
11410 return may_involve
;
11413 return isl_bool_false
;
11416 /* Return the first integer division of "bmap" in the range
11417 * [first, first + n[ that may depend on any output dimensions and
11418 * that has a non-zero coefficient in "c" (where the first coefficient
11419 * in "c" corresponds to integer division "first").
11421 static int first_div_may_involve_output(__isl_keep isl_basic_map
*bmap
,
11422 isl_int
*c
, int first
, int n
)
11429 for (k
= first
; k
< first
+ n
; ++k
) {
11430 isl_bool may_involve
;
11432 if (isl_int_is_zero(c
[k
]))
11434 may_involve
= div_may_involve_output(bmap
, k
);
11435 if (may_involve
< 0)
11444 /* Look for a pair of inequality constraints in "bmap" of the form
11446 * -l + i >= 0 or i >= l
11448 * n + l - i >= 0 or i <= l + n
11450 * with n < "m" and i the output dimension at position "pos".
11451 * (Note that n >= 0 as otherwise the two constraints would conflict.)
11452 * Furthermore, "l" is only allowed to involve parameters, input dimensions
11453 * and earlier output dimensions, as well as integer divisions that do
11454 * not involve any of the output dimensions.
11456 * Return the index of the first inequality constraint or bmap->n_ineq
11457 * if no such pair can be found.
11459 static int find_modulo_constraint_pair(__isl_keep isl_basic_map
*bmap
,
11460 int pos
, isl_int m
)
11465 isl_size n_div
, n_out
;
11466 unsigned o_div
, o_out
;
11469 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
11470 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
11471 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
11472 if (total
< 0 || n_out
< 0 || n_div
< 0)
11475 ctx
= isl_basic_map_get_ctx(bmap
);
11476 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
11477 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
11478 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
11479 if (!isl_int_abs_eq(bmap
->ineq
[i
][o_out
+ pos
], ctx
->one
))
11481 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + o_out
+ pos
+ 1,
11482 n_out
- (pos
+ 1)) != -1)
11484 if (first_div_may_involve_output(bmap
, bmap
->ineq
[i
] + o_div
,
11487 for (j
= i
+ 1; j
< bmap
->n_ineq
; ++j
) {
11488 if (!isl_int_abs_eq(bmap
->ineq
[j
][o_out
+ pos
],
11491 if (!isl_seq_is_neg(bmap
->ineq
[i
] + 1,
11492 bmap
->ineq
[j
] + 1, total
))
11496 if (j
>= bmap
->n_ineq
)
11498 isl_int_add(bmap
->ineq
[i
][0],
11499 bmap
->ineq
[i
][0], bmap
->ineq
[j
][0]);
11500 less
= isl_int_abs_lt(bmap
->ineq
[i
][0], m
);
11501 isl_int_sub(bmap
->ineq
[i
][0],
11502 bmap
->ineq
[i
][0], bmap
->ineq
[j
][0]);
11505 if (isl_int_is_one(bmap
->ineq
[i
][o_out
+ pos
]))
11511 return bmap
->n_ineq
;
11514 /* Return the index of the equality of "bmap" that defines
11515 * the output dimension "pos" in terms of earlier dimensions.
11516 * The equality may also involve integer divisions, as long
11517 * as those integer divisions are defined in terms of
11518 * parameters or input dimensions.
11519 * In this case, *div is set to the number of integer divisions and
11520 * *ineq is set to the number of inequality constraints (provided
11521 * div and ineq are not NULL).
11523 * The equality may also involve a single integer division involving
11524 * the output dimensions (typically only output dimension "pos") as
11525 * long as the coefficient of output dimension "pos" is 1 or -1 and
11526 * there is a pair of constraints i >= l and i <= l + n, with i referring
11527 * to output dimension "pos", l an expression involving only earlier
11528 * dimensions and n smaller than the coefficient of the integer division
11529 * in the equality. In this case, the output dimension can be defined
11530 * in terms of a modulo expression that does not involve the integer division.
11531 * *div is then set to this single integer division and
11532 * *ineq is set to the index of constraint i >= l.
11534 * Return bmap->n_eq if there is no such equality.
11535 * Return -1 on error.
11537 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map
*bmap
,
11538 int pos
, int *div
, int *ineq
)
11541 isl_size n_div
, n_out
;
11542 unsigned o_div
, o_out
;
11544 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
11545 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
11546 if (n_out
< 0 || n_div
< 0)
11549 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
11550 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
11553 *ineq
= bmap
->n_ineq
;
11556 for (j
= 0; j
< bmap
->n_eq
; ++j
) {
11557 if (isl_int_is_zero(bmap
->eq
[j
][o_out
+ pos
]))
11559 if (isl_seq_first_non_zero(bmap
->eq
[j
] + o_out
+ pos
+ 1,
11560 n_out
- (pos
+ 1)) != -1)
11562 k
= first_div_may_involve_output(bmap
, bmap
->eq
[j
] + o_div
,
11566 if (!isl_int_is_one(bmap
->eq
[j
][o_out
+ pos
]) &&
11567 !isl_int_is_negone(bmap
->eq
[j
][o_out
+ pos
]))
11569 if (first_div_may_involve_output(bmap
, bmap
->eq
[j
] + o_div
,
11570 k
+ 1, n_div
- (k
+1)) < n_div
)
11572 l
= find_modulo_constraint_pair(bmap
, pos
,
11573 bmap
->eq
[j
][o_div
+ k
]);
11576 if (l
>= bmap
->n_ineq
)
11588 /* Check if the given basic map is obviously single-valued.
11589 * In particular, for each output dimension, check that there is
11590 * an equality that defines the output dimension in terms of
11591 * earlier dimensions.
11593 isl_bool
isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map
*bmap
)
11598 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
11600 return isl_bool_error
;
11602 for (i
= 0; i
< n_out
; ++i
) {
11605 eq
= isl_basic_map_output_defining_equality(bmap
, i
,
11608 return isl_bool_error
;
11609 if (eq
>= bmap
->n_eq
)
11610 return isl_bool_false
;
11613 return isl_bool_true
;
11616 /* Check if the given basic map is single-valued.
11617 * We simply compute
11621 * and check if the result is a subset of the identity mapping.
11623 isl_bool
isl_basic_map_is_single_valued(__isl_keep isl_basic_map
*bmap
)
11626 isl_basic_map
*test
;
11630 sv
= isl_basic_map_plain_is_single_valued(bmap
);
11634 test
= isl_basic_map_reverse(isl_basic_map_copy(bmap
));
11635 test
= isl_basic_map_apply_range(test
, isl_basic_map_copy(bmap
));
11637 space
= isl_basic_map_get_space(bmap
);
11638 space
= isl_space_map_from_set(isl_space_range(space
));
11639 id
= isl_basic_map_identity(space
);
11641 sv
= isl_basic_map_is_subset(test
, id
);
11643 isl_basic_map_free(test
);
11644 isl_basic_map_free(id
);
11649 /* Check if the given map is obviously single-valued.
11651 isl_bool
isl_map_plain_is_single_valued(__isl_keep isl_map
*map
)
11654 return isl_bool_error
;
11656 return isl_bool_true
;
11658 return isl_bool_false
;
11660 return isl_basic_map_plain_is_single_valued(map
->p
[0]);
11663 /* Check if the given map is single-valued.
11664 * We simply compute
11668 * and check if the result is a subset of the identity mapping.
11670 isl_bool
isl_map_is_single_valued(__isl_keep isl_map
*map
)
11677 sv
= isl_map_plain_is_single_valued(map
);
11681 test
= isl_map_reverse(isl_map_copy(map
));
11682 test
= isl_map_apply_range(test
, isl_map_copy(map
));
11684 space
= isl_space_map_from_set(isl_space_range(isl_map_get_space(map
)));
11685 id
= isl_map_identity(space
);
11687 sv
= isl_map_is_subset(test
, id
);
11689 isl_map_free(test
);
11695 isl_bool
isl_map_is_injective(__isl_keep isl_map
*map
)
11699 map
= isl_map_copy(map
);
11700 map
= isl_map_reverse(map
);
11701 in
= isl_map_is_single_valued(map
);
11707 /* Check if the given map is obviously injective.
11709 isl_bool
isl_map_plain_is_injective(__isl_keep isl_map
*map
)
11713 map
= isl_map_copy(map
);
11714 map
= isl_map_reverse(map
);
11715 in
= isl_map_plain_is_single_valued(map
);
11721 isl_bool
isl_map_is_bijective(__isl_keep isl_map
*map
)
11725 sv
= isl_map_is_single_valued(map
);
11729 return isl_map_is_injective(map
);
11732 isl_bool
isl_set_is_singleton(__isl_keep isl_set
*set
)
11734 return isl_map_is_single_valued(set_to_map(set
));
11737 /* Does "map" only map elements to themselves?
11739 * If the domain and range spaces are different, then "map"
11740 * is considered not to be an identity relation, even if it is empty.
11741 * Otherwise, construct the maximal identity relation and
11742 * check whether "map" is a subset of this relation.
11744 isl_bool
isl_map_is_identity(__isl_keep isl_map
*map
)
11748 isl_bool equal
, is_identity
;
11750 space
= isl_map_get_space(map
);
11751 equal
= isl_space_tuple_is_equal(space
, isl_dim_in
, space
, isl_dim_out
);
11752 isl_space_free(space
);
11753 if (equal
< 0 || !equal
)
11756 id
= isl_map_identity(isl_map_get_space(map
));
11757 is_identity
= isl_map_is_subset(map
, id
);
11760 return is_identity
;
11763 int isl_map_is_translation(__isl_keep isl_map
*map
)
11768 delta
= isl_map_deltas(isl_map_copy(map
));
11769 ok
= isl_set_is_singleton(delta
);
11770 isl_set_free(delta
);
11775 static int unique(isl_int
*p
, unsigned pos
, unsigned len
)
11777 if (isl_seq_first_non_zero(p
, pos
) != -1)
11779 if (isl_seq_first_non_zero(p
+ pos
+ 1, len
- pos
- 1) != -1)
11784 isl_bool
isl_basic_set_is_box(__isl_keep isl_basic_set
*bset
)
11787 isl_size nvar
, n_div
;
11790 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
11792 return isl_bool_error
;
11794 return isl_bool_false
;
11796 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
11798 return isl_bool_error
;
11799 ovar
= isl_space_offset(bset
->dim
, isl_dim_set
);
11800 for (j
= 0; j
< nvar
; ++j
) {
11801 int lower
= 0, upper
= 0;
11802 for (i
= 0; i
< bset
->n_eq
; ++i
) {
11803 if (isl_int_is_zero(bset
->eq
[i
][1 + ovar
+ j
]))
11805 if (!unique(bset
->eq
[i
] + 1 + ovar
, j
, nvar
))
11806 return isl_bool_false
;
11809 if (i
< bset
->n_eq
)
11811 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
11812 if (isl_int_is_zero(bset
->ineq
[i
][1 + ovar
+ j
]))
11814 if (!unique(bset
->ineq
[i
] + 1 + ovar
, j
, nvar
))
11815 return isl_bool_false
;
11816 if (isl_int_is_pos(bset
->ineq
[i
][1 + ovar
+ j
]))
11821 if (!lower
|| !upper
)
11822 return isl_bool_false
;
11825 return isl_bool_true
;
11828 isl_bool
isl_set_is_box(__isl_keep isl_set
*set
)
11831 return isl_bool_error
;
11833 return isl_bool_false
;
11835 return isl_basic_set_is_box(set
->p
[0]);
11838 isl_bool
isl_basic_set_is_wrapping(__isl_keep isl_basic_set
*bset
)
11841 return isl_bool_error
;
11843 return isl_space_is_wrapping(bset
->dim
);
11846 isl_bool
isl_set_is_wrapping(__isl_keep isl_set
*set
)
11849 return isl_bool_error
;
11851 return isl_space_is_wrapping(set
->dim
);
11854 /* Modify the space of "map" through a call to "change".
11855 * If "can_change" is set (not NULL), then first call it to check
11856 * if the modification is allowed, printing the error message "cannot_change"
11859 static __isl_give isl_map
*isl_map_change_space(__isl_take isl_map
*map
,
11860 isl_bool (*can_change
)(__isl_keep isl_map
*map
),
11861 const char *cannot_change
,
11862 __isl_give isl_space
*(*change
)(__isl_take isl_space
*space
))
11870 ok
= can_change
? can_change(map
) : isl_bool_true
;
11872 return isl_map_free(map
);
11874 isl_die(isl_map_get_ctx(map
), isl_error_invalid
, cannot_change
,
11875 return isl_map_free(map
));
11877 space
= change(isl_map_get_space(map
));
11878 map
= isl_map_reset_space(map
, space
);
11883 /* Is the domain of "map" a wrapped relation?
11885 isl_bool
isl_map_domain_is_wrapping(__isl_keep isl_map
*map
)
11888 return isl_bool_error
;
11890 return isl_space_domain_is_wrapping(map
->dim
);
11893 /* Does "map" have a wrapped relation in both domain and range?
11895 isl_bool
isl_map_is_product(__isl_keep isl_map
*map
)
11897 return isl_space_is_product(isl_map_peek_space(map
));
11900 /* Is the range of "map" a wrapped relation?
11902 isl_bool
isl_map_range_is_wrapping(__isl_keep isl_map
*map
)
11905 return isl_bool_error
;
11907 return isl_space_range_is_wrapping(map
->dim
);
11910 __isl_give isl_basic_set
*isl_basic_map_wrap(__isl_take isl_basic_map
*bmap
)
11914 space
= isl_basic_map_take_space(bmap
);
11915 space
= isl_space_wrap(space
);
11916 bmap
= isl_basic_map_restore_space(bmap
, space
);
11918 bmap
= isl_basic_map_finalize(bmap
);
11920 return bset_from_bmap(bmap
);
11923 /* Given a map A -> B, return the set (A -> B).
11925 __isl_give isl_set
*isl_map_wrap(__isl_take isl_map
*map
)
11927 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_wrap
);
11930 __isl_give isl_basic_map
*isl_basic_set_unwrap(__isl_take isl_basic_set
*bset
)
11932 bset
= isl_basic_set_cow(bset
);
11936 bset
->dim
= isl_space_unwrap(bset
->dim
);
11940 bset
= isl_basic_set_finalize(bset
);
11942 return bset_to_bmap(bset
);
11944 isl_basic_set_free(bset
);
11948 /* Given a set (A -> B), return the map A -> B.
11949 * Error out if "set" is not of the form (A -> B).
11951 __isl_give isl_map
*isl_set_unwrap(__isl_take isl_set
*set
)
11953 return isl_map_change_space(set
, &isl_set_is_wrapping
,
11954 "not a wrapping set", &isl_space_unwrap
);
11957 __isl_give isl_basic_map
*isl_basic_map_reset(__isl_take isl_basic_map
*bmap
,
11958 enum isl_dim_type type
)
11962 space
= isl_basic_map_take_space(bmap
);
11963 space
= isl_space_reset(space
, type
);
11964 bmap
= isl_basic_map_restore_space(bmap
, space
);
11966 bmap
= isl_basic_map_mark_final(bmap
);
11971 __isl_give isl_map
*isl_map_reset(__isl_take isl_map
*map
,
11972 enum isl_dim_type type
)
11980 if (!isl_space_is_named_or_nested(map
->dim
, type
))
11983 map
= isl_map_cow(map
);
11987 for (i
= 0; i
< map
->n
; ++i
) {
11988 map
->p
[i
] = isl_basic_map_reset(map
->p
[i
], type
);
11993 space
= isl_map_take_space(map
);
11994 space
= isl_space_reset(space
, type
);
11995 map
= isl_map_restore_space(map
, space
);
12003 __isl_give isl_basic_map
*isl_basic_map_flatten(__isl_take isl_basic_map
*bmap
)
12007 space
= isl_basic_map_take_space(bmap
);
12008 space
= isl_space_flatten(space
);
12009 bmap
= isl_basic_map_restore_space(bmap
, space
);
12011 bmap
= isl_basic_map_mark_final(bmap
);
12016 __isl_give isl_basic_set
*isl_basic_set_flatten(__isl_take isl_basic_set
*bset
)
12018 return bset_from_bmap(isl_basic_map_flatten(bset_to_bmap(bset
)));
12021 __isl_give isl_basic_map
*isl_basic_map_flatten_domain(
12022 __isl_take isl_basic_map
*bmap
)
12026 space
= isl_basic_map_take_space(bmap
);
12027 space
= isl_space_flatten_domain(space
);
12028 bmap
= isl_basic_map_restore_space(bmap
, space
);
12030 bmap
= isl_basic_map_mark_final(bmap
);
12035 __isl_give isl_basic_map
*isl_basic_map_flatten_range(
12036 __isl_take isl_basic_map
*bmap
)
12040 space
= isl_basic_map_take_space(bmap
);
12041 space
= isl_space_flatten_range(space
);
12042 bmap
= isl_basic_map_restore_space(bmap
, space
);
12044 bmap
= isl_basic_map_mark_final(bmap
);
12049 /* Remove any internal structure from the spaces of domain and range of "map".
12051 __isl_give isl_map
*isl_map_flatten(__isl_take isl_map
*map
)
12056 if (!map
->dim
->nested
[0] && !map
->dim
->nested
[1])
12059 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten
);
12062 __isl_give isl_set
*isl_set_flatten(__isl_take isl_set
*set
)
12064 return set_from_map(isl_map_flatten(set_to_map(set
)));
12067 __isl_give isl_map
*isl_set_flatten_map(__isl_take isl_set
*set
)
12069 isl_space
*space
, *flat_space
;
12072 space
= isl_set_get_space(set
);
12073 flat_space
= isl_space_flatten(isl_space_copy(space
));
12074 map
= isl_map_identity(isl_space_join(isl_space_reverse(space
),
12076 map
= isl_map_intersect_domain(map
, set
);
12081 /* Remove any internal structure from the space of the domain of "map".
12083 __isl_give isl_map
*isl_map_flatten_domain(__isl_take isl_map
*map
)
12088 if (!map
->dim
->nested
[0])
12091 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten_domain
);
12094 /* Remove any internal structure from the space of the range of "map".
12096 __isl_give isl_map
*isl_map_flatten_range(__isl_take isl_map
*map
)
12101 if (!map
->dim
->nested
[1])
12104 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten_range
);
12107 /* Reorder the dimensions of "bmap" according to the given dim_map
12108 * and set the dimension specification to "space" and
12109 * perform Gaussian elimination on the result.
12111 __isl_give isl_basic_map
*isl_basic_map_realign(__isl_take isl_basic_map
*bmap
,
12112 __isl_take isl_space
*space
, __isl_take
struct isl_dim_map
*dim_map
)
12114 isl_basic_map
*res
;
12118 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
12119 if (n_div
< 0 || !space
|| !dim_map
)
12122 flags
= bmap
->flags
;
12123 ISL_FL_CLR(flags
, ISL_BASIC_MAP_FINAL
);
12124 ISL_FL_CLR(flags
, ISL_BASIC_MAP_SORTED
);
12125 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
12126 res
= isl_basic_map_alloc_space(space
, n_div
, bmap
->n_eq
, bmap
->n_ineq
);
12127 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
12129 res
->flags
= flags
;
12130 res
= isl_basic_map_gauss(res
, NULL
);
12131 res
= isl_basic_map_finalize(res
);
12134 isl_dim_map_free(dim_map
);
12135 isl_basic_map_free(bmap
);
12136 isl_space_free(space
);
12140 /* Reorder the dimensions of "map" according to given reordering.
12142 __isl_give isl_map
*isl_map_realign(__isl_take isl_map
*map
,
12143 __isl_take isl_reordering
*r
)
12146 struct isl_dim_map
*dim_map
;
12148 map
= isl_map_cow(map
);
12149 dim_map
= isl_dim_map_from_reordering(r
);
12150 if (!map
|| !r
|| !dim_map
)
12153 for (i
= 0; i
< map
->n
; ++i
) {
12154 struct isl_dim_map
*dim_map_i
;
12157 dim_map_i
= isl_dim_map_extend(dim_map
, map
->p
[i
]);
12159 space
= isl_reordering_get_space(r
);
12160 map
->p
[i
] = isl_basic_map_realign(map
->p
[i
], space
, dim_map_i
);
12166 map
= isl_map_reset_space(map
, isl_reordering_get_space(r
));
12167 map
= isl_map_unmark_normalized(map
);
12169 isl_reordering_free(r
);
12170 isl_dim_map_free(dim_map
);
12173 isl_dim_map_free(dim_map
);
12175 isl_reordering_free(r
);
12179 __isl_give isl_set
*isl_set_realign(__isl_take isl_set
*set
,
12180 __isl_take isl_reordering
*r
)
12182 return set_from_map(isl_map_realign(set_to_map(set
), r
));
12185 __isl_give isl_map
*isl_map_align_params(__isl_take isl_map
*map
,
12186 __isl_take isl_space
*model
)
12191 if (!map
|| !model
)
12194 ctx
= isl_space_get_ctx(model
);
12195 if (!isl_space_has_named_params(model
))
12196 isl_die(ctx
, isl_error_invalid
,
12197 "model has unnamed parameters", goto error
);
12198 if (isl_map_check_named_params(map
) < 0)
12200 aligned
= isl_map_space_has_equal_params(map
, model
);
12204 isl_reordering
*exp
;
12206 exp
= isl_parameter_alignment_reordering(map
->dim
, model
);
12207 exp
= isl_reordering_extend_space(exp
, isl_map_get_space(map
));
12208 map
= isl_map_realign(map
, exp
);
12211 isl_space_free(model
);
12214 isl_space_free(model
);
12219 __isl_give isl_set
*isl_set_align_params(__isl_take isl_set
*set
,
12220 __isl_take isl_space
*model
)
12222 return isl_map_align_params(set
, model
);
12225 /* Align the parameters of "bmap" to those of "model", introducing
12226 * additional parameters if needed.
12228 __isl_give isl_basic_map
*isl_basic_map_align_params(
12229 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*model
)
12232 isl_bool equal_params
;
12234 if (!bmap
|| !model
)
12237 ctx
= isl_space_get_ctx(model
);
12238 if (!isl_space_has_named_params(model
))
12239 isl_die(ctx
, isl_error_invalid
,
12240 "model has unnamed parameters", goto error
);
12241 if (isl_basic_map_check_named_params(bmap
) < 0)
12243 equal_params
= isl_space_has_equal_params(bmap
->dim
, model
);
12244 if (equal_params
< 0)
12246 if (!equal_params
) {
12247 isl_reordering
*exp
;
12248 struct isl_dim_map
*dim_map
;
12250 exp
= isl_parameter_alignment_reordering(bmap
->dim
, model
);
12251 exp
= isl_reordering_extend_space(exp
,
12252 isl_basic_map_get_space(bmap
));
12253 dim_map
= isl_dim_map_from_reordering(exp
);
12254 bmap
= isl_basic_map_realign(bmap
,
12255 isl_reordering_get_space(exp
),
12256 isl_dim_map_extend(dim_map
, bmap
));
12257 isl_reordering_free(exp
);
12258 isl_dim_map_free(dim_map
);
12261 isl_space_free(model
);
12264 isl_space_free(model
);
12265 isl_basic_map_free(bmap
);
12269 /* Do "bset" and "space" have the same parameters?
12271 isl_bool
isl_basic_set_space_has_equal_params(__isl_keep isl_basic_set
*bset
,
12272 __isl_keep isl_space
*space
)
12274 isl_space
*bset_space
;
12276 bset_space
= isl_basic_set_peek_space(bset
);
12277 return isl_space_has_equal_params(bset_space
, space
);
12280 /* Do "map" and "space" have the same parameters?
12282 isl_bool
isl_map_space_has_equal_params(__isl_keep isl_map
*map
,
12283 __isl_keep isl_space
*space
)
12285 isl_space
*map_space
;
12287 map_space
= isl_map_peek_space(map
);
12288 return isl_space_has_equal_params(map_space
, space
);
12291 /* Do "set" and "space" have the same parameters?
12293 isl_bool
isl_set_space_has_equal_params(__isl_keep isl_set
*set
,
12294 __isl_keep isl_space
*space
)
12296 return isl_map_space_has_equal_params(set_to_map(set
), space
);
12299 /* Align the parameters of "bset" to those of "model", introducing
12300 * additional parameters if needed.
12302 __isl_give isl_basic_set
*isl_basic_set_align_params(
12303 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*model
)
12305 return isl_basic_map_align_params(bset
, model
);
12308 /* Drop all parameters not referenced by "map".
12310 __isl_give isl_map
*isl_map_drop_unused_params(__isl_take isl_map
*map
)
12315 n
= isl_map_dim(map
, isl_dim_param
);
12316 if (isl_map_check_named_params(map
) < 0 || n
< 0)
12317 return isl_map_free(map
);
12319 for (i
= n
- 1; i
>= 0; i
--) {
12322 involves
= isl_map_involves_dims(map
, isl_dim_param
, i
, 1);
12324 return isl_map_free(map
);
12326 map
= isl_map_project_out(map
, isl_dim_param
, i
, 1);
12332 /* Drop all parameters not referenced by "set".
12334 __isl_give isl_set
*isl_set_drop_unused_params(
12335 __isl_take isl_set
*set
)
12337 return set_from_map(isl_map_drop_unused_params(set_to_map(set
)));
12340 /* Drop all parameters not referenced by "bmap".
12342 __isl_give isl_basic_map
*isl_basic_map_drop_unused_params(
12343 __isl_take isl_basic_map
*bmap
)
12348 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
12349 if (nparam
< 0 || isl_basic_map_check_named_params(bmap
) < 0)
12350 return isl_basic_map_free(bmap
);
12352 for (i
= nparam
- 1; i
>= 0; i
--) {
12355 involves
= isl_basic_map_involves_dims(bmap
,
12356 isl_dim_param
, i
, 1);
12358 return isl_basic_map_free(bmap
);
12360 bmap
= isl_basic_map_drop(bmap
, isl_dim_param
, i
, 1);
12366 /* Drop all parameters not referenced by "bset".
12368 __isl_give isl_basic_set
*isl_basic_set_drop_unused_params(
12369 __isl_take isl_basic_set
*bset
)
12371 return bset_from_bmap(isl_basic_map_drop_unused_params(
12372 bset_to_bmap(bset
)));
12375 /* Given a tuple of identifiers "tuple" in a space that corresponds
12376 * to that of "set", if any of those identifiers appear as parameters
12377 * in "set", then equate those parameters with the corresponding
12378 * set dimensions and project out the parameters.
12379 * The result therefore has no such parameters.
12381 static __isl_give isl_set
*equate_params(__isl_take isl_set
*set
,
12382 __isl_keep isl_multi_id
*tuple
)
12386 isl_space
*set_space
, *tuple_space
;
12388 set_space
= isl_set_peek_space(set
);
12389 tuple_space
= isl_multi_id_peek_space(tuple
);
12390 if (isl_space_check_equal_tuples(tuple_space
, set_space
) < 0)
12391 return isl_set_free(set
);
12392 n
= isl_multi_id_size(tuple
);
12394 return isl_set_free(set
);
12395 for (i
= 0; i
< n
; ++i
) {
12399 id
= isl_multi_id_get_at(tuple
, i
);
12401 return isl_set_free(set
);
12402 pos
= isl_set_find_dim_by_id(set
, isl_dim_param
, id
);
12406 set
= isl_set_equate(set
, isl_dim_param
, pos
, isl_dim_set
, i
);
12407 set
= isl_set_project_out(set
, isl_dim_param
, pos
, 1);
12412 /* Bind the set dimensions of "set" to parameters with identifiers
12413 * specified by "tuple", living in the same space as "set".
12415 * If no parameters with these identifiers appear in "set" already,
12416 * then the set dimensions are simply reinterpreted as parameters.
12417 * Otherwise, the parameters are first equated to the corresponding
12420 __isl_give isl_set
*isl_set_bind(__isl_take isl_set
*set
,
12421 __isl_take isl_multi_id
*tuple
)
12425 set
= equate_params(set
, tuple
);
12426 space
= isl_set_get_space(set
);
12427 space
= isl_space_bind_set(space
, tuple
);
12428 isl_multi_id_free(tuple
);
12429 set
= isl_set_reset_space(set
, space
);
12434 /* Given a tuple of identifiers "tuple" in a space that corresponds
12435 * to the domain of "map", if any of those identifiers appear as parameters
12436 * in "map", then equate those parameters with the corresponding
12437 * input dimensions and project out the parameters.
12438 * The result therefore has no such parameters.
12440 static __isl_give isl_map
*map_equate_params(__isl_take isl_map
*map
,
12441 __isl_keep isl_multi_id
*tuple
)
12445 isl_space
*map_space
, *tuple_space
;
12447 map_space
= isl_map_peek_space(map
);
12448 tuple_space
= isl_multi_id_peek_space(tuple
);
12449 if (isl_space_check_domain_tuples(tuple_space
, map_space
) < 0)
12450 return isl_map_free(map
);
12451 n
= isl_multi_id_size(tuple
);
12453 return isl_map_free(map
);
12454 for (i
= 0; i
< n
; ++i
) {
12458 id
= isl_multi_id_get_at(tuple
, i
);
12460 return isl_map_free(map
);
12461 pos
= isl_map_find_dim_by_id(map
, isl_dim_param
, id
);
12465 map
= isl_map_equate(map
, isl_dim_param
, pos
, isl_dim_in
, i
);
12466 map
= isl_map_project_out(map
, isl_dim_param
, pos
, 1);
12471 /* Bind the input dimensions of "map" to parameters with identifiers
12472 * specified by "tuple", living in the domain space of "map".
12474 * If no parameters with these identifiers appear in "map" already,
12475 * then the input dimensions are simply reinterpreted as parameters.
12476 * Otherwise, the parameters are first equated to the corresponding
12477 * input dimensions.
12479 __isl_give isl_set
*isl_map_bind_domain(__isl_take isl_map
*map
,
12480 __isl_take isl_multi_id
*tuple
)
12485 map
= map_equate_params(map
, tuple
);
12486 space
= isl_map_get_space(map
);
12487 space
= isl_space_bind_map_domain(space
, tuple
);
12488 isl_multi_id_free(tuple
);
12489 set
= set_from_map(isl_map_reset_space(map
, space
));
12494 /* Bind the output dimensions of "map" to parameters with identifiers
12495 * specified by "tuple", living in the range space of "map".
12497 * Since binding is more easily implemented on the domain,
12498 * bind the input dimensions of the inverse of "map".
12500 __isl_give isl_set
*isl_map_bind_range(__isl_take isl_map
*map
,
12501 __isl_take isl_multi_id
*tuple
)
12503 return isl_map_bind_domain(isl_map_reverse(map
), tuple
);
12506 /* Insert a domain corresponding to "tuple"
12507 * into the nullary or unary relation "set".
12508 * The result has an extra initial tuple and is therefore
12509 * either a unary or binary relation.
12510 * Any parameters with identifiers in "tuple" are reinterpreted
12511 * as the corresponding domain dimensions.
12513 static __isl_give isl_map
*unbind_params_insert_domain(
12514 __isl_take isl_set
*set
, __isl_take isl_multi_id
*tuple
)
12519 space
= isl_set_peek_space(set
);
12520 r
= isl_reordering_unbind_params_insert_domain(space
, tuple
);
12521 isl_multi_id_free(tuple
);
12523 return isl_map_realign(set_to_map(set
), r
);
12526 /* Construct a set with "tuple" as domain from the parameter domain "set".
12527 * Any parameters with identifiers in "tuple" are reinterpreted
12528 * as the corresponding set dimensions.
12530 __isl_give isl_set
*isl_set_unbind_params(__isl_take isl_set
*set
,
12531 __isl_take isl_multi_id
*tuple
)
12533 isl_bool is_params
;
12535 is_params
= isl_set_is_params(set
);
12537 set
= isl_set_free(set
);
12538 else if (!is_params
)
12539 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
12540 "expecting parameter domain", set
= isl_set_free(set
));
12541 return set_from_map(unbind_params_insert_domain(set
, tuple
));
12544 /* Construct a map with "domain" as domain and "set" as range.
12545 * Any parameters with identifiers in "domain" are reinterpreted
12546 * as the corresponding domain dimensions.
12548 __isl_give isl_map
*isl_set_unbind_params_insert_domain(
12549 __isl_take isl_set
*set
, __isl_take isl_multi_id
*domain
)
12551 isl_bool is_params
;
12553 is_params
= isl_set_is_params(set
);
12555 set
= isl_set_free(set
);
12556 else if (is_params
)
12557 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
12558 "expecting proper set", set
= isl_set_free(set
));
12559 return unbind_params_insert_domain(set
, domain
);
12562 __isl_give isl_mat
*isl_basic_map_equalities_matrix(
12563 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
12564 enum isl_dim_type c2
, enum isl_dim_type c3
,
12565 enum isl_dim_type c4
, enum isl_dim_type c5
)
12567 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
12568 struct isl_mat
*mat
;
12573 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
12576 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_eq
, total
+ 1);
12579 for (i
= 0; i
< bmap
->n_eq
; ++i
)
12580 for (j
= 0, pos
= 0; j
< 5; ++j
) {
12581 int off
= isl_basic_map_offset(bmap
, c
[j
]);
12582 isl_size dim
= isl_basic_map_dim(bmap
, c
[j
]);
12584 return isl_mat_free(mat
);
12585 for (k
= 0; k
< dim
; ++k
) {
12586 isl_int_set(mat
->row
[i
][pos
],
12587 bmap
->eq
[i
][off
+ k
]);
12595 __isl_give isl_mat
*isl_basic_map_inequalities_matrix(
12596 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
12597 enum isl_dim_type c2
, enum isl_dim_type c3
,
12598 enum isl_dim_type c4
, enum isl_dim_type c5
)
12600 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
12601 struct isl_mat
*mat
;
12606 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
12609 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_ineq
, total
+ 1);
12612 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
12613 for (j
= 0, pos
= 0; j
< 5; ++j
) {
12614 int off
= isl_basic_map_offset(bmap
, c
[j
]);
12615 isl_size dim
= isl_basic_map_dim(bmap
, c
[j
]);
12617 return isl_mat_free(mat
);
12618 for (k
= 0; k
< dim
; ++k
) {
12619 isl_int_set(mat
->row
[i
][pos
],
12620 bmap
->ineq
[i
][off
+ k
]);
12628 __isl_give isl_basic_map
*isl_basic_map_from_constraint_matrices(
12629 __isl_take isl_space
*space
,
12630 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
12631 enum isl_dim_type c2
, enum isl_dim_type c3
,
12632 enum isl_dim_type c4
, enum isl_dim_type c5
)
12634 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
12635 isl_basic_map
*bmap
= NULL
;
12642 dim
= isl_space_dim(space
, isl_dim_all
);
12643 if (dim
< 0 || !eq
|| !ineq
)
12646 if (eq
->n_col
!= ineq
->n_col
)
12647 isl_die(space
->ctx
, isl_error_invalid
,
12648 "equalities and inequalities matrices should have "
12649 "same number of columns", goto error
);
12653 if (eq
->n_col
< total
)
12654 isl_die(space
->ctx
, isl_error_invalid
,
12655 "number of columns too small", goto error
);
12657 extra
= eq
->n_col
- total
;
12659 bmap
= isl_basic_map_alloc_space(isl_space_copy(space
), extra
,
12660 eq
->n_row
, ineq
->n_row
);
12663 for (i
= 0; i
< extra
; ++i
) {
12664 k
= isl_basic_map_alloc_div(bmap
);
12667 isl_int_set_si(bmap
->div
[k
][0], 0);
12669 for (i
= 0; i
< eq
->n_row
; ++i
) {
12670 l
= isl_basic_map_alloc_equality(bmap
);
12673 for (j
= 0, pos
= 0; j
< 5; ++j
) {
12674 int off
= isl_basic_map_offset(bmap
, c
[j
]);
12675 isl_size dim
= isl_basic_map_dim(bmap
, c
[j
]);
12678 for (k
= 0; k
< dim
; ++k
) {
12679 isl_int_set(bmap
->eq
[l
][off
+ k
],
12685 for (i
= 0; i
< ineq
->n_row
; ++i
) {
12686 l
= isl_basic_map_alloc_inequality(bmap
);
12689 for (j
= 0, pos
= 0; j
< 5; ++j
) {
12690 int off
= isl_basic_map_offset(bmap
, c
[j
]);
12691 isl_size dim
= isl_basic_map_dim(bmap
, c
[j
]);
12694 for (k
= 0; k
< dim
; ++k
) {
12695 isl_int_set(bmap
->ineq
[l
][off
+ k
],
12696 ineq
->row
[i
][pos
]);
12702 isl_space_free(space
);
12704 isl_mat_free(ineq
);
12706 bmap
= isl_basic_map_simplify(bmap
);
12707 return isl_basic_map_finalize(bmap
);
12709 isl_space_free(space
);
12711 isl_mat_free(ineq
);
12712 isl_basic_map_free(bmap
);
12716 __isl_give isl_mat
*isl_basic_set_equalities_matrix(
12717 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
12718 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
12720 return isl_basic_map_equalities_matrix(bset_to_bmap(bset
),
12721 c1
, c2
, c3
, c4
, isl_dim_in
);
12724 __isl_give isl_mat
*isl_basic_set_inequalities_matrix(
12725 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
12726 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
12728 return isl_basic_map_inequalities_matrix(bset_to_bmap(bset
),
12729 c1
, c2
, c3
, c4
, isl_dim_in
);
12732 __isl_give isl_basic_set
*isl_basic_set_from_constraint_matrices(
12733 __isl_take isl_space
*space
,
12734 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
12735 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
12737 isl_basic_map
*bmap
;
12738 bmap
= isl_basic_map_from_constraint_matrices(space
, eq
, ineq
,
12739 c1
, c2
, c3
, c4
, isl_dim_in
);
12740 return bset_from_bmap(bmap
);
12743 isl_bool
isl_basic_map_can_zip(__isl_keep isl_basic_map
*bmap
)
12746 return isl_bool_error
;
12748 return isl_space_can_zip(bmap
->dim
);
12751 isl_bool
isl_map_can_zip(__isl_keep isl_map
*map
)
12754 return isl_bool_error
;
12756 return isl_space_can_zip(map
->dim
);
12759 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
12760 * (A -> C) -> (B -> D).
12762 __isl_give isl_basic_map
*isl_basic_map_zip(__isl_take isl_basic_map
*bmap
)
12772 if (!isl_basic_map_can_zip(bmap
))
12773 isl_die(bmap
->ctx
, isl_error_invalid
,
12774 "basic map cannot be zipped", goto error
);
12775 n_in
= isl_space_dim(bmap
->dim
->nested
[0], isl_dim_in
);
12776 n1
= isl_space_dim(bmap
->dim
->nested
[0], isl_dim_out
);
12777 n2
= isl_space_dim(bmap
->dim
->nested
[1], isl_dim_in
);
12778 if (n_in
< 0 || n1
< 0 || n2
< 0)
12779 return isl_basic_map_free(bmap
);
12780 pos
= isl_basic_map_offset(bmap
, isl_dim_in
) + n_in
;
12781 bmap
= isl_basic_map_cow(bmap
);
12782 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
12785 bmap
->dim
= isl_space_zip(bmap
->dim
);
12788 bmap
= isl_basic_map_mark_final(bmap
);
12791 isl_basic_map_free(bmap
);
12795 /* Given a map (A -> B) -> (C -> D), return the corresponding map
12796 * (A -> C) -> (B -> D).
12798 __isl_give isl_map
*isl_map_zip(__isl_take isl_map
*map
)
12803 if (!isl_map_can_zip(map
))
12804 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be zipped",
12807 return isl_map_transform(map
, &isl_space_zip
, &isl_basic_map_zip
);
12813 /* Can we apply isl_basic_map_curry to "bmap"?
12814 * That is, does it have a nested relation in its domain?
12816 isl_bool
isl_basic_map_can_curry(__isl_keep isl_basic_map
*bmap
)
12819 return isl_bool_error
;
12821 return isl_space_can_curry(bmap
->dim
);
12824 /* Can we apply isl_map_curry to "map"?
12825 * That is, does it have a nested relation in its domain?
12827 isl_bool
isl_map_can_curry(__isl_keep isl_map
*map
)
12830 return isl_bool_error
;
12832 return isl_space_can_curry(map
->dim
);
12835 /* Given a basic map (A -> B) -> C, return the corresponding basic map
12838 __isl_give isl_basic_map
*isl_basic_map_curry(__isl_take isl_basic_map
*bmap
)
12844 if (!isl_basic_map_can_curry(bmap
))
12845 isl_die(bmap
->ctx
, isl_error_invalid
,
12846 "basic map cannot be curried", goto error
);
12847 bmap
= isl_basic_map_cow(bmap
);
12850 bmap
->dim
= isl_space_curry(bmap
->dim
);
12853 bmap
= isl_basic_map_mark_final(bmap
);
12856 isl_basic_map_free(bmap
);
12860 /* Given a map (A -> B) -> C, return the corresponding map
12863 __isl_give isl_map
*isl_map_curry(__isl_take isl_map
*map
)
12865 return isl_map_change_space(map
, &isl_map_can_curry
,
12866 "map cannot be curried", &isl_space_curry
);
12869 /* Can isl_map_range_curry be applied to "map"?
12870 * That is, does it have a nested relation in its range,
12871 * the domain of which is itself a nested relation?
12873 isl_bool
isl_map_can_range_curry(__isl_keep isl_map
*map
)
12876 return isl_bool_error
;
12878 return isl_space_can_range_curry(map
->dim
);
12881 /* Given a map A -> ((B -> C) -> D), return the corresponding map
12882 * A -> (B -> (C -> D)).
12884 __isl_give isl_map
*isl_map_range_curry(__isl_take isl_map
*map
)
12886 return isl_map_change_space(map
, &isl_map_can_range_curry
,
12887 "map range cannot be curried",
12888 &isl_space_range_curry
);
12891 /* Can we apply isl_basic_map_uncurry to "bmap"?
12892 * That is, does it have a nested relation in its domain?
12894 isl_bool
isl_basic_map_can_uncurry(__isl_keep isl_basic_map
*bmap
)
12897 return isl_bool_error
;
12899 return isl_space_can_uncurry(bmap
->dim
);
12902 /* Can we apply isl_map_uncurry to "map"?
12903 * That is, does it have a nested relation in its domain?
12905 isl_bool
isl_map_can_uncurry(__isl_keep isl_map
*map
)
12908 return isl_bool_error
;
12910 return isl_space_can_uncurry(map
->dim
);
12913 /* Given a basic map A -> (B -> C), return the corresponding basic map
12916 __isl_give isl_basic_map
*isl_basic_map_uncurry(__isl_take isl_basic_map
*bmap
)
12922 if (!isl_basic_map_can_uncurry(bmap
))
12923 isl_die(bmap
->ctx
, isl_error_invalid
,
12924 "basic map cannot be uncurried",
12925 return isl_basic_map_free(bmap
));
12926 bmap
= isl_basic_map_cow(bmap
);
12929 bmap
->dim
= isl_space_uncurry(bmap
->dim
);
12931 return isl_basic_map_free(bmap
);
12932 bmap
= isl_basic_map_mark_final(bmap
);
12936 /* Given a map A -> (B -> C), return the corresponding map
12939 __isl_give isl_map
*isl_map_uncurry(__isl_take isl_map
*map
)
12941 return isl_map_change_space(map
, &isl_map_can_uncurry
,
12942 "map cannot be uncurried", &isl_space_uncurry
);
12945 __isl_give isl_set
*isl_set_equate(__isl_take isl_set
*set
,
12946 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12948 return isl_map_equate(set
, type1
, pos1
, type2
, pos2
);
12951 /* Construct a basic map where the given dimensions are equal to each other.
12953 static __isl_give isl_basic_map
*equator(__isl_take isl_space
*space
,
12954 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12956 isl_basic_map
*bmap
= NULL
;
12960 total
= isl_space_dim(space
, isl_dim_all
);
12962 isl_space_check_range(space
, type1
, pos1
, 1) < 0 ||
12963 isl_space_check_range(space
, type2
, pos2
, 1) < 0)
12966 if (type1
== type2
&& pos1
== pos2
)
12967 return isl_basic_map_universe(space
);
12969 bmap
= isl_basic_map_alloc_space(isl_space_copy(space
), 0, 1, 0);
12970 i
= isl_basic_map_alloc_equality(bmap
);
12973 isl_seq_clr(bmap
->eq
[i
], 1 + total
);
12974 pos1
+= isl_basic_map_offset(bmap
, type1
);
12975 pos2
+= isl_basic_map_offset(bmap
, type2
);
12976 isl_int_set_si(bmap
->eq
[i
][pos1
], -1);
12977 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
12978 bmap
= isl_basic_map_finalize(bmap
);
12979 isl_space_free(space
);
12982 isl_space_free(space
);
12983 isl_basic_map_free(bmap
);
12987 /* Add a constraint imposing that the given two dimensions are equal.
12989 __isl_give isl_basic_map
*isl_basic_map_equate(__isl_take isl_basic_map
*bmap
,
12990 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
12994 eq
= equator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
12996 bmap
= isl_basic_map_intersect(bmap
, eq
);
13001 /* Add a constraint imposing that the given two dimensions are equal.
13003 __isl_give isl_map
*isl_map_equate(__isl_take isl_map
*map
,
13004 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13006 isl_basic_map
*bmap
;
13008 bmap
= equator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
13010 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
13015 /* Add a constraint imposing that the given two dimensions have opposite values.
13017 __isl_give isl_map
*isl_map_oppose(__isl_take isl_map
*map
,
13018 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13020 isl_basic_map
*bmap
= NULL
;
13024 if (isl_map_check_range(map
, type1
, pos1
, 1) < 0)
13025 return isl_map_free(map
);
13026 if (isl_map_check_range(map
, type2
, pos2
, 1) < 0)
13027 return isl_map_free(map
);
13029 total
= isl_map_dim(map
, isl_dim_all
);
13031 return isl_map_free(map
);
13032 bmap
= isl_basic_map_alloc_space(isl_map_get_space(map
), 0, 1, 0);
13033 i
= isl_basic_map_alloc_equality(bmap
);
13036 isl_seq_clr(bmap
->eq
[i
], 1 + total
);
13037 pos1
+= isl_basic_map_offset(bmap
, type1
);
13038 pos2
+= isl_basic_map_offset(bmap
, type2
);
13039 isl_int_set_si(bmap
->eq
[i
][pos1
], 1);
13040 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
13041 bmap
= isl_basic_map_finalize(bmap
);
13043 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
13047 isl_basic_map_free(bmap
);
13052 /* Construct a constraint imposing that the value of the first dimension is
13053 * greater than or equal to that of the second.
13055 static __isl_give isl_constraint
*constraint_order_ge(
13056 __isl_take isl_space
*space
, enum isl_dim_type type1
, int pos1
,
13057 enum isl_dim_type type2
, int pos2
)
13061 if (isl_space_check_range(space
, type1
, pos1
, 1) < 0 ||
13062 isl_space_check_range(space
, type2
, pos2
, 1) < 0)
13063 space
= isl_space_free(space
);
13067 c
= isl_constraint_alloc_inequality(isl_local_space_from_space(space
));
13069 if (type1
== type2
&& pos1
== pos2
)
13072 c
= isl_constraint_set_coefficient_si(c
, type1
, pos1
, 1);
13073 c
= isl_constraint_set_coefficient_si(c
, type2
, pos2
, -1);
13078 /* Add a constraint imposing that the value of the first dimension is
13079 * greater than or equal to that of the second.
13081 __isl_give isl_basic_map
*isl_basic_map_order_ge(__isl_take isl_basic_map
*bmap
,
13082 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13087 if (type1
== type2
&& pos1
== pos2
)
13089 space
= isl_basic_map_get_space(bmap
);
13090 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
13091 bmap
= isl_basic_map_add_constraint(bmap
, c
);
13096 /* Add a constraint imposing that the value of the first dimension is
13097 * greater than or equal to that of the second.
13099 __isl_give isl_map
*isl_map_order_ge(__isl_take isl_map
*map
,
13100 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13105 if (type1
== type2
&& pos1
== pos2
)
13107 space
= isl_map_get_space(map
);
13108 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
13109 map
= isl_map_add_constraint(map
, c
);
13114 /* Add a constraint imposing that the value of the first dimension is
13115 * less than or equal to that of the second.
13117 __isl_give isl_map
*isl_map_order_le(__isl_take isl_map
*map
,
13118 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13120 return isl_map_order_ge(map
, type2
, pos2
, type1
, pos1
);
13123 /* Construct a basic map where the value of the first dimension is
13124 * greater than that of the second.
13126 static __isl_give isl_basic_map
*greator(__isl_take isl_space
*space
,
13127 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13129 isl_basic_map
*bmap
= NULL
;
13133 if (isl_space_check_range(space
, type1
, pos1
, 1) < 0 ||
13134 isl_space_check_range(space
, type2
, pos2
, 1) < 0)
13137 if (type1
== type2
&& pos1
== pos2
)
13138 return isl_basic_map_empty(space
);
13140 bmap
= isl_basic_map_alloc_space(space
, 0, 0, 1);
13141 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
13142 i
= isl_basic_map_alloc_inequality(bmap
);
13143 if (total
< 0 || i
< 0)
13144 return isl_basic_map_free(bmap
);
13145 isl_seq_clr(bmap
->ineq
[i
], 1 + total
);
13146 pos1
+= isl_basic_map_offset(bmap
, type1
);
13147 pos2
+= isl_basic_map_offset(bmap
, type2
);
13148 isl_int_set_si(bmap
->ineq
[i
][pos1
], 1);
13149 isl_int_set_si(bmap
->ineq
[i
][pos2
], -1);
13150 isl_int_set_si(bmap
->ineq
[i
][0], -1);
13151 bmap
= isl_basic_map_finalize(bmap
);
13155 isl_space_free(space
);
13156 isl_basic_map_free(bmap
);
13160 /* Add a constraint imposing that the value of the first dimension is
13161 * greater than that of the second.
13163 __isl_give isl_basic_map
*isl_basic_map_order_gt(__isl_take isl_basic_map
*bmap
,
13164 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13168 gt
= greator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
13170 bmap
= isl_basic_map_intersect(bmap
, gt
);
13175 /* Add a constraint imposing that the value of the first dimension is
13176 * greater than that of the second.
13178 __isl_give isl_map
*isl_map_order_gt(__isl_take isl_map
*map
,
13179 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13181 isl_basic_map
*bmap
;
13183 bmap
= greator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
13185 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
13190 /* Add a constraint imposing that the value of the first dimension is
13191 * smaller than that of the second.
13193 __isl_give isl_map
*isl_map_order_lt(__isl_take isl_map
*map
,
13194 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13196 return isl_map_order_gt(map
, type2
, pos2
, type1
, pos1
);
13199 __isl_give isl_aff
*isl_basic_map_get_div(__isl_keep isl_basic_map
*bmap
,
13203 isl_local_space
*ls
;
13208 if (!isl_basic_map_divs_known(bmap
))
13209 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
13210 "some divs are unknown", return NULL
);
13212 ls
= isl_basic_map_get_local_space(bmap
);
13213 div
= isl_local_space_get_div(ls
, pos
);
13214 isl_local_space_free(ls
);
13219 __isl_give isl_aff
*isl_basic_set_get_div(__isl_keep isl_basic_set
*bset
,
13222 return isl_basic_map_get_div(bset
, pos
);
13225 /* Plug in "subs" for dimension "type", "pos" of "bset".
13227 * Let i be the dimension to replace and let "subs" be of the form
13231 * Any integer division with a non-zero coefficient for i,
13233 * floor((a i + g)/m)
13237 * floor((a f + d g)/(m d))
13239 * Constraints of the form
13247 * We currently require that "subs" is an integral expression.
13248 * Handling rational expressions may require us to add stride constraints
13249 * as we do in isl_basic_set_preimage_multi_aff.
13251 __isl_give isl_basic_set
*isl_basic_set_substitute(
13252 __isl_take isl_basic_set
*bset
,
13253 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
13260 if (bset
&& isl_basic_set_plain_is_empty(bset
))
13263 bset
= isl_basic_set_cow(bset
);
13264 if (!bset
|| !subs
)
13267 ctx
= isl_basic_set_get_ctx(bset
);
13268 if (!isl_space_is_equal(bset
->dim
, subs
->ls
->dim
))
13269 isl_die(ctx
, isl_error_invalid
,
13270 "spaces don't match", goto error
);
13271 n_div
= isl_local_space_dim(subs
->ls
, isl_dim_div
);
13275 isl_die(ctx
, isl_error_unsupported
,
13276 "cannot handle divs yet", goto error
);
13277 if (!isl_int_is_one(subs
->v
->el
[0]))
13278 isl_die(ctx
, isl_error_invalid
,
13279 "can only substitute integer expressions", goto error
);
13281 pos
+= isl_basic_set_offset(bset
, type
);
13285 for (i
= 0; i
< bset
->n_eq
; ++i
) {
13286 if (isl_int_is_zero(bset
->eq
[i
][pos
]))
13288 isl_int_set(v
, bset
->eq
[i
][pos
]);
13289 isl_int_set_si(bset
->eq
[i
][pos
], 0);
13290 isl_seq_combine(bset
->eq
[i
], subs
->v
->el
[0], bset
->eq
[i
],
13291 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
13294 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
13295 if (isl_int_is_zero(bset
->ineq
[i
][pos
]))
13297 isl_int_set(v
, bset
->ineq
[i
][pos
]);
13298 isl_int_set_si(bset
->ineq
[i
][pos
], 0);
13299 isl_seq_combine(bset
->ineq
[i
], subs
->v
->el
[0], bset
->ineq
[i
],
13300 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
13303 for (i
= 0; i
< bset
->n_div
; ++i
) {
13304 if (isl_int_is_zero(bset
->div
[i
][1 + pos
]))
13306 isl_int_set(v
, bset
->div
[i
][1 + pos
]);
13307 isl_int_set_si(bset
->div
[i
][1 + pos
], 0);
13308 isl_seq_combine(bset
->div
[i
] + 1,
13309 subs
->v
->el
[0], bset
->div
[i
] + 1,
13310 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
13311 isl_int_mul(bset
->div
[i
][0], bset
->div
[i
][0], subs
->v
->el
[0]);
13316 bset
= isl_basic_set_simplify(bset
);
13317 return isl_basic_set_finalize(bset
);
13319 isl_basic_set_free(bset
);
13323 /* Plug in "subs" for dimension "type", "pos" of "set".
13325 __isl_give isl_set
*isl_set_substitute(__isl_take isl_set
*set
,
13326 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
13330 if (set
&& isl_set_plain_is_empty(set
))
13333 set
= isl_set_cow(set
);
13337 for (i
= set
->n
- 1; i
>= 0; --i
) {
13338 set
->p
[i
] = isl_basic_set_substitute(set
->p
[i
], type
, pos
, subs
);
13339 set
= set_from_map(remove_if_empty(set_to_map(set
), i
));
13350 /* Check if the range of "ma" is compatible with the domain or range
13351 * (depending on "type") of "bmap".
13353 static isl_stat
check_basic_map_compatible_range_multi_aff(
13354 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type type
,
13355 __isl_keep isl_multi_aff
*ma
)
13358 isl_space
*ma_space
;
13360 ma_space
= isl_multi_aff_get_space(ma
);
13362 m
= isl_space_has_equal_params(bmap
->dim
, ma_space
);
13366 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
13367 "parameters don't match", goto error
);
13368 m
= isl_space_tuple_is_equal(bmap
->dim
, type
, ma_space
, isl_dim_out
);
13372 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
13373 "spaces don't match", goto error
);
13375 isl_space_free(ma_space
);
13376 return isl_stat_ok
;
13378 isl_space_free(ma_space
);
13379 return isl_stat_error
;
13382 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
13383 * coefficients before the transformed range of dimensions,
13384 * the "n_after" coefficients after the transformed range of dimensions
13385 * and the coefficients of the other divs in "bmap".
13387 static __isl_give isl_basic_map
*set_ma_divs(__isl_take isl_basic_map
*bmap
,
13388 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
, int n_div
)
13393 isl_local_space
*ls
;
13398 ls
= isl_aff_get_domain_local_space(ma
->u
.p
[0]);
13399 n_param
= isl_local_space_dim(ls
, isl_dim_param
);
13400 n_set
= isl_local_space_dim(ls
, isl_dim_set
);
13401 if (n_param
< 0 || n_set
< 0)
13402 return isl_basic_map_free(bmap
);
13404 for (i
= 0; i
< n_div
; ++i
) {
13405 int o_bmap
= 0, o_ls
= 0;
13407 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], 1 + 1 + n_param
);
13408 o_bmap
+= 1 + 1 + n_param
;
13409 o_ls
+= 1 + 1 + n_param
;
13410 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_before
);
13411 o_bmap
+= n_before
;
13412 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
13413 ls
->div
->row
[i
] + o_ls
, n_set
);
13416 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_after
);
13418 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
13419 ls
->div
->row
[i
] + o_ls
, n_div
);
13422 isl_seq_clr(bmap
->div
[i
] + o_bmap
, bmap
->n_div
- n_div
);
13423 bmap
= isl_basic_map_add_div_constraints(bmap
, i
);
13428 isl_local_space_free(ls
);
13431 isl_local_space_free(ls
);
13432 return isl_basic_map_free(bmap
);
13435 /* How many stride constraints does "ma" enforce?
13436 * That is, how many of the affine expressions have a denominator
13437 * different from one?
13439 static int multi_aff_strides(__isl_keep isl_multi_aff
*ma
)
13444 for (i
= 0; i
< ma
->n
; ++i
)
13445 if (!isl_int_is_one(ma
->u
.p
[i
]->v
->el
[0]))
13451 /* For each affine expression in ma of the form
13453 * x_i = (f_i y + h_i)/m_i
13455 * with m_i different from one, add a constraint to "bmap"
13458 * f_i y + h_i = m_i alpha_i
13460 * with alpha_i an additional existentially quantified variable.
13462 * The input variables of "ma" correspond to a subset of the variables
13463 * of "bmap". There are "n_before" variables in "bmap" before this
13464 * subset and "n_after" variables after this subset.
13465 * The integer divisions of the affine expressions in "ma" are assumed
13466 * to have been aligned. There are "n_div_ma" of them and
13467 * they appear first in "bmap", straight after the "n_after" variables.
13469 static __isl_give isl_basic_map
*add_ma_strides(
13470 __isl_take isl_basic_map
*bmap
, __isl_keep isl_multi_aff
*ma
,
13471 int n_before
, int n_after
, int n_div_ma
)
13479 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
13480 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
13481 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
13482 if (total
< 0 || n_param
< 0 || n_in
< 0)
13483 return isl_basic_map_free(bmap
);
13484 for (i
= 0; i
< ma
->n
; ++i
) {
13485 int o_bmap
= 0, o_ma
= 1;
13487 if (isl_int_is_one(ma
->u
.p
[i
]->v
->el
[0]))
13489 div
= isl_basic_map_alloc_div(bmap
);
13490 k
= isl_basic_map_alloc_equality(bmap
);
13491 if (div
< 0 || k
< 0)
13493 isl_int_set_si(bmap
->div
[div
][0], 0);
13494 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
13495 ma
->u
.p
[i
]->v
->el
+ o_ma
, 1 + n_param
);
13496 o_bmap
+= 1 + n_param
;
13497 o_ma
+= 1 + n_param
;
13498 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_before
);
13499 o_bmap
+= n_before
;
13500 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
13501 ma
->u
.p
[i
]->v
->el
+ o_ma
, n_in
);
13504 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_after
);
13506 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
13507 ma
->u
.p
[i
]->v
->el
+ o_ma
, n_div_ma
);
13508 o_bmap
+= n_div_ma
;
13510 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, 1 + total
- o_bmap
);
13511 isl_int_neg(bmap
->eq
[k
][1 + total
], ma
->u
.p
[i
]->v
->el
[0]);
13517 isl_basic_map_free(bmap
);
13521 /* Replace the domain or range space (depending on "type) of "space" by "set".
13523 static __isl_give isl_space
*isl_space_set(__isl_take isl_space
*space
,
13524 enum isl_dim_type type
, __isl_take isl_space
*set
)
13526 if (type
== isl_dim_in
) {
13527 space
= isl_space_range(space
);
13528 space
= isl_space_map_from_domain_and_range(set
, space
);
13530 space
= isl_space_domain(space
);
13531 space
= isl_space_map_from_domain_and_range(space
, set
);
13537 /* Compute the preimage of the domain or range (depending on "type")
13538 * of "bmap" under the function represented by "ma".
13539 * In other words, plug in "ma" in the domain or range of "bmap".
13540 * The result is a basic map that lives in the same space as "bmap"
13541 * except that the domain or range has been replaced by
13542 * the domain space of "ma".
13544 * If bmap is represented by
13546 * A(p) + S u + B x + T v + C(divs) >= 0,
13548 * where u and x are input and output dimensions if type == isl_dim_out
13549 * while x and v are input and output dimensions if type == isl_dim_in,
13550 * and ma is represented by
13552 * x = D(p) + F(y) + G(divs')
13554 * then the result is
13556 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
13558 * The divs in the input set are similarly adjusted.
13561 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
13565 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
13566 * B_i G(divs') + c_i(divs))/n_i)
13568 * If bmap is not a rational map and if F(y) involves any denominators
13570 * x_i = (f_i y + h_i)/m_i
13572 * then additional constraints are added to ensure that we only
13573 * map back integer points. That is we enforce
13575 * f_i y + h_i = m_i alpha_i
13577 * with alpha_i an additional existentially quantified variable.
13579 * We first copy over the divs from "ma".
13580 * Then we add the modified constraints and divs from "bmap".
13581 * Finally, we add the stride constraints, if needed.
13583 __isl_give isl_basic_map
*isl_basic_map_preimage_multi_aff(
13584 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
13585 __isl_take isl_multi_aff
*ma
)
13589 isl_basic_map
*res
= NULL
;
13590 isl_size n_before
, n_after
, n_div_bmap
, n_div_ma
;
13591 isl_int f
, c1
, c2
, g
;
13600 ma
= isl_multi_aff_align_divs(ma
);
13603 if (check_basic_map_compatible_range_multi_aff(bmap
, type
, ma
) < 0)
13606 if (type
== isl_dim_in
) {
13608 n_after
= isl_basic_map_dim(bmap
, isl_dim_out
);
13610 n_before
= isl_basic_map_dim(bmap
, isl_dim_in
);
13613 n_div_bmap
= isl_basic_map_dim(bmap
, isl_dim_div
);
13614 n_div_ma
= ma
->n
? isl_aff_dim(ma
->u
.p
[0], isl_dim_div
) : 0;
13615 if (n_before
< 0 || n_after
< 0 || n_div_bmap
< 0 || n_div_ma
< 0)
13618 space
= isl_multi_aff_get_domain_space(ma
);
13619 space
= isl_space_set(isl_basic_map_get_space(bmap
), type
, space
);
13620 rational
= isl_basic_map_is_rational(bmap
);
13621 strides
= rational
? 0 : multi_aff_strides(ma
);
13622 res
= isl_basic_map_alloc_space(space
, n_div_ma
+ n_div_bmap
+ strides
,
13623 bmap
->n_eq
+ strides
, bmap
->n_ineq
+ 2 * n_div_ma
);
13625 res
= isl_basic_map_set_rational(res
);
13627 for (i
= 0; i
< n_div_ma
+ n_div_bmap
; ++i
)
13628 if (isl_basic_map_alloc_div(res
) < 0)
13631 res
= set_ma_divs(res
, ma
, n_before
, n_after
, n_div_ma
);
13635 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
13636 k
= isl_basic_map_alloc_equality(res
);
13639 if (isl_seq_preimage(res
->eq
[k
], bmap
->eq
[i
], ma
, n_before
,
13640 n_after
, n_div_ma
, n_div_bmap
,
13641 f
, c1
, c2
, g
, 0) < 0)
13645 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
13646 k
= isl_basic_map_alloc_inequality(res
);
13649 if (isl_seq_preimage(res
->ineq
[k
], bmap
->ineq
[i
], ma
, n_before
,
13650 n_after
, n_div_ma
, n_div_bmap
,
13651 f
, c1
, c2
, g
, 0) < 0)
13655 for (i
= 0; i
< bmap
->n_div
; ++i
) {
13656 if (isl_int_is_zero(bmap
->div
[i
][0])) {
13657 isl_int_set_si(res
->div
[n_div_ma
+ i
][0], 0);
13660 if (isl_seq_preimage(res
->div
[n_div_ma
+ i
], bmap
->div
[i
], ma
,
13661 n_before
, n_after
, n_div_ma
, n_div_bmap
,
13662 f
, c1
, c2
, g
, 1) < 0)
13667 res
= add_ma_strides(res
, ma
, n_before
, n_after
, n_div_ma
);
13673 isl_basic_map_free(bmap
);
13674 isl_multi_aff_free(ma
);
13675 res
= isl_basic_map_simplify(res
);
13676 return isl_basic_map_finalize(res
);
13682 isl_basic_map_free(bmap
);
13683 isl_multi_aff_free(ma
);
13684 isl_basic_map_free(res
);
13688 /* Compute the preimage of "bset" under the function represented by "ma".
13689 * In other words, plug in "ma" in "bset". The result is a basic set
13690 * that lives in the domain space of "ma".
13692 __isl_give isl_basic_set
*isl_basic_set_preimage_multi_aff(
13693 __isl_take isl_basic_set
*bset
, __isl_take isl_multi_aff
*ma
)
13695 return isl_basic_map_preimage_multi_aff(bset
, isl_dim_set
, ma
);
13698 /* Compute the preimage of the domain of "bmap" under the function
13699 * represented by "ma".
13700 * In other words, plug in "ma" in the domain of "bmap".
13701 * The result is a basic map that lives in the same space as "bmap"
13702 * except that the domain has been replaced by the domain space of "ma".
13704 __isl_give isl_basic_map
*isl_basic_map_preimage_domain_multi_aff(
13705 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
13707 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_in
, ma
);
13710 /* Compute the preimage of the range of "bmap" under the function
13711 * represented by "ma".
13712 * In other words, plug in "ma" in the range of "bmap".
13713 * The result is a basic map that lives in the same space as "bmap"
13714 * except that the range has been replaced by the domain space of "ma".
13716 __isl_give isl_basic_map
*isl_basic_map_preimage_range_multi_aff(
13717 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
13719 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_out
, ma
);
13722 /* Check if the range of "ma" is compatible with the domain or range
13723 * (depending on "type") of "map".
13724 * Return isl_stat_error if anything is wrong.
13726 static isl_stat
check_map_compatible_range_multi_aff(
13727 __isl_keep isl_map
*map
, enum isl_dim_type type
,
13728 __isl_keep isl_multi_aff
*ma
)
13731 isl_space
*ma_space
;
13733 ma_space
= isl_multi_aff_get_space(ma
);
13734 m
= isl_space_tuple_is_equal(map
->dim
, type
, ma_space
, isl_dim_out
);
13735 isl_space_free(ma_space
);
13737 return isl_stat_error
;
13739 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
13740 "spaces don't match", return isl_stat_error
);
13741 return isl_stat_ok
;
13744 /* Compute the preimage of the domain or range (depending on "type")
13745 * of "map" under the function represented by "ma".
13746 * In other words, plug in "ma" in the domain or range of "map".
13747 * The result is a map that lives in the same space as "map"
13748 * except that the domain or range has been replaced by
13749 * the domain space of "ma".
13751 * The parameters are assumed to have been aligned.
13753 static __isl_give isl_map
*map_preimage_multi_aff(__isl_take isl_map
*map
,
13754 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
13759 map
= isl_map_cow(map
);
13760 ma
= isl_multi_aff_align_divs(ma
);
13763 if (check_map_compatible_range_multi_aff(map
, type
, ma
) < 0)
13766 for (i
= 0; i
< map
->n
; ++i
) {
13767 map
->p
[i
] = isl_basic_map_preimage_multi_aff(map
->p
[i
], type
,
13768 isl_multi_aff_copy(ma
));
13773 space
= isl_multi_aff_get_domain_space(ma
);
13774 space
= isl_space_set(isl_map_get_space(map
), type
, space
);
13776 isl_space_free(isl_map_take_space(map
));
13777 map
= isl_map_restore_space(map
, space
);
13781 isl_multi_aff_free(ma
);
13783 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
13784 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
13787 isl_multi_aff_free(ma
);
13792 /* Compute the preimage of the domain or range (depending on "type")
13793 * of "map" under the function represented by "ma".
13794 * In other words, plug in "ma" in the domain or range of "map".
13795 * The result is a map that lives in the same space as "map"
13796 * except that the domain or range has been replaced by
13797 * the domain space of "ma".
13799 __isl_give isl_map
*isl_map_preimage_multi_aff(__isl_take isl_map
*map
,
13800 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
13807 aligned
= isl_map_space_has_equal_params(map
, ma
->space
);
13811 return map_preimage_multi_aff(map
, type
, ma
);
13813 if (isl_map_check_named_params(map
) < 0)
13815 if (!isl_space_has_named_params(ma
->space
))
13816 isl_die(map
->ctx
, isl_error_invalid
,
13817 "unaligned unnamed parameters", goto error
);
13818 map
= isl_map_align_params(map
, isl_multi_aff_get_space(ma
));
13819 ma
= isl_multi_aff_align_params(ma
, isl_map_get_space(map
));
13821 return map_preimage_multi_aff(map
, type
, ma
);
13823 isl_multi_aff_free(ma
);
13824 return isl_map_free(map
);
13827 /* Compute the preimage of "set" under the function represented by "ma".
13828 * In other words, plug in "ma" in "set". The result is a set
13829 * that lives in the domain space of "ma".
13831 __isl_give isl_set
*isl_set_preimage_multi_aff(__isl_take isl_set
*set
,
13832 __isl_take isl_multi_aff
*ma
)
13834 return isl_map_preimage_multi_aff(set
, isl_dim_set
, ma
);
13837 /* Compute the preimage of the domain of "map" under the function
13838 * represented by "ma".
13839 * In other words, plug in "ma" in the domain of "map".
13840 * The result is a map that lives in the same space as "map"
13841 * except that the domain has been replaced by the domain space of "ma".
13843 __isl_give isl_map
*isl_map_preimage_domain_multi_aff(__isl_take isl_map
*map
,
13844 __isl_take isl_multi_aff
*ma
)
13846 return isl_map_preimage_multi_aff(map
, isl_dim_in
, ma
);
13849 /* Compute the preimage of the range of "map" under the function
13850 * represented by "ma".
13851 * In other words, plug in "ma" in the range of "map".
13852 * The result is a map that lives in the same space as "map"
13853 * except that the range has been replaced by the domain space of "ma".
13855 __isl_give isl_map
*isl_map_preimage_range_multi_aff(__isl_take isl_map
*map
,
13856 __isl_take isl_multi_aff
*ma
)
13858 return isl_map_preimage_multi_aff(map
, isl_dim_out
, ma
);
13861 /* Compute the preimage of "map" under the function represented by "pma".
13862 * In other words, plug in "pma" in the domain or range of "map".
13863 * The result is a map that lives in the same space as "map",
13864 * except that the space of type "type" has been replaced by
13865 * the domain space of "pma".
13867 * The parameters of "map" and "pma" are assumed to have been aligned.
13869 static __isl_give isl_map
*isl_map_preimage_pw_multi_aff_aligned(
13870 __isl_take isl_map
*map
, enum isl_dim_type type
,
13871 __isl_take isl_pw_multi_aff
*pma
)
13880 isl_pw_multi_aff_free(pma
);
13881 res
= isl_map_empty(isl_map_get_space(map
));
13886 res
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
13887 isl_multi_aff_copy(pma
->p
[0].maff
));
13888 if (type
== isl_dim_in
)
13889 res
= isl_map_intersect_domain(res
,
13890 isl_map_copy(pma
->p
[0].set
));
13892 res
= isl_map_intersect_range(res
,
13893 isl_map_copy(pma
->p
[0].set
));
13895 for (i
= 1; i
< pma
->n
; ++i
) {
13898 res_i
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
13899 isl_multi_aff_copy(pma
->p
[i
].maff
));
13900 if (type
== isl_dim_in
)
13901 res_i
= isl_map_intersect_domain(res_i
,
13902 isl_map_copy(pma
->p
[i
].set
));
13904 res_i
= isl_map_intersect_range(res_i
,
13905 isl_map_copy(pma
->p
[i
].set
));
13906 res
= isl_map_union(res
, res_i
);
13909 isl_pw_multi_aff_free(pma
);
13913 isl_pw_multi_aff_free(pma
);
13918 /* Compute the preimage of "map" under the function represented by "pma".
13919 * In other words, plug in "pma" in the domain or range of "map".
13920 * The result is a map that lives in the same space as "map",
13921 * except that the space of type "type" has been replaced by
13922 * the domain space of "pma".
13924 __isl_give isl_map
*isl_map_preimage_pw_multi_aff(__isl_take isl_map
*map
,
13925 enum isl_dim_type type
, __isl_take isl_pw_multi_aff
*pma
)
13932 aligned
= isl_map_space_has_equal_params(map
, pma
->dim
);
13936 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
13938 if (isl_map_check_named_params(map
) < 0)
13940 if (isl_pw_multi_aff_check_named_params(pma
) < 0)
13942 map
= isl_map_align_params(map
, isl_pw_multi_aff_get_space(pma
));
13943 pma
= isl_pw_multi_aff_align_params(pma
, isl_map_get_space(map
));
13945 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
13947 isl_pw_multi_aff_free(pma
);
13948 return isl_map_free(map
);
13951 /* Compute the preimage of "set" under the function represented by "pma".
13952 * In other words, plug in "pma" in "set". The result is a set
13953 * that lives in the domain space of "pma".
13955 __isl_give isl_set
*isl_set_preimage_pw_multi_aff(__isl_take isl_set
*set
,
13956 __isl_take isl_pw_multi_aff
*pma
)
13958 return isl_map_preimage_pw_multi_aff(set
, isl_dim_set
, pma
);
13961 /* Compute the preimage of the domain of "map" under the function
13962 * represented by "pma".
13963 * In other words, plug in "pma" in the domain of "map".
13964 * The result is a map that lives in the same space as "map",
13965 * except that domain space has been replaced by the domain space of "pma".
13967 __isl_give isl_map
*isl_map_preimage_domain_pw_multi_aff(
13968 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
13970 return isl_map_preimage_pw_multi_aff(map
, isl_dim_in
, pma
);
13973 /* Compute the preimage of the range of "map" under the function
13974 * represented by "pma".
13975 * In other words, plug in "pma" in the range of "map".
13976 * The result is a map that lives in the same space as "map",
13977 * except that range space has been replaced by the domain space of "pma".
13979 __isl_give isl_map
*isl_map_preimage_range_pw_multi_aff(
13980 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
13982 return isl_map_preimage_pw_multi_aff(map
, isl_dim_out
, pma
);
13985 /* Compute the preimage of "map" under the function represented by "mpa".
13986 * In other words, plug in "mpa" in the domain or range of "map".
13987 * The result is a map that lives in the same space as "map",
13988 * except that the space of type "type" has been replaced by
13989 * the domain space of "mpa".
13991 * If the map does not involve any constraints that refer to the
13992 * dimensions of the substituted space, then the only possible
13993 * effect of "mpa" on the map is to map the space to a different space.
13994 * We create a separate isl_multi_aff to effectuate this change
13995 * in order to avoid spurious splitting of the map along the pieces
13997 * If "mpa" has a non-trivial explicit domain, however,
13998 * then the full substitution should be performed.
14000 __isl_give isl_map
*isl_map_preimage_multi_pw_aff(__isl_take isl_map
*map
,
14001 enum isl_dim_type type
, __isl_take isl_multi_pw_aff
*mpa
)
14005 isl_pw_multi_aff
*pma
;
14007 n
= isl_map_dim(map
, type
);
14011 full
= isl_map_involves_dims(map
, type
, 0, n
);
14012 if (full
>= 0 && !full
)
14013 full
= isl_multi_pw_aff_has_non_trivial_domain(mpa
);
14020 space
= isl_multi_pw_aff_get_space(mpa
);
14021 isl_multi_pw_aff_free(mpa
);
14022 ma
= isl_multi_aff_zero(space
);
14023 return isl_map_preimage_multi_aff(map
, type
, ma
);
14026 pma
= isl_pw_multi_aff_from_multi_pw_aff(mpa
);
14027 return isl_map_preimage_pw_multi_aff(map
, type
, pma
);
14030 isl_multi_pw_aff_free(mpa
);
14034 /* Compute the preimage of "map" under the function represented by "mpa".
14035 * In other words, plug in "mpa" in the domain "map".
14036 * The result is a map that lives in the same space as "map",
14037 * except that domain space has been replaced by the domain space of "mpa".
14039 __isl_give isl_map
*isl_map_preimage_domain_multi_pw_aff(
14040 __isl_take isl_map
*map
, __isl_take isl_multi_pw_aff
*mpa
)
14042 return isl_map_preimage_multi_pw_aff(map
, isl_dim_in
, mpa
);
14045 /* Compute the preimage of "set" by the function represented by "mpa".
14046 * In other words, plug in "mpa" in "set".
14048 __isl_give isl_set
*isl_set_preimage_multi_pw_aff(__isl_take isl_set
*set
,
14049 __isl_take isl_multi_pw_aff
*mpa
)
14051 return isl_map_preimage_multi_pw_aff(set
, isl_dim_set
, mpa
);
14054 /* Return a copy of the equality constraints of "bset" as a matrix.
14056 __isl_give isl_mat
*isl_basic_set_extract_equalities(
14057 __isl_keep isl_basic_set
*bset
)
14062 total
= isl_basic_set_dim(bset
, isl_dim_all
);
14066 ctx
= isl_basic_set_get_ctx(bset
);
14067 return isl_mat_sub_alloc6(ctx
, bset
->eq
, 0, bset
->n_eq
, 0, 1 + total
);
14070 /* Are the "n" "coefficients" starting at "first" of the integer division
14071 * expressions at position "pos1" in "bmap1" and "pos2" in "bmap2" equal
14073 * The "coefficient" at position 0 is the denominator.
14074 * The "coefficient" at position 1 is the constant term.
14076 isl_bool
isl_basic_map_equal_div_expr_part(__isl_keep isl_basic_map
*bmap1
,
14077 int pos1
, __isl_keep isl_basic_map
*bmap2
, int pos2
,
14078 unsigned first
, unsigned n
)
14080 if (isl_basic_map_check_range(bmap1
, isl_dim_div
, pos1
, 1) < 0)
14081 return isl_bool_error
;
14082 if (isl_basic_map_check_range(bmap2
, isl_dim_div
, pos2
, 1) < 0)
14083 return isl_bool_error
;
14084 return isl_seq_eq(bmap1
->div
[pos1
] + first
,
14085 bmap2
->div
[pos2
] + first
, n
);
14088 /* Are the integer division expressions at position "pos1" in "bmap1" and
14089 * "pos2" in "bmap2" equal to each other, except that the constant terms
14092 isl_bool
isl_basic_map_equal_div_expr_except_constant(
14093 __isl_keep isl_basic_map
*bmap1
, int pos1
,
14094 __isl_keep isl_basic_map
*bmap2
, int pos2
)
14097 isl_size total
, total2
;
14099 total
= isl_basic_map_dim(bmap1
, isl_dim_all
);
14100 total2
= isl_basic_map_dim(bmap2
, isl_dim_all
);
14101 if (total
< 0 || total2
< 0)
14102 return isl_bool_error
;
14103 if (total
!= total2
)
14104 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
14105 "incomparable div expressions", return isl_bool_error
);
14106 equal
= isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
14108 if (equal
< 0 || !equal
)
14110 equal
= isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
14112 if (equal
< 0 || equal
)
14113 return isl_bool_not(equal
);
14114 return isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
14118 /* Replace the numerator of the constant term of the integer division
14119 * expression at position "div" in "bmap" by "value".
14120 * The caller guarantees that this does not change the meaning
14123 __isl_give isl_basic_map
*isl_basic_map_set_div_expr_constant_num_si_inplace(
14124 __isl_take isl_basic_map
*bmap
, int div
, int value
)
14126 if (isl_basic_map_check_range(bmap
, isl_dim_div
, div
, 1) < 0)
14127 return isl_basic_map_free(bmap
);
14129 isl_int_set_si(bmap
->div
[div
][1], value
);
14134 /* Is the point "inner" internal to inequality constraint "ineq"
14136 * The point is considered to be internal to the inequality constraint,
14137 * if it strictly lies on the positive side of the inequality constraint,
14138 * or if it lies on the constraint and the constraint is lexico-positive.
14140 static isl_bool
is_internal(__isl_keep isl_vec
*inner
,
14141 __isl_keep isl_basic_set
*bset
, int ineq
)
14147 if (!inner
|| !bset
)
14148 return isl_bool_error
;
14150 ctx
= isl_basic_set_get_ctx(bset
);
14151 isl_seq_inner_product(inner
->el
, bset
->ineq
[ineq
], inner
->size
,
14152 &ctx
->normalize_gcd
);
14153 if (!isl_int_is_zero(ctx
->normalize_gcd
))
14154 return isl_int_is_nonneg(ctx
->normalize_gcd
);
14156 total
= isl_basic_set_dim(bset
, isl_dim_all
);
14158 return isl_bool_error
;
14159 pos
= isl_seq_first_non_zero(bset
->ineq
[ineq
] + 1, total
);
14160 return isl_int_is_pos(bset
->ineq
[ineq
][1 + pos
]);
14163 /* Tighten the inequality constraints of "bset" that are outward with respect
14164 * to the point "vec".
14165 * That is, tighten the constraints that are not satisfied by "vec".
14167 * "vec" is a point internal to some superset S of "bset" that is used
14168 * to make the subsets of S disjoint, by tightening one half of the constraints
14169 * that separate two subsets. In particular, the constraints of S
14170 * are all satisfied by "vec" and should not be tightened.
14171 * Of the internal constraints, those that have "vec" on the outside
14172 * are tightened. The shared facet is included in the adjacent subset
14173 * with the opposite constraint.
14174 * For constraints that saturate "vec", this criterion cannot be used
14175 * to determine which of the two sides should be tightened.
14176 * Instead, the sign of the first non-zero coefficient is used
14177 * to make this choice. Note that this second criterion is never used
14178 * on the constraints of S since "vec" is interior to "S".
14180 __isl_give isl_basic_set
*isl_basic_set_tighten_outward(
14181 __isl_take isl_basic_set
*bset
, __isl_keep isl_vec
*vec
)
14185 bset
= isl_basic_set_cow(bset
);
14188 for (j
= 0; j
< bset
->n_ineq
; ++j
) {
14191 internal
= is_internal(vec
, bset
, j
);
14193 return isl_basic_set_free(bset
);
14196 isl_int_sub_ui(bset
->ineq
[j
][0], bset
->ineq
[j
][0], 1);
14202 /* Replace the variables x of type "type" starting at "first" in "bmap"
14203 * by x' with x = M x' with M the matrix trans.
14204 * That is, replace the corresponding coefficients c by c M.
14206 * The transformation matrix should be a square matrix.
14208 __isl_give isl_basic_map
*isl_basic_map_transform_dims(
14209 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
, unsigned first
,
14210 __isl_take isl_mat
*trans
)
14214 bmap
= isl_basic_map_cow(bmap
);
14215 if (!bmap
|| !trans
)
14218 if (trans
->n_row
!= trans
->n_col
)
14219 isl_die(trans
->ctx
, isl_error_invalid
,
14220 "expecting square transformation matrix", goto error
);
14221 if (isl_basic_map_check_range(bmap
, type
, first
, trans
->n_row
) < 0)
14224 pos
= isl_basic_map_offset(bmap
, type
) + first
;
14226 if (isl_mat_sub_transform(bmap
->eq
, bmap
->n_eq
, pos
,
14227 isl_mat_copy(trans
)) < 0)
14229 if (isl_mat_sub_transform(bmap
->ineq
, bmap
->n_ineq
, pos
,
14230 isl_mat_copy(trans
)) < 0)
14232 if (isl_mat_sub_transform(bmap
->div
, bmap
->n_div
, 1 + pos
,
14233 isl_mat_copy(trans
)) < 0)
14236 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
14237 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
14239 isl_mat_free(trans
);
14242 isl_mat_free(trans
);
14243 isl_basic_map_free(bmap
);
14247 /* Replace the variables x of type "type" starting at "first" in "bset"
14248 * by x' with x = M x' with M the matrix trans.
14249 * That is, replace the corresponding coefficients c by c M.
14251 * The transformation matrix should be a square matrix.
14253 __isl_give isl_basic_set
*isl_basic_set_transform_dims(
14254 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned first
,
14255 __isl_take isl_mat
*trans
)
14257 return isl_basic_map_transform_dims(bset
, type
, first
, trans
);