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 /* Replace the identifier of the domain tuple of "map" by "id".
747 __isl_give isl_map
*isl_map_set_domain_tuple_id(__isl_take isl_map
*map
,
748 __isl_take isl_id
*id
)
750 return isl_map_set_tuple_id(map
, isl_dim_in
, id
);
753 /* Replace the identifier of the range tuple of "map" by "id".
755 __isl_give isl_map
*isl_map_set_range_tuple_id(__isl_take isl_map
*map
,
756 __isl_take isl_id
*id
)
758 return isl_map_set_tuple_id(map
, isl_dim_out
, id
);
761 __isl_give isl_set
*isl_set_set_tuple_id(__isl_take isl_set
*set
,
762 __isl_take isl_id
*id
)
764 return isl_map_set_tuple_id(set
, isl_dim_set
, id
);
767 __isl_give isl_map
*isl_map_reset_tuple_id(__isl_take isl_map
*map
,
768 enum isl_dim_type type
)
772 space
= isl_map_take_space(map
);
773 space
= isl_space_reset_tuple_id(space
, type
);
774 map
= isl_map_restore_space(map
, space
);
776 return isl_map_reset_space(map
, isl_map_get_space(map
));
779 __isl_give isl_set
*isl_set_reset_tuple_id(__isl_take isl_set
*set
)
781 return isl_map_reset_tuple_id(set
, isl_dim_set
);
784 isl_bool
isl_map_has_tuple_id(__isl_keep isl_map
*map
, enum isl_dim_type type
)
786 return map
? isl_space_has_tuple_id(map
->dim
, type
) : isl_bool_error
;
789 /* Does the domain tuple of "map" have an identifier?
791 isl_bool
isl_map_has_domain_tuple_id(__isl_keep isl_map
*map
)
793 return isl_map_has_tuple_id(map
, isl_dim_in
);
796 /* Does the range tuple of "map" have an identifier?
798 isl_bool
isl_map_has_range_tuple_id(__isl_keep isl_map
*map
)
800 return isl_map_has_tuple_id(map
, isl_dim_out
);
803 __isl_give isl_id
*isl_map_get_tuple_id(__isl_keep isl_map
*map
,
804 enum isl_dim_type type
)
806 return map
? isl_space_get_tuple_id(map
->dim
, type
) : NULL
;
809 /* Return the identifier of the domain tuple of "map", assuming it has one.
811 __isl_give isl_id
*isl_map_get_domain_tuple_id(__isl_keep isl_map
*map
)
813 return isl_map_get_tuple_id(map
, isl_dim_in
);
816 /* Return the identifier of the range tuple of "map", assuming it has one.
818 __isl_give isl_id
*isl_map_get_range_tuple_id(__isl_keep isl_map
*map
)
820 return isl_map_get_tuple_id(map
, isl_dim_out
);
823 isl_bool
isl_set_has_tuple_id(__isl_keep isl_set
*set
)
825 return isl_map_has_tuple_id(set
, isl_dim_set
);
828 __isl_give isl_id
*isl_set_get_tuple_id(__isl_keep isl_set
*set
)
830 return isl_map_get_tuple_id(set
, isl_dim_set
);
833 /* Does the set tuple have a name?
835 isl_bool
isl_set_has_tuple_name(__isl_keep isl_set
*set
)
838 return isl_bool_error
;
839 return isl_space_has_tuple_name(set
->dim
, isl_dim_set
);
843 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set
*bset
)
845 return bset
? isl_space_get_tuple_name(bset
->dim
, isl_dim_set
) : NULL
;
848 const char *isl_set_get_tuple_name(__isl_keep isl_set
*set
)
850 return set
? isl_space_get_tuple_name(set
->dim
, isl_dim_set
) : NULL
;
853 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map
*bmap
,
854 enum isl_dim_type type
, unsigned pos
)
856 return bmap
? isl_space_get_dim_name(bmap
->dim
, type
, pos
) : NULL
;
859 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set
*bset
,
860 enum isl_dim_type type
, unsigned pos
)
862 return bset
? isl_space_get_dim_name(bset
->dim
, type
, pos
) : NULL
;
865 /* Does the given dimension have a name?
867 isl_bool
isl_map_has_dim_name(__isl_keep isl_map
*map
,
868 enum isl_dim_type type
, unsigned pos
)
871 return isl_bool_error
;
872 return isl_space_has_dim_name(map
->dim
, type
, pos
);
875 const char *isl_map_get_dim_name(__isl_keep isl_map
*map
,
876 enum isl_dim_type type
, unsigned pos
)
878 return map
? isl_space_get_dim_name(map
->dim
, type
, pos
) : NULL
;
881 const char *isl_set_get_dim_name(__isl_keep isl_set
*set
,
882 enum isl_dim_type type
, unsigned pos
)
884 return set
? isl_space_get_dim_name(set
->dim
, type
, pos
) : NULL
;
887 /* Does the given dimension have a name?
889 isl_bool
isl_set_has_dim_name(__isl_keep isl_set
*set
,
890 enum isl_dim_type type
, unsigned pos
)
893 return isl_bool_error
;
894 return isl_space_has_dim_name(set
->dim
, type
, pos
);
897 __isl_give isl_basic_map
*isl_basic_map_set_dim_name(
898 __isl_take isl_basic_map
*bmap
,
899 enum isl_dim_type type
, unsigned pos
, const char *s
)
903 space
= isl_basic_map_take_space(bmap
);
904 space
= isl_space_set_dim_name(space
, type
, pos
, s
);
905 bmap
= isl_basic_map_restore_space(bmap
, space
);
906 return isl_basic_map_finalize(bmap
);
909 __isl_give isl_map
*isl_map_set_dim_name(__isl_take isl_map
*map
,
910 enum isl_dim_type type
, unsigned pos
, const char *s
)
915 map
= isl_map_cow(map
);
919 for (i
= 0; i
< map
->n
; ++i
) {
920 map
->p
[i
] = isl_basic_map_set_dim_name(map
->p
[i
], type
, pos
, s
);
925 space
= isl_map_take_space(map
);
926 space
= isl_space_set_dim_name(space
, type
, pos
, s
);
927 map
= isl_map_restore_space(map
, space
);
935 __isl_give isl_basic_set
*isl_basic_set_set_dim_name(
936 __isl_take isl_basic_set
*bset
,
937 enum isl_dim_type type
, unsigned pos
, const char *s
)
939 return bset_from_bmap(isl_basic_map_set_dim_name(bset_to_bmap(bset
),
943 __isl_give isl_set
*isl_set_set_dim_name(__isl_take isl_set
*set
,
944 enum isl_dim_type type
, unsigned pos
, const char *s
)
946 return set_from_map(isl_map_set_dim_name(set_to_map(set
),
950 isl_bool
isl_basic_map_has_dim_id(__isl_keep isl_basic_map
*bmap
,
951 enum isl_dim_type type
, unsigned pos
)
954 return isl_bool_error
;
955 return isl_space_has_dim_id(bmap
->dim
, type
, pos
);
958 __isl_give isl_id
*isl_basic_set_get_dim_id(__isl_keep isl_basic_set
*bset
,
959 enum isl_dim_type type
, unsigned pos
)
961 return bset
? isl_space_get_dim_id(bset
->dim
, type
, pos
) : NULL
;
964 isl_bool
isl_map_has_dim_id(__isl_keep isl_map
*map
,
965 enum isl_dim_type type
, unsigned pos
)
967 return map
? isl_space_has_dim_id(map
->dim
, type
, pos
) : isl_bool_error
;
970 __isl_give isl_id
*isl_map_get_dim_id(__isl_keep isl_map
*map
,
971 enum isl_dim_type type
, unsigned pos
)
973 return map
? isl_space_get_dim_id(map
->dim
, type
, pos
) : NULL
;
976 isl_bool
isl_set_has_dim_id(__isl_keep isl_set
*set
,
977 enum isl_dim_type type
, unsigned pos
)
979 return isl_map_has_dim_id(set
, type
, pos
);
982 __isl_give isl_id
*isl_set_get_dim_id(__isl_keep isl_set
*set
,
983 enum isl_dim_type type
, unsigned pos
)
985 return isl_map_get_dim_id(set
, type
, pos
);
988 __isl_give isl_map
*isl_map_set_dim_id(__isl_take isl_map
*map
,
989 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
993 space
= isl_map_take_space(map
);
994 space
= isl_space_set_dim_id(space
, type
, pos
, id
);
995 map
= isl_map_restore_space(map
, space
);
997 return isl_map_reset_space(map
, isl_map_get_space(map
));
1000 __isl_give isl_set
*isl_set_set_dim_id(__isl_take isl_set
*set
,
1001 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
1003 return isl_map_set_dim_id(set
, type
, pos
, id
);
1006 int isl_map_find_dim_by_id(__isl_keep isl_map
*map
, enum isl_dim_type type
,
1007 __isl_keep isl_id
*id
)
1011 return isl_space_find_dim_by_id(map
->dim
, type
, id
);
1014 int isl_set_find_dim_by_id(__isl_keep isl_set
*set
, enum isl_dim_type type
,
1015 __isl_keep isl_id
*id
)
1017 return isl_map_find_dim_by_id(set
, type
, id
);
1020 /* Return the position of the dimension of the given type and name
1022 * Return -1 if no such dimension can be found.
1024 int isl_basic_map_find_dim_by_name(__isl_keep isl_basic_map
*bmap
,
1025 enum isl_dim_type type
, const char *name
)
1029 return isl_space_find_dim_by_name(bmap
->dim
, type
, name
);
1032 int isl_map_find_dim_by_name(__isl_keep isl_map
*map
, enum isl_dim_type type
,
1037 return isl_space_find_dim_by_name(map
->dim
, type
, name
);
1040 int isl_set_find_dim_by_name(__isl_keep isl_set
*set
, enum isl_dim_type type
,
1043 return isl_map_find_dim_by_name(set
, type
, name
);
1046 /* Check whether equality i of bset is a pure stride constraint
1047 * on a single dimension, i.e., of the form
1051 * with k a constant and e an existentially quantified variable.
1053 isl_bool
isl_basic_set_eq_is_stride(__isl_keep isl_basic_set
*bset
, int i
)
1061 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
1062 d
= isl_basic_set_dim(bset
, isl_dim_set
);
1063 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
1064 if (nparam
< 0 || d
< 0 || n_div
< 0)
1065 return isl_bool_error
;
1067 if (!isl_int_is_zero(bset
->eq
[i
][0]))
1068 return isl_bool_false
;
1070 if (isl_seq_first_non_zero(bset
->eq
[i
] + 1, nparam
) != -1)
1071 return isl_bool_false
;
1072 pos1
= isl_seq_first_non_zero(bset
->eq
[i
] + 1 + nparam
, d
);
1074 return isl_bool_false
;
1075 if (isl_seq_first_non_zero(bset
->eq
[i
] + 1 + nparam
+ pos1
+ 1,
1076 d
- pos1
- 1) != -1)
1077 return isl_bool_false
;
1079 pos2
= isl_seq_first_non_zero(bset
->eq
[i
] + 1 + nparam
+ d
, n_div
);
1081 return isl_bool_false
;
1082 if (isl_seq_first_non_zero(bset
->eq
[i
] + 1 + nparam
+ d
+ pos2
+ 1,
1083 n_div
- pos2
- 1) != -1)
1084 return isl_bool_false
;
1085 if (!isl_int_is_one(bset
->eq
[i
][1 + nparam
+ pos1
]) &&
1086 !isl_int_is_negone(bset
->eq
[i
][1 + nparam
+ pos1
]))
1087 return isl_bool_false
;
1089 return isl_bool_true
;
1092 /* Reset the user pointer on all identifiers of parameters and tuples
1093 * of the space of "map".
1095 __isl_give isl_map
*isl_map_reset_user(__isl_take isl_map
*map
)
1099 space
= isl_map_get_space(map
);
1100 space
= isl_space_reset_user(space
);
1101 map
= isl_map_reset_space(map
, space
);
1106 /* Reset the user pointer on all identifiers of parameters and tuples
1107 * of the space of "set".
1109 __isl_give isl_set
*isl_set_reset_user(__isl_take isl_set
*set
)
1111 return isl_map_reset_user(set
);
1114 isl_bool
isl_basic_map_is_rational(__isl_keep isl_basic_map
*bmap
)
1117 return isl_bool_error
;
1118 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
1121 /* Has "map" been marked as a rational map?
1122 * In particular, have all basic maps in "map" been marked this way?
1123 * An empty map is not considered to be rational.
1124 * Maps where only some of the basic maps are marked rational
1127 isl_bool
isl_map_is_rational(__isl_keep isl_map
*map
)
1133 return isl_bool_error
;
1135 return isl_bool_false
;
1136 rational
= isl_basic_map_is_rational(map
->p
[0]);
1139 for (i
= 1; i
< map
->n
; ++i
) {
1140 isl_bool rational_i
;
1142 rational_i
= isl_basic_map_is_rational(map
->p
[i
]);
1145 if (rational
!= rational_i
)
1146 isl_die(isl_map_get_ctx(map
), isl_error_unsupported
,
1147 "mixed rational and integer basic maps "
1148 "not supported", return isl_bool_error
);
1154 /* Has "set" been marked as a rational set?
1155 * In particular, have all basic set in "set" been marked this way?
1156 * An empty set is not considered to be rational.
1157 * Sets where only some of the basic sets are marked rational
1160 isl_bool
isl_set_is_rational(__isl_keep isl_set
*set
)
1162 return isl_map_is_rational(set
);
1165 int isl_basic_set_is_rational(__isl_keep isl_basic_set
*bset
)
1167 return isl_basic_map_is_rational(bset
);
1170 /* Does "bmap" contain any rational points?
1172 * If "bmap" has an equality for each dimension, equating the dimension
1173 * to an integer constant, then it has no rational points, even if it
1174 * is marked as rational.
1176 isl_bool
isl_basic_map_has_rational(__isl_keep isl_basic_map
*bmap
)
1178 isl_bool has_rational
= isl_bool_true
;
1182 return isl_bool_error
;
1183 if (isl_basic_map_plain_is_empty(bmap
))
1184 return isl_bool_false
;
1185 if (!isl_basic_map_is_rational(bmap
))
1186 return isl_bool_false
;
1187 bmap
= isl_basic_map_copy(bmap
);
1188 bmap
= isl_basic_map_implicit_equalities(bmap
);
1189 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
1191 return isl_bool_error
;
1192 if (bmap
->n_eq
== total
) {
1194 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
1195 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1, total
);
1198 if (!isl_int_is_one(bmap
->eq
[i
][1 + j
]) &&
1199 !isl_int_is_negone(bmap
->eq
[i
][1 + j
]))
1201 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + j
+ 1,
1206 if (i
== bmap
->n_eq
)
1207 has_rational
= isl_bool_false
;
1209 isl_basic_map_free(bmap
);
1211 return has_rational
;
1214 /* Does "map" contain any rational points?
1216 isl_bool
isl_map_has_rational(__isl_keep isl_map
*map
)
1219 isl_bool has_rational
;
1222 return isl_bool_error
;
1223 for (i
= 0; i
< map
->n
; ++i
) {
1224 has_rational
= isl_basic_map_has_rational(map
->p
[i
]);
1225 if (has_rational
< 0 || has_rational
)
1226 return has_rational
;
1228 return isl_bool_false
;
1231 /* Does "set" contain any rational points?
1233 isl_bool
isl_set_has_rational(__isl_keep isl_set
*set
)
1235 return isl_map_has_rational(set
);
1238 /* Is this basic set a parameter domain?
1240 isl_bool
isl_basic_set_is_params(__isl_keep isl_basic_set
*bset
)
1243 return isl_bool_error
;
1244 return isl_space_is_params(bset
->dim
);
1247 /* Is this set a parameter domain?
1249 isl_bool
isl_set_is_params(__isl_keep isl_set
*set
)
1252 return isl_bool_error
;
1253 return isl_space_is_params(set
->dim
);
1256 /* Is this map actually a parameter domain?
1257 * Users should never call this function. Outside of isl,
1258 * a map can never be a parameter domain.
1260 isl_bool
isl_map_is_params(__isl_keep isl_map
*map
)
1263 return isl_bool_error
;
1264 return isl_space_is_params(map
->dim
);
1267 static __isl_give isl_basic_map
*basic_map_init(isl_ctx
*ctx
,
1268 __isl_take isl_basic_map
*bmap
, unsigned extra
,
1269 unsigned n_eq
, unsigned n_ineq
)
1272 isl_space
*space
= isl_basic_map_peek_space(bmap
);
1273 isl_size n_var
= isl_space_dim(space
, isl_dim_all
);
1274 size_t row_size
= 1 + n_var
+ extra
;
1280 return isl_basic_map_free(bmap
);
1282 bmap
->block
= isl_blk_alloc(ctx
, (n_ineq
+ n_eq
) * row_size
);
1283 if (isl_blk_is_error(bmap
->block
))
1286 bmap
->ineq
= isl_alloc_array(ctx
, isl_int
*, n_ineq
+ n_eq
);
1287 if ((n_ineq
+ n_eq
) && !bmap
->ineq
)
1291 bmap
->block2
= isl_blk_empty();
1294 bmap
->block2
= isl_blk_alloc(ctx
, extra
* (1 + row_size
));
1295 if (isl_blk_is_error(bmap
->block2
))
1298 bmap
->div
= isl_alloc_array(ctx
, isl_int
*, extra
);
1303 for (i
= 0; i
< n_ineq
+ n_eq
; ++i
)
1304 bmap
->ineq
[i
] = bmap
->block
.data
+ i
* row_size
;
1306 for (i
= 0; i
< extra
; ++i
)
1307 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + row_size
);
1311 bmap
->c_size
= n_eq
+ n_ineq
;
1312 bmap
->eq
= bmap
->ineq
+ n_ineq
;
1313 bmap
->extra
= extra
;
1317 bmap
->sample
= NULL
;
1321 isl_basic_map_free(bmap
);
1325 __isl_give isl_basic_set
*isl_basic_set_alloc(isl_ctx
*ctx
,
1326 unsigned nparam
, unsigned dim
, unsigned extra
,
1327 unsigned n_eq
, unsigned n_ineq
)
1329 struct isl_basic_map
*bmap
;
1332 space
= isl_space_set_alloc(ctx
, nparam
, dim
);
1336 bmap
= isl_basic_map_alloc_space(space
, extra
, n_eq
, n_ineq
);
1337 return bset_from_bmap(bmap
);
1340 __isl_give isl_basic_set
*isl_basic_set_alloc_space(__isl_take isl_space
*space
,
1341 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
1343 struct isl_basic_map
*bmap
;
1346 isl_assert(space
->ctx
, space
->n_in
== 0, goto error
);
1347 bmap
= isl_basic_map_alloc_space(space
, extra
, n_eq
, n_ineq
);
1348 return bset_from_bmap(bmap
);
1350 isl_space_free(space
);
1354 __isl_give isl_basic_map
*isl_basic_map_alloc_space(__isl_take isl_space
*space
,
1355 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
1357 struct isl_basic_map
*bmap
;
1361 bmap
= isl_calloc_type(space
->ctx
, struct isl_basic_map
);
1366 return basic_map_init(space
->ctx
, bmap
, extra
, n_eq
, n_ineq
);
1368 isl_space_free(space
);
1372 __isl_give isl_basic_map
*isl_basic_map_alloc(isl_ctx
*ctx
,
1373 unsigned nparam
, unsigned in
, unsigned out
, unsigned extra
,
1374 unsigned n_eq
, unsigned n_ineq
)
1376 struct isl_basic_map
*bmap
;
1379 space
= isl_space_alloc(ctx
, nparam
, in
, out
);
1383 bmap
= isl_basic_map_alloc_space(space
, extra
, n_eq
, n_ineq
);
1387 static __isl_give isl_basic_map
*dup_constraints(__isl_take isl_basic_map
*dst
,
1388 __isl_keep isl_basic_map
*src
)
1391 isl_size total
= isl_basic_map_dim(src
, isl_dim_all
);
1393 if (!dst
|| total
< 0)
1394 return isl_basic_map_free(dst
);
1396 for (i
= 0; i
< src
->n_eq
; ++i
) {
1397 int j
= isl_basic_map_alloc_equality(dst
);
1399 return isl_basic_map_free(dst
);
1400 isl_seq_cpy(dst
->eq
[j
], src
->eq
[i
], 1+total
);
1403 for (i
= 0; i
< src
->n_ineq
; ++i
) {
1404 int j
= isl_basic_map_alloc_inequality(dst
);
1406 return isl_basic_map_free(dst
);
1407 isl_seq_cpy(dst
->ineq
[j
], src
->ineq
[i
], 1+total
);
1410 for (i
= 0; i
< src
->n_div
; ++i
) {
1411 int j
= isl_basic_map_alloc_div(dst
);
1413 return isl_basic_map_free(dst
);
1414 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+total
);
1416 ISL_F_SET(dst
, ISL_BASIC_SET_FINAL
);
1420 __isl_give isl_basic_map
*isl_basic_map_dup(__isl_keep isl_basic_map
*bmap
)
1422 struct isl_basic_map
*dup
;
1426 dup
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
1427 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
1428 dup
= dup_constraints(dup
, bmap
);
1431 dup
->flags
= bmap
->flags
;
1432 dup
->sample
= isl_vec_copy(bmap
->sample
);
1436 __isl_give isl_basic_set
*isl_basic_set_dup(__isl_keep isl_basic_set
*bset
)
1438 struct isl_basic_map
*dup
;
1440 dup
= isl_basic_map_dup(bset_to_bmap(bset
));
1441 return bset_from_bmap(dup
);
1444 __isl_give isl_basic_set
*isl_basic_set_copy(__isl_keep isl_basic_set
*bset
)
1449 if (ISL_F_ISSET(bset
, ISL_BASIC_SET_FINAL
)) {
1453 return isl_basic_set_dup(bset
);
1456 __isl_give isl_set
*isl_set_copy(__isl_keep isl_set
*set
)
1465 __isl_give isl_basic_map
*isl_basic_map_copy(__isl_keep isl_basic_map
*bmap
)
1470 if (ISL_F_ISSET(bmap
, ISL_BASIC_SET_FINAL
)) {
1474 bmap
= isl_basic_map_dup(bmap
);
1476 ISL_F_SET(bmap
, ISL_BASIC_SET_FINAL
);
1480 __isl_give isl_map
*isl_map_copy(__isl_keep isl_map
*map
)
1489 __isl_null isl_basic_map
*isl_basic_map_free(__isl_take isl_basic_map
*bmap
)
1494 if (--bmap
->ref
> 0)
1497 isl_ctx_deref(bmap
->ctx
);
1499 isl_blk_free(bmap
->ctx
, bmap
->block2
);
1501 isl_blk_free(bmap
->ctx
, bmap
->block
);
1502 isl_vec_free(bmap
->sample
);
1503 isl_space_free(bmap
->dim
);
1509 __isl_null isl_basic_set
*isl_basic_set_free(__isl_take isl_basic_set
*bset
)
1511 return isl_basic_map_free(bset_to_bmap(bset
));
1514 static int room_for_con(__isl_keep isl_basic_map
*bmap
, unsigned n
)
1516 return bmap
->n_eq
+ bmap
->n_ineq
+ n
<= bmap
->c_size
;
1519 /* Check that "bset" does not involve any parameters.
1521 isl_stat
isl_basic_set_check_no_params(__isl_keep isl_basic_set
*bset
)
1525 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
1527 return isl_stat_error
;
1529 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
1530 "basic set should not have any parameters",
1531 return isl_stat_error
);
1535 /* Check that "bset" does not involve any local variables.
1537 isl_stat
isl_basic_set_check_no_locals(__isl_keep isl_basic_set
*bset
)
1541 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
1543 return isl_stat_error
;
1545 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
1546 "basic set should not have any local variables",
1547 return isl_stat_error
);
1552 #define TYPE isl_map
1554 #include "isl_check_named_params_templ.c"
1557 #define TYPE isl_basic_map
1560 #include "isl_check_named_params_templ.c"
1562 /* Check that "bmap1" and "bmap2" have the same parameters,
1563 * reporting an error if they do not.
1565 static isl_stat
isl_basic_map_check_equal_params(
1566 __isl_keep isl_basic_map
*bmap1
, __isl_keep isl_basic_map
*bmap2
)
1570 match
= isl_basic_map_has_equal_params(bmap1
, bmap2
);
1572 return isl_stat_error
;
1574 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
1575 "parameters don't match", return isl_stat_error
);
1580 #define TYPE isl_map
1582 #include "isl_align_params_bin_templ.c"
1587 #define ARG1 isl_map
1589 #define ARG2 isl_set
1591 #include "isl_align_params_templ.c"
1593 isl_bool
isl_map_align_params_map_map_and_test(__isl_keep isl_map
*map1
,
1594 __isl_keep isl_map
*map2
,
1595 isl_bool (*fn
)(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
))
1600 return isl_bool_error
;
1601 if (isl_map_has_equal_params(map1
, map2
))
1602 return fn(map1
, map2
);
1603 if (isl_map_check_named_params(map1
) < 0)
1604 return isl_bool_error
;
1605 if (isl_map_check_named_params(map2
) < 0)
1606 return isl_bool_error
;
1607 map1
= isl_map_copy(map1
);
1608 map2
= isl_map_copy(map2
);
1609 map1
= isl_map_align_params(map1
, isl_map_get_space(map2
));
1610 map2
= isl_map_align_params(map2
, isl_map_get_space(map1
));
1617 int isl_basic_map_alloc_equality(__isl_keep isl_basic_map
*bmap
)
1620 struct isl_ctx
*ctx
;
1622 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
1626 isl_assert(ctx
, room_for_con(bmap
, 1), return -1);
1627 isl_assert(ctx
, (bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
<= bmap
->c_size
,
1629 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1630 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1631 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1632 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1633 if ((bmap
->eq
- bmap
->ineq
) + bmap
->n_eq
== bmap
->c_size
) {
1635 int j
= isl_basic_map_alloc_inequality(bmap
);
1639 bmap
->ineq
[j
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1640 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
1647 isl_seq_clr(bmap
->eq
[bmap
->n_eq
] + 1 + total
,
1648 bmap
->extra
- bmap
->n_div
);
1649 return bmap
->n_eq
++;
1652 int isl_basic_set_alloc_equality(__isl_keep isl_basic_set
*bset
)
1654 return isl_basic_map_alloc_equality(bset_to_bmap(bset
));
1657 __isl_give isl_basic_map
*isl_basic_map_free_equality(
1658 __isl_take isl_basic_map
*bmap
, unsigned n
)
1663 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
1664 "invalid number of equalities",
1665 isl_basic_map_free(bmap
));
1670 __isl_give isl_basic_set
*isl_basic_set_free_equality(
1671 __isl_take isl_basic_set
*bset
, unsigned n
)
1673 return bset_from_bmap(isl_basic_map_free_equality(bset_to_bmap(bset
),
1677 /* Drop the equality constraint at position "pos",
1678 * preserving the order of the other equality constraints.
1680 int isl_basic_map_drop_equality(__isl_keep isl_basic_map
*bmap
, unsigned pos
)
1687 isl_assert(bmap
->ctx
, pos
< bmap
->n_eq
, return -1);
1691 for (r
= pos
; r
< bmap
->n_eq
; ++r
)
1692 bmap
->eq
[r
] = bmap
->eq
[r
+ 1];
1693 bmap
->eq
[bmap
->n_eq
] = t
;
1698 /* Turn inequality "pos" of "bmap" into an equality.
1700 * In particular, we move the inequality in front of the equalities
1701 * and move the last inequality in the position of the moved inequality.
1702 * Note that isl_tab_make_equalities_explicit depends on this particular
1703 * change in the ordering of the constraints.
1705 void isl_basic_map_inequality_to_equality(
1706 __isl_keep isl_basic_map
*bmap
, unsigned pos
)
1710 t
= bmap
->ineq
[pos
];
1711 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1712 bmap
->ineq
[bmap
->n_ineq
- 1] = bmap
->eq
[-1];
1717 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1718 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
1719 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1720 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1723 static int room_for_ineq(__isl_keep isl_basic_map
*bmap
, unsigned n
)
1725 return bmap
->n_ineq
+ n
<= bmap
->eq
- bmap
->ineq
;
1728 int isl_basic_map_alloc_inequality(__isl_keep isl_basic_map
*bmap
)
1731 struct isl_ctx
*ctx
;
1733 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
1737 isl_assert(ctx
, room_for_ineq(bmap
, 1), return -1);
1738 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_IMPLICIT
);
1739 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
1740 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
1741 ISL_F_CLR(bmap
, ISL_BASIC_MAP_ALL_EQUALITIES
);
1742 isl_seq_clr(bmap
->ineq
[bmap
->n_ineq
] + 1 + total
,
1743 bmap
->extra
- bmap
->n_div
);
1744 return bmap
->n_ineq
++;
1747 int isl_basic_set_alloc_inequality(__isl_keep isl_basic_set
*bset
)
1749 return isl_basic_map_alloc_inequality(bset_to_bmap(bset
));
1752 __isl_give isl_basic_map
*isl_basic_map_free_inequality(
1753 __isl_take isl_basic_map
*bmap
, unsigned n
)
1757 if (n
> bmap
->n_ineq
)
1758 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
1759 "invalid number of inequalities",
1760 return isl_basic_map_free(bmap
));
1765 __isl_give isl_basic_set
*isl_basic_set_free_inequality(
1766 __isl_take isl_basic_set
*bset
, unsigned n
)
1768 return bset_from_bmap(isl_basic_map_free_inequality(bset_to_bmap(bset
),
1772 int isl_basic_map_drop_inequality(__isl_keep isl_basic_map
*bmap
, unsigned pos
)
1777 isl_assert(bmap
->ctx
, pos
< bmap
->n_ineq
, return -1);
1779 if (pos
!= bmap
->n_ineq
- 1) {
1780 t
= bmap
->ineq
[pos
];
1781 bmap
->ineq
[pos
] = bmap
->ineq
[bmap
->n_ineq
- 1];
1782 bmap
->ineq
[bmap
->n_ineq
- 1] = t
;
1783 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
1789 int isl_basic_set_drop_inequality(__isl_keep isl_basic_set
*bset
, unsigned pos
)
1791 return isl_basic_map_drop_inequality(bset_to_bmap(bset
), pos
);
1794 __isl_give isl_basic_map
*isl_basic_map_add_eq(__isl_take isl_basic_map
*bmap
,
1801 empty
= isl_basic_map_plain_is_empty(bmap
);
1803 return isl_basic_map_free(bmap
);
1807 bmap
= isl_basic_map_cow(bmap
);
1808 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
1809 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
1811 return isl_basic_map_free(bmap
);
1812 k
= isl_basic_map_alloc_equality(bmap
);
1815 isl_seq_cpy(bmap
->eq
[k
], eq
, 1 + total
);
1818 isl_basic_map_free(bmap
);
1822 __isl_give isl_basic_set
*isl_basic_set_add_eq(__isl_take isl_basic_set
*bset
,
1825 return bset_from_bmap(isl_basic_map_add_eq(bset_to_bmap(bset
), eq
));
1828 __isl_give isl_basic_map
*isl_basic_map_add_ineq(__isl_take isl_basic_map
*bmap
,
1834 bmap
= isl_basic_map_cow(bmap
);
1835 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
1836 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
1838 return isl_basic_map_free(bmap
);
1839 k
= isl_basic_map_alloc_inequality(bmap
);
1842 isl_seq_cpy(bmap
->ineq
[k
], ineq
, 1 + total
);
1845 isl_basic_map_free(bmap
);
1849 __isl_give isl_basic_set
*isl_basic_set_add_ineq(__isl_take isl_basic_set
*bset
,
1852 return bset_from_bmap(isl_basic_map_add_ineq(bset_to_bmap(bset
), ineq
));
1855 int isl_basic_map_alloc_div(__isl_keep isl_basic_map
*bmap
)
1859 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
1862 isl_assert(bmap
->ctx
, bmap
->n_div
< bmap
->extra
, return -1);
1863 isl_seq_clr(bmap
->div
[bmap
->n_div
] + 1 + 1 + total
,
1864 bmap
->extra
- bmap
->n_div
);
1865 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1866 return bmap
->n_div
++;
1869 int isl_basic_set_alloc_div(__isl_keep isl_basic_set
*bset
)
1871 return isl_basic_map_alloc_div(bset_to_bmap(bset
));
1875 #define TYPE isl_basic_map
1876 #include "check_type_range_templ.c"
1878 /* Check that there are "n" dimensions of type "type" starting at "first"
1881 isl_stat
isl_basic_set_check_range(__isl_keep isl_basic_set
*bset
,
1882 enum isl_dim_type type
, unsigned first
, unsigned n
)
1884 return isl_basic_map_check_range(bset_to_bmap(bset
),
1888 /* Insert an extra integer division, prescribed by "div", to "bmap"
1889 * at (integer division) position "pos".
1891 * The integer division is first added at the end and then moved
1892 * into the right position.
1894 __isl_give isl_basic_map
*isl_basic_map_insert_div(
1895 __isl_take isl_basic_map
*bmap
, int pos
, __isl_keep isl_vec
*div
)
1900 bmap
= isl_basic_map_cow(bmap
);
1901 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
1902 if (total
< 0 || !div
)
1903 return isl_basic_map_free(bmap
);
1905 if (div
->size
!= 1 + 1 + total
)
1906 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
1907 "unexpected size", return isl_basic_map_free(bmap
));
1908 if (isl_basic_map_check_range(bmap
, isl_dim_div
, pos
, 0) < 0)
1909 return isl_basic_map_free(bmap
);
1911 bmap
= isl_basic_map_extend(bmap
, 1, 0, 2);
1912 k
= isl_basic_map_alloc_div(bmap
);
1914 return isl_basic_map_free(bmap
);
1915 isl_seq_cpy(bmap
->div
[k
], div
->el
, div
->size
);
1916 isl_int_set_si(bmap
->div
[k
][div
->size
], 0);
1918 for (i
= k
; i
> pos
; --i
)
1919 bmap
= isl_basic_map_swap_div(bmap
, i
, i
- 1);
1924 isl_stat
isl_basic_map_free_div(__isl_keep isl_basic_map
*bmap
, unsigned n
)
1927 return isl_stat_error
;
1928 isl_assert(bmap
->ctx
, n
<= bmap
->n_div
, return isl_stat_error
);
1933 static __isl_give isl_basic_map
*add_constraints(
1934 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
,
1935 unsigned i_pos
, unsigned o_pos
)
1937 isl_size total
, n_param
, n_in
, n_out
, n_div
;
1938 unsigned o_in
, o_out
;
1941 struct isl_dim_map
*dim_map
;
1943 space
= isl_basic_map_peek_space(bmap2
);
1944 if (!bmap1
|| !space
)
1947 total
= isl_basic_map_dim(bmap1
, isl_dim_all
);
1948 n_param
= isl_basic_map_dim(bmap2
, isl_dim_param
);
1949 n_in
= isl_basic_map_dim(bmap2
, isl_dim_in
);
1950 o_in
= isl_basic_map_offset(bmap1
, isl_dim_in
) - 1 + i_pos
;
1951 n_out
= isl_basic_map_dim(bmap2
, isl_dim_out
);
1952 o_out
= isl_basic_map_offset(bmap1
, isl_dim_out
) - 1 + o_pos
;
1953 n_div
= isl_basic_map_dim(bmap2
, isl_dim_div
);
1954 if (total
< 0 || n_param
< 0 || n_in
< 0 || n_out
< 0 || n_div
< 0)
1956 ctx
= isl_basic_map_get_ctx(bmap1
);
1957 dim_map
= isl_dim_map_alloc(ctx
, total
+ n_div
);
1958 isl_dim_map_dim_range(dim_map
, space
, isl_dim_param
, 0, n_param
, 0);
1959 isl_dim_map_dim_range(dim_map
, space
, isl_dim_in
, 0, n_in
, o_in
);
1960 isl_dim_map_dim_range(dim_map
, space
, isl_dim_out
, 0, n_out
, o_out
);
1961 isl_dim_map_div(dim_map
, bmap2
, total
);
1963 return isl_basic_map_add_constraints_dim_map(bmap1
, bmap2
, dim_map
);
1965 isl_basic_map_free(bmap1
);
1966 isl_basic_map_free(bmap2
);
1970 __isl_give isl_basic_map
*isl_basic_map_extend(__isl_take isl_basic_map
*base
,
1971 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
1974 struct isl_basic_map
*ext
;
1981 dims_ok
= base
->extra
>= base
->n_div
+ extra
;
1983 if (dims_ok
&& room_for_con(base
, n_eq
+ n_ineq
) &&
1984 room_for_ineq(base
, n_ineq
))
1987 extra
+= base
->extra
;
1989 n_ineq
+= base
->n_ineq
;
1991 space
= isl_basic_map_get_space(base
);
1992 ext
= isl_basic_map_alloc_space(space
, extra
, n_eq
, n_ineq
);
1997 ext
->sample
= isl_vec_copy(base
->sample
);
1998 flags
= base
->flags
;
1999 ext
= add_constraints(ext
, base
, 0, 0);
2002 ISL_F_CLR(ext
, ISL_BASIC_SET_FINAL
);
2008 isl_basic_map_free(base
);
2012 __isl_give isl_basic_set
*isl_basic_set_extend(__isl_take isl_basic_set
*base
,
2013 unsigned extra
, unsigned n_eq
, unsigned n_ineq
)
2015 return bset_from_bmap(isl_basic_map_extend(bset_to_bmap(base
),
2016 extra
, n_eq
, n_ineq
));
2019 __isl_give isl_basic_map
*isl_basic_map_extend_constraints(
2020 __isl_take isl_basic_map
*base
, unsigned n_eq
, unsigned n_ineq
)
2022 return isl_basic_map_extend(base
, 0, n_eq
, n_ineq
);
2025 __isl_give isl_basic_set
*isl_basic_set_extend_constraints(
2026 __isl_take isl_basic_set
*base
, unsigned n_eq
, unsigned n_ineq
)
2028 isl_basic_map
*bmap
= bset_to_bmap(base
);
2029 bmap
= isl_basic_map_extend_constraints(bmap
, n_eq
, n_ineq
);
2030 return bset_from_bmap(bmap
);
2033 __isl_give isl_basic_set
*isl_basic_set_cow(__isl_take isl_basic_set
*bset
)
2035 return bset_from_bmap(isl_basic_map_cow(bset_to_bmap(bset
)));
2038 __isl_give isl_basic_map
*isl_basic_map_cow(__isl_take isl_basic_map
*bmap
)
2043 if (bmap
->ref
> 1) {
2045 bmap
= isl_basic_map_dup(bmap
);
2048 ISL_F_CLR(bmap
, ISL_BASIC_SET_FINAL
);
2049 ISL_F_CLR(bmap
, ISL_BASIC_MAP_REDUCED_COEFFICIENTS
);
2054 /* Clear all cached information in "map", either because it is about
2055 * to be modified or because it is being freed.
2056 * Always return the same pointer that is passed in.
2057 * This is needed for the use in isl_map_free.
2059 static __isl_give isl_map
*clear_caches(__isl_take isl_map
*map
)
2061 isl_basic_map_free(map
->cached_simple_hull
[0]);
2062 isl_basic_map_free(map
->cached_simple_hull
[1]);
2063 map
->cached_simple_hull
[0] = NULL
;
2064 map
->cached_simple_hull
[1] = NULL
;
2068 __isl_give isl_set
*isl_set_cow(__isl_take isl_set
*set
)
2070 return isl_map_cow(set
);
2073 /* Return an isl_map that is equal to "map" and that has only
2074 * a single reference.
2076 * If the original input already has only one reference, then
2077 * simply return it, but clear all cached information, since
2078 * it may be rendered invalid by the operations that will be
2079 * performed on the result.
2081 * Otherwise, create a duplicate (without any cached information).
2083 __isl_give isl_map
*isl_map_cow(__isl_take isl_map
*map
)
2089 return clear_caches(map
);
2091 return isl_map_dup(map
);
2094 static void swap_vars(struct isl_blk blk
, isl_int
*a
,
2095 unsigned a_len
, unsigned b_len
)
2097 isl_seq_cpy(blk
.data
, a
+a_len
, b_len
);
2098 isl_seq_cpy(blk
.data
+b_len
, a
, a_len
);
2099 isl_seq_cpy(a
, blk
.data
, b_len
+a_len
);
2102 static __isl_give isl_basic_map
*isl_basic_map_swap_vars(
2103 __isl_take isl_basic_map
*bmap
, unsigned pos
, unsigned n1
, unsigned n2
)
2108 if (isl_basic_map_check_range(bmap
, isl_dim_all
, pos
- 1, n1
+ n2
) < 0)
2111 if (n1
== 0 || n2
== 0)
2114 bmap
= isl_basic_map_cow(bmap
);
2118 blk
= isl_blk_alloc(bmap
->ctx
, n1
+ n2
);
2119 if (isl_blk_is_error(blk
))
2122 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2124 bmap
->eq
[i
] + pos
, n1
, n2
);
2126 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2128 bmap
->ineq
[i
] + pos
, n1
, n2
);
2130 for (i
= 0; i
< bmap
->n_div
; ++i
)
2132 bmap
->div
[i
]+1 + pos
, n1
, n2
);
2134 isl_blk_free(bmap
->ctx
, blk
);
2136 ISL_F_CLR(bmap
, ISL_BASIC_SET_SORTED
);
2137 bmap
= isl_basic_map_gauss(bmap
, NULL
);
2138 return isl_basic_map_finalize(bmap
);
2140 isl_basic_map_free(bmap
);
2144 /* The given basic map has turned out to be empty.
2145 * Explicitly mark it as such and change the representation
2146 * to a canonical representation of the empty basic map.
2147 * Since the basic map has conflicting constraints,
2148 * it must have at least one constraint, except perhaps
2149 * if it was already explicitly marked as being empty.
2150 * Do nothing in the latter case, i.e., if it has been marked empty and
2151 * has no constraints.
2153 __isl_give isl_basic_map
*isl_basic_map_set_to_empty(
2154 __isl_take isl_basic_map
*bmap
)
2161 n
= isl_basic_map_n_constraint(bmap
);
2162 empty
= isl_basic_map_plain_is_empty(bmap
);
2163 if (n
< 0 || empty
< 0)
2164 return isl_basic_map_free(bmap
);
2165 if (n
== 0 && empty
)
2167 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
2169 return isl_basic_map_free(bmap
);
2170 if (isl_basic_map_free_div(bmap
, bmap
->n_div
) < 0)
2171 return isl_basic_map_free(bmap
);
2172 bmap
= isl_basic_map_free_inequality(bmap
, bmap
->n_ineq
);
2175 if (bmap
->n_eq
> 0) {
2176 bmap
= isl_basic_map_free_equality(bmap
, bmap
->n_eq
- 1);
2180 i
= isl_basic_map_alloc_equality(bmap
);
2184 isl_int_set_si(bmap
->eq
[i
][0], 1);
2185 isl_seq_clr(bmap
->eq
[i
]+1, total
);
2186 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
2187 isl_vec_free(bmap
->sample
);
2188 bmap
->sample
= NULL
;
2189 return isl_basic_map_finalize(bmap
);
2191 isl_basic_map_free(bmap
);
2195 __isl_give isl_basic_set
*isl_basic_set_set_to_empty(
2196 __isl_take isl_basic_set
*bset
)
2198 return bset_from_bmap(isl_basic_map_set_to_empty(bset_to_bmap(bset
)));
2201 __isl_give isl_basic_map
*isl_basic_map_set_rational(
2202 __isl_take isl_basic_map
*bmap
)
2207 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
2210 bmap
= isl_basic_map_cow(bmap
);
2214 ISL_F_SET(bmap
, ISL_BASIC_MAP_RATIONAL
);
2216 return isl_basic_map_finalize(bmap
);
2219 __isl_give isl_basic_set
*isl_basic_set_set_rational(
2220 __isl_take isl_basic_set
*bset
)
2222 return isl_basic_map_set_rational(bset
);
2225 __isl_give isl_basic_set
*isl_basic_set_set_integral(
2226 __isl_take isl_basic_set
*bset
)
2231 if (!ISL_F_ISSET(bset
, ISL_BASIC_MAP_RATIONAL
))
2234 bset
= isl_basic_set_cow(bset
);
2238 ISL_F_CLR(bset
, ISL_BASIC_MAP_RATIONAL
);
2240 return isl_basic_set_finalize(bset
);
2243 __isl_give isl_map
*isl_map_set_rational(__isl_take isl_map
*map
)
2247 map
= isl_map_cow(map
);
2250 for (i
= 0; i
< map
->n
; ++i
) {
2251 map
->p
[i
] = isl_basic_map_set_rational(map
->p
[i
]);
2261 __isl_give isl_set
*isl_set_set_rational(__isl_take isl_set
*set
)
2263 return isl_map_set_rational(set
);
2266 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
2269 static void swap_div(__isl_keep isl_basic_map
*bmap
, int a
, int b
)
2271 isl_int
*t
= bmap
->div
[a
];
2272 bmap
->div
[a
] = bmap
->div
[b
];
2276 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
2277 * div definitions accordingly.
2279 __isl_give isl_basic_map
*isl_basic_map_swap_div(__isl_take isl_basic_map
*bmap
,
2285 off
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
2287 return isl_basic_map_free(bmap
);
2289 swap_div(bmap
, a
, b
);
2291 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2292 isl_int_swap(bmap
->eq
[i
][1+off
+a
], bmap
->eq
[i
][1+off
+b
]);
2294 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2295 isl_int_swap(bmap
->ineq
[i
][1+off
+a
], bmap
->ineq
[i
][1+off
+b
]);
2297 for (i
= 0; i
< bmap
->n_div
; ++i
)
2298 isl_int_swap(bmap
->div
[i
][1+1+off
+a
], bmap
->div
[i
][1+1+off
+b
]);
2299 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
2304 static void constraint_drop_vars(isl_int
*c
, unsigned n
, unsigned rem
)
2306 isl_seq_cpy(c
, c
+ n
, rem
);
2307 isl_seq_clr(c
+ rem
, n
);
2310 /* Drop n dimensions starting at first.
2312 * In principle, this frees up some extra variables as the number
2313 * of columns remains constant, but we would have to extend
2314 * the div array too as the number of rows in this array is assumed
2315 * to be equal to extra.
2317 __isl_give isl_basic_set
*isl_basic_set_drop_dims(
2318 __isl_take isl_basic_set
*bset
, unsigned first
, unsigned n
)
2320 return isl_basic_map_drop(bset_to_bmap(bset
), isl_dim_set
, first
, n
);
2323 /* Move "n" divs starting at "first" to the end of the list of divs.
2325 static __isl_give isl_basic_map
*move_divs_last(__isl_take isl_basic_map
*bmap
,
2326 unsigned first
, unsigned n
)
2331 if (first
+ n
== bmap
->n_div
)
2334 div
= isl_alloc_array(bmap
->ctx
, isl_int
*, n
);
2337 for (i
= 0; i
< n
; ++i
)
2338 div
[i
] = bmap
->div
[first
+ i
];
2339 for (i
= 0; i
< bmap
->n_div
- first
- n
; ++i
)
2340 bmap
->div
[first
+ i
] = bmap
->div
[first
+ n
+ i
];
2341 for (i
= 0; i
< n
; ++i
)
2342 bmap
->div
[bmap
->n_div
- n
+ i
] = div
[i
];
2346 isl_basic_map_free(bmap
);
2351 #define TYPE isl_map
2353 #include "check_type_range_templ.c"
2355 /* Check that there are "n" dimensions of type "type" starting at "first"
2358 isl_stat
isl_set_check_range(__isl_keep isl_set
*set
,
2359 enum isl_dim_type type
, unsigned first
, unsigned n
)
2361 return isl_map_check_range(set_to_map(set
), type
, first
, n
);
2364 /* Drop "n" dimensions of type "type" starting at "first".
2365 * Perform the core computation, without cowing or
2366 * simplifying and finalizing the result.
2368 * In principle, this frees up some extra variables as the number
2369 * of columns remains constant, but we would have to extend
2370 * the div array too as the number of rows in this array is assumed
2371 * to be equal to extra.
2373 __isl_give isl_basic_map
*isl_basic_map_drop_core(
2374 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
2375 unsigned first
, unsigned n
)
2382 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2383 return isl_basic_map_free(bmap
);
2385 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
2387 return isl_basic_map_free(bmap
);
2389 offset
= isl_basic_map_offset(bmap
, type
) + first
;
2390 left
= total
- (offset
- 1) - n
;
2391 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2392 constraint_drop_vars(bmap
->eq
[i
]+offset
, n
, left
);
2394 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2395 constraint_drop_vars(bmap
->ineq
[i
]+offset
, n
, left
);
2397 for (i
= 0; i
< bmap
->n_div
; ++i
)
2398 constraint_drop_vars(bmap
->div
[i
]+1+offset
, n
, left
);
2400 if (type
== isl_dim_div
) {
2401 bmap
= move_divs_last(bmap
, first
, n
);
2404 if (isl_basic_map_free_div(bmap
, n
) < 0)
2405 return isl_basic_map_free(bmap
);
2407 bmap
->dim
= isl_space_drop_dims(bmap
->dim
, type
, first
, n
);
2409 return isl_basic_map_free(bmap
);
2411 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
2412 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
2416 /* Drop "n" dimensions of type "type" starting at "first".
2418 * In principle, this frees up some extra variables as the number
2419 * of columns remains constant, but we would have to extend
2420 * the div array too as the number of rows in this array is assumed
2421 * to be equal to extra.
2423 __isl_give isl_basic_map
*isl_basic_map_drop(__isl_take isl_basic_map
*bmap
,
2424 enum isl_dim_type type
, unsigned first
, unsigned n
)
2428 if (n
== 0 && !isl_space_is_named_or_nested(bmap
->dim
, type
))
2431 bmap
= isl_basic_map_cow(bmap
);
2435 bmap
= isl_basic_map_drop_core(bmap
, type
, first
, n
);
2437 bmap
= isl_basic_map_simplify(bmap
);
2438 return isl_basic_map_finalize(bmap
);
2441 __isl_give isl_basic_set
*isl_basic_set_drop(__isl_take isl_basic_set
*bset
,
2442 enum isl_dim_type type
, unsigned first
, unsigned n
)
2444 return bset_from_bmap(isl_basic_map_drop(bset_to_bmap(bset
),
2448 /* No longer consider "map" to be normalized.
2450 static __isl_give isl_map
*isl_map_unmark_normalized(__isl_take isl_map
*map
)
2454 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
2458 __isl_give isl_map
*isl_map_drop(__isl_take isl_map
*map
,
2459 enum isl_dim_type type
, unsigned first
, unsigned n
)
2464 if (isl_map_check_range(map
, type
, first
, n
) < 0)
2465 return isl_map_free(map
);
2467 if (n
== 0 && !isl_space_is_named_or_nested(map
->dim
, type
))
2469 map
= isl_map_cow(map
);
2473 for (i
= 0; i
< map
->n
; ++i
) {
2474 map
->p
[i
] = isl_basic_map_drop(map
->p
[i
], type
, first
, n
);
2478 map
= isl_map_unmark_normalized(map
);
2480 space
= isl_map_take_space(map
);
2481 space
= isl_space_drop_dims(space
, type
, first
, n
);
2482 map
= isl_map_restore_space(map
, space
);
2490 __isl_give isl_set
*isl_set_drop(__isl_take isl_set
*set
,
2491 enum isl_dim_type type
, unsigned first
, unsigned n
)
2493 return set_from_map(isl_map_drop(set_to_map(set
), type
, first
, n
));
2496 /* Drop the integer division at position "div", which is assumed
2497 * not to appear in any of the constraints or
2498 * in any of the other integer divisions.
2500 * Since the integer division is redundant, there is no need to cow.
2502 __isl_give isl_basic_map
*isl_basic_map_drop_div(
2503 __isl_take isl_basic_map
*bmap
, unsigned div
)
2505 return isl_basic_map_drop_core(bmap
, isl_dim_div
, div
, 1);
2508 /* Eliminate the specified n dimensions starting at first from the
2509 * constraints, without removing the dimensions from the space.
2510 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2512 __isl_give isl_map
*isl_map_eliminate(__isl_take isl_map
*map
,
2513 enum isl_dim_type type
, unsigned first
, unsigned n
)
2520 if (isl_map_check_range(map
, type
, first
, n
) < 0)
2521 return isl_map_free(map
);
2523 map
= isl_map_cow(map
);
2527 for (i
= 0; i
< map
->n
; ++i
) {
2528 map
->p
[i
] = isl_basic_map_eliminate(map
->p
[i
], type
, first
, n
);
2538 /* Eliminate the specified n dimensions starting at first from the
2539 * constraints, without removing the dimensions from the space.
2540 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2542 __isl_give isl_set
*isl_set_eliminate(__isl_take isl_set
*set
,
2543 enum isl_dim_type type
, unsigned first
, unsigned n
)
2545 return set_from_map(isl_map_eliminate(set_to_map(set
), type
, first
, n
));
2548 /* Eliminate the specified n dimensions starting at first from the
2549 * constraints, without removing the dimensions from the space.
2550 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2552 __isl_give isl_set
*isl_set_eliminate_dims(__isl_take isl_set
*set
,
2553 unsigned first
, unsigned n
)
2555 return isl_set_eliminate(set
, isl_dim_set
, first
, n
);
2558 __isl_give isl_basic_map
*isl_basic_map_remove_divs(
2559 __isl_take isl_basic_map
*bmap
)
2563 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
2565 return isl_basic_map_free(bmap
);
2566 bmap
= isl_basic_map_eliminate_vars(bmap
, v_div
, bmap
->n_div
);
2570 return isl_basic_map_finalize(bmap
);
2573 __isl_give isl_basic_set
*isl_basic_set_remove_divs(
2574 __isl_take isl_basic_set
*bset
)
2576 return bset_from_bmap(isl_basic_map_remove_divs(bset_to_bmap(bset
)));
2579 __isl_give isl_map
*isl_map_remove_divs(__isl_take isl_map
*map
)
2588 map
= isl_map_cow(map
);
2592 for (i
= 0; i
< map
->n
; ++i
) {
2593 map
->p
[i
] = isl_basic_map_remove_divs(map
->p
[i
]);
2603 __isl_give isl_set
*isl_set_remove_divs(__isl_take isl_set
*set
)
2605 return isl_map_remove_divs(set
);
2608 __isl_give isl_basic_map
*isl_basic_map_remove_dims(
2609 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
2610 unsigned first
, unsigned n
)
2612 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2613 return isl_basic_map_free(bmap
);
2614 if (n
== 0 && !isl_space_is_named_or_nested(bmap
->dim
, type
))
2616 bmap
= isl_basic_map_eliminate_vars(bmap
,
2617 isl_basic_map_offset(bmap
, type
) - 1 + first
, n
);
2620 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
) && type
== isl_dim_div
)
2622 bmap
= isl_basic_map_drop(bmap
, type
, first
, n
);
2626 /* Return true if the definition of the given div (recursively) involves
2627 * any of the given variables.
2629 static isl_bool
div_involves_vars(__isl_keep isl_basic_map
*bmap
, int div
,
2630 unsigned first
, unsigned n
)
2633 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
2635 if (isl_int_is_zero(bmap
->div
[div
][0]))
2636 return isl_bool_false
;
2637 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + first
, n
) >= 0)
2638 return isl_bool_true
;
2640 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2643 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
2645 involves
= div_involves_vars(bmap
, i
, first
, n
);
2646 if (involves
< 0 || involves
)
2650 return isl_bool_false
;
2653 /* Try and add a lower and/or upper bound on "div" to "bmap"
2654 * based on inequality "i".
2655 * "total" is the total number of variables (excluding the divs).
2656 * "v" is a temporary object that can be used during the calculations.
2657 * If "lb" is set, then a lower bound should be constructed.
2658 * If "ub" is set, then an upper bound should be constructed.
2660 * The calling function has already checked that the inequality does not
2661 * reference "div", but we still need to check that the inequality is
2662 * of the right form. We'll consider the case where we want to construct
2663 * a lower bound. The construction of upper bounds is similar.
2665 * Let "div" be of the form
2667 * q = floor((a + f(x))/d)
2669 * We essentially check if constraint "i" is of the form
2673 * so that we can use it to derive a lower bound on "div".
2674 * However, we allow a slightly more general form
2678 * with the condition that the coefficients of g(x) - f(x) are all
2680 * Rewriting this constraint as
2684 * adding a + f(x) to both sides and dividing by d, we obtain
2686 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
2688 * Taking the floor on both sides, we obtain
2690 * q >= floor((a-b)/d) + (f(x)-g(x))/d
2694 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
2696 * In the case of an upper bound, we construct the constraint
2698 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
2701 static __isl_give isl_basic_map
*insert_bounds_on_div_from_ineq(
2702 __isl_take isl_basic_map
*bmap
, int div
, int i
,
2703 unsigned total
, isl_int v
, int lb
, int ub
)
2707 for (j
= 0; (lb
|| ub
) && j
< total
+ bmap
->n_div
; ++j
) {
2709 isl_int_sub(v
, bmap
->ineq
[i
][1 + j
],
2710 bmap
->div
[div
][1 + 1 + j
]);
2711 lb
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
2714 isl_int_add(v
, bmap
->ineq
[i
][1 + j
],
2715 bmap
->div
[div
][1 + 1 + j
]);
2716 ub
= isl_int_is_divisible_by(v
, bmap
->div
[div
][0]);
2722 bmap
= isl_basic_map_cow(bmap
);
2723 bmap
= isl_basic_map_extend_constraints(bmap
, 0, lb
+ ub
);
2725 int k
= isl_basic_map_alloc_inequality(bmap
);
2728 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
2729 isl_int_sub(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
2730 bmap
->div
[div
][1 + j
]);
2731 isl_int_cdiv_q(bmap
->ineq
[k
][j
],
2732 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
2734 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], 1);
2737 int k
= isl_basic_map_alloc_inequality(bmap
);
2740 for (j
= 0; j
< 1 + total
+ bmap
->n_div
; ++j
) {
2741 isl_int_add(bmap
->ineq
[k
][j
], bmap
->ineq
[i
][j
],
2742 bmap
->div
[div
][1 + j
]);
2743 isl_int_fdiv_q(bmap
->ineq
[k
][j
],
2744 bmap
->ineq
[k
][j
], bmap
->div
[div
][0]);
2746 isl_int_set_si(bmap
->ineq
[k
][1 + total
+ div
], -1);
2751 isl_basic_map_free(bmap
);
2755 /* This function is called right before "div" is eliminated from "bmap"
2756 * using Fourier-Motzkin.
2757 * Look through the constraints of "bmap" for constraints on the argument
2758 * of the integer division and use them to construct constraints on the
2759 * integer division itself. These constraints can then be combined
2760 * during the Fourier-Motzkin elimination.
2761 * Note that it is only useful to introduce lower bounds on "div"
2762 * if "bmap" already contains upper bounds on "div" as the newly
2763 * introduce lower bounds can then be combined with the pre-existing
2764 * upper bounds. Similarly for upper bounds.
2765 * We therefore first check if "bmap" contains any lower and/or upper bounds
2768 * It is interesting to note that the introduction of these constraints
2769 * can indeed lead to more accurate results, even when compared to
2770 * deriving constraints on the argument of "div" from constraints on "div".
2771 * Consider, for example, the set
2773 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2775 * The second constraint can be rewritten as
2777 * 2 * [(-i-2j+3)/4] + k >= 0
2779 * from which we can derive
2781 * -i - 2j + 3 >= -2k
2787 * Combined with the first constraint, we obtain
2789 * -3 <= 3 + 2k or k >= -3
2791 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2792 * the first constraint, we obtain
2794 * [(i + 2j)/4] >= [-3/4] = -1
2796 * Combining this constraint with the second constraint, we obtain
2800 static __isl_give isl_basic_map
*insert_bounds_on_div(
2801 __isl_take isl_basic_map
*bmap
, int div
)
2804 int check_lb
, check_ub
;
2811 if (isl_int_is_zero(bmap
->div
[div
][0]))
2814 v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
2816 return isl_basic_map_free(bmap
);
2820 for (i
= 0; (!check_lb
|| !check_ub
) && i
< bmap
->n_ineq
; ++i
) {
2821 int s
= isl_int_sgn(bmap
->ineq
[i
][1 + v_div
+ div
]);
2828 if (!check_lb
&& !check_ub
)
2833 for (i
= 0; bmap
&& i
< bmap
->n_ineq
; ++i
) {
2834 if (!isl_int_is_zero(bmap
->ineq
[i
][1 + v_div
+ div
]))
2837 bmap
= insert_bounds_on_div_from_ineq(bmap
, div
, i
, v_div
, v
,
2838 check_lb
, check_ub
);
2846 /* Remove all divs (recursively) involving any of the given dimensions
2847 * in their definitions.
2849 __isl_give isl_basic_map
*isl_basic_map_remove_divs_involving_dims(
2850 __isl_take isl_basic_map
*bmap
,
2851 enum isl_dim_type type
, unsigned first
, unsigned n
)
2855 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2856 return isl_basic_map_free(bmap
);
2857 first
+= isl_basic_map_offset(bmap
, type
);
2859 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
2862 involves
= div_involves_vars(bmap
, i
, first
, n
);
2864 return isl_basic_map_free(bmap
);
2867 bmap
= insert_bounds_on_div(bmap
, i
);
2868 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
2877 __isl_give isl_basic_set
*isl_basic_set_remove_divs_involving_dims(
2878 __isl_take isl_basic_set
*bset
,
2879 enum isl_dim_type type
, unsigned first
, unsigned n
)
2881 return isl_basic_map_remove_divs_involving_dims(bset
, type
, first
, n
);
2884 __isl_give isl_map
*isl_map_remove_divs_involving_dims(__isl_take isl_map
*map
,
2885 enum isl_dim_type type
, unsigned first
, unsigned n
)
2894 map
= isl_map_cow(map
);
2898 for (i
= 0; i
< map
->n
; ++i
) {
2899 map
->p
[i
] = isl_basic_map_remove_divs_involving_dims(map
->p
[i
],
2910 __isl_give isl_set
*isl_set_remove_divs_involving_dims(__isl_take isl_set
*set
,
2911 enum isl_dim_type type
, unsigned first
, unsigned n
)
2913 return set_from_map(isl_map_remove_divs_involving_dims(set_to_map(set
),
2917 /* Does the description of "bmap" depend on the specified dimensions?
2918 * We also check whether the dimensions appear in any of the div definitions.
2919 * In principle there is no need for this check. If the dimensions appear
2920 * in a div definition, they also appear in the defining constraints of that
2923 isl_bool
isl_basic_map_involves_dims(__isl_keep isl_basic_map
*bmap
,
2924 enum isl_dim_type type
, unsigned first
, unsigned n
)
2928 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
2929 return isl_bool_error
;
2931 first
+= isl_basic_map_offset(bmap
, type
);
2932 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2933 if (isl_seq_first_non_zero(bmap
->eq
[i
] + first
, n
) >= 0)
2934 return isl_bool_true
;
2935 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2936 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + first
, n
) >= 0)
2937 return isl_bool_true
;
2938 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2939 if (isl_int_is_zero(bmap
->div
[i
][0]))
2941 if (isl_seq_first_non_zero(bmap
->div
[i
] + 1 + first
, n
) >= 0)
2942 return isl_bool_true
;
2945 return isl_bool_false
;
2948 isl_bool
isl_map_involves_dims(__isl_keep isl_map
*map
,
2949 enum isl_dim_type type
, unsigned first
, unsigned n
)
2953 if (isl_map_check_range(map
, type
, first
, n
) < 0)
2954 return isl_bool_error
;
2956 for (i
= 0; i
< map
->n
; ++i
) {
2957 isl_bool involves
= isl_basic_map_involves_dims(map
->p
[i
],
2959 if (involves
< 0 || involves
)
2963 return isl_bool_false
;
2966 isl_bool
isl_basic_set_involves_dims(__isl_keep isl_basic_set
*bset
,
2967 enum isl_dim_type type
, unsigned first
, unsigned n
)
2969 return isl_basic_map_involves_dims(bset
, type
, first
, n
);
2972 isl_bool
isl_set_involves_dims(__isl_keep isl_set
*set
,
2973 enum isl_dim_type type
, unsigned first
, unsigned n
)
2975 return isl_map_involves_dims(set
, type
, first
, n
);
2978 /* Does "bset" involve any local variables, i.e., integer divisions?
2980 static isl_bool
isl_basic_set_involves_locals(__isl_keep isl_basic_set
*bset
)
2984 n
= isl_basic_set_dim(bset
, isl_dim_div
);
2986 return isl_bool_error
;
2987 return isl_bool_ok(n
> 0);
2990 /* isl_set_every_basic_set callback that checks whether "bset"
2991 * is free of local variables.
2993 static isl_bool
basic_set_no_locals(__isl_keep isl_basic_set
*bset
, void *user
)
2995 return isl_bool_not(isl_basic_set_involves_locals(bset
));
2998 /* Does "set" involve any local variables, i.e., integer divisions?
3000 isl_bool
isl_set_involves_locals(__isl_keep isl_set
*set
)
3004 no_locals
= isl_set_every_basic_set(set
, &basic_set_no_locals
, NULL
);
3005 return isl_bool_not(no_locals
);
3008 /* Drop all constraints in bmap that involve any of the dimensions
3009 * first to first+n-1.
3010 * This function only performs the actual removal of constraints.
3012 * This function should not call finalize since it is used by
3013 * remove_redundant_divs, which in turn is called by isl_basic_map_finalize.
3015 __isl_give isl_basic_map
*isl_basic_map_drop_constraints_involving(
3016 __isl_take isl_basic_map
*bmap
, unsigned first
, unsigned n
)
3023 bmap
= isl_basic_map_cow(bmap
);
3028 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
) {
3029 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + first
, n
) == -1)
3031 if (isl_basic_map_drop_equality(bmap
, i
) < 0)
3032 return isl_basic_map_free(bmap
);
3035 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
3036 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + 1 + first
, n
) == -1)
3038 if (isl_basic_map_drop_inequality(bmap
, i
) < 0)
3039 return isl_basic_map_free(bmap
);
3045 /* Drop all constraints in bset that involve any of the dimensions
3046 * first to first+n-1.
3047 * This function only performs the actual removal of constraints.
3049 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_involving(
3050 __isl_take isl_basic_set
*bset
, unsigned first
, unsigned n
)
3052 return isl_basic_map_drop_constraints_involving(bset
, first
, n
);
3055 /* Drop all constraints in bmap that do not involve any of the dimensions
3056 * first to first + n - 1 of the given type.
3058 __isl_give isl_basic_map
*isl_basic_map_drop_constraints_not_involving_dims(
3059 __isl_take isl_basic_map
*bmap
,
3060 enum isl_dim_type type
, unsigned first
, unsigned n
)
3065 isl_space
*space
= isl_basic_map_get_space(bmap
);
3066 isl_basic_map_free(bmap
);
3067 return isl_basic_map_universe(space
);
3069 bmap
= isl_basic_map_cow(bmap
);
3073 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
3074 return isl_basic_map_free(bmap
);
3076 first
+= isl_basic_map_offset(bmap
, type
) - 1;
3078 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
) {
3079 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + first
, n
) != -1)
3081 if (isl_basic_map_drop_equality(bmap
, i
) < 0)
3082 return isl_basic_map_free(bmap
);
3085 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
3086 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + 1 + first
, n
) != -1)
3088 if (isl_basic_map_drop_inequality(bmap
, i
) < 0)
3089 return isl_basic_map_free(bmap
);
3092 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
3096 /* Drop all constraints in bset that do not involve any of the dimensions
3097 * first to first + n - 1 of the given type.
3099 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_not_involving_dims(
3100 __isl_take isl_basic_set
*bset
,
3101 enum isl_dim_type type
, unsigned first
, unsigned n
)
3103 return isl_basic_map_drop_constraints_not_involving_dims(bset
,
3107 /* Drop all constraints in bmap that involve any of the dimensions
3108 * first to first + n - 1 of the given type.
3110 __isl_give isl_basic_map
*isl_basic_map_drop_constraints_involving_dims(
3111 __isl_take isl_basic_map
*bmap
,
3112 enum isl_dim_type type
, unsigned first
, unsigned n
)
3119 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
3120 return isl_basic_map_free(bmap
);
3122 bmap
= isl_basic_map_remove_divs_involving_dims(bmap
, type
, first
, n
);
3123 first
+= isl_basic_map_offset(bmap
, type
) - 1;
3124 bmap
= isl_basic_map_drop_constraints_involving(bmap
, first
, n
);
3125 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
3129 /* Drop all constraints in bset that involve any of the dimensions
3130 * first to first + n - 1 of the given type.
3132 __isl_give isl_basic_set
*isl_basic_set_drop_constraints_involving_dims(
3133 __isl_take isl_basic_set
*bset
,
3134 enum isl_dim_type type
, unsigned first
, unsigned n
)
3136 return isl_basic_map_drop_constraints_involving_dims(bset
,
3140 /* Drop constraints from "map" by applying "drop" to each basic map.
3142 static __isl_give isl_map
*drop_constraints(__isl_take isl_map
*map
,
3143 enum isl_dim_type type
, unsigned first
, unsigned n
,
3144 __isl_give isl_basic_map
*(*drop
)(__isl_take isl_basic_map
*bmap
,
3145 enum isl_dim_type type
, unsigned first
, unsigned n
))
3149 if (isl_map_check_range(map
, type
, first
, n
) < 0)
3150 return isl_map_free(map
);
3152 map
= isl_map_cow(map
);
3156 for (i
= 0; i
< map
->n
; ++i
) {
3157 map
->p
[i
] = drop(map
->p
[i
], type
, first
, n
);
3159 return isl_map_free(map
);
3163 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
3168 /* Drop all constraints in map that involve any of the dimensions
3169 * first to first + n - 1 of the given type.
3171 __isl_give isl_map
*isl_map_drop_constraints_involving_dims(
3172 __isl_take isl_map
*map
,
3173 enum isl_dim_type type
, unsigned first
, unsigned n
)
3177 return drop_constraints(map
, type
, first
, n
,
3178 &isl_basic_map_drop_constraints_involving_dims
);
3181 /* Drop all constraints in "map" that do not involve any of the dimensions
3182 * first to first + n - 1 of the given type.
3184 __isl_give isl_map
*isl_map_drop_constraints_not_involving_dims(
3185 __isl_take isl_map
*map
,
3186 enum isl_dim_type type
, unsigned first
, unsigned n
)
3189 isl_space
*space
= isl_map_get_space(map
);
3191 return isl_map_universe(space
);
3193 return drop_constraints(map
, type
, first
, n
,
3194 &isl_basic_map_drop_constraints_not_involving_dims
);
3197 /* Drop all constraints in set that involve any of the dimensions
3198 * first to first + n - 1 of the given type.
3200 __isl_give isl_set
*isl_set_drop_constraints_involving_dims(
3201 __isl_take isl_set
*set
,
3202 enum isl_dim_type type
, unsigned first
, unsigned n
)
3204 return isl_map_drop_constraints_involving_dims(set
, type
, first
, n
);
3207 /* Drop all constraints in "set" that do not involve any of the dimensions
3208 * first to first + n - 1 of the given type.
3210 __isl_give isl_set
*isl_set_drop_constraints_not_involving_dims(
3211 __isl_take isl_set
*set
,
3212 enum isl_dim_type type
, unsigned first
, unsigned n
)
3214 return isl_map_drop_constraints_not_involving_dims(set
, type
, first
, n
);
3217 /* Does local variable "div" of "bmap" have a complete explicit representation?
3218 * Having a complete explicit representation requires not only
3219 * an explicit representation, but also that all local variables
3220 * that appear in this explicit representation in turn have
3221 * a complete explicit representation.
3223 isl_bool
isl_basic_map_div_is_known(__isl_keep isl_basic_map
*bmap
, int div
)
3226 unsigned div_offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
3229 marked
= isl_basic_map_div_is_marked_unknown(bmap
, div
);
3230 if (marked
< 0 || marked
)
3231 return isl_bool_not(marked
);
3233 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
3236 if (isl_int_is_zero(bmap
->div
[div
][1 + div_offset
+ i
]))
3238 known
= isl_basic_map_div_is_known(bmap
, i
);
3239 if (known
< 0 || !known
)
3243 return isl_bool_true
;
3246 /* Remove all divs that are unknown or defined in terms of unknown divs.
3248 __isl_give isl_basic_map
*isl_basic_map_remove_unknown_divs(
3249 __isl_take isl_basic_map
*bmap
)
3256 for (i
= bmap
->n_div
- 1; i
>= 0; --i
) {
3257 if (isl_basic_map_div_is_known(bmap
, i
))
3259 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, i
, 1);
3268 /* Remove all divs that are unknown or defined in terms of unknown divs.
3270 __isl_give isl_basic_set
*isl_basic_set_remove_unknown_divs(
3271 __isl_take isl_basic_set
*bset
)
3273 return isl_basic_map_remove_unknown_divs(bset
);
3276 __isl_give isl_map
*isl_map_remove_unknown_divs(__isl_take isl_map
*map
)
3285 map
= isl_map_cow(map
);
3289 for (i
= 0; i
< map
->n
; ++i
) {
3290 map
->p
[i
] = isl_basic_map_remove_unknown_divs(map
->p
[i
]);
3300 __isl_give isl_set
*isl_set_remove_unknown_divs(__isl_take isl_set
*set
)
3302 return set_from_map(isl_map_remove_unknown_divs(set_to_map(set
)));
3305 __isl_give isl_basic_set
*isl_basic_set_remove_dims(
3306 __isl_take isl_basic_set
*bset
,
3307 enum isl_dim_type type
, unsigned first
, unsigned n
)
3309 isl_basic_map
*bmap
= bset_to_bmap(bset
);
3310 bmap
= isl_basic_map_remove_dims(bmap
, type
, first
, n
);
3311 return bset_from_bmap(bmap
);
3314 __isl_give isl_map
*isl_map_remove_dims(__isl_take isl_map
*map
,
3315 enum isl_dim_type type
, unsigned first
, unsigned n
)
3322 map
= isl_map_cow(map
);
3323 if (isl_map_check_range(map
, type
, first
, n
) < 0)
3324 return isl_map_free(map
);
3326 for (i
= 0; i
< map
->n
; ++i
) {
3327 map
->p
[i
] = isl_basic_map_eliminate_vars(map
->p
[i
],
3328 isl_basic_map_offset(map
->p
[i
], type
) - 1 + first
, n
);
3332 map
= isl_map_drop(map
, type
, first
, n
);
3339 __isl_give isl_set
*isl_set_remove_dims(__isl_take isl_set
*bset
,
3340 enum isl_dim_type type
, unsigned first
, unsigned n
)
3342 return set_from_map(isl_map_remove_dims(set_to_map(bset
),
3346 /* Project out n inputs starting at first using Fourier-Motzkin */
3347 __isl_give isl_map
*isl_map_remove_inputs(__isl_take isl_map
*map
,
3348 unsigned first
, unsigned n
)
3350 return isl_map_remove_dims(map
, isl_dim_in
, first
, n
);
3353 void isl_basic_set_print_internal(__isl_keep isl_basic_set
*bset
,
3354 FILE *out
, int indent
)
3359 fprintf(out
, "null basic set\n");
3363 fprintf(out
, "%*s", indent
, "");
3364 fprintf(out
, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
3365 bset
->ref
, bset
->dim
->nparam
, bset
->dim
->n_out
,
3366 bset
->extra
, bset
->flags
);
3368 p
= isl_printer_to_file(isl_basic_set_get_ctx(bset
), out
);
3369 p
= isl_printer_set_dump(p
, 1);
3370 p
= isl_printer_set_indent(p
, indent
);
3371 p
= isl_printer_start_line(p
);
3372 p
= isl_printer_print_basic_set(p
, bset
);
3373 p
= isl_printer_end_line(p
);
3374 isl_printer_free(p
);
3377 void isl_basic_map_print_internal(__isl_keep isl_basic_map
*bmap
,
3378 FILE *out
, int indent
)
3383 fprintf(out
, "null basic map\n");
3387 fprintf(out
, "%*s", indent
, "");
3388 fprintf(out
, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
3389 "flags: %x, n_name: %d\n",
3391 bmap
->dim
->nparam
, bmap
->dim
->n_in
, bmap
->dim
->n_out
,
3392 bmap
->extra
, bmap
->flags
, bmap
->dim
->n_id
);
3394 p
= isl_printer_to_file(isl_basic_map_get_ctx(bmap
), out
);
3395 p
= isl_printer_set_dump(p
, 1);
3396 p
= isl_printer_set_indent(p
, indent
);
3397 p
= isl_printer_start_line(p
);
3398 p
= isl_printer_print_basic_map(p
, bmap
);
3399 p
= isl_printer_end_line(p
);
3400 isl_printer_free(p
);
3403 __isl_give isl_basic_map
*isl_inequality_negate(__isl_take isl_basic_map
*bmap
,
3408 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
3410 return isl_basic_map_free(bmap
);
3411 if (pos
>= bmap
->n_ineq
)
3412 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
3413 "invalid position", return isl_basic_map_free(bmap
));
3414 isl_seq_neg(bmap
->ineq
[pos
], bmap
->ineq
[pos
], 1 + total
);
3415 isl_int_sub_ui(bmap
->ineq
[pos
][0], bmap
->ineq
[pos
][0], 1);
3416 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NO_REDUNDANT
);
3417 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
3421 __isl_give isl_set
*isl_set_alloc_space(__isl_take isl_space
*space
, int n
,
3424 if (isl_space_check_is_set(space
) < 0)
3426 return isl_map_alloc_space(space
, n
, flags
);
3428 isl_space_free(space
);
3432 /* Make sure "map" has room for at least "n" more basic maps.
3434 __isl_give isl_map
*isl_map_grow(__isl_take isl_map
*map
, int n
)
3437 struct isl_map
*grown
= NULL
;
3441 isl_assert(map
->ctx
, n
>= 0, goto error
);
3442 if (map
->n
+ n
<= map
->size
)
3444 grown
= isl_map_alloc_space(isl_map_get_space(map
), map
->n
+ n
, map
->flags
);
3447 for (i
= 0; i
< map
->n
; ++i
) {
3448 grown
->p
[i
] = isl_basic_map_copy(map
->p
[i
]);
3456 isl_map_free(grown
);
3461 /* Make sure "set" has room for at least "n" more basic sets.
3463 __isl_give isl_set
*isl_set_grow(__isl_take isl_set
*set
, int n
)
3465 return set_from_map(isl_map_grow(set_to_map(set
), n
));
3468 __isl_give isl_set
*isl_set_from_basic_set(__isl_take isl_basic_set
*bset
)
3470 return isl_map_from_basic_map(bset
);
3473 /* This function performs the same operation as isl_set_from_basic_set,
3474 * but is considered as a function on an isl_basic_set when exported.
3476 __isl_give isl_set
*isl_basic_set_to_set(__isl_take isl_basic_set
*bset
)
3478 return isl_set_from_basic_set(bset
);
3481 __isl_give isl_map
*isl_map_from_basic_map(__isl_take isl_basic_map
*bmap
)
3483 struct isl_map
*map
;
3488 map
= isl_map_alloc_space(isl_space_copy(bmap
->dim
), 1, ISL_MAP_DISJOINT
);
3489 return isl_map_add_basic_map(map
, bmap
);
3492 __isl_give isl_set
*isl_set_add_basic_set(__isl_take isl_set
*set
,
3493 __isl_take isl_basic_set
*bset
)
3495 return set_from_map(isl_map_add_basic_map(set_to_map(set
),
3496 bset_to_bmap(bset
)));
3499 __isl_null isl_set
*isl_set_free(__isl_take isl_set
*set
)
3501 return isl_map_free(set
);
3504 void isl_set_print_internal(__isl_keep isl_set
*set
, FILE *out
, int indent
)
3509 fprintf(out
, "null set\n");
3513 fprintf(out
, "%*s", indent
, "");
3514 fprintf(out
, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
3515 set
->ref
, set
->n
, set
->dim
->nparam
, set
->dim
->n_out
,
3517 for (i
= 0; i
< set
->n
; ++i
) {
3518 fprintf(out
, "%*s", indent
, "");
3519 fprintf(out
, "basic set %d:\n", i
);
3520 isl_basic_set_print_internal(set
->p
[i
], out
, indent
+4);
3524 void isl_map_print_internal(__isl_keep isl_map
*map
, FILE *out
, int indent
)
3529 fprintf(out
, "null map\n");
3533 fprintf(out
, "%*s", indent
, "");
3534 fprintf(out
, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
3535 "flags: %x, n_name: %d\n",
3536 map
->ref
, map
->n
, map
->dim
->nparam
, map
->dim
->n_in
,
3537 map
->dim
->n_out
, map
->flags
, map
->dim
->n_id
);
3538 for (i
= 0; i
< map
->n
; ++i
) {
3539 fprintf(out
, "%*s", indent
, "");
3540 fprintf(out
, "basic map %d:\n", i
);
3541 isl_basic_map_print_internal(map
->p
[i
], out
, indent
+4);
3545 /* Check that the space of "bset" is the same as that of the domain of "bmap".
3547 static isl_stat
isl_basic_map_check_compatible_domain(
3548 __isl_keep isl_basic_map
*bmap
, __isl_keep isl_basic_set
*bset
)
3552 ok
= isl_basic_map_compatible_domain(bmap
, bset
);
3554 return isl_stat_error
;
3556 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
3557 "incompatible spaces", return isl_stat_error
);
3562 __isl_give isl_basic_map
*isl_basic_map_intersect_domain(
3563 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*bset
)
3565 struct isl_basic_map
*bmap_domain
;
3568 if (isl_basic_map_check_equal_params(bmap
, bset_to_bmap(bset
)) < 0)
3571 dim
= isl_basic_set_dim(bset
, isl_dim_set
);
3575 isl_basic_map_check_compatible_domain(bmap
, bset
) < 0)
3578 bmap
= isl_basic_map_cow(bmap
);
3581 bmap
= isl_basic_map_extend(bmap
,
3582 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
3583 bmap_domain
= isl_basic_map_from_domain(bset
);
3584 bmap
= add_constraints(bmap
, bmap_domain
, 0, 0);
3586 bmap
= isl_basic_map_simplify(bmap
);
3587 return isl_basic_map_finalize(bmap
);
3589 isl_basic_map_free(bmap
);
3590 isl_basic_set_free(bset
);
3594 /* Check that the space of "bset" is the same as that of the range of "bmap".
3596 static isl_stat
isl_basic_map_check_compatible_range(
3597 __isl_keep isl_basic_map
*bmap
, __isl_keep isl_basic_set
*bset
)
3601 ok
= isl_basic_map_compatible_range(bmap
, bset
);
3603 return isl_stat_error
;
3605 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
3606 "incompatible spaces", return isl_stat_error
);
3611 __isl_give isl_basic_map
*isl_basic_map_intersect_range(
3612 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*bset
)
3614 struct isl_basic_map
*bmap_range
;
3617 if (isl_basic_map_check_equal_params(bmap
, bset_to_bmap(bset
)) < 0)
3620 dim
= isl_basic_set_dim(bset
, isl_dim_set
);
3623 if (dim
!= 0 && isl_basic_map_check_compatible_range(bmap
, bset
) < 0)
3626 if (isl_basic_set_plain_is_universe(bset
)) {
3627 isl_basic_set_free(bset
);
3631 bmap
= isl_basic_map_cow(bmap
);
3634 bmap
= isl_basic_map_extend(bmap
,
3635 bset
->n_div
, bset
->n_eq
, bset
->n_ineq
);
3636 bmap_range
= bset_to_bmap(bset
);
3637 bmap
= add_constraints(bmap
, bmap_range
, 0, 0);
3639 bmap
= isl_basic_map_simplify(bmap
);
3640 return isl_basic_map_finalize(bmap
);
3642 isl_basic_map_free(bmap
);
3643 isl_basic_set_free(bset
);
3647 isl_bool
isl_basic_map_contains(__isl_keep isl_basic_map
*bmap
,
3648 __isl_keep isl_vec
*vec
)
3654 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
3655 if (total
< 0 || !vec
)
3656 return isl_bool_error
;
3658 if (1 + total
!= vec
->size
)
3659 return isl_bool_false
;
3663 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
3664 isl_seq_inner_product(vec
->el
, bmap
->eq
[i
], 1 + total
, &s
);
3665 if (!isl_int_is_zero(s
)) {
3667 return isl_bool_false
;
3671 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
3672 isl_seq_inner_product(vec
->el
, bmap
->ineq
[i
], 1 + total
, &s
);
3673 if (isl_int_is_neg(s
)) {
3675 return isl_bool_false
;
3681 return isl_bool_true
;
3684 isl_bool
isl_basic_set_contains(__isl_keep isl_basic_set
*bset
,
3685 __isl_keep isl_vec
*vec
)
3687 return isl_basic_map_contains(bset_to_bmap(bset
), vec
);
3690 __isl_give isl_basic_map
*isl_basic_map_intersect(
3691 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
3693 struct isl_vec
*sample
= NULL
;
3694 isl_space
*space1
, *space2
;
3695 isl_size dim1
, dim2
, nparam1
, nparam2
;
3697 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
3699 space1
= isl_basic_map_peek_space(bmap1
);
3700 space2
= isl_basic_map_peek_space(bmap2
);
3701 dim1
= isl_space_dim(space1
, isl_dim_all
);
3702 dim2
= isl_space_dim(space2
, isl_dim_all
);
3703 nparam1
= isl_space_dim(space1
, isl_dim_param
);
3704 nparam2
= isl_space_dim(space2
, isl_dim_param
);
3705 if (dim1
< 0 || dim2
< 0 || nparam1
< 0 || nparam2
< 0)
3707 if (dim1
== nparam1
&& dim2
!= nparam2
)
3708 return isl_basic_map_intersect(bmap2
, bmap1
);
3710 if (dim2
!= nparam2
&&
3711 isl_basic_map_check_equal_space(bmap1
, bmap2
) < 0)
3714 if (isl_basic_map_plain_is_empty(bmap1
)) {
3715 isl_basic_map_free(bmap2
);
3718 if (isl_basic_map_plain_is_empty(bmap2
)) {
3719 isl_basic_map_free(bmap1
);
3723 if (bmap1
->sample
&&
3724 isl_basic_map_contains(bmap1
, bmap1
->sample
) > 0 &&
3725 isl_basic_map_contains(bmap2
, bmap1
->sample
) > 0)
3726 sample
= isl_vec_copy(bmap1
->sample
);
3727 else if (bmap2
->sample
&&
3728 isl_basic_map_contains(bmap1
, bmap2
->sample
) > 0 &&
3729 isl_basic_map_contains(bmap2
, bmap2
->sample
) > 0)
3730 sample
= isl_vec_copy(bmap2
->sample
);
3732 bmap1
= isl_basic_map_cow(bmap1
);
3735 bmap1
= isl_basic_map_extend(bmap1
,
3736 bmap2
->n_div
, bmap2
->n_eq
, bmap2
->n_ineq
);
3737 bmap1
= add_constraints(bmap1
, bmap2
, 0, 0);
3740 isl_vec_free(sample
);
3742 isl_vec_free(bmap1
->sample
);
3743 bmap1
->sample
= sample
;
3746 bmap1
= isl_basic_map_simplify(bmap1
);
3747 return isl_basic_map_finalize(bmap1
);
3750 isl_vec_free(sample
);
3751 isl_basic_map_free(bmap1
);
3752 isl_basic_map_free(bmap2
);
3756 __isl_give isl_basic_set
*isl_basic_set_intersect(
3757 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
3759 return bset_from_bmap(isl_basic_map_intersect(bset_to_bmap(bset1
),
3760 bset_to_bmap(bset2
)));
3763 __isl_give isl_basic_set
*isl_basic_set_intersect_params(
3764 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
3766 return isl_basic_set_intersect(bset1
, bset2
);
3769 /* Special case of isl_map_intersect, where both map1 and map2
3770 * are convex, without any divs and such that either map1 or map2
3771 * contains a single constraint. This constraint is then simply
3772 * added to the other map.
3774 static __isl_give isl_map
*map_intersect_add_constraint(
3775 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
3777 isl_assert(map1
->ctx
, map1
->n
== 1, goto error
);
3778 isl_assert(map2
->ctx
, map1
->n
== 1, goto error
);
3779 isl_assert(map1
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
3780 isl_assert(map2
->ctx
, map1
->p
[0]->n_div
== 0, goto error
);
3782 if (map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
!= 1)
3783 return isl_map_intersect(map2
, map1
);
3785 map1
= isl_map_cow(map1
);
3788 if (isl_map_plain_is_empty(map1
)) {
3792 if (map2
->p
[0]->n_eq
== 1)
3793 map1
->p
[0] = isl_basic_map_add_eq(map1
->p
[0], map2
->p
[0]->eq
[0]);
3795 map1
->p
[0] = isl_basic_map_add_ineq(map1
->p
[0],
3796 map2
->p
[0]->ineq
[0]);
3798 map1
->p
[0] = isl_basic_map_simplify(map1
->p
[0]);
3799 map1
->p
[0] = isl_basic_map_finalize(map1
->p
[0]);
3803 if (isl_basic_map_plain_is_empty(map1
->p
[0])) {
3804 isl_basic_map_free(map1
->p
[0]);
3810 map1
= isl_map_unmark_normalized(map1
);
3818 /* map2 may be either a parameter domain or a map living in the same
3821 static __isl_give isl_map
*map_intersect_internal(__isl_take isl_map
*map1
,
3822 __isl_take isl_map
*map2
)
3828 isl_size dim2
, nparam2
;
3833 if ((isl_map_plain_is_empty(map1
) ||
3834 isl_map_plain_is_universe(map2
)) &&
3835 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
3839 if ((isl_map_plain_is_empty(map2
) ||
3840 isl_map_plain_is_universe(map1
)) &&
3841 isl_space_is_equal(map1
->dim
, map2
->dim
)) {
3846 if (map1
->n
== 1 && map2
->n
== 1 &&
3847 map1
->p
[0]->n_div
== 0 && map2
->p
[0]->n_div
== 0 &&
3848 isl_space_is_equal(map1
->dim
, map2
->dim
) &&
3849 (map1
->p
[0]->n_eq
+ map1
->p
[0]->n_ineq
== 1 ||
3850 map2
->p
[0]->n_eq
+ map2
->p
[0]->n_ineq
== 1))
3851 return map_intersect_add_constraint(map1
, map2
);
3853 equal
= isl_map_plain_is_equal(map1
, map2
);
3861 dim2
= isl_map_dim(map2
, isl_dim_all
);
3862 nparam2
= isl_map_dim(map2
, isl_dim_param
);
3863 if (dim2
< 0 || nparam2
< 0)
3865 if (dim2
!= nparam2
)
3866 isl_assert(map1
->ctx
,
3867 isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
3869 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
3870 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
3871 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
3873 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
3874 map1
->n
* map2
->n
, flags
);
3877 for (i
= 0; i
< map1
->n
; ++i
)
3878 for (j
= 0; j
< map2
->n
; ++j
) {
3879 struct isl_basic_map
*part
;
3880 part
= isl_basic_map_intersect(
3881 isl_basic_map_copy(map1
->p
[i
]),
3882 isl_basic_map_copy(map2
->p
[j
]));
3883 if (isl_basic_map_is_empty(part
) < 0)
3884 part
= isl_basic_map_free(part
);
3885 result
= isl_map_add_basic_map(result
, part
);
3898 static __isl_give isl_map
*map_intersect(__isl_take isl_map
*map1
,
3899 __isl_take isl_map
*map2
)
3901 if (isl_map_check_equal_space(map1
, map2
) < 0)
3903 return map_intersect_internal(map1
, map2
);
3910 __isl_give isl_map
*isl_map_intersect(__isl_take isl_map
*map1
,
3911 __isl_take isl_map
*map2
)
3913 isl_map_align_params_bin(&map1
, &map2
);
3914 return map_intersect(map1
, map2
);
3917 __isl_give isl_set
*isl_set_intersect(__isl_take isl_set
*set1
,
3918 __isl_take isl_set
*set2
)
3920 return set_from_map(isl_map_intersect(set_to_map(set1
),
3924 /* map_intersect_internal accepts intersections
3925 * with parameter domains, so we can just call that function.
3927 __isl_give isl_map
*isl_map_intersect_params(__isl_take isl_map
*map
,
3928 __isl_take isl_set
*params
)
3930 isl_map_align_params_set(&map
, ¶ms
);
3931 return map_intersect_internal(map
, params
);
3934 __isl_give isl_set
*isl_set_intersect_params(__isl_take isl_set
*set
,
3935 __isl_take isl_set
*params
)
3937 return isl_map_intersect_params(set
, params
);
3940 __isl_give isl_basic_map
*isl_basic_map_reverse(__isl_take isl_basic_map
*bmap
)
3948 bmap
= isl_basic_map_cow(bmap
);
3951 space
= isl_space_reverse(isl_space_copy(bmap
->dim
));
3952 pos
= isl_basic_map_offset(bmap
, isl_dim_in
);
3953 n1
= isl_basic_map_dim(bmap
, isl_dim_in
);
3954 n2
= isl_basic_map_dim(bmap
, isl_dim_out
);
3955 if (n1
< 0 || n2
< 0)
3956 bmap
= isl_basic_map_free(bmap
);
3957 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
3958 return isl_basic_map_reset_space(bmap
, space
);
3961 /* Given a basic map A -> (B -> C), return the corresponding basic map
3964 static __isl_give isl_basic_map
*isl_basic_map_range_reverse(
3965 __isl_take isl_basic_map
*bmap
)
3968 isl_size offset
, n1
, n2
;
3970 space
= isl_basic_map_peek_space(bmap
);
3971 if (isl_space_check_range_is_wrapping(space
) < 0)
3972 return isl_basic_map_free(bmap
);
3973 offset
= isl_basic_map_var_offset(bmap
, isl_dim_out
);
3974 n1
= isl_space_wrapped_dim(space
, isl_dim_out
, isl_dim_in
);
3975 n2
= isl_space_wrapped_dim(space
, isl_dim_out
, isl_dim_out
);
3976 if (offset
< 0 || n1
< 0 || n2
< 0)
3977 return isl_basic_map_free(bmap
);
3979 bmap
= isl_basic_map_swap_vars(bmap
, 1 + offset
, n1
, n2
);
3981 space
= isl_basic_map_take_space(bmap
);
3982 space
= isl_space_range_reverse(space
);
3983 bmap
= isl_basic_map_restore_space(bmap
, space
);
3988 static __isl_give isl_basic_map
*basic_map_space_reset(
3989 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
)
3995 if (!isl_space_is_named_or_nested(bmap
->dim
, type
))
3998 space
= isl_basic_map_get_space(bmap
);
3999 space
= isl_space_reset(space
, type
);
4000 bmap
= isl_basic_map_reset_space(bmap
, space
);
4004 __isl_give isl_basic_map
*isl_basic_map_insert_dims(
4005 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
4006 unsigned pos
, unsigned n
)
4008 isl_bool rational
, is_empty
;
4009 isl_space
*res_space
;
4010 struct isl_basic_map
*res
;
4011 struct isl_dim_map
*dim_map
;
4014 enum isl_dim_type t
;
4017 return basic_map_space_reset(bmap
, type
);
4019 is_empty
= isl_basic_map_plain_is_empty(bmap
);
4020 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4021 if (is_empty
< 0 || total
< 0)
4022 return isl_basic_map_free(bmap
);
4023 res_space
= isl_space_insert_dims(isl_basic_map_get_space(bmap
),
4026 return isl_basic_map_free(bmap
);
4028 isl_basic_map_free(bmap
);
4029 return isl_basic_map_empty(res_space
);
4032 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
+ n
);
4034 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
4038 isl_dim_map_dim(dim_map
, bmap
->dim
, t
, off
);
4040 isl_size size
= isl_basic_map_dim(bmap
, t
);
4042 dim_map
= isl_dim_map_free(dim_map
);
4043 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
4045 isl_dim_map_dim_range(dim_map
, bmap
->dim
, t
,
4046 pos
, size
- pos
, off
+ pos
+ n
);
4048 dim
= isl_space_dim(res_space
, t
);
4050 dim_map
= isl_dim_map_free(dim_map
);
4053 isl_dim_map_div(dim_map
, bmap
, off
);
4055 res
= isl_basic_map_alloc_space(res_space
,
4056 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
4057 rational
= isl_basic_map_is_rational(bmap
);
4059 res
= isl_basic_map_free(res
);
4061 res
= isl_basic_map_set_rational(res
);
4062 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
4063 return isl_basic_map_finalize(res
);
4066 __isl_give isl_basic_set
*isl_basic_set_insert_dims(
4067 __isl_take isl_basic_set
*bset
,
4068 enum isl_dim_type type
, unsigned pos
, unsigned n
)
4070 return isl_basic_map_insert_dims(bset
, type
, pos
, n
);
4073 __isl_give isl_basic_map
*isl_basic_map_add_dims(__isl_take isl_basic_map
*bmap
,
4074 enum isl_dim_type type
, unsigned n
)
4078 dim
= isl_basic_map_dim(bmap
, type
);
4080 return isl_basic_map_free(bmap
);
4081 return isl_basic_map_insert_dims(bmap
, type
, dim
, n
);
4084 __isl_give isl_basic_set
*isl_basic_set_add_dims(__isl_take isl_basic_set
*bset
,
4085 enum isl_dim_type type
, unsigned n
)
4089 isl_assert(bset
->ctx
, type
!= isl_dim_in
, goto error
);
4090 return isl_basic_map_add_dims(bset
, type
, n
);
4092 isl_basic_set_free(bset
);
4096 static __isl_give isl_map
*map_space_reset(__isl_take isl_map
*map
,
4097 enum isl_dim_type type
)
4101 if (!map
|| !isl_space_is_named_or_nested(map
->dim
, type
))
4104 space
= isl_map_get_space(map
);
4105 space
= isl_space_reset(space
, type
);
4106 map
= isl_map_reset_space(map
, space
);
4110 __isl_give isl_map
*isl_map_insert_dims(__isl_take isl_map
*map
,
4111 enum isl_dim_type type
, unsigned pos
, unsigned n
)
4117 return map_space_reset(map
, type
);
4119 map
= isl_map_cow(map
);
4123 for (i
= 0; i
< map
->n
; ++i
) {
4124 map
->p
[i
] = isl_basic_map_insert_dims(map
->p
[i
], type
, pos
, n
);
4129 space
= isl_map_take_space(map
);
4130 space
= isl_space_insert_dims(space
, type
, pos
, n
);
4131 map
= isl_map_restore_space(map
, space
);
4139 __isl_give isl_set
*isl_set_insert_dims(__isl_take isl_set
*set
,
4140 enum isl_dim_type type
, unsigned pos
, unsigned n
)
4142 return isl_map_insert_dims(set
, type
, pos
, n
);
4145 __isl_give isl_map
*isl_map_add_dims(__isl_take isl_map
*map
,
4146 enum isl_dim_type type
, unsigned n
)
4150 dim
= isl_map_dim(map
, type
);
4152 return isl_map_free(map
);
4153 return isl_map_insert_dims(map
, type
, dim
, n
);
4156 __isl_give isl_set
*isl_set_add_dims(__isl_take isl_set
*set
,
4157 enum isl_dim_type type
, unsigned n
)
4161 isl_assert(set
->ctx
, type
!= isl_dim_in
, goto error
);
4162 return set_from_map(isl_map_add_dims(set_to_map(set
), type
, n
));
4168 __isl_give isl_basic_map
*isl_basic_map_move_dims(
4169 __isl_take isl_basic_map
*bmap
,
4170 enum isl_dim_type dst_type
, unsigned dst_pos
,
4171 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
4174 struct isl_dim_map
*dim_map
;
4175 struct isl_basic_map
*res
;
4176 enum isl_dim_type t
;
4183 bmap
= isl_basic_map_reset(bmap
, src_type
);
4184 bmap
= isl_basic_map_reset(bmap
, dst_type
);
4188 if (isl_basic_map_check_range(bmap
, src_type
, src_pos
, n
) < 0)
4189 return isl_basic_map_free(bmap
);
4191 if (dst_type
== src_type
&& dst_pos
== src_pos
)
4194 isl_assert(bmap
->ctx
, dst_type
!= src_type
, goto error
);
4196 if (pos(bmap
->dim
, dst_type
) + dst_pos
==
4197 pos(bmap
->dim
, src_type
) + src_pos
+
4198 ((src_type
< dst_type
) ? n
: 0)) {
4199 space
= isl_basic_map_take_space(bmap
);
4200 space
= isl_space_move_dims(space
, dst_type
, dst_pos
,
4201 src_type
, src_pos
, n
);
4202 bmap
= isl_basic_map_restore_space(bmap
, space
);
4203 bmap
= isl_basic_map_finalize(bmap
);
4208 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4210 return isl_basic_map_free(bmap
);
4211 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
4214 space
= isl_basic_map_peek_space(bmap
);
4215 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
4216 isl_size size
= isl_space_dim(space
, t
);
4218 dim_map
= isl_dim_map_free(dim_map
);
4219 if (t
== dst_type
) {
4220 isl_dim_map_dim_range(dim_map
, space
, t
,
4223 isl_dim_map_dim_range(dim_map
, space
, src_type
,
4226 isl_dim_map_dim_range(dim_map
, space
, t
,
4227 dst_pos
, size
- dst_pos
, off
);
4228 off
+= size
- dst_pos
;
4229 } else if (t
== src_type
) {
4230 isl_dim_map_dim_range(dim_map
, space
, t
,
4233 isl_dim_map_dim_range(dim_map
, space
, t
,
4234 src_pos
+ n
, size
- src_pos
- n
, off
);
4235 off
+= size
- src_pos
- n
;
4237 isl_dim_map_dim(dim_map
, space
, t
, off
);
4241 isl_dim_map_div(dim_map
, bmap
, off
);
4243 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
4244 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
4245 bmap
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
4246 space
= isl_basic_map_take_space(bmap
);
4247 space
= isl_space_move_dims(space
, dst_type
, dst_pos
,
4248 src_type
, src_pos
, n
);
4249 bmap
= isl_basic_map_restore_space(bmap
, space
);
4253 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
4254 bmap
= isl_basic_map_gauss(bmap
, NULL
);
4255 bmap
= isl_basic_map_finalize(bmap
);
4259 isl_basic_map_free(bmap
);
4263 __isl_give isl_basic_set
*isl_basic_set_move_dims(__isl_take isl_basic_set
*bset
,
4264 enum isl_dim_type dst_type
, unsigned dst_pos
,
4265 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
4267 isl_basic_map
*bmap
= bset_to_bmap(bset
);
4268 bmap
= isl_basic_map_move_dims(bmap
, dst_type
, dst_pos
,
4269 src_type
, src_pos
, n
);
4270 return bset_from_bmap(bmap
);
4273 __isl_give isl_set
*isl_set_move_dims(__isl_take isl_set
*set
,
4274 enum isl_dim_type dst_type
, unsigned dst_pos
,
4275 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
4279 isl_assert(set
->ctx
, dst_type
!= isl_dim_in
, goto error
);
4280 return set_from_map(isl_map_move_dims(set_to_map(set
),
4281 dst_type
, dst_pos
, src_type
, src_pos
, n
));
4287 __isl_give isl_map
*isl_map_move_dims(__isl_take isl_map
*map
,
4288 enum isl_dim_type dst_type
, unsigned dst_pos
,
4289 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
4295 map
= isl_map_reset(map
, src_type
);
4296 map
= isl_map_reset(map
, dst_type
);
4300 if (isl_map_check_range(map
, src_type
, src_pos
, n
))
4301 return isl_map_free(map
);
4303 if (dst_type
== src_type
&& dst_pos
== src_pos
)
4306 isl_assert(map
->ctx
, dst_type
!= src_type
, goto error
);
4308 map
= isl_map_cow(map
);
4312 for (i
= 0; i
< map
->n
; ++i
) {
4313 map
->p
[i
] = isl_basic_map_move_dims(map
->p
[i
],
4315 src_type
, src_pos
, n
);
4320 space
= isl_map_take_space(map
);
4321 space
= isl_space_move_dims(space
, dst_type
, dst_pos
,
4322 src_type
, src_pos
, n
);
4323 map
= isl_map_restore_space(map
, space
);
4331 /* Move the specified dimensions to the last columns right before
4332 * the divs. Don't change the dimension specification of bmap.
4333 * That's the responsibility of the caller.
4335 static __isl_give isl_basic_map
*move_last(__isl_take isl_basic_map
*bmap
,
4336 enum isl_dim_type type
, unsigned first
, unsigned n
)
4339 struct isl_dim_map
*dim_map
;
4340 struct isl_basic_map
*res
;
4341 enum isl_dim_type t
;
4347 if (isl_basic_map_offset(bmap
, type
) + first
+ n
==
4348 isl_basic_map_offset(bmap
, isl_dim_div
))
4351 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4353 return isl_basic_map_free(bmap
);
4354 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
4357 space
= isl_basic_map_peek_space(bmap
);
4358 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
4359 isl_size size
= isl_space_dim(space
, t
);
4361 dim_map
= isl_dim_map_free(dim_map
);
4363 isl_dim_map_dim_range(dim_map
, space
, t
,
4366 isl_dim_map_dim_range(dim_map
, space
, t
,
4367 first
, n
, total
- bmap
->n_div
- n
);
4368 isl_dim_map_dim_range(dim_map
, space
, t
,
4369 first
+ n
, size
- (first
+ n
), off
);
4370 off
+= size
- (first
+ n
);
4372 isl_dim_map_dim(dim_map
, space
, t
, off
);
4376 isl_dim_map_div(dim_map
, bmap
, off
+ n
);
4378 res
= isl_basic_map_alloc_space(isl_basic_map_get_space(bmap
),
4379 bmap
->n_div
, bmap
->n_eq
, bmap
->n_ineq
);
4380 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
4384 /* Insert "n" rows in the divs of "bmap".
4386 * The number of columns is not changed, which means that the last
4387 * dimensions of "bmap" are being reintepreted as the new divs.
4388 * The space of "bmap" is not adjusted, however, which means
4389 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
4390 * from the space of "bmap" is the responsibility of the caller.
4392 static __isl_give isl_basic_map
*insert_div_rows(__isl_take isl_basic_map
*bmap
,
4400 bmap
= isl_basic_map_cow(bmap
);
4404 row_size
= isl_basic_map_offset(bmap
, isl_dim_div
) + bmap
->extra
;
4405 old
= bmap
->block2
.data
;
4406 bmap
->block2
= isl_blk_extend(bmap
->ctx
, bmap
->block2
,
4407 (bmap
->extra
+ n
) * (1 + row_size
));
4408 if (!bmap
->block2
.data
)
4409 return isl_basic_map_free(bmap
);
4410 new_div
= isl_alloc_array(bmap
->ctx
, isl_int
*, bmap
->extra
+ n
);
4412 return isl_basic_map_free(bmap
);
4413 for (i
= 0; i
< n
; ++i
) {
4414 new_div
[i
] = bmap
->block2
.data
+
4415 (bmap
->extra
+ i
) * (1 + row_size
);
4416 isl_seq_clr(new_div
[i
], 1 + row_size
);
4418 for (i
= 0; i
< bmap
->extra
; ++i
)
4419 new_div
[n
+ i
] = bmap
->block2
.data
+ (bmap
->div
[i
] - old
);
4421 bmap
->div
= new_div
;
4428 /* Drop constraints from "bmap" that only involve the variables
4429 * of "type" in the range [first, first + n] that are not related
4430 * to any of the variables outside that interval.
4431 * These constraints cannot influence the values for the variables
4432 * outside the interval, except in case they cause "bmap" to be empty.
4433 * Only drop the constraints if "bmap" is known to be non-empty.
4435 static __isl_give isl_basic_map
*drop_irrelevant_constraints(
4436 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
4437 unsigned first
, unsigned n
)
4441 isl_size dim
, n_div
;
4444 non_empty
= isl_basic_map_plain_is_non_empty(bmap
);
4446 return isl_basic_map_free(bmap
);
4450 dim
= isl_basic_map_dim(bmap
, isl_dim_all
);
4451 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4452 if (dim
< 0 || n_div
< 0)
4453 return isl_basic_map_free(bmap
);
4454 groups
= isl_calloc_array(isl_basic_map_get_ctx(bmap
), int, dim
);
4456 return isl_basic_map_free(bmap
);
4457 first
+= isl_basic_map_offset(bmap
, type
) - 1;
4458 for (i
= 0; i
< first
; ++i
)
4460 for (i
= first
+ n
; i
< dim
- n_div
; ++i
)
4463 bmap
= isl_basic_map_drop_unrelated_constraints(bmap
, groups
);
4468 /* Turn the n dimensions of type type, starting at first
4469 * into existentially quantified variables.
4471 * If a subset of the projected out variables are unrelated
4472 * to any of the variables that remain, then the constraints
4473 * involving this subset are simply dropped first.
4475 __isl_give isl_basic_map
*isl_basic_map_project_out(
4476 __isl_take isl_basic_map
*bmap
,
4477 enum isl_dim_type type
, unsigned first
, unsigned n
)
4483 return basic_map_space_reset(bmap
, type
);
4484 if (type
== isl_dim_div
)
4485 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
4486 "cannot project out existentially quantified variables",
4487 return isl_basic_map_free(bmap
));
4489 empty
= isl_basic_map_plain_is_empty(bmap
);
4491 return isl_basic_map_free(bmap
);
4493 bmap
= isl_basic_map_set_to_empty(bmap
);
4495 bmap
= drop_irrelevant_constraints(bmap
, type
, first
, n
);
4499 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
4500 return isl_basic_map_remove_dims(bmap
, type
, first
, n
);
4502 if (isl_basic_map_check_range(bmap
, type
, first
, n
) < 0)
4503 return isl_basic_map_free(bmap
);
4505 bmap
= move_last(bmap
, type
, first
, n
);
4506 bmap
= isl_basic_map_cow(bmap
);
4507 bmap
= insert_div_rows(bmap
, n
);
4509 space
= isl_basic_map_take_space(bmap
);
4510 space
= isl_space_drop_dims(space
, type
, first
, n
);
4511 bmap
= isl_basic_map_restore_space(bmap
, space
);
4512 bmap
= isl_basic_map_simplify(bmap
);
4513 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
4514 return isl_basic_map_finalize(bmap
);
4517 /* Turn the n dimensions of type type, starting at first
4518 * into existentially quantified variables.
4520 __isl_give isl_basic_set
*isl_basic_set_project_out(
4521 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
,
4522 unsigned first
, unsigned n
)
4524 return bset_from_bmap(isl_basic_map_project_out(bset_to_bmap(bset
),
4528 /* Turn the n dimensions of type type, starting at first
4529 * into existentially quantified variables.
4531 __isl_give isl_map
*isl_map_project_out(__isl_take isl_map
*map
,
4532 enum isl_dim_type type
, unsigned first
, unsigned n
)
4538 return map_space_reset(map
, type
);
4540 if (isl_map_check_range(map
, type
, first
, n
) < 0)
4541 return isl_map_free(map
);
4543 map
= isl_map_cow(map
);
4547 for (i
= 0; i
< map
->n
; ++i
) {
4548 map
->p
[i
] = isl_basic_map_project_out(map
->p
[i
], type
, first
, n
);
4554 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
4555 map
= isl_map_unmark_normalized(map
);
4557 space
= isl_map_take_space(map
);
4558 space
= isl_space_drop_dims(space
, type
, first
, n
);
4559 map
= isl_map_restore_space(map
, space
);
4568 #define TYPE isl_map
4569 #include "isl_project_out_all_params_templ.c"
4571 /* Turn all the dimensions of type "type", except the "n" starting at "first"
4572 * into existentially quantified variables.
4574 __isl_give isl_map
*isl_map_project_onto(__isl_take isl_map
*map
,
4575 enum isl_dim_type type
, unsigned first
, unsigned n
)
4579 dim
= isl_map_dim(map
, type
);
4580 if (isl_map_check_range(map
, type
, first
, n
) < 0 || dim
< 0)
4581 return isl_map_free(map
);
4582 map
= isl_map_project_out(map
, type
, first
+ n
, dim
- (first
+ n
));
4583 map
= isl_map_project_out(map
, type
, 0, first
);
4587 /* Turn the n dimensions of type type, starting at first
4588 * into existentially quantified variables.
4590 __isl_give isl_set
*isl_set_project_out(__isl_take isl_set
*set
,
4591 enum isl_dim_type type
, unsigned first
, unsigned n
)
4593 return set_from_map(isl_map_project_out(set_to_map(set
),
4597 /* If "set" involves a parameter with identifier "id",
4598 * then turn it into an existentially quantified variable.
4600 __isl_give isl_set
*isl_set_project_out_param_id(__isl_take isl_set
*set
,
4601 __isl_take isl_id
*id
)
4607 pos
= isl_set_find_dim_by_id(set
, isl_dim_param
, id
);
4611 return isl_set_project_out(set
, isl_dim_param
, pos
, 1);
4618 /* If "set" involves any of the parameters with identifiers in "list",
4619 * then turn them into existentially quantified variables.
4621 __isl_give isl_set
*isl_set_project_out_param_id_list(__isl_take isl_set
*set
,
4622 __isl_take isl_id_list
*list
)
4627 n
= isl_id_list_size(list
);
4630 for (i
= 0; i
< n
; ++i
) {
4633 id
= isl_id_list_get_at(list
, i
);
4634 set
= isl_set_project_out_param_id(set
, id
);
4637 isl_id_list_free(list
);
4640 isl_id_list_free(list
);
4645 /* Project out all parameters from "set" by existentially quantifying
4648 __isl_give isl_set
*isl_set_project_out_all_params(__isl_take isl_set
*set
)
4650 return set_from_map(isl_map_project_out_all_params(set_to_map(set
)));
4653 /* Return a map that projects the elements in "set" onto their
4654 * "n" set dimensions starting at "first".
4655 * "type" should be equal to isl_dim_set.
4657 __isl_give isl_map
*isl_set_project_onto_map(__isl_take isl_set
*set
,
4658 enum isl_dim_type type
, unsigned first
, unsigned n
)
4663 if (type
!= isl_dim_set
)
4664 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
4665 "only set dimensions can be projected out", goto error
);
4666 if (isl_set_check_range(set
, type
, first
, n
) < 0)
4667 return isl_set_free(set
);
4669 map
= isl_map_from_domain(set
);
4670 map
= isl_map_add_dims(map
, isl_dim_out
, n
);
4671 for (i
= 0; i
< n
; ++i
)
4672 map
= isl_map_equate(map
, isl_dim_in
, first
+ i
,
4680 static __isl_give isl_basic_map
*add_divs(__isl_take isl_basic_map
*bmap
,
4686 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4688 return isl_basic_map_free(bmap
);
4689 for (i
= 0; i
< n
; ++i
) {
4690 j
= isl_basic_map_alloc_div(bmap
);
4693 isl_seq_clr(bmap
->div
[j
], 1 + 1 + total
);
4697 isl_basic_map_free(bmap
);
4701 /* Does "bmap2" apply to the range of "bmap1" (ignoring parameters)?
4703 isl_bool
isl_basic_map_applies_range(__isl_keep isl_basic_map
*bmap1
,
4704 __isl_keep isl_basic_map
*bmap2
)
4706 isl_space
*space1
, *space2
;
4708 space1
= isl_basic_map_peek_space(bmap1
);
4709 space2
= isl_basic_map_peek_space(bmap2
);
4710 return isl_space_tuple_is_equal(space1
, isl_dim_out
,
4711 space2
, isl_dim_in
);
4714 /* Check that "bmap2" applies to the range of "bmap1" (ignoring parameters).
4716 static isl_stat
isl_basic_map_check_applies_range(
4717 __isl_keep isl_basic_map
*bmap1
, __isl_keep isl_basic_map
*bmap2
)
4721 equal
= isl_basic_map_applies_range(bmap1
, bmap2
);
4723 return isl_stat_error
;
4725 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
4726 "spaces don't match", return isl_stat_error
);
4730 __isl_give isl_basic_map
*isl_basic_map_apply_range(
4731 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
4733 isl_space
*space_result
= NULL
;
4734 struct isl_basic_map
*bmap
;
4735 isl_size n_in
, n_out
, n
, nparam
;
4736 unsigned total
, pos
;
4737 struct isl_dim_map
*dim_map1
, *dim_map2
;
4739 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
4741 if (isl_basic_map_check_applies_range(bmap1
, bmap2
) < 0)
4744 n_in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
4745 n_out
= isl_basic_map_dim(bmap2
, isl_dim_out
);
4746 n
= isl_basic_map_dim(bmap1
, isl_dim_out
);
4747 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
4748 if (n_in
< 0 || n_out
< 0 || n
< 0 || nparam
< 0)
4751 space_result
= isl_space_join(isl_basic_map_get_space(bmap1
),
4752 isl_basic_map_get_space(bmap2
));
4754 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ n
;
4755 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4756 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4757 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
4758 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
4759 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
4760 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_in
);
4761 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_out
);
4762 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
4763 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
4764 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
4766 bmap
= isl_basic_map_alloc_space(space_result
,
4767 bmap1
->n_div
+ bmap2
->n_div
+ n
,
4768 bmap1
->n_eq
+ bmap2
->n_eq
,
4769 bmap1
->n_ineq
+ bmap2
->n_ineq
);
4770 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
4771 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
4772 bmap
= add_divs(bmap
, n
);
4773 bmap
= isl_basic_map_simplify(bmap
);
4774 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
4775 return isl_basic_map_finalize(bmap
);
4777 isl_basic_map_free(bmap1
);
4778 isl_basic_map_free(bmap2
);
4782 __isl_give isl_basic_set
*isl_basic_set_apply(__isl_take isl_basic_set
*bset
,
4783 __isl_take isl_basic_map
*bmap
)
4785 if (isl_basic_map_check_compatible_domain(bmap
, bset
) < 0)
4788 return bset_from_bmap(isl_basic_map_apply_range(bset_to_bmap(bset
),
4791 isl_basic_set_free(bset
);
4792 isl_basic_map_free(bmap
);
4796 __isl_give isl_basic_map
*isl_basic_map_apply_domain(
4797 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
4799 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
4801 if (!isl_space_tuple_is_equal(bmap1
->dim
, isl_dim_in
,
4802 bmap2
->dim
, isl_dim_in
))
4803 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
4804 "spaces don't match", goto error
);
4806 bmap1
= isl_basic_map_reverse(bmap1
);
4807 bmap1
= isl_basic_map_apply_range(bmap1
, bmap2
);
4808 return isl_basic_map_reverse(bmap1
);
4810 isl_basic_map_free(bmap1
);
4811 isl_basic_map_free(bmap2
);
4815 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
4816 * A \cap B -> f(A) + f(B)
4818 __isl_give isl_basic_map
*isl_basic_map_sum(__isl_take isl_basic_map
*bmap1
,
4819 __isl_take isl_basic_map
*bmap2
)
4821 isl_size n_in
, n_out
, nparam
;
4822 unsigned total
, pos
;
4823 struct isl_basic_map
*bmap
= NULL
;
4824 struct isl_dim_map
*dim_map1
, *dim_map2
;
4827 if (isl_basic_map_check_equal_space(bmap1
, bmap2
) < 0)
4830 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
4831 n_in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
4832 n_out
= isl_basic_map_dim(bmap1
, isl_dim_out
);
4833 if (nparam
< 0 || n_in
< 0 || n_out
< 0)
4836 total
= nparam
+ n_in
+ n_out
+ bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
;
4837 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
4838 dim_map2
= isl_dim_map_alloc(bmap2
->ctx
, total
);
4839 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
4840 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
);
4841 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
4842 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
4843 isl_dim_map_div(dim_map1
, bmap1
, pos
+= n_in
+ n_out
);
4844 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
4845 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= bmap2
->n_div
);
4846 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= n_out
);
4848 bmap
= isl_basic_map_alloc_space(isl_space_copy(bmap1
->dim
),
4849 bmap1
->n_div
+ bmap2
->n_div
+ 2 * n_out
,
4850 bmap1
->n_eq
+ bmap2
->n_eq
+ n_out
,
4851 bmap1
->n_ineq
+ bmap2
->n_ineq
);
4852 for (i
= 0; i
< n_out
; ++i
) {
4853 int j
= isl_basic_map_alloc_equality(bmap
);
4856 isl_seq_clr(bmap
->eq
[j
], 1+total
);
4857 isl_int_set_si(bmap
->eq
[j
][1+nparam
+n_in
+i
], -1);
4858 isl_int_set_si(bmap
->eq
[j
][1+pos
+i
], 1);
4859 isl_int_set_si(bmap
->eq
[j
][1+pos
-n_out
+i
], 1);
4861 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
4862 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
4863 bmap
= add_divs(bmap
, 2 * n_out
);
4865 bmap
= isl_basic_map_simplify(bmap
);
4866 return isl_basic_map_finalize(bmap
);
4868 isl_basic_map_free(bmap
);
4869 isl_basic_map_free(bmap1
);
4870 isl_basic_map_free(bmap2
);
4874 /* Given two maps A -> f(A) and B -> g(B), construct a map
4875 * A \cap B -> f(A) + f(B)
4877 __isl_give isl_map
*isl_map_sum(__isl_take isl_map
*map1
,
4878 __isl_take isl_map
*map2
)
4880 struct isl_map
*result
;
4883 if (isl_map_check_equal_space(map1
, map2
) < 0)
4886 result
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
4887 map1
->n
* map2
->n
, 0);
4890 for (i
= 0; i
< map1
->n
; ++i
)
4891 for (j
= 0; j
< map2
->n
; ++j
) {
4892 struct isl_basic_map
*part
;
4893 part
= isl_basic_map_sum(
4894 isl_basic_map_copy(map1
->p
[i
]),
4895 isl_basic_map_copy(map2
->p
[j
]));
4896 if (isl_basic_map_is_empty(part
))
4897 isl_basic_map_free(part
);
4899 result
= isl_map_add_basic_map(result
, part
);
4912 __isl_give isl_set
*isl_set_sum(__isl_take isl_set
*set1
,
4913 __isl_take isl_set
*set2
)
4915 return set_from_map(isl_map_sum(set_to_map(set1
), set_to_map(set2
)));
4918 /* Given a basic map A -> f(A), construct A -> -f(A).
4920 __isl_give isl_basic_map
*isl_basic_map_neg(__isl_take isl_basic_map
*bmap
)
4926 bmap
= isl_basic_map_cow(bmap
);
4927 n
= isl_basic_map_dim(bmap
, isl_dim_out
);
4929 return isl_basic_map_free(bmap
);
4931 off
= isl_basic_map_offset(bmap
, isl_dim_out
);
4932 for (i
= 0; i
< bmap
->n_eq
; ++i
)
4933 for (j
= 0; j
< n
; ++j
)
4934 isl_int_neg(bmap
->eq
[i
][off
+j
], bmap
->eq
[i
][off
+j
]);
4935 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
4936 for (j
= 0; j
< n
; ++j
)
4937 isl_int_neg(bmap
->ineq
[i
][off
+j
], bmap
->ineq
[i
][off
+j
]);
4938 for (i
= 0; i
< bmap
->n_div
; ++i
)
4939 for (j
= 0; j
< n
; ++j
)
4940 isl_int_neg(bmap
->div
[i
][1+off
+j
], bmap
->div
[i
][1+off
+j
]);
4941 bmap
= isl_basic_map_gauss(bmap
, NULL
);
4942 return isl_basic_map_finalize(bmap
);
4945 __isl_give isl_basic_set
*isl_basic_set_neg(__isl_take isl_basic_set
*bset
)
4947 return isl_basic_map_neg(bset
);
4950 /* Given a map A -> f(A), construct A -> -f(A).
4952 __isl_give isl_map
*isl_map_neg(__isl_take isl_map
*map
)
4956 map
= isl_map_cow(map
);
4960 for (i
= 0; i
< map
->n
; ++i
) {
4961 map
->p
[i
] = isl_basic_map_neg(map
->p
[i
]);
4972 __isl_give isl_set
*isl_set_neg(__isl_take isl_set
*set
)
4974 return set_from_map(isl_map_neg(set_to_map(set
)));
4977 /* Given a basic map A -> f(A) and an integer d, construct a basic map
4978 * A -> floor(f(A)/d).
4980 __isl_give isl_basic_map
*isl_basic_map_floordiv(__isl_take isl_basic_map
*bmap
,
4983 isl_size n_in
, n_out
, nparam
;
4984 unsigned total
, pos
;
4985 struct isl_basic_map
*result
= NULL
;
4986 struct isl_dim_map
*dim_map
;
4989 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
4990 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
4991 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4992 if (nparam
< 0 || n_in
< 0 || n_out
< 0)
4993 return isl_basic_map_free(bmap
);
4995 total
= nparam
+ n_in
+ n_out
+ bmap
->n_div
+ n_out
;
4996 dim_map
= isl_dim_map_alloc(bmap
->ctx
, total
);
4997 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_param
, pos
= 0);
4998 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_in
, pos
+= nparam
);
4999 isl_dim_map_div(dim_map
, bmap
, pos
+= n_in
+ n_out
);
5000 isl_dim_map_dim(dim_map
, bmap
->dim
, isl_dim_out
, pos
+= bmap
->n_div
);
5002 result
= isl_basic_map_alloc_space(isl_space_copy(bmap
->dim
),
5003 bmap
->n_div
+ n_out
,
5004 bmap
->n_eq
, bmap
->n_ineq
+ 2 * n_out
);
5005 result
= isl_basic_map_add_constraints_dim_map(result
, bmap
, dim_map
);
5006 result
= add_divs(result
, n_out
);
5007 for (i
= 0; i
< n_out
; ++i
) {
5009 j
= isl_basic_map_alloc_inequality(result
);
5012 isl_seq_clr(result
->ineq
[j
], 1+total
);
5013 isl_int_neg(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
5014 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], 1);
5015 j
= isl_basic_map_alloc_inequality(result
);
5018 isl_seq_clr(result
->ineq
[j
], 1+total
);
5019 isl_int_set(result
->ineq
[j
][1+nparam
+n_in
+i
], d
);
5020 isl_int_set_si(result
->ineq
[j
][1+pos
+i
], -1);
5021 isl_int_sub_ui(result
->ineq
[j
][0], d
, 1);
5024 result
= isl_basic_map_simplify(result
);
5025 return isl_basic_map_finalize(result
);
5027 isl_basic_map_free(result
);
5031 /* Given a map A -> f(A) and an integer d, construct a map
5032 * A -> floor(f(A)/d).
5034 __isl_give isl_map
*isl_map_floordiv(__isl_take isl_map
*map
, isl_int d
)
5038 map
= isl_map_cow(map
);
5042 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
5043 for (i
= 0; i
< map
->n
; ++i
) {
5044 map
->p
[i
] = isl_basic_map_floordiv(map
->p
[i
], d
);
5048 map
= isl_map_unmark_normalized(map
);
5056 /* Given a map A -> f(A) and an integer d, construct a map
5057 * A -> floor(f(A)/d).
5059 __isl_give isl_map
*isl_map_floordiv_val(__isl_take isl_map
*map
,
5060 __isl_take isl_val
*d
)
5064 if (!isl_val_is_int(d
))
5065 isl_die(isl_val_get_ctx(d
), isl_error_invalid
,
5066 "expecting integer denominator", goto error
);
5067 map
= isl_map_floordiv(map
, d
->n
);
5076 static __isl_give isl_basic_map
*var_equal(__isl_take isl_basic_map
*bmap
,
5084 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5085 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5086 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5087 if (total
< 0 || nparam
< 0 || n_in
< 0)
5088 return isl_basic_map_free(bmap
);
5089 i
= isl_basic_map_alloc_equality(bmap
);
5092 isl_seq_clr(bmap
->eq
[i
], 1 + total
);
5093 isl_int_set_si(bmap
->eq
[i
][1+nparam
+pos
], -1);
5094 isl_int_set_si(bmap
->eq
[i
][1+nparam
+n_in
+pos
], 1);
5095 return isl_basic_map_finalize(bmap
);
5097 isl_basic_map_free(bmap
);
5101 /* Add a constraint to "bmap" expressing i_pos < o_pos
5103 static __isl_give isl_basic_map
*var_less(__isl_take isl_basic_map
*bmap
,
5111 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5112 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5113 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5114 if (total
< 0 || nparam
< 0 || n_in
< 0)
5115 return isl_basic_map_free(bmap
);
5116 i
= isl_basic_map_alloc_inequality(bmap
);
5119 isl_seq_clr(bmap
->ineq
[i
], 1 + total
);
5120 isl_int_set_si(bmap
->ineq
[i
][0], -1);
5121 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
5122 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
5123 return isl_basic_map_finalize(bmap
);
5125 isl_basic_map_free(bmap
);
5129 /* Add a constraint to "bmap" expressing i_pos <= o_pos
5131 static __isl_give isl_basic_map
*var_less_or_equal(
5132 __isl_take isl_basic_map
*bmap
, unsigned pos
)
5139 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5140 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5141 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5142 if (total
< 0 || nparam
< 0 || n_in
< 0)
5143 return isl_basic_map_free(bmap
);
5144 i
= isl_basic_map_alloc_inequality(bmap
);
5147 isl_seq_clr(bmap
->ineq
[i
], 1 + total
);
5148 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], -1);
5149 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], 1);
5150 return isl_basic_map_finalize(bmap
);
5152 isl_basic_map_free(bmap
);
5156 /* Add a constraint to "bmap" expressing i_pos > o_pos
5158 static __isl_give isl_basic_map
*var_more(__isl_take isl_basic_map
*bmap
,
5166 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5167 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5168 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5169 if (total
< 0 || nparam
< 0 || n_in
< 0)
5170 return isl_basic_map_free(bmap
);
5171 i
= isl_basic_map_alloc_inequality(bmap
);
5174 isl_seq_clr(bmap
->ineq
[i
], 1 + total
);
5175 isl_int_set_si(bmap
->ineq
[i
][0], -1);
5176 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
5177 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
5178 return isl_basic_map_finalize(bmap
);
5180 isl_basic_map_free(bmap
);
5184 /* Add a constraint to "bmap" expressing i_pos >= o_pos
5186 static __isl_give isl_basic_map
*var_more_or_equal(
5187 __isl_take isl_basic_map
*bmap
, unsigned pos
)
5194 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5195 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
5196 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
5197 if (total
< 0 || nparam
< 0 || n_in
< 0)
5198 return isl_basic_map_free(bmap
);
5199 i
= isl_basic_map_alloc_inequality(bmap
);
5202 isl_seq_clr(bmap
->ineq
[i
], 1 + total
);
5203 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+pos
], 1);
5204 isl_int_set_si(bmap
->ineq
[i
][1+nparam
+n_in
+pos
], -1);
5205 return isl_basic_map_finalize(bmap
);
5207 isl_basic_map_free(bmap
);
5211 __isl_give isl_basic_map
*isl_basic_map_equal(
5212 __isl_take isl_space
*space
, unsigned n_equal
)
5215 struct isl_basic_map
*bmap
;
5216 bmap
= isl_basic_map_alloc_space(space
, 0, n_equal
, 0);
5219 for (i
= 0; i
< n_equal
&& bmap
; ++i
)
5220 bmap
= var_equal(bmap
, i
);
5221 return isl_basic_map_finalize(bmap
);
5224 /* Return a relation on of dimension "space" expressing i_[0..pos] << o_[0..pos]
5226 __isl_give isl_basic_map
*isl_basic_map_less_at(__isl_take isl_space
*space
,
5230 struct isl_basic_map
*bmap
;
5231 bmap
= isl_basic_map_alloc_space(space
, 0, pos
, 1);
5234 for (i
= 0; i
< pos
&& bmap
; ++i
)
5235 bmap
= var_equal(bmap
, i
);
5237 bmap
= var_less(bmap
, pos
);
5238 return isl_basic_map_finalize(bmap
);
5241 /* Return a relation on "space" expressing i_[0..pos] <<= o_[0..pos]
5243 __isl_give isl_basic_map
*isl_basic_map_less_or_equal_at(
5244 __isl_take isl_space
*space
, unsigned pos
)
5247 isl_basic_map
*bmap
;
5249 bmap
= isl_basic_map_alloc_space(space
, 0, pos
, 1);
5250 for (i
= 0; i
< pos
; ++i
)
5251 bmap
= var_equal(bmap
, i
);
5252 bmap
= var_less_or_equal(bmap
, pos
);
5253 return isl_basic_map_finalize(bmap
);
5256 /* Return a relation on "space" expressing i_pos > o_pos
5258 __isl_give isl_basic_map
*isl_basic_map_more_at(__isl_take isl_space
*space
,
5262 struct isl_basic_map
*bmap
;
5263 bmap
= isl_basic_map_alloc_space(space
, 0, pos
, 1);
5266 for (i
= 0; i
< pos
&& bmap
; ++i
)
5267 bmap
= var_equal(bmap
, i
);
5269 bmap
= var_more(bmap
, pos
);
5270 return isl_basic_map_finalize(bmap
);
5273 /* Return a relation on "space" expressing i_[0..pos] >>= o_[0..pos]
5275 __isl_give isl_basic_map
*isl_basic_map_more_or_equal_at(
5276 __isl_take isl_space
*space
, unsigned pos
)
5279 isl_basic_map
*bmap
;
5281 bmap
= isl_basic_map_alloc_space(space
, 0, pos
, 1);
5282 for (i
= 0; i
< pos
; ++i
)
5283 bmap
= var_equal(bmap
, i
);
5284 bmap
= var_more_or_equal(bmap
, pos
);
5285 return isl_basic_map_finalize(bmap
);
5288 static __isl_give isl_map
*map_lex_lte_first(__isl_take isl_space
*space
,
5289 unsigned n
, int equal
)
5291 struct isl_map
*map
;
5294 if (n
== 0 && equal
)
5295 return isl_map_universe(space
);
5297 map
= isl_map_alloc_space(isl_space_copy(space
), n
, ISL_MAP_DISJOINT
);
5299 for (i
= 0; i
+ 1 < n
; ++i
)
5300 map
= isl_map_add_basic_map(map
,
5301 isl_basic_map_less_at(isl_space_copy(space
), i
));
5304 map
= isl_map_add_basic_map(map
,
5305 isl_basic_map_less_or_equal_at(space
, n
- 1));
5307 map
= isl_map_add_basic_map(map
,
5308 isl_basic_map_less_at(space
, n
- 1));
5310 isl_space_free(space
);
5315 static __isl_give isl_map
*map_lex_lte(__isl_take isl_space
*space
, int equal
)
5319 return map_lex_lte_first(space
, space
->n_out
, equal
);
5322 __isl_give isl_map
*isl_map_lex_lt_first(__isl_take isl_space
*space
,
5325 return map_lex_lte_first(space
, n
, 0);
5328 __isl_give isl_map
*isl_map_lex_le_first(__isl_take isl_space
*space
,
5331 return map_lex_lte_first(space
, n
, 1);
5334 __isl_give isl_map
*isl_map_lex_lt(__isl_take isl_space
*set_space
)
5336 return map_lex_lte(isl_space_map_from_set(set_space
), 0);
5339 __isl_give isl_map
*isl_map_lex_le(__isl_take isl_space
*set_space
)
5341 return map_lex_lte(isl_space_map_from_set(set_space
), 1);
5344 static __isl_give isl_map
*map_lex_gte_first(__isl_take isl_space
*space
,
5345 unsigned n
, int equal
)
5347 struct isl_map
*map
;
5350 if (n
== 0 && equal
)
5351 return isl_map_universe(space
);
5353 map
= isl_map_alloc_space(isl_space_copy(space
), n
, ISL_MAP_DISJOINT
);
5355 for (i
= 0; i
+ 1 < n
; ++i
)
5356 map
= isl_map_add_basic_map(map
,
5357 isl_basic_map_more_at(isl_space_copy(space
), i
));
5360 map
= isl_map_add_basic_map(map
,
5361 isl_basic_map_more_or_equal_at(space
, n
- 1));
5363 map
= isl_map_add_basic_map(map
,
5364 isl_basic_map_more_at(space
, n
- 1));
5366 isl_space_free(space
);
5371 static __isl_give isl_map
*map_lex_gte(__isl_take isl_space
*space
, int equal
)
5375 return map_lex_gte_first(space
, space
->n_out
, equal
);
5378 __isl_give isl_map
*isl_map_lex_gt_first(__isl_take isl_space
*space
,
5381 return map_lex_gte_first(space
, n
, 0);
5384 __isl_give isl_map
*isl_map_lex_ge_first(__isl_take isl_space
*space
,
5387 return map_lex_gte_first(space
, n
, 1);
5390 __isl_give isl_map
*isl_map_lex_gt(__isl_take isl_space
*set_space
)
5392 return map_lex_gte(isl_space_map_from_set(set_space
), 0);
5395 __isl_give isl_map
*isl_map_lex_ge(__isl_take isl_space
*set_space
)
5397 return map_lex_gte(isl_space_map_from_set(set_space
), 1);
5400 __isl_give isl_map
*isl_set_lex_le_set(__isl_take isl_set
*set1
,
5401 __isl_take isl_set
*set2
)
5404 map
= isl_map_lex_le(isl_set_get_space(set1
));
5405 map
= isl_map_intersect_domain(map
, set1
);
5406 map
= isl_map_intersect_range(map
, set2
);
5410 __isl_give isl_map
*isl_set_lex_lt_set(__isl_take isl_set
*set1
,
5411 __isl_take isl_set
*set2
)
5414 map
= isl_map_lex_lt(isl_set_get_space(set1
));
5415 map
= isl_map_intersect_domain(map
, set1
);
5416 map
= isl_map_intersect_range(map
, set2
);
5420 __isl_give isl_map
*isl_set_lex_ge_set(__isl_take isl_set
*set1
,
5421 __isl_take isl_set
*set2
)
5424 map
= isl_map_lex_ge(isl_set_get_space(set1
));
5425 map
= isl_map_intersect_domain(map
, set1
);
5426 map
= isl_map_intersect_range(map
, set2
);
5430 __isl_give isl_map
*isl_set_lex_gt_set(__isl_take isl_set
*set1
,
5431 __isl_take isl_set
*set2
)
5434 map
= isl_map_lex_gt(isl_set_get_space(set1
));
5435 map
= isl_map_intersect_domain(map
, set1
);
5436 map
= isl_map_intersect_range(map
, set2
);
5440 __isl_give isl_map
*isl_map_lex_le_map(__isl_take isl_map
*map1
,
5441 __isl_take isl_map
*map2
)
5444 map
= isl_map_lex_le(isl_space_range(isl_map_get_space(map1
)));
5445 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
5446 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
5450 __isl_give isl_map
*isl_map_lex_lt_map(__isl_take isl_map
*map1
,
5451 __isl_take isl_map
*map2
)
5454 map
= isl_map_lex_lt(isl_space_range(isl_map_get_space(map1
)));
5455 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
5456 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
5460 __isl_give isl_map
*isl_map_lex_ge_map(__isl_take isl_map
*map1
,
5461 __isl_take isl_map
*map2
)
5464 map
= isl_map_lex_ge(isl_space_range(isl_map_get_space(map1
)));
5465 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
5466 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
5470 __isl_give isl_map
*isl_map_lex_gt_map(__isl_take isl_map
*map1
,
5471 __isl_take isl_map
*map2
)
5474 map
= isl_map_lex_gt(isl_space_range(isl_map_get_space(map1
)));
5475 map
= isl_map_apply_domain(map
, isl_map_reverse(map1
));
5476 map
= isl_map_apply_range(map
, isl_map_reverse(map2
));
5480 /* For the div d = floor(f/m) at position "div", add the constraint
5484 static __isl_give isl_basic_map
*add_upper_div_constraint(
5485 __isl_take isl_basic_map
*bmap
, unsigned div
)
5488 isl_size v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
5492 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
5493 if (v_div
< 0 || n_div
< 0)
5494 return isl_basic_map_free(bmap
);
5496 i
= isl_basic_map_alloc_inequality(bmap
);
5498 return isl_basic_map_free(bmap
);
5499 isl_seq_cpy(bmap
->ineq
[i
], bmap
->div
[div
] + 1, 1 + v_div
+ n_div
);
5500 isl_int_neg(bmap
->ineq
[i
][1 + pos
], bmap
->div
[div
][0]);
5505 /* For the div d = floor(f/m) at position "div", add the constraint
5507 * -(f-(m-1)) + m d >= 0
5509 static __isl_give isl_basic_map
*add_lower_div_constraint(
5510 __isl_take isl_basic_map
*bmap
, unsigned div
)
5513 isl_size v_div
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
5517 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
5518 if (v_div
< 0 || n_div
< 0)
5519 return isl_basic_map_free(bmap
);
5521 i
= isl_basic_map_alloc_inequality(bmap
);
5523 return isl_basic_map_free(bmap
);
5524 isl_seq_neg(bmap
->ineq
[i
], bmap
->div
[div
] + 1, 1 + v_div
+ n_div
);
5525 isl_int_set(bmap
->ineq
[i
][1 + pos
], bmap
->div
[div
][0]);
5526 isl_int_add(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], bmap
->ineq
[i
][1 + pos
]);
5527 isl_int_sub_ui(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], 1);
5532 /* For the div d = floor(f/m) at position "pos", add the constraints
5535 * -(f-(m-1)) + m d >= 0
5537 * Note that the second constraint is the negation of
5541 __isl_give isl_basic_map
*isl_basic_map_add_div_constraints(
5542 __isl_take isl_basic_map
*bmap
, unsigned pos
)
5544 bmap
= add_upper_div_constraint(bmap
, pos
);
5545 bmap
= add_lower_div_constraint(bmap
, pos
);
5549 /* For each known div d = floor(f/m), add the constraints
5552 * -(f-(m-1)) + m d >= 0
5554 * Remove duplicate constraints in case of some these div constraints
5555 * already appear in "bmap".
5557 __isl_give isl_basic_map
*isl_basic_map_add_known_div_constraints(
5558 __isl_take isl_basic_map
*bmap
)
5562 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
5564 return isl_basic_map_free(bmap
);
5568 bmap
= add_known_div_constraints(bmap
);
5569 bmap
= isl_basic_map_remove_duplicate_constraints(bmap
, NULL
, 0);
5570 bmap
= isl_basic_map_finalize(bmap
);
5574 /* Add the div constraint of sign "sign" for div "div" of "bmap".
5576 * In particular, if this div is of the form d = floor(f/m),
5577 * then add the constraint
5581 * if sign < 0 or the constraint
5583 * -(f-(m-1)) + m d >= 0
5587 __isl_give isl_basic_map
*isl_basic_map_add_div_constraint(
5588 __isl_take isl_basic_map
*bmap
, unsigned div
, int sign
)
5591 return add_upper_div_constraint(bmap
, div
);
5593 return add_lower_div_constraint(bmap
, div
);
5596 __isl_give isl_basic_set
*isl_basic_map_underlying_set(
5597 __isl_take isl_basic_map
*bmap
)
5603 if (bmap
->dim
->nparam
== 0 && bmap
->dim
->n_in
== 0 &&
5605 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_in
) &&
5606 !isl_space_is_named_or_nested(bmap
->dim
, isl_dim_out
))
5607 return bset_from_bmap(bmap
);
5608 bmap
= isl_basic_map_cow(bmap
);
5611 space
= isl_basic_map_take_space(bmap
);
5612 space
= isl_space_underlying(space
, bmap
->n_div
);
5613 bmap
= isl_basic_map_restore_space(bmap
, space
);
5616 bmap
->extra
-= bmap
->n_div
;
5618 bmap
= isl_basic_map_finalize(bmap
);
5619 return bset_from_bmap(bmap
);
5621 isl_basic_map_free(bmap
);
5625 __isl_give isl_basic_set
*isl_basic_set_underlying_set(
5626 __isl_take isl_basic_set
*bset
)
5628 return isl_basic_map_underlying_set(bset_to_bmap(bset
));
5631 /* Replace each element in "list" by the result of applying
5632 * isl_basic_map_underlying_set to the element.
5634 __isl_give isl_basic_set_list
*isl_basic_map_list_underlying_set(
5635 __isl_take isl_basic_map_list
*list
)
5640 n
= isl_basic_map_list_n_basic_map(list
);
5644 for (i
= 0; i
< n
; ++i
) {
5645 isl_basic_map
*bmap
;
5646 isl_basic_set
*bset
;
5648 bmap
= isl_basic_map_list_get_basic_map(list
, i
);
5649 bset
= isl_basic_set_underlying_set(bmap
);
5650 list
= isl_basic_set_list_set_basic_set(list
, i
, bset
);
5655 isl_basic_map_list_free(list
);
5659 __isl_give isl_basic_map
*isl_basic_map_overlying_set(
5660 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_map
*like
)
5662 struct isl_basic_map
*bmap
;
5663 struct isl_ctx
*ctx
;
5664 isl_size dim
, bmap_total
;
5671 if (isl_basic_set_check_no_params(bset
) < 0 ||
5672 isl_basic_set_check_no_locals(bset
) < 0)
5674 dim
= isl_basic_set_dim(bset
, isl_dim_set
);
5675 bmap_total
= isl_basic_map_dim(like
, isl_dim_all
);
5676 if (dim
< 0 || bmap_total
< 0)
5678 isl_assert(ctx
, dim
== bmap_total
, goto error
);
5679 if (like
->n_div
== 0) {
5680 isl_space
*space
= isl_basic_map_get_space(like
);
5681 isl_basic_map_free(like
);
5682 return isl_basic_map_reset_space(bset
, space
);
5684 bset
= isl_basic_set_cow(bset
);
5687 total
= dim
+ bset
->extra
;
5688 bmap
= bset_to_bmap(bset
);
5689 isl_space_free(isl_basic_map_take_space(bmap
));
5690 bmap
= isl_basic_map_restore_space(bmap
, isl_basic_map_get_space(like
));
5693 bmap
->n_div
= like
->n_div
;
5694 bmap
->extra
+= like
->n_div
;
5698 ltotal
= total
- bmap
->extra
+ like
->extra
;
5701 bmap
->block2
= isl_blk_extend(ctx
, bmap
->block2
,
5702 bmap
->extra
* (1 + 1 + total
));
5703 if (isl_blk_is_error(bmap
->block2
))
5705 div
= isl_realloc_array(ctx
, bmap
->div
, isl_int
*, bmap
->extra
);
5709 for (i
= 0; i
< bmap
->extra
; ++i
)
5710 bmap
->div
[i
] = bmap
->block2
.data
+ i
* (1 + 1 + total
);
5711 for (i
= 0; i
< like
->n_div
; ++i
) {
5712 isl_seq_cpy(bmap
->div
[i
], like
->div
[i
], 1 + 1 + ltotal
);
5713 isl_seq_clr(bmap
->div
[i
]+1+1+ltotal
, total
- ltotal
);
5715 bmap
= isl_basic_map_add_known_div_constraints(bmap
);
5717 isl_basic_map_free(like
);
5718 bmap
= isl_basic_map_simplify(bmap
);
5719 bmap
= isl_basic_map_finalize(bmap
);
5722 isl_basic_map_free(like
);
5723 isl_basic_set_free(bset
);
5727 __isl_give isl_basic_set
*isl_basic_set_from_underlying_set(
5728 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*like
)
5730 return bset_from_bmap(isl_basic_map_overlying_set(bset
,
5731 bset_to_bmap(like
)));
5734 __isl_give isl_set
*isl_map_underlying_set(__isl_take isl_map
*map
)
5738 map
= isl_map_cow(map
);
5741 map
->dim
= isl_space_cow(map
->dim
);
5745 for (i
= 1; i
< map
->n
; ++i
)
5746 isl_assert(map
->ctx
, map
->p
[0]->n_div
== map
->p
[i
]->n_div
,
5748 for (i
= 0; i
< map
->n
; ++i
) {
5749 map
->p
[i
] = bset_to_bmap(
5750 isl_basic_map_underlying_set(map
->p
[i
]));
5755 map
->dim
= isl_space_underlying(map
->dim
, 0);
5757 isl_space_free(map
->dim
);
5758 map
->dim
= isl_space_copy(map
->p
[0]->dim
);
5762 return set_from_map(map
);
5768 /* Replace the space of "bmap" by "space".
5770 * If the space of "bmap" is identical to "space" (including the identifiers
5771 * of the input and output dimensions), then simply return the original input.
5773 __isl_give isl_basic_map
*isl_basic_map_reset_space(
5774 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*space
)
5777 isl_space
*bmap_space
;
5779 bmap_space
= isl_basic_map_peek_space(bmap
);
5780 equal
= isl_space_is_equal(bmap_space
, space
);
5781 if (equal
>= 0 && equal
)
5782 equal
= isl_space_has_equal_ids(bmap_space
, space
);
5786 isl_space_free(space
);
5789 isl_space_free(isl_basic_map_take_space(bmap
));
5790 bmap
= isl_basic_map_restore_space(bmap
, space
);
5792 bmap
= isl_basic_map_finalize(bmap
);
5796 isl_basic_map_free(bmap
);
5797 isl_space_free(space
);
5801 __isl_give isl_basic_set
*isl_basic_set_reset_space(
5802 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*space
)
5804 return bset_from_bmap(isl_basic_map_reset_space(bset_to_bmap(bset
),
5808 /* Check that the total dimensions of "map" and "space" are the same.
5810 static isl_stat
check_map_space_equal_total_dim(__isl_keep isl_map
*map
,
5811 __isl_keep isl_space
*space
)
5813 isl_size dim1
, dim2
;
5815 dim1
= isl_map_dim(map
, isl_dim_all
);
5816 dim2
= isl_space_dim(space
, isl_dim_all
);
5817 if (dim1
< 0 || dim2
< 0)
5818 return isl_stat_error
;
5821 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
5822 "total dimensions do not match", return isl_stat_error
);
5825 __isl_give isl_map
*isl_map_reset_space(__isl_take isl_map
*map
,
5826 __isl_take isl_space
*space
)
5830 map
= isl_map_cow(map
);
5834 for (i
= 0; i
< map
->n
; ++i
) {
5835 map
->p
[i
] = isl_basic_map_reset_space(map
->p
[i
],
5836 isl_space_copy(space
));
5840 isl_space_free(isl_map_take_space(map
));
5841 map
= isl_map_restore_space(map
, space
);
5846 isl_space_free(space
);
5850 /* Replace the space of "map" by "space", without modifying
5851 * the dimension of "map".
5853 * If the space of "map" is identical to "space" (including the identifiers
5854 * of the input and output dimensions), then simply return the original input.
5856 __isl_give isl_map
*isl_map_reset_equal_dim_space(__isl_take isl_map
*map
,
5857 __isl_take isl_space
*space
)
5860 isl_space
*map_space
;
5862 map_space
= isl_map_peek_space(map
);
5863 equal
= isl_space_is_equal(map_space
, space
);
5864 if (equal
>= 0 && equal
)
5865 equal
= isl_space_has_equal_ids(map_space
, space
);
5869 isl_space_free(space
);
5872 if (check_map_space_equal_total_dim(map
, space
) < 0)
5874 return isl_map_reset_space(map
, space
);
5877 isl_space_free(space
);
5881 __isl_give isl_set
*isl_set_reset_space(__isl_take isl_set
*set
,
5882 __isl_take isl_space
*space
)
5884 return set_from_map(isl_map_reset_space(set_to_map(set
), space
));
5887 /* Compute the parameter domain of the given basic set.
5889 __isl_give isl_basic_set
*isl_basic_set_params(__isl_take isl_basic_set
*bset
)
5895 is_params
= isl_basic_set_is_params(bset
);
5897 return isl_basic_set_free(bset
);
5901 n
= isl_basic_set_dim(bset
, isl_dim_set
);
5903 return isl_basic_set_free(bset
);
5904 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
5905 space
= isl_basic_set_get_space(bset
);
5906 space
= isl_space_params(space
);
5907 bset
= isl_basic_set_reset_space(bset
, space
);
5911 /* Construct a zero-dimensional basic set with the given parameter domain.
5913 __isl_give isl_basic_set
*isl_basic_set_from_params(
5914 __isl_take isl_basic_set
*bset
)
5917 space
= isl_basic_set_get_space(bset
);
5918 space
= isl_space_set_from_params(space
);
5919 bset
= isl_basic_set_reset_space(bset
, space
);
5923 /* Compute the parameter domain of the given set.
5925 __isl_give isl_set
*isl_set_params(__isl_take isl_set
*set
)
5927 return isl_map_params(set_to_map(set
));
5930 /* Construct a zero-dimensional set with the given parameter domain.
5932 __isl_give isl_set
*isl_set_from_params(__isl_take isl_set
*set
)
5935 space
= isl_set_get_space(set
);
5936 space
= isl_space_set_from_params(space
);
5937 set
= isl_set_reset_space(set
, space
);
5941 /* Compute the parameter domain of the given map.
5943 __isl_give isl_set
*isl_map_params(__isl_take isl_map
*map
)
5946 isl_size n_in
, n_out
;
5948 n_in
= isl_map_dim(map
, isl_dim_in
);
5949 n_out
= isl_map_dim(map
, isl_dim_out
);
5950 if (n_in
< 0 || n_out
< 0)
5951 return isl_map_free(map
);
5952 map
= isl_map_project_out(map
, isl_dim_in
, 0, n_in
);
5953 map
= isl_map_project_out(map
, isl_dim_out
, 0, n_out
);
5954 space
= isl_map_get_space(map
);
5955 space
= isl_space_params(space
);
5956 map
= isl_map_reset_space(map
, space
);
5960 __isl_give isl_basic_set
*isl_basic_map_domain(__isl_take isl_basic_map
*bmap
)
5965 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5967 return isl_basic_map_free(bmap
);
5968 space
= isl_space_domain(isl_basic_map_get_space(bmap
));
5970 bmap
= isl_basic_map_project_out(bmap
, isl_dim_out
, 0, n_out
);
5972 return isl_basic_map_reset_space(bmap
, space
);
5975 isl_bool
isl_basic_map_may_be_set(__isl_keep isl_basic_map
*bmap
)
5978 return isl_bool_error
;
5979 return isl_space_may_be_set(bmap
->dim
);
5982 /* Is this basic map actually a set?
5983 * Users should never call this function. Outside of isl,
5984 * the type should indicate whether something is a set or a map.
5986 isl_bool
isl_basic_map_is_set(__isl_keep isl_basic_map
*bmap
)
5989 return isl_bool_error
;
5990 return isl_space_is_set(bmap
->dim
);
5993 __isl_give isl_basic_set
*isl_basic_map_range(__isl_take isl_basic_map
*bmap
)
5997 is_set
= isl_basic_map_is_set(bmap
);
6002 return isl_basic_map_domain(isl_basic_map_reverse(bmap
));
6004 isl_basic_map_free(bmap
);
6008 __isl_give isl_basic_map
*isl_basic_map_domain_map(
6009 __isl_take isl_basic_map
*bmap
)
6013 isl_basic_map
*domain
;
6014 isl_size nparam
, n_in
, n_out
;
6016 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
6017 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
6018 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
6019 if (nparam
< 0 || n_in
< 0 || n_out
< 0)
6020 return isl_basic_map_free(bmap
);
6022 space
= isl_basic_map_get_space(bmap
);
6023 space
= isl_space_from_range(isl_space_domain(space
));
6024 domain
= isl_basic_map_universe(space
);
6026 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
6027 bmap
= isl_basic_map_apply_range(bmap
, domain
);
6028 bmap
= isl_basic_map_extend_constraints(bmap
, n_in
, 0);
6030 for (i
= 0; i
< n_in
; ++i
)
6031 bmap
= isl_basic_map_equate(bmap
, isl_dim_in
, i
,
6034 bmap
= isl_basic_map_gauss(bmap
, NULL
);
6035 return isl_basic_map_finalize(bmap
);
6038 __isl_give isl_basic_map
*isl_basic_map_range_map(
6039 __isl_take isl_basic_map
*bmap
)
6043 isl_basic_map
*range
;
6044 isl_size nparam
, n_in
, n_out
;
6046 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
6047 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
6048 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
6049 if (nparam
< 0 || n_in
< 0 || n_out
< 0)
6050 return isl_basic_map_free(bmap
);
6052 space
= isl_basic_map_get_space(bmap
);
6053 space
= isl_space_from_range(isl_space_range(space
));
6054 range
= isl_basic_map_universe(space
);
6056 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
6057 bmap
= isl_basic_map_apply_range(bmap
, range
);
6058 bmap
= isl_basic_map_extend_constraints(bmap
, n_out
, 0);
6060 for (i
= 0; i
< n_out
; ++i
)
6061 bmap
= isl_basic_map_equate(bmap
, isl_dim_in
, n_in
+ i
,
6064 bmap
= isl_basic_map_gauss(bmap
, NULL
);
6065 return isl_basic_map_finalize(bmap
);
6068 int isl_map_may_be_set(__isl_keep isl_map
*map
)
6072 return isl_space_may_be_set(map
->dim
);
6075 /* Is this map actually a set?
6076 * Users should never call this function. Outside of isl,
6077 * the type should indicate whether something is a set or a map.
6079 isl_bool
isl_map_is_set(__isl_keep isl_map
*map
)
6082 return isl_bool_error
;
6083 return isl_space_is_set(map
->dim
);
6086 __isl_give isl_set
*isl_map_range(__isl_take isl_map
*map
)
6091 n_in
= isl_map_dim(map
, isl_dim_in
);
6093 return set_from_map(isl_map_free(map
));
6094 space
= isl_space_range(isl_map_get_space(map
));
6096 map
= isl_map_project_out(map
, isl_dim_in
, 0, n_in
);
6098 return set_from_map(isl_map_reset_space(map
, space
));
6101 /* Transform "map" by applying "fn_space" to its space and "fn_bmap"
6102 * to each of its basic maps.
6104 static __isl_give isl_map
*isl_map_transform(__isl_take isl_map
*map
,
6105 __isl_give isl_space
*(*fn_space
)(__isl_take isl_space
*space
),
6106 __isl_give isl_basic_map
*(*fn_bmap
)(__isl_take isl_basic_map
*bmap
))
6111 map
= isl_map_cow(map
);
6115 for (i
= 0; i
< map
->n
; ++i
) {
6116 map
->p
[i
] = fn_bmap(map
->p
[i
]);
6118 return isl_map_free(map
);
6120 map
= isl_map_unmark_normalized(map
);
6122 space
= isl_map_take_space(map
);
6123 space
= fn_space(space
);
6124 map
= isl_map_restore_space(map
, space
);
6129 __isl_give isl_map
*isl_map_domain_map(__isl_take isl_map
*map
)
6131 return isl_map_transform(map
, &isl_space_domain_map
,
6132 &isl_basic_map_domain_map
);
6135 __isl_give isl_map
*isl_map_range_map(__isl_take isl_map
*map
)
6137 return isl_map_transform(map
, &isl_space_range_map
,
6138 &isl_basic_map_range_map
);
6141 /* Given a wrapped map of the form A[B -> C],
6142 * return the map A[B -> C] -> B.
6144 __isl_give isl_map
*isl_set_wrapped_domain_map(__isl_take isl_set
*set
)
6151 if (!isl_set_has_tuple_id(set
))
6152 return isl_map_domain_map(isl_set_unwrap(set
));
6154 id
= isl_set_get_tuple_id(set
);
6155 map
= isl_map_domain_map(isl_set_unwrap(set
));
6156 map
= isl_map_set_tuple_id(map
, isl_dim_in
, id
);
6161 __isl_give isl_basic_map
*isl_basic_map_from_domain(
6162 __isl_take isl_basic_set
*bset
)
6164 return isl_basic_map_reverse(isl_basic_map_from_range(bset
));
6167 __isl_give isl_basic_map
*isl_basic_map_from_range(
6168 __isl_take isl_basic_set
*bset
)
6171 space
= isl_basic_set_get_space(bset
);
6172 space
= isl_space_from_range(space
);
6173 bset
= isl_basic_set_reset_space(bset
, space
);
6174 return bset_to_bmap(bset
);
6177 /* Create a relation with the given set as range.
6178 * The domain of the created relation is a zero-dimensional
6179 * flat anonymous space.
6181 __isl_give isl_map
*isl_map_from_range(__isl_take isl_set
*set
)
6184 space
= isl_set_get_space(set
);
6185 space
= isl_space_from_range(space
);
6186 set
= isl_set_reset_space(set
, space
);
6187 return set_to_map(set
);
6190 /* Create a relation with the given set as domain.
6191 * The range of the created relation is a zero-dimensional
6192 * flat anonymous space.
6194 __isl_give isl_map
*isl_map_from_domain(__isl_take isl_set
*set
)
6196 return isl_map_reverse(isl_map_from_range(set
));
6199 __isl_give isl_basic_map
*isl_basic_map_from_domain_and_range(
6200 __isl_take isl_basic_set
*domain
, __isl_take isl_basic_set
*range
)
6202 return isl_basic_map_apply_range(isl_basic_map_reverse(domain
), range
);
6205 __isl_give isl_map
*isl_map_from_domain_and_range(__isl_take isl_set
*domain
,
6206 __isl_take isl_set
*range
)
6208 return isl_map_apply_range(isl_map_reverse(domain
), range
);
6211 /* Return a newly allocated isl_map with given space and flags and
6212 * room for "n" basic maps.
6213 * Make sure that all cached information is cleared.
6215 __isl_give isl_map
*isl_map_alloc_space(__isl_take isl_space
*space
, int n
,
6218 struct isl_map
*map
;
6223 isl_die(space
->ctx
, isl_error_internal
,
6224 "negative number of basic maps", goto error
);
6225 map
= isl_calloc(space
->ctx
, struct isl_map
,
6226 sizeof(struct isl_map
) +
6227 (n
- 1) * sizeof(struct isl_basic_map
*));
6231 map
->ctx
= space
->ctx
;
6232 isl_ctx_ref(map
->ctx
);
6240 isl_space_free(space
);
6244 __isl_give isl_basic_map
*isl_basic_map_empty(__isl_take isl_space
*space
)
6246 struct isl_basic_map
*bmap
;
6247 bmap
= isl_basic_map_alloc_space(space
, 0, 1, 0);
6248 bmap
= isl_basic_map_set_to_empty(bmap
);
6252 __isl_give isl_basic_set
*isl_basic_set_empty(__isl_take isl_space
*space
)
6254 struct isl_basic_set
*bset
;
6255 bset
= isl_basic_set_alloc_space(space
, 0, 1, 0);
6256 bset
= isl_basic_set_set_to_empty(bset
);
6260 __isl_give isl_basic_map
*isl_basic_map_universe(__isl_take isl_space
*space
)
6262 struct isl_basic_map
*bmap
;
6263 bmap
= isl_basic_map_alloc_space(space
, 0, 0, 0);
6264 bmap
= isl_basic_map_finalize(bmap
);
6268 __isl_give isl_basic_set
*isl_basic_set_universe(__isl_take isl_space
*space
)
6270 struct isl_basic_set
*bset
;
6271 bset
= isl_basic_set_alloc_space(space
, 0, 0, 0);
6272 bset
= isl_basic_set_finalize(bset
);
6276 __isl_give isl_basic_map
*isl_basic_map_nat_universe(
6277 __isl_take isl_space
*space
)
6280 isl_size total
= isl_space_dim(space
, isl_dim_all
);
6281 isl_basic_map
*bmap
;
6284 space
= isl_space_free(space
);
6285 bmap
= isl_basic_map_alloc_space(space
, 0, 0, total
);
6286 for (i
= 0; i
< total
; ++i
) {
6287 int k
= isl_basic_map_alloc_inequality(bmap
);
6290 isl_seq_clr(bmap
->ineq
[k
], 1 + total
);
6291 isl_int_set_si(bmap
->ineq
[k
][1 + i
], 1);
6295 isl_basic_map_free(bmap
);
6299 __isl_give isl_basic_set
*isl_basic_set_nat_universe(
6300 __isl_take isl_space
*space
)
6302 return isl_basic_map_nat_universe(space
);
6305 __isl_give isl_map
*isl_map_nat_universe(__isl_take isl_space
*space
)
6307 return isl_map_from_basic_map(isl_basic_map_nat_universe(space
));
6310 __isl_give isl_set
*isl_set_nat_universe(__isl_take isl_space
*space
)
6312 return isl_map_nat_universe(space
);
6315 __isl_give isl_map
*isl_map_empty(__isl_take isl_space
*space
)
6317 return isl_map_alloc_space(space
, 0, ISL_MAP_DISJOINT
);
6320 __isl_give isl_set
*isl_set_empty(__isl_take isl_space
*space
)
6322 return isl_set_alloc_space(space
, 0, ISL_MAP_DISJOINT
);
6325 __isl_give isl_map
*isl_map_universe(__isl_take isl_space
*space
)
6327 struct isl_map
*map
;
6330 map
= isl_map_alloc_space(isl_space_copy(space
), 1, ISL_MAP_DISJOINT
);
6331 map
= isl_map_add_basic_map(map
, isl_basic_map_universe(space
));
6335 /* This function performs the same operation as isl_map_universe,
6336 * but is considered as a function on an isl_space when exported.
6338 __isl_give isl_map
*isl_space_universe_map(__isl_take isl_space
*space
)
6340 return isl_map_universe(space
);
6343 __isl_give isl_set
*isl_set_universe(__isl_take isl_space
*space
)
6345 struct isl_set
*set
;
6348 set
= isl_set_alloc_space(isl_space_copy(space
), 1, ISL_MAP_DISJOINT
);
6349 set
= isl_set_add_basic_set(set
, isl_basic_set_universe(space
));
6353 /* This function performs the same operation as isl_set_universe,
6354 * but is considered as a function on an isl_space when exported.
6356 __isl_give isl_set
*isl_space_universe_set(__isl_take isl_space
*space
)
6358 return isl_set_universe(space
);
6361 __isl_give isl_map
*isl_map_dup(__isl_keep isl_map
*map
)
6364 struct isl_map
*dup
;
6368 dup
= isl_map_alloc_space(isl_space_copy(map
->dim
), map
->n
, map
->flags
);
6369 for (i
= 0; i
< map
->n
; ++i
)
6370 dup
= isl_map_add_basic_map(dup
, isl_basic_map_copy(map
->p
[i
]));
6374 __isl_give isl_map
*isl_map_add_basic_map(__isl_take isl_map
*map
,
6375 __isl_take isl_basic_map
*bmap
)
6379 if (isl_basic_map_plain_is_empty(bmap
)) {
6380 isl_basic_map_free(bmap
);
6383 if (isl_map_basic_map_check_equal_space(map
, bmap
) < 0)
6385 isl_assert(map
->ctx
, map
->n
< map
->size
, goto error
);
6386 map
->p
[map
->n
] = bmap
;
6388 map
= isl_map_unmark_normalized(map
);
6394 isl_basic_map_free(bmap
);
6398 __isl_null isl_map
*isl_map_free(__isl_take isl_map
*map
)
6409 isl_ctx_deref(map
->ctx
);
6410 for (i
= 0; i
< map
->n
; ++i
)
6411 isl_basic_map_free(map
->p
[i
]);
6412 isl_space_free(map
->dim
);
6418 static __isl_give isl_basic_map
*isl_basic_map_fix_pos_si(
6419 __isl_take isl_basic_map
*bmap
, unsigned pos
, int value
)
6424 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
6426 return isl_basic_map_free(bmap
);
6428 bmap
= isl_basic_map_cow(bmap
);
6429 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
6430 j
= isl_basic_map_alloc_equality(bmap
);
6433 isl_seq_clr(bmap
->eq
[j
] + 1, total
);
6434 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
6435 isl_int_set_si(bmap
->eq
[j
][0], value
);
6436 bmap
= isl_basic_map_simplify(bmap
);
6437 return isl_basic_map_finalize(bmap
);
6439 isl_basic_map_free(bmap
);
6443 static __isl_give isl_basic_map
*isl_basic_map_fix_pos(
6444 __isl_take isl_basic_map
*bmap
, unsigned pos
, isl_int value
)
6449 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
6451 return isl_basic_map_free(bmap
);
6453 bmap
= isl_basic_map_cow(bmap
);
6454 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 0);
6455 j
= isl_basic_map_alloc_equality(bmap
);
6458 isl_seq_clr(bmap
->eq
[j
] + 1, total
);
6459 isl_int_set_si(bmap
->eq
[j
][pos
], -1);
6460 isl_int_set(bmap
->eq
[j
][0], value
);
6461 bmap
= isl_basic_map_simplify(bmap
);
6462 return isl_basic_map_finalize(bmap
);
6464 isl_basic_map_free(bmap
);
6468 __isl_give isl_basic_map
*isl_basic_map_fix_si(__isl_take isl_basic_map
*bmap
,
6469 enum isl_dim_type type
, unsigned pos
, int value
)
6471 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6472 return isl_basic_map_free(bmap
);
6473 return isl_basic_map_fix_pos_si(bmap
,
6474 isl_basic_map_offset(bmap
, type
) + pos
, value
);
6477 __isl_give isl_basic_map
*isl_basic_map_fix(__isl_take isl_basic_map
*bmap
,
6478 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6480 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6481 return isl_basic_map_free(bmap
);
6482 return isl_basic_map_fix_pos(bmap
,
6483 isl_basic_map_offset(bmap
, type
) + pos
, value
);
6486 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
6487 * to be equal to "v".
6489 __isl_give isl_basic_map
*isl_basic_map_fix_val(__isl_take isl_basic_map
*bmap
,
6490 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6494 if (!isl_val_is_int(v
))
6495 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
6496 "expecting integer value", goto error
);
6497 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6499 pos
+= isl_basic_map_offset(bmap
, type
);
6500 bmap
= isl_basic_map_fix_pos(bmap
, pos
, v
->n
);
6504 isl_basic_map_free(bmap
);
6509 /* Fix the value of the variable at position "pos" of type "type" of "bset"
6510 * to be equal to "v".
6512 __isl_give isl_basic_set
*isl_basic_set_fix_val(__isl_take isl_basic_set
*bset
,
6513 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6515 return isl_basic_map_fix_val(bset
, type
, pos
, v
);
6518 __isl_give isl_basic_set
*isl_basic_set_fix_si(__isl_take isl_basic_set
*bset
,
6519 enum isl_dim_type type
, unsigned pos
, int value
)
6521 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset
),
6525 __isl_give isl_basic_set
*isl_basic_set_fix(__isl_take isl_basic_set
*bset
,
6526 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6528 return bset_from_bmap(isl_basic_map_fix(bset_to_bmap(bset
),
6532 /* Remove the basic map at position "i" from "map" if this basic map
6533 * is (obviously) empty.
6535 static __isl_give isl_map
*remove_if_empty(__isl_take isl_map
*map
, int i
)
6542 empty
= isl_basic_map_plain_is_empty(map
->p
[i
]);
6544 return isl_map_free(map
);
6548 isl_basic_map_free(map
->p
[i
]);
6551 map
->p
[i
] = map
->p
[map
->n
];
6552 map
= isl_map_unmark_normalized(map
);
6559 /* Perform "fn" on each basic map of "map", where we may not be holding
6560 * the only reference to "map".
6561 * In particular, "fn" should be a semantics preserving operation
6562 * that we want to apply to all copies of "map". We therefore need
6563 * to be careful not to modify "map" in a way that breaks "map"
6564 * in case anything goes wrong.
6566 __isl_give isl_map
*isl_map_inline_foreach_basic_map(__isl_take isl_map
*map
,
6567 __isl_give isl_basic_map
*(*fn
)(__isl_take isl_basic_map
*bmap
))
6569 struct isl_basic_map
*bmap
;
6575 for (i
= map
->n
- 1; i
>= 0; --i
) {
6576 bmap
= isl_basic_map_copy(map
->p
[i
]);
6580 isl_basic_map_free(map
->p
[i
]);
6582 map
= remove_if_empty(map
, i
);
6593 __isl_give isl_map
*isl_map_fix_si(__isl_take isl_map
*map
,
6594 enum isl_dim_type type
, unsigned pos
, int value
)
6598 map
= isl_map_cow(map
);
6599 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
6600 return isl_map_free(map
);
6601 for (i
= map
->n
- 1; i
>= 0; --i
) {
6602 map
->p
[i
] = isl_basic_map_fix_si(map
->p
[i
], type
, pos
, value
);
6603 map
= remove_if_empty(map
, i
);
6607 map
= isl_map_unmark_normalized(map
);
6611 __isl_give isl_set
*isl_set_fix_si(__isl_take isl_set
*set
,
6612 enum isl_dim_type type
, unsigned pos
, int value
)
6614 return set_from_map(isl_map_fix_si(set_to_map(set
), type
, pos
, value
));
6617 __isl_give isl_map
*isl_map_fix(__isl_take isl_map
*map
,
6618 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6622 map
= isl_map_cow(map
);
6623 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
6624 return isl_map_free(map
);
6625 for (i
= 0; i
< map
->n
; ++i
) {
6626 map
->p
[i
] = isl_basic_map_fix(map
->p
[i
], type
, pos
, value
);
6630 map
= isl_map_unmark_normalized(map
);
6637 __isl_give isl_set
*isl_set_fix(__isl_take isl_set
*set
,
6638 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6640 return set_from_map(isl_map_fix(set_to_map(set
), type
, pos
, value
));
6643 /* Fix the value of the variable at position "pos" of type "type" of "map"
6644 * to be equal to "v".
6646 __isl_give isl_map
*isl_map_fix_val(__isl_take isl_map
*map
,
6647 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6651 map
= isl_map_cow(map
);
6655 if (!isl_val_is_int(v
))
6656 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
6657 "expecting integer value", goto error
);
6658 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
6660 for (i
= map
->n
- 1; i
>= 0; --i
) {
6661 map
->p
[i
] = isl_basic_map_fix_val(map
->p
[i
], type
, pos
,
6663 map
= remove_if_empty(map
, i
);
6667 map
= isl_map_unmark_normalized(map
);
6676 /* Fix the value of the variable at position "pos" of type "type" of "set"
6677 * to be equal to "v".
6679 __isl_give isl_set
*isl_set_fix_val(__isl_take isl_set
*set
,
6680 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*v
)
6682 return isl_map_fix_val(set
, type
, pos
, v
);
6685 __isl_give isl_map
*isl_map_fix_input_si(__isl_take isl_map
*map
,
6686 unsigned input
, int value
)
6688 return isl_map_fix_si(map
, isl_dim_in
, input
, value
);
6691 __isl_give isl_set
*isl_set_fix_dim_si(__isl_take isl_set
*set
, unsigned dim
,
6694 return set_from_map(isl_map_fix_si(set_to_map(set
),
6695 isl_dim_set
, dim
, value
));
6698 static __isl_give isl_basic_map
*basic_map_bound_si(
6699 __isl_take isl_basic_map
*bmap
,
6700 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
6705 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6706 return isl_basic_map_free(bmap
);
6707 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
6709 return isl_basic_map_free(bmap
);
6710 pos
+= isl_basic_map_offset(bmap
, type
);
6711 bmap
= isl_basic_map_cow(bmap
);
6712 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
6713 j
= isl_basic_map_alloc_inequality(bmap
);
6716 isl_seq_clr(bmap
->ineq
[j
], 1 + total
);
6718 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
6719 isl_int_set_si(bmap
->ineq
[j
][0], value
);
6721 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
6722 isl_int_set_si(bmap
->ineq
[j
][0], -value
);
6724 bmap
= isl_basic_map_simplify(bmap
);
6725 return isl_basic_map_finalize(bmap
);
6727 isl_basic_map_free(bmap
);
6731 __isl_give isl_basic_map
*isl_basic_map_lower_bound_si(
6732 __isl_take isl_basic_map
*bmap
,
6733 enum isl_dim_type type
, unsigned pos
, int value
)
6735 return basic_map_bound_si(bmap
, type
, pos
, value
, 0);
6738 /* Constrain the values of the given dimension to be no greater than "value".
6740 __isl_give isl_basic_map
*isl_basic_map_upper_bound_si(
6741 __isl_take isl_basic_map
*bmap
,
6742 enum isl_dim_type type
, unsigned pos
, int value
)
6744 return basic_map_bound_si(bmap
, type
, pos
, value
, 1);
6747 static __isl_give isl_map
*map_bound_si(__isl_take isl_map
*map
,
6748 enum isl_dim_type type
, unsigned pos
, int value
, int upper
)
6752 map
= isl_map_cow(map
);
6753 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
6754 return isl_map_free(map
);
6755 for (i
= 0; i
< map
->n
; ++i
) {
6756 map
->p
[i
] = basic_map_bound_si(map
->p
[i
],
6757 type
, pos
, value
, upper
);
6761 map
= isl_map_unmark_normalized(map
);
6768 __isl_give isl_map
*isl_map_lower_bound_si(__isl_take isl_map
*map
,
6769 enum isl_dim_type type
, unsigned pos
, int value
)
6771 return map_bound_si(map
, type
, pos
, value
, 0);
6774 __isl_give isl_map
*isl_map_upper_bound_si(__isl_take isl_map
*map
,
6775 enum isl_dim_type type
, unsigned pos
, int value
)
6777 return map_bound_si(map
, type
, pos
, value
, 1);
6780 __isl_give isl_set
*isl_set_lower_bound_si(__isl_take isl_set
*set
,
6781 enum isl_dim_type type
, unsigned pos
, int value
)
6783 return set_from_map(isl_map_lower_bound_si(set_to_map(set
),
6787 __isl_give isl_set
*isl_set_upper_bound_si(__isl_take isl_set
*set
,
6788 enum isl_dim_type type
, unsigned pos
, int value
)
6790 return isl_map_upper_bound_si(set
, type
, pos
, value
);
6793 /* Bound the given variable of "bmap" from below (or above is "upper"
6794 * is set) to "value".
6796 static __isl_give isl_basic_map
*basic_map_bound(
6797 __isl_take isl_basic_map
*bmap
,
6798 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
6803 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
6804 return isl_basic_map_free(bmap
);
6805 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
6807 return isl_basic_map_free(bmap
);
6808 pos
+= isl_basic_map_offset(bmap
, type
);
6809 bmap
= isl_basic_map_cow(bmap
);
6810 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
6811 j
= isl_basic_map_alloc_inequality(bmap
);
6814 isl_seq_clr(bmap
->ineq
[j
], 1 + total
);
6816 isl_int_set_si(bmap
->ineq
[j
][pos
], -1);
6817 isl_int_set(bmap
->ineq
[j
][0], value
);
6819 isl_int_set_si(bmap
->ineq
[j
][pos
], 1);
6820 isl_int_neg(bmap
->ineq
[j
][0], value
);
6822 bmap
= isl_basic_map_simplify(bmap
);
6823 return isl_basic_map_finalize(bmap
);
6825 isl_basic_map_free(bmap
);
6829 /* Bound the given variable of "map" from below (or above is "upper"
6830 * is set) to "value".
6832 static __isl_give isl_map
*map_bound(__isl_take isl_map
*map
,
6833 enum isl_dim_type type
, unsigned pos
, isl_int value
, int upper
)
6837 map
= isl_map_cow(map
);
6838 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
6839 return isl_map_free(map
);
6840 for (i
= map
->n
- 1; i
>= 0; --i
) {
6841 map
->p
[i
] = basic_map_bound(map
->p
[i
], type
, pos
, value
, upper
);
6842 map
= remove_if_empty(map
, i
);
6846 map
= isl_map_unmark_normalized(map
);
6850 __isl_give isl_map
*isl_map_lower_bound(__isl_take isl_map
*map
,
6851 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6853 return map_bound(map
, type
, pos
, value
, 0);
6856 __isl_give isl_map
*isl_map_upper_bound(__isl_take isl_map
*map
,
6857 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6859 return map_bound(map
, type
, pos
, value
, 1);
6862 __isl_give isl_set
*isl_set_lower_bound(__isl_take isl_set
*set
,
6863 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6865 return isl_map_lower_bound(set
, type
, pos
, value
);
6868 __isl_give isl_set
*isl_set_upper_bound(__isl_take isl_set
*set
,
6869 enum isl_dim_type type
, unsigned pos
, isl_int value
)
6871 return isl_map_upper_bound(set
, type
, pos
, value
);
6874 /* Force the values of the variable at position "pos" of type "type" of "map"
6875 * to be no smaller than "value".
6877 __isl_give isl_map
*isl_map_lower_bound_val(__isl_take isl_map
*map
,
6878 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
6882 if (!isl_val_is_int(value
))
6883 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
6884 "expecting integer value", goto error
);
6885 map
= isl_map_lower_bound(map
, type
, pos
, value
->n
);
6886 isl_val_free(value
);
6889 isl_val_free(value
);
6894 /* Force the values of the variable at position "pos" of type "type" of "set"
6895 * to be no smaller than "value".
6897 __isl_give isl_set
*isl_set_lower_bound_val(__isl_take isl_set
*set
,
6898 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
6902 map
= set_to_map(set
);
6903 return set_from_map(isl_map_lower_bound_val(map
, type
, pos
, value
));
6906 /* Force the values of the variable at position "pos" of type "type" of "map"
6907 * to be no greater than "value".
6909 __isl_give isl_map
*isl_map_upper_bound_val(__isl_take isl_map
*map
,
6910 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
6914 if (!isl_val_is_int(value
))
6915 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
6916 "expecting integer value", goto error
);
6917 map
= isl_map_upper_bound(map
, type
, pos
, value
->n
);
6918 isl_val_free(value
);
6921 isl_val_free(value
);
6926 /* Force the values of the variable at position "pos" of type "type" of "set"
6927 * to be no greater than "value".
6929 __isl_give isl_set
*isl_set_upper_bound_val(__isl_take isl_set
*set
,
6930 enum isl_dim_type type
, unsigned pos
, __isl_take isl_val
*value
)
6934 map
= set_to_map(set
);
6935 return set_from_map(isl_map_upper_bound_val(map
, type
, pos
, value
));
6938 /* If "mv" has an explicit domain, then intersect the domain of "map"
6939 * with this explicit domain.
6941 * An isl_multi_val object never has an explicit domain,
6942 * so simply return "map".
6944 static __isl_give isl_map
*isl_map_intersect_multi_val_explicit_domain(
6945 __isl_take isl_map
*map
, __isl_keep isl_multi_val
*mv
)
6952 #include "isl_map_bound_templ.c"
6954 /* Apply "map_bound" to "set" with the corresponding value in "bound"
6955 * for each set dimension, by treating the set as a map.
6957 static __isl_give isl_set
*set_bound_multi_val(__isl_take isl_set
*set
,
6958 __isl_take isl_multi_val
*bound
,
6959 __isl_give isl_map
*map_bound(__isl_take isl_map
*map
,
6960 unsigned pos
, __isl_take isl_val
*value
))
6964 map
= set_to_map(set
);
6965 return set_from_map(map_bound_multi_val(map
, bound
, map_bound
));
6970 #include "isl_map_bound_templ.c"
6972 /* Apply "map_bound" to "set" with the corresponding value in "bound"
6973 * for each set dimension, by converting the set and the bound
6974 * to objects living in a map space.
6976 static __isl_give isl_set
*set_bound_multi_pw_aff(__isl_take isl_set
*set
,
6977 __isl_take isl_multi_pw_aff
*bound
,
6978 __isl_give isl_map
*set_bound(__isl_take isl_map
*map
,
6979 unsigned pos
, __isl_take TYPE
*value
))
6983 map
= isl_map_from_range(set
);
6984 bound
= isl_multi_pw_aff_from_range(bound
);
6985 map
= map_bound_multi_pw_aff(map
, bound
, set_bound
);
6986 return isl_map_range(map
);
6989 /* Wrapper around isl_map_lower_bound_val for use in map_bound_multi_val,
6990 * setting a bound on the given output dimension.
6992 static __isl_give isl_map
*map_lower_bound_val(__isl_take isl_map
*map
,
6993 unsigned pos
, __isl_take isl_val
*v
)
6995 return isl_map_lower_bound_val(map
, isl_dim_out
, pos
, v
);
6998 /* Force the values of the set dimensions of "set"
6999 * to be no smaller than the corresponding values in "lower".
7001 __isl_give isl_set
*isl_set_lower_bound_multi_val(__isl_take isl_set
*set
,
7002 __isl_take isl_multi_val
*lower
)
7004 return set_bound_multi_val(set
, lower
, &map_lower_bound_val
);
7007 /* Wrapper around isl_map_upper_bound_val for use in map_bound_multi_val,
7008 * setting a bound on the given output dimension.
7010 static __isl_give isl_map
*map_upper_bound_val(__isl_take isl_map
*map
,
7011 unsigned pos
, __isl_take isl_val
*v
)
7013 return isl_map_upper_bound_val(map
, isl_dim_out
, pos
, v
);
7016 /* Force the values of the set dimensions of "set"
7017 * to be no greater than the corresponding values in "upper".
7019 __isl_give isl_set
*isl_set_upper_bound_multi_val(__isl_take isl_set
*set
,
7020 __isl_take isl_multi_val
*upper
)
7022 return set_bound_multi_val(set
, upper
, &map_upper_bound_val
);
7025 /* Force the symbolic constant expression "bound"
7026 * to satisfy the relation "order" with respect to
7027 * the output variable at position "pos" of "map".
7029 * Create an affine expression representing the output variable
7030 * in terms of the range and
7031 * compare it using "order" to "bound" (defined on the domain).
7032 * The result is a relation between elements in domain and range that
7033 * can be intersected with "map".
7035 static __isl_give isl_map
*map_bound_pw_aff(__isl_take isl_map
*map
,
7036 unsigned pos
, __isl_take isl_pw_aff
*bound
,
7037 __isl_give isl_map
*(*order
)(__isl_take isl_pw_aff
*pa1
,
7038 __isl_take isl_pw_aff
*pa2
))
7041 isl_local_space
*ls
;
7044 space
= isl_space_range(isl_map_get_space(map
));
7045 ls
= isl_local_space_from_space(space
);
7046 var
= isl_pw_aff_var_on_domain(ls
, isl_dim_set
, pos
);
7047 map
= isl_map_intersect(map
, order(bound
, var
));
7051 /* Force the values of the output variable at position "pos" of "map"
7052 * to be no smaller than the symbolic constant expression "lower".
7054 static __isl_give isl_map
*map_lower_bound_pw_aff(__isl_take isl_map
*map
,
7055 unsigned pos
, __isl_take isl_pw_aff
*lower
)
7057 return map_bound_pw_aff(map
, pos
, lower
, &isl_pw_aff_le_map
);
7060 /* Force the values of the output variable at position "pos" of "map"
7061 * to be no greater than the symbolic constant expression "upper".
7063 static __isl_give isl_map
*map_upper_bound_pw_aff(__isl_take isl_map
*map
,
7064 unsigned pos
, __isl_take isl_pw_aff
*upper
)
7066 return map_bound_pw_aff(map
, pos
, upper
, &isl_pw_aff_ge_map
);
7069 /* Force the values of the set dimensions of "set"
7070 * to be no smaller than the corresponding constant symbolic expressions
7073 __isl_give isl_set
*isl_set_lower_bound_multi_pw_aff(__isl_take isl_set
*set
,
7074 __isl_take isl_multi_pw_aff
*lower
)
7076 return set_bound_multi_pw_aff(set
, lower
, &map_lower_bound_pw_aff
);
7079 /* Force the values of the set dimensions of "set"
7080 * to be no greater than the corresponding constant symbolic expressions
7083 __isl_give isl_set
*isl_set_upper_bound_multi_pw_aff(__isl_take isl_set
*set
,
7084 __isl_take isl_multi_pw_aff
*upper
)
7086 return set_bound_multi_pw_aff(set
, upper
, &map_upper_bound_pw_aff
);
7089 /* Force the values of the output dimensions of "map"
7090 * to be no smaller than the corresponding constant symbolic expressions
7093 __isl_give isl_map
*isl_map_lower_bound_multi_pw_aff(__isl_take isl_map
*map
,
7094 __isl_take isl_multi_pw_aff
*lower
)
7096 return map_bound_multi_pw_aff(map
, lower
, &map_lower_bound_pw_aff
);
7099 /* Force the values of the output dimensions of "map"
7100 * to be no greater than the corresponding constant symbolic expressions
7103 __isl_give isl_map
*isl_map_upper_bound_multi_pw_aff(__isl_take isl_map
*map
,
7104 __isl_take isl_multi_pw_aff
*upper
)
7106 return map_bound_multi_pw_aff(map
, upper
, &map_upper_bound_pw_aff
);
7109 /* Bound the given variable of "bset" from below (or above is "upper"
7110 * is set) to "value".
7112 static __isl_give isl_basic_set
*isl_basic_set_bound(
7113 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned pos
,
7114 isl_int value
, int upper
)
7116 return bset_from_bmap(basic_map_bound(bset_to_bmap(bset
),
7117 type
, pos
, value
, upper
));
7120 /* Bound the given variable of "bset" from below (or above is "upper"
7121 * is set) to "value".
7123 static __isl_give isl_basic_set
*isl_basic_set_bound_val(
7124 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned pos
,
7125 __isl_take isl_val
*value
, int upper
)
7129 if (!isl_val_is_int(value
))
7130 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
7131 "expecting integer value", goto error
);
7132 bset
= isl_basic_set_bound(bset
, type
, pos
, value
->n
, upper
);
7133 isl_val_free(value
);
7136 isl_val_free(value
);
7137 isl_basic_set_free(bset
);
7141 /* Bound the given variable of "bset" from below to "value".
7143 __isl_give isl_basic_set
*isl_basic_set_lower_bound_val(
7144 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned pos
,
7145 __isl_take isl_val
*value
)
7147 return isl_basic_set_bound_val(bset
, type
, pos
, value
, 0);
7150 /* Bound the given variable of "bset" from above to "value".
7152 __isl_give isl_basic_set
*isl_basic_set_upper_bound_val(
7153 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned pos
,
7154 __isl_take isl_val
*value
)
7156 return isl_basic_set_bound_val(bset
, type
, pos
, value
, 1);
7159 __isl_give isl_map
*isl_map_reverse(__isl_take isl_map
*map
)
7161 return isl_map_transform(map
, &isl_space_reverse
,
7162 &isl_basic_map_reverse
);
7165 /* Given a map A -> (B -> C), return the corresponding map A -> (C -> B).
7167 __isl_give isl_map
*isl_map_range_reverse(__isl_take isl_map
*map
)
7169 return isl_map_transform(map
, &isl_space_range_reverse
,
7170 &isl_basic_map_range_reverse
);
7174 #define TYPE isl_pw_multi_aff
7176 #define SUFFIX _pw_multi_aff
7178 #define EMPTY isl_pw_multi_aff_empty
7180 #define ADD isl_pw_multi_aff_union_add
7181 #include "isl_map_lexopt_templ.c"
7183 /* Given a map "map", compute the lexicographically minimal
7184 * (or maximal) image element for each domain element in dom,
7185 * in the form of an isl_pw_multi_aff.
7186 * If "empty" is not NULL, then set *empty to those elements in dom that
7187 * do not have an image element.
7188 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
7189 * should be computed over the domain of "map". "empty" is also NULL
7192 * We first compute the lexicographically minimal or maximal element
7193 * in the first basic map. This results in a partial solution "res"
7194 * and a subset "todo" of dom that still need to be handled.
7195 * We then consider each of the remaining maps in "map" and successively
7196 * update both "res" and "todo".
7197 * If "empty" is NULL, then the todo sets are not needed and therefore
7198 * also not computed.
7200 static __isl_give isl_pw_multi_aff
*isl_map_partial_lexopt_aligned_pw_multi_aff(
7201 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
7202 __isl_give isl_set
**empty
, unsigned flags
)
7206 isl_pw_multi_aff
*res
;
7209 full
= ISL_FL_ISSET(flags
, ISL_OPT_FULL
);
7210 if (!map
|| (!full
&& !dom
))
7213 if (isl_map_plain_is_empty(map
)) {
7218 return isl_pw_multi_aff_from_map(map
);
7221 res
= basic_map_partial_lexopt_pw_multi_aff(
7222 isl_basic_map_copy(map
->p
[0]),
7223 isl_set_copy(dom
), empty
, flags
);
7227 for (i
= 1; i
< map
->n
; ++i
) {
7228 isl_pw_multi_aff
*res_i
;
7230 res_i
= basic_map_partial_lexopt_pw_multi_aff(
7231 isl_basic_map_copy(map
->p
[i
]),
7232 isl_set_copy(dom
), empty
, flags
);
7234 if (ISL_FL_ISSET(flags
, ISL_OPT_MAX
))
7235 res
= isl_pw_multi_aff_union_lexmax(res
, res_i
);
7237 res
= isl_pw_multi_aff_union_lexmin(res
, res_i
);
7240 todo
= isl_set_intersect(todo
, *empty
);
7259 #define TYPE isl_map
7263 #define EMPTY isl_map_empty
7265 #define ADD isl_map_union_disjoint
7266 #include "isl_map_lexopt_templ.c"
7268 /* Given a map "map", compute the lexicographically minimal
7269 * (or maximal) image element for each domain element in "dom",
7270 * in the form of an isl_map.
7271 * If "empty" is not NULL, then set *empty to those elements in "dom" that
7272 * do not have an image element.
7273 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
7274 * should be computed over the domain of "map". "empty" is also NULL
7277 * If the input consists of more than one disjunct, then first
7278 * compute the desired result in the form of an isl_pw_multi_aff and
7279 * then convert that into an isl_map.
7281 * This function used to have an explicit implementation in terms
7282 * of isl_maps, but it would continually intersect the domains of
7283 * partial results with the complement of the domain of the next
7284 * partial solution, potentially leading to an explosion in the number
7285 * of disjuncts if there are several disjuncts in the input.
7286 * An even earlier implementation of this function would look for
7287 * better results in the domain of the partial result and for extra
7288 * results in the complement of this domain, which would lead to
7289 * even more splintering.
7291 static __isl_give isl_map
*isl_map_partial_lexopt_aligned(
7292 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
7293 __isl_give isl_set
**empty
, unsigned flags
)
7296 struct isl_map
*res
;
7297 isl_pw_multi_aff
*pma
;
7299 full
= ISL_FL_ISSET(flags
, ISL_OPT_FULL
);
7300 if (!map
|| (!full
&& !dom
))
7303 if (isl_map_plain_is_empty(map
)) {
7312 res
= basic_map_partial_lexopt(isl_basic_map_copy(map
->p
[0]),
7318 pma
= isl_map_partial_lexopt_aligned_pw_multi_aff(map
, dom
, empty
,
7320 return isl_map_from_pw_multi_aff_internal(pma
);
7329 __isl_give isl_map
*isl_map_partial_lexmax(
7330 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
7331 __isl_give isl_set
**empty
)
7333 return isl_map_partial_lexopt(map
, dom
, empty
, ISL_OPT_MAX
);
7336 __isl_give isl_map
*isl_map_partial_lexmin(
7337 __isl_take isl_map
*map
, __isl_take isl_set
*dom
,
7338 __isl_give isl_set
**empty
)
7340 return isl_map_partial_lexopt(map
, dom
, empty
, 0);
7343 __isl_give isl_set
*isl_set_partial_lexmin(
7344 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
7345 __isl_give isl_set
**empty
)
7347 return set_from_map(isl_map_partial_lexmin(set_to_map(set
),
7351 __isl_give isl_set
*isl_set_partial_lexmax(
7352 __isl_take isl_set
*set
, __isl_take isl_set
*dom
,
7353 __isl_give isl_set
**empty
)
7355 return set_from_map(isl_map_partial_lexmax(set_to_map(set
),
7359 /* Compute the lexicographic minimum (or maximum if "flags" includes
7360 * ISL_OPT_MAX) of "bset" over its parametric domain.
7362 __isl_give isl_set
*isl_basic_set_lexopt(__isl_take isl_basic_set
*bset
,
7365 return isl_basic_map_lexopt(bset
, flags
);
7368 __isl_give isl_map
*isl_basic_map_lexmax(__isl_take isl_basic_map
*bmap
)
7370 return isl_basic_map_lexopt(bmap
, ISL_OPT_MAX
);
7373 __isl_give isl_set
*isl_basic_set_lexmin(__isl_take isl_basic_set
*bset
)
7375 return set_from_map(isl_basic_map_lexmin(bset_to_bmap(bset
)));
7378 __isl_give isl_set
*isl_basic_set_lexmax(__isl_take isl_basic_set
*bset
)
7380 return set_from_map(isl_basic_map_lexmax(bset_to_bmap(bset
)));
7383 /* Compute the lexicographic minimum of "bset" over its parametric domain
7384 * for the purpose of quantifier elimination.
7385 * That is, find an explicit representation for all the existentially
7386 * quantified variables in "bset" by computing their lexicographic
7389 static __isl_give isl_set
*isl_basic_set_lexmin_compute_divs(
7390 __isl_take isl_basic_set
*bset
)
7392 return isl_basic_set_lexopt(bset
, ISL_OPT_QE
);
7395 /* Given a basic map with one output dimension, compute the minimum or
7396 * maximum of that dimension as an isl_pw_aff.
7398 * Compute the optimum as a lexicographic optimum over the single
7399 * output dimension and extract the single isl_pw_aff from the result.
7401 static __isl_give isl_pw_aff
*basic_map_dim_opt(__isl_keep isl_basic_map
*bmap
,
7404 isl_pw_multi_aff
*pma
;
7407 bmap
= isl_basic_map_copy(bmap
);
7408 pma
= isl_basic_map_lexopt_pw_multi_aff(bmap
, max
? ISL_OPT_MAX
: 0);
7409 pwaff
= isl_pw_multi_aff_get_pw_aff(pma
, 0);
7410 isl_pw_multi_aff_free(pma
);
7415 /* Compute the minimum or maximum of the given output dimension
7416 * as a function of the parameters and the input dimensions,
7417 * but independently of the other output dimensions.
7419 * We first project out the other output dimension and then compute
7420 * the "lexicographic" maximum in each basic map, combining the results
7421 * using isl_pw_aff_union_max.
7423 static __isl_give isl_pw_aff
*map_dim_opt(__isl_take isl_map
*map
, int pos
,
7430 n_out
= isl_map_dim(map
, isl_dim_out
);
7432 map
= isl_map_free(map
);
7433 map
= isl_map_project_out(map
, isl_dim_out
, pos
+ 1, n_out
- (pos
+ 1));
7434 map
= isl_map_project_out(map
, isl_dim_out
, 0, pos
);
7439 isl_space
*space
= isl_map_get_space(map
);
7441 return isl_pw_aff_empty(space
);
7444 pwaff
= basic_map_dim_opt(map
->p
[0], max
);
7445 for (i
= 1; i
< map
->n
; ++i
) {
7446 isl_pw_aff
*pwaff_i
;
7448 pwaff_i
= basic_map_dim_opt(map
->p
[i
], max
);
7449 pwaff
= isl_pw_aff_union_opt(pwaff
, pwaff_i
, max
);
7457 /* Compute the minimum of the given output dimension as a function of the
7458 * parameters and input dimensions, but independently of
7459 * the other output dimensions.
7461 __isl_give isl_pw_aff
*isl_map_dim_min(__isl_take isl_map
*map
, int pos
)
7463 return map_dim_opt(map
, pos
, 0);
7466 /* Compute the maximum of the given output dimension as a function of the
7467 * parameters and input dimensions, but independently of
7468 * the other output dimensions.
7470 __isl_give isl_pw_aff
*isl_map_dim_max(__isl_take isl_map
*map
, int pos
)
7472 return map_dim_opt(map
, pos
, 1);
7475 /* Compute the minimum or maximum of the given set dimension
7476 * as a function of the parameters,
7477 * but independently of the other set dimensions.
7479 static __isl_give isl_pw_aff
*set_dim_opt(__isl_take isl_set
*set
, int pos
,
7482 return map_dim_opt(set
, pos
, max
);
7485 /* Compute the maximum of the given set dimension as a function of the
7486 * parameters, but independently of the other set dimensions.
7488 __isl_give isl_pw_aff
*isl_set_dim_max(__isl_take isl_set
*set
, int pos
)
7490 return set_dim_opt(set
, pos
, 1);
7493 /* Compute the minimum of the given set dimension as a function of the
7494 * parameters, but independently of the other set dimensions.
7496 __isl_give isl_pw_aff
*isl_set_dim_min(__isl_take isl_set
*set
, int pos
)
7498 return set_dim_opt(set
, pos
, 0);
7501 /* Apply a preimage specified by "mat" on the parameters of "bset".
7502 * bset is assumed to have only parameters and divs.
7504 static __isl_give isl_basic_set
*basic_set_parameter_preimage(
7505 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*mat
)
7509 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
7510 if (nparam
< 0 || !mat
)
7513 bset
->dim
= isl_space_cow(bset
->dim
);
7517 isl_assert(bset
->ctx
, mat
->n_row
== 1 + nparam
, goto error
);
7519 bset
->dim
->nparam
= 0;
7520 bset
->dim
->n_out
= nparam
;
7521 bset
= isl_basic_set_preimage(bset
, mat
);
7523 bset
->dim
->nparam
= bset
->dim
->n_out
;
7524 bset
->dim
->n_out
= 0;
7529 isl_basic_set_free(bset
);
7533 /* Apply a preimage specified by "mat" on the parameters of "set".
7534 * set is assumed to have only parameters and divs.
7536 static __isl_give isl_set
*set_parameter_preimage(__isl_take isl_set
*set
,
7537 __isl_take isl_mat
*mat
)
7542 nparam
= isl_set_dim(set
, isl_dim_param
);
7543 if (nparam
< 0 || !mat
)
7546 if (mat
->n_row
!= 1 + nparam
)
7547 isl_die(isl_set_get_ctx(set
), isl_error_internal
,
7548 "unexpected number of rows", goto error
);
7550 space
= isl_set_get_space(set
);
7551 space
= isl_space_move_dims(space
, isl_dim_set
, 0,
7552 isl_dim_param
, 0, nparam
);
7553 set
= isl_set_reset_space(set
, space
);
7554 set
= isl_set_preimage(set
, mat
);
7555 nparam
= isl_set_dim(set
, isl_dim_out
);
7557 set
= isl_set_free(set
);
7558 space
= isl_set_get_space(set
);
7559 space
= isl_space_move_dims(space
, isl_dim_param
, 0,
7560 isl_dim_out
, 0, nparam
);
7561 set
= isl_set_reset_space(set
, space
);
7569 /* Intersect the basic set "bset" with the affine space specified by the
7570 * equalities in "eq".
7572 static __isl_give isl_basic_set
*basic_set_append_equalities(
7573 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*eq
)
7581 bset
= isl_basic_set_extend(bset
, 0, eq
->n_row
, 0);
7585 len
= isl_basic_set_offset(bset
, isl_dim_div
) + bset
->extra
;
7586 for (i
= 0; i
< eq
->n_row
; ++i
) {
7587 k
= isl_basic_set_alloc_equality(bset
);
7590 isl_seq_cpy(bset
->eq
[k
], eq
->row
[i
], eq
->n_col
);
7591 isl_seq_clr(bset
->eq
[k
] + eq
->n_col
, len
- eq
->n_col
);
7595 bset
= isl_basic_set_gauss(bset
, NULL
);
7596 bset
= isl_basic_set_finalize(bset
);
7601 isl_basic_set_free(bset
);
7605 /* Intersect the set "set" with the affine space specified by the
7606 * equalities in "eq".
7608 static __isl_give isl_set
*set_append_equalities(__isl_take isl_set
*set
,
7609 __isl_take isl_mat
*eq
)
7616 for (i
= 0; i
< set
->n
; ++i
) {
7617 set
->p
[i
] = basic_set_append_equalities(set
->p
[i
],
7630 /* Given a basic set "bset" that only involves parameters and existentially
7631 * quantified variables, return the index of the first equality
7632 * that only involves parameters. If there is no such equality then
7633 * return bset->n_eq.
7635 * This function assumes that isl_basic_set_gauss has been called on "bset".
7637 static int first_parameter_equality(__isl_keep isl_basic_set
*bset
)
7640 isl_size nparam
, n_div
;
7642 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
7643 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
7644 if (nparam
< 0 || n_div
< 0)
7647 for (i
= 0, j
= n_div
- 1; i
< bset
->n_eq
&& j
>= 0; --j
) {
7648 if (!isl_int_is_zero(bset
->eq
[i
][1 + nparam
+ j
]))
7655 /* Compute an explicit representation for the existentially quantified
7656 * variables in "bset" by computing the "minimal value" of the set
7657 * variables. Since there are no set variables, the computation of
7658 * the minimal value essentially computes an explicit representation
7659 * of the non-empty part(s) of "bset".
7661 * The input only involves parameters and existentially quantified variables.
7662 * All equalities among parameters have been removed.
7664 * Since the existentially quantified variables in the result are in general
7665 * going to be different from those in the input, we first replace
7666 * them by the minimal number of variables based on their equalities.
7667 * This should simplify the parametric integer programming.
7669 static __isl_give isl_set
*base_compute_divs(__isl_take isl_basic_set
*bset
)
7671 isl_morph
*morph1
, *morph2
;
7677 if (bset
->n_eq
== 0)
7678 return isl_basic_set_lexmin_compute_divs(bset
);
7680 morph1
= isl_basic_set_parameter_compression(bset
);
7681 bset
= isl_morph_basic_set(isl_morph_copy(morph1
), bset
);
7682 bset
= isl_basic_set_lift(bset
);
7683 morph2
= isl_basic_set_variable_compression(bset
, isl_dim_set
);
7684 bset
= isl_morph_basic_set(morph2
, bset
);
7685 n
= isl_basic_set_dim(bset
, isl_dim_set
);
7687 bset
= isl_basic_set_free(bset
);
7688 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, n
);
7690 set
= isl_basic_set_lexmin_compute_divs(bset
);
7692 set
= isl_morph_set(isl_morph_inverse(morph1
), set
);
7697 /* Project the given basic set onto its parameter domain, possibly introducing
7698 * new, explicit, existential variables in the constraints.
7699 * The input has parameters and (possibly implicit) existential variables.
7700 * The output has the same parameters, but only
7701 * explicit existentially quantified variables.
7703 * The actual projection is performed by pip, but pip doesn't seem
7704 * to like equalities very much, so we first remove the equalities
7705 * among the parameters by performing a variable compression on
7706 * the parameters. Afterward, an inverse transformation is performed
7707 * and the equalities among the parameters are inserted back in.
7709 * The variable compression on the parameters may uncover additional
7710 * equalities that were only implicit before. We therefore check
7711 * if there are any new parameter equalities in the result and
7712 * if so recurse. The removal of parameter equalities is required
7713 * for the parameter compression performed by base_compute_divs.
7715 static __isl_give isl_set
*parameter_compute_divs(
7716 __isl_take isl_basic_set
*bset
)
7720 struct isl_mat
*T
, *T2
;
7721 struct isl_set
*set
;
7724 bset
= isl_basic_set_cow(bset
);
7728 if (bset
->n_eq
== 0)
7729 return base_compute_divs(bset
);
7731 bset
= isl_basic_set_gauss(bset
, NULL
);
7734 if (isl_basic_set_plain_is_empty(bset
))
7735 return isl_set_from_basic_set(bset
);
7737 i
= first_parameter_equality(bset
);
7738 if (i
== bset
->n_eq
)
7739 return base_compute_divs(bset
);
7741 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
7743 return isl_set_from_basic_set(isl_basic_set_free(bset
));
7744 eq
= isl_mat_sub_alloc6(bset
->ctx
, bset
->eq
, i
, bset
->n_eq
- i
,
7746 eq
= isl_mat_cow(eq
);
7747 T
= isl_mat_variable_compression(isl_mat_copy(eq
), &T2
);
7748 if (T
&& T
->n_col
== 0) {
7752 bset
= isl_basic_set_set_to_empty(bset
);
7753 return isl_set_from_basic_set(bset
);
7755 bset
= basic_set_parameter_preimage(bset
, T
);
7757 i
= first_parameter_equality(bset
);
7760 else if (i
== bset
->n_eq
)
7761 set
= base_compute_divs(bset
);
7763 set
= parameter_compute_divs(bset
);
7764 set
= set_parameter_preimage(set
, T2
);
7765 set
= set_append_equalities(set
, eq
);
7769 /* Insert the divs from "ls" before those of "bmap".
7771 * The number of columns is not changed, which means that the last
7772 * dimensions of "bmap" are being reintepreted as the divs from "ls".
7773 * The caller is responsible for removing the same number of dimensions
7774 * from the space of "bmap".
7776 static __isl_give isl_basic_map
*insert_divs_from_local_space(
7777 __isl_take isl_basic_map
*bmap
, __isl_keep isl_local_space
*ls
)
7783 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
7785 return isl_basic_map_free(bmap
);
7789 old_n_div
= bmap
->n_div
;
7790 bmap
= insert_div_rows(bmap
, n_div
);
7794 for (i
= 0; i
< n_div
; ++i
) {
7795 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], ls
->div
->n_col
);
7796 isl_seq_clr(bmap
->div
[i
] + ls
->div
->n_col
, old_n_div
);
7802 /* Replace the space of "bmap" by the space and divs of "ls".
7804 * If "ls" has any divs, then we simplify the result since we may
7805 * have discovered some additional equalities that could simplify
7806 * the div expressions.
7808 static __isl_give isl_basic_map
*basic_replace_space_by_local_space(
7809 __isl_take isl_basic_map
*bmap
, __isl_take isl_local_space
*ls
)
7813 bmap
= isl_basic_map_cow(bmap
);
7814 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
7815 if (!bmap
|| n_div
< 0)
7818 bmap
= insert_divs_from_local_space(bmap
, ls
);
7822 isl_space_free(bmap
->dim
);
7823 bmap
->dim
= isl_local_space_get_space(ls
);
7827 isl_local_space_free(ls
);
7829 bmap
= isl_basic_map_simplify(bmap
);
7830 bmap
= isl_basic_map_finalize(bmap
);
7833 isl_basic_map_free(bmap
);
7834 isl_local_space_free(ls
);
7838 /* Replace the space of "map" by the space and divs of "ls".
7840 static __isl_give isl_map
*replace_space_by_local_space(__isl_take isl_map
*map
,
7841 __isl_take isl_local_space
*ls
)
7845 map
= isl_map_cow(map
);
7849 for (i
= 0; i
< map
->n
; ++i
) {
7850 map
->p
[i
] = basic_replace_space_by_local_space(map
->p
[i
],
7851 isl_local_space_copy(ls
));
7855 isl_space_free(isl_map_take_space(map
));
7856 map
= isl_map_restore_space(map
, isl_local_space_get_space(ls
));
7858 isl_local_space_free(ls
);
7861 isl_local_space_free(ls
);
7866 /* Compute an explicit representation for the existentially
7867 * quantified variables for which do not know any explicit representation yet.
7869 * We first sort the existentially quantified variables so that the
7870 * existentially quantified variables for which we already have an explicit
7871 * representation are placed before those for which we do not.
7872 * The input dimensions, the output dimensions and the existentially
7873 * quantified variables for which we already have an explicit
7874 * representation are then turned into parameters.
7875 * compute_divs returns a map with the same parameters and
7876 * no input or output dimensions and the dimension specification
7877 * is reset to that of the input, including the existentially quantified
7878 * variables for which we already had an explicit representation.
7880 static __isl_give isl_map
*compute_divs(__isl_take isl_basic_map
*bmap
)
7882 struct isl_basic_set
*bset
;
7883 struct isl_set
*set
;
7884 struct isl_map
*map
;
7886 isl_local_space
*ls
;
7893 bmap
= isl_basic_map_sort_divs(bmap
);
7894 bmap
= isl_basic_map_cow(bmap
);
7898 n_known
= isl_basic_map_first_unknown_div(bmap
);
7899 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
7900 n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
7901 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
7902 if (n_known
< 0 || nparam
< 0 || n_in
< 0 || n_out
< 0)
7903 return isl_map_from_basic_map(isl_basic_map_free(bmap
));
7905 space
= isl_space_set_alloc(bmap
->ctx
,
7906 nparam
+ n_in
+ n_out
+ n_known
, 0);
7910 ls
= isl_basic_map_get_local_space(bmap
);
7911 ls
= isl_local_space_drop_dims(ls
, isl_dim_div
,
7912 n_known
, bmap
->n_div
- n_known
);
7914 for (i
= n_known
; i
< bmap
->n_div
; ++i
)
7915 swap_div(bmap
, i
- n_known
, i
);
7916 bmap
->n_div
-= n_known
;
7917 bmap
->extra
-= n_known
;
7919 bmap
= isl_basic_map_reset_space(bmap
, space
);
7920 bset
= bset_from_bmap(bmap
);
7922 set
= parameter_compute_divs(bset
);
7923 map
= set_to_map(set
);
7924 map
= replace_space_by_local_space(map
, ls
);
7928 isl_basic_map_free(bmap
);
7932 /* Remove the explicit representation of local variable "div",
7935 __isl_give isl_basic_map
*isl_basic_map_mark_div_unknown(
7936 __isl_take isl_basic_map
*bmap
, int div
)
7940 unknown
= isl_basic_map_div_is_marked_unknown(bmap
, div
);
7942 return isl_basic_map_free(bmap
);
7946 bmap
= isl_basic_map_cow(bmap
);
7949 isl_int_set_si(bmap
->div
[div
][0], 0);
7953 /* Is local variable "div" of "bmap" marked as not having an explicit
7955 * Note that even if "div" is not marked in this way and therefore
7956 * has an explicit representation, this representation may still
7957 * depend (indirectly) on other local variables that do not
7958 * have an explicit representation.
7960 isl_bool
isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map
*bmap
,
7963 if (isl_basic_map_check_range(bmap
, isl_dim_div
, div
, 1) < 0)
7964 return isl_bool_error
;
7965 return isl_int_is_zero(bmap
->div
[div
][0]);
7968 /* Return the position of the first local variable that does not
7969 * have an explicit representation.
7970 * Return the total number of local variables if they all have
7971 * an explicit representation.
7972 * Return -1 on error.
7974 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map
*bmap
)
7981 for (i
= 0; i
< bmap
->n_div
; ++i
) {
7982 if (!isl_basic_map_div_is_known(bmap
, i
))
7988 /* Return the position of the first local variable that does not
7989 * have an explicit representation.
7990 * Return the total number of local variables if they all have
7991 * an explicit representation.
7992 * Return -1 on error.
7994 int isl_basic_set_first_unknown_div(__isl_keep isl_basic_set
*bset
)
7996 return isl_basic_map_first_unknown_div(bset
);
7999 /* Does "bmap" have an explicit representation for all local variables?
8001 isl_bool
isl_basic_map_divs_known(__isl_keep isl_basic_map
*bmap
)
8006 n
= isl_basic_map_dim(bmap
, isl_dim_div
);
8007 first
= isl_basic_map_first_unknown_div(bmap
);
8008 if (n
< 0 || first
< 0)
8009 return isl_bool_error
;
8013 /* Do all basic maps in "map" have an explicit representation
8014 * for all local variables?
8016 isl_bool
isl_map_divs_known(__isl_keep isl_map
*map
)
8021 return isl_bool_error
;
8023 for (i
= 0; i
< map
->n
; ++i
) {
8024 int known
= isl_basic_map_divs_known(map
->p
[i
]);
8029 return isl_bool_true
;
8032 /* If bmap contains any unknown divs, then compute explicit
8033 * expressions for them. However, this computation may be
8034 * quite expensive, so first try to remove divs that aren't
8037 __isl_give isl_map
*isl_basic_map_compute_divs(__isl_take isl_basic_map
*bmap
)
8040 struct isl_map
*map
;
8042 known
= isl_basic_map_divs_known(bmap
);
8046 return isl_map_from_basic_map(bmap
);
8048 bmap
= isl_basic_map_drop_redundant_divs(bmap
);
8050 known
= isl_basic_map_divs_known(bmap
);
8054 return isl_map_from_basic_map(bmap
);
8056 map
= compute_divs(bmap
);
8059 isl_basic_map_free(bmap
);
8063 __isl_give isl_map
*isl_map_compute_divs(__isl_take isl_map
*map
)
8067 struct isl_map
*res
;
8074 known
= isl_map_divs_known(map
);
8082 res
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[0]));
8083 for (i
= 1 ; i
< map
->n
; ++i
) {
8085 r2
= isl_basic_map_compute_divs(isl_basic_map_copy(map
->p
[i
]));
8086 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
8087 res
= isl_map_union_disjoint(res
, r2
);
8089 res
= isl_map_union(res
, r2
);
8096 __isl_give isl_set
*isl_basic_set_compute_divs(__isl_take isl_basic_set
*bset
)
8098 return set_from_map(isl_basic_map_compute_divs(bset_to_bmap(bset
)));
8101 __isl_give isl_set
*isl_set_compute_divs(__isl_take isl_set
*set
)
8103 return set_from_map(isl_map_compute_divs(set_to_map(set
)));
8106 __isl_give isl_set
*isl_map_domain(__isl_take isl_map
*map
)
8111 n_out
= isl_map_dim(map
, isl_dim_out
);
8113 return set_from_map(isl_map_free(map
));
8114 space
= isl_space_domain(isl_map_get_space(map
));
8116 map
= isl_map_project_out(map
, isl_dim_out
, 0, n_out
);
8118 return set_from_map(isl_map_reset_space(map
, space
));
8121 /* Return the union of "map1" and "map2", where we assume for now that
8122 * "map1" and "map2" are disjoint. Note that the basic maps inside
8123 * "map1" or "map2" may not be disjoint from each other.
8124 * Also note that this function is also called from isl_map_union,
8125 * which takes care of handling the situation where "map1" and "map2"
8126 * may not be disjoint.
8128 * If one of the inputs is empty, we can simply return the other input.
8129 * Similarly, if one of the inputs is universal, then it is equal to the union.
8131 static __isl_give isl_map
*map_union_disjoint(__isl_take isl_map
*map1
,
8132 __isl_take isl_map
*map2
)
8136 struct isl_map
*map
= NULL
;
8139 if (isl_map_check_equal_space(map1
, map2
) < 0)
8151 is_universe
= isl_map_plain_is_universe(map1
);
8152 if (is_universe
< 0)
8159 is_universe
= isl_map_plain_is_universe(map2
);
8160 if (is_universe
< 0)
8167 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
8168 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
8169 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
8171 map
= isl_map_alloc_space(isl_space_copy(map1
->dim
),
8172 map1
->n
+ map2
->n
, flags
);
8175 for (i
= 0; i
< map1
->n
; ++i
) {
8176 map
= isl_map_add_basic_map(map
,
8177 isl_basic_map_copy(map1
->p
[i
]));
8181 for (i
= 0; i
< map2
->n
; ++i
) {
8182 map
= isl_map_add_basic_map(map
,
8183 isl_basic_map_copy(map2
->p
[i
]));
8197 /* Return the union of "map1" and "map2", where "map1" and "map2" are
8198 * guaranteed to be disjoint by the caller.
8200 * Note that this functions is called from within isl_map_make_disjoint,
8201 * so we have to be careful not to touch the constraints of the inputs
8204 __isl_give isl_map
*isl_map_union_disjoint(__isl_take isl_map
*map1
,
8205 __isl_take isl_map
*map2
)
8207 isl_map_align_params_bin(&map1
, &map2
);
8208 return map_union_disjoint(map1
, map2
);
8211 /* Return the union of "map1" and "map2", where "map1" and "map2" may
8214 * We currently simply call map_union_disjoint, the internal operation
8215 * of which does not really depend on the inputs being disjoint.
8216 * If the result contains more than one basic map, then we clear
8217 * the disjoint flag since the result may contain basic maps from
8218 * both inputs and these are not guaranteed to be disjoint.
8220 * As a special case, if "map1" and "map2" are obviously equal,
8221 * then we simply return "map1".
8223 __isl_give isl_map
*isl_map_union(__isl_take isl_map
*map1
,
8224 __isl_take isl_map
*map2
)
8228 if (isl_map_align_params_bin(&map1
, &map2
) < 0)
8231 equal
= isl_map_plain_is_equal(map1
, map2
);
8239 map1
= map_union_disjoint(map1
, map2
);
8243 ISL_F_CLR(map1
, ISL_MAP_DISJOINT
);
8251 __isl_give isl_set
*isl_set_union_disjoint(
8252 __isl_take isl_set
*set1
, __isl_take isl_set
*set2
)
8254 return set_from_map(isl_map_union_disjoint(set_to_map(set1
),
8258 __isl_give isl_set
*isl_set_union(__isl_take isl_set
*set1
,
8259 __isl_take isl_set
*set2
)
8261 return set_from_map(isl_map_union(set_to_map(set1
), set_to_map(set2
)));
8264 /* Apply "fn" to pairs of elements from "map" and "set" and collect
8265 * the results in a map living in "space".
8267 * "map" and "set" are assumed to be compatible and non-NULL.
8269 static __isl_give isl_map
*map_intersect_set(__isl_take isl_map
*map
,
8270 __isl_take isl_space
*space
, __isl_take isl_set
*set
,
8271 __isl_give isl_basic_map
*fn(__isl_take isl_basic_map
*bmap
,
8272 __isl_take isl_basic_set
*bset
))
8275 struct isl_map
*result
;
8278 if (isl_set_plain_is_universe(set
)) {
8280 return isl_map_reset_equal_dim_space(map
, space
);
8283 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
) &&
8284 ISL_F_ISSET(set
, ISL_MAP_DISJOINT
))
8285 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
8287 result
= isl_map_alloc_space(space
, map
->n
* set
->n
, flags
);
8288 for (i
= 0; result
&& i
< map
->n
; ++i
)
8289 for (j
= 0; j
< set
->n
; ++j
) {
8290 result
= isl_map_add_basic_map(result
,
8291 fn(isl_basic_map_copy(map
->p
[i
]),
8292 isl_basic_set_copy(set
->p
[j
])));
8302 __isl_give isl_map
*isl_map_intersect_range(__isl_take isl_map
*map
,
8303 __isl_take isl_set
*set
)
8308 isl_map_align_params_set(&map
, &set
);
8309 ok
= isl_map_compatible_range(map
, set
);
8313 isl_die(set
->ctx
, isl_error_invalid
,
8314 "incompatible spaces", goto error
);
8316 space
= isl_map_get_space(map
);
8317 return map_intersect_set(map
, space
, set
,
8318 &isl_basic_map_intersect_range
);
8325 /* Intersect the domain of "map" with "set".
8327 * If the domain dimensions of "map" do not have any identifiers,
8328 * then copy them over from "set".
8330 __isl_give isl_map
*isl_map_intersect_domain(__isl_take isl_map
*map
,
8331 __isl_take isl_set
*set
)
8336 isl_map_align_params_set(&map
, &set
);
8337 ok
= isl_map_compatible_domain(map
, set
);
8341 isl_die(set
->ctx
, isl_error_invalid
,
8342 "incompatible spaces", goto error
);
8344 space
= isl_map_get_space(map
);
8345 space
= isl_space_copy_ids_if_unset(space
, isl_dim_in
,
8346 isl_set_peek_space(set
), isl_dim_set
);
8347 return map_intersect_set(map
, space
, set
,
8348 &isl_basic_map_intersect_domain
);
8356 #define TYPE isl_map
8358 #include "isl_copy_tuple_id_templ.c"
8360 /* Data structure that specifies how isl_map_intersect_factor
8363 * "preserve_type" is the tuple where the factor differs from
8364 * the input map and of which the identifiers needs
8365 * to be preserved explicitly.
8366 * "other_factor" is used to extract the space of the other factor
8367 * from the space of the product ("map").
8368 * "product" is used to combine the given factor and a universe map
8369 * in the space returned by "other_factor" to produce a map
8370 * that lives in the same space as the input map.
8372 struct isl_intersect_factor_control
{
8373 enum isl_dim_type preserve_type
;
8374 __isl_give isl_space
*(*other_factor
)(__isl_take isl_space
*space
);
8375 __isl_give isl_map
*(*product
)(__isl_take isl_map
*factor
,
8376 __isl_take isl_map
*other
);
8379 /* Given a map "map" in some product space and a map "factor"
8380 * living in some factor space, return the intersection.
8382 * After aligning the parameters,
8383 * the map "factor" is first extended to a map living in the same space
8384 * as "map" and then a regular intersection is computed.
8386 * Note that the extension is computed as a product, which is anonymous
8387 * by default. If "map" has an identifier on the corresponding tuple,
8388 * then this identifier needs to be set on the product
8389 * before the intersection is computed.
8391 static __isl_give isl_map
*isl_map_intersect_factor(
8392 __isl_take isl_map
*map
, __isl_take isl_map
*factor
,
8393 struct isl_intersect_factor_control
*control
)
8397 isl_map
*other
, *product
;
8399 equal
= isl_map_has_equal_params(map
, factor
);
8403 map
= isl_map_align_params(map
, isl_map_get_space(factor
));
8404 factor
= isl_map_align_params(factor
, isl_map_get_space(map
));
8407 space
= isl_map_get_space(map
);
8408 other
= isl_map_universe(control
->other_factor(space
));
8409 product
= control
->product(factor
, other
);
8411 space
= isl_map_peek_space(map
);
8412 product
= isl_map_copy_tuple_id(product
, control
->preserve_type
,
8413 space
, control
->preserve_type
);
8414 return map_intersect(map
, product
);
8417 isl_map_free(factor
);
8421 /* Return the domain product of "map2" and "map1".
8423 static __isl_give isl_map
*isl_map_reverse_domain_product(
8424 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
8426 return isl_map_domain_product(map2
, map1
);
8429 /* Return the range product of "map2" and "map1".
8431 static __isl_give isl_map
*isl_map_reverse_range_product(
8432 __isl_take isl_map
*map1
, __isl_take isl_map
*map2
)
8434 return isl_map_range_product(map2
, map1
);
8437 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
8438 * in the space A -> C, return the intersection.
8440 __isl_give isl_map
*isl_map_intersect_domain_factor_domain(
8441 __isl_take isl_map
*map
, __isl_take isl_map
*factor
)
8443 struct isl_intersect_factor_control control
= {
8444 .preserve_type
= isl_dim_in
,
8445 .other_factor
= isl_space_domain_factor_range
,
8446 .product
= isl_map_domain_product
,
8449 return isl_map_intersect_factor(map
, factor
, &control
);
8452 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
8453 * in the space B -> C, return the intersection.
8455 __isl_give isl_map
*isl_map_intersect_domain_factor_range(
8456 __isl_take isl_map
*map
, __isl_take isl_map
*factor
)
8458 struct isl_intersect_factor_control control
= {
8459 .preserve_type
= isl_dim_in
,
8460 .other_factor
= isl_space_domain_factor_domain
,
8461 .product
= isl_map_reverse_domain_product
,
8464 return isl_map_intersect_factor(map
, factor
, &control
);
8467 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
8468 * in the space A -> B, return the intersection.
8470 __isl_give isl_map
*isl_map_intersect_range_factor_domain(
8471 __isl_take isl_map
*map
, __isl_take isl_map
*factor
)
8473 struct isl_intersect_factor_control control
= {
8474 .preserve_type
= isl_dim_out
,
8475 .other_factor
= isl_space_range_factor_range
,
8476 .product
= isl_map_range_product
,
8479 return isl_map_intersect_factor(map
, factor
, &control
);
8482 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
8483 * in the space A -> C, return the intersection.
8485 __isl_give isl_map
*isl_map_intersect_range_factor_range(
8486 __isl_take isl_map
*map
, __isl_take isl_map
*factor
)
8488 struct isl_intersect_factor_control control
= {
8489 .preserve_type
= isl_dim_out
,
8490 .other_factor
= isl_space_range_factor_domain
,
8491 .product
= isl_map_reverse_range_product
,
8494 return isl_map_intersect_factor(map
, factor
, &control
);
8497 /* Given a set "set" in a space [A -> B] and a set "domain"
8498 * in the space A, return the intersection.
8500 * The set "domain" is first extended to a set living in the space
8501 * [A -> B] and then a regular intersection is computed.
8503 __isl_give isl_set
*isl_set_intersect_factor_domain(__isl_take isl_set
*set
,
8504 __isl_take isl_set
*domain
)
8506 struct isl_intersect_factor_control control
= {
8507 .preserve_type
= isl_dim_set
,
8508 .other_factor
= isl_space_factor_range
,
8509 .product
= isl_map_range_product
,
8512 return set_from_map(isl_map_intersect_factor(set_to_map(set
),
8513 set_to_map(domain
), &control
));
8516 /* Given a set "set" in a space [A -> B] and a set "range"
8517 * in the space B, return the intersection.
8519 * The set "range" is first extended to a set living in the space
8520 * [A -> B] and then a regular intersection is computed.
8522 __isl_give isl_set
*isl_set_intersect_factor_range(__isl_take isl_set
*set
,
8523 __isl_take isl_set
*range
)
8525 struct isl_intersect_factor_control control
= {
8526 .preserve_type
= isl_dim_set
,
8527 .other_factor
= isl_space_factor_domain
,
8528 .product
= isl_map_reverse_range_product
,
8531 return set_from_map(isl_map_intersect_factor(set_to_map(set
),
8532 set_to_map(range
), &control
));
8535 __isl_give isl_map
*isl_map_apply_domain(__isl_take isl_map
*map1
,
8536 __isl_take isl_map
*map2
)
8538 if (isl_map_align_params_bin(&map1
, &map2
) < 0)
8540 map1
= isl_map_reverse(map1
);
8541 map1
= isl_map_apply_range(map1
, map2
);
8542 return isl_map_reverse(map1
);
8549 __isl_give isl_map
*isl_map_apply_range(__isl_take isl_map
*map1
,
8550 __isl_take isl_map
*map2
)
8553 struct isl_map
*result
;
8556 if (isl_map_align_params_bin(&map1
, &map2
) < 0)
8559 space
= isl_space_join(isl_space_copy(map1
->dim
),
8560 isl_space_copy(map2
->dim
));
8562 result
= isl_map_alloc_space(space
, map1
->n
* map2
->n
, 0);
8565 for (i
= 0; i
< map1
->n
; ++i
)
8566 for (j
= 0; j
< map2
->n
; ++j
) {
8567 result
= isl_map_add_basic_map(result
,
8568 isl_basic_map_apply_range(
8569 isl_basic_map_copy(map1
->p
[i
]),
8570 isl_basic_map_copy(map2
->p
[j
])));
8576 if (result
&& result
->n
<= 1)
8577 ISL_F_SET(result
, ISL_MAP_DISJOINT
);
8585 /* Is "bmap" a transformation, i.e.,
8586 * does it relate elements from the same space.
8588 isl_bool
isl_basic_map_is_transformation(__isl_keep isl_basic_map
*bmap
)
8592 space
= isl_basic_map_peek_space(bmap
);
8593 return isl_space_tuple_is_equal(space
, isl_dim_in
, space
, isl_dim_out
);
8596 /* Check that "bmap" is a transformation, i.e.,
8597 * that it relates elements from the same space.
8599 static isl_stat
isl_basic_map_check_transformation(
8600 __isl_keep isl_basic_map
*bmap
)
8604 equal
= isl_basic_map_is_transformation(bmap
);
8606 return isl_stat_error
;
8608 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
8609 "domain and range don't match", return isl_stat_error
);
8614 * returns range - domain
8616 __isl_give isl_basic_set
*isl_basic_map_deltas(__isl_take isl_basic_map
*bmap
)
8618 isl_space
*target_space
;
8619 struct isl_basic_set
*bset
;
8625 if (isl_basic_map_check_transformation(bmap
) < 0)
8626 return isl_basic_map_free(bmap
);
8627 dim
= isl_basic_map_dim(bmap
, isl_dim_in
);
8628 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
8629 if (dim
< 0 || nparam
< 0)
8631 target_space
= isl_space_domain(isl_basic_map_get_space(bmap
));
8632 bmap
= isl_basic_map_from_range(isl_basic_map_wrap(bmap
));
8633 bmap
= isl_basic_map_add_dims(bmap
, isl_dim_in
, dim
);
8634 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
8636 bmap
= isl_basic_map_free(bmap
);
8637 bmap
= isl_basic_map_extend_constraints(bmap
, dim
, 0);
8638 for (i
= 0; i
< dim
; ++i
) {
8639 int j
= isl_basic_map_alloc_equality(bmap
);
8641 bmap
= isl_basic_map_free(bmap
);
8644 isl_seq_clr(bmap
->eq
[j
], 1 + total
);
8645 isl_int_set_si(bmap
->eq
[j
][1+nparam
+i
], 1);
8646 isl_int_set_si(bmap
->eq
[j
][1+nparam
+dim
+i
], 1);
8647 isl_int_set_si(bmap
->eq
[j
][1+nparam
+2*dim
+i
], -1);
8649 bset
= isl_basic_map_domain(bmap
);
8650 bset
= isl_basic_set_reset_space(bset
, target_space
);
8653 isl_basic_map_free(bmap
);
8657 /* Is the tuple of type "type1" of "map" the same as
8658 * the tuple of type "type2" of "space"?
8660 isl_bool
isl_map_space_tuple_is_equal(__isl_keep isl_map
*map
,
8661 enum isl_dim_type type1
, __isl_keep isl_space
*space
,
8662 enum isl_dim_type type2
)
8664 isl_space
*map_space
;
8666 map_space
= isl_map_peek_space(map
);
8667 return isl_space_tuple_is_equal(map_space
, type1
, space
, type2
);
8670 /* Is the tuple of type "type1" of "map1" the same as
8671 * the tuple of type "type2" of "map2"?
8673 isl_bool
isl_map_tuple_is_equal(__isl_keep isl_map
*map1
,
8674 enum isl_dim_type type1
, __isl_keep isl_map
*map2
,
8675 enum isl_dim_type type2
)
8677 isl_space
*space1
, *space2
;
8679 space1
= isl_map_peek_space(map1
);
8680 space2
= isl_map_peek_space(map2
);
8681 return isl_space_tuple_is_equal(space1
, type1
, space2
, type2
);
8684 /* Is the space of "obj" equal to "space", ignoring parameters?
8686 isl_bool
isl_map_has_space_tuples(__isl_keep isl_map
*map
,
8687 __isl_keep isl_space
*space
)
8689 isl_space
*map_space
;
8691 map_space
= isl_map_peek_space(map
);
8692 return isl_space_has_equal_tuples(map_space
, space
);
8695 /* Check that "map" is a transformation, i.e.,
8696 * that it relates elements from the same space.
8698 isl_stat
isl_map_check_transformation(__isl_keep isl_map
*map
)
8702 equal
= isl_map_tuple_is_equal(map
, isl_dim_in
, map
, isl_dim_out
);
8704 return isl_stat_error
;
8706 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
8707 "domain and range don't match", return isl_stat_error
);
8712 * returns range - domain
8714 __isl_give isl_set
*isl_map_deltas(__isl_take isl_map
*map
)
8718 struct isl_set
*result
;
8720 if (isl_map_check_transformation(map
) < 0)
8722 space
= isl_map_get_space(map
);
8723 space
= isl_space_domain(space
);
8724 result
= isl_set_alloc_space(space
, map
->n
, 0);
8727 for (i
= 0; i
< map
->n
; ++i
)
8728 result
= isl_set_add_basic_set(result
,
8729 isl_basic_map_deltas(isl_basic_map_copy(map
->p
[i
])));
8738 * returns [domain -> range] -> range - domain
8740 __isl_give isl_basic_map
*isl_basic_map_deltas_map(
8741 __isl_take isl_basic_map
*bmap
)
8745 isl_basic_map
*domain
;
8749 if (isl_basic_map_check_transformation(bmap
) < 0)
8750 return isl_basic_map_free(bmap
);
8752 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
8753 n
= isl_basic_map_dim(bmap
, isl_dim_in
);
8754 if (nparam
< 0 || n
< 0)
8755 return isl_basic_map_free(bmap
);
8757 space
= isl_basic_map_get_space(bmap
);
8758 space
= isl_space_from_range(isl_space_domain(space
));
8759 domain
= isl_basic_map_universe(space
);
8761 bmap
= isl_basic_map_from_domain(isl_basic_map_wrap(bmap
));
8762 bmap
= isl_basic_map_apply_range(bmap
, domain
);
8763 bmap
= isl_basic_map_extend_constraints(bmap
, n
, 0);
8765 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
8767 return isl_basic_map_free(bmap
);
8769 for (i
= 0; i
< n
; ++i
) {
8770 k
= isl_basic_map_alloc_equality(bmap
);
8773 isl_seq_clr(bmap
->eq
[k
], 1 + total
);
8774 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ i
], 1);
8775 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ i
], -1);
8776 isl_int_set_si(bmap
->eq
[k
][1 + nparam
+ n
+ n
+ i
], 1);
8779 bmap
= isl_basic_map_gauss(bmap
, NULL
);
8780 return isl_basic_map_finalize(bmap
);
8782 isl_basic_map_free(bmap
);
8787 * returns [domain -> range] -> range - domain
8789 __isl_give isl_map
*isl_map_deltas_map(__isl_take isl_map
*map
)
8791 if (isl_map_check_transformation(map
) < 0)
8792 return isl_map_free(map
);
8794 return isl_map_transform(map
, &isl_space_range_map
,
8795 &isl_basic_map_deltas_map
);
8798 /* Return pairs of elements { x -> y } such that y - x is in "deltas".
8800 __isl_give isl_map
*isl_set_translation(__isl_take isl_set
*deltas
)
8805 space
= isl_space_map_from_set(isl_set_get_space(deltas
));
8806 map
= isl_map_deltas_map(isl_map_universe(space
));
8807 map
= isl_map_intersect_range(map
, deltas
);
8809 return isl_set_unwrap(isl_map_domain(map
));
8812 __isl_give isl_basic_map
*isl_basic_map_identity(__isl_take isl_space
*space
)
8814 isl_size n_in
, n_out
;
8816 n_in
= isl_space_dim(space
, isl_dim_in
);
8817 n_out
= isl_space_dim(space
, isl_dim_out
);
8818 if (n_in
< 0 || n_out
< 0)
8821 isl_die(space
->ctx
, isl_error_invalid
,
8822 "number of input and output dimensions needs to be "
8823 "the same", goto error
);
8824 return isl_basic_map_equal(space
, n_in
);
8826 isl_space_free(space
);
8830 __isl_give isl_map
*isl_map_identity(__isl_take isl_space
*space
)
8832 return isl_map_from_basic_map(isl_basic_map_identity(space
));
8835 __isl_give isl_map
*isl_set_identity(__isl_take isl_set
*set
)
8837 isl_space
*space
= isl_set_get_space(set
);
8839 id
= isl_map_identity(isl_space_map_from_set(space
));
8840 return isl_map_intersect_range(id
, set
);
8843 /* Construct a basic set with all set dimensions having only non-negative
8846 __isl_give isl_basic_set
*isl_basic_set_positive_orthant(
8847 __isl_take isl_space
*space
)
8853 struct isl_basic_set
*bset
;
8855 nparam
= isl_space_dim(space
, isl_dim_param
);
8856 dim
= isl_space_dim(space
, isl_dim_set
);
8857 total
= isl_space_dim(space
, isl_dim_all
);
8858 if (nparam
< 0 || dim
< 0 || total
< 0)
8859 space
= isl_space_free(space
);
8860 bset
= isl_basic_set_alloc_space(space
, 0, 0, dim
);
8863 for (i
= 0; i
< dim
; ++i
) {
8864 int k
= isl_basic_set_alloc_inequality(bset
);
8867 isl_seq_clr(bset
->ineq
[k
], 1 + total
);
8868 isl_int_set_si(bset
->ineq
[k
][1 + nparam
+ i
], 1);
8872 isl_basic_set_free(bset
);
8876 /* Construct the half-space x_pos >= 0.
8878 static __isl_give isl_basic_set
*nonneg_halfspace(__isl_take isl_space
*space
,
8883 isl_basic_set
*nonneg
;
8885 total
= isl_space_dim(space
, isl_dim_all
);
8887 space
= isl_space_free(space
);
8888 nonneg
= isl_basic_set_alloc_space(space
, 0, 0, 1);
8889 k
= isl_basic_set_alloc_inequality(nonneg
);
8892 isl_seq_clr(nonneg
->ineq
[k
], 1 + total
);
8893 isl_int_set_si(nonneg
->ineq
[k
][pos
], 1);
8895 return isl_basic_set_finalize(nonneg
);
8897 isl_basic_set_free(nonneg
);
8901 /* Construct the half-space x_pos <= -1.
8903 static __isl_give isl_basic_set
*neg_halfspace(__isl_take isl_space
*space
,
8910 total
= isl_space_dim(space
, isl_dim_all
);
8912 space
= isl_space_free(space
);
8913 neg
= isl_basic_set_alloc_space(space
, 0, 0, 1);
8914 k
= isl_basic_set_alloc_inequality(neg
);
8917 isl_seq_clr(neg
->ineq
[k
], 1 + total
);
8918 isl_int_set_si(neg
->ineq
[k
][0], -1);
8919 isl_int_set_si(neg
->ineq
[k
][pos
], -1);
8921 return isl_basic_set_finalize(neg
);
8923 isl_basic_set_free(neg
);
8927 __isl_give isl_set
*isl_set_split_dims(__isl_take isl_set
*set
,
8928 enum isl_dim_type type
, unsigned first
, unsigned n
)
8932 isl_basic_set
*nonneg
;
8938 if (isl_set_check_range(set
, type
, first
, n
) < 0)
8939 return isl_set_free(set
);
8941 offset
= pos(set
->dim
, type
);
8942 for (i
= 0; i
< n
; ++i
) {
8943 nonneg
= nonneg_halfspace(isl_set_get_space(set
),
8944 offset
+ first
+ i
);
8945 neg
= neg_halfspace(isl_set_get_space(set
), offset
+ first
+ i
);
8947 set
= isl_set_intersect(set
, isl_basic_set_union(nonneg
, neg
));
8953 static isl_stat
foreach_orthant(__isl_take isl_set
*set
, int *signs
, int first
,
8955 isl_stat (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
8961 return isl_stat_error
;
8962 if (isl_set_plain_is_empty(set
)) {
8967 return fn(set
, signs
, user
);
8970 half
= isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set
),
8972 half
= isl_set_intersect(half
, isl_set_copy(set
));
8973 if (foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
) < 0)
8977 half
= isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set
),
8979 half
= isl_set_intersect(half
, set
);
8980 return foreach_orthant(half
, signs
, first
+ 1, len
, fn
, user
);
8983 return isl_stat_error
;
8986 /* Call "fn" on the intersections of "set" with each of the orthants
8987 * (except for obviously empty intersections). The orthant is identified
8988 * by the signs array, with each entry having value 1 or -1 according
8989 * to the sign of the corresponding variable.
8991 isl_stat
isl_set_foreach_orthant(__isl_keep isl_set
*set
,
8992 isl_stat (*fn
)(__isl_take isl_set
*orthant
, int *signs
, void *user
),
9001 return isl_stat_error
;
9002 if (isl_set_plain_is_empty(set
))
9005 nparam
= isl_set_dim(set
, isl_dim_param
);
9006 nvar
= isl_set_dim(set
, isl_dim_set
);
9007 if (nparam
< 0 || nvar
< 0)
9008 return isl_stat_error
;
9010 signs
= isl_alloc_array(set
->ctx
, int, nparam
+ nvar
);
9012 r
= foreach_orthant(isl_set_copy(set
), signs
, 0, nparam
+ nvar
,
9020 isl_bool
isl_set_is_equal(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
9022 return isl_map_is_equal(set_to_map(set1
), set_to_map(set2
));
9025 isl_bool
isl_basic_map_is_subset(__isl_keep isl_basic_map
*bmap1
,
9026 __isl_keep isl_basic_map
*bmap2
)
9029 struct isl_map
*map1
;
9030 struct isl_map
*map2
;
9032 if (!bmap1
|| !bmap2
)
9033 return isl_bool_error
;
9035 map1
= isl_map_from_basic_map(isl_basic_map_copy(bmap1
));
9036 map2
= isl_map_from_basic_map(isl_basic_map_copy(bmap2
));
9038 is_subset
= isl_map_is_subset(map1
, map2
);
9046 isl_bool
isl_basic_set_is_subset(__isl_keep isl_basic_set
*bset1
,
9047 __isl_keep isl_basic_set
*bset2
)
9049 return isl_basic_map_is_subset(bset1
, bset2
);
9052 isl_bool
isl_basic_map_is_equal(__isl_keep isl_basic_map
*bmap1
,
9053 __isl_keep isl_basic_map
*bmap2
)
9057 if (!bmap1
|| !bmap2
)
9058 return isl_bool_error
;
9059 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
9060 if (is_subset
!= isl_bool_true
)
9062 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
9066 isl_bool
isl_basic_set_is_equal(__isl_keep isl_basic_set
*bset1
,
9067 __isl_keep isl_basic_set
*bset2
)
9069 return isl_basic_map_is_equal(
9070 bset_to_bmap(bset1
), bset_to_bmap(bset2
));
9073 isl_bool
isl_map_is_empty(__isl_keep isl_map
*map
)
9079 return isl_bool_error
;
9080 for (i
= 0; i
< map
->n
; ++i
) {
9081 is_empty
= isl_basic_map_is_empty(map
->p
[i
]);
9083 return isl_bool_error
;
9085 return isl_bool_false
;
9087 return isl_bool_true
;
9090 isl_bool
isl_map_plain_is_empty(__isl_keep isl_map
*map
)
9092 return map
? map
->n
== 0 : isl_bool_error
;
9095 isl_bool
isl_set_plain_is_empty(__isl_keep isl_set
*set
)
9097 return set
? set
->n
== 0 : isl_bool_error
;
9100 isl_bool
isl_set_is_empty(__isl_keep isl_set
*set
)
9102 return isl_map_is_empty(set_to_map(set
));
9106 #define TYPE isl_basic_map
9109 #include "isl_type_has_equal_space_bin_templ.c"
9110 #include "isl_type_check_equal_space_templ.c"
9112 /* Check that "bset1" and "bset2" live in the same space,
9113 * reporting an error if they do not.
9115 isl_stat
isl_basic_set_check_equal_space(__isl_keep isl_basic_set
*bset1
,
9116 __isl_keep isl_basic_set
*bset2
)
9118 return isl_basic_map_check_equal_space(bset_to_bmap(bset1
),
9119 bset_to_bmap(bset1
));
9123 #define TYPE isl_map
9125 #include "isl_type_has_equal_space_bin_templ.c"
9126 #include "isl_type_check_equal_space_templ.c"
9127 #include "isl_type_has_space_templ.c"
9129 isl_bool
isl_set_has_equal_space(__isl_keep isl_set
*set1
,
9130 __isl_keep isl_set
*set2
)
9132 return isl_map_has_equal_space(set_to_map(set1
), set_to_map(set2
));
9136 #define TYPE1 isl_map
9138 #define TYPE2 isl_basic_map
9140 #define TYPE_PAIR isl_map_basic_map
9143 #include "isl_type_has_equal_space_templ.c"
9144 #include "isl_type_check_equal_space_templ.c"
9146 /* Check that "set" and "bset" live in the same space,
9147 * reporting an error if they do not.
9149 isl_stat
isl_set_basic_set_check_equal_space(__isl_keep isl_set
*set
,
9150 __isl_keep isl_basic_set
*bset
)
9152 return isl_map_basic_map_check_equal_space(set_to_map(set
),
9153 bset_to_bmap(bset
));
9156 static isl_bool
map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
9161 return isl_bool_error
;
9162 is_subset
= isl_map_is_subset(map1
, map2
);
9163 if (is_subset
!= isl_bool_true
)
9165 is_subset
= isl_map_is_subset(map2
, map1
);
9169 /* Is "map1" equal to "map2"?
9171 * First check if they are obviously equal.
9172 * If not, then perform a more detailed analysis.
9174 isl_bool
isl_map_is_equal(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
9178 equal
= isl_map_plain_is_equal(map1
, map2
);
9179 if (equal
< 0 || equal
)
9181 return isl_map_align_params_map_map_and_test(map1
, map2
, &map_is_equal
);
9184 isl_bool
isl_basic_map_is_strict_subset(__isl_keep isl_basic_map
*bmap1
,
9185 __isl_keep isl_basic_map
*bmap2
)
9189 if (!bmap1
|| !bmap2
)
9190 return isl_bool_error
;
9191 is_subset
= isl_basic_map_is_subset(bmap1
, bmap2
);
9192 if (is_subset
!= isl_bool_true
)
9194 is_subset
= isl_basic_map_is_subset(bmap2
, bmap1
);
9195 return isl_bool_not(is_subset
);
9198 isl_bool
isl_map_is_strict_subset(__isl_keep isl_map
*map1
,
9199 __isl_keep isl_map
*map2
)
9204 return isl_bool_error
;
9205 is_subset
= isl_map_is_subset(map1
, map2
);
9206 if (is_subset
!= isl_bool_true
)
9208 is_subset
= isl_map_is_subset(map2
, map1
);
9209 return isl_bool_not(is_subset
);
9212 isl_bool
isl_set_is_strict_subset(__isl_keep isl_set
*set1
,
9213 __isl_keep isl_set
*set2
)
9215 return isl_map_is_strict_subset(set_to_map(set1
), set_to_map(set2
));
9218 /* Is "bmap" obviously equal to the universe with the same space?
9220 * That is, does it not have any constraints?
9222 isl_bool
isl_basic_map_plain_is_universe(__isl_keep isl_basic_map
*bmap
)
9225 return isl_bool_error
;
9226 return bmap
->n_eq
== 0 && bmap
->n_ineq
== 0;
9229 /* Is "bset" obviously equal to the universe with the same space?
9231 isl_bool
isl_basic_set_plain_is_universe(__isl_keep isl_basic_set
*bset
)
9233 return isl_basic_map_plain_is_universe(bset
);
9236 /* If "c" does not involve any existentially quantified variables,
9237 * then set *univ to false and abort
9239 static isl_stat
involves_divs(__isl_take isl_constraint
*c
, void *user
)
9241 isl_bool
*univ
= user
;
9244 n
= isl_constraint_dim(c
, isl_dim_div
);
9246 c
= isl_constraint_free(c
);
9247 *univ
= isl_constraint_involves_dims(c
, isl_dim_div
, 0, n
);
9248 isl_constraint_free(c
);
9249 if (*univ
< 0 || !*univ
)
9250 return isl_stat_error
;
9254 /* Is "bmap" equal to the universe with the same space?
9256 * First check if it is obviously equal to the universe.
9257 * If not and if there are any constraints not involving
9258 * existentially quantified variables, then it is certainly
9259 * not equal to the universe.
9260 * Otherwise, check if the universe is a subset of "bmap".
9262 isl_bool
isl_basic_map_is_universe(__isl_keep isl_basic_map
*bmap
)
9266 isl_basic_map
*test
;
9268 univ
= isl_basic_map_plain_is_universe(bmap
);
9269 if (univ
< 0 || univ
)
9271 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
9273 return isl_bool_error
;
9275 return isl_bool_false
;
9276 univ
= isl_bool_true
;
9277 if (isl_basic_map_foreach_constraint(bmap
, &involves_divs
, &univ
) < 0 &&
9279 return isl_bool_error
;
9280 if (univ
< 0 || !univ
)
9282 test
= isl_basic_map_universe(isl_basic_map_get_space(bmap
));
9283 univ
= isl_basic_map_is_subset(test
, bmap
);
9284 isl_basic_map_free(test
);
9288 /* Is "bset" equal to the universe with the same space?
9290 isl_bool
isl_basic_set_is_universe(__isl_keep isl_basic_set
*bset
)
9292 return isl_basic_map_is_universe(bset
);
9295 isl_bool
isl_map_plain_is_universe(__isl_keep isl_map
*map
)
9300 return isl_bool_error
;
9302 for (i
= 0; i
< map
->n
; ++i
) {
9303 isl_bool r
= isl_basic_map_plain_is_universe(map
->p
[i
]);
9308 return isl_bool_false
;
9311 isl_bool
isl_set_plain_is_universe(__isl_keep isl_set
*set
)
9313 return isl_map_plain_is_universe(set_to_map(set
));
9316 isl_bool
isl_basic_map_is_empty(__isl_keep isl_basic_map
*bmap
)
9318 struct isl_basic_set
*bset
= NULL
;
9319 struct isl_vec
*sample
= NULL
;
9320 isl_bool empty
, non_empty
;
9323 return isl_bool_error
;
9325 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
9326 return isl_bool_true
;
9328 if (isl_basic_map_plain_is_universe(bmap
))
9329 return isl_bool_false
;
9331 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
9332 struct isl_basic_map
*copy
= isl_basic_map_copy(bmap
);
9333 copy
= isl_basic_map_remove_redundancies(copy
);
9334 empty
= isl_basic_map_plain_is_empty(copy
);
9335 isl_basic_map_free(copy
);
9339 non_empty
= isl_basic_map_plain_is_non_empty(bmap
);
9341 return isl_bool_error
;
9343 return isl_bool_false
;
9344 isl_vec_free(bmap
->sample
);
9345 bmap
->sample
= NULL
;
9346 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
9348 return isl_bool_error
;
9349 sample
= isl_basic_set_sample_vec(bset
);
9351 return isl_bool_error
;
9352 empty
= sample
->size
== 0;
9353 isl_vec_free(bmap
->sample
);
9354 bmap
->sample
= sample
;
9356 ISL_F_SET(bmap
, ISL_BASIC_MAP_EMPTY
);
9361 isl_bool
isl_basic_map_plain_is_empty(__isl_keep isl_basic_map
*bmap
)
9364 return isl_bool_error
;
9365 return ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
);
9368 isl_bool
isl_basic_set_plain_is_empty(__isl_keep isl_basic_set
*bset
)
9371 return isl_bool_error
;
9372 return ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
);
9375 /* Is "bmap" known to be non-empty?
9377 * That is, is the cached sample still valid?
9379 isl_bool
isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map
*bmap
)
9384 return isl_bool_error
;
9386 return isl_bool_false
;
9387 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
9389 return isl_bool_error
;
9390 if (bmap
->sample
->size
!= 1 + total
)
9391 return isl_bool_false
;
9392 return isl_basic_map_contains(bmap
, bmap
->sample
);
9395 isl_bool
isl_basic_set_is_empty(__isl_keep isl_basic_set
*bset
)
9397 return isl_basic_map_is_empty(bset_to_bmap(bset
));
9400 __isl_give isl_map
*isl_basic_map_union(__isl_take isl_basic_map
*bmap1
,
9401 __isl_take isl_basic_map
*bmap2
)
9403 struct isl_map
*map
;
9405 if (isl_basic_map_check_equal_space(bmap1
, bmap2
) < 0)
9408 map
= isl_map_alloc_space(isl_space_copy(bmap1
->dim
), 2, 0);
9411 map
= isl_map_add_basic_map(map
, bmap1
);
9412 map
= isl_map_add_basic_map(map
, bmap2
);
9415 isl_basic_map_free(bmap1
);
9416 isl_basic_map_free(bmap2
);
9420 __isl_give isl_set
*isl_basic_set_union(__isl_take isl_basic_set
*bset1
,
9421 __isl_take isl_basic_set
*bset2
)
9423 return set_from_map(isl_basic_map_union(bset_to_bmap(bset1
),
9424 bset_to_bmap(bset2
)));
9427 /* Order divs such that any div only depends on previous divs */
9428 __isl_give isl_basic_map
*isl_basic_map_order_divs(
9429 __isl_take isl_basic_map
*bmap
)
9434 off
= isl_basic_map_var_offset(bmap
, isl_dim_div
);
9436 return isl_basic_map_free(bmap
);
9438 for (i
= 0; i
< bmap
->n_div
; ++i
) {
9440 if (isl_int_is_zero(bmap
->div
[i
][0]))
9442 pos
= isl_seq_first_non_zero(bmap
->div
[i
]+1+1+off
+i
,
9447 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_internal
,
9448 "integer division depends on itself",
9449 return isl_basic_map_free(bmap
));
9450 bmap
= isl_basic_map_swap_div(bmap
, i
, i
+ pos
);
9458 __isl_give isl_map
*isl_map_order_divs(__isl_take isl_map
*map
)
9465 for (i
= 0; i
< map
->n
; ++i
) {
9466 map
->p
[i
] = isl_basic_map_order_divs(map
->p
[i
]);
9477 /* Sort the local variables of "bset".
9479 __isl_give isl_basic_set
*isl_basic_set_sort_divs(
9480 __isl_take isl_basic_set
*bset
)
9482 return bset_from_bmap(isl_basic_map_sort_divs(bset_to_bmap(bset
)));
9485 /* Apply the expansion computed by isl_merge_divs.
9486 * The expansion itself is given by "exp" while the resulting
9487 * list of divs is given by "div".
9489 * Move the integer divisions of "bmap" into the right position
9490 * according to "exp" and then introduce the additional integer
9491 * divisions, adding div constraints.
9492 * The moving should be done first to avoid moving coefficients
9493 * in the definitions of the extra integer divisions.
9495 __isl_give isl_basic_map
*isl_basic_map_expand_divs(
9496 __isl_take isl_basic_map
*bmap
, __isl_take isl_mat
*div
, int *exp
)
9501 bmap
= isl_basic_map_cow(bmap
);
9505 if (div
->n_row
< bmap
->n_div
)
9506 isl_die(isl_mat_get_ctx(div
), isl_error_invalid
,
9507 "not an expansion", goto error
);
9509 n_div
= bmap
->n_div
;
9510 bmap
= isl_basic_map_extend(bmap
, div
->n_row
- n_div
, 0,
9511 2 * (div
->n_row
- n_div
));
9513 for (i
= n_div
; i
< div
->n_row
; ++i
)
9514 if (isl_basic_map_alloc_div(bmap
) < 0)
9517 for (j
= n_div
- 1; j
>= 0; --j
) {
9520 bmap
= isl_basic_map_swap_div(bmap
, j
, exp
[j
]);
9525 for (i
= 0; i
< div
->n_row
; ++i
) {
9526 if (j
< n_div
&& exp
[j
] == i
) {
9529 isl_seq_cpy(bmap
->div
[i
], div
->row
[i
], div
->n_col
);
9530 if (isl_basic_map_div_is_marked_unknown(bmap
, i
))
9532 bmap
= isl_basic_map_add_div_constraints(bmap
, i
);
9541 isl_basic_map_free(bmap
);
9546 /* Apply the expansion computed by isl_merge_divs.
9547 * The expansion itself is given by "exp" while the resulting
9548 * list of divs is given by "div".
9550 __isl_give isl_basic_set
*isl_basic_set_expand_divs(
9551 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*div
, int *exp
)
9553 return isl_basic_map_expand_divs(bset
, div
, exp
);
9556 /* Look for a div in dst that corresponds to the div "div" in src.
9557 * The divs before "div" in src and dst are assumed to be the same.
9559 * Return the position of the corresponding div in dst
9560 * if there is one. Otherwise, return a position beyond the integer divisions.
9561 * Return -1 on error.
9563 static int find_div(__isl_keep isl_basic_map
*dst
,
9564 __isl_keep isl_basic_map
*src
, unsigned div
)
9570 v_div
= isl_basic_map_var_offset(src
, isl_dim_div
);
9571 n_div
= isl_basic_map_dim(dst
, isl_dim_div
);
9572 if (n_div
< 0 || v_div
< 0)
9574 isl_assert(dst
->ctx
, div
<= n_div
, return -1);
9575 for (i
= div
; i
< n_div
; ++i
)
9576 if (isl_seq_eq(dst
->div
[i
], src
->div
[div
], 1+1+v_div
+div
) &&
9577 isl_seq_first_non_zero(dst
->div
[i
] + 1 + 1 + v_div
+ div
,
9583 /* Align the divs of "dst" to those of "src", adding divs from "src"
9584 * if needed. That is, make sure that the first src->n_div divs
9585 * of the result are equal to those of src.
9586 * The integer division of "src" are assumed to be ordered.
9588 * The integer divisions are swapped into the right position
9589 * (possibly after adding them first). This may result
9590 * in the remaining integer divisions appearing in the wrong order,
9591 * i.e., with some integer division appearing before
9592 * some other integer division on which it depends.
9593 * The integer divisions therefore need to be ordered.
9594 * This will not affect the integer divisions aligned to those of "src",
9595 * since "src" is assumed to have ordered integer divisions.
9597 * The result is not finalized as by design it will have redundant
9598 * divs if any divs from "src" were copied.
9600 __isl_give isl_basic_map
*isl_basic_map_align_divs(
9601 __isl_take isl_basic_map
*dst
, __isl_keep isl_basic_map
*src
)
9610 return isl_basic_map_free(dst
);
9612 if (src
->n_div
== 0)
9615 known
= isl_basic_map_divs_known(src
);
9617 return isl_basic_map_free(dst
);
9619 isl_die(isl_basic_map_get_ctx(src
), isl_error_invalid
,
9620 "some src divs are unknown",
9621 return isl_basic_map_free(dst
));
9623 v_div
= isl_basic_map_var_offset(src
, isl_dim_div
);
9625 return isl_basic_map_free(dst
);
9628 dst_n_div
= isl_basic_map_dim(dst
, isl_dim_div
);
9630 dst
= isl_basic_map_free(dst
);
9631 for (i
= 0; i
< src
->n_div
; ++i
) {
9632 int j
= find_div(dst
, src
, i
);
9634 dst
= isl_basic_map_free(dst
);
9635 if (j
== dst_n_div
) {
9637 int extra
= src
->n_div
- i
;
9638 dst
= isl_basic_map_cow(dst
);
9640 return isl_basic_map_free(dst
);
9641 dst
= isl_basic_map_extend(dst
,
9642 extra
, 0, 2 * extra
);
9645 j
= isl_basic_map_alloc_div(dst
);
9647 return isl_basic_map_free(dst
);
9648 isl_seq_cpy(dst
->div
[j
], src
->div
[i
], 1+1+v_div
+i
);
9649 isl_seq_clr(dst
->div
[j
]+1+1+v_div
+i
, dst
->n_div
- i
);
9651 dst
= isl_basic_map_add_div_constraints(dst
, j
);
9653 return isl_basic_map_free(dst
);
9656 dst
= isl_basic_map_swap_div(dst
, i
, j
);
9658 return isl_basic_map_free(dst
);
9660 return isl_basic_map_order_divs(dst
);
9663 __isl_give isl_map
*isl_map_align_divs_internal(__isl_take isl_map
*map
)
9671 map
= isl_map_compute_divs(map
);
9672 map
= isl_map_order_divs(map
);
9673 map
= isl_map_cow(map
);
9677 for (i
= 1; i
< map
->n
; ++i
)
9678 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], map
->p
[i
]);
9679 for (i
= 1; i
< map
->n
; ++i
) {
9680 map
->p
[i
] = isl_basic_map_align_divs(map
->p
[i
], map
->p
[0]);
9682 return isl_map_free(map
);
9685 map
= isl_map_unmark_normalized(map
);
9689 __isl_give isl_map
*isl_map_align_divs(__isl_take isl_map
*map
)
9691 return isl_map_align_divs_internal(map
);
9694 __isl_give isl_set
*isl_set_align_divs(__isl_take isl_set
*set
)
9696 return set_from_map(isl_map_align_divs_internal(set_to_map(set
)));
9699 /* Align the divs of the basic maps in "map" to those
9700 * of the basic maps in "list", as well as to the other basic maps in "map".
9701 * The elements in "list" are assumed to have known divs.
9703 __isl_give isl_map
*isl_map_align_divs_to_basic_map_list(
9704 __isl_take isl_map
*map
, __isl_keep isl_basic_map_list
*list
)
9709 n
= isl_basic_map_list_n_basic_map(list
);
9710 map
= isl_map_compute_divs(map
);
9711 map
= isl_map_cow(map
);
9713 return isl_map_free(map
);
9717 for (i
= 0; i
< n
; ++i
) {
9718 isl_basic_map
*bmap
;
9720 bmap
= isl_basic_map_list_get_basic_map(list
, i
);
9721 bmap
= isl_basic_map_order_divs(bmap
);
9722 map
->p
[0] = isl_basic_map_align_divs(map
->p
[0], bmap
);
9723 isl_basic_map_free(bmap
);
9726 return isl_map_free(map
);
9728 return isl_map_align_divs_internal(map
);
9731 /* Align the divs of each element of "list" to those of "bmap".
9732 * Both "bmap" and the elements of "list" are assumed to have known divs.
9734 __isl_give isl_basic_map_list
*isl_basic_map_list_align_divs_to_basic_map(
9735 __isl_take isl_basic_map_list
*list
, __isl_keep isl_basic_map
*bmap
)
9740 n
= isl_basic_map_list_n_basic_map(list
);
9742 return isl_basic_map_list_free(list
);
9744 for (i
= 0; i
< n
; ++i
) {
9745 isl_basic_map
*bmap_i
;
9747 bmap_i
= isl_basic_map_list_get_basic_map(list
, i
);
9748 bmap_i
= isl_basic_map_align_divs(bmap_i
, bmap
);
9749 list
= isl_basic_map_list_set_basic_map(list
, i
, bmap_i
);
9755 __isl_give isl_set
*isl_set_apply( __isl_take isl_set
*set
,
9756 __isl_take isl_map
*map
)
9760 isl_map_align_params_set(&map
, &set
);
9761 ok
= isl_map_compatible_domain(map
, set
);
9765 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
9766 "incompatible spaces", goto error
);
9767 map
= isl_map_intersect_domain(map
, set
);
9768 set
= isl_map_range(map
);
9776 /* There is no need to cow as removing empty parts doesn't change
9777 * the meaning of the set.
9779 __isl_give isl_map
*isl_map_remove_empty_parts(__isl_take isl_map
*map
)
9786 for (i
= map
->n
- 1; i
>= 0; --i
)
9787 map
= remove_if_empty(map
, i
);
9792 __isl_give isl_set
*isl_set_remove_empty_parts(__isl_take isl_set
*set
)
9794 return set_from_map(isl_map_remove_empty_parts(set_to_map(set
)));
9797 /* Create a binary relation that maps the shared initial "pos" dimensions
9798 * of "bset1" and "bset2" to the remaining dimensions of "bset1" and "bset2".
9800 static __isl_give isl_basic_map
*join_initial(__isl_keep isl_basic_set
*bset1
,
9801 __isl_keep isl_basic_set
*bset2
, int pos
)
9803 isl_basic_map
*bmap1
;
9804 isl_basic_map
*bmap2
;
9806 bmap1
= isl_basic_map_from_range(isl_basic_set_copy(bset1
));
9807 bmap2
= isl_basic_map_from_range(isl_basic_set_copy(bset2
));
9808 bmap1
= isl_basic_map_move_dims(bmap1
, isl_dim_in
, 0,
9809 isl_dim_out
, 0, pos
);
9810 bmap2
= isl_basic_map_move_dims(bmap2
, isl_dim_in
, 0,
9811 isl_dim_out
, 0, pos
);
9812 return isl_basic_map_range_product(bmap1
, bmap2
);
9815 /* Given two basic sets bset1 and bset2, compute the maximal difference
9816 * between the values of dimension pos in bset1 and those in bset2
9817 * for any common value of the parameters and dimensions preceding pos.
9819 static enum isl_lp_result
basic_set_maximal_difference_at(
9820 __isl_keep isl_basic_set
*bset1
, __isl_keep isl_basic_set
*bset2
,
9821 int pos
, isl_int
*opt
)
9823 isl_basic_map
*bmap1
;
9824 struct isl_ctx
*ctx
;
9825 struct isl_vec
*obj
;
9829 enum isl_lp_result res
;
9831 nparam
= isl_basic_set_dim(bset1
, isl_dim_param
);
9832 dim1
= isl_basic_set_dim(bset1
, isl_dim_set
);
9833 if (nparam
< 0 || dim1
< 0 || !bset2
)
9834 return isl_lp_error
;
9836 bmap1
= join_initial(bset1
, bset2
, pos
);
9837 total
= isl_basic_map_dim(bmap1
, isl_dim_all
);
9839 return isl_lp_error
;
9842 obj
= isl_vec_alloc(ctx
, 1 + total
);
9845 isl_seq_clr(obj
->block
.data
, 1 + total
);
9846 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
], 1);
9847 isl_int_set_si(obj
->block
.data
[1+nparam
+pos
+(dim1
-pos
)], -1);
9848 res
= isl_basic_map_solve_lp(bmap1
, 1, obj
->block
.data
, ctx
->one
,
9850 isl_basic_map_free(bmap1
);
9854 isl_basic_map_free(bmap1
);
9855 return isl_lp_error
;
9858 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
9859 * for any common value of the parameters and dimensions preceding pos
9860 * in both basic sets, the values of dimension pos in bset1 are
9861 * smaller or larger than those in bset2.
9864 * 1 if bset1 follows bset2
9865 * -1 if bset1 precedes bset2
9866 * 0 if bset1 and bset2 are incomparable
9867 * -2 if some error occurred.
9869 int isl_basic_set_compare_at(__isl_keep isl_basic_set
*bset1
,
9870 __isl_keep isl_basic_set
*bset2
, int pos
)
9873 enum isl_lp_result res
;
9878 res
= basic_set_maximal_difference_at(bset1
, bset2
, pos
, &opt
);
9880 if (res
== isl_lp_empty
)
9882 else if ((res
== isl_lp_ok
&& isl_int_is_pos(opt
)) ||
9883 res
== isl_lp_unbounded
)
9885 else if (res
== isl_lp_ok
&& isl_int_is_neg(opt
))
9894 /* Given two basic sets bset1 and bset2, check whether
9895 * for any common value of the parameters and dimensions preceding pos
9896 * there is a value of dimension pos in bset1 that is larger
9897 * than a value of the same dimension in bset2.
9900 * 1 if there exists such a pair
9901 * 0 if there is no such pair, but there is a pair of equal values
9903 * -2 if some error occurred.
9905 int isl_basic_set_follows_at(__isl_keep isl_basic_set
*bset1
,
9906 __isl_keep isl_basic_set
*bset2
, int pos
)
9909 isl_basic_map
*bmap
;
9912 dim1
= isl_basic_set_dim(bset1
, isl_dim_set
);
9915 bmap
= join_initial(bset1
, bset2
, pos
);
9916 bmap
= isl_basic_map_order_ge(bmap
, isl_dim_out
, 0,
9917 isl_dim_out
, dim1
- pos
);
9918 empty
= isl_basic_map_is_empty(bmap
);
9922 isl_basic_map_free(bmap
);
9925 bmap
= isl_basic_map_order_gt(bmap
, isl_dim_out
, 0,
9926 isl_dim_out
, dim1
- pos
);
9927 empty
= isl_basic_map_is_empty(bmap
);
9930 isl_basic_map_free(bmap
);
9935 isl_basic_map_free(bmap
);
9939 /* Given two sets set1 and set2, check whether
9940 * for any common value of the parameters and dimensions preceding pos
9941 * there is a value of dimension pos in set1 that is larger
9942 * than a value of the same dimension in set2.
9945 * 1 if there exists such a pair
9946 * 0 if there is no such pair, but there is a pair of equal values
9948 * -2 if some error occurred.
9950 int isl_set_follows_at(__isl_keep isl_set
*set1
,
9951 __isl_keep isl_set
*set2
, int pos
)
9959 for (i
= 0; i
< set1
->n
; ++i
)
9960 for (j
= 0; j
< set2
->n
; ++j
) {
9962 f
= isl_basic_set_follows_at(set1
->p
[i
], set2
->p
[j
], pos
);
9963 if (f
== 1 || f
== -2)
9972 static isl_bool
isl_basic_map_plain_has_fixed_var(
9973 __isl_keep isl_basic_map
*bmap
, unsigned pos
, isl_int
*val
)
9979 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
9981 return isl_bool_error
;
9982 for (i
= 0, d
= total
-1; i
< bmap
->n_eq
&& d
+1 > pos
; ++i
) {
9983 for (; d
+1 > pos
; --d
)
9984 if (!isl_int_is_zero(bmap
->eq
[i
][1+d
]))
9988 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1, d
) != -1)
9989 return isl_bool_false
;
9990 if (isl_seq_first_non_zero(bmap
->eq
[i
]+1+d
+1, total
-d
-1) != -1)
9991 return isl_bool_false
;
9992 if (!isl_int_is_one(bmap
->eq
[i
][1+d
]))
9993 return isl_bool_false
;
9995 isl_int_neg(*val
, bmap
->eq
[i
][0]);
9996 return isl_bool_true
;
9998 return isl_bool_false
;
10001 static isl_bool
isl_map_plain_has_fixed_var(__isl_keep isl_map
*map
,
10002 unsigned pos
, isl_int
*val
)
10010 return isl_bool_error
;
10012 return isl_bool_false
;
10014 return isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, val
);
10017 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[0], pos
, &v
);
10018 for (i
= 1; fixed
== isl_bool_true
&& i
< map
->n
; ++i
) {
10019 fixed
= isl_basic_map_plain_has_fixed_var(map
->p
[i
], pos
, &tmp
);
10020 if (fixed
== isl_bool_true
&& isl_int_ne(tmp
, v
))
10021 fixed
= isl_bool_false
;
10024 isl_int_set(*val
, v
);
10025 isl_int_clear(tmp
);
10030 static isl_bool
isl_basic_set_plain_has_fixed_var(
10031 __isl_keep isl_basic_set
*bset
, unsigned pos
, isl_int
*val
)
10033 return isl_basic_map_plain_has_fixed_var(bset_to_bmap(bset
),
10037 isl_bool
isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map
*bmap
,
10038 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
10040 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
10041 return isl_bool_error
;
10042 return isl_basic_map_plain_has_fixed_var(bmap
,
10043 isl_basic_map_offset(bmap
, type
) - 1 + pos
, val
);
10046 /* If "bmap" obviously lies on a hyperplane where the given dimension
10047 * has a fixed value, then return that value.
10048 * Otherwise return NaN.
10050 __isl_give isl_val
*isl_basic_map_plain_get_val_if_fixed(
10051 __isl_keep isl_basic_map
*bmap
,
10052 enum isl_dim_type type
, unsigned pos
)
10060 ctx
= isl_basic_map_get_ctx(bmap
);
10061 v
= isl_val_alloc(ctx
);
10064 fixed
= isl_basic_map_plain_is_fixed(bmap
, type
, pos
, &v
->n
);
10066 return isl_val_free(v
);
10068 isl_int_set_si(v
->d
, 1);
10072 return isl_val_nan(ctx
);
10075 isl_bool
isl_map_plain_is_fixed(__isl_keep isl_map
*map
,
10076 enum isl_dim_type type
, unsigned pos
, isl_int
*val
)
10078 if (isl_map_check_range(map
, type
, pos
, 1) < 0)
10079 return isl_bool_error
;
10080 return isl_map_plain_has_fixed_var(map
,
10081 map_offset(map
, type
) - 1 + pos
, val
);
10084 /* If "map" obviously lies on a hyperplane where the given dimension
10085 * has a fixed value, then return that value.
10086 * Otherwise return NaN.
10088 __isl_give isl_val
*isl_map_plain_get_val_if_fixed(__isl_keep isl_map
*map
,
10089 enum isl_dim_type type
, unsigned pos
)
10097 ctx
= isl_map_get_ctx(map
);
10098 v
= isl_val_alloc(ctx
);
10101 fixed
= isl_map_plain_is_fixed(map
, type
, pos
, &v
->n
);
10103 return isl_val_free(v
);
10105 isl_int_set_si(v
->d
, 1);
10109 return isl_val_nan(ctx
);
10112 /* If "set" obviously lies on a hyperplane where the given dimension
10113 * has a fixed value, then return that value.
10114 * Otherwise return NaN.
10116 __isl_give isl_val
*isl_set_plain_get_val_if_fixed(__isl_keep isl_set
*set
,
10117 enum isl_dim_type type
, unsigned pos
)
10119 return isl_map_plain_get_val_if_fixed(set
, type
, pos
);
10122 /* Return a sequence of values in the same space as "set"
10123 * that are equal to the corresponding set dimensions of "set"
10124 * for those set dimensions that obviously lie on a hyperplane
10125 * where the dimension has a fixed value.
10126 * The other elements are set to NaN.
10128 __isl_give isl_multi_val
*isl_set_get_plain_multi_val_if_fixed(
10129 __isl_keep isl_set
*set
)
10136 space
= isl_space_drop_all_params(isl_set_get_space(set
));
10137 mv
= isl_multi_val_alloc(space
);
10138 n
= isl_multi_val_size(mv
);
10140 return isl_multi_val_free(mv
);
10142 for (i
= 0; i
< n
; ++i
) {
10145 v
= isl_set_plain_get_val_if_fixed(set
, isl_dim_set
, i
);
10146 mv
= isl_multi_val_set_val(mv
, i
, v
);
10152 /* Check if dimension dim has fixed value and if so and if val is not NULL,
10153 * then return this fixed value in *val.
10155 isl_bool
isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set
*bset
,
10156 unsigned dim
, isl_int
*val
)
10160 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
10162 return isl_bool_error
;
10163 return isl_basic_set_plain_has_fixed_var(bset
, nparam
+ dim
, val
);
10166 /* Return -1 if the constraint "c1" should be sorted before "c2"
10167 * and 1 if it should be sorted after "c2".
10168 * Return 0 if the two constraints are the same (up to the constant term).
10170 * In particular, if a constraint involves later variables than another
10171 * then it is sorted after this other constraint.
10172 * uset_gist depends on constraints without existentially quantified
10173 * variables sorting first.
10175 * For constraints that have the same latest variable, those
10176 * with the same coefficient for this latest variable (first in absolute value
10177 * and then in actual value) are grouped together.
10178 * This is useful for detecting pairs of constraints that can
10179 * be chained in their printed representation.
10181 * Finally, within a group, constraints are sorted according to
10182 * their coefficients (excluding the constant term).
10184 static int sort_constraint_cmp(const void *p1
, const void *p2
, void *arg
)
10186 isl_int
**c1
= (isl_int
**) p1
;
10187 isl_int
**c2
= (isl_int
**) p2
;
10189 unsigned size
= *(unsigned *) arg
;
10192 l1
= isl_seq_last_non_zero(*c1
+ 1, size
);
10193 l2
= isl_seq_last_non_zero(*c2
+ 1, size
);
10198 cmp
= isl_int_abs_cmp((*c1
)[1 + l1
], (*c2
)[1 + l1
]);
10201 cmp
= isl_int_cmp((*c1
)[1 + l1
], (*c2
)[1 + l1
]);
10205 return isl_seq_cmp(*c1
+ 1, *c2
+ 1, size
);
10208 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
10209 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
10210 * and 0 if the two constraints are the same (up to the constant term).
10212 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map
*bmap
,
10213 isl_int
*c1
, isl_int
*c2
)
10218 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
10222 return sort_constraint_cmp(&c1
, &c2
, &size
);
10225 __isl_give isl_basic_map
*isl_basic_map_sort_constraints(
10226 __isl_take isl_basic_map
*bmap
)
10233 if (bmap
->n_ineq
== 0)
10235 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_SORTED
))
10237 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
10239 return isl_basic_map_free(bmap
);
10241 if (isl_sort(bmap
->ineq
, bmap
->n_ineq
, sizeof(isl_int
*),
10242 &sort_constraint_cmp
, &size
) < 0)
10243 return isl_basic_map_free(bmap
);
10244 ISL_F_SET(bmap
, ISL_BASIC_MAP_SORTED
);
10248 __isl_give isl_basic_set
*isl_basic_set_sort_constraints(
10249 __isl_take isl_basic_set
*bset
)
10251 isl_basic_map
*bmap
= bset_to_bmap(bset
);
10252 return bset_from_bmap(isl_basic_map_sort_constraints(bmap
));
10255 __isl_give isl_basic_map
*isl_basic_map_normalize(
10256 __isl_take isl_basic_map
*bmap
)
10258 bmap
= isl_basic_map_remove_redundancies(bmap
);
10259 bmap
= isl_basic_map_sort_constraints(bmap
);
10262 int isl_basic_map_plain_cmp(__isl_keep isl_basic_map
*bmap1
,
10263 __isl_keep isl_basic_map
*bmap2
)
10267 isl_space
*space1
, *space2
;
10269 if (!bmap1
|| !bmap2
)
10272 if (bmap1
== bmap2
)
10274 space1
= isl_basic_map_peek_space(bmap1
);
10275 space2
= isl_basic_map_peek_space(bmap2
);
10276 cmp
= isl_space_cmp(space1
, space2
);
10279 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) !=
10280 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_RATIONAL
))
10281 return ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_RATIONAL
) ? -1 : 1;
10282 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
) &&
10283 ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
10285 if (ISL_F_ISSET(bmap1
, ISL_BASIC_MAP_EMPTY
))
10287 if (ISL_F_ISSET(bmap2
, ISL_BASIC_MAP_EMPTY
))
10289 if (bmap1
->n_eq
!= bmap2
->n_eq
)
10290 return bmap1
->n_eq
- bmap2
->n_eq
;
10291 if (bmap1
->n_ineq
!= bmap2
->n_ineq
)
10292 return bmap1
->n_ineq
- bmap2
->n_ineq
;
10293 if (bmap1
->n_div
!= bmap2
->n_div
)
10294 return bmap1
->n_div
- bmap2
->n_div
;
10295 total
= isl_basic_map_dim(bmap1
, isl_dim_all
);
10298 for (i
= 0; i
< bmap1
->n_eq
; ++i
) {
10299 cmp
= isl_seq_cmp(bmap1
->eq
[i
], bmap2
->eq
[i
], 1+total
);
10303 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
10304 cmp
= isl_seq_cmp(bmap1
->ineq
[i
], bmap2
->ineq
[i
], 1+total
);
10308 for (i
= 0; i
< bmap1
->n_div
; ++i
) {
10309 cmp
= isl_seq_cmp(bmap1
->div
[i
], bmap2
->div
[i
], 1+1+total
);
10316 int isl_basic_set_plain_cmp(__isl_keep isl_basic_set
*bset1
,
10317 __isl_keep isl_basic_set
*bset2
)
10319 return isl_basic_map_plain_cmp(bset1
, bset2
);
10322 int isl_set_plain_cmp(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
10328 if (set1
->n
!= set2
->n
)
10329 return set1
->n
- set2
->n
;
10331 for (i
= 0; i
< set1
->n
; ++i
) {
10332 cmp
= isl_basic_set_plain_cmp(set1
->p
[i
], set2
->p
[i
]);
10340 isl_bool
isl_basic_map_plain_is_equal(__isl_keep isl_basic_map
*bmap1
,
10341 __isl_keep isl_basic_map
*bmap2
)
10343 if (!bmap1
|| !bmap2
)
10344 return isl_bool_error
;
10345 return isl_basic_map_plain_cmp(bmap1
, bmap2
) == 0;
10348 isl_bool
isl_basic_set_plain_is_equal(__isl_keep isl_basic_set
*bset1
,
10349 __isl_keep isl_basic_set
*bset2
)
10351 return isl_basic_map_plain_is_equal(bset_to_bmap(bset1
),
10352 bset_to_bmap(bset2
));
10355 static int qsort_bmap_cmp(const void *p1
, const void *p2
)
10357 isl_basic_map
*bmap1
= *(isl_basic_map
**) p1
;
10358 isl_basic_map
*bmap2
= *(isl_basic_map
**) p2
;
10360 return isl_basic_map_plain_cmp(bmap1
, bmap2
);
10363 /* Sort the basic maps of "map" and remove duplicate basic maps.
10365 * While removing basic maps, we make sure that the basic maps remain
10366 * sorted because isl_map_normalize expects the basic maps of the result
10369 static __isl_give isl_map
*sort_and_remove_duplicates(__isl_take isl_map
*map
)
10373 map
= isl_map_remove_empty_parts(map
);
10376 qsort(map
->p
, map
->n
, sizeof(struct isl_basic_map
*), qsort_bmap_cmp
);
10377 for (i
= map
->n
- 1; i
>= 1; --i
) {
10378 if (!isl_basic_map_plain_is_equal(map
->p
[i
- 1], map
->p
[i
]))
10380 isl_basic_map_free(map
->p
[i
-1]);
10381 for (j
= i
; j
< map
->n
; ++j
)
10382 map
->p
[j
- 1] = map
->p
[j
];
10389 /* Remove obvious duplicates among the basic maps of "map".
10391 * Unlike isl_map_normalize, this function does not remove redundant
10392 * constraints and only removes duplicates that have exactly the same
10393 * constraints in the input. It does sort the constraints and
10394 * the basic maps to ease the detection of duplicates.
10396 * If "map" has already been normalized or if the basic maps are
10397 * disjoint, then there can be no duplicates.
10399 __isl_give isl_map
*isl_map_remove_obvious_duplicates(__isl_take isl_map
*map
)
10402 isl_basic_map
*bmap
;
10408 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
| ISL_MAP_DISJOINT
))
10410 for (i
= 0; i
< map
->n
; ++i
) {
10411 bmap
= isl_basic_map_copy(map
->p
[i
]);
10412 bmap
= isl_basic_map_sort_constraints(bmap
);
10414 return isl_map_free(map
);
10415 isl_basic_map_free(map
->p
[i
]);
10419 map
= sort_and_remove_duplicates(map
);
10423 /* We normalize in place, but if anything goes wrong we need
10424 * to return NULL, so we need to make sure we don't change the
10425 * meaning of any possible other copies of map.
10427 __isl_give isl_map
*isl_map_normalize(__isl_take isl_map
*map
)
10430 struct isl_basic_map
*bmap
;
10434 if (ISL_F_ISSET(map
, ISL_MAP_NORMALIZED
))
10436 for (i
= 0; i
< map
->n
; ++i
) {
10437 bmap
= isl_basic_map_normalize(isl_basic_map_copy(map
->p
[i
]));
10440 isl_basic_map_free(map
->p
[i
]);
10444 map
= sort_and_remove_duplicates(map
);
10446 ISL_F_SET(map
, ISL_MAP_NORMALIZED
);
10453 __isl_give isl_set
*isl_set_normalize(__isl_take isl_set
*set
)
10455 return set_from_map(isl_map_normalize(set_to_map(set
)));
10458 isl_bool
isl_map_plain_is_equal(__isl_keep isl_map
*map1
,
10459 __isl_keep isl_map
*map2
)
10464 if (!map1
|| !map2
)
10465 return isl_bool_error
;
10468 return isl_bool_true
;
10469 equal
= isl_map_has_equal_space(map1
, map2
);
10470 if (equal
< 0 || !equal
)
10473 map1
= isl_map_copy(map1
);
10474 map2
= isl_map_copy(map2
);
10475 map1
= isl_map_normalize(map1
);
10476 map2
= isl_map_normalize(map2
);
10477 if (!map1
|| !map2
)
10479 equal
= map1
->n
== map2
->n
;
10480 for (i
= 0; equal
&& i
< map1
->n
; ++i
) {
10481 equal
= isl_basic_map_plain_is_equal(map1
->p
[i
], map2
->p
[i
]);
10485 isl_map_free(map1
);
10486 isl_map_free(map2
);
10489 isl_map_free(map1
);
10490 isl_map_free(map2
);
10491 return isl_bool_error
;
10494 isl_bool
isl_set_plain_is_equal(__isl_keep isl_set
*set1
,
10495 __isl_keep isl_set
*set2
)
10497 return isl_map_plain_is_equal(set_to_map(set1
), set_to_map(set2
));
10500 /* Return the basic maps in "map" as a list.
10502 __isl_give isl_basic_map_list
*isl_map_get_basic_map_list(
10503 __isl_keep isl_map
*map
)
10507 isl_basic_map_list
*list
;
10511 ctx
= isl_map_get_ctx(map
);
10512 list
= isl_basic_map_list_alloc(ctx
, map
->n
);
10514 for (i
= 0; i
< map
->n
; ++i
) {
10515 isl_basic_map
*bmap
;
10517 bmap
= isl_basic_map_copy(map
->p
[i
]);
10518 list
= isl_basic_map_list_add(list
, bmap
);
10524 /* Return the intersection of the elements in the non-empty list "list".
10525 * All elements are assumed to live in the same space.
10527 __isl_give isl_basic_map
*isl_basic_map_list_intersect(
10528 __isl_take isl_basic_map_list
*list
)
10532 isl_basic_map
*bmap
;
10534 n
= isl_basic_map_list_n_basic_map(list
);
10538 isl_die(isl_basic_map_list_get_ctx(list
), isl_error_invalid
,
10539 "expecting non-empty list", goto error
);
10541 bmap
= isl_basic_map_list_get_basic_map(list
, 0);
10542 for (i
= 1; i
< n
; ++i
) {
10543 isl_basic_map
*bmap_i
;
10545 bmap_i
= isl_basic_map_list_get_basic_map(list
, i
);
10546 bmap
= isl_basic_map_intersect(bmap
, bmap_i
);
10549 isl_basic_map_list_free(list
);
10552 isl_basic_map_list_free(list
);
10556 /* Return the intersection of the elements in the non-empty list "list".
10557 * All elements are assumed to live in the same space.
10559 __isl_give isl_basic_set
*isl_basic_set_list_intersect(
10560 __isl_take isl_basic_set_list
*list
)
10562 return isl_basic_map_list_intersect(list
);
10565 /* Return the union of the elements of "list".
10566 * The list is required to have at least one element.
10568 __isl_give isl_set
*isl_basic_set_list_union(
10569 __isl_take isl_basic_set_list
*list
)
10574 isl_basic_set
*bset
;
10577 n
= isl_basic_set_list_n_basic_set(list
);
10581 isl_die(isl_basic_set_list_get_ctx(list
), isl_error_invalid
,
10582 "expecting non-empty list", goto error
);
10584 bset
= isl_basic_set_list_get_basic_set(list
, 0);
10585 space
= isl_basic_set_get_space(bset
);
10586 isl_basic_set_free(bset
);
10588 set
= isl_set_alloc_space(space
, n
, 0);
10589 for (i
= 0; i
< n
; ++i
) {
10590 bset
= isl_basic_set_list_get_basic_set(list
, i
);
10591 set
= isl_set_add_basic_set(set
, bset
);
10594 isl_basic_set_list_free(list
);
10597 isl_basic_set_list_free(list
);
10601 /* Return the union of the elements in the non-empty list "list".
10602 * All elements are assumed to live in the same space.
10604 __isl_give isl_set
*isl_set_list_union(__isl_take isl_set_list
*list
)
10610 n
= isl_set_list_n_set(list
);
10614 isl_die(isl_set_list_get_ctx(list
), isl_error_invalid
,
10615 "expecting non-empty list", goto error
);
10617 set
= isl_set_list_get_set(list
, 0);
10618 for (i
= 1; i
< n
; ++i
) {
10621 set_i
= isl_set_list_get_set(list
, i
);
10622 set
= isl_set_union(set
, set_i
);
10625 isl_set_list_free(list
);
10628 isl_set_list_free(list
);
10632 __isl_give isl_basic_map
*isl_basic_map_product(
10633 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
10635 isl_space
*space_result
= NULL
;
10636 struct isl_basic_map
*bmap
;
10637 unsigned in1
, in2
, out1
, out2
, nparam
, total
, pos
;
10638 struct isl_dim_map
*dim_map1
, *dim_map2
;
10640 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
10642 space_result
= isl_space_product(isl_space_copy(bmap1
->dim
),
10643 isl_space_copy(bmap2
->dim
));
10645 in1
= isl_basic_map_dim(bmap1
, isl_dim_in
);
10646 in2
= isl_basic_map_dim(bmap2
, isl_dim_in
);
10647 out1
= isl_basic_map_dim(bmap1
, isl_dim_out
);
10648 out2
= isl_basic_map_dim(bmap2
, isl_dim_out
);
10649 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
10651 total
= nparam
+ in1
+ in2
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
10652 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
10653 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
10654 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
10655 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
10656 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
10657 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
10658 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
10659 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
10660 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
10661 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
10663 bmap
= isl_basic_map_alloc_space(space_result
,
10664 bmap1
->n_div
+ bmap2
->n_div
,
10665 bmap1
->n_eq
+ bmap2
->n_eq
,
10666 bmap1
->n_ineq
+ bmap2
->n_ineq
);
10667 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
10668 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
10669 bmap
= isl_basic_map_simplify(bmap
);
10670 return isl_basic_map_finalize(bmap
);
10672 isl_basic_map_free(bmap1
);
10673 isl_basic_map_free(bmap2
);
10677 __isl_give isl_basic_map
*isl_basic_map_flat_product(
10678 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
10680 isl_basic_map
*prod
;
10682 prod
= isl_basic_map_product(bmap1
, bmap2
);
10683 prod
= isl_basic_map_flatten(prod
);
10687 __isl_give isl_basic_set
*isl_basic_set_flat_product(
10688 __isl_take isl_basic_set
*bset1
, __isl_take isl_basic_set
*bset2
)
10690 return isl_basic_map_flat_range_product(bset1
, bset2
);
10693 __isl_give isl_basic_map
*isl_basic_map_domain_product(
10694 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
10696 isl_space
*space1
, *space2
;
10697 isl_space
*space_result
= NULL
;
10698 isl_basic_map
*bmap
;
10699 isl_size in1
, in2
, out
, nparam
;
10700 unsigned total
, pos
;
10701 struct isl_dim_map
*dim_map1
, *dim_map2
;
10703 in1
= isl_basic_map_dim(bmap1
, isl_dim_in
);
10704 in2
= isl_basic_map_dim(bmap2
, isl_dim_in
);
10705 out
= isl_basic_map_dim(bmap1
, isl_dim_out
);
10706 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
10707 if (in1
< 0 || in2
< 0 || out
< 0 || nparam
< 0)
10710 space1
= isl_basic_map_get_space(bmap1
);
10711 space2
= isl_basic_map_get_space(bmap2
);
10712 space_result
= isl_space_domain_product(space1
, space2
);
10714 total
= nparam
+ in1
+ in2
+ out
+ bmap1
->n_div
+ bmap2
->n_div
;
10715 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
10716 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
10717 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
10718 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
10719 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
10720 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
+= in1
);
10721 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in2
);
10722 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
);
10723 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out
);
10724 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
10726 bmap
= isl_basic_map_alloc_space(space_result
,
10727 bmap1
->n_div
+ bmap2
->n_div
,
10728 bmap1
->n_eq
+ bmap2
->n_eq
,
10729 bmap1
->n_ineq
+ bmap2
->n_ineq
);
10730 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
10731 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
10732 bmap
= isl_basic_map_simplify(bmap
);
10733 return isl_basic_map_finalize(bmap
);
10735 isl_basic_map_free(bmap1
);
10736 isl_basic_map_free(bmap2
);
10740 __isl_give isl_basic_map
*isl_basic_map_range_product(
10741 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
10744 isl_space
*space_result
= NULL
;
10745 isl_basic_map
*bmap
;
10746 isl_size in
, out1
, out2
, nparam
;
10747 unsigned total
, pos
;
10748 struct isl_dim_map
*dim_map1
, *dim_map2
;
10750 rational
= isl_basic_map_is_rational(bmap1
);
10751 if (rational
>= 0 && rational
)
10752 rational
= isl_basic_map_is_rational(bmap2
);
10753 in
= isl_basic_map_dim(bmap1
, isl_dim_in
);
10754 out1
= isl_basic_map_dim(bmap1
, isl_dim_out
);
10755 out2
= isl_basic_map_dim(bmap2
, isl_dim_out
);
10756 nparam
= isl_basic_map_dim(bmap1
, isl_dim_param
);
10757 if (in
< 0 || out1
< 0 || out2
< 0 || nparam
< 0 || rational
< 0)
10760 if (isl_basic_map_check_equal_params(bmap1
, bmap2
) < 0)
10763 space_result
= isl_space_range_product(isl_space_copy(bmap1
->dim
),
10764 isl_space_copy(bmap2
->dim
));
10766 total
= nparam
+ in
+ out1
+ out2
+ bmap1
->n_div
+ bmap2
->n_div
;
10767 dim_map1
= isl_dim_map_alloc(bmap1
->ctx
, total
);
10768 dim_map2
= isl_dim_map_alloc(bmap1
->ctx
, total
);
10769 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_param
, pos
= 0);
10770 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_param
, pos
= 0);
10771 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_in
, pos
+= nparam
);
10772 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_in
, pos
);
10773 isl_dim_map_dim(dim_map1
, bmap1
->dim
, isl_dim_out
, pos
+= in
);
10774 isl_dim_map_dim(dim_map2
, bmap2
->dim
, isl_dim_out
, pos
+= out1
);
10775 isl_dim_map_div(dim_map1
, bmap1
, pos
+= out2
);
10776 isl_dim_map_div(dim_map2
, bmap2
, pos
+= bmap1
->n_div
);
10778 bmap
= isl_basic_map_alloc_space(space_result
,
10779 bmap1
->n_div
+ bmap2
->n_div
,
10780 bmap1
->n_eq
+ bmap2
->n_eq
,
10781 bmap1
->n_ineq
+ bmap2
->n_ineq
);
10782 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap1
, dim_map1
);
10783 bmap
= isl_basic_map_add_constraints_dim_map(bmap
, bmap2
, dim_map2
);
10785 bmap
= isl_basic_map_set_rational(bmap
);
10786 bmap
= isl_basic_map_simplify(bmap
);
10787 return isl_basic_map_finalize(bmap
);
10789 isl_basic_map_free(bmap1
);
10790 isl_basic_map_free(bmap2
);
10794 __isl_give isl_basic_map
*isl_basic_map_flat_range_product(
10795 __isl_take isl_basic_map
*bmap1
, __isl_take isl_basic_map
*bmap2
)
10797 isl_basic_map
*prod
;
10799 prod
= isl_basic_map_range_product(bmap1
, bmap2
);
10800 prod
= isl_basic_map_flatten_range(prod
);
10804 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
10805 * and collect the results.
10806 * The result live in the space obtained by calling "space_product"
10807 * on the spaces of "map1" and "map2".
10808 * If "remove_duplicates" is set then the result may contain duplicates
10809 * (even if the inputs do not) and so we try and remove the obvious
10812 static __isl_give isl_map
*map_product(__isl_take isl_map
*map1
,
10813 __isl_take isl_map
*map2
,
10814 __isl_give isl_space
*(*space_product
)(__isl_take isl_space
*left
,
10815 __isl_take isl_space
*right
),
10816 __isl_give isl_basic_map
*(*basic_map_product
)(
10817 __isl_take isl_basic_map
*left
,
10818 __isl_take isl_basic_map
*right
),
10819 int remove_duplicates
)
10821 unsigned flags
= 0;
10822 struct isl_map
*result
;
10826 m
= isl_map_has_equal_params(map1
, map2
);
10830 isl_die(isl_map_get_ctx(map1
), isl_error_invalid
,
10831 "parameters don't match", goto error
);
10833 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
) &&
10834 ISL_F_ISSET(map2
, ISL_MAP_DISJOINT
))
10835 ISL_FL_SET(flags
, ISL_MAP_DISJOINT
);
10837 result
= isl_map_alloc_space(space_product(isl_space_copy(map1
->dim
),
10838 isl_space_copy(map2
->dim
)),
10839 map1
->n
* map2
->n
, flags
);
10842 for (i
= 0; i
< map1
->n
; ++i
)
10843 for (j
= 0; j
< map2
->n
; ++j
) {
10844 struct isl_basic_map
*part
;
10845 part
= basic_map_product(isl_basic_map_copy(map1
->p
[i
]),
10846 isl_basic_map_copy(map2
->p
[j
]));
10847 if (isl_basic_map_is_empty(part
))
10848 isl_basic_map_free(part
);
10850 result
= isl_map_add_basic_map(result
, part
);
10854 if (remove_duplicates
)
10855 result
= isl_map_remove_obvious_duplicates(result
);
10856 isl_map_free(map1
);
10857 isl_map_free(map2
);
10860 isl_map_free(map1
);
10861 isl_map_free(map2
);
10865 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
10867 __isl_give isl_map
*isl_map_product(__isl_take isl_map
*map1
,
10868 __isl_take isl_map
*map2
)
10870 isl_map_align_params_bin(&map1
, &map2
);
10871 return map_product(map1
, map2
, &isl_space_product
,
10872 &isl_basic_map_product
, 0);
10875 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
10877 __isl_give isl_map
*isl_map_flat_product(__isl_take isl_map
*map1
,
10878 __isl_take isl_map
*map2
)
10882 prod
= isl_map_product(map1
, map2
);
10883 prod
= isl_map_flatten(prod
);
10887 /* Given two set A and B, construct its Cartesian product A x B.
10889 __isl_give isl_set
*isl_set_product(__isl_take isl_set
*set1
,
10890 __isl_take isl_set
*set2
)
10892 return isl_map_range_product(set1
, set2
);
10895 __isl_give isl_set
*isl_set_flat_product(__isl_take isl_set
*set1
,
10896 __isl_take isl_set
*set2
)
10898 return isl_map_flat_range_product(set1
, set2
);
10901 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
10903 __isl_give isl_map
*isl_map_domain_product(__isl_take isl_map
*map1
,
10904 __isl_take isl_map
*map2
)
10906 isl_map_align_params_bin(&map1
, &map2
);
10907 return map_product(map1
, map2
, &isl_space_domain_product
,
10908 &isl_basic_map_domain_product
, 1);
10911 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
10913 __isl_give isl_map
*isl_map_range_product(__isl_take isl_map
*map1
,
10914 __isl_take isl_map
*map2
)
10916 isl_map_align_params_bin(&map1
, &map2
);
10917 return map_product(map1
, map2
, &isl_space_range_product
,
10918 &isl_basic_map_range_product
, 1);
10921 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
10923 __isl_give isl_map
*isl_map_factor_domain(__isl_take isl_map
*map
)
10926 isl_size total1
, keep1
, total2
, keep2
;
10928 total1
= isl_map_dim(map
, isl_dim_in
);
10929 total2
= isl_map_dim(map
, isl_dim_out
);
10930 if (total1
< 0 || total2
< 0)
10931 return isl_map_free(map
);
10932 if (!isl_space_domain_is_wrapping(map
->dim
) ||
10933 !isl_space_range_is_wrapping(map
->dim
))
10934 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10935 "not a product", return isl_map_free(map
));
10937 space
= isl_map_get_space(map
);
10938 space
= isl_space_factor_domain(space
);
10939 keep1
= isl_space_dim(space
, isl_dim_in
);
10940 keep2
= isl_space_dim(space
, isl_dim_out
);
10941 if (keep1
< 0 || keep2
< 0)
10942 map
= isl_map_free(map
);
10943 map
= isl_map_project_out(map
, isl_dim_in
, keep1
, total1
- keep1
);
10944 map
= isl_map_project_out(map
, isl_dim_out
, keep2
, total2
- keep2
);
10945 map
= isl_map_reset_space(map
, space
);
10950 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
10952 __isl_give isl_map
*isl_map_factor_range(__isl_take isl_map
*map
)
10955 isl_size total1
, keep1
, total2
, keep2
;
10957 total1
= isl_map_dim(map
, isl_dim_in
);
10958 total2
= isl_map_dim(map
, isl_dim_out
);
10959 if (total1
< 0 || total2
< 0)
10960 return isl_map_free(map
);
10961 if (!isl_space_domain_is_wrapping(map
->dim
) ||
10962 !isl_space_range_is_wrapping(map
->dim
))
10963 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10964 "not a product", return isl_map_free(map
));
10966 space
= isl_map_get_space(map
);
10967 space
= isl_space_factor_range(space
);
10968 keep1
= isl_space_dim(space
, isl_dim_in
);
10969 keep2
= isl_space_dim(space
, isl_dim_out
);
10970 if (keep1
< 0 || keep2
< 0)
10971 map
= isl_map_free(map
);
10972 map
= isl_map_project_out(map
, isl_dim_in
, 0, total1
- keep1
);
10973 map
= isl_map_project_out(map
, isl_dim_out
, 0, total2
- keep2
);
10974 map
= isl_map_reset_space(map
, space
);
10979 /* Given a map of the form [A -> B] -> C, return the map A -> C.
10981 __isl_give isl_map
*isl_map_domain_factor_domain(__isl_take isl_map
*map
)
10984 isl_size total
, keep
;
10986 total
= isl_map_dim(map
, isl_dim_in
);
10988 return isl_map_free(map
);
10989 if (!isl_space_domain_is_wrapping(map
->dim
))
10990 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
10991 "domain is not a product", return isl_map_free(map
));
10993 space
= isl_map_get_space(map
);
10994 space
= isl_space_domain_factor_domain(space
);
10995 keep
= isl_space_dim(space
, isl_dim_in
);
10997 map
= isl_map_free(map
);
10998 map
= isl_map_project_out(map
, isl_dim_in
, keep
, total
- keep
);
10999 map
= isl_map_reset_space(map
, space
);
11004 /* Given a map of the form [A -> B] -> C, return the map B -> C.
11006 __isl_give isl_map
*isl_map_domain_factor_range(__isl_take isl_map
*map
)
11009 isl_size total
, keep
;
11011 total
= isl_map_dim(map
, isl_dim_in
);
11013 return isl_map_free(map
);
11014 if (!isl_space_domain_is_wrapping(map
->dim
))
11015 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
11016 "domain is not a product", return isl_map_free(map
));
11018 space
= isl_map_get_space(map
);
11019 space
= isl_space_domain_factor_range(space
);
11020 keep
= isl_space_dim(space
, isl_dim_in
);
11022 map
= isl_map_free(map
);
11023 map
= isl_map_project_out(map
, isl_dim_in
, 0, total
- keep
);
11024 map
= isl_map_reset_space(map
, space
);
11029 /* Given a map A -> [B -> C], extract the map A -> B.
11031 __isl_give isl_map
*isl_map_range_factor_domain(__isl_take isl_map
*map
)
11034 isl_size total
, keep
;
11036 total
= isl_map_dim(map
, isl_dim_out
);
11038 return isl_map_free(map
);
11039 if (!isl_space_range_is_wrapping(map
->dim
))
11040 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
11041 "range is not a product", return isl_map_free(map
));
11043 space
= isl_map_get_space(map
);
11044 space
= isl_space_range_factor_domain(space
);
11045 keep
= isl_space_dim(space
, isl_dim_out
);
11047 map
= isl_map_free(map
);
11048 map
= isl_map_project_out(map
, isl_dim_out
, keep
, total
- keep
);
11049 map
= isl_map_reset_space(map
, space
);
11054 /* Given a map A -> [B -> C], extract the map A -> C.
11056 __isl_give isl_map
*isl_map_range_factor_range(__isl_take isl_map
*map
)
11059 isl_size total
, keep
;
11061 total
= isl_map_dim(map
, isl_dim_out
);
11063 return isl_map_free(map
);
11064 if (!isl_space_range_is_wrapping(map
->dim
))
11065 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
11066 "range is not a product", return isl_map_free(map
));
11068 space
= isl_map_get_space(map
);
11069 space
= isl_space_range_factor_range(space
);
11070 keep
= isl_space_dim(space
, isl_dim_out
);
11072 map
= isl_map_free(map
);
11073 map
= isl_map_project_out(map
, isl_dim_out
, 0, total
- keep
);
11074 map
= isl_map_reset_space(map
, space
);
11079 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
11081 __isl_give isl_map
*isl_map_flat_domain_product(__isl_take isl_map
*map1
,
11082 __isl_take isl_map
*map2
)
11086 prod
= isl_map_domain_product(map1
, map2
);
11087 prod
= isl_map_flatten_domain(prod
);
11091 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
11093 __isl_give isl_map
*isl_map_flat_range_product(__isl_take isl_map
*map1
,
11094 __isl_take isl_map
*map2
)
11098 prod
= isl_map_range_product(map1
, map2
);
11099 prod
= isl_map_flatten_range(prod
);
11103 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map
*bmap
)
11106 uint32_t hash
= isl_hash_init();
11111 bmap
= isl_basic_map_copy(bmap
);
11112 bmap
= isl_basic_map_normalize(bmap
);
11113 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
11116 isl_hash_byte(hash
, bmap
->n_eq
& 0xFF);
11117 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
11119 c_hash
= isl_seq_get_hash(bmap
->eq
[i
], 1 + total
);
11120 isl_hash_hash(hash
, c_hash
);
11122 isl_hash_byte(hash
, bmap
->n_ineq
& 0xFF);
11123 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
11125 c_hash
= isl_seq_get_hash(bmap
->ineq
[i
], 1 + total
);
11126 isl_hash_hash(hash
, c_hash
);
11128 isl_hash_byte(hash
, bmap
->n_div
& 0xFF);
11129 for (i
= 0; i
< bmap
->n_div
; ++i
) {
11131 if (isl_int_is_zero(bmap
->div
[i
][0]))
11133 isl_hash_byte(hash
, i
& 0xFF);
11134 c_hash
= isl_seq_get_hash(bmap
->div
[i
], 1 + 1 + total
);
11135 isl_hash_hash(hash
, c_hash
);
11137 isl_basic_map_free(bmap
);
11141 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set
*bset
)
11143 return isl_basic_map_get_hash(bset_to_bmap(bset
));
11146 uint32_t isl_map_get_hash(__isl_keep isl_map
*map
)
11153 map
= isl_map_copy(map
);
11154 map
= isl_map_normalize(map
);
11158 hash
= isl_hash_init();
11159 for (i
= 0; i
< map
->n
; ++i
) {
11160 uint32_t bmap_hash
;
11161 bmap_hash
= isl_basic_map_get_hash(map
->p
[i
]);
11162 isl_hash_hash(hash
, bmap_hash
);
11170 uint32_t isl_set_get_hash(__isl_keep isl_set
*set
)
11172 return isl_map_get_hash(set_to_map(set
));
11175 /* Return the number of basic maps in the (current) representation of "map".
11177 isl_size
isl_map_n_basic_map(__isl_keep isl_map
*map
)
11179 return map
? map
->n
: isl_size_error
;
11182 isl_size
isl_set_n_basic_set(__isl_keep isl_set
*set
)
11184 return set
? set
->n
: isl_size_error
;
11187 isl_stat
isl_map_foreach_basic_map(__isl_keep isl_map
*map
,
11188 isl_stat (*fn
)(__isl_take isl_basic_map
*bmap
, void *user
), void *user
)
11193 return isl_stat_error
;
11195 for (i
= 0; i
< map
->n
; ++i
)
11196 if (fn(isl_basic_map_copy(map
->p
[i
]), user
) < 0)
11197 return isl_stat_error
;
11199 return isl_stat_ok
;
11202 isl_stat
isl_set_foreach_basic_set(__isl_keep isl_set
*set
,
11203 isl_stat (*fn
)(__isl_take isl_basic_set
*bset
, void *user
), void *user
)
11208 return isl_stat_error
;
11210 for (i
= 0; i
< set
->n
; ++i
)
11211 if (fn(isl_basic_set_copy(set
->p
[i
]), user
) < 0)
11212 return isl_stat_error
;
11214 return isl_stat_ok
;
11217 /* Does "test" succeed on every basic set in "set"?
11219 isl_bool
isl_set_every_basic_set(__isl_keep isl_set
*set
,
11220 isl_bool (*test
)(__isl_keep isl_basic_set
*bset
, void *user
),
11226 return isl_bool_error
;
11228 for (i
= 0; i
< set
->n
; ++i
) {
11231 r
= test(set
->p
[i
], user
);
11236 return isl_bool_true
;
11239 /* Return a list of basic sets, the union of which is equal to "set".
11241 __isl_give isl_basic_set_list
*isl_set_get_basic_set_list(
11242 __isl_keep isl_set
*set
)
11245 isl_basic_set_list
*list
;
11250 list
= isl_basic_set_list_alloc(isl_set_get_ctx(set
), set
->n
);
11251 for (i
= 0; i
< set
->n
; ++i
) {
11252 isl_basic_set
*bset
;
11254 bset
= isl_basic_set_copy(set
->p
[i
]);
11255 list
= isl_basic_set_list_add(list
, bset
);
11261 __isl_give isl_basic_set
*isl_basic_set_lift(__isl_take isl_basic_set
*bset
)
11268 bset
= isl_basic_set_cow(bset
);
11272 space
= isl_basic_set_get_space(bset
);
11273 space
= isl_space_lift(space
, bset
->n_div
);
11276 isl_space_free(bset
->dim
);
11278 bset
->extra
-= bset
->n_div
;
11281 bset
= isl_basic_set_finalize(bset
);
11285 isl_basic_set_free(bset
);
11289 __isl_give isl_set
*isl_set_lift(__isl_take isl_set
*set
)
11295 set
= set_from_map(isl_map_align_divs_internal(set_to_map(set
)));
11300 set
= isl_set_cow(set
);
11304 n_div
= set
->p
[0]->n_div
;
11305 space
= isl_set_get_space(set
);
11306 space
= isl_space_lift(space
, n_div
);
11309 isl_space_free(set
->dim
);
11312 for (i
= 0; i
< set
->n
; ++i
) {
11313 set
->p
[i
] = isl_basic_set_lift(set
->p
[i
]);
11324 int isl_basic_set_size(__isl_keep isl_basic_set
*bset
)
11329 dim
= isl_basic_set_dim(bset
, isl_dim_all
);
11332 size
+= bset
->n_eq
* (1 + dim
);
11333 size
+= bset
->n_ineq
* (1 + dim
);
11334 size
+= bset
->n_div
* (2 + dim
);
11339 int isl_set_size(__isl_keep isl_set
*set
)
11347 for (i
= 0; i
< set
->n
; ++i
)
11348 size
+= isl_basic_set_size(set
->p
[i
]);
11353 /* Check if there is any lower bound (if lower == 0) and/or upper
11354 * bound (if upper == 0) on the specified dim.
11356 static isl_bool
basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
11357 enum isl_dim_type type
, unsigned pos
, int lower
, int upper
)
11361 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
11362 return isl_bool_error
;
11364 pos
+= isl_basic_map_offset(bmap
, type
);
11366 for (i
= 0; i
< bmap
->n_div
; ++i
) {
11367 if (isl_int_is_zero(bmap
->div
[i
][0]))
11369 if (!isl_int_is_zero(bmap
->div
[i
][1 + pos
]))
11370 return isl_bool_true
;
11373 for (i
= 0; i
< bmap
->n_eq
; ++i
)
11374 if (!isl_int_is_zero(bmap
->eq
[i
][pos
]))
11375 return isl_bool_true
;
11377 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
11378 int sgn
= isl_int_sgn(bmap
->ineq
[i
][pos
]);
11385 return lower
&& upper
;
11388 isl_bool
isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map
*bmap
,
11389 enum isl_dim_type type
, unsigned pos
)
11391 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 0);
11394 isl_bool
isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map
*bmap
,
11395 enum isl_dim_type type
, unsigned pos
)
11397 return basic_map_dim_is_bounded(bmap
, type
, pos
, 0, 1);
11400 isl_bool
isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map
*bmap
,
11401 enum isl_dim_type type
, unsigned pos
)
11403 return basic_map_dim_is_bounded(bmap
, type
, pos
, 1, 0);
11406 isl_bool
isl_map_dim_is_bounded(__isl_keep isl_map
*map
,
11407 enum isl_dim_type type
, unsigned pos
)
11412 return isl_bool_error
;
11414 for (i
= 0; i
< map
->n
; ++i
) {
11416 bounded
= isl_basic_map_dim_is_bounded(map
->p
[i
], type
, pos
);
11417 if (bounded
< 0 || !bounded
)
11421 return isl_bool_true
;
11424 /* Return true if the specified dim is involved in both an upper bound
11425 * and a lower bound.
11427 isl_bool
isl_set_dim_is_bounded(__isl_keep isl_set
*set
,
11428 enum isl_dim_type type
, unsigned pos
)
11430 return isl_map_dim_is_bounded(set_to_map(set
), type
, pos
);
11433 /* Does "map" have a bound (according to "fn") for any of its basic maps?
11435 static isl_bool
has_any_bound(__isl_keep isl_map
*map
,
11436 enum isl_dim_type type
, unsigned pos
,
11437 isl_bool (*fn
)(__isl_keep isl_basic_map
*bmap
,
11438 enum isl_dim_type type
, unsigned pos
))
11443 return isl_bool_error
;
11445 for (i
= 0; i
< map
->n
; ++i
) {
11447 bounded
= fn(map
->p
[i
], type
, pos
);
11448 if (bounded
< 0 || bounded
)
11452 return isl_bool_false
;
11455 /* Return 1 if the specified dim is involved in any lower bound.
11457 isl_bool
isl_set_dim_has_any_lower_bound(__isl_keep isl_set
*set
,
11458 enum isl_dim_type type
, unsigned pos
)
11460 return has_any_bound(set
, type
, pos
,
11461 &isl_basic_map_dim_has_lower_bound
);
11464 /* Return 1 if the specified dim is involved in any upper bound.
11466 isl_bool
isl_set_dim_has_any_upper_bound(__isl_keep isl_set
*set
,
11467 enum isl_dim_type type
, unsigned pos
)
11469 return has_any_bound(set
, type
, pos
,
11470 &isl_basic_map_dim_has_upper_bound
);
11473 /* Does "map" have a bound (according to "fn") for all of its basic maps?
11475 static isl_bool
has_bound(__isl_keep isl_map
*map
,
11476 enum isl_dim_type type
, unsigned pos
,
11477 isl_bool (*fn
)(__isl_keep isl_basic_map
*bmap
,
11478 enum isl_dim_type type
, unsigned pos
))
11483 return isl_bool_error
;
11485 for (i
= 0; i
< map
->n
; ++i
) {
11487 bounded
= fn(map
->p
[i
], type
, pos
);
11488 if (bounded
< 0 || !bounded
)
11492 return isl_bool_true
;
11495 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
11497 isl_bool
isl_set_dim_has_lower_bound(__isl_keep isl_set
*set
,
11498 enum isl_dim_type type
, unsigned pos
)
11500 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_lower_bound
);
11503 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
11505 isl_bool
isl_set_dim_has_upper_bound(__isl_keep isl_set
*set
,
11506 enum isl_dim_type type
, unsigned pos
)
11508 return has_bound(set
, type
, pos
, &isl_basic_map_dim_has_upper_bound
);
11511 /* For each of the "n" variables starting at "first", determine
11512 * the sign of the variable and put the results in the first "n"
11513 * elements of the array "signs".
11515 * 1 means that the variable is non-negative
11516 * -1 means that the variable is non-positive
11517 * 0 means the variable attains both positive and negative values.
11519 isl_stat
isl_basic_set_vars_get_sign(__isl_keep isl_basic_set
*bset
,
11520 unsigned first
, unsigned n
, int *signs
)
11522 isl_vec
*bound
= NULL
;
11523 struct isl_tab
*tab
= NULL
;
11524 struct isl_tab_undo
*snap
;
11528 total
= isl_basic_set_dim(bset
, isl_dim_all
);
11529 if (total
< 0 || !signs
)
11530 return isl_stat_error
;
11532 bound
= isl_vec_alloc(bset
->ctx
, 1 + total
);
11533 tab
= isl_tab_from_basic_set(bset
, 0);
11534 if (!bound
|| !tab
)
11537 isl_seq_clr(bound
->el
, bound
->size
);
11538 isl_int_set_si(bound
->el
[0], -1);
11540 snap
= isl_tab_snap(tab
);
11541 for (i
= 0; i
< n
; ++i
) {
11544 isl_int_set_si(bound
->el
[1 + first
+ i
], -1);
11545 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
11547 empty
= tab
->empty
;
11548 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
11549 if (isl_tab_rollback(tab
, snap
) < 0)
11557 isl_int_set_si(bound
->el
[1 + first
+ i
], 1);
11558 if (isl_tab_add_ineq(tab
, bound
->el
) < 0)
11560 empty
= tab
->empty
;
11561 isl_int_set_si(bound
->el
[1 + first
+ i
], 0);
11562 if (isl_tab_rollback(tab
, snap
) < 0)
11565 signs
[i
] = empty
? -1 : 0;
11569 isl_vec_free(bound
);
11570 return isl_stat_ok
;
11573 isl_vec_free(bound
);
11574 return isl_stat_error
;
11577 isl_stat
isl_basic_set_dims_get_sign(__isl_keep isl_basic_set
*bset
,
11578 enum isl_dim_type type
, unsigned first
, unsigned n
, int *signs
)
11580 if (!bset
|| !signs
)
11581 return isl_stat_error
;
11582 if (isl_basic_set_check_range(bset
, type
, first
, n
) < 0)
11583 return isl_stat_error
;
11585 first
+= pos(bset
->dim
, type
) - 1;
11586 return isl_basic_set_vars_get_sign(bset
, first
, n
, signs
);
11589 /* Is it possible for the integer division "div" to depend (possibly
11590 * indirectly) on any output dimensions?
11592 * If the div is undefined, then we conservatively assume that it
11593 * may depend on them.
11594 * Otherwise, we check if it actually depends on them or on any integer
11595 * divisions that may depend on them.
11597 static isl_bool
div_may_involve_output(__isl_keep isl_basic_map
*bmap
, int div
)
11600 isl_size n_out
, n_div
;
11601 unsigned o_out
, o_div
;
11603 if (isl_int_is_zero(bmap
->div
[div
][0]))
11604 return isl_bool_true
;
11606 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
11608 return isl_bool_error
;
11609 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
11611 if (isl_seq_first_non_zero(bmap
->div
[div
] + 1 + o_out
, n_out
) != -1)
11612 return isl_bool_true
;
11614 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
11616 return isl_bool_error
;
11617 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
11619 for (i
= 0; i
< n_div
; ++i
) {
11620 isl_bool may_involve
;
11622 if (isl_int_is_zero(bmap
->div
[div
][1 + o_div
+ i
]))
11624 may_involve
= div_may_involve_output(bmap
, i
);
11625 if (may_involve
< 0 || may_involve
)
11626 return may_involve
;
11629 return isl_bool_false
;
11632 /* Return the first integer division of "bmap" in the range
11633 * [first, first + n[ that may depend on any output dimensions and
11634 * that has a non-zero coefficient in "c" (where the first coefficient
11635 * in "c" corresponds to integer division "first").
11637 static int first_div_may_involve_output(__isl_keep isl_basic_map
*bmap
,
11638 isl_int
*c
, int first
, int n
)
11645 for (k
= first
; k
< first
+ n
; ++k
) {
11646 isl_bool may_involve
;
11648 if (isl_int_is_zero(c
[k
]))
11650 may_involve
= div_may_involve_output(bmap
, k
);
11651 if (may_involve
< 0)
11660 /* Look for a pair of inequality constraints in "bmap" of the form
11662 * -l + i >= 0 or i >= l
11664 * n + l - i >= 0 or i <= l + n
11666 * with n < "m" and i the output dimension at position "pos".
11667 * (Note that n >= 0 as otherwise the two constraints would conflict.)
11668 * Furthermore, "l" is only allowed to involve parameters, input dimensions
11669 * and earlier output dimensions, as well as integer divisions that do
11670 * not involve any of the output dimensions.
11672 * Return the index of the first inequality constraint or bmap->n_ineq
11673 * if no such pair can be found.
11675 static int find_modulo_constraint_pair(__isl_keep isl_basic_map
*bmap
,
11676 int pos
, isl_int m
)
11681 isl_size n_div
, n_out
;
11682 unsigned o_div
, o_out
;
11685 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
11686 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
11687 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
11688 if (total
< 0 || n_out
< 0 || n_div
< 0)
11691 ctx
= isl_basic_map_get_ctx(bmap
);
11692 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
11693 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
11694 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
11695 if (!isl_int_abs_eq(bmap
->ineq
[i
][o_out
+ pos
], ctx
->one
))
11697 if (isl_seq_first_non_zero(bmap
->ineq
[i
] + o_out
+ pos
+ 1,
11698 n_out
- (pos
+ 1)) != -1)
11700 if (first_div_may_involve_output(bmap
, bmap
->ineq
[i
] + o_div
,
11703 for (j
= i
+ 1; j
< bmap
->n_ineq
; ++j
) {
11704 if (!isl_int_abs_eq(bmap
->ineq
[j
][o_out
+ pos
],
11707 if (!isl_seq_is_neg(bmap
->ineq
[i
] + 1,
11708 bmap
->ineq
[j
] + 1, total
))
11712 if (j
>= bmap
->n_ineq
)
11714 isl_int_add(bmap
->ineq
[i
][0],
11715 bmap
->ineq
[i
][0], bmap
->ineq
[j
][0]);
11716 less
= isl_int_abs_lt(bmap
->ineq
[i
][0], m
);
11717 isl_int_sub(bmap
->ineq
[i
][0],
11718 bmap
->ineq
[i
][0], bmap
->ineq
[j
][0]);
11721 if (isl_int_is_one(bmap
->ineq
[i
][o_out
+ pos
]))
11727 return bmap
->n_ineq
;
11730 /* Return the index of the equality of "bmap" that defines
11731 * the output dimension "pos" in terms of earlier dimensions.
11732 * The equality may also involve integer divisions, as long
11733 * as those integer divisions are defined in terms of
11734 * parameters or input dimensions.
11735 * In this case, *div is set to the number of integer divisions and
11736 * *ineq is set to the number of inequality constraints (provided
11737 * div and ineq are not NULL).
11739 * The equality may also involve a single integer division involving
11740 * the output dimensions (typically only output dimension "pos") as
11741 * long as the coefficient of output dimension "pos" is 1 or -1 and
11742 * there is a pair of constraints i >= l and i <= l + n, with i referring
11743 * to output dimension "pos", l an expression involving only earlier
11744 * dimensions and n smaller than the coefficient of the integer division
11745 * in the equality. In this case, the output dimension can be defined
11746 * in terms of a modulo expression that does not involve the integer division.
11747 * *div is then set to this single integer division and
11748 * *ineq is set to the index of constraint i >= l.
11750 * Return bmap->n_eq if there is no such equality.
11751 * Return -1 on error.
11753 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map
*bmap
,
11754 int pos
, int *div
, int *ineq
)
11757 isl_size n_div
, n_out
;
11758 unsigned o_div
, o_out
;
11760 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
11761 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
11762 if (n_out
< 0 || n_div
< 0)
11765 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
11766 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
11769 *ineq
= bmap
->n_ineq
;
11772 for (j
= 0; j
< bmap
->n_eq
; ++j
) {
11773 if (isl_int_is_zero(bmap
->eq
[j
][o_out
+ pos
]))
11775 if (isl_seq_first_non_zero(bmap
->eq
[j
] + o_out
+ pos
+ 1,
11776 n_out
- (pos
+ 1)) != -1)
11778 k
= first_div_may_involve_output(bmap
, bmap
->eq
[j
] + o_div
,
11782 if (!isl_int_is_one(bmap
->eq
[j
][o_out
+ pos
]) &&
11783 !isl_int_is_negone(bmap
->eq
[j
][o_out
+ pos
]))
11785 if (first_div_may_involve_output(bmap
, bmap
->eq
[j
] + o_div
,
11786 k
+ 1, n_div
- (k
+1)) < n_div
)
11788 l
= find_modulo_constraint_pair(bmap
, pos
,
11789 bmap
->eq
[j
][o_div
+ k
]);
11792 if (l
>= bmap
->n_ineq
)
11804 /* Check if the given basic map is obviously single-valued.
11805 * In particular, for each output dimension, check that there is
11806 * an equality that defines the output dimension in terms of
11807 * earlier dimensions.
11809 isl_bool
isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map
*bmap
)
11814 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
11816 return isl_bool_error
;
11818 for (i
= 0; i
< n_out
; ++i
) {
11821 eq
= isl_basic_map_output_defining_equality(bmap
, i
,
11824 return isl_bool_error
;
11825 if (eq
>= bmap
->n_eq
)
11826 return isl_bool_false
;
11829 return isl_bool_true
;
11832 /* Check if the given basic map is single-valued.
11833 * We simply compute
11837 * and check if the result is a subset of the identity mapping.
11839 isl_bool
isl_basic_map_is_single_valued(__isl_keep isl_basic_map
*bmap
)
11842 isl_basic_map
*test
;
11846 sv
= isl_basic_map_plain_is_single_valued(bmap
);
11850 test
= isl_basic_map_reverse(isl_basic_map_copy(bmap
));
11851 test
= isl_basic_map_apply_range(test
, isl_basic_map_copy(bmap
));
11853 space
= isl_basic_map_get_space(bmap
);
11854 space
= isl_space_map_from_set(isl_space_range(space
));
11855 id
= isl_basic_map_identity(space
);
11857 sv
= isl_basic_map_is_subset(test
, id
);
11859 isl_basic_map_free(test
);
11860 isl_basic_map_free(id
);
11865 /* Check if the given map is obviously single-valued.
11867 isl_bool
isl_map_plain_is_single_valued(__isl_keep isl_map
*map
)
11870 return isl_bool_error
;
11872 return isl_bool_true
;
11874 return isl_bool_false
;
11876 return isl_basic_map_plain_is_single_valued(map
->p
[0]);
11879 /* Check if the given map is single-valued.
11880 * We simply compute
11884 * and check if the result is a subset of the identity mapping.
11886 isl_bool
isl_map_is_single_valued(__isl_keep isl_map
*map
)
11893 sv
= isl_map_plain_is_single_valued(map
);
11897 test
= isl_map_reverse(isl_map_copy(map
));
11898 test
= isl_map_apply_range(test
, isl_map_copy(map
));
11900 space
= isl_space_map_from_set(isl_space_range(isl_map_get_space(map
)));
11901 id
= isl_map_identity(space
);
11903 sv
= isl_map_is_subset(test
, id
);
11905 isl_map_free(test
);
11911 isl_bool
isl_map_is_injective(__isl_keep isl_map
*map
)
11915 map
= isl_map_copy(map
);
11916 map
= isl_map_reverse(map
);
11917 in
= isl_map_is_single_valued(map
);
11923 /* Check if the given map is obviously injective.
11925 isl_bool
isl_map_plain_is_injective(__isl_keep isl_map
*map
)
11929 map
= isl_map_copy(map
);
11930 map
= isl_map_reverse(map
);
11931 in
= isl_map_plain_is_single_valued(map
);
11937 isl_bool
isl_map_is_bijective(__isl_keep isl_map
*map
)
11941 sv
= isl_map_is_single_valued(map
);
11945 return isl_map_is_injective(map
);
11948 isl_bool
isl_set_is_singleton(__isl_keep isl_set
*set
)
11950 return isl_map_is_single_valued(set_to_map(set
));
11953 /* Does "map" only map elements to themselves?
11955 * If the domain and range spaces are different, then "map"
11956 * is considered not to be an identity relation, even if it is empty.
11957 * Otherwise, construct the maximal identity relation and
11958 * check whether "map" is a subset of this relation.
11960 isl_bool
isl_map_is_identity(__isl_keep isl_map
*map
)
11963 isl_bool equal
, is_identity
;
11965 equal
= isl_map_tuple_is_equal(map
, isl_dim_in
, map
, isl_dim_out
);
11966 if (equal
< 0 || !equal
)
11969 id
= isl_map_identity(isl_map_get_space(map
));
11970 is_identity
= isl_map_is_subset(map
, id
);
11973 return is_identity
;
11976 int isl_map_is_translation(__isl_keep isl_map
*map
)
11981 delta
= isl_map_deltas(isl_map_copy(map
));
11982 ok
= isl_set_is_singleton(delta
);
11983 isl_set_free(delta
);
11988 static int unique(isl_int
*p
, unsigned pos
, unsigned len
)
11990 if (isl_seq_first_non_zero(p
, pos
) != -1)
11992 if (isl_seq_first_non_zero(p
+ pos
+ 1, len
- pos
- 1) != -1)
11997 isl_bool
isl_basic_set_is_box(__isl_keep isl_basic_set
*bset
)
12000 isl_size nvar
, n_div
;
12003 n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
12005 return isl_bool_error
;
12007 return isl_bool_false
;
12009 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
12011 return isl_bool_error
;
12012 ovar
= isl_space_offset(bset
->dim
, isl_dim_set
);
12013 for (j
= 0; j
< nvar
; ++j
) {
12014 int lower
= 0, upper
= 0;
12015 for (i
= 0; i
< bset
->n_eq
; ++i
) {
12016 if (isl_int_is_zero(bset
->eq
[i
][1 + ovar
+ j
]))
12018 if (!unique(bset
->eq
[i
] + 1 + ovar
, j
, nvar
))
12019 return isl_bool_false
;
12022 if (i
< bset
->n_eq
)
12024 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
12025 if (isl_int_is_zero(bset
->ineq
[i
][1 + ovar
+ j
]))
12027 if (!unique(bset
->ineq
[i
] + 1 + ovar
, j
, nvar
))
12028 return isl_bool_false
;
12029 if (isl_int_is_pos(bset
->ineq
[i
][1 + ovar
+ j
]))
12034 if (!lower
|| !upper
)
12035 return isl_bool_false
;
12038 return isl_bool_true
;
12041 isl_bool
isl_set_is_box(__isl_keep isl_set
*set
)
12044 return isl_bool_error
;
12046 return isl_bool_false
;
12048 return isl_basic_set_is_box(set
->p
[0]);
12051 isl_bool
isl_basic_set_is_wrapping(__isl_keep isl_basic_set
*bset
)
12054 return isl_bool_error
;
12056 return isl_space_is_wrapping(bset
->dim
);
12059 isl_bool
isl_set_is_wrapping(__isl_keep isl_set
*set
)
12062 return isl_bool_error
;
12064 return isl_space_is_wrapping(set
->dim
);
12067 /* Modify the space of "map" through a call to "change".
12068 * If "can_change" is set (not NULL), then first call it to check
12069 * if the modification is allowed, printing the error message "cannot_change"
12072 static __isl_give isl_map
*isl_map_change_space(__isl_take isl_map
*map
,
12073 isl_bool (*can_change
)(__isl_keep isl_map
*map
),
12074 const char *cannot_change
,
12075 __isl_give isl_space
*(*change
)(__isl_take isl_space
*space
))
12083 ok
= can_change
? can_change(map
) : isl_bool_true
;
12085 return isl_map_free(map
);
12087 isl_die(isl_map_get_ctx(map
), isl_error_invalid
, cannot_change
,
12088 return isl_map_free(map
));
12090 space
= change(isl_map_get_space(map
));
12091 map
= isl_map_reset_space(map
, space
);
12096 /* Is the domain of "map" a wrapped relation?
12098 isl_bool
isl_map_domain_is_wrapping(__isl_keep isl_map
*map
)
12101 return isl_bool_error
;
12103 return isl_space_domain_is_wrapping(map
->dim
);
12106 /* Does "map" have a wrapped relation in both domain and range?
12108 isl_bool
isl_map_is_product(__isl_keep isl_map
*map
)
12110 return isl_space_is_product(isl_map_peek_space(map
));
12113 /* Is the range of "map" a wrapped relation?
12115 isl_bool
isl_map_range_is_wrapping(__isl_keep isl_map
*map
)
12118 return isl_bool_error
;
12120 return isl_space_range_is_wrapping(map
->dim
);
12123 __isl_give isl_basic_set
*isl_basic_map_wrap(__isl_take isl_basic_map
*bmap
)
12127 space
= isl_basic_map_take_space(bmap
);
12128 space
= isl_space_wrap(space
);
12129 bmap
= isl_basic_map_restore_space(bmap
, space
);
12131 bmap
= isl_basic_map_finalize(bmap
);
12133 return bset_from_bmap(bmap
);
12136 /* Given a map A -> B, return the set (A -> B).
12138 __isl_give isl_set
*isl_map_wrap(__isl_take isl_map
*map
)
12140 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_wrap
);
12143 __isl_give isl_basic_map
*isl_basic_set_unwrap(__isl_take isl_basic_set
*bset
)
12145 bset
= isl_basic_set_cow(bset
);
12149 bset
->dim
= isl_space_unwrap(bset
->dim
);
12153 bset
= isl_basic_set_finalize(bset
);
12155 return bset_to_bmap(bset
);
12157 isl_basic_set_free(bset
);
12161 /* Given a set (A -> B), return the map A -> B.
12162 * Error out if "set" is not of the form (A -> B).
12164 __isl_give isl_map
*isl_set_unwrap(__isl_take isl_set
*set
)
12166 return isl_map_change_space(set
, &isl_set_is_wrapping
,
12167 "not a wrapping set", &isl_space_unwrap
);
12170 __isl_give isl_basic_map
*isl_basic_map_reset(__isl_take isl_basic_map
*bmap
,
12171 enum isl_dim_type type
)
12175 space
= isl_basic_map_take_space(bmap
);
12176 space
= isl_space_reset(space
, type
);
12177 bmap
= isl_basic_map_restore_space(bmap
, space
);
12179 bmap
= isl_basic_map_mark_final(bmap
);
12184 __isl_give isl_map
*isl_map_reset(__isl_take isl_map
*map
,
12185 enum isl_dim_type type
)
12193 if (!isl_space_is_named_or_nested(map
->dim
, type
))
12196 map
= isl_map_cow(map
);
12200 for (i
= 0; i
< map
->n
; ++i
) {
12201 map
->p
[i
] = isl_basic_map_reset(map
->p
[i
], type
);
12206 space
= isl_map_take_space(map
);
12207 space
= isl_space_reset(space
, type
);
12208 map
= isl_map_restore_space(map
, space
);
12216 __isl_give isl_basic_map
*isl_basic_map_flatten(__isl_take isl_basic_map
*bmap
)
12220 space
= isl_basic_map_take_space(bmap
);
12221 space
= isl_space_flatten(space
);
12222 bmap
= isl_basic_map_restore_space(bmap
, space
);
12224 bmap
= isl_basic_map_mark_final(bmap
);
12229 __isl_give isl_basic_set
*isl_basic_set_flatten(__isl_take isl_basic_set
*bset
)
12231 return bset_from_bmap(isl_basic_map_flatten(bset_to_bmap(bset
)));
12234 __isl_give isl_basic_map
*isl_basic_map_flatten_domain(
12235 __isl_take isl_basic_map
*bmap
)
12239 space
= isl_basic_map_take_space(bmap
);
12240 space
= isl_space_flatten_domain(space
);
12241 bmap
= isl_basic_map_restore_space(bmap
, space
);
12243 bmap
= isl_basic_map_mark_final(bmap
);
12248 __isl_give isl_basic_map
*isl_basic_map_flatten_range(
12249 __isl_take isl_basic_map
*bmap
)
12253 space
= isl_basic_map_take_space(bmap
);
12254 space
= isl_space_flatten_range(space
);
12255 bmap
= isl_basic_map_restore_space(bmap
, space
);
12257 bmap
= isl_basic_map_mark_final(bmap
);
12262 /* Remove any internal structure from the spaces of domain and range of "map".
12264 __isl_give isl_map
*isl_map_flatten(__isl_take isl_map
*map
)
12269 if (!map
->dim
->nested
[0] && !map
->dim
->nested
[1])
12272 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten
);
12275 __isl_give isl_set
*isl_set_flatten(__isl_take isl_set
*set
)
12277 return set_from_map(isl_map_flatten(set_to_map(set
)));
12280 __isl_give isl_map
*isl_set_flatten_map(__isl_take isl_set
*set
)
12282 isl_space
*space
, *flat_space
;
12285 space
= isl_set_get_space(set
);
12286 flat_space
= isl_space_flatten(isl_space_copy(space
));
12287 map
= isl_map_identity(isl_space_join(isl_space_reverse(space
),
12289 map
= isl_map_intersect_domain(map
, set
);
12294 /* Remove any internal structure from the space of the domain of "map".
12296 __isl_give isl_map
*isl_map_flatten_domain(__isl_take isl_map
*map
)
12301 if (!map
->dim
->nested
[0])
12304 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten_domain
);
12307 /* Remove any internal structure from the space of the range of "map".
12309 __isl_give isl_map
*isl_map_flatten_range(__isl_take isl_map
*map
)
12314 if (!map
->dim
->nested
[1])
12317 return isl_map_change_space(map
, NULL
, NULL
, &isl_space_flatten_range
);
12320 /* Reorder the dimensions of "bmap" according to the given dim_map
12321 * and set the dimension specification to "space" and
12322 * perform Gaussian elimination on the result.
12324 __isl_give isl_basic_map
*isl_basic_map_realign(__isl_take isl_basic_map
*bmap
,
12325 __isl_take isl_space
*space
, __isl_take
struct isl_dim_map
*dim_map
)
12327 isl_basic_map
*res
;
12331 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
12332 if (n_div
< 0 || !space
|| !dim_map
)
12335 flags
= bmap
->flags
;
12336 ISL_FL_CLR(flags
, ISL_BASIC_MAP_FINAL
);
12337 ISL_FL_CLR(flags
, ISL_BASIC_MAP_SORTED
);
12338 ISL_FL_CLR(flags
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
12339 res
= isl_basic_map_alloc_space(space
, n_div
, bmap
->n_eq
, bmap
->n_ineq
);
12340 res
= isl_basic_map_add_constraints_dim_map(res
, bmap
, dim_map
);
12342 res
->flags
= flags
;
12343 res
= isl_basic_map_gauss(res
, NULL
);
12344 res
= isl_basic_map_finalize(res
);
12347 isl_dim_map_free(dim_map
);
12348 isl_basic_map_free(bmap
);
12349 isl_space_free(space
);
12353 /* Reorder the dimensions of "map" according to given reordering.
12355 __isl_give isl_map
*isl_map_realign(__isl_take isl_map
*map
,
12356 __isl_take isl_reordering
*r
)
12359 struct isl_dim_map
*dim_map
;
12361 map
= isl_map_cow(map
);
12362 dim_map
= isl_dim_map_from_reordering(r
);
12363 if (!map
|| !r
|| !dim_map
)
12366 for (i
= 0; i
< map
->n
; ++i
) {
12367 struct isl_dim_map
*dim_map_i
;
12370 dim_map_i
= isl_dim_map_extend(dim_map
, map
->p
[i
]);
12372 space
= isl_reordering_get_space(r
);
12373 map
->p
[i
] = isl_basic_map_realign(map
->p
[i
], space
, dim_map_i
);
12379 map
= isl_map_reset_space(map
, isl_reordering_get_space(r
));
12380 map
= isl_map_unmark_normalized(map
);
12382 isl_reordering_free(r
);
12383 isl_dim_map_free(dim_map
);
12386 isl_dim_map_free(dim_map
);
12388 isl_reordering_free(r
);
12392 __isl_give isl_set
*isl_set_realign(__isl_take isl_set
*set
,
12393 __isl_take isl_reordering
*r
)
12395 return set_from_map(isl_map_realign(set_to_map(set
), r
));
12398 __isl_give isl_map
*isl_map_align_params(__isl_take isl_map
*map
,
12399 __isl_take isl_space
*model
)
12404 if (!map
|| !model
)
12407 ctx
= isl_space_get_ctx(model
);
12408 if (!isl_space_has_named_params(model
))
12409 isl_die(ctx
, isl_error_invalid
,
12410 "model has unnamed parameters", goto error
);
12411 if (isl_map_check_named_params(map
) < 0)
12413 aligned
= isl_map_space_has_equal_params(map
, model
);
12417 isl_reordering
*exp
;
12419 exp
= isl_parameter_alignment_reordering(map
->dim
, model
);
12420 exp
= isl_reordering_extend_space(exp
, isl_map_get_space(map
));
12421 map
= isl_map_realign(map
, exp
);
12424 isl_space_free(model
);
12427 isl_space_free(model
);
12432 __isl_give isl_set
*isl_set_align_params(__isl_take isl_set
*set
,
12433 __isl_take isl_space
*model
)
12435 return isl_map_align_params(set
, model
);
12438 /* Align the parameters of "bmap" to those of "model", introducing
12439 * additional parameters if needed.
12441 __isl_give isl_basic_map
*isl_basic_map_align_params(
12442 __isl_take isl_basic_map
*bmap
, __isl_take isl_space
*model
)
12445 isl_bool equal_params
;
12447 if (!bmap
|| !model
)
12450 ctx
= isl_space_get_ctx(model
);
12451 if (!isl_space_has_named_params(model
))
12452 isl_die(ctx
, isl_error_invalid
,
12453 "model has unnamed parameters", goto error
);
12454 if (isl_basic_map_check_named_params(bmap
) < 0)
12456 equal_params
= isl_space_has_equal_params(bmap
->dim
, model
);
12457 if (equal_params
< 0)
12459 if (!equal_params
) {
12460 isl_reordering
*exp
;
12461 struct isl_dim_map
*dim_map
;
12463 exp
= isl_parameter_alignment_reordering(bmap
->dim
, model
);
12464 exp
= isl_reordering_extend_space(exp
,
12465 isl_basic_map_get_space(bmap
));
12466 dim_map
= isl_dim_map_from_reordering(exp
);
12467 bmap
= isl_basic_map_realign(bmap
,
12468 isl_reordering_get_space(exp
),
12469 isl_dim_map_extend(dim_map
, bmap
));
12470 isl_reordering_free(exp
);
12471 isl_dim_map_free(dim_map
);
12474 isl_space_free(model
);
12477 isl_space_free(model
);
12478 isl_basic_map_free(bmap
);
12482 /* Do "bset" and "space" have the same parameters?
12484 isl_bool
isl_basic_set_space_has_equal_params(__isl_keep isl_basic_set
*bset
,
12485 __isl_keep isl_space
*space
)
12487 isl_space
*bset_space
;
12489 bset_space
= isl_basic_set_peek_space(bset
);
12490 return isl_space_has_equal_params(bset_space
, space
);
12493 /* Do "map" and "space" have the same parameters?
12495 isl_bool
isl_map_space_has_equal_params(__isl_keep isl_map
*map
,
12496 __isl_keep isl_space
*space
)
12498 isl_space
*map_space
;
12500 map_space
= isl_map_peek_space(map
);
12501 return isl_space_has_equal_params(map_space
, space
);
12504 /* Do "set" and "space" have the same parameters?
12506 isl_bool
isl_set_space_has_equal_params(__isl_keep isl_set
*set
,
12507 __isl_keep isl_space
*space
)
12509 return isl_map_space_has_equal_params(set_to_map(set
), space
);
12512 /* Align the parameters of "bset" to those of "model", introducing
12513 * additional parameters if needed.
12515 __isl_give isl_basic_set
*isl_basic_set_align_params(
12516 __isl_take isl_basic_set
*bset
, __isl_take isl_space
*model
)
12518 return isl_basic_map_align_params(bset
, model
);
12521 /* Drop all parameters not referenced by "map".
12523 __isl_give isl_map
*isl_map_drop_unused_params(__isl_take isl_map
*map
)
12528 n
= isl_map_dim(map
, isl_dim_param
);
12529 if (isl_map_check_named_params(map
) < 0 || n
< 0)
12530 return isl_map_free(map
);
12532 for (i
= n
- 1; i
>= 0; i
--) {
12535 involves
= isl_map_involves_dims(map
, isl_dim_param
, i
, 1);
12537 return isl_map_free(map
);
12539 map
= isl_map_project_out(map
, isl_dim_param
, i
, 1);
12545 /* Drop all parameters not referenced by "set".
12547 __isl_give isl_set
*isl_set_drop_unused_params(
12548 __isl_take isl_set
*set
)
12550 return set_from_map(isl_map_drop_unused_params(set_to_map(set
)));
12553 /* Drop all parameters not referenced by "bmap".
12555 __isl_give isl_basic_map
*isl_basic_map_drop_unused_params(
12556 __isl_take isl_basic_map
*bmap
)
12561 nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
12562 if (nparam
< 0 || isl_basic_map_check_named_params(bmap
) < 0)
12563 return isl_basic_map_free(bmap
);
12565 for (i
= nparam
- 1; i
>= 0; i
--) {
12568 involves
= isl_basic_map_involves_dims(bmap
,
12569 isl_dim_param
, i
, 1);
12571 return isl_basic_map_free(bmap
);
12573 bmap
= isl_basic_map_drop(bmap
, isl_dim_param
, i
, 1);
12579 /* Drop all parameters not referenced by "bset".
12581 __isl_give isl_basic_set
*isl_basic_set_drop_unused_params(
12582 __isl_take isl_basic_set
*bset
)
12584 return bset_from_bmap(isl_basic_map_drop_unused_params(
12585 bset_to_bmap(bset
)));
12588 /* Given a tuple of identifiers "tuple" in a space that corresponds
12589 * to that of "set", if any of those identifiers appear as parameters
12590 * in "set", then equate those parameters with the corresponding
12591 * set dimensions and project out the parameters.
12592 * The result therefore has no such parameters.
12594 static __isl_give isl_set
*equate_params(__isl_take isl_set
*set
,
12595 __isl_keep isl_multi_id
*tuple
)
12599 isl_space
*set_space
, *tuple_space
;
12601 set_space
= isl_set_peek_space(set
);
12602 tuple_space
= isl_multi_id_peek_space(tuple
);
12603 if (isl_space_check_equal_tuples(tuple_space
, set_space
) < 0)
12604 return isl_set_free(set
);
12605 n
= isl_multi_id_size(tuple
);
12607 return isl_set_free(set
);
12608 for (i
= 0; i
< n
; ++i
) {
12612 id
= isl_multi_id_get_at(tuple
, i
);
12614 return isl_set_free(set
);
12615 pos
= isl_set_find_dim_by_id(set
, isl_dim_param
, id
);
12619 set
= isl_set_equate(set
, isl_dim_param
, pos
, isl_dim_set
, i
);
12620 set
= isl_set_project_out(set
, isl_dim_param
, pos
, 1);
12625 /* Bind the set dimensions of "set" to parameters with identifiers
12626 * specified by "tuple", living in the same space as "set".
12628 * If no parameters with these identifiers appear in "set" already,
12629 * then the set dimensions are simply reinterpreted as parameters.
12630 * Otherwise, the parameters are first equated to the corresponding
12633 __isl_give isl_set
*isl_set_bind(__isl_take isl_set
*set
,
12634 __isl_take isl_multi_id
*tuple
)
12638 set
= equate_params(set
, tuple
);
12639 space
= isl_set_get_space(set
);
12640 space
= isl_space_bind_set(space
, tuple
);
12641 isl_multi_id_free(tuple
);
12642 set
= isl_set_reset_space(set
, space
);
12647 /* Given a tuple of identifiers "tuple" in a space that corresponds
12648 * to the domain of "map", if any of those identifiers appear as parameters
12649 * in "map", then equate those parameters with the corresponding
12650 * input dimensions and project out the parameters.
12651 * The result therefore has no such parameters.
12653 static __isl_give isl_map
*map_equate_params(__isl_take isl_map
*map
,
12654 __isl_keep isl_multi_id
*tuple
)
12658 isl_space
*map_space
, *tuple_space
;
12660 map_space
= isl_map_peek_space(map
);
12661 tuple_space
= isl_multi_id_peek_space(tuple
);
12662 if (isl_space_check_domain_tuples(tuple_space
, map_space
) < 0)
12663 return isl_map_free(map
);
12664 n
= isl_multi_id_size(tuple
);
12666 return isl_map_free(map
);
12667 for (i
= 0; i
< n
; ++i
) {
12671 id
= isl_multi_id_get_at(tuple
, i
);
12673 return isl_map_free(map
);
12674 pos
= isl_map_find_dim_by_id(map
, isl_dim_param
, id
);
12678 map
= isl_map_equate(map
, isl_dim_param
, pos
, isl_dim_in
, i
);
12679 map
= isl_map_project_out(map
, isl_dim_param
, pos
, 1);
12684 /* Bind the input dimensions of "map" to parameters with identifiers
12685 * specified by "tuple", living in the domain space of "map".
12687 * If no parameters with these identifiers appear in "map" already,
12688 * then the input dimensions are simply reinterpreted as parameters.
12689 * Otherwise, the parameters are first equated to the corresponding
12690 * input dimensions.
12692 __isl_give isl_set
*isl_map_bind_domain(__isl_take isl_map
*map
,
12693 __isl_take isl_multi_id
*tuple
)
12698 map
= map_equate_params(map
, tuple
);
12699 space
= isl_map_get_space(map
);
12700 space
= isl_space_bind_map_domain(space
, tuple
);
12701 isl_multi_id_free(tuple
);
12702 set
= set_from_map(isl_map_reset_space(map
, space
));
12707 /* Bind the output dimensions of "map" to parameters with identifiers
12708 * specified by "tuple", living in the range space of "map".
12710 * Since binding is more easily implemented on the domain,
12711 * bind the input dimensions of the inverse of "map".
12713 __isl_give isl_set
*isl_map_bind_range(__isl_take isl_map
*map
,
12714 __isl_take isl_multi_id
*tuple
)
12716 return isl_map_bind_domain(isl_map_reverse(map
), tuple
);
12719 /* Insert a domain corresponding to "tuple"
12720 * into the nullary or unary relation "set".
12721 * The result has an extra initial tuple and is therefore
12722 * either a unary or binary relation.
12723 * Any parameters with identifiers in "tuple" are reinterpreted
12724 * as the corresponding domain dimensions.
12726 static __isl_give isl_map
*unbind_params_insert_domain(
12727 __isl_take isl_set
*set
, __isl_take isl_multi_id
*tuple
)
12732 space
= isl_set_peek_space(set
);
12733 r
= isl_reordering_unbind_params_insert_domain(space
, tuple
);
12734 isl_multi_id_free(tuple
);
12736 return isl_map_realign(set_to_map(set
), r
);
12739 /* Construct a set with "tuple" as domain from the parameter domain "set".
12740 * Any parameters with identifiers in "tuple" are reinterpreted
12741 * as the corresponding set dimensions.
12743 __isl_give isl_set
*isl_set_unbind_params(__isl_take isl_set
*set
,
12744 __isl_take isl_multi_id
*tuple
)
12746 isl_bool is_params
;
12748 is_params
= isl_set_is_params(set
);
12750 set
= isl_set_free(set
);
12751 else if (!is_params
)
12752 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
12753 "expecting parameter domain", set
= isl_set_free(set
));
12754 return set_from_map(unbind_params_insert_domain(set
, tuple
));
12757 /* Check that "set" is a proper set, i.e., that it is not a parameter domain.
12759 static isl_stat
isl_set_check_is_set(__isl_keep isl_set
*set
)
12761 isl_bool is_params
;
12763 is_params
= isl_set_is_params(set
);
12765 return isl_stat_error
;
12766 else if (is_params
)
12767 isl_die(isl_set_get_ctx(set
), isl_error_invalid
,
12768 "expecting proper set", return isl_stat_error
);
12770 return isl_stat_ok
;
12773 /* Construct a map with "domain" as domain and "set" as range.
12774 * Any parameters with identifiers in "domain" are reinterpreted
12775 * as the corresponding domain dimensions.
12777 __isl_give isl_map
*isl_set_unbind_params_insert_domain(
12778 __isl_take isl_set
*set
, __isl_take isl_multi_id
*domain
)
12780 if (isl_set_check_is_set(set
) < 0)
12781 set
= isl_set_free(set
);
12782 return unbind_params_insert_domain(set
, domain
);
12785 /* Construct a map with "domain" as domain and "set" as range.
12787 __isl_give isl_map
*isl_set_insert_domain(__isl_take isl_set
*set
,
12788 __isl_take isl_space
*domain
)
12794 if (isl_set_check_is_set(set
) < 0 || isl_space_check_is_set(domain
) < 0)
12795 domain
= isl_space_free(domain
);
12796 dim
= isl_space_dim(domain
, isl_dim_set
);
12798 domain
= isl_space_free(domain
);
12799 space
= isl_set_get_space(set
);
12800 domain
= isl_space_replace_params(domain
, space
);
12801 space
= isl_space_map_from_domain_and_range(domain
, space
);
12803 map
= isl_map_from_range(set
);
12804 map
= isl_map_add_dims(map
, isl_dim_in
, dim
);
12805 map
= isl_map_reset_space(map
, space
);
12810 __isl_give isl_mat
*isl_basic_map_equalities_matrix(
12811 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
12812 enum isl_dim_type c2
, enum isl_dim_type c3
,
12813 enum isl_dim_type c4
, enum isl_dim_type c5
)
12815 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
12816 struct isl_mat
*mat
;
12821 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
12824 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_eq
, total
+ 1);
12827 for (i
= 0; i
< bmap
->n_eq
; ++i
)
12828 for (j
= 0, pos
= 0; j
< 5; ++j
) {
12829 int off
= isl_basic_map_offset(bmap
, c
[j
]);
12830 isl_size dim
= isl_basic_map_dim(bmap
, c
[j
]);
12832 return isl_mat_free(mat
);
12833 for (k
= 0; k
< dim
; ++k
) {
12834 isl_int_set(mat
->row
[i
][pos
],
12835 bmap
->eq
[i
][off
+ k
]);
12843 __isl_give isl_mat
*isl_basic_map_inequalities_matrix(
12844 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type c1
,
12845 enum isl_dim_type c2
, enum isl_dim_type c3
,
12846 enum isl_dim_type c4
, enum isl_dim_type c5
)
12848 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
12849 struct isl_mat
*mat
;
12854 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
12857 mat
= isl_mat_alloc(bmap
->ctx
, bmap
->n_ineq
, total
+ 1);
12860 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
12861 for (j
= 0, pos
= 0; j
< 5; ++j
) {
12862 int off
= isl_basic_map_offset(bmap
, c
[j
]);
12863 isl_size dim
= isl_basic_map_dim(bmap
, c
[j
]);
12865 return isl_mat_free(mat
);
12866 for (k
= 0; k
< dim
; ++k
) {
12867 isl_int_set(mat
->row
[i
][pos
],
12868 bmap
->ineq
[i
][off
+ k
]);
12876 __isl_give isl_basic_map
*isl_basic_map_from_constraint_matrices(
12877 __isl_take isl_space
*space
,
12878 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
12879 enum isl_dim_type c2
, enum isl_dim_type c3
,
12880 enum isl_dim_type c4
, enum isl_dim_type c5
)
12882 enum isl_dim_type c
[5] = { c1
, c2
, c3
, c4
, c5
};
12883 isl_basic_map
*bmap
= NULL
;
12890 dim
= isl_space_dim(space
, isl_dim_all
);
12891 if (dim
< 0 || !eq
|| !ineq
)
12894 if (eq
->n_col
!= ineq
->n_col
)
12895 isl_die(space
->ctx
, isl_error_invalid
,
12896 "equalities and inequalities matrices should have "
12897 "same number of columns", goto error
);
12901 if (eq
->n_col
< total
)
12902 isl_die(space
->ctx
, isl_error_invalid
,
12903 "number of columns too small", goto error
);
12905 extra
= eq
->n_col
- total
;
12907 bmap
= isl_basic_map_alloc_space(isl_space_copy(space
), extra
,
12908 eq
->n_row
, ineq
->n_row
);
12911 for (i
= 0; i
< extra
; ++i
) {
12912 k
= isl_basic_map_alloc_div(bmap
);
12915 isl_int_set_si(bmap
->div
[k
][0], 0);
12917 for (i
= 0; i
< eq
->n_row
; ++i
) {
12918 l
= isl_basic_map_alloc_equality(bmap
);
12921 for (j
= 0, pos
= 0; j
< 5; ++j
) {
12922 int off
= isl_basic_map_offset(bmap
, c
[j
]);
12923 isl_size dim
= isl_basic_map_dim(bmap
, c
[j
]);
12926 for (k
= 0; k
< dim
; ++k
) {
12927 isl_int_set(bmap
->eq
[l
][off
+ k
],
12933 for (i
= 0; i
< ineq
->n_row
; ++i
) {
12934 l
= isl_basic_map_alloc_inequality(bmap
);
12937 for (j
= 0, pos
= 0; j
< 5; ++j
) {
12938 int off
= isl_basic_map_offset(bmap
, c
[j
]);
12939 isl_size dim
= isl_basic_map_dim(bmap
, c
[j
]);
12942 for (k
= 0; k
< dim
; ++k
) {
12943 isl_int_set(bmap
->ineq
[l
][off
+ k
],
12944 ineq
->row
[i
][pos
]);
12950 isl_space_free(space
);
12952 isl_mat_free(ineq
);
12954 bmap
= isl_basic_map_simplify(bmap
);
12955 return isl_basic_map_finalize(bmap
);
12957 isl_space_free(space
);
12959 isl_mat_free(ineq
);
12960 isl_basic_map_free(bmap
);
12964 __isl_give isl_mat
*isl_basic_set_equalities_matrix(
12965 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
12966 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
12968 return isl_basic_map_equalities_matrix(bset_to_bmap(bset
),
12969 c1
, c2
, c3
, c4
, isl_dim_in
);
12972 __isl_give isl_mat
*isl_basic_set_inequalities_matrix(
12973 __isl_keep isl_basic_set
*bset
, enum isl_dim_type c1
,
12974 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
12976 return isl_basic_map_inequalities_matrix(bset_to_bmap(bset
),
12977 c1
, c2
, c3
, c4
, isl_dim_in
);
12980 __isl_give isl_basic_set
*isl_basic_set_from_constraint_matrices(
12981 __isl_take isl_space
*space
,
12982 __isl_take isl_mat
*eq
, __isl_take isl_mat
*ineq
, enum isl_dim_type c1
,
12983 enum isl_dim_type c2
, enum isl_dim_type c3
, enum isl_dim_type c4
)
12985 isl_basic_map
*bmap
;
12986 bmap
= isl_basic_map_from_constraint_matrices(space
, eq
, ineq
,
12987 c1
, c2
, c3
, c4
, isl_dim_in
);
12988 return bset_from_bmap(bmap
);
12991 isl_bool
isl_basic_map_can_zip(__isl_keep isl_basic_map
*bmap
)
12994 return isl_bool_error
;
12996 return isl_space_can_zip(bmap
->dim
);
12999 isl_bool
isl_map_can_zip(__isl_keep isl_map
*map
)
13002 return isl_bool_error
;
13004 return isl_space_can_zip(map
->dim
);
13007 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
13008 * (A -> C) -> (B -> D).
13010 __isl_give isl_basic_map
*isl_basic_map_zip(__isl_take isl_basic_map
*bmap
)
13020 if (!isl_basic_map_can_zip(bmap
))
13021 isl_die(bmap
->ctx
, isl_error_invalid
,
13022 "basic map cannot be zipped", goto error
);
13023 n_in
= isl_space_dim(bmap
->dim
->nested
[0], isl_dim_in
);
13024 n1
= isl_space_dim(bmap
->dim
->nested
[0], isl_dim_out
);
13025 n2
= isl_space_dim(bmap
->dim
->nested
[1], isl_dim_in
);
13026 if (n_in
< 0 || n1
< 0 || n2
< 0)
13027 return isl_basic_map_free(bmap
);
13028 pos
= isl_basic_map_offset(bmap
, isl_dim_in
) + n_in
;
13029 bmap
= isl_basic_map_cow(bmap
);
13030 bmap
= isl_basic_map_swap_vars(bmap
, pos
, n1
, n2
);
13033 bmap
->dim
= isl_space_zip(bmap
->dim
);
13036 bmap
= isl_basic_map_mark_final(bmap
);
13039 isl_basic_map_free(bmap
);
13043 /* Given a map (A -> B) -> (C -> D), return the corresponding map
13044 * (A -> C) -> (B -> D).
13046 __isl_give isl_map
*isl_map_zip(__isl_take isl_map
*map
)
13051 if (!isl_map_can_zip(map
))
13052 isl_die(map
->ctx
, isl_error_invalid
, "map cannot be zipped",
13055 return isl_map_transform(map
, &isl_space_zip
, &isl_basic_map_zip
);
13061 /* Can we apply isl_basic_map_curry to "bmap"?
13062 * That is, does it have a nested relation in its domain?
13064 isl_bool
isl_basic_map_can_curry(__isl_keep isl_basic_map
*bmap
)
13067 return isl_bool_error
;
13069 return isl_space_can_curry(bmap
->dim
);
13072 /* Can we apply isl_map_curry to "map"?
13073 * That is, does it have a nested relation in its domain?
13075 isl_bool
isl_map_can_curry(__isl_keep isl_map
*map
)
13078 return isl_bool_error
;
13080 return isl_space_can_curry(map
->dim
);
13083 /* Given a basic map (A -> B) -> C, return the corresponding basic map
13086 __isl_give isl_basic_map
*isl_basic_map_curry(__isl_take isl_basic_map
*bmap
)
13092 if (!isl_basic_map_can_curry(bmap
))
13093 isl_die(bmap
->ctx
, isl_error_invalid
,
13094 "basic map cannot be curried", goto error
);
13095 bmap
= isl_basic_map_cow(bmap
);
13098 bmap
->dim
= isl_space_curry(bmap
->dim
);
13101 bmap
= isl_basic_map_mark_final(bmap
);
13104 isl_basic_map_free(bmap
);
13108 /* Given a map (A -> B) -> C, return the corresponding map
13111 __isl_give isl_map
*isl_map_curry(__isl_take isl_map
*map
)
13113 return isl_map_change_space(map
, &isl_map_can_curry
,
13114 "map cannot be curried", &isl_space_curry
);
13117 /* Can isl_map_range_curry be applied to "map"?
13118 * That is, does it have a nested relation in its range,
13119 * the domain of which is itself a nested relation?
13121 isl_bool
isl_map_can_range_curry(__isl_keep isl_map
*map
)
13124 return isl_bool_error
;
13126 return isl_space_can_range_curry(map
->dim
);
13129 /* Given a map A -> ((B -> C) -> D), return the corresponding map
13130 * A -> (B -> (C -> D)).
13132 __isl_give isl_map
*isl_map_range_curry(__isl_take isl_map
*map
)
13134 return isl_map_change_space(map
, &isl_map_can_range_curry
,
13135 "map range cannot be curried",
13136 &isl_space_range_curry
);
13139 /* Can we apply isl_basic_map_uncurry to "bmap"?
13140 * That is, does it have a nested relation in its domain?
13142 isl_bool
isl_basic_map_can_uncurry(__isl_keep isl_basic_map
*bmap
)
13145 return isl_bool_error
;
13147 return isl_space_can_uncurry(bmap
->dim
);
13150 /* Can we apply isl_map_uncurry to "map"?
13151 * That is, does it have a nested relation in its domain?
13153 isl_bool
isl_map_can_uncurry(__isl_keep isl_map
*map
)
13156 return isl_bool_error
;
13158 return isl_space_can_uncurry(map
->dim
);
13161 /* Given a basic map A -> (B -> C), return the corresponding basic map
13164 __isl_give isl_basic_map
*isl_basic_map_uncurry(__isl_take isl_basic_map
*bmap
)
13170 if (!isl_basic_map_can_uncurry(bmap
))
13171 isl_die(bmap
->ctx
, isl_error_invalid
,
13172 "basic map cannot be uncurried",
13173 return isl_basic_map_free(bmap
));
13174 bmap
= isl_basic_map_cow(bmap
);
13177 bmap
->dim
= isl_space_uncurry(bmap
->dim
);
13179 return isl_basic_map_free(bmap
);
13180 bmap
= isl_basic_map_mark_final(bmap
);
13184 /* Given a map A -> (B -> C), return the corresponding map
13187 __isl_give isl_map
*isl_map_uncurry(__isl_take isl_map
*map
)
13189 return isl_map_change_space(map
, &isl_map_can_uncurry
,
13190 "map cannot be uncurried", &isl_space_uncurry
);
13193 __isl_give isl_set
*isl_set_equate(__isl_take isl_set
*set
,
13194 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13196 return isl_map_equate(set
, type1
, pos1
, type2
, pos2
);
13199 /* Construct a basic map where the given dimensions are equal to each other.
13201 static __isl_give isl_basic_map
*equator(__isl_take isl_space
*space
,
13202 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13204 isl_basic_map
*bmap
= NULL
;
13208 total
= isl_space_dim(space
, isl_dim_all
);
13210 isl_space_check_range(space
, type1
, pos1
, 1) < 0 ||
13211 isl_space_check_range(space
, type2
, pos2
, 1) < 0)
13214 if (type1
== type2
&& pos1
== pos2
)
13215 return isl_basic_map_universe(space
);
13217 bmap
= isl_basic_map_alloc_space(isl_space_copy(space
), 0, 1, 0);
13218 i
= isl_basic_map_alloc_equality(bmap
);
13221 isl_seq_clr(bmap
->eq
[i
], 1 + total
);
13222 pos1
+= isl_basic_map_offset(bmap
, type1
);
13223 pos2
+= isl_basic_map_offset(bmap
, type2
);
13224 isl_int_set_si(bmap
->eq
[i
][pos1
], -1);
13225 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
13226 bmap
= isl_basic_map_finalize(bmap
);
13227 isl_space_free(space
);
13230 isl_space_free(space
);
13231 isl_basic_map_free(bmap
);
13235 /* Add a constraint imposing that the given two dimensions are equal.
13237 __isl_give isl_basic_map
*isl_basic_map_equate(__isl_take isl_basic_map
*bmap
,
13238 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13242 eq
= equator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
13244 bmap
= isl_basic_map_intersect(bmap
, eq
);
13249 /* Add a constraint imposing that the given two dimensions are equal.
13251 __isl_give isl_map
*isl_map_equate(__isl_take isl_map
*map
,
13252 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13254 isl_basic_map
*bmap
;
13256 bmap
= equator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
13258 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
13263 /* Add a constraint imposing that the given two dimensions have opposite values.
13265 __isl_give isl_map
*isl_map_oppose(__isl_take isl_map
*map
,
13266 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13268 isl_basic_map
*bmap
= NULL
;
13272 if (isl_map_check_range(map
, type1
, pos1
, 1) < 0)
13273 return isl_map_free(map
);
13274 if (isl_map_check_range(map
, type2
, pos2
, 1) < 0)
13275 return isl_map_free(map
);
13277 total
= isl_map_dim(map
, isl_dim_all
);
13279 return isl_map_free(map
);
13280 bmap
= isl_basic_map_alloc_space(isl_map_get_space(map
), 0, 1, 0);
13281 i
= isl_basic_map_alloc_equality(bmap
);
13284 isl_seq_clr(bmap
->eq
[i
], 1 + total
);
13285 pos1
+= isl_basic_map_offset(bmap
, type1
);
13286 pos2
+= isl_basic_map_offset(bmap
, type2
);
13287 isl_int_set_si(bmap
->eq
[i
][pos1
], 1);
13288 isl_int_set_si(bmap
->eq
[i
][pos2
], 1);
13289 bmap
= isl_basic_map_finalize(bmap
);
13291 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
13295 isl_basic_map_free(bmap
);
13300 /* Construct a constraint imposing that the value of the first dimension is
13301 * greater than or equal to that of the second.
13303 static __isl_give isl_constraint
*constraint_order_ge(
13304 __isl_take isl_space
*space
, enum isl_dim_type type1
, int pos1
,
13305 enum isl_dim_type type2
, int pos2
)
13309 if (isl_space_check_range(space
, type1
, pos1
, 1) < 0 ||
13310 isl_space_check_range(space
, type2
, pos2
, 1) < 0)
13311 space
= isl_space_free(space
);
13315 c
= isl_constraint_alloc_inequality(isl_local_space_from_space(space
));
13317 if (type1
== type2
&& pos1
== pos2
)
13320 c
= isl_constraint_set_coefficient_si(c
, type1
, pos1
, 1);
13321 c
= isl_constraint_set_coefficient_si(c
, type2
, pos2
, -1);
13326 /* Add a constraint imposing that the value of the first dimension is
13327 * greater than or equal to that of the second.
13329 __isl_give isl_basic_map
*isl_basic_map_order_ge(__isl_take isl_basic_map
*bmap
,
13330 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13335 if (type1
== type2
&& pos1
== pos2
)
13337 space
= isl_basic_map_get_space(bmap
);
13338 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
13339 bmap
= isl_basic_map_add_constraint(bmap
, c
);
13344 /* Add a constraint imposing that the value of the first dimension is
13345 * greater than or equal to that of the second.
13347 __isl_give isl_map
*isl_map_order_ge(__isl_take isl_map
*map
,
13348 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13353 if (type1
== type2
&& pos1
== pos2
)
13355 space
= isl_map_get_space(map
);
13356 c
= constraint_order_ge(space
, type1
, pos1
, type2
, pos2
);
13357 map
= isl_map_add_constraint(map
, c
);
13362 /* Add a constraint imposing that the value of the first dimension is
13363 * less than or equal to that of the second.
13365 __isl_give isl_map
*isl_map_order_le(__isl_take isl_map
*map
,
13366 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13368 return isl_map_order_ge(map
, type2
, pos2
, type1
, pos1
);
13371 /* Construct a basic map where the value of the first dimension is
13372 * greater than that of the second.
13374 static __isl_give isl_basic_map
*greator(__isl_take isl_space
*space
,
13375 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13377 isl_basic_map
*bmap
= NULL
;
13381 if (isl_space_check_range(space
, type1
, pos1
, 1) < 0 ||
13382 isl_space_check_range(space
, type2
, pos2
, 1) < 0)
13385 if (type1
== type2
&& pos1
== pos2
)
13386 return isl_basic_map_empty(space
);
13388 bmap
= isl_basic_map_alloc_space(space
, 0, 0, 1);
13389 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
13390 i
= isl_basic_map_alloc_inequality(bmap
);
13391 if (total
< 0 || i
< 0)
13392 return isl_basic_map_free(bmap
);
13393 isl_seq_clr(bmap
->ineq
[i
], 1 + total
);
13394 pos1
+= isl_basic_map_offset(bmap
, type1
);
13395 pos2
+= isl_basic_map_offset(bmap
, type2
);
13396 isl_int_set_si(bmap
->ineq
[i
][pos1
], 1);
13397 isl_int_set_si(bmap
->ineq
[i
][pos2
], -1);
13398 isl_int_set_si(bmap
->ineq
[i
][0], -1);
13399 bmap
= isl_basic_map_finalize(bmap
);
13403 isl_space_free(space
);
13404 isl_basic_map_free(bmap
);
13408 /* Add a constraint imposing that the value of the first dimension is
13409 * greater than that of the second.
13411 __isl_give isl_basic_map
*isl_basic_map_order_gt(__isl_take isl_basic_map
*bmap
,
13412 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13416 gt
= greator(isl_basic_map_get_space(bmap
), type1
, pos1
, type2
, pos2
);
13418 bmap
= isl_basic_map_intersect(bmap
, gt
);
13423 /* Add a constraint imposing that the value of the first dimension is
13424 * greater than that of the second.
13426 __isl_give isl_map
*isl_map_order_gt(__isl_take isl_map
*map
,
13427 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13429 isl_basic_map
*bmap
;
13431 bmap
= greator(isl_map_get_space(map
), type1
, pos1
, type2
, pos2
);
13433 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
13438 /* Add a constraint imposing that the value of the first dimension is
13439 * smaller than that of the second.
13441 __isl_give isl_map
*isl_map_order_lt(__isl_take isl_map
*map
,
13442 enum isl_dim_type type1
, int pos1
, enum isl_dim_type type2
, int pos2
)
13444 return isl_map_order_gt(map
, type2
, pos2
, type1
, pos1
);
13447 __isl_give isl_aff
*isl_basic_map_get_div(__isl_keep isl_basic_map
*bmap
,
13451 isl_local_space
*ls
;
13456 if (!isl_basic_map_divs_known(bmap
))
13457 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
13458 "some divs are unknown", return NULL
);
13460 ls
= isl_basic_map_get_local_space(bmap
);
13461 div
= isl_local_space_get_div(ls
, pos
);
13462 isl_local_space_free(ls
);
13467 __isl_give isl_aff
*isl_basic_set_get_div(__isl_keep isl_basic_set
*bset
,
13470 return isl_basic_map_get_div(bset
, pos
);
13473 /* Plug in "subs" for dimension "type", "pos" of "bset".
13475 * Let i be the dimension to replace and let "subs" be of the form
13479 * Any integer division with a non-zero coefficient for i,
13481 * floor((a i + g)/m)
13485 * floor((a f + d g)/(m d))
13487 * Constraints of the form
13495 * We currently require that "subs" is an integral expression.
13496 * Handling rational expressions may require us to add stride constraints
13497 * as we do in isl_basic_set_preimage_multi_aff.
13499 __isl_give isl_basic_set
*isl_basic_set_substitute(
13500 __isl_take isl_basic_set
*bset
,
13501 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
13508 if (bset
&& isl_basic_set_plain_is_empty(bset
))
13511 bset
= isl_basic_set_cow(bset
);
13512 if (!bset
|| !subs
)
13515 ctx
= isl_basic_set_get_ctx(bset
);
13516 if (!isl_space_is_equal(bset
->dim
, subs
->ls
->dim
))
13517 isl_die(ctx
, isl_error_invalid
,
13518 "spaces don't match", goto error
);
13519 n_div
= isl_local_space_dim(subs
->ls
, isl_dim_div
);
13523 isl_die(ctx
, isl_error_unsupported
,
13524 "cannot handle divs yet", goto error
);
13525 if (!isl_int_is_one(subs
->v
->el
[0]))
13526 isl_die(ctx
, isl_error_invalid
,
13527 "can only substitute integer expressions", goto error
);
13529 pos
+= isl_basic_set_offset(bset
, type
);
13533 for (i
= 0; i
< bset
->n_eq
; ++i
) {
13534 if (isl_int_is_zero(bset
->eq
[i
][pos
]))
13536 isl_int_set(v
, bset
->eq
[i
][pos
]);
13537 isl_int_set_si(bset
->eq
[i
][pos
], 0);
13538 isl_seq_combine(bset
->eq
[i
], subs
->v
->el
[0], bset
->eq
[i
],
13539 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
13542 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
13543 if (isl_int_is_zero(bset
->ineq
[i
][pos
]))
13545 isl_int_set(v
, bset
->ineq
[i
][pos
]);
13546 isl_int_set_si(bset
->ineq
[i
][pos
], 0);
13547 isl_seq_combine(bset
->ineq
[i
], subs
->v
->el
[0], bset
->ineq
[i
],
13548 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
13551 for (i
= 0; i
< bset
->n_div
; ++i
) {
13552 if (isl_int_is_zero(bset
->div
[i
][1 + pos
]))
13554 isl_int_set(v
, bset
->div
[i
][1 + pos
]);
13555 isl_int_set_si(bset
->div
[i
][1 + pos
], 0);
13556 isl_seq_combine(bset
->div
[i
] + 1,
13557 subs
->v
->el
[0], bset
->div
[i
] + 1,
13558 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
13559 isl_int_mul(bset
->div
[i
][0], bset
->div
[i
][0], subs
->v
->el
[0]);
13564 bset
= isl_basic_set_simplify(bset
);
13565 return isl_basic_set_finalize(bset
);
13567 isl_basic_set_free(bset
);
13571 /* Plug in "subs" for dimension "type", "pos" of "set".
13573 __isl_give isl_set
*isl_set_substitute(__isl_take isl_set
*set
,
13574 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
13578 if (set
&& isl_set_plain_is_empty(set
))
13581 set
= isl_set_cow(set
);
13585 for (i
= set
->n
- 1; i
>= 0; --i
) {
13586 set
->p
[i
] = isl_basic_set_substitute(set
->p
[i
], type
, pos
, subs
);
13587 set
= set_from_map(remove_if_empty(set_to_map(set
), i
));
13598 /* Check if the range of "ma" is compatible with the domain or range
13599 * (depending on "type") of "bmap".
13601 static isl_stat
check_basic_map_compatible_range_multi_aff(
13602 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type type
,
13603 __isl_keep isl_multi_aff
*ma
)
13606 isl_space
*ma_space
;
13608 ma_space
= isl_multi_aff_get_space(ma
);
13610 m
= isl_space_has_equal_params(bmap
->dim
, ma_space
);
13614 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
13615 "parameters don't match", goto error
);
13616 m
= isl_space_tuple_is_equal(bmap
->dim
, type
, ma_space
, isl_dim_out
);
13620 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
13621 "spaces don't match", goto error
);
13623 isl_space_free(ma_space
);
13624 return isl_stat_ok
;
13626 isl_space_free(ma_space
);
13627 return isl_stat_error
;
13630 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
13631 * coefficients before the transformed range of dimensions,
13632 * the "n_after" coefficients after the transformed range of dimensions
13633 * and the coefficients of the other divs in "bmap".
13635 static __isl_give isl_basic_map
*set_ma_divs(__isl_take isl_basic_map
*bmap
,
13636 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
, int n_div
)
13641 isl_local_space
*ls
;
13646 ls
= isl_aff_get_domain_local_space(ma
->u
.p
[0]);
13647 n_param
= isl_local_space_dim(ls
, isl_dim_param
);
13648 n_set
= isl_local_space_dim(ls
, isl_dim_set
);
13649 if (n_param
< 0 || n_set
< 0)
13650 return isl_basic_map_free(bmap
);
13652 for (i
= 0; i
< n_div
; ++i
) {
13653 int o_bmap
= 0, o_ls
= 0;
13655 isl_seq_cpy(bmap
->div
[i
], ls
->div
->row
[i
], 1 + 1 + n_param
);
13656 o_bmap
+= 1 + 1 + n_param
;
13657 o_ls
+= 1 + 1 + n_param
;
13658 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_before
);
13659 o_bmap
+= n_before
;
13660 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
13661 ls
->div
->row
[i
] + o_ls
, n_set
);
13664 isl_seq_clr(bmap
->div
[i
] + o_bmap
, n_after
);
13666 isl_seq_cpy(bmap
->div
[i
] + o_bmap
,
13667 ls
->div
->row
[i
] + o_ls
, n_div
);
13670 isl_seq_clr(bmap
->div
[i
] + o_bmap
, bmap
->n_div
- n_div
);
13671 bmap
= isl_basic_map_add_div_constraints(bmap
, i
);
13676 isl_local_space_free(ls
);
13679 isl_local_space_free(ls
);
13680 return isl_basic_map_free(bmap
);
13683 /* How many stride constraints does "ma" enforce?
13684 * That is, how many of the affine expressions have a denominator
13685 * different from one?
13687 static int multi_aff_strides(__isl_keep isl_multi_aff
*ma
)
13692 for (i
= 0; i
< ma
->n
; ++i
)
13693 if (!isl_int_is_one(ma
->u
.p
[i
]->v
->el
[0]))
13699 /* For each affine expression in ma of the form
13701 * x_i = (f_i y + h_i)/m_i
13703 * with m_i different from one, add a constraint to "bmap"
13706 * f_i y + h_i = m_i alpha_i
13708 * with alpha_i an additional existentially quantified variable.
13710 * The input variables of "ma" correspond to a subset of the variables
13711 * of "bmap". There are "n_before" variables in "bmap" before this
13712 * subset and "n_after" variables after this subset.
13713 * The integer divisions of the affine expressions in "ma" are assumed
13714 * to have been aligned. There are "n_div_ma" of them and
13715 * they appear first in "bmap", straight after the "n_after" variables.
13717 static __isl_give isl_basic_map
*add_ma_strides(
13718 __isl_take isl_basic_map
*bmap
, __isl_keep isl_multi_aff
*ma
,
13719 int n_before
, int n_after
, int n_div_ma
)
13727 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
13728 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
13729 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
13730 if (total
< 0 || n_param
< 0 || n_in
< 0)
13731 return isl_basic_map_free(bmap
);
13732 for (i
= 0; i
< ma
->n
; ++i
) {
13733 int o_bmap
= 0, o_ma
= 1;
13735 if (isl_int_is_one(ma
->u
.p
[i
]->v
->el
[0]))
13737 div
= isl_basic_map_alloc_div(bmap
);
13738 k
= isl_basic_map_alloc_equality(bmap
);
13739 if (div
< 0 || k
< 0)
13741 isl_int_set_si(bmap
->div
[div
][0], 0);
13742 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
13743 ma
->u
.p
[i
]->v
->el
+ o_ma
, 1 + n_param
);
13744 o_bmap
+= 1 + n_param
;
13745 o_ma
+= 1 + n_param
;
13746 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_before
);
13747 o_bmap
+= n_before
;
13748 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
13749 ma
->u
.p
[i
]->v
->el
+ o_ma
, n_in
);
13752 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, n_after
);
13754 isl_seq_cpy(bmap
->eq
[k
] + o_bmap
,
13755 ma
->u
.p
[i
]->v
->el
+ o_ma
, n_div_ma
);
13756 o_bmap
+= n_div_ma
;
13758 isl_seq_clr(bmap
->eq
[k
] + o_bmap
, 1 + total
- o_bmap
);
13759 isl_int_neg(bmap
->eq
[k
][1 + total
], ma
->u
.p
[i
]->v
->el
[0]);
13765 isl_basic_map_free(bmap
);
13769 /* Replace the domain or range space (depending on "type) of "space" by "set".
13771 static __isl_give isl_space
*isl_space_set(__isl_take isl_space
*space
,
13772 enum isl_dim_type type
, __isl_take isl_space
*set
)
13774 if (type
== isl_dim_in
) {
13775 space
= isl_space_range(space
);
13776 space
= isl_space_map_from_domain_and_range(set
, space
);
13778 space
= isl_space_domain(space
);
13779 space
= isl_space_map_from_domain_and_range(space
, set
);
13785 /* Compute the preimage of the domain or range (depending on "type")
13786 * of "bmap" under the function represented by "ma".
13787 * In other words, plug in "ma" in the domain or range of "bmap".
13788 * The result is a basic map that lives in the same space as "bmap"
13789 * except that the domain or range has been replaced by
13790 * the domain space of "ma".
13792 * If bmap is represented by
13794 * A(p) + S u + B x + T v + C(divs) >= 0,
13796 * where u and x are input and output dimensions if type == isl_dim_out
13797 * while x and v are input and output dimensions if type == isl_dim_in,
13798 * and ma is represented by
13800 * x = D(p) + F(y) + G(divs')
13802 * then the result is
13804 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
13806 * The divs in the input set are similarly adjusted.
13809 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
13813 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
13814 * B_i G(divs') + c_i(divs))/n_i)
13816 * If bmap is not a rational map and if F(y) involves any denominators
13818 * x_i = (f_i y + h_i)/m_i
13820 * then additional constraints are added to ensure that we only
13821 * map back integer points. That is we enforce
13823 * f_i y + h_i = m_i alpha_i
13825 * with alpha_i an additional existentially quantified variable.
13827 * We first copy over the divs from "ma".
13828 * Then we add the modified constraints and divs from "bmap".
13829 * Finally, we add the stride constraints, if needed.
13831 __isl_give isl_basic_map
*isl_basic_map_preimage_multi_aff(
13832 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
,
13833 __isl_take isl_multi_aff
*ma
)
13837 isl_basic_map
*res
= NULL
;
13838 isl_size n_before
, n_after
, n_div_bmap
, n_div_ma
;
13839 isl_int f
, c1
, c2
, g
;
13848 ma
= isl_multi_aff_align_divs(ma
);
13851 if (check_basic_map_compatible_range_multi_aff(bmap
, type
, ma
) < 0)
13854 if (type
== isl_dim_in
) {
13856 n_after
= isl_basic_map_dim(bmap
, isl_dim_out
);
13858 n_before
= isl_basic_map_dim(bmap
, isl_dim_in
);
13861 n_div_bmap
= isl_basic_map_dim(bmap
, isl_dim_div
);
13862 n_div_ma
= ma
->n
? isl_aff_dim(ma
->u
.p
[0], isl_dim_div
) : 0;
13863 if (n_before
< 0 || n_after
< 0 || n_div_bmap
< 0 || n_div_ma
< 0)
13866 space
= isl_multi_aff_get_domain_space(ma
);
13867 space
= isl_space_set(isl_basic_map_get_space(bmap
), type
, space
);
13868 rational
= isl_basic_map_is_rational(bmap
);
13869 strides
= rational
? 0 : multi_aff_strides(ma
);
13870 res
= isl_basic_map_alloc_space(space
, n_div_ma
+ n_div_bmap
+ strides
,
13871 bmap
->n_eq
+ strides
, bmap
->n_ineq
+ 2 * n_div_ma
);
13873 res
= isl_basic_map_set_rational(res
);
13875 for (i
= 0; i
< n_div_ma
+ n_div_bmap
; ++i
)
13876 if (isl_basic_map_alloc_div(res
) < 0)
13879 res
= set_ma_divs(res
, ma
, n_before
, n_after
, n_div_ma
);
13883 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
13884 k
= isl_basic_map_alloc_equality(res
);
13887 if (isl_seq_preimage(res
->eq
[k
], bmap
->eq
[i
], ma
, n_before
,
13888 n_after
, n_div_ma
, n_div_bmap
,
13889 f
, c1
, c2
, g
, 0) < 0)
13893 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
13894 k
= isl_basic_map_alloc_inequality(res
);
13897 if (isl_seq_preimage(res
->ineq
[k
], bmap
->ineq
[i
], ma
, n_before
,
13898 n_after
, n_div_ma
, n_div_bmap
,
13899 f
, c1
, c2
, g
, 0) < 0)
13903 for (i
= 0; i
< bmap
->n_div
; ++i
) {
13904 if (isl_int_is_zero(bmap
->div
[i
][0])) {
13905 isl_int_set_si(res
->div
[n_div_ma
+ i
][0], 0);
13908 if (isl_seq_preimage(res
->div
[n_div_ma
+ i
], bmap
->div
[i
], ma
,
13909 n_before
, n_after
, n_div_ma
, n_div_bmap
,
13910 f
, c1
, c2
, g
, 1) < 0)
13915 res
= add_ma_strides(res
, ma
, n_before
, n_after
, n_div_ma
);
13921 isl_basic_map_free(bmap
);
13922 isl_multi_aff_free(ma
);
13923 res
= isl_basic_map_simplify(res
);
13924 return isl_basic_map_finalize(res
);
13930 isl_basic_map_free(bmap
);
13931 isl_multi_aff_free(ma
);
13932 isl_basic_map_free(res
);
13936 /* Compute the preimage of "bset" under the function represented by "ma".
13937 * In other words, plug in "ma" in "bset". The result is a basic set
13938 * that lives in the domain space of "ma".
13940 __isl_give isl_basic_set
*isl_basic_set_preimage_multi_aff(
13941 __isl_take isl_basic_set
*bset
, __isl_take isl_multi_aff
*ma
)
13943 return isl_basic_map_preimage_multi_aff(bset
, isl_dim_set
, ma
);
13946 /* Compute the preimage of the domain of "bmap" under the function
13947 * represented by "ma".
13948 * In other words, plug in "ma" in the domain of "bmap".
13949 * The result is a basic map that lives in the same space as "bmap"
13950 * except that the domain has been replaced by the domain space of "ma".
13952 __isl_give isl_basic_map
*isl_basic_map_preimage_domain_multi_aff(
13953 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
13955 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_in
, ma
);
13958 /* Compute the preimage of the range of "bmap" under the function
13959 * represented by "ma".
13960 * In other words, plug in "ma" in the range of "bmap".
13961 * The result is a basic map that lives in the same space as "bmap"
13962 * except that the range has been replaced by the domain space of "ma".
13964 __isl_give isl_basic_map
*isl_basic_map_preimage_range_multi_aff(
13965 __isl_take isl_basic_map
*bmap
, __isl_take isl_multi_aff
*ma
)
13967 return isl_basic_map_preimage_multi_aff(bmap
, isl_dim_out
, ma
);
13970 /* Check if the range of "ma" is compatible with the domain or range
13971 * (depending on "type") of "map".
13972 * Return isl_stat_error if anything is wrong.
13974 static isl_stat
check_map_compatible_range_multi_aff(
13975 __isl_keep isl_map
*map
, enum isl_dim_type type
,
13976 __isl_keep isl_multi_aff
*ma
)
13979 isl_space
*ma_space
;
13981 ma_space
= isl_multi_aff_get_space(ma
);
13982 m
= isl_map_space_tuple_is_equal(map
, type
, ma_space
, isl_dim_out
);
13983 isl_space_free(ma_space
);
13985 return isl_stat_error
;
13987 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
13988 "spaces don't match", return isl_stat_error
);
13989 return isl_stat_ok
;
13992 /* Compute the preimage of the domain or range (depending on "type")
13993 * of "map" under the function represented by "ma".
13994 * In other words, plug in "ma" in the domain or range of "map".
13995 * The result is a map that lives in the same space as "map"
13996 * except that the domain or range has been replaced by
13997 * the domain space of "ma".
13999 * The parameters are assumed to have been aligned.
14001 static __isl_give isl_map
*map_preimage_multi_aff(__isl_take isl_map
*map
,
14002 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
14007 map
= isl_map_cow(map
);
14008 ma
= isl_multi_aff_align_divs(ma
);
14011 if (check_map_compatible_range_multi_aff(map
, type
, ma
) < 0)
14014 for (i
= 0; i
< map
->n
; ++i
) {
14015 map
->p
[i
] = isl_basic_map_preimage_multi_aff(map
->p
[i
], type
,
14016 isl_multi_aff_copy(ma
));
14021 space
= isl_multi_aff_get_domain_space(ma
);
14022 space
= isl_space_set(isl_map_get_space(map
), type
, space
);
14024 isl_space_free(isl_map_take_space(map
));
14025 map
= isl_map_restore_space(map
, space
);
14029 isl_multi_aff_free(ma
);
14031 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
14032 ISL_F_CLR(map
, ISL_SET_NORMALIZED
);
14035 isl_multi_aff_free(ma
);
14040 /* Compute the preimage of the domain or range (depending on "type")
14041 * of "map" under the function represented by "ma".
14042 * In other words, plug in "ma" in the domain or range of "map".
14043 * The result is a map that lives in the same space as "map"
14044 * except that the domain or range has been replaced by
14045 * the domain space of "ma".
14047 __isl_give isl_map
*isl_map_preimage_multi_aff(__isl_take isl_map
*map
,
14048 enum isl_dim_type type
, __isl_take isl_multi_aff
*ma
)
14055 aligned
= isl_map_space_has_equal_params(map
, ma
->space
);
14059 return map_preimage_multi_aff(map
, type
, ma
);
14061 if (isl_map_check_named_params(map
) < 0)
14063 if (!isl_space_has_named_params(ma
->space
))
14064 isl_die(map
->ctx
, isl_error_invalid
,
14065 "unaligned unnamed parameters", goto error
);
14066 map
= isl_map_align_params(map
, isl_multi_aff_get_space(ma
));
14067 ma
= isl_multi_aff_align_params(ma
, isl_map_get_space(map
));
14069 return map_preimage_multi_aff(map
, type
, ma
);
14071 isl_multi_aff_free(ma
);
14072 return isl_map_free(map
);
14075 /* Compute the preimage of "set" under the function represented by "ma".
14076 * In other words, plug in "ma" in "set". The result is a set
14077 * that lives in the domain space of "ma".
14079 __isl_give isl_set
*isl_set_preimage_multi_aff(__isl_take isl_set
*set
,
14080 __isl_take isl_multi_aff
*ma
)
14082 return isl_map_preimage_multi_aff(set
, isl_dim_set
, ma
);
14085 /* Compute the preimage of the domain of "map" under the function
14086 * represented by "ma".
14087 * In other words, plug in "ma" in the domain of "map".
14088 * The result is a map that lives in the same space as "map"
14089 * except that the domain has been replaced by the domain space of "ma".
14091 __isl_give isl_map
*isl_map_preimage_domain_multi_aff(__isl_take isl_map
*map
,
14092 __isl_take isl_multi_aff
*ma
)
14094 return isl_map_preimage_multi_aff(map
, isl_dim_in
, ma
);
14097 /* Compute the preimage of the range of "map" under the function
14098 * represented by "ma".
14099 * In other words, plug in "ma" in the range of "map".
14100 * The result is a map that lives in the same space as "map"
14101 * except that the range has been replaced by the domain space of "ma".
14103 __isl_give isl_map
*isl_map_preimage_range_multi_aff(__isl_take isl_map
*map
,
14104 __isl_take isl_multi_aff
*ma
)
14106 return isl_map_preimage_multi_aff(map
, isl_dim_out
, ma
);
14109 /* Compute the preimage of "map" under the function represented by "pma".
14110 * In other words, plug in "pma" in the domain or range of "map".
14111 * The result is a map that lives in the same space as "map",
14112 * except that the space of type "type" has been replaced by
14113 * the domain space of "pma".
14115 * The parameters of "map" and "pma" are assumed to have been aligned.
14117 static __isl_give isl_map
*isl_map_preimage_pw_multi_aff_aligned(
14118 __isl_take isl_map
*map
, enum isl_dim_type type
,
14119 __isl_take isl_pw_multi_aff
*pma
)
14128 isl_pw_multi_aff_free(pma
);
14129 res
= isl_map_empty(isl_map_get_space(map
));
14134 res
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
14135 isl_multi_aff_copy(pma
->p
[0].maff
));
14136 if (type
== isl_dim_in
)
14137 res
= isl_map_intersect_domain(res
,
14138 isl_map_copy(pma
->p
[0].set
));
14140 res
= isl_map_intersect_range(res
,
14141 isl_map_copy(pma
->p
[0].set
));
14143 for (i
= 1; i
< pma
->n
; ++i
) {
14146 res_i
= isl_map_preimage_multi_aff(isl_map_copy(map
), type
,
14147 isl_multi_aff_copy(pma
->p
[i
].maff
));
14148 if (type
== isl_dim_in
)
14149 res_i
= isl_map_intersect_domain(res_i
,
14150 isl_map_copy(pma
->p
[i
].set
));
14152 res_i
= isl_map_intersect_range(res_i
,
14153 isl_map_copy(pma
->p
[i
].set
));
14154 res
= isl_map_union(res
, res_i
);
14157 isl_pw_multi_aff_free(pma
);
14161 isl_pw_multi_aff_free(pma
);
14166 /* Compute the preimage of "map" under the function represented by "pma".
14167 * In other words, plug in "pma" in the domain or range of "map".
14168 * The result is a map that lives in the same space as "map",
14169 * except that the space of type "type" has been replaced by
14170 * the domain space of "pma".
14172 __isl_give isl_map
*isl_map_preimage_pw_multi_aff(__isl_take isl_map
*map
,
14173 enum isl_dim_type type
, __isl_take isl_pw_multi_aff
*pma
)
14180 aligned
= isl_map_space_has_equal_params(map
, pma
->dim
);
14184 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
14186 if (isl_map_check_named_params(map
) < 0)
14188 if (isl_pw_multi_aff_check_named_params(pma
) < 0)
14190 map
= isl_map_align_params(map
, isl_pw_multi_aff_get_space(pma
));
14191 pma
= isl_pw_multi_aff_align_params(pma
, isl_map_get_space(map
));
14193 return isl_map_preimage_pw_multi_aff_aligned(map
, type
, pma
);
14195 isl_pw_multi_aff_free(pma
);
14196 return isl_map_free(map
);
14199 /* Compute the preimage of "set" under the function represented by "pma".
14200 * In other words, plug in "pma" in "set". The result is a set
14201 * that lives in the domain space of "pma".
14203 __isl_give isl_set
*isl_set_preimage_pw_multi_aff(__isl_take isl_set
*set
,
14204 __isl_take isl_pw_multi_aff
*pma
)
14206 return isl_map_preimage_pw_multi_aff(set
, isl_dim_set
, pma
);
14209 /* Compute the preimage of the domain of "map" under the function
14210 * represented by "pma".
14211 * In other words, plug in "pma" in the domain of "map".
14212 * The result is a map that lives in the same space as "map",
14213 * except that domain space has been replaced by the domain space of "pma".
14215 __isl_give isl_map
*isl_map_preimage_domain_pw_multi_aff(
14216 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
14218 return isl_map_preimage_pw_multi_aff(map
, isl_dim_in
, pma
);
14221 /* Compute the preimage of the range of "map" under the function
14222 * represented by "pma".
14223 * In other words, plug in "pma" in the range of "map".
14224 * The result is a map that lives in the same space as "map",
14225 * except that range space has been replaced by the domain space of "pma".
14227 __isl_give isl_map
*isl_map_preimage_range_pw_multi_aff(
14228 __isl_take isl_map
*map
, __isl_take isl_pw_multi_aff
*pma
)
14230 return isl_map_preimage_pw_multi_aff(map
, isl_dim_out
, pma
);
14233 /* Compute the preimage of "map" under the function represented by "mpa".
14234 * In other words, plug in "mpa" in the domain or range of "map".
14235 * The result is a map that lives in the same space as "map",
14236 * except that the space of type "type" has been replaced by
14237 * the domain space of "mpa".
14239 * If the map does not involve any constraints that refer to the
14240 * dimensions of the substituted space, then the only possible
14241 * effect of "mpa" on the map is to map the space to a different space.
14242 * We create a separate isl_multi_aff to effectuate this change
14243 * in order to avoid spurious splitting of the map along the pieces
14245 * If "mpa" has a non-trivial explicit domain, however,
14246 * then the full substitution should be performed.
14248 __isl_give isl_map
*isl_map_preimage_multi_pw_aff(__isl_take isl_map
*map
,
14249 enum isl_dim_type type
, __isl_take isl_multi_pw_aff
*mpa
)
14253 isl_pw_multi_aff
*pma
;
14255 n
= isl_map_dim(map
, type
);
14259 full
= isl_map_involves_dims(map
, type
, 0, n
);
14260 if (full
>= 0 && !full
)
14261 full
= isl_multi_pw_aff_has_non_trivial_domain(mpa
);
14268 space
= isl_multi_pw_aff_get_space(mpa
);
14269 isl_multi_pw_aff_free(mpa
);
14270 ma
= isl_multi_aff_zero(space
);
14271 return isl_map_preimage_multi_aff(map
, type
, ma
);
14274 pma
= isl_pw_multi_aff_from_multi_pw_aff(mpa
);
14275 return isl_map_preimage_pw_multi_aff(map
, type
, pma
);
14278 isl_multi_pw_aff_free(mpa
);
14282 /* Compute the preimage of "map" under the function represented by "mpa".
14283 * In other words, plug in "mpa" in the domain "map".
14284 * The result is a map that lives in the same space as "map",
14285 * except that domain space has been replaced by the domain space of "mpa".
14287 __isl_give isl_map
*isl_map_preimage_domain_multi_pw_aff(
14288 __isl_take isl_map
*map
, __isl_take isl_multi_pw_aff
*mpa
)
14290 return isl_map_preimage_multi_pw_aff(map
, isl_dim_in
, mpa
);
14293 /* Compute the preimage of "set" by the function represented by "mpa".
14294 * In other words, plug in "mpa" in "set".
14296 __isl_give isl_set
*isl_set_preimage_multi_pw_aff(__isl_take isl_set
*set
,
14297 __isl_take isl_multi_pw_aff
*mpa
)
14299 return isl_map_preimage_multi_pw_aff(set
, isl_dim_set
, mpa
);
14302 /* Return a copy of the equality constraints of "bset" as a matrix.
14304 __isl_give isl_mat
*isl_basic_set_extract_equalities(
14305 __isl_keep isl_basic_set
*bset
)
14310 total
= isl_basic_set_dim(bset
, isl_dim_all
);
14314 ctx
= isl_basic_set_get_ctx(bset
);
14315 return isl_mat_sub_alloc6(ctx
, bset
->eq
, 0, bset
->n_eq
, 0, 1 + total
);
14318 /* Are the "n" "coefficients" starting at "first" of the integer division
14319 * expressions at position "pos1" in "bmap1" and "pos2" in "bmap2" equal
14321 * The "coefficient" at position 0 is the denominator.
14322 * The "coefficient" at position 1 is the constant term.
14324 isl_bool
isl_basic_map_equal_div_expr_part(__isl_keep isl_basic_map
*bmap1
,
14325 int pos1
, __isl_keep isl_basic_map
*bmap2
, int pos2
,
14326 unsigned first
, unsigned n
)
14328 if (isl_basic_map_check_range(bmap1
, isl_dim_div
, pos1
, 1) < 0)
14329 return isl_bool_error
;
14330 if (isl_basic_map_check_range(bmap2
, isl_dim_div
, pos2
, 1) < 0)
14331 return isl_bool_error
;
14332 return isl_seq_eq(bmap1
->div
[pos1
] + first
,
14333 bmap2
->div
[pos2
] + first
, n
);
14336 /* Are the integer division expressions at position "pos1" in "bmap1" and
14337 * "pos2" in "bmap2" equal to each other, except that the constant terms
14340 isl_bool
isl_basic_map_equal_div_expr_except_constant(
14341 __isl_keep isl_basic_map
*bmap1
, int pos1
,
14342 __isl_keep isl_basic_map
*bmap2
, int pos2
)
14345 isl_size total
, total2
;
14347 total
= isl_basic_map_dim(bmap1
, isl_dim_all
);
14348 total2
= isl_basic_map_dim(bmap2
, isl_dim_all
);
14349 if (total
< 0 || total2
< 0)
14350 return isl_bool_error
;
14351 if (total
!= total2
)
14352 isl_die(isl_basic_map_get_ctx(bmap1
), isl_error_invalid
,
14353 "incomparable div expressions", return isl_bool_error
);
14354 equal
= isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
14356 if (equal
< 0 || !equal
)
14358 equal
= isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
14360 if (equal
< 0 || equal
)
14361 return isl_bool_not(equal
);
14362 return isl_basic_map_equal_div_expr_part(bmap1
, pos1
, bmap2
, pos2
,
14366 /* Replace the numerator of the constant term of the integer division
14367 * expression at position "div" in "bmap" by "value".
14368 * The caller guarantees that this does not change the meaning
14371 __isl_give isl_basic_map
*isl_basic_map_set_div_expr_constant_num_si_inplace(
14372 __isl_take isl_basic_map
*bmap
, int div
, int value
)
14374 if (isl_basic_map_check_range(bmap
, isl_dim_div
, div
, 1) < 0)
14375 return isl_basic_map_free(bmap
);
14377 isl_int_set_si(bmap
->div
[div
][1], value
);
14382 /* Is the point "inner" internal to inequality constraint "ineq"
14384 * The point is considered to be internal to the inequality constraint,
14385 * if it strictly lies on the positive side of the inequality constraint,
14386 * or if it lies on the constraint and the constraint is lexico-positive.
14388 static isl_bool
is_internal(__isl_keep isl_vec
*inner
,
14389 __isl_keep isl_basic_set
*bset
, int ineq
)
14395 if (!inner
|| !bset
)
14396 return isl_bool_error
;
14398 ctx
= isl_basic_set_get_ctx(bset
);
14399 isl_seq_inner_product(inner
->el
, bset
->ineq
[ineq
], inner
->size
,
14400 &ctx
->normalize_gcd
);
14401 if (!isl_int_is_zero(ctx
->normalize_gcd
))
14402 return isl_int_is_nonneg(ctx
->normalize_gcd
);
14404 total
= isl_basic_set_dim(bset
, isl_dim_all
);
14406 return isl_bool_error
;
14407 pos
= isl_seq_first_non_zero(bset
->ineq
[ineq
] + 1, total
);
14408 return isl_int_is_pos(bset
->ineq
[ineq
][1 + pos
]);
14411 /* Tighten the inequality constraints of "bset" that are outward with respect
14412 * to the point "vec".
14413 * That is, tighten the constraints that are not satisfied by "vec".
14415 * "vec" is a point internal to some superset S of "bset" that is used
14416 * to make the subsets of S disjoint, by tightening one half of the constraints
14417 * that separate two subsets. In particular, the constraints of S
14418 * are all satisfied by "vec" and should not be tightened.
14419 * Of the internal constraints, those that have "vec" on the outside
14420 * are tightened. The shared facet is included in the adjacent subset
14421 * with the opposite constraint.
14422 * For constraints that saturate "vec", this criterion cannot be used
14423 * to determine which of the two sides should be tightened.
14424 * Instead, the sign of the first non-zero coefficient is used
14425 * to make this choice. Note that this second criterion is never used
14426 * on the constraints of S since "vec" is interior to "S".
14428 __isl_give isl_basic_set
*isl_basic_set_tighten_outward(
14429 __isl_take isl_basic_set
*bset
, __isl_keep isl_vec
*vec
)
14433 bset
= isl_basic_set_cow(bset
);
14436 for (j
= 0; j
< bset
->n_ineq
; ++j
) {
14439 internal
= is_internal(vec
, bset
, j
);
14441 return isl_basic_set_free(bset
);
14444 isl_int_sub_ui(bset
->ineq
[j
][0], bset
->ineq
[j
][0], 1);
14450 /* Replace the variables x of type "type" starting at "first" in "bmap"
14451 * by x' with x = M x' with M the matrix trans.
14452 * That is, replace the corresponding coefficients c by c M.
14454 * The transformation matrix should be a square matrix.
14456 __isl_give isl_basic_map
*isl_basic_map_transform_dims(
14457 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
, unsigned first
,
14458 __isl_take isl_mat
*trans
)
14462 bmap
= isl_basic_map_cow(bmap
);
14463 if (!bmap
|| !trans
)
14466 if (trans
->n_row
!= trans
->n_col
)
14467 isl_die(trans
->ctx
, isl_error_invalid
,
14468 "expecting square transformation matrix", goto error
);
14469 if (isl_basic_map_check_range(bmap
, type
, first
, trans
->n_row
) < 0)
14472 pos
= isl_basic_map_offset(bmap
, type
) + first
;
14474 if (isl_mat_sub_transform(bmap
->eq
, bmap
->n_eq
, pos
,
14475 isl_mat_copy(trans
)) < 0)
14477 if (isl_mat_sub_transform(bmap
->ineq
, bmap
->n_ineq
, pos
,
14478 isl_mat_copy(trans
)) < 0)
14480 if (isl_mat_sub_transform(bmap
->div
, bmap
->n_div
, 1 + pos
,
14481 isl_mat_copy(trans
)) < 0)
14484 ISL_F_CLR(bmap
, ISL_BASIC_MAP_SORTED
);
14485 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
14487 isl_mat_free(trans
);
14490 isl_mat_free(trans
);
14491 isl_basic_map_free(bmap
);
14495 /* Replace the variables x of type "type" starting at "first" in "bset"
14496 * by x' with x = M x' with M the matrix trans.
14497 * That is, replace the corresponding coefficients c by c M.
14499 * The transformation matrix should be a square matrix.
14501 __isl_give isl_basic_set
*isl_basic_set_transform_dims(
14502 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned first
,
14503 __isl_take isl_mat
*trans
)
14505 return isl_basic_map_transform_dims(bset
, type
, first
, trans
);