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 /* Check that "bmap2" applies to the range of "bmap1" (ignoring parameters).
4618 static isl_stat
isl_basic_map_check_applies_range(
4619 __isl_keep isl_basic_map
*bmap1
, __isl_keep isl_basic_map
*bmap2
)
4622 isl_space
*space1
, *space2
;
4624 space1
= isl_basic_map_peek_space(bmap1
);
4625 space2
= isl_basic_map_peek_space(bmap2
);
4626 equal
= isl_space_tuple_is_equal(space1
, isl_dim_out
,
4627 space2
, isl_dim_in
);
4629 return isl_stat_error
;
4631 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
4632 "spaces don't match", return isl_stat_error
);
4636 __isl_give isl_basic_map
*isl_basic_map_apply_range(
4637 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
4639 isl_space
*space_result
= NULL
;
4640 struct isl_basic_map
*bmap
;
4641 isl_size n_in
, n_out
, n
, nparam
;
4642 unsigned total
, pos
;
4643 struct isl_dim_map
*dim_map1
, *dim_map2
;
4645 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
4647 if (isl_basic_map_check_applies_range(bmap1
, bmap2
) < 0)
4650 n_in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
4651 n_out
= isl_basic_map_dim(bmap2
, isl_dim_out
);
4652 n
= isl_basic_map_dim(bmap1
, isl_dim_out
);
4653 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
4654 if (n_in
< 0 || n_out
< 0 || n
< 0 || nparam
< 0)
4657 space_result
= isl_space_join(isl_basic_map_get_space(bmap1
),
4658 isl_basic_map_get_space(bmap2
));
4660 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ n
;
4661 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4662 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4663 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
4664 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
4665 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
4666 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_in
);
4667 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_out
);
4668 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
4669 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
4670 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
4672 bmap
= isl_basic_map_alloc_space(space_result
,
4673 bmap1
->n_div
+ bmap2
->n_div
+ n
,
4674 bmap1
->n_eq
+ bmap2
->n_eq
,
4675 bmap1
->n_ineq
+ bmap2
->n_ineq
);
4676 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
4677 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
4678 bmap
= add_divs(bmap
, n
);
4679 bmap
= isl_basic_map_simplify(bmap
);
4680 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
4681 return isl_basic_map_finalize(bmap
);
4683 isl_basic_map_free(bmap1
);
4684 isl_basic_map_free(bmap2
);
4688 __isl_give isl_basic_set
*isl_basic_set_apply(__isl_take isl_basic_set
*bset
,
4689 __isl_take isl_basic_map
*bmap
)
4691 if (isl_basic_map_check_compatible_domain(bmap
, bset
) < 0)
4694 return bset_from_bmap(isl_basic_map_apply_range(bset_to_bmap(bset
),
4697 isl_basic_set_free(bset
);
4698 isl_basic_map_free(bmap
);
4702 __isl_give isl_basic_map
*isl_basic_map_apply_domain(
4703 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
4705 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
4707 if (!isl_space_tuple_is_equal(bmap1
->dim
, isl_dim_in
,
4708 bmap2
->dim
, isl_dim_in
))
4709 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
4710 "spaces don't match", goto error
);
4712 bmap1
= isl_basic_map_reverse(bmap1
);
4713 bmap1
= isl_basic_map_apply_range(bmap1
, bmap2
);
4714 return isl_basic_map_reverse(bmap1
);
4716 isl_basic_map_free(bmap1
);
4717 isl_basic_map_free(bmap2
);
4721 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
4722 * A \cap B -> f(A) + f(B)
4724 __isl_give isl_basic_map
*isl_basic_map_sum(__isl_take isl_basic_map
*bmap1
,
4725 __isl_take isl_basic_map
*bmap2
)
4727 isl_size n_in
, n_out
, nparam
;
4728 unsigned total
, pos
;
4729 struct isl_basic_map
*bmap
= NULL
;
4730 struct isl_dim_map
*dim_map1
, *dim_map2
;
4733 if (isl_basic_map_check_equal_space(bmap1
, bmap2
) < 0)
4736 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
4737 n_in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
4738 n_out
= isl_basic_map_dim(bmap1
, isl_dim_out
);
4739 if (nparam
< 0 || n_in
< 0 || n_out
< 0)
4742 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
;
4743 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4744 dim_map2
= isl_dim_map_alloc(bmap2
->ctx
, total
);
4745 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
4746 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
);
4747 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
4748 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
4749 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_in
+ n_out
);
4750 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
4751 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
4752 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_out
);
4754 bmap
= isl_basic_map_alloc_space(isl_space_copy(bmap1
->dim
),
4755 bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
,
4756 bmap1
->n_eq
+ bmap2
->n_eq
+ n_out
,
4757 bmap1
->n_ineq
+ bmap2
->n_ineq
);
4758 for (i
= 0; i
< n_out
; ++i
) {
4759 int j
= isl_basic_map_alloc_equality(bmap
);
4762 isl_seq_clr(bmap
->eq
[j
], 1+total
);
4763 isl_int_set_si(bmap
->eq
[j
][1+nparam
+n_in
+i
], -1);
4764 isl_int_set_si(bmap
->eq
[j
][1+pos
+i
], 1);
4765 isl_int_set_si(bmap
->eq
[j
][1+pos
-n_out
+i
], 1);
4767 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
4768 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
4769 bmap
= add_divs(bmap
, 2 * n_out
);
4771 bmap
= isl_basic_map_simplify(bmap
);
4772 return isl_basic_map_finalize(bmap
);
4774 isl_basic_map_free(bmap
);
4775 isl_basic_map_free(bmap1
);
4776 isl_basic_map_free(bmap2
);
4780 /* Given two maps A -> f(A) and B -> g(B), construct a map
4781 * A \cap B -> f(A) + f(B)
4783 __isl_give isl_map
*isl_map_sum(__isl_take isl_map
*map1
,
4784 __isl_take isl_map
*map2
)
4786 struct isl_map
*result
;
4789 if (isl_map_check_equal_space(map1
, map2
) < 0)
4792 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
4793 map1
->n
* map2
->n
, 0);
4796 for (i
= 0; i
< map1
->n
; ++i
)
4797 for (j
= 0; j
< map2
->n
; ++j
) {
4798 struct isl_basic_map
*part
;
4799 part
= isl_basic_map_sum(
4800 isl_basic_map_copy(map1
->p
[i
]),
4801 isl_basic_map_copy(map2
->p
[j
]));
4802 if (isl_basic_map_is_empty(part
))
4803 isl_basic_map_free(part
);
4805 result
= isl_map_add_basic_map(result
, part
);
4818 __isl_give isl_set
*isl_set_sum(__isl_take isl_set
*set1
,
4819 __isl_take isl_set
*set2
)
4821 return set_from_map(isl_map_sum(set_to_map(set1
), set_to_map(set2
)));
4824 /* Given a basic map A -> f(A), construct A -> -f(A).
4826 __isl_give isl_basic_map
*isl_basic_map_neg(__isl_take isl_basic_map
*bmap
)
4832 bmap
= isl_basic_map_cow(bmap
);
4833 n
= isl_basic_map_dim(bmap
, isl_dim_out
);
4835 return isl_basic_map_free(bmap
);
4837 off
= isl_basic_map_offset(bmap
, isl_dim_out
);
4838 for (i
= 0; i
< bmap
->n_eq
; ++i
)
4839 for (j
= 0; j
< n
; ++j
)
4840 isl_int_neg(bmap
->eq
[i
][off
+j
], bmap
->eq
[i
][off
+j
]);
4841 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
4842 for (j
= 0; j
< n
; ++j
)
4843 isl_int_neg(bmap
->ineq
[i
][off
+j
], bmap
->ineq
[i
][off
+j
]);
4844 for (i
= 0; i
< bmap
->n_div
; ++i
)
4845 for (j
= 0; j
< n
; ++j
)
4846 isl_int_neg(bmap
->div
[i
][1+off
+j
], bmap
->div
[i
][1+off
+j
]);
4847 bmap
= isl_basic_map_gauss(bmap
, NULL
);
4848 return isl_basic_map_finalize(bmap
);
4851 __isl_give isl_basic_set
*isl_basic_set_neg(__isl_take isl_basic_set
*bset
)
4853 return isl_basic_map_neg(bset
);
4856 /* Given a map A -> f(A), construct A -> -f(A).
4858 __isl_give isl_map
*isl_map_neg(__isl_take isl_map
*map
)
4862 map
= isl_map_cow(map
);
4866 for (i
= 0; i
< map
->n
; ++i
) {
4867 map
->p
[i
] = isl_basic_map_neg(map
->p
[i
]);
4878 __isl_give isl_set
*isl_set_neg(__isl_take isl_set
*set
)
4880 return set_from_map(isl_map_neg(set_to_map(set
)));
4883 /* Given a basic map A -> f(A) and an integer d, construct a basic map
4884 * A -> floor(f(A)/d).
4886 __isl_give isl_basic_map
*isl_basic_map_floordiv(__isl_take isl_basic_map
*bmap
,
4889 isl_size n_in
, n_out
, nparam
;
4890 unsigned total
, pos
;
4891 struct isl_basic_map
*result
= NULL
;
4892 struct isl_dim_map
*dim_map
;
4895 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4896 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4897 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4898 if (nparam
< 0 || n_in
< 0 || n_out
< 0)
4899 return isl_basic_map_free(bmap
);
4901 total
= nparam
+ n_in
+ n_out
+ bmap
->n_div
+ n_out
;
4902 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
4903 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_param
, pos
= 0);
4904 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_in
, pos
+= nparam
);
4905 isl_dim_map_div(dim_map
, bmap
, pos
+= n_in
+ n_out
);
4906 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_out
, pos
+= bmap
->n_div
);
4908 result
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
4909 bmap
->n_div
+ n_out
,
4910 bmap
->n_eq
, bmap
->n_ineq
+ 2 * n_out
);
4911 result
= isl_basic_map_add_constraints_dim_map(result
, bmap
, dim_map
);
4912 result
= add_divs(result
, n_out
);
4913 for (i
= 0; i
< n_out
; ++i
) {
4915 j
= isl_basic_map_alloc_inequality(result
);
4918 isl_seq_clr(result
->ineq
[j
], 1+total
);
4919 isl_int_neg(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
4920 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], 1);
4921 j
= isl_basic_map_alloc_inequality(result
);
4924 isl_seq_clr(result
->ineq
[j
], 1+total
);
4925 isl_int_set(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
4926 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], -1);
4927 isl_int_sub_ui(result
->ineq
[j
][0], d
, 1);
4930 result
= isl_basic_map_simplify(result
);
4931 return isl_basic_map_finalize(result
);
4933 isl_basic_map_free(result
);
4937 /* Given a map A -> f(A) and an integer d, construct a map
4938 * A -> floor(f(A)/d).
4940 __isl_give isl_map
*isl_map_floordiv(__isl_take isl_map
*map
, isl_int d
)
4944 map
= isl_map_cow(map
);
4948 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
4949 for (i
= 0; i
< map
->n
; ++i
) {
4950 map
->p
[i
] = isl_basic_map_floordiv(map
->p
[i
], d
);
4954 map
= isl_map_unmark_normalized(map
);
4962 /* Given a map A -> f(A) and an integer d, construct a map
4963 * A -> floor(f(A)/d).
4965 __isl_give isl_map
*isl_map_floordiv_val(__isl_take isl_map
*map
,
4966 __isl_take isl_val
*d
)
4970 if (!isl_val_is_int(d
))
4971 isl_die(isl_val_get_ctx(d
), isl_error_invalid
,
4972 "expecting integer denominator", goto error
);
4973 map
= isl_map_floordiv(map
, d
->n
);
4982 static __isl_give isl_basic_map
*var_equal(__isl_take isl_basic_map
*bmap
,
4990 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4991 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4992 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4993 if (total
< 0 || nparam
< 0 || n_in
< 0)
4994 return isl_basic_map_free(bmap
);
4995 i
= isl_basic_map_alloc_equality(bmap
);
4998 isl_seq_clr(bmap
->eq
[i
], 1 + total
);
4999 isl_int_set_si(bmap
->eq
[i
][1+nparam
+pos
], -1);
5000 isl_int_set_si(bmap
->eq
[i
][1+nparam
+n_in
+pos
], 1);
5001 return isl_basic_map_finalize(bmap
);
5003 isl_basic_map_free(bmap
);
5007 /* Add a constraint to "bmap" expressing i_pos < o_pos
5009 static __isl_give isl_basic_map
*var_less(__isl_take isl_basic_map
*bmap
,
5017 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5018 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5019 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5020 if (total
< 0 || nparam
< 0 || n_in
< 0)
5021 return isl_basic_map_free(bmap
);
5022 i
= isl_basic_map_alloc_inequality(bmap
);
5025 isl_seq_clr(bmap
->ineq
[i
], 1 + total
);
5026 isl_int_set_si(bmap
->ineq
[i
][0], -1);
5027 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
5028 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
5029 return isl_basic_map_finalize(bmap
);
5031 isl_basic_map_free(bmap
);
5035 /* Add a constraint to "bmap" expressing i_pos <= o_pos
5037 static __isl_give isl_basic_map
*var_less_or_equal(
5038 __isl_take isl_basic_map
*bmap
, unsigned pos
)
5045 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5046 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5047 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5048 if (total
< 0 || nparam
< 0 || n_in
< 0)
5049 return isl_basic_map_free(bmap
);
5050 i
= isl_basic_map_alloc_inequality(bmap
);
5053 isl_seq_clr(bmap
->ineq
[i
], 1 + total
);
5054 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
5055 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
5056 return isl_basic_map_finalize(bmap
);
5058 isl_basic_map_free(bmap
);
5062 /* Add a constraint to "bmap" expressing i_pos > o_pos
5064 static __isl_give isl_basic_map
*var_more(__isl_take isl_basic_map
*bmap
,
5072 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5073 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5074 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5075 if (total
< 0 || nparam
< 0 || n_in
< 0)
5076 return isl_basic_map_free(bmap
);
5077 i
= isl_basic_map_alloc_inequality(bmap
);
5080 isl_seq_clr(bmap
->ineq
[i
], 1 + total
);
5081 isl_int_set_si(bmap
->ineq
[i
][0], -1);
5082 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
5083 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
5084 return isl_basic_map_finalize(bmap
);
5086 isl_basic_map_free(bmap
);
5090 /* Add a constraint to "bmap" expressing i_pos >= o_pos
5092 static __isl_give isl_basic_map
*var_more_or_equal(
5093 __isl_take isl_basic_map
*bmap
, unsigned pos
)
5100 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5101 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5102 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5103 if (total
< 0 || nparam
< 0 || n_in
< 0)
5104 return isl_basic_map_free(bmap
);
5105 i
= isl_basic_map_alloc_inequality(bmap
);
5108 isl_seq_clr(bmap
->ineq
[i
], 1 + total
);
5109 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
5110 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
5111 return isl_basic_map_finalize(bmap
);
5113 isl_basic_map_free(bmap
);
5117 __isl_give isl_basic_map
*isl_basic_map_equal(
5118 __isl_take isl_space
*space
, unsigned n_equal
)
5121 struct isl_basic_map
*bmap
;
5122 bmap
= isl_basic_map_alloc_space(space
, 0, n_equal
, 0);
5125 for (i
= 0; i
< n_equal
&& bmap
; ++i
)
5126 bmap
= var_equal(bmap
, i
);
5127 return isl_basic_map_finalize(bmap
);
5130 /* Return a relation on of dimension "space" expressing i_[0..pos] << o_[0..pos]
5132 __isl_give isl_basic_map
*isl_basic_map_less_at(__isl_take isl_space
*space
,
5136 struct isl_basic_map
*bmap
;
5137 bmap
= isl_basic_map_alloc_space(space
, 0, pos
, 1);
5140 for (i
= 0; i
< pos
&& bmap
; ++i
)
5141 bmap
= var_equal(bmap
, i
);
5143 bmap
= var_less(bmap
, pos
);
5144 return isl_basic_map_finalize(bmap
);
5147 /* Return a relation on "space" expressing i_[0..pos] <<= o_[0..pos]
5149 __isl_give isl_basic_map
*isl_basic_map_less_or_equal_at(
5150 __isl_take isl_space
*space
, unsigned pos
)
5153 isl_basic_map
*bmap
;
5155 bmap
= isl_basic_map_alloc_space(space
, 0, pos
, 1);
5156 for (i
= 0; i
< pos
; ++i
)
5157 bmap
= var_equal(bmap
, i
);
5158 bmap
= var_less_or_equal(bmap
, pos
);
5159 return isl_basic_map_finalize(bmap
);
5162 /* Return a relation on "space" expressing i_pos > o_pos
5164 __isl_give isl_basic_map
*isl_basic_map_more_at(__isl_take isl_space
*space
,
5168 struct isl_basic_map
*bmap
;
5169 bmap
= isl_basic_map_alloc_space(space
, 0, pos
, 1);
5172 for (i
= 0; i
< pos
&& bmap
; ++i
)
5173 bmap
= var_equal(bmap
, i
);
5175 bmap
= var_more(bmap
, pos
);
5176 return isl_basic_map_finalize(bmap
);
5179 /* Return a relation on "space" expressing i_[0..pos] >>= o_[0..pos]
5181 __isl_give isl_basic_map
*isl_basic_map_more_or_equal_at(
5182 __isl_take isl_space
*space
, unsigned pos
)
5185 isl_basic_map
*bmap
;
5187 bmap
= isl_basic_map_alloc_space(space
, 0, pos
, 1);
5188 for (i
= 0; i
< pos
; ++i
)
5189 bmap
= var_equal(bmap
, i
);
5190 bmap
= var_more_or_equal(bmap
, pos
);
5191 return isl_basic_map_finalize(bmap
);
5194 static __isl_give isl_map
*map_lex_lte_first(__isl_take isl_space
*space
,
5195 unsigned n
, int equal
)
5197 struct isl_map
*map
;
5200 if (n
== 0 && equal
)
5201 return isl_map_universe(space
);
5203 map
= isl_map_alloc_space(isl_space_copy(space
), n
, ISL_MAP_DISJOINT
);
5205 for (i
= 0; i
+ 1 < n
; ++i
)
5206 map
= isl_map_add_basic_map(map
,
5207 isl_basic_map_less_at(isl_space_copy(space
), i
));
5210 map
= isl_map_add_basic_map(map
,
5211 isl_basic_map_less_or_equal_at(space
, n
- 1));
5213 map
= isl_map_add_basic_map(map
,
5214 isl_basic_map_less_at(space
, n
- 1));
5216 isl_space_free(space
);
5221 static __isl_give isl_map
*map_lex_lte(__isl_take isl_space
*space
, int equal
)
5225 return map_lex_lte_first(space
, space
->n_out
, equal
);
5228 __isl_give isl_map
*isl_map_lex_lt_first(__isl_take isl_space
*space
,
5231 return map_lex_lte_first(space
, n
, 0);
5234 __isl_give isl_map
*isl_map_lex_le_first(__isl_take isl_space
*space
,
5237 return map_lex_lte_first(space
, n
, 1);
5240 __isl_give isl_map
*isl_map_lex_lt(__isl_take isl_space
*set_space
)
5242 return map_lex_lte(isl_space_map_from_set(set_space
), 0);
5245 __isl_give isl_map
*isl_map_lex_le(__isl_take isl_space
*set_space
)
5247 return map_lex_lte(isl_space_map_from_set(set_space
), 1);
5250 static __isl_give isl_map
*map_lex_gte_first(__isl_take isl_space
*space
,
5251 unsigned n
, int equal
)
5253 struct isl_map
*map
;
5256 if (n
== 0 && equal
)
5257 return isl_map_universe(space
);
5259 map
= isl_map_alloc_space(isl_space_copy(space
), n
, ISL_MAP_DISJOINT
);
5261 for (i
= 0; i
+ 1 < n
; ++i
)
5262 map
= isl_map_add_basic_map(map
,
5263 isl_basic_map_more_at(isl_space_copy(space
), i
));
5266 map
= isl_map_add_basic_map(map
,
5267 isl_basic_map_more_or_equal_at(space
, n
- 1));
5269 map
= isl_map_add_basic_map(map
,
5270 isl_basic_map_more_at(space
, n
- 1));
5272 isl_space_free(space
);
5277 static __isl_give isl_map
*map_lex_gte(__isl_take isl_space
*space
, int equal
)
5281 return map_lex_gte_first(space
, space
->n_out
, equal
);
5284 __isl_give isl_map
*isl_map_lex_gt_first(__isl_take isl_space
*space
,
5287 return map_lex_gte_first(space
, n
, 0);
5290 __isl_give isl_map
*isl_map_lex_ge_first(__isl_take isl_space
*space
,
5293 return map_lex_gte_first(space
, n
, 1);
5296 __isl_give isl_map
*isl_map_lex_gt(__isl_take isl_space
*set_space
)
5298 return map_lex_gte(isl_space_map_from_set(set_space
), 0);
5301 __isl_give isl_map
*isl_map_lex_ge(__isl_take isl_space
*set_space
)
5303 return map_lex_gte(isl_space_map_from_set(set_space
), 1);
5306 __isl_give isl_map
*isl_set_lex_le_set(__isl_take isl_set
*set1
,
5307 __isl_take isl_set
*set2
)
5310 map
= isl_map_lex_le(isl_set_get_space(set1
));
5311 map
= isl_map_intersect_domain(map
, set1
);
5312 map
= isl_map_intersect_range(map
, set2
);
5316 __isl_give isl_map
*isl_set_lex_lt_set(__isl_take isl_set
*set1
,
5317 __isl_take isl_set
*set2
)
5320 map
= isl_map_lex_lt(isl_set_get_space(set1
));
5321 map
= isl_map_intersect_domain(map
, set1
);
5322 map
= isl_map_intersect_range(map
, set2
);
5326 __isl_give isl_map
*isl_set_lex_ge_set(__isl_take isl_set
*set1
,
5327 __isl_take isl_set
*set2
)
5330 map
= isl_map_lex_ge(isl_set_get_space(set1
));
5331 map
= isl_map_intersect_domain(map
, set1
);
5332 map
= isl_map_intersect_range(map
, set2
);
5336 __isl_give isl_map
*isl_set_lex_gt_set(__isl_take isl_set
*set1
,
5337 __isl_take isl_set
*set2
)
5340 map
= isl_map_lex_gt(isl_set_get_space(set1
));
5341 map
= isl_map_intersect_domain(map
, set1
);
5342 map
= isl_map_intersect_range(map
, set2
);
5346 __isl_give isl_map
*isl_map_lex_le_map(__isl_take isl_map
*map1
,
5347 __isl_take isl_map
*map2
)
5350 map
= isl_map_lex_le(isl_space_range(isl_map_get_space(map1
)));
5351 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
5352 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
5356 __isl_give isl_map
*isl_map_lex_lt_map(__isl_take isl_map
*map1
,
5357 __isl_take isl_map
*map2
)
5360 map
= isl_map_lex_lt(isl_space_range(isl_map_get_space(map1
)));
5361 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
5362 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
5366 __isl_give isl_map
*isl_map_lex_ge_map(__isl_take isl_map
*map1
,
5367 __isl_take isl_map
*map2
)
5370 map
= isl_map_lex_ge(isl_space_range(isl_map_get_space(map1
)));
5371 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
5372 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
5376 __isl_give isl_map
*isl_map_lex_gt_map(__isl_take isl_map
*map1
,
5377 __isl_take isl_map
*map2
)
5380 map
= isl_map_lex_gt(isl_space_range(isl_map_get_space(map1
)));
5381 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
5382 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
5386 /* For the div d = floor(f/m) at position "div", add the constraint
5390 static __isl_give isl_basic_map
*add_upper_div_constraint(
5391 __isl_take isl_basic_map
*bmap
, unsigned div
)
5394 isl_size v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
5398 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
5399 if (v_div
< 0 || n_div
< 0)
5400 return isl_basic_map_free(bmap
);
5402 i
= isl_basic_map_alloc_inequality(bmap
);
5404 return isl_basic_map_free(bmap
);
5405 isl_seq_cpy(bmap
->ineq
[i
], bmap
->div
[div
] + 1, 1 + v_div
+ n_div
);
5406 isl_int_neg(bmap
->ineq
[i
][1 + pos
], bmap
->div
[div
][0]);
5411 /* For the div d = floor(f/m) at position "div", add the constraint
5413 * -(f-(m-1)) + m d >= 0
5415 static __isl_give isl_basic_map
*add_lower_div_constraint(
5416 __isl_take isl_basic_map
*bmap
, unsigned div
)
5419 isl_size v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
5423 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
5424 if (v_div
< 0 || n_div
< 0)
5425 return isl_basic_map_free(bmap
);
5427 i
= isl_basic_map_alloc_inequality(bmap
);
5429 return isl_basic_map_free(bmap
);
5430 isl_seq_neg(bmap
->ineq
[i
], bmap
->div
[div
] + 1, 1 + v_div
+ n_div
);
5431 isl_int_set(bmap
->ineq
[i
][1 + pos
], bmap
->div
[div
][0]);
5432 isl_int_add(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], bmap
->ineq
[i
][1 + pos
]);
5433 isl_int_sub_ui(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], 1);
5438 /* For the div d = floor(f/m) at position "pos", add the constraints
5441 * -(f-(m-1)) + m d >= 0
5443 * Note that the second constraint is the negation of
5447 __isl_give isl_basic_map
*isl_basic_map_add_div_constraints(
5448 __isl_take isl_basic_map
*bmap
, unsigned pos
)
5450 bmap
= add_upper_div_constraint(bmap
, pos
);
5451 bmap
= add_lower_div_constraint(bmap
, pos
);
5455 /* For each known div d = floor(f/m), add the constraints
5458 * -(f-(m-1)) + m d >= 0
5460 * Remove duplicate constraints in case of some these div constraints
5461 * already appear in "bmap".
5463 __isl_give isl_basic_map
*isl_basic_map_add_known_div_constraints(
5464 __isl_take isl_basic_map
*bmap
)
5468 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
5470 return isl_basic_map_free(bmap
);
5474 bmap
= add_known_div_constraints(bmap
);
5475 bmap
= isl_basic_map_remove_duplicate_constraints(bmap
, NULL
, 0);
5476 bmap
= isl_basic_map_finalize(bmap
);
5480 /* Add the div constraint of sign "sign" for div "div" of "bmap".
5482 * In particular, if this div is of the form d = floor(f/m),
5483 * then add the constraint
5487 * if sign < 0 or the constraint
5489 * -(f-(m-1)) + m d >= 0
5493 __isl_give isl_basic_map
*isl_basic_map_add_div_constraint(
5494 __isl_take isl_basic_map
*bmap
, unsigned div
, int sign
)
5497 return add_upper_div_constraint(bmap
, div
);
5499 return add_lower_div_constraint(bmap
, div
);
5502 __isl_give isl_basic_set
*isl_basic_map_underlying_set(
5503 __isl_take isl_basic_map
*bmap
)
5509 if (bmap
->dim
->nparam
== 0 && bmap
->dim
->n_in
== 0 &&
5511 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_in
) &&
5512 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_out
))
5513 return bset_from_bmap(bmap
);
5514 bmap
= isl_basic_map_cow(bmap
);
5517 space
= isl_basic_map_take_space(bmap
);
5518 space
= isl_space_underlying(space
, bmap
->n_div
);
5519 bmap
= isl_basic_map_restore_space(bmap
, space
);
5522 bmap
->extra
-= bmap
->n_div
;
5524 bmap
= isl_basic_map_finalize(bmap
);
5525 return bset_from_bmap(bmap
);
5527 isl_basic_map_free(bmap
);
5531 __isl_give isl_basic_set
*isl_basic_set_underlying_set(
5532 __isl_take isl_basic_set
*bset
)
5534 return isl_basic_map_underlying_set(bset_to_bmap(bset
));
5537 /* Replace each element in "list" by the result of applying
5538 * isl_basic_map_underlying_set to the element.
5540 __isl_give isl_basic_set_list
*isl_basic_map_list_underlying_set(
5541 __isl_take isl_basic_map_list
*list
)
5546 n
= isl_basic_map_list_n_basic_map(list
);
5550 for (i
= 0; i
< n
; ++i
) {
5551 isl_basic_map
*bmap
;
5552 isl_basic_set
*bset
;
5554 bmap
= isl_basic_map_list_get_basic_map(list
, i
);
5555 bset
= isl_basic_set_underlying_set(bmap
);
5556 list
= isl_basic_set_list_set_basic_set(list
, i
, bset
);
5561 isl_basic_map_list_free(list
);
5565 __isl_give isl_basic_map
*isl_basic_map_overlying_set(
5566 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_map
*like
)
5568 struct isl_basic_map
*bmap
;
5569 struct isl_ctx
*ctx
;
5570 isl_size dim
, bmap_total
;
5577 if (isl_basic_set_check_no_params(bset
) < 0 ||
5578 isl_basic_set_check_no_locals(bset
) < 0)
5580 dim
= isl_basic_set_dim(bset
, isl_dim_set
);
5581 bmap_total
= isl_basic_map_dim(like
, isl_dim_all
);
5582 if (dim
< 0 || bmap_total
< 0)
5584 isl_assert(ctx
, dim
== bmap_total
, goto error
);
5585 if (like
->n_div
== 0) {
5586 isl_space
*space
= isl_basic_map_get_space(like
);
5587 isl_basic_map_free(like
);
5588 return isl_basic_map_reset_space(bset
, space
);
5590 bset
= isl_basic_set_cow(bset
);
5593 total
= dim
+ bset
->extra
;
5594 bmap
= bset_to_bmap(bset
);
5595 isl_space_free(isl_basic_map_take_space(bmap
));
5596 bmap
= isl_basic_map_restore_space(bmap
, isl_basic_map_get_space(like
));
5599 bmap
->n_div
= like
->n_div
;
5600 bmap
->extra
+= like
->n_div
;
5604 ltotal
= total
- bmap
->extra
+ like
->extra
;
5607 bmap
->block2
= isl_blk_extend(ctx
, bmap
->block2
,
5608 bmap
->extra
* (1 + 1 + total
));
5609 if (isl_blk_is_error(bmap
->block2
))
5611 div
= isl_realloc_array(ctx
, bmap
->div
, isl_int
*, bmap
->extra
);
5615 for (i
= 0; i
< bmap
->extra
; ++i
)
5616 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + 1 + total
);
5617 for (i
= 0; i
< like
->n_div
; ++i
) {
5618 isl_seq_cpy(bmap
->div
[i
], like
->div
[i
], 1 + 1 + ltotal
);
5619 isl_seq_clr(bmap
->div
[i
]+1+1+ltotal
, total
- ltotal
);
5621 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
5623 isl_basic_map_free(like
);
5624 bmap
= isl_basic_map_simplify(bmap
);
5625 bmap
= isl_basic_map_finalize(bmap
);
5628 isl_basic_map_free(like
);
5629 isl_basic_set_free(bset
);
5633 __isl_give isl_basic_set
*isl_basic_set_from_underlying_set(
5634 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*like
)
5636 return bset_from_bmap(isl_basic_map_overlying_set(bset
,
5637 bset_to_bmap(like
)));
5640 __isl_give isl_set
*isl_map_underlying_set(__isl_take isl_map
*map
)
5644 map
= isl_map_cow(map
);
5647 map
->dim
= isl_space_cow(map
->dim
);
5651 for (i
= 1; i
< map
->n
; ++i
)
5652 isl_assert(map
->ctx
, map
->p
[0]->n_div
== map
->p
[i
]->n_div
,
5654 for (i
= 0; i
< map
->n
; ++i
) {
5655 map
->p
[i
] = bset_to_bmap(
5656 isl_basic_map_underlying_set(map
->p
[i
]));
5661 map
->dim
= isl_space_underlying(map
->dim
, 0);
5663 isl_space_free(map
->dim
);
5664 map
->dim
= isl_space_copy(map
->p
[0]->dim
);
5668 return set_from_map(map
);
5674 /* Replace the space of "bmap" by "space".
5676 * If the space of "bmap" is identical to "space" (including the identifiers
5677 * of the input and output dimensions), then simply return the original input.
5679 __isl_give isl_basic_map
*isl_basic_map_reset_space(
5680 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*space
)
5683 isl_space
*bmap_space
;
5685 bmap_space
= isl_basic_map_peek_space(bmap
);
5686 equal
= isl_space_is_equal(bmap_space
, space
);
5687 if (equal
>= 0 && equal
)
5688 equal
= isl_space_has_equal_ids(bmap_space
, space
);
5692 isl_space_free(space
);
5695 isl_space_free(isl_basic_map_take_space(bmap
));
5696 bmap
= isl_basic_map_restore_space(bmap
, space
);
5698 bmap
= isl_basic_map_finalize(bmap
);
5702 isl_basic_map_free(bmap
);
5703 isl_space_free(space
);
5707 __isl_give isl_basic_set
*isl_basic_set_reset_space(
5708 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*space
)
5710 return bset_from_bmap(isl_basic_map_reset_space(bset_to_bmap(bset
),
5714 /* Check that the total dimensions of "map" and "space" are the same.
5716 static isl_stat
check_map_space_equal_total_dim(__isl_keep isl_map
*map
,
5717 __isl_keep isl_space
*space
)
5719 isl_size dim1
, dim2
;
5721 dim1
= isl_map_dim(map
, isl_dim_all
);
5722 dim2
= isl_space_dim(space
, isl_dim_all
);
5723 if (dim1
< 0 || dim2
< 0)
5724 return isl_stat_error
;
5727 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
5728 "total dimensions do not match", return isl_stat_error
);
5731 __isl_give isl_map
*isl_map_reset_space(__isl_take isl_map
*map
,
5732 __isl_take isl_space
*space
)
5736 map
= isl_map_cow(map
);
5740 for (i
= 0; i
< map
->n
; ++i
) {
5741 map
->p
[i
] = isl_basic_map_reset_space(map
->p
[i
],
5742 isl_space_copy(space
));
5746 isl_space_free(isl_map_take_space(map
));
5747 map
= isl_map_restore_space(map
, space
);
5752 isl_space_free(space
);
5756 /* Replace the space of "map" by "space", without modifying
5757 * the dimension of "map".
5759 * If the space of "map" is identical to "space" (including the identifiers
5760 * of the input and output dimensions), then simply return the original input.
5762 __isl_give isl_map
*isl_map_reset_equal_dim_space(__isl_take isl_map
*map
,
5763 __isl_take isl_space
*space
)
5766 isl_space
*map_space
;
5768 map_space
= isl_map_peek_space(map
);
5769 equal
= isl_space_is_equal(map_space
, space
);
5770 if (equal
>= 0 && equal
)
5771 equal
= isl_space_has_equal_ids(map_space
, space
);
5775 isl_space_free(space
);
5778 if (check_map_space_equal_total_dim(map
, space
) < 0)
5780 return isl_map_reset_space(map
, space
);
5783 isl_space_free(space
);
5787 __isl_give isl_set
*isl_set_reset_space(__isl_take isl_set
*set
,
5788 __isl_take isl_space
*space
)
5790 return set_from_map(isl_map_reset_space(set_to_map(set
), space
));
5793 /* Compute the parameter domain of the given basic set.
5795 __isl_give isl_basic_set
*isl_basic_set_params(__isl_take isl_basic_set
*bset
)
5801 is_params
= isl_basic_set_is_params(bset
);
5803 return isl_basic_set_free(bset
);
5807 n
= isl_basic_set_dim(bset
, isl_dim_set
);
5809 return isl_basic_set_free(bset
);
5810 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
5811 space
= isl_basic_set_get_space(bset
);
5812 space
= isl_space_params(space
);
5813 bset
= isl_basic_set_reset_space(bset
, space
);
5817 /* Construct a zero-dimensional basic set with the given parameter domain.
5819 __isl_give isl_basic_set
*isl_basic_set_from_params(
5820 __isl_take isl_basic_set
*bset
)
5823 space
= isl_basic_set_get_space(bset
);
5824 space
= isl_space_set_from_params(space
);
5825 bset
= isl_basic_set_reset_space(bset
, space
);
5829 /* Compute the parameter domain of the given set.
5831 __isl_give isl_set
*isl_set_params(__isl_take isl_set
*set
)
5833 return isl_map_params(set_to_map(set
));
5836 /* Construct a zero-dimensional set with the given parameter domain.
5838 __isl_give isl_set
*isl_set_from_params(__isl_take isl_set
*set
)
5841 space
= isl_set_get_space(set
);
5842 space
= isl_space_set_from_params(space
);
5843 set
= isl_set_reset_space(set
, space
);
5847 /* Compute the parameter domain of the given map.
5849 __isl_give isl_set
*isl_map_params(__isl_take isl_map
*map
)
5852 isl_size n_in
, n_out
;
5854 n_in
= isl_map_dim(map
, isl_dim_in
);
5855 n_out
= isl_map_dim(map
, isl_dim_out
);
5856 if (n_in
< 0 || n_out
< 0)
5857 return isl_map_free(map
);
5858 map
= isl_map_project_out(map
, isl_dim_in
, 0, n_in
);
5859 map
= isl_map_project_out(map
, isl_dim_out
, 0, n_out
);
5860 space
= isl_map_get_space(map
);
5861 space
= isl_space_params(space
);
5862 map
= isl_map_reset_space(map
, space
);
5866 __isl_give isl_basic_set
*isl_basic_map_domain(__isl_take isl_basic_map
*bmap
)
5871 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5873 return isl_basic_map_free(bmap
);
5874 space
= isl_space_domain(isl_basic_map_get_space(bmap
));
5876 bmap
= isl_basic_map_project_out(bmap
, isl_dim_out
, 0, n_out
);
5878 return isl_basic_map_reset_space(bmap
, space
);
5881 isl_bool
isl_basic_map_may_be_set(__isl_keep isl_basic_map
*bmap
)
5884 return isl_bool_error
;
5885 return isl_space_may_be_set(bmap
->dim
);
5888 /* Is this basic map actually a set?
5889 * Users should never call this function. Outside of isl,
5890 * the type should indicate whether something is a set or a map.
5892 isl_bool
isl_basic_map_is_set(__isl_keep isl_basic_map
*bmap
)
5895 return isl_bool_error
;
5896 return isl_space_is_set(bmap
->dim
);
5899 __isl_give isl_basic_set
*isl_basic_map_range(__isl_take isl_basic_map
*bmap
)
5903 is_set
= isl_basic_map_is_set(bmap
);
5908 return isl_basic_map_domain(isl_basic_map_reverse(bmap
));
5910 isl_basic_map_free(bmap
);
5914 __isl_give isl_basic_map
*isl_basic_map_domain_map(
5915 __isl_take isl_basic_map
*bmap
)
5919 isl_basic_map
*domain
;
5920 isl_size nparam
, n_in
, n_out
;
5922 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5923 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5924 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5925 if (nparam
< 0 || n_in
< 0 || n_out
< 0)
5926 return isl_basic_map_free(bmap
);
5928 space
= isl_basic_map_get_space(bmap
);
5929 space
= isl_space_from_range(isl_space_domain(space
));
5930 domain
= isl_basic_map_universe(space
);
5932 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
5933 bmap
= isl_basic_map_apply_range(bmap
, domain
);
5934 bmap
= isl_basic_map_extend_constraints(bmap
, n_in
, 0);
5936 for (i
= 0; i
< n_in
; ++i
)
5937 bmap
= isl_basic_map_equate(bmap
, isl_dim_in
, i
,
5940 bmap
= isl_basic_map_gauss(bmap
, NULL
);
5941 return isl_basic_map_finalize(bmap
);
5944 __isl_give isl_basic_map
*isl_basic_map_range_map(
5945 __isl_take isl_basic_map
*bmap
)
5949 isl_basic_map
*range
;
5950 isl_size nparam
, n_in
, n_out
;
5952 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5953 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5954 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5955 if (nparam
< 0 || n_in
< 0 || n_out
< 0)
5956 return isl_basic_map_free(bmap
);
5958 space
= isl_basic_map_get_space(bmap
);
5959 space
= isl_space_from_range(isl_space_range(space
));
5960 range
= isl_basic_map_universe(space
);
5962 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
5963 bmap
= isl_basic_map_apply_range(bmap
, range
);
5964 bmap
= isl_basic_map_extend_constraints(bmap
, n_out
, 0);
5966 for (i
= 0; i
< n_out
; ++i
)
5967 bmap
= isl_basic_map_equate(bmap
, isl_dim_in
, n_in
+ i
,
5970 bmap
= isl_basic_map_gauss(bmap
, NULL
);
5971 return isl_basic_map_finalize(bmap
);
5974 int isl_map_may_be_set(__isl_keep isl_map
*map
)
5978 return isl_space_may_be_set(map
->dim
);
5981 /* Is this map actually a set?
5982 * Users should never call this function. Outside of isl,
5983 * the type should indicate whether something is a set or a map.
5985 isl_bool
isl_map_is_set(__isl_keep isl_map
*map
)
5988 return isl_bool_error
;
5989 return isl_space_is_set(map
->dim
);
5992 __isl_give isl_set
*isl_map_range(__isl_take isl_map
*map
)
5996 struct isl_set
*set
;
5998 is_set
= isl_map_is_set(map
);
6002 return set_from_map(map
);
6004 map
= isl_map_cow(map
);
6008 set
= set_from_map(map
);
6009 set
->dim
= isl_space_range(set
->dim
);
6012 for (i
= 0; i
< map
->n
; ++i
) {
6013 set
->p
[i
] = isl_basic_map_range(map
->p
[i
]);
6017 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
6018 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
6025 /* Transform "map" by applying "fn_space" to its space and "fn_bmap"
6026 * to each of its basic maps.
6028 static __isl_give isl_map
*isl_map_transform(__isl_take isl_map
*map
,
6029 __isl_give isl_space
*(*fn_space
)(__isl_take isl_space
*space
),
6030 __isl_give isl_basic_map
*(*fn_bmap
)(__isl_take isl_basic_map
*bmap
))
6035 map
= isl_map_cow(map
);
6039 for (i
= 0; i
< map
->n
; ++i
) {
6040 map
->p
[i
] = fn_bmap(map
->p
[i
]);
6042 return isl_map_free(map
);
6044 map
= isl_map_unmark_normalized(map
);
6046 space
= isl_map_take_space(map
);
6047 space
= fn_space(space
);
6048 map
= isl_map_restore_space(map
, space
);
6053 __isl_give isl_map
*isl_map_domain_map(__isl_take isl_map
*map
)
6055 return isl_map_transform(map
, &isl_space_domain_map
,
6056 &isl_basic_map_domain_map
);
6059 __isl_give isl_map
*isl_map_range_map(__isl_take isl_map
*map
)
6061 return isl_map_transform(map
, &isl_space_range_map
,
6062 &isl_basic_map_range_map
);
6065 /* Given a wrapped map of the form A[B -> C],
6066 * return the map A[B -> C] -> B.
6068 __isl_give isl_map
*isl_set_wrapped_domain_map(__isl_take isl_set
*set
)
6075 if (!isl_set_has_tuple_id(set
))
6076 return isl_map_domain_map(isl_set_unwrap(set
));
6078 id
= isl_set_get_tuple_id(set
);
6079 map
= isl_map_domain_map(isl_set_unwrap(set
));
6080 map
= isl_map_set_tuple_id(map
, isl_dim_in
, id
);
6085 __isl_give isl_basic_map
*isl_basic_map_from_domain(
6086 __isl_take isl_basic_set
*bset
)
6088 return isl_basic_map_reverse(isl_basic_map_from_range(bset
));
6091 __isl_give isl_basic_map
*isl_basic_map_from_range(
6092 __isl_take isl_basic_set
*bset
)
6095 space
= isl_basic_set_get_space(bset
);
6096 space
= isl_space_from_range(space
);
6097 bset
= isl_basic_set_reset_space(bset
, space
);
6098 return bset_to_bmap(bset
);
6101 /* Create a relation with the given set as range.
6102 * The domain of the created relation is a zero-dimensional
6103 * flat anonymous space.
6105 __isl_give isl_map
*isl_map_from_range(__isl_take isl_set
*set
)
6108 space
= isl_set_get_space(set
);
6109 space
= isl_space_from_range(space
);
6110 set
= isl_set_reset_space(set
, space
);
6111 return set_to_map(set
);
6114 /* Create a relation with the given set as domain.
6115 * The range of the created relation is a zero-dimensional
6116 * flat anonymous space.
6118 __isl_give isl_map
*isl_map_from_domain(__isl_take isl_set
*set
)
6120 return isl_map_reverse(isl_map_from_range(set
));
6123 __isl_give isl_basic_map
*isl_basic_map_from_domain_and_range(
6124 __isl_take isl_basic_set
*domain
, __isl_take isl_basic_set
*range
)
6126 return isl_basic_map_apply_range(isl_basic_map_reverse(domain
), range
);
6129 __isl_give isl_map
*isl_map_from_domain_and_range(__isl_take isl_set
*domain
,
6130 __isl_take isl_set
*range
)
6132 return isl_map_apply_range(isl_map_reverse(domain
), range
);
6135 /* Return a newly allocated isl_map with given space and flags and
6136 * room for "n" basic maps.
6137 * Make sure that all cached information is cleared.
6139 __isl_give isl_map
*isl_map_alloc_space(__isl_take isl_space
*space
, int n
,
6142 struct isl_map
*map
;
6147 isl_die(space
->ctx
, isl_error_internal
,
6148 "negative number of basic maps", goto error
);
6149 map
= isl_calloc(space
->ctx
, struct isl_map
,
6150 sizeof(struct isl_map
) +
6151 (n
- 1) * sizeof(struct isl_basic_map
*));
6155 map
->ctx
= space
->ctx
;
6156 isl_ctx_ref(map
->ctx
);
6164 isl_space_free(space
);
6168 __isl_give isl_basic_map
*isl_basic_map_empty(__isl_take isl_space
*space
)
6170 struct isl_basic_map
*bmap
;
6171 bmap
= isl_basic_map_alloc_space(space
, 0, 1, 0);
6172 bmap
= isl_basic_map_set_to_empty(bmap
);
6176 __isl_give isl_basic_set
*isl_basic_set_empty(__isl_take isl_space
*space
)
6178 struct isl_basic_set
*bset
;
6179 bset
= isl_basic_set_alloc_space(space
, 0, 1, 0);
6180 bset
= isl_basic_set_set_to_empty(bset
);
6184 __isl_give isl_basic_map
*isl_basic_map_universe(__isl_take isl_space
*space
)
6186 struct isl_basic_map
*bmap
;
6187 bmap
= isl_basic_map_alloc_space(space
, 0, 0, 0);
6188 bmap
= isl_basic_map_finalize(bmap
);
6192 __isl_give isl_basic_set
*isl_basic_set_universe(__isl_take isl_space
*space
)
6194 struct isl_basic_set
*bset
;
6195 bset
= isl_basic_set_alloc_space(space
, 0, 0, 0);
6196 bset
= isl_basic_set_finalize(bset
);
6200 __isl_give isl_basic_map
*isl_basic_map_nat_universe(
6201 __isl_take isl_space
*space
)
6204 isl_size total
= isl_space_dim(space
, isl_dim_all
);
6205 isl_basic_map
*bmap
;
6208 space
= isl_space_free(space
);
6209 bmap
= isl_basic_map_alloc_space(space
, 0, 0, total
);
6210 for (i
= 0; i
< total
; ++i
) {
6211 int k
= isl_basic_map_alloc_inequality(bmap
);
6214 isl_seq_clr(bmap
->ineq
[k
], 1 + total
);
6215 isl_int_set_si(bmap
->ineq
[k
][1 + i
], 1);
6219 isl_basic_map_free(bmap
);
6223 __isl_give isl_basic_set
*isl_basic_set_nat_universe(
6224 __isl_take isl_space
*space
)
6226 return isl_basic_map_nat_universe(space
);
6229 __isl_give isl_map
*isl_map_nat_universe(__isl_take isl_space
*space
)
6231 return isl_map_from_basic_map(isl_basic_map_nat_universe(space
));
6234 __isl_give isl_set
*isl_set_nat_universe(__isl_take isl_space
*space
)
6236 return isl_map_nat_universe(space
);
6239 __isl_give isl_map
*isl_map_empty(__isl_take isl_space
*space
)
6241 return isl_map_alloc_space(space
, 0, ISL_MAP_DISJOINT
);
6244 __isl_give isl_set
*isl_set_empty(__isl_take isl_space
*space
)
6246 return isl_set_alloc_space(space
, 0, ISL_MAP_DISJOINT
);
6249 __isl_give isl_map
*isl_map_universe(__isl_take isl_space
*space
)
6251 struct isl_map
*map
;
6254 map
= isl_map_alloc_space(isl_space_copy(space
), 1, ISL_MAP_DISJOINT
);
6255 map
= isl_map_add_basic_map(map
, isl_basic_map_universe(space
));
6259 __isl_give isl_set
*isl_set_universe(__isl_take isl_space
*space
)
6261 struct isl_set
*set
;
6264 set
= isl_set_alloc_space(isl_space_copy(space
), 1, ISL_MAP_DISJOINT
);
6265 set
= isl_set_add_basic_set(set
, isl_basic_set_universe(space
));
6269 __isl_give isl_map
*isl_map_dup(__isl_keep isl_map
*map
)
6272 struct isl_map
*dup
;
6276 dup
= isl_map_alloc_space(isl_space_copy(map
->dim
), map
->n
, map
->flags
);
6277 for (i
= 0; i
< map
->n
; ++i
)
6278 dup
= isl_map_add_basic_map(dup
, isl_basic_map_copy(map
->p
[i
]));
6282 __isl_give isl_map
*isl_map_add_basic_map(__isl_take isl_map
*map
,
6283 __isl_take isl_basic_map
*bmap
)
6287 if (isl_basic_map_plain_is_empty(bmap
)) {
6288 isl_basic_map_free(bmap
);
6291 if (isl_map_basic_map_check_equal_space(map
, bmap
) < 0)
6293 isl_assert(map
->ctx
, map
->n
< map
->size
, goto error
);
6294 map
->p
[map
->n
] = bmap
;
6296 map
= isl_map_unmark_normalized(map
);
6302 isl_basic_map_free(bmap
);
6306 __isl_null isl_map
*isl_map_free(__isl_take isl_map
*map
)
6317 isl_ctx_deref(map
->ctx
);
6318 for (i
= 0; i
< map
->n
; ++i
)
6319 isl_basic_map_free(map
->p
[i
]);
6320 isl_space_free(map
->dim
);
6326 static __isl_give isl_basic_map
*isl_basic_map_fix_pos_si(
6327 __isl_take isl_basic_map
*bmap
, unsigned pos
, int value
)
6332 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
6334 return isl_basic_map_free(bmap
);
6336 bmap
= isl_basic_map_cow(bmap
);
6337 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
6338 j
= isl_basic_map_alloc_equality(bmap
);
6341 isl_seq_clr(bmap
->eq
[j
] + 1, total
);
6342 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
6343 isl_int_set_si(bmap
->eq
[j
][0], value
);
6344 bmap
= isl_basic_map_simplify(bmap
);
6345 return isl_basic_map_finalize(bmap
);
6347 isl_basic_map_free(bmap
);
6351 static __isl_give isl_basic_map
*isl_basic_map_fix_pos(
6352 __isl_take isl_basic_map
*bmap
, unsigned pos
, isl_int value
)
6357 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
6359 return isl_basic_map_free(bmap
);
6361 bmap
= isl_basic_map_cow(bmap
);
6362 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
6363 j
= isl_basic_map_alloc_equality(bmap
);
6366 isl_seq_clr(bmap
->eq
[j
] + 1, total
);
6367 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
6368 isl_int_set(bmap
->eq
[j
][0], value
);
6369 bmap
= isl_basic_map_simplify(bmap
);
6370 return isl_basic_map_finalize(bmap
);
6372 isl_basic_map_free(bmap
);
6376 __isl_give isl_basic_map
*isl_basic_map_fix_si(__isl_take isl_basic_map
*bmap
,
6377 enum isl_dim_type type
, unsigned pos
, int value
)
6379 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6380 return isl_basic_map_free(bmap
);
6381 return isl_basic_map_fix_pos_si(bmap
,
6382 isl_basic_map_offset(bmap
, type
) + pos
, value
);
6385 __isl_give isl_basic_map
*isl_basic_map_fix(__isl_take isl_basic_map
*bmap
,
6386 enum isl_dim_type type
, unsigned pos
, isl_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(bmap
,
6391 isl_basic_map_offset(bmap
, type
) + pos
, value
);
6394 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
6395 * to be equal to "v".
6397 __isl_give isl_basic_map
*isl_basic_map_fix_val(__isl_take isl_basic_map
*bmap
,
6398 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6402 if (!isl_val_is_int(v
))
6403 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
6404 "expecting integer value", goto error
);
6405 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6407 pos
+= isl_basic_map_offset(bmap
, type
);
6408 bmap
= isl_basic_map_fix_pos(bmap
, pos
, v
->n
);
6412 isl_basic_map_free(bmap
);
6417 /* Fix the value of the variable at position "pos" of type "type" of "bset"
6418 * to be equal to "v".
6420 __isl_give isl_basic_set
*isl_basic_set_fix_val(__isl_take isl_basic_set
*bset
,
6421 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6423 return isl_basic_map_fix_val(bset
, type
, pos
, v
);
6426 __isl_give isl_basic_set
*isl_basic_set_fix_si(__isl_take isl_basic_set
*bset
,
6427 enum isl_dim_type type
, unsigned pos
, int value
)
6429 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset
),
6433 __isl_give isl_basic_set
*isl_basic_set_fix(__isl_take isl_basic_set
*bset
,
6434 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6436 return bset_from_bmap(isl_basic_map_fix(bset_to_bmap(bset
),
6440 /* Remove the basic map at position "i" from "map" if this basic map
6441 * is (obviously) empty.
6443 static __isl_give isl_map
*remove_if_empty(__isl_take isl_map
*map
, int i
)
6450 empty
= isl_basic_map_plain_is_empty(map
->p
[i
]);
6452 return isl_map_free(map
);
6456 isl_basic_map_free(map
->p
[i
]);
6459 map
->p
[i
] = map
->p
[map
->n
];
6460 map
= isl_map_unmark_normalized(map
);
6467 /* Perform "fn" on each basic map of "map", where we may not be holding
6468 * the only reference to "map".
6469 * In particular, "fn" should be a semantics preserving operation
6470 * that we want to apply to all copies of "map". We therefore need
6471 * to be careful not to modify "map" in a way that breaks "map"
6472 * in case anything goes wrong.
6474 __isl_give isl_map
*isl_map_inline_foreach_basic_map(__isl_take isl_map
*map
,
6475 __isl_give isl_basic_map
*(*fn
)(__isl_take isl_basic_map
*bmap
))
6477 struct isl_basic_map
*bmap
;
6483 for (i
= map
->n
- 1; i
>= 0; --i
) {
6484 bmap
= isl_basic_map_copy(map
->p
[i
]);
6488 isl_basic_map_free(map
->p
[i
]);
6490 map
= remove_if_empty(map
, i
);
6501 __isl_give isl_map
*isl_map_fix_si(__isl_take isl_map
*map
,
6502 enum isl_dim_type type
, unsigned pos
, int value
)
6506 map
= isl_map_cow(map
);
6507 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
6508 return isl_map_free(map
);
6509 for (i
= map
->n
- 1; i
>= 0; --i
) {
6510 map
->p
[i
] = isl_basic_map_fix_si(map
->p
[i
], type
, pos
, value
);
6511 map
= remove_if_empty(map
, i
);
6515 map
= isl_map_unmark_normalized(map
);
6519 __isl_give isl_set
*isl_set_fix_si(__isl_take isl_set
*set
,
6520 enum isl_dim_type type
, unsigned pos
, int value
)
6522 return set_from_map(isl_map_fix_si(set_to_map(set
), type
, pos
, value
));
6525 __isl_give isl_map
*isl_map_fix(__isl_take isl_map
*map
,
6526 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6530 map
= isl_map_cow(map
);
6531 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
6532 return isl_map_free(map
);
6533 for (i
= 0; i
< map
->n
; ++i
) {
6534 map
->p
[i
] = isl_basic_map_fix(map
->p
[i
], type
, pos
, value
);
6538 map
= isl_map_unmark_normalized(map
);
6545 __isl_give isl_set
*isl_set_fix(__isl_take isl_set
*set
,
6546 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6548 return set_from_map(isl_map_fix(set_to_map(set
), type
, pos
, value
));
6551 /* Fix the value of the variable at position "pos" of type "type" of "map"
6552 * to be equal to "v".
6554 __isl_give isl_map
*isl_map_fix_val(__isl_take isl_map
*map
,
6555 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6559 map
= isl_map_cow(map
);
6563 if (!isl_val_is_int(v
))
6564 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
6565 "expecting integer value", goto error
);
6566 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
6568 for (i
= map
->n
- 1; i
>= 0; --i
) {
6569 map
->p
[i
] = isl_basic_map_fix_val(map
->p
[i
], type
, pos
,
6571 map
= remove_if_empty(map
, i
);
6575 map
= isl_map_unmark_normalized(map
);
6584 /* Fix the value of the variable at position "pos" of type "type" of "set"
6585 * to be equal to "v".
6587 __isl_give isl_set
*isl_set_fix_val(__isl_take isl_set
*set
,
6588 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6590 return isl_map_fix_val(set
, type
, pos
, v
);
6593 __isl_give isl_map
*isl_map_fix_input_si(__isl_take isl_map
*map
,
6594 unsigned input
, int value
)
6596 return isl_map_fix_si(map
, isl_dim_in
, input
, value
);
6599 __isl_give isl_set
*isl_set_fix_dim_si(__isl_take isl_set
*set
, unsigned dim
,
6602 return set_from_map(isl_map_fix_si(set_to_map(set
),
6603 isl_dim_set
, dim
, value
));
6606 static __isl_give isl_basic_map
*basic_map_bound_si(
6607 __isl_take isl_basic_map
*bmap
,
6608 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
6613 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6614 return isl_basic_map_free(bmap
);
6615 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
6617 return isl_basic_map_free(bmap
);
6618 pos
+= isl_basic_map_offset(bmap
, type
);
6619 bmap
= isl_basic_map_cow(bmap
);
6620 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
6621 j
= isl_basic_map_alloc_inequality(bmap
);
6624 isl_seq_clr(bmap
->ineq
[j
], 1 + total
);
6626 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
6627 isl_int_set_si(bmap
->ineq
[j
][0], value
);
6629 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
6630 isl_int_set_si(bmap
->ineq
[j
][0], -value
);
6632 bmap
= isl_basic_map_simplify(bmap
);
6633 return isl_basic_map_finalize(bmap
);
6635 isl_basic_map_free(bmap
);
6639 __isl_give isl_basic_map
*isl_basic_map_lower_bound_si(
6640 __isl_take isl_basic_map
*bmap
,
6641 enum isl_dim_type type
, unsigned pos
, int value
)
6643 return basic_map_bound_si(bmap
, type
, pos
, value
, 0);
6646 /* Constrain the values of the given dimension to be no greater than "value".
6648 __isl_give isl_basic_map
*isl_basic_map_upper_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
, 1);
6655 static __isl_give isl_map
*map_bound_si(__isl_take isl_map
*map
,
6656 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
6660 map
= isl_map_cow(map
);
6661 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
6662 return isl_map_free(map
);
6663 for (i
= 0; i
< map
->n
; ++i
) {
6664 map
->p
[i
] = basic_map_bound_si(map
->p
[i
],
6665 type
, pos
, value
, upper
);
6669 map
= isl_map_unmark_normalized(map
);
6676 __isl_give isl_map
*isl_map_lower_bound_si(__isl_take isl_map
*map
,
6677 enum isl_dim_type type
, unsigned pos
, int value
)
6679 return map_bound_si(map
, type
, pos
, value
, 0);
6682 __isl_give isl_map
*isl_map_upper_bound_si(__isl_take isl_map
*map
,
6683 enum isl_dim_type type
, unsigned pos
, int value
)
6685 return map_bound_si(map
, type
, pos
, value
, 1);
6688 __isl_give isl_set
*isl_set_lower_bound_si(__isl_take isl_set
*set
,
6689 enum isl_dim_type type
, unsigned pos
, int value
)
6691 return set_from_map(isl_map_lower_bound_si(set_to_map(set
),
6695 __isl_give isl_set
*isl_set_upper_bound_si(__isl_take isl_set
*set
,
6696 enum isl_dim_type type
, unsigned pos
, int value
)
6698 return isl_map_upper_bound_si(set
, type
, pos
, value
);
6701 /* Bound the given variable of "bmap" from below (or above is "upper"
6702 * is set) to "value".
6704 static __isl_give isl_basic_map
*basic_map_bound(
6705 __isl_take isl_basic_map
*bmap
,
6706 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
6711 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6712 return isl_basic_map_free(bmap
);
6713 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
6715 return isl_basic_map_free(bmap
);
6716 pos
+= isl_basic_map_offset(bmap
, type
);
6717 bmap
= isl_basic_map_cow(bmap
);
6718 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
6719 j
= isl_basic_map_alloc_inequality(bmap
);
6722 isl_seq_clr(bmap
->ineq
[j
], 1 + total
);
6724 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
6725 isl_int_set(bmap
->ineq
[j
][0], value
);
6727 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
6728 isl_int_neg(bmap
->ineq
[j
][0], value
);
6730 bmap
= isl_basic_map_simplify(bmap
);
6731 return isl_basic_map_finalize(bmap
);
6733 isl_basic_map_free(bmap
);
6737 /* Bound the given variable of "map" from below (or above is "upper"
6738 * is set) to "value".
6740 static __isl_give isl_map
*map_bound(__isl_take isl_map
*map
,
6741 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
6745 map
= isl_map_cow(map
);
6746 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
6747 return isl_map_free(map
);
6748 for (i
= map
->n
- 1; i
>= 0; --i
) {
6749 map
->p
[i
] = basic_map_bound(map
->p
[i
], type
, pos
, value
, upper
);
6750 map
= remove_if_empty(map
, i
);
6754 map
= isl_map_unmark_normalized(map
);
6758 __isl_give isl_map
*isl_map_lower_bound(__isl_take isl_map
*map
,
6759 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6761 return map_bound(map
, type
, pos
, value
, 0);
6764 __isl_give isl_map
*isl_map_upper_bound(__isl_take isl_map
*map
,
6765 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6767 return map_bound(map
, type
, pos
, value
, 1);
6770 __isl_give isl_set
*isl_set_lower_bound(__isl_take isl_set
*set
,
6771 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6773 return isl_map_lower_bound(set
, type
, pos
, value
);
6776 __isl_give isl_set
*isl_set_upper_bound(__isl_take isl_set
*set
,
6777 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6779 return isl_map_upper_bound(set
, type
, pos
, value
);
6782 /* Force the values of the variable at position "pos" of type "type" of "map"
6783 * to be no smaller than "value".
6785 __isl_give isl_map
*isl_map_lower_bound_val(__isl_take isl_map
*map
,
6786 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
6790 if (!isl_val_is_int(value
))
6791 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
6792 "expecting integer value", goto error
);
6793 map
= isl_map_lower_bound(map
, type
, pos
, value
->n
);
6794 isl_val_free(value
);
6797 isl_val_free(value
);
6802 /* Force the values of the variable at position "pos" of type "type" of "set"
6803 * to be no smaller than "value".
6805 __isl_give isl_set
*isl_set_lower_bound_val(__isl_take isl_set
*set
,
6806 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
6810 map
= set_to_map(set
);
6811 return set_from_map(isl_map_lower_bound_val(map
, type
, pos
, value
));
6814 /* Force the values of the variable at position "pos" of type "type" of "map"
6815 * to be no greater than "value".
6817 __isl_give isl_map
*isl_map_upper_bound_val(__isl_take isl_map
*map
,
6818 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
6822 if (!isl_val_is_int(value
))
6823 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
6824 "expecting integer value", goto error
);
6825 map
= isl_map_upper_bound(map
, type
, pos
, value
->n
);
6826 isl_val_free(value
);
6829 isl_val_free(value
);
6834 /* Force the values of the variable at position "pos" of type "type" of "set"
6835 * to be no greater than "value".
6837 __isl_give isl_set
*isl_set_upper_bound_val(__isl_take isl_set
*set
,
6838 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
6842 map
= set_to_map(set
);
6843 return set_from_map(isl_map_upper_bound_val(map
, type
, pos
, value
));
6848 #include "isl_map_bound_templ.c"
6850 /* Apply "map_bound" to "set" with the corresponding value in "bound"
6851 * for each set dimension, by treating the set as a map.
6853 static __isl_give isl_set
*set_bound_multi_val(__isl_take isl_set
*set
,
6854 __isl_take isl_multi_val
*bound
,
6855 __isl_give isl_map
*map_bound(__isl_take isl_map
*map
,
6856 unsigned pos
, __isl_take isl_val
*value
))
6860 map
= set_to_map(set
);
6861 return set_from_map(map_bound_multi_val(map
, bound
, map_bound
));
6866 #include "isl_map_bound_templ.c"
6868 /* Apply "map_bound" to "set" with the corresponding value in "bound"
6869 * for each set dimension, by converting the set and the bound
6870 * to objects living in a map space.
6872 static __isl_give isl_set
*set_bound_multi_pw_aff(__isl_take isl_set
*set
,
6873 __isl_take isl_multi_pw_aff
*bound
,
6874 __isl_give isl_map
*set_bound(__isl_take isl_map
*map
,
6875 unsigned pos
, __isl_take TYPE
*value
))
6879 map
= isl_map_from_range(set
);
6880 bound
= isl_multi_pw_aff_from_range(bound
);
6881 map
= map_bound_multi_pw_aff(map
, bound
, set_bound
);
6882 return isl_map_range(map
);
6885 /* Wrapper around isl_map_lower_bound_val for use in map_bound_multi_val,
6886 * setting a bound on the given output dimension.
6888 static __isl_give isl_map
*map_lower_bound_val(__isl_take isl_map
*map
,
6889 unsigned pos
, __isl_take isl_val
*v
)
6891 return isl_map_lower_bound_val(map
, isl_dim_out
, pos
, v
);
6894 /* Force the values of the set dimensions of "set"
6895 * to be no smaller than the corresponding values in "lower".
6897 __isl_give isl_set
*isl_set_lower_bound_multi_val(__isl_take isl_set
*set
,
6898 __isl_take isl_multi_val
*lower
)
6900 return set_bound_multi_val(set
, lower
, &map_lower_bound_val
);
6903 /* Force the values of the output dimensions of "map"
6904 * to be no smaller than the corresponding values in "lower".
6906 __isl_give isl_map
*isl_map_lower_bound_multi_val(__isl_take isl_map
*map
,
6907 __isl_take isl_multi_val
*lower
)
6909 return map_bound_multi_val(map
, lower
, &map_lower_bound_val
);
6912 /* Wrapper around isl_map_upper_bound_val for use in map_bound_multi_val,
6913 * setting a bound on the given output dimension.
6915 static __isl_give isl_map
*map_upper_bound_val(__isl_take isl_map
*map
,
6916 unsigned pos
, __isl_take isl_val
*v
)
6918 return isl_map_upper_bound_val(map
, isl_dim_out
, pos
, v
);
6921 /* Force the values of the set dimensions of "set"
6922 * to be no greater than the corresponding values in "upper".
6924 __isl_give isl_set
*isl_set_upper_bound_multi_val(__isl_take isl_set
*set
,
6925 __isl_take isl_multi_val
*upper
)
6927 return set_bound_multi_val(set
, upper
, &map_upper_bound_val
);
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_map
*isl_map_upper_bound_multi_val(__isl_take isl_map
*map
,
6934 __isl_take isl_multi_val
*upper
)
6936 return map_bound_multi_val(map
, upper
, &map_upper_bound_val
);
6939 /* Force the symbolic constant expression "bound"
6940 * to satisfy the relation "order" with respect to
6941 * the output variable at position "pos" of "map".
6943 * Create an affine expression representing the output variable
6944 * in terms of the range and
6945 * compare it using "order" to "bound" (defined on the domain).
6946 * The result is a relation between elements in domain and range that
6947 * can be intersected with "map".
6949 static __isl_give isl_map
*map_bound_pw_aff(__isl_take isl_map
*map
,
6950 unsigned pos
, __isl_take isl_pw_aff
*bound
,
6951 __isl_give isl_map
*(*order
)(__isl_take isl_pw_aff
*pa1
,
6952 __isl_take isl_pw_aff
*pa2
))
6955 isl_local_space
*ls
;
6958 space
= isl_space_range(isl_map_get_space(map
));
6959 ls
= isl_local_space_from_space(space
);
6960 var
= isl_pw_aff_var_on_domain(ls
, isl_dim_set
, pos
);
6961 map
= isl_map_intersect(map
, order(bound
, var
));
6965 /* Force the values of the output variable at position "pos" of "map"
6966 * to be no smaller than the symbolic constant expression "lower".
6968 static __isl_give isl_map
*map_lower_bound_pw_aff(__isl_take isl_map
*map
,
6969 unsigned pos
, __isl_take isl_pw_aff
*lower
)
6971 return map_bound_pw_aff(map
, pos
, lower
, &isl_pw_aff_le_map
);
6974 /* Force the values of the output variable at position "pos" of "map"
6975 * to be no greater than the symbolic constant expression "upper".
6977 static __isl_give isl_map
*map_upper_bound_pw_aff(__isl_take isl_map
*map
,
6978 unsigned pos
, __isl_take isl_pw_aff
*upper
)
6980 return map_bound_pw_aff(map
, pos
, upper
, &isl_pw_aff_ge_map
);
6983 /* Force the values of the set dimensions of "set"
6984 * to be no smaller than the corresponding constant symbolic expressions
6987 __isl_give isl_set
*isl_set_lower_bound_multi_pw_aff(__isl_take isl_set
*set
,
6988 __isl_take isl_multi_pw_aff
*lower
)
6990 return set_bound_multi_pw_aff(set
, lower
, &map_lower_bound_pw_aff
);
6993 /* Force the values of the set dimensions of "set"
6994 * to be no greater than the corresponding constant symbolic expressions
6997 __isl_give isl_set
*isl_set_upper_bound_multi_pw_aff(__isl_take isl_set
*set
,
6998 __isl_take isl_multi_pw_aff
*upper
)
7000 return set_bound_multi_pw_aff(set
, upper
, &map_upper_bound_pw_aff
);
7003 /* Force the values of the output dimensions of "map"
7004 * to be no smaller than the corresponding constant symbolic expressions
7007 __isl_give isl_map
*isl_map_lower_bound_multi_pw_aff(__isl_take isl_map
*map
,
7008 __isl_take isl_multi_pw_aff
*lower
)
7010 return map_bound_multi_pw_aff(map
, lower
, &map_lower_bound_pw_aff
);
7013 /* Force the values of the output dimensions of "map"
7014 * to be no greater than the corresponding constant symbolic expressions
7017 __isl_give isl_map
*isl_map_upper_bound_multi_pw_aff(__isl_take isl_map
*map
,
7018 __isl_take isl_multi_pw_aff
*upper
)
7020 return map_bound_multi_pw_aff(map
, upper
, &map_upper_bound_pw_aff
);
7023 /* Bound the given variable of "bset" from below (or above is "upper"
7024 * is set) to "value".
7026 static __isl_give isl_basic_set
*isl_basic_set_bound(
7027 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned pos
,
7028 isl_int value
, int upper
)
7030 return bset_from_bmap(basic_map_bound(bset_to_bmap(bset
),
7031 type
, pos
, value
, upper
));
7034 /* Bound the given variable of "bset" from below (or above is "upper"
7035 * is set) to "value".
7037 static __isl_give isl_basic_set
*isl_basic_set_bound_val(
7038 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned pos
,
7039 __isl_take isl_val
*value
, int upper
)
7043 if (!isl_val_is_int(value
))
7044 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
7045 "expecting integer value", goto error
);
7046 bset
= isl_basic_set_bound(bset
, type
, pos
, value
->n
, upper
);
7047 isl_val_free(value
);
7050 isl_val_free(value
);
7051 isl_basic_set_free(bset
);
7055 /* Bound the given variable of "bset" from below to "value".
7057 __isl_give isl_basic_set
*isl_basic_set_lower_bound_val(
7058 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned pos
,
7059 __isl_take isl_val
*value
)
7061 return isl_basic_set_bound_val(bset
, type
, pos
, value
, 0);
7064 /* Bound the given variable of "bset" from above to "value".
7066 __isl_give isl_basic_set
*isl_basic_set_upper_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
, 1);
7073 __isl_give isl_map
*isl_map_reverse(__isl_take isl_map
*map
)
7075 return isl_map_transform(map
, &isl_space_reverse
,
7076 &isl_basic_map_reverse
);
7079 /* Given a map A -> (B -> C), return the corresponding map A -> (C -> B).
7081 __isl_give isl_map
*isl_map_range_reverse(__isl_take isl_map
*map
)
7083 return isl_map_transform(map
, &isl_space_range_reverse
,
7084 &isl_basic_map_range_reverse
);
7088 #define TYPE isl_pw_multi_aff
7090 #define SUFFIX _pw_multi_aff
7092 #define EMPTY isl_pw_multi_aff_empty
7094 #define ADD isl_pw_multi_aff_union_add
7095 #include "isl_map_lexopt_templ.c"
7097 /* Given a map "map", compute the lexicographically minimal
7098 * (or maximal) image element for each domain element in dom,
7099 * in the form of an isl_pw_multi_aff.
7100 * If "empty" is not NULL, then set *empty to those elements in dom that
7101 * do not have an image element.
7102 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
7103 * should be computed over the domain of "map". "empty" is also NULL
7106 * We first compute the lexicographically minimal or maximal element
7107 * in the first basic map. This results in a partial solution "res"
7108 * and a subset "todo" of dom that still need to be handled.
7109 * We then consider each of the remaining maps in "map" and successively
7110 * update both "res" and "todo".
7111 * If "empty" is NULL, then the todo sets are not needed and therefore
7112 * also not computed.
7114 static __isl_give isl_pw_multi_aff
*isl_map_partial_lexopt_aligned_pw_multi_aff(
7115 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
7116 __isl_give isl_set
**empty
, unsigned flags
)
7120 isl_pw_multi_aff
*res
;
7123 full
= ISL_FL_ISSET(flags
, ISL_OPT_FULL
);
7124 if (!map
|| (!full
&& !dom
))
7127 if (isl_map_plain_is_empty(map
)) {
7132 return isl_pw_multi_aff_from_map(map
);
7135 res
= basic_map_partial_lexopt_pw_multi_aff(
7136 isl_basic_map_copy(map
->p
[0]),
7137 isl_set_copy(dom
), empty
, flags
);
7141 for (i
= 1; i
< map
->n
; ++i
) {
7142 isl_pw_multi_aff
*res_i
;
7144 res_i
= basic_map_partial_lexopt_pw_multi_aff(
7145 isl_basic_map_copy(map
->p
[i
]),
7146 isl_set_copy(dom
), empty
, flags
);
7148 if (ISL_FL_ISSET(flags
, ISL_OPT_MAX
))
7149 res
= isl_pw_multi_aff_union_lexmax(res
, res_i
);
7151 res
= isl_pw_multi_aff_union_lexmin(res
, res_i
);
7154 todo
= isl_set_intersect(todo
, *empty
);
7173 #define TYPE isl_map
7177 #define EMPTY isl_map_empty
7179 #define ADD isl_map_union_disjoint
7180 #include "isl_map_lexopt_templ.c"
7182 /* Given a map "map", compute the lexicographically minimal
7183 * (or maximal) image element for each domain element in "dom",
7184 * in the form of an isl_map.
7185 * If "empty" is not NULL, then set *empty to those elements in "dom" that
7186 * do not have an image element.
7187 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
7188 * should be computed over the domain of "map". "empty" is also NULL
7191 * If the input consists of more than one disjunct, then first
7192 * compute the desired result in the form of an isl_pw_multi_aff and
7193 * then convert that into an isl_map.
7195 * This function used to have an explicit implementation in terms
7196 * of isl_maps, but it would continually intersect the domains of
7197 * partial results with the complement of the domain of the next
7198 * partial solution, potentially leading to an explosion in the number
7199 * of disjuncts if there are several disjuncts in the input.
7200 * An even earlier implementation of this function would look for
7201 * better results in the domain of the partial result and for extra
7202 * results in the complement of this domain, which would lead to
7203 * even more splintering.
7205 static __isl_give isl_map
*isl_map_partial_lexopt_aligned(
7206 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
7207 __isl_give isl_set
**empty
, unsigned flags
)
7210 struct isl_map
*res
;
7211 isl_pw_multi_aff
*pma
;
7213 full
= ISL_FL_ISSET(flags
, ISL_OPT_FULL
);
7214 if (!map
|| (!full
&& !dom
))
7217 if (isl_map_plain_is_empty(map
)) {
7226 res
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[0]),
7232 pma
= isl_map_partial_lexopt_aligned_pw_multi_aff(map
, dom
, empty
,
7234 return isl_map_from_pw_multi_aff_internal(pma
);
7243 __isl_give isl_map
*isl_map_partial_lexmax(
7244 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
7245 __isl_give isl_set
**empty
)
7247 return isl_map_partial_lexopt(map
, dom
, empty
, ISL_OPT_MAX
);
7250 __isl_give isl_map
*isl_map_partial_lexmin(
7251 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
7252 __isl_give isl_set
**empty
)
7254 return isl_map_partial_lexopt(map
, dom
, empty
, 0);
7257 __isl_give isl_set
*isl_set_partial_lexmin(
7258 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
7259 __isl_give isl_set
**empty
)
7261 return set_from_map(isl_map_partial_lexmin(set_to_map(set
),
7265 __isl_give isl_set
*isl_set_partial_lexmax(
7266 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
7267 __isl_give isl_set
**empty
)
7269 return set_from_map(isl_map_partial_lexmax(set_to_map(set
),
7273 /* Compute the lexicographic minimum (or maximum if "flags" includes
7274 * ISL_OPT_MAX) of "bset" over its parametric domain.
7276 __isl_give isl_set
*isl_basic_set_lexopt(__isl_take isl_basic_set
*bset
,
7279 return isl_basic_map_lexopt(bset
, flags
);
7282 __isl_give isl_map
*isl_basic_map_lexmax(__isl_take isl_basic_map
*bmap
)
7284 return isl_basic_map_lexopt(bmap
, ISL_OPT_MAX
);
7287 __isl_give isl_set
*isl_basic_set_lexmin(__isl_take isl_basic_set
*bset
)
7289 return set_from_map(isl_basic_map_lexmin(bset_to_bmap(bset
)));
7292 __isl_give isl_set
*isl_basic_set_lexmax(__isl_take isl_basic_set
*bset
)
7294 return set_from_map(isl_basic_map_lexmax(bset_to_bmap(bset
)));
7297 /* Compute the lexicographic minimum of "bset" over its parametric domain
7298 * for the purpose of quantifier elimination.
7299 * That is, find an explicit representation for all the existentially
7300 * quantified variables in "bset" by computing their lexicographic
7303 static __isl_give isl_set
*isl_basic_set_lexmin_compute_divs(
7304 __isl_take isl_basic_set
*bset
)
7306 return isl_basic_set_lexopt(bset
, ISL_OPT_QE
);
7309 /* Given a basic map with one output dimension, compute the minimum or
7310 * maximum of that dimension as an isl_pw_aff.
7312 * Compute the optimum as a lexicographic optimum over the single
7313 * output dimension and extract the single isl_pw_aff from the result.
7315 static __isl_give isl_pw_aff
*basic_map_dim_opt(__isl_keep isl_basic_map
*bmap
,
7318 isl_pw_multi_aff
*pma
;
7321 bmap
= isl_basic_map_copy(bmap
);
7322 pma
= isl_basic_map_lexopt_pw_multi_aff(bmap
, max
? ISL_OPT_MAX
: 0);
7323 pwaff
= isl_pw_multi_aff_get_pw_aff(pma
, 0);
7324 isl_pw_multi_aff_free(pma
);
7329 /* Compute the minimum or maximum of the given output dimension
7330 * as a function of the parameters and the input dimensions,
7331 * but independently of the other output dimensions.
7333 * We first project out the other output dimension and then compute
7334 * the "lexicographic" maximum in each basic map, combining the results
7335 * using isl_pw_aff_union_max.
7337 static __isl_give isl_pw_aff
*map_dim_opt(__isl_take isl_map
*map
, int pos
,
7344 n_out
= isl_map_dim(map
, isl_dim_out
);
7346 map
= isl_map_free(map
);
7347 map
= isl_map_project_out(map
, isl_dim_out
, pos
+ 1, n_out
- (pos
+ 1));
7348 map
= isl_map_project_out(map
, isl_dim_out
, 0, pos
);
7353 isl_space
*space
= isl_map_get_space(map
);
7355 return isl_pw_aff_empty(space
);
7358 pwaff
= basic_map_dim_opt(map
->p
[0], max
);
7359 for (i
= 1; i
< map
->n
; ++i
) {
7360 isl_pw_aff
*pwaff_i
;
7362 pwaff_i
= basic_map_dim_opt(map
->p
[i
], max
);
7363 pwaff
= isl_pw_aff_union_opt(pwaff
, pwaff_i
, max
);
7371 /* Compute the minimum of the given output dimension as a function of the
7372 * parameters and input dimensions, but independently of
7373 * the other output dimensions.
7375 __isl_give isl_pw_aff
*isl_map_dim_min(__isl_take isl_map
*map
, int pos
)
7377 return map_dim_opt(map
, pos
, 0);
7380 /* Compute the maximum 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_max(__isl_take isl_map
*map
, int pos
)
7386 return map_dim_opt(map
, pos
, 1);
7389 /* Compute the minimum or maximum of the given set dimension
7390 * as a function of the parameters,
7391 * but independently of the other set dimensions.
7393 static __isl_give isl_pw_aff
*set_dim_opt(__isl_take isl_set
*set
, int pos
,
7396 return map_dim_opt(set
, pos
, max
);
7399 /* Compute the maximum of the given set dimension as a function of the
7400 * parameters, but independently of the other set dimensions.
7402 __isl_give isl_pw_aff
*isl_set_dim_max(__isl_take isl_set
*set
, int pos
)
7404 return set_dim_opt(set
, pos
, 1);
7407 /* Compute the minimum of the given set dimension as a function of the
7408 * parameters, but independently of the other set dimensions.
7410 __isl_give isl_pw_aff
*isl_set_dim_min(__isl_take isl_set
*set
, int pos
)
7412 return set_dim_opt(set
, pos
, 0);
7415 /* Apply a preimage specified by "mat" on the parameters of "bset".
7416 * bset is assumed to have only parameters and divs.
7418 static __isl_give isl_basic_set
*basic_set_parameter_preimage(
7419 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*mat
)
7423 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
7424 if (nparam
< 0 || !mat
)
7427 bset
->dim
= isl_space_cow(bset
->dim
);
7431 isl_assert(bset
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
7433 bset
->dim
->nparam
= 0;
7434 bset
->dim
->n_out
= nparam
;
7435 bset
= isl_basic_set_preimage(bset
, mat
);
7437 bset
->dim
->nparam
= bset
->dim
->n_out
;
7438 bset
->dim
->n_out
= 0;
7443 isl_basic_set_free(bset
);
7447 /* Apply a preimage specified by "mat" on the parameters of "set".
7448 * set is assumed to have only parameters and divs.
7450 static __isl_give isl_set
*set_parameter_preimage(__isl_take isl_set
*set
,
7451 __isl_take isl_mat
*mat
)
7456 nparam
= isl_set_dim(set
, isl_dim_param
);
7457 if (nparam
< 0 || !mat
)
7460 if (mat
->n_row
!= 1 + nparam
)
7461 isl_die(isl_set_get_ctx(set
), isl_error_internal
,
7462 "unexpected number of rows", goto error
);
7464 space
= isl_set_get_space(set
);
7465 space
= isl_space_move_dims(space
, isl_dim_set
, 0,
7466 isl_dim_param
, 0, nparam
);
7467 set
= isl_set_reset_space(set
, space
);
7468 set
= isl_set_preimage(set
, mat
);
7469 nparam
= isl_set_dim(set
, isl_dim_out
);
7471 set
= isl_set_free(set
);
7472 space
= isl_set_get_space(set
);
7473 space
= isl_space_move_dims(space
, isl_dim_param
, 0,
7474 isl_dim_out
, 0, nparam
);
7475 set
= isl_set_reset_space(set
, space
);
7483 /* Intersect the basic set "bset" with the affine space specified by the
7484 * equalities in "eq".
7486 static __isl_give isl_basic_set
*basic_set_append_equalities(
7487 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*eq
)
7495 bset
= isl_basic_set_extend(bset
, 0, eq
->n_row
, 0);
7499 len
= isl_basic_set_offset(bset
, isl_dim_div
) + bset
->extra
;
7500 for (i
= 0; i
< eq
->n_row
; ++i
) {
7501 k
= isl_basic_set_alloc_equality(bset
);
7504 isl_seq_cpy(bset
->eq
[k
], eq
->row
[i
], eq
->n_col
);
7505 isl_seq_clr(bset
->eq
[k
] + eq
->n_col
, len
- eq
->n_col
);
7509 bset
= isl_basic_set_gauss(bset
, NULL
);
7510 bset
= isl_basic_set_finalize(bset
);
7515 isl_basic_set_free(bset
);
7519 /* Intersect the set "set" with the affine space specified by the
7520 * equalities in "eq".
7522 static __isl_give isl_set
*set_append_equalities(__isl_take isl_set
*set
,
7523 __isl_take isl_mat
*eq
)
7530 for (i
= 0; i
< set
->n
; ++i
) {
7531 set
->p
[i
] = basic_set_append_equalities(set
->p
[i
],
7544 /* Given a basic set "bset" that only involves parameters and existentially
7545 * quantified variables, return the index of the first equality
7546 * that only involves parameters. If there is no such equality then
7547 * return bset->n_eq.
7549 * This function assumes that isl_basic_set_gauss has been called on "bset".
7551 static int first_parameter_equality(__isl_keep isl_basic_set
*bset
)
7554 isl_size nparam
, n_div
;
7556 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
7557 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
7558 if (nparam
< 0 || n_div
< 0)
7561 for (i
= 0, j
= n_div
- 1; i
< bset
->n_eq
&& j
>= 0; --j
) {
7562 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ j
]))
7569 /* Compute an explicit representation for the existentially quantified
7570 * variables in "bset" by computing the "minimal value" of the set
7571 * variables. Since there are no set variables, the computation of
7572 * the minimal value essentially computes an explicit representation
7573 * of the non-empty part(s) of "bset".
7575 * The input only involves parameters and existentially quantified variables.
7576 * All equalities among parameters have been removed.
7578 * Since the existentially quantified variables in the result are in general
7579 * going to be different from those in the input, we first replace
7580 * them by the minimal number of variables based on their equalities.
7581 * This should simplify the parametric integer programming.
7583 static __isl_give isl_set
*base_compute_divs(__isl_take isl_basic_set
*bset
)
7585 isl_morph
*morph1
, *morph2
;
7591 if (bset
->n_eq
== 0)
7592 return isl_basic_set_lexmin_compute_divs(bset
);
7594 morph1
= isl_basic_set_parameter_compression(bset
);
7595 bset
= isl_morph_basic_set(isl_morph_copy(morph1
), bset
);
7596 bset
= isl_basic_set_lift(bset
);
7597 morph2
= isl_basic_set_variable_compression(bset
, isl_dim_set
);
7598 bset
= isl_morph_basic_set(morph2
, bset
);
7599 n
= isl_basic_set_dim(bset
, isl_dim_set
);
7601 bset
= isl_basic_set_free(bset
);
7602 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
7604 set
= isl_basic_set_lexmin_compute_divs(bset
);
7606 set
= isl_morph_set(isl_morph_inverse(morph1
), set
);
7611 /* Project the given basic set onto its parameter domain, possibly introducing
7612 * new, explicit, existential variables in the constraints.
7613 * The input has parameters and (possibly implicit) existential variables.
7614 * The output has the same parameters, but only
7615 * explicit existentially quantified variables.
7617 * The actual projection is performed by pip, but pip doesn't seem
7618 * to like equalities very much, so we first remove the equalities
7619 * among the parameters by performing a variable compression on
7620 * the parameters. Afterward, an inverse transformation is performed
7621 * and the equalities among the parameters are inserted back in.
7623 * The variable compression on the parameters may uncover additional
7624 * equalities that were only implicit before. We therefore check
7625 * if there are any new parameter equalities in the result and
7626 * if so recurse. The removal of parameter equalities is required
7627 * for the parameter compression performed by base_compute_divs.
7629 static __isl_give isl_set
*parameter_compute_divs(
7630 __isl_take isl_basic_set
*bset
)
7634 struct isl_mat
*T
, *T2
;
7635 struct isl_set
*set
;
7638 bset
= isl_basic_set_cow(bset
);
7642 if (bset
->n_eq
== 0)
7643 return base_compute_divs(bset
);
7645 bset
= isl_basic_set_gauss(bset
, NULL
);
7648 if (isl_basic_set_plain_is_empty(bset
))
7649 return isl_set_from_basic_set(bset
);
7651 i
= first_parameter_equality(bset
);
7652 if (i
== bset
->n_eq
)
7653 return base_compute_divs(bset
);
7655 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
7657 return isl_set_from_basic_set(isl_basic_set_free(bset
));
7658 eq
= isl_mat_sub_alloc6(bset
->ctx
, bset
->eq
, i
, bset
->n_eq
- i
,
7660 eq
= isl_mat_cow(eq
);
7661 T
= isl_mat_variable_compression(isl_mat_copy(eq
), &T2
);
7662 if (T
&& T
->n_col
== 0) {
7666 bset
= isl_basic_set_set_to_empty(bset
);
7667 return isl_set_from_basic_set(bset
);
7669 bset
= basic_set_parameter_preimage(bset
, T
);
7671 i
= first_parameter_equality(bset
);
7674 else if (i
== bset
->n_eq
)
7675 set
= base_compute_divs(bset
);
7677 set
= parameter_compute_divs(bset
);
7678 set
= set_parameter_preimage(set
, T2
);
7679 set
= set_append_equalities(set
, eq
);
7683 /* Insert the divs from "ls" before those of "bmap".
7685 * The number of columns is not changed, which means that the last
7686 * dimensions of "bmap" are being reintepreted as the divs from "ls".
7687 * The caller is responsible for removing the same number of dimensions
7688 * from the space of "bmap".
7690 static __isl_give isl_basic_map
*insert_divs_from_local_space(
7691 __isl_take isl_basic_map
*bmap
, __isl_keep isl_local_space
*ls
)
7697 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
7699 return isl_basic_map_free(bmap
);
7703 old_n_div
= bmap
->n_div
;
7704 bmap
= insert_div_rows(bmap
, n_div
);
7708 for (i
= 0; i
< n_div
; ++i
) {
7709 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], ls
->div
->n_col
);
7710 isl_seq_clr(bmap
->div
[i
] + ls
->div
->n_col
, old_n_div
);
7716 /* Replace the space of "bmap" by the space and divs of "ls".
7718 * If "ls" has any divs, then we simplify the result since we may
7719 * have discovered some additional equalities that could simplify
7720 * the div expressions.
7722 static __isl_give isl_basic_map
*basic_replace_space_by_local_space(
7723 __isl_take isl_basic_map
*bmap
, __isl_take isl_local_space
*ls
)
7727 bmap
= isl_basic_map_cow(bmap
);
7728 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
7729 if (!bmap
|| n_div
< 0)
7732 bmap
= insert_divs_from_local_space(bmap
, ls
);
7736 isl_space_free(bmap
->dim
);
7737 bmap
->dim
= isl_local_space_get_space(ls
);
7741 isl_local_space_free(ls
);
7743 bmap
= isl_basic_map_simplify(bmap
);
7744 bmap
= isl_basic_map_finalize(bmap
);
7747 isl_basic_map_free(bmap
);
7748 isl_local_space_free(ls
);
7752 /* Replace the space of "map" by the space and divs of "ls".
7754 static __isl_give isl_map
*replace_space_by_local_space(__isl_take isl_map
*map
,
7755 __isl_take isl_local_space
*ls
)
7759 map
= isl_map_cow(map
);
7763 for (i
= 0; i
< map
->n
; ++i
) {
7764 map
->p
[i
] = basic_replace_space_by_local_space(map
->p
[i
],
7765 isl_local_space_copy(ls
));
7769 isl_space_free(isl_map_take_space(map
));
7770 map
= isl_map_restore_space(map
, isl_local_space_get_space(ls
));
7772 isl_local_space_free(ls
);
7775 isl_local_space_free(ls
);
7780 /* Compute an explicit representation for the existentially
7781 * quantified variables for which do not know any explicit representation yet.
7783 * We first sort the existentially quantified variables so that the
7784 * existentially quantified variables for which we already have an explicit
7785 * representation are placed before those for which we do not.
7786 * The input dimensions, the output dimensions and the existentially
7787 * quantified variables for which we already have an explicit
7788 * representation are then turned into parameters.
7789 * compute_divs returns a map with the same parameters and
7790 * no input or output dimensions and the dimension specification
7791 * is reset to that of the input, including the existentially quantified
7792 * variables for which we already had an explicit representation.
7794 static __isl_give isl_map
*compute_divs(__isl_take isl_basic_map
*bmap
)
7796 struct isl_basic_set
*bset
;
7797 struct isl_set
*set
;
7798 struct isl_map
*map
;
7800 isl_local_space
*ls
;
7807 bmap
= isl_basic_map_sort_divs(bmap
);
7808 bmap
= isl_basic_map_cow(bmap
);
7812 n_known
= isl_basic_map_first_unknown_div(bmap
);
7813 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
7814 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
7815 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
7816 if (n_known
< 0 || nparam
< 0 || n_in
< 0 || n_out
< 0)
7817 return isl_map_from_basic_map(isl_basic_map_free(bmap
));
7819 space
= isl_space_set_alloc(bmap
->ctx
,
7820 nparam
+ n_in
+ n_out
+ n_known
, 0);
7824 ls
= isl_basic_map_get_local_space(bmap
);
7825 ls
= isl_local_space_drop_dims(ls
, isl_dim_div
,
7826 n_known
, bmap
->n_div
- n_known
);
7828 for (i
= n_known
; i
< bmap
->n_div
; ++i
)
7829 swap_div(bmap
, i
- n_known
, i
);
7830 bmap
->n_div
-= n_known
;
7831 bmap
->extra
-= n_known
;
7833 bmap
= isl_basic_map_reset_space(bmap
, space
);
7834 bset
= bset_from_bmap(bmap
);
7836 set
= parameter_compute_divs(bset
);
7837 map
= set_to_map(set
);
7838 map
= replace_space_by_local_space(map
, ls
);
7842 isl_basic_map_free(bmap
);
7846 /* Remove the explicit representation of local variable "div",
7849 __isl_give isl_basic_map
*isl_basic_map_mark_div_unknown(
7850 __isl_take isl_basic_map
*bmap
, int div
)
7854 unknown
= isl_basic_map_div_is_marked_unknown(bmap
, div
);
7856 return isl_basic_map_free(bmap
);
7860 bmap
= isl_basic_map_cow(bmap
);
7863 isl_int_set_si(bmap
->div
[div
][0], 0);
7867 /* Is local variable "div" of "bmap" marked as not having an explicit
7869 * Note that even if "div" is not marked in this way and therefore
7870 * has an explicit representation, this representation may still
7871 * depend (indirectly) on other local variables that do not
7872 * have an explicit representation.
7874 isl_bool
isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map
*bmap
,
7877 if (isl_basic_map_check_range(bmap
, isl_dim_div
, div
, 1) < 0)
7878 return isl_bool_error
;
7879 return isl_int_is_zero(bmap
->div
[div
][0]);
7882 /* Return the position of the first local variable that does not
7883 * have an explicit representation.
7884 * Return the total number of local variables if they all have
7885 * an explicit representation.
7886 * Return -1 on error.
7888 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map
*bmap
)
7895 for (i
= 0; i
< bmap
->n_div
; ++i
) {
7896 if (!isl_basic_map_div_is_known(bmap
, i
))
7902 /* Return the position of the first local variable that does not
7903 * have an explicit representation.
7904 * Return the total number of local variables if they all have
7905 * an explicit representation.
7906 * Return -1 on error.
7908 int isl_basic_set_first_unknown_div(__isl_keep isl_basic_set
*bset
)
7910 return isl_basic_map_first_unknown_div(bset
);
7913 /* Does "bmap" have an explicit representation for all local variables?
7915 isl_bool
isl_basic_map_divs_known(__isl_keep isl_basic_map
*bmap
)
7920 n
= isl_basic_map_dim(bmap
, isl_dim_div
);
7921 first
= isl_basic_map_first_unknown_div(bmap
);
7922 if (n
< 0 || first
< 0)
7923 return isl_bool_error
;
7927 /* Do all basic maps in "map" have an explicit representation
7928 * for all local variables?
7930 isl_bool
isl_map_divs_known(__isl_keep isl_map
*map
)
7935 return isl_bool_error
;
7937 for (i
= 0; i
< map
->n
; ++i
) {
7938 int known
= isl_basic_map_divs_known(map
->p
[i
]);
7943 return isl_bool_true
;
7946 /* If bmap contains any unknown divs, then compute explicit
7947 * expressions for them. However, this computation may be
7948 * quite expensive, so first try to remove divs that aren't
7951 __isl_give isl_map
*isl_basic_map_compute_divs(__isl_take isl_basic_map
*bmap
)
7954 struct isl_map
*map
;
7956 known
= isl_basic_map_divs_known(bmap
);
7960 return isl_map_from_basic_map(bmap
);
7962 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
7964 known
= isl_basic_map_divs_known(bmap
);
7968 return isl_map_from_basic_map(bmap
);
7970 map
= compute_divs(bmap
);
7973 isl_basic_map_free(bmap
);
7977 __isl_give isl_map
*isl_map_compute_divs(__isl_take isl_map
*map
)
7981 struct isl_map
*res
;
7988 known
= isl_map_divs_known(map
);
7996 res
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[0]));
7997 for (i
= 1 ; i
< map
->n
; ++i
) {
7999 r2
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[i
]));
8000 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
8001 res
= isl_map_union_disjoint(res
, r2
);
8003 res
= isl_map_union(res
, r2
);
8010 __isl_give isl_set
*isl_basic_set_compute_divs(__isl_take isl_basic_set
*bset
)
8012 return set_from_map(isl_basic_map_compute_divs(bset_to_bmap(bset
)));
8015 __isl_give isl_set
*isl_set_compute_divs(__isl_take isl_set
*set
)
8017 return set_from_map(isl_map_compute_divs(set_to_map(set
)));
8020 __isl_give isl_set
*isl_map_domain(__isl_take isl_map
*map
)
8023 struct isl_set
*set
;
8028 map
= isl_map_cow(map
);
8032 set
= set_from_map(map
);
8033 set
->dim
= isl_space_domain(set
->dim
);
8036 for (i
= 0; i
< map
->n
; ++i
) {
8037 set
->p
[i
] = isl_basic_map_domain(map
->p
[i
]);
8041 ISL_F_CLR(set
, ISL_MAP_DISJOINT
);
8042 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
8049 /* Return the union of "map1" and "map2", where we assume for now that
8050 * "map1" and "map2" are disjoint. Note that the basic maps inside
8051 * "map1" or "map2" may not be disjoint from each other.
8052 * Also note that this function is also called from isl_map_union,
8053 * which takes care of handling the situation where "map1" and "map2"
8054 * may not be disjoint.
8056 * If one of the inputs is empty, we can simply return the other input.
8057 * Similarly, if one of the inputs is universal, then it is equal to the union.
8059 static __isl_give isl_map
*map_union_disjoint(__isl_take isl_map
*map1
,
8060 __isl_take isl_map
*map2
)
8064 struct isl_map
*map
= NULL
;
8067 if (isl_map_check_equal_space(map1
, map2
) < 0)
8079 is_universe
= isl_map_plain_is_universe(map1
);
8080 if (is_universe
< 0)
8087 is_universe
= isl_map_plain_is_universe(map2
);
8088 if (is_universe
< 0)
8095 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
8096 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
8097 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
8099 map
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
8100 map1
->n
+ map2
->n
, flags
);
8103 for (i
= 0; i
< map1
->n
; ++i
) {
8104 map
= isl_map_add_basic_map(map
,
8105 isl_basic_map_copy(map1
->p
[i
]));
8109 for (i
= 0; i
< map2
->n
; ++i
) {
8110 map
= isl_map_add_basic_map(map
,
8111 isl_basic_map_copy(map2
->p
[i
]));
8125 /* Return the union of "map1" and "map2", where "map1" and "map2" are
8126 * guaranteed to be disjoint by the caller.
8128 * Note that this functions is called from within isl_map_make_disjoint,
8129 * so we have to be careful not to touch the constraints of the inputs
8132 __isl_give isl_map
*isl_map_union_disjoint(__isl_take isl_map
*map1
,
8133 __isl_take isl_map
*map2
)
8135 isl_map_align_params_bin(&map1
, &map2
);
8136 return map_union_disjoint(map1
, map2
);
8139 /* Return the union of "map1" and "map2", where "map1" and "map2" may
8142 * We currently simply call map_union_disjoint, the internal operation
8143 * of which does not really depend on the inputs being disjoint.
8144 * If the result contains more than one basic map, then we clear
8145 * the disjoint flag since the result may contain basic maps from
8146 * both inputs and these are not guaranteed to be disjoint.
8148 * As a special case, if "map1" and "map2" are obviously equal,
8149 * then we simply return "map1".
8151 __isl_give isl_map
*isl_map_union(__isl_take isl_map
*map1
,
8152 __isl_take isl_map
*map2
)
8156 if (isl_map_align_params_bin(&map1
, &map2
) < 0)
8159 equal
= isl_map_plain_is_equal(map1
, map2
);
8167 map1
= map_union_disjoint(map1
, map2
);
8171 ISL_F_CLR(map1
, ISL_MAP_DISJOINT
);
8179 __isl_give isl_set
*isl_set_union_disjoint(
8180 __isl_take isl_set
*set1
, __isl_take isl_set
*set2
)
8182 return set_from_map(isl_map_union_disjoint(set_to_map(set1
),
8186 __isl_give isl_set
*isl_set_union(__isl_take isl_set
*set1
,
8187 __isl_take isl_set
*set2
)
8189 return set_from_map(isl_map_union(set_to_map(set1
), set_to_map(set2
)));
8192 /* Apply "fn" to pairs of elements from "map" and "set" and collect
8193 * the results in a map living in "space".
8195 * "map" and "set" are assumed to be compatible and non-NULL.
8197 static __isl_give isl_map
*map_intersect_set(__isl_take isl_map
*map
,
8198 __isl_take isl_space
*space
, __isl_take isl_set
*set
,
8199 __isl_give isl_basic_map
*fn(__isl_take isl_basic_map
*bmap
,
8200 __isl_take isl_basic_set
*bset
))
8203 struct isl_map
*result
;
8206 if (isl_set_plain_is_universe(set
)) {
8208 return isl_map_reset_equal_dim_space(map
, space
);
8211 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
) &&
8212 ISL_F_ISSET(set
, ISL_MAP_DISJOINT
))
8213 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
8215 result
= isl_map_alloc_space(space
, map
->n
* set
->n
, flags
);
8216 for (i
= 0; result
&& i
< map
->n
; ++i
)
8217 for (j
= 0; j
< set
->n
; ++j
) {
8218 result
= isl_map_add_basic_map(result
,
8219 fn(isl_basic_map_copy(map
->p
[i
]),
8220 isl_basic_set_copy(set
->p
[j
])));
8230 __isl_give isl_map
*isl_map_intersect_range(__isl_take isl_map
*map
,
8231 __isl_take isl_set
*set
)
8236 isl_map_align_params_set(&map
, &set
);
8237 ok
= isl_map_compatible_range(map
, set
);
8241 isl_die(set
->ctx
, isl_error_invalid
,
8242 "incompatible spaces", goto error
);
8244 space
= isl_map_get_space(map
);
8245 return map_intersect_set(map
, space
, set
,
8246 &isl_basic_map_intersect_range
);
8253 /* Intersect the domain of "map" with "set".
8255 * If the domain dimensions of "map" do not have any identifiers,
8256 * then copy them over from "set".
8258 __isl_give isl_map
*isl_map_intersect_domain(__isl_take isl_map
*map
,
8259 __isl_take isl_set
*set
)
8264 isl_map_align_params_set(&map
, &set
);
8265 ok
= isl_map_compatible_domain(map
, set
);
8269 isl_die(set
->ctx
, isl_error_invalid
,
8270 "incompatible spaces", goto error
);
8272 space
= isl_map_get_space(map
);
8273 space
= isl_space_copy_ids_if_unset(space
, isl_dim_in
,
8274 isl_set_peek_space(set
), isl_dim_set
);
8275 return map_intersect_set(map
, space
, set
,
8276 &isl_basic_map_intersect_domain
);
8283 /* Data structure that specifies how isl_map_intersect_factor
8286 * "preserve_type" is the tuple where the factor differs from
8287 * the input map and of which the identifiers needs
8288 * to be preserved explicitly.
8289 * "other_factor" is used to extract the space of the other factor
8290 * from the space of the product ("map").
8291 * "product" is used to combine the given factor and a universe map
8292 * in the space returned by "other_factor" to produce a map
8293 * that lives in the same space as the input map.
8295 struct isl_intersect_factor_control
{
8296 enum isl_dim_type preserve_type
;
8297 __isl_give isl_space
*(*other_factor
)(__isl_take isl_space
*space
);
8298 __isl_give isl_map
*(*product
)(__isl_take isl_map
*factor
,
8299 __isl_take isl_map
*other
);
8302 /* Given a map "map" in some product space and a map "factor"
8303 * living in some factor space, return the intersection.
8305 * After aligning the parameters,
8306 * the map "factor" is first extended to a map living in the same space
8307 * as "map" and then a regular intersection is computed.
8309 * Note that the extension is computed as a product, which is anonymous
8310 * by default. If "map" has an identifier on the corresponding tuple,
8311 * then this identifier needs to be set on the product
8312 * before the intersection is computed.
8314 static __isl_give isl_map
*isl_map_intersect_factor(
8315 __isl_take isl_map
*map
, __isl_take isl_map
*factor
,
8316 struct isl_intersect_factor_control
*control
)
8318 isl_bool equal
, has_id
;
8321 isl_map
*other
, *product
;
8323 equal
= isl_map_has_equal_params(map
, factor
);
8327 map
= isl_map_align_params(map
, isl_map_get_space(factor
));
8328 factor
= isl_map_align_params(factor
, isl_map_get_space(map
));
8331 space
= isl_map_get_space(map
);
8332 has_id
= isl_space_has_tuple_id(space
, control
->preserve_type
);
8334 space
= isl_space_free(space
);
8336 id
= isl_space_get_tuple_id(space
, control
->preserve_type
);
8338 other
= isl_map_universe(control
->other_factor(space
));
8339 product
= control
->product(factor
, other
);
8341 if (has_id
>= 0 && has_id
)
8342 product
= isl_map_set_tuple_id(product
,
8343 control
->preserve_type
, id
);
8345 return map_intersect(map
, product
);
8348 isl_map_free(factor
);
8352 /* Return the domain product of "map2" and "map1".
8354 static __isl_give isl_map
*isl_map_reverse_domain_product(
8355 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
8357 return isl_map_domain_product(map2
, map1
);
8360 /* Return the range product of "map2" and "map1".
8362 static __isl_give isl_map
*isl_map_reverse_range_product(
8363 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
8365 return isl_map_range_product(map2
, map1
);
8368 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
8369 * in the space B -> C, return the intersection.
8371 __isl_give isl_map
*isl_map_intersect_domain_factor_range(
8372 __isl_take isl_map
*map
, __isl_take isl_map
*factor
)
8374 struct isl_intersect_factor_control control
= {
8375 .preserve_type
= isl_dim_in
,
8376 .other_factor
= isl_space_domain_factor_domain
,
8377 .product
= isl_map_reverse_domain_product
,
8380 return isl_map_intersect_factor(map
, factor
, &control
);
8383 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
8384 * in the space A -> B, return the intersection.
8386 __isl_give isl_map
*isl_map_intersect_range_factor_domain(
8387 __isl_take isl_map
*map
, __isl_take isl_map
*factor
)
8389 struct isl_intersect_factor_control control
= {
8390 .preserve_type
= isl_dim_out
,
8391 .other_factor
= isl_space_range_factor_range
,
8392 .product
= isl_map_range_product
,
8395 return isl_map_intersect_factor(map
, factor
, &control
);
8398 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
8399 * in the space A -> C, return the intersection.
8401 __isl_give isl_map
*isl_map_intersect_range_factor_range(
8402 __isl_take isl_map
*map
, __isl_take isl_map
*factor
)
8404 struct isl_intersect_factor_control control
= {
8405 .preserve_type
= isl_dim_out
,
8406 .other_factor
= isl_space_range_factor_domain
,
8407 .product
= isl_map_reverse_range_product
,
8410 return isl_map_intersect_factor(map
, factor
, &control
);
8413 /* Given a set "set" in a space [A -> B] and a set "domain"
8414 * in the space A, return the intersection.
8416 * The set "domain" is first extended to a set living in the space
8417 * [A -> B] and then a regular intersection is computed.
8419 __isl_give isl_set
*isl_set_intersect_factor_domain(__isl_take isl_set
*set
,
8420 __isl_take isl_set
*domain
)
8422 struct isl_intersect_factor_control control
= {
8423 .preserve_type
= isl_dim_set
,
8424 .other_factor
= isl_space_factor_range
,
8425 .product
= isl_map_range_product
,
8428 return set_from_map(isl_map_intersect_factor(set_to_map(set
),
8429 set_to_map(domain
), &control
));
8432 /* Given a set "set" in a space [A -> B] and a set "range"
8433 * in the space B, return the intersection.
8435 * The set "range" is first extended to a set living in the space
8436 * [A -> B] and then a regular intersection is computed.
8438 __isl_give isl_set
*isl_set_intersect_factor_range(__isl_take isl_set
*set
,
8439 __isl_take isl_set
*range
)
8441 struct isl_intersect_factor_control control
= {
8442 .preserve_type
= isl_dim_set
,
8443 .other_factor
= isl_space_factor_domain
,
8444 .product
= isl_map_reverse_range_product
,
8447 return set_from_map(isl_map_intersect_factor(set_to_map(set
),
8448 set_to_map(range
), &control
));
8451 __isl_give isl_map
*isl_map_apply_domain(__isl_take isl_map
*map1
,
8452 __isl_take isl_map
*map2
)
8454 if (isl_map_align_params_bin(&map1
, &map2
) < 0)
8456 map1
= isl_map_reverse(map1
);
8457 map1
= isl_map_apply_range(map1
, map2
);
8458 return isl_map_reverse(map1
);
8465 __isl_give isl_map
*isl_map_apply_range(__isl_take isl_map
*map1
,
8466 __isl_take isl_map
*map2
)
8469 struct isl_map
*result
;
8472 if (isl_map_align_params_bin(&map1
, &map2
) < 0)
8475 space
= isl_space_join(isl_space_copy(map1
->dim
),
8476 isl_space_copy(map2
->dim
));
8478 result
= isl_map_alloc_space(space
, map1
->n
* map2
->n
, 0);
8481 for (i
= 0; i
< map1
->n
; ++i
)
8482 for (j
= 0; j
< map2
->n
; ++j
) {
8483 result
= isl_map_add_basic_map(result
,
8484 isl_basic_map_apply_range(
8485 isl_basic_map_copy(map1
->p
[i
]),
8486 isl_basic_map_copy(map2
->p
[j
])));
8492 if (result
&& result
->n
<= 1)
8493 ISL_F_SET(result
, ISL_MAP_DISJOINT
);
8501 /* Is "bmap" a transformation, i.e.,
8502 * does it relate elements from the same space.
8504 isl_bool
isl_basic_map_is_transformation(__isl_keep isl_basic_map
*bmap
)
8508 space
= isl_basic_map_peek_space(bmap
);
8509 return isl_space_tuple_is_equal(space
, isl_dim_in
, space
, isl_dim_out
);
8512 /* Check that "bmap" is a transformation, i.e.,
8513 * that it relates elements from the same space.
8515 static isl_stat
isl_basic_map_check_transformation(
8516 __isl_keep isl_basic_map
*bmap
)
8520 equal
= isl_basic_map_is_transformation(bmap
);
8522 return isl_stat_error
;
8524 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
8525 "domain and range don't match", return isl_stat_error
);
8530 * returns range - domain
8532 __isl_give isl_basic_set
*isl_basic_map_deltas(__isl_take isl_basic_map
*bmap
)
8534 isl_space
*target_space
;
8535 struct isl_basic_set
*bset
;
8541 if (isl_basic_map_check_transformation(bmap
) < 0)
8542 return isl_basic_map_free(bmap
);
8543 dim
= isl_basic_map_dim(bmap
, isl_dim_in
);
8544 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
8545 if (dim
< 0 || nparam
< 0)
8547 target_space
= isl_space_domain(isl_basic_map_get_space(bmap
));
8548 bmap
= isl_basic_map_from_range(isl_basic_map_wrap(bmap
));
8549 bmap
= isl_basic_map_add_dims(bmap
, isl_dim_in
, dim
);
8550 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
8552 bmap
= isl_basic_map_free(bmap
);
8553 bmap
= isl_basic_map_extend_constraints(bmap
, dim
, 0);
8554 for (i
= 0; i
< dim
; ++i
) {
8555 int j
= isl_basic_map_alloc_equality(bmap
);
8557 bmap
= isl_basic_map_free(bmap
);
8560 isl_seq_clr(bmap
->eq
[j
], 1 + total
);
8561 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
8562 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], 1);
8563 isl_int_set_si(bmap
->eq
[j
][1+nparam
+2*dim
+i
], -1);
8565 bset
= isl_basic_map_domain(bmap
);
8566 bset
= isl_basic_set_reset_space(bset
, target_space
);
8569 isl_basic_map_free(bmap
);
8573 /* Is the tuple of type "type1" of "map" the same as
8574 * the tuple of type "type2" of "space"?
8576 isl_bool
isl_map_space_tuple_is_equal(__isl_keep isl_map
*map
,
8577 enum isl_dim_type type1
, __isl_keep isl_space
*space
,
8578 enum isl_dim_type type2
)
8580 isl_space
*map_space
;
8582 map_space
= isl_map_peek_space(map
);
8583 return isl_space_tuple_is_equal(map_space
, type1
, space
, type2
);
8586 /* Is the tuple of type "type1" of "map1" the same as
8587 * the tuple of type "type2" of "map2"?
8589 isl_bool
isl_map_tuple_is_equal(__isl_keep isl_map
*map1
,
8590 enum isl_dim_type type1
, __isl_keep isl_map
*map2
,
8591 enum isl_dim_type type2
)
8593 isl_space
*space1
, *space2
;
8595 space1
= isl_map_peek_space(map1
);
8596 space2
= isl_map_peek_space(map2
);
8597 return isl_space_tuple_is_equal(space1
, type1
, space2
, type2
);
8600 /* Check that "map" is a transformation, i.e.,
8601 * that it relates elements from the same space.
8603 isl_stat
isl_map_check_transformation(__isl_keep isl_map
*map
)
8607 equal
= isl_map_tuple_is_equal(map
, isl_dim_in
, map
, isl_dim_out
);
8609 return isl_stat_error
;
8611 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
8612 "domain and range don't match", return isl_stat_error
);
8617 * returns range - domain
8619 __isl_give isl_set
*isl_map_deltas(__isl_take isl_map
*map
)
8623 struct isl_set
*result
;
8625 if (isl_map_check_transformation(map
) < 0)
8627 space
= isl_map_get_space(map
);
8628 space
= isl_space_domain(space
);
8629 result
= isl_set_alloc_space(space
, map
->n
, 0);
8632 for (i
= 0; i
< map
->n
; ++i
)
8633 result
= isl_set_add_basic_set(result
,
8634 isl_basic_map_deltas(isl_basic_map_copy(map
->p
[i
])));
8643 * returns [domain -> range] -> range - domain
8645 __isl_give isl_basic_map
*isl_basic_map_deltas_map(
8646 __isl_take isl_basic_map
*bmap
)
8650 isl_basic_map
*domain
;
8654 if (isl_basic_map_check_transformation(bmap
) < 0)
8655 return isl_basic_map_free(bmap
);
8657 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
8658 n
= isl_basic_map_dim(bmap
, isl_dim_in
);
8659 if (nparam
< 0 || n
< 0)
8660 return isl_basic_map_free(bmap
);
8662 space
= isl_basic_map_get_space(bmap
);
8663 space
= isl_space_from_range(isl_space_domain(space
));
8664 domain
= isl_basic_map_universe(space
);
8666 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
8667 bmap
= isl_basic_map_apply_range(bmap
, domain
);
8668 bmap
= isl_basic_map_extend_constraints(bmap
, n
, 0);
8670 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
8672 return isl_basic_map_free(bmap
);
8674 for (i
= 0; i
< n
; ++i
) {
8675 k
= isl_basic_map_alloc_equality(bmap
);
8678 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
8679 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], 1);
8680 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ i
], -1);
8681 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ n
+ i
], 1);
8684 bmap
= isl_basic_map_gauss(bmap
, NULL
);
8685 return isl_basic_map_finalize(bmap
);
8687 isl_basic_map_free(bmap
);
8692 * returns [domain -> range] -> range - domain
8694 __isl_give isl_map
*isl_map_deltas_map(__isl_take isl_map
*map
)
8696 if (isl_map_check_transformation(map
) < 0)
8697 return isl_map_free(map
);
8699 return isl_map_transform(map
, &isl_space_range_map
,
8700 &isl_basic_map_deltas_map
);
8703 __isl_give isl_basic_map
*isl_basic_map_identity(__isl_take isl_space
*space
)
8705 isl_size n_in
, n_out
;
8707 n_in
= isl_space_dim(space
, isl_dim_in
);
8708 n_out
= isl_space_dim(space
, isl_dim_out
);
8709 if (n_in
< 0 || n_out
< 0)
8712 isl_die(space
->ctx
, isl_error_invalid
,
8713 "number of input and output dimensions needs to be "
8714 "the same", goto error
);
8715 return isl_basic_map_equal(space
, n_in
);
8717 isl_space_free(space
);
8721 __isl_give isl_map
*isl_map_identity(__isl_take isl_space
*space
)
8723 return isl_map_from_basic_map(isl_basic_map_identity(space
));
8726 __isl_give isl_map
*isl_set_identity(__isl_take isl_set
*set
)
8728 isl_space
*space
= isl_set_get_space(set
);
8730 id
= isl_map_identity(isl_space_map_from_set(space
));
8731 return isl_map_intersect_range(id
, set
);
8734 /* Construct a basic set with all set dimensions having only non-negative
8737 __isl_give isl_basic_set
*isl_basic_set_positive_orthant(
8738 __isl_take isl_space
*space
)
8744 struct isl_basic_set
*bset
;
8746 nparam
= isl_space_dim(space
, isl_dim_param
);
8747 dim
= isl_space_dim(space
, isl_dim_set
);
8748 total
= isl_space_dim(space
, isl_dim_all
);
8749 if (nparam
< 0 || dim
< 0 || total
< 0)
8750 space
= isl_space_free(space
);
8751 bset
= isl_basic_set_alloc_space(space
, 0, 0, dim
);
8754 for (i
= 0; i
< dim
; ++i
) {
8755 int k
= isl_basic_set_alloc_inequality(bset
);
8758 isl_seq_clr(bset
->ineq
[k
], 1 + total
);
8759 isl_int_set_si(bset
->ineq
[k
][1 + nparam
+ i
], 1);
8763 isl_basic_set_free(bset
);
8767 /* Construct the half-space x_pos >= 0.
8769 static __isl_give isl_basic_set
*nonneg_halfspace(__isl_take isl_space
*space
,
8774 isl_basic_set
*nonneg
;
8776 total
= isl_space_dim(space
, isl_dim_all
);
8778 space
= isl_space_free(space
);
8779 nonneg
= isl_basic_set_alloc_space(space
, 0, 0, 1);
8780 k
= isl_basic_set_alloc_inequality(nonneg
);
8783 isl_seq_clr(nonneg
->ineq
[k
], 1 + total
);
8784 isl_int_set_si(nonneg
->ineq
[k
][pos
], 1);
8786 return isl_basic_set_finalize(nonneg
);
8788 isl_basic_set_free(nonneg
);
8792 /* Construct the half-space x_pos <= -1.
8794 static __isl_give isl_basic_set
*neg_halfspace(__isl_take isl_space
*space
,
8801 total
= isl_space_dim(space
, isl_dim_all
);
8803 space
= isl_space_free(space
);
8804 neg
= isl_basic_set_alloc_space(space
, 0, 0, 1);
8805 k
= isl_basic_set_alloc_inequality(neg
);
8808 isl_seq_clr(neg
->ineq
[k
], 1 + total
);
8809 isl_int_set_si(neg
->ineq
[k
][0], -1);
8810 isl_int_set_si(neg
->ineq
[k
][pos
], -1);
8812 return isl_basic_set_finalize(neg
);
8814 isl_basic_set_free(neg
);
8818 __isl_give isl_set
*isl_set_split_dims(__isl_take isl_set
*set
,
8819 enum isl_dim_type type
, unsigned first
, unsigned n
)
8823 isl_basic_set
*nonneg
;
8829 if (isl_set_check_range(set
, type
, first
, n
) < 0)
8830 return isl_set_free(set
);
8832 offset
= pos(set
->dim
, type
);
8833 for (i
= 0; i
< n
; ++i
) {
8834 nonneg
= nonneg_halfspace(isl_set_get_space(set
),
8835 offset
+ first
+ i
);
8836 neg
= neg_halfspace(isl_set_get_space(set
), offset
+ first
+ i
);
8838 set
= isl_set_intersect(set
, isl_basic_set_union(nonneg
, neg
));
8844 static isl_stat
foreach_orthant(__isl_take isl_set
*set
, int *signs
, int first
,
8846 isl_stat (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
8852 return isl_stat_error
;
8853 if (isl_set_plain_is_empty(set
)) {
8858 return fn(set
, signs
, user
);
8861 half
= isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set
),
8863 half
= isl_set_intersect(half
, isl_set_copy(set
));
8864 if (foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
) < 0)
8868 half
= isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set
),
8870 half
= isl_set_intersect(half
, set
);
8871 return foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
);
8874 return isl_stat_error
;
8877 /* Call "fn" on the intersections of "set" with each of the orthants
8878 * (except for obviously empty intersections). The orthant is identified
8879 * by the signs array, with each entry having value 1 or -1 according
8880 * to the sign of the corresponding variable.
8882 isl_stat
isl_set_foreach_orthant(__isl_keep isl_set
*set
,
8883 isl_stat (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
8892 return isl_stat_error
;
8893 if (isl_set_plain_is_empty(set
))
8896 nparam
= isl_set_dim(set
, isl_dim_param
);
8897 nvar
= isl_set_dim(set
, isl_dim_set
);
8898 if (nparam
< 0 || nvar
< 0)
8899 return isl_stat_error
;
8901 signs
= isl_alloc_array(set
->ctx
, int, nparam
+ nvar
);
8903 r
= foreach_orthant(isl_set_copy(set
), signs
, 0, nparam
+ nvar
,
8911 isl_bool
isl_set_is_equal(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
8913 return isl_map_is_equal(set_to_map(set1
), set_to_map(set2
));
8916 isl_bool
isl_basic_map_is_subset(__isl_keep isl_basic_map
*bmap1
,
8917 __isl_keep isl_basic_map
*bmap2
)
8920 struct isl_map
*map1
;
8921 struct isl_map
*map2
;
8923 if (!bmap1
|| !bmap2
)
8924 return isl_bool_error
;
8926 map1
= isl_map_from_basic_map(isl_basic_map_copy(bmap1
));
8927 map2
= isl_map_from_basic_map(isl_basic_map_copy(bmap2
));
8929 is_subset
= isl_map_is_subset(map1
, map2
);
8937 isl_bool
isl_basic_set_is_subset(__isl_keep isl_basic_set
*bset1
,
8938 __isl_keep isl_basic_set
*bset2
)
8940 return isl_basic_map_is_subset(bset1
, bset2
);
8943 isl_bool
isl_basic_map_is_equal(__isl_keep isl_basic_map
*bmap1
,
8944 __isl_keep isl_basic_map
*bmap2
)
8948 if (!bmap1
|| !bmap2
)
8949 return isl_bool_error
;
8950 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
8951 if (is_subset
!= isl_bool_true
)
8953 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
8957 isl_bool
isl_basic_set_is_equal(__isl_keep isl_basic_set
*bset1
,
8958 __isl_keep isl_basic_set
*bset2
)
8960 return isl_basic_map_is_equal(
8961 bset_to_bmap(bset1
), bset_to_bmap(bset2
));
8964 isl_bool
isl_map_is_empty(__isl_keep isl_map
*map
)
8970 return isl_bool_error
;
8971 for (i
= 0; i
< map
->n
; ++i
) {
8972 is_empty
= isl_basic_map_is_empty(map
->p
[i
]);
8974 return isl_bool_error
;
8976 return isl_bool_false
;
8978 return isl_bool_true
;
8981 isl_bool
isl_map_plain_is_empty(__isl_keep isl_map
*map
)
8983 return map
? map
->n
== 0 : isl_bool_error
;
8986 isl_bool
isl_set_plain_is_empty(__isl_keep isl_set
*set
)
8988 return set
? set
->n
== 0 : isl_bool_error
;
8991 isl_bool
isl_set_is_empty(__isl_keep isl_set
*set
)
8993 return isl_map_is_empty(set_to_map(set
));
8997 #define TYPE isl_basic_map
9000 #include "isl_type_has_equal_space_bin_templ.c"
9001 #include "isl_type_check_equal_space_templ.c"
9003 /* Check that "bset1" and "bset2" live in the same space,
9004 * reporting an error if they do not.
9006 isl_stat
isl_basic_set_check_equal_space(__isl_keep isl_basic_set
*bset1
,
9007 __isl_keep isl_basic_set
*bset2
)
9009 return isl_basic_map_check_equal_space(bset_to_bmap(bset1
),
9010 bset_to_bmap(bset1
));
9014 #define TYPE isl_map
9016 #include "isl_type_has_equal_space_bin_templ.c"
9017 #include "isl_type_check_equal_space_templ.c"
9019 isl_bool
isl_set_has_equal_space(__isl_keep isl_set
*set1
,
9020 __isl_keep isl_set
*set2
)
9022 return isl_map_has_equal_space(set_to_map(set1
), set_to_map(set2
));
9026 #define TYPE1 isl_map
9028 #define TYPE2 isl_basic_map
9030 #define TYPE_PAIR isl_map_basic_map
9033 #include "isl_type_has_equal_space_templ.c"
9034 #include "isl_type_check_equal_space_templ.c"
9036 /* Check that "set" and "bset" live in the same space,
9037 * reporting an error if they do not.
9039 isl_stat
isl_set_basic_set_check_equal_space(__isl_keep isl_set
*set
,
9040 __isl_keep isl_basic_set
*bset
)
9042 return isl_map_basic_map_check_equal_space(set_to_map(set
),
9043 bset_to_bmap(bset
));
9046 static isl_bool
map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
9051 return isl_bool_error
;
9052 is_subset
= isl_map_is_subset(map1
, map2
);
9053 if (is_subset
!= isl_bool_true
)
9055 is_subset
= isl_map_is_subset(map2
, map1
);
9059 /* Is "map1" equal to "map2"?
9061 * First check if they are obviously equal.
9062 * If not, then perform a more detailed analysis.
9064 isl_bool
isl_map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
9068 equal
= isl_map_plain_is_equal(map1
, map2
);
9069 if (equal
< 0 || equal
)
9071 return isl_map_align_params_map_map_and_test(map1
, map2
, &map_is_equal
);
9074 isl_bool
isl_basic_map_is_strict_subset(__isl_keep isl_basic_map
*bmap1
,
9075 __isl_keep isl_basic_map
*bmap2
)
9079 if (!bmap1
|| !bmap2
)
9080 return isl_bool_error
;
9081 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
9082 if (is_subset
!= isl_bool_true
)
9084 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
9085 return isl_bool_not(is_subset
);
9088 isl_bool
isl_map_is_strict_subset(__isl_keep isl_map
*map1
,
9089 __isl_keep isl_map
*map2
)
9094 return isl_bool_error
;
9095 is_subset
= isl_map_is_subset(map1
, map2
);
9096 if (is_subset
!= isl_bool_true
)
9098 is_subset
= isl_map_is_subset(map2
, map1
);
9099 return isl_bool_not(is_subset
);
9102 isl_bool
isl_set_is_strict_subset(__isl_keep isl_set
*set1
,
9103 __isl_keep isl_set
*set2
)
9105 return isl_map_is_strict_subset(set_to_map(set1
), set_to_map(set2
));
9108 /* Is "bmap" obviously equal to the universe with the same space?
9110 * That is, does it not have any constraints?
9112 isl_bool
isl_basic_map_plain_is_universe(__isl_keep isl_basic_map
*bmap
)
9115 return isl_bool_error
;
9116 return bmap
->n_eq
== 0 && bmap
->n_ineq
== 0;
9119 /* Is "bset" obviously equal to the universe with the same space?
9121 isl_bool
isl_basic_set_plain_is_universe(__isl_keep isl_basic_set
*bset
)
9123 return isl_basic_map_plain_is_universe(bset
);
9126 /* If "c" does not involve any existentially quantified variables,
9127 * then set *univ to false and abort
9129 static isl_stat
involves_divs(__isl_take isl_constraint
*c
, void *user
)
9131 isl_bool
*univ
= user
;
9134 n
= isl_constraint_dim(c
, isl_dim_div
);
9136 c
= isl_constraint_free(c
);
9137 *univ
= isl_constraint_involves_dims(c
, isl_dim_div
, 0, n
);
9138 isl_constraint_free(c
);
9139 if (*univ
< 0 || !*univ
)
9140 return isl_stat_error
;
9144 /* Is "bmap" equal to the universe with the same space?
9146 * First check if it is obviously equal to the universe.
9147 * If not and if there are any constraints not involving
9148 * existentially quantified variables, then it is certainly
9149 * not equal to the universe.
9150 * Otherwise, check if the universe is a subset of "bmap".
9152 isl_bool
isl_basic_map_is_universe(__isl_keep isl_basic_map
*bmap
)
9156 isl_basic_map
*test
;
9158 univ
= isl_basic_map_plain_is_universe(bmap
);
9159 if (univ
< 0 || univ
)
9161 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
9163 return isl_bool_error
;
9165 return isl_bool_false
;
9166 univ
= isl_bool_true
;
9167 if (isl_basic_map_foreach_constraint(bmap
, &involves_divs
, &univ
) < 0 &&
9169 return isl_bool_error
;
9170 if (univ
< 0 || !univ
)
9172 test
= isl_basic_map_universe(isl_basic_map_get_space(bmap
));
9173 univ
= isl_basic_map_is_subset(test
, bmap
);
9174 isl_basic_map_free(test
);
9178 /* Is "bset" equal to the universe with the same space?
9180 isl_bool
isl_basic_set_is_universe(__isl_keep isl_basic_set
*bset
)
9182 return isl_basic_map_is_universe(bset
);
9185 isl_bool
isl_map_plain_is_universe(__isl_keep isl_map
*map
)
9190 return isl_bool_error
;
9192 for (i
= 0; i
< map
->n
; ++i
) {
9193 isl_bool r
= isl_basic_map_plain_is_universe(map
->p
[i
]);
9198 return isl_bool_false
;
9201 isl_bool
isl_set_plain_is_universe(__isl_keep isl_set
*set
)
9203 return isl_map_plain_is_universe(set_to_map(set
));
9206 isl_bool
isl_basic_map_is_empty(__isl_keep isl_basic_map
*bmap
)
9208 struct isl_basic_set
*bset
= NULL
;
9209 struct isl_vec
*sample
= NULL
;
9210 isl_bool empty
, non_empty
;
9213 return isl_bool_error
;
9215 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
9216 return isl_bool_true
;
9218 if (isl_basic_map_plain_is_universe(bmap
))
9219 return isl_bool_false
;
9221 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
9222 struct isl_basic_map
*copy
= isl_basic_map_copy(bmap
);
9223 copy
= isl_basic_map_remove_redundancies(copy
);
9224 empty
= isl_basic_map_plain_is_empty(copy
);
9225 isl_basic_map_free(copy
);
9229 non_empty
= isl_basic_map_plain_is_non_empty(bmap
);
9231 return isl_bool_error
;
9233 return isl_bool_false
;
9234 isl_vec_free(bmap
->sample
);
9235 bmap
->sample
= NULL
;
9236 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
9238 return isl_bool_error
;
9239 sample
= isl_basic_set_sample_vec(bset
);
9241 return isl_bool_error
;
9242 empty
= sample
->size
== 0;
9243 isl_vec_free(bmap
->sample
);
9244 bmap
->sample
= sample
;
9246 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
9251 isl_bool
isl_basic_map_plain_is_empty(__isl_keep isl_basic_map
*bmap
)
9254 return isl_bool_error
;
9255 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
);
9258 isl_bool
isl_basic_set_plain_is_empty(__isl_keep isl_basic_set
*bset
)
9261 return isl_bool_error
;
9262 return ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
);
9265 /* Is "bmap" known to be non-empty?
9267 * That is, is the cached sample still valid?
9269 isl_bool
isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map
*bmap
)
9274 return isl_bool_error
;
9276 return isl_bool_false
;
9277 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
9279 return isl_bool_error
;
9280 if (bmap
->sample
->size
!= 1 + total
)
9281 return isl_bool_false
;
9282 return isl_basic_map_contains(bmap
, bmap
->sample
);
9285 isl_bool
isl_basic_set_is_empty(__isl_keep isl_basic_set
*bset
)
9287 return isl_basic_map_is_empty(bset_to_bmap(bset
));
9290 __isl_give isl_map
*isl_basic_map_union(__isl_take isl_basic_map
*bmap1
,
9291 __isl_take isl_basic_map
*bmap2
)
9293 struct isl_map
*map
;
9295 if (isl_basic_map_check_equal_space(bmap1
, bmap2
) < 0)
9298 map
= isl_map_alloc_space(isl_space_copy(bmap1
->dim
), 2, 0);
9301 map
= isl_map_add_basic_map(map
, bmap1
);
9302 map
= isl_map_add_basic_map(map
, bmap2
);
9305 isl_basic_map_free(bmap1
);
9306 isl_basic_map_free(bmap2
);
9310 __isl_give isl_set
*isl_basic_set_union(__isl_take isl_basic_set
*bset1
,
9311 __isl_take isl_basic_set
*bset2
)
9313 return set_from_map(isl_basic_map_union(bset_to_bmap(bset1
),
9314 bset_to_bmap(bset2
)));
9317 /* Order divs such that any div only depends on previous divs */
9318 __isl_give isl_basic_map
*isl_basic_map_order_divs(
9319 __isl_take isl_basic_map
*bmap
)
9324 off
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
9326 return isl_basic_map_free(bmap
);
9328 for (i
= 0; i
< bmap
->n_div
; ++i
) {
9330 if (isl_int_is_zero(bmap
->div
[i
][0]))
9332 pos
= isl_seq_first_non_zero(bmap
->div
[i
]+1+1+off
+i
,
9337 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_internal
,
9338 "integer division depends on itself",
9339 return isl_basic_map_free(bmap
));
9340 bmap
= isl_basic_map_swap_div(bmap
, i
, i
+ pos
);
9348 __isl_give isl_map
*isl_map_order_divs(__isl_take isl_map
*map
)
9355 for (i
= 0; i
< map
->n
; ++i
) {
9356 map
->p
[i
] = isl_basic_map_order_divs(map
->p
[i
]);
9367 /* Sort the local variables of "bset".
9369 __isl_give isl_basic_set
*isl_basic_set_sort_divs(
9370 __isl_take isl_basic_set
*bset
)
9372 return bset_from_bmap(isl_basic_map_sort_divs(bset_to_bmap(bset
)));
9375 /* Apply the expansion computed by isl_merge_divs.
9376 * The expansion itself is given by "exp" while the resulting
9377 * list of divs is given by "div".
9379 * Move the integer divisions of "bmap" into the right position
9380 * according to "exp" and then introduce the additional integer
9381 * divisions, adding div constraints.
9382 * The moving should be done first to avoid moving coefficients
9383 * in the definitions of the extra integer divisions.
9385 __isl_give isl_basic_map
*isl_basic_map_expand_divs(
9386 __isl_take isl_basic_map
*bmap
, __isl_take isl_mat
*div
, int *exp
)
9391 bmap
= isl_basic_map_cow(bmap
);
9395 if (div
->n_row
< bmap
->n_div
)
9396 isl_die(isl_mat_get_ctx(div
), isl_error_invalid
,
9397 "not an expansion", goto error
);
9399 n_div
= bmap
->n_div
;
9400 bmap
= isl_basic_map_extend(bmap
, div
->n_row
- n_div
, 0,
9401 2 * (div
->n_row
- n_div
));
9403 for (i
= n_div
; i
< div
->n_row
; ++i
)
9404 if (isl_basic_map_alloc_div(bmap
) < 0)
9407 for (j
= n_div
- 1; j
>= 0; --j
) {
9410 bmap
= isl_basic_map_swap_div(bmap
, j
, exp
[j
]);
9415 for (i
= 0; i
< div
->n_row
; ++i
) {
9416 if (j
< n_div
&& exp
[j
] == i
) {
9419 isl_seq_cpy(bmap
->div
[i
], div
->row
[i
], div
->n_col
);
9420 if (isl_basic_map_div_is_marked_unknown(bmap
, i
))
9422 bmap
= isl_basic_map_add_div_constraints(bmap
, i
);
9431 isl_basic_map_free(bmap
);
9436 /* Apply the expansion computed by isl_merge_divs.
9437 * The expansion itself is given by "exp" while the resulting
9438 * list of divs is given by "div".
9440 __isl_give isl_basic_set
*isl_basic_set_expand_divs(
9441 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*div
, int *exp
)
9443 return isl_basic_map_expand_divs(bset
, div
, exp
);
9446 /* Look for a div in dst that corresponds to the div "div" in src.
9447 * The divs before "div" in src and dst are assumed to be the same.
9449 * Return the position of the corresponding div in dst
9450 * if there is one. Otherwise, return a position beyond the integer divisions.
9451 * Return -1 on error.
9453 static int find_div(__isl_keep isl_basic_map
*dst
,
9454 __isl_keep isl_basic_map
*src
, unsigned div
)
9460 v_div
= isl_basic_map_var_offset(src
, isl_dim_div
);
9461 n_div
= isl_basic_map_dim(dst
, isl_dim_div
);
9462 if (n_div
< 0 || v_div
< 0)
9464 isl_assert(dst
->ctx
, div
<= n_div
, return -1);
9465 for (i
= div
; i
< n_div
; ++i
)
9466 if (isl_seq_eq(dst
->div
[i
], src
->div
[div
], 1+1+v_div
+div
) &&
9467 isl_seq_first_non_zero(dst
->div
[i
] + 1 + 1 + v_div
+ div
,
9473 /* Align the divs of "dst" to those of "src", adding divs from "src"
9474 * if needed. That is, make sure that the first src->n_div divs
9475 * of the result are equal to those of src.
9477 * The result is not finalized as by design it will have redundant
9478 * divs if any divs from "src" were copied.
9480 __isl_give isl_basic_map
*isl_basic_map_align_divs(
9481 __isl_take isl_basic_map
*dst
, __isl_keep isl_basic_map
*src
)
9490 return isl_basic_map_free(dst
);
9492 if (src
->n_div
== 0)
9495 known
= isl_basic_map_divs_known(src
);
9497 return isl_basic_map_free(dst
);
9499 isl_die(isl_basic_map_get_ctx(src
), isl_error_invalid
,
9500 "some src divs are unknown",
9501 return isl_basic_map_free(dst
));
9503 v_div
= isl_basic_map_var_offset(src
, isl_dim_div
);
9505 return isl_basic_map_free(dst
);
9507 src
= isl_basic_map_order_divs(isl_basic_map_copy(src
));
9509 return isl_basic_map_free(dst
);
9512 dst_n_div
= isl_basic_map_dim(dst
, isl_dim_div
);
9514 dst
= isl_basic_map_free(dst
);
9515 for (i
= 0; i
< src
->n_div
; ++i
) {
9516 int j
= find_div(dst
, src
, i
);
9518 dst
= isl_basic_map_free(dst
);
9519 if (j
== dst_n_div
) {
9521 int extra
= src
->n_div
- i
;
9522 dst
= isl_basic_map_cow(dst
);
9525 dst
= isl_basic_map_extend(dst
,
9526 extra
, 0, 2 * extra
);
9529 j
= isl_basic_map_alloc_div(dst
);
9532 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+v_div
+i
);
9533 isl_seq_clr(dst
->div
[j
]+1+1+v_div
+i
, dst
->n_div
- i
);
9535 dst
= isl_basic_map_add_div_constraints(dst
, j
);
9540 dst
= isl_basic_map_swap_div(dst
, i
, j
);
9544 isl_basic_map_free(src
);
9547 isl_basic_map_free(src
);
9548 isl_basic_map_free(dst
);
9552 __isl_give isl_map
*isl_map_align_divs_internal(__isl_take isl_map
*map
)
9560 map
= isl_map_compute_divs(map
);
9561 map
= isl_map_cow(map
);
9565 for (i
= 1; i
< map
->n
; ++i
)
9566 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], map
->p
[i
]);
9567 for (i
= 1; i
< map
->n
; ++i
) {
9568 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], map
->p
[0]);
9570 return isl_map_free(map
);
9573 map
= isl_map_unmark_normalized(map
);
9577 __isl_give isl_map
*isl_map_align_divs(__isl_take isl_map
*map
)
9579 return isl_map_align_divs_internal(map
);
9582 __isl_give isl_set
*isl_set_align_divs(__isl_take isl_set
*set
)
9584 return set_from_map(isl_map_align_divs_internal(set_to_map(set
)));
9587 /* Align the divs of the basic maps in "map" to those
9588 * of the basic maps in "list", as well as to the other basic maps in "map".
9589 * The elements in "list" are assumed to have known divs.
9591 __isl_give isl_map
*isl_map_align_divs_to_basic_map_list(
9592 __isl_take isl_map
*map
, __isl_keep isl_basic_map_list
*list
)
9597 n
= isl_basic_map_list_n_basic_map(list
);
9598 map
= isl_map_compute_divs(map
);
9599 map
= isl_map_cow(map
);
9601 return isl_map_free(map
);
9605 for (i
= 0; i
< n
; ++i
) {
9606 isl_basic_map
*bmap
;
9608 bmap
= isl_basic_map_list_get_basic_map(list
, i
);
9609 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], bmap
);
9610 isl_basic_map_free(bmap
);
9613 return isl_map_free(map
);
9615 return isl_map_align_divs_internal(map
);
9618 /* Align the divs of each element of "list" to those of "bmap".
9619 * Both "bmap" and the elements of "list" are assumed to have known divs.
9621 __isl_give isl_basic_map_list
*isl_basic_map_list_align_divs_to_basic_map(
9622 __isl_take isl_basic_map_list
*list
, __isl_keep isl_basic_map
*bmap
)
9627 n
= isl_basic_map_list_n_basic_map(list
);
9629 return isl_basic_map_list_free(list
);
9631 for (i
= 0; i
< n
; ++i
) {
9632 isl_basic_map
*bmap_i
;
9634 bmap_i
= isl_basic_map_list_get_basic_map(list
, i
);
9635 bmap_i
= isl_basic_map_align_divs(bmap_i
, bmap
);
9636 list
= isl_basic_map_list_set_basic_map(list
, i
, bmap_i
);
9642 __isl_give isl_set
*isl_set_apply( __isl_take isl_set
*set
,
9643 __isl_take isl_map
*map
)
9647 isl_map_align_params_set(&map
, &set
);
9648 ok
= isl_map_compatible_domain(map
, set
);
9652 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
9653 "incompatible spaces", goto error
);
9654 map
= isl_map_intersect_domain(map
, set
);
9655 set
= isl_map_range(map
);
9663 /* There is no need to cow as removing empty parts doesn't change
9664 * the meaning of the set.
9666 __isl_give isl_map
*isl_map_remove_empty_parts(__isl_take isl_map
*map
)
9673 for (i
= map
->n
- 1; i
>= 0; --i
)
9674 map
= remove_if_empty(map
, i
);
9679 __isl_give isl_set
*isl_set_remove_empty_parts(__isl_take isl_set
*set
)
9681 return set_from_map(isl_map_remove_empty_parts(set_to_map(set
)));
9684 /* Create a binary relation that maps the shared initial "pos" dimensions
9685 * of "bset1" and "bset2" to the remaining dimensions of "bset1" and "bset2".
9687 static __isl_give isl_basic_map
*join_initial(__isl_keep isl_basic_set
*bset1
,
9688 __isl_keep isl_basic_set
*bset2
, int pos
)
9690 isl_basic_map
*bmap1
;
9691 isl_basic_map
*bmap2
;
9693 bmap1
= isl_basic_map_from_range(isl_basic_set_copy(bset1
));
9694 bmap2
= isl_basic_map_from_range(isl_basic_set_copy(bset2
));
9695 bmap1
= isl_basic_map_move_dims(bmap1
, isl_dim_in
, 0,
9696 isl_dim_out
, 0, pos
);
9697 bmap2
= isl_basic_map_move_dims(bmap2
, isl_dim_in
, 0,
9698 isl_dim_out
, 0, pos
);
9699 return isl_basic_map_range_product(bmap1
, bmap2
);
9702 /* Given two basic sets bset1 and bset2, compute the maximal difference
9703 * between the values of dimension pos in bset1 and those in bset2
9704 * for any common value of the parameters and dimensions preceding pos.
9706 static enum isl_lp_result
basic_set_maximal_difference_at(
9707 __isl_keep isl_basic_set
*bset1
, __isl_keep isl_basic_set
*bset2
,
9708 int pos
, isl_int
*opt
)
9710 isl_basic_map
*bmap1
;
9711 struct isl_ctx
*ctx
;
9712 struct isl_vec
*obj
;
9716 enum isl_lp_result res
;
9718 nparam
= isl_basic_set_dim(bset1
, isl_dim_param
);
9719 dim1
= isl_basic_set_dim(bset1
, isl_dim_set
);
9720 if (nparam
< 0 || dim1
< 0 || !bset2
)
9721 return isl_lp_error
;
9723 bmap1
= join_initial(bset1
, bset2
, pos
);
9724 total
= isl_basic_map_dim(bmap1
, isl_dim_all
);
9726 return isl_lp_error
;
9729 obj
= isl_vec_alloc(ctx
, 1 + total
);
9732 isl_seq_clr(obj
->block
.data
, 1 + total
);
9733 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
], 1);
9734 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
+(dim1
-pos
)], -1);
9735 res
= isl_basic_map_solve_lp(bmap1
, 1, obj
->block
.data
, ctx
->one
,
9737 isl_basic_map_free(bmap1
);
9741 isl_basic_map_free(bmap1
);
9742 return isl_lp_error
;
9745 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
9746 * for any common value of the parameters and dimensions preceding pos
9747 * in both basic sets, the values of dimension pos in bset1 are
9748 * smaller or larger than those in bset2.
9751 * 1 if bset1 follows bset2
9752 * -1 if bset1 precedes bset2
9753 * 0 if bset1 and bset2 are incomparable
9754 * -2 if some error occurred.
9756 int isl_basic_set_compare_at(__isl_keep isl_basic_set
*bset1
,
9757 __isl_keep isl_basic_set
*bset2
, int pos
)
9760 enum isl_lp_result res
;
9765 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
9767 if (res
== isl_lp_empty
)
9769 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
9770 res
== isl_lp_unbounded
)
9772 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
9781 /* Given two basic sets bset1 and bset2, check whether
9782 * for any common value of the parameters and dimensions preceding pos
9783 * there is a value of dimension pos in bset1 that is larger
9784 * than a value of the same dimension in bset2.
9787 * 1 if there exists such a pair
9788 * 0 if there is no such pair, but there is a pair of equal values
9790 * -2 if some error occurred.
9792 int isl_basic_set_follows_at(__isl_keep isl_basic_set
*bset1
,
9793 __isl_keep isl_basic_set
*bset2
, int pos
)
9796 isl_basic_map
*bmap
;
9799 dim1
= isl_basic_set_dim(bset1
, isl_dim_set
);
9802 bmap
= join_initial(bset1
, bset2
, pos
);
9803 bmap
= isl_basic_map_order_ge(bmap
, isl_dim_out
, 0,
9804 isl_dim_out
, dim1
- pos
);
9805 empty
= isl_basic_map_is_empty(bmap
);
9809 isl_basic_map_free(bmap
);
9812 bmap
= isl_basic_map_order_gt(bmap
, isl_dim_out
, 0,
9813 isl_dim_out
, dim1
- pos
);
9814 empty
= isl_basic_map_is_empty(bmap
);
9817 isl_basic_map_free(bmap
);
9822 isl_basic_map_free(bmap
);
9826 /* Given two sets set1 and set2, check whether
9827 * for any common value of the parameters and dimensions preceding pos
9828 * there is a value of dimension pos in set1 that is larger
9829 * than a value of the same dimension in set2.
9832 * 1 if there exists such a pair
9833 * 0 if there is no such pair, but there is a pair of equal values
9835 * -2 if some error occurred.
9837 int isl_set_follows_at(__isl_keep isl_set
*set1
,
9838 __isl_keep isl_set
*set2
, int pos
)
9846 for (i
= 0; i
< set1
->n
; ++i
)
9847 for (j
= 0; j
< set2
->n
; ++j
) {
9849 f
= isl_basic_set_follows_at(set1
->p
[i
], set2
->p
[j
], pos
);
9850 if (f
== 1 || f
== -2)
9859 static isl_bool
isl_basic_map_plain_has_fixed_var(
9860 __isl_keep isl_basic_map
*bmap
, unsigned pos
, isl_int
*val
)
9866 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
9868 return isl_bool_error
;
9869 for (i
= 0, d
= total
-1; i
< bmap
->n_eq
&& d
+1 > pos
; ++i
) {
9870 for (; d
+1 > pos
; --d
)
9871 if (!isl_int_is_zero(bmap
->eq
[i
][1+d
]))
9875 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1, d
) != -1)
9876 return isl_bool_false
;
9877 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1+d
+1, total
-d
-1) != -1)
9878 return isl_bool_false
;
9879 if (!isl_int_is_one(bmap
->eq
[i
][1+d
]))
9880 return isl_bool_false
;
9882 isl_int_neg(*val
, bmap
->eq
[i
][0]);
9883 return isl_bool_true
;
9885 return isl_bool_false
;
9888 static isl_bool
isl_map_plain_has_fixed_var(__isl_keep isl_map
*map
,
9889 unsigned pos
, isl_int
*val
)
9897 return isl_bool_error
;
9899 return isl_bool_false
;
9901 return isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, val
);
9904 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, &v
);
9905 for (i
= 1; fixed
== isl_bool_true
&& i
< map
->n
; ++i
) {
9906 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[i
], pos
, &tmp
);
9907 if (fixed
== isl_bool_true
&& isl_int_ne(tmp
, v
))
9908 fixed
= isl_bool_false
;
9911 isl_int_set(*val
, v
);
9917 static isl_bool
isl_basic_set_plain_has_fixed_var(
9918 __isl_keep isl_basic_set
*bset
, unsigned pos
, isl_int
*val
)
9920 return isl_basic_map_plain_has_fixed_var(bset_to_bmap(bset
),
9924 isl_bool
isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map
*bmap
,
9925 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
9927 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
9928 return isl_bool_error
;
9929 return isl_basic_map_plain_has_fixed_var(bmap
,
9930 isl_basic_map_offset(bmap
, type
) - 1 + pos
, val
);
9933 /* If "bmap" obviously lies on a hyperplane where the given dimension
9934 * has a fixed value, then return that value.
9935 * Otherwise return NaN.
9937 __isl_give isl_val
*isl_basic_map_plain_get_val_if_fixed(
9938 __isl_keep isl_basic_map
*bmap
,
9939 enum isl_dim_type type
, unsigned pos
)
9947 ctx
= isl_basic_map_get_ctx(bmap
);
9948 v
= isl_val_alloc(ctx
);
9951 fixed
= isl_basic_map_plain_is_fixed(bmap
, type
, pos
, &v
->n
);
9953 return isl_val_free(v
);
9955 isl_int_set_si(v
->d
, 1);
9959 return isl_val_nan(ctx
);
9962 isl_bool
isl_map_plain_is_fixed(__isl_keep isl_map
*map
,
9963 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
9965 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
9966 return isl_bool_error
;
9967 return isl_map_plain_has_fixed_var(map
,
9968 map_offset(map
, type
) - 1 + pos
, val
);
9971 /* If "map" obviously lies on a hyperplane where the given dimension
9972 * has a fixed value, then return that value.
9973 * Otherwise return NaN.
9975 __isl_give isl_val
*isl_map_plain_get_val_if_fixed(__isl_keep isl_map
*map
,
9976 enum isl_dim_type type
, unsigned pos
)
9984 ctx
= isl_map_get_ctx(map
);
9985 v
= isl_val_alloc(ctx
);
9988 fixed
= isl_map_plain_is_fixed(map
, type
, pos
, &v
->n
);
9990 return isl_val_free(v
);
9992 isl_int_set_si(v
->d
, 1);
9996 return isl_val_nan(ctx
);
9999 /* If "set" obviously lies on a hyperplane where the given dimension
10000 * has a fixed value, then return that value.
10001 * Otherwise return NaN.
10003 __isl_give isl_val
*isl_set_plain_get_val_if_fixed(__isl_keep isl_set
*set
,
10004 enum isl_dim_type type
, unsigned pos
)
10006 return isl_map_plain_get_val_if_fixed(set
, type
, pos
);
10009 /* Return a sequence of values in the same space as "set"
10010 * that are equal to the corresponding set dimensions of "set"
10011 * for those set dimensions that obviously lie on a hyperplane
10012 * where the dimension has a fixed value.
10013 * The other elements are set to NaN.
10015 __isl_give isl_multi_val
*isl_set_get_plain_multi_val_if_fixed(
10016 __isl_keep isl_set
*set
)
10023 space
= isl_space_drop_all_params(isl_set_get_space(set
));
10024 mv
= isl_multi_val_alloc(space
);
10025 n
= isl_multi_val_size(mv
);
10027 return isl_multi_val_free(mv
);
10029 for (i
= 0; i
< n
; ++i
) {
10032 v
= isl_set_plain_get_val_if_fixed(set
, isl_dim_set
, i
);
10033 mv
= isl_multi_val_set_val(mv
, i
, v
);
10039 /* Check if dimension dim has fixed value and if so and if val is not NULL,
10040 * then return this fixed value in *val.
10042 isl_bool
isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set
*bset
,
10043 unsigned dim
, isl_int
*val
)
10047 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
10049 return isl_bool_error
;
10050 return isl_basic_set_plain_has_fixed_var(bset
, nparam
+ dim
, val
);
10053 /* Return -1 if the constraint "c1" should be sorted before "c2"
10054 * and 1 if it should be sorted after "c2".
10055 * Return 0 if the two constraints are the same (up to the constant term).
10057 * In particular, if a constraint involves later variables than another
10058 * then it is sorted after this other constraint.
10059 * uset_gist depends on constraints without existentially quantified
10060 * variables sorting first.
10062 * For constraints that have the same latest variable, those
10063 * with the same coefficient for this latest variable (first in absolute value
10064 * and then in actual value) are grouped together.
10065 * This is useful for detecting pairs of constraints that can
10066 * be chained in their printed representation.
10068 * Finally, within a group, constraints are sorted according to
10069 * their coefficients (excluding the constant term).
10071 static int sort_constraint_cmp(const void *p1
, const void *p2
, void *arg
)
10073 isl_int
**c1
= (isl_int
**) p1
;
10074 isl_int
**c2
= (isl_int
**) p2
;
10076 unsigned size
= *(unsigned *) arg
;
10079 l1
= isl_seq_last_non_zero(*c1
+ 1, size
);
10080 l2
= isl_seq_last_non_zero(*c2
+ 1, size
);
10085 cmp
= isl_int_abs_cmp((*c1
)[1 + l1
], (*c2
)[1 + l1
]);
10088 cmp
= isl_int_cmp((*c1
)[1 + l1
], (*c2
)[1 + l1
]);
10092 return isl_seq_cmp(*c1
+ 1, *c2
+ 1, size
);
10095 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
10096 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
10097 * and 0 if the two constraints are the same (up to the constant term).
10099 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map
*bmap
,
10100 isl_int
*c1
, isl_int
*c2
)
10105 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
10109 return sort_constraint_cmp(&c1
, &c2
, &size
);
10112 __isl_give isl_basic_map
*isl_basic_map_sort_constraints(
10113 __isl_take isl_basic_map
*bmap
)
10120 if (bmap
->n_ineq
== 0)
10122 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_SORTED
))
10124 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
10126 return isl_basic_map_free(bmap
);
10128 if (isl_sort(bmap
->ineq
, bmap
->n_ineq
, sizeof(isl_int
*),
10129 &sort_constraint_cmp
, &size
) < 0)
10130 return isl_basic_map_free(bmap
);
10131 ISL_F_SET(bmap
, ISL_BASIC_MAP_SORTED
);
10135 __isl_give isl_basic_set
*isl_basic_set_sort_constraints(
10136 __isl_take isl_basic_set
*bset
)
10138 isl_basic_map
*bmap
= bset_to_bmap(bset
);
10139 return bset_from_bmap(isl_basic_map_sort_constraints(bmap
));
10142 __isl_give isl_basic_map
*isl_basic_map_normalize(
10143 __isl_take isl_basic_map
*bmap
)
10145 bmap
= isl_basic_map_remove_redundancies(bmap
);
10146 bmap
= isl_basic_map_sort_constraints(bmap
);
10149 int isl_basic_map_plain_cmp(__isl_keep isl_basic_map
*bmap1
,
10150 __isl_keep isl_basic_map
*bmap2
)
10154 isl_space
*space1
, *space2
;
10156 if (!bmap1
|| !bmap2
)
10159 if (bmap1
== bmap2
)
10161 space1
= isl_basic_map_peek_space(bmap1
);
10162 space2
= isl_basic_map_peek_space(bmap2
);
10163 cmp
= isl_space_cmp(space1
, space2
);
10166 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) !=
10167 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_RATIONAL
))
10168 return ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) ? -1 : 1;
10169 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
) &&
10170 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
10172 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
))
10174 if (ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
10176 if (bmap1
->n_eq
!= bmap2
->n_eq
)
10177 return bmap1
->n_eq
- bmap2
->n_eq
;
10178 if (bmap1
->n_ineq
!= bmap2
->n_ineq
)
10179 return bmap1
->n_ineq
- bmap2
->n_ineq
;
10180 if (bmap1
->n_div
!= bmap2
->n_div
)
10181 return bmap1
->n_div
- bmap2
->n_div
;
10182 total
= isl_basic_map_dim(bmap1
, isl_dim_all
);
10185 for (i
= 0; i
< bmap1
->n_eq
; ++i
) {
10186 cmp
= isl_seq_cmp(bmap1
->eq
[i
], bmap2
->eq
[i
], 1+total
);
10190 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
10191 cmp
= isl_seq_cmp(bmap1
->ineq
[i
], bmap2
->ineq
[i
], 1+total
);
10195 for (i
= 0; i
< bmap1
->n_div
; ++i
) {
10196 cmp
= isl_seq_cmp(bmap1
->div
[i
], bmap2
->div
[i
], 1+1+total
);
10203 int isl_basic_set_plain_cmp(__isl_keep isl_basic_set
*bset1
,
10204 __isl_keep isl_basic_set
*bset2
)
10206 return isl_basic_map_plain_cmp(bset1
, bset2
);
10209 int isl_set_plain_cmp(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
10215 if (set1
->n
!= set2
->n
)
10216 return set1
->n
- set2
->n
;
10218 for (i
= 0; i
< set1
->n
; ++i
) {
10219 cmp
= isl_basic_set_plain_cmp(set1
->p
[i
], set2
->p
[i
]);
10227 isl_bool
isl_basic_map_plain_is_equal(__isl_keep isl_basic_map
*bmap1
,
10228 __isl_keep isl_basic_map
*bmap2
)
10230 if (!bmap1
|| !bmap2
)
10231 return isl_bool_error
;
10232 return isl_basic_map_plain_cmp(bmap1
, bmap2
) == 0;
10235 isl_bool
isl_basic_set_plain_is_equal(__isl_keep isl_basic_set
*bset1
,
10236 __isl_keep isl_basic_set
*bset2
)
10238 return isl_basic_map_plain_is_equal(bset_to_bmap(bset1
),
10239 bset_to_bmap(bset2
));
10242 static int qsort_bmap_cmp(const void *p1
, const void *p2
)
10244 isl_basic_map
*bmap1
= *(isl_basic_map
**) p1
;
10245 isl_basic_map
*bmap2
= *(isl_basic_map
**) p2
;
10247 return isl_basic_map_plain_cmp(bmap1
, bmap2
);
10250 /* Sort the basic maps of "map" and remove duplicate basic maps.
10252 * While removing basic maps, we make sure that the basic maps remain
10253 * sorted because isl_map_normalize expects the basic maps of the result
10256 static __isl_give isl_map
*sort_and_remove_duplicates(__isl_take isl_map
*map
)
10260 map
= isl_map_remove_empty_parts(map
);
10263 qsort(map
->p
, map
->n
, sizeof(struct isl_basic_map
*), qsort_bmap_cmp
);
10264 for (i
= map
->n
- 1; i
>= 1; --i
) {
10265 if (!isl_basic_map_plain_is_equal(map
->p
[i
- 1], map
->p
[i
]))
10267 isl_basic_map_free(map
->p
[i
-1]);
10268 for (j
= i
; j
< map
->n
; ++j
)
10269 map
->p
[j
- 1] = map
->p
[j
];
10276 /* Remove obvious duplicates among the basic maps of "map".
10278 * Unlike isl_map_normalize, this function does not remove redundant
10279 * constraints and only removes duplicates that have exactly the same
10280 * constraints in the input. It does sort the constraints and
10281 * the basic maps to ease the detection of duplicates.
10283 * If "map" has already been normalized or if the basic maps are
10284 * disjoint, then there can be no duplicates.
10286 __isl_give isl_map
*isl_map_remove_obvious_duplicates(__isl_take isl_map
*map
)
10289 isl_basic_map
*bmap
;
10295 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
| ISL_MAP_DISJOINT
))
10297 for (i
= 0; i
< map
->n
; ++i
) {
10298 bmap
= isl_basic_map_copy(map
->p
[i
]);
10299 bmap
= isl_basic_map_sort_constraints(bmap
);
10301 return isl_map_free(map
);
10302 isl_basic_map_free(map
->p
[i
]);
10306 map
= sort_and_remove_duplicates(map
);
10310 /* We normalize in place, but if anything goes wrong we need
10311 * to return NULL, so we need to make sure we don't change the
10312 * meaning of any possible other copies of map.
10314 __isl_give isl_map
*isl_map_normalize(__isl_take isl_map
*map
)
10317 struct isl_basic_map
*bmap
;
10321 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
))
10323 for (i
= 0; i
< map
->n
; ++i
) {
10324 bmap
= isl_basic_map_normalize(isl_basic_map_copy(map
->p
[i
]));
10327 isl_basic_map_free(map
->p
[i
]);
10331 map
= sort_and_remove_duplicates(map
);
10333 ISL_F_SET(map
, ISL_MAP_NORMALIZED
);
10340 __isl_give isl_set
*isl_set_normalize(__isl_take isl_set
*set
)
10342 return set_from_map(isl_map_normalize(set_to_map(set
)));
10345 isl_bool
isl_map_plain_is_equal(__isl_keep isl_map
*map1
,
10346 __isl_keep isl_map
*map2
)
10351 if (!map1
|| !map2
)
10352 return isl_bool_error
;
10355 return isl_bool_true
;
10356 equal
= isl_map_has_equal_space(map1
, map2
);
10357 if (equal
< 0 || !equal
)
10360 map1
= isl_map_copy(map1
);
10361 map2
= isl_map_copy(map2
);
10362 map1
= isl_map_normalize(map1
);
10363 map2
= isl_map_normalize(map2
);
10364 if (!map1
|| !map2
)
10366 equal
= map1
->n
== map2
->n
;
10367 for (i
= 0; equal
&& i
< map1
->n
; ++i
) {
10368 equal
= isl_basic_map_plain_is_equal(map1
->p
[i
], map2
->p
[i
]);
10372 isl_map_free(map1
);
10373 isl_map_free(map2
);
10376 isl_map_free(map1
);
10377 isl_map_free(map2
);
10378 return isl_bool_error
;
10381 isl_bool
isl_set_plain_is_equal(__isl_keep isl_set
*set1
,
10382 __isl_keep isl_set
*set2
)
10384 return isl_map_plain_is_equal(set_to_map(set1
), set_to_map(set2
));
10387 /* Return the basic maps in "map" as a list.
10389 __isl_give isl_basic_map_list
*isl_map_get_basic_map_list(
10390 __isl_keep isl_map
*map
)
10394 isl_basic_map_list
*list
;
10398 ctx
= isl_map_get_ctx(map
);
10399 list
= isl_basic_map_list_alloc(ctx
, map
->n
);
10401 for (i
= 0; i
< map
->n
; ++i
) {
10402 isl_basic_map
*bmap
;
10404 bmap
= isl_basic_map_copy(map
->p
[i
]);
10405 list
= isl_basic_map_list_add(list
, bmap
);
10411 /* Return the intersection of the elements in the non-empty list "list".
10412 * All elements are assumed to live in the same space.
10414 __isl_give isl_basic_map
*isl_basic_map_list_intersect(
10415 __isl_take isl_basic_map_list
*list
)
10419 isl_basic_map
*bmap
;
10421 n
= isl_basic_map_list_n_basic_map(list
);
10425 isl_die(isl_basic_map_list_get_ctx(list
), isl_error_invalid
,
10426 "expecting non-empty list", goto error
);
10428 bmap
= isl_basic_map_list_get_basic_map(list
, 0);
10429 for (i
= 1; i
< n
; ++i
) {
10430 isl_basic_map
*bmap_i
;
10432 bmap_i
= isl_basic_map_list_get_basic_map(list
, i
);
10433 bmap
= isl_basic_map_intersect(bmap
, bmap_i
);
10436 isl_basic_map_list_free(list
);
10439 isl_basic_map_list_free(list
);
10443 /* Return the intersection of the elements in the non-empty list "list".
10444 * All elements are assumed to live in the same space.
10446 __isl_give isl_basic_set
*isl_basic_set_list_intersect(
10447 __isl_take isl_basic_set_list
*list
)
10449 return isl_basic_map_list_intersect(list
);
10452 /* Return the union of the elements of "list".
10453 * The list is required to have at least one element.
10455 __isl_give isl_set
*isl_basic_set_list_union(
10456 __isl_take isl_basic_set_list
*list
)
10461 isl_basic_set
*bset
;
10464 n
= isl_basic_set_list_n_basic_set(list
);
10468 isl_die(isl_basic_set_list_get_ctx(list
), isl_error_invalid
,
10469 "expecting non-empty list", goto error
);
10471 bset
= isl_basic_set_list_get_basic_set(list
, 0);
10472 space
= isl_basic_set_get_space(bset
);
10473 isl_basic_set_free(bset
);
10475 set
= isl_set_alloc_space(space
, n
, 0);
10476 for (i
= 0; i
< n
; ++i
) {
10477 bset
= isl_basic_set_list_get_basic_set(list
, i
);
10478 set
= isl_set_add_basic_set(set
, bset
);
10481 isl_basic_set_list_free(list
);
10484 isl_basic_set_list_free(list
);
10488 /* Return the union of the elements in the non-empty list "list".
10489 * All elements are assumed to live in the same space.
10491 __isl_give isl_set
*isl_set_list_union(__isl_take isl_set_list
*list
)
10497 n
= isl_set_list_n_set(list
);
10501 isl_die(isl_set_list_get_ctx(list
), isl_error_invalid
,
10502 "expecting non-empty list", goto error
);
10504 set
= isl_set_list_get_set(list
, 0);
10505 for (i
= 1; i
< n
; ++i
) {
10508 set_i
= isl_set_list_get_set(list
, i
);
10509 set
= isl_set_union(set
, set_i
);
10512 isl_set_list_free(list
);
10515 isl_set_list_free(list
);
10519 __isl_give isl_basic_map
*isl_basic_map_product(
10520 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
10522 isl_space
*space_result
= NULL
;
10523 struct isl_basic_map
*bmap
;
10524 unsigned in1
, in2
, out1
, out2
, nparam
, total
, pos
;
10525 struct isl_dim_map
*dim_map1
, *dim_map2
;
10527 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
10529 space_result
= isl_space_product(isl_space_copy(bmap1
->dim
),
10530 isl_space_copy(bmap2
->dim
));
10532 in1
= isl_basic_map_dim(bmap1
, isl_dim_in
);
10533 in2
= isl_basic_map_dim(bmap2
, isl_dim_in
);
10534 out1
= isl_basic_map_dim(bmap1
, isl_dim_out
);
10535 out2
= isl_basic_map_dim(bmap2
, isl_dim_out
);
10536 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
10538 total
= nparam
+ in1
+ in2
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
10539 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
10540 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
10541 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
10542 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
10543 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
10544 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
10545 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
10546 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
10547 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
10548 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
10550 bmap
= isl_basic_map_alloc_space(space_result
,
10551 bmap1
->n_div
+ bmap2
->n_div
,
10552 bmap1
->n_eq
+ bmap2
->n_eq
,
10553 bmap1
->n_ineq
+ bmap2
->n_ineq
);
10554 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
10555 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
10556 bmap
= isl_basic_map_simplify(bmap
);
10557 return isl_basic_map_finalize(bmap
);
10559 isl_basic_map_free(bmap1
);
10560 isl_basic_map_free(bmap2
);
10564 __isl_give isl_basic_map
*isl_basic_map_flat_product(
10565 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
10567 isl_basic_map
*prod
;
10569 prod
= isl_basic_map_product(bmap1
, bmap2
);
10570 prod
= isl_basic_map_flatten(prod
);
10574 __isl_give isl_basic_set
*isl_basic_set_flat_product(
10575 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
10577 return isl_basic_map_flat_range_product(bset1
, bset2
);
10580 __isl_give isl_basic_map
*isl_basic_map_domain_product(
10581 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
10583 isl_space
*space1
, *space2
;
10584 isl_space
*space_result
= NULL
;
10585 isl_basic_map
*bmap
;
10586 isl_size in1
, in2
, out
, nparam
;
10587 unsigned total
, pos
;
10588 struct isl_dim_map
*dim_map1
, *dim_map2
;
10590 in1
= isl_basic_map_dim(bmap1
, isl_dim_in
);
10591 in2
= isl_basic_map_dim(bmap2
, isl_dim_in
);
10592 out
= isl_basic_map_dim(bmap1
, isl_dim_out
);
10593 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
10594 if (in1
< 0 || in2
< 0 || out
< 0 || nparam
< 0)
10597 space1
= isl_basic_map_get_space(bmap1
);
10598 space2
= isl_basic_map_get_space(bmap2
);
10599 space_result
= isl_space_domain_product(space1
, space2
);
10601 total
= nparam
+ in1
+ in2
+ out
+ bmap1
->n_div
+ bmap2
->n_div
;
10602 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
10603 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
10604 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
10605 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
10606 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
10607 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
10608 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
10609 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
);
10610 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out
);
10611 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
10613 bmap
= isl_basic_map_alloc_space(space_result
,
10614 bmap1
->n_div
+ bmap2
->n_div
,
10615 bmap1
->n_eq
+ bmap2
->n_eq
,
10616 bmap1
->n_ineq
+ bmap2
->n_ineq
);
10617 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
10618 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
10619 bmap
= isl_basic_map_simplify(bmap
);
10620 return isl_basic_map_finalize(bmap
);
10622 isl_basic_map_free(bmap1
);
10623 isl_basic_map_free(bmap2
);
10627 __isl_give isl_basic_map
*isl_basic_map_range_product(
10628 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
10631 isl_space
*space_result
= NULL
;
10632 isl_basic_map
*bmap
;
10633 isl_size in
, out1
, out2
, nparam
;
10634 unsigned total
, pos
;
10635 struct isl_dim_map
*dim_map1
, *dim_map2
;
10637 rational
= isl_basic_map_is_rational(bmap1
);
10638 if (rational
>= 0 && rational
)
10639 rational
= isl_basic_map_is_rational(bmap2
);
10640 in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
10641 out1
= isl_basic_map_dim(bmap1
, isl_dim_out
);
10642 out2
= isl_basic_map_dim(bmap2
, isl_dim_out
);
10643 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
10644 if (in
< 0 || out1
< 0 || out2
< 0 || nparam
< 0 || rational
< 0)
10647 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
10650 space_result
= isl_space_range_product(isl_space_copy(bmap1
->dim
),
10651 isl_space_copy(bmap2
->dim
));
10653 total
= nparam
+ in
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
10654 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
10655 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
10656 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
10657 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
10658 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
10659 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
10660 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in
);
10661 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
10662 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
10663 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
10665 bmap
= isl_basic_map_alloc_space(space_result
,
10666 bmap1
->n_div
+ bmap2
->n_div
,
10667 bmap1
->n_eq
+ bmap2
->n_eq
,
10668 bmap1
->n_ineq
+ bmap2
->n_ineq
);
10669 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
10670 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
10672 bmap
= isl_basic_map_set_rational(bmap
);
10673 bmap
= isl_basic_map_simplify(bmap
);
10674 return isl_basic_map_finalize(bmap
);
10676 isl_basic_map_free(bmap1
);
10677 isl_basic_map_free(bmap2
);
10681 __isl_give isl_basic_map
*isl_basic_map_flat_range_product(
10682 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
10684 isl_basic_map
*prod
;
10686 prod
= isl_basic_map_range_product(bmap1
, bmap2
);
10687 prod
= isl_basic_map_flatten_range(prod
);
10691 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
10692 * and collect the results.
10693 * The result live in the space obtained by calling "space_product"
10694 * on the spaces of "map1" and "map2".
10695 * If "remove_duplicates" is set then the result may contain duplicates
10696 * (even if the inputs do not) and so we try and remove the obvious
10699 static __isl_give isl_map
*map_product(__isl_take isl_map
*map1
,
10700 __isl_take isl_map
*map2
,
10701 __isl_give isl_space
*(*space_product
)(__isl_take isl_space
*left
,
10702 __isl_take isl_space
*right
),
10703 __isl_give isl_basic_map
*(*basic_map_product
)(
10704 __isl_take isl_basic_map
*left
,
10705 __isl_take isl_basic_map
*right
),
10706 int remove_duplicates
)
10708 unsigned flags
= 0;
10709 struct isl_map
*result
;
10713 m
= isl_map_has_equal_params(map1
, map2
);
10717 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
10718 "parameters don't match", goto error
);
10720 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
10721 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
10722 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
10724 result
= isl_map_alloc_space(space_product(isl_space_copy(map1
->dim
),
10725 isl_space_copy(map2
->dim
)),
10726 map1
->n
* map2
->n
, flags
);
10729 for (i
= 0; i
< map1
->n
; ++i
)
10730 for (j
= 0; j
< map2
->n
; ++j
) {
10731 struct isl_basic_map
*part
;
10732 part
= basic_map_product(isl_basic_map_copy(map1
->p
[i
]),
10733 isl_basic_map_copy(map2
->p
[j
]));
10734 if (isl_basic_map_is_empty(part
))
10735 isl_basic_map_free(part
);
10737 result
= isl_map_add_basic_map(result
, part
);
10741 if (remove_duplicates
)
10742 result
= isl_map_remove_obvious_duplicates(result
);
10743 isl_map_free(map1
);
10744 isl_map_free(map2
);
10747 isl_map_free(map1
);
10748 isl_map_free(map2
);
10752 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
10754 __isl_give isl_map
*isl_map_product(__isl_take isl_map
*map1
,
10755 __isl_take isl_map
*map2
)
10757 isl_map_align_params_bin(&map1
, &map2
);
10758 return map_product(map1
, map2
, &isl_space_product
,
10759 &isl_basic_map_product
, 0);
10762 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
10764 __isl_give isl_map
*isl_map_flat_product(__isl_take isl_map
*map1
,
10765 __isl_take isl_map
*map2
)
10769 prod
= isl_map_product(map1
, map2
);
10770 prod
= isl_map_flatten(prod
);
10774 /* Given two set A and B, construct its Cartesian product A x B.
10776 __isl_give isl_set
*isl_set_product(__isl_take isl_set
*set1
,
10777 __isl_take isl_set
*set2
)
10779 return isl_map_range_product(set1
, set2
);
10782 __isl_give isl_set
*isl_set_flat_product(__isl_take isl_set
*set1
,
10783 __isl_take isl_set
*set2
)
10785 return isl_map_flat_range_product(set1
, set2
);
10788 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
10790 __isl_give isl_map
*isl_map_domain_product(__isl_take isl_map
*map1
,
10791 __isl_take isl_map
*map2
)
10793 isl_map_align_params_bin(&map1
, &map2
);
10794 return map_product(map1
, map2
, &isl_space_domain_product
,
10795 &isl_basic_map_domain_product
, 1);
10798 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
10800 __isl_give isl_map
*isl_map_range_product(__isl_take isl_map
*map1
,
10801 __isl_take isl_map
*map2
)
10803 isl_map_align_params_bin(&map1
, &map2
);
10804 return map_product(map1
, map2
, &isl_space_range_product
,
10805 &isl_basic_map_range_product
, 1);
10808 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
10810 __isl_give isl_map
*isl_map_factor_domain(__isl_take isl_map
*map
)
10813 isl_size total1
, keep1
, total2
, keep2
;
10815 total1
= isl_map_dim(map
, isl_dim_in
);
10816 total2
= isl_map_dim(map
, isl_dim_out
);
10817 if (total1
< 0 || total2
< 0)
10818 return isl_map_free(map
);
10819 if (!isl_space_domain_is_wrapping(map
->dim
) ||
10820 !isl_space_range_is_wrapping(map
->dim
))
10821 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10822 "not a product", return isl_map_free(map
));
10824 space
= isl_map_get_space(map
);
10825 space
= isl_space_factor_domain(space
);
10826 keep1
= isl_space_dim(space
, isl_dim_in
);
10827 keep2
= isl_space_dim(space
, isl_dim_out
);
10828 if (keep1
< 0 || keep2
< 0)
10829 map
= isl_map_free(map
);
10830 map
= isl_map_project_out(map
, isl_dim_in
, keep1
, total1
- keep1
);
10831 map
= isl_map_project_out(map
, isl_dim_out
, keep2
, total2
- keep2
);
10832 map
= isl_map_reset_space(map
, space
);
10837 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
10839 __isl_give isl_map
*isl_map_factor_range(__isl_take isl_map
*map
)
10842 isl_size total1
, keep1
, total2
, keep2
;
10844 total1
= isl_map_dim(map
, isl_dim_in
);
10845 total2
= isl_map_dim(map
, isl_dim_out
);
10846 if (total1
< 0 || total2
< 0)
10847 return isl_map_free(map
);
10848 if (!isl_space_domain_is_wrapping(map
->dim
) ||
10849 !isl_space_range_is_wrapping(map
->dim
))
10850 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10851 "not a product", return isl_map_free(map
));
10853 space
= isl_map_get_space(map
);
10854 space
= isl_space_factor_range(space
);
10855 keep1
= isl_space_dim(space
, isl_dim_in
);
10856 keep2
= isl_space_dim(space
, isl_dim_out
);
10857 if (keep1
< 0 || keep2
< 0)
10858 map
= isl_map_free(map
);
10859 map
= isl_map_project_out(map
, isl_dim_in
, 0, total1
- keep1
);
10860 map
= isl_map_project_out(map
, isl_dim_out
, 0, total2
- keep2
);
10861 map
= isl_map_reset_space(map
, space
);
10866 /* Given a map of the form [A -> B] -> C, return the map A -> C.
10868 __isl_give isl_map
*isl_map_domain_factor_domain(__isl_take isl_map
*map
)
10871 isl_size total
, keep
;
10873 total
= isl_map_dim(map
, isl_dim_in
);
10875 return isl_map_free(map
);
10876 if (!isl_space_domain_is_wrapping(map
->dim
))
10877 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10878 "domain is not a product", return isl_map_free(map
));
10880 space
= isl_map_get_space(map
);
10881 space
= isl_space_domain_factor_domain(space
);
10882 keep
= isl_space_dim(space
, isl_dim_in
);
10884 map
= isl_map_free(map
);
10885 map
= isl_map_project_out(map
, isl_dim_in
, keep
, total
- keep
);
10886 map
= isl_map_reset_space(map
, space
);
10891 /* Given a map of the form [A -> B] -> C, return the map B -> C.
10893 __isl_give isl_map
*isl_map_domain_factor_range(__isl_take isl_map
*map
)
10896 isl_size total
, keep
;
10898 total
= isl_map_dim(map
, isl_dim_in
);
10900 return isl_map_free(map
);
10901 if (!isl_space_domain_is_wrapping(map
->dim
))
10902 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10903 "domain is not a product", return isl_map_free(map
));
10905 space
= isl_map_get_space(map
);
10906 space
= isl_space_domain_factor_range(space
);
10907 keep
= isl_space_dim(space
, isl_dim_in
);
10909 map
= isl_map_free(map
);
10910 map
= isl_map_project_out(map
, isl_dim_in
, 0, total
- keep
);
10911 map
= isl_map_reset_space(map
, space
);
10916 /* Given a map A -> [B -> C], extract the map A -> B.
10918 __isl_give isl_map
*isl_map_range_factor_domain(__isl_take isl_map
*map
)
10921 isl_size total
, keep
;
10923 total
= isl_map_dim(map
, isl_dim_out
);
10925 return isl_map_free(map
);
10926 if (!isl_space_range_is_wrapping(map
->dim
))
10927 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10928 "range is not a product", return isl_map_free(map
));
10930 space
= isl_map_get_space(map
);
10931 space
= isl_space_range_factor_domain(space
);
10932 keep
= isl_space_dim(space
, isl_dim_out
);
10934 map
= isl_map_free(map
);
10935 map
= isl_map_project_out(map
, isl_dim_out
, keep
, total
- keep
);
10936 map
= isl_map_reset_space(map
, space
);
10941 /* Given a map A -> [B -> C], extract the map A -> C.
10943 __isl_give isl_map
*isl_map_range_factor_range(__isl_take isl_map
*map
)
10946 isl_size total
, keep
;
10948 total
= isl_map_dim(map
, isl_dim_out
);
10950 return isl_map_free(map
);
10951 if (!isl_space_range_is_wrapping(map
->dim
))
10952 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10953 "range is not a product", return isl_map_free(map
));
10955 space
= isl_map_get_space(map
);
10956 space
= isl_space_range_factor_range(space
);
10957 keep
= isl_space_dim(space
, isl_dim_out
);
10959 map
= isl_map_free(map
);
10960 map
= isl_map_project_out(map
, isl_dim_out
, 0, total
- keep
);
10961 map
= isl_map_reset_space(map
, space
);
10966 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
10968 __isl_give isl_map
*isl_map_flat_domain_product(__isl_take isl_map
*map1
,
10969 __isl_take isl_map
*map2
)
10973 prod
= isl_map_domain_product(map1
, map2
);
10974 prod
= isl_map_flatten_domain(prod
);
10978 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
10980 __isl_give isl_map
*isl_map_flat_range_product(__isl_take isl_map
*map1
,
10981 __isl_take isl_map
*map2
)
10985 prod
= isl_map_range_product(map1
, map2
);
10986 prod
= isl_map_flatten_range(prod
);
10990 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map
*bmap
)
10993 uint32_t hash
= isl_hash_init();
10998 bmap
= isl_basic_map_copy(bmap
);
10999 bmap
= isl_basic_map_normalize(bmap
);
11000 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
11003 isl_hash_byte(hash
, bmap
->n_eq
& 0xFF);
11004 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
11006 c_hash
= isl_seq_get_hash(bmap
->eq
[i
], 1 + total
);
11007 isl_hash_hash(hash
, c_hash
);
11009 isl_hash_byte(hash
, bmap
->n_ineq
& 0xFF);
11010 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
11012 c_hash
= isl_seq_get_hash(bmap
->ineq
[i
], 1 + total
);
11013 isl_hash_hash(hash
, c_hash
);
11015 isl_hash_byte(hash
, bmap
->n_div
& 0xFF);
11016 for (i
= 0; i
< bmap
->n_div
; ++i
) {
11018 if (isl_int_is_zero(bmap
->div
[i
][0]))
11020 isl_hash_byte(hash
, i
& 0xFF);
11021 c_hash
= isl_seq_get_hash(bmap
->div
[i
], 1 + 1 + total
);
11022 isl_hash_hash(hash
, c_hash
);
11024 isl_basic_map_free(bmap
);
11028 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set
*bset
)
11030 return isl_basic_map_get_hash(bset_to_bmap(bset
));
11033 uint32_t isl_map_get_hash(__isl_keep isl_map
*map
)
11040 map
= isl_map_copy(map
);
11041 map
= isl_map_normalize(map
);
11045 hash
= isl_hash_init();
11046 for (i
= 0; i
< map
->n
; ++i
) {
11047 uint32_t bmap_hash
;
11048 bmap_hash
= isl_basic_map_get_hash(map
->p
[i
]);
11049 isl_hash_hash(hash
, bmap_hash
);
11057 uint32_t isl_set_get_hash(__isl_keep isl_set
*set
)
11059 return isl_map_get_hash(set_to_map(set
));
11062 /* Return the number of basic maps in the (current) representation of "map".
11064 isl_size
isl_map_n_basic_map(__isl_keep isl_map
*map
)
11066 return map
? map
->n
: isl_size_error
;
11069 isl_size
isl_set_n_basic_set(__isl_keep isl_set
*set
)
11071 return set
? set
->n
: isl_size_error
;
11074 isl_stat
isl_map_foreach_basic_map(__isl_keep isl_map
*map
,
11075 isl_stat (*fn
)(__isl_take isl_basic_map
*bmap
, void *user
), void *user
)
11080 return isl_stat_error
;
11082 for (i
= 0; i
< map
->n
; ++i
)
11083 if (fn(isl_basic_map_copy(map
->p
[i
]), user
) < 0)
11084 return isl_stat_error
;
11086 return isl_stat_ok
;
11089 isl_stat
isl_set_foreach_basic_set(__isl_keep isl_set
*set
,
11090 isl_stat (*fn
)(__isl_take isl_basic_set
*bset
, void *user
), void *user
)
11095 return isl_stat_error
;
11097 for (i
= 0; i
< set
->n
; ++i
)
11098 if (fn(isl_basic_set_copy(set
->p
[i
]), user
) < 0)
11099 return isl_stat_error
;
11101 return isl_stat_ok
;
11104 /* Return a list of basic sets, the union of which is equal to "set".
11106 __isl_give isl_basic_set_list
*isl_set_get_basic_set_list(
11107 __isl_keep isl_set
*set
)
11110 isl_basic_set_list
*list
;
11115 list
= isl_basic_set_list_alloc(isl_set_get_ctx(set
), set
->n
);
11116 for (i
= 0; i
< set
->n
; ++i
) {
11117 isl_basic_set
*bset
;
11119 bset
= isl_basic_set_copy(set
->p
[i
]);
11120 list
= isl_basic_set_list_add(list
, bset
);
11126 __isl_give isl_basic_set
*isl_basic_set_lift(__isl_take isl_basic_set
*bset
)
11133 bset
= isl_basic_set_cow(bset
);
11137 space
= isl_basic_set_get_space(bset
);
11138 space
= isl_space_lift(space
, bset
->n_div
);
11141 isl_space_free(bset
->dim
);
11143 bset
->extra
-= bset
->n_div
;
11146 bset
= isl_basic_set_finalize(bset
);
11150 isl_basic_set_free(bset
);
11154 __isl_give isl_set
*isl_set_lift(__isl_take isl_set
*set
)
11160 set
= set_from_map(isl_map_align_divs_internal(set_to_map(set
)));
11165 set
= isl_set_cow(set
);
11169 n_div
= set
->p
[0]->n_div
;
11170 space
= isl_set_get_space(set
);
11171 space
= isl_space_lift(space
, n_div
);
11174 isl_space_free(set
->dim
);
11177 for (i
= 0; i
< set
->n
; ++i
) {
11178 set
->p
[i
] = isl_basic_set_lift(set
->p
[i
]);
11189 int isl_basic_set_size(__isl_keep isl_basic_set
*bset
)
11194 dim
= isl_basic_set_dim(bset
, isl_dim_all
);
11197 size
+= bset
->n_eq
* (1 + dim
);
11198 size
+= bset
->n_ineq
* (1 + dim
);
11199 size
+= bset
->n_div
* (2 + dim
);
11204 int isl_set_size(__isl_keep isl_set
*set
)
11212 for (i
= 0; i
< set
->n
; ++i
)
11213 size
+= isl_basic_set_size(set
->p
[i
]);
11218 /* Check if there is any lower bound (if lower == 0) and/or upper
11219 * bound (if upper == 0) on the specified dim.
11221 static isl_bool
basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
11222 enum isl_dim_type type
, unsigned pos
, int lower
, int upper
)
11226 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
11227 return isl_bool_error
;
11229 pos
+= isl_basic_map_offset(bmap
, type
);
11231 for (i
= 0; i
< bmap
->n_div
; ++i
) {
11232 if (isl_int_is_zero(bmap
->div
[i
][0]))
11234 if (!isl_int_is_zero(bmap
->div
[i
][1 + pos
]))
11235 return isl_bool_true
;
11238 for (i
= 0; i
< bmap
->n_eq
; ++i
)
11239 if (!isl_int_is_zero(bmap
->eq
[i
][pos
]))
11240 return isl_bool_true
;
11242 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
11243 int sgn
= isl_int_sgn(bmap
->ineq
[i
][pos
]);
11250 return lower
&& upper
;
11253 isl_bool
isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
11254 enum isl_dim_type type
, unsigned pos
)
11256 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 0);
11259 isl_bool
isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map
*bmap
,
11260 enum isl_dim_type type
, unsigned pos
)
11262 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 1);
11265 isl_bool
isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map
*bmap
,
11266 enum isl_dim_type type
, unsigned pos
)
11268 return basic_map_dim_is_bounded(bmap
, type
, pos
, 1, 0);
11271 isl_bool
isl_map_dim_is_bounded(__isl_keep isl_map
*map
,
11272 enum isl_dim_type type
, unsigned pos
)
11277 return isl_bool_error
;
11279 for (i
= 0; i
< map
->n
; ++i
) {
11281 bounded
= isl_basic_map_dim_is_bounded(map
->p
[i
], type
, pos
);
11282 if (bounded
< 0 || !bounded
)
11286 return isl_bool_true
;
11289 /* Return true if the specified dim is involved in both an upper bound
11290 * and a lower bound.
11292 isl_bool
isl_set_dim_is_bounded(__isl_keep isl_set
*set
,
11293 enum isl_dim_type type
, unsigned pos
)
11295 return isl_map_dim_is_bounded(set_to_map(set
), type
, pos
);
11298 /* Does "map" have a bound (according to "fn") for any of its basic maps?
11300 static isl_bool
has_any_bound(__isl_keep isl_map
*map
,
11301 enum isl_dim_type type
, unsigned pos
,
11302 isl_bool (*fn
)(__isl_keep isl_basic_map
*bmap
,
11303 enum isl_dim_type type
, unsigned pos
))
11308 return isl_bool_error
;
11310 for (i
= 0; i
< map
->n
; ++i
) {
11312 bounded
= fn(map
->p
[i
], type
, pos
);
11313 if (bounded
< 0 || bounded
)
11317 return isl_bool_false
;
11320 /* Return 1 if the specified dim is involved in any lower bound.
11322 isl_bool
isl_set_dim_has_any_lower_bound(__isl_keep isl_set
*set
,
11323 enum isl_dim_type type
, unsigned pos
)
11325 return has_any_bound(set
, type
, pos
,
11326 &isl_basic_map_dim_has_lower_bound
);
11329 /* Return 1 if the specified dim is involved in any upper bound.
11331 isl_bool
isl_set_dim_has_any_upper_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_upper_bound
);
11338 /* Does "map" have a bound (according to "fn") for all of its basic maps?
11340 static isl_bool
has_bound(__isl_keep isl_map
*map
,
11341 enum isl_dim_type type
, unsigned pos
,
11342 isl_bool (*fn
)(__isl_keep isl_basic_map
*bmap
,
11343 enum isl_dim_type type
, unsigned pos
))
11348 return isl_bool_error
;
11350 for (i
= 0; i
< map
->n
; ++i
) {
11352 bounded
= fn(map
->p
[i
], type
, pos
);
11353 if (bounded
< 0 || !bounded
)
11357 return isl_bool_true
;
11360 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
11362 isl_bool
isl_set_dim_has_lower_bound(__isl_keep isl_set
*set
,
11363 enum isl_dim_type type
, unsigned pos
)
11365 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_lower_bound
);
11368 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
11370 isl_bool
isl_set_dim_has_upper_bound(__isl_keep isl_set
*set
,
11371 enum isl_dim_type type
, unsigned pos
)
11373 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_upper_bound
);
11376 /* For each of the "n" variables starting at "first", determine
11377 * the sign of the variable and put the results in the first "n"
11378 * elements of the array "signs".
11380 * 1 means that the variable is non-negative
11381 * -1 means that the variable is non-positive
11382 * 0 means the variable attains both positive and negative values.
11384 isl_stat
isl_basic_set_vars_get_sign(__isl_keep isl_basic_set
*bset
,
11385 unsigned first
, unsigned n
, int *signs
)
11387 isl_vec
*bound
= NULL
;
11388 struct isl_tab
*tab
= NULL
;
11389 struct isl_tab_undo
*snap
;
11393 total
= isl_basic_set_dim(bset
, isl_dim_all
);
11394 if (total
< 0 || !signs
)
11395 return isl_stat_error
;
11397 bound
= isl_vec_alloc(bset
->ctx
, 1 + total
);
11398 tab
= isl_tab_from_basic_set(bset
, 0);
11399 if (!bound
|| !tab
)
11402 isl_seq_clr(bound
->el
, bound
->size
);
11403 isl_int_set_si(bound
->el
[0], -1);
11405 snap
= isl_tab_snap(tab
);
11406 for (i
= 0; i
< n
; ++i
) {
11409 isl_int_set_si(bound
->el
[1 + first
+ i
], -1);
11410 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
11412 empty
= tab
->empty
;
11413 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
11414 if (isl_tab_rollback(tab
, snap
) < 0)
11422 isl_int_set_si(bound
->el
[1 + first
+ i
], 1);
11423 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
11425 empty
= tab
->empty
;
11426 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
11427 if (isl_tab_rollback(tab
, snap
) < 0)
11430 signs
[i
] = empty
? -1 : 0;
11434 isl_vec_free(bound
);
11435 return isl_stat_ok
;
11438 isl_vec_free(bound
);
11439 return isl_stat_error
;
11442 isl_stat
isl_basic_set_dims_get_sign(__isl_keep isl_basic_set
*bset
,
11443 enum isl_dim_type type
, unsigned first
, unsigned n
, int *signs
)
11445 if (!bset
|| !signs
)
11446 return isl_stat_error
;
11447 if (isl_basic_set_check_range(bset
, type
, first
, n
) < 0)
11448 return isl_stat_error
;
11450 first
+= pos(bset
->dim
, type
) - 1;
11451 return isl_basic_set_vars_get_sign(bset
, first
, n
, signs
);
11454 /* Is it possible for the integer division "div" to depend (possibly
11455 * indirectly) on any output dimensions?
11457 * If the div is undefined, then we conservatively assume that it
11458 * may depend on them.
11459 * Otherwise, we check if it actually depends on them or on any integer
11460 * divisions that may depend on them.
11462 static isl_bool
div_may_involve_output(__isl_keep isl_basic_map
*bmap
, int div
)
11465 isl_size n_out
, n_div
;
11466 unsigned o_out
, o_div
;
11468 if (isl_int_is_zero(bmap
->div
[div
][0]))
11469 return isl_bool_true
;
11471 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
11473 return isl_bool_error
;
11474 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
11476 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + o_out
, n_out
) != -1)
11477 return isl_bool_true
;
11479 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
11481 return isl_bool_error
;
11482 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
11484 for (i
= 0; i
< n_div
; ++i
) {
11485 isl_bool may_involve
;
11487 if (isl_int_is_zero(bmap
->div
[div
][1 + o_div
+ i
]))
11489 may_involve
= div_may_involve_output(bmap
, i
);
11490 if (may_involve
< 0 || may_involve
)
11491 return may_involve
;
11494 return isl_bool_false
;
11497 /* Return the first integer division of "bmap" in the range
11498 * [first, first + n[ that may depend on any output dimensions and
11499 * that has a non-zero coefficient in "c" (where the first coefficient
11500 * in "c" corresponds to integer division "first").
11502 static int first_div_may_involve_output(__isl_keep isl_basic_map
*bmap
,
11503 isl_int
*c
, int first
, int n
)
11510 for (k
= first
; k
< first
+ n
; ++k
) {
11511 isl_bool may_involve
;
11513 if (isl_int_is_zero(c
[k
]))
11515 may_involve
= div_may_involve_output(bmap
, k
);
11516 if (may_involve
< 0)
11525 /* Look for a pair of inequality constraints in "bmap" of the form
11527 * -l + i >= 0 or i >= l
11529 * n + l - i >= 0 or i <= l + n
11531 * with n < "m" and i the output dimension at position "pos".
11532 * (Note that n >= 0 as otherwise the two constraints would conflict.)
11533 * Furthermore, "l" is only allowed to involve parameters, input dimensions
11534 * and earlier output dimensions, as well as integer divisions that do
11535 * not involve any of the output dimensions.
11537 * Return the index of the first inequality constraint or bmap->n_ineq
11538 * if no such pair can be found.
11540 static int find_modulo_constraint_pair(__isl_keep isl_basic_map
*bmap
,
11541 int pos
, isl_int m
)
11546 isl_size n_div
, n_out
;
11547 unsigned o_div
, o_out
;
11550 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
11551 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
11552 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
11553 if (total
< 0 || n_out
< 0 || n_div
< 0)
11556 ctx
= isl_basic_map_get_ctx(bmap
);
11557 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
11558 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
11559 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
11560 if (!isl_int_abs_eq(bmap
->ineq
[i
][o_out
+ pos
], ctx
->one
))
11562 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + o_out
+ pos
+ 1,
11563 n_out
- (pos
+ 1)) != -1)
11565 if (first_div_may_involve_output(bmap
, bmap
->ineq
[i
] + o_div
,
11568 for (j
= i
+ 1; j
< bmap
->n_ineq
; ++j
) {
11569 if (!isl_int_abs_eq(bmap
->ineq
[j
][o_out
+ pos
],
11572 if (!isl_seq_is_neg(bmap
->ineq
[i
] + 1,
11573 bmap
->ineq
[j
] + 1, total
))
11577 if (j
>= bmap
->n_ineq
)
11579 isl_int_add(bmap
->ineq
[i
][0],
11580 bmap
->ineq
[i
][0], bmap
->ineq
[j
][0]);
11581 less
= isl_int_abs_lt(bmap
->ineq
[i
][0], m
);
11582 isl_int_sub(bmap
->ineq
[i
][0],
11583 bmap
->ineq
[i
][0], bmap
->ineq
[j
][0]);
11586 if (isl_int_is_one(bmap
->ineq
[i
][o_out
+ pos
]))
11592 return bmap
->n_ineq
;
11595 /* Return the index of the equality of "bmap" that defines
11596 * the output dimension "pos" in terms of earlier dimensions.
11597 * The equality may also involve integer divisions, as long
11598 * as those integer divisions are defined in terms of
11599 * parameters or input dimensions.
11600 * In this case, *div is set to the number of integer divisions and
11601 * *ineq is set to the number of inequality constraints (provided
11602 * div and ineq are not NULL).
11604 * The equality may also involve a single integer division involving
11605 * the output dimensions (typically only output dimension "pos") as
11606 * long as the coefficient of output dimension "pos" is 1 or -1 and
11607 * there is a pair of constraints i >= l and i <= l + n, with i referring
11608 * to output dimension "pos", l an expression involving only earlier
11609 * dimensions and n smaller than the coefficient of the integer division
11610 * in the equality. In this case, the output dimension can be defined
11611 * in terms of a modulo expression that does not involve the integer division.
11612 * *div is then set to this single integer division and
11613 * *ineq is set to the index of constraint i >= l.
11615 * Return bmap->n_eq if there is no such equality.
11616 * Return -1 on error.
11618 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map
*bmap
,
11619 int pos
, int *div
, int *ineq
)
11622 isl_size n_div
, n_out
;
11623 unsigned o_div
, o_out
;
11625 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
11626 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
11627 if (n_out
< 0 || n_div
< 0)
11630 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
11631 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
11634 *ineq
= bmap
->n_ineq
;
11637 for (j
= 0; j
< bmap
->n_eq
; ++j
) {
11638 if (isl_int_is_zero(bmap
->eq
[j
][o_out
+ pos
]))
11640 if (isl_seq_first_non_zero(bmap
->eq
[j
] + o_out
+ pos
+ 1,
11641 n_out
- (pos
+ 1)) != -1)
11643 k
= first_div_may_involve_output(bmap
, bmap
->eq
[j
] + o_div
,
11647 if (!isl_int_is_one(bmap
->eq
[j
][o_out
+ pos
]) &&
11648 !isl_int_is_negone(bmap
->eq
[j
][o_out
+ pos
]))
11650 if (first_div_may_involve_output(bmap
, bmap
->eq
[j
] + o_div
,
11651 k
+ 1, n_div
- (k
+1)) < n_div
)
11653 l
= find_modulo_constraint_pair(bmap
, pos
,
11654 bmap
->eq
[j
][o_div
+ k
]);
11657 if (l
>= bmap
->n_ineq
)
11669 /* Check if the given basic map is obviously single-valued.
11670 * In particular, for each output dimension, check that there is
11671 * an equality that defines the output dimension in terms of
11672 * earlier dimensions.
11674 isl_bool
isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map
*bmap
)
11679 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
11681 return isl_bool_error
;
11683 for (i
= 0; i
< n_out
; ++i
) {
11686 eq
= isl_basic_map_output_defining_equality(bmap
, i
,
11689 return isl_bool_error
;
11690 if (eq
>= bmap
->n_eq
)
11691 return isl_bool_false
;
11694 return isl_bool_true
;
11697 /* Check if the given basic map is single-valued.
11698 * We simply compute
11702 * and check if the result is a subset of the identity mapping.
11704 isl_bool
isl_basic_map_is_single_valued(__isl_keep isl_basic_map
*bmap
)
11707 isl_basic_map
*test
;
11711 sv
= isl_basic_map_plain_is_single_valued(bmap
);
11715 test
= isl_basic_map_reverse(isl_basic_map_copy(bmap
));
11716 test
= isl_basic_map_apply_range(test
, isl_basic_map_copy(bmap
));
11718 space
= isl_basic_map_get_space(bmap
);
11719 space
= isl_space_map_from_set(isl_space_range(space
));
11720 id
= isl_basic_map_identity(space
);
11722 sv
= isl_basic_map_is_subset(test
, id
);
11724 isl_basic_map_free(test
);
11725 isl_basic_map_free(id
);
11730 /* Check if the given map is obviously single-valued.
11732 isl_bool
isl_map_plain_is_single_valued(__isl_keep isl_map
*map
)
11735 return isl_bool_error
;
11737 return isl_bool_true
;
11739 return isl_bool_false
;
11741 return isl_basic_map_plain_is_single_valued(map
->p
[0]);
11744 /* Check if the given map is single-valued.
11745 * We simply compute
11749 * and check if the result is a subset of the identity mapping.
11751 isl_bool
isl_map_is_single_valued(__isl_keep isl_map
*map
)
11758 sv
= isl_map_plain_is_single_valued(map
);
11762 test
= isl_map_reverse(isl_map_copy(map
));
11763 test
= isl_map_apply_range(test
, isl_map_copy(map
));
11765 space
= isl_space_map_from_set(isl_space_range(isl_map_get_space(map
)));
11766 id
= isl_map_identity(space
);
11768 sv
= isl_map_is_subset(test
, id
);
11770 isl_map_free(test
);
11776 isl_bool
isl_map_is_injective(__isl_keep isl_map
*map
)
11780 map
= isl_map_copy(map
);
11781 map
= isl_map_reverse(map
);
11782 in
= isl_map_is_single_valued(map
);
11788 /* Check if the given map is obviously injective.
11790 isl_bool
isl_map_plain_is_injective(__isl_keep isl_map
*map
)
11794 map
= isl_map_copy(map
);
11795 map
= isl_map_reverse(map
);
11796 in
= isl_map_plain_is_single_valued(map
);
11802 isl_bool
isl_map_is_bijective(__isl_keep isl_map
*map
)
11806 sv
= isl_map_is_single_valued(map
);
11810 return isl_map_is_injective(map
);
11813 isl_bool
isl_set_is_singleton(__isl_keep isl_set
*set
)
11815 return isl_map_is_single_valued(set_to_map(set
));
11818 /* Does "map" only map elements to themselves?
11820 * If the domain and range spaces are different, then "map"
11821 * is considered not to be an identity relation, even if it is empty.
11822 * Otherwise, construct the maximal identity relation and
11823 * check whether "map" is a subset of this relation.
11825 isl_bool
isl_map_is_identity(__isl_keep isl_map
*map
)
11828 isl_bool equal
, is_identity
;
11830 equal
= isl_map_tuple_is_equal(map
, isl_dim_in
, map
, isl_dim_out
);
11831 if (equal
< 0 || !equal
)
11834 id
= isl_map_identity(isl_map_get_space(map
));
11835 is_identity
= isl_map_is_subset(map
, id
);
11838 return is_identity
;
11841 int isl_map_is_translation(__isl_keep isl_map
*map
)
11846 delta
= isl_map_deltas(isl_map_copy(map
));
11847 ok
= isl_set_is_singleton(delta
);
11848 isl_set_free(delta
);
11853 static int unique(isl_int
*p
, unsigned pos
, unsigned len
)
11855 if (isl_seq_first_non_zero(p
, pos
) != -1)
11857 if (isl_seq_first_non_zero(p
+ pos
+ 1, len
- pos
- 1) != -1)
11862 isl_bool
isl_basic_set_is_box(__isl_keep isl_basic_set
*bset
)
11865 isl_size nvar
, n_div
;
11868 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
11870 return isl_bool_error
;
11872 return isl_bool_false
;
11874 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
11876 return isl_bool_error
;
11877 ovar
= isl_space_offset(bset
->dim
, isl_dim_set
);
11878 for (j
= 0; j
< nvar
; ++j
) {
11879 int lower
= 0, upper
= 0;
11880 for (i
= 0; i
< bset
->n_eq
; ++i
) {
11881 if (isl_int_is_zero(bset
->eq
[i
][1 + ovar
+ j
]))
11883 if (!unique(bset
->eq
[i
] + 1 + ovar
, j
, nvar
))
11884 return isl_bool_false
;
11887 if (i
< bset
->n_eq
)
11889 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
11890 if (isl_int_is_zero(bset
->ineq
[i
][1 + ovar
+ j
]))
11892 if (!unique(bset
->ineq
[i
] + 1 + ovar
, j
, nvar
))
11893 return isl_bool_false
;
11894 if (isl_int_is_pos(bset
->ineq
[i
][1 + ovar
+ j
]))
11899 if (!lower
|| !upper
)
11900 return isl_bool_false
;
11903 return isl_bool_true
;
11906 isl_bool
isl_set_is_box(__isl_keep isl_set
*set
)
11909 return isl_bool_error
;
11911 return isl_bool_false
;
11913 return isl_basic_set_is_box(set
->p
[0]);
11916 isl_bool
isl_basic_set_is_wrapping(__isl_keep isl_basic_set
*bset
)
11919 return isl_bool_error
;
11921 return isl_space_is_wrapping(bset
->dim
);
11924 isl_bool
isl_set_is_wrapping(__isl_keep isl_set
*set
)
11927 return isl_bool_error
;
11929 return isl_space_is_wrapping(set
->dim
);
11932 /* Modify the space of "map" through a call to "change".
11933 * If "can_change" is set (not NULL), then first call it to check
11934 * if the modification is allowed, printing the error message "cannot_change"
11937 static __isl_give isl_map
*isl_map_change_space(__isl_take isl_map
*map
,
11938 isl_bool (*can_change
)(__isl_keep isl_map
*map
),
11939 const char *cannot_change
,
11940 __isl_give isl_space
*(*change
)(__isl_take isl_space
*space
))
11948 ok
= can_change
? can_change(map
) : isl_bool_true
;
11950 return isl_map_free(map
);
11952 isl_die(isl_map_get_ctx(map
), isl_error_invalid
, cannot_change
,
11953 return isl_map_free(map
));
11955 space
= change(isl_map_get_space(map
));
11956 map
= isl_map_reset_space(map
, space
);
11961 /* Is the domain of "map" a wrapped relation?
11963 isl_bool
isl_map_domain_is_wrapping(__isl_keep isl_map
*map
)
11966 return isl_bool_error
;
11968 return isl_space_domain_is_wrapping(map
->dim
);
11971 /* Does "map" have a wrapped relation in both domain and range?
11973 isl_bool
isl_map_is_product(__isl_keep isl_map
*map
)
11975 return isl_space_is_product(isl_map_peek_space(map
));
11978 /* Is the range of "map" a wrapped relation?
11980 isl_bool
isl_map_range_is_wrapping(__isl_keep isl_map
*map
)
11983 return isl_bool_error
;
11985 return isl_space_range_is_wrapping(map
->dim
);
11988 __isl_give isl_basic_set
*isl_basic_map_wrap(__isl_take isl_basic_map
*bmap
)
11992 space
= isl_basic_map_take_space(bmap
);
11993 space
= isl_space_wrap(space
);
11994 bmap
= isl_basic_map_restore_space(bmap
, space
);
11996 bmap
= isl_basic_map_finalize(bmap
);
11998 return bset_from_bmap(bmap
);
12001 /* Given a map A -> B, return the set (A -> B).
12003 __isl_give isl_set
*isl_map_wrap(__isl_take isl_map
*map
)
12005 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_wrap
);
12008 __isl_give isl_basic_map
*isl_basic_set_unwrap(__isl_take isl_basic_set
*bset
)
12010 bset
= isl_basic_set_cow(bset
);
12014 bset
->dim
= isl_space_unwrap(bset
->dim
);
12018 bset
= isl_basic_set_finalize(bset
);
12020 return bset_to_bmap(bset
);
12022 isl_basic_set_free(bset
);
12026 /* Given a set (A -> B), return the map A -> B.
12027 * Error out if "set" is not of the form (A -> B).
12029 __isl_give isl_map
*isl_set_unwrap(__isl_take isl_set
*set
)
12031 return isl_map_change_space(set
, &isl_set_is_wrapping
,
12032 "not a wrapping set", &isl_space_unwrap
);
12035 __isl_give isl_basic_map
*isl_basic_map_reset(__isl_take isl_basic_map
*bmap
,
12036 enum isl_dim_type type
)
12040 space
= isl_basic_map_take_space(bmap
);
12041 space
= isl_space_reset(space
, type
);
12042 bmap
= isl_basic_map_restore_space(bmap
, space
);
12044 bmap
= isl_basic_map_mark_final(bmap
);
12049 __isl_give isl_map
*isl_map_reset(__isl_take isl_map
*map
,
12050 enum isl_dim_type type
)
12058 if (!isl_space_is_named_or_nested(map
->dim
, type
))
12061 map
= isl_map_cow(map
);
12065 for (i
= 0; i
< map
->n
; ++i
) {
12066 map
->p
[i
] = isl_basic_map_reset(map
->p
[i
], type
);
12071 space
= isl_map_take_space(map
);
12072 space
= isl_space_reset(space
, type
);
12073 map
= isl_map_restore_space(map
, space
);
12081 __isl_give isl_basic_map
*isl_basic_map_flatten(__isl_take isl_basic_map
*bmap
)
12085 space
= isl_basic_map_take_space(bmap
);
12086 space
= isl_space_flatten(space
);
12087 bmap
= isl_basic_map_restore_space(bmap
, space
);
12089 bmap
= isl_basic_map_mark_final(bmap
);
12094 __isl_give isl_basic_set
*isl_basic_set_flatten(__isl_take isl_basic_set
*bset
)
12096 return bset_from_bmap(isl_basic_map_flatten(bset_to_bmap(bset
)));
12099 __isl_give isl_basic_map
*isl_basic_map_flatten_domain(
12100 __isl_take isl_basic_map
*bmap
)
12104 space
= isl_basic_map_take_space(bmap
);
12105 space
= isl_space_flatten_domain(space
);
12106 bmap
= isl_basic_map_restore_space(bmap
, space
);
12108 bmap
= isl_basic_map_mark_final(bmap
);
12113 __isl_give isl_basic_map
*isl_basic_map_flatten_range(
12114 __isl_take isl_basic_map
*bmap
)
12118 space
= isl_basic_map_take_space(bmap
);
12119 space
= isl_space_flatten_range(space
);
12120 bmap
= isl_basic_map_restore_space(bmap
, space
);
12122 bmap
= isl_basic_map_mark_final(bmap
);
12127 /* Remove any internal structure from the spaces of domain and range of "map".
12129 __isl_give isl_map
*isl_map_flatten(__isl_take isl_map
*map
)
12134 if (!map
->dim
->nested
[0] && !map
->dim
->nested
[1])
12137 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten
);
12140 __isl_give isl_set
*isl_set_flatten(__isl_take isl_set
*set
)
12142 return set_from_map(isl_map_flatten(set_to_map(set
)));
12145 __isl_give isl_map
*isl_set_flatten_map(__isl_take isl_set
*set
)
12147 isl_space
*space
, *flat_space
;
12150 space
= isl_set_get_space(set
);
12151 flat_space
= isl_space_flatten(isl_space_copy(space
));
12152 map
= isl_map_identity(isl_space_join(isl_space_reverse(space
),
12154 map
= isl_map_intersect_domain(map
, set
);
12159 /* Remove any internal structure from the space of the domain of "map".
12161 __isl_give isl_map
*isl_map_flatten_domain(__isl_take isl_map
*map
)
12166 if (!map
->dim
->nested
[0])
12169 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten_domain
);
12172 /* Remove any internal structure from the space of the range of "map".
12174 __isl_give isl_map
*isl_map_flatten_range(__isl_take isl_map
*map
)
12179 if (!map
->dim
->nested
[1])
12182 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten_range
);
12185 /* Reorder the dimensions of "bmap" according to the given dim_map
12186 * and set the dimension specification to "space" and
12187 * perform Gaussian elimination on the result.
12189 __isl_give isl_basic_map
*isl_basic_map_realign(__isl_take isl_basic_map
*bmap
,
12190 __isl_take isl_space
*space
, __isl_take
struct isl_dim_map
*dim_map
)
12192 isl_basic_map
*res
;
12196 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
12197 if (n_div
< 0 || !space
|| !dim_map
)
12200 flags
= bmap
->flags
;
12201 ISL_FL_CLR(flags
, ISL_BASIC_MAP_FINAL
);
12202 ISL_FL_CLR(flags
, ISL_BASIC_MAP_SORTED
);
12203 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
12204 res
= isl_basic_map_alloc_space(space
, n_div
, bmap
->n_eq
, bmap
->n_ineq
);
12205 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
12207 res
->flags
= flags
;
12208 res
= isl_basic_map_gauss(res
, NULL
);
12209 res
= isl_basic_map_finalize(res
);
12212 isl_dim_map_free(dim_map
);
12213 isl_basic_map_free(bmap
);
12214 isl_space_free(space
);
12218 /* Reorder the dimensions of "map" according to given reordering.
12220 __isl_give isl_map
*isl_map_realign(__isl_take isl_map
*map
,
12221 __isl_take isl_reordering
*r
)
12224 struct isl_dim_map
*dim_map
;
12226 map
= isl_map_cow(map
);
12227 dim_map
= isl_dim_map_from_reordering(r
);
12228 if (!map
|| !r
|| !dim_map
)
12231 for (i
= 0; i
< map
->n
; ++i
) {
12232 struct isl_dim_map
*dim_map_i
;
12235 dim_map_i
= isl_dim_map_extend(dim_map
, map
->p
[i
]);
12237 space
= isl_reordering_get_space(r
);
12238 map
->p
[i
] = isl_basic_map_realign(map
->p
[i
], space
, dim_map_i
);
12244 map
= isl_map_reset_space(map
, isl_reordering_get_space(r
));
12245 map
= isl_map_unmark_normalized(map
);
12247 isl_reordering_free(r
);
12248 isl_dim_map_free(dim_map
);
12251 isl_dim_map_free(dim_map
);
12253 isl_reordering_free(r
);
12257 __isl_give isl_set
*isl_set_realign(__isl_take isl_set
*set
,
12258 __isl_take isl_reordering
*r
)
12260 return set_from_map(isl_map_realign(set_to_map(set
), r
));
12263 __isl_give isl_map
*isl_map_align_params(__isl_take isl_map
*map
,
12264 __isl_take isl_space
*model
)
12269 if (!map
|| !model
)
12272 ctx
= isl_space_get_ctx(model
);
12273 if (!isl_space_has_named_params(model
))
12274 isl_die(ctx
, isl_error_invalid
,
12275 "model has unnamed parameters", goto error
);
12276 if (isl_map_check_named_params(map
) < 0)
12278 aligned
= isl_map_space_has_equal_params(map
, model
);
12282 isl_reordering
*exp
;
12284 exp
= isl_parameter_alignment_reordering(map
->dim
, model
);
12285 exp
= isl_reordering_extend_space(exp
, isl_map_get_space(map
));
12286 map
= isl_map_realign(map
, exp
);
12289 isl_space_free(model
);
12292 isl_space_free(model
);
12297 __isl_give isl_set
*isl_set_align_params(__isl_take isl_set
*set
,
12298 __isl_take isl_space
*model
)
12300 return isl_map_align_params(set
, model
);
12303 /* Align the parameters of "bmap" to those of "model", introducing
12304 * additional parameters if needed.
12306 __isl_give isl_basic_map
*isl_basic_map_align_params(
12307 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*model
)
12310 isl_bool equal_params
;
12312 if (!bmap
|| !model
)
12315 ctx
= isl_space_get_ctx(model
);
12316 if (!isl_space_has_named_params(model
))
12317 isl_die(ctx
, isl_error_invalid
,
12318 "model has unnamed parameters", goto error
);
12319 if (isl_basic_map_check_named_params(bmap
) < 0)
12321 equal_params
= isl_space_has_equal_params(bmap
->dim
, model
);
12322 if (equal_params
< 0)
12324 if (!equal_params
) {
12325 isl_reordering
*exp
;
12326 struct isl_dim_map
*dim_map
;
12328 exp
= isl_parameter_alignment_reordering(bmap
->dim
, model
);
12329 exp
= isl_reordering_extend_space(exp
,
12330 isl_basic_map_get_space(bmap
));
12331 dim_map
= isl_dim_map_from_reordering(exp
);
12332 bmap
= isl_basic_map_realign(bmap
,
12333 isl_reordering_get_space(exp
),
12334 isl_dim_map_extend(dim_map
, bmap
));
12335 isl_reordering_free(exp
);
12336 isl_dim_map_free(dim_map
);
12339 isl_space_free(model
);
12342 isl_space_free(model
);
12343 isl_basic_map_free(bmap
);
12347 /* Do "bset" and "space" have the same parameters?
12349 isl_bool
isl_basic_set_space_has_equal_params(__isl_keep isl_basic_set
*bset
,
12350 __isl_keep isl_space
*space
)
12352 isl_space
*bset_space
;
12354 bset_space
= isl_basic_set_peek_space(bset
);
12355 return isl_space_has_equal_params(bset_space
, space
);
12358 /* Do "map" and "space" have the same parameters?
12360 isl_bool
isl_map_space_has_equal_params(__isl_keep isl_map
*map
,
12361 __isl_keep isl_space
*space
)
12363 isl_space
*map_space
;
12365 map_space
= isl_map_peek_space(map
);
12366 return isl_space_has_equal_params(map_space
, space
);
12369 /* Do "set" and "space" have the same parameters?
12371 isl_bool
isl_set_space_has_equal_params(__isl_keep isl_set
*set
,
12372 __isl_keep isl_space
*space
)
12374 return isl_map_space_has_equal_params(set_to_map(set
), space
);
12377 /* Align the parameters of "bset" to those of "model", introducing
12378 * additional parameters if needed.
12380 __isl_give isl_basic_set
*isl_basic_set_align_params(
12381 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*model
)
12383 return isl_basic_map_align_params(bset
, model
);
12386 /* Drop all parameters not referenced by "map".
12388 __isl_give isl_map
*isl_map_drop_unused_params(__isl_take isl_map
*map
)
12393 n
= isl_map_dim(map
, isl_dim_param
);
12394 if (isl_map_check_named_params(map
) < 0 || n
< 0)
12395 return isl_map_free(map
);
12397 for (i
= n
- 1; i
>= 0; i
--) {
12400 involves
= isl_map_involves_dims(map
, isl_dim_param
, i
, 1);
12402 return isl_map_free(map
);
12404 map
= isl_map_project_out(map
, isl_dim_param
, i
, 1);
12410 /* Drop all parameters not referenced by "set".
12412 __isl_give isl_set
*isl_set_drop_unused_params(
12413 __isl_take isl_set
*set
)
12415 return set_from_map(isl_map_drop_unused_params(set_to_map(set
)));
12418 /* Drop all parameters not referenced by "bmap".
12420 __isl_give isl_basic_map
*isl_basic_map_drop_unused_params(
12421 __isl_take isl_basic_map
*bmap
)
12426 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
12427 if (nparam
< 0 || isl_basic_map_check_named_params(bmap
) < 0)
12428 return isl_basic_map_free(bmap
);
12430 for (i
= nparam
- 1; i
>= 0; i
--) {
12433 involves
= isl_basic_map_involves_dims(bmap
,
12434 isl_dim_param
, i
, 1);
12436 return isl_basic_map_free(bmap
);
12438 bmap
= isl_basic_map_drop(bmap
, isl_dim_param
, i
, 1);
12444 /* Drop all parameters not referenced by "bset".
12446 __isl_give isl_basic_set
*isl_basic_set_drop_unused_params(
12447 __isl_take isl_basic_set
*bset
)
12449 return bset_from_bmap(isl_basic_map_drop_unused_params(
12450 bset_to_bmap(bset
)));
12453 /* Given a tuple of identifiers "tuple" in a space that corresponds
12454 * to that of "set", if any of those identifiers appear as parameters
12455 * in "set", then equate those parameters with the corresponding
12456 * set dimensions and project out the parameters.
12457 * The result therefore has no such parameters.
12459 static __isl_give isl_set
*equate_params(__isl_take isl_set
*set
,
12460 __isl_keep isl_multi_id
*tuple
)
12464 isl_space
*set_space
, *tuple_space
;
12466 set_space
= isl_set_peek_space(set
);
12467 tuple_space
= isl_multi_id_peek_space(tuple
);
12468 if (isl_space_check_equal_tuples(tuple_space
, set_space
) < 0)
12469 return isl_set_free(set
);
12470 n
= isl_multi_id_size(tuple
);
12472 return isl_set_free(set
);
12473 for (i
= 0; i
< n
; ++i
) {
12477 id
= isl_multi_id_get_at(tuple
, i
);
12479 return isl_set_free(set
);
12480 pos
= isl_set_find_dim_by_id(set
, isl_dim_param
, id
);
12484 set
= isl_set_equate(set
, isl_dim_param
, pos
, isl_dim_set
, i
);
12485 set
= isl_set_project_out(set
, isl_dim_param
, pos
, 1);
12490 /* Bind the set dimensions of "set" to parameters with identifiers
12491 * specified by "tuple", living in the same space as "set".
12493 * If no parameters with these identifiers appear in "set" already,
12494 * then the set dimensions are simply reinterpreted as parameters.
12495 * Otherwise, the parameters are first equated to the corresponding
12498 __isl_give isl_set
*isl_set_bind(__isl_take isl_set
*set
,
12499 __isl_take isl_multi_id
*tuple
)
12503 set
= equate_params(set
, tuple
);
12504 space
= isl_set_get_space(set
);
12505 space
= isl_space_bind_set(space
, tuple
);
12506 isl_multi_id_free(tuple
);
12507 set
= isl_set_reset_space(set
, space
);
12512 /* Given a tuple of identifiers "tuple" in a space that corresponds
12513 * to the domain of "map", if any of those identifiers appear as parameters
12514 * in "map", then equate those parameters with the corresponding
12515 * input dimensions and project out the parameters.
12516 * The result therefore has no such parameters.
12518 static __isl_give isl_map
*map_equate_params(__isl_take isl_map
*map
,
12519 __isl_keep isl_multi_id
*tuple
)
12523 isl_space
*map_space
, *tuple_space
;
12525 map_space
= isl_map_peek_space(map
);
12526 tuple_space
= isl_multi_id_peek_space(tuple
);
12527 if (isl_space_check_domain_tuples(tuple_space
, map_space
) < 0)
12528 return isl_map_free(map
);
12529 n
= isl_multi_id_size(tuple
);
12531 return isl_map_free(map
);
12532 for (i
= 0; i
< n
; ++i
) {
12536 id
= isl_multi_id_get_at(tuple
, i
);
12538 return isl_map_free(map
);
12539 pos
= isl_map_find_dim_by_id(map
, isl_dim_param
, id
);
12543 map
= isl_map_equate(map
, isl_dim_param
, pos
, isl_dim_in
, i
);
12544 map
= isl_map_project_out(map
, isl_dim_param
, pos
, 1);
12549 /* Bind the input dimensions of "map" to parameters with identifiers
12550 * specified by "tuple", living in the domain space of "map".
12552 * If no parameters with these identifiers appear in "map" already,
12553 * then the input dimensions are simply reinterpreted as parameters.
12554 * Otherwise, the parameters are first equated to the corresponding
12555 * input dimensions.
12557 __isl_give isl_set
*isl_map_bind_domain(__isl_take isl_map
*map
,
12558 __isl_take isl_multi_id
*tuple
)
12563 map
= map_equate_params(map
, tuple
);
12564 space
= isl_map_get_space(map
);
12565 space
= isl_space_bind_map_domain(space
, tuple
);
12566 isl_multi_id_free(tuple
);
12567 set
= set_from_map(isl_map_reset_space(map
, space
));
12572 /* Bind the output dimensions of "map" to parameters with identifiers
12573 * specified by "tuple", living in the range space of "map".
12575 * Since binding is more easily implemented on the domain,
12576 * bind the input dimensions of the inverse of "map".
12578 __isl_give isl_set
*isl_map_bind_range(__isl_take isl_map
*map
,
12579 __isl_take isl_multi_id
*tuple
)
12581 return isl_map_bind_domain(isl_map_reverse(map
), tuple
);
12584 /* Insert a domain corresponding to "tuple"
12585 * into the nullary or unary relation "set".
12586 * The result has an extra initial tuple and is therefore
12587 * either a unary or binary relation.
12588 * Any parameters with identifiers in "tuple" are reinterpreted
12589 * as the corresponding domain dimensions.
12591 static __isl_give isl_map
*unbind_params_insert_domain(
12592 __isl_take isl_set
*set
, __isl_take isl_multi_id
*tuple
)
12597 space
= isl_set_peek_space(set
);
12598 r
= isl_reordering_unbind_params_insert_domain(space
, tuple
);
12599 isl_multi_id_free(tuple
);
12601 return isl_map_realign(set_to_map(set
), r
);
12604 /* Construct a set with "tuple" as domain from the parameter domain "set".
12605 * Any parameters with identifiers in "tuple" are reinterpreted
12606 * as the corresponding set dimensions.
12608 __isl_give isl_set
*isl_set_unbind_params(__isl_take isl_set
*set
,
12609 __isl_take isl_multi_id
*tuple
)
12611 isl_bool is_params
;
12613 is_params
= isl_set_is_params(set
);
12615 set
= isl_set_free(set
);
12616 else if (!is_params
)
12617 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
12618 "expecting parameter domain", set
= isl_set_free(set
));
12619 return set_from_map(unbind_params_insert_domain(set
, tuple
));
12622 /* Construct a map with "domain" as domain and "set" as range.
12623 * Any parameters with identifiers in "domain" are reinterpreted
12624 * as the corresponding domain dimensions.
12626 __isl_give isl_map
*isl_set_unbind_params_insert_domain(
12627 __isl_take isl_set
*set
, __isl_take isl_multi_id
*domain
)
12629 isl_bool is_params
;
12631 is_params
= isl_set_is_params(set
);
12633 set
= isl_set_free(set
);
12634 else if (is_params
)
12635 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
12636 "expecting proper set", set
= isl_set_free(set
));
12637 return unbind_params_insert_domain(set
, domain
);
12640 __isl_give isl_mat
*isl_basic_map_equalities_matrix(
12641 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
12642 enum isl_dim_type c2
, enum isl_dim_type c3
,
12643 enum isl_dim_type c4
, enum isl_dim_type c5
)
12645 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
12646 struct isl_mat
*mat
;
12651 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
12654 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_eq
, total
+ 1);
12657 for (i
= 0; i
< bmap
->n_eq
; ++i
)
12658 for (j
= 0, pos
= 0; j
< 5; ++j
) {
12659 int off
= isl_basic_map_offset(bmap
, c
[j
]);
12660 isl_size dim
= isl_basic_map_dim(bmap
, c
[j
]);
12662 return isl_mat_free(mat
);
12663 for (k
= 0; k
< dim
; ++k
) {
12664 isl_int_set(mat
->row
[i
][pos
],
12665 bmap
->eq
[i
][off
+ k
]);
12673 __isl_give isl_mat
*isl_basic_map_inequalities_matrix(
12674 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
12675 enum isl_dim_type c2
, enum isl_dim_type c3
,
12676 enum isl_dim_type c4
, enum isl_dim_type c5
)
12678 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
12679 struct isl_mat
*mat
;
12684 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
12687 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_ineq
, total
+ 1);
12690 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
12691 for (j
= 0, pos
= 0; j
< 5; ++j
) {
12692 int off
= isl_basic_map_offset(bmap
, c
[j
]);
12693 isl_size dim
= isl_basic_map_dim(bmap
, c
[j
]);
12695 return isl_mat_free(mat
);
12696 for (k
= 0; k
< dim
; ++k
) {
12697 isl_int_set(mat
->row
[i
][pos
],
12698 bmap
->ineq
[i
][off
+ k
]);
12706 __isl_give isl_basic_map
*isl_basic_map_from_constraint_matrices(
12707 __isl_take isl_space
*space
,
12708 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
12709 enum isl_dim_type c2
, enum isl_dim_type c3
,
12710 enum isl_dim_type c4
, enum isl_dim_type c5
)
12712 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
12713 isl_basic_map
*bmap
= NULL
;
12720 dim
= isl_space_dim(space
, isl_dim_all
);
12721 if (dim
< 0 || !eq
|| !ineq
)
12724 if (eq
->n_col
!= ineq
->n_col
)
12725 isl_die(space
->ctx
, isl_error_invalid
,
12726 "equalities and inequalities matrices should have "
12727 "same number of columns", goto error
);
12731 if (eq
->n_col
< total
)
12732 isl_die(space
->ctx
, isl_error_invalid
,
12733 "number of columns too small", goto error
);
12735 extra
= eq
->n_col
- total
;
12737 bmap
= isl_basic_map_alloc_space(isl_space_copy(space
), extra
,
12738 eq
->n_row
, ineq
->n_row
);
12741 for (i
= 0; i
< extra
; ++i
) {
12742 k
= isl_basic_map_alloc_div(bmap
);
12745 isl_int_set_si(bmap
->div
[k
][0], 0);
12747 for (i
= 0; i
< eq
->n_row
; ++i
) {
12748 l
= isl_basic_map_alloc_equality(bmap
);
12751 for (j
= 0, pos
= 0; j
< 5; ++j
) {
12752 int off
= isl_basic_map_offset(bmap
, c
[j
]);
12753 isl_size dim
= isl_basic_map_dim(bmap
, c
[j
]);
12756 for (k
= 0; k
< dim
; ++k
) {
12757 isl_int_set(bmap
->eq
[l
][off
+ k
],
12763 for (i
= 0; i
< ineq
->n_row
; ++i
) {
12764 l
= isl_basic_map_alloc_inequality(bmap
);
12767 for (j
= 0, pos
= 0; j
< 5; ++j
) {
12768 int off
= isl_basic_map_offset(bmap
, c
[j
]);
12769 isl_size dim
= isl_basic_map_dim(bmap
, c
[j
]);
12772 for (k
= 0; k
< dim
; ++k
) {
12773 isl_int_set(bmap
->ineq
[l
][off
+ k
],
12774 ineq
->row
[i
][pos
]);
12780 isl_space_free(space
);
12782 isl_mat_free(ineq
);
12784 bmap
= isl_basic_map_simplify(bmap
);
12785 return isl_basic_map_finalize(bmap
);
12787 isl_space_free(space
);
12789 isl_mat_free(ineq
);
12790 isl_basic_map_free(bmap
);
12794 __isl_give isl_mat
*isl_basic_set_equalities_matrix(
12795 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
12796 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
12798 return isl_basic_map_equalities_matrix(bset_to_bmap(bset
),
12799 c1
, c2
, c3
, c4
, isl_dim_in
);
12802 __isl_give isl_mat
*isl_basic_set_inequalities_matrix(
12803 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
12804 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
12806 return isl_basic_map_inequalities_matrix(bset_to_bmap(bset
),
12807 c1
, c2
, c3
, c4
, isl_dim_in
);
12810 __isl_give isl_basic_set
*isl_basic_set_from_constraint_matrices(
12811 __isl_take isl_space
*space
,
12812 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
12813 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
12815 isl_basic_map
*bmap
;
12816 bmap
= isl_basic_map_from_constraint_matrices(space
, eq
, ineq
,
12817 c1
, c2
, c3
, c4
, isl_dim_in
);
12818 return bset_from_bmap(bmap
);
12821 isl_bool
isl_basic_map_can_zip(__isl_keep isl_basic_map
*bmap
)
12824 return isl_bool_error
;
12826 return isl_space_can_zip(bmap
->dim
);
12829 isl_bool
isl_map_can_zip(__isl_keep isl_map
*map
)
12832 return isl_bool_error
;
12834 return isl_space_can_zip(map
->dim
);
12837 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
12838 * (A -> C) -> (B -> D).
12840 __isl_give isl_basic_map
*isl_basic_map_zip(__isl_take isl_basic_map
*bmap
)
12850 if (!isl_basic_map_can_zip(bmap
))
12851 isl_die(bmap
->ctx
, isl_error_invalid
,
12852 "basic map cannot be zipped", goto error
);
12853 n_in
= isl_space_dim(bmap
->dim
->nested
[0], isl_dim_in
);
12854 n1
= isl_space_dim(bmap
->dim
->nested
[0], isl_dim_out
);
12855 n2
= isl_space_dim(bmap
->dim
->nested
[1], isl_dim_in
);
12856 if (n_in
< 0 || n1
< 0 || n2
< 0)
12857 return isl_basic_map_free(bmap
);
12858 pos
= isl_basic_map_offset(bmap
, isl_dim_in
) + n_in
;
12859 bmap
= isl_basic_map_cow(bmap
);
12860 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
12863 bmap
->dim
= isl_space_zip(bmap
->dim
);
12866 bmap
= isl_basic_map_mark_final(bmap
);
12869 isl_basic_map_free(bmap
);
12873 /* Given a map (A -> B) -> (C -> D), return the corresponding map
12874 * (A -> C) -> (B -> D).
12876 __isl_give isl_map
*isl_map_zip(__isl_take isl_map
*map
)
12881 if (!isl_map_can_zip(map
))
12882 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be zipped",
12885 return isl_map_transform(map
, &isl_space_zip
, &isl_basic_map_zip
);
12891 /* Can we apply isl_basic_map_curry to "bmap"?
12892 * That is, does it have a nested relation in its domain?
12894 isl_bool
isl_basic_map_can_curry(__isl_keep isl_basic_map
*bmap
)
12897 return isl_bool_error
;
12899 return isl_space_can_curry(bmap
->dim
);
12902 /* Can we apply isl_map_curry to "map"?
12903 * That is, does it have a nested relation in its domain?
12905 isl_bool
isl_map_can_curry(__isl_keep isl_map
*map
)
12908 return isl_bool_error
;
12910 return isl_space_can_curry(map
->dim
);
12913 /* Given a basic map (A -> B) -> C, return the corresponding basic map
12916 __isl_give isl_basic_map
*isl_basic_map_curry(__isl_take isl_basic_map
*bmap
)
12922 if (!isl_basic_map_can_curry(bmap
))
12923 isl_die(bmap
->ctx
, isl_error_invalid
,
12924 "basic map cannot be curried", goto error
);
12925 bmap
= isl_basic_map_cow(bmap
);
12928 bmap
->dim
= isl_space_curry(bmap
->dim
);
12931 bmap
= isl_basic_map_mark_final(bmap
);
12934 isl_basic_map_free(bmap
);
12938 /* Given a map (A -> B) -> C, return the corresponding map
12941 __isl_give isl_map
*isl_map_curry(__isl_take isl_map
*map
)
12943 return isl_map_change_space(map
, &isl_map_can_curry
,
12944 "map cannot be curried", &isl_space_curry
);
12947 /* Can isl_map_range_curry be applied to "map"?
12948 * That is, does it have a nested relation in its range,
12949 * the domain of which is itself a nested relation?
12951 isl_bool
isl_map_can_range_curry(__isl_keep isl_map
*map
)
12954 return isl_bool_error
;
12956 return isl_space_can_range_curry(map
->dim
);
12959 /* Given a map A -> ((B -> C) -> D), return the corresponding map
12960 * A -> (B -> (C -> D)).
12962 __isl_give isl_map
*isl_map_range_curry(__isl_take isl_map
*map
)
12964 return isl_map_change_space(map
, &isl_map_can_range_curry
,
12965 "map range cannot be curried",
12966 &isl_space_range_curry
);
12969 /* Can we apply isl_basic_map_uncurry to "bmap"?
12970 * That is, does it have a nested relation in its domain?
12972 isl_bool
isl_basic_map_can_uncurry(__isl_keep isl_basic_map
*bmap
)
12975 return isl_bool_error
;
12977 return isl_space_can_uncurry(bmap
->dim
);
12980 /* Can we apply isl_map_uncurry to "map"?
12981 * That is, does it have a nested relation in its domain?
12983 isl_bool
isl_map_can_uncurry(__isl_keep isl_map
*map
)
12986 return isl_bool_error
;
12988 return isl_space_can_uncurry(map
->dim
);
12991 /* Given a basic map A -> (B -> C), return the corresponding basic map
12994 __isl_give isl_basic_map
*isl_basic_map_uncurry(__isl_take isl_basic_map
*bmap
)
13000 if (!isl_basic_map_can_uncurry(bmap
))
13001 isl_die(bmap
->ctx
, isl_error_invalid
,
13002 "basic map cannot be uncurried",
13003 return isl_basic_map_free(bmap
));
13004 bmap
= isl_basic_map_cow(bmap
);
13007 bmap
->dim
= isl_space_uncurry(bmap
->dim
);
13009 return isl_basic_map_free(bmap
);
13010 bmap
= isl_basic_map_mark_final(bmap
);
13014 /* Given a map A -> (B -> C), return the corresponding map
13017 __isl_give isl_map
*isl_map_uncurry(__isl_take isl_map
*map
)
13019 return isl_map_change_space(map
, &isl_map_can_uncurry
,
13020 "map cannot be uncurried", &isl_space_uncurry
);
13023 __isl_give isl_set
*isl_set_equate(__isl_take isl_set
*set
,
13024 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13026 return isl_map_equate(set
, type1
, pos1
, type2
, pos2
);
13029 /* Construct a basic map where the given dimensions are equal to each other.
13031 static __isl_give isl_basic_map
*equator(__isl_take isl_space
*space
,
13032 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13034 isl_basic_map
*bmap
= NULL
;
13038 total
= isl_space_dim(space
, isl_dim_all
);
13040 isl_space_check_range(space
, type1
, pos1
, 1) < 0 ||
13041 isl_space_check_range(space
, type2
, pos2
, 1) < 0)
13044 if (type1
== type2
&& pos1
== pos2
)
13045 return isl_basic_map_universe(space
);
13047 bmap
= isl_basic_map_alloc_space(isl_space_copy(space
), 0, 1, 0);
13048 i
= isl_basic_map_alloc_equality(bmap
);
13051 isl_seq_clr(bmap
->eq
[i
], 1 + total
);
13052 pos1
+= isl_basic_map_offset(bmap
, type1
);
13053 pos2
+= isl_basic_map_offset(bmap
, type2
);
13054 isl_int_set_si(bmap
->eq
[i
][pos1
], -1);
13055 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
13056 bmap
= isl_basic_map_finalize(bmap
);
13057 isl_space_free(space
);
13060 isl_space_free(space
);
13061 isl_basic_map_free(bmap
);
13065 /* Add a constraint imposing that the given two dimensions are equal.
13067 __isl_give isl_basic_map
*isl_basic_map_equate(__isl_take isl_basic_map
*bmap
,
13068 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13072 eq
= equator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
13074 bmap
= isl_basic_map_intersect(bmap
, eq
);
13079 /* Add a constraint imposing that the given two dimensions are equal.
13081 __isl_give isl_map
*isl_map_equate(__isl_take isl_map
*map
,
13082 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13084 isl_basic_map
*bmap
;
13086 bmap
= equator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
13088 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
13093 /* Add a constraint imposing that the given two dimensions have opposite values.
13095 __isl_give isl_map
*isl_map_oppose(__isl_take isl_map
*map
,
13096 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13098 isl_basic_map
*bmap
= NULL
;
13102 if (isl_map_check_range(map
, type1
, pos1
, 1) < 0)
13103 return isl_map_free(map
);
13104 if (isl_map_check_range(map
, type2
, pos2
, 1) < 0)
13105 return isl_map_free(map
);
13107 total
= isl_map_dim(map
, isl_dim_all
);
13109 return isl_map_free(map
);
13110 bmap
= isl_basic_map_alloc_space(isl_map_get_space(map
), 0, 1, 0);
13111 i
= isl_basic_map_alloc_equality(bmap
);
13114 isl_seq_clr(bmap
->eq
[i
], 1 + total
);
13115 pos1
+= isl_basic_map_offset(bmap
, type1
);
13116 pos2
+= isl_basic_map_offset(bmap
, type2
);
13117 isl_int_set_si(bmap
->eq
[i
][pos1
], 1);
13118 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
13119 bmap
= isl_basic_map_finalize(bmap
);
13121 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
13125 isl_basic_map_free(bmap
);
13130 /* Construct a constraint imposing that the value of the first dimension is
13131 * greater than or equal to that of the second.
13133 static __isl_give isl_constraint
*constraint_order_ge(
13134 __isl_take isl_space
*space
, enum isl_dim_type type1
, int pos1
,
13135 enum isl_dim_type type2
, int pos2
)
13139 if (isl_space_check_range(space
, type1
, pos1
, 1) < 0 ||
13140 isl_space_check_range(space
, type2
, pos2
, 1) < 0)
13141 space
= isl_space_free(space
);
13145 c
= isl_constraint_alloc_inequality(isl_local_space_from_space(space
));
13147 if (type1
== type2
&& pos1
== pos2
)
13150 c
= isl_constraint_set_coefficient_si(c
, type1
, pos1
, 1);
13151 c
= isl_constraint_set_coefficient_si(c
, type2
, pos2
, -1);
13156 /* Add a constraint imposing that the value of the first dimension is
13157 * greater than or equal to that of the second.
13159 __isl_give isl_basic_map
*isl_basic_map_order_ge(__isl_take isl_basic_map
*bmap
,
13160 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13165 if (type1
== type2
&& pos1
== pos2
)
13167 space
= isl_basic_map_get_space(bmap
);
13168 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
13169 bmap
= isl_basic_map_add_constraint(bmap
, c
);
13174 /* Add a constraint imposing that the value of the first dimension is
13175 * greater than or equal to that of the second.
13177 __isl_give isl_map
*isl_map_order_ge(__isl_take isl_map
*map
,
13178 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13183 if (type1
== type2
&& pos1
== pos2
)
13185 space
= isl_map_get_space(map
);
13186 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
13187 map
= isl_map_add_constraint(map
, c
);
13192 /* Add a constraint imposing that the value of the first dimension is
13193 * less than or equal to that of the second.
13195 __isl_give isl_map
*isl_map_order_le(__isl_take isl_map
*map
,
13196 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13198 return isl_map_order_ge(map
, type2
, pos2
, type1
, pos1
);
13201 /* Construct a basic map where the value of the first dimension is
13202 * greater than that of the second.
13204 static __isl_give isl_basic_map
*greator(__isl_take isl_space
*space
,
13205 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13207 isl_basic_map
*bmap
= NULL
;
13211 if (isl_space_check_range(space
, type1
, pos1
, 1) < 0 ||
13212 isl_space_check_range(space
, type2
, pos2
, 1) < 0)
13215 if (type1
== type2
&& pos1
== pos2
)
13216 return isl_basic_map_empty(space
);
13218 bmap
= isl_basic_map_alloc_space(space
, 0, 0, 1);
13219 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
13220 i
= isl_basic_map_alloc_inequality(bmap
);
13221 if (total
< 0 || i
< 0)
13222 return isl_basic_map_free(bmap
);
13223 isl_seq_clr(bmap
->ineq
[i
], 1 + total
);
13224 pos1
+= isl_basic_map_offset(bmap
, type1
);
13225 pos2
+= isl_basic_map_offset(bmap
, type2
);
13226 isl_int_set_si(bmap
->ineq
[i
][pos1
], 1);
13227 isl_int_set_si(bmap
->ineq
[i
][pos2
], -1);
13228 isl_int_set_si(bmap
->ineq
[i
][0], -1);
13229 bmap
= isl_basic_map_finalize(bmap
);
13233 isl_space_free(space
);
13234 isl_basic_map_free(bmap
);
13238 /* Add a constraint imposing that the value of the first dimension is
13239 * greater than that of the second.
13241 __isl_give isl_basic_map
*isl_basic_map_order_gt(__isl_take isl_basic_map
*bmap
,
13242 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13246 gt
= greator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
13248 bmap
= isl_basic_map_intersect(bmap
, gt
);
13253 /* Add a constraint imposing that the value of the first dimension is
13254 * greater than that of the second.
13256 __isl_give isl_map
*isl_map_order_gt(__isl_take isl_map
*map
,
13257 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13259 isl_basic_map
*bmap
;
13261 bmap
= greator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
13263 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
13268 /* Add a constraint imposing that the value of the first dimension is
13269 * smaller than that of the second.
13271 __isl_give isl_map
*isl_map_order_lt(__isl_take isl_map
*map
,
13272 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13274 return isl_map_order_gt(map
, type2
, pos2
, type1
, pos1
);
13277 __isl_give isl_aff
*isl_basic_map_get_div(__isl_keep isl_basic_map
*bmap
,
13281 isl_local_space
*ls
;
13286 if (!isl_basic_map_divs_known(bmap
))
13287 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
13288 "some divs are unknown", return NULL
);
13290 ls
= isl_basic_map_get_local_space(bmap
);
13291 div
= isl_local_space_get_div(ls
, pos
);
13292 isl_local_space_free(ls
);
13297 __isl_give isl_aff
*isl_basic_set_get_div(__isl_keep isl_basic_set
*bset
,
13300 return isl_basic_map_get_div(bset
, pos
);
13303 /* Plug in "subs" for dimension "type", "pos" of "bset".
13305 * Let i be the dimension to replace and let "subs" be of the form
13309 * Any integer division with a non-zero coefficient for i,
13311 * floor((a i + g)/m)
13315 * floor((a f + d g)/(m d))
13317 * Constraints of the form
13325 * We currently require that "subs" is an integral expression.
13326 * Handling rational expressions may require us to add stride constraints
13327 * as we do in isl_basic_set_preimage_multi_aff.
13329 __isl_give isl_basic_set
*isl_basic_set_substitute(
13330 __isl_take isl_basic_set
*bset
,
13331 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
13338 if (bset
&& isl_basic_set_plain_is_empty(bset
))
13341 bset
= isl_basic_set_cow(bset
);
13342 if (!bset
|| !subs
)
13345 ctx
= isl_basic_set_get_ctx(bset
);
13346 if (!isl_space_is_equal(bset
->dim
, subs
->ls
->dim
))
13347 isl_die(ctx
, isl_error_invalid
,
13348 "spaces don't match", goto error
);
13349 n_div
= isl_local_space_dim(subs
->ls
, isl_dim_div
);
13353 isl_die(ctx
, isl_error_unsupported
,
13354 "cannot handle divs yet", goto error
);
13355 if (!isl_int_is_one(subs
->v
->el
[0]))
13356 isl_die(ctx
, isl_error_invalid
,
13357 "can only substitute integer expressions", goto error
);
13359 pos
+= isl_basic_set_offset(bset
, type
);
13363 for (i
= 0; i
< bset
->n_eq
; ++i
) {
13364 if (isl_int_is_zero(bset
->eq
[i
][pos
]))
13366 isl_int_set(v
, bset
->eq
[i
][pos
]);
13367 isl_int_set_si(bset
->eq
[i
][pos
], 0);
13368 isl_seq_combine(bset
->eq
[i
], subs
->v
->el
[0], bset
->eq
[i
],
13369 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
13372 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
13373 if (isl_int_is_zero(bset
->ineq
[i
][pos
]))
13375 isl_int_set(v
, bset
->ineq
[i
][pos
]);
13376 isl_int_set_si(bset
->ineq
[i
][pos
], 0);
13377 isl_seq_combine(bset
->ineq
[i
], subs
->v
->el
[0], bset
->ineq
[i
],
13378 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
13381 for (i
= 0; i
< bset
->n_div
; ++i
) {
13382 if (isl_int_is_zero(bset
->div
[i
][1 + pos
]))
13384 isl_int_set(v
, bset
->div
[i
][1 + pos
]);
13385 isl_int_set_si(bset
->div
[i
][1 + pos
], 0);
13386 isl_seq_combine(bset
->div
[i
] + 1,
13387 subs
->v
->el
[0], bset
->div
[i
] + 1,
13388 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
13389 isl_int_mul(bset
->div
[i
][0], bset
->div
[i
][0], subs
->v
->el
[0]);
13394 bset
= isl_basic_set_simplify(bset
);
13395 return isl_basic_set_finalize(bset
);
13397 isl_basic_set_free(bset
);
13401 /* Plug in "subs" for dimension "type", "pos" of "set".
13403 __isl_give isl_set
*isl_set_substitute(__isl_take isl_set
*set
,
13404 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
13408 if (set
&& isl_set_plain_is_empty(set
))
13411 set
= isl_set_cow(set
);
13415 for (i
= set
->n
- 1; i
>= 0; --i
) {
13416 set
->p
[i
] = isl_basic_set_substitute(set
->p
[i
], type
, pos
, subs
);
13417 set
= set_from_map(remove_if_empty(set_to_map(set
), i
));
13428 /* Check if the range of "ma" is compatible with the domain or range
13429 * (depending on "type") of "bmap".
13431 static isl_stat
check_basic_map_compatible_range_multi_aff(
13432 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type type
,
13433 __isl_keep isl_multi_aff
*ma
)
13436 isl_space
*ma_space
;
13438 ma_space
= isl_multi_aff_get_space(ma
);
13440 m
= isl_space_has_equal_params(bmap
->dim
, ma_space
);
13444 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
13445 "parameters don't match", goto error
);
13446 m
= isl_space_tuple_is_equal(bmap
->dim
, type
, ma_space
, isl_dim_out
);
13450 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
13451 "spaces don't match", goto error
);
13453 isl_space_free(ma_space
);
13454 return isl_stat_ok
;
13456 isl_space_free(ma_space
);
13457 return isl_stat_error
;
13460 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
13461 * coefficients before the transformed range of dimensions,
13462 * the "n_after" coefficients after the transformed range of dimensions
13463 * and the coefficients of the other divs in "bmap".
13465 static __isl_give isl_basic_map
*set_ma_divs(__isl_take isl_basic_map
*bmap
,
13466 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
, int n_div
)
13471 isl_local_space
*ls
;
13476 ls
= isl_aff_get_domain_local_space(ma
->u
.p
[0]);
13477 n_param
= isl_local_space_dim(ls
, isl_dim_param
);
13478 n_set
= isl_local_space_dim(ls
, isl_dim_set
);
13479 if (n_param
< 0 || n_set
< 0)
13480 return isl_basic_map_free(bmap
);
13482 for (i
= 0; i
< n_div
; ++i
) {
13483 int o_bmap
= 0, o_ls
= 0;
13485 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], 1 + 1 + n_param
);
13486 o_bmap
+= 1 + 1 + n_param
;
13487 o_ls
+= 1 + 1 + n_param
;
13488 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_before
);
13489 o_bmap
+= n_before
;
13490 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
13491 ls
->div
->row
[i
] + o_ls
, n_set
);
13494 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_after
);
13496 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
13497 ls
->div
->row
[i
] + o_ls
, n_div
);
13500 isl_seq_clr(bmap
->div
[i
] + o_bmap
, bmap
->n_div
- n_div
);
13501 bmap
= isl_basic_map_add_div_constraints(bmap
, i
);
13506 isl_local_space_free(ls
);
13509 isl_local_space_free(ls
);
13510 return isl_basic_map_free(bmap
);
13513 /* How many stride constraints does "ma" enforce?
13514 * That is, how many of the affine expressions have a denominator
13515 * different from one?
13517 static int multi_aff_strides(__isl_keep isl_multi_aff
*ma
)
13522 for (i
= 0; i
< ma
->n
; ++i
)
13523 if (!isl_int_is_one(ma
->u
.p
[i
]->v
->el
[0]))
13529 /* For each affine expression in ma of the form
13531 * x_i = (f_i y + h_i)/m_i
13533 * with m_i different from one, add a constraint to "bmap"
13536 * f_i y + h_i = m_i alpha_i
13538 * with alpha_i an additional existentially quantified variable.
13540 * The input variables of "ma" correspond to a subset of the variables
13541 * of "bmap". There are "n_before" variables in "bmap" before this
13542 * subset and "n_after" variables after this subset.
13543 * The integer divisions of the affine expressions in "ma" are assumed
13544 * to have been aligned. There are "n_div_ma" of them and
13545 * they appear first in "bmap", straight after the "n_after" variables.
13547 static __isl_give isl_basic_map
*add_ma_strides(
13548 __isl_take isl_basic_map
*bmap
, __isl_keep isl_multi_aff
*ma
,
13549 int n_before
, int n_after
, int n_div_ma
)
13557 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
13558 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
13559 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
13560 if (total
< 0 || n_param
< 0 || n_in
< 0)
13561 return isl_basic_map_free(bmap
);
13562 for (i
= 0; i
< ma
->n
; ++i
) {
13563 int o_bmap
= 0, o_ma
= 1;
13565 if (isl_int_is_one(ma
->u
.p
[i
]->v
->el
[0]))
13567 div
= isl_basic_map_alloc_div(bmap
);
13568 k
= isl_basic_map_alloc_equality(bmap
);
13569 if (div
< 0 || k
< 0)
13571 isl_int_set_si(bmap
->div
[div
][0], 0);
13572 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
13573 ma
->u
.p
[i
]->v
->el
+ o_ma
, 1 + n_param
);
13574 o_bmap
+= 1 + n_param
;
13575 o_ma
+= 1 + n_param
;
13576 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_before
);
13577 o_bmap
+= n_before
;
13578 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
13579 ma
->u
.p
[i
]->v
->el
+ o_ma
, n_in
);
13582 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_after
);
13584 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
13585 ma
->u
.p
[i
]->v
->el
+ o_ma
, n_div_ma
);
13586 o_bmap
+= n_div_ma
;
13588 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, 1 + total
- o_bmap
);
13589 isl_int_neg(bmap
->eq
[k
][1 + total
], ma
->u
.p
[i
]->v
->el
[0]);
13595 isl_basic_map_free(bmap
);
13599 /* Replace the domain or range space (depending on "type) of "space" by "set".
13601 static __isl_give isl_space
*isl_space_set(__isl_take isl_space
*space
,
13602 enum isl_dim_type type
, __isl_take isl_space
*set
)
13604 if (type
== isl_dim_in
) {
13605 space
= isl_space_range(space
);
13606 space
= isl_space_map_from_domain_and_range(set
, space
);
13608 space
= isl_space_domain(space
);
13609 space
= isl_space_map_from_domain_and_range(space
, set
);
13615 /* Compute the preimage of the domain or range (depending on "type")
13616 * of "bmap" under the function represented by "ma".
13617 * In other words, plug in "ma" in the domain or range of "bmap".
13618 * The result is a basic map that lives in the same space as "bmap"
13619 * except that the domain or range has been replaced by
13620 * the domain space of "ma".
13622 * If bmap is represented by
13624 * A(p) + S u + B x + T v + C(divs) >= 0,
13626 * where u and x are input and output dimensions if type == isl_dim_out
13627 * while x and v are input and output dimensions if type == isl_dim_in,
13628 * and ma is represented by
13630 * x = D(p) + F(y) + G(divs')
13632 * then the result is
13634 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
13636 * The divs in the input set are similarly adjusted.
13639 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
13643 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
13644 * B_i G(divs') + c_i(divs))/n_i)
13646 * If bmap is not a rational map and if F(y) involves any denominators
13648 * x_i = (f_i y + h_i)/m_i
13650 * then additional constraints are added to ensure that we only
13651 * map back integer points. That is we enforce
13653 * f_i y + h_i = m_i alpha_i
13655 * with alpha_i an additional existentially quantified variable.
13657 * We first copy over the divs from "ma".
13658 * Then we add the modified constraints and divs from "bmap".
13659 * Finally, we add the stride constraints, if needed.
13661 __isl_give isl_basic_map
*isl_basic_map_preimage_multi_aff(
13662 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
13663 __isl_take isl_multi_aff
*ma
)
13667 isl_basic_map
*res
= NULL
;
13668 isl_size n_before
, n_after
, n_div_bmap
, n_div_ma
;
13669 isl_int f
, c1
, c2
, g
;
13678 ma
= isl_multi_aff_align_divs(ma
);
13681 if (check_basic_map_compatible_range_multi_aff(bmap
, type
, ma
) < 0)
13684 if (type
== isl_dim_in
) {
13686 n_after
= isl_basic_map_dim(bmap
, isl_dim_out
);
13688 n_before
= isl_basic_map_dim(bmap
, isl_dim_in
);
13691 n_div_bmap
= isl_basic_map_dim(bmap
, isl_dim_div
);
13692 n_div_ma
= ma
->n
? isl_aff_dim(ma
->u
.p
[0], isl_dim_div
) : 0;
13693 if (n_before
< 0 || n_after
< 0 || n_div_bmap
< 0 || n_div_ma
< 0)
13696 space
= isl_multi_aff_get_domain_space(ma
);
13697 space
= isl_space_set(isl_basic_map_get_space(bmap
), type
, space
);
13698 rational
= isl_basic_map_is_rational(bmap
);
13699 strides
= rational
? 0 : multi_aff_strides(ma
);
13700 res
= isl_basic_map_alloc_space(space
, n_div_ma
+ n_div_bmap
+ strides
,
13701 bmap
->n_eq
+ strides
, bmap
->n_ineq
+ 2 * n_div_ma
);
13703 res
= isl_basic_map_set_rational(res
);
13705 for (i
= 0; i
< n_div_ma
+ n_div_bmap
; ++i
)
13706 if (isl_basic_map_alloc_div(res
) < 0)
13709 res
= set_ma_divs(res
, ma
, n_before
, n_after
, n_div_ma
);
13713 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
13714 k
= isl_basic_map_alloc_equality(res
);
13717 if (isl_seq_preimage(res
->eq
[k
], bmap
->eq
[i
], ma
, n_before
,
13718 n_after
, n_div_ma
, n_div_bmap
,
13719 f
, c1
, c2
, g
, 0) < 0)
13723 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
13724 k
= isl_basic_map_alloc_inequality(res
);
13727 if (isl_seq_preimage(res
->ineq
[k
], bmap
->ineq
[i
], ma
, n_before
,
13728 n_after
, n_div_ma
, n_div_bmap
,
13729 f
, c1
, c2
, g
, 0) < 0)
13733 for (i
= 0; i
< bmap
->n_div
; ++i
) {
13734 if (isl_int_is_zero(bmap
->div
[i
][0])) {
13735 isl_int_set_si(res
->div
[n_div_ma
+ i
][0], 0);
13738 if (isl_seq_preimage(res
->div
[n_div_ma
+ i
], bmap
->div
[i
], ma
,
13739 n_before
, n_after
, n_div_ma
, n_div_bmap
,
13740 f
, c1
, c2
, g
, 1) < 0)
13745 res
= add_ma_strides(res
, ma
, n_before
, n_after
, n_div_ma
);
13751 isl_basic_map_free(bmap
);
13752 isl_multi_aff_free(ma
);
13753 res
= isl_basic_map_simplify(res
);
13754 return isl_basic_map_finalize(res
);
13760 isl_basic_map_free(bmap
);
13761 isl_multi_aff_free(ma
);
13762 isl_basic_map_free(res
);
13766 /* Compute the preimage of "bset" under the function represented by "ma".
13767 * In other words, plug in "ma" in "bset". The result is a basic set
13768 * that lives in the domain space of "ma".
13770 __isl_give isl_basic_set
*isl_basic_set_preimage_multi_aff(
13771 __isl_take isl_basic_set
*bset
, __isl_take isl_multi_aff
*ma
)
13773 return isl_basic_map_preimage_multi_aff(bset
, isl_dim_set
, ma
);
13776 /* Compute the preimage of the domain of "bmap" under the function
13777 * represented by "ma".
13778 * In other words, plug in "ma" in the domain of "bmap".
13779 * The result is a basic map that lives in the same space as "bmap"
13780 * except that the domain has been replaced by the domain space of "ma".
13782 __isl_give isl_basic_map
*isl_basic_map_preimage_domain_multi_aff(
13783 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
13785 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_in
, ma
);
13788 /* Compute the preimage of the range of "bmap" under the function
13789 * represented by "ma".
13790 * In other words, plug in "ma" in the range of "bmap".
13791 * The result is a basic map that lives in the same space as "bmap"
13792 * except that the range has been replaced by the domain space of "ma".
13794 __isl_give isl_basic_map
*isl_basic_map_preimage_range_multi_aff(
13795 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
13797 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_out
, ma
);
13800 /* Check if the range of "ma" is compatible with the domain or range
13801 * (depending on "type") of "map".
13802 * Return isl_stat_error if anything is wrong.
13804 static isl_stat
check_map_compatible_range_multi_aff(
13805 __isl_keep isl_map
*map
, enum isl_dim_type type
,
13806 __isl_keep isl_multi_aff
*ma
)
13809 isl_space
*ma_space
;
13811 ma_space
= isl_multi_aff_get_space(ma
);
13812 m
= isl_map_space_tuple_is_equal(map
, type
, ma_space
, isl_dim_out
);
13813 isl_space_free(ma_space
);
13815 return isl_stat_error
;
13817 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
13818 "spaces don't match", return isl_stat_error
);
13819 return isl_stat_ok
;
13822 /* Compute the preimage of the domain or range (depending on "type")
13823 * of "map" under the function represented by "ma".
13824 * In other words, plug in "ma" in the domain or range of "map".
13825 * The result is a map that lives in the same space as "map"
13826 * except that the domain or range has been replaced by
13827 * the domain space of "ma".
13829 * The parameters are assumed to have been aligned.
13831 static __isl_give isl_map
*map_preimage_multi_aff(__isl_take isl_map
*map
,
13832 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
13837 map
= isl_map_cow(map
);
13838 ma
= isl_multi_aff_align_divs(ma
);
13841 if (check_map_compatible_range_multi_aff(map
, type
, ma
) < 0)
13844 for (i
= 0; i
< map
->n
; ++i
) {
13845 map
->p
[i
] = isl_basic_map_preimage_multi_aff(map
->p
[i
], type
,
13846 isl_multi_aff_copy(ma
));
13851 space
= isl_multi_aff_get_domain_space(ma
);
13852 space
= isl_space_set(isl_map_get_space(map
), type
, space
);
13854 isl_space_free(isl_map_take_space(map
));
13855 map
= isl_map_restore_space(map
, space
);
13859 isl_multi_aff_free(ma
);
13861 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
13862 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
13865 isl_multi_aff_free(ma
);
13870 /* Compute the preimage of the domain or range (depending on "type")
13871 * of "map" under the function represented by "ma".
13872 * In other words, plug in "ma" in the domain or range of "map".
13873 * The result is a map that lives in the same space as "map"
13874 * except that the domain or range has been replaced by
13875 * the domain space of "ma".
13877 __isl_give isl_map
*isl_map_preimage_multi_aff(__isl_take isl_map
*map
,
13878 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
13885 aligned
= isl_map_space_has_equal_params(map
, ma
->space
);
13889 return map_preimage_multi_aff(map
, type
, ma
);
13891 if (isl_map_check_named_params(map
) < 0)
13893 if (!isl_space_has_named_params(ma
->space
))
13894 isl_die(map
->ctx
, isl_error_invalid
,
13895 "unaligned unnamed parameters", goto error
);
13896 map
= isl_map_align_params(map
, isl_multi_aff_get_space(ma
));
13897 ma
= isl_multi_aff_align_params(ma
, isl_map_get_space(map
));
13899 return map_preimage_multi_aff(map
, type
, ma
);
13901 isl_multi_aff_free(ma
);
13902 return isl_map_free(map
);
13905 /* Compute the preimage of "set" under the function represented by "ma".
13906 * In other words, plug in "ma" in "set". The result is a set
13907 * that lives in the domain space of "ma".
13909 __isl_give isl_set
*isl_set_preimage_multi_aff(__isl_take isl_set
*set
,
13910 __isl_take isl_multi_aff
*ma
)
13912 return isl_map_preimage_multi_aff(set
, isl_dim_set
, ma
);
13915 /* Compute the preimage of the domain of "map" under the function
13916 * represented by "ma".
13917 * In other words, plug in "ma" in the domain of "map".
13918 * The result is a map that lives in the same space as "map"
13919 * except that the domain has been replaced by the domain space of "ma".
13921 __isl_give isl_map
*isl_map_preimage_domain_multi_aff(__isl_take isl_map
*map
,
13922 __isl_take isl_multi_aff
*ma
)
13924 return isl_map_preimage_multi_aff(map
, isl_dim_in
, ma
);
13927 /* Compute the preimage of the range of "map" under the function
13928 * represented by "ma".
13929 * In other words, plug in "ma" in the range of "map".
13930 * The result is a map that lives in the same space as "map"
13931 * except that the range has been replaced by the domain space of "ma".
13933 __isl_give isl_map
*isl_map_preimage_range_multi_aff(__isl_take isl_map
*map
,
13934 __isl_take isl_multi_aff
*ma
)
13936 return isl_map_preimage_multi_aff(map
, isl_dim_out
, ma
);
13939 /* Compute the preimage of "map" under the function represented by "pma".
13940 * In other words, plug in "pma" in the domain or range of "map".
13941 * The result is a map that lives in the same space as "map",
13942 * except that the space of type "type" has been replaced by
13943 * the domain space of "pma".
13945 * The parameters of "map" and "pma" are assumed to have been aligned.
13947 static __isl_give isl_map
*isl_map_preimage_pw_multi_aff_aligned(
13948 __isl_take isl_map
*map
, enum isl_dim_type type
,
13949 __isl_take isl_pw_multi_aff
*pma
)
13958 isl_pw_multi_aff_free(pma
);
13959 res
= isl_map_empty(isl_map_get_space(map
));
13964 res
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
13965 isl_multi_aff_copy(pma
->p
[0].maff
));
13966 if (type
== isl_dim_in
)
13967 res
= isl_map_intersect_domain(res
,
13968 isl_map_copy(pma
->p
[0].set
));
13970 res
= isl_map_intersect_range(res
,
13971 isl_map_copy(pma
->p
[0].set
));
13973 for (i
= 1; i
< pma
->n
; ++i
) {
13976 res_i
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
13977 isl_multi_aff_copy(pma
->p
[i
].maff
));
13978 if (type
== isl_dim_in
)
13979 res_i
= isl_map_intersect_domain(res_i
,
13980 isl_map_copy(pma
->p
[i
].set
));
13982 res_i
= isl_map_intersect_range(res_i
,
13983 isl_map_copy(pma
->p
[i
].set
));
13984 res
= isl_map_union(res
, res_i
);
13987 isl_pw_multi_aff_free(pma
);
13991 isl_pw_multi_aff_free(pma
);
13996 /* Compute the preimage of "map" under the function represented by "pma".
13997 * In other words, plug in "pma" in the domain or range of "map".
13998 * The result is a map that lives in the same space as "map",
13999 * except that the space of type "type" has been replaced by
14000 * the domain space of "pma".
14002 __isl_give isl_map
*isl_map_preimage_pw_multi_aff(__isl_take isl_map
*map
,
14003 enum isl_dim_type type
, __isl_take isl_pw_multi_aff
*pma
)
14010 aligned
= isl_map_space_has_equal_params(map
, pma
->dim
);
14014 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
14016 if (isl_map_check_named_params(map
) < 0)
14018 if (isl_pw_multi_aff_check_named_params(pma
) < 0)
14020 map
= isl_map_align_params(map
, isl_pw_multi_aff_get_space(pma
));
14021 pma
= isl_pw_multi_aff_align_params(pma
, isl_map_get_space(map
));
14023 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
14025 isl_pw_multi_aff_free(pma
);
14026 return isl_map_free(map
);
14029 /* Compute the preimage of "set" under the function represented by "pma".
14030 * In other words, plug in "pma" in "set". The result is a set
14031 * that lives in the domain space of "pma".
14033 __isl_give isl_set
*isl_set_preimage_pw_multi_aff(__isl_take isl_set
*set
,
14034 __isl_take isl_pw_multi_aff
*pma
)
14036 return isl_map_preimage_pw_multi_aff(set
, isl_dim_set
, pma
);
14039 /* Compute the preimage of the domain of "map" under the function
14040 * represented by "pma".
14041 * In other words, plug in "pma" in the domain of "map".
14042 * The result is a map that lives in the same space as "map",
14043 * except that domain space has been replaced by the domain space of "pma".
14045 __isl_give isl_map
*isl_map_preimage_domain_pw_multi_aff(
14046 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
14048 return isl_map_preimage_pw_multi_aff(map
, isl_dim_in
, pma
);
14051 /* Compute the preimage of the range of "map" under the function
14052 * represented by "pma".
14053 * In other words, plug in "pma" in the range of "map".
14054 * The result is a map that lives in the same space as "map",
14055 * except that range space has been replaced by the domain space of "pma".
14057 __isl_give isl_map
*isl_map_preimage_range_pw_multi_aff(
14058 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
14060 return isl_map_preimage_pw_multi_aff(map
, isl_dim_out
, pma
);
14063 /* Compute the preimage of "map" under the function represented by "mpa".
14064 * In other words, plug in "mpa" in the domain or range of "map".
14065 * The result is a map that lives in the same space as "map",
14066 * except that the space of type "type" has been replaced by
14067 * the domain space of "mpa".
14069 * If the map does not involve any constraints that refer to the
14070 * dimensions of the substituted space, then the only possible
14071 * effect of "mpa" on the map is to map the space to a different space.
14072 * We create a separate isl_multi_aff to effectuate this change
14073 * in order to avoid spurious splitting of the map along the pieces
14075 * If "mpa" has a non-trivial explicit domain, however,
14076 * then the full substitution should be performed.
14078 __isl_give isl_map
*isl_map_preimage_multi_pw_aff(__isl_take isl_map
*map
,
14079 enum isl_dim_type type
, __isl_take isl_multi_pw_aff
*mpa
)
14083 isl_pw_multi_aff
*pma
;
14085 n
= isl_map_dim(map
, type
);
14089 full
= isl_map_involves_dims(map
, type
, 0, n
);
14090 if (full
>= 0 && !full
)
14091 full
= isl_multi_pw_aff_has_non_trivial_domain(mpa
);
14098 space
= isl_multi_pw_aff_get_space(mpa
);
14099 isl_multi_pw_aff_free(mpa
);
14100 ma
= isl_multi_aff_zero(space
);
14101 return isl_map_preimage_multi_aff(map
, type
, ma
);
14104 pma
= isl_pw_multi_aff_from_multi_pw_aff(mpa
);
14105 return isl_map_preimage_pw_multi_aff(map
, type
, pma
);
14108 isl_multi_pw_aff_free(mpa
);
14112 /* Compute the preimage of "map" under the function represented by "mpa".
14113 * In other words, plug in "mpa" in the domain "map".
14114 * The result is a map that lives in the same space as "map",
14115 * except that domain space has been replaced by the domain space of "mpa".
14117 __isl_give isl_map
*isl_map_preimage_domain_multi_pw_aff(
14118 __isl_take isl_map
*map
, __isl_take isl_multi_pw_aff
*mpa
)
14120 return isl_map_preimage_multi_pw_aff(map
, isl_dim_in
, mpa
);
14123 /* Compute the preimage of "set" by the function represented by "mpa".
14124 * In other words, plug in "mpa" in "set".
14126 __isl_give isl_set
*isl_set_preimage_multi_pw_aff(__isl_take isl_set
*set
,
14127 __isl_take isl_multi_pw_aff
*mpa
)
14129 return isl_map_preimage_multi_pw_aff(set
, isl_dim_set
, mpa
);
14132 /* Return a copy of the equality constraints of "bset" as a matrix.
14134 __isl_give isl_mat
*isl_basic_set_extract_equalities(
14135 __isl_keep isl_basic_set
*bset
)
14140 total
= isl_basic_set_dim(bset
, isl_dim_all
);
14144 ctx
= isl_basic_set_get_ctx(bset
);
14145 return isl_mat_sub_alloc6(ctx
, bset
->eq
, 0, bset
->n_eq
, 0, 1 + total
);
14148 /* Are the "n" "coefficients" starting at "first" of the integer division
14149 * expressions at position "pos1" in "bmap1" and "pos2" in "bmap2" equal
14151 * The "coefficient" at position 0 is the denominator.
14152 * The "coefficient" at position 1 is the constant term.
14154 isl_bool
isl_basic_map_equal_div_expr_part(__isl_keep isl_basic_map
*bmap1
,
14155 int pos1
, __isl_keep isl_basic_map
*bmap2
, int pos2
,
14156 unsigned first
, unsigned n
)
14158 if (isl_basic_map_check_range(bmap1
, isl_dim_div
, pos1
, 1) < 0)
14159 return isl_bool_error
;
14160 if (isl_basic_map_check_range(bmap2
, isl_dim_div
, pos2
, 1) < 0)
14161 return isl_bool_error
;
14162 return isl_seq_eq(bmap1
->div
[pos1
] + first
,
14163 bmap2
->div
[pos2
] + first
, n
);
14166 /* Are the integer division expressions at position "pos1" in "bmap1" and
14167 * "pos2" in "bmap2" equal to each other, except that the constant terms
14170 isl_bool
isl_basic_map_equal_div_expr_except_constant(
14171 __isl_keep isl_basic_map
*bmap1
, int pos1
,
14172 __isl_keep isl_basic_map
*bmap2
, int pos2
)
14175 isl_size total
, total2
;
14177 total
= isl_basic_map_dim(bmap1
, isl_dim_all
);
14178 total2
= isl_basic_map_dim(bmap2
, isl_dim_all
);
14179 if (total
< 0 || total2
< 0)
14180 return isl_bool_error
;
14181 if (total
!= total2
)
14182 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
14183 "incomparable div expressions", return isl_bool_error
);
14184 equal
= isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
14186 if (equal
< 0 || !equal
)
14188 equal
= isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
14190 if (equal
< 0 || equal
)
14191 return isl_bool_not(equal
);
14192 return isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
14196 /* Replace the numerator of the constant term of the integer division
14197 * expression at position "div" in "bmap" by "value".
14198 * The caller guarantees that this does not change the meaning
14201 __isl_give isl_basic_map
*isl_basic_map_set_div_expr_constant_num_si_inplace(
14202 __isl_take isl_basic_map
*bmap
, int div
, int value
)
14204 if (isl_basic_map_check_range(bmap
, isl_dim_div
, div
, 1) < 0)
14205 return isl_basic_map_free(bmap
);
14207 isl_int_set_si(bmap
->div
[div
][1], value
);
14212 /* Is the point "inner" internal to inequality constraint "ineq"
14214 * The point is considered to be internal to the inequality constraint,
14215 * if it strictly lies on the positive side of the inequality constraint,
14216 * or if it lies on the constraint and the constraint is lexico-positive.
14218 static isl_bool
is_internal(__isl_keep isl_vec
*inner
,
14219 __isl_keep isl_basic_set
*bset
, int ineq
)
14225 if (!inner
|| !bset
)
14226 return isl_bool_error
;
14228 ctx
= isl_basic_set_get_ctx(bset
);
14229 isl_seq_inner_product(inner
->el
, bset
->ineq
[ineq
], inner
->size
,
14230 &ctx
->normalize_gcd
);
14231 if (!isl_int_is_zero(ctx
->normalize_gcd
))
14232 return isl_int_is_nonneg(ctx
->normalize_gcd
);
14234 total
= isl_basic_set_dim(bset
, isl_dim_all
);
14236 return isl_bool_error
;
14237 pos
= isl_seq_first_non_zero(bset
->ineq
[ineq
] + 1, total
);
14238 return isl_int_is_pos(bset
->ineq
[ineq
][1 + pos
]);
14241 /* Tighten the inequality constraints of "bset" that are outward with respect
14242 * to the point "vec".
14243 * That is, tighten the constraints that are not satisfied by "vec".
14245 * "vec" is a point internal to some superset S of "bset" that is used
14246 * to make the subsets of S disjoint, by tightening one half of the constraints
14247 * that separate two subsets. In particular, the constraints of S
14248 * are all satisfied by "vec" and should not be tightened.
14249 * Of the internal constraints, those that have "vec" on the outside
14250 * are tightened. The shared facet is included in the adjacent subset
14251 * with the opposite constraint.
14252 * For constraints that saturate "vec", this criterion cannot be used
14253 * to determine which of the two sides should be tightened.
14254 * Instead, the sign of the first non-zero coefficient is used
14255 * to make this choice. Note that this second criterion is never used
14256 * on the constraints of S since "vec" is interior to "S".
14258 __isl_give isl_basic_set
*isl_basic_set_tighten_outward(
14259 __isl_take isl_basic_set
*bset
, __isl_keep isl_vec
*vec
)
14263 bset
= isl_basic_set_cow(bset
);
14266 for (j
= 0; j
< bset
->n_ineq
; ++j
) {
14269 internal
= is_internal(vec
, bset
, j
);
14271 return isl_basic_set_free(bset
);
14274 isl_int_sub_ui(bset
->ineq
[j
][0], bset
->ineq
[j
][0], 1);
14280 /* Replace the variables x of type "type" starting at "first" in "bmap"
14281 * by x' with x = M x' with M the matrix trans.
14282 * That is, replace the corresponding coefficients c by c M.
14284 * The transformation matrix should be a square matrix.
14286 __isl_give isl_basic_map
*isl_basic_map_transform_dims(
14287 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
, unsigned first
,
14288 __isl_take isl_mat
*trans
)
14292 bmap
= isl_basic_map_cow(bmap
);
14293 if (!bmap
|| !trans
)
14296 if (trans
->n_row
!= trans
->n_col
)
14297 isl_die(trans
->ctx
, isl_error_invalid
,
14298 "expecting square transformation matrix", goto error
);
14299 if (isl_basic_map_check_range(bmap
, type
, first
, trans
->n_row
) < 0)
14302 pos
= isl_basic_map_offset(bmap
, type
) + first
;
14304 if (isl_mat_sub_transform(bmap
->eq
, bmap
->n_eq
, pos
,
14305 isl_mat_copy(trans
)) < 0)
14307 if (isl_mat_sub_transform(bmap
->ineq
, bmap
->n_ineq
, pos
,
14308 isl_mat_copy(trans
)) < 0)
14310 if (isl_mat_sub_transform(bmap
->div
, bmap
->n_div
, 1 + pos
,
14311 isl_mat_copy(trans
)) < 0)
14314 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
14315 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
14317 isl_mat_free(trans
);
14320 isl_mat_free(trans
);
14321 isl_basic_map_free(bmap
);
14325 /* Replace the variables x of type "type" starting at "first" in "bset"
14326 * by x' with x = M x' with M the matrix trans.
14327 * That is, replace the corresponding coefficients c by c M.
14329 * The transformation matrix should be a square matrix.
14331 __isl_give isl_basic_set
*isl_basic_set_transform_dims(
14332 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned first
,
14333 __isl_take isl_mat
*trans
)
14335 return isl_basic_map_transform_dims(bset
, type
, first
, trans
);