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 /* Is the tuple of type "type" of "bmap" the same as the single tuple of "bset"?
293 static isl_bool
isl_basic_map_set_tuple_is_equal(__isl_keep isl_basic_map
*bmap
,
294 enum isl_dim_type type
, __isl_keep isl_basic_set
*bset
)
296 isl_space
*bmap_space
, *bset_space
;
298 bmap_space
= isl_basic_map_peek_space(bmap
);
299 bset_space
= isl_basic_set_peek_space(bset
);
300 return isl_space_tuple_is_equal(bmap_space
, type
,
301 bset_space
, isl_dim_set
);
304 /* Is the tuple of type "type" of "map" the same as the single tuple of "set"?
306 static isl_bool
isl_map_set_tuple_is_equal(__isl_keep isl_map
*map
,
307 enum isl_dim_type type
, __isl_keep isl_set
*set
)
309 return isl_map_tuple_is_equal(map
, type
, set_to_map(set
), isl_dim_set
);
312 isl_bool
isl_map_compatible_domain(__isl_keep isl_map
*map
,
313 __isl_keep isl_set
*set
)
317 return isl_bool_error
;
318 m
= isl_map_has_equal_params(map
, set_to_map(set
));
321 return isl_map_set_tuple_is_equal(map
, isl_dim_in
, set
);
324 isl_bool
isl_basic_map_compatible_domain(__isl_keep isl_basic_map
*bmap
,
325 __isl_keep isl_basic_set
*bset
)
329 return isl_bool_error
;
330 m
= isl_basic_map_has_equal_params(bmap
, bset_to_bmap(bset
));
333 return isl_basic_map_set_tuple_is_equal(bmap
, isl_dim_in
, bset
);
336 isl_bool
isl_map_compatible_range(__isl_keep isl_map
*map
,
337 __isl_keep isl_set
*set
)
341 return isl_bool_error
;
342 m
= isl_map_has_equal_params(map
, set_to_map(set
));
345 return isl_map_set_tuple_is_equal(map
, isl_dim_out
, set
);
348 isl_bool
isl_basic_map_compatible_range(__isl_keep isl_basic_map
*bmap
,
349 __isl_keep isl_basic_set
*bset
)
353 return isl_bool_error
;
354 m
= isl_basic_map_has_equal_params(bmap
, bset_to_bmap(bset
));
357 return isl_basic_map_set_tuple_is_equal(bmap
, isl_dim_out
, bset
);
360 isl_ctx
*isl_basic_map_get_ctx(__isl_keep isl_basic_map
*bmap
)
362 return bmap
? bmap
->ctx
: NULL
;
365 isl_ctx
*isl_basic_set_get_ctx(__isl_keep isl_basic_set
*bset
)
367 return bset
? bset
->ctx
: NULL
;
370 isl_ctx
*isl_map_get_ctx(__isl_keep isl_map
*map
)
372 return map
? map
->ctx
: NULL
;
375 isl_ctx
*isl_set_get_ctx(__isl_keep isl_set
*set
)
377 return set
? set
->ctx
: NULL
;
380 /* Return the space of "bmap".
382 __isl_keep isl_space
*isl_basic_map_peek_space(
383 __isl_keep
const isl_basic_map
*bmap
)
385 return bmap
? bmap
->dim
: NULL
;
388 /* Return the space of "bset".
390 __isl_keep isl_space
*isl_basic_set_peek_space(__isl_keep isl_basic_set
*bset
)
392 return isl_basic_map_peek_space(bset_to_bmap(bset
));
395 __isl_give isl_space
*isl_basic_map_get_space(__isl_keep isl_basic_map
*bmap
)
397 return isl_space_copy(isl_basic_map_peek_space(bmap
));
400 __isl_give isl_space
*isl_basic_set_get_space(__isl_keep isl_basic_set
*bset
)
402 return isl_basic_map_get_space(bset_to_bmap(bset
));
405 /* Return the space of "bmap".
406 * This may be either a copy or the space itself
407 * if there is only one reference to "bmap".
408 * This allows the space to be modified inplace
409 * if both the basic map and its space have only a single reference.
410 * The caller is not allowed to modify "bmap" between this call and
411 * a subsequent call to isl_basic_map_restore_space.
412 * The only exception is that isl_basic_map_free can be called instead.
414 static __isl_give isl_space
*isl_basic_map_take_space(
415 __isl_keep isl_basic_map
*bmap
)
422 return isl_basic_map_get_space(bmap
);
428 /* Set the space of "bmap" to "space", where the space of "bmap" may be missing
429 * due to a preceding call to isl_basic_map_take_space.
430 * However, in this case, "bmap" only has a single reference and
431 * then the call to isl_basic_map_cow has no effect.
433 static __isl_give isl_basic_map
*isl_basic_map_restore_space(
434 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*space
)
439 if (bmap
->dim
== space
) {
440 isl_space_free(space
);
444 bmap
= isl_basic_map_cow(bmap
);
447 isl_space_free(bmap
->dim
);
452 isl_basic_map_free(bmap
);
453 isl_space_free(space
);
457 /* Extract the divs in "bmap" as a matrix.
459 __isl_give isl_mat
*isl_basic_map_get_divs(__isl_keep isl_basic_map
*bmap
)
467 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
471 ctx
= isl_basic_map_get_ctx(bmap
);
472 cols
= 1 + 1 + v_div
+ bmap
->n_div
;
473 div
= isl_mat_alloc(ctx
, bmap
->n_div
, cols
);
477 for (i
= 0; i
< bmap
->n_div
; ++i
)
478 isl_seq_cpy(div
->row
[i
], bmap
->div
[i
], cols
);
483 /* Extract the divs in "bset" as a matrix.
485 __isl_give isl_mat
*isl_basic_set_get_divs(__isl_keep isl_basic_set
*bset
)
487 return isl_basic_map_get_divs(bset
);
490 __isl_give isl_local_space
*isl_basic_map_get_local_space(
491 __isl_keep isl_basic_map
*bmap
)
498 div
= isl_basic_map_get_divs(bmap
);
499 return isl_local_space_alloc_div(isl_space_copy(bmap
->dim
), div
);
502 __isl_give isl_local_space
*isl_basic_set_get_local_space(
503 __isl_keep isl_basic_set
*bset
)
505 return isl_basic_map_get_local_space(bset
);
508 /* For each known div d = floor(f/m), add the constraints
511 * -(f-(m-1)) + m d >= 0
513 * Do not finalize the result.
515 static __isl_give isl_basic_map
*add_known_div_constraints(
516 __isl_take isl_basic_map
*bmap
)
521 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
523 return isl_basic_map_free(bmap
);
526 bmap
= isl_basic_map_cow(bmap
);
527 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 2 * n_div
);
530 for (i
= 0; i
< n_div
; ++i
) {
531 if (isl_int_is_zero(bmap
->div
[i
][0]))
533 bmap
= isl_basic_map_add_div_constraints(bmap
, i
);
539 __isl_give isl_basic_map
*isl_basic_map_from_local_space(
540 __isl_take isl_local_space
*ls
)
546 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
548 ls
= isl_local_space_free(ls
);
552 bmap
= isl_basic_map_alloc_space(isl_local_space_get_space(ls
),
553 n_div
, 0, 2 * n_div
);
555 for (i
= 0; i
< n_div
; ++i
)
556 if (isl_basic_map_alloc_div(bmap
) < 0)
559 for (i
= 0; i
< n_div
; ++i
)
560 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], ls
->div
->n_col
);
561 bmap
= add_known_div_constraints(bmap
);
563 isl_local_space_free(ls
);
566 isl_local_space_free(ls
);
567 isl_basic_map_free(bmap
);
571 __isl_give isl_basic_set
*isl_basic_set_from_local_space(
572 __isl_take isl_local_space
*ls
)
574 return isl_basic_map_from_local_space(ls
);
577 __isl_give isl_space
*isl_map_get_space(__isl_keep isl_map
*map
)
579 return isl_space_copy(isl_map_peek_space(map
));
582 __isl_give isl_space
*isl_set_get_space(__isl_keep isl_set
*set
)
586 return isl_space_copy(set
->dim
);
589 /* Return the space of "map".
590 * This may be either a copy or the space itself
591 * if there is only one reference to "map".
592 * This allows the space to be modified inplace
593 * if both the map and its space have only a single reference.
594 * The caller is not allowed to modify "map" between this call and
595 * a subsequent call to isl_map_restore_space.
596 * The only exception is that isl_map_free can be called instead.
598 static __isl_give isl_space
*isl_map_take_space(__isl_keep isl_map
*map
)
605 return isl_map_get_space(map
);
611 /* Set the space of "map" to "space", where the space of "map" may be missing
612 * due to a preceding call to isl_map_take_space.
613 * However, in this case, "map" only has a single reference and
614 * then the call to isl_map_cow has no effect.
616 static __isl_give isl_map
*isl_map_restore_space(__isl_take isl_map
*map
,
617 __isl_take isl_space
*space
)
622 if (map
->dim
== space
) {
623 isl_space_free(space
);
627 map
= isl_map_cow(map
);
630 isl_space_free(map
->dim
);
636 isl_space_free(space
);
640 __isl_give isl_basic_map
*isl_basic_map_set_tuple_name(
641 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
, const char *s
)
645 space
= isl_basic_map_take_space(bmap
);
646 space
= isl_space_set_tuple_name(space
, type
, s
);
647 bmap
= isl_basic_map_restore_space(bmap
, space
);
648 bmap
= isl_basic_map_finalize(bmap
);
652 __isl_give isl_basic_set
*isl_basic_set_set_tuple_name(
653 __isl_take isl_basic_set
*bset
, const char *s
)
655 return isl_basic_map_set_tuple_name(bset
, isl_dim_set
, s
);
658 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map
*bmap
,
659 enum isl_dim_type type
)
661 return bmap
? isl_space_get_tuple_name(bmap
->dim
, type
) : NULL
;
664 __isl_give isl_map
*isl_map_set_tuple_name(__isl_take isl_map
*map
,
665 enum isl_dim_type type
, const char *s
)
670 map
= isl_map_cow(map
);
674 for (i
= 0; i
< map
->n
; ++i
) {
675 map
->p
[i
] = isl_basic_map_set_tuple_name(map
->p
[i
], type
, s
);
680 space
= isl_map_take_space(map
);
681 space
= isl_space_set_tuple_name(space
, type
, s
);
682 map
= isl_map_restore_space(map
, space
);
690 /* Replace the identifier of the tuple of type "type" by "id".
692 __isl_give isl_basic_map
*isl_basic_map_set_tuple_id(
693 __isl_take isl_basic_map
*bmap
,
694 enum isl_dim_type type
, __isl_take isl_id
*id
)
698 space
= isl_basic_map_take_space(bmap
);
699 space
= isl_space_set_tuple_id(space
, type
, id
);
700 bmap
= isl_basic_map_restore_space(bmap
, space
);
701 bmap
= isl_basic_map_finalize(bmap
);
705 /* Replace the identifier of the tuple by "id".
707 __isl_give isl_basic_set
*isl_basic_set_set_tuple_id(
708 __isl_take isl_basic_set
*bset
, __isl_take isl_id
*id
)
710 return isl_basic_map_set_tuple_id(bset
, isl_dim_set
, id
);
713 /* Does the input or output tuple have a name?
715 isl_bool
isl_map_has_tuple_name(__isl_keep isl_map
*map
, enum isl_dim_type type
)
717 return map
? isl_space_has_tuple_name(map
->dim
, type
) : isl_bool_error
;
720 const char *isl_map_get_tuple_name(__isl_keep isl_map
*map
,
721 enum isl_dim_type type
)
723 return map
? isl_space_get_tuple_name(map
->dim
, type
) : NULL
;
726 __isl_give isl_set
*isl_set_set_tuple_name(__isl_take isl_set
*set
,
729 return set_from_map(isl_map_set_tuple_name(set_to_map(set
),
733 __isl_give isl_map
*isl_map_set_tuple_id(__isl_take isl_map
*map
,
734 enum isl_dim_type type
, __isl_take isl_id
*id
)
738 space
= isl_map_take_space(map
);
739 space
= isl_space_set_tuple_id(space
, type
, id
);
740 map
= isl_map_restore_space(map
, space
);
742 return isl_map_reset_space(map
, isl_map_get_space(map
));
745 __isl_give isl_set
*isl_set_set_tuple_id(__isl_take isl_set
*set
,
746 __isl_take isl_id
*id
)
748 return isl_map_set_tuple_id(set
, isl_dim_set
, id
);
751 __isl_give isl_map
*isl_map_reset_tuple_id(__isl_take isl_map
*map
,
752 enum isl_dim_type type
)
756 space
= isl_map_take_space(map
);
757 space
= isl_space_reset_tuple_id(space
, type
);
758 map
= isl_map_restore_space(map
, space
);
760 return isl_map_reset_space(map
, isl_map_get_space(map
));
763 __isl_give isl_set
*isl_set_reset_tuple_id(__isl_take isl_set
*set
)
765 return isl_map_reset_tuple_id(set
, isl_dim_set
);
768 isl_bool
isl_map_has_tuple_id(__isl_keep isl_map
*map
, enum isl_dim_type type
)
770 return map
? isl_space_has_tuple_id(map
->dim
, type
) : isl_bool_error
;
773 __isl_give isl_id
*isl_map_get_tuple_id(__isl_keep isl_map
*map
,
774 enum isl_dim_type type
)
776 return map
? isl_space_get_tuple_id(map
->dim
, type
) : NULL
;
779 isl_bool
isl_set_has_tuple_id(__isl_keep isl_set
*set
)
781 return isl_map_has_tuple_id(set
, isl_dim_set
);
784 __isl_give isl_id
*isl_set_get_tuple_id(__isl_keep isl_set
*set
)
786 return isl_map_get_tuple_id(set
, isl_dim_set
);
789 /* Does the set tuple have a name?
791 isl_bool
isl_set_has_tuple_name(__isl_keep isl_set
*set
)
794 return isl_bool_error
;
795 return isl_space_has_tuple_name(set
->dim
, isl_dim_set
);
799 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set
*bset
)
801 return bset
? isl_space_get_tuple_name(bset
->dim
, isl_dim_set
) : NULL
;
804 const char *isl_set_get_tuple_name(__isl_keep isl_set
*set
)
806 return set
? isl_space_get_tuple_name(set
->dim
, isl_dim_set
) : NULL
;
809 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map
*bmap
,
810 enum isl_dim_type type
, unsigned pos
)
812 return bmap
? isl_space_get_dim_name(bmap
->dim
, type
, pos
) : NULL
;
815 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set
*bset
,
816 enum isl_dim_type type
, unsigned pos
)
818 return bset
? isl_space_get_dim_name(bset
->dim
, type
, pos
) : NULL
;
821 /* Does the given dimension have a name?
823 isl_bool
isl_map_has_dim_name(__isl_keep isl_map
*map
,
824 enum isl_dim_type type
, unsigned pos
)
827 return isl_bool_error
;
828 return isl_space_has_dim_name(map
->dim
, type
, pos
);
831 const char *isl_map_get_dim_name(__isl_keep isl_map
*map
,
832 enum isl_dim_type type
, unsigned pos
)
834 return map
? isl_space_get_dim_name(map
->dim
, type
, pos
) : NULL
;
837 const char *isl_set_get_dim_name(__isl_keep isl_set
*set
,
838 enum isl_dim_type type
, unsigned pos
)
840 return set
? isl_space_get_dim_name(set
->dim
, type
, pos
) : NULL
;
843 /* Does the given dimension have a name?
845 isl_bool
isl_set_has_dim_name(__isl_keep isl_set
*set
,
846 enum isl_dim_type type
, unsigned pos
)
849 return isl_bool_error
;
850 return isl_space_has_dim_name(set
->dim
, type
, pos
);
853 __isl_give isl_basic_map
*isl_basic_map_set_dim_name(
854 __isl_take isl_basic_map
*bmap
,
855 enum isl_dim_type type
, unsigned pos
, const char *s
)
859 space
= isl_basic_map_take_space(bmap
);
860 space
= isl_space_set_dim_name(space
, type
, pos
, s
);
861 bmap
= isl_basic_map_restore_space(bmap
, space
);
862 return isl_basic_map_finalize(bmap
);
865 __isl_give isl_map
*isl_map_set_dim_name(__isl_take isl_map
*map
,
866 enum isl_dim_type type
, unsigned pos
, const char *s
)
871 map
= isl_map_cow(map
);
875 for (i
= 0; i
< map
->n
; ++i
) {
876 map
->p
[i
] = isl_basic_map_set_dim_name(map
->p
[i
], type
, pos
, s
);
881 space
= isl_map_take_space(map
);
882 space
= isl_space_set_dim_name(space
, type
, pos
, s
);
883 map
= isl_map_restore_space(map
, space
);
891 __isl_give isl_basic_set
*isl_basic_set_set_dim_name(
892 __isl_take isl_basic_set
*bset
,
893 enum isl_dim_type type
, unsigned pos
, const char *s
)
895 return bset_from_bmap(isl_basic_map_set_dim_name(bset_to_bmap(bset
),
899 __isl_give isl_set
*isl_set_set_dim_name(__isl_take isl_set
*set
,
900 enum isl_dim_type type
, unsigned pos
, const char *s
)
902 return set_from_map(isl_map_set_dim_name(set_to_map(set
),
906 isl_bool
isl_basic_map_has_dim_id(__isl_keep isl_basic_map
*bmap
,
907 enum isl_dim_type type
, unsigned pos
)
910 return isl_bool_error
;
911 return isl_space_has_dim_id(bmap
->dim
, type
, pos
);
914 __isl_give isl_id
*isl_basic_set_get_dim_id(__isl_keep isl_basic_set
*bset
,
915 enum isl_dim_type type
, unsigned pos
)
917 return bset
? isl_space_get_dim_id(bset
->dim
, type
, pos
) : NULL
;
920 isl_bool
isl_map_has_dim_id(__isl_keep isl_map
*map
,
921 enum isl_dim_type type
, unsigned pos
)
923 return map
? isl_space_has_dim_id(map
->dim
, type
, pos
) : isl_bool_error
;
926 __isl_give isl_id
*isl_map_get_dim_id(__isl_keep isl_map
*map
,
927 enum isl_dim_type type
, unsigned pos
)
929 return map
? isl_space_get_dim_id(map
->dim
, type
, pos
) : NULL
;
932 isl_bool
isl_set_has_dim_id(__isl_keep isl_set
*set
,
933 enum isl_dim_type type
, unsigned pos
)
935 return isl_map_has_dim_id(set
, type
, pos
);
938 __isl_give isl_id
*isl_set_get_dim_id(__isl_keep isl_set
*set
,
939 enum isl_dim_type type
, unsigned pos
)
941 return isl_map_get_dim_id(set
, type
, pos
);
944 __isl_give isl_map
*isl_map_set_dim_id(__isl_take isl_map
*map
,
945 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
949 space
= isl_map_take_space(map
);
950 space
= isl_space_set_dim_id(space
, type
, pos
, id
);
951 map
= isl_map_restore_space(map
, space
);
953 return isl_map_reset_space(map
, isl_map_get_space(map
));
956 __isl_give isl_set
*isl_set_set_dim_id(__isl_take isl_set
*set
,
957 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
959 return isl_map_set_dim_id(set
, type
, pos
, id
);
962 int isl_map_find_dim_by_id(__isl_keep isl_map
*map
, enum isl_dim_type type
,
963 __isl_keep isl_id
*id
)
967 return isl_space_find_dim_by_id(map
->dim
, type
, id
);
970 int isl_set_find_dim_by_id(__isl_keep isl_set
*set
, enum isl_dim_type type
,
971 __isl_keep isl_id
*id
)
973 return isl_map_find_dim_by_id(set
, type
, id
);
976 /* Return the position of the dimension of the given type and name
978 * Return -1 if no such dimension can be found.
980 int isl_basic_map_find_dim_by_name(__isl_keep isl_basic_map
*bmap
,
981 enum isl_dim_type type
, const char *name
)
985 return isl_space_find_dim_by_name(bmap
->dim
, type
, name
);
988 int isl_map_find_dim_by_name(__isl_keep isl_map
*map
, enum isl_dim_type type
,
993 return isl_space_find_dim_by_name(map
->dim
, type
, name
);
996 int isl_set_find_dim_by_name(__isl_keep isl_set
*set
, enum isl_dim_type type
,
999 return isl_map_find_dim_by_name(set
, type
, name
);
1002 /* Check whether equality i of bset is a pure stride constraint
1003 * on a single dimension, i.e., of the form
1007 * with k a constant and e an existentially quantified variable.
1009 isl_bool
isl_basic_set_eq_is_stride(__isl_keep isl_basic_set
*bset
, int i
)
1017 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
1018 d
= isl_basic_set_dim(bset
, isl_dim_set
);
1019 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
1020 if (nparam
< 0 || d
< 0 || n_div
< 0)
1021 return isl_bool_error
;
1023 if (!isl_int_is_zero(bset
->eq
[i
][0]))
1024 return isl_bool_false
;
1026 if (isl_seq_first_non_zero(bset
->eq
[i
] + 1, nparam
) != -1)
1027 return isl_bool_false
;
1028 pos1
= isl_seq_first_non_zero(bset
->eq
[i
] + 1 + nparam
, d
);
1030 return isl_bool_false
;
1031 if (isl_seq_first_non_zero(bset
->eq
[i
] + 1 + nparam
+ pos1
+ 1,
1032 d
- pos1
- 1) != -1)
1033 return isl_bool_false
;
1035 pos2
= isl_seq_first_non_zero(bset
->eq
[i
] + 1 + nparam
+ d
, n_div
);
1037 return isl_bool_false
;
1038 if (isl_seq_first_non_zero(bset
->eq
[i
] + 1 + nparam
+ d
+ pos2
+ 1,
1039 n_div
- pos2
- 1) != -1)
1040 return isl_bool_false
;
1041 if (!isl_int_is_one(bset
->eq
[i
][1 + nparam
+ pos1
]) &&
1042 !isl_int_is_negone(bset
->eq
[i
][1 + nparam
+ pos1
]))
1043 return isl_bool_false
;
1045 return isl_bool_true
;
1048 /* Reset the user pointer on all identifiers of parameters and tuples
1049 * of the space of "map".
1051 __isl_give isl_map
*isl_map_reset_user(__isl_take isl_map
*map
)
1055 space
= isl_map_get_space(map
);
1056 space
= isl_space_reset_user(space
);
1057 map
= isl_map_reset_space(map
, space
);
1062 /* Reset the user pointer on all identifiers of parameters and tuples
1063 * of the space of "set".
1065 __isl_give isl_set
*isl_set_reset_user(__isl_take isl_set
*set
)
1067 return isl_map_reset_user(set
);
1070 isl_bool
isl_basic_map_is_rational(__isl_keep isl_basic_map
*bmap
)
1073 return isl_bool_error
;
1074 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
1077 /* Has "map" been marked as a rational map?
1078 * In particular, have all basic maps in "map" been marked this way?
1079 * An empty map is not considered to be rational.
1080 * Maps where only some of the basic maps are marked rational
1083 isl_bool
isl_map_is_rational(__isl_keep isl_map
*map
)
1089 return isl_bool_error
;
1091 return isl_bool_false
;
1092 rational
= isl_basic_map_is_rational(map
->p
[0]);
1095 for (i
= 1; i
< map
->n
; ++i
) {
1096 isl_bool rational_i
;
1098 rational_i
= isl_basic_map_is_rational(map
->p
[i
]);
1101 if (rational
!= rational_i
)
1102 isl_die(isl_map_get_ctx(map
), isl_error_unsupported
,
1103 "mixed rational and integer basic maps "
1104 "not supported", return isl_bool_error
);
1110 /* Has "set" been marked as a rational set?
1111 * In particular, have all basic set in "set" been marked this way?
1112 * An empty set is not considered to be rational.
1113 * Sets where only some of the basic sets are marked rational
1116 isl_bool
isl_set_is_rational(__isl_keep isl_set
*set
)
1118 return isl_map_is_rational(set
);
1121 int isl_basic_set_is_rational(__isl_keep isl_basic_set
*bset
)
1123 return isl_basic_map_is_rational(bset
);
1126 /* Does "bmap" contain any rational points?
1128 * If "bmap" has an equality for each dimension, equating the dimension
1129 * to an integer constant, then it has no rational points, even if it
1130 * is marked as rational.
1132 isl_bool
isl_basic_map_has_rational(__isl_keep isl_basic_map
*bmap
)
1134 isl_bool has_rational
= isl_bool_true
;
1138 return isl_bool_error
;
1139 if (isl_basic_map_plain_is_empty(bmap
))
1140 return isl_bool_false
;
1141 if (!isl_basic_map_is_rational(bmap
))
1142 return isl_bool_false
;
1143 bmap
= isl_basic_map_copy(bmap
);
1144 bmap
= isl_basic_map_implicit_equalities(bmap
);
1145 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
1147 return isl_bool_error
;
1148 if (bmap
->n_eq
== total
) {
1150 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
1151 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1, total
);
1154 if (!isl_int_is_one(bmap
->eq
[i
][1 + j
]) &&
1155 !isl_int_is_negone(bmap
->eq
[i
][1 + j
]))
1157 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + j
+ 1,
1162 if (i
== bmap
->n_eq
)
1163 has_rational
= isl_bool_false
;
1165 isl_basic_map_free(bmap
);
1167 return has_rational
;
1170 /* Does "map" contain any rational points?
1172 isl_bool
isl_map_has_rational(__isl_keep isl_map
*map
)
1175 isl_bool has_rational
;
1178 return isl_bool_error
;
1179 for (i
= 0; i
< map
->n
; ++i
) {
1180 has_rational
= isl_basic_map_has_rational(map
->p
[i
]);
1181 if (has_rational
< 0 || has_rational
)
1182 return has_rational
;
1184 return isl_bool_false
;
1187 /* Does "set" contain any rational points?
1189 isl_bool
isl_set_has_rational(__isl_keep isl_set
*set
)
1191 return isl_map_has_rational(set
);
1194 /* Is this basic set a parameter domain?
1196 isl_bool
isl_basic_set_is_params(__isl_keep isl_basic_set
*bset
)
1199 return isl_bool_error
;
1200 return isl_space_is_params(bset
->dim
);
1203 /* Is this set a parameter domain?
1205 isl_bool
isl_set_is_params(__isl_keep isl_set
*set
)
1208 return isl_bool_error
;
1209 return isl_space_is_params(set
->dim
);
1212 /* Is this map actually a parameter domain?
1213 * Users should never call this function. Outside of isl,
1214 * a map can never be a parameter domain.
1216 isl_bool
isl_map_is_params(__isl_keep isl_map
*map
)
1219 return isl_bool_error
;
1220 return isl_space_is_params(map
->dim
);
1223 static __isl_give isl_basic_map
*basic_map_init(isl_ctx
*ctx
,
1224 __isl_take isl_basic_map
*bmap
, unsigned extra
,
1225 unsigned n_eq
, unsigned n_ineq
)
1228 isl_space
*space
= isl_basic_map_peek_space(bmap
);
1229 isl_size n_var
= isl_space_dim(space
, isl_dim_all
);
1230 size_t row_size
= 1 + n_var
+ extra
;
1236 return isl_basic_map_free(bmap
);
1238 bmap
->block
= isl_blk_alloc(ctx
, (n_ineq
+ n_eq
) * row_size
);
1239 if (isl_blk_is_error(bmap
->block
))
1242 bmap
->ineq
= isl_alloc_array(ctx
, isl_int
*, n_ineq
+ n_eq
);
1243 if ((n_ineq
+ n_eq
) && !bmap
->ineq
)
1247 bmap
->block2
= isl_blk_empty();
1250 bmap
->block2
= isl_blk_alloc(ctx
, extra
* (1 + row_size
));
1251 if (isl_blk_is_error(bmap
->block2
))
1254 bmap
->div
= isl_alloc_array(ctx
, isl_int
*, extra
);
1259 for (i
= 0; i
< n_ineq
+ n_eq
; ++i
)
1260 bmap
->ineq
[i
] = bmap
->block
.data
+ i
* row_size
;
1262 for (i
= 0; i
< extra
; ++i
)
1263 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + row_size
);
1267 bmap
->c_size
= n_eq
+ n_ineq
;
1268 bmap
->eq
= bmap
->ineq
+ n_ineq
;
1269 bmap
->extra
= extra
;
1273 bmap
->sample
= NULL
;
1277 isl_basic_map_free(bmap
);
1281 __isl_give isl_basic_set
*isl_basic_set_alloc(isl_ctx
*ctx
,
1282 unsigned nparam
, unsigned dim
, unsigned extra
,
1283 unsigned n_eq
, unsigned n_ineq
)
1285 struct isl_basic_map
*bmap
;
1288 space
= isl_space_set_alloc(ctx
, nparam
, dim
);
1292 bmap
= isl_basic_map_alloc_space(space
, extra
, n_eq
, n_ineq
);
1293 return bset_from_bmap(bmap
);
1296 __isl_give isl_basic_set
*isl_basic_set_alloc_space(__isl_take isl_space
*space
,
1297 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
1299 struct isl_basic_map
*bmap
;
1302 isl_assert(space
->ctx
, space
->n_in
== 0, goto error
);
1303 bmap
= isl_basic_map_alloc_space(space
, extra
, n_eq
, n_ineq
);
1304 return bset_from_bmap(bmap
);
1306 isl_space_free(space
);
1310 __isl_give isl_basic_map
*isl_basic_map_alloc_space(__isl_take isl_space
*space
,
1311 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
1313 struct isl_basic_map
*bmap
;
1317 bmap
= isl_calloc_type(space
->ctx
, struct isl_basic_map
);
1322 return basic_map_init(space
->ctx
, bmap
, extra
, n_eq
, n_ineq
);
1324 isl_space_free(space
);
1328 __isl_give isl_basic_map
*isl_basic_map_alloc(isl_ctx
*ctx
,
1329 unsigned nparam
, unsigned in
, unsigned out
, unsigned extra
,
1330 unsigned n_eq
, unsigned n_ineq
)
1332 struct isl_basic_map
*bmap
;
1335 space
= isl_space_alloc(ctx
, nparam
, in
, out
);
1339 bmap
= isl_basic_map_alloc_space(space
, extra
, n_eq
, n_ineq
);
1343 static __isl_give isl_basic_map
*dup_constraints(__isl_take isl_basic_map
*dst
,
1344 __isl_keep isl_basic_map
*src
)
1347 isl_size total
= isl_basic_map_dim(src
, isl_dim_all
);
1349 if (!dst
|| total
< 0)
1350 return isl_basic_map_free(dst
);
1352 for (i
= 0; i
< src
->n_eq
; ++i
) {
1353 int j
= isl_basic_map_alloc_equality(dst
);
1355 return isl_basic_map_free(dst
);
1356 isl_seq_cpy(dst
->eq
[j
], src
->eq
[i
], 1+total
);
1359 for (i
= 0; i
< src
->n_ineq
; ++i
) {
1360 int j
= isl_basic_map_alloc_inequality(dst
);
1362 return isl_basic_map_free(dst
);
1363 isl_seq_cpy(dst
->ineq
[j
], src
->ineq
[i
], 1+total
);
1366 for (i
= 0; i
< src
->n_div
; ++i
) {
1367 int j
= isl_basic_map_alloc_div(dst
);
1369 return isl_basic_map_free(dst
);
1370 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
);
1372 ISL_F_SET(dst
, ISL_BASIC_SET_FINAL
);
1376 __isl_give isl_basic_map
*isl_basic_map_dup(__isl_keep isl_basic_map
*bmap
)
1378 struct isl_basic_map
*dup
;
1382 dup
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
1383 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
1384 dup
= dup_constraints(dup
, bmap
);
1387 dup
->flags
= bmap
->flags
;
1388 dup
->sample
= isl_vec_copy(bmap
->sample
);
1392 __isl_give isl_basic_set
*isl_basic_set_dup(__isl_keep isl_basic_set
*bset
)
1394 struct isl_basic_map
*dup
;
1396 dup
= isl_basic_map_dup(bset_to_bmap(bset
));
1397 return bset_from_bmap(dup
);
1400 __isl_give isl_basic_set
*isl_basic_set_copy(__isl_keep isl_basic_set
*bset
)
1405 if (ISL_F_ISSET(bset
, ISL_BASIC_SET_FINAL
)) {
1409 return isl_basic_set_dup(bset
);
1412 __isl_give isl_set
*isl_set_copy(__isl_keep isl_set
*set
)
1421 __isl_give isl_basic_map
*isl_basic_map_copy(__isl_keep isl_basic_map
*bmap
)
1426 if (ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
)) {
1430 bmap
= isl_basic_map_dup(bmap
);
1432 ISL_F_SET(bmap
, ISL_BASIC_SET_FINAL
);
1436 __isl_give isl_map
*isl_map_copy(__isl_keep isl_map
*map
)
1445 __isl_null isl_basic_map
*isl_basic_map_free(__isl_take isl_basic_map
*bmap
)
1450 if (--bmap
->ref
> 0)
1453 isl_ctx_deref(bmap
->ctx
);
1455 isl_blk_free(bmap
->ctx
, bmap
->block2
);
1457 isl_blk_free(bmap
->ctx
, bmap
->block
);
1458 isl_vec_free(bmap
->sample
);
1459 isl_space_free(bmap
->dim
);
1465 __isl_null isl_basic_set
*isl_basic_set_free(__isl_take isl_basic_set
*bset
)
1467 return isl_basic_map_free(bset_to_bmap(bset
));
1470 static int room_for_con(__isl_keep isl_basic_map
*bmap
, unsigned n
)
1472 return bmap
->n_eq
+ bmap
->n_ineq
+ n
<= bmap
->c_size
;
1475 /* Check that "bset" does not involve any parameters.
1477 isl_stat
isl_basic_set_check_no_params(__isl_keep isl_basic_set
*bset
)
1481 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
1483 return isl_stat_error
;
1485 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
1486 "basic set should not have any parameters",
1487 return isl_stat_error
);
1491 /* Check that "bset" does not involve any local variables.
1493 isl_stat
isl_basic_set_check_no_locals(__isl_keep isl_basic_set
*bset
)
1497 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
1499 return isl_stat_error
;
1501 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
1502 "basic set should not have any local variables",
1503 return isl_stat_error
);
1508 #define TYPE isl_map
1510 #include "isl_check_named_params_templ.c"
1513 #define TYPE isl_basic_map
1516 #include "isl_check_named_params_templ.c"
1518 /* Check that "bmap1" and "bmap2" have the same parameters,
1519 * reporting an error if they do not.
1521 static isl_stat
isl_basic_map_check_equal_params(
1522 __isl_keep isl_basic_map
*bmap1
, __isl_keep isl_basic_map
*bmap2
)
1526 match
= isl_basic_map_has_equal_params(bmap1
, bmap2
);
1528 return isl_stat_error
;
1530 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
1531 "parameters don't match", return isl_stat_error
);
1536 #define TYPE isl_map
1538 #include "isl_align_params_bin_templ.c"
1543 #define ARG1 isl_map
1545 #define ARG2 isl_set
1547 #include "isl_align_params_templ.c"
1549 isl_bool
isl_map_align_params_map_map_and_test(__isl_keep isl_map
*map1
,
1550 __isl_keep isl_map
*map2
,
1551 isl_bool (*fn
)(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
))
1556 return isl_bool_error
;
1557 if (isl_map_has_equal_params(map1
, map2
))
1558 return fn(map1
, map2
);
1559 if (isl_map_check_named_params(map1
) < 0)
1560 return isl_bool_error
;
1561 if (isl_map_check_named_params(map2
) < 0)
1562 return isl_bool_error
;
1563 map1
= isl_map_copy(map1
);
1564 map2
= isl_map_copy(map2
);
1565 map1
= isl_map_align_params(map1
, isl_map_get_space(map2
));
1566 map2
= isl_map_align_params(map2
, isl_map_get_space(map1
));
1573 int isl_basic_map_alloc_equality(__isl_keep isl_basic_map
*bmap
)
1576 struct isl_ctx
*ctx
;
1578 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
1582 isl_assert(ctx
, room_for_con(bmap
, 1), return -1);
1583 isl_assert(ctx
, (bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
<= bmap
->c_size
,
1585 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1586 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1587 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1588 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1589 if ((bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
== bmap
->c_size
) {
1591 int j
= isl_basic_map_alloc_inequality(bmap
);
1595 bmap
->ineq
[j
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1596 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
1603 isl_seq_clr(bmap
->eq
[bmap
->n_eq
] + 1 + total
,
1604 bmap
->extra
- bmap
->n_div
);
1605 return bmap
->n_eq
++;
1608 int isl_basic_set_alloc_equality(__isl_keep isl_basic_set
*bset
)
1610 return isl_basic_map_alloc_equality(bset_to_bmap(bset
));
1613 __isl_give isl_basic_map
*isl_basic_map_free_equality(
1614 __isl_take isl_basic_map
*bmap
, unsigned n
)
1619 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
1620 "invalid number of equalities",
1621 isl_basic_map_free(bmap
));
1626 __isl_give isl_basic_set
*isl_basic_set_free_equality(
1627 __isl_take isl_basic_set
*bset
, unsigned n
)
1629 return bset_from_bmap(isl_basic_map_free_equality(bset_to_bmap(bset
),
1633 /* Drop the equality constraint at position "pos",
1634 * preserving the order of the other equality constraints.
1636 int isl_basic_map_drop_equality(__isl_keep isl_basic_map
*bmap
, unsigned pos
)
1643 isl_assert(bmap
->ctx
, pos
< bmap
->n_eq
, return -1);
1647 for (r
= pos
; r
< bmap
->n_eq
; ++r
)
1648 bmap
->eq
[r
] = bmap
->eq
[r
+ 1];
1649 bmap
->eq
[bmap
->n_eq
] = t
;
1654 /* Turn inequality "pos" of "bmap" into an equality.
1656 * In particular, we move the inequality in front of the equalities
1657 * and move the last inequality in the position of the moved inequality.
1658 * Note that isl_tab_make_equalities_explicit depends on this particular
1659 * change in the ordering of the constraints.
1661 void isl_basic_map_inequality_to_equality(
1662 __isl_keep isl_basic_map
*bmap
, unsigned pos
)
1666 t
= bmap
->ineq
[pos
];
1667 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1668 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
1673 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1674 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
1675 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1676 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1679 static int room_for_ineq(__isl_keep isl_basic_map
*bmap
, unsigned n
)
1681 return bmap
->n_ineq
+ n
<= bmap
->eq
- bmap
->ineq
;
1684 int isl_basic_map_alloc_inequality(__isl_keep isl_basic_map
*bmap
)
1687 struct isl_ctx
*ctx
;
1689 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
1693 isl_assert(ctx
, room_for_ineq(bmap
, 1), return -1);
1694 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1695 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1696 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
1697 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1698 isl_seq_clr(bmap
->ineq
[bmap
->n_ineq
] + 1 + total
,
1699 bmap
->extra
- bmap
->n_div
);
1700 return bmap
->n_ineq
++;
1703 int isl_basic_set_alloc_inequality(__isl_keep isl_basic_set
*bset
)
1705 return isl_basic_map_alloc_inequality(bset_to_bmap(bset
));
1708 __isl_give isl_basic_map
*isl_basic_map_free_inequality(
1709 __isl_take isl_basic_map
*bmap
, unsigned n
)
1713 if (n
> bmap
->n_ineq
)
1714 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
1715 "invalid number of inequalities",
1716 return isl_basic_map_free(bmap
));
1721 __isl_give isl_basic_set
*isl_basic_set_free_inequality(
1722 __isl_take isl_basic_set
*bset
, unsigned n
)
1724 return bset_from_bmap(isl_basic_map_free_inequality(bset_to_bmap(bset
),
1728 int isl_basic_map_drop_inequality(__isl_keep isl_basic_map
*bmap
, unsigned pos
)
1733 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
1735 if (pos
!= bmap
->n_ineq
- 1) {
1736 t
= bmap
->ineq
[pos
];
1737 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1738 bmap
->ineq
[bmap
->n_ineq
- 1] = t
;
1739 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
1745 int isl_basic_set_drop_inequality(__isl_keep isl_basic_set
*bset
, unsigned pos
)
1747 return isl_basic_map_drop_inequality(bset_to_bmap(bset
), pos
);
1750 __isl_give isl_basic_map
*isl_basic_map_add_eq(__isl_take isl_basic_map
*bmap
,
1757 empty
= isl_basic_map_plain_is_empty(bmap
);
1759 return isl_basic_map_free(bmap
);
1763 bmap
= isl_basic_map_cow(bmap
);
1764 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
1765 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
1767 return isl_basic_map_free(bmap
);
1768 k
= isl_basic_map_alloc_equality(bmap
);
1771 isl_seq_cpy(bmap
->eq
[k
], eq
, 1 + total
);
1774 isl_basic_map_free(bmap
);
1778 __isl_give isl_basic_set
*isl_basic_set_add_eq(__isl_take isl_basic_set
*bset
,
1781 return bset_from_bmap(isl_basic_map_add_eq(bset_to_bmap(bset
), eq
));
1784 __isl_give isl_basic_map
*isl_basic_map_add_ineq(__isl_take isl_basic_map
*bmap
,
1790 bmap
= isl_basic_map_cow(bmap
);
1791 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
1792 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
1794 return isl_basic_map_free(bmap
);
1795 k
= isl_basic_map_alloc_inequality(bmap
);
1798 isl_seq_cpy(bmap
->ineq
[k
], ineq
, 1 + total
);
1801 isl_basic_map_free(bmap
);
1805 __isl_give isl_basic_set
*isl_basic_set_add_ineq(__isl_take isl_basic_set
*bset
,
1808 return bset_from_bmap(isl_basic_map_add_ineq(bset_to_bmap(bset
), ineq
));
1811 int isl_basic_map_alloc_div(__isl_keep isl_basic_map
*bmap
)
1815 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
1818 isl_assert(bmap
->ctx
, bmap
->n_div
< bmap
->extra
, return -1);
1819 isl_seq_clr(bmap
->div
[bmap
->n_div
] + 1 + 1 + total
,
1820 bmap
->extra
- bmap
->n_div
);
1821 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1822 return bmap
->n_div
++;
1825 int isl_basic_set_alloc_div(__isl_keep isl_basic_set
*bset
)
1827 return isl_basic_map_alloc_div(bset_to_bmap(bset
));
1831 #define TYPE isl_basic_map
1832 #include "check_type_range_templ.c"
1834 /* Check that there are "n" dimensions of type "type" starting at "first"
1837 isl_stat
isl_basic_set_check_range(__isl_keep isl_basic_set
*bset
,
1838 enum isl_dim_type type
, unsigned first
, unsigned n
)
1840 return isl_basic_map_check_range(bset_to_bmap(bset
),
1844 /* Insert an extra integer division, prescribed by "div", to "bmap"
1845 * at (integer division) position "pos".
1847 * The integer division is first added at the end and then moved
1848 * into the right position.
1850 __isl_give isl_basic_map
*isl_basic_map_insert_div(
1851 __isl_take isl_basic_map
*bmap
, int pos
, __isl_keep isl_vec
*div
)
1856 bmap
= isl_basic_map_cow(bmap
);
1857 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
1858 if (total
< 0 || !div
)
1859 return isl_basic_map_free(bmap
);
1861 if (div
->size
!= 1 + 1 + total
)
1862 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
1863 "unexpected size", return isl_basic_map_free(bmap
));
1864 if (isl_basic_map_check_range(bmap
, isl_dim_div
, pos
, 0) < 0)
1865 return isl_basic_map_free(bmap
);
1867 bmap
= isl_basic_map_extend(bmap
, 1, 0, 2);
1868 k
= isl_basic_map_alloc_div(bmap
);
1870 return isl_basic_map_free(bmap
);
1871 isl_seq_cpy(bmap
->div
[k
], div
->el
, div
->size
);
1872 isl_int_set_si(bmap
->div
[k
][div
->size
], 0);
1874 for (i
= k
; i
> pos
; --i
)
1875 bmap
= isl_basic_map_swap_div(bmap
, i
, i
- 1);
1880 isl_stat
isl_basic_map_free_div(__isl_keep isl_basic_map
*bmap
, unsigned n
)
1883 return isl_stat_error
;
1884 isl_assert(bmap
->ctx
, n
<= bmap
->n_div
, return isl_stat_error
);
1889 static __isl_give isl_basic_map
*add_constraints(
1890 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
,
1891 unsigned i_pos
, unsigned o_pos
)
1893 isl_size total
, n_param
, n_in
, n_out
, n_div
;
1894 unsigned o_in
, o_out
;
1897 struct isl_dim_map
*dim_map
;
1899 space
= isl_basic_map_peek_space(bmap2
);
1900 if (!bmap1
|| !space
)
1903 total
= isl_basic_map_dim(bmap1
, isl_dim_all
);
1904 n_param
= isl_basic_map_dim(bmap2
, isl_dim_param
);
1905 n_in
= isl_basic_map_dim(bmap2
, isl_dim_in
);
1906 o_in
= isl_basic_map_offset(bmap1
, isl_dim_in
) - 1 + i_pos
;
1907 n_out
= isl_basic_map_dim(bmap2
, isl_dim_out
);
1908 o_out
= isl_basic_map_offset(bmap1
, isl_dim_out
) - 1 + o_pos
;
1909 n_div
= isl_basic_map_dim(bmap2
, isl_dim_div
);
1910 if (total
< 0 || n_param
< 0 || n_in
< 0 || n_out
< 0 || n_div
< 0)
1912 ctx
= isl_basic_map_get_ctx(bmap1
);
1913 dim_map
= isl_dim_map_alloc(ctx
, total
+ n_div
);
1914 isl_dim_map_dim_range(dim_map
, space
, isl_dim_param
, 0, n_param
, 0);
1915 isl_dim_map_dim_range(dim_map
, space
, isl_dim_in
, 0, n_in
, o_in
);
1916 isl_dim_map_dim_range(dim_map
, space
, isl_dim_out
, 0, n_out
, o_out
);
1917 isl_dim_map_div(dim_map
, bmap2
, total
);
1919 return isl_basic_map_add_constraints_dim_map(bmap1
, bmap2
, dim_map
);
1921 isl_basic_map_free(bmap1
);
1922 isl_basic_map_free(bmap2
);
1926 __isl_give isl_basic_map
*isl_basic_map_extend(__isl_take isl_basic_map
*base
,
1927 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
1930 struct isl_basic_map
*ext
;
1937 dims_ok
= base
->extra
>= base
->n_div
+ extra
;
1939 if (dims_ok
&& room_for_con(base
, n_eq
+ n_ineq
) &&
1940 room_for_ineq(base
, n_ineq
))
1943 extra
+= base
->extra
;
1945 n_ineq
+= base
->n_ineq
;
1947 space
= isl_basic_map_get_space(base
);
1948 ext
= isl_basic_map_alloc_space(space
, extra
, n_eq
, n_ineq
);
1953 ext
->sample
= isl_vec_copy(base
->sample
);
1954 flags
= base
->flags
;
1955 ext
= add_constraints(ext
, base
, 0, 0);
1958 ISL_F_CLR(ext
, ISL_BASIC_SET_FINAL
);
1964 isl_basic_map_free(base
);
1968 __isl_give isl_basic_set
*isl_basic_set_extend(__isl_take isl_basic_set
*base
,
1969 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
1971 return bset_from_bmap(isl_basic_map_extend(bset_to_bmap(base
),
1972 extra
, n_eq
, n_ineq
));
1975 __isl_give isl_basic_map
*isl_basic_map_extend_constraints(
1976 __isl_take isl_basic_map
*base
, unsigned n_eq
, unsigned n_ineq
)
1978 return isl_basic_map_extend(base
, 0, n_eq
, n_ineq
);
1981 __isl_give isl_basic_set
*isl_basic_set_extend_constraints(
1982 __isl_take isl_basic_set
*base
, unsigned n_eq
, unsigned n_ineq
)
1984 isl_basic_map
*bmap
= bset_to_bmap(base
);
1985 bmap
= isl_basic_map_extend_constraints(bmap
, n_eq
, n_ineq
);
1986 return bset_from_bmap(bmap
);
1989 __isl_give isl_basic_set
*isl_basic_set_cow(__isl_take isl_basic_set
*bset
)
1991 return bset_from_bmap(isl_basic_map_cow(bset_to_bmap(bset
)));
1994 __isl_give isl_basic_map
*isl_basic_map_cow(__isl_take isl_basic_map
*bmap
)
1999 if (bmap
->ref
> 1) {
2001 bmap
= isl_basic_map_dup(bmap
);
2004 ISL_F_CLR(bmap
, ISL_BASIC_SET_FINAL
);
2005 ISL_F_CLR(bmap
, ISL_BASIC_MAP_REDUCED_COEFFICIENTS
);
2010 /* Clear all cached information in "map", either because it is about
2011 * to be modified or because it is being freed.
2012 * Always return the same pointer that is passed in.
2013 * This is needed for the use in isl_map_free.
2015 static __isl_give isl_map
*clear_caches(__isl_take isl_map
*map
)
2017 isl_basic_map_free(map
->cached_simple_hull
[0]);
2018 isl_basic_map_free(map
->cached_simple_hull
[1]);
2019 map
->cached_simple_hull
[0] = NULL
;
2020 map
->cached_simple_hull
[1] = NULL
;
2024 __isl_give isl_set
*isl_set_cow(__isl_take isl_set
*set
)
2026 return isl_map_cow(set
);
2029 /* Return an isl_map that is equal to "map" and that has only
2030 * a single reference.
2032 * If the original input already has only one reference, then
2033 * simply return it, but clear all cached information, since
2034 * it may be rendered invalid by the operations that will be
2035 * performed on the result.
2037 * Otherwise, create a duplicate (without any cached information).
2039 __isl_give isl_map
*isl_map_cow(__isl_take isl_map
*map
)
2045 return clear_caches(map
);
2047 return isl_map_dup(map
);
2050 static void swap_vars(struct isl_blk blk
, isl_int
*a
,
2051 unsigned a_len
, unsigned b_len
)
2053 isl_seq_cpy(blk
.data
, a
+a_len
, b_len
);
2054 isl_seq_cpy(blk
.data
+b_len
, a
, a_len
);
2055 isl_seq_cpy(a
, blk
.data
, b_len
+a_len
);
2058 static __isl_give isl_basic_map
*isl_basic_map_swap_vars(
2059 __isl_take isl_basic_map
*bmap
, unsigned pos
, unsigned n1
, unsigned n2
)
2064 if (isl_basic_map_check_range(bmap
, isl_dim_all
, pos
- 1, n1
+ n2
) < 0)
2067 if (n1
== 0 || n2
== 0)
2070 bmap
= isl_basic_map_cow(bmap
);
2074 blk
= isl_blk_alloc(bmap
->ctx
, n1
+ n2
);
2075 if (isl_blk_is_error(blk
))
2078 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2080 bmap
->eq
[i
] + pos
, n1
, n2
);
2082 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2084 bmap
->ineq
[i
] + pos
, n1
, n2
);
2086 for (i
= 0; i
< bmap
->n_div
; ++i
)
2088 bmap
->div
[i
]+1 + pos
, n1
, n2
);
2090 isl_blk_free(bmap
->ctx
, blk
);
2092 ISL_F_CLR(bmap
, ISL_BASIC_SET_SORTED
);
2093 bmap
= isl_basic_map_gauss(bmap
, NULL
);
2094 return isl_basic_map_finalize(bmap
);
2096 isl_basic_map_free(bmap
);
2100 /* The given basic map has turned out to be empty.
2101 * Explicitly mark it as such and change the representation
2102 * to a canonical representation of the empty basic map.
2103 * Since the basic map has conflicting constraints,
2104 * it must have at least one constraint, except perhaps
2105 * if it was already explicitly marked as being empty.
2106 * Do nothing in the latter case.
2108 __isl_give isl_basic_map
*isl_basic_map_set_to_empty(
2109 __isl_take isl_basic_map
*bmap
)
2115 empty
= isl_basic_map_plain_is_empty(bmap
);
2117 return isl_basic_map_free(bmap
);
2120 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
2122 return isl_basic_map_free(bmap
);
2123 if (isl_basic_map_free_div(bmap
, bmap
->n_div
) < 0)
2124 return isl_basic_map_free(bmap
);
2125 bmap
= isl_basic_map_free_inequality(bmap
, bmap
->n_ineq
);
2128 if (bmap
->n_eq
> 0) {
2129 bmap
= isl_basic_map_free_equality(bmap
, bmap
->n_eq
- 1);
2133 i
= isl_basic_map_alloc_equality(bmap
);
2137 isl_int_set_si(bmap
->eq
[i
][0], 1);
2138 isl_seq_clr(bmap
->eq
[i
]+1, total
);
2139 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
2140 isl_vec_free(bmap
->sample
);
2141 bmap
->sample
= NULL
;
2142 return isl_basic_map_finalize(bmap
);
2144 isl_basic_map_free(bmap
);
2148 __isl_give isl_basic_set
*isl_basic_set_set_to_empty(
2149 __isl_take isl_basic_set
*bset
)
2151 return bset_from_bmap(isl_basic_map_set_to_empty(bset_to_bmap(bset
)));
2154 __isl_give isl_basic_map
*isl_basic_map_set_rational(
2155 __isl_take isl_basic_map
*bmap
)
2160 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
2163 bmap
= isl_basic_map_cow(bmap
);
2167 ISL_F_SET(bmap
, ISL_BASIC_MAP_RATIONAL
);
2169 return isl_basic_map_finalize(bmap
);
2172 __isl_give isl_basic_set
*isl_basic_set_set_rational(
2173 __isl_take isl_basic_set
*bset
)
2175 return isl_basic_map_set_rational(bset
);
2178 __isl_give isl_basic_set
*isl_basic_set_set_integral(
2179 __isl_take isl_basic_set
*bset
)
2184 if (!ISL_F_ISSET(bset
, ISL_BASIC_MAP_RATIONAL
))
2187 bset
= isl_basic_set_cow(bset
);
2191 ISL_F_CLR(bset
, ISL_BASIC_MAP_RATIONAL
);
2193 return isl_basic_set_finalize(bset
);
2196 __isl_give isl_map
*isl_map_set_rational(__isl_take isl_map
*map
)
2200 map
= isl_map_cow(map
);
2203 for (i
= 0; i
< map
->n
; ++i
) {
2204 map
->p
[i
] = isl_basic_map_set_rational(map
->p
[i
]);
2214 __isl_give isl_set
*isl_set_set_rational(__isl_take isl_set
*set
)
2216 return isl_map_set_rational(set
);
2219 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
2222 static void swap_div(__isl_keep isl_basic_map
*bmap
, int a
, int b
)
2224 isl_int
*t
= bmap
->div
[a
];
2225 bmap
->div
[a
] = bmap
->div
[b
];
2229 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
2230 * div definitions accordingly.
2232 __isl_give isl_basic_map
*isl_basic_map_swap_div(__isl_take isl_basic_map
*bmap
,
2238 off
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
2240 return isl_basic_map_free(bmap
);
2242 swap_div(bmap
, a
, b
);
2244 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2245 isl_int_swap(bmap
->eq
[i
][1+off
+a
], bmap
->eq
[i
][1+off
+b
]);
2247 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2248 isl_int_swap(bmap
->ineq
[i
][1+off
+a
], bmap
->ineq
[i
][1+off
+b
]);
2250 for (i
= 0; i
< bmap
->n_div
; ++i
)
2251 isl_int_swap(bmap
->div
[i
][1+1+off
+a
], bmap
->div
[i
][1+1+off
+b
]);
2252 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
2257 static void constraint_drop_vars(isl_int
*c
, unsigned n
, unsigned rem
)
2259 isl_seq_cpy(c
, c
+ n
, rem
);
2260 isl_seq_clr(c
+ rem
, n
);
2263 /* Drop n dimensions starting at first.
2265 * In principle, this frees up some extra variables as the number
2266 * of columns remains constant, but we would have to extend
2267 * the div array too as the number of rows in this array is assumed
2268 * to be equal to extra.
2270 __isl_give isl_basic_set
*isl_basic_set_drop_dims(
2271 __isl_take isl_basic_set
*bset
, unsigned first
, unsigned n
)
2273 return isl_basic_map_drop(bset_to_bmap(bset
), isl_dim_set
, first
, n
);
2276 /* Move "n" divs starting at "first" to the end of the list of divs.
2278 static __isl_give isl_basic_map
*move_divs_last(__isl_take isl_basic_map
*bmap
,
2279 unsigned first
, unsigned n
)
2284 if (first
+ n
== bmap
->n_div
)
2287 div
= isl_alloc_array(bmap
->ctx
, isl_int
*, n
);
2290 for (i
= 0; i
< n
; ++i
)
2291 div
[i
] = bmap
->div
[first
+ i
];
2292 for (i
= 0; i
< bmap
->n_div
- first
- n
; ++i
)
2293 bmap
->div
[first
+ i
] = bmap
->div
[first
+ n
+ i
];
2294 for (i
= 0; i
< n
; ++i
)
2295 bmap
->div
[bmap
->n_div
- n
+ i
] = div
[i
];
2299 isl_basic_map_free(bmap
);
2304 #define TYPE isl_map
2306 #include "check_type_range_templ.c"
2308 /* Check that there are "n" dimensions of type "type" starting at "first"
2311 static isl_stat
isl_set_check_range(__isl_keep isl_set
*set
,
2312 enum isl_dim_type type
, unsigned first
, unsigned n
)
2314 return isl_map_check_range(set_to_map(set
), type
, first
, n
);
2317 /* Drop "n" dimensions of type "type" starting at "first".
2318 * Perform the core computation, without cowing or
2319 * simplifying and finalizing the result.
2321 * In principle, this frees up some extra variables as the number
2322 * of columns remains constant, but we would have to extend
2323 * the div array too as the number of rows in this array is assumed
2324 * to be equal to extra.
2326 __isl_give isl_basic_map
*isl_basic_map_drop_core(
2327 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
2328 unsigned first
, unsigned n
)
2335 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2336 return isl_basic_map_free(bmap
);
2338 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
2340 return isl_basic_map_free(bmap
);
2342 offset
= isl_basic_map_offset(bmap
, type
) + first
;
2343 left
= total
- (offset
- 1) - n
;
2344 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2345 constraint_drop_vars(bmap
->eq
[i
]+offset
, n
, left
);
2347 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2348 constraint_drop_vars(bmap
->ineq
[i
]+offset
, n
, left
);
2350 for (i
= 0; i
< bmap
->n_div
; ++i
)
2351 constraint_drop_vars(bmap
->div
[i
]+1+offset
, n
, left
);
2353 if (type
== isl_dim_div
) {
2354 bmap
= move_divs_last(bmap
, first
, n
);
2357 if (isl_basic_map_free_div(bmap
, n
) < 0)
2358 return isl_basic_map_free(bmap
);
2360 bmap
->dim
= isl_space_drop_dims(bmap
->dim
, type
, first
, n
);
2362 return isl_basic_map_free(bmap
);
2364 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
2365 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
2369 /* Drop "n" dimensions of type "type" starting at "first".
2371 * In principle, this frees up some extra variables as the number
2372 * of columns remains constant, but we would have to extend
2373 * the div array too as the number of rows in this array is assumed
2374 * to be equal to extra.
2376 __isl_give isl_basic_map
*isl_basic_map_drop(__isl_take isl_basic_map
*bmap
,
2377 enum isl_dim_type type
, unsigned first
, unsigned n
)
2381 if (n
== 0 && !isl_space_is_named_or_nested(bmap
->dim
, type
))
2384 bmap
= isl_basic_map_cow(bmap
);
2388 bmap
= isl_basic_map_drop_core(bmap
, type
, first
, n
);
2390 bmap
= isl_basic_map_simplify(bmap
);
2391 return isl_basic_map_finalize(bmap
);
2394 __isl_give isl_basic_set
*isl_basic_set_drop(__isl_take isl_basic_set
*bset
,
2395 enum isl_dim_type type
, unsigned first
, unsigned n
)
2397 return bset_from_bmap(isl_basic_map_drop(bset_to_bmap(bset
),
2401 /* No longer consider "map" to be normalized.
2403 static __isl_give isl_map
*isl_map_unmark_normalized(__isl_take isl_map
*map
)
2407 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
2411 __isl_give isl_map
*isl_map_drop(__isl_take isl_map
*map
,
2412 enum isl_dim_type type
, unsigned first
, unsigned n
)
2417 if (isl_map_check_range(map
, type
, first
, n
) < 0)
2418 return isl_map_free(map
);
2420 if (n
== 0 && !isl_space_is_named_or_nested(map
->dim
, type
))
2422 map
= isl_map_cow(map
);
2426 for (i
= 0; i
< map
->n
; ++i
) {
2427 map
->p
[i
] = isl_basic_map_drop(map
->p
[i
], type
, first
, n
);
2431 map
= isl_map_unmark_normalized(map
);
2433 space
= isl_map_take_space(map
);
2434 space
= isl_space_drop_dims(space
, type
, first
, n
);
2435 map
= isl_map_restore_space(map
, space
);
2443 __isl_give isl_set
*isl_set_drop(__isl_take isl_set
*set
,
2444 enum isl_dim_type type
, unsigned first
, unsigned n
)
2446 return set_from_map(isl_map_drop(set_to_map(set
), type
, first
, n
));
2449 /* Drop the integer division at position "div", which is assumed
2450 * not to appear in any of the constraints or
2451 * in any of the other integer divisions.
2453 * Since the integer division is redundant, there is no need to cow.
2455 __isl_give isl_basic_map
*isl_basic_map_drop_div(
2456 __isl_take isl_basic_map
*bmap
, unsigned div
)
2458 return isl_basic_map_drop_core(bmap
, isl_dim_div
, div
, 1);
2461 /* Eliminate the specified n dimensions starting at first from the
2462 * constraints, without removing the dimensions from the space.
2463 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2465 __isl_give isl_map
*isl_map_eliminate(__isl_take isl_map
*map
,
2466 enum isl_dim_type type
, unsigned first
, unsigned n
)
2473 if (isl_map_check_range(map
, type
, first
, n
) < 0)
2474 return isl_map_free(map
);
2476 map
= isl_map_cow(map
);
2480 for (i
= 0; i
< map
->n
; ++i
) {
2481 map
->p
[i
] = isl_basic_map_eliminate(map
->p
[i
], type
, first
, n
);
2491 /* Eliminate the specified n dimensions starting at first from the
2492 * constraints, without removing the dimensions from the space.
2493 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2495 __isl_give isl_set
*isl_set_eliminate(__isl_take isl_set
*set
,
2496 enum isl_dim_type type
, unsigned first
, unsigned n
)
2498 return set_from_map(isl_map_eliminate(set_to_map(set
), type
, first
, n
));
2501 /* Eliminate the specified n dimensions starting at first from the
2502 * constraints, without removing the dimensions from the space.
2503 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2505 __isl_give isl_set
*isl_set_eliminate_dims(__isl_take isl_set
*set
,
2506 unsigned first
, unsigned n
)
2508 return isl_set_eliminate(set
, isl_dim_set
, first
, n
);
2511 __isl_give isl_basic_map
*isl_basic_map_remove_divs(
2512 __isl_take isl_basic_map
*bmap
)
2516 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
2518 return isl_basic_map_free(bmap
);
2519 bmap
= isl_basic_map_eliminate_vars(bmap
, v_div
, bmap
->n_div
);
2523 return isl_basic_map_finalize(bmap
);
2526 __isl_give isl_basic_set
*isl_basic_set_remove_divs(
2527 __isl_take isl_basic_set
*bset
)
2529 return bset_from_bmap(isl_basic_map_remove_divs(bset_to_bmap(bset
)));
2532 __isl_give isl_map
*isl_map_remove_divs(__isl_take isl_map
*map
)
2541 map
= isl_map_cow(map
);
2545 for (i
= 0; i
< map
->n
; ++i
) {
2546 map
->p
[i
] = isl_basic_map_remove_divs(map
->p
[i
]);
2556 __isl_give isl_set
*isl_set_remove_divs(__isl_take isl_set
*set
)
2558 return isl_map_remove_divs(set
);
2561 __isl_give isl_basic_map
*isl_basic_map_remove_dims(
2562 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
2563 unsigned first
, unsigned n
)
2565 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2566 return isl_basic_map_free(bmap
);
2567 if (n
== 0 && !isl_space_is_named_or_nested(bmap
->dim
, type
))
2569 bmap
= isl_basic_map_eliminate_vars(bmap
,
2570 isl_basic_map_offset(bmap
, type
) - 1 + first
, n
);
2573 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
) && type
== isl_dim_div
)
2575 bmap
= isl_basic_map_drop(bmap
, type
, first
, n
);
2579 /* Return true if the definition of the given div (recursively) involves
2580 * any of the given variables.
2582 static isl_bool
div_involves_vars(__isl_keep isl_basic_map
*bmap
, int div
,
2583 unsigned first
, unsigned n
)
2586 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
2588 if (isl_int_is_zero(bmap
->div
[div
][0]))
2589 return isl_bool_false
;
2590 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + first
, n
) >= 0)
2591 return isl_bool_true
;
2593 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2596 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
2598 involves
= div_involves_vars(bmap
, i
, first
, n
);
2599 if (involves
< 0 || involves
)
2603 return isl_bool_false
;
2606 /* Try and add a lower and/or upper bound on "div" to "bmap"
2607 * based on inequality "i".
2608 * "total" is the total number of variables (excluding the divs).
2609 * "v" is a temporary object that can be used during the calculations.
2610 * If "lb" is set, then a lower bound should be constructed.
2611 * If "ub" is set, then an upper bound should be constructed.
2613 * The calling function has already checked that the inequality does not
2614 * reference "div", but we still need to check that the inequality is
2615 * of the right form. We'll consider the case where we want to construct
2616 * a lower bound. The construction of upper bounds is similar.
2618 * Let "div" be of the form
2620 * q = floor((a + f(x))/d)
2622 * We essentially check if constraint "i" is of the form
2626 * so that we can use it to derive a lower bound on "div".
2627 * However, we allow a slightly more general form
2631 * with the condition that the coefficients of g(x) - f(x) are all
2633 * Rewriting this constraint as
2637 * adding a + f(x) to both sides and dividing by d, we obtain
2639 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
2641 * Taking the floor on both sides, we obtain
2643 * q >= floor((a-b)/d) + (f(x)-g(x))/d
2647 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
2649 * In the case of an upper bound, we construct the constraint
2651 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
2654 static __isl_give isl_basic_map
*insert_bounds_on_div_from_ineq(
2655 __isl_take isl_basic_map
*bmap
, int div
, int i
,
2656 unsigned total
, isl_int v
, int lb
, int ub
)
2660 for (j
= 0; (lb
|| ub
) && j
< total
+ bmap
->n_div
; ++j
) {
2662 isl_int_sub(v
, bmap
->ineq
[i
][1 + j
],
2663 bmap
->div
[div
][1 + 1 + j
]);
2664 lb
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
2667 isl_int_add(v
, bmap
->ineq
[i
][1 + j
],
2668 bmap
->div
[div
][1 + 1 + j
]);
2669 ub
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
2675 bmap
= isl_basic_map_cow(bmap
);
2676 bmap
= isl_basic_map_extend_constraints(bmap
, 0, lb
+ ub
);
2678 int k
= isl_basic_map_alloc_inequality(bmap
);
2681 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
2682 isl_int_sub(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
2683 bmap
->div
[div
][1 + j
]);
2684 isl_int_cdiv_q(bmap
->ineq
[k
][j
],
2685 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
2687 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], 1);
2690 int k
= isl_basic_map_alloc_inequality(bmap
);
2693 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
2694 isl_int_add(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
2695 bmap
->div
[div
][1 + j
]);
2696 isl_int_fdiv_q(bmap
->ineq
[k
][j
],
2697 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
2699 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], -1);
2704 isl_basic_map_free(bmap
);
2708 /* This function is called right before "div" is eliminated from "bmap"
2709 * using Fourier-Motzkin.
2710 * Look through the constraints of "bmap" for constraints on the argument
2711 * of the integer division and use them to construct constraints on the
2712 * integer division itself. These constraints can then be combined
2713 * during the Fourier-Motzkin elimination.
2714 * Note that it is only useful to introduce lower bounds on "div"
2715 * if "bmap" already contains upper bounds on "div" as the newly
2716 * introduce lower bounds can then be combined with the pre-existing
2717 * upper bounds. Similarly for upper bounds.
2718 * We therefore first check if "bmap" contains any lower and/or upper bounds
2721 * It is interesting to note that the introduction of these constraints
2722 * can indeed lead to more accurate results, even when compared to
2723 * deriving constraints on the argument of "div" from constraints on "div".
2724 * Consider, for example, the set
2726 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2728 * The second constraint can be rewritten as
2730 * 2 * [(-i-2j+3)/4] + k >= 0
2732 * from which we can derive
2734 * -i - 2j + 3 >= -2k
2740 * Combined with the first constraint, we obtain
2742 * -3 <= 3 + 2k or k >= -3
2744 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2745 * the first constraint, we obtain
2747 * [(i + 2j)/4] >= [-3/4] = -1
2749 * Combining this constraint with the second constraint, we obtain
2753 static __isl_give isl_basic_map
*insert_bounds_on_div(
2754 __isl_take isl_basic_map
*bmap
, int div
)
2757 int check_lb
, check_ub
;
2764 if (isl_int_is_zero(bmap
->div
[div
][0]))
2767 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
2769 return isl_basic_map_free(bmap
);
2773 for (i
= 0; (!check_lb
|| !check_ub
) && i
< bmap
->n_ineq
; ++i
) {
2774 int s
= isl_int_sgn(bmap
->ineq
[i
][1 + v_div
+ div
]);
2781 if (!check_lb
&& !check_ub
)
2786 for (i
= 0; bmap
&& i
< bmap
->n_ineq
; ++i
) {
2787 if (!isl_int_is_zero(bmap
->ineq
[i
][1 + v_div
+ div
]))
2790 bmap
= insert_bounds_on_div_from_ineq(bmap
, div
, i
, v_div
, v
,
2791 check_lb
, check_ub
);
2799 /* Remove all divs (recursively) involving any of the given dimensions
2800 * in their definitions.
2802 __isl_give isl_basic_map
*isl_basic_map_remove_divs_involving_dims(
2803 __isl_take isl_basic_map
*bmap
,
2804 enum isl_dim_type type
, unsigned first
, unsigned n
)
2808 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2809 return isl_basic_map_free(bmap
);
2810 first
+= isl_basic_map_offset(bmap
, type
);
2812 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2815 involves
= div_involves_vars(bmap
, i
, first
, n
);
2817 return isl_basic_map_free(bmap
);
2820 bmap
= insert_bounds_on_div(bmap
, i
);
2821 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
2830 __isl_give isl_basic_set
*isl_basic_set_remove_divs_involving_dims(
2831 __isl_take isl_basic_set
*bset
,
2832 enum isl_dim_type type
, unsigned first
, unsigned n
)
2834 return isl_basic_map_remove_divs_involving_dims(bset
, type
, first
, n
);
2837 __isl_give isl_map
*isl_map_remove_divs_involving_dims(__isl_take isl_map
*map
,
2838 enum isl_dim_type type
, unsigned first
, unsigned n
)
2847 map
= isl_map_cow(map
);
2851 for (i
= 0; i
< map
->n
; ++i
) {
2852 map
->p
[i
] = isl_basic_map_remove_divs_involving_dims(map
->p
[i
],
2863 __isl_give isl_set
*isl_set_remove_divs_involving_dims(__isl_take isl_set
*set
,
2864 enum isl_dim_type type
, unsigned first
, unsigned n
)
2866 return set_from_map(isl_map_remove_divs_involving_dims(set_to_map(set
),
2870 /* Does the description of "bmap" depend on the specified dimensions?
2871 * We also check whether the dimensions appear in any of the div definitions.
2872 * In principle there is no need for this check. If the dimensions appear
2873 * in a div definition, they also appear in the defining constraints of that
2876 isl_bool
isl_basic_map_involves_dims(__isl_keep isl_basic_map
*bmap
,
2877 enum isl_dim_type type
, unsigned first
, unsigned n
)
2881 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2882 return isl_bool_error
;
2884 first
+= isl_basic_map_offset(bmap
, type
);
2885 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2886 if (isl_seq_first_non_zero(bmap
->eq
[i
] + first
, n
) >= 0)
2887 return isl_bool_true
;
2888 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2889 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + first
, n
) >= 0)
2890 return isl_bool_true
;
2891 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2892 if (isl_int_is_zero(bmap
->div
[i
][0]))
2894 if (isl_seq_first_non_zero(bmap
->div
[i
] + 1 + first
, n
) >= 0)
2895 return isl_bool_true
;
2898 return isl_bool_false
;
2901 isl_bool
isl_map_involves_dims(__isl_keep isl_map
*map
,
2902 enum isl_dim_type type
, unsigned first
, unsigned n
)
2906 if (isl_map_check_range(map
, type
, first
, n
) < 0)
2907 return isl_bool_error
;
2909 for (i
= 0; i
< map
->n
; ++i
) {
2910 isl_bool involves
= isl_basic_map_involves_dims(map
->p
[i
],
2912 if (involves
< 0 || involves
)
2916 return isl_bool_false
;
2919 isl_bool
isl_basic_set_involves_dims(__isl_keep isl_basic_set
*bset
,
2920 enum isl_dim_type type
, unsigned first
, unsigned n
)
2922 return isl_basic_map_involves_dims(bset
, type
, first
, n
);
2925 isl_bool
isl_set_involves_dims(__isl_keep isl_set
*set
,
2926 enum isl_dim_type type
, unsigned first
, unsigned n
)
2928 return isl_map_involves_dims(set
, type
, first
, n
);
2931 /* Drop all constraints in bmap that involve any of the dimensions
2932 * first to first+n-1.
2933 * This function only performs the actual removal of constraints.
2935 * This function should not call finalize since it is used by
2936 * remove_redundant_divs, which in turn is called by isl_basic_map_finalize.
2938 __isl_give isl_basic_map
*isl_basic_map_drop_constraints_involving(
2939 __isl_take isl_basic_map
*bmap
, unsigned first
, unsigned n
)
2946 bmap
= isl_basic_map_cow(bmap
);
2951 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
) {
2952 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + first
, n
) == -1)
2954 if (isl_basic_map_drop_equality(bmap
, i
) < 0)
2955 return isl_basic_map_free(bmap
);
2958 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
2959 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + 1 + first
, n
) == -1)
2961 if (isl_basic_map_drop_inequality(bmap
, i
) < 0)
2962 return isl_basic_map_free(bmap
);
2968 /* Drop all constraints in bset that involve any of the dimensions
2969 * first to first+n-1.
2970 * This function only performs the actual removal of constraints.
2972 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_involving(
2973 __isl_take isl_basic_set
*bset
, unsigned first
, unsigned n
)
2975 return isl_basic_map_drop_constraints_involving(bset
, first
, n
);
2978 /* Drop all constraints in bmap that do not involve any of the dimensions
2979 * first to first + n - 1 of the given type.
2981 __isl_give isl_basic_map
*isl_basic_map_drop_constraints_not_involving_dims(
2982 __isl_take isl_basic_map
*bmap
,
2983 enum isl_dim_type type
, unsigned first
, unsigned n
)
2988 isl_space
*space
= isl_basic_map_get_space(bmap
);
2989 isl_basic_map_free(bmap
);
2990 return isl_basic_map_universe(space
);
2992 bmap
= isl_basic_map_cow(bmap
);
2996 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2997 return isl_basic_map_free(bmap
);
2999 first
+= isl_basic_map_offset(bmap
, type
) - 1;
3001 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
) {
3002 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + first
, n
) != -1)
3004 if (isl_basic_map_drop_equality(bmap
, i
) < 0)
3005 return isl_basic_map_free(bmap
);
3008 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
3009 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + 1 + first
, n
) != -1)
3011 if (isl_basic_map_drop_inequality(bmap
, i
) < 0)
3012 return isl_basic_map_free(bmap
);
3015 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
3019 /* Drop all constraints in bset that do not involve any of the dimensions
3020 * first to first + n - 1 of the given type.
3022 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_not_involving_dims(
3023 __isl_take isl_basic_set
*bset
,
3024 enum isl_dim_type type
, unsigned first
, unsigned n
)
3026 return isl_basic_map_drop_constraints_not_involving_dims(bset
,
3030 /* Drop all constraints in bmap that involve any of the dimensions
3031 * first to first + n - 1 of the given type.
3033 __isl_give isl_basic_map
*isl_basic_map_drop_constraints_involving_dims(
3034 __isl_take isl_basic_map
*bmap
,
3035 enum isl_dim_type type
, unsigned first
, unsigned n
)
3042 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
3043 return isl_basic_map_free(bmap
);
3045 bmap
= isl_basic_map_remove_divs_involving_dims(bmap
, type
, first
, n
);
3046 first
+= isl_basic_map_offset(bmap
, type
) - 1;
3047 bmap
= isl_basic_map_drop_constraints_involving(bmap
, first
, n
);
3048 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
3052 /* Drop all constraints in bset that involve any of the dimensions
3053 * first to first + n - 1 of the given type.
3055 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_involving_dims(
3056 __isl_take isl_basic_set
*bset
,
3057 enum isl_dim_type type
, unsigned first
, unsigned n
)
3059 return isl_basic_map_drop_constraints_involving_dims(bset
,
3063 /* Drop constraints from "map" by applying "drop" to each basic map.
3065 static __isl_give isl_map
*drop_constraints(__isl_take isl_map
*map
,
3066 enum isl_dim_type type
, unsigned first
, unsigned n
,
3067 __isl_give isl_basic_map
*(*drop
)(__isl_take isl_basic_map
*bmap
,
3068 enum isl_dim_type type
, unsigned first
, unsigned n
))
3072 if (isl_map_check_range(map
, type
, first
, n
) < 0)
3073 return isl_map_free(map
);
3075 map
= isl_map_cow(map
);
3079 for (i
= 0; i
< map
->n
; ++i
) {
3080 map
->p
[i
] = drop(map
->p
[i
], type
, first
, n
);
3082 return isl_map_free(map
);
3086 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
3091 /* Drop all constraints in map that involve any of the dimensions
3092 * first to first + n - 1 of the given type.
3094 __isl_give isl_map
*isl_map_drop_constraints_involving_dims(
3095 __isl_take isl_map
*map
,
3096 enum isl_dim_type type
, unsigned first
, unsigned n
)
3100 return drop_constraints(map
, type
, first
, n
,
3101 &isl_basic_map_drop_constraints_involving_dims
);
3104 /* Drop all constraints in "map" that do not involve any of the dimensions
3105 * first to first + n - 1 of the given type.
3107 __isl_give isl_map
*isl_map_drop_constraints_not_involving_dims(
3108 __isl_take isl_map
*map
,
3109 enum isl_dim_type type
, unsigned first
, unsigned n
)
3112 isl_space
*space
= isl_map_get_space(map
);
3114 return isl_map_universe(space
);
3116 return drop_constraints(map
, type
, first
, n
,
3117 &isl_basic_map_drop_constraints_not_involving_dims
);
3120 /* Drop all constraints in set that involve any of the dimensions
3121 * first to first + n - 1 of the given type.
3123 __isl_give isl_set
*isl_set_drop_constraints_involving_dims(
3124 __isl_take isl_set
*set
,
3125 enum isl_dim_type type
, unsigned first
, unsigned n
)
3127 return isl_map_drop_constraints_involving_dims(set
, type
, first
, n
);
3130 /* Drop all constraints in "set" that do not involve any of the dimensions
3131 * first to first + n - 1 of the given type.
3133 __isl_give isl_set
*isl_set_drop_constraints_not_involving_dims(
3134 __isl_take isl_set
*set
,
3135 enum isl_dim_type type
, unsigned first
, unsigned n
)
3137 return isl_map_drop_constraints_not_involving_dims(set
, type
, first
, n
);
3140 /* Does local variable "div" of "bmap" have a complete explicit representation?
3141 * Having a complete explicit representation requires not only
3142 * an explicit representation, but also that all local variables
3143 * that appear in this explicit representation in turn have
3144 * a complete explicit representation.
3146 isl_bool
isl_basic_map_div_is_known(__isl_keep isl_basic_map
*bmap
, int div
)
3149 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
3152 marked
= isl_basic_map_div_is_marked_unknown(bmap
, div
);
3153 if (marked
< 0 || marked
)
3154 return isl_bool_not(marked
);
3156 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
3159 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
3161 known
= isl_basic_map_div_is_known(bmap
, i
);
3162 if (known
< 0 || !known
)
3166 return isl_bool_true
;
3169 /* Remove all divs that are unknown or defined in terms of unknown divs.
3171 __isl_give isl_basic_map
*isl_basic_map_remove_unknown_divs(
3172 __isl_take isl_basic_map
*bmap
)
3179 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
3180 if (isl_basic_map_div_is_known(bmap
, i
))
3182 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
3191 /* Remove all divs that are unknown or defined in terms of unknown divs.
3193 __isl_give isl_basic_set
*isl_basic_set_remove_unknown_divs(
3194 __isl_take isl_basic_set
*bset
)
3196 return isl_basic_map_remove_unknown_divs(bset
);
3199 __isl_give isl_map
*isl_map_remove_unknown_divs(__isl_take isl_map
*map
)
3208 map
= isl_map_cow(map
);
3212 for (i
= 0; i
< map
->n
; ++i
) {
3213 map
->p
[i
] = isl_basic_map_remove_unknown_divs(map
->p
[i
]);
3223 __isl_give isl_set
*isl_set_remove_unknown_divs(__isl_take isl_set
*set
)
3225 return set_from_map(isl_map_remove_unknown_divs(set_to_map(set
)));
3228 __isl_give isl_basic_set
*isl_basic_set_remove_dims(
3229 __isl_take isl_basic_set
*bset
,
3230 enum isl_dim_type type
, unsigned first
, unsigned n
)
3232 isl_basic_map
*bmap
= bset_to_bmap(bset
);
3233 bmap
= isl_basic_map_remove_dims(bmap
, type
, first
, n
);
3234 return bset_from_bmap(bmap
);
3237 __isl_give isl_map
*isl_map_remove_dims(__isl_take isl_map
*map
,
3238 enum isl_dim_type type
, unsigned first
, unsigned n
)
3245 map
= isl_map_cow(map
);
3246 if (isl_map_check_range(map
, type
, first
, n
) < 0)
3247 return isl_map_free(map
);
3249 for (i
= 0; i
< map
->n
; ++i
) {
3250 map
->p
[i
] = isl_basic_map_eliminate_vars(map
->p
[i
],
3251 isl_basic_map_offset(map
->p
[i
], type
) - 1 + first
, n
);
3255 map
= isl_map_drop(map
, type
, first
, n
);
3262 __isl_give isl_set
*isl_set_remove_dims(__isl_take isl_set
*bset
,
3263 enum isl_dim_type type
, unsigned first
, unsigned n
)
3265 return set_from_map(isl_map_remove_dims(set_to_map(bset
),
3269 /* Project out n inputs starting at first using Fourier-Motzkin */
3270 __isl_give isl_map
*isl_map_remove_inputs(__isl_take isl_map
*map
,
3271 unsigned first
, unsigned n
)
3273 return isl_map_remove_dims(map
, isl_dim_in
, first
, n
);
3276 void isl_basic_set_print_internal(__isl_keep isl_basic_set
*bset
,
3277 FILE *out
, int indent
)
3282 fprintf(out
, "null basic set\n");
3286 fprintf(out
, "%*s", indent
, "");
3287 fprintf(out
, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
3288 bset
->ref
, bset
->dim
->nparam
, bset
->dim
->n_out
,
3289 bset
->extra
, bset
->flags
);
3291 p
= isl_printer_to_file(isl_basic_set_get_ctx(bset
), out
);
3292 p
= isl_printer_set_dump(p
, 1);
3293 p
= isl_printer_set_indent(p
, indent
);
3294 p
= isl_printer_start_line(p
);
3295 p
= isl_printer_print_basic_set(p
, bset
);
3296 p
= isl_printer_end_line(p
);
3297 isl_printer_free(p
);
3300 void isl_basic_map_print_internal(__isl_keep isl_basic_map
*bmap
,
3301 FILE *out
, int indent
)
3306 fprintf(out
, "null basic map\n");
3310 fprintf(out
, "%*s", indent
, "");
3311 fprintf(out
, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
3312 "flags: %x, n_name: %d\n",
3314 bmap
->dim
->nparam
, bmap
->dim
->n_in
, bmap
->dim
->n_out
,
3315 bmap
->extra
, bmap
->flags
, bmap
->dim
->n_id
);
3317 p
= isl_printer_to_file(isl_basic_map_get_ctx(bmap
), out
);
3318 p
= isl_printer_set_dump(p
, 1);
3319 p
= isl_printer_set_indent(p
, indent
);
3320 p
= isl_printer_start_line(p
);
3321 p
= isl_printer_print_basic_map(p
, bmap
);
3322 p
= isl_printer_end_line(p
);
3323 isl_printer_free(p
);
3326 __isl_give isl_basic_map
*isl_inequality_negate(__isl_take isl_basic_map
*bmap
,
3331 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
3333 return isl_basic_map_free(bmap
);
3334 if (pos
>= bmap
->n_ineq
)
3335 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
3336 "invalid position", return isl_basic_map_free(bmap
));
3337 isl_seq_neg(bmap
->ineq
[pos
], bmap
->ineq
[pos
], 1 + total
);
3338 isl_int_sub_ui(bmap
->ineq
[pos
][0], bmap
->ineq
[pos
][0], 1);
3339 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
3340 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
3344 __isl_give isl_set
*isl_set_alloc_space(__isl_take isl_space
*space
, int n
,
3347 if (isl_space_check_is_set(space
) < 0)
3349 return isl_map_alloc_space(space
, n
, flags
);
3351 isl_space_free(space
);
3355 /* Make sure "map" has room for at least "n" more basic maps.
3357 __isl_give isl_map
*isl_map_grow(__isl_take isl_map
*map
, int n
)
3360 struct isl_map
*grown
= NULL
;
3364 isl_assert(map
->ctx
, n
>= 0, goto error
);
3365 if (map
->n
+ n
<= map
->size
)
3367 grown
= isl_map_alloc_space(isl_map_get_space(map
), map
->n
+ n
, map
->flags
);
3370 for (i
= 0; i
< map
->n
; ++i
) {
3371 grown
->p
[i
] = isl_basic_map_copy(map
->p
[i
]);
3379 isl_map_free(grown
);
3384 /* Make sure "set" has room for at least "n" more basic sets.
3386 __isl_give isl_set
*isl_set_grow(__isl_take isl_set
*set
, int n
)
3388 return set_from_map(isl_map_grow(set_to_map(set
), n
));
3391 __isl_give isl_set
*isl_set_from_basic_set(__isl_take isl_basic_set
*bset
)
3393 return isl_map_from_basic_map(bset
);
3396 __isl_give isl_map
*isl_map_from_basic_map(__isl_take isl_basic_map
*bmap
)
3398 struct isl_map
*map
;
3403 map
= isl_map_alloc_space(isl_space_copy(bmap
->dim
), 1, ISL_MAP_DISJOINT
);
3404 return isl_map_add_basic_map(map
, bmap
);
3407 __isl_give isl_set
*isl_set_add_basic_set(__isl_take isl_set
*set
,
3408 __isl_take isl_basic_set
*bset
)
3410 return set_from_map(isl_map_add_basic_map(set_to_map(set
),
3411 bset_to_bmap(bset
)));
3414 __isl_null isl_set
*isl_set_free(__isl_take isl_set
*set
)
3416 return isl_map_free(set
);
3419 void isl_set_print_internal(__isl_keep isl_set
*set
, FILE *out
, int indent
)
3424 fprintf(out
, "null set\n");
3428 fprintf(out
, "%*s", indent
, "");
3429 fprintf(out
, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
3430 set
->ref
, set
->n
, set
->dim
->nparam
, set
->dim
->n_out
,
3432 for (i
= 0; i
< set
->n
; ++i
) {
3433 fprintf(out
, "%*s", indent
, "");
3434 fprintf(out
, "basic set %d:\n", i
);
3435 isl_basic_set_print_internal(set
->p
[i
], out
, indent
+4);
3439 void isl_map_print_internal(__isl_keep isl_map
*map
, FILE *out
, int indent
)
3444 fprintf(out
, "null map\n");
3448 fprintf(out
, "%*s", indent
, "");
3449 fprintf(out
, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
3450 "flags: %x, n_name: %d\n",
3451 map
->ref
, map
->n
, map
->dim
->nparam
, map
->dim
->n_in
,
3452 map
->dim
->n_out
, map
->flags
, map
->dim
->n_id
);
3453 for (i
= 0; i
< map
->n
; ++i
) {
3454 fprintf(out
, "%*s", indent
, "");
3455 fprintf(out
, "basic map %d:\n", i
);
3456 isl_basic_map_print_internal(map
->p
[i
], out
, indent
+4);
3460 /* Check that the space of "bset" is the same as that of the domain of "bmap".
3462 static isl_stat
isl_basic_map_check_compatible_domain(
3463 __isl_keep isl_basic_map
*bmap
, __isl_keep isl_basic_set
*bset
)
3467 ok
= isl_basic_map_compatible_domain(bmap
, bset
);
3469 return isl_stat_error
;
3471 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
3472 "incompatible spaces", return isl_stat_error
);
3477 __isl_give isl_basic_map
*isl_basic_map_intersect_domain(
3478 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*bset
)
3480 struct isl_basic_map
*bmap_domain
;
3483 if (isl_basic_map_check_equal_params(bmap
, bset_to_bmap(bset
)) < 0)
3486 dim
= isl_basic_set_dim(bset
, isl_dim_set
);
3490 isl_basic_map_check_compatible_domain(bmap
, bset
) < 0)
3493 bmap
= isl_basic_map_cow(bmap
);
3496 bmap
= isl_basic_map_extend(bmap
,
3497 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
3498 bmap_domain
= isl_basic_map_from_domain(bset
);
3499 bmap
= add_constraints(bmap
, bmap_domain
, 0, 0);
3501 bmap
= isl_basic_map_simplify(bmap
);
3502 return isl_basic_map_finalize(bmap
);
3504 isl_basic_map_free(bmap
);
3505 isl_basic_set_free(bset
);
3509 /* Check that the space of "bset" is the same as that of the range of "bmap".
3511 static isl_stat
isl_basic_map_check_compatible_range(
3512 __isl_keep isl_basic_map
*bmap
, __isl_keep isl_basic_set
*bset
)
3516 ok
= isl_basic_map_compatible_range(bmap
, bset
);
3518 return isl_stat_error
;
3520 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
3521 "incompatible spaces", return isl_stat_error
);
3526 __isl_give isl_basic_map
*isl_basic_map_intersect_range(
3527 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*bset
)
3529 struct isl_basic_map
*bmap_range
;
3532 if (isl_basic_map_check_equal_params(bmap
, bset_to_bmap(bset
)) < 0)
3535 dim
= isl_basic_set_dim(bset
, isl_dim_set
);
3538 if (dim
!= 0 && isl_basic_map_check_compatible_range(bmap
, bset
) < 0)
3541 if (isl_basic_set_plain_is_universe(bset
)) {
3542 isl_basic_set_free(bset
);
3546 bmap
= isl_basic_map_cow(bmap
);
3549 bmap
= isl_basic_map_extend(bmap
,
3550 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
3551 bmap_range
= bset_to_bmap(bset
);
3552 bmap
= add_constraints(bmap
, bmap_range
, 0, 0);
3554 bmap
= isl_basic_map_simplify(bmap
);
3555 return isl_basic_map_finalize(bmap
);
3557 isl_basic_map_free(bmap
);
3558 isl_basic_set_free(bset
);
3562 isl_bool
isl_basic_map_contains(__isl_keep isl_basic_map
*bmap
,
3563 __isl_keep isl_vec
*vec
)
3569 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
3570 if (total
< 0 || !vec
)
3571 return isl_bool_error
;
3573 if (1 + total
!= vec
->size
)
3574 return isl_bool_false
;
3578 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
3579 isl_seq_inner_product(vec
->el
, bmap
->eq
[i
], 1 + total
, &s
);
3580 if (!isl_int_is_zero(s
)) {
3582 return isl_bool_false
;
3586 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
3587 isl_seq_inner_product(vec
->el
, bmap
->ineq
[i
], 1 + total
, &s
);
3588 if (isl_int_is_neg(s
)) {
3590 return isl_bool_false
;
3596 return isl_bool_true
;
3599 isl_bool
isl_basic_set_contains(__isl_keep isl_basic_set
*bset
,
3600 __isl_keep isl_vec
*vec
)
3602 return isl_basic_map_contains(bset_to_bmap(bset
), vec
);
3605 __isl_give isl_basic_map
*isl_basic_map_intersect(
3606 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
3608 struct isl_vec
*sample
= NULL
;
3609 isl_space
*space1
, *space2
;
3610 isl_size dim1
, dim2
, nparam1
, nparam2
;
3612 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
3614 space1
= isl_basic_map_peek_space(bmap1
);
3615 space2
= isl_basic_map_peek_space(bmap2
);
3616 dim1
= isl_space_dim(space1
, isl_dim_all
);
3617 dim2
= isl_space_dim(space2
, isl_dim_all
);
3618 nparam1
= isl_space_dim(space1
, isl_dim_param
);
3619 nparam2
= isl_space_dim(space2
, isl_dim_param
);
3620 if (dim1
< 0 || dim2
< 0 || nparam1
< 0 || nparam2
< 0)
3622 if (dim1
== nparam1
&& dim2
!= nparam2
)
3623 return isl_basic_map_intersect(bmap2
, bmap1
);
3625 if (dim2
!= nparam2
&&
3626 isl_basic_map_check_equal_space(bmap1
, bmap2
) < 0)
3629 if (isl_basic_map_plain_is_empty(bmap1
)) {
3630 isl_basic_map_free(bmap2
);
3633 if (isl_basic_map_plain_is_empty(bmap2
)) {
3634 isl_basic_map_free(bmap1
);
3638 if (bmap1
->sample
&&
3639 isl_basic_map_contains(bmap1
, bmap1
->sample
) > 0 &&
3640 isl_basic_map_contains(bmap2
, bmap1
->sample
) > 0)
3641 sample
= isl_vec_copy(bmap1
->sample
);
3642 else if (bmap2
->sample
&&
3643 isl_basic_map_contains(bmap1
, bmap2
->sample
) > 0 &&
3644 isl_basic_map_contains(bmap2
, bmap2
->sample
) > 0)
3645 sample
= isl_vec_copy(bmap2
->sample
);
3647 bmap1
= isl_basic_map_cow(bmap1
);
3650 bmap1
= isl_basic_map_extend(bmap1
,
3651 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
3652 bmap1
= add_constraints(bmap1
, bmap2
, 0, 0);
3655 isl_vec_free(sample
);
3657 isl_vec_free(bmap1
->sample
);
3658 bmap1
->sample
= sample
;
3661 bmap1
= isl_basic_map_simplify(bmap1
);
3662 return isl_basic_map_finalize(bmap1
);
3665 isl_vec_free(sample
);
3666 isl_basic_map_free(bmap1
);
3667 isl_basic_map_free(bmap2
);
3671 __isl_give isl_basic_set
*isl_basic_set_intersect(
3672 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
3674 return bset_from_bmap(isl_basic_map_intersect(bset_to_bmap(bset1
),
3675 bset_to_bmap(bset2
)));
3678 __isl_give isl_basic_set
*isl_basic_set_intersect_params(
3679 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
3681 return isl_basic_set_intersect(bset1
, bset2
);
3684 /* Special case of isl_map_intersect, where both map1 and map2
3685 * are convex, without any divs and such that either map1 or map2
3686 * contains a single constraint. This constraint is then simply
3687 * added to the other map.
3689 static __isl_give isl_map
*map_intersect_add_constraint(
3690 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
3692 isl_assert(map1
->ctx
, map1
->n
== 1, goto error
);
3693 isl_assert(map2
->ctx
, map1
->n
== 1, goto error
);
3694 isl_assert(map1
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
3695 isl_assert(map2
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
3697 if (map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
!= 1)
3698 return isl_map_intersect(map2
, map1
);
3700 map1
= isl_map_cow(map1
);
3703 if (isl_map_plain_is_empty(map1
)) {
3707 if (map2
->p
[0]->n_eq
== 1)
3708 map1
->p
[0] = isl_basic_map_add_eq(map1
->p
[0], map2
->p
[0]->eq
[0]);
3710 map1
->p
[0] = isl_basic_map_add_ineq(map1
->p
[0],
3711 map2
->p
[0]->ineq
[0]);
3713 map1
->p
[0] = isl_basic_map_simplify(map1
->p
[0]);
3714 map1
->p
[0] = isl_basic_map_finalize(map1
->p
[0]);
3718 if (isl_basic_map_plain_is_empty(map1
->p
[0])) {
3719 isl_basic_map_free(map1
->p
[0]);
3725 map1
= isl_map_unmark_normalized(map1
);
3733 /* map2 may be either a parameter domain or a map living in the same
3736 static __isl_give isl_map
*map_intersect_internal(__isl_take isl_map
*map1
,
3737 __isl_take isl_map
*map2
)
3743 isl_size dim2
, nparam2
;
3748 if ((isl_map_plain_is_empty(map1
) ||
3749 isl_map_plain_is_universe(map2
)) &&
3750 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
3754 if ((isl_map_plain_is_empty(map2
) ||
3755 isl_map_plain_is_universe(map1
)) &&
3756 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
3761 if (map1
->n
== 1 && map2
->n
== 1 &&
3762 map1
->p
[0]->n_div
== 0 && map2
->p
[0]->n_div
== 0 &&
3763 isl_space_is_equal(map1
->dim
, map2
->dim
) &&
3764 (map1
->p
[0]->n_eq
+ map1
->p
[0]->n_ineq
== 1 ||
3765 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1))
3766 return map_intersect_add_constraint(map1
, map2
);
3768 equal
= isl_map_plain_is_equal(map1
, map2
);
3776 dim2
= isl_map_dim(map2
, isl_dim_all
);
3777 nparam2
= isl_map_dim(map2
, isl_dim_param
);
3778 if (dim2
< 0 || nparam2
< 0)
3780 if (dim2
!= nparam2
)
3781 isl_assert(map1
->ctx
,
3782 isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
3784 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
3785 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
3786 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
3788 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
3789 map1
->n
* map2
->n
, flags
);
3792 for (i
= 0; i
< map1
->n
; ++i
)
3793 for (j
= 0; j
< map2
->n
; ++j
) {
3794 struct isl_basic_map
*part
;
3795 part
= isl_basic_map_intersect(
3796 isl_basic_map_copy(map1
->p
[i
]),
3797 isl_basic_map_copy(map2
->p
[j
]));
3798 if (isl_basic_map_is_empty(part
) < 0)
3799 part
= isl_basic_map_free(part
);
3800 result
= isl_map_add_basic_map(result
, part
);
3813 static __isl_give isl_map
*map_intersect(__isl_take isl_map
*map1
,
3814 __isl_take isl_map
*map2
)
3816 if (isl_map_check_equal_space(map1
, map2
) < 0)
3818 return map_intersect_internal(map1
, map2
);
3825 __isl_give isl_map
*isl_map_intersect(__isl_take isl_map
*map1
,
3826 __isl_take isl_map
*map2
)
3828 isl_map_align_params_bin(&map1
, &map2
);
3829 return map_intersect(map1
, map2
);
3832 __isl_give isl_set
*isl_set_intersect(__isl_take isl_set
*set1
,
3833 __isl_take isl_set
*set2
)
3835 return set_from_map(isl_map_intersect(set_to_map(set1
),
3839 /* map_intersect_internal accepts intersections
3840 * with parameter domains, so we can just call that function.
3842 __isl_give isl_map
*isl_map_intersect_params(__isl_take isl_map
*map
,
3843 __isl_take isl_set
*params
)
3845 isl_map_align_params_set(&map
, ¶ms
);
3846 return map_intersect_internal(map
, params
);
3849 __isl_give isl_set
*isl_set_intersect_params(__isl_take isl_set
*set
,
3850 __isl_take isl_set
*params
)
3852 return isl_map_intersect_params(set
, params
);
3855 __isl_give isl_basic_map
*isl_basic_map_reverse(__isl_take isl_basic_map
*bmap
)
3863 bmap
= isl_basic_map_cow(bmap
);
3866 space
= isl_space_reverse(isl_space_copy(bmap
->dim
));
3867 pos
= isl_basic_map_offset(bmap
, isl_dim_in
);
3868 n1
= isl_basic_map_dim(bmap
, isl_dim_in
);
3869 n2
= isl_basic_map_dim(bmap
, isl_dim_out
);
3870 if (n1
< 0 || n2
< 0)
3871 bmap
= isl_basic_map_free(bmap
);
3872 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
3873 return isl_basic_map_reset_space(bmap
, space
);
3876 /* Given a basic map A -> (B -> C), return the corresponding basic map
3879 static __isl_give isl_basic_map
*isl_basic_map_range_reverse(
3880 __isl_take isl_basic_map
*bmap
)
3883 isl_size offset
, n1
, n2
;
3885 space
= isl_basic_map_peek_space(bmap
);
3886 if (isl_space_check_range_is_wrapping(space
) < 0)
3887 return isl_basic_map_free(bmap
);
3888 offset
= isl_basic_map_var_offset(bmap
, isl_dim_out
);
3889 n1
= isl_space_wrapped_dim(space
, isl_dim_out
, isl_dim_in
);
3890 n2
= isl_space_wrapped_dim(space
, isl_dim_out
, isl_dim_out
);
3891 if (offset
< 0 || n1
< 0 || n2
< 0)
3892 return isl_basic_map_free(bmap
);
3894 bmap
= isl_basic_map_swap_vars(bmap
, 1 + offset
, n1
, n2
);
3896 space
= isl_basic_map_take_space(bmap
);
3897 space
= isl_space_range_reverse(space
);
3898 bmap
= isl_basic_map_restore_space(bmap
, space
);
3903 static __isl_give isl_basic_map
*basic_map_space_reset(
3904 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
)
3910 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
3913 space
= isl_basic_map_get_space(bmap
);
3914 space
= isl_space_reset(space
, type
);
3915 bmap
= isl_basic_map_reset_space(bmap
, space
);
3919 __isl_give isl_basic_map
*isl_basic_map_insert_dims(
3920 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
3921 unsigned pos
, unsigned n
)
3923 isl_bool rational
, is_empty
;
3924 isl_space
*res_space
;
3925 struct isl_basic_map
*res
;
3926 struct isl_dim_map
*dim_map
;
3929 enum isl_dim_type t
;
3932 return basic_map_space_reset(bmap
, type
);
3934 is_empty
= isl_basic_map_plain_is_empty(bmap
);
3935 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
3936 if (is_empty
< 0 || total
< 0)
3937 return isl_basic_map_free(bmap
);
3938 res_space
= isl_space_insert_dims(isl_basic_map_get_space(bmap
),
3941 return isl_basic_map_free(bmap
);
3943 isl_basic_map_free(bmap
);
3944 return isl_basic_map_empty(res_space
);
3947 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
+ n
);
3949 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
3953 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
3955 isl_size size
= isl_basic_map_dim(bmap
, t
);
3957 dim_map
= isl_dim_map_free(dim_map
);
3958 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3960 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
3961 pos
, size
- pos
, off
+ pos
+ n
);
3963 dim
= isl_space_dim(res_space
, t
);
3965 dim_map
= isl_dim_map_free(dim_map
);
3968 isl_dim_map_div(dim_map
, bmap
, off
);
3970 res
= isl_basic_map_alloc_space(res_space
,
3971 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
3972 rational
= isl_basic_map_is_rational(bmap
);
3974 res
= isl_basic_map_free(res
);
3976 res
= isl_basic_map_set_rational(res
);
3977 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
3978 return isl_basic_map_finalize(res
);
3981 __isl_give isl_basic_set
*isl_basic_set_insert_dims(
3982 __isl_take isl_basic_set
*bset
,
3983 enum isl_dim_type type
, unsigned pos
, unsigned n
)
3985 return isl_basic_map_insert_dims(bset
, type
, pos
, n
);
3988 __isl_give isl_basic_map
*isl_basic_map_add_dims(__isl_take isl_basic_map
*bmap
,
3989 enum isl_dim_type type
, unsigned n
)
3993 dim
= isl_basic_map_dim(bmap
, type
);
3995 return isl_basic_map_free(bmap
);
3996 return isl_basic_map_insert_dims(bmap
, type
, dim
, n
);
3999 __isl_give isl_basic_set
*isl_basic_set_add_dims(__isl_take isl_basic_set
*bset
,
4000 enum isl_dim_type type
, unsigned n
)
4004 isl_assert(bset
->ctx
, type
!= isl_dim_in
, goto error
);
4005 return isl_basic_map_add_dims(bset
, type
, n
);
4007 isl_basic_set_free(bset
);
4011 static __isl_give isl_map
*map_space_reset(__isl_take isl_map
*map
,
4012 enum isl_dim_type type
)
4016 if (!map
|| !isl_space_is_named_or_nested(map
->dim
, type
))
4019 space
= isl_map_get_space(map
);
4020 space
= isl_space_reset(space
, type
);
4021 map
= isl_map_reset_space(map
, space
);
4025 __isl_give isl_map
*isl_map_insert_dims(__isl_take isl_map
*map
,
4026 enum isl_dim_type type
, unsigned pos
, unsigned n
)
4032 return map_space_reset(map
, type
);
4034 map
= isl_map_cow(map
);
4038 for (i
= 0; i
< map
->n
; ++i
) {
4039 map
->p
[i
] = isl_basic_map_insert_dims(map
->p
[i
], type
, pos
, n
);
4044 space
= isl_map_take_space(map
);
4045 space
= isl_space_insert_dims(space
, type
, pos
, n
);
4046 map
= isl_map_restore_space(map
, space
);
4054 __isl_give isl_set
*isl_set_insert_dims(__isl_take isl_set
*set
,
4055 enum isl_dim_type type
, unsigned pos
, unsigned n
)
4057 return isl_map_insert_dims(set
, type
, pos
, n
);
4060 __isl_give isl_map
*isl_map_add_dims(__isl_take isl_map
*map
,
4061 enum isl_dim_type type
, unsigned n
)
4065 dim
= isl_map_dim(map
, type
);
4067 return isl_map_free(map
);
4068 return isl_map_insert_dims(map
, type
, dim
, n
);
4071 __isl_give isl_set
*isl_set_add_dims(__isl_take isl_set
*set
,
4072 enum isl_dim_type type
, unsigned n
)
4076 isl_assert(set
->ctx
, type
!= isl_dim_in
, goto error
);
4077 return set_from_map(isl_map_add_dims(set_to_map(set
), type
, n
));
4083 __isl_give isl_basic_map
*isl_basic_map_move_dims(
4084 __isl_take isl_basic_map
*bmap
,
4085 enum isl_dim_type dst_type
, unsigned dst_pos
,
4086 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
4089 struct isl_dim_map
*dim_map
;
4090 struct isl_basic_map
*res
;
4091 enum isl_dim_type t
;
4098 bmap
= isl_basic_map_reset(bmap
, src_type
);
4099 bmap
= isl_basic_map_reset(bmap
, dst_type
);
4103 if (isl_basic_map_check_range(bmap
, src_type
, src_pos
, n
) < 0)
4104 return isl_basic_map_free(bmap
);
4106 if (dst_type
== src_type
&& dst_pos
== src_pos
)
4109 isl_assert(bmap
->ctx
, dst_type
!= src_type
, goto error
);
4111 if (pos(bmap
->dim
, dst_type
) + dst_pos
==
4112 pos(bmap
->dim
, src_type
) + src_pos
+
4113 ((src_type
< dst_type
) ? n
: 0)) {
4114 space
= isl_basic_map_take_space(bmap
);
4115 space
= isl_space_move_dims(space
, dst_type
, dst_pos
,
4116 src_type
, src_pos
, n
);
4117 bmap
= isl_basic_map_restore_space(bmap
, space
);
4118 bmap
= isl_basic_map_finalize(bmap
);
4123 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4125 return isl_basic_map_free(bmap
);
4126 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
4129 space
= isl_basic_map_peek_space(bmap
);
4130 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
4131 isl_size size
= isl_space_dim(space
, t
);
4133 dim_map
= isl_dim_map_free(dim_map
);
4134 if (t
== dst_type
) {
4135 isl_dim_map_dim_range(dim_map
, space
, t
,
4138 isl_dim_map_dim_range(dim_map
, space
, src_type
,
4141 isl_dim_map_dim_range(dim_map
, space
, t
,
4142 dst_pos
, size
- dst_pos
, off
);
4143 off
+= size
- dst_pos
;
4144 } else if (t
== src_type
) {
4145 isl_dim_map_dim_range(dim_map
, space
, t
,
4148 isl_dim_map_dim_range(dim_map
, space
, t
,
4149 src_pos
+ n
, size
- src_pos
- n
, off
);
4150 off
+= size
- src_pos
- n
;
4152 isl_dim_map_dim(dim_map
, space
, t
, off
);
4156 isl_dim_map_div(dim_map
, bmap
, off
);
4158 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
4159 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
4160 bmap
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
4161 space
= isl_basic_map_take_space(bmap
);
4162 space
= isl_space_move_dims(space
, dst_type
, dst_pos
,
4163 src_type
, src_pos
, n
);
4164 bmap
= isl_basic_map_restore_space(bmap
, space
);
4168 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
4169 bmap
= isl_basic_map_gauss(bmap
, NULL
);
4170 bmap
= isl_basic_map_finalize(bmap
);
4174 isl_basic_map_free(bmap
);
4178 __isl_give isl_basic_set
*isl_basic_set_move_dims(__isl_take isl_basic_set
*bset
,
4179 enum isl_dim_type dst_type
, unsigned dst_pos
,
4180 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
4182 isl_basic_map
*bmap
= bset_to_bmap(bset
);
4183 bmap
= isl_basic_map_move_dims(bmap
, dst_type
, dst_pos
,
4184 src_type
, src_pos
, n
);
4185 return bset_from_bmap(bmap
);
4188 __isl_give isl_set
*isl_set_move_dims(__isl_take isl_set
*set
,
4189 enum isl_dim_type dst_type
, unsigned dst_pos
,
4190 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
4194 isl_assert(set
->ctx
, dst_type
!= isl_dim_in
, goto error
);
4195 return set_from_map(isl_map_move_dims(set_to_map(set
),
4196 dst_type
, dst_pos
, src_type
, src_pos
, n
));
4202 __isl_give isl_map
*isl_map_move_dims(__isl_take isl_map
*map
,
4203 enum isl_dim_type dst_type
, unsigned dst_pos
,
4204 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
4210 map
= isl_map_reset(map
, src_type
);
4211 map
= isl_map_reset(map
, dst_type
);
4215 if (isl_map_check_range(map
, src_type
, src_pos
, n
))
4216 return isl_map_free(map
);
4218 if (dst_type
== src_type
&& dst_pos
== src_pos
)
4221 isl_assert(map
->ctx
, dst_type
!= src_type
, goto error
);
4223 map
= isl_map_cow(map
);
4227 for (i
= 0; i
< map
->n
; ++i
) {
4228 map
->p
[i
] = isl_basic_map_move_dims(map
->p
[i
],
4230 src_type
, src_pos
, n
);
4235 space
= isl_map_take_space(map
);
4236 space
= isl_space_move_dims(space
, dst_type
, dst_pos
,
4237 src_type
, src_pos
, n
);
4238 map
= isl_map_restore_space(map
, space
);
4246 /* Move the specified dimensions to the last columns right before
4247 * the divs. Don't change the dimension specification of bmap.
4248 * That's the responsibility of the caller.
4250 static __isl_give isl_basic_map
*move_last(__isl_take isl_basic_map
*bmap
,
4251 enum isl_dim_type type
, unsigned first
, unsigned n
)
4254 struct isl_dim_map
*dim_map
;
4255 struct isl_basic_map
*res
;
4256 enum isl_dim_type t
;
4262 if (isl_basic_map_offset(bmap
, type
) + first
+ n
==
4263 isl_basic_map_offset(bmap
, isl_dim_div
))
4266 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4268 return isl_basic_map_free(bmap
);
4269 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
4272 space
= isl_basic_map_peek_space(bmap
);
4273 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
4274 isl_size size
= isl_space_dim(space
, t
);
4276 dim_map
= isl_dim_map_free(dim_map
);
4278 isl_dim_map_dim_range(dim_map
, space
, t
,
4281 isl_dim_map_dim_range(dim_map
, space
, t
,
4282 first
, n
, total
- bmap
->n_div
- n
);
4283 isl_dim_map_dim_range(dim_map
, space
, t
,
4284 first
+ n
, size
- (first
+ n
), off
);
4285 off
+= size
- (first
+ n
);
4287 isl_dim_map_dim(dim_map
, space
, t
, off
);
4291 isl_dim_map_div(dim_map
, bmap
, off
+ n
);
4293 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
4294 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
4295 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
4299 /* Insert "n" rows in the divs of "bmap".
4301 * The number of columns is not changed, which means that the last
4302 * dimensions of "bmap" are being reintepreted as the new divs.
4303 * The space of "bmap" is not adjusted, however, which means
4304 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
4305 * from the space of "bmap" is the responsibility of the caller.
4307 static __isl_give isl_basic_map
*insert_div_rows(__isl_take isl_basic_map
*bmap
,
4315 bmap
= isl_basic_map_cow(bmap
);
4319 row_size
= isl_basic_map_offset(bmap
, isl_dim_div
) + bmap
->extra
;
4320 old
= bmap
->block2
.data
;
4321 bmap
->block2
= isl_blk_extend(bmap
->ctx
, bmap
->block2
,
4322 (bmap
->extra
+ n
) * (1 + row_size
));
4323 if (!bmap
->block2
.data
)
4324 return isl_basic_map_free(bmap
);
4325 new_div
= isl_alloc_array(bmap
->ctx
, isl_int
*, bmap
->extra
+ n
);
4327 return isl_basic_map_free(bmap
);
4328 for (i
= 0; i
< n
; ++i
) {
4329 new_div
[i
] = bmap
->block2
.data
+
4330 (bmap
->extra
+ i
) * (1 + row_size
);
4331 isl_seq_clr(new_div
[i
], 1 + row_size
);
4333 for (i
= 0; i
< bmap
->extra
; ++i
)
4334 new_div
[n
+ i
] = bmap
->block2
.data
+ (bmap
->div
[i
] - old
);
4336 bmap
->div
= new_div
;
4343 /* Drop constraints from "bmap" that only involve the variables
4344 * of "type" in the range [first, first + n] that are not related
4345 * to any of the variables outside that interval.
4346 * These constraints cannot influence the values for the variables
4347 * outside the interval, except in case they cause "bmap" to be empty.
4348 * Only drop the constraints if "bmap" is known to be non-empty.
4350 static __isl_give isl_basic_map
*drop_irrelevant_constraints(
4351 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
4352 unsigned first
, unsigned n
)
4356 isl_size dim
, n_div
;
4359 non_empty
= isl_basic_map_plain_is_non_empty(bmap
);
4361 return isl_basic_map_free(bmap
);
4365 dim
= isl_basic_map_dim(bmap
, isl_dim_all
);
4366 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4367 if (dim
< 0 || n_div
< 0)
4368 return isl_basic_map_free(bmap
);
4369 groups
= isl_calloc_array(isl_basic_map_get_ctx(bmap
), int, dim
);
4371 return isl_basic_map_free(bmap
);
4372 first
+= isl_basic_map_offset(bmap
, type
) - 1;
4373 for (i
= 0; i
< first
; ++i
)
4375 for (i
= first
+ n
; i
< dim
- n_div
; ++i
)
4378 bmap
= isl_basic_map_drop_unrelated_constraints(bmap
, groups
);
4383 /* Turn the n dimensions of type type, starting at first
4384 * into existentially quantified variables.
4386 * If a subset of the projected out variables are unrelated
4387 * to any of the variables that remain, then the constraints
4388 * involving this subset are simply dropped first.
4390 __isl_give isl_basic_map
*isl_basic_map_project_out(
4391 __isl_take isl_basic_map
*bmap
,
4392 enum isl_dim_type type
, unsigned first
, unsigned n
)
4398 return basic_map_space_reset(bmap
, type
);
4399 if (type
== isl_dim_div
)
4400 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
4401 "cannot project out existentially quantified variables",
4402 return isl_basic_map_free(bmap
));
4404 empty
= isl_basic_map_plain_is_empty(bmap
);
4406 return isl_basic_map_free(bmap
);
4408 bmap
= isl_basic_map_set_to_empty(bmap
);
4410 bmap
= drop_irrelevant_constraints(bmap
, type
, first
, n
);
4414 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
4415 return isl_basic_map_remove_dims(bmap
, type
, first
, n
);
4417 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
4418 return isl_basic_map_free(bmap
);
4420 bmap
= move_last(bmap
, type
, first
, n
);
4421 bmap
= isl_basic_map_cow(bmap
);
4422 bmap
= insert_div_rows(bmap
, n
);
4424 space
= isl_basic_map_take_space(bmap
);
4425 space
= isl_space_drop_dims(space
, type
, first
, n
);
4426 bmap
= isl_basic_map_restore_space(bmap
, space
);
4427 bmap
= isl_basic_map_simplify(bmap
);
4428 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
4429 return isl_basic_map_finalize(bmap
);
4432 /* Turn the n dimensions of type type, starting at first
4433 * into existentially quantified variables.
4435 __isl_give isl_basic_set
*isl_basic_set_project_out(
4436 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
,
4437 unsigned first
, unsigned n
)
4439 return bset_from_bmap(isl_basic_map_project_out(bset_to_bmap(bset
),
4443 /* Turn the n dimensions of type type, starting at first
4444 * into existentially quantified variables.
4446 __isl_give isl_map
*isl_map_project_out(__isl_take isl_map
*map
,
4447 enum isl_dim_type type
, unsigned first
, unsigned n
)
4453 return map_space_reset(map
, type
);
4455 if (isl_map_check_range(map
, type
, first
, n
) < 0)
4456 return isl_map_free(map
);
4458 map
= isl_map_cow(map
);
4462 for (i
= 0; i
< map
->n
; ++i
) {
4463 map
->p
[i
] = isl_basic_map_project_out(map
->p
[i
], type
, first
, n
);
4469 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
4470 map
= isl_map_unmark_normalized(map
);
4472 space
= isl_map_take_space(map
);
4473 space
= isl_space_drop_dims(space
, type
, first
, n
);
4474 map
= isl_map_restore_space(map
, space
);
4483 #define TYPE isl_map
4484 #include "isl_project_out_all_params_templ.c"
4486 /* Turn all the dimensions of type "type", except the "n" starting at "first"
4487 * into existentially quantified variables.
4489 __isl_give isl_map
*isl_map_project_onto(__isl_take isl_map
*map
,
4490 enum isl_dim_type type
, unsigned first
, unsigned n
)
4494 dim
= isl_map_dim(map
, type
);
4495 if (isl_map_check_range(map
, type
, first
, n
) < 0 || dim
< 0)
4496 return isl_map_free(map
);
4497 map
= isl_map_project_out(map
, type
, first
+ n
, dim
- (first
+ n
));
4498 map
= isl_map_project_out(map
, type
, 0, first
);
4502 /* Turn the n dimensions of type type, starting at first
4503 * into existentially quantified variables.
4505 __isl_give isl_set
*isl_set_project_out(__isl_take isl_set
*set
,
4506 enum isl_dim_type type
, unsigned first
, unsigned n
)
4508 return set_from_map(isl_map_project_out(set_to_map(set
),
4512 /* If "set" involves a parameter with identifier "id",
4513 * then turn it into an existentially quantified variable.
4515 __isl_give isl_set
*isl_set_project_out_param_id(__isl_take isl_set
*set
,
4516 __isl_take isl_id
*id
)
4522 pos
= isl_set_find_dim_by_id(set
, isl_dim_param
, id
);
4526 return isl_set_project_out(set
, isl_dim_param
, pos
, 1);
4533 /* If "set" involves any of the parameters with identifiers in "list",
4534 * then turn them into existentially quantified variables.
4536 __isl_give isl_set
*isl_set_project_out_param_id_list(__isl_take isl_set
*set
,
4537 __isl_take isl_id_list
*list
)
4542 n
= isl_id_list_size(list
);
4545 for (i
= 0; i
< n
; ++i
) {
4548 id
= isl_id_list_get_at(list
, i
);
4549 set
= isl_set_project_out_param_id(set
, id
);
4552 isl_id_list_free(list
);
4555 isl_id_list_free(list
);
4560 /* Project out all parameters from "set" by existentially quantifying
4563 __isl_give isl_set
*isl_set_project_out_all_params(__isl_take isl_set
*set
)
4565 return set_from_map(isl_map_project_out_all_params(set_to_map(set
)));
4568 /* Return a map that projects the elements in "set" onto their
4569 * "n" set dimensions starting at "first".
4570 * "type" should be equal to isl_dim_set.
4572 __isl_give isl_map
*isl_set_project_onto_map(__isl_take isl_set
*set
,
4573 enum isl_dim_type type
, unsigned first
, unsigned n
)
4578 if (type
!= isl_dim_set
)
4579 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
4580 "only set dimensions can be projected out", goto error
);
4581 if (isl_set_check_range(set
, type
, first
, n
) < 0)
4582 return isl_set_free(set
);
4584 map
= isl_map_from_domain(set
);
4585 map
= isl_map_add_dims(map
, isl_dim_out
, n
);
4586 for (i
= 0; i
< n
; ++i
)
4587 map
= isl_map_equate(map
, isl_dim_in
, first
+ i
,
4595 static __isl_give isl_basic_map
*add_divs(__isl_take isl_basic_map
*bmap
,
4601 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4603 return isl_basic_map_free(bmap
);
4604 for (i
= 0; i
< n
; ++i
) {
4605 j
= isl_basic_map_alloc_div(bmap
);
4608 isl_seq_clr(bmap
->div
[j
], 1 + 1 + total
);
4612 isl_basic_map_free(bmap
);
4616 /* Does "bmap2" apply to the range of "bmap1" (ignoring parameters)?
4618 isl_bool
isl_basic_map_applies_range(__isl_keep isl_basic_map
*bmap1
,
4619 __isl_keep isl_basic_map
*bmap2
)
4621 isl_space
*space1
, *space2
;
4623 space1
= isl_basic_map_peek_space(bmap1
);
4624 space2
= isl_basic_map_peek_space(bmap2
);
4625 return isl_space_tuple_is_equal(space1
, isl_dim_out
,
4626 space2
, isl_dim_in
);
4629 /* Check that "bmap2" applies to the range of "bmap1" (ignoring parameters).
4631 static isl_stat
isl_basic_map_check_applies_range(
4632 __isl_keep isl_basic_map
*bmap1
, __isl_keep isl_basic_map
*bmap2
)
4636 equal
= isl_basic_map_applies_range(bmap1
, bmap2
);
4638 return isl_stat_error
;
4640 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
4641 "spaces don't match", return isl_stat_error
);
4645 __isl_give isl_basic_map
*isl_basic_map_apply_range(
4646 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
4648 isl_space
*space_result
= NULL
;
4649 struct isl_basic_map
*bmap
;
4650 isl_size n_in
, n_out
, n
, nparam
;
4651 unsigned total
, pos
;
4652 struct isl_dim_map
*dim_map1
, *dim_map2
;
4654 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
4656 if (isl_basic_map_check_applies_range(bmap1
, bmap2
) < 0)
4659 n_in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
4660 n_out
= isl_basic_map_dim(bmap2
, isl_dim_out
);
4661 n
= isl_basic_map_dim(bmap1
, isl_dim_out
);
4662 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
4663 if (n_in
< 0 || n_out
< 0 || n
< 0 || nparam
< 0)
4666 space_result
= isl_space_join(isl_basic_map_get_space(bmap1
),
4667 isl_basic_map_get_space(bmap2
));
4669 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ n
;
4670 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4671 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4672 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
4673 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
4674 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
4675 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_in
);
4676 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_out
);
4677 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
4678 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
4679 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
4681 bmap
= isl_basic_map_alloc_space(space_result
,
4682 bmap1
->n_div
+ bmap2
->n_div
+ n
,
4683 bmap1
->n_eq
+ bmap2
->n_eq
,
4684 bmap1
->n_ineq
+ bmap2
->n_ineq
);
4685 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
4686 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
4687 bmap
= add_divs(bmap
, n
);
4688 bmap
= isl_basic_map_simplify(bmap
);
4689 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
4690 return isl_basic_map_finalize(bmap
);
4692 isl_basic_map_free(bmap1
);
4693 isl_basic_map_free(bmap2
);
4697 __isl_give isl_basic_set
*isl_basic_set_apply(__isl_take isl_basic_set
*bset
,
4698 __isl_take isl_basic_map
*bmap
)
4700 if (isl_basic_map_check_compatible_domain(bmap
, bset
) < 0)
4703 return bset_from_bmap(isl_basic_map_apply_range(bset_to_bmap(bset
),
4706 isl_basic_set_free(bset
);
4707 isl_basic_map_free(bmap
);
4711 __isl_give isl_basic_map
*isl_basic_map_apply_domain(
4712 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
4714 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
4716 if (!isl_space_tuple_is_equal(bmap1
->dim
, isl_dim_in
,
4717 bmap2
->dim
, isl_dim_in
))
4718 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
4719 "spaces don't match", goto error
);
4721 bmap1
= isl_basic_map_reverse(bmap1
);
4722 bmap1
= isl_basic_map_apply_range(bmap1
, bmap2
);
4723 return isl_basic_map_reverse(bmap1
);
4725 isl_basic_map_free(bmap1
);
4726 isl_basic_map_free(bmap2
);
4730 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
4731 * A \cap B -> f(A) + f(B)
4733 __isl_give isl_basic_map
*isl_basic_map_sum(__isl_take isl_basic_map
*bmap1
,
4734 __isl_take isl_basic_map
*bmap2
)
4736 isl_size n_in
, n_out
, nparam
;
4737 unsigned total
, pos
;
4738 struct isl_basic_map
*bmap
= NULL
;
4739 struct isl_dim_map
*dim_map1
, *dim_map2
;
4742 if (isl_basic_map_check_equal_space(bmap1
, bmap2
) < 0)
4745 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
4746 n_in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
4747 n_out
= isl_basic_map_dim(bmap1
, isl_dim_out
);
4748 if (nparam
< 0 || n_in
< 0 || n_out
< 0)
4751 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
;
4752 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4753 dim_map2
= isl_dim_map_alloc(bmap2
->ctx
, total
);
4754 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
4755 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
);
4756 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
4757 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
4758 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_in
+ n_out
);
4759 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
4760 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
4761 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_out
);
4763 bmap
= isl_basic_map_alloc_space(isl_space_copy(bmap1
->dim
),
4764 bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
,
4765 bmap1
->n_eq
+ bmap2
->n_eq
+ n_out
,
4766 bmap1
->n_ineq
+ bmap2
->n_ineq
);
4767 for (i
= 0; i
< n_out
; ++i
) {
4768 int j
= isl_basic_map_alloc_equality(bmap
);
4771 isl_seq_clr(bmap
->eq
[j
], 1+total
);
4772 isl_int_set_si(bmap
->eq
[j
][1+nparam
+n_in
+i
], -1);
4773 isl_int_set_si(bmap
->eq
[j
][1+pos
+i
], 1);
4774 isl_int_set_si(bmap
->eq
[j
][1+pos
-n_out
+i
], 1);
4776 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
4777 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
4778 bmap
= add_divs(bmap
, 2 * n_out
);
4780 bmap
= isl_basic_map_simplify(bmap
);
4781 return isl_basic_map_finalize(bmap
);
4783 isl_basic_map_free(bmap
);
4784 isl_basic_map_free(bmap1
);
4785 isl_basic_map_free(bmap2
);
4789 /* Given two maps A -> f(A) and B -> g(B), construct a map
4790 * A \cap B -> f(A) + f(B)
4792 __isl_give isl_map
*isl_map_sum(__isl_take isl_map
*map1
,
4793 __isl_take isl_map
*map2
)
4795 struct isl_map
*result
;
4798 if (isl_map_check_equal_space(map1
, map2
) < 0)
4801 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
4802 map1
->n
* map2
->n
, 0);
4805 for (i
= 0; i
< map1
->n
; ++i
)
4806 for (j
= 0; j
< map2
->n
; ++j
) {
4807 struct isl_basic_map
*part
;
4808 part
= isl_basic_map_sum(
4809 isl_basic_map_copy(map1
->p
[i
]),
4810 isl_basic_map_copy(map2
->p
[j
]));
4811 if (isl_basic_map_is_empty(part
))
4812 isl_basic_map_free(part
);
4814 result
= isl_map_add_basic_map(result
, part
);
4827 __isl_give isl_set
*isl_set_sum(__isl_take isl_set
*set1
,
4828 __isl_take isl_set
*set2
)
4830 return set_from_map(isl_map_sum(set_to_map(set1
), set_to_map(set2
)));
4833 /* Given a basic map A -> f(A), construct A -> -f(A).
4835 __isl_give isl_basic_map
*isl_basic_map_neg(__isl_take isl_basic_map
*bmap
)
4841 bmap
= isl_basic_map_cow(bmap
);
4842 n
= isl_basic_map_dim(bmap
, isl_dim_out
);
4844 return isl_basic_map_free(bmap
);
4846 off
= isl_basic_map_offset(bmap
, isl_dim_out
);
4847 for (i
= 0; i
< bmap
->n_eq
; ++i
)
4848 for (j
= 0; j
< n
; ++j
)
4849 isl_int_neg(bmap
->eq
[i
][off
+j
], bmap
->eq
[i
][off
+j
]);
4850 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
4851 for (j
= 0; j
< n
; ++j
)
4852 isl_int_neg(bmap
->ineq
[i
][off
+j
], bmap
->ineq
[i
][off
+j
]);
4853 for (i
= 0; i
< bmap
->n_div
; ++i
)
4854 for (j
= 0; j
< n
; ++j
)
4855 isl_int_neg(bmap
->div
[i
][1+off
+j
], bmap
->div
[i
][1+off
+j
]);
4856 bmap
= isl_basic_map_gauss(bmap
, NULL
);
4857 return isl_basic_map_finalize(bmap
);
4860 __isl_give isl_basic_set
*isl_basic_set_neg(__isl_take isl_basic_set
*bset
)
4862 return isl_basic_map_neg(bset
);
4865 /* Given a map A -> f(A), construct A -> -f(A).
4867 __isl_give isl_map
*isl_map_neg(__isl_take isl_map
*map
)
4871 map
= isl_map_cow(map
);
4875 for (i
= 0; i
< map
->n
; ++i
) {
4876 map
->p
[i
] = isl_basic_map_neg(map
->p
[i
]);
4887 __isl_give isl_set
*isl_set_neg(__isl_take isl_set
*set
)
4889 return set_from_map(isl_map_neg(set_to_map(set
)));
4892 /* Given a basic map A -> f(A) and an integer d, construct a basic map
4893 * A -> floor(f(A)/d).
4895 __isl_give isl_basic_map
*isl_basic_map_floordiv(__isl_take isl_basic_map
*bmap
,
4898 isl_size n_in
, n_out
, nparam
;
4899 unsigned total
, pos
;
4900 struct isl_basic_map
*result
= NULL
;
4901 struct isl_dim_map
*dim_map
;
4904 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4905 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4906 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4907 if (nparam
< 0 || n_in
< 0 || n_out
< 0)
4908 return isl_basic_map_free(bmap
);
4910 total
= nparam
+ n_in
+ n_out
+ bmap
->n_div
+ n_out
;
4911 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
4912 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_param
, pos
= 0);
4913 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_in
, pos
+= nparam
);
4914 isl_dim_map_div(dim_map
, bmap
, pos
+= n_in
+ n_out
);
4915 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_out
, pos
+= bmap
->n_div
);
4917 result
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
4918 bmap
->n_div
+ n_out
,
4919 bmap
->n_eq
, bmap
->n_ineq
+ 2 * n_out
);
4920 result
= isl_basic_map_add_constraints_dim_map(result
, bmap
, dim_map
);
4921 result
= add_divs(result
, n_out
);
4922 for (i
= 0; i
< n_out
; ++i
) {
4924 j
= isl_basic_map_alloc_inequality(result
);
4927 isl_seq_clr(result
->ineq
[j
], 1+total
);
4928 isl_int_neg(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
4929 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], 1);
4930 j
= isl_basic_map_alloc_inequality(result
);
4933 isl_seq_clr(result
->ineq
[j
], 1+total
);
4934 isl_int_set(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
4935 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], -1);
4936 isl_int_sub_ui(result
->ineq
[j
][0], d
, 1);
4939 result
= isl_basic_map_simplify(result
);
4940 return isl_basic_map_finalize(result
);
4942 isl_basic_map_free(result
);
4946 /* Given a map A -> f(A) and an integer d, construct a map
4947 * A -> floor(f(A)/d).
4949 __isl_give isl_map
*isl_map_floordiv(__isl_take isl_map
*map
, isl_int d
)
4953 map
= isl_map_cow(map
);
4957 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
4958 for (i
= 0; i
< map
->n
; ++i
) {
4959 map
->p
[i
] = isl_basic_map_floordiv(map
->p
[i
], d
);
4963 map
= isl_map_unmark_normalized(map
);
4971 /* Given a map A -> f(A) and an integer d, construct a map
4972 * A -> floor(f(A)/d).
4974 __isl_give isl_map
*isl_map_floordiv_val(__isl_take isl_map
*map
,
4975 __isl_take isl_val
*d
)
4979 if (!isl_val_is_int(d
))
4980 isl_die(isl_val_get_ctx(d
), isl_error_invalid
,
4981 "expecting integer denominator", goto error
);
4982 map
= isl_map_floordiv(map
, d
->n
);
4991 static __isl_give isl_basic_map
*var_equal(__isl_take isl_basic_map
*bmap
,
4999 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5000 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5001 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5002 if (total
< 0 || nparam
< 0 || n_in
< 0)
5003 return isl_basic_map_free(bmap
);
5004 i
= isl_basic_map_alloc_equality(bmap
);
5007 isl_seq_clr(bmap
->eq
[i
], 1 + total
);
5008 isl_int_set_si(bmap
->eq
[i
][1+nparam
+pos
], -1);
5009 isl_int_set_si(bmap
->eq
[i
][1+nparam
+n_in
+pos
], 1);
5010 return isl_basic_map_finalize(bmap
);
5012 isl_basic_map_free(bmap
);
5016 /* Add a constraint to "bmap" expressing i_pos < o_pos
5018 static __isl_give isl_basic_map
*var_less(__isl_take isl_basic_map
*bmap
,
5026 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5027 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5028 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5029 if (total
< 0 || nparam
< 0 || n_in
< 0)
5030 return isl_basic_map_free(bmap
);
5031 i
= isl_basic_map_alloc_inequality(bmap
);
5034 isl_seq_clr(bmap
->ineq
[i
], 1 + total
);
5035 isl_int_set_si(bmap
->ineq
[i
][0], -1);
5036 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
5037 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
5038 return isl_basic_map_finalize(bmap
);
5040 isl_basic_map_free(bmap
);
5044 /* Add a constraint to "bmap" expressing i_pos <= o_pos
5046 static __isl_give isl_basic_map
*var_less_or_equal(
5047 __isl_take isl_basic_map
*bmap
, unsigned pos
)
5054 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5055 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5056 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5057 if (total
< 0 || nparam
< 0 || n_in
< 0)
5058 return isl_basic_map_free(bmap
);
5059 i
= isl_basic_map_alloc_inequality(bmap
);
5062 isl_seq_clr(bmap
->ineq
[i
], 1 + total
);
5063 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
5064 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
5065 return isl_basic_map_finalize(bmap
);
5067 isl_basic_map_free(bmap
);
5071 /* Add a constraint to "bmap" expressing i_pos > o_pos
5073 static __isl_give isl_basic_map
*var_more(__isl_take isl_basic_map
*bmap
,
5081 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5082 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5083 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5084 if (total
< 0 || nparam
< 0 || n_in
< 0)
5085 return isl_basic_map_free(bmap
);
5086 i
= isl_basic_map_alloc_inequality(bmap
);
5089 isl_seq_clr(bmap
->ineq
[i
], 1 + total
);
5090 isl_int_set_si(bmap
->ineq
[i
][0], -1);
5091 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
5092 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
5093 return isl_basic_map_finalize(bmap
);
5095 isl_basic_map_free(bmap
);
5099 /* Add a constraint to "bmap" expressing i_pos >= o_pos
5101 static __isl_give isl_basic_map
*var_more_or_equal(
5102 __isl_take isl_basic_map
*bmap
, unsigned pos
)
5109 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5110 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5111 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5112 if (total
< 0 || nparam
< 0 || n_in
< 0)
5113 return isl_basic_map_free(bmap
);
5114 i
= isl_basic_map_alloc_inequality(bmap
);
5117 isl_seq_clr(bmap
->ineq
[i
], 1 + total
);
5118 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
5119 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
5120 return isl_basic_map_finalize(bmap
);
5122 isl_basic_map_free(bmap
);
5126 __isl_give isl_basic_map
*isl_basic_map_equal(
5127 __isl_take isl_space
*space
, unsigned n_equal
)
5130 struct isl_basic_map
*bmap
;
5131 bmap
= isl_basic_map_alloc_space(space
, 0, n_equal
, 0);
5134 for (i
= 0; i
< n_equal
&& bmap
; ++i
)
5135 bmap
= var_equal(bmap
, i
);
5136 return isl_basic_map_finalize(bmap
);
5139 /* Return a relation on of dimension "space" expressing i_[0..pos] << o_[0..pos]
5141 __isl_give isl_basic_map
*isl_basic_map_less_at(__isl_take isl_space
*space
,
5145 struct isl_basic_map
*bmap
;
5146 bmap
= isl_basic_map_alloc_space(space
, 0, pos
, 1);
5149 for (i
= 0; i
< pos
&& bmap
; ++i
)
5150 bmap
= var_equal(bmap
, i
);
5152 bmap
= var_less(bmap
, pos
);
5153 return isl_basic_map_finalize(bmap
);
5156 /* Return a relation on "space" expressing i_[0..pos] <<= o_[0..pos]
5158 __isl_give isl_basic_map
*isl_basic_map_less_or_equal_at(
5159 __isl_take isl_space
*space
, unsigned pos
)
5162 isl_basic_map
*bmap
;
5164 bmap
= isl_basic_map_alloc_space(space
, 0, pos
, 1);
5165 for (i
= 0; i
< pos
; ++i
)
5166 bmap
= var_equal(bmap
, i
);
5167 bmap
= var_less_or_equal(bmap
, pos
);
5168 return isl_basic_map_finalize(bmap
);
5171 /* Return a relation on "space" expressing i_pos > o_pos
5173 __isl_give isl_basic_map
*isl_basic_map_more_at(__isl_take isl_space
*space
,
5177 struct isl_basic_map
*bmap
;
5178 bmap
= isl_basic_map_alloc_space(space
, 0, pos
, 1);
5181 for (i
= 0; i
< pos
&& bmap
; ++i
)
5182 bmap
= var_equal(bmap
, i
);
5184 bmap
= var_more(bmap
, pos
);
5185 return isl_basic_map_finalize(bmap
);
5188 /* Return a relation on "space" expressing i_[0..pos] >>= o_[0..pos]
5190 __isl_give isl_basic_map
*isl_basic_map_more_or_equal_at(
5191 __isl_take isl_space
*space
, unsigned pos
)
5194 isl_basic_map
*bmap
;
5196 bmap
= isl_basic_map_alloc_space(space
, 0, pos
, 1);
5197 for (i
= 0; i
< pos
; ++i
)
5198 bmap
= var_equal(bmap
, i
);
5199 bmap
= var_more_or_equal(bmap
, pos
);
5200 return isl_basic_map_finalize(bmap
);
5203 static __isl_give isl_map
*map_lex_lte_first(__isl_take isl_space
*space
,
5204 unsigned n
, int equal
)
5206 struct isl_map
*map
;
5209 if (n
== 0 && equal
)
5210 return isl_map_universe(space
);
5212 map
= isl_map_alloc_space(isl_space_copy(space
), n
, ISL_MAP_DISJOINT
);
5214 for (i
= 0; i
+ 1 < n
; ++i
)
5215 map
= isl_map_add_basic_map(map
,
5216 isl_basic_map_less_at(isl_space_copy(space
), i
));
5219 map
= isl_map_add_basic_map(map
,
5220 isl_basic_map_less_or_equal_at(space
, n
- 1));
5222 map
= isl_map_add_basic_map(map
,
5223 isl_basic_map_less_at(space
, n
- 1));
5225 isl_space_free(space
);
5230 static __isl_give isl_map
*map_lex_lte(__isl_take isl_space
*space
, int equal
)
5234 return map_lex_lte_first(space
, space
->n_out
, equal
);
5237 __isl_give isl_map
*isl_map_lex_lt_first(__isl_take isl_space
*space
,
5240 return map_lex_lte_first(space
, n
, 0);
5243 __isl_give isl_map
*isl_map_lex_le_first(__isl_take isl_space
*space
,
5246 return map_lex_lte_first(space
, n
, 1);
5249 __isl_give isl_map
*isl_map_lex_lt(__isl_take isl_space
*set_space
)
5251 return map_lex_lte(isl_space_map_from_set(set_space
), 0);
5254 __isl_give isl_map
*isl_map_lex_le(__isl_take isl_space
*set_space
)
5256 return map_lex_lte(isl_space_map_from_set(set_space
), 1);
5259 static __isl_give isl_map
*map_lex_gte_first(__isl_take isl_space
*space
,
5260 unsigned n
, int equal
)
5262 struct isl_map
*map
;
5265 if (n
== 0 && equal
)
5266 return isl_map_universe(space
);
5268 map
= isl_map_alloc_space(isl_space_copy(space
), n
, ISL_MAP_DISJOINT
);
5270 for (i
= 0; i
+ 1 < n
; ++i
)
5271 map
= isl_map_add_basic_map(map
,
5272 isl_basic_map_more_at(isl_space_copy(space
), i
));
5275 map
= isl_map_add_basic_map(map
,
5276 isl_basic_map_more_or_equal_at(space
, n
- 1));
5278 map
= isl_map_add_basic_map(map
,
5279 isl_basic_map_more_at(space
, n
- 1));
5281 isl_space_free(space
);
5286 static __isl_give isl_map
*map_lex_gte(__isl_take isl_space
*space
, int equal
)
5290 return map_lex_gte_first(space
, space
->n_out
, equal
);
5293 __isl_give isl_map
*isl_map_lex_gt_first(__isl_take isl_space
*space
,
5296 return map_lex_gte_first(space
, n
, 0);
5299 __isl_give isl_map
*isl_map_lex_ge_first(__isl_take isl_space
*space
,
5302 return map_lex_gte_first(space
, n
, 1);
5305 __isl_give isl_map
*isl_map_lex_gt(__isl_take isl_space
*set_space
)
5307 return map_lex_gte(isl_space_map_from_set(set_space
), 0);
5310 __isl_give isl_map
*isl_map_lex_ge(__isl_take isl_space
*set_space
)
5312 return map_lex_gte(isl_space_map_from_set(set_space
), 1);
5315 __isl_give isl_map
*isl_set_lex_le_set(__isl_take isl_set
*set1
,
5316 __isl_take isl_set
*set2
)
5319 map
= isl_map_lex_le(isl_set_get_space(set1
));
5320 map
= isl_map_intersect_domain(map
, set1
);
5321 map
= isl_map_intersect_range(map
, set2
);
5325 __isl_give isl_map
*isl_set_lex_lt_set(__isl_take isl_set
*set1
,
5326 __isl_take isl_set
*set2
)
5329 map
= isl_map_lex_lt(isl_set_get_space(set1
));
5330 map
= isl_map_intersect_domain(map
, set1
);
5331 map
= isl_map_intersect_range(map
, set2
);
5335 __isl_give isl_map
*isl_set_lex_ge_set(__isl_take isl_set
*set1
,
5336 __isl_take isl_set
*set2
)
5339 map
= isl_map_lex_ge(isl_set_get_space(set1
));
5340 map
= isl_map_intersect_domain(map
, set1
);
5341 map
= isl_map_intersect_range(map
, set2
);
5345 __isl_give isl_map
*isl_set_lex_gt_set(__isl_take isl_set
*set1
,
5346 __isl_take isl_set
*set2
)
5349 map
= isl_map_lex_gt(isl_set_get_space(set1
));
5350 map
= isl_map_intersect_domain(map
, set1
);
5351 map
= isl_map_intersect_range(map
, set2
);
5355 __isl_give isl_map
*isl_map_lex_le_map(__isl_take isl_map
*map1
,
5356 __isl_take isl_map
*map2
)
5359 map
= isl_map_lex_le(isl_space_range(isl_map_get_space(map1
)));
5360 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
5361 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
5365 __isl_give isl_map
*isl_map_lex_lt_map(__isl_take isl_map
*map1
,
5366 __isl_take isl_map
*map2
)
5369 map
= isl_map_lex_lt(isl_space_range(isl_map_get_space(map1
)));
5370 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
5371 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
5375 __isl_give isl_map
*isl_map_lex_ge_map(__isl_take isl_map
*map1
,
5376 __isl_take isl_map
*map2
)
5379 map
= isl_map_lex_ge(isl_space_range(isl_map_get_space(map1
)));
5380 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
5381 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
5385 __isl_give isl_map
*isl_map_lex_gt_map(__isl_take isl_map
*map1
,
5386 __isl_take isl_map
*map2
)
5389 map
= isl_map_lex_gt(isl_space_range(isl_map_get_space(map1
)));
5390 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
5391 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
5395 /* For the div d = floor(f/m) at position "div", add the constraint
5399 static __isl_give isl_basic_map
*add_upper_div_constraint(
5400 __isl_take isl_basic_map
*bmap
, unsigned div
)
5403 isl_size v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
5407 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
5408 if (v_div
< 0 || n_div
< 0)
5409 return isl_basic_map_free(bmap
);
5411 i
= isl_basic_map_alloc_inequality(bmap
);
5413 return isl_basic_map_free(bmap
);
5414 isl_seq_cpy(bmap
->ineq
[i
], bmap
->div
[div
] + 1, 1 + v_div
+ n_div
);
5415 isl_int_neg(bmap
->ineq
[i
][1 + pos
], bmap
->div
[div
][0]);
5420 /* For the div d = floor(f/m) at position "div", add the constraint
5422 * -(f-(m-1)) + m d >= 0
5424 static __isl_give isl_basic_map
*add_lower_div_constraint(
5425 __isl_take isl_basic_map
*bmap
, unsigned div
)
5428 isl_size v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
5432 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
5433 if (v_div
< 0 || n_div
< 0)
5434 return isl_basic_map_free(bmap
);
5436 i
= isl_basic_map_alloc_inequality(bmap
);
5438 return isl_basic_map_free(bmap
);
5439 isl_seq_neg(bmap
->ineq
[i
], bmap
->div
[div
] + 1, 1 + v_div
+ n_div
);
5440 isl_int_set(bmap
->ineq
[i
][1 + pos
], bmap
->div
[div
][0]);
5441 isl_int_add(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], bmap
->ineq
[i
][1 + pos
]);
5442 isl_int_sub_ui(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], 1);
5447 /* For the div d = floor(f/m) at position "pos", add the constraints
5450 * -(f-(m-1)) + m d >= 0
5452 * Note that the second constraint is the negation of
5456 __isl_give isl_basic_map
*isl_basic_map_add_div_constraints(
5457 __isl_take isl_basic_map
*bmap
, unsigned pos
)
5459 bmap
= add_upper_div_constraint(bmap
, pos
);
5460 bmap
= add_lower_div_constraint(bmap
, pos
);
5464 /* For each known div d = floor(f/m), add the constraints
5467 * -(f-(m-1)) + m d >= 0
5469 * Remove duplicate constraints in case of some these div constraints
5470 * already appear in "bmap".
5472 __isl_give isl_basic_map
*isl_basic_map_add_known_div_constraints(
5473 __isl_take isl_basic_map
*bmap
)
5477 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
5479 return isl_basic_map_free(bmap
);
5483 bmap
= add_known_div_constraints(bmap
);
5484 bmap
= isl_basic_map_remove_duplicate_constraints(bmap
, NULL
, 0);
5485 bmap
= isl_basic_map_finalize(bmap
);
5489 /* Add the div constraint of sign "sign" for div "div" of "bmap".
5491 * In particular, if this div is of the form d = floor(f/m),
5492 * then add the constraint
5496 * if sign < 0 or the constraint
5498 * -(f-(m-1)) + m d >= 0
5502 __isl_give isl_basic_map
*isl_basic_map_add_div_constraint(
5503 __isl_take isl_basic_map
*bmap
, unsigned div
, int sign
)
5506 return add_upper_div_constraint(bmap
, div
);
5508 return add_lower_div_constraint(bmap
, div
);
5511 __isl_give isl_basic_set
*isl_basic_map_underlying_set(
5512 __isl_take isl_basic_map
*bmap
)
5518 if (bmap
->dim
->nparam
== 0 && bmap
->dim
->n_in
== 0 &&
5520 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_in
) &&
5521 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_out
))
5522 return bset_from_bmap(bmap
);
5523 bmap
= isl_basic_map_cow(bmap
);
5526 space
= isl_basic_map_take_space(bmap
);
5527 space
= isl_space_underlying(space
, bmap
->n_div
);
5528 bmap
= isl_basic_map_restore_space(bmap
, space
);
5531 bmap
->extra
-= bmap
->n_div
;
5533 bmap
= isl_basic_map_finalize(bmap
);
5534 return bset_from_bmap(bmap
);
5536 isl_basic_map_free(bmap
);
5540 __isl_give isl_basic_set
*isl_basic_set_underlying_set(
5541 __isl_take isl_basic_set
*bset
)
5543 return isl_basic_map_underlying_set(bset_to_bmap(bset
));
5546 /* Replace each element in "list" by the result of applying
5547 * isl_basic_map_underlying_set to the element.
5549 __isl_give isl_basic_set_list
*isl_basic_map_list_underlying_set(
5550 __isl_take isl_basic_map_list
*list
)
5555 n
= isl_basic_map_list_n_basic_map(list
);
5559 for (i
= 0; i
< n
; ++i
) {
5560 isl_basic_map
*bmap
;
5561 isl_basic_set
*bset
;
5563 bmap
= isl_basic_map_list_get_basic_map(list
, i
);
5564 bset
= isl_basic_set_underlying_set(bmap
);
5565 list
= isl_basic_set_list_set_basic_set(list
, i
, bset
);
5570 isl_basic_map_list_free(list
);
5574 __isl_give isl_basic_map
*isl_basic_map_overlying_set(
5575 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_map
*like
)
5577 struct isl_basic_map
*bmap
;
5578 struct isl_ctx
*ctx
;
5579 isl_size dim
, bmap_total
;
5586 if (isl_basic_set_check_no_params(bset
) < 0 ||
5587 isl_basic_set_check_no_locals(bset
) < 0)
5589 dim
= isl_basic_set_dim(bset
, isl_dim_set
);
5590 bmap_total
= isl_basic_map_dim(like
, isl_dim_all
);
5591 if (dim
< 0 || bmap_total
< 0)
5593 isl_assert(ctx
, dim
== bmap_total
, goto error
);
5594 if (like
->n_div
== 0) {
5595 isl_space
*space
= isl_basic_map_get_space(like
);
5596 isl_basic_map_free(like
);
5597 return isl_basic_map_reset_space(bset
, space
);
5599 bset
= isl_basic_set_cow(bset
);
5602 total
= dim
+ bset
->extra
;
5603 bmap
= bset_to_bmap(bset
);
5604 isl_space_free(isl_basic_map_take_space(bmap
));
5605 bmap
= isl_basic_map_restore_space(bmap
, isl_basic_map_get_space(like
));
5608 bmap
->n_div
= like
->n_div
;
5609 bmap
->extra
+= like
->n_div
;
5613 ltotal
= total
- bmap
->extra
+ like
->extra
;
5616 bmap
->block2
= isl_blk_extend(ctx
, bmap
->block2
,
5617 bmap
->extra
* (1 + 1 + total
));
5618 if (isl_blk_is_error(bmap
->block2
))
5620 div
= isl_realloc_array(ctx
, bmap
->div
, isl_int
*, bmap
->extra
);
5624 for (i
= 0; i
< bmap
->extra
; ++i
)
5625 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + 1 + total
);
5626 for (i
= 0; i
< like
->n_div
; ++i
) {
5627 isl_seq_cpy(bmap
->div
[i
], like
->div
[i
], 1 + 1 + ltotal
);
5628 isl_seq_clr(bmap
->div
[i
]+1+1+ltotal
, total
- ltotal
);
5630 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
5632 isl_basic_map_free(like
);
5633 bmap
= isl_basic_map_simplify(bmap
);
5634 bmap
= isl_basic_map_finalize(bmap
);
5637 isl_basic_map_free(like
);
5638 isl_basic_set_free(bset
);
5642 __isl_give isl_basic_set
*isl_basic_set_from_underlying_set(
5643 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*like
)
5645 return bset_from_bmap(isl_basic_map_overlying_set(bset
,
5646 bset_to_bmap(like
)));
5649 __isl_give isl_set
*isl_map_underlying_set(__isl_take isl_map
*map
)
5653 map
= isl_map_cow(map
);
5656 map
->dim
= isl_space_cow(map
->dim
);
5660 for (i
= 1; i
< map
->n
; ++i
)
5661 isl_assert(map
->ctx
, map
->p
[0]->n_div
== map
->p
[i
]->n_div
,
5663 for (i
= 0; i
< map
->n
; ++i
) {
5664 map
->p
[i
] = bset_to_bmap(
5665 isl_basic_map_underlying_set(map
->p
[i
]));
5670 map
->dim
= isl_space_underlying(map
->dim
, 0);
5672 isl_space_free(map
->dim
);
5673 map
->dim
= isl_space_copy(map
->p
[0]->dim
);
5677 return set_from_map(map
);
5683 /* Replace the space of "bmap" by "space".
5685 * If the space of "bmap" is identical to "space" (including the identifiers
5686 * of the input and output dimensions), then simply return the original input.
5688 __isl_give isl_basic_map
*isl_basic_map_reset_space(
5689 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*space
)
5692 isl_space
*bmap_space
;
5694 bmap_space
= isl_basic_map_peek_space(bmap
);
5695 equal
= isl_space_is_equal(bmap_space
, space
);
5696 if (equal
>= 0 && equal
)
5697 equal
= isl_space_has_equal_ids(bmap_space
, space
);
5701 isl_space_free(space
);
5704 isl_space_free(isl_basic_map_take_space(bmap
));
5705 bmap
= isl_basic_map_restore_space(bmap
, space
);
5707 bmap
= isl_basic_map_finalize(bmap
);
5711 isl_basic_map_free(bmap
);
5712 isl_space_free(space
);
5716 __isl_give isl_basic_set
*isl_basic_set_reset_space(
5717 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*space
)
5719 return bset_from_bmap(isl_basic_map_reset_space(bset_to_bmap(bset
),
5723 /* Check that the total dimensions of "map" and "space" are the same.
5725 static isl_stat
check_map_space_equal_total_dim(__isl_keep isl_map
*map
,
5726 __isl_keep isl_space
*space
)
5728 isl_size dim1
, dim2
;
5730 dim1
= isl_map_dim(map
, isl_dim_all
);
5731 dim2
= isl_space_dim(space
, isl_dim_all
);
5732 if (dim1
< 0 || dim2
< 0)
5733 return isl_stat_error
;
5736 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
5737 "total dimensions do not match", return isl_stat_error
);
5740 __isl_give isl_map
*isl_map_reset_space(__isl_take isl_map
*map
,
5741 __isl_take isl_space
*space
)
5745 map
= isl_map_cow(map
);
5749 for (i
= 0; i
< map
->n
; ++i
) {
5750 map
->p
[i
] = isl_basic_map_reset_space(map
->p
[i
],
5751 isl_space_copy(space
));
5755 isl_space_free(isl_map_take_space(map
));
5756 map
= isl_map_restore_space(map
, space
);
5761 isl_space_free(space
);
5765 /* Replace the space of "map" by "space", without modifying
5766 * the dimension of "map".
5768 * If the space of "map" is identical to "space" (including the identifiers
5769 * of the input and output dimensions), then simply return the original input.
5771 __isl_give isl_map
*isl_map_reset_equal_dim_space(__isl_take isl_map
*map
,
5772 __isl_take isl_space
*space
)
5775 isl_space
*map_space
;
5777 map_space
= isl_map_peek_space(map
);
5778 equal
= isl_space_is_equal(map_space
, space
);
5779 if (equal
>= 0 && equal
)
5780 equal
= isl_space_has_equal_ids(map_space
, space
);
5784 isl_space_free(space
);
5787 if (check_map_space_equal_total_dim(map
, space
) < 0)
5789 return isl_map_reset_space(map
, space
);
5792 isl_space_free(space
);
5796 __isl_give isl_set
*isl_set_reset_space(__isl_take isl_set
*set
,
5797 __isl_take isl_space
*space
)
5799 return set_from_map(isl_map_reset_space(set_to_map(set
), space
));
5802 /* Compute the parameter domain of the given basic set.
5804 __isl_give isl_basic_set
*isl_basic_set_params(__isl_take isl_basic_set
*bset
)
5810 is_params
= isl_basic_set_is_params(bset
);
5812 return isl_basic_set_free(bset
);
5816 n
= isl_basic_set_dim(bset
, isl_dim_set
);
5818 return isl_basic_set_free(bset
);
5819 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
5820 space
= isl_basic_set_get_space(bset
);
5821 space
= isl_space_params(space
);
5822 bset
= isl_basic_set_reset_space(bset
, space
);
5826 /* Construct a zero-dimensional basic set with the given parameter domain.
5828 __isl_give isl_basic_set
*isl_basic_set_from_params(
5829 __isl_take isl_basic_set
*bset
)
5832 space
= isl_basic_set_get_space(bset
);
5833 space
= isl_space_set_from_params(space
);
5834 bset
= isl_basic_set_reset_space(bset
, space
);
5838 /* Compute the parameter domain of the given set.
5840 __isl_give isl_set
*isl_set_params(__isl_take isl_set
*set
)
5842 return isl_map_params(set_to_map(set
));
5845 /* Construct a zero-dimensional set with the given parameter domain.
5847 __isl_give isl_set
*isl_set_from_params(__isl_take isl_set
*set
)
5850 space
= isl_set_get_space(set
);
5851 space
= isl_space_set_from_params(space
);
5852 set
= isl_set_reset_space(set
, space
);
5856 /* Compute the parameter domain of the given map.
5858 __isl_give isl_set
*isl_map_params(__isl_take isl_map
*map
)
5861 isl_size n_in
, n_out
;
5863 n_in
= isl_map_dim(map
, isl_dim_in
);
5864 n_out
= isl_map_dim(map
, isl_dim_out
);
5865 if (n_in
< 0 || n_out
< 0)
5866 return isl_map_free(map
);
5867 map
= isl_map_project_out(map
, isl_dim_in
, 0, n_in
);
5868 map
= isl_map_project_out(map
, isl_dim_out
, 0, n_out
);
5869 space
= isl_map_get_space(map
);
5870 space
= isl_space_params(space
);
5871 map
= isl_map_reset_space(map
, space
);
5875 __isl_give isl_basic_set
*isl_basic_map_domain(__isl_take isl_basic_map
*bmap
)
5880 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5882 return isl_basic_map_free(bmap
);
5883 space
= isl_space_domain(isl_basic_map_get_space(bmap
));
5885 bmap
= isl_basic_map_project_out(bmap
, isl_dim_out
, 0, n_out
);
5887 return isl_basic_map_reset_space(bmap
, space
);
5890 isl_bool
isl_basic_map_may_be_set(__isl_keep isl_basic_map
*bmap
)
5893 return isl_bool_error
;
5894 return isl_space_may_be_set(bmap
->dim
);
5897 /* Is this basic map actually a set?
5898 * Users should never call this function. Outside of isl,
5899 * the type should indicate whether something is a set or a map.
5901 isl_bool
isl_basic_map_is_set(__isl_keep isl_basic_map
*bmap
)
5904 return isl_bool_error
;
5905 return isl_space_is_set(bmap
->dim
);
5908 __isl_give isl_basic_set
*isl_basic_map_range(__isl_take isl_basic_map
*bmap
)
5912 is_set
= isl_basic_map_is_set(bmap
);
5917 return isl_basic_map_domain(isl_basic_map_reverse(bmap
));
5919 isl_basic_map_free(bmap
);
5923 __isl_give isl_basic_map
*isl_basic_map_domain_map(
5924 __isl_take isl_basic_map
*bmap
)
5928 isl_basic_map
*domain
;
5929 isl_size nparam
, n_in
, n_out
;
5931 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5932 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5933 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5934 if (nparam
< 0 || n_in
< 0 || n_out
< 0)
5935 return isl_basic_map_free(bmap
);
5937 space
= isl_basic_map_get_space(bmap
);
5938 space
= isl_space_from_range(isl_space_domain(space
));
5939 domain
= isl_basic_map_universe(space
);
5941 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
5942 bmap
= isl_basic_map_apply_range(bmap
, domain
);
5943 bmap
= isl_basic_map_extend_constraints(bmap
, n_in
, 0);
5945 for (i
= 0; i
< n_in
; ++i
)
5946 bmap
= isl_basic_map_equate(bmap
, isl_dim_in
, i
,
5949 bmap
= isl_basic_map_gauss(bmap
, NULL
);
5950 return isl_basic_map_finalize(bmap
);
5953 __isl_give isl_basic_map
*isl_basic_map_range_map(
5954 __isl_take isl_basic_map
*bmap
)
5958 isl_basic_map
*range
;
5959 isl_size nparam
, n_in
, n_out
;
5961 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5962 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5963 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5964 if (nparam
< 0 || n_in
< 0 || n_out
< 0)
5965 return isl_basic_map_free(bmap
);
5967 space
= isl_basic_map_get_space(bmap
);
5968 space
= isl_space_from_range(isl_space_range(space
));
5969 range
= isl_basic_map_universe(space
);
5971 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
5972 bmap
= isl_basic_map_apply_range(bmap
, range
);
5973 bmap
= isl_basic_map_extend_constraints(bmap
, n_out
, 0);
5975 for (i
= 0; i
< n_out
; ++i
)
5976 bmap
= isl_basic_map_equate(bmap
, isl_dim_in
, n_in
+ i
,
5979 bmap
= isl_basic_map_gauss(bmap
, NULL
);
5980 return isl_basic_map_finalize(bmap
);
5983 int isl_map_may_be_set(__isl_keep isl_map
*map
)
5987 return isl_space_may_be_set(map
->dim
);
5990 /* Is this map actually a set?
5991 * Users should never call this function. Outside of isl,
5992 * the type should indicate whether something is a set or a map.
5994 isl_bool
isl_map_is_set(__isl_keep isl_map
*map
)
5997 return isl_bool_error
;
5998 return isl_space_is_set(map
->dim
);
6001 __isl_give isl_set
*isl_map_range(__isl_take isl_map
*map
)
6005 struct isl_set
*set
;
6007 is_set
= isl_map_is_set(map
);
6011 return set_from_map(map
);
6013 map
= isl_map_cow(map
);
6017 set
= set_from_map(map
);
6018 set
->dim
= isl_space_range(set
->dim
);
6021 for (i
= 0; i
< map
->n
; ++i
) {
6022 set
->p
[i
] = isl_basic_map_range(map
->p
[i
]);
6026 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
6027 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
6034 /* Transform "map" by applying "fn_space" to its space and "fn_bmap"
6035 * to each of its basic maps.
6037 static __isl_give isl_map
*isl_map_transform(__isl_take isl_map
*map
,
6038 __isl_give isl_space
*(*fn_space
)(__isl_take isl_space
*space
),
6039 __isl_give isl_basic_map
*(*fn_bmap
)(__isl_take isl_basic_map
*bmap
))
6044 map
= isl_map_cow(map
);
6048 for (i
= 0; i
< map
->n
; ++i
) {
6049 map
->p
[i
] = fn_bmap(map
->p
[i
]);
6051 return isl_map_free(map
);
6053 map
= isl_map_unmark_normalized(map
);
6055 space
= isl_map_take_space(map
);
6056 space
= fn_space(space
);
6057 map
= isl_map_restore_space(map
, space
);
6062 __isl_give isl_map
*isl_map_domain_map(__isl_take isl_map
*map
)
6064 return isl_map_transform(map
, &isl_space_domain_map
,
6065 &isl_basic_map_domain_map
);
6068 __isl_give isl_map
*isl_map_range_map(__isl_take isl_map
*map
)
6070 return isl_map_transform(map
, &isl_space_range_map
,
6071 &isl_basic_map_range_map
);
6074 /* Given a wrapped map of the form A[B -> C],
6075 * return the map A[B -> C] -> B.
6077 __isl_give isl_map
*isl_set_wrapped_domain_map(__isl_take isl_set
*set
)
6084 if (!isl_set_has_tuple_id(set
))
6085 return isl_map_domain_map(isl_set_unwrap(set
));
6087 id
= isl_set_get_tuple_id(set
);
6088 map
= isl_map_domain_map(isl_set_unwrap(set
));
6089 map
= isl_map_set_tuple_id(map
, isl_dim_in
, id
);
6094 __isl_give isl_basic_map
*isl_basic_map_from_domain(
6095 __isl_take isl_basic_set
*bset
)
6097 return isl_basic_map_reverse(isl_basic_map_from_range(bset
));
6100 __isl_give isl_basic_map
*isl_basic_map_from_range(
6101 __isl_take isl_basic_set
*bset
)
6104 space
= isl_basic_set_get_space(bset
);
6105 space
= isl_space_from_range(space
);
6106 bset
= isl_basic_set_reset_space(bset
, space
);
6107 return bset_to_bmap(bset
);
6110 /* Create a relation with the given set as range.
6111 * The domain of the created relation is a zero-dimensional
6112 * flat anonymous space.
6114 __isl_give isl_map
*isl_map_from_range(__isl_take isl_set
*set
)
6117 space
= isl_set_get_space(set
);
6118 space
= isl_space_from_range(space
);
6119 set
= isl_set_reset_space(set
, space
);
6120 return set_to_map(set
);
6123 /* Create a relation with the given set as domain.
6124 * The range of the created relation is a zero-dimensional
6125 * flat anonymous space.
6127 __isl_give isl_map
*isl_map_from_domain(__isl_take isl_set
*set
)
6129 return isl_map_reverse(isl_map_from_range(set
));
6132 __isl_give isl_basic_map
*isl_basic_map_from_domain_and_range(
6133 __isl_take isl_basic_set
*domain
, __isl_take isl_basic_set
*range
)
6135 return isl_basic_map_apply_range(isl_basic_map_reverse(domain
), range
);
6138 __isl_give isl_map
*isl_map_from_domain_and_range(__isl_take isl_set
*domain
,
6139 __isl_take isl_set
*range
)
6141 return isl_map_apply_range(isl_map_reverse(domain
), range
);
6144 /* Return a newly allocated isl_map with given space and flags and
6145 * room for "n" basic maps.
6146 * Make sure that all cached information is cleared.
6148 __isl_give isl_map
*isl_map_alloc_space(__isl_take isl_space
*space
, int n
,
6151 struct isl_map
*map
;
6156 isl_die(space
->ctx
, isl_error_internal
,
6157 "negative number of basic maps", goto error
);
6158 map
= isl_calloc(space
->ctx
, struct isl_map
,
6159 sizeof(struct isl_map
) +
6160 (n
- 1) * sizeof(struct isl_basic_map
*));
6164 map
->ctx
= space
->ctx
;
6165 isl_ctx_ref(map
->ctx
);
6173 isl_space_free(space
);
6177 __isl_give isl_basic_map
*isl_basic_map_empty(__isl_take isl_space
*space
)
6179 struct isl_basic_map
*bmap
;
6180 bmap
= isl_basic_map_alloc_space(space
, 0, 1, 0);
6181 bmap
= isl_basic_map_set_to_empty(bmap
);
6185 __isl_give isl_basic_set
*isl_basic_set_empty(__isl_take isl_space
*space
)
6187 struct isl_basic_set
*bset
;
6188 bset
= isl_basic_set_alloc_space(space
, 0, 1, 0);
6189 bset
= isl_basic_set_set_to_empty(bset
);
6193 __isl_give isl_basic_map
*isl_basic_map_universe(__isl_take isl_space
*space
)
6195 struct isl_basic_map
*bmap
;
6196 bmap
= isl_basic_map_alloc_space(space
, 0, 0, 0);
6197 bmap
= isl_basic_map_finalize(bmap
);
6201 __isl_give isl_basic_set
*isl_basic_set_universe(__isl_take isl_space
*space
)
6203 struct isl_basic_set
*bset
;
6204 bset
= isl_basic_set_alloc_space(space
, 0, 0, 0);
6205 bset
= isl_basic_set_finalize(bset
);
6209 __isl_give isl_basic_map
*isl_basic_map_nat_universe(
6210 __isl_take isl_space
*space
)
6213 isl_size total
= isl_space_dim(space
, isl_dim_all
);
6214 isl_basic_map
*bmap
;
6217 space
= isl_space_free(space
);
6218 bmap
= isl_basic_map_alloc_space(space
, 0, 0, total
);
6219 for (i
= 0; i
< total
; ++i
) {
6220 int k
= isl_basic_map_alloc_inequality(bmap
);
6223 isl_seq_clr(bmap
->ineq
[k
], 1 + total
);
6224 isl_int_set_si(bmap
->ineq
[k
][1 + i
], 1);
6228 isl_basic_map_free(bmap
);
6232 __isl_give isl_basic_set
*isl_basic_set_nat_universe(
6233 __isl_take isl_space
*space
)
6235 return isl_basic_map_nat_universe(space
);
6238 __isl_give isl_map
*isl_map_nat_universe(__isl_take isl_space
*space
)
6240 return isl_map_from_basic_map(isl_basic_map_nat_universe(space
));
6243 __isl_give isl_set
*isl_set_nat_universe(__isl_take isl_space
*space
)
6245 return isl_map_nat_universe(space
);
6248 __isl_give isl_map
*isl_map_empty(__isl_take isl_space
*space
)
6250 return isl_map_alloc_space(space
, 0, ISL_MAP_DISJOINT
);
6253 __isl_give isl_set
*isl_set_empty(__isl_take isl_space
*space
)
6255 return isl_set_alloc_space(space
, 0, ISL_MAP_DISJOINT
);
6258 __isl_give isl_map
*isl_map_universe(__isl_take isl_space
*space
)
6260 struct isl_map
*map
;
6263 map
= isl_map_alloc_space(isl_space_copy(space
), 1, ISL_MAP_DISJOINT
);
6264 map
= isl_map_add_basic_map(map
, isl_basic_map_universe(space
));
6268 __isl_give isl_set
*isl_set_universe(__isl_take isl_space
*space
)
6270 struct isl_set
*set
;
6273 set
= isl_set_alloc_space(isl_space_copy(space
), 1, ISL_MAP_DISJOINT
);
6274 set
= isl_set_add_basic_set(set
, isl_basic_set_universe(space
));
6278 __isl_give isl_map
*isl_map_dup(__isl_keep isl_map
*map
)
6281 struct isl_map
*dup
;
6285 dup
= isl_map_alloc_space(isl_space_copy(map
->dim
), map
->n
, map
->flags
);
6286 for (i
= 0; i
< map
->n
; ++i
)
6287 dup
= isl_map_add_basic_map(dup
, isl_basic_map_copy(map
->p
[i
]));
6291 __isl_give isl_map
*isl_map_add_basic_map(__isl_take isl_map
*map
,
6292 __isl_take isl_basic_map
*bmap
)
6296 if (isl_basic_map_plain_is_empty(bmap
)) {
6297 isl_basic_map_free(bmap
);
6300 if (isl_map_basic_map_check_equal_space(map
, bmap
) < 0)
6302 isl_assert(map
->ctx
, map
->n
< map
->size
, goto error
);
6303 map
->p
[map
->n
] = bmap
;
6305 map
= isl_map_unmark_normalized(map
);
6311 isl_basic_map_free(bmap
);
6315 __isl_null isl_map
*isl_map_free(__isl_take isl_map
*map
)
6326 isl_ctx_deref(map
->ctx
);
6327 for (i
= 0; i
< map
->n
; ++i
)
6328 isl_basic_map_free(map
->p
[i
]);
6329 isl_space_free(map
->dim
);
6335 static __isl_give isl_basic_map
*isl_basic_map_fix_pos_si(
6336 __isl_take isl_basic_map
*bmap
, unsigned pos
, int value
)
6341 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
6343 return isl_basic_map_free(bmap
);
6345 bmap
= isl_basic_map_cow(bmap
);
6346 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
6347 j
= isl_basic_map_alloc_equality(bmap
);
6350 isl_seq_clr(bmap
->eq
[j
] + 1, total
);
6351 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
6352 isl_int_set_si(bmap
->eq
[j
][0], value
);
6353 bmap
= isl_basic_map_simplify(bmap
);
6354 return isl_basic_map_finalize(bmap
);
6356 isl_basic_map_free(bmap
);
6360 static __isl_give isl_basic_map
*isl_basic_map_fix_pos(
6361 __isl_take isl_basic_map
*bmap
, unsigned pos
, isl_int value
)
6366 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
6368 return isl_basic_map_free(bmap
);
6370 bmap
= isl_basic_map_cow(bmap
);
6371 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
6372 j
= isl_basic_map_alloc_equality(bmap
);
6375 isl_seq_clr(bmap
->eq
[j
] + 1, total
);
6376 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
6377 isl_int_set(bmap
->eq
[j
][0], value
);
6378 bmap
= isl_basic_map_simplify(bmap
);
6379 return isl_basic_map_finalize(bmap
);
6381 isl_basic_map_free(bmap
);
6385 __isl_give isl_basic_map
*isl_basic_map_fix_si(__isl_take isl_basic_map
*bmap
,
6386 enum isl_dim_type type
, unsigned pos
, int value
)
6388 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6389 return isl_basic_map_free(bmap
);
6390 return isl_basic_map_fix_pos_si(bmap
,
6391 isl_basic_map_offset(bmap
, type
) + pos
, value
);
6394 __isl_give isl_basic_map
*isl_basic_map_fix(__isl_take isl_basic_map
*bmap
,
6395 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6397 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6398 return isl_basic_map_free(bmap
);
6399 return isl_basic_map_fix_pos(bmap
,
6400 isl_basic_map_offset(bmap
, type
) + pos
, value
);
6403 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
6404 * to be equal to "v".
6406 __isl_give isl_basic_map
*isl_basic_map_fix_val(__isl_take isl_basic_map
*bmap
,
6407 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6411 if (!isl_val_is_int(v
))
6412 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
6413 "expecting integer value", goto error
);
6414 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6416 pos
+= isl_basic_map_offset(bmap
, type
);
6417 bmap
= isl_basic_map_fix_pos(bmap
, pos
, v
->n
);
6421 isl_basic_map_free(bmap
);
6426 /* Fix the value of the variable at position "pos" of type "type" of "bset"
6427 * to be equal to "v".
6429 __isl_give isl_basic_set
*isl_basic_set_fix_val(__isl_take isl_basic_set
*bset
,
6430 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6432 return isl_basic_map_fix_val(bset
, type
, pos
, v
);
6435 __isl_give isl_basic_set
*isl_basic_set_fix_si(__isl_take isl_basic_set
*bset
,
6436 enum isl_dim_type type
, unsigned pos
, int value
)
6438 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset
),
6442 __isl_give isl_basic_set
*isl_basic_set_fix(__isl_take isl_basic_set
*bset
,
6443 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6445 return bset_from_bmap(isl_basic_map_fix(bset_to_bmap(bset
),
6449 /* Remove the basic map at position "i" from "map" if this basic map
6450 * is (obviously) empty.
6452 static __isl_give isl_map
*remove_if_empty(__isl_take isl_map
*map
, int i
)
6459 empty
= isl_basic_map_plain_is_empty(map
->p
[i
]);
6461 return isl_map_free(map
);
6465 isl_basic_map_free(map
->p
[i
]);
6468 map
->p
[i
] = map
->p
[map
->n
];
6469 map
= isl_map_unmark_normalized(map
);
6476 /* Perform "fn" on each basic map of "map", where we may not be holding
6477 * the only reference to "map".
6478 * In particular, "fn" should be a semantics preserving operation
6479 * that we want to apply to all copies of "map". We therefore need
6480 * to be careful not to modify "map" in a way that breaks "map"
6481 * in case anything goes wrong.
6483 __isl_give isl_map
*isl_map_inline_foreach_basic_map(__isl_take isl_map
*map
,
6484 __isl_give isl_basic_map
*(*fn
)(__isl_take isl_basic_map
*bmap
))
6486 struct isl_basic_map
*bmap
;
6492 for (i
= map
->n
- 1; i
>= 0; --i
) {
6493 bmap
= isl_basic_map_copy(map
->p
[i
]);
6497 isl_basic_map_free(map
->p
[i
]);
6499 map
= remove_if_empty(map
, i
);
6510 __isl_give isl_map
*isl_map_fix_si(__isl_take isl_map
*map
,
6511 enum isl_dim_type type
, unsigned pos
, int value
)
6515 map
= isl_map_cow(map
);
6516 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
6517 return isl_map_free(map
);
6518 for (i
= map
->n
- 1; i
>= 0; --i
) {
6519 map
->p
[i
] = isl_basic_map_fix_si(map
->p
[i
], type
, pos
, value
);
6520 map
= remove_if_empty(map
, i
);
6524 map
= isl_map_unmark_normalized(map
);
6528 __isl_give isl_set
*isl_set_fix_si(__isl_take isl_set
*set
,
6529 enum isl_dim_type type
, unsigned pos
, int value
)
6531 return set_from_map(isl_map_fix_si(set_to_map(set
), type
, pos
, value
));
6534 __isl_give isl_map
*isl_map_fix(__isl_take isl_map
*map
,
6535 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6539 map
= isl_map_cow(map
);
6540 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
6541 return isl_map_free(map
);
6542 for (i
= 0; i
< map
->n
; ++i
) {
6543 map
->p
[i
] = isl_basic_map_fix(map
->p
[i
], type
, pos
, value
);
6547 map
= isl_map_unmark_normalized(map
);
6554 __isl_give isl_set
*isl_set_fix(__isl_take isl_set
*set
,
6555 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6557 return set_from_map(isl_map_fix(set_to_map(set
), type
, pos
, value
));
6560 /* Fix the value of the variable at position "pos" of type "type" of "map"
6561 * to be equal to "v".
6563 __isl_give isl_map
*isl_map_fix_val(__isl_take isl_map
*map
,
6564 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6568 map
= isl_map_cow(map
);
6572 if (!isl_val_is_int(v
))
6573 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
6574 "expecting integer value", goto error
);
6575 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
6577 for (i
= map
->n
- 1; i
>= 0; --i
) {
6578 map
->p
[i
] = isl_basic_map_fix_val(map
->p
[i
], type
, pos
,
6580 map
= remove_if_empty(map
, i
);
6584 map
= isl_map_unmark_normalized(map
);
6593 /* Fix the value of the variable at position "pos" of type "type" of "set"
6594 * to be equal to "v".
6596 __isl_give isl_set
*isl_set_fix_val(__isl_take isl_set
*set
,
6597 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6599 return isl_map_fix_val(set
, type
, pos
, v
);
6602 __isl_give isl_map
*isl_map_fix_input_si(__isl_take isl_map
*map
,
6603 unsigned input
, int value
)
6605 return isl_map_fix_si(map
, isl_dim_in
, input
, value
);
6608 __isl_give isl_set
*isl_set_fix_dim_si(__isl_take isl_set
*set
, unsigned dim
,
6611 return set_from_map(isl_map_fix_si(set_to_map(set
),
6612 isl_dim_set
, dim
, value
));
6615 static __isl_give isl_basic_map
*basic_map_bound_si(
6616 __isl_take isl_basic_map
*bmap
,
6617 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
6622 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6623 return isl_basic_map_free(bmap
);
6624 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
6626 return isl_basic_map_free(bmap
);
6627 pos
+= isl_basic_map_offset(bmap
, type
);
6628 bmap
= isl_basic_map_cow(bmap
);
6629 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
6630 j
= isl_basic_map_alloc_inequality(bmap
);
6633 isl_seq_clr(bmap
->ineq
[j
], 1 + total
);
6635 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
6636 isl_int_set_si(bmap
->ineq
[j
][0], value
);
6638 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
6639 isl_int_set_si(bmap
->ineq
[j
][0], -value
);
6641 bmap
= isl_basic_map_simplify(bmap
);
6642 return isl_basic_map_finalize(bmap
);
6644 isl_basic_map_free(bmap
);
6648 __isl_give isl_basic_map
*isl_basic_map_lower_bound_si(
6649 __isl_take isl_basic_map
*bmap
,
6650 enum isl_dim_type type
, unsigned pos
, int value
)
6652 return basic_map_bound_si(bmap
, type
, pos
, value
, 0);
6655 /* Constrain the values of the given dimension to be no greater than "value".
6657 __isl_give isl_basic_map
*isl_basic_map_upper_bound_si(
6658 __isl_take isl_basic_map
*bmap
,
6659 enum isl_dim_type type
, unsigned pos
, int value
)
6661 return basic_map_bound_si(bmap
, type
, pos
, value
, 1);
6664 static __isl_give isl_map
*map_bound_si(__isl_take isl_map
*map
,
6665 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
6669 map
= isl_map_cow(map
);
6670 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
6671 return isl_map_free(map
);
6672 for (i
= 0; i
< map
->n
; ++i
) {
6673 map
->p
[i
] = basic_map_bound_si(map
->p
[i
],
6674 type
, pos
, value
, upper
);
6678 map
= isl_map_unmark_normalized(map
);
6685 __isl_give isl_map
*isl_map_lower_bound_si(__isl_take isl_map
*map
,
6686 enum isl_dim_type type
, unsigned pos
, int value
)
6688 return map_bound_si(map
, type
, pos
, value
, 0);
6691 __isl_give isl_map
*isl_map_upper_bound_si(__isl_take isl_map
*map
,
6692 enum isl_dim_type type
, unsigned pos
, int value
)
6694 return map_bound_si(map
, type
, pos
, value
, 1);
6697 __isl_give isl_set
*isl_set_lower_bound_si(__isl_take isl_set
*set
,
6698 enum isl_dim_type type
, unsigned pos
, int value
)
6700 return set_from_map(isl_map_lower_bound_si(set_to_map(set
),
6704 __isl_give isl_set
*isl_set_upper_bound_si(__isl_take isl_set
*set
,
6705 enum isl_dim_type type
, unsigned pos
, int value
)
6707 return isl_map_upper_bound_si(set
, type
, pos
, value
);
6710 /* Bound the given variable of "bmap" from below (or above is "upper"
6711 * is set) to "value".
6713 static __isl_give isl_basic_map
*basic_map_bound(
6714 __isl_take isl_basic_map
*bmap
,
6715 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
6720 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6721 return isl_basic_map_free(bmap
);
6722 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
6724 return isl_basic_map_free(bmap
);
6725 pos
+= isl_basic_map_offset(bmap
, type
);
6726 bmap
= isl_basic_map_cow(bmap
);
6727 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
6728 j
= isl_basic_map_alloc_inequality(bmap
);
6731 isl_seq_clr(bmap
->ineq
[j
], 1 + total
);
6733 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
6734 isl_int_set(bmap
->ineq
[j
][0], value
);
6736 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
6737 isl_int_neg(bmap
->ineq
[j
][0], value
);
6739 bmap
= isl_basic_map_simplify(bmap
);
6740 return isl_basic_map_finalize(bmap
);
6742 isl_basic_map_free(bmap
);
6746 /* Bound the given variable of "map" from below (or above is "upper"
6747 * is set) to "value".
6749 static __isl_give isl_map
*map_bound(__isl_take isl_map
*map
,
6750 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
6754 map
= isl_map_cow(map
);
6755 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
6756 return isl_map_free(map
);
6757 for (i
= map
->n
- 1; i
>= 0; --i
) {
6758 map
->p
[i
] = basic_map_bound(map
->p
[i
], type
, pos
, value
, upper
);
6759 map
= remove_if_empty(map
, i
);
6763 map
= isl_map_unmark_normalized(map
);
6767 __isl_give isl_map
*isl_map_lower_bound(__isl_take isl_map
*map
,
6768 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6770 return map_bound(map
, type
, pos
, value
, 0);
6773 __isl_give isl_map
*isl_map_upper_bound(__isl_take isl_map
*map
,
6774 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6776 return map_bound(map
, type
, pos
, value
, 1);
6779 __isl_give isl_set
*isl_set_lower_bound(__isl_take isl_set
*set
,
6780 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6782 return isl_map_lower_bound(set
, type
, pos
, value
);
6785 __isl_give isl_set
*isl_set_upper_bound(__isl_take isl_set
*set
,
6786 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6788 return isl_map_upper_bound(set
, type
, pos
, value
);
6791 /* Force the values of the variable at position "pos" of type "type" of "map"
6792 * to be no smaller than "value".
6794 __isl_give isl_map
*isl_map_lower_bound_val(__isl_take isl_map
*map
,
6795 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
6799 if (!isl_val_is_int(value
))
6800 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
6801 "expecting integer value", goto error
);
6802 map
= isl_map_lower_bound(map
, type
, pos
, value
->n
);
6803 isl_val_free(value
);
6806 isl_val_free(value
);
6811 /* Force the values of the variable at position "pos" of type "type" of "set"
6812 * to be no smaller than "value".
6814 __isl_give isl_set
*isl_set_lower_bound_val(__isl_take isl_set
*set
,
6815 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
6819 map
= set_to_map(set
);
6820 return set_from_map(isl_map_lower_bound_val(map
, type
, pos
, value
));
6823 /* Force the values of the variable at position "pos" of type "type" of "map"
6824 * to be no greater than "value".
6826 __isl_give isl_map
*isl_map_upper_bound_val(__isl_take isl_map
*map
,
6827 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
6831 if (!isl_val_is_int(value
))
6832 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
6833 "expecting integer value", goto error
);
6834 map
= isl_map_upper_bound(map
, type
, pos
, value
->n
);
6835 isl_val_free(value
);
6838 isl_val_free(value
);
6843 /* Force the values of the variable at position "pos" of type "type" of "set"
6844 * to be no greater than "value".
6846 __isl_give isl_set
*isl_set_upper_bound_val(__isl_take isl_set
*set
,
6847 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
6851 map
= set_to_map(set
);
6852 return set_from_map(isl_map_upper_bound_val(map
, type
, pos
, value
));
6857 #include "isl_map_bound_templ.c"
6859 /* Apply "map_bound" to "set" with the corresponding value in "bound"
6860 * for each set dimension, by treating the set as a map.
6862 static __isl_give isl_set
*set_bound_multi_val(__isl_take isl_set
*set
,
6863 __isl_take isl_multi_val
*bound
,
6864 __isl_give isl_map
*map_bound(__isl_take isl_map
*map
,
6865 unsigned pos
, __isl_take isl_val
*value
))
6869 map
= set_to_map(set
);
6870 return set_from_map(map_bound_multi_val(map
, bound
, map_bound
));
6875 #include "isl_map_bound_templ.c"
6877 /* Apply "map_bound" to "set" with the corresponding value in "bound"
6878 * for each set dimension, by converting the set and the bound
6879 * to objects living in a map space.
6881 static __isl_give isl_set
*set_bound_multi_pw_aff(__isl_take isl_set
*set
,
6882 __isl_take isl_multi_pw_aff
*bound
,
6883 __isl_give isl_map
*set_bound(__isl_take isl_map
*map
,
6884 unsigned pos
, __isl_take TYPE
*value
))
6888 map
= isl_map_from_range(set
);
6889 bound
= isl_multi_pw_aff_from_range(bound
);
6890 map
= map_bound_multi_pw_aff(map
, bound
, set_bound
);
6891 return isl_map_range(map
);
6894 /* Wrapper around isl_map_lower_bound_val for use in map_bound_multi_val,
6895 * setting a bound on the given output dimension.
6897 static __isl_give isl_map
*map_lower_bound_val(__isl_take isl_map
*map
,
6898 unsigned pos
, __isl_take isl_val
*v
)
6900 return isl_map_lower_bound_val(map
, isl_dim_out
, pos
, v
);
6903 /* Force the values of the set dimensions of "set"
6904 * to be no smaller than the corresponding values in "lower".
6906 __isl_give isl_set
*isl_set_lower_bound_multi_val(__isl_take isl_set
*set
,
6907 __isl_take isl_multi_val
*lower
)
6909 return set_bound_multi_val(set
, lower
, &map_lower_bound_val
);
6912 /* Force the values of the output dimensions of "map"
6913 * to be no smaller than the corresponding values in "lower".
6915 __isl_give isl_map
*isl_map_lower_bound_multi_val(__isl_take isl_map
*map
,
6916 __isl_take isl_multi_val
*lower
)
6918 return map_bound_multi_val(map
, lower
, &map_lower_bound_val
);
6921 /* Wrapper around isl_map_upper_bound_val for use in map_bound_multi_val,
6922 * setting a bound on the given output dimension.
6924 static __isl_give isl_map
*map_upper_bound_val(__isl_take isl_map
*map
,
6925 unsigned pos
, __isl_take isl_val
*v
)
6927 return isl_map_upper_bound_val(map
, isl_dim_out
, pos
, v
);
6930 /* Force the values of the set dimensions of "set"
6931 * to be no greater than the corresponding values in "upper".
6933 __isl_give isl_set
*isl_set_upper_bound_multi_val(__isl_take isl_set
*set
,
6934 __isl_take isl_multi_val
*upper
)
6936 return set_bound_multi_val(set
, upper
, &map_upper_bound_val
);
6939 /* Force the values of the set dimensions of "set"
6940 * to be no greater than the corresponding values in "upper".
6942 __isl_give isl_map
*isl_map_upper_bound_multi_val(__isl_take isl_map
*map
,
6943 __isl_take isl_multi_val
*upper
)
6945 return map_bound_multi_val(map
, upper
, &map_upper_bound_val
);
6948 /* Force the symbolic constant expression "bound"
6949 * to satisfy the relation "order" with respect to
6950 * the output variable at position "pos" of "map".
6952 * Create an affine expression representing the output variable
6953 * in terms of the range and
6954 * compare it using "order" to "bound" (defined on the domain).
6955 * The result is a relation between elements in domain and range that
6956 * can be intersected with "map".
6958 static __isl_give isl_map
*map_bound_pw_aff(__isl_take isl_map
*map
,
6959 unsigned pos
, __isl_take isl_pw_aff
*bound
,
6960 __isl_give isl_map
*(*order
)(__isl_take isl_pw_aff
*pa1
,
6961 __isl_take isl_pw_aff
*pa2
))
6964 isl_local_space
*ls
;
6967 space
= isl_space_range(isl_map_get_space(map
));
6968 ls
= isl_local_space_from_space(space
);
6969 var
= isl_pw_aff_var_on_domain(ls
, isl_dim_set
, pos
);
6970 map
= isl_map_intersect(map
, order(bound
, var
));
6974 /* Force the values of the output variable at position "pos" of "map"
6975 * to be no smaller than the symbolic constant expression "lower".
6977 static __isl_give isl_map
*map_lower_bound_pw_aff(__isl_take isl_map
*map
,
6978 unsigned pos
, __isl_take isl_pw_aff
*lower
)
6980 return map_bound_pw_aff(map
, pos
, lower
, &isl_pw_aff_le_map
);
6983 /* Force the values of the output variable at position "pos" of "map"
6984 * to be no greater than the symbolic constant expression "upper".
6986 static __isl_give isl_map
*map_upper_bound_pw_aff(__isl_take isl_map
*map
,
6987 unsigned pos
, __isl_take isl_pw_aff
*upper
)
6989 return map_bound_pw_aff(map
, pos
, upper
, &isl_pw_aff_ge_map
);
6992 /* Force the values of the set dimensions of "set"
6993 * to be no smaller than the corresponding constant symbolic expressions
6996 __isl_give isl_set
*isl_set_lower_bound_multi_pw_aff(__isl_take isl_set
*set
,
6997 __isl_take isl_multi_pw_aff
*lower
)
6999 return set_bound_multi_pw_aff(set
, lower
, &map_lower_bound_pw_aff
);
7002 /* Force the values of the set dimensions of "set"
7003 * to be no greater than the corresponding constant symbolic expressions
7006 __isl_give isl_set
*isl_set_upper_bound_multi_pw_aff(__isl_take isl_set
*set
,
7007 __isl_take isl_multi_pw_aff
*upper
)
7009 return set_bound_multi_pw_aff(set
, upper
, &map_upper_bound_pw_aff
);
7012 /* Force the values of the output dimensions of "map"
7013 * to be no smaller than the corresponding constant symbolic expressions
7016 __isl_give isl_map
*isl_map_lower_bound_multi_pw_aff(__isl_take isl_map
*map
,
7017 __isl_take isl_multi_pw_aff
*lower
)
7019 return map_bound_multi_pw_aff(map
, lower
, &map_lower_bound_pw_aff
);
7022 /* Force the values of the output dimensions of "map"
7023 * to be no greater than the corresponding constant symbolic expressions
7026 __isl_give isl_map
*isl_map_upper_bound_multi_pw_aff(__isl_take isl_map
*map
,
7027 __isl_take isl_multi_pw_aff
*upper
)
7029 return map_bound_multi_pw_aff(map
, upper
, &map_upper_bound_pw_aff
);
7032 /* Bound the given variable of "bset" from below (or above is "upper"
7033 * is set) to "value".
7035 static __isl_give isl_basic_set
*isl_basic_set_bound(
7036 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned pos
,
7037 isl_int value
, int upper
)
7039 return bset_from_bmap(basic_map_bound(bset_to_bmap(bset
),
7040 type
, pos
, value
, upper
));
7043 /* Bound the given variable of "bset" from below (or above is "upper"
7044 * is set) to "value".
7046 static __isl_give isl_basic_set
*isl_basic_set_bound_val(
7047 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned pos
,
7048 __isl_take isl_val
*value
, int upper
)
7052 if (!isl_val_is_int(value
))
7053 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
7054 "expecting integer value", goto error
);
7055 bset
= isl_basic_set_bound(bset
, type
, pos
, value
->n
, upper
);
7056 isl_val_free(value
);
7059 isl_val_free(value
);
7060 isl_basic_set_free(bset
);
7064 /* Bound the given variable of "bset" from below to "value".
7066 __isl_give isl_basic_set
*isl_basic_set_lower_bound_val(
7067 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned pos
,
7068 __isl_take isl_val
*value
)
7070 return isl_basic_set_bound_val(bset
, type
, pos
, value
, 0);
7073 /* Bound the given variable of "bset" from above to "value".
7075 __isl_give isl_basic_set
*isl_basic_set_upper_bound_val(
7076 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned pos
,
7077 __isl_take isl_val
*value
)
7079 return isl_basic_set_bound_val(bset
, type
, pos
, value
, 1);
7082 __isl_give isl_map
*isl_map_reverse(__isl_take isl_map
*map
)
7084 return isl_map_transform(map
, &isl_space_reverse
,
7085 &isl_basic_map_reverse
);
7088 /* Given a map A -> (B -> C), return the corresponding map A -> (C -> B).
7090 __isl_give isl_map
*isl_map_range_reverse(__isl_take isl_map
*map
)
7092 return isl_map_transform(map
, &isl_space_range_reverse
,
7093 &isl_basic_map_range_reverse
);
7097 #define TYPE isl_pw_multi_aff
7099 #define SUFFIX _pw_multi_aff
7101 #define EMPTY isl_pw_multi_aff_empty
7103 #define ADD isl_pw_multi_aff_union_add
7104 #include "isl_map_lexopt_templ.c"
7106 /* Given a map "map", compute the lexicographically minimal
7107 * (or maximal) image element for each domain element in dom,
7108 * in the form of an isl_pw_multi_aff.
7109 * If "empty" is not NULL, then set *empty to those elements in dom that
7110 * do not have an image element.
7111 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
7112 * should be computed over the domain of "map". "empty" is also NULL
7115 * We first compute the lexicographically minimal or maximal element
7116 * in the first basic map. This results in a partial solution "res"
7117 * and a subset "todo" of dom that still need to be handled.
7118 * We then consider each of the remaining maps in "map" and successively
7119 * update both "res" and "todo".
7120 * If "empty" is NULL, then the todo sets are not needed and therefore
7121 * also not computed.
7123 static __isl_give isl_pw_multi_aff
*isl_map_partial_lexopt_aligned_pw_multi_aff(
7124 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
7125 __isl_give isl_set
**empty
, unsigned flags
)
7129 isl_pw_multi_aff
*res
;
7132 full
= ISL_FL_ISSET(flags
, ISL_OPT_FULL
);
7133 if (!map
|| (!full
&& !dom
))
7136 if (isl_map_plain_is_empty(map
)) {
7141 return isl_pw_multi_aff_from_map(map
);
7144 res
= basic_map_partial_lexopt_pw_multi_aff(
7145 isl_basic_map_copy(map
->p
[0]),
7146 isl_set_copy(dom
), empty
, flags
);
7150 for (i
= 1; i
< map
->n
; ++i
) {
7151 isl_pw_multi_aff
*res_i
;
7153 res_i
= basic_map_partial_lexopt_pw_multi_aff(
7154 isl_basic_map_copy(map
->p
[i
]),
7155 isl_set_copy(dom
), empty
, flags
);
7157 if (ISL_FL_ISSET(flags
, ISL_OPT_MAX
))
7158 res
= isl_pw_multi_aff_union_lexmax(res
, res_i
);
7160 res
= isl_pw_multi_aff_union_lexmin(res
, res_i
);
7163 todo
= isl_set_intersect(todo
, *empty
);
7182 #define TYPE isl_map
7186 #define EMPTY isl_map_empty
7188 #define ADD isl_map_union_disjoint
7189 #include "isl_map_lexopt_templ.c"
7191 /* Given a map "map", compute the lexicographically minimal
7192 * (or maximal) image element for each domain element in "dom",
7193 * in the form of an isl_map.
7194 * If "empty" is not NULL, then set *empty to those elements in "dom" that
7195 * do not have an image element.
7196 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
7197 * should be computed over the domain of "map". "empty" is also NULL
7200 * If the input consists of more than one disjunct, then first
7201 * compute the desired result in the form of an isl_pw_multi_aff and
7202 * then convert that into an isl_map.
7204 * This function used to have an explicit implementation in terms
7205 * of isl_maps, but it would continually intersect the domains of
7206 * partial results with the complement of the domain of the next
7207 * partial solution, potentially leading to an explosion in the number
7208 * of disjuncts if there are several disjuncts in the input.
7209 * An even earlier implementation of this function would look for
7210 * better results in the domain of the partial result and for extra
7211 * results in the complement of this domain, which would lead to
7212 * even more splintering.
7214 static __isl_give isl_map
*isl_map_partial_lexopt_aligned(
7215 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
7216 __isl_give isl_set
**empty
, unsigned flags
)
7219 struct isl_map
*res
;
7220 isl_pw_multi_aff
*pma
;
7222 full
= ISL_FL_ISSET(flags
, ISL_OPT_FULL
);
7223 if (!map
|| (!full
&& !dom
))
7226 if (isl_map_plain_is_empty(map
)) {
7235 res
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[0]),
7241 pma
= isl_map_partial_lexopt_aligned_pw_multi_aff(map
, dom
, empty
,
7243 return isl_map_from_pw_multi_aff_internal(pma
);
7252 __isl_give isl_map
*isl_map_partial_lexmax(
7253 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
7254 __isl_give isl_set
**empty
)
7256 return isl_map_partial_lexopt(map
, dom
, empty
, ISL_OPT_MAX
);
7259 __isl_give isl_map
*isl_map_partial_lexmin(
7260 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
7261 __isl_give isl_set
**empty
)
7263 return isl_map_partial_lexopt(map
, dom
, empty
, 0);
7266 __isl_give isl_set
*isl_set_partial_lexmin(
7267 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
7268 __isl_give isl_set
**empty
)
7270 return set_from_map(isl_map_partial_lexmin(set_to_map(set
),
7274 __isl_give isl_set
*isl_set_partial_lexmax(
7275 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
7276 __isl_give isl_set
**empty
)
7278 return set_from_map(isl_map_partial_lexmax(set_to_map(set
),
7282 /* Compute the lexicographic minimum (or maximum if "flags" includes
7283 * ISL_OPT_MAX) of "bset" over its parametric domain.
7285 __isl_give isl_set
*isl_basic_set_lexopt(__isl_take isl_basic_set
*bset
,
7288 return isl_basic_map_lexopt(bset
, flags
);
7291 __isl_give isl_map
*isl_basic_map_lexmax(__isl_take isl_basic_map
*bmap
)
7293 return isl_basic_map_lexopt(bmap
, ISL_OPT_MAX
);
7296 __isl_give isl_set
*isl_basic_set_lexmin(__isl_take isl_basic_set
*bset
)
7298 return set_from_map(isl_basic_map_lexmin(bset_to_bmap(bset
)));
7301 __isl_give isl_set
*isl_basic_set_lexmax(__isl_take isl_basic_set
*bset
)
7303 return set_from_map(isl_basic_map_lexmax(bset_to_bmap(bset
)));
7306 /* Compute the lexicographic minimum of "bset" over its parametric domain
7307 * for the purpose of quantifier elimination.
7308 * That is, find an explicit representation for all the existentially
7309 * quantified variables in "bset" by computing their lexicographic
7312 static __isl_give isl_set
*isl_basic_set_lexmin_compute_divs(
7313 __isl_take isl_basic_set
*bset
)
7315 return isl_basic_set_lexopt(bset
, ISL_OPT_QE
);
7318 /* Given a basic map with one output dimension, compute the minimum or
7319 * maximum of that dimension as an isl_pw_aff.
7321 * Compute the optimum as a lexicographic optimum over the single
7322 * output dimension and extract the single isl_pw_aff from the result.
7324 static __isl_give isl_pw_aff
*basic_map_dim_opt(__isl_keep isl_basic_map
*bmap
,
7327 isl_pw_multi_aff
*pma
;
7330 bmap
= isl_basic_map_copy(bmap
);
7331 pma
= isl_basic_map_lexopt_pw_multi_aff(bmap
, max
? ISL_OPT_MAX
: 0);
7332 pwaff
= isl_pw_multi_aff_get_pw_aff(pma
, 0);
7333 isl_pw_multi_aff_free(pma
);
7338 /* Compute the minimum or maximum of the given output dimension
7339 * as a function of the parameters and the input dimensions,
7340 * but independently of the other output dimensions.
7342 * We first project out the other output dimension and then compute
7343 * the "lexicographic" maximum in each basic map, combining the results
7344 * using isl_pw_aff_union_max.
7346 static __isl_give isl_pw_aff
*map_dim_opt(__isl_take isl_map
*map
, int pos
,
7353 n_out
= isl_map_dim(map
, isl_dim_out
);
7355 map
= isl_map_free(map
);
7356 map
= isl_map_project_out(map
, isl_dim_out
, pos
+ 1, n_out
- (pos
+ 1));
7357 map
= isl_map_project_out(map
, isl_dim_out
, 0, pos
);
7362 isl_space
*space
= isl_map_get_space(map
);
7364 return isl_pw_aff_empty(space
);
7367 pwaff
= basic_map_dim_opt(map
->p
[0], max
);
7368 for (i
= 1; i
< map
->n
; ++i
) {
7369 isl_pw_aff
*pwaff_i
;
7371 pwaff_i
= basic_map_dim_opt(map
->p
[i
], max
);
7372 pwaff
= isl_pw_aff_union_opt(pwaff
, pwaff_i
, max
);
7380 /* Compute the minimum of the given output dimension as a function of the
7381 * parameters and input dimensions, but independently of
7382 * the other output dimensions.
7384 __isl_give isl_pw_aff
*isl_map_dim_min(__isl_take isl_map
*map
, int pos
)
7386 return map_dim_opt(map
, pos
, 0);
7389 /* Compute the maximum of the given output dimension as a function of the
7390 * parameters and input dimensions, but independently of
7391 * the other output dimensions.
7393 __isl_give isl_pw_aff
*isl_map_dim_max(__isl_take isl_map
*map
, int pos
)
7395 return map_dim_opt(map
, pos
, 1);
7398 /* Compute the minimum or maximum of the given set dimension
7399 * as a function of the parameters,
7400 * but independently of the other set dimensions.
7402 static __isl_give isl_pw_aff
*set_dim_opt(__isl_take isl_set
*set
, int pos
,
7405 return map_dim_opt(set
, pos
, max
);
7408 /* Compute the maximum of the given set dimension as a function of the
7409 * parameters, but independently of the other set dimensions.
7411 __isl_give isl_pw_aff
*isl_set_dim_max(__isl_take isl_set
*set
, int pos
)
7413 return set_dim_opt(set
, pos
, 1);
7416 /* Compute the minimum of the given set dimension as a function of the
7417 * parameters, but independently of the other set dimensions.
7419 __isl_give isl_pw_aff
*isl_set_dim_min(__isl_take isl_set
*set
, int pos
)
7421 return set_dim_opt(set
, pos
, 0);
7424 /* Apply a preimage specified by "mat" on the parameters of "bset".
7425 * bset is assumed to have only parameters and divs.
7427 static __isl_give isl_basic_set
*basic_set_parameter_preimage(
7428 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*mat
)
7432 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
7433 if (nparam
< 0 || !mat
)
7436 bset
->dim
= isl_space_cow(bset
->dim
);
7440 isl_assert(bset
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
7442 bset
->dim
->nparam
= 0;
7443 bset
->dim
->n_out
= nparam
;
7444 bset
= isl_basic_set_preimage(bset
, mat
);
7446 bset
->dim
->nparam
= bset
->dim
->n_out
;
7447 bset
->dim
->n_out
= 0;
7452 isl_basic_set_free(bset
);
7456 /* Apply a preimage specified by "mat" on the parameters of "set".
7457 * set is assumed to have only parameters and divs.
7459 static __isl_give isl_set
*set_parameter_preimage(__isl_take isl_set
*set
,
7460 __isl_take isl_mat
*mat
)
7465 nparam
= isl_set_dim(set
, isl_dim_param
);
7466 if (nparam
< 0 || !mat
)
7469 if (mat
->n_row
!= 1 + nparam
)
7470 isl_die(isl_set_get_ctx(set
), isl_error_internal
,
7471 "unexpected number of rows", goto error
);
7473 space
= isl_set_get_space(set
);
7474 space
= isl_space_move_dims(space
, isl_dim_set
, 0,
7475 isl_dim_param
, 0, nparam
);
7476 set
= isl_set_reset_space(set
, space
);
7477 set
= isl_set_preimage(set
, mat
);
7478 nparam
= isl_set_dim(set
, isl_dim_out
);
7480 set
= isl_set_free(set
);
7481 space
= isl_set_get_space(set
);
7482 space
= isl_space_move_dims(space
, isl_dim_param
, 0,
7483 isl_dim_out
, 0, nparam
);
7484 set
= isl_set_reset_space(set
, space
);
7492 /* Intersect the basic set "bset" with the affine space specified by the
7493 * equalities in "eq".
7495 static __isl_give isl_basic_set
*basic_set_append_equalities(
7496 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*eq
)
7504 bset
= isl_basic_set_extend(bset
, 0, eq
->n_row
, 0);
7508 len
= isl_basic_set_offset(bset
, isl_dim_div
) + bset
->extra
;
7509 for (i
= 0; i
< eq
->n_row
; ++i
) {
7510 k
= isl_basic_set_alloc_equality(bset
);
7513 isl_seq_cpy(bset
->eq
[k
], eq
->row
[i
], eq
->n_col
);
7514 isl_seq_clr(bset
->eq
[k
] + eq
->n_col
, len
- eq
->n_col
);
7518 bset
= isl_basic_set_gauss(bset
, NULL
);
7519 bset
= isl_basic_set_finalize(bset
);
7524 isl_basic_set_free(bset
);
7528 /* Intersect the set "set" with the affine space specified by the
7529 * equalities in "eq".
7531 static __isl_give isl_set
*set_append_equalities(__isl_take isl_set
*set
,
7532 __isl_take isl_mat
*eq
)
7539 for (i
= 0; i
< set
->n
; ++i
) {
7540 set
->p
[i
] = basic_set_append_equalities(set
->p
[i
],
7553 /* Given a basic set "bset" that only involves parameters and existentially
7554 * quantified variables, return the index of the first equality
7555 * that only involves parameters. If there is no such equality then
7556 * return bset->n_eq.
7558 * This function assumes that isl_basic_set_gauss has been called on "bset".
7560 static int first_parameter_equality(__isl_keep isl_basic_set
*bset
)
7563 isl_size nparam
, n_div
;
7565 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
7566 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
7567 if (nparam
< 0 || n_div
< 0)
7570 for (i
= 0, j
= n_div
- 1; i
< bset
->n_eq
&& j
>= 0; --j
) {
7571 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ j
]))
7578 /* Compute an explicit representation for the existentially quantified
7579 * variables in "bset" by computing the "minimal value" of the set
7580 * variables. Since there are no set variables, the computation of
7581 * the minimal value essentially computes an explicit representation
7582 * of the non-empty part(s) of "bset".
7584 * The input only involves parameters and existentially quantified variables.
7585 * All equalities among parameters have been removed.
7587 * Since the existentially quantified variables in the result are in general
7588 * going to be different from those in the input, we first replace
7589 * them by the minimal number of variables based on their equalities.
7590 * This should simplify the parametric integer programming.
7592 static __isl_give isl_set
*base_compute_divs(__isl_take isl_basic_set
*bset
)
7594 isl_morph
*morph1
, *morph2
;
7600 if (bset
->n_eq
== 0)
7601 return isl_basic_set_lexmin_compute_divs(bset
);
7603 morph1
= isl_basic_set_parameter_compression(bset
);
7604 bset
= isl_morph_basic_set(isl_morph_copy(morph1
), bset
);
7605 bset
= isl_basic_set_lift(bset
);
7606 morph2
= isl_basic_set_variable_compression(bset
, isl_dim_set
);
7607 bset
= isl_morph_basic_set(morph2
, bset
);
7608 n
= isl_basic_set_dim(bset
, isl_dim_set
);
7610 bset
= isl_basic_set_free(bset
);
7611 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
7613 set
= isl_basic_set_lexmin_compute_divs(bset
);
7615 set
= isl_morph_set(isl_morph_inverse(morph1
), set
);
7620 /* Project the given basic set onto its parameter domain, possibly introducing
7621 * new, explicit, existential variables in the constraints.
7622 * The input has parameters and (possibly implicit) existential variables.
7623 * The output has the same parameters, but only
7624 * explicit existentially quantified variables.
7626 * The actual projection is performed by pip, but pip doesn't seem
7627 * to like equalities very much, so we first remove the equalities
7628 * among the parameters by performing a variable compression on
7629 * the parameters. Afterward, an inverse transformation is performed
7630 * and the equalities among the parameters are inserted back in.
7632 * The variable compression on the parameters may uncover additional
7633 * equalities that were only implicit before. We therefore check
7634 * if there are any new parameter equalities in the result and
7635 * if so recurse. The removal of parameter equalities is required
7636 * for the parameter compression performed by base_compute_divs.
7638 static __isl_give isl_set
*parameter_compute_divs(
7639 __isl_take isl_basic_set
*bset
)
7643 struct isl_mat
*T
, *T2
;
7644 struct isl_set
*set
;
7647 bset
= isl_basic_set_cow(bset
);
7651 if (bset
->n_eq
== 0)
7652 return base_compute_divs(bset
);
7654 bset
= isl_basic_set_gauss(bset
, NULL
);
7657 if (isl_basic_set_plain_is_empty(bset
))
7658 return isl_set_from_basic_set(bset
);
7660 i
= first_parameter_equality(bset
);
7661 if (i
== bset
->n_eq
)
7662 return base_compute_divs(bset
);
7664 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
7666 return isl_set_from_basic_set(isl_basic_set_free(bset
));
7667 eq
= isl_mat_sub_alloc6(bset
->ctx
, bset
->eq
, i
, bset
->n_eq
- i
,
7669 eq
= isl_mat_cow(eq
);
7670 T
= isl_mat_variable_compression(isl_mat_copy(eq
), &T2
);
7671 if (T
&& T
->n_col
== 0) {
7675 bset
= isl_basic_set_set_to_empty(bset
);
7676 return isl_set_from_basic_set(bset
);
7678 bset
= basic_set_parameter_preimage(bset
, T
);
7680 i
= first_parameter_equality(bset
);
7683 else if (i
== bset
->n_eq
)
7684 set
= base_compute_divs(bset
);
7686 set
= parameter_compute_divs(bset
);
7687 set
= set_parameter_preimage(set
, T2
);
7688 set
= set_append_equalities(set
, eq
);
7692 /* Insert the divs from "ls" before those of "bmap".
7694 * The number of columns is not changed, which means that the last
7695 * dimensions of "bmap" are being reintepreted as the divs from "ls".
7696 * The caller is responsible for removing the same number of dimensions
7697 * from the space of "bmap".
7699 static __isl_give isl_basic_map
*insert_divs_from_local_space(
7700 __isl_take isl_basic_map
*bmap
, __isl_keep isl_local_space
*ls
)
7706 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
7708 return isl_basic_map_free(bmap
);
7712 old_n_div
= bmap
->n_div
;
7713 bmap
= insert_div_rows(bmap
, n_div
);
7717 for (i
= 0; i
< n_div
; ++i
) {
7718 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], ls
->div
->n_col
);
7719 isl_seq_clr(bmap
->div
[i
] + ls
->div
->n_col
, old_n_div
);
7725 /* Replace the space of "bmap" by the space and divs of "ls".
7727 * If "ls" has any divs, then we simplify the result since we may
7728 * have discovered some additional equalities that could simplify
7729 * the div expressions.
7731 static __isl_give isl_basic_map
*basic_replace_space_by_local_space(
7732 __isl_take isl_basic_map
*bmap
, __isl_take isl_local_space
*ls
)
7736 bmap
= isl_basic_map_cow(bmap
);
7737 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
7738 if (!bmap
|| n_div
< 0)
7741 bmap
= insert_divs_from_local_space(bmap
, ls
);
7745 isl_space_free(bmap
->dim
);
7746 bmap
->dim
= isl_local_space_get_space(ls
);
7750 isl_local_space_free(ls
);
7752 bmap
= isl_basic_map_simplify(bmap
);
7753 bmap
= isl_basic_map_finalize(bmap
);
7756 isl_basic_map_free(bmap
);
7757 isl_local_space_free(ls
);
7761 /* Replace the space of "map" by the space and divs of "ls".
7763 static __isl_give isl_map
*replace_space_by_local_space(__isl_take isl_map
*map
,
7764 __isl_take isl_local_space
*ls
)
7768 map
= isl_map_cow(map
);
7772 for (i
= 0; i
< map
->n
; ++i
) {
7773 map
->p
[i
] = basic_replace_space_by_local_space(map
->p
[i
],
7774 isl_local_space_copy(ls
));
7778 isl_space_free(isl_map_take_space(map
));
7779 map
= isl_map_restore_space(map
, isl_local_space_get_space(ls
));
7781 isl_local_space_free(ls
);
7784 isl_local_space_free(ls
);
7789 /* Compute an explicit representation for the existentially
7790 * quantified variables for which do not know any explicit representation yet.
7792 * We first sort the existentially quantified variables so that the
7793 * existentially quantified variables for which we already have an explicit
7794 * representation are placed before those for which we do not.
7795 * The input dimensions, the output dimensions and the existentially
7796 * quantified variables for which we already have an explicit
7797 * representation are then turned into parameters.
7798 * compute_divs returns a map with the same parameters and
7799 * no input or output dimensions and the dimension specification
7800 * is reset to that of the input, including the existentially quantified
7801 * variables for which we already had an explicit representation.
7803 static __isl_give isl_map
*compute_divs(__isl_take isl_basic_map
*bmap
)
7805 struct isl_basic_set
*bset
;
7806 struct isl_set
*set
;
7807 struct isl_map
*map
;
7809 isl_local_space
*ls
;
7816 bmap
= isl_basic_map_sort_divs(bmap
);
7817 bmap
= isl_basic_map_cow(bmap
);
7821 n_known
= isl_basic_map_first_unknown_div(bmap
);
7822 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
7823 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
7824 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
7825 if (n_known
< 0 || nparam
< 0 || n_in
< 0 || n_out
< 0)
7826 return isl_map_from_basic_map(isl_basic_map_free(bmap
));
7828 space
= isl_space_set_alloc(bmap
->ctx
,
7829 nparam
+ n_in
+ n_out
+ n_known
, 0);
7833 ls
= isl_basic_map_get_local_space(bmap
);
7834 ls
= isl_local_space_drop_dims(ls
, isl_dim_div
,
7835 n_known
, bmap
->n_div
- n_known
);
7837 for (i
= n_known
; i
< bmap
->n_div
; ++i
)
7838 swap_div(bmap
, i
- n_known
, i
);
7839 bmap
->n_div
-= n_known
;
7840 bmap
->extra
-= n_known
;
7842 bmap
= isl_basic_map_reset_space(bmap
, space
);
7843 bset
= bset_from_bmap(bmap
);
7845 set
= parameter_compute_divs(bset
);
7846 map
= set_to_map(set
);
7847 map
= replace_space_by_local_space(map
, ls
);
7851 isl_basic_map_free(bmap
);
7855 /* Remove the explicit representation of local variable "div",
7858 __isl_give isl_basic_map
*isl_basic_map_mark_div_unknown(
7859 __isl_take isl_basic_map
*bmap
, int div
)
7863 unknown
= isl_basic_map_div_is_marked_unknown(bmap
, div
);
7865 return isl_basic_map_free(bmap
);
7869 bmap
= isl_basic_map_cow(bmap
);
7872 isl_int_set_si(bmap
->div
[div
][0], 0);
7876 /* Is local variable "div" of "bmap" marked as not having an explicit
7878 * Note that even if "div" is not marked in this way and therefore
7879 * has an explicit representation, this representation may still
7880 * depend (indirectly) on other local variables that do not
7881 * have an explicit representation.
7883 isl_bool
isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map
*bmap
,
7886 if (isl_basic_map_check_range(bmap
, isl_dim_div
, div
, 1) < 0)
7887 return isl_bool_error
;
7888 return isl_int_is_zero(bmap
->div
[div
][0]);
7891 /* Return the position of the first local variable that does not
7892 * have an explicit representation.
7893 * Return the total number of local variables if they all have
7894 * an explicit representation.
7895 * Return -1 on error.
7897 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map
*bmap
)
7904 for (i
= 0; i
< bmap
->n_div
; ++i
) {
7905 if (!isl_basic_map_div_is_known(bmap
, i
))
7911 /* Return the position of the first local variable that does not
7912 * have an explicit representation.
7913 * Return the total number of local variables if they all have
7914 * an explicit representation.
7915 * Return -1 on error.
7917 int isl_basic_set_first_unknown_div(__isl_keep isl_basic_set
*bset
)
7919 return isl_basic_map_first_unknown_div(bset
);
7922 /* Does "bmap" have an explicit representation for all local variables?
7924 isl_bool
isl_basic_map_divs_known(__isl_keep isl_basic_map
*bmap
)
7929 n
= isl_basic_map_dim(bmap
, isl_dim_div
);
7930 first
= isl_basic_map_first_unknown_div(bmap
);
7931 if (n
< 0 || first
< 0)
7932 return isl_bool_error
;
7936 /* Do all basic maps in "map" have an explicit representation
7937 * for all local variables?
7939 isl_bool
isl_map_divs_known(__isl_keep isl_map
*map
)
7944 return isl_bool_error
;
7946 for (i
= 0; i
< map
->n
; ++i
) {
7947 int known
= isl_basic_map_divs_known(map
->p
[i
]);
7952 return isl_bool_true
;
7955 /* If bmap contains any unknown divs, then compute explicit
7956 * expressions for them. However, this computation may be
7957 * quite expensive, so first try to remove divs that aren't
7960 __isl_give isl_map
*isl_basic_map_compute_divs(__isl_take isl_basic_map
*bmap
)
7963 struct isl_map
*map
;
7965 known
= isl_basic_map_divs_known(bmap
);
7969 return isl_map_from_basic_map(bmap
);
7971 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
7973 known
= isl_basic_map_divs_known(bmap
);
7977 return isl_map_from_basic_map(bmap
);
7979 map
= compute_divs(bmap
);
7982 isl_basic_map_free(bmap
);
7986 __isl_give isl_map
*isl_map_compute_divs(__isl_take isl_map
*map
)
7990 struct isl_map
*res
;
7997 known
= isl_map_divs_known(map
);
8005 res
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[0]));
8006 for (i
= 1 ; i
< map
->n
; ++i
) {
8008 r2
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[i
]));
8009 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
8010 res
= isl_map_union_disjoint(res
, r2
);
8012 res
= isl_map_union(res
, r2
);
8019 __isl_give isl_set
*isl_basic_set_compute_divs(__isl_take isl_basic_set
*bset
)
8021 return set_from_map(isl_basic_map_compute_divs(bset_to_bmap(bset
)));
8024 __isl_give isl_set
*isl_set_compute_divs(__isl_take isl_set
*set
)
8026 return set_from_map(isl_map_compute_divs(set_to_map(set
)));
8029 __isl_give isl_set
*isl_map_domain(__isl_take isl_map
*map
)
8032 struct isl_set
*set
;
8037 map
= isl_map_cow(map
);
8041 set
= set_from_map(map
);
8042 set
->dim
= isl_space_domain(set
->dim
);
8045 for (i
= 0; i
< map
->n
; ++i
) {
8046 set
->p
[i
] = isl_basic_map_domain(map
->p
[i
]);
8050 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
8051 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
8058 /* Return the union of "map1" and "map2", where we assume for now that
8059 * "map1" and "map2" are disjoint. Note that the basic maps inside
8060 * "map1" or "map2" may not be disjoint from each other.
8061 * Also note that this function is also called from isl_map_union,
8062 * which takes care of handling the situation where "map1" and "map2"
8063 * may not be disjoint.
8065 * If one of the inputs is empty, we can simply return the other input.
8066 * Similarly, if one of the inputs is universal, then it is equal to the union.
8068 static __isl_give isl_map
*map_union_disjoint(__isl_take isl_map
*map1
,
8069 __isl_take isl_map
*map2
)
8073 struct isl_map
*map
= NULL
;
8076 if (isl_map_check_equal_space(map1
, map2
) < 0)
8088 is_universe
= isl_map_plain_is_universe(map1
);
8089 if (is_universe
< 0)
8096 is_universe
= isl_map_plain_is_universe(map2
);
8097 if (is_universe
< 0)
8104 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
8105 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
8106 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
8108 map
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
8109 map1
->n
+ map2
->n
, flags
);
8112 for (i
= 0; i
< map1
->n
; ++i
) {
8113 map
= isl_map_add_basic_map(map
,
8114 isl_basic_map_copy(map1
->p
[i
]));
8118 for (i
= 0; i
< map2
->n
; ++i
) {
8119 map
= isl_map_add_basic_map(map
,
8120 isl_basic_map_copy(map2
->p
[i
]));
8134 /* Return the union of "map1" and "map2", where "map1" and "map2" are
8135 * guaranteed to be disjoint by the caller.
8137 * Note that this functions is called from within isl_map_make_disjoint,
8138 * so we have to be careful not to touch the constraints of the inputs
8141 __isl_give isl_map
*isl_map_union_disjoint(__isl_take isl_map
*map1
,
8142 __isl_take isl_map
*map2
)
8144 isl_map_align_params_bin(&map1
, &map2
);
8145 return map_union_disjoint(map1
, map2
);
8148 /* Return the union of "map1" and "map2", where "map1" and "map2" may
8151 * We currently simply call map_union_disjoint, the internal operation
8152 * of which does not really depend on the inputs being disjoint.
8153 * If the result contains more than one basic map, then we clear
8154 * the disjoint flag since the result may contain basic maps from
8155 * both inputs and these are not guaranteed to be disjoint.
8157 * As a special case, if "map1" and "map2" are obviously equal,
8158 * then we simply return "map1".
8160 __isl_give isl_map
*isl_map_union(__isl_take isl_map
*map1
,
8161 __isl_take isl_map
*map2
)
8165 if (isl_map_align_params_bin(&map1
, &map2
) < 0)
8168 equal
= isl_map_plain_is_equal(map1
, map2
);
8176 map1
= map_union_disjoint(map1
, map2
);
8180 ISL_F_CLR(map1
, ISL_MAP_DISJOINT
);
8188 __isl_give isl_set
*isl_set_union_disjoint(
8189 __isl_take isl_set
*set1
, __isl_take isl_set
*set2
)
8191 return set_from_map(isl_map_union_disjoint(set_to_map(set1
),
8195 __isl_give isl_set
*isl_set_union(__isl_take isl_set
*set1
,
8196 __isl_take isl_set
*set2
)
8198 return set_from_map(isl_map_union(set_to_map(set1
), set_to_map(set2
)));
8201 /* Apply "fn" to pairs of elements from "map" and "set" and collect
8202 * the results in a map living in "space".
8204 * "map" and "set" are assumed to be compatible and non-NULL.
8206 static __isl_give isl_map
*map_intersect_set(__isl_take isl_map
*map
,
8207 __isl_take isl_space
*space
, __isl_take isl_set
*set
,
8208 __isl_give isl_basic_map
*fn(__isl_take isl_basic_map
*bmap
,
8209 __isl_take isl_basic_set
*bset
))
8212 struct isl_map
*result
;
8215 if (isl_set_plain_is_universe(set
)) {
8217 return isl_map_reset_equal_dim_space(map
, space
);
8220 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
) &&
8221 ISL_F_ISSET(set
, ISL_MAP_DISJOINT
))
8222 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
8224 result
= isl_map_alloc_space(space
, map
->n
* set
->n
, flags
);
8225 for (i
= 0; result
&& i
< map
->n
; ++i
)
8226 for (j
= 0; j
< set
->n
; ++j
) {
8227 result
= isl_map_add_basic_map(result
,
8228 fn(isl_basic_map_copy(map
->p
[i
]),
8229 isl_basic_set_copy(set
->p
[j
])));
8239 __isl_give isl_map
*isl_map_intersect_range(__isl_take isl_map
*map
,
8240 __isl_take isl_set
*set
)
8245 isl_map_align_params_set(&map
, &set
);
8246 ok
= isl_map_compatible_range(map
, set
);
8250 isl_die(set
->ctx
, isl_error_invalid
,
8251 "incompatible spaces", goto error
);
8253 space
= isl_map_get_space(map
);
8254 return map_intersect_set(map
, space
, set
,
8255 &isl_basic_map_intersect_range
);
8262 /* Intersect the domain of "map" with "set".
8264 * If the domain dimensions of "map" do not have any identifiers,
8265 * then copy them over from "set".
8267 __isl_give isl_map
*isl_map_intersect_domain(__isl_take isl_map
*map
,
8268 __isl_take isl_set
*set
)
8273 isl_map_align_params_set(&map
, &set
);
8274 ok
= isl_map_compatible_domain(map
, set
);
8278 isl_die(set
->ctx
, isl_error_invalid
,
8279 "incompatible spaces", goto error
);
8281 space
= isl_map_get_space(map
);
8282 space
= isl_space_copy_ids_if_unset(space
, isl_dim_in
,
8283 isl_set_peek_space(set
), isl_dim_set
);
8284 return map_intersect_set(map
, space
, set
,
8285 &isl_basic_map_intersect_domain
);
8292 /* Data structure that specifies how isl_map_intersect_factor
8295 * "preserve_type" is the tuple where the factor differs from
8296 * the input map and of which the identifiers needs
8297 * to be preserved explicitly.
8298 * "other_factor" is used to extract the space of the other factor
8299 * from the space of the product ("map").
8300 * "product" is used to combine the given factor and a universe map
8301 * in the space returned by "other_factor" to produce a map
8302 * that lives in the same space as the input map.
8304 struct isl_intersect_factor_control
{
8305 enum isl_dim_type preserve_type
;
8306 __isl_give isl_space
*(*other_factor
)(__isl_take isl_space
*space
);
8307 __isl_give isl_map
*(*product
)(__isl_take isl_map
*factor
,
8308 __isl_take isl_map
*other
);
8311 /* Given a map "map" in some product space and a map "factor"
8312 * living in some factor space, return the intersection.
8314 * After aligning the parameters,
8315 * the map "factor" is first extended to a map living in the same space
8316 * as "map" and then a regular intersection is computed.
8318 * Note that the extension is computed as a product, which is anonymous
8319 * by default. If "map" has an identifier on the corresponding tuple,
8320 * then this identifier needs to be set on the product
8321 * before the intersection is computed.
8323 static __isl_give isl_map
*isl_map_intersect_factor(
8324 __isl_take isl_map
*map
, __isl_take isl_map
*factor
,
8325 struct isl_intersect_factor_control
*control
)
8327 isl_bool equal
, has_id
;
8330 isl_map
*other
, *product
;
8332 equal
= isl_map_has_equal_params(map
, factor
);
8336 map
= isl_map_align_params(map
, isl_map_get_space(factor
));
8337 factor
= isl_map_align_params(factor
, isl_map_get_space(map
));
8340 space
= isl_map_get_space(map
);
8341 has_id
= isl_space_has_tuple_id(space
, control
->preserve_type
);
8343 space
= isl_space_free(space
);
8345 id
= isl_space_get_tuple_id(space
, control
->preserve_type
);
8347 other
= isl_map_universe(control
->other_factor(space
));
8348 product
= control
->product(factor
, other
);
8350 if (has_id
>= 0 && has_id
)
8351 product
= isl_map_set_tuple_id(product
,
8352 control
->preserve_type
, id
);
8354 return map_intersect(map
, product
);
8357 isl_map_free(factor
);
8361 /* Return the domain product of "map2" and "map1".
8363 static __isl_give isl_map
*isl_map_reverse_domain_product(
8364 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
8366 return isl_map_domain_product(map2
, map1
);
8369 /* Return the range product of "map2" and "map1".
8371 static __isl_give isl_map
*isl_map_reverse_range_product(
8372 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
8374 return isl_map_range_product(map2
, map1
);
8377 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
8378 * in the space B -> C, return the intersection.
8380 __isl_give isl_map
*isl_map_intersect_domain_factor_range(
8381 __isl_take isl_map
*map
, __isl_take isl_map
*factor
)
8383 struct isl_intersect_factor_control control
= {
8384 .preserve_type
= isl_dim_in
,
8385 .other_factor
= isl_space_domain_factor_domain
,
8386 .product
= isl_map_reverse_domain_product
,
8389 return isl_map_intersect_factor(map
, factor
, &control
);
8392 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
8393 * in the space A -> B, return the intersection.
8395 __isl_give isl_map
*isl_map_intersect_range_factor_domain(
8396 __isl_take isl_map
*map
, __isl_take isl_map
*factor
)
8398 struct isl_intersect_factor_control control
= {
8399 .preserve_type
= isl_dim_out
,
8400 .other_factor
= isl_space_range_factor_range
,
8401 .product
= isl_map_range_product
,
8404 return isl_map_intersect_factor(map
, factor
, &control
);
8407 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
8408 * in the space A -> C, return the intersection.
8410 __isl_give isl_map
*isl_map_intersect_range_factor_range(
8411 __isl_take isl_map
*map
, __isl_take isl_map
*factor
)
8413 struct isl_intersect_factor_control control
= {
8414 .preserve_type
= isl_dim_out
,
8415 .other_factor
= isl_space_range_factor_domain
,
8416 .product
= isl_map_reverse_range_product
,
8419 return isl_map_intersect_factor(map
, factor
, &control
);
8422 /* Given a set "set" in a space [A -> B] and a set "domain"
8423 * in the space A, return the intersection.
8425 * The set "domain" is first extended to a set living in the space
8426 * [A -> B] and then a regular intersection is computed.
8428 __isl_give isl_set
*isl_set_intersect_factor_domain(__isl_take isl_set
*set
,
8429 __isl_take isl_set
*domain
)
8431 struct isl_intersect_factor_control control
= {
8432 .preserve_type
= isl_dim_set
,
8433 .other_factor
= isl_space_factor_range
,
8434 .product
= isl_map_range_product
,
8437 return set_from_map(isl_map_intersect_factor(set_to_map(set
),
8438 set_to_map(domain
), &control
));
8441 /* Given a set "set" in a space [A -> B] and a set "range"
8442 * in the space B, return the intersection.
8444 * The set "range" is first extended to a set living in the space
8445 * [A -> B] and then a regular intersection is computed.
8447 __isl_give isl_set
*isl_set_intersect_factor_range(__isl_take isl_set
*set
,
8448 __isl_take isl_set
*range
)
8450 struct isl_intersect_factor_control control
= {
8451 .preserve_type
= isl_dim_set
,
8452 .other_factor
= isl_space_factor_domain
,
8453 .product
= isl_map_reverse_range_product
,
8456 return set_from_map(isl_map_intersect_factor(set_to_map(set
),
8457 set_to_map(range
), &control
));
8460 __isl_give isl_map
*isl_map_apply_domain(__isl_take isl_map
*map1
,
8461 __isl_take isl_map
*map2
)
8463 if (isl_map_align_params_bin(&map1
, &map2
) < 0)
8465 map1
= isl_map_reverse(map1
);
8466 map1
= isl_map_apply_range(map1
, map2
);
8467 return isl_map_reverse(map1
);
8474 __isl_give isl_map
*isl_map_apply_range(__isl_take isl_map
*map1
,
8475 __isl_take isl_map
*map2
)
8478 struct isl_map
*result
;
8481 if (isl_map_align_params_bin(&map1
, &map2
) < 0)
8484 space
= isl_space_join(isl_space_copy(map1
->dim
),
8485 isl_space_copy(map2
->dim
));
8487 result
= isl_map_alloc_space(space
, map1
->n
* map2
->n
, 0);
8490 for (i
= 0; i
< map1
->n
; ++i
)
8491 for (j
= 0; j
< map2
->n
; ++j
) {
8492 result
= isl_map_add_basic_map(result
,
8493 isl_basic_map_apply_range(
8494 isl_basic_map_copy(map1
->p
[i
]),
8495 isl_basic_map_copy(map2
->p
[j
])));
8501 if (result
&& result
->n
<= 1)
8502 ISL_F_SET(result
, ISL_MAP_DISJOINT
);
8510 /* Is "bmap" a transformation, i.e.,
8511 * does it relate elements from the same space.
8513 isl_bool
isl_basic_map_is_transformation(__isl_keep isl_basic_map
*bmap
)
8517 space
= isl_basic_map_peek_space(bmap
);
8518 return isl_space_tuple_is_equal(space
, isl_dim_in
, space
, isl_dim_out
);
8521 /* Check that "bmap" is a transformation, i.e.,
8522 * that it relates elements from the same space.
8524 static isl_stat
isl_basic_map_check_transformation(
8525 __isl_keep isl_basic_map
*bmap
)
8529 equal
= isl_basic_map_is_transformation(bmap
);
8531 return isl_stat_error
;
8533 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
8534 "domain and range don't match", return isl_stat_error
);
8539 * returns range - domain
8541 __isl_give isl_basic_set
*isl_basic_map_deltas(__isl_take isl_basic_map
*bmap
)
8543 isl_space
*target_space
;
8544 struct isl_basic_set
*bset
;
8550 if (isl_basic_map_check_transformation(bmap
) < 0)
8551 return isl_basic_map_free(bmap
);
8552 dim
= isl_basic_map_dim(bmap
, isl_dim_in
);
8553 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
8554 if (dim
< 0 || nparam
< 0)
8556 target_space
= isl_space_domain(isl_basic_map_get_space(bmap
));
8557 bmap
= isl_basic_map_from_range(isl_basic_map_wrap(bmap
));
8558 bmap
= isl_basic_map_add_dims(bmap
, isl_dim_in
, dim
);
8559 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
8561 bmap
= isl_basic_map_free(bmap
);
8562 bmap
= isl_basic_map_extend_constraints(bmap
, dim
, 0);
8563 for (i
= 0; i
< dim
; ++i
) {
8564 int j
= isl_basic_map_alloc_equality(bmap
);
8566 bmap
= isl_basic_map_free(bmap
);
8569 isl_seq_clr(bmap
->eq
[j
], 1 + total
);
8570 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
8571 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], 1);
8572 isl_int_set_si(bmap
->eq
[j
][1+nparam
+2*dim
+i
], -1);
8574 bset
= isl_basic_map_domain(bmap
);
8575 bset
= isl_basic_set_reset_space(bset
, target_space
);
8578 isl_basic_map_free(bmap
);
8582 /* Is the tuple of type "type1" of "map" the same as
8583 * the tuple of type "type2" of "space"?
8585 isl_bool
isl_map_space_tuple_is_equal(__isl_keep isl_map
*map
,
8586 enum isl_dim_type type1
, __isl_keep isl_space
*space
,
8587 enum isl_dim_type type2
)
8589 isl_space
*map_space
;
8591 map_space
= isl_map_peek_space(map
);
8592 return isl_space_tuple_is_equal(map_space
, type1
, space
, type2
);
8595 /* Is the tuple of type "type1" of "map1" the same as
8596 * the tuple of type "type2" of "map2"?
8598 isl_bool
isl_map_tuple_is_equal(__isl_keep isl_map
*map1
,
8599 enum isl_dim_type type1
, __isl_keep isl_map
*map2
,
8600 enum isl_dim_type type2
)
8602 isl_space
*space1
, *space2
;
8604 space1
= isl_map_peek_space(map1
);
8605 space2
= isl_map_peek_space(map2
);
8606 return isl_space_tuple_is_equal(space1
, type1
, space2
, type2
);
8609 /* Check that "map" is a transformation, i.e.,
8610 * that it relates elements from the same space.
8612 isl_stat
isl_map_check_transformation(__isl_keep isl_map
*map
)
8616 equal
= isl_map_tuple_is_equal(map
, isl_dim_in
, map
, isl_dim_out
);
8618 return isl_stat_error
;
8620 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
8621 "domain and range don't match", return isl_stat_error
);
8626 * returns range - domain
8628 __isl_give isl_set
*isl_map_deltas(__isl_take isl_map
*map
)
8632 struct isl_set
*result
;
8634 if (isl_map_check_transformation(map
) < 0)
8636 space
= isl_map_get_space(map
);
8637 space
= isl_space_domain(space
);
8638 result
= isl_set_alloc_space(space
, map
->n
, 0);
8641 for (i
= 0; i
< map
->n
; ++i
)
8642 result
= isl_set_add_basic_set(result
,
8643 isl_basic_map_deltas(isl_basic_map_copy(map
->p
[i
])));
8652 * returns [domain -> range] -> range - domain
8654 __isl_give isl_basic_map
*isl_basic_map_deltas_map(
8655 __isl_take isl_basic_map
*bmap
)
8659 isl_basic_map
*domain
;
8663 if (isl_basic_map_check_transformation(bmap
) < 0)
8664 return isl_basic_map_free(bmap
);
8666 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
8667 n
= isl_basic_map_dim(bmap
, isl_dim_in
);
8668 if (nparam
< 0 || n
< 0)
8669 return isl_basic_map_free(bmap
);
8671 space
= isl_basic_map_get_space(bmap
);
8672 space
= isl_space_from_range(isl_space_domain(space
));
8673 domain
= isl_basic_map_universe(space
);
8675 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
8676 bmap
= isl_basic_map_apply_range(bmap
, domain
);
8677 bmap
= isl_basic_map_extend_constraints(bmap
, n
, 0);
8679 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
8681 return isl_basic_map_free(bmap
);
8683 for (i
= 0; i
< n
; ++i
) {
8684 k
= isl_basic_map_alloc_equality(bmap
);
8687 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
8688 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], 1);
8689 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ i
], -1);
8690 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ n
+ i
], 1);
8693 bmap
= isl_basic_map_gauss(bmap
, NULL
);
8694 return isl_basic_map_finalize(bmap
);
8696 isl_basic_map_free(bmap
);
8701 * returns [domain -> range] -> range - domain
8703 __isl_give isl_map
*isl_map_deltas_map(__isl_take isl_map
*map
)
8705 if (isl_map_check_transformation(map
) < 0)
8706 return isl_map_free(map
);
8708 return isl_map_transform(map
, &isl_space_range_map
,
8709 &isl_basic_map_deltas_map
);
8712 __isl_give isl_basic_map
*isl_basic_map_identity(__isl_take isl_space
*space
)
8714 isl_size n_in
, n_out
;
8716 n_in
= isl_space_dim(space
, isl_dim_in
);
8717 n_out
= isl_space_dim(space
, isl_dim_out
);
8718 if (n_in
< 0 || n_out
< 0)
8721 isl_die(space
->ctx
, isl_error_invalid
,
8722 "number of input and output dimensions needs to be "
8723 "the same", goto error
);
8724 return isl_basic_map_equal(space
, n_in
);
8726 isl_space_free(space
);
8730 __isl_give isl_map
*isl_map_identity(__isl_take isl_space
*space
)
8732 return isl_map_from_basic_map(isl_basic_map_identity(space
));
8735 __isl_give isl_map
*isl_set_identity(__isl_take isl_set
*set
)
8737 isl_space
*space
= isl_set_get_space(set
);
8739 id
= isl_map_identity(isl_space_map_from_set(space
));
8740 return isl_map_intersect_range(id
, set
);
8743 /* Construct a basic set with all set dimensions having only non-negative
8746 __isl_give isl_basic_set
*isl_basic_set_positive_orthant(
8747 __isl_take isl_space
*space
)
8753 struct isl_basic_set
*bset
;
8755 nparam
= isl_space_dim(space
, isl_dim_param
);
8756 dim
= isl_space_dim(space
, isl_dim_set
);
8757 total
= isl_space_dim(space
, isl_dim_all
);
8758 if (nparam
< 0 || dim
< 0 || total
< 0)
8759 space
= isl_space_free(space
);
8760 bset
= isl_basic_set_alloc_space(space
, 0, 0, dim
);
8763 for (i
= 0; i
< dim
; ++i
) {
8764 int k
= isl_basic_set_alloc_inequality(bset
);
8767 isl_seq_clr(bset
->ineq
[k
], 1 + total
);
8768 isl_int_set_si(bset
->ineq
[k
][1 + nparam
+ i
], 1);
8772 isl_basic_set_free(bset
);
8776 /* Construct the half-space x_pos >= 0.
8778 static __isl_give isl_basic_set
*nonneg_halfspace(__isl_take isl_space
*space
,
8783 isl_basic_set
*nonneg
;
8785 total
= isl_space_dim(space
, isl_dim_all
);
8787 space
= isl_space_free(space
);
8788 nonneg
= isl_basic_set_alloc_space(space
, 0, 0, 1);
8789 k
= isl_basic_set_alloc_inequality(nonneg
);
8792 isl_seq_clr(nonneg
->ineq
[k
], 1 + total
);
8793 isl_int_set_si(nonneg
->ineq
[k
][pos
], 1);
8795 return isl_basic_set_finalize(nonneg
);
8797 isl_basic_set_free(nonneg
);
8801 /* Construct the half-space x_pos <= -1.
8803 static __isl_give isl_basic_set
*neg_halfspace(__isl_take isl_space
*space
,
8810 total
= isl_space_dim(space
, isl_dim_all
);
8812 space
= isl_space_free(space
);
8813 neg
= isl_basic_set_alloc_space(space
, 0, 0, 1);
8814 k
= isl_basic_set_alloc_inequality(neg
);
8817 isl_seq_clr(neg
->ineq
[k
], 1 + total
);
8818 isl_int_set_si(neg
->ineq
[k
][0], -1);
8819 isl_int_set_si(neg
->ineq
[k
][pos
], -1);
8821 return isl_basic_set_finalize(neg
);
8823 isl_basic_set_free(neg
);
8827 __isl_give isl_set
*isl_set_split_dims(__isl_take isl_set
*set
,
8828 enum isl_dim_type type
, unsigned first
, unsigned n
)
8832 isl_basic_set
*nonneg
;
8838 if (isl_set_check_range(set
, type
, first
, n
) < 0)
8839 return isl_set_free(set
);
8841 offset
= pos(set
->dim
, type
);
8842 for (i
= 0; i
< n
; ++i
) {
8843 nonneg
= nonneg_halfspace(isl_set_get_space(set
),
8844 offset
+ first
+ i
);
8845 neg
= neg_halfspace(isl_set_get_space(set
), offset
+ first
+ i
);
8847 set
= isl_set_intersect(set
, isl_basic_set_union(nonneg
, neg
));
8853 static isl_stat
foreach_orthant(__isl_take isl_set
*set
, int *signs
, int first
,
8855 isl_stat (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
8861 return isl_stat_error
;
8862 if (isl_set_plain_is_empty(set
)) {
8867 return fn(set
, signs
, user
);
8870 half
= isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set
),
8872 half
= isl_set_intersect(half
, isl_set_copy(set
));
8873 if (foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
) < 0)
8877 half
= isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set
),
8879 half
= isl_set_intersect(half
, set
);
8880 return foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
);
8883 return isl_stat_error
;
8886 /* Call "fn" on the intersections of "set" with each of the orthants
8887 * (except for obviously empty intersections). The orthant is identified
8888 * by the signs array, with each entry having value 1 or -1 according
8889 * to the sign of the corresponding variable.
8891 isl_stat
isl_set_foreach_orthant(__isl_keep isl_set
*set
,
8892 isl_stat (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
8901 return isl_stat_error
;
8902 if (isl_set_plain_is_empty(set
))
8905 nparam
= isl_set_dim(set
, isl_dim_param
);
8906 nvar
= isl_set_dim(set
, isl_dim_set
);
8907 if (nparam
< 0 || nvar
< 0)
8908 return isl_stat_error
;
8910 signs
= isl_alloc_array(set
->ctx
, int, nparam
+ nvar
);
8912 r
= foreach_orthant(isl_set_copy(set
), signs
, 0, nparam
+ nvar
,
8920 isl_bool
isl_set_is_equal(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
8922 return isl_map_is_equal(set_to_map(set1
), set_to_map(set2
));
8925 isl_bool
isl_basic_map_is_subset(__isl_keep isl_basic_map
*bmap1
,
8926 __isl_keep isl_basic_map
*bmap2
)
8929 struct isl_map
*map1
;
8930 struct isl_map
*map2
;
8932 if (!bmap1
|| !bmap2
)
8933 return isl_bool_error
;
8935 map1
= isl_map_from_basic_map(isl_basic_map_copy(bmap1
));
8936 map2
= isl_map_from_basic_map(isl_basic_map_copy(bmap2
));
8938 is_subset
= isl_map_is_subset(map1
, map2
);
8946 isl_bool
isl_basic_set_is_subset(__isl_keep isl_basic_set
*bset1
,
8947 __isl_keep isl_basic_set
*bset2
)
8949 return isl_basic_map_is_subset(bset1
, bset2
);
8952 isl_bool
isl_basic_map_is_equal(__isl_keep isl_basic_map
*bmap1
,
8953 __isl_keep isl_basic_map
*bmap2
)
8957 if (!bmap1
|| !bmap2
)
8958 return isl_bool_error
;
8959 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
8960 if (is_subset
!= isl_bool_true
)
8962 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
8966 isl_bool
isl_basic_set_is_equal(__isl_keep isl_basic_set
*bset1
,
8967 __isl_keep isl_basic_set
*bset2
)
8969 return isl_basic_map_is_equal(
8970 bset_to_bmap(bset1
), bset_to_bmap(bset2
));
8973 isl_bool
isl_map_is_empty(__isl_keep isl_map
*map
)
8979 return isl_bool_error
;
8980 for (i
= 0; i
< map
->n
; ++i
) {
8981 is_empty
= isl_basic_map_is_empty(map
->p
[i
]);
8983 return isl_bool_error
;
8985 return isl_bool_false
;
8987 return isl_bool_true
;
8990 isl_bool
isl_map_plain_is_empty(__isl_keep isl_map
*map
)
8992 return map
? map
->n
== 0 : isl_bool_error
;
8995 isl_bool
isl_set_plain_is_empty(__isl_keep isl_set
*set
)
8997 return set
? set
->n
== 0 : isl_bool_error
;
9000 isl_bool
isl_set_is_empty(__isl_keep isl_set
*set
)
9002 return isl_map_is_empty(set_to_map(set
));
9006 #define TYPE isl_basic_map
9009 #include "isl_type_has_equal_space_bin_templ.c"
9010 #include "isl_type_check_equal_space_templ.c"
9012 /* Check that "bset1" and "bset2" live in the same space,
9013 * reporting an error if they do not.
9015 isl_stat
isl_basic_set_check_equal_space(__isl_keep isl_basic_set
*bset1
,
9016 __isl_keep isl_basic_set
*bset2
)
9018 return isl_basic_map_check_equal_space(bset_to_bmap(bset1
),
9019 bset_to_bmap(bset1
));
9023 #define TYPE isl_map
9025 #include "isl_type_has_equal_space_bin_templ.c"
9026 #include "isl_type_check_equal_space_templ.c"
9028 isl_bool
isl_set_has_equal_space(__isl_keep isl_set
*set1
,
9029 __isl_keep isl_set
*set2
)
9031 return isl_map_has_equal_space(set_to_map(set1
), set_to_map(set2
));
9035 #define TYPE1 isl_map
9037 #define TYPE2 isl_basic_map
9039 #define TYPE_PAIR isl_map_basic_map
9042 #include "isl_type_has_equal_space_templ.c"
9043 #include "isl_type_check_equal_space_templ.c"
9045 /* Check that "set" and "bset" live in the same space,
9046 * reporting an error if they do not.
9048 isl_stat
isl_set_basic_set_check_equal_space(__isl_keep isl_set
*set
,
9049 __isl_keep isl_basic_set
*bset
)
9051 return isl_map_basic_map_check_equal_space(set_to_map(set
),
9052 bset_to_bmap(bset
));
9055 static isl_bool
map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
9060 return isl_bool_error
;
9061 is_subset
= isl_map_is_subset(map1
, map2
);
9062 if (is_subset
!= isl_bool_true
)
9064 is_subset
= isl_map_is_subset(map2
, map1
);
9068 /* Is "map1" equal to "map2"?
9070 * First check if they are obviously equal.
9071 * If not, then perform a more detailed analysis.
9073 isl_bool
isl_map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
9077 equal
= isl_map_plain_is_equal(map1
, map2
);
9078 if (equal
< 0 || equal
)
9080 return isl_map_align_params_map_map_and_test(map1
, map2
, &map_is_equal
);
9083 isl_bool
isl_basic_map_is_strict_subset(__isl_keep isl_basic_map
*bmap1
,
9084 __isl_keep isl_basic_map
*bmap2
)
9088 if (!bmap1
|| !bmap2
)
9089 return isl_bool_error
;
9090 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
9091 if (is_subset
!= isl_bool_true
)
9093 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
9094 return isl_bool_not(is_subset
);
9097 isl_bool
isl_map_is_strict_subset(__isl_keep isl_map
*map1
,
9098 __isl_keep isl_map
*map2
)
9103 return isl_bool_error
;
9104 is_subset
= isl_map_is_subset(map1
, map2
);
9105 if (is_subset
!= isl_bool_true
)
9107 is_subset
= isl_map_is_subset(map2
, map1
);
9108 return isl_bool_not(is_subset
);
9111 isl_bool
isl_set_is_strict_subset(__isl_keep isl_set
*set1
,
9112 __isl_keep isl_set
*set2
)
9114 return isl_map_is_strict_subset(set_to_map(set1
), set_to_map(set2
));
9117 /* Is "bmap" obviously equal to the universe with the same space?
9119 * That is, does it not have any constraints?
9121 isl_bool
isl_basic_map_plain_is_universe(__isl_keep isl_basic_map
*bmap
)
9124 return isl_bool_error
;
9125 return bmap
->n_eq
== 0 && bmap
->n_ineq
== 0;
9128 /* Is "bset" obviously equal to the universe with the same space?
9130 isl_bool
isl_basic_set_plain_is_universe(__isl_keep isl_basic_set
*bset
)
9132 return isl_basic_map_plain_is_universe(bset
);
9135 /* If "c" does not involve any existentially quantified variables,
9136 * then set *univ to false and abort
9138 static isl_stat
involves_divs(__isl_take isl_constraint
*c
, void *user
)
9140 isl_bool
*univ
= user
;
9143 n
= isl_constraint_dim(c
, isl_dim_div
);
9145 c
= isl_constraint_free(c
);
9146 *univ
= isl_constraint_involves_dims(c
, isl_dim_div
, 0, n
);
9147 isl_constraint_free(c
);
9148 if (*univ
< 0 || !*univ
)
9149 return isl_stat_error
;
9153 /* Is "bmap" equal to the universe with the same space?
9155 * First check if it is obviously equal to the universe.
9156 * If not and if there are any constraints not involving
9157 * existentially quantified variables, then it is certainly
9158 * not equal to the universe.
9159 * Otherwise, check if the universe is a subset of "bmap".
9161 isl_bool
isl_basic_map_is_universe(__isl_keep isl_basic_map
*bmap
)
9165 isl_basic_map
*test
;
9167 univ
= isl_basic_map_plain_is_universe(bmap
);
9168 if (univ
< 0 || univ
)
9170 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
9172 return isl_bool_error
;
9174 return isl_bool_false
;
9175 univ
= isl_bool_true
;
9176 if (isl_basic_map_foreach_constraint(bmap
, &involves_divs
, &univ
) < 0 &&
9178 return isl_bool_error
;
9179 if (univ
< 0 || !univ
)
9181 test
= isl_basic_map_universe(isl_basic_map_get_space(bmap
));
9182 univ
= isl_basic_map_is_subset(test
, bmap
);
9183 isl_basic_map_free(test
);
9187 /* Is "bset" equal to the universe with the same space?
9189 isl_bool
isl_basic_set_is_universe(__isl_keep isl_basic_set
*bset
)
9191 return isl_basic_map_is_universe(bset
);
9194 isl_bool
isl_map_plain_is_universe(__isl_keep isl_map
*map
)
9199 return isl_bool_error
;
9201 for (i
= 0; i
< map
->n
; ++i
) {
9202 isl_bool r
= isl_basic_map_plain_is_universe(map
->p
[i
]);
9207 return isl_bool_false
;
9210 isl_bool
isl_set_plain_is_universe(__isl_keep isl_set
*set
)
9212 return isl_map_plain_is_universe(set_to_map(set
));
9215 isl_bool
isl_basic_map_is_empty(__isl_keep isl_basic_map
*bmap
)
9217 struct isl_basic_set
*bset
= NULL
;
9218 struct isl_vec
*sample
= NULL
;
9219 isl_bool empty
, non_empty
;
9222 return isl_bool_error
;
9224 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
9225 return isl_bool_true
;
9227 if (isl_basic_map_plain_is_universe(bmap
))
9228 return isl_bool_false
;
9230 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
9231 struct isl_basic_map
*copy
= isl_basic_map_copy(bmap
);
9232 copy
= isl_basic_map_remove_redundancies(copy
);
9233 empty
= isl_basic_map_plain_is_empty(copy
);
9234 isl_basic_map_free(copy
);
9238 non_empty
= isl_basic_map_plain_is_non_empty(bmap
);
9240 return isl_bool_error
;
9242 return isl_bool_false
;
9243 isl_vec_free(bmap
->sample
);
9244 bmap
->sample
= NULL
;
9245 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
9247 return isl_bool_error
;
9248 sample
= isl_basic_set_sample_vec(bset
);
9250 return isl_bool_error
;
9251 empty
= sample
->size
== 0;
9252 isl_vec_free(bmap
->sample
);
9253 bmap
->sample
= sample
;
9255 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
9260 isl_bool
isl_basic_map_plain_is_empty(__isl_keep isl_basic_map
*bmap
)
9263 return isl_bool_error
;
9264 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
);
9267 isl_bool
isl_basic_set_plain_is_empty(__isl_keep isl_basic_set
*bset
)
9270 return isl_bool_error
;
9271 return ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
);
9274 /* Is "bmap" known to be non-empty?
9276 * That is, is the cached sample still valid?
9278 isl_bool
isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map
*bmap
)
9283 return isl_bool_error
;
9285 return isl_bool_false
;
9286 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
9288 return isl_bool_error
;
9289 if (bmap
->sample
->size
!= 1 + total
)
9290 return isl_bool_false
;
9291 return isl_basic_map_contains(bmap
, bmap
->sample
);
9294 isl_bool
isl_basic_set_is_empty(__isl_keep isl_basic_set
*bset
)
9296 return isl_basic_map_is_empty(bset_to_bmap(bset
));
9299 __isl_give isl_map
*isl_basic_map_union(__isl_take isl_basic_map
*bmap1
,
9300 __isl_take isl_basic_map
*bmap2
)
9302 struct isl_map
*map
;
9304 if (isl_basic_map_check_equal_space(bmap1
, bmap2
) < 0)
9307 map
= isl_map_alloc_space(isl_space_copy(bmap1
->dim
), 2, 0);
9310 map
= isl_map_add_basic_map(map
, bmap1
);
9311 map
= isl_map_add_basic_map(map
, bmap2
);
9314 isl_basic_map_free(bmap1
);
9315 isl_basic_map_free(bmap2
);
9319 __isl_give isl_set
*isl_basic_set_union(__isl_take isl_basic_set
*bset1
,
9320 __isl_take isl_basic_set
*bset2
)
9322 return set_from_map(isl_basic_map_union(bset_to_bmap(bset1
),
9323 bset_to_bmap(bset2
)));
9326 /* Order divs such that any div only depends on previous divs */
9327 __isl_give isl_basic_map
*isl_basic_map_order_divs(
9328 __isl_take isl_basic_map
*bmap
)
9333 off
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
9335 return isl_basic_map_free(bmap
);
9337 for (i
= 0; i
< bmap
->n_div
; ++i
) {
9339 if (isl_int_is_zero(bmap
->div
[i
][0]))
9341 pos
= isl_seq_first_non_zero(bmap
->div
[i
]+1+1+off
+i
,
9346 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_internal
,
9347 "integer division depends on itself",
9348 return isl_basic_map_free(bmap
));
9349 bmap
= isl_basic_map_swap_div(bmap
, i
, i
+ pos
);
9357 __isl_give isl_map
*isl_map_order_divs(__isl_take isl_map
*map
)
9364 for (i
= 0; i
< map
->n
; ++i
) {
9365 map
->p
[i
] = isl_basic_map_order_divs(map
->p
[i
]);
9376 /* Sort the local variables of "bset".
9378 __isl_give isl_basic_set
*isl_basic_set_sort_divs(
9379 __isl_take isl_basic_set
*bset
)
9381 return bset_from_bmap(isl_basic_map_sort_divs(bset_to_bmap(bset
)));
9384 /* Apply the expansion computed by isl_merge_divs.
9385 * The expansion itself is given by "exp" while the resulting
9386 * list of divs is given by "div".
9388 * Move the integer divisions of "bmap" into the right position
9389 * according to "exp" and then introduce the additional integer
9390 * divisions, adding div constraints.
9391 * The moving should be done first to avoid moving coefficients
9392 * in the definitions of the extra integer divisions.
9394 __isl_give isl_basic_map
*isl_basic_map_expand_divs(
9395 __isl_take isl_basic_map
*bmap
, __isl_take isl_mat
*div
, int *exp
)
9400 bmap
= isl_basic_map_cow(bmap
);
9404 if (div
->n_row
< bmap
->n_div
)
9405 isl_die(isl_mat_get_ctx(div
), isl_error_invalid
,
9406 "not an expansion", goto error
);
9408 n_div
= bmap
->n_div
;
9409 bmap
= isl_basic_map_extend(bmap
, div
->n_row
- n_div
, 0,
9410 2 * (div
->n_row
- n_div
));
9412 for (i
= n_div
; i
< div
->n_row
; ++i
)
9413 if (isl_basic_map_alloc_div(bmap
) < 0)
9416 for (j
= n_div
- 1; j
>= 0; --j
) {
9419 bmap
= isl_basic_map_swap_div(bmap
, j
, exp
[j
]);
9424 for (i
= 0; i
< div
->n_row
; ++i
) {
9425 if (j
< n_div
&& exp
[j
] == i
) {
9428 isl_seq_cpy(bmap
->div
[i
], div
->row
[i
], div
->n_col
);
9429 if (isl_basic_map_div_is_marked_unknown(bmap
, i
))
9431 bmap
= isl_basic_map_add_div_constraints(bmap
, i
);
9440 isl_basic_map_free(bmap
);
9445 /* Apply the expansion computed by isl_merge_divs.
9446 * The expansion itself is given by "exp" while the resulting
9447 * list of divs is given by "div".
9449 __isl_give isl_basic_set
*isl_basic_set_expand_divs(
9450 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*div
, int *exp
)
9452 return isl_basic_map_expand_divs(bset
, div
, exp
);
9455 /* Look for a div in dst that corresponds to the div "div" in src.
9456 * The divs before "div" in src and dst are assumed to be the same.
9458 * Return the position of the corresponding div in dst
9459 * if there is one. Otherwise, return a position beyond the integer divisions.
9460 * Return -1 on error.
9462 static int find_div(__isl_keep isl_basic_map
*dst
,
9463 __isl_keep isl_basic_map
*src
, unsigned div
)
9469 v_div
= isl_basic_map_var_offset(src
, isl_dim_div
);
9470 n_div
= isl_basic_map_dim(dst
, isl_dim_div
);
9471 if (n_div
< 0 || v_div
< 0)
9473 isl_assert(dst
->ctx
, div
<= n_div
, return -1);
9474 for (i
= div
; i
< n_div
; ++i
)
9475 if (isl_seq_eq(dst
->div
[i
], src
->div
[div
], 1+1+v_div
+div
) &&
9476 isl_seq_first_non_zero(dst
->div
[i
] + 1 + 1 + v_div
+ div
,
9482 /* Align the divs of "dst" to those of "src", adding divs from "src"
9483 * if needed. That is, make sure that the first src->n_div divs
9484 * of the result are equal to those of src.
9486 * The result is not finalized as by design it will have redundant
9487 * divs if any divs from "src" were copied.
9489 __isl_give isl_basic_map
*isl_basic_map_align_divs(
9490 __isl_take isl_basic_map
*dst
, __isl_keep isl_basic_map
*src
)
9499 return isl_basic_map_free(dst
);
9501 if (src
->n_div
== 0)
9504 known
= isl_basic_map_divs_known(src
);
9506 return isl_basic_map_free(dst
);
9508 isl_die(isl_basic_map_get_ctx(src
), isl_error_invalid
,
9509 "some src divs are unknown",
9510 return isl_basic_map_free(dst
));
9512 v_div
= isl_basic_map_var_offset(src
, isl_dim_div
);
9514 return isl_basic_map_free(dst
);
9516 src
= isl_basic_map_order_divs(isl_basic_map_copy(src
));
9518 return isl_basic_map_free(dst
);
9521 dst_n_div
= isl_basic_map_dim(dst
, isl_dim_div
);
9523 dst
= isl_basic_map_free(dst
);
9524 for (i
= 0; i
< src
->n_div
; ++i
) {
9525 int j
= find_div(dst
, src
, i
);
9527 dst
= isl_basic_map_free(dst
);
9528 if (j
== dst_n_div
) {
9530 int extra
= src
->n_div
- i
;
9531 dst
= isl_basic_map_cow(dst
);
9534 dst
= isl_basic_map_extend(dst
,
9535 extra
, 0, 2 * extra
);
9538 j
= isl_basic_map_alloc_div(dst
);
9541 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+v_div
+i
);
9542 isl_seq_clr(dst
->div
[j
]+1+1+v_div
+i
, dst
->n_div
- i
);
9544 dst
= isl_basic_map_add_div_constraints(dst
, j
);
9549 dst
= isl_basic_map_swap_div(dst
, i
, j
);
9553 isl_basic_map_free(src
);
9556 isl_basic_map_free(src
);
9557 isl_basic_map_free(dst
);
9561 __isl_give isl_map
*isl_map_align_divs_internal(__isl_take isl_map
*map
)
9569 map
= isl_map_compute_divs(map
);
9570 map
= isl_map_cow(map
);
9574 for (i
= 1; i
< map
->n
; ++i
)
9575 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], map
->p
[i
]);
9576 for (i
= 1; i
< map
->n
; ++i
) {
9577 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], map
->p
[0]);
9579 return isl_map_free(map
);
9582 map
= isl_map_unmark_normalized(map
);
9586 __isl_give isl_map
*isl_map_align_divs(__isl_take isl_map
*map
)
9588 return isl_map_align_divs_internal(map
);
9591 __isl_give isl_set
*isl_set_align_divs(__isl_take isl_set
*set
)
9593 return set_from_map(isl_map_align_divs_internal(set_to_map(set
)));
9596 /* Align the divs of the basic maps in "map" to those
9597 * of the basic maps in "list", as well as to the other basic maps in "map".
9598 * The elements in "list" are assumed to have known divs.
9600 __isl_give isl_map
*isl_map_align_divs_to_basic_map_list(
9601 __isl_take isl_map
*map
, __isl_keep isl_basic_map_list
*list
)
9606 n
= isl_basic_map_list_n_basic_map(list
);
9607 map
= isl_map_compute_divs(map
);
9608 map
= isl_map_cow(map
);
9610 return isl_map_free(map
);
9614 for (i
= 0; i
< n
; ++i
) {
9615 isl_basic_map
*bmap
;
9617 bmap
= isl_basic_map_list_get_basic_map(list
, i
);
9618 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], bmap
);
9619 isl_basic_map_free(bmap
);
9622 return isl_map_free(map
);
9624 return isl_map_align_divs_internal(map
);
9627 /* Align the divs of each element of "list" to those of "bmap".
9628 * Both "bmap" and the elements of "list" are assumed to have known divs.
9630 __isl_give isl_basic_map_list
*isl_basic_map_list_align_divs_to_basic_map(
9631 __isl_take isl_basic_map_list
*list
, __isl_keep isl_basic_map
*bmap
)
9636 n
= isl_basic_map_list_n_basic_map(list
);
9638 return isl_basic_map_list_free(list
);
9640 for (i
= 0; i
< n
; ++i
) {
9641 isl_basic_map
*bmap_i
;
9643 bmap_i
= isl_basic_map_list_get_basic_map(list
, i
);
9644 bmap_i
= isl_basic_map_align_divs(bmap_i
, bmap
);
9645 list
= isl_basic_map_list_set_basic_map(list
, i
, bmap_i
);
9651 __isl_give isl_set
*isl_set_apply( __isl_take isl_set
*set
,
9652 __isl_take isl_map
*map
)
9656 isl_map_align_params_set(&map
, &set
);
9657 ok
= isl_map_compatible_domain(map
, set
);
9661 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
9662 "incompatible spaces", goto error
);
9663 map
= isl_map_intersect_domain(map
, set
);
9664 set
= isl_map_range(map
);
9672 /* There is no need to cow as removing empty parts doesn't change
9673 * the meaning of the set.
9675 __isl_give isl_map
*isl_map_remove_empty_parts(__isl_take isl_map
*map
)
9682 for (i
= map
->n
- 1; i
>= 0; --i
)
9683 map
= remove_if_empty(map
, i
);
9688 __isl_give isl_set
*isl_set_remove_empty_parts(__isl_take isl_set
*set
)
9690 return set_from_map(isl_map_remove_empty_parts(set_to_map(set
)));
9693 /* Create a binary relation that maps the shared initial "pos" dimensions
9694 * of "bset1" and "bset2" to the remaining dimensions of "bset1" and "bset2".
9696 static __isl_give isl_basic_map
*join_initial(__isl_keep isl_basic_set
*bset1
,
9697 __isl_keep isl_basic_set
*bset2
, int pos
)
9699 isl_basic_map
*bmap1
;
9700 isl_basic_map
*bmap2
;
9702 bmap1
= isl_basic_map_from_range(isl_basic_set_copy(bset1
));
9703 bmap2
= isl_basic_map_from_range(isl_basic_set_copy(bset2
));
9704 bmap1
= isl_basic_map_move_dims(bmap1
, isl_dim_in
, 0,
9705 isl_dim_out
, 0, pos
);
9706 bmap2
= isl_basic_map_move_dims(bmap2
, isl_dim_in
, 0,
9707 isl_dim_out
, 0, pos
);
9708 return isl_basic_map_range_product(bmap1
, bmap2
);
9711 /* Given two basic sets bset1 and bset2, compute the maximal difference
9712 * between the values of dimension pos in bset1 and those in bset2
9713 * for any common value of the parameters and dimensions preceding pos.
9715 static enum isl_lp_result
basic_set_maximal_difference_at(
9716 __isl_keep isl_basic_set
*bset1
, __isl_keep isl_basic_set
*bset2
,
9717 int pos
, isl_int
*opt
)
9719 isl_basic_map
*bmap1
;
9720 struct isl_ctx
*ctx
;
9721 struct isl_vec
*obj
;
9725 enum isl_lp_result res
;
9727 nparam
= isl_basic_set_dim(bset1
, isl_dim_param
);
9728 dim1
= isl_basic_set_dim(bset1
, isl_dim_set
);
9729 if (nparam
< 0 || dim1
< 0 || !bset2
)
9730 return isl_lp_error
;
9732 bmap1
= join_initial(bset1
, bset2
, pos
);
9733 total
= isl_basic_map_dim(bmap1
, isl_dim_all
);
9735 return isl_lp_error
;
9738 obj
= isl_vec_alloc(ctx
, 1 + total
);
9741 isl_seq_clr(obj
->block
.data
, 1 + total
);
9742 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
], 1);
9743 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
+(dim1
-pos
)], -1);
9744 res
= isl_basic_map_solve_lp(bmap1
, 1, obj
->block
.data
, ctx
->one
,
9746 isl_basic_map_free(bmap1
);
9750 isl_basic_map_free(bmap1
);
9751 return isl_lp_error
;
9754 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
9755 * for any common value of the parameters and dimensions preceding pos
9756 * in both basic sets, the values of dimension pos in bset1 are
9757 * smaller or larger than those in bset2.
9760 * 1 if bset1 follows bset2
9761 * -1 if bset1 precedes bset2
9762 * 0 if bset1 and bset2 are incomparable
9763 * -2 if some error occurred.
9765 int isl_basic_set_compare_at(__isl_keep isl_basic_set
*bset1
,
9766 __isl_keep isl_basic_set
*bset2
, int pos
)
9769 enum isl_lp_result res
;
9774 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
9776 if (res
== isl_lp_empty
)
9778 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
9779 res
== isl_lp_unbounded
)
9781 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
9790 /* Given two basic sets bset1 and bset2, check whether
9791 * for any common value of the parameters and dimensions preceding pos
9792 * there is a value of dimension pos in bset1 that is larger
9793 * than a value of the same dimension in bset2.
9796 * 1 if there exists such a pair
9797 * 0 if there is no such pair, but there is a pair of equal values
9799 * -2 if some error occurred.
9801 int isl_basic_set_follows_at(__isl_keep isl_basic_set
*bset1
,
9802 __isl_keep isl_basic_set
*bset2
, int pos
)
9805 isl_basic_map
*bmap
;
9808 dim1
= isl_basic_set_dim(bset1
, isl_dim_set
);
9811 bmap
= join_initial(bset1
, bset2
, pos
);
9812 bmap
= isl_basic_map_order_ge(bmap
, isl_dim_out
, 0,
9813 isl_dim_out
, dim1
- pos
);
9814 empty
= isl_basic_map_is_empty(bmap
);
9818 isl_basic_map_free(bmap
);
9821 bmap
= isl_basic_map_order_gt(bmap
, isl_dim_out
, 0,
9822 isl_dim_out
, dim1
- pos
);
9823 empty
= isl_basic_map_is_empty(bmap
);
9826 isl_basic_map_free(bmap
);
9831 isl_basic_map_free(bmap
);
9835 /* Given two sets set1 and set2, check whether
9836 * for any common value of the parameters and dimensions preceding pos
9837 * there is a value of dimension pos in set1 that is larger
9838 * than a value of the same dimension in set2.
9841 * 1 if there exists such a pair
9842 * 0 if there is no such pair, but there is a pair of equal values
9844 * -2 if some error occurred.
9846 int isl_set_follows_at(__isl_keep isl_set
*set1
,
9847 __isl_keep isl_set
*set2
, int pos
)
9855 for (i
= 0; i
< set1
->n
; ++i
)
9856 for (j
= 0; j
< set2
->n
; ++j
) {
9858 f
= isl_basic_set_follows_at(set1
->p
[i
], set2
->p
[j
], pos
);
9859 if (f
== 1 || f
== -2)
9868 static isl_bool
isl_basic_map_plain_has_fixed_var(
9869 __isl_keep isl_basic_map
*bmap
, unsigned pos
, isl_int
*val
)
9875 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
9877 return isl_bool_error
;
9878 for (i
= 0, d
= total
-1; i
< bmap
->n_eq
&& d
+1 > pos
; ++i
) {
9879 for (; d
+1 > pos
; --d
)
9880 if (!isl_int_is_zero(bmap
->eq
[i
][1+d
]))
9884 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1, d
) != -1)
9885 return isl_bool_false
;
9886 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1+d
+1, total
-d
-1) != -1)
9887 return isl_bool_false
;
9888 if (!isl_int_is_one(bmap
->eq
[i
][1+d
]))
9889 return isl_bool_false
;
9891 isl_int_neg(*val
, bmap
->eq
[i
][0]);
9892 return isl_bool_true
;
9894 return isl_bool_false
;
9897 static isl_bool
isl_map_plain_has_fixed_var(__isl_keep isl_map
*map
,
9898 unsigned pos
, isl_int
*val
)
9906 return isl_bool_error
;
9908 return isl_bool_false
;
9910 return isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, val
);
9913 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, &v
);
9914 for (i
= 1; fixed
== isl_bool_true
&& i
< map
->n
; ++i
) {
9915 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[i
], pos
, &tmp
);
9916 if (fixed
== isl_bool_true
&& isl_int_ne(tmp
, v
))
9917 fixed
= isl_bool_false
;
9920 isl_int_set(*val
, v
);
9926 static isl_bool
isl_basic_set_plain_has_fixed_var(
9927 __isl_keep isl_basic_set
*bset
, unsigned pos
, isl_int
*val
)
9929 return isl_basic_map_plain_has_fixed_var(bset_to_bmap(bset
),
9933 isl_bool
isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map
*bmap
,
9934 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
9936 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
9937 return isl_bool_error
;
9938 return isl_basic_map_plain_has_fixed_var(bmap
,
9939 isl_basic_map_offset(bmap
, type
) - 1 + pos
, val
);
9942 /* If "bmap" obviously lies on a hyperplane where the given dimension
9943 * has a fixed value, then return that value.
9944 * Otherwise return NaN.
9946 __isl_give isl_val
*isl_basic_map_plain_get_val_if_fixed(
9947 __isl_keep isl_basic_map
*bmap
,
9948 enum isl_dim_type type
, unsigned pos
)
9956 ctx
= isl_basic_map_get_ctx(bmap
);
9957 v
= isl_val_alloc(ctx
);
9960 fixed
= isl_basic_map_plain_is_fixed(bmap
, type
, pos
, &v
->n
);
9962 return isl_val_free(v
);
9964 isl_int_set_si(v
->d
, 1);
9968 return isl_val_nan(ctx
);
9971 isl_bool
isl_map_plain_is_fixed(__isl_keep isl_map
*map
,
9972 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
9974 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
9975 return isl_bool_error
;
9976 return isl_map_plain_has_fixed_var(map
,
9977 map_offset(map
, type
) - 1 + pos
, val
);
9980 /* If "map" obviously lies on a hyperplane where the given dimension
9981 * has a fixed value, then return that value.
9982 * Otherwise return NaN.
9984 __isl_give isl_val
*isl_map_plain_get_val_if_fixed(__isl_keep isl_map
*map
,
9985 enum isl_dim_type type
, unsigned pos
)
9993 ctx
= isl_map_get_ctx(map
);
9994 v
= isl_val_alloc(ctx
);
9997 fixed
= isl_map_plain_is_fixed(map
, type
, pos
, &v
->n
);
9999 return isl_val_free(v
);
10001 isl_int_set_si(v
->d
, 1);
10005 return isl_val_nan(ctx
);
10008 /* If "set" obviously lies on a hyperplane where the given dimension
10009 * has a fixed value, then return that value.
10010 * Otherwise return NaN.
10012 __isl_give isl_val
*isl_set_plain_get_val_if_fixed(__isl_keep isl_set
*set
,
10013 enum isl_dim_type type
, unsigned pos
)
10015 return isl_map_plain_get_val_if_fixed(set
, type
, pos
);
10018 /* Return a sequence of values in the same space as "set"
10019 * that are equal to the corresponding set dimensions of "set"
10020 * for those set dimensions that obviously lie on a hyperplane
10021 * where the dimension has a fixed value.
10022 * The other elements are set to NaN.
10024 __isl_give isl_multi_val
*isl_set_get_plain_multi_val_if_fixed(
10025 __isl_keep isl_set
*set
)
10032 space
= isl_space_drop_all_params(isl_set_get_space(set
));
10033 mv
= isl_multi_val_alloc(space
);
10034 n
= isl_multi_val_size(mv
);
10036 return isl_multi_val_free(mv
);
10038 for (i
= 0; i
< n
; ++i
) {
10041 v
= isl_set_plain_get_val_if_fixed(set
, isl_dim_set
, i
);
10042 mv
= isl_multi_val_set_val(mv
, i
, v
);
10048 /* Check if dimension dim has fixed value and if so and if val is not NULL,
10049 * then return this fixed value in *val.
10051 isl_bool
isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set
*bset
,
10052 unsigned dim
, isl_int
*val
)
10056 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
10058 return isl_bool_error
;
10059 return isl_basic_set_plain_has_fixed_var(bset
, nparam
+ dim
, val
);
10062 /* Return -1 if the constraint "c1" should be sorted before "c2"
10063 * and 1 if it should be sorted after "c2".
10064 * Return 0 if the two constraints are the same (up to the constant term).
10066 * In particular, if a constraint involves later variables than another
10067 * then it is sorted after this other constraint.
10068 * uset_gist depends on constraints without existentially quantified
10069 * variables sorting first.
10071 * For constraints that have the same latest variable, those
10072 * with the same coefficient for this latest variable (first in absolute value
10073 * and then in actual value) are grouped together.
10074 * This is useful for detecting pairs of constraints that can
10075 * be chained in their printed representation.
10077 * Finally, within a group, constraints are sorted according to
10078 * their coefficients (excluding the constant term).
10080 static int sort_constraint_cmp(const void *p1
, const void *p2
, void *arg
)
10082 isl_int
**c1
= (isl_int
**) p1
;
10083 isl_int
**c2
= (isl_int
**) p2
;
10085 unsigned size
= *(unsigned *) arg
;
10088 l1
= isl_seq_last_non_zero(*c1
+ 1, size
);
10089 l2
= isl_seq_last_non_zero(*c2
+ 1, size
);
10094 cmp
= isl_int_abs_cmp((*c1
)[1 + l1
], (*c2
)[1 + l1
]);
10097 cmp
= isl_int_cmp((*c1
)[1 + l1
], (*c2
)[1 + l1
]);
10101 return isl_seq_cmp(*c1
+ 1, *c2
+ 1, size
);
10104 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
10105 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
10106 * and 0 if the two constraints are the same (up to the constant term).
10108 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map
*bmap
,
10109 isl_int
*c1
, isl_int
*c2
)
10114 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
10118 return sort_constraint_cmp(&c1
, &c2
, &size
);
10121 __isl_give isl_basic_map
*isl_basic_map_sort_constraints(
10122 __isl_take isl_basic_map
*bmap
)
10129 if (bmap
->n_ineq
== 0)
10131 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_SORTED
))
10133 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
10135 return isl_basic_map_free(bmap
);
10137 if (isl_sort(bmap
->ineq
, bmap
->n_ineq
, sizeof(isl_int
*),
10138 &sort_constraint_cmp
, &size
) < 0)
10139 return isl_basic_map_free(bmap
);
10140 ISL_F_SET(bmap
, ISL_BASIC_MAP_SORTED
);
10144 __isl_give isl_basic_set
*isl_basic_set_sort_constraints(
10145 __isl_take isl_basic_set
*bset
)
10147 isl_basic_map
*bmap
= bset_to_bmap(bset
);
10148 return bset_from_bmap(isl_basic_map_sort_constraints(bmap
));
10151 __isl_give isl_basic_map
*isl_basic_map_normalize(
10152 __isl_take isl_basic_map
*bmap
)
10154 bmap
= isl_basic_map_remove_redundancies(bmap
);
10155 bmap
= isl_basic_map_sort_constraints(bmap
);
10158 int isl_basic_map_plain_cmp(__isl_keep isl_basic_map
*bmap1
,
10159 __isl_keep isl_basic_map
*bmap2
)
10163 isl_space
*space1
, *space2
;
10165 if (!bmap1
|| !bmap2
)
10168 if (bmap1
== bmap2
)
10170 space1
= isl_basic_map_peek_space(bmap1
);
10171 space2
= isl_basic_map_peek_space(bmap2
);
10172 cmp
= isl_space_cmp(space1
, space2
);
10175 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) !=
10176 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_RATIONAL
))
10177 return ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) ? -1 : 1;
10178 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
) &&
10179 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
10181 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
))
10183 if (ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
10185 if (bmap1
->n_eq
!= bmap2
->n_eq
)
10186 return bmap1
->n_eq
- bmap2
->n_eq
;
10187 if (bmap1
->n_ineq
!= bmap2
->n_ineq
)
10188 return bmap1
->n_ineq
- bmap2
->n_ineq
;
10189 if (bmap1
->n_div
!= bmap2
->n_div
)
10190 return bmap1
->n_div
- bmap2
->n_div
;
10191 total
= isl_basic_map_dim(bmap1
, isl_dim_all
);
10194 for (i
= 0; i
< bmap1
->n_eq
; ++i
) {
10195 cmp
= isl_seq_cmp(bmap1
->eq
[i
], bmap2
->eq
[i
], 1+total
);
10199 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
10200 cmp
= isl_seq_cmp(bmap1
->ineq
[i
], bmap2
->ineq
[i
], 1+total
);
10204 for (i
= 0; i
< bmap1
->n_div
; ++i
) {
10205 cmp
= isl_seq_cmp(bmap1
->div
[i
], bmap2
->div
[i
], 1+1+total
);
10212 int isl_basic_set_plain_cmp(__isl_keep isl_basic_set
*bset1
,
10213 __isl_keep isl_basic_set
*bset2
)
10215 return isl_basic_map_plain_cmp(bset1
, bset2
);
10218 int isl_set_plain_cmp(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
10224 if (set1
->n
!= set2
->n
)
10225 return set1
->n
- set2
->n
;
10227 for (i
= 0; i
< set1
->n
; ++i
) {
10228 cmp
= isl_basic_set_plain_cmp(set1
->p
[i
], set2
->p
[i
]);
10236 isl_bool
isl_basic_map_plain_is_equal(__isl_keep isl_basic_map
*bmap1
,
10237 __isl_keep isl_basic_map
*bmap2
)
10239 if (!bmap1
|| !bmap2
)
10240 return isl_bool_error
;
10241 return isl_basic_map_plain_cmp(bmap1
, bmap2
) == 0;
10244 isl_bool
isl_basic_set_plain_is_equal(__isl_keep isl_basic_set
*bset1
,
10245 __isl_keep isl_basic_set
*bset2
)
10247 return isl_basic_map_plain_is_equal(bset_to_bmap(bset1
),
10248 bset_to_bmap(bset2
));
10251 static int qsort_bmap_cmp(const void *p1
, const void *p2
)
10253 isl_basic_map
*bmap1
= *(isl_basic_map
**) p1
;
10254 isl_basic_map
*bmap2
= *(isl_basic_map
**) p2
;
10256 return isl_basic_map_plain_cmp(bmap1
, bmap2
);
10259 /* Sort the basic maps of "map" and remove duplicate basic maps.
10261 * While removing basic maps, we make sure that the basic maps remain
10262 * sorted because isl_map_normalize expects the basic maps of the result
10265 static __isl_give isl_map
*sort_and_remove_duplicates(__isl_take isl_map
*map
)
10269 map
= isl_map_remove_empty_parts(map
);
10272 qsort(map
->p
, map
->n
, sizeof(struct isl_basic_map
*), qsort_bmap_cmp
);
10273 for (i
= map
->n
- 1; i
>= 1; --i
) {
10274 if (!isl_basic_map_plain_is_equal(map
->p
[i
- 1], map
->p
[i
]))
10276 isl_basic_map_free(map
->p
[i
-1]);
10277 for (j
= i
; j
< map
->n
; ++j
)
10278 map
->p
[j
- 1] = map
->p
[j
];
10285 /* Remove obvious duplicates among the basic maps of "map".
10287 * Unlike isl_map_normalize, this function does not remove redundant
10288 * constraints and only removes duplicates that have exactly the same
10289 * constraints in the input. It does sort the constraints and
10290 * the basic maps to ease the detection of duplicates.
10292 * If "map" has already been normalized or if the basic maps are
10293 * disjoint, then there can be no duplicates.
10295 __isl_give isl_map
*isl_map_remove_obvious_duplicates(__isl_take isl_map
*map
)
10298 isl_basic_map
*bmap
;
10304 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
| ISL_MAP_DISJOINT
))
10306 for (i
= 0; i
< map
->n
; ++i
) {
10307 bmap
= isl_basic_map_copy(map
->p
[i
]);
10308 bmap
= isl_basic_map_sort_constraints(bmap
);
10310 return isl_map_free(map
);
10311 isl_basic_map_free(map
->p
[i
]);
10315 map
= sort_and_remove_duplicates(map
);
10319 /* We normalize in place, but if anything goes wrong we need
10320 * to return NULL, so we need to make sure we don't change the
10321 * meaning of any possible other copies of map.
10323 __isl_give isl_map
*isl_map_normalize(__isl_take isl_map
*map
)
10326 struct isl_basic_map
*bmap
;
10330 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
))
10332 for (i
= 0; i
< map
->n
; ++i
) {
10333 bmap
= isl_basic_map_normalize(isl_basic_map_copy(map
->p
[i
]));
10336 isl_basic_map_free(map
->p
[i
]);
10340 map
= sort_and_remove_duplicates(map
);
10342 ISL_F_SET(map
, ISL_MAP_NORMALIZED
);
10349 __isl_give isl_set
*isl_set_normalize(__isl_take isl_set
*set
)
10351 return set_from_map(isl_map_normalize(set_to_map(set
)));
10354 isl_bool
isl_map_plain_is_equal(__isl_keep isl_map
*map1
,
10355 __isl_keep isl_map
*map2
)
10360 if (!map1
|| !map2
)
10361 return isl_bool_error
;
10364 return isl_bool_true
;
10365 equal
= isl_map_has_equal_space(map1
, map2
);
10366 if (equal
< 0 || !equal
)
10369 map1
= isl_map_copy(map1
);
10370 map2
= isl_map_copy(map2
);
10371 map1
= isl_map_normalize(map1
);
10372 map2
= isl_map_normalize(map2
);
10373 if (!map1
|| !map2
)
10375 equal
= map1
->n
== map2
->n
;
10376 for (i
= 0; equal
&& i
< map1
->n
; ++i
) {
10377 equal
= isl_basic_map_plain_is_equal(map1
->p
[i
], map2
->p
[i
]);
10381 isl_map_free(map1
);
10382 isl_map_free(map2
);
10385 isl_map_free(map1
);
10386 isl_map_free(map2
);
10387 return isl_bool_error
;
10390 isl_bool
isl_set_plain_is_equal(__isl_keep isl_set
*set1
,
10391 __isl_keep isl_set
*set2
)
10393 return isl_map_plain_is_equal(set_to_map(set1
), set_to_map(set2
));
10396 /* Return the basic maps in "map" as a list.
10398 __isl_give isl_basic_map_list
*isl_map_get_basic_map_list(
10399 __isl_keep isl_map
*map
)
10403 isl_basic_map_list
*list
;
10407 ctx
= isl_map_get_ctx(map
);
10408 list
= isl_basic_map_list_alloc(ctx
, map
->n
);
10410 for (i
= 0; i
< map
->n
; ++i
) {
10411 isl_basic_map
*bmap
;
10413 bmap
= isl_basic_map_copy(map
->p
[i
]);
10414 list
= isl_basic_map_list_add(list
, bmap
);
10420 /* Return the intersection of the elements in the non-empty list "list".
10421 * All elements are assumed to live in the same space.
10423 __isl_give isl_basic_map
*isl_basic_map_list_intersect(
10424 __isl_take isl_basic_map_list
*list
)
10428 isl_basic_map
*bmap
;
10430 n
= isl_basic_map_list_n_basic_map(list
);
10434 isl_die(isl_basic_map_list_get_ctx(list
), isl_error_invalid
,
10435 "expecting non-empty list", goto error
);
10437 bmap
= isl_basic_map_list_get_basic_map(list
, 0);
10438 for (i
= 1; i
< n
; ++i
) {
10439 isl_basic_map
*bmap_i
;
10441 bmap_i
= isl_basic_map_list_get_basic_map(list
, i
);
10442 bmap
= isl_basic_map_intersect(bmap
, bmap_i
);
10445 isl_basic_map_list_free(list
);
10448 isl_basic_map_list_free(list
);
10452 /* Return the intersection of the elements in the non-empty list "list".
10453 * All elements are assumed to live in the same space.
10455 __isl_give isl_basic_set
*isl_basic_set_list_intersect(
10456 __isl_take isl_basic_set_list
*list
)
10458 return isl_basic_map_list_intersect(list
);
10461 /* Return the union of the elements of "list".
10462 * The list is required to have at least one element.
10464 __isl_give isl_set
*isl_basic_set_list_union(
10465 __isl_take isl_basic_set_list
*list
)
10470 isl_basic_set
*bset
;
10473 n
= isl_basic_set_list_n_basic_set(list
);
10477 isl_die(isl_basic_set_list_get_ctx(list
), isl_error_invalid
,
10478 "expecting non-empty list", goto error
);
10480 bset
= isl_basic_set_list_get_basic_set(list
, 0);
10481 space
= isl_basic_set_get_space(bset
);
10482 isl_basic_set_free(bset
);
10484 set
= isl_set_alloc_space(space
, n
, 0);
10485 for (i
= 0; i
< n
; ++i
) {
10486 bset
= isl_basic_set_list_get_basic_set(list
, i
);
10487 set
= isl_set_add_basic_set(set
, bset
);
10490 isl_basic_set_list_free(list
);
10493 isl_basic_set_list_free(list
);
10497 /* Return the union of the elements in the non-empty list "list".
10498 * All elements are assumed to live in the same space.
10500 __isl_give isl_set
*isl_set_list_union(__isl_take isl_set_list
*list
)
10506 n
= isl_set_list_n_set(list
);
10510 isl_die(isl_set_list_get_ctx(list
), isl_error_invalid
,
10511 "expecting non-empty list", goto error
);
10513 set
= isl_set_list_get_set(list
, 0);
10514 for (i
= 1; i
< n
; ++i
) {
10517 set_i
= isl_set_list_get_set(list
, i
);
10518 set
= isl_set_union(set
, set_i
);
10521 isl_set_list_free(list
);
10524 isl_set_list_free(list
);
10528 __isl_give isl_basic_map
*isl_basic_map_product(
10529 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
10531 isl_space
*space_result
= NULL
;
10532 struct isl_basic_map
*bmap
;
10533 unsigned in1
, in2
, out1
, out2
, nparam
, total
, pos
;
10534 struct isl_dim_map
*dim_map1
, *dim_map2
;
10536 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
10538 space_result
= isl_space_product(isl_space_copy(bmap1
->dim
),
10539 isl_space_copy(bmap2
->dim
));
10541 in1
= isl_basic_map_dim(bmap1
, isl_dim_in
);
10542 in2
= isl_basic_map_dim(bmap2
, isl_dim_in
);
10543 out1
= isl_basic_map_dim(bmap1
, isl_dim_out
);
10544 out2
= isl_basic_map_dim(bmap2
, isl_dim_out
);
10545 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
10547 total
= nparam
+ in1
+ in2
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
10548 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
10549 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
10550 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
10551 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
10552 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
10553 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
10554 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
10555 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
10556 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
10557 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
10559 bmap
= isl_basic_map_alloc_space(space_result
,
10560 bmap1
->n_div
+ bmap2
->n_div
,
10561 bmap1
->n_eq
+ bmap2
->n_eq
,
10562 bmap1
->n_ineq
+ bmap2
->n_ineq
);
10563 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
10564 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
10565 bmap
= isl_basic_map_simplify(bmap
);
10566 return isl_basic_map_finalize(bmap
);
10568 isl_basic_map_free(bmap1
);
10569 isl_basic_map_free(bmap2
);
10573 __isl_give isl_basic_map
*isl_basic_map_flat_product(
10574 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
10576 isl_basic_map
*prod
;
10578 prod
= isl_basic_map_product(bmap1
, bmap2
);
10579 prod
= isl_basic_map_flatten(prod
);
10583 __isl_give isl_basic_set
*isl_basic_set_flat_product(
10584 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
10586 return isl_basic_map_flat_range_product(bset1
, bset2
);
10589 __isl_give isl_basic_map
*isl_basic_map_domain_product(
10590 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
10592 isl_space
*space1
, *space2
;
10593 isl_space
*space_result
= NULL
;
10594 isl_basic_map
*bmap
;
10595 isl_size in1
, in2
, out
, nparam
;
10596 unsigned total
, pos
;
10597 struct isl_dim_map
*dim_map1
, *dim_map2
;
10599 in1
= isl_basic_map_dim(bmap1
, isl_dim_in
);
10600 in2
= isl_basic_map_dim(bmap2
, isl_dim_in
);
10601 out
= isl_basic_map_dim(bmap1
, isl_dim_out
);
10602 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
10603 if (in1
< 0 || in2
< 0 || out
< 0 || nparam
< 0)
10606 space1
= isl_basic_map_get_space(bmap1
);
10607 space2
= isl_basic_map_get_space(bmap2
);
10608 space_result
= isl_space_domain_product(space1
, space2
);
10610 total
= nparam
+ in1
+ in2
+ out
+ bmap1
->n_div
+ bmap2
->n_div
;
10611 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
10612 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
10613 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
10614 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
10615 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
10616 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
10617 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
10618 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
);
10619 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out
);
10620 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
10622 bmap
= isl_basic_map_alloc_space(space_result
,
10623 bmap1
->n_div
+ bmap2
->n_div
,
10624 bmap1
->n_eq
+ bmap2
->n_eq
,
10625 bmap1
->n_ineq
+ bmap2
->n_ineq
);
10626 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
10627 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
10628 bmap
= isl_basic_map_simplify(bmap
);
10629 return isl_basic_map_finalize(bmap
);
10631 isl_basic_map_free(bmap1
);
10632 isl_basic_map_free(bmap2
);
10636 __isl_give isl_basic_map
*isl_basic_map_range_product(
10637 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
10640 isl_space
*space_result
= NULL
;
10641 isl_basic_map
*bmap
;
10642 isl_size in
, out1
, out2
, nparam
;
10643 unsigned total
, pos
;
10644 struct isl_dim_map
*dim_map1
, *dim_map2
;
10646 rational
= isl_basic_map_is_rational(bmap1
);
10647 if (rational
>= 0 && rational
)
10648 rational
= isl_basic_map_is_rational(bmap2
);
10649 in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
10650 out1
= isl_basic_map_dim(bmap1
, isl_dim_out
);
10651 out2
= isl_basic_map_dim(bmap2
, isl_dim_out
);
10652 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
10653 if (in
< 0 || out1
< 0 || out2
< 0 || nparam
< 0 || rational
< 0)
10656 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
10659 space_result
= isl_space_range_product(isl_space_copy(bmap1
->dim
),
10660 isl_space_copy(bmap2
->dim
));
10662 total
= nparam
+ in
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
10663 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
10664 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
10665 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
10666 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
10667 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
10668 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
10669 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in
);
10670 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
10671 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
10672 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
10674 bmap
= isl_basic_map_alloc_space(space_result
,
10675 bmap1
->n_div
+ bmap2
->n_div
,
10676 bmap1
->n_eq
+ bmap2
->n_eq
,
10677 bmap1
->n_ineq
+ bmap2
->n_ineq
);
10678 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
10679 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
10681 bmap
= isl_basic_map_set_rational(bmap
);
10682 bmap
= isl_basic_map_simplify(bmap
);
10683 return isl_basic_map_finalize(bmap
);
10685 isl_basic_map_free(bmap1
);
10686 isl_basic_map_free(bmap2
);
10690 __isl_give isl_basic_map
*isl_basic_map_flat_range_product(
10691 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
10693 isl_basic_map
*prod
;
10695 prod
= isl_basic_map_range_product(bmap1
, bmap2
);
10696 prod
= isl_basic_map_flatten_range(prod
);
10700 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
10701 * and collect the results.
10702 * The result live in the space obtained by calling "space_product"
10703 * on the spaces of "map1" and "map2".
10704 * If "remove_duplicates" is set then the result may contain duplicates
10705 * (even if the inputs do not) and so we try and remove the obvious
10708 static __isl_give isl_map
*map_product(__isl_take isl_map
*map1
,
10709 __isl_take isl_map
*map2
,
10710 __isl_give isl_space
*(*space_product
)(__isl_take isl_space
*left
,
10711 __isl_take isl_space
*right
),
10712 __isl_give isl_basic_map
*(*basic_map_product
)(
10713 __isl_take isl_basic_map
*left
,
10714 __isl_take isl_basic_map
*right
),
10715 int remove_duplicates
)
10717 unsigned flags
= 0;
10718 struct isl_map
*result
;
10722 m
= isl_map_has_equal_params(map1
, map2
);
10726 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
10727 "parameters don't match", goto error
);
10729 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
10730 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
10731 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
10733 result
= isl_map_alloc_space(space_product(isl_space_copy(map1
->dim
),
10734 isl_space_copy(map2
->dim
)),
10735 map1
->n
* map2
->n
, flags
);
10738 for (i
= 0; i
< map1
->n
; ++i
)
10739 for (j
= 0; j
< map2
->n
; ++j
) {
10740 struct isl_basic_map
*part
;
10741 part
= basic_map_product(isl_basic_map_copy(map1
->p
[i
]),
10742 isl_basic_map_copy(map2
->p
[j
]));
10743 if (isl_basic_map_is_empty(part
))
10744 isl_basic_map_free(part
);
10746 result
= isl_map_add_basic_map(result
, part
);
10750 if (remove_duplicates
)
10751 result
= isl_map_remove_obvious_duplicates(result
);
10752 isl_map_free(map1
);
10753 isl_map_free(map2
);
10756 isl_map_free(map1
);
10757 isl_map_free(map2
);
10761 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
10763 __isl_give isl_map
*isl_map_product(__isl_take isl_map
*map1
,
10764 __isl_take isl_map
*map2
)
10766 isl_map_align_params_bin(&map1
, &map2
);
10767 return map_product(map1
, map2
, &isl_space_product
,
10768 &isl_basic_map_product
, 0);
10771 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
10773 __isl_give isl_map
*isl_map_flat_product(__isl_take isl_map
*map1
,
10774 __isl_take isl_map
*map2
)
10778 prod
= isl_map_product(map1
, map2
);
10779 prod
= isl_map_flatten(prod
);
10783 /* Given two set A and B, construct its Cartesian product A x B.
10785 __isl_give isl_set
*isl_set_product(__isl_take isl_set
*set1
,
10786 __isl_take isl_set
*set2
)
10788 return isl_map_range_product(set1
, set2
);
10791 __isl_give isl_set
*isl_set_flat_product(__isl_take isl_set
*set1
,
10792 __isl_take isl_set
*set2
)
10794 return isl_map_flat_range_product(set1
, set2
);
10797 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
10799 __isl_give isl_map
*isl_map_domain_product(__isl_take isl_map
*map1
,
10800 __isl_take isl_map
*map2
)
10802 isl_map_align_params_bin(&map1
, &map2
);
10803 return map_product(map1
, map2
, &isl_space_domain_product
,
10804 &isl_basic_map_domain_product
, 1);
10807 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
10809 __isl_give isl_map
*isl_map_range_product(__isl_take isl_map
*map1
,
10810 __isl_take isl_map
*map2
)
10812 isl_map_align_params_bin(&map1
, &map2
);
10813 return map_product(map1
, map2
, &isl_space_range_product
,
10814 &isl_basic_map_range_product
, 1);
10817 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
10819 __isl_give isl_map
*isl_map_factor_domain(__isl_take isl_map
*map
)
10822 isl_size total1
, keep1
, total2
, keep2
;
10824 total1
= isl_map_dim(map
, isl_dim_in
);
10825 total2
= isl_map_dim(map
, isl_dim_out
);
10826 if (total1
< 0 || total2
< 0)
10827 return isl_map_free(map
);
10828 if (!isl_space_domain_is_wrapping(map
->dim
) ||
10829 !isl_space_range_is_wrapping(map
->dim
))
10830 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10831 "not a product", return isl_map_free(map
));
10833 space
= isl_map_get_space(map
);
10834 space
= isl_space_factor_domain(space
);
10835 keep1
= isl_space_dim(space
, isl_dim_in
);
10836 keep2
= isl_space_dim(space
, isl_dim_out
);
10837 if (keep1
< 0 || keep2
< 0)
10838 map
= isl_map_free(map
);
10839 map
= isl_map_project_out(map
, isl_dim_in
, keep1
, total1
- keep1
);
10840 map
= isl_map_project_out(map
, isl_dim_out
, keep2
, total2
- keep2
);
10841 map
= isl_map_reset_space(map
, space
);
10846 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
10848 __isl_give isl_map
*isl_map_factor_range(__isl_take isl_map
*map
)
10851 isl_size total1
, keep1
, total2
, keep2
;
10853 total1
= isl_map_dim(map
, isl_dim_in
);
10854 total2
= isl_map_dim(map
, isl_dim_out
);
10855 if (total1
< 0 || total2
< 0)
10856 return isl_map_free(map
);
10857 if (!isl_space_domain_is_wrapping(map
->dim
) ||
10858 !isl_space_range_is_wrapping(map
->dim
))
10859 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10860 "not a product", return isl_map_free(map
));
10862 space
= isl_map_get_space(map
);
10863 space
= isl_space_factor_range(space
);
10864 keep1
= isl_space_dim(space
, isl_dim_in
);
10865 keep2
= isl_space_dim(space
, isl_dim_out
);
10866 if (keep1
< 0 || keep2
< 0)
10867 map
= isl_map_free(map
);
10868 map
= isl_map_project_out(map
, isl_dim_in
, 0, total1
- keep1
);
10869 map
= isl_map_project_out(map
, isl_dim_out
, 0, total2
- keep2
);
10870 map
= isl_map_reset_space(map
, space
);
10875 /* Given a map of the form [A -> B] -> C, return the map A -> C.
10877 __isl_give isl_map
*isl_map_domain_factor_domain(__isl_take isl_map
*map
)
10880 isl_size total
, keep
;
10882 total
= isl_map_dim(map
, isl_dim_in
);
10884 return isl_map_free(map
);
10885 if (!isl_space_domain_is_wrapping(map
->dim
))
10886 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10887 "domain is not a product", return isl_map_free(map
));
10889 space
= isl_map_get_space(map
);
10890 space
= isl_space_domain_factor_domain(space
);
10891 keep
= isl_space_dim(space
, isl_dim_in
);
10893 map
= isl_map_free(map
);
10894 map
= isl_map_project_out(map
, isl_dim_in
, keep
, total
- keep
);
10895 map
= isl_map_reset_space(map
, space
);
10900 /* Given a map of the form [A -> B] -> C, return the map B -> C.
10902 __isl_give isl_map
*isl_map_domain_factor_range(__isl_take isl_map
*map
)
10905 isl_size total
, keep
;
10907 total
= isl_map_dim(map
, isl_dim_in
);
10909 return isl_map_free(map
);
10910 if (!isl_space_domain_is_wrapping(map
->dim
))
10911 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10912 "domain is not a product", return isl_map_free(map
));
10914 space
= isl_map_get_space(map
);
10915 space
= isl_space_domain_factor_range(space
);
10916 keep
= isl_space_dim(space
, isl_dim_in
);
10918 map
= isl_map_free(map
);
10919 map
= isl_map_project_out(map
, isl_dim_in
, 0, total
- keep
);
10920 map
= isl_map_reset_space(map
, space
);
10925 /* Given a map A -> [B -> C], extract the map A -> B.
10927 __isl_give isl_map
*isl_map_range_factor_domain(__isl_take isl_map
*map
)
10930 isl_size total
, keep
;
10932 total
= isl_map_dim(map
, isl_dim_out
);
10934 return isl_map_free(map
);
10935 if (!isl_space_range_is_wrapping(map
->dim
))
10936 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10937 "range is not a product", return isl_map_free(map
));
10939 space
= isl_map_get_space(map
);
10940 space
= isl_space_range_factor_domain(space
);
10941 keep
= isl_space_dim(space
, isl_dim_out
);
10943 map
= isl_map_free(map
);
10944 map
= isl_map_project_out(map
, isl_dim_out
, keep
, total
- keep
);
10945 map
= isl_map_reset_space(map
, space
);
10950 /* Given a map A -> [B -> C], extract the map A -> C.
10952 __isl_give isl_map
*isl_map_range_factor_range(__isl_take isl_map
*map
)
10955 isl_size total
, keep
;
10957 total
= isl_map_dim(map
, isl_dim_out
);
10959 return isl_map_free(map
);
10960 if (!isl_space_range_is_wrapping(map
->dim
))
10961 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10962 "range is not a product", return isl_map_free(map
));
10964 space
= isl_map_get_space(map
);
10965 space
= isl_space_range_factor_range(space
);
10966 keep
= isl_space_dim(space
, isl_dim_out
);
10968 map
= isl_map_free(map
);
10969 map
= isl_map_project_out(map
, isl_dim_out
, 0, total
- keep
);
10970 map
= isl_map_reset_space(map
, space
);
10975 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
10977 __isl_give isl_map
*isl_map_flat_domain_product(__isl_take isl_map
*map1
,
10978 __isl_take isl_map
*map2
)
10982 prod
= isl_map_domain_product(map1
, map2
);
10983 prod
= isl_map_flatten_domain(prod
);
10987 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
10989 __isl_give isl_map
*isl_map_flat_range_product(__isl_take isl_map
*map1
,
10990 __isl_take isl_map
*map2
)
10994 prod
= isl_map_range_product(map1
, map2
);
10995 prod
= isl_map_flatten_range(prod
);
10999 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map
*bmap
)
11002 uint32_t hash
= isl_hash_init();
11007 bmap
= isl_basic_map_copy(bmap
);
11008 bmap
= isl_basic_map_normalize(bmap
);
11009 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
11012 isl_hash_byte(hash
, bmap
->n_eq
& 0xFF);
11013 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
11015 c_hash
= isl_seq_get_hash(bmap
->eq
[i
], 1 + total
);
11016 isl_hash_hash(hash
, c_hash
);
11018 isl_hash_byte(hash
, bmap
->n_ineq
& 0xFF);
11019 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
11021 c_hash
= isl_seq_get_hash(bmap
->ineq
[i
], 1 + total
);
11022 isl_hash_hash(hash
, c_hash
);
11024 isl_hash_byte(hash
, bmap
->n_div
& 0xFF);
11025 for (i
= 0; i
< bmap
->n_div
; ++i
) {
11027 if (isl_int_is_zero(bmap
->div
[i
][0]))
11029 isl_hash_byte(hash
, i
& 0xFF);
11030 c_hash
= isl_seq_get_hash(bmap
->div
[i
], 1 + 1 + total
);
11031 isl_hash_hash(hash
, c_hash
);
11033 isl_basic_map_free(bmap
);
11037 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set
*bset
)
11039 return isl_basic_map_get_hash(bset_to_bmap(bset
));
11042 uint32_t isl_map_get_hash(__isl_keep isl_map
*map
)
11049 map
= isl_map_copy(map
);
11050 map
= isl_map_normalize(map
);
11054 hash
= isl_hash_init();
11055 for (i
= 0; i
< map
->n
; ++i
) {
11056 uint32_t bmap_hash
;
11057 bmap_hash
= isl_basic_map_get_hash(map
->p
[i
]);
11058 isl_hash_hash(hash
, bmap_hash
);
11066 uint32_t isl_set_get_hash(__isl_keep isl_set
*set
)
11068 return isl_map_get_hash(set_to_map(set
));
11071 /* Return the number of basic maps in the (current) representation of "map".
11073 isl_size
isl_map_n_basic_map(__isl_keep isl_map
*map
)
11075 return map
? map
->n
: isl_size_error
;
11078 isl_size
isl_set_n_basic_set(__isl_keep isl_set
*set
)
11080 return set
? set
->n
: isl_size_error
;
11083 isl_stat
isl_map_foreach_basic_map(__isl_keep isl_map
*map
,
11084 isl_stat (*fn
)(__isl_take isl_basic_map
*bmap
, void *user
), void *user
)
11089 return isl_stat_error
;
11091 for (i
= 0; i
< map
->n
; ++i
)
11092 if (fn(isl_basic_map_copy(map
->p
[i
]), user
) < 0)
11093 return isl_stat_error
;
11095 return isl_stat_ok
;
11098 isl_stat
isl_set_foreach_basic_set(__isl_keep isl_set
*set
,
11099 isl_stat (*fn
)(__isl_take isl_basic_set
*bset
, void *user
), void *user
)
11104 return isl_stat_error
;
11106 for (i
= 0; i
< set
->n
; ++i
)
11107 if (fn(isl_basic_set_copy(set
->p
[i
]), user
) < 0)
11108 return isl_stat_error
;
11110 return isl_stat_ok
;
11113 /* Return a list of basic sets, the union of which is equal to "set".
11115 __isl_give isl_basic_set_list
*isl_set_get_basic_set_list(
11116 __isl_keep isl_set
*set
)
11119 isl_basic_set_list
*list
;
11124 list
= isl_basic_set_list_alloc(isl_set_get_ctx(set
), set
->n
);
11125 for (i
= 0; i
< set
->n
; ++i
) {
11126 isl_basic_set
*bset
;
11128 bset
= isl_basic_set_copy(set
->p
[i
]);
11129 list
= isl_basic_set_list_add(list
, bset
);
11135 __isl_give isl_basic_set
*isl_basic_set_lift(__isl_take isl_basic_set
*bset
)
11142 bset
= isl_basic_set_cow(bset
);
11146 space
= isl_basic_set_get_space(bset
);
11147 space
= isl_space_lift(space
, bset
->n_div
);
11150 isl_space_free(bset
->dim
);
11152 bset
->extra
-= bset
->n_div
;
11155 bset
= isl_basic_set_finalize(bset
);
11159 isl_basic_set_free(bset
);
11163 __isl_give isl_set
*isl_set_lift(__isl_take isl_set
*set
)
11169 set
= set_from_map(isl_map_align_divs_internal(set_to_map(set
)));
11174 set
= isl_set_cow(set
);
11178 n_div
= set
->p
[0]->n_div
;
11179 space
= isl_set_get_space(set
);
11180 space
= isl_space_lift(space
, n_div
);
11183 isl_space_free(set
->dim
);
11186 for (i
= 0; i
< set
->n
; ++i
) {
11187 set
->p
[i
] = isl_basic_set_lift(set
->p
[i
]);
11198 int isl_basic_set_size(__isl_keep isl_basic_set
*bset
)
11203 dim
= isl_basic_set_dim(bset
, isl_dim_all
);
11206 size
+= bset
->n_eq
* (1 + dim
);
11207 size
+= bset
->n_ineq
* (1 + dim
);
11208 size
+= bset
->n_div
* (2 + dim
);
11213 int isl_set_size(__isl_keep isl_set
*set
)
11221 for (i
= 0; i
< set
->n
; ++i
)
11222 size
+= isl_basic_set_size(set
->p
[i
]);
11227 /* Check if there is any lower bound (if lower == 0) and/or upper
11228 * bound (if upper == 0) on the specified dim.
11230 static isl_bool
basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
11231 enum isl_dim_type type
, unsigned pos
, int lower
, int upper
)
11235 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
11236 return isl_bool_error
;
11238 pos
+= isl_basic_map_offset(bmap
, type
);
11240 for (i
= 0; i
< bmap
->n_div
; ++i
) {
11241 if (isl_int_is_zero(bmap
->div
[i
][0]))
11243 if (!isl_int_is_zero(bmap
->div
[i
][1 + pos
]))
11244 return isl_bool_true
;
11247 for (i
= 0; i
< bmap
->n_eq
; ++i
)
11248 if (!isl_int_is_zero(bmap
->eq
[i
][pos
]))
11249 return isl_bool_true
;
11251 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
11252 int sgn
= isl_int_sgn(bmap
->ineq
[i
][pos
]);
11259 return lower
&& upper
;
11262 isl_bool
isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
11263 enum isl_dim_type type
, unsigned pos
)
11265 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 0);
11268 isl_bool
isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map
*bmap
,
11269 enum isl_dim_type type
, unsigned pos
)
11271 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 1);
11274 isl_bool
isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map
*bmap
,
11275 enum isl_dim_type type
, unsigned pos
)
11277 return basic_map_dim_is_bounded(bmap
, type
, pos
, 1, 0);
11280 isl_bool
isl_map_dim_is_bounded(__isl_keep isl_map
*map
,
11281 enum isl_dim_type type
, unsigned pos
)
11286 return isl_bool_error
;
11288 for (i
= 0; i
< map
->n
; ++i
) {
11290 bounded
= isl_basic_map_dim_is_bounded(map
->p
[i
], type
, pos
);
11291 if (bounded
< 0 || !bounded
)
11295 return isl_bool_true
;
11298 /* Return true if the specified dim is involved in both an upper bound
11299 * and a lower bound.
11301 isl_bool
isl_set_dim_is_bounded(__isl_keep isl_set
*set
,
11302 enum isl_dim_type type
, unsigned pos
)
11304 return isl_map_dim_is_bounded(set_to_map(set
), type
, pos
);
11307 /* Does "map" have a bound (according to "fn") for any of its basic maps?
11309 static isl_bool
has_any_bound(__isl_keep isl_map
*map
,
11310 enum isl_dim_type type
, unsigned pos
,
11311 isl_bool (*fn
)(__isl_keep isl_basic_map
*bmap
,
11312 enum isl_dim_type type
, unsigned pos
))
11317 return isl_bool_error
;
11319 for (i
= 0; i
< map
->n
; ++i
) {
11321 bounded
= fn(map
->p
[i
], type
, pos
);
11322 if (bounded
< 0 || bounded
)
11326 return isl_bool_false
;
11329 /* Return 1 if the specified dim is involved in any lower bound.
11331 isl_bool
isl_set_dim_has_any_lower_bound(__isl_keep isl_set
*set
,
11332 enum isl_dim_type type
, unsigned pos
)
11334 return has_any_bound(set
, type
, pos
,
11335 &isl_basic_map_dim_has_lower_bound
);
11338 /* Return 1 if the specified dim is involved in any upper bound.
11340 isl_bool
isl_set_dim_has_any_upper_bound(__isl_keep isl_set
*set
,
11341 enum isl_dim_type type
, unsigned pos
)
11343 return has_any_bound(set
, type
, pos
,
11344 &isl_basic_map_dim_has_upper_bound
);
11347 /* Does "map" have a bound (according to "fn") for all of its basic maps?
11349 static isl_bool
has_bound(__isl_keep isl_map
*map
,
11350 enum isl_dim_type type
, unsigned pos
,
11351 isl_bool (*fn
)(__isl_keep isl_basic_map
*bmap
,
11352 enum isl_dim_type type
, unsigned pos
))
11357 return isl_bool_error
;
11359 for (i
= 0; i
< map
->n
; ++i
) {
11361 bounded
= fn(map
->p
[i
], type
, pos
);
11362 if (bounded
< 0 || !bounded
)
11366 return isl_bool_true
;
11369 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
11371 isl_bool
isl_set_dim_has_lower_bound(__isl_keep isl_set
*set
,
11372 enum isl_dim_type type
, unsigned pos
)
11374 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_lower_bound
);
11377 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
11379 isl_bool
isl_set_dim_has_upper_bound(__isl_keep isl_set
*set
,
11380 enum isl_dim_type type
, unsigned pos
)
11382 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_upper_bound
);
11385 /* For each of the "n" variables starting at "first", determine
11386 * the sign of the variable and put the results in the first "n"
11387 * elements of the array "signs".
11389 * 1 means that the variable is non-negative
11390 * -1 means that the variable is non-positive
11391 * 0 means the variable attains both positive and negative values.
11393 isl_stat
isl_basic_set_vars_get_sign(__isl_keep isl_basic_set
*bset
,
11394 unsigned first
, unsigned n
, int *signs
)
11396 isl_vec
*bound
= NULL
;
11397 struct isl_tab
*tab
= NULL
;
11398 struct isl_tab_undo
*snap
;
11402 total
= isl_basic_set_dim(bset
, isl_dim_all
);
11403 if (total
< 0 || !signs
)
11404 return isl_stat_error
;
11406 bound
= isl_vec_alloc(bset
->ctx
, 1 + total
);
11407 tab
= isl_tab_from_basic_set(bset
, 0);
11408 if (!bound
|| !tab
)
11411 isl_seq_clr(bound
->el
, bound
->size
);
11412 isl_int_set_si(bound
->el
[0], -1);
11414 snap
= isl_tab_snap(tab
);
11415 for (i
= 0; i
< n
; ++i
) {
11418 isl_int_set_si(bound
->el
[1 + first
+ i
], -1);
11419 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
11421 empty
= tab
->empty
;
11422 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
11423 if (isl_tab_rollback(tab
, snap
) < 0)
11431 isl_int_set_si(bound
->el
[1 + first
+ i
], 1);
11432 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
11434 empty
= tab
->empty
;
11435 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
11436 if (isl_tab_rollback(tab
, snap
) < 0)
11439 signs
[i
] = empty
? -1 : 0;
11443 isl_vec_free(bound
);
11444 return isl_stat_ok
;
11447 isl_vec_free(bound
);
11448 return isl_stat_error
;
11451 isl_stat
isl_basic_set_dims_get_sign(__isl_keep isl_basic_set
*bset
,
11452 enum isl_dim_type type
, unsigned first
, unsigned n
, int *signs
)
11454 if (!bset
|| !signs
)
11455 return isl_stat_error
;
11456 if (isl_basic_set_check_range(bset
, type
, first
, n
) < 0)
11457 return isl_stat_error
;
11459 first
+= pos(bset
->dim
, type
) - 1;
11460 return isl_basic_set_vars_get_sign(bset
, first
, n
, signs
);
11463 /* Is it possible for the integer division "div" to depend (possibly
11464 * indirectly) on any output dimensions?
11466 * If the div is undefined, then we conservatively assume that it
11467 * may depend on them.
11468 * Otherwise, we check if it actually depends on them or on any integer
11469 * divisions that may depend on them.
11471 static isl_bool
div_may_involve_output(__isl_keep isl_basic_map
*bmap
, int div
)
11474 isl_size n_out
, n_div
;
11475 unsigned o_out
, o_div
;
11477 if (isl_int_is_zero(bmap
->div
[div
][0]))
11478 return isl_bool_true
;
11480 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
11482 return isl_bool_error
;
11483 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
11485 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + o_out
, n_out
) != -1)
11486 return isl_bool_true
;
11488 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
11490 return isl_bool_error
;
11491 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
11493 for (i
= 0; i
< n_div
; ++i
) {
11494 isl_bool may_involve
;
11496 if (isl_int_is_zero(bmap
->div
[div
][1 + o_div
+ i
]))
11498 may_involve
= div_may_involve_output(bmap
, i
);
11499 if (may_involve
< 0 || may_involve
)
11500 return may_involve
;
11503 return isl_bool_false
;
11506 /* Return the first integer division of "bmap" in the range
11507 * [first, first + n[ that may depend on any output dimensions and
11508 * that has a non-zero coefficient in "c" (where the first coefficient
11509 * in "c" corresponds to integer division "first").
11511 static int first_div_may_involve_output(__isl_keep isl_basic_map
*bmap
,
11512 isl_int
*c
, int first
, int n
)
11519 for (k
= first
; k
< first
+ n
; ++k
) {
11520 isl_bool may_involve
;
11522 if (isl_int_is_zero(c
[k
]))
11524 may_involve
= div_may_involve_output(bmap
, k
);
11525 if (may_involve
< 0)
11534 /* Look for a pair of inequality constraints in "bmap" of the form
11536 * -l + i >= 0 or i >= l
11538 * n + l - i >= 0 or i <= l + n
11540 * with n < "m" and i the output dimension at position "pos".
11541 * (Note that n >= 0 as otherwise the two constraints would conflict.)
11542 * Furthermore, "l" is only allowed to involve parameters, input dimensions
11543 * and earlier output dimensions, as well as integer divisions that do
11544 * not involve any of the output dimensions.
11546 * Return the index of the first inequality constraint or bmap->n_ineq
11547 * if no such pair can be found.
11549 static int find_modulo_constraint_pair(__isl_keep isl_basic_map
*bmap
,
11550 int pos
, isl_int m
)
11555 isl_size n_div
, n_out
;
11556 unsigned o_div
, o_out
;
11559 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
11560 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
11561 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
11562 if (total
< 0 || n_out
< 0 || n_div
< 0)
11565 ctx
= isl_basic_map_get_ctx(bmap
);
11566 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
11567 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
11568 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
11569 if (!isl_int_abs_eq(bmap
->ineq
[i
][o_out
+ pos
], ctx
->one
))
11571 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + o_out
+ pos
+ 1,
11572 n_out
- (pos
+ 1)) != -1)
11574 if (first_div_may_involve_output(bmap
, bmap
->ineq
[i
] + o_div
,
11577 for (j
= i
+ 1; j
< bmap
->n_ineq
; ++j
) {
11578 if (!isl_int_abs_eq(bmap
->ineq
[j
][o_out
+ pos
],
11581 if (!isl_seq_is_neg(bmap
->ineq
[i
] + 1,
11582 bmap
->ineq
[j
] + 1, total
))
11586 if (j
>= bmap
->n_ineq
)
11588 isl_int_add(bmap
->ineq
[i
][0],
11589 bmap
->ineq
[i
][0], bmap
->ineq
[j
][0]);
11590 less
= isl_int_abs_lt(bmap
->ineq
[i
][0], m
);
11591 isl_int_sub(bmap
->ineq
[i
][0],
11592 bmap
->ineq
[i
][0], bmap
->ineq
[j
][0]);
11595 if (isl_int_is_one(bmap
->ineq
[i
][o_out
+ pos
]))
11601 return bmap
->n_ineq
;
11604 /* Return the index of the equality of "bmap" that defines
11605 * the output dimension "pos" in terms of earlier dimensions.
11606 * The equality may also involve integer divisions, as long
11607 * as those integer divisions are defined in terms of
11608 * parameters or input dimensions.
11609 * In this case, *div is set to the number of integer divisions and
11610 * *ineq is set to the number of inequality constraints (provided
11611 * div and ineq are not NULL).
11613 * The equality may also involve a single integer division involving
11614 * the output dimensions (typically only output dimension "pos") as
11615 * long as the coefficient of output dimension "pos" is 1 or -1 and
11616 * there is a pair of constraints i >= l and i <= l + n, with i referring
11617 * to output dimension "pos", l an expression involving only earlier
11618 * dimensions and n smaller than the coefficient of the integer division
11619 * in the equality. In this case, the output dimension can be defined
11620 * in terms of a modulo expression that does not involve the integer division.
11621 * *div is then set to this single integer division and
11622 * *ineq is set to the index of constraint i >= l.
11624 * Return bmap->n_eq if there is no such equality.
11625 * Return -1 on error.
11627 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map
*bmap
,
11628 int pos
, int *div
, int *ineq
)
11631 isl_size n_div
, n_out
;
11632 unsigned o_div
, o_out
;
11634 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
11635 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
11636 if (n_out
< 0 || n_div
< 0)
11639 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
11640 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
11643 *ineq
= bmap
->n_ineq
;
11646 for (j
= 0; j
< bmap
->n_eq
; ++j
) {
11647 if (isl_int_is_zero(bmap
->eq
[j
][o_out
+ pos
]))
11649 if (isl_seq_first_non_zero(bmap
->eq
[j
] + o_out
+ pos
+ 1,
11650 n_out
- (pos
+ 1)) != -1)
11652 k
= first_div_may_involve_output(bmap
, bmap
->eq
[j
] + o_div
,
11656 if (!isl_int_is_one(bmap
->eq
[j
][o_out
+ pos
]) &&
11657 !isl_int_is_negone(bmap
->eq
[j
][o_out
+ pos
]))
11659 if (first_div_may_involve_output(bmap
, bmap
->eq
[j
] + o_div
,
11660 k
+ 1, n_div
- (k
+1)) < n_div
)
11662 l
= find_modulo_constraint_pair(bmap
, pos
,
11663 bmap
->eq
[j
][o_div
+ k
]);
11666 if (l
>= bmap
->n_ineq
)
11678 /* Check if the given basic map is obviously single-valued.
11679 * In particular, for each output dimension, check that there is
11680 * an equality that defines the output dimension in terms of
11681 * earlier dimensions.
11683 isl_bool
isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map
*bmap
)
11688 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
11690 return isl_bool_error
;
11692 for (i
= 0; i
< n_out
; ++i
) {
11695 eq
= isl_basic_map_output_defining_equality(bmap
, i
,
11698 return isl_bool_error
;
11699 if (eq
>= bmap
->n_eq
)
11700 return isl_bool_false
;
11703 return isl_bool_true
;
11706 /* Check if the given basic map is single-valued.
11707 * We simply compute
11711 * and check if the result is a subset of the identity mapping.
11713 isl_bool
isl_basic_map_is_single_valued(__isl_keep isl_basic_map
*bmap
)
11716 isl_basic_map
*test
;
11720 sv
= isl_basic_map_plain_is_single_valued(bmap
);
11724 test
= isl_basic_map_reverse(isl_basic_map_copy(bmap
));
11725 test
= isl_basic_map_apply_range(test
, isl_basic_map_copy(bmap
));
11727 space
= isl_basic_map_get_space(bmap
);
11728 space
= isl_space_map_from_set(isl_space_range(space
));
11729 id
= isl_basic_map_identity(space
);
11731 sv
= isl_basic_map_is_subset(test
, id
);
11733 isl_basic_map_free(test
);
11734 isl_basic_map_free(id
);
11739 /* Check if the given map is obviously single-valued.
11741 isl_bool
isl_map_plain_is_single_valued(__isl_keep isl_map
*map
)
11744 return isl_bool_error
;
11746 return isl_bool_true
;
11748 return isl_bool_false
;
11750 return isl_basic_map_plain_is_single_valued(map
->p
[0]);
11753 /* Check if the given map is single-valued.
11754 * We simply compute
11758 * and check if the result is a subset of the identity mapping.
11760 isl_bool
isl_map_is_single_valued(__isl_keep isl_map
*map
)
11767 sv
= isl_map_plain_is_single_valued(map
);
11771 test
= isl_map_reverse(isl_map_copy(map
));
11772 test
= isl_map_apply_range(test
, isl_map_copy(map
));
11774 space
= isl_space_map_from_set(isl_space_range(isl_map_get_space(map
)));
11775 id
= isl_map_identity(space
);
11777 sv
= isl_map_is_subset(test
, id
);
11779 isl_map_free(test
);
11785 isl_bool
isl_map_is_injective(__isl_keep isl_map
*map
)
11789 map
= isl_map_copy(map
);
11790 map
= isl_map_reverse(map
);
11791 in
= isl_map_is_single_valued(map
);
11797 /* Check if the given map is obviously injective.
11799 isl_bool
isl_map_plain_is_injective(__isl_keep isl_map
*map
)
11803 map
= isl_map_copy(map
);
11804 map
= isl_map_reverse(map
);
11805 in
= isl_map_plain_is_single_valued(map
);
11811 isl_bool
isl_map_is_bijective(__isl_keep isl_map
*map
)
11815 sv
= isl_map_is_single_valued(map
);
11819 return isl_map_is_injective(map
);
11822 isl_bool
isl_set_is_singleton(__isl_keep isl_set
*set
)
11824 return isl_map_is_single_valued(set_to_map(set
));
11827 /* Does "map" only map elements to themselves?
11829 * If the domain and range spaces are different, then "map"
11830 * is considered not to be an identity relation, even if it is empty.
11831 * Otherwise, construct the maximal identity relation and
11832 * check whether "map" is a subset of this relation.
11834 isl_bool
isl_map_is_identity(__isl_keep isl_map
*map
)
11837 isl_bool equal
, is_identity
;
11839 equal
= isl_map_tuple_is_equal(map
, isl_dim_in
, map
, isl_dim_out
);
11840 if (equal
< 0 || !equal
)
11843 id
= isl_map_identity(isl_map_get_space(map
));
11844 is_identity
= isl_map_is_subset(map
, id
);
11847 return is_identity
;
11850 int isl_map_is_translation(__isl_keep isl_map
*map
)
11855 delta
= isl_map_deltas(isl_map_copy(map
));
11856 ok
= isl_set_is_singleton(delta
);
11857 isl_set_free(delta
);
11862 static int unique(isl_int
*p
, unsigned pos
, unsigned len
)
11864 if (isl_seq_first_non_zero(p
, pos
) != -1)
11866 if (isl_seq_first_non_zero(p
+ pos
+ 1, len
- pos
- 1) != -1)
11871 isl_bool
isl_basic_set_is_box(__isl_keep isl_basic_set
*bset
)
11874 isl_size nvar
, n_div
;
11877 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
11879 return isl_bool_error
;
11881 return isl_bool_false
;
11883 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
11885 return isl_bool_error
;
11886 ovar
= isl_space_offset(bset
->dim
, isl_dim_set
);
11887 for (j
= 0; j
< nvar
; ++j
) {
11888 int lower
= 0, upper
= 0;
11889 for (i
= 0; i
< bset
->n_eq
; ++i
) {
11890 if (isl_int_is_zero(bset
->eq
[i
][1 + ovar
+ j
]))
11892 if (!unique(bset
->eq
[i
] + 1 + ovar
, j
, nvar
))
11893 return isl_bool_false
;
11896 if (i
< bset
->n_eq
)
11898 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
11899 if (isl_int_is_zero(bset
->ineq
[i
][1 + ovar
+ j
]))
11901 if (!unique(bset
->ineq
[i
] + 1 + ovar
, j
, nvar
))
11902 return isl_bool_false
;
11903 if (isl_int_is_pos(bset
->ineq
[i
][1 + ovar
+ j
]))
11908 if (!lower
|| !upper
)
11909 return isl_bool_false
;
11912 return isl_bool_true
;
11915 isl_bool
isl_set_is_box(__isl_keep isl_set
*set
)
11918 return isl_bool_error
;
11920 return isl_bool_false
;
11922 return isl_basic_set_is_box(set
->p
[0]);
11925 isl_bool
isl_basic_set_is_wrapping(__isl_keep isl_basic_set
*bset
)
11928 return isl_bool_error
;
11930 return isl_space_is_wrapping(bset
->dim
);
11933 isl_bool
isl_set_is_wrapping(__isl_keep isl_set
*set
)
11936 return isl_bool_error
;
11938 return isl_space_is_wrapping(set
->dim
);
11941 /* Modify the space of "map" through a call to "change".
11942 * If "can_change" is set (not NULL), then first call it to check
11943 * if the modification is allowed, printing the error message "cannot_change"
11946 static __isl_give isl_map
*isl_map_change_space(__isl_take isl_map
*map
,
11947 isl_bool (*can_change
)(__isl_keep isl_map
*map
),
11948 const char *cannot_change
,
11949 __isl_give isl_space
*(*change
)(__isl_take isl_space
*space
))
11957 ok
= can_change
? can_change(map
) : isl_bool_true
;
11959 return isl_map_free(map
);
11961 isl_die(isl_map_get_ctx(map
), isl_error_invalid
, cannot_change
,
11962 return isl_map_free(map
));
11964 space
= change(isl_map_get_space(map
));
11965 map
= isl_map_reset_space(map
, space
);
11970 /* Is the domain of "map" a wrapped relation?
11972 isl_bool
isl_map_domain_is_wrapping(__isl_keep isl_map
*map
)
11975 return isl_bool_error
;
11977 return isl_space_domain_is_wrapping(map
->dim
);
11980 /* Does "map" have a wrapped relation in both domain and range?
11982 isl_bool
isl_map_is_product(__isl_keep isl_map
*map
)
11984 return isl_space_is_product(isl_map_peek_space(map
));
11987 /* Is the range of "map" a wrapped relation?
11989 isl_bool
isl_map_range_is_wrapping(__isl_keep isl_map
*map
)
11992 return isl_bool_error
;
11994 return isl_space_range_is_wrapping(map
->dim
);
11997 __isl_give isl_basic_set
*isl_basic_map_wrap(__isl_take isl_basic_map
*bmap
)
12001 space
= isl_basic_map_take_space(bmap
);
12002 space
= isl_space_wrap(space
);
12003 bmap
= isl_basic_map_restore_space(bmap
, space
);
12005 bmap
= isl_basic_map_finalize(bmap
);
12007 return bset_from_bmap(bmap
);
12010 /* Given a map A -> B, return the set (A -> B).
12012 __isl_give isl_set
*isl_map_wrap(__isl_take isl_map
*map
)
12014 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_wrap
);
12017 __isl_give isl_basic_map
*isl_basic_set_unwrap(__isl_take isl_basic_set
*bset
)
12019 bset
= isl_basic_set_cow(bset
);
12023 bset
->dim
= isl_space_unwrap(bset
->dim
);
12027 bset
= isl_basic_set_finalize(bset
);
12029 return bset_to_bmap(bset
);
12031 isl_basic_set_free(bset
);
12035 /* Given a set (A -> B), return the map A -> B.
12036 * Error out if "set" is not of the form (A -> B).
12038 __isl_give isl_map
*isl_set_unwrap(__isl_take isl_set
*set
)
12040 return isl_map_change_space(set
, &isl_set_is_wrapping
,
12041 "not a wrapping set", &isl_space_unwrap
);
12044 __isl_give isl_basic_map
*isl_basic_map_reset(__isl_take isl_basic_map
*bmap
,
12045 enum isl_dim_type type
)
12049 space
= isl_basic_map_take_space(bmap
);
12050 space
= isl_space_reset(space
, type
);
12051 bmap
= isl_basic_map_restore_space(bmap
, space
);
12053 bmap
= isl_basic_map_mark_final(bmap
);
12058 __isl_give isl_map
*isl_map_reset(__isl_take isl_map
*map
,
12059 enum isl_dim_type type
)
12067 if (!isl_space_is_named_or_nested(map
->dim
, type
))
12070 map
= isl_map_cow(map
);
12074 for (i
= 0; i
< map
->n
; ++i
) {
12075 map
->p
[i
] = isl_basic_map_reset(map
->p
[i
], type
);
12080 space
= isl_map_take_space(map
);
12081 space
= isl_space_reset(space
, type
);
12082 map
= isl_map_restore_space(map
, space
);
12090 __isl_give isl_basic_map
*isl_basic_map_flatten(__isl_take isl_basic_map
*bmap
)
12094 space
= isl_basic_map_take_space(bmap
);
12095 space
= isl_space_flatten(space
);
12096 bmap
= isl_basic_map_restore_space(bmap
, space
);
12098 bmap
= isl_basic_map_mark_final(bmap
);
12103 __isl_give isl_basic_set
*isl_basic_set_flatten(__isl_take isl_basic_set
*bset
)
12105 return bset_from_bmap(isl_basic_map_flatten(bset_to_bmap(bset
)));
12108 __isl_give isl_basic_map
*isl_basic_map_flatten_domain(
12109 __isl_take isl_basic_map
*bmap
)
12113 space
= isl_basic_map_take_space(bmap
);
12114 space
= isl_space_flatten_domain(space
);
12115 bmap
= isl_basic_map_restore_space(bmap
, space
);
12117 bmap
= isl_basic_map_mark_final(bmap
);
12122 __isl_give isl_basic_map
*isl_basic_map_flatten_range(
12123 __isl_take isl_basic_map
*bmap
)
12127 space
= isl_basic_map_take_space(bmap
);
12128 space
= isl_space_flatten_range(space
);
12129 bmap
= isl_basic_map_restore_space(bmap
, space
);
12131 bmap
= isl_basic_map_mark_final(bmap
);
12136 /* Remove any internal structure from the spaces of domain and range of "map".
12138 __isl_give isl_map
*isl_map_flatten(__isl_take isl_map
*map
)
12143 if (!map
->dim
->nested
[0] && !map
->dim
->nested
[1])
12146 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten
);
12149 __isl_give isl_set
*isl_set_flatten(__isl_take isl_set
*set
)
12151 return set_from_map(isl_map_flatten(set_to_map(set
)));
12154 __isl_give isl_map
*isl_set_flatten_map(__isl_take isl_set
*set
)
12156 isl_space
*space
, *flat_space
;
12159 space
= isl_set_get_space(set
);
12160 flat_space
= isl_space_flatten(isl_space_copy(space
));
12161 map
= isl_map_identity(isl_space_join(isl_space_reverse(space
),
12163 map
= isl_map_intersect_domain(map
, set
);
12168 /* Remove any internal structure from the space of the domain of "map".
12170 __isl_give isl_map
*isl_map_flatten_domain(__isl_take isl_map
*map
)
12175 if (!map
->dim
->nested
[0])
12178 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten_domain
);
12181 /* Remove any internal structure from the space of the range of "map".
12183 __isl_give isl_map
*isl_map_flatten_range(__isl_take isl_map
*map
)
12188 if (!map
->dim
->nested
[1])
12191 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten_range
);
12194 /* Reorder the dimensions of "bmap" according to the given dim_map
12195 * and set the dimension specification to "space" and
12196 * perform Gaussian elimination on the result.
12198 __isl_give isl_basic_map
*isl_basic_map_realign(__isl_take isl_basic_map
*bmap
,
12199 __isl_take isl_space
*space
, __isl_take
struct isl_dim_map
*dim_map
)
12201 isl_basic_map
*res
;
12205 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
12206 if (n_div
< 0 || !space
|| !dim_map
)
12209 flags
= bmap
->flags
;
12210 ISL_FL_CLR(flags
, ISL_BASIC_MAP_FINAL
);
12211 ISL_FL_CLR(flags
, ISL_BASIC_MAP_SORTED
);
12212 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
12213 res
= isl_basic_map_alloc_space(space
, n_div
, bmap
->n_eq
, bmap
->n_ineq
);
12214 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
12216 res
->flags
= flags
;
12217 res
= isl_basic_map_gauss(res
, NULL
);
12218 res
= isl_basic_map_finalize(res
);
12221 isl_dim_map_free(dim_map
);
12222 isl_basic_map_free(bmap
);
12223 isl_space_free(space
);
12227 /* Reorder the dimensions of "map" according to given reordering.
12229 __isl_give isl_map
*isl_map_realign(__isl_take isl_map
*map
,
12230 __isl_take isl_reordering
*r
)
12233 struct isl_dim_map
*dim_map
;
12235 map
= isl_map_cow(map
);
12236 dim_map
= isl_dim_map_from_reordering(r
);
12237 if (!map
|| !r
|| !dim_map
)
12240 for (i
= 0; i
< map
->n
; ++i
) {
12241 struct isl_dim_map
*dim_map_i
;
12244 dim_map_i
= isl_dim_map_extend(dim_map
, map
->p
[i
]);
12246 space
= isl_reordering_get_space(r
);
12247 map
->p
[i
] = isl_basic_map_realign(map
->p
[i
], space
, dim_map_i
);
12253 map
= isl_map_reset_space(map
, isl_reordering_get_space(r
));
12254 map
= isl_map_unmark_normalized(map
);
12256 isl_reordering_free(r
);
12257 isl_dim_map_free(dim_map
);
12260 isl_dim_map_free(dim_map
);
12262 isl_reordering_free(r
);
12266 __isl_give isl_set
*isl_set_realign(__isl_take isl_set
*set
,
12267 __isl_take isl_reordering
*r
)
12269 return set_from_map(isl_map_realign(set_to_map(set
), r
));
12272 __isl_give isl_map
*isl_map_align_params(__isl_take isl_map
*map
,
12273 __isl_take isl_space
*model
)
12278 if (!map
|| !model
)
12281 ctx
= isl_space_get_ctx(model
);
12282 if (!isl_space_has_named_params(model
))
12283 isl_die(ctx
, isl_error_invalid
,
12284 "model has unnamed parameters", goto error
);
12285 if (isl_map_check_named_params(map
) < 0)
12287 aligned
= isl_map_space_has_equal_params(map
, model
);
12291 isl_reordering
*exp
;
12293 exp
= isl_parameter_alignment_reordering(map
->dim
, model
);
12294 exp
= isl_reordering_extend_space(exp
, isl_map_get_space(map
));
12295 map
= isl_map_realign(map
, exp
);
12298 isl_space_free(model
);
12301 isl_space_free(model
);
12306 __isl_give isl_set
*isl_set_align_params(__isl_take isl_set
*set
,
12307 __isl_take isl_space
*model
)
12309 return isl_map_align_params(set
, model
);
12312 /* Align the parameters of "bmap" to those of "model", introducing
12313 * additional parameters if needed.
12315 __isl_give isl_basic_map
*isl_basic_map_align_params(
12316 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*model
)
12319 isl_bool equal_params
;
12321 if (!bmap
|| !model
)
12324 ctx
= isl_space_get_ctx(model
);
12325 if (!isl_space_has_named_params(model
))
12326 isl_die(ctx
, isl_error_invalid
,
12327 "model has unnamed parameters", goto error
);
12328 if (isl_basic_map_check_named_params(bmap
) < 0)
12330 equal_params
= isl_space_has_equal_params(bmap
->dim
, model
);
12331 if (equal_params
< 0)
12333 if (!equal_params
) {
12334 isl_reordering
*exp
;
12335 struct isl_dim_map
*dim_map
;
12337 exp
= isl_parameter_alignment_reordering(bmap
->dim
, model
);
12338 exp
= isl_reordering_extend_space(exp
,
12339 isl_basic_map_get_space(bmap
));
12340 dim_map
= isl_dim_map_from_reordering(exp
);
12341 bmap
= isl_basic_map_realign(bmap
,
12342 isl_reordering_get_space(exp
),
12343 isl_dim_map_extend(dim_map
, bmap
));
12344 isl_reordering_free(exp
);
12345 isl_dim_map_free(dim_map
);
12348 isl_space_free(model
);
12351 isl_space_free(model
);
12352 isl_basic_map_free(bmap
);
12356 /* Do "bset" and "space" have the same parameters?
12358 isl_bool
isl_basic_set_space_has_equal_params(__isl_keep isl_basic_set
*bset
,
12359 __isl_keep isl_space
*space
)
12361 isl_space
*bset_space
;
12363 bset_space
= isl_basic_set_peek_space(bset
);
12364 return isl_space_has_equal_params(bset_space
, space
);
12367 /* Do "map" and "space" have the same parameters?
12369 isl_bool
isl_map_space_has_equal_params(__isl_keep isl_map
*map
,
12370 __isl_keep isl_space
*space
)
12372 isl_space
*map_space
;
12374 map_space
= isl_map_peek_space(map
);
12375 return isl_space_has_equal_params(map_space
, space
);
12378 /* Do "set" and "space" have the same parameters?
12380 isl_bool
isl_set_space_has_equal_params(__isl_keep isl_set
*set
,
12381 __isl_keep isl_space
*space
)
12383 return isl_map_space_has_equal_params(set_to_map(set
), space
);
12386 /* Align the parameters of "bset" to those of "model", introducing
12387 * additional parameters if needed.
12389 __isl_give isl_basic_set
*isl_basic_set_align_params(
12390 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*model
)
12392 return isl_basic_map_align_params(bset
, model
);
12395 /* Drop all parameters not referenced by "map".
12397 __isl_give isl_map
*isl_map_drop_unused_params(__isl_take isl_map
*map
)
12402 n
= isl_map_dim(map
, isl_dim_param
);
12403 if (isl_map_check_named_params(map
) < 0 || n
< 0)
12404 return isl_map_free(map
);
12406 for (i
= n
- 1; i
>= 0; i
--) {
12409 involves
= isl_map_involves_dims(map
, isl_dim_param
, i
, 1);
12411 return isl_map_free(map
);
12413 map
= isl_map_project_out(map
, isl_dim_param
, i
, 1);
12419 /* Drop all parameters not referenced by "set".
12421 __isl_give isl_set
*isl_set_drop_unused_params(
12422 __isl_take isl_set
*set
)
12424 return set_from_map(isl_map_drop_unused_params(set_to_map(set
)));
12427 /* Drop all parameters not referenced by "bmap".
12429 __isl_give isl_basic_map
*isl_basic_map_drop_unused_params(
12430 __isl_take isl_basic_map
*bmap
)
12435 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
12436 if (nparam
< 0 || isl_basic_map_check_named_params(bmap
) < 0)
12437 return isl_basic_map_free(bmap
);
12439 for (i
= nparam
- 1; i
>= 0; i
--) {
12442 involves
= isl_basic_map_involves_dims(bmap
,
12443 isl_dim_param
, i
, 1);
12445 return isl_basic_map_free(bmap
);
12447 bmap
= isl_basic_map_drop(bmap
, isl_dim_param
, i
, 1);
12453 /* Drop all parameters not referenced by "bset".
12455 __isl_give isl_basic_set
*isl_basic_set_drop_unused_params(
12456 __isl_take isl_basic_set
*bset
)
12458 return bset_from_bmap(isl_basic_map_drop_unused_params(
12459 bset_to_bmap(bset
)));
12462 /* Given a tuple of identifiers "tuple" in a space that corresponds
12463 * to that of "set", if any of those identifiers appear as parameters
12464 * in "set", then equate those parameters with the corresponding
12465 * set dimensions and project out the parameters.
12466 * The result therefore has no such parameters.
12468 static __isl_give isl_set
*equate_params(__isl_take isl_set
*set
,
12469 __isl_keep isl_multi_id
*tuple
)
12473 isl_space
*set_space
, *tuple_space
;
12475 set_space
= isl_set_peek_space(set
);
12476 tuple_space
= isl_multi_id_peek_space(tuple
);
12477 if (isl_space_check_equal_tuples(tuple_space
, set_space
) < 0)
12478 return isl_set_free(set
);
12479 n
= isl_multi_id_size(tuple
);
12481 return isl_set_free(set
);
12482 for (i
= 0; i
< n
; ++i
) {
12486 id
= isl_multi_id_get_at(tuple
, i
);
12488 return isl_set_free(set
);
12489 pos
= isl_set_find_dim_by_id(set
, isl_dim_param
, id
);
12493 set
= isl_set_equate(set
, isl_dim_param
, pos
, isl_dim_set
, i
);
12494 set
= isl_set_project_out(set
, isl_dim_param
, pos
, 1);
12499 /* Bind the set dimensions of "set" to parameters with identifiers
12500 * specified by "tuple", living in the same space as "set".
12502 * If no parameters with these identifiers appear in "set" already,
12503 * then the set dimensions are simply reinterpreted as parameters.
12504 * Otherwise, the parameters are first equated to the corresponding
12507 __isl_give isl_set
*isl_set_bind(__isl_take isl_set
*set
,
12508 __isl_take isl_multi_id
*tuple
)
12512 set
= equate_params(set
, tuple
);
12513 space
= isl_set_get_space(set
);
12514 space
= isl_space_bind_set(space
, tuple
);
12515 isl_multi_id_free(tuple
);
12516 set
= isl_set_reset_space(set
, space
);
12521 /* Given a tuple of identifiers "tuple" in a space that corresponds
12522 * to the domain of "map", if any of those identifiers appear as parameters
12523 * in "map", then equate those parameters with the corresponding
12524 * input dimensions and project out the parameters.
12525 * The result therefore has no such parameters.
12527 static __isl_give isl_map
*map_equate_params(__isl_take isl_map
*map
,
12528 __isl_keep isl_multi_id
*tuple
)
12532 isl_space
*map_space
, *tuple_space
;
12534 map_space
= isl_map_peek_space(map
);
12535 tuple_space
= isl_multi_id_peek_space(tuple
);
12536 if (isl_space_check_domain_tuples(tuple_space
, map_space
) < 0)
12537 return isl_map_free(map
);
12538 n
= isl_multi_id_size(tuple
);
12540 return isl_map_free(map
);
12541 for (i
= 0; i
< n
; ++i
) {
12545 id
= isl_multi_id_get_at(tuple
, i
);
12547 return isl_map_free(map
);
12548 pos
= isl_map_find_dim_by_id(map
, isl_dim_param
, id
);
12552 map
= isl_map_equate(map
, isl_dim_param
, pos
, isl_dim_in
, i
);
12553 map
= isl_map_project_out(map
, isl_dim_param
, pos
, 1);
12558 /* Bind the input dimensions of "map" to parameters with identifiers
12559 * specified by "tuple", living in the domain space of "map".
12561 * If no parameters with these identifiers appear in "map" already,
12562 * then the input dimensions are simply reinterpreted as parameters.
12563 * Otherwise, the parameters are first equated to the corresponding
12564 * input dimensions.
12566 __isl_give isl_set
*isl_map_bind_domain(__isl_take isl_map
*map
,
12567 __isl_take isl_multi_id
*tuple
)
12572 map
= map_equate_params(map
, tuple
);
12573 space
= isl_map_get_space(map
);
12574 space
= isl_space_bind_map_domain(space
, tuple
);
12575 isl_multi_id_free(tuple
);
12576 set
= set_from_map(isl_map_reset_space(map
, space
));
12581 /* Bind the output dimensions of "map" to parameters with identifiers
12582 * specified by "tuple", living in the range space of "map".
12584 * Since binding is more easily implemented on the domain,
12585 * bind the input dimensions of the inverse of "map".
12587 __isl_give isl_set
*isl_map_bind_range(__isl_take isl_map
*map
,
12588 __isl_take isl_multi_id
*tuple
)
12590 return isl_map_bind_domain(isl_map_reverse(map
), tuple
);
12593 /* Insert a domain corresponding to "tuple"
12594 * into the nullary or unary relation "set".
12595 * The result has an extra initial tuple and is therefore
12596 * either a unary or binary relation.
12597 * Any parameters with identifiers in "tuple" are reinterpreted
12598 * as the corresponding domain dimensions.
12600 static __isl_give isl_map
*unbind_params_insert_domain(
12601 __isl_take isl_set
*set
, __isl_take isl_multi_id
*tuple
)
12606 space
= isl_set_peek_space(set
);
12607 r
= isl_reordering_unbind_params_insert_domain(space
, tuple
);
12608 isl_multi_id_free(tuple
);
12610 return isl_map_realign(set_to_map(set
), r
);
12613 /* Construct a set with "tuple" as domain from the parameter domain "set".
12614 * Any parameters with identifiers in "tuple" are reinterpreted
12615 * as the corresponding set dimensions.
12617 __isl_give isl_set
*isl_set_unbind_params(__isl_take isl_set
*set
,
12618 __isl_take isl_multi_id
*tuple
)
12620 isl_bool is_params
;
12622 is_params
= isl_set_is_params(set
);
12624 set
= isl_set_free(set
);
12625 else if (!is_params
)
12626 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
12627 "expecting parameter domain", set
= isl_set_free(set
));
12628 return set_from_map(unbind_params_insert_domain(set
, tuple
));
12631 /* Construct a map with "domain" as domain and "set" as range.
12632 * Any parameters with identifiers in "domain" are reinterpreted
12633 * as the corresponding domain dimensions.
12635 __isl_give isl_map
*isl_set_unbind_params_insert_domain(
12636 __isl_take isl_set
*set
, __isl_take isl_multi_id
*domain
)
12638 isl_bool is_params
;
12640 is_params
= isl_set_is_params(set
);
12642 set
= isl_set_free(set
);
12643 else if (is_params
)
12644 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
12645 "expecting proper set", set
= isl_set_free(set
));
12646 return unbind_params_insert_domain(set
, domain
);
12649 __isl_give isl_mat
*isl_basic_map_equalities_matrix(
12650 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
12651 enum isl_dim_type c2
, enum isl_dim_type c3
,
12652 enum isl_dim_type c4
, enum isl_dim_type c5
)
12654 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
12655 struct isl_mat
*mat
;
12660 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
12663 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_eq
, total
+ 1);
12666 for (i
= 0; i
< bmap
->n_eq
; ++i
)
12667 for (j
= 0, pos
= 0; j
< 5; ++j
) {
12668 int off
= isl_basic_map_offset(bmap
, c
[j
]);
12669 isl_size dim
= isl_basic_map_dim(bmap
, c
[j
]);
12671 return isl_mat_free(mat
);
12672 for (k
= 0; k
< dim
; ++k
) {
12673 isl_int_set(mat
->row
[i
][pos
],
12674 bmap
->eq
[i
][off
+ k
]);
12682 __isl_give isl_mat
*isl_basic_map_inequalities_matrix(
12683 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
12684 enum isl_dim_type c2
, enum isl_dim_type c3
,
12685 enum isl_dim_type c4
, enum isl_dim_type c5
)
12687 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
12688 struct isl_mat
*mat
;
12693 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
12696 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_ineq
, total
+ 1);
12699 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
12700 for (j
= 0, pos
= 0; j
< 5; ++j
) {
12701 int off
= isl_basic_map_offset(bmap
, c
[j
]);
12702 isl_size dim
= isl_basic_map_dim(bmap
, c
[j
]);
12704 return isl_mat_free(mat
);
12705 for (k
= 0; k
< dim
; ++k
) {
12706 isl_int_set(mat
->row
[i
][pos
],
12707 bmap
->ineq
[i
][off
+ k
]);
12715 __isl_give isl_basic_map
*isl_basic_map_from_constraint_matrices(
12716 __isl_take isl_space
*space
,
12717 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
12718 enum isl_dim_type c2
, enum isl_dim_type c3
,
12719 enum isl_dim_type c4
, enum isl_dim_type c5
)
12721 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
12722 isl_basic_map
*bmap
= NULL
;
12729 dim
= isl_space_dim(space
, isl_dim_all
);
12730 if (dim
< 0 || !eq
|| !ineq
)
12733 if (eq
->n_col
!= ineq
->n_col
)
12734 isl_die(space
->ctx
, isl_error_invalid
,
12735 "equalities and inequalities matrices should have "
12736 "same number of columns", goto error
);
12740 if (eq
->n_col
< total
)
12741 isl_die(space
->ctx
, isl_error_invalid
,
12742 "number of columns too small", goto error
);
12744 extra
= eq
->n_col
- total
;
12746 bmap
= isl_basic_map_alloc_space(isl_space_copy(space
), extra
,
12747 eq
->n_row
, ineq
->n_row
);
12750 for (i
= 0; i
< extra
; ++i
) {
12751 k
= isl_basic_map_alloc_div(bmap
);
12754 isl_int_set_si(bmap
->div
[k
][0], 0);
12756 for (i
= 0; i
< eq
->n_row
; ++i
) {
12757 l
= isl_basic_map_alloc_equality(bmap
);
12760 for (j
= 0, pos
= 0; j
< 5; ++j
) {
12761 int off
= isl_basic_map_offset(bmap
, c
[j
]);
12762 isl_size dim
= isl_basic_map_dim(bmap
, c
[j
]);
12765 for (k
= 0; k
< dim
; ++k
) {
12766 isl_int_set(bmap
->eq
[l
][off
+ k
],
12772 for (i
= 0; i
< ineq
->n_row
; ++i
) {
12773 l
= isl_basic_map_alloc_inequality(bmap
);
12776 for (j
= 0, pos
= 0; j
< 5; ++j
) {
12777 int off
= isl_basic_map_offset(bmap
, c
[j
]);
12778 isl_size dim
= isl_basic_map_dim(bmap
, c
[j
]);
12781 for (k
= 0; k
< dim
; ++k
) {
12782 isl_int_set(bmap
->ineq
[l
][off
+ k
],
12783 ineq
->row
[i
][pos
]);
12789 isl_space_free(space
);
12791 isl_mat_free(ineq
);
12793 bmap
= isl_basic_map_simplify(bmap
);
12794 return isl_basic_map_finalize(bmap
);
12796 isl_space_free(space
);
12798 isl_mat_free(ineq
);
12799 isl_basic_map_free(bmap
);
12803 __isl_give isl_mat
*isl_basic_set_equalities_matrix(
12804 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
12805 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
12807 return isl_basic_map_equalities_matrix(bset_to_bmap(bset
),
12808 c1
, c2
, c3
, c4
, isl_dim_in
);
12811 __isl_give isl_mat
*isl_basic_set_inequalities_matrix(
12812 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
12813 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
12815 return isl_basic_map_inequalities_matrix(bset_to_bmap(bset
),
12816 c1
, c2
, c3
, c4
, isl_dim_in
);
12819 __isl_give isl_basic_set
*isl_basic_set_from_constraint_matrices(
12820 __isl_take isl_space
*space
,
12821 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
12822 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
12824 isl_basic_map
*bmap
;
12825 bmap
= isl_basic_map_from_constraint_matrices(space
, eq
, ineq
,
12826 c1
, c2
, c3
, c4
, isl_dim_in
);
12827 return bset_from_bmap(bmap
);
12830 isl_bool
isl_basic_map_can_zip(__isl_keep isl_basic_map
*bmap
)
12833 return isl_bool_error
;
12835 return isl_space_can_zip(bmap
->dim
);
12838 isl_bool
isl_map_can_zip(__isl_keep isl_map
*map
)
12841 return isl_bool_error
;
12843 return isl_space_can_zip(map
->dim
);
12846 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
12847 * (A -> C) -> (B -> D).
12849 __isl_give isl_basic_map
*isl_basic_map_zip(__isl_take isl_basic_map
*bmap
)
12859 if (!isl_basic_map_can_zip(bmap
))
12860 isl_die(bmap
->ctx
, isl_error_invalid
,
12861 "basic map cannot be zipped", goto error
);
12862 n_in
= isl_space_dim(bmap
->dim
->nested
[0], isl_dim_in
);
12863 n1
= isl_space_dim(bmap
->dim
->nested
[0], isl_dim_out
);
12864 n2
= isl_space_dim(bmap
->dim
->nested
[1], isl_dim_in
);
12865 if (n_in
< 0 || n1
< 0 || n2
< 0)
12866 return isl_basic_map_free(bmap
);
12867 pos
= isl_basic_map_offset(bmap
, isl_dim_in
) + n_in
;
12868 bmap
= isl_basic_map_cow(bmap
);
12869 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
12872 bmap
->dim
= isl_space_zip(bmap
->dim
);
12875 bmap
= isl_basic_map_mark_final(bmap
);
12878 isl_basic_map_free(bmap
);
12882 /* Given a map (A -> B) -> (C -> D), return the corresponding map
12883 * (A -> C) -> (B -> D).
12885 __isl_give isl_map
*isl_map_zip(__isl_take isl_map
*map
)
12890 if (!isl_map_can_zip(map
))
12891 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be zipped",
12894 return isl_map_transform(map
, &isl_space_zip
, &isl_basic_map_zip
);
12900 /* Can we apply isl_basic_map_curry to "bmap"?
12901 * That is, does it have a nested relation in its domain?
12903 isl_bool
isl_basic_map_can_curry(__isl_keep isl_basic_map
*bmap
)
12906 return isl_bool_error
;
12908 return isl_space_can_curry(bmap
->dim
);
12911 /* Can we apply isl_map_curry to "map"?
12912 * That is, does it have a nested relation in its domain?
12914 isl_bool
isl_map_can_curry(__isl_keep isl_map
*map
)
12917 return isl_bool_error
;
12919 return isl_space_can_curry(map
->dim
);
12922 /* Given a basic map (A -> B) -> C, return the corresponding basic map
12925 __isl_give isl_basic_map
*isl_basic_map_curry(__isl_take isl_basic_map
*bmap
)
12931 if (!isl_basic_map_can_curry(bmap
))
12932 isl_die(bmap
->ctx
, isl_error_invalid
,
12933 "basic map cannot be curried", goto error
);
12934 bmap
= isl_basic_map_cow(bmap
);
12937 bmap
->dim
= isl_space_curry(bmap
->dim
);
12940 bmap
= isl_basic_map_mark_final(bmap
);
12943 isl_basic_map_free(bmap
);
12947 /* Given a map (A -> B) -> C, return the corresponding map
12950 __isl_give isl_map
*isl_map_curry(__isl_take isl_map
*map
)
12952 return isl_map_change_space(map
, &isl_map_can_curry
,
12953 "map cannot be curried", &isl_space_curry
);
12956 /* Can isl_map_range_curry be applied to "map"?
12957 * That is, does it have a nested relation in its range,
12958 * the domain of which is itself a nested relation?
12960 isl_bool
isl_map_can_range_curry(__isl_keep isl_map
*map
)
12963 return isl_bool_error
;
12965 return isl_space_can_range_curry(map
->dim
);
12968 /* Given a map A -> ((B -> C) -> D), return the corresponding map
12969 * A -> (B -> (C -> D)).
12971 __isl_give isl_map
*isl_map_range_curry(__isl_take isl_map
*map
)
12973 return isl_map_change_space(map
, &isl_map_can_range_curry
,
12974 "map range cannot be curried",
12975 &isl_space_range_curry
);
12978 /* Can we apply isl_basic_map_uncurry to "bmap"?
12979 * That is, does it have a nested relation in its domain?
12981 isl_bool
isl_basic_map_can_uncurry(__isl_keep isl_basic_map
*bmap
)
12984 return isl_bool_error
;
12986 return isl_space_can_uncurry(bmap
->dim
);
12989 /* Can we apply isl_map_uncurry to "map"?
12990 * That is, does it have a nested relation in its domain?
12992 isl_bool
isl_map_can_uncurry(__isl_keep isl_map
*map
)
12995 return isl_bool_error
;
12997 return isl_space_can_uncurry(map
->dim
);
13000 /* Given a basic map A -> (B -> C), return the corresponding basic map
13003 __isl_give isl_basic_map
*isl_basic_map_uncurry(__isl_take isl_basic_map
*bmap
)
13009 if (!isl_basic_map_can_uncurry(bmap
))
13010 isl_die(bmap
->ctx
, isl_error_invalid
,
13011 "basic map cannot be uncurried",
13012 return isl_basic_map_free(bmap
));
13013 bmap
= isl_basic_map_cow(bmap
);
13016 bmap
->dim
= isl_space_uncurry(bmap
->dim
);
13018 return isl_basic_map_free(bmap
);
13019 bmap
= isl_basic_map_mark_final(bmap
);
13023 /* Given a map A -> (B -> C), return the corresponding map
13026 __isl_give isl_map
*isl_map_uncurry(__isl_take isl_map
*map
)
13028 return isl_map_change_space(map
, &isl_map_can_uncurry
,
13029 "map cannot be uncurried", &isl_space_uncurry
);
13032 __isl_give isl_set
*isl_set_equate(__isl_take isl_set
*set
,
13033 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13035 return isl_map_equate(set
, type1
, pos1
, type2
, pos2
);
13038 /* Construct a basic map where the given dimensions are equal to each other.
13040 static __isl_give isl_basic_map
*equator(__isl_take isl_space
*space
,
13041 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13043 isl_basic_map
*bmap
= NULL
;
13047 total
= isl_space_dim(space
, isl_dim_all
);
13049 isl_space_check_range(space
, type1
, pos1
, 1) < 0 ||
13050 isl_space_check_range(space
, type2
, pos2
, 1) < 0)
13053 if (type1
== type2
&& pos1
== pos2
)
13054 return isl_basic_map_universe(space
);
13056 bmap
= isl_basic_map_alloc_space(isl_space_copy(space
), 0, 1, 0);
13057 i
= isl_basic_map_alloc_equality(bmap
);
13060 isl_seq_clr(bmap
->eq
[i
], 1 + total
);
13061 pos1
+= isl_basic_map_offset(bmap
, type1
);
13062 pos2
+= isl_basic_map_offset(bmap
, type2
);
13063 isl_int_set_si(bmap
->eq
[i
][pos1
], -1);
13064 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
13065 bmap
= isl_basic_map_finalize(bmap
);
13066 isl_space_free(space
);
13069 isl_space_free(space
);
13070 isl_basic_map_free(bmap
);
13074 /* Add a constraint imposing that the given two dimensions are equal.
13076 __isl_give isl_basic_map
*isl_basic_map_equate(__isl_take isl_basic_map
*bmap
,
13077 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13081 eq
= equator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
13083 bmap
= isl_basic_map_intersect(bmap
, eq
);
13088 /* Add a constraint imposing that the given two dimensions are equal.
13090 __isl_give isl_map
*isl_map_equate(__isl_take isl_map
*map
,
13091 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13093 isl_basic_map
*bmap
;
13095 bmap
= equator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
13097 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
13102 /* Add a constraint imposing that the given two dimensions have opposite values.
13104 __isl_give isl_map
*isl_map_oppose(__isl_take isl_map
*map
,
13105 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13107 isl_basic_map
*bmap
= NULL
;
13111 if (isl_map_check_range(map
, type1
, pos1
, 1) < 0)
13112 return isl_map_free(map
);
13113 if (isl_map_check_range(map
, type2
, pos2
, 1) < 0)
13114 return isl_map_free(map
);
13116 total
= isl_map_dim(map
, isl_dim_all
);
13118 return isl_map_free(map
);
13119 bmap
= isl_basic_map_alloc_space(isl_map_get_space(map
), 0, 1, 0);
13120 i
= isl_basic_map_alloc_equality(bmap
);
13123 isl_seq_clr(bmap
->eq
[i
], 1 + total
);
13124 pos1
+= isl_basic_map_offset(bmap
, type1
);
13125 pos2
+= isl_basic_map_offset(bmap
, type2
);
13126 isl_int_set_si(bmap
->eq
[i
][pos1
], 1);
13127 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
13128 bmap
= isl_basic_map_finalize(bmap
);
13130 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
13134 isl_basic_map_free(bmap
);
13139 /* Construct a constraint imposing that the value of the first dimension is
13140 * greater than or equal to that of the second.
13142 static __isl_give isl_constraint
*constraint_order_ge(
13143 __isl_take isl_space
*space
, enum isl_dim_type type1
, int pos1
,
13144 enum isl_dim_type type2
, int pos2
)
13148 if (isl_space_check_range(space
, type1
, pos1
, 1) < 0 ||
13149 isl_space_check_range(space
, type2
, pos2
, 1) < 0)
13150 space
= isl_space_free(space
);
13154 c
= isl_constraint_alloc_inequality(isl_local_space_from_space(space
));
13156 if (type1
== type2
&& pos1
== pos2
)
13159 c
= isl_constraint_set_coefficient_si(c
, type1
, pos1
, 1);
13160 c
= isl_constraint_set_coefficient_si(c
, type2
, pos2
, -1);
13165 /* Add a constraint imposing that the value of the first dimension is
13166 * greater than or equal to that of the second.
13168 __isl_give isl_basic_map
*isl_basic_map_order_ge(__isl_take isl_basic_map
*bmap
,
13169 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13174 if (type1
== type2
&& pos1
== pos2
)
13176 space
= isl_basic_map_get_space(bmap
);
13177 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
13178 bmap
= isl_basic_map_add_constraint(bmap
, c
);
13183 /* Add a constraint imposing that the value of the first dimension is
13184 * greater than or equal to that of the second.
13186 __isl_give isl_map
*isl_map_order_ge(__isl_take isl_map
*map
,
13187 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13192 if (type1
== type2
&& pos1
== pos2
)
13194 space
= isl_map_get_space(map
);
13195 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
13196 map
= isl_map_add_constraint(map
, c
);
13201 /* Add a constraint imposing that the value of the first dimension is
13202 * less than or equal to that of the second.
13204 __isl_give isl_map
*isl_map_order_le(__isl_take isl_map
*map
,
13205 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13207 return isl_map_order_ge(map
, type2
, pos2
, type1
, pos1
);
13210 /* Construct a basic map where the value of the first dimension is
13211 * greater than that of the second.
13213 static __isl_give isl_basic_map
*greator(__isl_take isl_space
*space
,
13214 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13216 isl_basic_map
*bmap
= NULL
;
13220 if (isl_space_check_range(space
, type1
, pos1
, 1) < 0 ||
13221 isl_space_check_range(space
, type2
, pos2
, 1) < 0)
13224 if (type1
== type2
&& pos1
== pos2
)
13225 return isl_basic_map_empty(space
);
13227 bmap
= isl_basic_map_alloc_space(space
, 0, 0, 1);
13228 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
13229 i
= isl_basic_map_alloc_inequality(bmap
);
13230 if (total
< 0 || i
< 0)
13231 return isl_basic_map_free(bmap
);
13232 isl_seq_clr(bmap
->ineq
[i
], 1 + total
);
13233 pos1
+= isl_basic_map_offset(bmap
, type1
);
13234 pos2
+= isl_basic_map_offset(bmap
, type2
);
13235 isl_int_set_si(bmap
->ineq
[i
][pos1
], 1);
13236 isl_int_set_si(bmap
->ineq
[i
][pos2
], -1);
13237 isl_int_set_si(bmap
->ineq
[i
][0], -1);
13238 bmap
= isl_basic_map_finalize(bmap
);
13242 isl_space_free(space
);
13243 isl_basic_map_free(bmap
);
13247 /* Add a constraint imposing that the value of the first dimension is
13248 * greater than that of the second.
13250 __isl_give isl_basic_map
*isl_basic_map_order_gt(__isl_take isl_basic_map
*bmap
,
13251 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13255 gt
= greator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
13257 bmap
= isl_basic_map_intersect(bmap
, gt
);
13262 /* Add a constraint imposing that the value of the first dimension is
13263 * greater than that of the second.
13265 __isl_give isl_map
*isl_map_order_gt(__isl_take isl_map
*map
,
13266 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13268 isl_basic_map
*bmap
;
13270 bmap
= greator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
13272 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
13277 /* Add a constraint imposing that the value of the first dimension is
13278 * smaller than that of the second.
13280 __isl_give isl_map
*isl_map_order_lt(__isl_take isl_map
*map
,
13281 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13283 return isl_map_order_gt(map
, type2
, pos2
, type1
, pos1
);
13286 __isl_give isl_aff
*isl_basic_map_get_div(__isl_keep isl_basic_map
*bmap
,
13290 isl_local_space
*ls
;
13295 if (!isl_basic_map_divs_known(bmap
))
13296 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
13297 "some divs are unknown", return NULL
);
13299 ls
= isl_basic_map_get_local_space(bmap
);
13300 div
= isl_local_space_get_div(ls
, pos
);
13301 isl_local_space_free(ls
);
13306 __isl_give isl_aff
*isl_basic_set_get_div(__isl_keep isl_basic_set
*bset
,
13309 return isl_basic_map_get_div(bset
, pos
);
13312 /* Plug in "subs" for dimension "type", "pos" of "bset".
13314 * Let i be the dimension to replace and let "subs" be of the form
13318 * Any integer division with a non-zero coefficient for i,
13320 * floor((a i + g)/m)
13324 * floor((a f + d g)/(m d))
13326 * Constraints of the form
13334 * We currently require that "subs" is an integral expression.
13335 * Handling rational expressions may require us to add stride constraints
13336 * as we do in isl_basic_set_preimage_multi_aff.
13338 __isl_give isl_basic_set
*isl_basic_set_substitute(
13339 __isl_take isl_basic_set
*bset
,
13340 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
13347 if (bset
&& isl_basic_set_plain_is_empty(bset
))
13350 bset
= isl_basic_set_cow(bset
);
13351 if (!bset
|| !subs
)
13354 ctx
= isl_basic_set_get_ctx(bset
);
13355 if (!isl_space_is_equal(bset
->dim
, subs
->ls
->dim
))
13356 isl_die(ctx
, isl_error_invalid
,
13357 "spaces don't match", goto error
);
13358 n_div
= isl_local_space_dim(subs
->ls
, isl_dim_div
);
13362 isl_die(ctx
, isl_error_unsupported
,
13363 "cannot handle divs yet", goto error
);
13364 if (!isl_int_is_one(subs
->v
->el
[0]))
13365 isl_die(ctx
, isl_error_invalid
,
13366 "can only substitute integer expressions", goto error
);
13368 pos
+= isl_basic_set_offset(bset
, type
);
13372 for (i
= 0; i
< bset
->n_eq
; ++i
) {
13373 if (isl_int_is_zero(bset
->eq
[i
][pos
]))
13375 isl_int_set(v
, bset
->eq
[i
][pos
]);
13376 isl_int_set_si(bset
->eq
[i
][pos
], 0);
13377 isl_seq_combine(bset
->eq
[i
], subs
->v
->el
[0], bset
->eq
[i
],
13378 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
13381 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
13382 if (isl_int_is_zero(bset
->ineq
[i
][pos
]))
13384 isl_int_set(v
, bset
->ineq
[i
][pos
]);
13385 isl_int_set_si(bset
->ineq
[i
][pos
], 0);
13386 isl_seq_combine(bset
->ineq
[i
], subs
->v
->el
[0], bset
->ineq
[i
],
13387 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
13390 for (i
= 0; i
< bset
->n_div
; ++i
) {
13391 if (isl_int_is_zero(bset
->div
[i
][1 + pos
]))
13393 isl_int_set(v
, bset
->div
[i
][1 + pos
]);
13394 isl_int_set_si(bset
->div
[i
][1 + pos
], 0);
13395 isl_seq_combine(bset
->div
[i
] + 1,
13396 subs
->v
->el
[0], bset
->div
[i
] + 1,
13397 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
13398 isl_int_mul(bset
->div
[i
][0], bset
->div
[i
][0], subs
->v
->el
[0]);
13403 bset
= isl_basic_set_simplify(bset
);
13404 return isl_basic_set_finalize(bset
);
13406 isl_basic_set_free(bset
);
13410 /* Plug in "subs" for dimension "type", "pos" of "set".
13412 __isl_give isl_set
*isl_set_substitute(__isl_take isl_set
*set
,
13413 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
13417 if (set
&& isl_set_plain_is_empty(set
))
13420 set
= isl_set_cow(set
);
13424 for (i
= set
->n
- 1; i
>= 0; --i
) {
13425 set
->p
[i
] = isl_basic_set_substitute(set
->p
[i
], type
, pos
, subs
);
13426 set
= set_from_map(remove_if_empty(set_to_map(set
), i
));
13437 /* Check if the range of "ma" is compatible with the domain or range
13438 * (depending on "type") of "bmap".
13440 static isl_stat
check_basic_map_compatible_range_multi_aff(
13441 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type type
,
13442 __isl_keep isl_multi_aff
*ma
)
13445 isl_space
*ma_space
;
13447 ma_space
= isl_multi_aff_get_space(ma
);
13449 m
= isl_space_has_equal_params(bmap
->dim
, ma_space
);
13453 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
13454 "parameters don't match", goto error
);
13455 m
= isl_space_tuple_is_equal(bmap
->dim
, type
, ma_space
, isl_dim_out
);
13459 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
13460 "spaces don't match", goto error
);
13462 isl_space_free(ma_space
);
13463 return isl_stat_ok
;
13465 isl_space_free(ma_space
);
13466 return isl_stat_error
;
13469 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
13470 * coefficients before the transformed range of dimensions,
13471 * the "n_after" coefficients after the transformed range of dimensions
13472 * and the coefficients of the other divs in "bmap".
13474 static __isl_give isl_basic_map
*set_ma_divs(__isl_take isl_basic_map
*bmap
,
13475 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
, int n_div
)
13480 isl_local_space
*ls
;
13485 ls
= isl_aff_get_domain_local_space(ma
->u
.p
[0]);
13486 n_param
= isl_local_space_dim(ls
, isl_dim_param
);
13487 n_set
= isl_local_space_dim(ls
, isl_dim_set
);
13488 if (n_param
< 0 || n_set
< 0)
13489 return isl_basic_map_free(bmap
);
13491 for (i
= 0; i
< n_div
; ++i
) {
13492 int o_bmap
= 0, o_ls
= 0;
13494 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], 1 + 1 + n_param
);
13495 o_bmap
+= 1 + 1 + n_param
;
13496 o_ls
+= 1 + 1 + n_param
;
13497 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_before
);
13498 o_bmap
+= n_before
;
13499 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
13500 ls
->div
->row
[i
] + o_ls
, n_set
);
13503 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_after
);
13505 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
13506 ls
->div
->row
[i
] + o_ls
, n_div
);
13509 isl_seq_clr(bmap
->div
[i
] + o_bmap
, bmap
->n_div
- n_div
);
13510 bmap
= isl_basic_map_add_div_constraints(bmap
, i
);
13515 isl_local_space_free(ls
);
13518 isl_local_space_free(ls
);
13519 return isl_basic_map_free(bmap
);
13522 /* How many stride constraints does "ma" enforce?
13523 * That is, how many of the affine expressions have a denominator
13524 * different from one?
13526 static int multi_aff_strides(__isl_keep isl_multi_aff
*ma
)
13531 for (i
= 0; i
< ma
->n
; ++i
)
13532 if (!isl_int_is_one(ma
->u
.p
[i
]->v
->el
[0]))
13538 /* For each affine expression in ma of the form
13540 * x_i = (f_i y + h_i)/m_i
13542 * with m_i different from one, add a constraint to "bmap"
13545 * f_i y + h_i = m_i alpha_i
13547 * with alpha_i an additional existentially quantified variable.
13549 * The input variables of "ma" correspond to a subset of the variables
13550 * of "bmap". There are "n_before" variables in "bmap" before this
13551 * subset and "n_after" variables after this subset.
13552 * The integer divisions of the affine expressions in "ma" are assumed
13553 * to have been aligned. There are "n_div_ma" of them and
13554 * they appear first in "bmap", straight after the "n_after" variables.
13556 static __isl_give isl_basic_map
*add_ma_strides(
13557 __isl_take isl_basic_map
*bmap
, __isl_keep isl_multi_aff
*ma
,
13558 int n_before
, int n_after
, int n_div_ma
)
13566 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
13567 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
13568 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
13569 if (total
< 0 || n_param
< 0 || n_in
< 0)
13570 return isl_basic_map_free(bmap
);
13571 for (i
= 0; i
< ma
->n
; ++i
) {
13572 int o_bmap
= 0, o_ma
= 1;
13574 if (isl_int_is_one(ma
->u
.p
[i
]->v
->el
[0]))
13576 div
= isl_basic_map_alloc_div(bmap
);
13577 k
= isl_basic_map_alloc_equality(bmap
);
13578 if (div
< 0 || k
< 0)
13580 isl_int_set_si(bmap
->div
[div
][0], 0);
13581 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
13582 ma
->u
.p
[i
]->v
->el
+ o_ma
, 1 + n_param
);
13583 o_bmap
+= 1 + n_param
;
13584 o_ma
+= 1 + n_param
;
13585 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_before
);
13586 o_bmap
+= n_before
;
13587 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
13588 ma
->u
.p
[i
]->v
->el
+ o_ma
, n_in
);
13591 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_after
);
13593 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
13594 ma
->u
.p
[i
]->v
->el
+ o_ma
, n_div_ma
);
13595 o_bmap
+= n_div_ma
;
13597 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, 1 + total
- o_bmap
);
13598 isl_int_neg(bmap
->eq
[k
][1 + total
], ma
->u
.p
[i
]->v
->el
[0]);
13604 isl_basic_map_free(bmap
);
13608 /* Replace the domain or range space (depending on "type) of "space" by "set".
13610 static __isl_give isl_space
*isl_space_set(__isl_take isl_space
*space
,
13611 enum isl_dim_type type
, __isl_take isl_space
*set
)
13613 if (type
== isl_dim_in
) {
13614 space
= isl_space_range(space
);
13615 space
= isl_space_map_from_domain_and_range(set
, space
);
13617 space
= isl_space_domain(space
);
13618 space
= isl_space_map_from_domain_and_range(space
, set
);
13624 /* Compute the preimage of the domain or range (depending on "type")
13625 * of "bmap" under the function represented by "ma".
13626 * In other words, plug in "ma" in the domain or range of "bmap".
13627 * The result is a basic map that lives in the same space as "bmap"
13628 * except that the domain or range has been replaced by
13629 * the domain space of "ma".
13631 * If bmap is represented by
13633 * A(p) + S u + B x + T v + C(divs) >= 0,
13635 * where u and x are input and output dimensions if type == isl_dim_out
13636 * while x and v are input and output dimensions if type == isl_dim_in,
13637 * and ma is represented by
13639 * x = D(p) + F(y) + G(divs')
13641 * then the result is
13643 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
13645 * The divs in the input set are similarly adjusted.
13648 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
13652 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
13653 * B_i G(divs') + c_i(divs))/n_i)
13655 * If bmap is not a rational map and if F(y) involves any denominators
13657 * x_i = (f_i y + h_i)/m_i
13659 * then additional constraints are added to ensure that we only
13660 * map back integer points. That is we enforce
13662 * f_i y + h_i = m_i alpha_i
13664 * with alpha_i an additional existentially quantified variable.
13666 * We first copy over the divs from "ma".
13667 * Then we add the modified constraints and divs from "bmap".
13668 * Finally, we add the stride constraints, if needed.
13670 __isl_give isl_basic_map
*isl_basic_map_preimage_multi_aff(
13671 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
13672 __isl_take isl_multi_aff
*ma
)
13676 isl_basic_map
*res
= NULL
;
13677 isl_size n_before
, n_after
, n_div_bmap
, n_div_ma
;
13678 isl_int f
, c1
, c2
, g
;
13687 ma
= isl_multi_aff_align_divs(ma
);
13690 if (check_basic_map_compatible_range_multi_aff(bmap
, type
, ma
) < 0)
13693 if (type
== isl_dim_in
) {
13695 n_after
= isl_basic_map_dim(bmap
, isl_dim_out
);
13697 n_before
= isl_basic_map_dim(bmap
, isl_dim_in
);
13700 n_div_bmap
= isl_basic_map_dim(bmap
, isl_dim_div
);
13701 n_div_ma
= ma
->n
? isl_aff_dim(ma
->u
.p
[0], isl_dim_div
) : 0;
13702 if (n_before
< 0 || n_after
< 0 || n_div_bmap
< 0 || n_div_ma
< 0)
13705 space
= isl_multi_aff_get_domain_space(ma
);
13706 space
= isl_space_set(isl_basic_map_get_space(bmap
), type
, space
);
13707 rational
= isl_basic_map_is_rational(bmap
);
13708 strides
= rational
? 0 : multi_aff_strides(ma
);
13709 res
= isl_basic_map_alloc_space(space
, n_div_ma
+ n_div_bmap
+ strides
,
13710 bmap
->n_eq
+ strides
, bmap
->n_ineq
+ 2 * n_div_ma
);
13712 res
= isl_basic_map_set_rational(res
);
13714 for (i
= 0; i
< n_div_ma
+ n_div_bmap
; ++i
)
13715 if (isl_basic_map_alloc_div(res
) < 0)
13718 res
= set_ma_divs(res
, ma
, n_before
, n_after
, n_div_ma
);
13722 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
13723 k
= isl_basic_map_alloc_equality(res
);
13726 if (isl_seq_preimage(res
->eq
[k
], bmap
->eq
[i
], ma
, n_before
,
13727 n_after
, n_div_ma
, n_div_bmap
,
13728 f
, c1
, c2
, g
, 0) < 0)
13732 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
13733 k
= isl_basic_map_alloc_inequality(res
);
13736 if (isl_seq_preimage(res
->ineq
[k
], bmap
->ineq
[i
], ma
, n_before
,
13737 n_after
, n_div_ma
, n_div_bmap
,
13738 f
, c1
, c2
, g
, 0) < 0)
13742 for (i
= 0; i
< bmap
->n_div
; ++i
) {
13743 if (isl_int_is_zero(bmap
->div
[i
][0])) {
13744 isl_int_set_si(res
->div
[n_div_ma
+ i
][0], 0);
13747 if (isl_seq_preimage(res
->div
[n_div_ma
+ i
], bmap
->div
[i
], ma
,
13748 n_before
, n_after
, n_div_ma
, n_div_bmap
,
13749 f
, c1
, c2
, g
, 1) < 0)
13754 res
= add_ma_strides(res
, ma
, n_before
, n_after
, n_div_ma
);
13760 isl_basic_map_free(bmap
);
13761 isl_multi_aff_free(ma
);
13762 res
= isl_basic_map_simplify(res
);
13763 return isl_basic_map_finalize(res
);
13769 isl_basic_map_free(bmap
);
13770 isl_multi_aff_free(ma
);
13771 isl_basic_map_free(res
);
13775 /* Compute the preimage of "bset" under the function represented by "ma".
13776 * In other words, plug in "ma" in "bset". The result is a basic set
13777 * that lives in the domain space of "ma".
13779 __isl_give isl_basic_set
*isl_basic_set_preimage_multi_aff(
13780 __isl_take isl_basic_set
*bset
, __isl_take isl_multi_aff
*ma
)
13782 return isl_basic_map_preimage_multi_aff(bset
, isl_dim_set
, ma
);
13785 /* Compute the preimage of the domain of "bmap" under the function
13786 * represented by "ma".
13787 * In other words, plug in "ma" in the domain of "bmap".
13788 * The result is a basic map that lives in the same space as "bmap"
13789 * except that the domain has been replaced by the domain space of "ma".
13791 __isl_give isl_basic_map
*isl_basic_map_preimage_domain_multi_aff(
13792 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
13794 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_in
, ma
);
13797 /* Compute the preimage of the range of "bmap" under the function
13798 * represented by "ma".
13799 * In other words, plug in "ma" in the range of "bmap".
13800 * The result is a basic map that lives in the same space as "bmap"
13801 * except that the range has been replaced by the domain space of "ma".
13803 __isl_give isl_basic_map
*isl_basic_map_preimage_range_multi_aff(
13804 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
13806 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_out
, ma
);
13809 /* Check if the range of "ma" is compatible with the domain or range
13810 * (depending on "type") of "map".
13811 * Return isl_stat_error if anything is wrong.
13813 static isl_stat
check_map_compatible_range_multi_aff(
13814 __isl_keep isl_map
*map
, enum isl_dim_type type
,
13815 __isl_keep isl_multi_aff
*ma
)
13818 isl_space
*ma_space
;
13820 ma_space
= isl_multi_aff_get_space(ma
);
13821 m
= isl_map_space_tuple_is_equal(map
, type
, ma_space
, isl_dim_out
);
13822 isl_space_free(ma_space
);
13824 return isl_stat_error
;
13826 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
13827 "spaces don't match", return isl_stat_error
);
13828 return isl_stat_ok
;
13831 /* Compute the preimage of the domain or range (depending on "type")
13832 * of "map" under the function represented by "ma".
13833 * In other words, plug in "ma" in the domain or range of "map".
13834 * The result is a map that lives in the same space as "map"
13835 * except that the domain or range has been replaced by
13836 * the domain space of "ma".
13838 * The parameters are assumed to have been aligned.
13840 static __isl_give isl_map
*map_preimage_multi_aff(__isl_take isl_map
*map
,
13841 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
13846 map
= isl_map_cow(map
);
13847 ma
= isl_multi_aff_align_divs(ma
);
13850 if (check_map_compatible_range_multi_aff(map
, type
, ma
) < 0)
13853 for (i
= 0; i
< map
->n
; ++i
) {
13854 map
->p
[i
] = isl_basic_map_preimage_multi_aff(map
->p
[i
], type
,
13855 isl_multi_aff_copy(ma
));
13860 space
= isl_multi_aff_get_domain_space(ma
);
13861 space
= isl_space_set(isl_map_get_space(map
), type
, space
);
13863 isl_space_free(isl_map_take_space(map
));
13864 map
= isl_map_restore_space(map
, space
);
13868 isl_multi_aff_free(ma
);
13870 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
13871 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
13874 isl_multi_aff_free(ma
);
13879 /* Compute the preimage of the domain or range (depending on "type")
13880 * of "map" under the function represented by "ma".
13881 * In other words, plug in "ma" in the domain or range of "map".
13882 * The result is a map that lives in the same space as "map"
13883 * except that the domain or range has been replaced by
13884 * the domain space of "ma".
13886 __isl_give isl_map
*isl_map_preimage_multi_aff(__isl_take isl_map
*map
,
13887 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
13894 aligned
= isl_map_space_has_equal_params(map
, ma
->space
);
13898 return map_preimage_multi_aff(map
, type
, ma
);
13900 if (isl_map_check_named_params(map
) < 0)
13902 if (!isl_space_has_named_params(ma
->space
))
13903 isl_die(map
->ctx
, isl_error_invalid
,
13904 "unaligned unnamed parameters", goto error
);
13905 map
= isl_map_align_params(map
, isl_multi_aff_get_space(ma
));
13906 ma
= isl_multi_aff_align_params(ma
, isl_map_get_space(map
));
13908 return map_preimage_multi_aff(map
, type
, ma
);
13910 isl_multi_aff_free(ma
);
13911 return isl_map_free(map
);
13914 /* Compute the preimage of "set" under the function represented by "ma".
13915 * In other words, plug in "ma" in "set". The result is a set
13916 * that lives in the domain space of "ma".
13918 __isl_give isl_set
*isl_set_preimage_multi_aff(__isl_take isl_set
*set
,
13919 __isl_take isl_multi_aff
*ma
)
13921 return isl_map_preimage_multi_aff(set
, isl_dim_set
, ma
);
13924 /* Compute the preimage of the domain of "map" under the function
13925 * represented by "ma".
13926 * In other words, plug in "ma" in the domain of "map".
13927 * The result is a map that lives in the same space as "map"
13928 * except that the domain has been replaced by the domain space of "ma".
13930 __isl_give isl_map
*isl_map_preimage_domain_multi_aff(__isl_take isl_map
*map
,
13931 __isl_take isl_multi_aff
*ma
)
13933 return isl_map_preimage_multi_aff(map
, isl_dim_in
, ma
);
13936 /* Compute the preimage of the range of "map" under the function
13937 * represented by "ma".
13938 * In other words, plug in "ma" in the range of "map".
13939 * The result is a map that lives in the same space as "map"
13940 * except that the range has been replaced by the domain space of "ma".
13942 __isl_give isl_map
*isl_map_preimage_range_multi_aff(__isl_take isl_map
*map
,
13943 __isl_take isl_multi_aff
*ma
)
13945 return isl_map_preimage_multi_aff(map
, isl_dim_out
, ma
);
13948 /* Compute the preimage of "map" under the function represented by "pma".
13949 * In other words, plug in "pma" in the domain or range of "map".
13950 * The result is a map that lives in the same space as "map",
13951 * except that the space of type "type" has been replaced by
13952 * the domain space of "pma".
13954 * The parameters of "map" and "pma" are assumed to have been aligned.
13956 static __isl_give isl_map
*isl_map_preimage_pw_multi_aff_aligned(
13957 __isl_take isl_map
*map
, enum isl_dim_type type
,
13958 __isl_take isl_pw_multi_aff
*pma
)
13967 isl_pw_multi_aff_free(pma
);
13968 res
= isl_map_empty(isl_map_get_space(map
));
13973 res
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
13974 isl_multi_aff_copy(pma
->p
[0].maff
));
13975 if (type
== isl_dim_in
)
13976 res
= isl_map_intersect_domain(res
,
13977 isl_map_copy(pma
->p
[0].set
));
13979 res
= isl_map_intersect_range(res
,
13980 isl_map_copy(pma
->p
[0].set
));
13982 for (i
= 1; i
< pma
->n
; ++i
) {
13985 res_i
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
13986 isl_multi_aff_copy(pma
->p
[i
].maff
));
13987 if (type
== isl_dim_in
)
13988 res_i
= isl_map_intersect_domain(res_i
,
13989 isl_map_copy(pma
->p
[i
].set
));
13991 res_i
= isl_map_intersect_range(res_i
,
13992 isl_map_copy(pma
->p
[i
].set
));
13993 res
= isl_map_union(res
, res_i
);
13996 isl_pw_multi_aff_free(pma
);
14000 isl_pw_multi_aff_free(pma
);
14005 /* Compute the preimage of "map" under the function represented by "pma".
14006 * In other words, plug in "pma" in the domain or range of "map".
14007 * The result is a map that lives in the same space as "map",
14008 * except that the space of type "type" has been replaced by
14009 * the domain space of "pma".
14011 __isl_give isl_map
*isl_map_preimage_pw_multi_aff(__isl_take isl_map
*map
,
14012 enum isl_dim_type type
, __isl_take isl_pw_multi_aff
*pma
)
14019 aligned
= isl_map_space_has_equal_params(map
, pma
->dim
);
14023 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
14025 if (isl_map_check_named_params(map
) < 0)
14027 if (isl_pw_multi_aff_check_named_params(pma
) < 0)
14029 map
= isl_map_align_params(map
, isl_pw_multi_aff_get_space(pma
));
14030 pma
= isl_pw_multi_aff_align_params(pma
, isl_map_get_space(map
));
14032 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
14034 isl_pw_multi_aff_free(pma
);
14035 return isl_map_free(map
);
14038 /* Compute the preimage of "set" under the function represented by "pma".
14039 * In other words, plug in "pma" in "set". The result is a set
14040 * that lives in the domain space of "pma".
14042 __isl_give isl_set
*isl_set_preimage_pw_multi_aff(__isl_take isl_set
*set
,
14043 __isl_take isl_pw_multi_aff
*pma
)
14045 return isl_map_preimage_pw_multi_aff(set
, isl_dim_set
, pma
);
14048 /* Compute the preimage of the domain of "map" under the function
14049 * represented by "pma".
14050 * In other words, plug in "pma" in the domain of "map".
14051 * The result is a map that lives in the same space as "map",
14052 * except that domain space has been replaced by the domain space of "pma".
14054 __isl_give isl_map
*isl_map_preimage_domain_pw_multi_aff(
14055 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
14057 return isl_map_preimage_pw_multi_aff(map
, isl_dim_in
, pma
);
14060 /* Compute the preimage of the range of "map" under the function
14061 * represented by "pma".
14062 * In other words, plug in "pma" in the range of "map".
14063 * The result is a map that lives in the same space as "map",
14064 * except that range space has been replaced by the domain space of "pma".
14066 __isl_give isl_map
*isl_map_preimage_range_pw_multi_aff(
14067 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
14069 return isl_map_preimage_pw_multi_aff(map
, isl_dim_out
, pma
);
14072 /* Compute the preimage of "map" under the function represented by "mpa".
14073 * In other words, plug in "mpa" in the domain or range of "map".
14074 * The result is a map that lives in the same space as "map",
14075 * except that the space of type "type" has been replaced by
14076 * the domain space of "mpa".
14078 * If the map does not involve any constraints that refer to the
14079 * dimensions of the substituted space, then the only possible
14080 * effect of "mpa" on the map is to map the space to a different space.
14081 * We create a separate isl_multi_aff to effectuate this change
14082 * in order to avoid spurious splitting of the map along the pieces
14084 * If "mpa" has a non-trivial explicit domain, however,
14085 * then the full substitution should be performed.
14087 __isl_give isl_map
*isl_map_preimage_multi_pw_aff(__isl_take isl_map
*map
,
14088 enum isl_dim_type type
, __isl_take isl_multi_pw_aff
*mpa
)
14092 isl_pw_multi_aff
*pma
;
14094 n
= isl_map_dim(map
, type
);
14098 full
= isl_map_involves_dims(map
, type
, 0, n
);
14099 if (full
>= 0 && !full
)
14100 full
= isl_multi_pw_aff_has_non_trivial_domain(mpa
);
14107 space
= isl_multi_pw_aff_get_space(mpa
);
14108 isl_multi_pw_aff_free(mpa
);
14109 ma
= isl_multi_aff_zero(space
);
14110 return isl_map_preimage_multi_aff(map
, type
, ma
);
14113 pma
= isl_pw_multi_aff_from_multi_pw_aff(mpa
);
14114 return isl_map_preimage_pw_multi_aff(map
, type
, pma
);
14117 isl_multi_pw_aff_free(mpa
);
14121 /* Compute the preimage of "map" under the function represented by "mpa".
14122 * In other words, plug in "mpa" in the domain "map".
14123 * The result is a map that lives in the same space as "map",
14124 * except that domain space has been replaced by the domain space of "mpa".
14126 __isl_give isl_map
*isl_map_preimage_domain_multi_pw_aff(
14127 __isl_take isl_map
*map
, __isl_take isl_multi_pw_aff
*mpa
)
14129 return isl_map_preimage_multi_pw_aff(map
, isl_dim_in
, mpa
);
14132 /* Compute the preimage of "set" by the function represented by "mpa".
14133 * In other words, plug in "mpa" in "set".
14135 __isl_give isl_set
*isl_set_preimage_multi_pw_aff(__isl_take isl_set
*set
,
14136 __isl_take isl_multi_pw_aff
*mpa
)
14138 return isl_map_preimage_multi_pw_aff(set
, isl_dim_set
, mpa
);
14141 /* Return a copy of the equality constraints of "bset" as a matrix.
14143 __isl_give isl_mat
*isl_basic_set_extract_equalities(
14144 __isl_keep isl_basic_set
*bset
)
14149 total
= isl_basic_set_dim(bset
, isl_dim_all
);
14153 ctx
= isl_basic_set_get_ctx(bset
);
14154 return isl_mat_sub_alloc6(ctx
, bset
->eq
, 0, bset
->n_eq
, 0, 1 + total
);
14157 /* Are the "n" "coefficients" starting at "first" of the integer division
14158 * expressions at position "pos1" in "bmap1" and "pos2" in "bmap2" equal
14160 * The "coefficient" at position 0 is the denominator.
14161 * The "coefficient" at position 1 is the constant term.
14163 isl_bool
isl_basic_map_equal_div_expr_part(__isl_keep isl_basic_map
*bmap1
,
14164 int pos1
, __isl_keep isl_basic_map
*bmap2
, int pos2
,
14165 unsigned first
, unsigned n
)
14167 if (isl_basic_map_check_range(bmap1
, isl_dim_div
, pos1
, 1) < 0)
14168 return isl_bool_error
;
14169 if (isl_basic_map_check_range(bmap2
, isl_dim_div
, pos2
, 1) < 0)
14170 return isl_bool_error
;
14171 return isl_seq_eq(bmap1
->div
[pos1
] + first
,
14172 bmap2
->div
[pos2
] + first
, n
);
14175 /* Are the integer division expressions at position "pos1" in "bmap1" and
14176 * "pos2" in "bmap2" equal to each other, except that the constant terms
14179 isl_bool
isl_basic_map_equal_div_expr_except_constant(
14180 __isl_keep isl_basic_map
*bmap1
, int pos1
,
14181 __isl_keep isl_basic_map
*bmap2
, int pos2
)
14184 isl_size total
, total2
;
14186 total
= isl_basic_map_dim(bmap1
, isl_dim_all
);
14187 total2
= isl_basic_map_dim(bmap2
, isl_dim_all
);
14188 if (total
< 0 || total2
< 0)
14189 return isl_bool_error
;
14190 if (total
!= total2
)
14191 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
14192 "incomparable div expressions", return isl_bool_error
);
14193 equal
= isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
14195 if (equal
< 0 || !equal
)
14197 equal
= isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
14199 if (equal
< 0 || equal
)
14200 return isl_bool_not(equal
);
14201 return isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
14205 /* Replace the numerator of the constant term of the integer division
14206 * expression at position "div" in "bmap" by "value".
14207 * The caller guarantees that this does not change the meaning
14210 __isl_give isl_basic_map
*isl_basic_map_set_div_expr_constant_num_si_inplace(
14211 __isl_take isl_basic_map
*bmap
, int div
, int value
)
14213 if (isl_basic_map_check_range(bmap
, isl_dim_div
, div
, 1) < 0)
14214 return isl_basic_map_free(bmap
);
14216 isl_int_set_si(bmap
->div
[div
][1], value
);
14221 /* Is the point "inner" internal to inequality constraint "ineq"
14223 * The point is considered to be internal to the inequality constraint,
14224 * if it strictly lies on the positive side of the inequality constraint,
14225 * or if it lies on the constraint and the constraint is lexico-positive.
14227 static isl_bool
is_internal(__isl_keep isl_vec
*inner
,
14228 __isl_keep isl_basic_set
*bset
, int ineq
)
14234 if (!inner
|| !bset
)
14235 return isl_bool_error
;
14237 ctx
= isl_basic_set_get_ctx(bset
);
14238 isl_seq_inner_product(inner
->el
, bset
->ineq
[ineq
], inner
->size
,
14239 &ctx
->normalize_gcd
);
14240 if (!isl_int_is_zero(ctx
->normalize_gcd
))
14241 return isl_int_is_nonneg(ctx
->normalize_gcd
);
14243 total
= isl_basic_set_dim(bset
, isl_dim_all
);
14245 return isl_bool_error
;
14246 pos
= isl_seq_first_non_zero(bset
->ineq
[ineq
] + 1, total
);
14247 return isl_int_is_pos(bset
->ineq
[ineq
][1 + pos
]);
14250 /* Tighten the inequality constraints of "bset" that are outward with respect
14251 * to the point "vec".
14252 * That is, tighten the constraints that are not satisfied by "vec".
14254 * "vec" is a point internal to some superset S of "bset" that is used
14255 * to make the subsets of S disjoint, by tightening one half of the constraints
14256 * that separate two subsets. In particular, the constraints of S
14257 * are all satisfied by "vec" and should not be tightened.
14258 * Of the internal constraints, those that have "vec" on the outside
14259 * are tightened. The shared facet is included in the adjacent subset
14260 * with the opposite constraint.
14261 * For constraints that saturate "vec", this criterion cannot be used
14262 * to determine which of the two sides should be tightened.
14263 * Instead, the sign of the first non-zero coefficient is used
14264 * to make this choice. Note that this second criterion is never used
14265 * on the constraints of S since "vec" is interior to "S".
14267 __isl_give isl_basic_set
*isl_basic_set_tighten_outward(
14268 __isl_take isl_basic_set
*bset
, __isl_keep isl_vec
*vec
)
14272 bset
= isl_basic_set_cow(bset
);
14275 for (j
= 0; j
< bset
->n_ineq
; ++j
) {
14278 internal
= is_internal(vec
, bset
, j
);
14280 return isl_basic_set_free(bset
);
14283 isl_int_sub_ui(bset
->ineq
[j
][0], bset
->ineq
[j
][0], 1);
14289 /* Replace the variables x of type "type" starting at "first" in "bmap"
14290 * by x' with x = M x' with M the matrix trans.
14291 * That is, replace the corresponding coefficients c by c M.
14293 * The transformation matrix should be a square matrix.
14295 __isl_give isl_basic_map
*isl_basic_map_transform_dims(
14296 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
, unsigned first
,
14297 __isl_take isl_mat
*trans
)
14301 bmap
= isl_basic_map_cow(bmap
);
14302 if (!bmap
|| !trans
)
14305 if (trans
->n_row
!= trans
->n_col
)
14306 isl_die(trans
->ctx
, isl_error_invalid
,
14307 "expecting square transformation matrix", goto error
);
14308 if (isl_basic_map_check_range(bmap
, type
, first
, trans
->n_row
) < 0)
14311 pos
= isl_basic_map_offset(bmap
, type
) + first
;
14313 if (isl_mat_sub_transform(bmap
->eq
, bmap
->n_eq
, pos
,
14314 isl_mat_copy(trans
)) < 0)
14316 if (isl_mat_sub_transform(bmap
->ineq
, bmap
->n_ineq
, pos
,
14317 isl_mat_copy(trans
)) < 0)
14319 if (isl_mat_sub_transform(bmap
->div
, bmap
->n_div
, 1 + pos
,
14320 isl_mat_copy(trans
)) < 0)
14323 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
14324 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
14326 isl_mat_free(trans
);
14329 isl_mat_free(trans
);
14330 isl_basic_map_free(bmap
);
14334 /* Replace the variables x of type "type" starting at "first" in "bset"
14335 * by x' with x = M x' with M the matrix trans.
14336 * That is, replace the corresponding coefficients c by c M.
14338 * The transformation matrix should be a square matrix.
14340 __isl_give isl_basic_set
*isl_basic_set_transform_dims(
14341 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned first
,
14342 __isl_take isl_mat
*trans
)
14344 return isl_basic_map_transform_dims(bset
, type
, first
, trans
);